Hex Artifact Content
Not logged in

Artifact e1c4c35b595c3a5c20c51a24e6ba89cd875ced0f:

File src/sqlite3.c part of check-in [856e23a19b] - Merge with 076f7adff. by dmitry on 2009-10-15 08:01:58. Also file src/sqlite3.c part of check-in [076f7adff2] - Update to the latest version of SQLite. by drh on 2009-10-13 16:25:41.

0000: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
0010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0020: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0030: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0040: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
0050: 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20  ** This file is 
0060: 61 6e 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20  an amalgamation 
0070: 6f 66 20 6d 61 6e 79 20 73 65 70 61 72 61 74 65  of many separate
0080: 20 43 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20   C source files 
0090: 66 72 6f 6d 20 53 51 4c 69 74 65 0a 2a 2a 20 76  from SQLite.** v
00a0: 65 72 73 69 6f 6e 20 33 2e 36 2e 31 39 2e 20 20  ersion 3.6.19.  
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 31 30 2d 31 33 20 31 36 3a 32 32 3a  009-10-13 16:22:
0510: 30 30 20 55 54 43 2e 0a 2a 2f 0a 23 64 65 66 69  00 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 2a 0a 2a 2a   triggers..**.**
2460: 20 41 20 76 61 6c 75 65 20 6f 66 20 31 20 6d 65   A value of 1 me
2470: 61 6e 73 20 74 68 61 74 20 61 20 74 72 69 67 67  ans that a trigg
2480: 65 72 20 70 72 6f 67 72 61 6d 20 77 69 6c 6c 20  er program will 
2490: 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 69  not be able to i
24a0: 74 73 65 6c 66 0a 2a 2a 20 66 69 72 65 20 61 6e  tself.** fire an
24b0: 79 20 74 72 69 67 67 65 72 73 2e 20 41 20 76 61  y triggers. A va
24c0: 6c 75 65 20 6f 66 20 30 20 6d 65 61 6e 73 20 74  lue of 0 means t
24d0: 68 61 74 20 6e 6f 20 74 72 69 67 67 65 72 20 70  hat no trigger p
24e0: 72 6f 67 72 61 6d 73 20 61 74 20 61 6c 6c 20 0a  rograms at all .
24f0: 2a 2a 20 6d 61 79 20 62 65 20 65 78 65 63 75 74  ** may be execut
2500: 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ed..*/.#ifndef S
2510: 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47 47 45  QLITE_MAX_TRIGGE
2520: 52 5f 44 45 50 54 48 0a 23 69 66 20 64 65 66 69  R_DEPTH.#if defi
2530: 6e 65 64 28 53 51 4c 49 54 45 5f 53 4d 41 4c 4c  ned(SQLITE_SMALL
2540: 5f 53 54 41 43 4b 29 0a 23 20 64 65 66 69 6e 65  _STACK).# define
2550: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47   SQLITE_MAX_TRIG
2560: 47 45 52 5f 44 45 50 54 48 20 31 30 0a 23 65 6c  GER_DEPTH 10.#el
2570: 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  se.# define SQLI
2580: 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44  TE_MAX_TRIGGER_D
2590: 45 50 54 48 20 31 30 30 30 0a 23 65 6e 64 69 66  EPTH 1000.#endif
25a0: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
25b0: 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
25c0: 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a  sqliteLimit.h **
25d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25f0: 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
2600: 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
2610: 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
2620: 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49  t off in sqliteI
2630: 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
2640: 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 44 69 73  *******/../* Dis
2650: 61 62 6c 65 20 6e 75 69 73 61 6e 63 65 20 77 61  able nuisance wa
2660: 72 6e 69 6e 67 73 20 6f 6e 20 42 6f 72 6c 61 6e  rnings on Borlan
2670: 64 20 63 6f 6d 70 69 6c 65 72 73 20 2a 2f 0a 23  d compilers */.#
2680: 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52  if defined(__BOR
2690: 4c 41 4e 44 43 5f 5f 29 0a 23 70 72 61 67 6d 61  LANDC__).#pragma
26a0: 20 77 61 72 6e 20 2d 72 63 68 20 2f 2a 20 75 6e   warn -rch /* un
26b0: 72 65 61 63 68 61 62 6c 65 20 63 6f 64 65 20 2a  reachable code *
26c0: 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d  /.#pragma warn -
26d0: 63 63 63 20 2f 2a 20 43 6f 6e 64 69 74 69 6f 6e  ccc /* Condition
26e0: 20 69 73 20 61 6c 77 61 79 73 20 74 72 75 65 20   is always true 
26f0: 6f 72 20 66 61 6c 73 65 20 2a 2f 0a 23 70 72 61  or false */.#pra
2700: 67 6d 61 20 77 61 72 6e 20 2d 61 75 73 20 2f 2a  gma warn -aus /*
2710: 20 41 73 73 69 67 6e 65 64 20 76 61 6c 75 65 20   Assigned value 
2720: 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 2a 2f  is never used */
2730: 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 63  .#pragma warn -c
2740: 73 75 20 2f 2a 20 43 6f 6d 70 61 72 69 6e 67 20  su /* Comparing 
2750: 73 69 67 6e 65 64 20 61 6e 64 20 75 6e 73 69 67  signed and unsig
2760: 6e 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20 77  ned */.#pragma w
2770: 61 72 6e 20 2d 73 70 61 20 2f 2a 20 53 75 73 70  arn -spa /* Susp
2780: 69 63 69 6f 75 73 20 70 6f 69 6e 74 65 72 20 61  icious pointer a
2790: 72 69 74 68 6d 65 74 69 63 20 2a 2f 0a 23 65 6e  rithmetic */.#en
27a0: 64 69 66 0a 0a 2f 2a 20 4e 65 65 64 65 64 20 66  dif../* Needed f
27b0: 6f 72 20 76 61 72 69 6f 75 73 20 64 65 66 69 6e  or various defin
27c0: 69 74 69 6f 6e 73 2e 2e 2e 20 2a 2f 0a 23 69 66  itions... */.#if
27d0: 6e 64 65 66 20 5f 47 4e 55 5f 53 4f 55 52 43 45  ndef _GNU_SOURCE
27e0: 0a 23 20 64 65 66 69 6e 65 20 5f 47 4e 55 5f 53  .# define _GNU_S
27f0: 4f 55 52 43 45 0a 23 65 6e 64 69 66 0a 0a 2f 2a  OURCE.#endif../*
2800: 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 74 61 6e  .** Include stan
2810: 64 61 72 64 20 68 65 61 64 65 72 20 66 69 6c 65  dard header file
2820: 73 20 61 73 20 6e 65 63 65 73 73 61 72 79 0a 2a  s as necessary.*
2830: 2f 0a 23 69 66 64 65 66 20 48 41 56 45 5f 53 54  /.#ifdef HAVE_ST
2840: 44 49 4e 54 5f 48 0a 23 69 6e 63 6c 75 64 65 20  DINT_H.#include 
2850: 3c 73 74 64 69 6e 74 2e 68 3e 0a 23 65 6e 64 69  <stdint.h>.#endi
2860: 66 0a 23 69 66 64 65 66 20 48 41 56 45 5f 49 4e  f.#ifdef HAVE_IN
2870: 54 54 59 50 45 53 5f 48 0a 23 69 6e 63 6c 75 64  TTYPES_H.#includ
2880: 65 20 3c 69 6e 74 74 79 70 65 73 2e 68 3e 0a 23  e <inttypes.h>.#
2890: 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 53  endif..#define S
28a0: 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50  QLITE_INDEX_SAMP
28b0: 4c 45 53 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 54 68  LES 10../*.** Th
28c0: 69 73 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64  is macro is used
28d0: 20 74 6f 20 22 68 69 64 65 22 20 73 6f 6d 65 20   to "hide" some 
28e0: 75 67 6c 69 6e 65 73 73 20 69 6e 20 63 61 73 74  ugliness in cast
28f0: 69 6e 67 20 61 6e 20 69 6e 74 0a 2a 2a 20 76 61  ing an int.** va
2900: 6c 75 65 20 74 6f 20 61 20 70 74 72 20 76 61 6c  lue to a ptr val
2910: 75 65 20 75 6e 64 65 72 20 74 68 65 20 4d 53 56  ue under the MSV
2920: 43 20 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c 65  C 64-bit compile
2930: 72 2e 20 20 20 43 61 73 74 69 6e 67 0a 2a 2a 20  r.   Casting.** 
2940: 6e 6f 6e 20 36 34 2d 62 69 74 20 76 61 6c 75 65  non 64-bit value
2950: 73 20 74 6f 20 70 74 72 20 74 79 70 65 73 20 72  s to ptr types r
2960: 65 73 75 6c 74 73 20 69 6e 20 61 20 22 68 61 72  esults in a "har
2970: 64 22 20 65 72 72 6f 72 20 77 69 74 68 20 0a 2a  d" error with .*
2980: 2a 20 74 68 65 20 4d 53 56 43 20 36 34 2d 62 69  * the MSVC 64-bi
2990: 74 20 63 6f 6d 70 69 6c 65 72 20 77 68 69 63 68  t compiler which
29a0: 20 74 68 69 73 20 61 74 74 65 6d 70 74 73 20 74   this attempts t
29b0: 6f 20 61 76 6f 69 64 2e 20 20 0a 2a 2a 0a 2a 2a  o avoid.  .**.**
29c0: 20 41 20 73 69 6d 70 6c 65 20 63 6f 6d 70 69 6c   A simple compil
29d0: 65 72 20 70 72 61 67 6d 61 20 6f 72 20 63 61 73  er pragma or cas
29e0: 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f  ting sequence co
29f0: 75 6c 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e 64  uld not be found
2a00: 0a 2a 2a 20 74 6f 20 63 6f 72 72 65 63 74 20 74  .** to correct t
2a10: 68 69 73 20 69 6e 20 61 6c 6c 20 73 69 74 75 61  his in all situa
2a20: 74 69 6f 6e 73 2c 20 73 6f 20 74 68 69 73 20 6d  tions, so this m
2a30: 61 63 72 6f 20 77 61 73 20 69 6e 74 72 6f 64 75  acro was introdu
2a40: 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 63 6f  ced..**.** It co
2a50: 75 6c 64 20 62 65 20 61 72 67 75 65 64 20 74 68  uld be argued th
2a60: 61 74 20 74 68 65 20 69 6e 74 70 74 72 5f 74 20  at the intptr_t 
2a70: 74 79 70 65 20 63 6f 75 6c 64 20 62 65 20 75 73  type could be us
2a80: 65 64 20 69 6e 20 74 68 69 73 0a 2a 2a 20 63 61  ed in this.** ca
2a90: 73 65 2c 20 62 75 74 20 74 68 61 74 20 74 79 70  se, but that typ
2aa0: 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62  e is not availab
2ab0: 6c 65 20 6f 6e 20 61 6c 6c 20 63 6f 6d 70 69 6c  le on all compil
2ac0: 65 72 73 2c 20 6f 72 20 0a 2a 2a 20 72 65 71 75  ers, or .** requ
2ad0: 69 72 65 73 20 74 68 65 20 23 69 6e 63 6c 75 64  ires the #includ
2ae0: 65 20 6f 66 20 73 70 65 63 69 66 69 63 20 68 65  e of specific he
2af0: 61 64 65 72 73 20 77 68 69 63 68 20 64 69 66 66  aders which diff
2b00: 65 72 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 70  ers between.** p
2b10: 6c 61 74 66 6f 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20  latforms..**.** 
2b20: 54 69 63 6b 65 74 20 23 33 38 36 30 3a 20 20 54  Ticket #3860:  T
2b30: 68 65 20 6c 6c 76 6d 2d 67 63 63 2d 34 2e 32 20  he llvm-gcc-4.2 
2b40: 63 6f 6d 70 69 6c 65 72 20 66 72 6f 6d 20 41 70  compiler from Ap
2b50: 70 6c 65 20 63 68 6f 6b 65 73 20 6f 6e 0a 2a 2a  ple chokes on.**
2b60: 20 74 68 65 20 28 28 76 6f 69 64 2a 29 26 28 28   the ((void*)&((
2b70: 63 68 61 72 2a 29 30 29 5b 58 5d 29 20 63 6f 6e  char*)0)[X]) con
2b80: 73 74 72 75 63 74 2e 20 20 42 75 74 20 4d 53 56  struct.  But MSV
2b90: 43 20 63 68 6f 6b 65 73 20 6f 6e 20 28 28 76 6f  C chokes on ((vo
2ba0: 69 64 2a 29 28 58 29 29 2e 0a 2a 2a 20 53 6f 20  id*)(X))..** So 
2bb0: 77 65 20 68 61 76 65 20 74 6f 20 64 65 66 69 6e  we have to defin
2bc0: 65 20 74 68 65 20 6d 61 63 72 6f 73 20 69 6e 20  e the macros in 
2bd0: 64 69 66 66 65 72 65 6e 74 20 77 61 79 73 20 64  different ways d
2be0: 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 0a  epending on the.
2bf0: 2a 2a 20 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2f 0a  ** compiler..*/.
2c00: 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e  #if defined(__GN
2c10: 55 43 5f 5f 29 0a 23 20 69 66 20 64 65 66 69 6e  UC__).# if defin
2c20: 65 64 28 48 41 56 45 5f 53 54 44 49 4e 54 5f 48  ed(HAVE_STDINT_H
2c30: 29 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c  ).#   define SQL
2c40: 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 58  ITE_INT_TO_PTR(X
2c50: 29 20 20 28 28 76 6f 69 64 2a 29 28 69 6e 74 70  )  ((void*)(intp
2c60: 74 72 5f 74 29 28 58 29 29 0a 23 20 20 20 64 65  tr_t)(X)).#   de
2c70: 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54 52 5f  fine SQLITE_PTR_
2c80: 54 4f 5f 49 4e 54 28 58 29 20 20 28 28 69 6e 74  TO_INT(X)  ((int
2c90: 29 28 69 6e 74 70 74 72 5f 74 29 28 58 29 29 0a  )(intptr_t)(X)).
2ca0: 23 20 65 6c 73 65 0a 23 20 20 20 64 65 66 69 6e  # else.#   defin
2cb0: 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f  e SQLITE_INT_TO_
2cc0: 50 54 52 28 58 29 20 20 28 28 76 6f 69 64 2a 29  PTR(X)  ((void*)
2cd0: 28 58 29 29 0a 23 20 20 20 64 65 66 69 6e 65 20  (X)).#   define 
2ce0: 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e  SQLITE_PTR_TO_IN
2cf0: 54 28 58 29 20 20 28 28 69 6e 74 29 28 58 29 29  T(X)  ((int)(X))
2d00: 0a 23 20 65 6e 64 69 66 0a 23 65 6c 73 65 0a 23  .# endif.#else.#
2d10: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49   define SQLITE_I
2d20: 4e 54 5f 54 4f 5f 50 54 52 28 58 29 20 20 20 28  NT_TO_PTR(X)   (
2d30: 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72 2a 29  (void*)&((char*)
2d40: 30 29 5b 58 5d 29 0a 23 20 64 65 66 69 6e 65 20  0)[X]).# define 
2d50: 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e  SQLITE_PTR_TO_IN
2d60: 54 28 58 29 20 20 20 28 28 69 6e 74 29 28 28 28  T(X)   ((int)(((
2d70: 63 68 61 72 2a 29 58 29 2d 28 63 68 61 72 2a 29  char*)X)-(char*)
2d80: 30 29 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  0)).#endif.../*.
2d90: 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 54 48  ** The SQLITE_TH
2da0: 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 20 6d  READSAFE macro m
2db0: 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 61  ust be defined a
2dc0: 73 20 65 69 74 68 65 72 20 30 20 6f 72 20 31 2e  s either 0 or 1.
2dd0: 0a 2a 2a 20 4f 6c 64 65 72 20 76 65 72 73 69 6f  .** Older versio
2de0: 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 75 73 65  ns of SQLite use
2df0: 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 54 48  d an optional TH
2e00: 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 2e 0a  READSAFE macro..
2e10: 2a 2a 20 57 65 20 73 75 70 70 6f 72 74 20 74 68  ** We support th
2e20: 61 74 20 66 6f 72 20 6c 65 67 61 63 79 0a 2a 2f  at for legacy.*/
2e30: 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
2e40: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 29  LITE_THREADSAFE)
2e50: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48 52  .#if defined(THR
2e60: 45 41 44 53 41 46 45 29 0a 23 20 64 65 66 69 6e  EADSAFE).# defin
2e70: 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  e SQLITE_THREADS
2e80: 41 46 45 20 54 48 52 45 41 44 53 41 46 45 0a 23  AFE THREADSAFE.#
2e90: 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51  else.# define SQ
2ea0: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20  LITE_THREADSAFE 
2eb0: 31 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  1.#endif.#endif.
2ec0: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54  ./*.** The SQLIT
2ed0: 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41  E_DEFAULT_MEMSTA
2ee0: 54 55 53 20 6d 61 63 72 6f 20 6d 75 73 74 20 62  TUS macro must b
2ef0: 65 20 64 65 66 69 6e 65 64 20 61 73 20 65 69 74  e defined as eit
2f00: 68 65 72 20 30 20 6f 72 20 31 2e 0a 2a 2a 20 49  her 0 or 1..** I
2f10: 74 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65  t determines whe
2f20: 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
2f30: 66 65 61 74 75 72 65 73 20 72 65 6c 61 74 65 64  features related
2f40: 20 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43   to .** SQLITE_C
2f50: 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20  ONFIG_MEMSTATUS 
2f60: 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 62 79  are available by
2f70: 20 64 65 66 61 75 6c 74 20 6f 72 20 6e 6f 74 2e   default or not.
2f80: 20 54 68 69 73 20 76 61 6c 75 65 20 63 61 6e 0a   This value can.
2f90: 2a 2a 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e  ** be overridden
2fa0: 20 61 74 20 72 75 6e 74 69 6d 65 20 75 73 69 6e   at runtime usin
2fb0: 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f  g the sqlite3_co
2fc0: 6e 66 69 67 28 29 20 41 50 49 2e 0a 2a 2f 0a 23  nfig() API..*/.#
2fd0: 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
2fe0: 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54  TE_DEFAULT_MEMST
2ff0: 41 54 55 53 29 0a 23 20 64 65 66 69 6e 65 20 53  ATUS).# define S
3000: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45  QLITE_DEFAULT_ME
3010: 4d 53 54 41 54 55 53 20 31 0a 23 65 6e 64 69 66  MSTATUS 1.#endif
3020: 0a 0a 2f 2a 0a 2a 2a 20 45 78 61 63 74 6c 79 20  ../*.** Exactly 
3030: 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  one of the follo
3040: 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 75 73 74  wing macros must
3050: 20 62 65 20 64 65 66 69 6e 65 64 20 69 6e 20 6f   be defined in o
3060: 72 64 65 72 20 74 6f 0a 2a 2a 20 73 70 65 63 69  rder to.** speci
3070: 66 79 20 77 68 69 63 68 20 6d 65 6d 6f 72 79 20  fy which memory 
3080: 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
3090: 73 74 65 6d 20 74 6f 20 75 73 65 2e 0a 2a 2a 0a  stem to use..**.
30a0: 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 53 59  **     SQLITE_SY
30b0: 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 20 20 20 20  STEM_MALLOC     
30c0: 20 20 20 20 20 2f 2f 20 55 73 65 20 6e 6f 72 6d       // Use norm
30d0: 61 6c 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63  al system malloc
30e0: 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  ().**     SQLITE
30f0: 5f 4d 45 4d 44 45 42 55 47 20 20 20 20 20 20 20  _MEMDEBUG       
3100: 20 20 20 20 20 20 20 20 2f 2f 20 44 65 62 75 67          // Debug
3110: 67 69 6e 67 20 76 65 72 73 69 6f 6e 20 6f 66 20  ging version of 
3120: 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 0a  system malloc().
3130: 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 45  **     SQLITE_ME
3140: 4d 4f 52 59 5f 53 49 5a 45 20 20 20 20 20 20 20  MORY_SIZE       
3150: 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c       // internal
3160: 20 61 6c 6c 6f 63 61 74 6f 72 20 23 31 0a 2a 2a   allocator #1.**
3170: 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 4d 41 50       SQLITE_MMAP
3180: 5f 48 45 41 50 5f 53 49 5a 45 20 20 20 20 20 20  _HEAP_SIZE      
3190: 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c 20 6d     // internal m
31a0: 6d 61 70 28 29 20 61 6c 6c 6f 63 61 74 6f 72 0a  map() allocator.
31b0: 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 50 4f  **     SQLITE_PO
31c0: 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 20  W2_MEMORY_SIZE  
31d0: 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c       // internal
31e0: 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61 6c   power-of-two al
31f0: 6c 6f 63 61 74 6f 72 0a 2a 2a 0a 2a 2a 20 49 66  locator.**.** If
3200: 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61 62 6f   none of the abo
3210: 76 65 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20  ve are defined, 
3220: 74 68 65 6e 20 73 65 74 20 53 51 4c 49 54 45 5f  then set SQLITE_
3230: 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 61 73  SYSTEM_MALLOC as
3240: 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 2e  .** the default.
3250: 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
3260: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41  SQLITE_SYSTEM_MA
3270: 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51  LLOC)+defined(SQ
3280: 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c  LITE_MEMDEBUG)+\
3290: 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c  .    defined(SQL
32a0: 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29  ITE_MEMORY_SIZE)
32b0: 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  +defined(SQLITE_
32c0: 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b  MMAP_HEAP_SIZE)+
32d0: 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51  \.    defined(SQ
32e0: 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59  LITE_POW2_MEMORY
32f0: 5f 53 49 5a 45 29 3e 31 0a 23 20 65 72 72 6f 72  _SIZE)>1.# error
3300: 20 22 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 66   "At most one of
3310: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   the following c
3320: 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 63 6f 6e 66  ompile-time conf
3330: 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e  iguration option
3340: 73 5c 0a 20 69 73 20 61 6c 6c 6f 77 73 3a 20 53  s\. is allows: S
3350: 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c  QLITE_SYSTEM_MAL
3360: 4c 4f 43 2c 20 53 51 4c 49 54 45 5f 4d 45 4d 44  LOC, SQLITE_MEMD
3370: 45 42 55 47 2c 20 53 51 4c 49 54 45 5f 4d 45 4d  EBUG, SQLITE_MEM
3380: 4f 52 59 5f 53 49 5a 45 2c 5c 0a 20 53 51 4c 49  ORY_SIZE,\. SQLI
3390: 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a  TE_MMAP_HEAP_SIZ
33a0: 45 2c 20 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d  E, SQLITE_POW2_M
33b0: 45 4d 4f 52 59 5f 53 49 5a 45 22 0a 23 65 6e 64  EMORY_SIZE".#end
33c0: 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  if.#if defined(S
33d0: 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c  QLITE_SYSTEM_MAL
33e0: 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51 4c  LOC)+defined(SQL
33f0: 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c 0a  ITE_MEMDEBUG)+\.
3400: 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c 49      defined(SQLI
3410: 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 2b  TE_MEMORY_SIZE)+
3420: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d  defined(SQLITE_M
3430: 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b 5c  MAP_HEAP_SIZE)+\
3440: 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c  .    defined(SQL
3450: 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f  ITE_POW2_MEMORY_
3460: 53 49 5a 45 29 3d 3d 30 0a 23 20 64 65 66 69 6e  SIZE)==0.# defin
3470: 65 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f  e SQLITE_SYSTEM_
3480: 4d 41 4c 4c 4f 43 20 31 0a 23 65 6e 64 69 66 0a  MALLOC 1.#endif.
3490: 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45  ./*.** If SQLITE
34a0: 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d  _MALLOC_SOFT_LIM
34b0: 49 54 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20  IT is not zero, 
34c0: 74 68 65 6e 20 74 72 79 20 74 6f 20 6b 65 65 70  then try to keep
34d0: 20 74 68 65 0a 2a 2a 20 73 69 7a 65 73 20 6f 66   the.** sizes of
34e0: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
34f0: 6f 6e 73 20 62 65 6c 6f 77 20 74 68 69 73 20 76  ons below this v
3500: 61 6c 75 65 20 77 68 65 72 65 20 70 6f 73 73 69  alue where possi
3510: 62 6c 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66  ble..*/.#if !def
3520: 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 41 4c 4c  ined(SQLITE_MALL
3530: 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 29 0a 23  OC_SOFT_LIMIT).#
3540: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
3550: 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54  ALLOC_SOFT_LIMIT
3560: 20 31 30 32 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a   1024.#endif../*
3570: 0a 2a 2a 20 57 65 20 6e 65 65 64 20 74 6f 20 64  .** We need to d
3580: 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55  efine _XOPEN_SOU
3590: 52 43 45 20 61 73 20 66 6f 6c 6c 6f 77 73 20 69  RCE as follows i
35a0: 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e 61 62 6c  n order to enabl
35b0: 65 0a 2a 2a 20 72 65 63 75 72 73 69 76 65 20 6d  e.** recursive m
35c0: 75 74 65 78 65 73 20 6f 6e 20 6d 6f 73 74 20 55  utexes on most U
35d0: 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 20 42 75  nix systems.  Bu
35e0: 74 20 4d 61 63 20 4f 53 20 58 20 69 73 20 64 69  t Mac OS X is di
35f0: 66 66 65 72 65 6e 74 2e 0a 2a 2a 20 54 68 65 20  fferent..** The 
3600: 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 64 65  _XOPEN_SOURCE de
3610: 66 69 6e 65 20 63 61 75 73 65 73 20 70 72 6f 62  fine causes prob
3620: 6c 65 6d 73 20 66 6f 72 20 4d 61 63 20 4f 53 20  lems for Mac OS 
3630: 58 20 77 65 20 61 72 65 20 74 6f 6c 64 2c 0a 2a  X we are told,.*
3640: 2a 20 73 6f 20 69 74 20 69 73 20 6f 6d 69 74 74  * so it is omitt
3650: 65 64 20 74 68 65 72 65 2e 20 20 53 65 65 20 74  ed there.  See t
3660: 69 63 6b 65 74 20 23 32 36 37 33 2e 0a 2a 2a 0a  icket #2673..**.
3670: 2a 2a 20 4c 61 74 65 72 20 77 65 20 6c 65 61 72  ** Later we lear
3680: 6e 20 74 68 61 74 20 5f 58 4f 50 45 4e 5f 53 4f  n that _XOPEN_SO
3690: 55 52 43 45 20 69 73 20 70 6f 6f 72 6c 79 20 6f  URCE is poorly o
36a0: 72 20 69 6e 63 6f 72 72 65 63 74 6c 79 0a 2a 2a  r incorrectly.**
36b0: 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 6f 6e 20   implemented on 
36c0: 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e 20 20 53  some systems.  S
36d0: 6f 20 77 65 20 61 76 6f 69 64 20 64 65 66 69 6e  o we avoid defin
36e0: 69 6e 67 20 69 74 20 61 74 20 61 6c 6c 0a 2a 2a  ing it at all.**
36f0: 20 69 66 20 69 74 20 69 73 20 61 6c 72 65 61 64   if it is alread
3700: 79 20 64 65 66 69 6e 65 64 20 6f 72 20 69 66 20  y defined or if 
3710: 69 74 20 69 73 20 75 6e 6e 65 65 64 65 64 20 62  it is unneeded b
3720: 65 63 61 75 73 65 20 77 65 20 61 72 65 0a 2a 2a  ecause we are.**
3730: 20 6e 6f 74 20 64 6f 69 6e 67 20 61 20 74 68 72   not doing a thr
3740: 65 61 64 73 61 66 65 20 62 75 69 6c 64 2e 20 20  eadsafe build.  
3750: 54 69 63 6b 65 74 20 23 32 36 38 31 2e 0a 2a 2a  Ticket #2681..**
3760: 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 69 63  .** See also tic
3770: 6b 65 74 20 23 32 37 34 31 2e 0a 2a 2f 0a 23 69  ket #2741..*/.#i
3780: 66 20 21 64 65 66 69 6e 65 64 28 5f 58 4f 50 45  f !defined(_XOPE
3790: 4e 5f 53 4f 55 52 43 45 29 20 26 26 20 21 64 65  N_SOURCE) && !de
37a0: 66 69 6e 65 64 28 5f 5f 44 41 52 57 49 4e 5f 5f  fined(__DARWIN__
37b0: 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f  ) && !defined(__
37c0: 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49  APPLE__) && SQLI
37d0: 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20  TE_THREADSAFE.# 
37e0: 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53   define _XOPEN_S
37f0: 4f 55 52 43 45 20 35 30 30 20 20 2f 2a 20 4e 65  OURCE 500  /* Ne
3800: 65 64 65 64 20 74 6f 20 65 6e 61 62 6c 65 20 70  eded to enable p
3810: 74 68 72 65 61 64 20 72 65 63 75 72 73 69 76 65  thread recursive
3820: 20 6d 75 74 65 78 65 73 20 2a 2f 0a 23 65 6e 64   mutexes */.#end
3830: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 43  if../*.** The TC
3840: 4c 20 68 65 61 64 65 72 73 20 61 72 65 20 6f 6e  L headers are on
3850: 6c 79 20 6e 65 65 64 65 64 20 77 68 65 6e 20 63  ly needed when c
3860: 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 54 43 4c  ompiling the TCL
3870: 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2f 0a 23 69   bindings..*/.#i
3880: 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
3890: 5f 54 43 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64  _TCL) || defined
38a0: 28 54 43 4c 53 48 29 0a 23 20 69 6e 63 6c 75 64  (TCLSH).# includ
38b0: 65 20 3c 74 63 6c 2e 68 3e 0a 23 65 6e 64 69 66  e <tcl.h>.#endif
38c0: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e 79 20 70 65 6f  ../*.** Many peo
38d0: 70 6c 65 20 61 72 65 20 66 61 69 6c 69 6e 67 20  ple are failing 
38e0: 74 6f 20 73 65 74 20 2d 44 4e 44 45 42 55 47 3d  to set -DNDEBUG=
38f0: 31 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67  1 when compiling
3900: 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 53 65 74 74   SQLite..** Sett
3910: 69 6e 67 20 4e 44 45 42 55 47 20 6d 61 6b 65 73  ing NDEBUG makes
3920: 20 74 68 65 20 63 6f 64 65 20 73 6d 61 6c 6c 65   the code smalle
3930: 72 20 61 6e 64 20 72 75 6e 20 66 61 73 74 65 72  r and run faster
3940: 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77  .  So the follow
3950: 69 6e 67 0a 2a 2a 20 6c 69 6e 65 73 20 61 72 65  ing.** lines are
3960: 20 61 64 64 65 64 20 74 6f 20 61 75 74 6f 6d 61   added to automa
3970: 74 69 63 61 6c 6c 79 20 73 65 74 20 4e 44 45 42  tically set NDEB
3980: 55 47 20 75 6e 6c 65 73 73 20 74 68 65 20 2d 44  UG unless the -D
3990: 53 51 4c 49 54 45 5f 44 45 42 55 47 3d 31 0a 2a  SQLITE_DEBUG=1.*
39a0: 2a 20 6f 70 74 69 6f 6e 20 69 73 20 73 65 74 2e  * option is set.
39b0: 20 20 54 68 75 73 20 4e 44 45 42 55 47 20 62 65    Thus NDEBUG be
39c0: 63 6f 6d 65 73 20 61 6e 20 6f 70 74 2d 69 6e 20  comes an opt-in 
39d0: 72 61 74 68 65 72 20 74 68 61 6e 20 61 6e 20 6f  rather than an o
39e0: 70 74 2d 6f 75 74 0a 2a 2a 20 66 65 61 74 75 72  pt-out.** featur
39f0: 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  e..*/.#if !defin
3a00: 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 21 64  ed(NDEBUG) && !d
3a10: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
3a20: 42 55 47 29 20 0a 23 20 64 65 66 69 6e 65 20 4e  BUG) .# define N
3a30: 44 45 42 55 47 20 31 0a 23 65 6e 64 69 66 0a 0a  DEBUG 1.#endif..
3a40: 2f 2a 0a 2a 2a 20 54 68 65 20 74 65 73 74 63 61  /*.** The testca
3a50: 73 65 28 29 20 6d 61 63 72 6f 20 69 73 20 75 73  se() macro is us
3a60: 65 64 20 74 6f 20 61 69 64 20 69 6e 20 63 6f 76  ed to aid in cov
3a70: 65 72 61 67 65 20 74 65 73 74 69 6e 67 2e 20 20  erage testing.  
3a80: 57 68 65 6e 20 0a 2a 2a 20 64 6f 69 6e 67 20 63  When .** doing c
3a90: 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 2c  overage testing,
3aa0: 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 69   the condition i
3ab0: 6e 73 69 64 65 20 74 68 65 20 61 72 67 75 6d 65  nside the argume
3ac0: 6e 74 20 74 6f 0a 2a 2a 20 74 65 73 74 63 61 73  nt to.** testcas
3ad0: 65 28 29 20 6d 75 73 74 20 62 65 20 65 76 61 6c  e() must be eval
3ae0: 75 61 74 65 64 20 62 6f 74 68 20 74 72 75 65 20  uated both true 
3af0: 61 6e 64 20 66 61 6c 73 65 20 69 6e 20 6f 72 64  and false in ord
3b00: 65 72 20 74 6f 0a 2a 2a 20 67 65 74 20 66 75 6c  er to.** get ful
3b10: 6c 20 62 72 61 6e 63 68 20 63 6f 76 65 72 61 67  l branch coverag
3b20: 65 2e 20 20 54 68 65 20 74 65 73 74 63 61 73 65  e.  The testcase
3b30: 28 29 20 6d 61 63 72 6f 20 69 73 20 69 6e 73 65  () macro is inse
3b40: 72 74 65 64 0a 2a 2a 20 74 6f 20 68 65 6c 70 20  rted.** to help 
3b50: 65 6e 73 75 72 65 20 61 64 65 71 75 61 74 65 20  ensure adequate 
3b60: 74 65 73 74 20 63 6f 76 65 72 61 67 65 20 69 6e  test coverage in
3b70: 20 70 6c 61 63 65 73 20 77 68 65 72 65 20 73 69   places where si
3b80: 6d 70 6c 65 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f  mple.** conditio
3b90: 6e 2f 64 65 63 69 73 69 6f 6e 20 63 6f 76 65 72  n/decision cover
3ba0: 61 67 65 20 69 73 20 69 6e 61 64 65 71 75 61 74  age is inadequat
3bb0: 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  e.  For example,
3bc0: 20 74 65 73 74 63 61 73 65 28 29 0a 2a 2a 20 63   testcase().** c
3bd0: 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6d 61  an be used to ma
3be0: 6b 65 20 73 75 72 65 20 62 6f 75 6e 64 61 72 79  ke sure boundary
3bf0: 20 76 61 6c 75 65 73 20 61 72 65 20 74 65 73 74   values are test
3c00: 65 64 2e 20 20 46 6f 72 0a 2a 2a 20 62 69 74 6d  ed.  For.** bitm
3c10: 61 73 6b 20 74 65 73 74 73 2c 20 74 65 73 74 63  ask tests, testc
3c20: 61 73 65 28 29 20 63 61 6e 20 62 65 20 75 73 65  ase() can be use
3c30: 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 65  d to make sure e
3c40: 61 63 68 20 62 69 74 0a 2a 2a 20 69 73 20 73 69  ach bit.** is si
3c50: 67 6e 69 66 69 63 61 6e 74 20 61 6e 64 20 75 73  gnificant and us
3c60: 65 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 63 65  ed at least once
3c70: 2e 20 20 4f 6e 20 73 77 69 74 63 68 20 73 74 61  .  On switch sta
3c80: 74 65 6d 65 6e 74 73 0a 2a 2a 20 77 68 65 72 65  tements.** where
3c90: 20 6d 75 6c 74 69 70 6c 65 20 63 61 73 65 73 20   multiple cases 
3ca0: 67 6f 20 74 6f 20 74 68 65 20 73 61 6d 65 20 62  go to the same b
3cb0: 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 2c 20 74 65  lock of code, te
3cc0: 73 74 63 61 73 65 28 29 0a 2a 2a 20 63 61 6e 20  stcase().** can 
3cd0: 69 6e 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20  insure that all 
3ce0: 63 61 73 65 73 20 61 72 65 20 65 76 61 6c 75 61  cases are evalua
3cf0: 74 65 64 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 64 65  ted..**.*/.#ifde
3d00: 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47  f SQLITE_COVERAG
3d10: 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52  E_TEST.SQLITE_PR
3d20: 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
3d30: 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e 74  ite3Coverage(int
3d40: 29 3b 0a 23 20 64 65 66 69 6e 65 20 74 65 73 74  );.# define test
3d50: 63 61 73 65 28 58 29 20 20 69 66 28 20 58 20 29  case(X)  if( X )
3d60: 7b 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67  { sqlite3Coverag
3d70: 65 28 5f 5f 4c 49 4e 45 5f 5f 29 3b 20 7d 0a 23  e(__LINE__); }.#
3d80: 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 74 65  else.# define te
3d90: 73 74 63 61 73 65 28 58 29 0a 23 65 6e 64 69 66  stcase(X).#endif
3da0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 45 53 54  ../*.** The TEST
3db0: 4f 4e 4c 59 20 6d 61 63 72 6f 20 69 73 20 75 73  ONLY macro is us
3dc0: 65 64 20 74 6f 20 65 6e 63 6c 6f 73 65 20 76 61  ed to enclose va
3dd0: 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74 69  riable declarati
3de0: 6f 6e 73 20 6f 72 0a 2a 2a 20 6f 74 68 65 72 20  ons or.** other 
3df0: 62 69 74 73 20 6f 66 20 63 6f 64 65 20 74 68 61  bits of code tha
3e00: 74 20 61 72 65 20 6e 65 65 64 65 64 20 74 6f 20  t are needed to 
3e10: 73 75 70 70 6f 72 74 20 74 68 65 20 61 72 67 75  support the argu
3e20: 6d 65 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20  ments.** within 
3e30: 74 65 73 74 63 61 73 65 28 29 20 61 6e 64 20 61  testcase() and a
3e40: 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73 2e 0a  ssert() macros..
3e50: 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
3e60: 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e  NDEBUG) || defin
3e70: 65 64 28 53 51 4c 49 54 45 5f 43 4f 56 45 52 41  ed(SQLITE_COVERA
3e80: 47 45 5f 54 45 53 54 29 0a 23 20 64 65 66 69 6e  GE_TEST).# defin
3e90: 65 20 54 45 53 54 4f 4e 4c 59 28 58 29 20 20 58  e TESTONLY(X)  X
3ea0: 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
3eb0: 54 45 53 54 4f 4e 4c 59 28 58 29 0a 23 65 6e 64  TESTONLY(X).#end
3ec0: 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74 69  if../*.** Someti
3ed0: 6d 65 73 20 77 65 20 6e 65 65 64 20 61 20 73 6d  mes we need a sm
3ee0: 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 63 6f  all amount of co
3ef0: 64 65 20 73 75 63 68 20 61 73 20 61 20 76 61 72  de such as a var
3f00: 69 61 62 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61  iable initializa
3f10: 74 69 6f 6e 0a 2a 2a 20 74 6f 20 73 65 74 75 70  tion.** to setup
3f20: 20 66 6f 72 20 61 20 6c 61 74 65 72 20 61 73 73   for a later ass
3f30: 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 2e  ert() statement.
3f40: 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74    We do not want
3f50: 20 74 68 69 73 20 63 6f 64 65 20 74 6f 0a 2a 2a   this code to.**
3f60: 20 61 70 70 65 61 72 20 77 68 65 6e 20 61 73 73   appear when ass
3f70: 65 72 74 28 29 20 69 73 20 64 69 73 61 62 6c 65  ert() is disable
3f80: 64 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  d.  The followin
3f90: 67 20 6d 61 63 72 6f 20 69 73 20 74 68 65 72 65  g macro is there
3fa0: 66 6f 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20  fore.** used to 
3fb0: 63 6f 6e 74 61 69 6e 20 74 68 61 74 20 73 65 74  contain that set
3fc0: 75 70 20 63 6f 64 65 2e 20 20 54 68 65 20 22 56  up code.  The "V
3fd0: 56 41 22 20 61 63 72 6f 6e 79 6d 20 73 74 61 6e  VA" acronym stan
3fe0: 64 73 20 66 6f 72 0a 2a 2a 20 22 56 65 72 69 66  ds for.** "Verif
3ff0: 69 63 61 74 69 6f 6e 2c 20 56 61 6c 69 64 61 74  ication, Validat
4000: 69 6f 6e 2c 20 61 6e 64 20 41 63 63 72 65 64 69  ion, and Accredi
4010: 74 61 74 69 6f 6e 22 2e 20 20 49 6e 20 6f 74 68  tation".  In oth
4020: 65 72 20 77 6f 72 64 73 2c 20 74 68 65 0a 2a 2a  er words, the.**
4030: 20 63 6f 64 65 20 77 69 74 68 69 6e 20 56 56 41   code within VVA
4040: 5f 4f 4e 4c 59 28 29 20 77 69 6c 6c 20 6f 6e 6c  _ONLY() will onl
4050: 79 20 72 75 6e 20 64 75 72 69 6e 67 20 76 65 72  y run during ver
4060: 69 66 69 63 61 74 69 6f 6e 20 70 72 6f 63 65 73  ification proces
4070: 73 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ses..*/.#ifndef 
4080: 4e 44 45 42 55 47 0a 23 20 64 65 66 69 6e 65 20  NDEBUG.# define 
4090: 56 56 41 5f 4f 4e 4c 59 28 58 29 20 20 58 0a 23  VVA_ONLY(X)  X.#
40a0: 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56 56  else.# define VV
40b0: 41 5f 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69 66  A_ONLY(X).#endif
40c0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 41 4c 57 41  ../*.** The ALWA
40d0: 59 53 20 61 6e 64 20 4e 45 56 45 52 20 6d 61 63  YS and NEVER mac
40e0: 72 6f 73 20 73 75 72 72 6f 75 6e 64 20 62 6f 6f  ros surround boo
40f0: 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 73  lean expressions
4100: 20 77 68 69 63 68 20 0a 2a 2a 20 61 72 65 20 69   which .** are i
4110: 6e 74 65 6e 64 65 64 20 74 6f 20 61 6c 77 61 79  ntended to alway
4120: 73 20 62 65 20 74 72 75 65 20 6f 72 20 66 61 6c  s be true or fal
4130: 73 65 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79  se, respectively
4140: 2e 20 20 53 75 63 68 0a 2a 2a 20 65 78 70 72 65  .  Such.** expre
4150: 73 73 69 6f 6e 73 20 63 6f 75 6c 64 20 62 65 20  ssions could be 
4160: 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65  omitted from the
4170: 20 63 6f 64 65 20 63 6f 6d 70 6c 65 74 65 6c 79   code completely
4180: 2e 20 20 42 75 74 20 74 68 65 79 0a 2a 2a 20 61  .  But they.** a
4190: 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 61  re included in a
41a0: 20 66 65 77 20 63 61 73 65 73 20 69 6e 20 6f 72   few cases in or
41b0: 64 65 72 20 74 6f 20 65 6e 68 61 6e 63 65 20 74  der to enhance t
41c0: 68 65 20 72 65 73 69 6c 69 65 6e 63 65 0a 2a 2a  he resilience.**
41d0: 20 6f 66 20 53 51 4c 69 74 65 20 74 6f 20 75 6e   of SQLite to un
41e0: 65 78 70 65 63 74 65 64 20 62 65 68 61 76 69 6f  expected behavio
41f0: 72 20 2d 20 74 6f 20 6d 61 6b 65 20 74 68 65 20  r - to make the 
4200: 63 6f 64 65 20 22 73 65 6c 66 2d 68 65 61 6c 69  code "self-heali
4210: 6e 67 22 0a 2a 2a 20 6f 72 20 22 64 75 63 74 69  ng".** or "ducti
4220: 6c 65 22 20 72 61 74 68 65 72 20 74 68 61 6e 20  le" rather than 
4230: 62 65 69 6e 67 20 22 62 72 69 74 74 6c 65 22 20  being "brittle" 
4240: 61 6e 64 20 63 72 61 73 68 69 6e 67 20 61 74 20  and crashing at 
4250: 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 68 69 6e  the first.** hin
4260: 74 20 6f 66 20 75 6e 70 6c 61 6e 6e 65 64 20 62  t of unplanned b
4270: 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49  ehavior..**.** I
4280: 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 41  n other words, A
4290: 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20  LWAYS and NEVER 
42a0: 61 72 65 20 61 64 64 65 64 20 66 6f 72 20 64 65  are added for de
42b0: 66 65 6e 73 69 76 65 20 63 6f 64 65 2e 0a 2a 2a  fensive code..**
42c0: 0a 2a 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20 63  .** When doing c
42d0: 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 20  overage testing 
42e0: 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52  ALWAYS and NEVER
42f0: 20 61 72 65 20 68 61 72 64 2d 63 6f 64 65 64 20   are hard-coded 
4300: 74 6f 0a 2a 2a 20 62 65 20 74 72 75 65 20 61 6e  to.** be true an
4310: 64 20 66 61 6c 73 65 20 73 6f 20 74 68 61 74 20  d false so that 
4320: 74 68 65 20 75 6e 72 65 61 63 68 61 62 6c 65 20  the unreachable 
4330: 63 6f 64 65 20 74 68 65 6e 20 73 70 65 63 69 66  code then specif
4340: 79 20 77 69 6c 6c 0a 2a 2a 20 6e 6f 74 20 62 65  y will.** not be
4350: 20 63 6f 75 6e 74 65 64 20 61 73 20 75 6e 74 65   counted as unte
4360: 73 74 65 64 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69  sted code..*/.#i
4370: 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
4380: 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29 0a  _COVERAGE_TEST).
4390: 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28  # define ALWAYS(
43a0: 58 29 20 20 20 20 20 20 28 31 29 0a 23 20 64 65  X)      (1).# de
43b0: 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20  fine NEVER(X)   
43c0: 20 20 20 20 28 30 29 0a 23 65 6c 69 66 20 21 64      (0).#elif !d
43d0: 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 23  efined(NDEBUG).#
43e0: 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28 58   define ALWAYS(X
43f0: 29 20 20 20 20 20 20 28 28 58 29 3f 31 3a 28 61  )      ((X)?1:(a
4400: 73 73 65 72 74 28 30 29 2c 30 29 29 0a 23 20 64  ssert(0),0)).# d
4410: 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20  efine NEVER(X)  
4420: 20 20 20 20 20 28 28 58 29 3f 28 61 73 73 65 72       ((X)?(asser
4430: 74 28 30 29 2c 31 29 3a 30 29 0a 23 65 6c 73 65  t(0),1):0).#else
4440: 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53  .# define ALWAYS
4450: 28 58 29 20 20 20 20 20 20 28 58 29 0a 23 20 64  (X)      (X).# d
4460: 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20  efine NEVER(X)  
4470: 20 20 20 20 20 28 58 29 0a 23 65 6e 64 69 66 0a       (X).#endif.
4480: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f  ./*.** The macro
4490: 20 75 6e 6c 69 6b 65 6c 79 28 29 20 69 73 20 61   unlikely() is a
44a0: 20 68 69 6e 74 20 74 68 61 74 20 73 75 72 72 6f   hint that surro
44b0: 75 6e 64 73 20 61 20 62 6f 6f 6c 65 61 6e 0a 2a  unds a boolean.*
44c0: 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  * expression tha
44d0: 74 20 69 73 20 75 73 75 61 6c 6c 79 20 66 61 6c  t is usually fal
44e0: 73 65 2e 20 20 4d 61 63 72 6f 20 6c 69 6b 65 6c  se.  Macro likel
44f0: 79 28 29 20 73 75 72 72 6f 75 6e 64 73 0a 2a 2a  y() surrounds.**
4500: 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65   a boolean expre
4510: 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 75 73  ssion that is us
4520: 75 61 6c 6c 79 20 74 72 75 65 2e 20 20 47 43 43  ually true.  GCC
4530: 20 69 73 20 61 62 6c 65 20 74 6f 0a 2a 2a 20 75   is able to.** u
4540: 73 65 20 74 68 65 73 65 20 68 69 6e 74 73 20 74  se these hints t
4550: 6f 20 67 65 6e 65 72 61 74 65 20 62 65 74 74 65  o generate bette
4560: 72 20 63 6f 64 65 2c 20 73 6f 6d 65 74 69 6d 65  r code, sometime
4570: 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  s..*/.#if define
4580: 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 30  d(__GNUC__) && 0
4590: 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c 79  .# define likely
45a0: 28 58 29 20 20 20 20 5f 5f 62 75 69 6c 74 69 6e  (X)    __builtin
45b0: 5f 65 78 70 65 63 74 28 28 58 29 2c 31 29 0a 23  _expect((X),1).#
45c0: 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79   define unlikely
45d0: 28 58 29 20 20 5f 5f 62 75 69 6c 74 69 6e 5f 65  (X)  __builtin_e
45e0: 78 70 65 63 74 28 28 58 29 2c 30 29 0a 23 65 6c  xpect((X),0).#el
45f0: 73 65 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b 65  se.# define like
4600: 6c 79 28 58 29 20 20 20 20 21 21 28 58 29 0a 23  ly(X)    !!(X).#
4610: 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79   define unlikely
4620: 28 58 29 20 20 21 21 28 58 29 0a 23 65 6e 64 69  (X)  !!(X).#endi
4630: 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
4640: 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74  ** Include sqlit
4650: 65 33 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  e3.h in the midd
4660: 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e  le of sqliteInt.
4670: 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
4680: 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
4690: 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71  ** Begin file sq
46a0: 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  lite3.h ********
46b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46d0: 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
46e0: 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
46f0: 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
4700: 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
4710: 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
4720: 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
4730: 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
4740: 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
4750: 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
4760: 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
4770: 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
4780: 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
4790: 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
47a0: 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
47b0: 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
47c0: 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
47d0: 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
47e0: 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
47f0: 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
4800: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4810: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4820: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4830: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4840: 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
4850: 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65  s header file de
4860: 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66  fines the interf
4870: 61 63 65 20 74 68 61 74 20 74 68 65 20 53 51 4c  ace that the SQL
4880: 69 74 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 70  ite library.** p
4890: 72 65 73 65 6e 74 73 20 74 6f 20 63 6c 69 65 6e  resents to clien
48a0: 74 20 70 72 6f 67 72 61 6d 73 2e 20 20 49 66 20  t programs.  If 
48b0: 61 20 43 2d 66 75 6e 63 74 69 6f 6e 2c 20 73 74  a C-function, st
48c0: 72 75 63 74 75 72 65 2c 20 64 61 74 61 74 79 70  ructure, datatyp
48d0: 65 2c 0a 2a 2a 20 6f 72 20 63 6f 6e 73 74 61 6e  e,.** or constan
48e0: 74 20 64 65 66 69 6e 69 74 69 6f 6e 20 64 6f 65  t definition doe
48f0: 73 20 6e 6f 74 20 61 70 70 65 61 72 20 69 6e 20  s not appear in 
4900: 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 6e 20  this file, then 
4910: 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20 70  it is.** not a p
4920: 75 62 6c 69 73 68 65 64 20 41 50 49 20 6f 66 20  ublished API of 
4930: 53 51 4c 69 74 65 2c 20 69 73 20 73 75 62 6a 65  SQLite, is subje
4940: 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74  ct to change wit
4950: 68 6f 75 74 0a 2a 2a 20 6e 6f 74 69 63 65 2c 20  hout.** notice, 
4960: 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  and should not b
4970: 65 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20  e referenced by 
4980: 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20 75 73  programs that us
4990: 65 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  e SQLite..**.** 
49a0: 53 6f 6d 65 20 6f 66 20 74 68 65 20 64 65 66 69  Some of the defi
49b0: 6e 69 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65  nitions that are
49c0: 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 61 72   in this file ar
49d0: 65 20 6d 61 72 6b 65 64 20 61 73 0a 2a 2a 20 22  e marked as.** "
49e0: 65 78 70 65 72 69 6d 65 6e 74 61 6c 22 2e 20 20  experimental".  
49f0: 45 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74  Experimental int
4a00: 65 72 66 61 63 65 73 20 61 72 65 20 6e 6f 72 6d  erfaces are norm
4a10: 61 6c 6c 79 20 6e 65 77 0a 2a 2a 20 66 65 61 74  ally new.** feat
4a20: 75 72 65 73 20 72 65 63 65 6e 74 6c 79 20 61 64  ures recently ad
4a30: 64 65 64 20 74 6f 20 53 51 4c 69 74 65 2e 20 20  ded to SQLite.  
4a40: 57 65 20 64 6f 20 6e 6f 74 20 61 6e 74 69 63 69  We do not antici
4a50: 70 61 74 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20  pate changes.** 
4a60: 74 6f 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20  to experimental 
4a70: 69 6e 74 65 72 66 61 63 65 73 20 62 75 74 20 72  interfaces but r
4a80: 65 73 65 72 76 65 20 74 68 65 20 72 69 67 68 74  eserve the right
4a90: 20 74 6f 20 6d 61 6b 65 20 6d 69 6e 6f 72 20 63   to make minor c
4aa0: 68 61 6e 67 65 73 0a 2a 2a 20 69 66 20 65 78 70  hanges.** if exp
4ab0: 65 72 69 65 6e 63 65 20 66 72 6f 6d 20 75 73 65  erience from use
4ac0: 20 22 69 6e 20 74 68 65 20 77 69 6c 64 22 20 73   "in the wild" s
4ad0: 75 67 67 65 73 74 20 73 75 63 68 20 63 68 61 6e  uggest such chan
4ae0: 67 65 73 20 61 72 65 20 70 72 75 64 65 6e 74 2e  ges are prudent.
4af0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 66 66 69 63  .**.** The offic
4b00: 69 61 6c 20 43 2d 6c 61 6e 67 75 61 67 65 20 41  ial C-language A
4b10: 50 49 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  PI documentation
4b20: 20 66 6f 72 20 53 51 4c 69 74 65 20 69 73 20 64   for SQLite is d
4b30: 65 72 69 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 63  erived.** from c
4b40: 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68 69 73 20  omments in this 
4b50: 66 69 6c 65 2e 20 20 54 68 69 73 20 66 69 6c 65  file.  This file
4b60: 20 69 73 20 74 68 65 20 61 75 74 68 6f 72 69 74   is the authorit
4b70: 61 74 69 76 65 20 73 6f 75 72 63 65 0a 2a 2a 20  ative source.** 
4b80: 6f 6e 20 68 6f 77 20 53 51 4c 69 74 65 20 69 6e  on how SQLite in
4b90: 74 65 72 66 61 63 65 73 20 61 72 65 20 73 75 70  terfaces are sup
4ba0: 70 6f 73 65 20 74 6f 20 6f 70 65 72 61 74 65 2e  pose to operate.
4bb0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20  .**.** The name 
4bc0: 6f 66 20 74 68 69 73 20 66 69 6c 65 20 75 6e 64  of this file und
4bd0: 65 72 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  er configuration
4be0: 20 6d 61 6e 61 67 65 6d 65 6e 74 20 69 73 20 22   management is "
4bf0: 73 71 6c 69 74 65 2e 68 2e 69 6e 22 2e 0a 2a 2a  sqlite.h.in"..**
4c00: 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 6d 61   The makefile ma
4c10: 6b 65 73 20 73 6f 6d 65 20 6d 69 6e 6f 72 20 63  kes some minor c
4c20: 68 61 6e 67 65 73 20 74 6f 20 74 68 69 73 20 66  hanges to this f
4c30: 69 6c 65 20 28 73 75 63 68 20 61 73 20 69 6e 73  ile (such as ins
4c40: 65 72 74 69 6e 67 0a 2a 2a 20 74 68 65 20 76 65  erting.** the ve
4c50: 72 73 69 6f 6e 20 6e 75 6d 62 65 72 29 20 61 6e  rsion number) an
4c60: 64 20 63 68 61 6e 67 65 73 20 69 74 73 20 6e 61  d changes its na
4c70: 6d 65 20 74 6f 20 22 73 71 6c 69 74 65 33 2e 68  me to "sqlite3.h
4c80: 22 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20  " as.** part of 
4c90: 74 68 65 20 62 75 69 6c 64 20 70 72 6f 63 65 73  the build proces
4ca0: 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53  s..*/.#ifndef _S
4cb0: 51 4c 49 54 45 33 5f 48 5f 0a 23 64 65 66 69 6e  QLITE3_H_.#defin
4cc0: 65 20 5f 53 51 4c 49 54 45 33 5f 48 5f 0a 23 69  e _SQLITE3_H_.#i
4cd0: 6e 63 6c 75 64 65 20 3c 73 74 64 61 72 67 2e 68  nclude <stdarg.h
4ce0: 3e 20 20 20 20 20 2f 2a 20 4e 65 65 64 65 64 20  >     /* Needed 
4cf0: 66 6f 72 20 74 68 65 20 64 65 66 69 6e 69 74 69  for the definiti
4d00: 6f 6e 20 6f 66 20 76 61 5f 6c 69 73 74 20 2a 2f  on of va_list */
4d10: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72  ../*.** Make sur
4d20: 65 20 77 65 20 63 61 6e 20 63 61 6c 6c 20 74 68  e we can call th
4d30: 69 73 20 73 74 75 66 66 20 66 72 6f 6d 20 43 2b  is stuff from C+
4d40: 2b 2e 0a 2a 2f 0a 23 69 66 20 30 0a 65 78 74 65  +..*/.#if 0.exte
4d50: 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 0a  rn "C" {.#endif.
4d60: 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20  ../*.** Add the 
4d70: 61 62 69 6c 69 74 79 20 74 6f 20 6f 76 65 72 72  ability to overr
4d80: 69 64 65 20 27 65 78 74 65 72 6e 27 0a 2a 2f 0a  ide 'extern'.*/.
4d90: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 45  #ifndef SQLITE_E
4da0: 58 54 45 52 4e 0a 23 20 64 65 66 69 6e 65 20 53  XTERN.# define S
4db0: 51 4c 49 54 45 5f 45 58 54 45 52 4e 20 65 78 74  QLITE_EXTERN ext
4dc0: 65 72 6e 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  ern.#endif..#ifn
4dd0: 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49 0a 23  def SQLITE_API.#
4de0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41   define SQLITE_A
4df0: 50 49 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  PI.#endif.../*.*
4e00: 2a 20 54 68 65 73 65 20 6e 6f 2d 6f 70 20 6d 61  * These no-op ma
4e10: 63 72 6f 73 20 61 72 65 20 75 73 65 64 20 69 6e  cros are used in
4e20: 20 66 72 6f 6e 74 20 6f 66 20 69 6e 74 65 72 66   front of interf
4e30: 61 63 65 73 20 74 6f 20 6d 61 72 6b 20 74 68 6f  aces to mark tho
4e40: 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73  se.** interfaces
4e50: 20 61 73 20 65 69 74 68 65 72 20 64 65 70 72 65   as either depre
4e60: 63 61 74 65 64 20 6f 72 20 65 78 70 65 72 69 6d  cated or experim
4e70: 65 6e 74 61 6c 2e 20 20 4e 65 77 20 61 70 70 6c  ental.  New appl
4e80: 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75  ications.** shou
4e90: 6c 64 20 6e 6f 74 20 75 73 65 20 64 65 70 72 65  ld not use depre
4ea0: 63 61 74 65 64 20 69 6e 74 65 72 66 61 63 65 73  cated interfaces
4eb0: 20 2d 20 74 68 65 79 20 61 72 65 20 73 75 70 70   - they are supp
4ec0: 6f 72 74 20 66 6f 72 20 62 61 63 6b 77 61 72 64  ort for backward
4ed0: 73 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69  s.** compatibili
4ee0: 74 79 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63  ty only.  Applic
4ef0: 61 74 69 6f 6e 20 77 72 69 74 65 72 73 20 73 68  ation writers sh
4f00: 6f 75 6c 64 20 62 65 20 61 77 61 72 65 20 74 68  ould be aware th
4f10: 61 74 0a 2a 2a 20 65 78 70 65 72 69 6d 65 6e 74  at.** experiment
4f20: 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20 61 72  al interfaces ar
4f30: 65 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61  e subject to cha
4f40: 6e 67 65 20 69 6e 20 70 6f 69 6e 74 20 72 65 6c  nge in point rel
4f50: 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  eases..**.** The
4f60: 73 65 20 6d 61 63 72 6f 73 20 75 73 65 64 20 74  se macros used t
4f70: 6f 20 72 65 73 6f 6c 76 65 20 74 6f 20 76 61 72  o resolve to var
4f80: 69 6f 75 73 20 6b 69 6e 64 73 20 6f 66 20 63 6f  ious kinds of co
4f90: 6d 70 69 6c 65 72 20 6d 61 67 69 63 20 74 68 61  mpiler magic tha
4fa0: 74 0a 2a 2a 20 77 6f 75 6c 64 20 67 65 6e 65 72  t.** would gener
4fb0: 61 74 65 20 77 61 72 6e 69 6e 67 20 6d 65 73 73  ate warning mess
4fc0: 61 67 65 73 20 77 68 65 6e 20 74 68 65 79 20 77  ages when they w
4fd0: 65 72 65 20 75 73 65 64 2e 20 20 42 75 74 20 74  ere used.  But t
4fe0: 68 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20  hat.** compiler 
4ff0: 6d 61 67 69 63 20 65 6e 64 65 64 20 75 70 20 67  magic ended up g
5000: 65 6e 65 72 61 74 69 6e 67 20 73 75 63 68 20 61  enerating such a
5010: 20 66 6c 75 72 72 79 20 6f 66 20 62 75 67 20 72   flurry of bug r
5020: 65 70 6f 72 74 73 0a 2a 2a 20 74 68 61 74 20 77  eports.** that w
5030: 65 20 68 61 76 65 20 74 61 6b 65 6e 20 69 74 20  e have taken it 
5040: 61 6c 6c 20 6f 75 74 20 61 6e 64 20 67 6f 6e 65  all out and gone
5050: 20 62 61 63 6b 20 74 6f 20 75 73 69 6e 67 20 73   back to using s
5060: 69 6d 70 6c 65 0a 2a 2a 20 6e 6f 6f 70 20 6d 61  imple.** noop ma
5070: 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  cros..*/.#define
5080: 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54   SQLITE_DEPRECAT
5090: 45 44 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  ED.#define SQLIT
50a0: 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 0a  E_EXPERIMENTAL..
50b0: 2f 2a 0a 2a 2a 20 45 6e 73 75 72 65 20 74 68 65  /*.** Ensure the
50c0: 73 65 20 73 79 6d 62 6f 6c 73 20 77 65 72 65 20  se symbols were 
50d0: 6e 6f 74 20 64 65 66 69 6e 65 64 20 62 79 20 73  not defined by s
50e0: 6f 6d 65 20 70 72 65 76 69 6f 75 73 20 68 65 61  ome previous hea
50f0: 64 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66  der file..*/.#if
5100: 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49  def SQLITE_VERSI
5110: 4f 4e 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54  ON.# undef SQLIT
5120: 45 5f 56 45 52 53 49 4f 4e 0a 23 65 6e 64 69 66  E_VERSION.#endif
5130: 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 56  .#ifdef SQLITE_V
5140: 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 20  ERSION_NUMBER.# 
5150: 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52  undef SQLITE_VER
5160: 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 65 6e 64  SION_NUMBER.#end
5170: 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  if../*.** CAPI3R
5180: 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65  EF: Compile-Time
5190: 20 4c 69 62 72 61 72 79 20 56 65 72 73 69 6f 6e   Library Version
51a0: 20 4e 75 6d 62 65 72 73 20 7b 48 31 30 30 31 30   Numbers {H10010
51b0: 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 2a  } <S60100>.**.**
51c0: 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53   The SQLITE_VERS
51d0: 49 4f 4e 20 61 6e 64 20 53 51 4c 49 54 45 5f 56  ION and SQLITE_V
51e0: 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 23 64  ERSION_NUMBER #d
51f0: 65 66 69 6e 65 73 20 69 6e 0a 2a 2a 20 74 68 65  efines in.** the
5200: 20 73 71 6c 69 74 65 33 2e 68 20 66 69 6c 65 20   sqlite3.h file 
5210: 73 70 65 63 69 66 79 20 74 68 65 20 76 65 72 73  specify the vers
5220: 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 77 69  ion of SQLite wi
5230: 74 68 20 77 68 69 63 68 0a 2a 2a 20 74 68 61 74  th which.** that
5240: 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 20   header file is 
5250: 61 73 73 6f 63 69 61 74 65 64 2e 0a 2a 2a 0a 2a  associated..**.*
5260: 2a 20 54 68 65 20 22 76 65 72 73 69 6f 6e 22 20  * The "version" 
5270: 6f 66 20 53 51 4c 69 74 65 20 69 73 20 61 20 73  of SQLite is a s
5280: 74 72 69 6e 67 20 6f 66 20 74 68 65 20 66 6f 72  tring of the for
5290: 6d 20 22 57 2e 58 2e 59 22 20 6f 72 20 22 57 2e  m "W.X.Y" or "W.
52a0: 58 2e 59 2e 5a 22 2e 0a 2a 2a 20 54 68 65 20 57  X.Y.Z"..** The W
52b0: 20 76 61 6c 75 65 20 69 73 20 6d 61 6a 6f 72 20   value is major 
52c0: 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 61  version number a
52d0: 6e 64 20 69 73 20 61 6c 77 61 79 73 20 33 20 69  nd is always 3 i
52e0: 6e 20 53 51 4c 69 74 65 33 2e 0a 2a 2a 20 54 68  n SQLite3..** Th
52f0: 65 20 57 20 76 61 6c 75 65 20 6f 6e 6c 79 20 63  e W value only c
5300: 68 61 6e 67 65 73 20 77 68 65 6e 20 62 61 63 6b  hanges when back
5310: 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c  wards compatibil
5320: 69 74 79 20 69 73 0a 2a 2a 20 62 72 6f 6b 65 6e  ity is.** broken
5330: 20 61 6e 64 20 77 65 20 69 6e 74 65 6e 64 20 74   and we intend t
5340: 6f 20 6e 65 76 65 72 20 62 72 65 61 6b 20 62 61  o never break ba
5350: 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62  ckwards compatib
5360: 69 6c 69 74 79 2e 0a 2a 2a 20 54 68 65 20 58 20  ility..** The X 
5370: 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d 69 6e  value is the min
5380: 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65  or version numbe
5390: 72 20 61 6e 64 20 6f 6e 6c 79 20 63 68 61 6e 67  r and only chang
53a0: 65 73 20 77 68 65 6e 0a 2a 2a 20 74 68 65 72 65  es when.** there
53b0: 20 61 72 65 20 6d 61 6a 6f 72 20 66 65 61 74 75   are major featu
53c0: 72 65 20 65 6e 68 61 6e 63 65 6d 65 6e 74 73 20  re enhancements 
53d0: 74 68 61 74 20 61 72 65 20 66 6f 72 77 61 72 64  that are forward
53e0: 73 20 63 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a 20  s compatible.** 
53f0: 62 75 74 20 6e 6f 74 20 62 61 63 6b 77 61 72 64  but not backward
5400: 73 20 63 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a 2a  s compatible..**
5410: 20 54 68 65 20 59 20 76 61 6c 75 65 20 69 73 20   The Y value is 
5420: 74 68 65 20 72 65 6c 65 61 73 65 20 6e 75 6d 62  the release numb
5430: 65 72 20 61 6e 64 20 69 73 20 69 6e 63 72 65 6d  er and is increm
5440: 65 6e 74 65 64 20 77 69 74 68 0a 2a 2a 20 65 61  ented with.** ea
5450: 63 68 20 72 65 6c 65 61 73 65 20 62 75 74 20 72  ch release but r
5460: 65 73 65 74 73 20 62 61 63 6b 20 74 6f 20 30 20  esets back to 0 
5470: 77 68 65 6e 65 76 65 72 20 58 20 69 73 20 69 6e  whenever X is in
5480: 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a 20 54 68  cremented..** Th
5490: 65 20 5a 20 76 61 6c 75 65 20 6f 6e 6c 79 20 61  e Z value only a
54a0: 70 70 65 61 72 73 20 6f 6e 20 62 72 61 6e 63 68  ppears on branch
54b0: 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a   releases..**.**
54c0: 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53   The SQLITE_VERS
54d0: 49 4f 4e 5f 4e 55 4d 42 45 52 20 69 73 20 61 6e  ION_NUMBER is an
54e0: 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 73   integer that is
54f0: 20 63 6f 6d 70 75 74 65 64 20 61 73 0a 2a 2a 20   computed as.** 
5500: 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c  follows:.**.** <
5510: 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e  blockquote><pre>
5520: 0a 2a 2a 20 53 51 4c 49 54 45 5f 56 45 52 53 49  .** SQLITE_VERSI
5530: 4f 4e 5f 4e 55 4d 42 45 52 20 3d 20 57 2a 31 30  ON_NUMBER = W*10
5540: 30 30 30 30 30 20 2b 20 58 2a 31 30 30 30 20 2b  00000 + X*1000 +
5550: 20 59 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c   Y.** </pre></bl
5560: 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20  ockquote>.**.** 
5570: 53 69 6e 63 65 20 76 65 72 73 69 6f 6e 20 33 2e  Since version 3.
5580: 36 2e 31 38 2c 20 53 51 4c 69 74 65 20 73 6f 75  6.18, SQLite sou
5590: 72 63 65 20 63 6f 64 65 20 68 61 73 20 62 65 65  rce code has bee
55a0: 6e 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a  n stored in the.
55b0: 2a 2a 20 3c 61 20 68 72 65 66 3d 22 68 74 74 70  ** <a href="http
55c0: 3a 2f 2f 77 77 77 2e 66 6f 73 73 69 6c 2d 73 63  ://www.fossil-sc
55d0: 6d 2e 6f 72 67 2f 22 3e 66 6f 73 73 69 6c 20 63  m.org/">fossil c
55e0: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6d 61 6e  onfiguration man
55f0: 61 67 65 6d 65 6e 74 0a 2a 2a 20 73 79 73 74 65  agement.** syste
5600: 6d 3c 2f 61 3e 2e 20 20 54 68 65 20 53 51 4c 49  m</a>.  The SQLI
5610: 54 45 5f 53 4f 55 52 43 45 5f 49 44 0a 2a 2a 20  TE_SOURCE_ID.** 
5620: 6d 61 63 72 6f 20 69 73 20 61 20 73 74 72 69 6e  macro is a strin
5630: 67 20 77 68 69 63 68 20 69 64 65 6e 74 69 66 69  g which identifi
5640: 65 73 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  es a particular 
5650: 63 68 65 63 6b 2d 69 6e 20 6f 66 20 53 51 4c 69  check-in of SQLi
5660: 74 65 0a 2a 2a 20 77 69 74 68 69 6e 20 69 74 73  te.** within its
5670: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6d   configuration m
5680: 61 6e 61 67 65 6d 65 6e 74 20 73 79 73 74 65 6d  anagement system
5690: 2e 20 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f  .  The string co
56a0: 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 64 61  ntains the.** da
56b0: 74 65 20 61 6e 64 20 74 69 6d 65 20 6f 66 20 74  te and time of t
56c0: 68 65 20 63 68 65 63 6b 2d 69 6e 20 28 55 54 43  he check-in (UTC
56d0: 29 20 61 6e 64 20 61 6e 20 53 48 41 31 20 68 61  ) and an SHA1 ha
56e0: 73 68 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65  sh of the entire
56f0: 0a 2a 2a 20 73 6f 75 72 63 65 20 74 72 65 65 2e  .** source tree.
5700: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
5710: 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72   [sqlite3_libver
5720: 73 69 6f 6e 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c  sion()],.** [sql
5730: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f  ite3_libversion_
5740: 6e 75 6d 62 65 72 28 29 5d 2c 20 5b 73 71 6c 69  number()], [sqli
5750: 74 65 33 5f 73 6f 75 72 63 65 69 64 28 29 5d 2c  te3_sourceid()],
5760: 0a 2a 2a 20 5b 73 71 6c 69 74 65 5f 76 65 72 73  .** [sqlite_vers
5770: 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  ion()] and [sqli
5780: 74 65 5f 73 6f 75 72 63 65 5f 69 64 28 29 5d 2e  te_source_id()].
5790: 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
57a0: 6e 74 73 3a 20 5b 48 31 30 30 31 31 5d 20 5b 48  nts: [H10011] [H
57b0: 31 30 30 31 34 5d 0a 2a 2f 0a 23 64 65 66 69 6e  10014].*/.#defin
57c0: 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e  e SQLITE_VERSION
57d0: 20 20 20 20 20 20 20 20 22 33 2e 36 2e 31 39 22          "3.6.19"
57e0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
57f0: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 33  VERSION_NUMBER 3
5800: 30 30 36 30 31 39 0a 23 64 65 66 69 6e 65 20 53  006019.#define S
5810: 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 20  QLITE_SOURCE_ID 
5820: 20 20 20 20 20 22 32 30 30 39 2d 31 30 2d 31 33       "2009-10-13
5830: 20 31 35 3a 34 32 3a 34 39 20 66 38 39 34 65 62   15:42:49 f894eb
5840: 66 38 36 64 36 62 61 66 63 64 31 34 36 31 66 31  f86d6bafcd1461f1
5850: 30 34 66 35 66 36 37 37 62 33 62 36 61 33 61 61  04f5f677b3b6a3aa
5860: 31 61 22 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  1a"../*.** CAPI3
5870: 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 69  REF: Run-Time Li
5880: 62 72 61 72 79 20 56 65 72 73 69 6f 6e 20 4e 75  brary Version Nu
5890: 6d 62 65 72 73 20 7b 48 31 30 30 32 30 7d 20 3c  mbers {H10020} <
58a0: 53 36 30 31 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f  S60100>.** KEYWO
58b0: 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 76 65 72  RDS: sqlite3_ver
58c0: 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  sion.**.** These
58d0: 20 69 6e 74 65 72 66 61 63 65 73 20 70 72 6f 76   interfaces prov
58e0: 69 64 65 20 74 68 65 20 73 61 6d 65 20 69 6e 66  ide the same inf
58f0: 6f 72 6d 61 74 69 6f 6e 20 61 73 20 74 68 65 20  ormation as the 
5900: 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5d  [SQLITE_VERSION]
5910: 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 56 45 52  ,.** [SQLITE_VER
5920: 53 49 4f 4e 5f 4e 55 4d 42 45 52 5d 2c 20 61 6e  SION_NUMBER], an
5930: 64 20 5b 53 51 4c 49 54 45 5f 53 4f 55 52 43 45  d [SQLITE_SOURCE
5940: 5f 49 44 5d 20 23 64 65 66 69 6e 65 73 20 69 6e  _ID] #defines in
5950: 20 74 68 65 20 68 65 61 64 65 72 2c 0a 2a 2a 20   the header,.** 
5960: 62 75 74 20 61 72 65 20 61 73 73 6f 63 69 61 74  but are associat
5970: 65 64 20 77 69 74 68 20 74 68 65 20 6c 69 62 72  ed with the libr
5980: 61 72 79 20 69 6e 73 74 65 61 64 20 6f 66 20 74  ary instead of t
5990: 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e 20  he header file. 
59a0: 20 43 61 75 74 69 6f 75 73 0a 2a 2a 20 70 72 6f   Cautious.** pro
59b0: 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 69  grammers might i
59c0: 6e 63 6c 75 64 65 20 61 73 73 65 72 74 28 29 20  nclude assert() 
59d0: 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68  statements in th
59e0: 65 69 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  eir application 
59f0: 74 6f 0a 2a 2a 20 76 65 72 69 66 79 20 74 68 61  to.** verify tha
5a00: 74 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65  t values returne
5a10: 64 20 62 79 20 74 68 65 73 65 20 69 6e 74 65 72  d by these inter
5a20: 66 61 63 65 73 20 6d 61 74 63 68 20 74 68 65 20  faces match the 
5a30: 6d 61 63 72 6f 73 20 69 6e 0a 2a 2a 20 74 68 65  macros in.** the
5a40: 20 68 65 61 64 65 72 2c 20 61 6e 64 20 74 68 75   header, and thu
5a50: 73 20 69 6e 73 75 72 65 20 74 68 61 74 20 74 68  s insure that th
5a60: 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 73  e application is
5a70: 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 77 69 74  .** compiled wit
5a80: 68 20 6d 61 74 63 68 69 6e 67 20 6c 69 62 72 61  h matching libra
5a90: 72 79 20 61 6e 64 20 68 65 61 64 65 72 20 66 69  ry and header fi
5aa0: 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63  les..**.** <bloc
5ab0: 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20  kquote><pre>.** 
5ac0: 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
5ad0: 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65  libversion_numbe
5ae0: 72 28 29 3d 3d 53 51 4c 49 54 45 5f 56 45 52 53  r()==SQLITE_VERS
5af0: 49 4f 4e 5f 4e 55 4d 42 45 52 20 29 3b 0a 2a 2a  ION_NUMBER );.**
5b00: 20 61 73 73 65 72 74 28 20 73 74 72 63 6d 70 28   assert( strcmp(
5b10: 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64  sqlite3_sourceid
5b20: 28 29 2c 53 51 4c 49 54 45 5f 53 4f 55 52 43 45  (),SQLITE_SOURCE
5b30: 5f 49 44 29 3d 3d 30 20 29 3b 0a 2a 2a 20 61 73  _ID)==0 );.** as
5b40: 73 65 72 74 28 20 73 74 72 63 6d 70 28 73 71 6c  sert( strcmp(sql
5b50: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 2c  ite3_libversion,
5b60: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 29 3d  SQLITE_VERSION)=
5b70: 3d 30 20 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c  =0 );.** </pre><
5b80: 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a  /blockquote>.**.
5b90: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c  ** The sqlite3_l
5ba0: 69 62 76 65 72 73 69 6f 6e 28 29 20 66 75 6e 63  ibversion() func
5bb0: 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65  tion returns the
5bc0: 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f   same informatio
5bd0: 6e 20 61 73 20 69 73 0a 2a 2a 20 69 6e 20 74 68  n as is.** in th
5be0: 65 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f  e sqlite3_versio
5bf0: 6e 5b 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73 74  n[] string const
5c00: 61 6e 74 2e 20 20 54 68 65 20 66 75 6e 63 74 69  ant.  The functi
5c10: 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 0a 2a  on is provided.*
5c20: 2a 20 66 6f 72 20 75 73 65 20 69 6e 20 44 4c 4c  * for use in DLL
5c30: 73 20 73 69 6e 63 65 20 44 4c 4c 20 75 73 65 72  s since DLL user
5c40: 73 20 75 73 75 61 6c 6c 79 20 64 6f 20 6e 6f 74  s usually do not
5c50: 20 68 61 76 65 20 64 69 72 65 63 74 20 61 63 63   have direct acc
5c60: 65 73 73 20 74 6f 20 73 74 72 69 6e 67 0a 2a 2a  ess to string.**
5c70: 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 74 68 69   constants withi
5c80: 6e 20 74 68 65 20 44 4c 4c 2e 20 20 53 69 6d 69  n the DLL.  Simi
5c90: 6c 61 72 6c 79 2c 20 74 68 65 20 73 71 6c 69 74  larly, the sqlit
5ca0: 65 33 5f 73 6f 75 72 63 65 69 64 28 29 20 66 75  e3_sourceid() fu
5cb0: 6e 63 74 69 6f 6e 0a 2a 2a 20 72 65 74 75 72 6e  nction.** return
5cc0: 73 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72  s the same infor
5cd0: 6d 61 74 69 6f 6e 20 61 73 20 69 73 20 69 6e 20  mation as is in 
5ce0: 74 68 65 20 5b 53 51 4c 49 54 45 5f 53 4f 55 52  the [SQLITE_SOUR
5cf0: 43 45 5f 49 44 5d 20 23 64 65 66 69 6e 65 20 6f  CE_ID] #define o
5d00: 66 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 20  f.** the header 
5d10: 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  file..**.** See 
5d20: 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 5f 76 65  also: [sqlite_ve
5d30: 72 73 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71  rsion()] and [sq
5d40: 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69 64 28 29  lite_source_id()
5d50: 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  ]..**.** Require
5d60: 6d 65 6e 74 73 3a 20 5b 48 31 30 30 32 31 5d 20  ments: [H10021] 
5d70: 5b 48 31 30 30 32 32 5d 20 5b 48 31 30 30 32 33  [H10022] [H10023
5d80: 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
5d90: 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74  const char sqlit
5da0: 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 53  e3_version[] = S
5db0: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0a 53  QLITE_VERSION;.S
5dc0: 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
5dd0: 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c 69  char *sqlite3_li
5de0: 62 76 65 72 73 69 6f 6e 28 76 6f 69 64 29 3b 0a  bversion(void);.
5df0: 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
5e00: 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73   char *sqlite3_s
5e10: 6f 75 72 63 65 69 64 28 76 6f 69 64 29 3b 0a 53  ourceid(void);.S
5e20: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
5e30: 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e  lite3_libversion
5e40: 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29 3b 0a 0a  _number(void);..
5e50: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
5e60: 54 65 73 74 20 54 6f 20 53 65 65 20 49 66 20 54  Test To See If T
5e70: 68 65 20 4c 69 62 72 61 72 79 20 49 73 20 54 68  he Library Is Th
5e80: 72 65 61 64 73 61 66 65 20 7b 48 31 30 31 30 30  readsafe {H10100
5e90: 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 2a  } <S60100>.**.**
5ea0: 20 53 51 4c 69 74 65 20 63 61 6e 20 62 65 20 63   SQLite can be c
5eb0: 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 6f 72 20  ompiled with or 
5ec0: 77 69 74 68 6f 75 74 20 6d 75 74 65 78 65 73 2e  without mutexes.
5ed0: 20 20 57 68 65 6e 0a 2a 2a 20 74 68 65 20 5b 53    When.** the [S
5ee0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
5ef0: 5d 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f 72  ] C preprocessor
5f00: 20 6d 61 63 72 6f 20 69 73 20 31 20 6f 72 20 32   macro is 1 or 2
5f10: 2c 20 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 65  , mutexes.** are
5f20: 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 53 51 4c   enabled and SQL
5f30: 69 74 65 20 69 73 20 74 68 72 65 61 64 73 61 66  ite is threadsaf
5f40: 65 2e 20 20 57 68 65 6e 20 74 68 65 0a 2a 2a 20  e.  When the.** 
5f50: 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41  [SQLITE_THREADSA
5f60: 46 45 5d 20 6d 61 63 72 6f 20 69 73 20 30 2c 20  FE] macro is 0, 
5f70: 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 65 73 20  .** the mutexes 
5f80: 61 72 65 20 6f 6d 69 74 74 65 64 2e 20 20 57 69  are omitted.  Wi
5f90: 74 68 6f 75 74 20 74 68 65 20 6d 75 74 65 78 65  thout the mutexe
5fa0: 73 2c 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66  s, it is not saf
5fb0: 65 0a 2a 2a 20 74 6f 20 75 73 65 20 53 51 4c 69  e.** to use SQLi
5fc0: 74 65 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 20  te concurrently 
5fd0: 66 72 6f 6d 20 6d 6f 72 65 20 74 68 61 6e 20 6f  from more than o
5fe0: 6e 65 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a  ne thread..**.**
5ff0: 20 45 6e 61 62 6c 69 6e 67 20 6d 75 74 65 78 65   Enabling mutexe
6000: 73 20 69 6e 63 75 72 73 20 61 20 6d 65 61 73 75  s incurs a measu
6010: 72 61 62 6c 65 20 70 65 72 66 6f 72 6d 61 6e 63  rable performanc
6020: 65 20 70 65 6e 61 6c 74 79 2e 0a 2a 2a 20 53 6f  e penalty..** So
6030: 20 69 66 20 73 70 65 65 64 20 69 73 20 6f 66 20   if speed is of 
6040: 75 74 6d 6f 73 74 20 69 6d 70 6f 72 74 61 6e 63  utmost importanc
6050: 65 2c 20 69 74 20 6d 61 6b 65 73 20 73 65 6e 73  e, it makes sens
6060: 65 20 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20  e to disable.** 
6070: 74 68 65 20 6d 75 74 65 78 65 73 2e 20 20 42 75  the mutexes.  Bu
6080: 74 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 73 61  t for maximum sa
6090: 66 65 74 79 2c 20 6d 75 74 65 78 65 73 20 73 68  fety, mutexes sh
60a0: 6f 75 6c 64 20 62 65 20 65 6e 61 62 6c 65 64 2e  ould be enabled.
60b0: 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20  .** The default 
60c0: 62 65 68 61 76 69 6f 72 20 69 73 20 66 6f 72 20  behavior is for 
60d0: 6d 75 74 65 78 65 73 20 74 6f 20 62 65 20 65 6e  mutexes to be en
60e0: 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  abled..**.** Thi
60f0: 73 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20  s interface can 
6100: 62 65 20 75 73 65 64 20 62 79 20 61 6e 20 61 70  be used by an ap
6110: 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20 6d 61 6b  plication to mak
6120: 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 0a  e sure that the.
6130: 2a 2a 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51  ** version of SQ
6140: 4c 69 74 65 20 74 68 61 74 20 69 74 20 69 73 20  Lite that it is 
6150: 6c 69 6e 6b 69 6e 67 20 61 67 61 69 6e 73 74 20  linking against 
6160: 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74  was compiled wit
6170: 68 0a 2a 2a 20 74 68 65 20 64 65 73 69 72 65 64  h.** the desired
6180: 20 73 65 74 74 69 6e 67 20 6f 66 20 74 68 65 20   setting of the 
6190: 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41  [SQLITE_THREADSA
61a0: 46 45 5d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a  FE] macro..**.**
61b0: 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   This interface 
61c0: 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20 6f 6e 20  only reports on 
61d0: 74 68 65 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  the compile-time
61e0: 20 6d 75 74 65 78 20 73 65 74 74 69 6e 67 0a 2a   mutex setting.*
61f0: 2a 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45  * of the [SQLITE
6200: 5f 54 48 52 45 41 44 53 41 46 45 5d 20 66 6c 61  _THREADSAFE] fla
6210: 67 2e 20 20 49 66 20 53 51 4c 69 74 65 20 69 73  g.  If SQLite is
6220: 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a   compiled with.*
6230: 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  * SQLITE_THREADS
6240: 41 46 45 3d 31 20 74 68 65 6e 20 6d 75 74 65 78  AFE=1 then mutex
6250: 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20 62  es are enabled b
6260: 79 20 64 65 66 61 75 6c 74 20 62 75 74 0a 2a 2a  y default but.**
6270: 20 63 61 6e 20 62 65 20 66 75 6c 6c 79 20 6f 72   can be fully or
6280: 20 70 61 72 74 69 61 6c 6c 79 20 64 69 73 61 62   partially disab
6290: 6c 65 64 20 75 73 69 6e 67 20 61 20 63 61 6c 6c  led using a call
62a0: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e   to [sqlite3_con
62b0: 66 69 67 28 29 5d 0a 2a 2a 20 77 69 74 68 20 74  fig()].** with t
62c0: 68 65 20 76 65 72 62 73 20 5b 53 51 4c 49 54 45  he verbs [SQLITE
62d0: 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48  _CONFIG_SINGLETH
62e0: 52 45 41 44 5d 2c 20 5b 53 51 4c 49 54 45 5f 43  READ], [SQLITE_C
62f0: 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41  ONFIG_MULTITHREA
6300: 44 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54  D],.** or [SQLIT
6310: 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 2e  E_CONFIG_MUTEX].
6320: 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c    The return val
6330: 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74  ue of this funct
6340: 69 6f 6e 20 73 68 6f 77 73 0a 2a 2a 20 6f 6e 6c  ion shows.** onl
6350: 79 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f  y the default co
6360: 6d 70 69 6c 65 2d 74 69 6d 65 20 73 65 74 74 69  mpile-time setti
6370: 6e 67 2c 20 6e 6f 74 20 61 6e 79 20 72 75 6e 2d  ng, not any run-
6380: 74 69 6d 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20  time changes.** 
6390: 74 6f 20 74 68 61 74 20 73 65 74 74 69 6e 67 2e  to that setting.
63a0: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 74 68 65 20 5b  .**.** See the [
63b0: 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20  threading mode] 
63c0: 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f  documentation fo
63d0: 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
63e0: 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  ormation..**.** 
63f0: 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48  Requirements: [H
6400: 31 30 31 30 31 5d 20 5b 48 31 30 31 30 32 5d 0a  10101] [H10102].
6410: 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
6420: 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64  t sqlite3_thread
6430: 73 61 66 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a  safe(void);../*.
6440: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74  ** CAPI3REF: Dat
6450: 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e  abase Connection
6460: 20 48 61 6e 64 6c 65 20 7b 48 31 32 30 30 30 7d   Handle {H12000}
6470: 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 20 4b 45 59   <S40200>.** KEY
6480: 57 4f 52 44 53 3a 20 7b 64 61 74 61 62 61 73 65  WORDS: {database
6490: 20 63 6f 6e 6e 65 63 74 69 6f 6e 7d 20 7b 64 61   connection} {da
64a0: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
64b0: 6e 73 7d 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 6f  ns}.**.** Each o
64c0: 70 65 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62  pen SQLite datab
64d0: 61 73 65 20 69 73 20 72 65 70 72 65 73 65 6e 74  ase is represent
64e0: 65 64 20 62 79 20 61 20 70 6f 69 6e 74 65 72 20  ed by a pointer 
64f0: 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  to an instance o
6500: 66 0a 2a 2a 20 74 68 65 20 6f 70 61 71 75 65 20  f.** the opaque 
6510: 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20  structure named 
6520: 22 73 71 6c 69 74 65 33 22 2e 20 20 49 74 20 69  "sqlite3".  It i
6530: 73 20 75 73 65 66 75 6c 20 74 6f 20 74 68 69 6e  s useful to thin
6540: 6b 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 0a  k of an sqlite3.
6550: 2a 2a 20 70 6f 69 6e 74 65 72 20 61 73 20 61 6e  ** pointer as an
6560: 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 5b 73   object.  The [s
6570: 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20  qlite3_open()], 
6580: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28  [sqlite3_open16(
6590: 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69  )], and.** [sqli
65a0: 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69  te3_open_v2()] i
65b0: 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 69 74  nterfaces are it
65c0: 73 20 63 6f 6e 73 74 72 75 63 74 6f 72 73 2c 20  s constructors, 
65d0: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f  and [sqlite3_clo
65e0: 73 65 28 29 5d 0a 2a 2a 20 69 73 20 69 74 73 20  se()].** is its 
65f0: 64 65 73 74 72 75 63 74 6f 72 2e 20 20 54 68 65  destructor.  The
6600: 72 65 20 61 72 65 20 6d 61 6e 79 20 6f 74 68 65  re are many othe
6610: 72 20 69 6e 74 65 72 66 61 63 65 73 20 28 73 75  r interfaces (su
6620: 63 68 20 61 73 0a 2a 2a 20 5b 73 71 6c 69 74 65  ch as.** [sqlite
6630: 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c  3_prepare_v2()],
6640: 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
6650: 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 2c 20 61 6e  _function()], an
6660: 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 75  d.** [sqlite3_bu
6670: 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d 20 74 6f  sy_timeout()] to
6680: 20 6e 61 6d 65 20 62 75 74 20 74 68 72 65 65 29   name but three)
6690: 20 74 68 61 74 20 61 72 65 20 6d 65 74 68 6f 64   that are method
66a0: 73 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74  s on an.** sqlit
66b0: 65 33 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79  e3 object..*/.ty
66c0: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
66d0: 69 74 65 33 20 73 71 6c 69 74 65 33 3b 0a 0a 2f  ite3 sqlite3;../
66e0: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 36  *.** CAPI3REF: 6
66f0: 34 2d 42 69 74 20 49 6e 74 65 67 65 72 20 54 79  4-Bit Integer Ty
6700: 70 65 73 20 7b 48 31 30 32 30 30 7d 20 3c 53 31  pes {H10200} <S1
6710: 30 31 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  0110>.** KEYWORD
6720: 53 3a 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  S: sqlite_int64 
6730: 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 0a 2a 2a  sqlite_uint64.**
6740: 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 72  .** Because ther
6750: 65 20 69 73 20 6e 6f 20 63 72 6f 73 73 2d 70 6c  e is no cross-pl
6760: 61 74 66 6f 72 6d 20 77 61 79 20 74 6f 20 73 70  atform way to sp
6770: 65 63 69 66 79 20 36 34 2d 62 69 74 20 69 6e 74  ecify 64-bit int
6780: 65 67 65 72 20 74 79 70 65 73 0a 2a 2a 20 53 51  eger types.** SQ
6790: 4c 69 74 65 20 69 6e 63 6c 75 64 65 73 20 74 79  Lite includes ty
67a0: 70 65 64 65 66 73 20 66 6f 72 20 36 34 2d 62 69  pedefs for 64-bi
67b0: 74 20 73 69 67 6e 65 64 20 61 6e 64 20 75 6e 73  t signed and uns
67c0: 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a  igned integers..
67d0: 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
67e0: 33 5f 69 6e 74 36 34 20 61 6e 64 20 73 71 6c 69  3_int64 and sqli
67f0: 74 65 33 5f 75 69 6e 74 36 34 20 61 72 65 20 74  te3_uint64 are t
6800: 68 65 20 70 72 65 66 65 72 72 65 64 20 74 79 70  he preferred typ
6810: 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 2a  e definitions..*
6820: 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 69 6e 74  * The sqlite_int
6830: 36 34 20 61 6e 64 20 73 71 6c 69 74 65 5f 75 69  64 and sqlite_ui
6840: 6e 74 36 34 20 74 79 70 65 73 20 61 72 65 20 73  nt64 types are s
6850: 75 70 70 6f 72 74 65 64 20 66 6f 72 20 62 61 63  upported for bac
6860: 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 61 74  kwards.** compat
6870: 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 0a 2a 2a  ibility only..**
6880: 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
6890: 3a 20 5b 48 31 30 32 30 31 5d 20 5b 48 31 30 32  : [H10201] [H102
68a0: 30 32 5d 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  02].*/.#ifdef SQ
68b0: 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 0a  LITE_INT64_TYPE.
68c0: 20 20 74 79 70 65 64 65 66 20 53 51 4c 49 54 45    typedef SQLITE
68d0: 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c 69  _INT64_TYPE sqli
68e0: 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65  te_int64;.  type
68f0: 64 65 66 20 75 6e 73 69 67 6e 65 64 20 53 51 4c  def unsigned SQL
6900: 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73  ITE_INT64_TYPE s
6910: 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65  qlite_uint64;.#e
6920: 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43  lif defined(_MSC
6930: 5f 56 45 52 29 20 7c 7c 20 64 65 66 69 6e 65 64  _VER) || defined
6940: 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 20  (__BORLANDC__). 
6950: 20 74 79 70 65 64 65 66 20 5f 5f 69 6e 74 36 34   typedef __int64
6960: 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20   sqlite_int64;. 
6970: 20 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65   typedef unsigne
6980: 64 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  d __int64 sqlite
6990: 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 73 65 0a 20  _uint64;.#else. 
69a0: 20 74 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f   typedef long lo
69b0: 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 69 6e  ng int sqlite_in
69c0: 74 36 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75  t64;.  typedef u
69d0: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
69e0: 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 75 69 6e  g int sqlite_uin
69f0: 74 36 34 3b 0a 23 65 6e 64 69 66 0a 74 79 70 65  t64;.#endif.type
6a00: 64 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  def sqlite_int64
6a10: 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 3b 0a   sqlite3_int64;.
6a20: 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f 75  typedef sqlite_u
6a30: 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 75 69  int64 sqlite3_ui
6a40: 6e 74 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  nt64;../*.** If 
6a50: 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20  compiling for a 
6a60: 70 72 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c  processor that l
6a70: 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f  acks floating po
6a80: 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20  int support,.** 
6a90: 73 75 62 73 74 69 74 75 74 65 20 69 6e 74 65 67  substitute integ
6aa0: 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d  er for floating-
6ab0: 70 6f 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 65 66  point..*/.#ifdef
6ac0: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
6ad0: 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65  ATING_POINT.# de
6ae0: 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69  fine double sqli
6af0: 74 65 33 5f 69 6e 74 36 34 0a 23 65 6e 64 69 66  te3_int64.#endif
6b00: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
6b10: 3a 20 43 6c 6f 73 69 6e 67 20 41 20 44 61 74 61  : Closing A Data
6b20: 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20  base Connection 
6b30: 7b 48 31 32 30 31 30 7d 20 3c 53 33 30 31 30 30  {H12010} <S30100
6b40: 3e 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20  ><S40200>.**.** 
6b50: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
6b60: 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66  the destructor f
6b70: 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d  or the [sqlite3]
6b80: 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41   object..**.** A
6b90: 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f 75  pplications shou
6ba0: 6c 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61  ld [sqlite3_fina
6bb0: 6c 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 5d  lize | finalize]
6bc0: 20 61 6c 6c 20 5b 70 72 65 70 61 72 65 64 20 73   all [prepared s
6bd0: 74 61 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20 61 6e  tatements].** an
6be0: 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  d [sqlite3_blob_
6bf0: 63 6c 6f 73 65 20 7c 20 63 6c 6f 73 65 5d 20 61  close | close] a
6c00: 6c 6c 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 73  ll [BLOB handles
6c10: 5d 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  ] associated wit
6c20: 68 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65  h.** the [sqlite
6c30: 33 5d 20 6f 62 6a 65 63 74 20 70 72 69 6f 72 20  3] object prior 
6c40: 74 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f  to attempting to
6c50: 20 63 6c 6f 73 65 20 74 68 65 20 6f 62 6a 65 63   close the objec
6c60: 74 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74  t..** The [sqlit
6c70: 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 29 5d 20  e3_next_stmt()] 
6c80: 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 65  interface can be
6c90: 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20   used to locate 
6ca0: 61 6c 6c 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64  all.** [prepared
6cb0: 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 61 73 73   statements] ass
6cc0: 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 5b  ociated with a [
6cd0: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
6ce0: 69 6f 6e 5d 20 69 66 20 64 65 73 69 72 65 64 2e  ion] if desired.
6cf0: 0a 2a 2a 20 54 79 70 69 63 61 6c 20 63 6f 64 65  .** Typical code
6d00: 20 6d 69 67 68 74 20 6c 6f 6f 6b 20 6c 69 6b 65   might look like
6d10: 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c   this:.**.** <bl
6d20: 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a  ockquote><pre>.*
6d30: 2a 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  * sqlite3_stmt *
6d40: 70 53 74 6d 74 3b 0a 2a 2a 20 77 68 69 6c 65 28  pStmt;.** while(
6d50: 20 28 70 53 74 6d 74 20 3d 20 73 71 6c 69 74 65   (pStmt = sqlite
6d60: 33 5f 6e 65 78 74 5f 73 74 6d 74 28 64 62 2c 20  3_next_stmt(db, 
6d70: 30 29 29 21 3d 30 20 29 7b 0a 2a 2a 20 26 6e 62  0))!=0 ){.** &nb
6d80: 73 70 3b 20 20 20 73 71 6c 69 74 65 33 5f 66 69  sp;   sqlite3_fi
6d90: 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 2a  nalize(pStmt);.*
6da0: 2a 20 7d 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62  * }.** </pre></b
6db0: 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a  lockquote>.**.**
6dc0: 20 49 66 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f   If [sqlite3_clo
6dd0: 73 65 28 29 5d 20 69 73 20 69 6e 76 6f 6b 65 64  se()] is invoked
6de0: 20 77 68 69 6c 65 20 61 20 74 72 61 6e 73 61 63   while a transac
6df0: 74 69 6f 6e 20 69 73 20 6f 70 65 6e 2c 0a 2a 2a  tion is open,.**
6e00: 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
6e10: 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   is automaticall
6e20: 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a  y rolled back..*
6e30: 2a 0a 2a 2a 20 54 68 65 20 43 20 70 61 72 61 6d  *.** The C param
6e40: 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33  eter to [sqlite3
6e50: 5f 63 6c 6f 73 65 28 43 29 5d 20 6d 75 73 74 20  _close(C)] must 
6e60: 62 65 20 65 69 74 68 65 72 20 61 20 4e 55 4c 4c  be either a NULL
6e70: 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 6f 72 20 61  .** pointer or a
6e80: 6e 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a 65  n [sqlite3] obje
6e90: 63 74 20 70 6f 69 6e 74 65 72 20 6f 62 74 61 69  ct pointer obtai
6ea0: 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c  ned.** from [sql
6eb0: 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73  ite3_open()], [s
6ec0: 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d  qlite3_open16()]
6ed0: 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  , or.** [sqlite3
6ee0: 5f 6f 70 65 6e 5f 76 32 28 29 5d 2c 20 61 6e 64  _open_v2()], and
6ef0: 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20   not previously 
6f00: 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65  closed..**.** Re
6f10: 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
6f20: 48 31 32 30 31 31 5d 20 5b 48 31 32 30 31 32 5d  H12011] [H12012]
6f30: 20 5b 48 31 32 30 31 33 5d 20 5b 48 31 32 30 31   [H12013] [H1201
6f40: 34 5d 20 5b 48 31 32 30 31 35 5d 20 5b 48 31 32  4] [H12015] [H12
6f50: 30 31 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  019].*/.SQLITE_A
6f60: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
6f70: 6c 6f 73 65 28 73 71 6c 69 74 65 33 20 2a 29 3b  lose(sqlite3 *);
6f80: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65  ../*.** The type
6f90: 20 66 6f 72 20 61 20 63 61 6c 6c 62 61 63 6b 20   for a callback 
6fa0: 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69  function..** Thi
6fb0: 73 20 69 73 20 6c 65 67 61 63 79 20 61 6e 64 20  s is legacy and 
6fc0: 64 65 70 72 65 63 61 74 65 64 2e 20 20 49 74 20  deprecated.  It 
6fd0: 69 73 20 69 6e 63 6c 75 64 65 64 20 66 6f 72 20  is included for 
6fe0: 68 69 73 74 6f 72 69 63 61 6c 0a 2a 2a 20 63 6f  historical.** co
6ff0: 6d 70 61 74 69 62 69 6c 69 74 79 20 61 6e 64 20  mpatibility and 
7000: 69 73 20 6e 6f 74 20 64 6f 63 75 6d 65 6e 74 65  is not documente
7010: 64 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 69 6e  d..*/.typedef in
7020: 74 20 28 2a 73 71 6c 69 74 65 33 5f 63 61 6c 6c  t (*sqlite3_call
7030: 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  back)(void*,int,
7040: 63 68 61 72 2a 2a 2c 20 63 68 61 72 2a 2a 29 3b  char**, char**);
7050: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
7060: 3a 20 4f 6e 65 2d 53 74 65 70 20 51 75 65 72 79  : One-Step Query
7070: 20 45 78 65 63 75 74 69 6f 6e 20 49 6e 74 65 72   Execution Inter
7080: 66 61 63 65 20 7b 48 31 32 31 30 30 7d 20 3c 53  face {H12100} <S
7090: 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  10000>.**.** The
70a0: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
70b0: 69 6e 74 65 72 66 61 63 65 20 69 73 20 61 20 63  interface is a c
70c0: 6f 6e 76 65 6e 69 65 6e 74 20 77 61 79 20 6f 66  onvenient way of
70d0: 20 72 75 6e 6e 69 6e 67 20 6f 6e 65 20 6f 72 20   running one or 
70e0: 6d 6f 72 65 0a 2a 2a 20 53 51 4c 20 73 74 61 74  more.** SQL stat
70f0: 65 6d 65 6e 74 73 20 77 69 74 68 6f 75 74 20 68  ements without h
7100: 61 76 69 6e 67 20 74 6f 20 77 72 69 74 65 20 61  aving to write a
7110: 20 6c 6f 74 20 6f 66 20 43 20 63 6f 64 65 2e 20   lot of C code. 
7120: 20 54 68 65 20 55 54 46 2d 38 20 65 6e 63 6f 64   The UTF-8 encod
7130: 65 64 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d  ed.** SQL statem
7140: 65 6e 74 73 20 61 72 65 20 70 61 73 73 65 64 20  ents are passed 
7150: 69 6e 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  in as the second
7160: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71   parameter to sq
7170: 6c 69 74 65 33 5f 65 78 65 63 28 29 2e 0a 2a 2a  lite3_exec()..**
7180: 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 73 20   The statements 
7190: 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 6f 6e  are evaluated on
71a0: 65 20 62 79 20 6f 6e 65 20 75 6e 74 69 6c 20 65  e by one until e
71b0: 69 74 68 65 72 20 61 6e 20 65 72 72 6f 72 20 6f  ither an error o
71c0: 72 0a 2a 2a 20 61 6e 20 69 6e 74 65 72 72 75 70  r.** an interrup
71d0: 74 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  t is encountered
71e0: 2c 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 79 20  , or until they 
71f0: 61 72 65 20 61 6c 6c 20 64 6f 6e 65 2e 20 20 54  are all done.  T
7200: 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72  he 3rd parameter
7210: 0a 2a 2a 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e  .** is an option
7220: 61 6c 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74  al callback that
7230: 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65   is invoked once
7240: 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f 66   for each row of
7250: 20 61 6e 79 20 71 75 65 72 79 0a 2a 2a 20 72 65   any query.** re
7260: 73 75 6c 74 73 20 70 72 6f 64 75 63 65 64 20 62  sults produced b
7270: 79 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  y the SQL statem
7280: 65 6e 74 73 2e 20 20 54 68 65 20 35 74 68 20 70  ents.  The 5th p
7290: 61 72 61 6d 65 74 65 72 20 74 65 6c 6c 73 20 77  arameter tells w
72a0: 68 65 72 65 0a 2a 2a 20 74 6f 20 77 72 69 74 65  here.** to write
72b0: 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61   any error messa
72c0: 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65  ges..**.** The e
72d0: 72 72 6f 72 20 6d 65 73 73 61 67 65 20 70 61 73  rror message pas
72e0: 73 65 64 20 62 61 63 6b 20 74 68 72 6f 75 67 68  sed back through
72f0: 20 74 68 65 20 35 74 68 20 70 61 72 61 6d 65 74   the 5th paramet
7300: 65 72 20 69 73 20 68 65 6c 64 0a 2a 2a 20 69 6e  er is held.** in
7310: 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
7320: 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d   from [sqlite3_m
7330: 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 6f 20 61 76  alloc()].  To av
7340: 6f 69 64 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61  oid a memory lea
7350: 6b 2c 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e  k,.** the callin
7360: 67 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68  g application sh
7370: 6f 75 6c 64 20 63 61 6c 6c 20 5b 73 71 6c 69 74  ould call [sqlit
7380: 65 33 5f 66 72 65 65 28 29 5d 20 6f 6e 20 61 6e  e3_free()] on an
7390: 79 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61  y error.** messa
73a0: 67 65 20 72 65 74 75 72 6e 65 64 20 74 68 72 6f  ge returned thro
73b0: 75 67 68 20 74 68 65 20 35 74 68 20 70 61 72 61  ugh the 5th para
73c0: 6d 65 74 65 72 20 77 68 65 6e 20 69 74 20 68 61  meter when it ha
73d0: 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e 67  s finished using
73e0: 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 6d 65  .** the error me
73f0: 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ssage..**.** If 
7400: 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
7410: 74 20 69 6e 20 74 68 65 20 32 6e 64 20 70 61 72  t in the 2nd par
7420: 61 6d 65 74 65 72 20 69 73 20 4e 55 4c 4c 20 6f  ameter is NULL o
7430: 72 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e  r an empty strin
7440: 67 0a 2a 2a 20 6f 72 20 61 20 73 74 72 69 6e 67  g.** or a string
7450: 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6f 6e 6c 79   containing only
7460: 20 77 68 69 74 65 73 70 61 63 65 20 61 6e 64 20   whitespace and 
7470: 63 6f 6d 6d 65 6e 74 73 2c 20 74 68 65 6e 20 6e  comments, then n
7480: 6f 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65  o SQL.** stateme
7490: 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61 74 65  nts are evaluate
74a0: 64 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61  d and the databa
74b0: 73 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65  se is not change
74c0: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  d..**.** The sql
74d0: 69 74 65 33 5f 65 78 65 63 28 29 20 69 6e 74 65  ite3_exec() inte
74e0: 72 66 61 63 65 20 69 73 20 69 6d 70 6c 65 6d 65  rface is impleme
74f0: 6e 74 65 64 20 69 6e 20 74 65 72 6d 73 20 6f 66  nted in terms of
7500: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  .** [sqlite3_pre
7510: 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c  pare_v2()], [sql
7520: 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e  ite3_step()], an
7530: 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  d [sqlite3_final
7540: 69 7a 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73  ize()]..** The s
7550: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 72 6f  qlite3_exec() ro
7560: 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 68 69  utine does nothi
7570: 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ng to the databa
7580: 73 65 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 62  se that cannot b
7590: 65 20 64 6f 6e 65 0a 2a 2a 20 62 79 20 5b 73 71  e done.** by [sq
75a0: 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
75b0: 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74  ()], [sqlite3_st
75c0: 65 70 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69  ep()], and [sqli
75d0: 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e  te3_finalize()].
75e0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
75f0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73   parameter to [s
7600: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 6d  qlite3_exec()] m
7610: 75 73 74 20 62 65 20 61 6e 20 76 61 6c 69 64 20  ust be an valid 
7620: 61 6e 64 20 6f 70 65 6e 0a 2a 2a 20 5b 64 61 74  and open.** [dat
7630: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
7640: 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 61 74  ]..**.** The dat
7650: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
7660: 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 63 6c 6f   must not be clo
7670: 73 65 64 20 77 68 69 6c 65 0a 2a 2a 20 5b 73 71  sed while.** [sq
7680: 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 69 73  lite3_exec()] is
7690: 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20   running..**.** 
76a0: 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  The calling func
76b0: 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 20  tion should use 
76c0: 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d  [sqlite3_free()]
76d0: 20 74 6f 20 66 72 65 65 0a 2a 2a 20 74 68 65 20   to free.** the 
76e0: 6d 65 6d 6f 72 79 20 74 68 61 74 20 2a 65 72 72  memory that *err
76f0: 6d 73 67 20 69 73 20 6c 65 66 74 20 70 6f 69 6e  msg is left poin
7700: 74 69 6e 67 20 61 74 20 6f 6e 63 65 20 74 68 65  ting at once the
7710: 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67   error.** messag
7720: 65 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6e  e is no longer n
7730: 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  eeded..**.** The
7740: 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74   SQL statement t
7750: 65 78 74 20 69 6e 20 74 68 65 20 32 6e 64 20 70  ext in the 2nd p
7760: 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c  arameter to [sql
7770: 69 74 65 33 5f 65 78 65 63 28 29 5d 0a 2a 2a 20  ite3_exec()].** 
7780: 6d 75 73 74 20 72 65 6d 61 69 6e 20 75 6e 63 68  must remain unch
7790: 61 6e 67 65 64 20 77 68 69 6c 65 20 5b 73 71 6c  anged while [sql
77a0: 69 74 65 33 5f 65 78 65 63 28 29 5d 20 69 73 20  ite3_exec()] is 
77b0: 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52  running..**.** R
77c0: 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
77d0: 5b 48 31 32 31 30 31 5d 20 5b 48 31 32 31 30 32  [H12101] [H12102
77e0: 5d 20 5b 48 31 32 31 30 34 5d 20 5b 48 31 32 31  ] [H12104] [H121
77f0: 30 35 5d 20 5b 48 31 32 31 30 37 5d 20 5b 48 31  05] [H12107] [H1
7800: 32 31 31 30 5d 20 5b 48 31 32 31 31 33 5d 20 5b  2110] [H12113] [
7810: 48 31 32 31 31 36 5d 0a 2a 2a 20 5b 48 31 32 31  H12116].** [H121
7820: 31 39 5d 20 5b 48 31 32 31 32 32 5d 20 5b 48 31  19] [H12122] [H1
7830: 32 31 32 35 5d 20 5b 48 31 32 31 33 31 5d 20 5b  2125] [H12131] [
7840: 48 31 32 31 33 34 5d 20 5b 48 31 32 31 33 37 5d  H12134] [H12137]
7850: 20 5b 48 31 32 31 33 38 5d 0a 2a 2f 0a 53 51 4c   [H12138].*/.SQL
7860: 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
7870: 74 65 33 5f 65 78 65 63 28 0a 20 20 73 71 6c 69  te3_exec(.  sqli
7880: 74 65 33 2a 2c 20 20 20 20 20 20 20 20 20 20 20  te3*,           
7890: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78a0: 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65         /* An ope
78b0: 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  n database */.  
78c0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 2c  const char *sql,
78d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78e0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51             /* SQ
78f0: 4c 20 74 6f 20 62 65 20 65 76 61 6c 75 61 74 65  L to be evaluate
7900: 64 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 63 61 6c  d */.  int (*cal
7910: 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74  lback)(void*,int
7920: 2c 63 68 61 72 2a 2a 2c 63 68 61 72 2a 2a 29 2c  ,char**,char**),
7930: 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 75    /* Callback fu
7940: 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64  nction */.  void
7950: 20 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   *,             
7960: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7970: 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20 61 72         /* 1st ar
7980: 67 75 6d 65 6e 74 20 74 6f 20 63 61 6c 6c 62 61  gument to callba
7990: 63 6b 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 65  ck */.  char **e
79a0: 72 72 6d 73 67 20 20 20 20 20 20 20 20 20 20 20  rrmsg           
79b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
79c0: 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20     /* Error msg 
79d0: 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a  written here */.
79e0: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
79f0: 45 46 3a 20 52 65 73 75 6c 74 20 43 6f 64 65 73  EF: Result Codes
7a00: 20 7b 48 31 30 32 31 30 7d 20 3c 53 31 30 37 30   {H10210} <S1070
7a10: 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  0>.** KEYWORDS: 
7a20: 53 51 4c 49 54 45 5f 4f 4b 20 7b 65 72 72 6f 72  SQLITE_OK {error
7a30: 20 63 6f 64 65 7d 20 7b 65 72 72 6f 72 20 63 6f   code} {error co
7a40: 64 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  des}.** KEYWORDS
7a50: 3a 20 7b 72 65 73 75 6c 74 20 63 6f 64 65 7d 20  : {result code} 
7a60: 7b 72 65 73 75 6c 74 20 63 6f 64 65 73 7d 0a 2a  {result codes}.*
7a70: 2a 0a 2a 2a 20 4d 61 6e 79 20 53 51 4c 69 74 65  *.** Many SQLite
7a80: 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72   functions retur
7a90: 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 73  n an integer res
7aa0: 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20 74 68  ult code from th
7ab0: 65 20 73 65 74 20 73 68 6f 77 6e 0a 2a 2a 20 68  e set shown.** h
7ac0: 65 72 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ere in order to 
7ad0: 69 6e 64 69 63 61 74 65 73 20 73 75 63 63 65 73  indicates succes
7ae0: 73 20 6f 72 20 66 61 69 6c 75 72 65 2e 0a 2a 2a  s or failure..**
7af0: 0a 2a 2a 20 4e 65 77 20 65 72 72 6f 72 20 63 6f  .** New error co
7b00: 64 65 73 20 6d 61 79 20 62 65 20 61 64 64 65 64  des may be added
7b10: 20 69 6e 20 66 75 74 75 72 65 20 76 65 72 73 69   in future versi
7b20: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  ons of SQLite..*
7b30: 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b  *.** See also: [
7b40: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41  SQLITE_IOERR_REA
7b50: 44 20 7c 20 65 78 74 65 6e 64 65 64 20 72 65 73  D | extended res
7b60: 75 6c 74 20 63 6f 64 65 73 5d 0a 2a 2f 0a 23 64  ult codes].*/.#d
7b70: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4b 20  efine SQLITE_OK 
7b80: 20 20 20 20 20 20 20 20 20 20 30 20 20 20 2f 2a            0   /*
7b90: 20 53 75 63 63 65 73 73 66 75 6c 20 72 65 73 75   Successful resu
7ba0: 6c 74 20 2a 2f 0a 2f 2a 20 62 65 67 69 6e 6e 69  lt */./* beginni
7bb0: 6e 67 2d 6f 66 2d 65 72 72 6f 72 2d 63 6f 64 65  ng-of-error-code
7bc0: 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s */.#define SQL
7bd0: 49 54 45 5f 45 52 52 4f 52 20 20 20 20 20 20 20  ITE_ERROR       
7be0: 20 31 20 20 20 2f 2a 20 53 51 4c 20 65 72 72 6f   1   /* SQL erro
7bf0: 72 20 6f 72 20 6d 69 73 73 69 6e 67 20 64 61 74  r or missing dat
7c00: 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65  abase */.#define
7c10: 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c   SQLITE_INTERNAL
7c20: 20 20 20 20 20 32 20 20 20 2f 2a 20 49 6e 74 65       2   /* Inte
7c30: 72 6e 61 6c 20 6c 6f 67 69 63 20 65 72 72 6f 72  rnal logic error
7c40: 20 69 6e 20 53 51 4c 69 74 65 20 2a 2f 0a 23 64   in SQLite */.#d
7c50: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 45 52  efine SQLITE_PER
7c60: 4d 20 20 20 20 20 20 20 20 20 33 20 20 20 2f 2a  M         3   /*
7c70: 20 41 63 63 65 73 73 20 70 65 72 6d 69 73 73 69   Access permissi
7c80: 6f 6e 20 64 65 6e 69 65 64 20 2a 2f 0a 23 64 65  on denied */.#de
7c90: 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 42 4f 52  fine SQLITE_ABOR
7ca0: 54 20 20 20 20 20 20 20 20 34 20 20 20 2f 2a 20  T        4   /* 
7cb0: 43 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65  Callback routine
7cc0: 20 72 65 71 75 65 73 74 65 64 20 61 6e 20 61 62   requested an ab
7cd0: 6f 72 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ort */.#define S
7ce0: 51 4c 49 54 45 5f 42 55 53 59 20 20 20 20 20 20  QLITE_BUSY      
7cf0: 20 20 20 35 20 20 20 2f 2a 20 54 68 65 20 64 61     5   /* The da
7d00: 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6c  tabase file is l
7d10: 6f 63 6b 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  ocked */.#define
7d20: 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 20   SQLITE_LOCKED  
7d30: 20 20 20 20 20 36 20 20 20 2f 2a 20 41 20 74 61       6   /* A ta
7d40: 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  ble in the datab
7d50: 61 73 65 20 69 73 20 6c 6f 63 6b 65 64 20 2a 2f  ase is locked */
7d60: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
7d70: 4e 4f 4d 45 4d 20 20 20 20 20 20 20 20 37 20 20  NOMEM        7  
7d80: 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 28 29 20 66   /* A malloc() f
7d90: 61 69 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  ailed */.#define
7da0: 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59   SQLITE_READONLY
7db0: 20 20 20 20 20 38 20 20 20 2f 2a 20 41 74 74 65       8   /* Atte
7dc0: 6d 70 74 20 74 6f 20 77 72 69 74 65 20 61 20 72  mpt to write a r
7dd0: 65 61 64 6f 6e 6c 79 20 64 61 74 61 62 61 73 65  eadonly database
7de0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
7df0: 54 45 5f 49 4e 54 45 52 52 55 50 54 20 20 20 20  TE_INTERRUPT    
7e00: 39 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e  9   /* Operation
7e10: 20 74 65 72 6d 69 6e 61 74 65 64 20 62 79 20 73   terminated by s
7e20: 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
7e30: 28 29 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ()*/.#define SQL
7e40: 49 54 45 5f 49 4f 45 52 52 20 20 20 20 20 20 20  ITE_IOERR       
7e50: 31 30 20 20 20 2f 2a 20 53 6f 6d 65 20 6b 69 6e  10   /* Some kin
7e60: 64 20 6f 66 20 64 69 73 6b 20 49 2f 4f 20 65 72  d of disk I/O er
7e70: 72 6f 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a  ror occurred */.
7e80: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
7e90: 4f 52 52 55 50 54 20 20 20 20 20 31 31 20 20 20  ORRUPT     11   
7ea0: 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
7eb0: 64 69 73 6b 20 69 6d 61 67 65 20 69 73 20 6d 61  disk image is ma
7ec0: 6c 66 6f 72 6d 65 64 20 2a 2f 0a 23 64 65 66 69  lformed */.#defi
7ed0: 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 54 46 4f 55  ne SQLITE_NOTFOU
7ee0: 4e 44 20 20 20 20 31 32 20 20 20 2f 2a 20 4e 4f  ND    12   /* NO
7ef0: 54 20 55 53 45 44 2e 20 54 61 62 6c 65 20 6f 72  T USED. Table or
7f00: 20 72 65 63 6f 72 64 20 6e 6f 74 20 66 6f 75 6e   record not foun
7f10: 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  d */.#define SQL
7f20: 49 54 45 5f 46 55 4c 4c 20 20 20 20 20 20 20 20  ITE_FULL        
7f30: 31 33 20 20 20 2f 2a 20 49 6e 73 65 72 74 69 6f  13   /* Insertio
7f40: 6e 20 66 61 69 6c 65 64 20 62 65 63 61 75 73 65  n failed because
7f50: 20 64 61 74 61 62 61 73 65 20 69 73 20 66 75 6c   database is ful
7f60: 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  l */.#define SQL
7f70: 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 20 20 20  ITE_CANTOPEN    
7f80: 31 34 20 20 20 2f 2a 20 55 6e 61 62 6c 65 20 74  14   /* Unable t
7f90: 6f 20 6f 70 65 6e 20 74 68 65 20 64 61 74 61 62  o open the datab
7fa0: 61 73 65 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66  ase file */.#def
7fb0: 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 4f 54 4f  ine SQLITE_PROTO
7fc0: 43 4f 4c 20 20 20 20 31 35 20 20 20 2f 2a 20 4e  COL    15   /* N
7fd0: 4f 54 20 55 53 45 44 2e 20 44 61 74 61 62 61 73  OT USED. Databas
7fe0: 65 20 6c 6f 63 6b 20 70 72 6f 74 6f 63 6f 6c 20  e lock protocol 
7ff0: 65 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65  error */.#define
8000: 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 20 20   SQLITE_EMPTY   
8010: 20 20 20 20 31 36 20 20 20 2f 2a 20 44 61 74 61      16   /* Data
8020: 62 61 73 65 20 69 73 20 65 6d 70 74 79 20 2a 2f  base is empty */
8030: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8040: 53 43 48 45 4d 41 20 20 20 20 20 20 31 37 20 20  SCHEMA      17  
8050: 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
8060: 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 64 20   schema changed 
8070: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
8080: 45 5f 54 4f 4f 42 49 47 20 20 20 20 20 20 31 38  E_TOOBIG      18
8090: 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20     /* String or 
80a0: 42 4c 4f 42 20 65 78 63 65 65 64 73 20 73 69 7a  BLOB exceeds siz
80b0: 65 20 6c 69 6d 69 74 20 2a 2f 0a 23 64 65 66 69  e limit */.#defi
80c0: 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  ne SQLITE_CONSTR
80d0: 41 49 4e 54 20 20 31 39 20 20 20 2f 2a 20 41 62  AINT  19   /* Ab
80e0: 6f 72 74 20 64 75 65 20 74 6f 20 63 6f 6e 73 74  ort due to const
80f0: 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20  raint violation 
8100: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
8110: 45 5f 4d 49 53 4d 41 54 43 48 20 20 20 20 32 30  E_MISMATCH    20
8120: 20 20 20 2f 2a 20 44 61 74 61 20 74 79 70 65 20     /* Data type 
8130: 6d 69 73 6d 61 74 63 68 20 2a 2f 0a 23 64 65 66  mismatch */.#def
8140: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  ine SQLITE_MISUS
8150: 45 20 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4c  E      21   /* L
8160: 69 62 72 61 72 79 20 75 73 65 64 20 69 6e 63 6f  ibrary used inco
8170: 72 72 65 63 74 6c 79 20 2a 2f 0a 23 64 65 66 69  rrectly */.#defi
8180: 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 20  ne SQLITE_NOLFS 
8190: 20 20 20 20 20 20 32 32 20 20 20 2f 2a 20 55 73        22   /* Us
81a0: 65 73 20 4f 53 20 66 65 61 74 75 72 65 73 20 6e  es OS features n
81b0: 6f 74 20 73 75 70 70 6f 72 74 65 64 20 6f 6e 20  ot supported on 
81c0: 68 6f 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  host */.#define 
81d0: 53 51 4c 49 54 45 5f 41 55 54 48 20 20 20 20 20  SQLITE_AUTH     
81e0: 20 20 20 32 33 20 20 20 2f 2a 20 41 75 74 68 6f     23   /* Autho
81f0: 72 69 7a 61 74 69 6f 6e 20 64 65 6e 69 65 64 20  rization denied 
8200: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
8210: 45 5f 46 4f 52 4d 41 54 20 20 20 20 20 20 32 34  E_FORMAT      24
8220: 20 20 20 2f 2a 20 41 75 78 69 6c 69 61 72 79 20     /* Auxiliary 
8230: 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 20  database format 
8240: 65 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65  error */.#define
8250: 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 20 20 20   SQLITE_RANGE   
8260: 20 20 20 20 32 35 20 20 20 2f 2a 20 32 6e 64 20      25   /* 2nd 
8270: 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
8280: 69 74 65 33 5f 62 69 6e 64 20 6f 75 74 20 6f 66  ite3_bind out of
8290: 20 72 61 6e 67 65 20 2a 2f 0a 23 64 65 66 69 6e   range */.#defin
82a0: 65 20 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 20  e SQLITE_NOTADB 
82b0: 20 20 20 20 20 32 36 20 20 20 2f 2a 20 46 69 6c       26   /* Fil
82c0: 65 20 6f 70 65 6e 65 64 20 74 68 61 74 20 69 73  e opened that is
82d0: 20 6e 6f 74 20 61 20 64 61 74 61 62 61 73 65 20   not a database 
82e0: 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  file */.#define 
82f0: 53 51 4c 49 54 45 5f 52 4f 57 20 20 20 20 20 20  SQLITE_ROW      
8300: 20 20 20 31 30 30 20 20 2f 2a 20 73 71 6c 69 74     100  /* sqlit
8310: 65 33 5f 73 74 65 70 28 29 20 68 61 73 20 61 6e  e3_step() has an
8320: 6f 74 68 65 72 20 72 6f 77 20 72 65 61 64 79 20  other row ready 
8330: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
8340: 45 5f 44 4f 4e 45 20 20 20 20 20 20 20 20 31 30  E_DONE        10
8350: 31 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74  1  /* sqlite3_st
8360: 65 70 28 29 20 68 61 73 20 66 69 6e 69 73 68 65  ep() has finishe
8370: 64 20 65 78 65 63 75 74 69 6e 67 20 2a 2f 0a 2f  d executing */./
8380: 2a 20 65 6e 64 2d 6f 66 2d 65 72 72 6f 72 2d 63  * end-of-error-c
8390: 6f 64 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  odes */../*.** C
83a0: 41 50 49 33 52 45 46 3a 20 45 78 74 65 6e 64 65  API3REF: Extende
83b0: 64 20 52 65 73 75 6c 74 20 43 6f 64 65 73 20 7b  d Result Codes {
83c0: 48 31 30 32 32 30 7d 20 3c 53 31 30 37 30 30 3e  H10220} <S10700>
83d0: 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 65  .** KEYWORDS: {e
83e0: 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f  xtended error co
83f0: 64 65 7d 20 7b 65 78 74 65 6e 64 65 64 20 65 72  de} {extended er
8400: 72 6f 72 20 63 6f 64 65 73 7d 0a 2a 2a 20 4b 45  ror codes}.** KE
8410: 59 57 4f 52 44 53 3a 20 7b 65 78 74 65 6e 64 65  YWORDS: {extende
8420: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 7d 20 7b  d result code} {
8430: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
8440: 63 6f 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20  codes}.**.** In 
8450: 69 74 73 20 64 65 66 61 75 6c 74 20 63 6f 6e 66  its default conf
8460: 69 67 75 72 61 74 69 6f 6e 2c 20 53 51 4c 69 74  iguration, SQLit
8470: 65 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20 72  e API routines r
8480: 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 32 36 20  eturn one of 26 
8490: 69 6e 74 65 67 65 72 0a 2a 2a 20 5b 53 51 4c 49  integer.** [SQLI
84a0: 54 45 5f 4f 4b 20 7c 20 72 65 73 75 6c 74 20 63  TE_OK | result c
84b0: 6f 64 65 73 5d 2e 20 20 48 6f 77 65 76 65 72 2c  odes].  However,
84c0: 20 65 78 70 65 72 69 65 6e 63 65 20 68 61 73 20   experience has 
84d0: 73 68 6f 77 6e 20 74 68 61 74 20 6d 61 6e 79 20  shown that many 
84e0: 6f 66 0a 2a 2a 20 74 68 65 73 65 20 72 65 73 75  of.** these resu
84f0: 6c 74 20 63 6f 64 65 73 20 61 72 65 20 74 6f 6f  lt codes are too
8500: 20 63 6f 61 72 73 65 2d 67 72 61 69 6e 65 64 2e   coarse-grained.
8510: 20 20 54 68 65 79 20 64 6f 20 6e 6f 74 20 70 72    They do not pr
8520: 6f 76 69 64 65 20 61 73 0a 2a 2a 20 6d 75 63 68  ovide as.** much
8530: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
8540: 75 74 20 70 72 6f 62 6c 65 6d 73 20 61 73 20 70  ut problems as p
8550: 72 6f 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74  rogrammers might
8560: 20 6c 69 6b 65 2e 20 20 49 6e 20 61 6e 20 65 66   like.  In an ef
8570: 66 6f 72 74 20 74 6f 0a 2a 2a 20 61 64 64 72 65  fort to.** addre
8580: 73 73 20 74 68 69 73 2c 20 6e 65 77 65 72 20 76  ss this, newer v
8590: 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  ersions of SQLit
85a0: 65 20 28 76 65 72 73 69 6f 6e 20 33 2e 33 2e 38  e (version 3.3.8
85b0: 20 61 6e 64 20 6c 61 74 65 72 29 20 69 6e 63 6c   and later) incl
85c0: 75 64 65 0a 2a 2a 20 73 75 70 70 6f 72 74 20 66  ude.** support f
85d0: 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 72 65  or additional re
85e0: 73 75 6c 74 20 63 6f 64 65 73 20 74 68 61 74 20  sult codes that 
85f0: 70 72 6f 76 69 64 65 20 6d 6f 72 65 20 64 65 74  provide more det
8600: 61 69 6c 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f  ailed informatio
8610: 6e 0a 2a 2a 20 61 62 6f 75 74 20 65 72 72 6f 72  n.** about error
8620: 73 2e 20 54 68 65 20 65 78 74 65 6e 64 65 64 20  s. The extended 
8630: 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65  result codes are
8640: 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61   enabled or disa
8650: 62 6c 65 64 0a 2a 2a 20 6f 6e 20 61 20 70 65 72  bled.** on a per
8660: 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
8670: 74 69 6f 6e 20 62 61 73 69 73 20 75 73 69 6e 67  tion basis using
8680: 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   the.** [sqlite3
8690: 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74  _extended_result
86a0: 5f 63 6f 64 65 73 28 29 5d 20 41 50 49 2e 0a 2a  _codes()] API..*
86b0: 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65  *.** Some of the
86c0: 20 61 76 61 69 6c 61 62 6c 65 20 65 78 74 65 6e   available exten
86d0: 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73  ded result codes
86e0: 20 61 72 65 20 6c 69 73 74 65 64 20 68 65 72 65   are listed here
86f0: 2e 0a 2a 2a 20 4f 6e 65 20 6d 61 79 20 65 78 70  ..** One may exp
8700: 65 63 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ect the number o
8710: 66 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c  f extended resul
8720: 74 20 63 6f 64 65 73 20 77 69 6c 6c 20 62 65 20  t codes will be 
8730: 65 78 70 61 6e 64 0a 2a 2a 20 6f 76 65 72 20 74  expand.** over t
8740: 69 6d 65 2e 20 20 53 6f 66 74 77 61 72 65 20 74  ime.  Software t
8750: 68 61 74 20 75 73 65 73 20 65 78 74 65 6e 64 65  hat uses extende
8760: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 73  d result codes s
8770: 68 6f 75 6c 64 20 65 78 70 65 63 74 0a 2a 2a 20  hould expect.** 
8780: 74 6f 20 73 65 65 20 6e 65 77 20 72 65 73 75 6c  to see new resul
8790: 74 20 63 6f 64 65 73 20 69 6e 20 66 75 74 75 72  t codes in futur
87a0: 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51  e releases of SQ
87b0: 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  Lite..**.** The 
87c0: 53 51 4c 49 54 45 5f 4f 4b 20 72 65 73 75 6c 74  SQLITE_OK result
87d0: 20 63 6f 64 65 20 77 69 6c 6c 20 6e 65 76 65 72   code will never
87e0: 20 62 65 20 65 78 74 65 6e 64 65 64 2e 20 20 49   be extended.  I
87f0: 74 20 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a  t will always.**
8800: 20 62 65 20 65 78 61 63 74 6c 79 20 7a 65 72 6f   be exactly zero
8810: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
8820: 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 20  ITE_IOERR_READ  
8830: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c              (SQL
8840: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 3c 3c  ITE_IOERR | (1<<
8850: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  8)).#define SQLI
8860: 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52  TE_IOERR_SHORT_R
8870: 45 41 44 20 20 20 20 20 20 20 20 28 53 51 4c 49  EAD        (SQLI
8880: 54 45 5f 49 4f 45 52 52 20 7c 20 28 32 3c 3c 38  TE_IOERR | (2<<8
8890: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
88a0: 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 20 20 20  E_IOERR_WRITE   
88b0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
88c0: 45 5f 49 4f 45 52 52 20 7c 20 28 33 3c 3c 38 29  E_IOERR | (3<<8)
88d0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
88e0: 5f 49 4f 45 52 52 5f 46 53 59 4e 43 20 20 20 20  _IOERR_FSYNC    
88f0: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
8900: 5f 49 4f 45 52 52 20 7c 20 28 34 3c 3c 38 29 29  _IOERR | (4<<8))
8910: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8920: 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e 43 20  IOERR_DIR_FSYNC 
8930: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
8940: 49 4f 45 52 52 20 7c 20 28 35 3c 3c 38 29 29 0a  IOERR | (5<<8)).
8950: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
8960: 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 20 20 20  OERR_TRUNCATE   
8970: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
8980: 4f 45 52 52 20 7c 20 28 36 3c 3c 38 29 29 0a 23  OERR | (6<<8)).#
8990: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f  define SQLITE_IO
89a0: 45 52 52 5f 46 53 54 41 54 20 20 20 20 20 20 20  ERR_FSTAT       
89b0: 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f        (SQLITE_IO
89c0: 45 52 52 20 7c 20 28 37 3c 3c 38 29 29 0a 23 64  ERR | (7<<8)).#d
89d0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
89e0: 52 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 20  RR_UNLOCK       
89f0: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
8a00: 52 52 20 7c 20 28 38 3c 3c 38 29 29 0a 23 64 65  RR | (8<<8)).#de
8a10: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52  fine SQLITE_IOER
8a20: 52 5f 52 44 4c 4f 43 4b 20 20 20 20 20 20 20 20  R_RDLOCK        
8a30: 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52      (SQLITE_IOER
8a40: 52 20 7c 20 28 39 3c 3c 38 29 29 0a 23 64 65 66  R | (9<<8)).#def
8a50: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
8a60: 5f 44 45 4c 45 54 45 20 20 20 20 20 20 20 20 20  _DELETE         
8a70: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
8a80: 20 7c 20 28 31 30 3c 3c 38 29 29 0a 23 64 65 66   | (10<<8)).#def
8a90: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
8aa0: 5f 42 4c 4f 43 4b 45 44 20 20 20 20 20 20 20 20  _BLOCKED        
8ab0: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
8ac0: 20 7c 20 28 31 31 3c 3c 38 29 29 0a 23 64 65 66   | (11<<8)).#def
8ad0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
8ae0: 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20 20 20 20  _NOMEM          
8af0: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
8b00: 20 7c 20 28 31 32 3c 3c 38 29 29 0a 23 64 65 66   | (12<<8)).#def
8b10: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
8b20: 5f 41 43 43 45 53 53 20 20 20 20 20 20 20 20 20  _ACCESS         
8b30: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
8b40: 20 7c 20 28 31 33 3c 3c 38 29 29 0a 23 64 65 66   | (13<<8)).#def
8b50: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
8b60: 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f  _CHECKRESERVEDLO
8b70: 43 4b 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52  CK (SQLITE_IOERR
8b80: 20 7c 20 28 31 34 3c 3c 38 29 29 0a 23 64 65 66   | (14<<8)).#def
8b90: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
8ba0: 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 20 20  _LOCK           
8bb0: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
8bc0: 20 7c 20 28 31 35 3c 3c 38 29 29 0a 23 64 65 66   | (15<<8)).#def
8bd0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
8be0: 5f 43 4c 4f 53 45 20 20 20 20 20 20 20 20 20 20  _CLOSE          
8bf0: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
8c00: 20 7c 20 28 31 36 3c 3c 38 29 29 0a 23 64 65 66   | (16<<8)).#def
8c10: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
8c20: 5f 44 49 52 5f 43 4c 4f 53 45 20 20 20 20 20 20  _DIR_CLOSE      
8c30: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
8c40: 20 7c 20 28 31 37 3c 3c 38 29 29 0a 23 64 65 66   | (17<<8)).#def
8c50: 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  ine SQLITE_LOCKE
8c60: 44 5f 53 48 41 52 45 44 43 41 43 48 45 20 20 20  D_SHAREDCACHE   
8c70: 20 20 20 28 53 51 4c 49 54 45 5f 4c 4f 43 4b 45     (SQLITE_LOCKE
8c80: 44 20 7c 20 28 31 3c 3c 38 29 20 29 0a 0a 2f 2a  D | (1<<8) )../*
8c90: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6c  .** CAPI3REF: Fl
8ca0: 61 67 73 20 46 6f 72 20 46 69 6c 65 20 4f 70 65  ags For File Ope
8cb0: 6e 20 4f 70 65 72 61 74 69 6f 6e 73 20 7b 48 31  n Operations {H1
8cc0: 30 32 33 30 7d 20 3c 48 31 31 31 32 30 3e 20 3c  0230} <H11120> <
8cd0: 48 31 32 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  H12700>.**.** Th
8ce0: 65 73 65 20 62 69 74 20 76 61 6c 75 65 73 20 61  ese bit values a
8cf0: 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20  re intended for 
8d00: 75 73 65 20 69 6e 20 74 68 65 0a 2a 2a 20 33 72  use in the.** 3r
8d10: 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  d parameter to t
8d20: 68 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  he [sqlite3_open
8d30: 5f 76 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65  _v2()] interface
8d40: 20 61 6e 64 0a 2a 2a 20 69 6e 20 74 68 65 20 34   and.** in the 4
8d50: 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  th parameter to 
8d60: 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64  the xOpen method
8d70: 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69   of the.** [sqli
8d80: 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e  te3_vfs] object.
8d90: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
8da0: 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
8db0: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30           0x00000
8dc0: 30 30 31 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73  001  /* Ok for s
8dd0: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
8de0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
8df0: 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
8e00: 45 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30  E        0x00000
8e10: 30 30 32 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73  002  /* Ok for s
8e20: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
8e30: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
8e40: 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 20  TE_OPEN_CREATE  
8e50: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30           0x00000
8e60: 30 30 34 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73  004  /* Ok for s
8e70: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
8e80: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
8e90: 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e  TE_OPEN_DELETEON
8ea0: 43 4c 4f 53 45 20 20 20 20 30 78 30 30 30 30 30  CLOSE    0x00000
8eb0: 30 30 38 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79  008  /* VFS only
8ec0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
8ed0: 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56  TE_OPEN_EXCLUSIV
8ee0: 45 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30  E        0x00000
8ef0: 30 31 30 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79  010  /* VFS only
8f00: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
8f10: 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20  TE_OPEN_MAIN_DB 
8f20: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30           0x00000
8f30: 31 30 30 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79  100  /* VFS only
8f40: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
8f50: 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20  TE_OPEN_TEMP_DB 
8f60: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30           0x00000
8f70: 32 30 30 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79  200  /* VFS only
8f80: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
8f90: 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e  TE_OPEN_TRANSIEN
8fa0: 54 5f 44 42 20 20 20 20 20 30 78 30 30 30 30 30  T_DB     0x00000
8fb0: 34 30 30 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79  400  /* VFS only
8fc0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
8fd0: 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55  TE_OPEN_MAIN_JOU
8fe0: 52 4e 41 4c 20 20 20 20 20 30 78 30 30 30 30 30  RNAL     0x00000
8ff0: 38 30 30 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79  800  /* VFS only
9000: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
9010: 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55  TE_OPEN_TEMP_JOU
9020: 52 4e 41 4c 20 20 20 20 20 30 78 30 30 30 30 31  RNAL     0x00001
9030: 30 30 30 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79  000  /* VFS only
9040: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
9050: 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e  TE_OPEN_SUBJOURN
9060: 41 4c 20 20 20 20 20 20 20 30 78 30 30 30 30 32  AL       0x00002
9070: 30 30 30 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79  000  /* VFS only
9080: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
9090: 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a  TE_OPEN_MASTER_J
90a0: 4f 55 52 4e 41 4c 20 20 20 30 78 30 30 30 30 34  OURNAL   0x00004
90b0: 30 30 30 20 20 2f 2a 20 56 46 53 20 6f 6e 6c 79  000  /* VFS only
90c0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
90d0: 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 20  TE_OPEN_NOMUTEX 
90e0: 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30 38           0x00008
90f0: 30 30 30 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73  000  /* Ok for s
9100: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
9110: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
9120: 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45  TE_OPEN_FULLMUTE
9130: 58 20 20 20 20 20 20 20 20 30 78 30 30 30 31 30  X        0x00010
9140: 30 30 30 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73  000  /* Ok for s
9150: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
9160: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
9170: 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41  TE_OPEN_SHAREDCA
9180: 43 48 45 20 20 20 20 20 20 30 78 30 30 30 32 30  CHE      0x00020
9190: 30 30 30 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73  000  /* Ok for s
91a0: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
91b0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
91c0: 54 45 5f 4f 50 45 4e 5f 50 52 49 56 41 54 45 43  TE_OPEN_PRIVATEC
91d0: 41 43 48 45 20 20 20 20 20 30 78 30 30 30 34 30  ACHE     0x00040
91e0: 30 30 30 20 20 2f 2a 20 4f 6b 20 66 6f 72 20 73  000  /* Ok for s
91f0: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
9200: 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33   */../*.** CAPI3
9210: 52 45 46 3a 20 44 65 76 69 63 65 20 43 68 61 72  REF: Device Char
9220: 61 63 74 65 72 69 73 74 69 63 73 20 7b 48 31 30  acteristics {H10
9230: 32 34 30 7d 20 3c 48 31 31 31 32 30 3e 0a 2a 2a  240} <H11120>.**
9240: 0a 2a 2a 20 54 68 65 20 78 44 65 76 69 63 65 43  .** The xDeviceC
9250: 61 70 61 62 69 6c 69 74 69 65 73 20 6d 65 74 68  apabilities meth
9260: 6f 64 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  od of the [sqlit
9270: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a  e3_io_methods].*
9280: 2a 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 73  * object returns
9290: 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63   an integer whic
92a0: 68 20 69 73 20 61 20 76 65 63 74 6f 72 20 6f 66  h is a vector of
92b0: 20 74 68 65 20 74 68 65 73 65 0a 2a 2a 20 62 69   the these.** bi
92c0: 74 20 76 61 6c 75 65 73 20 65 78 70 72 65 73 73  t values express
92d0: 69 6e 67 20 49 2f 4f 20 63 68 61 72 61 63 74 65  ing I/O characte
92e0: 72 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 6d  ristics of the m
92f0: 61 73 73 20 73 74 6f 72 61 67 65 0a 2a 2a 20 64  ass storage.** d
9300: 65 76 69 63 65 20 74 68 61 74 20 68 6f 6c 64 73  evice that holds
9310: 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 20 74   the file that t
9320: 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  he [sqlite3_io_m
9330: 65 74 68 6f 64 73 5d 0a 2a 2a 20 72 65 66 65 72  ethods].** refer
9340: 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  s to..**.** The 
9350: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
9360: 4d 49 43 20 70 72 6f 70 65 72 74 79 20 6d 65 61  MIC property mea
9370: 6e 73 20 74 68 61 74 20 61 6c 6c 20 77 72 69 74  ns that all writ
9380: 65 73 20 6f 66 0a 2a 2a 20 61 6e 79 20 73 69 7a  es of.** any siz
9390: 65 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54  e are atomic.  T
93a0: 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  he SQLITE_IOCAP_
93b0: 41 54 4f 4d 49 43 6e 6e 6e 20 76 61 6c 75 65 73  ATOMICnnn values
93c0: 0a 2a 2a 20 6d 65 61 6e 20 74 68 61 74 20 77 72  .** mean that wr
93d0: 69 74 65 73 20 6f 66 20 62 6c 6f 63 6b 73 20 74  ites of blocks t
93e0: 68 61 74 20 61 72 65 20 6e 6e 6e 20 62 79 74 65  hat are nnn byte
93f0: 73 20 69 6e 20 73 69 7a 65 20 61 6e 64 0a 2a 2a  s in size and.**
9400: 20 61 72 65 20 61 6c 69 67 6e 65 64 20 74 6f 20   are aligned to 
9410: 61 6e 20 61 64 64 72 65 73 73 20 77 68 69 63 68  an address which
9420: 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6d   is an integer m
9430: 75 6c 74 69 70 6c 65 20 6f 66 0a 2a 2a 20 6e 6e  ultiple of.** nn
9440: 6e 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54  n are atomic.  T
9450: 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  he SQLITE_IOCAP_
9460: 53 41 46 45 5f 41 50 50 45 4e 44 20 76 61 6c 75  SAFE_APPEND valu
9470: 65 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20  e means.** that 
9480: 77 68 65 6e 20 64 61 74 61 20 69 73 20 61 70 70  when data is app
9490: 65 6e 64 65 64 20 74 6f 20 61 20 66 69 6c 65 2c  ended to a file,
94a0: 20 74 68 65 20 64 61 74 61 20 69 73 20 61 70 70   the data is app
94b0: 65 6e 64 65 64 0a 2a 2a 20 66 69 72 73 74 20 74  ended.** first t
94c0: 68 65 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20  hen the size of 
94d0: 74 68 65 20 66 69 6c 65 20 69 73 20 65 78 74 65  the file is exte
94e0: 6e 64 65 64 2c 20 6e 65 76 65 72 20 74 68 65 20  nded, never the 
94f0: 6f 74 68 65 72 0a 2a 2a 20 77 61 79 20 61 72 6f  other.** way aro
9500: 75 6e 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45  und.  The SQLITE
9510: 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41  _IOCAP_SEQUENTIA
9520: 4c 20 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73  L property means
9530: 20 74 68 61 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61   that.** informa
9540: 74 69 6f 6e 20 69 73 20 77 72 69 74 74 65 6e 20  tion is written 
9550: 74 6f 20 64 69 73 6b 20 69 6e 20 74 68 65 20 73  to disk in the s
9560: 61 6d 65 20 6f 72 64 65 72 20 61 73 20 63 61 6c  ame order as cal
9570: 6c 73 0a 2a 2a 20 74 6f 20 78 57 72 69 74 65 28  ls.** to xWrite(
9580: 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  )..*/.#define SQ
9590: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
95a0: 43 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30  C          0x000
95b0: 30 30 30 30 31 0a 23 64 65 66 69 6e 65 20 53 51  00001.#define SQ
95c0: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
95d0: 43 35 31 32 20 20 20 20 20 20 20 30 78 30 30 30  C512       0x000
95e0: 30 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51  00002.#define SQ
95f0: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
9600: 43 31 4b 20 20 20 20 20 20 20 20 30 78 30 30 30  C1K        0x000
9610: 30 30 30 30 34 0a 23 64 65 66 69 6e 65 20 53 51  00004.#define SQ
9620: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
9630: 43 32 4b 20 20 20 20 20 20 20 20 30 78 30 30 30  C2K        0x000
9640: 30 30 30 30 38 0a 23 64 65 66 69 6e 65 20 53 51  00008.#define SQ
9650: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
9660: 43 34 4b 20 20 20 20 20 20 20 20 30 78 30 30 30  C4K        0x000
9670: 30 30 30 31 30 0a 23 64 65 66 69 6e 65 20 53 51  00010.#define SQ
9680: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
9690: 43 38 4b 20 20 20 20 20 20 20 20 30 78 30 30 30  C8K        0x000
96a0: 30 30 30 32 30 0a 23 64 65 66 69 6e 65 20 53 51  00020.#define SQ
96b0: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
96c0: 43 31 36 4b 20 20 20 20 20 20 20 30 78 30 30 30  C16K       0x000
96d0: 30 30 30 34 30 0a 23 64 65 66 69 6e 65 20 53 51  00040.#define SQ
96e0: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
96f0: 43 33 32 4b 20 20 20 20 20 20 20 30 78 30 30 30  C32K       0x000
9700: 30 30 30 38 30 0a 23 64 65 66 69 6e 65 20 53 51  00080.#define SQ
9710: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
9720: 43 36 34 4b 20 20 20 20 20 20 20 30 78 30 30 30  C64K       0x000
9730: 30 30 31 30 30 0a 23 64 65 66 69 6e 65 20 53 51  00100.#define SQ
9740: 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f  LITE_IOCAP_SAFE_
9750: 41 50 50 45 4e 44 20 20 20 20 20 30 78 30 30 30  APPEND     0x000
9760: 30 30 32 30 30 0a 23 64 65 66 69 6e 65 20 53 51  00200.#define SQ
9770: 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45  LITE_IOCAP_SEQUE
9780: 4e 54 49 41 4c 20 20 20 20 20 20 30 78 30 30 30  NTIAL      0x000
9790: 30 30 34 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  00400../*.** CAP
97a0: 49 33 52 45 46 3a 20 46 69 6c 65 20 4c 6f 63 6b  I3REF: File Lock
97b0: 69 6e 67 20 4c 65 76 65 6c 73 20 7b 48 31 30 32  ing Levels {H102
97c0: 35 30 7d 20 3c 48 31 31 31 32 30 3e 20 3c 48 31  50} <H11120> <H1
97d0: 31 33 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  1310>.**.** SQLi
97e0: 74 65 20 75 73 65 73 20 6f 6e 65 20 6f 66 20 74  te uses one of t
97f0: 68 65 73 65 20 69 6e 74 65 67 65 72 20 76 61 6c  hese integer val
9800: 75 65 73 20 61 73 20 74 68 65 20 73 65 63 6f 6e  ues as the secon
9810: 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f  d.** argument to
9820: 20 63 61 6c 6c 73 20 69 74 20 6d 61 6b 65 73 20   calls it makes 
9830: 74 6f 20 74 68 65 20 78 4c 6f 63 6b 28 29 20 61  to the xLock() a
9840: 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20 6d 65 74  nd xUnlock() met
9850: 68 6f 64 73 0a 2a 2a 20 6f 66 20 61 6e 20 5b 73  hods.** of an [s
9860: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
9870: 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 23 64  s] object..*/.#d
9880: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43  efine SQLITE_LOC
9890: 4b 5f 4e 4f 4e 45 20 20 20 20 20 20 20 20 20 20  K_NONE          
98a0: 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  0.#define SQLITE
98b0: 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 20 20 20 20  _LOCK_SHARED    
98c0: 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51      1.#define SQ
98d0: 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56  LITE_LOCK_RESERV
98e0: 45 44 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e  ED      2.#defin
98f0: 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45  e SQLITE_LOCK_PE
9900: 4e 44 49 4e 47 20 20 20 20 20 20 20 33 0a 23 64  NDING       3.#d
9910: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43  efine SQLITE_LOC
9920: 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 20  K_EXCLUSIVE     
9930: 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  4../*.** CAPI3RE
9940: 46 3a 20 53 79 6e 63 68 72 6f 6e 69 7a 61 74 69  F: Synchronizati
9950: 6f 6e 20 54 79 70 65 20 46 6c 61 67 73 20 7b 48  on Type Flags {H
9960: 31 30 32 36 30 7d 20 3c 48 31 31 31 32 30 3e 0a  10260} <H11120>.
9970: 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 69 74  **.** When SQLit
9980: 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 78 53  e invokes the xS
9990: 79 6e 63 28 29 20 6d 65 74 68 6f 64 20 6f 66 20  ync() method of 
99a0: 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69  an.** [sqlite3_i
99b0: 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63  o_methods] objec
99c0: 74 20 69 74 20 75 73 65 73 20 61 20 63 6f 6d 62  t it uses a comb
99d0: 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68  ination of.** th
99e0: 65 73 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75  ese integer valu
99f0: 65 73 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  es as the second
9a00: 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
9a10: 20 57 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45   When the SQLITE
9a20: 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 66  _SYNC_DATAONLY f
9a30: 6c 61 67 20 69 73 20 75 73 65 64 2c 20 69 74 20  lag is used, it 
9a40: 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a  means that the.*
9a50: 2a 20 73 79 6e 63 20 6f 70 65 72 61 74 69 6f 6e  * sync operation
9a60: 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 66   only needs to f
9a70: 6c 75 73 68 20 64 61 74 61 20 74 6f 20 6d 61 73  lush data to mas
9a80: 73 20 73 74 6f 72 61 67 65 2e 20 20 49 6e 6f 64  s storage.  Inod
9a90: 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  e.** information
9aa0: 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 66 6c 75   need not be flu
9ab0: 73 68 65 64 2e 20 49 66 20 74 68 65 20 6c 6f 77  shed. If the low
9ac0: 65 72 20 66 6f 75 72 20 62 69 74 73 20 6f 66 20  er four bits of 
9ad0: 74 68 65 20 66 6c 61 67 0a 2a 2a 20 65 71 75 61  the flag.** equa
9ae0: 6c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f  l SQLITE_SYNC_NO
9af0: 52 4d 41 4c 2c 20 74 68 61 74 20 6d 65 61 6e 73  RMAL, that means
9b00: 20 74 6f 20 75 73 65 20 6e 6f 72 6d 61 6c 20 66   to use normal f
9b10: 73 79 6e 63 28 29 20 73 65 6d 61 6e 74 69 63 73  sync() semantics
9b20: 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 77 65  ..** If the lowe
9b30: 72 20 66 6f 75 72 20 62 69 74 73 20 65 71 75 61  r four bits equa
9b40: 6c 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55  l SQLITE_SYNC_FU
9b50: 4c 4c 2c 20 74 68 61 74 20 6d 65 61 6e 73 0a 2a  LL, that means.*
9b60: 2a 20 74 6f 20 75 73 65 20 4d 61 63 20 4f 53 20  * to use Mac OS 
9b70: 58 20 73 74 79 6c 65 20 66 75 6c 6c 73 79 6e 63  X style fullsync
9b80: 20 69 6e 73 74 65 61 64 20 6f 66 20 66 73 79 6e   instead of fsyn
9b90: 63 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  c()..*/.#define 
9ba0: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d  SQLITE_SYNC_NORM
9bb0: 41 4c 20 20 20 20 20 20 20 20 30 78 30 30 30 30  AL        0x0000
9bc0: 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  2.#define SQLITE
9bd0: 5f 53 59 4e 43 5f 46 55 4c 4c 20 20 20 20 20 20  _SYNC_FULL      
9be0: 20 20 20 20 30 78 30 30 30 30 33 0a 23 64 65 66      0x00003.#def
9bf0: 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  ine SQLITE_SYNC_
9c00: 44 41 54 41 4f 4e 4c 59 20 20 20 20 20 20 30 78  DATAONLY      0x
9c10: 30 30 30 31 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  00010../*.** CAP
9c20: 49 33 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66  I3REF: OS Interf
9c30: 61 63 65 20 4f 70 65 6e 20 46 69 6c 65 20 48 61  ace Open File Ha
9c40: 6e 64 6c 65 20 7b 48 31 31 31 31 30 7d 20 3c 53  ndle {H11110} <S
9c50: 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 41 6e 20  20110>.**.** An 
9c60: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f  [sqlite3_file] o
9c70: 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 73  bject represents
9c80: 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 69 6e   an open file in
9c90: 20 74 68 65 20 0a 2a 2a 20 5b 73 71 6c 69 74 65   the .** [sqlite
9ca0: 33 5f 76 66 73 20 7c 20 4f 53 20 69 6e 74 65 72  3_vfs | OS inter
9cb0: 66 61 63 65 20 6c 61 79 65 72 5d 2e 20 20 49 6e  face layer].  In
9cc0: 64 69 76 69 64 75 61 6c 20 4f 53 20 69 6e 74 65  dividual OS inte
9cd0: 72 66 61 63 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65  rface.** impleme
9ce0: 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 0a 2a 2a  ntations will.**
9cf0: 20 77 61 6e 74 20 74 6f 20 73 75 62 63 6c 61 73   want to subclas
9d00: 73 20 74 68 69 73 20 6f 62 6a 65 63 74 20 62 79  s this object by
9d10: 20 61 70 70 65 6e 64 69 6e 67 20 61 64 64 69 74   appending addit
9d20: 69 6f 6e 61 6c 20 66 69 65 6c 64 73 0a 2a 2a 20  ional fields.** 
9d30: 66 6f 72 20 74 68 65 69 72 20 6f 77 6e 20 75 73  for their own us
9d40: 65 2e 20 20 54 68 65 20 70 4d 65 74 68 6f 64 73  e.  The pMethods
9d50: 20 65 6e 74 72 79 20 69 73 20 61 20 70 6f 69 6e   entry is a poin
9d60: 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 71  ter to an.** [sq
9d70: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
9d80: 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20 64 65  ] object that de
9d90: 66 69 6e 65 73 20 6d 65 74 68 6f 64 73 20 66 6f  fines methods fo
9da0: 72 20 70 65 72 66 6f 72 6d 69 6e 67 0a 2a 2a 20  r performing.** 
9db0: 49 2f 4f 20 6f 70 65 72 61 74 69 6f 6e 73 20 6f  I/O operations o
9dc0: 6e 20 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 2e  n the open file.
9dd0: 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
9de0: 63 74 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ct sqlite3_file 
9df0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 3b 0a 73 74  sqlite3_file;.st
9e00: 72 75 63 74 20 73 71 6c 69 74 65 33 5f 66 69 6c  ruct sqlite3_fil
9e10: 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 74 72 75  e {.  const stru
9e20: 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  ct sqlite3_io_me
9e30: 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73 3b  thods *pMethods;
9e40: 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f 72    /* Methods for
9e50: 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f   an open file */
9e60: 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .};../*.** CAPI3
9e70: 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63  REF: OS Interfac
9e80: 65 20 46 69 6c 65 20 56 69 72 74 75 61 6c 20 4d  e File Virtual M
9e90: 65 74 68 6f 64 73 20 4f 62 6a 65 63 74 20 7b 48  ethods Object {H
9ea0: 31 31 31 32 30 7d 20 3c 53 32 30 31 31 30 3e 0a  11120} <S20110>.
9eb0: 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 66 69 6c 65  **.** Every file
9ec0: 20 6f 70 65 6e 65 64 20 62 79 20 74 68 65 20 5b   opened by the [
9ed0: 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 78 4f 70  sqlite3_vfs] xOp
9ee0: 65 6e 20 6d 65 74 68 6f 64 20 70 6f 70 75 6c 61  en method popula
9ef0: 74 65 73 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74  tes an.** [sqlit
9f00: 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74 20  e3_file] object 
9f10: 28 6f 72 2c 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e  (or, more common
9f20: 6c 79 2c 20 61 20 73 75 62 63 6c 61 73 73 20 6f  ly, a subclass o
9f30: 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65  f the.** [sqlite
9f40: 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74 29 20  3_file] object) 
9f50: 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20 74  with a pointer t
9f60: 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  o an instance of
9f70: 20 74 68 69 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a   this object..**
9f80: 20 54 68 69 73 20 6f 62 6a 65 63 74 20 64 65 66   This object def
9f90: 69 6e 65 73 20 74 68 65 20 6d 65 74 68 6f 64 73  ines the methods
9fa0: 20 75 73 65 64 20 74 6f 20 70 65 72 66 6f 72 6d   used to perform
9fb0: 20 76 61 72 69 6f 75 73 20 6f 70 65 72 61 74 69   various operati
9fc0: 6f 6e 73 0a 2a 2a 20 61 67 61 69 6e 73 74 20 74  ons.** against t
9fd0: 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 72 65 70  he open file rep
9fe0: 72 65 73 65 6e 74 65 64 20 62 79 20 74 68 65 20  resented by the 
9ff0: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f  [sqlite3_file] o
a000: 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  bject..**.** If 
a010: 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64  the xOpen method
a020: 20 73 65 74 73 20 74 68 65 20 73 71 6c 69 74 65   sets the sqlite
a030: 33 5f 66 69 6c 65 2e 70 4d 65 74 68 6f 64 73 20  3_file.pMethods 
a040: 65 6c 65 6d 65 6e 74 20 0a 2a 2a 20 74 6f 20 61  element .** to a
a050: 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65   non-NULL pointe
a060: 72 2c 20 74 68 65 6e 20 74 68 65 20 73 71 6c 69  r, then the sqli
a070: 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 2e 78  te3_io_methods.x
a080: 43 6c 6f 73 65 20 6d 65 74 68 6f 64 0a 2a 2a 20  Close method.** 
a090: 6d 61 79 20 62 65 20 69 6e 76 6f 6b 65 64 20 65  may be invoked e
a0a0: 76 65 6e 20 69 66 20 74 68 65 20 78 4f 70 65 6e  ven if the xOpen
a0b0: 20 72 65 70 6f 72 74 65 64 20 74 68 61 74 20 69   reported that i
a0c0: 74 20 66 61 69 6c 65 64 2e 20 20 54 68 65 0a 2a  t failed.  The.*
a0d0: 2a 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 70 72  * only way to pr
a0e0: 65 76 65 6e 74 20 61 20 63 61 6c 6c 20 74 6f 20  event a call to 
a0f0: 78 43 6c 6f 73 65 20 66 6f 6c 6c 6f 77 69 6e 67  xClose following
a100: 20 61 20 66 61 69 6c 65 64 20 78 4f 70 65 6e 0a   a failed xOpen.
a110: 2a 2a 20 69 73 20 66 6f 72 20 74 68 65 20 78 4f  ** is for the xO
a120: 70 65 6e 20 74 6f 20 73 65 74 20 74 68 65 20 73  pen to set the s
a130: 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74  qlite3_file.pMet
a140: 68 6f 64 73 20 65 6c 65 6d 65 6e 74 20 74 6f 20  hods element to 
a150: 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  NULL..**.** The 
a160: 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74  flags argument t
a170: 6f 20 78 53 79 6e 63 20 6d 61 79 20 62 65 20 6f  o xSync may be o
a180: 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 53 59  ne of [SQLITE_SY
a190: 4e 43 5f 4e 4f 52 4d 41 4c 5d 20 6f 72 0a 2a 2a  NC_NORMAL] or.**
a1a0: 20 5b 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55   [SQLITE_SYNC_FU
a1b0: 4c 4c 5d 2e 20 20 54 68 65 20 66 69 72 73 74 20  LL].  The first 
a1c0: 63 68 6f 69 63 65 20 69 73 20 74 68 65 20 6e 6f  choice is the no
a1d0: 72 6d 61 6c 20 66 73 79 6e 63 28 29 2e 0a 2a 2a  rmal fsync()..**
a1e0: 20 54 68 65 20 73 65 63 6f 6e 64 20 63 68 6f 69   The second choi
a1f0: 63 65 20 69 73 20 61 20 4d 61 63 20 4f 53 20 58  ce is a Mac OS X
a200: 20 73 74 79 6c 65 20 66 75 6c 6c 73 79 6e 63 2e   style fullsync.
a210: 20 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 53 59    The [SQLITE_SY
a220: 4e 43 5f 44 41 54 41 4f 4e 4c 59 5d 0a 2a 2a 20  NC_DATAONLY].** 
a230: 66 6c 61 67 20 6d 61 79 20 62 65 20 4f 52 65 64  flag may be ORed
a240: 20 69 6e 20 74 6f 20 69 6e 64 69 63 61 74 65 20   in to indicate 
a250: 74 68 61 74 20 6f 6e 6c 79 20 74 68 65 20 64 61  that only the da
a260: 74 61 20 6f 66 20 74 68 65 20 66 69 6c 65 0a 2a  ta of the file.*
a270: 2a 20 61 6e 64 20 6e 6f 74 20 69 74 73 20 69 6e  * and not its in
a280: 6f 64 65 20 6e 65 65 64 73 20 74 6f 20 62 65 20  ode needs to be 
a290: 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  synced..**.** Th
a2a0: 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73  e integer values
a2b0: 20 74 6f 20 78 4c 6f 63 6b 28 29 20 61 6e 64 20   to xLock() and 
a2c0: 78 55 6e 6c 6f 63 6b 28 29 20 61 72 65 20 6f 6e  xUnlock() are on
a2d0: 65 20 6f 66 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  e of.** <ul>.** 
a2e0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43  <li> [SQLITE_LOC
a2f0: 4b 5f 4e 4f 4e 45 5d 2c 0a 2a 2a 20 3c 6c 69 3e  K_NONE],.** <li>
a300: 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48   [SQLITE_LOCK_SH
a310: 41 52 45 44 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b  ARED],.** <li> [
a320: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45  SQLITE_LOCK_RESE
a330: 52 56 45 44 5d 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b  RVED],.** <li> [
a340: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44  SQLITE_LOCK_PEND
a350: 49 4e 47 5d 2c 20 6f 72 0a 2a 2a 20 3c 6c 69 3e  ING], or.** <li>
a360: 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 45 58   [SQLITE_LOCK_EX
a370: 43 4c 55 53 49 56 45 5d 2e 0a 2a 2a 20 3c 2f 75  CLUSIVE]..** </u
a380: 6c 3e 0a 2a 2a 20 78 4c 6f 63 6b 28 29 20 69 6e  l>.** xLock() in
a390: 63 72 65 61 73 65 73 20 74 68 65 20 6c 6f 63 6b  creases the lock
a3a0: 2e 20 78 55 6e 6c 6f 63 6b 28 29 20 64 65 63 72  . xUnlock() decr
a3b0: 65 61 73 65 73 20 74 68 65 20 6c 6f 63 6b 2e 0a  eases the lock..
a3c0: 2a 2a 20 54 68 65 20 78 43 68 65 63 6b 52 65 73  ** The xCheckRes
a3d0: 65 72 76 65 64 4c 6f 63 6b 28 29 20 6d 65 74 68  ervedLock() meth
a3e0: 6f 64 20 63 68 65 63 6b 73 20 77 68 65 74 68 65  od checks whethe
a3f0: 72 20 61 6e 79 20 64 61 74 61 62 61 73 65 20 63  r any database c
a400: 6f 6e 6e 65 63 74 69 6f 6e 2c 0a 2a 2a 20 65 69  onnection,.** ei
a410: 74 68 65 72 20 69 6e 20 74 68 69 73 20 70 72 6f  ther in this pro
a420: 63 65 73 73 20 6f 72 20 69 6e 20 73 6f 6d 65 20  cess or in some 
a430: 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2c 20 69  other process, i
a440: 73 20 68 6f 6c 64 69 6e 67 20 61 20 52 45 53 45  s holding a RESE
a450: 52 56 45 44 2c 0a 2a 2a 20 50 45 4e 44 49 4e 47  RVED,.** PENDING
a460: 2c 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c  , or EXCLUSIVE l
a470: 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e  ock on the file.
a480: 20 20 49 74 20 72 65 74 75 72 6e 73 20 74 72 75    It returns tru
a490: 65 0a 2a 2a 20 69 66 20 73 75 63 68 20 61 20 6c  e.** if such a l
a4a0: 6f 63 6b 20 65 78 69 73 74 73 20 61 6e 64 20 66  ock exists and f
a4b0: 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65 2e 0a  alse otherwise..
a4c0: 2a 2a 0a 2a 2a 20 54 68 65 20 78 46 69 6c 65 43  **.** The xFileC
a4d0: 6f 6e 74 72 6f 6c 28 29 20 6d 65 74 68 6f 64 20  ontrol() method 
a4e0: 69 73 20 61 20 67 65 6e 65 72 69 63 20 69 6e 74  is a generic int
a4f0: 65 72 66 61 63 65 20 74 68 61 74 20 61 6c 6c 6f  erface that allo
a500: 77 73 20 63 75 73 74 6f 6d 0a 2a 2a 20 56 46 53  ws custom.** VFS
a510: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
a520: 20 74 6f 20 64 69 72 65 63 74 6c 79 20 63 6f 6e   to directly con
a530: 74 72 6f 6c 20 61 6e 20 6f 70 65 6e 20 66 69 6c  trol an open fil
a540: 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b  e using the.** [
a550: 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e  sqlite3_file_con
a560: 74 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61 63  trol()] interfac
a570: 65 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 22  e.  The second "
a580: 6f 70 22 20 61 72 67 75 6d 65 6e 74 20 69 73 20  op" argument is 
a590: 61 6e 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6f 70  an.** integer op
a5a0: 63 6f 64 65 2e 20 20 54 68 65 20 74 68 69 72 64  code.  The third
a5b0: 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 67   argument is a g
a5c0: 65 6e 65 72 69 63 20 70 6f 69 6e 74 65 72 20 69  eneric pointer i
a5d0: 6e 74 65 6e 64 65 64 20 74 6f 0a 2a 2a 20 70 6f  ntended to.** po
a5e0: 69 6e 74 20 74 6f 20 61 20 73 74 72 75 63 74 75  int to a structu
a5f0: 72 65 20 74 68 61 74 20 6d 61 79 20 63 6f 6e 74  re that may cont
a600: 61 69 6e 20 61 72 67 75 6d 65 6e 74 73 20 6f 72  ain arguments or
a610: 20 73 70 61 63 65 20 69 6e 20 77 68 69 63 68 20   space in which 
a620: 74 6f 0a 2a 2a 20 77 72 69 74 65 20 72 65 74 75  to.** write retu
a630: 72 6e 20 76 61 6c 75 65 73 2e 20 20 50 6f 74 65  rn values.  Pote
a640: 6e 74 69 61 6c 20 75 73 65 73 20 66 6f 72 20 78  ntial uses for x
a650: 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20 6d 69  FileControl() mi
a660: 67 68 74 20 62 65 0a 2a 2a 20 66 75 6e 63 74 69  ght be.** functi
a670: 6f 6e 73 20 74 6f 20 65 6e 61 62 6c 65 20 62 6c  ons to enable bl
a680: 6f 63 6b 69 6e 67 20 6c 6f 63 6b 73 20 77 69 74  ocking locks wit
a690: 68 20 74 69 6d 65 6f 75 74 73 2c 20 74 6f 20 63  h timeouts, to c
a6a0: 68 61 6e 67 65 20 74 68 65 0a 2a 2a 20 6c 6f 63  hange the.** loc
a6b0: 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 28 66  king strategy (f
a6c0: 6f 72 20 65 78 61 6d 70 6c 65 20 74 6f 20 75 73  or example to us
a6d0: 65 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73  e dot-file locks
a6e0: 29 2c 20 74 6f 20 69 6e 71 75 69 72 65 0a 2a 2a  ), to inquire.**
a6f0: 20 61 62 6f 75 74 20 74 68 65 20 73 74 61 74 75   about the statu
a700: 73 20 6f 66 20 61 20 6c 6f 63 6b 2c 20 6f 72 20  s of a lock, or 
a710: 74 6f 20 62 72 65 61 6b 20 73 74 61 6c 65 20 6c  to break stale l
a720: 6f 63 6b 73 2e 20 20 54 68 65 20 53 51 4c 69 74  ocks.  The SQLit
a730: 65 0a 2a 2a 20 63 6f 72 65 20 72 65 73 65 72 76  e.** core reserv
a740: 65 73 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 6c  es all opcodes l
a750: 65 73 73 20 74 68 61 6e 20 31 30 30 20 66 6f 72  ess than 100 for
a760: 20 69 74 73 20 6f 77 6e 20 75 73 65 2e 0a 2a 2a   its own use..**
a770: 20 41 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c   A [SQLITE_FCNTL
a780: 5f 4c 4f 43 4b 53 54 41 54 45 20 7c 20 6c 69 73  _LOCKSTATE | lis
a790: 74 20 6f 66 20 6f 70 63 6f 64 65 73 5d 20 6c 65  t of opcodes] le
a7a0: 73 73 20 74 68 61 6e 20 31 30 30 20 69 73 20 61  ss than 100 is a
a7b0: 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 41 70 70  vailable..** App
a7c0: 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 64  lications that d
a7d0: 65 66 69 6e 65 20 61 20 63 75 73 74 6f 6d 20 78  efine a custom x
a7e0: 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68  FileControl meth
a7f0: 6f 64 20 73 68 6f 75 6c 64 20 75 73 65 20 6f 70  od should use op
a800: 63 6f 64 65 73 0a 2a 2a 20 67 72 65 61 74 65 72  codes.** greater
a810: 20 74 68 61 6e 20 31 30 30 20 74 6f 20 61 76 6f   than 100 to avo
a820: 69 64 20 63 6f 6e 66 6c 69 63 74 73 2e 0a 2a 2a  id conflicts..**
a830: 0a 2a 2a 20 54 68 65 20 78 53 65 63 74 6f 72 53  .** The xSectorS
a840: 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 72 65 74  ize() method ret
a850: 75 72 6e 73 20 74 68 65 20 73 65 63 74 6f 72 20  urns the sector 
a860: 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64  size of the.** d
a870: 65 76 69 63 65 20 74 68 61 74 20 75 6e 64 65 72  evice that under
a880: 6c 69 65 73 20 74 68 65 20 66 69 6c 65 2e 20 20  lies the file.  
a890: 54 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20  The sector size 
a8a0: 69 73 20 74 68 65 0a 2a 2a 20 6d 69 6e 69 6d 75  is the.** minimu
a8b0: 6d 20 77 72 69 74 65 20 74 68 61 74 20 63 61 6e  m write that can
a8c0: 20 62 65 20 70 65 72 66 6f 72 6d 65 64 20 77 69   be performed wi
a8d0: 74 68 6f 75 74 20 64 69 73 74 75 72 62 69 6e 67  thout disturbing
a8e0: 0a 2a 2a 20 6f 74 68 65 72 20 62 79 74 65 73 20  .** other bytes 
a8f0: 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68  in the file.  Th
a900: 65 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74  e xDeviceCharact
a910: 65 72 69 73 74 69 63 73 28 29 0a 2a 2a 20 6d 65  eristics().** me
a920: 74 68 6f 64 20 72 65 74 75 72 6e 73 20 61 20 62  thod returns a b
a930: 69 74 20 76 65 63 74 6f 72 20 64 65 73 63 72 69  it vector descri
a940: 62 69 6e 67 20 62 65 68 61 76 69 6f 72 73 20 6f  bing behaviors o
a950: 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79  f the.** underly
a960: 69 6e 67 20 64 65 76 69 63 65 3a 0a 2a 2a 0a 2a  ing device:.**.*
a970: 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b  * <ul>.** <li> [
a980: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
a990: 4d 49 43 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51  MIC].** <li> [SQ
a9a0: 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49  LITE_IOCAP_ATOMI
a9b0: 43 35 31 32 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53  C512].** <li> [S
a9c0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
a9d0: 49 43 31 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53  IC1K].** <li> [S
a9e0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
a9f0: 49 43 32 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53  IC2K].** <li> [S
aa00: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
aa10: 49 43 34 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53  IC4K].** <li> [S
aa20: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
aa30: 49 43 38 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53  IC8K].** <li> [S
aa40: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
aa50: 49 43 31 36 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b  IC16K].** <li> [
aa60: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
aa70: 4d 49 43 33 32 4b 5d 0a 2a 2a 20 3c 6c 69 3e 20  MIC32K].** <li> 
aa80: 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54  [SQLITE_IOCAP_AT
aa90: 4f 4d 49 43 36 34 4b 5d 0a 2a 2a 20 3c 6c 69 3e  OMIC64K].** <li>
aaa0: 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53   [SQLITE_IOCAP_S
aab0: 41 46 45 5f 41 50 50 45 4e 44 5d 0a 2a 2a 20 3c  AFE_APPEND].** <
aac0: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41  li> [SQLITE_IOCA
aad0: 50 5f 53 45 51 55 45 4e 54 49 41 4c 5d 0a 2a 2a  P_SEQUENTIAL].**
aae0: 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65   </ul>.**.** The
aaf0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54   SQLITE_IOCAP_AT
ab00: 4f 4d 49 43 20 70 72 6f 70 65 72 74 79 20 6d 65  OMIC property me
ab10: 61 6e 73 20 74 68 61 74 20 61 6c 6c 20 77 72 69  ans that all wri
ab20: 74 65 73 20 6f 66 0a 2a 2a 20 61 6e 79 20 73 69  tes of.** any si
ab30: 7a 65 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20  ze are atomic.  
ab40: 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  The SQLITE_IOCAP
ab50: 5f 41 54 4f 4d 49 43 6e 6e 6e 20 76 61 6c 75 65  _ATOMICnnn value
ab60: 73 0a 2a 2a 20 6d 65 61 6e 20 74 68 61 74 20 77  s.** mean that w
ab70: 72 69 74 65 73 20 6f 66 20 62 6c 6f 63 6b 73 20  rites of blocks 
ab80: 74 68 61 74 20 61 72 65 20 6e 6e 6e 20 62 79 74  that are nnn byt
ab90: 65 73 20 69 6e 20 73 69 7a 65 20 61 6e 64 0a 2a  es in size and.*
aba0: 2a 20 61 72 65 20 61 6c 69 67 6e 65 64 20 74 6f  * are aligned to
abb0: 20 61 6e 20 61 64 64 72 65 73 73 20 77 68 69 63   an address whic
abc0: 68 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20  h is an integer 
abd0: 6d 75 6c 74 69 70 6c 65 20 6f 66 0a 2a 2a 20 6e  multiple of.** n
abe0: 6e 6e 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20  nn are atomic.  
abf0: 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  The SQLITE_IOCAP
ac00: 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 76 61 6c  _SAFE_APPEND val
ac10: 75 65 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74  ue means.** that
ac20: 20 77 68 65 6e 20 64 61 74 61 20 69 73 20 61 70   when data is ap
ac30: 70 65 6e 64 65 64 20 74 6f 20 61 20 66 69 6c 65  pended to a file
ac40: 2c 20 74 68 65 20 64 61 74 61 20 69 73 20 61 70  , the data is ap
ac50: 70 65 6e 64 65 64 0a 2a 2a 20 66 69 72 73 74 20  pended.** first 
ac60: 74 68 65 6e 20 74 68 65 20 73 69 7a 65 20 6f 66  then the size of
ac70: 20 74 68 65 20 66 69 6c 65 20 69 73 20 65 78 74   the file is ext
ac80: 65 6e 64 65 64 2c 20 6e 65 76 65 72 20 74 68 65  ended, never the
ac90: 20 6f 74 68 65 72 0a 2a 2a 20 77 61 79 20 61 72   other.** way ar
aca0: 6f 75 6e 64 2e 20 20 54 68 65 20 53 51 4c 49 54  ound.  The SQLIT
acb0: 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49  E_IOCAP_SEQUENTI
acc0: 41 4c 20 70 72 6f 70 65 72 74 79 20 6d 65 61 6e  AL property mean
acd0: 73 20 74 68 61 74 0a 2a 2a 20 69 6e 66 6f 72 6d  s that.** inform
ace0: 61 74 69 6f 6e 20 69 73 20 77 72 69 74 74 65 6e  ation is written
acf0: 20 74 6f 20 64 69 73 6b 20 69 6e 20 74 68 65 20   to disk in the 
ad00: 73 61 6d 65 20 6f 72 64 65 72 20 61 73 20 63 61  same order as ca
ad10: 6c 6c 73 0a 2a 2a 20 74 6f 20 78 57 72 69 74 65  lls.** to xWrite
ad20: 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 78 52 65  ()..**.** If xRe
ad30: 61 64 28 29 20 72 65 74 75 72 6e 73 20 53 51 4c  ad() returns SQL
ad40: 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f  ITE_IOERR_SHORT_
ad50: 52 45 41 44 20 69 74 20 6d 75 73 74 20 61 6c 73  READ it must als
ad60: 6f 20 66 69 6c 6c 0a 2a 2a 20 69 6e 20 74 68 65  o fill.** in the
ad70: 20 75 6e 72 65 61 64 20 70 6f 72 74 69 6f 6e 73   unread portions
ad80: 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 77   of the buffer w
ad90: 69 74 68 20 7a 65 72 6f 73 2e 20 20 41 20 56 46  ith zeros.  A VF
ada0: 53 20 74 68 61 74 0a 2a 2a 20 66 61 69 6c 73 20  S that.** fails 
adb0: 74 6f 20 7a 65 72 6f 2d 66 69 6c 6c 20 73 68 6f  to zero-fill sho
adc0: 72 74 20 72 65 61 64 73 20 6d 69 67 68 74 20 73  rt reads might s
add0: 65 65 6d 20 74 6f 20 77 6f 72 6b 2e 20 20 48 6f  eem to work.  Ho
ade0: 77 65 76 65 72 2c 0a 2a 2a 20 66 61 69 6c 75 72  wever,.** failur
adf0: 65 20 74 6f 20 7a 65 72 6f 2d 66 69 6c 6c 20 73  e to zero-fill s
ae00: 68 6f 72 74 20 72 65 61 64 73 20 77 69 6c 6c 20  hort reads will 
ae10: 65 76 65 6e 74 75 61 6c 6c 79 20 6c 65 61 64 20  eventually lead 
ae20: 74 6f 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  to.** database c
ae30: 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 74 79  orruption..*/.ty
ae40: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
ae50: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
ae60: 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
ae70: 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74  ds;.struct sqlit
ae80: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 7b 0a  e3_io_methods {.
ae90: 20 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a    int iVersion;.
aea0: 20 20 69 6e 74 20 28 2a 78 43 6c 6f 73 65 29 28    int (*xClose)(
aeb0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a  sqlite3_file*);.
aec0: 20 20 69 6e 74 20 28 2a 78 52 65 61 64 29 28 73    int (*xRead)(s
aed0: 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 76 6f  qlite3_file*, vo
aee0: 69 64 2a 2c 20 69 6e 74 20 69 41 6d 74 2c 20 73  id*, int iAmt, s
aef0: 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66  qlite3_int64 iOf
af00: 73 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 57 72  st);.  int (*xWr
af10: 69 74 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ite)(sqlite3_fil
af20: 65 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  e*, const void*,
af30: 20 69 6e 74 20 69 41 6d 74 2c 20 73 71 6c 69 74   int iAmt, sqlit
af40: 65 33 5f 69 6e 74 36 34 20 69 4f 66 73 74 29 3b  e3_int64 iOfst);
af50: 0a 20 20 69 6e 74 20 28 2a 78 54 72 75 6e 63 61  .  int (*xTrunca
af60: 74 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  te)(sqlite3_file
af70: 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  *, sqlite3_int64
af80: 20 73 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a   size);.  int (*
af90: 78 53 79 6e 63 29 28 73 71 6c 69 74 65 33 5f 66  xSync)(sqlite3_f
afa0: 69 6c 65 2a 2c 20 69 6e 74 20 66 6c 61 67 73 29  ile*, int flags)
afb0: 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69 6c 65 53  ;.  int (*xFileS
afc0: 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ize)(sqlite3_fil
afd0: 65 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  e*, sqlite3_int6
afe0: 34 20 2a 70 53 69 7a 65 29 3b 0a 20 20 69 6e 74  4 *pSize);.  int
aff0: 20 28 2a 78 4c 6f 63 6b 29 28 73 71 6c 69 74 65   (*xLock)(sqlite
b000: 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20  3_file*, int);. 
b010: 20 69 6e 74 20 28 2a 78 55 6e 6c 6f 63 6b 29 28   int (*xUnlock)(
b020: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69  sqlite3_file*, i
b030: 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 68  nt);.  int (*xCh
b040: 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 29  eckReservedLock)
b050: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20  (sqlite3_file*, 
b060: 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 3b 0a 20  int *pResOut);. 
b070: 20 69 6e 74 20 28 2a 78 46 69 6c 65 43 6f 6e 74   int (*xFileCont
b080: 72 6f 6c 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  rol)(sqlite3_fil
b090: 65 2a 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64  e*, int op, void
b0a0: 20 2a 70 41 72 67 29 3b 0a 20 20 69 6e 74 20 28   *pArg);.  int (
b0b0: 2a 78 53 65 63 74 6f 72 53 69 7a 65 29 28 73 71  *xSectorSize)(sq
b0c0: 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20  lite3_file*);.  
b0d0: 69 6e 74 20 28 2a 78 44 65 76 69 63 65 43 68 61  int (*xDeviceCha
b0e0: 72 61 63 74 65 72 69 73 74 69 63 73 29 28 73 71  racteristics)(sq
b0f0: 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 20 20  lite3_file*);.  
b100: 2f 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 6d 65  /* Additional me
b110: 74 68 6f 64 73 20 6d 61 79 20 62 65 20 61 64 64  thods may be add
b120: 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c  ed in future rel
b130: 65 61 73 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  eases */.};../*.
b140: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 74 61  ** CAPI3REF: Sta
b150: 6e 64 61 72 64 20 46 69 6c 65 20 43 6f 6e 74 72  ndard File Contr
b160: 6f 6c 20 4f 70 63 6f 64 65 73 20 7b 48 31 31 33  ol Opcodes {H113
b170: 31 30 7d 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a  10} <S30800>.**.
b180: 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72  ** These integer
b190: 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 6f   constants are o
b1a0: 70 63 6f 64 65 73 20 66 6f 72 20 74 68 65 20 78  pcodes for the x
b1b0: 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68  FileControl meth
b1c0: 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71  od.** of the [sq
b1d0: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
b1e0: 5d 20 6f 62 6a 65 63 74 20 61 6e 64 20 66 6f 72  ] object and for
b1f0: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69   the [sqlite3_fi
b200: 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 0a 2a 2a  le_control()].**
b210: 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a   interface..**.*
b220: 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 46 43  * The [SQLITE_FC
b230: 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 5d 20 6f  NTL_LOCKSTATE] o
b240: 70 63 6f 64 65 20 69 73 20 75 73 65 64 20 66 6f  pcode is used fo
b250: 72 20 64 65 62 75 67 67 69 6e 67 2e 20 20 54 68  r debugging.  Th
b260: 69 73 0a 2a 2a 20 6f 70 63 6f 64 65 20 63 61 75  is.** opcode cau
b270: 73 65 73 20 74 68 65 20 78 46 69 6c 65 43 6f 6e  ses the xFileCon
b280: 74 72 6f 6c 20 6d 65 74 68 6f 64 20 74 6f 20 77  trol method to w
b290: 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74  rite the current
b2a0: 20 73 74 61 74 65 20 6f 66 0a 2a 2a 20 74 68 65   state of.** the
b2b0: 20 6c 6f 63 6b 20 28 6f 6e 65 20 6f 66 20 5b 53   lock (one of [S
b2c0: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d  QLITE_LOCK_NONE]
b2d0: 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53  , [SQLITE_LOCK_S
b2e0: 48 41 52 45 44 5d 2c 0a 2a 2a 20 5b 53 51 4c 49  HARED],.** [SQLI
b2f0: 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44  TE_LOCK_RESERVED
b300: 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f  ], [SQLITE_LOCK_
b310: 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 20 5b 53 51  PENDING], or [SQ
b320: 4c 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53  LITE_LOCK_EXCLUS
b330: 49 56 45 5d 29 0a 2a 2a 20 69 6e 74 6f 20 61 6e  IVE]).** into an
b340: 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 74 68   integer that th
b350: 65 20 70 41 72 67 20 61 72 67 75 6d 65 6e 74 20  e pArg argument 
b360: 70 6f 69 6e 74 73 20 74 6f 2e 20 54 68 69 73 20  points to. This 
b370: 63 61 70 61 62 69 6c 69 74 79 0a 2a 2a 20 69 73  capability.** is
b380: 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 73   used during tes
b390: 74 69 6e 67 20 61 6e 64 20 6f 6e 6c 79 20 6e 65  ting and only ne
b3a0: 65 64 73 20 74 6f 20 62 65 20 73 75 70 70 6f 72  eds to be suppor
b3b0: 74 65 64 20 77 68 65 6e 20 53 51 4c 49 54 45 5f  ted when SQLITE_
b3c0: 54 45 53 54 0a 2a 2a 20 69 73 20 64 65 66 69 6e  TEST.** is defin
b3d0: 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ed..*/.#define S
b3e0: 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b  QLITE_FCNTL_LOCK
b3f0: 53 54 41 54 45 20 20 20 20 20 20 20 20 31 0a 23  STATE        1.#
b400: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 47 45  define SQLITE_GE
b410: 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 20  T_LOCKPROXYFILE 
b420: 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53       2.#define S
b430: 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52  QLITE_SET_LOCKPR
b440: 4f 58 59 46 49 4c 45 20 20 20 20 20 20 33 0a 23  OXYFILE      3.#
b450: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 41  define SQLITE_LA
b460: 53 54 5f 45 52 52 4e 4f 20 20 20 20 20 20 20 20  ST_ERRNO        
b470: 20 20 20 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 43 41       4../*.** CA
b480: 50 49 33 52 45 46 3a 20 4d 75 74 65 78 20 48 61  PI3REF: Mutex Ha
b490: 6e 64 6c 65 20 7b 48 31 37 31 31 30 7d 20 3c 53  ndle {H17110} <S
b4a0: 32 30 31 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  20130>.**.** The
b4b0: 20 6d 75 74 65 78 20 6d 6f 64 75 6c 65 20 77 69   mutex module wi
b4c0: 74 68 69 6e 20 53 51 4c 69 74 65 20 64 65 66 69  thin SQLite defi
b4d0: 6e 65 73 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74  nes [sqlite3_mut
b4e0: 65 78 5d 20 74 6f 20 62 65 20 61 6e 0a 2a 2a 20  ex] to be an.** 
b4f0: 61 62 73 74 72 61 63 74 20 74 79 70 65 20 66 6f  abstract type fo
b500: 72 20 61 20 6d 75 74 65 78 20 6f 62 6a 65 63 74  r a mutex object
b510: 2e 20 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f  .  The SQLite co
b520: 72 65 20 6e 65 76 65 72 20 6c 6f 6f 6b 73 0a 2a  re never looks.*
b530: 2a 20 61 74 20 74 68 65 20 69 6e 74 65 72 6e 61  * at the interna
b540: 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  l representation
b550: 20 6f 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f   of an [sqlite3_
b560: 6d 75 74 65 78 5d 2e 20 20 49 74 20 6f 6e 6c 79  mutex].  It only
b570: 0a 2a 2a 20 64 65 61 6c 73 20 77 69 74 68 20 70  .** deals with p
b580: 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 5b  ointers to the [
b590: 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20 6f  sqlite3_mutex] o
b5a0: 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 4d 75 74  bject..**.** Mut
b5b0: 65 78 65 73 20 61 72 65 20 63 72 65 61 74 65 64  exes are created
b5c0: 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
b5d0: 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d 2e 0a  mutex_alloc()]..
b5e0: 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
b5f0: 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  t sqlite3_mutex 
b600: 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 3b 0a 0a  sqlite3_mutex;..
b610: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
b620: 4f 53 20 49 6e 74 65 72 66 61 63 65 20 4f 62 6a  OS Interface Obj
b630: 65 63 74 20 7b 48 31 31 31 34 30 7d 20 3c 53 32  ect {H11140} <S2
b640: 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69  0100>.**.** An i
b650: 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73  nstance of the s
b660: 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63  qlite3_vfs objec
b670: 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e  t defines the in
b680: 74 65 72 66 61 63 65 20 62 65 74 77 65 65 6e 0a  terface between.
b690: 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f  ** the SQLite co
b6a0: 72 65 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72  re and the under
b6b0: 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20  lying operating 
b6c0: 73 79 73 74 65 6d 2e 20 20 54 68 65 20 22 76 66  system.  The "vf
b6d0: 73 22 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61 6d  s".** in the nam
b6e0: 65 20 6f 66 20 74 68 65 20 6f 62 6a 65 63 74 20  e of the object 
b6f0: 73 74 61 6e 64 73 20 66 6f 72 20 22 76 69 72 74  stands for "virt
b700: 75 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 22  ual file system"
b710: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75  ..**.** The valu
b720: 65 20 6f 66 20 74 68 65 20 69 56 65 72 73 69 6f  e of the iVersio
b730: 6e 20 66 69 65 6c 64 20 69 73 20 69 6e 69 74 69  n field is initi
b740: 61 6c 6c 79 20 31 20 62 75 74 20 6d 61 79 20 62  ally 1 but may b
b750: 65 20 6c 61 72 67 65 72 20 69 6e 0a 2a 2a 20 66  e larger in.** f
b760: 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f  uture versions o
b770: 66 20 53 51 4c 69 74 65 2e 20 20 41 64 64 69 74  f SQLite.  Addit
b780: 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 6d 61 79  ional fields may
b790: 20 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20   be appended to 
b7a0: 74 68 69 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 77  this.** object w
b7b0: 68 65 6e 20 74 68 65 20 69 56 65 72 73 69 6f 6e  hen the iVersion
b7c0: 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61   value is increa
b7d0: 73 65 64 2e 20 20 4e 6f 74 65 20 74 68 61 74 20  sed.  Note that 
b7e0: 74 68 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a  the structure.**
b7f0: 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
b800: 76 66 73 20 6f 62 6a 65 63 74 20 63 68 61 6e 67  vfs object chang
b810: 65 73 20 69 6e 20 74 68 65 20 74 72 61 6e 73 61  es in the transa
b820: 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 0a 2a 2a  ction between.**
b830: 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20   SQLite version 
b840: 33 2e 35 2e 39 20 61 6e 64 20 33 2e 36 2e 30 20  3.5.9 and 3.6.0 
b850: 61 6e 64 20 79 65 74 20 74 68 65 20 69 56 65 72  and yet the iVer
b860: 73 69 6f 6e 20 66 69 65 6c 64 20 77 61 73 20 6e  sion field was n
b870: 6f 74 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 2e 0a  ot.** modified..
b880: 2a 2a 0a 2a 2a 20 54 68 65 20 73 7a 4f 73 46 69  **.** The szOsFi
b890: 6c 65 20 66 69 65 6c 64 20 69 73 20 74 68 65 20  le field is the 
b8a0: 73 69 7a 65 20 6f 66 20 74 68 65 20 73 75 62 63  size of the subc
b8b0: 6c 61 73 73 65 64 20 5b 73 71 6c 69 74 65 33 5f  lassed [sqlite3_
b8c0: 66 69 6c 65 5d 0a 2a 2a 20 73 74 72 75 63 74 75  file].** structu
b8d0: 72 65 20 75 73 65 64 20 62 79 20 74 68 69 73 20  re used by this 
b8e0: 56 46 53 2e 20 20 6d 78 50 61 74 68 6e 61 6d 65  VFS.  mxPathname
b8f0: 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20   is the maximum 
b900: 6c 65 6e 67 74 68 20 6f 66 0a 2a 2a 20 61 20 70  length of.** a p
b910: 61 74 68 6e 61 6d 65 20 69 6e 20 74 68 69 73 20  athname in this 
b920: 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73  VFS..**.** Regis
b930: 74 65 72 65 64 20 73 71 6c 69 74 65 33 5f 76 66  tered sqlite3_vf
b940: 73 20 6f 62 6a 65 63 74 73 20 61 72 65 20 6b 65  s objects are ke
b950: 70 74 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c  pt on a linked l
b960: 69 73 74 20 66 6f 72 6d 65 64 20 62 79 0a 2a 2a  ist formed by.**
b970: 20 74 68 65 20 70 4e 65 78 74 20 70 6f 69 6e 74   the pNext point
b980: 65 72 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65  er.  The [sqlite
b990: 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 29  3_vfs_register()
b9a0: 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65  ].** and [sqlite
b9b0: 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72  3_vfs_unregister
b9c0: 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 6d  ()] interfaces m
b9d0: 61 6e 61 67 65 20 74 68 69 73 20 6c 69 73 74 0a  anage this list.
b9e0: 2a 2a 20 69 6e 20 61 20 74 68 72 65 61 64 2d 73  ** in a thread-s
b9f0: 61 66 65 20 77 61 79 2e 20 20 54 68 65 20 5b 73  afe way.  The [s
ba00: 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28  qlite3_vfs_find(
ba10: 29 5d 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20  )] interface.** 
ba20: 73 65 61 72 63 68 65 73 20 74 68 65 20 6c 69 73  searches the lis
ba30: 74 2e 20 20 4e 65 69 74 68 65 72 20 74 68 65 20  t.  Neither the 
ba40: 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 6f 64 65  application code
ba50: 20 6e 6f 72 20 74 68 65 20 56 46 53 0a 2a 2a 20   nor the VFS.** 
ba60: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 73  implementation s
ba70: 68 6f 75 6c 64 20 75 73 65 20 74 68 65 20 70 4e  hould use the pN
ba80: 65 78 74 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a  ext pointer..**.
ba90: 2a 2a 20 54 68 65 20 70 4e 65 78 74 20 66 69 65  ** The pNext fie
baa0: 6c 64 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 66  ld is the only f
bab0: 69 65 6c 64 20 69 6e 20 74 68 65 20 73 71 6c 69  ield in the sqli
bac0: 74 65 33 5f 76 66 73 0a 2a 2a 20 73 74 72 75 63  te3_vfs.** struc
bad0: 74 75 72 65 20 74 68 61 74 20 53 51 4c 69 74 65  ture that SQLite
bae0: 20 77 69 6c 6c 20 65 76 65 72 20 6d 6f 64 69 66   will ever modif
baf0: 79 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  y.  SQLite will 
bb00: 6f 6e 6c 79 20 61 63 63 65 73 73 0a 2a 2a 20 6f  only access.** o
bb10: 72 20 6d 6f 64 69 66 79 20 74 68 69 73 20 66 69  r modify this fi
bb20: 65 6c 64 20 77 68 69 6c 65 20 68 6f 6c 64 69 6e  eld while holdin
bb30: 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 73  g a particular s
bb40: 74 61 74 69 63 20 6d 75 74 65 78 2e 0a 2a 2a 20  tatic mutex..** 
bb50: 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  The application 
bb60: 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 6d 6f 64  should never mod
bb70: 69 66 79 20 61 6e 79 74 68 69 6e 67 20 77 69 74  ify anything wit
bb80: 68 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f  hin the sqlite3_
bb90: 76 66 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 6f 6e  vfs.** object on
bba0: 63 65 20 74 68 65 20 6f 62 6a 65 63 74 20 68 61  ce the object ha
bbb0: 73 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65  s been registere
bbc0: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 4e 61  d..**.** The zNa
bbd0: 6d 65 20 66 69 65 6c 64 20 68 6f 6c 64 73 20 74  me field holds t
bbe0: 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 56  he name of the V
bbf0: 46 53 20 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20  FS module.  The 
bc00: 6e 61 6d 65 20 6d 75 73 74 0a 2a 2a 20 62 65 20  name must.** be 
bc10: 75 6e 69 71 75 65 20 61 63 72 6f 73 73 20 61 6c  unique across al
bc20: 6c 20 56 46 53 20 6d 6f 64 75 6c 65 73 2e 0a 2a  l VFS modules..*
bc30: 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c  *.** SQLite will
bc40: 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20   guarantee that 
bc50: 74 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 70 61  the zFilename pa
bc60: 72 61 6d 65 74 65 72 20 74 6f 20 78 4f 70 65 6e  rameter to xOpen
bc70: 0a 2a 2a 20 69 73 20 65 69 74 68 65 72 20 61 20  .** is either a 
bc80: 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 6f 72 20  NULL pointer or 
bc90: 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 0a  string obtained.
bca0: 2a 2a 20 66 72 6f 6d 20 78 46 75 6c 6c 50 61 74  ** from xFullPat
bcb0: 68 6e 61 6d 65 28 29 2e 20 20 53 51 4c 69 74 65  hname().  SQLite
bcc0: 20 66 75 72 74 68 65 72 20 67 75 61 72 61 6e 74   further guarant
bcd0: 65 65 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20  ees that.** the 
bce0: 73 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 76  string will be v
bcf0: 61 6c 69 64 20 61 6e 64 20 75 6e 63 68 61 6e 67  alid and unchang
bd00: 65 64 20 75 6e 74 69 6c 20 78 43 6c 6f 73 65 28  ed until xClose(
bd10: 29 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e 20  ) is.** called. 
bd20: 42 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 70  Because of the p
bd30: 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65  revious sentence
bd40: 2c 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65  ,.** the [sqlite
bd50: 33 5f 66 69 6c 65 5d 20 63 61 6e 20 73 61 66 65  3_file] can safe
bd60: 6c 79 20 73 74 6f 72 65 20 61 20 70 6f 69 6e 74  ly store a point
bd70: 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 66 69 6c  er to the.** fil
bd80: 65 6e 61 6d 65 20 69 66 20 69 74 20 6e 65 65 64  ename if it need
bd90: 73 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68  s to remember th
bda0: 65 20 66 69 6c 65 6e 61 6d 65 20 66 6f 72 20 73  e filename for s
bdb0: 6f 6d 65 20 72 65 61 73 6f 6e 2e 0a 2a 2a 20 49  ome reason..** I
bdc0: 66 20 74 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20  f the zFilename 
bdd0: 70 61 72 61 6d 65 74 65 72 20 69 73 20 78 4f 70  parameter is xOp
bde0: 65 6e 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69  en is a NULL poi
bdf0: 6e 74 65 72 20 74 68 65 6e 20 78 4f 70 65 6e 0a  nter then xOpen.
be00: 2a 2a 20 6d 75 73 74 20 69 6e 76 65 6e 74 20 69  ** must invent i
be10: 74 73 20 6f 77 6e 20 74 65 6d 70 6f 72 61 72 79  ts own temporary
be20: 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 66 69   name for the fi
be30: 6c 65 2e 20 20 57 68 65 6e 65 76 65 72 20 74 68  le.  Whenever th
be40: 65 20 0a 2a 2a 20 78 46 69 6c 65 6e 61 6d 65 20  e .** xFilename 
be50: 70 61 72 61 6d 65 74 65 72 20 69 73 20 4e 55 4c  parameter is NUL
be60: 4c 20 69 74 20 77 69 6c 6c 20 61 6c 73 6f 20 62  L it will also b
be70: 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20  e the case that 
be80: 74 68 65 0a 2a 2a 20 66 6c 61 67 73 20 70 61 72  the.** flags par
be90: 61 6d 65 74 65 72 20 77 69 6c 6c 20 69 6e 63 6c  ameter will incl
bea0: 75 64 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  ude [SQLITE_OPEN
beb0: 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 2e  _DELETEONCLOSE].
bec0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73  .**.** The flags
bed0: 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 4f 70   argument to xOp
bee0: 65 6e 28 29 20 69 6e 63 6c 75 64 65 73 20 61 6c  en() includes al
bef0: 6c 20 62 69 74 73 20 73 65 74 20 69 6e 0a 2a 2a  l bits set in.**
bf00: 20 74 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d   the flags argum
bf10: 65 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  ent to [sqlite3_
bf20: 6f 70 65 6e 5f 76 32 28 29 5d 2e 20 20 4f 72 20  open_v2()].  Or 
bf30: 69 66 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  if [sqlite3_open
bf40: 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74  ()].** or [sqlit
bf50: 65 33 5f 6f 70 65 6e 31 36 28 29 5d 20 69 73 20  e3_open16()] is 
bf60: 75 73 65 64 2c 20 74 68 65 6e 20 66 6c 61 67 73  used, then flags
bf70: 20 69 6e 63 6c 75 64 65 73 20 61 74 20 6c 65 61   includes at lea
bf80: 73 74 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50  st.** [SQLITE_OP
bf90: 45 4e 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20  EN_READWRITE] | 
bfa0: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45  [SQLITE_OPEN_CRE
bfb0: 41 54 45 5d 2e 20 0a 2a 2a 20 49 66 20 78 4f 70  ATE]. .** If xOp
bfc0: 65 6e 28 29 20 6f 70 65 6e 73 20 61 20 66 69 6c  en() opens a fil
bfd0: 65 20 72 65 61 64 2d 6f 6e 6c 79 20 74 68 65 6e  e read-only then
bfe0: 20 69 74 20 73 65 74 73 20 2a 70 4f 75 74 46 6c   it sets *pOutFl
bff0: 61 67 73 20 74 6f 0a 2a 2a 20 69 6e 63 6c 75 64  ags to.** includ
c000: 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  e [SQLITE_OPEN_R
c010: 45 41 44 4f 4e 4c 59 5d 2e 20 20 4f 74 68 65 72  EADONLY].  Other
c020: 20 62 69 74 73 20 69 6e 20 2a 70 4f 75 74 46 6c   bits in *pOutFl
c030: 61 67 73 20 6d 61 79 20 62 65 20 73 65 74 2e 0a  ags may be set..
c040: 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c  **.** SQLite wil
c050: 6c 20 61 6c 73 6f 20 61 64 64 20 6f 6e 65 20 6f  l also add one o
c060: 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
c070: 66 6c 61 67 73 20 74 6f 20 74 68 65 20 78 4f 70  flags to the xOp
c080: 65 6e 28 29 0a 2a 2a 20 63 61 6c 6c 2c 20 64 65  en().** call, de
c090: 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f  pending on the o
c0a0: 62 6a 65 63 74 20 62 65 69 6e 67 20 6f 70 65 6e  bject being open
c0b0: 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a  ed:.**.** <ul>.*
c0c0: 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f  * <li>  [SQLITE_
c0d0: 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 5d 0a 2a 2a  OPEN_MAIN_DB].**
c0e0: 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f   <li>  [SQLITE_O
c0f0: 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c  PEN_MAIN_JOURNAL
c100: 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49  ].** <li>  [SQLI
c110: 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 5d  TE_OPEN_TEMP_DB]
c120: 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54  .** <li>  [SQLIT
c130: 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52  E_OPEN_TEMP_JOUR
c140: 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53  NAL].** <li>  [S
c150: 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53  QLITE_OPEN_TRANS
c160: 49 45 4e 54 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e  IENT_DB].** <li>
c170: 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53    [SQLITE_OPEN_S
c180: 55 42 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c  UBJOURNAL].** <l
c190: 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  i>  [SQLITE_OPEN
c1a0: 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 5d  _MASTER_JOURNAL]
c1b0: 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20  .** </ul>.**.** 
c1c0: 54 68 65 20 66 69 6c 65 20 49 2f 4f 20 69 6d 70  The file I/O imp
c1d0: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20  lementation can 
c1e0: 75 73 65 20 74 68 65 20 6f 62 6a 65 63 74 20 74  use the object t
c1f0: 79 70 65 20 66 6c 61 67 73 20 74 6f 0a 2a 2a 20  ype flags to.** 
c200: 63 68 61 6e 67 65 20 74 68 65 20 77 61 79 20 69  change the way i
c210: 74 20 64 65 61 6c 73 20 77 69 74 68 20 66 69 6c  t deals with fil
c220: 65 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  es.  For example
c230: 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e  , an application
c240: 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f  .** that does no
c250: 74 20 63 61 72 65 20 61 62 6f 75 74 20 63 72 61  t care about cra
c260: 73 68 20 72 65 63 6f 76 65 72 79 20 6f 72 20 72  sh recovery or r
c270: 6f 6c 6c 62 61 63 6b 20 6d 69 67 68 74 20 6d 61  ollback might ma
c280: 6b 65 0a 2a 2a 20 74 68 65 20 6f 70 65 6e 20 6f  ke.** the open o
c290: 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  f a journal file
c2a0: 20 61 20 6e 6f 2d 6f 70 2e 20 20 57 72 69 74 65   a no-op.  Write
c2b0: 73 20 74 6f 20 74 68 69 73 20 6a 6f 75 72 6e 61  s to this journa
c2c0: 6c 20 77 6f 75 6c 64 0a 2a 2a 20 61 6c 73 6f 20  l would.** also 
c2d0: 62 65 20 6e 6f 2d 6f 70 73 2c 20 61 6e 64 20 61  be no-ops, and a
c2e0: 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65  ny attempt to re
c2f0: 61 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77  ad the journal w
c300: 6f 75 6c 64 20 72 65 74 75 72 6e 0a 2a 2a 20 53  ould return.** S
c310: 51 4c 49 54 45 5f 49 4f 45 52 52 2e 20 20 4f 72  QLITE_IOERR.  Or
c320: 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
c330: 69 6f 6e 20 6d 69 67 68 74 20 72 65 63 6f 67 6e  ion might recogn
c340: 69 7a 65 20 74 68 61 74 20 61 20 64 61 74 61 62  ize that a datab
c350: 61 73 65 0a 2a 2a 20 66 69 6c 65 20 77 69 6c 6c  ase.** file will
c360: 20 62 65 20 64 6f 69 6e 67 20 70 61 67 65 2d 61   be doing page-a
c370: 6c 69 67 6e 65 64 20 73 65 63 74 6f 72 20 72 65  ligned sector re
c380: 61 64 73 20 61 6e 64 20 77 72 69 74 65 73 20 69  ads and writes i
c390: 6e 20 61 20 72 61 6e 64 6f 6d 0a 2a 2a 20 6f 72  n a random.** or
c3a0: 64 65 72 20 61 6e 64 20 73 65 74 20 75 70 20 69  der and set up i
c3b0: 74 73 20 49 2f 4f 20 73 75 62 73 79 73 74 65 6d  ts I/O subsystem
c3c0: 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a   accordingly..**
c3d0: 0a 2a 2a 20 53 51 4c 69 74 65 20 6d 69 67 68 74  .** SQLite might
c3e0: 20 61 6c 73 6f 20 61 64 64 20 6f 6e 65 20 6f 66   also add one of
c3f0: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
c400: 6c 61 67 73 20 74 6f 20 74 68 65 20 78 4f 70 65  lags to the xOpe
c410: 6e 20 6d 65 74 68 6f 64 3a 0a 2a 2a 0a 2a 2a 20  n method:.**.** 
c420: 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51  <ul>.** <li> [SQ
c430: 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45  LITE_OPEN_DELETE
c440: 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 3c 6c 69 3e  ONCLOSE].** <li>
c450: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58   [SQLITE_OPEN_EX
c460: 43 4c 55 53 49 56 45 5d 0a 2a 2a 20 3c 2f 75 6c  CLUSIVE].** </ul
c470: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 4c  >.**.** The [SQL
c480: 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f  ITE_OPEN_DELETEO
c490: 4e 43 4c 4f 53 45 5d 20 66 6c 61 67 20 6d 65 61  NCLOSE] flag mea
c4a0: 6e 73 20 74 68 65 20 66 69 6c 65 20 73 68 6f 75  ns the file shou
c4b0: 6c 64 20 62 65 0a 2a 2a 20 64 65 6c 65 74 65 64  ld be.** deleted
c4c0: 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73   when it is clos
c4d0: 65 64 2e 20 20 54 68 65 20 5b 53 51 4c 49 54 45  ed.  The [SQLITE
c4e0: 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
c4f0: 4f 53 45 5d 0a 2a 2a 20 77 69 6c 6c 20 62 65 20  OSE].** will be 
c500: 73 65 74 20 66 6f 72 20 54 45 4d 50 20 20 64 61  set for TEMP  da
c510: 74 61 62 61 73 65 73 2c 20 6a 6f 75 72 6e 61 6c  tabases, journal
c520: 73 20 61 6e 64 20 66 6f 72 20 73 75 62 6a 6f 75  s and for subjou
c530: 72 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  rnals..**.** The
c540: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58   [SQLITE_OPEN_EX
c550: 43 4c 55 53 49 56 45 5d 20 66 6c 61 67 20 69 73  CLUSIVE] flag is
c560: 20 61 6c 77 61 79 73 20 75 73 65 64 20 69 6e 20   always used in 
c570: 63 6f 6e 6a 75 6e 63 74 69 6f 6e 0a 2a 2a 20 77  conjunction.** w
c580: 69 74 68 20 74 68 65 20 5b 53 51 4c 49 54 45 5f  ith the [SQLITE_
c590: 4f 50 45 4e 5f 43 52 45 41 54 45 5d 20 66 6c 61  OPEN_CREATE] fla
c5a0: 67 2c 20 77 68 69 63 68 20 61 72 65 20 62 6f 74  g, which are bot
c5b0: 68 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 61 6e  h directly.** an
c5c0: 61 6c 6f 67 6f 75 73 20 74 6f 20 74 68 65 20 4f  alogous to the O
c5d0: 5f 45 58 43 4c 20 61 6e 64 20 4f 5f 43 52 45 41  _EXCL and O_CREA
c5e0: 54 20 66 6c 61 67 73 20 6f 66 20 74 68 65 20 50  T flags of the P
c5f0: 4f 53 49 58 20 6f 70 65 6e 28 29 0a 2a 2a 20 41  OSIX open().** A
c600: 50 49 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f  PI.  The SQLITE_
c610: 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 66  OPEN_EXCLUSIVE f
c620: 6c 61 67 2c 20 77 68 65 6e 20 70 61 69 72 65 64  lag, when paired
c630: 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 53 51   with the .** SQ
c640: 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
c650: 2c 20 69 73 20 75 73 65 64 20 74 6f 20 69 6e 64  , is used to ind
c660: 69 63 61 74 65 20 74 68 61 74 20 66 69 6c 65 20  icate that file 
c670: 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 0a 2a 2a  should always.**
c680: 20 62 65 20 63 72 65 61 74 65 64 2c 20 61 6e 64   be created, and
c690: 20 74 68 61 74 20 69 74 20 69 73 20 61 6e 20 65   that it is an e
c6a0: 72 72 6f 72 20 69 66 20 69 74 20 61 6c 72 65 61  rror if it alrea
c6b0: 64 79 20 65 78 69 73 74 73 2e 0a 2a 2a 20 49 74  dy exists..** It
c6c0: 20 69 73 20 3c 69 3e 6e 6f 74 3c 2f 69 3e 20 75   is <i>not</i> u
c6d0: 73 65 64 20 74 6f 20 69 6e 64 69 63 61 74 65 20  sed to indicate 
c6e0: 74 68 65 20 66 69 6c 65 20 73 68 6f 75 6c 64 20  the file should 
c6f0: 62 65 20 6f 70 65 6e 65 64 20 0a 2a 2a 20 66 6f  be opened .** fo
c700: 72 20 65 78 63 6c 75 73 69 76 65 20 61 63 63 65  r exclusive acce
c710: 73 73 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 6c 65 61  ss..**.** At lea
c720: 73 74 20 73 7a 4f 73 46 69 6c 65 20 62 79 74 65  st szOsFile byte
c730: 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 72 65 20  s of memory are 
c740: 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 53 51 4c  allocated by SQL
c750: 69 74 65 0a 2a 2a 20 74 6f 20 68 6f 6c 64 20 74  ite.** to hold t
c760: 68 65 20 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c  he  [sqlite3_fil
c770: 65 5d 20 73 74 72 75 63 74 75 72 65 20 70 61 73  e] structure pas
c780: 73 65 64 20 61 73 20 74 68 65 20 74 68 69 72 64  sed as the third
c790: 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  .** argument to 
c7a0: 78 4f 70 65 6e 2e 20 20 54 68 65 20 78 4f 70 65  xOpen.  The xOpe
c7b0: 6e 20 6d 65 74 68 6f 64 20 64 6f 65 73 20 6e 6f  n method does no
c7c0: 74 20 68 61 76 65 20 74 6f 0a 2a 2a 20 61 6c 6c  t have to.** all
c7d0: 6f 63 61 74 65 20 74 68 65 20 73 74 72 75 63 74  ocate the struct
c7e0: 75 72 65 3b 20 69 74 20 73 68 6f 75 6c 64 20 6a  ure; it should j
c7f0: 75 73 74 20 66 69 6c 6c 20 69 74 20 69 6e 2e 20  ust fill it in. 
c800: 20 4e 6f 74 65 20 74 68 61 74 0a 2a 2a 20 74 68   Note that.** th
c810: 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 6d  e xOpen method m
c820: 75 73 74 20 73 65 74 20 74 68 65 20 73 71 6c 69  ust set the sqli
c830: 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68 6f 64  te3_file.pMethod
c840: 73 20 74 6f 20 65 69 74 68 65 72 0a 2a 2a 20 61  s to either.** a
c850: 20 76 61 6c 69 64 20 5b 73 71 6c 69 74 65 33 5f   valid [sqlite3_
c860: 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65  io_methods] obje
c870: 63 74 20 6f 72 20 74 6f 20 4e 55 4c 4c 2e 20 20  ct or to NULL.  
c880: 78 4f 70 65 6e 20 6d 75 73 74 20 64 6f 0a 2a 2a  xOpen must do.**
c890: 20 74 68 69 73 20 65 76 65 6e 20 69 66 20 74 68   this even if th
c8a0: 65 20 6f 70 65 6e 20 66 61 69 6c 73 2e 20 20 53  e open fails.  S
c8b0: 51 4c 69 74 65 20 65 78 70 65 63 74 73 20 74 68  QLite expects th
c8c0: 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66  at the sqlite3_f
c8d0: 69 6c 65 2e 70 4d 65 74 68 6f 64 73 0a 2a 2a 20  ile.pMethods.** 
c8e0: 65 6c 65 6d 65 6e 74 20 77 69 6c 6c 20 62 65 20  element will be 
c8f0: 76 61 6c 69 64 20 61 66 74 65 72 20 78 4f 70 65  valid after xOpe
c900: 6e 20 72 65 74 75 72 6e 73 20 72 65 67 61 72 64  n returns regard
c910: 6c 65 73 73 20 6f 66 20 74 68 65 20 73 75 63 63  less of the succ
c920: 65 73 73 0a 2a 2a 20 6f 72 20 66 61 69 6c 75 72  ess.** or failur
c930: 65 20 6f 66 20 74 68 65 20 78 4f 70 65 6e 20 63  e of the xOpen c
c940: 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  all..**.** The f
c950: 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 6f  lags argument to
c960: 20 78 41 63 63 65 73 73 28 29 20 6d 61 79 20 62   xAccess() may b
c970: 65 20 5b 53 51 4c 49 54 45 5f 41 43 43 45 53 53  e [SQLITE_ACCESS
c980: 5f 45 58 49 53 54 53 5d 0a 2a 2a 20 74 6f 20 74  _EXISTS].** to t
c990: 65 73 74 20 66 6f 72 20 74 68 65 20 65 78 69 73  est for the exis
c9a0: 74 65 6e 63 65 20 6f 66 20 61 20 66 69 6c 65 2c  tence of a file,
c9b0: 20 6f 72 20 5b 53 51 4c 49 54 45 5f 41 43 43 45   or [SQLITE_ACCE
c9c0: 53 53 5f 52 45 41 44 57 52 49 54 45 5d 20 74 6f  SS_READWRITE] to
c9d0: 0a 2a 2a 20 74 65 73 74 20 77 68 65 74 68 65 72  .** test whether
c9e0: 20 61 20 66 69 6c 65 20 69 73 20 72 65 61 64 61   a file is reada
c9f0: 62 6c 65 20 61 6e 64 20 77 72 69 74 61 62 6c 65  ble and writable
ca00: 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 41 43 43  , or [SQLITE_ACC
ca10: 45 53 53 5f 52 45 41 44 5d 0a 2a 2a 20 74 6f 20  ESS_READ].** to 
ca20: 74 65 73 74 20 77 68 65 74 68 65 72 20 61 20 66  test whether a f
ca30: 69 6c 65 20 69 73 20 61 74 20 6c 65 61 73 74 20  ile is at least 
ca40: 72 65 61 64 61 62 6c 65 2e 20 20 20 54 68 65 20  readable.   The 
ca50: 66 69 6c 65 20 63 61 6e 20 62 65 20 61 0a 2a 2a  file can be a.**
ca60: 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 0a 2a   directory..**.*
ca70: 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c  * SQLite will al
ca80: 77 61 79 73 20 61 6c 6c 6f 63 61 74 65 20 61 74  ways allocate at
ca90: 20 6c 65 61 73 74 20 6d 78 50 61 74 68 6e 61 6d   least mxPathnam
caa0: 65 2b 31 20 62 79 74 65 73 20 66 6f 72 20 74 68  e+1 bytes for th
cab0: 65 0a 2a 2a 20 6f 75 74 70 75 74 20 62 75 66 66  e.** output buff
cac0: 65 72 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65  er xFullPathname
cad0: 2e 20 20 54 68 65 20 65 78 61 63 74 20 73 69 7a  .  The exact siz
cae0: 65 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20  e of the output 
caf0: 62 75 66 66 65 72 0a 2a 2a 20 69 73 20 61 6c 73  buffer.** is als
cb00: 6f 20 70 61 73 73 65 64 20 61 73 20 61 20 70 61  o passed as a pa
cb10: 72 61 6d 65 74 65 72 20 74 6f 20 62 6f 74 68 20  rameter to both 
cb20: 20 6d 65 74 68 6f 64 73 2e 20 49 66 20 74 68 65   methods. If the
cb30: 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 0a 2a   output buffer.*
cb40: 2a 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 20 65  * is not large e
cb50: 6e 6f 75 67 68 2c 20 5b 53 51 4c 49 54 45 5f 43  nough, [SQLITE_C
cb60: 41 4e 54 4f 50 45 4e 5d 20 73 68 6f 75 6c 64 20  ANTOPEN] should 
cb70: 62 65 20 72 65 74 75 72 6e 65 64 2e 20 53 69 6e  be returned. Sin
cb80: 63 65 20 74 68 69 73 20 69 73 0a 2a 2a 20 68 61  ce this is.** ha
cb90: 6e 64 6c 65 64 20 61 73 20 61 20 66 61 74 61 6c  ndled as a fatal
cba0: 20 65 72 72 6f 72 20 62 79 20 53 51 4c 69 74 65   error by SQLite
cbb0: 2c 20 76 66 73 20 69 6d 70 6c 65 6d 65 6e 74 61  , vfs implementa
cbc0: 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 65 6e 64  tions should end
cbd0: 65 61 76 6f 72 0a 2a 2a 20 74 6f 20 70 72 65 76  eavor.** to prev
cbe0: 65 6e 74 20 74 68 69 73 20 62 79 20 73 65 74 74  ent this by sett
cbf0: 69 6e 67 20 6d 78 50 61 74 68 6e 61 6d 65 20 74  ing mxPathname t
cc00: 6f 20 61 20 73 75 66 66 69 63 69 65 6e 74 6c 79  o a sufficiently
cc10: 20 6c 61 72 67 65 20 76 61 6c 75 65 2e 0a 2a 2a   large value..**
cc20: 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f 6d 6e  .** The xRandomn
cc30: 65 73 73 28 29 2c 20 78 53 6c 65 65 70 28 29 2c  ess(), xSleep(),
cc40: 20 61 6e 64 20 78 43 75 72 72 65 6e 74 54 69 6d   and xCurrentTim
cc50: 65 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a  e() interfaces.*
cc60: 2a 20 61 72 65 20 6e 6f 74 20 73 74 72 69 63 74  * are not strict
cc70: 6c 79 20 61 20 70 61 72 74 20 6f 66 20 74 68 65  ly a part of the
cc80: 20 66 69 6c 65 73 79 73 74 65 6d 2c 20 62 75 74   filesystem, but
cc90: 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 69 6e 63   they are.** inc
cca0: 6c 75 64 65 64 20 69 6e 20 74 68 65 20 56 46 53  luded in the VFS
ccb0: 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 63   structure for c
ccc0: 6f 6d 70 6c 65 74 65 6e 65 73 73 2e 0a 2a 2a 20  ompleteness..** 
ccd0: 54 68 65 20 78 52 61 6e 64 6f 6d 6e 65 73 73 28  The xRandomness(
cce0: 29 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d  ) function attem
ccf0: 70 74 73 20 74 6f 20 72 65 74 75 72 6e 20 6e 42  pts to return nB
cd00: 79 74 65 73 20 62 79 74 65 73 0a 2a 2a 20 6f 66  ytes bytes.** of
cd10: 20 67 6f 6f 64 2d 71 75 61 6c 69 74 79 20 72 61   good-quality ra
cd20: 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 4f  ndomness into zO
cd30: 75 74 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20  ut.  The return 
cd40: 76 61 6c 75 65 20 69 73 0a 2a 2a 20 74 68 65 20  value is.** the 
cd50: 61 63 74 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66  actual number of
cd60: 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d   bytes of random
cd70: 6e 65 73 73 20 6f 62 74 61 69 6e 65 64 2e 0a 2a  ness obtained..*
cd80: 2a 20 54 68 65 20 78 53 6c 65 65 70 28 29 20 6d  * The xSleep() m
cd90: 65 74 68 6f 64 20 63 61 75 73 65 73 20 74 68 65  ethod causes the
cda0: 20 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20   calling thread 
cdb0: 74 6f 20 73 6c 65 65 70 20 66 6f 72 20 61 74 0a  to sleep for at.
cdc0: 2a 2a 20 6c 65 61 73 74 20 74 68 65 20 6e 75 6d  ** least the num
cdd0: 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f  ber of microseco
cde0: 6e 64 73 20 67 69 76 65 6e 2e 20 20 54 68 65 20  nds given.  The 
cdf0: 78 43 75 72 72 65 6e 74 54 69 6d 65 28 29 0a 2a  xCurrentTime().*
ce00: 2a 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73  * method returns
ce10: 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75   a Julian Day Nu
ce20: 6d 62 65 72 20 66 6f 72 20 74 68 65 20 63 75 72  mber for the cur
ce30: 72 65 6e 74 20 64 61 74 65 20 61 6e 64 20 74 69  rent date and ti
ce40: 6d 65 2e 0a 2a 2a 0a 2a 2f 0a 74 79 70 65 64 65  me..**.*/.typede
ce50: 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
ce60: 5f 76 66 73 20 73 71 6c 69 74 65 33 5f 76 66 73  _vfs sqlite3_vfs
ce70: 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  ;.struct sqlite3
ce80: 5f 76 66 73 20 7b 0a 20 20 69 6e 74 20 69 56 65  _vfs {.  int iVe
ce90: 72 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20  rsion;          
cea0: 20 20 2f 2a 20 53 74 72 75 63 74 75 72 65 20 76    /* Structure v
ceb0: 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 2a 2f  ersion number */
cec0: 0a 20 20 69 6e 74 20 73 7a 4f 73 46 69 6c 65 3b  .  int szOsFile;
ced0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
cee0: 69 7a 65 20 6f 66 20 73 75 62 63 6c 61 73 73 65  ize of subclasse
cef0: 64 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  d sqlite3_file *
cf00: 2f 0a 20 20 69 6e 74 20 6d 78 50 61 74 68 6e 61  /.  int mxPathna
cf10: 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  me;          /* 
cf20: 4d 61 78 69 6d 75 6d 20 66 69 6c 65 20 70 61 74  Maximum file pat
cf30: 68 6e 61 6d 65 20 6c 65 6e 67 74 68 20 2a 2f 0a  hname length */.
cf40: 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
cf50: 4e 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65  Next;      /* Ne
cf60: 78 74 20 72 65 67 69 73 74 65 72 65 64 20 56 46  xt registered VF
cf70: 53 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  S */.  const cha
cf80: 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20  r *zName;       
cf90: 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20  /* Name of this 
cfa0: 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73  virtual file sys
cfb0: 74 65 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  tem */.  void *p
cfc0: 41 70 70 44 61 74 61 3b 20 20 20 20 20 20 20 20  AppData;        
cfd0: 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
cfe0: 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 70 65 63  application-spec
cff0: 69 66 69 63 20 64 61 74 61 20 2a 2f 0a 20 20 69  ific data */.  i
d000: 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c 69  nt (*xOpen)(sqli
d010: 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20  te3_vfs*, const 
d020: 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 73 71 6c  char *zName, sql
d030: 69 74 65 33 5f 66 69 6c 65 2a 2c 0a 20 20 20 20  ite3_file*,.    
d040: 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 66             int f
d050: 6c 61 67 73 2c 20 69 6e 74 20 2a 70 4f 75 74 46  lags, int *pOutF
d060: 6c 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78  lags);.  int (*x
d070: 44 65 6c 65 74 65 29 28 73 71 6c 69 74 65 33 5f  Delete)(sqlite3_
d080: 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  vfs*, const char
d090: 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 73 79 6e   *zName, int syn
d0a0: 63 44 69 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78  cDir);.  int (*x
d0b0: 41 63 63 65 73 73 29 28 73 71 6c 69 74 65 33 5f  Access)(sqlite3_
d0c0: 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  vfs*, const char
d0d0: 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 66 6c 61   *zName, int fla
d0e0: 67 73 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  gs, int *pResOut
d0f0: 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 75 6c 6c  );.  int (*xFull
d100: 50 61 74 68 6e 61 6d 65 29 28 73 71 6c 69 74 65  Pathname)(sqlite
d110: 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68  3_vfs*, const ch
d120: 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e  ar *zName, int n
d130: 4f 75 74 2c 20 63 68 61 72 20 2a 7a 4f 75 74 29  Out, char *zOut)
d140: 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 44 6c 4f  ;.  void *(*xDlO
d150: 70 65 6e 29 28 73 71 6c 69 74 65 33 5f 76 66 73  pen)(sqlite3_vfs
d160: 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  *, const char *z
d170: 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 76 6f 69  Filename);.  voi
d180: 64 20 28 2a 78 44 6c 45 72 72 6f 72 29 28 73 71  d (*xDlError)(sq
d190: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20  lite3_vfs*, int 
d1a0: 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 45 72  nByte, char *zEr
d1b0: 72 4d 73 67 29 3b 0a 20 20 76 6f 69 64 20 28 2a  rMsg);.  void (*
d1c0: 28 2a 78 44 6c 53 79 6d 29 28 73 71 6c 69 74 65  (*xDlSym)(sqlite
d1d0: 33 5f 76 66 73 2a 2c 76 6f 69 64 2a 2c 20 63 6f  3_vfs*,void*, co
d1e0: 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f  nst char *zSymbo
d1f0: 6c 29 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69  l))(void);.  voi
d200: 64 20 28 2a 78 44 6c 43 6c 6f 73 65 29 28 73 71  d (*xDlClose)(sq
d210: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 76 6f 69 64  lite3_vfs*, void
d220: 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 61 6e  *);.  int (*xRan
d230: 64 6f 6d 6e 65 73 73 29 28 73 71 6c 69 74 65 33  domness)(sqlite3
d240: 5f 76 66 73 2a 2c 20 69 6e 74 20 6e 42 79 74 65  _vfs*, int nByte
d250: 2c 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b 0a 20  , char *zOut);. 
d260: 20 69 6e 74 20 28 2a 78 53 6c 65 65 70 29 28 73   int (*xSleep)(s
d270: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74  qlite3_vfs*, int
d280: 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 3b 0a   microseconds);.
d290: 20 20 69 6e 74 20 28 2a 78 43 75 72 72 65 6e 74    int (*xCurrent
d2a0: 54 69 6d 65 29 28 73 71 6c 69 74 65 33 5f 76 66  Time)(sqlite3_vf
d2b0: 73 2a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 20 20  s*, double*);.  
d2c0: 69 6e 74 20 28 2a 78 47 65 74 4c 61 73 74 45 72  int (*xGetLastEr
d2d0: 72 6f 72 29 28 73 71 6c 69 74 65 33 5f 76 66 73  ror)(sqlite3_vfs
d2e0: 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b  *, int, char *);
d2f0: 0a 20 20 2f 2a 20 4e 65 77 20 66 69 65 6c 64 73  .  /* New fields
d300: 20 6d 61 79 20 62 65 20 61 70 70 65 6e 64 65 64   may be appended
d310: 20 69 6e 20 66 69 67 75 72 65 20 76 65 72 73 69   in figure versi
d320: 6f 6e 73 2e 20 20 54 68 65 20 69 56 65 72 73 69  ons.  The iVersi
d330: 6f 6e 0a 20 20 2a 2a 20 76 61 6c 75 65 20 77 69  on.  ** value wi
d340: 6c 6c 20 69 6e 63 72 65 6d 65 6e 74 20 77 68 65  ll increment whe
d350: 6e 65 76 65 72 20 74 68 69 73 20 68 61 70 70 65  never this happe
d360: 6e 73 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ns. */.};../*.**
d370: 20 43 41 50 49 33 52 45 46 3a 20 46 6c 61 67 73   CAPI3REF: Flags
d380: 20 66 6f 72 20 74 68 65 20 78 41 63 63 65 73 73   for the xAccess
d390: 20 56 46 53 20 6d 65 74 68 6f 64 20 7b 48 31 31   VFS method {H11
d3a0: 31 39 30 7d 20 3c 48 31 31 31 34 30 3e 0a 2a 2a  190} <H11140>.**
d3b0: 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65  .** These intege
d3c0: 72 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 6e 20  r constants can 
d3d0: 62 65 20 75 73 65 64 20 61 73 20 74 68 65 20 74  be used as the t
d3e0: 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74  hird parameter t
d3f0: 6f 0a 2a 2a 20 74 68 65 20 78 41 63 63 65 73 73  o.** the xAccess
d400: 20 6d 65 74 68 6f 64 20 6f 66 20 61 6e 20 5b 73   method of an [s
d410: 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65  qlite3_vfs] obje
d420: 63 74 2e 20 7b 45 4e 44 7d 20 20 54 68 65 79 20  ct. {END}  They 
d430: 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 61  determine.** wha
d440: 74 20 6b 69 6e 64 20 6f 66 20 70 65 72 6d 69 73  t kind of permis
d450: 73 69 6f 6e 73 20 74 68 65 20 78 41 63 63 65 73  sions the xAcces
d460: 73 20 6d 65 74 68 6f 64 20 69 73 20 6c 6f 6f 6b  s method is look
d470: 69 6e 67 20 66 6f 72 2e 0a 2a 2a 20 57 69 74 68  ing for..** With
d480: 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45   SQLITE_ACCESS_E
d490: 58 49 53 54 53 2c 20 74 68 65 20 78 41 63 63 65  XISTS, the xAcce
d4a0: 73 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 73 69 6d  ss method.** sim
d4b0: 70 6c 79 20 63 68 65 63 6b 73 20 77 68 65 74 68  ply checks wheth
d4c0: 65 72 20 74 68 65 20 66 69 6c 65 20 65 78 69 73  er the file exis
d4d0: 74 73 2e 0a 2a 2a 20 57 69 74 68 20 53 51 4c 49  ts..** With SQLI
d4e0: 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52  TE_ACCESS_READWR
d4f0: 49 54 45 2c 20 74 68 65 20 78 41 63 63 65 73 73  ITE, the xAccess
d500: 20 6d 65 74 68 6f 64 0a 2a 2a 20 63 68 65 63 6b   method.** check
d510: 73 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69  s whether the fi
d520: 6c 65 20 69 73 20 62 6f 74 68 20 72 65 61 64 61  le is both reada
d530: 62 6c 65 20 61 6e 64 20 77 72 69 74 61 62 6c 65  ble and writable
d540: 2e 0a 2a 2a 20 57 69 74 68 20 53 51 4c 49 54 45  ..** With SQLITE
d550: 5f 41 43 43 45 53 53 5f 52 45 41 44 2c 20 74 68  _ACCESS_READ, th
d560: 65 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64  e xAccess method
d570: 0a 2a 2a 20 63 68 65 63 6b 73 20 77 68 65 74 68  .** checks wheth
d580: 65 72 20 74 68 65 20 66 69 6c 65 20 69 73 20 72  er the file is r
d590: 65 61 64 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66  eadable..*/.#def
d5a0: 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53  ine SQLITE_ACCES
d5b0: 53 5f 45 58 49 53 54 53 20 20 20 20 30 0a 23 64  S_EXISTS    0.#d
d5c0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 43 43  efine SQLITE_ACC
d5d0: 45 53 53 5f 52 45 41 44 57 52 49 54 45 20 31 0a  ESS_READWRITE 1.
d5e0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
d5f0: 43 43 45 53 53 5f 52 45 41 44 20 20 20 20 20 20  CCESS_READ      
d600: 32 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  2../*.** CAPI3RE
d610: 46 3a 20 49 6e 69 74 69 61 6c 69 7a 65 20 54 68  F: Initialize Th
d620: 65 20 53 51 4c 69 74 65 20 4c 69 62 72 61 72 79  e SQLite Library
d630: 20 7b 48 31 30 31 33 30 7d 20 3c 53 32 30 30 30   {H10130} <S2000
d640: 30 3e 3c 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a 2a  0><S30100>.**.**
d650: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69   The sqlite3_ini
d660: 74 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e  tialize() routin
d670: 65 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 74 68  e initializes th
d680: 65 0a 2a 2a 20 53 51 4c 69 74 65 20 6c 69 62 72  e.** SQLite libr
d690: 61 72 79 2e 20 20 54 68 65 20 73 71 6c 69 74 65  ary.  The sqlite
d6a0: 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 72 6f 75  3_shutdown() rou
d6b0: 74 69 6e 65 0a 2a 2a 20 64 65 61 6c 6c 6f 63 61  tine.** dealloca
d6c0: 74 65 73 20 61 6e 79 20 72 65 73 6f 75 72 63 65  tes any resource
d6d0: 73 20 74 68 61 74 20 77 65 72 65 20 61 6c 6c 6f  s that were allo
d6e0: 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33  cated by sqlite3
d6f0: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 0a 2a  _initialize()..*
d700: 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73  *.** A call to 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 61 6e 20 22 65 66 66 65 63  e() is an "effec
d730: 74 69 76 65 22 20 63 61 6c 6c 20 69 66 20 69 74  tive" call if it
d740: 20 69 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74   is.** the first
d750: 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e   time sqlite3_in
d760: 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e  itialize() is in
d770: 76 6f 6b 65 64 20 64 75 72 69 6e 67 20 74 68 65  voked during the
d780: 20 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20   lifetime of.** 
d790: 74 68 65 20 70 72 6f 63 65 73 73 2c 20 6f 72 20  the process, or 
d7a0: 69 66 20 69 74 20 69 73 20 74 68 65 20 66 69 72  if it is the fir
d7b0: 73 74 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f  st time sqlite3_
d7c0: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20  initialize() is 
d7d0: 69 6e 76 6f 6b 65 64 0a 2a 2a 20 66 6f 6c 6c 6f  invoked.** follo
d7e0: 77 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73  wing a call to s
d7f0: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
d800: 29 2e 20 20 4f 6e 6c 79 20 61 6e 20 65 66 66 65  ).  Only an effe
d810: 63 74 69 76 65 20 63 61 6c 6c 0a 2a 2a 20 6f 66  ctive call.** of
d820: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
d830: 69 7a 65 28 29 20 64 6f 65 73 20 61 6e 79 20 69  ize() does any i
d840: 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 20 20  nitialization.  
d850: 41 6c 6c 20 6f 74 68 65 72 20 63 61 6c 6c 73 0a  All other calls.
d860: 2a 2a 20 61 72 65 20 68 61 72 6d 6c 65 73 73 20  ** are harmless 
d870: 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41 20  no-ops..**.** A 
d880: 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
d890: 73 68 75 74 64 6f 77 6e 28 29 20 69 73 20 61 6e  shutdown() is an
d8a0: 20 22 65 66 66 65 63 74 69 76 65 22 20 63 61 6c   "effective" cal
d8b0: 6c 20 69 66 20 69 74 20 69 73 20 74 68 65 20 66  l if it is the f
d8c0: 69 72 73 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20  irst.** call to 
d8d0: 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
d8e0: 28 29 20 73 69 6e 63 65 20 74 68 65 20 6c 61 73  () since the las
d8f0: 74 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  t sqlite3_initia
d900: 6c 69 7a 65 28 29 2e 20 20 4f 6e 6c 79 0a 2a 2a  lize().  Only.**
d910: 20 61 6e 20 65 66 66 65 63 74 69 76 65 20 63 61   an effective ca
d920: 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68  ll to sqlite3_sh
d930: 75 74 64 6f 77 6e 28 29 20 64 6f 65 73 20 61 6e  utdown() does an
d940: 79 20 64 65 69 6e 69 74 69 61 6c 69 7a 61 74 69  y deinitializati
d950: 6f 6e 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72  on..** All other
d960: 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65   calls to sqlite
d970: 33 5f 73 68 75 74 64 6f 77 6e 28 29 20 61 72 65  3_shutdown() are
d980: 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 73   harmless no-ops
d990: 2e 0a 2a 2a 0a 2a 2a 20 41 6d 6f 6e 67 20 6f 74  ..**.** Among ot
d9a0: 68 65 72 20 74 68 69 6e 67 73 2c 20 73 71 6c 69  her things, sqli
d9b0: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
d9c0: 20 73 68 61 6c 6c 20 69 6e 76 6f 6b 65 0a 2a 2a   shall invoke.**
d9d0: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74   sqlite3_os_init
d9e0: 28 29 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20  ().  Similarly, 
d9f0: 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
da00: 28 29 0a 2a 2a 20 73 68 61 6c 6c 20 69 6e 76 6f  ().** shall invo
da10: 6b 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e  ke sqlite3_os_en
da20: 64 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  d()..**.** The s
da30: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
da40: 65 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75  e() routine retu
da50: 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20  rns [SQLITE_OK] 
da60: 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 20 49  on success..** I
da70: 66 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f  f for some reaso
da80: 6e 2c 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  n, sqlite3_initi
da90: 61 6c 69 7a 65 28 29 20 69 73 20 75 6e 61 62 6c  alize() is unabl
daa0: 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 0a  e to initialize.
dab0: 2a 2a 20 74 68 65 20 6c 69 62 72 61 72 79 20 28  ** the library (
dac0: 70 65 72 68 61 70 73 20 69 74 20 69 73 20 75 6e  perhaps it is un
dad0: 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65  able to allocate
dae0: 20 61 20 6e 65 65 64 65 64 20 72 65 73 6f 75 72   a needed resour
daf0: 63 65 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 20  ce such.** as a 
db00: 6d 75 74 65 78 29 20 69 74 20 72 65 74 75 72 6e  mutex) it return
db10: 73 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65  s an [error code
db20: 5d 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53 51  ] other than [SQ
db30: 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20  LITE_OK]..**.** 
db40: 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  The sqlite3_init
db50: 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e 65  ialize() routine
db60: 20 69 73 20 63 61 6c 6c 65 64 20 69 6e 74 65 72   is called inter
db70: 6e 61 6c 6c 79 20 62 79 20 6d 61 6e 79 20 6f 74  nally by many ot
db80: 68 65 72 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e  her.** SQLite in
db90: 74 65 72 66 61 63 65 73 20 73 6f 20 74 68 61 74  terfaces so that
dba0: 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20   an application 
dbb0: 75 73 75 61 6c 6c 79 20 64 6f 65 73 20 6e 6f 74  usually does not
dbc0: 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 69 6e 76 6f   need to.** invo
dbd0: 6b 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  ke sqlite3_initi
dbe0: 61 6c 69 7a 65 28 29 20 64 69 72 65 63 74 6c 79  alize() directly
dbf0: 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  .  For example, 
dc00: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d  [sqlite3_open()]
dc10: 0a 2a 2a 20 63 61 6c 6c 73 20 73 71 6c 69 74 65  .** calls sqlite
dc20: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 73  3_initialize() s
dc30: 6f 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62  o the SQLite lib
dc40: 72 61 72 79 20 77 69 6c 6c 20 62 65 20 61 75 74  rary will be aut
dc50: 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 69 6e  omatically.** in
dc60: 69 74 69 61 6c 69 7a 65 64 20 77 68 65 6e 20 5b  itialized when [
dc70: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 20  sqlite3_open()] 
dc80: 69 73 20 63 61 6c 6c 65 64 20 69 66 20 69 74 20  is called if it 
dc90: 68 61 73 20 6e 6f 74 20 62 65 20 69 6e 69 74 69  has not be initi
dca0: 61 6c 69 7a 65 64 0a 2a 2a 20 61 6c 72 65 61 64  alized.** alread
dcb0: 79 2e 20 20 48 6f 77 65 76 65 72 2c 20 69 66 20  y.  However, if 
dcc0: 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c  SQLite is compil
dcd0: 65 64 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c  ed with the [SQL
dce0: 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49  ITE_OMIT_AUTOINI
dcf0: 54 5d 0a 2a 2a 20 63 6f 6d 70 69 6c 65 2d 74 69  T].** compile-ti
dd00: 6d 65 20 6f 70 74 69 6f 6e 2c 20 74 68 65 6e 20  me option, then 
dd10: 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20 63 61  the automatic ca
dd20: 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 69  lls to sqlite3_i
dd30: 6e 69 74 69 61 6c 69 7a 65 28 29 0a 2a 2a 20 61  nitialize().** a
dd40: 72 65 20 6f 6d 69 74 74 65 64 20 61 6e 64 20 74  re omitted and t
dd50: 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d  he application m
dd60: 75 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  ust call sqlite3
dd70: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64 69  _initialize() di
dd80: 72 65 63 74 6c 79 0a 2a 2a 20 70 72 69 6f 72 20  rectly.** prior 
dd90: 74 6f 20 75 73 69 6e 67 20 61 6e 79 20 6f 74 68  to using any oth
dda0: 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66  er SQLite interf
ddb0: 61 63 65 2e 20 20 46 6f 72 20 6d 61 78 69 6d 75  ace.  For maximu
ddc0: 6d 20 70 6f 72 74 61 62 69 6c 69 74 79 2c 0a 2a  m portability,.*
ddd0: 2a 20 69 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e  * it is recommen
dde0: 64 65 64 20 74 68 61 74 20 61 70 70 6c 69 63 61  ded that applica
ddf0: 74 69 6f 6e 73 20 61 6c 77 61 79 73 20 69 6e 76  tions always inv
de00: 6f 6b 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  oke sqlite3_init
de10: 69 61 6c 69 7a 65 28 29 0a 2a 2a 20 64 69 72 65  ialize().** dire
de20: 63 74 6c 79 20 70 72 69 6f 72 20 74 6f 20 75 73  ctly prior to us
de30: 69 6e 67 20 61 6e 79 20 6f 74 68 65 72 20 53 51  ing any other SQ
de40: 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 2e 20  Lite interface. 
de50: 20 46 75 74 75 72 65 20 72 65 6c 65 61 73 65 73   Future releases
de60: 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20 6d 61  .** of SQLite ma
de70: 79 20 72 65 71 75 69 72 65 20 74 68 69 73 2e 20  y require this. 
de80: 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
de90: 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 65 78   the behavior ex
dea0: 68 69 62 69 74 65 64 0a 2a 2a 20 77 68 65 6e 20  hibited.** when 
deb0: 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c  SQLite is compil
dec0: 65 64 20 77 69 74 68 20 5b 53 51 4c 49 54 45 5f  ed with [SQLITE_
ded0: 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 5d 20 6d  OMIT_AUTOINIT] m
dee0: 69 67 68 74 20 62 65 63 6f 6d 65 20 74 68 65 0a  ight become the.
def0: 2a 2a 20 64 65 66 61 75 6c 74 20 62 65 68 61 76  ** default behav
df00: 69 6f 72 20 69 6e 20 73 6f 6d 65 20 66 75 74 75  ior in some futu
df10: 72 65 20 72 65 6c 65 61 73 65 20 6f 66 20 53 51  re release of SQ
df20: 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  Lite..**.** The 
df30: 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28  sqlite3_os_init(
df40: 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6f  ) routine does o
df50: 70 65 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20  perating-system 
df60: 73 70 65 63 69 66 69 63 0a 2a 2a 20 69 6e 69 74  specific.** init
df70: 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 74 68  ialization of th
df80: 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  e SQLite library
df90: 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f  .  The sqlite3_o
dfa0: 73 5f 65 6e 64 28 29 0a 2a 2a 20 72 6f 75 74 69  s_end().** routi
dfb0: 6e 65 20 75 6e 64 6f 65 73 20 74 68 65 20 65 66  ne undoes the ef
dfc0: 66 65 63 74 20 6f 66 20 73 71 6c 69 74 65 33 5f  fect of sqlite3_
dfd0: 6f 73 5f 69 6e 69 74 28 29 2e 20 20 54 79 70 69  os_init().  Typi
dfe0: 63 61 6c 20 74 61 73 6b 73 0a 2a 2a 20 70 65 72  cal tasks.** per
dff0: 66 6f 72 6d 65 64 20 62 79 20 74 68 65 73 65 20  formed by these 
e000: 72 6f 75 74 69 6e 65 73 20 69 6e 63 6c 75 64 65  routines include
e010: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 72 20 64   allocation or d
e020: 65 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 6f  eallocation.** o
e030: 66 20 73 74 61 74 69 63 20 72 65 73 6f 75 72 63  f static resourc
e040: 65 73 2c 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  es, initializati
e050: 6f 6e 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72  on of global var
e060: 69 61 62 6c 65 73 2c 0a 2a 2a 20 73 65 74 74 69  iables,.** setti
e070: 6e 67 20 75 70 20 61 20 64 65 66 61 75 6c 74 20  ng up a default 
e080: 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6d 6f  [sqlite3_vfs] mo
e090: 64 75 6c 65 2c 20 6f 72 20 73 65 74 74 69 6e 67  dule, or setting
e0a0: 20 75 70 0a 2a 2a 20 61 20 64 65 66 61 75 6c 74   up.** a default
e0b0: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 75   configuration u
e0c0: 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 6f  sing [sqlite3_co
e0d0: 6e 66 69 67 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54  nfig()]..**.** T
e0e0: 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73  he application s
e0f0: 68 6f 75 6c 64 20 6e 65 76 65 72 20 69 6e 76 6f  hould never invo
e100: 6b 65 20 65 69 74 68 65 72 20 73 71 6c 69 74 65  ke either sqlite
e110: 33 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a 2a 20 6f  3_os_init().** o
e120: 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64  r sqlite3_os_end
e130: 28 29 20 64 69 72 65 63 74 6c 79 2e 20 20 54 68  () directly.  Th
e140: 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68  e application sh
e150: 6f 75 6c 64 20 6f 6e 6c 79 20 69 6e 76 6f 6b 65  ould only invoke
e160: 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  .** sqlite3_init
e170: 69 61 6c 69 7a 65 28 29 20 61 6e 64 20 73 71 6c  ialize() and sql
e180: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e  ite3_shutdown().
e190: 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 73    The sqlite3_os
e1a0: 5f 69 6e 69 74 28 29 0a 2a 2a 20 69 6e 74 65 72  _init().** inter
e1b0: 66 61 63 65 20 69 73 20 63 61 6c 6c 65 64 20 61  face is called a
e1c0: 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 20  utomatically by 
e1d0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
e1e0: 7a 65 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69  ze() and.** sqli
e1f0: 74 65 33 5f 6f 73 5f 65 6e 64 28 29 20 69 73 20  te3_os_end() is 
e200: 63 61 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 65  called by sqlite
e210: 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e 20 20 41  3_shutdown().  A
e220: 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 69 6d  ppropriate.** im
e230: 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f  plementations fo
e240: 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69  r sqlite3_os_ini
e250: 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  t() and sqlite3_
e260: 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 61 72 65 20  os_end().** are 
e270: 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74  built into SQLit
e280: 65 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f 6d  e when it is com
e290: 70 69 6c 65 64 20 66 6f 72 20 55 6e 69 78 2c 20  piled for Unix, 
e2a0: 57 69 6e 64 6f 77 73 2c 20 6f 72 20 4f 53 2f 32  Windows, or OS/2
e2b0: 2e 0a 2a 2a 20 57 68 65 6e 20 5b 63 75 73 74 6f  ..** When [custo
e2c0: 6d 20 62 75 69 6c 64 73 20 7c 20 62 75 69 6c 74  m builds | built
e2d0: 20 66 6f 72 20 6f 74 68 65 72 20 70 6c 61 74 66   for other platf
e2e0: 6f 72 6d 73 5d 0a 2a 2a 20 28 75 73 69 6e 67 20  orms].** (using 
e2f0: 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 53 5f 4f  the [SQLITE_OS_O
e300: 54 48 45 52 3d 31 5d 20 63 6f 6d 70 69 6c 65 2d  THER=1] compile-
e310: 74 69 6d 65 0a 2a 2a 20 6f 70 74 69 6f 6e 29 20  time.** option) 
e320: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
e330: 6d 75 73 74 20 73 75 70 70 6c 79 20 61 20 73 75  must supply a su
e340: 69 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  itable implement
e350: 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 73 71 6c  ation for.** sql
e360: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61  ite3_os_init() a
e370: 6e 64 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e  nd sqlite3_os_en
e380: 64 28 29 2e 20 20 41 6e 20 61 70 70 6c 69 63 61  d().  An applica
e390: 74 69 6f 6e 2d 73 75 70 70 6c 69 65 64 0a 2a 2a  tion-supplied.**
e3a0: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
e3b0: 6f 66 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  of sqlite3_os_in
e3c0: 69 74 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f  it() or sqlite3_
e3d0: 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 6d 75 73 74  os_end().** must
e3e0: 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f   return [SQLITE_
e3f0: 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 20 61  OK] on success a
e400: 6e 64 20 73 6f 6d 65 20 6f 74 68 65 72 20 5b 65  nd some other [e
e410: 72 72 6f 72 20 63 6f 64 65 5d 20 75 70 6f 6e 0a  rror code] upon.
e420: 2a 2a 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 53  ** failure..*/.S
e430: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
e440: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
e450: 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41  (void);.SQLITE_A
e460: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  PI int sqlite3_s
e470: 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 3b 0a 53  hutdown(void);.S
e480: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
e490: 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f  lite3_os_init(vo
e4a0: 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  id);.SQLITE_API 
e4b0: 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65  int sqlite3_os_e
e4c0: 6e 64 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a  nd(void);../*.**
e4d0: 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69   CAPI3REF: Confi
e4e0: 67 75 72 69 6e 67 20 54 68 65 20 53 51 4c 69 74  guring The SQLit
e4f0: 65 20 4c 69 62 72 61 72 79 20 7b 48 31 34 31 30  e Library {H1410
e500: 30 7d 20 3c 53 32 30 30 30 30 3e 3c 53 33 30 32  0} <S20000><S302
e510: 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  00>.** EXPERIMEN
e520: 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  TAL.**.** The sq
e530: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 69  lite3_config() i
e540: 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64  nterface is used
e550: 20 74 6f 20 6d 61 6b 65 20 67 6c 6f 62 61 6c 20   to make global 
e560: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a 2a 2a  configuration.**
e570: 20 63 68 61 6e 67 65 73 20 74 6f 20 53 51 4c 69   changes to SQLi
e580: 74 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 74  te in order to t
e590: 75 6e 65 20 53 51 4c 69 74 65 20 74 6f 20 74 68  une SQLite to th
e5a0: 65 20 73 70 65 63 69 66 69 63 20 6e 65 65 64 73  e specific needs
e5b0: 20 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69   of.** the appli
e5c0: 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 64 65 66  cation.  The def
e5d0: 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69  ault configurati
e5e0: 6f 6e 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65  on is recommende
e5f0: 64 20 66 6f 72 20 6d 6f 73 74 0a 2a 2a 20 61 70  d for most.** ap
e600: 70 6c 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 73  plications and s
e610: 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  o this routine i
e620: 73 20 75 73 75 61 6c 6c 79 20 6e 6f 74 20 6e 65  s usually not ne
e630: 63 65 73 73 61 72 79 2e 20 20 49 74 20 69 73 0a  cessary.  It is.
e640: 2a 2a 20 70 72 6f 76 69 64 65 64 20 74 6f 20 73  ** provided to s
e650: 75 70 70 6f 72 74 20 72 61 72 65 20 61 70 70 6c  upport rare appl
e660: 69 63 61 74 69 6f 6e 73 20 77 69 74 68 20 75 6e  ications with un
e670: 75 73 75 61 6c 20 6e 65 65 64 73 2e 0a 2a 2a 0a  usual needs..**.
e680: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63  ** The sqlite3_c
e690: 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63  onfig() interfac
e6a0: 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73  e is not threads
e6b0: 61 66 65 2e 20 20 54 68 65 20 61 70 70 6c 69 63  afe.  The applic
e6c0: 61 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e  ation.** must in
e6d0: 73 75 72 65 20 74 68 61 74 20 6e 6f 20 6f 74 68  sure that no oth
e6e0: 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66  er SQLite interf
e6f0: 61 63 65 73 20 61 72 65 20 69 6e 76 6f 6b 65 64  aces are invoked
e700: 20 62 79 20 6f 74 68 65 72 0a 2a 2a 20 74 68 72   by other.** thr
e710: 65 61 64 73 20 77 68 69 6c 65 20 73 71 6c 69 74  eads while sqlit
e720: 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 72  e3_config() is r
e730: 75 6e 6e 69 6e 67 2e 20 20 46 75 72 74 68 65 72  unning.  Further
e740: 6d 6f 72 65 2c 20 73 71 6c 69 74 65 33 5f 63 6f  more, sqlite3_co
e750: 6e 66 69 67 28 29 0a 2a 2a 20 6d 61 79 20 6f 6e  nfig().** may on
e760: 6c 79 20 62 65 20 69 6e 76 6f 6b 65 64 20 70 72  ly be invoked pr
e770: 69 6f 72 20 74 6f 20 6c 69 62 72 61 72 79 20 69  ior to library i
e780: 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 75 73  nitialization us
e790: 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  ing.** [sqlite3_
e7a0: 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20 6f 72  initialize()] or
e7b0: 20 61 66 74 65 72 20 73 68 75 74 64 6f 77 6e 20   after shutdown 
e7c0: 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74  by [sqlite3_shut
e7d0: 64 6f 77 6e 28 29 5d 2e 0a 2a 2a 20 4e 6f 74 65  down()]..** Note
e7e0: 2c 20 68 6f 77 65 76 65 72 2c 20 74 68 61 74 20  , however, that 
e7f0: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
e800: 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61   can be called a
e810: 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a  s part of the.**
e820: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
e830: 6f 66 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f  of an applicatio
e840: 6e 2d 64 65 66 69 6e 65 64 20 5b 73 71 6c 69 74  n-defined [sqlit
e850: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 5d 2e 0a 2a  e3_os_init()]..*
e860: 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  *.** The first a
e870: 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74  rgument to sqlit
e880: 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 73 20 61  e3_config() is a
e890: 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 5b 53 51  n integer.** [SQ
e8a0: 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47  LITE_CONFIG_SING
e8b0: 4c 45 54 48 52 45 41 44 20 7c 20 63 6f 6e 66 69  LETHREAD | confi
e8c0: 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d  guration option]
e8d0: 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73   that determines
e8e0: 0a 2a 2a 20 77 68 61 74 20 70 72 6f 70 65 72 74  .** what propert
e8f0: 79 20 6f 66 20 53 51 4c 69 74 65 20 69 73 20 74  y of SQLite is t
e900: 6f 20 62 65 20 63 6f 6e 66 69 67 75 72 65 64 2e  o be configured.
e910: 20 20 53 75 62 73 65 71 75 65 6e 74 20 61 72 67    Subsequent arg
e920: 75 6d 65 6e 74 73 0a 2a 2a 20 76 61 72 79 20 64  uments.** vary d
e930: 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20  epending on the 
e940: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53  [SQLITE_CONFIG_S
e950: 49 4e 47 4c 45 54 48 52 45 41 44 20 7c 20 63 6f  INGLETHREAD | co
e960: 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69  nfiguration opti
e970: 6f 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69  on].** in the fi
e980: 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a  rst argument..**
e990: 0a 2a 2a 20 57 68 65 6e 20 61 20 63 6f 6e 66 69  .** When a confi
e9a0: 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20  guration option 
e9b0: 69 73 20 73 65 74 2c 20 73 71 6c 69 74 65 33 5f  is set, sqlite3_
e9c0: 63 6f 6e 66 69 67 28 29 20 72 65 74 75 72 6e 73  config() returns
e9d0: 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a   [SQLITE_OK]..**
e9e0: 20 49 66 20 74 68 65 20 6f 70 74 69 6f 6e 20 69   If the option i
e9f0: 73 20 75 6e 6b 6e 6f 77 6e 20 6f 72 20 53 51 4c  s unknown or SQL
ea00: 69 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f  ite is unable to
ea10: 20 73 65 74 20 74 68 65 20 6f 70 74 69 6f 6e 0a   set the option.
ea20: 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  ** then this rou
ea30: 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 6e  tine returns a n
ea40: 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63  on-zero [error c
ea50: 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  ode]..**.** Requ
ea60: 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
ea70: 34 31 30 33 5d 20 5b 48 31 34 31 30 36 5d 20 5b  4103] [H14106] [
ea80: 48 31 34 31 32 30 5d 20 5b 48 31 34 31 32 33 5d  H14120] [H14123]
ea90: 20 5b 48 31 34 31 32 36 5d 20 5b 48 31 34 31 32   [H14126] [H1412
eaa0: 39 5d 20 5b 48 31 34 31 33 32 5d 20 5b 48 31 34  9] [H14132] [H14
eab0: 31 33 35 5d 0a 2a 2a 20 5b 48 31 34 31 33 38 5d  135].** [H14138]
eac0: 20 5b 48 31 34 31 34 31 5d 20 5b 48 31 34 31 34   [H14141] [H1414
ead0: 34 5d 20 5b 48 31 34 31 34 37 5d 20 5b 48 31 34  4] [H14147] [H14
eae0: 31 35 30 5d 20 5b 48 31 34 31 35 33 5d 20 5b 48  150] [H14153] [H
eaf0: 31 34 31 35 36 5d 20 5b 48 31 34 31 35 39 5d 0a  14156] [H14159].
eb00: 2a 2a 20 5b 48 31 34 31 36 32 5d 20 5b 48 31 34  ** [H14162] [H14
eb10: 31 36 35 5d 20 5b 48 31 34 31 36 38 5d 0a 2a 2f  165] [H14168].*/
eb20: 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
eb30: 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20  TE_EXPERIMENTAL 
eb40: 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  int sqlite3_conf
eb50: 69 67 28 69 6e 74 2c 20 2e 2e 2e 29 3b 0a 0a 2f  ig(int, ...);../
eb60: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43  *.** CAPI3REF: C
eb70: 6f 6e 66 69 67 75 72 65 20 64 61 74 61 62 61 73  onfigure databas
eb80: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 20 7b  e connections  {
eb90: 48 31 34 32 30 30 7d 20 3c 53 32 30 30 30 30 3e  H14200} <S20000>
eba0: 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
ebb0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
ebc0: 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 20 69  e3_db_config() i
ebd0: 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64  nterface is used
ebe0: 20 74 6f 20 6d 61 6b 65 20 63 6f 6e 66 69 67 75   to make configu
ebf0: 72 61 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65  ration.** change
ec00: 73 20 74 6f 20 61 20 5b 64 61 74 61 62 61 73 65  s to a [database
ec10: 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 54   connection].  T
ec20: 68 65 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  he interface is 
ec30: 73 69 6d 69 6c 61 72 20 74 6f 0a 2a 2a 20 5b 73  similar to.** [s
ec40: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d  qlite3_config()]
ec50: 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65   except that the
ec60: 20 63 68 61 6e 67 65 73 20 61 70 70 6c 79 20 74   changes apply t
ec70: 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 5b 64  o a single.** [d
ec80: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
ec90: 6f 6e 5d 20 28 73 70 65 63 69 66 69 65 64 20 69  on] (specified i
eca0: 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  n the first argu
ecb0: 6d 65 6e 74 29 2e 20 20 54 68 65 0a 2a 2a 20 73  ment).  The.** s
ecc0: 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67  qlite3_db_config
ecd0: 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e  () interface can
ece0: 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 69 6d   only be used im
ecf0: 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 0a  mediately after.
ed00: 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
ed10: 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 72  connection is cr
ed20: 65 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71 6c  eated using [sql
ed30: 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 0a 2a 2a  ite3_open()],.**
ed40: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36   [sqlite3_open16
ed50: 28 29 5d 2c 20 6f 72 20 5b 73 71 6c 69 74 65 33  ()], or [sqlite3
ed60: 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e 20 20 0a 2a  _open_v2()].  .*
ed70: 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  *.** The second 
ed80: 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69  argument to sqli
ed90: 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 44 2c  te3_db_config(D,
eda0: 56 2c 2e 2e 2e 29 20 20 69 73 20 74 68 65 0a 2a  V,...)  is the.*
edb0: 2a 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  * configuration 
edc0: 76 65 72 62 20 2d 20 61 6e 20 69 6e 74 65 67 65  verb - an intege
edd0: 72 20 63 6f 64 65 20 74 68 61 74 20 69 6e 64 69  r code that indi
ede0: 63 61 74 65 73 20 77 68 61 74 0a 2a 2a 20 61 73  cates what.** as
edf0: 70 65 63 74 20 6f 66 20 74 68 65 20 5b 64 61 74  pect of the [dat
ee00: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
ee10: 5d 20 69 73 20 62 65 69 6e 67 20 63 6f 6e 66 69  ] is being confi
ee20: 67 75 72 65 64 2e 0a 2a 2a 20 54 68 65 20 6f 6e  gured..** The on
ee30: 6c 79 20 63 68 6f 69 63 65 20 66 6f 72 20 74 68  ly choice for th
ee40: 69 73 20 76 61 6c 75 65 20 69 73 20 5b 53 51 4c  is value is [SQL
ee50: 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f  ITE_DBCONFIG_LOO
ee60: 4b 41 53 49 44 45 5d 2e 0a 2a 2a 20 4e 65 77 20  KASIDE]..** New 
ee70: 76 65 72 62 73 20 61 72 65 20 6c 69 6b 65 6c 79  verbs are likely
ee80: 20 74 6f 20 62 65 20 61 64 64 65 64 20 69 6e 20   to be added in 
ee90: 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20  future releases 
eea0: 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 41 64  of SQLite..** Ad
eeb0: 64 69 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e  ditional argumen
eec0: 74 73 20 64 65 70 65 6e 64 20 6f 6e 20 74 68 65  ts depend on the
eed0: 20 76 65 72 62 2e 0a 2a 2a 0a 2a 2a 20 52 65 71   verb..**.** Req
eee0: 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
eef0: 31 34 32 30 33 5d 20 5b 48 31 34 32 30 36 5d 20  14203] [H14206] 
ef00: 5b 48 31 34 32 30 39 5d 20 5b 48 31 34 32 31 32  [H14209] [H14212
ef10: 5d 20 5b 48 31 34 32 31 35 5d 0a 2a 2f 0a 53 51  ] [H14215].*/.SQ
ef20: 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f  LITE_API SQLITE_
ef30: 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74  EXPERIMENTAL int
ef40: 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66   sqlite3_db_conf
ef50: 69 67 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  ig(sqlite3*, int
ef60: 20 6f 70 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a   op, ...);../*.*
ef70: 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 65 6d 6f  * CAPI3REF: Memo
ef80: 72 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 20 52 6f  ry Allocation Ro
ef90: 75 74 69 6e 65 73 20 7b 48 31 30 31 35 35 7d 20  utines {H10155} 
efa0: 3c 53 32 30 31 32 30 3e 0a 2a 2a 20 45 58 50 45  <S20120>.** EXPE
efb0: 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 41  RIMENTAL.**.** A
efc0: 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
efd0: 69 73 20 6f 62 6a 65 63 74 20 64 65 66 69 6e 65  is object define
efe0: 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20  s the interface 
eff0: 62 65 74 77 65 65 6e 20 53 51 4c 69 74 65 0a 2a  between SQLite.*
f000: 2a 20 61 6e 64 20 6c 6f 77 2d 6c 65 76 65 6c 20  * and low-level 
f010: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
f020: 6e 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a  n routines..**.*
f030: 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73  * This object is
f040: 20 75 73 65 64 20 69 6e 20 6f 6e 6c 79 20 6f 6e   used in only on
f050: 65 20 70 6c 61 63 65 20 69 6e 20 74 68 65 20 53  e place in the S
f060: 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 2e  QLite interface.
f070: 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  .** A pointer to
f080: 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
f090: 74 68 69 73 20 6f 62 6a 65 63 74 20 69 73 20 74  this object is t
f0a0: 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a  he argument to.*
f0b0: 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  * [sqlite3_confi
f0c0: 67 28 29 5d 20 77 68 65 6e 20 74 68 65 20 63 6f  g()] when the co
f0d0: 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69  nfiguration opti
f0e0: 6f 6e 20 69 73 0a 2a 2a 20 5b 53 51 4c 49 54 45  on is.** [SQLITE
f0f0: 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 20  _CONFIG_MALLOC] 
f100: 6f 72 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  or [SQLITE_CONFI
f110: 47 5f 47 45 54 4d 41 4c 4c 4f 43 5d 2e 20 20 0a  G_GETMALLOC].  .
f120: 2a 2a 20 42 79 20 63 72 65 61 74 69 6e 67 20 61  ** By creating a
f130: 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
f140: 69 73 20 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64  is object.** and
f150: 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20 5b   passing it to [
f160: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28  sqlite3_config](
f170: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
f180: 41 4c 4c 4f 43 5d 29 0a 2a 2a 20 64 75 72 69 6e  ALLOC]).** durin
f190: 67 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c  g configuration,
f1a0: 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20   an application 
f1b0: 63 61 6e 20 73 70 65 63 69 66 79 20 61 6e 20 61  can specify an a
f1c0: 6c 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 6d 65  lternative.** me
f1d0: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
f1e0: 73 75 62 73 79 73 74 65 6d 20 66 6f 72 20 53 51  subsystem for SQ
f1f0: 4c 69 74 65 20 74 6f 20 75 73 65 20 66 6f 72 20  Lite to use for 
f200: 61 6c 6c 20 6f 66 20 69 74 73 0a 2a 2a 20 64 79  all of its.** dy
f210: 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20 6e 65 65  namic memory nee
f220: 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  ds..**.** Note t
f230: 68 61 74 20 53 51 4c 69 74 65 20 63 6f 6d 65 73  hat SQLite comes
f240: 20 77 69 74 68 20 73 65 76 65 72 61 6c 20 5b 62   with several [b
f250: 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61  uilt-in memory a
f260: 6c 6c 6f 63 61 74 6f 72 73 5d 0a 2a 2a 20 74 68  llocators].** th
f270: 61 74 20 61 72 65 20 70 65 72 66 65 63 74 6c 79  at are perfectly
f280: 20 61 64 65 71 75 61 74 65 20 66 6f 72 20 74 68   adequate for th
f290: 65 20 6f 76 65 72 77 68 65 6c 6d 69 6e 67 20 6d  e overwhelming m
f2a0: 61 6a 6f 72 69 74 79 20 6f 66 20 61 70 70 6c 69  ajority of appli
f2b0: 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 6e 64 20 74  cations.** and t
f2c0: 68 61 74 20 74 68 69 73 20 6f 62 6a 65 63 74 20  hat this object 
f2d0: 69 73 20 6f 6e 6c 79 20 75 73 65 66 75 6c 20 74  is only useful t
f2e0: 6f 20 61 20 74 69 6e 79 20 6d 69 6e 6f 72 69 74  o a tiny minorit
f2f0: 79 20 6f 66 20 61 70 70 6c 69 63 61 74 69 6f 6e  y of application
f300: 73 0a 2a 2a 20 77 69 74 68 20 73 70 65 63 69 61  s.** with specia
f310: 6c 69 7a 65 64 20 6d 65 6d 6f 72 79 20 61 6c 6c  lized memory all
f320: 6f 63 61 74 69 6f 6e 20 72 65 71 75 69 72 65 6d  ocation requirem
f330: 65 6e 74 73 2e 20 20 54 68 69 73 20 6f 62 6a 65  ents.  This obje
f340: 63 74 20 69 73 0a 2a 2a 20 61 6c 73 6f 20 75 73  ct is.** also us
f350: 65 64 20 64 75 72 69 6e 67 20 74 65 73 74 69 6e  ed during testin
f360: 67 20 6f 66 20 53 51 4c 69 74 65 20 69 6e 20 6f  g of SQLite in o
f370: 72 64 65 72 20 74 6f 20 73 70 65 63 69 66 79 20  rder to specify 
f380: 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 0a 2a  an alternative.*
f390: 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  * memory allocat
f3a0: 6f 72 20 74 68 61 74 20 73 69 6d 75 6c 61 74 65  or that simulate
f3b0: 73 20 6d 65 6d 6f 72 79 20 6f 75 74 2d 6f 66 2d  s memory out-of-
f3c0: 6d 65 6d 6f 72 79 20 63 6f 6e 64 69 74 69 6f 6e  memory condition
f3d0: 73 20 69 6e 0a 2a 2a 20 6f 72 64 65 72 20 74 6f  s in.** order to
f3e0: 20 76 65 72 69 66 79 20 74 68 61 74 20 53 51 4c   verify that SQL
f3f0: 69 74 65 20 72 65 63 6f 76 65 72 73 20 67 72 61  ite recovers gra
f400: 63 65 66 75 6c 6c 79 20 66 72 6f 6d 20 73 75 63  cefully from suc
f410: 68 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 73 2e  h.** conditions.
f420: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 61 6c 6c  .**.** The xMall
f430: 6f 63 20 61 6e 64 20 78 46 72 65 65 20 6d 65 74  oc and xFree met
f440: 68 6f 64 73 20 6d 75 73 74 20 77 6f 72 6b 20 6c  hods must work l
f450: 69 6b 65 20 74 68 65 0a 2a 2a 20 6d 61 6c 6c 6f  ike the.** mallo
f460: 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 66  c() and free() f
f470: 75 6e 63 74 69 6f 6e 73 20 66 72 6f 6d 20 74 68  unctions from th
f480: 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62  e standard C lib
f490: 72 61 72 79 2e 0a 2a 2a 20 54 68 65 20 78 52 65  rary..** The xRe
f4a0: 61 6c 6c 6f 63 20 6d 65 74 68 6f 64 20 6d 75 73  alloc method mus
f4b0: 74 20 77 6f 72 6b 20 6c 69 6b 65 20 72 65 61 6c  t work like real
f4c0: 6c 6f 63 28 29 20 66 72 6f 6d 20 74 68 65 20 73  loc() from the s
f4d0: 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72  tandard C librar
f4e0: 79 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 65 78  y.** with the ex
f4f0: 63 65 70 74 69 6f 6e 20 74 68 61 74 20 69 66 20  ception that if 
f500: 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
f510: 65 6e 74 20 74 6f 20 78 52 65 61 6c 6c 6f 63 20  ent to xRealloc 
f520: 69 73 20 7a 65 72 6f 2c 0a 2a 2a 20 78 52 65 61  is zero,.** xRea
f530: 6c 6c 6f 63 20 6d 75 73 74 20 62 65 20 61 20 6e  lloc must be a n
f540: 6f 2d 6f 70 20 2d 20 69 74 20 6d 75 73 74 20 6e  o-op - it must n
f550: 6f 74 20 70 65 72 66 6f 72 6d 20 61 6e 79 20 61  ot perform any a
f560: 6c 6c 6f 63 61 74 69 6f 6e 20 6f 72 0a 2a 2a 20  llocation or.** 
f570: 64 65 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 53  deallocation.  S
f580: 51 4c 69 74 65 20 67 75 61 72 61 6e 74 65 65 64  QLite guaranteed
f590: 73 20 74 68 61 74 20 74 68 65 20 73 65 63 6f 6e  s that the secon
f5a0: 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a  d argument to.**
f5b0: 20 78 52 65 61 6c 6c 6f 63 20 69 73 20 61 6c 77   xRealloc is alw
f5c0: 61 79 73 20 61 20 76 61 6c 75 65 20 72 65 74 75  ays a value retu
f5d0: 72 6e 65 64 20 62 79 20 61 20 70 72 69 6f 72 20  rned by a prior 
f5e0: 63 61 6c 6c 20 74 6f 20 78 52 6f 75 6e 64 75 70  call to xRoundup
f5f0: 2e 0a 2a 2a 20 41 6e 64 20 73 6f 20 69 6e 20 63  ..** And so in c
f600: 61 73 65 73 20 77 68 65 72 65 20 78 52 6f 75 6e  ases where xRoun
f610: 64 75 70 20 61 6c 77 61 79 73 20 72 65 74 75 72  dup always retur
f620: 6e 73 20 61 20 70 6f 73 69 74 69 76 65 20 6e 75  ns a positive nu
f630: 6d 62 65 72 2c 0a 2a 2a 20 78 52 65 61 6c 6c 6f  mber,.** xReallo
f640: 63 20 63 61 6e 20 70 65 72 66 6f 72 6d 20 65 78  c can perform ex
f650: 61 63 74 6c 79 20 61 73 20 74 68 65 20 73 74 61  actly as the sta
f660: 6e 64 61 72 64 20 6c 69 62 72 61 72 79 20 72 65  ndard library re
f670: 61 6c 6c 6f 63 28 29 20 61 6e 64 0a 2a 2a 20 73  alloc() and.** s
f680: 74 69 6c 6c 20 62 65 20 69 6e 20 63 6f 6d 70 6c  till be in compl
f690: 69 61 6e 63 65 20 77 69 74 68 20 74 68 69 73 20  iance with this 
f6a0: 73 70 65 63 69 66 69 63 61 74 69 6f 6e 2e 0a 2a  specification..*
f6b0: 2a 0a 2a 2a 20 78 53 69 7a 65 20 73 68 6f 75 6c  *.** xSize shoul
f6c0: 64 20 72 65 74 75 72 6e 20 74 68 65 20 61 6c 6c  d return the all
f6d0: 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61  ocated size of a
f6e0: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
f6f0: 6f 6e 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79  on.** previously
f700: 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 78   obtained from x
f710: 4d 61 6c 6c 6f 63 20 6f 72 20 78 52 65 61 6c 6c  Malloc or xReall
f720: 6f 63 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 74  oc.  The allocat
f730: 65 64 20 73 69 7a 65 0a 2a 2a 20 69 73 20 61 6c  ed size.** is al
f740: 77 61 79 73 20 61 74 20 6c 65 61 73 74 20 61 73  ways at least as
f750: 20 62 69 67 20 61 73 20 74 68 65 20 72 65 71 75   big as the requ
f760: 65 73 74 65 64 20 73 69 7a 65 20 62 75 74 20 6d  ested size but m
f770: 61 79 20 62 65 20 6c 61 72 67 65 72 2e 0a 2a 2a  ay be larger..**
f780: 0a 2a 2a 20 54 68 65 20 78 52 6f 75 6e 64 75 70  .** The xRoundup
f790: 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20   method returns 
f7a0: 77 68 61 74 20 77 6f 75 6c 64 20 62 65 20 74 68  what would be th
f7b0: 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65  e allocated size
f7c0: 20 6f 66 0a 2a 2a 20 61 20 6d 65 6d 6f 72 79 20   of.** a memory 
f7d0: 61 6c 6c 6f 63 61 74 69 6f 6e 20 67 69 76 65 6e  allocation given
f7e0: 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 65   a particular re
f7f0: 71 75 65 73 74 65 64 20 73 69 7a 65 2e 20 20 4d  quested size.  M
f800: 6f 73 74 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c  ost memory.** al
f810: 6c 6f 63 61 74 6f 72 73 20 72 6f 75 6e 64 20 75  locators round u
f820: 70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  p memory allocat
f830: 69 6f 6e 73 20 61 74 20 6c 65 61 73 74 20 74 6f  ions at least to
f840: 20 74 68 65 20 6e 65 78 74 20 6d 75 6c 74 69 70   the next multip
f850: 6c 65 0a 2a 2a 20 6f 66 20 38 2e 20 20 53 6f 6d  le.** of 8.  Som
f860: 65 20 61 6c 6c 6f 63 61 74 6f 72 73 20 72 6f 75  e allocators rou
f870: 6e 64 20 75 70 20 74 6f 20 61 20 6c 61 72 67 65  nd up to a large
f880: 72 20 6d 75 6c 74 69 70 6c 65 20 6f 72 20 74 6f  r multiple or to
f890: 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a   a power of 2..*
f8a0: 2a 20 45 76 65 72 79 20 6d 65 6d 6f 72 79 20 61  * Every memory a
f8b0: 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73  llocation reques
f8c0: 74 20 63 6f 6d 69 6e 67 20 69 6e 20 74 68 72 6f  t coming in thro
f8d0: 75 67 68 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  ugh [sqlite3_mal
f8e0: 6c 6f 63 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71  loc()].** or [sq
f8f0: 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d  lite3_realloc()]
f900: 20 66 69 72 73 74 20 63 61 6c 6c 73 20 78 52 6f   first calls xRo
f910: 75 6e 64 75 70 2e 20 20 49 66 20 78 52 6f 75 6e  undup.  If xRoun
f920: 64 75 70 20 72 65 74 75 72 6e 73 20 30 2c 20 0a  dup returns 0, .
f930: 2a 2a 20 74 68 61 74 20 63 61 75 73 65 73 20 74  ** that causes t
f940: 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
f950: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
f960: 6f 6e 20 74 6f 20 66 61 69 6c 2e 0a 2a 2a 0a 2a  on to fail..**.*
f970: 2a 20 54 68 65 20 78 49 6e 69 74 20 6d 65 74 68  * The xInit meth
f980: 6f 64 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 74  od initializes t
f990: 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
f9a0: 74 6f 72 2e 20 20 28 46 6f 72 20 65 78 61 6d 70  tor.  (For examp
f9b0: 6c 65 2c 0a 2a 2a 20 69 74 20 6d 69 67 68 74 20  le,.** it might 
f9c0: 61 6c 6c 6f 63 61 74 65 20 61 6e 79 20 72 65 71  allocate any req
f9d0: 75 69 72 65 20 6d 75 74 65 78 65 73 20 6f 72 20  uire mutexes or 
f9e0: 69 6e 69 74 69 61 6c 69 7a 65 20 69 6e 74 65 72  initialize inter
f9f0: 6e 61 6c 20 64 61 74 61 0a 2a 2a 20 73 74 72 75  nal data.** stru
fa00: 63 74 75 72 65 73 2e 20 20 54 68 65 20 78 53 68  ctures.  The xSh
fa10: 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 20 69 73  utdown method is
fa20: 20 69 6e 76 6f 6b 65 64 20 28 69 6e 64 69 72 65   invoked (indire
fa30: 63 74 6c 79 29 20 62 79 0a 2a 2a 20 5b 73 71 6c  ctly) by.** [sql
fa40: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d  ite3_shutdown()]
fa50: 20 61 6e 64 20 73 68 6f 75 6c 64 20 64 65 61 6c   and should deal
fa60: 6c 6f 63 61 74 65 20 61 6e 79 20 72 65 73 6f 75  locate any resou
fa70: 72 63 65 73 20 61 63 71 75 69 72 65 64 0a 2a 2a  rces acquired.**
fa80: 20 62 79 20 78 49 6e 69 74 2e 20 20 54 68 65 20   by xInit.  The 
fa90: 70 41 70 70 44 61 74 61 20 70 6f 69 6e 74 65 72  pAppData pointer
faa0: 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65 20   is used as the 
fab0: 6f 6e 6c 79 20 70 61 72 61 6d 65 74 65 72 20 74  only parameter t
fac0: 6f 0a 2a 2a 20 78 49 6e 69 74 20 61 6e 64 20 78  o.** xInit and x
fad0: 53 68 75 74 64 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20  Shutdown..**.** 
fae0: 53 51 4c 69 74 65 20 68 6f 6c 64 73 20 74 68 65  SQLite holds the
faf0: 20 5b 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53   [SQLITE_MUTEX_S
fb00: 54 41 54 49 43 5f 4d 41 53 54 45 52 5d 20 6d 75  TATIC_MASTER] mu
fb10: 74 65 78 20 77 68 65 6e 20 69 74 20 69 6e 76 6f  tex when it invo
fb20: 6b 65 73 0a 2a 2a 20 74 68 65 20 78 49 6e 69 74  kes.** the xInit
fb30: 20 6d 65 74 68 6f 64 2c 20 73 6f 20 74 68 65 20   method, so the 
fb40: 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 6e 65 65  xInit method nee
fb50: 64 20 6e 6f 74 20 62 65 20 74 68 72 65 61 64 73  d not be threads
fb60: 61 66 65 2e 20 20 54 68 65 0a 2a 2a 20 78 53 68  afe.  The.** xSh
fb70: 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 20 69 73  utdown method is
fb80: 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f   only called fro
fb90: 6d 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64  m [sqlite3_shutd
fba0: 6f 77 6e 28 29 5d 20 73 6f 20 69 74 20 64 6f 65  own()] so it doe
fbb0: 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f  s.** not need to
fbc0: 20 62 65 20 74 68 72 65 61 64 73 61 66 65 20 65   be threadsafe e
fbd0: 69 74 68 65 72 2e 20 20 46 6f 72 20 61 6c 6c 20  ither.  For all 
fbe0: 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 2c 20 53  other methods, S
fbf0: 51 4c 69 74 65 0a 2a 2a 20 68 6f 6c 64 73 20 74  QLite.** holds t
fc00: 68 65 20 5b 53 51 4c 49 54 45 5f 4d 55 54 45 58  he [SQLITE_MUTEX
fc10: 5f 53 54 41 54 49 43 5f 4d 45 4d 5d 20 6d 75 74  _STATIC_MEM] mut
fc20: 65 78 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68  ex as long as th
fc30: 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e  e.** [SQLITE_CON
fc40: 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 5d 20 63  FIG_MEMSTATUS] c
fc50: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74  onfiguration opt
fc60: 69 6f 6e 20 69 73 20 74 75 72 6e 65 64 20 6f 6e  ion is turned on
fc70: 20 28 77 68 69 63 68 0a 2a 2a 20 69 74 20 69 73   (which.** it is
fc80: 20 62 79 20 64 65 66 61 75 6c 74 29 20 61 6e 64   by default) and
fc90: 20 73 6f 20 74 68 65 20 6d 65 74 68 6f 64 73 20   so the methods 
fca0: 61 72 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  are automaticall
fcb0: 79 20 73 65 72 69 61 6c 69 7a 65 64 2e 0a 2a 2a  y serialized..**
fcc0: 20 48 6f 77 65 76 65 72 2c 20 69 66 20 5b 53 51   However, if [SQ
fcd0: 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53  LITE_CONFIG_MEMS
fce0: 54 41 54 55 53 5d 20 69 73 20 64 69 73 61 62 6c  TATUS] is disabl
fcf0: 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6f 74 68  ed, then the oth
fd00: 65 72 0a 2a 2a 20 6d 65 74 68 6f 64 73 20 6d 75  er.** methods mu
fd10: 73 74 20 62 65 20 74 68 72 65 61 64 73 61 66 65  st be threadsafe
fd20: 20 6f 72 20 65 6c 73 65 20 6d 61 6b 65 20 74 68   or else make th
fd30: 65 69 72 20 6f 77 6e 20 61 72 72 61 6e 67 65 6d  eir own arrangem
fd40: 65 6e 74 73 20 66 6f 72 0a 2a 2a 20 73 65 72 69  ents for.** seri
fd50: 61 6c 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  alization..**.**
fd60: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76   SQLite will nev
fd70: 65 72 20 69 6e 76 6f 6b 65 20 78 49 6e 69 74 28  er invoke xInit(
fd80: 29 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  ) more than once
fd90: 20 77 69 74 68 6f 75 74 20 61 6e 20 69 6e 74 65   without an inte
fda0: 72 76 65 6e 69 6e 67 0a 2a 2a 20 63 61 6c 6c 20  rvening.** call 
fdb0: 74 6f 20 78 53 68 75 74 64 6f 77 6e 28 29 2e 0a  to xShutdown()..
fdc0: 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
fdd0: 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65  t sqlite3_mem_me
fde0: 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 6d 65  thods sqlite3_me
fdf0: 6d 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63  m_methods;.struc
fe00: 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65  t sqlite3_mem_me
fe10: 74 68 6f 64 73 20 7b 0a 20 20 76 6f 69 64 20 2a  thods {.  void *
fe20: 28 2a 78 4d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b  (*xMalloc)(int);
fe30: 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f           /* Memo
fe40: 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75  ry allocation fu
fe50: 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64  nction */.  void
fe60: 20 28 2a 78 46 72 65 65 29 28 76 6f 69 64 2a 29   (*xFree)(void*)
fe70: 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 72  ;          /* Fr
fe80: 65 65 20 61 20 70 72 69 6f 72 20 61 6c 6c 6f 63  ee a prior alloc
fe90: 61 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20  ation */.  void 
fea0: 2a 28 2a 78 52 65 61 6c 6c 6f 63 29 28 76 6f 69  *(*xRealloc)(voi
feb0: 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 52 65 73  d*,int);  /* Res
fec0: 69 7a 65 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f  ize an allocatio
fed0: 6e 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 69  n */.  int (*xSi
fee0: 7a 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20  ze)(void*);     
fef0: 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
ff00: 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 61  the size of an a
ff10: 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 69  llocation */.  i
ff20: 6e 74 20 28 2a 78 52 6f 75 6e 64 75 70 29 28 69  nt (*xRoundup)(i
ff30: 6e 74 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  nt);          /*
ff40: 20 52 6f 75 6e 64 20 75 70 20 72 65 71 75 65 73   Round up reques
ff50: 74 20 73 69 7a 65 20 74 6f 20 61 6c 6c 6f 63 61  t size to alloca
ff60: 74 69 6f 6e 20 73 69 7a 65 20 2a 2f 0a 20 20 69  tion size */.  i
ff70: 6e 74 20 28 2a 78 49 6e 69 74 29 28 76 6f 69 64  nt (*xInit)(void
ff80: 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  *);           /*
ff90: 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   Initialize the 
ffa0: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
ffb0: 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 68   */.  void (*xSh
ffc0: 75 74 64 6f 77 6e 29 28 76 6f 69 64 2a 29 3b 20  utdown)(void*); 
ffd0: 20 20 20 20 20 2f 2a 20 44 65 69 6e 69 74 69 61       /* Deinitia
ffe0: 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  lize the memory 
fff0: 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 76  allocator */.  v
10000 6f 69 64 20 2a 70 41 70 70 44 61 74 61 3b 20 20  oid *pAppData;  
10010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10020 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 49 6e   Argument to xIn
10030 69 74 28 29 20 61 6e 64 20 78 53 68 75 74 64 6f  it() and xShutdo
10040 77 6e 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  wn() */.};../*.*
10050 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 66  * CAPI3REF: Conf
10060 69 67 75 72 61 74 69 6f 6e 20 4f 70 74 69 6f 6e  iguration Option
10070 73 20 7b 48 31 30 31 36 30 7d 20 3c 53 32 30 30  s {H10160} <S200
10080 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  00>.** EXPERIMEN
10090 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  TAL.**.** These 
100a0 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68  constants are th
100b0 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 65  e available inte
100c0 67 65 72 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  ger configuratio
100d0 6e 20 6f 70 74 69 6f 6e 73 20 74 68 61 74 0a 2a  n options that.*
100e0 2a 20 63 61 6e 20 62 65 20 70 61 73 73 65 64 20  * can be passed 
100f0 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  as the first arg
10100 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b 73 71  ument to the [sq
10110 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20  lite3_config()] 
10120 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a  interface..**.**
10130 20 4e 65 77 20 63 6f 6e 66 69 67 75 72 61 74 69   New configurati
10140 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d 61 79 20 62  on options may b
10150 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72  e added in futur
10160 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51  e releases of SQ
10170 4c 69 74 65 2e 0a 2a 2a 20 45 78 69 73 74 69 6e  Lite..** Existin
10180 67 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  g configuration 
10190 6f 70 74 69 6f 6e 73 20 6d 69 67 68 74 20 62 65  options might be
101a0 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 2e 20 20   discontinued.  
101b0 41 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20  Applications.** 
101c0 73 68 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 65  should check the
101d0 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f   return code fro
101e0 6d 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  m [sqlite3_confi
101f0 67 28 29 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72  g()] to make sur
10200 65 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 61  e that.** the ca
10210 6c 6c 20 77 6f 72 6b 65 64 2e 20 20 54 68 65 20  ll worked.  The 
10220 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28  [sqlite3_config(
10230 29 5d 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c  )] interface wil
10240 6c 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f  l return a.** no
10250 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f  n-zero [error co
10260 64 65 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74  de] if a discont
10270 69 6e 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f  inued or unsuppo
10280 72 74 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69  rted configurati
10290 6f 6e 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20  on option.** is 
102a0 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c  invoked..**.** <
102b0 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  dl>.** <dt>SQLIT
102c0 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54  E_CONFIG_SINGLET
102d0 48 52 45 41 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  HREAD</dt>.** <d
102e0 64 3e 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61  d>There are no a
102f0 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73  rguments to this
10300 20 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f   option.  This o
10310 70 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 0a 2a  ption disables.*
10320 2a 20 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 61  * all mutexing a
10330 6e 64 20 70 75 74 73 20 53 51 4c 69 74 65 20 69  nd puts SQLite i
10340 6e 74 6f 20 61 20 6d 6f 64 65 20 77 68 65 72 65  nto a mode where
10350 20 69 74 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20   it can only be 
10360 75 73 65 64 0a 2a 2a 20 62 79 20 61 20 73 69 6e  used.** by a sin
10370 67 6c 65 20 74 68 72 65 61 64 2e 3c 2f 64 64 3e  gle thread.</dd>
10380 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
10390 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48  E_CONFIG_MULTITH
103a0 52 45 41 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  READ</dt>.** <dd
103b0 3e 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72  >There are no ar
103c0 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20  guments to this 
103d0 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70  option.  This op
103e0 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 0a 2a 2a  tion disables.**
103f0 20 6d 75 74 65 78 69 6e 67 20 6f 6e 20 5b 64 61   mutexing on [da
10400 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
10410 6e 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64  n] and [prepared
10420 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f 62 6a 65   statement] obje
10430 63 74 73 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c  cts..** The appl
10440 69 63 61 74 69 6f 6e 20 69 73 20 72 65 73 70 6f  ication is respo
10450 6e 73 69 62 6c 65 20 66 6f 72 20 73 65 72 69 61  nsible for seria
10460 6c 69 7a 69 6e 67 20 61 63 63 65 73 73 20 74 6f  lizing access to
10470 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f  .** [database co
10480 6e 6e 65 63 74 69 6f 6e 73 5d 20 61 6e 64 20 5b  nnections] and [
10490 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
104a0 6e 74 73 5d 2e 20 20 42 75 74 20 6f 74 68 65 72  nts].  But other
104b0 20 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 65 20   mutexes.** are 
104c0 65 6e 61 62 6c 65 64 20 73 6f 20 74 68 61 74 20  enabled so that 
104d0 53 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 73  SQLite will be s
104e0 61 66 65 20 74 6f 20 75 73 65 20 69 6e 20 61 20  afe to use in a 
104f0 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64 0a 2a  multi-threaded.*
10500 2a 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 61 73  * environment as
10510 20 6c 6f 6e 67 20 61 73 20 6e 6f 20 74 77 6f 20   long as no two 
10520 74 68 72 65 61 64 73 20 61 74 74 65 6d 70 74 20  threads attempt 
10530 74 6f 20 75 73 65 20 74 68 65 20 73 61 6d 65 0a  to use the same.
10540 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ** [database con
10550 6e 65 63 74 69 6f 6e 5d 20 61 74 20 74 68 65 20  nection] at the 
10560 73 61 6d 65 20 74 69 6d 65 2e 20 20 53 65 65 20  same time.  See 
10570 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d  the [threading m
10580 6f 64 65 5d 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74  ode].** document
10590 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69  ation for additi
105a0 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
105b0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
105c0 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53  >SQLITE_CONFIG_S
105d0 45 52 49 41 4c 49 5a 45 44 3c 2f 64 74 3e 0a 2a  ERIALIZED</dt>.*
105e0 2a 20 3c 64 64 3e 54 68 65 72 65 20 61 72 65 20  * <dd>There are 
105f0 6e 6f 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  no arguments to 
10600 74 68 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54 68  this option.  Th
10610 69 73 20 6f 70 74 69 6f 6e 20 65 6e 61 62 6c 65  is option enable
10620 73 0a 2a 2a 20 61 6c 6c 20 6d 75 74 65 78 65 73  s.** all mutexes
10630 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 72   including the r
10640 65 63 75 72 73 69 76 65 0a 2a 2a 20 6d 75 74 65  ecursive.** mute
10650 78 65 73 20 6f 6e 20 5b 64 61 74 61 62 61 73 65  xes on [database
10660 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64   connection] and
10670 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
10680 6d 65 6e 74 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a  ment] objects..*
10690 2a 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 20 28  * In this mode (
106a0 77 68 69 63 68 20 69 73 20 74 68 65 20 64 65 66  which is the def
106b0 61 75 6c 74 20 77 68 65 6e 20 53 51 4c 69 74 65  ault when SQLite
106c0 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74   is compiled wit
106d0 68 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 54 48 52  h.** [SQLITE_THR
106e0 45 41 44 53 41 46 45 3d 31 5d 29 20 74 68 65 20  EADSAFE=1]) the 
106f0 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77  SQLite library w
10700 69 6c 6c 20 69 74 73 65 6c 66 20 73 65 72 69 61  ill itself seria
10710 6c 69 7a 65 20 61 63 63 65 73 73 0a 2a 2a 20 74  lize access.** t
10720 6f 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  o [database conn
10730 65 63 74 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72  ections] and [pr
10740 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
10750 73 5d 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a  s] so that the.*
10760 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 73  * application is
10770 20 66 72 65 65 20 74 6f 20 75 73 65 20 74 68 65   free to use the
10780 20 73 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20   same [database 
10790 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f 72 20 74  connection] or t
107a0 68 65 0a 2a 2a 20 73 61 6d 65 20 5b 70 72 65 70  he.** same [prep
107b0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
107c0 69 6e 20 64 69 66 66 65 72 65 6e 74 20 74 68 72  in different thr
107d0 65 61 64 73 20 61 74 20 74 68 65 20 73 61 6d 65  eads at the same
107e0 20 74 69 6d 65 2e 0a 2a 2a 20 53 65 65 20 74 68   time..** See th
107f0 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64  e [threading mod
10800 65 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  e] documentation
10810 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
10820 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64  information.</dd
10830 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
10840 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43  TE_CONFIG_MALLOC
10850 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
10860 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61  s option takes a
10870 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74   single argument
10880 20 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e   which is a poin
10890 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73  ter to an.** ins
108a0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71  tance of the [sq
108b0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
108c0 73 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  s] structure.  T
108d0 68 65 20 61 72 67 75 6d 65 6e 74 20 73 70 65 63  he argument spec
108e0 69 66 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61  ifies.** alterna
108f0 74 69 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d  tive low-level m
10900 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
10910 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20   routines to be 
10920 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66  used in place of
10930 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  .** the memory a
10940 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e  llocation routin
10950 65 73 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51  es built into SQ
10960 4c 69 74 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  Lite.</dd>.**.**
10970 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46   <dt>SQLITE_CONF
10980 49 47 5f 47 45 54 4d 41 4c 4c 4f 43 3c 2f 64 74  IG_GETMALLOC</dt
10990 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70  >.** <dd>This op
109a0 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e  tion takes a sin
109b0 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69  gle argument whi
109c0 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ch is a pointer 
109d0 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63  to an.** instanc
109e0 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65  e of the [sqlite
109f0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73  3_mem_methods] s
10a00 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 5b  tructure.  The [
10a10 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
10a20 6f 64 73 5d 0a 2a 2a 20 73 74 72 75 63 74 75 72  ods].** structur
10a30 65 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68  e is filled with
10a40 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20 64   the currently d
10a50 65 66 69 6e 65 64 20 6d 65 6d 6f 72 79 20 61 6c  efined memory al
10a60 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65  location routine
10a70 73 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f  s..** This optio
10a80 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  n can be used to
10a90 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 64 65   overload the de
10aa0 66 61 75 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c  fault memory all
10ab0 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69  ocation.** routi
10ac0 6e 65 73 20 77 69 74 68 20 61 20 77 72 61 70 70  nes with a wrapp
10ad0 65 72 20 74 68 61 74 20 73 69 6d 75 6c 61 74 69  er that simulati
10ae0 6f 6e 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ons memory alloc
10af0 61 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 6f 72  ation failure or
10b00 0a 2a 2a 20 74 72 61 63 6b 73 20 6d 65 6d 6f 72  .** tracks memor
10b10 79 20 75 73 61 67 65 2c 20 66 6f 72 20 65 78 61  y usage, for exa
10b20 6d 70 6c 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  mple.</dd>.**.**
10b30 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46   <dt>SQLITE_CONF
10b40 49 47 5f 4d 45 4d 53 54 41 54 55 53 3c 2f 64 74  IG_MEMSTATUS</dt
10b50 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70  >.** <dd>This op
10b60 74 69 6f 6e 20 74 61 6b 65 73 20 73 69 6e 67 6c  tion takes singl
10b70 65 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 79  e argument of ty
10b80 70 65 20 69 6e 74 2c 20 69 6e 74 65 72 70 72 65  pe int, interpre
10b90 74 65 64 20 61 73 20 61 20 0a 2a 2a 20 62 6f 6f  ted as a .** boo
10ba0 6c 65 61 6e 2c 20 77 68 69 63 68 20 65 6e 61 62  lean, which enab
10bb0 6c 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20  les or disables 
10bc0 74 68 65 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f  the collection o
10bd0 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  f memory allocat
10be0 69 6f 6e 20 0a 2a 2a 20 73 74 61 74 69 73 74 69  ion .** statisti
10bf0 63 73 2e 20 57 68 65 6e 20 64 69 73 61 62 6c 65  cs. When disable
10c00 64 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  d, the following
10c10 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63   SQLite interfac
10c20 65 73 20 62 65 63 6f 6d 65 20 0a 2a 2a 20 6e 6f  es become .** no
10c30 6e 2d 6f 70 65 72 61 74 69 6f 6e 61 6c 3a 0a 2a  n-operational:.*
10c40 2a 20 20 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c  *   <ul>.**   <l
10c50 69 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  i> [sqlite3_memo
10c60 72 79 5f 75 73 65 64 28 29 5d 0a 2a 2a 20 20 20  ry_used()].**   
10c70 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f 6d 65  <li> [sqlite3_me
10c80 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 29  mory_highwater()
10c90 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c  ].**   <li> [sql
10ca0 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c  ite3_soft_heap_l
10cb0 69 6d 69 74 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69  imit()].**   <li
10cc0 3e 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75  > [sqlite3_statu
10cd0 73 28 29 5d 0a 2a 2a 20 20 20 3c 2f 75 6c 3e 0a  s()].**   </ul>.
10ce0 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  ** </dd>.**.** <
10cf0 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  dt>SQLITE_CONFIG
10d00 5f 53 43 52 41 54 43 48 3c 2f 64 74 3e 0a 2a 2a  _SCRATCH</dt>.**
10d10 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e   <dd>This option
10d20 20 73 70 65 63 69 66 69 65 73 20 61 20 73 74 61   specifies a sta
10d30 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65  tic memory buffe
10d40 72 20 74 68 61 74 20 53 51 4c 69 74 65 20 63 61  r that SQLite ca
10d50 6e 20 75 73 65 20 66 6f 72 0a 2a 2a 20 73 63 72  n use for.** scr
10d60 61 74 63 68 20 6d 65 6d 6f 72 79 2e 20 20 54 68  atch memory.  Th
10d70 65 72 65 20 61 72 65 20 74 68 72 65 65 20 61 72  ere are three ar
10d80 67 75 6d 65 6e 74 73 3a 20 20 41 20 70 6f 69 6e  guments:  A poin
10d90 74 65 72 20 61 6e 20 38 2d 62 79 74 65 0a 2a 2a  ter an 8-byte.**
10da0 20 61 6c 69 67 6e 65 64 20 6d 65 6d 6f 72 79 20   aligned memory 
10db0 62 75 66 66 65 72 20 66 72 6f 6d 20 77 68 69 63  buffer from whic
10dc0 68 20 74 68 65 20 73 63 72 61 63 68 20 61 6c 6c  h the scrach all
10dd0 6f 63 61 74 69 6f 6e 73 20 77 69 6c 6c 20 62 65  ocations will be
10de0 0a 2a 2a 20 64 72 61 77 6e 2c 20 74 68 65 20 73  .** drawn, the s
10df0 69 7a 65 20 6f 66 20 65 61 63 68 20 73 63 72 61  ize of each scra
10e00 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28  tch allocation (
10e10 73 7a 29 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20  sz),.** and the 
10e20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
10e30 66 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61  f scratch alloca
10e40 74 69 6f 6e 73 20 28 4e 29 2e 20 20 54 68 65 20  tions (N).  The 
10e50 73 7a 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6d  sz.** argument m
10e60 75 73 74 20 62 65 20 61 20 6d 75 6c 74 69 70 6c  ust be a multipl
10e70 65 20 6f 66 20 31 36 2e 20 54 68 65 20 73 7a 20  e of 16. The sz 
10e80 70 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64  parameter should
10e90 20 62 65 20 61 20 66 65 77 20 62 79 74 65 73 0a   be a few bytes.
10ea0 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  ** larger than t
10eb0 68 65 20 61 63 74 75 61 6c 20 73 63 72 61 74 63  he actual scratc
10ec0 68 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64  h space required
10ed0 20 64 75 65 20 74 6f 20 69 6e 74 65 72 6e 61 6c   due to internal
10ee0 20 6f 76 65 72 68 65 61 64 2e 0a 2a 2a 20 54 68   overhead..** Th
10ef0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
10f00 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74 65 72 20   should pointer 
10f10 74 6f 20 61 6e 20 38 2d 62 79 74 65 20 61 6c 69  to an 8-byte ali
10f20 67 6e 65 64 20 62 75 66 66 65 72 0a 2a 2a 20 6f  gned buffer.** o
10f30 66 20 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20  f at least sz*N 
10f40 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e  bytes of memory.
10f50 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  .** SQLite will 
10f60 75 73 65 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e  use no more than
10f70 20 6f 6e 65 20 73 63 72 61 74 63 68 20 62 75 66   one scratch buf
10f80 66 65 72 20 61 74 20 6f 6e 63 65 20 70 65 72 20  fer at once per 
10f90 74 68 72 65 61 64 2c 20 73 6f 0a 2a 2a 20 4e 20  thread, so.** N 
10fa0 73 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f  should be set to
10fb0 20 74 68 65 20 65 78 70 65 63 74 65 64 20 6d 61   the expected ma
10fc0 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
10fd0 74 68 72 65 61 64 73 2e 20 20 54 68 65 20 73 7a  threads.  The sz
10fe0 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 73 68  .** parameter sh
10ff0 6f 75 6c 64 20 62 65 20 36 20 74 69 6d 65 73 20  ould be 6 times 
11000 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
11010 6c 61 72 67 65 73 74 20 64 61 74 61 62 61 73 65  largest database
11020 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 53   page size..** S
11030 63 72 61 74 63 68 20 62 75 66 66 65 72 73 20 61  cratch buffers a
11040 72 65 20 75 73 65 64 20 61 73 20 70 61 72 74 20  re used as part 
11050 6f 66 20 74 68 65 20 62 74 72 65 65 20 62 61 6c  of the btree bal
11060 61 6e 63 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20  ance operation. 
11070 20 49 66 0a 2a 2a 20 54 68 65 20 62 74 72 65 65   If.** The btree
11080 20 62 61 6c 61 6e 63 65 72 20 6e 65 65 64 73 20   balancer needs 
11090 61 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72  additional memor
110a0 79 20 62 65 79 6f 6e 64 20 77 68 61 74 20 69 73  y beyond what is
110b0 20 70 72 6f 76 69 64 65 64 20 62 79 0a 2a 2a 20   provided by.** 
110c0 73 63 72 61 74 63 68 20 62 75 66 66 65 72 73 20  scratch buffers 
110d0 6f 72 20 69 66 20 6e 6f 20 73 63 72 61 74 63 68  or if no scratch
110e0 20 62 75 66 66 65 72 20 73 70 61 63 65 20 69 73   buffer space is
110f0 20 73 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e   specified, then
11100 20 53 51 4c 69 74 65 0a 2a 2a 20 67 6f 65 73 20   SQLite.** goes 
11110 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  to [sqlite3_mall
11120 6f 63 28 29 5d 20 74 6f 20 6f 62 74 61 69 6e 20  oc()] to obtain 
11130 74 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65  the memory it ne
11140 65 64 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  eds.</dd>.**.** 
11150 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49  <dt>SQLITE_CONFI
11160 47 5f 50 41 47 45 43 41 43 48 45 3c 2f 64 74 3e  G_PAGECACHE</dt>
11170 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74  .** <dd>This opt
11180 69 6f 6e 20 73 70 65 63 69 66 69 65 73 20 61 20  ion specifies a 
11190 73 74 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75  static memory bu
111a0 66 66 65 72 20 74 68 61 74 20 53 51 4c 69 74 65  ffer that SQLite
111b0 20 63 61 6e 20 75 73 65 20 66 6f 72 0a 2a 2a 20   can use for.** 
111c0 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67  the database pag
111d0 65 20 63 61 63 68 65 20 77 69 74 68 20 74 68 65  e cache with the
111e0 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 63 61   default page ca
111f0 63 68 65 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f  che implemenatio
11200 6e 2e 20 20 0a 2a 2a 20 54 68 69 73 20 63 6f 6e  n.  .** This con
11210 66 69 67 75 72 61 74 69 6f 6e 20 73 68 6f 75 6c  figuration shoul
11220 64 20 6e 6f 74 20 62 65 20 75 73 65 64 20 69 66  d not be used if
11230 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d   an application-
11240 64 65 66 69 6e 65 20 70 61 67 65 0a 2a 2a 20 63  define page.** c
11250 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ache implementat
11260 69 6f 6e 20 69 73 20 6c 6f 61 64 65 64 20 75 73  ion is loaded us
11270 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f 43  ing the SQLITE_C
11280 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 6f 70 74  ONFIG_PCACHE opt
11290 69 6f 6e 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72  ion..** There ar
112a0 65 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74  e three argument
112b0 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e  s to this option
112c0 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 38  : A pointer to 8
112d0 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 0a 2a 2a  -byte aligned.**
112e0 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 73 69 7a   memory, the siz
112f0 65 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 62  e of each page b
11300 75 66 66 65 72 20 28 73 7a 29 2c 20 61 6e 64 20  uffer (sz), and 
11310 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
11320 67 65 73 20 28 4e 29 2e 0a 2a 2a 20 54 68 65 20  ges (N)..** The 
11330 73 7a 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75  sz argument shou
11340 6c 64 20 62 65 20 74 68 65 20 73 69 7a 65 20 6f  ld be the size o
11350 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 64 61  f the largest da
11360 74 61 62 61 73 65 20 70 61 67 65 0a 2a 2a 20 28  tabase page.** (
11370 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 62  a power of two b
11380 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20 33  etween 512 and 3
11390 32 37 36 38 29 20 70 6c 75 73 20 61 20 6c 69 74  2768) plus a lit
113a0 74 6c 65 20 65 78 74 72 61 20 66 6f 72 20 65 61  tle extra for ea
113b0 63 68 0a 2a 2a 20 70 61 67 65 20 68 65 61 64 65  ch.** page heade
113c0 72 2e 20 20 54 68 65 20 70 61 67 65 20 68 65 61  r.  The page hea
113d0 64 65 72 20 73 69 7a 65 20 69 73 20 32 30 20 74  der size is 20 t
113e0 6f 20 34 30 20 62 79 74 65 73 20 64 65 70 65 6e  o 40 bytes depen
113f0 64 69 6e 67 20 6f 6e 0a 2a 2a 20 74 68 65 20 68  ding on.** the h
11400 6f 73 74 20 61 72 63 68 69 74 65 63 74 75 72 65  ost architecture
11410 2e 20 20 49 74 20 69 73 20 68 61 72 6d 6c 65 73  .  It is harmles
11420 73 2c 20 61 70 61 72 74 20 66 72 6f 6d 20 74 68  s, apart from th
11430 65 20 77 61 73 74 65 64 20 6d 65 6d 6f 72 79 2c  e wasted memory,
11440 0a 2a 2a 20 74 6f 20 6d 61 6b 65 20 73 7a 20 61  .** to make sz a
11450 20 6c 69 74 74 6c 65 20 74 6f 6f 20 6c 61 72 67   little too larg
11460 65 2e 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a  e.  The first.**
11470 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64   argument should
11480 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 61 6c 6c   point to an all
11490 6f 63 61 74 69 6f 6e 20 6f 66 20 61 74 20 6c 65  ocation of at le
114a0 61 73 74 20 73 7a 2a 4e 20 62 79 74 65 73 20 6f  ast sz*N bytes o
114b0 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 53 51 4c  f memory..** SQL
114c0 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65  ite will use the
114d0 20 6d 65 6d 6f 72 79 20 70 72 6f 76 69 64 65 64   memory provided
114e0 20 62 79 20 74 68 65 20 66 69 72 73 74 20 61 72   by the first ar
114f0 67 75 6d 65 6e 74 20 74 6f 20 73 61 74 69 73 66  gument to satisf
11500 79 20 69 74 73 0a 2a 2a 20 6d 65 6d 6f 72 79 20  y its.** memory 
11510 6e 65 65 64 73 20 66 6f 72 20 74 68 65 20 66 69  needs for the fi
11520 72 73 74 20 4e 20 70 61 67 65 73 20 74 68 61 74  rst N pages that
11530 20 69 74 20 61 64 64 73 20 74 6f 20 63 61 63 68   it adds to cach
11540 65 2e 20 20 49 66 20 61 64 64 69 74 69 6f 6e 61  e.  If additiona
11550 6c 0a 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20  l.** page cache 
11560 6d 65 6d 6f 72 79 20 69 73 20 6e 65 65 64 65 64  memory is needed
11570 20 62 65 79 6f 6e 64 20 77 68 61 74 20 69 73 20   beyond what is 
11580 70 72 6f 76 69 64 65 64 20 62 79 20 74 68 69 73  provided by this
11590 20 6f 70 74 69 6f 6e 2c 20 74 68 65 6e 0a 2a 2a   option, then.**
115a0 20 53 51 4c 69 74 65 20 67 6f 65 73 20 74 6f 20   SQLite goes to 
115b0 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
115c0 29 5d 20 66 6f 72 20 74 68 65 20 61 64 64 69 74  )] for the addit
115d0 69 6f 6e 61 6c 20 73 74 6f 72 61 67 65 20 73 70  ional storage sp
115e0 61 63 65 2e 0a 2a 2a 20 54 68 65 20 69 6d 70 6c  ace..** The impl
115f0 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74  ementation might
11600 20 75 73 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65   use one or more
11610 20 6f 66 20 74 68 65 20 4e 20 62 75 66 66 65 72   of the N buffer
11620 73 20 74 6f 20 68 6f 6c 64 20 0a 2a 2a 20 6d 65  s to hold .** me
11630 6d 6f 72 79 20 61 63 63 6f 75 6e 74 69 6e 67 20  mory accounting 
11640 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 54 68 65  information. The
11650 20 70 6f 69 6e 74 65 72 20 69 6e 20 74 68 65 20   pointer in the 
11660 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 6d  first argument m
11670 75 73 74 0a 2a 2a 20 62 65 20 61 6c 69 67 6e 65  ust.** be aligne
11680 64 20 74 6f 20 61 6e 20 38 2d 62 79 74 65 20 62  d to an 8-byte b
11690 6f 75 6e 64 61 72 79 20 6f 72 20 73 75 62 73 65  oundary or subse
116a0 71 75 65 6e 74 20 62 65 68 61 76 69 6f 72 20 6f  quent behavior o
116b0 66 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c  f SQLite.** will
116c0 20 62 65 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f   be undefined.</
116d0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
116e0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45 41 50  LITE_CONFIG_HEAP
116f0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
11700 73 20 6f 70 74 69 6f 6e 20 73 70 65 63 69 66 69  s option specifi
11710 65 73 20 61 20 73 74 61 74 69 63 20 6d 65 6d 6f  es a static memo
11720 72 79 20 62 75 66 66 65 72 20 74 68 61 74 20 53  ry buffer that S
11730 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 0a 2a  QLite will use.*
11740 2a 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73  * for all of its
11750 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72 79 20   dynamic memory 
11760 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73  allocation needs
11770 20 62 65 79 6f 6e 64 20 74 68 6f 73 65 20 70 72   beyond those pr
11780 6f 76 69 64 65 64 0a 2a 2a 20 66 6f 72 20 62 79  ovided.** for by
11790 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
117a0 53 43 52 41 54 43 48 5d 20 61 6e 64 20 5b 53 51  SCRATCH] and [SQ
117b0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45  LITE_CONFIG_PAGE
117c0 43 41 43 48 45 5d 2e 0a 2a 2a 20 54 68 65 72 65  CACHE]..** There
117d0 20 61 72 65 20 74 68 72 65 65 20 61 72 67 75 6d   are three argum
117e0 65 6e 74 73 3a 20 41 6e 20 38 2d 62 79 74 65 20  ents: An 8-byte 
117f0 61 6c 69 67 6e 65 64 20 70 6f 69 6e 74 65 72 20  aligned pointer 
11800 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 2c 0a 2a  to the memory,.*
11810 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
11820 62 79 74 65 73 20 69 6e 20 74 68 65 20 6d 65 6d  bytes in the mem
11830 6f 72 79 20 62 75 66 66 65 72 2c 20 61 6e 64 20  ory buffer, and 
11840 74 68 65 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f  the minimum allo
11850 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2a 20  cation size..** 
11860 49 66 20 74 68 65 20 66 69 72 73 74 20 70 6f 69  If the first poi
11870 6e 74 65 72 20 28 74 68 65 20 6d 65 6d 6f 72 79  nter (the memory
11880 20 70 6f 69 6e 74 65 72 29 20 69 73 20 4e 55 4c   pointer) is NUL
11890 4c 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 72  L, then SQLite r
118a0 65 76 65 72 74 73 0a 2a 2a 20 74 6f 20 75 73 69  everts.** to usi
118b0 6e 67 20 69 74 73 20 64 65 66 61 75 6c 74 20 6d  ng its default m
118c0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
118d0 28 74 68 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c  (the system mall
118e0 6f 63 28 29 20 69 6d 70 6c 65 6d 65 6e 74 61 74  oc() implementat
118f0 69 6f 6e 29 2c 0a 2a 2a 20 75 6e 64 6f 69 6e 67  ion),.** undoing
11900 20 61 6e 79 20 70 72 69 6f 72 20 69 6e 76 6f 63   any prior invoc
11910 61 74 69 6f 6e 20 6f 66 20 5b 53 51 4c 49 54 45  ation of [SQLITE
11920 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 2e  _CONFIG_MALLOC].
11930 20 20 49 66 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f    If the.** memo
11940 72 79 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f  ry pointer is no
11950 74 20 4e 55 4c 4c 20 61 6e 64 20 65 69 74 68 65  t NULL and eithe
11960 72 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  r [SQLITE_ENABLE
11970 5f 4d 45 4d 53 59 53 33 5d 20 6f 72 0a 2a 2a 20  _MEMSYS3] or.** 
11980 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d  [SQLITE_ENABLE_M
11990 45 4d 53 59 53 35 5d 20 61 72 65 20 64 65 66 69  EMSYS5] are defi
119a0 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20 61 6c  ned, then the al
119b0 74 65 72 6e 61 74 69 76 65 20 6d 65 6d 6f 72 79  ternative memory
119c0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73  .** allocator is
119d0 20 65 6e 67 61 67 65 64 20 74 6f 20 68 61 6e 64   engaged to hand
119e0 6c 65 20 61 6c 6c 20 6f 66 20 53 51 4c 69 74 65  le all of SQLite
119f0 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  s memory allocat
11a00 69 6f 6e 20 6e 65 65 64 73 2e 0a 2a 2a 20 54 68  ion needs..** Th
11a10 65 20 66 69 72 73 74 20 70 6f 69 6e 74 65 72 20  e first pointer 
11a20 28 74 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e  (the memory poin
11a30 74 65 72 29 20 6d 75 73 74 20 62 65 20 61 6c 69  ter) must be ali
11a40 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 79 74  gned to an 8-byt
11a50 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79 20 6f 72  e.** boundary or
11a60 20 73 75 62 73 65 71 75 65 6e 74 20 62 65 68 61   subsequent beha
11a70 76 69 6f 72 20 6f 66 20 53 51 4c 69 74 65 20 77  vior of SQLite w
11a80 69 6c 6c 20 62 65 20 75 6e 64 65 66 69 6e 65 64  ill be undefined
11a90 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
11aa0 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  >SQLITE_CONFIG_M
11ab0 55 54 45 58 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  UTEX</dt>.** <dd
11ac0 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b  >This option tak
11ad0 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75  es a single argu
11ae0 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 20  ment which is a 
11af0 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a  pointer to an.**
11b00 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
11b10 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f   [sqlite3_mutex_
11b20 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74 75  methods] structu
11b30 72 65 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e  re.  The argumen
11b40 74 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 61  t specifies.** a
11b50 6c 74 65 72 6e 61 74 69 76 65 20 6c 6f 77 2d 6c  lternative low-l
11b60 65 76 65 6c 20 6d 75 74 65 78 20 72 6f 75 74 69  evel mutex routi
11b70 6e 65 73 20 74 6f 20 62 65 20 75 73 65 64 20 69  nes to be used i
11b80 6e 20 70 6c 61 63 65 0a 2a 2a 20 74 68 65 20 6d  n place.** the m
11b90 75 74 65 78 20 72 6f 75 74 69 6e 65 73 20 62 75  utex routines bu
11ba0 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74 65 2e  ilt into SQLite.
11bb0 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
11bc0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45  SQLITE_CONFIG_GE
11bd0 54 4d 55 54 45 58 3c 2f 64 74 3e 0a 2a 2a 20 3c  TMUTEX</dt>.** <
11be0 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74  dd>This option t
11bf0 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72  akes a single ar
11c00 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20  gument which is 
11c10 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a  a pointer to an.
11c20 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ** instance of t
11c30 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  he [sqlite3_mute
11c40 78 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63  x_methods] struc
11c50 74 75 72 65 2e 20 20 54 68 65 0a 2a 2a 20 5b 73  ture.  The.** [s
11c60 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
11c70 68 6f 64 73 5d 0a 2a 2a 20 73 74 72 75 63 74 75  hods].** structu
11c80 72 65 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74  re is filled wit
11c90 68 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20  h the currently 
11ca0 64 65 66 69 6e 65 64 20 6d 75 74 65 78 20 72 6f  defined mutex ro
11cb0 75 74 69 6e 65 73 2e 0a 2a 2a 20 54 68 69 73 20  utines..** This 
11cc0 6f 70 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73  option can be us
11cd0 65 64 20 74 6f 20 6f 76 65 72 6c 6f 61 64 20 74  ed to overload t
11ce0 68 65 20 64 65 66 61 75 6c 74 20 6d 75 74 65 78  he default mutex
11cf0 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72   allocation.** r
11d00 6f 75 74 69 6e 65 73 20 77 69 74 68 20 61 20 77  outines with a w
11d10 72 61 70 70 65 72 20 75 73 65 64 20 74 6f 20 74  rapper used to t
11d20 72 61 63 6b 20 6d 75 74 65 78 20 75 73 61 67 65  rack mutex usage
11d30 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65   for performance
11d40 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 20 6f 72  .** profiling or
11d50 20 74 65 73 74 69 6e 67 2c 20 66 6f 72 20 65 78   testing, for ex
11d60 61 6d 70 6c 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  ample.</dd>.**.*
11d70 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  * <dt>SQLITE_CON
11d80 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3c 2f 64  FIG_LOOKASIDE</d
11d90 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f  t>.** <dd>This o
11da0 70 74 69 6f 6e 20 74 61 6b 65 73 20 74 77 6f 20  ption takes two 
11db0 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 64  arguments that d
11dc0 65 74 65 72 6d 69 6e 65 20 74 68 65 20 64 65 66  etermine the def
11dd0 61 75 6c 74 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61  ault.** memory a
11de0 6c 6c 6f 63 61 74 69 6f 6e 20 6c 6f 6f 6b 61 73  llocation lookas
11df0 69 64 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ide optimization
11e00 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67  .  The first arg
11e10 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20  ument is the.** 
11e20 73 69 7a 65 20 6f 66 20 65 61 63 68 20 6c 6f 6f  size of each loo
11e30 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 73 6c  kaside buffer sl
11e40 6f 74 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e  ot and the secon
11e50 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  d is the number 
11e60 6f 66 0a 2a 2a 20 73 6c 6f 74 73 20 61 6c 6c 6f  of.** slots allo
11e70 63 61 74 65 64 20 74 6f 20 65 61 63 68 20 64 61  cated to each da
11e80 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
11e90 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20  n.  This option 
11ea0 73 65 74 73 20 74 68 65 0a 2a 2a 20 3c 69 3e 64  sets the.** <i>d
11eb0 65 66 61 75 6c 74 3c 2f 69 3e 20 6c 6f 6f 6b 61  efault</i> looka
11ec0 73 69 64 65 20 73 69 7a 65 2e 20 20 54 68 65 20  side size.  The 
11ed0 5b 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47  [SQLITE_DBCONFIG
11ee0 5f 4c 4f 4f 4b 41 53 49 44 45 5d 0a 2a 2a 20 76  _LOOKASIDE].** v
11ef0 65 72 62 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  erb to [sqlite3_
11f00 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 63 61 6e  db_config()] can
11f10 20 62 65 20 75 73 65 64 20 74 6f 20 63 68 61 6e   be used to chan
11f20 67 65 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65  ge the lookaside
11f30 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  .** configuratio
11f40 6e 20 6f 6e 20 69 6e 64 69 76 69 64 75 61 6c 20  n on individual 
11f50 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 3c 2f 64 64  connections.</dd
11f60 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
11f70 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45  TE_CONFIG_PCACHE
11f80 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
11f90 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61  s option takes a
11fa0 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74   single argument
11fb0 20 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e   which is a poin
11fc0 74 65 72 20 74 6f 0a 2a 2a 20 61 6e 20 5b 73 71  ter to.** an [sq
11fd0 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74  lite3_pcache_met
11fe0 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 20 20 54  hods] object.  T
11ff0 68 69 73 20 6f 62 6a 65 63 74 20 73 70 65 63 69  his object speci
12000 66 69 65 73 20 74 68 65 20 69 6e 74 65 72 66 61  fies the interfa
12010 63 65 0a 2a 2a 20 74 6f 20 61 20 63 75 73 74 6f  ce.** to a custo
12020 6d 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70  m page cache imp
12030 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 53 51  lementation.  SQ
12040 4c 69 74 65 20 6d 61 6b 65 73 20 61 20 63 6f 70  Lite makes a cop
12050 79 20 6f 66 20 74 68 65 0a 2a 2a 20 6f 62 6a 65  y of the.** obje
12060 63 74 20 61 6e 64 20 75 73 65 73 20 69 74 20 66  ct and uses it f
12070 6f 72 20 70 61 67 65 20 63 61 63 68 65 20 6d 65  or page cache me
12080 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  mory allocations
12090 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
120a0 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47  >SQLITE_CONFIG_G
120b0 45 54 50 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a  ETPCACHE</dt>.**
120c0 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e   <dd>This option
120d0 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20   takes a single 
120e0 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69  argument which i
120f0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
12100 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 63  n.** [sqlite3_pc
12110 61 63 68 65 5f 6d 65 74 68 6f 64 73 5d 20 6f 62  ache_methods] ob
12120 6a 65 63 74 2e 20 20 53 51 4c 69 74 65 20 63 6f  ject.  SQLite co
12130 70 69 65 73 20 6f 66 20 74 68 65 20 63 75 72 72  pies of the curr
12140 65 6e 74 0a 2a 2a 20 70 61 67 65 20 63 61 63 68  ent.** page cach
12150 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
12160 20 69 6e 74 6f 20 74 68 61 74 20 6f 62 6a 65 63   into that objec
12170 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f  t.</dd>.**.** </
12180 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  dl>.*/.#define S
12190 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e  QLITE_CONFIG_SIN
121a0 47 4c 45 54 48 52 45 41 44 20 20 31 20 20 2f 2a  GLETHREAD  1  /*
121b0 20 6e 69 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20   nil */.#define 
121c0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55  SQLITE_CONFIG_MU
121d0 4c 54 49 54 48 52 45 41 44 20 20 20 32 20 20 2f  LTITHREAD   2  /
121e0 2a 20 6e 69 6c 20 2a 2f 0a 23 64 65 66 69 6e 65  * nil */.#define
121f0 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53   SQLITE_CONFIG_S
12200 45 52 49 41 4c 49 5a 45 44 20 20 20 20 33 20 20  ERIALIZED    3  
12210 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 64 65 66 69 6e  /* nil */.#defin
12220 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
12230 4d 41 4c 4c 4f 43 20 20 20 20 20 20 20 20 34 20  MALLOC        4 
12240 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f   /* sqlite3_mem_
12250 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66  methods* */.#def
12260 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ine SQLITE_CONFI
12270 47 5f 47 45 54 4d 41 4c 4c 4f 43 20 20 20 20 20  G_GETMALLOC     
12280 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 65  5  /* sqlite3_me
12290 6d 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64  m_methods* */.#d
122a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
122b0 46 49 47 5f 53 43 52 41 54 43 48 20 20 20 20 20  FIG_SCRATCH     
122c0 20 20 36 20 20 2f 2a 20 76 6f 69 64 2a 2c 20 69    6  /* void*, i
122d0 6e 74 20 73 7a 2c 20 69 6e 74 20 4e 20 2a 2f 0a  nt sz, int N */.
122e0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
122f0 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 20  ONFIG_PAGECACHE 
12300 20 20 20 20 37 20 20 2f 2a 20 76 6f 69 64 2a 2c      7  /* void*,
12310 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 4e 20 2a   int sz, int N *
12320 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
12330 5f 43 4f 4e 46 49 47 5f 48 45 41 50 20 20 20 20  _CONFIG_HEAP    
12340 20 20 20 20 20 20 38 20 20 2f 2a 20 76 6f 69 64        8  /* void
12350 2a 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 69 6e  *, int nByte, in
12360 74 20 6d 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65  t min */.#define
12370 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d   SQLITE_CONFIG_M
12380 45 4d 53 54 41 54 55 53 20 20 20 20 20 39 20 20  EMSTATUS     9  
12390 2f 2a 20 62 6f 6f 6c 65 61 6e 20 2a 2f 0a 23 64  /* boolean */.#d
123a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
123b0 46 49 47 5f 4d 55 54 45 58 20 20 20 20 20 20 20  FIG_MUTEX       
123c0 20 31 30 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f   10  /* sqlite3_
123d0 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 2a 20 2a  mutex_methods* *
123e0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
123f0 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45 58  _CONFIG_GETMUTEX
12400 20 20 20 20 20 31 31 20 20 2f 2a 20 73 71 6c 69       11  /* sqli
12410 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
12420 73 2a 20 2a 2f 0a 2f 2a 20 70 72 65 76 69 6f 75  s* */./* previou
12430 73 6c 79 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  sly SQLITE_CONFI
12440 47 5f 43 48 55 4e 4b 41 4c 4c 4f 43 20 31 32 20  G_CHUNKALLOC 12 
12450 77 68 69 63 68 20 69 73 20 6e 6f 77 20 75 6e 75  which is now unu
12460 73 65 64 2e 20 2a 2f 20 0a 23 64 65 66 69 6e 65  sed. */ .#define
12470 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c   SQLITE_CONFIG_L
12480 4f 4f 4b 41 53 49 44 45 20 20 20 20 31 33 20 20  OOKASIDE    13  
12490 2f 2a 20 69 6e 74 20 69 6e 74 20 2a 2f 0a 23 64  /* int int */.#d
124a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
124b0 46 49 47 5f 50 43 41 43 48 45 20 20 20 20 20 20  FIG_PCACHE      
124c0 20 31 34 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f   14  /* sqlite3_
124d0 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a 20  pcache_methods* 
124e0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
124f0 45 5f 43 4f 4e 46 49 47 5f 47 45 54 50 43 41 43  E_CONFIG_GETPCAC
12500 48 45 20 20 20 20 31 35 20 20 2f 2a 20 73 71 6c  HE    15  /* sql
12510 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68  ite3_pcache_meth
12520 6f 64 73 2a 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  ods* */../*.** C
12530 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75  API3REF: Configu
12540 72 61 74 69 6f 6e 20 4f 70 74 69 6f 6e 73 20 7b  ration Options {
12550 48 31 30 31 37 30 7d 20 3c 53 32 30 30 30 30 3e  H10170} <S20000>
12560 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
12570 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e  .**.** These con
12580 73 74 61 6e 74 73 20 61 72 65 20 74 68 65 20 61  stants are the a
12590 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 67 65 72  vailable integer
125a0 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f   configuration o
125b0 70 74 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 63  ptions that.** c
125c0 61 6e 20 62 65 20 70 61 73 73 65 64 20 61 73 20  an be passed as 
125d0 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
125e0 65 6e 74 20 74 6f 20 74 68 65 20 5b 73 71 6c 69  ent to the [sqli
125f0 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d  te3_db_config()]
12600 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a   interface..**.*
12610 2a 20 4e 65 77 20 63 6f 6e 66 69 67 75 72 61 74  * New configurat
12620 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d 61 79 20  ion options may 
12630 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75  be added in futu
12640 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53  re releases of S
12650 51 4c 69 74 65 2e 0a 2a 2a 20 45 78 69 73 74 69  QLite..** Existi
12660 6e 67 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  ng configuration
12670 20 6f 70 74 69 6f 6e 73 20 6d 69 67 68 74 20 62   options might b
12680 65 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 2e 20  e discontinued. 
12690 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a   Applications.**
126a0 20 73 68 6f 75 6c 64 20 63 68 65 63 6b 20 74 68   should check th
126b0 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72  e return code fr
126c0 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63  om [sqlite3_db_c
126d0 6f 6e 66 69 67 28 29 5d 20 74 6f 20 6d 61 6b 65  onfig()] to make
126e0 20 73 75 72 65 20 74 68 61 74 0a 2a 2a 20 74 68   sure that.** th
126f0 65 20 63 61 6c 6c 20 77 6f 72 6b 65 64 2e 20 20  e call worked.  
12700 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f  The [sqlite3_db_
12710 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66  config()] interf
12720 61 63 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  ace will return 
12730 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 5b 65  a.** non-zero [e
12740 72 72 6f 72 20 63 6f 64 65 5d 20 69 66 20 61 20  rror code] if a 
12750 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f 72 20  discontinued or 
12760 75 6e 73 75 70 70 6f 72 74 65 64 20 63 6f 6e 66  unsupported conf
12770 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e  iguration option
12780 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a  .** is invoked..
12790 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64  **.** <dl>.** <d
127a0 74 3e 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49  t>SQLITE_DBCONFI
127b0 47 5f 4c 4f 4f 4b 41 53 49 44 45 3c 2f 64 74 3e  G_LOOKASIDE</dt>
127c0 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74  .** <dd>This opt
127d0 69 6f 6e 20 74 61 6b 65 73 20 74 68 72 65 65 20  ion takes three 
127e0 61 64 64 69 74 69 6f 6e 61 6c 20 61 72 67 75 6d  additional argum
127f0 65 6e 74 73 20 74 68 61 74 20 64 65 74 65 72 6d  ents that determ
12800 69 6e 65 20 74 68 65 20 0a 2a 2a 20 5b 6c 6f 6f  ine the .** [loo
12810 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c  kaside memory al
12820 6c 6f 63 61 74 6f 72 5d 20 63 6f 6e 66 69 67 75  locator] configu
12830 72 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 5b  ration for the [
12840 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
12850 69 6f 6e 5d 2e 0a 2a 2a 20 54 68 65 20 66 69 72  ion]..** The fir
12860 73 74 20 61 72 67 75 6d 65 6e 74 20 28 74 68 65  st argument (the
12870 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72   third parameter
12880 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f   to [sqlite3_db_
12890 63 6f 6e 66 69 67 28 29 5d 20 69 73 20 61 0a 2a  config()] is a.*
128a0 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  * pointer to an 
128b0 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 6f  memory buffer to
128c0 20 75 73 65 20 66 6f 72 20 6c 6f 6f 6b 61 73 69   use for lookasi
128d0 64 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54 68  de memory..** Th
128e0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
128f0 20 6d 61 79 20 62 65 20 4e 55 4c 4c 20 69 6e 20   may be NULL in 
12900 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 69 74  which case SQLit
12910 65 20 77 69 6c 6c 20 61 6c 6c 6f 63 61 74 65 20  e will allocate 
12920 74 68 65 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65  the.** lookaside
12930 20 62 75 66 66 65 72 20 69 74 73 65 6c 66 20 75   buffer itself u
12940 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61  sing [sqlite3_ma
12950 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 65 20 73 65  lloc()].  The se
12960 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
12970 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20   the.** size of 
12980 65 61 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20 62  each lookaside b
12990 75 66 66 65 72 20 73 6c 6f 74 20 61 6e 64 20 74  uffer slot and t
129a0 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
129b0 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  t is the number 
129c0 6f 66 0a 2a 2a 20 73 6c 6f 74 73 2e 20 20 54 68  of.** slots.  Th
129d0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75  e size of the bu
129e0 66 66 65 72 20 69 6e 20 74 68 65 20 66 69 72 73  ffer in the firs
129f0 74 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 20  t argument must 
12a00 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a  be greater than.
12a10 2a 2a 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74  ** or equal to t
12a20 68 65 20 70 72 6f 64 75 63 74 20 6f 66 20 74 68  he product of th
12a30 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 68 69  e second and thi
12a40 72 64 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 54  rd arguments.  T
12a50 68 65 20 62 75 66 66 65 72 0a 2a 2a 20 6d 75 73  he buffer.** mus
12a60 74 20 62 65 20 61 6c 69 67 6e 65 64 20 74 6f 20  t be aligned to 
12a70 61 6e 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61  an 8-byte bounda
12a80 72 79 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f  ry.  If the seco
12a90 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e  nd argument is n
12aa0 6f 74 0a 2a 2a 20 61 20 6d 75 6c 74 69 70 6c 65  ot.** a multiple
12ab0 20 6f 66 20 38 2c 20 69 74 20 69 73 20 69 6e 74   of 8, it is int
12ac0 65 72 6e 61 6c 6c 79 20 72 6f 75 6e 64 65 64 20  ernally rounded 
12ad0 64 6f 77 6e 20 74 6f 20 74 68 65 20 6e 65 78 74  down to the next
12ae0 20 73 6d 61 6c 6c 65 72 0a 2a 2a 20 6d 75 6c 74   smaller.** mult
12af0 69 70 6c 65 20 6f 66 20 38 2e 20 20 53 65 65 20  iple of 8.  See 
12b00 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 43 4f  also: [SQLITE_CO
12b10 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d 3c  NFIG_LOOKASIDE]<
12b20 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e  /dd>.**.** </dl>
12b30 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
12b40 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b  TE_DBCONFIG_LOOK
12b50 41 53 49 44 45 20 20 20 20 31 30 30 31 20 20 2f  ASIDE    1001  /
12b60 2a 20 76 6f 69 64 2a 20 69 6e 74 20 69 6e 74 20  * void* int int 
12b70 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  */.../*.** CAPI3
12b80 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 20 44  REF: Enable Or D
12b90 69 73 61 62 6c 65 20 45 78 74 65 6e 64 65 64 20  isable Extended 
12ba0 52 65 73 75 6c 74 20 43 6f 64 65 73 20 7b 48 31  Result Codes {H1
12bb0 32 32 30 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a  2200} <S10700>.*
12bc0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
12bd0 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74  _extended_result
12be0 5f 63 6f 64 65 73 28 29 20 72 6f 75 74 69 6e 65  _codes() routine
12bf0 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 69 73 61   enables or disa
12c00 62 6c 65 73 20 74 68 65 0a 2a 2a 20 5b 65 78 74  bles the.** [ext
12c10 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64  ended result cod
12c20 65 73 5d 20 66 65 61 74 75 72 65 20 6f 66 20 53  es] feature of S
12c30 51 4c 69 74 65 2e 20 54 68 65 20 65 78 74 65 6e  QLite. The exten
12c40 64 65 64 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f  ded result.** co
12c50 64 65 73 20 61 72 65 20 64 69 73 61 62 6c 65 64  des are disabled
12c60 20 62 79 20 64 65 66 61 75 6c 74 20 66 6f 72 20   by default for 
12c70 68 69 73 74 6f 72 69 63 61 6c 20 63 6f 6d 70 61  historical compa
12c80 74 69 62 69 6c 69 74 79 20 63 6f 6e 73 69 64 65  tibility conside
12c90 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 52  rations..**.** R
12ca0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
12cb0 5b 48 31 32 32 30 31 5d 20 5b 48 31 32 32 30 32  [H12201] [H12202
12cc0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
12cd0 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65  int sqlite3_exte
12ce0 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65  nded_result_code
12cf0 73 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20  s(sqlite3*, int 
12d00 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  onoff);../*.** C
12d10 41 50 49 33 52 45 46 3a 20 4c 61 73 74 20 49 6e  API3REF: Last In
12d20 73 65 72 74 20 52 6f 77 69 64 20 7b 48 31 32 32  sert Rowid {H122
12d30 32 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a  20} <S10700>.**.
12d40 2a 2a 20 45 61 63 68 20 65 6e 74 72 79 20 69 6e  ** Each entry in
12d50 20 61 6e 20 53 51 4c 69 74 65 20 74 61 62 6c 65   an SQLite table
12d60 20 68 61 73 20 61 20 75 6e 69 71 75 65 20 36 34   has a unique 64
12d70 2d 62 69 74 20 73 69 67 6e 65 64 0a 2a 2a 20 69  -bit signed.** i
12d80 6e 74 65 67 65 72 20 6b 65 79 20 63 61 6c 6c 65  nteger key calle
12d90 64 20 74 68 65 20 5b 52 4f 57 49 44 20 7c 20 22  d the [ROWID | "
12da0 72 6f 77 69 64 22 5d 2e 20 54 68 65 20 72 6f 77  rowid"]. The row
12db0 69 64 20 69 73 20 61 6c 77 61 79 73 20 61 76 61  id is always ava
12dc0 69 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 61 6e 20  ilable.** as an 
12dd0 75 6e 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d  undeclared colum
12de0 6e 20 6e 61 6d 65 64 20 52 4f 57 49 44 2c 20 4f  n named ROWID, O
12df0 49 44 2c 20 6f 72 20 5f 52 4f 57 49 44 5f 20 61  ID, or _ROWID_ a
12e00 73 20 6c 6f 6e 67 20 61 73 20 74 68 6f 73 65 0a  s long as those.
12e10 2a 2a 20 6e 61 6d 65 73 20 61 72 65 20 6e 6f 74  ** names are not
12e20 20 61 6c 73 6f 20 75 73 65 64 20 62 79 20 65 78   also used by ex
12e30 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65  plicitly declare
12e40 64 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 0a 2a 2a  d columns. If.**
12e50 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61   the table has a
12e60 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 79 70 65 20   column of type 
12e70 5b 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59  [INTEGER PRIMARY
12e80 20 4b 45 59 5d 20 74 68 65 6e 20 74 68 61 74 20   KEY] then that 
12e90 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 61 6e 6f  column.** is ano
12ea0 74 68 65 72 20 61 6c 69 61 73 20 66 6f 72 20 74  ther alias for t
12eb0 68 65 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20  he rowid..**.** 
12ec0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
12ed0 75 72 6e 73 20 74 68 65 20 5b 72 6f 77 69 64 5d  urns the [rowid]
12ee0 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
12ef0 65 6e 74 0a 2a 2a 20 73 75 63 63 65 73 73 66 75  ent.** successfu
12f00 6c 20 5b 49 4e 53 45 52 54 5d 20 69 6e 74 6f 20  l [INSERT] into 
12f10 74 68 65 20 64 61 74 61 62 61 73 65 20 66 72 6f  the database fro
12f20 6d 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  m the [database 
12f30 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 69  connection].** i
12f40 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  n the first argu
12f50 6d 65 6e 74 2e 20 20 49 66 20 6e 6f 20 73 75 63  ment.  If no suc
12f60 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54 5d  cessful [INSERT]
12f70 73 0a 2a 2a 20 68 61 76 65 20 65 76 65 72 20 6f  s.** have ever o
12f80 63 63 75 72 72 65 64 20 6f 6e 20 74 68 61 74 20  ccurred on that 
12f90 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
12fa0 69 6f 6e 2c 20 7a 65 72 6f 20 69 73 20 72 65 74  ion, zero is ret
12fb0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  urned..**.** If 
12fc0 61 6e 20 5b 49 4e 53 45 52 54 5d 20 6f 63 63 75  an [INSERT] occu
12fd0 72 73 20 77 69 74 68 69 6e 20 61 20 74 72 69 67  rs within a trig
12fe0 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20 5b 72  ger, then the [r
12ff0 6f 77 69 64 5d 20 6f 66 20 74 68 65 20 69 6e 73  owid] of the ins
13000 65 72 74 65 64 0a 2a 2a 20 72 6f 77 20 69 73 20  erted.** row is 
13010 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73  returned by this
13020 20 72 6f 75 74 69 6e 65 20 61 73 20 6c 6f 6e 67   routine as long
13030 20 61 73 20 74 68 65 20 74 72 69 67 67 65 72 20   as the trigger 
13040 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 20 42  is running..** B
13050 75 74 20 6f 6e 63 65 20 74 68 65 20 74 72 69 67  ut once the trig
13060 67 65 72 20 74 65 72 6d 69 6e 61 74 65 73 2c 20  ger terminates, 
13070 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
13080 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69  ed by this routi
13090 6e 65 0a 2a 2a 20 72 65 76 65 72 74 73 20 74 6f  ne.** reverts to
130a0 20 74 68 65 20 6c 61 73 74 20 76 61 6c 75 65 20   the last value 
130b0 69 6e 73 65 72 74 65 64 20 62 65 66 6f 72 65 20  inserted before 
130c0 74 68 65 20 74 72 69 67 67 65 72 20 66 69 72 65  the trigger fire
130d0 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 5b 49 4e 53  d..**.** An [INS
130e0 45 52 54 5d 20 74 68 61 74 20 66 61 69 6c 73 20  ERT] that fails 
130f0 64 75 65 20 74 6f 20 61 20 63 6f 6e 73 74 72 61  due to a constra
13100 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 69 73  int violation is
13110 20 6e 6f 74 20 61 0a 2a 2a 20 73 75 63 63 65 73   not a.** succes
13120 73 66 75 6c 20 5b 49 4e 53 45 52 54 5d 20 61 6e  sful [INSERT] an
13130 64 20 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67  d does not chang
13140 65 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  e the value retu
13150 72 6e 65 64 20 62 79 20 74 68 69 73 0a 2a 2a 20  rned by this.** 
13160 72 6f 75 74 69 6e 65 2e 20 20 54 68 75 73 20 49  routine.  Thus I
13170 4e 53 45 52 54 20 4f 52 20 46 41 49 4c 2c 20 49  NSERT OR FAIL, I
13180 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 2c  NSERT OR IGNORE,
13190 20 49 4e 53 45 52 54 20 4f 52 20 52 4f 4c 4c 42   INSERT OR ROLLB
131a0 41 43 4b 2c 0a 2a 2a 20 61 6e 64 20 49 4e 53 45  ACK,.** and INSE
131b0 52 54 20 4f 52 20 41 42 4f 52 54 20 6d 61 6b 65  RT OR ABORT make
131c0 20 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f 20 74   no changes to t
131d0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
131e0 6f 66 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69  of this.** routi
131f0 6e 65 20 77 68 65 6e 20 74 68 65 69 72 20 69 6e  ne when their in
13200 73 65 72 74 69 6f 6e 20 66 61 69 6c 73 2e 20 20  sertion fails.  
13210 57 68 65 6e 20 49 4e 53 45 52 54 20 4f 52 20 52  When INSERT OR R
13220 45 50 4c 41 43 45 0a 2a 2a 20 65 6e 63 6f 75 6e  EPLACE.** encoun
13230 74 65 72 73 20 61 20 63 6f 6e 73 74 72 61 69 6e  ters a constrain
13240 74 20 76 69 6f 6c 61 74 69 6f 6e 2c 20 69 74 20  t violation, it 
13250 64 6f 65 73 20 6e 6f 74 20 66 61 69 6c 2e 20 20  does not fail.  
13260 54 68 65 0a 2a 2a 20 49 4e 53 45 52 54 20 63 6f  The.** INSERT co
13270 6e 74 69 6e 75 65 73 20 74 6f 20 63 6f 6d 70 6c  ntinues to compl
13280 65 74 69 6f 6e 20 61 66 74 65 72 20 64 65 6c 65  etion after dele
13290 74 69 6e 67 20 72 6f 77 73 20 74 68 61 74 20 63  ting rows that c
132a0 61 75 73 65 64 0a 2a 2a 20 74 68 65 20 63 6f 6e  aused.** the con
132b0 73 74 72 61 69 6e 74 20 70 72 6f 62 6c 65 6d 20  straint problem 
132c0 73 6f 20 49 4e 53 45 52 54 20 4f 52 20 52 45 50  so INSERT OR REP
132d0 4c 41 43 45 20 77 69 6c 6c 20 61 6c 77 61 79 73  LACE will always
132e0 20 63 68 61 6e 67 65 0a 2a 2a 20 74 68 65 20 72   change.** the r
132f0 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74  eturn value of t
13300 68 69 73 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a  his interface..*
13310 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72  *.** For the pur
13320 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20 72 6f  poses of this ro
13330 75 74 69 6e 65 2c 20 61 6e 20 5b 49 4e 53 45 52  utine, an [INSER
13340 54 5d 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  T] is considered
13350 20 74 6f 0a 2a 2a 20 62 65 20 73 75 63 63 65 73   to.** be succes
13360 73 66 75 6c 20 65 76 65 6e 20 69 66 20 69 74 20  sful even if it 
13370 69 73 20 73 75 62 73 65 71 75 65 6e 74 6c 79 20  is subsequently 
13380 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a  rolled back..**.
13390 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
133a0 0a 2a 2a 20 5b 48 31 32 32 32 31 5d 20 5b 48 31  .** [H12221] [H1
133b0 32 32 32 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61  2223].**.** If a
133c0 20 73 65 70 61 72 61 74 65 20 74 68 72 65 61 64   separate thread
133d0 20 70 65 72 66 6f 72 6d 73 20 61 20 6e 65 77 20   performs a new 
133e0 5b 49 4e 53 45 52 54 5d 20 6f 6e 20 74 68 65 20  [INSERT] on the 
133f0 73 61 6d 65 0a 2a 2a 20 64 61 74 61 62 61 73 65  same.** database
13400 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c   connection whil
13410 65 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c  e the [sqlite3_l
13420 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64  ast_insert_rowid
13430 28 29 5d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  ()].** function 
13440 69 73 20 72 75 6e 6e 69 6e 67 20 61 6e 64 20 74  is running and t
13450 68 75 73 20 63 68 61 6e 67 65 73 20 74 68 65 20  hus changes the 
13460 6c 61 73 74 20 69 6e 73 65 72 74 20 5b 72 6f 77  last insert [row
13470 69 64 5d 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65  id],.** then the
13480 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
13490 62 79 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74  by [sqlite3_last
134a0 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 5d  _insert_rowid()]
134b0 20 69 73 0a 2a 2a 20 75 6e 70 72 65 64 69 63 74   is.** unpredict
134c0 61 62 6c 65 20 61 6e 64 20 6d 69 67 68 74 20 6e  able and might n
134d0 6f 74 20 65 71 75 61 6c 20 65 69 74 68 65 72 20  ot equal either 
134e0 74 68 65 20 6f 6c 64 20 6f 72 20 74 68 65 20 6e  the old or the n
134f0 65 77 0a 2a 2a 20 6c 61 73 74 20 69 6e 73 65 72  ew.** last inser
13500 74 20 5b 72 6f 77 69 64 5d 2e 0a 2a 2f 0a 53 51  t [rowid]..*/.SQ
13510 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
13520 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6c  _int64 sqlite3_l
13530 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64  ast_insert_rowid
13540 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a  (sqlite3*);../*.
13550 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 75  ** CAPI3REF: Cou
13560 6e 74 20 54 68 65 20 4e 75 6d 62 65 72 20 4f 66  nt The Number Of
13570 20 52 6f 77 73 20 4d 6f 64 69 66 69 65 64 20 7b   Rows Modified {
13580 48 31 32 32 34 30 7d 20 3c 53 31 30 36 30 30 3e  H12240} <S10600>
13590 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
135a0 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65  tion returns the
135b0 20 6e 75 6d 62 65 72 20 6f 66 20 64 61 74 61 62   number of datab
135c0 61 73 65 20 72 6f 77 73 20 74 68 61 74 20 77 65  ase rows that we
135d0 72 65 20 63 68 61 6e 67 65 64 0a 2a 2a 20 6f 72  re changed.** or
135e0 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c   inserted or del
135f0 65 74 65 64 20 62 79 20 74 68 65 20 6d 6f 73 74  eted by the most
13600 20 72 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c 65   recently comple
13610 74 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ted SQL statemen
13620 74 0a 2a 2a 20 6f 6e 20 74 68 65 20 5b 64 61 74  t.** on the [dat
13630 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
13640 5d 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74  ] specified by t
13650 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
13660 65 72 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 68 61 6e  er..** Only chan
13670 67 65 73 20 74 68 61 74 20 61 72 65 20 64 69 72  ges that are dir
13680 65 63 74 6c 79 20 73 70 65 63 69 66 69 65 64 20  ectly specified 
13690 62 79 20 74 68 65 20 5b 49 4e 53 45 52 54 5d 2c  by the [INSERT],
136a0 20 5b 55 50 44 41 54 45 5d 2c 0a 2a 2a 20 6f 72   [UPDATE],.** or
136b0 20 5b 44 45 4c 45 54 45 5d 20 73 74 61 74 65 6d   [DELETE] statem
136c0 65 6e 74 20 61 72 65 20 63 6f 75 6e 74 65 64 2e  ent are counted.
136d0 20 20 41 75 78 69 6c 69 61 72 79 20 63 68 61 6e    Auxiliary chan
136e0 67 65 73 20 63 61 75 73 65 64 20 62 79 0a 2a 2a  ges caused by.**
136f0 20 74 72 69 67 67 65 72 73 20 6f 72 20 5b 66 6f   triggers or [fo
13700 72 65 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e  reign key action
13710 73 5d 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74  s] are not count
13720 65 64 2e 20 55 73 65 20 74 68 65 0a 2a 2a 20 5b  ed. Use the.** [
13730 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68  sqlite3_total_ch
13740 61 6e 67 65 73 28 29 5d 20 66 75 6e 63 74 69 6f  anges()] functio
13750 6e 20 74 6f 20 66 69 6e 64 20 74 68 65 20 74 6f  n to find the to
13760 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  tal number of ch
13770 61 6e 67 65 73 0a 2a 2a 20 69 6e 63 6c 75 64 69  anges.** includi
13780 6e 67 20 63 68 61 6e 67 65 73 20 63 61 75 73 65  ng changes cause
13790 64 20 62 79 20 74 72 69 67 67 65 72 73 20 61 6e  d by triggers an
137a0 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63  d foreign key ac
137b0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43 68 61  tions..**.** Cha
137c0 6e 67 65 73 20 74 6f 20 61 20 76 69 65 77 20 74  nges to a view t
137d0 68 61 74 20 61 72 65 20 73 69 6d 75 6c 61 74 65  hat are simulate
137e0 64 20 62 79 20 61 6e 20 5b 49 4e 53 54 45 41 44  d by an [INSTEAD
137f0 20 4f 46 20 74 72 69 67 67 65 72 5d 0a 2a 2a 20   OF trigger].** 
13800 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 2e  are not counted.
13810 20 20 4f 6e 6c 79 20 72 65 61 6c 20 74 61 62 6c    Only real tabl
13820 65 20 63 68 61 6e 67 65 73 20 61 72 65 20 63 6f  e changes are co
13830 75 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 22  unted..**.** A "
13840 72 6f 77 20 63 68 61 6e 67 65 22 20 69 73 20 61  row change" is a
13850 20 63 68 61 6e 67 65 20 74 6f 20 61 20 73 69 6e   change to a sin
13860 67 6c 65 20 72 6f 77 20 6f 66 20 61 20 73 69 6e  gle row of a sin
13870 67 6c 65 20 74 61 62 6c 65 0a 2a 2a 20 63 61 75  gle table.** cau
13880 73 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52 54  sed by an INSERT
13890 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44  , DELETE, or UPD
138a0 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ATE statement.  
138b0 52 6f 77 73 20 74 68 61 74 0a 2a 2a 20 61 72 65  Rows that.** are
138c0 20 63 68 61 6e 67 65 64 20 61 73 20 73 69 64 65   changed as side
138d0 20 65 66 66 65 63 74 73 20 6f 66 20 5b 52 45 50   effects of [REP
138e0 4c 41 43 45 5d 20 63 6f 6e 73 74 72 61 69 6e 74  LACE] constraint
138f0 20 72 65 73 6f 6c 75 74 69 6f 6e 2c 0a 2a 2a 20   resolution,.** 
13900 72 6f 6c 6c 62 61 63 6b 2c 20 41 42 4f 52 54 20  rollback, ABORT 
13910 70 72 6f 63 65 73 73 69 6e 67 2c 20 5b 44 52 4f  processing, [DRO
13920 50 20 54 41 42 4c 45 5d 2c 20 6f 72 20 62 79 20  P TABLE], or by 
13930 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6d 65 63  any other.** mec
13940 68 61 6e 69 73 6d 73 20 64 6f 20 6e 6f 74 20 63  hanisms do not c
13950 6f 75 6e 74 20 61 73 20 64 69 72 65 63 74 20 72  ount as direct r
13960 6f 77 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 0a 2a  ow changes..**.*
13970 2a 20 41 20 22 74 72 69 67 67 65 72 20 63 6f 6e  * A "trigger con
13980 74 65 78 74 22 20 69 73 20 61 20 73 63 6f 70 65  text" is a scope
13990 20 6f 66 20 65 78 65 63 75 74 69 6f 6e 20 74 68   of execution th
139a0 61 74 20 62 65 67 69 6e 73 20 61 6e 64 0a 2a 2a  at begins and.**
139b0 20 65 6e 64 73 20 77 69 74 68 20 74 68 65 20 73   ends with the s
139c0 63 72 69 70 74 20 6f 66 20 61 20 5b 43 52 45 41  cript of a [CREA
139d0 54 45 20 54 52 49 47 47 45 52 20 7c 20 74 72 69  TE TRIGGER | tri
139e0 67 67 65 72 5d 2e 20 0a 2a 2a 20 4d 6f 73 74 20  gger]. .** Most 
139f0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61  SQL statements a
13a00 72 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20  re.** evaluated 
13a10 6f 75 74 73 69 64 65 20 6f 66 20 61 6e 79 20 74  outside of any t
13a20 72 69 67 67 65 72 2e 20 20 54 68 69 73 20 69 73  rigger.  This is
13a30 20 74 68 65 20 22 74 6f 70 20 6c 65 76 65 6c 22   the "top level"
13a40 0a 2a 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74  .** trigger cont
13a50 65 78 74 2e 20 20 49 66 20 61 20 74 72 69 67 67  ext.  If a trigg
13a60 65 72 20 66 69 72 65 73 20 66 72 6f 6d 20 74 68  er fires from th
13a70 65 20 74 6f 70 20 6c 65 76 65 6c 2c 20 61 0a 2a  e top level, a.*
13a80 2a 20 6e 65 77 20 74 72 69 67 67 65 72 20 63 6f  * new trigger co
13a90 6e 74 65 78 74 20 69 73 20 65 6e 74 65 72 65 64  ntext is entered
13aa0 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f   for the duratio
13ab0 6e 20 6f 66 20 74 68 61 74 20 6f 6e 65 0a 2a 2a  n of that one.**
13ac0 20 74 72 69 67 67 65 72 2e 20 20 53 75 62 74 72   trigger.  Subtr
13ad0 69 67 67 65 72 73 20 63 72 65 61 74 65 20 73 75  iggers create su
13ae0 62 63 6f 6e 74 65 78 74 73 20 66 6f 72 20 74 68  bcontexts for th
13af0 65 69 72 20 64 75 72 61 74 69 6f 6e 2e 0a 2a 2a  eir duration..**
13b00 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 5b 73 71 6c  .** Calling [sql
13b10 69 74 65 33 5f 65 78 65 63 28 29 5d 20 6f 72 20  ite3_exec()] or 
13b20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
13b30 20 72 65 63 75 72 73 69 76 65 6c 79 20 64 6f 65   recursively doe
13b40 73 0a 2a 2a 20 6e 6f 74 20 63 72 65 61 74 65 20  s.** not create 
13b50 61 20 6e 65 77 20 74 72 69 67 67 65 72 20 63 6f  a new trigger co
13b60 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ntext..**.** Thi
13b70 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
13b80 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
13b90 20 64 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e   direct row chan
13ba0 67 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6d 6f  ges in the.** mo
13bb0 73 74 20 72 65 63 65 6e 74 20 49 4e 53 45 52 54  st recent INSERT
13bc0 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c  , UPDATE, or DEL
13bd0 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 77 69  ETE statement wi
13be0 74 68 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a  thin the same.**
13bf0 20 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74   trigger context
13c00 2e 0a 2a 2a 0a 2a 2a 20 54 68 75 73 2c 20 77 68  ..**.** Thus, wh
13c10 65 6e 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74  en called from t
13c20 68 65 20 74 6f 70 20 6c 65 76 65 6c 2c 20 74 68  he top level, th
13c30 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
13c40 72 6e 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65  rns the.** numbe
13c50 72 20 6f 66 20 63 68 61 6e 67 65 73 20 69 6e 20  r of changes in 
13c60 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
13c70 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20  INSERT, UPDATE, 
13c80 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68 61  or DELETE.** tha
13c90 74 20 61 6c 73 6f 20 6f 63 63 75 72 72 65 64 20  t also occurred 
13ca0 61 74 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c  at the top level
13cb0 2e 20 20 57 69 74 68 69 6e 20 74 68 65 20 62 6f  .  Within the bo
13cc0 64 79 20 6f 66 20 61 20 74 72 69 67 67 65 72 2c  dy of a trigger,
13cd0 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f  .** the sqlite3_
13ce0 63 68 61 6e 67 65 73 28 29 20 69 6e 74 65 72 66  changes() interf
13cf0 61 63 65 20 63 61 6e 20 62 65 20 63 61 6c 6c 65  ace can be calle
13d00 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 75  d to find the nu
13d10 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 6e 67  mber of.** chang
13d20 65 73 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 72  es in the most r
13d30 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65  ecently complete
13d40 64 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45  d INSERT, UPDATE
13d50 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 73  , or DELETE.** s
13d60 74 61 74 65 6d 65 6e 74 20 77 69 74 68 69 6e 20  tatement within 
13d70 74 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20  the body of the 
13d80 73 61 6d 65 20 74 72 69 67 67 65 72 2e 0a 2a 2a  same trigger..**
13d90 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 6e 75   However, the nu
13da0 6d 62 65 72 20 72 65 74 75 72 6e 65 64 20 64 6f  mber returned do
13db0 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63  es not include c
13dc0 68 61 6e 67 65 73 0a 2a 2a 20 63 61 75 73 65 64  hanges.** caused
13dd0 20 62 79 20 73 75 62 74 72 69 67 67 65 72 73 20   by subtriggers 
13de0 73 69 6e 63 65 20 74 68 6f 73 65 20 68 61 76 65  since those have
13df0 20 74 68 65 69 72 20 6f 77 6e 20 63 6f 6e 74 65   their own conte
13e00 78 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  xt..**.** See al
13e10 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  so the [sqlite3_
13e20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d  total_changes()]
13e30 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64 20 74   interface and t
13e40 68 65 0a 2a 2a 20 5b 63 6f 75 6e 74 5f 63 68 61  he.** [count_cha
13e50 6e 67 65 73 20 70 72 61 67 6d 61 5d 2e 0a 2a 2a  nges pragma]..**
13e60 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
13e70 3a 0a 2a 2a 20 5b 48 31 32 32 34 31 5d 20 5b 48  :.** [H12241] [H
13e80 31 32 32 34 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20  12243].**.** If 
13e90 61 20 73 65 70 61 72 61 74 65 20 74 68 72 65 61  a separate threa
13ea0 64 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20  d makes changes 
13eb0 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  on the same data
13ec0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  base connection.
13ed0 2a 2a 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65  ** while [sqlite
13ee0 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 73 20  3_changes()] is 
13ef0 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20 74 68 65  running then the
13f00 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 0a   value returned.
13f10 2a 2a 20 69 73 20 75 6e 70 72 65 64 69 63 74 61  ** is unpredicta
13f20 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d 65 61 6e  ble and not mean
13f30 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54  ingful..*/.SQLIT
13f40 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
13f50 33 5f 63 68 61 6e 67 65 73 28 73 71 6c 69 74 65  3_changes(sqlite
13f60 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  3*);../*.** CAPI
13f70 33 52 45 46 3a 20 54 6f 74 61 6c 20 4e 75 6d 62  3REF: Total Numb
13f80 65 72 20 4f 66 20 52 6f 77 73 20 4d 6f 64 69 66  er Of Rows Modif
13f90 69 65 64 20 7b 48 31 32 32 36 30 7d 20 3c 53 31  ied {H12260} <S1
13fa0 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  0600>.**.** This
13fb0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
13fc0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
13fd0 72 6f 77 20 63 68 61 6e 67 65 73 20 63 61 75 73  row changes caus
13fe0 65 64 20 62 79 20 5b 49 4e 53 45 52 54 5d 2c 0a  ed by [INSERT],.
13ff0 2a 2a 20 5b 55 50 44 41 54 45 5d 20 6f 72 20 5b  ** [UPDATE] or [
14000 44 45 4c 45 54 45 5d 20 73 74 61 74 65 6d 65 6e  DELETE] statemen
14010 74 73 20 73 69 6e 63 65 20 74 68 65 20 5b 64 61  ts since the [da
14020 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
14030 6e 5d 20 77 61 73 20 6f 70 65 6e 65 64 2e 0a 2a  n] was opened..*
14040 2a 20 54 68 65 20 63 6f 75 6e 74 20 69 6e 63 6c  * The count incl
14050 75 64 65 73 20 61 6c 6c 20 63 68 61 6e 67 65 73  udes all changes
14060 20 66 72 6f 6d 20 61 6c 6c 20 5b 43 52 45 41 54   from all [CREAT
14070 45 20 54 52 49 47 47 45 52 20 7c 20 74 72 69 67  E TRIGGER | trig
14080 67 65 72 5d 20 0a 2a 2a 20 63 6f 6e 74 65 78 74  ger] .** context
14090 73 20 61 6e 64 20 63 68 61 6e 67 65 73 20 6d 61  s and changes ma
140a0 64 65 20 62 79 20 5b 66 6f 72 65 69 67 6e 20 6b  de by [foreign k
140b0 65 79 20 61 63 74 69 6f 6e 73 5d 2e 20 48 6f 77  ey actions]. How
140c0 65 76 65 72 2c 0a 2a 2a 20 74 68 65 20 63 6f 75  ever,.** the cou
140d0 6e 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c  nt does not incl
140e0 75 64 65 20 63 68 61 6e 67 65 73 20 75 73 65 64  ude changes used
140f0 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 5b 52   to implement [R
14100 45 50 4c 41 43 45 5d 20 63 6f 6e 73 74 72 61 69  EPLACE] constrai
14110 6e 74 73 2c 0a 2a 2a 20 64 6f 20 72 6f 6c 6c 62  nts,.** do rollb
14120 61 63 6b 73 20 6f 72 20 41 42 4f 52 54 20 70 72  acks or ABORT pr
14130 6f 63 65 73 73 69 6e 67 2c 20 6f 72 20 5b 44 52  ocessing, or [DR
14140 4f 50 20 54 41 42 4c 45 5d 20 70 72 6f 63 65 73  OP TABLE] proces
14150 73 69 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 63 6f  sing.  The.** co
14160 75 6e 74 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63  unt does not inc
14170 6c 75 64 65 20 72 6f 77 73 20 6f 66 20 76 69 65  lude rows of vie
14180 77 73 20 74 68 61 74 20 66 69 72 65 20 61 6e 20  ws that fire an 
14190 5b 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67  [INSTEAD OF trig
141a0 67 65 72 5d 2c 0a 2a 2a 20 74 68 6f 75 67 68 20  ger],.** though 
141b0 69 66 20 74 68 65 20 49 4e 53 54 45 41 44 20 4f  if the INSTEAD O
141c0 46 20 74 72 69 67 67 65 72 20 6d 61 6b 65 73 20  F trigger makes 
141d0 63 68 61 6e 67 65 73 20 6f 66 20 69 74 73 20 6f  changes of its o
141e0 77 6e 2c 20 74 68 6f 73 65 20 63 68 61 6e 67 65  wn, those change
141f0 73 20 0a 2a 2a 20 61 72 65 20 63 6f 75 6e 74 65  s .** are counte
14200 64 2e 0a 2a 2a 20 54 68 65 20 63 68 61 6e 67 65  d..** The change
14210 73 20 61 72 65 20 63 6f 75 6e 74 65 64 20 61 73  s are counted as
14220 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 73 74 61   soon as the sta
14230 74 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 6b 65  tement that make
14240 73 20 74 68 65 6d 20 69 73 0a 2a 2a 20 63 6f 6d  s them is.** com
14250 70 6c 65 74 65 64 20 28 77 68 65 6e 20 74 68 65  pleted (when the
14260 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c   statement handl
14270 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 5b  e is passed to [
14280 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
14290 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   or.** [sqlite3_
142a0 66 69 6e 61 6c 69 7a 65 28 29 5d 29 2e 0a 2a 2a  finalize()])..**
142b0 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65  .** See also the
142c0 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65   [sqlite3_change
142d0 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 61  s()] interface a
142e0 6e 64 20 74 68 65 0a 2a 2a 20 5b 63 6f 75 6e 74  nd the.** [count
142f0 5f 63 68 61 6e 67 65 73 20 70 72 61 67 6d 61 5d  _changes pragma]
14300 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
14310 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 36 31  ents:.** [H12261
14320 5d 20 5b 48 31 32 32 36 33 5d 0a 2a 2a 0a 2a 2a  ] [H12263].**.**
14330 20 49 66 20 61 20 73 65 70 61 72 61 74 65 20 74   If a separate t
14340 68 72 65 61 64 20 6d 61 6b 65 73 20 63 68 61 6e  hread makes chan
14350 67 65 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 20  ges on the same 
14360 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
14370 69 6f 6e 0a 2a 2a 20 77 68 69 6c 65 20 5b 73 71  ion.** while [sq
14380 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e  lite3_total_chan
14390 67 65 73 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e  ges()] is runnin
143a0 67 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  g then the value
143b0 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 73 20  .** returned is 
143c0 75 6e 70 72 65 64 69 63 74 61 62 6c 65 20 61 6e  unpredictable an
143d0 64 20 6e 6f 74 20 6d 65 61 6e 69 6e 67 66 75 6c  d not meaningful
143e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
143f0 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 6f 74 61  int sqlite3_tota
14400 6c 5f 63 68 61 6e 67 65 73 28 73 71 6c 69 74 65  l_changes(sqlite
14410 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  3*);../*.** CAPI
14420 33 52 45 46 3a 20 49 6e 74 65 72 72 75 70 74 20  3REF: Interrupt 
14430 41 20 4c 6f 6e 67 2d 52 75 6e 6e 69 6e 67 20 51  A Long-Running Q
14440 75 65 72 79 20 7b 48 31 32 32 37 30 7d 20 3c 53  uery {H12270} <S
14450 33 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  30500>.**.** Thi
14460 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 75 73 65  s function cause
14470 73 20 61 6e 79 20 70 65 6e 64 69 6e 67 20 64 61  s any pending da
14480 74 61 62 61 73 65 20 6f 70 65 72 61 74 69 6f 6e  tabase operation
14490 20 74 6f 20 61 62 6f 72 74 20 61 6e 64 0a 2a 2a   to abort and.**
144a0 20 72 65 74 75 72 6e 20 61 74 20 69 74 73 20 65   return at its e
144b0 61 72 6c 69 65 73 74 20 6f 70 70 6f 72 74 75 6e  arliest opportun
144c0 69 74 79 2e 20 54 68 69 73 20 72 6f 75 74 69 6e  ity. This routin
144d0 65 20 69 73 20 74 79 70 69 63 61 6c 6c 79 0a 2a  e is typically.*
144e0 2a 20 63 61 6c 6c 65 64 20 69 6e 20 72 65 73 70  * called in resp
144f0 6f 6e 73 65 20 74 6f 20 61 20 75 73 65 72 20 61  onse to a user a
14500 63 74 69 6f 6e 20 73 75 63 68 20 61 73 20 70 72  ction such as pr
14510 65 73 73 69 6e 67 20 22 43 61 6e 63 65 6c 22 0a  essing "Cancel".
14520 2a 2a 20 6f 72 20 43 74 72 6c 2d 43 20 77 68 65  ** or Ctrl-C whe
14530 72 65 20 74 68 65 20 75 73 65 72 20 77 61 6e 74  re the user want
14540 73 20 61 20 6c 6f 6e 67 20 71 75 65 72 79 20 6f  s a long query o
14550 70 65 72 61 74 69 6f 6e 20 74 6f 20 68 61 6c 74  peration to halt
14560 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e  .** immediately.
14570 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 73 61 66  .**.** It is saf
14580 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 72  e to call this r
14590 6f 75 74 69 6e 65 20 66 72 6f 6d 20 61 20 74 68  outine from a th
145a0 72 65 61 64 20 64 69 66 66 65 72 65 6e 74 20 66  read different f
145b0 72 6f 6d 20 74 68 65 0a 2a 2a 20 74 68 72 65 61  rom the.** threa
145c0 64 20 74 68 61 74 20 69 73 20 63 75 72 72 65 6e  d that is curren
145d0 74 6c 79 20 72 75 6e 6e 69 6e 67 20 74 68 65 20  tly running the 
145e0 64 61 74 61 62 61 73 65 20 6f 70 65 72 61 74 69  database operati
145f0 6f 6e 2e 20 20 42 75 74 20 69 74 0a 2a 2a 20 69  on.  But it.** i
14600 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 63 61  s not safe to ca
14610 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ll this routine 
14620 77 69 74 68 20 61 20 5b 64 61 74 61 62 61 73 65  with a [database
14630 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 68 61   connection] tha
14640 74 0a 2a 2a 20 69 73 20 63 6c 6f 73 65 64 20 6f  t.** is closed o
14650 72 20 6d 69 67 68 74 20 63 6c 6f 73 65 20 62 65  r might close be
14660 66 6f 72 65 20 73 71 6c 69 74 65 33 5f 69 6e 74  fore sqlite3_int
14670 65 72 72 75 70 74 28 29 20 72 65 74 75 72 6e 73  errupt() returns
14680 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 53 51  ..**.** If an SQ
14690 4c 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 76  L operation is v
146a0 65 72 79 20 6e 65 61 72 6c 79 20 66 69 6e 69 73  ery nearly finis
146b0 68 65 64 20 61 74 20 74 68 65 20 74 69 6d 65 20  hed at the time 
146c0 77 68 65 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  when.** sqlite3_
146d0 69 6e 74 65 72 72 75 70 74 28 29 20 69 73 20 63  interrupt() is c
146e0 61 6c 6c 65 64 2c 20 74 68 65 6e 20 69 74 20 6d  alled, then it m
146f0 69 67 68 74 20 6e 6f 74 20 68 61 76 65 20 61 6e  ight not have an
14700 20 6f 70 70 6f 72 74 75 6e 69 74 79 0a 2a 2a 20   opportunity.** 
14710 74 6f 20 62 65 20 69 6e 74 65 72 72 75 70 74 65  to be interrupte
14720 64 20 61 6e 64 20 6d 69 67 68 74 20 63 6f 6e 74  d and might cont
14730 69 6e 75 65 20 74 6f 20 63 6f 6d 70 6c 65 74 69  inue to completi
14740 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 53 51 4c  on..**.** An SQL
14750 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61 74 20   operation that 
14760 69 73 20 69 6e 74 65 72 72 75 70 74 65 64 20 77  is interrupted w
14770 69 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51 4c 49  ill return [SQLI
14780 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 2e 0a 2a  TE_INTERRUPT]..*
14790 2a 20 49 66 20 74 68 65 20 69 6e 74 65 72 72 75  * If the interru
147a0 70 74 65 64 20 53 51 4c 20 6f 70 65 72 61 74 69  pted SQL operati
147b0 6f 6e 20 69 73 20 61 6e 20 49 4e 53 45 52 54 2c  on is an INSERT,
147c0 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45   UPDATE, or DELE
147d0 54 45 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e  TE.** that is in
147e0 73 69 64 65 20 61 6e 20 65 78 70 6c 69 63 69 74  side an explicit
147f0 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68   transaction, th
14800 65 6e 20 74 68 65 20 65 6e 74 69 72 65 20 74 72  en the entire tr
14810 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c  ansaction.** wil
14820 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  l be rolled back
14830 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a   automatically..
14840 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
14850 33 5f 69 6e 74 65 72 72 75 70 74 28 44 29 20 63  3_interrupt(D) c
14860 61 6c 6c 20 69 73 20 69 6e 20 65 66 66 65 63 74  all is in effect
14870 20 75 6e 74 69 6c 20 61 6c 6c 20 63 75 72 72 65   until all curre
14880 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 0a 2a 2a 20  ntly running.** 
14890 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 6f  SQL statements o
148a0 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  n [database conn
148b0 65 63 74 69 6f 6e 5d 20 44 20 63 6f 6d 70 6c 65  ection] D comple
148c0 74 65 2e 20 20 41 6e 79 20 6e 65 77 20 53 51 4c  te.  Any new SQL
148d0 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74   statements.** t
148e0 68 61 74 20 61 72 65 20 73 74 61 72 74 65 64 20  hat are started 
148f0 61 66 74 65 72 20 74 68 65 20 73 71 6c 69 74 65  after the sqlite
14900 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 63 61  3_interrupt() ca
14910 6c 6c 20 61 6e 64 20 62 65 66 6f 72 65 20 74 68  ll and before th
14920 65 20 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 73 74  e .** running st
14930 61 74 65 6d 65 6e 74 73 20 72 65 61 63 68 65 73  atements reaches
14940 20 7a 65 72 6f 20 61 72 65 20 69 6e 74 65 72 72   zero are interr
14950 75 70 74 65 64 20 61 73 20 69 66 20 74 68 65 79  upted as if they
14960 20 68 61 64 20 62 65 65 6e 0a 2a 2a 20 72 75 6e   had been.** run
14970 6e 69 6e 67 20 70 72 69 6f 72 20 74 6f 20 74 68  ning prior to th
14980 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72  e sqlite3_interr
14990 75 70 74 28 29 20 63 61 6c 6c 2e 20 20 4e 65 77  upt() call.  New
149a0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a   SQL statements.
149b0 2a 2a 20 74 68 61 74 20 61 72 65 20 73 74 61 72  ** that are star
149c0 74 65 64 20 61 66 74 65 72 20 74 68 65 20 72 75  ted after the ru
149d0 6e 6e 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20  nning statement 
149e0 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65  count reaches ze
149f0 72 6f 20 61 72 65 0a 2a 2a 20 6e 6f 74 20 65 66  ro are.** not ef
14a00 66 65 63 74 65 64 20 62 79 20 74 68 65 20 73 71  fected by the sq
14a10 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28  lite3_interrupt(
14a20 29 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20  )..** A call to 
14a30 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
14a40 74 28 44 29 20 74 68 61 74 20 6f 63 63 75 72 73  t(D) that occurs
14a50 20 77 68 65 6e 20 74 68 65 72 65 20 61 72 65 20   when there are 
14a60 6e 6f 20 72 75 6e 6e 69 6e 67 0a 2a 2a 20 53 51  no running.** SQ
14a70 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 73 20  L statements is 
14a80 61 20 6e 6f 2d 6f 70 20 61 6e 64 20 68 61 73 20  a no-op and has 
14a90 6e 6f 20 65 66 66 65 63 74 20 6f 6e 20 53 51 4c  no effect on SQL
14aa0 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74   statements.** t
14ab0 68 61 74 20 61 72 65 20 73 74 61 72 74 65 64 20  hat are started 
14ac0 61 66 74 65 72 20 74 68 65 20 73 71 6c 69 74 65  after the sqlite
14ad0 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 63 61  3_interrupt() ca
14ae0 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a  ll returns..**.*
14af0 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
14b00 2a 2a 20 5b 48 31 32 32 37 31 5d 20 5b 48 31 32  ** [H12271] [H12
14b10 32 37 32 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  272].**.** If th
14b20 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
14b30 63 74 69 6f 6e 20 63 6c 6f 73 65 73 20 77 68 69  ction closes whi
14b40 6c 65 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65  le [sqlite3_inte
14b50 72 72 75 70 74 28 29 5d 0a 2a 2a 20 69 73 20 72  rrupt()].** is r
14b60 75 6e 6e 69 6e 67 20 74 68 65 6e 20 62 61 64 20  unning then bad 
14b70 74 68 69 6e 67 73 20 77 69 6c 6c 20 6c 69 6b 65  things will like
14b80 6c 79 20 68 61 70 70 65 6e 2e 0a 2a 2f 0a 53 51  ly happen..*/.SQ
14b90 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
14ba0 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28  lite3_interrupt(
14bb0 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a  sqlite3*);../*.*
14bc0 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 74 65  * CAPI3REF: Dete
14bd0 72 6d 69 6e 65 20 49 66 20 41 6e 20 53 51 4c 20  rmine If An SQL 
14be0 53 74 61 74 65 6d 65 6e 74 20 49 73 20 43 6f 6d  Statement Is Com
14bf0 70 6c 65 74 65 20 7b 48 31 30 35 31 30 7d 20 3c  plete {H10510} <
14c00 53 37 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S70200>.**.** Th
14c10 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
14c20 20 75 73 65 66 75 6c 20 64 75 72 69 6e 67 20 63   useful during c
14c30 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 69 6e 70 75  ommand-line inpu
14c40 74 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69  t to determine i
14c50 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74  f the.** current
14c60 6c 79 20 65 6e 74 65 72 65 64 20 74 65 78 74 20  ly entered text 
14c70 73 65 65 6d 73 20 74 6f 20 66 6f 72 6d 20 61 20  seems to form a 
14c80 63 6f 6d 70 6c 65 74 65 20 53 51 4c 20 73 74 61  complete SQL sta
14c90 74 65 6d 65 6e 74 20 6f 72 0a 2a 2a 20 69 66 20  tement or.** if 
14ca0 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 70 75 74  additional input
14cb0 20 69 73 20 6e 65 65 64 65 64 20 62 65 66 6f 72   is needed befor
14cc0 65 20 73 65 6e 64 69 6e 67 20 74 68 65 20 74 65  e sending the te
14cd0 78 74 20 69 6e 74 6f 0a 2a 2a 20 53 51 4c 69 74  xt into.** SQLit
14ce0 65 20 66 6f 72 20 70 61 72 73 69 6e 67 2e 20 20  e for parsing.  
14cf0 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72  These routines r
14d00 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 69  eturn 1 if the i
14d10 6e 70 75 74 20 73 74 72 69 6e 67 0a 2a 2a 20 61  nput string.** a
14d20 70 70 65 61 72 73 20 74 6f 20 62 65 20 61 20 63  ppears to be a c
14d30 6f 6d 70 6c 65 74 65 20 53 51 4c 20 73 74 61 74  omplete SQL stat
14d40 65 6d 65 6e 74 2e 20 20 41 20 73 74 61 74 65 6d  ement.  A statem
14d50 65 6e 74 20 69 73 20 6a 75 64 67 65 64 20 74 6f  ent is judged to
14d60 20 62 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 20   be.** complete 
14d70 69 66 20 69 74 20 65 6e 64 73 20 77 69 74 68 20  if it ends with 
14d80 61 20 73 65 6d 69 63 6f 6c 6f 6e 20 74 6f 6b 65  a semicolon toke
14d90 6e 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 20 70  n and is not a p
14da0 72 65 66 69 78 20 6f 66 20 61 0a 2a 2a 20 77 65  refix of a.** we
14db0 6c 6c 2d 66 6f 72 6d 65 64 20 43 52 45 41 54 45  ll-formed CREATE
14dc0 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65   TRIGGER stateme
14dd0 6e 74 2e 20 20 53 65 6d 69 63 6f 6c 6f 6e 73 20  nt.  Semicolons 
14de0 74 68 61 74 20 61 72 65 20 65 6d 62 65 64 64 65  that are embedde
14df0 64 20 77 69 74 68 69 6e 0a 2a 2a 20 73 74 72 69  d within.** stri
14e00 6e 67 20 6c 69 74 65 72 61 6c 73 20 6f 72 20 71  ng literals or q
14e10 75 6f 74 65 64 20 69 64 65 6e 74 69 66 69 65 72  uoted identifier
14e20 20 6e 61 6d 65 73 20 6f 72 20 63 6f 6d 6d 65 6e   names or commen
14e30 74 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 69 6e  ts are not.** in
14e40 64 65 70 65 6e 64 65 6e 74 20 74 6f 6b 65 6e 73  dependent tokens
14e50 20 28 74 68 65 79 20 61 72 65 20 70 61 72 74 20   (they are part 
14e60 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 6e 20  of the token in 
14e70 77 68 69 63 68 20 74 68 65 79 20 61 72 65 0a 2a  which they are.*
14e80 2a 20 65 6d 62 65 64 64 65 64 29 20 61 6e 64 20  * embedded) and 
14e90 74 68 75 73 20 64 6f 20 6e 6f 74 20 63 6f 75 6e  thus do not coun
14ea0 74 20 61 73 20 61 20 73 74 61 74 65 6d 65 6e 74  t as a statement
14eb0 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 57 68   terminator.  Wh
14ec0 69 74 65 73 70 61 63 65 0a 2a 2a 20 61 6e 64 20  itespace.** and 
14ed0 63 6f 6d 6d 65 6e 74 73 20 74 68 61 74 20 66 6f  comments that fo
14ee0 6c 6c 6f 77 20 74 68 65 20 66 69 6e 61 6c 20 73  llow the final s
14ef0 65 6d 69 63 6f 6c 6f 6e 20 61 72 65 20 69 67 6e  emicolon are ign
14f00 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  ored..**.** Thes
14f10 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72  e routines retur
14f20 6e 20 30 20 69 66 20 74 68 65 20 73 74 61 74 65  n 0 if the state
14f30 6d 65 6e 74 20 69 73 20 69 6e 63 6f 6d 70 6c 65  ment is incomple
14f40 74 65 2e 20 20 49 66 20 61 0a 2a 2a 20 6d 65 6d  te.  If a.** mem
14f50 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
14f60 61 69 6c 73 2c 20 74 68 65 6e 20 53 51 4c 49 54  ails, then SQLIT
14f70 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72  E_NOMEM is retur
14f80 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  ned..**.** These
14f90 20 72 6f 75 74 69 6e 65 73 20 64 6f 20 6e 6f 74   routines do not
14fa0 20 70 61 72 73 65 20 74 68 65 20 53 51 4c 20 73   parse the SQL s
14fb0 74 61 74 65 6d 65 6e 74 73 20 74 68 75 73 0a 2a  tatements thus.*
14fc0 2a 20 77 69 6c 6c 20 6e 6f 74 20 64 65 74 65 63  * will not detec
14fd0 74 20 73 79 6e 74 61 63 74 69 63 61 6c 6c 79 20  t syntactically 
14fe0 69 6e 63 6f 72 72 65 63 74 20 53 51 4c 2e 0a 2a  incorrect SQL..*
14ff0 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 68  *.** If SQLite h
15000 61 73 20 6e 6f 74 20 62 65 65 6e 20 69 6e 69 74  as not been init
15010 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 20 5b 73  ialized using [s
15020 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
15030 65 28 29 5d 20 70 72 69 6f 72 20 0a 2a 2a 20 74  e()] prior .** t
15040 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74  o invoking sqlit
15050 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 20  e3_complete16() 
15060 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 69 6e 69  then sqlite3_ini
15070 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76  tialize() is inv
15080 6f 6b 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69  oked.** automati
15090 63 61 6c 6c 79 20 62 79 20 73 71 6c 69 74 65 33  cally by sqlite3
150a0 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 2e 20 20  _complete16().  
150b0 49 66 20 74 68 61 74 20 69 6e 69 74 69 61 6c 69  If that initiali
150c0 7a 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a 2a  zation fails,.**
150d0 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e   then the return
150e0 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71 6c 69   value from sqli
150f0 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29  te3_complete16()
15100 20 77 69 6c 6c 20 62 65 20 6e 6f 6e 2d 7a 65 72   will be non-zer
15110 6f 0a 2a 2a 20 72 65 67 61 72 64 6c 65 73 73 20  o.** regardless 
15120 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  of whether or no
15130 74 20 74 68 65 20 69 6e 70 75 74 20 53 51 4c 20  t the input SQL 
15140 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a 2a 0a  is complete..**.
15150 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
15160 20 5b 48 31 30 35 31 31 5d 20 5b 48 31 30 35 31   [H10511] [H1051
15170 32 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70  2].**.** The inp
15180 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  ut to [sqlite3_c
15190 6f 6d 70 6c 65 74 65 28 29 5d 20 6d 75 73 74 20  omplete()] must 
151a0 62 65 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  be a zero-termin
151b0 61 74 65 64 0a 2a 2a 20 55 54 46 2d 38 20 73 74  ated.** UTF-8 st
151c0 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ring..**.** The 
151d0 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65  input to [sqlite
151e0 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 5d 20  3_complete16()] 
151f0 6d 75 73 74 20 62 65 20 61 20 7a 65 72 6f 2d 74  must be a zero-t
15200 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55 54 46  erminated.** UTF
15210 2d 31 36 20 73 74 72 69 6e 67 20 69 6e 20 6e 61  -16 string in na
15220 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2e  tive byte order.
15230 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
15240 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  nt sqlite3_compl
15250 65 74 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ete(const char *
15260 73 71 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  sql);.SQLITE_API
15270 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d   int sqlite3_com
15280 70 6c 65 74 65 31 36 28 63 6f 6e 73 74 20 76 6f  plete16(const vo
15290 69 64 20 2a 73 71 6c 29 3b 0a 0a 2f 2a 0a 2a 2a  id *sql);../*.**
152a0 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69 73   CAPI3REF: Regis
152b0 74 65 72 20 41 20 43 61 6c 6c 62 61 63 6b 20 54  ter A Callback T
152c0 6f 20 48 61 6e 64 6c 65 20 53 51 4c 49 54 45 5f  o Handle SQLITE_
152d0 42 55 53 59 20 45 72 72 6f 72 73 20 7b 48 31 32  BUSY Errors {H12
152e0 33 31 30 7d 20 3c 53 34 30 34 30 30 3e 0a 2a 2a  310} <S40400>.**
152f0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
15300 20 73 65 74 73 20 61 20 63 61 6c 6c 62 61 63 6b   sets a callback
15310 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 6d   function that m
15320 69 67 68 74 20 62 65 20 69 6e 76 6f 6b 65 64 20  ight be invoked 
15330 77 68 65 6e 65 76 65 72 0a 2a 2a 20 61 6e 20 61  whenever.** an a
15340 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74  ttempt is made t
15350 6f 20 6f 70 65 6e 20 61 20 64 61 74 61 62 61 73  o open a databas
15360 65 20 74 61 62 6c 65 20 74 68 61 74 20 61 6e 6f  e table that ano
15370 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 6f  ther thread.** o
15380 72 20 70 72 6f 63 65 73 73 20 68 61 73 20 6c 6f  r process has lo
15390 63 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  cked..**.** If t
153a0 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b  he busy callback
153b0 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 5b   is NULL, then [
153c0 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 20  SQLITE_BUSY] or 
153d0 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c  [SQLITE_IOERR_BL
153e0 4f 43 4b 45 44 5d 0a 2a 2a 20 69 73 20 72 65 74  OCKED].** is ret
153f0 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c  urned immediatel
15400 79 20 75 70 6f 6e 20 65 6e 63 6f 75 6e 74 65 72  y upon encounter
15410 69 6e 67 20 74 68 65 20 6c 6f 63 6b 2e 20 49 66  ing the lock. If
15420 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61   the busy callba
15430 63 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55 4c  ck.** is not NUL
15440 4c 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c  L, then the call
15450 62 61 63 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76  back will be inv
15460 6f 6b 65 64 20 77 69 74 68 20 74 77 6f 20 61 72  oked with two ar
15470 67 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  guments..**.** T
15480 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
15490 74 20 74 6f 20 74 68 65 20 68 61 6e 64 6c 65 72  t to the handler
154a0 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68   is a copy of th
154b0 65 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72 20  e void* pointer 
154c0 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 68 65 20  which.** is the 
154d0 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 74  third argument t
154e0 6f 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68  o sqlite3_busy_h
154f0 61 6e 64 6c 65 72 28 29 2e 20 20 54 68 65 20 73  andler().  The s
15500 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
15510 6f 0a 2a 2a 20 74 68 65 20 68 61 6e 64 6c 65 72  o.** the handler
15520 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 74 68 65   callback is the
15530 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73   number of times
15540 20 74 68 61 74 20 74 68 65 20 62 75 73 79 20 68   that the busy h
15550 61 6e 64 6c 65 72 20 68 61 73 0a 2a 2a 20 62 65  andler has.** be
15560 65 6e 20 69 6e 76 6f 6b 65 64 20 66 6f 72 20 74  en invoked for t
15570 68 69 73 20 6c 6f 63 6b 69 6e 67 20 65 76 65 6e  his locking even
15580 74 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 62 75  t.  If the.** bu
15590 73 79 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75  sy callback retu
155a0 72 6e 73 20 30 2c 20 74 68 65 6e 20 6e 6f 20 61  rns 0, then no a
155b0 64 64 69 74 69 6f 6e 61 6c 20 61 74 74 65 6d 70  dditional attemp
155c0 74 73 20 61 72 65 20 6d 61 64 65 20 74 6f 0a 2a  ts are made to.*
155d0 2a 20 61 63 63 65 73 73 20 74 68 65 20 64 61 74  * access the dat
155e0 61 62 61 73 65 20 61 6e 64 20 5b 53 51 4c 49 54  abase and [SQLIT
155f0 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49  E_BUSY] or [SQLI
15600 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44  TE_IOERR_BLOCKED
15610 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  ] is returned..*
15620 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63  * If the callbac
15630 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65  k returns non-ze
15640 72 6f 2c 20 74 68 65 6e 20 61 6e 6f 74 68 65 72  ro, then another
15650 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73 20 6d   attempt.** is m
15660 61 64 65 20 74 6f 20 6f 70 65 6e 20 74 68 65 20  ade to open the 
15670 64 61 74 61 62 61 73 65 20 66 6f 72 20 72 65 61  database for rea
15680 64 69 6e 67 20 61 6e 64 20 74 68 65 20 63 79 63  ding and the cyc
15690 6c 65 20 72 65 70 65 61 74 73 2e 0a 2a 2a 0a 2a  le repeats..**.*
156a0 2a 20 54 68 65 20 70 72 65 73 65 6e 63 65 20 6f  * The presence o
156b0 66 20 61 20 62 75 73 79 20 68 61 6e 64 6c 65 72  f a busy handler
156c0 20 64 6f 65 73 20 6e 6f 74 20 67 75 61 72 61 6e   does not guaran
156d0 74 65 65 20 74 68 61 74 20 69 74 20 77 69 6c 6c  tee that it will
156e0 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 77   be invoked.** w
156f0 68 65 6e 20 74 68 65 72 65 20 69 73 20 6c 6f 63  hen there is loc
15700 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2e 20 49 66  k contention. If
15710 20 53 51 4c 69 74 65 20 64 65 74 65 72 6d 69 6e   SQLite determin
15720 65 73 20 74 68 61 74 20 69 6e 76 6f 6b 69 6e 67  es that invoking
15730 20 74 68 65 20 62 75 73 79 0a 2a 2a 20 68 61 6e   the busy.** han
15740 64 6c 65 72 20 63 6f 75 6c 64 20 72 65 73 75 6c  dler could resul
15750 74 20 69 6e 20 61 20 64 65 61 64 6c 6f 63 6b 2c  t in a deadlock,
15760 20 69 74 20 77 69 6c 6c 20 67 6f 20 61 68 65 61   it will go ahea
15770 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b 53 51  d and return [SQ
15780 4c 49 54 45 5f 42 55 53 59 5d 0a 2a 2a 20 6f 72  LITE_BUSY].** or
15790 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42   [SQLITE_IOERR_B
157a0 4c 4f 43 4b 45 44 5d 20 69 6e 73 74 65 61 64 20  LOCKED] instead 
157b0 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20  of invoking the 
157c0 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a  busy handler..**
157d0 20 43 6f 6e 73 69 64 65 72 20 61 20 73 63 65 6e   Consider a scen
157e0 61 72 69 6f 20 77 68 65 72 65 20 6f 6e 65 20 70  ario where one p
157f0 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e  rocess is holdin
15800 67 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 74 68  g a read lock th
15810 61 74 0a 2a 2a 20 69 74 20 69 73 20 74 72 79 69  at.** it is tryi
15820 6e 67 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f  ng to promote to
15830 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b   a reserved lock
15840 20 61 6e 64 0a 2a 2a 20 61 20 73 65 63 6f 6e 64   and.** a second
15850 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64   process is hold
15860 69 6e 67 20 61 20 72 65 73 65 72 76 65 64 20 6c  ing a reserved l
15870 6f 63 6b 20 74 68 61 74 20 69 74 20 69 73 20 74  ock that it is t
15880 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 70 72 6f 6d  rying.** to prom
15890 6f 74 65 20 74 6f 20 61 6e 20 65 78 63 6c 75 73  ote to an exclus
158a0 69 76 65 20 6c 6f 63 6b 2e 20 20 54 68 65 20 66  ive lock.  The f
158b0 69 72 73 74 20 70 72 6f 63 65 73 73 20 63 61 6e  irst process can
158c0 6e 6f 74 20 70 72 6f 63 65 65 64 0a 2a 2a 20 62  not proceed.** b
158d0 65 63 61 75 73 65 20 69 74 20 69 73 20 62 6c 6f  ecause it is blo
158e0 63 6b 65 64 20 62 79 20 74 68 65 20 73 65 63 6f  cked by the seco
158f0 6e 64 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e  nd and the secon
15900 64 20 70 72 6f 63 65 73 73 20 63 61 6e 6e 6f 74  d process cannot
15910 0a 2a 2a 20 70 72 6f 63 65 65 64 20 62 65 63 61  .** proceed beca
15920 75 73 65 20 69 74 20 69 73 20 62 6c 6f 63 6b 65  use it is blocke
15930 64 20 62 79 20 74 68 65 20 66 69 72 73 74 2e 20  d by the first. 
15940 20 49 66 20 62 6f 74 68 20 70 72 6f 63 65 73 73   If both process
15950 65 73 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65  es.** invoke the
15960 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73 2c 20   busy handlers, 
15970 6e 65 69 74 68 65 72 20 77 69 6c 6c 20 6d 61 6b  neither will mak
15980 65 20 61 6e 79 20 70 72 6f 67 72 65 73 73 2e 20  e any progress. 
15990 20 54 68 65 72 65 66 6f 72 65 2c 0a 2a 2a 20 53   Therefore,.** S
159a0 51 4c 69 74 65 20 72 65 74 75 72 6e 73 20 5b 53  QLite returns [S
159b0 51 4c 49 54 45 5f 42 55 53 59 5d 20 66 6f 72 20  QLITE_BUSY] for 
159c0 74 68 65 20 66 69 72 73 74 20 70 72 6f 63 65 73  the first proces
159d0 73 2c 20 68 6f 70 69 6e 67 20 74 68 61 74 20 74  s, hoping that t
159e0 68 69 73 0a 2a 2a 20 77 69 6c 6c 20 69 6e 64 75  his.** will indu
159f0 63 65 20 74 68 65 20 66 69 72 73 74 20 70 72 6f  ce the first pro
15a00 63 65 73 73 20 74 6f 20 72 65 6c 65 61 73 65 20  cess to release 
15a10 69 74 73 20 72 65 61 64 20 6c 6f 63 6b 20 61 6e  its read lock an
15a20 64 20 61 6c 6c 6f 77 0a 2a 2a 20 74 68 65 20 73  d allow.** the s
15a30 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20 74 6f  econd process to
15a40 20 70 72 6f 63 65 65 64 2e 0a 2a 2a 0a 2a 2a 20   proceed..**.** 
15a50 54 68 65 20 64 65 66 61 75 6c 74 20 62 75 73 79  The default busy
15a60 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 4c   callback is NUL
15a70 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51  L..**.** The [SQ
15a80 4c 49 54 45 5f 42 55 53 59 5d 20 65 72 72 6f 72  LITE_BUSY] error
15a90 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20 74 6f   is converted to
15aa0 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42   [SQLITE_IOERR_B
15ab0 4c 4f 43 4b 45 44 5d 0a 2a 2a 20 77 68 65 6e 20  LOCKED].** when 
15ac0 53 51 4c 69 74 65 20 69 73 20 69 6e 20 74 68 65  SQLite is in the
15ad0 20 6d 69 64 64 6c 65 20 6f 66 20 61 20 6c 61 72   middle of a lar
15ae0 67 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  ge transaction w
15af0 68 65 72 65 20 61 6c 6c 20 74 68 65 0a 2a 2a 20  here all the.** 
15b00 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 6e 6f 74  changes will not
15b10 20 66 69 74 20 69 6e 74 6f 20 74 68 65 20 69 6e   fit into the in
15b20 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e 20 20  -memory cache.  
15b30 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 61  SQLite will.** a
15b40 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 52 45  lready hold a RE
15b50 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74  SERVED lock on t
15b60 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
15b70 2c 20 62 75 74 20 69 74 20 6e 65 65 64 73 0a 2a  , but it needs.*
15b80 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 68 69  * to promote thi
15b90 73 20 6c 6f 63 6b 20 74 6f 20 45 58 43 4c 55 53  s lock to EXCLUS
15ba0 49 56 45 20 73 6f 20 74 68 61 74 20 69 74 20 63  IVE so that it c
15bb0 61 6e 20 73 70 69 6c 6c 20 63 61 63 68 65 0a 2a  an spill cache.*
15bc0 2a 20 70 61 67 65 73 20 69 6e 74 6f 20 74 68 65  * pages into the
15bd0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77   database file w
15be0 69 74 68 6f 75 74 20 68 61 72 6d 20 74 6f 20 63  ithout harm to c
15bf0 6f 6e 63 75 72 72 65 6e 74 0a 2a 2a 20 72 65 61  oncurrent.** rea
15c00 64 65 72 73 2e 20 20 49 66 20 69 74 20 69 73 20  ders.  If it is 
15c10 75 6e 61 62 6c 65 20 74 6f 20 70 72 6f 6d 6f 74  unable to promot
15c20 65 20 74 68 65 20 6c 6f 63 6b 2c 20 74 68 65 6e  e the lock, then
15c30 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a   the in-memory.*
15c40 2a 20 63 61 63 68 65 20 77 69 6c 6c 20 62 65 20  * cache will be 
15c50 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e  left in an incon
15c60 73 69 73 74 65 6e 74 20 73 74 61 74 65 20 61 6e  sistent state an
15c70 64 20 73 6f 20 74 68 65 20 65 72 72 6f 72 0a 2a  d so the error.*
15c80 2a 20 63 6f 64 65 20 69 73 20 70 72 6f 6d 6f 74  * code is promot
15c90 65 64 20 66 72 6f 6d 20 74 68 65 20 72 65 6c 61  ed from the rela
15ca0 74 69 76 65 6c 79 20 62 65 6e 69 67 6e 20 5b 53  tively benign [S
15cb0 51 4c 49 54 45 5f 42 55 53 59 5d 20 74 6f 0a 2a  QLITE_BUSY] to.*
15cc0 2a 20 74 68 65 20 6d 6f 72 65 20 73 65 76 65 72  * the more sever
15cd0 65 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  e [SQLITE_IOERR_
15ce0 42 4c 4f 43 4b 45 44 5d 2e 20 20 54 68 69 73 20  BLOCKED].  This 
15cf0 65 72 72 6f 72 20 63 6f 64 65 20 70 72 6f 6d 6f  error code promo
15d00 74 69 6f 6e 0a 2a 2a 20 66 6f 72 63 65 73 20 61  tion.** forces a
15d10 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c 6c  n automatic roll
15d20 62 61 63 6b 20 6f 66 20 74 68 65 20 63 68 61 6e  back of the chan
15d30 67 65 73 2e 20 20 53 65 65 20 74 68 65 0a 2a 2a  ges.  See the.**
15d40 20 3c 61 20 68 72 65 66 3d 22 2f 63 76 73 74 72   <a href="/cvstr
15d50 61 63 2f 77 69 6b 69 3f 70 3d 43 6f 72 72 75 70  ac/wiki?p=Corrup
15d60 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e 67 42 75 73  tionFollowingBus
15d70 79 45 72 72 6f 72 22 3e 0a 2a 2a 20 43 6f 72 72  yError">.** Corr
15d80 75 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e 67 42  uptionFollowingB
15d90 75 73 79 45 72 72 6f 72 3c 2f 61 3e 20 77 69 6b  usyError</a> wik
15da0 69 20 70 61 67 65 20 66 6f 72 20 61 20 64 69 73  i page for a dis
15db0 63 75 73 73 69 6f 6e 20 6f 66 20 77 68 79 0a 2a  cussion of why.*
15dc0 2a 20 74 68 69 73 20 69 73 20 69 6d 70 6f 72 74  * this is import
15dd0 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  ant..**.** There
15de0 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 61 20 73   can only be a s
15df0 69 6e 67 6c 65 20 62 75 73 79 20 68 61 6e 64 6c  ingle busy handl
15e00 65 72 20 64 65 66 69 6e 65 64 20 66 6f 72 20 65  er defined for e
15e10 61 63 68 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65  ach.** [database
15e20 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 53   connection].  S
15e30 65 74 74 69 6e 67 20 61 20 6e 65 77 20 62 75 73  etting a new bus
15e40 79 20 68 61 6e 64 6c 65 72 20 63 6c 65 61 72 73  y handler clears
15e50 20 61 6e 79 0a 2a 2a 20 70 72 65 76 69 6f 75 73   any.** previous
15e60 6c 79 20 73 65 74 20 68 61 6e 64 6c 65 72 2e 20  ly set handler. 
15e70 20 4e 6f 74 65 20 74 68 61 74 20 63 61 6c 6c 69   Note that calli
15e80 6e 67 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79  ng [sqlite3_busy
15e90 5f 74 69 6d 65 6f 75 74 28 29 5d 0a 2a 2a 20 77  _timeout()].** w
15ea0 69 6c 6c 20 61 6c 73 6f 20 73 65 74 20 6f 72 20  ill also set or 
15eb0 63 6c 65 61 72 20 74 68 65 20 62 75 73 79 20 68  clear the busy h
15ec0 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  andler..**.** Th
15ed0 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20  e busy callback 
15ee0 73 68 6f 75 6c 64 20 6e 6f 74 20 74 61 6b 65 20  should not take 
15ef0 61 6e 79 20 61 63 74 69 6f 6e 73 20 77 68 69 63  any actions whic
15f00 68 20 6d 6f 64 69 66 79 20 74 68 65 0a 2a 2a 20  h modify the.** 
15f10 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
15f20 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64  ion that invoked
15f30 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65   the busy handle
15f40 72 2e 20 20 41 6e 79 20 73 75 63 68 20 61 63 74  r.  Any such act
15f50 69 6f 6e 73 0a 2a 2a 20 72 65 73 75 6c 74 20 69  ions.** result i
15f60 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65 68 61  n undefined beha
15f70 76 69 6f 72 2e 0a 2a 2a 20 0a 2a 2a 20 52 65 71  vior..** .** Req
15f80 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
15f90 31 32 33 31 31 5d 20 5b 48 31 32 33 31 32 5d 20  12311] [H12312] 
15fa0 5b 48 31 32 33 31 34 5d 20 5b 48 31 32 33 31 36  [H12314] [H12316
15fb0 5d 20 5b 48 31 32 33 31 38 5d 0a 2a 2a 0a 2a 2a  ] [H12318].**.**
15fc0 20 41 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20   A busy handler 
15fd0 6d 75 73 74 20 6e 6f 74 20 63 6c 6f 73 65 20 74  must not close t
15fe0 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
15ff0 65 63 74 69 6f 6e 0a 2a 2a 20 6f 72 20 5b 70 72  ection.** or [pr
16000 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
16010 5d 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74  ] that invoked t
16020 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e  he busy handler.
16030 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
16040 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f  nt sqlite3_busy_
16050 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 33 2a  handler(sqlite3*
16060 2c 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69  , int(*)(void*,i
16070 6e 74 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a  nt), void*);../*
16080 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 65  .** CAPI3REF: Se
16090 74 20 41 20 42 75 73 79 20 54 69 6d 65 6f 75 74  t A Busy Timeout
160a0 20 7b 48 31 32 33 34 30 7d 20 3c 53 34 30 34 31   {H12340} <S4041
160b0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  0>.**.** This ro
160c0 75 74 69 6e 65 20 73 65 74 73 20 61 20 5b 73 71  utine sets a [sq
160d0 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c  lite3_busy_handl
160e0 65 72 20 7c 20 62 75 73 79 20 68 61 6e 64 6c 65  er | busy handle
160f0 72 5d 20 74 68 61 74 20 73 6c 65 65 70 73 0a 2a  r] that sleeps.*
16100 2a 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 65  * for a specifie
16110 64 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65  d amount of time
16120 20 77 68 65 6e 20 61 20 74 61 62 6c 65 20 69 73   when a table is
16130 20 6c 6f 63 6b 65 64 2e 20 20 54 68 65 20 68 61   locked.  The ha
16140 6e 64 6c 65 72 0a 2a 2a 20 77 69 6c 6c 20 73 6c  ndler.** will sl
16150 65 65 70 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d  eep multiple tim
16160 65 73 20 75 6e 74 69 6c 20 61 74 20 6c 65 61 73  es until at leas
16170 74 20 22 6d 73 22 20 6d 69 6c 6c 69 73 65 63 6f  t "ms" milliseco
16180 6e 64 73 20 6f 66 20 73 6c 65 65 70 69 6e 67 0a  nds of sleeping.
16190 2a 2a 20 68 61 76 65 20 61 63 63 75 6d 75 6c 61  ** have accumula
161a0 74 65 64 2e 20 7b 48 31 32 33 34 33 7d 20 41 66  ted. {H12343} Af
161b0 74 65 72 20 22 6d 73 22 20 6d 69 6c 6c 69 73 65  ter "ms" millise
161c0 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 69 6e  conds of sleepin
161d0 67 2c 0a 2a 2a 20 74 68 65 20 68 61 6e 64 6c 65  g,.** the handle
161e0 72 20 72 65 74 75 72 6e 73 20 30 20 77 68 69 63  r returns 0 whic
161f0 68 20 63 61 75 73 65 73 20 5b 73 71 6c 69 74 65  h causes [sqlite
16200 33 5f 73 74 65 70 28 29 5d 20 74 6f 20 72 65 74  3_step()] to ret
16210 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 42  urn.** [SQLITE_B
16220 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f  USY] or [SQLITE_
16230 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 2e 0a  IOERR_BLOCKED]..
16240 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 74 68  **.** Calling th
16250 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20  is routine with 
16260 61 6e 20 61 72 67 75 6d 65 6e 74 20 6c 65 73 73  an argument less
16270 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
16280 6f 20 7a 65 72 6f 0a 2a 2a 20 74 75 72 6e 73 20  o zero.** turns 
16290 6f 66 66 20 61 6c 6c 20 62 75 73 79 20 68 61 6e  off all busy han
162a0 64 6c 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  dlers..**.** The
162b0 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 61  re can only be a
162c0 20 73 69 6e 67 6c 65 20 62 75 73 79 20 68 61 6e   single busy han
162d0 64 6c 65 72 20 66 6f 72 20 61 20 70 61 72 74 69  dler for a parti
162e0 63 75 6c 61 72 0a 2a 2a 20 5b 64 61 74 61 62 61  cular.** [databa
162f0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61  se connection] a
16300 6e 79 20 61 6e 79 20 67 69 76 65 6e 20 6d 6f 6d  ny any given mom
16310 65 6e 74 2e 20 20 49 66 20 61 6e 6f 74 68 65 72  ent.  If another
16320 20 62 75 73 79 20 68 61 6e 64 6c 65 72 0a 2a 2a   busy handler.**
16330 20 77 61 73 20 64 65 66 69 6e 65 64 20 20 28 75   was defined  (u
16340 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 62 75  sing [sqlite3_bu
16350 73 79 5f 68 61 6e 64 6c 65 72 28 29 5d 29 20 70  sy_handler()]) p
16360 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 0a  rior to calling.
16370 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c  ** this routine,
16380 20 74 68 61 74 20 6f 74 68 65 72 20 62 75 73 79   that other busy
16390 20 68 61 6e 64 6c 65 72 20 69 73 20 63 6c 65 61   handler is clea
163a0 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  red..**.** Requi
163b0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32  rements:.** [H12
163c0 33 34 31 5d 20 5b 48 31 32 33 34 33 5d 20 5b 48  341] [H12343] [H
163d0 31 32 33 34 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45  12344].*/.SQLITE
163e0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
163f0 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 73 71  _busy_timeout(sq
16400 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6d 73 29 3b  lite3*, int ms);
16410 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
16420 3a 20 43 6f 6e 76 65 6e 69 65 6e 63 65 20 52 6f  : Convenience Ro
16430 75 74 69 6e 65 73 20 46 6f 72 20 52 75 6e 6e 69  utines For Runni
16440 6e 67 20 51 75 65 72 69 65 73 20 7b 48 31 32 33  ng Queries {H123
16450 37 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a  70} <S10000>.**.
16460 2a 2a 20 44 65 66 69 6e 69 74 69 6f 6e 3a 20 41  ** Definition: A
16470 20 3c 62 3e 72 65 73 75 6c 74 20 74 61 62 6c 65   <b>result table
16480 3c 2f 62 3e 20 69 73 20 6d 65 6d 6f 72 79 20 64  </b> is memory d
16490 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63 72  ata structure cr
164a0 65 61 74 65 64 20 62 79 20 74 68 65 0a 2a 2a 20  eated by the.** 
164b0 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62  [sqlite3_get_tab
164c0 6c 65 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e  le()] interface.
164d0 20 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65    A result table
164e0 20 72 65 63 6f 72 64 73 20 74 68 65 0a 2a 2a 20   records the.** 
164f0 63 6f 6d 70 6c 65 74 65 20 71 75 65 72 79 20 72  complete query r
16500 65 73 75 6c 74 73 20 66 72 6f 6d 20 6f 6e 65 20  esults from one 
16510 6f 72 20 6d 6f 72 65 20 71 75 65 72 69 65 73 2e  or more queries.
16520 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65  .**.** The table
16530 20 63 6f 6e 63 65 70 74 75 61 6c 6c 79 20 68 61   conceptually ha
16540 73 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  s a number of ro
16550 77 73 20 61 6e 64 20 63 6f 6c 75 6d 6e 73 2e 20  ws and columns. 
16560 20 42 75 74 0a 2a 2a 20 74 68 65 73 65 20 6e 75   But.** these nu
16570 6d 62 65 72 73 20 61 72 65 20 6e 6f 74 20 70 61  mbers are not pa
16580 72 74 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  rt of the result
16590 20 74 61 62 6c 65 20 69 74 73 65 6c 66 2e 20 20   table itself.  
165a0 54 68 65 73 65 0a 2a 2a 20 6e 75 6d 62 65 72 73  These.** numbers
165b0 20 61 72 65 20 6f 62 74 61 69 6e 65 64 20 73 65   are obtained se
165c0 70 61 72 61 74 65 6c 79 2e 20 20 4c 65 74 20 4e  parately.  Let N
165d0 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   be the number o
165e0 66 20 72 6f 77 73 0a 2a 2a 20 61 6e 64 20 4d 20  f rows.** and M 
165f0 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  be the number of
16600 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 0a 2a 2a 20   columns..**.** 
16610 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 69  A result table i
16620 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 6f  s an array of po
16630 69 6e 74 65 72 73 20 74 6f 20 7a 65 72 6f 2d 74  inters to zero-t
16640 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20  erminated UTF-8 
16650 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 54 68 65 72  strings..** Ther
16660 65 20 61 72 65 20 28 4e 2b 31 29 2a 4d 20 65 6c  e are (N+1)*M el
16670 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 61 72  ements in the ar
16680 72 61 79 2e 20 20 54 68 65 20 66 69 72 73 74 20  ray.  The first 
16690 4d 20 70 6f 69 6e 74 65 72 73 20 70 6f 69 6e 74  M pointers point
166a0 0a 2a 2a 20 74 6f 20 7a 65 72 6f 2d 74 65 72 6d  .** to zero-term
166b0 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20 74  inated strings t
166c0 68 61 74 20 20 63 6f 6e 74 61 69 6e 20 74 68 65  hat  contain the
166d0 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 63 6f   names of the co
166e0 6c 75 6d 6e 73 2e 0a 2a 2a 20 54 68 65 20 72 65  lumns..** The re
166f0 6d 61 69 6e 69 6e 67 20 65 6e 74 72 69 65 73 20  maining entries 
16700 61 6c 6c 20 70 6f 69 6e 74 20 74 6f 20 71 75 65  all point to que
16710 72 79 20 72 65 73 75 6c 74 73 2e 20 20 4e 55 4c  ry results.  NUL
16720 4c 20 76 61 6c 75 65 73 20 72 65 73 75 6c 74 0a  L values result.
16730 2a 2a 20 69 6e 20 4e 55 4c 4c 20 70 6f 69 6e 74  ** in NULL point
16740 65 72 73 2e 20 20 41 6c 6c 20 6f 74 68 65 72 20  ers.  All other 
16750 76 61 6c 75 65 73 20 61 72 65 20 69 6e 20 74 68  values are in th
16760 65 69 72 20 55 54 46 2d 38 20 7a 65 72 6f 2d 74  eir UTF-8 zero-t
16770 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 73 74 72  erminated.** str
16780 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69  ing representati
16790 6f 6e 20 61 73 20 72 65 74 75 72 6e 65 64 20 62  on as returned b
167a0 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  y [sqlite3_colum
167b0 6e 5f 74 65 78 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a  n_text()]..**.**
167c0 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20   A result table 
167d0 6d 69 67 68 74 20 63 6f 6e 73 69 73 74 20 6f 66  might consist of
167e0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6d 65 6d   one or more mem
167f0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e  ory allocations.
16800 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61  .** It is not sa
16810 66 65 20 74 6f 20 70 61 73 73 20 61 20 72 65 73  fe to pass a res
16820 75 6c 74 20 74 61 62 6c 65 20 64 69 72 65 63 74  ult table direct
16830 6c 79 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66  ly to [sqlite3_f
16840 72 65 65 28 29 5d 2e 0a 2a 2a 20 41 20 72 65 73  ree()]..** A res
16850 75 6c 74 20 74 61 62 6c 65 20 73 68 6f 75 6c 64  ult table should
16860 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 20   be deallocated 
16870 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66  using [sqlite3_f
16880 72 65 65 5f 74 61 62 6c 65 28 29 5d 2e 0a 2a 2a  ree_table()]..**
16890 0a 2a 2a 20 41 73 20 61 6e 20 65 78 61 6d 70 6c  .** As an exampl
168a0 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  e of the result 
168b0 74 61 62 6c 65 20 66 6f 72 6d 61 74 2c 20 73 75  table format, su
168c0 70 70 6f 73 65 20 61 20 71 75 65 72 79 20 72 65  ppose a query re
168d0 73 75 6c 74 0a 2a 2a 20 69 73 20 61 73 20 66 6f  sult.** is as fo
168e0 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c  llows:.**.** <bl
168f0 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a  ockquote><pre>.*
16900 2a 20 20 20 20 20 20 20 20 4e 61 6d 65 20 20 20  *        Name   
16910 20 20 20 20 20 7c 20 41 67 65 0a 2a 2a 20 20 20       | Age.**   
16920 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d       -----------
16930 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20  ------------.** 
16940 20 20 20 20 20 20 20 41 6c 69 63 65 20 20 20 20         Alice    
16950 20 20 20 7c 20 34 33 0a 2a 2a 20 20 20 20 20 20     | 43.**      
16960 20 20 42 6f 62 20 20 20 20 20 20 20 20 20 7c 20    Bob         | 
16970 32 38 0a 2a 2a 20 20 20 20 20 20 20 20 43 69 6e  28.**        Cin
16980 64 79 20 20 20 20 20 20 20 7c 20 32 31 0a 2a 2a  dy       | 21.**
16990 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75   </pre></blockqu
169a0 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  ote>.**.** There
169b0 20 61 72 65 20 74 77 6f 20 63 6f 6c 75 6d 6e 20   are two column 
169c0 28 4d 3d 3d 32 29 20 61 6e 64 20 74 68 72 65 65  (M==2) and three
169d0 20 72 6f 77 73 20 28 4e 3d 3d 33 29 2e 20 20 54   rows (N==3).  T
169e0 68 75 73 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c  hus the.** resul
169f0 74 20 74 61 62 6c 65 20 68 61 73 20 38 20 65 6e  t table has 8 en
16a00 74 72 69 65 73 2e 20 20 53 75 70 70 6f 73 65 20  tries.  Suppose 
16a10 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65  the result table
16a20 20 69 73 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e   is stored.** in
16a30 20 61 6e 20 61 72 72 61 79 20 6e 61 6d 65 73 20   an array names 
16a40 61 7a 52 65 73 75 6c 74 2e 20 20 54 68 65 6e 20  azResult.  Then 
16a50 61 7a 52 65 73 75 6c 74 20 68 6f 6c 64 73 20 74  azResult holds t
16a60 68 69 73 20 63 6f 6e 74 65 6e 74 3a 0a 2a 2a 0a  his content:.**.
16a70 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c  ** <blockquote><
16a80 70 72 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 61  pre>.**        a
16a90 7a 52 65 73 75 6c 74 26 23 39 31 3b 30 5d 20 3d  zResult&#91;0] =
16aa0 20 22 4e 61 6d 65 22 3b 0a 2a 2a 20 20 20 20 20   "Name";.**     
16ab0 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b     azResult&#91;
16ac0 31 5d 20 3d 20 22 41 67 65 22 3b 0a 2a 2a 20 20  1] = "Age";.**  
16ad0 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23        azResult&#
16ae0 39 31 3b 32 5d 20 3d 20 22 41 6c 69 63 65 22 3b  91;2] = "Alice";
16af0 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73  .**        azRes
16b00 75 6c 74 26 23 39 31 3b 33 5d 20 3d 20 22 34 33  ult&#91;3] = "43
16b10 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52  ";.**        azR
16b20 65 73 75 6c 74 26 23 39 31 3b 34 5d 20 3d 20 22  esult&#91;4] = "
16b30 42 6f 62 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20  Bob";.**        
16b40 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 35 5d 20  azResult&#91;5] 
16b50 3d 20 22 32 38 22 3b 0a 2a 2a 20 20 20 20 20 20  = "28";.**      
16b60 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 36    azResult&#91;6
16b70 5d 20 3d 20 22 43 69 6e 64 79 22 3b 0a 2a 2a 20  ] = "Cindy";.** 
16b80 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26         azResult&
16b90 23 39 31 3b 37 5d 20 3d 20 22 32 31 22 3b 0a 2a  #91;7] = "21";.*
16ba0 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71  * </pre></blockq
16bb0 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  uote>.**.** The 
16bc0 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
16bd0 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 65 76 61  e() function eva
16be0 6c 75 61 74 65 73 20 6f 6e 65 20 6f 72 20 6d 6f  luates one or mo
16bf0 72 65 0a 2a 2a 20 73 65 6d 69 63 6f 6c 6f 6e 2d  re.** semicolon-
16c00 73 65 70 61 72 61 74 65 64 20 53 51 4c 20 73 74  separated SQL st
16c10 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  atements in the 
16c20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20  zero-terminated 
16c30 55 54 46 2d 38 0a 2a 2a 20 73 74 72 69 6e 67 20  UTF-8.** string 
16c40 6f 66 20 69 74 73 20 32 6e 64 20 70 61 72 61 6d  of its 2nd param
16c50 65 74 65 72 2e 20 20 49 74 20 72 65 74 75 72 6e  eter.  It return
16c60 73 20 61 20 72 65 73 75 6c 74 20 74 61 62 6c 65  s a result table
16c70 20 74 6f 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74   to the.** point
16c80 65 72 20 67 69 76 65 6e 20 69 6e 20 69 74 73 20  er given in its 
16c90 33 72 64 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  3rd parameter..*
16ca0 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20 63  *.** After the c
16cb0 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  alling function 
16cc0 68 61 73 20 66 69 6e 69 73 68 65 64 20 75 73 69  has finished usi
16cd0 6e 67 20 74 68 65 20 72 65 73 75 6c 74 2c 20 69  ng the result, i
16ce0 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 70 61 73 73  t should.** pass
16cf0 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20   the pointer to 
16d00 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65  the result table
16d10 20 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65   to sqlite3_free
16d20 5f 74 61 62 6c 65 28 29 20 69 6e 20 6f 72 64 65  _table() in orde
16d30 72 20 74 6f 0a 2a 2a 20 72 65 6c 65 61 73 65 20  r to.** release 
16d40 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 61 74 20  the memory that 
16d50 77 61 73 20 6d 61 6c 6c 6f 63 65 64 2e 20 20 42  was malloced.  B
16d60 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 77 61  ecause of the wa
16d70 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65  y the.** [sqlite
16d80 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 68 61 70 70  3_malloc()] happ
16d90 65 6e 73 20 77 69 74 68 69 6e 20 73 71 6c 69 74  ens within sqlit
16da0 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 2c 20  e3_get_table(), 
16db0 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66  the calling.** f
16dc0 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74  unction must not
16dd0 20 74 72 79 20 74 6f 20 63 61 6c 6c 20 5b 73 71   try to call [sq
16de0 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 64 69  lite3_free()] di
16df0 72 65 63 74 6c 79 2e 20 20 4f 6e 6c 79 0a 2a 2a  rectly.  Only.**
16e00 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74   [sqlite3_free_t
16e10 61 62 6c 65 28 29 5d 20 69 73 20 61 62 6c 65 20  able()] is able 
16e20 74 6f 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d  to release the m
16e30 65 6d 6f 72 79 20 70 72 6f 70 65 72 6c 79 20 61  emory properly a
16e40 6e 64 20 73 61 66 65 6c 79 2e 0a 2a 2a 0a 2a 2a  nd safely..**.**
16e50 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74   The sqlite3_get
16e60 5f 74 61 62 6c 65 28 29 20 69 6e 74 65 72 66 61  _table() interfa
16e70 63 65 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65  ce is implemente
16e80 64 20 61 73 20 61 20 77 72 61 70 70 65 72 20 61  d as a wrapper a
16e90 72 6f 75 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65  round.** [sqlite
16ea0 33 5f 65 78 65 63 28 29 5d 2e 20 20 54 68 65 20  3_exec()].  The 
16eb0 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
16ec0 65 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73  e() routine does
16ed0 20 6e 6f 74 20 68 61 76 65 20 61 63 63 65 73 73   not have access
16ee0 0a 2a 2a 20 74 6f 20 61 6e 79 20 69 6e 74 65 72  .** to any inter
16ef0 6e 61 6c 20 64 61 74 61 20 73 74 72 75 63 74 75  nal data structu
16f00 72 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20  res of SQLite.  
16f10 49 74 20 75 73 65 73 20 6f 6e 6c 79 20 74 68 65  It uses only the
16f20 20 70 75 62 6c 69 63 0a 2a 2a 20 69 6e 74 65 72   public.** inter
16f30 66 61 63 65 20 64 65 66 69 6e 65 64 20 68 65 72  face defined her
16f40 65 2e 20 20 41 73 20 61 20 63 6f 6e 73 65 71 75  e.  As a consequ
16f50 65 6e 63 65 2c 20 65 72 72 6f 72 73 20 74 68 61  ence, errors tha
16f60 74 20 6f 63 63 75 72 20 69 6e 20 74 68 65 0a 2a  t occur in the.*
16f70 2a 20 77 72 61 70 70 65 72 20 6c 61 79 65 72 20  * wrapper layer 
16f80 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 20 69  outside of the i
16f90 6e 74 65 72 6e 61 6c 20 5b 73 71 6c 69 74 65 33  nternal [sqlite3
16fa0 5f 65 78 65 63 28 29 5d 20 63 61 6c 6c 20 61 72  _exec()] call ar
16fb0 65 20 6e 6f 74 0a 2a 2a 20 72 65 66 6c 65 63 74  e not.** reflect
16fc0 65 64 20 69 6e 20 73 75 62 73 65 71 75 65 6e 74  ed in subsequent
16fd0 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74   calls to [sqlit
16fe0 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 20 6f 72  e3_errcode()] or
16ff0 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67   [sqlite3_errmsg
17000 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ()]..**.** Requi
17010 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32  rements:.** [H12
17020 33 37 31 5d 20 5b 48 31 32 33 37 33 5d 20 5b 48  371] [H12373] [H
17030 31 32 33 37 34 5d 20 5b 48 31 32 33 37 36 5d 20  12374] [H12376] 
17040 5b 48 31 32 33 37 39 5d 20 5b 48 31 32 33 38 32  [H12379] [H12382
17050 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
17060 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  int sqlite3_get_
17070 74 61 62 6c 65 28 0a 20 20 73 71 6c 69 74 65 33  table(.  sqlite3
17080 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f   *db,          /
17090 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 61  * An open databa
170a0 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  se */.  const ch
170b0 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 2f 2a  ar *zSql,     /*
170c0 20 53 51 4c 20 74 6f 20 62 65 20 65 76 61 6c 75   SQL to be evalu
170d0 61 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ated */.  char *
170e0 2a 2a 70 61 7a 52 65 73 75 6c 74 2c 20 20 20 20  **pazResult,    
170f0 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 20 74 68  /* Results of th
17100 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74  e query */.  int
17110 20 2a 70 6e 52 6f 77 2c 20 20 20 20 20 20 20 20   *pnRow,        
17120 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
17130 72 65 73 75 6c 74 20 72 6f 77 73 20 77 72 69 74  result rows writ
17140 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e  ten here */.  in
17150 74 20 2a 70 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20  t *pnColumn,    
17160 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
17170 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20   result columns 
17180 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a  written here */.
17190 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 6d 73    char **pzErrms
171a0 67 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72  g       /* Error
171b0 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68 65 72   msg written her
171c0 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41  e */.);.SQLITE_A
171d0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
171e0 66 72 65 65 5f 74 61 62 6c 65 28 63 68 61 72 20  free_table(char 
171f0 2a 2a 72 65 73 75 6c 74 29 3b 0a 0a 2f 2a 0a 2a  **result);../*.*
17200 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6f 72 6d  * CAPI3REF: Form
17210 61 74 74 65 64 20 53 74 72 69 6e 67 20 50 72 69  atted String Pri
17220 6e 74 69 6e 67 20 46 75 6e 63 74 69 6f 6e 73 20  nting Functions 
17230 7b 48 31 37 34 30 30 7d 20 3c 53 37 30 30 30 30  {H17400} <S70000
17240 3e 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20  ><S20000>.**.** 
17250 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  These routines a
17260 72 65 20 77 6f 72 6b 2d 61 6c 69 6b 65 73 20 6f  re work-alikes o
17270 66 20 74 68 65 20 22 70 72 69 6e 74 66 28 29 22  f the "printf()"
17280 20 66 61 6d 69 6c 79 20 6f 66 20 66 75 6e 63 74   family of funct
17290 69 6f 6e 73 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  ions.** from the
172a0 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72   standard C libr
172b0 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ary..**.** The s
172c0 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29  qlite3_mprintf()
172d0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 6d 70   and sqlite3_vmp
172e0 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 73  rintf() routines
172f0 20 77 72 69 74 65 20 74 68 65 69 72 0a 2a 2a 20   write their.** 
17300 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 6d 65 6d  results into mem
17310 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
17320 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  m [sqlite3_mallo
17330 63 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 74 72  c()]..** The str
17340 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62 79  ings returned by
17350 20 74 68 65 73 65 20 74 77 6f 20 72 6f 75 74 69   these two routi
17360 6e 65 73 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a  nes should be.**
17370 20 72 65 6c 65 61 73 65 64 20 62 79 20 5b 73 71   released by [sq
17380 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 20 20  lite3_free()].  
17390 42 6f 74 68 20 72 6f 75 74 69 6e 65 73 20 72 65  Both routines re
173a0 74 75 72 6e 20 61 0a 2a 2a 20 4e 55 4c 4c 20 70  turn a.** NULL p
173b0 6f 69 6e 74 65 72 20 69 66 20 5b 73 71 6c 69 74  ointer if [sqlit
173c0 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 69 73 20  e3_malloc()] is 
173d0 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61  unable to alloca
173e0 74 65 20 65 6e 6f 75 67 68 0a 2a 2a 20 6d 65 6d  te enough.** mem
173f0 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ory to hold the 
17400 72 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e 67  resulting string
17410 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 73 71 6c 69 74  ..**.** In sqlit
17420 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 72 6f  e3_snprintf() ro
17430 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72  utine is similar
17440 20 74 6f 20 22 73 6e 70 72 69 6e 74 66 28 29 22   to "snprintf()"
17450 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 73 74 61   from.** the sta
17460 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e  ndard C library.
17470 20 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20    The result is 
17480 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
17490 0a 2a 2a 20 62 75 66 66 65 72 20 73 75 70 70 6c  .** buffer suppl
174a0 69 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  ied as the secon
174b0 64 20 70 61 72 61 6d 65 74 65 72 20 77 68 6f 73  d parameter whos
174c0 65 20 73 69 7a 65 20 69 73 20 67 69 76 65 6e 20  e size is given 
174d0 62 79 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20  by.** the first 
174e0 70 61 72 61 6d 65 74 65 72 2e 20 4e 6f 74 65 20  parameter. Note 
174f0 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 6f  that the order o
17500 66 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 74  f the.** first t
17510 77 6f 20 70 61 72 61 6d 65 74 65 72 73 20 69 73  wo parameters is
17520 20 72 65 76 65 72 73 65 64 20 66 72 6f 6d 20 73   reversed from s
17530 6e 70 72 69 6e 74 66 28 29 2e 20 20 54 68 69 73  nprintf().  This
17540 20 69 73 20 61 6e 0a 2a 2a 20 68 69 73 74 6f 72   is an.** histor
17550 69 63 61 6c 20 61 63 63 69 64 65 6e 74 20 74 68  ical accident th
17560 61 74 20 63 61 6e 6e 6f 74 20 62 65 20 66 69 78  at cannot be fix
17570 65 64 20 77 69 74 68 6f 75 74 20 62 72 65 61 6b  ed without break
17580 69 6e 67 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73  ing.** backwards
17590 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 20   compatibility. 
175a0 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20   Note also that 
175b0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
175c0 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20  ().** returns a 
175d0 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 20 62  pointer to its b
175e0 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66  uffer instead of
175f0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
17600 2a 20 63 68 61 72 61 63 74 65 72 73 20 61 63 74  * characters act
17610 75 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 6e  ually written in
17620 74 6f 20 74 68 65 20 62 75 66 66 65 72 2e 20 20  to the buffer.  
17630 57 65 20 61 64 6d 69 74 20 74 68 61 74 0a 2a 2a  We admit that.**
17640 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
17650 68 61 72 61 63 74 65 72 73 20 77 72 69 74 74 65  haracters writte
17660 6e 20 77 6f 75 6c 64 20 62 65 20 61 20 6d 6f 72  n would be a mor
17670 65 20 75 73 65 66 75 6c 20 72 65 74 75 72 6e 0a  e useful return.
17680 2a 2a 20 76 61 6c 75 65 20 62 75 74 20 77 65 20  ** value but we 
17690 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20 74 68  cannot change th
176a0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
176b0 20 6f 66 20 73 71 6c 69 74 65 33 5f 73 6e 70 72   of sqlite3_snpr
176c0 69 6e 74 66 28 29 0a 2a 2a 20 6e 6f 77 20 77 69  intf().** now wi
176d0 74 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 20 63  thout breaking c
176e0 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a  ompatibility..**
176f0 0a 2a 2a 20 41 73 20 6c 6f 6e 67 20 61 73 20 74  .** As long as t
17700 68 65 20 62 75 66 66 65 72 20 73 69 7a 65 20 69  he buffer size i
17710 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a  s greater than z
17720 65 72 6f 2c 20 73 71 6c 69 74 65 33 5f 73 6e 70  ero, sqlite3_snp
17730 72 69 6e 74 66 28 29 0a 2a 2a 20 67 75 61 72 61  rintf().** guara
17740 6e 74 65 65 73 20 74 68 61 74 20 74 68 65 20 62  ntees that the b
17750 75 66 66 65 72 20 69 73 20 61 6c 77 61 79 73 20  uffer is always 
17760 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e  zero-terminated.
17770 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a 20 70    The first.** p
17780 61 72 61 6d 65 74 65 72 20 22 6e 22 20 69 73 20  arameter "n" is 
17790 74 68 65 20 74 6f 74 61 6c 20 73 69 7a 65 20 6f  the total size o
177a0 66 20 74 68 65 20 62 75 66 66 65 72 2c 20 69 6e  f the buffer, in
177b0 63 6c 75 64 69 6e 67 20 73 70 61 63 65 20 66 6f  cluding space fo
177c0 72 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 20 74 65  r.** the zero te
177d0 72 6d 69 6e 61 74 6f 72 2e 20 20 53 6f 20 74 68  rminator.  So th
177e0 65 20 6c 6f 6e 67 65 73 74 20 73 74 72 69 6e 67  e longest string
177f0 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 6f 6d   that can be com
17800 70 6c 65 74 65 6c 79 0a 2a 2a 20 77 72 69 74 74  pletely.** writt
17810 65 6e 20 77 69 6c 6c 20 62 65 20 6e 2d 31 20 63  en will be n-1 c
17820 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a  haracters..**.**
17830 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
17840 61 6c 6c 20 69 6d 70 6c 65 6d 65 6e 74 20 73 6f  all implement so
17850 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 6f  me additional fo
17860 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 70 74 69  rmatting.** opti
17870 6f 6e 73 20 74 68 61 74 20 61 72 65 20 75 73 65  ons that are use
17880 66 75 6c 20 66 6f 72 20 63 6f 6e 73 74 72 75 63  ful for construc
17890 74 69 6e 67 20 53 51 4c 20 73 74 61 74 65 6d 65  ting SQL stateme
178a0 6e 74 73 2e 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74  nts..** All of t
178b0 68 65 20 75 73 75 61 6c 20 70 72 69 6e 74 66 28  he usual printf(
178c0 29 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74  ) formatting opt
178d0 69 6f 6e 73 20 61 70 70 6c 79 2e 20 20 49 6e 20  ions apply.  In 
178e0 61 64 64 69 74 69 6f 6e 2c 20 74 68 65 72 65 0a  addition, there.
178f0 2a 2a 20 69 73 20 61 72 65 20 22 25 71 22 2c 20  ** is are "%q", 
17900 22 25 51 22 2c 20 61 6e 64 20 22 25 7a 22 20 6f  "%Q", and "%z" o
17910 70 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ptions..**.** Th
17920 65 20 25 71 20 6f 70 74 69 6f 6e 20 77 6f 72 6b  e %q option work
17930 73 20 6c 69 6b 65 20 25 73 20 69 6e 20 74 68 61  s like %s in tha
17940 74 20 69 74 20 73 75 62 73 74 69 74 75 74 65 73  t it substitutes
17950 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74   a null-terminat
17960 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 66 72 6f  ed.** string fro
17970 6d 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 6c  m the argument l
17980 69 73 74 2e 20 20 42 75 74 20 25 71 20 61 6c 73  ist.  But %q als
17990 6f 20 64 6f 75 62 6c 65 73 20 65 76 65 72 79 20  o doubles every 
179a0 27 5c 27 27 20 63 68 61 72 61 63 74 65 72 2e 0a  '\'' character..
179b0 2a 2a 20 25 71 20 69 73 20 64 65 73 69 67 6e 65  ** %q is designe
179c0 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65  d for use inside
179d0 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61   a string litera
179e0 6c 2e 20 20 42 79 20 64 6f 75 62 6c 69 6e 67 20  l.  By doubling 
179f0 65 61 63 68 20 27 5c 27 27 0a 2a 2a 20 63 68 61  each '\''.** cha
17a00 72 61 63 74 65 72 20 69 74 20 65 73 63 61 70 65  racter it escape
17a10 73 20 74 68 61 74 20 63 68 61 72 61 63 74 65 72  s that character
17a20 20 61 6e 64 20 61 6c 6c 6f 77 73 20 69 74 20 74   and allows it t
17a30 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e  o be inserted in
17a40 74 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67  to.** the string
17a50 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d  ..**.** For exam
17a60 70 6c 65 2c 20 61 73 73 75 6d 65 20 74 68 65 20  ple, assume the 
17a70 73 74 72 69 6e 67 20 76 61 72 69 61 62 6c 65 20  string variable 
17a80 7a 54 65 78 74 20 63 6f 6e 74 61 69 6e 73 20 74  zText contains t
17a90 65 78 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  ext as follows:.
17aa0 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74  **.** <blockquot
17ab0 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72  e><pre>.**  char
17ac0 20 2a 7a 54 65 78 74 20 3d 20 22 49 74 27 73 20   *zText = "It's 
17ad0 61 20 68 61 70 70 79 20 64 61 79 21 22 3b 0a 2a  a happy day!";.*
17ae0 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71  * </pre></blockq
17af0 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20  uote>.**.** One 
17b00 63 61 6e 20 75 73 65 20 74 68 69 73 20 74 65 78  can use this tex
17b10 74 20 69 6e 20 61 6e 20 53 51 4c 20 73 74 61 74  t in an SQL stat
17b20 65 6d 65 6e 74 20 61 73 20 66 6f 6c 6c 6f 77 73  ement as follows
17b30 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75  :.**.** <blockqu
17b40 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68  ote><pre>.**  ch
17b50 61 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c 69 74  ar *zSQL = sqlit
17b60 65 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e 53 45  e3_mprintf("INSE
17b70 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 20 56 41  RT INTO table VA
17b80 4c 55 45 53 28 27 25 71 27 29 22 2c 20 7a 54 65  LUES('%q')", zTe
17b90 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33  xt);.**  sqlite3
17ba0 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c 2c 20  _exec(db, zSQL, 
17bb0 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 73 71  0, 0, 0);.**  sq
17bc0 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51 4c 29  lite3_free(zSQL)
17bd0 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f  ;.** </pre></blo
17be0 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 42  ckquote>.**.** B
17bf0 65 63 61 75 73 65 20 74 68 65 20 25 71 20 66 6f  ecause the %q fo
17c00 72 6d 61 74 20 73 74 72 69 6e 67 20 69 73 20 75  rmat string is u
17c10 73 65 64 2c 20 74 68 65 20 27 5c 27 27 20 63 68  sed, the '\'' ch
17c20 61 72 61 63 74 65 72 20 69 6e 20 7a 54 65 78 74  aracter in zText
17c30 0a 2a 2a 20 69 73 20 65 73 63 61 70 65 64 20 61  .** is escaped a
17c40 6e 64 20 74 68 65 20 53 51 4c 20 67 65 6e 65 72  nd the SQL gener
17c50 61 74 65 64 20 69 73 20 61 73 20 66 6f 6c 6c 6f  ated is as follo
17c60 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b  ws:.**.** <block
17c70 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20  quote><pre>.**  
17c80 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c  INSERT INTO tabl
17c90 65 31 20 56 41 4c 55 45 53 28 27 49 74 27 27 73  e1 VALUES('It''s
17ca0 20 61 20 68 61 70 70 79 20 64 61 79 21 27 29 0a   a happy day!').
17cb0 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b  ** </pre></block
17cc0 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  quote>.**.** Thi
17cd0 73 20 69 73 20 63 6f 72 72 65 63 74 2e 20 20 48  s is correct.  H
17ce0 61 64 20 77 65 20 75 73 65 64 20 25 73 20 69 6e  ad we used %s in
17cf0 73 74 65 61 64 20 6f 66 20 25 71 2c 20 74 68 65  stead of %q, the
17d00 20 67 65 6e 65 72 61 74 65 64 20 53 51 4c 0a 2a   generated SQL.*
17d10 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 6c 6f 6f  * would have loo
17d20 6b 65 64 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a  ked like this:.*
17d30 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65  *.** <blockquote
17d40 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52  ><pre>.**  INSER
17d50 54 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 56 41  T INTO table1 VA
17d60 4c 55 45 53 28 27 49 74 27 73 20 61 20 68 61 70  LUES('It's a hap
17d70 70 79 20 64 61 79 21 27 29 3b 0a 2a 2a 20 3c 2f  py day!');.** </
17d80 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65  pre></blockquote
17d90 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 65 63  >.**.** This sec
17da0 6f 6e 64 20 65 78 61 6d 70 6c 65 20 69 73 20 61  ond example is a
17db0 6e 20 53 51 4c 20 73 79 6e 74 61 78 20 65 72 72  n SQL syntax err
17dc0 6f 72 2e 20 20 41 73 20 61 20 67 65 6e 65 72 61  or.  As a genera
17dd0 6c 20 72 75 6c 65 20 79 6f 75 20 73 68 6f 75 6c  l rule you shoul
17de0 64 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73 65 20  d.** always use 
17df0 25 71 20 69 6e 73 74 65 61 64 20 6f 66 20 25 73  %q instead of %s
17e00 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20   when inserting 
17e10 74 65 78 74 20 69 6e 74 6f 20 61 20 73 74 72 69  text into a stri
17e20 6e 67 20 6c 69 74 65 72 61 6c 2e 0a 2a 2a 0a 2a  ng literal..**.*
17e30 2a 20 54 68 65 20 25 51 20 6f 70 74 69 6f 6e 20  * The %Q option 
17e40 77 6f 72 6b 73 20 6c 69 6b 65 20 25 71 20 65 78  works like %q ex
17e50 63 65 70 74 20 69 74 20 61 6c 73 6f 20 61 64 64  cept it also add
17e60 73 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 20  s single quotes 
17e70 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 65 20 6f 75  around.** the ou
17e80 74 73 69 64 65 20 6f 66 20 74 68 65 20 74 6f 74  tside of the tot
17e90 61 6c 20 73 74 72 69 6e 67 2e 20 20 41 64 64 69  al string.  Addi
17ea0 74 69 6f 6e 61 6c 6c 79 2c 20 69 66 20 74 68 65  tionally, if the
17eb0 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20 74 68   parameter in th
17ec0 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6c 69  e.** argument li
17ed0 73 74 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69  st is a NULL poi
17ee0 6e 74 65 72 2c 20 25 51 20 73 75 62 73 74 69 74  nter, %Q substit
17ef0 75 74 65 73 20 74 68 65 20 74 65 78 74 20 22 4e  utes the text "N
17f00 55 4c 4c 22 20 28 77 69 74 68 6f 75 74 0a 2a 2a  ULL" (without.**
17f10 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 29 20   single quotes) 
17f20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65 20  in place of the 
17f30 25 51 20 6f 70 74 69 6f 6e 2e 20 20 53 6f 2c 20  %Q option.  So, 
17f40 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 6f 6e 65  for example, one
17f50 20 63 6f 75 6c 64 20 73 61 79 3a 0a 2a 2a 0a 2a   could say:.**.*
17f60 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70  * <blockquote><p
17f70 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 53  re>.**  char *zS
17f80 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  QL = sqlite3_mpr
17f90 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49 4e 54  intf("INSERT INT
17fa0 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53 28 25  O table VALUES(%
17fb0 51 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a 2a 20  Q)", zText);.** 
17fc0 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62   sqlite3_exec(db
17fd0 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20 30 29  , zSQL, 0, 0, 0)
17fe0 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.**  sqlite3_fr
17ff0 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c 2f 70  ee(zSQL);.** </p
18000 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  re></blockquote>
18010 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  .**.** The code 
18020 61 62 6f 76 65 20 77 69 6c 6c 20 72 65 6e 64 65  above will rende
18030 72 20 61 20 63 6f 72 72 65 63 74 20 53 51 4c 20  r a correct SQL 
18040 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65  statement in the
18050 20 7a 53 51 4c 0a 2a 2a 20 76 61 72 69 61 62 6c   zSQL.** variabl
18060 65 20 65 76 65 6e 20 69 66 20 74 68 65 20 7a 54  e even if the zT
18070 65 78 74 20 76 61 72 69 61 62 6c 65 20 69 73 20  ext variable is 
18080 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a  a NULL pointer..
18090 2a 2a 0a 2a 2a 20 54 68 65 20 22 25 7a 22 20 66  **.** The "%z" f
180a0 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74 69 6f 6e  ormatting option
180b0 20 77 6f 72 6b 73 20 65 78 61 63 74 6c 79 20 6c   works exactly l
180c0 69 6b 65 20 22 25 73 22 20 77 69 74 68 20 74 68  ike "%s" with th
180d0 65 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 20 74 68  e.** addition th
180e0 61 74 20 61 66 74 65 72 20 74 68 65 20 73 74 72  at after the str
180f0 69 6e 67 20 68 61 73 20 62 65 65 6e 20 72 65 61  ing has been rea
18100 64 20 61 6e 64 20 63 6f 70 69 65 64 20 69 6e 74  d and copied int
18110 6f 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2c  o.** the result,
18120 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29   [sqlite3_free()
18130 5d 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74  ] is called on t
18140 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 2e  he input string.
18150 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 52 65 71   {END}.**.** Req
18160 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
18170 31 37 34 30 33 5d 20 5b 48 31 37 34 30 36 5d 20  17403] [H17406] 
18180 5b 48 31 37 34 30 37 5d 0a 2a 2f 0a 53 51 4c 49  [H17407].*/.SQLI
18190 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c  TE_API char *sql
181a0 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 63 6f 6e  ite3_mprintf(con
181b0 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 53  st char*,...);.S
181c0 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a  QLITE_API char *
181d0 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66  sqlite3_vmprintf
181e0 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76 61  (const char*, va
181f0 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f 41  _list);.SQLITE_A
18200 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  PI char *sqlite3
18210 5f 73 6e 70 72 69 6e 74 66 28 69 6e 74 2c 63 68  _snprintf(int,ch
18220 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
18230 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   ...);../*.** CA
18240 50 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41  PI3REF: Memory A
18250 6c 6c 6f 63 61 74 69 6f 6e 20 53 75 62 73 79 73  llocation Subsys
18260 74 65 6d 20 7b 48 31 37 33 30 30 7d 20 3c 53 32  tem {H17300} <S2
18270 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0000>.**.** The 
18280 53 51 4c 69 74 65 20 63 6f 72 65 20 20 75 73 65  SQLite core  use
18290 73 20 74 68 65 73 65 20 74 68 72 65 65 20 72 6f  s these three ro
182a0 75 74 69 6e 65 73 20 66 6f 72 20 61 6c 6c 20 6f  utines for all o
182b0 66 20 69 74 73 20 6f 77 6e 0a 2a 2a 20 69 6e 74  f its own.** int
182c0 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  ernal memory all
182d0 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 22  ocation needs. "
182e0 43 6f 72 65 22 20 69 6e 20 74 68 65 20 70 72 65  Core" in the pre
182f0 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 0a 2a  vious sentence.*
18300 2a 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75  * does not inclu
18310 64 65 20 6f 70 65 72 61 74 69 6e 67 2d 73 79 73  de operating-sys
18320 74 65 6d 20 73 70 65 63 69 66 69 63 20 56 46 53  tem specific VFS
18330 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e   implementation.
18340 20 20 54 68 65 0a 2a 2a 20 57 69 6e 64 6f 77 73    The.** Windows
18350 20 56 46 53 20 75 73 65 73 20 6e 61 74 69 76 65   VFS uses native
18360 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72   malloc() and fr
18370 65 65 28 29 20 66 6f 72 20 73 6f 6d 65 20 6f 70  ee() for some op
18380 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20  erations..**.** 
18390 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  The sqlite3_mall
183a0 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74  oc() routine ret
183b0 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  urns a pointer t
183c0 6f 20 61 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20  o a block.** of 
183d0 6d 65 6d 6f 72 79 20 61 74 20 6c 65 61 73 74 20  memory at least 
183e0 4e 20 62 79 74 65 73 20 69 6e 20 6c 65 6e 67 74  N bytes in lengt
183f0 68 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68  h, where N is th
18400 65 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20  e parameter..** 
18410 49 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  If sqlite3_mallo
18420 63 28 29 20 69 73 20 75 6e 61 62 6c 65 20 74 6f  c() is unable to
18430 20 6f 62 74 61 69 6e 20 73 75 66 66 69 63 69 65   obtain sufficie
18440 6e 74 20 66 72 65 65 0a 2a 2a 20 6d 65 6d 6f 72  nt free.** memor
18450 79 2c 20 69 74 20 72 65 74 75 72 6e 73 20 61 20  y, it returns a 
18460 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 20 49  NULL pointer.  I
18470 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20  f the parameter 
18480 4e 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  N to.** sqlite3_
18490 6d 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f  malloc() is zero
184a0 20 6f 72 20 6e 65 67 61 74 69 76 65 20 74 68 65   or negative the
184b0 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  n sqlite3_malloc
184c0 28 29 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 20  () returns.** a 
184d0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a  NULL pointer..**
184e0 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 73 71 6c 69  .** Calling sqli
184f0 74 65 33 5f 66 72 65 65 28 29 20 77 69 74 68 20  te3_free() with 
18500 61 20 70 6f 69 6e 74 65 72 20 70 72 65 76 69 6f  a pointer previo
18510 75 73 6c 79 20 72 65 74 75 72 6e 65 64 0a 2a 2a  usly returned.**
18520 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   by sqlite3_mall
18530 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f  oc() or sqlite3_
18540 72 65 61 6c 6c 6f 63 28 29 20 72 65 6c 65 61 73  realloc() releas
18550 65 73 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 73  es that memory s
18560 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 6d 69 67  o.** that it mig
18570 68 74 20 62 65 20 72 65 75 73 65 64 2e 20 20 54  ht be reused.  T
18580 68 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  he sqlite3_free(
18590 29 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a 2a 20  ) routine is.** 
185a0 61 20 6e 6f 2d 6f 70 20 69 66 20 69 73 20 63 61  a no-op if is ca
185b0 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c  lled with a NULL
185c0 20 70 6f 69 6e 74 65 72 2e 20 20 50 61 73 73 69   pointer.  Passi
185d0 6e 67 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  ng a NULL pointe
185e0 72 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f  r.** to sqlite3_
185f0 66 72 65 65 28 29 20 69 73 20 68 61 72 6d 6c 65  free() is harmle
18600 73 73 2e 20 20 41 66 74 65 72 20 62 65 69 6e 67  ss.  After being
18610 20 66 72 65 65 64 2c 20 6d 65 6d 6f 72 79 0a 2a   freed, memory.*
18620 2a 20 73 68 6f 75 6c 64 20 6e 65 69 74 68 65 72  * should neither
18630 20 62 65 20 72 65 61 64 20 6e 6f 72 20 77 72 69   be read nor wri
18640 74 74 65 6e 2e 20 20 45 76 65 6e 20 72 65 61 64  tten.  Even read
18650 69 6e 67 20 70 72 65 76 69 6f 75 73 6c 79 20 66  ing previously f
18660 72 65 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6d  reed.** memory m
18670 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e 20 61  ight result in a
18680 20 73 65 67 6d 65 6e 74 61 74 69 6f 6e 20 66 61   segmentation fa
18690 75 6c 74 20 6f 72 20 6f 74 68 65 72 20 73 65 76  ult or other sev
186a0 65 72 65 20 65 72 72 6f 72 2e 0a 2a 2a 20 4d 65  ere error..** Me
186b0 6d 6f 72 79 20 63 6f 72 72 75 70 74 69 6f 6e 2c  mory corruption,
186c0 20 61 20 73 65 67 6d 65 6e 74 61 74 69 6f 6e 20   a segmentation 
186d0 66 61 75 6c 74 2c 20 6f 72 20 6f 74 68 65 72 20  fault, or other 
186e0 73 65 76 65 72 65 20 65 72 72 6f 72 0a 2a 2a 20  severe error.** 
186f0 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69 66 20  might result if 
18700 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69  sqlite3_free() i
18710 73 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20  s called with a 
18720 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  non-NULL pointer
18730 20 74 68 61 74 0a 2a 2a 20 77 61 73 20 6e 6f 74   that.** was not
18740 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
18750 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
18760 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  or sqlite3_reall
18770 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  oc()..**.** The 
18780 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
18790 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65  ) interface atte
187a0 6d 70 74 73 20 74 6f 20 72 65 73 69 7a 65 20 61  mpts to resize a
187b0 0a 2a 2a 20 70 72 69 6f 72 20 6d 65 6d 6f 72 79  .** prior memory
187c0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62   allocation to b
187d0 65 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74  e at least N byt
187e0 65 73 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74  es, where N is t
187f0 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72  he.** second par
18800 61 6d 65 74 65 72 2e 20 20 54 68 65 20 6d 65 6d  ameter.  The mem
18810 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74  ory allocation t
18820 6f 20 62 65 20 72 65 73 69 7a 65 64 20 69 73 20  o be resized is 
18830 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 61 72  the first.** par
18840 61 6d 65 74 65 72 2e 20 20 49 66 20 74 68 65 20  ameter.  If the 
18850 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
18860 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  to sqlite3_reall
18870 6f 63 28 29 0a 2a 2a 20 69 73 20 61 20 4e 55 4c  oc().** is a NUL
18880 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20 69  L pointer then i
18890 74 73 20 62 65 68 61 76 69 6f 72 20 69 73 20 69  ts behavior is i
188a0 64 65 6e 74 69 63 61 6c 20 74 6f 20 63 61 6c 6c  dentical to call
188b0 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d  ing.** sqlite3_m
188c0 61 6c 6c 6f 63 28 4e 29 20 77 68 65 72 65 20 4e  alloc(N) where N
188d0 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70   is the second p
188e0 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69  arameter to sqli
188f0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a  te3_realloc()..*
18900 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20  * If the second 
18910 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
18920 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69  ite3_realloc() i
18930 73 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65 67  s zero or.** neg
18940 61 74 69 76 65 20 74 68 65 6e 20 74 68 65 20 62  ative then the b
18950 65 68 61 76 69 6f 72 20 69 73 20 65 78 61 63 74  ehavior is exact
18960 6c 79 20 74 68 65 20 73 61 6d 65 20 61 73 20 63  ly the same as c
18970 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65  alling.** sqlite
18980 33 5f 66 72 65 65 28 50 29 20 77 68 65 72 65 20  3_free(P) where 
18990 50 20 69 73 20 74 68 65 20 66 69 72 73 74 20 70  P is the first p
189a0 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69  arameter to sqli
189b0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a  te3_realloc()..*
189c0 2a 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  * sqlite3_reallo
189d0 63 28 29 20 72 65 74 75 72 6e 73 20 61 20 70 6f  c() returns a po
189e0 69 6e 74 65 72 20 74 6f 20 61 20 6d 65 6d 6f 72  inter to a memor
189f0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20  y allocation.** 
18a00 6f 66 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79  of at least N by
18a10 74 65 73 20 69 6e 20 73 69 7a 65 20 6f 72 20 4e  tes in size or N
18a20 55 4c 4c 20 69 66 20 73 75 66 66 69 63 69 65 6e  ULL if sufficien
18a30 74 20 6d 65 6d 6f 72 79 20 69 73 20 75 6e 61 76  t memory is unav
18a40 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 49 66 20 4d  ailable..** If M
18a50 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20   is the size of 
18a60 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61  the prior alloca
18a70 74 69 6f 6e 2c 20 74 68 65 6e 20 6d 69 6e 28 4e  tion, then min(N
18a80 2c 4d 29 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20  ,M) bytes.** of 
18a90 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61  the prior alloca
18aa0 74 69 6f 6e 20 61 72 65 20 63 6f 70 69 65 64 20  tion are copied 
18ab0 69 6e 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69  into the beginni
18ac0 6e 67 20 6f 66 20 62 75 66 66 65 72 20 72 65 74  ng of buffer ret
18ad0 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 6c 69  urned.** by sqli
18ae0 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 61 6e  te3_realloc() an
18af0 64 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f  d the prior allo
18b00 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 64 2e  cation is freed.
18b10 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 72  .** If sqlite3_r
18b20 65 61 6c 6c 6f 63 28 29 20 72 65 74 75 72 6e 73  ealloc() returns
18b30 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20   NULL, then the 
18b40 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e  prior allocation
18b50 0a 2a 2a 20 69 73 20 6e 6f 74 20 66 72 65 65 64  .** is not freed
18b60 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f  ..**.** The memo
18b70 72 79 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  ry returned by s
18b80 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
18b90 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  and sqlite3_real
18ba0 6c 6f 63 28 29 0a 2a 2a 20 69 73 20 61 6c 77 61  loc().** is alwa
18bb0 79 73 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 74  ys aligned to at
18bc0 20 6c 65 61 73 74 20 61 6e 20 38 20 62 79 74 65   least an 8 byte
18bd0 20 62 6f 75 6e 64 61 72 79 2e 20 7b 45 4e 44 7d   boundary. {END}
18be0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75  .**.** The defau
18bf0 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  lt implementatio
18c00 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20  n of the memory 
18c10 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
18c20 73 74 65 6d 20 75 73 65 73 0a 2a 2a 20 74 68 65  stem uses.** the
18c30 20 6d 61 6c 6c 6f 63 28 29 2c 20 72 65 61 6c 6c   malloc(), reall
18c40 6f 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20  oc() and free() 
18c50 70 72 6f 76 69 64 65 64 20 62 79 20 74 68 65 20  provided by the 
18c60 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61  standard C libra
18c70 72 79 2e 0a 2a 2a 20 7b 48 31 37 33 38 32 7d 20  ry..** {H17382} 
18c80 48 6f 77 65 76 65 72 2c 20 69 66 20 53 51 4c 69  However, if SQLi
18c90 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77  te is compiled w
18ca0 69 74 68 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54  ith the.** SQLIT
18cb0 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 3d 3c 69  E_MEMORY_SIZE=<i
18cc0 3e 4e 4e 4e 3c 2f 69 3e 20 43 20 70 72 65 70 72  >NNN</i> C prepr
18cd0 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 28 77  ocessor macro (w
18ce0 68 65 72 65 20 3c 69 3e 4e 4e 4e 3c 2f 69 3e 0a  here <i>NNN</i>.
18cf0 2a 2a 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  ** is an integer
18d00 29 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 63  ), then SQLite c
18d10 72 65 61 74 65 20 61 20 73 74 61 74 69 63 20 61  reate a static a
18d20 72 72 61 79 20 6f 66 20 61 74 20 6c 65 61 73 74  rray of at least
18d30 0a 2a 2a 20 3c 69 3e 4e 4e 4e 3c 2f 69 3e 20 62  .** <i>NNN</i> b
18d40 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61 6e 64  ytes in size and
18d50 20 75 73 65 73 20 74 68 61 74 20 61 72 72 61 79   uses that array
18d60 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20   for all of its 
18d70 64 79 6e 61 6d 69 63 0a 2a 2a 20 6d 65 6d 6f 72  dynamic.** memor
18d80 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65  y allocation nee
18d90 64 73 2e 20 7b 45 4e 44 7d 20 20 41 64 64 69 74  ds. {END}  Addit
18da0 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  ional memory all
18db0 6f 63 61 74 6f 72 20 6f 70 74 69 6f 6e 73 0a 2a  ocator options.*
18dc0 2a 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69  * may be added i
18dd0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
18de0 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 53 51 4c 69  s..**.** In SQLi
18df0 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30  te version 3.5.0
18e00 20 61 6e 64 20 33 2e 35 2e 31 2c 20 69 74 20 77   and 3.5.1, it w
18e10 61 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 64  as possible to d
18e20 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53 51 4c  efine.** the SQL
18e30 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 5f  ITE_OMIT_MEMORY_
18e40 41 4c 4c 4f 43 41 54 49 4f 4e 20 77 68 69 63 68  ALLOCATION which
18e50 20 77 6f 75 6c 64 20 63 61 75 73 65 20 74 68 65   would cause the
18e60 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 69 6d 70   built-in.** imp
18e70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
18e80 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f  hese routines to
18e90 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 54 68   be omitted.  Th
18ea0 61 74 20 63 61 70 61 62 69 6c 69 74 79 0a 2a 2a  at capability.**
18eb0 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 70 72   is no longer pr
18ec0 6f 76 69 64 65 64 2e 20 20 4f 6e 6c 79 20 62 75  ovided.  Only bu
18ed0 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c  ilt-in memory al
18ee0 6c 6f 63 61 74 6f 72 73 20 63 61 6e 20 62 65 20  locators can be 
18ef0 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  used..**.** The 
18f00 57 69 6e 64 6f 77 73 20 4f 53 20 69 6e 74 65 72  Windows OS inter
18f10 66 61 63 65 20 6c 61 79 65 72 20 63 61 6c 6c 73  face layer calls
18f20 0a 2a 2a 20 74 68 65 20 73 79 73 74 65 6d 20 6d  .** the system m
18f30 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72 65 65  alloc() and free
18f40 28 29 20 64 69 72 65 63 74 6c 79 20 77 68 65 6e  () directly when
18f50 20 63 6f 6e 76 65 72 74 69 6e 67 0a 2a 2a 20 66   converting.** f
18f60 69 6c 65 6e 61 6d 65 73 20 62 65 74 77 65 65 6e  ilenames between
18f70 20 74 68 65 20 55 54 46 2d 38 20 65 6e 63 6f 64   the UTF-8 encod
18f80 69 6e 67 20 75 73 65 64 20 62 79 20 53 51 4c 69  ing used by SQLi
18f90 74 65 0a 2a 2a 20 61 6e 64 20 77 68 61 74 65 76  te.** and whatev
18fa0 65 72 20 66 69 6c 65 6e 61 6d 65 20 65 6e 63 6f  er filename enco
18fb0 64 69 6e 67 20 69 73 20 75 73 65 64 20 62 79 20  ding is used by 
18fc0 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 57  the particular W
18fd0 69 6e 64 6f 77 73 0a 2a 2a 20 69 6e 73 74 61 6c  indows.** instal
18fe0 6c 61 74 69 6f 6e 2e 20 20 4d 65 6d 6f 72 79 20  lation.  Memory 
18ff0 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
19000 73 20 61 72 65 20 64 65 74 65 63 74 65 64 2c 20  s are detected, 
19010 62 75 74 0a 2a 2a 20 74 68 65 79 20 61 72 65 20  but.** they are 
19020 72 65 70 6f 72 74 65 64 20 62 61 63 6b 20 61 73  reported back as
19030 20 5b 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45   [SQLITE_CANTOPE
19040 4e 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45  N] or.** [SQLITE
19050 5f 49 4f 45 52 52 5d 20 72 61 74 68 65 72 20 74  _IOERR] rather t
19060 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45  han [SQLITE_NOME
19070 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  M]..**.** Requir
19080 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 33  ements:.** [H173
19090 30 33 5d 20 5b 48 31 37 33 30 34 5d 20 5b 48 31  03] [H17304] [H1
190a0 37 33 30 35 5d 20 5b 48 31 37 33 30 36 5d 20 5b  7305] [H17306] [
190b0 48 31 37 33 31 30 5d 20 5b 48 31 37 33 31 32 5d  H17310] [H17312]
190c0 20 5b 48 31 37 33 31 35 5d 20 5b 48 31 37 33 31   [H17315] [H1731
190d0 38 5d 0a 2a 2a 20 5b 48 31 37 33 32 31 5d 20 5b  8].** [H17321] [
190e0 48 31 37 33 32 32 5d 20 5b 48 31 37 33 32 33 5d  H17322] [H17323]
190f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74  .**.** The point
19100 65 72 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  er arguments to 
19110 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d  [sqlite3_free()]
19120 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 65   and [sqlite3_re
19130 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 6d 75 73 74  alloc()].** must
19140 20 62 65 20 65 69 74 68 65 72 20 4e 55 4c 4c 20   be either NULL 
19150 6f 72 20 65 6c 73 65 20 70 6f 69 6e 74 65 72 73  or else pointers
19160 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61   obtained from a
19170 20 70 72 69 6f 72 0a 2a 2a 20 69 6e 76 6f 63 61   prior.** invoca
19180 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33  tion of [sqlite3
19190 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73  _malloc()] or [s
191a0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29  qlite3_realloc()
191b0 5d 20 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 6e  ] that have.** n
191c0 6f 74 20 79 65 74 20 62 65 65 6e 20 72 65 6c 65  ot yet been rele
191d0 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ased..**.** The 
191e0 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74  application must
191f0 20 6e 6f 74 20 72 65 61 64 20 6f 72 20 77 72 69   not read or wri
19200 74 65 20 61 6e 79 20 70 61 72 74 20 6f 66 0a 2a  te any part of.*
19210 2a 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d  * a block of mem
19220 6f 72 79 20 61 66 74 65 72 20 69 74 20 68 61 73  ory after it has
19230 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 20 75   been released u
19240 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  sing.** [sqlite3
19250 5f 66 72 65 65 28 29 5d 20 6f 72 20 5b 73 71 6c  _free()] or [sql
19260 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 2e  ite3_realloc()].
19270 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
19280 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 6d 61 6c  oid *sqlite3_mal
19290 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45  loc(int);.SQLITE
192a0 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
192b0 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f 69 64 2a  e3_realloc(void*
192c0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41  , int);.SQLITE_A
192d0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
192e0 66 72 65 65 28 76 6f 69 64 2a 29 3b 0a 0a 2f 2a  free(void*);../*
192f0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 65  .** CAPI3REF: Me
19300 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 6f 72 20 53  mory Allocator S
19310 74 61 74 69 73 74 69 63 73 20 7b 48 31 37 33 37  tatistics {H1737
19320 30 7d 20 3c 53 33 30 32 31 30 3e 0a 2a 2a 0a 2a  0} <S30210>.**.*
19330 2a 20 53 51 4c 69 74 65 20 70 72 6f 76 69 64 65  * SQLite provide
19340 73 20 74 68 65 73 65 20 74 77 6f 20 69 6e 74 65  s these two inte
19350 72 66 61 63 65 73 20 66 6f 72 20 72 65 70 6f 72  rfaces for repor
19360 74 69 6e 67 20 6f 6e 20 74 68 65 20 73 74 61 74  ting on the stat
19370 75 73 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71  us.** of the [sq
19380 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c  lite3_malloc()],
19390 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29   [sqlite3_free()
193a0 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  ], and [sqlite3_
193b0 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 72 6f  realloc()].** ro
193c0 75 74 69 6e 65 73 2c 20 77 68 69 63 68 20 66 6f  utines, which fo
193d0 72 6d 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  rm the built-in 
193e0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
193f0 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2a 0a  n subsystem..**.
19400 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
19410 0a 2a 2a 20 5b 48 31 37 33 37 31 5d 20 5b 48 31  .** [H17371] [H1
19420 37 33 37 33 5d 20 5b 48 31 37 33 37 34 5d 20 5b  7373] [H17374] [
19430 48 31 37 33 37 35 5d 0a 2a 2f 0a 53 51 4c 49 54  H17375].*/.SQLIT
19440 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e  E_API sqlite3_in
19450 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  t64 sqlite3_memo
19460 72 79 5f 75 73 65 64 28 76 6f 69 64 29 3b 0a 53  ry_used(void);.S
19470 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
19480 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  3_int64 sqlite3_
19490 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72  memory_highwater
194a0 28 69 6e 74 20 72 65 73 65 74 46 6c 61 67 29 3b  (int resetFlag);
194b0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
194c0 3a 20 50 73 65 75 64 6f 2d 52 61 6e 64 6f 6d 20  : Pseudo-Random 
194d0 4e 75 6d 62 65 72 20 47 65 6e 65 72 61 74 6f 72  Number Generator
194e0 20 7b 48 31 37 33 39 30 7d 20 3c 53 32 30 30 30   {H17390} <S2000
194f0 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  0>.**.** SQLite 
19500 63 6f 6e 74 61 69 6e 73 20 61 20 68 69 67 68 2d  contains a high-
19510 71 75 61 6c 69 74 79 20 70 73 65 75 64 6f 2d 72  quality pseudo-r
19520 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e  andom number gen
19530 65 72 61 74 6f 72 20 28 50 52 4e 47 29 20 75 73  erator (PRNG) us
19540 65 64 20 74 6f 0a 2a 2a 20 73 65 6c 65 63 74 20  ed to.** select 
19550 72 61 6e 64 6f 6d 20 5b 52 4f 57 49 44 20 7c 20  random [ROWID | 
19560 52 4f 57 49 44 73 5d 20 77 68 65 6e 20 69 6e 73  ROWIDs] when ins
19570 65 72 74 69 6e 67 20 6e 65 77 20 72 65 63 6f 72  erting new recor
19580 64 73 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20  ds into a table 
19590 74 68 61 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20  that.** already 
195a0 75 73 65 73 20 74 68 65 20 6c 61 72 67 65 73 74  uses the largest
195b0 20 70 6f 73 73 69 62 6c 65 20 5b 52 4f 57 49 44   possible [ROWID
195c0 5d 2e 20 20 54 68 65 20 50 52 4e 47 20 69 73 20  ].  The PRNG is 
195d0 61 6c 73 6f 20 75 73 65 64 20 66 6f 72 0a 2a 2a  also used for.**
195e0 20 74 68 65 20 62 75 69 6c 64 2d 69 6e 20 72 61   the build-in ra
195f0 6e 64 6f 6d 28 29 20 61 6e 64 20 72 61 6e 64 6f  ndom() and rando
19600 6d 62 6c 6f 62 28 29 20 53 51 4c 20 66 75 6e 63  mblob() SQL func
19610 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 69 6e 74  tions.  This int
19620 65 72 66 61 63 65 20 61 6c 6c 6f 77 73 0a 2a 2a  erface allows.**
19630 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 6f   applications to
19640 20 61 63 63 65 73 73 20 74 68 65 20 73 61 6d 65   access the same
19650 20 50 52 4e 47 20 66 6f 72 20 6f 74 68 65 72 20   PRNG for other 
19660 70 75 72 70 6f 73 65 73 2e 0a 2a 2a 0a 2a 2a 20  purposes..**.** 
19670 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 72  A call to this r
19680 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 4e 20  outine stores N 
19690 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e  bytes of randomn
196a0 65 73 73 20 69 6e 74 6f 20 62 75 66 66 65 72 20  ess into buffer 
196b0 50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  P..**.** The fir
196c0 73 74 20 74 69 6d 65 20 74 68 69 73 20 72 6f 75  st time this rou
196d0 74 69 6e 65 20 69 73 20 69 6e 76 6f 6b 65 64 20  tine is invoked 
196e0 28 65 69 74 68 65 72 20 69 6e 74 65 72 6e 61 6c  (either internal
196f0 6c 79 20 6f 72 20 62 79 0a 2a 2a 20 74 68 65 20  ly or by.** the 
19700 61 70 70 6c 69 63 61 74 69 6f 6e 29 20 74 68 65  application) the
19710 20 50 52 4e 47 20 69 73 20 73 65 65 64 65 64 20   PRNG is seeded 
19720 75 73 69 6e 67 20 72 61 6e 64 6f 6d 6e 65 73 73  using randomness
19730 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f   obtained.** fro
19740 6d 20 74 68 65 20 78 52 61 6e 64 6f 6d 6e 65 73  m the xRandomnes
19750 73 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20  s method of the 
19760 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33  default [sqlite3
19770 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a  _vfs] object..**
19780 20 4f 6e 20 61 6c 6c 20 73 75 62 73 65 71 75 65   On all subseque
19790 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2c 20  nt invocations, 
197a0 74 68 65 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f  the pseudo-rando
197b0 6d 6e 65 73 73 20 69 73 20 67 65 6e 65 72 61 74  mness is generat
197c0 65 64 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 6c 79  ed.** internally
197d0 20 61 6e 64 20 77 69 74 68 6f 75 74 20 72 65 63   and without rec
197e0 6f 75 72 73 65 20 74 6f 20 74 68 65 20 5b 73 71  ourse to the [sq
197f0 6c 69 74 65 33 5f 76 66 73 5d 20 78 52 61 6e 64  lite3_vfs] xRand
19800 6f 6d 6e 65 73 73 0a 2a 2a 20 6d 65 74 68 6f 64  omness.** method
19810 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
19820 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 33 39 32  ents:.** [H17392
19830 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
19840 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 61 6e  void sqlite3_ran
19850 64 6f 6d 6e 65 73 73 28 69 6e 74 20 4e 2c 20 76  domness(int N, v
19860 6f 69 64 20 2a 50 29 3b 0a 0a 2f 2a 0a 2a 2a 20  oid *P);../*.** 
19870 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 69 6c  CAPI3REF: Compil
19880 65 2d 54 69 6d 65 20 41 75 74 68 6f 72 69 7a 61  e-Time Authoriza
19890 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b  tion Callbacks {
198a0 48 31 32 35 30 30 7d 20 3c 53 37 30 31 30 30 3e  H12500} <S70100>
198b0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
198c0 69 6e 65 20 72 65 67 69 73 74 65 72 73 20 61 20  ine registers a 
198d0 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62  authorizer callb
198e0 61 63 6b 20 77 69 74 68 20 61 20 70 61 72 74 69  ack with a parti
198f0 63 75 6c 61 72 0a 2a 2a 20 5b 64 61 74 61 62 61  cular.** [databa
19900 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2c 20  se connection], 
19910 73 75 70 70 6c 69 65 64 20 69 6e 20 74 68 65 20  supplied in the 
19920 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a  first argument..
19930 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65  ** The authorize
19940 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e  r callback is in
19950 76 6f 6b 65 64 20 61 73 20 53 51 4c 20 73 74 61  voked as SQL sta
19960 74 65 6d 65 6e 74 73 20 61 72 65 20 62 65 69 6e  tements are bein
19970 67 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 62 79  g compiled.** by
19980 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
19990 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72 69  e()] or its vari
199a0 61 6e 74 73 20 5b 73 71 6c 69 74 65 33 5f 70 72  ants [sqlite3_pr
199b0 65 70 61 72 65 5f 76 32 28 29 5d 2c 0a 2a 2a 20  epare_v2()],.** 
199c0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
199d0 31 36 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  16()] and [sqlit
199e0 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28  e3_prepare16_v2(
199f0 29 5d 2e 20 20 41 74 20 76 61 72 69 6f 75 73 0a  )].  At various.
19a00 2a 2a 20 70 6f 69 6e 74 73 20 64 75 72 69 6e 67  ** points during
19a10 20 74 68 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e   the compilation
19a20 20 70 72 6f 63 65 73 73 2c 20 61 73 20 6c 6f 67   process, as log
19a30 69 63 20 69 73 20 62 65 69 6e 67 20 63 72 65 61  ic is being crea
19a40 74 65 64 0a 2a 2a 20 74 6f 20 70 65 72 66 6f 72  ted.** to perfor
19a50 6d 20 76 61 72 69 6f 75 73 20 61 63 74 69 6f 6e  m various action
19a60 73 2c 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65  s, the authorize
19a70 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e  r callback is in
19a80 76 6f 6b 65 64 20 74 6f 0a 2a 2a 20 73 65 65 20  voked to.** see 
19a90 69 66 20 74 68 6f 73 65 20 61 63 74 69 6f 6e 73  if those actions
19aa0 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 20 20 54   are allowed.  T
19ab0 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61  he authorizer ca
19ac0 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 0a 2a 2a  llback should.**
19ad0 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f   return [SQLITE_
19ae0 4f 4b 5d 20 74 6f 20 61 6c 6c 6f 77 20 74 68 65  OK] to allow the
19af0 20 61 63 74 69 6f 6e 2c 20 5b 53 51 4c 49 54 45   action, [SQLITE
19b00 5f 49 47 4e 4f 52 45 5d 20 74 6f 20 64 69 73 61  _IGNORE] to disa
19b10 6c 6c 6f 77 20 74 68 65 0a 2a 2a 20 73 70 65 63  llow the.** spec
19b20 69 66 69 63 20 61 63 74 69 6f 6e 20 62 75 74 20  ific action but 
19b30 61 6c 6c 6f 77 20 74 68 65 20 53 51 4c 20 73 74  allow the SQL st
19b40 61 74 65 6d 65 6e 74 20 74 6f 20 63 6f 6e 74 69  atement to conti
19b50 6e 75 65 20 74 6f 20 62 65 0a 2a 2a 20 63 6f 6d  nue to be.** com
19b60 70 69 6c 65 64 2c 20 6f 72 20 5b 53 51 4c 49 54  piled, or [SQLIT
19b70 45 5f 44 45 4e 59 5d 20 74 6f 20 63 61 75 73 65  E_DENY] to cause
19b80 20 74 68 65 20 65 6e 74 69 72 65 20 53 51 4c 20   the entire SQL 
19b90 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 0a  statement to be.
19ba0 2a 2a 20 72 65 6a 65 63 74 65 64 20 77 69 74 68  ** rejected with
19bb0 20 61 6e 20 65 72 72 6f 72 2e 20 20 49 66 20 74   an error.  If t
19bc0 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61  he authorizer ca
19bd0 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 0a 2a  llback returns.*
19be0 2a 20 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 65  * any value othe
19bf0 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 49  r than [SQLITE_I
19c00 47 4e 4f 52 45 5d 2c 20 5b 53 51 4c 49 54 45 5f  GNORE], [SQLITE_
19c10 4f 4b 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f  OK], or [SQLITE_
19c20 44 45 4e 59 5d 0a 2a 2a 20 74 68 65 6e 20 74 68  DENY].** then th
19c30 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  e [sqlite3_prepa
19c40 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 75 69  re_v2()] or equi
19c50 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61 74  valent call that
19c60 20 74 72 69 67 67 65 72 65 64 0a 2a 2a 20 74 68   triggered.** th
19c70 65 20 61 75 74 68 6f 72 69 7a 65 72 20 77 69 6c  e authorizer wil
19c80 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 20 65  l fail with an e
19c90 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a  rror message..**
19ca0 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 61 6c  .** When the cal
19cb0 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53  lback returns [S
19cc0 51 4c 49 54 45 5f 4f 4b 5d 2c 20 74 68 61 74 20  QLITE_OK], that 
19cd0 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72 61 74  means the operat
19ce0 69 6f 6e 0a 2a 2a 20 72 65 71 75 65 73 74 65 64  ion.** requested
19cf0 20 69 73 20 6f 6b 2e 20 20 57 68 65 6e 20 74 68   is ok.  When th
19d00 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  e callback retur
19d10 6e 73 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d  ns [SQLITE_DENY]
19d20 2c 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65  , the.** [sqlite
19d30 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20  3_prepare_v2()] 
19d40 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 20 63 61  or equivalent ca
19d50 6c 6c 20 74 68 61 74 20 74 72 69 67 67 65 72 65  ll that triggere
19d60 64 20 74 68 65 0a 2a 2a 20 61 75 74 68 6f 72 69  d the.** authori
19d70 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c 20 77 69  zer will fail wi
19d80 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  th an error mess
19d90 61 67 65 20 65 78 70 6c 61 69 6e 69 6e 67 20 74  age explaining t
19da0 68 61 74 0a 2a 2a 20 61 63 63 65 73 73 20 69 73  hat.** access is
19db0 20 64 65 6e 69 65 64 2e 20 0a 2a 2a 0a 2a 2a 20   denied. .**.** 
19dc0 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  The first parame
19dd0 74 65 72 20 74 6f 20 74 68 65 20 61 75 74 68 6f  ter to the autho
19de0 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69  rizer callback i
19df0 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
19e00 74 68 69 72 64 0a 2a 2a 20 70 61 72 61 6d 65 74  third.** paramet
19e10 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65  er to the sqlite
19e20 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
19e30 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20 54 68  () interface. Th
19e40 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
19e50 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c  er.** to the cal
19e60 6c 62 61 63 6b 20 69 73 20 61 6e 20 69 6e 74 65  lback is an inte
19e70 67 65 72 20 5b 53 51 4c 49 54 45 5f 43 4f 50 59  ger [SQLITE_COPY
19e80 20 7c 20 61 63 74 69 6f 6e 20 63 6f 64 65 5d 20   | action code] 
19e90 74 68 61 74 20 73 70 65 63 69 66 69 65 73 0a 2a  that specifies.*
19ea0 2a 20 74 68 65 20 70 61 72 74 69 63 75 6c 61 72  * the particular
19eb0 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 75   action to be au
19ec0 74 68 6f 72 69 7a 65 64 2e 20 54 68 65 20 74 68  thorized. The th
19ed0 69 72 64 20 74 68 72 6f 75 67 68 20 73 69 78 74  ird through sixt
19ee0 68 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20  h parameters.** 
19ef0 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  to the callback 
19f00 61 72 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61  are zero-termina
19f10 74 65 64 20 73 74 72 69 6e 67 73 20 74 68 61 74  ted strings that
19f20 20 63 6f 6e 74 61 69 6e 20 61 64 64 69 74 69 6f   contain additio
19f30 6e 61 6c 0a 2a 2a 20 64 65 74 61 69 6c 73 20 61  nal.** details a
19f40 62 6f 75 74 20 74 68 65 20 61 63 74 69 6f 6e 20  bout the action 
19f50 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65 64  to be authorized
19f60 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61  ..**.** If the a
19f70 63 74 69 6f 6e 20 63 6f 64 65 20 69 73 20 5b 53  ction code is [S
19f80 51 4c 49 54 45 5f 52 45 41 44 5d 0a 2a 2a 20 61  QLITE_READ].** a
19f90 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  nd the callback 
19fa0 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
19fb0 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74 68 65  IGNORE] then the
19fc0 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74  .** [prepared st
19fd0 61 74 65 6d 65 6e 74 5d 20 73 74 61 74 65 6d 65  atement] stateme
19fe0 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65  nt is constructe
19ff0 64 20 74 6f 20 73 75 62 73 74 69 74 75 74 65 0a  d to substitute.
1a000 2a 2a 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20  ** a NULL value 
1a010 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65 20  in place of the 
1a020 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61  table column tha
1a030 74 20 77 6f 75 6c 64 20 68 61 76 65 0a 2a 2a 20  t would have.** 
1a040 62 65 65 6e 20 72 65 61 64 20 69 66 20 5b 53 51  been read if [SQ
1a050 4c 49 54 45 5f 4f 4b 5d 20 68 61 64 20 62 65 65  LITE_OK] had bee
1a060 6e 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65  n returned.  The
1a070 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d   [SQLITE_IGNORE]
1a080 0a 2a 2a 20 72 65 74 75 72 6e 20 63 61 6e 20 62  .** return can b
1a090 65 20 75 73 65 64 20 74 6f 20 64 65 6e 79 20 61  e used to deny a
1a0a0 6e 20 75 6e 74 72 75 73 74 65 64 20 75 73 65 72  n untrusted user
1a0b0 20 61 63 63 65 73 73 20 74 6f 20 69 6e 64 69 76   access to indiv
1a0c0 69 64 75 61 6c 0a 2a 2a 20 63 6f 6c 75 6d 6e 73  idual.** columns
1a0d0 20 6f 66 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20   of a table..** 
1a0e0 49 66 20 74 68 65 20 61 63 74 69 6f 6e 20 63 6f  If the action co
1a0f0 64 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 45  de is [SQLITE_DE
1a100 4c 45 54 45 5d 20 61 6e 64 20 74 68 65 20 63 61  LETE] and the ca
1a110 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 0a 2a  llback returns.*
1a120 2a 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  * [SQLITE_IGNORE
1a130 5d 20 74 68 65 6e 20 74 68 65 20 5b 44 45 4c 45  ] then the [DELE
1a140 54 45 5d 20 6f 70 65 72 61 74 69 6f 6e 20 70 72  TE] operation pr
1a150 6f 63 65 65 64 73 20 62 75 74 20 74 68 65 0a 2a  oceeds but the.*
1a160 2a 20 5b 74 72 75 6e 63 61 74 65 20 6f 70 74 69  * [truncate opti
1a170 6d 69 7a 61 74 69 6f 6e 5d 20 69 73 20 64 69 73  mization] is dis
1a180 61 62 6c 65 64 20 61 6e 64 20 61 6c 6c 20 72 6f  abled and all ro
1a190 77 73 20 61 72 65 20 64 65 6c 65 74 65 64 20 69  ws are deleted i
1a1a0 6e 64 69 76 69 64 75 61 6c 6c 79 2e 0a 2a 2a 0a  ndividually..**.
1a1b0 2a 2a 20 41 6e 20 61 75 74 68 6f 72 69 7a 65 72  ** An authorizer
1a1c0 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 5b 73   is used when [s
1a1d0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c  qlite3_prepare |
1a1e0 20 70 72 65 70 61 72 69 6e 67 5d 0a 2a 2a 20 53   preparing].** S
1a1f0 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 66 72  QL statements fr
1a200 6f 6d 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20  om an untrusted 
1a210 73 6f 75 72 63 65 2c 20 74 6f 20 65 6e 73 75 72  source, to ensur
1a220 65 20 74 68 61 74 20 74 68 65 20 53 51 4c 20 73  e that the SQL s
1a230 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 64 6f 20  tatements.** do 
1a240 6e 6f 74 20 74 72 79 20 74 6f 20 61 63 63 65 73  not try to acces
1a250 73 20 64 61 74 61 20 74 68 65 79 20 61 72 65 20  s data they are 
1a260 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 73  not allowed to s
1a270 65 65 2c 20 6f 72 20 74 68 61 74 20 74 68 65 79  ee, or that they
1a280 20 64 6f 20 6e 6f 74 0a 2a 2a 20 74 72 79 20 74   do not.** try t
1a290 6f 20 65 78 65 63 75 74 65 20 6d 61 6c 69 63 69  o execute malici
1a2a0 6f 75 73 20 73 74 61 74 65 6d 65 6e 74 73 20 74  ous statements t
1a2b0 68 61 74 20 64 61 6d 61 67 65 20 74 68 65 20 64  hat damage the d
1a2c0 61 74 61 62 61 73 65 2e 20 20 46 6f 72 0a 2a 2a  atabase.  For.**
1a2d0 20 65 78 61 6d 70 6c 65 2c 20 61 6e 20 61 70 70   example, an app
1a2e0 6c 69 63 61 74 69 6f 6e 20 6d 61 79 20 61 6c 6c  lication may all
1a2f0 6f 77 20 61 20 75 73 65 72 20 74 6f 20 65 6e 74  ow a user to ent
1a300 65 72 20 61 72 62 69 74 72 61 72 79 0a 2a 2a 20  er arbitrary.** 
1a310 53 51 4c 20 71 75 65 72 69 65 73 20 66 6f 72 20  SQL queries for 
1a320 65 76 61 6c 75 61 74 69 6f 6e 20 62 79 20 61 20  evaluation by a 
1a330 64 61 74 61 62 61 73 65 2e 20 20 42 75 74 20 74  database.  But t
1a340 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64  he application d
1a350 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20  oes.** not want 
1a360 74 68 65 20 75 73 65 72 20 74 6f 20 62 65 20 61  the user to be a
1a370 62 6c 65 20 74 6f 20 6d 61 6b 65 20 61 72 62 69  ble to make arbi
1a380 74 72 61 72 79 20 63 68 61 6e 67 65 73 20 74 6f  trary changes to
1a390 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
1a3a0 2e 20 20 41 6e 20 61 75 74 68 6f 72 69 7a 65 72  .  An authorizer
1a3b0 20 63 6f 75 6c 64 20 74 68 65 6e 20 62 65 20 70   could then be p
1a3c0 75 74 20 69 6e 20 70 6c 61 63 65 20 77 68 69 6c  ut in place whil
1a3d0 65 20 74 68 65 0a 2a 2a 20 75 73 65 72 2d 65 6e  e the.** user-en
1a3e0 74 65 72 65 64 20 53 51 4c 20 69 73 20 62 65 69  tered SQL is bei
1a3f0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ng [sqlite3_prep
1a400 61 72 65 20 7c 20 70 72 65 70 61 72 65 64 5d 20  are | prepared] 
1a410 74 68 61 74 0a 2a 2a 20 64 69 73 61 6c 6c 6f 77  that.** disallow
1a420 73 20 65 76 65 72 79 74 68 69 6e 67 20 65 78 63  s everything exc
1a430 65 70 74 20 5b 53 45 4c 45 43 54 5d 20 73 74 61  ept [SELECT] sta
1a440 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 41  tements..**.** A
1a450 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74  pplications that
1a460 20 6e 65 65 64 20 74 6f 20 70 72 6f 63 65 73 73   need to process
1a470 20 53 51 4c 20 66 72 6f 6d 20 75 6e 74 72 75 73   SQL from untrus
1a480 74 65 64 20 73 6f 75 72 63 65 73 0a 2a 2a 20 6d  ted sources.** m
1a490 69 67 68 74 20 61 6c 73 6f 20 63 6f 6e 73 69 64  ight also consid
1a4a0 65 72 20 6c 6f 77 65 72 69 6e 67 20 72 65 73 6f  er lowering reso
1a4b0 75 72 63 65 20 6c 69 6d 69 74 73 20 75 73 69 6e  urce limits usin
1a4c0 67 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74  g [sqlite3_limit
1a4d0 28 29 5d 0a 2a 2a 20 61 6e 64 20 6c 69 6d 69 74  ()].** and limit
1a4e0 69 6e 67 20 64 61 74 61 62 61 73 65 20 73 69 7a  ing database siz
1a4f0 65 20 75 73 69 6e 67 20 74 68 65 20 5b 6d 61 78  e using the [max
1a500 5f 70 61 67 65 5f 63 6f 75 6e 74 5d 20 5b 50 52  _page_count] [PR
1a510 41 47 4d 41 5d 0a 2a 2a 20 69 6e 20 61 64 64 69  AGMA].** in addi
1a520 74 69 6f 6e 20 74 6f 20 75 73 69 6e 67 20 61 6e  tion to using an
1a530 20 61 75 74 68 6f 72 69 7a 65 72 2e 0a 2a 2a 0a   authorizer..**.
1a540 2a 2a 20 4f 6e 6c 79 20 61 20 73 69 6e 67 6c 65  ** Only a single
1a550 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6e 20   authorizer can 
1a560 62 65 20 69 6e 20 70 6c 61 63 65 20 6f 6e 20 61  be in place on a
1a570 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1a580 74 69 6f 6e 0a 2a 2a 20 61 74 20 61 20 74 69 6d  tion.** at a tim
1a590 65 2e 20 20 45 61 63 68 20 63 61 6c 6c 20 74 6f  e.  Each call to
1a5a0 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74   sqlite3_set_aut
1a5b0 68 6f 72 69 7a 65 72 20 6f 76 65 72 72 69 64 65  horizer override
1a5c0 73 20 74 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75  s the.** previou
1a5d0 73 20 63 61 6c 6c 2e 20 20 44 69 73 61 62 6c 65  s call.  Disable
1a5e0 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20   the authorizer 
1a5f0 62 79 20 69 6e 73 74 61 6c 6c 69 6e 67 20 61 20  by installing a 
1a600 4e 55 4c 4c 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a  NULL callback..*
1a610 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72  * The authorizer
1a620 20 69 73 20 64 69 73 61 62 6c 65 64 20 62 79 20   is disabled by 
1a630 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54  default..**.** T
1a640 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61  he authorizer ca
1a650 6c 6c 62 61 63 6b 20 6d 75 73 74 20 6e 6f 74 20  llback must not 
1a660 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74  do anything that
1a670 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20   will modify.** 
1a680 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
1a690 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76  nection that inv
1a6a0 6f 6b 65 64 20 74 68 65 20 61 75 74 68 6f 72 69  oked the authori
1a6b0 7a 65 72 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a  zer callback..**
1a6c0 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69   Note that [sqli
1a6d0 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
1a6e0 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73  ] and [sqlite3_s
1a6f0 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69  tep()] both modi
1a700 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61  fy their.** data
1a710 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
1a720 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67   for the meaning
1a730 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20   of "modify" in 
1a740 74 68 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a  this paragraph..
1a750 2a 2a 0a 2a 2a 20 57 68 65 6e 20 5b 73 71 6c 69  **.** When [sqli
1a760 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
1a770 5d 20 69 73 20 75 73 65 64 20 74 6f 20 70 72 65  ] is used to pre
1a780 70 61 72 65 20 61 20 73 74 61 74 65 6d 65 6e 74  pare a statement
1a790 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65  , the.** stateme
1a7a0 6e 74 20 6d 69 67 68 74 20 62 65 20 72 65 2d 70  nt might be re-p
1a7b0 72 65 70 61 72 65 64 20 64 75 72 69 6e 67 20 5b  repared during [
1a7c0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
1a7d0 64 75 65 20 74 6f 20 61 20 0a 2a 2a 20 73 63 68  due to a .** sch
1a7e0 65 6d 61 20 63 68 61 6e 67 65 2e 20 20 48 65 6e  ema change.  Hen
1a7f0 63 65 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74  ce, the applicat
1a800 69 6f 6e 20 73 68 6f 75 6c 64 20 65 6e 73 75 72  ion should ensur
1a810 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 63 6f  e that the.** co
1a820 72 72 65 63 74 20 61 75 74 68 6f 72 69 7a 65 72  rrect authorizer
1a830 20 63 61 6c 6c 62 61 63 6b 20 72 65 6d 61 69 6e   callback remain
1a840 73 20 69 6e 20 70 6c 61 63 65 20 64 75 72 69 6e  s in place durin
1a850 67 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73  g the [sqlite3_s
1a860 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  tep()]..**.** No
1a870 74 65 20 74 68 61 74 20 74 68 65 20 61 75 74 68  te that the auth
1a880 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20  orizer callback 
1a890 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 6c 79 20  is invoked only 
1a8a0 64 75 72 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74  during.** [sqlit
1a8b0 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72  e3_prepare()] or
1a8c0 20 69 74 73 20 76 61 72 69 61 6e 74 73 2e 20 20   its variants.  
1a8d0 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 69 73  Authorization is
1a8e0 20 6e 6f 74 0a 2a 2a 20 70 65 72 66 6f 72 6d 65   not.** performe
1a8f0 64 20 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65  d during stateme
1a900 6e 74 20 65 76 61 6c 75 61 74 69 6f 6e 20 69 6e  nt evaluation in
1a910 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
1a920 5d 2c 20 75 6e 6c 65 73 73 0a 2a 2a 20 61 73 20  ], unless.** as 
1a930 73 74 61 74 65 64 20 69 6e 20 74 68 65 20 70 72  stated in the pr
1a940 65 76 69 6f 75 73 20 70 61 72 61 67 72 61 70 68  evious paragraph
1a950 2c 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  , sqlite3_step()
1a960 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 73 71 6c 69   invokes.** sqli
1a970 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
1a980 20 74 6f 20 72 65 70 72 65 70 61 72 65 20 61 20   to reprepare a 
1a990 73 74 61 74 65 6d 65 6e 74 20 61 66 74 65 72 20  statement after 
1a9a0 61 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2e  a schema change.
1a9b0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
1a9c0 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 35 30 31 5d  nts:.** [H12501]
1a9d0 20 5b 48 31 32 35 30 32 5d 20 5b 48 31 32 35 30   [H12502] [H1250
1a9e0 33 5d 20 5b 48 31 32 35 30 34 5d 20 5b 48 31 32  3] [H12504] [H12
1a9f0 35 30 35 5d 20 5b 48 31 32 35 30 36 5d 20 5b 48  505] [H12506] [H
1aa00 31 32 35 30 37 5d 20 5b 48 31 32 35 31 30 5d 0a  12507] [H12510].
1aa10 2a 2a 20 5b 48 31 32 35 31 31 5d 20 5b 48 31 32  ** [H12511] [H12
1aa20 35 31 32 5d 20 5b 48 31 32 35 32 30 5d 20 5b 48  512] [H12520] [H
1aa30 31 32 35 32 31 5d 20 5b 48 31 32 35 32 32 5d 0a  12521] [H12522].
1aa40 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1aa50 74 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75  t sqlite3_set_au
1aa60 74 68 6f 72 69 7a 65 72 28 0a 20 20 73 71 6c 69  thorizer(.  sqli
1aa70 74 65 33 2a 2c 0a 20 20 69 6e 74 20 28 2a 78 41  te3*,.  int (*xA
1aa80 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  uth)(void*,int,c
1aa90 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
1aaa0 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
1aab0 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c  r*,const char*),
1aac0 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72 44 61  .  void *pUserDa
1aad0 74 61 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ta.);../*.** CAP
1aae0 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69 7a 65  I3REF: Authorize
1aaf0 72 20 52 65 74 75 72 6e 20 43 6f 64 65 73 20 7b  r Return Codes {
1ab00 48 31 32 35 39 30 7d 20 3c 48 31 32 35 30 30 3e  H12590} <H12500>
1ab10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69  .**.** The [sqli
1ab20 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
1ab30 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 72 20  er | authorizer 
1ab40 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  callback functio
1ab50 6e 5d 20 6d 75 73 74 0a 2a 2a 20 72 65 74 75 72  n] must.** retur
1ab60 6e 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45  n either [SQLITE
1ab70 5f 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 74  _OK] or one of t
1ab80 68 65 73 65 20 74 77 6f 20 63 6f 6e 73 74 61 6e  hese two constan
1ab90 74 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74  ts in order.** t
1aba0 6f 20 73 69 67 6e 61 6c 20 53 51 4c 69 74 65 20  o signal SQLite 
1abb0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
1abc0 68 65 20 61 63 74 69 6f 6e 20 69 73 20 70 65 72  he action is per
1abd0 6d 69 74 74 65 64 2e 20 20 53 65 65 20 74 68 65  mitted.  See the
1abe0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 65 74  .** [sqlite3_set
1abf0 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75  _authorizer | au
1ac00 74 68 6f 72 69 7a 65 72 20 64 6f 63 75 6d 65 6e  thorizer documen
1ac10 74 61 74 69 6f 6e 5d 20 66 6f 72 20 61 64 64 69  tation] for addi
1ac20 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d  tional.** inform
1ac30 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ation..*/.#defin
1ac40 65 20 53 51 4c 49 54 45 5f 44 45 4e 59 20 20 20  e SQLITE_DENY   
1ac50 31 20 20 20 2f 2a 20 41 62 6f 72 74 20 74 68 65  1   /* Abort the
1ac60 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 77   SQL statement w
1ac70 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a  ith an error */.
1ac80 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
1ac90 47 4e 4f 52 45 20 32 20 20 20 2f 2a 20 44 6f 6e  GNORE 2   /* Don
1aca0 27 74 20 61 6c 6c 6f 77 20 61 63 63 65 73 73 2c  't allow access,
1acb0 20 62 75 74 20 64 6f 6e 27 74 20 67 65 6e 65 72   but don't gener
1acc0 61 74 65 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a  ate an error */.
1acd0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
1ace0 20 41 75 74 68 6f 72 69 7a 65 72 20 41 63 74 69   Authorizer Acti
1acf0 6f 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 35 30  on Codes {H12550
1ad00 7d 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 2a  } <H12500>.**.**
1ad10 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65   The [sqlite3_se
1ad20 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 5d 20  t_authorizer()] 
1ad30 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74  interface regist
1ad40 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66  ers a callback f
1ad50 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20  unction.** that 
1ad60 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20 61 75  is invoked to au
1ad70 74 68 6f 72 69 7a 65 20 63 65 72 74 61 69 6e 20  thorize certain 
1ad80 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 63  SQL statement ac
1ad90 74 69 6f 6e 73 2e 20 20 54 68 65 0a 2a 2a 20 73  tions.  The.** s
1ada0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
1adb0 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  to the callback 
1adc0 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f  is an integer co
1add0 64 65 20 74 68 61 74 20 73 70 65 63 69 66 69 65  de that specifie
1ade0 73 0a 2a 2a 20 77 68 61 74 20 61 63 74 69 6f 6e  s.** what action
1adf0 20 69 73 20 62 65 69 6e 67 20 61 75 74 68 6f 72   is being author
1ae00 69 7a 65 64 2e 20 20 54 68 65 73 65 20 61 72 65  ized.  These are
1ae10 20 74 68 65 20 69 6e 74 65 67 65 72 20 61 63 74   the integer act
1ae20 69 6f 6e 20 63 6f 64 65 73 20 74 68 61 74 0a 2a  ion codes that.*
1ae30 2a 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72  * the authorizer
1ae40 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 62 65   callback may be
1ae50 20 70 61 73 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54   passed..**.** T
1ae60 68 65 73 65 20 61 63 74 69 6f 6e 20 63 6f 64 65  hese action code
1ae70 20 76 61 6c 75 65 73 20 73 69 67 6e 69 66 79 20   values signify 
1ae80 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 6f 70 65  what kind of ope
1ae90 72 61 74 69 6f 6e 20 69 73 20 74 6f 20 62 65 0a  ration is to be.
1aea0 2a 2a 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 20  ** authorized.  
1aeb0 54 68 65 20 33 72 64 20 61 6e 64 20 34 74 68 20  The 3rd and 4th 
1aec0 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68  parameters to th
1aed0 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 0a  e authorization.
1aee0 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  ** callback func
1aef0 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 70 61 72  tion will be par
1af00 61 6d 65 74 65 72 73 20 6f 72 20 4e 55 4c 4c 20  ameters or NULL 
1af10 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 69  depending on whi
1af20 63 68 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 63  ch of these.** c
1af30 6f 64 65 73 20 69 73 20 75 73 65 64 20 61 73 20  odes is used as 
1af40 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
1af50 65 74 65 72 2e 20 20 54 68 65 20 35 74 68 20 70  eter.  The 5th p
1af60 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 0a  arameter to the.
1af70 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61  ** authorizer ca
1af80 6c 6c 62 61 63 6b 20 69 73 20 74 68 65 20 6e 61  llback is the na
1af90 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
1afa0 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 74 65 6d  se ("main", "tem
1afb0 70 22 2c 0a 2a 2a 20 65 74 63 2e 29 20 69 66 20  p",.** etc.) if 
1afc0 61 70 70 6c 69 63 61 62 6c 65 2e 20 20 54 68 65  applicable.  The
1afd0 20 36 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   6th parameter t
1afe0 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72  o the authorizer
1aff0 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20   callback.** is 
1b000 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
1b010 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 69 67 67  inner-most trigg
1b020 65 72 20 6f 72 20 76 69 65 77 20 74 68 61 74 20  er or view that 
1b030 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
1b040 6f 72 0a 2a 2a 20 74 68 65 20 61 63 63 65 73 73  or.** the access
1b050 20 61 74 74 65 6d 70 74 20 6f 72 20 4e 55 4c 4c   attempt or NULL
1b060 20 69 66 20 74 68 69 73 20 61 63 63 65 73 73 20   if this access 
1b070 61 74 74 65 6d 70 74 20 69 73 20 64 69 72 65 63  attempt is direc
1b080 74 6c 79 20 66 72 6f 6d 0a 2a 2a 20 74 6f 70 2d  tly from.** top-
1b090 6c 65 76 65 6c 20 53 51 4c 20 63 6f 64 65 2e 0a  level SQL code..
1b0a0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
1b0b0 74 73 3a 0a 2a 2a 20 5b 48 31 32 35 35 31 5d 20  ts:.** [H12551] 
1b0c0 5b 48 31 32 35 35 32 5d 20 5b 48 31 32 35 35 33  [H12552] [H12553
1b0d0 5d 20 5b 48 31 32 35 35 34 5d 0a 2a 2f 0a 2f 2a  ] [H12554].*/./*
1b0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 33 72 64 20 2a  ********** 3rd *
1b110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 34 74 68 20  *********** 4th 
1b120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 64 65  ***********/.#de
1b130 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41  fine SQLITE_CREA
1b140 54 45 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20  TE_INDEX        
1b150 20 20 31 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e    1   /* Index N
1b160 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e  ame      Table N
1b170 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  ame      */.#def
1b180 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54  ine SQLITE_CREAT
1b190 45 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20  E_TABLE         
1b1a0 20 32 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61   2   /* Table Na
1b1b0 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  me      NULL    
1b1c0 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
1b1d0 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45  ne SQLITE_CREATE
1b1e0 5f 54 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20  _TEMP_INDEX     
1b1f0 33 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d  3   /* Index Nam
1b200 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d  e      Table Nam
1b210 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  e      */.#defin
1b220 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  e SQLITE_CREATE_
1b230 54 45 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 34  TEMP_TABLE     4
1b240 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65     /* Table Name
1b250 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20        NULL      
1b260 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
1b270 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
1b280 45 4d 50 5f 54 52 49 47 47 45 52 20 20 20 35 20  EMP_TRIGGER   5 
1b290 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d    /* Trigger Nam
1b2a0 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20  e    Table Name 
1b2b0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
1b2c0 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45  SQLITE_CREATE_TE
1b2d0 4d 50 5f 56 49 45 57 20 20 20 20 20 20 36 20 20  MP_VIEW      6  
1b2e0 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20   /* View Name   
1b2f0 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20      NULL        
1b300 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
1b310 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 52 49  QLITE_CREATE_TRI
1b320 47 47 45 52 20 20 20 20 20 20 20 20 37 20 20 20  GGER        7   
1b330 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20  /* Trigger Name 
1b340 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20     Table Name   
1b350 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1b360 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 49 45 57  LITE_CREATE_VIEW
1b370 20 20 20 20 20 20 20 20 20 20 20 38 20 20 20 2f             8   /
1b380 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20  * View Name     
1b390 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
1b3a0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1b3b0 49 54 45 5f 44 45 4c 45 54 45 20 20 20 20 20 20  ITE_DELETE      
1b3c0 20 20 20 20 20 20 20 20 20 20 39 20 20 20 2f 2a            9   /*
1b3d0 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
1b3e0 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
1b3f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1b400 54 45 5f 44 52 4f 50 5f 49 4e 44 45 58 20 20 20  TE_DROP_INDEX   
1b410 20 20 20 20 20 20 20 20 31 30 20 20 20 2f 2a 20          10   /* 
1b420 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20  Index Name      
1b430 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1b440 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1b450 45 5f 44 52 4f 50 5f 54 41 42 4c 45 20 20 20 20  E_DROP_TABLE    
1b460 20 20 20 20 20 20 20 31 31 20 20 20 2f 2a 20 54         11   /* T
1b470 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e  able Name      N
1b480 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
1b490 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1b4a0 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58  _DROP_TEMP_INDEX
1b4b0 20 20 20 20 20 20 31 32 20 20 20 2f 2a 20 49 6e        12   /* In
1b4c0 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 54 61  dex Name      Ta
1b4d0 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f  ble Name      */
1b4e0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1b4f0 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 20  DROP_TEMP_TABLE 
1b500 20 20 20 20 20 31 33 20 20 20 2f 2a 20 54 61 62       13   /* Tab
1b510 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c  le Name      NUL
1b520 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
1b530 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
1b540 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52  ROP_TEMP_TRIGGER
1b550 20 20 20 20 31 34 20 20 20 2f 2a 20 54 72 69 67      14   /* Trig
1b560 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c  ger Name    Tabl
1b570 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23  e Name      */.#
1b580 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52  define SQLITE_DR
1b590 4f 50 5f 54 45 4d 50 5f 56 49 45 57 20 20 20 20  OP_TEMP_VIEW    
1b5a0 20 20 20 31 35 20 20 20 2f 2a 20 56 69 65 77 20     15   /* View 
1b5b0 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20  Name       NULL 
1b5c0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
1b5d0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f  efine SQLITE_DRO
1b5e0 50 5f 54 52 49 47 47 45 52 20 20 20 20 20 20 20  P_TRIGGER       
1b5f0 20 20 31 36 20 20 20 2f 2a 20 54 72 69 67 67 65    16   /* Trigge
1b600 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c 65 20  r Name    Table 
1b610 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65  Name      */.#de
1b620 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50  fine SQLITE_DROP
1b630 5f 56 49 45 57 20 20 20 20 20 20 20 20 20 20 20  _VIEW           
1b640 20 31 37 20 20 20 2f 2a 20 56 69 65 77 20 4e 61   17   /* View Na
1b650 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20  me       NULL   
1b660 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66           */.#def
1b670 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 53 45 52  ine SQLITE_INSER
1b680 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
1b690 31 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61  18   /* Table Na
1b6a0 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  me      NULL    
1b6b0 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
1b6c0 6e 65 20 53 51 4c 49 54 45 5f 50 52 41 47 4d 41  ne SQLITE_PRAGMA
1b6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
1b6e0 39 20 20 20 2f 2a 20 50 72 61 67 6d 61 20 4e 61  9   /* Pragma Na
1b6f0 6d 65 20 20 20 20 20 31 73 74 20 61 72 67 20 6f  me     1st arg o
1b700 72 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e  r NULL */.#defin
1b710 65 20 53 51 4c 49 54 45 5f 52 45 41 44 20 20 20  e SQLITE_READ   
1b720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 30                20
1b730 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65     /* Table Name
1b740 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 4e 61 6d        Column Nam
1b750 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  e     */.#define
1b760 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54 20 20   SQLITE_SELECT  
1b770 20 20 20 20 20 20 20 20 20 20 20 20 20 32 31 20               21 
1b780 20 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20    /* NULL       
1b790 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20       NULL       
1b7a0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
1b7b0 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49  SQLITE_TRANSACTI
1b7c0 4f 4e 20 20 20 20 20 20 20 20 20 20 32 32 20 20  ON          22  
1b7d0 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 20 20   /* Operation   
1b7e0 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20      NULL        
1b7f0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
1b800 51 4c 49 54 45 5f 55 50 44 41 54 45 20 20 20 20  QLITE_UPDATE    
1b810 20 20 20 20 20 20 20 20 20 20 20 32 33 20 20 20             23   
1b820 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20  /* Table Name   
1b830 20 20 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20     Column Name  
1b840 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1b850 4c 49 54 45 5f 41 54 54 41 43 48 20 20 20 20 20  LITE_ATTACH     
1b860 20 20 20 20 20 20 20 20 20 20 32 34 20 20 20 2f            24   /
1b870 2a 20 46 69 6c 65 6e 61 6d 65 20 20 20 20 20 20  * Filename      
1b880 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
1b890 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1b8a0 49 54 45 5f 44 45 54 41 43 48 20 20 20 20 20 20  ITE_DETACH      
1b8b0 20 20 20 20 20 20 20 20 20 32 35 20 20 20 2f 2a           25   /*
1b8c0 20 44 61 74 61 62 61 73 65 20 4e 61 6d 65 20 20   Database Name  
1b8d0 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
1b8e0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1b8f0 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 20 20  TE_ALTER_TABLE  
1b900 20 20 20 20 20 20 20 20 32 36 20 20 20 2f 2a 20          26   /* 
1b910 44 61 74 61 62 61 73 65 20 4e 61 6d 65 20 20 20  Database Name   
1b920 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1b930 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1b940 45 5f 52 45 49 4e 44 45 58 20 20 20 20 20 20 20  E_REINDEX       
1b950 20 20 20 20 20 20 20 32 37 20 20 20 2f 2a 20 49         27   /* I
1b960 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20 4e  ndex Name      N
1b970 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
1b980 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1b990 5f 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20  _ANALYZE        
1b9a0 20 20 20 20 20 20 32 38 20 20 20 2f 2a 20 54 61        28   /* Ta
1b9b0 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e 55  ble Name      NU
1b9c0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  LL            */
1b9d0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1b9e0 43 52 45 41 54 45 5f 56 54 41 42 4c 45 20 20 20  CREATE_VTABLE   
1b9f0 20 20 20 20 20 32 39 20 20 20 2f 2a 20 54 61 62       29   /* Tab
1ba00 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4d 6f 64  le Name      Mod
1ba10 75 6c 65 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a  ule Name     */.
1ba20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
1ba30 52 4f 50 5f 56 54 41 42 4c 45 20 20 20 20 20 20  ROP_VTABLE      
1ba40 20 20 20 20 33 30 20 20 20 2f 2a 20 54 61 62 6c      30   /* Tabl
1ba50 65 20 4e 61 6d 65 20 20 20 20 20 20 4d 6f 64 75  e Name      Modu
1ba60 6c 65 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23  le Name     */.#
1ba70 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55  define SQLITE_FU
1ba80 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20  NCTION          
1ba90 20 20 20 33 31 20 20 20 2f 2a 20 4e 55 4c 4c 20     31   /* NULL 
1baa0 20 20 20 20 20 20 20 20 20 20 20 46 75 6e 63 74             Funct
1bab0 69 6f 6e 20 4e 61 6d 65 20 20 20 2a 2f 0a 23 64  ion Name   */.#d
1bac0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 41 56  efine SQLITE_SAV
1bad0 45 50 4f 49 4e 54 20 20 20 20 20 20 20 20 20 20  EPOINT          
1bae0 20 20 33 32 20 20 20 2f 2a 20 4f 70 65 72 61 74    32   /* Operat
1baf0 69 6f 6e 20 20 20 20 20 20 20 53 61 76 65 70 6f  ion       Savepo
1bb00 69 6e 74 20 4e 61 6d 65 20 20 2a 2f 0a 23 64 65  int Name  */.#de
1bb10 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 50 59  fine SQLITE_COPY
1bb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bb30 20 20 30 20 20 20 2f 2a 20 4e 6f 20 6c 6f 6e 67    0   /* No long
1bb40 65 72 20 75 73 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a  er used */../*.*
1bb50 2a 20 43 41 50 49 33 52 45 46 3a 20 54 72 61 63  * CAPI3REF: Trac
1bb60 69 6e 67 20 41 6e 64 20 50 72 6f 66 69 6c 69 6e  ing And Profilin
1bb70 67 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 32  g Functions {H12
1bb80 32 38 30 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2a  280} <S60400>.**
1bb90 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
1bba0 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
1bbb0 65 73 20 72 65 67 69 73 74 65 72 20 63 61 6c 6c  es register call
1bbc0 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 73 20 74  back functions t
1bbd0 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 20  hat can be used 
1bbe0 66 6f 72 0a 2a 2a 20 74 72 61 63 69 6e 67 20 61  for.** tracing a
1bbf0 6e 64 20 70 72 6f 66 69 6c 69 6e 67 20 74 68 65  nd profiling the
1bc00 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 53 51   execution of SQ
1bc10 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a  L statements..**
1bc20 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b  .** The callback
1bc30 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74   function regist
1bc40 65 72 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  ered by sqlite3_
1bc50 74 72 61 63 65 28 29 20 69 73 20 69 6e 76 6f 6b  trace() is invok
1bc60 65 64 20 61 74 0a 2a 2a 20 76 61 72 69 6f 75 73  ed at.** various
1bc70 20 74 69 6d 65 73 20 77 68 65 6e 20 61 6e 20 53   times when an S
1bc80 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  QL statement is 
1bc90 62 65 69 6e 67 20 72 75 6e 20 62 79 20 5b 73 71  being run by [sq
1bca0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a 2a  lite3_step()]..*
1bcb0 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 72  * The callback r
1bcc0 65 74 75 72 6e 73 20 61 20 55 54 46 2d 38 20 72  eturns a UTF-8 r
1bcd0 65 6e 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20  endering of the 
1bce0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 65  SQL statement te
1bcf0 78 74 0a 2a 2a 20 61 73 20 74 68 65 20 73 74 61  xt.** as the sta
1bd00 74 65 6d 65 6e 74 20 66 69 72 73 74 20 62 65 67  tement first beg
1bd10 69 6e 73 20 65 78 65 63 75 74 69 6e 67 2e 20 20  ins executing.  
1bd20 41 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c 6c 62  Additional callb
1bd30 61 63 6b 73 20 6f 63 63 75 72 0a 2a 2a 20 61 73  acks occur.** as
1bd40 20 65 61 63 68 20 74 72 69 67 67 65 72 65 64 20   each triggered 
1bd50 73 75 62 70 72 6f 67 72 61 6d 20 69 73 20 65 6e  subprogram is en
1bd60 74 65 72 65 64 2e 20 20 54 68 65 20 63 61 6c 6c  tered.  The call
1bd70 62 61 63 6b 73 20 66 6f 72 20 74 72 69 67 67 65  backs for trigge
1bd80 72 73 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20 61 20  rs.** contain a 
1bd90 55 54 46 2d 38 20 53 51 4c 20 63 6f 6d 6d 65 6e  UTF-8 SQL commen
1bda0 74 20 74 68 61 74 20 69 64 65 6e 74 69 66 69 65  t that identifie
1bdb0 73 20 74 68 65 20 74 72 69 67 67 65 72 2e 0a 2a  s the trigger..*
1bdc0 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63  *.** The callbac
1bdd0 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73  k function regis
1bde0 74 65 72 65 64 20 62 79 20 73 71 6c 69 74 65 33  tered by sqlite3
1bdf0 5f 70 72 6f 66 69 6c 65 28 29 20 69 73 20 69 6e  _profile() is in
1be00 76 6f 6b 65 64 0a 2a 2a 20 61 73 20 65 61 63 68  voked.** as each
1be10 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 66   SQL statement f
1be20 69 6e 69 73 68 65 73 2e 20 20 54 68 65 20 70 72  inishes.  The pr
1be30 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b 20 63  ofile callback c
1be40 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 6f  ontains.** the o
1be50 72 69 67 69 6e 61 6c 20 73 74 61 74 65 6d 65 6e  riginal statemen
1be60 74 20 74 65 78 74 20 61 6e 64 20 61 6e 20 65 73  t text and an es
1be70 74 69 6d 61 74 65 20 6f 66 20 77 61 6c 6c 2d 63  timate of wall-c
1be80 6c 6f 63 6b 20 74 69 6d 65 0a 2a 2a 20 6f 66 20  lock time.** of 
1be90 68 6f 77 20 6c 6f 6e 67 20 74 68 61 74 20 73 74  how long that st
1bea0 61 74 65 6d 65 6e 74 20 74 6f 6f 6b 20 74 6f 20  atement took to 
1beb0 72 75 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  run..**.** Requi
1bec0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32  rements:.** [H12
1bed0 32 38 31 5d 20 5b 48 31 32 32 38 32 5d 20 5b 48  281] [H12282] [H
1bee0 31 32 32 38 33 5d 20 5b 48 31 32 32 38 34 5d 20  12283] [H12284] 
1bef0 5b 48 31 32 32 38 35 5d 20 5b 48 31 32 32 38 37  [H12285] [H12287
1bf00 5d 20 5b 48 31 32 32 38 38 5d 20 5b 48 31 32 32  ] [H12288] [H122
1bf10 38 39 5d 0a 2a 2a 20 5b 48 31 32 32 39 30 5d 0a  89].** [H12290].
1bf20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  */.SQLITE_API SQ
1bf30 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41  LITE_EXPERIMENTA
1bf40 4c 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  L void *sqlite3_
1bf50 74 72 61 63 65 28 73 71 6c 69 74 65 33 2a 2c 20  trace(sqlite3*, 
1bf60 76 6f 69 64 28 2a 78 54 72 61 63 65 29 28 76 6f  void(*xTrace)(vo
1bf70 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  id*,const char*)
1bf80 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45  , void*);.SQLITE
1bf90 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45  _API SQLITE_EXPE
1bfa0 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20 2a 73  RIMENTAL void *s
1bfb0 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 28 73  qlite3_profile(s
1bfc0 71 6c 69 74 65 33 2a 2c 0a 20 20 20 76 6f 69 64  qlite3*,.   void
1bfd0 28 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f 69 64  (*xProfile)(void
1bfe0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71  *,const char*,sq
1bff0 6c 69 74 65 33 5f 75 69 6e 74 36 34 29 2c 20 76  lite3_uint64), v
1c000 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  oid*);../*.** CA
1c010 50 49 33 52 45 46 3a 20 51 75 65 72 79 20 50 72  PI3REF: Query Pr
1c020 6f 67 72 65 73 73 20 43 61 6c 6c 62 61 63 6b 73  ogress Callbacks
1c030 20 7b 48 31 32 39 31 30 7d 20 3c 53 36 30 34 30   {H12910} <S6040
1c040 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  0>.**.** This ro
1c050 75 74 69 6e 65 20 63 6f 6e 66 69 67 75 72 65 73  utine configures
1c060 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63   a callback func
1c070 74 69 6f 6e 20 2d 20 74 68 65 0a 2a 2a 20 70 72  tion - the.** pr
1c080 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20  ogress callback 
1c090 2d 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65  - that is invoke
1c0a0 64 20 70 65 72 69 6f 64 69 63 61 6c 6c 79 20 64  d periodically d
1c0b0 75 72 69 6e 67 20 6c 6f 6e 67 0a 2a 2a 20 72 75  uring long.** ru
1c0c0 6e 6e 69 6e 67 20 63 61 6c 6c 73 20 74 6f 20 5b  nning calls to [
1c0d0 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 2c  sqlite3_exec()],
1c0e0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
1c0f0 5d 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65  ] and.** [sqlite
1c100 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 2e 20  3_get_table()]. 
1c110 20 41 6e 20 65 78 61 6d 70 6c 65 20 75 73 65 20   An example use 
1c120 66 6f 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65  for this.** inte
1c130 72 66 61 63 65 20 69 73 20 74 6f 20 6b 65 65 70  rface is to keep
1c140 20 61 20 47 55 49 20 75 70 64 61 74 65 64 20 64   a GUI updated d
1c150 75 72 69 6e 67 20 61 20 6c 61 72 67 65 20 71 75  uring a large qu
1c160 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ery..**.** If th
1c170 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62  e progress callb
1c180 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d  ack returns non-
1c190 7a 65 72 6f 2c 20 74 68 65 20 6f 70 65 72 61 74  zero, the operat
1c1a0 69 6f 6e 20 69 73 0a 2a 2a 20 69 6e 74 65 72 72  ion is.** interr
1c1b0 75 70 74 65 64 2e 20 20 54 68 69 73 20 66 65 61  upted.  This fea
1c1c0 74 75 72 65 20 63 61 6e 20 62 65 20 75 73 65 64  ture can be used
1c1d0 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 0a   to implement a.
1c1e0 2a 2a 20 22 43 61 6e 63 65 6c 22 20 62 75 74 74  ** "Cancel" butt
1c1f0 6f 6e 20 6f 6e 20 61 20 47 55 49 20 70 72 6f 67  on on a GUI prog
1c200 72 65 73 73 20 64 69 61 6c 6f 67 20 62 6f 78 2e  ress dialog box.
1c210 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 67 72  .**.** The progr
1c220 65 73 73 20 68 61 6e 64 6c 65 72 20 6d 75 73 74  ess handler must
1c230 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e 67   not do anything
1c240 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66   that will modif
1c250 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  y.** the databas
1c260 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61  e connection tha
1c270 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 70 72  t invoked the pr
1c280 6f 67 72 65 73 73 20 68 61 6e 64 6c 65 72 2e 0a  ogress handler..
1c290 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71  ** Note that [sq
1c2a0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1c2b0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
1c2c0 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f  _step()] both mo
1c2d0 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61  dify their.** da
1c2e0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1c2f0 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69  ns for the meani
1c300 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69  ng of "modify" i
1c310 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68  n this paragraph
1c320 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
1c330 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 39 31 31  ents:.** [H12911
1c340 5d 20 5b 48 31 32 39 31 32 5d 20 5b 48 31 32 39  ] [H12912] [H129
1c350 31 33 5d 20 5b 48 31 32 39 31 34 5d 20 5b 48 31  13] [H12914] [H1
1c360 32 39 31 35 5d 20 5b 48 31 32 39 31 36 5d 20 5b  2915] [H12916] [
1c370 48 31 32 39 31 37 5d 20 5b 48 31 32 39 31 38 5d  H12917] [H12918]
1c380 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  .**.*/.SQLITE_AP
1c390 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 70  I void sqlite3_p
1c3a0 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 28  rogress_handler(
1c3b0 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 69  sqlite3*, int, i
1c3c0 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 20 76 6f  nt(*)(void*), vo
1c3d0 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  id*);../*.** CAP
1c3e0 49 33 52 45 46 3a 20 4f 70 65 6e 69 6e 67 20 41  I3REF: Opening A
1c3f0 20 4e 65 77 20 44 61 74 61 62 61 73 65 20 43 6f   New Database Co
1c400 6e 6e 65 63 74 69 6f 6e 20 7b 48 31 32 37 30 30  nnection {H12700
1c410 7d 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a  } <S40200>.**.**
1c420 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
1c430 6f 70 65 6e 20 61 6e 20 53 51 4c 69 74 65 20 64  open an SQLite d
1c440 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 6f  atabase file who
1c450 73 65 20 6e 61 6d 65 20 69 73 20 67 69 76 65 6e  se name is given
1c460 20 62 79 20 74 68 65 0a 2a 2a 20 66 69 6c 65 6e   by the.** filen
1c470 61 6d 65 20 61 72 67 75 6d 65 6e 74 2e 20 54 68  ame argument. Th
1c480 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d  e filename argum
1c490 65 6e 74 20 69 73 20 69 6e 74 65 72 70 72 65 74  ent is interpret
1c4a0 65 64 20 61 73 20 55 54 46 2d 38 20 66 6f 72 0a  ed as UTF-8 for.
1c4b0 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  ** sqlite3_open(
1c4c0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70  ) and sqlite3_op
1c4d0 65 6e 5f 76 32 28 29 20 61 6e 64 20 61 73 20 55  en_v2() and as U
1c4e0 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74  TF-16 in the nat
1c4f0 69 76 65 20 62 79 74 65 0a 2a 2a 20 6f 72 64 65  ive byte.** orde
1c500 72 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  r for sqlite3_op
1c510 65 6e 31 36 28 29 2e 20 41 20 5b 64 61 74 61 62  en16(). A [datab
1c520 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
1c530 68 61 6e 64 6c 65 20 69 73 20 75 73 75 61 6c 6c  handle is usuall
1c540 79 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e  y.** returned in
1c550 20 2a 70 70 44 62 2c 20 65 76 65 6e 20 69 66 20   *ppDb, even if 
1c560 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e  an error occurs.
1c570 20 20 54 68 65 20 6f 6e 6c 79 20 65 78 63 65 70    The only excep
1c580 74 69 6f 6e 20 69 73 20 74 68 61 74 0a 2a 2a 20  tion is that.** 
1c590 69 66 20 53 51 4c 69 74 65 20 69 73 20 75 6e 61  if SQLite is una
1c5a0 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ble to allocate 
1c5b0 6d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74  memory to hold t
1c5c0 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f 62 6a  he [sqlite3] obj
1c5d0 65 63 74 2c 0a 2a 2a 20 61 20 4e 55 4c 4c 20 77  ect,.** a NULL w
1c5e0 69 6c 6c 20 62 65 20 77 72 69 74 74 65 6e 20 69  ill be written i
1c5f0 6e 74 6f 20 2a 70 70 44 62 20 69 6e 73 74 65 61  nto *ppDb instea
1c600 64 20 6f 66 20 61 20 70 6f 69 6e 74 65 72 20 74  d of a pointer t
1c610 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 0a  o the [sqlite3].
1c620 2a 2a 20 6f 62 6a 65 63 74 2e 20 49 66 20 74 68  ** object. If th
1c630 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70  e database is op
1c640 65 6e 65 64 20 28 61 6e 64 2f 6f 72 20 63 72 65  ened (and/or cre
1c650 61 74 65 64 29 20 73 75 63 63 65 73 73 66 75 6c  ated) successful
1c660 6c 79 2c 20 74 68 65 6e 0a 2a 2a 20 5b 53 51 4c  ly, then.** [SQL
1c670 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72  ITE_OK] is retur
1c680 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20  ned.  Otherwise 
1c690 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20  an [error code] 
1c6a0 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68  is returned.  Th
1c6b0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 72  e.** [sqlite3_er
1c6c0 72 6d 73 67 28 29 5d 20 6f 72 20 5b 73 71 6c 69  rmsg()] or [sqli
1c6d0 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 20  te3_errmsg16()] 
1c6e0 72 6f 75 74 69 6e 65 73 20 63 61 6e 20 62 65 20  routines can be 
1c6f0 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e 0a 2a  used to obtain.*
1c700 2a 20 61 6e 20 45 6e 67 6c 69 73 68 20 6c 61 6e  * an English lan
1c710 67 75 61 67 65 20 64 65 73 63 72 69 70 74 69 6f  guage descriptio
1c720 6e 20 6f 66 20 74 68 65 20 65 72 72 6f 72 2e 0a  n of the error..
1c730 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c  **.** The defaul
1c740 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74  t encoding for t
1c750 68 65 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c  he database will
1c760 20 62 65 20 55 54 46 2d 38 20 69 66 0a 2a 2a 20   be UTF-8 if.** 
1c770 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 6f  sqlite3_open() o
1c780 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76  r sqlite3_open_v
1c790 32 28 29 20 69 73 20 63 61 6c 6c 65 64 20 61 6e  2() is called an
1c7a0 64 0a 2a 2a 20 55 54 46 2d 31 36 20 69 6e 20 74  d.** UTF-16 in t
1c7b0 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f  he native byte o
1c7c0 72 64 65 72 20 69 66 20 73 71 6c 69 74 65 33 5f  rder if sqlite3_
1c7d0 6f 70 65 6e 31 36 28 29 20 69 73 20 75 73 65 64  open16() is used
1c7e0 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20  ..**.** Whether 
1c7f0 6f 72 20 6e 6f 74 20 61 6e 20 65 72 72 6f 72 20  or not an error 
1c800 6f 63 63 75 72 73 20 77 68 65 6e 20 69 74 20 69  occurs when it i
1c810 73 20 6f 70 65 6e 65 64 2c 20 72 65 73 6f 75 72  s opened, resour
1c820 63 65 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65  ces.** associate
1c830 64 20 77 69 74 68 20 74 68 65 20 5b 64 61 74 61  d with the [data
1c840 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
1c850 20 68 61 6e 64 6c 65 20 73 68 6f 75 6c 64 20 62   handle should b
1c860 65 20 72 65 6c 65 61 73 65 64 20 62 79 0a 2a 2a  e released by.**
1c870 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20 5b   passing it to [
1c880 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d  sqlite3_close()]
1c890 20 77 68 65 6e 20 69 74 20 69 73 20 6e 6f 20 6c   when it is no l
1c8a0 6f 6e 67 65 72 20 72 65 71 75 69 72 65 64 2e 0a  onger required..
1c8b0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
1c8c0 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 6e 74 65  3_open_v2() inte
1c8d0 72 66 61 63 65 20 77 6f 72 6b 73 20 6c 69 6b 65  rface works like
1c8e0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 0a   sqlite3_open().
1c8f0 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69  ** except that i
1c900 74 20 61 63 63 65 70 74 73 20 74 77 6f 20 61 64  t accepts two ad
1c910 64 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d 65 74  ditional paramet
1c920 65 72 73 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  ers for addition
1c930 61 6c 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 6f 76  al control.** ov
1c940 65 72 20 74 68 65 20 6e 65 77 20 64 61 74 61 62  er the new datab
1c950 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  ase connection. 
1c960 20 54 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d   The flags param
1c970 65 74 65 72 20 63 61 6e 20 74 61 6b 65 20 6f 6e  eter can take on
1c980 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c  e of.** the foll
1c990 6f 77 69 6e 67 20 74 68 72 65 65 20 76 61 6c 75  owing three valu
1c9a0 65 73 2c 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 63  es, optionally c
1c9b0 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 74 68 65  ombined with the
1c9c0 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50 45   .** [SQLITE_OPE
1c9d0 4e 5f 4e 4f 4d 55 54 45 58 5d 2c 20 5b 53 51 4c  N_NOMUTEX], [SQL
1c9e0 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54  ITE_OPEN_FULLMUT
1c9f0 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 50 45  EX], [SQLITE_OPE
1ca00 4e 5f 53 48 41 52 45 44 43 41 43 48 45 5d 2c 0a  N_SHAREDCACHE],.
1ca10 2a 2a 20 61 6e 64 2f 6f 72 20 5b 53 51 4c 49 54  ** and/or [SQLIT
1ca20 45 5f 4f 50 45 4e 5f 50 52 49 56 41 54 45 43 41  E_OPEN_PRIVATECA
1ca30 43 48 45 5d 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a  CHE] flags:.**.*
1ca40 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 5b 53  * <dl>.** <dt>[S
1ca50 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
1ca60 4e 4c 59 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  NLY]</dt>.** <dd
1ca70 3e 54 68 65 20 64 61 74 61 62 61 73 65 20 69 73  >The database is
1ca80 20 6f 70 65 6e 65 64 20 69 6e 20 72 65 61 64 2d   opened in read-
1ca90 6f 6e 6c 79 20 6d 6f 64 65 2e 20 20 49 66 20 74  only mode.  If t
1caa0 68 65 20 64 61 74 61 62 61 73 65 20 64 6f 65 73  he database does
1cab0 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79 20   not.** already 
1cac0 65 78 69 73 74 2c 20 61 6e 20 65 72 72 6f 72 20  exist, an error 
1cad0 69 73 20 72 65 74 75 72 6e 65 64 2e 3c 2f 64 64  is returned.</dd
1cae0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c  >.**.** <dt>[SQL
1caf0 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
1cb00 54 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  TE]</dt>.** <dd>
1cb10 54 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  The database is 
1cb20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69  opened for readi
1cb30 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67 20 69  ng and writing i
1cb40 66 20 70 6f 73 73 69 62 6c 65 2c 20 6f 72 20 72  f possible, or r
1cb50 65 61 64 69 6e 67 0a 2a 2a 20 6f 6e 6c 79 20 69  eading.** only i
1cb60 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 77 72  f the file is wr
1cb70 69 74 65 20 70 72 6f 74 65 63 74 65 64 20 62 79  ite protected by
1cb80 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73   the operating s
1cb90 79 73 74 65 6d 2e 20 20 49 6e 20 65 69 74 68 65  ystem.  In eithe
1cba0 72 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 64 61  r.** case the da
1cbb0 74 61 62 61 73 65 20 6d 75 73 74 20 61 6c 72 65  tabase must alre
1cbc0 61 64 79 20 65 78 69 73 74 2c 20 6f 74 68 65 72  ady exist, other
1cbd0 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20 69 73  wise an error is
1cbe0 20 72 65 74 75 72 6e 65 64 2e 3c 2f 64 64 3e 0a   returned.</dd>.
1cbf0 2a 2a 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54  **.** <dt>[SQLIT
1cc00 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
1cc10 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  ] | [SQLITE_OPEN
1cc20 5f 43 52 45 41 54 45 5d 3c 2f 64 74 3e 0a 2a 2a  _CREATE]</dt>.**
1cc30 20 3c 64 64 3e 54 68 65 20 64 61 74 61 62 61 73   <dd>The databas
1cc40 65 20 69 73 20 6f 70 65 6e 65 64 20 66 6f 72 20  e is opened for 
1cc50 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74  reading and writ
1cc60 69 6e 67 2c 20 61 6e 64 20 69 73 20 63 72 65 61  ing, and is crea
1cc70 74 65 73 20 69 74 20 69 66 0a 2a 2a 20 69 74 20  tes it if.** it 
1cc80 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  does not already
1cc90 20 65 78 69 73 74 2e 20 54 68 69 73 20 69 73 20   exist. This is 
1cca0 74 68 65 20 62 65 68 61 76 69 6f 72 20 74 68 61  the behavior tha
1ccb0 74 20 69 73 20 61 6c 77 61 79 73 20 75 73 65 64  t is always used
1ccc0 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   for.** sqlite3_
1ccd0 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c 69 74  open() and sqlit
1cce0 65 33 5f 6f 70 65 6e 31 36 28 29 2e 3c 2f 64 64  e3_open16().</dd
1ccf0 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a  >.** </dl>.**.**
1cd00 20 49 66 20 74 68 65 20 33 72 64 20 70 61 72 61   If the 3rd para
1cd10 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33  meter to sqlite3
1cd20 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20 6e 6f  _open_v2() is no
1cd30 74 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20  t one of the.** 
1cd40 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68 6f  combinations sho
1cd50 77 6e 20 61 62 6f 76 65 20 6f 72 20 6f 6e 65 20  wn above or one 
1cd60 6f 66 20 74 68 65 20 63 6f 6d 62 69 6e 61 74 69  of the combinati
1cd70 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f 76 65 20  ons shown above 
1cd80 63 6f 6d 62 69 6e 65 64 0a 2a 2a 20 77 69 74 68  combined.** with
1cd90 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45   the [SQLITE_OPE
1cda0 4e 5f 4e 4f 4d 55 54 45 58 5d 2c 20 5b 53 51 4c  N_NOMUTEX], [SQL
1cdb0 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54  ITE_OPEN_FULLMUT
1cdc0 45 58 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  EX],.** [SQLITE_
1cdd0 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48 45  OPEN_SHAREDCACHE
1cde0 5d 20 61 6e 64 2f 6f 72 20 5b 53 51 4c 49 54 45  ] and/or [SQLITE
1cdf0 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48  _OPEN_SHAREDCACH
1ce00 45 5d 20 66 6c 61 67 73 2c 0a 2a 2a 20 74 68 65  E] flags,.** the
1ce10 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  n the behavior i
1ce20 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a  s undefined..**.
1ce30 2a 2a 20 49 66 20 74 68 65 20 5b 53 51 4c 49 54  ** If the [SQLIT
1ce40 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 20  E_OPEN_NOMUTEX] 
1ce50 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65  flag is set, the
1ce60 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  n the database c
1ce70 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6f 70 65  onnection.** ope
1ce80 6e 73 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d  ns in the multi-
1ce90 74 68 72 65 61 64 20 5b 74 68 72 65 61 64 69 6e  thread [threadin
1cea0 67 20 6d 6f 64 65 5d 20 61 73 20 6c 6f 6e 67 20  g mode] as long 
1ceb0 61 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 74 68  as the single-th
1cec0 72 65 61 64 0a 2a 2a 20 6d 6f 64 65 20 68 61 73  read.** mode has
1ced0 20 6e 6f 74 20 62 65 65 6e 20 73 65 74 20 61 74   not been set at
1cee0 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72   compile-time or
1cef0 20 73 74 61 72 74 2d 74 69 6d 65 2e 20 20 49 66   start-time.  If
1cf00 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f   the.** [SQLITE_
1cf10 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20  OPEN_FULLMUTEX] 
1cf20 66 6c 61 67 20 69 73 20 73 65 74 20 74 68 65 6e  flag is set then
1cf30 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
1cf40 6e 6e 65 63 74 69 6f 6e 20 6f 70 65 6e 73 0a 2a  nnection opens.*
1cf50 2a 20 69 6e 20 74 68 65 20 73 65 72 69 61 6c 69  * in the seriali
1cf60 7a 65 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d  zed [threading m
1cf70 6f 64 65 5d 20 75 6e 6c 65 73 73 20 73 69 6e 67  ode] unless sing
1cf80 6c 65 2d 74 68 72 65 61 64 20 77 61 73 0a 2a 2a  le-thread was.**
1cf90 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 6c 65   previously sele
1cfa0 63 74 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d  cted at compile-
1cfb0 74 69 6d 65 20 6f 72 20 73 74 61 72 74 2d 74 69  time or start-ti
1cfc0 6d 65 2e 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49  me..** The [SQLI
1cfd0 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41  TE_OPEN_SHAREDCA
1cfe0 43 48 45 5d 20 66 6c 61 67 20 63 61 75 73 65 73  CHE] flag causes
1cff0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
1d000 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 62 65 0a 2a  nnection to be.*
1d010 2a 20 65 6c 69 67 69 62 6c 65 20 74 6f 20 75 73  * eligible to us
1d020 65 20 5b 73 68 61 72 65 64 20 63 61 63 68 65 20  e [shared cache 
1d030 6d 6f 64 65 5d 2c 20 72 65 67 61 72 64 6c 65 73  mode], regardles
1d040 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20  s of whether or 
1d050 6e 6f 74 20 73 68 61 72 65 64 0a 2a 2a 20 63 61  not shared.** ca
1d060 63 68 65 20 69 73 20 65 6e 61 62 6c 65 64 20 75  che is enabled u
1d070 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 65 6e  sing [sqlite3_en
1d080 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68  able_shared_cach
1d090 65 28 29 5d 2e 20 20 54 68 65 0a 2a 2a 20 5b 53  e()].  The.** [S
1d0a0 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56 41  QLITE_OPEN_PRIVA
1d0b0 54 45 43 41 43 48 45 5d 20 66 6c 61 67 20 63 61  TECACHE] flag ca
1d0c0 75 73 65 73 20 74 68 65 20 64 61 74 61 62 61 73  uses the databas
1d0d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20  e connection to 
1d0e0 6e 6f 74 0a 2a 2a 20 70 61 72 74 69 63 69 70 61  not.** participa
1d0f0 74 65 20 69 6e 20 5b 73 68 61 72 65 64 20 63 61  te in [shared ca
1d100 63 68 65 20 6d 6f 64 65 5d 20 65 76 65 6e 20 69  che mode] even i
1d110 66 20 69 74 20 69 73 20 65 6e 61 62 6c 65 64 2e  f it is enabled.
1d120 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69  .**.** If the fi
1d130 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f  lename is ":memo
1d140 72 79 3a 22 2c 20 74 68 65 6e 20 61 20 70 72 69  ry:", then a pri
1d150 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72 79 20  vate, temporary 
1d160 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
1d170 73 65 0a 2a 2a 20 69 73 20 63 72 65 61 74 65 64  se.** is created
1d180 20 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65 63 74   for the connect
1d190 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e 2d 6d 65  ion.  This in-me
1d1a0 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 77 69  mory database wi
1d1b0 6c 6c 20 76 61 6e 69 73 68 20 77 68 65 6e 0a 2a  ll vanish when.*
1d1c0 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  * the database c
1d1d0 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f  onnection is clo
1d1e0 73 65 64 2e 20 20 46 75 74 75 72 65 20 76 65 72  sed.  Future ver
1d1f0 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20  sions of SQLite 
1d200 6d 69 67 68 74 0a 2a 2a 20 6d 61 6b 65 20 75 73  might.** make us
1d210 65 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 6c 20  e of additional 
1d220 73 70 65 63 69 61 6c 20 66 69 6c 65 6e 61 6d 65  special filename
1d230 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69 74  s that begin wit
1d240 68 20 74 68 65 20 22 3a 22 20 63 68 61 72 61 63  h the ":" charac
1d250 74 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 72 65  ter..** It is re
1d260 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 77  commended that w
1d270 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 66  hen a database f
1d280 69 6c 65 6e 61 6d 65 20 61 63 74 75 61 6c 6c 79  ilename actually
1d290 20 64 6f 65 73 20 62 65 67 69 6e 20 77 69 74 68   does begin with
1d2a0 0a 2a 2a 20 61 20 22 3a 22 20 63 68 61 72 61 63  .** a ":" charac
1d2b0 74 65 72 20 79 6f 75 20 73 68 6f 75 6c 64 20 70  ter you should p
1d2c0 72 65 66 69 78 20 74 68 65 20 66 69 6c 65 6e 61  refix the filena
1d2d0 6d 65 20 77 69 74 68 20 61 20 70 61 74 68 6e 61  me with a pathna
1d2e0 6d 65 20 73 75 63 68 20 61 73 0a 2a 2a 20 22 2e  me such as.** ".
1d2f0 2f 22 20 74 6f 20 61 76 6f 69 64 20 61 6d 62 69  /" to avoid ambi
1d300 67 75 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  guity..**.** If 
1d310 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69 73 20  the filename is 
1d320 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c  an empty string,
1d330 20 74 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c   then a private,
1d340 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a 20 6f 6e   temporary.** on
1d350 2d 64 69 73 6b 20 64 61 74 61 62 61 73 65 20 77  -disk database w
1d360 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64 2e 20  ill be created. 
1d370 20 54 68 69 73 20 70 72 69 76 61 74 65 20 64 61   This private da
1d380 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 0a 2a  tabase will be.*
1d390 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  * automatically 
1d3a0 64 65 6c 65 74 65 64 20 61 73 20 73 6f 6f 6e 20  deleted as soon 
1d3b0 61 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  as the database 
1d3c0 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c  connection is cl
1d3d0 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  osed..**.** The 
1d3e0 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72  fourth parameter
1d3f0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e   to sqlite3_open
1d400 5f 76 32 28 29 20 69 73 20 74 68 65 20 6e 61 6d  _v2() is the nam
1d410 65 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c  e of the.** [sql
1d420 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74  ite3_vfs] object
1d430 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68   that defines th
1d440 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  e operating syst
1d450 65 6d 20 69 6e 74 65 72 66 61 63 65 20 74 68 61  em interface tha
1d460 74 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74  t.** the new dat
1d470 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1d480 20 73 68 6f 75 6c 64 20 75 73 65 2e 20 20 49 66   should use.  If
1d490 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61   the fourth para
1d4a0 6d 65 74 65 72 20 69 73 0a 2a 2a 20 61 20 4e 55  meter is.** a NU
1d4b0 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20  LL pointer then 
1d4c0 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c  the default [sql
1d4d0 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74  ite3_vfs] object
1d4e0 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20   is used..**.** 
1d4f0 3c 62 3e 4e 6f 74 65 20 74 6f 20 57 69 6e 64 6f  <b>Note to Windo
1d500 77 73 20 75 73 65 72 73 3a 3c 2f 62 3e 20 20 54  ws users:</b>  T
1d510 68 65 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64  he encoding used
1d520 20 66 6f 72 20 74 68 65 20 66 69 6c 65 6e 61 6d   for the filenam
1d530 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 6f 66  e argument.** of
1d540 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20   sqlite3_open() 
1d550 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  and sqlite3_open
1d560 5f 76 32 28 29 20 6d 75 73 74 20 62 65 20 55 54  _v2() must be UT
1d570 46 2d 38 2c 20 6e 6f 74 20 77 68 61 74 65 76 65  F-8, not whateve
1d580 72 0a 2a 2a 20 63 6f 64 65 70 61 67 65 20 69 73  r.** codepage is
1d590 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e   currently defin
1d5a0 65 64 2e 20 20 46 69 6c 65 6e 61 6d 65 73 20 63  ed.  Filenames c
1d5b0 6f 6e 74 61 69 6e 69 6e 67 20 69 6e 74 65 72 6e  ontaining intern
1d5c0 61 74 69 6f 6e 61 6c 0a 2a 2a 20 63 68 61 72 61  ational.** chara
1d5d0 63 74 65 72 73 20 6d 75 73 74 20 62 65 20 63 6f  cters must be co
1d5e0 6e 76 65 72 74 65 64 20 74 6f 20 55 54 46 2d 38  nverted to UTF-8
1d5f0 20 70 72 69 6f 72 20 74 6f 20 70 61 73 73 69 6e   prior to passin
1d600 67 20 74 68 65 6d 20 69 6e 74 6f 0a 2a 2a 20 73  g them into.** s
1d610 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 6f 72  qlite3_open() or
1d620 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
1d630 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ()..**.** Requir
1d640 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 37  ements:.** [H127
1d650 30 31 5d 20 5b 48 31 32 37 30 32 5d 20 5b 48 31  01] [H12702] [H1
1d660 32 37 30 33 5d 20 5b 48 31 32 37 30 34 5d 20 5b  2703] [H12704] [
1d670 48 31 32 37 30 36 5d 20 5b 48 31 32 37 30 37 5d  H12706] [H12707]
1d680 20 5b 48 31 32 37 30 39 5d 20 5b 48 31 32 37 31   [H12709] [H1271
1d690 31 5d 0a 2a 2a 20 5b 48 31 32 37 31 32 5d 20 5b  1].** [H12712] [
1d6a0 48 31 32 37 31 33 5d 20 5b 48 31 32 37 31 34 5d  H12713] [H12714]
1d6b0 20 5b 48 31 32 37 31 37 5d 20 5b 48 31 32 37 31   [H12717] [H1271
1d6c0 39 5d 20 5b 48 31 32 37 32 31 5d 20 5b 48 31 32  9] [H12721] [H12
1d6d0 37 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  723].*/.SQLITE_A
1d6e0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
1d6f0 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61  pen(.  const cha
1d700 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f  r *filename,   /
1d710 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e  * Database filen
1d720 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20  ame (UTF-8) */. 
1d730 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 20   sqlite3 **ppDb 
1d740 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
1d750 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c   SQLite db handl
1d760 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41  e */.);.SQLITE_A
1d770 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
1d780 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73 74 20 76  pen16(.  const v
1d790 6f 69 64 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20  oid *filename,  
1d7a0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c   /* Database fil
1d7b0 65 6e 61 6d 65 20 28 55 54 46 2d 31 36 29 20 2a  ename (UTF-16) *
1d7c0 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70  /.  sqlite3 **pp
1d7d0 44 62 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  Db          /* O
1d7e0 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61  UT: SQLite db ha
1d7f0 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54  ndle */.);.SQLIT
1d800 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1d810 33 5f 6f 70 65 6e 5f 76 32 28 0a 20 20 63 6f 6e  3_open_v2(.  con
1d820 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e 61 6d  st char *filenam
1d830 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65  e,   /* Database
1d840 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d 38   filename (UTF-8
1d850 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  ) */.  sqlite3 *
1d860 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20 20 2f  *ppDb,         /
1d870 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62  * OUT: SQLite db
1d880 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74   handle */.  int
1d890 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
1d8a0 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f       /* Flags */
1d8b0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1d8c0 56 66 73 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  Vfs        /* Na
1d8d0 6d 65 20 6f 66 20 56 46 53 20 6d 6f 64 75 6c 65  me of VFS module
1d8e0 20 74 6f 20 75 73 65 20 2a 2f 0a 29 3b 0a 0a 2f   to use */.);../
1d8f0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45  *.** CAPI3REF: E
1d900 72 72 6f 72 20 43 6f 64 65 73 20 41 6e 64 20 4d  rror Codes And M
1d910 65 73 73 61 67 65 73 20 7b 48 31 32 38 30 30 7d  essages {H12800}
1d920 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S60200>.**.** 
1d930 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72 72 63  The sqlite3_errc
1d940 6f 64 65 28 29 20 69 6e 74 65 72 66 61 63 65 20  ode() interface 
1d950 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 65  returns the nume
1d960 72 69 63 20 5b 72 65 73 75 6c 74 20 63 6f 64 65  ric [result code
1d970 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e 64 65  ] or.** [extende
1d980 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 66  d result code] f
1d990 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  or the most rece
1d9a0 6e 74 20 66 61 69 6c 65 64 20 73 71 6c 69 74 65  nt failed sqlite
1d9b0 33 5f 2a 20 41 50 49 20 63 61 6c 6c 0a 2a 2a 20  3_* API call.** 
1d9c0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
1d9d0 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  a [database conn
1d9e0 65 63 74 69 6f 6e 5d 2e 20 49 66 20 61 20 70 72  ection]. If a pr
1d9f0 69 6f 72 20 41 50 49 20 63 61 6c 6c 20 66 61 69  ior API call fai
1da00 6c 65 64 0a 2a 2a 20 62 75 74 20 74 68 65 20 6d  led.** but the m
1da10 6f 73 74 20 72 65 63 65 6e 74 20 41 50 49 20 63  ost recent API c
1da20 61 6c 6c 20 73 75 63 63 65 65 64 65 64 2c 20 74  all succeeded, t
1da30 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
1da40 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  from.** sqlite3_
1da50 65 72 72 63 6f 64 65 28 29 20 69 73 20 75 6e 64  errcode() is und
1da60 65 66 69 6e 65 64 2e 20 20 54 68 65 20 73 71 6c  efined.  The sql
1da70 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72  ite3_extended_er
1da80 72 63 6f 64 65 28 29 0a 2a 2a 20 69 6e 74 65 72  rcode().** inter
1da90 66 61 63 65 20 69 73 20 74 68 65 20 73 61 6d 65  face is the same
1daa0 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20   except that it 
1dab0 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 74  always returns t
1dac0 68 65 20 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64  he .** [extended
1dad0 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20 65 76   result code] ev
1dae0 65 6e 20 77 68 65 6e 20 65 78 74 65 6e 64 65 64  en when extended
1daf0 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61 72   result codes ar
1db00 65 0a 2a 2a 20 64 69 73 61 62 6c 65 64 2e 0a 2a  e.** disabled..*
1db10 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
1db20 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71  _errmsg() and sq
1db30 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29  lite3_errmsg16()
1db40 20 72 65 74 75 72 6e 20 45 6e 67 6c 69 73 68 2d   return English-
1db50 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 74 65 78 74  language.** text
1db60 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20   that describes 
1db70 74 68 65 20 65 72 72 6f 72 2c 20 61 73 20 65 69  the error, as ei
1db80 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55 54  ther UTF-8 or UT
1db90 46 2d 31 36 20 72 65 73 70 65 63 74 69 76 65 6c  F-16 respectivel
1dba0 79 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74 6f 20  y..** Memory to 
1dbb0 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72 20 6d  hold the error m
1dbc0 65 73 73 61 67 65 20 73 74 72 69 6e 67 20 69 73  essage string is
1dbd0 20 6d 61 6e 61 67 65 64 20 69 6e 74 65 72 6e 61   managed interna
1dbe0 6c 6c 79 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c  lly..** The appl
1dbf0 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74  ication does not
1dc00 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 20 61   need to worry a
1dc10 62 6f 75 74 20 66 72 65 65 69 6e 67 20 74 68 65  bout freeing the
1dc20 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 48 6f 77 65   result..** Howe
1dc30 76 65 72 2c 20 74 68 65 20 65 72 72 6f 72 20 73  ver, the error s
1dc40 74 72 69 6e 67 20 6d 69 67 68 74 20 62 65 20 6f  tring might be o
1dc50 76 65 72 77 72 69 74 74 65 6e 20 6f 72 20 64 65  verwritten or de
1dc60 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a 20  allocated by.** 
1dc70 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  subsequent calls
1dc80 20 74 6f 20 6f 74 68 65 72 20 53 51 4c 69 74 65   to other SQLite
1dc90 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e 63 74   interface funct
1dca0 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  ions..**.** When
1dcb0 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20   the serialized 
1dcc0 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d  [threading mode]
1dcd0 20 69 73 20 69 6e 20 75 73 65 2c 20 69 74 20 6d   is in use, it m
1dce0 69 67 68 74 20 62 65 20 74 68 65 0a 2a 2a 20 63  ight be the.** c
1dcf0 61 73 65 20 74 68 61 74 20 61 20 73 65 63 6f 6e  ase that a secon
1dd00 64 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 6f  d error occurs o
1dd10 6e 20 61 20 73 65 70 61 72 61 74 65 20 74 68 72  n a separate thr
1dd20 65 61 64 20 69 6e 20 62 65 74 77 65 65 6e 0a 2a  ead in between.*
1dd30 2a 20 74 68 65 20 74 69 6d 65 20 6f 66 20 74 68  * the time of th
1dd40 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 61 6e  e first error an
1dd50 64 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 74 68  d the call to th
1dd60 65 73 65 20 69 6e 74 65 72 66 61 63 65 73 2e 0a  ese interfaces..
1dd70 2a 2a 20 57 68 65 6e 20 74 68 61 74 20 68 61 70  ** When that hap
1dd80 70 65 6e 73 2c 20 74 68 65 20 73 65 63 6f 6e 64  pens, the second
1dd90 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 72   error will be r
1dda0 65 70 6f 72 74 65 64 20 73 69 6e 63 65 20 74 68  eported since th
1ddb0 65 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  ese.** interface
1ddc0 73 20 61 6c 77 61 79 73 20 72 65 70 6f 72 74 20  s always report 
1ddd0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
1dde0 72 65 73 75 6c 74 2e 20 20 54 6f 20 61 76 6f 69  result.  To avoi
1ddf0 64 0a 2a 2a 20 74 68 69 73 2c 20 65 61 63 68 20  d.** this, each 
1de00 74 68 72 65 61 64 20 63 61 6e 20 6f 62 74 61 69  thread can obtai
1de10 6e 20 65 78 63 6c 75 73 69 76 65 20 75 73 65 20  n exclusive use 
1de20 6f 66 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  of the [database
1de30 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 0a 2a   connection] D.*
1de40 2a 20 62 79 20 69 6e 76 6f 6b 69 6e 67 20 5b 73  * by invoking [s
1de50 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
1de60 65 72 5d 28 5b 73 71 6c 69 74 65 33 5f 64 62 5f  er]([sqlite3_db_
1de70 6d 75 74 65 78 5d 28 44 29 29 20 62 65 66 6f 72  mutex](D)) befor
1de80 65 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 74  e beginning.** t
1de90 6f 20 75 73 65 20 44 20 61 6e 64 20 69 6e 76 6f  o use D and invo
1dea0 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 75  king [sqlite3_mu
1deb0 74 65 78 5f 6c 65 61 76 65 5d 28 5b 73 71 6c 69  tex_leave]([sqli
1dec0 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28 44 29  te3_db_mutex](D)
1ded0 29 20 61 66 74 65 72 0a 2a 2a 20 61 6c 6c 20 63  ) after.** all c
1dee0 61 6c 6c 73 20 74 6f 20 74 68 65 20 69 6e 74 65  alls to the inte
1def0 72 66 61 63 65 73 20 6c 69 73 74 65 64 20 68 65  rfaces listed he
1df00 72 65 20 61 72 65 20 63 6f 6d 70 6c 65 74 65 64  re are completed
1df10 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6e  ..**.** If an in
1df20 74 65 72 66 61 63 65 20 66 61 69 6c 73 20 77 69  terface fails wi
1df30 74 68 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  th SQLITE_MISUSE
1df40 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 65  , that means the
1df50 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 77 61   interface.** wa
1df60 73 20 69 6e 76 6f 6b 65 64 20 69 6e 63 6f 72 72  s invoked incorr
1df70 65 63 74 6c 79 20 62 79 20 74 68 65 20 61 70 70  ectly by the app
1df80 6c 69 63 61 74 69 6f 6e 2e 20 20 49 6e 20 74 68  lication.  In th
1df90 61 74 20 63 61 73 65 2c 20 74 68 65 0a 2a 2a 20  at case, the.** 
1dfa0 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 6d  error code and m
1dfb0 65 73 73 61 67 65 20 6d 61 79 20 6f 72 20 6d 61  essage may or ma
1dfc0 79 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a 2a 2a  y not be set..**
1dfd0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
1dfe0 3a 0a 2a 2a 20 5b 48 31 32 38 30 31 5d 20 5b 48  :.** [H12801] [H
1dff0 31 32 38 30 32 5d 20 5b 48 31 32 38 30 33 5d 20  12802] [H12803] 
1e000 5b 48 31 32 38 30 37 5d 20 5b 48 31 32 38 30 38  [H12807] [H12808
1e010 5d 20 5b 48 31 32 38 30 39 5d 0a 2a 2f 0a 53 51  ] [H12809].*/.SQ
1e020 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1e030 69 74 65 33 5f 65 72 72 63 6f 64 65 28 73 71 6c  ite3_errcode(sql
1e040 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54  ite3 *db);.SQLIT
1e050 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1e060 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63 6f  3_extended_errco
1e070 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b  de(sqlite3 *db);
1e080 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
1e090 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  t char *sqlite3_
1e0a0 65 72 72 6d 73 67 28 73 71 6c 69 74 65 33 2a 29  errmsg(sqlite3*)
1e0b0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ;.SQLITE_API con
1e0c0 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
1e0d0 5f 65 72 72 6d 73 67 31 36 28 73 71 6c 69 74 65  _errmsg16(sqlite
1e0e0 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  3*);../*.** CAPI
1e0f0 33 52 45 46 3a 20 53 51 4c 20 53 74 61 74 65 6d  3REF: SQL Statem
1e100 65 6e 74 20 4f 62 6a 65 63 74 20 7b 48 31 33 30  ent Object {H130
1e110 30 30 7d 20 3c 48 31 33 30 31 30 3e 0a 2a 2a 20  00} <H13010>.** 
1e120 4b 45 59 57 4f 52 44 53 3a 20 7b 70 72 65 70 61  KEYWORDS: {prepa
1e130 72 65 64 20 73 74 61 74 65 6d 65 6e 74 7d 20 7b  red statement} {
1e140 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
1e150 6e 74 73 7d 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e  nts}.**.** An in
1e160 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f  stance of this o
1e170 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 73  bject represents
1e180 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 74   a single SQL st
1e190 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54 68 69 73  atement..** This
1e1a0 20 6f 62 6a 65 63 74 20 69 73 20 76 61 72 69 6f   object is vario
1e1b0 75 73 6c 79 20 6b 6e 6f 77 6e 20 61 73 20 61 20  usly known as a 
1e1c0 22 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  "prepared statem
1e1d0 65 6e 74 22 20 6f 72 20 61 0a 2a 2a 20 22 63 6f  ent" or a.** "co
1e1e0 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65  mpiled SQL state
1e1f0 6d 65 6e 74 22 20 6f 72 20 73 69 6d 70 6c 79 20  ment" or simply 
1e200 61 73 20 61 20 22 73 74 61 74 65 6d 65 6e 74 22  as a "statement"
1e210 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 69 66 65  ..**.** The life
1e220 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20   of a statement 
1e230 6f 62 6a 65 63 74 20 67 6f 65 73 20 73 6f 6d 65  object goes some
1e240 74 68 69 6e 67 20 6c 69 6b 65 20 74 68 69 73 3a  thing like this:
1e250 0a 2a 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c  .**.** <ol>.** <
1e260 6c 69 3e 20 43 72 65 61 74 65 20 74 68 65 20 6f  li> Create the o
1e270 62 6a 65 63 74 20 75 73 69 6e 67 20 5b 73 71 6c  bject using [sql
1e280 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
1e290 29 5d 20 6f 72 20 61 20 72 65 6c 61 74 65 64 0a  )] or a related.
1e2a0 2a 2a 20 20 20 20 20 20 66 75 6e 63 74 69 6f 6e  **      function
1e2b0 2e 0a 2a 2a 20 3c 6c 69 3e 20 42 69 6e 64 20 76  ..** <li> Bind v
1e2c0 61 6c 75 65 73 20 74 6f 20 5b 68 6f 73 74 20 70  alues to [host p
1e2d0 61 72 61 6d 65 74 65 72 73 5d 20 75 73 69 6e 67  arameters] using
1e2e0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e   the sqlite3_bin
1e2f0 64 5f 2a 28 29 0a 2a 2a 20 20 20 20 20 20 69 6e  d_*().**      in
1e300 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 3c 6c 69  terfaces..** <li
1e310 3e 20 52 75 6e 20 74 68 65 20 53 51 4c 20 62 79  > Run the SQL by
1e320 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65   calling [sqlite
1e330 33 5f 73 74 65 70 28 29 5d 20 6f 6e 65 20 6f 72  3_step()] one or
1e340 20 6d 6f 72 65 20 74 69 6d 65 73 2e 0a 2a 2a 20   more times..** 
1e350 3c 6c 69 3e 20 52 65 73 65 74 20 74 68 65 20 73  <li> Reset the s
1e360 74 61 74 65 6d 65 6e 74 20 75 73 69 6e 67 20 5b  tatement using [
1e370 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
1e380 20 74 68 65 6e 20 67 6f 20 62 61 63 6b 0a 2a 2a   then go back.**
1e390 20 20 20 20 20 20 74 6f 20 73 74 65 70 20 32 2e        to step 2.
1e3a0 20 20 44 6f 20 74 68 69 73 20 7a 65 72 6f 20 6f    Do this zero o
1e3b0 72 20 6d 6f 72 65 20 74 69 6d 65 73 2e 0a 2a 2a  r more times..**
1e3c0 20 3c 6c 69 3e 20 44 65 73 74 72 6f 79 20 74 68   <li> Destroy th
1e3d0 65 20 6f 62 6a 65 63 74 20 75 73 69 6e 67 20 5b  e object using [
1e3e0 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
1e3f0 28 29 5d 2e 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a  ()]..** </ol>.**
1e400 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 64 6f 63  .** Refer to doc
1e410 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e 20 69 6e  umentation on in
1e420 64 69 76 69 64 75 61 6c 20 6d 65 74 68 6f 64 73  dividual methods
1e430 20 61 62 6f 76 65 20 66 6f 72 20 61 64 64 69 74   above for addit
1e440 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61  ional.** informa
1e450 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  tion..*/.typedef
1e460 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
1e470 73 74 6d 74 20 73 71 6c 69 74 65 33 5f 73 74 6d  stmt sqlite3_stm
1e480 74 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  t;../*.** CAPI3R
1e490 45 46 3a 20 52 75 6e 2d 74 69 6d 65 20 4c 69 6d  EF: Run-time Lim
1e4a0 69 74 73 20 7b 48 31 32 37 36 30 7d 20 3c 53 32  its {H12760} <S2
1e4b0 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  0600>.**.** This
1e4c0 20 69 6e 74 65 72 66 61 63 65 20 61 6c 6c 6f 77   interface allow
1e4d0 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 76 61  s the size of va
1e4e0 72 69 6f 75 73 20 63 6f 6e 73 74 72 75 63 74 73  rious constructs
1e4f0 20 74 6f 20 62 65 20 6c 69 6d 69 74 65 64 0a 2a   to be limited.*
1e500 2a 20 6f 6e 20 61 20 63 6f 6e 6e 65 63 74 69 6f  * on a connectio
1e510 6e 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  n by connection 
1e520 62 61 73 69 73 2e 20 20 54 68 65 20 66 69 72 73  basis.  The firs
1e530 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  t parameter is t
1e540 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20  he.** [database 
1e550 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 77 68 6f 73  connection] whos
1e560 65 20 6c 69 6d 69 74 20 69 73 20 74 6f 20 62 65  e limit is to be
1e570 20 73 65 74 20 6f 72 20 71 75 65 72 69 65 64 2e   set or queried.
1e580 20 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20    The.** second 
1e590 70 61 72 61 6d 65 74 65 72 20 69 73 20 6f 6e 65  parameter is one
1e5a0 20 6f 66 20 74 68 65 20 5b 6c 69 6d 69 74 20 63   of the [limit c
1e5b0 61 74 65 67 6f 72 69 65 73 5d 20 74 68 61 74 20  ategories] that 
1e5c0 64 65 66 69 6e 65 20 61 0a 2a 2a 20 63 6c 61 73  define a.** clas
1e5d0 73 20 6f 66 20 63 6f 6e 73 74 72 75 63 74 73 20  s of constructs 
1e5e0 74 6f 20 62 65 20 73 69 7a 65 20 6c 69 6d 69 74  to be size limit
1e5f0 65 64 2e 20 20 54 68 65 20 74 68 69 72 64 20 70  ed.  The third p
1e600 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 0a  arameter is the.
1e610 2a 2a 20 6e 65 77 20 6c 69 6d 69 74 20 66 6f 72  ** new limit for
1e620 20 74 68 61 74 20 63 6f 6e 73 74 72 75 63 74 2e   that construct.
1e630 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 72    The function r
1e640 65 74 75 72 6e 73 20 74 68 65 20 6f 6c 64 20 6c  eturns the old l
1e650 69 6d 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  imit..**.** If t
1e660 68 65 20 6e 65 77 20 6c 69 6d 69 74 20 69 73 20  he new limit is 
1e670 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65  a negative numbe
1e680 72 2c 20 74 68 65 20 6c 69 6d 69 74 20 69 73 20  r, the limit is 
1e690 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 46 6f  unchanged..** Fo
1e6a0 72 20 74 68 65 20 6c 69 6d 69 74 20 63 61 74 65  r the limit cate
1e6b0 67 6f 72 79 20 6f 66 20 53 51 4c 49 54 45 5f 4c  gory of SQLITE_L
1e6c0 49 4d 49 54 5f 58 59 5a 20 74 68 65 72 65 20 69  IMIT_XYZ there i
1e6d0 73 20 61 20 0a 2a 2a 20 5b 6c 69 6d 69 74 73 20  s a .** [limits 
1e6e0 7c 20 68 61 72 64 20 75 70 70 65 72 20 62 6f 75  | hard upper bou
1e6f0 6e 64 5d 0a 2a 2a 20 73 65 74 20 62 79 20 61 20  nd].** set by a 
1e700 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 43 20 70  compile-time C p
1e710 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72  reprocessor macr
1e720 6f 20 6e 61 6d 65 64 20 0a 2a 2a 20 5b 6c 69 6d  o named .** [lim
1e730 69 74 73 20 7c 20 53 51 4c 49 54 45 5f 4d 41 58  its | SQLITE_MAX
1e740 5f 58 59 5a 5d 2e 0a 2a 2a 20 28 54 68 65 20 22  _XYZ]..** (The "
1e750 5f 4c 49 4d 49 54 5f 22 20 69 6e 20 74 68 65 20  _LIMIT_" in the 
1e760 6e 61 6d 65 20 69 73 20 63 68 61 6e 67 65 64 20  name is changed 
1e770 74 6f 20 22 5f 4d 41 58 5f 22 2e 29 0a 2a 2a 20  to "_MAX_".).** 
1e780 41 74 74 65 6d 70 74 73 20 74 6f 20 69 6e 63 72  Attempts to incr
1e790 65 61 73 65 20 61 20 6c 69 6d 69 74 20 61 62 6f  ease a limit abo
1e7a0 76 65 20 69 74 73 20 68 61 72 64 20 75 70 70 65  ve its hard uppe
1e7b0 72 20 62 6f 75 6e 64 20 61 72 65 0a 2a 2a 20 73  r bound are.** s
1e7c0 69 6c 65 6e 74 6c 79 20 74 72 75 6e 63 61 74 65  ilently truncate
1e7d0 64 20 74 6f 20 74 68 65 20 68 61 72 64 20 75 70  d to the hard up
1e7e0 70 65 72 20 6c 69 6d 69 74 2e 0a 2a 2a 0a 2a 2a  per limit..**.**
1e7f0 20 52 75 6e 20 74 69 6d 65 20 6c 69 6d 69 74 73   Run time limits
1e800 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f   are intended fo
1e810 72 20 75 73 65 20 69 6e 20 61 70 70 6c 69 63 61  r use in applica
1e820 74 69 6f 6e 73 20 74 68 61 74 20 6d 61 6e 61 67  tions that manag
1e830 65 0a 2a 2a 20 62 6f 74 68 20 74 68 65 69 72 20  e.** both their 
1e840 6f 77 6e 20 69 6e 74 65 72 6e 61 6c 20 64 61 74  own internal dat
1e850 61 62 61 73 65 20 61 6e 64 20 61 6c 73 6f 20 64  abase and also d
1e860 61 74 61 62 61 73 65 73 20 74 68 61 74 20 61 72  atabases that ar
1e870 65 20 63 6f 6e 74 72 6f 6c 6c 65 64 0a 2a 2a 20  e controlled.** 
1e880 62 79 20 75 6e 74 72 75 73 74 65 64 20 65 78 74  by untrusted ext
1e890 65 72 6e 61 6c 20 73 6f 75 72 63 65 73 2e 20 20  ernal sources.  
1e8a0 41 6e 20 65 78 61 6d 70 6c 65 20 61 70 70 6c 69  An example appli
1e8b0 63 61 74 69 6f 6e 20 6d 69 67 68 74 20 62 65 20  cation might be 
1e8c0 61 0a 2a 2a 20 77 65 62 20 62 72 6f 77 73 65 72  a.** web browser
1e8d0 20 74 68 61 74 20 68 61 73 20 69 74 73 20 6f 77   that has its ow
1e8e0 6e 20 64 61 74 61 62 61 73 65 73 20 66 6f 72 20  n databases for 
1e8f0 73 74 6f 72 69 6e 67 20 68 69 73 74 6f 72 79 20  storing history 
1e900 61 6e 64 0a 2a 2a 20 73 65 70 61 72 61 74 65 20  and.** separate 
1e910 64 61 74 61 62 61 73 65 73 20 63 6f 6e 74 72 6f  databases contro
1e920 6c 6c 65 64 20 62 79 20 4a 61 76 61 53 63 72 69  lled by JavaScri
1e930 70 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  pt applications 
1e940 64 6f 77 6e 6c 6f 61 64 65 64 0a 2a 2a 20 6f 66  downloaded.** of
1e950 66 20 74 68 65 20 49 6e 74 65 72 6e 65 74 2e 20  f the Internet. 
1e960 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 20 64 61   The internal da
1e970 74 61 62 61 73 65 73 20 63 61 6e 20 62 65 20 67  tabases can be g
1e980 69 76 65 6e 20 74 68 65 0a 2a 2a 20 6c 61 72 67  iven the.** larg
1e990 65 2c 20 64 65 66 61 75 6c 74 20 6c 69 6d 69 74  e, default limit
1e9a0 73 2e 20 20 44 61 74 61 62 61 73 65 73 20 6d 61  s.  Databases ma
1e9b0 6e 61 67 65 64 20 62 79 20 65 78 74 65 72 6e 61  naged by externa
1e9c0 6c 20 73 6f 75 72 63 65 73 20 63 61 6e 0a 2a 2a  l sources can.**
1e9d0 20 62 65 20 67 69 76 65 6e 20 6d 75 63 68 20 73   be given much s
1e9e0 6d 61 6c 6c 65 72 20 6c 69 6d 69 74 73 20 64 65  maller limits de
1e9f0 73 69 67 6e 65 64 20 74 6f 20 70 72 65 76 65 6e  signed to preven
1ea00 74 20 61 20 64 65 6e 69 61 6c 20 6f 66 20 73 65  t a denial of se
1ea10 72 76 69 63 65 0a 2a 2a 20 61 74 74 61 63 6b 2e  rvice.** attack.
1ea20 20 20 44 65 76 65 6c 6f 70 65 72 73 20 6d 69 67    Developers mig
1ea30 68 74 20 61 6c 73 6f 20 77 61 6e 74 20 74 6f 20  ht also want to 
1ea40 75 73 65 20 74 68 65 20 5b 73 71 6c 69 74 65 33  use the [sqlite3
1ea50 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28  _set_authorizer(
1ea60 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  )].** interface 
1ea70 74 6f 20 66 75 72 74 68 65 72 20 63 6f 6e 74 72  to further contr
1ea80 6f 6c 20 75 6e 74 72 75 73 74 65 64 20 53 51 4c  ol untrusted SQL
1ea90 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74  .  The size of t
1eaa0 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63  he database.** c
1eab0 72 65 61 74 65 64 20 62 79 20 61 6e 20 75 6e 74  reated by an unt
1eac0 72 75 73 74 65 64 20 73 63 72 69 70 74 20 63 61  rusted script ca
1ead0 6e 20 62 65 20 63 6f 6e 74 61 69 6e 65 64 20 75  n be contained u
1eae0 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 6d 61 78  sing the.** [max
1eaf0 5f 70 61 67 65 5f 63 6f 75 6e 74 5d 20 5b 50 52  _page_count] [PR
1eb00 41 47 4d 41 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77  AGMA]..**.** New
1eb10 20 72 75 6e 2d 74 69 6d 65 20 6c 69 6d 69 74 20   run-time limit 
1eb20 63 61 74 65 67 6f 72 69 65 73 20 6d 61 79 20 62  categories may b
1eb30 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72  e added in futur
1eb40 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a  e releases..**.*
1eb50 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
1eb60 2a 2a 20 5b 48 31 32 37 36 32 5d 20 5b 48 31 32  ** [H12762] [H12
1eb70 37 36 36 5d 20 5b 48 31 32 37 36 39 5d 0a 2a 2f  766] [H12769].*/
1eb80 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1eb90 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 73 71  sqlite3_limit(sq
1eba0 6c 69 74 65 33 2a 2c 20 69 6e 74 20 69 64 2c 20  lite3*, int id, 
1ebb0 69 6e 74 20 6e 65 77 56 61 6c 29 3b 0a 0a 2f 2a  int newVal);../*
1ebc0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75  .** CAPI3REF: Ru
1ebd0 6e 2d 54 69 6d 65 20 4c 69 6d 69 74 20 43 61 74  n-Time Limit Cat
1ebe0 65 67 6f 72 69 65 73 20 7b 48 31 32 37 39 30 7d  egories {H12790}
1ebf0 20 3c 48 31 32 37 36 30 3e 0a 2a 2a 20 4b 45 59   <H12760>.** KEY
1ec00 57 4f 52 44 53 3a 20 7b 6c 69 6d 69 74 20 63 61  WORDS: {limit ca
1ec10 74 65 67 6f 72 79 7d 20 7b 6c 69 6d 69 74 20 63  tegory} {limit c
1ec20 61 74 65 67 6f 72 69 65 73 7d 0a 2a 2a 0a 2a 2a  ategories}.**.**
1ec30 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73   These constants
1ec40 20 64 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20   define various 
1ec50 70 65 72 66 6f 72 6d 61 6e 63 65 20 6c 69 6d 69  performance limi
1ec60 74 73 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62  ts.** that can b
1ec70 65 20 6c 6f 77 65 72 65 64 20 61 74 20 72 75 6e  e lowered at run
1ec80 2d 74 69 6d 65 20 75 73 69 6e 67 20 5b 73 71 6c  -time using [sql
1ec90 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d 2e 0a 2a  ite3_limit()]..*
1eca0 2a 20 54 68 65 20 73 79 6e 6f 70 73 69 73 20 6f  * The synopsis o
1ecb0 66 20 74 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f  f the meanings o
1ecc0 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 6c 69  f the various li
1ecd0 6d 69 74 73 20 69 73 20 73 68 6f 77 6e 20 62 65  mits is shown be
1ece0 6c 6f 77 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e  low..** Addition
1ecf0 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  al information i
1ed00 73 20 61 76 61 69 6c 61 62 6c 65 20 61 74 20 5b  s available at [
1ed10 6c 69 6d 69 74 73 20 7c 20 4c 69 6d 69 74 73 20  limits | Limits 
1ed20 69 6e 20 53 51 4c 69 74 65 5d 2e 0a 2a 2a 0a 2a  in SQLite]..**.*
1ed30 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51  * <dl>.** <dt>SQ
1ed40 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
1ed50 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  H</dt>.** <dd>Th
1ed60 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f  e maximum size o
1ed70 66 20 61 6e 79 20 73 74 72 69 6e 67 20 6f 72 20  f any string or 
1ed80 42 4c 4f 42 20 6f 72 20 74 61 62 6c 65 20 72 6f  BLOB or table ro
1ed90 77 2e 3c 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  w.<dd>.**.** <dt
1eda0 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51  >SQLITE_LIMIT_SQ
1edb0 4c 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a  L_LENGTH</dt>.**
1edc0 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d   <dd>The maximum
1edd0 20 6c 65 6e 67 74 68 20 6f 66 20 61 6e 20 53 51   length of an SQ
1ede0 4c 20 73 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64  L statement.</dd
1edf0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
1ee00 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 3c  TE_LIMIT_COLUMN<
1ee10 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20  /dt>.** <dd>The 
1ee20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
1ee30 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 74  f columns in a t
1ee40 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 20  able definition 
1ee50 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 72 65 73  or in the.** res
1ee60 75 6c 74 20 73 65 74 20 6f 66 20 61 20 5b 53 45  ult set of a [SE
1ee70 4c 45 43 54 5d 20 6f 72 20 74 68 65 20 6d 61 78  LECT] or the max
1ee80 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63  imum number of c
1ee90 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 20 69 6e 64  olumns in an ind
1eea0 65 78 0a 2a 2a 20 6f 72 20 69 6e 20 61 6e 20 4f  ex.** or in an O
1eeb0 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50  RDER BY or GROUP
1eec0 20 42 59 20 63 6c 61 75 73 65 2e 3c 2f 64 64 3e   BY clause.</dd>
1eed0 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
1eee0 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45 50  E_LIMIT_EXPR_DEP
1eef0 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  TH</dt>.** <dd>T
1ef00 68 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 74 68  he maximum depth
1ef10 20 6f 66 20 74 68 65 20 70 61 72 73 65 20 74 72   of the parse tr
1ef20 65 65 20 6f 6e 20 61 6e 79 20 65 78 70 72 65 73  ee on any expres
1ef30 73 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  sion.</dd>.**.**
1ef40 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49   <dt>SQLITE_LIMI
1ef50 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43  T_COMPOUND_SELEC
1ef60 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  T</dt>.** <dd>Th
1ef70 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
1ef80 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61 20 63   of terms in a c
1ef90 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73  ompound SELECT s
1efa0 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a  tatement.</dd>.*
1efb0 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
1efc0 4c 49 4d 49 54 5f 56 44 42 45 5f 4f 50 3c 2f 64  LIMIT_VDBE_OP</d
1efd0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61  t>.** <dd>The ma
1efe0 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
1eff0 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 69 6e 20  instructions in 
1f000 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  a virtual machin
1f010 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 75 73 65  e program.** use
1f020 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61  d to implement a
1f030 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  n SQL statement.
1f040 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
1f050 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e  SQLITE_LIMIT_FUN
1f060 43 54 49 4f 4e 5f 41 52 47 3c 2f 64 74 3e 0a 2a  CTION_ARG</dt>.*
1f070 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75  * <dd>The maximu
1f080 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  m number of argu
1f090 6d 65 6e 74 73 20 6f 6e 20 61 20 66 75 6e 63 74  ments on a funct
1f0a0 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ion.</dd>.**.** 
1f0b0 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54  <dt>SQLITE_LIMIT
1f0c0 5f 41 54 54 41 43 48 45 44 3c 2f 64 74 3e 0a 2a  _ATTACHED</dt>.*
1f0d0 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75  * <dd>The maximu
1f0e0 6d 20 6e 75 6d 62 65 72 20 6f 66 20 5b 41 54 54  m number of [ATT
1f0f0 41 43 48 20 7c 20 61 74 74 61 63 68 65 64 20 64  ACH | attached d
1f100 61 74 61 62 61 73 65 73 5d 2e 3c 2f 64 64 3e 0a  atabases].</dd>.
1f110 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
1f120 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54  _LIMIT_LIKE_PATT
1f130 45 52 4e 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a  ERN_LENGTH</dt>.
1f140 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d  ** <dd>The maxim
1f150 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  um length of the
1f160 20 70 61 74 74 65 72 6e 20 61 72 67 75 6d 65 6e   pattern argumen
1f170 74 20 74 6f 20 74 68 65 20 5b 4c 49 4b 45 5d 20  t to the [LIKE] 
1f180 6f 72 0a 2a 2a 20 5b 47 4c 4f 42 5d 20 6f 70 65  or.** [GLOB] ope
1f190 72 61 74 6f 72 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a  rators.</dd>.**.
1f1a0 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49  ** <dt>SQLITE_LI
1f1b0 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d  MIT_VARIABLE_NUM
1f1c0 42 45 52 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  BER</dt>.** <dd>
1f1d0 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62  The maximum numb
1f1e0 65 72 20 6f 66 20 76 61 72 69 61 62 6c 65 73 20  er of variables 
1f1f0 69 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d  in an SQL statem
1f200 65 6e 74 20 74 68 61 74 20 63 61 6e 0a 2a 2a 20  ent that can.** 
1f210 62 65 20 62 6f 75 6e 64 2e 3c 2f 64 64 3e 0a 2a  be bound.</dd>.*
1f220 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
1f230 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45  LIMIT_TRIGGER_DE
1f240 50 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  PTH</dt>.** <dd>
1f250 54 68 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 74  The maximum dept
1f260 68 20 6f 66 20 72 65 63 75 72 73 69 6f 6e 20 66  h of recursion f
1f270 6f 72 20 74 72 69 67 67 65 72 73 2e 3c 2f 64 64  or triggers.</dd
1f280 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64  >.** </dl>.*/.#d
1f290 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d  efine SQLITE_LIM
1f2a0 49 54 5f 4c 45 4e 47 54 48 20 20 20 20 20 20 20  IT_LENGTH       
1f2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23               0.#
1f2c0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49  define SQLITE_LI
1f2d0 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 20  MIT_SQL_LENGTH  
1f2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a                1.
1f2f0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
1f300 49 4d 49 54 5f 43 4f 4c 55 4d 4e 20 20 20 20 20  IMIT_COLUMN     
1f310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
1f320 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1f330 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45 50 54 48  LIMIT_EXPR_DEPTH
1f340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f350 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  3.#define SQLITE
1f360 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f  _LIMIT_COMPOUND_
1f370 53 45 4c 45 43 54 20 20 20 20 20 20 20 20 20 20  SELECT          
1f380 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   4.#define SQLIT
1f390 45 5f 4c 49 4d 49 54 5f 56 44 42 45 5f 4f 50 20  E_LIMIT_VDBE_OP 
1f3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f3b0 20 20 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    5.#define SQLI
1f3c0 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f  TE_LIMIT_FUNCTIO
1f3d0 4e 5f 41 52 47 20 20 20 20 20 20 20 20 20 20 20  N_ARG           
1f3e0 20 20 20 36 0a 23 64 65 66 69 6e 65 20 53 51 4c     6.#define SQL
1f3f0 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48  ITE_LIMIT_ATTACH
1f400 45 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ED              
1f410 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51      7.#define SQ
1f420 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f  LITE_LIMIT_LIKE_
1f430 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20 20  PATTERN_LENGTH  
1f440 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20 53       8.#define S
1f450 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49  QLITE_LIMIT_VARI
1f460 41 42 4c 45 5f 4e 55 4d 42 45 52 20 20 20 20 20  ABLE_NUMBER     
1f470 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20        9.#define 
1f480 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49  SQLITE_LIMIT_TRI
1f490 47 47 45 52 5f 44 45 50 54 48 20 20 20 20 20 20  GGER_DEPTH      
1f4a0 20 20 20 20 20 20 31 30 0a 0a 2f 2a 0a 2a 2a 20        10../*.** 
1f4b0 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 69 6c  CAPI3REF: Compil
1f4c0 69 6e 67 20 41 6e 20 53 51 4c 20 53 74 61 74 65  ing An SQL State
1f4d0 6d 65 6e 74 20 7b 48 31 33 30 31 30 7d 20 3c 53  ment {H13010} <S
1f4e0 31 30 30 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  10000>.** KEYWOR
1f4f0 44 53 3a 20 7b 53 51 4c 20 73 74 61 74 65 6d 65  DS: {SQL stateme
1f500 6e 74 20 63 6f 6d 70 69 6c 65 72 7d 0a 2a 2a 0a  nt compiler}.**.
1f510 2a 2a 20 54 6f 20 65 78 65 63 75 74 65 20 61 6e  ** To execute an
1f520 20 53 51 4c 20 71 75 65 72 79 2c 20 69 74 20 6d   SQL query, it m
1f530 75 73 74 20 66 69 72 73 74 20 62 65 20 63 6f 6d  ust first be com
1f540 70 69 6c 65 64 20 69 6e 74 6f 20 61 20 62 79 74  piled into a byt
1f550 65 2d 63 6f 64 65 0a 2a 2a 20 70 72 6f 67 72 61  e-code.** progra
1f560 6d 20 75 73 69 6e 67 20 6f 6e 65 20 6f 66 20 74  m using one of t
1f570 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a  hese routines..*
1f580 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  *.** The first a
1f590 72 67 75 6d 65 6e 74 2c 20 22 64 62 22 2c 20 69  rgument, "db", i
1f5a0 73 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f  s a [database co
1f5b0 6e 6e 65 63 74 69 6f 6e 5d 20 6f 62 74 61 69 6e  nnection] obtain
1f5c0 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 70 72 69  ed from a.** pri
1f5d0 6f 72 20 73 75 63 63 65 73 73 66 75 6c 20 63 61  or successful ca
1f5e0 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f  ll to [sqlite3_o
1f5f0 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  pen()], [sqlite3
1f600 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 6f 72 0a 2a  _open_v2()] or.*
1f610 2a 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  * [sqlite3_open1
1f620 36 28 29 5d 2e 20 20 54 68 65 20 64 61 74 61 62  6()].  The datab
1f630 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d  ase connection m
1f640 75 73 74 20 6e 6f 74 20 68 61 76 65 20 62 65 65  ust not have bee
1f650 6e 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20  n closed..**.** 
1f660 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
1f670 65 6e 74 2c 20 22 7a 53 71 6c 22 2c 20 69 73 20  ent, "zSql", is 
1f680 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  the statement to
1f690 20 62 65 20 63 6f 6d 70 69 6c 65 64 2c 20 65 6e   be compiled, en
1f6a0 63 6f 64 65 64 0a 2a 2a 20 61 73 20 65 69 74 68  coded.** as eith
1f6b0 65 72 20 55 54 46 2d 38 20 6f 72 20 55 54 46 2d  er UTF-8 or UTF-
1f6c0 31 36 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  16.  The sqlite3
1f6d0 5f 70 72 65 70 61 72 65 28 29 20 61 6e 64 20 73  _prepare() and s
1f6e0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
1f6f0 32 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  2().** interface
1f700 73 20 75 73 65 20 55 54 46 2d 38 2c 20 61 6e 64  s use UTF-8, and
1f710 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
1f720 31 36 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  16() and sqlite3
1f730 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 0a  _prepare16_v2().
1f740 2a 2a 20 75 73 65 20 55 54 46 2d 31 36 2e 0a 2a  ** use UTF-16..*
1f750 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 42 79 74  *.** If the nByt
1f760 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6c 65  e argument is le
1f770 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68  ss than zero, th
1f780 65 6e 20 7a 53 71 6c 20 69 73 20 72 65 61 64 20  en zSql is read 
1f790 75 70 20 74 6f 20 74 68 65 0a 2a 2a 20 66 69 72  up to the.** fir
1f7a0 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74  st zero terminat
1f7b0 6f 72 2e 20 49 66 20 6e 42 79 74 65 20 69 73 20  or. If nByte is 
1f7c0 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68  non-negative, th
1f7d0 65 6e 20 69 74 20 69 73 20 74 68 65 20 6d 61 78  en it is the max
1f7e0 69 6d 75 6d 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f  imum.** number o
1f7f0 66 20 20 62 79 74 65 73 20 72 65 61 64 20 66 72  f  bytes read fr
1f800 6f 6d 20 7a 53 71 6c 2e 20 20 57 68 65 6e 20 6e  om zSql.  When n
1f810 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e 65 67 61  Byte is non-nega
1f820 74 69 76 65 2c 20 74 68 65 0a 2a 2a 20 7a 53 71  tive, the.** zSq
1f830 6c 20 73 74 72 69 6e 67 20 65 6e 64 73 20 61 74  l string ends at
1f840 20 65 69 74 68 65 72 20 74 68 65 20 66 69 72 73   either the firs
1f850 74 20 27 5c 30 30 30 27 20 6f 72 20 27 5c 75 30  t '\000' or '\u0
1f860 30 30 30 27 20 63 68 61 72 61 63 74 65 72 20 6f  000' character o
1f870 72 0a 2a 2a 20 74 68 65 20 6e 42 79 74 65 2d 74  r.** the nByte-t
1f880 68 20 62 79 74 65 2c 20 77 68 69 63 68 65 76 65  h byte, whicheve
1f890 72 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 20 49  r comes first. I
1f8a0 66 20 74 68 65 20 63 61 6c 6c 65 72 20 6b 6e 6f  f the caller kno
1f8b0 77 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 73  ws.** that the s
1f8c0 75 70 70 6c 69 65 64 20 73 74 72 69 6e 67 20 69  upplied string i
1f8d0 73 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64  s nul-terminated
1f8e0 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20  , then there is 
1f8f0 61 20 73 6d 61 6c 6c 0a 2a 2a 20 70 65 72 66 6f  a small.** perfo
1f900 72 6d 61 6e 63 65 20 61 64 76 61 6e 74 61 67 65  rmance advantage
1f910 20 74 6f 20 62 65 20 67 61 69 6e 65 64 20 62 79   to be gained by
1f920 20 70 61 73 73 69 6e 67 20 61 6e 20 6e 42 79 74   passing an nByt
1f930 65 20 70 61 72 61 6d 65 74 65 72 20 74 68 61 74  e parameter that
1f940 0a 2a 2a 20 69 73 20 65 71 75 61 6c 20 74 6f 20  .** is equal to 
1f950 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
1f960 74 65 73 20 69 6e 20 74 68 65 20 69 6e 70 75 74  tes in the input
1f970 20 73 74 72 69 6e 67 20 3c 69 3e 69 6e 63 6c 75   string <i>inclu
1f980 64 69 6e 67 3c 2f 69 3e 0a 2a 2a 20 74 68 65 20  ding</i>.** the 
1f990 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 20 62  nul-terminator b
1f9a0 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70  ytes..**.** If p
1f9b0 7a 54 61 69 6c 20 69 73 20 6e 6f 74 20 4e 55 4c  zTail is not NUL
1f9c0 4c 20 74 68 65 6e 20 2a 70 7a 54 61 69 6c 20 69  L then *pzTail i
1f9d0 73 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20  s made to point 
1f9e0 74 6f 20 74 68 65 20 66 69 72 73 74 20 62 79 74  to the first byt
1f9f0 65 0a 2a 2a 20 70 61 73 74 20 74 68 65 20 65 6e  e.** past the en
1fa00 64 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 53  d of the first S
1fa10 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20  QL statement in 
1fa20 7a 53 71 6c 2e 20 20 54 68 65 73 65 20 72 6f 75  zSql.  These rou
1fa30 74 69 6e 65 73 20 6f 6e 6c 79 0a 2a 2a 20 63 6f  tines only.** co
1fa40 6d 70 69 6c 65 20 74 68 65 20 66 69 72 73 74 20  mpile the first 
1fa50 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 7a 53 71  statement in zSq
1fa60 6c 2c 20 73 6f 20 2a 70 7a 54 61 69 6c 20 69 73  l, so *pzTail is
1fa70 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 74   left pointing t
1fa80 6f 0a 2a 2a 20 77 68 61 74 20 72 65 6d 61 69 6e  o.** what remain
1fa90 73 20 75 6e 63 6f 6d 70 69 6c 65 64 2e 0a 2a 2a  s uncompiled..**
1faa0 0a 2a 2a 20 2a 70 70 53 74 6d 74 20 69 73 20 6c  .** *ppStmt is l
1fab0 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  eft pointing to 
1fac0 61 20 63 6f 6d 70 69 6c 65 64 20 5b 70 72 65 70  a compiled [prep
1fad0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
1fae0 74 68 61 74 20 63 61 6e 20 62 65 0a 2a 2a 20 65  that can be.** e
1faf0 78 65 63 75 74 65 64 20 75 73 69 6e 67 20 5b 73  xecuted using [s
1fb00 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 20  qlite3_step()]. 
1fb10 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 20   If there is an 
1fb20 65 72 72 6f 72 2c 20 2a 70 70 53 74 6d 74 20 69  error, *ppStmt i
1fb30 73 20 73 65 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c  s set.** to NULL
1fb40 2e 20 20 49 66 20 74 68 65 20 69 6e 70 75 74 20  .  If the input 
1fb50 74 65 78 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f  text contains no
1fb60 20 53 51 4c 20 28 69 66 20 74 68 65 20 69 6e 70   SQL (if the inp
1fb70 75 74 20 69 73 20 61 6e 20 65 6d 70 74 79 0a 2a  ut is an empty.*
1fb80 2a 20 73 74 72 69 6e 67 20 6f 72 20 61 20 63 6f  * string or a co
1fb90 6d 6d 65 6e 74 29 20 74 68 65 6e 20 2a 70 70 53  mment) then *ppS
1fba0 74 6d 74 20 69 73 20 73 65 74 20 74 6f 20 4e 55  tmt is set to NU
1fbb0 4c 4c 2e 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69  LL..** The calli
1fbc0 6e 67 20 70 72 6f 63 65 64 75 72 65 20 69 73 20  ng procedure is 
1fbd0 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
1fbe0 64 65 6c 65 74 69 6e 67 20 74 68 65 20 63 6f 6d  deleting the com
1fbf0 70 69 6c 65 64 0a 2a 2a 20 53 51 4c 20 73 74 61  piled.** SQL sta
1fc00 74 65 6d 65 6e 74 20 75 73 69 6e 67 20 5b 73 71  tement using [sq
1fc10 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
1fc20 5d 20 61 66 74 65 72 20 69 74 20 68 61 73 20 66  ] after it has f
1fc30 69 6e 69 73 68 65 64 20 77 69 74 68 20 69 74 2e  inished with it.
1fc40 0a 2a 2a 20 70 70 53 74 6d 74 20 6d 61 79 20 6e  .** ppStmt may n
1fc50 6f 74 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  ot be NULL..**.*
1fc60 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 5b 53  * On success, [S
1fc70 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74  QLITE_OK] is ret
1fc80 75 72 6e 65 64 2c 20 6f 74 68 65 72 77 69 73 65  urned, otherwise
1fc90 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d   an [error code]
1fca0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
1fcb0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
1fcc0 70 72 65 70 61 72 65 5f 76 32 28 29 20 61 6e 64  prepare_v2() and
1fcd0 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
1fce0 31 36 5f 76 32 28 29 20 69 6e 74 65 72 66 61 63  16_v2() interfac
1fcf0 65 73 20 61 72 65 0a 2a 2a 20 72 65 63 6f 6d 6d  es are.** recomm
1fd00 65 6e 64 65 64 20 66 6f 72 20 61 6c 6c 20 6e 65  ended for all ne
1fd10 77 20 70 72 6f 67 72 61 6d 73 2e 20 54 68 65 20  w programs. The 
1fd20 74 77 6f 20 6f 6c 64 65 72 20 69 6e 74 65 72 66  two older interf
1fd30 61 63 65 73 20 61 72 65 20 72 65 74 61 69 6e 65  aces are retaine
1fd40 64 0a 2a 2a 20 66 6f 72 20 62 61 63 6b 77 61 72  d.** for backwar
1fd50 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  ds compatibility
1fd60 2c 20 62 75 74 20 74 68 65 69 72 20 75 73 65 20  , but their use 
1fd70 69 73 20 64 69 73 63 6f 75 72 61 67 65 64 2e 0a  is discouraged..
1fd80 2a 2a 20 49 6e 20 74 68 65 20 22 76 32 22 20 69  ** In the "v2" i
1fd90 6e 74 65 72 66 61 63 65 73 2c 20 74 68 65 20 70  nterfaces, the p
1fda0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
1fdb0 74 0a 2a 2a 20 74 68 61 74 20 69 73 20 72 65 74  t.** that is ret
1fdc0 75 72 6e 65 64 20 28 74 68 65 20 5b 73 71 6c 69  urned (the [sqli
1fdd0 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74  te3_stmt] object
1fde0 29 20 63 6f 6e 74 61 69 6e 73 20 61 20 63 6f 70  ) contains a cop
1fdf0 79 20 6f 66 20 74 68 65 0a 2a 2a 20 6f 72 69 67  y of the.** orig
1fe00 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 2e 20 54  inal SQL text. T
1fe10 68 69 73 20 63 61 75 73 65 73 20 74 68 65 20 5b  his causes the [
1fe20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
1fe30 69 6e 74 65 72 66 61 63 65 20 74 6f 0a 2a 2a 20  interface to.** 
1fe40 62 65 68 61 76 65 20 61 20 64 69 66 66 65 72 65  behave a differe
1fe50 6e 74 6c 79 20 69 6e 20 74 77 6f 20 77 61 79 73  ntly in two ways
1fe60 3a 0a 2a 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20  :.**.** <ol>.** 
1fe70 3c 6c 69 3e 0a 2a 2a 20 49 66 20 74 68 65 20 64  <li>.** If the d
1fe80 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 63  atabase schema c
1fe90 68 61 6e 67 65 73 2c 20 69 6e 73 74 65 61 64 20  hanges, instead 
1fea0 6f 66 20 72 65 74 75 72 6e 69 6e 67 20 5b 53 51  of returning [SQ
1feb0 4c 49 54 45 5f 53 43 48 45 4d 41 5d 20 61 73 20  LITE_SCHEMA] as 
1fec0 69 74 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73 65  it.** always use
1fed0 64 20 74 6f 20 64 6f 2c 20 5b 73 71 6c 69 74 65  d to do, [sqlite
1fee0 33 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 61  3_step()] will a
1fef0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 65 63  utomatically rec
1ff00 6f 6d 70 69 6c 65 20 74 68 65 20 53 51 4c 0a 2a  ompile the SQL.*
1ff10 2a 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20  * statement and 
1ff20 74 72 79 20 74 6f 20 72 75 6e 20 69 74 20 61 67  try to run it ag
1ff30 61 69 6e 2e 20 20 49 66 20 74 68 65 20 73 63 68  ain.  If the sch
1ff40 65 6d 61 20 68 61 73 20 63 68 61 6e 67 65 64 20  ema has changed 
1ff50 69 6e 0a 2a 2a 20 61 20 77 61 79 20 74 68 61 74  in.** a way that
1ff60 20 6d 61 6b 65 73 20 74 68 65 20 73 74 61 74 65   makes the state
1ff70 6d 65 6e 74 20 6e 6f 20 6c 6f 6e 67 65 72 20 76  ment no longer v
1ff80 61 6c 69 64 2c 20 5b 73 71 6c 69 74 65 33 5f 73  alid, [sqlite3_s
1ff90 74 65 70 28 29 5d 20 77 69 6c 6c 20 73 74 69 6c  tep()] will stil
1ffa0 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 5b 53 51 4c  l.** return [SQL
1ffb0 49 54 45 5f 53 43 48 45 4d 41 5d 2e 20 20 42 75  ITE_SCHEMA].  Bu
1ffc0 74 20 75 6e 6c 69 6b 65 20 74 68 65 20 6c 65 67  t unlike the leg
1ffd0 61 63 79 20 62 65 68 61 76 69 6f 72 2c 20 5b 53  acy behavior, [S
1ffe0 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 20 69 73  QLITE_SCHEMA] is
1fff0 0a 2a 2a 20 6e 6f 77 20 61 20 66 61 74 61 6c 20  .** now a fatal 
20000 65 72 72 6f 72 2e 20 20 43 61 6c 6c 69 6e 67 20  error.  Calling 
20010 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
20020 5f 76 32 28 29 5d 20 61 67 61 69 6e 20 77 69 6c  _v2()] again wil
20030 6c 20 6e 6f 74 20 6d 61 6b 65 20 74 68 65 0a 2a  l not make the.*
20040 2a 20 65 72 72 6f 72 20 67 6f 20 61 77 61 79 2e  * error go away.
20050 20 20 4e 6f 74 65 3a 20 75 73 65 20 5b 73 71 6c    Note: use [sql
20060 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 20 74  ite3_errmsg()] t
20070 6f 20 66 69 6e 64 20 74 68 65 20 74 65 78 74 0a  o find the text.
20080 2a 2a 20 6f 66 20 74 68 65 20 70 61 72 73 69 6e  ** of the parsin
20090 67 20 65 72 72 6f 72 20 74 68 61 74 20 72 65 73  g error that res
200a0 75 6c 74 73 20 69 6e 20 61 6e 20 5b 53 51 4c 49  ults in an [SQLI
200b0 54 45 5f 53 43 48 45 4d 41 5d 20 72 65 74 75 72  TE_SCHEMA] retur
200c0 6e 2e 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a 0a 2a  n..** </li>.**.*
200d0 2a 20 3c 6c 69 3e 0a 2a 2a 20 57 68 65 6e 20 61  * <li>.** When a
200e0 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
200f0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
20100 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 6f 6e 65   will return one
20110 20 6f 66 20 74 68 65 20 64 65 74 61 69 6c 65 64   of the detailed
20120 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 73  .** [error codes
20130 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 64 20 65  ] or [extended e
20140 72 72 6f 72 20 63 6f 64 65 73 5d 2e 20 20 54 68  rror codes].  Th
20150 65 20 6c 65 67 61 63 79 20 62 65 68 61 76 69 6f  e legacy behavio
20160 72 20 77 61 73 20 74 68 61 74 0a 2a 2a 20 5b 73  r was that.** [s
20170 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77  qlite3_step()] w
20180 6f 75 6c 64 20 6f 6e 6c 79 20 72 65 74 75 72 6e  ould only return
20190 20 61 20 67 65 6e 65 72 69 63 20 5b 53 51 4c 49   a generic [SQLI
201a0 54 45 5f 45 52 52 4f 52 5d 20 72 65 73 75 6c 74  TE_ERROR] result
201b0 20 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 79 6f 75   code.** and you
201c0 20 77 6f 75 6c 64 20 68 61 76 65 20 74 6f 20 6d   would have to m
201d0 61 6b 65 20 61 20 73 65 63 6f 6e 64 20 63 61 6c  ake a second cal
201e0 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65  l to [sqlite3_re
201f0 73 65 74 28 29 5d 20 69 6e 20 6f 72 64 65 72 0a  set()] in order.
20200 2a 2a 20 74 6f 20 66 69 6e 64 20 74 68 65 20 75  ** to find the u
20210 6e 64 65 72 6c 79 69 6e 67 20 63 61 75 73 65 20  nderlying cause 
20220 6f 66 20 74 68 65 20 70 72 6f 62 6c 65 6d 2e 20  of the problem. 
20230 57 69 74 68 20 74 68 65 20 22 76 32 22 20 70 72  With the "v2" pr
20240 65 70 61 72 65 0a 2a 2a 20 69 6e 74 65 72 66 61  epare.** interfa
20250 63 65 73 2c 20 74 68 65 20 75 6e 64 65 72 6c 79  ces, the underly
20260 69 6e 67 20 72 65 61 73 6f 6e 20 66 6f 72 20 74  ing reason for t
20270 68 65 20 65 72 72 6f 72 20 69 73 20 72 65 74 75  he error is retu
20280 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79  rned immediately
20290 2e 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f  ..** </li>.** </
202a0 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ol>.**.** Requir
202b0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 30  ements:.** [H130
202c0 31 31 5d 20 5b 48 31 33 30 31 32 5d 20 5b 48 31  11] [H13012] [H1
202d0 33 30 31 33 5d 20 5b 48 31 33 30 31 34 5d 20 5b  3013] [H13014] [
202e0 48 31 33 30 31 35 5d 20 5b 48 31 33 30 31 36 5d  H13015] [H13016]
202f0 20 5b 48 31 33 30 31 39 5d 20 5b 48 31 33 30 32   [H13019] [H1302
20300 31 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f  1].**.*/.SQLITE_
20310 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
20320 70 72 65 70 61 72 65 28 0a 20 20 73 71 6c 69 74  prepare(.  sqlit
20330 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
20340 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68     /* Database h
20350 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
20360 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20   char *zSql,    
20370 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d     /* SQL statem
20380 65 6e 74 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64  ent, UTF-8 encod
20390 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  ed */.  int nByt
203a0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
203b0 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  /* Maximum lengt
203c0 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74  h of zSql in byt
203d0 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  es. */.  sqlite3
203e0 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20  _stmt **ppStmt, 
203f0 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65   /* OUT: Stateme
20400 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63  nt handle */.  c
20410 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61  onst char **pzTa
20420 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50  il     /* OUT: P
20430 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64  ointer to unused
20440 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c   portion of zSql
20450 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50   */.);.SQLITE_AP
20460 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72  I int sqlite3_pr
20470 65 70 61 72 65 5f 76 32 28 0a 20 20 73 71 6c 69  epare_v2(.  sqli
20480 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
20490 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
204a0 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  handle */.  cons
204b0 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20  t char *zSql,   
204c0 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65      /* SQL state
204d0 6d 65 6e 74 2c 20 55 54 46 2d 38 20 65 6e 63 6f  ment, UTF-8 enco
204e0 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  ded */.  int nBy
204f0 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  te,             
20500 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67   /* Maximum leng
20510 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79  th of zSql in by
20520 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  tes. */.  sqlite
20530 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c  3_stmt **ppStmt,
20540 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d    /* OUT: Statem
20550 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ent handle */.  
20560 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54  const char **pzT
20570 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20  ail     /* OUT: 
20580 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65  Pointer to unuse
20590 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71  d portion of zSq
205a0 6c 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41  l */.);.SQLITE_A
205b0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70  PI int sqlite3_p
205c0 72 65 70 61 72 65 31 36 28 0a 20 20 73 71 6c 69  repare16(.  sqli
205d0 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
205e0 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
205f0 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  handle */.  cons
20600 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20  t void *zSql,   
20610 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65      /* SQL state
20620 6d 65 6e 74 2c 20 55 54 46 2d 31 36 20 65 6e 63  ment, UTF-16 enc
20630 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  oded */.  int nB
20640 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  yte,            
20650 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e    /* Maximum len
20660 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62  gth of zSql in b
20670 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  ytes. */.  sqlit
20680 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74  e3_stmt **ppStmt
20690 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65  ,  /* OUT: State
206a0 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  ment handle */. 
206b0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a   const void **pz
206c0 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a  Tail     /* OUT:
206d0 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73   Pointer to unus
206e0 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53  ed portion of zS
206f0 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f  ql */.);.SQLITE_
20700 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
20710 70 72 65 70 61 72 65 31 36 5f 76 32 28 0a 20 20  prepare16_v2(.  
20720 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
20730 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
20740 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ase handle */.  
20750 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c  const void *zSql
20760 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73  ,       /* SQL s
20770 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d 31 36  tatement, UTF-16
20780 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e   encoded */.  in
20790 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20  t nByte,        
207a0 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d        /* Maximum
207b0 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20   length of zSql 
207c0 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73  in bytes. */.  s
207d0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
207e0 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53  Stmt,  /* OUT: S
207f0 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20  tatement handle 
20800 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
20810 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20  **pzTail     /* 
20820 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20  OUT: Pointer to 
20830 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f  unused portion o
20840 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 0a 2f 2a  f zSql */.);../*
20850 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65  .** CAPI3REF: Re
20860 74 72 69 65 76 69 6e 67 20 53 74 61 74 65 6d 65  trieving Stateme
20870 6e 74 20 53 51 4c 20 7b 48 31 33 31 30 30 7d 20  nt SQL {H13100} 
20880 3c 48 31 33 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <H13000>.**.** T
20890 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 63 61  his interface ca
208a0 6e 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 74  n be used to ret
208b0 72 69 65 76 65 20 61 20 73 61 76 65 64 20 63 6f  rieve a saved co
208c0 70 79 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e  py of the origin
208d0 61 6c 0a 2a 2a 20 53 51 4c 20 74 65 78 74 20 75  al.** SQL text u
208e0 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20  sed to create a 
208f0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
20900 65 6e 74 5d 20 69 66 20 74 68 61 74 20 73 74 61  ent] if that sta
20910 74 65 6d 65 6e 74 20 77 61 73 0a 2a 2a 20 63 6f  tement was.** co
20920 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 65 69 74  mpiled using eit
20930 68 65 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  her [sqlite3_pre
20940 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73  pare_v2()] or [s
20950 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
20960 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65  _v2()]..**.** Re
20970 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
20980 48 31 33 31 30 31 5d 20 5b 48 31 33 31 30 32 5d  H13101] [H13102]
20990 20 5b 48 31 33 31 30 33 5d 0a 2a 2f 0a 53 51 4c   [H13103].*/.SQL
209a0 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
209b0 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28  ar *sqlite3_sql(
209c0 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
209d0 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  tmt);../*.** CAP
209e0 49 33 52 45 46 3a 20 44 79 6e 61 6d 69 63 61 6c  I3REF: Dynamical
209f0 6c 79 20 54 79 70 65 64 20 56 61 6c 75 65 20 4f  ly Typed Value O
20a00 62 6a 65 63 74 20 7b 48 31 35 30 30 30 7d 20 3c  bject {H15000} <
20a10 53 32 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f  S20200>.** KEYWO
20a20 52 44 53 3a 20 7b 70 72 6f 74 65 63 74 65 64 20  RDS: {protected 
20a30 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 7d 20 7b  sqlite3_value} {
20a40 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69  unprotected sqli
20a50 74 65 33 5f 76 61 6c 75 65 7d 0a 2a 2a 0a 2a 2a  te3_value}.**.**
20a60 20 53 51 4c 69 74 65 20 75 73 65 73 20 74 68 65   SQLite uses the
20a70 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
20a80 62 6a 65 63 74 20 74 6f 20 72 65 70 72 65 73 65  bject to represe
20a90 6e 74 20 61 6c 6c 20 76 61 6c 75 65 73 0a 2a 2a  nt all values.**
20aa0 20 74 68 61 74 20 63 61 6e 20 62 65 20 73 74 6f   that can be sto
20ab0 72 65 64 20 69 6e 20 61 20 64 61 74 61 62 61 73  red in a databas
20ac0 65 20 74 61 62 6c 65 2e 20 53 51 4c 69 74 65 20  e table. SQLite 
20ad0 75 73 65 73 20 64 79 6e 61 6d 69 63 20 74 79 70  uses dynamic typ
20ae0 69 6e 67 0a 2a 2a 20 66 6f 72 20 74 68 65 20 76  ing.** for the v
20af0 61 6c 75 65 73 20 69 74 20 73 74 6f 72 65 73 2e  alues it stores.
20b00 20 56 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69   Values stored i
20b10 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  n sqlite3_value 
20b20 6f 62 6a 65 63 74 73 0a 2a 2a 20 63 61 6e 20 62  objects.** can b
20b30 65 20 69 6e 74 65 67 65 72 73 2c 20 66 6c 6f 61  e integers, floa
20b40 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65  ting point value
20b50 73 2c 20 73 74 72 69 6e 67 73 2c 20 42 4c 4f 42  s, strings, BLOB
20b60 73 2c 20 6f 72 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  s, or NULL..**.*
20b70 2a 20 41 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c  * An sqlite3_val
20b80 75 65 20 6f 62 6a 65 63 74 20 6d 61 79 20 62 65  ue object may be
20b90 20 65 69 74 68 65 72 20 22 70 72 6f 74 65 63 74   either "protect
20ba0 65 64 22 20 6f 72 20 22 75 6e 70 72 6f 74 65 63  ed" or "unprotec
20bb0 74 65 64 22 2e 0a 2a 2a 20 53 6f 6d 65 20 69 6e  ted"..** Some in
20bc0 74 65 72 66 61 63 65 73 20 72 65 71 75 69 72 65  terfaces require
20bd0 20 61 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c   a protected sql
20be0 69 74 65 33 5f 76 61 6c 75 65 2e 20 20 4f 74 68  ite3_value.  Oth
20bf0 65 72 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a  er interfaces.**
20c00 20 77 69 6c 6c 20 61 63 63 65 70 74 20 65 69 74   will accept eit
20c10 68 65 72 20 61 20 70 72 6f 74 65 63 74 65 64 20  her a protected 
20c20 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 63 74 65  or an unprotecte
20c30 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e  d sqlite3_value.
20c40 0a 2a 2a 20 45 76 65 72 79 20 69 6e 74 65 72 66  .** Every interf
20c50 61 63 65 20 74 68 61 74 20 61 63 63 65 70 74 73  ace that accepts
20c60 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 61   sqlite3_value a
20c70 72 67 75 6d 65 6e 74 73 20 73 70 65 63 69 66 69  rguments specifi
20c80 65 73 0a 2a 2a 20 77 68 65 74 68 65 72 20 6f 72  es.** whether or
20c90 20 6e 6f 74 20 69 74 20 72 65 71 75 69 72 65 73   not it requires
20ca0 20 61 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c   a protected sql
20cb0 69 74 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a 0a 2a  ite3_value..**.*
20cc0 2a 20 54 68 65 20 74 65 72 6d 73 20 22 70 72 6f  * The terms "pro
20cd0 74 65 63 74 65 64 22 20 61 6e 64 20 22 75 6e 70  tected" and "unp
20ce0 72 6f 74 65 63 74 65 64 22 20 72 65 66 65 72 20  rotected" refer 
20cf0 74 6f 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  to whether or no
20d00 74 0a 2a 2a 20 61 20 6d 75 74 65 78 20 69 73 20  t.** a mutex is 
20d10 68 65 6c 64 2e 20 20 41 20 69 6e 74 65 72 6e 61  held.  A interna
20d20 6c 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20  l mutex is held 
20d30 66 6f 72 20 61 20 70 72 6f 74 65 63 74 65 64 0a  for a protected.
20d40 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ** sqlite3_value
20d50 20 6f 62 6a 65 63 74 20 62 75 74 20 6e 6f 20 6d   object but no m
20d60 75 74 65 78 20 69 73 20 68 65 6c 64 20 66 6f 72  utex is held for
20d70 20 61 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 0a   an unprotected.
20d80 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ** sqlite3_value
20d90 20 6f 62 6a 65 63 74 2e 20 20 49 66 20 53 51 4c   object.  If SQL
20da0 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20  ite is compiled 
20db0 74 6f 20 62 65 20 73 69 6e 67 6c 65 2d 74 68 72  to be single-thr
20dc0 65 61 64 65 64 0a 2a 2a 20 28 77 69 74 68 20 5b  eaded.** (with [
20dd0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
20de0 45 3d 30 5d 20 61 6e 64 20 77 69 74 68 20 5b 73  E=0] and with [s
20df0 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61 66  qlite3_threadsaf
20e00 65 28 29 5d 20 72 65 74 75 72 6e 69 6e 67 20 30  e()] returning 0
20e10 29 0a 2a 2a 20 6f 72 20 69 66 20 53 51 4c 69 74  ).** or if SQLit
20e20 65 20 69 73 20 72 75 6e 20 69 6e 20 6f 6e 65 20  e is run in one 
20e30 6f 66 20 72 65 64 75 63 65 64 20 6d 75 74 65 78  of reduced mutex
20e40 20 6d 6f 64 65 73 20 0a 2a 2a 20 5b 53 51 4c 49   modes .** [SQLI
20e50 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45  TE_CONFIG_SINGLE
20e60 54 48 52 45 41 44 5d 20 6f 72 20 5b 53 51 4c 49  THREAD] or [SQLI
20e70 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54  TE_CONFIG_MULTIT
20e80 48 52 45 41 44 5d 0a 2a 2a 20 74 68 65 6e 20 74  HREAD].** then t
20e90 68 65 72 65 20 69 73 20 6e 6f 20 64 69 73 74 69  here is no disti
20ea0 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 70  nction between p
20eb0 72 6f 74 65 63 74 65 64 20 61 6e 64 20 75 6e 70  rotected and unp
20ec0 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69  rotected.** sqli
20ed0 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74  te3_value object
20ee0 73 20 61 6e 64 20 74 68 65 79 20 63 61 6e 20 62  s and they can b
20ef0 65 20 75 73 65 64 20 69 6e 74 65 72 63 68 61 6e  e used interchan
20f00 67 65 61 62 6c 79 2e 20 20 48 6f 77 65 76 65 72  geably.  However
20f10 2c 0a 2a 2a 20 66 6f 72 20 6d 61 78 69 6d 75 6d  ,.** for maximum
20f20 20 63 6f 64 65 20 70 6f 72 74 61 62 69 6c 69 74   code portabilit
20f30 79 20 69 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e  y it is recommen
20f40 64 65 64 20 74 68 61 74 20 61 70 70 6c 69 63 61  ded that applica
20f50 74 69 6f 6e 73 0a 2a 2a 20 73 74 69 6c 6c 20 6d  tions.** still m
20f60 61 6b 65 20 74 68 65 20 64 69 73 74 69 6e 63 74  ake the distinct
20f70 69 6f 6e 20 62 65 74 77 65 65 6e 20 62 65 74 77  ion between betw
20f80 65 65 6e 20 70 72 6f 74 65 63 74 65 64 20 61 6e  een protected an
20f90 64 20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a  d unprotected.**
20fa0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
20fb0 62 6a 65 63 74 73 20 65 76 65 6e 20 77 68 65 6e  bjects even when
20fc0 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20 72 65   not strictly re
20fd0 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  quired..**.** Th
20fe0 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  e sqlite3_value 
20ff0 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61 72 65  objects that are
21000 20 70 61 73 73 65 64 20 61 73 20 70 61 72 61 6d   passed as param
21010 65 74 65 72 73 20 69 6e 74 6f 20 74 68 65 0a 2a  eters into the.*
21020 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
21030 20 6f 66 20 5b 61 70 70 6c 69 63 61 74 69 6f 6e   of [application
21040 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e  -defined SQL fun
21050 63 74 69 6f 6e 73 5d 20 61 72 65 20 70 72 6f 74  ctions] are prot
21060 65 63 74 65 64 2e 0a 2a 2a 20 54 68 65 20 73 71  ected..** The sq
21070 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
21080 63 74 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a  ct returned by.*
21090 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  * [sqlite3_colum
210a0 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 20 75 6e  n_value()] is un
210b0 70 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 55 6e  protected..** Un
210c0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
210d0 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20  3_value objects 
210e0 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64  may only be used
210f0 20 77 69 74 68 0a 2a 2a 20 5b 73 71 6c 69 74 65   with.** [sqlite
21100 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29  3_result_value()
21110 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62  ] and [sqlite3_b
21120 69 6e 64 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a  ind_value()]..**
21130 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61   The [sqlite3_va
21140 6c 75 65 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74  lue_blob | sqlit
21150 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 29 5d  e3_value_type()]
21160 20 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20 69 6e   family of.** in
21170 74 65 72 66 61 63 65 73 20 72 65 71 75 69 72 65  terfaces require
21180 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74   protected sqlit
21190 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73  e3_value objects
211a0 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
211b0 75 63 74 20 4d 65 6d 20 73 71 6c 69 74 65 33 5f  uct Mem sqlite3_
211c0 76 61 6c 75 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  value;../*.** CA
211d0 50 49 33 52 45 46 3a 20 53 51 4c 20 46 75 6e 63  PI3REF: SQL Func
211e0 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 4f 62 6a  tion Context Obj
211f0 65 63 74 20 7b 48 31 36 30 30 31 7d 20 3c 53 32  ect {H16001} <S2
21200 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0200>.**.** The 
21210 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 63 68  context in which
21220 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e   an SQL function
21230 20 65 78 65 63 75 74 65 73 20 69 73 20 73 74 6f   executes is sto
21240 72 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 73 71 6c  red in an.** sql
21250 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f 62 6a  ite3_context obj
21260 65 63 74 2e 20 20 41 20 70 6f 69 6e 74 65 72 20  ect.  A pointer 
21270 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 63 6f  to an sqlite3_co
21280 6e 74 65 78 74 20 6f 62 6a 65 63 74 0a 2a 2a 20  ntext object.** 
21290 69 73 20 61 6c 77 61 79 73 20 66 69 72 73 74 20  is always first 
212a0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 61 70  parameter to [ap
212b0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
212c0 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d  d SQL functions]
212d0 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61  ..** The applica
212e0 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c  tion-defined SQL
212f0 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d   function implem
21300 65 6e 74 61 74 69 6f 6e 20 77 69 6c 6c 20 70 61  entation will pa
21310 73 73 20 74 68 69 73 0a 2a 2a 20 70 6f 69 6e 74  ss this.** point
21320 65 72 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20  er through into 
21330 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65  calls to [sqlite
21340 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 7c 20 73  3_result_int | s
21350 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 28 29 5d  qlite3_result()]
21360 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 61 67  ,.** [sqlite3_ag
21370 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28  gregate_context(
21380 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 75 73 65  )], [sqlite3_use
21390 72 5f 64 61 74 61 28 29 5d 2c 0a 2a 2a 20 5b 73  r_data()],.** [s
213a0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
213b0 62 5f 68 61 6e 64 6c 65 28 29 5d 2c 20 5b 73 71  b_handle()], [sq
213c0 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74  lite3_get_auxdat
213d0 61 28 29 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f 72 20  a()],.** and/or 
213e0 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78  [sqlite3_set_aux
213f0 64 61 74 61 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65  data()]..*/.type
21400 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
21410 65 33 5f 63 6f 6e 74 65 78 74 20 73 71 6c 69 74  e3_context sqlit
21420 65 33 5f 63 6f 6e 74 65 78 74 3b 0a 0a 2f 2a 0a  e3_context;../*.
21430 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 42 69 6e  ** CAPI3REF: Bin
21440 64 69 6e 67 20 56 61 6c 75 65 73 20 54 6f 20 50  ding Values To P
21450 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e  repared Statemen
21460 74 73 20 7b 48 31 33 35 30 30 7d 20 3c 53 37 30  ts {H13500} <S70
21470 33 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  300>.** KEYWORDS
21480 3a 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65  : {host paramete
21490 72 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 74  r} {host paramet
214a0 65 72 73 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d  ers} {host param
214b0 65 74 65 72 20 6e 61 6d 65 7d 0a 2a 2a 20 4b 45  eter name}.** KE
214c0 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 70 61 72  YWORDS: {SQL par
214d0 61 6d 65 74 65 72 7d 20 7b 53 51 4c 20 70 61 72  ameter} {SQL par
214e0 61 6d 65 74 65 72 73 7d 20 7b 70 61 72 61 6d 65  ameters} {parame
214f0 74 65 72 20 62 69 6e 64 69 6e 67 7d 0a 2a 2a 0a  ter binding}.**.
21500 2a 2a 20 49 6e 20 74 68 65 20 53 51 4c 20 73 74  ** In the SQL st
21510 72 69 6e 67 73 20 69 6e 70 75 74 20 74 6f 20 5b  rings input to [
21520 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
21530 76 32 28 29 5d 20 61 6e 64 20 69 74 73 20 76 61  v2()] and its va
21540 72 69 61 6e 74 73 2c 0a 2a 2a 20 6c 69 74 65 72  riants,.** liter
21550 61 6c 73 20 6d 61 79 20 62 65 20 72 65 70 6c 61  als may be repla
21560 63 65 64 20 62 79 20 61 20 5b 70 61 72 61 6d 65  ced by a [parame
21570 74 65 72 5d 20 74 68 61 74 20 6d 61 74 63 68 65  ter] that matche
21580 73 20 6f 6e 65 20 6f 66 20 66 6f 6c 6c 6f 77 69  s one of followi
21590 6e 67 0a 2a 2a 20 74 65 6d 70 6c 61 74 65 73 3a  ng.** templates:
215a0 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c  .**.** <ul>.** <
215b0 6c 69 3e 20 20 3f 0a 2a 2a 20 3c 6c 69 3e 20 20  li>  ?.** <li>  
215c0 3f 4e 4e 4e 0a 2a 2a 20 3c 6c 69 3e 20 20 3a 56  ?NNN.** <li>  :V
215d0 56 56 0a 2a 2a 20 3c 6c 69 3e 20 20 40 56 56 56  VV.** <li>  @VVV
215e0 0a 2a 2a 20 3c 6c 69 3e 20 20 24 56 56 56 0a 2a  .** <li>  $VVV.*
215f0 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e  * </ul>.**.** In
21600 20 74 68 65 20 74 65 6d 70 6c 61 74 65 73 20 61   the templates a
21610 62 6f 76 65 2c 20 4e 4e 4e 20 72 65 70 72 65 73  bove, NNN repres
21620 65 6e 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20  ents an integer 
21630 6c 69 74 65 72 61 6c 2c 0a 2a 2a 20 61 6e 64 20  literal,.** and 
21640 56 56 56 20 72 65 70 72 65 73 65 6e 74 73 20 61  VVV represents a
21650 6e 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 20 69  n alphanumeric i
21660 64 65 6e 74 69 66 65 72 2e 20 20 54 68 65 20 76  dentifer.  The v
21670 61 6c 75 65 73 20 6f 66 20 74 68 65 73 65 0a 2a  alues of these.*
21680 2a 20 70 61 72 61 6d 65 74 65 72 73 20 28 61 6c  * parameters (al
21690 73 6f 20 63 61 6c 6c 65 64 20 22 68 6f 73 74 20  so called "host 
216a0 70 61 72 61 6d 65 74 65 72 20 6e 61 6d 65 73 22  parameter names"
216b0 20 6f 72 20 22 53 51 4c 20 70 61 72 61 6d 65 74   or "SQL paramet
216c0 65 72 73 22 29 0a 2a 2a 20 63 61 6e 20 62 65 20  ers").** can be 
216d0 73 65 74 20 75 73 69 6e 67 20 74 68 65 20 73 71  set using the sq
216e0 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72  lite3_bind_*() r
216f0 6f 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64 20  outines defined 
21700 68 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  here..**.** The 
21710 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
21720 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69  o the sqlite3_bi
21730 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20  nd_*() routines 
21740 69 73 20 61 6c 77 61 79 73 0a 2a 2a 20 61 20 70  is always.** a p
21750 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 73  ointer to the [s
21760 71 6c 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a  qlite3_stmt] obj
21770 65 63 74 20 72 65 74 75 72 6e 65 64 20 66 72 6f  ect returned fro
21780 6d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72  m.** [sqlite3_pr
21790 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 69  epare_v2()] or i
217a0 74 73 20 76 61 72 69 61 6e 74 73 2e 0a 2a 2a 0a  ts variants..**.
217b0 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  ** The second ar
217c0 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 69 6e  gument is the in
217d0 64 65 78 20 6f 66 20 74 68 65 20 53 51 4c 20 70  dex of the SQL p
217e0 61 72 61 6d 65 74 65 72 20 74 6f 20 62 65 20 73  arameter to be s
217f0 65 74 2e 0a 2a 2a 20 54 68 65 20 6c 65 66 74 6d  et..** The leftm
21800 6f 73 74 20 53 51 4c 20 70 61 72 61 6d 65 74 65  ost SQL paramete
21810 72 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f  r has an index o
21820 66 20 31 2e 20 20 57 68 65 6e 20 74 68 65 20 73  f 1.  When the s
21830 61 6d 65 20 6e 61 6d 65 64 0a 2a 2a 20 53 51 4c  ame named.** SQL
21840 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 73   parameter is us
21850 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63  ed more than onc
21860 65 2c 20 73 65 63 6f 6e 64 20 61 6e 64 20 73 75  e, second and su
21870 62 73 65 71 75 65 6e 74 0a 2a 2a 20 6f 63 63 75  bsequent.** occu
21880 72 72 65 6e 63 65 73 20 68 61 76 65 20 74 68 65  rrences have the
21890 20 73 61 6d 65 20 69 6e 64 65 78 20 61 73 20 74   same index as t
218a0 68 65 20 66 69 72 73 74 20 6f 63 63 75 72 72 65  he first occurre
218b0 6e 63 65 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 65  nce..** The inde
218c0 78 20 66 6f 72 20 6e 61 6d 65 64 20 70 61 72 61  x for named para
218d0 6d 65 74 65 72 73 20 63 61 6e 20 62 65 20 6c 6f  meters can be lo
218e0 6f 6b 65 64 20 75 70 20 75 73 69 6e 67 20 74 68  oked up using th
218f0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69  e.** [sqlite3_bi
21900 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64  nd_parameter_ind
21910 65 78 28 29 5d 20 41 50 49 20 69 66 20 64 65 73  ex()] API if des
21920 69 72 65 64 2e 20 20 54 68 65 20 69 6e 64 65 78  ired.  The index
21930 0a 2a 2a 20 66 6f 72 20 22 3f 4e 4e 4e 22 20 70  .** for "?NNN" p
21940 61 72 61 6d 65 74 65 72 73 20 69 73 20 74 68 65  arameters is the
21950 20 76 61 6c 75 65 20 6f 66 20 4e 4e 4e 2e 0a 2a   value of NNN..*
21960 2a 20 54 68 65 20 4e 4e 4e 20 76 61 6c 75 65 20  * The NNN value 
21970 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e 20  must be between 
21980 31 20 61 6e 64 20 74 68 65 20 5b 73 71 6c 69 74  1 and the [sqlit
21990 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 70  e3_limit()].** p
219a0 61 72 61 6d 65 74 65 72 20 5b 53 51 4c 49 54 45  arameter [SQLITE
219b0 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f  _LIMIT_VARIABLE_
219c0 4e 55 4d 42 45 52 5d 20 28 64 65 66 61 75 6c 74  NUMBER] (default
219d0 20 76 61 6c 75 65 3a 20 39 39 39 29 2e 0a 2a 2a   value: 999)..**
219e0 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 61 72  .** The third ar
219f0 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 76 61  gument is the va
21a00 6c 75 65 20 74 6f 20 62 69 6e 64 20 74 6f 20 74  lue to bind to t
21a10 68 65 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a  he parameter..**
21a20 0a 2a 2a 20 49 6e 20 74 68 6f 73 65 20 72 6f 75  .** In those rou
21a30 74 69 6e 65 73 20 74 68 61 74 20 68 61 76 65 20  tines that have 
21a40 61 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e  a fourth argumen
21a50 74 2c 20 69 74 73 20 76 61 6c 75 65 20 69 73 20  t, its value is 
21a60 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66  the.** number of
21a70 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 70 61   bytes in the pa
21a80 72 61 6d 65 74 65 72 2e 20 20 54 6f 20 62 65 20  rameter.  To be 
21a90 63 6c 65 61 72 3a 20 74 68 65 20 76 61 6c 75 65  clear: the value
21aa0 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65   is the.** numbe
21ab0 72 20 6f 66 20 3c 75 3e 62 79 74 65 73 3c 2f 75  r of <u>bytes</u
21ac0 3e 20 69 6e 20 74 68 65 20 76 61 6c 75 65 2c 20  > in the value, 
21ad0 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  not the number o
21ae0 66 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a  f characters..**
21af0 20 49 66 20 74 68 65 20 66 6f 75 72 74 68 20 70   If the fourth p
21b00 61 72 61 6d 65 74 65 72 20 69 73 20 6e 65 67 61  arameter is nega
21b10 74 69 76 65 2c 20 74 68 65 20 6c 65 6e 67 74 68  tive, the length
21b20 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 69   of the string i
21b30 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20  s.** the number 
21b40 6f 66 20 62 79 74 65 73 20 75 70 20 74 6f 20 74  of bytes up to t
21b50 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20 74 65  he first zero te
21b60 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20  rminator..**.** 
21b70 54 68 65 20 66 69 66 74 68 20 61 72 67 75 6d 65  The fifth argume
21b80 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69  nt to sqlite3_bi
21b90 6e 64 5f 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74  nd_blob(), sqlit
21ba0 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 29 2c 20  e3_bind_text(), 
21bb0 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62  and.** sqlite3_b
21bc0 69 6e 64 5f 74 65 78 74 31 36 28 29 20 69 73 20  ind_text16() is 
21bd0 61 20 64 65 73 74 72 75 63 74 6f 72 20 75 73 65  a destructor use
21be0 64 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20  d to dispose of 
21bf0 74 68 65 20 42 4c 4f 42 20 6f 72 0a 2a 2a 20 73  the BLOB or.** s
21c00 74 72 69 6e 67 20 61 66 74 65 72 20 53 51 4c 69  tring after SQLi
21c10 74 65 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  te has finished 
21c20 77 69 74 68 20 69 74 2e 20 49 66 20 74 68 65 20  with it. If the 
21c30 66 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20 69  fifth argument i
21c40 73 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 61 6c  s.** the special
21c50 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 53   value [SQLITE_S
21c60 54 41 54 49 43 5d 2c 20 74 68 65 6e 20 53 51 4c  TATIC], then SQL
21c70 69 74 65 20 61 73 73 75 6d 65 73 20 74 68 61 74  ite assumes that
21c80 20 74 68 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74   the.** informat
21c90 69 6f 6e 20 69 73 20 69 6e 20 73 74 61 74 69 63  ion is in static
21ca0 2c 20 75 6e 6d 61 6e 61 67 65 64 20 73 70 61 63  , unmanaged spac
21cb0 65 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e  e and does not n
21cc0 65 65 64 20 74 6f 20 62 65 20 66 72 65 65 64 2e  eed to be freed.
21cd0 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 66 74 68  .** If the fifth
21ce0 20 61 72 67 75 6d 65 6e 74 20 68 61 73 20 74 68   argument has th
21cf0 65 20 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f  e value [SQLITE_
21d00 54 52 41 4e 53 49 45 4e 54 5d 2c 20 74 68 65 6e  TRANSIENT], then
21d10 0a 2a 2a 20 53 51 4c 69 74 65 20 6d 61 6b 65 73  .** SQLite makes
21d20 20 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65   its own private
21d30 20 63 6f 70 79 20 6f 66 20 74 68 65 20 64 61 74   copy of the dat
21d40 61 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c 20 62  a immediately, b
21d50 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 73 71 6c  efore.** the sql
21d60 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f  ite3_bind_*() ro
21d70 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a  utine returns..*
21d80 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
21d90 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29  _bind_zeroblob()
21da0 20 72 6f 75 74 69 6e 65 20 62 69 6e 64 73 20 61   routine binds a
21db0 20 42 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 20   BLOB of length 
21dc0 4e 20 74 68 61 74 0a 2a 2a 20 69 73 20 66 69 6c  N that.** is fil
21dd0 6c 65 64 20 77 69 74 68 20 7a 65 72 6f 65 73 2e  led with zeroes.
21de0 20 20 41 20 7a 65 72 6f 62 6c 6f 62 20 75 73 65    A zeroblob use
21df0 73 20 61 20 66 69 78 65 64 20 61 6d 6f 75 6e 74  s a fixed amount
21e00 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 28 6a   of memory.** (j
21e10 75 73 74 20 61 6e 20 69 6e 74 65 67 65 72 20 74  ust an integer t
21e20 6f 20 68 6f 6c 64 20 69 74 73 20 73 69 7a 65 29  o hold its size)
21e30 20 77 68 69 6c 65 20 69 74 20 69 73 20 62 65 69   while it is bei
21e40 6e 67 20 70 72 6f 63 65 73 73 65 64 2e 0a 2a 2a  ng processed..**
21e50 20 5a 65 72 6f 62 6c 6f 62 73 20 61 72 65 20 69   Zeroblobs are i
21e60 6e 74 65 6e 64 65 64 20 74 6f 20 73 65 72 76 65  ntended to serve
21e70 20 61 73 20 70 6c 61 63 65 68 6f 6c 64 65 72 73   as placeholders
21e80 20 66 6f 72 20 42 4c 4f 42 73 20 77 68 6f 73 65   for BLOBs whose
21e90 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 69 73 20 6c  .** content is l
21ea0 61 74 65 72 20 77 72 69 74 74 65 6e 20 75 73 69  ater written usi
21eb0 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62  ng.** [sqlite3_b
21ec0 6c 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65  lob_open | incre
21ed0 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d  mental BLOB I/O]
21ee0 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 41 20   routines..** A 
21ef0 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20 66  negative value f
21f00 6f 72 20 74 68 65 20 7a 65 72 6f 62 6c 6f 62 20  or the zeroblob 
21f10 72 65 73 75 6c 74 73 20 69 6e 20 61 20 7a 65 72  results in a zer
21f20 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 2e 0a 2a  o-length BLOB..*
21f30 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
21f40 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e  _bind_*() routin
21f50 65 73 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65  es must be calle
21f60 64 20 61 66 74 65 72 0a 2a 2a 20 5b 73 71 6c 69  d after.** [sqli
21f70 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
21f80 5d 20 28 61 6e 64 20 69 74 73 20 76 61 72 69 61  ] (and its varia
21f90 6e 74 73 29 20 6f 72 20 5b 73 71 6c 69 74 65 33  nts) or [sqlite3
21fa0 5f 72 65 73 65 74 28 29 5d 20 61 6e 64 0a 2a 2a  _reset()] and.**
21fb0 20 62 65 66 6f 72 65 20 5b 73 71 6c 69 74 65 33   before [sqlite3
21fc0 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 42 69 6e  _step()]..** Bin
21fd0 64 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 63 6c  dings are not cl
21fe0 65 61 72 65 64 20 62 79 20 74 68 65 20 5b 73 71  eared by the [sq
21ff0 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 72  lite3_reset()] r
22000 6f 75 74 69 6e 65 2e 0a 2a 2a 20 55 6e 62 6f 75  outine..** Unbou
22010 6e 64 20 70 61 72 61 6d 65 74 65 72 73 20 61 72  nd parameters ar
22020 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73  e interpreted as
22030 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   NULL..**.** The
22040 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75  se routines retu
22050 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f  rn [SQLITE_OK] o
22060 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20  n success or an 
22070 65 72 72 6f 72 20 63 6f 64 65 20 69 66 0a 2a 2a  error code if.**
22080 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77   anything goes w
22090 72 6f 6e 67 2e 20 20 5b 53 51 4c 49 54 45 5f 52  rong.  [SQLITE_R
220a0 41 4e 47 45 5d 20 69 73 20 72 65 74 75 72 6e 65  ANGE] is returne
220b0 64 20 69 66 20 74 68 65 20 70 61 72 61 6d 65 74  d if the paramet
220c0 65 72 0a 2a 2a 20 69 6e 64 65 78 20 69 73 20 6f  er.** index is o
220d0 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 20 5b 53  ut of range.  [S
220e0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 20 69 73 20  QLITE_NOMEM] is 
220f0 72 65 74 75 72 6e 65 64 20 69 66 20 6d 61 6c 6c  returned if mall
22100 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2a 20 5b  oc() fails..** [
22110 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20 6d  SQLITE_MISUSE] m
22120 69 67 68 74 20 62 65 20 72 65 74 75 72 6e 65 64  ight be returned
22130 20 69 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e   if these routin
22140 65 73 20 61 72 65 20 63 61 6c 6c 65 64 20 6f 6e  es are called on
22150 20 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61   a.** virtual ma
22160 63 68 69 6e 65 20 74 68 61 74 20 69 73 20 74 68  chine that is th
22170 65 20 77 72 6f 6e 67 20 73 74 61 74 65 20 6f 72  e wrong state or
22180 20 77 68 69 63 68 20 68 61 73 20 61 6c 72 65 61   which has alrea
22190 64 79 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a 65  dy been finalize
221a0 64 2e 0a 2a 2a 20 44 65 74 65 63 74 69 6f 6e 20  d..** Detection 
221b0 6f 66 20 6d 69 73 75 73 65 20 69 73 20 75 6e 72  of misuse is unr
221c0 65 6c 69 61 62 6c 65 2e 20 20 41 70 70 6c 69 63  eliable.  Applic
221d0 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f  ations should no
221e0 74 20 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 53  t depend.** on S
221f0 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 72 65 74  QLITE_MISUSE ret
22200 75 72 6e 73 2e 20 20 53 51 4c 49 54 45 5f 4d 49  urns.  SQLITE_MI
22210 53 55 53 45 20 69 73 20 69 6e 74 65 6e 64 65 64  SUSE is intended
22220 20 74 6f 20 69 6e 64 69 63 61 74 65 20 61 0a 2a   to indicate a.*
22230 2a 20 61 20 6c 6f 67 69 63 20 65 72 72 6f 72 20  * a logic error 
22240 69 6e 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  in the applicati
22250 6f 6e 2e 20 20 46 75 74 75 72 65 20 76 65 72 73  on.  Future vers
22260 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d  ions of SQLite m
22270 69 67 68 74 0a 2a 2a 20 70 61 6e 69 63 20 72 61  ight.** panic ra
22280 74 68 65 72 20 74 68 61 6e 20 72 65 74 75 72 6e  ther than return
22290 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2e 0a   SQLITE_MISUSE..
222a0 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
222b0 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61  [sqlite3_bind_pa
222c0 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d  rameter_count()]
222d0 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69  ,.** [sqlite3_bi
222e0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
222f0 65 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74  e()], and [sqlit
22300 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
22310 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a  r_index()]..**.*
22320 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
22330 2a 2a 20 5b 48 31 33 35 30 36 5d 20 5b 48 31 33  ** [H13506] [H13
22340 35 30 39 5d 20 5b 48 31 33 35 31 32 5d 20 5b 48  509] [H13512] [H
22350 31 33 35 31 35 5d 20 5b 48 31 33 35 31 38 5d 20  13515] [H13518] 
22360 5b 48 31 33 35 32 31 5d 20 5b 48 31 33 35 32 34  [H13521] [H13524
22370 5d 20 5b 48 31 33 35 32 37 5d 0a 2a 2a 20 5b 48  ] [H13527].** [H
22380 31 33 35 33 30 5d 20 5b 48 31 33 35 33 33 5d 20  13530] [H13533] 
22390 5b 48 31 33 35 33 36 5d 20 5b 48 31 33 35 33 39  [H13536] [H13539
223a0 5d 20 5b 48 31 33 35 34 32 5d 20 5b 48 31 33 35  ] [H13542] [H135
223b0 34 35 5d 20 5b 48 31 33 35 34 38 5d 20 5b 48 31  45] [H13548] [H1
223c0 33 35 35 31 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49  3551].**.*/.SQLI
223d0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
223e0 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 73 71 6c  e3_bind_blob(sql
223f0 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c  ite3_stmt*, int,
22400 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e   const void*, in
22410 74 20 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69  t n, void(*)(voi
22420 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  d*));.SQLITE_API
22430 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
22440 64 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33  d_double(sqlite3
22450 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f 75  _stmt*, int, dou
22460 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ble);.SQLITE_API
22470 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
22480 64 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74  d_int(sqlite3_st
22490 6d 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  mt*, int, int);.
224a0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
224b0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
224c0 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  4(sqlite3_stmt*,
224d0 20 69 6e 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e   int, sqlite3_in
224e0 74 36 34 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  t64);.SQLITE_API
224f0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
22500 64 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73  d_null(sqlite3_s
22510 74 6d 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  tmt*, int);.SQLI
22520 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
22530 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 73 71 6c  e3_bind_text(sql
22540 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c  ite3_stmt*, int,
22550 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e   const char*, in
22560 74 20 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69  t n, void(*)(voi
22570 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  d*));.SQLITE_API
22580 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
22590 64 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33  d_text16(sqlite3
225a0 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e  _stmt*, int, con
225b0 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76  st void*, int, v
225c0 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
225d0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
225e0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75  qlite3_bind_valu
225f0 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  e(sqlite3_stmt*,
22600 20 69 6e 74 2c 20 63 6f 6e 73 74 20 73 71 6c 69   int, const sqli
22610 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c  te3_value*);.SQL
22620 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
22630 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f  te3_bind_zeroblo
22640 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  b(sqlite3_stmt*,
22650 20 69 6e 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f   int, int n);../
22660 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e  *.** CAPI3REF: N
22670 75 6d 62 65 72 20 4f 66 20 53 51 4c 20 50 61 72  umber Of SQL Par
22680 61 6d 65 74 65 72 73 20 7b 48 31 33 36 30 30 7d  ameters {H13600}
22690 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S70300>.**.** 
226a0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e  This routine can
226b0 20 62 65 20 75 73 65 64 20 74 6f 20 66 69 6e 64   be used to find
226c0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 5b   the number of [
226d0 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 5d 0a  SQL parameters].
226e0 2a 2a 20 69 6e 20 61 20 5b 70 72 65 70 61 72 65  ** in a [prepare
226f0 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 53  d statement].  S
22700 51 4c 20 70 61 72 61 6d 65 74 65 72 73 20 61 72  QL parameters ar
22710 65 20 74 6f 6b 65 6e 73 20 6f 66 20 74 68 65 0a  e tokens of the.
22720 2a 2a 20 66 6f 72 6d 20 22 3f 22 2c 20 22 3f 4e  ** form "?", "?N
22730 4e 4e 22 2c 20 22 3a 41 41 41 22 2c 20 22 24 41  NN", ":AAA", "$A
22740 41 41 22 2c 20 6f 72 20 22 40 41 41 41 22 20 74  AA", or "@AAA" t
22750 68 61 74 20 73 65 72 76 65 20 61 73 0a 2a 2a 20  hat serve as.** 
22760 70 6c 61 63 65 68 6f 6c 64 65 72 73 20 66 6f 72  placeholders for
22770 20 76 61 6c 75 65 73 20 74 68 61 74 20 61 72 65   values that are
22780 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62   [sqlite3_bind_b
22790 6c 6f 62 20 7c 20 62 6f 75 6e 64 5d 0a 2a 2a 20  lob | bound].** 
227a0 74 6f 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  to the parameter
227b0 73 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d  s at a later tim
227c0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
227d0 75 74 69 6e 65 20 61 63 74 75 61 6c 6c 79 20 72  utine actually r
227e0 65 74 75 72 6e 73 20 74 68 65 20 69 6e 64 65 78  eturns the index
227f0 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74 20   of the largest 
22800 28 72 69 67 68 74 6d 6f 73 74 29 0a 2a 2a 20 70  (rightmost).** p
22810 61 72 61 6d 65 74 65 72 2e 20 46 6f 72 20 61 6c  arameter. For al
22820 6c 20 66 6f 72 6d 73 20 65 78 63 65 70 74 20 3f  l forms except ?
22830 4e 4e 4e 2c 20 74 68 69 73 20 77 69 6c 6c 20 63  NNN, this will c
22840 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65  orrespond to the
22850 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e  .** number of un
22860 69 71 75 65 20 70 61 72 61 6d 65 74 65 72 73 2e  ique parameters.
22870 20 20 49 66 20 70 61 72 61 6d 65 74 65 72 73 20    If parameters 
22880 6f 66 20 74 68 65 20 3f 4e 4e 4e 20 61 72 65 20  of the ?NNN are 
22890 75 73 65 64 2c 0a 2a 2a 20 74 68 65 72 65 20 6d  used,.** there m
228a0 61 79 20 62 65 20 67 61 70 73 20 69 6e 20 74 68  ay be gaps in th
228b0 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 65  e list..**.** Se
228c0 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33  e also: [sqlite3
228d0 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74  _bind_blob|sqlit
228e0 65 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b  e3_bind()],.** [
228f0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
22900 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20  ameter_name()], 
22910 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  and.** [sqlite3_
22920 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69  bind_parameter_i
22930 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52  ndex()]..**.** R
22940 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
22950 5b 48 31 33 36 30 31 5d 0a 2a 2f 0a 53 51 4c 49  [H13601].*/.SQLI
22960 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
22970 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
22980 72 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f  r_count(sqlite3_
22990 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  stmt*);../*.** C
229a0 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66  API3REF: Name Of
229b0 20 41 20 48 6f 73 74 20 50 61 72 61 6d 65 74 65   A Host Paramete
229c0 72 20 7b 48 31 33 36 32 30 7d 20 3c 53 37 30 33  r {H13620} <S703
229d0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  00>.**.** This r
229e0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61  outine returns a
229f0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
22a00 6e 61 6d 65 20 6f 66 20 74 68 65 20 6e 2d 74 68  name of the n-th
22a10 0a 2a 2a 20 5b 53 51 4c 20 70 61 72 61 6d 65 74  .** [SQL paramet
22a20 65 72 5d 20 69 6e 20 61 20 5b 70 72 65 70 61 72  er] in a [prepar
22a30 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a  ed statement]..*
22a40 2a 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73  * SQL parameters
22a50 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 4e   of the form "?N
22a60 4e 4e 22 20 6f 72 20 22 3a 41 41 41 22 20 6f 72  NN" or ":AAA" or
22a70 20 22 40 41 41 41 22 20 6f 72 20 22 24 41 41 41   "@AAA" or "$AAA
22a80 22 0a 2a 2a 20 68 61 76 65 20 61 20 6e 61 6d 65  ".** have a name
22a90 20 77 68 69 63 68 20 69 73 20 74 68 65 20 73 74   which is the st
22aa0 72 69 6e 67 20 22 3f 4e 4e 4e 22 20 6f 72 20 22  ring "?NNN" or "
22ab0 3a 41 41 41 22 20 6f 72 20 22 40 41 41 41 22 20  :AAA" or "@AAA" 
22ac0 6f 72 20 22 24 41 41 41 22 0a 2a 2a 20 72 65 73  or "$AAA".** res
22ad0 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 49 6e  pectively..** In
22ae0 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68   other words, th
22af0 65 20 69 6e 69 74 69 61 6c 20 22 3a 22 20 6f 72  e initial ":" or
22b00 20 22 24 22 20 6f 72 20 22 40 22 20 6f 72 20 22   "$" or "@" or "
22b10 3f 22 0a 2a 2a 20 69 73 20 69 6e 63 6c 75 64 65  ?".** is include
22b20 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65  d as part of the
22b30 20 6e 61 6d 65 2e 0a 2a 2a 20 50 61 72 61 6d 65   name..** Parame
22b40 74 65 72 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  ters of the form
22b50 20 22 3f 22 20 77 69 74 68 6f 75 74 20 61 20 66   "?" without a f
22b60 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 67 65 72  ollowing integer
22b70 20 68 61 76 65 20 6e 6f 20 6e 61 6d 65 0a 2a 2a   have no name.**
22b80 20 61 6e 64 20 61 72 65 20 61 6c 73 6f 20 72 65   and are also re
22b90 66 65 72 72 65 64 20 74 6f 20 61 73 20 22 61 6e  ferred to as "an
22ba0 6f 6e 79 6d 6f 75 73 20 70 61 72 61 6d 65 74 65  onymous paramete
22bb0 72 73 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  rs"..**.** The f
22bc0 69 72 73 74 20 68 6f 73 74 20 70 61 72 61 6d 65  irst host parame
22bd0 74 65 72 20 68 61 73 20 61 6e 20 69 6e 64 65 78  ter has an index
22be0 20 6f 66 20 31 2c 20 6e 6f 74 20 30 2e 0a 2a 2a   of 1, not 0..**
22bf0 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
22c00 20 6e 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e   n is out of ran
22c10 67 65 20 6f 72 20 69 66 20 74 68 65 20 6e 2d 74  ge or if the n-t
22c20 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a  h parameter is.*
22c30 2a 20 6e 61 6d 65 6c 65 73 73 2c 20 74 68 65 6e  * nameless, then
22c40 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
22c50 64 2e 20 20 54 68 65 20 72 65 74 75 72 6e 65 64  d.  The returned
22c60 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 61 6c   string is.** al
22c70 77 61 79 73 20 69 6e 20 55 54 46 2d 38 20 65 6e  ways in UTF-8 en
22c80 63 6f 64 69 6e 67 20 65 76 65 6e 20 69 66 20 74  coding even if t
22c90 68 65 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 74  he named paramet
22ca0 65 72 20 77 61 73 0a 2a 2a 20 6f 72 69 67 69 6e  er was.** origin
22cb0 61 6c 6c 79 20 73 70 65 63 69 66 69 65 64 20 61  ally specified a
22cc0 73 20 55 54 46 2d 31 36 20 69 6e 20 5b 73 71 6c  s UTF-16 in [sql
22cd0 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29  ite3_prepare16()
22ce0 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ] or.** [sqlite3
22cf0 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d  _prepare16_v2()]
22d00 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
22d10 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  : [sqlite3_bind_
22d20 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e  blob|sqlite3_bin
22d30 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65  d()],.** [sqlite
22d40 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  3_bind_parameter
22d50 5f 63 6f 75 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a  _count()], and.*
22d60 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  * [sqlite3_bind_
22d70 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28  parameter_index(
22d80 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  )]..**.** Requir
22d90 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36  ements:.** [H136
22da0 32 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  21].*/.SQLITE_AP
22db0 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
22dc0 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
22dd0 65 74 65 72 5f 6e 61 6d 65 28 73 71 6c 69 74 65  eter_name(sqlite
22de0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 29 3b 0a 0a  3_stmt*, int);..
22df0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
22e00 49 6e 64 65 78 20 4f 66 20 41 20 50 61 72 61 6d  Index Of A Param
22e10 65 74 65 72 20 57 69 74 68 20 41 20 47 69 76 65  eter With A Give
22e20 6e 20 4e 61 6d 65 20 7b 48 31 33 36 34 30 7d 20  n Name {H13640} 
22e30 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 52  <S70300>.**.** R
22e40 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20  eturn the index 
22e50 6f 66 20 61 6e 20 53 51 4c 20 70 61 72 61 6d 65  of an SQL parame
22e60 74 65 72 20 67 69 76 65 6e 20 69 74 73 20 6e 61  ter given its na
22e70 6d 65 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 64 65  me.  The.** inde
22e80 78 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  x value returned
22e90 20 69 73 20 73 75 69 74 61 62 6c 65 20 66 6f 72   is suitable for
22ea0 20 75 73 65 20 61 73 20 74 68 65 20 73 65 63 6f   use as the seco
22eb0 6e 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  nd.** parameter 
22ec0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  to [sqlite3_bind
22ed0 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69  _blob|sqlite3_bi
22ee0 6e 64 28 29 5d 2e 20 20 41 20 7a 65 72 6f 0a 2a  nd()].  A zero.*
22ef0 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  * is returned if
22f00 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 70 61 72   no matching par
22f10 61 6d 65 74 65 72 20 69 73 20 66 6f 75 6e 64 2e  ameter is found.
22f20 20 20 54 68 65 20 70 61 72 61 6d 65 74 65 72 0a    The parameter.
22f30 2a 2a 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20  ** name must be 
22f40 67 69 76 65 6e 20 69 6e 20 55 54 46 2d 38 20 65  given in UTF-8 e
22f50 76 65 6e 20 69 66 20 74 68 65 20 6f 72 69 67 69  ven if the origi
22f60 6e 61 6c 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  nal statement.**
22f70 20 77 61 73 20 70 72 65 70 61 72 65 64 20 66 72   was prepared fr
22f80 6f 6d 20 55 54 46 2d 31 36 20 74 65 78 74 20 75  om UTF-16 text u
22f90 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72  sing [sqlite3_pr
22fa0 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a  epare16_v2()]..*
22fb0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b  *.** See also: [
22fc0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
22fd0 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29  b|sqlite3_bind()
22fe0 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62  ],.** [sqlite3_b
22ff0 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f  ind_parameter_co
23000 75 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b  unt()], and.** [
23010 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
23020 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e  ameter_index()].
23030 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
23040 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 34 31 5d  nts:.** [H13641]
23050 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
23060 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
23070 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28  parameter_index(
23080 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 63  sqlite3_stmt*, c
23090 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
230a0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
230b0 45 46 3a 20 52 65 73 65 74 20 41 6c 6c 20 42 69  EF: Reset All Bi
230c0 6e 64 69 6e 67 73 20 4f 6e 20 41 20 50 72 65 70  ndings On A Prep
230d0 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b  ared Statement {
230e0 48 31 33 36 36 30 7d 20 3c 53 37 30 33 30 30 3e  H13660} <S70300>
230f0 0a 2a 2a 0a 2a 2a 20 43 6f 6e 74 72 61 72 79 20  .**.** Contrary 
23100 74 6f 20 74 68 65 20 69 6e 74 75 69 74 69 6f 6e  to the intuition
23110 20 6f 66 20 6d 61 6e 79 2c 20 5b 73 71 6c 69 74   of many, [sqlit
23120 65 33 5f 72 65 73 65 74 28 29 5d 20 64 6f 65 73  e3_reset()] does
23130 20 6e 6f 74 20 72 65 73 65 74 0a 2a 2a 20 74 68   not reset.** th
23140 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  e [sqlite3_bind_
23150 62 6c 6f 62 20 7c 20 62 69 6e 64 69 6e 67 73 5d  blob | bindings]
23160 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20   on a [prepared 
23170 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 55  statement]..** U
23180 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  se this routine 
23190 74 6f 20 72 65 73 65 74 20 61 6c 6c 20 68 6f 73  to reset all hos
231a0 74 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20  t parameters to 
231b0 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  NULL..**.** Requ
231c0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
231d0 33 36 36 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  3661].*/.SQLITE_
231e0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
231f0 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 73  clear_bindings(s
23200 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a  qlite3_stmt*);..
23210 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
23220 4e 75 6d 62 65 72 20 4f 66 20 43 6f 6c 75 6d 6e  Number Of Column
23230 73 20 49 6e 20 41 20 52 65 73 75 6c 74 20 53 65  s In A Result Se
23240 74 20 7b 48 31 33 37 31 30 7d 20 3c 53 31 30 37  t {H13710} <S107
23250 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  00>.**.** Return
23260 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
23270 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65  olumns in the re
23280 73 75 6c 74 20 73 65 74 20 72 65 74 75 72 6e 65  sult set returne
23290 64 20 62 79 20 74 68 65 0a 2a 2a 20 5b 70 72 65  d by the.** [pre
232a0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
232b0 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  . This routine r
232c0 65 74 75 72 6e 73 20 30 20 69 66 20 70 53 74 6d  eturns 0 if pStm
232d0 74 20 69 73 20 61 6e 20 53 51 4c 0a 2a 2a 20 73  t is an SQL.** s
232e0 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 64 6f  tatement that do
232f0 65 73 20 6e 6f 74 20 72 65 74 75 72 6e 20 64 61  es not return da
23300 74 61 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20  ta (for example 
23310 61 6e 20 5b 55 50 44 41 54 45 5d 29 2e 0a 2a 2a  an [UPDATE])..**
23320 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
23330 3a 0a 2a 2a 20 5b 48 31 33 37 31 31 5d 0a 2a 2f  :.** [H13711].*/
23340 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
23350 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63  sqlite3_column_c
23360 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  ount(sqlite3_stm
23370 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a  t *pStmt);../*.*
23380 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6c 75  * CAPI3REF: Colu
23390 6d 6e 20 4e 61 6d 65 73 20 49 6e 20 41 20 52 65  mn Names In A Re
233a0 73 75 6c 74 20 53 65 74 20 7b 48 31 33 37 32 30  sult Set {H13720
233b0 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a  } <S10700>.**.**
233c0 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
233d0 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20  return the name 
233e0 61 73 73 69 67 6e 65 64 20 74 6f 20 61 20 70 61  assigned to a pa
233f0 72 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 0a  rticular column.
23400 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ** in the result
23410 20 73 65 74 20 6f 66 20 61 20 5b 53 45 4c 45 43   set of a [SELEC
23420 54 5d 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54  T] statement.  T
23430 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  he sqlite3_colum
23440 6e 5f 6e 61 6d 65 28 29 0a 2a 2a 20 69 6e 74 65  n_name().** inte
23450 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20  rface returns a 
23460 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 7a 65 72  pointer to a zer
23470 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46  o-terminated UTF
23480 2d 38 20 73 74 72 69 6e 67 0a 2a 2a 20 61 6e 64  -8 string.** and
23490 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
234a0 6e 61 6d 65 31 36 28 29 20 72 65 74 75 72 6e 73  name16() returns
234b0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
234c0 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a  zero-terminated.
234d0 2a 2a 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67  ** UTF-16 string
234e0 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61 72  .  The first par
234f0 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 70  ameter is the [p
23500 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
23510 74 5d 0a 2a 2a 20 74 68 61 74 20 69 6d 70 6c 65  t].** that imple
23520 6d 65 6e 74 73 20 74 68 65 20 5b 53 45 4c 45 43  ments the [SELEC
23530 54 5d 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68  T] statement. Th
23540 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
23550 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6c  er is the.** col
23560 75 6d 6e 20 6e 75 6d 62 65 72 2e 20 20 54 68 65  umn number.  The
23570 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e   leftmost column
23580 20 69 73 20 6e 75 6d 62 65 72 20 30 2e 0a 2a 2a   is number 0..**
23590 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64  .** The returned
235a0 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65 72 20   string pointer 
235b0 69 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 65  is valid until e
235c0 69 74 68 65 72 20 74 68 65 20 5b 70 72 65 70 61  ither the [prepa
235d0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a  red statement].*
235e0 2a 20 69 73 20 64 65 73 74 72 6f 79 65 64 20 62  * is destroyed b
235f0 79 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  y [sqlite3_final
23600 69 7a 65 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20  ize()] or until 
23610 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f  the next call to
23620 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  .** sqlite3_colu
23630 6d 6e 5f 6e 61 6d 65 28 29 20 6f 72 20 73 71 6c  mn_name() or sql
23640 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  ite3_column_name
23650 31 36 28 29 20 6f 6e 20 74 68 65 20 73 61 6d 65  16() on the same
23660 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49   column..**.** I
23670 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  f sqlite3_malloc
23680 28 29 20 66 61 69 6c 73 20 64 75 72 69 6e 67 20  () fails during 
23690 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6f  the processing o
236a0 66 20 65 69 74 68 65 72 20 72 6f 75 74 69 6e 65  f either routine
236b0 0a 2a 2a 20 28 66 6f 72 20 65 78 61 6d 70 6c 65  .** (for example
236c0 20 64 75 72 69 6e 67 20 61 20 63 6f 6e 76 65 72   during a conver
236d0 73 69 6f 6e 20 66 72 6f 6d 20 55 54 46 2d 38 20  sion from UTF-8 
236e0 74 6f 20 55 54 46 2d 31 36 29 20 74 68 65 6e 20  to UTF-16) then 
236f0 61 0a 2a 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  a.** NULL pointe
23700 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  r is returned..*
23710 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66  *.** The name of
23720 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e   a result column
23730 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66   is the value of
23740 20 74 68 65 20 22 41 53 22 20 63 6c 61 75 73 65   the "AS" clause
23750 20 66 6f 72 0a 2a 2a 20 74 68 61 74 20 63 6f 6c   for.** that col
23760 75 6d 6e 2c 20 69 66 20 74 68 65 72 65 20 69 73  umn, if there is
23770 20 61 6e 20 41 53 20 63 6c 61 75 73 65 2e 20 20   an AS clause.  
23780 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 41  If there is no A
23790 53 20 63 6c 61 75 73 65 0a 2a 2a 20 74 68 65 6e  S clause.** then
237a0 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
237b0 20 63 6f 6c 75 6d 6e 20 69 73 20 75 6e 73 70 65   column is unspe
237c0 63 69 66 69 65 64 20 61 6e 64 20 6d 61 79 20 63  cified and may c
237d0 68 61 6e 67 65 20 66 72 6f 6d 0a 2a 2a 20 6f 6e  hange from.** on
237e0 65 20 72 65 6c 65 61 73 65 20 6f 66 20 53 51 4c  e release of SQL
237f0 69 74 65 20 74 6f 20 74 68 65 20 6e 65 78 74 2e  ite to the next.
23800 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
23810 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 32 31 5d  nts:.** [H13721]
23820 20 5b 48 31 33 37 32 33 5d 20 5b 48 31 33 37 32   [H13723] [H1372
23830 34 5d 20 5b 48 31 33 37 32 35 5d 20 5b 48 31 33  4] [H13725] [H13
23840 37 32 36 5d 20 5b 48 31 33 37 32 37 5d 0a 2a 2f  726] [H13727].*/
23850 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
23860 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  t char *sqlite3_
23870 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69  column_name(sqli
23880 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e  te3_stmt*, int N
23890 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  );.SQLITE_API co
238a0 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
238b0 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28  3_column_name16(
238c0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
238d0 6e 74 20 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  nt N);../*.** CA
238e0 50 49 33 52 45 46 3a 20 53 6f 75 72 63 65 20 4f  PI3REF: Source O
238f0 66 20 44 61 74 61 20 49 6e 20 41 20 51 75 65 72  f Data In A Quer
23900 79 20 52 65 73 75 6c 74 20 7b 48 31 33 37 34 30  y Result {H13740
23910 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a  } <S10700>.**.**
23920 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
23930 70 72 6f 76 69 64 65 20 61 20 6d 65 61 6e 73 20  provide a means 
23940 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61  to determine wha
23950 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 77 68 61 74  t column of what
23960 0a 2a 2a 20 74 61 62 6c 65 20 69 6e 20 77 68 69  .** table in whi
23970 63 68 20 64 61 74 61 62 61 73 65 20 61 20 72 65  ch database a re
23980 73 75 6c 74 20 6f 66 20 61 20 5b 53 45 4c 45 43  sult of a [SELEC
23990 54 5d 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d  T] statement com
239a0 65 73 20 66 72 6f 6d 2e 0a 2a 2a 20 54 68 65 20  es from..** The 
239b0 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  name of the data
239c0 62 61 73 65 20 6f 72 20 74 61 62 6c 65 20 6f 72  base or table or
239d0 20 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 72   column can be r
239e0 65 74 75 72 6e 65 64 20 61 73 0a 2a 2a 20 65 69  eturned as.** ei
239f0 74 68 65 72 20 61 20 55 54 46 2d 38 20 6f 72 20  ther a UTF-8 or 
23a00 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20  UTF-16 string.  
23a10 54 68 65 20 5f 64 61 74 61 62 61 73 65 5f 20 72  The _database_ r
23a20 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 0a 2a  outines return.*
23a30 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  * the database n
23a40 61 6d 65 2c 20 74 68 65 20 5f 74 61 62 6c 65 5f  ame, the _table_
23a50 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e   routines return
23a60 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2c   the table name,
23a70 20 61 6e 64 0a 2a 2a 20 74 68 65 20 6f 72 69 67   and.** the orig
23a80 69 6e 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 74  in_ routines ret
23a90 75 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e  urn the column n
23aa0 61 6d 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75  ame..** The retu
23ab0 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 76  rned string is v
23ac0 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 5b  alid until the [
23ad0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
23ae0 6e 74 5d 20 69 73 20 64 65 73 74 72 6f 79 65 64  nt] is destroyed
23af0 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  .** using [sqlit
23b00 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f  e3_finalize()] o
23b10 72 20 75 6e 74 69 6c 20 74 68 65 20 73 61 6d 65  r until the same
23b20 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20   information is 
23b30 72 65 71 75 65 73 74 65 64 0a 2a 2a 20 61 67 61  requested.** aga
23b40 69 6e 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e  in in a differen
23b50 74 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a  t encoding..**.*
23b60 2a 20 54 68 65 20 6e 61 6d 65 73 20 72 65 74 75  * The names retu
23b70 72 6e 65 64 20 61 72 65 20 74 68 65 20 6f 72 69  rned are the ori
23b80 67 69 6e 61 6c 20 75 6e 2d 61 6c 69 61 73 65 64  ginal un-aliased
23b90 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a   names of the.**
23ba0 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65   database, table
23bb0 2c 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a  , and column..**
23bc0 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72  .** The first ar
23bd0 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 6f  gument to the fo
23be0 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 73 20 69 73  llowing calls is
23bf0 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61   a [prepared sta
23c00 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 54 68 65 73  tement]..** Thes
23c10 65 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75  e functions retu
23c20 72 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  rn information a
23c30 62 6f 75 74 20 74 68 65 20 4e 74 68 20 63 6f 6c  bout the Nth col
23c40 75 6d 6e 20 72 65 74 75 72 6e 65 64 20 62 79 0a  umn returned by.
23c50 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  ** the statement
23c60 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65  , where N is the
23c70 20 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e   second function
23c80 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
23c90 20 49 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75   If the Nth colu
23ca0 6d 6e 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  mn returned by t
23cb0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
23cc0 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72  an expression or
23cd0 0a 2a 2a 20 73 75 62 71 75 65 72 79 20 61 6e 64  .** subquery and
23ce0 20 69 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e   is not a column
23cf0 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c   value, then all
23d00 20 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69   of these functi
23d10 6f 6e 73 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55  ons return.** NU
23d20 4c 4c 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69  LL.  These routi
23d30 6e 65 20 6d 69 67 68 74 20 61 6c 73 6f 20 72 65  ne might also re
23d40 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 61 20 6d  turn NULL if a m
23d50 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
23d60 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73   error.** occurs
23d70 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68  .  Otherwise, th
23d80 65 79 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61  ey return the na
23d90 6d 65 20 6f 66 20 74 68 65 20 61 74 74 61 63 68  me of the attach
23da0 65 64 20 64 61 74 61 62 61 73 65 2c 20 74 61 62  ed database, tab
23db0 6c 65 0a 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e  le.** and column
23dc0 20 74 68 61 74 20 71 75 65 72 79 20 72 65 73 75   that query resu
23dd0 6c 74 20 63 6f 6c 75 6d 6e 20 77 61 73 20 65 78  lt column was ex
23de0 74 72 61 63 74 65 64 20 66 72 6f 6d 2e 0a 2a 2a  tracted from..**
23df0 0a 2a 2a 20 41 73 20 77 69 74 68 20 61 6c 6c 20  .** As with all 
23e00 6f 74 68 65 72 20 53 51 4c 69 74 65 20 41 50 49  other SQLite API
23e10 73 2c 20 74 68 6f 73 65 20 70 6f 73 74 66 69 78  s, those postfix
23e20 65 64 20 77 69 74 68 20 22 31 36 22 20 72 65 74  ed with "16" ret
23e30 75 72 6e 0a 2a 2a 20 55 54 46 2d 31 36 20 65 6e  urn.** UTF-16 en
23e40 63 6f 64 65 64 20 73 74 72 69 6e 67 73 2c 20 74  coded strings, t
23e50 68 65 20 6f 74 68 65 72 20 66 75 6e 63 74 69 6f  he other functio
23e60 6e 73 20 72 65 74 75 72 6e 20 55 54 46 2d 38 2e  ns return UTF-8.
23e70 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65   {END}.**.** The
23e80 73 65 20 41 50 49 73 20 61 72 65 20 6f 6e 6c 79  se APIs are only
23e90 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68   available if th
23ea0 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f  e library was co
23eb0 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a  mpiled with the.
23ec0 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ** [SQLITE_ENABL
23ed0 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54  E_COLUMN_METADAT
23ee0 41 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f  A] C-preprocesso
23ef0 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64  r symbol defined
23f00 2e 0a 2a 2a 0a 2a 2a 20 7b 41 31 33 37 35 31 7d  ..**.** {A13751}
23f10 0a 2a 2a 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f  .** If two or mo
23f20 72 65 20 74 68 72 65 61 64 73 20 63 61 6c 6c 20  re threads call 
23f30 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74  one or more of t
23f40 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 67  hese routines ag
23f50 61 69 6e 73 74 20 74 68 65 20 73 61 6d 65 0a 2a  ainst the same.*
23f60 2a 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  * prepared state
23f70 6d 65 6e 74 20 61 6e 64 20 63 6f 6c 75 6d 6e 20  ment and column 
23f80 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
23f90 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74   then the result
23fa0 73 20 61 72 65 0a 2a 2a 20 75 6e 64 65 66 69 6e  s are.** undefin
23fb0 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ed..**.** Requir
23fc0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37  ements:.** [H137
23fd0 34 31 5d 20 5b 48 31 33 37 34 32 5d 20 5b 48 31  41] [H13742] [H1
23fe0 33 37 34 33 5d 20 5b 48 31 33 37 34 34 5d 20 5b  3743] [H13744] [
23ff0 48 31 33 37 34 35 5d 20 5b 48 31 33 37 34 36 5d  H13745] [H13746]
24000 20 5b 48 31 33 37 34 38 5d 0a 2a 2a 0a 2a 2a 20   [H13748].**.** 
24010 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 74  If two or more t
24020 68 72 65 61 64 73 20 63 61 6c 6c 20 6f 6e 65 20  hreads call one 
24030 6f 72 20 6d 6f 72 65 0a 2a 2a 20 5b 73 71 6c 69  or more.** [sqli
24040 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
24050 61 73 65 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 6d  ase_name | colum
24060 6e 20 6d 65 74 61 64 61 74 61 20 69 6e 74 65 72  n metadata inter
24070 66 61 63 65 73 5d 0a 2a 2a 20 66 6f 72 20 74 68  faces].** for th
24080 65 20 73 61 6d 65 20 5b 70 72 65 70 61 72 65 64  e same [prepared
24090 20 73 74 61 74 65 6d 65 6e 74 5d 20 61 6e 64 20   statement] and 
240a0 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a  result column.**
240b0 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
240c0 65 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  e then the resul
240d0 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64  ts are undefined
240e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
240f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
24100 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
24110 61 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33  ase_name(sqlite3
24120 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c  _stmt*,int);.SQL
24130 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
24140 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
24150 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
24160 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  16(sqlite3_stmt*
24170 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50  ,int);.SQLITE_AP
24180 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
24190 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62  lite3_column_tab
241a0 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f  le_name(sqlite3_
241b0 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  stmt*,int);.SQLI
241c0 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
241d0 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  d *sqlite3_colum
241e0 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73  n_table_name16(s
241f0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
24200 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  );.SQLITE_API co
24210 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
24220 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f  3_column_origin_
24230 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  name(sqlite3_stm
24240 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t*,int);.SQLITE_
24250 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
24260 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f  sqlite3_column_o
24270 72 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c  rigin_name16(sql
24280 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
24290 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
242a0 3a 20 44 65 63 6c 61 72 65 64 20 44 61 74 61 74  : Declared Datat
242b0 79 70 65 20 4f 66 20 41 20 51 75 65 72 79 20 52  ype Of A Query R
242c0 65 73 75 6c 74 20 7b 48 31 33 37 36 30 7d 20 3c  esult {H13760} <
242d0 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S10700>.**.** Th
242e0 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
242f0 72 20 69 73 20 61 20 5b 70 72 65 70 61 72 65 64  r is a [prepared
24300 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20   statement]..** 
24310 49 66 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e  If this statemen
24320 74 20 69 73 20 61 20 5b 53 45 4c 45 43 54 5d 20  t is a [SELECT] 
24330 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 74 68  statement and th
24340 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20  e Nth column of 
24350 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  the.** returned 
24360 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68  result set of th
24370 61 74 20 5b 53 45 4c 45 43 54 5d 20 69 73 20 61  at [SELECT] is a
24380 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 28 6e   table column (n
24390 6f 74 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73  ot an.** express
243a0 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79 29  ion or subquery)
243b0 20 74 68 65 6e 20 74 68 65 20 64 65 63 6c 61 72   then the declar
243c0 65 64 20 74 79 70 65 20 6f 66 20 74 68 65 20 74  ed type of the t
243d0 61 62 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69  able.** column i
243e0 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20  s returned.  If 
243f0 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f  the Nth column o
24400 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  f the result set
24410 20 69 73 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73   is an.** expres
24420 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79  sion or subquery
24430 2c 20 74 68 65 6e 20 61 20 4e 55 4c 4c 20 70 6f  , then a NULL po
24440 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65  inter is returne
24450 64 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  d..** The return
24460 65 64 20 73 74 72 69 6e 67 20 69 73 20 61 6c 77  ed string is alw
24470 61 79 73 20 55 54 46 2d 38 20 65 6e 63 6f 64 65  ays UTF-8 encode
24480 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 46  d. {END}.**.** F
24490 6f 72 20 65 78 61 6d 70 6c 65 2c 20 67 69 76 65  or example, give
244a0 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  n the database s
244b0 63 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52 45  chema:.**.** CRE
244c0 41 54 45 20 54 41 42 4c 45 20 74 31 28 63 31 20  ATE TABLE t1(c1 
244d0 56 41 52 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a 20  VARIANT);.**.** 
244e0 61 6e 64 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  and the followin
244f0 67 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62  g statement to b
24500 65 20 63 6f 6d 70 69 6c 65 64 3a 0a 2a 2a 0a 2a  e compiled:.**.*
24510 2a 20 53 45 4c 45 43 54 20 63 31 20 2b 20 31 2c  * SELECT c1 + 1,
24520 20 63 31 20 46 52 4f 4d 20 74 31 3b 0a 2a 2a 0a   c1 FROM t1;.**.
24530 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
24540 77 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65  would return the
24550 20 73 74 72 69 6e 67 20 22 56 41 52 49 41 4e 54   string "VARIANT
24560 22 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64  " for the second
24570 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d   result.** colum
24580 6e 20 28 69 3d 3d 31 29 2c 20 61 6e 64 20 61 20  n (i==1), and a 
24590 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 66 6f 72  NULL pointer for
245a0 20 74 68 65 20 66 69 72 73 74 20 72 65 73 75 6c   the first resul
245b0 74 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 30 29 2e  t column (i==0).
245c0 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73  .**.** SQLite us
245d0 65 73 20 64 79 6e 61 6d 69 63 20 72 75 6e 2d 74  es dynamic run-t
245e0 69 6d 65 20 74 79 70 69 6e 67 2e 20 20 53 6f 20  ime typing.  So 
245f0 6a 75 73 74 20 62 65 63 61 75 73 65 20 61 20 63  just because a c
24600 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 64 65 63 6c  olumn.** is decl
24610 61 72 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20  ared to contain 
24620 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 79 70  a particular typ
24630 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e 20  e does not mean 
24640 74 68 61 74 20 74 68 65 0a 2a 2a 20 64 61 74 61  that the.** data
24650 20 73 74 6f 72 65 64 20 69 6e 20 74 68 61 74 20   stored in that 
24660 63 6f 6c 75 6d 6e 20 69 73 20 6f 66 20 74 68 65  column is of the
24670 20 64 65 63 6c 61 72 65 64 20 74 79 70 65 2e 20   declared type. 
24680 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 73 74   SQLite is.** st
24690 72 6f 6e 67 6c 79 20 74 79 70 65 64 2c 20 62 75  rongly typed, bu
246a0 74 20 74 68 65 20 74 79 70 69 6e 67 20 69 73 20  t the typing is 
246b0 64 79 6e 61 6d 69 63 20 6e 6f 74 20 73 74 61 74  dynamic not stat
246c0 69 63 2e 20 20 54 79 70 65 0a 2a 2a 20 69 73 20  ic.  Type.** is 
246d0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
246e0 69 6e 64 69 76 69 64 75 61 6c 20 76 61 6c 75 65  individual value
246f0 73 2c 20 6e 6f 74 20 77 69 74 68 20 74 68 65 20  s, not with the 
24700 63 6f 6e 74 61 69 6e 65 72 73 0a 2a 2a 20 75 73  containers.** us
24710 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 6f 73 65  ed to hold those
24720 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 52   values..**.** R
24730 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
24740 5b 48 31 33 37 36 31 5d 20 5b 48 31 33 37 36 32  [H13761] [H13762
24750 5d 20 5b 48 31 33 37 36 33 5d 0a 2a 2f 0a 53 51  ] [H13763].*/.SQ
24760 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63  LITE_API const c
24770 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  har *sqlite3_col
24780 75 6d 6e 5f 64 65 63 6c 74 79 70 65 28 73 71 6c  umn_decltype(sql
24790 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
247a0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
247b0 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
247c0 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31  column_decltype1
247d0 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  6(sqlite3_stmt*,
247e0 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  int);../*.** CAP
247f0 49 33 52 45 46 3a 20 45 76 61 6c 75 61 74 65 20  I3REF: Evaluate 
24800 41 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74  An SQL Statement
24810 20 7b 48 31 33 32 30 30 7d 20 3c 53 31 30 30 30   {H13200} <S1000
24820 30 3e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61  0>.**.** After a
24830 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
24840 6d 65 6e 74 5d 20 68 61 73 20 62 65 65 6e 20 70  ment] has been p
24850 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 65 69  repared using ei
24860 74 68 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ther.** [sqlite3
24870 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f  _prepare_v2()] o
24880 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  r [sqlite3_prepa
24890 72 65 31 36 5f 76 32 28 29 5d 20 6f 72 20 6f 6e  re16_v2()] or on
248a0 65 20 6f 66 20 74 68 65 20 6c 65 67 61 63 79 0a  e of the legacy.
248b0 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 5b 73  ** interfaces [s
248c0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29  qlite3_prepare()
248d0 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72  ] or [sqlite3_pr
248e0 65 70 61 72 65 31 36 28 29 5d 2c 20 74 68 69 73  epare16()], this
248f0 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73   function.** mus
24900 74 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 65 20  t be called one 
24910 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f  or more times to
24920 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 73 74   evaluate the st
24930 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  atement..**.** T
24940 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68  he details of th
24950 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68  e behavior of th
24960 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  e sqlite3_step()
24970 20 69 6e 74 65 72 66 61 63 65 20 64 65 70 65 6e   interface depen
24980 64 0a 2a 2a 20 6f 6e 20 77 68 65 74 68 65 72 20  d.** on whether 
24990 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 61  the statement wa
249a0 73 20 70 72 65 70 61 72 65 64 20 75 73 69 6e 67  s prepared using
249b0 20 74 68 65 20 6e 65 77 65 72 20 22 76 32 22 20   the newer "v2" 
249c0 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 5b 73 71  interface.** [sq
249d0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
249e0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
249f0 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d  _prepare16_v2()]
24a00 20 6f 72 20 74 68 65 20 6f 6c 64 65 72 20 6c 65   or the older le
24a10 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63  gacy.** interfac
24a20 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  e [sqlite3_prepa
24a30 72 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  re()] and [sqlit
24a40 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2e  e3_prepare16()].
24a50 20 20 54 68 65 20 75 73 65 20 6f 66 20 74 68 65    The use of the
24a60 0a 2a 2a 20 6e 65 77 20 22 76 32 22 20 69 6e 74  .** new "v2" int
24a70 65 72 66 61 63 65 20 69 73 20 72 65 63 6f 6d 6d  erface is recomm
24a80 65 6e 64 65 64 20 66 6f 72 20 6e 65 77 20 61 70  ended for new ap
24a90 70 6c 69 63 61 74 69 6f 6e 73 20 62 75 74 20 74  plications but t
24aa0 68 65 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74  he legacy.** int
24ab0 65 72 66 61 63 65 20 77 69 6c 6c 20 63 6f 6e 74  erface will cont
24ac0 69 6e 75 65 20 74 6f 20 62 65 20 73 75 70 70 6f  inue to be suppo
24ad0 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74  rted..**.** In t
24ae0 68 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72 66  he legacy interf
24af0 61 63 65 2c 20 74 68 65 20 72 65 74 75 72 6e 20  ace, the return 
24b00 76 61 6c 75 65 20 77 69 6c 6c 20 62 65 20 65 69  value will be ei
24b10 74 68 65 72 20 5b 53 51 4c 49 54 45 5f 42 55 53  ther [SQLITE_BUS
24b20 59 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44  Y],.** [SQLITE_D
24b30 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 52 4f  ONE], [SQLITE_RO
24b40 57 5d 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f  W], [SQLITE_ERRO
24b50 52 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d  R], or [SQLITE_M
24b60 49 53 55 53 45 5d 2e 0a 2a 2a 20 57 69 74 68 20  ISUSE]..** With 
24b70 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61  the "v2" interfa
24b80 63 65 2c 20 61 6e 79 20 6f 66 20 74 68 65 20 6f  ce, any of the o
24b90 74 68 65 72 20 5b 72 65 73 75 6c 74 20 63 6f 64  ther [result cod
24ba0 65 73 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e  es] or.** [exten
24bb0 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73  ded result codes
24bc0 5d 20 6d 69 67 68 74 20 62 65 20 72 65 74 75 72  ] might be retur
24bd0 6e 65 64 20 61 73 20 77 65 6c 6c 2e 0a 2a 2a 0a  ned as well..**.
24be0 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d  ** [SQLITE_BUSY]
24bf0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
24c00 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20  database engine 
24c10 77 61 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 63  was unable to ac
24c20 71 75 69 72 65 20 74 68 65 0a 2a 2a 20 64 61 74  quire the.** dat
24c30 61 62 61 73 65 20 6c 6f 63 6b 73 20 69 74 20 6e  abase locks it n
24c40 65 65 64 73 20 74 6f 20 64 6f 20 69 74 73 20 6a  eeds to do its j
24c50 6f 62 2e 20 20 49 66 20 74 68 65 20 73 74 61 74  ob.  If the stat
24c60 65 6d 65 6e 74 20 69 73 20 61 20 5b 43 4f 4d 4d  ement is a [COMM
24c70 49 54 5d 0a 2a 2a 20 6f 72 20 6f 63 63 75 72 73  IT].** or occurs
24c80 20 6f 75 74 73 69 64 65 20 6f 66 20 61 6e 20 65   outside of an e
24c90 78 70 6c 69 63 69 74 20 74 72 61 6e 73 61 63 74  xplicit transact
24ca0 69 6f 6e 2c 20 74 68 65 6e 20 79 6f 75 20 63 61  ion, then you ca
24cb0 6e 20 72 65 74 72 79 20 74 68 65 0a 2a 2a 20 73  n retry the.** s
24cc0 74 61 74 65 6d 65 6e 74 2e 20 20 49 66 20 74 68  tatement.  If th
24cd0 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6e  e statement is n
24ce0 6f 74 20 61 20 5b 43 4f 4d 4d 49 54 5d 20 61 6e  ot a [COMMIT] an
24cf0 64 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20  d occurs within 
24d00 61 0a 2a 2a 20 65 78 70 6c 69 63 69 74 20 74 72  a.** explicit tr
24d10 61 6e 73 61 63 74 69 6f 6e 20 74 68 65 6e 20 79  ansaction then y
24d20 6f 75 20 73 68 6f 75 6c 64 20 72 6f 6c 6c 62 61  ou should rollba
24d30 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ck the transacti
24d40 6f 6e 20 62 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e  on before.** con
24d50 74 69 6e 75 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 5b  tinuing..**.** [
24d60 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 20 6d 65 61  SQLITE_DONE] mea
24d70 6e 73 20 74 68 61 74 20 74 68 65 20 73 74 61 74  ns that the stat
24d80 65 6d 65 6e 74 20 68 61 73 20 66 69 6e 69 73 68  ement has finish
24d90 65 64 20 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20  ed executing.** 
24da0 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 73  successfully.  s
24db0 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 73 68  qlite3_step() sh
24dc0 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c  ould not be call
24dd0 65 64 20 61 67 61 69 6e 20 6f 6e 20 74 68 69 73  ed again on this
24de0 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68   virtual.** mach
24df0 69 6e 65 20 77 69 74 68 6f 75 74 20 66 69 72 73  ine without firs
24e00 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74  t calling [sqlit
24e10 65 33 5f 72 65 73 65 74 28 29 5d 20 74 6f 20 72  e3_reset()] to r
24e20 65 73 65 74 20 74 68 65 20 76 69 72 74 75 61 6c  eset the virtual
24e30 0a 2a 2a 20 6d 61 63 68 69 6e 65 20 62 61 63 6b  .** machine back
24e40 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20   to its initial 
24e50 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  state..**.** If 
24e60 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
24e70 74 20 62 65 69 6e 67 20 65 78 65 63 75 74 65 64  t being executed
24e80 20 72 65 74 75 72 6e 73 20 61 6e 79 20 64 61 74   returns any dat
24e90 61 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f  a, then [SQLITE_
24ea0 52 4f 57 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72  ROW].** is retur
24eb0 6e 65 64 20 65 61 63 68 20 74 69 6d 65 20 61 20  ned each time a 
24ec0 6e 65 77 20 72 6f 77 20 6f 66 20 64 61 74 61 20  new row of data 
24ed0 69 73 20 72 65 61 64 79 20 66 6f 72 20 70 72 6f  is ready for pro
24ee0 63 65 73 73 69 6e 67 20 62 79 20 74 68 65 0a 2a  cessing by the.*
24ef0 2a 20 63 61 6c 6c 65 72 2e 20 54 68 65 20 76 61  * caller. The va
24f00 6c 75 65 73 20 6d 61 79 20 62 65 20 61 63 63 65  lues may be acce
24f10 73 73 65 64 20 75 73 69 6e 67 20 74 68 65 20 5b  ssed using the [
24f20 63 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75  column access fu
24f30 6e 63 74 69 6f 6e 73 5d 2e 0a 2a 2a 20 73 71 6c  nctions]..** sql
24f40 69 74 65 33 5f 73 74 65 70 28 29 20 69 73 20 63  ite3_step() is c
24f50 61 6c 6c 65 64 20 61 67 61 69 6e 20 74 6f 20 72  alled again to r
24f60 65 74 72 69 65 76 65 20 74 68 65 20 6e 65 78 74  etrieve the next
24f70 20 72 6f 77 20 6f 66 20 64 61 74 61 2e 0a 2a 2a   row of data..**
24f80 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f  .** [SQLITE_ERRO
24f90 52 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20  R] means that a 
24fa0 72 75 6e 2d 74 69 6d 65 20 65 72 72 6f 72 20 28  run-time error (
24fb0 73 75 63 68 20 61 73 20 61 20 63 6f 6e 73 74 72  such as a constr
24fc0 61 69 6e 74 0a 2a 2a 20 76 69 6f 6c 61 74 69 6f  aint.** violatio
24fd0 6e 29 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e  n) has occurred.
24fe0 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29    sqlite3_step()
24ff0 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63   should not be c
25000 61 6c 6c 65 64 20 61 67 61 69 6e 20 6f 6e 0a 2a  alled again on.*
25010 2a 20 74 68 65 20 56 4d 2e 20 4d 6f 72 65 20 69  * the VM. More i
25020 6e 66 6f 72 6d 61 74 69 6f 6e 20 6d 61 79 20 62  nformation may b
25030 65 20 66 6f 75 6e 64 20 62 79 20 63 61 6c 6c 69  e found by calli
25040 6e 67 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d  ng [sqlite3_errm
25050 73 67 28 29 5d 2e 0a 2a 2a 20 57 69 74 68 20 74  sg()]..** With t
25060 68 65 20 6c 65 67 61 63 79 20 69 6e 74 65 72 66  he legacy interf
25070 61 63 65 2c 20 61 20 6d 6f 72 65 20 73 70 65 63  ace, a more spec
25080 69 66 69 63 20 65 72 72 6f 72 20 63 6f 64 65 20  ific error code 
25090 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a  (for example,.**
250a0 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55   [SQLITE_INTERRU
250b0 50 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48  PT], [SQLITE_SCH
250c0 45 4d 41 5d 2c 20 5b 53 51 4c 49 54 45 5f 43 4f  EMA], [SQLITE_CO
250d0 52 52 55 50 54 5d 2c 20 61 6e 64 20 73 6f 20 66  RRUPT], and so f
250e0 6f 72 74 68 29 0a 2a 2a 20 63 61 6e 20 62 65 20  orth).** can be 
250f0 6f 62 74 61 69 6e 65 64 20 62 79 20 63 61 6c 6c  obtained by call
25100 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72 65 73  ing [sqlite3_res
25110 65 74 28 29 5d 20 6f 6e 20 74 68 65 0a 2a 2a 20  et()] on the.** 
25120 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
25130 65 6e 74 5d 2e 20 20 49 6e 20 74 68 65 20 22 76  ent].  In the "v
25140 32 22 20 69 6e 74 65 72 66 61 63 65 2c 0a 2a 2a  2" interface,.**
25150 20 74 68 65 20 6d 6f 72 65 20 73 70 65 63 69 66   the more specif
25160 69 63 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  ic error code is
25170 20 72 65 74 75 72 6e 65 64 20 64 69 72 65 63 74   returned direct
25180 6c 79 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74  ly by sqlite3_st
25190 65 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c  ep()..**.** [SQL
251a0 49 54 45 5f 4d 49 53 55 53 45 5d 20 6d 65 61 6e  ITE_MISUSE] mean
251b0 73 20 74 68 61 74 20 74 68 65 20 74 68 69 73 20  s that the this 
251c0 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c  routine was call
251d0 65 64 20 69 6e 61 70 70 72 6f 70 72 69 61 74 65  ed inappropriate
251e0 6c 79 2e 0a 2a 2a 20 50 65 72 68 61 70 73 20 69  ly..** Perhaps i
251f0 74 20 77 61 73 20 63 61 6c 6c 65 64 20 6f 6e 20  t was called on 
25200 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  a [prepared stat
25210 65 6d 65 6e 74 5d 20 74 68 61 74 20 68 61 73 0a  ement] that has.
25220 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ** already been 
25230 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a  [sqlite3_finaliz
25240 65 20 7c 20 66 69 6e 61 6c 69 7a 65 64 5d 20 6f  e | finalized] o
25250 72 20 6f 6e 20 6f 6e 65 20 74 68 61 74 20 68 61  r on one that ha
25260 64 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20  d.** previously 
25270 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45  returned [SQLITE
25280 5f 45 52 52 4f 52 5d 20 6f 72 20 5b 53 51 4c 49  _ERROR] or [SQLI
25290 54 45 5f 44 4f 4e 45 5d 2e 20 20 4f 72 20 69 74  TE_DONE].  Or it
252a0 20 63 6f 75 6c 64 0a 2a 2a 20 62 65 20 74 68 65   could.** be the
252b0 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20 73   case that the s
252c0 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ame database con
252d0 6e 65 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67  nection is being
252e0 20 75 73 65 64 20 62 79 20 74 77 6f 20 6f 72 0a   used by two or.
252f0 2a 2a 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20  ** more threads 
25300 61 74 20 74 68 65 20 73 61 6d 65 20 6d 6f 6d 65  at the same mome
25310 6e 74 20 69 6e 20 74 69 6d 65 2e 0a 2a 2a 0a 2a  nt in time..**.*
25320 2a 20 3c 62 3e 47 6f 6f 66 79 20 49 6e 74 65 72  * <b>Goofy Inter
25330 66 61 63 65 20 41 6c 65 72 74 3a 3c 2f 62 3e 20  face Alert:</b> 
25340 49 6e 20 74 68 65 20 6c 65 67 61 63 79 20 69 6e  In the legacy in
25350 74 65 72 66 61 63 65 2c 20 74 68 65 20 73 71 6c  terface, the sql
25360 69 74 65 33 5f 73 74 65 70 28 29 0a 2a 2a 20 41  ite3_step().** A
25370 50 49 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e  PI always return
25380 73 20 61 20 67 65 6e 65 72 69 63 20 65 72 72 6f  s a generic erro
25390 72 20 63 6f 64 65 2c 20 5b 53 51 4c 49 54 45 5f  r code, [SQLITE_
253a0 45 52 52 4f 52 5d 2c 20 66 6f 6c 6c 6f 77 69 6e  ERROR], followin
253b0 67 20 61 6e 79 0a 2a 2a 20 65 72 72 6f 72 20 6f  g any.** error o
253c0 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54  ther than [SQLIT
253d0 45 5f 42 55 53 59 5d 20 61 6e 64 20 5b 53 51 4c  E_BUSY] and [SQL
253e0 49 54 45 5f 4d 49 53 55 53 45 5d 2e 20 20 59 6f  ITE_MISUSE].  Yo
253f0 75 20 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a 20 5b  u must call.** [
25400 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
25410 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e   or [sqlite3_fin
25420 61 6c 69 7a 65 28 29 5d 20 69 6e 20 6f 72 64 65  alize()] in orde
25430 72 20 74 6f 20 66 69 6e 64 20 6f 6e 65 20 6f 66  r to find one of
25440 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63   the.** specific
25450 20 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 74   [error codes] t
25460 68 61 74 20 62 65 74 74 65 72 20 64 65 73 63 72  hat better descr
25470 69 62 65 73 20 74 68 65 20 65 72 72 6f 72 2e 0a  ibes the error..
25480 2a 2a 20 57 65 20 61 64 6d 69 74 20 74 68 61 74  ** We admit that
25490 20 74 68 69 73 20 69 73 20 61 20 67 6f 6f 66 79   this is a goofy
254a0 20 64 65 73 69 67 6e 2e 20 20 54 68 65 20 70 72   design.  The pr
254b0 6f 62 6c 65 6d 20 68 61 73 20 62 65 65 6e 20 66  oblem has been f
254c0 69 78 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65  ixed.** with the
254d0 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 2e   "v2" interface.
254e0 20 20 49 66 20 79 6f 75 20 70 72 65 70 61 72 65    If you prepare
254f0 20 61 6c 6c 20 6f 66 20 79 6f 75 72 20 53 51 4c   all of your SQL
25500 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 75   statements.** u
25510 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73 71 6c  sing either [sql
25520 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
25530 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70  )] or [sqlite3_p
25540 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 69  repare16_v2()] i
25550 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65  nstead.** of the
25560 20 6c 65 67 61 63 79 20 5b 73 71 6c 69 74 65 33   legacy [sqlite3
25570 5f 70 72 65 70 61 72 65 28 29 5d 20 61 6e 64 20  _prepare()] and 
25580 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
25590 31 36 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73  16()] interfaces
255a0 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 6d 6f  ,.** then the mo
255b0 72 65 20 73 70 65 63 69 66 69 63 20 5b 65 72 72  re specific [err
255c0 6f 72 20 63 6f 64 65 73 5d 20 61 72 65 20 72 65  or codes] are re
255d0 74 75 72 6e 65 64 20 64 69 72 65 63 74 6c 79 0a  turned directly.
255e0 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74  ** by sqlite3_st
255f0 65 70 28 29 2e 20 20 54 68 65 20 75 73 65 20 6f  ep().  The use o
25600 66 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72  f the "v2" inter
25610 66 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e  face is recommen
25620 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ded..**.** Requi
25630 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33  rements:.** [H13
25640 32 30 32 5d 20 5b 48 31 35 33 30 34 5d 20 5b 48  202] [H15304] [H
25650 31 35 33 30 36 5d 20 5b 48 31 35 33 30 38 5d 20  15306] [H15308] 
25660 5b 48 31 35 33 31 30 5d 0a 2a 2f 0a 53 51 4c 49  [H15310].*/.SQLI
25670 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
25680 65 33 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f  e3_step(sqlite3_
25690 73 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  stmt*);../*.** C
256a0 41 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20  API3REF: Number 
256b0 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20  of columns in a 
256c0 72 65 73 75 6c 74 20 73 65 74 20 7b 48 31 33 37  result set {H137
256d0 37 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a  70} <S10700>.**.
256e0 2a 2a 20 52 65 74 75 72 6e 73 20 74 68 65 20 6e  ** Returns the n
256f0 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20  umber of values 
25700 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  in the current r
25710 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ow of the result
25720 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75   set..**.** Requ
25730 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
25740 33 37 37 31 5d 20 5b 48 31 33 37 37 32 5d 0a 2a  3771] [H13772].*
25750 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
25760 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f   sqlite3_data_co
25770 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  unt(sqlite3_stmt
25780 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a   *pStmt);../*.**
25790 20 43 41 50 49 33 52 45 46 3a 20 46 75 6e 64 61   CAPI3REF: Funda
257a0 6d 65 6e 74 61 6c 20 44 61 74 61 74 79 70 65 73  mental Datatypes
257b0 20 7b 48 31 30 32 36 35 7d 20 3c 53 31 30 31 31   {H10265} <S1011
257c0 30 3e 3c 53 31 30 31 32 30 3e 0a 2a 2a 20 4b 45  0><S10120>.** KE
257d0 59 57 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f 54  YWORDS: SQLITE_T
257e0 45 58 54 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 36  EXT.**.** {H1026
257f0 36 7d 20 45 76 65 72 79 20 76 61 6c 75 65 20 69  6} Every value i
25800 6e 20 53 51 4c 69 74 65 20 68 61 73 20 6f 6e 65  n SQLite has one
25810 20 6f 66 20 66 69 76 65 20 66 75 6e 64 61 6d 65   of five fundame
25820 6e 74 61 6c 20 64 61 74 61 74 79 70 65 73 3a 0a  ntal datatypes:.
25830 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c  **.** <ul>.** <l
25840 69 3e 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64  i> 64-bit signed
25850 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 6c 69 3e   integer.** <li>
25860 20 36 34 2d 62 69 74 20 49 45 45 45 20 66 6c 6f   64-bit IEEE flo
25870 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62  ating point numb
25880 65 72 0a 2a 2a 20 3c 6c 69 3e 20 73 74 72 69 6e  er.** <li> strin
25890 67 0a 2a 2a 20 3c 6c 69 3e 20 42 4c 4f 42 0a 2a  g.** <li> BLOB.*
258a0 2a 20 3c 6c 69 3e 20 4e 55 4c 4c 0a 2a 2a 20 3c  * <li> NULL.** <
258b0 2f 75 6c 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a  /ul> {END}.**.**
258c0 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73   These constants
258d0 20 61 72 65 20 63 6f 64 65 73 20 66 6f 72 20 65   are codes for e
258e0 61 63 68 20 6f 66 20 74 68 6f 73 65 20 74 79 70  ach of those typ
258f0 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  es..**.** Note t
25900 68 61 74 20 74 68 65 20 53 51 4c 49 54 45 5f 54  hat the SQLITE_T
25910 45 58 54 20 63 6f 6e 73 74 61 6e 74 20 77 61 73  EXT constant was
25920 20 61 6c 73 6f 20 75 73 65 64 20 69 6e 20 53 51   also used in SQ
25930 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 32 0a 2a  Lite version 2.*
25940 2a 20 66 6f 72 20 61 20 63 6f 6d 70 6c 65 74 65  * for a complete
25950 6c 79 20 64 69 66 66 65 72 65 6e 74 20 6d 65 61  ly different mea
25960 6e 69 6e 67 2e 20 20 53 6f 66 74 77 61 72 65 20  ning.  Software 
25970 74 68 61 74 20 6c 69 6e 6b 73 20 61 67 61 69 6e  that links again
25980 73 74 20 62 6f 74 68 0a 2a 2a 20 53 51 4c 69 74  st both.** SQLit
25990 65 20 76 65 72 73 69 6f 6e 20 32 20 61 6e 64 20  e version 2 and 
259a0 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33  SQLite version 3
259b0 20 73 68 6f 75 6c 64 20 75 73 65 20 53 51 4c 49   should use SQLI
259c0 54 45 33 5f 54 45 58 54 2c 20 6e 6f 74 0a 2a 2a  TE3_TEXT, not.**
259d0 20 53 51 4c 49 54 45 5f 54 45 58 54 2e 0a 2a 2f   SQLITE_TEXT..*/
259e0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
259f0 49 4e 54 45 47 45 52 20 20 31 0a 23 64 65 66 69  INTEGER  1.#defi
25a00 6e 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20  ne SQLITE_FLOAT 
25a10 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c     2.#define SQL
25a20 49 54 45 5f 42 4c 4f 42 20 20 20 20 20 34 0a 23  ITE_BLOB     4.#
25a30 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 55  define SQLITE_NU
25a40 4c 4c 20 20 20 20 20 35 0a 23 69 66 64 65 66 20  LL     5.#ifdef 
25a50 53 51 4c 49 54 45 5f 54 45 58 54 0a 23 20 75 6e  SQLITE_TEXT.# un
25a60 64 65 66 20 53 51 4c 49 54 45 5f 54 45 58 54 0a  def SQLITE_TEXT.
25a70 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53  #else.# define S
25a80 51 4c 49 54 45 5f 54 45 58 54 20 20 20 20 20 33  QLITE_TEXT     3
25a90 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20  .#endif.#define 
25aa0 53 51 4c 49 54 45 33 5f 54 45 58 54 20 20 20 20  SQLITE3_TEXT    
25ab0 20 33 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52   3../*.** CAPI3R
25ac0 45 46 3a 20 52 65 73 75 6c 74 20 56 61 6c 75 65  EF: Result Value
25ad0 73 20 46 72 6f 6d 20 41 20 51 75 65 72 79 20 7b  s From A Query {
25ae0 48 31 33 38 30 30 7d 20 3c 53 31 30 37 30 30 3e  H13800} <S10700>
25af0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 63  .** KEYWORDS: {c
25b00 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e  olumn access fun
25b10 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54 68  ctions}.**.** Th
25b20 65 73 65 20 72 6f 75 74 69 6e 65 73 20 66 6f 72  ese routines for
25b30 6d 20 74 68 65 20 22 72 65 73 75 6c 74 20 73 65  m the "result se
25b40 74 20 71 75 65 72 79 22 20 69 6e 74 65 72 66 61  t query" interfa
25b50 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ce..**.** These 
25b60 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20  routines return 
25b70 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
25b80 74 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d  t a single colum
25b90 6e 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74  n of the current
25ba0 0a 2a 2a 20 72 65 73 75 6c 74 20 72 6f 77 20 6f  .** result row o
25bb0 66 20 61 20 71 75 65 72 79 2e 20 20 49 6e 20 65  f a query.  In e
25bc0 76 65 72 79 20 63 61 73 65 20 74 68 65 20 66 69  very case the fi
25bd0 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20  rst argument is 
25be0 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20  a pointer.** to 
25bf0 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74  the [prepared st
25c00 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 69 73  atement] that is
25c10 20 62 65 69 6e 67 20 65 76 61 6c 75 61 74 65 64   being evaluated
25c20 20 28 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73   (the [sqlite3_s
25c30 74 6d 74 2a 5d 0a 2a 2a 20 74 68 61 74 20 77 61  tmt*].** that wa
25c40 73 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20  s returned from 
25c50 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
25c60 5f 76 32 28 29 5d 20 6f 72 20 6f 6e 65 20 6f 66  _v2()] or one of
25c70 20 69 74 73 20 76 61 72 69 61 6e 74 73 29 0a 2a   its variants).*
25c80 2a 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64  * and the second
25c90 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
25ca0 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 6f   index of the co
25cb0 6c 75 6d 6e 20 66 6f 72 20 77 68 69 63 68 20 69  lumn for which i
25cc0 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 68  nformation.** sh
25cd0 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64  ould be returned
25ce0 2e 20 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 20  .  The leftmost 
25cf0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65  column of the re
25d00 73 75 6c 74 20 73 65 74 20 68 61 73 20 74 68 65  sult set has the
25d10 20 69 6e 64 65 78 20 30 2e 0a 2a 2a 0a 2a 2a 20   index 0..**.** 
25d20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  If the SQL state
25d30 6d 65 6e 74 20 64 6f 65 73 20 6e 6f 74 20 63 75  ment does not cu
25d40 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20 74 6f  rrently point to
25d50 20 61 20 76 61 6c 69 64 20 72 6f 77 2c 20 6f 72   a valid row, or
25d60 20 69 66 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d   if the.** colum
25d70 6e 20 69 6e 64 65 78 20 69 73 20 6f 75 74 20 6f  n index is out o
25d80 66 20 72 61 6e 67 65 2c 20 74 68 65 20 72 65 73  f range, the res
25d90 75 6c 74 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ult is undefined
25da0 2e 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  ..** These routi
25db0 6e 65 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20  nes may only be 
25dc0 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20  called when the 
25dd0 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c  most recent call
25de0 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   to.** [sqlite3_
25df0 73 74 65 70 28 29 5d 20 68 61 73 20 72 65 74 75  step()] has retu
25e00 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 52 4f 57  rned [SQLITE_ROW
25e10 5d 20 61 6e 64 20 6e 65 69 74 68 65 72 0a 2a 2a  ] and neither.**
25e20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
25e30 29 5d 20 6e 6f 72 20 5b 73 71 6c 69 74 65 33 5f  )] nor [sqlite3_
25e40 66 69 6e 61 6c 69 7a 65 28 29 5d 20 68 61 76 65  finalize()] have
25e50 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 73 75 62   been called sub
25e60 73 65 71 75 65 6e 74 6c 79 2e 0a 2a 2a 20 49 66  sequently..** If
25e70 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 72 6f   any of these ro
25e80 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c 65  utines are calle
25e90 64 20 61 66 74 65 72 20 5b 73 71 6c 69 74 65 33  d after [sqlite3
25ea0 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 20  _reset()] or.** 
25eb0 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a  [sqlite3_finaliz
25ec0 65 28 29 5d 20 6f 72 20 61 66 74 65 72 20 5b 73  e()] or after [s
25ed0 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 68  qlite3_step()] h
25ee0 61 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 73  as returned.** s
25ef0 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74  omething other t
25f00 68 61 6e 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d  han [SQLITE_ROW]
25f10 2c 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 72  , the results ar
25f20 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20  e undefined..** 
25f30 49 66 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  If [sqlite3_step
25f40 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
25f50 72 65 73 65 74 28 29 5d 20 6f 72 20 5b 73 71 6c  reset()] or [sql
25f60 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d  ite3_finalize()]
25f70 0a 2a 2a 20 61 72 65 20 63 61 6c 6c 65 64 20 66  .** are called f
25f80 72 6f 6d 20 61 20 64 69 66 66 65 72 65 6e 74 20  rom a different 
25f90 74 68 72 65 61 64 20 77 68 69 6c 65 20 61 6e 79  thread while any
25fa0 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e   of these routin
25fb0 65 73 0a 2a 2a 20 61 72 65 20 70 65 6e 64 69 6e  es.** are pendin
25fc0 67 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  g, then the resu
25fd0 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65  lts are undefine
25fe0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  d..**.** The sql
25ff0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65  ite3_column_type
26000 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  () routine retur
26010 6e 73 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54  ns the.** [SQLIT
26020 45 5f 49 4e 54 45 47 45 52 20 7c 20 64 61 74 61  E_INTEGER | data
26030 74 79 70 65 20 63 6f 64 65 5d 20 66 6f 72 20 74  type code] for t
26040 68 65 20 69 6e 69 74 69 61 6c 20 64 61 74 61 20  he initial data 
26050 74 79 70 65 0a 2a 2a 20 6f 66 20 74 68 65 20 72  type.** of the r
26060 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 2e 20 20 54  esult column.  T
26070 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75  he returned valu
26080 65 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c  e is one of [SQL
26090 49 54 45 5f 49 4e 54 45 47 45 52 5d 2c 0a 2a 2a  ITE_INTEGER],.**
260a0 20 5b 53 51 4c 49 54 45 5f 46 4c 4f 41 54 5d 2c   [SQLITE_FLOAT],
260b0 20 5b 53 51 4c 49 54 45 5f 54 45 58 54 5d 2c 20   [SQLITE_TEXT], 
260c0 5b 53 51 4c 49 54 45 5f 42 4c 4f 42 5d 2c 20 6f  [SQLITE_BLOB], o
260d0 72 20 5b 53 51 4c 49 54 45 5f 4e 55 4c 4c 5d 2e  r [SQLITE_NULL].
260e0 20 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20 72    The value.** r
260f0 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74  eturned by sqlit
26100 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29  e3_column_type()
26110 20 69 73 20 6f 6e 6c 79 20 6d 65 61 6e 69 6e 67   is only meaning
26120 66 75 6c 20 69 66 20 6e 6f 20 74 79 70 65 0a 2a  ful if no type.*
26130 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 68 61  * conversions ha
26140 76 65 20 6f 63 63 75 72 72 65 64 20 61 73 20 64  ve occurred as d
26150 65 73 63 72 69 62 65 64 20 62 65 6c 6f 77 2e 20  escribed below. 
26160 20 41 66 74 65 72 20 61 20 74 79 70 65 20 63 6f   After a type co
26170 6e 76 65 72 73 69 6f 6e 2c 0a 2a 2a 20 74 68 65  nversion,.** the
26180 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
26190 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  by sqlite3_colum
261a0 6e 5f 74 79 70 65 28 29 20 69 73 20 75 6e 64 65  n_type() is unde
261b0 66 69 6e 65 64 2e 20 20 46 75 74 75 72 65 0a 2a  fined.  Future.*
261c0 2a 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  * versions of SQ
261d0 4c 69 74 65 20 6d 61 79 20 63 68 61 6e 67 65 20  Lite may change 
261e0 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20  the behavior of 
261f0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
26200 79 70 65 28 29 0a 2a 2a 20 66 6f 6c 6c 6f 77 69  ype().** followi
26210 6e 67 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72  ng a type conver
26220 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  sion..**.** If t
26230 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 42  he result is a B
26240 4c 4f 42 20 6f 72 20 55 54 46 2d 38 20 73 74 72  LOB or UTF-8 str
26250 69 6e 67 20 74 68 65 6e 20 74 68 65 20 73 71 6c  ing then the 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 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 72  s().** routine r
26280 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
26290 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
262a0 61 74 20 42 4c 4f 42 20 6f 72 20 73 74 72 69 6e  at BLOB or strin
262b0 67 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73  g..** If the res
262c0 75 6c 74 20 69 73 20 61 20 55 54 46 2d 31 36 20  ult is a UTF-16 
262d0 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 73 71 6c  string, then sql
262e0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
262f0 73 28 29 20 63 6f 6e 76 65 72 74 73 0a 2a 2a 20  s() converts.** 
26300 74 68 65 20 73 74 72 69 6e 67 20 74 6f 20 55 54  the string to UT
26310 46 2d 38 20 61 6e 64 20 74 68 65 6e 20 72 65 74  F-8 and then ret
26320 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
26330 6f 66 20 62 79 74 65 73 2e 0a 2a 2a 20 49 66 20  of bytes..** If 
26340 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20  the result is a 
26350 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20 74 68  numeric value th
26360 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  en sqlite3_colum
26370 6e 5f 62 79 74 65 73 28 29 20 75 73 65 73 0a 2a  n_bytes() uses.*
26380 2a 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  * [sqlite3_snpri
26390 6e 74 66 28 29 5d 20 74 6f 20 63 6f 6e 76 65 72  ntf()] to conver
263a0 74 20 74 68 61 74 20 76 61 6c 75 65 20 74 6f 20  t that value to 
263b0 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 61  a UTF-8 string a
263c0 6e 64 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68  nd returns.** th
263d0 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
263e0 73 20 69 6e 20 74 68 61 74 20 73 74 72 69 6e 67  s in that string
263f0 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72  ..** The value r
26400 65 74 75 72 6e 65 64 20 64 6f 65 73 20 6e 6f 74  eturned does not
26410 20 69 6e 63 6c 75 64 65 20 74 68 65 20 7a 65 72   include the zer
26420 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 61 74 20  o terminator at 
26430 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 74 68  the end.** of th
26440 65 20 73 74 72 69 6e 67 2e 20 20 46 6f 72 20 63  e string.  For c
26450 6c 61 72 69 74 79 3a 20 74 68 65 20 76 61 6c 75  larity: the valu
26460 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68  e returned is th
26470 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62  e number of.** b
26480 79 74 65 73 20 69 6e 20 74 68 65 20 73 74 72 69  ytes in the stri
26490 6e 67 2c 20 6e 6f 74 20 74 68 65 20 6e 75 6d 62  ng, not the numb
264a0 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  er of characters
264b0 2e 0a 2a 2a 0a 2a 2a 20 53 74 72 69 6e 67 73 20  ..**.** Strings 
264c0 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69  returned by sqli
264d0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28  te3_column_text(
264e0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f  ) and sqlite3_co
264f0 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c 0a 2a  lumn_text16(),.*
26500 2a 20 65 76 65 6e 20 65 6d 70 74 79 20 73 74 72  * even empty str
26510 69 6e 67 73 2c 20 61 72 65 20 61 6c 77 61 79 73  ings, are always
26520 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 64   zero terminated
26530 2e 20 20 54 68 65 20 72 65 74 75 72 6e 0a 2a 2a  .  The return.**
26540 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71 6c 69   value from sqli
26550 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
26560 29 20 66 6f 72 20 61 20 7a 65 72 6f 2d 6c 65 6e  ) for a zero-len
26570 67 74 68 20 42 4c 4f 42 20 69 73 20 61 6e 20 61  gth BLOB is an a
26580 72 62 69 74 72 61 72 79 0a 2a 2a 20 70 6f 69 6e  rbitrary.** poin
26590 74 65 72 2c 20 70 6f 73 73 69 62 6c 79 20 65 76  ter, possibly ev
265a0 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  en a NULL pointe
265b0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  r..**.** The sql
265c0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
265d0 73 31 36 28 29 20 72 6f 75 74 69 6e 65 20 69 73  s16() routine is
265e0 20 73 69 6d 69 6c 61 72 20 74 6f 20 73 71 6c 69   similar to sqli
265f0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
26600 28 29 0a 2a 2a 20 62 75 74 20 6c 65 61 76 65 73  ().** but leaves
26610 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 55   the result in U
26620 54 46 2d 31 36 20 69 6e 20 6e 61 74 69 76 65 20  TF-16 in native 
26630 62 79 74 65 20 6f 72 64 65 72 20 69 6e 73 74 65  byte order inste
26640 61 64 20 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20  ad of UTF-8..** 
26650 54 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61  The zero termina
26660 74 6f 72 20 69 73 20 6e 6f 74 20 69 6e 63 6c 75  tor is not inclu
26670 64 65 64 20 69 6e 20 74 68 69 73 20 63 6f 75 6e  ded in this coun
26680 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 62 6a  t..**.** The obj
26690 65 63 74 20 72 65 74 75 72 6e 65 64 20 62 79 20  ect returned by 
266a0 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  [sqlite3_column_
266b0 76 61 6c 75 65 28 29 5d 20 69 73 20 61 6e 0a 2a  value()] is an.*
266c0 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73  * [unprotected s
266d0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62  qlite3_value] ob
266e0 6a 65 63 74 2e 20 20 41 6e 20 75 6e 70 72 6f 74  ject.  An unprot
266f0 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61  ected sqlite3_va
26700 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 6d 61  lue object.** ma
26710 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 77  y only be used w
26720 69 74 68 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  ith [sqlite3_bin
26730 64 5f 76 61 6c 75 65 28 29 5d 20 61 6e 64 20 5b  d_value()] and [
26740 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
26750 61 6c 75 65 28 29 5d 2e 0a 2a 2a 20 49 66 20 74  alue()]..** If t
26760 68 65 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20  he [unprotected 
26770 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
26780 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 62  bject returned b
26790 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f  y.** [sqlite3_co
267a0 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73  lumn_value()] is
267b0 20 75 73 65 64 20 69 6e 20 61 6e 79 20 6f 74 68   used in any oth
267c0 65 72 20 77 61 79 2c 20 69 6e 63 6c 75 64 69 6e  er way, includin
267d0 67 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 72 6f  g calls.** to ro
267e0 75 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73 71 6c  utines like [sql
267f0 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 29  ite3_value_int()
26800 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  ], [sqlite3_valu
26810 65 5f 74 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72  e_text()],.** or
26820 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   [sqlite3_value_
26830 62 79 74 65 73 28 29 5d 2c 20 74 68 65 6e 20 74  bytes()], then t
26840 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75  he behavior is u
26850 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  ndefined..**.** 
26860 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  These routines a
26870 74 74 65 6d 70 74 20 74 6f 20 63 6f 6e 76 65 72  ttempt to conver
26880 74 20 74 68 65 20 76 61 6c 75 65 20 77 68 65 72  t the value wher
26890 65 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20 20  e appropriate.  
268a0 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20  For.** example, 
268b0 69 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  if the internal 
268c0 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69  representation i
268d0 73 20 46 4c 4f 41 54 20 61 6e 64 20 61 20 74 65  s FLOAT and a te
268e0 78 74 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20  xt result.** is 
268f0 72 65 71 75 65 73 74 65 64 2c 20 5b 73 71 6c 69  requested, [sqli
26900 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d 20  te3_snprintf()] 
26910 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c  is used internal
26920 6c 79 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68  ly to perform th
26930 65 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20  e.** conversion 
26940 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20  automatically.  
26950 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61  The following ta
26960 62 6c 65 20 64 65 74 61 69 6c 73 20 74 68 65 20  ble details the 
26970 63 6f 6e 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 74  conversions.** t
26980 68 61 74 20 61 72 65 20 61 70 70 6c 69 65 64 3a  hat are applied:
26990 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f  .**.** <blockquo
269a0 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f  te>.** <table bo
269b0 72 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72  rder="1">.** <tr
269c0 3e 3c 74 68 3e 20 49 6e 74 65 72 6e 61 6c 3c 62  ><th> Internal<b
269d0 72 3e 54 79 70 65 20 3c 74 68 3e 20 52 65 71 75  r>Type <th> Requ
269e0 65 73 74 65 64 3c 62 72 3e 54 79 70 65 20 3c 74  ested<br>Type <t
269f0 68 3e 20 20 43 6f 6e 76 65 72 73 69 6f 6e 0a 2a  h>  Conversion.*
26a00 2a 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e  *.** <tr><td>  N
26a10 55 4c 4c 20 20 20 20 3c 74 64 3e 20 49 4e 54 45  ULL    <td> INTE
26a20 47 45 52 20 20 20 3c 74 64 3e 20 52 65 73 75 6c  GER   <td> Resul
26a30 74 20 69 73 20 30 0a 2a 2a 20 3c 74 72 3e 3c 74  t is 0.** <tr><t
26a40 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e  d>  NULL    <td>
26a50 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20    FLOAT    <td> 
26a60 52 65 73 75 6c 74 20 69 73 20 30 2e 30 0a 2a 2a  Result is 0.0.**
26a70 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20   <tr><td>  NULL 
26a80 20 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20     <td>   TEXT  
26a90 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73    <td> Result is
26aa0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a   NULL pointer.**
26ab0 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20   <tr><td>  NULL 
26ac0 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20     <td>   BLOB  
26ad0 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73    <td> Result is
26ae0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a   NULL pointer.**
26af0 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47 45   <tr><td> INTEGE
26b00 52 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20  R  <td>  FLOAT  
26b10 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 66    <td> Convert f
26b20 72 6f 6d 20 69 6e 74 65 67 65 72 20 74 6f 20 66  rom integer to f
26b30 6c 6f 61 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  loat.** <tr><td>
26b40 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20   INTEGER  <td>  
26b50 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 41 53   TEXT    <td> AS
26b60 43 49 49 20 72 65 6e 64 65 72 69 6e 67 20 6f 66  CII rendering of
26b70 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20   the integer.** 
26b80 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52  <tr><td> INTEGER
26b90 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20 20    <td>   BLOB   
26ba0 20 3c 74 64 3e 20 53 61 6d 65 20 61 73 20 49 4e   <td> Same as IN
26bb0 54 45 47 45 52 2d 3e 54 45 58 54 0a 2a 2a 20 3c  TEGER->TEXT.** <
26bc0 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20  tr><td>  FLOAT  
26bd0 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20   <td> INTEGER   
26be0 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 66 72 6f  <td> Convert fro
26bf0 6d 20 66 6c 6f 61 74 20 74 6f 20 69 6e 74 65 67  m float to integ
26c00 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  er.** <tr><td>  
26c10 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 20 20 54  FLOAT   <td>   T
26c20 45 58 54 20 20 20 20 3c 74 64 3e 20 41 53 43 49  EXT    <td> ASCI
26c30 49 20 72 65 6e 64 65 72 69 6e 67 20 6f 66 20 74  I rendering of t
26c40 68 65 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72 3e  he float.** <tr>
26c50 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74  <td>  FLOAT   <t
26c60 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64  d>   BLOB    <td
26c70 3e 20 53 61 6d 65 20 61 73 20 46 4c 4f 41 54 2d  > Same as FLOAT-
26c80 3e 54 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74 64  >TEXT.** <tr><td
26c90 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20  >  TEXT    <td> 
26ca0 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 55  INTEGER   <td> U
26cb0 73 65 20 61 74 6f 69 28 29 0a 2a 2a 20 3c 74 72  se atoi().** <tr
26cc0 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20 20 3c  ><td>  TEXT    <
26cd0 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c 74  td>  FLOAT    <t
26ce0 64 3e 20 55 73 65 20 61 74 6f 66 28 29 0a 2a 2a  d> Use atof().**
26cf0 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58 54 20   <tr><td>  TEXT 
26d00 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20     <td>   BLOB  
26d10 20 20 3c 74 64 3e 20 4e 6f 20 63 68 61 6e 67 65    <td> No change
26d20 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42 4c  .** <tr><td>  BL
26d30 4f 42 20 20 20 20 3c 74 64 3e 20 49 4e 54 45 47  OB    <td> INTEG
26d40 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72  ER   <td> Conver
26d50 74 20 74 6f 20 54 45 58 54 20 74 68 65 6e 20 75  t to TEXT then u
26d60 73 65 20 61 74 6f 69 28 29 0a 2a 2a 20 3c 74 72  se atoi().** <tr
26d70 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c  ><td>  BLOB    <
26d80 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c 74  td>  FLOAT    <t
26d90 64 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20 54 45  d> Convert to TE
26da0 58 54 20 74 68 65 6e 20 75 73 65 20 61 74 6f 66  XT then use atof
26db0 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  ().** <tr><td>  
26dc0 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20 20 54  BLOB    <td>   T
26dd0 45 58 54 20 20 20 20 3c 74 64 3e 20 41 64 64 20  EXT    <td> Add 
26de0 61 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f  a zero terminato
26df0 72 20 69 66 20 6e 65 65 64 65 64 0a 2a 2a 20 3c  r if needed.** <
26e00 2f 74 61 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f  /table>.** </blo
26e10 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54  ckquote>.**.** T
26e20 68 65 20 74 61 62 6c 65 20 61 62 6f 76 65 20 6d  he table above m
26e30 61 6b 65 73 20 72 65 66 65 72 65 6e 63 65 20 74  akes reference t
26e40 6f 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62  o standard C lib
26e50 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 61  rary functions a
26e60 74 6f 69 28 29 0a 2a 2a 20 61 6e 64 20 61 74 6f  toi().** and ato
26e70 66 28 29 2e 20 20 53 51 4c 69 74 65 20 64 6f 65  f().  SQLite doe
26e80 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 75 73 65  s not really use
26e90 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73   these functions
26ea0 2e 20 20 49 74 20 68 61 73 20 69 74 73 0a 2a 2a  .  It has its.**
26eb0 20 6f 77 6e 20 65 71 75 69 76 61 6c 65 6e 74 20   own equivalent 
26ec0 69 6e 74 65 72 6e 61 6c 20 72 6f 75 74 69 6e 65  internal routine
26ed0 73 2e 20 20 54 68 65 20 61 74 6f 69 28 29 20 61  s.  The atoi() a
26ee0 6e 64 20 61 74 6f 66 28 29 20 6e 61 6d 65 73 20  nd atof() names 
26ef0 61 72 65 0a 2a 2a 20 75 73 65 64 20 69 6e 20 74  are.** used in t
26f00 68 65 20 74 61 62 6c 65 20 66 6f 72 20 62 72 65  he table for bre
26f10 76 69 74 79 20 61 6e 64 20 62 65 63 61 75 73 65  vity and because
26f20 20 74 68 65 79 20 61 72 65 20 66 61 6d 69 6c 69   they are famili
26f30 61 72 20 74 6f 20 6d 6f 73 74 0a 2a 2a 20 43 20  ar to most.** C 
26f40 70 72 6f 67 72 61 6d 6d 65 72 73 2e 0a 2a 2a 0a  programmers..**.
26f50 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68 65  ** Note that whe
26f60 6e 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f  n type conversio
26f70 6e 73 20 6f 63 63 75 72 2c 20 70 6f 69 6e 74 65  ns occur, pointe
26f80 72 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 70  rs returned by p
26f90 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f  rior.** calls to
26fa0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
26fb0 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65 33 5f  blob(), sqlite3_
26fc0 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 2c 20 61  column_text(), a
26fd0 6e 64 2f 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33  nd/or.** sqlite3
26fe0 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29  _column_text16()
26ff0 20 6d 61 79 20 62 65 20 69 6e 76 61 6c 69 64 61   may be invalida
27000 74 65 64 2e 0a 2a 2a 20 54 79 70 65 20 63 6f 6e  ted..** Type con
27010 76 65 72 73 69 6f 6e 73 20 61 6e 64 20 70 6f 69  versions and poi
27020 6e 74 65 72 20 69 6e 76 61 6c 69 64 61 74 69 6f  nter invalidatio
27030 6e 73 20 6d 69 67 68 74 20 6f 63 63 75 72 0a 2a  ns might occur.*
27040 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * in the followi
27050 6e 67 20 63 61 73 65 73 3a 0a 2a 2a 0a 2a 2a 20  ng cases:.**.** 
27060 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65  <ul>.** <li> The
27070 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74   initial content
27080 20 69 73 20 61 20 42 4c 4f 42 20 61 6e 64 20 73   is a BLOB and s
27090 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
270a0 78 74 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20  xt() or.**      
270b0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
270c0 65 78 74 31 36 28 29 20 69 73 20 63 61 6c 6c 65  ext16() is calle
270d0 64 2e 20 20 41 20 7a 65 72 6f 2d 74 65 72 6d 69  d.  A zero-termi
270e0 6e 61 74 6f 72 20 6d 69 67 68 74 0a 2a 2a 20 20  nator might.**  
270f0 20 20 20 20 6e 65 65 64 20 74 6f 20 62 65 20 61      need to be a
27100 64 64 65 64 20 74 6f 20 74 68 65 20 73 74 72 69  dded to the stri
27110 6e 67 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c 69 3e  ng.</li>.** <li>
27120 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63 6f 6e   The initial con
27130 74 65 6e 74 20 69 73 20 55 54 46 2d 38 20 74 65  tent is UTF-8 te
27140 78 74 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63  xt and sqlite3_c
27150 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 20  olumn_bytes16() 
27160 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74  or.**      sqlit
27170 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
27180 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54  () is called.  T
27190 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20  he content must 
271a0 62 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a 20  be converted.** 
271b0 20 20 20 20 20 74 6f 20 55 54 46 2d 31 36 2e 3c       to UTF-16.<
271c0 2f 6c 69 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65  /li>.** <li> The
271d0 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74   initial content
271e0 20 69 73 20 55 54 46 2d 31 36 20 74 65 78 74 20   is UTF-16 text 
271f0 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  and sqlite3_colu
27200 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a 2a 2a  mn_bytes() or.**
27210 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f        sqlite3_co
27220 6c 75 6d 6e 5f 74 65 78 74 28 29 20 69 73 20 63  lumn_text() is c
27230 61 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f 6e 74  alled.  The cont
27240 65 6e 74 20 6d 75 73 74 20 62 65 20 63 6f 6e 76  ent must be conv
27250 65 72 74 65 64 0a 2a 2a 20 20 20 20 20 20 74 6f  erted.**      to
27260 20 55 54 46 2d 38 2e 3c 2f 6c 69 3e 0a 2a 2a 20   UTF-8.</li>.** 
27270 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76  </ul>.**.** Conv
27280 65 72 73 69 6f 6e 73 20 62 65 74 77 65 65 6e 20  ersions between 
27290 55 54 46 2d 31 36 62 65 20 61 6e 64 20 55 54 46  UTF-16be and UTF
272a0 2d 31 36 6c 65 20 61 72 65 20 61 6c 77 61 79 73  -16le are always
272b0 20 64 6f 6e 65 20 69 6e 20 70 6c 61 63 65 20 61   done in place a
272c0 6e 64 20 64 6f 0a 2a 2a 20 6e 6f 74 20 69 6e 76  nd do.** not inv
272d0 61 6c 69 64 61 74 65 20 61 20 70 72 69 6f 72 20  alidate a prior 
272e0 70 6f 69 6e 74 65 72 2c 20 74 68 6f 75 67 68 20  pointer, though 
272f0 6f 66 20 63 6f 75 72 73 65 20 74 68 65 20 63 6f  of course the co
27300 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 62 75 66  ntent of the buf
27310 66 65 72 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  fer.** that the 
27320 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 20 70 6f  prior pointer po
27330 69 6e 74 73 20 74 6f 20 77 69 6c 6c 20 68 61 76  ints to will hav
27340 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e  e been modified.
27350 20 20 4f 74 68 65 72 20 6b 69 6e 64 73 0a 2a 2a    Other kinds.**
27360 20 6f 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 61   of conversion a
27370 72 65 20 64 6f 6e 65 20 69 6e 20 70 6c 61 63 65  re done in place
27380 20 77 68 65 6e 20 69 74 20 69 73 20 70 6f 73 73   when it is poss
27390 69 62 6c 65 2c 20 62 75 74 20 73 6f 6d 65 74 69  ible, but someti
273a0 6d 65 73 20 74 68 65 79 0a 2a 2a 20 61 72 65 20  mes they.** are 
273b0 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 61 6e 64  not possible and
273c0 20 69 6e 20 74 68 6f 73 65 20 63 61 73 65 73 20   in those cases 
273d0 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 73 20 61  prior pointers a
273e0 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a  re invalidated..
273f0 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 66 65 73 74  **.** The safest
27400 20 61 6e 64 20 65 61 73 69 65 73 74 20 74 6f 20   and easiest to 
27410 72 65 6d 65 6d 62 65 72 20 70 6f 6c 69 63 79 20  remember policy 
27420 69 73 20 74 6f 20 69 6e 76 6f 6b 65 20 74 68 65  is to invoke the
27430 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69  se routines.** i
27440 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c  n one of the fol
27450 6c 6f 77 69 6e 67 20 77 61 79 73 3a 0a 2a 2a 0a  lowing ways:.**.
27460 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 20 3c 6c 69 3e  ** <ul>.**  <li>
27470 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
27480 65 78 74 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62  ext() followed b
27490 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  y sqlite3_column
274a0 5f 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a  _bytes()</li>.**
274b0 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f    <li>sqlite3_co
274c0 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 6c 6c  lumn_blob() foll
274d0 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  owed by sqlite3_
274e0 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3c 2f  column_bytes()</
274f0 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69  li>.**  <li>sqli
27500 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  te3_column_text1
27510 36 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  6() followed by 
27520 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
27530 79 74 65 73 31 36 28 29 3c 2f 6c 69 3e 0a 2a 2a  ytes16()</li>.**
27540 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20   </ul>.**.** In 
27550 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 79 6f 75  other words, you
27560 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 71 6c   should call sql
27570 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
27580 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63  (),.** sqlite3_c
27590 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 6f 72  olumn_blob(), or
275a0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
275b0 74 65 78 74 31 36 28 29 20 66 69 72 73 74 20 74  text16() first t
275c0 6f 20 66 6f 72 63 65 20 74 68 65 20 72 65 73 75  o force the resu
275d0 6c 74 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 64  lt.** into the d
275e0 65 73 69 72 65 64 20 66 6f 72 6d 61 74 2c 20 74  esired format, t
275f0 68 65 6e 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74  hen invoke sqlit
27600 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
27610 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ) or.** sqlite3_
27620 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29  column_bytes16()
27630 20 74 6f 20 66 69 6e 64 20 74 68 65 20 73 69 7a   to find the siz
27640 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2e  e of the result.
27650 20 20 44 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c    Do not mix cal
27660 6c 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33  ls.** to sqlite3
27670 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f  _column_text() o
27680 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  r sqlite3_column
27690 5f 62 6c 6f 62 28 29 20 77 69 74 68 20 63 61 6c  _blob() with cal
276a0 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33  ls to.** sqlite3
276b0 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28  _column_bytes16(
276c0 29 2c 20 61 6e 64 20 64 6f 20 6e 6f 74 20 6d 69  ), and do not mi
276d0 78 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  x calls to sqlit
276e0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
276f0 28 29 0a 2a 2a 20 77 69 74 68 20 63 61 6c 6c 73  ().** with calls
27700 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   to sqlite3_colu
27710 6d 6e 5f 62 79 74 65 73 28 29 2e 0a 2a 2a 0a 2a  mn_bytes()..**.*
27720 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 73 20 72  * The pointers r
27730 65 74 75 72 6e 65 64 20 61 72 65 20 76 61 6c 69  eturned are vali
27740 64 20 75 6e 74 69 6c 20 61 20 74 79 70 65 20 63  d until a type c
27750 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75 72 73  onversion occurs
27760 20 61 73 0a 2a 2a 20 64 65 73 63 72 69 62 65 64   as.** described
27770 20 61 62 6f 76 65 2c 20 6f 72 20 75 6e 74 69 6c   above, or until
27780 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
27790 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65  ] or [sqlite3_re
277a0 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71  set()] or.** [sq
277b0 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
277c0 5d 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68  ] is called.  Th
277d0 65 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 75  e memory space u
277e0 73 65 64 20 74 6f 20 68 6f 6c 64 20 73 74 72 69  sed to hold stri
277f0 6e 67 73 0a 2a 2a 20 61 6e 64 20 42 4c 4f 42 73  ngs.** and BLOBs
27800 20 69 73 20 66 72 65 65 64 20 61 75 74 6f 6d 61   is freed automa
27810 74 69 63 61 6c 6c 79 2e 20 20 44 6f 20 3c 62 3e  tically.  Do <b>
27820 6e 6f 74 3c 2f 62 3e 20 70 61 73 73 20 74 68 65  not</b> pass the
27830 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e   pointers return
27840 65 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63  ed.** [sqlite3_c
27850 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 5d 2c 20 5b  olumn_blob()], [
27860 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
27870 65 78 74 28 29 5d 2c 20 65 74 63 2e 20 69 6e 74  ext()], etc. int
27880 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72  o.** [sqlite3_fr
27890 65 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ee()]..**.** If 
278a0 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
278b0 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73  ion error occurs
278c0 20 64 75 72 69 6e 67 20 74 68 65 20 65 76 61 6c   during the eval
278d0 75 61 74 69 6f 6e 20 6f 66 20 61 6e 79 0a 2a 2a  uation of any.**
278e0 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e   of these routin
278f0 65 73 2c 20 61 20 64 65 66 61 75 6c 74 20 76 61  es, a default va
27900 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  lue is returned.
27910 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61    The default va
27920 6c 75 65 0a 2a 2a 20 69 73 20 65 69 74 68 65 72  lue.** is either
27930 20 74 68 65 20 69 6e 74 65 67 65 72 20 30 2c 20   the integer 0, 
27940 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  the floating poi
27950 6e 74 20 6e 75 6d 62 65 72 20 30 2e 30 2c 20 6f  nt number 0.0, o
27960 72 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e  r a NULL.** poin
27970 74 65 72 2e 20 20 53 75 62 73 65 71 75 65 6e 74  ter.  Subsequent
27980 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74   calls to [sqlit
27990 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 20 77 69  e3_errcode()] wi
279a0 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 5b 53 51  ll return.** [SQ
279b0 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a 2a 0a  LITE_NOMEM]..**.
279c0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
279d0 0a 2a 2a 20 5b 48 31 33 38 30 33 5d 20 5b 48 31  .** [H13803] [H1
279e0 33 38 30 36 5d 20 5b 48 31 33 38 30 39 5d 20 5b  3806] [H13809] [
279f0 48 31 33 38 31 32 5d 20 5b 48 31 33 38 31 35 5d  H13812] [H13815]
27a00 20 5b 48 31 33 38 31 38 5d 20 5b 48 31 33 38 32   [H13818] [H1382
27a10 31 5d 20 5b 48 31 33 38 32 34 5d 0a 2a 2a 20 5b  1] [H13824].** [
27a20 48 31 33 38 32 37 5d 20 5b 48 31 33 38 33 30 5d  H13827] [H13830]
27a30 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
27a40 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
27a50 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73  e3_column_blob(s
27a60 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
27a70 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f  t iCol);.SQLITE_
27a80 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
27a90 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 71 6c  column_bytes(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 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  I int sqlite3_co
27ad0 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 73 71 6c  lumn_bytes16(sql
27ae0 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20  ite3_stmt*, int 
27af0 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50  iCol);.SQLITE_AP
27b00 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33  I double sqlite3
27b10 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 73  _column_double(s
27b20 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
27b30 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f  t iCol);.SQLITE_
27b40 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
27b50 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 69 74  column_int(sqlit
27b60 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43  e3_stmt*, int iC
27b70 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  ol);.SQLITE_API 
27b80 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71  sqlite3_int64 sq
27b90 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
27ba0 36 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  64(sqlite3_stmt*
27bb0 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c  , int iCol);.SQL
27bc0 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75 6e  ITE_API const un
27bd0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71 6c  signed char *sql
27be0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
27bf0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
27c00 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54  int iCol);.SQLIT
27c10 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
27c20 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
27c30 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f  _text16(sqlite3_
27c40 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29  stmt*, int iCol)
27c50 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
27c60 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
27c70 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  type(sqlite3_stm
27c80 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53  t*, int iCol);.S
27c90 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
27ca0 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33  3_value *sqlite3
27cb0 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 71  _column_value(sq
27cc0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
27cd0 20 69 43 6f 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43   iCol);../*.** C
27ce0 41 50 49 33 52 45 46 3a 20 44 65 73 74 72 6f 79  API3REF: Destroy
27cf0 20 41 20 50 72 65 70 61 72 65 64 20 53 74 61 74   A Prepared Stat
27d00 65 6d 65 6e 74 20 4f 62 6a 65 63 74 20 7b 48 31  ement Object {H1
27d10 33 33 30 30 7d 20 3c 53 37 30 33 30 30 3e 3c 53  3300} <S70300><S
27d20 33 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  30100>.**.** The
27d30 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
27d40 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  e() function is 
27d50 63 61 6c 6c 65 64 20 74 6f 20 64 65 6c 65 74 65  called to delete
27d60 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61   a [prepared sta
27d70 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 49 66 20 74  tement]..** If t
27d80 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73  he statement was
27d90 20 65 78 65 63 75 74 65 64 20 73 75 63 63 65 73   executed succes
27da0 73 66 75 6c 6c 79 20 6f 72 20 6e 6f 74 20 65 78  sfully or not ex
27db0 65 63 75 74 65 64 20 61 74 20 61 6c 6c 2c 20 74  ecuted at all, t
27dc0 68 65 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  hen.** SQLITE_OK
27dd0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66   is returned. If
27de0 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68   execution of th
27df0 65 20 73 74 61 74 65 6d 65 6e 74 20 66 61 69 6c  e statement fail
27e00 65 64 20 74 68 65 6e 20 61 6e 0a 2a 2a 20 5b 65  ed then an.** [e
27e10 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 5b 65  rror code] or [e
27e20 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f  xtended error co
27e30 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e  de] is returned.
27e40 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
27e50 69 6e 65 20 63 61 6e 20 62 65 20 63 61 6c 6c 65  ine can be calle
27e60 64 20 61 74 20 61 6e 79 20 70 6f 69 6e 74 20 64  d at any point d
27e70 75 72 69 6e 67 20 74 68 65 20 65 78 65 63 75 74  uring the execut
27e80 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 70  ion of the.** [p
27e90 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
27ea0 74 5d 2e 20 20 49 66 20 74 68 65 20 76 69 72 74  t].  If the virt
27eb0 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 73 20  ual machine has 
27ec0 6e 6f 74 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 64  not.** completed
27ed0 20 65 78 65 63 75 74 69 6f 6e 20 77 68 65 6e 20   execution when 
27ee0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
27ef0 63 61 6c 6c 65 64 2c 20 74 68 61 74 20 69 73 20  called, that is 
27f00 6c 69 6b 65 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65  like.** encounte
27f10 72 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 6f 72  ring an error or
27f20 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74   an [sqlite3_int
27f30 65 72 72 75 70 74 20 7c 20 69 6e 74 65 72 72 75  errupt | interru
27f40 70 74 5d 2e 0a 2a 2a 20 49 6e 63 6f 6d 70 6c 65  pt]..** Incomple
27f50 74 65 20 75 70 64 61 74 65 73 20 6d 61 79 20 62  te updates may b
27f60 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e  e rolled back an
27f70 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 63  d transactions c
27f80 61 6e 63 65 6c 65 64 2c 0a 2a 2a 20 64 65 70 65  anceled,.** depe
27f90 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 63 69 72  nding on the cir
27fa0 63 75 6d 73 74 61 6e 63 65 73 2c 20 61 6e 64 20  cumstances, and 
27fb0 74 68 65 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f  the.** [error co
27fc0 64 65 5d 20 72 65 74 75 72 6e 65 64 20 77 69 6c  de] returned wil
27fd0 6c 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 42 4f  l be [SQLITE_ABO
27fe0 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  RT]..**.** Requi
27ff0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 31  rements:.** [H11
28000 33 30 32 5d 20 5b 48 31 31 33 30 34 5d 0a 2a 2f  302] [H11304].*/
28010 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
28020 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
28030 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
28040 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  Stmt);../*.** CA
28050 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 20  PI3REF: Reset A 
28060 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65  Prepared Stateme
28070 6e 74 20 4f 62 6a 65 63 74 20 7b 48 31 33 33 33  nt Object {H1333
28080 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a  0} <S70300>.**.*
28090 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65  * The sqlite3_re
280a0 73 65 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  set() function i
280b0 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 73 65  s called to rese
280c0 74 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74  t a [prepared st
280d0 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 6f 62 6a 65  atement].** obje
280e0 63 74 20 62 61 63 6b 20 74 6f 20 69 74 73 20 69  ct back to its i
280f0 6e 69 74 69 61 6c 20 73 74 61 74 65 2c 20 72 65  nitial state, re
28100 61 64 79 20 74 6f 20 62 65 20 72 65 2d 65 78 65  ady to be re-exe
28110 63 75 74 65 64 2e 0a 2a 2a 20 41 6e 79 20 53 51  cuted..** Any SQ
28120 4c 20 73 74 61 74 65 6d 65 6e 74 20 76 61 72 69  L statement vari
28130 61 62 6c 65 73 20 74 68 61 74 20 68 61 64 20 76  ables that had v
28140 61 6c 75 65 73 20 62 6f 75 6e 64 20 74 6f 20 74  alues bound to t
28150 68 65 6d 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65  hem using.** the
28160 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62   [sqlite3_bind_b
28170 6c 6f 62 20 7c 20 73 71 6c 69 74 65 33 5f 62 69  lob | sqlite3_bi
28180 6e 64 5f 2a 28 29 20 41 50 49 5d 20 72 65 74 61  nd_*() API] reta
28190 69 6e 20 74 68 65 69 72 20 76 61 6c 75 65 73 2e  in their values.
281a0 0a 2a 2a 20 55 73 65 20 5b 73 71 6c 69 74 65 33  .** Use [sqlite3
281b0 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 28  _clear_bindings(
281c0 29 5d 20 74 6f 20 72 65 73 65 74 20 74 68 65 20  )] to reset the 
281d0 62 69 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20  bindings..**.** 
281e0 7b 48 31 31 33 33 32 7d 20 54 68 65 20 5b 73 71  {H11332} The [sq
281f0 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d 20  lite3_reset(S)] 
28200 69 6e 74 65 72 66 61 63 65 20 72 65 73 65 74 73  interface resets
28210 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73   the [prepared s
28220 74 61 74 65 6d 65 6e 74 5d 20 53 0a 2a 2a 20 20  tatement] S.**  
28230 20 20 20 20 20 20 20 20 62 61 63 6b 20 74 6f 20          back to 
28240 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
28250 20 69 74 73 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a   its program..**
28260 0a 2a 2a 20 7b 48 31 31 33 33 34 7d 20 49 66 20  .** {H11334} If 
28270 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
28280 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
28290 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 20 74 68  _step(S)] for th
282a0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70  e.**          [p
282b0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
282c0 74 5d 20 53 20 72 65 74 75 72 6e 65 64 20 5b 53  t] S returned [S
282d0 51 4c 49 54 45 5f 52 4f 57 5d 20 6f 72 20 5b 53  QLITE_ROW] or [S
282e0 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 0a 2a 2a 20  QLITE_DONE],.** 
282f0 20 20 20 20 20 20 20 20 20 6f 72 20 69 66 20 5b           or if [
28300 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d  sqlite3_step(S)]
28310 20 68 61 73 20 6e 65 76 65 72 20 62 65 66 6f 72   has never befor
28320 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 6f 6e  e been called on
28330 20 53 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   S,.**          
28340 74 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 72 65  then [sqlite3_re
28350 73 65 74 28 53 29 5d 20 72 65 74 75 72 6e 73 20  set(S)] returns 
28360 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a  [SQLITE_OK]..**.
28370 2a 2a 20 7b 48 31 31 33 33 36 7d 20 49 66 20 74  ** {H11336} If t
28380 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63  he most recent c
28390 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
283a0 73 74 65 70 28 53 29 5d 20 66 6f 72 20 74 68 65  step(S)] for the
283b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72  .**          [pr
283c0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
283d0 5d 20 53 20 69 6e 64 69 63 61 74 65 64 20 61 6e  ] S indicated an
283e0 20 65 72 72 6f 72 2c 20 74 68 65 6e 0a 2a 2a 20   error, then.** 
283f0 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
28400 33 5f 72 65 73 65 74 28 53 29 5d 20 72 65 74 75  3_reset(S)] retu
28410 72 6e 73 20 61 6e 20 61 70 70 72 6f 70 72 69 61  rns an appropria
28420 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 2e  te [error code].
28430 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33 38 7d 20  .**.** {H11338} 
28440 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73  The [sqlite3_res
28450 65 74 28 53 29 5d 20 69 6e 74 65 72 66 61 63 65  et(S)] interface
28460 20 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67 65   does not change
28470 20 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a 20 20   the values.**  
28480 20 20 20 20 20 20 20 20 6f 66 20 61 6e 79 20 5b          of any [
28490 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
284a0 62 7c 62 69 6e 64 69 6e 67 73 5d 20 6f 6e 20 74  b|bindings] on t
284b0 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  he [prepared sta
284c0 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 51  tement] S..*/.SQ
284d0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
284e0 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c 69 74  ite3_reset(sqlit
284f0 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b  e3_stmt *pStmt);
28500 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
28510 3a 20 43 72 65 61 74 65 20 4f 72 20 52 65 64 65  : Create Or Rede
28520 66 69 6e 65 20 53 51 4c 20 46 75 6e 63 74 69 6f  fine SQL Functio
28530 6e 73 20 7b 48 31 36 31 30 30 7d 20 3c 53 32 30  ns {H16100} <S20
28540 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  200>.** KEYWORDS
28550 3a 20 7b 66 75 6e 63 74 69 6f 6e 20 63 72 65 61  : {function crea
28560 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 7d 0a 2a  tion routines}.*
28570 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 70 70  * KEYWORDS: {app
28580 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
28590 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 7d 0a 2a   SQL function}.*
285a0 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61 70 70  * KEYWORDS: {app
285b0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
285c0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 7d 0a   SQL functions}.
285d0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 74 77 6f 20  **.** These two 
285e0 66 75 6e 63 74 69 6f 6e 73 20 28 63 6f 6c 6c 65  functions (colle
285f0 63 74 69 76 65 6c 79 20 6b 6e 6f 77 6e 20 61 73  ctively known as
28600 20 22 66 75 6e 63 74 69 6f 6e 20 63 72 65 61 74   "function creat
28610 69 6f 6e 20 72 6f 75 74 69 6e 65 73 22 29 0a 2a  ion routines").*
28620 2a 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 64  * are used to ad
28630 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20  d SQL functions 
28640 6f 72 20 61 67 67 72 65 67 61 74 65 73 20 6f 72  or aggregates or
28650 20 74 6f 20 72 65 64 65 66 69 6e 65 20 74 68 65   to redefine the
28660 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 6f 66 20   behavior.** of 
28670 65 78 69 73 74 69 6e 67 20 53 51 4c 20 66 75 6e  existing SQL fun
28680 63 74 69 6f 6e 73 20 6f 72 20 61 67 67 72 65 67  ctions or aggreg
28690 61 74 65 73 2e 20 20 54 68 65 20 6f 6e 6c 79 20  ates.  The only 
286a0 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65  difference betwe
286b0 65 6e 20 74 68 65 0a 2a 2a 20 74 77 6f 20 69 73  en the.** two is
286c0 20 74 68 61 74 20 74 68 65 20 73 65 63 6f 6e 64   that the second
286d0 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 20   parameter, the 
286e0 6e 61 6d 65 20 6f 66 20 74 68 65 20 28 73 63 61  name of the (sca
286f0 6c 61 72 29 20 66 75 6e 63 74 69 6f 6e 20 6f 72  lar) function or
28700 0a 2a 2a 20 61 67 67 72 65 67 61 74 65 2c 20 69  .** aggregate, i
28710 73 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46  s encoded in UTF
28720 2d 38 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63  -8 for sqlite3_c
28730 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29  reate_function()
28740 20 61 6e 64 20 55 54 46 2d 31 36 0a 2a 2a 20 66   and UTF-16.** f
28750 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  or sqlite3_creat
28760 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 2e 0a  e_function16()..
28770 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
28780 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65  parameter is the
28790 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
287a0 63 74 69 6f 6e 5d 20 74 6f 20 77 68 69 63 68 20  ction] to which 
287b0 74 68 65 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74  the SQL.** funct
287c0 69 6f 6e 20 69 73 20 74 6f 20 62 65 20 61 64 64  ion is to be add
287d0 65 64 2e 20 20 49 66 20 61 20 73 69 6e 67 6c 65  ed.  If a single
287e0 20 70 72 6f 67 72 61 6d 20 75 73 65 73 20 6d 6f   program uses mo
287f0 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 61  re than one data
28800 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69  base.** connecti
28810 6f 6e 20 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 74  on internally, t
28820 68 65 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  hen SQL function
28830 73 20 6d 75 73 74 20 62 65 20 61 64 64 65 64 20  s must be added 
28840 69 6e 64 69 76 69 64 75 61 6c 6c 79 20 74 6f 0a  individually to.
28850 2a 2a 20 65 61 63 68 20 64 61 74 61 62 61 73 65  ** each database
28860 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a   connection..**.
28870 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61  ** The second pa
28880 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e  rameter is the n
28890 61 6d 65 20 6f 66 20 74 68 65 20 53 51 4c 20 66  ame of the SQL f
288a0 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 72  unction to be cr
288b0 65 61 74 65 64 20 6f 72 0a 2a 2a 20 72 65 64 65  eated or.** rede
288c0 66 69 6e 65 64 2e 20 20 54 68 65 20 6c 65 6e 67  fined.  The leng
288d0 74 68 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 69  th of the name i
288e0 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 32 35 35  s limited to 255
288f0 20 62 79 74 65 73 2c 20 65 78 63 6c 75 73 69 76   bytes, exclusiv
28900 65 20 6f 66 0a 2a 2a 20 74 68 65 20 7a 65 72 6f  e of.** the zero
28910 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 4e 6f  -terminator.  No
28920 74 65 20 74 68 61 74 20 74 68 65 20 6e 61 6d 65  te that the name
28930 20 6c 65 6e 67 74 68 20 6c 69 6d 69 74 20 69 73   length limit is
28940 20 69 6e 20 62 79 74 65 73 2c 20 6e 6f 74 0a 2a   in bytes, not.*
28950 2a 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 41  * characters.  A
28960 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 63 72  ny attempt to cr
28970 65 61 74 65 20 61 20 66 75 6e 63 74 69 6f 6e 20  eate a function 
28980 77 69 74 68 20 61 20 6c 6f 6e 67 65 72 20 6e 61  with a longer na
28990 6d 65 0a 2a 2a 20 77 69 6c 6c 20 72 65 73 75 6c  me.** will resul
289a0 74 20 69 6e 20 5b 53 51 4c 49 54 45 5f 45 52 52  t in [SQLITE_ERR
289b0 4f 52 5d 20 62 65 69 6e 67 20 72 65 74 75 72 6e  OR] being return
289c0 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68  ed..**.** The th
289d0 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 28 6e  ird parameter (n
289e0 41 72 67 29 0a 2a 2a 20 69 73 20 74 68 65 20 6e  Arg).** is the n
289f0 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
28a00 74 73 20 74 68 61 74 20 74 68 65 20 53 51 4c 20  ts that the SQL 
28a10 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61  function or.** a
28a20 67 67 72 65 67 61 74 65 20 74 61 6b 65 73 2e 20  ggregate takes. 
28a30 49 66 20 74 68 69 73 20 70 61 72 61 6d 65 74 65  If this paramete
28a40 72 20 69 73 20 2d 31 2c 20 74 68 65 6e 20 74 68  r is -1, then th
28a50 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f  e SQL function o
28a60 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65 20 6d  r.** aggregate m
28a70 61 79 20 74 61 6b 65 20 61 6e 79 20 6e 75 6d 62  ay take any numb
28a80 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
28a90 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 74 68  between 0 and th
28aa0 65 20 6c 69 6d 69 74 0a 2a 2a 20 73 65 74 20 62  e limit.** set b
28ab0 79 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74  y [sqlite3_limit
28ac0 5d 28 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  ]([SQLITE_LIMIT_
28ad0 46 55 4e 43 54 49 4f 4e 5f 41 52 47 5d 29 2e 20  FUNCTION_ARG]). 
28ae0 20 49 66 20 74 68 65 20 74 68 69 72 64 0a 2a 2a   If the third.**
28af0 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6c 65   parameter is le
28b00 73 73 20 74 68 61 6e 20 2d 31 20 6f 72 20 67 72  ss than -1 or gr
28b10 65 61 74 65 72 20 74 68 61 6e 20 31 32 37 20 74  eater than 127 t
28b20 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72  hen the behavior
28b30 20 69 73 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64   is.** undefined
28b40 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 75 72  ..**.** The four
28b50 74 68 20 70 61 72 61 6d 65 74 65 72 2c 20 65 54  th parameter, eT
28b60 65 78 74 52 65 70 2c 20 73 70 65 63 69 66 69 65  extRep, specifie
28b70 73 20 77 68 61 74 0a 2a 2a 20 5b 53 51 4c 49 54  s what.** [SQLIT
28b80 45 5f 55 54 46 38 20 7c 20 74 65 78 74 20 65 6e  E_UTF8 | text en
28b90 63 6f 64 69 6e 67 5d 20 74 68 69 73 20 53 51 4c  coding] this SQL
28ba0 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 66 65 72   function prefer
28bb0 73 20 66 6f 72 0a 2a 2a 20 69 74 73 20 70 61 72  s for.** its par
28bc0 61 6d 65 74 65 72 73 2e 20 20 41 6e 79 20 53 51  ameters.  Any SQ
28bd0 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65  L function imple
28be0 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64  mentation should
28bf0 20 62 65 20 61 62 6c 65 20 74 6f 20 77 6f 72 6b   be able to work
28c00 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68 20 55 54  .** work with UT
28c10 46 2d 38 2c 20 55 54 46 2d 31 36 6c 65 2c 20 6f  F-8, UTF-16le, o
28c20 72 20 55 54 46 2d 31 36 62 65 2e 20 20 42 75 74  r UTF-16be.  But
28c30 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74 61   some implementa
28c40 74 69 6f 6e 73 20 6d 61 79 20 62 65 0a 2a 2a 20  tions may be.** 
28c50 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74 20 77  more efficient w
28c60 69 74 68 20 6f 6e 65 20 65 6e 63 6f 64 69 6e 67  ith one encoding
28c70 20 74 68 61 6e 20 61 6e 6f 74 68 65 72 2e 20 20   than another.  
28c80 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d  An application m
28c90 61 79 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c  ay.** invoke sql
28ca0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
28cb0 74 69 6f 6e 28 29 20 6f 72 20 73 71 6c 69 74 65  tion() or sqlite
28cc0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
28cd0 6e 31 36 28 29 20 6d 75 6c 74 69 70 6c 65 0a 2a  n16() multiple.*
28ce0 2a 20 74 69 6d 65 73 20 77 69 74 68 20 74 68 65  * times with the
28cf0 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 62   same function b
28d00 75 74 20 77 69 74 68 20 64 69 66 66 65 72 65 6e  ut with differen
28d10 74 20 76 61 6c 75 65 73 20 6f 66 20 65 54 65 78  t values of eTex
28d20 74 52 65 70 2e 0a 2a 2a 20 57 68 65 6e 20 6d 75  tRep..** When mu
28d30 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  ltiple implement
28d40 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61  ations of the sa
28d50 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20  me function are 
28d60 61 76 61 69 6c 61 62 6c 65 2c 20 53 51 4c 69 74  available, SQLit
28d70 65 0a 2a 2a 20 77 69 6c 6c 20 70 69 63 6b 20 74  e.** will pick t
28d80 68 65 20 6f 6e 65 20 74 68 61 74 20 69 6e 76 6f  he one that invo
28d90 6c 76 65 73 20 74 68 65 20 6c 65 61 73 74 20 61  lves the least a
28da0 6d 6f 75 6e 74 20 6f 66 20 64 61 74 61 20 63 6f  mount of data co
28db0 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 20 49 66 20  nversion..** If 
28dc0 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20  there is only a 
28dd0 73 69 6e 67 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  single implement
28de0 61 74 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73  ation which does
28df0 20 6e 6f 74 20 63 61 72 65 20 77 68 61 74 20 74   not care what t
28e00 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 20  ext.** encoding 
28e10 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 74 68  is used, then th
28e20 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e  e fourth argumen
28e30 74 20 73 68 6f 75 6c 64 20 62 65 20 5b 53 51 4c  t should be [SQL
28e40 49 54 45 5f 41 4e 59 5d 2e 0a 2a 2a 0a 2a 2a 20  ITE_ANY]..**.** 
28e50 54 68 65 20 66 69 66 74 68 20 70 61 72 61 6d 65  The fifth parame
28e60 74 65 72 20 69 73 20 61 6e 20 61 72 62 69 74 72  ter is an arbitr
28e70 61 72 79 20 70 6f 69 6e 74 65 72 2e 20 20 54 68  ary pointer.  Th
28e80 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
28e90 20 6f 66 20 74 68 65 0a 2a 2a 20 66 75 6e 63 74   of the.** funct
28ea0 69 6f 6e 20 63 61 6e 20 67 61 69 6e 20 61 63 63  ion can gain acc
28eb0 65 73 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e  ess to this poin
28ec0 74 65 72 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  ter using [sqlit
28ed0 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 5d 2e  e3_user_data()].
28ee0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 76 65 6e  .**.** The seven
28ef0 74 68 2c 20 65 69 67 68 74 68 20 61 6e 64 20 6e  th, eighth and n
28f00 69 6e 74 68 20 70 61 72 61 6d 65 74 65 72 73 2c  inth parameters,
28f10 20 78 46 75 6e 63 2c 20 78 53 74 65 70 20 61 6e   xFunc, xStep an
28f20 64 20 78 46 69 6e 61 6c 2c 20 61 72 65 0a 2a 2a  d xFinal, are.**
28f30 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 43 2d 6c   pointers to C-l
28f40 61 6e 67 75 61 67 65 20 66 75 6e 63 74 69 6f 6e  anguage function
28f50 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
28f60 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
28f70 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74  n or.** aggregat
28f80 65 2e 20 41 20 73 63 61 6c 61 72 20 53 51 4c 20  e. A scalar SQL 
28f90 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65  function require
28fa0 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74  s an implementat
28fb0 69 6f 6e 20 6f 66 20 74 68 65 20 78 46 75 6e 63  ion of the xFunc
28fc0 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 6c  .** callback onl
28fd0 79 2c 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73  y, NULL pointers
28fe0 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65   should be passe
28ff0 64 20 61 73 20 74 68 65 20 78 53 74 65 70 20 61  d as the xStep a
29000 6e 64 20 78 46 69 6e 61 6c 0a 2a 2a 20 70 61 72  nd xFinal.** par
29010 61 6d 65 74 65 72 73 2e 20 41 6e 20 61 67 67 72  ameters. An aggr
29020 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69  egate SQL functi
29030 6f 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 69  on requires an i
29040 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
29050 20 78 53 74 65 70 0a 2a 2a 20 61 6e 64 20 78 46   xStep.** and xF
29060 69 6e 61 6c 20 61 6e 64 20 4e 55 4c 4c 20 73 68  inal and NULL sh
29070 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 66  ould be passed f
29080 6f 72 20 78 46 75 6e 63 2e 20 54 6f 20 64 65 6c  or xFunc. To del
29090 65 74 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a  ete an existing.
290a0 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  ** SQL function 
290b0 6f 72 20 61 67 67 72 65 67 61 74 65 2c 20 70 61  or aggregate, pa
290c0 73 73 20 4e 55 4c 4c 20 66 6f 72 20 61 6c 6c 20  ss NULL for all 
290d0 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 20 63  three function c
290e0 61 6c 6c 62 61 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20  allbacks..**.** 
290f0 49 74 20 69 73 20 70 65 72 6d 69 74 74 65 64 20  It is permitted 
29100 74 6f 20 72 65 67 69 73 74 65 72 20 6d 75 6c 74  to register mult
29110 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  iple implementat
29120 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65  ions of the same
29130 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 77 69  .** functions wi
29140 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  th the same name
29150 20 62 75 74 20 77 69 74 68 20 65 69 74 68 65 72   but with either
29160 20 64 69 66 66 65 72 69 6e 67 20 6e 75 6d 62 65   differing numbe
29170 72 73 20 6f 66 0a 2a 2a 20 61 72 67 75 6d 65 6e  rs of.** argumen
29180 74 73 20 6f 72 20 64 69 66 66 65 72 69 6e 67 20  ts or differing 
29190 70 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65  preferred text e
291a0 6e 63 6f 64 69 6e 67 73 2e 20 20 53 51 4c 69 74  ncodings.  SQLit
291b0 65 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 74 68  e will use.** th
291c0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
291d0 20 74 68 61 74 20 6d 6f 73 74 20 63 6c 6f 73 65   that most close
291e0 6c 79 20 6d 61 74 63 68 65 73 20 74 68 65 20 77  ly matches the w
291f0 61 79 20 69 6e 20 77 68 69 63 68 20 74 68 65 0a  ay in which the.
29200 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  ** SQL function 
29210 69 73 20 75 73 65 64 2e 20 20 41 20 66 75 6e 63  is used.  A func
29220 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74  tion implementat
29230 69 6f 6e 20 77 69 74 68 20 61 20 6e 6f 6e 2d 6e  ion with a non-n
29240 65 67 61 74 69 76 65 0a 2a 2a 20 6e 41 72 67 20  egative.** nArg 
29250 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 62  parameter is a b
29260 65 74 74 65 72 20 6d 61 74 63 68 20 74 68 61 6e  etter match than
29270 20 61 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c   a function impl
29280 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74 68 0a  ementation with.
29290 2a 2a 20 61 20 6e 65 67 61 74 69 76 65 20 6e 41  ** a negative nA
292a0 72 67 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20  rg.  A function 
292b0 77 68 65 72 65 20 74 68 65 20 70 72 65 66 65 72  where the prefer
292c0 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  red text encodin
292d0 67 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74 68 65  g.** matches the
292e0 20 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69   database encodi
292f0 6e 67 20 69 73 20 61 20 62 65 74 74 65 72 0a 2a  ng is a better.*
29300 2a 20 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66  * match than a f
29310 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68  unction where th
29320 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 64 69  e encoding is di
29330 66 66 65 72 65 6e 74 2e 20 20 0a 2a 2a 20 41 20  fferent.  .** A 
29340 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20 74  function where t
29350 68 65 20 65 6e 63 6f 64 69 6e 67 20 64 69 66 66  he encoding diff
29360 65 72 65 6e 63 65 20 69 73 20 62 65 74 77 65 65  erence is betwee
29370 6e 20 55 54 46 31 36 6c 65 20 61 6e 64 20 55 54  n UTF16le and UT
29380 46 31 36 62 65 0a 2a 2a 20 69 73 20 61 20 63 6c  F16be.** is a cl
29390 6f 73 65 72 20 6d 61 74 63 68 20 74 68 61 6e 20  oser match than 
293a0 61 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65  a function where
293b0 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 64 69   the encoding di
293c0 66 66 65 72 65 6e 63 65 20 69 73 0a 2a 2a 20 62  fference is.** b
293d0 65 74 77 65 65 6e 20 55 54 46 38 20 61 6e 64 20  etween UTF8 and 
293e0 55 54 46 31 36 2e 0a 2a 2a 0a 2a 2a 20 42 75 69  UTF16..**.** Bui
293f0 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20  lt-in functions 
29400 6d 61 79 20 62 65 20 6f 76 65 72 6c 6f 61 64 65  may be overloade
29410 64 20 62 79 20 6e 65 77 20 61 70 70 6c 69 63 61  d by new applica
29420 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e  tion-defined fun
29430 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 66  ctions..** The f
29440 69 72 73 74 20 61 70 70 6c 69 63 61 74 69 6f 6e  irst application
29450 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
29460 6e 20 77 69 74 68 20 61 20 67 69 76 65 6e 20 6e  n with a given n
29470 61 6d 65 20 6f 76 65 72 72 69 64 65 73 20 61 6c  ame overrides al
29480 6c 0a 2a 2a 20 62 75 69 6c 74 2d 69 6e 20 66 75  l.** built-in fu
29490 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 73  nctions in the s
294a0 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  ame [database co
294b0 6e 6e 65 63 74 69 6f 6e 5d 20 77 69 74 68 20 74  nnection] with t
294c0 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 0a 2a 2a  he same name..**
294d0 20 53 75 62 73 65 71 75 65 6e 74 20 61 70 70 6c   Subsequent appl
294e0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
294f0 66 75 6e 63 74 69 6f 6e 73 20 6f 66 20 74 68 65  functions of the
29500 20 73 61 6d 65 20 6e 61 6d 65 20 6f 6e 6c 79 20   same name only 
29510 6f 76 65 72 72 69 64 65 20 0a 2a 2a 20 70 72 69  override .** pri
29520 6f 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  or application-d
29530 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73  efined functions
29540 20 74 68 61 74 20 61 72 65 20 61 6e 20 65 78 61   that are an exa
29550 63 74 20 6d 61 74 63 68 20 66 6f 72 20 74 68 65  ct match for the
29560 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  .** number of pa
29570 72 61 6d 65 74 65 72 73 20 61 6e 64 20 70 72 65  rameters and pre
29580 66 65 72 72 65 64 20 65 6e 63 6f 64 69 6e 67 2e  ferred encoding.
29590 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 70 70 6c 69 63  .**.** An applic
295a0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75  ation-defined fu
295b0 6e 63 74 69 6f 6e 20 69 73 20 70 65 72 6d 69 74  nction is permit
295c0 74 65 64 20 74 6f 20 63 61 6c 6c 20 6f 74 68 65  ted to call othe
295d0 72 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e 74 65  r.** SQLite inte
295e0 72 66 61 63 65 73 2e 20 20 48 6f 77 65 76 65 72  rfaces.  However
295f0 2c 20 73 75 63 68 20 63 61 6c 6c 73 20 6d 75 73  , such calls mus
29600 74 20 6e 6f 74 0a 2a 2a 20 63 6c 6f 73 65 20 74  t not.** close t
29610 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
29620 65 63 74 69 6f 6e 20 6e 6f 72 20 66 69 6e 61 6c  ection nor final
29630 69 7a 65 20 6f 72 20 72 65 73 65 74 20 74 68 65  ize or reset the
29640 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 73 74 61   prepared.** sta
29650 74 65 6d 65 6e 74 20 69 6e 20 77 68 69 63 68 20  tement in which 
29660 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
29670 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52  running..**.** R
29680 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
29690 5b 48 31 36 31 30 33 5d 20 5b 48 31 36 31 30 36  [H16103] [H16106
296a0 5d 20 5b 48 31 36 31 30 39 5d 20 5b 48 31 36 31  ] [H16109] [H161
296b0 31 32 5d 20 5b 48 31 36 31 31 38 5d 20 5b 48 31  12] [H16118] [H1
296c0 36 31 32 31 5d 20 5b 48 31 36 31 32 37 5d 0a 2a  6121] [H16127].*
296d0 2a 20 5b 48 31 36 31 33 30 5d 20 5b 48 31 36 31  * [H16130] [H161
296e0 33 33 5d 20 5b 48 31 36 31 33 36 5d 20 5b 48 31  33] [H16136] [H1
296f0 36 31 33 39 5d 20 5b 48 31 36 31 34 32 5d 0a 2a  6139] [H16142].*
29700 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
29710 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
29720 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69  function(.  sqli
29730 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74  te3 *db,.  const
29740 20 63 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e   char *zFunction
29750 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67  Name,.  int nArg
29760 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70  ,.  int eTextRep
29770 2c 0a 20 20 76 6f 69 64 20 2a 70 41 70 70 2c 0a  ,.  void *pApp,.
29780 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28    void (*xFunc)(
29790 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
297a0 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
297b0 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a  ue**),.  void (*
297c0 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63  xStep)(sqlite3_c
297d0 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
297e0 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20  te3_value**),.  
297f0 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73  void (*xFinal)(s
29800 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
29810 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .);.SQLITE_API i
29820 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
29830 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 0a 20 20  e_function16(.  
29840 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63  sqlite3 *db,.  c
29850 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46 75 6e 63  onst void *zFunc
29860 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  tionName,.  int 
29870 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78  nArg,.  int eTex
29880 74 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 41  tRep,.  void *pA
29890 70 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75  pp,.  void (*xFu
298a0 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nc)(sqlite3_cont
298b0 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
298c0 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69  _value**),.  voi
298d0 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74  d (*xStep)(sqlit
298e0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
298f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
29900 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61  ,.  void (*xFina
29910 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  l)(sqlite3_conte
29920 78 74 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  xt*).);../*.** C
29930 41 50 49 33 52 45 46 3a 20 54 65 78 74 20 45 6e  API3REF: Text En
29940 63 6f 64 69 6e 67 73 20 7b 48 31 30 32 36 37 7d  codings {H10267}
29950 20 3c 53 35 30 32 30 30 3e 20 3c 48 31 36 31 30   <S50200> <H1610
29960 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63  0>.**.** These c
29970 6f 6e 73 74 61 6e 74 20 64 65 66 69 6e 65 20 69  onstant define i
29980 6e 74 65 67 65 72 20 63 6f 64 65 73 20 74 68 61  nteger codes tha
29990 74 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20  t represent the 
299a0 76 61 72 69 6f 75 73 0a 2a 2a 20 74 65 78 74 20  various.** text 
299b0 65 6e 63 6f 64 69 6e 67 73 20 73 75 70 70 6f 72  encodings suppor
299c0 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a  ted by SQLite..*
299d0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
299e0 5f 55 54 46 38 20 20 20 20 20 20 20 20 20 20 20  _UTF8           
299f0 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  1.#define SQLITE
29a00 5f 55 54 46 31 36 4c 45 20 20 20 20 20 20 20 20  _UTF16LE        
29a10 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  2.#define SQLITE
29a20 5f 55 54 46 31 36 42 45 20 20 20 20 20 20 20 20  _UTF16BE        
29a30 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  3.#define SQLITE
29a40 5f 55 54 46 31 36 20 20 20 20 20 20 20 20 20 20  _UTF16          
29a50 34 20 20 20 20 2f 2a 20 55 73 65 20 6e 61 74 69  4    /* Use nati
29a60 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 2a 2f  ve byte order */
29a70 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
29a80 41 4e 59 20 20 20 20 20 20 20 20 20 20 20 20 35  ANY            5
29a90 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63      /* sqlite3_c
29aa0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 6f  reate_function o
29ab0 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  nly */.#define S
29ac0 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47  QLITE_UTF16_ALIG
29ad0 4e 45 44 20 20 38 20 20 20 20 2f 2a 20 73 71 6c  NED  8    /* sql
29ae0 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
29af0 61 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f  ation only */../
29b00 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44  *.** CAPI3REF: D
29b10 65 70 72 65 63 61 74 65 64 20 46 75 6e 63 74 69  eprecated Functi
29b20 6f 6e 73 0a 2a 2a 20 44 45 50 52 45 43 41 54 45  ons.** DEPRECATE
29b30 44 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75  D.**.** These fu
29b40 6e 63 74 69 6f 6e 73 20 61 72 65 20 5b 64 65 70  nctions are [dep
29b50 72 65 63 61 74 65 64 5d 2e 20 20 49 6e 20 6f 72  recated].  In or
29b60 64 65 72 20 74 6f 20 6d 61 69 6e 74 61 69 6e 0a  der to maintain.
29b70 2a 2a 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d  ** backwards com
29b80 70 61 74 69 62 69 6c 69 74 79 20 77 69 74 68 20  patibility with 
29b90 6f 6c 64 65 72 20 63 6f 64 65 2c 20 74 68 65 73  older code, thes
29ba0 65 20 66 75 6e 63 74 69 6f 6e 73 20 63 6f 6e 74  e functions cont
29bb0 69 6e 75 65 20 0a 2a 2a 20 74 6f 20 62 65 20 73  inue .** to be s
29bc0 75 70 70 6f 72 74 65 64 2e 20 20 48 6f 77 65 76  upported.  Howev
29bd0 65 72 2c 20 6e 65 77 20 61 70 70 6c 69 63 61 74  er, new applicat
29be0 69 6f 6e 73 20 73 68 6f 75 6c 64 20 61 76 6f 69  ions should avoi
29bf0 64 0a 2a 2a 20 74 68 65 20 75 73 65 20 6f 66 20  d.** the use of 
29c00 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 2e  these functions.
29c10 20 20 54 6f 20 68 65 6c 70 20 65 6e 63 6f 75 72    To help encour
29c20 61 67 65 20 70 65 6f 70 6c 65 20 74 6f 20 61 76  age people to av
29c30 6f 69 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65  oid.** using the
29c40 73 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20 77 65  se functions, we
29c50 20 61 72 65 20 6e 6f 74 20 67 6f 69 6e 67 20 74   are not going t
29c60 6f 20 74 65 6c 6c 20 79 6f 75 20 77 68 61 74 20  o tell you what 
29c70 74 68 65 79 20 64 6f 2e 0a 2a 2f 0a 23 69 66 6e  they do..*/.#ifn
29c80 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
29c90 44 45 50 52 45 43 41 54 45 44 0a 53 51 4c 49 54  DEPRECATED.SQLIT
29ca0 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50  E_API SQLITE_DEP
29cb0 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69  RECATED int sqli
29cc0 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
29cd0 75 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  unt(sqlite3_cont
29ce0 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  ext*);.SQLITE_AP
29cf0 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41  I SQLITE_DEPRECA
29d00 54 45 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  TED int sqlite3_
29d10 65 78 70 69 72 65 64 28 73 71 6c 69 74 65 33 5f  expired(sqlite3_
29d20 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  stmt*);.SQLITE_A
29d30 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43  PI SQLITE_DEPREC
29d40 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74 65 33  ATED int sqlite3
29d50 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e  _transfer_bindin
29d60 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  gs(sqlite3_stmt*
29d70 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  , sqlite3_stmt*)
29d80 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c  ;.SQLITE_API SQL
29d90 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 69  ITE_DEPRECATED i
29da0 6e 74 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62 61  nt sqlite3_globa
29db0 6c 5f 72 65 63 6f 76 65 72 28 76 6f 69 64 29 3b  l_recover(void);
29dc0 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
29dd0 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 76 6f  TE_DEPRECATED vo
29de0 69 64 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61  id sqlite3_threa
29df0 64 5f 63 6c 65 61 6e 75 70 28 76 6f 69 64 29 3b  d_cleanup(void);
29e00 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
29e10 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e  TE_DEPRECATED in
29e20 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79  t sqlite3_memory
29e30 5f 61 6c 61 72 6d 28 76 6f 69 64 28 2a 29 28 76  _alarm(void(*)(v
29e40 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74  oid*,sqlite3_int
29e50 36 34 2c 69 6e 74 29 2c 76 6f 69 64 2a 2c 73 71  64,int),void*,sq
29e60 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 23 65  lite3_int64);.#e
29e70 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ndif../*.** CAPI
29e80 33 52 45 46 3a 20 4f 62 74 61 69 6e 69 6e 67 20  3REF: Obtaining 
29e90 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20 50 61 72  SQL Function Par
29ea0 61 6d 65 74 65 72 20 56 61 6c 75 65 73 20 7b 48  ameter Values {H
29eb0 31 35 31 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a  15100} <S20200>.
29ec0 2a 2a 0a 2a 2a 20 54 68 65 20 43 2d 6c 61 6e 67  **.** The C-lang
29ed0 75 61 67 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  uage implementat
29ee0 69 6f 6e 20 6f 66 20 53 51 4c 20 66 75 6e 63 74  ion of SQL funct
29ef0 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 65 67 61  ions and aggrega
29f00 74 65 73 20 75 73 65 73 0a 2a 2a 20 74 68 69 73  tes uses.** this
29f10 20 73 65 74 20 6f 66 20 69 6e 74 65 72 66 61 63   set of interfac
29f20 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 20 61 63  e routines to ac
29f30 63 65 73 73 20 74 68 65 20 70 61 72 61 6d 65 74  cess the paramet
29f40 65 72 20 76 61 6c 75 65 73 20 6f 6e 0a 2a 2a 20  er values on.** 
29f50 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20  the function or 
29f60 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a 0a 2a 2a  aggregate..**.**
29f70 20 54 68 65 20 78 46 75 6e 63 20 28 66 6f 72 20   The xFunc (for 
29f80 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 73  scalar functions
29f90 29 20 6f 72 20 78 53 74 65 70 20 28 66 6f 72 20  ) or xStep (for 
29fa0 61 67 67 72 65 67 61 74 65 73 29 20 70 61 72 61  aggregates) para
29fb0 6d 65 74 65 72 73 0a 2a 2a 20 74 6f 20 5b 73 71  meters.** to [sq
29fc0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
29fd0 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71  ction()] and [sq
29fe0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
29ff0 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a 20 64 65  ction16()].** de
2a000 66 69 6e 65 20 63 61 6c 6c 62 61 63 6b 73 20 74  fine callbacks t
2a010 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68  hat implement th
2a020 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20  e SQL functions 
2a030 61 6e 64 20 61 67 67 72 65 67 61 74 65 73 2e 0a  and aggregates..
2a040 2a 2a 20 54 68 65 20 34 74 68 20 70 61 72 61 6d  ** The 4th param
2a050 65 74 65 72 20 74 6f 20 74 68 65 73 65 20 63 61  eter to these ca
2a060 6c 6c 62 61 63 6b 73 20 69 73 20 61 6e 20 61 72  llbacks is an ar
2a070 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20  ray of pointers 
2a080 74 6f 0a 2a 2a 20 5b 70 72 6f 74 65 63 74 65 64  to.** [protected
2a090 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20   sqlite3_value] 
2a0a0 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 72 65 20  objects.  There 
2a0b0 69 73 20 6f 6e 65 20 5b 73 71 6c 69 74 65 33 5f  is one [sqlite3_
2a0c0 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 66 6f  value] object fo
2a0d0 72 0a 2a 2a 20 65 61 63 68 20 70 61 72 61 6d 65  r.** each parame
2a0e0 74 65 72 20 74 6f 20 74 68 65 20 53 51 4c 20 66  ter to the SQL f
2a0f0 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73 65 20  unction.  These 
2a100 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65  routines are use
2a110 64 20 74 6f 0a 2a 2a 20 65 78 74 72 61 63 74 20  d to.** extract 
2a120 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65 20  values from the 
2a130 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20  [sqlite3_value] 
2a140 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  objects..**.** T
2a150 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77 6f  hese routines wo
2a160 72 6b 20 6f 6e 6c 79 20 77 69 74 68 20 5b 70 72  rk only with [pr
2a170 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
2a180 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73 2e 0a  value] objects..
2a190 2a 2a 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74  ** Any attempt t
2a1a0 6f 20 75 73 65 20 74 68 65 73 65 20 72 6f 75 74  o use these rout
2a1b0 69 6e 65 73 20 6f 6e 20 61 6e 20 5b 75 6e 70 72  ines on an [unpr
2a1c0 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
2a1d0 76 61 6c 75 65 5d 0a 2a 2a 20 6f 62 6a 65 63 74  value].** object
2a1e0 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65   results in unde
2a1f0 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a  fined behavior..
2a200 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
2a210 69 6e 65 73 20 77 6f 72 6b 20 6a 75 73 74 20 6c  ines work just l
2a220 69 6b 65 20 74 68 65 20 63 6f 72 72 65 73 70 6f  ike the correspo
2a230 6e 64 69 6e 67 20 5b 63 6f 6c 75 6d 6e 20 61 63  nding [column ac
2a240 63 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 5d 0a  cess functions].
2a250 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 20  ** except that  
2a260 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 74  these routines t
2a270 61 6b 65 20 61 20 73 69 6e 67 6c 65 20 5b 70 72  ake a single [pr
2a280 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
2a290 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 0a 2a 2a  value] object.**
2a2a0 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64   pointer instead
2a2b0 20 6f 66 20 61 20 5b 73 71 6c 69 74 65 33 5f 73   of a [sqlite3_s
2a2c0 74 6d 74 2a 5d 20 70 6f 69 6e 74 65 72 20 61 6e  tmt*] pointer an
2a2d0 64 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 6c  d an integer col
2a2e0 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a  umn number..**.*
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 74 65 78 74 31 36 28 29 20 69 6e 74  lue_text16() int
2a310 65 72 66 61 63 65 20 65 78 74 72 61 63 74 73 20  erface extracts 
2a320 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 0a  a UTF-16 string.
2a330 2a 2a 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65  ** in the native
2a340 20 62 79 74 65 2d 6f 72 64 65 72 20 6f 66 20 74   byte-order of t
2a350 68 65 20 68 6f 73 74 20 6d 61 63 68 69 6e 65 2e  he host machine.
2a360 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33    The.** sqlite3
2a370 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28  _value_text16be(
2a380 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 61  ) and sqlite3_va
2a390 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 29 20 69  lue_text16le() i
2a3a0 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 65 78 74  nterfaces.** ext
2a3b0 72 61 63 74 20 55 54 46 2d 31 36 20 73 74 72 69  ract UTF-16 stri
2a3c0 6e 67 73 20 61 73 20 62 69 67 2d 65 6e 64 69 61  ngs as big-endia
2a3d0 6e 20 61 6e 64 20 6c 69 74 74 6c 65 2d 65 6e 64  n and little-end
2a3e0 69 61 6e 20 72 65 73 70 65 63 74 69 76 65 6c 79  ian respectively
2a3f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
2a400 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69  te3_value_numeri
2a410 63 5f 74 79 70 65 28 29 20 69 6e 74 65 72 66 61  c_type() interfa
2a420 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 61  ce attempts to a
2a430 70 70 6c 79 0a 2a 2a 20 6e 75 6d 65 72 69 63 20  pply.** numeric 
2a440 61 66 66 69 6e 69 74 79 20 74 6f 20 74 68 65 20  affinity to the 
2a450 76 61 6c 75 65 2e 20 20 54 68 69 73 20 6d 65 61  value.  This mea
2a460 6e 73 20 74 68 61 74 20 61 6e 20 61 74 74 65 6d  ns that an attem
2a470 70 74 20 69 73 0a 2a 2a 20 6d 61 64 65 20 74 6f  pt is.** made to
2a480 20 63 6f 6e 76 65 72 74 20 74 68 65 20 76 61 6c   convert the val
2a490 75 65 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72  ue to an integer
2a4a0 20 6f 72 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69   or floating poi
2a4b0 6e 74 2e 20 20 49 66 0a 2a 2a 20 73 75 63 68 20  nt.  If.** such 
2a4c0 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20  a conversion is 
2a4d0 70 6f 73 73 69 62 6c 65 20 77 69 74 68 6f 75 74  possible without
2a4e0 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61   loss of informa
2a4f0 74 69 6f 6e 20 28 69 6e 20 6f 74 68 65 72 0a 2a  tion (in other.*
2a500 2a 20 77 6f 72 64 73 2c 20 69 66 20 74 68 65 20  * words, if the 
2a510 76 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e  value is a strin
2a520 67 20 74 68 61 74 20 6c 6f 6f 6b 73 20 6c 69 6b  g that looks lik
2a530 65 20 61 20 6e 75 6d 62 65 72 29 0a 2a 2a 20 74  e a number).** t
2a540 68 65 6e 20 74 68 65 20 63 6f 6e 76 65 72 73 69  hen the conversi
2a550 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e  on is performed.
2a560 20 20 4f 74 68 65 72 77 69 73 65 20 6e 6f 20 63    Otherwise no c
2a570 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75 72 73  onversion occurs
2a580 2e 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45  ..** The [SQLITE
2a590 5f 49 4e 54 45 47 45 52 20 7c 20 64 61 74 61 74  _INTEGER | datat
2a5a0 79 70 65 5d 20 61 66 74 65 72 20 63 6f 6e 76 65  ype] after conve
2a5b0 72 73 69 6f 6e 20 69 73 20 72 65 74 75 72 6e 65  rsion is returne
2a5c0 64 2e 0a 2a 2a 0a 2a 2a 20 50 6c 65 61 73 65 20  d..**.** Please 
2a5d0 70 61 79 20 70 61 72 74 69 63 75 6c 61 72 20 61  pay particular a
2a5e0 74 74 65 6e 74 69 6f 6e 20 74 6f 20 74 68 65 20  ttention to the 
2a5f0 66 61 63 74 20 74 68 61 74 20 74 68 65 20 70 6f  fact that the po
2a600 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64 0a 2a  inter returned.*
2a610 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f  * from [sqlite3_
2a620 76 61 6c 75 65 5f 62 6c 6f 62 28 29 5d 2c 20 5b  value_blob()], [
2a630 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2a640 78 74 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71  xt()], or.** [sq
2a650 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
2a660 31 36 28 29 5d 20 63 61 6e 20 62 65 20 69 6e 76  16()] can be inv
2a670 61 6c 69 64 61 74 65 64 20 62 79 20 61 20 73 75  alidated by a su
2a680 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f  bsequent call to
2a690 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c  .** [sqlite3_val
2a6a0 75 65 5f 62 79 74 65 73 28 29 5d 2c 20 5b 73 71  ue_bytes()], [sq
2a6b0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
2a6c0 73 31 36 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33  s16()], [sqlite3
2a6d0 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c 0a  _value_text()],.
2a6e0 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76  ** or [sqlite3_v
2a6f0 61 6c 75 65 5f 74 65 78 74 31 36 28 29 5d 2e 0a  alue_text16()]..
2a700 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
2a710 69 6e 65 73 20 6d 75 73 74 20 62 65 20 63 61 6c  ines must be cal
2a720 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d  led from the sam
2a730 65 20 74 68 72 65 61 64 20 61 73 0a 2a 2a 20 74  e thread as.** t
2a740 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  he SQL function 
2a750 74 68 61 74 20 73 75 70 70 6c 69 65 64 20 74 68  that supplied th
2a760 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  e [sqlite3_value
2a770 2a 5d 20 70 61 72 61 6d 65 74 65 72 73 2e 0a 2a  *] parameters..*
2a780 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
2a790 73 3a 0a 2a 2a 20 5b 48 31 35 31 30 33 5d 20 5b  s:.** [H15103] [
2a7a0 48 31 35 31 30 36 5d 20 5b 48 31 35 31 30 39 5d  H15106] [H15109]
2a7b0 20 5b 48 31 35 31 31 32 5d 20 5b 48 31 35 31 31   [H15112] [H1511
2a7c0 35 5d 20 5b 48 31 35 31 31 38 5d 20 5b 48 31 35  5] [H15118] [H15
2a7d0 31 32 31 5d 20 5b 48 31 35 31 32 34 5d 0a 2a 2a  121] [H15124].**
2a7e0 20 5b 48 31 35 31 32 37 5d 20 5b 48 31 35 31 33   [H15127] [H1513
2a7f0 30 5d 20 5b 48 31 35 31 33 33 5d 20 5b 48 31 35  0] [H15133] [H15
2a800 31 33 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  136].*/.SQLITE_A
2a810 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
2a820 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
2a830 62 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  b(sqlite3_value*
2a840 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
2a850 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t sqlite3_value_
2a860 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61  bytes(sqlite3_va
2a870 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  lue*);.SQLITE_AP
2a880 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61  I int sqlite3_va
2a890 6c 75 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69  lue_bytes16(sqli
2a8a0 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c  te3_value*);.SQL
2a8b0 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73  ITE_API double s
2a8c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75  qlite3_value_dou
2a8d0 62 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  ble(sqlite3_valu
2a8e0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  e*);.SQLITE_API 
2a8f0 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  int sqlite3_valu
2a900 65 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 61  e_int(sqlite3_va
2a910 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  lue*);.SQLITE_AP
2a920 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  I sqlite3_int64 
2a930 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
2a940 74 36 34 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  t64(sqlite3_valu
2a950 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  e*);.SQLITE_API 
2a960 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
2a970 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c  har *sqlite3_val
2a980 75 65 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f  ue_text(sqlite3_
2a990 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
2a9a0 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
2a9b0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2a9c0 78 74 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c  xt16(sqlite3_val
2a9d0 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ue*);.SQLITE_API
2a9e0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
2a9f0 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
2aa00 36 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  6le(sqlite3_valu
2aa10 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  e*);.SQLITE_API 
2aa20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
2aa30 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
2aa40 62 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  be(sqlite3_value
2aa50 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  *);.SQLITE_API i
2aa60 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt sqlite3_value
2aa70 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61  _type(sqlite3_va
2aa80 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  lue*);.SQLITE_AP
2aa90 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61  I int sqlite3_va
2aaa0 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65  lue_numeric_type
2aab0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
2aac0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
2aad0 46 3a 20 4f 62 74 61 69 6e 20 41 67 67 72 65 67  F: Obtain Aggreg
2aae0 61 74 65 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e  ate Function Con
2aaf0 74 65 78 74 20 7b 48 31 36 32 31 30 7d 20 3c 53  text {H16210} <S
2ab00 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  20200>.**.** The
2ab10 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
2ab20 6f 66 20 61 67 67 72 65 67 61 74 65 20 53 51 4c  of aggregate SQL
2ab30 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 20 74   functions use t
2ab40 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 61  his routine to a
2ab50 6c 6c 6f 63 61 74 65 0a 2a 2a 20 61 20 73 74 72  llocate.** a str
2ab60 75 63 74 75 72 65 20 66 6f 72 20 73 74 6f 72 69  ucture for stori
2ab70 6e 67 20 74 68 65 69 72 20 73 74 61 74 65 2e 0a  ng their state..
2ab80 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
2ab90 74 69 6d 65 20 74 68 65 20 73 71 6c 69 74 65 33  time the sqlite3
2aba0 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
2abb0 78 74 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20  xt() routine is 
2abc0 63 61 6c 6c 65 64 20 66 6f 72 20 61 0a 2a 2a 20  called for a.** 
2abd0 70 61 72 74 69 63 75 6c 61 72 20 61 67 67 72 65  particular aggre
2abe0 67 61 74 65 2c 20 53 51 4c 69 74 65 20 61 6c 6c  gate, SQLite all
2abf0 6f 63 61 74 65 73 20 6e 42 79 74 65 73 20 6f 66  ocates nBytes of
2ac00 20 6d 65 6d 6f 72 79 2c 20 7a 65 72 6f 65 73 20   memory, zeroes 
2ac10 6f 75 74 20 74 68 61 74 0a 2a 2a 20 6d 65 6d 6f  out that.** memo
2ac20 72 79 2c 20 61 6e 64 20 72 65 74 75 72 6e 73 20  ry, and returns 
2ac30 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e  a pointer to it.
2ac40 20 4f 6e 20 73 65 63 6f 6e 64 20 61 6e 64 20 73   On second and s
2ac50 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20  ubsequent calls 
2ac60 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 61 67  to.** sqlite3_ag
2ac70 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28  gregate_context(
2ac80 29 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 61  ) for the same a
2ac90 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
2aca0 6e 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68 65 20  n index,.** the 
2acb0 73 61 6d 65 20 62 75 66 66 65 72 20 69 73 20 72  same buffer is r
2acc0 65 74 75 72 6e 65 64 2e 20 54 68 65 20 69 6d 70  eturned. The imp
2acd0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
2ace0 68 65 20 61 67 67 72 65 67 61 74 65 20 63 61 6e  he aggregate can
2acf0 20 75 73 65 0a 2a 2a 20 74 68 65 20 72 65 74 75   use.** the retu
2ad00 72 6e 65 64 20 62 75 66 66 65 72 20 74 6f 20 61  rned buffer to a
2ad10 63 63 75 6d 75 6c 61 74 65 20 64 61 74 61 2e 0a  ccumulate data..
2ad20 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 61 75 74  **.** SQLite aut
2ad30 6f 6d 61 74 69 63 61 6c 6c 79 20 66 72 65 65 73  omatically frees
2ad40 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 62   the allocated b
2ad50 75 66 66 65 72 20 77 68 65 6e 20 74 68 65 20 61  uffer when the a
2ad60 67 67 72 65 67 61 74 65 0a 2a 2a 20 71 75 65 72  ggregate.** quer
2ad70 79 20 63 6f 6e 63 6c 75 64 65 73 2e 0a 2a 2a 0a  y concludes..**.
2ad80 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72  ** The first par
2ad90 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65  ameter should be
2ada0 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a   a copy of the.*
2adb0 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  * [sqlite3_conte
2adc0 78 74 20 7c 20 53 51 4c 20 66 75 6e 63 74 69 6f  xt | SQL functio
2add0 6e 20 63 6f 6e 74 65 78 74 5d 20 74 68 61 74 20  n context] that 
2ade0 69 73 20 74 68 65 20 66 69 72 73 74 20 70 61 72  is the first par
2adf0 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65  ameter.** to the
2ae00 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e   callback routin
2ae10 65 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  e that implement
2ae20 73 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20  s the aggregate 
2ae30 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
2ae40 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73  This routine mus
2ae50 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d  t be called from
2ae60 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
2ae70 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65   in which.** the
2ae80 20 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 66   aggregate SQL f
2ae90 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69  unction is runni
2aea0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ng..**.** Requir
2aeb0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32  ements:.** [H162
2aec0 31 31 5d 20 5b 48 31 36 32 31 33 5d 20 5b 48 31  11] [H16213] [H1
2aed0 36 32 31 35 5d 20 5b 48 31 36 32 31 37 5d 0a 2a  6215] [H16217].*
2aee0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
2aef0 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 67 72 65  d *sqlite3_aggre
2af00 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 73 71 6c  gate_context(sql
2af10 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69  ite3_context*, i
2af20 6e 74 20 6e 42 79 74 65 73 29 3b 0a 0a 2f 2a 0a  nt nBytes);../*.
2af30 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 55 73 65  ** CAPI3REF: Use
2af40 72 20 44 61 74 61 20 46 6f 72 20 46 75 6e 63 74  r Data For Funct
2af50 69 6f 6e 73 20 7b 48 31 36 32 34 30 7d 20 3c 53  ions {H16240} <S
2af60 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  20200>.**.** The
2af70 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61   sqlite3_user_da
2af80 74 61 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  ta() interface r
2af90 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66  eturns a copy of
2afa0 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20  .** the pointer 
2afb0 74 68 61 74 20 77 61 73 20 74 68 65 20 70 55 73  that was the pUs
2afc0 65 72 44 61 74 61 20 70 61 72 61 6d 65 74 65 72  erData parameter
2afd0 20 28 74 68 65 20 35 74 68 20 70 61 72 61 6d 65   (the 5th parame
2afe0 74 65 72 29 0a 2a 2a 20 6f 66 20 74 68 65 20 5b  ter).** of the [
2aff0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
2b000 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e  unction()].** an
2b010 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74  d [sqlite3_creat
2b020 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 20  e_function16()] 
2b030 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 6f 72  routines that or
2b040 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 72 65 67 69  iginally.** regi
2b050 73 74 65 72 65 64 20 74 68 65 20 61 70 70 6c 69  stered the appli
2b060 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 66  cation defined f
2b070 75 6e 63 74 69 6f 6e 2e 20 7b 45 4e 44 7d 0a 2a  unction. {END}.*
2b080 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2b090 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  e must be called
2b0a0 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74   from the same t
2b0b0 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a 2a  hread in which.*
2b0c0 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  * the applicatio
2b0d0 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  n-defined functi
2b0e0 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a  on is running..*
2b0f0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
2b100 73 3a 0a 2a 2a 20 5b 48 31 36 32 34 33 5d 0a 2a  s:.** [H16243].*
2b110 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
2b120 64 20 2a 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  d *sqlite3_user_
2b130 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e  data(sqlite3_con
2b140 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  text*);../*.** C
2b150 41 50 49 33 52 45 46 3a 20 44 61 74 61 62 61 73  API3REF: Databas
2b160 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 46 6f 72  e Connection For
2b170 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31 36 32   Functions {H162
2b180 35 30 7d 20 3c 53 36 30 36 30 30 3e 3c 53 32 30  50} <S60600><S20
2b190 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  200>.**.** The s
2b1a0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
2b1b0 62 5f 68 61 6e 64 6c 65 28 29 20 69 6e 74 65 72  b_handle() inter
2b1c0 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 63  face returns a c
2b1d0 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 70 6f  opy of.** the po
2b1e0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 64 61  inter to the [da
2b1f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2b200 6e 5d 20 28 74 68 65 20 31 73 74 20 70 61 72 61  n] (the 1st para
2b210 6d 65 74 65 72 29 0a 2a 2a 20 6f 66 20 74 68 65  meter).** of the
2b220 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
2b230 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20  _function()].** 
2b240 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  and [sqlite3_cre
2b250 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29  ate_function16()
2b260 5d 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20  ] routines that 
2b270 6f 72 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 72 65  originally.** re
2b280 67 69 73 74 65 72 65 64 20 74 68 65 20 61 70 70  gistered the app
2b290 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64  lication defined
2b2a0 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
2b2b0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
2b2c0 2a 20 5b 48 31 36 32 35 33 5d 0a 2a 2f 0a 53 51  * [H16253].*/.SQ
2b2d0 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33  LITE_API sqlite3
2b2e0 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   *sqlite3_contex
2b2f0 74 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69  t_db_handle(sqli
2b300 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 0a  te3_context*);..
2b310 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
2b320 46 75 6e 63 74 69 6f 6e 20 41 75 78 69 6c 69 61  Function Auxilia
2b330 72 79 20 44 61 74 61 20 7b 48 31 36 32 37 30 7d  ry Data {H16270}
2b340 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S20200>.**.** 
2b350 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77  The following tw
2b360 6f 20 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20  o functions may 
2b370 62 65 20 75 73 65 64 20 62 79 20 73 63 61 6c 61  be used by scala
2b380 72 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20  r SQL functions 
2b390 74 6f 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 20  to.** associate 
2b3a0 6d 65 74 61 64 61 74 61 20 77 69 74 68 20 61 72  metadata with ar
2b3b0 67 75 6d 65 6e 74 20 76 61 6c 75 65 73 2e 20 49  gument values. I
2b3c0 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65  f the same value
2b3d0 20 69 73 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a   is passed to.**
2b3e0 20 6d 75 6c 74 69 70 6c 65 20 69 6e 76 6f 63 61   multiple invoca
2b3f0 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d  tions of the sam
2b400 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 64  e SQL function d
2b410 75 72 69 6e 67 20 71 75 65 72 79 20 65 78 65 63  uring query exec
2b420 75 74 69 6f 6e 2c 20 75 6e 64 65 72 0a 2a 2a 20  ution, under.** 
2b430 73 6f 6d 65 20 63 69 72 63 75 6d 73 74 61 6e 63  some circumstanc
2b440 65 73 20 74 68 65 20 61 73 73 6f 63 69 61 74 65  es the associate
2b450 64 20 6d 65 74 61 64 61 74 61 20 6d 61 79 20 62  d metadata may b
2b460 65 20 70 72 65 73 65 72 76 65 64 2e 20 54 68 69  e preserved. Thi
2b470 73 20 6d 61 79 0a 2a 2a 20 62 65 20 75 73 65 64  s may.** be used
2b480 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74  , for example, t
2b490 6f 20 61 64 64 20 61 20 72 65 67 75 6c 61 72 2d  o add a regular-
2b4a0 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 74 63 68  expression match
2b4b0 69 6e 67 20 73 63 61 6c 61 72 0a 2a 2a 20 66 75  ing scalar.** fu
2b4c0 6e 63 74 69 6f 6e 2e 20 54 68 65 20 63 6f 6d 70  nction. The comp
2b4d0 69 6c 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20  iled version of 
2b4e0 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 70 72  the regular expr
2b4f0 65 73 73 69 6f 6e 20 69 73 20 73 74 6f 72 65 64  ession is stored
2b500 20 61 73 0a 2a 2a 20 6d 65 74 61 64 61 74 61 20   as.** metadata 
2b510 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
2b520 74 68 65 20 53 51 4c 20 76 61 6c 75 65 20 70 61  the SQL value pa
2b530 73 73 65 64 20 61 73 20 74 68 65 20 72 65 67 75  ssed as the regu
2b540 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a  lar expression.*
2b550 2a 20 70 61 74 74 65 72 6e 2e 20 20 54 68 65 20  * pattern.  The 
2b560 63 6f 6d 70 69 6c 65 64 20 72 65 67 75 6c 61 72  compiled regular
2b570 20 65 78 70 72 65 73 73 69 6f 6e 20 63 61 6e 20   expression can 
2b580 62 65 20 72 65 75 73 65 64 20 6f 6e 20 6d 75 6c  be reused on mul
2b590 74 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63 61 74  tiple.** invocat
2b5a0 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65  ions of the same
2b5b0 20 66 75 6e 63 74 69 6f 6e 20 73 6f 20 74 68 61   function so tha
2b5c0 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70  t the original p
2b5d0 61 74 74 65 72 6e 20 73 74 72 69 6e 67 0a 2a 2a  attern string.**
2b5e0 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
2b5f0 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20  o be recompiled 
2b600 6f 6e 20 65 61 63 68 20 69 6e 76 6f 63 61 74 69  on each invocati
2b610 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  on..**.** The sq
2b620 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74  lite3_get_auxdat
2b630 61 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65  a() interface re
2b640 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
2b650 74 6f 20 74 68 65 20 6d 65 74 61 64 61 74 61 0a  to the metadata.
2b660 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 62 79  ** associated by
2b670 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 65 74   the sqlite3_set
2b680 5f 61 75 78 64 61 74 61 28 29 20 66 75 6e 63 74  _auxdata() funct
2b690 69 6f 6e 20 77 69 74 68 20 74 68 65 20 4e 74 68  ion with the Nth
2b6a0 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 76 61 6c   argument.** val
2b6b0 75 65 20 74 6f 20 74 68 65 20 61 70 70 6c 69 63  ue to the applic
2b6c0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75  ation-defined fu
2b6d0 6e 63 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 6d 65  nction. If no me
2b6e0 74 61 64 61 74 61 20 68 61 73 20 62 65 65 6e 20  tadata has been 
2b6f0 65 76 65 72 0a 2a 2a 20 62 65 65 6e 20 73 65 74  ever.** been set
2b700 20 66 6f 72 20 74 68 65 20 4e 74 68 20 61 72 67   for the Nth arg
2b710 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 75 6e  ument of the fun
2b720 63 74 69 6f 6e 2c 20 6f 72 20 69 66 20 74 68 65  ction, or if the
2b730 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a   corresponding.*
2b740 2a 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d  * function param
2b750 65 74 65 72 20 68 61 73 20 63 68 61 6e 67 65 64  eter has changed
2b760 20 73 69 6e 63 65 20 74 68 65 20 6d 65 74 61 2d   since the meta-
2b770 64 61 74 61 20 77 61 73 20 73 65 74 2c 0a 2a 2a  data was set,.**
2b780 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 67 65   then sqlite3_ge
2b790 74 5f 61 75 78 64 61 74 61 28 29 20 72 65 74 75  t_auxdata() retu
2b7a0 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  rns a NULL point
2b7b0 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  er..**.** The sq
2b7c0 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74  lite3_set_auxdat
2b7d0 61 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 61  a() interface sa
2b7e0 76 65 73 20 74 68 65 20 6d 65 74 61 64 61 74 61  ves the metadata
2b7f0 0a 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  .** pointed to b
2b800 79 20 69 74 73 20 33 72 64 20 70 61 72 61 6d 65  y its 3rd parame
2b810 74 65 72 20 61 73 20 74 68 65 20 6d 65 74 61 64  ter as the metad
2b820 61 74 61 20 66 6f 72 20 74 68 65 20 4e 2d 74 68  ata for the N-th
2b830 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6f 66 20  .** argument of 
2b840 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  the application-
2b850 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
2b860 2e 20 20 53 75 62 73 65 71 75 65 6e 74 0a 2a 2a  .  Subsequent.**
2b870 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65   calls to sqlite
2b880 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29 20  3_get_auxdata() 
2b890 6d 69 67 68 74 20 72 65 74 75 72 6e 20 74 68 69  might return thi
2b8a0 73 20 64 61 74 61 2c 20 69 66 20 69 74 20 68 61  s data, if it ha
2b8b0 73 0a 2a 2a 20 6e 6f 74 20 62 65 65 6e 20 64 65  s.** not been de
2b8c0 73 74 72 6f 79 65 64 2e 0a 2a 2a 20 49 66 20 69  stroyed..** If i
2b8d0 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 53  t is not NULL, S
2b8e0 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76 6f 6b  QLite will invok
2b8f0 65 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72  e the destructor
2b900 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 67 69 76  .** function giv
2b910 65 6e 20 62 79 20 74 68 65 20 34 74 68 20 70 61  en by the 4th pa
2b920 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
2b930 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29  e3_set_auxdata()
2b940 20 6f 6e 0a 2a 2a 20 74 68 65 20 6d 65 74 61 64   on.** the metad
2b950 61 74 61 20 77 68 65 6e 20 74 68 65 20 63 6f 72  ata when the cor
2b960 72 65 73 70 6f 6e 64 69 6e 67 20 66 75 6e 63 74  responding funct
2b970 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 63 68  ion parameter ch
2b980 61 6e 67 65 73 0a 2a 2a 20 6f 72 20 77 68 65 6e  anges.** or when
2b990 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
2b9a0 6e 74 20 63 6f 6d 70 6c 65 74 65 73 2c 20 77 68  nt completes, wh
2b9b0 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69  ichever comes fi
2b9c0 72 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  rst..**.** SQLit
2b9d0 65 20 69 73 20 66 72 65 65 20 74 6f 20 63 61 6c  e is free to cal
2b9e0 6c 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72  l the destructor
2b9f0 20 61 6e 64 20 64 72 6f 70 20 6d 65 74 61 64 61   and drop metada
2ba00 74 61 20 6f 6e 20 61 6e 79 0a 2a 2a 20 70 61 72  ta on any.** par
2ba10 61 6d 65 74 65 72 20 6f 66 20 61 6e 79 20 66 75  ameter of any fu
2ba20 6e 63 74 69 6f 6e 20 61 74 20 61 6e 79 20 74 69  nction at any ti
2ba30 6d 65 2e 20 20 54 68 65 20 6f 6e 6c 79 20 67 75  me.  The only gu
2ba40 61 72 61 6e 74 65 65 20 69 73 20 74 68 61 74 0a  arantee is that.
2ba50 2a 2a 20 74 68 65 20 64 65 73 74 72 75 63 74 6f  ** the destructo
2ba60 72 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64  r will be called
2ba70 20 62 65 66 6f 72 65 20 74 68 65 20 6d 65 74 61   before the meta
2ba80 64 61 74 61 20 69 73 20 64 72 6f 70 70 65 64 2e  data is dropped.
2ba90 0a 2a 2a 0a 2a 2a 20 49 6e 20 70 72 61 63 74 69  .**.** In practi
2baa0 63 65 2c 20 6d 65 74 61 64 61 74 61 20 69 73 20  ce, metadata is 
2bab0 70 72 65 73 65 72 76 65 64 20 62 65 74 77 65 65  preserved betwee
2bac0 6e 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73  n function calls
2bad0 20 66 6f 72 0a 2a 2a 20 65 78 70 72 65 73 73 69   for.** expressi
2bae0 6f 6e 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e  ons that are con
2baf0 73 74 61 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65  stant at compile
2bb00 20 74 69 6d 65 2e 20 54 68 69 73 20 69 6e 63 6c   time. This incl
2bb10 75 64 65 73 20 6c 69 74 65 72 61 6c 0a 2a 2a 20  udes literal.** 
2bb20 76 61 6c 75 65 73 20 61 6e 64 20 53 51 4c 20 76  values and SQL v
2bb30 61 72 69 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  ariables..**.** 
2bb40 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d  These routines m
2bb50 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72  ust be called fr
2bb60 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  om the same thre
2bb70 61 64 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74  ad in which.** t
2bb80 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  he SQL function 
2bb90 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a  is running..**.*
2bba0 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
2bbb0 2a 2a 20 5b 48 31 36 32 37 32 5d 20 5b 48 31 36  ** [H16272] [H16
2bbc0 32 37 34 5d 20 5b 48 31 36 32 37 36 5d 20 5b 48  274] [H16276] [H
2bbd0 31 36 32 37 37 5d 20 5b 48 31 36 32 37 38 5d 20  16277] [H16278] 
2bbe0 5b 48 31 36 32 37 39 5d 0a 2a 2f 0a 53 51 4c 49  [H16279].*/.SQLI
2bbf0 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
2bc00 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61  ite3_get_auxdata
2bc10 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2bc20 2a 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54  *, int N);.SQLIT
2bc30 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
2bc40 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 73  e3_set_auxdata(s
2bc50 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
2bc60 20 69 6e 74 20 4e 2c 20 76 6f 69 64 2a 2c 20 76   int N, void*, v
2bc70 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b  oid (*)(void*));
2bc80 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  .../*.** CAPI3RE
2bc90 46 3a 20 43 6f 6e 73 74 61 6e 74 73 20 44 65 66  F: Constants Def
2bca0 69 6e 69 6e 67 20 53 70 65 63 69 61 6c 20 44 65  ining Special De
2bcb0 73 74 72 75 63 74 6f 72 20 42 65 68 61 76 69 6f  structor Behavio
2bcc0 72 20 7b 48 31 30 32 38 30 7d 20 3c 53 33 30 31  r {H10280} <S301
2bcd0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  00>.**.** These 
2bce0 61 72 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75  are special valu
2bcf0 65 73 20 66 6f 72 20 74 68 65 20 64 65 73 74 72  es for the destr
2bd00 75 63 74 6f 72 20 74 68 61 74 20 69 73 20 70 61  uctor that is pa
2bd10 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 0a 2a  ssed in as the.*
2bd20 2a 20 66 69 6e 61 6c 20 61 72 67 75 6d 65 6e 74  * final argument
2bd30 20 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69 6b   to routines lik
2bd40 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  e [sqlite3_resul
2bd50 74 5f 62 6c 6f 62 28 29 5d 2e 20 20 49 66 20 74  t_blob()].  If t
2bd60 68 65 20 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a  he destructor.**
2bd70 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 51 4c   argument is SQL
2bd80 49 54 45 5f 53 54 41 54 49 43 2c 20 69 74 20 6d  ITE_STATIC, it m
2bd90 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f  eans that the co
2bda0 6e 74 65 6e 74 20 70 6f 69 6e 74 65 72 20 69 73  ntent pointer is
2bdb0 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 6e 64   constant.** and
2bdc0 20 77 69 6c 6c 20 6e 65 76 65 72 20 63 68 61 6e   will never chan
2bdd0 67 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74  ge.  It does not
2bde0 20 6e 65 65 64 20 74 6f 20 62 65 20 64 65 73 74   need to be dest
2bdf0 72 6f 79 65 64 2e 20 20 54 68 65 0a 2a 2a 20 53  royed.  The.** S
2be00 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20  QLITE_TRANSIENT 
2be10 76 61 6c 75 65 20 6d 65 61 6e 73 20 74 68 61 74  value means that
2be20 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 6c   the content wil
2be30 6c 20 6c 69 6b 65 6c 79 20 63 68 61 6e 67 65 20  l likely change 
2be40 69 6e 0a 2a 2a 20 74 68 65 20 6e 65 61 72 20 66  in.** the near f
2be50 75 74 75 72 65 20 61 6e 64 20 74 68 61 74 20 53  uture and that S
2be60 51 4c 69 74 65 20 73 68 6f 75 6c 64 20 6d 61 6b  QLite should mak
2be70 65 20 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74  e its own privat
2be80 65 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65  e copy of.** the
2be90 20 63 6f 6e 74 65 6e 74 20 62 65 66 6f 72 65 20   content before 
2bea0 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a  returning..**.**
2beb0 20 54 68 65 20 74 79 70 65 64 65 66 20 69 73 20   The typedef is 
2bec0 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77 6f 72  necessary to wor
2bed0 6b 20 61 72 6f 75 6e 64 20 70 72 6f 62 6c 65 6d  k around problem
2bee0 73 20 69 6e 20 63 65 72 74 61 69 6e 0a 2a 2a 20  s in certain.** 
2bef0 43 2b 2b 20 63 6f 6d 70 69 6c 65 72 73 2e 20 20  C++ compilers.  
2bf00 53 65 65 20 74 69 63 6b 65 74 20 23 32 31 39 31  See ticket #2191
2bf10 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 76 6f 69  ..*/.typedef voi
2bf20 64 20 28 2a 73 71 6c 69 74 65 33 5f 64 65 73 74  d (*sqlite3_dest
2bf30 72 75 63 74 6f 72 5f 74 79 70 65 29 28 76 6f 69  ructor_type)(voi
2bf40 64 2a 29 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c  d*);.#define SQL
2bf50 49 54 45 5f 53 54 41 54 49 43 20 20 20 20 20 20  ITE_STATIC      
2bf60 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75  ((sqlite3_destru
2bf70 63 74 6f 72 5f 74 79 70 65 29 30 29 0a 23 64 65  ctor_type)0).#de
2bf80 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 52 41 4e  fine SQLITE_TRAN
2bf90 53 49 45 4e 54 20 20 20 28 28 73 71 6c 69 74 65  SIENT   ((sqlite
2bfa0 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70  3_destructor_typ
2bfb0 65 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  e)-1)../*.** CAP
2bfc0 49 33 52 45 46 3a 20 53 65 74 74 69 6e 67 20 54  I3REF: Setting T
2bfd0 68 65 20 52 65 73 75 6c 74 20 4f 66 20 41 6e 20  he Result Of An 
2bfe0 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20 7b 48 31  SQL Function {H1
2bff0 36 34 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a  6400} <S20200>.*
2c000 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
2c010 6e 65 73 20 61 72 65 20 75 73 65 64 20 62 79 20  nes are used by 
2c020 74 68 65 20 78 46 75 6e 63 20 6f 72 20 78 46 69  the xFunc or xFi
2c030 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 74 68  nal callbacks th
2c040 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20  at.** implement 
2c050 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e  SQL functions an
2c060 64 20 61 67 67 72 65 67 61 74 65 73 2e 20 20 53  d aggregates.  S
2c070 65 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63  ee.** [sqlite3_c
2c080 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29  reate_function()
2c090 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63  ] and [sqlite3_c
2c0a0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36  reate_function16
2c0b0 28 29 5d 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74  ()].** for addit
2c0c0 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
2c0d0 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66  n..**.** These f
2c0e0 75 6e 63 74 69 6f 6e 73 20 77 6f 72 6b 20 76 65  unctions work ve
2c0f0 72 79 20 6d 75 63 68 20 6c 69 6b 65 20 74 68 65  ry much like the
2c100 20 5b 70 61 72 61 6d 65 74 65 72 20 62 69 6e 64   [parameter bind
2c110 69 6e 67 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a  ing] family of.*
2c120 2a 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64  * functions used
2c130 20 74 6f 20 62 69 6e 64 20 76 61 6c 75 65 73 20   to bind values 
2c140 74 6f 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65  to host paramete
2c150 72 73 20 69 6e 20 70 72 65 70 61 72 65 64 20 73  rs in prepared s
2c160 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 52 65  tatements..** Re
2c170 66 65 72 20 74 6f 20 74 68 65 20 5b 53 51 4c 20  fer to the [SQL 
2c180 70 61 72 61 6d 65 74 65 72 5d 20 64 6f 63 75 6d  parameter] docum
2c190 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64  entation for add
2c1a0 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
2c1b0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ion..**.** The s
2c1c0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
2c1d0 6f 62 28 29 20 69 6e 74 65 72 66 61 63 65 20 73  ob() interface s
2c1e0 65 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 66  ets the result f
2c1f0 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63  rom.** an applic
2c200 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75  ation-defined fu
2c210 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65  nction to be the
2c220 20 42 4c 4f 42 20 77 68 6f 73 65 20 63 6f 6e 74   BLOB whose cont
2c230 65 6e 74 20 69 73 20 70 6f 69 6e 74 65 64 0a 2a  ent is pointed.*
2c240 2a 20 74 6f 20 62 79 20 74 68 65 20 73 65 63 6f  * to by the seco
2c250 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64  nd parameter and
2c260 20 77 68 69 63 68 20 69 73 20 4e 20 62 79 74 65   which is N byte
2c270 73 20 6c 6f 6e 67 20 77 68 65 72 65 20 4e 20 69  s long where N i
2c280 73 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 70  s the.** third p
2c290 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20  arameter..**.** 
2c2a0 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  The sqlite3_resu
2c2b0 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 69 6e  lt_zeroblob() in
2c2c0 74 65 72 66 61 63 65 73 20 73 65 74 20 74 68 65  terfaces set the
2c2d0 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68   result of.** th
2c2e0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  e application-de
2c2f0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74  fined function t
2c300 6f 20 62 65 20 61 20 42 4c 4f 42 20 63 6f 6e 74  o be a BLOB cont
2c310 61 69 6e 69 6e 67 20 61 6c 6c 20 7a 65 72 6f 0a  aining all zero.
2c320 2a 2a 20 62 79 74 65 73 20 61 6e 64 20 4e 20 62  ** bytes and N b
2c330 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 77 68  ytes in size, wh
2c340 65 72 65 20 4e 20 69 73 20 74 68 65 20 76 61 6c  ere N is the val
2c350 75 65 20 6f 66 20 74 68 65 20 32 6e 64 20 70 61  ue of the 2nd pa
2c360 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  rameter..**.** T
2c370 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
2c380 74 5f 64 6f 75 62 6c 65 28 29 20 69 6e 74 65 72  t_double() inter
2c390 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65  face sets the re
2c3a0 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20  sult from.** an 
2c3b0 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
2c3c0 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  ned function to 
2c3d0 62 65 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f  be a floating po
2c3e0 69 6e 74 20 76 61 6c 75 65 20 73 70 65 63 69 66  int value specif
2c3f0 69 65 64 0a 2a 2a 20 62 79 20 69 74 73 20 32 6e  ied.** by its 2n
2c400 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a  d argument..**.*
2c410 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65  * The sqlite3_re
2c420 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 61 6e 64  sult_error() and
2c430 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2c440 65 72 72 6f 72 31 36 28 29 20 66 75 6e 63 74 69  error16() functi
2c450 6f 6e 73 0a 2a 2a 20 63 61 75 73 65 20 74 68 65  ons.** cause the
2c460 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 53 51 4c   implemented SQL
2c470 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 72   function to thr
2c480 6f 77 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 2e  ow an exception.
2c490 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20  .** SQLite uses 
2c4a0 74 68 65 20 73 74 72 69 6e 67 20 70 6f 69 6e 74  the string point
2c4b0 65 64 20 74 6f 20 62 79 20 74 68 65 0a 2a 2a 20  ed to by the.** 
2c4c0 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 6f 66  2nd parameter of
2c4d0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2c4e0 65 72 72 6f 72 28 29 20 6f 72 20 73 71 6c 69 74  error() or sqlit
2c4f0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31  e3_result_error1
2c500 36 28 29 0a 2a 2a 20 61 73 20 74 68 65 20 74 65  6().** as the te
2c510 78 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d  xt of an error m
2c520 65 73 73 61 67 65 2e 20 20 53 51 4c 69 74 65 20  essage.  SQLite 
2c530 69 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 65  interprets the e
2c540 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20  rror.** message 
2c550 73 74 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69  string from sqli
2c560 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2c570 28 29 20 61 73 20 55 54 46 2d 38 2e 20 53 51 4c  () as UTF-8. SQL
2c580 69 74 65 0a 2a 2a 20 69 6e 74 65 72 70 72 65 74  ite.** interpret
2c590 73 20 74 68 65 20 73 74 72 69 6e 67 20 66 72 6f  s the string fro
2c5a0 6d 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  m sqlite3_result
2c5b0 5f 65 72 72 6f 72 31 36 28 29 20 61 73 20 55 54  _error16() as UT
2c5c0 46 2d 31 36 20 69 6e 20 6e 61 74 69 76 65 0a 2a  F-16 in native.*
2c5d0 2a 20 62 79 74 65 20 6f 72 64 65 72 2e 20 20 49  * byte order.  I
2c5e0 66 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61  f the third para
2c5f0 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33  meter to sqlite3
2c600 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 0a  _result_error().
2c610 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65  ** or sqlite3_re
2c620 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 69  sult_error16() i
2c630 73 20 6e 65 67 61 74 69 76 65 20 74 68 65 6e 20  s negative then 
2c640 53 51 4c 69 74 65 20 74 61 6b 65 73 20 61 73 20  SQLite takes as 
2c650 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73  the error.** mes
2c660 73 61 67 65 20 61 6c 6c 20 74 65 78 74 20 75 70  sage all text up
2c670 20 74 68 72 6f 75 67 68 20 74 68 65 20 66 69 72   through the fir
2c680 73 74 20 7a 65 72 6f 20 63 68 61 72 61 63 74 65  st zero characte
2c690 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 68 69  r..** If the thi
2c6a0 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  rd parameter to 
2c6b0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2c6c0 72 72 6f 72 28 29 20 6f 72 0a 2a 2a 20 73 71 6c  rror() or.** sql
2c6d0 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
2c6e0 72 31 36 28 29 20 69 73 20 6e 6f 6e 2d 6e 65 67  r16() is non-neg
2c6f0 61 74 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74  ative then SQLit
2c700 65 20 74 61 6b 65 73 20 74 68 61 74 20 6d 61 6e  e takes that man
2c710 79 0a 2a 2a 20 62 79 74 65 73 20 28 6e 6f 74 20  y.** bytes (not 
2c720 63 68 61 72 61 63 74 65 72 73 29 20 66 72 6f 6d  characters) from
2c730 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74   the 2nd paramet
2c740 65 72 20 61 73 20 74 68 65 20 65 72 72 6f 72 20  er as the error 
2c750 6d 65 73 73 61 67 65 2e 0a 2a 2a 20 54 68 65 20  message..** The 
2c760 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2c770 72 72 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74  rror() and sqlit
2c780 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31  e3_result_error1
2c790 36 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20  6().** routines 
2c7a0 6d 61 6b 65 20 61 20 70 72 69 76 61 74 65 20 63  make a private c
2c7b0 6f 70 79 20 6f 66 20 74 68 65 20 65 72 72 6f 72  opy of the error
2c7c0 20 6d 65 73 73 61 67 65 20 74 65 78 74 20 62 65   message text be
2c7d0 66 6f 72 65 0a 2a 2a 20 74 68 65 79 20 72 65 74  fore.** they ret
2c7e0 75 72 6e 2e 20 20 48 65 6e 63 65 2c 20 74 68 65  urn.  Hence, the
2c7f0 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
2c800 6e 20 63 61 6e 20 64 65 61 6c 6c 6f 63 61 74 65  n can deallocate
2c810 20 6f 72 0a 2a 2a 20 6d 6f 64 69 66 79 20 74 68   or.** modify th
2c820 65 20 74 65 78 74 20 61 66 74 65 72 20 74 68 65  e text after the
2c830 79 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74  y return without
2c840 20 68 61 72 6d 2e 0a 2a 2a 20 54 68 65 20 73 71   harm..** The sq
2c850 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2c860 6f 72 5f 63 6f 64 65 28 29 20 66 75 6e 63 74 69  or_code() functi
2c870 6f 6e 20 63 68 61 6e 67 65 73 20 74 68 65 20 65  on changes the e
2c880 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 72 65 74  rror code.** ret
2c890 75 72 6e 65 64 20 62 79 20 53 51 4c 69 74 65 20  urned by SQLite 
2c8a0 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61  as a result of a
2c8b0 6e 20 65 72 72 6f 72 20 69 6e 20 61 20 66 75 6e  n error in a fun
2c8c0 63 74 69 6f 6e 2e 20 20 42 79 20 64 65 66 61 75  ction.  By defau
2c8d0 6c 74 2c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72  lt,.** the error
2c8e0 20 63 6f 64 65 20 69 73 20 53 51 4c 49 54 45 5f   code is SQLITE_
2c8f0 45 52 52 4f 52 2e 20 20 41 20 73 75 62 73 65 71  ERROR.  A subseq
2c900 75 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c  uent call to sql
2c910 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
2c920 72 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65  r().** or sqlite
2c930 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36  3_result_error16
2c940 28 29 20 72 65 73 65 74 73 20 74 68 65 20 65 72  () resets the er
2c950 72 6f 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49  ror code to SQLI
2c960 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20  TE_ERROR..**.** 
2c970 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  The sqlite3_resu
2c980 6c 74 5f 74 6f 6f 62 69 67 28 29 20 69 6e 74 65  lt_toobig() inte
2c990 72 66 61 63 65 20 63 61 75 73 65 73 20 53 51 4c  rface causes SQL
2c9a0 69 74 65 20 74 6f 20 74 68 72 6f 77 20 61 6e 20  ite to throw an 
2c9b0 65 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 61 74  error.** indicat
2c9c0 69 6e 67 20 74 68 61 74 20 61 20 73 74 72 69 6e  ing that a strin
2c9d0 67 20 6f 72 20 42 4c 4f 42 20 69 73 20 74 6f 20  g or BLOB is to 
2c9e0 6c 6f 6e 67 20 74 6f 20 72 65 70 72 65 73 65 6e  long to represen
2c9f0 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  t..**.** The sql
2ca00 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 6f 6d 65  ite3_result_nome
2ca10 6d 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61  m() interface ca
2ca20 75 73 65 73 20 53 51 4c 69 74 65 20 74 6f 20 74  uses SQLite to t
2ca30 68 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a 2a 2a  hrow an error.**
2ca40 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74   indicating that
2ca50 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
2ca60 74 69 6f 6e 20 66 61 69 6c 65 64 2e 0a 2a 2a 0a  tion failed..**.
2ca70 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72  ** The sqlite3_r
2ca80 65 73 75 6c 74 5f 69 6e 74 28 29 20 69 6e 74 65  esult_int() inte
2ca90 72 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72  rface sets the r
2caa0 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f  eturn value.** o
2cab0 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  f the applicatio
2cac0 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  n-defined functi
2cad0 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 33 32 2d  on to be the 32-
2cae0 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67  bit signed integ
2caf0 65 72 0a 2a 2a 20 76 61 6c 75 65 20 67 69 76 65  er.** value give
2cb00 6e 20 69 6e 20 74 68 65 20 32 6e 64 20 61 72 67  n in the 2nd arg
2cb10 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 71  ument..** The sq
2cb20 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
2cb30 36 34 28 29 20 69 6e 74 65 72 66 61 63 65 20 73  64() interface s
2cb40 65 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 76  ets the return v
2cb50 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61  alue.** of the a
2cb60 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
2cb70 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62  ed function to b
2cb80 65 20 74 68 65 20 36 34 2d 62 69 74 20 73 69 67  e the 64-bit sig
2cb90 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76  ned integer.** v
2cba0 61 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68  alue given in th
2cbb0 65 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a  e 2nd argument..
2cbc0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
2cbd0 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 29 20  3_result_null() 
2cbe0 69 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74  interface sets t
2cbf0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a  he return value.
2cc00 2a 2a 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63  ** of the applic
2cc10 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75  ation-defined fu
2cc20 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 4e 55 4c  nction to be NUL
2cc30 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  L..**.** The sql
2cc40 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2cc50 28 29 2c 20 73 71 6c 69 74 65 33 5f 72 65 73 75  (), sqlite3_resu
2cc60 6c 74 5f 74 65 78 74 31 36 28 29 2c 0a 2a 2a 20  lt_text16(),.** 
2cc70 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
2cc80 65 78 74 31 36 6c 65 28 29 2c 20 61 6e 64 20 73  ext16le(), and s
2cc90 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
2cca0 78 74 31 36 62 65 28 29 20 69 6e 74 65 72 66 61  xt16be() interfa
2ccb0 63 65 73 0a 2a 2a 20 73 65 74 20 74 68 65 20 72  ces.** set the r
2ccc0 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74  eturn value of t
2ccd0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  he application-d
2cce0 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20  efined function 
2ccf0 74 6f 20 62 65 0a 2a 2a 20 61 20 74 65 78 74 20  to be.** a text 
2cd00 73 74 72 69 6e 67 20 77 68 69 63 68 20 69 73 20  string which is 
2cd10 72 65 70 72 65 73 65 6e 74 65 64 20 61 73 20 55  represented as U
2cd20 54 46 2d 38 2c 20 55 54 46 2d 31 36 20 6e 61 74  TF-8, UTF-16 nat
2cd30 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 2c 0a  ive byte order,.
2cd40 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65  ** UTF-16 little
2cd50 20 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54 46 2d   endian, or UTF-
2cd60 31 36 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 72  16 big endian, r
2cd70 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20  espectively..** 
2cd80 53 51 4c 69 74 65 20 74 61 6b 65 73 20 74 68 65  SQLite takes the
2cd90 20 74 65 78 74 20 72 65 73 75 6c 74 20 66 72 6f   text result fro
2cda0 6d 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  m the applicatio
2cdb0 6e 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 32 6e  n from.** the 2n
2cdc0 64 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74  d parameter of t
2cdd0 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
2cde0 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63  t_text* interfac
2cdf0 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 33 72  es..** If the 3r
2ce00 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  d parameter to t
2ce10 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
2ce20 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63  t_text* interfac
2ce30 65 73 0a 2a 2a 20 69 73 20 6e 65 67 61 74 69 76  es.** is negativ
2ce40 65 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74  e, then SQLite t
2ce50 61 6b 65 73 20 72 65 73 75 6c 74 20 74 65 78 74  akes result text
2ce60 20 66 72 6f 6d 20 74 68 65 20 32 6e 64 20 70 61   from the 2nd pa
2ce70 72 61 6d 65 74 65 72 0a 2a 2a 20 74 68 72 6f 75  rameter.** throu
2ce80 67 68 20 74 68 65 20 66 69 72 73 74 20 7a 65 72  gh the first zer
2ce90 6f 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20  o character..** 
2cea0 49 66 20 74 68 65 20 33 72 64 20 70 61 72 61 6d  If the 3rd param
2ceb0 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69  eter to the sqli
2cec0 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a  te3_result_text*
2ced0 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 69   interfaces.** i
2cee0 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20  s non-negative, 
2cef0 74 68 65 6e 20 61 73 20 6d 61 6e 79 20 62 79 74  then as many byt
2cf00 65 73 20 28 6e 6f 74 20 63 68 61 72 61 63 74 65  es (not characte
2cf10 72 73 29 20 6f 66 20 74 68 65 20 74 65 78 74 0a  rs) of the text.
2cf20 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ** pointed to by
2cf30 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74   the 2nd paramet
2cf40 65 72 20 61 72 65 20 74 61 6b 65 6e 20 61 73 20  er are taken as 
2cf50 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  the application-
2cf60 64 65 66 69 6e 65 64 0a 2a 2a 20 66 75 6e 63 74  defined.** funct
2cf70 69 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 49  ion result..** I
2cf80 66 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65  f the 4th parame
2cf90 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74  ter to the sqlit
2cfa0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20  e3_result_text* 
2cfb0 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 6f 72  interfaces.** or
2cfc0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2cfd0 62 6c 6f 62 20 69 73 20 61 20 6e 6f 6e 2d 4e 55  blob is a non-NU
2cfe0 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e  LL pointer, then
2cff0 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68   SQLite calls th
2d000 61 74 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61  at.** function a
2d010 73 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72  s the destructor
2d020 20 6f 6e 20 74 68 65 20 74 65 78 74 20 6f 72 20   on the text or 
2d030 42 4c 4f 42 20 72 65 73 75 6c 74 20 77 68 65 6e  BLOB result when
2d040 20 69 74 20 68 61 73 0a 2a 2a 20 66 69 6e 69 73   it has.** finis
2d050 68 65 64 20 75 73 69 6e 67 20 74 68 61 74 20 72  hed using that r
2d060 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65  esult..** If the
2d070 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   4th parameter t
2d080 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65  o the sqlite3_re
2d090 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72  sult_text* inter
2d0a0 66 61 63 65 73 20 6f 72 20 74 6f 0a 2a 2a 20 73  faces or to.** s
2d0b0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
2d0c0 6f 62 20 69 73 20 74 68 65 20 73 70 65 63 69 61  ob is the specia
2d0d0 6c 20 63 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54  l constant SQLIT
2d0e0 45 5f 53 54 41 54 49 43 2c 20 74 68 65 6e 20 53  E_STATIC, then S
2d0f0 51 4c 69 74 65 0a 2a 2a 20 61 73 73 75 6d 65 73  QLite.** assumes
2d100 20 74 68 61 74 20 74 68 65 20 74 65 78 74 20 6f   that the text o
2d110 72 20 42 4c 4f 42 20 72 65 73 75 6c 74 20 69 73  r BLOB result is
2d120 20 69 6e 20 63 6f 6e 73 74 61 6e 74 20 73 70 61   in constant spa
2d130 63 65 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 0a  ce and does not.
2d140 2a 2a 20 63 6f 70 79 20 74 68 65 20 63 6f 6e 74  ** copy the cont
2d150 65 6e 74 20 6f 66 20 74 68 65 20 70 61 72 61 6d  ent of the param
2d160 65 74 65 72 20 6e 6f 72 20 63 61 6c 6c 20 61 20  eter nor call a 
2d170 64 65 73 74 72 75 63 74 6f 72 20 6f 6e 20 74 68  destructor on th
2d180 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 77 68 65  e content.** whe
2d190 6e 20 69 74 20 68 61 73 20 66 69 6e 69 73 68 65  n it has finishe
2d1a0 64 20 75 73 69 6e 67 20 74 68 61 74 20 72 65 73  d using that res
2d1b0 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 34  ult..** If the 4
2d1c0 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  th parameter to 
2d1d0 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  the sqlite3_resu
2d1e0 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61  lt_text* interfa
2d1f0 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65  ces.** or sqlite
2d200 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73  3_result_blob is
2d210 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f 6e   the special con
2d220 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 54 52 41  stant SQLITE_TRA
2d230 4e 53 49 45 4e 54 0a 2a 2a 20 74 68 65 6e 20 53  NSIENT.** then S
2d240 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 20 63 6f  QLite makes a co
2d250 70 79 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  py of the result
2d260 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 74 61   into space obta
2d270 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 66 72 6f  ined from.** fro
2d280 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  m [sqlite3_mallo
2d290 63 28 29 5d 20 62 65 66 6f 72 65 20 69 74 20 72  c()] before it r
2d2a0 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  eturns..**.** Th
2d2b0 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
2d2c0 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72 66 61  _value() interfa
2d2d0 63 65 20 73 65 74 73 20 74 68 65 20 72 65 73 75  ce sets the resu
2d2e0 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70  lt of.** the app
2d2f0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
2d300 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20   function to be 
2d310 61 20 63 6f 70 79 20 74 68 65 0a 2a 2a 20 5b 75  a copy the.** [u
2d320 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  nprotected sqlit
2d330 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
2d340 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68   specified by th
2d350 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e  e 2nd parameter.
2d360 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33    The.** sqlite3
2d370 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 20  _result_value() 
2d380 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 73 20  interface makes 
2d390 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 5b 73  a copy of the [s
2d3a0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a 2a 2a  qlite3_value].**
2d3b0 20 73 6f 20 74 68 61 74 20 74 68 65 20 5b 73 71   so that the [sq
2d3c0 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 73 70 65  lite3_value] spe
2d3d0 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 70 61  cified in the pa
2d3e0 72 61 6d 65 74 65 72 20 6d 61 79 20 63 68 61 6e  rameter may chan
2d3f0 67 65 20 6f 72 0a 2a 2a 20 62 65 20 64 65 61 6c  ge or.** be deal
2d400 6c 6f 63 61 74 65 64 20 61 66 74 65 72 20 73 71  located after sq
2d410 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
2d420 75 65 28 29 20 72 65 74 75 72 6e 73 20 77 69 74  ue() returns wit
2d430 68 6f 75 74 20 68 61 72 6d 2e 0a 2a 2a 20 41 20  hout harm..** A 
2d440 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  [protected sqlit
2d450 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
2d460 20 6d 61 79 20 61 6c 77 61 79 73 20 62 65 20 75   may always be u
2d470 73 65 64 20 77 68 65 72 65 20 61 6e 0a 2a 2a 20  sed where an.** 
2d480 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c  [unprotected sql
2d490 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65  ite3_value] obje
2d4a0 63 74 20 69 73 20 72 65 71 75 69 72 65 64 2c 20  ct is required, 
2d4b0 73 6f 20 65 69 74 68 65 72 0a 2a 2a 20 6b 69 6e  so either.** kin
2d4c0 64 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 76 61  d of [sqlite3_va
2d4d0 6c 75 65 5d 20 6f 62 6a 65 63 74 20 63 61 6e 20  lue] object can 
2d4e0 62 65 20 75 73 65 64 20 77 69 74 68 20 74 68 69  be used with thi
2d4f0 73 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a  s interface..**.
2d500 2a 2a 20 49 66 20 74 68 65 73 65 20 72 6f 75 74  ** If these rout
2d510 69 6e 65 73 20 61 72 65 20 63 61 6c 6c 65 64 20  ines are called 
2d520 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20  from within the 
2d530 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64  different thread
2d540 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 6f 6e 65  .** than the one
2d550 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
2d560 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
2d570 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  ned function tha
2d580 74 20 72 65 63 65 69 76 65 64 0a 2a 2a 20 74 68  t received.** th
2d590 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  e [sqlite3_conte
2d5a0 78 74 5d 20 70 6f 69 6e 74 65 72 2c 20 74 68 65  xt] pointer, the
2d5b0 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64   results are und
2d5c0 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65  efined..**.** Re
2d5d0 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b  quirements:.** [
2d5e0 48 31 36 34 30 33 5d 20 5b 48 31 36 34 30 36 5d  H16403] [H16406]
2d5f0 20 5b 48 31 36 34 30 39 5d 20 5b 48 31 36 34 31   [H16409] [H1641
2d600 32 5d 20 5b 48 31 36 34 31 35 5d 20 5b 48 31 36  2] [H16415] [H16
2d610 34 31 38 5d 20 5b 48 31 36 34 32 31 5d 20 5b 48  418] [H16421] [H
2d620 31 36 34 32 34 5d 0a 2a 2a 20 5b 48 31 36 34 32  16424].** [H1642
2d630 37 5d 20 5b 48 31 36 34 33 30 5d 20 5b 48 31 36  7] [H16430] [H16
2d640 34 33 33 5d 20 5b 48 31 36 34 33 36 5d 20 5b 48  433] [H16436] [H
2d650 31 36 34 33 39 5d 20 5b 48 31 36 34 34 32 5d 20  16439] [H16442] 
2d660 5b 48 31 36 34 34 35 5d 20 5b 48 31 36 34 34 38  [H16445] [H16448
2d670 5d 0a 2a 2a 20 5b 48 31 36 34 35 31 5d 20 5b 48  ].** [H16451] [H
2d680 31 36 34 35 34 5d 20 5b 48 31 36 34 35 37 5d 20  16454] [H16457] 
2d690 5b 48 31 36 34 36 30 5d 20 5b 48 31 36 34 36 33  [H16460] [H16463
2d6a0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
2d6b0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
2d6c0 75 6c 74 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33  ult_blob(sqlite3
2d6d0 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74  _context*, const
2d6e0 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69   void*, int, voi
2d6f0 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51  d(*)(void*));.SQ
2d700 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
2d710 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
2d720 62 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ble(sqlite3_cont
2d730 65 78 74 2a 2c 20 64 6f 75 62 6c 65 29 3b 0a 53  ext*, double);.S
2d740 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
2d750 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
2d760 72 6f 72 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ror(sqlite3_cont
2d770 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  ext*, const char
2d780 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
2d790 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
2d7a0 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28  _result_error16(
2d7b0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
2d7c0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69  , const void*, i
2d7d0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  nt);.SQLITE_API 
2d7e0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
2d7f0 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67  ult_error_toobig
2d800 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2d810 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  *);.SQLITE_API v
2d820 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
2d830 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73  lt_error_nomem(s
2d840 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
2d850 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
2d860 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
2d870 5f 65 72 72 6f 72 5f 63 6f 64 65 28 73 71 6c 69  _error_code(sqli
2d880 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e  te3_context*, in
2d890 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  t);.SQLITE_API v
2d8a0 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
2d8b0 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 63  lt_int(sqlite3_c
2d8c0 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53  ontext*, int);.S
2d8d0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
2d8e0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
2d8f0 74 36 34 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  t64(sqlite3_cont
2d900 65 78 74 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e  ext*, sqlite3_in
2d910 74 36 34 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  t64);.SQLITE_API
2d920 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
2d930 73 75 6c 74 5f 6e 75 6c 6c 28 73 71 6c 69 74 65  sult_null(sqlite
2d940 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c  3_context*);.SQL
2d950 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
2d960 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2d970 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2d980 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  *, const char*, 
2d990 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69  int, void(*)(voi
2d9a0 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  d*));.SQLITE_API
2d9b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
2d9c0 73 75 6c 74 5f 74 65 78 74 31 36 28 73 71 6c 69  sult_text16(sqli
2d9d0 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f  te3_context*, co
2d9e0 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20  nst void*, int, 
2d9f0 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
2da00 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2da10 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2da20 74 65 78 74 31 36 6c 65 28 73 71 6c 69 74 65 33  text16le(sqlite3
2da30 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74  _context*, const
2da40 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 76 6f 69 64   void*, int,void
2da50 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c  (*)(void*));.SQL
2da60 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
2da70 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2da80 31 36 62 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e  16be(sqlite3_con
2da90 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69  text*, const voi
2daa0 64 2a 2c 20 69 6e 74 2c 76 6f 69 64 28 2a 29 28  d*, int,void(*)(
2dab0 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f  void*));.SQLITE_
2dac0 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
2dad0 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 73 71  _result_value(sq
2dae0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
2daf0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
2db00 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2db10 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2db20 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33  zeroblob(sqlite3
2db30 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 6e  _context*, int n
2db40 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
2db50 45 46 3a 20 44 65 66 69 6e 65 20 4e 65 77 20 43  EF: Define New C
2db60 6f 6c 6c 61 74 69 6e 67 20 53 65 71 75 65 6e 63  ollating Sequenc
2db70 65 73 20 7b 48 31 36 36 30 30 7d 20 3c 53 32 30  es {H16600} <S20
2db80 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  300>.**.** These
2db90 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 75   functions are u
2dba0 73 65 64 20 74 6f 20 61 64 64 20 6e 65 77 20 63  sed to add new c
2dbb0 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2dbc0 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 5b 64 61  es to the.** [da
2dbd0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2dbe0 6e 5d 20 73 70 65 63 69 66 69 65 64 20 61 73 20  n] specified as 
2dbf0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
2dc00 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61  nt..**.** The na
2dc10 6d 65 20 6f 66 20 74 68 65 20 6e 65 77 20 63 6f  me of the new co
2dc20 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
2dc30 20 69 73 20 73 70 65 63 69 66 69 65 64 20 61 73   is specified as
2dc40 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67 0a   a UTF-8 string.
2dc50 2a 2a 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63  ** for sqlite3_c
2dc60 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28  reate_collation(
2dc70 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 72  ) and sqlite3_cr
2dc80 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76  eate_collation_v
2dc90 32 28 29 0a 2a 2a 20 61 6e 64 20 61 20 55 54 46  2().** and a UTF
2dca0 2d 31 36 20 73 74 72 69 6e 67 20 66 6f 72 20 73  -16 string for s
2dcb0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
2dcc0 6c 6c 61 74 69 6f 6e 31 36 28 29 2e 20 49 6e 20  llation16(). In 
2dcd0 61 6c 6c 20 63 61 73 65 73 0a 2a 2a 20 74 68 65  all cases.** the
2dce0 20 6e 61 6d 65 20 69 73 20 70 61 73 73 65 64 20   name is passed 
2dcf0 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 66 75  as the second fu
2dd00 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 2e  nction argument.
2dd10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64  .**.** The third
2dd20 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 20 62 65   argument may be
2dd30 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f 6e 73   one of the cons
2dd40 74 61 6e 74 73 20 5b 53 51 4c 49 54 45 5f 55 54  tants [SQLITE_UT
2dd50 46 38 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  F8],.** [SQLITE_
2dd60 55 54 46 31 36 4c 45 5d 2c 20 6f 72 20 5b 53 51  UTF16LE], or [SQ
2dd70 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2c 20 69  LITE_UTF16BE], i
2dd80 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 74  ndicating that t
2dd90 68 65 20 75 73 65 72 2d 73 75 70 70 6c 69 65 64  he user-supplied
2dda0 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 65 78 70 65  .** routine expe
2ddb0 63 74 73 20 74 6f 20 62 65 20 70 61 73 73 65 64  cts to be passed
2ddc0 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 73 74 72   pointers to str
2ddd0 69 6e 67 73 20 65 6e 63 6f 64 65 64 20 75 73 69  ings encoded usi
2dde0 6e 67 20 55 54 46 2d 38 2c 0a 2a 2a 20 55 54 46  ng UTF-8,.** UTF
2ddf0 2d 31 36 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61  -16 little-endia
2de00 6e 2c 20 6f 72 20 55 54 46 2d 31 36 20 62 69 67  n, or UTF-16 big
2de10 2d 65 6e 64 69 61 6e 2c 20 72 65 73 70 65 63 74  -endian, respect
2de20 69 76 65 6c 79 2e 20 54 68 65 0a 2a 2a 20 74 68  ively. The.** th
2de30 69 72 64 20 61 72 67 75 6d 65 6e 74 20 6d 69 67  ird argument mig
2de40 68 74 20 61 6c 73 6f 20 62 65 20 5b 53 51 4c 49  ht also be [SQLI
2de50 54 45 5f 55 54 46 31 36 5d 20 74 6f 20 69 6e 64  TE_UTF16] to ind
2de60 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20 72  icate that the r
2de70 6f 75 74 69 6e 65 0a 2a 2a 20 65 78 70 65 63 74  outine.** expect
2de80 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 62 65  s pointers to be
2de90 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 73 20   UTF-16 strings 
2dea0 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79  in the native by
2deb0 74 65 20 6f 72 64 65 72 2c 20 6f 72 20 74 68 65  te order, or the
2dec0 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 63 61 6e  .** argument can
2ded0 20 62 65 20 5b 53 51 4c 49 54 45 5f 55 54 46 31   be [SQLITE_UTF1
2dee0 36 5f 41 4c 49 47 4e 45 44 5d 20 69 66 20 74 68  6_ALIGNED] if th
2def0 65 0a 2a 2a 20 74 68 65 20 72 6f 75 74 69 6e 65  e.** the routine
2df00 20 65 78 70 65 63 74 73 20 70 6f 69 6e 74 65 72   expects pointer
2df10 73 20 74 6f 20 31 36 2d 62 69 74 20 77 6f 72 64  s to 16-bit word
2df20 20 61 6c 69 67 6e 65 64 20 73 74 72 69 6e 67 73   aligned strings
2df30 0a 2a 2a 20 6f 66 20 55 54 46 2d 31 36 20 69 6e  .** of UTF-16 in
2df40 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 65   the native byte
2df50 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20   order..**.** A 
2df60 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 75  pointer to the u
2df70 73 65 72 20 73 75 70 70 6c 69 65 64 20 72 6f 75  ser supplied rou
2df80 74 69 6e 65 20 6d 75 73 74 20 62 65 20 70 61 73  tine must be pas
2df90 73 65 64 20 61 73 20 74 68 65 20 66 69 66 74 68  sed as the fifth
2dfa0 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20 20 49  .** argument.  I
2dfb0 66 20 69 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68  f it is NULL, th
2dfc0 69 73 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  is is the same a
2dfd0 73 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 63  s deleting the c
2dfe0 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75  ollation.** sequ
2dff0 65 6e 63 65 20 28 73 6f 20 74 68 61 74 20 53 51  ence (so that SQ
2e000 4c 69 74 65 20 63 61 6e 6e 6f 74 20 63 61 6c 6c  Lite cannot call
2e010 20 69 74 20 61 6e 79 6d 6f 72 65 29 2e 0a 2a 2a   it anymore)..**
2e020 20 45 61 63 68 20 74 69 6d 65 20 74 68 65 20 61   Each time the a
2e030 70 70 6c 69 63 61 74 69 6f 6e 20 73 75 70 70 6c  pplication suppl
2e040 69 65 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  ied function is 
2e050 69 6e 76 6f 6b 65 64 2c 20 69 74 20 69 73 20 70  invoked, it is p
2e060 61 73 73 65 64 0a 2a 2a 20 61 73 20 69 74 73 20  assed.** as its 
2e070 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
2e080 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 6f  a copy of the vo
2e090 69 64 2a 20 70 61 73 73 65 64 20 61 73 20 74 68  id* passed as th
2e0a0 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e  e fourth argumen
2e0b0 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f  t.** to sqlite3_
2e0c0 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
2e0d0 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 72  () or sqlite3_cr
2e0e0 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36  eate_collation16
2e0f0 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  ()..**.** The re
2e100 6d 61 69 6e 69 6e 67 20 61 72 67 75 6d 65 6e 74  maining argument
2e110 73 20 74 6f 20 74 68 65 20 61 70 70 6c 69 63 61  s to the applica
2e120 74 69 6f 6e 2d 73 75 70 70 6c 69 65 64 20 72 6f  tion-supplied ro
2e130 75 74 69 6e 65 20 61 72 65 20 74 77 6f 20 73 74  utine are two st
2e140 72 69 6e 67 73 2c 0a 2a 2a 20 65 61 63 68 20 72  rings,.** each r
2e150 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 61 20  epresented by a 
2e160 28 6c 65 6e 67 74 68 2c 20 64 61 74 61 29 20 70  (length, data) p
2e170 61 69 72 20 61 6e 64 20 65 6e 63 6f 64 65 64 20  air and encoded 
2e180 69 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 0a  in the encoding.
2e190 2a 2a 20 74 68 61 74 20 77 61 73 20 70 61 73 73  ** that was pass
2e1a0 65 64 20 61 73 20 74 68 65 20 74 68 69 72 64 20  ed as the third 
2e1b0 61 72 67 75 6d 65 6e 74 20 77 68 65 6e 20 74 68  argument when th
2e1c0 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
2e1d0 65 6e 63 65 20 77 61 73 0a 2a 2a 20 72 65 67 69  ence was.** regi
2e1e0 73 74 65 72 65 64 2e 20 7b 45 4e 44 7d 20 20 54  stered. {END}  T
2e1f0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64  he application d
2e200 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  efined collation
2e210 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 0a   routine should.
2e220 2a 2a 20 72 65 74 75 72 6e 20 6e 65 67 61 74 69  ** return negati
2e230 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f 73 69  ve, zero or posi
2e240 74 69 76 65 20 69 66 20 74 68 65 20 66 69 72 73  tive if the firs
2e250 74 20 73 74 72 69 6e 67 20 69 73 20 6c 65 73 73  t string is less
2e260 20 74 68 61 6e 2c 0a 2a 2a 20 65 71 75 61 6c 20   than,.** equal 
2e270 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 20 74  to, or greater t
2e280 68 61 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 73  han the second s
2e290 74 72 69 6e 67 2e 20 69 2e 65 2e 20 28 53 54 52  tring. i.e. (STR
2e2a0 49 4e 47 31 20 2d 20 53 54 52 49 4e 47 32 29 2e  ING1 - STRING2).
2e2b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
2e2c0 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
2e2d0 69 6f 6e 5f 76 32 28 29 20 77 6f 72 6b 73 20 6c  ion_v2() works l
2e2e0 69 6b 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61  ike sqlite3_crea
2e2f0 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 0a 2a  te_collation().*
2e300 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74  * except that it
2e310 20 74 61 6b 65 73 20 61 6e 20 65 78 74 72 61 20   takes an extra 
2e320 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69  argument which i
2e330 73 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 66  s a destructor f
2e340 6f 72 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 74  or.** the collat
2e350 69 6f 6e 2e 20 20 54 68 65 20 64 65 73 74 72 75  ion.  The destru
2e360 63 74 6f 72 20 69 73 20 63 61 6c 6c 65 64 20 77  ctor is called w
2e370 68 65 6e 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  hen the collatio
2e380 6e 20 69 73 0a 2a 2a 20 64 65 73 74 72 6f 79 65  n is.** destroye
2e390 64 20 61 6e 64 20 69 73 20 70 61 73 73 65 64 20  d and is passed 
2e3a0 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 66 6f  a copy of the fo
2e3b0 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 76  urth parameter v
2e3c0 6f 69 64 2a 20 70 6f 69 6e 74 65 72 0a 2a 2a 20  oid* pointer.** 
2e3d0 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  of the sqlite3_c
2e3e0 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  reate_collation_
2e3f0 76 32 28 29 2e 0a 2a 2a 20 43 6f 6c 6c 61 74 69  v2()..** Collati
2e400 6f 6e 73 20 61 72 65 20 64 65 73 74 72 6f 79 65  ons are destroye
2e410 64 20 77 68 65 6e 20 74 68 65 79 20 61 72 65 20  d when they are 
2e420 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20 6c 61  overridden by la
2e430 74 65 72 20 63 61 6c 6c 73 20 74 6f 20 74 68 65  ter calls to the
2e440 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 72  .** collation cr
2e450 65 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73  eation functions
2e460 20 6f 72 20 77 68 65 6e 20 74 68 65 20 5b 64 61   or when the [da
2e470 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2e480 6e 5d 20 69 73 20 63 6c 6f 73 65 64 0a 2a 2a 20  n] is closed.** 
2e490 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63  using [sqlite3_c
2e4a0 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53  lose()]..**.** S
2e4b0 65 65 20 61 6c 73 6f 3a 20 20 5b 73 71 6c 69 74  ee also:  [sqlit
2e4c0 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
2e4d0 64 65 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  ded()] and [sqli
2e4e0 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65  te3_collation_ne
2e4f0 65 64 65 64 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a  eded16()]..**.**
2e500 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
2e510 2a 20 5b 48 31 36 36 30 33 5d 20 5b 48 31 36 36  * [H16603] [H166
2e520 30 34 5d 20 5b 48 31 36 36 30 36 5d 20 5b 48 31  04] [H16606] [H1
2e530 36 36 30 39 5d 20 5b 48 31 36 36 31 32 5d 20 5b  6609] [H16612] [
2e540 48 31 36 36 31 35 5d 20 5b 48 31 36 36 31 38 5d  H16615] [H16618]
2e550 20 5b 48 31 36 36 32 31 5d 0a 2a 2a 20 5b 48 31   [H16621].** [H1
2e560 36 36 32 34 5d 20 5b 48 31 36 36 32 37 5d 20 5b  6624] [H16627] [
2e570 48 31 36 36 33 30 5d 0a 2a 2f 0a 53 51 4c 49 54  H16630].*/.SQLIT
2e580 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2e590 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
2e5a0 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20  on(.  sqlite3*, 
2e5b0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2e5c0 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65 54 65  Name, .  int eTe
2e5d0 78 74 52 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c  xtRep, .  void*,
2e5e0 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65  .  int(*xCompare
2e5f0 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
2e600 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  t void*,int,cons
2e610 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 53 51 4c 49  t void*).);.SQLI
2e620 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2e630 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
2e640 69 6f 6e 5f 76 32 28 0a 20 20 73 71 6c 69 74 65  ion_v2(.  sqlite
2e650 33 2a 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  3*, .  const cha
2e660 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74  r *zName, .  int
2e670 20 65 54 65 78 74 52 65 70 2c 20 0a 20 20 76 6f   eTextRep, .  vo
2e680 69 64 2a 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d  id*,.  int(*xCom
2e690 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  pare)(void*,int,
2e6a0 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
2e6b0 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 0a 20 20  const void*),.  
2e6c0 76 6f 69 64 28 2a 78 44 65 73 74 72 6f 79 29 28  void(*xDestroy)(
2e6d0 76 6f 69 64 2a 29 0a 29 3b 0a 53 51 4c 49 54 45  void*).);.SQLITE
2e6e0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2e6f0 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
2e700 6e 31 36 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c  n16(.  sqlite3*,
2e710 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   .  const void *
2e720 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 65 54 65  zName,.  int eTe
2e730 78 74 52 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c  xtRep, .  void*,
2e740 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65  .  int(*xCompare
2e750 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
2e760 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  t void*,int,cons
2e770 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a  t void*).);../*.
2e780 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6c  ** CAPI3REF: Col
2e790 6c 61 74 69 6f 6e 20 4e 65 65 64 65 64 20 43 61  lation Needed Ca
2e7a0 6c 6c 62 61 63 6b 73 20 7b 48 31 36 37 30 30 7d  llbacks {H16700}
2e7b0 20 3c 53 32 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S20300>.**.** 
2e7c0 54 6f 20 61 76 6f 69 64 20 68 61 76 69 6e 67 20  To avoid having 
2e7d0 74 6f 20 72 65 67 69 73 74 65 72 20 61 6c 6c 20  to register all 
2e7e0 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
2e7f0 63 65 73 20 62 65 66 6f 72 65 20 61 20 64 61 74  ces before a dat
2e800 61 62 61 73 65 0a 2a 2a 20 63 61 6e 20 62 65 20  abase.** can be 
2e810 75 73 65 64 2c 20 61 20 73 69 6e 67 6c 65 20 63  used, a single c
2e820 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
2e830 20 6d 61 79 20 62 65 20 72 65 67 69 73 74 65 72   may be register
2e840 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 5b  ed with the.** [
2e850 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2e860 69 6f 6e 5d 20 74 6f 20 62 65 20 63 61 6c 6c 65  ion] to be calle
2e870 64 20 77 68 65 6e 65 76 65 72 20 61 6e 20 75 6e  d whenever an un
2e880 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f  defined collatio
2e890 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 69 73  n.** sequence is
2e8a0 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a   required..**.**
2e8b0 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   If the function
2e8c0 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20 75   is registered u
2e8d0 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33  sing the sqlite3
2e8e0 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
2e8f0 64 28 29 20 41 50 49 2c 0a 2a 2a 20 74 68 65 6e  d() API,.** then
2e900 20 69 74 20 69 73 20 70 61 73 73 65 64 20 74 68   it is passed th
2e910 65 20 6e 61 6d 65 73 20 6f 66 20 75 6e 64 65 66  e names of undef
2e920 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  ined collation s
2e930 65 71 75 65 6e 63 65 73 20 61 73 20 73 74 72 69  equences as stri
2e940 6e 67 73 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 69  ngs.** encoded i
2e950 6e 20 55 54 46 2d 38 2e 20 7b 48 31 36 37 30 33  n UTF-8. {H16703
2e960 7d 20 49 66 20 73 71 6c 69 74 65 33 5f 63 6f 6c  } If sqlite3_col
2e970 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28  lation_needed16(
2e980 29 20 69 73 20 75 73 65 64 2c 0a 2a 2a 20 74 68  ) is used,.** th
2e990 65 20 6e 61 6d 65 73 20 61 72 65 20 70 61 73 73  e names are pass
2e9a0 65 64 20 61 73 20 55 54 46 2d 31 36 20 69 6e 20  ed as UTF-16 in 
2e9b0 6d 61 63 68 69 6e 65 20 6e 61 74 69 76 65 20 62  machine native b
2e9c0 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2a 20 41 20  yte order..** A 
2e9d0 63 61 6c 6c 20 74 6f 20 65 69 74 68 65 72 20 66  call to either f
2e9e0 75 6e 63 74 69 6f 6e 20 72 65 70 6c 61 63 65 73  unction replaces
2e9f0 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 63 61   any existing ca
2ea00 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 57 68  llback..**.** Wh
2ea10 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  en the callback 
2ea20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 74 68 65 20  is invoked, the 
2ea30 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 70  first argument p
2ea40 61 73 73 65 64 20 69 73 20 61 20 63 6f 70 79 0a  assed is a copy.
2ea50 2a 2a 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64  ** of the second
2ea60 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c   argument to sql
2ea70 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
2ea80 65 65 64 65 64 28 29 20 6f 72 0a 2a 2a 20 73 71  eeded() or.** sq
2ea90 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  lite3_collation_
2eaa0 6e 65 65 64 65 64 31 36 28 29 2e 20 20 54 68 65  needed16().  The
2eab0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
2eac0 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65   is the database
2ead0 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  .** connection. 
2eae0 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d   The third argum
2eaf0 65 6e 74 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53  ent is one of [S
2eb00 51 4c 49 54 45 5f 55 54 46 38 5d 2c 20 5b 53 51  QLITE_UTF8], [SQ
2eb10 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2c 0a 2a  LITE_UTF16BE],.*
2eb20 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 54 46  * or [SQLITE_UTF
2eb30 31 36 4c 45 5d 2c 20 69 6e 64 69 63 61 74 69 6e  16LE], indicatin
2eb40 67 20 74 68 65 20 6d 6f 73 74 20 64 65 73 69 72  g the most desir
2eb50 61 62 6c 65 20 66 6f 72 6d 20 6f 66 20 74 68 65  able form of the
2eb60 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65   collation.** se
2eb70 71 75 65 6e 63 65 20 66 75 6e 63 74 69 6f 6e 20  quence function 
2eb80 72 65 71 75 69 72 65 64 2e 20 20 54 68 65 20 66  required.  The f
2eb90 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 20  ourth parameter 
2eba0 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
2ebb0 68 65 0a 2a 2a 20 72 65 71 75 69 72 65 64 20 63  he.** required c
2ebc0 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2ebd0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c  e..**.** The cal
2ebe0 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 73  lback function s
2ebf0 68 6f 75 6c 64 20 72 65 67 69 73 74 65 72 20 74  hould register t
2ec00 68 65 20 64 65 73 69 72 65 64 20 63 6f 6c 6c 61  he desired colla
2ec10 74 69 6f 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73  tion using.** [s
2ec20 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
2ec30 6c 6c 61 74 69 6f 6e 28 29 5d 2c 20 5b 73 71 6c  llation()], [sql
2ec40 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
2ec50 61 74 69 6f 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a  ation16()], or.*
2ec60 2a 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74  * [sqlite3_creat
2ec70 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29  e_collation_v2()
2ec80 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  ]..**.** Require
2ec90 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 37 30  ments:.** [H1670
2eca0 32 5d 20 5b 48 31 36 37 30 34 5d 20 5b 48 31 36  2] [H16704] [H16
2ecb0 37 30 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  706].*/.SQLITE_A
2ecc0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
2ecd0 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28  ollation_needed(
2ece0 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20  .  sqlite3*, .  
2ecf0 76 6f 69 64 2a 2c 20 0a 20 20 76 6f 69 64 28 2a  void*, .  void(*
2ed00 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a  )(void*,sqlite3*
2ed10 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f  ,int eTextRep,co
2ed20 6e 73 74 20 63 68 61 72 2a 29 0a 29 3b 0a 53 51  nst char*).);.SQ
2ed30 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2ed40 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
2ed50 65 65 64 65 64 31 36 28 0a 20 20 73 71 6c 69 74  eeded16(.  sqlit
2ed60 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20  e3*, .  void*,. 
2ed70 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73   void(*)(void*,s
2ed80 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78  qlite3*,int eTex
2ed90 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  tRep,const void*
2eda0 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 70 65 63  ).);../*.** Spec
2edb0 69 66 79 20 74 68 65 20 6b 65 79 20 66 6f 72 20  ify the key for 
2edc0 61 6e 20 65 6e 63 72 79 70 74 65 64 20 64 61 74  an encrypted dat
2edd0 61 62 61 73 65 2e 20 20 54 68 69 73 20 72 6f 75  abase.  This rou
2ede0 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 0a 2a  tine should be.*
2edf0 2a 20 63 61 6c 6c 65 64 20 72 69 67 68 74 20 61  * called right a
2ee00 66 74 65 72 20 73 71 6c 69 74 65 33 5f 6f 70 65  fter sqlite3_ope
2ee10 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  n()..**.** The c
2ee20 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ode to implement
2ee30 20 74 68 69 73 20 41 50 49 20 69 73 20 6e 6f 74   this API is not
2ee40 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68   available in th
2ee50 65 20 70 75 62 6c 69 63 20 72 65 6c 65 61 73 65  e public release
2ee60 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  .** of SQLite..*
2ee70 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2ee80 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 0a 20 20   sqlite3_key(.  
2ee90 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
2eea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2eeb0 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 62 65  * Database to be
2eec0 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20 63 6f   rekeyed */.  co
2eed0 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
2eee0 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f 2a 20  int nKey     /* 
2eef0 54 68 65 20 6b 65 79 20 2a 2f 0a 29 3b 0a 0a 2f  The key */.);../
2ef00 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
2ef10 6b 65 79 20 6f 6e 20 61 6e 20 6f 70 65 6e 20 64  key on an open d
2ef20 61 74 61 62 61 73 65 2e 20 20 49 66 20 74 68 65  atabase.  If the
2ef30 20 63 75 72 72 65 6e 74 20 64 61 74 61 62 61 73   current databas
2ef40 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 65 6e 63 72  e is not.** encr
2ef50 79 70 74 65 64 2c 20 74 68 69 73 20 72 6f 75 74  ypted, this rout
2ef60 69 6e 65 20 77 69 6c 6c 20 65 6e 63 72 79 70 74  ine will encrypt
2ef70 20 69 74 2e 20 20 49 66 20 70 4e 65 77 3d 3d 30   it.  If pNew==0
2ef80 20 6f 72 20 6e 4e 65 77 3d 3d 30 2c 20 74 68 65   or nNew==0, the
2ef90 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73 20  .** database is 
2efa0 64 65 63 72 79 70 74 65 64 2e 0a 2a 2a 0a 2a 2a  decrypted..**.**
2efb0 20 54 68 65 20 63 6f 64 65 20 74 6f 20 69 6d 70   The code to imp
2efc0 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 50 49 20  lement this API 
2efd0 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65  is not available
2efe0 20 69 6e 20 74 68 65 20 70 75 62 6c 69 63 20 72   in the public r
2eff0 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 53 51 4c  elease.** of SQL
2f000 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ite..*/.SQLITE_A
2f010 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72  PI int sqlite3_r
2f020 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65 33 20  ekey(.  sqlite3 
2f030 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
2f040 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2f050 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79 65 64  se to be rekeyed
2f060 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
2f070 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79   *pKey, int nKey
2f080 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20       /* The new 
2f090 6b 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  key */.);../*.**
2f0a0 20 43 41 50 49 33 52 45 46 3a 20 53 75 73 70 65   CAPI3REF: Suspe
2f0b0 6e 64 20 45 78 65 63 75 74 69 6f 6e 20 46 6f 72  nd Execution For
2f0c0 20 41 20 53 68 6f 72 74 20 54 69 6d 65 20 7b 48   A Short Time {H
2f0d0 31 30 35 33 30 7d 20 3c 53 34 30 34 31 30 3e 0a  10530} <S40410>.
2f0e0 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
2f0f0 33 5f 73 6c 65 65 70 28 29 20 66 75 6e 63 74 69  3_sleep() functi
2f100 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20 63 75  on causes the cu
2f110 72 72 65 6e 74 20 74 68 72 65 61 64 20 74 6f 20  rrent thread to 
2f120 73 75 73 70 65 6e 64 20 65 78 65 63 75 74 69 6f  suspend executio
2f130 6e 0a 2a 2a 20 66 6f 72 20 61 74 20 6c 65 61 73  n.** for at leas
2f140 74 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69  t a number of mi
2f150 6c 6c 69 73 65 63 6f 6e 64 73 20 73 70 65 63 69  lliseconds speci
2f160 66 69 65 64 20 69 6e 20 69 74 73 20 70 61 72 61  fied in its para
2f170 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  meter..**.** If 
2f180 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  the operating sy
2f190 73 74 65 6d 20 64 6f 65 73 20 6e 6f 74 20 73 75  stem does not su
2f1a0 70 70 6f 72 74 20 73 6c 65 65 70 20 72 65 71 75  pport sleep requ
2f1b0 65 73 74 73 20 77 69 74 68 0a 2a 2a 20 6d 69 6c  ests with.** mil
2f1c0 6c 69 73 65 63 6f 6e 64 20 74 69 6d 65 20 72 65  lisecond time re
2f1d0 73 6f 6c 75 74 69 6f 6e 2c 20 74 68 65 6e 20 74  solution, then t
2f1e0 68 65 20 74 69 6d 65 20 77 69 6c 6c 20 62 65 20  he time will be 
2f1f0 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 0a 2a 2a  rounded up to.**
2f200 20 74 68 65 20 6e 65 61 72 65 73 74 20 73 65 63   the nearest sec
2f210 6f 6e 64 2e 20 54 68 65 20 6e 75 6d 62 65 72 20  ond. The number 
2f220 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20  of milliseconds 
2f230 6f 66 20 73 6c 65 65 70 20 61 63 74 75 61 6c 6c  of sleep actuall
2f240 79 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 20 66  y.** requested f
2f250 72 6f 6d 20 74 68 65 20 6f 70 65 72 61 74 69 6e  rom the operatin
2f260 67 20 73 79 73 74 65 6d 20 69 73 20 72 65 74 75  g system is retu
2f270 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  rned..**.** SQLi
2f280 74 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  te implements th
2f290 69 73 20 69 6e 74 65 72 66 61 63 65 20 62 79 20  is interface by 
2f2a0 63 61 6c 6c 69 6e 67 20 74 68 65 20 78 53 6c 65  calling the xSle
2f2b0 65 70 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f  ep().** method o
2f2c0 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73  f the default [s
2f2d0 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65  qlite3_vfs] obje
2f2e0 63 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ct..**.** Requir
2f2f0 65 6d 65 6e 74 73 3a 20 5b 48 31 30 35 33 33 5d  ements: [H10533]
2f300 20 5b 48 31 30 35 33 36 5d 0a 2a 2f 0a 53 51 4c   [H10536].*/.SQL
2f310 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2f320 74 65 33 5f 73 6c 65 65 70 28 69 6e 74 29 3b 0a  te3_sleep(int);.
2f330 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
2f340 20 4e 61 6d 65 20 4f 66 20 54 68 65 20 46 6f 6c   Name Of The Fol
2f350 64 65 72 20 48 6f 6c 64 69 6e 67 20 54 65 6d 70  der Holding Temp
2f360 6f 72 61 72 79 20 46 69 6c 65 73 20 7b 48 31 30  orary Files {H10
2f370 33 31 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a  310} <S20000>.**
2f380 0a 2a 2a 20 49 66 20 74 68 69 73 20 67 6c 6f 62  .** If this glob
2f390 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 6d  al variable is m
2f3a0 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  ade to point to 
2f3b0 61 20 73 74 72 69 6e 67 20 77 68 69 63 68 20 69  a string which i
2f3c0 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f 66  s.** the name of
2f3d0 20 61 20 66 6f 6c 64 65 72 20 28 61 2e 6b 2e 61   a folder (a.k.a
2f3e0 2e 20 64 69 72 65 63 74 6f 72 79 29 2c 20 74 68  . directory), th
2f3f0 65 6e 20 61 6c 6c 20 74 65 6d 70 6f 72 61 72 79  en all temporary
2f400 20 66 69 6c 65 73 0a 2a 2a 20 63 72 65 61 74 65   files.** create
2f410 64 20 62 79 20 53 51 4c 69 74 65 20 77 69 6c 6c  d by SQLite will
2f420 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20 74 68   be placed in th
2f430 61 74 20 64 69 72 65 63 74 6f 72 79 2e 20 20 49  at directory.  I
2f440 66 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 0a  f this variable.
2f450 2a 2a 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69  ** is a NULL poi
2f460 6e 74 65 72 2c 20 74 68 65 6e 20 53 51 4c 69 74  nter, then SQLit
2f470 65 20 70 65 72 66 6f 72 6d 73 20 61 20 73 65 61  e performs a sea
2f480 72 63 68 20 66 6f 72 20 61 6e 20 61 70 70 72 6f  rch for an appro
2f490 70 72 69 61 74 65 0a 2a 2a 20 74 65 6d 70 6f 72  priate.** tempor
2f4a0 61 72 79 20 66 69 6c 65 20 64 69 72 65 63 74 6f  ary file directo
2f4b0 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20  ry..**.** It is 
2f4c0 6e 6f 74 20 73 61 66 65 20 74 6f 20 72 65 61 64  not safe to read
2f4d0 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 69 73 20   or modify this 
2f4e0 76 61 72 69 61 62 6c 65 20 69 6e 20 6d 6f 72 65  variable in more
2f4f0 20 74 68 61 6e 20 6f 6e 65 0a 2a 2a 20 74 68 72   than one.** thr
2f500 65 61 64 20 61 74 20 61 20 74 69 6d 65 2e 20 20  ead at a time.  
2f510 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74  It is not safe t
2f520 6f 20 72 65 61 64 20 6f 72 20 6d 6f 64 69 66 79  o read or modify
2f530 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 0a 2a   this variable.*
2f540 2a 20 69 66 20 61 20 5b 64 61 74 61 62 61 73 65  * if a [database
2f550 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20   connection] is 
2f560 62 65 69 6e 67 20 75 73 65 64 20 61 74 20 74 68  being used at th
2f570 65 20 73 61 6d 65 20 74 69 6d 65 20 69 6e 20 61  e same time in a
2f580 20 73 65 70 61 72 61 74 65 0a 2a 2a 20 74 68 72   separate.** thr
2f590 65 61 64 2e 0a 2a 2a 20 49 74 20 69 73 20 69 6e  ead..** It is in
2f5a0 74 65 6e 64 65 64 20 74 68 61 74 20 74 68 69 73  tended that this
2f5b0 20 76 61 72 69 61 62 6c 65 20 62 65 20 73 65 74   variable be set
2f5c0 20 6f 6e 63 65 0a 2a 2a 20 61 73 20 70 61 72 74   once.** as part
2f5d0 20 6f 66 20 70 72 6f 63 65 73 73 20 69 6e 69 74   of process init
2f5e0 69 61 6c 69 7a 61 74 69 6f 6e 20 61 6e 64 20 62  ialization and b
2f5f0 65 66 6f 72 65 20 61 6e 79 20 53 51 4c 69 74 65  efore any SQLite
2f600 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 72 6f   interface.** ro
2f610 75 74 69 6e 65 73 20 68 61 76 65 20 62 65 65 6e  utines have been
2f620 20 63 61 6c 6c 65 64 20 61 6e 64 20 74 68 61 74   called and that
2f630 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 72   this variable r
2f640 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 0a  emain unchanged.
2f650 2a 2a 20 74 68 65 72 65 61 66 74 65 72 2e 0a 2a  ** thereafter..*
2f660 2a 0a 2a 2a 20 54 68 65 20 5b 74 65 6d 70 5f 73  *.** The [temp_s
2f670 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20 70  tore_directory p
2f680 72 61 67 6d 61 5d 20 6d 61 79 20 6d 6f 64 69 66  ragma] may modif
2f690 79 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20  y this variable 
2f6a0 61 6e 64 20 63 61 75 73 65 0a 2a 2a 20 69 74 20  and cause.** it 
2f6b0 74 6f 20 70 6f 69 6e 74 20 74 6f 20 6d 65 6d 6f  to point to memo
2f6c0 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
2f6d0 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
2f6e0 5d 2e 20 20 46 75 72 74 68 65 72 6d 6f 72 65 2c  ].  Furthermore,
2f6f0 0a 2a 2a 20 74 68 65 20 5b 74 65 6d 70 5f 73 74  .** the [temp_st
2f700 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20 70 72  ore_directory pr
2f710 61 67 6d 61 5d 20 61 6c 77 61 79 73 20 61 73 73  agma] always ass
2f720 75 6d 65 73 20 74 68 61 74 20 61 6e 79 20 73 74  umes that any st
2f730 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20 74 68 69  ring.** that thi
2f740 73 20 76 61 72 69 61 62 6c 65 20 70 6f 69 6e 74  s variable point
2f750 73 20 74 6f 20 69 73 20 68 65 6c 64 20 69 6e 20  s to is held in 
2f760 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20  memory obtained 
2f770 66 72 6f 6d 20 0a 2a 2a 20 5b 73 71 6c 69 74 65  from .** [sqlite
2f780 33 5f 6d 61 6c 6c 6f 63 5d 20 61 6e 64 20 74 68  3_malloc] and th
2f790 65 20 70 72 61 67 6d 61 20 6d 61 79 20 61 74 74  e pragma may att
2f7a0 65 6d 70 74 20 74 6f 20 66 72 65 65 20 74 68 61  empt to free tha
2f7b0 74 20 6d 65 6d 6f 72 79 0a 2a 2a 20 75 73 69 6e  t memory.** usin
2f7c0 67 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5d  g [sqlite3_free]
2f7d0 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 69 66 20 74  ..** Hence, if t
2f7e0 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 73 20  his variable is 
2f7f0 6d 6f 64 69 66 69 65 64 20 64 69 72 65 63 74 6c  modified directl
2f800 79 2c 20 65 69 74 68 65 72 20 69 74 20 73 68 6f  y, either it sho
2f810 75 6c 64 20 62 65 0a 2a 2a 20 6d 61 64 65 20 4e  uld be.** made N
2f820 55 4c 4c 20 6f 72 20 6d 61 64 65 20 74 6f 20 70  ULL or made to p
2f830 6f 69 6e 74 20 74 6f 20 6d 65 6d 6f 72 79 20 6f  oint to memory o
2f840 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71  btained from [sq
2f850 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 5d 0a 2a 2a  lite3_malloc].**
2f860 20 6f 72 20 65 6c 73 65 20 74 68 65 20 75 73 65   or else the use
2f870 20 6f 66 20 74 68 65 20 5b 74 65 6d 70 5f 73 74   of the [temp_st
2f880 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20 70 72  ore_directory pr
2f890 61 67 6d 61 5d 20 73 68 6f 75 6c 64 20 62 65 20  agma] should be 
2f8a0 61 76 6f 69 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49  avoided..*/.SQLI
2f8b0 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c  TE_API char *sql
2f8c0 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74  ite3_temp_direct
2f8d0 6f 72 79 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ory;../*.** CAPI
2f8e0 33 52 45 46 3a 20 54 65 73 74 20 46 6f 72 20 41  3REF: Test For A
2f8f0 75 74 6f 2d 43 6f 6d 6d 69 74 20 4d 6f 64 65 20  uto-Commit Mode 
2f900 7b 48 31 32 39 33 30 7d 20 3c 53 36 30 32 30 30  {H12930} <S60200
2f910 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  >.** KEYWORDS: {
2f920 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 7d  autocommit mode}
2f930 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
2f940 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69  e3_get_autocommi
2f950 74 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65  t() interface re
2f960 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f  turns non-zero o
2f970 72 0a 2a 2a 20 7a 65 72 6f 20 69 66 20 74 68 65  r.** zero if the
2f980 20 67 69 76 65 6e 20 64 61 74 61 62 61 73 65 20   given database 
2f990 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 72  connection is or
2f9a0 20 69 73 20 6e 6f 74 20 69 6e 20 61 75 74 6f 63   is not in autoc
2f9b0 6f 6d 6d 69 74 20 6d 6f 64 65 2c 0a 2a 2a 20 72  ommit mode,.** r
2f9c0 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20 41 75  espectively.  Au
2f9d0 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73  tocommit mode is
2f9e0 20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 2e 0a   on by default..
2f9f0 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f  ** Autocommit mo
2fa00 64 65 20 69 73 20 64 69 73 61 62 6c 65 64 20 62  de is disabled b
2fa10 79 20 61 20 5b 42 45 47 49 4e 5d 20 73 74 61 74  y a [BEGIN] stat
2fa20 65 6d 65 6e 74 2e 0a 2a 2a 20 41 75 74 6f 63 6f  ement..** Autoco
2fa30 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 72 65 2d  mmit mode is re-
2fa40 65 6e 61 62 6c 65 64 20 62 79 20 61 20 5b 43 4f  enabled by a [CO
2fa50 4d 4d 49 54 5d 20 6f 72 20 5b 52 4f 4c 4c 42 41  MMIT] or [ROLLBA
2fa60 43 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 65  CK]..**.** If ce
2fa70 72 74 61 69 6e 20 6b 69 6e 64 73 20 6f 66 20 65  rtain kinds of e
2fa80 72 72 6f 72 73 20 6f 63 63 75 72 20 6f 6e 20 61  rrors occur on a
2fa90 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 69   statement withi
2faa0 6e 20 61 20 6d 75 6c 74 69 2d 73 74 61 74 65 6d  n a multi-statem
2fab0 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69  ent.** transacti
2fac0 6f 6e 20 28 65 72 72 6f 72 73 20 69 6e 63 6c 75  on (errors inclu
2fad0 64 69 6e 67 20 5b 53 51 4c 49 54 45 5f 46 55 4c  ding [SQLITE_FUL
2fae0 4c 5d 2c 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52  L], [SQLITE_IOER
2faf0 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e  R],.** [SQLITE_N
2fb00 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 45 5f 42  OMEM], [SQLITE_B
2fb10 55 53 59 5d 2c 20 61 6e 64 20 5b 53 51 4c 49 54  USY], and [SQLIT
2fb20 45 5f 49 4e 54 45 52 52 55 50 54 5d 29 20 74 68  E_INTERRUPT]) th
2fb30 65 6e 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 61  en the.** transa
2fb40 63 74 69 6f 6e 20 6d 69 67 68 74 20 62 65 20 72  ction might be r
2fb50 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75 74 6f 6d  olled back autom
2fb60 61 74 69 63 61 6c 6c 79 2e 20 20 54 68 65 20 6f  atically.  The o
2fb70 6e 6c 79 20 77 61 79 20 74 6f 0a 2a 2a 20 66 69  nly way to.** fi
2fb80 6e 64 20 6f 75 74 20 77 68 65 74 68 65 72 20 53  nd out whether S
2fb90 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63 61  QLite automatica
2fba0 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20  lly rolled back 
2fbb0 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
2fbc0 61 66 74 65 72 0a 2a 2a 20 61 6e 20 65 72 72 6f  after.** an erro
2fbd0 72 20 69 73 20 74 6f 20 75 73 65 20 74 68 69 73  r is to use this
2fbe0 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
2fbf0 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65   If another thre
2fc00 61 64 20 63 68 61 6e 67 65 73 20 74 68 65 20 61  ad changes the a
2fc10 75 74 6f 63 6f 6d 6d 69 74 20 73 74 61 74 75 73  utocommit status
2fc20 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
2fc30 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77  .** connection w
2fc40 68 69 6c 65 20 74 68 69 73 20 72 6f 75 74 69 6e  hile this routin
2fc50 65 20 69 73 20 72 75 6e 6e 69 6e 67 2c 20 74 68  e is running, th
2fc60 65 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  en the return va
2fc70 6c 75 65 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69  lue.** is undefi
2fc80 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ned..**.** Requi
2fc90 72 65 6d 65 6e 74 73 3a 20 5b 48 31 32 39 33 31  rements: [H12931
2fca0 5d 20 5b 48 31 32 39 33 32 5d 20 5b 48 31 32 39  ] [H12932] [H129
2fcb0 33 33 5d 20 5b 48 31 32 39 33 34 5d 0a 2a 2f 0a  33] [H12934].*/.
2fcc0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2fcd0 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63  qlite3_get_autoc
2fce0 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 2a 29 3b  ommit(sqlite3*);
2fcf0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
2fd00 3a 20 46 69 6e 64 20 54 68 65 20 44 61 74 61 62  : Find The Datab
2fd10 61 73 65 20 48 61 6e 64 6c 65 20 4f 66 20 41 20  ase Handle Of A 
2fd20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65  Prepared Stateme
2fd30 6e 74 20 7b 48 31 33 31 32 30 7d 20 3c 53 36 30  nt {H13120} <S60
2fd40 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  600>.**.** The s
2fd50 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65  qlite3_db_handle
2fd60 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
2fd70 6e 73 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  ns the [database
2fd80 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e   connection] han
2fd90 64 6c 65 0a 2a 2a 20 74 6f 20 77 68 69 63 68 20  dle.** to which 
2fda0 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  a [prepared stat
2fdb0 65 6d 65 6e 74 5d 20 62 65 6c 6f 6e 67 73 2e 20  ement] belongs. 
2fdc0 20 54 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   The [database c
2fdd0 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 72 65  onnection].** re
2fde0 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65  turned by sqlite
2fdf0 33 5f 64 62 5f 68 61 6e 64 6c 65 20 69 73 20 74  3_db_handle is t
2fe00 68 65 20 73 61 6d 65 20 5b 64 61 74 61 62 61 73  he same [databas
2fe10 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 68  e connection] th
2fe20 61 74 20 77 61 73 20 74 68 65 20 66 69 72 73 74  at was the first
2fe30 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20   argument.** to 
2fe40 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  the [sqlite3_pre
2fe50 70 61 72 65 5f 76 32 28 29 5d 20 63 61 6c 6c 20  pare_v2()] call 
2fe60 28 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73  (or its variants
2fe70 29 20 74 68 61 74 20 77 61 73 20 75 73 65 64 20  ) that was used 
2fe80 74 6f 0a 2a 2a 20 63 72 65 61 74 65 20 74 68 65  to.** create the
2fe90 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68   statement in th
2fea0 65 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a  e first place..*
2feb0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
2fec0 73 3a 20 5b 48 31 33 31 32 33 5d 0a 2a 2f 0a 53  s: [H13123].*/.S
2fed0 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
2fee0 33 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f 68 61  3 *sqlite3_db_ha
2fef0 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  ndle(sqlite3_stm
2ff00 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  t*);../*.** CAPI
2ff10 33 52 45 46 3a 20 46 69 6e 64 20 74 68 65 20 6e  3REF: Find the n
2ff20 65 78 74 20 70 72 65 70 61 72 65 64 20 73 74 61  ext prepared sta
2ff30 74 65 6d 65 6e 74 20 7b 48 31 33 31 34 30 7d 20  tement {H13140} 
2ff40 3c 53 36 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S60600>.**.** T
2ff50 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 72 65  his interface re
2ff60 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
2ff70 74 6f 20 74 68 65 20 6e 65 78 74 20 5b 70 72 65  to the next [pre
2ff80 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
2ff90 20 61 66 74 65 72 0a 2a 2a 20 70 53 74 6d 74 20   after.** pStmt 
2ffa0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
2ffb0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  the [database co
2ffc0 6e 6e 65 63 74 69 6f 6e 5d 20 70 44 62 2e 20 20  nnection] pDb.  
2ffd0 49 66 20 70 53 74 6d 74 20 69 73 20 4e 55 4c 4c  If pStmt is NULL
2ffe0 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 69 6e  .** then this in
2fff0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
30000 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
30010 20 66 69 72 73 74 20 70 72 65 70 61 72 65 64 20   first prepared 
30020 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61 73 73  statement.** ass
30030 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
30040 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
30050 74 69 6f 6e 20 70 44 62 2e 20 20 49 66 20 6e 6f  tion pDb.  If no
30060 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
30070 65 6e 74 0a 2a 2a 20 73 61 74 69 73 66 69 65 73  ent.** satisfies
30080 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20   the conditions 
30090 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c  of this routine,
300a0 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c   it returns NULL
300b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 64 61 74  ..**.** The [dat
300c0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
300d0 5d 20 70 6f 69 6e 74 65 72 20 44 20 69 6e 20 61  ] pointer D in a
300e0 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c   call to.** [sql
300f0 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 44  ite3_next_stmt(D
30100 2c 53 29 5d 20 6d 75 73 74 20 72 65 66 65 72 20  ,S)] must refer 
30110 74 6f 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 62  to an open datab
30120 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f  ase.** connectio
30130 6e 20 61 6e 64 20 69 6e 20 70 61 72 74 69 63 75  n and in particu
30140 6c 61 72 20 6d 75 73 74 20 6e 6f 74 20 62 65 20  lar must not be 
30150 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a  a NULL pointer..
30160 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
30170 74 73 3a 20 5b 48 31 33 31 34 33 5d 20 5b 48 31  ts: [H13143] [H1
30180 33 31 34 36 5d 20 5b 48 31 33 31 34 39 5d 20 5b  3146] [H13149] [
30190 48 31 33 31 35 32 5d 0a 2a 2f 0a 53 51 4c 49 54  H13152].*/.SQLIT
301a0 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 73 74  E_API sqlite3_st
301b0 6d 74 20 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74  mt *sqlite3_next
301c0 5f 73 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 70  _stmt(sqlite3 *p
301d0 44 62 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  Db, sqlite3_stmt
301e0 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a   *pStmt);../*.**
301f0 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 6d 69   CAPI3REF: Commi
30200 74 20 41 6e 64 20 52 6f 6c 6c 62 61 63 6b 20 4e  t And Rollback N
30210 6f 74 69 66 69 63 61 74 69 6f 6e 20 43 61 6c 6c  otification Call
30220 62 61 63 6b 73 20 7b 48 31 32 39 35 30 7d 20 3c  backs {H12950} <
30230 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S60400>.**.** Th
30240 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74  e sqlite3_commit
30250 5f 68 6f 6f 6b 28 29 20 69 6e 74 65 72 66 61 63  _hook() interfac
30260 65 20 72 65 67 69 73 74 65 72 73 20 61 20 63 61  e registers a ca
30270 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69  llback.** functi
30280 6f 6e 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64  on to be invoked
30290 20 77 68 65 6e 65 76 65 72 20 61 20 74 72 61 6e   whenever a tran
302a0 73 61 63 74 69 6f 6e 20 69 73 20 5b 43 4f 4d 4d  saction is [COMM
302b0 49 54 20 7c 20 63 6f 6d 6d 69 74 74 65 64 5d 2e  IT | committed].
302c0 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63 6b  .** Any callback
302d0 20 73 65 74 20 62 79 20 61 20 70 72 65 76 69 6f   set by a previo
302e0 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  us call to sqlit
302f0 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29  e3_commit_hook()
30300 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d 65  .** for the same
30310 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
30320 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 64  tion is overridd
30330 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  en..** The sqlit
30340 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b  e3_rollback_hook
30350 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 67  () interface reg
30360 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63  isters a callbac
30370 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 6f  k.** function to
30380 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e   be invoked when
30390 65 76 65 72 20 61 20 74 72 61 6e 73 61 63 74 69  ever a transacti
303a0 6f 6e 20 69 73 20 5b 52 4f 4c 4c 42 41 43 4b 20  on is [ROLLBACK 
303b0 7c 20 72 6f 6c 6c 65 64 20 62 61 63 6b 5d 2e 0a  | rolled back]..
303c0 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20  ** Any callback 
303d0 73 65 74 20 62 79 20 61 20 70 72 65 76 69 6f 75  set by a previou
303e0 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  s call to sqlite
303f0 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 0a  3_commit_hook().
30400 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20  ** for the same 
30410 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
30420 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 64 65  ion is overridde
30430 6e 2e 0a 2a 2a 20 54 68 65 20 70 41 72 67 20 61  n..** The pArg a
30440 72 67 75 6d 65 6e 74 20 69 73 20 70 61 73 73 65  rgument is passe
30450 64 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65  d through to the
30460 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 49 66   callback..** If
30470 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e   the callback on
30480 20 61 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 66   a commit hook f
30490 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
304a0 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65  non-zero,.** the
304b0 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 69 73 20  n the commit is 
304c0 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61  converted into a
304d0 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a   rollback..**.**
304e0 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75 6e 63   If another func
304f0 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69 6f 75  tion was previou
30500 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 2c 20  sly registered, 
30510 69 74 73 0a 2a 2a 20 70 41 72 67 20 76 61 6c 75  its.** pArg valu
30520 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  e is returned.  
30530 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 20 69  Otherwise NULL i
30540 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
30550 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 69  * The callback i
30560 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75  mplementation mu
30570 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69  st not do anythi
30580 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64  ng that will mod
30590 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62  ify.** the datab
305a0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  ase connection t
305b0 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20  hat invoked the 
305c0 63 61 6c 6c 62 61 63 6b 2e 20 20 41 6e 79 20 61  callback.  Any a
305d0 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d 6f 64  ctions.** to mod
305e0 69 66 79 20 74 68 65 20 64 61 74 61 62 61 73 65  ify the database
305f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75 73 74   connection must
30600 20 62 65 20 64 65 66 65 72 72 65 64 20 75 6e 74   be deferred unt
30610 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a 2a 20  il after the.** 
30620 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20 74 68  completion of th
30630 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  e [sqlite3_step(
30640 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69  )] call that tri
30650 67 67 65 72 65 64 20 74 68 65 20 63 6f 6d 6d 69  ggered the commi
30660 74 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 62 61 63 6b  t.** or rollback
30670 20 68 6f 6f 6b 20 69 6e 20 74 68 65 20 66 69 72   hook in the fir
30680 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 20 4e 6f 74  st place..** Not
30690 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f  e that [sqlite3_
306a0 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e  prepare_v2()] an
306b0 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  d [sqlite3_step(
306c0 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74  )] both modify t
306d0 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65  heir.** database
306e0 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72   connections for
306f0 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20   the meaning of 
30700 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73  "modify" in this
30710 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a   paragraph..**.*
30720 2a 20 52 65 67 69 73 74 65 72 69 6e 67 20 61 20  * Registering a 
30730 4e 55 4c 4c 20 66 75 6e 63 74 69 6f 6e 20 64 69  NULL function di
30740 73 61 62 6c 65 73 20 74 68 65 20 63 61 6c 6c 62  sables the callb
30750 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  ack..**.** When 
30760 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20  the commit hook 
30770 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65  callback routine
30780 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 2c 20 74   returns zero, t
30790 68 65 20 5b 43 4f 4d 4d 49 54 5d 0a 2a 2a 20 6f  he [COMMIT].** o
307a0 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6c 6c 6f  peration is allo
307b0 77 65 64 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20  wed to continue 
307c0 6e 6f 72 6d 61 6c 6c 79 2e 20 20 49 66 20 74 68  normally.  If th
307d0 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 0a 2a 2a  e commit hook.**
307e0 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
307f0 6f 2c 20 74 68 65 6e 20 74 68 65 20 5b 43 4f 4d  o, then the [COM
30800 4d 49 54 5d 20 69 73 20 63 6f 6e 76 65 72 74 65  MIT] is converte
30810 64 20 69 6e 74 6f 20 61 20 5b 52 4f 4c 4c 42 41  d into a [ROLLBA
30820 43 4b 5d 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c  CK]..** The roll
30830 62 61 63 6b 20 68 6f 6f 6b 20 69 73 20 69 6e 76  back hook is inv
30840 6f 6b 65 64 20 6f 6e 20 61 20 72 6f 6c 6c 62 61  oked on a rollba
30850 63 6b 20 74 68 61 74 20 72 65 73 75 6c 74 73 20  ck that results 
30860 66 72 6f 6d 20 61 20 63 6f 6d 6d 69 74 0a 2a 2a  from a commit.**
30870 20 68 6f 6f 6b 20 72 65 74 75 72 6e 69 6e 67 20   hook returning 
30880 6e 6f 6e 2d 7a 65 72 6f 2c 20 6a 75 73 74 20 61  non-zero, just a
30890 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20 77 69  s it would be wi
308a0 74 68 20 61 6e 79 20 6f 74 68 65 72 20 72 6f 6c  th any other rol
308b0 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  lback..**.** For
308c0 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66   the purposes of
308d0 20 74 68 69 73 20 41 50 49 2c 20 61 20 74 72 61   this API, a tra
308e0 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 61 69 64  nsaction is said
308f0 20 74 6f 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a   to have been.**
30900 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66 20   rolled back if 
30910 61 6e 20 65 78 70 6c 69 63 69 74 20 22 52 4f 4c  an explicit "ROL
30920 4c 42 41 43 4b 22 20 73 74 61 74 65 6d 65 6e 74  LBACK" statement
30930 20 69 73 20 65 78 65 63 75 74 65 64 2c 20 6f 72   is executed, or
30940 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 72 20  .** an error or 
30950 63 6f 6e 73 74 72 61 69 6e 74 20 63 61 75 73 65  constraint cause
30960 73 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 72 6f  s an implicit ro
30970 6c 6c 62 61 63 6b 20 74 6f 20 6f 63 63 75 72 2e  llback to occur.
30980 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b  .** The rollback
30990 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74   callback is not
309a0 20 69 6e 76 6f 6b 65 64 20 69 66 20 61 20 74 72   invoked if a tr
309b0 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20  ansaction is.** 
309c0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 6f  automatically ro
309d0 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61 75 73  lled back becaus
309e0 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  e the database c
309f0 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f  onnection is clo
30a00 73 65 64 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c  sed..** The roll
30a10 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20 69 73  back callback is
30a20 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66 20   not invoked if 
30a30 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
30a40 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20  .** rolled back 
30a50 62 65 63 61 75 73 65 20 61 20 63 6f 6d 6d 69 74  because a commit
30a60 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e   callback return
30a70 65 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 20  ed non-zero..** 
30a80 3c 74 6f 64 6f 3e 20 43 68 65 63 6b 20 6f 6e 20  <todo> Check on 
30a90 74 68 69 73 20 3c 2f 74 6f 64 6f 3e 0a 2a 2a 0a  this </todo>.**.
30aa0 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20  ** See also the 
30ab0 5b 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f  [sqlite3_update_
30ac0 68 6f 6f 6b 28 29 5d 20 69 6e 74 65 72 66 61 63  hook()] interfac
30ad0 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  e..**.** Require
30ae0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 39 35  ments:.** [H1295
30af0 31 5d 20 5b 48 31 32 39 35 32 5d 20 5b 48 31 32  1] [H12952] [H12
30b00 39 35 33 5d 20 5b 48 31 32 39 35 34 5d 20 5b 48  953] [H12954] [H
30b10 31 32 39 35 35 5d 0a 2a 2a 20 5b 48 31 32 39 36  12955].** [H1296
30b20 31 5d 20 5b 48 31 32 39 36 32 5d 20 5b 48 31 32  1] [H12962] [H12
30b30 39 36 33 5d 20 5b 48 31 32 39 36 34 5d 0a 2a 2f  963] [H12964].*/
30b40 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
30b50 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74   *sqlite3_commit
30b60 5f 68 6f 6f 6b 28 73 71 6c 69 74 65 33 2a 2c 20  _hook(sqlite3*, 
30b70 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 20 76  int(*)(void*), v
30b80 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  oid*);.SQLITE_AP
30b90 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  I void *sqlite3_
30ba0 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 73 71  rollback_hook(sq
30bb0 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 29 28  lite3*, void(*)(
30bc0 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64 2a 29 3b  void *), void*);
30bd0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
30be0 3a 20 44 61 74 61 20 43 68 61 6e 67 65 20 4e 6f  : Data Change No
30bf0 74 69 66 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62  tification Callb
30c00 61 63 6b 73 20 7b 48 31 32 39 37 30 7d 20 3c 53  acks {H12970} <S
30c10 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  60400>.**.** The
30c20 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f   sqlite3_update_
30c30 68 6f 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65  hook() interface
30c40 20 72 65 67 69 73 74 65 72 73 20 61 20 63 61 6c   registers a cal
30c50 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a  lback function.*
30c60 2a 20 77 69 74 68 20 74 68 65 20 5b 64 61 74 61  * with the [data
30c70 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
30c80 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74   identified by t
30c90 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
30ca0 74 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b  t.** to be invok
30cb0 65 64 20 77 68 65 6e 65 76 65 72 20 61 20 72 6f  ed whenever a ro
30cc0 77 20 69 73 20 75 70 64 61 74 65 64 2c 20 69 6e  w is updated, in
30cd0 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65 74 65  serted or delete
30ce0 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61  d..** Any callba
30cf0 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76  ck set by a prev
30d00 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 74 68 69  ious call to thi
30d10 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f  s function.** fo
30d20 72 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  r the same datab
30d30 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  ase connection i
30d40 73 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a  s overridden..**
30d50 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61  .** The second a
30d60 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69  rgument is a poi
30d70 6e 74 65 72 20 74 6f 20 74 68 65 20 66 75 6e 63  nter to the func
30d80 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 77  tion to invoke w
30d90 68 65 6e 20 61 0a 2a 2a 20 72 6f 77 20 69 73 20  hen a.** row is 
30da0 75 70 64 61 74 65 64 2c 20 69 6e 73 65 72 74 65  updated, inserte
30db0 64 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a  d or deleted..**
30dc0 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
30dd0 65 6e 74 20 74 6f 20 74 68 65 20 63 61 6c 6c 62  ent to the callb
30de0 61 63 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66  ack is a copy of
30df0 20 74 68 65 20 74 68 69 72 64 20 61 72 67 75 6d   the third argum
30e00 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65  ent.** to sqlite
30e10 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29 2e  3_update_hook().
30e20 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 63  .** The second c
30e30 61 6c 6c 62 61 63 6b 20 61 72 67 75 6d 65 6e 74  allback argument
30e40 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49   is one of [SQLI
30e50 54 45 5f 49 4e 53 45 52 54 5d 2c 20 5b 53 51 4c  TE_INSERT], [SQL
30e60 49 54 45 5f 44 45 4c 45 54 45 5d 2c 0a 2a 2a 20  ITE_DELETE],.** 
30e70 6f 72 20 5b 53 51 4c 49 54 45 5f 55 50 44 41 54  or [SQLITE_UPDAT
30e80 45 5d 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  E], depending on
30e90 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74   the operation t
30ea0 68 61 74 20 63 61 75 73 65 64 20 74 68 65 20 63  hat caused the c
30eb0 61 6c 6c 62 61 63 6b 0a 2a 2a 20 74 6f 20 62 65  allback.** to be
30ec0 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 54 68 65   invoked..** The
30ed0 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74   third and fourt
30ee0 68 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74  h arguments to t
30ef0 68 65 20 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74  he callback cont
30f00 61 69 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  ain pointers to 
30f10 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
30f20 61 6e 64 20 74 61 62 6c 65 20 6e 61 6d 65 20 63  and table name c
30f30 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 61 66  ontaining the af
30f40 66 65 63 74 65 64 20 72 6f 77 2e 0a 2a 2a 20 54  fected row..** T
30f50 68 65 20 66 69 6e 61 6c 20 63 61 6c 6c 62 61 63  he final callbac
30f60 6b 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  k parameter is t
30f70 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68  he [rowid] of th
30f80 65 20 72 6f 77 2e 0a 2a 2a 20 49 6e 20 74 68 65  e row..** In the
30f90 20 63 61 73 65 20 6f 66 20 61 6e 20 75 70 64 61   case of an upda
30fa0 74 65 2c 20 74 68 69 73 20 69 73 20 74 68 65 20  te, this is the 
30fb0 5b 72 6f 77 69 64 5d 20 61 66 74 65 72 20 74 68  [rowid] after th
30fc0 65 20 75 70 64 61 74 65 20 74 61 6b 65 73 20 70  e update takes p
30fd0 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  lace..**.** The 
30fe0 75 70 64 61 74 65 20 68 6f 6f 6b 20 69 73 20 6e  update hook is n
30ff0 6f 74 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20  ot invoked when 
31000 69 6e 74 65 72 6e 61 6c 20 73 79 73 74 65 6d 20  internal system 
31010 74 61 62 6c 65 73 20 61 72 65 0a 2a 2a 20 6d 6f  tables are.** mo
31020 64 69 66 69 65 64 20 28 69 2e 65 2e 20 73 71 6c  dified (i.e. sql
31030 69 74 65 5f 6d 61 73 74 65 72 20 61 6e 64 20 73  ite_master and s
31040 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 29 2e  qlite_sequence).
31050 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 63 75  .**.** In the cu
31060 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74 61  rrent implementa
31070 74 69 6f 6e 2c 20 74 68 65 20 75 70 64 61 74 65  tion, the update
31080 20 68 6f 6f 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20   hook.** is not 
31090 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 64 75 70  invoked when dup
310a0 6c 69 63 61 74 69 6f 6e 20 72 6f 77 73 20 61 72  lication rows ar
310b0 65 20 64 65 6c 65 74 65 64 20 62 65 63 61 75 73  e deleted becaus
310c0 65 20 6f 66 20 61 6e 0a 2a 2a 20 5b 4f 4e 20 43  e of an.** [ON C
310d0 4f 4e 46 4c 49 43 54 20 7c 20 4f 4e 20 43 4f 4e  ONFLICT | ON CON
310e0 46 4c 49 43 54 20 52 45 50 4c 41 43 45 5d 20 63  FLICT REPLACE] c
310f0 6c 61 75 73 65 2e 20 20 4e 6f 72 20 69 73 20 74  lause.  Nor is t
31100 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 0a 2a  he update hook.*
31110 2a 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 72  * invoked when r
31120 6f 77 73 20 61 72 65 20 64 65 6c 65 74 65 64 20  ows are deleted 
31130 75 73 69 6e 67 20 74 68 65 20 5b 74 72 75 6e 63  using the [trunc
31140 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ate optimization
31150 5d 2e 0a 2a 2a 20 54 68 65 20 65 78 63 65 70 74  ]..** The except
31160 69 6f 6e 73 20 64 65 66 69 6e 65 64 20 69 6e 20  ions defined in 
31170 74 68 69 73 20 70 61 72 61 67 72 61 70 68 20 6d  this paragraph m
31180 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e 20 61  ight change in a
31190 20 66 75 74 75 72 65 0a 2a 2a 20 72 65 6c 65 61   future.** relea
311a0 73 65 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a  se of SQLite..**
311b0 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65 20 68  .** The update h
311c0 6f 6f 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ook implementati
311d0 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61  on must not do a
311e0 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c  nything that wil
311f0 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20  l modify.** the 
31200 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
31210 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64  ion that invoked
31220 20 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b   the update hook
31230 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73 0a 2a  .  Any actions.*
31240 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20  * to modify the 
31250 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
31260 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65 66 65  ion must be defe
31270 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74 65 72  rred until after
31280 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69   the.** completi
31290 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  on of the [sqlit
312a0 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c 6c 20  e3_step()] call 
312b0 74 68 61 74 20 74 72 69 67 67 65 72 65 64 20 74  that triggered t
312c0 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 2e 0a  he update hook..
312d0 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71  ** Note that [sq
312e0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
312f0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
31300 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f  _step()] both mo
31310 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61  dify their.** da
31320 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
31330 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69  ns for the meani
31340 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69  ng of "modify" i
31350 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68  n this paragraph
31360 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68  ..**.** If anoth
31370 65 72 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20  er function was 
31380 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73  previously regis
31390 74 65 72 65 64 2c 20 69 74 73 20 70 41 72 67 20  tered, its pArg 
313a0 76 61 6c 75 65 0a 2a 2a 20 69 73 20 72 65 74 75  value.** is retu
313b0 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65  rned.  Otherwise
313c0 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
313d0 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  d..**.** See als
313e0 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63  o the [sqlite3_c
313f0 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 5d 20 61 6e  ommit_hook()] an
31400 64 20 5b 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62  d [sqlite3_rollb
31410 61 63 6b 5f 68 6f 6f 6b 28 29 5d 0a 2a 2a 20 69  ack_hook()].** i
31420 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a 0a 2a 2a  nterfaces..**.**
31430 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
31440 2a 20 5b 48 31 32 39 37 31 5d 20 5b 48 31 32 39  * [H12971] [H129
31450 37 33 5d 20 5b 48 31 32 39 37 35 5d 20 5b 48 31  73] [H12975] [H1
31460 32 39 37 37 5d 20 5b 48 31 32 39 37 39 5d 20 5b  2977] [H12979] [
31470 48 31 32 39 38 31 5d 20 5b 48 31 32 39 38 33 5d  H12981] [H12983]
31480 20 5b 48 31 32 39 38 36 5d 0a 2a 2f 0a 53 51 4c   [H12986].*/.SQL
31490 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
314a0 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f  lite3_update_hoo
314b0 6b 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a  k(.  sqlite3*, .
314c0 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a    void(*)(void *
314d0 2c 69 6e 74 20 2c 63 68 61 72 20 63 6f 6e 73 74  ,int ,char const
314e0 20 2a 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c   *,char const *,
314f0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 2c 0a  sqlite3_int64),.
31500 20 20 76 6f 69 64 2a 0a 29 3b 0a 0a 2f 2a 0a 2a    void*.);../*.*
31510 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62  * CAPI3REF: Enab
31520 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20 53 68  le Or Disable Sh
31530 61 72 65 64 20 50 61 67 65 72 20 43 61 63 68 65  ared Pager Cache
31540 20 7b 48 31 30 33 33 30 7d 20 3c 53 33 30 39 30   {H10330} <S3090
31550 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  0>.** KEYWORDS: 
31560 7b 73 68 61 72 65 64 20 63 61 63 68 65 7d 0a 2a  {shared cache}.*
31570 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
31580 65 20 65 6e 61 62 6c 65 73 20 6f 72 20 64 69 73  e enables or dis
31590 61 62 6c 65 73 20 74 68 65 20 73 68 61 72 69 6e  ables the sharin
315a0 67 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  g of the databas
315b0 65 20 63 61 63 68 65 0a 2a 2a 20 61 6e 64 20 73  e cache.** and s
315c0 63 68 65 6d 61 20 64 61 74 61 20 73 74 72 75 63  chema data struc
315d0 74 75 72 65 73 20 62 65 74 77 65 65 6e 20 5b 64  tures between [d
315e0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
315f0 6f 6e 20 7c 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  on | connections
31600 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 73 61 6d 65  ].** to the same
31610 20 64 61 74 61 62 61 73 65 2e 20 53 68 61 72 69   database. Shari
31620 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20 69 66  ng is enabled if
31630 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73   the argument is
31640 20 74 72 75 65 0a 2a 2a 20 61 6e 64 20 64 69 73   true.** and dis
31650 61 62 6c 65 64 20 69 66 20 74 68 65 20 61 72 67  abled if the arg
31660 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 65 2e 0a  ument is false..
31670 2a 2a 0a 2a 2a 20 43 61 63 68 65 20 73 68 61 72  **.** Cache shar
31680 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20 61  ing is enabled a
31690 6e 64 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20  nd disabled for 
316a0 61 6e 20 65 6e 74 69 72 65 20 70 72 6f 63 65 73  an entire proces
316b0 73 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20  s..** This is a 
316c0 63 68 61 6e 67 65 20 61 73 20 6f 66 20 53 51 4c  change as of SQL
316d0 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e  ite version 3.5.
316e0 30 2e 20 49 6e 20 70 72 69 6f 72 20 76 65 72 73  0. In prior vers
316f0 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2c 0a  ions of SQLite,.
31700 2a 2a 20 73 68 61 72 69 6e 67 20 77 61 73 20 65  ** sharing was e
31710 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c  nabled or disabl
31720 65 64 20 66 6f 72 20 65 61 63 68 20 74 68 72 65  ed for each thre
31730 61 64 20 73 65 70 61 72 61 74 65 6c 79 2e 0a 2a  ad separately..*
31740 2a 0a 2a 2a 20 54 68 65 20 63 61 63 68 65 20 73  *.** The cache s
31750 68 61 72 69 6e 67 20 6d 6f 64 65 20 73 65 74 20  haring mode set 
31760 62 79 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  by this interfac
31770 65 20 65 66 66 65 63 74 73 20 61 6c 6c 20 73 75  e effects all su
31780 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c  bsequent.** call
31790 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70  s to [sqlite3_op
317a0 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  en()], [sqlite3_
317b0 6f 70 65 6e 5f 76 32 28 29 5d 2c 20 61 6e 64 20  open_v2()], and 
317c0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28  [sqlite3_open16(
317d0 29 5d 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20  )]..** Existing 
317e0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
317f0 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75 73  ions continue us
31800 65 20 74 68 65 20 73 68 61 72 69 6e 67 20 6d 6f  e the sharing mo
31810 64 65 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 69  de.** that was i
31820 6e 20 65 66 66 65 63 74 20 61 74 20 74 68 65 20  n effect at the 
31830 74 69 6d 65 20 74 68 65 79 20 77 65 72 65 20 6f  time they were o
31840 70 65 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 69 72  pened..**.** Vir
31850 74 75 61 6c 20 74 61 62 6c 65 73 20 63 61 6e 6e  tual tables cann
31860 6f 74 20 62 65 20 75 73 65 64 20 77 69 74 68 20  ot be used with 
31870 61 20 73 68 61 72 65 64 20 63 61 63 68 65 2e 20  a shared cache. 
31880 20 57 68 65 6e 20 73 68 61 72 65 64 0a 2a 2a 20   When shared.** 
31890 63 61 63 68 65 20 69 73 20 65 6e 61 62 6c 65 64  cache is enabled
318a0 2c 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63  , the [sqlite3_c
318b0 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20  reate_module()] 
318c0 41 50 49 20 75 73 65 64 20 74 6f 20 72 65 67 69  API used to regi
318d0 73 74 65 72 0a 2a 2a 20 76 69 72 74 75 61 6c 20  ster.** virtual 
318e0 74 61 62 6c 65 73 20 77 69 6c 6c 20 61 6c 77 61  tables will alwa
318f0 79 73 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  ys return an err
31900 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  or..**.** This r
31910 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 5b  outine returns [
31920 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 66 20 73 68  SQLITE_OK] if sh
31930 61 72 65 64 20 63 61 63 68 65 20 77 61 73 20 65  ared cache was e
31940 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c  nabled or disabl
31950 65 64 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c  ed.** successful
31960 6c 79 2e 20 20 41 6e 20 5b 65 72 72 6f 72 20 63  ly.  An [error c
31970 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64  ode] is returned
31980 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a   otherwise..**.*
31990 2a 20 53 68 61 72 65 64 20 63 61 63 68 65 20 69  * Shared cache i
319a0 73 20 64 69 73 61 62 6c 65 64 20 62 79 20 64 65  s disabled by de
319b0 66 61 75 6c 74 2e 20 42 75 74 20 74 68 69 73 20  fault. But this 
319c0 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e 0a  might change in.
319d0 2a 2a 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  ** future releas
319e0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 41  es of SQLite.  A
319f0 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74  pplications that
31a00 20 63 61 72 65 20 61 62 6f 75 74 20 73 68 61 72   care about shar
31a10 65 64 0a 2a 2a 20 63 61 63 68 65 20 73 65 74 74  ed.** cache sett
31a20 69 6e 67 20 73 68 6f 75 6c 64 20 73 65 74 20 69  ing should set i
31a30 74 20 65 78 70 6c 69 63 69 74 6c 79 2e 0a 2a 2a  t explicitly..**
31a40 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20 20 5b  .** See Also:  [
31a50 53 51 4c 69 74 65 20 53 68 61 72 65 64 2d 43 61  SQLite Shared-Ca
31a60 63 68 65 20 4d 6f 64 65 5d 0a 2a 2a 0a 2a 2a 20  che Mode].**.** 
31a70 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48  Requirements: [H
31a80 31 30 33 33 31 5d 20 5b 48 31 30 33 33 36 5d 20  10331] [H10336] 
31a90 5b 48 31 30 33 33 37 5d 20 5b 48 31 30 33 33 39  [H10337] [H10339
31aa0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
31ab0 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62  int sqlite3_enab
31ac0 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 28  le_shared_cache(
31ad0 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  int);../*.** CAP
31ae0 49 33 52 45 46 3a 20 41 74 74 65 6d 70 74 20 54  I3REF: Attempt T
31af0 6f 20 46 72 65 65 20 48 65 61 70 20 4d 65 6d 6f  o Free Heap Memo
31b00 72 79 20 7b 48 31 37 33 34 30 7d 20 3c 53 33 30  ry {H17340} <S30
31b10 32 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  220>.**.** The s
31b20 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
31b30 65 6d 6f 72 79 28 29 20 69 6e 74 65 72 66 61 63  emory() interfac
31b40 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 66 72  e attempts to fr
31b50 65 65 20 4e 20 62 79 74 65 73 0a 2a 2a 20 6f 66  ee N bytes.** of
31b60 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 62 79 20   heap memory by 
31b70 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 6e 6f 6e  deallocating non
31b80 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f 72  -essential memor
31b90 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a  y allocations.**
31ba0 20 68 65 6c 64 20 62 79 20 74 68 65 20 64 61 74   held by the dat
31bb0 61 62 61 73 65 20 6c 69 62 72 61 72 79 2e 20 7b  abase library. {
31bc0 45 4e 44 7d 20 20 4d 65 6d 6f 72 79 20 75 73 65  END}  Memory use
31bd0 64 20 74 6f 20 63 61 63 68 65 20 64 61 74 61 62  d to cache datab
31be0 61 73 65 0a 2a 2a 20 70 61 67 65 73 20 74 6f 20  ase.** pages to 
31bf0 69 6d 70 72 6f 76 65 20 70 65 72 66 6f 72 6d 61  improve performa
31c00 6e 63 65 20 69 73 20 61 6e 20 65 78 61 6d 70 6c  nce is an exampl
31c10 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69  e of non-essenti
31c20 61 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 73 71  al memory..** sq
31c30 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65  lite3_release_me
31c40 6d 6f 72 79 28 29 20 72 65 74 75 72 6e 73 20 74  mory() returns t
31c50 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
31c60 65 73 20 61 63 74 75 61 6c 6c 79 20 66 72 65 65  es actually free
31c70 64 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 69 67 68  d,.** which migh
31c80 74 20 62 65 20 6d 6f 72 65 20 6f 72 20 6c 65 73  t be more or les
31c90 73 20 74 68 61 6e 20 74 68 65 20 61 6d 6f 75 6e  s than the amoun
31ca0 74 20 72 65 71 75 65 73 74 65 64 2e 0a 2a 2a 0a  t requested..**.
31cb0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
31cc0 20 5b 48 31 37 33 34 31 5d 20 5b 48 31 37 33 34   [H17341] [H1734
31cd0 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  2].*/.SQLITE_API
31ce0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 6c   int sqlite3_rel
31cf0 65 61 73 65 5f 6d 65 6d 6f 72 79 28 69 6e 74 29  ease_memory(int)
31d00 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
31d10 46 3a 20 49 6d 70 6f 73 65 20 41 20 4c 69 6d 69  F: Impose A Limi
31d20 74 20 4f 6e 20 48 65 61 70 20 53 69 7a 65 20 7b  t On Heap Size {
31d30 48 31 37 33 35 30 7d 20 3c 53 33 30 32 32 30 3e  H17350} <S30220>
31d40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
31d50 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d  e3_soft_heap_lim
31d60 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 20 70  it() interface p
31d70 6c 61 63 65 73 20 61 20 22 73 6f 66 74 22 20 6c  laces a "soft" l
31d80 69 6d 69 74 0a 2a 2a 20 6f 6e 20 74 68 65 20 61  imit.** on the a
31d90 6d 6f 75 6e 74 20 6f 66 20 68 65 61 70 20 6d 65  mount of heap me
31da0 6d 6f 72 79 20 74 68 61 74 20 6d 61 79 20 62 65  mory that may be
31db0 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 53 51   allocated by SQ
31dc0 4c 69 74 65 2e 0a 2a 2a 20 49 66 20 61 6e 20 69  Lite..** If an i
31dd0 6e 74 65 72 6e 61 6c 20 61 6c 6c 6f 63 61 74 69  nternal allocati
31de0 6f 6e 20 69 73 20 72 65 71 75 65 73 74 65 64 20  on is requested 
31df0 74 68 61 74 20 77 6f 75 6c 64 20 65 78 63 65 65  that would excee
31e00 64 20 74 68 65 0a 2a 2a 20 73 6f 66 74 20 68 65  d the.** soft he
31e10 61 70 20 6c 69 6d 69 74 2c 20 5b 73 71 6c 69 74  ap limit, [sqlit
31e20 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72  e3_release_memor
31e30 79 28 29 5d 20 69 73 20 69 6e 76 6f 6b 65 64 20  y()] is invoked 
31e40 6f 6e 65 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 74  one or.** more t
31e50 69 6d 65 73 20 74 6f 20 66 72 65 65 20 75 70 20  imes to free up 
31e60 73 6f 6d 65 20 73 70 61 63 65 20 62 65 66 6f 72  some space befor
31e70 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  e the allocation
31e80 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 0a 2a   is performed..*
31e90 2a 0a 2a 2a 20 54 68 65 20 6c 69 6d 69 74 20 69  *.** The limit i
31ea0 73 20 63 61 6c 6c 65 64 20 22 73 6f 66 74 22 2c  s called "soft",
31eb0 20 62 65 63 61 75 73 65 20 69 66 20 5b 73 71 6c   because if [sql
31ec0 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
31ed0 6f 72 79 28 29 5d 0a 2a 2a 20 63 61 6e 6e 6f 74  ory()].** cannot
31ee0 20 66 72 65 65 20 73 75 66 66 69 63 69 65 6e 74   free sufficient
31ef0 20 6d 65 6d 6f 72 79 20 74 6f 20 70 72 65 76 65   memory to preve
31f00 6e 74 20 74 68 65 20 6c 69 6d 69 74 20 66 72 6f  nt the limit fro
31f10 6d 20 62 65 69 6e 67 20 65 78 63 65 65 64 65 64  m being exceeded
31f20 2c 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20  ,.** the memory 
31f30 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 79  is allocated any
31f40 77 61 79 20 61 6e 64 20 74 68 65 20 63 75 72 72  way and the curr
31f50 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 70 72  ent operation pr
31f60 6f 63 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 20  oceeds..**.** A 
31f70 6e 65 67 61 74 69 76 65 20 6f 72 20 7a 65 72 6f  negative or zero
31f80 20 76 61 6c 75 65 20 66 6f 72 20 4e 20 6d 65 61   value for N mea
31f90 6e 73 20 74 68 61 74 20 74 68 65 72 65 20 69 73  ns that there is
31fa0 20 6e 6f 20 73 6f 66 74 20 68 65 61 70 20 6c 69   no soft heap li
31fb0 6d 69 74 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69  mit and.** [sqli
31fc0 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f  te3_release_memo
31fd0 72 79 28 29 5d 20 77 69 6c 6c 20 6f 6e 6c 79 20  ry()] will only 
31fe0 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 6d  be called when m
31ff0 65 6d 6f 72 79 20 69 73 20 65 78 68 61 75 73 74  emory is exhaust
32000 65 64 2e 0a 2a 2a 20 54 68 65 20 64 65 66 61 75  ed..** The defau
32010 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  lt value for the
32020 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74   soft heap limit
32030 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20   is zero..**.** 
32040 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 20 62  SQLite makes a b
32050 65 73 74 20 65 66 66 6f 72 74 20 74 6f 20 68 6f  est effort to ho
32060 6e 6f 72 20 74 68 65 20 73 6f 66 74 20 68 65 61  nor the soft hea
32070 70 20 6c 69 6d 69 74 2e 0a 2a 2a 20 42 75 74 20  p limit..** But 
32080 69 66 20 74 68 65 20 73 6f 66 74 20 68 65 61 70  if the soft heap
32090 20 6c 69 6d 69 74 20 63 61 6e 6e 6f 74 20 62 65   limit cannot be
320a0 20 68 6f 6e 6f 72 65 64 2c 20 65 78 65 63 75 74   honored, execut
320b0 69 6f 6e 20 77 69 6c 6c 0a 2a 2a 20 63 6f 6e 74  ion will.** cont
320c0 69 6e 75 65 20 77 69 74 68 6f 75 74 20 65 72 72  inue without err
320d0 6f 72 20 6f 72 20 6e 6f 74 69 66 69 63 61 74 69  or or notificati
320e0 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 77 68 79  on.  This is why
320f0 20 74 68 65 20 6c 69 6d 69 74 20 69 73 0a 2a 2a   the limit is.**
32100 20 63 61 6c 6c 65 64 20 61 20 22 73 6f 66 74 22   called a "soft"
32110 20 6c 69 6d 69 74 2e 20 20 49 74 20 69 73 20 61   limit.  It is a
32120 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a  dvisory only..**
32130 0a 2a 2a 20 50 72 69 6f 72 20 74 6f 20 53 51 4c  .** Prior to SQL
32140 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e  ite version 3.5.
32150 30 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  0, this routine 
32160 6f 6e 6c 79 20 63 6f 6e 73 74 72 61 69 6e 65 64  only constrained
32170 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61   the memory.** a
32180 6c 6c 6f 63 61 74 65 64 20 62 79 20 61 20 73 69  llocated by a si
32190 6e 67 6c 65 20 74 68 72 65 61 64 20 2d 20 74 68  ngle thread - th
321a0 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e  e same thread in
321b0 20 77 68 69 63 68 20 74 68 69 73 20 72 6f 75 74   which this rout
321c0 69 6e 65 0a 2a 2a 20 72 75 6e 73 2e 20 20 42 65  ine.** runs.  Be
321d0 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 51 4c  ginning with SQL
321e0 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e  ite version 3.5.
321f0 30 2c 20 74 68 65 20 73 6f 66 74 20 68 65 61 70  0, the soft heap
32200 20 6c 69 6d 69 74 20 69 73 0a 2a 2a 20 61 70 70   limit is.** app
32210 6c 69 65 64 20 74 6f 20 61 6c 6c 20 74 68 72 65  lied to all thre
32220 61 64 73 2e 20 54 68 65 20 76 61 6c 75 65 20 73  ads. The value s
32230 70 65 63 69 66 69 65 64 20 66 6f 72 20 74 68 65  pecified for the
32240 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74   soft heap limit
32250 0a 2a 2a 20 69 73 20 61 6e 20 75 70 70 65 72 20  .** is an upper 
32260 62 6f 75 6e 64 20 6f 6e 20 74 68 65 20 74 6f 74  bound on the tot
32270 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  al memory alloca
32280 74 69 6f 6e 20 66 6f 72 20 61 6c 6c 20 74 68 72  tion for all thr
32290 65 61 64 73 2e 20 49 6e 0a 2a 2a 20 76 65 72 73  eads. In.** vers
322a0 69 6f 6e 20 33 2e 35 2e 30 20 74 68 65 72 65 20  ion 3.5.0 there 
322b0 69 73 20 6e 6f 20 6d 65 63 68 61 6e 69 73 6d 20  is no mechanism 
322c0 66 6f 72 20 6c 69 6d 69 74 69 6e 67 20 74 68 65  for limiting the
322d0 20 68 65 61 70 20 75 73 61 67 65 20 66 6f 72 0a   heap usage for.
322e0 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 74 68  ** individual th
322f0 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  reads..**.** Req
32300 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
32310 31 36 33 35 31 5d 20 5b 48 31 36 33 35 32 5d 20  16351] [H16352] 
32320 5b 48 31 36 33 35 33 5d 20 5b 48 31 36 33 35 34  [H16353] [H16354
32330 5d 20 5b 48 31 36 33 35 35 5d 20 5b 48 31 36 33  ] [H16355] [H163
32340 35 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  58].*/.SQLITE_AP
32350 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73  I void sqlite3_s
32360 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 69  oft_heap_limit(i
32370 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  nt);../*.** CAPI
32380 33 52 45 46 3a 20 45 78 74 72 61 63 74 20 4d 65  3REF: Extract Me
32390 74 61 64 61 74 61 20 41 62 6f 75 74 20 41 20 43  tadata About A C
323a0 6f 6c 75 6d 6e 20 4f 66 20 41 20 54 61 62 6c 65  olumn Of A Table
323b0 20 7b 48 31 32 38 35 30 7d 20 3c 53 36 30 33 30   {H12850} <S6030
323c0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  0>.**.** This ro
323d0 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 6d 65  utine returns me
323e0 74 61 64 61 74 61 20 61 62 6f 75 74 20 61 20 73  tadata about a s
323f0 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e 20 6f  pecific column o
32400 66 20 61 20 73 70 65 63 69 66 69 63 0a 2a 2a 20  f a specific.** 
32410 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 61  database table a
32420 63 63 65 73 73 69 62 6c 65 20 75 73 69 6e 67 20  ccessible using 
32430 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  the [database co
32440 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65  nnection] handle
32450 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74 68  .** passed as th
32460 65 20 66 69 72 73 74 20 66 75 6e 63 74 69 6f 6e  e first function
32470 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
32480 20 54 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 69   The column is i
32490 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65  dentified by the
324a0 20 73 65 63 6f 6e 64 2c 20 74 68 69 72 64 20 61   second, third a
324b0 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65  nd fourth parame
324c0 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68 69 73 20  ters to.** this 
324d0 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 73 65  function. The se
324e0 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69  cond parameter i
324f0 73 20 65 69 74 68 65 72 20 74 68 65 20 6e 61 6d  s either the nam
32500 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
32510 65 0a 2a 2a 20 28 69 2e 65 2e 20 22 6d 61 69 6e  e.** (i.e. "main
32520 22 2c 20 22 74 65 6d 70 22 20 6f 72 20 61 6e 20  ", "temp" or an 
32530 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
32540 65 29 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  e) containing th
32550 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 74  e specified.** t
32560 61 62 6c 65 20 6f 72 20 4e 55 4c 4c 2e 20 49 66  able or NULL. If
32570 20 69 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65   it is NULL, the
32580 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64  n all attached d
32590 61 74 61 62 61 73 65 73 20 61 72 65 20 73 65 61  atabases are sea
325a0 72 63 68 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65  rched.** for the
325b0 20 74 61 62 6c 65 20 75 73 69 6e 67 20 74 68 65   table using the
325c0 20 73 61 6d 65 20 61 6c 67 6f 72 69 74 68 6d 20   same algorithm 
325d0 75 73 65 64 20 62 79 20 74 68 65 20 64 61 74 61  used by the data
325e0 62 61 73 65 20 65 6e 67 69 6e 65 20 74 6f 0a 2a  base engine to.*
325f0 2a 20 72 65 73 6f 6c 76 65 20 75 6e 71 75 61 6c  * resolve unqual
32600 69 66 69 65 64 20 74 61 62 6c 65 20 72 65 66 65  ified table refe
32610 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  rences..**.** Th
32620 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72  e third and four
32630 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  th parameters to
32640 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   this function a
32650 72 65 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64  re the table and
32660 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 20   column.** name 
32670 6f 66 20 74 68 65 20 64 65 73 69 72 65 64 20 63  of the desired c
32680 6f 6c 75 6d 6e 2c 20 72 65 73 70 65 63 74 69 76  olumn, respectiv
32690 65 6c 79 2e 20 4e 65 69 74 68 65 72 20 6f 66 20  ely. Neither of 
326a0 74 68 65 73 65 20 70 61 72 61 6d 65 74 65 72 73  these parameters
326b0 0a 2a 2a 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e  .** may be NULL.
326c0 0a 2a 2a 0a 2a 2a 20 4d 65 74 61 64 61 74 61 20  .**.** Metadata 
326d0 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 77  is returned by w
326e0 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 6d 65  riting to the me
326f0 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 70  mory locations p
32700 61 73 73 65 64 20 61 73 20 74 68 65 20 35 74 68  assed as the 5th
32710 0a 2a 2a 20 61 6e 64 20 73 75 62 73 65 71 75 65  .** and subseque
32720 6e 74 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  nt parameters to
32730 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20   this function. 
32740 41 6e 79 20 6f 66 20 74 68 65 73 65 20 61 72 67  Any of these arg
32750 75 6d 65 6e 74 73 20 6d 61 79 20 62 65 0a 2a 2a  uments may be.**
32760 20 4e 55 4c 4c 2c 20 69 6e 20 77 68 69 63 68 20   NULL, in which 
32770 63 61 73 65 20 74 68 65 20 63 6f 72 72 65 73 70  case the corresp
32780 6f 6e 64 69 6e 67 20 65 6c 65 6d 65 6e 74 20 6f  onding element o
32790 66 20 6d 65 74 61 64 61 74 61 20 69 73 20 6f 6d  f metadata is om
327a0 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c  itted..**.** <bl
327b0 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61  ockquote>.** <ta
327c0 62 6c 65 20 62 6f 72 64 65 72 3d 22 31 22 3e 0a  ble border="1">.
327d0 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20 50 61 72 61  ** <tr><th> Para
327e0 6d 65 74 65 72 20 3c 74 68 3e 20 4f 75 74 70 75  meter <th> Outpu
327f0 74 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e 20 20  t<br>Type <th>  
32800 44 65 73 63 72 69 70 74 69 6f 6e 0a 2a 2a 0a 2a  Description.**.*
32810 2a 20 3c 74 72 3e 3c 74 64 3e 20 35 74 68 20 3c  * <tr><td> 5th <
32820 74 64 3e 20 63 6f 6e 73 74 20 63 68 61 72 2a 20  td> const char* 
32830 3c 74 64 3e 20 44 61 74 61 20 74 79 70 65 0a 2a  <td> Data type.*
32840 2a 20 3c 74 72 3e 3c 74 64 3e 20 36 74 68 20 3c  * <tr><td> 6th <
32850 74 64 3e 20 63 6f 6e 73 74 20 63 68 61 72 2a 20  td> const char* 
32860 3c 74 64 3e 20 4e 61 6d 65 20 6f 66 20 64 65 66  <td> Name of def
32870 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  ault collation s
32880 65 71 75 65 6e 63 65 0a 2a 2a 20 3c 74 72 3e 3c  equence.** <tr><
32890 74 64 3e 20 37 74 68 20 3c 74 64 3e 20 69 6e 74  td> 7th <td> int
328a0 20 20 20 20 20 20 20 20 20 3c 74 64 3e 20 54 72           <td> Tr
328b0 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 68 61 73  ue if column has
328c0 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73   a NOT NULL cons
328d0 74 72 61 69 6e 74 0a 2a 2a 20 3c 74 72 3e 3c 74  traint.** <tr><t
328e0 64 3e 20 38 74 68 20 3c 74 64 3e 20 69 6e 74 20  d> 8th <td> int 
328f0 20 20 20 20 20 20 20 20 3c 74 64 3e 20 54 72 75          <td> Tru
32900 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 70  e if column is p
32910 61 72 74 20 6f 66 20 74 68 65 20 50 52 49 4d 41  art of the PRIMA
32920 52 59 20 4b 45 59 0a 2a 2a 20 3c 74 72 3e 3c 74  RY KEY.** <tr><t
32930 64 3e 20 39 74 68 20 3c 74 64 3e 20 69 6e 74 20  d> 9th <td> int 
32940 20 20 20 20 20 20 20 20 3c 74 64 3e 20 54 72 75          <td> Tru
32950 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 5b  e if column is [
32960 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 5d 0a 2a  AUTOINCREMENT].*
32970 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20 3c 2f  * </table>.** </
32980 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a  blockquote>.**.*
32990 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69  * The memory poi
329a0 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 63  nted to by the c
329b0 68 61 72 61 63 74 65 72 20 70 6f 69 6e 74 65 72  haracter pointer
329c0 73 20 72 65 74 75 72 6e 65 64 20 66 6f 72 20 74  s returned for t
329d0 68 65 0a 2a 2a 20 64 65 63 6c 61 72 61 74 69 6f  he.** declaratio
329e0 6e 20 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61  n type and colla
329f0 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73  tion sequence is
32a00 20 76 61 6c 69 64 20 6f 6e 6c 79 20 75 6e 74 69   valid only unti
32a10 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 63 61  l the next.** ca
32a20 6c 6c 20 74 6f 20 61 6e 79 20 53 51 4c 69 74 65  ll to any SQLite
32a30 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a   API function..*
32a40 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 70 65 63  *.** If the spec
32a50 69 66 69 65 64 20 74 61 62 6c 65 20 69 73 20 61  ified table is a
32a60 63 74 75 61 6c 6c 79 20 61 20 76 69 65 77 2c 20  ctually a view, 
32a70 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20  an [error code] 
32a80 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
32a90 2a 2a 20 49 66 20 74 68 65 20 73 70 65 63 69 66  ** If the specif
32aa0 69 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20 22 72  ied column is "r
32ab0 6f 77 69 64 22 2c 20 22 6f 69 64 22 20 6f 72 20  owid", "oid" or 
32ac0 22 5f 72 6f 77 69 64 5f 22 20 61 6e 64 20 61 6e  "_rowid_" and an
32ad0 0a 2a 2a 20 5b 49 4e 54 45 47 45 52 20 50 52 49  .** [INTEGER PRI
32ae0 4d 41 52 59 20 4b 45 59 5d 20 63 6f 6c 75 6d 6e  MARY KEY] column
32af0 20 68 61 73 20 62 65 65 6e 20 65 78 70 6c 69 63   has been explic
32b00 69 74 6c 79 20 64 65 63 6c 61 72 65 64 2c 20 74  itly declared, t
32b10 68 65 6e 20 74 68 65 20 6f 75 74 70 75 74 0a 2a  hen the output.*
32b20 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65  * parameters are
32b30 20 73 65 74 20 66 6f 72 20 74 68 65 20 65 78 70   set for the exp
32b40 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64  licitly declared
32b50 20 63 6f 6c 75 6d 6e 2e 20 49 66 20 74 68 65 72   column. If ther
32b60 65 20 69 73 20 6e 6f 0a 2a 2a 20 65 78 70 6c 69  e is no.** expli
32b70 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64 20 5b  citly declared [
32b80 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
32b90 4b 45 59 5d 20 63 6f 6c 75 6d 6e 2c 20 74 68 65  KEY] column, the
32ba0 6e 20 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20  n the output.** 
32bb0 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 73  parameters are s
32bc0 65 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  et as follows:.*
32bd0 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20 20 20  *.** <pre>.**   
32be0 20 20 64 61 74 61 20 74 79 70 65 3a 20 22 49 4e    data type: "IN
32bf0 54 45 47 45 52 22 0a 2a 2a 20 20 20 20 20 63 6f  TEGER".**     co
32c00 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
32c10 3a 20 22 42 49 4e 41 52 59 22 0a 2a 2a 20 20 20  : "BINARY".**   
32c20 20 20 6e 6f 74 20 6e 75 6c 6c 3a 20 30 0a 2a 2a    not null: 0.**
32c30 20 20 20 20 20 70 72 69 6d 61 72 79 20 6b 65 79       primary key
32c40 3a 20 31 0a 2a 2a 20 20 20 20 20 61 75 74 6f 20  : 1.**     auto 
32c50 69 6e 63 72 65 6d 65 6e 74 3a 20 30 0a 2a 2a 20  increment: 0.** 
32c60 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  </pre>.**.** Thi
32c70 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6c  s function may l
32c80 6f 61 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  oad one or more 
32c90 73 63 68 65 6d 61 73 20 66 72 6f 6d 20 64 61 74  schemas from dat
32ca0 61 62 61 73 65 20 66 69 6c 65 73 2e 20 49 66 20  abase files. If 
32cb0 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6f 63 63 75  an.** error occu
32cc0 72 73 20 64 75 72 69 6e 67 20 74 68 69 73 20 70  rs during this p
32cd0 72 6f 63 65 73 73 2c 20 6f 72 20 69 66 20 74 68  rocess, or if th
32ce0 65 20 72 65 71 75 65 73 74 65 64 20 74 61 62 6c  e requested tabl
32cf0 65 20 6f 72 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 63  e or column.** c
32d00 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20  annot be found, 
32d10 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20  an [error code] 
32d20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
32d30 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
32d40 20 6c 65 66 74 0a 2a 2a 20 69 6e 20 74 68 65 20   left.** in the 
32d50 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
32d60 74 69 6f 6e 5d 20 28 74 6f 20 62 65 20 72 65 74  tion] (to be ret
32d70 72 69 65 76 65 64 20 75 73 69 6e 67 20 73 71 6c  rieved using sql
32d80 69 74 65 33 5f 65 72 72 6d 73 67 28 29 29 2e 0a  ite3_errmsg())..
32d90 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 69  **.** This API i
32da0 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  s only available
32db0 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 20   if the library 
32dc0 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74  was compiled wit
32dd0 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45  h the.** [SQLITE
32de0 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d  _ENABLE_COLUMN_M
32df0 45 54 41 44 41 54 41 5d 20 43 2d 70 72 65 70 72  ETADATA] C-prepr
32e00 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c 20 64  ocessor symbol d
32e10 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  efined..*/.SQLIT
32e20 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
32e30 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d  3_table_column_m
32e40 65 74 61 64 61 74 61 28 0a 20 20 73 71 6c 69 74  etadata(.  sqlit
32e50 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
32e60 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63         /* Connec
32e70 74 69 6f 6e 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  tion handle */. 
32e80 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
32e90 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  Name,        /* 
32ea0 44 61 74 61 62 61 73 65 20 6e 61 6d 65 20 6f 72  Database name or
32eb0 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74   NULL */.  const
32ec0 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 4e 61 6d   char *zTableNam
32ed0 65 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20  e,     /* Table 
32ee0 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  name */.  const 
32ef0 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 4e 61 6d  char *zColumnNam
32f00 65 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20  e,    /* Column 
32f10 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61 72 20 63  name */.  char c
32f20 6f 6e 73 74 20 2a 2a 70 7a 44 61 74 61 54 79 70  onst **pzDataTyp
32f30 65 2c 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a  e,    /* OUTPUT:
32f40 20 44 65 63 6c 61 72 65 64 20 64 61 74 61 20 74   Declared data t
32f50 79 70 65 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f  ype */.  char co
32f60 6e 73 74 20 2a 2a 70 7a 43 6f 6c 6c 53 65 71 2c  nst **pzCollSeq,
32f70 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20       /* OUTPUT: 
32f80 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  Collation sequen
32f90 63 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74  ce name */.  int
32fa0 20 2a 70 4e 6f 74 4e 75 6c 6c 2c 20 20 20 20 20   *pNotNull,     
32fb0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50           /* OUTP
32fc0 55 54 3a 20 54 72 75 65 20 69 66 20 4e 4f 54 20  UT: True if NOT 
32fd0 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 20  NULL constraint 
32fe0 65 78 69 73 74 73 20 2a 2f 0a 20 20 69 6e 74 20  exists */.  int 
32ff0 2a 70 50 72 69 6d 61 72 79 4b 65 79 2c 20 20 20  *pPrimaryKey,   
33000 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55          /* OUTPU
33010 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d  T: True if colum
33020 6e 20 70 61 72 74 20 6f 66 20 50 4b 20 2a 2f 0a  n part of PK */.
33030 20 20 69 6e 74 20 2a 70 41 75 74 6f 69 6e 63 20    int *pAutoinc 
33040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
33050 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20 69 66   OUTPUT: True if
33060 20 63 6f 6c 75 6d 6e 20 69 73 20 61 75 74 6f 2d   column is auto-
33070 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 29 3b 0a  increment */.);.
33080 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
33090 20 4c 6f 61 64 20 41 6e 20 45 78 74 65 6e 73 69   Load An Extensi
330a0 6f 6e 20 7b 48 31 32 36 30 30 7d 20 3c 53 32 30  on {H12600} <S20
330b0 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  500>.**.** This 
330c0 69 6e 74 65 72 66 61 63 65 20 6c 6f 61 64 73 20  interface loads 
330d0 61 6e 20 53 51 4c 69 74 65 20 65 78 74 65 6e 73  an SQLite extens
330e0 69 6f 6e 20 6c 69 62 72 61 72 79 20 66 72 6f 6d  ion library from
330f0 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e   the named file.
33100 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 31 7d 20  .**.** {H12601} 
33110 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64  The sqlite3_load
33120 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 69 6e 74  _extension() int
33130 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20  erface attempts 
33140 74 6f 20 6c 6f 61 64 20 61 6e 0a 2a 2a 20 20 20  to load an.**   
33150 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 65 78         SQLite ex
33160 74 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 20  tension library 
33170 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65  contained in the
33180 20 66 69 6c 65 20 7a 46 69 6c 65 2e 0a 2a 2a 0a   file zFile..**.
33190 2a 2a 20 7b 48 31 32 36 30 32 7d 20 54 68 65 20  ** {H12602} The 
331a0 65 6e 74 72 79 20 70 6f 69 6e 74 20 69 73 20 7a  entry point is z
331b0 50 72 6f 63 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  Proc..**.** {H12
331c0 36 30 33 7d 20 7a 50 72 6f 63 20 6d 61 79 20 62  603} zProc may b
331d0 65 20 30 2c 20 69 6e 20 77 68 69 63 68 20 63 61  e 0, in which ca
331e0 73 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  se the name of t
331f0 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 0a 2a  he entry point.*
33200 2a 20 20 20 20 20 20 20 20 20 20 64 65 66 61 75  *          defau
33210 6c 74 73 20 74 6f 20 22 73 71 6c 69 74 65 33 5f  lts to "sqlite3_
33220 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 2e  extension_init".
33230 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 34 7d 20  .**.** {H12604} 
33240 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64  The sqlite3_load
33250 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 69 6e 74  _extension() int
33260 65 72 66 61 63 65 20 73 68 61 6c 6c 20 72 65 74  erface shall ret
33270 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  urn.**          
33280 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73  [SQLITE_OK] on s
33290 75 63 63 65 73 73 20 61 6e 64 20 5b 53 51 4c 49  uccess and [SQLI
332a0 54 45 5f 45 52 52 4f 52 5d 20 69 66 20 73 6f 6d  TE_ERROR] if som
332b0 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e  ething goes wron
332c0 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 35  g..**.** {H12605
332d0 7d 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  } If an error oc
332e0 63 75 72 73 20 61 6e 64 20 70 7a 45 72 72 4d 73  curs and pzErrMs
332f0 67 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e  g is not 0, then
33300 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
33310 20 5b 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65   [sqlite3_load_e
33320 78 74 65 6e 73 69 6f 6e 28 29 5d 20 69 6e 74 65  xtension()] inte
33330 72 66 61 63 65 20 73 68 61 6c 6c 20 61 74 74 65  rface shall atte
33340 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20  mpt to.**       
33350 20 20 20 66 69 6c 6c 20 2a 70 7a 45 72 72 4d 73     fill *pzErrMs
33360 67 20 77 69 74 68 20 65 72 72 6f 72 20 6d 65 73  g with error mes
33370 73 61 67 65 20 74 65 78 74 20 73 74 6f 72 65 64  sage text stored
33380 20 69 6e 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20   in memory.**   
33390 20 20 20 20 20 20 20 6f 62 74 61 69 6e 65 64 20         obtained 
333a0 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61  from [sqlite3_ma
333b0 6c 6c 6f 63 28 29 5d 2e 20 7b 45 4e 44 7d 20 20  lloc()]. {END}  
333c0 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  The calling func
333d0 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  tion.**         
333e0 20 73 68 6f 75 6c 64 20 66 72 65 65 20 74 68 69   should free thi
333f0 73 20 6d 65 6d 6f 72 79 20 62 79 20 63 61 6c 6c  s memory by call
33400 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 72 65  ing [sqlite3_fre
33410 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  e()]..**.** {H12
33420 36 30 36 7d 20 45 78 74 65 6e 73 69 6f 6e 20 6c  606} Extension l
33430 6f 61 64 69 6e 67 20 6d 75 73 74 20 62 65 20 65  oading must be e
33440 6e 61 62 6c 65 64 20 75 73 69 6e 67 0a 2a 2a 20  nabled using.** 
33450 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
33460 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78  3_enable_load_ex
33470 74 65 6e 73 69 6f 6e 28 29 5d 20 70 72 69 6f 72  tension()] prior
33480 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73   to calling this
33490 20 41 50 49 2c 0a 2a 2a 20 20 20 20 20 20 20 20   API,.**        
334a0 20 20 6f 74 68 65 72 77 69 73 65 20 61 6e 20 65    otherwise an e
334b0 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 72 65 74  rror will be ret
334c0 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  urned..*/.SQLITE
334d0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
334e0 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28  _load_extension(
334f0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
33500 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61 64           /* Load
33510 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69   the extension i
33520 6e 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73  nto this databas
33530 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
33540 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
33550 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ile,    /* Name 
33560 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 6c 69  of the shared li
33570 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67  brary containing
33580 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20   extension */.  
33590 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 6f  const char *zPro
335a0 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72 79 20 70  c,    /* Entry p
335b0 6f 69 6e 74 2e 20 20 44 65 72 69 76 65 64 20 66  oint.  Derived f
335c0 72 6f 6d 20 7a 46 69 6c 65 20 69 66 20 30 20 2a  rom zFile if 0 *
335d0 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  /.  char **pzErr
335e0 4d 73 67 20 20 20 20 20 20 20 2f 2a 20 50 75 74  Msg       /* Put
335f0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68   error message h
33600 65 72 65 20 69 66 20 6e 6f 74 20 30 20 2a 2f 0a  ere if not 0 */.
33610 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
33620 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 20 44 69  EF: Enable Or Di
33630 73 61 62 6c 65 20 45 78 74 65 6e 73 69 6f 6e 20  sable Extension 
33640 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 32 30 7d  Loading {H12620}
33650 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S20500>.**.** 
33660 53 6f 20 61 73 20 6e 6f 74 20 74 6f 20 6f 70 65  So as not to ope
33670 6e 20 73 65 63 75 72 69 74 79 20 68 6f 6c 65 73  n security holes
33680 20 69 6e 20 6f 6c 64 65 72 20 61 70 70 6c 69 63   in older applic
33690 61 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65 0a  ations that are.
336a0 2a 2a 20 75 6e 70 72 65 70 61 72 65 64 20 74 6f  ** unprepared to
336b0 20 64 65 61 6c 20 77 69 74 68 20 65 78 74 65 6e   deal with exten
336c0 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 2c 20 61 6e  sion loading, an
336d0 64 20 61 73 20 61 20 6d 65 61 6e 73 20 6f 66 20  d as a means of 
336e0 64 69 73 61 62 6c 69 6e 67 0a 2a 2a 20 65 78 74  disabling.** ext
336f0 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 77  ension loading w
33700 68 69 6c 65 20 65 76 61 6c 75 61 74 69 6e 67 20  hile evaluating 
33710 75 73 65 72 2d 65 6e 74 65 72 65 64 20 53 51 4c  user-entered SQL
33720 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  , the following 
33730 41 50 49 0a 2a 2a 20 69 73 20 70 72 6f 76 69 64  API.** is provid
33740 65 64 20 74 6f 20 74 75 72 6e 20 74 68 65 20 5b  ed to turn the [
33750 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74  sqlite3_load_ext
33760 65 6e 73 69 6f 6e 28 29 5d 20 6d 65 63 68 61 6e  ension()] mechan
33770 69 73 6d 20 6f 6e 20 61 6e 64 20 6f 66 66 2e 0a  ism on and off..
33780 2a 2a 0a 2a 2a 20 45 78 74 65 6e 73 69 6f 6e 20  **.** Extension 
33790 6c 6f 61 64 69 6e 67 20 69 73 20 6f 66 66 20 62  loading is off b
337a0 79 20 64 65 66 61 75 6c 74 2e 20 53 65 65 20 74  y default. See t
337b0 69 63 6b 65 74 20 23 31 38 36 33 2e 0a 2a 2a 0a  icket #1863..**.
337c0 2a 2a 20 7b 48 31 32 36 32 31 7d 20 43 61 6c 6c  ** {H12621} Call
337d0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 65 6e 61   the sqlite3_ena
337e0 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69  ble_load_extensi
337f0 6f 6e 28 29 20 72 6f 75 74 69 6e 65 20 77 69 74  on() routine wit
33800 68 20 6f 6e 6f 66 66 3d 3d 31 0a 2a 2a 20 20 20  h onoff==1.**   
33810 20 20 20 20 20 20 20 74 6f 20 74 75 72 6e 20 65         to turn e
33820 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67  xtension loading
33830 20 6f 6e 20 61 6e 64 20 63 61 6c 6c 20 69 74 20   on and call it 
33840 77 69 74 68 20 6f 6e 6f 66 66 3d 3d 30 20 74 6f  with onoff==0 to
33850 20 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20   turn.**        
33860 20 20 69 74 20 62 61 63 6b 20 6f 66 66 20 61 67    it back off ag
33870 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36  ain..**.** {H126
33880 32 32 7d 20 45 78 74 65 6e 73 69 6f 6e 20 6c 6f  22} Extension lo
33890 61 64 69 6e 67 20 69 73 20 6f 66 66 20 62 79 20  ading is off by 
338a0 64 65 66 61 75 6c 74 2e 0a 2a 2f 0a 53 51 4c 49  default..*/.SQLI
338b0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
338c0 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65  e3_enable_load_e
338d0 78 74 65 6e 73 69 6f 6e 28 73 71 6c 69 74 65 33  xtension(sqlite3
338e0 20 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f 66 66 29   *db, int onoff)
338f0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
33900 46 3a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79  F: Automatically
33910 20 4c 6f 61 64 20 41 6e 20 45 78 74 65 6e 73 69   Load An Extensi
33920 6f 6e 73 20 7b 48 31 32 36 34 30 7d 20 3c 53 32  ons {H12640} <S2
33930 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  0500>.**.** This
33940 20 41 50 49 20 63 61 6e 20 62 65 20 69 6e 76 6f   API can be invo
33950 6b 65 64 20 61 74 20 70 72 6f 67 72 61 6d 20 73  ked at program s
33960 74 61 72 74 75 70 20 69 6e 20 6f 72 64 65 72 20  tartup in order 
33970 74 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6f  to register.** o
33980 6e 65 20 6f 72 20 6d 6f 72 65 20 73 74 61 74 69  ne or more stati
33990 63 61 6c 6c 79 20 6c 69 6e 6b 65 64 20 65 78 74  cally linked ext
339a0 65 6e 73 69 6f 6e 73 20 74 68 61 74 20 77 69 6c  ensions that wil
339b0 6c 20 62 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a  l be available.*
339c0 2a 20 74 6f 20 61 6c 6c 20 6e 65 77 20 5b 64 61  * to all new [da
339d0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
339e0 6e 73 5d 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a  ns]. {END}.**.**
339f0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74   This routine st
33a00 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74  ores a pointer t
33a10 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20  o the extension 
33a20 69 6e 20 61 6e 20 61 72 72 61 79 20 74 68 61 74  in an array that
33a30 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20   is.** obtained 
33a40 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61  from [sqlite3_ma
33a50 6c 6c 6f 63 28 29 5d 2e 20 20 49 66 20 79 6f 75  lloc()].  If you
33a60 20 72 75 6e 20 61 20 6d 65 6d 6f 72 79 20 6c 65   run a memory le
33a70 61 6b 20 63 68 65 63 6b 65 72 0a 2a 2a 20 6f 6e  ak checker.** on
33a80 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 20 61 6e   your program an
33a90 64 20 69 74 20 72 65 70 6f 72 74 73 20 61 20 6c  d it reports a l
33aa0 65 61 6b 20 62 65 63 61 75 73 65 20 6f 66 20 74  eak because of t
33ab0 68 69 73 20 61 72 72 61 79 2c 20 69 6e 76 6f 6b  his array, invok
33ac0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65  e.** [sqlite3_re
33ad0 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69  set_auto_extensi
33ae0 6f 6e 28 29 5d 20 70 72 69 6f 72 20 74 6f 20 73  on()] prior to s
33af0 68 75 74 64 6f 77 6e 20 74 6f 20 66 72 65 65 20  hutdown to free 
33b00 74 68 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a  the memory..**.*
33b10 2a 20 7b 48 31 32 36 34 31 7d 20 54 68 69 73 20  * {H12641} This 
33b20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65  function registe
33b30 72 73 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 20  rs an extension 
33b40 65 6e 74 72 79 20 70 6f 69 6e 74 20 74 68 61 74  entry point that
33b50 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   is.**          
33b60 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 6e  automatically in
33b70 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20 61  voked whenever a
33b80 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65 20 63   new [database c
33b90 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 20 20  onnection].**   
33ba0 20 20 20 20 20 20 20 69 73 20 6f 70 65 6e 65 64         is opened
33bb0 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
33bc0 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65  open()], [sqlite
33bd0 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 0a 2a 2a 20  3_open16()],.** 
33be0 20 20 20 20 20 20 20 20 20 6f 72 20 5b 73 71 6c           or [sql
33bf0 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e  ite3_open_v2()].
33c00 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 32 7d 20  .**.** {H12642} 
33c10 44 75 70 6c 69 63 61 74 65 20 65 78 74 65 6e 73  Duplicate extens
33c20 69 6f 6e 73 20 61 72 65 20 64 65 74 65 63 74 65  ions are detecte
33c30 64 20 73 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69  d so calling thi
33c40 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 20 20 20  s routine.**    
33c50 20 20 20 20 20 20 6d 75 6c 74 69 70 6c 65 20 74        multiple t
33c60 69 6d 65 73 20 77 69 74 68 20 74 68 65 20 73 61  imes with the sa
33c70 6d 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20  me extension is 
33c80 68 61 72 6d 6c 65 73 73 2e 0a 2a 2a 0a 2a 2a 20  harmless..**.** 
33c90 7b 48 31 32 36 34 33 7d 20 54 68 69 73 20 72 6f  {H12643} This ro
33ca0 75 74 69 6e 65 20 73 74 6f 72 65 73 20 61 20 70  utine stores a p
33cb0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65 78  ointer to the ex
33cc0 74 65 6e 73 69 6f 6e 20 69 6e 20 61 6e 20 61 72  tension in an ar
33cd0 72 61 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ray.**          
33ce0 74 68 61 74 20 69 73 20 6f 62 74 61 69 6e 65 64  that is obtained
33cf0 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d   from [sqlite3_m
33d00 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  alloc()]..**.** 
33d10 7b 48 31 32 36 34 34 7d 20 41 75 74 6f 6d 61 74  {H12644} Automat
33d20 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 20 61 70  ic extensions ap
33d30 70 6c 79 20 61 63 72 6f 73 73 20 61 6c 6c 20 74  ply across all t
33d40 68 72 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54  hreads..*/.SQLIT
33d50 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
33d60 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e  3_auto_extension
33d70 28 76 6f 69 64 20 28 2a 78 45 6e 74 72 79 50 6f  (void (*xEntryPo
33d80 69 6e 74 29 28 76 6f 69 64 29 29 3b 0a 0a 2f 2a  int)(void));../*
33d90 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65  .** CAPI3REF: Re
33da0 73 65 74 20 41 75 74 6f 6d 61 74 69 63 20 45 78  set Automatic Ex
33db0 74 65 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e 67 20  tension Loading 
33dc0 7b 48 31 32 36 36 30 7d 20 3c 53 32 30 35 30 30  {H12660} <S20500
33dd0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  >.**.** This fun
33de0 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 61  ction disables a
33df0 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65  ll previously re
33e00 67 69 73 74 65 72 65 64 20 61 75 74 6f 6d 61 74  gistered automat
33e10 69 63 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 73  ic.** extensions
33e20 2e 20 7b 45 4e 44 7d 20 20 49 74 20 75 6e 64 6f  . {END}  It undo
33e30 65 73 20 74 68 65 20 65 66 66 65 63 74 20 6f 66  es the effect of
33e40 20 61 6c 6c 20 70 72 69 6f 72 0a 2a 2a 20 5b 73   all prior.** [s
33e50 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78 74 65  qlite3_auto_exte
33e60 6e 73 69 6f 6e 28 29 5d 20 63 61 6c 6c 73 2e 0a  nsion()] calls..
33e70 2a 2a 0a 2a 2a 20 7b 48 31 32 36 36 31 7d 20 54  **.** {H12661} T
33e80 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 69 73  his function dis
33e90 61 62 6c 65 73 20 61 6c 6c 20 70 72 65 76 69 6f  ables all previo
33ea0 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 0a  usly registered.
33eb0 2a 2a 20 20 20 20 20 20 20 20 20 20 61 75 74 6f  **          auto
33ec0 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73  matic extensions
33ed0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 36 32 7d  ..**.** {H12662}
33ee0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64   This function d
33ef0 69 73 61 62 6c 65 73 20 61 75 74 6f 6d 61 74 69  isables automati
33f00 63 20 65 78 74 65 6e 73 69 6f 6e 73 20 69 6e 20  c extensions in 
33f10 61 6c 6c 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a  all threads..*/.
33f20 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
33f30 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75  sqlite3_reset_au
33f40 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69  to_extension(voi
33f50 64 29 3b 0a 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 20 45  d);../*.****** E
33f60 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20 73 75  XPERIMENTAL - su
33f70 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20  bject to change 
33f80 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 20 2a  without notice *
33f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
33fa0 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63  .** The interfac
33fb0 65 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c  e to the virtual
33fc0 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d  -table mechanism
33fd0 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 63 6f   is currently co
33fe0 6e 73 69 64 65 72 65 64 0a 2a 2a 20 74 6f 20 62  nsidered.** to b
33ff0 65 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20  e experimental. 
34000 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 6d   The interface m
34010 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e 20 69  ight change in i
34020 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77 61 79 73  ncompatible ways
34030 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20  ..** If this is 
34040 61 20 70 72 6f 62 6c 65 6d 20 66 6f 72 20 79 6f  a problem for yo
34050 75 2c 20 64 6f 20 6e 6f 74 20 75 73 65 20 74 68  u, do not use th
34060 65 20 69 6e 74 65 72 66 61 63 65 20 61 74 20 74  e interface at t
34070 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20  his time..**.** 
34080 57 68 65 6e 20 74 68 65 20 76 69 72 74 75 61 6c  When the virtual
34090 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d  -table mechanism
340a0 20 73 74 61 62 69 6c 69 7a 65 73 2c 20 77 65 20   stabilizes, we 
340b0 77 69 6c 6c 20 64 65 63 6c 61 72 65 20 74 68 65  will declare the
340c0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 66 69  .** interface fi
340d0 78 65 64 2c 20 73 75 70 70 6f 72 74 20 69 74 20  xed, support it 
340e0 69 6e 64 65 66 69 6e 69 74 65 6c 79 2c 20 61 6e  indefinitely, an
340f0 64 20 72 65 6d 6f 76 65 20 74 68 69 73 20 63 6f  d remove this co
34100 6d 6d 65 6e 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  mment..*/../*.**
34110 20 53 74 72 75 63 74 75 72 65 73 20 75 73 65 64   Structures used
34120 20 62 79 20 74 68 65 20 76 69 72 74 75 61 6c 20   by the virtual 
34130 74 61 62 6c 65 20 69 6e 74 65 72 66 61 63 65 0a  table interface.
34140 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
34150 74 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73  t sqlite3_vtab s
34160 71 6c 69 74 65 33 5f 76 74 61 62 3b 0a 74 79 70  qlite3_vtab;.typ
34170 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
34180 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73  te3_index_info s
34190 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
341a0 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  o;.typedef struc
341b0 74 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  t sqlite3_vtab_c
341c0 75 72 73 6f 72 20 73 71 6c 69 74 65 33 5f 76 74  ursor sqlite3_vt
341d0 61 62 5f 63 75 72 73 6f 72 3b 0a 74 79 70 65 64  ab_cursor;.typed
341e0 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
341f0 33 5f 6d 6f 64 75 6c 65 20 73 71 6c 69 74 65 33  3_module sqlite3
34200 5f 6d 6f 64 75 6c 65 3b 0a 0a 2f 2a 0a 2a 2a 20  _module;../*.** 
34210 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61  CAPI3REF: Virtua
34220 6c 20 54 61 62 6c 65 20 4f 62 6a 65 63 74 20 7b  l Table Object {
34230 48 31 38 30 30 30 7d 20 3c 53 32 30 34 30 30 3e  H18000} <S20400>
34240 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71  .** KEYWORDS: sq
34250 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 76 69  lite3_module {vi
34260 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
34270 6c 65 7d 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  le}.** EXPERIMEN
34280 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73  TAL.**.** This s
34290 74 72 75 63 74 75 72 65 2c 20 73 6f 6d 65 74 69  tructure, someti
342a0 6d 65 73 20 63 61 6c 6c 65 64 20 61 20 61 20 22  mes called a a "
342b0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
342c0 64 75 6c 65 22 2c 20 0a 2a 2a 20 64 65 66 69 6e  dule", .** defin
342d0 65 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  es the implement
342e0 61 74 69 6f 6e 20 6f 66 20 61 20 5b 76 69 72 74  ation of a [virt
342f0 75 61 6c 20 74 61 62 6c 65 73 5d 2e 20 20 0a 2a  ual tables].  .*
34300 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65  * This structure
34310 20 63 6f 6e 73 69 73 74 73 20 6d 6f 73 74 6c 79   consists mostly
34320 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20   of methods for 
34330 74 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a  the module..**.*
34340 2a 20 41 20 76 69 72 74 75 61 6c 20 74 61 62 6c  * A virtual tabl
34350 65 20 6d 6f 64 75 6c 65 20 69 73 20 63 72 65 61  e module is crea
34360 74 65 64 20 62 79 20 66 69 6c 6c 69 6e 67 20 69  ted by filling i
34370 6e 20 61 20 70 65 72 73 69 73 74 65 6e 74 0a 2a  n a persistent.*
34380 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  * instance of th
34390 69 73 20 73 74 72 75 63 74 75 72 65 20 61 6e 64  is structure and
343a0 20 70 61 73 73 69 6e 67 20 61 20 70 6f 69 6e 74   passing a point
343b0 65 72 20 74 6f 20 74 68 61 74 20 69 6e 73 74 61  er to that insta
343c0 6e 63 65 0a 2a 2a 20 74 6f 20 5b 73 71 6c 69 74  nce.** to [sqlit
343d0 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
343e0 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
343f0 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32  create_module_v2
34400 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 72 65 67 69  ()]..** The regi
34410 73 74 72 61 74 69 6f 6e 20 72 65 6d 61 69 6e 73  stration remains
34420 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 69 74 20   valid until it 
34430 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 61  is replaced by a
34440 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 6d 6f   different.** mo
34450 64 75 6c 65 20 6f 72 20 75 6e 74 69 6c 20 74 68  dule or until th
34460 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
34470 65 63 74 69 6f 6e 5d 20 63 6c 6f 73 65 73 2e 20  ection] closes. 
34480 20 54 68 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20   The content.** 
34490 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
344a0 65 20 6d 75 73 74 20 6e 6f 74 20 63 68 61 6e 67  e must not chang
344b0 65 20 77 68 69 6c 65 20 69 74 20 69 73 20 72 65  e while it is re
344c0 67 69 73 74 65 72 65 64 20 77 69 74 68 0a 2a 2a  gistered with.**
344d0 20 61 6e 79 20 64 61 74 61 62 61 73 65 20 63 6f   any database co
344e0 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72  nnection..*/.str
344f0 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  uct sqlite3_modu
34500 6c 65 20 7b 0a 20 20 69 6e 74 20 69 56 65 72 73  le {.  int iVers
34510 69 6f 6e 3b 0a 20 20 69 6e 74 20 28 2a 78 43 72  ion;.  int (*xCr
34520 65 61 74 65 29 28 73 71 6c 69 74 65 33 2a 2c 20  eate)(sqlite3*, 
34530 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 20 20  void *pAux,.    
34540 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 61             int a
34550 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rgc, const char 
34560 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 20  *const*argv,.   
34570 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
34580 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61  te3_vtab **ppVTa
34590 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e  b, char**);.  in
345a0 74 20 28 2a 78 43 6f 6e 6e 65 63 74 29 28 73 71  t (*xConnect)(sq
345b0 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a 70 41  lite3*, void *pA
345c0 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ux,.            
345d0 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e     int argc, con
345e0 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61  st char *const*a
345f0 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rgv,.           
34600 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
34610 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 72 2a   **ppVTab, char*
34620 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42 65 73  *);.  int (*xBes
34630 74 49 6e 64 65 78 29 28 73 71 6c 69 74 65 33 5f  tIndex)(sqlite3_
34640 76 74 61 62 20 2a 70 56 54 61 62 2c 20 73 71 6c  vtab *pVTab, sql
34650 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 2a  ite3_index_info*
34660 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 69 73 63  );.  int (*xDisc
34670 6f 6e 6e 65 63 74 29 28 73 71 6c 69 74 65 33 5f  onnect)(sqlite3_
34680 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20  vtab *pVTab);.  
34690 69 6e 74 20 28 2a 78 44 65 73 74 72 6f 79 29 28  int (*xDestroy)(
346a0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
346b0 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4f  Tab);.  int (*xO
346c0 70 65 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61  pen)(sqlite3_vta
346d0 62 20 2a 70 56 54 61 62 2c 20 73 71 6c 69 74 65  b *pVTab, sqlite
346e0 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 2a  3_vtab_cursor **
346f0 70 70 43 75 72 73 6f 72 29 3b 0a 20 20 69 6e 74  ppCursor);.  int
34700 20 28 2a 78 43 6c 6f 73 65 29 28 73 71 6c 69 74   (*xClose)(sqlit
34710 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 29  e3_vtab_cursor*)
34720 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69 6c 74 65  ;.  int (*xFilte
34730 72 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  r)(sqlite3_vtab_
34740 63 75 72 73 6f 72 2a 2c 20 69 6e 74 20 69 64 78  cursor*, int idx
34750 4e 75 6d 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Num, const char 
34760 2a 69 64 78 53 74 72 2c 0a 20 20 20 20 20 20 20  *idxStr,.       
34770 20 20 20 20 20 20 20 20 20 69 6e 74 20 61 72 67           int arg
34780 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  c, sqlite3_value
34790 20 2a 2a 61 72 67 76 29 3b 0a 20 20 69 6e 74 20   **argv);.  int 
347a0 28 2a 78 4e 65 78 74 29 28 73 71 6c 69 74 65 33  (*xNext)(sqlite3
347b0 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a  _vtab_cursor*);.
347c0 20 20 69 6e 74 20 28 2a 78 45 6f 66 29 28 73 71    int (*xEof)(sq
347d0 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
347e0 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f  r*);.  int (*xCo
347f0 6c 75 6d 6e 29 28 73 71 6c 69 74 65 33 5f 76 74  lumn)(sqlite3_vt
34800 61 62 5f 63 75 72 73 6f 72 2a 2c 20 73 71 6c 69  ab_cursor*, sqli
34810 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e  te3_context*, in
34820 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 6f 77  t);.  int (*xRow
34830 69 64 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  id)(sqlite3_vtab
34840 5f 63 75 72 73 6f 72 2a 2c 20 73 71 6c 69 74 65  _cursor*, sqlite
34850 33 5f 69 6e 74 36 34 20 2a 70 52 6f 77 69 64 29  3_int64 *pRowid)
34860 3b 0a 20 20 69 6e 74 20 28 2a 78 55 70 64 61 74  ;.  int (*xUpdat
34870 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  e)(sqlite3_vtab 
34880 2a 2c 20 69 6e 74 2c 20 73 71 6c 69 74 65 33 5f  *, int, sqlite3_
34890 76 61 6c 75 65 20 2a 2a 2c 20 73 71 6c 69 74 65  value **, sqlite
348a0 33 5f 69 6e 74 36 34 20 2a 29 3b 0a 20 20 69 6e  3_int64 *);.  in
348b0 74 20 28 2a 78 42 65 67 69 6e 29 28 73 71 6c 69  t (*xBegin)(sqli
348c0 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29  te3_vtab *pVTab)
348d0 3b 0a 20 20 69 6e 74 20 28 2a 78 53 79 6e 63 29  ;.  int (*xSync)
348e0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
348f0 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78  VTab);.  int (*x
34900 43 6f 6d 6d 69 74 29 28 73 71 6c 69 74 65 33 5f  Commit)(sqlite3_
34910 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20  vtab *pVTab);.  
34920 69 6e 74 20 28 2a 78 52 6f 6c 6c 62 61 63 6b 29  int (*xRollback)
34930 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
34940 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78  VTab);.  int (*x
34950 46 69 6e 64 46 75 6e 63 74 69 6f 6e 29 28 73 71  FindFunction)(sq
34960 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
34970 62 2c 20 69 6e 74 20 6e 41 72 67 2c 20 63 6f 6e  b, int nArg, con
34980 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a  st char *zName,.
34990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
349a0 20 20 20 20 20 20 20 76 6f 69 64 20 28 2a 2a 70         void (**p
349b0 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
349c0 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
349d0 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20  te3_value**),.  
349e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
349f0 20 20 20 20 20 76 6f 69 64 20 2a 2a 70 70 41 72       void **ppAr
34a00 67 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 65 6e  g);.  int (*xRen
34a10 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61  ame)(sqlite3_vta
34a20 62 20 2a 70 56 74 61 62 2c 20 63 6f 6e 73 74 20  b *pVtab, const 
34a30 63 68 61 72 20 2a 7a 4e 65 77 29 3b 0a 7d 3b 0a  char *zNew);.};.
34a40 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
34a50 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 49   Virtual Table I
34a60 6e 64 65 78 69 6e 67 20 49 6e 66 6f 72 6d 61 74  ndexing Informat
34a70 69 6f 6e 20 7b 48 31 38 31 30 30 7d 20 3c 53 32  ion {H18100} <S2
34a80 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  0400>.** KEYWORD
34a90 53 3a 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  S: sqlite3_index
34aa0 5f 69 6e 66 6f 0a 2a 2a 20 45 58 50 45 52 49 4d  _info.** EXPERIM
34ab0 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ENTAL.**.** The 
34ac0 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
34ad0 66 6f 20 73 74 72 75 63 74 75 72 65 20 61 6e 64  fo structure and
34ae0 20 69 74 73 20 73 75 62 73 74 72 75 63 74 75 72   its substructur
34af0 65 73 20 69 73 20 75 73 65 64 20 74 6f 0a 2a 2a  es is used to.**
34b00 20 70 61 73 73 20 69 6e 66 6f 72 6d 61 74 69 6f   pass informatio
34b10 6e 20 69 6e 74 6f 20 61 6e 64 20 72 65 63 65 69  n into and recei
34b20 76 65 20 74 68 65 20 72 65 70 6c 79 20 66 72 6f  ve the reply fro
34b30 6d 20 74 68 65 20 5b 78 42 65 73 74 49 6e 64 65  m the [xBestInde
34b40 78 5d 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20  x].** method of 
34b50 61 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65  a [virtual table
34b60 20 6d 6f 64 75 6c 65 5d 2e 20 20 54 68 65 20 66   module].  The f
34b70 69 65 6c 64 73 20 75 6e 64 65 72 20 2a 2a 49 6e  ields under **In
34b80 70 75 74 73 2a 2a 20 61 72 65 20 74 68 65 0a 2a  puts** are the.*
34b90 2a 20 69 6e 70 75 74 73 20 74 6f 20 78 42 65 73  * inputs to xBes
34ba0 74 49 6e 64 65 78 20 61 6e 64 20 61 72 65 20 72  tIndex and are r
34bb0 65 61 64 2d 6f 6e 6c 79 2e 20 20 78 42 65 73 74  ead-only.  xBest
34bc0 49 6e 64 65 78 20 69 6e 73 65 72 74 73 20 69 74  Index inserts it
34bd0 73 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 74  s.** results int
34be0 6f 20 74 68 65 20 2a 2a 4f 75 74 70 75 74 73 2a  o the **Outputs*
34bf0 2a 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20  * fields..**.** 
34c00 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b  The aConstraint[
34c10 5d 20 61 72 72 61 79 20 72 65 63 6f 72 64 73 20  ] array records 
34c20 57 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f 6e  WHERE clause con
34c30 73 74 72 61 69 6e 74 73 20 6f 66 20 74 68 65 20  straints of the 
34c40 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65  form:.**.** <pre
34c50 3e 63 6f 6c 75 6d 6e 20 4f 50 20 65 78 70 72 3c  >column OP expr<
34c60 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72  /pre>.**.** wher
34c70 65 20 4f 50 20 69 73 20 3d 2c 20 26 6c 74 3b 2c  e OP is =, &lt;,
34c80 20 26 6c 74 3b 3d 2c 20 26 67 74 3b 2c 20 6f 72   &lt;=, &gt;, or
34c90 20 26 67 74 3b 3d 2e 20 20 54 68 65 20 70 61 72   &gt;=.  The par
34ca0 74 69 63 75 6c 61 72 20 6f 70 65 72 61 74 6f 72  ticular operator
34cb0 20 69 73 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e   is.** stored in
34cc0 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 6f   aConstraint[].o
34cd0 70 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66  p.  The index of
34ce0 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 73   the column is s
34cf0 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 61 43 6f 6e  tored in.** aCon
34d00 73 74 72 61 69 6e 74 5b 5d 2e 69 43 6f 6c 75 6d  straint[].iColum
34d10 6e 2e 20 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b  n.  aConstraint[
34d20 5d 2e 75 73 61 62 6c 65 20 69 73 20 54 52 55 45  ].usable is TRUE
34d30 20 69 66 20 74 68 65 0a 2a 2a 20 65 78 70 72 20   if the.** expr 
34d40 6f 6e 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e  on the right-han
34d50 64 20 73 69 64 65 20 63 61 6e 20 62 65 20 65 76  d side can be ev
34d60 61 6c 75 61 74 65 64 20 28 61 6e 64 20 74 68 75  aluated (and thu
34d70 73 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  s the constraint
34d80 0a 2a 2a 20 69 73 20 75 73 61 62 6c 65 29 20 61  .** is usable) a
34d90 6e 64 20 66 61 6c 73 65 20 69 66 20 69 74 20 63  nd false if it c
34da0 61 6e 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  annot..**.** The
34db0 20 6f 70 74 69 6d 69 7a 65 72 20 61 75 74 6f 6d   optimizer autom
34dc0 61 74 69 63 61 6c 6c 79 20 69 6e 76 65 72 74 73  atically inverts
34dd0 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 66 6f   terms of the fo
34de0 72 6d 20 22 65 78 70 72 20 4f 50 20 63 6f 6c 75  rm "expr OP colu
34df0 6d 6e 22 0a 2a 2a 20 61 6e 64 20 6d 61 6b 65 73  mn".** and makes
34e00 20 6f 74 68 65 72 20 73 69 6d 70 6c 69 66 69 63   other simplific
34e10 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 57 48  ations to the WH
34e20 45 52 45 20 63 6c 61 75 73 65 20 69 6e 20 61 6e  ERE clause in an
34e30 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 67   attempt to.** g
34e40 65 74 20 61 73 20 6d 61 6e 79 20 57 48 45 52 45  et as many WHERE
34e50 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 69 6e   clause terms in
34e60 74 6f 20 74 68 65 20 66 6f 72 6d 20 73 68 6f 77  to the form show
34e70 6e 20 61 62 6f 76 65 20 61 73 20 70 6f 73 73 69  n above as possi
34e80 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 61 43 6f 6e  ble..** The aCon
34e90 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 20  straint[] array 
34ea0 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20 57 48 45  only reports WHE
34eb0 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20  RE clause terms 
34ec0 69 6e 20 74 68 65 20 63 6f 72 72 65 63 74 0a 2a  in the correct.*
34ed0 2a 20 66 6f 72 6d 20 74 68 61 74 20 72 65 66 65  * form that refe
34ee0 72 20 74 6f 20 74 68 65 20 70 61 72 74 69 63 75  r to the particu
34ef0 6c 61 72 20 76 69 72 74 75 61 6c 20 74 61 62 6c  lar virtual tabl
34f00 65 20 62 65 69 6e 67 20 71 75 65 72 69 65 64 2e  e being queried.
34f10 0a 2a 2a 0a 2a 2a 20 49 6e 66 6f 72 6d 61 74 69  .**.** Informati
34f20 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 4f 52 44  on about the ORD
34f30 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20  ER BY clause is 
34f40 73 74 6f 72 65 64 20 69 6e 20 61 4f 72 64 65 72  stored in aOrder
34f50 42 79 5b 5d 2e 0a 2a 2a 20 45 61 63 68 20 74 65  By[]..** Each te
34f60 72 6d 20 6f 66 20 61 4f 72 64 65 72 42 79 20 72  rm of aOrderBy r
34f70 65 63 6f 72 64 73 20 61 20 63 6f 6c 75 6d 6e 20  ecords a column 
34f80 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  of the ORDER BY 
34f90 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  clause..**.** Th
34fa0 65 20 5b 78 42 65 73 74 49 6e 64 65 78 5d 20 6d  e [xBestIndex] m
34fb0 65 74 68 6f 64 20 6d 75 73 74 20 66 69 6c 6c 20  ethod must fill 
34fc0 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
34fd0 5b 5d 20 77 69 74 68 20 69 6e 66 6f 72 6d 61 74  [] with informat
34fe0 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 77 68 61  ion.** about wha
34ff0 74 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20  t parameters to 
35000 70 61 73 73 20 74 6f 20 78 46 69 6c 74 65 72 2e  pass to xFilter.
35010 20 20 49 66 20 61 72 67 76 49 6e 64 65 78 3e 30    If argvIndex>0
35020 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 72 69 67   then.** the rig
35030 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20  ht-hand side of 
35040 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
35050 67 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20  g aConstraint[] 
35060 69 73 20 65 76 61 6c 75 61 74 65 64 0a 2a 2a 20  is evaluated.** 
35070 61 6e 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20  and becomes the 
35080 61 72 67 76 49 6e 64 65 78 2d 74 68 20 65 6e 74  argvIndex-th ent
35090 72 79 20 69 6e 20 61 72 67 76 2e 20 20 49 66 20  ry in argv.  If 
350a0 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
350b0 5b 5d 2e 6f 6d 69 74 0a 2a 2a 20 69 73 20 74 72  [].omit.** is tr
350c0 75 65 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6e  ue, then the con
350d0 73 74 72 61 69 6e 74 20 69 73 20 61 73 73 75 6d  straint is assum
350e0 65 64 20 74 6f 20 62 65 20 66 75 6c 6c 79 20 68  ed to be fully h
350f0 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a  andled by the.**
35100 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 61   virtual table a
35110 6e 64 20 69 73 20 6e 6f 74 20 63 68 65 63 6b 65  nd is not checke
35120 64 20 61 67 61 69 6e 20 62 79 20 53 51 4c 69 74  d again by SQLit
35130 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 64 78  e..**.** The idx
35140 4e 75 6d 20 61 6e 64 20 69 64 78 50 74 72 20 76  Num and idxPtr v
35150 61 6c 75 65 73 20 61 72 65 20 72 65 63 6f 72 64  alues are record
35160 65 64 20 61 6e 64 20 70 61 73 73 65 64 20 69 6e  ed and passed in
35170 74 6f 20 74 68 65 0a 2a 2a 20 5b 78 46 69 6c 74  to the.** [xFilt
35180 65 72 5d 20 6d 65 74 68 6f 64 2e 0a 2a 2a 20 5b  er] method..** [
35190 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20  sqlite3_free()] 
351a0 69 73 20 75 73 65 64 20 74 6f 20 66 72 65 65 20  is used to free 
351b0 69 64 78 50 74 72 20 69 66 20 61 6e 64 20 6f 6e  idxPtr if and on
351c0 6c 79 20 69 66 66 0a 2a 2a 20 6e 65 65 64 54 6f  ly iff.** needTo
351d0 46 72 65 65 49 64 78 50 74 72 20 69 73 20 74 72  FreeIdxPtr is tr
351e0 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72  ue..**.** The or
351f0 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 20 6d 65  derByConsumed me
35200 61 6e 73 20 74 68 61 74 20 6f 75 74 70 75 74 20  ans that output 
35210 66 72 6f 6d 20 5b 78 46 69 6c 74 65 72 5d 2f 5b  from [xFilter]/[
35220 78 4e 65 78 74 5d 20 77 69 6c 6c 20 6f 63 63 75  xNext] will occu
35230 72 20 69 6e 0a 2a 2a 20 74 68 65 20 63 6f 72 72  r in.** the corr
35240 65 63 74 20 6f 72 64 65 72 20 74 6f 20 73 61 74  ect order to sat
35250 69 73 66 79 20 74 68 65 20 4f 52 44 45 52 20 42  isfy the ORDER B
35260 59 20 63 6c 61 75 73 65 20 73 6f 20 74 68 61 74  Y clause so that
35270 20 6e 6f 20 73 65 70 61 72 61 74 65 0a 2a 2a 20   no separate.** 
35280 73 6f 72 74 69 6e 67 20 73 74 65 70 20 69 73 20  sorting step is 
35290 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  required..**.** 
352a0 54 68 65 20 65 73 74 69 6d 61 74 65 64 43 6f 73  The estimatedCos
352b0 74 20 76 61 6c 75 65 20 69 73 20 61 6e 20 65 73  t value is an es
352c0 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20 63 6f  timate of the co
352d0 73 74 20 6f 66 20 64 6f 69 6e 67 20 74 68 65 0a  st of doing the.
352e0 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 6c 6f  ** particular lo
352f0 6f 6b 75 70 2e 20 20 41 20 66 75 6c 6c 20 73 63  okup.  A full sc
35300 61 6e 20 6f 66 20 61 20 74 61 62 6c 65 20 77 69  an of a table wi
35310 74 68 20 4e 20 65 6e 74 72 69 65 73 20 73 68 6f  th N entries sho
35320 75 6c 64 20 68 61 76 65 0a 2a 2a 20 61 20 63 6f  uld have.** a co
35330 73 74 20 6f 66 20 4e 2e 20 20 41 20 62 69 6e 61  st of N.  A bina
35340 72 79 20 73 65 61 72 63 68 20 6f 66 20 61 20 74  ry search of a t
35350 61 62 6c 65 20 6f 66 20 4e 20 65 6e 74 72 69 65  able of N entrie
35360 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 0a  s should have a.
35370 2a 2a 20 63 6f 73 74 20 6f 66 20 61 70 70 72 6f  ** cost of appro
35380 78 69 6d 61 74 65 6c 79 20 6c 6f 67 28 4e 29 2e  ximately log(N).
35390 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74  .*/.struct sqlit
353a0 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 7b 0a  e3_index_info {.
353b0 20 20 2f 2a 20 49 6e 70 75 74 73 20 2a 2f 0a 20    /* Inputs */. 
353c0 20 69 6e 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74   int nConstraint
353d0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
353e0 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
353f0 20 69 6e 20 61 43 6f 6e 73 74 72 61 69 6e 74 20   in aConstraint 
35400 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69  */.  struct sqli
35410 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72  te3_index_constr
35420 61 69 6e 74 20 7b 0a 20 20 20 20 20 69 6e 74 20  aint {.     int 
35430 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20  iColumn;        
35440 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20        /* Column 
35450 6f 6e 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64  on left-hand sid
35460 65 20 6f 66 20 63 6f 6e 73 74 72 61 69 6e 74 20  e of constraint 
35470 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64  */.     unsigned
35480 20 63 68 61 72 20 6f 70 3b 20 20 20 20 20 20 20   char op;       
35490 20 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e 74 20    /* Constraint 
354a0 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20  operator */.    
354b0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 75   unsigned char u
354c0 73 61 62 6c 65 3b 20 20 20 20 20 2f 2a 20 54 72  sable;     /* Tr
354d0 75 65 20 69 66 20 74 68 69 73 20 63 6f 6e 73 74  ue if this const
354e0 72 61 69 6e 74 20 69 73 20 75 73 61 62 6c 65 20  raint is usable 
354f0 2a 2f 0a 20 20 20 20 20 69 6e 74 20 69 54 65 72  */.     int iTer
35500 6d 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20  mOffset;        
35510 20 20 2f 2a 20 55 73 65 64 20 69 6e 74 65 72 6e    /* Used intern
35520 61 6c 6c 79 20 2d 20 78 42 65 73 74 49 6e 64 65  ally - xBestInde
35530 78 20 73 68 6f 75 6c 64 20 69 67 6e 6f 72 65 20  x should ignore 
35540 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6e 73 74 72 61  */.  } *aConstra
35550 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  int;            
35560 2f 2a 20 54 61 62 6c 65 20 6f 66 20 57 48 45 52  /* Table of WHER
35570 45 20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61  E clause constra
35580 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f  ints */.  int nO
35590 72 64 65 72 42 79 3b 20 20 20 20 20 20 20 20 20  rderBy;         
355a0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
355b0 66 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 4f  f terms in the O
355c0 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a  RDER BY clause *
355d0 2f 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74  /.  struct sqlit
355e0 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62 79  e3_index_orderby
355f0 20 7b 0a 20 20 20 20 20 69 6e 74 20 69 43 6f 6c   {.     int iCol
35600 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  umn;            
35610 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62    /* Column numb
35620 65 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67  er */.     unsig
35630 6e 65 64 20 63 68 61 72 20 64 65 73 63 3b 20 20  ned char desc;  
35640 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72       /* True for
35650 20 44 45 53 43 2e 20 20 46 61 6c 73 65 20 66 6f   DESC.  False fo
35660 72 20 41 53 43 2e 20 2a 2f 0a 20 20 7d 20 2a 61  r ASC. */.  } *a
35670 4f 72 64 65 72 42 79 3b 20 20 20 20 20 20 20 20  OrderBy;        
35680 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52         /* The OR
35690 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
356a0 0a 20 20 2f 2a 20 4f 75 74 70 75 74 73 20 2a 2f  .  /* Outputs */
356b0 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  .  struct sqlite
356c0 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
356d0 6e 74 5f 75 73 61 67 65 20 7b 0a 20 20 20 20 69  nt_usage {.    i
356e0 6e 74 20 61 72 67 76 49 6e 64 65 78 3b 20 20 20  nt argvIndex;   
356f0 20 20 20 20 20 20 20 20 2f 2a 20 69 66 20 3e 30          /* if >0
35700 2c 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20  , constraint is 
35710 70 61 72 74 20 6f 66 20 61 72 67 76 20 74 6f 20  part of argv to 
35720 78 46 69 6c 74 65 72 20 2a 2f 0a 20 20 20 20 75  xFilter */.    u
35730 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f 6d 69  nsigned char omi
35740 74 3b 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f  t;      /* Do no
35750 74 20 63 6f 64 65 20 61 20 74 65 73 74 20 66 6f  t code a test fo
35760 72 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e  r this constrain
35770 74 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6e 73 74  t */.  } *aConst
35780 72 61 69 6e 74 55 73 61 67 65 3b 0a 20 20 69 6e  raintUsage;.  in
35790 74 20 69 64 78 4e 75 6d 3b 20 20 20 20 20 20 20  t idxNum;       
357a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
357b0 65 72 20 75 73 65 64 20 74 6f 20 69 64 65 6e 74  er used to ident
357c0 69 66 79 20 74 68 65 20 69 6e 64 65 78 20 2a 2f  ify the index */
357d0 0a 20 20 63 68 61 72 20 2a 69 64 78 53 74 72 3b  .  char *idxStr;
357e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
357f0 20 53 74 72 69 6e 67 2c 20 70 6f 73 73 69 62 6c   String, possibl
35800 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  y obtained from 
35810 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 2a  sqlite3_malloc *
35820 2f 0a 20 20 69 6e 74 20 6e 65 65 64 54 6f 46 72  /.  int needToFr
35830 65 65 49 64 78 53 74 72 3b 20 20 20 20 20 20 2f  eeIdxStr;      /
35840 2a 20 46 72 65 65 20 69 64 78 53 74 72 20 75 73  * Free idxStr us
35850 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ing sqlite3_free
35860 28 29 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20  () if true */.  
35870 69 6e 74 20 6f 72 64 65 72 42 79 43 6f 6e 73 75  int orderByConsu
35880 6d 65 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 72  med;       /* Tr
35890 75 65 20 69 66 20 6f 75 74 70 75 74 20 69 73 20  ue if output is 
358a0 61 6c 72 65 61 64 79 20 6f 72 64 65 72 65 64 20  already ordered 
358b0 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 65 73 74 69  */.  double esti
358c0 6d 61 74 65 64 43 6f 73 74 3b 20 20 20 20 20 20  matedCost;      
358d0 2f 2a 20 45 73 74 69 6d 61 74 65 64 20 63 6f 73  /* Estimated cos
358e0 74 20 6f 66 20 75 73 69 6e 67 20 74 68 69 73 20  t of using this 
358f0 69 6e 64 65 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66  index */.};.#def
35900 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  ine SQLITE_INDEX
35910 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20 20  _CONSTRAINT_EQ  
35920 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    2.#define SQLI
35930 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
35940 49 4e 54 5f 47 54 20 20 20 20 34 0a 23 64 65 66  INT_GT    4.#def
35950 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  ine SQLITE_INDEX
35960 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 45 20 20  _CONSTRAINT_LE  
35970 20 20 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    8.#define SQLI
35980 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
35990 49 4e 54 5f 4c 54 20 20 20 20 31 36 0a 23 64 65  INT_LT    16.#de
359a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45  fine SQLITE_INDE
359b0 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 45 20  X_CONSTRAINT_GE 
359c0 20 20 20 33 32 0a 23 64 65 66 69 6e 65 20 53 51     32.#define SQ
359d0 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
359e0 52 41 49 4e 54 5f 4d 41 54 43 48 20 36 34 0a 0a  RAINT_MATCH 64..
359f0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
35a00 52 65 67 69 73 74 65 72 20 41 20 56 69 72 74 75  Register A Virtu
35a10 61 6c 20 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65  al Table Impleme
35a20 6e 74 61 74 69 6f 6e 20 7b 48 31 38 32 30 30 7d  ntation {H18200}
35a30 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50   <S20400>.** EXP
35a40 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20  ERIMENTAL.**.** 
35a50 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
35a60 75 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 72  used to register
35a70 20 61 20 6e 65 77 20 5b 76 69 72 74 75 61 6c 20   a new [virtual 
35a80 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 6e 61  table module] na
35a90 6d 65 2e 0a 2a 2a 20 4d 6f 64 75 6c 65 20 6e 61  me..** Module na
35aa0 6d 65 73 20 6d 75 73 74 20 62 65 20 72 65 67 69  mes must be regi
35ab0 73 74 65 72 65 64 20 62 65 66 6f 72 65 0a 2a 2a  stered before.**
35ac0 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20   creating a new 
35ad0 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 20  [virtual table] 
35ae0 75 73 69 6e 67 20 74 68 65 20 6d 6f 64 75 6c 65  using the module
35af0 2c 20 6f 72 20 62 65 66 6f 72 65 20 75 73 69 6e  , or before usin
35b00 67 20 61 0a 2a 2a 20 70 72 65 65 78 69 73 74 69  g a.** preexisti
35b10 6e 67 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c  ng [virtual tabl
35b20 65 5d 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c  e] for the modul
35b30 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 6f 64  e..**.** The mod
35b40 75 6c 65 20 6e 61 6d 65 20 69 73 20 72 65 67 69  ule name is regi
35b50 73 74 65 72 65 64 20 6f 6e 20 74 68 65 20 5b 64  stered on the [d
35b60 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
35b70 6f 6e 5d 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  on] specified.**
35b80 20 62 79 20 74 68 65 20 66 69 72 73 74 20 70 61   by the first pa
35b90 72 61 6d 65 74 65 72 2e 20 20 54 68 65 20 6e 61  rameter.  The na
35ba0 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65  me of the module
35bb0 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65   is given by the
35bc0 20 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61   .** second para
35bd0 6d 65 74 65 72 2e 20 20 54 68 65 20 74 68 69 72  meter.  The thir
35be0 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61  d parameter is a
35bf0 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74   pointer to.** t
35c00 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
35c10 6e 20 6f 66 20 74 68 65 20 5b 76 69 72 74 75 61  n of the [virtua
35c20 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e  l table module].
35c30 20 20 20 54 68 65 20 66 6f 75 72 74 68 0a 2a 2a     The fourth.**
35c40 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 6e   parameter is an
35c50 20 61 72 62 69 74 72 61 72 79 20 63 6c 69 65 6e   arbitrary clien
35c60 74 20 64 61 74 61 20 70 6f 69 6e 74 65 72 20 74  t data pointer t
35c70 68 61 74 20 69 73 20 70 61 73 73 65 64 20 74 68  hat is passed th
35c80 72 6f 75 67 68 0a 2a 2a 20 69 6e 74 6f 20 74 68  rough.** into th
35c90 65 20 5b 78 43 72 65 61 74 65 5d 20 61 6e 64 20  e [xCreate] and 
35ca0 5b 78 43 6f 6e 6e 65 63 74 5d 20 6d 65 74 68 6f  [xConnect] metho
35cb0 64 73 20 6f 66 20 74 68 65 20 76 69 72 74 75 61  ds of the virtua
35cc0 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 0a 2a  l table module.*
35cd0 2a 20 77 68 65 6e 20 61 20 6e 65 77 20 76 69 72  * when a new vir
35ce0 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20 62 65  tual table is be
35cf0 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 20 6f   being created o
35d00 72 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 64 2e  r reinitialized.
35d10 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65  .**.** This inte
35d20 72 66 61 63 65 20 68 61 73 20 65 78 61 63 74 6c  rface has exactl
35d30 79 20 74 68 65 20 73 61 6d 65 20 65 66 66 65 63  y the same effec
35d40 74 20 61 73 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20  t as calling.** 
35d50 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
35d60 6d 6f 64 75 6c 65 5f 76 32 28 29 5d 20 77 69 74  module_v2()] wit
35d70 68 20 61 20 4e 55 4c 4c 20 63 6c 69 65 6e 74 20  h a NULL client 
35d80 64 61 74 61 20 64 65 73 74 72 75 63 74 6f 72 2e  data destructor.
35d90 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  .*/.SQLITE_API S
35da0 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54  QLITE_EXPERIMENT
35db0 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  AL int sqlite3_c
35dc0 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 0a 20 20  reate_module(.  
35dd0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
35de0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51             /* SQ
35df0 4c 69 74 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  Lite connection 
35e00 74 6f 20 72 65 67 69 73 74 65 72 20 6d 6f 64 75  to register modu
35e10 6c 65 20 77 69 74 68 20 2a 2f 0a 20 20 63 6f 6e  le with */.  con
35e20 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
35e30 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
35e40 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f  of the module */
35e50 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
35e60 5f 6d 6f 64 75 6c 65 20 2a 70 2c 20 20 20 2f 2a  _module *p,   /*
35e70 20 4d 65 74 68 6f 64 73 20 66 6f 72 20 74 68 65   Methods for the
35e80 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69   module */.  voi
35e90 64 20 2a 70 43 6c 69 65 6e 74 44 61 74 61 20 20  d *pClientData  
35ea0 20 20 20 20 20 20 20 20 2f 2a 20 43 6c 69 65 6e          /* Clien
35eb0 74 20 64 61 74 61 20 66 6f 72 20 78 43 72 65 61  t data for xCrea
35ec0 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29  te/xConnect */.)
35ed0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
35ee0 46 3a 20 52 65 67 69 73 74 65 72 20 41 20 56 69  F: Register A Vi
35ef0 72 74 75 61 6c 20 54 61 62 6c 65 20 49 6d 70 6c  rtual Table Impl
35f00 65 6d 65 6e 74 61 74 69 6f 6e 20 7b 48 31 38 32  ementation {H182
35f10 31 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20  10} <S20400>.** 
35f20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
35f30 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
35f40 69 73 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20  is identical to 
35f50 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  the [sqlite3_cre
35f60 61 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 6d 65  ate_module()] me
35f70 74 68 6f 64 2c 0a 2a 2a 20 65 78 63 65 70 74 20  thod,.** except 
35f80 74 68 61 74 20 69 74 20 68 61 73 20 61 6e 20 65  that it has an e
35f90 78 74 72 61 20 70 61 72 61 6d 65 74 65 72 20 74  xtra parameter t
35fa0 6f 20 73 70 65 63 69 66 79 20 0a 2a 2a 20 61 20  o specify .** a 
35fb0 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74  destructor funct
35fc0 69 6f 6e 20 66 6f 72 20 74 68 65 20 63 6c 69 65  ion for the clie
35fd0 6e 74 20 64 61 74 61 20 70 6f 69 6e 74 65 72 2e  nt data pointer.
35fe0 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a    SQLite will.**
35ff0 20 69 6e 76 6f 6b 65 20 74 68 65 20 64 65 73 74   invoke the dest
36000 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20  ructor function 
36010 28 69 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55  (if it is not NU
36020 4c 4c 29 20 77 68 65 6e 20 53 51 4c 69 74 65 0a  LL) when SQLite.
36030 2a 2a 20 6e 6f 20 6c 6f 6e 67 65 72 20 6e 65 65  ** no longer nee
36040 64 73 20 74 68 65 20 70 43 6c 69 65 6e 74 44 61  ds the pClientDa
36050 74 61 20 70 6f 69 6e 74 65 72 2e 20 20 0a 2a 2f  ta pointer.  .*/
36060 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
36070 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20  TE_EXPERIMENTAL 
36080 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
36090 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 0a 20 20  te_module_v2(.  
360a0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
360b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51             /* SQ
360c0 4c 69 74 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  Lite connection 
360d0 74 6f 20 72 65 67 69 73 74 65 72 20 6d 6f 64 75  to register modu
360e0 6c 65 20 77 69 74 68 20 2a 2f 0a 20 20 63 6f 6e  le with */.  con
360f0 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
36100 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
36110 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f  of the module */
36120 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
36130 5f 6d 6f 64 75 6c 65 20 2a 70 2c 20 20 20 2f 2a  _module *p,   /*
36140 20 4d 65 74 68 6f 64 73 20 66 6f 72 20 74 68 65   Methods for the
36150 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69   module */.  voi
36160 64 20 2a 70 43 6c 69 65 6e 74 44 61 74 61 2c 20  d *pClientData, 
36170 20 20 20 20 20 20 20 20 2f 2a 20 43 6c 69 65 6e          /* Clien
36180 74 20 64 61 74 61 20 66 6f 72 20 78 43 72 65 61  t data for xCrea
36190 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20  te/xConnect */. 
361a0 20 76 6f 69 64 28 2a 78 44 65 73 74 72 6f 79 29   void(*xDestroy)
361b0 28 76 6f 69 64 2a 29 20 20 20 20 20 2f 2a 20 4d  (void*)     /* M
361c0 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74 6f 72  odule destructor
361d0 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 3b 0a   function */.);.
361e0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
361f0 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 49   Virtual Table I
36200 6e 73 74 61 6e 63 65 20 4f 62 6a 65 63 74 20 7b  nstance Object {
36210 48 31 38 30 31 30 7d 20 3c 53 32 30 34 30 30 3e  H18010} <S20400>
36220 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71  .** KEYWORDS: sq
36230 6c 69 74 65 33 5f 76 74 61 62 0a 2a 2a 20 45 58  lite3_vtab.** EX
36240 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a  PERIMENTAL.**.**
36250 20 45 76 65 72 79 20 5b 76 69 72 74 75 61 6c 20   Every [virtual 
36260 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 69 6d  table module] im
36270 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65  plementation use
36280 73 20 61 20 73 75 62 63 6c 61 73 73 0a 2a 2a 20  s a subclass.** 
36290 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
362a0 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 64 65   structure to de
362b0 73 63 72 69 62 65 20 61 20 70 61 72 74 69 63 75  scribe a particu
362c0 6c 61 72 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20  lar instance.** 
362d0 6f 66 20 74 68 65 20 5b 76 69 72 74 75 61 6c 20  of the [virtual 
362e0 74 61 62 6c 65 5d 2e 20 20 45 61 63 68 20 73 75  table].  Each su
362f0 62 63 6c 61 73 73 20 77 69 6c 6c 0a 2a 2a 20 62  bclass will.** b
36300 65 20 74 61 69 6c 6f 72 65 64 20 74 6f 20 74 68  e tailored to th
36310 65 20 73 70 65 63 69 66 69 63 20 6e 65 65 64 73  e specific needs
36320 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 69   of the module i
36330 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a  mplementation..*
36340 2a 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66  * The purpose of
36350 20 74 68 69 73 20 73 75 70 65 72 63 6c 61 73 73   this superclass
36360 20 69 73 20 74 6f 20 64 65 66 69 6e 65 20 63 65   is to define ce
36370 72 74 61 69 6e 20 66 69 65 6c 64 73 20 74 68 61  rtain fields tha
36380 74 20 61 72 65 0a 2a 2a 20 63 6f 6d 6d 6f 6e 20  t are.** common 
36390 74 6f 20 61 6c 6c 20 6d 6f 64 75 6c 65 20 69 6d  to all module im
363a0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a  plementations..*
363b0 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 61 62  *.** Virtual tab
363c0 6c 65 73 20 6d 65 74 68 6f 64 73 20 63 61 6e 20  les methods can 
363d0 73 65 74 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  set an error mes
363e0 73 61 67 65 20 62 79 20 61 73 73 69 67 6e 69 6e  sage by assignin
363f0 67 20 61 0a 2a 2a 20 73 74 72 69 6e 67 20 6f 62  g a.** string ob
36400 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c  tained from [sql
36410 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 5d 20  ite3_mprintf()] 
36420 74 6f 20 7a 45 72 72 4d 73 67 2e 20 20 54 68 65  to zErrMsg.  The
36430 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 0a 2a   method should.*
36440 2a 20 74 61 6b 65 20 63 61 72 65 20 74 68 61 74  * take care that
36450 20 61 6e 79 20 70 72 69 6f 72 20 73 74 72 69 6e   any prior strin
36460 67 20 69 73 20 66 72 65 65 64 20 62 79 20 61 20  g is freed by a 
36470 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
36480 5f 66 72 65 65 28 29 5d 0a 2a 2a 20 70 72 69 6f  _free()].** prio
36490 72 20 74 6f 20 61 73 73 69 67 6e 69 6e 67 20 61  r to assigning a
364a0 20 6e 65 77 20 73 74 72 69 6e 67 20 74 6f 20 7a   new string to z
364b0 45 72 72 4d 73 67 2e 20 20 41 66 74 65 72 20 74  ErrMsg.  After t
364c0 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
364d0 0a 2a 2a 20 69 73 20 64 65 6c 69 76 65 72 65 64  .** is delivered
364e0 20 75 70 20 74 6f 20 74 68 65 20 63 6c 69 65 6e   up to the clien
364f0 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 2c 20 74  t application, t
36500 68 65 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 62  he string will b
36510 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a  e automatically.
36520 2a 2a 20 66 72 65 65 64 20 62 79 20 73 71 6c 69  ** freed by sqli
36530 74 65 33 5f 66 72 65 65 28 29 20 61 6e 64 20 74  te3_free() and t
36540 68 65 20 7a 45 72 72 4d 73 67 20 66 69 65 6c 64  he zErrMsg field
36550 20 77 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 2e   will be zeroed.
36560 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74  .*/.struct sqlit
36570 65 33 5f 76 74 61 62 20 7b 0a 20 20 63 6f 6e 73  e3_vtab {.  cons
36580 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
36590 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 2f 2a 20 54   *pModule;  /* T
365a0 68 65 20 6d 6f 64 75 6c 65 20 66 6f 72 20 74 68  he module for th
365b0 69 73 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  is virtual table
365c0 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20   */.  int nRef; 
365d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
365e0 20 20 20 20 20 20 2f 2a 20 4e 4f 20 4c 4f 4e 47        /* NO LONG
365f0 45 52 20 55 53 45 44 20 2a 2f 0a 20 20 63 68 61  ER USED */.  cha
36600 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20  r *zErrMsg;     
36610 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36620 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 72  Error message fr
36630 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e  om sqlite3_mprin
36640 74 66 28 29 20 2a 2f 0a 20 20 2f 2a 20 56 69 72  tf() */.  /* Vir
36650 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65  tual table imple
36660 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20  mentations will 
36670 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61 64  typically add ad
36680 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20  ditional fields 
36690 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  */.};../*.** CAP
366a0 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54  I3REF: Virtual T
366b0 61 62 6c 65 20 43 75 72 73 6f 72 20 4f 62 6a 65  able Cursor Obje
366c0 63 74 20 20 7b 48 31 38 30 32 30 7d 20 3c 53 32  ct  {H18020} <S2
366d0 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  0400>.** KEYWORD
366e0 53 3a 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  S: sqlite3_vtab_
366f0 63 75 72 73 6f 72 20 7b 76 69 72 74 75 61 6c 20  cursor {virtual 
36700 74 61 62 6c 65 20 63 75 72 73 6f 72 7d 0a 2a 2a  table cursor}.**
36710 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
36720 0a 2a 2a 20 45 76 65 72 79 20 5b 76 69 72 74 75  .** Every [virtu
36730 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d  al table module]
36740 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
36750 75 73 65 73 20 61 20 73 75 62 63 6c 61 73 73 20  uses a subclass 
36760 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77  of the.** follow
36770 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 74 6f  ing structure to
36780 20 64 65 73 63 72 69 62 65 20 63 75 72 73 6f 72   describe cursor
36790 73 20 74 68 61 74 20 70 6f 69 6e 74 20 69 6e 74  s that point int
367a0 6f 20 74 68 65 0a 2a 2a 20 5b 76 69 72 74 75 61  o the.** [virtua
367b0 6c 20 74 61 62 6c 65 5d 20 61 6e 64 20 61 72 65  l table] and are
367c0 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 6f 70   used.** to loop
367d0 20 74 68 72 6f 75 67 68 20 74 68 65 20 76 69 72   through the vir
367e0 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 43 75 72  tual table.  Cur
367f0 73 6f 72 73 20 61 72 65 20 63 72 65 61 74 65 64  sors are created
36800 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73   using the.** [s
36810 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2e 78 4f  qlite3_module.xO
36820 70 65 6e 20 7c 20 78 4f 70 65 6e 5d 20 6d 65 74  pen | xOpen] met
36830 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c  hod of the modul
36840 65 20 61 6e 64 20 61 72 65 20 64 65 73 74 72 6f  e and are destro
36850 79 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 5b 73  yed.** by the [s
36860 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2e 78 43  qlite3_module.xC
36870 6c 6f 73 65 20 7c 20 78 43 6c 6f 73 65 5d 20 6d  lose | xClose] m
36880 65 74 68 6f 64 2e 20 20 43 75 73 73 6f 72 73 20  ethod.  Cussors 
36890 61 72 65 20 75 73 65 64 0a 2a 2a 20 62 79 20 74  are used.** by t
368a0 68 65 20 5b 78 46 69 6c 74 65 72 5d 2c 20 5b 78  he [xFilter], [x
368b0 4e 65 78 74 5d 2c 20 5b 78 45 6f 66 5d 2c 20 5b  Next], [xEof], [
368c0 78 43 6f 6c 75 6d 6e 5d 2c 20 61 6e 64 20 5b 78  xColumn], and [x
368d0 52 6f 77 69 64 5d 20 6d 65 74 68 6f 64 73 0a 2a  Rowid] methods.*
368e0 2a 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e  * of the module.
368f0 20 20 45 61 63 68 20 6d 6f 64 75 6c 65 20 69 6d    Each module im
36900 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 6c  plementation wil
36910 6c 20 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20  l define.** the 
36920 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 63 75 72  content of a cur
36930 73 6f 72 20 73 74 72 75 63 74 75 72 65 20 74 6f  sor structure to
36940 20 73 75 69 74 20 69 74 73 20 6f 77 6e 20 6e 65   suit its own ne
36950 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  eds..**.** This 
36960 73 75 70 65 72 63 6c 61 73 73 20 65 78 69 73 74  superclass exist
36970 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 64 65  s in order to de
36980 66 69 6e 65 20 66 69 65 6c 64 73 20 6f 66 20 74  fine fields of t
36990 68 65 20 63 75 72 73 6f 72 20 74 68 61 74 0a 2a  he cursor that.*
369a0 2a 20 61 72 65 20 63 6f 6d 6d 6f 6e 20 74 6f 20  * are common to 
369b0 61 6c 6c 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  all implementati
369c0 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73  ons..*/.struct s
369d0 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
369e0 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76  or {.  sqlite3_v
369f0 74 61 62 20 2a 70 56 74 61 62 3b 20 20 20 20 20  tab *pVtab;     
36a00 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c   /* Virtual tabl
36a10 65 20 6f 66 20 74 68 69 73 20 63 75 72 73 6f 72  e of this cursor
36a20 20 2a 2f 0a 20 20 2f 2a 20 56 69 72 74 75 61 6c   */.  /* Virtual
36a30 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74   table implement
36a40 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69  ations will typi
36a50 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69  cally add additi
36a60 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d  onal fields */.}
36a70 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
36a80 46 3a 20 44 65 63 6c 61 72 65 20 54 68 65 20 53  F: Declare The S
36a90 63 68 65 6d 61 20 4f 66 20 41 20 56 69 72 74 75  chema Of A Virtu
36aa0 61 6c 20 54 61 62 6c 65 20 7b 48 31 38 32 38 30  al Table {H18280
36ab0 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58  } <S20400>.** EX
36ac0 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a  PERIMENTAL.**.**
36ad0 20 54 68 65 20 5b 78 43 72 65 61 74 65 5d 20 61   The [xCreate] a
36ae0 6e 64 20 5b 78 43 6f 6e 6e 65 63 74 5d 20 6d 65  nd [xConnect] me
36af0 74 68 6f 64 73 20 6f 66 20 61 0a 2a 2a 20 5b 76  thods of a.** [v
36b00 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
36b10 75 6c 65 5d 20 63 61 6c 6c 20 74 68 69 73 20 69  ule] call this i
36b20 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 64  nterface.** to d
36b30 65 63 6c 61 72 65 20 74 68 65 20 66 6f 72 6d 61  eclare the forma
36b40 74 20 28 74 68 65 20 6e 61 6d 65 73 20 61 6e 64  t (the names and
36b50 20 64 61 74 61 74 79 70 65 73 20 6f 66 20 74 68   datatypes of th
36b60 65 20 63 6f 6c 75 6d 6e 73 29 20 6f 66 0a 2a 2a  e columns) of.**
36b70 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
36b80 6c 65 73 20 74 68 65 79 20 69 6d 70 6c 65 6d 65  les they impleme
36b90 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  nt..*/.SQLITE_AP
36ba0 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d  I SQLITE_EXPERIM
36bb0 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65  ENTAL int sqlite
36bc0 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28 73  3_declare_vtab(s
36bd0 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63  qlite3*, const c
36be0 68 61 72 20 2a 7a 53 51 4c 29 3b 0a 0a 2f 2a 0a  har *zSQL);../*.
36bf0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 76 65  ** CAPI3REF: Ove
36c00 72 6c 6f 61 64 20 41 20 46 75 6e 63 74 69 6f 6e  rload A Function
36c10 20 46 6f 72 20 41 20 56 69 72 74 75 61 6c 20 54   For A Virtual T
36c20 61 62 6c 65 20 7b 48 31 38 33 30 30 7d 20 3c 53  able {H18300} <S
36c30 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49  20400>.** EXPERI
36c40 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 56 69 72  MENTAL.**.** Vir
36c50 74 75 61 6c 20 74 61 62 6c 65 73 20 63 61 6e 20  tual tables can 
36c60 70 72 6f 76 69 64 65 20 61 6c 74 65 72 6e 61 74  provide alternat
36c70 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ive implementati
36c80 6f 6e 73 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73  ons of functions
36c90 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 5b 78  .** using the [x
36ca0 46 69 6e 64 46 75 6e 63 74 69 6f 6e 5d 20 6d 65  FindFunction] me
36cb0 74 68 6f 64 20 6f 66 20 74 68 65 20 5b 76 69 72  thod of the [vir
36cc0 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
36cd0 65 5d 2e 20 20 0a 2a 2a 20 42 75 74 20 67 6c 6f  e].  .** But glo
36ce0 62 61 6c 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  bal versions of 
36cf0 74 68 6f 73 65 20 66 75 6e 63 74 69 6f 6e 73 0a  those functions.
36d00 2a 2a 20 6d 75 73 74 20 65 78 69 73 74 20 69 6e  ** must exist in
36d10 20 6f 72 64 65 72 20 74 6f 20 62 65 20 6f 76 65   order to be ove
36d20 72 6c 6f 61 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  rloaded..**.** T
36d30 68 69 73 20 41 50 49 20 6d 61 6b 65 73 20 73 75  his API makes su
36d40 72 65 20 61 20 67 6c 6f 62 61 6c 20 76 65 72 73  re a global vers
36d50 69 6f 6e 20 6f 66 20 61 20 66 75 6e 63 74 69 6f  ion of a functio
36d60 6e 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75  n with a particu
36d70 6c 61 72 0a 2a 2a 20 6e 61 6d 65 20 61 6e 64 20  lar.** name and 
36d80 6e 75 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65  number of parame
36d90 74 65 72 73 20 65 78 69 73 74 73 2e 20 20 49 66  ters exists.  If
36da0 20 6e 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f   no such functio
36db0 6e 20 65 78 69 73 74 73 0a 2a 2a 20 62 65 66 6f  n exists.** befo
36dc0 72 65 20 74 68 69 73 20 41 50 49 20 69 73 20 63  re this API is c
36dd0 61 6c 6c 65 64 2c 20 61 20 6e 65 77 20 66 75 6e  alled, a new fun
36de0 63 74 69 6f 6e 20 69 73 20 63 72 65 61 74 65 64  ction is created
36df0 2e 20 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74  .  The implement
36e00 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20  ation.** of the 
36e10 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77  new function alw
36e20 61 79 73 20 63 61 75 73 65 73 20 61 6e 20 65 78  ays causes an ex
36e30 63 65 70 74 69 6f 6e 20 74 6f 20 62 65 20 74 68  ception to be th
36e40 72 6f 77 6e 2e 20 20 53 6f 0a 2a 2a 20 74 68 65  rown.  So.** the
36e50 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20 69 73   new function is
36e60 20 6e 6f 74 20 67 6f 6f 64 20 66 6f 72 20 61 6e   not good for an
36e70 79 74 68 69 6e 67 20 62 79 20 69 74 73 65 6c 66  ything by itself
36e80 2e 20 20 49 74 73 20 6f 6e 6c 79 0a 2a 2a 20 70  .  Its only.** p
36e90 75 72 70 6f 73 65 20 69 73 20 74 6f 20 62 65 20  urpose is to be 
36ea0 61 20 70 6c 61 63 65 68 6f 6c 64 65 72 20 66 75  a placeholder fu
36eb0 6e 63 74 69 6f 6e 20 74 68 61 74 20 63 61 6e 20  nction that can 
36ec0 62 65 20 6f 76 65 72 6c 6f 61 64 65 64 0a 2a 2a  be overloaded.**
36ed0 20 62 79 20 61 20 5b 76 69 72 74 75 61 6c 20 74   by a [virtual t
36ee0 61 62 6c 65 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  able]..*/.SQLITE
36ef0 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45  _API SQLITE_EXPE
36f00 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c  RIMENTAL int sql
36f10 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75  ite3_overload_fu
36f20 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c  nction(sqlite3*,
36f30 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75   const char *zFu
36f40 6e 63 4e 61 6d 65 2c 20 69 6e 74 20 6e 41 72 67  ncName, int nArg
36f50 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e  );../*.** The in
36f60 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 76  terface to the v
36f70 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63  irtual-table mec
36f80 68 61 6e 69 73 6d 20 64 65 66 69 6e 65 64 20 61  hanism defined a
36f90 62 6f 76 65 20 28 62 61 63 6b 20 75 70 0a 2a 2a  bove (back up.**
36fa0 20 74 6f 20 61 20 63 6f 6d 6d 65 6e 74 20 72 65   to a comment re
36fb0 6d 61 72 6b 61 62 6c 79 20 73 69 6d 69 6c 61 72  markably similar
36fc0 20 74 6f 20 74 68 69 73 20 6f 6e 65 29 20 69 73   to this one) is
36fd0 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69   currently consi
36fe0 64 65 72 65 64 0a 2a 2a 20 74 6f 20 62 65 20 65  dered.** to be e
36ff0 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20 54 68  xperimental.  Th
37000 65 20 69 6e 74 65 72 66 61 63 65 20 6d 69 67 68  e interface migh
37010 74 20 63 68 61 6e 67 65 20 69 6e 20 69 6e 63 6f  t change in inco
37020 6d 70 61 74 69 62 6c 65 20 77 61 79 73 2e 0a 2a  mpatible ways..*
37030 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 70  * If this is a p
37040 72 6f 62 6c 65 6d 20 66 6f 72 20 79 6f 75 2c 20  roblem for you, 
37050 64 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 20 69  do not use the i
37060 6e 74 65 72 66 61 63 65 20 61 74 20 74 68 69 73  nterface at this
37070 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65   time..**.** Whe
37080 6e 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61  n the virtual-ta
37090 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 73 74  ble mechanism st
370a0 61 62 69 6c 69 7a 65 73 2c 20 77 65 20 77 69 6c  abilizes, we wil
370b0 6c 20 64 65 63 6c 61 72 65 20 74 68 65 0a 2a 2a  l declare the.**
370c0 20 69 6e 74 65 72 66 61 63 65 20 66 69 78 65 64   interface fixed
370d0 2c 20 73 75 70 70 6f 72 74 20 69 74 20 69 6e 64  , support it ind
370e0 65 66 69 6e 69 74 65 6c 79 2c 20 61 6e 64 20 72  efinitely, and r
370f0 65 6d 6f 76 65 20 74 68 69 73 20 63 6f 6d 6d 65  emove this comme
37100 6e 74 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58  nt..**.****** EX
37110 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20 73 75 62  PERIMENTAL - sub
37120 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77  ject to change w
37130 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 20 2a 2a  ithout notice **
37140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a  ************.*/.
37150 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
37160 20 41 20 48 61 6e 64 6c 65 20 54 6f 20 41 6e 20   A Handle To An 
37170 4f 70 65 6e 20 42 4c 4f 42 20 7b 48 31 37 38 30  Open BLOB {H1780
37180 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 20 4b  0} <S30230>.** K
37190 45 59 57 4f 52 44 53 3a 20 7b 42 4c 4f 42 20 68  EYWORDS: {BLOB h
371a0 61 6e 64 6c 65 7d 20 7b 42 4c 4f 42 20 68 61 6e  andle} {BLOB han
371b0 64 6c 65 73 7d 0a 2a 2a 0a 2a 2a 20 41 6e 20 69  dles}.**.** An i
371c0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
371d0 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74  object represent
371e0 73 20 61 6e 20 6f 70 65 6e 20 42 4c 4f 42 20 6f  s an open BLOB o
371f0 6e 20 77 68 69 63 68 0a 2a 2a 20 5b 73 71 6c 69  n which.** [sqli
37200 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 7c 20  te3_blob_open | 
37210 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42 4c 4f 42  incremental BLOB
37220 20 49 2f 4f 5d 20 63 61 6e 20 62 65 20 70 65 72   I/O] can be per
37230 66 6f 72 6d 65 64 2e 0a 2a 2a 20 4f 62 6a 65 63  formed..** Objec
37240 74 73 20 6f 66 20 74 68 69 73 20 74 79 70 65 20  ts of this type 
37250 61 72 65 20 63 72 65 61 74 65 64 20 62 79 20 5b  are created by [
37260 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65  sqlite3_blob_ope
37270 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 64 65 73 74  n()].** and dest
37280 72 6f 79 65 64 20 62 79 20 5b 73 71 6c 69 74 65  royed by [sqlite
37290 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e  3_blob_close()].
372a0 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33  .** The [sqlite3
372b0 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20 61 6e  _blob_read()] an
372c0 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  d [sqlite3_blob_
372d0 77 72 69 74 65 28 29 5d 20 69 6e 74 65 72 66 61  write()] interfa
372e0 63 65 73 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73  ces.** can be us
372f0 65 64 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72  ed to read or wr
37300 69 74 65 20 73 6d 61 6c 6c 20 73 75 62 73 65 63  ite small subsec
37310 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 42 4c 4f  tions of the BLO
37320 42 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74  B..** The [sqlit
37330 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 29 5d  e3_blob_bytes()]
37340 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
37350 6e 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  ns the size of t
37360 68 65 20 42 4c 4f 42 20 69 6e 20 62 79 74 65 73  he BLOB in bytes
37370 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
37380 75 63 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  uct sqlite3_blob
37390 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 3b 0a 0a   sqlite3_blob;..
373a0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
373b0 4f 70 65 6e 20 41 20 42 4c 4f 42 20 46 6f 72 20  Open A BLOB For 
373c0 49 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f 4f 20  Incremental I/O 
373d0 7b 48 31 37 38 31 30 7d 20 3c 53 33 30 32 33 30  {H17810} <S30230
373e0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74  >.**.** This int
373f0 65 72 66 61 63 65 73 20 6f 70 65 6e 73 20 61 20  erfaces opens a 
37400 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 20 7c 20 68  [BLOB handle | h
37410 61 6e 64 6c 65 5d 20 74 6f 20 74 68 65 20 42 4c  andle] to the BL
37420 4f 42 20 6c 6f 63 61 74 65 64 0a 2a 2a 20 69 6e  OB located.** in
37430 20 72 6f 77 20 69 52 6f 77 2c 20 63 6f 6c 75 6d   row iRow, colum
37440 6e 20 7a 43 6f 6c 75 6d 6e 2c 20 74 61 62 6c 65  n zColumn, table
37450 20 7a 54 61 62 6c 65 20 69 6e 20 64 61 74 61 62   zTable in datab
37460 61 73 65 20 7a 44 62 3b 0a 2a 2a 20 69 6e 20 6f  ase zDb;.** in o
37470 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20  ther words, the 
37480 73 61 6d 65 20 42 4c 4f 42 20 74 68 61 74 20 77  same BLOB that w
37490 6f 75 6c 64 20 62 65 20 73 65 6c 65 63 74 65 64  ould be selected
374a0 20 62 79 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e   by:.**.** <pre>
374b0 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 7a  .**     SELECT z
374c0 43 6f 6c 75 6d 6e 20 46 52 4f 4d 20 7a 44 62 2e  Column FROM zDb.
374d0 7a 54 61 62 6c 65 20 57 48 45 52 45 20 5b 72 6f  zTable WHERE [ro
374e0 77 69 64 5d 20 3d 20 69 52 6f 77 3b 0a 2a 2a 20  wid] = iRow;.** 
374f0 3c 2f 70 72 65 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a  </pre> {END}.**.
37500 2a 2a 20 49 66 20 74 68 65 20 66 6c 61 67 73 20  ** If the flags 
37510 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e  parameter is non
37520 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20  -zero, then the 
37530 42 4c 4f 42 20 69 73 20 6f 70 65 6e 65 64 20 66  BLOB is opened f
37540 6f 72 20 72 65 61 64 0a 2a 2a 20 61 6e 64 20 77  or read.** and w
37550 72 69 74 65 20 61 63 63 65 73 73 2e 20 49 66 20  rite access. If 
37560 69 74 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 20  it is zero, the 
37570 42 4c 4f 42 20 69 73 20 6f 70 65 6e 65 64 20 66  BLOB is opened f
37580 6f 72 20 72 65 61 64 20 61 63 63 65 73 73 2e 0a  or read access..
37590 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73  ** It is not pos
375a0 73 69 62 6c 65 20 74 6f 20 6f 70 65 6e 20 61 20  sible to open a 
375b0 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 70  column that is p
375c0 61 72 74 20 6f 66 20 61 6e 20 69 6e 64 65 78 20  art of an index 
375d0 6f 72 20 70 72 69 6d 61 72 79 20 0a 2a 2a 20 6b  or primary .** k
375e0 65 79 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20  ey for writing. 
375f0 5e 49 66 20 5b 66 6f 72 65 69 67 6e 20 6b 65 79  ^If [foreign key
37600 20 63 6f 6e 73 74 72 61 69 6e 74 73 5d 20 61 72   constraints] ar
37610 65 20 65 6e 61 62 6c 65 64 2c 20 69 74 20 69 73  e enabled, it is
37620 20 0a 2a 2a 20 6e 6f 74 20 70 6f 73 73 69 62 6c   .** not possibl
37630 65 20 74 6f 20 6f 70 65 6e 20 61 20 63 6f 6c 75  e to open a colu
37640 6d 6e 20 74 68 61 74 20 69 73 20 70 61 72 74 20  mn that is part 
37650 6f 66 20 61 20 5b 63 68 69 6c 64 20 6b 65 79 5d  of a [child key]
37660 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 0a 2a 2a   for writing..**
37670 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68  .** Note that th
37680 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  e database name 
37690 69 73 20 6e 6f 74 20 74 68 65 20 66 69 6c 65 6e  is not the filen
376a0 61 6d 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ame that contain
376b0 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  s.** the databas
376c0 65 20 62 75 74 20 72 61 74 68 65 72 20 74 68 65  e but rather the
376d0 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 6f   symbolic name o
376e0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  f the database t
376f0 68 61 74 0a 2a 2a 20 69 73 20 61 73 73 69 67 6e  hat.** is assign
37700 65 64 20 77 68 65 6e 20 74 68 65 20 64 61 74 61  ed when the data
37710 62 61 73 65 20 69 73 20 63 6f 6e 6e 65 63 74 65  base is connecte
37720 64 20 75 73 69 6e 67 20 5b 41 54 54 41 43 48 5d  d using [ATTACH]
37730 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6d 61 69  ..** For the mai
37740 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  n database file,
37750 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61   the database na
37760 6d 65 20 69 73 20 22 6d 61 69 6e 22 2e 0a 2a 2a  me is "main"..**
37770 20 46 6f 72 20 54 45 4d 50 20 74 61 62 6c 65 73   For TEMP tables
37780 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  , the database n
37790 61 6d 65 20 69 73 20 22 74 65 6d 70 22 2e 0a 2a  ame is "temp"..*
377a0 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c  *.** On success,
377b0 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20   [SQLITE_OK] is 
377c0 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65  returned and the
377d0 20 6e 65 77 20 5b 42 4c 4f 42 20 68 61 6e 64 6c   new [BLOB handl
377e0 65 5d 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a  e] is written.**
377f0 20 74 6f 20 2a 70 70 42 6c 6f 62 2e 20 4f 74 68   to *ppBlob. Oth
37800 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 6f 72  erwise an [error
37810 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e   code] is return
37820 65 64 20 61 6e 64 20 2a 70 70 42 6c 6f 62 20 69  ed and *ppBlob i
37830 73 20 73 65 74 0a 2a 2a 20 74 6f 20 62 65 20 61  s set.** to be a
37840 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2e 0a 2a   null pointer..*
37850 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
37860 73 65 74 73 20 74 68 65 20 5b 64 61 74 61 62 61  sets the [databa
37870 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 65  se connection] e
37880 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 6d 65  rror code and me
37890 73 73 61 67 65 0a 2a 2a 20 61 63 63 65 73 73 69  ssage.** accessi
378a0 62 6c 65 20 76 69 61 20 5b 73 71 6c 69 74 65 33  ble via [sqlite3
378b0 5f 65 72 72 63 6f 64 65 28 29 5d 20 61 6e 64 20  _errcode()] and 
378c0 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28  [sqlite3_errmsg(
378d0 29 5d 20 61 6e 64 20 72 65 6c 61 74 65 64 0a 2a  )] and related.*
378e0 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 4e 6f  * functions.  No
378f0 74 65 20 74 68 61 74 20 74 68 65 20 2a 70 70 42  te that the *ppB
37900 6c 6f 62 20 76 61 72 69 61 62 6c 65 20 69 73 20  lob variable is 
37910 61 6c 77 61 79 73 20 69 6e 69 74 69 61 6c 69 7a  always initializ
37920 65 64 20 69 6e 20 61 0a 2a 2a 20 77 61 79 20 74  ed in a.** way t
37930 68 61 74 20 6d 61 6b 65 73 20 69 74 20 73 61 66  hat makes it saf
37940 65 20 74 6f 20 69 6e 76 6f 6b 65 20 5b 73 71 6c  e to invoke [sql
37950 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28  ite3_blob_close(
37960 29 5d 20 6f 6e 20 2a 70 70 42 6c 6f 62 0a 2a 2a  )] on *ppBlob.**
37970 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74   regardless of t
37980 68 65 20 73 75 63 63 65 73 73 20 6f 72 20 66 61  he success or fa
37990 69 6c 75 72 65 20 6f 66 20 74 68 69 73 20 72 6f  ilure of this ro
379a0 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  utine..**.** If 
379b0 74 68 65 20 72 6f 77 20 74 68 61 74 20 61 20 42  the row that a B
379c0 4c 4f 42 20 68 61 6e 64 6c 65 20 70 6f 69 6e 74  LOB handle point
379d0 73 20 74 6f 20 69 73 20 6d 6f 64 69 66 69 65 64  s to is modified
379e0 20 62 79 20 61 6e 0a 2a 2a 20 5b 55 50 44 41 54   by an.** [UPDAT
379f0 45 5d 2c 20 5b 44 45 4c 45 54 45 5d 2c 20 6f 72  E], [DELETE], or
37a00 20 62 79 20 5b 4f 4e 20 43 4f 4e 46 4c 49 43 54   by [ON CONFLICT
37a10 5d 20 73 69 64 65 2d 65 66 66 65 63 74 73 0a 2a  ] side-effects.*
37a20 2a 20 74 68 65 6e 20 74 68 65 20 42 4c 4f 42 20  * then the BLOB 
37a30 68 61 6e 64 6c 65 20 69 73 20 6d 61 72 6b 65 64  handle is marked
37a40 20 61 73 20 22 65 78 70 69 72 65 64 22 2e 0a 2a   as "expired"..*
37a50 2a 20 54 68 69 73 20 69 73 20 74 72 75 65 20 69  * This is true i
37a60 66 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6f 66 20  f any column of 
37a70 74 68 65 20 72 6f 77 20 69 73 20 63 68 61 6e 67  the row is chang
37a80 65 64 2c 20 65 76 65 6e 20 61 20 63 6f 6c 75 6d  ed, even a colum
37a90 6e 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20  n.** other than 
37aa0 74 68 65 20 6f 6e 65 20 74 68 65 20 42 4c 4f 42  the one the BLOB
37ab0 20 68 61 6e 64 6c 65 20 69 73 20 6f 70 65 6e 20   handle is open 
37ac0 6f 6e 2e 0a 2a 2a 20 43 61 6c 6c 73 20 74 6f 20  on..** Calls to 
37ad0 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65  [sqlite3_blob_re
37ae0 61 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  ad()] and [sqlit
37af0 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d  e3_blob_write()]
37b00 20 66 6f 72 0a 2a 2a 20 61 20 65 78 70 69 72 65   for.** a expire
37b10 64 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20 66 61  d BLOB handle fa
37b20 69 6c 20 77 69 74 68 20 61 6e 20 72 65 74 75 72  il with an retur
37b30 6e 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54  n code of [SQLIT
37b40 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 20 43 68 61  E_ABORT]..** Cha
37b50 6e 67 65 73 20 77 72 69 74 74 65 6e 20 69 6e 74  nges written int
37b60 6f 20 61 20 42 4c 4f 42 20 70 72 69 6f 72 20 74  o a BLOB prior t
37b70 6f 20 74 68 65 20 42 4c 4f 42 20 65 78 70 69 72  o the BLOB expir
37b80 69 6e 67 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 72  ing are not.** r
37b90 6f 6c 6c 62 61 63 6b 20 62 79 20 74 68 65 20 65  ollback by the e
37ba0 78 70 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  xpiration of the
37bb0 20 42 4c 4f 42 2e 20 20 53 75 63 68 20 63 68 61   BLOB.  Such cha
37bc0 6e 67 65 73 20 77 69 6c 6c 20 65 76 65 6e 74 75  nges will eventu
37bd0 61 6c 6c 79 0a 2a 2a 20 63 6f 6d 6d 69 74 20 69  ally.** commit i
37be0 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  f the transactio
37bf0 6e 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f 20 63  n continues to c
37c00 6f 6d 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  ompletion..**.**
37c10 20 55 73 65 20 74 68 65 20 5b 73 71 6c 69 74 65   Use the [sqlite
37c20 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 29 5d 20  3_blob_bytes()] 
37c30 69 6e 74 65 72 66 61 63 65 20 74 6f 20 64 65 74  interface to det
37c40 65 72 6d 69 6e 65 20 74 68 65 20 73 69 7a 65 20  ermine the size 
37c50 6f 66 0a 2a 2a 20 74 68 65 20 6f 70 65 6e 65 64  of.** the opened
37c60 20 62 6c 6f 62 2e 20 20 54 68 65 20 73 69 7a 65   blob.  The size
37c70 20 6f 66 20 61 20 62 6c 6f 62 20 6d 61 79 20 6e   of a blob may n
37c80 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20 62 79  ot be changed by
37c90 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66 61   this.** interfa
37ca0 63 65 2e 20 20 55 73 65 20 74 68 65 20 5b 55 50  ce.  Use the [UP
37cb0 44 41 54 45 5d 20 53 51 4c 20 63 6f 6d 6d 61 6e  DATE] SQL comman
37cc0 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20  d to change the 
37cd0 73 69 7a 65 20 6f 66 20 61 0a 2a 2a 20 62 6c 6f  size of a.** blo
37ce0 62 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71  b..**.** The [sq
37cf0 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62  lite3_bind_zerob
37d00 6c 6f 62 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  lob()] and [sqli
37d10 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62  te3_result_zerob
37d20 6c 6f 62 28 29 5d 20 69 6e 74 65 72 66 61 63 65  lob()] interface
37d30 73 0a 2a 2a 20 61 6e 64 20 74 68 65 20 62 75 69  s.** and the bui
37d40 6c 74 2d 69 6e 20 5b 7a 65 72 6f 62 6c 6f 62 5d  lt-in [zeroblob]
37d50 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 63 61   SQL function ca
37d60 6e 20 62 65 20 75 73 65 64 2c 20 69 66 20 64 65  n be used, if de
37d70 73 69 72 65 64 2c 0a 2a 2a 20 74 6f 20 63 72 65  sired,.** to cre
37d80 61 74 65 20 61 6e 20 65 6d 70 74 79 2c 20 7a 65  ate an empty, ze
37d90 72 6f 2d 66 69 6c 6c 65 64 20 62 6c 6f 62 20 69  ro-filled blob i
37da0 6e 20 77 68 69 63 68 20 74 6f 20 72 65 61 64 20  n which to read 
37db0 6f 72 20 77 72 69 74 65 20 75 73 69 6e 67 0a 2a  or write using.*
37dc0 2a 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65  * this interface
37dd0 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64  ..**.** To avoid
37de0 20 61 20 72 65 73 6f 75 72 63 65 20 6c 65 61 6b   a resource leak
37df0 2c 20 65 76 65 72 79 20 6f 70 65 6e 20 5b 42 4c  , every open [BL
37e00 4f 42 20 68 61 6e 64 6c 65 5d 20 73 68 6f 75 6c  OB handle] shoul
37e10 64 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20  d eventually.** 
37e20 62 65 20 72 65 6c 65 61 73 65 64 20 62 79 20 61  be released by a
37e30 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
37e40 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e  3_blob_close()].
37e50 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
37e60 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 31 33 5d  nts:.** [H17813]
37e70 20 5b 48 31 37 38 31 34 5d 20 5b 48 31 37 38 31   [H17814] [H1781
37e80 36 5d 20 5b 48 31 37 38 31 39 5d 20 5b 48 31 37  6] [H17819] [H17
37e90 38 32 31 5d 20 5b 48 31 37 38 32 34 5d 0a 2a 2f  821] [H17824].*/
37ea0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
37eb0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65  sqlite3_blob_ope
37ec0 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 0a 20  n(.  sqlite3*,. 
37ed0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
37ee0 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
37ef0 7a 54 61 62 6c 65 2c 0a 20 20 63 6f 6e 73 74 20  zTable,.  const 
37f00 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c 0a 20  char *zColumn,. 
37f10 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
37f20 52 6f 77 2c 0a 20 20 69 6e 74 20 66 6c 61 67 73  Row,.  int flags
37f30 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ,.  sqlite3_blob
37f40 20 2a 2a 70 70 42 6c 6f 62 0a 29 3b 0a 0a 2f 2a   **ppBlob.);../*
37f50 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6c  .** CAPI3REF: Cl
37f60 6f 73 65 20 41 20 42 4c 4f 42 20 48 61 6e 64 6c  ose A BLOB Handl
37f70 65 20 7b 48 31 37 38 33 30 7d 20 3c 53 33 30 32  e {H17830} <S302
37f80 33 30 3e 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 73  30>.**.** Closes
37f90 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68   an open [BLOB h
37fa0 61 6e 64 6c 65 5d 2e 0a 2a 2a 0a 2a 2a 20 43 6c  andle]..**.** Cl
37fb0 6f 73 69 6e 67 20 61 20 42 4c 4f 42 20 73 68 61  osing a BLOB sha
37fc0 6c 6c 20 63 61 75 73 65 20 74 68 65 20 63 75 72  ll cause the cur
37fd0 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  rent transaction
37fe0 20 74 6f 20 63 6f 6d 6d 69 74 0a 2a 2a 20 69 66   to commit.** if
37ff0 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f 74   there are no ot
38000 68 65 72 20 42 4c 4f 42 73 2c 20 6e 6f 20 70 65  her BLOBs, no pe
38010 6e 64 69 6e 67 20 70 72 65 70 61 72 65 64 20 73  nding prepared s
38020 74 61 74 65 6d 65 6e 74 73 2c 20 61 6e 64 20 74  tatements, and t
38030 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  he.** database c
38040 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20  onnection is in 
38050 5b 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65  [autocommit mode
38060 5d 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 77 72 69  ]..** If any wri
38070 74 65 73 20 77 65 72 65 20 6d 61 64 65 20 74 6f  tes were made to
38080 20 74 68 65 20 42 4c 4f 42 2c 20 74 68 65 79 20   the BLOB, they 
38090 6d 69 67 68 74 20 62 65 20 68 65 6c 64 20 69 6e  might be held in
380a0 20 63 61 63 68 65 0a 2a 2a 20 75 6e 74 69 6c 20   cache.** until 
380b0 74 68 65 20 63 6c 6f 73 65 20 6f 70 65 72 61 74  the close operat
380c0 69 6f 6e 20 69 66 20 74 68 65 79 20 77 69 6c 6c  ion if they will
380d0 20 66 69 74 2e 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73   fit..**.** Clos
380e0 69 6e 67 20 74 68 65 20 42 4c 4f 42 20 6f 66 74  ing the BLOB oft
380f0 65 6e 20 66 6f 72 63 65 73 20 74 68 65 20 63 68  en forces the ch
38100 61 6e 67 65 73 0a 2a 2a 20 6f 75 74 20 74 6f 20  anges.** out to 
38110 64 69 73 6b 20 61 6e 64 20 73 6f 20 69 66 20 61  disk and so if a
38120 6e 79 20 49 2f 4f 20 65 72 72 6f 72 73 20 6f 63  ny I/O errors oc
38130 63 75 72 2c 20 74 68 65 79 20 77 69 6c 6c 20 6c  cur, they will l
38140 69 6b 65 6c 79 20 6f 63 63 75 72 0a 2a 2a 20 61  ikely occur.** a
38150 74 20 74 68 65 20 74 69 6d 65 20 77 68 65 6e 20  t the time when 
38160 74 68 65 20 42 4c 4f 42 20 69 73 20 63 6c 6f 73  the BLOB is clos
38170 65 64 2e 20 20 41 6e 79 20 65 72 72 6f 72 73 20  ed.  Any errors 
38180 74 68 61 74 20 6f 63 63 75 72 20 64 75 72 69 6e  that occur durin
38190 67 0a 2a 2a 20 63 6c 6f 73 69 6e 67 20 61 72 65  g.** closing are
381a0 20 72 65 70 6f 72 74 65 64 20 61 73 20 61 20 6e   reported as a n
381b0 6f 6e 2d 7a 65 72 6f 20 72 65 74 75 72 6e 20 76  on-zero return v
381c0 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  alue..**.** The 
381d0 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65 64 20 75  BLOB is closed u
381e0 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 2e 20  nconditionally. 
381f0 20 45 76 65 6e 20 69 66 20 74 68 69 73 20 72 6f   Even if this ro
38200 75 74 69 6e 65 20 72 65 74 75 72 6e 73 0a 2a 2a  utine returns.**
38210 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2c 20   an error code, 
38220 74 68 65 20 42 4c 4f 42 20 69 73 20 73 74 69 6c  the BLOB is stil
38230 6c 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20  l closed..**.** 
38240 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75  Calling this rou
38250 74 69 6e 65 20 77 69 74 68 20 61 20 6e 75 6c 6c  tine with a null
38260 20 70 6f 69 6e 74 65 72 20 28 77 68 69 63 68 20   pointer (which 
38270 61 73 20 77 6f 75 6c 64 20 62 65 20 72 65 74 75  as would be retu
38280 72 6e 65 64 0a 2a 2a 20 62 79 20 66 61 69 6c 65  rned.** by faile
38290 64 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  d call to [sqlit
382a0 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 29  e3_blob_open()])
382b0 20 69 73 20 61 20 68 61 72 6d 6c 65 73 73 20 6e   is a harmless n
382c0 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  o-op..**.** Requ
382d0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
382e0 37 38 33 33 5d 20 5b 48 31 37 38 33 36 5d 20 5b  7833] [H17836] [
382f0 48 31 37 38 33 39 5d 0a 2a 2f 0a 53 51 4c 49 54  H17839].*/.SQLIT
38300 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
38310 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 73 71 6c  3_blob_close(sql
38320 69 74 65 33 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f  ite3_blob *);../
38330 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52  *.** CAPI3REF: R
38340 65 74 75 72 6e 20 54 68 65 20 53 69 7a 65 20 4f  eturn The Size O
38350 66 20 41 6e 20 4f 70 65 6e 20 42 4c 4f 42 20 7b  f An Open BLOB {
38360 48 31 37 38 34 30 7d 20 3c 53 33 30 32 33 30 3e  H17840} <S30230>
38370 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 73 20 74  .**.** Returns t
38380 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73  he size in bytes
38390 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 61 63 63   of the BLOB acc
383a0 65 73 73 69 62 6c 65 20 76 69 61 20 74 68 65 20  essible via the 
383b0 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c 79  .** successfully
383c0 20 6f 70 65 6e 65 64 20 5b 42 4c 4f 42 20 68 61   opened [BLOB ha
383d0 6e 64 6c 65 5d 20 69 6e 20 69 74 73 20 6f 6e 6c  ndle] in its onl
383e0 79 20 61 72 67 75 6d 65 6e 74 2e 20 20 54 68 65  y argument.  The
383f0 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20  .** incremental 
38400 62 6c 6f 62 20 49 2f 4f 20 72 6f 75 74 69 6e 65  blob I/O routine
38410 73 20 63 61 6e 20 6f 6e 6c 79 20 72 65 61 64 20  s can only read 
38420 6f 72 20 6f 76 65 72 77 72 69 74 69 6e 67 20 65  or overwriting e
38430 78 69 73 74 69 6e 67 0a 2a 2a 20 62 6c 6f 62 20  xisting.** blob 
38440 63 6f 6e 74 65 6e 74 3b 20 74 68 65 79 20 63 61  content; they ca
38450 6e 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20  nnot change the 
38460 73 69 7a 65 20 6f 66 20 61 20 62 6c 6f 62 2e 0a  size of a blob..
38470 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
38480 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e  ne only works on
38490 20 61 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d   a [BLOB handle]
384a0 20 77 68 69 63 68 20 68 61 73 20 62 65 65 6e 20   which has been 
384b0 63 72 65 61 74 65 64 0a 2a 2a 20 62 79 20 61 20  created.** by a 
384c0 70 72 69 6f 72 20 73 75 63 63 65 73 73 66 75 6c  prior successful
384d0 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
384e0 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 61  3_blob_open()] a
384f0 6e 64 20 77 68 69 63 68 20 68 61 73 20 6e 6f 74  nd which has not
38500 0a 2a 2a 20 62 65 65 6e 20 63 6c 6f 73 65 64 20  .** been closed 
38510 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  by [sqlite3_blob
38520 5f 63 6c 6f 73 65 28 29 5d 2e 20 20 50 61 73 73  _close()].  Pass
38530 69 6e 67 20 61 6e 79 20 6f 74 68 65 72 20 70 6f  ing any other po
38540 69 6e 74 65 72 20 69 6e 0a 2a 2a 20 74 6f 20 74  inter in.** to t
38550 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 73 75  his routine resu
38560 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64  lts in undefined
38570 20 61 6e 64 20 70 72 6f 62 61 62 6c 79 20 75 6e   and probably un
38580 64 65 73 69 72 61 62 6c 65 20 62 65 68 61 76 69  desirable behavi
38590 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  or..**.** Requir
385a0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38  ements:.** [H178
385b0 34 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  43].*/.SQLITE_AP
385c0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
385d0 6f 62 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33  ob_bytes(sqlite3
385e0 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  _blob *);../*.**
385f0 20 43 41 50 49 33 52 45 46 3a 20 52 65 61 64 20   CAPI3REF: Read 
38600 44 61 74 61 20 46 72 6f 6d 20 41 20 42 4c 4f 42  Data From A BLOB
38610 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 7b   Incrementally {
38620 48 31 37 38 35 30 7d 20 3c 53 33 30 32 33 30 3e  H17850} <S30230>
38630 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
38640 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
38650 72 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61  read data from a
38660 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e  n open [BLOB han
38670 64 6c 65 5d 20 69 6e 74 6f 20 61 0a 2a 2a 20 63  dle] into a.** c
38680 61 6c 6c 65 72 2d 73 75 70 70 6c 69 65 64 20 62  aller-supplied b
38690 75 66 66 65 72 2e 20 4e 20 62 79 74 65 73 20 6f  uffer. N bytes o
386a0 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 69 65  f data are copie
386b0 64 20 69 6e 74 6f 20 62 75 66 66 65 72 20 5a 0a  d into buffer Z.
386c0 2a 2a 20 66 72 6f 6d 20 74 68 65 20 6f 70 65 6e  ** from the open
386d0 20 42 4c 4f 42 2c 20 73 74 61 72 74 69 6e 67 20   BLOB, starting 
386e0 61 74 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65  at offset iOffse
386f0 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6f 66 66 73  t..**.** If offs
38700 65 74 20 69 4f 66 66 73 65 74 20 69 73 20 6c 65  et iOffset is le
38710 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65 73 20  ss than N bytes 
38720 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 20  from the end of 
38730 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20 5b 53 51  the BLOB,.** [SQ
38740 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72  LITE_ERROR] is r
38750 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64  eturned and no d
38760 61 74 61 20 69 73 20 72 65 61 64 2e 20 20 49 66  ata is read.  If
38770 20 4e 20 6f 72 20 69 4f 66 66 73 65 74 20 69 73   N or iOffset is
38780 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 7a 65  .** less than ze
38790 72 6f 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f  ro, [SQLITE_ERRO
387a0 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  R] is returned a
387b0 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 72 65  nd no data is re
387c0 61 64 2e 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20  ad..** The size 
387d0 6f 66 20 74 68 65 20 62 6c 6f 62 20 28 61 6e 64  of the blob (and
387e0 20 68 65 6e 63 65 20 74 68 65 20 6d 61 78 69 6d   hence the maxim
387f0 75 6d 20 76 61 6c 75 65 20 6f 66 20 4e 2b 69 4f  um value of N+iO
38800 66 66 73 65 74 29 0a 2a 2a 20 63 61 6e 20 62 65  ffset).** can be
38810 20 64 65 74 65 72 6d 69 6e 65 64 20 75 73 69 6e   determined usin
38820 67 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62  g the [sqlite3_b
38830 6c 6f 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74  lob_bytes()] int
38840 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e  erface..**.** An
38850 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64   attempt to read
38860 20 66 72 6f 6d 20 61 6e 20 65 78 70 69 72 65 64   from an expired
38870 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66   [BLOB handle] f
38880 61 69 6c 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20  ails with an.** 
38890 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53  error code of [S
388a0 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a  QLITE_ABORT]..**
388b0 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20  .** On success, 
388c0 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
388d0 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77  urned..** Otherw
388e0 69 73 65 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63  ise, an [error c
388f0 6f 64 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65  ode] or an [exte
38900 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d  nded error code]
38910 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
38920 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
38930 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61   only works on a
38940 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 77   [BLOB handle] w
38950 68 69 63 68 20 68 61 73 20 62 65 65 6e 20 63 72  hich has been cr
38960 65 61 74 65 64 0a 2a 2a 20 62 79 20 61 20 70 72  eated.** by a pr
38970 69 6f 72 20 73 75 63 63 65 73 73 66 75 6c 20 63  ior successful c
38980 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
38990 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64  blob_open()] and
389a0 20 77 68 69 63 68 20 68 61 73 20 6e 6f 74 0a 2a   which has not.*
389b0 2a 20 62 65 65 6e 20 63 6c 6f 73 65 64 20 62 79  * been closed by
389c0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63   [sqlite3_blob_c
389d0 6c 6f 73 65 28 29 5d 2e 20 20 50 61 73 73 69 6e  lose()].  Passin
389e0 67 20 61 6e 79 20 6f 74 68 65 72 20 70 6f 69 6e  g any other poin
389f0 74 65 72 20 69 6e 0a 2a 2a 20 74 6f 20 74 68 69  ter in.** to thi
38a00 73 20 72 6f 75 74 69 6e 65 20 72 65 73 75 6c 74  s routine result
38a10 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 61  s in undefined a
38a20 6e 64 20 70 72 6f 62 61 62 6c 79 20 75 6e 64 65  nd probably unde
38a30 73 69 72 61 62 6c 65 20 62 65 68 61 76 69 6f 72  sirable behavior
38a40 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
38a50 3a 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  : [sqlite3_blob_
38a60 77 72 69 74 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  write()]..**.** 
38a70 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
38a80 20 5b 48 31 37 38 35 33 5d 20 5b 48 31 37 38 35   [H17853] [H1785
38a90 36 5d 20 5b 48 31 37 38 35 39 5d 20 5b 48 31 37  6] [H17859] [H17
38aa0 38 36 32 5d 20 5b 48 31 37 38 36 33 5d 20 5b 48  862] [H17863] [H
38ab0 31 37 38 36 35 5d 20 5b 48 31 37 38 36 38 5d 0a  17865] [H17868].
38ac0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
38ad0 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72  t sqlite3_blob_r
38ae0 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ead(sqlite3_blob
38af0 20 2a 2c 20 76 6f 69 64 20 2a 5a 2c 20 69 6e 74   *, void *Z, int
38b00 20 4e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29   N, int iOffset)
38b10 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
38b20 46 3a 20 57 72 69 74 65 20 44 61 74 61 20 49 6e  F: Write Data In
38b30 74 6f 20 41 20 42 4c 4f 42 20 49 6e 63 72 65 6d  to A BLOB Increm
38b40 65 6e 74 61 6c 6c 79 20 7b 48 31 37 38 37 30 7d  entally {H17870}
38b50 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20   <S30230>.**.** 
38b60 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
38b70 20 75 73 65 64 20 74 6f 20 77 72 69 74 65 20 64   used to write d
38b80 61 74 61 20 69 6e 74 6f 20 61 6e 20 6f 70 65 6e  ata into an open
38b90 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66   [BLOB handle] f
38ba0 72 6f 6d 20 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d  rom a.** caller-
38bb0 73 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 2e  supplied buffer.
38bc0 20 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61   N bytes of data
38bd0 20 61 72 65 20 63 6f 70 69 65 64 20 66 72 6f 6d   are copied from
38be0 20 74 68 65 20 62 75 66 66 65 72 20 5a 0a 2a 2a   the buffer Z.**
38bf0 20 69 6e 74 6f 20 74 68 65 20 6f 70 65 6e 20 42   into the open B
38c00 4c 4f 42 2c 20 73 74 61 72 74 69 6e 67 20 61 74  LOB, starting at
38c10 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74 2e   offset iOffset.
38c20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 5b 42  .**.** If the [B
38c30 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 70 61 73 73  LOB handle] pass
38c40 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ed as the first 
38c50 61 72 67 75 6d 65 6e 74 20 77 61 73 20 6e 6f 74  argument was not
38c60 20 6f 70 65 6e 65 64 20 66 6f 72 0a 2a 2a 20 77   opened for.** w
38c70 72 69 74 69 6e 67 20 28 74 68 65 20 66 6c 61 67  riting (the flag
38c80 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  s parameter to [
38c90 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65  sqlite3_blob_ope
38ca0 6e 28 29 5d 20 77 61 73 20 7a 65 72 6f 29 2c 0a  n()] was zero),.
38cb0 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** this function
38cc0 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
38cd0 5f 52 45 41 44 4f 4e 4c 59 5d 2e 0a 2a 2a 0a 2a  _READONLY]..**.*
38ce0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
38cf0 6d 61 79 20 6f 6e 6c 79 20 6d 6f 64 69 66 79 20  may only modify 
38d00 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
38d10 74 68 65 20 42 4c 4f 42 3b 20 69 74 20 69 73 0a  the BLOB; it is.
38d20 2a 2a 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  ** not possible 
38d30 74 6f 20 69 6e 63 72 65 61 73 65 20 74 68 65 20  to increase the 
38d40 73 69 7a 65 20 6f 66 20 61 20 42 4c 4f 42 20 75  size of a BLOB u
38d50 73 69 6e 67 20 74 68 69 73 20 41 50 49 2e 0a 2a  sing this API..*
38d60 2a 20 49 66 20 6f 66 66 73 65 74 20 69 4f 66 66  * If offset iOff
38d70 73 65 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e  set is less than
38d80 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68   N bytes from th
38d90 65 20 65 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f  e end of the BLO
38da0 42 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52  B,.** [SQLITE_ER
38db0 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64  ROR] is returned
38dc0 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20   and no data is 
38dd0 77 72 69 74 74 65 6e 2e 20 20 49 66 20 4e 20 69  written.  If N i
38de0 73 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 7a  s.** less than z
38df0 65 72 6f 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f  ero [SQLITE_ERRO
38e00 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  R] is returned a
38e10 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 77 72  nd no data is wr
38e20 69 74 74 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 69  itten..** The si
38e30 7a 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 28  ze of the BLOB (
38e40 61 6e 64 20 68 65 6e 63 65 20 74 68 65 20 6d 61  and hence the ma
38e50 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 4e  ximum value of N
38e60 2b 69 4f 66 66 73 65 74 29 0a 2a 2a 20 63 61 6e  +iOffset).** can
38e70 20 62 65 20 64 65 74 65 72 6d 69 6e 65 64 20 75   be determined u
38e80 73 69 6e 67 20 74 68 65 20 5b 73 71 6c 69 74 65  sing the [sqlite
38e90 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 29 5d 20  3_blob_bytes()] 
38ea0 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a  interface..**.**
38eb0 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 77   An attempt to w
38ec0 72 69 74 65 20 74 6f 20 61 6e 20 65 78 70 69 72  rite to an expir
38ed0 65 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d  ed [BLOB handle]
38ee0 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 0a 2a   fails with an.*
38ef0 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20  * error code of 
38f00 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 20  [SQLITE_ABORT]. 
38f10 20 57 72 69 74 65 73 20 74 6f 20 74 68 65 20 42   Writes to the B
38f20 4c 4f 42 20 74 68 61 74 20 6f 63 63 75 72 72 65  LOB that occurre
38f30 64 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20  d.** before the 
38f40 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 65 78  [BLOB handle] ex
38f50 70 69 72 65 64 20 61 72 65 20 6e 6f 74 20 72 6f  pired are not ro
38f60 6c 6c 65 64 20 62 61 63 6b 20 62 79 20 74 68 65  lled back by the
38f70 0a 2a 2a 20 65 78 70 69 72 61 74 69 6f 6e 20 6f  .** expiration o
38f80 66 20 74 68 65 20 68 61 6e 64 6c 65 2c 20 74 68  f the handle, th
38f90 6f 75 67 68 20 6f 66 20 63 6f 75 72 73 65 20 74  ough of course t
38fa0 68 6f 73 65 20 63 68 61 6e 67 65 73 20 6d 69 67  hose changes mig
38fb0 68 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20  ht.** have been 
38fc0 6f 76 65 72 77 72 69 74 74 65 6e 20 62 79 20 74  overwritten by t
38fd0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  he statement tha
38fe0 74 20 65 78 70 69 72 65 64 20 74 68 65 20 42 4c  t expired the BL
38ff0 4f 42 20 68 61 6e 64 6c 65 0a 2a 2a 20 6f 72 20  OB handle.** or 
39000 62 79 20 6f 74 68 65 72 20 69 6e 64 65 70 65 6e  by other indepen
39010 64 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 73 2e  dent statements.
39020 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73  .**.** On succes
39030 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  s, SQLITE_OK is 
39040 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74 68  returned..** Oth
39050 65 72 77 69 73 65 2c 20 61 6e 20 20 5b 65 72 72  erwise, an  [err
39060 6f 72 20 63 6f 64 65 5d 20 6f 72 20 61 6e 20 5b  or code] or an [
39070 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63  extended error c
39080 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64  ode] is returned
39090 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
390a0 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20  tine only works 
390b0 6f 6e 20 61 20 5b 42 4c 4f 42 20 68 61 6e 64 6c  on a [BLOB handl
390c0 65 5d 20 77 68 69 63 68 20 68 61 73 20 62 65 65  e] which has bee
390d0 6e 20 63 72 65 61 74 65 64 0a 2a 2a 20 62 79 20  n created.** by 
390e0 61 20 70 72 69 6f 72 20 73 75 63 63 65 73 73 66  a prior successf
390f0 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  ul call to [sqli
39100 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d  te3_blob_open()]
39110 20 61 6e 64 20 77 68 69 63 68 20 68 61 73 20 6e   and which has n
39120 6f 74 0a 2a 2a 20 62 65 65 6e 20 63 6c 6f 73 65  ot.** been close
39130 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c  d by [sqlite3_bl
39140 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e 20 20 50 61  ob_close()].  Pa
39150 73 73 69 6e 67 20 61 6e 79 20 6f 74 68 65 72 20  ssing any other 
39160 70 6f 69 6e 74 65 72 20 69 6e 0a 2a 2a 20 74 6f  pointer in.** to
39170 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
39180 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e  sults in undefin
39190 65 64 20 61 6e 64 20 70 72 6f 62 61 62 6c 79 20  ed and probably 
391a0 75 6e 64 65 73 69 72 61 62 6c 65 20 62 65 68 61  undesirable beha
391b0 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  vior..**.** See 
391c0 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62  also: [sqlite3_b
391d0 6c 6f 62 5f 72 65 61 64 28 29 5d 2e 0a 2a 2a 0a  lob_read()]..**.
391e0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
391f0 0a 2a 2a 20 5b 48 31 37 38 37 33 5d 20 5b 48 31  .** [H17873] [H1
39200 37 38 37 34 5d 20 5b 48 31 37 38 37 35 5d 20 5b  7874] [H17875] [
39210 48 31 37 38 37 36 5d 20 5b 48 31 37 38 37 37 5d  H17876] [H17877]
39220 20 5b 48 31 37 38 37 39 5d 20 5b 48 31 37 38 38   [H17879] [H1788
39230 32 5d 20 5b 48 31 37 38 38 35 5d 0a 2a 2a 20 5b  2] [H17885].** [
39240 48 31 37 38 38 38 5d 0a 2a 2f 0a 53 51 4c 49 54  H17888].*/.SQLIT
39250 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
39260 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 73 71 6c  3_blob_write(sql
39270 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 63 6f 6e  ite3_blob *, con
39280 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20  st void *z, int 
39290 6e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29 3b  n, int iOffset);
392a0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
392b0 3a 20 56 69 72 74 75 61 6c 20 46 69 6c 65 20 53  : Virtual File S
392c0 79 73 74 65 6d 20 4f 62 6a 65 63 74 73 20 7b 48  ystem Objects {H
392d0 31 31 32 30 30 7d 20 3c 53 32 30 31 30 30 3e 0a  11200} <S20100>.
392e0 2a 2a 0a 2a 2a 20 41 20 76 69 72 74 75 61 6c 20  **.** A virtual 
392f0 66 69 6c 65 73 79 73 74 65 6d 20 28 56 46 53 29  filesystem (VFS)
39300 20 69 73 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f   is an [sqlite3_
39310 76 66 73 5d 20 6f 62 6a 65 63 74 0a 2a 2a 20 74  vfs] object.** t
39320 68 61 74 20 53 51 4c 69 74 65 20 75 73 65 73 20  hat SQLite uses 
39330 74 6f 20 69 6e 74 65 72 61 63 74 0a 2a 2a 20 77  to interact.** w
39340 69 74 68 20 74 68 65 20 75 6e 64 65 72 6c 79 69  ith the underlyi
39350 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  ng operating sys
39360 74 65 6d 2e 20 20 4d 6f 73 74 20 53 51 4c 69 74  tem.  Most SQLit
39370 65 20 62 75 69 6c 64 73 20 63 6f 6d 65 20 77 69  e builds come wi
39380 74 68 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 64  th a.** single d
39390 65 66 61 75 6c 74 20 56 46 53 20 74 68 61 74 20  efault VFS that 
393a0 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  is appropriate f
393b0 6f 72 20 74 68 65 20 68 6f 73 74 20 63 6f 6d 70  or the host comp
393c0 75 74 65 72 2e 0a 2a 2a 20 4e 65 77 20 56 46 53  uter..** New VFS
393d0 65 73 20 63 61 6e 20 62 65 20 72 65 67 69 73 74  es can be regist
393e0 65 72 65 64 20 61 6e 64 20 65 78 69 73 74 69 6e  ered and existin
393f0 67 20 56 46 53 65 73 20 63 61 6e 20 62 65 20 75  g VFSes can be u
39400 6e 72 65 67 69 73 74 65 72 65 64 2e 0a 2a 2a 20  nregistered..** 
39410 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e  The following in
39420 74 65 72 66 61 63 65 73 20 61 72 65 20 70 72 6f  terfaces are pro
39430 76 69 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  vided..**.** The
39440 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e   sqlite3_vfs_fin
39450 64 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65  d() interface re
39460 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
39470 74 6f 20 61 20 56 46 53 20 67 69 76 65 6e 20 69  to a VFS given i
39480 74 73 20 6e 61 6d 65 2e 0a 2a 2a 20 4e 61 6d 65  ts name..** Name
39490 73 20 61 72 65 20 63 61 73 65 20 73 65 6e 73 69  s are case sensi
394a0 74 69 76 65 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61  tive..** Names a
394b0 72 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  re zero-terminat
394c0 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73  ed UTF-8 strings
394d0 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73  ..** If there is
394e0 20 6e 6f 20 6d 61 74 63 68 2c 20 61 20 4e 55 4c   no match, a NUL
394f0 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74  L pointer is ret
39500 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 7a 56 66  urned..** If zVf
39510 73 4e 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74 68  sName is NULL th
39520 65 6e 20 74 68 65 20 64 65 66 61 75 6c 74 20 56  en the default V
39530 46 53 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  FS is returned..
39540 2a 2a 0a 2a 2a 20 4e 65 77 20 56 46 53 65 73 20  **.** New VFSes 
39550 61 72 65 20 72 65 67 69 73 74 65 72 65 64 20 77  are registered w
39560 69 74 68 20 73 71 6c 69 74 65 33 5f 76 66 73 5f  ith sqlite3_vfs_
39570 72 65 67 69 73 74 65 72 28 29 2e 0a 2a 2a 20 45  register()..** E
39580 61 63 68 20 6e 65 77 20 56 46 53 20 62 65 63 6f  ach new VFS beco
39590 6d 65 73 20 74 68 65 20 64 65 66 61 75 6c 74 20  mes the default 
395a0 56 46 53 20 69 66 20 74 68 65 20 6d 61 6b 65 44  VFS if the makeD
395b0 66 6c 74 20 66 6c 61 67 20 69 73 20 73 65 74 2e  flt flag is set.
395c0 0a 2a 2a 20 54 68 65 20 73 61 6d 65 20 56 46 53  .** The same VFS
395d0 20 63 61 6e 20 62 65 20 72 65 67 69 73 74 65 72   can be register
395e0 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  ed multiple time
395f0 73 20 77 69 74 68 6f 75 74 20 69 6e 6a 75 72 79  s without injury
39600 2e 0a 2a 2a 20 54 6f 20 6d 61 6b 65 20 61 6e 20  ..** To make an 
39610 65 78 69 73 74 69 6e 67 20 56 46 53 20 69 6e 74  existing VFS int
39620 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20 56 46  o the default VF
39630 53 2c 20 72 65 67 69 73 74 65 72 20 69 74 20 61  S, register it a
39640 67 61 69 6e 0a 2a 2a 20 77 69 74 68 20 74 68 65  gain.** with the
39650 20 6d 61 6b 65 44 66 6c 74 20 66 6c 61 67 20 73   makeDflt flag s
39660 65 74 2e 20 20 49 66 20 74 77 6f 20 64 69 66 66  et.  If two diff
39670 65 72 65 6e 74 20 56 46 53 65 73 20 77 69 74 68  erent VFSes with
39680 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6e 61 6d   the.** same nam
39690 65 20 61 72 65 20 72 65 67 69 73 74 65 72 65 64  e are registered
396a0 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  , the behavior i
396b0 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 49 66  s undefined.  If
396c0 20 61 0a 2a 2a 20 56 46 53 20 69 73 20 72 65 67   a.** VFS is reg
396d0 69 73 74 65 72 65 64 20 77 69 74 68 20 61 20 6e  istered with a n
396e0 61 6d 65 20 74 68 61 74 20 69 73 20 4e 55 4c 4c  ame that is NULL
396f0 20 6f 72 20 61 6e 20 65 6d 70 74 79 20 73 74 72   or an empty str
39700 69 6e 67 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ing,.** then the
39710 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64   behavior is und
39720 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 55 6e  efined..**.** Un
39730 72 65 67 69 73 74 65 72 20 61 20 56 46 53 20 77  register a VFS w
39740 69 74 68 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ith the sqlite3_
39750 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 28 29  vfs_unregister()
39760 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 49   interface..** I
39770 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 56 46  f the default VF
39780 53 20 69 73 20 75 6e 72 65 67 69 73 74 65 72 65  S is unregistere
39790 64 2c 20 61 6e 6f 74 68 65 72 20 56 46 53 20 69  d, another VFS i
397a0 73 20 63 68 6f 73 65 6e 20 61 73 0a 2a 2a 20 74  s chosen as.** t
397b0 68 65 20 64 65 66 61 75 6c 74 2e 20 20 54 68 65  he default.  The
397c0 20 63 68 6f 69 63 65 20 66 6f 72 20 74 68 65 20   choice for the 
397d0 6e 65 77 20 56 46 53 20 69 73 20 61 72 62 69 74  new VFS is arbit
397e0 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  rary..**.** Requ
397f0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
39800 31 32 30 33 5d 20 5b 48 31 31 32 30 36 5d 20 5b  1203] [H11206] [
39810 48 31 31 32 30 39 5d 20 5b 48 31 31 32 31 32 5d  H11209] [H11212]
39820 20 5b 48 31 31 32 31 35 5d 20 5b 48 31 31 32 31   [H11215] [H1121
39830 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  8].*/.SQLITE_API
39840 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 73 71   sqlite3_vfs *sq
39850 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 63  lite3_vfs_find(c
39860 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 4e  onst char *zVfsN
39870 61 6d 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ame);.SQLITE_API
39880 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73   int sqlite3_vfs
39890 5f 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 65  _register(sqlite
398a0 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6d 61 6b 65  3_vfs*, int make
398b0 44 66 6c 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50  Dflt);.SQLITE_AP
398c0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66  I int sqlite3_vf
398d0 73 5f 75 6e 72 65 67 69 73 74 65 72 28 73 71 6c  s_unregister(sql
398e0 69 74 65 33 5f 76 66 73 2a 29 3b 0a 0a 2f 2a 0a  ite3_vfs*);../*.
398f0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74  ** CAPI3REF: Mut
39900 65 78 65 73 20 7b 48 31 37 30 30 30 7d 20 3c 53  exes {H17000} <S
39910 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  20000>.**.** The
39920 20 53 51 4c 69 74 65 20 63 6f 72 65 20 75 73 65   SQLite core use
39930 73 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  s these routines
39940 20 66 6f 72 20 74 68 72 65 61 64 0a 2a 2a 20 73   for thread.** s
39950 79 6e 63 68 72 6f 6e 69 7a 61 74 69 6f 6e 2e 20  ynchronization. 
39960 54 68 6f 75 67 68 20 74 68 65 79 20 61 72 65 20  Though they are 
39970 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 69 6e 74  intended for int
39980 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20  ernal.** use by 
39990 53 51 4c 69 74 65 2c 20 63 6f 64 65 20 74 68 61  SQLite, code tha
399a0 74 20 6c 69 6e 6b 73 20 61 67 61 69 6e 73 74 20  t links against 
399b0 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20 70 65 72  SQLite is.** per
399c0 6d 69 74 74 65 64 20 74 6f 20 75 73 65 20 61 6e  mitted to use an
399d0 79 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69  y of these routi
399e0 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53  nes..**.** The S
399f0 51 4c 69 74 65 20 73 6f 75 72 63 65 20 63 6f 64  QLite source cod
39a00 65 20 63 6f 6e 74 61 69 6e 73 20 6d 75 6c 74 69  e contains multi
39a10 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ple implementati
39a20 6f 6e 73 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20  ons.** of these 
39a30 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 2e 20  mutex routines. 
39a40 20 41 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20   An appropriate 
39a50 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a  implementation.*
39a60 2a 20 69 73 20 73 65 6c 65 63 74 65 64 20 61 75  * is selected au
39a70 74 6f 6d 61 74 69 63 61 6c 6c 79 20 61 74 20 63  tomatically at c
39a80 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 20 20 54 68  ompile-time.  Th
39a90 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 69  e following.** i
39aa0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 61  mplementations a
39ab0 72 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20  re available in 
39ac0 74 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 3a  the SQLite core:
39ad0 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c  .**.** <ul>.** <
39ae0 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54  li>   SQLITE_MUT
39af0 45 58 5f 4f 53 32 0a 2a 2a 20 3c 6c 69 3e 20 20  EX_OS2.** <li>  
39b00 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54   SQLITE_MUTEX_PT
39b10 48 52 45 41 44 0a 2a 2a 20 3c 6c 69 3e 20 20 20  HREAD.** <li>   
39b20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32  SQLITE_MUTEX_W32
39b30 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54  .** <li>   SQLIT
39b40 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 2a 2a 20  E_MUTEX_NOOP.** 
39b50 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  </ul>.**.** The 
39b60 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f  SQLITE_MUTEX_NOO
39b70 50 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  P implementation
39b80 20 69 73 20 61 20 73 65 74 20 6f 66 20 72 6f 75   is a set of rou
39b90 74 69 6e 65 73 0a 2a 2a 20 74 68 61 74 20 64 6f  tines.** that do
39ba0 65 73 20 6e 6f 20 72 65 61 6c 20 6c 6f 63 6b 69  es no real locki
39bb0 6e 67 20 61 6e 64 20 69 73 20 61 70 70 72 6f 70  ng and is approp
39bc0 72 69 61 74 65 20 66 6f 72 20 75 73 65 20 69 6e  riate for use in
39bd0 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 2d 74 68 72  .** a single-thr
39be0 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f  eaded applicatio
39bf0 6e 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 4d  n.  The SQLITE_M
39c00 55 54 45 58 5f 4f 53 32 2c 0a 2a 2a 20 53 51 4c  UTEX_OS2,.** SQL
39c10 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41  ITE_MUTEX_PTHREA
39c20 44 2c 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55  D, and SQLITE_MU
39c30 54 45 58 5f 57 33 32 20 69 6d 70 6c 65 6d 65 6e  TEX_W32 implemen
39c40 74 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 61  tations.** are a
39c50 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 75  ppropriate for u
39c60 73 65 20 6f 6e 20 4f 53 2f 32 2c 20 55 6e 69 78  se on OS/2, Unix
39c70 2c 20 61 6e 64 20 57 69 6e 64 6f 77 73 2e 0a 2a  , and Windows..*
39c80 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69  *.** If SQLite i
39c90 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  s compiled with 
39ca0 74 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  the SQLITE_MUTEX
39cb0 5f 41 50 50 44 45 46 20 70 72 65 70 72 6f 63 65  _APPDEF preproce
39cc0 73 73 6f 72 0a 2a 2a 20 6d 61 63 72 6f 20 64 65  ssor.** macro de
39cd0 66 69 6e 65 64 20 28 77 69 74 68 20 22 2d 44 53  fined (with "-DS
39ce0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 44  QLITE_MUTEX_APPD
39cf0 45 46 3d 31 22 29 2c 20 74 68 65 6e 20 6e 6f 20  EF=1"), then no 
39d00 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65  mutex.** impleme
39d10 6e 74 61 74 69 6f 6e 20 69 73 20 69 6e 63 6c 75  ntation is inclu
39d20 64 65 64 20 77 69 74 68 20 74 68 65 20 6c 69 62  ded with the lib
39d30 72 61 72 79 2e 20 49 6e 20 74 68 69 73 20 63 61  rary. In this ca
39d40 73 65 20 74 68 65 0a 2a 2a 20 61 70 70 6c 69 63  se the.** applic
39d50 61 74 69 6f 6e 20 6d 75 73 74 20 73 75 70 70 6c  ation must suppl
39d60 79 20 61 20 63 75 73 74 6f 6d 20 6d 75 74 65 78  y a custom mutex
39d70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
39d80 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 53 51  using the.** [SQ
39d90 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45  LITE_CONFIG_MUTE
39da0 58 5d 20 6f 70 74 69 6f 6e 20 6f 66 20 74 68 65  X] option of the
39db0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
39dc0 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65  ) function.** be
39dd0 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c  fore calling sql
39de0 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
39df0 29 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70  ) or any other p
39e00 75 62 6c 69 63 20 73 71 6c 69 74 65 33 5f 0a 2a  ublic sqlite3_.*
39e10 2a 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  * function that 
39e20 63 61 6c 6c 73 20 73 71 6c 69 74 65 33 5f 69 6e  calls sqlite3_in
39e30 69 74 69 61 6c 69 7a 65 28 29 2e 0a 2a 2a 0a 2a  itialize()..**.*
39e40 2a 20 7b 48 31 37 30 31 31 7d 20 54 68 65 20 73  * {H17011} The s
39e50 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
39e60 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c  oc() routine all
39e70 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20  ocates a new.** 
39e80 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e  mutex and return
39e90 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  s a pointer to i
39ea0 74 2e 20 7b 48 31 37 30 31 32 7d 20 49 66 20 69  t. {H17012} If i
39eb0 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a  t returns NULL.*
39ec0 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61  * that means tha
39ed0 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64 20  t a mutex could 
39ee0 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64  not be allocated
39ef0 2e 20 7b 48 31 37 30 31 33 7d 20 53 51 4c 69 74  . {H17013} SQLit
39f00 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64  e.** will unwind
39f10 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72   its stack and r
39f20 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20  eturn an error. 
39f30 7b 48 31 37 30 31 34 7d 20 54 68 65 20 61 72 67  {H17014} The arg
39f40 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69  ument.** to sqli
39f50 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
39f60 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73  ) is one of thes
39f70 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61  e integer consta
39f80 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a  nts:.**.** <ul>.
39f90 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
39fa0 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 3c 6c  MUTEX_FAST.** <l
39fb0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
39fc0 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 3c 6c  _RECURSIVE.** <l
39fd0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
39fe0 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a  _STATIC_MASTER.*
39ff0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
3a000 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 0a  UTEX_STATIC_MEM.
3a010 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
3a020 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d  MUTEX_STATIC_MEM
3a030 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  2.** <li>  SQLIT
3a040 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 50  E_MUTEX_STATIC_P
3a050 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  RNG.** <li>  SQL
3a060 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
3a070 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  _LRU.** <li>  SQ
3a080 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
3a090 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f 75 6c 3e 0a  C_LRU2.** </ul>.
3a0a0 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 35 7d 20 54  **.** {H17015} T
3a0b0 68 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e  he first two con
3a0c0 73 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c  stants cause sql
3a0d0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
3a0e0 28 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20  () to create.** 
3a0f0 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68  a new mutex.  Th
3a100 65 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72  e new mutex is r
3a110 65 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 51  ecursive when SQ
3a120 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
3a130 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20  SIVE.** is used 
3a140 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72  but not necessar
3a150 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49  ily so when SQLI
3a160 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73  TE_MUTEX_FAST is
3a170 20 75 73 65 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 20   used. {END}.** 
3a180 54 68 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d  The mutex implem
3a190 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f  entation does no
3a1a0 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61  t need to make a
3a1b0 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20   distinction.** 
3a1c0 62 65 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d  between SQLITE_M
3a1d0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 61  UTEX_RECURSIVE a
3a1e0 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  nd SQLITE_MUTEX_
3a1f0 46 41 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a  FAST if it does.
3a200 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20  ** not want to. 
3a210 20 7b 48 31 37 30 31 36 7d 20 42 75 74 20 53 51   {H17016} But SQ
3a220 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72  Lite will only r
3a230 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69  equest a recursi
3a240 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63  ve mutex in.** c
3a250 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65  ases where it re
3a260 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20  ally needs one. 
3a270 20 7b 45 4e 44 7d 20 49 66 20 61 20 66 61 73 74   {END} If a fast
3a280 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65  er non-recursive
3a290 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d   mutex.** implem
3a2a0 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69  entation is avai
3a2b0 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73  lable on the hos
3a2c0 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20  t platform, the 
3a2d0 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a  mutex subsystem.
3a2e0 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20  ** might return 
3a2f0 73 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20  such a mutex in 
3a300 72 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49  response to SQLI
3a310 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a  TE_MUTEX_FAST..*
3a320 2a 0a 2a 2a 20 7b 48 31 37 30 31 37 7d 20 54 68  *.** {H17017} Th
3a330 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20  e other allowed 
3a340 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71  parameters to sq
3a350 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
3a360 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a  c() each return.
3a370 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
3a380 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73  a static preexis
3a390 74 69 6e 67 20 6d 75 74 65 78 2e 20 7b 45 4e 44  ting mutex. {END
3a3a0 7d 20 20 53 69 78 20 73 74 61 74 69 63 20 6d 75  }  Six static mu
3a3b0 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65  texes are.** use
3a3c0 64 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74  d by the current
3a3d0 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69   version of SQLi
3a3e0 74 65 2e 20 20 46 75 74 75 72 65 20 76 65 72 73  te.  Future vers
3a3f0 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a  ions of SQLite.*
3a400 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69 74 69  * may add additi
3a410 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65  onal static mute
3a420 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74  xes.  Static mut
3a430 65 78 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74  exes are for int
3a440 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20  ernal.** use by 
3a450 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70  SQLite only.  Ap
3a460 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20  plications that 
3a470 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78  use SQLite mutex
3a480 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65  es should.** use
3a490 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69   only the dynami
3a4a0 63 20 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e  c mutexes return
3a4b0 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54  ed by SQLITE_MUT
3a4c0 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51  EX_FAST or.** SQ
3a4d0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
3a4e0 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  SIVE..**.** {H17
3a4f0 30 31 38 7d 20 4e 6f 74 65 20 74 68 61 74 20 69  018} Note that i
3a500 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 79 6e  f one of the dyn
3a510 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72 61 6d  amic mutex param
3a520 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f 4d 55  eters (SQLITE_MU
3a530 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53  TEX_FAST.** or S
3a540 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
3a550 52 53 49 56 45 29 20 69 73 20 75 73 65 64 20 74  RSIVE) is used t
3a560 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  hen sqlite3_mute
3a570 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74  x_alloc().** ret
3a580 75 72 6e 73 20 61 20 64 69 66 66 65 72 65 6e 74  urns a different
3a590 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20   mutex on every 
3a5a0 63 61 6c 6c 2e 20 20 7b 48 31 37 30 33 34 7d 20  call.  {H17034} 
3a5b0 42 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74  But for the stat
3a5c0 69 63 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65  ic.** mutex type
3a5d0 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65  s, the same mute
3a5e0 78 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e  x is returned on
3a5f0 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74   every call that
3a600 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65   has.** the same
3a610 20 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a   type number..**
3a620 0a 2a 2a 20 7b 48 31 37 30 31 39 7d 20 54 68 65  .** {H17019} The
3a630 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66   sqlite3_mutex_f
3a640 72 65 65 28 29 20 72 6f 75 74 69 6e 65 20 64 65  ree() routine de
3a650 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76  allocates a prev
3a660 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61  iously.** alloca
3a670 74 65 64 20 64 79 6e 61 6d 69 63 20 6d 75 74 65  ted dynamic mute
3a680 78 2e 20 7b 48 31 37 30 32 30 7d 20 53 51 4c 69  x. {H17020} SQLi
3a690 74 65 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f  te is careful to
3a6a0 20 64 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72   deallocate ever
3a6b0 79 0a 2a 2a 20 64 79 6e 61 6d 69 63 20 6d 75 74  y.** dynamic mut
3a6c0 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 63  ex that it alloc
3a6d0 61 74 65 73 2e 20 7b 41 31 37 30 32 31 7d 20 54  ates. {A17021} T
3a6e0 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78  he dynamic mutex
3a6f0 65 73 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 69  es must not be i
3a700 6e 0a 2a 2a 20 75 73 65 20 77 68 65 6e 20 74 68  n.** use when th
3a710 65 79 20 61 72 65 20 64 65 61 6c 6c 6f 63 61 74  ey are deallocat
3a720 65 64 2e 20 7b 41 31 37 30 32 32 7d 20 41 74 74  ed. {A17022} Att
3a730 65 6d 70 74 69 6e 67 20 74 6f 20 64 65 61 6c 6c  empting to deall
3a740 6f 63 61 74 65 20 61 20 73 74 61 74 69 63 0a 2a  ocate a static.*
3a750 2a 20 6d 75 74 65 78 20 72 65 73 75 6c 74 73 20  * mutex results 
3a760 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65 68  in undefined beh
3a770 61 76 69 6f 72 2e 20 7b 48 31 37 30 32 33 7d 20  avior. {H17023} 
3a780 53 51 4c 69 74 65 20 6e 65 76 65 72 20 64 65 61  SQLite never dea
3a790 6c 6c 6f 63 61 74 65 73 0a 2a 2a 20 61 20 73 74  llocates.** a st
3a7a0 61 74 69 63 20 6d 75 74 65 78 2e 20 7b 45 4e 44  atic mutex. {END
3a7b0 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  }.**.** The sqli
3a7c0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
3a7d0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75  ) and sqlite3_mu
3a7e0 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e  tex_try() routin
3a7f0 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f  es attempt.** to
3a800 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20   enter a mutex. 
3a810 7b 48 31 37 30 32 34 7d 20 49 66 20 61 6e 6f 74  {H17024} If anot
3a820 68 65 72 20 74 68 72 65 61 64 20 69 73 20 61 6c  her thread is al
3a830 72 65 61 64 79 20 77 69 74 68 69 6e 20 74 68 65  ready within the
3a840 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74   mutex,.** sqlit
3a850 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
3a860 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20   will block and 
3a870 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
3a880 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a  y() will return.
3a890 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20  ** SQLITE_BUSY. 
3a8a0 7b 48 31 37 30 32 35 7d 20 20 54 68 65 20 73 71  {H17025}  The sq
3a8b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
3a8c0 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ) interface retu
3a8d0 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 0a  rns [SQLITE_OK].
3a8e0 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66  ** upon successf
3a8f0 75 6c 20 65 6e 74 72 79 2e 20 20 7b 48 31 37 30  ul entry.  {H170
3a900 32 36 7d 20 4d 75 74 65 78 65 73 20 63 72 65 61  26} Mutexes crea
3a910 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 53 51 4c  ted using.** SQL
3a920 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
3a930 49 56 45 20 63 61 6e 20 62 65 20 65 6e 74 65 72  IVE can be enter
3a940 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  ed multiple time
3a950 73 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68  s by the same th
3a960 72 65 61 64 2e 0a 2a 2a 20 7b 48 31 37 30 32 37  read..** {H17027
3a970 7d 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20  } In such cases 
3a980 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75  the,.** mutex mu
3a990 73 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20  st be exited an 
3a9a0 65 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  equal number of 
3a9b0 74 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f  times before ano
3a9c0 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63  ther thread.** c
3a9d0 61 6e 20 65 6e 74 65 72 2e 20 20 7b 41 31 37 30  an enter.  {A170
3a9e0 32 38 7d 20 49 66 20 74 68 65 20 73 61 6d 65 20  28} If the same 
3a9f0 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f 20  thread tries to 
3aa00 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72 0a  enter any other.
3aa10 2a 2a 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78  ** kind of mutex
3aa20 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c   more than once,
3aa30 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73   the behavior is
3aa40 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 7b   undefined..** {
3aa50 48 31 37 30 32 39 7d 20 53 51 4c 69 74 65 20 77  H17029} SQLite w
3aa60 69 6c 6c 20 6e 65 76 65 72 20 65 78 68 69 62 69  ill never exhibi
3aa70 74 0a 2a 2a 20 73 75 63 68 20 62 65 68 61 76 69  t.** such behavi
3aa80 6f 72 20 69 6e 20 69 74 73 20 6f 77 6e 20 75 73  or in its own us
3aa90 65 20 6f 66 20 6d 75 74 65 78 65 73 2e 0a 2a 2a  e of mutexes..**
3aaa0 0a 2a 2a 20 53 6f 6d 65 20 73 79 73 74 65 6d 73  .** Some systems
3aab0 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 57   (for example, W
3aac0 69 6e 64 6f 77 73 20 39 35 29 20 64 6f 20 6e 6f  indows 95) do no
3aad0 74 20 73 75 70 70 6f 72 74 20 74 68 65 20 6f 70  t support the op
3aae0 65 72 61 74 69 6f 6e 0a 2a 2a 20 69 6d 70 6c 65  eration.** imple
3aaf0 6d 65 6e 74 65 64 20 62 79 20 73 71 6c 69 74 65  mented by sqlite
3ab00 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 2e 20 20  3_mutex_try().  
3ab10 4f 6e 20 74 68 6f 73 65 20 73 79 73 74 65 6d 73  On those systems
3ab20 2c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  , sqlite3_mutex_
3ab30 74 72 79 28 29 0a 2a 2a 20 77 69 6c 6c 20 61 6c  try().** will al
3ab40 77 61 79 73 20 72 65 74 75 72 6e 20 53 51 4c 49  ways return SQLI
3ab50 54 45 5f 42 55 53 59 2e 20 20 7b 48 31 37 30 33  TE_BUSY.  {H1703
3ab60 30 7d 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f  0} The SQLite co
3ab70 72 65 20 6f 6e 6c 79 20 65 76 65 72 20 75 73 65  re only ever use
3ab80 73 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74  s.** sqlite3_mut
3ab90 65 78 5f 74 72 79 28 29 20 61 73 20 61 6e 20 6f  ex_try() as an o
3aba0 70 74 69 6d 69 7a 61 74 69 6f 6e 20 73 6f 20 74  ptimization so t
3abb0 68 69 73 20 69 73 20 61 63 63 65 70 74 61 62 6c  his is acceptabl
3abc0 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a  e behavior..**.*
3abd0 2a 20 7b 48 31 37 30 33 31 7d 20 54 68 65 20 73  * {H17031} The s
3abe0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
3abf0 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69  ve() routine exi
3ac00 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20  ts a mutex that 
3ac10 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c  was.** previousl
3ac20 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65  y entered by the
3ac30 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 7b   same thread.  {
3ac40 41 31 37 30 33 32 7d 20 54 68 65 20 62 65 68 61  A17032} The beha
3ac50 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66  vior.** is undef
3ac60 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65  ined if the mute
3ac70 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74  x is not current
3ac80 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 68  ly entered by th
3ac90 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 72  e.** calling thr
3aca0 65 61 64 20 6f 72 20 69 73 20 6e 6f 74 20 63 75  ead or is not cu
3acb0 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65  rrently allocate
3acc0 64 2e 20 20 7b 48 31 37 30 33 33 7d 20 53 51 4c  d.  {H17033} SQL
3acd0 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65  ite will.** neve
3ace0 72 20 64 6f 20 65 69 74 68 65 72 2e 20 7b 45 4e  r do either. {EN
3acf0 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  D}.**.** If the 
3ad00 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69  argument to sqli
3ad10 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
3ad20 29 2c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ), sqlite3_mutex
3ad30 5f 74 72 79 28 29 2c 20 6f 72 0a 2a 2a 20 73 71  _try(), or.** sq
3ad40 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
3ad50 65 28 29 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f  e() is a NULL po
3ad60 69 6e 74 65 72 2c 20 74 68 65 6e 20 61 6c 6c 20  inter, then all 
3ad70 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73 0a 2a  three routines.*
3ad80 2a 20 62 65 68 61 76 65 20 61 73 20 6e 6f 2d 6f  * behave as no-o
3ad90 70 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  ps..**.** See al
3ada0 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74  so: [sqlite3_mut
3adb0 65 78 5f 68 65 6c 64 28 29 5d 20 61 6e 64 20 5b  ex_held()] and [
3adc0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f  sqlite3_mutex_no
3add0 74 68 65 6c 64 28 29 5d 2e 0a 2a 2f 0a 53 51 4c  theld()]..*/.SQL
3ade0 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
3adf0 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 6d  mutex *sqlite3_m
3ae00 75 74 65 78 5f 61 6c 6c 6f 63 28 69 6e 74 29 3b  utex_alloc(int);
3ae10 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
3ae20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66   sqlite3_mutex_f
3ae30 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ree(sqlite3_mute
3ae40 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  x*);.SQLITE_API 
3ae50 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74  void sqlite3_mut
3ae60 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33  ex_enter(sqlite3
3ae70 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54 45  _mutex*);.SQLITE
3ae80 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
3ae90 5f 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74  _mutex_try(sqlit
3aea0 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49  e3_mutex*);.SQLI
3aeb0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
3aec0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
3aed0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b  sqlite3_mutex*);
3aee0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
3aef0 3a 20 4d 75 74 65 78 20 4d 65 74 68 6f 64 73 20  : Mutex Methods 
3af00 4f 62 6a 65 63 74 20 7b 48 31 37 31 32 30 7d 20  Object {H17120} 
3af10 3c 53 32 30 31 33 30 3e 0a 2a 2a 20 45 58 50 45  <S20130>.** EXPE
3af20 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 41  RIMENTAL.**.** A
3af30 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
3af40 69 73 20 73 74 72 75 63 74 75 72 65 20 64 65 66  is structure def
3af50 69 6e 65 73 20 74 68 65 20 6c 6f 77 2d 6c 65 76  ines the low-lev
3af60 65 6c 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 75  el routines.** u
3af70 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  sed to allocate 
3af80 61 6e 64 20 75 73 65 20 6d 75 74 65 78 65 73 2e  and use mutexes.
3af90 0a 2a 2a 0a 2a 2a 20 55 73 75 61 6c 6c 79 2c 20  .**.** Usually, 
3afa0 74 68 65 20 64 65 66 61 75 6c 74 20 6d 75 74 65  the default mute
3afb0 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
3afc0 73 20 70 72 6f 76 69 64 65 64 20 62 79 20 53 51  s provided by SQ
3afd0 4c 69 74 65 20 61 72 65 0a 2a 2a 20 73 75 66 66  Lite are.** suff
3afe0 69 63 69 65 6e 74 2c 20 68 6f 77 65 76 65 72 20  icient, however 
3aff0 74 68 65 20 75 73 65 72 20 68 61 73 20 74 68 65  the user has the
3b000 20 6f 70 74 69 6f 6e 20 6f 66 20 73 75 62 73 74   option of subst
3b010 69 74 75 74 69 6e 67 20 61 20 63 75 73 74 6f 6d  ituting a custom
3b020 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
3b030 6f 6e 20 66 6f 72 20 73 70 65 63 69 61 6c 69 7a  on for specializ
3b040 65 64 20 64 65 70 6c 6f 79 6d 65 6e 74 73 20 6f  ed deployments o
3b050 72 20 73 79 73 74 65 6d 73 20 66 6f 72 20 77 68  r systems for wh
3b060 69 63 68 20 53 51 4c 69 74 65 0a 2a 2a 20 64 6f  ich SQLite.** do
3b070 65 73 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 61  es not provide a
3b080 20 73 75 69 74 61 62 6c 65 20 69 6d 70 6c 65 6d   suitable implem
3b090 65 6e 74 61 74 69 6f 6e 2e 20 49 6e 20 74 68 69  entation. In thi
3b0a0 73 20 63 61 73 65 2c 20 74 68 65 20 75 73 65 72  s case, the user
3b0b0 0a 2a 2a 20 63 72 65 61 74 65 73 20 61 6e 64 20  .** creates and 
3b0c0 70 6f 70 75 6c 61 74 65 73 20 61 6e 20 69 6e 73  populates an ins
3b0d0 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
3b0e0 72 75 63 74 75 72 65 20 74 6f 20 70 61 73 73 0a  ructure to pass.
3b0f0 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  ** to sqlite3_co
3b100 6e 66 69 67 28 29 20 61 6c 6f 6e 67 20 77 69 74  nfig() along wit
3b110 68 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f  h the [SQLITE_CO
3b120 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 74 69  NFIG_MUTEX] opti
3b130 6f 6e 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61  on..** Additiona
3b140 6c 6c 79 2c 20 61 6e 20 69 6e 73 74 61 6e 63 65  lly, an instance
3b150 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
3b160 72 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 61  re can be used a
3b170 73 20 61 6e 0a 2a 2a 20 6f 75 74 70 75 74 20 76  s an.** output v
3b180 61 72 69 61 62 6c 65 20 77 68 65 6e 20 71 75 65  ariable when que
3b190 72 79 69 6e 67 20 74 68 65 20 73 79 73 74 65 6d  rying the system
3b1a0 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74   for the current
3b1b0 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d   mutex.** implem
3b1c0 65 6e 74 61 74 69 6f 6e 2c 20 75 73 69 6e 67 20  entation, using 
3b1d0 74 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  the [SQLITE_CONF
3b1e0 49 47 5f 47 45 54 4d 55 54 45 58 5d 20 6f 70 74  IG_GETMUTEX] opt
3b1f0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  ion..**.** The x
3b200 4d 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f 64  MutexInit method
3b210 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 69 73   defined by this
3b220 20 73 74 72 75 63 74 75 72 65 20 69 73 20 69 6e   structure is in
3b230 76 6f 6b 65 64 20 61 73 0a 2a 2a 20 70 61 72 74  voked as.** part
3b240 20 6f 66 20 73 79 73 74 65 6d 20 69 6e 69 74 69   of system initi
3b250 61 6c 69 7a 61 74 69 6f 6e 20 62 79 20 74 68 65  alization by the
3b260 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
3b270 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a  ize() function..
3b280 2a 2a 20 7b 48 31 37 30 30 31 7d 20 54 68 65 20  ** {H17001} The 
3b290 78 4d 75 74 65 78 49 6e 69 74 20 72 6f 75 74 69  xMutexInit routi
3b2a0 6e 65 20 73 68 61 6c 6c 20 62 65 20 63 61 6c 6c  ne shall be call
3b2b0 65 64 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 63  ed by SQLite onc
3b2c0 65 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 65 66  e for each.** ef
3b2d0 66 65 63 74 69 76 65 20 63 61 6c 6c 20 74 6f 20  fective call to 
3b2e0 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c  [sqlite3_initial
3b2f0 69 7a 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  ize()]..**.** Th
3b300 65 20 78 4d 75 74 65 78 45 6e 64 20 6d 65 74 68  e xMutexEnd meth
3b310 6f 64 20 64 65 66 69 6e 65 64 20 62 79 20 74 68  od defined by th
3b320 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
3b330 69 6e 76 6f 6b 65 64 20 61 73 0a 2a 2a 20 70 61  invoked as.** pa
3b340 72 74 20 6f 66 20 73 79 73 74 65 6d 20 73 68 75  rt of system shu
3b350 74 64 6f 77 6e 20 62 79 20 74 68 65 20 73 71 6c  tdown by the sql
3b360 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20  ite3_shutdown() 
3b370 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 0a 2a 2a  function. The.**
3b380 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3b390 6f 66 20 74 68 69 73 20 6d 65 74 68 6f 64 20 69  of this method i
3b3a0 73 20 65 78 70 65 63 74 65 64 20 74 6f 20 72 65  s expected to re
3b3b0 6c 65 61 73 65 20 61 6c 6c 20 6f 75 74 73 74 61  lease all outsta
3b3c0 6e 64 69 6e 67 0a 2a 2a 20 72 65 73 6f 75 72 63  nding.** resourc
3b3d0 65 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74  es obtained by t
3b3e0 68 65 20 6d 75 74 65 78 20 6d 65 74 68 6f 64 73  he mutex methods
3b3f0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c   implementation,
3b400 20 65 73 70 65 63 69 61 6c 6c 79 0a 2a 2a 20 74   especially.** t
3b410 68 6f 73 65 20 6f 62 74 61 69 6e 65 64 20 62 79  hose obtained by
3b420 20 74 68 65 20 78 4d 75 74 65 78 49 6e 69 74 20   the xMutexInit 
3b430 6d 65 74 68 6f 64 2e 20 7b 48 31 37 30 30 33 7d  method. {H17003}
3b440 20 54 68 65 20 78 4d 75 74 65 78 45 6e 64 28 29   The xMutexEnd()
3b450 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 73 68  .** interface sh
3b460 61 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 6f  all be invoked o
3b470 6e 63 65 20 66 6f 72 20 65 61 63 68 20 63 61 6c  nce for each cal
3b480 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 68  l to [sqlite3_sh
3b490 75 74 64 6f 77 6e 28 29 5d 2e 0a 2a 2a 0a 2a 2a  utdown()]..**.**
3b4a0 20 54 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 73   The remaining s
3b4b0 65 76 65 6e 20 6d 65 74 68 6f 64 73 20 64 65 66  even methods def
3b4c0 69 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72  ined by this str
3b4d0 75 63 74 75 72 65 20 28 78 4d 75 74 65 78 41 6c  ucture (xMutexAl
3b4e0 6c 6f 63 2c 0a 2a 2a 20 78 4d 75 74 65 78 46 72  loc,.** xMutexFr
3b4f0 65 65 2c 20 78 4d 75 74 65 78 45 6e 74 65 72 2c  ee, xMutexEnter,
3b500 20 78 4d 75 74 65 78 54 72 79 2c 20 78 4d 75 74   xMutexTry, xMut
3b510 65 78 4c 65 61 76 65 2c 20 78 4d 75 74 65 78 48  exLeave, xMutexH
3b520 65 6c 64 20 61 6e 64 0a 2a 2a 20 78 4d 75 74 65  eld and.** xMute
3b530 78 4e 6f 74 68 65 6c 64 29 20 69 6d 70 6c 65 6d  xNotheld) implem
3b540 65 6e 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ent the followin
3b550 67 20 69 6e 74 65 72 66 61 63 65 73 20 28 72 65  g interfaces (re
3b560 73 70 65 63 74 69 76 65 6c 79 29 3a 0a 2a 2a 0a  spectively):.**.
3b570 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69  ** <ul>.**   <li
3b580 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  >  [sqlite3_mute
3b590 78 5f 61 6c 6c 6f 63 28 29 5d 20 3c 2f 6c 69 3e  x_alloc()] </li>
3b5a0 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c  .**   <li>  [sql
3b5b0 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28  ite3_mutex_free(
3b5c0 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c  )] </li>.**   <l
3b5d0 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74  i>  [sqlite3_mut
3b5e0 65 78 5f 65 6e 74 65 72 28 29 5d 20 3c 2f 6c 69  ex_enter()] </li
3b5f0 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71  >.**   <li>  [sq
3b600 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
3b610 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c  )] </li>.**   <l
3b620 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74  i>  [sqlite3_mut
3b630 65 78 5f 6c 65 61 76 65 28 29 5d 20 3c 2f 6c 69  ex_leave()] </li
3b640 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71  >.**   <li>  [sq
3b650 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
3b660 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c  ()] </li>.**   <
3b670 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75  li>  [sqlite3_mu
3b680 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 5d 20 3c  tex_notheld()] <
3b690 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a  /li>.** </ul>.**
3b6a0 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 64 69 66  .** The only dif
3b6b0 66 65 72 65 6e 63 65 20 69 73 20 74 68 61 74 20  ference is that 
3b6c0 74 68 65 20 70 75 62 6c 69 63 20 73 71 6c 69 74  the public sqlit
3b6d0 65 33 5f 58 58 58 20 66 75 6e 63 74 69 6f 6e 73  e3_XXX functions
3b6e0 20 65 6e 75 6d 65 72 61 74 65 64 0a 2a 2a 20 61   enumerated.** a
3b6f0 62 6f 76 65 20 73 69 6c 65 6e 74 6c 79 20 69 67  bove silently ig
3b700 6e 6f 72 65 20 61 6e 79 20 69 6e 76 6f 63 61 74  nore any invocat
3b710 69 6f 6e 73 20 74 68 61 74 20 70 61 73 73 20 61  ions that pass a
3b720 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 6e   NULL pointer in
3b730 73 74 65 61 64 0a 2a 2a 20 6f 66 20 61 20 76 61  stead.** of a va
3b740 6c 69 64 20 6d 75 74 65 78 20 68 61 6e 64 6c 65  lid mutex handle
3b750 2e 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  . The implementa
3b760 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 6d 65 74  tions of the met
3b770 68 6f 64 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20  hods defined.** 
3b780 62 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72  by this structur
3b790 65 20 61 72 65 20 6e 6f 74 20 72 65 71 75 69 72  e are not requir
3b7a0 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 69  ed to handle thi
3b7b0 73 20 63 61 73 65 2c 20 74 68 65 20 72 65 73 75  s case, the resu
3b7c0 6c 74 73 0a 2a 2a 20 6f 66 20 70 61 73 73 69 6e  lts.** of passin
3b7d0 67 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  g a NULL pointer
3b7e0 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20 76 61   instead of a va
3b7f0 6c 69 64 20 6d 75 74 65 78 20 68 61 6e 64 6c 65  lid mutex handle
3b800 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 0a 2a   are undefined.*
3b810 2a 20 28 69 2e 65 2e 20 69 74 20 69 73 20 61 63  * (i.e. it is ac
3b820 63 65 70 74 61 62 6c 65 20 74 6f 20 70 72 6f 76  ceptable to prov
3b830 69 64 65 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74  ide an implement
3b840 61 74 69 6f 6e 20 74 68 61 74 20 73 65 67 66 61  ation that segfa
3b850 75 6c 74 73 20 69 66 0a 2a 2a 20 69 74 20 69 73  ults if.** it is
3b860 20 70 61 73 73 65 64 20 61 20 4e 55 4c 4c 20 70   passed a NULL p
3b870 6f 69 6e 74 65 72 29 2e 0a 2a 2a 0a 2a 2a 20 54  ointer)..**.** T
3b880 68 65 20 78 4d 75 74 65 78 49 6e 69 74 28 29 20  he xMutexInit() 
3b890 6d 65 74 68 6f 64 20 6d 75 73 74 20 62 65 20 74  method must be t
3b8a0 68 72 65 61 64 73 61 66 65 2e 20 20 49 74 20 6d  hreadsafe.  It m
3b8b0 75 73 74 20 62 65 20 68 61 72 6d 6c 65 73 73 20  ust be harmless 
3b8c0 74 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 20 78 4d 75  to.** invoke xMu
3b8d0 74 65 78 49 6e 69 74 28 29 20 6d 75 74 69 70 6c  texInit() mutipl
3b8e0 65 20 74 69 6d 65 73 20 77 69 74 68 69 6e 20 74  e times within t
3b8f0 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20  he same process 
3b900 61 6e 64 20 77 69 74 68 6f 75 74 0a 2a 2a 20 69  and without.** i
3b910 6e 74 65 72 76 65 6e 69 6e 67 20 63 61 6c 6c 73  ntervening calls
3b920 20 74 6f 20 78 4d 75 74 65 78 45 6e 64 28 29 2e   to xMutexEnd().
3b930 20 20 53 65 63 6f 6e 64 20 61 6e 64 20 73 75 62    Second and sub
3b940 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f  sequent calls to
3b950 0a 2a 2a 20 78 4d 75 74 65 78 49 6e 69 74 28 29  .** xMutexInit()
3b960 20 6d 75 73 74 20 62 65 20 6e 6f 2d 6f 70 73 2e   must be no-ops.
3b970 0a 2a 2a 0a 2a 2a 20 78 4d 75 74 65 78 49 6e 69  .**.** xMutexIni
3b980 74 28 29 20 6d 75 73 74 20 6e 6f 74 20 75 73 65  t() must not use
3b990 20 53 51 4c 69 74 65 20 6d 65 6d 6f 72 79 20 61   SQLite memory a
3b9a0 6c 6c 6f 63 61 74 69 6f 6e 20 28 5b 73 71 6c 69  llocation ([sqli
3b9b0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 0a 2a 2a  te3_malloc()].**
3b9c0 20 61 6e 64 20 69 74 73 20 61 73 73 6f 63 69 61   and its associa
3b9d0 74 65 73 29 2e 20 20 53 69 6d 69 6c 61 72 6c 79  tes).  Similarly
3b9e0 2c 20 78 4d 75 74 65 78 41 6c 6c 6f 63 28 29 20  , xMutexAlloc() 
3b9f0 6d 75 73 74 20 6e 6f 74 20 75 73 65 20 53 51 4c  must not use SQL
3ba00 69 74 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c  ite memory.** al
3ba10 6c 6f 63 61 74 69 6f 6e 20 66 6f 72 20 61 20 73  location for a s
3ba20 74 61 74 69 63 20 6d 75 74 65 78 2e 20 20 48 6f  tatic mutex.  Ho
3ba30 77 65 76 65 72 20 78 4d 75 74 65 78 41 6c 6c 6f  wever xMutexAllo
3ba40 63 28 29 20 6d 61 79 20 75 73 65 20 53 51 4c 69  c() may use SQLi
3ba50 74 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c  te.** memory all
3ba60 6f 63 61 74 69 6f 6e 20 66 6f 72 20 61 20 66 61  ocation for a fa
3ba70 73 74 20 6f 72 20 72 65 63 75 72 73 69 76 65 20  st or recursive 
3ba80 6d 75 74 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  mutex..**.** SQL
3ba90 69 74 65 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20  ite will invoke 
3baa0 74 68 65 20 78 4d 75 74 65 78 45 6e 64 28 29 20  the xMutexEnd() 
3bab0 6d 65 74 68 6f 64 20 77 68 65 6e 20 5b 73 71 6c  method when [sql
3bac0 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d  ite3_shutdown()]
3bad0 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2c 20 62   is.** called, b
3bae0 75 74 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 70  ut only if the p
3baf0 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 78 4d 75  rior call to xMu
3bb00 74 65 78 49 6e 69 74 20 72 65 74 75 72 6e 65 64  texInit returned
3bb10 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 49   SQLITE_OK..** I
3bb20 66 20 78 4d 75 74 65 78 49 6e 69 74 20 66 61 69  f xMutexInit fai
3bb30 6c 73 20 69 6e 20 61 6e 79 20 77 61 79 2c 20 69  ls in any way, i
3bb40 74 20 69 73 20 65 78 70 65 63 74 65 64 20 74 6f  t is expected to
3bb50 20 63 6c 65 61 6e 20 75 70 20 61 66 74 65 72 20   clean up after 
3bb60 69 74 73 65 6c 66 0a 2a 2a 20 70 72 69 6f 72 20  itself.** prior 
3bb70 74 6f 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f  to returning..*/
3bb80 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
3bb90 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
3bba0 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 6d 75  thods sqlite3_mu
3bbb0 74 65 78 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72  tex_methods;.str
3bbc0 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  uct sqlite3_mute
3bbd0 78 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 69 6e  x_methods {.  in
3bbe0 74 20 28 2a 78 4d 75 74 65 78 49 6e 69 74 29 28  t (*xMutexInit)(
3bbf0 76 6f 69 64 29 3b 0a 20 20 69 6e 74 20 28 2a 78  void);.  int (*x
3bc00 4d 75 74 65 78 45 6e 64 29 28 76 6f 69 64 29 3b  MutexEnd)(void);
3bc10 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
3bc20 20 2a 28 2a 78 4d 75 74 65 78 41 6c 6c 6f 63 29   *(*xMutexAlloc)
3bc30 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a  (int);.  void (*
3bc40 78 4d 75 74 65 78 46 72 65 65 29 28 73 71 6c 69  xMutexFree)(sqli
3bc50 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20  te3_mutex *);.  
3bc60 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 45 6e 74  void (*xMutexEnt
3bc70 65 72 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  er)(sqlite3_mute
3bc80 78 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d  x *);.  int (*xM
3bc90 75 74 65 78 54 72 79 29 28 73 71 6c 69 74 65 33  utexTry)(sqlite3
3bca0 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 76 6f 69  _mutex *);.  voi
3bcb0 64 20 28 2a 78 4d 75 74 65 78 4c 65 61 76 65 29  d (*xMutexLeave)
3bcc0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
3bcd0 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65  );.  int (*xMute
3bce0 78 48 65 6c 64 29 28 73 71 6c 69 74 65 33 5f 6d  xHeld)(sqlite3_m
3bcf0 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20 28  utex *);.  int (
3bd00 2a 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 29 28  *xMutexNotheld)(
3bd10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29  sqlite3_mutex *)
3bd20 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ;.};../*.** CAPI
3bd30 33 52 45 46 3a 20 4d 75 74 65 78 20 56 65 72 69  3REF: Mutex Veri
3bd40 66 69 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65  fication Routine
3bd50 73 20 7b 48 31 37 30 38 30 7d 20 3c 53 32 30 31  s {H17080} <S201
3bd60 33 30 3e 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a  30> <S30800>.**.
3bd70 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  ** The sqlite3_m
3bd80 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20  utex_held() and 
3bd90 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f  sqlite3_mutex_no
3bda0 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 73  theld() routines
3bdb0 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e 64 65 64  .** are intended
3bdc0 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 20   for use inside 
3bdd0 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
3bde0 6e 74 73 2e 20 7b 48 31 37 30 38 31 7d 20 54 68  nts. {H17081} Th
3bdf0 65 20 53 51 4c 69 74 65 20 63 6f 72 65 0a 2a 2a  e SQLite core.**
3be00 20 6e 65 76 65 72 20 75 73 65 73 20 74 68 65 73   never uses thes
3be10 65 20 72 6f 75 74 69 6e 65 73 20 65 78 63 65 70  e routines excep
3be20 74 20 69 6e 73 69 64 65 20 61 6e 20 61 73 73 65  t inside an asse
3be30 72 74 28 29 20 61 6e 64 20 61 70 70 6c 69 63 61  rt() and applica
3be40 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 61 64 76  tions.** are adv
3be50 69 73 65 64 20 74 6f 20 66 6f 6c 6c 6f 77 20 74  ised to follow t
3be60 68 65 20 6c 65 61 64 20 6f 66 20 74 68 65 20 63  he lead of the c
3be70 6f 72 65 2e 20 20 7b 48 31 37 30 38 32 7d 20 54  ore.  {H17082} T
3be80 68 65 20 63 6f 72 65 20 6f 6e 6c 79 0a 2a 2a 20  he core only.** 
3be90 70 72 6f 76 69 64 65 73 20 69 6d 70 6c 65 6d 65  provides impleme
3bea0 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 74 68 65  ntations for the
3beb0 73 65 20 72 6f 75 74 69 6e 65 73 20 77 68 65 6e  se routines when
3bec0 20 69 74 20 69 73 20 63 6f 6d 70 69 6c 65 64 0a   it is compiled.
3bed0 2a 2a 20 77 69 74 68 20 74 68 65 20 53 51 4c 49  ** with the SQLI
3bee0 54 45 5f 44 45 42 55 47 20 66 6c 61 67 2e 20 20  TE_DEBUG flag.  
3bef0 7b 41 31 37 30 38 37 7d 20 45 78 74 65 72 6e 61  {A17087} Externa
3bf00 6c 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e  l mutex implemen
3bf10 74 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6f  tations.** are o
3bf20 6e 6c 79 20 72 65 71 75 69 72 65 64 20 74 6f 20  nly required to 
3bf30 70 72 6f 76 69 64 65 20 74 68 65 73 65 20 72 6f  provide these ro
3bf40 75 74 69 6e 65 73 20 69 66 20 53 51 4c 49 54 45  utines if SQLITE
3bf50 5f 44 45 42 55 47 20 69 73 0a 2a 2a 20 64 65 66  _DEBUG is.** def
3bf60 69 6e 65 64 20 61 6e 64 20 69 66 20 4e 44 45 42  ined and if NDEB
3bf70 55 47 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65  UG is not define
3bf80 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 38 33  d..**.** {H17083
3bf90 7d 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  } These routines
3bfa0 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74   should return t
3bfb0 72 75 65 20 69 66 20 74 68 65 20 6d 75 74 65 78  rue if the mutex
3bfc0 20 69 6e 20 74 68 65 69 72 20 61 72 67 75 6d 65   in their argume
3bfd0 6e 74 0a 2a 2a 20 69 73 20 68 65 6c 64 20 6f 72  nt.** is held or
3bfe0 20 6e 6f 74 20 68 65 6c 64 2c 20 72 65 73 70 65   not held, respe
3bff0 63 74 69 76 65 6c 79 2c 20 62 79 20 74 68 65 20  ctively, by the 
3c000 63 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 2e 0a  calling thread..
3c010 2a 2a 0a 2a 2a 20 7b 58 31 37 30 38 34 7d 20 54  **.** {X17084} T
3c020 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
3c030 6e 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65  n is not require
3c040 64 20 74 6f 20 70 72 6f 76 69 64 65 64 20 76 65  d to provided ve
3c050 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 73 65 0a  rsions of these.
3c060 2a 2a 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  ** routines that
3c070 20 61 63 74 75 61 6c 6c 79 20 77 6f 72 6b 2e 20   actually work. 
3c080 49 66 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  If the implement
3c090 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 70  ation does not p
3c0a0 72 6f 76 69 64 65 20 77 6f 72 6b 69 6e 67 0a 2a  rovide working.*
3c0b0 2a 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68  * versions of th
3c0c0 65 73 65 20 72 6f 75 74 69 6e 65 73 2c 20 69 74  ese routines, it
3c0d0 20 73 68 6f 75 6c 64 20 61 74 20 6c 65 61 73 74   should at least
3c0e0 20 70 72 6f 76 69 64 65 20 73 74 75 62 73 20 74   provide stubs t
3c0f0 68 61 74 20 61 6c 77 61 79 73 0a 2a 2a 20 72 65  hat always.** re
3c100 74 75 72 6e 20 74 72 75 65 20 73 6f 20 74 68 61  turn true so tha
3c110 74 20 6f 6e 65 20 64 6f 65 73 20 6e 6f 74 20 67  t one does not g
3c120 65 74 20 73 70 75 72 69 6f 75 73 20 61 73 73 65  et spurious asse
3c130 72 74 69 6f 6e 20 66 61 69 6c 75 72 65 73 2e 0a  rtion failures..
3c140 2a 2a 0a 2a 2a 20 7b 48 31 37 30 38 35 7d 20 49  **.** {H17085} I
3c150 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74  f the argument t
3c160 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  o sqlite3_mutex_
3c170 68 65 6c 64 28 29 20 69 73 20 61 20 4e 55 4c 4c  held() is a NULL
3c180 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 0a 2a 2a   pointer then.**
3c190 20 74 68 65 20 72 6f 75 74 69 6e 65 20 73 68 6f   the routine sho
3c1a0 75 6c 64 20 72 65 74 75 72 6e 20 31 2e 20 20 7b  uld return 1.  {
3c1b0 45 4e 44 7d 20 54 68 69 73 20 73 65 65 6d 73 20  END} This seems 
3c1c0 63 6f 75 6e 74 65 72 2d 69 6e 74 75 69 74 69 76  counter-intuitiv
3c1d0 65 20 73 69 6e 63 65 0a 2a 2a 20 63 6c 65 61 72  e since.** clear
3c1e0 6c 79 20 74 68 65 20 6d 75 74 65 78 20 63 61 6e  ly the mutex can
3c1f0 6e 6f 74 20 62 65 20 68 65 6c 64 20 69 66 20 69  not be held if i
3c200 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74  t does not exist
3c210 2e 20 20 42 75 74 20 74 68 65 0a 2a 2a 20 74 68  .  But the.** th
3c220 65 20 72 65 61 73 6f 6e 20 74 68 65 20 6d 75 74  e reason the mut
3c230 65 78 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  ex does not exis
3c240 74 20 69 73 20 62 65 63 61 75 73 65 20 74 68 65  t is because the
3c250 20 62 75 69 6c 64 20 69 73 20 6e 6f 74 0a 2a 2a   build is not.**
3c260 20 75 73 69 6e 67 20 6d 75 74 65 78 65 73 2e 20   using mutexes. 
3c270 20 41 6e 64 20 77 65 20 64 6f 20 6e 6f 74 20 77   And we do not w
3c280 61 6e 74 20 74 68 65 20 61 73 73 65 72 74 28 29  ant the assert()
3c290 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 0a   containing the.
3c2a0 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  ** call to sqlit
3c2b0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20  e3_mutex_held() 
3c2c0 74 6f 20 66 61 69 6c 2c 20 73 6f 20 61 20 6e 6f  to fail, so a no
3c2d0 6e 2d 7a 65 72 6f 20 72 65 74 75 72 6e 20 69 73  n-zero return is
3c2e0 0a 2a 2a 20 74 68 65 20 61 70 70 72 6f 70 72 69  .** the appropri
3c2f0 61 74 65 20 74 68 69 6e 67 20 74 6f 20 64 6f 2e  ate thing to do.
3c300 20 20 7b 48 31 37 30 38 36 7d 20 54 68 65 20 73    {H17086} The s
3c310 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
3c320 68 65 6c 64 28 29 0a 2a 2a 20 69 6e 74 65 72 66  held().** interf
3c330 61 63 65 20 73 68 6f 75 6c 64 20 61 6c 73 6f 20  ace should also 
3c340 72 65 74 75 72 6e 20 31 20 77 68 65 6e 20 67 69  return 1 when gi
3c350 76 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  ven a NULL point
3c360 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  er..*/.SQLITE_AP
3c370 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75  I int sqlite3_mu
3c380 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33  tex_held(sqlite3
3c390 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54 45  _mutex*);.SQLITE
3c3a0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
3c3b0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 73  _mutex_notheld(s
3c3c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a  qlite3_mutex*);.
3c3d0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
3c3e0 20 4d 75 74 65 78 20 54 79 70 65 73 20 7b 48 31   Mutex Types {H1
3c3f0 37 30 30 31 7d 20 3c 48 31 37 30 30 30 3e 0a 2a  7001} <H17000>.*
3c400 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65  *.** The [sqlite
3c410 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d  3_mutex_alloc()]
3c420 20 69 6e 74 65 72 66 61 63 65 20 74 61 6b 65 73   interface takes
3c430 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65   a single argume
3c440 6e 74 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 6f  nt.** which is o
3c450 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65  ne of these inte
3c460 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 2e 0a 2a  ger constants..*
3c470 2a 0a 2a 2a 20 54 68 65 20 73 65 74 20 6f 66 20  *.** The set of 
3c480 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 6d  static mutexes m
3c490 61 79 20 63 68 61 6e 67 65 20 66 72 6f 6d 20 6f  ay change from o
3c4a0 6e 65 20 53 51 4c 69 74 65 20 72 65 6c 65 61 73  ne SQLite releas
3c4b0 65 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 65 78 74  e to the.** next
3c4c0 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20  .  Applications 
3c4d0 74 68 61 74 20 6f 76 65 72 72 69 64 65 20 74 68  that override th
3c4e0 65 20 62 75 69 6c 74 2d 69 6e 20 6d 75 74 65 78  e built-in mutex
3c4f0 20 6c 6f 67 69 63 20 6d 75 73 74 20 62 65 0a 2a   logic must be.*
3c500 2a 20 70 72 65 70 61 72 65 64 20 74 6f 20 61 63  * prepared to ac
3c510 63 6f 6d 6d 6f 64 61 74 65 20 61 64 64 69 74 69  commodate additi
3c520 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65  onal static mute
3c530 78 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  xes..*/.#define 
3c540 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
3c550 54 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a  T             0.
3c560 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
3c570 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 20  UTEX_RECURSIVE  
3c580 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20        1.#define 
3c590 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
3c5a0 54 49 43 5f 4d 41 53 54 45 52 20 20 20 20 32 0a  TIC_MASTER    2.
3c5b0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
3c5c0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20  UTEX_STATIC_MEM 
3c5d0 20 20 20 20 20 20 33 20 20 2f 2a 20 73 71 6c 69        3  /* sqli
3c5e0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a  te3_malloc() */.
3c5f0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
3c600 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32  UTEX_STATIC_MEM2
3c610 20 20 20 20 20 20 34 20 20 2f 2a 20 4e 4f 54 20        4  /* NOT 
3c620 55 53 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20  USED */.#define 
3c630 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
3c640 54 49 43 5f 4f 50 45 4e 20 20 20 20 20 20 34 20  TIC_OPEN      4 
3c650 20 2f 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65   /* sqlite3Btree
3c660 4f 70 65 6e 28 29 20 2a 2f 0a 23 64 65 66 69 6e  Open() */.#defin
3c670 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  e SQLITE_MUTEX_S
3c680 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20 20  TATIC_PRNG      
3c690 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 72 61  5  /* sqlite3_ra
3c6a0 6e 64 6f 6d 28 29 20 2a 2f 0a 23 64 65 66 69 6e  ndom() */.#defin
3c6b0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  e SQLITE_MUTEX_S
3c6c0 54 41 54 49 43 5f 4c 52 55 20 20 20 20 20 20 20  TATIC_LRU       
3c6d0 36 20 20 2f 2a 20 6c 72 75 20 70 61 67 65 20 6c  6  /* lru page l
3c6e0 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ist */.#define S
3c6f0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
3c700 49 43 5f 4c 52 55 32 20 20 20 20 20 20 37 20 20  IC_LRU2      7  
3c710 2f 2a 20 6c 72 75 20 70 61 67 65 20 6c 69 73 74  /* lru page list
3c720 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33   */../*.** CAPI3
3c730 52 45 46 3a 20 52 65 74 72 69 65 76 65 20 74 68  REF: Retrieve th
3c740 65 20 6d 75 74 65 78 20 66 6f 72 20 61 20 64 61  e mutex for a da
3c750 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3c760 6e 20 7b 48 31 37 30 30 32 7d 20 3c 48 31 37 30  n {H17002} <H170
3c770 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  00>.**.** This i
3c780 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
3c790 20 61 20 70 6f 69 6e 74 65 72 20 74 68 65 20 5b   a pointer the [
3c7a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20 6f  sqlite3_mutex] o
3c7b0 62 6a 65 63 74 20 74 68 61 74 20 0a 2a 2a 20 73  bject that .** s
3c7c0 65 72 69 61 6c 69 7a 65 73 20 61 63 63 65 73 73  erializes access
3c7d0 20 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73   to the [databas
3c7e0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 67 69  e connection] gi
3c7f0 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75 6d  ven in the argum
3c800 65 6e 74 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20  ent.** when the 
3c810 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d  [threading mode]
3c820 20 69 73 20 53 65 72 69 61 6c 69 7a 65 64 2e 0a   is Serialized..
3c830 2a 2a 20 49 66 20 74 68 65 20 5b 74 68 72 65 61  ** If the [threa
3c840 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 53 69  ding mode] is Si
3c850 6e 67 6c 65 2d 74 68 72 65 61 64 20 6f 72 20 4d  ngle-thread or M
3c860 75 6c 74 69 2d 74 68 72 65 61 64 20 74 68 65 6e  ulti-thread then
3c870 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   this.** routine
3c880 20 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20   returns a NULL 
3c890 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49  pointer..*/.SQLI
3c8a0 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 6d  TE_API sqlite3_m
3c8b0 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 64 62  utex *sqlite3_db
3c8c0 5f 6d 75 74 65 78 28 73 71 6c 69 74 65 33 2a 29  _mutex(sqlite3*)
3c8d0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
3c8e0 46 3a 20 4c 6f 77 2d 4c 65 76 65 6c 20 43 6f 6e  F: Low-Level Con
3c8f0 74 72 6f 6c 20 4f 66 20 44 61 74 61 62 61 73 65  trol Of Database
3c900 20 46 69 6c 65 73 20 7b 48 31 31 33 30 30 7d 20   Files {H11300} 
3c910 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 7b  <S30800>.**.** {
3c920 48 31 31 33 30 31 7d 20 54 68 65 20 5b 73 71 6c  H11301} The [sql
3c930 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f  ite3_file_contro
3c940 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 6d  l()] interface m
3c950 61 6b 65 73 20 61 20 64 69 72 65 63 74 20 63 61  akes a direct ca
3c960 6c 6c 20 74 6f 20 74 68 65 0a 2a 2a 20 78 46 69  ll to the.** xFi
3c970 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64  leControl method
3c980 20 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65   for the [sqlite
3c990 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62  3_io_methods] ob
3c9a0 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65 64 0a  ject associated.
3c9b0 2a 2a 20 77 69 74 68 20 61 20 70 61 72 74 69 63  ** with a partic
3c9c0 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 69 64  ular database id
3c9d0 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20  entified by the 
3c9e0 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e  second argument.
3c9f0 20 7b 48 31 31 33 30 32 7d 20 54 68 65 0a 2a 2a   {H11302} The.**
3ca00 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   name of the dat
3ca10 61 62 61 73 65 20 69 73 20 74 68 65 20 6e 61 6d  abase is the nam
3ca20 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68  e assigned to th
3ca30 65 20 64 61 74 61 62 61 73 65 20 62 79 20 74 68  e database by th
3ca40 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d 22 6c 61  e.** <a href="la
3ca50 6e 67 5f 61 74 74 61 63 68 2e 68 74 6d 6c 22 3e  ng_attach.html">
3ca60 41 54 54 41 43 48 3c 2f 61 3e 20 53 51 4c 20 63  ATTACH</a> SQL c
3ca70 6f 6d 6d 61 6e 64 20 74 68 61 74 20 6f 70 65 6e  ommand that open
3ca80 65 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  ed the.** databa
3ca90 73 65 2e 20 7b 48 31 31 33 30 33 7d 20 54 6f 20  se. {H11303} To 
3caa0 63 6f 6e 74 72 6f 6c 20 74 68 65 20 6d 61 69 6e  control the main
3cab0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
3cac0 75 73 65 20 74 68 65 20 6e 61 6d 65 20 22 6d 61  use the name "ma
3cad0 69 6e 22 0a 2a 2a 20 6f 72 20 61 20 4e 55 4c 4c  in".** or a NULL
3cae0 20 70 6f 69 6e 74 65 72 2e 20 7b 48 31 31 33 30   pointer. {H1130
3caf0 34 7d 20 54 68 65 20 74 68 69 72 64 20 61 6e 64  4} The third and
3cb00 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65   fourth paramete
3cb10 72 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69  rs to this routi
3cb20 6e 65 0a 2a 2a 20 61 72 65 20 70 61 73 73 65 64  ne.** are passed
3cb30 20 64 69 72 65 63 74 6c 79 20 74 68 72 6f 75 67   directly throug
3cb40 68 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20  h to the second 
3cb50 61 6e 64 20 74 68 69 72 64 20 70 61 72 61 6d 65  and third parame
3cb60 74 65 72 73 20 6f 66 0a 2a 2a 20 74 68 65 20 78  ters of.** the x
3cb70 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68  FileControl meth
3cb80 6f 64 2e 20 20 7b 48 31 31 33 30 35 7d 20 54 68  od.  {H11305} Th
3cb90 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f  e return value o
3cba0 66 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72  f the xFileContr
3cbb0 6f 6c 0a 2a 2a 20 6d 65 74 68 6f 64 20 62 65 63  ol.** method bec
3cbc0 6f 6d 65 73 20 74 68 65 20 72 65 74 75 72 6e 20  omes the return 
3cbd0 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 72 6f  value of this ro
3cbe0 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  utine..**.** {H1
3cbf0 31 33 30 36 7d 20 49 66 20 74 68 65 20 73 65 63  1306} If the sec
3cc00 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 28 7a  ond parameter (z
3cc10 44 62 4e 61 6d 65 29 20 64 6f 65 73 20 6e 6f 74  DbName) does not
3cc20 20 6d 61 74 63 68 20 74 68 65 20 6e 61 6d 65 20   match the name 
3cc30 6f 66 20 61 6e 79 0a 2a 2a 20 6f 70 65 6e 20 64  of any.** open d
3cc40 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68  atabase file, th
3cc50 65 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  en SQLITE_ERROR 
3cc60 69 73 20 72 65 74 75 72 6e 65 64 2e 20 7b 48 31  is returned. {H1
3cc70 31 33 30 37 7d 20 54 68 69 73 20 65 72 72 6f 72  1307} This error
3cc80 0a 2a 2a 20 63 6f 64 65 20 69 73 20 6e 6f 74 20  .** code is not 
3cc90 72 65 6d 65 6d 62 65 72 65 64 20 61 6e 64 20 77  remembered and w
3cca0 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65 63 61 6c  ill not be recal
3ccb0 6c 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  led by [sqlite3_
3ccc0 65 72 72 63 6f 64 65 28 29 5d 0a 2a 2a 20 6f 72  errcode()].** or
3ccd0 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67   [sqlite3_errmsg
3cce0 28 29 5d 2e 20 7b 41 31 31 33 30 38 7d 20 54 68  ()]. {A11308} Th
3ccf0 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 78 46 69  e underlying xFi
3cd00 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64  leControl method
3cd10 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 6f 20 72   might.** also r
3cd20 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
3cd30 4f 52 2e 20 20 7b 41 31 31 33 30 39 7d 20 54 68  OR.  {A11309} Th
3cd40 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f  ere is no way to
3cd50 20 64 69 73 74 69 6e 67 75 69 73 68 20 62 65 74   distinguish bet
3cd60 77 65 65 6e 0a 2a 2a 20 61 6e 20 69 6e 63 6f 72  ween.** an incor
3cd70 72 65 63 74 20 7a 44 62 4e 61 6d 65 20 61 6e 64  rect zDbName and
3cd80 20 61 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52   an SQLITE_ERROR
3cd90 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 68 65   return from the
3cda0 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 78   underlying.** x
3cdb0 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68  FileControl meth
3cdc0 6f 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20  od. {END}.**.** 
3cdd0 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51 4c 49 54  See also: [SQLIT
3cde0 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54  E_FCNTL_LOCKSTAT
3cdf0 45 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  E].*/.SQLITE_API
3ce00 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 69 6c   int sqlite3_fil
3ce10 65 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65  e_control(sqlite
3ce20 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  3*, const char *
3ce30 7a 44 62 4e 61 6d 65 2c 20 69 6e 74 20 6f 70 2c  zDbName, int op,
3ce40 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20   void*);../*.** 
3ce50 43 41 50 49 33 52 45 46 3a 20 54 65 73 74 69 6e  CAPI3REF: Testin
3ce60 67 20 49 6e 74 65 72 66 61 63 65 20 7b 48 31 31  g Interface {H11
3ce70 34 30 30 7d 20 3c 53 33 30 38 30 30 3e 0a 2a 2a  400} <S30800>.**
3ce80 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
3ce90 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69  test_control() i
3cea0 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64  nterface is used
3ceb0 20 74 6f 20 72 65 61 64 20 6f 75 74 20 69 6e 74   to read out int
3cec0 65 72 6e 61 6c 0a 2a 2a 20 73 74 61 74 65 20 6f  ernal.** state o
3ced0 66 20 53 51 4c 69 74 65 20 61 6e 64 20 74 6f 20  f SQLite and to 
3cee0 69 6e 6a 65 63 74 20 66 61 75 6c 74 73 20 69 6e  inject faults in
3cef0 74 6f 20 53 51 4c 69 74 65 20 66 6f 72 20 74 65  to SQLite for te
3cf00 73 74 69 6e 67 0a 2a 2a 20 70 75 72 70 6f 73 65  sting.** purpose
3cf10 73 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61  s.  The first pa
3cf20 72 61 6d 65 74 65 72 20 69 73 20 61 6e 20 6f 70  rameter is an op
3cf30 65 72 61 74 69 6f 6e 20 63 6f 64 65 20 74 68 61  eration code tha
3cf40 74 20 64 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20  t determines.** 
3cf50 74 68 65 20 6e 75 6d 62 65 72 2c 20 6d 65 61 6e  the number, mean
3cf60 69 6e 67 2c 20 61 6e 64 20 6f 70 65 72 61 74 69  ing, and operati
3cf70 6f 6e 20 6f 66 20 61 6c 6c 20 73 75 62 73 65 71  on of all subseq
3cf80 75 65 6e 74 20 70 61 72 61 6d 65 74 65 72 73 2e  uent parameters.
3cf90 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65  .**.** This inte
3cfa0 72 66 61 63 65 20 69 73 20 6e 6f 74 20 66 6f 72  rface is not for
3cfb0 20 75 73 65 20 62 79 20 61 70 70 6c 69 63 61 74   use by applicat
3cfc0 69 6f 6e 73 2e 20 20 49 74 20 65 78 69 73 74 73  ions.  It exists
3cfd0 20 73 6f 6c 65 6c 79 0a 2a 2a 20 66 6f 72 20 76   solely.** for v
3cfe0 65 72 69 66 79 69 6e 67 20 74 68 65 20 63 6f 72  erifying the cor
3cff0 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f  rect operation o
3d000 66 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62  f the SQLite lib
3d010 72 61 72 79 2e 20 20 44 65 70 65 6e 64 69 6e 67  rary.  Depending
3d020 0a 2a 2a 20 6f 6e 20 68 6f 77 20 74 68 65 20 53  .** on how the S
3d030 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 69 73  QLite library is
3d040 20 63 6f 6d 70 69 6c 65 64 2c 20 74 68 69 73 20   compiled, this 
3d050 69 6e 74 65 72 66 61 63 65 20 6d 69 67 68 74 20  interface might 
3d060 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a 2a  not exist..**.**
3d070 20 54 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20   The details of 
3d080 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f  the operation co
3d090 64 65 73 2c 20 74 68 65 69 72 20 6d 65 61 6e 69  des, their meani
3d0a0 6e 67 73 2c 20 74 68 65 20 70 61 72 61 6d 65 74  ngs, the paramet
3d0b0 65 72 73 0a 2a 2a 20 74 68 65 79 20 74 61 6b 65  ers.** they take
3d0c0 2c 20 61 6e 64 20 77 68 61 74 20 74 68 65 79 20  , and what they 
3d0d0 64 6f 20 61 72 65 20 61 6c 6c 20 73 75 62 6a 65  do are all subje
3d0e0 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74  ct to change wit
3d0f0 68 6f 75 74 20 6e 6f 74 69 63 65 2e 0a 2a 2a 20  hout notice..** 
3d100 55 6e 6c 69 6b 65 20 6d 6f 73 74 20 6f 66 20 74  Unlike most of t
3d110 68 65 20 53 51 4c 69 74 65 20 41 50 49 2c 20 74  he SQLite API, t
3d120 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
3d130 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64 20 74  not guaranteed t
3d140 6f 0a 2a 2a 20 6f 70 65 72 61 74 65 20 63 6f 6e  o.** operate con
3d150 73 69 73 74 65 6e 74 6c 79 20 66 72 6f 6d 20 6f  sistently from o
3d160 6e 65 20 72 65 6c 65 61 73 65 20 74 6f 20 74 68  ne release to th
3d170 65 20 6e 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49 54  e next..*/.SQLIT
3d180 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3d190 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 69  3_test_control(i
3d1a0 6e 74 20 6f 70 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a  nt op, ...);../*
3d1b0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65  .** CAPI3REF: Te
3d1c0 73 74 69 6e 67 20 49 6e 74 65 72 66 61 63 65 20  sting Interface 
3d1d0 4f 70 65 72 61 74 69 6f 6e 20 43 6f 64 65 73 20  Operation Codes 
3d1e0 7b 48 31 31 34 31 30 7d 20 3c 48 31 31 34 30 30  {H11410} <H11400
3d1f0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f  >.**.** These co
3d200 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68 65 20  nstants are the 
3d210 76 61 6c 69 64 20 6f 70 65 72 61 74 69 6f 6e 20  valid operation 
3d220 63 6f 64 65 20 70 61 72 61 6d 65 74 65 72 73 20  code parameters 
3d230 75 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 66  used.** as the f
3d240 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
3d250 20 5b 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63   [sqlite3_test_c
3d260 6f 6e 74 72 6f 6c 28 29 5d 2e 0a 2a 2a 0a 2a 2a  ontrol()]..**.**
3d270 20 54 68 65 73 65 20 70 61 72 61 6d 65 74 65 72   These parameter
3d280 73 20 61 6e 64 20 74 68 65 69 72 20 6d 65 61 6e  s and their mean
3d290 69 6e 67 73 20 61 72 65 20 73 75 62 6a 65 63 74  ings are subject
3d2a0 20 74 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 77 69   to change.** wi
3d2b0 74 68 6f 75 74 20 6e 6f 74 69 63 65 2e 20 20 54  thout notice.  T
3d2c0 68 65 73 65 20 76 61 6c 75 65 73 20 61 72 65 20  hese values are 
3d2d0 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 70  for testing purp
3d2e0 6f 73 65 73 20 6f 6e 6c 79 2e 0a 2a 2a 20 41 70  oses only..** Ap
3d2f0 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c  plications shoul
3d300 64 20 6e 6f 74 20 75 73 65 20 61 6e 79 20 6f 66  d not use any of
3d310 20 74 68 65 73 65 20 70 61 72 61 6d 65 74 65 72   these parameter
3d320 73 20 6f 72 20 74 68 65 0a 2a 2a 20 5b 73 71 6c  s or the.** [sql
3d330 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f  ite3_test_contro
3d340 6c 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a  l()] interface..
3d350 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
3d360 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f  E_TESTCTRL_PRNG_
3d370 53 41 56 45 20 20 20 20 20 20 20 20 20 20 20 20  SAVE            
3d380 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53 51      5.#define SQ
3d390 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52  LITE_TESTCTRL_PR
3d3a0 4e 47 5f 52 45 53 54 4f 52 45 20 20 20 20 20 20  NG_RESTORE      
3d3b0 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65         6.#define
3d3c0 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c   SQLITE_TESTCTRL
3d3d0 5f 50 52 4e 47 5f 52 45 53 45 54 20 20 20 20 20  _PRNG_RESET     
3d3e0 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66            7.#def
3d3f0 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ine SQLITE_TESTC
3d400 54 52 4c 5f 42 49 54 56 45 43 5f 54 45 53 54 20  TRL_BITVEC_TEST 
3d410 20 20 20 20 20 20 20 20 20 20 20 20 20 38 0a 23               8.#
3d420 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
3d430 53 54 43 54 52 4c 5f 46 41 55 4c 54 5f 49 4e 53  STCTRL_FAULT_INS
3d440 54 41 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  TALL            
3d450 39 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  9.#define SQLITE
3d460 5f 54 45 53 54 43 54 52 4c 5f 42 45 4e 49 47 4e  _TESTCTRL_BENIGN
3d470 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 20 20 20  _MALLOC_HOOKS   
3d480 20 20 31 30 0a 23 64 65 66 69 6e 65 20 53 51 4c    10.#define SQL
3d490 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 45 4e  ITE_TESTCTRL_PEN
3d4a0 44 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20 20  DING_BYTE       
3d4b0 20 20 20 20 20 31 31 0a 23 64 65 66 69 6e 65 20       11.#define 
3d4c0 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
3d4d0 41 53 53 45 52 54 20 20 20 20 20 20 20 20 20 20  ASSERT          
3d4e0 20 20 20 20 20 20 20 20 31 32 0a 23 64 65 66 69          12.#defi
3d4f0 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ne SQLITE_TESTCT
3d500 52 4c 5f 41 4c 57 41 59 53 20 20 20 20 20 20 20  RL_ALWAYS       
3d510 20 20 20 20 20 20 20 20 20 20 20 31 33 0a 23 64             13.#d
3d520 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53  efine SQLITE_TES
3d530 54 43 54 52 4c 5f 52 45 53 45 52 56 45 20 20 20  TCTRL_RESERVE   
3d540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34                14
3d550 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
3d560 3a 20 53 51 4c 69 74 65 20 52 75 6e 74 69 6d 65  : SQLite Runtime
3d570 20 53 74 61 74 75 73 20 7b 48 31 37 32 30 30 7d   Status {H17200}
3d580 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 50   <S60200>.** EXP
3d590 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20  ERIMENTAL.**.** 
3d5a0 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69  This interface i
3d5b0 73 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 65  s used to retrie
3d5c0 76 65 20 72 75 6e 74 69 6d 65 20 73 74 61 74 75  ve runtime statu
3d5d0 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a  s information.**
3d5e0 20 61 62 6f 75 74 20 74 68 65 20 70 72 65 66 6f   about the prefo
3d5f0 72 6d 61 6e 63 65 20 6f 66 20 53 51 4c 69 74 65  rmance of SQLite
3d600 2c 20 61 6e 64 20 6f 70 74 69 6f 6e 61 6c 6c 79  , and optionally
3d610 20 74 6f 20 72 65 73 65 74 20 76 61 72 69 6f 75   to reset variou
3d620 73 0a 2a 2a 20 68 69 67 68 77 61 74 65 72 20 6d  s.** highwater m
3d630 61 72 6b 73 2e 20 20 54 68 65 20 66 69 72 73 74  arks.  The first
3d640 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20   argument is an 
3d650 69 6e 74 65 67 65 72 20 63 6f 64 65 20 66 6f 72  integer code for
3d660 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 63  .** the specific
3d670 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 6d 65   parameter to me
3d680 61 73 75 72 65 2e 20 20 52 65 63 6f 67 6e 69 7a  asure.  Recogniz
3d690 65 64 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73  ed integer codes
3d6a0 0a 2a 2a 20 61 72 65 20 6f 66 20 74 68 65 20 66  .** are of the f
3d6b0 6f 72 6d 20 5b 53 51 4c 49 54 45 5f 53 54 41 54  orm [SQLITE_STAT
3d6c0 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 20 7c  US_MEMORY_USED |
3d6d0 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 2e   SQLITE_STATUS_.
3d6e0 2e 2e 5d 2e 0a 2a 2a 20 54 68 65 20 63 75 72 72  ..]..** The curr
3d6f0 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65  ent value of the
3d700 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 72 65   parameter is re
3d710 74 75 72 6e 65 64 20 69 6e 74 6f 20 2a 70 43 75  turned into *pCu
3d720 72 72 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 68 69  rrent..** The hi
3d730 67 68 65 73 74 20 72 65 63 6f 72 64 65 64 20 76  ghest recorded v
3d740 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64  alue is returned
3d750 20 69 6e 20 2a 70 48 69 67 68 77 61 74 65 72 2e   in *pHighwater.
3d760 20 20 49 66 20 74 68 65 0a 2a 2a 20 72 65 73 65    If the.** rese
3d770 74 46 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74  tFlag is true, t
3d780 68 65 6e 20 74 68 65 20 68 69 67 68 65 73 74 20  hen the highest 
3d790 72 65 63 6f 72 64 20 76 61 6c 75 65 20 69 73 20  record value is 
3d7a0 72 65 73 65 74 20 61 66 74 65 72 0a 2a 2a 20 2a  reset after.** *
3d7b0 70 48 69 67 68 77 61 74 65 72 20 69 73 20 77 72  pHighwater is wr
3d7c0 69 74 74 65 6e 2e 20 53 6f 6d 65 20 70 61 72 61  itten. Some para
3d7d0 6d 65 74 65 72 73 20 64 6f 20 6e 6f 74 20 72 65  meters do not re
3d7e0 63 6f 72 64 20 74 68 65 20 68 69 67 68 65 73 74  cord the highest
3d7f0 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 46 6f 72 20  .** value.  For 
3d800 74 68 6f 73 65 20 70 61 72 61 6d 65 74 65 72 73  those parameters
3d810 0a 2a 2a 20 6e 6f 74 68 69 6e 67 20 69 73 20 77  .** nothing is w
3d820 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 48 69  ritten into *pHi
3d830 67 68 77 61 74 65 72 20 61 6e 64 20 74 68 65 20  ghwater and the 
3d840 72 65 73 65 74 46 6c 61 67 20 69 73 20 69 67 6e  resetFlag is ign
3d850 6f 72 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 20 70  ored..** Other p
3d860 61 72 61 6d 65 74 65 72 73 20 72 65 63 6f 72 64  arameters record
3d870 20 6f 6e 6c 79 20 74 68 65 20 68 69 67 68 77 61   only the highwa
3d880 74 65 72 20 6d 61 72 6b 20 61 6e 64 20 6e 6f 74  ter mark and not
3d890 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20   the current.** 
3d8a0 76 61 6c 75 65 2e 20 20 46 6f 72 20 74 68 65 73  value.  For thes
3d8b0 65 20 6c 61 74 74 65 72 20 70 61 72 61 6d 65 74  e latter paramet
3d8c0 65 72 73 20 6e 6f 74 68 69 6e 67 20 69 73 20 77  ers nothing is w
3d8d0 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 43 75  ritten into *pCu
3d8e0 72 72 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  rrent..**.** Thi
3d8f0 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
3d900 73 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  s SQLITE_OK on s
3d910 75 63 63 65 73 73 20 61 6e 64 20 61 20 6e 6f 6e  uccess and a non
3d920 2d 7a 65 72 6f 0a 2a 2a 20 5b 65 72 72 6f 72 20  -zero.** [error 
3d930 63 6f 64 65 5d 20 6f 6e 20 66 61 69 6c 75 72 65  code] on failure
3d940 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
3d950 74 69 6e 65 20 69 73 20 74 68 72 65 61 64 73 61  tine is threadsa
3d960 66 65 20 62 75 74 20 69 73 20 6e 6f 74 20 61 74  fe but is not at
3d970 6f 6d 69 63 2e 20 20 54 68 69 73 20 72 6f 75 74  omic.  This rout
3d980 69 6e 65 20 63 61 6e 20 62 65 0a 2a 2a 20 63 61  ine can be.** ca
3d990 6c 6c 65 64 20 77 68 69 6c 65 20 6f 74 68 65 72  lled while other
3d9a0 20 74 68 72 65 61 64 73 20 61 72 65 20 72 75 6e   threads are run
3d9b0 6e 69 6e 67 20 74 68 65 20 73 61 6d 65 20 6f 72  ning the same or
3d9c0 20 64 69 66 66 65 72 65 6e 74 20 53 51 4c 69 74   different SQLit
3d9d0 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 2e  e.** interfaces.
3d9e0 20 20 48 6f 77 65 76 65 72 20 74 68 65 20 76 61    However the va
3d9f0 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 69 6e  lues returned in
3da00 20 2a 70 43 75 72 72 65 6e 74 20 61 6e 64 0a 2a   *pCurrent and.*
3da10 2a 20 2a 70 48 69 67 68 77 61 74 65 72 20 72 65  * *pHighwater re
3da20 66 6c 65 63 74 20 74 68 65 20 73 74 61 74 75 73  flect the status
3da30 20 6f 66 20 53 51 4c 69 74 65 20 61 74 20 64 69   of SQLite at di
3da40 66 66 65 72 65 6e 74 20 70 6f 69 6e 74 73 20 69  fferent points i
3da50 6e 20 74 69 6d 65 0a 2a 2a 20 61 6e 64 20 69 74  n time.** and it
3da60 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 68 61   is possible tha
3da70 74 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64  t another thread
3da80 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74 68   might change th
3da90 65 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69  e parameter.** i
3daa0 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 69  n between the ti
3dab0 6d 65 73 20 77 68 65 6e 20 2a 70 43 75 72 72 65  mes when *pCurre
3dac0 6e 74 20 61 6e 64 20 2a 70 48 69 67 68 77 61 74  nt and *pHighwat
3dad0 65 72 20 61 72 65 20 77 72 69 74 74 65 6e 2e 0a  er are written..
3dae0 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
3daf0 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74  [sqlite3_db_stat
3db00 75 73 28 29 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  us()].*/.SQLITE_
3db10 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52  API SQLITE_EXPER
3db20 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69  IMENTAL int sqli
3db30 74 65 33 5f 73 74 61 74 75 73 28 69 6e 74 20 6f  te3_status(int o
3db40 70 2c 20 69 6e 74 20 2a 70 43 75 72 72 65 6e 74  p, int *pCurrent
3db50 2c 20 69 6e 74 20 2a 70 48 69 67 68 77 61 74 65  , int *pHighwate
3db60 72 2c 20 69 6e 74 20 72 65 73 65 74 46 6c 61 67  r, int resetFlag
3db70 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  );.../*.** CAPI3
3db80 52 45 46 3a 20 53 74 61 74 75 73 20 50 61 72 61  REF: Status Para
3db90 6d 65 74 65 72 73 20 7b 48 31 37 32 35 30 7d 20  meters {H17250} 
3dba0 3c 48 31 37 32 30 30 3e 0a 2a 2a 20 45 58 50 45  <H17200>.** EXPE
3dbb0 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54  RIMENTAL.**.** T
3dbc0 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e  hese integer con
3dbd0 73 74 61 6e 74 73 20 64 65 73 69 67 6e 61 74 65  stants designate
3dbe0 20 76 61 72 69 6f 75 73 20 72 75 6e 2d 74 69 6d   various run-tim
3dbf0 65 20 73 74 61 74 75 73 20 70 61 72 61 6d 65 74  e status paramet
3dc00 65 72 73 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20  ers.** that can 
3dc10 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b  be returned by [
3dc20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29  sqlite3_status()
3dc30 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a  ]..**.** <dl>.**
3dc40 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54   <dt>SQLITE_STAT
3dc50 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 3c 2f  US_MEMORY_USED</
3dc60 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20  dt>.** <dd>This 
3dc70 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65  parameter is the
3dc80 20 63 75 72 72 65 6e 74 20 61 6d 6f 75 6e 74 20   current amount 
3dc90 6f 66 20 6d 65 6d 6f 72 79 20 63 68 65 63 6b 65  of memory checke
3dca0 64 20 6f 75 74 0a 2a 2a 20 75 73 69 6e 67 20 5b  d out.** using [
3dcb0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
3dcc0 5d 2c 20 65 69 74 68 65 72 20 64 69 72 65 63 74  ], either direct
3dcd0 6c 79 20 6f 72 20 69 6e 64 69 72 65 63 74 6c 79  ly or indirectly
3dce0 2e 20 20 54 68 65 0a 2a 2a 20 66 69 67 75 72 65  .  The.** figure
3dcf0 20 69 6e 63 6c 75 64 65 73 20 63 61 6c 6c 73 20   includes calls 
3dd00 6d 61 64 65 20 74 6f 20 5b 73 71 6c 69 74 65 33  made to [sqlite3
3dd10 5f 6d 61 6c 6c 6f 63 28 29 5d 20 62 79 20 74 68  _malloc()] by th
3dd20 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a 2a 2a  e application.**
3dd30 20 61 6e 64 20 69 6e 74 65 72 6e 61 6c 20 6d 65   and internal me
3dd40 6d 6f 72 79 20 75 73 61 67 65 20 62 79 20 74 68  mory usage by th
3dd50 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  e SQLite library
3dd60 2e 20 20 53 63 72 61 74 63 68 20 6d 65 6d 6f 72  .  Scratch memor
3dd70 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 6c 65 64 20  y.** controlled 
3dd80 62 79 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  by [SQLITE_CONFI
3dd90 47 5f 53 43 52 41 54 43 48 5d 20 61 6e 64 20 61  G_SCRATCH] and a
3dda0 75 78 69 6c 69 61 72 79 20 70 61 67 65 2d 63 61  uxiliary page-ca
3ddb0 63 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 6f  che.** memory co
3ddc0 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b 53 51 4c  ntrolled by [SQL
3ddd0 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43  ITE_CONFIG_PAGEC
3dde0 41 43 48 45 5d 20 69 73 20 6e 6f 74 20 69 6e 63  ACHE] is not inc
3ddf0 6c 75 64 65 64 20 69 6e 0a 2a 2a 20 74 68 69 73  luded in.** this
3de00 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65   parameter.  The
3de10 20 61 6d 6f 75 6e 74 20 72 65 74 75 72 6e 65 64   amount returned
3de20 20 69 73 20 74 68 65 20 73 75 6d 20 6f 66 20 74   is the sum of t
3de30 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  he allocation.**
3de40 20 73 69 7a 65 73 20 61 73 20 72 65 70 6f 72 74   sizes as report
3de50 65 64 20 62 79 20 74 68 65 20 78 53 69 7a 65 20  ed by the xSize 
3de60 6d 65 74 68 6f 64 20 69 6e 20 5b 73 71 6c 69 74  method in [sqlit
3de70 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 2e  e3_mem_methods].
3de80 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
3de90 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41  SQLITE_STATUS_MA
3dea0 4c 4c 4f 43 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a  LLOC_SIZE</dt>.*
3deb0 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d  * <dd>This param
3dec0 65 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 65  eter records the
3ded0 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20   largest memory 
3dee0 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65  allocation reque
3def0 73 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20  st.** handed to 
3df00 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
3df10 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72  )] or [sqlite3_r
3df20 65 61 6c 6c 6f 63 28 29 5d 20 28 6f 72 20 74 68  ealloc()] (or th
3df30 65 69 72 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20  eir.** internal 
3df40 65 71 75 69 76 61 6c 65 6e 74 73 29 2e 20 20 4f  equivalents).  O
3df50 6e 6c 79 20 74 68 65 20 76 61 6c 75 65 20 72 65  nly the value re
3df60 74 75 72 6e 65 64 20 69 6e 20 74 68 65 0a 2a 2a  turned in the.**
3df70 20 2a 70 48 69 67 68 77 61 74 65 72 20 70 61 72   *pHighwater par
3df80 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74  ameter to [sqlit
3df90 65 33 5f 73 74 61 74 75 73 28 29 5d 20 69 73 20  e3_status()] is 
3dfa0 6f 66 20 69 6e 74 65 72 65 73 74 2e 20 20 0a 2a  of interest.  .*
3dfb0 2a 20 54 68 65 20 76 61 6c 75 65 20 77 72 69 74  * The value writ
3dfc0 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a 70 43  ten into the *pC
3dfd0 75 72 72 65 6e 74 20 70 61 72 61 6d 65 74 65 72  urrent parameter
3dfe0 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f   is undefined.</
3dff0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
3e000 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45  LITE_STATUS_PAGE
3e010 43 41 43 48 45 5f 55 53 45 44 3c 2f 64 74 3e 0a  CACHE_USED</dt>.
3e020 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61  ** <dd>This para
3e030 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68  meter returns th
3e040 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
3e050 73 20 75 73 65 64 20 6f 75 74 20 6f 66 20 74 68  s used out of th
3e060 65 0a 2a 2a 20 5b 70 61 67 65 63 61 63 68 65 20  e.** [pagecache 
3e070 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
3e080 5d 20 74 68 61 74 20 77 61 73 20 63 6f 6e 66 69  ] that was confi
3e090 67 75 72 65 64 20 75 73 69 6e 67 20 0a 2a 2a 20  gured using .** 
3e0a0 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50  [SQLITE_CONFIG_P
3e0b0 41 47 45 43 41 43 48 45 5d 2e 20 20 54 68 65 0a  AGECACHE].  The.
3e0c0 2a 2a 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  ** value returne
3e0d0 64 20 69 73 20 69 6e 20 70 61 67 65 73 2c 20 6e  d is in pages, n
3e0e0 6f 74 20 69 6e 20 62 79 74 65 73 2e 3c 2f 64 64  ot in bytes.</dd
3e0f0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
3e100 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41  TE_STATUS_PAGECA
3e110 43 48 45 5f 4f 56 45 52 46 4c 4f 57 3c 2f 64 74  CHE_OVERFLOW</dt
3e120 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61  >.** <dd>This pa
3e130 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20  rameter returns 
3e140 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
3e150 74 65 73 20 6f 66 20 70 61 67 65 20 63 61 63 68  tes of page cach
3e160 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  e.** allocation 
3e170 77 68 69 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20  which could not 
3e180 62 65 20 73 74 61 74 69 73 66 69 65 64 20 62 79  be statisfied by
3e190 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e   the [SQLITE_CON
3e1a0 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 0a 2a  FIG_PAGECACHE].*
3e1b0 2a 20 62 75 66 66 65 72 20 61 6e 64 20 77 68 65  * buffer and whe
3e1c0 72 65 20 66 6f 72 63 65 64 20 74 6f 20 6f 76 65  re forced to ove
3e1d0 72 66 6c 6f 77 20 74 6f 20 5b 73 71 6c 69 74 65  rflow to [sqlite
3e1e0 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 68  3_malloc()].  Th
3e1f0 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 76 61  e.** returned va
3e200 6c 75 65 20 69 6e 63 6c 75 64 65 73 20 61 6c 6c  lue includes all
3e210 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 76  ocations that ov
3e220 65 72 66 6c 6f 77 65 64 20 62 65 63 61 75 73 65  erflowed because
3e230 20 74 68 65 79 0a 2a 2a 20 77 68 65 72 65 20 74   they.** where t
3e240 6f 6f 20 6c 61 72 67 65 20 28 74 68 65 79 20 77  oo large (they w
3e250 65 72 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20  ere larger than 
3e260 74 68 65 20 22 73 7a 22 20 70 61 72 61 6d 65 74  the "sz" paramet
3e270 65 72 20 74 6f 0a 2a 2a 20 5b 53 51 4c 49 54 45  er to.** [SQLITE
3e280 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
3e290 45 5d 29 20 61 6e 64 20 61 6c 6c 6f 63 61 74 69  E]) and allocati
3e2a0 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 66 6c 6f  ons that overflo
3e2b0 77 65 64 20 62 65 63 61 75 73 65 0a 2a 2a 20 6e  wed because.** n
3e2c0 6f 20 73 70 61 63 65 20 77 61 73 20 6c 65 66 74  o space was left
3e2d0 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61 63   in the page cac
3e2e0 68 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  he.</dd>.**.** <
3e2f0 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dt>SQLITE_STATUS
3e300 5f 50 41 47 45 43 41 43 48 45 5f 53 49 5a 45 3c  _PAGECACHE_SIZE<
3e310 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
3e320 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72   parameter recor
3e330 64 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 6d  ds the largest m
3e340 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
3e350 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e 64   request.** hand
3e360 65 64 20 74 6f 20 5b 70 61 67 65 63 61 63 68 65  ed to [pagecache
3e370 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
3e380 72 5d 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 61  r].  Only the va
3e390 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 20  lue returned in 
3e3a0 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74  the.** *pHighwat
3e3b0 65 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  er parameter to 
3e3c0 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28  [sqlite3_status(
3e3d0 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65 73  )] is of interes
3e3e0 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75  t.  .** The valu
3e3f0 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  e written into t
3e400 68 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61 72  he *pCurrent par
3e410 61 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66 69  ameter is undefi
3e420 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ned.</dd>.**.** 
3e430 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55  <dt>SQLITE_STATU
3e440 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 3c 2f  S_SCRATCH_USED</
3e450 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20  dt>.** <dd>This 
3e460 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e  parameter return
3e470 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
3e480 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 75 73 65 64  allocations used
3e490 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 5b   out of the.** [
3e4a0 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 61  scratch memory a
3e4b0 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f 6e 66 69 67  llocator] config
3e4c0 75 72 65 64 20 75 73 69 6e 67 0a 2a 2a 20 5b 53  ured using.** [S
3e4d0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52  QLITE_CONFIG_SCR
3e4e0 41 54 43 48 5d 2e 20 20 54 68 65 20 76 61 6c 75  ATCH].  The valu
3e4f0 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 69 6e  e returned is in
3e500 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2c 20 6e 6f   allocations, no
3e510 74 0a 2a 2a 20 69 6e 20 62 79 74 65 73 2e 20 20  t.** in bytes.  
3e520 53 69 6e 63 65 20 61 20 73 69 6e 67 6c 65 20 74  Since a single t
3e530 68 72 65 61 64 20 6d 61 79 20 6f 6e 6c 79 20 68  hread may only h
3e540 61 76 65 20 6f 6e 65 20 73 63 72 61 74 63 68 20  ave one scratch 
3e550 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 6f 75  allocation.** ou
3e560 74 73 74 61 6e 64 69 6e 67 20 61 74 20 74 69 6d  tstanding at tim
3e570 65 2c 20 74 68 69 73 20 70 61 72 61 6d 65 74 65  e, this paramete
3e580 72 20 61 6c 73 6f 20 72 65 70 6f 72 74 73 20 74  r also reports t
3e590 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 72  he number of thr
3e5a0 65 61 64 73 0a 2a 2a 20 75 73 69 6e 67 20 73 63  eads.** using sc
3e5b0 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 61 74 20  ratch memory at 
3e5c0 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 3c 2f  the same time.</
3e5d0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
3e5e0 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41  LITE_STATUS_SCRA
3e5f0 54 43 48 5f 4f 56 45 52 46 4c 4f 57 3c 2f 64 74  TCH_OVERFLOW</dt
3e600 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61  >.** <dd>This pa
3e610 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20  rameter returns 
3e620 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
3e630 74 65 73 20 6f 66 20 73 63 72 61 74 63 68 20 6d  tes of scratch m
3e640 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74  emory.** allocat
3e650 69 6f 6e 20 77 68 69 63 68 20 63 6f 75 6c 64 20  ion which could 
3e660 6e 6f 74 20 62 65 20 73 74 61 74 69 73 66 69 65  not be statisfie
3e670 64 20 62 79 20 74 68 65 20 5b 53 51 4c 49 54 45  d by the [SQLITE
3e680 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d  _CONFIG_SCRATCH]
3e690 0a 2a 2a 20 62 75 66 66 65 72 20 61 6e 64 20 77  .** buffer and w
3e6a0 68 65 72 65 20 66 6f 72 63 65 64 20 74 6f 20 6f  here forced to o
3e6b0 76 65 72 66 6c 6f 77 20 74 6f 20 5b 73 71 6c 69  verflow to [sqli
3e6c0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20  te3_malloc()].  
3e6d0 54 68 65 20 76 61 6c 75 65 73 0a 2a 2a 20 72 65  The values.** re
3e6e0 74 75 72 6e 65 64 20 69 6e 63 6c 75 64 65 20 6f  turned include o
3e6f0 76 65 72 66 6c 6f 77 73 20 62 65 63 61 75 73 65  verflows because
3e700 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 61   the requested a
3e710 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20 74 6f  llocation was to
3e720 6f 0a 2a 2a 20 6c 61 72 67 65 72 20 28 74 68 61  o.** larger (tha
3e730 74 20 69 73 2c 20 62 65 63 61 75 73 65 20 74 68  t is, because th
3e740 65 20 72 65 71 75 65 73 74 65 64 20 61 6c 6c 6f  e requested allo
3e750 63 61 74 69 6f 6e 20 77 61 73 20 6c 61 72 67 65  cation was large
3e760 72 20 74 68 61 6e 20 74 68 65 0a 2a 2a 20 22 73  r than the.** "s
3e770 7a 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  z" parameter to 
3e780 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53  [SQLITE_CONFIG_S
3e790 43 52 41 54 43 48 5d 29 20 61 6e 64 20 62 65 63  CRATCH]) and bec
3e7a0 61 75 73 65 20 6e 6f 20 73 63 72 61 74 63 68 20  ause no scratch 
3e7b0 62 75 66 66 65 72 0a 2a 2a 20 73 6c 6f 74 73 20  buffer.** slots 
3e7c0 77 65 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a  were available..
3e7d0 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  ** </dd>.**.** <
3e7e0 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dt>SQLITE_STATUS
3e7f0 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 3c 2f 64  _SCRATCH_SIZE</d
3e800 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70  t>.** <dd>This p
3e810 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72 64 73  arameter records
3e820 20 74 68 65 20 6c 61 72 67 65 73 74 20 6d 65 6d   the largest mem
3e830 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72  ory allocation r
3e840 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e 64 65 64  equest.** handed
3e850 20 74 6f 20 5b 73 63 72 61 74 63 68 20 6d 65 6d   to [scratch mem
3e860 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20  ory allocator]. 
3e870 20 4f 6e 6c 79 20 74 68 65 20 76 61 6c 75 65 20   Only the value 
3e880 72 65 74 75 72 6e 65 64 20 69 6e 20 74 68 65 0a  returned in the.
3e890 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72 20 70  ** *pHighwater p
3e8a0 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c  arameter to [sql
3e8b0 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d 20 69  ite3_status()] i
3e8c0 73 20 6f 66 20 69 6e 74 65 72 65 73 74 2e 20 20  s of interest.  
3e8d0 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 77 72  .** The value wr
3e8e0 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a  itten into the *
3e8f0 70 43 75 72 72 65 6e 74 20 70 61 72 61 6d 65 74  pCurrent paramet
3e900 65 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  er is undefined.
3e910 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
3e920 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41  SQLITE_STATUS_PA
3e930 52 53 45 52 5f 53 54 41 43 4b 3c 2f 64 74 3e 0a  RSER_STACK</dt>.
3e940 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61  ** <dd>This para
3e950 6d 65 74 65 72 20 72 65 63 6f 72 64 73 20 74 68  meter records th
3e960 65 20 64 65 65 70 65 73 74 20 70 61 72 73 65 72  e deepest parser
3e970 20 73 74 61 63 6b 2e 20 20 49 74 20 69 73 20 6f   stack.  It is o
3e980 6e 6c 79 0a 2a 2a 20 6d 65 61 6e 69 6e 67 66 75  nly.** meaningfu
3e990 6c 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 63  l if SQLite is c
3e9a0 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 5b 59 59  ompiled with [YY
3e9b0 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50  TRACKMAXSTACKDEP
3e9c0 54 48 5d 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64  TH].</dd>.** </d
3e9d0 6c 3e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 73 74 61  l>.**.** New sta
3e9e0 74 75 73 20 70 61 72 61 6d 65 74 65 72 73 20 6d  tus parameters m
3e9f0 61 79 20 62 65 20 61 64 64 65 64 20 66 72 6f 6d  ay be added from
3ea00 20 74 69 6d 65 20 74 6f 20 74 69 6d 65 2e 0a 2a   time to time..*
3ea10 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
3ea20 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55  _STATUS_MEMORY_U
3ea30 53 45 44 20 20 20 20 20 20 20 20 20 20 30 0a 23  SED          0.#
3ea40 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54  define SQLITE_ST
3ea50 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55  ATUS_PAGECACHE_U
3ea60 53 45 44 20 20 20 20 20 20 20 31 0a 23 64 65 66  SED       1.#def
3ea70 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55  ine SQLITE_STATU
3ea80 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56 45 52  S_PAGECACHE_OVER
3ea90 46 4c 4f 57 20 20 20 32 0a 23 64 65 66 69 6e 65  FLOW   2.#define
3eaa0 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53   SQLITE_STATUS_S
3eab0 43 52 41 54 43 48 5f 55 53 45 44 20 20 20 20 20  CRATCH_USED     
3eac0 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51      3.#define SQ
3ead0 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41  LITE_STATUS_SCRA
3eae0 54 43 48 5f 4f 56 45 52 46 4c 4f 57 20 20 20 20  TCH_OVERFLOW    
3eaf0 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   4.#define SQLIT
3eb00 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f  E_STATUS_MALLOC_
3eb10 53 49 5a 45 20 20 20 20 20 20 20 20 20 20 35 0a  SIZE          5.
3eb20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
3eb30 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53 54 41  TATUS_PARSER_STA
3eb40 43 4b 20 20 20 20 20 20 20 20 20 36 0a 23 64 65  CK         6.#de
3eb50 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54  fine SQLITE_STAT
3eb60 55 53 5f 50 41 47 45 43 41 43 48 45 5f 53 49 5a  US_PAGECACHE_SIZ
3eb70 45 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e  E       7.#defin
3eb80 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  e SQLITE_STATUS_
3eb90 53 43 52 41 54 43 48 5f 53 49 5a 45 20 20 20 20  SCRATCH_SIZE    
3eba0 20 20 20 20 20 38 0a 0a 2f 2a 0a 2a 2a 20 43 41       8../*.** CA
3ebb0 50 49 33 52 45 46 3a 20 44 61 74 61 62 61 73 65  PI3REF: Database
3ebc0 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 53 74 61 74   Connection Stat
3ebd0 75 73 20 7b 48 31 37 35 30 30 7d 20 3c 53 36 30  us {H17500} <S60
3ebe0 32 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45  200>.** EXPERIME
3ebf0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  NTAL.**.** This 
3ec00 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65  interface is use
3ec10 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 72 75  d to retrieve ru
3ec20 6e 74 69 6d 65 20 73 74 61 74 75 73 20 69 6e 66  ntime status inf
3ec30 6f 72 6d 61 74 69 6f 6e 20 0a 2a 2a 20 61 62 6f  ormation .** abo
3ec40 75 74 20 61 20 73 69 6e 67 6c 65 20 5b 64 61 74  ut a single [dat
3ec50 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
3ec60 5d 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72  ].  The first ar
3ec70 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a  gument is the.**
3ec80 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
3ec90 74 69 6f 6e 20 6f 62 6a 65 63 74 20 74 6f 20 62  tion object to b
3eca0 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e 20  e interrogated. 
3ecb0 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   The second argu
3ecc0 6d 65 6e 74 0a 2a 2a 20 69 73 20 74 68 65 20 70  ment.** is the p
3ecd0 61 72 61 6d 65 74 65 72 20 74 6f 20 69 6e 74 65  arameter to inte
3ece0 72 72 6f 67 61 74 65 2e 20 20 43 75 72 72 65 6e  rrogate.  Curren
3ecf0 74 6c 79 2c 20 74 68 65 20 6f 6e 6c 79 20 61 6c  tly, the only al
3ed00 6c 6f 77 65 64 20 76 61 6c 75 65 0a 2a 2a 20 66  lowed value.** f
3ed10 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61  or the second pa
3ed20 72 61 6d 65 74 65 72 20 69 73 20 5b 53 51 4c 49  rameter is [SQLI
3ed30 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b  TE_DBSTATUS_LOOK
3ed40 41 53 49 44 45 5f 55 53 45 44 5d 2e 0a 2a 2a 20  ASIDE_USED]..** 
3ed50 41 64 64 69 74 69 6f 6e 61 6c 20 6f 70 74 69 6f  Additional optio
3ed60 6e 73 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 61  ns will likely a
3ed70 70 70 65 61 72 20 69 6e 20 66 75 74 75 72 65 20  ppear in future 
3ed80 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69  releases of SQLi
3ed90 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75  te..**.** The cu
3eda0 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74  rrent value of t
3edb0 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 72  he requested par
3edc0 61 6d 65 74 65 72 20 69 73 20 77 72 69 74 74 65  ameter is writte
3edd0 6e 20 69 6e 74 6f 20 2a 70 43 75 72 0a 2a 2a 20  n into *pCur.** 
3ede0 61 6e 64 20 74 68 65 20 68 69 67 68 65 73 74 20  and the highest 
3edf0 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 76 61  instantaneous va
3ee00 6c 75 65 20 69 73 20 77 72 69 74 74 65 6e 20 69  lue is written i
3ee10 6e 74 6f 20 2a 70 48 69 77 74 72 2e 20 20 49 66  nto *pHiwtr.  If
3ee20 0a 2a 2a 20 74 68 65 20 72 65 73 65 74 46 6c 67  .** the resetFlg
3ee30 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74   is true, then t
3ee40 68 65 20 68 69 67 68 65 73 74 20 69 6e 73 74 61  he highest insta
3ee50 6e 74 61 6e 65 6f 75 73 20 76 61 6c 75 65 20 69  ntaneous value i
3ee60 73 0a 2a 2a 20 72 65 73 65 74 20 62 61 63 6b 20  s.** reset back 
3ee70 64 6f 77 6e 20 74 6f 20 74 68 65 20 63 75 72 72  down to the curr
3ee80 65 6e 74 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a  ent value..**.**
3ee90 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69   See also: [sqli
3eea0 74 65 33 5f 73 74 61 74 75 73 28 29 5d 20 61 6e  te3_status()] an
3eeb0 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f  d [sqlite3_stmt_
3eec0 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2f 0a 53 51  status()]..*/.SQ
3eed0 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f  LITE_API SQLITE_
3eee0 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74  EXPERIMENTAL int
3eef0 20 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74   sqlite3_db_stat
3ef00 75 73 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  us(sqlite3*, int
3ef10 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72 2c 20   op, int *pCur, 
3ef20 69 6e 74 20 2a 70 48 69 77 74 72 2c 20 69 6e 74  int *pHiwtr, int
3ef30 20 72 65 73 65 74 46 6c 67 29 3b 0a 0a 2f 2a 0a   resetFlg);../*.
3ef40 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 74 61  ** CAPI3REF: Sta
3ef50 74 75 73 20 50 61 72 61 6d 65 74 65 72 73 20 66  tus Parameters f
3ef60 6f 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  or database conn
3ef70 65 63 74 69 6f 6e 73 20 7b 48 31 37 35 32 30 7d  ections {H17520}
3ef80 20 3c 48 31 37 35 30 30 3e 0a 2a 2a 20 45 58 50   <H17500>.** EXP
3ef90 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20  ERIMENTAL.**.** 
3efa0 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20  These constants 
3efb0 61 72 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c  are the availabl
3efc0 65 20 69 6e 74 65 67 65 72 20 22 76 65 72 62 73  e integer "verbs
3efd0 22 20 74 68 61 74 20 63 61 6e 20 62 65 20 70 61  " that can be pa
3efe0 73 73 65 64 20 61 73 0a 2a 2a 20 74 68 65 20 73  ssed as.** the s
3eff0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
3f000 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 64  o the [sqlite3_d
3f010 62 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74 65  b_status()] inte
3f020 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77  rface..**.** New
3f030 20 76 65 72 62 73 20 6d 61 79 20 62 65 20 61 64   verbs may be ad
3f040 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65  ded in future re
3f050 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65  leases of SQLite
3f060 2e 20 45 78 69 73 74 69 6e 67 20 76 65 72 62 73  . Existing verbs
3f070 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 64 69 73  .** might be dis
3f080 63 6f 6e 74 69 6e 75 65 64 2e 20 41 70 70 6c 69  continued. Appli
3f090 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 63  cations should c
3f0a0 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20  heck the return 
3f0b0 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20 5b 73 71  code from.** [sq
3f0c0 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28  lite3_db_status(
3f0d0 29 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  )] to make sure 
3f0e0 74 68 61 74 20 74 68 65 20 63 61 6c 6c 20 77 6f  that the call wo
3f0f0 72 6b 65 64 2e 0a 2a 2a 20 54 68 65 20 5b 73 71  rked..** The [sq
3f100 6c 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28  lite3_db_status(
3f110 29 5d 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c  )] interface wil
3f120 6c 20 72 65 74 75 72 6e 20 61 20 6e 6f 6e 2d 7a  l return a non-z
3f130 65 72 6f 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a  ero error code.*
3f140 2a 20 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e  * if a discontin
3f150 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74  ued or unsupport
3f160 65 64 20 76 65 72 62 20 69 73 20 69 6e 76 6f 6b  ed verb is invok
3f170 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a  ed..**.** <dl>.*
3f180 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 44 42 53  * <dt>SQLITE_DBS
3f190 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f  TATUS_LOOKASIDE_
3f1a0 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  USED</dt>.** <dd
3f1b0 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20  >This parameter 
3f1c0 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
3f1d0 65 72 20 6f 66 20 6c 6f 6f 6b 61 73 69 64 65 20  er of lookaside 
3f1e0 6d 65 6d 6f 72 79 20 73 6c 6f 74 73 20 63 75 72  memory slots cur
3f1f0 72 65 6e 74 6c 79 0a 2a 2a 20 63 68 65 63 6b 65  rently.** checke
3f200 64 20 6f 75 74 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c  d out.</dd>.** <
3f210 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  /dl>.*/.#define 
3f220 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f  SQLITE_DBSTATUS_
3f230 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 20 20  LOOKASIDE_USED  
3f240 20 20 20 30 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50     0.../*.** CAP
3f250 49 33 52 45 46 3a 20 50 72 65 70 61 72 65 64 20  I3REF: Prepared 
3f260 53 74 61 74 65 6d 65 6e 74 20 53 74 61 74 75 73  Statement Status
3f270 20 7b 48 31 37 35 35 30 7d 20 3c 53 36 30 32 30   {H17550} <S6020
3f280 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
3f290 41 4c 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 70 72  AL.**.** Each pr
3f2a0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
3f2b0 20 6d 61 69 6e 74 61 69 6e 73 20 76 61 72 69 6f   maintains vario
3f2c0 75 73 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 53 54  us.** [SQLITE_ST
3f2d0 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 20 7c 20  MTSTATUS_SORT | 
3f2e0 63 6f 75 6e 74 65 72 73 5d 20 74 68 61 74 20 6d  counters] that m
3f2f0 65 61 73 75 72 65 20 74 68 65 20 6e 75 6d 62 65  easure the numbe
3f300 72 0a 2a 2a 20 6f 66 20 74 69 6d 65 73 20 69 74  r.** of times it
3f310 20 68 61 73 20 70 65 72 66 6f 72 6d 65 64 20 73   has performed s
3f320 70 65 63 69 66 69 63 20 6f 70 65 72 61 74 69 6f  pecific operatio
3f330 6e 73 2e 20 20 54 68 65 73 65 20 63 6f 75 6e 74  ns.  These count
3f340 65 72 73 20 63 61 6e 0a 2a 2a 20 62 65 20 75 73  ers can.** be us
3f350 65 64 20 74 6f 20 6d 6f 6e 69 74 6f 72 20 74 68  ed to monitor th
3f360 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 63 68  e performance ch
3f370 61 72 61 63 74 65 72 69 73 74 69 63 73 20 6f 66  aracteristics of
3f380 20 74 68 65 20 70 72 65 70 61 72 65 64 0a 2a 2a   the prepared.**
3f390 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 46 6f   statements.  Fo
3f3a0 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68  r example, if th
3f3b0 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c  e number of tabl
3f3c0 65 20 73 74 65 70 73 20 67 72 65 61 74 6c 79 20  e steps greatly 
3f3d0 65 78 63 65 65 64 73 0a 2a 2a 20 74 68 65 20 6e  exceeds.** the n
3f3e0 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 20 73  umber of table s
3f3f0 65 61 72 63 68 65 73 20 6f 72 20 72 65 73 75 6c  earches or resul
3f400 74 20 72 6f 77 73 2c 20 74 68 61 74 20 77 6f 75  t rows, that wou
3f410 6c 64 20 74 65 6e 64 20 74 6f 20 69 6e 64 69 63  ld tend to indic
3f420 61 74 65 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  ate.** that the 
3f430 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
3f440 6e 74 20 69 73 20 75 73 69 6e 67 20 61 20 66 75  nt is using a fu
3f450 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e 20 72 61  ll table scan ra
3f460 74 68 65 72 20 74 68 61 6e 0a 2a 2a 20 61 6e 20  ther than.** an 
3f470 69 6e 64 65 78 2e 20 20 0a 2a 2a 0a 2a 2a 20 54  index.  .**.** T
3f480 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73  his interface is
3f490 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 65 76   used to retriev
3f4a0 65 20 61 6e 64 20 72 65 73 65 74 20 63 6f 75 6e  e and reset coun
3f4b0 74 65 72 20 76 61 6c 75 65 73 20 66 72 6f 6d 0a  ter values from.
3f4c0 2a 2a 20 61 20 5b 70 72 65 70 61 72 65 64 20 73  ** a [prepared s
3f4d0 74 61 74 65 6d 65 6e 74 5d 2e 20 20 54 68 65 20  tatement].  The 
3f4e0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69  first argument i
3f4f0 73 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73  s the prepared s
3f500 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 6f 62 6a 65  tatement.** obje
3f510 63 74 20 74 6f 20 62 65 20 69 6e 74 65 72 72 6f  ct to be interro
3f520 67 61 74 65 64 2e 20 20 54 68 65 20 73 65 63 6f  gated.  The seco
3f530 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69  nd argument.** i
3f540 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64  s an integer cod
3f550 65 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63  e for a specific
3f560 20 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41   [SQLITE_STMTSTA
3f570 54 55 53 5f 53 4f 52 54 20 7c 20 63 6f 75 6e 74  TUS_SORT | count
3f580 65 72 5d 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 74  er].** to be int
3f590 65 72 72 6f 67 61 74 65 64 2e 20 0a 2a 2a 20 54  errogated. .** T
3f5a0 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65  he current value
3f5b0 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65   of the requeste
3f5c0 64 20 63 6f 75 6e 74 65 72 20 69 73 20 72 65 74  d counter is ret
3f5d0 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65  urned..** If the
3f5e0 20 72 65 73 65 74 46 6c 67 20 69 73 20 74 72 75   resetFlg is tru
3f5f0 65 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 75 6e  e, then the coun
3f600 74 65 72 20 69 73 20 72 65 73 65 74 20 74 6f 20  ter is reset to 
3f610 7a 65 72 6f 20 61 66 74 65 72 20 74 68 69 73 0a  zero after this.
3f620 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 63 61 6c  ** interface cal
3f630 6c 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a  l returns..**.**
3f640 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69   See also: [sqli
3f650 74 65 33 5f 73 74 61 74 75 73 28 29 5d 20 61 6e  te3_status()] an
3f660 64 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73 74  d [sqlite3_db_st
3f670 61 74 75 73 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49  atus()]..*/.SQLI
3f680 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58  TE_API SQLITE_EX
3f690 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73  PERIMENTAL int s
3f6a0 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74  qlite3_stmt_stat
3f6b0 75 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  us(sqlite3_stmt*
3f6c0 2c 20 69 6e 74 20 6f 70 2c 69 6e 74 20 72 65 73  , int op,int res
3f6d0 65 74 46 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  etFlg);../*.** C
3f6e0 41 50 49 33 52 45 46 3a 20 53 74 61 74 75 73 20  API3REF: Status 
3f6f0 50 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 70  Parameters for p
3f700 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
3f710 74 73 20 7b 48 31 37 35 37 30 7d 20 3c 48 31 37  ts {H17570} <H17
3f720 35 35 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45  550>.** EXPERIME
3f730 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  NTAL.**.** These
3f740 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61   preprocessor ma
3f750 63 72 6f 73 20 64 65 66 69 6e 65 20 69 6e 74 65  cros define inte
3f760 67 65 72 20 63 6f 64 65 73 20 74 68 61 74 20 6e  ger codes that n
3f770 61 6d 65 20 63 6f 75 6e 74 65 72 0a 2a 2a 20 76  ame counter.** v
3f780 61 6c 75 65 73 20 61 73 73 6f 63 69 61 74 65 64  alues associated
3f790 20 77 69 74 68 20 74 68 65 20 5b 73 71 6c 69 74   with the [sqlit
3f7a0 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 29  e3_stmt_status()
3f7b0 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20  ] interface..** 
3f7c0 54 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20  The meanings of 
3f7d0 74 68 65 20 76 61 72 69 6f 75 73 20 63 6f 75 6e  the various coun
3f7e0 74 65 72 73 20 61 72 65 20 61 73 20 66 6f 6c 6c  ters are as foll
3f7f0 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a  ows:.**.** <dl>.
3f800 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54  ** <dt>SQLITE_ST
3f810 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43 41  MTSTATUS_FULLSCA
3f820 4e 5f 53 54 45 50 3c 2f 64 74 3e 0a 2a 2a 20 3c  N_STEP</dt>.** <
3f830 64 64 3e 54 68 69 73 20 69 73 20 74 68 65 20 6e  dd>This is the n
3f840 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74  umber of times t
3f850 68 61 74 20 53 51 4c 69 74 65 20 68 61 73 20 73  hat SQLite has s
3f860 74 65 70 70 65 64 20 66 6f 72 77 61 72 64 20 69  tepped forward i
3f870 6e 0a 2a 2a 20 61 20 74 61 62 6c 65 20 61 73 20  n.** a table as 
3f880 70 61 72 74 20 6f 66 20 61 20 66 75 6c 6c 20 74  part of a full t
3f890 61 62 6c 65 20 73 63 61 6e 2e 20 20 4c 61 72 67  able scan.  Larg
3f8a0 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 74 68  e numbers for th
3f8b0 69 73 20 63 6f 75 6e 74 65 72 0a 2a 2a 20 6d 61  is counter.** ma
3f8c0 79 20 69 6e 64 69 63 61 74 65 20 6f 70 70 6f 72  y indicate oppor
3f8d0 74 75 6e 69 74 69 65 73 20 66 6f 72 20 70 65 72  tunities for per
3f8e0 66 6f 72 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65  formance improve
3f8f0 6d 65 6e 74 20 74 68 72 6f 75 67 68 20 0a 2a 2a  ment through .**
3f900 20 63 61 72 65 66 75 6c 20 75 73 65 20 6f 66 20   careful use of 
3f910 69 6e 64 69 63 65 73 2e 3c 2f 64 64 3e 0a 2a 2a  indices.</dd>.**
3f920 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53  .** <dt>SQLITE_S
3f930 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 3c 2f  TMTSTATUS_SORT</
3f940 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20  dt>.** <dd>This 
3f950 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
3f960 20 73 6f 72 74 20 6f 70 65 72 61 74 69 6f 6e 73   sort operations
3f970 20 74 68 61 74 20 68 61 76 65 20 6f 63 63 75 72   that have occur
3f980 72 65 64 2e 0a 2a 2a 20 41 20 6e 6f 6e 2d 7a 65  red..** A non-ze
3f990 72 6f 20 76 61 6c 75 65 20 69 6e 20 74 68 69 73  ro value in this
3f9a0 20 63 6f 75 6e 74 65 72 20 6d 61 79 20 69 6e 64   counter may ind
3f9b0 69 63 61 74 65 20 61 6e 20 6f 70 70 6f 72 74 75  icate an opportu
3f9c0 6e 69 74 79 20 74 6f 0a 2a 2a 20 69 6d 70 72 6f  nity to.** impro
3f9d0 76 65 6d 65 6e 74 20 70 65 72 66 6f 72 6d 61 6e  vement performan
3f9e0 63 65 20 74 68 72 6f 75 67 68 20 63 61 72 65 66  ce through caref
3f9f0 75 6c 20 75 73 65 20 6f 66 20 69 6e 64 69 63 65  ul use of indice
3fa00 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f  s.</dd>.**.** </
3fa10 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  dl>.*/.#define S
3fa20 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53  QLITE_STMTSTATUS
3fa30 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 20 20  _FULLSCAN_STEP  
3fa40 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c     1.#define SQL
3fa50 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53  ITE_STMTSTATUS_S
3fa60 4f 52 54 20 20 20 20 20 20 20 20 20 20 20 20 20  ORT             
3fa70 20 32 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52   2../*.** CAPI3R
3fa80 45 46 3a 20 43 75 73 74 6f 6d 20 50 61 67 65 20  EF: Custom Page 
3fa90 43 61 63 68 65 20 4f 62 6a 65 63 74 0a 2a 2a 20  Cache Object.** 
3faa0 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
3fab0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 70  ** The sqlite3_p
3fac0 63 61 63 68 65 20 74 79 70 65 20 69 73 20 6f 70  cache type is op
3fad0 61 71 75 65 2e 20 20 49 74 20 69 73 20 69 6d 70  aque.  It is imp
3fae0 6c 65 6d 65 6e 74 65 64 20 62 79 0a 2a 2a 20 74  lemented by.** t
3faf0 68 65 20 70 6c 75 67 67 61 62 6c 65 20 6d 6f 64  he pluggable mod
3fb00 75 6c 65 2e 20 20 54 68 65 20 53 51 4c 69 74 65  ule.  The SQLite
3fb10 20 63 6f 72 65 20 68 61 73 20 6e 6f 20 6b 6e 6f   core has no kno
3fb20 77 6c 65 64 67 65 20 6f 66 0a 2a 2a 20 69 74 73  wledge of.** its
3fb30 20 73 69 7a 65 20 6f 72 20 69 6e 74 65 72 6e 61   size or interna
3fb40 6c 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20  l structure and 
3fb50 6e 65 76 65 72 20 64 65 61 6c 73 20 77 69 74 68  never deals with
3fb60 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   the.** sqlite3_
3fb70 70 63 61 63 68 65 20 6f 62 6a 65 63 74 20 65 78  pcache object ex
3fb80 63 65 70 74 20 62 79 20 68 6f 6c 64 69 6e 67 20  cept by holding 
3fb90 61 6e 64 20 70 61 73 73 69 6e 67 20 70 6f 69 6e  and passing poin
3fba0 74 65 72 73 0a 2a 2a 20 74 6f 20 74 68 65 20 6f  ters.** to the o
3fbb0 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  bject..**.** See
3fbc0 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65   [sqlite3_pcache
3fbd0 5f 6d 65 74 68 6f 64 73 5d 20 66 6f 72 20 61 64  _methods] for ad
3fbe0 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
3fbf0 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  tion..*/.typedef
3fc00 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
3fc10 70 63 61 63 68 65 20 73 71 6c 69 74 65 33 5f 70  pcache sqlite3_p
3fc20 63 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  cache;../*.** CA
3fc30 50 49 33 52 45 46 3a 20 41 70 70 6c 69 63 61 74  PI3REF: Applicat
3fc40 69 6f 6e 20 44 65 66 69 6e 65 64 20 50 61 67 65  ion Defined Page
3fc50 20 43 61 63 68 65 2e 0a 2a 2a 20 4b 45 59 57 4f   Cache..** KEYWO
3fc60 52 44 53 3a 20 7b 70 61 67 65 20 63 61 63 68 65  RDS: {page cache
3fc70 7d 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  }.** EXPERIMENTA
3fc80 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c  L.**.** The [sql
3fc90 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51  ite3_config]([SQ
3fca0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43  LITE_CONFIG_PCAC
3fcb0 48 45 5d 2c 20 2e 2e 2e 29 20 69 6e 74 65 72 66  HE], ...) interf
3fcc0 61 63 65 20 63 61 6e 0a 2a 2a 20 72 65 67 69 73  ace can.** regis
3fcd0 74 65 72 20 61 6e 20 61 6c 74 65 72 6e 61 74 69  ter an alternati
3fce0 76 65 20 70 61 67 65 20 63 61 63 68 65 20 69 6d  ve page cache im
3fcf0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 62 79 20  plementation by 
3fd00 70 61 73 73 69 6e 67 20 69 6e 20 61 6e 20 0a 2a  passing in an .*
3fd10 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  * instance of th
3fd20 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  e sqlite3_pcache
3fd30 5f 6d 65 74 68 6f 64 73 20 73 74 72 75 63 74 75  _methods structu
3fd40 72 65 2e 20 54 68 65 20 6d 61 6a 6f 72 69 74 79  re. The majority
3fd50 20 6f 66 20 74 68 65 20 0a 2a 2a 20 68 65 61 70   of the .** heap
3fd60 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20   memory used by 
3fd70 53 51 4c 69 74 65 20 69 73 20 75 73 65 64 20 62  SQLite is used b
3fd80 79 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  y the page cache
3fd90 20 74 6f 20 63 61 63 68 65 20 64 61 74 61 20 72   to cache data r
3fda0 65 61 64 20 0a 2a 2a 20 66 72 6f 6d 2c 20 6f 72  ead .** from, or
3fdb0 20 72 65 61 64 79 20 74 6f 20 62 65 20 77 72 69   ready to be wri
3fdc0 74 74 65 6e 20 74 6f 2c 20 74 68 65 20 64 61 74  tten to, the dat
3fdd0 61 62 61 73 65 20 66 69 6c 65 2e 20 42 79 20 69  abase file. By i
3fde0 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 61 20 0a 2a  mplementing a .*
3fdf0 2a 20 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61  * custom page ca
3fe00 63 68 65 20 75 73 69 6e 67 20 74 68 69 73 20 41  che using this A
3fe10 50 49 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69  PI, an applicati
3fe20 6f 6e 20 63 61 6e 20 63 6f 6e 74 72 6f 6c 20 6d  on can control m
3fe30 6f 72 65 20 0a 2a 2a 20 70 72 65 63 69 73 65 6c  ore .** precisel
3fe40 79 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20  y the amount of 
3fe50 6d 65 6d 6f 72 79 20 63 6f 6e 73 75 6d 65 64 20  memory consumed 
3fe60 62 79 20 53 51 4c 69 74 65 2c 20 74 68 65 20 77  by SQLite, the w
3fe70 61 79 20 69 6e 20 77 68 69 63 68 20 0a 2a 2a 20  ay in which .** 
3fe80 74 68 61 74 20 6d 65 6d 6f 72 79 20 69 73 20 61  that memory is a
3fe90 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 72 65 6c  llocated and rel
3fea0 65 61 73 65 64 2c 20 61 6e 64 20 74 68 65 20 70  eased, and the p
3feb0 6f 6c 69 63 69 65 73 20 75 73 65 64 20 74 6f 20  olicies used to 
3fec0 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 20 65 78  .** determine ex
3fed0 61 63 74 6c 79 20 77 68 69 63 68 20 70 61 72 74  actly which part
3fee0 73 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20  s of a database 
3fef0 66 69 6c 65 20 61 72 65 20 63 61 63 68 65 64 20  file are cached 
3ff00 61 6e 64 20 66 6f 72 20 0a 2a 2a 20 68 6f 77 20  and for .** how 
3ff10 6c 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  long..**.** The 
3ff20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
3ff30 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d  sqlite3_pcache_m
3ff40 65 74 68 6f 64 73 20 73 74 72 75 63 74 75 72 65  ethods structure
3ff50 20 61 72 65 20 63 6f 70 69 65 64 20 74 6f 20 61   are copied to a
3ff60 6e 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 62 75  n.** internal bu
3ff70 66 66 65 72 20 62 79 20 53 51 4c 69 74 65 20 77  ffer by SQLite w
3ff80 69 74 68 69 6e 20 74 68 65 20 63 61 6c 6c 20 74  ithin the call t
3ff90 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  o [sqlite3_confi
3ffa0 67 5d 2e 20 20 48 65 6e 63 65 0a 2a 2a 20 74 68  g].  Hence.** th
3ffb0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 61  e application ma
3ffc0 79 20 64 69 73 63 61 72 64 20 74 68 65 20 70 61  y discard the pa
3ffd0 72 61 6d 65 74 65 72 20 61 66 74 65 72 20 74 68  rameter after th
3ffe0 65 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71  e call to.** [sq
3fff0 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20  lite3_config()] 
40000 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54  returns..**.** T
40010 68 65 20 78 49 6e 69 74 28 29 20 6d 65 74 68 6f  he xInit() metho
40020 64 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65  d is called once
40030 20 66 6f 72 20 65 61 63 68 20 63 61 6c 6c 20 74   for each call t
40040 6f 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  o [sqlite3_initi
40050 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 28 75 73 75  alize()].** (usu
40060 61 6c 6c 79 20 6f 6e 6c 79 20 6f 6e 63 65 20 64  ally only once d
40070 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65 74 69  uring the lifeti
40080 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 63 65 73  me of the proces
40090 73 29 2e 20 49 74 20 69 73 20 70 61 73 73 65 64  s). It is passed
400a0 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 74 68  .** a copy of th
400b0 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  e sqlite3_pcache
400c0 5f 6d 65 74 68 6f 64 73 2e 70 41 72 67 20 76 61  _methods.pArg va
400d0 6c 75 65 2e 20 49 74 20 63 61 6e 20 62 65 20 75  lue. It can be u
400e0 73 65 64 20 74 6f 20 73 65 74 0a 2a 2a 20 75 70  sed to set.** up
400f0 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 72   global structur
40100 65 73 20 61 6e 64 20 6d 75 74 65 78 65 73 20 72  es and mutexes r
40110 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 63  equired by the c
40120 75 73 74 6f 6d 20 70 61 67 65 20 63 61 63 68 65  ustom page cache
40130 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74   .** implementat
40140 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ion. .**.** The 
40150 78 53 68 75 74 64 6f 77 6e 28 29 20 6d 65 74 68  xShutdown() meth
40160 6f 64 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f  od is called fro
40170 6d 20 77 69 74 68 69 6e 20 5b 73 71 6c 69 74 65  m within [sqlite
40180 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2c 20 0a  3_shutdown()], .
40190 2a 2a 20 69 66 20 74 68 65 20 61 70 70 6c 69 63  ** if the applic
401a0 61 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 20 74 68  ation invokes th
401b0 69 73 20 41 50 49 2e 20 49 74 20 63 61 6e 20 62  is API. It can b
401c0 65 20 75 73 65 64 20 74 6f 20 63 6c 65 61 6e 20  e used to clean 
401d0 75 70 20 0a 2a 2a 20 61 6e 79 20 6f 75 74 73 74  up .** any outst
401e0 61 6e 64 69 6e 67 20 72 65 73 6f 75 72 63 65 73  anding resources
401f0 20 62 65 66 6f 72 65 20 70 72 6f 63 65 73 73 20   before process 
40200 73 68 75 74 64 6f 77 6e 2c 20 69 66 20 72 65 71  shutdown, if req
40210 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  uired..**.** SQL
40220 69 74 65 20 68 6f 6c 64 73 20 61 20 5b 53 51 4c  ite holds a [SQL
40230 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
40240 49 56 45 5d 20 6d 75 74 65 78 20 77 68 65 6e 20  IVE] mutex when 
40250 69 74 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 74 68  it invokes.** th
40260 65 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 2c 20  e xInit method, 
40270 73 6f 20 74 68 65 20 78 49 6e 69 74 20 6d 65 74  so the xInit met
40280 68 6f 64 20 6e 65 65 64 20 6e 6f 74 20 62 65 20  hod need not be 
40290 74 68 72 65 61 64 73 61 66 65 2e 20 20 54 68 65  threadsafe.  The
402a0 0a 2a 2a 20 78 53 68 75 74 64 6f 77 6e 20 6d 65  .** xShutdown me
402b0 74 68 6f 64 20 69 73 20 6f 6e 6c 79 20 63 61 6c  thod is only cal
402c0 6c 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65  led from [sqlite
402d0 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 20 73 6f  3_shutdown()] so
402e0 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20   it does.** not 
402f0 6e 65 65 64 20 74 6f 20 62 65 20 74 68 72 65 61  need to be threa
40300 64 73 61 66 65 20 65 69 74 68 65 72 2e 20 20 41  dsafe either.  A
40310 6c 6c 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 73  ll other methods
40320 20 6d 75 73 74 20 62 65 20 74 68 72 65 61 64 73   must be threads
40330 61 66 65 0a 2a 2a 20 69 6e 20 6d 75 6c 74 69 74  afe.** in multit
40340 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61 74  hreaded applicat
40350 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  ions..**.** SQLi
40360 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 69 6e  te will never in
40370 76 6f 6b 65 20 78 49 6e 69 74 28 29 20 6d 6f 72  voke xInit() mor
40380 65 20 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68  e than once with
40390 6f 75 74 20 61 6e 20 69 6e 74 65 72 76 65 6e 69  out an interveni
403a0 6e 67 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 53  ng.** call to xS
403b0 68 75 74 64 6f 77 6e 28 29 2e 0a 2a 2a 0a 2a 2a  hutdown()..**.**
403c0 20 54 68 65 20 78 43 72 65 61 74 65 28 29 20 6d   The xCreate() m
403d0 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 74 6f  ethod is used to
403e0 20 63 6f 6e 73 74 72 75 63 74 20 61 20 6e 65 77   construct a new
403f0 20 63 61 63 68 65 20 69 6e 73 74 61 6e 63 65 2e   cache instance.
40400 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c    SQLite.** will
40410 20 74 79 70 69 63 61 6c 6c 79 20 63 72 65 61 74   typically creat
40420 65 20 6f 6e 65 20 63 61 63 68 65 20 69 6e 73 74  e one cache inst
40430 61 6e 63 65 20 66 6f 72 20 65 61 63 68 20 6f 70  ance for each op
40440 65 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  en database file
40450 2c 0a 2a 2a 20 74 68 6f 75 67 68 20 74 68 69 73  ,.** though this
40460 20 69 73 20 6e 6f 74 20 67 75 61 72 61 6e 74 65   is not guarante
40470 65 64 2e 20 54 68 65 0a 2a 2a 20 66 69 72 73 74  ed. The.** first
40480 20 70 61 72 61 6d 65 74 65 72 2c 20 73 7a 50 61   parameter, szPa
40490 67 65 2c 20 69 73 20 74 68 65 20 73 69 7a 65 20  ge, is the size 
404a0 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20  in bytes of the 
404b0 70 61 67 65 73 20 74 68 61 74 20 6d 75 73 74 0a  pages that must.
404c0 2a 2a 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20  ** be allocated 
404d0 62 79 20 74 68 65 20 63 61 63 68 65 2e 20 20 73  by the cache.  s
404e0 7a 50 61 67 65 20 77 69 6c 6c 20 6e 6f 74 20 62  zPage will not b
404f0 65 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f  e a power of two
40500 2e 20 20 73 7a 50 61 67 65 0a 2a 2a 20 77 69 6c  .  szPage.** wil
40510 6c 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20  l the page size 
40520 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
40530 66 69 6c 65 20 74 68 61 74 20 69 73 20 74 6f 20  file that is to 
40540 62 65 20 63 61 63 68 65 64 20 70 6c 75 73 20 61  be cached plus a
40550 6e 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 20 28  n.** increment (
40560 68 65 72 65 20 63 61 6c 6c 65 64 20 22 52 22 29  here called "R")
40570 20 6f 66 20 61 62 6f 75 74 20 31 30 30 20 6f 72   of about 100 or
40580 20 32 30 30 2e 20 20 53 51 4c 69 74 65 20 77 69   200.  SQLite wi
40590 6c 6c 20 75 73 65 20 74 68 65 0a 2a 2a 20 65 78  ll use the.** ex
405a0 74 72 61 20 52 20 62 79 74 65 73 20 6f 6e 20 65  tra R bytes on e
405b0 61 63 68 20 70 61 67 65 20 74 6f 20 73 74 6f 72  ach page to stor
405c0 65 20 6d 65 74 61 64 61 74 61 20 61 62 6f 75 74  e metadata about
405d0 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a   the underlying.
405e0 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 67 65  ** database page
405f0 20 6f 6e 20 64 69 73 6b 2e 20 20 54 68 65 20 76   on disk.  The v
40600 61 6c 75 65 20 6f 66 20 52 20 64 65 70 65 6e 64  alue of R depend
40610 73 0a 2a 2a 20 6f 6e 20 74 68 65 20 53 51 4c 69  s.** on the SQLi
40620 74 65 20 76 65 72 73 69 6f 6e 2c 20 74 68 65 20  te version, the 
40630 74 61 72 67 65 74 20 70 6c 61 74 66 6f 72 6d 2c  target platform,
40640 20 61 6e 64 20 68 6f 77 20 53 51 4c 69 74 65 20   and how SQLite 
40650 77 61 73 20 63 6f 6d 70 69 6c 65 64 2e 0a 2a 2a  was compiled..**
40660 20 52 20 69 73 20 63 6f 6e 73 74 61 6e 74 20 66   R is constant f
40670 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  or a particular 
40680 62 75 69 6c 64 20 6f 66 20 53 51 4c 69 74 65 2e  build of SQLite.
40690 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67    The second arg
406a0 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 78 43 72 65  ument to.** xCre
406b0 61 74 65 28 29 2c 20 62 50 75 72 67 65 61 62 6c  ate(), bPurgeabl
406c0 65 2c 20 69 73 20 74 72 75 65 20 69 66 20 74 68  e, is true if th
406d0 65 20 63 61 63 68 65 20 62 65 69 6e 67 20 63 72  e cache being cr
406e0 65 61 74 65 64 20 77 69 6c 6c 0a 2a 2a 20 62 65  eated will.** be
406f0 20 75 73 65 64 20 74 6f 20 63 61 63 68 65 20 64   used to cache d
40700 61 74 61 62 61 73 65 20 70 61 67 65 73 20 6f 66  atabase pages of
40710 20 61 20 66 69 6c 65 20 73 74 6f 72 65 64 20 6f   a file stored o
40720 6e 20 64 69 73 6b 2c 20 6f 72 0a 2a 2a 20 66 61  n disk, or.** fa
40730 6c 73 65 20 69 66 20 69 74 20 69 73 20 75 73 65  lse if it is use
40740 64 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f  d for an in-memo
40750 72 79 20 64 61 74 61 62 61 73 65 2e 20 54 68 65  ry database. The
40760 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74   cache implement
40770 61 74 69 6f 6e 0a 2a 2a 20 64 6f 65 73 20 6e 6f  ation.** does no
40780 74 20 68 61 76 65 20 74 6f 20 64 6f 20 61 6e 79  t have to do any
40790 74 68 69 6e 67 20 73 70 65 63 69 61 6c 20 62 61  thing special ba
407a0 73 65 64 20 77 69 74 68 20 74 68 65 20 76 61 6c  sed with the val
407b0 75 65 20 6f 66 20 62 50 75 72 67 65 61 62 6c 65  ue of bPurgeable
407c0 3b 0a 2a 2a 20 69 74 20 69 73 20 70 75 72 65 6c  ;.** it is purel
407d0 79 20 61 64 76 69 73 6f 72 79 2e 20 20 4f 6e 20  y advisory.  On 
407e0 61 20 63 61 63 68 65 20 77 68 65 72 65 20 62 50  a cache where bP
407f0 75 72 67 65 61 62 6c 65 20 69 73 20 66 61 6c 73  urgeable is fals
40800 65 2c 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a  e, SQLite will.*
40810 2a 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 20 78  * never invoke x
40820 55 6e 70 69 6e 28 29 20 65 78 63 65 70 74 20 74  Unpin() except t
40830 6f 20 64 65 6c 69 62 65 72 61 74 65 6c 79 20 64  o deliberately d
40840 65 6c 65 74 65 20 61 20 70 61 67 65 2e 0a 2a 2a  elete a page..**
40850 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
40860 20 61 20 63 61 63 68 65 20 63 72 65 61 74 65 64   a cache created
40870 20 77 69 74 68 20 62 50 75 72 67 65 61 62 6c 65   with bPurgeable
40880 20 73 65 74 20 74 6f 20 66 61 6c 73 65 20 77 69   set to false wi
40890 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 63 6f 6e 74  ll.** never cont
408a0 61 69 6e 20 61 6e 79 20 75 6e 70 69 6e 6e 65 64  ain any unpinned
408b0 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   pages..**.** Th
408c0 65 20 78 43 61 63 68 65 73 69 7a 65 28 29 20 6d  e xCachesize() m
408d0 65 74 68 6f 64 20 6d 61 79 20 62 65 20 63 61 6c  ethod may be cal
408e0 6c 65 64 20 61 74 20 61 6e 79 20 74 69 6d 65 20  led at any time 
408f0 62 79 20 53 51 4c 69 74 65 20 74 6f 20 73 65 74  by SQLite to set
40900 20 74 68 65 0a 2a 2a 20 73 75 67 67 65 73 74 65   the.** suggeste
40910 64 20 6d 61 78 69 6d 75 6d 20 63 61 63 68 65 2d  d maximum cache-
40920 73 69 7a 65 20 28 6e 75 6d 62 65 72 20 6f 66 20  size (number of 
40930 70 61 67 65 73 20 73 74 6f 72 65 64 20 62 79 29  pages stored by)
40940 20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 69 6e   the cache.** in
40950 73 74 61 6e 63 65 20 70 61 73 73 65 64 20 61 73  stance passed as
40960 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
40970 65 6e 74 2e 20 54 68 69 73 20 69 73 20 74 68 65  ent. This is the
40980 20 76 61 6c 75 65 20 63 6f 6e 66 69 67 75 72 65   value configure
40990 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 53  d using.** the S
409a0 51 4c 69 74 65 20 22 5b 50 52 41 47 4d 41 20 63  QLite "[PRAGMA c
409b0 61 63 68 65 5f 73 69 7a 65 5d 22 20 63 6f 6d 6d  ache_size]" comm
409c0 61 6e 64 2e 20 41 73 20 77 69 74 68 20 74 68 65  and. As with the
409d0 20 62 50 75 72 67 65 61 62 6c 65 20 70 61 72 61   bPurgeable para
409e0 6d 65 74 65 72 2c 0a 2a 2a 20 74 68 65 20 69 6d  meter,.** the im
409f0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20  plementation is 
40a00 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20  not required to 
40a10 64 6f 20 61 6e 79 74 68 69 6e 67 20 77 69 74 68  do anything with
40a20 20 74 68 69 73 0a 2a 2a 20 76 61 6c 75 65 3b 20   this.** value; 
40a30 69 74 20 69 73 20 61 64 76 69 73 6f 72 79 20 6f  it is advisory o
40a40 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  nly..**.** The x
40a50 50 61 67 65 63 6f 75 6e 74 28 29 20 6d 65 74 68  Pagecount() meth
40a60 6f 64 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e  od should return
40a70 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
40a80 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a  ages currently.*
40a90 2a 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  * stored in the 
40aa0 63 61 63 68 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68  cache..** .** Th
40ab0 65 20 78 46 65 74 63 68 28 29 20 6d 65 74 68 6f  e xFetch() metho
40ac0 64 20 69 73 20 75 73 65 64 20 74 6f 20 66 65 74  d is used to fet
40ad0 63 68 20 61 20 70 61 67 65 20 61 6e 64 20 72 65  ch a page and re
40ae0 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
40af0 6f 20 69 74 2e 20 0a 2a 2a 20 41 20 27 70 61 67  o it. .** A 'pag
40b00 65 27 2c 20 69 6e 20 74 68 69 73 20 63 6f 6e 74  e', in this cont
40b10 65 78 74 2c 20 69 73 20 61 20 62 75 66 66 65 72  ext, is a buffer
40b20 20 6f 66 20 73 7a 50 61 67 65 20 62 79 74 65 73   of szPage bytes
40b30 20 61 6c 69 67 6e 65 64 20 61 74 20 61 6e 0a 2a   aligned at an.*
40b40 2a 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72  * 8-byte boundar
40b50 79 2e 20 54 68 65 20 70 61 67 65 20 74 6f 20 62  y. The page to b
40b60 65 20 66 65 74 63 68 65 64 20 69 73 20 64 65 74  e fetched is det
40b70 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 6b  ermined by the k
40b80 65 79 2e 20 54 68 65 0a 2a 2a 20 6d 69 6d 69 6d  ey. The.** mimim
40b90 75 6d 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20  um key value is 
40ba0 31 2e 20 41 66 74 65 72 20 69 74 20 68 61 73 20  1. After it has 
40bb0 62 65 65 6e 20 72 65 74 72 69 65 76 65 64 20 75  been retrieved u
40bc0 73 69 6e 67 20 78 46 65 74 63 68 2c 20 74 68 65  sing xFetch, the
40bd0 20 70 61 67 65 20 0a 2a 2a 20 69 73 20 63 6f 6e   page .** is con
40be0 73 69 64 65 72 65 64 20 74 6f 20 62 65 20 22 70  sidered to be "p
40bf0 69 6e 6e 65 64 22 2e 0a 2a 2a 0a 2a 2a 20 49 66  inned"..**.** If
40c00 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70   the requested p
40c10 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 69  age is already i
40c20 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  n the page cache
40c30 2c 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20  , then the page 
40c40 63 61 63 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65  cache.** impleme
40c50 6e 74 61 74 69 6f 6e 20 6d 75 73 74 20 72 65 74  ntation must ret
40c60 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
40c70 20 74 68 65 20 70 61 67 65 20 62 75 66 66 65 72   the page buffer
40c80 20 77 69 74 68 20 69 74 73 20 63 6f 6e 74 65 6e   with its conten
40c90 74 0a 2a 2a 20 69 6e 74 61 63 74 2e 20 20 49 66  t.** intact.  If
40ca0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70   the requested p
40cb0 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61  age is not alrea
40cc0 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c  dy in the cache,
40cd0 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 62 65 68   then the.** beh
40ce0 61 76 69 6f 72 20 6f 66 20 74 68 65 20 63 61 63  avior of the cac
40cf0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
40d00 6e 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20  n is determined 
40d10 62 79 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  by the value of 
40d20 74 68 65 0a 2a 2a 20 63 72 65 61 74 65 46 6c 61  the.** createFla
40d30 67 20 70 61 72 61 6d 65 74 65 72 20 70 61 73 73  g parameter pass
40d40 65 64 20 74 6f 20 78 46 65 74 63 68 2c 20 61 63  ed to xFetch, ac
40d50 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 66  cording to the f
40d60 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 3a 0a  ollowing table:.
40d70 2a 2a 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72  **.** <table bor
40d80 64 65 72 3d 31 20 77 69 64 74 68 3d 38 35 25 20  der=1 width=85% 
40d90 61 6c 69 67 6e 3d 63 65 6e 74 65 72 3e 0a 2a 2a  align=center>.**
40da0 20 3c 74 72 3e 3c 74 68 3e 20 63 72 65 61 74 65   <tr><th> create
40db0 46 6c 61 67 20 3c 74 68 3e 20 42 65 68 61 76 69  Flag <th> Behavi
40dc0 6f 75 72 20 77 68 65 6e 20 70 61 67 65 20 69 73  our when page is
40dd0 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20   not already in 
40de0 63 61 63 68 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64  cache.** <tr><td
40df0 3e 20 30 20 3c 74 64 3e 20 44 6f 20 6e 6f 74 20  > 0 <td> Do not 
40e00 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  allocate a new p
40e10 61 67 65 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c  age.  Return NUL
40e20 4c 2e 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 31  L..** <tr><td> 1
40e30 20 3c 74 64 3e 20 41 6c 6c 6f 63 61 74 65 20 61   <td> Allocate a
40e40 20 6e 65 77 20 70 61 67 65 20 69 66 20 69 74 20   new page if it 
40e50 65 61 73 79 20 61 6e 64 20 63 6f 6e 76 65 6e 69  easy and conveni
40e60 65 6e 74 20 74 6f 20 64 6f 20 73 6f 2e 0a 2a 2a  ent to do so..**
40e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
40e80 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72   Otherwise retur
40e90 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20 3c 74 72 3e 3c  n NULL..** <tr><
40ea0 74 64 3e 20 32 20 3c 74 64 3e 20 4d 61 6b 65 20  td> 2 <td> Make 
40eb0 65 76 65 72 79 20 65 66 66 6f 72 74 20 74 6f 20  every effort to 
40ec0 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  allocate a new p
40ed0 61 67 65 2e 20 20 4f 6e 6c 79 20 72 65 74 75 72  age.  Only retur
40ee0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  n.**            
40ef0 20 20 20 20 20 4e 55 4c 4c 20 69 66 20 61 6c 6c       NULL if all
40f00 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 70 61  ocating a new pa
40f10 67 65 20 69 73 20 65 66 66 65 63 74 69 76 65 6c  ge is effectivel
40f20 79 20 69 6d 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a  y impossible..**
40f30 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20   </table>.**.** 
40f40 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 72 6d  SQLite will norm
40f50 61 6c 6c 79 20 69 6e 76 6f 6b 65 20 78 46 65 74  ally invoke xFet
40f60 63 68 28 29 20 77 69 74 68 20 61 20 63 72 65 61  ch() with a crea
40f70 74 65 46 6c 61 67 20 6f 66 20 30 20 6f 72 20 31  teFlag of 0 or 1
40f80 2e 20 20 49 66 0a 2a 2a 20 61 20 63 61 6c 6c 20  .  If.** a call 
40f90 74 6f 20 78 46 65 74 63 68 28 29 20 77 69 74 68  to xFetch() with
40fa0 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 72   createFlag==1 r
40fb0 65 74 75 72 6e 73 20 4e 55 4c 4c 2c 20 74 68 65  eturns NULL, the
40fc0 6e 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a  n SQLite will.**
40fd0 20 61 74 74 65 6d 70 74 20 74 6f 20 75 6e 70 69   attempt to unpi
40fe0 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 63 61  n one or more ca
40ff0 63 68 65 20 70 61 67 65 73 20 62 79 20 73 70 69  che pages by spi
41000 6c 6c 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e  lling the conten
41010 74 20 6f 66 0a 2a 2a 20 70 69 6e 6e 65 64 20 70  t of.** pinned p
41020 61 67 65 73 20 74 6f 20 64 69 73 6b 20 61 6e 64  ages to disk and
41030 20 73 79 6e 63 68 69 6e 67 20 74 68 65 20 6f 70   synching the op
41040 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 64  erating system d
41050 69 73 6b 20 63 61 63 68 65 2e 20 41 66 74 65 72  isk cache. After
41060 0a 2a 2a 20 61 74 74 65 6d 70 74 69 6e 67 20 74  .** attempting t
41070 6f 20 75 6e 70 69 6e 20 70 61 67 65 73 2c 20 74  o unpin pages, t
41080 68 65 20 78 46 65 74 63 68 28 29 20 6d 65 74 68  he xFetch() meth
41090 6f 64 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b  od will be invok
410a0 65 64 20 61 67 61 69 6e 20 77 69 74 68 0a 2a 2a  ed again with.**
410b0 20 61 20 63 72 65 61 74 65 46 6c 61 67 20 6f 66   a createFlag of
410c0 20 32 2e 0a 2a 2a 0a 2a 2a 20 78 55 6e 70 69 6e   2..**.** xUnpin
410d0 28 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  () is called by 
410e0 53 51 4c 69 74 65 20 77 69 74 68 20 61 20 70 6f  SQLite with a po
410f0 69 6e 74 65 72 20 74 6f 20 61 20 63 75 72 72 65  inter to a curre
41100 6e 74 6c 79 20 70 69 6e 6e 65 64 20 70 61 67 65  ntly pinned page
41110 0a 2a 2a 20 61 73 20 69 74 73 20 73 65 63 6f 6e  .** as its secon
41120 64 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 74  d argument. If t
41130 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74  he third paramet
41140 65 72 2c 20 64 69 73 63 61 72 64 2c 20 69 73 20  er, discard, is 
41150 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65  non-zero,.** the
41160 6e 20 74 68 65 20 70 61 67 65 20 73 68 6f 75 6c  n the page shoul
41170 64 20 62 65 20 65 76 69 63 74 65 64 20 66 72 6f  d be evicted fro
41180 6d 20 74 68 65 20 63 61 63 68 65 2e 20 49 6e 20  m the cache. In 
41190 74 68 69 73 20 63 61 73 65 20 53 51 4c 69 74 65  this case SQLite
411a0 20 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61   .** assumes tha
411b0 74 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20  t the next time 
411c0 74 68 65 20 70 61 67 65 20 69 73 20 72 65 74 72  the page is retr
411d0 69 65 76 65 64 20 66 72 6f 6d 20 74 68 65 20 63  ieved from the c
411e0 61 63 68 65 20 75 73 69 6e 67 0a 2a 2a 20 74 68  ache using.** th
411f0 65 20 78 46 65 74 63 68 28 29 20 6d 65 74 68 6f  e xFetch() metho
41200 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 7a 65  d, it will be ze
41210 72 6f 65 64 2e 20 49 66 20 74 68 65 20 64 69 73  roed. If the dis
41220 63 61 72 64 20 70 61 72 61 6d 65 74 65 72 20 69  card parameter i
41230 73 0a 2a 2a 20 7a 65 72 6f 2c 20 74 68 65 6e 20  s.** zero, then 
41240 74 68 65 20 70 61 67 65 20 69 73 20 63 6f 6e 73  the page is cons
41250 69 64 65 72 65 64 20 74 6f 20 62 65 20 75 6e 70  idered to be unp
41260 69 6e 6e 65 64 2e 20 54 68 65 20 63 61 63 68 65  inned. The cache
41270 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a   implementation.
41280 2a 2a 20 6d 61 79 20 63 68 6f 6f 73 65 20 74 6f  ** may choose to
41290 20 65 76 69 63 74 20 75 6e 70 69 6e 6e 65 64 20   evict unpinned 
412a0 70 61 67 65 73 20 61 74 20 61 6e 79 20 74 69 6d  pages at any tim
412b0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 63  e..**.** The cac
412c0 68 65 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72  he is not requir
412d0 65 64 20 74 6f 20 70 65 72 66 6f 72 6d 20 61 6e  ed to perform an
412e0 79 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  y reference coun
412f0 74 69 6e 67 2e 20 41 20 73 69 6e 67 6c 65 20 0a  ting. A single .
41300 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 55 6e 70 69  ** call to xUnpi
41310 6e 28 29 20 75 6e 70 69 6e 73 20 74 68 65 20 70  n() unpins the p
41320 61 67 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f  age regardless o
41330 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  f the number of 
41340 70 72 69 6f 72 20 63 61 6c 6c 73 20 0a 2a 2a 20  prior calls .** 
41350 74 6f 20 78 46 65 74 63 68 28 29 2e 0a 2a 2a 0a  to xFetch()..**.
41360 2a 2a 20 54 68 65 20 78 52 65 6b 65 79 28 29 20  ** The xRekey() 
41370 6d 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 74  method is used t
41380 6f 20 63 68 61 6e 67 65 20 74 68 65 20 6b 65 79  o change the key
41390 20 76 61 6c 75 65 20 61 73 73 6f 63 69 61 74 65   value associate
413a0 64 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 70 61  d with the.** pa
413b0 67 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ge passed as the
413c0 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
413d0 20 66 72 6f 6d 20 6f 6c 64 4b 65 79 20 74 6f 20   from oldKey to 
413e0 6e 65 77 4b 65 79 2e 20 49 66 20 74 68 65 20 63  newKey. If the c
413f0 61 63 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73  ache.** previous
41400 6c 79 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65  ly contains an e
41410 6e 74 72 79 20 61 73 73 6f 63 69 61 74 65 64 20  ntry associated 
41420 77 69 74 68 20 6e 65 77 4b 65 79 2c 20 69 74 20  with newKey, it 
41430 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 64 69 73  should be.** dis
41440 63 61 72 64 65 64 2e 20 41 6e 79 20 70 72 69 6f  carded. Any prio
41450 72 20 63 61 63 68 65 20 65 6e 74 72 79 20 61 73  r cache entry as
41460 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e 65  sociated with ne
41470 77 4b 65 79 20 69 73 20 67 75 61 72 61 6e 74 65  wKey is guarante
41480 65 64 20 6e 6f 74 0a 2a 2a 20 74 6f 20 62 65 20  ed not.** to be 
41490 70 69 6e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68  pinned..**.** Wh
414a0 65 6e 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20  en SQLite calls 
414b0 74 68 65 20 78 54 72 75 6e 63 61 74 65 28 29 20  the xTruncate() 
414c0 6d 65 74 68 6f 64 2c 20 74 68 65 20 63 61 63 68  method, the cach
414d0 65 20 6d 75 73 74 20 64 69 73 63 61 72 64 20 61  e must discard a
414e0 6c 6c 0a 2a 2a 20 65 78 69 73 74 69 6e 67 20 63  ll.** existing c
414f0 61 63 68 65 20 65 6e 74 72 69 65 73 20 77 69 74  ache entries wit
41500 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 28  h page numbers (
41510 6b 65 79 73 29 20 67 72 65 61 74 65 72 20 74 68  keys) greater th
41520 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a 2a 20 74  an or equal.** t
41530 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  o the value of t
41540 68 65 20 69 4c 69 6d 69 74 20 70 61 72 61 6d 65  he iLimit parame
41550 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 78 54  ter passed to xT
41560 72 75 6e 63 61 74 65 28 29 2e 20 49 66 20 61 6e  runcate(). If an
41570 79 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 70 61  y.** of these pa
41580 67 65 73 20 61 72 65 20 70 69 6e 6e 65 64 2c 20  ges are pinned, 
41590 74 68 65 79 20 61 72 65 20 69 6d 70 6c 69 63 69  they are implici
415a0 74 6c 79 20 75 6e 70 69 6e 6e 65 64 2c 20 6d 65  tly unpinned, me
415b0 61 6e 69 6e 67 20 74 68 61 74 0a 2a 2a 20 74 68  aning that.** th
415c0 65 79 20 63 61 6e 20 62 65 20 73 61 66 65 6c 79  ey can be safely
415d0 20 64 69 73 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a   discarded..**.*
415e0 2a 20 54 68 65 20 78 44 65 73 74 72 6f 79 28 29  * The xDestroy()
415f0 20 6d 65 74 68 6f 64 20 69 73 20 75 73 65 64 20   method is used 
41600 74 6f 20 64 65 6c 65 74 65 20 61 20 63 61 63 68  to delete a cach
41610 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 78  e allocated by x
41620 43 72 65 61 74 65 28 29 2e 0a 2a 2a 20 41 6c 6c  Create()..** All
41630 20 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63   resources assoc
41640 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73  iated with the s
41650 70 65 63 69 66 69 65 64 20 63 61 63 68 65 20 73  pecified cache s
41660 68 6f 75 6c 64 20 62 65 20 66 72 65 65 64 2e 20  hould be freed. 
41670 41 66 74 65 72 0a 2a 2a 20 63 61 6c 6c 69 6e 67  After.** calling
41680 20 74 68 65 20 78 44 65 73 74 72 6f 79 28 29 20   the xDestroy() 
41690 6d 65 74 68 6f 64 2c 20 53 51 4c 69 74 65 20 63  method, SQLite c
416a0 6f 6e 73 69 64 65 72 73 20 74 68 65 20 5b 73 71  onsiders the [sq
416b0 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 5d 0a 2a  lite3_pcache*].*
416c0 2a 20 68 61 6e 64 6c 65 20 69 6e 76 61 6c 69 64  * handle invalid
416d0 2c 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 75  , and will not u
416e0 73 65 20 69 74 20 77 69 74 68 20 61 6e 79 20 6f  se it with any o
416f0 74 68 65 72 20 73 71 6c 69 74 65 33 5f 70 63 61  ther sqlite3_pca
41700 63 68 65 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 66  che_methods.** f
41710 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 70  unctions..*/.typ
41720 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
41730 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f  te3_pcache_metho
41740 64 73 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  ds sqlite3_pcach
41750 65 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63  e_methods;.struc
41760 74 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  t sqlite3_pcache
41770 5f 6d 65 74 68 6f 64 73 20 7b 0a 20 20 76 6f 69  _methods {.  voi
41780 64 20 2a 70 41 72 67 3b 0a 20 20 69 6e 74 20 28  d *pArg;.  int (
41790 2a 78 49 6e 69 74 29 28 76 6f 69 64 2a 29 3b 0a  *xInit)(void*);.
417a0 20 20 76 6f 69 64 20 28 2a 78 53 68 75 74 64 6f    void (*xShutdo
417b0 77 6e 29 28 76 6f 69 64 2a 29 3b 0a 20 20 73 71  wn)(void*);.  sq
417c0 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 28 2a  lite3_pcache *(*
417d0 78 43 72 65 61 74 65 29 28 69 6e 74 20 73 7a 50  xCreate)(int szP
417e0 61 67 65 2c 20 69 6e 74 20 62 50 75 72 67 65 61  age, int bPurgea
417f0 62 6c 65 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78  ble);.  void (*x
41800 43 61 63 68 65 73 69 7a 65 29 28 73 71 6c 69 74  Cachesize)(sqlit
41810 65 33 5f 70 63 61 63 68 65 2a 2c 20 69 6e 74 20  e3_pcache*, int 
41820 6e 43 61 63 68 65 73 69 7a 65 29 3b 0a 20 20 69  nCachesize);.  i
41830 6e 74 20 28 2a 78 50 61 67 65 63 6f 75 6e 74 29  nt (*xPagecount)
41840 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a  (sqlite3_pcache*
41850 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 46 65  );.  void *(*xFe
41860 74 63 68 29 28 73 71 6c 69 74 65 33 5f 70 63 61  tch)(sqlite3_pca
41870 63 68 65 2a 2c 20 75 6e 73 69 67 6e 65 64 20 6b  che*, unsigned k
41880 65 79 2c 20 69 6e 74 20 63 72 65 61 74 65 46 6c  ey, int createFl
41890 61 67 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 55  ag);.  void (*xU
418a0 6e 70 69 6e 29 28 73 71 6c 69 74 65 33 5f 70 63  npin)(sqlite3_pc
418b0 61 63 68 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e  ache*, void*, in
418c0 74 20 64 69 73 63 61 72 64 29 3b 0a 20 20 76 6f  t discard);.  vo
418d0 69 64 20 28 2a 78 52 65 6b 65 79 29 28 73 71 6c  id (*xRekey)(sql
418e0 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 76 6f  ite3_pcache*, vo
418f0 69 64 2a 2c 20 75 6e 73 69 67 6e 65 64 20 6f 6c  id*, unsigned ol
41900 64 4b 65 79 2c 20 75 6e 73 69 67 6e 65 64 20 6e  dKey, unsigned n
41910 65 77 4b 65 79 29 3b 0a 20 20 76 6f 69 64 20 28  ewKey);.  void (
41920 2a 78 54 72 75 6e 63 61 74 65 29 28 73 71 6c 69  *xTruncate)(sqli
41930 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73  te3_pcache*, uns
41940 69 67 6e 65 64 20 69 4c 69 6d 69 74 29 3b 0a 20  igned iLimit);. 
41950 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79   void (*xDestroy
41960 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  )(sqlite3_pcache
41970 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  *);.};../*.** CA
41980 50 49 33 52 45 46 3a 20 4f 6e 6c 69 6e 65 20 42  PI3REF: Online B
41990 61 63 6b 75 70 20 4f 62 6a 65 63 74 0a 2a 2a 20  ackup Object.** 
419a0 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
419b0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62  ** The sqlite3_b
419c0 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 72 65 63  ackup object rec
419d0 6f 72 64 73 20 73 74 61 74 65 20 69 6e 66 6f 72  ords state infor
419e0 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 6e 20  mation about an 
419f0 6f 6e 67 6f 69 6e 67 0a 2a 2a 20 6f 6e 6c 69 6e  ongoing.** onlin
41a00 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69  e backup operati
41a10 6f 6e 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  on.  The sqlite3
41a20 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 69  _backup object i
41a30 73 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20  s created by.** 
41a40 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  a call to [sqlit
41a50 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29  e3_backup_init()
41a60 5d 20 61 6e 64 20 69 73 20 64 65 73 74 72 6f 79  ] and is destroy
41a70 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a  ed by a call to.
41a80 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b  ** [sqlite3_back
41a90 75 70 5f 66 69 6e 69 73 68 28 29 5d 2e 0a 2a 2a  up_finish()]..**
41aa0 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20 5b 55  .** See Also: [U
41ab0 73 69 6e 67 20 74 68 65 20 53 51 4c 69 74 65 20  sing the SQLite 
41ac0 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 50  Online Backup AP
41ad0 49 5d 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  I].*/.typedef st
41ae0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 62 61 63  ruct sqlite3_bac
41af0 6b 75 70 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  kup sqlite3_back
41b00 75 70 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  up;../*.** CAPI3
41b10 52 45 46 3a 20 4f 6e 6c 69 6e 65 20 42 61 63 6b  REF: Online Back
41b20 75 70 20 41 50 49 2e 0a 2a 2a 20 45 58 50 45 52  up API..** EXPER
41b30 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68  IMENTAL.**.** Th
41b40 69 73 20 41 50 49 20 69 73 20 75 73 65 64 20 74  is API is used t
41b50 6f 20 6f 76 65 72 77 72 69 74 65 20 74 68 65 20  o overwrite the 
41b60 63 6f 6e 74 65 6e 74 73 20 6f 66 20 6f 6e 65 20  contents of one 
41b70 64 61 74 61 62 61 73 65 20 77 69 74 68 20 74 68  database with th
41b80 61 74 0a 2a 2a 20 6f 66 20 61 6e 6f 74 68 65 72  at.** of another
41b90 2e 20 49 74 20 69 73 20 75 73 65 66 75 6c 20 65  . It is useful e
41ba0 69 74 68 65 72 20 66 6f 72 20 63 72 65 61 74 69  ither for creati
41bb0 6e 67 20 62 61 63 6b 75 70 73 20 6f 66 20 64 61  ng backups of da
41bc0 74 61 62 61 73 65 73 20 6f 72 0a 2a 2a 20 66 6f  tabases or.** fo
41bd0 72 20 63 6f 70 79 69 6e 67 20 69 6e 2d 6d 65 6d  r copying in-mem
41be0 6f 72 79 20 64 61 74 61 62 61 73 65 73 20 74 6f  ory databases to
41bf0 20 6f 72 20 66 72 6f 6d 20 70 65 72 73 69 73 74   or from persist
41c00 65 6e 74 20 66 69 6c 65 73 2e 20 0a 2a 2a 0a 2a  ent files. .**.*
41c10 2a 20 53 65 65 20 41 6c 73 6f 3a 20 5b 55 73 69  * See Also: [Usi
41c20 6e 67 20 74 68 65 20 53 51 4c 69 74 65 20 4f 6e  ng the SQLite On
41c30 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 50 49 5d  line Backup API]
41c40 0a 2a 2a 0a 2a 2a 20 45 78 63 6c 75 73 69 76 65  .**.** Exclusive
41c50 20 61 63 63 65 73 73 20 69 73 20 72 65 71 75 69   access is requi
41c60 72 65 64 20 74 6f 20 74 68 65 20 64 65 73 74 69  red to the desti
41c70 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20  nation database 
41c80 66 6f 72 20 74 68 65 20 0a 2a 2a 20 64 75 72 61  for the .** dura
41c90 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f 70 65 72  tion of the oper
41ca0 61 74 69 6f 6e 2e 20 48 6f 77 65 76 65 72 20 74  ation. However t
41cb0 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
41cc0 73 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 72 65  se is only.** re
41cd0 61 64 2d 6c 6f 63 6b 65 64 20 77 68 69 6c 65 20  ad-locked while 
41ce0 69 74 20 69 73 20 61 63 74 75 61 6c 6c 79 20 62  it is actually b
41cf0 65 69 6e 67 20 72 65 61 64 2c 20 69 74 20 69 73  eing read, it is
41d00 20 6e 6f 74 20 6c 6f 63 6b 65 64 0a 2a 2a 20 63   not locked.** c
41d10 6f 6e 74 69 6e 75 6f 75 73 6c 79 20 66 6f 72 20  ontinuously for 
41d20 74 68 65 20 65 6e 74 69 72 65 20 6f 70 65 72 61  the entire opera
41d30 74 69 6f 6e 2e 20 54 68 75 73 2c 20 74 68 65 20  tion. Thus, the 
41d40 62 61 63 6b 75 70 20 6d 61 79 20 62 65 0a 2a 2a  backup may be.**
41d50 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 61 20   performed on a 
41d60 6c 69 76 65 20 64 61 74 61 62 61 73 65 20 77 69  live database wi
41d70 74 68 6f 75 74 20 70 72 65 76 65 6e 74 69 6e 67  thout preventing
41d80 20 6f 74 68 65 72 20 75 73 65 72 73 20 66 72 6f   other users fro
41d90 6d 0a 2a 2a 20 77 72 69 74 69 6e 67 20 74 6f 20  m.** writing to 
41da0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 6f 72  the database for
41db0 20 61 6e 20 65 78 74 65 6e 64 65 64 20 70 65 72   an extended per
41dc0 69 6f 64 20 6f 66 20 74 69 6d 65 2e 0a 2a 2a 20  iod of time..** 
41dd0 0a 2a 2a 20 54 6f 20 70 65 72 66 6f 72 6d 20 61  .** To perform a
41de0 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f   backup operatio
41df0 6e 3a 20 0a 2a 2a 20 20 20 3c 6f 6c 3e 0a 2a 2a  n: .**   <ol>.**
41e00 20 20 20 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69       <li><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 20 69 73 20 63 61 6c 6c 65 64 20  )</b> is called 
41e30 6f 6e 63 65 20 74 6f 20 69 6e 69 74 69 61 6c 69  once to initiali
41e40 7a 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  ze the.**       
41e50 20 20 62 61 63 6b 75 70 2c 20 0a 2a 2a 20 20 20    backup, .**   
41e60 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 33    <li><b>sqlite3
41e70 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 3c 2f  _backup_step()</
41e80 62 3e 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 65  b> is called one
41e90 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73 20 74   or more times t
41ea0 6f 20 74 72 61 6e 73 66 65 72 20 0a 2a 2a 20 20  o transfer .**  
41eb0 20 20 20 20 20 20 20 74 68 65 20 64 61 74 61 20         the data 
41ec0 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 20  between the two 
41ed0 64 61 74 61 62 61 73 65 73 2c 20 61 6e 64 20 66  databases, and f
41ee0 69 6e 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 3c 6c  inally.**     <l
41ef0 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63  i><b>sqlite3_bac
41f00 6b 75 70 5f 66 69 6e 69 73 68 28 29 3c 2f 62 3e  kup_finish()</b>
41f10 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65   is called to re
41f20 6c 65 61 73 65 20 61 6c 6c 20 72 65 73 6f 75 72  lease all resour
41f30 63 65 73 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  ces .**         
41f40 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
41f50 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61  the backup opera
41f60 74 69 6f 6e 2e 20 0a 2a 2a 20 20 20 3c 2f 6f 6c  tion. .**   </ol
41f70 3e 0a 2a 2a 20 54 68 65 72 65 20 73 68 6f 75 6c  >.** There shoul
41f80 64 20 62 65 20 65 78 61 63 74 6c 79 20 6f 6e 65  d be exactly one
41f90 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
41fa0 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29  _backup_finish()
41fb0 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 73 75 63   for each.** suc
41fc0 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20  cessful call to 
41fd0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69  sqlite3_backup_i
41fe0 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e  nit()..**.** <b>
41ff0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69  sqlite3_backup_i
42000 6e 69 74 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20  nit()</b>.**.** 
42010 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 61 72  The first two ar
42020 67 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74  guments passed t
42030 6f 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  o [sqlite3_backu
42040 70 5f 69 6e 69 74 28 29 5d 20 61 72 65 20 74 68  p_init()] are th
42050 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 68 61  e database.** ha
42060 6e 64 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20  ndle associated 
42070 77 69 74 68 20 74 68 65 20 64 65 73 74 69 6e 61  with the destina
42080 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 61 6e  tion database an
42090 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  d the database n
420a0 61 6d 65 20 0a 2a 2a 20 75 73 65 64 20 74 6f 20  ame .** used to 
420b0 61 74 74 61 63 68 20 74 68 65 20 64 65 73 74 69  attach the desti
420c0 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20  nation database 
420d0 74 6f 20 74 68 65 20 68 61 6e 64 6c 65 2e 20 54  to the handle. T
420e0 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  he database name
420f0 0a 2a 2a 20 69 73 20 22 6d 61 69 6e 22 20 66 6f  .** is "main" fo
42100 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  r the main datab
42110 61 73 65 2c 20 22 74 65 6d 70 22 20 66 6f 72 20  ase, "temp" for 
42120 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61  the temporary da
42130 74 61 62 61 73 65 2c 20 6f 72 0a 2a 2a 20 74 68  tabase, or.** th
42140 65 20 6e 61 6d 65 20 73 70 65 63 69 66 69 65 64  e name specified
42150 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   as part of the 
42160 5b 41 54 54 41 43 48 5d 20 73 74 61 74 65 6d 65  [ATTACH] stateme
42170 6e 74 20 69 66 20 74 68 65 20 64 65 73 74 69 6e  nt if the destin
42180 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 61 6e 20 61  ation is.** an a
42190 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
421a0 2e 20 54 68 65 20 74 68 69 72 64 20 61 6e 64 20  . The third and 
421b0 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 73  fourth arguments
421c0 20 70 61 73 73 65 64 20 74 6f 20 0a 2a 2a 20 73   passed to .** s
421d0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e  qlite3_backup_in
421e0 69 74 28 29 20 69 64 65 6e 74 69 66 79 20 74 68  it() identify th
421f0 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
42200 65 63 74 69 6f 6e 5d 0a 2a 2a 20 61 6e 64 20 64  ection].** and d
42210 61 74 61 62 61 73 65 20 6e 61 6d 65 20 75 73 65  atabase name use
42220 64 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74  d.** to access t
42230 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
42240 73 65 2e 20 54 68 65 20 76 61 6c 75 65 73 20 70  se. The values p
42250 61 73 73 65 64 20 66 6f 72 20 74 68 65 20 73 6f  assed for the so
42260 75 72 63 65 20 61 6e 64 20 0a 2a 2a 20 64 65 73  urce and .** des
42270 74 69 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61  tination [databa
42280 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70  se connection] p
42290 61 72 61 6d 65 74 65 72 73 20 6d 75 73 74 20 6e  arameters must n
422a0 6f 74 20 62 65 20 74 68 65 20 73 61 6d 65 2e 0a  ot be the same..
422b0 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f  **.** If an erro
422c0 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20  r occurs within 
422d0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69  sqlite3_backup_i
422e0 6e 69 74 28 29 2c 20 74 68 65 6e 20 4e 55 4c 4c  nit(), then NULL
422f0 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20   is returned.** 
42300 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  and an error cod
42310 65 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73  e and error mess
42320 61 67 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  age written into
42330 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
42340 6f 6e 6e 65 63 74 69 6f 6e 5d 20 0a 2a 2a 20 70  onnection] .** p
42350 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72  assed as the fir
42360 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 65  st argument. The
42370 79 20 6d 61 79 20 62 65 20 72 65 74 72 69 65 76  y may be retriev
42380 65 64 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  ed using the.** 
42390 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65  [sqlite3_errcode
423a0 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 65 72  ()], [sqlite3_er
423b0 72 6d 73 67 28 29 5d 2c 20 61 6e 64 20 5b 73 71  rmsg()], and [sq
423c0 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29  lite3_errmsg16()
423d0 5d 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20  ] functions..** 
423e0 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 73 75  Otherwise, if su
423f0 63 63 65 73 73 66 75 6c 2c 20 61 20 70 6f 69 6e  ccessful, a poin
42400 74 65 72 20 74 6f 20 61 6e 20 5b 73 71 6c 69 74  ter to an [sqlit
42410 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63  e3_backup] objec
42420 74 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64  t is.** returned
42430 2e 20 54 68 69 73 20 70 6f 69 6e 74 65 72 20 6d  . This pointer m
42440 61 79 20 62 65 20 75 73 65 64 20 77 69 74 68 20  ay be used with 
42450 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  the sqlite3_back
42460 75 70 5f 73 74 65 70 28 29 20 61 6e 64 0a 2a 2a  up_step() and.**
42470 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
42480 66 69 6e 69 73 68 28 29 20 66 75 6e 63 74 69 6f  finish() functio
42490 6e 73 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68  ns to perform th
424a0 65 20 73 70 65 63 69 66 69 65 64 20 62 61 63 6b  e specified back
424b0 75 70 20 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e  up .** operation
424c0 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74  ..**.** <b>sqlit
424d0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
424e0 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74  </b>.**.** Funct
424f0 69 6f 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61 63  ion [sqlite3_bac
42500 6b 75 70 5f 73 74 65 70 28 29 5d 20 69 73 20 75  kup_step()] is u
42510 73 65 64 20 74 6f 20 63 6f 70 79 20 75 70 20 74  sed to copy up t
42520 6f 20 6e 50 61 67 65 20 70 61 67 65 73 20 62 65  o nPage pages be
42530 74 77 65 65 6e 20 0a 2a 2a 20 74 68 65 20 73 6f  tween .** the so
42540 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61  urce and destina
42550 74 69 6f 6e 20 64 61 74 61 62 61 73 65 73 2c 20  tion databases, 
42560 77 68 65 72 65 20 6e 50 61 67 65 20 69 73 20 74  where nPage is t
42570 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
42580 0a 2a 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  .** second param
42590 65 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 73  eter passed to s
425a0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
425b0 65 70 28 29 2e 20 49 66 20 6e 50 61 67 65 20 69  ep(). If nPage i
425c0 73 20 61 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20  s a negative.** 
425d0 76 61 6c 75 65 2c 20 61 6c 6c 20 72 65 6d 61 69  value, all remai
425e0 6e 69 6e 67 20 73 6f 75 72 63 65 20 70 61 67 65  ning source page
425f0 73 20 61 72 65 20 63 6f 70 69 65 64 2e 20 49 66  s are copied. If
42600 20 74 68 65 20 72 65 71 75 69 72 65 64 20 70 61   the required pa
42610 67 65 73 20 61 72 65 20 0a 2a 2a 20 73 75 63 63  ges are .** succ
42620 65 73 66 75 6c 6c 79 20 63 6f 70 69 65 64 2c 20  esfully copied, 
42630 62 75 74 20 74 68 65 72 65 20 61 72 65 20 73 74  but there are st
42640 69 6c 6c 20 6d 6f 72 65 20 70 61 67 65 73 20 74  ill more pages t
42650 6f 20 63 6f 70 79 20 62 65 66 6f 72 65 20 74 68  o copy before th
42660 65 20 0a 2a 2a 20 62 61 63 6b 75 70 20 69 73 20  e .** backup is 
42670 63 6f 6d 70 6c 65 74 65 2c 20 69 74 20 72 65 74  complete, it ret
42680 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  urns [SQLITE_OK]
42690 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63  . If no error oc
426a0 63 75 72 65 64 20 61 6e 64 20 74 68 65 72 65 20  cured and there 
426b0 0a 2a 2a 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20  .** are no more 
426c0 70 61 67 65 73 20 74 6f 20 63 6f 70 79 2c 20 74  pages to copy, t
426d0 68 65 6e 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45  hen [SQLITE_DONE
426e0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  ] is returned. I
426f0 66 20 61 6e 20 65 72 72 6f 72 20 0a 2a 2a 20 6f  f an error .** o
42700 63 63 75 72 73 2c 20 74 68 65 6e 20 61 6e 20 53  ccurs, then an S
42710 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
42720 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 41 73   is returned. As
42730 20 77 65 6c 6c 20 61 73 20 5b 53 51 4c 49 54 45   well as [SQLITE
42740 5f 4f 4b 5d 20 61 6e 64 0a 2a 2a 20 5b 53 51 4c  _OK] and.** [SQL
42750 49 54 45 5f 44 4f 4e 45 5d 2c 20 61 20 63 61 6c  ITE_DONE], a cal
42760 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63  l to sqlite3_bac
42770 6b 75 70 5f 73 74 65 70 28 29 20 6d 61 79 20 72  kup_step() may r
42780 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 52 45  eturn [SQLITE_RE
42790 41 44 4f 4e 4c 59 5d 2c 0a 2a 2a 20 5b 53 51 4c  ADONLY],.** [SQL
427a0 49 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c  ITE_NOMEM], [SQL
427b0 49 54 45 5f 42 55 53 59 5d 2c 20 5b 53 51 4c 49  ITE_BUSY], [SQLI
427c0 54 45 5f 4c 4f 43 4b 45 44 5d 2c 20 6f 72 20 61  TE_LOCKED], or a
427d0 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45  n.** [SQLITE_IOE
427e0 52 52 5f 41 43 43 45 53 53 20 7c 20 53 51 4c 49  RR_ACCESS | SQLI
427f0 54 45 5f 49 4f 45 52 52 5f 58 58 58 5d 20 65 78  TE_IOERR_XXX] ex
42800 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64  tended error cod
42810 65 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 77 65 6c 6c  e..**.** As well
42820 20 61 73 20 74 68 65 20 63 61 73 65 20 77 68 65   as the case whe
42830 72 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  re the destinati
42840 6f 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  on database file
42850 20 77 61 73 20 6f 70 65 6e 65 64 20 66 6f 72 0a   was opened for.
42860 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 20 61 63 63  ** read-only acc
42870 65 73 73 2c 20 73 71 6c 69 74 65 33 5f 62 61 63  ess, sqlite3_bac
42880 6b 75 70 5f 73 74 65 70 28 29 20 6d 61 79 20 72  kup_step() may r
42890 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 52 45  eturn [SQLITE_RE
428a0 41 44 4f 4e 4c 59 5d 20 69 66 0a 2a 2a 20 74 68  ADONLY] if.** th
428b0 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73  e destination is
428c0 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
428d0 74 61 62 61 73 65 20 77 69 74 68 20 61 20 64 69  tabase with a di
428e0 66 66 65 72 65 6e 74 20 70 61 67 65 20 73 69 7a  fferent page siz
428f0 65 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 73 6f  e.** from the so
42900 75 72 63 65 20 64 61 74 61 62 61 73 65 2e 0a 2a  urce database..*
42910 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f  *.** If sqlite3_
42920 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61  backup_step() ca
42930 6e 6e 6f 74 20 6f 62 74 61 69 6e 20 61 20 72 65  nnot obtain a re
42940 71 75 69 72 65 64 20 66 69 6c 65 2d 73 79 73 74  quired file-syst
42950 65 6d 20 6c 6f 63 6b 2c 20 74 68 65 6e 0a 2a 2a  em lock, then.**
42960 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 75   the [sqlite3_bu
42970 73 79 5f 68 61 6e 64 6c 65 72 20 7c 20 62 75 73  sy_handler | bus
42980 79 2d 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 69  y-handler functi
42990 6f 6e 5d 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65  on].** is invoke
429a0 64 20 28 69 66 20 6f 6e 65 20 69 73 20 73 70 65  d (if one is spe
429b0 63 69 66 69 65 64 29 2e 20 49 66 20 74 68 65 20  cified). If the 
429c0 0a 2a 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72  .** busy-handler
429d0 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
429e0 6f 20 62 65 66 6f 72 65 20 74 68 65 20 6c 6f 63  o before the loc
429f0 6b 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20  k is available, 
42a00 74 68 65 6e 20 0a 2a 2a 20 5b 53 51 4c 49 54 45  then .** [SQLITE
42a10 5f 42 55 53 59 5d 20 69 73 20 72 65 74 75 72 6e  _BUSY] is return
42a20 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  ed to the caller
42a30 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
42a40 68 65 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71  he call to.** sq
42a50 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
42a60 70 28 29 20 63 61 6e 20 62 65 20 72 65 74 72 69  p() can be retri
42a70 65 64 20 6c 61 74 65 72 2e 20 49 66 20 74 68 65  ed later. If the
42a80 20 73 6f 75 72 63 65 0a 2a 2a 20 5b 64 61 74 61   source.** [data
42a90 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
42aa0 0a 2a 2a 20 69 73 20 62 65 69 6e 67 20 75 73 65  .** is being use
42ab0 64 20 74 6f 20 77 72 69 74 65 20 74 6f 20 74 68  d to write to th
42ac0 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
42ad0 65 20 77 68 65 6e 20 73 71 6c 69 74 65 33 5f 62  e when sqlite3_b
42ae0 61 63 6b 75 70 5f 73 74 65 70 28 29 0a 2a 2a 20  ackup_step().** 
42af0 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20  is called, then 
42b00 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 20  [SQLITE_LOCKED] 
42b10 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65  is returned imme
42b20 64 69 61 74 65 6c 79 2e 20 41 67 61 69 6e 2c 20  diately. Again, 
42b30 69 6e 20 74 68 69 73 0a 2a 2a 20 63 61 73 65 20  in this.** case 
42b40 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  the call to sqli
42b50 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28  te3_backup_step(
42b60 29 20 63 61 6e 20 62 65 20 72 65 74 72 69 65 64  ) can be retried
42b70 20 6c 61 74 65 72 20 6f 6e 2e 20 49 66 0a 2a 2a   later on. If.**
42b80 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41   [SQLITE_IOERR_A
42b90 43 43 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49  CCESS | SQLITE_I
42ba0 4f 45 52 52 5f 58 58 58 5d 2c 20 5b 53 51 4c 49  OERR_XXX], [SQLI
42bb0 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 6f 72 0a 2a 2a  TE_NOMEM], or.**
42bc0 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c   [SQLITE_READONL
42bd0 59 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20  Y] is returned, 
42be0 74 68 65 6e 20 0a 2a 2a 20 74 68 65 72 65 20 69  then .** there i
42bf0 73 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 72 65  s no point in re
42c00 74 72 79 69 6e 67 20 74 68 65 20 63 61 6c 6c 20  trying the call 
42c10 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
42c20 70 5f 73 74 65 70 28 29 2e 20 54 68 65 73 65 20  p_step(). These 
42c30 0a 2a 2a 20 65 72 72 6f 72 73 20 61 72 65 20 63  .** errors are c
42c40 6f 6e 73 69 64 65 72 65 64 20 66 61 74 61 6c 2e  onsidered fatal.
42c50 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74   At this point t
42c60 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d  he application m
42c70 75 73 74 20 61 63 63 65 70 74 20 0a 2a 2a 20 74  ust accept .** t
42c80 68 61 74 20 74 68 65 20 62 61 63 6b 75 70 20 6f  hat the backup o
42c90 70 65 72 61 74 69 6f 6e 20 68 61 73 20 66 61 69  peration has fai
42ca0 6c 65 64 20 61 6e 64 20 70 61 73 73 20 74 68 65  led and pass the
42cb0 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f   backup operatio
42cc0 6e 20 68 61 6e 64 6c 65 20 0a 2a 2a 20 74 6f 20  n handle .** to 
42cd0 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  the sqlite3_back
42ce0 75 70 5f 66 69 6e 69 73 68 28 29 20 74 6f 20 72  up_finish() to r
42cf0 65 6c 65 61 73 65 20 61 73 73 6f 63 69 61 74 65  elease associate
42d00 64 20 72 65 73 6f 75 72 63 65 73 2e 0a 2a 2a 0a  d resources..**.
42d10 2a 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20 74 68 65  ** Following the
42d20 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 73   first call to s
42d30 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
42d40 65 70 28 29 2c 20 61 6e 20 65 78 63 6c 75 73 69  ep(), an exclusi
42d50 76 65 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f 62  ve lock is.** ob
42d60 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 65  tained on the de
42d70 73 74 69 6e 61 74 69 6f 6e 20 66 69 6c 65 2e 20  stination file. 
42d80 49 74 20 69 73 20 6e 6f 74 20 72 65 6c 65 61 73  It is not releas
42d90 65 64 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20  ed until either 
42da0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  .** sqlite3_back
42db0 75 70 5f 66 69 6e 69 73 68 28 29 20 69 73 20 63  up_finish() is c
42dc0 61 6c 6c 65 64 20 6f 72 20 74 68 65 20 62 61 63  alled or the bac
42dd0 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73  kup operation is
42de0 20 63 6f 6d 70 6c 65 74 65 20 0a 2a 2a 20 61 6e   complete .** an
42df0 64 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  d sqlite3_backup
42e00 5f 73 74 65 70 28 29 20 72 65 74 75 72 6e 73 20  _step() returns 
42e10 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20 41  [SQLITE_DONE]. A
42e20 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 65 61 63  dditionally, eac
42e30 68 20 74 69 6d 65 20 0a 2a 2a 20 61 20 63 61 6c  h time .** a cal
42e40 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63  l to sqlite3_bac
42e50 6b 75 70 5f 73 74 65 70 28 29 20 69 73 20 6d 61  kup_step() is ma
42e60 64 65 20 61 20 5b 73 68 61 72 65 64 20 6c 6f 63  de a [shared loc
42e70 6b 5d 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f  k] is obtained o
42e80 6e 0a 2a 2a 20 74 68 65 20 73 6f 75 72 63 65 20  n.** the source 
42e90 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54  database file. T
42ea0 68 69 73 20 6c 6f 63 6b 20 69 73 20 72 65 6c 65  his lock is rele
42eb0 61 73 65 64 20 62 65 66 6f 72 65 20 74 68 65 0a  ased before the.
42ec0 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ** sqlite3_backu
42ed0 70 5f 73 74 65 70 28 29 20 63 61 6c 6c 20 72 65  p_step() call re
42ee0 74 75 72 6e 73 2e 20 42 65 63 61 75 73 65 20 74  turns. Because t
42ef0 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
42f00 73 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 6c 6f 63  se is not.** loc
42f10 6b 65 64 20 62 65 74 77 65 65 6e 20 63 61 6c 6c  ked between call
42f20 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63  s to sqlite3_bac
42f30 6b 75 70 5f 73 74 65 70 28 29 2c 20 69 74 20 6d  kup_step(), it m
42f40 61 79 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6d  ay be modified m
42f50 69 64 2d 77 61 79 0a 2a 2a 20 74 68 72 6f 75 67  id-way.** throug
42f60 68 20 74 68 65 20 62 61 63 6b 75 70 20 70 72 6f  h the backup pro
42f70 63 65 64 75 72 65 2e 20 49 66 20 74 68 65 20 73  cedure. If the s
42f80 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 69  ource database i
42f90 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e  s modified by an
42fa0 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 70 72 6f  .** external pro
42fb0 63 65 73 73 20 6f 72 20 76 69 61 20 61 20 64 61  cess or via a da
42fc0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
42fd0 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74 68 65  n other than the
42fe0 20 6f 6e 65 20 62 65 69 6e 67 0a 2a 2a 20 75 73   one being.** us
42ff0 65 64 20 62 79 20 74 68 65 20 62 61 63 6b 75 70  ed by the backup
43000 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e   operation, then
43010 20 74 68 65 20 62 61 63 6b 75 70 20 77 69 6c 6c   the backup will
43020 20 62 65 20 74 72 61 6e 73 70 61 72 65 6e 74 6c   be transparentl
43030 79 0a 2a 2a 20 72 65 73 74 61 72 74 65 64 20 62  y.** restarted b
43040 79 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20  y the next call 
43050 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
43060 70 5f 73 74 65 70 28 29 2e 20 49 66 20 74 68 65  p_step(). If the
43070 20 73 6f 75 72 63 65 20 0a 2a 2a 20 64 61 74 61   source .** data
43080 62 61 73 65 20 69 73 20 6d 6f 64 69 66 69 65 64  base is modified
43090 20 62 79 20 74 68 65 20 75 73 69 6e 67 20 74 68   by the using th
430a0 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
430b0 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 69 73  connection as is
430c0 20 75 73 65 64 0a 2a 2a 20 62 79 20 74 68 65 20   used.** by the 
430d0 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
430e0 2c 20 74 68 65 6e 20 74 68 65 20 62 61 63 6b 75  , then the backu
430f0 70 20 64 61 74 61 62 61 73 65 20 69 73 20 74 72  p database is tr
43100 61 6e 73 70 61 72 65 6e 74 6c 79 20 0a 2a 2a 20  ansparently .** 
43110 75 70 64 61 74 65 64 20 61 74 20 74 68 65 20 73  updated at the s
43120 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20  ame time..**.** 
43130 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  <b>sqlite3_backu
43140 70 5f 66 69 6e 69 73 68 28 29 3c 2f 62 3e 0a 2a  p_finish()</b>.*
43150 2a 0a 2a 2a 20 4f 6e 63 65 20 73 71 6c 69 74 65  *.** Once sqlite
43160 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20  3_backup_step() 
43170 68 61 73 20 72 65 74 75 72 6e 65 64 20 5b 53 51  has returned [SQ
43180 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 6f 72 20 77  LITE_DONE], or w
43190 68 65 6e 20 74 68 65 20 0a 2a 2a 20 61 70 70 6c  hen the .** appl
431a0 69 63 61 74 69 6f 6e 20 77 69 73 68 65 73 20 74  ication wishes t
431b0 6f 20 61 62 61 6e 64 6f 6e 20 74 68 65 20 62 61  o abandon the ba
431c0 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20  ckup operation, 
431d0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63  the [sqlite3_bac
431e0 6b 75 70 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 73  kup].** object s
431f0 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20  hould be passed 
43200 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
43210 70 5f 66 69 6e 69 73 68 28 29 2e 20 54 68 69 73  p_finish(). This
43220 20 72 65 6c 65 61 73 65 73 20 61 6c 6c 0a 2a 2a   releases all.**
43230 20 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63   resources assoc
43240 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 62  iated with the b
43250 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e  ackup operation.
43260 20 49 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   If sqlite3_back
43270 75 70 5f 73 74 65 70 28 29 0a 2a 2a 20 68 61 73  up_step().** has
43280 20 6e 6f 74 20 79 65 74 20 72 65 74 75 72 6e 65   not yet returne
43290 64 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c  d [SQLITE_DONE],
432a0 20 74 68 65 6e 20 61 6e 79 20 61 63 74 69 76 65   then any active
432b0 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
432c0 6f 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 65 73  on on the.** des
432d0 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73  tination databas
432e0 65 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b  e is rolled back
432f0 2e 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62  . The [sqlite3_b
43300 61 63 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 73  ackup] object is
43310 20 69 6e 76 61 6c 69 64 0a 2a 2a 20 61 6e 64 20   invalid.** and 
43320 6d 61 79 20 6e 6f 74 20 62 65 20 75 73 65 64 20  may not be used 
43330 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c 6c  following a call
43340 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   to sqlite3_back
43350 75 70 5f 66 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a  up_finish()..**.
43360 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74  ** The value ret
43370 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33  urned by sqlite3
43380 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 20 69  _backup_finish i
43390 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 66  s [SQLITE_OK] if
433a0 20 6e 6f 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63   no error.** occ
433b0 75 72 72 65 64 2c 20 72 65 67 61 72 64 6c 65 73  urred, regardles
433c0 73 20 6f 72 20 77 68 65 74 68 65 72 20 6f 72 20  s or whether or 
433d0 6e 6f 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  not sqlite3_back
433e0 75 70 5f 73 74 65 70 28 29 20 77 61 73 20 63 61  up_step() was ca
433f0 6c 6c 65 64 0a 2a 2a 20 61 20 73 75 66 66 69 63  lled.** a suffic
43400 69 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 74  ient number of t
43410 69 6d 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 65  imes to complete
43420 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72   the backup oper
43430 61 74 69 6f 6e 2e 20 4f 72 2c 20 69 66 0a 2a 2a  ation. Or, if.**
43440 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72   an out-of-memor
43450 79 20 63 6f 6e 64 69 74 69 6f 6e 20 6f 72 20 49  y condition or I
43460 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20  O error occured 
43470 64 75 72 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f  during a call to
43480 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  .** sqlite3_back
43490 75 70 5f 73 74 65 70 28 29 20 74 68 65 6e 20 5b  up_step() then [
434a0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 20 6f 72  SQLITE_NOMEM] or
434b0 20 61 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49   an.** [SQLITE_I
434c0 4f 45 52 52 5f 41 43 43 45 53 53 20 7c 20 53 51  OERR_ACCESS | SQ
434d0 4c 49 54 45 5f 49 4f 45 52 52 5f 58 58 58 5d 20  LITE_IOERR_XXX] 
434e0 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 73  error code.** is
434f0 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68   returned. In th
43500 69 73 20 63 61 73 65 20 74 68 65 20 65 72 72 6f  is case the erro
43510 72 20 63 6f 64 65 20 61 6e 64 20 61 6e 20 65 72  r code and an er
43520 72 6f 72 20 6d 65 73 73 61 67 65 20 61 72 65 0a  ror message are.
43530 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  ** written to th
43540 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b 64  e destination [d
43550 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
43560 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 74  on]..**.** A ret
43570 75 72 6e 20 6f 66 20 5b 53 51 4c 49 54 45 5f 42  urn of [SQLITE_B
43580 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f  USY] or [SQLITE_
43590 4c 4f 43 4b 45 44 5d 20 66 72 6f 6d 20 73 71 6c  LOCKED] from sql
435a0 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
435b0 28 29 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20 70  () is.** not a p
435c0 65 72 6d 61 6e 65 6e 74 20 65 72 72 6f 72 20 61  ermanent error a
435d0 6e 64 20 64 6f 65 73 20 6e 6f 74 20 61 66 66 65  nd does not affe
435e0 63 74 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  ct the return va
435f0 6c 75 65 20 6f 66 0a 2a 2a 20 73 71 6c 69 74 65  lue of.** sqlite
43600 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28  3_backup_finish(
43610 29 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69  )..**.** <b>sqli
43620 74 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69  te3_backup_remai
43630 6e 69 6e 67 28 29 2c 20 73 71 6c 69 74 65 33 5f  ning(), sqlite3_
43640 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74  backup_pagecount
43650 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 45 61 63  ()</b>.**.** Eac
43660 68 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  h call to sqlite
43670 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20  3_backup_step() 
43680 73 65 74 73 20 74 77 6f 20 76 61 6c 75 65 73 20  sets two values 
43690 73 74 6f 72 65 64 20 69 6e 74 65 72 6e 61 6c 6c  stored internall
436a0 79 0a 2a 2a 20 62 79 20 61 6e 20 5b 73 71 6c 69  y.** by an [sqli
436b0 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65  te3_backup] obje
436c0 63 74 2e 20 54 68 65 20 6e 75 6d 62 65 72 20 6f  ct. The number o
436d0 66 20 70 61 67 65 73 20 73 74 69 6c 6c 20 74 6f  f pages still to
436e0 20 62 65 20 62 61 63 6b 65 64 0a 2a 2a 20 75 70   be backed.** up
436f0 2c 20 77 68 69 63 68 20 6d 61 79 20 62 65 20 71  , which may be q
43700 75 65 72 69 65 64 20 62 79 20 73 71 6c 69 74 65  ueried by sqlite
43710 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69  3_backup_remaini
43720 6e 67 28 29 2c 20 61 6e 64 20 74 68 65 20 74 6f  ng(), and the to
43730 74 61 6c 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66  tal.** number of
43740 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 73 6f   pages in the so
43750 75 72 63 65 20 64 61 74 61 62 61 73 65 20 66 69  urce database fi
43760 6c 65 2c 20 77 68 69 63 68 20 6d 61 79 20 62 65  le, which may be
43770 20 71 75 65 72 69 65 64 20 62 79 0a 2a 2a 20 73   queried by.** s
43780 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61  qlite3_backup_pa
43790 67 65 63 6f 75 6e 74 28 29 2e 0a 2a 2a 0a 2a 2a  gecount()..**.**
437a0 20 54 68 65 20 76 61 6c 75 65 73 20 72 65 74 75   The values retu
437b0 72 6e 65 64 20 62 79 20 74 68 65 73 65 20 66 75  rned by these fu
437c0 6e 63 74 69 6f 6e 73 20 61 72 65 20 6f 6e 6c 79  nctions are only
437d0 20 75 70 64 61 74 65 64 20 62 79 0a 2a 2a 20 73   updated by.** s
437e0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
437f0 65 70 28 29 2e 20 49 66 20 74 68 65 20 73 6f 75  ep(). If the sou
43800 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20  rce database is 
43810 6d 6f 64 69 66 69 65 64 20 64 75 72 69 6e 67 20  modified during 
43820 61 20 62 61 63 6b 75 70 0a 2a 2a 20 6f 70 65 72  a backup.** oper
43830 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20  ation, then the 
43840 76 61 6c 75 65 73 20 61 72 65 20 6e 6f 74 20 75  values are not u
43850 70 64 61 74 65 64 20 74 6f 20 61 63 63 6f 75 6e  pdated to accoun
43860 74 20 66 6f 72 20 61 6e 79 20 65 78 74 72 61 0a  t for any extra.
43870 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20 6e 65  ** pages that ne
43880 65 64 20 74 6f 20 62 65 20 75 70 64 61 74 65 64  ed to be updated
43890 20 6f 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20   or the size of 
438a0 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62  the source datab
438b0 61 73 65 20 66 69 6c 65 0a 2a 2a 20 63 68 61 6e  ase file.** chan
438c0 67 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43  ging..**.** <b>C
438d0 6f 6e 63 75 72 72 65 6e 74 20 55 73 61 67 65 20  oncurrent Usage 
438e0 6f 66 20 44 61 74 61 62 61 73 65 20 48 61 6e 64  of Database Hand
438f0 6c 65 73 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 54 68  les</b>.**.** Th
43900 65 20 73 6f 75 72 63 65 20 5b 64 61 74 61 62 61  e source [databa
43910 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6d  se connection] m
43920 61 79 20 62 65 20 75 73 65 64 20 62 79 20 74 68  ay be used by th
43930 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 66 6f  e application fo
43940 72 20 6f 74 68 65 72 0a 2a 2a 20 70 75 72 70 6f  r other.** purpo
43950 73 65 73 20 77 68 69 6c 65 20 61 20 62 61 63 6b  ses while a back
43960 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20  up operation is 
43970 75 6e 64 65 72 77 61 79 20 6f 72 20 62 65 69 6e  underway or bein
43980 67 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a  g initialized..*
43990 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 63  * If SQLite is c
439a0 6f 6d 70 69 6c 65 64 20 61 6e 64 20 63 6f 6e 66  ompiled and conf
439b0 69 67 75 72 65 64 20 74 6f 20 73 75 70 70 6f 72  igured to suppor
439c0 74 20 74 68 72 65 61 64 73 61 66 65 20 64 61 74  t threadsafe dat
439d0 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74  abase.** connect
439e0 69 6f 6e 73 2c 20 74 68 65 6e 20 74 68 65 20 73  ions, then the s
439f0 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 63  ource database c
43a00 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 79 20 62 65  onnection may be
43a10 20 75 73 65 64 20 63 6f 6e 63 75 72 72 65 6e 74   used concurrent
43a20 6c 79 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69  ly.** from withi
43a30 6e 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 2e  n other threads.
43a40 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20  .**.** However, 
43a50 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
43a60 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65 20 74  must guarantee t
43a70 68 61 74 20 74 68 65 20 64 65 73 74 69 6e 61 74  hat the destinat
43a80 69 6f 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ion database.** 
43a90 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c  connection handl
43aa0 65 20 69 73 20 6e 6f 74 20 70 61 73 73 65 64 20  e is not passed 
43ab0 74 6f 20 61 6e 79 20 6f 74 68 65 72 20 41 50 49  to any other API
43ac0 20 28 62 79 20 61 6e 79 20 74 68 72 65 61 64 29   (by any thread)
43ad0 20 61 66 74 65 72 20 0a 2a 2a 20 73 71 6c 69 74   after .** sqlit
43ae0 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29  e3_backup_init()
43af0 20 69 73 20 63 61 6c 6c 65 64 20 61 6e 64 20 62   is called and b
43b00 65 66 6f 72 65 20 74 68 65 20 63 6f 72 72 65 73  efore the corres
43b10 70 6f 6e 64 69 6e 67 20 63 61 6c 6c 20 74 6f 0a  ponding call to.
43b20 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ** sqlite3_backu
43b30 70 5f 66 69 6e 69 73 68 28 29 2e 20 55 6e 66 6f  p_finish(). Unfo
43b40 72 74 75 6e 61 74 65 6c 79 20 53 51 4c 69 74 65  rtunately SQLite
43b50 20 64 6f 65 73 20 6e 6f 74 20 63 75 72 72 65 6e   does not curren
43b60 74 6c 79 20 63 68 65 63 6b 0a 2a 2a 20 66 6f 72  tly check.** for
43b70 20 74 68 69 73 2c 20 69 66 20 74 68 65 20 61 70   this, if the ap
43b80 70 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 75  plication does u
43b90 73 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  se the destinati
43ba0 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  on [database con
43bb0 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 66 6f 72 20  nection].** for 
43bc0 73 6f 6d 65 20 6f 74 68 65 72 20 70 75 72 70 6f  some other purpo
43bd0 73 65 20 64 75 72 69 6e 67 20 61 20 62 61 63 6b  se during a back
43be0 75 70 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68  up operation, th
43bf0 69 6e 67 73 20 6d 61 79 20 61 70 70 65 61 72 20  ings may appear 
43c00 74 6f 0a 2a 2a 20 77 6f 72 6b 20 63 6f 72 72 65  to.** work corre
43c10 63 74 6c 79 20 62 75 74 20 69 6e 20 66 61 63 74  ctly but in fact
43c20 20 62 65 20 73 75 62 74 6c 79 20 6d 61 6c 66 75   be subtly malfu
43c30 6e 63 74 69 6f 6e 69 6e 67 2e 20 20 55 73 65 20  nctioning.  Use 
43c40 6f 66 20 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e  of the.** destin
43c50 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20 63  ation database c
43c60 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20  onnection while 
43c70 61 20 62 61 63 6b 75 70 20 69 73 20 69 6e 20 70  a backup is in p
43c80 72 6f 67 72 65 73 73 20 6d 69 67 68 74 0a 2a 2a  rogress might.**
43c90 20 61 6c 73 6f 20 63 61 75 73 65 20 61 20 6d 75   also cause a mu
43ca0 74 65 78 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a  tex deadlock..**
43cb0 0a 2a 2a 20 46 75 72 74 68 65 72 6d 6f 72 65 2c  .** Furthermore,
43cc0 20 69 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 5b   if running in [
43cd0 73 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64  shared cache mod
43ce0 65 5d 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74  e], the applicat
43cf0 69 6f 6e 20 6d 75 73 74 0a 2a 2a 20 67 75 61 72  ion must.** guar
43d00 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 73  antee that the s
43d10 68 61 72 65 64 20 63 61 63 68 65 20 75 73 65 64  hared cache used
43d20 20 62 79 20 74 68 65 20 64 65 73 74 69 6e 61 74   by the destinat
43d30 69 6f 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ion database.** 
43d40 69 73 20 6e 6f 74 20 61 63 63 65 73 73 65 64 20  is not accessed 
43d50 77 68 69 6c 65 20 74 68 65 20 62 61 63 6b 75 70  while the backup
43d60 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20 49 6e 20   is running. In 
43d70 70 72 61 63 74 69 63 65 20 74 68 69 73 20 6d 65  practice this me
43d80 61 6e 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  ans.** that the 
43d90 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74  application must
43da0 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20   guarantee that 
43db0 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  the file-system 
43dc0 66 69 6c 65 20 62 65 69 6e 67 20 0a 2a 2a 20 62  file being .** b
43dd0 61 63 6b 65 64 20 75 70 20 74 6f 20 69 73 20 6e  acked up to is n
43de0 6f 74 20 61 63 63 65 73 73 65 64 20 62 79 20 61  ot accessed by a
43df0 6e 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69  ny connection wi
43e00 74 68 69 6e 20 74 68 65 20 70 72 6f 63 65 73 73  thin the process
43e10 2c 0a 2a 2a 20 6e 6f 74 20 6a 75 73 74 20 74 68  ,.** not just th
43e20 65 20 73 70 65 63 69 66 69 63 20 63 6f 6e 6e 65  e specific conne
43e30 63 74 69 6f 6e 20 74 68 61 74 20 77 61 73 20 70  ction that was p
43e40 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33  assed to sqlite3
43e50 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2e 0a  _backup_init()..
43e60 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74  **.** The [sqlit
43e70 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a 65 63  e3_backup] objec
43e80 74 20 69 74 73 65 6c 66 20 69 73 20 70 61 72 74  t itself is part
43e90 69 61 6c 6c 79 20 74 68 72 65 61 64 73 61 66 65  ially threadsafe
43ea0 2e 20 4d 75 6c 74 69 70 6c 65 20 0a 2a 2a 20 74  . Multiple .** t
43eb0 68 72 65 61 64 73 20 6d 61 79 20 73 61 66 65 6c  hreads may safel
43ec0 79 20 6d 61 6b 65 20 6d 75 6c 74 69 70 6c 65 20  y make multiple 
43ed0 63 6f 6e 63 75 72 72 65 6e 74 20 63 61 6c 6c 73  concurrent calls
43ee0 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   to sqlite3_back
43ef0 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2a 20 48 6f  up_step()..** Ho
43f00 77 65 76 65 72 2c 20 74 68 65 20 73 71 6c 69 74  wever, the sqlit
43f10 65 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e  e3_backup_remain
43f20 69 6e 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65  ing() and sqlite
43f30 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75  3_backup_pagecou
43f40 6e 74 28 29 0a 2a 2a 20 41 50 49 73 20 61 72 65  nt().** APIs are
43f50 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20 73 70   not strictly sp
43f60 65 61 6b 69 6e 67 20 74 68 72 65 61 64 73 61 66  eaking threadsaf
43f70 65 2e 20 49 66 20 74 68 65 79 20 61 72 65 20 69  e. If they are i
43f80 6e 76 6f 6b 65 64 20 61 74 20 74 68 65 0a 2a 2a  nvoked at the.**
43f90 20 73 61 6d 65 20 74 69 6d 65 20 61 73 20 61 6e   same time as an
43fa0 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20  other thread is 
43fb0 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74 65 33  invoking sqlite3
43fc0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 69  _backup_step() i
43fd0 74 20 69 73 0a 2a 2a 20 70 6f 73 73 69 62 6c 65  t is.** possible
43fe0 20 74 68 61 74 20 74 68 65 79 20 72 65 74 75 72   that they retur
43ff0 6e 20 69 6e 76 61 6c 69 64 20 76 61 6c 75 65 73  n invalid values
44000 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
44010 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
44020 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69  sqlite3_backup_i
44030 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  nit(.  sqlite3 *
44040 70 44 65 73 74 2c 20 20 20 20 20 20 20 20 20 20  pDest,          
44050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
44060 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   Destination dat
44070 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
44080 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
44090 65 73 74 4e 61 6d 65 2c 20 20 20 20 20 20 20 20  estName,        
440a0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74           /* Dest
440b0 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65  ination database
440c0 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 71 6c 69 74   name */.  sqlit
440d0 65 33 20 2a 70 53 6f 75 72 63 65 2c 20 20 20 20  e3 *pSource,    
440e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
440f0 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 61    /* Source data
44100 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  base handle */. 
44110 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 6f   const char *zSo
44120 75 72 63 65 4e 61 6d 65 20 20 20 20 20 20 20 20  urceName        
44130 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63          /* Sourc
44140 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  e database name 
44150 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  */.);.SQLITE_API
44160 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63   int sqlite3_bac
44170 6b 75 70 5f 73 74 65 70 28 73 71 6c 69 74 65 33  kup_step(sqlite3
44180 5f 62 61 63 6b 75 70 20 2a 70 2c 20 69 6e 74 20  _backup *p, int 
44190 6e 50 61 67 65 29 3b 0a 53 51 4c 49 54 45 5f 41  nPage);.SQLITE_A
441a0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
441b0 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 73 71 6c  ackup_finish(sql
441c0 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 3b  ite3_backup *p);
441d0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
441e0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72  sqlite3_backup_r
441f0 65 6d 61 69 6e 69 6e 67 28 73 71 6c 69 74 65 33  emaining(sqlite3
44200 5f 62 61 63 6b 75 70 20 2a 70 29 3b 0a 53 51 4c  _backup *p);.SQL
44210 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
44220 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67 65 63  te3_backup_pagec
44230 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 62 61 63  ount(sqlite3_bac
44240 6b 75 70 20 2a 70 29 3b 0a 0a 2f 2a 0a 2a 2a 20  kup *p);../*.** 
44250 43 41 50 49 33 52 45 46 3a 20 55 6e 6c 6f 63 6b  CAPI3REF: Unlock
44260 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 0a 2a 2a   Notification.**
44270 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
44280 0a 2a 2a 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67  .** When running
44290 20 69 6e 20 73 68 61 72 65 64 2d 63 61 63 68 65   in shared-cache
442a0 20 6d 6f 64 65 2c 20 61 20 64 61 74 61 62 61 73   mode, a databas
442b0 65 20 6f 70 65 72 61 74 69 6f 6e 20 6d 61 79 20  e operation may 
442c0 66 61 69 6c 20 77 69 74 68 0a 2a 2a 20 61 6e 20  fail with.** an 
442d0 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 20  [SQLITE_LOCKED] 
442e0 65 72 72 6f 72 20 69 66 20 74 68 65 20 72 65 71  error if the req
442f0 75 69 72 65 64 20 6c 6f 63 6b 73 20 6f 6e 20 74  uired locks on t
44300 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20  he shared-cache 
44310 6f 72 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c  or.** individual
44320 20 74 61 62 6c 65 73 20 77 69 74 68 69 6e 20 74   tables within t
44330 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20  he shared-cache 
44340 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e  cannot be obtain
44350 65 64 2e 20 53 65 65 0a 2a 2a 20 5b 53 51 4c 69  ed. See.** [SQLi
44360 74 65 20 53 68 61 72 65 64 2d 43 61 63 68 65 20  te Shared-Cache 
44370 4d 6f 64 65 5d 20 66 6f 72 20 61 20 64 65 73 63  Mode] for a desc
44380 72 69 70 74 69 6f 6e 20 6f 66 20 73 68 61 72 65  ription of share
44390 64 2d 63 61 63 68 65 20 6c 6f 63 6b 69 6e 67 2e  d-cache locking.
443a0 20 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 6d 61   .** This API ma
443b0 79 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 67  y be used to reg
443c0 69 73 74 65 72 20 61 20 63 61 6c 6c 62 61 63 6b  ister a callback
443d0 20 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c   that SQLite wil
443e0 6c 20 69 6e 76 6f 6b 65 20 0a 2a 2a 20 77 68 65  l invoke .** whe
443f0 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  n the connection
44400 20 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69   currently holdi
44410 6e 67 20 74 68 65 20 72 65 71 75 69 72 65 64 20  ng the required 
44420 6c 6f 63 6b 20 72 65 6c 69 6e 71 75 69 73 68 65  lock relinquishe
44430 73 20 69 74 2e 0a 2a 2a 20 54 68 69 73 20 41 50  s it..** This AP
44440 49 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61  I is only availa
44450 62 6c 65 20 69 66 20 74 68 65 20 6c 69 62 72 61  ble if the libra
44460 72 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20  ry was compiled 
44470 77 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c  with the.** [SQL
44480 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43  ITE_ENABLE_UNLOC
44490 4b 5f 4e 4f 54 49 46 59 5d 20 43 2d 70 72 65 70  K_NOTIFY] C-prep
444a0 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c 20  rocessor symbol 
444b0 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53  defined..**.** S
444c0 65 65 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20  ee Also: [Using 
444d0 74 68 65 20 53 51 4c 69 74 65 20 55 6e 6c 6f 63  the SQLite Unloc
444e0 6b 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 20 46  k Notification F
444f0 65 61 74 75 72 65 5d 2e 0a 2a 2a 0a 2a 2a 20 53  eature]..**.** S
44500 68 61 72 65 64 2d 63 61 63 68 65 20 6c 6f 63 6b  hared-cache lock
44510 73 20 61 72 65 20 72 65 6c 65 61 73 65 64 20 77  s are released w
44520 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 63  hen a database c
44530 6f 6e 6e 65 63 74 69 6f 6e 20 63 6f 6e 63 6c 75  onnection conclu
44540 64 65 73 0a 2a 2a 20 69 74 73 20 63 75 72 72 65  des.** its curre
44550 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  nt transaction, 
44560 65 69 74 68 65 72 20 62 79 20 63 6f 6d 6d 69 74  either by commit
44570 74 69 6e 67 20 69 74 20 6f 72 20 72 6f 6c 6c 69  ting it or rolli
44580 6e 67 20 69 74 20 62 61 63 6b 2e 20 0a 2a 2a 0a  ng it back. .**.
44590 2a 2a 20 57 68 65 6e 20 61 20 63 6f 6e 6e 65 63  ** When a connec
445a0 74 69 6f 6e 20 28 6b 6e 6f 77 6e 20 61 73 20 74  tion (known as t
445b0 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65  he blocked conne
445c0 63 74 69 6f 6e 29 20 66 61 69 6c 73 20 74 6f 20  ction) fails to 
445d0 6f 62 74 61 69 6e 20 61 0a 2a 2a 20 73 68 61 72  obtain a.** shar
445e0 65 64 2d 63 61 63 68 65 20 6c 6f 63 6b 20 61 6e  ed-cache lock an
445f0 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  d SQLITE_LOCKED 
44600 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74  is returned to t
44610 68 65 20 63 61 6c 6c 65 72 2c 20 74 68 65 0a 2a  he caller, the.*
44620 2a 20 69 64 65 6e 74 69 74 79 20 6f 66 20 74 68  * identity of th
44630 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
44640 63 74 69 6f 6e 20 28 74 68 65 20 62 6c 6f 63 6b  ction (the block
44650 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20  ing connection) 
44660 74 68 61 74 0a 2a 2a 20 68 61 73 20 6c 6f 63 6b  that.** has lock
44670 65 64 20 74 68 65 20 72 65 71 75 69 72 65 64 20  ed the required 
44680 72 65 73 6f 75 72 63 65 20 69 73 20 73 74 6f 72  resource is stor
44690 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 41  ed internally. A
446a0 66 74 65 72 20 61 6e 20 0a 2a 2a 20 61 70 70 6c  fter an .** appl
446b0 69 63 61 74 69 6f 6e 20 72 65 63 65 69 76 65 73  ication receives
446c0 20 61 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45   an SQLITE_LOCKE
446d0 44 20 65 72 72 6f 72 2c 20 69 74 20 6d 61 79 20  D error, it may 
446e0 63 61 6c 6c 20 74 68 65 0a 2a 2a 20 73 71 6c 69  call the.** sqli
446f0 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66  te3_unlock_notif
44700 79 28 29 20 6d 65 74 68 6f 64 20 77 69 74 68 20  y() method with 
44710 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e  the blocked conn
44720 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 61 73  ection handle as
44730 20 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 61   .** the first a
44740 72 67 75 6d 65 6e 74 20 74 6f 20 72 65 67 69 73  rgument to regis
44750 74 65 72 20 66 6f 72 20 61 20 63 61 6c 6c 62 61  ter for a callba
44760 63 6b 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20  ck that will be 
44770 69 6e 76 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 20  invoked.** when 
44780 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e  the blocking con
44790 6e 65 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74  nections current
447a0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
447b0 63 6f 6e 63 6c 75 64 65 64 2e 20 54 68 65 0a 2a  concluded. The.*
447c0 2a 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e  * callback is in
447d0 76 6f 6b 65 64 20 66 72 6f 6d 20 77 69 74 68 69  voked from withi
447e0 6e 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73  n the [sqlite3_s
447f0 74 65 70 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  tep] or [sqlite3
44800 5f 63 6c 6f 73 65 5d 0a 2a 2a 20 63 61 6c 6c 20  _close].** call 
44810 74 68 61 74 20 63 6f 6e 63 6c 75 64 65 73 20 74  that concludes t
44820 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e  he blocking conn
44830 65 63 74 69 6f 6e 73 20 74 72 61 6e 73 61 63 74  ections transact
44840 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71  ion..**.** If sq
44850 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74  lite3_unlock_not
44860 69 66 79 28 29 20 69 73 20 63 61 6c 6c 65 64 20  ify() is called 
44870 69 6e 20 61 20 6d 75 6c 74 69 2d 74 68 72 65 61  in a multi-threa
44880 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 2c  ded application,
44890 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20 63  .** there is a c
448a0 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 62  hance that the b
448b0 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69  locking connecti
448c0 6f 6e 20 77 69 6c 6c 20 68 61 76 65 20 61 6c 72  on will have alr
448d0 65 61 64 79 0a 2a 2a 20 63 6f 6e 63 6c 75 64 65  eady.** conclude
448e0 64 20 69 74 73 20 74 72 61 6e 73 61 63 74 69 6f  d its transactio
448f0 6e 20 62 79 20 74 68 65 20 74 69 6d 65 20 73 71  n by the time sq
44900 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74  lite3_unlock_not
44910 69 66 79 28 29 20 69 73 20 69 6e 76 6f 6b 65 64  ify() is invoked
44920 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 68 61 70  ..** If this hap
44930 70 65 6e 73 2c 20 74 68 65 6e 20 74 68 65 20 73  pens, then the s
44940 70 65 63 69 66 69 65 64 20 63 61 6c 6c 62 61 63  pecified callbac
44950 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 69 6d 6d  k is invoked imm
44960 65 64 69 61 74 65 6c 79 2c 0a 2a 2a 20 66 72 6f  ediately,.** fro
44970 6d 20 77 69 74 68 69 6e 20 74 68 65 20 63 61 6c  m within the cal
44980 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 6e 6c  l to sqlite3_unl
44990 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 2e 0a 2a 2a  ock_notify()..**
449a0 0a 2a 2a 20 49 66 20 74 68 65 20 62 6c 6f 63 6b  .** If the block
449b0 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  ed connection is
449c0 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 6f   attempting to o
449d0 62 74 61 69 6e 20 61 20 77 72 69 74 65 2d 6c 6f  btain a write-lo
449e0 63 6b 20 6f 6e 20 61 0a 2a 2a 20 73 68 61 72 65  ck on a.** share
449f0 64 2d 63 61 63 68 65 20 74 61 62 6c 65 2c 20 61  d-cache table, a
44a00 6e 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  nd more than one
44a10 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f   other connectio
44a20 6e 20 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64  n currently hold
44a30 73 0a 2a 2a 20 61 20 72 65 61 64 2d 6c 6f 63 6b  s.** a read-lock
44a40 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 74 61 62   on the same tab
44a50 6c 65 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20  le, then SQLite 
44a60 61 72 62 69 74 72 61 72 69 6c 79 20 73 65 6c 65  arbitrarily sele
44a70 63 74 73 20 6f 6e 65 20 6f 66 20 0a 2a 2a 20 74  cts one of .** t
44a80 68 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74  he other connect
44a90 69 6f 6e 73 20 74 6f 20 75 73 65 20 61 73 20 74  ions to use as t
44aa0 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e  he blocking conn
44ab0 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  ection..**.** Th
44ac0 65 72 65 20 6d 61 79 20 62 65 20 61 74 20 6d 6f  ere may be at mo
44ad0 73 74 20 6f 6e 65 20 75 6e 6c 6f 63 6b 2d 6e 6f  st one unlock-no
44ae0 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 72 65  tify callback re
44af0 67 69 73 74 65 72 65 64 20 62 79 20 61 20 0a 2a  gistered by a .*
44b00 2a 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63  * blocked connec
44b10 74 69 6f 6e 2e 20 49 66 20 73 71 6c 69 74 65 33  tion. If sqlite3
44b20 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
44b30 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   is called when 
44b40 74 68 65 0a 2a 2a 20 62 6c 6f 63 6b 65 64 20 63  the.** blocked c
44b50 6f 6e 6e 65 63 74 69 6f 6e 20 61 6c 72 65 61 64  onnection alread
44b60 79 20 68 61 73 20 61 20 72 65 67 69 73 74 65 72  y has a register
44b70 65 64 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79  ed unlock-notify
44b80 20 63 61 6c 6c 62 61 63 6b 2c 0a 2a 2a 20 74 68   callback,.** th
44b90 65 6e 20 74 68 65 20 6e 65 77 20 63 61 6c 6c 62  en the new callb
44ba0 61 63 6b 20 72 65 70 6c 61 63 65 73 20 74 68 65  ack replaces the
44bb0 20 6f 6c 64 2e 20 49 66 20 73 71 6c 69 74 65 33   old. If sqlite3
44bc0 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
44bd0 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 69   is.** called wi
44be0 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  th a NULL pointe
44bf0 72 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64 20  r as its second 
44c00 61 72 67 75 6d 65 6e 74 2c 20 74 68 65 6e 20 61  argument, then a
44c10 6e 79 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 75  ny existing.** u
44c20 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c  nlock-notify cal
44c30 6c 62 61 63 6b 20 69 73 20 63 61 6e 63 65 6c 6c  lback is cancell
44c40 65 64 2e 20 54 68 65 20 62 6c 6f 63 6b 65 64 20  ed. The blocked 
44c50 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 0a 2a 2a 20  connections .** 
44c60 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
44c70 6c 6c 62 61 63 6b 20 6d 61 79 20 61 6c 73 6f 20  llback may also 
44c80 62 65 20 63 61 6e 63 65 6c 65 64 20 62 79 20 63  be canceled by c
44c90 6c 6f 73 69 6e 67 20 74 68 65 20 62 6c 6f 63 6b  losing the block
44ca0 65 64 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  ed.** connection
44cb0 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
44cc0 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  close()]..**.** 
44cd0 54 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66  The unlock-notif
44ce0 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f  y callback is no
44cf0 74 20 72 65 65 6e 74 72 61 6e 74 2e 20 49 66 20  t reentrant. If 
44d00 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69  an application i
44d10 6e 76 6f 6b 65 73 0a 2a 2a 20 61 6e 79 20 73 71  nvokes.** any sq
44d20 6c 69 74 65 33 5f 78 78 78 20 41 50 49 20 66 75  lite3_xxx API fu
44d30 6e 63 74 69 6f 6e 73 20 66 72 6f 6d 20 77 69 74  nctions from wit
44d40 68 69 6e 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f  hin an unlock-no
44d50 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 2c 20 61  tify callback, a
44d60 0a 2a 2a 20 63 72 61 73 68 20 6f 72 20 64 65 61  .** crash or dea
44d70 64 6c 6f 63 6b 20 6d 61 79 20 62 65 20 74 68 65  dlock may be the
44d80 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 55   result..**.** U
44d90 6e 6c 65 73 73 20 64 65 61 64 6c 6f 63 6b 20 69  nless deadlock i
44da0 73 20 64 65 74 65 63 74 65 64 20 28 73 65 65 20  s detected (see 
44db0 62 65 6c 6f 77 29 2c 20 73 71 6c 69 74 65 33 5f  below), sqlite3_
44dc0 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20  unlock_notify() 
44dd0 61 6c 77 61 79 73 0a 2a 2a 20 72 65 74 75 72 6e  always.** return
44de0 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a  s SQLITE_OK..**.
44df0 2a 2a 20 3c 62 3e 43 61 6c 6c 62 61 63 6b 20 49  ** <b>Callback I
44e00 6e 76 6f 63 61 74 69 6f 6e 20 44 65 74 61 69 6c  nvocation Detail
44e10 73 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  s</b>.**.** When
44e20 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66   an unlock-notif
44e30 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 72 65  y callback is re
44e40 67 69 73 74 65 72 65 64 2c 20 74 68 65 20 61 70  gistered, the ap
44e50 70 6c 69 63 61 74 69 6f 6e 20 70 72 6f 76 69 64  plication provid
44e60 65 73 20 61 20 0a 2a 2a 20 73 69 6e 67 6c 65 20  es a .** single 
44e70 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72 20 74 68  void* pointer th
44e80 61 74 20 69 73 20 70 61 73 73 65 64 20 74 6f 20  at is passed to 
44e90 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 77 68 65  the callback whe
44ea0 6e 20 69 74 20 69 73 20 69 6e 76 6f 6b 65 64 2e  n it is invoked.
44eb0 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65  .** However, the
44ec0 20 73 69 67 6e 61 74 75 72 65 20 6f 66 20 74 68   signature of th
44ed0 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
44ee0 69 6f 6e 20 61 6c 6c 6f 77 73 20 53 51 4c 69 74  ion allows SQLit
44ef0 65 20 74 6f 20 70 61 73 73 0a 2a 2a 20 69 74 20  e to pass.** it 
44f00 61 6e 20 61 72 72 61 79 20 6f 66 20 76 6f 69 64  an array of void
44f10 2a 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65  * context pointe
44f20 72 73 2e 20 54 68 65 20 66 69 72 73 74 20 61 72  rs. The first ar
44f30 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f  gument passed to
44f40 0a 2a 2a 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f  .** an unlock-no
44f50 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73  tify callback is
44f60 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
44f70 20 61 72 72 61 79 20 6f 66 20 76 6f 69 64 2a 20   array of void* 
44f80 70 6f 69 6e 74 65 72 73 2c 0a 2a 2a 20 61 6e 64  pointers,.** and
44f90 20 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 74   the second is t
44fa0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74  he number of ent
44fb0 72 69 65 73 20 69 6e 20 74 68 65 20 61 72 72 61  ries in the arra
44fc0 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20  y..**.** When a 
44fd0 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74  blocking connect
44fe0 69 6f 6e 73 20 74 72 61 6e 73 61 63 74 69 6f 6e  ions transaction
44ff0 20 69 73 20 63 6f 6e 63 6c 75 64 65 64 2c 20 74   is concluded, t
45000 68 65 72 65 20 6d 61 79 20 62 65 0a 2a 2a 20 6d  here may be.** m
45010 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 62 6c 6f  ore than one blo
45020 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  cked connection 
45030 74 68 61 74 20 68 61 73 20 72 65 67 69 73 74 65  that has registe
45040 72 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63  red for an unloc
45050 6b 2d 6e 6f 74 69 66 79 0a 2a 2a 20 63 61 6c 6c  k-notify.** call
45060 62 61 63 6b 2e 20 49 66 20 74 77 6f 20 6f 72 20  back. If two or 
45070 6d 6f 72 65 20 73 75 63 68 20 62 6c 6f 63 6b 65  more such blocke
45080 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 68 61  d connections ha
45090 76 65 20 73 70 65 63 69 66 69 65 64 20 74 68 65  ve specified the
450a0 0a 2a 2a 20 73 61 6d 65 20 63 61 6c 6c 62 61 63  .** same callbac
450b0 6b 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 6e  k function, then
450c0 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f   instead of invo
450d0 6b 69 6e 67 20 74 68 65 20 63 61 6c 6c 62 61 63  king the callbac
450e0 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75  k function.** mu
450f0 6c 74 69 70 6c 65 20 74 69 6d 65 73 2c 20 69 74  ltiple times, it
45100 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65   is invoked once
45110 20 77 69 74 68 20 74 68 65 20 73 65 74 20 6f 66   with the set of
45120 20 76 6f 69 64 2a 20 63 6f 6e 74 65 78 74 20 70   void* context p
45130 6f 69 6e 74 65 72 73 0a 2a 2a 20 73 70 65 63 69  ointers.** speci
45140 66 69 65 64 20 62 79 20 74 68 65 20 62 6c 6f 63  fied by the bloc
45150 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ked connections 
45160 62 75 6e 64 6c 65 64 20 74 6f 67 65 74 68 65 72  bundled together
45170 20 69 6e 74 6f 20 61 6e 20 61 72 72 61 79 2e 0a   into an array..
45180 2a 2a 20 54 68 69 73 20 67 69 76 65 73 20 74 68  ** This gives th
45190 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 61 6e  e application an
451a0 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 20   opportunity to 
451b0 70 72 69 6f 72 69 74 69 7a 65 20 61 6e 79 20 61  prioritize any a
451c0 63 74 69 6f 6e 73 20 0a 2a 2a 20 72 65 6c 61 74  ctions .** relat
451d0 65 64 20 74 6f 20 74 68 65 20 73 65 74 20 6f 66  ed to the set of
451e0 20 75 6e 62 6c 6f 63 6b 65 64 20 64 61 74 61 62   unblocked datab
451f0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e  ase connections.
45200 0a 2a 2a 0a 2a 2a 20 3c 62 3e 44 65 61 64 6c 6f  .**.** <b>Deadlo
45210 63 6b 20 44 65 74 65 63 74 69 6f 6e 3c 2f 62 3e  ck Detection</b>
45220 0a 2a 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20  .**.** Assuming 
45230 74 68 61 74 20 61 66 74 65 72 20 72 65 67 69 73  that after regis
45240 74 65 72 69 6e 67 20 66 6f 72 20 61 6e 20 75 6e  tering for an un
45250 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c  lock-notify call
45260 62 61 63 6b 20 61 20 0a 2a 2a 20 64 61 74 61 62  back a .** datab
45270 61 73 65 20 77 61 69 74 73 20 66 6f 72 20 74 68  ase waits for th
45280 65 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65  e callback to be
45290 20 69 73 73 75 65 64 20 62 65 66 6f 72 65 20 74   issued before t
452a0 61 6b 69 6e 67 20 61 6e 79 20 66 75 72 74 68 65  aking any furthe
452b0 72 0a 2a 2a 20 61 63 74 69 6f 6e 20 28 61 20 72  r.** action (a r
452c0 65 61 73 6f 6e 61 62 6c 65 20 61 73 73 75 6d 70  easonable assump
452d0 74 69 6f 6e 29 2c 20 74 68 65 6e 20 75 73 69 6e  tion), then usin
452e0 67 20 74 68 69 73 20 41 50 49 20 6d 61 79 20 63  g this API may c
452f0 61 75 73 65 20 74 68 65 0a 2a 2a 20 61 70 70 6c  ause the.** appl
45300 69 63 61 74 69 6f 6e 20 74 6f 20 64 65 61 64 6c  ication to deadl
45310 6f 63 6b 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  ock. For example
45320 2c 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  , if connection 
45330 58 20 69 73 20 77 61 69 74 69 6e 67 20 66 6f 72  X is waiting for
45340 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 59  .** connection Y
45350 27 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74  's transaction t
45360 6f 20 62 65 20 63 6f 6e 63 6c 75 64 65 64 2c 20  o be concluded, 
45370 61 6e 64 20 73 69 6d 69 6c 61 72 6c 79 20 63 6f  and similarly co
45380 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 59 20 69 73  nnection.** Y is
45390 20 77 61 69 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e   waiting on conn
453a0 65 63 74 69 6f 6e 20 58 27 73 20 74 72 61 6e 73  ection X's trans
453b0 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 6e 65 69  action, then nei
453c0 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  ther connection.
453d0 2a 2a 20 77 69 6c 6c 20 70 72 6f 63 65 65 64 20  ** will proceed 
453e0 61 6e 64 20 74 68 65 20 73 79 73 74 65 6d 20 6d  and the system m
453f0 61 79 20 72 65 6d 61 69 6e 20 64 65 61 64 6c 6f  ay remain deadlo
45400 63 6b 65 64 20 69 6e 64 65 66 69 6e 69 74 65 6c  cked indefinitel
45410 79 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69  y..**.** To avoi
45420 64 20 74 68 69 73 20 73 63 65 6e 61 72 69 6f 2c  d this scenario,
45430 20 74 68 65 20 73 71 6c 69 74 65 33 5f 75 6e 6c   the sqlite3_unl
45440 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 70 65 72  ock_notify() per
45450 66 6f 72 6d 73 20 64 65 61 64 6c 6f 63 6b 0a 2a  forms deadlock.*
45460 2a 20 64 65 74 65 63 74 69 6f 6e 2e 20 49 66 20  * detection. If 
45470 61 20 67 69 76 65 6e 20 63 61 6c 6c 20 74 6f 20  a given call to 
45480 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e  sqlite3_unlock_n
45490 6f 74 69 66 79 28 29 20 77 6f 75 6c 64 20 70 75  otify() would pu
454a0 74 20 74 68 65 0a 2a 2a 20 73 79 73 74 65 6d 20  t the.** system 
454b0 69 6e 20 61 20 64 65 61 64 6c 6f 63 6b 65 64 20  in a deadlocked 
454c0 73 74 61 74 65 2c 20 74 68 65 6e 20 53 51 4c 49  state, then SQLI
454d0 54 45 5f 4c 4f 43 4b 45 44 20 69 73 20 72 65 74  TE_LOCKED is ret
454e0 75 72 6e 65 64 20 61 6e 64 20 6e 6f 0a 2a 2a 20  urned and no.** 
454f0 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
45500 6c 6c 62 61 63 6b 20 69 73 20 72 65 67 69 73 74  llback is regist
45510 65 72 65 64 2e 20 54 68 65 20 73 79 73 74 65 6d  ered. The system
45520 20 69 73 20 73 61 69 64 20 74 6f 20 62 65 20 69   is said to be i
45530 6e 0a 2a 2a 20 61 20 64 65 61 64 6c 6f 63 6b 65  n.** a deadlocke
45540 64 20 73 74 61 74 65 20 69 66 20 63 6f 6e 6e 65  d state if conne
45550 63 74 69 6f 6e 20 41 20 68 61 73 20 72 65 67 69  ction A has regi
45560 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75 6e  stered for an un
45570 6c 6f 63 6b 2d 6e 6f 74 69 66 79 0a 2a 2a 20 63  lock-notify.** c
45580 61 6c 6c 62 61 63 6b 20 6f 6e 20 74 68 65 20 63  allback on the c
45590 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 63 6f 6e  onclusion of con
455a0 6e 65 63 74 69 6f 6e 20 42 27 73 20 74 72 61 6e  nection B's tran
455b0 73 61 63 74 69 6f 6e 2c 20 61 6e 64 20 63 6f 6e  saction, and con
455c0 6e 65 63 74 69 6f 6e 0a 2a 2a 20 42 20 68 61 73  nection.** B has
455d0 20 69 74 73 65 6c 66 20 72 65 67 69 73 74 65 72   itself register
455e0 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b  ed for an unlock
455f0 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
45600 20 77 68 65 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e   when connection
45610 0a 2a 2a 20 41 27 73 20 74 72 61 6e 73 61 63 74  .** A's transact
45620 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65 64  ion is concluded
45630 2e 20 49 6e 64 69 72 65 63 74 20 64 65 61 64 6c  . Indirect deadl
45640 6f 63 6b 20 69 73 20 61 6c 73 6f 20 64 65 74 65  ock is also dete
45650 63 74 65 64 2c 20 73 6f 0a 2a 2a 20 74 68 65 20  cted, so.** the 
45660 73 79 73 74 65 6d 20 69 73 20 61 6c 73 6f 20 63  system is also c
45670 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65 20  onsidered to be 
45680 64 65 61 64 6c 6f 63 6b 65 64 20 69 66 20 63 6f  deadlocked if co
45690 6e 6e 65 63 74 69 6f 6e 20 42 20 68 61 73 0a 2a  nnection B has.*
456a0 2a 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72  * registered for
456b0 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66   an unlock-notif
456c0 79 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 74 68  y callback on th
456d0 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20  e conclusion of 
456e0 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 43 27  connection.** C'
456f0 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 77  s transaction, w
45700 68 65 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  here connection 
45710 43 20 69 73 20 77 61 69 74 69 6e 67 20 6f 6e 20  C is waiting on 
45720 63 6f 6e 6e 65 63 74 69 6f 6e 20 41 2e 20 41 6e  connection A. An
45730 79 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 6c  y.** number of l
45740 65 76 65 6c 73 20 6f 66 20 69 6e 64 69 72 65 63  evels of indirec
45750 74 69 6f 6e 20 61 72 65 20 61 6c 6c 6f 77 65 64  tion are allowed
45760 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 54 68 65 20 22  ..**.** <b>The "
45770 44 52 4f 50 20 54 41 42 4c 45 22 20 45 78 63 65  DROP TABLE" Exce
45780 70 74 69 6f 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20  ption</b>.**.** 
45790 57 68 65 6e 20 61 20 63 61 6c 6c 20 74 6f 20 5b  When a call to [
457a0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
457b0 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4c  returns SQLITE_L
457c0 4f 43 4b 45 44 2c 20 69 74 20 69 73 20 61 6c 6d  OCKED, it is alm
457d0 6f 73 74 20 0a 2a 2a 20 61 6c 77 61 79 73 20 61  ost .** always a
457e0 70 70 72 6f 70 72 69 61 74 65 20 74 6f 20 63 61  ppropriate to ca
457f0 6c 6c 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63  ll sqlite3_unloc
45800 6b 5f 6e 6f 74 69 66 79 28 29 2e 20 54 68 65 72  k_notify(). Ther
45810 65 20 69 73 20 68 6f 77 65 76 65 72 2c 0a 2a 2a  e is however,.**
45820 20 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e 2e 20   one exception. 
45830 57 68 65 6e 20 65 78 65 63 75 74 69 6e 67 20 61  When executing a
45840 20 22 44 52 4f 50 20 54 41 42 4c 45 22 20 6f 72   "DROP TABLE" or
45850 20 22 44 52 4f 50 20 49 4e 44 45 58 22 20 73 74   "DROP INDEX" st
45860 61 74 65 6d 65 6e 74 2c 0a 2a 2a 20 53 51 4c 69  atement,.** SQLi
45870 74 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65  te checks if the
45880 72 65 20 61 72 65 20 61 6e 79 20 63 75 72 72 65  re are any curre
45890 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 53  ntly executing S
458a0 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73  ELECT statements
458b0 0a 2a 2a 20 74 68 61 74 20 62 65 6c 6f 6e 67 20  .** that belong 
458c0 74 6f 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e  to the same conn
458d0 65 63 74 69 6f 6e 2e 20 49 66 20 74 68 65 72 65  ection. If there
458e0 20 61 72 65 2c 20 53 51 4c 49 54 45 5f 4c 4f 43   are, SQLITE_LOC
458f0 4b 45 44 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e  KED is.** return
45900 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ed. In this case
45910 20 74 68 65 72 65 20 69 73 20 6e 6f 20 22 62 6c   there is no "bl
45920 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f  ocking connectio
45930 6e 22 2c 20 73 6f 20 69 6e 76 6f 6b 69 6e 67 0a  n", so invoking.
45940 2a 2a 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63  ** sqlite3_unloc
45950 6b 5f 6e 6f 74 69 66 79 28 29 20 72 65 73 75 6c  k_notify() resul
45960 74 73 20 69 6e 20 74 68 65 20 75 6e 6c 6f 63 6b  ts in the unlock
45970 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
45980 20 62 65 69 6e 67 0a 2a 2a 20 69 6e 76 6f 6b 65   being.** invoke
45990 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49  d immediately. I
459a0 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  f the applicatio
459b0 6e 20 74 68 65 6e 20 72 65 2d 61 74 74 65 6d 70  n then re-attemp
459c0 74 73 20 74 68 65 20 22 44 52 4f 50 20 54 41 42  ts the "DROP TAB
459d0 4c 45 22 0a 2a 2a 20 6f 72 20 22 44 52 4f 50 20  LE".** or "DROP 
459e0 49 4e 44 45 58 22 20 71 75 65 72 79 2c 20 61 6e  INDEX" query, an
459f0 20 69 6e 66 69 6e 69 74 65 20 6c 6f 6f 70 20 6d   infinite loop m
45a00 69 67 68 74 20 62 65 20 74 68 65 20 72 65 73 75  ight be the resu
45a10 6c 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 77 61  lt..**.** One wa
45a20 79 20 61 72 6f 75 6e 64 20 74 68 69 73 20 70 72  y around this pr
45a30 6f 62 6c 65 6d 20 69 73 20 74 6f 20 63 68 65 63  oblem is to chec
45a40 6b 20 74 68 65 20 65 78 74 65 6e 64 65 64 20 65  k the extended e
45a50 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72 6e  rror code return
45a60 65 64 0a 2a 2a 20 62 79 20 61 6e 20 73 71 6c 69  ed.** by an sqli
45a70 74 65 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 2e  te3_step() call.
45a80 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 62   If there is a b
45a90 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69  locking connecti
45aa0 6f 6e 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20  on, then the.** 
45ab0 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63  extended error c
45ac0 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20 53 51  ode is set to SQ
45ad0 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52  LITE_LOCKED_SHAR
45ae0 45 44 43 41 43 48 45 2e 20 4f 74 68 65 72 77 69  EDCACHE. Otherwi
45af0 73 65 2c 20 69 6e 0a 2a 2a 20 74 68 65 20 73 70  se, in.** the sp
45b00 65 63 69 61 6c 20 22 44 52 4f 50 20 54 41 42 4c  ecial "DROP TABL
45b10 45 2f 49 4e 44 45 58 22 20 63 61 73 65 2c 20 74  E/INDEX" case, t
45b20 68 65 20 65 78 74 65 6e 64 65 64 20 65 72 72 6f  he extended erro
45b30 72 20 63 6f 64 65 20 69 73 20 6a 75 73 74 20 0a  r code is just .
45b40 2a 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  ** SQLITE_LOCKED
45b50 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
45b60 69 6e 74 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f  int sqlite3_unlo
45b70 63 6b 5f 6e 6f 74 69 66 79 28 0a 20 20 73 71 6c  ck_notify(.  sql
45b80 69 74 65 33 20 2a 70 42 6c 6f 63 6b 65 64 2c 20  ite3 *pBlocked, 
45b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45ba0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 61 69 74           /* Wait
45bb0 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ing connection *
45bc0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 4e 6f 74 69  /.  void (*xNoti
45bd0 66 79 29 28 76 6f 69 64 20 2a 2a 61 70 41 72 67  fy)(void **apArg
45be0 2c 20 69 6e 74 20 6e 41 72 67 29 2c 20 20 20 20  , int nArg),    
45bf0 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63  /* Callback func
45c00 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 2a  tion to invoke *
45c10 2f 0a 20 20 76 6f 69 64 20 2a 70 4e 6f 74 69 66  /.  void *pNotif
45c20 79 41 72 67 20 20 20 20 20 20 20 20 20 20 20 20  yArg            
45c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45c40 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 70  /* Argument to p
45c50 61 73 73 20 74 6f 20 78 4e 6f 74 69 66 79 20 2a  ass to xNotify *
45c60 2f 0a 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  /.);.../*.** CAP
45c70 49 33 52 45 46 3a 20 53 74 72 69 6e 67 20 43 6f  I3REF: String Co
45c80 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 45 58 50 45  mparison.** EXPE
45c90 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54  RIMENTAL.**.** T
45ca0 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 72 6e  he [sqlite3_strn
45cb0 69 63 6d 70 28 29 5d 20 41 50 49 20 61 6c 6c 6f  icmp()] API allo
45cc0 77 73 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  ws applications 
45cd0 61 6e 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 74  and extensions t
45ce0 6f 0a 2a 2a 20 63 6f 6d 70 61 72 65 20 74 68 65  o.** compare the
45cf0 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 77 6f   contents of two
45d00 20 62 75 66 66 65 72 73 20 63 6f 6e 74 61 69 6e   buffers contain
45d10 69 6e 67 20 55 54 46 2d 38 20 73 74 72 69 6e 67  ing UTF-8 string
45d20 73 20 69 6e 20 61 0a 2a 2a 20 63 61 73 65 2d 69  s in a.** case-i
45d30 6e 64 65 6e 64 65 6e 74 20 66 61 73 68 69 6f 6e  ndendent fashion
45d40 2c 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65  , using the same
45d50 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 63   definition of c
45d60 61 73 65 20 69 6e 64 65 70 65 6e 64 65 6e 63 65  ase independence
45d70 20 0a 2a 2a 20 74 68 61 74 20 53 51 4c 69 74 65   .** that SQLite
45d80 20 75 73 65 73 20 69 6e 74 65 72 6e 61 6c 6c 79   uses internally
45d90 20 77 68 65 6e 20 63 6f 6d 70 61 72 69 6e 67 20   when comparing 
45da0 69 64 65 6e 74 69 66 69 65 72 73 2e 0a 2a 2f 0a  identifiers..*/.
45db0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
45dc0 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28  qlite3_strnicmp(
45dd0 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f  const char *, co
45de0 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29  nst char *, int)
45df0 3b 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 74 68  ;../*.** Undo th
45e00 65 20 68 61 63 6b 20 74 68 61 74 20 63 6f 6e 76  e hack that conv
45e10 65 72 74 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f  erts floating po
45e20 69 6e 74 20 74 79 70 65 73 20 74 6f 20 69 6e 74  int types to int
45e30 65 67 65 72 20 66 6f 72 0a 2a 2a 20 62 75 69 6c  eger for.** buil
45e40 64 73 20 6f 6e 20 70 72 6f 63 65 73 73 6f 72 73  ds on processors
45e50 20 77 69 74 68 6f 75 74 20 66 6c 6f 61 74 69 6e   without floatin
45e60 67 20 70 6f 69 6e 74 20 73 75 70 70 6f 72 74 2e  g point support.
45e70 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
45e80 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
45e90 50 4f 49 4e 54 0a 23 20 75 6e 64 65 66 20 64 6f  POINT.# undef do
45ea0 75 62 6c 65 0a 23 65 6e 64 69 66 0a 0a 23 69 66  uble.#endif..#if
45eb0 20 30 0a 7d 20 20 2f 2a 20 45 6e 64 20 6f 66 20   0.}  /* End of 
45ec0 74 68 65 20 27 65 78 74 65 72 6e 20 22 43 22 27  the 'extern "C"'
45ed0 20 62 6c 6f 63 6b 20 2a 2f 0a 23 65 6e 64 69 66   block */.#endif
45ee0 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 2a 2a 2a 2a  .#endif.../*****
45ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
45f00 20 73 71 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a   sqlite3.h *****
45f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45f30 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
45f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
45f50 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
45f60 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65  ft off in sqlite
45f70 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
45f80 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
45f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
45fa0 65 20 68 61 73 68 2e 68 20 69 6e 20 74 68 65 20  e hash.h in the 
45fb0 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65  middle of sqlite
45fc0 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
45fd0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
45fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
45ff0 66 69 6c 65 20 68 61 73 68 2e 68 20 2a 2a 2a 2a  file hash.h ****
46000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46020 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
46030 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 32  2001 September 2
46040 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
46050 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
46060 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
46070 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
46080 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
46090 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
460a0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
460b0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
460c0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
460d0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
460e0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
460f0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
46100 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
46110 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
46120 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
46130 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
46140 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
46150 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
46160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
461a0 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
461b0 68 65 61 64 65 72 20 66 69 6c 65 20 66 6f 72 20  header file for 
461c0 74 68 65 20 67 65 6e 65 72 69 63 20 68 61 73 68  the generic hash
461d0 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 61  -table implemena
461e0 74 69 6f 6e 0a 2a 2a 20 75 73 65 64 20 69 6e 20  tion.** used in 
461f0 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  SQLite..**.** $I
46200 64 3a 20 68 61 73 68 2e 68 2c 76 20 31 2e 31 35  d: hash.h,v 1.15
46210 20 32 30 30 39 2f 30 35 2f 30 32 20 31 33 3a 32   2009/05/02 13:2
46220 39 3a 33 38 20 64 72 68 20 45 78 70 20 24 0a 2a  9:38 drh Exp $.*
46230 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54  /.#ifndef _SQLIT
46240 45 5f 48 41 53 48 5f 48 5f 0a 23 64 65 66 69 6e  E_HASH_H_.#defin
46250 65 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48  e _SQLITE_HASH_H
46260 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65  _../* Forward de
46270 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 74  clarations of st
46280 72 75 63 74 75 72 65 73 2e 20 2a 2f 0a 74 79 70  ructures. */.typ
46290 65 64 65 66 20 73 74 72 75 63 74 20 48 61 73 68  edef struct Hash
462a0 20 48 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73   Hash;.typedef s
462b0 74 72 75 63 74 20 48 61 73 68 45 6c 65 6d 20 48  truct HashElem H
462c0 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20 41 20 63  ashElem;../* A c
462d0 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61 62  omplete hash tab
462e0 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  le is an instanc
462f0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
46300 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ng structure..**
46310 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f   The internals o
46320 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
46330 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f   are intended to
46340 20 62 65 20 6f 70 61 71 75 65 20 2d 2d 20 63 6c   be opaque -- cl
46350 69 65 6e 74 0a 2a 2a 20 63 6f 64 65 20 73 68 6f  ient.** code sho
46360 75 6c 64 20 6e 6f 74 20 61 74 74 65 6d 70 74 20  uld not attempt 
46370 74 6f 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64  to access or mod
46380 69 66 79 20 74 68 65 20 66 69 65 6c 64 73 20 6f  ify the fields o
46390 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
463a0 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2e 20 20 43  .** directly.  C
463b0 68 61 6e 67 65 20 74 68 69 73 20 73 74 72 75 63  hange this struc
463c0 74 75 72 65 20 6f 6e 6c 79 20 62 79 20 75 73 69  ture only by usi
463d0 6e 67 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20  ng the routines 
463e0 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77 65 76 65  below..** Howeve
463f0 72 2c 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 22  r, some of the "
46400 70 72 6f 63 65 64 75 72 65 73 22 20 61 6e 64 20  procedures" and 
46410 22 66 75 6e 63 74 69 6f 6e 73 22 20 66 6f 72 20  "functions" for 
46420 6d 6f 64 69 66 79 69 6e 67 20 61 6e 64 0a 2a 2a  modifying and.**
46430 20 61 63 63 65 73 73 69 6e 67 20 74 68 69 73 20   accessing this 
46440 73 74 72 75 63 74 75 72 65 20 61 72 65 20 72 65  structure are re
46450 61 6c 6c 79 20 6d 61 63 72 6f 73 2c 20 73 6f 20  ally macros, so 
46460 77 65 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 20  we can't really 
46470 6d 61 6b 65 0a 2a 2a 20 74 68 69 73 20 73 74 72  make.** this str
46480 75 63 74 75 72 65 20 6f 70 61 71 75 65 2e 0a 2a  ucture opaque..*
46490 2a 0a 2a 2a 20 41 6c 6c 20 65 6c 65 6d 65 6e 74  *.** All element
464a0 73 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 61  s of the hash ta
464b0 62 6c 65 20 61 72 65 20 6f 6e 20 61 20 73 69 6e  ble are on a sin
464c0 67 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65  gle doubly-linke
464d0 64 20 6c 69 73 74 2e 0a 2a 2a 20 48 61 73 68 2e  d list..** Hash.
464e0 66 69 72 73 74 20 70 6f 69 6e 74 73 20 74 6f 20  first points to 
464f0 74 68 65 20 68 65 61 64 20 6f 66 20 74 68 69 73  the head of this
46500 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   list..**.** The
46510 72 65 20 61 72 65 20 48 61 73 68 2e 68 74 73 69  re are Hash.htsi
46520 7a 65 20 62 75 63 6b 65 74 73 2e 20 20 45 61 63  ze buckets.  Eac
46530 68 20 62 75 63 6b 65 74 20 70 6f 69 6e 74 73 20  h bucket points 
46540 74 6f 20 61 20 73 70 6f 74 20 69 6e 0a 2a 2a 20  to a spot in.** 
46550 74 68 65 20 67 6c 6f 62 61 6c 20 64 6f 75 62 6c  the global doubl
46560 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 20 20  y-linked list.  
46570 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  The contents of 
46580 74 68 65 20 62 75 63 6b 65 74 20 61 72 65 20 74  the bucket are t
46590 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 70 6f  he.** element po
465a0 69 6e 74 65 64 20 74 6f 20 70 6c 75 73 20 74 68  inted to plus th
465b0 65 20 6e 65 78 74 20 5f 68 74 2e 63 6f 75 6e 74  e next _ht.count
465c0 2d 31 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74  -1 elements in t
465d0 68 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 48  he list..**.** H
465e0 61 73 68 2e 68 74 73 69 7a 65 20 61 6e 64 20 48  ash.htsize and H
465f0 61 73 68 2e 68 74 20 6d 61 79 20 62 65 20 7a 65  ash.ht may be ze
46600 72 6f 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73  ro.  In that cas
46610 65 20 6c 6f 6f 6b 75 70 20 69 73 20 64 6f 6e 65  e lookup is done
46620 0a 2a 2a 20 62 79 20 61 20 6c 69 6e 65 61 72 20  .** by a linear 
46630 73 65 61 72 63 68 20 6f 66 20 74 68 65 20 67 6c  search of the gl
46640 6f 62 61 6c 20 6c 69 73 74 2e 20 20 46 6f 72 20  obal list.  For 
46650 73 6d 61 6c 6c 20 74 61 62 6c 65 73 2c 20 74 68  small tables, th
46660 65 20 0a 2a 2a 20 48 61 73 68 2e 68 74 20 74 61  e .** Hash.ht ta
46670 62 6c 65 20 69 73 20 6e 65 76 65 72 20 61 6c 6c  ble is never all
46680 6f 63 61 74 65 64 20 62 65 63 61 75 73 65 20 69  ocated because i
46690 66 20 74 68 65 72 65 20 61 72 65 20 66 65 77 20  f there are few 
466a0 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 74  elements.** in t
466b0 68 65 20 74 61 62 6c 65 2c 20 69 74 20 69 73 20  he table, it is 
466c0 66 61 73 74 65 72 20 74 6f 20 64 6f 20 61 20 6c  faster to do a l
466d0 69 6e 65 61 72 20 73 65 61 72 63 68 20 74 68 61  inear search tha
466e0 6e 20 74 6f 20 6d 61 6e 61 67 65 0a 2a 2a 20 74  n to manage.** t
466f0 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a  he hash table..*
46700 2f 0a 73 74 72 75 63 74 20 48 61 73 68 20 7b 0a  /.struct Hash {.
46710 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68    unsigned int h
46720 74 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4e  tsize;      /* N
46730 75 6d 62 65 72 20 6f 66 20 62 75 63 6b 65 74 73  umber of buckets
46740 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62   in the hash tab
46750 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  le */.  unsigned
46760 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20   int count;     
46770 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
46780 6e 74 72 69 65 73 20 69 6e 20 74 68 69 73 20 74  ntries in this t
46790 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c  able */.  HashEl
467a0 65 6d 20 2a 66 69 72 73 74 3b 20 20 20 20 20 20  em *first;      
467b0 20 20 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74      /* The first
467c0 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
467d0 61 72 72 61 79 20 2a 2f 0a 20 20 73 74 72 75 63  array */.  struc
467e0 74 20 5f 68 74 20 7b 20 20 20 20 20 20 20 20 20  t _ht {         
467f0 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73 68       /* the hash
46800 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e   table */.    in
46810 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20  t count;        
46820 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
46830 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 77 69  er of entries wi
46840 74 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0a  th this hash */.
46850 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 63 68      HashElem *ch
46860 61 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ain;           /
46870 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 69 72  * Pointer to fir
46880 73 74 20 65 6e 74 72 79 20 77 69 74 68 20 74 68  st entry with th
46890 69 73 20 68 61 73 68 20 2a 2f 0a 20 20 7d 20 2a  is hash */.  } *
468a0 68 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20  ht;.};../* Each 
468b0 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 68  element in the h
468c0 61 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e 20  ash table is an 
468d0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
468e0 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74  following .** st
468f0 72 75 63 74 75 72 65 2e 20 20 41 6c 6c 20 65 6c  ructure.  All el
46900 65 6d 65 6e 74 73 20 61 72 65 20 73 74 6f 72 65  ements are store
46910 64 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f  d on a single do
46920 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74  ubly-linked list
46930 2e 0a 2a 2a 0a 2a 2a 20 41 67 61 69 6e 2c 20 74  ..**.** Again, t
46940 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73  his structure is
46950 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20   intended to be 
46960 6f 70 61 71 75 65 2c 20 62 75 74 20 69 74 20 63  opaque, but it c
46970 61 6e 27 74 20 72 65 61 6c 6c 79 0a 2a 2a 20 62  an't really.** b
46980 65 20 6f 70 61 71 75 65 20 62 65 63 61 75 73 65  e opaque because
46990 20 69 74 20 69 73 20 75 73 65 64 20 62 79 20 6d   it is used by m
469a0 61 63 72 6f 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  acros..*/.struct
469b0 20 48 61 73 68 45 6c 65 6d 20 7b 0a 20 20 48 61   HashElem {.  Ha
469c0 73 68 45 6c 65 6d 20 2a 6e 65 78 74 2c 20 2a 70  shElem *next, *p
469d0 72 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 4e 65  rev;       /* Ne
469e0 78 74 20 61 6e 64 20 70 72 65 76 69 6f 75 73 20  xt and previous 
469f0 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  elements in the 
46a00 74 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20  table */.  void 
46a10 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20  *data;          
46a20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20          /* Data 
46a30 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
46a40 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a  this element */.
46a50 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b    const char *pK
46a60 65 79 3b 20 69 6e 74 20 6e 4b 65 79 3b 20 20 2f  ey; int nKey;  /
46a70 2a 20 4b 65 79 20 61 73 73 6f 63 69 61 74 65 64  * Key associated
46a80 20 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65   with this eleme
46a90 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  nt */.};../*.** 
46aa0 41 63 63 65 73 73 20 72 6f 75 74 69 6e 65 73 2e  Access routines.
46ab0 20 20 54 6f 20 64 65 6c 65 74 65 2c 20 69 6e 73    To delete, ins
46ac0 65 72 74 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  ert a NULL point
46ad0 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
46ae0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
46af0 65 33 48 61 73 68 49 6e 69 74 28 48 61 73 68 2a  e3HashInit(Hash*
46b00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
46b10 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48  E void *sqlite3H
46b20 61 73 68 49 6e 73 65 72 74 28 48 61 73 68 2a 2c  ashInsert(Hash*,
46b30 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65   const char *pKe
46b40 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69  y, int nKey, voi
46b50 64 20 2a 70 44 61 74 61 29 3b 0a 53 51 4c 49 54  d *pData);.SQLIT
46b60 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
46b70 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28  sqlite3HashFind(
46b80 63 6f 6e 73 74 20 48 61 73 68 2a 2c 20 63 6f 6e  const Hash*, con
46b90 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 69  st char *pKey, i
46ba0 6e 74 20 6e 4b 65 79 29 3b 0a 53 51 4c 49 54 45  nt nKey);.SQLITE
46bb0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
46bc0 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 48  lite3HashClear(H
46bd0 61 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61  ash*);../*.** Ma
46be0 63 72 6f 73 20 66 6f 72 20 6c 6f 6f 70 69 6e 67  cros for looping
46bf0 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e   over all elemen
46c00 74 73 20 6f 66 20 61 20 68 61 73 68 20 74 61 62  ts of a hash tab
46c10 6c 65 2e 20 20 54 68 65 20 69 64 69 6f 6d 20 69  le.  The idiom i
46c20 73 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a  s.** like this:.
46c30 2a 2a 0a 2a 2a 20 20 20 48 61 73 68 20 68 3b 0a  **.**   Hash h;.
46c40 2a 2a 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 70  **   HashElem *p
46c50 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20  ;.**   ....**   
46c60 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 61 73 68  for(p=sqliteHash
46c70 46 69 72 73 74 28 26 68 29 3b 20 70 3b 20 70 3d  First(&h); p; p=
46c80 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70  sqliteHashNext(p
46c90 29 29 7b 0a 2a 2a 20 20 20 20 20 53 6f 6d 65 53  )){.**     SomeS
46ca0 74 72 75 63 74 75 72 65 20 2a 70 44 61 74 61 20  tructure *pData 
46cb0 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61  = sqliteHashData
46cc0 28 70 29 3b 0a 2a 2a 20 20 20 20 20 2f 2f 20 64  (p);.**     // d
46cd0 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 69 74 68  o something with
46ce0 20 70 44 61 74 61 0a 2a 2a 20 20 20 7d 0a 2a 2f   pData.**   }.*/
46cf0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48  .#define sqliteH
46d00 61 73 68 46 69 72 73 74 28 48 29 20 20 28 28 48  ashFirst(H)  ((H
46d10 29 2d 3e 66 69 72 73 74 29 0a 23 64 65 66 69 6e  )->first).#defin
46d20 65 20 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  e sqliteHashNext
46d30 28 45 29 20 20 20 28 28 45 29 2d 3e 6e 65 78 74  (E)   ((E)->next
46d40 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  ).#define sqlite
46d50 48 61 73 68 44 61 74 61 28 45 29 20 20 20 28 28  HashData(E)   ((
46d60 45 29 2d 3e 64 61 74 61 29 0a 2f 2a 20 23 64 65  E)->data)./* #de
46d70 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 4b  fine sqliteHashK
46d80 65 79 28 45 29 20 20 20 20 28 28 45 29 2d 3e 70  ey(E)    ((E)->p
46d90 4b 65 79 29 20 2f 2f 20 4e 4f 54 20 55 53 45 44  Key) // NOT USED
46da0 20 2a 2f 0a 2f 2a 20 23 64 65 66 69 6e 65 20 73   */./* #define s
46db0 71 6c 69 74 65 48 61 73 68 4b 65 79 73 69 7a 65  qliteHashKeysize
46dc0 28 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79 29 20  (E) ((E)->nKey) 
46dd0 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a   // NOT USED */.
46de0 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66  ./*.** Number of
46df0 20 65 6e 74 72 69 65 73 20 69 6e 20 61 20 68 61   entries in a ha
46e00 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 2f 2a 20 23  sh table.*/./* #
46e10 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73  define sqliteHas
46e20 68 43 6f 75 6e 74 28 48 29 20 20 28 28 48 29 2d  hCount(H)  ((H)-
46e30 3e 63 6f 75 6e 74 29 20 2f 2f 20 4e 4f 54 20 55  >count) // NOT U
46e40 53 45 44 20 2a 2f 0a 0a 23 65 6e 64 69 66 20 2f  SED */..#endif /
46e50 2a 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48  * _SQLITE_HASH_H
46e60 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  _ */../*********
46e70 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73  ***** End of has
46e80 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h.h ************
46e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46eb0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
46ec0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
46ed0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
46ee0 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  ff in sqliteInt.
46ef0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
46f00 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
46f10 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 61  ***** Include pa
46f20 72 73 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  rse.h in the mid
46f30 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74  dle of sqliteInt
46f40 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
46f50 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
46f60 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
46f70 20 70 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a   parse.h *******
46f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46fa0 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4b  ****/.#define TK
46fb0 5f 53 45 4d 49 20 20 20 20 20 20 20 20 20 20 20  _SEMI           
46fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46fd0 20 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58   1.#define TK_EX
46fe0 50 4c 41 49 4e 20 20 20 20 20 20 20 20 20 20 20  PLAIN           
46ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a                2.
47000 23 64 65 66 69 6e 65 20 54 4b 5f 51 55 45 52 59  #define TK_QUERY
47010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47020 20 20 20 20 20 20 20 20 20 20 20 33 0a 23 64 65             3.#de
47030 66 69 6e 65 20 54 4b 5f 50 4c 41 4e 20 20 20 20  fine TK_PLAN    
47040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47050 20 20 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e          4.#defin
47060 65 20 54 4b 5f 42 45 47 49 4e 20 20 20 20 20 20  e TK_BEGIN      
47070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47080 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 54       5.#define T
47090 4b 5f 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20  K_TRANSACTION   
470a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
470b0 20 20 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44    6.#define TK_D
470c0 45 46 45 52 52 45 44 20 20 20 20 20 20 20 20 20  EFERRED         
470d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
470e0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4d 4d 45  .#define TK_IMME
470f0 44 49 41 54 45 20 20 20 20 20 20 20 20 20 20 20  DIATE           
47100 20 20 20 20 20 20 20 20 20 20 20 20 38 0a 23 64              8.#d
47110 65 66 69 6e 65 20 54 4b 5f 45 58 43 4c 55 53 49  efine TK_EXCLUSI
47120 56 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  VE              
47130 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69           9.#defi
47140 6e 65 20 54 4b 5f 43 4f 4d 4d 49 54 20 20 20 20  ne TK_COMMIT    
47150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47160 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e 65 20       10.#define 
47170 54 4b 5f 45 4e 44 20 20 20 20 20 20 20 20 20 20  TK_END          
47180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47190 20 20 31 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f    11.#define TK_
471a0 52 4f 4c 4c 42 41 43 4b 20 20 20 20 20 20 20 20  ROLLBACK        
471b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
471c0 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 41 56  2.#define TK_SAV
471d0 45 50 4f 49 4e 54 20 20 20 20 20 20 20 20 20 20  EPOINT          
471e0 20 20 20 20 20 20 20 20 20 20 20 20 31 33 0a 23              13.#
471f0 64 65 66 69 6e 65 20 54 4b 5f 52 45 4c 45 41 53  define TK_RELEAS
47200 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
47210 20 20 20 20 20 20 20 20 20 31 34 0a 23 64 65 66           14.#def
47220 69 6e 65 20 54 4b 5f 54 4f 20 20 20 20 20 20 20  ine TK_TO       
47230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47240 20 20 20 20 20 20 31 35 0a 23 64 65 66 69 6e 65        15.#define
47250 20 54 4b 5f 54 41 42 4c 45 20 20 20 20 20 20 20   TK_TABLE       
47260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47270 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20 54 4b     16.#define TK
47280 5f 43 52 45 41 54 45 20 20 20 20 20 20 20 20 20  _CREATE         
47290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
472a0 31 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 46  17.#define TK_IF
472b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
472c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 38 0a               18.
472d0 23 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 54 20 20  #define TK_NOT  
472e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
472f0 20 20 20 20 20 20 20 20 20 20 31 39 0a 23 64 65            19.#de
47300 66 69 6e 65 20 54 4b 5f 45 58 49 53 54 53 20 20  fine TK_EXISTS  
47310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47320 20 20 20 20 20 20 20 32 30 0a 23 64 65 66 69 6e         20.#defin
47330 65 20 54 4b 5f 54 45 4d 50 20 20 20 20 20 20 20  e TK_TEMP       
47340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47350 20 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20 54      21.#define T
47360 4b 5f 4c 50 20 20 20 20 20 20 20 20 20 20 20 20  K_LP            
47370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47380 20 32 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52   22.#define TK_R
47390 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  P               
473a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 33                23
473b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 53 20 20  .#define TK_AS  
473c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
473d0 20 20 20 20 20 20 20 20 20 20 20 32 34 0a 23 64             24.#d
473e0 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 41 20 20  efine TK_COMMA  
473f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47400 20 20 20 20 20 20 20 20 32 35 0a 23 64 65 66 69          25.#defi
47410 6e 65 20 54 4b 5f 49 44 20 20 20 20 20 20 20 20  ne TK_ID        
47420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47430 20 20 20 20 20 32 36 0a 23 64 65 66 69 6e 65 20       26.#define 
47440 54 4b 5f 49 4e 44 45 58 45 44 20 20 20 20 20 20  TK_INDEXED      
47450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47460 20 20 32 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f    27.#define TK_
47470 41 42 4f 52 54 20 20 20 20 20 20 20 20 20 20 20  ABORT           
47480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
47490 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 43 54  8.#define TK_ACT
474a0 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20  ION             
474b0 20 20 20 20 20 20 20 20 20 20 20 20 32 39 0a 23              29.#
474c0 64 65 66 69 6e 65 20 54 4b 5f 41 46 54 45 52 20  define TK_AFTER 
474d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
474e0 20 20 20 20 20 20 20 20 20 33 30 0a 23 64 65 66           30.#def
474f0 69 6e 65 20 54 4b 5f 41 4e 41 4c 59 5a 45 20 20  ine TK_ANALYZE  
47500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47510 20 20 20 20 20 20 33 31 0a 23 64 65 66 69 6e 65        31.#define
47520 20 54 4b 5f 41 53 43 20 20 20 20 20 20 20 20 20   TK_ASC         
47530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47540 20 20 20 33 32 0a 23 64 65 66 69 6e 65 20 54 4b     32.#define TK
47550 5f 41 54 54 41 43 48 20 20 20 20 20 20 20 20 20  _ATTACH         
47560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47570 33 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 45  33.#define TK_BE
47580 46 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20  FORE            
47590 20 20 20 20 20 20 20 20 20 20 20 20 20 33 34 0a               34.
475a0 23 64 65 66 69 6e 65 20 54 4b 5f 42 59 20 20 20  #define TK_BY   
475b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
475c0 20 20 20 20 20 20 20 20 20 20 33 35 0a 23 64 65            35.#de
475d0 66 69 6e 65 20 54 4b 5f 43 41 53 43 41 44 45 20  fine TK_CASCADE 
475e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
475f0 20 20 20 20 20 20 20 33 36 0a 23 64 65 66 69 6e         36.#defin
47600 65 20 54 4b 5f 43 41 53 54 20 20 20 20 20 20 20  e TK_CAST       
47610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47620 20 20 20 20 33 37 0a 23 64 65 66 69 6e 65 20 54      37.#define T
47630 4b 5f 43 4f 4c 55 4d 4e 4b 57 20 20 20 20 20 20  K_COLUMNKW      
47640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47650 20 33 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43   38.#define TK_C
47660 4f 4e 46 4c 49 43 54 20 20 20 20 20 20 20 20 20  ONFLICT         
47670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 39                39
47680 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 41 54 41  .#define TK_DATA
47690 42 41 53 45 20 20 20 20 20 20 20 20 20 20 20 20  BASE            
476a0 20 20 20 20 20 20 20 20 20 20 20 34 30 0a 23 64             40.#d
476b0 65 66 69 6e 65 20 54 4b 5f 44 45 53 43 20 20 20  efine TK_DESC   
476c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
476d0 20 20 20 20 20 20 20 20 34 31 0a 23 64 65 66 69          41.#defi
476e0 6e 65 20 54 4b 5f 44 45 54 41 43 48 20 20 20 20  ne TK_DETACH    
476f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47700 20 20 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20       42.#define 
47710 54 4b 5f 45 41 43 48 20 20 20 20 20 20 20 20 20  TK_EACH         
47720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47730 20 20 34 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f    43.#define TK_
47740 46 41 49 4c 20 20 20 20 20 20 20 20 20 20 20 20  FAIL            
47750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
47760 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52  4.#define TK_FOR
47770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47780 20 20 20 20 20 20 20 20 20 20 20 20 34 35 0a 23              45.#
47790 64 65 66 69 6e 65 20 54 4b 5f 49 47 4e 4f 52 45  define TK_IGNORE
477a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
477b0 20 20 20 20 20 20 20 20 20 34 36 0a 23 64 65 66           46.#def
477c0 69 6e 65 20 54 4b 5f 49 4e 49 54 49 41 4c 4c 59  ine TK_INITIALLY
477d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
477e0 20 20 20 20 20 20 34 37 0a 23 64 65 66 69 6e 65        47.#define
477f0 20 54 4b 5f 49 4e 53 54 45 41 44 20 20 20 20 20   TK_INSTEAD     
47800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47810 20 20 20 34 38 0a 23 64 65 66 69 6e 65 20 54 4b     48.#define TK
47820 5f 4c 49 4b 45 5f 4b 57 20 20 20 20 20 20 20 20  _LIKE_KW        
47830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47840 34 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d 41  49.#define TK_MA
47850 54 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20  TCH             
47860 20 20 20 20 20 20 20 20 20 20 20 20 20 35 30 0a               50.
47870 23 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 20 20 20  #define TK_NO   
47880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47890 20 20 20 20 20 20 20 20 20 20 35 31 0a 23 64 65            51.#de
478a0 66 69 6e 65 20 54 4b 5f 4b 45 59 20 20 20 20 20  fine TK_KEY     
478b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
478c0 20 20 20 20 20 20 20 35 32 0a 23 64 65 66 69 6e         52.#defin
478d0 65 20 54 4b 5f 4f 46 20 20 20 20 20 20 20 20 20  e TK_OF         
478e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
478f0 20 20 20 20 35 33 0a 23 64 65 66 69 6e 65 20 54      53.#define T
47900 4b 5f 4f 46 46 53 45 54 20 20 20 20 20 20 20 20  K_OFFSET        
47910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47920 20 35 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50   54.#define TK_P
47930 52 41 47 4d 41 20 20 20 20 20 20 20 20 20 20 20  RAGMA           
47940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 35                55
47950 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 41 49 53  .#define TK_RAIS
47960 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
47970 20 20 20 20 20 20 20 20 20 20 20 35 36 0a 23 64             56.#d
47980 65 66 69 6e 65 20 54 4b 5f 52 45 50 4c 41 43 45  efine TK_REPLACE
47990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
479a0 20 20 20 20 20 20 20 20 35 37 0a 23 64 65 66 69          57.#defi
479b0 6e 65 20 54 4b 5f 52 45 53 54 52 49 43 54 20 20  ne TK_RESTRICT  
479c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
479d0 20 20 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20       58.#define 
479e0 54 4b 5f 52 4f 57 20 20 20 20 20 20 20 20 20 20  TK_ROW          
479f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a00 20 20 35 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f    59.#define TK_
47a10 54 52 49 47 47 45 52 20 20 20 20 20 20 20 20 20  TRIGGER         
47a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
47a30 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 43  0.#define TK_VAC
47a40 55 55 4d 20 20 20 20 20 20 20 20 20 20 20 20 20  UUM             
47a50 20 20 20 20 20 20 20 20 20 20 20 20 36 31 0a 23              61.#
47a60 64 65 66 69 6e 65 20 54 4b 5f 56 49 45 57 20 20  define TK_VIEW  
47a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a80 20 20 20 20 20 20 20 20 20 36 32 0a 23 64 65 66           62.#def
47a90 69 6e 65 20 54 4b 5f 56 49 52 54 55 41 4c 20 20  ine TK_VIRTUAL  
47aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ab0 20 20 20 20 20 20 36 33 0a 23 64 65 66 69 6e 65        63.#define
47ac0 20 54 4b 5f 52 45 49 4e 44 45 58 20 20 20 20 20   TK_REINDEX     
47ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ae0 20 20 20 36 34 0a 23 64 65 66 69 6e 65 20 54 4b     64.#define TK
47af0 5f 52 45 4e 41 4d 45 20 20 20 20 20 20 20 20 20  _RENAME         
47b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b10 36 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 54  65.#define TK_CT
47b20 49 4d 45 5f 4b 57 20 20 20 20 20 20 20 20 20 20  IME_KW          
47b30 20 20 20 20 20 20 20 20 20 20 20 20 20 36 36 0a               66.
47b40 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 59 20 20  #define TK_ANY  
47b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b60 20 20 20 20 20 20 20 20 20 20 36 37 0a 23 64 65            67.#de
47b70 66 69 6e 65 20 54 4b 5f 4f 52 20 20 20 20 20 20  fine TK_OR      
47b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b90 20 20 20 20 20 20 20 36 38 0a 23 64 65 66 69 6e         68.#defin
47ba0 65 20 54 4b 5f 41 4e 44 20 20 20 20 20 20 20 20  e TK_AND        
47bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47bc0 20 20 20 20 36 39 0a 23 64 65 66 69 6e 65 20 54      69.#define T
47bd0 4b 5f 49 53 20 20 20 20 20 20 20 20 20 20 20 20  K_IS            
47be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47bf0 20 37 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42   70.#define TK_B
47c00 45 54 57 45 45 4e 20 20 20 20 20 20 20 20 20 20  ETWEEN          
47c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 31                71
47c20 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 20 20  .#define TK_IN  
47c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c40 20 20 20 20 20 20 20 20 20 20 20 37 32 0a 23 64             72.#d
47c50 65 66 69 6e 65 20 54 4b 5f 49 53 4e 55 4c 4c 20  efine TK_ISNULL 
47c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c70 20 20 20 20 20 20 20 20 37 33 0a 23 64 65 66 69          73.#defi
47c80 6e 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20  ne TK_NOTNULL   
47c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ca0 20 20 20 20 20 37 34 0a 23 64 65 66 69 6e 65 20       74.#define 
47cb0 54 4b 5f 4e 45 20 20 20 20 20 20 20 20 20 20 20  TK_NE           
47cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47cd0 20 20 37 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f    75.#define TK_
47ce0 45 51 20 20 20 20 20 20 20 20 20 20 20 20 20 20  EQ              
47cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
47d00 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 54 20  6.#define TK_GT 
47d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d20 20 20 20 20 20 20 20 20 20 20 20 20 37 37 0a 23              77.#
47d30 64 65 66 69 6e 65 20 54 4b 5f 4c 45 20 20 20 20  define TK_LE    
47d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d50 20 20 20 20 20 20 20 20 20 37 38 0a 23 64 65 66           78.#def
47d60 69 6e 65 20 54 4b 5f 4c 54 20 20 20 20 20 20 20  ine TK_LT       
47d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d80 20 20 20 20 20 20 37 39 0a 23 64 65 66 69 6e 65        79.#define
47d90 20 54 4b 5f 47 45 20 20 20 20 20 20 20 20 20 20   TK_GE          
47da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47db0 20 20 20 38 30 0a 23 64 65 66 69 6e 65 20 54 4b     80.#define TK
47dc0 5f 45 53 43 41 50 45 20 20 20 20 20 20 20 20 20  _ESCAPE         
47dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47de0 38 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49  81.#define TK_BI
47df0 54 41 4e 44 20 20 20 20 20 20 20 20 20 20 20 20  TAND            
47e00 20 20 20 20 20 20 20 20 20 20 20 20 20 38 32 0a               82.
47e10 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 4f 52  #define TK_BITOR
47e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e30 20 20 20 20 20 20 20 20 20 20 38 33 0a 23 64 65            83.#de
47e40 66 69 6e 65 20 54 4b 5f 4c 53 48 49 46 54 20 20  fine TK_LSHIFT  
47e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e60 20 20 20 20 20 20 20 38 34 0a 23 64 65 66 69 6e         84.#defin
47e70 65 20 54 4b 5f 52 53 48 49 46 54 20 20 20 20 20  e TK_RSHIFT     
47e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e90 20 20 20 20 38 35 0a 23 64 65 66 69 6e 65 20 54      85.#define T
47ea0 4b 5f 50 4c 55 53 20 20 20 20 20 20 20 20 20 20  K_PLUS          
47eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ec0 20 38 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d   86.#define TK_M
47ed0 49 4e 55 53 20 20 20 20 20 20 20 20 20 20 20 20  INUS            
47ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 37                87
47ef0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 54 41 52  .#define TK_STAR
47f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f10 20 20 20 20 20 20 20 20 20 20 20 38 38 0a 23 64             88.#d
47f20 65 66 69 6e 65 20 54 4b 5f 53 4c 41 53 48 20 20  efine TK_SLASH  
47f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f40 20 20 20 20 20 20 20 20 38 39 0a 23 64 65 66 69          89.#defi
47f50 6e 65 20 54 4b 5f 52 45 4d 20 20 20 20 20 20 20  ne TK_REM       
47f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f70 20 20 20 20 20 39 30 0a 23 64 65 66 69 6e 65 20       90.#define 
47f80 54 4b 5f 43 4f 4e 43 41 54 20 20 20 20 20 20 20  TK_CONCAT       
47f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47fa0 20 20 39 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f    91.#define TK_
47fb0 43 4f 4c 4c 41 54 45 20 20 20 20 20 20 20 20 20  COLLATE         
47fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
47fd0 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54  2.#define TK_BIT
47fe0 4e 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20  NOT             
47ff0 20 20 20 20 20 20 20 20 20 20 20 20 39 33 0a 23              93.#
48000 64 65 66 69 6e 65 20 54 4b 5f 53 54 52 49 4e 47  define TK_STRING
48010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48020 20 20 20 20 20 20 20 20 20 39 34 0a 23 64 65 66           94.#def
48030 69 6e 65 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 20  ine TK_JOIN_KW  
48040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48050 20 20 20 20 20 20 39 35 0a 23 64 65 66 69 6e 65        95.#define
48060 20 54 4b 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20   TK_CONSTRAINT  
48070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48080 20 20 20 39 36 0a 23 64 65 66 69 6e 65 20 54 4b     96.#define TK
48090 5f 44 45 46 41 55 4c 54 20 20 20 20 20 20 20 20  _DEFAULT        
480a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
480b0 39 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 55  97.#define TK_NU
480c0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LL              
480d0 20 20 20 20 20 20 20 20 20 20 20 20 20 39 38 0a               98.
480e0 23 64 65 66 69 6e 65 20 54 4b 5f 50 52 49 4d 41  #define TK_PRIMA
480f0 52 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20  RY              
48100 20 20 20 20 20 20 20 20 20 20 39 39 0a 23 64 65            99.#de
48110 66 69 6e 65 20 54 4b 5f 55 4e 49 51 55 45 20 20  fine TK_UNIQUE  
48120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48130 20 20 20 20 20 20 20 31 30 30 0a 23 64 65 66 69         100.#defi
48140 6e 65 20 54 4b 5f 43 48 45 43 4b 20 20 20 20 20  ne TK_CHECK     
48150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48160 20 20 20 20 20 31 30 31 0a 23 64 65 66 69 6e 65       101.#define
48170 20 54 4b 5f 52 45 46 45 52 45 4e 43 45 53 20 20   TK_REFERENCES  
48180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48190 20 20 20 31 30 32 0a 23 64 65 66 69 6e 65 20 54     102.#define T
481a0 4b 5f 41 55 54 4f 49 4e 43 52 20 20 20 20 20 20  K_AUTOINCR      
481b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
481c0 20 31 30 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f   103.#define TK_
481d0 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ON              
481e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
481f0 30 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45  04.#define TK_DE
48200 4c 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20  LETE            
48210 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 35               105
48220 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 50 44 41  .#define TK_UPDA
48230 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TE              
48240 20 20 20 20 20 20 20 20 20 20 20 31 30 36 0a 23             106.#
48250 64 65 66 69 6e 65 20 54 4b 5f 53 45 54 20 20 20  define TK_SET   
48260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48270 20 20 20 20 20 20 20 20 20 31 30 37 0a 23 64 65           107.#de
48280 66 69 6e 65 20 54 4b 5f 44 45 46 45 52 52 41 42  fine TK_DEFERRAB
48290 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LE              
482a0 20 20 20 20 20 20 20 31 30 38 0a 23 64 65 66 69         108.#defi
482b0 6e 65 20 54 4b 5f 46 4f 52 45 49 47 4e 20 20 20  ne TK_FOREIGN   
482c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
482d0 20 20 20 20 20 31 30 39 0a 23 64 65 66 69 6e 65       109.#define
482e0 20 54 4b 5f 44 52 4f 50 20 20 20 20 20 20 20 20   TK_DROP        
482f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48300 20 20 20 31 31 30 0a 23 64 65 66 69 6e 65 20 54     110.#define T
48310 4b 5f 55 4e 49 4f 4e 20 20 20 20 20 20 20 20 20  K_UNION         
48320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48330 20 31 31 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f   111.#define TK_
48340 41 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 20  ALL             
48350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48360 31 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58  12.#define TK_EX
48370 43 45 50 54 20 20 20 20 20 20 20 20 20 20 20 20  CEPT            
48380 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 33               113
48390 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54 45  .#define TK_INTE
483a0 52 53 45 43 54 20 20 20 20 20 20 20 20 20 20 20  RSECT           
483b0 20 20 20 20 20 20 20 20 20 20 20 31 31 34 0a 23             114.#
483c0 64 65 66 69 6e 65 20 54 4b 5f 53 45 4c 45 43 54  define TK_SELECT
483d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
483e0 20 20 20 20 20 20 20 20 20 31 31 35 0a 23 64 65           115.#de
483f0 66 69 6e 65 20 54 4b 5f 44 49 53 54 49 4e 43 54  fine TK_DISTINCT
48400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48410 20 20 20 20 20 20 20 31 31 36 0a 23 64 65 66 69         116.#defi
48420 6e 65 20 54 4b 5f 44 4f 54 20 20 20 20 20 20 20  ne TK_DOT       
48430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48440 20 20 20 20 20 31 31 37 0a 23 64 65 66 69 6e 65       117.#define
48450 20 54 4b 5f 46 52 4f 4d 20 20 20 20 20 20 20 20   TK_FROM        
48460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48470 20 20 20 31 31 38 0a 23 64 65 66 69 6e 65 20 54     118.#define T
48480 4b 5f 4a 4f 49 4e 20 20 20 20 20 20 20 20 20 20  K_JOIN          
48490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
484a0 20 31 31 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f   119.#define TK_
484b0 55 53 49 4e 47 20 20 20 20 20 20 20 20 20 20 20  USING           
484c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
484d0 32 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52  20.#define TK_OR
484e0 44 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20  DER             
484f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 31               121
48500 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 52 4f 55  .#define TK_GROU
48510 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  P               
48520 20 20 20 20 20 20 20 20 20 20 20 31 32 32 0a 23             122.#
48530 64 65 66 69 6e 65 20 54 4b 5f 48 41 56 49 4e 47  define TK_HAVING
48540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48550 20 20 20 20 20 20 20 20 20 31 32 33 0a 23 64 65           123.#de
48560 66 69 6e 65 20 54 4b 5f 4c 49 4d 49 54 20 20 20  fine TK_LIMIT   
48570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48580 20 20 20 20 20 20 20 31 32 34 0a 23 64 65 66 69         124.#defi
48590 6e 65 20 54 4b 5f 57 48 45 52 45 20 20 20 20 20  ne TK_WHERE     
485a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
485b0 20 20 20 20 20 31 32 35 0a 23 64 65 66 69 6e 65       125.#define
485c0 20 54 4b 5f 49 4e 54 4f 20 20 20 20 20 20 20 20   TK_INTO        
485d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
485e0 20 20 20 31 32 36 0a 23 64 65 66 69 6e 65 20 54     126.#define T
485f0 4b 5f 56 41 4c 55 45 53 20 20 20 20 20 20 20 20  K_VALUES        
48600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48610 20 31 32 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f   127.#define TK_
48620 49 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 20  INSERT          
48630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48640 32 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e  28.#define TK_IN
48650 54 45 47 45 52 20 20 20 20 20 20 20 20 20 20 20  TEGER           
48660 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 39               129
48670 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4c 4f 41  .#define TK_FLOA
48680 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
48690 20 20 20 20 20 20 20 20 20 20 20 31 33 30 0a 23             130.#
486a0 64 65 66 69 6e 65 20 54 4b 5f 42 4c 4f 42 20 20  define TK_BLOB  
486b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
486c0 20 20 20 20 20 20 20 20 20 31 33 31 0a 23 64 65           131.#de
486d0 66 69 6e 65 20 54 4b 5f 52 45 47 49 53 54 45 52  fine TK_REGISTER
486e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
486f0 20 20 20 20 20 20 20 31 33 32 0a 23 64 65 66 69         132.#defi
48700 6e 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 20 20  ne TK_VARIABLE  
48710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48720 20 20 20 20 20 31 33 33 0a 23 64 65 66 69 6e 65       133.#define
48730 20 54 4b 5f 43 41 53 45 20 20 20 20 20 20 20 20   TK_CASE        
48740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48750 20 20 20 31 33 34 0a 23 64 65 66 69 6e 65 20 54     134.#define T
48760 4b 5f 57 48 45 4e 20 20 20 20 20 20 20 20 20 20  K_WHEN          
48770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48780 20 31 33 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f   135.#define TK_
48790 54 48 45 4e 20 20 20 20 20 20 20 20 20 20 20 20  THEN            
487a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
487b0 33 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4c  36.#define TK_EL
487c0 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  SE              
487d0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 37               137
487e0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 44 45  .#define TK_INDE
487f0 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  X               
48800 20 20 20 20 20 20 20 20 20 20 20 31 33 38 0a 23             138.#
48810 64 65 66 69 6e 65 20 54 4b 5f 41 4c 54 45 52 20  define TK_ALTER 
48820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48830 20 20 20 20 20 20 20 20 20 31 33 39 0a 23 64 65           139.#de
48840 66 69 6e 65 20 54 4b 5f 41 44 44 20 20 20 20 20  fine TK_ADD     
48850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48860 20 20 20 20 20 20 20 31 34 30 0a 23 64 65 66 69         140.#defi
48870 6e 65 20 54 4b 5f 54 4f 5f 54 45 58 54 20 20 20  ne TK_TO_TEXT   
48880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48890 20 20 20 20 20 31 34 31 0a 23 64 65 66 69 6e 65       141.#define
488a0 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 20 20 20   TK_TO_BLOB     
488b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
488c0 20 20 20 31 34 32 0a 23 64 65 66 69 6e 65 20 54     142.#define T
488d0 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 20 20 20 20  K_TO_NUMERIC    
488e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
488f0 20 31 34 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f   143.#define TK_
48900 54 4f 5f 49 4e 54 20 20 20 20 20 20 20 20 20 20  TO_INT          
48910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48920 34 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f  44.#define TK_TO
48930 5f 52 45 41 4c 20 20 20 20 20 20 20 20 20 20 20  _REAL           
48940 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 35               145
48950 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 4e 4f  .#define TK_ISNO
48960 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
48970 20 20 20 20 20 20 20 20 20 20 20 31 34 36 0a 23             146.#
48980 64 65 66 69 6e 65 20 54 4b 5f 45 4e 44 5f 4f 46  define TK_END_OF
48990 5f 46 49 4c 45 20 20 20 20 20 20 20 20 20 20 20  _FILE           
489a0 20 20 20 20 20 20 20 20 20 31 34 37 0a 23 64 65           147.#de
489b0 66 69 6e 65 20 54 4b 5f 49 4c 4c 45 47 41 4c 20  fine TK_ILLEGAL 
489c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
489d0 20 20 20 20 20 20 20 31 34 38 0a 23 64 65 66 69         148.#defi
489e0 6e 65 20 54 4b 5f 53 50 41 43 45 20 20 20 20 20  ne TK_SPACE     
489f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a00 20 20 20 20 20 31 34 39 0a 23 64 65 66 69 6e 65       149.#define
48a10 20 54 4b 5f 55 4e 43 4c 4f 53 45 44 5f 53 54 52   TK_UNCLOSED_STR
48a20 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20  ING             
48a30 20 20 20 31 35 30 0a 23 64 65 66 69 6e 65 20 54     150.#define T
48a40 4b 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20  K_FUNCTION      
48a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a60 20 31 35 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f   151.#define TK_
48a70 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20  COLUMN          
48a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48a90 35 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 47  52.#define TK_AG
48aa0 47 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20  G_FUNCTION      
48ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 33               153
48ac0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 47 47 5f  .#define TK_AGG_
48ad0 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20  COLUMN          
48ae0 20 20 20 20 20 20 20 20 20 20 20 31 35 34 0a 23             154.#
48af0 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 5f  define TK_CONST_
48b00 46 55 4e 43 20 20 20 20 20 20 20 20 20 20 20 20  FUNC            
48b10 20 20 20 20 20 20 20 20 20 31 35 35 0a 23 64 65           155.#de
48b20 66 69 6e 65 20 54 4b 5f 55 4d 49 4e 55 53 20 20  fine TK_UMINUS  
48b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48b40 20 20 20 20 20 20 20 31 35 36 0a 23 64 65 66 69         156.#defi
48b50 6e 65 20 54 4b 5f 55 50 4c 55 53 20 20 20 20 20  ne TK_UPLUS     
48b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48b70 20 20 20 20 20 31 35 37 0a 0a 2f 2a 2a 2a 2a 2a       157../*****
48b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
48b90 20 70 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a   parse.h *******
48ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48bc0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
48bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
48be0 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
48bf0 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65  ft off in sqlite
48c00 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
48c10 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 6e 63 6c 75  ********/.#inclu
48c20 64 65 20 3c 73 74 64 69 6f 2e 68 3e 0a 23 69 6e  de <stdio.h>.#in
48c30 63 6c 75 64 65 20 3c 73 74 64 6c 69 62 2e 68 3e  clude <stdlib.h>
48c40 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e  .#include <strin
48c50 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 61  g.h>.#include <a
48c60 73 73 65 72 74 2e 68 3e 0a 23 69 6e 63 6c 75 64  ssert.h>.#includ
48c70 65 20 3c 73 74 64 64 65 66 2e 68 3e 0a 0a 2f 2a  e <stddef.h>../*
48c80 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 69 6e 67  .** If compiling
48c90 20 66 6f 72 20 61 20 70 72 6f 63 65 73 73 6f 72   for a processor
48ca0 20 74 68 61 74 20 6c 61 63 6b 73 20 66 6c 6f 61   that lacks floa
48cb0 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f  ting point suppo
48cc0 72 74 2c 0a 2a 2a 20 73 75 62 73 74 69 74 75 74  rt,.** substitut
48cd0 65 20 69 6e 74 65 67 65 72 20 66 6f 72 20 66 6c  e integer for fl
48ce0 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 0a 2a 2f 0a  oating-point.*/.
48cf0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
48d00 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
48d10 54 0a 23 20 64 65 66 69 6e 65 20 64 6f 75 62 6c  T.# define doubl
48d20 65 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23  e sqlite_int64.#
48d30 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42   define LONGDOUB
48d40 4c 45 5f 54 59 50 45 20 73 71 6c 69 74 65 5f 69  LE_TYPE sqlite_i
48d50 6e 74 36 34 0a 23 20 69 66 6e 64 65 66 20 53 51  nt64.# ifndef SQ
48d60 4c 49 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 20  LITE_BIG_DBL.#  
48d70 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42   define SQLITE_B
48d80 49 47 5f 44 42 4c 20 28 28 28 73 71 6c 69 74 65  IG_DBL (((sqlite
48d90 33 5f 69 6e 74 36 34 29 31 29 3c 3c 35 30 29 0a  3_int64)1)<<50).
48da0 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65  # endif.# define
48db0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54   SQLITE_OMIT_DAT
48dc0 45 54 49 4d 45 5f 46 55 4e 43 53 20 31 0a 23 20  ETIME_FUNCS 1.# 
48dd0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d  define SQLITE_OM
48de0 49 54 5f 54 52 41 43 45 20 31 0a 23 20 75 6e 64  IT_TRACE 1.# und
48df0 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f  ef SQLITE_MIXED_
48e00 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f  ENDIAN_64BIT_FLO
48e10 41 54 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54  AT.# undef SQLIT
48e20 45 5f 48 41 56 45 5f 49 53 4e 41 4e 0a 23 65 6e  E_HAVE_ISNAN.#en
48e30 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  dif.#ifndef SQLI
48e40 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 64 65 66  TE_BIG_DBL.# def
48e50 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44  ine SQLITE_BIG_D
48e60 42 4c 20 28 31 65 39 39 29 0a 23 65 6e 64 69 66  BL (1e99).#endif
48e70 0a 0a 2f 2a 0a 2a 2a 20 4f 4d 49 54 5f 54 45 4d  ../*.** OMIT_TEM
48e80 50 44 42 20 69 73 20 73 65 74 20 74 6f 20 31 20  PDB is set to 1 
48e90 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  if SQLITE_OMIT_T
48ea0 45 4d 50 44 42 20 69 73 20 64 65 66 69 6e 65 64  EMPDB is defined
48eb0 2c 20 6f 72 20 30 0a 2a 2a 20 61 66 74 65 72 77  , or 0.** afterw
48ec0 61 72 64 2e 20 48 61 76 69 6e 67 20 74 68 69 73  ard. Having this
48ed0 20 6d 61 63 72 6f 20 61 6c 6c 6f 77 73 20 75 73   macro allows us
48ee0 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 43 20   to cause the C 
48ef0 63 6f 6d 70 69 6c 65 72 20 0a 2a 2a 20 74 6f 20  compiler .** to 
48f00 6f 6d 69 74 20 63 6f 64 65 20 75 73 65 64 20 62  omit code used b
48f10 79 20 54 45 4d 50 20 74 61 62 6c 65 73 20 77 69  y TEMP tables wi
48f20 74 68 6f 75 74 20 6d 65 73 73 79 20 23 69 66 6e  thout messy #ifn
48f30 64 65 66 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  def statements..
48f40 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
48f50 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 23 64 65  _OMIT_TEMPDB.#de
48f60 66 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42  fine OMIT_TEMPDB
48f70 20 31 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65   1.#else.#define
48f80 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 30 0a 23   OMIT_TEMPDB 0.#
48f90 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  endif../*.** If 
48fa0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61  the following ma
48fb0 63 72 6f 20 69 73 20 73 65 74 20 74 6f 20 31 2c  cro is set to 1,
48fc0 20 74 68 65 6e 20 4e 55 4c 4c 20 76 61 6c 75 65   then NULL value
48fd0 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64  s are considered
48fe0 0a 2a 2a 20 64 69 73 74 69 6e 63 74 20 77 68 65  .** distinct whe
48ff0 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68  n determining wh
49000 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 77 6f  ether or not two
49010 20 65 6e 74 72 69 65 73 20 61 72 65 20 74 68 65   entries are the
49020 20 73 61 6d 65 0a 2a 2a 20 69 6e 20 61 20 55 4e   same.** in a UN
49030 49 51 55 45 20 69 6e 64 65 78 2e 20 20 54 68 69  IQUE index.  Thi
49040 73 20 69 73 20 74 68 65 20 77 61 79 20 50 6f 73  s is the way Pos
49050 74 67 72 65 53 51 4c 2c 20 4f 72 61 63 6c 65 2c  tgreSQL, Oracle,
49060 20 44 42 32 2c 20 4d 79 53 51 4c 2c 0a 2a 2a 20   DB2, MySQL,.** 
49070 4f 43 45 4c 4f 54 2c 20 61 6e 64 20 46 69 72 65  OCELOT, and Fire
49080 62 69 72 64 20 61 6c 6c 20 77 6f 72 6b 2e 20 20  bird all work.  
49090 54 68 65 20 53 51 4c 39 32 20 73 70 65 63 20 65  The SQL92 spec e
490a0 78 70 6c 69 63 69 74 6c 79 20 73 61 79 73 20 74  xplicitly says t
490b0 68 69 73 0a 2a 2a 20 69 73 20 74 68 65 20 77 61  his.** is the wa
490c0 79 20 74 68 69 6e 67 73 20 61 72 65 20 73 75 70  y things are sup
490d0 70 6f 73 65 20 74 6f 20 77 6f 72 6b 2e 0a 2a 2a  pose to work..**
490e0 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f  .** If the follo
490f0 77 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 73 65  wing macro is se
49100 74 20 74 6f 20 30 2c 20 74 68 65 20 4e 55 4c 4c  t to 0, the NULL
49110 73 20 61 72 65 20 69 6e 64 69 73 74 69 6e 63 74  s are indistinct
49120 20 66 6f 72 0a 2a 2a 20 61 20 55 4e 49 51 55 45   for.** a UNIQUE
49130 20 69 6e 64 65 78 2e 20 20 49 6e 20 74 68 69 73   index.  In this
49140 20 6d 6f 64 65 2c 20 79 6f 75 20 63 61 6e 20 6f   mode, you can o
49150 6e 6c 79 20 68 61 76 65 20 61 20 73 69 6e 67 6c  nly have a singl
49160 65 20 4e 55 4c 4c 20 65 6e 74 72 79 0a 2a 2a 20  e NULL entry.** 
49170 66 6f 72 20 61 20 63 6f 6c 75 6d 6e 20 64 65 63  for a column dec
49180 6c 61 72 65 64 20 55 4e 49 51 55 45 2e 20 20 54  lared UNIQUE.  T
49190 68 69 73 20 69 73 20 74 68 65 20 77 61 79 20 49  his is the way I
491a0 6e 66 6f 72 6d 69 78 20 61 6e 64 20 53 51 4c 20  nformix and SQL 
491b0 53 65 72 76 65 72 0a 2a 2a 20 77 6f 72 6b 2e 0a  Server.** work..
491c0 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 55 4c 4c 5f  */.#define NULL_
491d0 44 49 53 54 49 4e 43 54 5f 46 4f 52 5f 55 4e 49  DISTINCT_FOR_UNI
491e0 51 55 45 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  QUE 1../*.** The
491f0 20 22 66 69 6c 65 20 66 6f 72 6d 61 74 22 20 6e   "file format" n
49200 75 6d 62 65 72 20 69 73 20 61 6e 20 69 6e 74 65  umber is an inte
49210 67 65 72 20 74 68 61 74 20 69 73 20 69 6e 63 72  ger that is incr
49220 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72  emented whenever
49230 0a 2a 2a 20 74 68 65 20 56 44 42 45 2d 6c 65 76  .** the VDBE-lev
49240 65 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 63  el file format c
49250 68 61 6e 67 65 73 2e 20 20 54 68 65 20 66 6f 6c  hanges.  The fol
49260 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 64 65  lowing macros de
49270 66 69 6e 65 20 74 68 65 0a 2a 2a 20 74 68 65 20  fine the.** the 
49280 64 65 66 61 75 6c 74 20 66 69 6c 65 20 66 6f 72  default file for
49290 6d 61 74 20 66 6f 72 20 6e 65 77 20 64 61 74 61  mat for new data
492a0 62 61 73 65 73 20 61 6e 64 20 74 68 65 20 6d 61  bases and the ma
492b0 78 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61  ximum file forma
492c0 74 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6c 69  t.** that the li
492d0 62 72 61 72 79 20 63 61 6e 20 72 65 61 64 2e 0a  brary can read..
492e0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
492f0 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41  E_MAX_FILE_FORMA
49300 54 20 34 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  T 4.#ifndef SQLI
49310 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f  TE_DEFAULT_FILE_
49320 46 4f 52 4d 41 54 0a 23 20 64 65 66 69 6e 65 20  FORMAT.# define 
49330 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46  SQLITE_DEFAULT_F
49340 49 4c 45 5f 46 4f 52 4d 41 54 20 31 0a 23 65 6e  ILE_FORMAT 1.#en
49350 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
49360 49 54 45 5f 44 45 46 41 55 4c 54 5f 52 45 43 55  ITE_DEFAULT_RECU
49370 52 53 49 56 45 5f 54 52 49 47 47 45 52 53 0a 23  RSIVE_TRIGGERS.#
49380 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44   define SQLITE_D
49390 45 46 41 55 4c 54 5f 52 45 43 55 52 53 49 56 45  EFAULT_RECURSIVE
493a0 5f 54 52 49 47 47 45 52 53 20 30 0a 23 65 6e 64  _TRIGGERS 0.#end
493b0 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 76 69 64  if../*.** Provid
493c0 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75  e a default valu
493d0 65 20 66 6f 72 20 53 51 4c 49 54 45 5f 54 45 4d  e for SQLITE_TEM
493e0 50 5f 53 54 4f 52 45 20 69 6e 20 63 61 73 65 20  P_STORE in case 
493f0 69 74 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66  it is not specif
49400 69 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f  ied.** on the co
49410 6d 6d 61 6e 64 2d 6c 69 6e 65 0a 2a 2f 0a 23 69  mmand-line.*/.#i
49420 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d  fndef SQLITE_TEM
49430 50 5f 53 54 4f 52 45 0a 23 20 64 65 66 69 6e 65  P_STORE.# define
49440 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f   SQLITE_TEMP_STO
49450 52 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  RE 1.#endif../*.
49460 2a 2a 20 47 43 43 20 64 6f 65 73 20 6e 6f 74 20  ** GCC does not 
49470 64 65 66 69 6e 65 20 74 68 65 20 6f 66 66 73 65  define the offse
49480 74 6f 66 28 29 20 6d 61 63 72 6f 20 73 6f 20 77  tof() macro so w
49490 65 27 6c 6c 20 68 61 76 65 20 74 6f 20 64 6f 20  e'll have to do 
494a0 69 74 0a 2a 2a 20 6f 75 72 73 65 6c 76 65 73 2e  it.** ourselves.
494b0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 6f 66 66 73  .*/.#ifndef offs
494c0 65 74 6f 66 0a 23 64 65 66 69 6e 65 20 6f 66 66  etof.#define off
494d0 73 65 74 6f 66 28 53 54 52 55 43 54 55 52 45 2c  setof(STRUCTURE,
494e0 46 49 45 4c 44 29 20 28 28 69 6e 74 29 28 28 63  FIELD) ((int)((c
494f0 68 61 72 2a 29 26 28 28 53 54 52 55 43 54 55 52  har*)&((STRUCTUR
49500 45 2a 29 30 29 2d 3e 46 49 45 4c 44 29 29 0a 23  E*)0)->FIELD)).#
49510 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  endif../*.** Che
49520 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 69  ck to see if thi
49530 73 20 6d 61 63 68 69 6e 65 20 75 73 65 73 20 45  s machine uses E
49540 42 43 44 49 43 2e 20 20 28 59 65 73 2c 20 62 65  BCDIC.  (Yes, be
49550 6c 69 65 76 65 20 69 74 20 6f 72 0a 2a 2a 20 6e  lieve it or.** n
49560 6f 74 2c 20 74 68 65 72 65 20 61 72 65 20 73 74  ot, there are st
49570 69 6c 6c 20 6d 61 63 68 69 6e 65 73 20 6f 75 74  ill machines out
49580 20 74 68 65 72 65 20 74 68 61 74 20 75 73 65 20   there that use 
49590 45 42 43 44 49 43 2e 29 0a 2a 2f 0a 23 69 66 20  EBCDIC.).*/.#if 
495a0 27 41 27 20 3d 3d 20 27 5c 33 30 31 27 0a 23 20  'A' == '\301'.# 
495b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 42  define SQLITE_EB
495c0 43 44 49 43 20 31 0a 23 65 6c 73 65 0a 23 20 64  CDIC 1.#else.# d
495d0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 53 43  efine SQLITE_ASC
495e0 49 49 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  II 1.#endif../*.
495f0 2a 2a 20 49 6e 74 65 67 65 72 73 20 6f 66 20 6b  ** Integers of k
49600 6e 6f 77 6e 20 73 69 7a 65 73 2e 20 20 54 68 65  nown sizes.  The
49610 73 65 20 74 79 70 65 64 65 66 73 20 6d 69 67 68  se typedefs migh
49620 74 20 63 68 61 6e 67 65 20 66 6f 72 20 61 72 63  t change for arc
49630 68 69 74 65 63 74 75 72 65 73 0a 2a 2a 20 77 68  hitectures.** wh
49640 65 72 65 20 74 68 65 20 73 69 7a 65 73 20 76 65  ere the sizes ve
49650 72 79 2e 20 20 50 72 65 70 72 6f 63 65 73 73 6f  ry.  Preprocesso
49660 72 20 6d 61 63 72 6f 73 20 61 72 65 20 61 76 61  r macros are ava
49670 69 6c 61 62 6c 65 20 73 6f 20 74 68 61 74 20 74  ilable so that t
49680 68 65 0a 2a 2a 20 74 79 70 65 73 20 63 61 6e 20  he.** types can 
49690 62 65 20 63 6f 6e 76 65 6e 69 65 6e 74 6c 79 20  be conveniently 
496a0 72 65 64 65 66 69 6e 65 64 20 61 74 20 63 6f 6d  redefined at com
496b0 70 69 6c 65 2d 74 79 70 65 2e 20 20 4c 69 6b 65  pile-type.  Like
496c0 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
496d0 20 20 20 20 20 63 63 20 27 2d 44 55 49 4e 54 50       cc '-DUINTP
496e0 54 52 5f 54 59 50 45 3d 6c 6f 6e 67 20 6c 6f 6e  TR_TYPE=long lon
496f0 67 20 69 6e 74 27 20 2e 2e 2e 0a 2a 2f 0a 23 69  g int' ....*/.#i
49700 66 6e 64 65 66 20 55 49 4e 54 33 32 5f 54 59 50  fndef UINT32_TYP
49710 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 55  E.# ifdef HAVE_U
49720 49 4e 54 33 32 5f 54 0a 23 20 20 64 65 66 69 6e  INT32_T.#  defin
49730 65 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 69  e UINT32_TYPE ui
49740 6e 74 33 32 5f 74 0a 23 20 65 6c 73 65 0a 23 20  nt32_t.# else.# 
49750 20 64 65 66 69 6e 65 20 55 49 4e 54 33 32 5f 54   define UINT32_T
49760 59 50 45 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  YPE unsigned int
49770 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  .# endif.#endif.
49780 23 69 66 6e 64 65 66 20 55 49 4e 54 31 36 5f 54  #ifndef UINT16_T
49790 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45  YPE.# ifdef HAVE
497a0 5f 55 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 66  _UINT16_T.#  def
497b0 69 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20  ine UINT16_TYPE 
497c0 75 69 6e 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a  uint16_t.# else.
497d0 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 31 36  #  define UINT16
497e0 5f 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 73  _TYPE unsigned s
497f0 68 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64 69 66  hort int.# endif
49800 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
49810 49 4e 54 31 36 5f 54 59 50 45 0a 23 20 69 66 64  INT16_TYPE.# ifd
49820 65 66 20 48 41 56 45 5f 49 4e 54 31 36 5f 54 0a  ef HAVE_INT16_T.
49830 23 20 20 64 65 66 69 6e 65 20 49 4e 54 31 36 5f  #  define INT16_
49840 54 59 50 45 20 69 6e 74 31 36 5f 74 0a 23 20 65  TYPE int16_t.# e
49850 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 4e  lse.#  define IN
49860 54 31 36 5f 54 59 50 45 20 73 68 6f 72 74 20 69  T16_TYPE short i
49870 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69  nt.# endif.#endi
49880 66 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 38 5f  f.#ifndef UINT8_
49890 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56  TYPE.# ifdef HAV
498a0 45 5f 55 49 4e 54 38 5f 54 0a 23 20 20 64 65 66  E_UINT8_T.#  def
498b0 69 6e 65 20 55 49 4e 54 38 5f 54 59 50 45 20 75  ine UINT8_TYPE u
498c0 69 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a 23 20  int8_t.# else.# 
498d0 20 64 65 66 69 6e 65 20 55 49 4e 54 38 5f 54 59   define UINT8_TY
498e0 50 45 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  PE unsigned char
498f0 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  .# endif.#endif.
49900 23 69 66 6e 64 65 66 20 49 4e 54 38 5f 54 59 50  #ifndef INT8_TYP
49910 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 49  E.# ifdef HAVE_I
49920 4e 54 38 5f 54 0a 23 20 20 64 65 66 69 6e 65 20  NT8_T.#  define 
49930 49 4e 54 38 5f 54 59 50 45 20 69 6e 74 38 5f 74  INT8_TYPE int8_t
49940 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e  .# else.#  defin
49950 65 20 49 4e 54 38 5f 54 59 50 45 20 73 69 67 6e  e INT8_TYPE sign
49960 65 64 20 63 68 61 72 0a 23 20 65 6e 64 69 66 0a  ed char.# endif.
49970 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4c  #endif.#ifndef L
49980 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 0a 23  ONGDOUBLE_TYPE.#
49990 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42   define LONGDOUB
499a0 4c 45 5f 54 59 50 45 20 6c 6f 6e 67 20 64 6f 75  LE_TYPE long dou
499b0 62 6c 65 0a 23 65 6e 64 69 66 0a 74 79 70 65 64  ble.#endif.typed
499c0 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  ef sqlite_int64 
499d0 69 36 34 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  i64;          /*
499e0 20 38 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69   8-byte signed i
499f0 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65  nteger */.typede
49a00 66 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  f sqlite_uint64 
49a10 75 36 34 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  u64;         /* 
49a20 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20  8-byte unsigned 
49a30 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64  integer */.typed
49a40 65 66 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75  ef UINT32_TYPE u
49a50 33 32 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  32;           /*
49a60 20 34 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64   4-byte unsigned
49a70 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65   integer */.type
49a80 64 65 66 20 55 49 4e 54 31 36 5f 54 59 50 45 20  def UINT16_TYPE 
49a90 75 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 2f  u16;           /
49aa0 2a 20 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65  * 2-byte unsigne
49ab0 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70  d integer */.typ
49ac0 65 64 65 66 20 49 4e 54 31 36 5f 54 59 50 45 20  edef INT16_TYPE 
49ad0 69 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20  i16;            
49ae0 2f 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64  /* 2-byte signed
49af0 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65   integer */.type
49b00 64 65 66 20 55 49 4e 54 38 5f 54 59 50 45 20 75  def UINT8_TYPE u
49b10 38 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  8;             /
49b20 2a 20 31 2d 62 79 74 65 20 75 6e 73 69 67 6e 65  * 1-byte unsigne
49b30 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70  d integer */.typ
49b40 65 64 65 66 20 49 4e 54 38 5f 54 59 50 45 20 69  edef INT8_TYPE i
49b50 38 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  8;              
49b60 2f 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64  /* 1-byte signed
49b70 20 69 6e 74 65 67 65 72 20 2a 2f 0a 0a 2f 2a 0a   integer */../*.
49b80 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33  ** SQLITE_MAX_U3
49b90 32 20 69 73 20 61 20 75 36 34 20 63 6f 6e 73 74  2 is a u64 const
49ba0 61 6e 74 20 74 68 61 74 20 69 73 20 74 68 65 20  ant that is the 
49bb0 6d 61 78 69 6d 75 6d 20 75 36 34 20 76 61 6c 75  maximum u64 valu
49bc0 65 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65  e.** that can be
49bd0 20 73 74 6f 72 65 64 20 69 6e 20 61 20 75 33 32   stored in a u32
49be0 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66   without loss of
49bf0 20 64 61 74 61 2e 20 20 54 68 65 20 76 61 6c 75   data.  The valu
49c00 65 0a 2a 2a 20 69 73 20 30 78 30 30 30 30 30 30  e.** is 0x000000
49c10 30 30 66 66 66 66 66 66 66 66 2e 20 20 42 75 74  00ffffffff.  But
49c20 20 62 65 63 61 75 73 65 20 6f 66 20 71 75 69 72   because of quir
49c30 6b 73 20 6f 66 20 73 6f 6d 65 20 63 6f 6d 70 69  ks of some compi
49c40 6c 65 72 73 2c 20 77 65 0a 2a 2a 20 68 61 76 65  lers, we.** have
49c50 20 74 6f 20 73 70 65 63 69 66 79 20 74 68 65 20   to specify the 
49c60 76 61 6c 75 65 20 69 6e 20 74 68 65 20 6c 65 73  value in the les
49c70 73 20 69 6e 74 75 69 74 69 76 65 20 6d 61 6e 6e  s intuitive mann
49c80 65 72 20 73 68 6f 77 6e 3a 0a 2a 2f 0a 23 64 65  er shown:.*/.#de
49c90 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
49ca0 55 33 32 20 20 28 28 28 28 75 36 34 29 31 29 3c  U32  ((((u64)1)<
49cb0 3c 33 32 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 4d  <32)-1)../*.** M
49cc0 61 63 72 6f 73 20 74 6f 20 64 65 74 65 72 6d 69  acros to determi
49cd0 6e 65 20 77 68 65 74 68 65 72 20 74 68 65 20 6d  ne whether the m
49ce0 61 63 68 69 6e 65 20 69 73 20 62 69 67 20 6f 72  achine is big or
49cf0 20 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e 2c 0a   little endian,.
49d00 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 61 74 20  ** evaluated at 
49d10 72 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64  runtime..*/.#ifd
49d20 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41  ef SQLITE_AMALGA
49d30 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52  MATION.SQLITE_PR
49d40 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20  IVATE const int 
49d50 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b 0a  sqlite3one = 1;.
49d60 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49  #else.SQLITE_PRI
49d70 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73  VATE const int s
49d80 71 6c 69 74 65 33 6f 6e 65 3b 0a 23 65 6e 64 69  qlite3one;.#endi
49d90 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 69 33  f.#if defined(i3
49da0 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  86) || defined(_
49db0 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69  _i386__) || defi
49dc0 6e 65 64 28 5f 4d 5f 49 58 38 36 29 5c 0a 20 20  ned(_M_IX86)\.  
49dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
49de0 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65             || de
49df0 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 29 20  fined(__x86_64) 
49e00 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36  || defined(__x86
49e10 5f 36 34 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20  _64__).# define 
49e20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e  SQLITE_BIGENDIAN
49e30 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 53      0.# define S
49e40 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49  QLITE_LITTLEENDI
49e50 41 4e 20 31 0a 23 20 64 65 66 69 6e 65 20 53 51  AN 1.# define SQ
49e60 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
49e70 20 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45    SQLITE_UTF16LE
49e80 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
49e90 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e  SQLITE_BIGENDIAN
49ea0 20 20 20 20 28 2a 28 63 68 61 72 20 2a 29 28 26      (*(char *)(&
49eb0 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d 30 29 0a  sqlite3one)==0).
49ec0 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
49ed0 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 28 2a 28  LITTLEENDIAN (*(
49ee0 63 68 61 72 20 2a 29 28 26 73 71 6c 69 74 65 33  char *)(&sqlite3
49ef0 6f 6e 65 29 3d 3d 31 29 0a 23 20 64 65 66 69 6e  one)==1).# defin
49f00 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  e SQLITE_UTF16NA
49f10 54 49 56 45 20 28 53 51 4c 49 54 45 5f 42 49 47  TIVE (SQLITE_BIG
49f20 45 4e 44 49 41 4e 3f 53 51 4c 49 54 45 5f 55 54  ENDIAN?SQLITE_UT
49f30 46 31 36 42 45 3a 53 51 4c 49 54 45 5f 55 54 46  F16BE:SQLITE_UTF
49f40 31 36 4c 45 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  16LE).#endif../*
49f50 0a 2a 2a 20 43 6f 6e 73 74 61 6e 74 73 20 66 6f  .** Constants fo
49f60 72 20 74 68 65 20 6c 61 72 67 65 73 74 20 61 6e  r the largest an
49f70 64 20 73 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69  d smallest possi
49f80 62 6c 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65  ble 64-bit signe
49f90 64 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54  d integers..** T
49fa0 68 65 73 65 20 6d 61 63 72 6f 73 20 61 72 65 20  hese macros are 
49fb0 64 65 73 69 67 6e 65 64 20 74 6f 20 77 6f 72 6b  designed to work
49fc0 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 62 6f   correctly on bo
49fd0 74 68 20 33 32 2d 62 69 74 20 61 6e 64 20 36 34  th 32-bit and 64
49fe0 2d 62 69 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72  -bit.** compiler
49ff0 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 41  s..*/.#define LA
4a000 52 47 45 53 54 5f 49 4e 54 36 34 20 20 28 30 78  RGEST_INT64  (0x
4a010 66 66 66 66 66 66 66 66 7c 28 28 28 69 36 34 29  ffffffff|(((i64)
4a020 30 78 37 66 66 66 66 66 66 66 29 3c 3c 33 32 29  0x7fffffff)<<32)
4a030 29 0a 23 64 65 66 69 6e 65 20 53 4d 41 4c 4c 45  ).#define SMALLE
4a040 53 54 5f 49 4e 54 36 34 20 28 28 28 69 36 34 29  ST_INT64 (((i64)
4a050 2d 31 29 20 2d 20 4c 41 52 47 45 53 54 5f 49 4e  -1) - LARGEST_IN
4a060 54 36 34 29 0a 0a 2f 2a 20 0a 2a 2a 20 52 6f 75  T64)../* .** Rou
4a070 6e 64 20 75 70 20 61 20 6e 75 6d 62 65 72 20 74  nd up a number t
4a080 6f 20 74 68 65 20 6e 65 78 74 20 6c 61 72 67 65  o the next large
4a090 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 2e  r multiple of 8.
4a0a0 20 20 54 68 69 73 20 69 73 20 75 73 65 64 0a 2a    This is used.*
4a0b0 2a 20 74 6f 20 66 6f 72 63 65 20 38 2d 62 79 74  * to force 8-byt
4a0c0 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 6e 20 36  e alignment on 6
4a0d0 34 2d 62 69 74 20 61 72 63 68 69 74 65 63 74 75  4-bit architectu
4a0e0 72 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  res..*/.#define 
4a0f0 52 4f 55 4e 44 38 28 78 29 20 20 20 20 20 28 28  ROUND8(x)     ((
4a100 28 78 29 2b 37 29 26 7e 37 29 0a 0a 2f 2a 0a 2a  (x)+7)&~7)../*.*
4a110 2a 20 52 6f 75 6e 64 20 64 6f 77 6e 20 74 6f 20  * Round down to 
4a120 74 68 65 20 6e 65 61 72 65 73 74 20 6d 75 6c 74  the nearest mult
4a130 69 70 6c 65 20 6f 66 20 38 0a 2a 2f 0a 23 64 65  iple of 8.*/.#de
4a140 66 69 6e 65 20 52 4f 55 4e 44 44 4f 57 4e 38 28  fine ROUNDDOWN8(
4a150 78 29 20 28 28 78 29 26 7e 37 29 0a 0a 2f 2a 0a  x) ((x)&~7)../*.
4a160 2a 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74  ** Assert that t
4a170 68 65 20 70 6f 69 6e 74 65 72 20 58 20 69 73 20  he pointer X is 
4a180 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d  aligned to an 8-
4a190 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 0a 2a  byte boundary..*
4a1a0 2f 0a 23 64 65 66 69 6e 65 20 45 49 47 48 54 5f  /.#define EIGHT_
4a1b0 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 58  BYTE_ALIGNMENT(X
4a1c0 29 20 20 20 28 28 28 28 63 68 61 72 2a 29 28 58  )   ((((char*)(X
4a1d0 29 20 2d 20 28 63 68 61 72 2a 29 30 29 26 37 29  ) - (char*)0)&7)
4a1e0 3d 3d 30 29 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  ==0).../*.** An 
4a1f0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
4a200 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
4a210 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 73  ure is used to s
4a220 74 6f 72 65 20 74 68 65 20 62 75 73 79 2d 68 61  tore the busy-ha
4a230 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63  ndler.** callbac
4a240 6b 20 66 6f 72 20 61 20 67 69 76 65 6e 20 73 71  k for a given sq
4a250 6c 69 74 65 20 68 61 6e 64 6c 65 2e 20 0a 2a 2a  lite handle. .**
4a260 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 62  .** The sqlite.b
4a270 75 73 79 48 61 6e 64 6c 65 72 20 6d 65 6d 62 65  usyHandler membe
4a280 72 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 20  r of the sqlite 
4a290 73 74 72 75 63 74 20 63 6f 6e 74 61 69 6e 73 20  struct contains 
4a2a0 74 68 65 20 62 75 73 79 0a 2a 2a 20 63 61 6c 6c  the busy.** call
4a2b0 62 61 63 6b 20 66 6f 72 20 74 68 65 20 64 61 74  back for the dat
4a2c0 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 45 61  abase handle. Ea
4a2d0 63 68 20 70 61 67 65 72 20 6f 70 65 6e 65 64 20  ch pager opened 
4a2e0 76 69 61 20 74 68 65 20 73 71 6c 69 74 65 0a 2a  via the sqlite.*
4a2f0 2a 20 68 61 6e 64 6c 65 20 69 73 20 70 61 73 73  * handle is pass
4a300 65 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ed a pointer to 
4a310 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e 64 6c  sqlite.busyHandl
4a320 65 72 2e 20 54 68 65 20 62 75 73 79 2d 68 61 6e  er. The busy-han
4a330 64 6c 65 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b  dler.** callback
4a340 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e   is currently in
4a350 76 6f 6b 65 64 20 6f 6e 6c 79 20 66 72 6f 6d 20  voked only from 
4a360 77 69 74 68 69 6e 20 70 61 67 65 72 2e 63 2e 0a  within pager.c..
4a370 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
4a380 74 20 42 75 73 79 48 61 6e 64 6c 65 72 20 42 75  t BusyHandler Bu
4a390 73 79 48 61 6e 64 6c 65 72 3b 0a 73 74 72 75 63  syHandler;.struc
4a3a0 74 20 42 75 73 79 48 61 6e 64 6c 65 72 20 7b 0a  t BusyHandler {.
4a3b0 20 20 69 6e 74 20 28 2a 78 46 75 6e 63 29 28 76    int (*xFunc)(v
4a3c0 6f 69 64 20 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20  oid *,int);  /* 
4a3d0 54 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63  The busy callbac
4a3e0 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72  k */.  void *pAr
4a3f0 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
4a400 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 20 74    /* First arg t
4a410 6f 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20  o busy callback 
4a420 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 73 79 3b 20  */.  int nBusy; 
4a430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a440 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 65 64 20 77  /* Incremented w
4a450 69 74 68 20 65 61 63 68 20 62 75 73 79 20 63 61  ith each busy ca
4a460 6c 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ll */.};../*.** 
4a470 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 61 73 74  Name of the mast
4a480 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  er database tabl
4a490 65 2e 20 20 54 68 65 20 6d 61 73 74 65 72 20 64  e.  The master d
4a4a0 61 74 61 62 61 73 65 20 74 61 62 6c 65 0a 2a 2a  atabase table.**
4a4b0 20 69 73 20 61 20 73 70 65 63 69 61 6c 20 74 61   is a special ta
4a4c0 62 6c 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74  ble that holds t
4a4d0 68 65 20 6e 61 6d 65 73 20 61 6e 64 20 61 74 74  he names and att
4a4e0 72 69 62 75 74 65 73 20 6f 66 20 61 6c 6c 0a 2a  ributes of all.*
4a4f0 2a 20 75 73 65 72 20 74 61 62 6c 65 73 20 61 6e  * user tables an
4a500 64 20 69 6e 64 69 63 65 73 2e 0a 2a 2f 0a 23 64  d indices..*/.#d
4a510 65 66 69 6e 65 20 4d 41 53 54 45 52 5f 4e 41 4d  efine MASTER_NAM
4a520 45 20 20 20 20 20 20 20 22 73 71 6c 69 74 65 5f  E       "sqlite_
4a530 6d 61 73 74 65 72 22 0a 23 64 65 66 69 6e 65 20  master".#define 
4a540 54 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41 4d 45  TEMP_MASTER_NAME
4a550 20 20 22 73 71 6c 69 74 65 5f 74 65 6d 70 5f 6d    "sqlite_temp_m
4a560 61 73 74 65 72 22 0a 0a 2f 2a 0a 2a 2a 20 54 68  aster"../*.** Th
4a570 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20 74  e root-page of t
4a580 68 65 20 6d 61 73 74 65 72 20 64 61 74 61 62 61  he master databa
4a590 73 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65  se table..*/.#de
4a5a0 66 69 6e 65 20 4d 41 53 54 45 52 5f 52 4f 4f 54  fine MASTER_ROOT
4a5b0 20 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20         1../*.** 
4a5c0 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
4a5d0 73 63 68 65 6d 61 20 74 61 62 6c 65 2e 0a 2a 2f  schema table..*/
4a5e0 0a 23 64 65 66 69 6e 65 20 53 43 48 45 4d 41 5f  .#define SCHEMA_
4a5f0 54 41 42 4c 45 28 78 29 20 20 28 28 21 4f 4d 49  TABLE(x)  ((!OMI
4a600 54 5f 54 45 4d 50 44 42 29 26 26 28 78 3d 3d 31  T_TEMPDB)&&(x==1
4a610 29 3f 54 45 4d 50 5f 4d 41 53 54 45 52 5f 4e 41  )?TEMP_MASTER_NA
4a620 4d 45 3a 4d 41 53 54 45 52 5f 4e 41 4d 45 29 0a  ME:MASTER_NAME).
4a630 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6e 76 65 6e 69  ./*.** A conveni
4a640 65 6e 63 65 20 6d 61 63 72 6f 20 74 68 61 74 20  ence macro that 
4a650 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
4a660 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69  er of elements i
4a670 6e 0a 2a 2a 20 61 6e 20 61 72 72 61 79 2e 0a 2a  n.** an array..*
4a680 2f 0a 23 64 65 66 69 6e 65 20 41 72 72 61 79 53  /.#define ArrayS
4a690 69 7a 65 28 58 29 20 20 20 20 28 28 69 6e 74 29  ize(X)    ((int)
4a6a0 28 73 69 7a 65 6f 66 28 58 29 2f 73 69 7a 65 6f  (sizeof(X)/sizeo
4a6b0 66 28 58 5b 30 5d 29 29 29 0a 0a 2f 2a 0a 2a 2a  f(X[0])))../*.**
4a6c0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76   The following v
4a6d0 61 6c 75 65 20 61 73 20 61 20 64 65 73 74 72 75  alue as a destru
4a6e0 63 74 6f 72 20 6d 65 61 6e 73 20 74 6f 20 75 73  ctor means to us
4a6f0 65 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28  e sqlite3DbFree(
4a700 29 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e  )..** This is an
4a710 20 69 6e 74 65 72 6e 61 6c 20 65 78 74 65 6e 73   internal extens
4a720 69 6f 6e 20 74 6f 20 53 51 4c 49 54 45 5f 53 54  ion to SQLITE_ST
4a730 41 54 49 43 20 61 6e 64 20 53 51 4c 49 54 45 5f  ATIC and SQLITE_
4a740 54 52 41 4e 53 49 45 4e 54 2e 0a 2a 2f 0a 23 64  TRANSIENT..*/.#d
4a750 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 59 4e  efine SQLITE_DYN
4a760 41 4d 49 43 20 20 20 28 28 73 71 6c 69 74 65 33  AMIC   ((sqlite3
4a770 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65  _destructor_type
4a780 29 73 71 6c 69 74 65 33 44 62 46 72 65 65 29 0a  )sqlite3DbFree).
4a790 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 49  ./*.** When SQLI
4a7a0 54 45 5f 4f 4d 49 54 5f 57 53 44 20 69 73 20 64  TE_OMIT_WSD is d
4a7b0 65 66 69 6e 65 64 2c 20 69 74 20 6d 65 61 6e 73  efined, it means
4a7c0 20 74 68 61 74 20 74 68 65 20 74 61 72 67 65 74   that the target
4a7d0 20 70 6c 61 74 66 6f 72 6d 20 64 6f 65 73 0a 2a   platform does.*
4a7e0 2a 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 57 72  * not support Wr
4a7f0 69 74 61 62 6c 65 20 53 74 61 74 69 63 20 44 61  itable Static Da
4a800 74 61 20 28 57 53 44 29 20 73 75 63 68 20 61 73  ta (WSD) such as
4a810 20 67 6c 6f 62 61 6c 20 61 6e 64 20 73 74 61 74   global and stat
4a820 69 63 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a  ic variables..**
4a830 20 41 6c 6c 20 76 61 72 69 61 62 6c 65 73 20 6d   All variables m
4a840 75 73 74 20 65 69 74 68 65 72 20 62 65 20 6f 6e  ust either be on
4a850 20 74 68 65 20 73 74 61 63 6b 20 6f 72 20 64 79   the stack or dy
4a860 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
4a870 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20  ted from.** the 
4a880 68 65 61 70 2e 20 20 57 68 65 6e 20 57 53 44 20  heap.  When WSD 
4a890 69 73 20 75 6e 73 75 70 70 6f 72 74 65 64 2c 20  is unsupported, 
4a8a0 74 68 65 20 76 61 72 69 61 62 6c 65 20 64 65 63  the variable dec
4a8b0 6c 61 72 61 74 69 6f 6e 73 20 73 63 61 74 74 65  larations scatte
4a8c0 72 65 64 0a 2a 2a 20 74 68 72 6f 75 67 68 6f 75  red.** throughou
4a8d0 74 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f 64  t the SQLite cod
4a8e0 65 20 6d 75 73 74 20 62 65 63 6f 6d 65 20 63 6f  e must become co
4a8f0 6e 73 74 61 6e 74 73 20 69 6e 73 74 65 61 64 2e  nstants instead.
4a900 20 20 54 68 65 20 53 51 4c 49 54 45 5f 57 53 44    The SQLITE_WSD
4a910 0a 2a 2a 20 6d 61 63 72 6f 20 69 73 20 75 73 65  .** macro is use
4a920 64 20 66 6f 72 20 74 68 69 73 20 70 75 72 70 6f  d for this purpo
4a930 73 65 2e 20 20 41 6e 64 20 69 6e 73 74 65 61 64  se.  And instead
4a940 20 6f 66 20 72 65 66 65 72 65 6e 63 69 6e 67 20   of referencing 
4a950 74 68 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20  the variable.** 
4a960 64 69 72 65 63 74 6c 79 2c 20 77 65 20 75 73 65  directly, we use
4a970 20 69 74 73 20 63 6f 6e 73 74 61 6e 74 20 61 73   its constant as
4a980 20 61 20 6b 65 79 20 74 6f 20 6c 6f 6f 6b 75 70   a key to lookup
4a990 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20 61 6c   the run-time al
4a9a0 6c 6f 63 61 74 65 64 0a 2a 2a 20 62 75 66 66 65  located.** buffe
4a9b0 72 20 74 68 61 74 20 68 6f 6c 64 73 20 72 65 61  r that holds rea
4a9c0 6c 20 76 61 72 69 61 62 6c 65 2e 20 20 54 68 65  l variable.  The
4a9d0 20 63 6f 6e 73 74 61 6e 74 20 69 73 20 61 6c 73   constant is als
4a9e0 6f 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 65  o the initialize
4a9f0 72 0a 2a 2a 20 66 6f 72 20 74 68 65 20 72 75 6e  r.** for the run
4aa00 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65 64 20  -time allocated 
4aa10 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e  buffer..**.** In
4aa20 20 74 68 65 20 75 73 75 61 6c 20 63 61 73 65 20   the usual case 
4aa30 77 68 65 72 65 20 57 53 44 20 69 73 20 73 75 70  where WSD is sup
4aa40 70 6f 72 74 65 64 2c 20 74 68 65 20 53 51 4c 49  ported, the SQLI
4aa50 54 45 5f 57 53 44 20 61 6e 64 20 47 4c 4f 42 41  TE_WSD and GLOBA
4aa60 4c 0a 2a 2a 20 6d 61 63 72 6f 73 20 62 65 63 6f  L.** macros beco
4aa70 6d 65 20 6e 6f 2d 6f 70 73 20 61 6e 64 20 68 61  me no-ops and ha
4aa80 76 65 20 7a 65 72 6f 20 70 65 72 66 6f 72 6d 61  ve zero performa
4aa90 6e 63 65 20 69 6d 70 61 63 74 2e 0a 2a 2f 0a 23  nce impact..*/.#
4aaa0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
4aab0 54 5f 57 53 44 0a 20 20 23 64 65 66 69 6e 65 20  T_WSD.  #define 
4aac0 53 51 4c 49 54 45 5f 57 53 44 20 63 6f 6e 73 74  SQLITE_WSD const
4aad0 0a 20 20 23 64 65 66 69 6e 65 20 47 4c 4f 42 41  .  #define GLOBA
4aae0 4c 28 74 2c 76 29 20 28 2a 28 74 2a 29 73 71 6c  L(t,v) (*(t*)sql
4aaf0 69 74 65 33 5f 77 73 64 5f 66 69 6e 64 28 28 76  ite3_wsd_find((v
4ab00 6f 69 64 2a 29 26 28 76 29 2c 20 73 69 7a 65 6f  oid*)&(v), sizeo
4ab10 66 28 76 29 29 29 0a 20 20 23 64 65 66 69 6e 65  f(v))).  #define
4ab20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
4ab30 6e 66 69 67 20 47 4c 4f 42 41 4c 28 73 74 72 75  nfig GLOBAL(stru
4ab40 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67  ct Sqlite3Config
4ab50 2c 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 29  , sqlite3Config)
4ab60 0a 53 51 4c 49 54 45 5f 41 50 49 20 20 20 69 6e  .SQLITE_API   in
4ab70 74 20 73 71 6c 69 74 65 33 5f 77 73 64 5f 69 6e  t sqlite3_wsd_in
4ab80 69 74 28 69 6e 74 20 4e 2c 20 69 6e 74 20 4a 29  it(int N, int J)
4ab90 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 20 20 76  ;.SQLITE_API   v
4aba0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 77 73 64  oid *sqlite3_wsd
4abb0 5f 66 69 6e 64 28 76 6f 69 64 20 2a 4b 2c 20 69  _find(void *K, i
4abc0 6e 74 20 4c 29 3b 0a 23 65 6c 73 65 0a 20 20 23  nt L);.#else.  #
4abd0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 53  define SQLITE_WS
4abe0 44 20 0a 20 20 23 64 65 66 69 6e 65 20 47 4c 4f  D .  #define GLO
4abf0 42 41 4c 28 74 2c 76 29 20 76 0a 20 20 23 64 65  BAL(t,v) v.  #de
4ac00 66 69 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f 62  fine sqlite3Glob
4ac10 61 6c 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33  alConfig sqlite3
4ac20 43 6f 6e 66 69 67 0a 23 65 6e 64 69 66 0a 0a 2f  Config.#endif../
4ac30 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
4ac40 6e 67 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73  ng macros are us
4ac50 65 64 20 74 6f 20 73 75 70 70 72 65 73 73 20 63  ed to suppress c
4ac60 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73  ompiler warnings
4ac70 20 61 6e 64 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20   and to.** make 
4ac80 69 74 20 63 6c 65 61 72 20 74 6f 20 68 75 6d 61  it clear to huma
4ac90 6e 20 72 65 61 64 65 72 73 20 77 68 65 6e 20 61  n readers when a
4aca0 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65   function parame
4acb0 74 65 72 20 69 73 20 64 65 6c 69 62 65 72 61 74  ter is deliberat
4acc0 65 6c 79 20 0a 2a 2a 20 6c 65 66 74 20 75 6e 75  ely .** left unu
4acd0 73 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 62  sed within the b
4ace0 6f 64 79 20 6f 66 20 61 20 66 75 6e 63 74 69 6f  ody of a functio
4acf0 6e 2e 20 54 68 69 73 20 75 73 75 61 6c 6c 79 20  n. This usually 
4ad00 68 61 70 70 65 6e 73 20 77 68 65 6e 0a 2a 2a 20  happens when.** 
4ad10 61 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  a function is ca
4ad20 6c 6c 65 64 20 76 69 61 20 61 20 66 75 6e 63 74  lled via a funct
4ad30 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 20 46 6f 72  ion pointer. For
4ad40 20 65 78 61 6d 70 6c 65 20 74 68 65 20 0a 2a 2a   example the .**
4ad50 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
4ad60 6f 66 20 61 6e 20 53 51 4c 20 61 67 67 72 65 67  of an SQL aggreg
4ad70 61 74 65 20 73 74 65 70 20 63 61 6c 6c 62 61 63  ate step callbac
4ad80 6b 20 6d 61 79 20 6e 6f 74 20 75 73 65 20 74 68  k may not use th
4ad90 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69  e.** parameter i
4ada0 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 6e 75  ndicating the nu
4adb0 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
4adc0 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20  s passed to the 
4add0 61 67 67 72 65 67 61 74 65 2c 0a 2a 2a 20 69 66  aggregate,.** if
4ade0 20 69 74 20 6b 6e 6f 77 73 20 74 68 61 74 20 74   it knows that t
4adf0 68 69 73 20 69 73 20 65 6e 66 6f 72 63 65 64 20  his is enforced 
4ae00 65 6c 73 65 77 68 65 72 65 2e 0a 2a 2a 0a 2a 2a  elsewhere..**.**
4ae10 20 57 68 65 6e 20 61 20 66 75 6e 63 74 69 6f 6e   When a function
4ae20 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f   parameter is no
4ae30 74 20 75 73 65 64 20 61 74 20 61 6c 6c 20 77 69  t used at all wi
4ae40 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66  thin the body of
4ae50 20 61 20 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20   a function,.** 
4ae60 69 74 20 69 73 20 67 65 6e 65 72 61 6c 6c 79 20  it is generally 
4ae70 6e 61 6d 65 64 20 22 4e 6f 74 55 73 65 64 22 20  named "NotUsed" 
4ae80 6f 72 20 22 4e 6f 74 55 73 65 64 32 22 20 74 6f  or "NotUsed2" to
4ae90 20 6d 61 6b 65 20 74 68 69 6e 67 73 20 65 76 65   make things eve
4aea0 6e 20 63 6c 65 61 72 65 72 2e 0a 2a 2a 20 48 6f  n clearer..** Ho
4aeb0 77 65 76 65 72 2c 20 74 68 65 73 65 20 6d 61 63  wever, these mac
4aec0 72 6f 73 20 6d 61 79 20 61 6c 73 6f 20 62 65 20  ros may also be 
4aed0 75 73 65 64 20 74 6f 20 73 75 70 70 72 65 73 73  used to suppress
4aee0 20 77 61 72 6e 69 6e 67 73 20 72 65 6c 61 74 65   warnings relate
4aef0 64 20 74 6f 0a 2a 2a 20 70 61 72 61 6d 65 74 65  d to.** paramete
4af00 72 73 20 74 68 61 74 20 6d 61 79 20 6f 72 20 6d  rs that may or m
4af10 61 79 20 6e 6f 74 20 62 65 20 75 73 65 64 20 64  ay not be used d
4af20 65 70 65 6e 64 69 6e 67 20 6f 6e 20 63 6f 6d 70  epending on comp
4af30 69 6c 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 2e  ilation options.
4af40 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 20  .** For example 
4af50 74 68 6f 73 65 20 70 61 72 61 6d 65 74 65 72 73  those parameters
4af60 20 6f 6e 6c 79 20 75 73 65 64 20 69 6e 20 61 73   only used in as
4af70 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
4af80 73 2e 20 49 6e 20 74 68 65 73 65 0a 2a 2a 20 63  s. In these.** c
4af90 61 73 65 73 20 74 68 65 20 70 61 72 61 6d 65 74  ases the paramet
4afa0 65 72 73 20 61 72 65 20 6e 61 6d 65 64 20 61 73  ers are named as
4afb0 20 70 65 72 20 74 68 65 20 75 73 75 61 6c 20 63   per the usual c
4afc0 6f 6e 76 65 6e 74 69 6f 6e 73 2e 0a 2a 2f 0a 23  onventions..*/.#
4afd0 64 65 66 69 6e 65 20 55 4e 55 53 45 44 5f 50 41  define UNUSED_PA
4afe0 52 41 4d 45 54 45 52 28 78 29 20 28 76 6f 69 64  RAMETER(x) (void
4aff0 29 28 78 29 0a 23 64 65 66 69 6e 65 20 55 4e 55  )(x).#define UNU
4b000 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 78  SED_PARAMETER2(x
4b010 2c 79 29 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ,y) UNUSED_PARAM
4b020 45 54 45 52 28 78 29 2c 55 4e 55 53 45 44 5f 50  ETER(x),UNUSED_P
4b030 41 52 41 4d 45 54 45 52 28 79 29 0a 0a 2f 2a 0a  ARAMETER(y)../*.
4b040 2a 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72  ** Forward refer
4b050 65 6e 63 65 73 20 74 6f 20 73 74 72 75 63 74 75  ences to structu
4b060 72 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  res.*/.typedef s
4b070 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 41 67  truct AggInfo Ag
4b080 67 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73  gInfo;.typedef s
4b090 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74 65 78  truct AuthContex
4b0a0 74 20 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 74  t AuthContext;.t
4b0b0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 41 75  ypedef struct Au
4b0c0 74 6f 69 6e 63 49 6e 66 6f 20 41 75 74 6f 69 6e  toincInfo Autoin
4b0d0 63 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73  cInfo;.typedef s
4b0e0 74 72 75 63 74 20 42 69 74 76 65 63 20 42 69 74  truct Bitvec Bit
4b0f0 76 65 63 3b 0a 74 79 70 65 64 65 66 20 73 74 72  vec;.typedef str
4b100 75 63 74 20 52 6f 77 53 65 74 20 52 6f 77 53 65  uct RowSet RowSe
4b110 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  t;.typedef struc
4b120 74 20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c 53 65  t CollSeq CollSe
4b130 71 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  q;.typedef struc
4b140 74 20 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d 6e 3b  t Column Column;
4b150 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b160 44 62 20 44 62 3b 0a 74 79 70 65 64 65 66 20 73  Db Db;.typedef s
4b170 74 72 75 63 74 20 53 63 68 65 6d 61 20 53 63 68  truct Schema Sch
4b180 65 6d 61 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ema;.typedef str
4b190 75 63 74 20 45 78 70 72 20 45 78 70 72 3b 0a 74  uct Expr Expr;.t
4b1a0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78  ypedef struct Ex
4b1b0 70 72 4c 69 73 74 20 45 78 70 72 4c 69 73 74 3b  prList ExprList;
4b1c0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b1d0 45 78 70 72 53 70 61 6e 20 45 78 70 72 53 70 61  ExprSpan ExprSpa
4b1e0 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  n;.typedef struc
4b1f0 74 20 46 4b 65 79 20 46 4b 65 79 3b 0a 74 79 70  t FKey FKey;.typ
4b200 65 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63  edef struct Func
4b210 44 65 66 20 46 75 6e 63 44 65 66 3b 0a 74 79 70  Def FuncDef;.typ
4b220 65 64 65 66 20 73 74 72 75 63 74 20 46 75 6e 63  edef struct Func
4b230 44 65 66 48 61 73 68 20 46 75 6e 63 44 65 66 48  DefHash FuncDefH
4b240 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ash;.typedef str
4b250 75 63 74 20 49 64 4c 69 73 74 20 49 64 4c 69 73  uct IdList IdLis
4b260 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  t;.typedef struc
4b270 74 20 49 6e 64 65 78 20 49 6e 64 65 78 3b 0a 74  t Index Index;.t
4b280 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e  ypedef struct In
4b290 64 65 78 53 61 6d 70 6c 65 20 49 6e 64 65 78 53  dexSample IndexS
4b2a0 61 6d 70 6c 65 3b 0a 74 79 70 65 64 65 66 20 73  ample;.typedef s
4b2b0 74 72 75 63 74 20 4b 65 79 43 6c 61 73 73 20 4b  truct KeyClass K
4b2c0 65 79 43 6c 61 73 73 3b 0a 74 79 70 65 64 65 66  eyClass;.typedef
4b2d0 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20   struct KeyInfo 
4b2e0 4b 65 79 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66  KeyInfo;.typedef
4b2f0 20 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64   struct Lookasid
4b300 65 20 4c 6f 6f 6b 61 73 69 64 65 3b 0a 74 79 70  e Lookaside;.typ
4b310 65 64 65 66 20 73 74 72 75 63 74 20 4c 6f 6f 6b  edef struct Look
4b320 61 73 69 64 65 53 6c 6f 74 20 4c 6f 6f 6b 61 73  asideSlot Lookas
4b330 69 64 65 53 6c 6f 74 3b 0a 74 79 70 65 64 65 66  ideSlot;.typedef
4b340 20 73 74 72 75 63 74 20 4d 6f 64 75 6c 65 20 4d   struct Module M
4b350 6f 64 75 6c 65 3b 0a 74 79 70 65 64 65 66 20 73  odule;.typedef s
4b360 74 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78  truct NameContex
4b370 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 3b 0a 74  t NameContext;.t
4b380 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61  ypedef struct Pa
4b390 72 73 65 20 50 61 72 73 65 3b 0a 74 79 70 65 64  rse Parse;.typed
4b3a0 65 66 20 73 74 72 75 63 74 20 53 61 76 65 70 6f  ef struct Savepo
4b3b0 69 6e 74 20 53 61 76 65 70 6f 69 6e 74 3b 0a 74  int Savepoint;.t
4b3c0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 65  ypedef struct Se
4b3d0 6c 65 63 74 20 53 65 6c 65 63 74 3b 0a 74 79 70  lect Select;.typ
4b3e0 65 64 65 66 20 73 74 72 75 63 74 20 53 72 63 4c  edef struct SrcL
4b3f0 69 73 74 20 53 72 63 4c 69 73 74 3b 0a 74 79 70  ist SrcList;.typ
4b400 65 64 65 66 20 73 74 72 75 63 74 20 53 74 72 41  edef struct StrA
4b410 63 63 75 6d 20 53 74 72 41 63 63 75 6d 3b 0a 74  ccum StrAccum;.t
4b420 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 61  ypedef struct Ta
4b430 62 6c 65 20 54 61 62 6c 65 3b 0a 74 79 70 65 64  ble Table;.typed
4b440 65 66 20 73 74 72 75 63 74 20 54 61 62 6c 65 4c  ef struct TableL
4b450 6f 63 6b 20 54 61 62 6c 65 4c 6f 63 6b 3b 0a 74  ock TableLock;.t
4b460 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54 6f  ypedef struct To
4b470 6b 65 6e 20 54 6f 6b 65 6e 3b 0a 74 79 70 65 64  ken Token;.typed
4b480 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65  ef struct Trigge
4b490 72 50 72 67 20 54 72 69 67 67 65 72 50 72 67 3b  rPrg TriggerPrg;
4b4a0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b4b0 54 72 69 67 67 65 72 53 74 65 70 20 54 72 69 67  TriggerStep Trig
4b4c0 67 65 72 53 74 65 70 3b 0a 74 79 70 65 64 65 66  gerStep;.typedef
4b4d0 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20   struct Trigger 
4b4e0 54 72 69 67 67 65 72 3b 0a 74 79 70 65 64 65 66  Trigger;.typedef
4b4f0 20 73 74 72 75 63 74 20 55 6e 70 61 63 6b 65 64   struct Unpacked
4b500 52 65 63 6f 72 64 20 55 6e 70 61 63 6b 65 64 52  Record UnpackedR
4b510 65 63 6f 72 64 3b 0a 74 79 70 65 64 65 66 20 73  ecord;.typedef s
4b520 74 72 75 63 74 20 56 54 61 62 6c 65 20 56 54 61  truct VTable VTa
4b530 62 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ble;.typedef str
4b540 75 63 74 20 57 61 6c 6b 65 72 20 57 61 6c 6b 65  uct Walker Walke
4b550 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  r;.typedef struc
4b560 74 20 57 68 65 72 65 50 6c 61 6e 20 57 68 65 72  t WherePlan Wher
4b570 65 50 6c 61 6e 3b 0a 74 79 70 65 64 65 66 20 73  ePlan;.typedef s
4b580 74 72 75 63 74 20 57 68 65 72 65 49 6e 66 6f 20  truct WhereInfo 
4b590 57 68 65 72 65 49 6e 66 6f 3b 0a 74 79 70 65 64  WhereInfo;.typed
4b5a0 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 4c  ef struct WhereL
4b5b0 65 76 65 6c 20 57 68 65 72 65 4c 65 76 65 6c 3b  evel WhereLevel;
4b5c0 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72 20 73 6f  ../*.** Defer so
4b5d0 75 72 63 69 6e 67 20 76 64 62 65 2e 68 20 61 6e  urcing vdbe.h an
4b5e0 64 20 62 74 72 65 65 2e 68 20 75 6e 74 69 6c 20  d btree.h until 
4b5f0 61 66 74 65 72 20 74 68 65 20 22 75 38 22 20 61  after the "u8" a
4b600 6e 64 20 0a 2a 2a 20 22 42 75 73 79 48 61 6e 64  nd .** "BusyHand
4b610 6c 65 72 22 20 74 79 70 65 64 65 66 73 2e 20 76  ler" typedefs. v
4b620 64 62 65 2e 68 20 61 6c 73 6f 20 72 65 71 75 69  dbe.h also requi
4b630 72 65 73 20 61 20 66 65 77 20 6f 66 20 74 68 65  res a few of the
4b640 20 6f 70 61 71 75 65 0a 2a 2a 20 70 6f 69 6e 74   opaque.** point
4b650 65 72 20 74 79 70 65 73 20 28 69 2e 65 2e 20 46  er types (i.e. F
4b660 75 6e 63 44 65 66 29 20 64 65 66 69 6e 65 64 20  uncDef) defined 
4b670 61 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a  above..*/./*****
4b680 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
4b690 65 20 62 74 72 65 65 2e 68 20 69 6e 20 74 68 65  e btree.h in the
4b6a0 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74   middle of sqlit
4b6b0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
4b6c0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
4b6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
4b6e0 66 69 6c 65 20 62 74 72 65 65 2e 68 20 2a 2a 2a  file btree.h ***
4b6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b710 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
4b720 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
4b730 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
4b740 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
4b750 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
4b760 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
4b770 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
4b780 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
4b790 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
4b7a0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
4b7b0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
4b7c0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
4b7d0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
4b7e0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
4b7f0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
4b800 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
4b810 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
4b820 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
4b830 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
4b840 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
4b850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b890 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20  .** This header 
4b8a0 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65  file defines the
4b8b0 20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20   interface that 
4b8c0 74 68 65 20 73 71 6c 69 74 65 20 42 2d 54 72 65  the sqlite B-Tre
4b8d0 65 20 66 69 6c 65 0a 2a 2a 20 73 75 62 73 79 73  e file.** subsys
4b8e0 74 65 6d 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e  tem.  See commen
4b8f0 74 73 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65  ts in the source
4b900 20 63 6f 64 65 20 66 6f 72 20 61 20 64 65 74 61   code for a deta
4b910 69 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e  iled description
4b920 0a 2a 2a 20 6f 66 20 77 68 61 74 20 65 61 63 68  .** of what each
4b930 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69   interface routi
4b940 6e 65 20 64 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 40  ne does..**.** @
4b950 28 23 29 20 24 49 64 3a 20 62 74 72 65 65 2e 68  (#) $Id: btree.h
4b960 2c 76 20 31 2e 31 32 30 20 32 30 30 39 2f 30 37  ,v 1.120 2009/07
4b970 2f 32 32 20 30 30 3a 33 35 3a 32 34 20 64 72 68  /22 00:35:24 drh
4b980 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65   Exp $.*/.#ifnde
4b990 66 20 5f 42 54 52 45 45 5f 48 5f 0a 23 64 65 66  f _BTREE_H_.#def
4b9a0 69 6e 65 20 5f 42 54 52 45 45 5f 48 5f 0a 0a 2f  ine _BTREE_H_../
4b9b0 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 64 65 66  * TODO: This def
4b9c0 69 6e 69 74 69 6f 6e 20 69 73 20 6a 75 73 74 20  inition is just 
4b9d0 69 6e 63 6c 75 64 65 64 20 73 6f 20 6f 74 68 65  included so othe
4b9e0 72 20 6d 6f 64 75 6c 65 73 20 63 6f 6d 70 69 6c  r modules compil
4b9f0 65 2e 20 49 74 0a 2a 2a 20 6e 65 65 64 73 20 74  e. It.** needs t
4ba00 6f 20 62 65 20 72 65 76 69 73 69 74 65 64 2e 0a  o be revisited..
4ba10 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
4ba20 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41 20 31  E_N_BTREE_META 1
4ba30 30 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 64 65 66 69  0../*.** If defi
4ba40 6e 65 64 20 61 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  ned as non-zero,
4ba50 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20   auto-vacuum is 
4ba60 65 6e 61 62 6c 65 64 20 62 79 20 64 65 66 61 75  enabled by defau
4ba70 6c 74 2e 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a  lt. Otherwise.**
4ba80 20 69 74 20 6d 75 73 74 20 62 65 20 74 75 72 6e   it must be turn
4ba90 65 64 20 6f 6e 20 66 6f 72 20 65 61 63 68 20 64  ed on for each d
4baa0 61 74 61 62 61 73 65 20 75 73 69 6e 67 20 22 50  atabase using "P
4bab0 52 41 47 4d 41 20 61 75 74 6f 5f 76 61 63 75 75  RAGMA auto_vacuu
4bac0 6d 20 3d 20 31 22 2e 0a 2a 2f 0a 23 69 66 6e 64  m = 1"..*/.#ifnd
4bad0 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ef SQLITE_DEFAUL
4bae0 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 23  T_AUTOVACUUM.  #
4baf0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
4bb00 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d  FAULT_AUTOVACUUM
4bb10 20 30 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69   0.#endif..#defi
4bb20 6e 65 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43  ne BTREE_AUTOVAC
4bb30 55 55 4d 5f 4e 4f 4e 45 20 30 20 20 20 20 20 20  UUM_NONE 0      
4bb40 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 6f 20 61    /* Do not do a
4bb50 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a 23 64  uto-vacuum */.#d
4bb60 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55 54 4f  efine BTREE_AUTO
4bb70 56 41 43 55 55 4d 5f 46 55 4c 4c 20 31 20 20 20  VACUUM_FULL 1   
4bb80 20 20 20 20 20 2f 2a 20 44 6f 20 66 75 6c 6c 20       /* Do full 
4bb90 61 75 74 6f 2d 76 61 63 75 75 6d 20 2a 2f 0a 23  auto-vacuum */.#
4bba0 64 65 66 69 6e 65 20 42 54 52 45 45 5f 41 55 54  define BTREE_AUT
4bbb0 4f 56 41 43 55 55 4d 5f 49 4e 43 52 20 32 20 20  OVACUUM_INCR 2  
4bbc0 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65        /* Increme
4bbd0 6e 74 61 6c 20 76 61 63 75 75 6d 20 2a 2f 0a 0a  ntal vacuum */..
4bbe0 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20 64 65  /*.** Forward de
4bbf0 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 74  clarations of st
4bc00 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70 65 64  ructure.*/.typed
4bc10 65 66 20 73 74 72 75 63 74 20 42 74 72 65 65 20  ef struct Btree 
4bc20 42 74 72 65 65 3b 0a 74 79 70 65 64 65 66 20 73  Btree;.typedef s
4bc30 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 20 42  truct BtCursor B
4bc40 74 43 75 72 73 6f 72 3b 0a 74 79 70 65 64 65 66  tCursor;.typedef
4bc50 20 73 74 72 75 63 74 20 42 74 53 68 61 72 65 64   struct BtShared
4bc60 20 42 74 53 68 61 72 65 64 3b 0a 74 79 70 65 64   BtShared;.typed
4bc70 65 66 20 73 74 72 75 63 74 20 42 74 72 65 65 4d  ef struct BtreeM
4bc80 75 74 65 78 41 72 72 61 79 20 42 74 72 65 65 4d  utexArray BtreeM
4bc90 75 74 65 78 41 72 72 61 79 3b 0a 0a 2f 2a 0a 2a  utexArray;../*.*
4bca0 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65  * This structure
4bcb0 20 72 65 63 6f 72 64 73 20 61 6c 6c 20 6f 66 20   records all of 
4bcc0 74 68 65 20 42 74 72 65 65 73 20 74 68 61 74 20  the Btrees that 
4bcd0 6e 65 65 64 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20  need to hold.** 
4bce0 61 20 6d 75 74 65 78 20 62 65 66 6f 72 65 20 77  a mutex before w
4bcf0 65 20 65 6e 74 65 72 20 73 71 6c 69 74 65 33 56  e enter sqlite3V
4bd00 64 62 65 45 78 65 63 28 29 2e 20 20 54 68 65 20  dbeExec().  The 
4bd10 42 74 72 65 65 73 20 61 72 65 0a 2a 2a 20 61 72  Btrees are.** ar
4bd20 65 20 70 6c 61 63 65 64 20 69 6e 20 61 42 74 72  e placed in aBtr
4bd30 65 65 5b 5d 20 69 6e 20 6f 72 64 65 72 20 6f 66  ee[] in order of
4bd40 20 61 42 74 72 65 65 5b 5d 2d 3e 70 42 74 2e 20   aBtree[]->pBt. 
4bd50 20 54 68 61 74 20 77 61 79 2c 0a 2a 2a 20 77 65   That way,.** we
4bd60 20 63 61 6e 20 61 6c 77 61 79 73 20 6c 6f 63 6b   can always lock
4bd70 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 6d   and unlock them
4bd80 20 61 6c 6c 20 71 75 69 63 6b 6c 79 2e 0a 2a 2f   all quickly..*/
4bd90 0a 73 74 72 75 63 74 20 42 74 72 65 65 4d 75 74  .struct BtreeMut
4bda0 65 78 41 72 72 61 79 20 7b 0a 20 20 69 6e 74 20  exArray {.  int 
4bdb0 6e 4d 75 74 65 78 3b 0a 20 20 42 74 72 65 65 20  nMutex;.  Btree 
4bdc0 2a 61 42 74 72 65 65 5b 53 51 4c 49 54 45 5f 4d  *aBtree[SQLITE_M
4bdd0 41 58 5f 41 54 54 41 43 48 45 44 2b 31 5d 3b 0a  AX_ATTACHED+1];.
4bde0 7d 3b 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  };...SQLITE_PRIV
4bdf0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4be00 74 72 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73  treeOpen(.  cons
4be10 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
4be20 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  e,   /* Name of 
4be30 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f  database file to
4be40 20 6f 70 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74   open */.  sqlit
4be50 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
4be60 20 20 20 20 2f 2a 20 41 73 73 6f 63 69 61 74 65      /* Associate
4be70 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  d database conne
4be80 63 74 69 6f 6e 20 2a 2f 0a 20 20 42 74 72 65 65  ction */.  Btree
4be90 20 2a 2a 70 70 42 74 72 65 65 2c 20 20 20 20 20   **ppBtree,     
4bea0 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 6f 70      /* Return op
4beb0 65 6e 20 42 74 72 65 65 2a 20 68 65 72 65 20 2a  en Btree* here *
4bec0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
4bed0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4bee0 46 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 76  Flags */.  int v
4bef0 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20  fsFlags         
4bf00 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73      /* Flags pas
4bf10 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 56  sed through to V
4bf20 46 53 20 6f 70 65 6e 20 2a 2f 0a 29 3b 0a 0a 2f  FS open */.);../
4bf30 2a 20 54 68 65 20 66 6c 61 67 73 20 70 61 72 61  * The flags para
4bf40 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33  meter to sqlite3
4bf50 42 74 72 65 65 4f 70 65 6e 20 63 61 6e 20 62 65  BtreeOpen can be
4bf60 20 74 68 65 20 62 69 74 77 69 73 65 20 6f 72 20   the bitwise or 
4bf70 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77  of the.** follow
4bf80 69 6e 67 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a  ing values..**.*
4bf90 2a 20 4e 4f 54 45 3a 20 20 54 68 65 73 65 20 76  * NOTE:  These v
4bfa0 61 6c 75 65 73 20 6d 75 73 74 20 6d 61 74 63 68  alues must match
4bfb0 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
4bfc0 6e 67 20 50 41 47 45 52 5f 20 76 61 6c 75 65 73  ng PAGER_ values
4bfd0 20 69 6e 0a 2a 2a 20 70 61 67 65 72 2e 68 2e 0a   in.** pager.h..
4bfe0 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45  */.#define BTREE
4bff0 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 20 31  _OMIT_JOURNAL  1
4c000 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20    /* Do not use 
4c010 6a 6f 75 72 6e 61 6c 2e 20 20 4e 6f 20 61 72 67  journal.  No arg
4c020 75 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65  ument */.#define
4c030 20 42 54 52 45 45 5f 4e 4f 5f 52 45 41 44 4c 4f   BTREE_NO_READLO
4c040 43 4b 20 20 20 32 20 20 2f 2a 20 4f 6d 69 74 20  CK   2  /* Omit 
4c050 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65 61  readlocks on rea
4c060 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a 23  donly files */.#
4c070 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4d 45 4d  define BTREE_MEM
4c080 4f 52 59 20 20 20 20 20 20 20 20 34 20 20 2f 2a  ORY        4  /*
4c090 20 49 6e 2d 6d 65 6d 6f 72 79 20 44 42 2e 20 20   In-memory DB.  
4c0a0 4e 6f 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 23  No argument */.#
4c0b0 64 65 66 69 6e 65 20 42 54 52 45 45 5f 52 45 41  define BTREE_REA
4c0c0 44 4f 4e 4c 59 20 20 20 20 20 20 38 20 20 2f 2a  DONLY      8  /*
4c0d0 20 4f 70 65 6e 20 74 68 65 20 64 61 74 61 62 61   Open the databa
4c0e0 73 65 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20  se in read-only 
4c0f0 6d 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  mode */.#define 
4c100 42 54 52 45 45 5f 52 45 41 44 57 52 49 54 45 20  BTREE_READWRITE 
4c110 20 20 20 31 36 20 20 2f 2a 20 4f 70 65 6e 20 66     16  /* Open f
4c120 6f 72 20 62 6f 74 68 20 72 65 61 64 69 6e 67 20  or both reading 
4c130 61 6e 64 20 77 72 69 74 69 6e 67 20 2a 2f 0a 23  and writing */.#
4c140 64 65 66 69 6e 65 20 42 54 52 45 45 5f 43 52 45  define BTREE_CRE
4c150 41 54 45 20 20 20 20 20 20 20 33 32 20 20 2f 2a  ATE       32  /*
4c160 20 43 72 65 61 74 65 20 74 68 65 20 64 61 74 61   Create the data
4c170 62 61 73 65 20 69 66 20 69 74 20 64 6f 65 73 20  base if it does 
4c180 6e 6f 74 20 65 78 69 73 74 20 2a 2f 0a 0a 53 51  not exist */..SQ
4c190 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4c1a0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f   sqlite3BtreeClo
4c1b0 73 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49  se(Btree*);.SQLI
4c1c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4c1d0 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61  qlite3BtreeSetCa
4c1e0 63 68 65 53 69 7a 65 28 42 74 72 65 65 2a 2c 69  cheSize(Btree*,i
4c1f0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
4c200 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4c210 74 72 65 65 53 65 74 53 61 66 65 74 79 4c 65 76  treeSetSafetyLev
4c220 65 6c 28 42 74 72 65 65 2a 2c 69 6e 74 2c 69 6e  el(Btree*,int,in
4c230 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
4c240 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
4c250 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28  reeSyncDisabled(
4c260 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Btree*);.SQLITE_
4c270 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4c280 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53  te3BtreeSetPageS
4c290 69 7a 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e  ize(Btree *p, in
4c2a0 74 20 6e 50 61 67 65 73 69 7a 65 2c 20 69 6e 74  t nPagesize, int
4c2b0 20 6e 52 65 73 65 72 76 65 2c 20 69 6e 74 20 65   nReserve, int e
4c2c0 46 69 78 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Fix);.SQLITE_PRI
4c2d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4c2e0 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65  BtreeGetPageSize
4c2f0 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
4c300 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4c310 69 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 65  ite3BtreeMaxPage
4c320 43 6f 75 6e 74 28 42 74 72 65 65 2a 2c 69 6e 74  Count(Btree*,int
4c330 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4c340 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4c350 65 65 47 65 74 52 65 73 65 72 76 65 28 42 74 72  eeGetReserve(Btr
4c360 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ee*);.SQLITE_PRI
4c370 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4c380 42 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 75  BtreeSetAutoVacu
4c390 75 6d 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 29  um(Btree *, int)
4c3a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4c3b0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4c3c0 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42  eGetAutoVacuum(B
4c3d0 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  tree *);.SQLITE_
4c3e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4c3f0 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61  te3BtreeBeginTra
4c400 6e 73 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a  ns(Btree*,int);.
4c410 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4c420 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
4c430 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 42 74  ommitPhaseOne(Bt
4c440 72 65 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  ree*, const char
4c450 20 2a 7a 4d 61 73 74 65 72 29 3b 0a 53 51 4c 49   *zMaster);.SQLI
4c460 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4c470 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
4c480 74 50 68 61 73 65 54 77 6f 28 42 74 72 65 65 2a  tPhaseTwo(Btree*
4c490 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4c4a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4c4b0 65 65 43 6f 6d 6d 69 74 28 42 74 72 65 65 2a 29  eeCommit(Btree*)
4c4c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4c4d0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4c4e0 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 2a  eRollback(Btree*
4c4f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4c500 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4c510 65 65 42 65 67 69 6e 53 74 6d 74 28 42 74 72 65  eeBeginStmt(Btre
4c520 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  e*,int);.SQLITE_
4c530 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4c540 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61  te3BtreeCreateTa
4c550 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2a  ble(Btree*, int*
4c560 2c 20 69 6e 74 20 66 6c 61 67 73 29 3b 0a 53 51  , int flags);.SQ
4c570 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4c580 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
4c590 6e 54 72 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a  nTrans(Btree*);.
4c5a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4c5b0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  nt sqlite3BtreeI
4c5c0 73 49 6e 52 65 61 64 54 72 61 6e 73 28 42 74 72  sInReadTrans(Btr
4c5d0 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ee*);.SQLITE_PRI
4c5e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4c5f0 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28  BtreeIsInBackup(
4c600 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Btree*);.SQLITE_
4c610 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
4c620 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61  lite3BtreeSchema
4c630 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20 76  (Btree *, int, v
4c640 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 29 29 3b  oid(*)(void *));
4c650 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4c660 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4c670 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28 42 74 72  SchemaLocked(Btr
4c680 65 65 20 2a 70 42 74 72 65 65 29 3b 0a 53 51 4c  ee *pBtree);.SQL
4c690 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4c6a0 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f 63 6b  sqlite3BtreeLock
4c6b0 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 42 74  Table(Btree *pBt
4c6c0 72 65 65 2c 20 69 6e 74 20 69 54 61 62 2c 20 75  ree, int iTab, u
4c6d0 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a  8 isWriteLock);.
4c6e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4c6f0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  nt sqlite3BtreeS
4c700 61 76 65 70 6f 69 6e 74 28 42 74 72 65 65 20 2a  avepoint(Btree *
4c710 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 53 51  , int, int);..SQ
4c720 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
4c730 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
4c740 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65  BtreeGetFilename
4c750 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54  (Btree *);.SQLIT
4c760 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
4c770 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72  char *sqlite3Btr
4c780 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65  eeGetJournalname
4c790 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54  (Btree *);.SQLIT
4c7a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4c7b0 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69  lite3BtreeCopyFi
4c7c0 6c 65 28 42 74 72 65 65 20 2a 2c 20 42 74 72 65  le(Btree *, Btre
4c7d0 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  e *);..SQLITE_PR
4c7e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4c7f0 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d  3BtreeIncrVacuum
4c800 28 42 74 72 65 65 20 2a 29 3b 0a 0a 2f 2a 20 54  (Btree *);../* T
4c810 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74  he flags paramet
4c820 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72  er to sqlite3Btr
4c830 65 65 43 72 65 61 74 65 54 61 62 6c 65 20 63 61  eeCreateTable ca
4c840 6e 20 62 65 20 74 68 65 20 62 69 74 77 69 73 65  n be the bitwise
4c850 20 4f 52 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f   OR.** of the fo
4c860 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 3a 0a 2a  llowing flags:.*
4c870 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  /.#define BTREE_
4c880 49 4e 54 4b 45 59 20 20 20 20 20 31 20 20 20 20  INTKEY     1    
4c890 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 6f 6e 6c  /* Table has onl
4c8a0 79 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20  y 64-bit signed 
4c8b0 69 6e 74 65 67 65 72 20 6b 65 79 73 20 2a 2f 0a  integer keys */.
4c8c0 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 5a 45  #define BTREE_ZE
4c8d0 52 4f 44 41 54 41 20 20 20 32 20 20 20 20 2f 2a  RODATA   2    /*
4c8e0 20 54 61 62 6c 65 20 68 61 73 20 6b 65 79 73 20   Table has keys 
4c8f0 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20 2a  only - no data *
4c900 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  /.#define BTREE_
4c910 4c 45 41 46 44 41 54 41 20 20 20 34 20 20 20 20  LEAFDATA   4    
4c920 2f 2a 20 44 61 74 61 20 73 74 6f 72 65 64 20 69  /* Data stored i
4c930 6e 20 6c 65 61 76 65 73 20 6f 6e 6c 79 2e 20 20  n leaves only.  
4c940 49 6d 70 6c 69 65 73 20 49 4e 54 4b 45 59 20 2a  Implies INTKEY *
4c950 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
4c960 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4c970 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 72 65  eeDropTable(Btre
4c980 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a  e*, int, int*);.
4c990 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4c9a0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
4c9b0 6c 65 61 72 54 61 62 6c 65 28 42 74 72 65 65 2a  learTable(Btree*
4c9c0 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51  , int, int*);.SQ
4c9d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
4c9e0 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72  d sqlite3BtreeTr
4c9f0 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 72  ipAllCursors(Btr
4ca00 65 65 2a 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c 49  ee*, int);..SQLI
4ca10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
4ca20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d  sqlite3BtreeGetM
4ca30 65 74 61 28 42 74 72 65 65 20 2a 70 42 74 72 65  eta(Btree *pBtre
4ca40 65 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20  e, int idx, u32 
4ca50 2a 70 56 61 6c 75 65 29 3b 0a 53 51 4c 49 54 45  *pValue);.SQLITE
4ca60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4ca70 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d  ite3BtreeUpdateM
4ca80 65 74 61 28 42 74 72 65 65 2a 2c 20 69 6e 74 20  eta(Btree*, int 
4ca90 69 64 78 2c 20 75 33 32 20 76 61 6c 75 65 29 3b  idx, u32 value);
4caa0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f  ../*.** The seco
4cab0 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  nd parameter to 
4cac0 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d  sqlite3BtreeGetM
4cad0 65 74 61 20 6f 72 20 73 71 6c 69 74 65 33 42 74  eta or sqlite3Bt
4cae0 72 65 65 55 70 64 61 74 65 4d 65 74 61 0a 2a 2a  reeUpdateMeta.**
4caf0 20 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 6f   should be one o
4cb00 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
4cb10 76 61 6c 75 65 73 2e 20 54 68 65 20 69 6e 74 65  values. The inte
4cb20 67 65 72 20 76 61 6c 75 65 73 20 61 72 65 20 61  ger values are a
4cb30 73 73 69 67 6e 65 64 20 0a 2a 2a 20 74 6f 20 63  ssigned .** to c
4cb40 6f 6e 73 74 61 6e 74 73 20 73 6f 20 74 68 61 74  onstants so that
4cb50 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74   the offset of t
4cb60 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
4cb70 20 66 69 65 6c 64 20 69 6e 20 61 6e 0a 2a 2a 20   field in an.** 
4cb80 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 20  SQLite database 
4cb90 68 65 61 64 65 72 20 6d 61 79 20 62 65 20 66 6f  header may be fo
4cba0 75 6e 64 20 75 73 69 6e 67 20 74 68 65 20 66 6f  und using the fo
4cbb0 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 75 6c 61 3a  llowing formula:
4cbc0 0a 2a 2a 0a 2a 2a 20 20 20 6f 66 66 73 65 74 20  .**.**   offset 
4cbd0 3d 20 33 36 20 2b 20 28 69 64 78 20 2a 20 34 29  = 36 + (idx * 4)
4cbe0 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70  .**.** For examp
4cbf0 6c 65 2c 20 74 68 65 20 66 72 65 65 2d 70 61 67  le, the free-pag
4cc00 65 2d 63 6f 75 6e 74 20 66 69 65 6c 64 20 69 73  e-count field is
4cc10 20 6c 6f 63 61 74 65 64 20 61 74 20 62 79 74 65   located at byte
4cc20 20 6f 66 66 73 65 74 20 33 36 20 6f 66 0a 2a 2a   offset 36 of.**
4cc30 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
4cc40 6c 65 20 68 65 61 64 65 72 2e 20 54 68 65 20 69  le header. The i
4cc50 6e 63 72 2d 76 61 63 75 75 6d 2d 66 6c 61 67 20  ncr-vacuum-flag 
4cc60 66 69 65 6c 64 20 69 73 20 6c 6f 63 61 74 65 64  field is located
4cc70 20 61 74 0a 2a 2a 20 62 79 74 65 20 6f 66 66 73   at.** byte offs
4cc80 65 74 20 36 34 20 28 3d 3d 20 33 36 2b 34 2a 37  et 64 (== 36+4*7
4cc90 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54  )..*/.#define BT
4cca0 52 45 45 5f 46 52 45 45 5f 50 41 47 45 5f 43 4f  REE_FREE_PAGE_CO
4ccb0 55 4e 54 20 20 20 20 20 30 0a 23 64 65 66 69 6e  UNT     0.#defin
4ccc0 65 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56  e BTREE_SCHEMA_V
4ccd0 45 52 53 49 4f 4e 20 20 20 20 20 20 31 0a 23 64  ERSION      1.#d
4cce0 65 66 69 6e 65 20 42 54 52 45 45 5f 46 49 4c 45  efine BTREE_FILE
4ccf0 5f 46 4f 52 4d 41 54 20 20 20 20 20 20 20 20 20  _FORMAT         
4cd00 32 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  2.#define BTREE_
4cd10 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49  DEFAULT_CACHE_SI
4cd20 5a 45 20 20 33 0a 23 64 65 66 69 6e 65 20 42 54  ZE  3.#define BT
4cd30 52 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54  REE_LARGEST_ROOT
4cd40 5f 50 41 47 45 20 20 20 34 0a 23 64 65 66 69 6e  _PAGE   4.#defin
4cd50 65 20 42 54 52 45 45 5f 54 45 58 54 5f 45 4e 43  e BTREE_TEXT_ENC
4cd60 4f 44 49 4e 47 20 20 20 20 20 20 20 35 0a 23 64  ODING       5.#d
4cd70 65 66 69 6e 65 20 42 54 52 45 45 5f 55 53 45 52  efine BTREE_USER
4cd80 5f 56 45 52 53 49 4f 4e 20 20 20 20 20 20 20 20  _VERSION        
4cd90 36 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  6.#define BTREE_
4cda0 49 4e 43 52 5f 56 41 43 55 55 4d 20 20 20 20 20  INCR_VACUUM     
4cdb0 20 20 20 20 37 0a 0a 53 51 4c 49 54 45 5f 50 52      7..SQLITE_PR
4cdc0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4cdd0 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a 20 20  3BtreeCursor(.  
4cde0 42 74 72 65 65 2a 2c 20 20 20 20 20 20 20 20 20  Btree*,         
4cdf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ce00 20 20 20 20 20 2f 2a 20 42 54 72 65 65 20 63 6f       /* BTree co
4ce10 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 20 74  ntaining table t
4ce20 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20  o open */.  int 
4ce30 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20  iTable,         
4ce40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ce50 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 6f 6f   /* Index of roo
4ce60 74 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  t page */.  int 
4ce70 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20  wrFlag,         
4ce80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ce90 20 2f 2a 20 31 20 66 6f 72 20 77 72 69 74 69 6e   /* 1 for writin
4cea0 67 2e 20 20 30 20 66 6f 72 20 72 65 61 64 2d 6f  g.  0 for read-o
4ceb0 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  nly */.  struct 
4cec0 4b 65 79 49 6e 66 6f 2a 2c 20 20 20 20 20 20 20  KeyInfo*,       
4ced0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4cee0 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   First argument 
4cef0 74 6f 20 63 6f 6d 70 61 72 65 20 66 75 6e 63 74  to compare funct
4cf00 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f  ion */.  BtCurso
4cf10 72 20 2a 70 43 75 72 73 6f 72 20 20 20 20 20 20  r *pCursor      
4cf20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4cf30 20 53 70 61 63 65 20 74 6f 20 77 72 69 74 65 20   Space to write 
4cf40 63 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65  cursor structure
4cf50 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52   */.);.SQLITE_PR
4cf60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4cf70 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65  3BtreeCursorSize
4cf80 28 76 6f 69 64 29 3b 0a 0a 53 51 4c 49 54 45 5f  (void);..SQLITE_
4cf90 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4cfa0 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72  te3BtreeCloseCur
4cfb0 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a  sor(BtCursor*);.
4cfc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4cfd0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  nt sqlite3BtreeM
4cfe0 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20  ovetoUnpacked(. 
4cff0 20 42 74 43 75 72 73 6f 72 2a 2c 0a 20 20 55 6e   BtCursor*,.  Un
4d000 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55  packedRecord *pU
4d010 6e 4b 65 79 2c 0a 20 20 69 36 34 20 69 6e 74 4b  nKey,.  i64 intK
4d020 65 79 2c 0a 20 20 69 6e 74 20 62 69 61 73 2c 0a  ey,.  int bias,.
4d030 20 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b 0a 53    int *pRes.);.S
4d040 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4d050 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  t sqlite3BtreeCu
4d060 72 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 43  rsorHasMoved(BtC
4d070 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53  ursor*, int*);.S
4d080 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4d090 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65  t sqlite3BtreeDe
4d0a0 6c 65 74 65 28 42 74 43 75 72 73 6f 72 2a 29 3b  lete(BtCursor*);
4d0b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d0c0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4d0d0 49 6e 73 65 72 74 28 42 74 43 75 72 73 6f 72 2a  Insert(BtCursor*
4d0e0 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  , const void *pK
4d0f0 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 0a 20 20  ey, i64 nKey,.  
4d100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d120 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74  const void *pDat
4d130 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20  a, int nData,.  
4d140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d160 69 6e 74 20 6e 5a 65 72 6f 2c 20 69 6e 74 20 62  int nZero, int b
4d170 69 61 73 2c 20 69 6e 74 20 73 65 65 6b 52 65 73  ias, int seekRes
4d180 75 6c 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ult);.SQLITE_PRI
4d190 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4d1a0 42 74 72 65 65 46 69 72 73 74 28 42 74 43 75 72  BtreeFirst(BtCur
4d1b0 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29  sor*, int *pRes)
4d1c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4d1d0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4d1e0 65 4c 61 73 74 28 42 74 43 75 72 73 6f 72 2a 2c  eLast(BtCursor*,
4d1f0 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c   int *pRes);.SQL
4d200 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4d210 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74  sqlite3BtreeNext
4d220 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20  (BtCursor*, int 
4d230 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50  *pRes);.SQLITE_P
4d240 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4d250 65 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 72  e3BtreeEof(BtCur
4d260 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  sor*);.SQLITE_PR
4d270 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4d280 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 42  3BtreePrevious(B
4d290 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70  tCursor*, int *p
4d2a0 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Res);.SQLITE_PRI
4d2b0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4d2c0 42 74 72 65 65 4b 65 79 53 69 7a 65 28 42 74 43  BtreeKeySize(BtC
4d2d0 75 72 73 6f 72 2a 2c 20 69 36 34 20 2a 70 53 69  ursor*, i64 *pSi
4d2e0 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ze);.SQLITE_PRIV
4d2f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4d300 74 72 65 65 4b 65 79 28 42 74 43 75 72 73 6f 72  treeKey(BtCursor
4d310 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75  *, u32 offset, u
4d320 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a  32 amt, void*);.
4d330 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
4d340 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
4d350 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28  e3BtreeKeyFetch(
4d360 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a  BtCursor*, int *
4d370 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  pAmt);.SQLITE_PR
4d380 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64  IVATE const void
4d390 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 44 61   *sqlite3BtreeDa
4d3a0 74 61 46 65 74 63 68 28 42 74 43 75 72 73 6f 72  taFetch(BtCursor
4d3b0 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53  *, int *pAmt);.S
4d3c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4d3d0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61  t sqlite3BtreeDa
4d3e0 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a  taSize(BtCursor*
4d3f0 2c 20 75 33 32 20 2a 70 53 69 7a 65 29 3b 0a 53  , u32 *pSize);.S
4d400 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4d410 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61  t sqlite3BtreeDa
4d420 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33  ta(BtCursor*, u3
4d430 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d  2 offset, u32 am
4d440 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54  t, void*);.SQLIT
4d450 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
4d460 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61  qlite3BtreeSetCa
4d470 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 73  chedRowid(BtCurs
4d480 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  or*, sqlite3_int
4d490 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  64);.SQLITE_PRIV
4d4a0 41 54 45 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ATE sqlite3_int6
4d4b0 34 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  4 sqlite3BtreeGe
4d4c0 74 43 61 63 68 65 64 52 6f 77 69 64 28 42 74 43  tCachedRowid(BtC
4d4d0 75 72 73 6f 72 2a 29 3b 0a 0a 53 51 4c 49 54 45  ursor*);..SQLITE
4d4e0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
4d4f0 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65 67  qlite3BtreeInteg
4d500 72 69 74 79 43 68 65 63 6b 28 42 74 72 65 65 2a  rityCheck(Btree*
4d510 2c 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 69 6e  , int *aRoot, in
4d520 74 20 6e 52 6f 6f 74 2c 20 69 6e 74 2c 20 69 6e  t nRoot, int, in
4d530 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
4d540 41 54 45 20 73 74 72 75 63 74 20 50 61 67 65 72  ATE struct Pager
4d550 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 50 61   *sqlite3BtreePa
4d560 67 65 72 28 42 74 72 65 65 2a 29 3b 0a 0a 53 51  ger(Btree*);..SQ
4d570 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4d580 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74   sqlite3BtreePut
4d590 44 61 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20  Data(BtCursor*, 
4d5a0 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20  u32 offset, u32 
4d5b0 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c  amt, void*);.SQL
4d5c0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
4d5d0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63   sqlite3BtreeCac
4d5e0 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 72  heOverflow(BtCur
4d5f0 73 6f 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  sor *);.SQLITE_P
4d600 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
4d610 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72  te3BtreeClearCur
4d620 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 29 3b  sor(BtCursor *);
4d630 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
4d640 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d650 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4d660 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 42 74  CursorIsValid(Bt
4d670 43 75 72 73 6f 72 2a 29 3b 0a 23 65 6e 64 69 66  Cursor*);.#endif
4d680 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
4d690 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54  _OMIT_BTREECOUNT
4d6a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d6b0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4d6c0 43 6f 75 6e 74 28 42 74 43 75 72 73 6f 72 20 2a  Count(BtCursor *
4d6d0 2c 20 69 36 34 20 2a 29 3b 0a 23 65 6e 64 69 66  , i64 *);.#endif
4d6e0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
4d6f0 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56  TEST.SQLITE_PRIV
4d700 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4d710 74 72 65 65 43 75 72 73 6f 72 49 6e 66 6f 28 42  treeCursorInfo(B
4d720 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 2c 20  tCursor*, int*, 
4d730 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
4d740 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
4d750 33 42 74 72 65 65 43 75 72 73 6f 72 4c 69 73 74  3BtreeCursorList
4d760 28 42 74 72 65 65 2a 29 3b 0a 23 65 6e 64 69 66  (Btree*);.#endif
4d770 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72  ../*.** If we ar
4d780 65 20 6e 6f 74 20 75 73 69 6e 67 20 73 68 61 72  e not using shar
4d790 65 64 20 63 61 63 68 65 2c 20 74 68 65 6e 20 74  ed cache, then t
4d7a0 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20  here is no need 
4d7b0 74 6f 0a 2a 2a 20 75 73 65 20 6d 75 74 65 78 65  to.** use mutexe
4d7c0 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20  s to access the 
4d7d0 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74 75  BtShared structu
4d7e0 72 65 73 2e 20 20 53 6f 20 6d 61 6b 65 20 74 68  res.  So make th
4d7f0 65 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20 4c  e.** Enter and L
4d800 65 61 76 65 20 70 72 6f 63 65 64 75 72 65 73 20  eave procedures 
4d810 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66 6e 64  no-ops..*/.#ifnd
4d820 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
4d830 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49  HARED_CACHE.SQLI
4d840 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
4d850 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  d sqlite3BtreeEn
4d860 74 65 72 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c  ter(Btree*);.SQL
4d870 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
4d880 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  id sqlite3BtreeE
4d890 6e 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 2a  nterAll(sqlite3*
4d8a0 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  );.#else.# defin
4d8b0 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  e sqlite3BtreeEn
4d8c0 74 65 72 28 58 29 20 0a 23 20 64 65 66 69 6e 65  ter(X) .# define
4d8d0 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
4d8e0 65 72 41 6c 6c 28 58 29 0a 23 65 6e 64 69 66 0a  erAll(X).#endif.
4d8f0 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
4d900 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
4d910 5f 43 41 43 48 45 29 20 26 26 20 53 51 4c 49 54  _CACHE) && SQLIT
4d920 45 5f 54 48 52 45 41 44 53 41 46 45 0a 53 51 4c  E_THREADSAFE.SQL
4d930 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
4d940 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  id sqlite3BtreeL
4d950 65 61 76 65 28 42 74 72 65 65 2a 29 3b 0a 53 51  eave(Btree*);.SQ
4d960 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
4d970 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
4d980 45 6e 74 65 72 43 75 72 73 6f 72 28 42 74 43 75  EnterCursor(BtCu
4d990 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  rsor*);.SQLITE_P
4d9a0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
4d9b0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 43  lite3BtreeLeaveC
4d9c0 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29  ursor(BtCursor*)
4d9d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4d9e0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42     void sqlite3B
4d9f0 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c  treeLeaveAll(sql
4da00 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ite3*);.SQLITE_P
4da10 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
4da20 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41  lite3BtreeMutexA
4da30 72 72 61 79 45 6e 74 65 72 28 42 74 72 65 65 4d  rrayEnter(BtreeM
4da40 75 74 65 78 41 72 72 61 79 2a 29 3b 0a 53 51 4c  utexArray*);.SQL
4da50 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
4da60 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  id sqlite3BtreeM
4da70 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 42  utexArrayLeave(B
4da80 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 2a 29  treeMutexArray*)
4da90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4daa0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42     void sqlite3B
4dab0 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e  treeMutexArrayIn
4dac0 73 65 72 74 28 42 74 72 65 65 4d 75 74 65 78 41  sert(BtreeMutexA
4dad0 72 72 61 79 2a 2c 20 42 74 72 65 65 2a 29 3b 0a  rray*, Btree*);.
4dae0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
4daf0 20 2f 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e   /* These routin
4db00 65 73 20 61 72 65 20 75 73 65 64 20 69 6e 73 69  es are used insi
4db10 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74  de assert() stat
4db20 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0a  ements only. */.
4db30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
4db40 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4db50 65 48 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65  eHoldsMutex(Btre
4db60 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
4db70 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
4db80 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75  3BtreeHoldsAllMu
4db90 74 65 78 65 73 28 73 71 6c 69 74 65 33 2a 29 3b  texes(sqlite3*);
4dba0 0a 23 65 6e 64 69 66 0a 23 65 6c 73 65 0a 0a 23  .#endif.#else..#
4dbb0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
4dbc0 74 72 65 65 4c 65 61 76 65 28 58 29 0a 23 20 64  treeLeave(X).# d
4dbd0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72  efine sqlite3Btr
4dbe0 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 58 29  eeEnterCursor(X)
4dbf0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
4dc00 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f  3BtreeLeaveCurso
4dc10 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71  r(X).# define sq
4dc20 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41  lite3BtreeLeaveA
4dc30 6c 6c 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73  ll(X).# define s
4dc40 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
4dc50 41 72 72 61 79 45 6e 74 65 72 28 58 29 0a 23 20  ArrayEnter(X).# 
4dc60 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74  define sqlite3Bt
4dc70 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61  reeMutexArrayLea
4dc80 76 65 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73  ve(X).# define s
4dc90 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
4dca0 41 72 72 61 79 49 6e 73 65 72 74 28 58 2c 59 29  ArrayInsert(X,Y)
4dcb0 0a 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ..# define sqlit
4dcc0 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
4dcd0 78 28 58 29 20 31 0a 23 20 64 65 66 69 6e 65 20  x(X) 1.# define 
4dce0 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
4dcf0 73 41 6c 6c 4d 75 74 65 78 65 73 28 58 29 20 31  sAllMutexes(X) 1
4dd00 0a 23 65 6e 64 69 66 0a 0a 0a 23 65 6e 64 69 66  .#endif...#endif
4dd10 20 2f 2a 20 5f 42 54 52 45 45 5f 48 5f 20 2a 2f   /* _BTREE_H_ */
4dd20 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
4dd30 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 2e 68  * End of btree.h
4dd40 20 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 2a 2a 2a 2a 2a 2a  ****************
4dd70 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
4dd80 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
4dd90 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
4dda0 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  n sqliteInt.h **
4ddb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4ddc0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
4ddd0 2a 20 49 6e 63 6c 75 64 65 20 76 64 62 65 2e 68  * Include vdbe.h
4dde0 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
4ddf0 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a  f sqliteInt.h **
4de00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4de10 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
4de20 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62  * Begin file vdb
4de30 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
4de40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4de50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4de60 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
4de70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
4de80 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
4de90 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
4dea0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
4deb0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
4dec0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
4ded0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
4dee0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
4def0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
4df00 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
4df10 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
4df20 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
4df30 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
4df40 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
4df50 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
4df60 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
4df70 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
4df80 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
4df90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dfa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dfb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dfc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dfd0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 61 64  ********.** Head
4dfe0 65 72 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20  er file for the 
4dff0 56 69 72 74 75 61 6c 20 44 61 74 61 42 61 73 65  Virtual DataBase
4e000 20 45 6e 67 69 6e 65 20 28 56 44 42 45 29 0a 2a   Engine (VDBE).*
4e010 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
4e020 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74   defines the int
4e030 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 76 69  erface to the vi
4e040 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65  rtual database e
4e050 6e 67 69 6e 65 0a 2a 2a 20 6f 72 20 56 44 42 45  ngine.** or VDBE
4e060 2e 20 20 54 68 65 20 56 44 42 45 20 69 6d 70 6c  .  The VDBE impl
4e070 65 6d 65 6e 74 73 20 61 6e 20 61 62 73 74 72 61  ements an abstra
4e080 63 74 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20  ct machine that 
4e090 72 75 6e 73 20 61 0a 2a 2a 20 73 69 6d 70 6c 65  runs a.** simple
4e0a0 20 70 72 6f 67 72 61 6d 20 74 6f 20 61 63 63 65   program to acce
4e0b0 73 73 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68  ss and modify th
4e0c0 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74  e underlying dat
4e0d0 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  abase..**.** $Id
4e0e0 3a 20 76 64 62 65 2e 68 2c 76 20 31 2e 31 34 32  : vdbe.h,v 1.142
4e0f0 20 32 30 30 39 2f 30 37 2f 32 34 20 31 37 3a 35   2009/07/24 17:5
4e100 38 3a 35 33 20 64 61 6e 69 65 6c 6b 31 39 37 37  8:53 danielk1977
4e110 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65   Exp $.*/.#ifnde
4e120 66 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f 48  f _SQLITE_VDBE_H
4e130 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54  _.#define _SQLIT
4e140 45 5f 56 44 42 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a  E_VDBE_H_../*.**
4e150 20 41 20 73 69 6e 67 6c 65 20 56 44 42 45 20 69   A single VDBE i
4e160 73 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75  s an opaque stru
4e170 63 74 75 72 65 20 6e 61 6d 65 64 20 22 56 64 62  cture named "Vdb
4e180 65 22 2e 20 20 4f 6e 6c 79 20 72 6f 75 74 69 6e  e".  Only routin
4e190 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 6f 75  es.** in the sou
4e1a0 72 63 65 20 66 69 6c 65 20 73 71 6c 69 74 65 56  rce file sqliteV
4e1b0 64 62 65 2e 63 20 61 72 65 20 61 6c 6c 6f 77 65  dbe.c are allowe
4e1c0 64 20 74 6f 20 73 65 65 20 74 68 65 20 69 6e 73  d to see the ins
4e1d0 69 64 65 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20  ides.** of this 
4e1e0 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79  structure..*/.ty
4e1f0 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62  pedef struct Vdb
4e200 65 20 56 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a 20 54  e Vdbe;../*.** T
4e210 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20  he names of the 
4e220 66 6f 6c 6c 6f 77 69 6e 67 20 74 79 70 65 73 20  following types 
4e230 64 65 63 6c 61 72 65 64 20 69 6e 20 76 64 62 65  declared in vdbe
4e240 49 6e 74 2e 68 20 61 72 65 20 72 65 71 75 69 72  Int.h are requir
4e250 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 56 64  ed.** for the Vd
4e260 62 65 4f 70 20 64 65 66 69 6e 69 74 69 6f 6e 2e  beOp definition.
4e270 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
4e280 63 74 20 56 64 62 65 46 75 6e 63 20 56 64 62 65  ct VdbeFunc Vdbe
4e290 46 75 6e 63 3b 0a 74 79 70 65 64 65 66 20 73 74  Func;.typedef st
4e2a0 72 75 63 74 20 4d 65 6d 20 4d 65 6d 3b 0a 74 79  ruct Mem Mem;.ty
4e2b0 70 65 64 65 66 20 73 74 72 75 63 74 20 53 75 62  pedef struct Sub
4e2c0 50 72 6f 67 72 61 6d 20 53 75 62 50 72 6f 67 72  Program SubProgr
4e2d0 61 6d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e  am;../*.** A sin
4e2e0 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  gle instruction 
4e2f0 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  of the virtual m
4e300 61 63 68 69 6e 65 20 68 61 73 20 61 6e 20 6f 70  achine has an op
4e310 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 61 73 20 6d  code.** and as m
4e320 61 6e 79 20 61 73 20 74 68 72 65 65 20 6f 70 65  any as three ope
4e330 72 61 6e 64 73 2e 20 20 54 68 65 20 69 6e 73 74  rands.  The inst
4e340 72 75 63 74 69 6f 6e 20 69 73 20 72 65 63 6f 72  ruction is recor
4e350 64 65 64 0a 2a 2a 20 61 73 20 61 6e 20 69 6e 73  ded.** as an ins
4e360 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
4e370 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
4e380 3a 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65  :.*/.struct Vdbe
4e390 4f 70 20 7b 0a 20 20 75 38 20 6f 70 63 6f 64 65  Op {.  u8 opcode
4e3a0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68  ;          /* Wh
4e3b0 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20  at operation to 
4e3c0 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20 73 69 67  perform */.  sig
4e3d0 6e 65 64 20 63 68 61 72 20 70 34 74 79 70 65 3b  ned char p4type;
4e3e0 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 50   /* One of the P
4e3f0 34 5f 78 78 78 20 63 6f 6e 73 74 61 6e 74 73 20  4_xxx constants 
4e400 66 6f 72 20 70 34 20 2a 2f 0a 20 20 75 38 20 6f  for p4 */.  u8 o
4e410 70 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20  pflags;         
4e420 2f 2a 20 4e 6f 74 20 63 75 72 72 65 6e 74 6c 79  /* Not currently
4e430 20 75 73 65 64 20 2a 2f 0a 20 20 75 38 20 70 35   used */.  u8 p5
4e440 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
4e450 2a 20 46 69 66 74 68 20 70 61 72 61 6d 65 74 65  * Fifth paramete
4e460 72 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64  r is an unsigned
4e470 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20   character */.  
4e480 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 20  int p1;         
4e490 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f 70 65      /* First ope
4e4a0 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 32  rand */.  int p2
4e4b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
4e4c0 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   Second paramete
4e4d0 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a 75 6d  r (often the jum
4e4e0 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 20 2a  p destination) *
4e4f0 2f 0a 20 20 69 6e 74 20 70 33 3b 20 20 20 20 20  /.  int p3;     
4e500 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
4e510 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 2a  hird parameter *
4e520 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20  /.  union {     
4e530 20 20 20 20 20 20 20 20 2f 2a 20 66 6f 75 72 74          /* fourt
4e540 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20  h parameter */. 
4e550 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20     int i;       
4e560 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74            /* Int
4e570 65 67 65 72 20 76 61 6c 75 65 20 69 66 20 70 34  eger value if p4
4e580 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 2a  type==P4_INT32 *
4e590 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 3b 20 20  /.    void *p;  
4e5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4e5b0 47 65 6e 65 72 69 63 20 70 6f 69 6e 74 65 72 20  Generic pointer 
4e5c0 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 20  */.    char *z; 
4e5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4e5e0 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 61   Pointer to data
4e5f0 20 66 6f 72 20 73 74 72 69 6e 67 20 28 63 68 61   for string (cha
4e600 72 20 61 72 72 61 79 29 20 74 79 70 65 73 20 2a  r array) types *
4e610 2f 0a 20 20 20 20 69 36 34 20 2a 70 49 36 34 3b  /.    i64 *pI64;
4e620 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4e630 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65  Used when p4type
4e640 20 69 73 20 50 34 5f 49 4e 54 36 34 20 2a 2f 0a   is P4_INT64 */.
4e650 20 20 20 20 64 6f 75 62 6c 65 20 2a 70 52 65 61      double *pRea
4e660 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73  l;         /* Us
4e670 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69  ed when p4type i
4e680 73 20 50 34 5f 52 45 41 4c 20 2a 2f 0a 20 20 20  s P4_REAL */.   
4e690 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 3b   FuncDef *pFunc;
4e6a0 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
4e6b0 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50  when p4type is P
4e6c0 34 5f 46 55 4e 43 44 45 46 20 2a 2f 0a 20 20 20  4_FUNCDEF */.   
4e6d0 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65   VdbeFunc *pVdbe
4e6e0 46 75 6e 63 3b 20 20 20 2f 2a 20 55 73 65 64 20  Func;   /* Used 
4e6f0 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50  when p4type is P
4e700 34 5f 56 44 42 45 46 55 4e 43 20 2a 2f 0a 20 20  4_VDBEFUNC */.  
4e710 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
4e720 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64  ;        /* Used
4e730 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20   when p4type is 
4e740 50 34 5f 43 4f 4c 4c 53 45 51 20 2a 2f 0a 20 20  P4_COLLSEQ */.  
4e750 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20    Mem *pMem;    
4e760 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64           /* Used
4e770 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20   when p4type is 
4e780 50 34 5f 4d 45 4d 20 2a 2f 0a 20 20 20 20 56 54  P4_MEM */.    VT
4e790 61 62 6c 65 20 2a 70 56 74 61 62 3b 20 20 20 20  able *pVtab;    
4e7a0 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65       /* Used whe
4e7b0 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 56  n p4type is P4_V
4e7c0 54 41 42 20 2a 2f 0a 20 20 20 20 4b 65 79 49 6e  TAB */.    KeyIn
4e7d0 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 20  fo *pKeyInfo;   
4e7e0 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70    /* Used when p
4e7f0 34 74 79 70 65 20 69 73 20 50 34 5f 4b 45 59 49  4type is P4_KEYI
4e800 4e 46 4f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a  NFO */.    int *
4e810 61 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ai;             
4e820 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70    /* Used when p
4e830 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e 54 41  4type is P4_INTA
4e840 52 52 41 59 20 2a 2f 0a 20 20 20 20 53 75 62 50  RRAY */.    SubP
4e850 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d  rogram *pProgram
4e860 3b 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20  ;  /* Used when 
4e870 70 34 74 79 70 65 20 69 73 20 50 34 5f 53 55 42  p4type is P4_SUB
4e880 50 52 4f 47 52 41 4d 20 2a 2f 0a 20 20 7d 20 70  PROGRAM */.  } p
4e890 34 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  4;.#ifdef SQLITE
4e8a0 5f 44 45 42 55 47 0a 20 20 63 68 61 72 20 2a 7a  _DEBUG.  char *z
4e8b0 43 6f 6d 6d 65 6e 74 3b 20 20 20 20 20 20 20 20  Comment;        
4e8c0 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74 6f 20    /* Comment to 
4e8d0 69 6d 70 72 6f 76 65 20 72 65 61 64 61 62 69 6c  improve readabil
4e8e0 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  ity */.#endif.#i
4e8f0 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c  fdef VDBE_PROFIL
4e900 45 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20  E.  int cnt;    
4e910 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4e920 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  Number of times 
4e930 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  this instruction
4e940 20 77 61 73 20 65 78 65 63 75 74 65 64 20 2a 2f   was executed */
4e950 0a 20 20 75 36 34 20 63 79 63 6c 65 73 3b 20 20  .  u64 cycles;  
4e960 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
4e970 6f 74 61 6c 20 74 69 6d 65 20 73 70 65 6e 74 20  otal time spent 
4e980 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 69  executing this i
4e990 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 23 65  nstruction */.#e
4e9a0 6e 64 69 66 0a 7d 3b 0a 74 79 70 65 64 65 66 20  ndif.};.typedef 
4e9b0 73 74 72 75 63 74 20 56 64 62 65 4f 70 20 56 64  struct VdbeOp Vd
4e9c0 62 65 4f 70 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20  beOp;.../*.** A 
4e9d0 73 75 62 2d 72 6f 75 74 69 6e 65 20 75 73 65 64  sub-routine used
4e9e0 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20   to implement a 
4e9f0 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e  trigger program.
4ea00 0a 2a 2f 0a 73 74 72 75 63 74 20 53 75 62 50 72  .*/.struct SubPr
4ea10 6f 67 72 61 6d 20 7b 0a 20 20 56 64 62 65 4f 70  ogram {.  VdbeOp
4ea20 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 20 20   *aOp;          
4ea30 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79          /* Array
4ea40 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f 72 20   of opcodes for 
4ea50 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20  sub-program */. 
4ea60 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20   int nOp;       
4ea70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4ea80 2a 20 45 6c 65 6d 65 6e 74 73 20 69 6e 20 61 4f  * Elements in aO
4ea90 70 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65  p[] */.  int nMe
4eaa0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
4eab0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
4eac0 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73   of memory cells
4ead0 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69   required */.  i
4eae0 6e 74 20 6e 43 73 72 3b 20 20 20 20 20 20 20 20  nt nCsr;        
4eaf0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
4eb00 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72  Number of cursor
4eb10 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20  s required */.  
4eb20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20  int nRef;       
4eb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4eb40 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74   Number of point
4eb50 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72 75  ers to this stru
4eb60 63 74 75 72 65 20 2a 2f 0a 20 20 76 6f 69 64 20  cture */.  void 
4eb70 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20  *token;         
4eb80 20 20 20 20 20 20 20 20 20 2f 2a 20 69 64 20 74           /* id t
4eb90 68 61 74 20 6d 61 79 20 62 65 20 75 73 65 64 20  hat may be used 
4eba0 74 6f 20 72 65 63 75 72 73 69 76 65 20 74 72 69  to recursive tri
4ebb0 67 67 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ggers */.};../*.
4ebc0 2a 2a 20 41 20 73 6d 61 6c 6c 65 72 20 76 65 72  ** A smaller ver
4ebd0 73 69 6f 6e 20 6f 66 20 56 64 62 65 4f 70 20 75  sion of VdbeOp u
4ebe0 73 65 64 20 66 6f 72 20 74 68 65 20 56 64 62 65  sed for the Vdbe
4ebf0 41 64 64 4f 70 4c 69 73 74 28 29 20 66 75 6e 63  AddOpList() func
4ec00 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a 2a 20  tion because.** 
4ec10 69 74 20 74 61 6b 65 73 20 75 70 20 6c 65 73 73  it takes up less
4ec20 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 72 75 63   space..*/.struc
4ec30 74 20 56 64 62 65 4f 70 4c 69 73 74 20 7b 0a 20  t VdbeOpList {. 
4ec40 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20   u8 opcode;     
4ec50 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65       /* What ope
4ec60 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72  ration to perfor
4ec70 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68  m */.  signed ch
4ec80 61 72 20 70 31 3b 20 20 20 20 20 2f 2a 20 46 69  ar p1;     /* Fi
4ec90 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  rst operand */. 
4eca0 20 73 69 67 6e 65 64 20 63 68 61 72 20 70 32 3b   signed char p2;
4ecb0 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70       /* Second p
4ecc0 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65 6e 20  arameter (often 
4ecd0 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69 6e 61  the jump destina
4ece0 74 69 6f 6e 29 20 2a 2f 0a 20 20 73 69 67 6e 65  tion) */.  signe
4ecf0 64 20 63 68 61 72 20 70 33 3b 20 20 20 20 20 2f  d char p3;     /
4ed00 2a 20 54 68 69 72 64 20 70 61 72 61 6d 65 74 65  * Third paramete
4ed10 72 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20  r */.};.typedef 
4ed20 73 74 72 75 63 74 20 56 64 62 65 4f 70 4c 69 73  struct VdbeOpLis
4ed30 74 20 56 64 62 65 4f 70 4c 69 73 74 3b 0a 0a 2f  t VdbeOpList;../
4ed40 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c  *.** Allowed val
4ed50 75 65 73 20 6f 66 20 56 64 62 65 4f 70 2e 70 34  ues of VdbeOp.p4
4ed60 74 79 70 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  type.*/.#define 
4ed70 50 34 5f 4e 4f 54 55 53 45 44 20 20 20 20 30 20  P4_NOTUSED    0 
4ed80 20 20 2f 2a 20 54 68 65 20 50 34 20 70 61 72 61    /* The P4 para
4ed90 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65  meter is not use
4eda0 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  d */.#define P4_
4edb0 44 59 4e 41 4d 49 43 20 20 28 2d 31 29 20 20 2f  DYNAMIC  (-1)  /
4edc0 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  * Pointer to a s
4edd0 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66  tring obtained f
4ede0 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  rom sqliteMalloc
4edf0 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34  () */.#define P4
4ee00 5f 53 54 41 54 49 43 20 20 20 28 2d 32 29 20 20  _STATIC   (-2)  
4ee10 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20  /* Pointer to a 
4ee20 73 74 61 74 69 63 20 73 74 72 69 6e 67 20 2a 2f  static string */
4ee30 0a 23 64 65 66 69 6e 65 20 50 34 5f 43 4f 4c 4c  .#define P4_COLL
4ee40 53 45 51 20 20 28 2d 34 29 20 20 2f 2a 20 50 34  SEQ  (-4)  /* P4
4ee50 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
4ee60 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63   a CollSeq struc
4ee70 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ture */.#define 
4ee80 50 34 5f 46 55 4e 43 44 45 46 20 20 28 2d 35 29  P4_FUNCDEF  (-5)
4ee90 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69    /* P4 is a poi
4eea0 6e 74 65 72 20 74 6f 20 61 20 46 75 6e 63 44 65  nter to a FuncDe
4eeb0 66 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23  f structure */.#
4eec0 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46  define P4_KEYINF
4eed0 4f 20 20 28 2d 36 29 20 20 2f 2a 20 50 34 20 69  O  (-6)  /* P4 i
4eee0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
4eef0 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
4ef00 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34  re */.#define P4
4ef10 5f 56 44 42 45 46 55 4e 43 20 28 2d 37 29 20 20  _VDBEFUNC (-7)  
4ef20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  /* P4 is a point
4ef30 65 72 20 74 6f 20 61 20 56 64 62 65 46 75 6e 63  er to a VdbeFunc
4ef40 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64   structure */.#d
4ef50 65 66 69 6e 65 20 50 34 5f 4d 45 4d 20 20 20 20  efine P4_MEM    
4ef60 20 20 28 2d 38 29 20 20 2f 2a 20 50 34 20 69 73    (-8)  /* P4 is
4ef70 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
4ef80 4d 65 6d 2a 20 20 20 20 73 74 72 75 63 74 75 72  Mem*    structur
4ef90 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  e */.#define P4_
4efa0 54 52 41 4e 53 49 45 4e 54 20 28 2d 39 29 20 2f  TRANSIENT (-9) /
4efb0 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65  * P4 is a pointe
4efc0 72 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74  r to a transient
4efd0 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69   string */.#defi
4efe0 6e 65 20 50 34 5f 56 54 41 42 20 20 20 20 20 28  ne P4_VTAB     (
4eff0 2d 31 30 29 20 2f 2a 20 50 34 20 69 73 20 61 20  -10) /* P4 is a 
4f000 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71  pointer to an sq
4f010 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63  lite3_vtab struc
4f020 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ture */.#define 
4f030 50 34 5f 4d 50 52 49 4e 54 46 20 20 28 2d 31 31  P4_MPRINTF  (-11
4f040 29 20 2f 2a 20 50 34 20 69 73 20 61 20 73 74 72  ) /* P4 is a str
4f050 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ing obtained fro
4f060 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  m sqlite3_mprint
4f070 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  f() */.#define P
4f080 34 5f 52 45 41 4c 20 20 20 20 20 28 2d 31 32 29  4_REAL     (-12)
4f090 20 2f 2a 20 50 34 20 69 73 20 61 20 36 34 2d 62   /* P4 is a 64-b
4f0a0 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  it floating poin
4f0b0 74 20 76 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69  t value */.#defi
4f0c0 6e 65 20 50 34 5f 49 4e 54 36 34 20 20 20 20 28  ne P4_INT64    (
4f0d0 2d 31 33 29 20 2f 2a 20 50 34 20 69 73 20 61 20  -13) /* P4 is a 
4f0e0 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  64-bit signed in
4f0f0 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  teger */.#define
4f100 20 50 34 5f 49 4e 54 33 32 20 20 20 20 28 2d 31   P4_INT32    (-1
4f110 34 29 20 2f 2a 20 50 34 20 69 73 20 61 20 33 32  4) /* P4 is a 32
4f120 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65  -bit signed inte
4f130 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ger */.#define P
4f140 34 5f 49 4e 54 41 52 52 41 59 20 28 2d 31 35 29  4_INTARRAY (-15)
4f150 20 2f 2a 20 50 34 20 69 73 20 61 20 76 65 63 74   /* P4 is a vect
4f160 6f 72 20 6f 66 20 33 32 2d 62 69 74 20 69 6e 74  or of 32-bit int
4f170 65 67 65 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65  egers */.#define
4f180 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 20   P4_SUBPROGRAM  
4f190 28 2d 31 38 29 20 2f 2a 20 50 34 20 69 73 20 61  (-18) /* P4 is a
4f1a0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 75   pointer to a Su
4f1b0 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63 74 75  bProgram structu
4f1c0 72 65 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e 20 61  re */../* When a
4f1d0 64 64 69 6e 67 20 61 20 50 34 20 61 72 67 75 6d  dding a P4 argum
4f1e0 65 6e 74 20 75 73 69 6e 67 20 50 34 5f 4b 45 59  ent using P4_KEY
4f1f0 49 4e 46 4f 2c 20 61 20 63 6f 70 79 20 6f 66 20  INFO, a copy of 
4f200 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  the KeyInfo stru
4f210 63 74 75 72 65 0a 2a 2a 20 69 73 20 6d 61 64 65  cture.** is made
4f220 2e 20 20 54 68 61 74 20 63 6f 70 79 20 69 73 20  .  That copy is 
4f230 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56  freed when the V
4f240 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64  dbe is finalized
4f250 2e 20 20 42 75 74 20 69 66 20 74 68 65 0a 2a 2a  .  But if the.**
4f260 20 61 72 67 75 6d 65 6e 74 20 69 73 20 50 34 5f   argument is P4_
4f270 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 2c  KEYINFO_HANDOFF,
4f280 20 74 68 65 20 70 61 73 73 65 64 20 69 6e 20 70   the passed in p
4f290 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 2e 20  ointer is used. 
4f2a0 20 49 74 20 73 74 69 6c 6c 0a 2a 2a 20 67 65 74   It still.** get
4f2b0 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65  s freed when the
4f2c0 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a   Vdbe is finaliz
4f2d0 65 64 20 73 6f 20 69 74 20 73 74 69 6c 6c 20 73  ed so it still s
4f2e0 68 6f 75 6c 64 20 62 65 20 6f 62 74 61 69 6e 65  hould be obtaine
4f2f0 64 0a 2a 2a 20 66 72 6f 6d 20 61 20 73 69 6e 67  d.** from a sing
4f300 6c 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  le sqliteMalloc(
4f310 29 2e 20 20 42 75 74 20 6e 6f 20 63 6f 70 79 20  ).  But no copy 
4f320 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68 65 20  is made and the 
4f330 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74  calling.** funct
4f340 69 6f 6e 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a  ion should *not*
4f350 20 74 72 79 20 74 6f 20 66 72 65 65 20 74 68 65   try to free the
4f360 20 4b 65 79 49 6e 66 6f 2e 0a 2a 2f 0a 23 64 65   KeyInfo..*/.#de
4f370 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  fine P4_KEYINFO_
4f380 48 41 4e 44 4f 46 46 20 28 2d 31 36 29 0a 23 64  HANDOFF (-16).#d
4f390 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46 4f  efine P4_KEYINFO
4f3a0 5f 53 54 41 54 49 43 20 20 28 2d 31 37 29 0a 0a  _STATIC  (-17)..
4f3b0 2f 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65 2e 61  /*.** The Vdbe.a
4f3c0 43 6f 6c 4e 61 6d 65 20 61 72 72 61 79 20 63 6f  ColName array co
4f3d0 6e 74 61 69 6e 73 20 35 6e 20 4d 65 6d 20 73 74  ntains 5n Mem st
4f3e0 72 75 63 74 75 72 65 73 2c 20 77 68 65 72 65 20  ructures, where 
4f3f0 6e 20 69 73 20 74 68 65 20 0a 2a 2a 20 6e 75 6d  n is the .** num
4f400 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f  ber of columns o
4f410 66 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 20  f data returned 
4f420 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  by the statement
4f430 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 4f 4c  ..*/.#define COL
4f440 4e 41 4d 45 5f 4e 41 4d 45 20 20 20 20 20 30 0a  NAME_NAME     0.
4f450 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f  #define COLNAME_
4f460 44 45 43 4c 54 59 50 45 20 31 0a 23 64 65 66 69  DECLTYPE 1.#defi
4f470 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42  ne COLNAME_DATAB
4f480 41 53 45 20 32 0a 23 64 65 66 69 6e 65 20 43 4f  ASE 2.#define CO
4f490 4c 4e 41 4d 45 5f 54 41 42 4c 45 20 20 20 20 33  LNAME_TABLE    3
4f4a0 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45  .#define COLNAME
4f4b0 5f 43 4f 4c 55 4d 4e 20 20 20 34 0a 23 69 66 64  _COLUMN   4.#ifd
4f4c0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
4f4d0 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41  _COLUMN_METADATA
4f4e0 0a 23 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d  .# define COLNAM
4f4f0 45 5f 4e 20 20 20 20 20 20 20 20 35 20 20 20 20  E_N        5    
4f500 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 43    /* Number of C
4f510 4f 4c 4e 41 4d 45 5f 78 78 78 20 73 79 6d 62 6f  OLNAME_xxx symbo
4f520 6c 73 20 2a 2f 0a 23 65 6c 73 65 0a 23 20 69 66  ls */.#else.# if
4f530 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
4f540 44 45 43 4c 54 59 50 45 0a 23 20 20 20 64 65 66  DECLTYPE.#   def
4f550 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20  ine COLNAME_N   
4f560 20 20 20 31 20 20 20 20 20 20 2f 2a 20 53 74 6f     1      /* Sto
4f570 72 65 20 6f 6e 6c 79 20 74 68 65 20 6e 61 6d 65  re only the name
4f580 20 2a 2f 0a 23 20 65 6c 73 65 0a 23 20 20 20 64   */.# else.#   d
4f590 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20  efine COLNAME_N 
4f5a0 20 20 20 20 20 32 20 20 20 20 20 20 2f 2a 20 53       2      /* S
4f5b0 74 6f 72 65 20 74 68 65 20 6e 61 6d 65 20 61 6e  tore the name an
4f5c0 64 20 64 65 63 6c 74 79 70 65 20 2a 2f 0a 23 20  d decltype */.# 
4f5d0 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a  endif.#endif../*
4f5e0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
4f5f0 67 20 6d 61 63 72 6f 20 63 6f 6e 76 65 72 74 73  g macro converts
4f600 20 61 20 72 65 6c 61 74 69 76 65 20 61 64 64 72   a relative addr
4f610 65 73 73 20 69 6e 20 74 68 65 20 70 32 20 66 69  ess in the p2 fi
4f620 65 6c 64 0a 2a 2a 20 6f 66 20 61 20 56 64 62 65  eld.** of a Vdbe
4f630 4f 70 20 73 74 72 75 63 74 75 72 65 20 69 6e 74  Op structure int
4f640 6f 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d  o a negative num
4f650 62 65 72 20 73 6f 20 74 68 61 74 20 0a 2a 2a 20  ber so that .** 
4f660 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
4f670 4c 69 73 74 28 29 20 6b 6e 6f 77 73 20 74 68 61  List() knows tha
4f680 74 20 74 68 65 20 61 64 64 72 65 73 73 20 69 73  t the address is
4f690 20 72 65 6c 61 74 69 76 65 2e 20 20 43 61 6c 6c   relative.  Call
4f6a0 69 6e 67 0a 2a 2a 20 74 68 65 20 6d 61 63 72 6f  ing.** the macro
4f6b0 20 61 67 61 69 6e 20 72 65 73 74 6f 72 65 73 20   again restores 
4f6c0 74 68 65 20 61 64 64 72 65 73 73 2e 0a 2a 2f 0a  the address..*/.
4f6d0 23 64 65 66 69 6e 65 20 41 44 44 52 28 58 29 20  #define ADDR(X) 
4f6e0 20 28 2d 31 2d 28 58 29 29 0a 0a 2f 2a 0a 2a 2a   (-1-(X))../*.**
4f6f0 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 73 63   The makefile sc
4f700 61 6e 73 20 74 68 65 20 76 64 62 65 2e 63 20 73  ans the vdbe.c s
4f710 6f 75 72 63 65 20 66 69 6c 65 20 61 6e 64 20 63  ource file and c
4f720 72 65 61 74 65 73 20 74 68 65 20 22 6f 70 63 6f  reates the "opco
4f730 64 65 73 2e 68 22 0a 2a 2a 20 68 65 61 64 65 72  des.h".** header
4f740 20 66 69 6c 65 20 74 68 61 74 20 64 65 66 69 6e   file that defin
4f750 65 73 20 61 20 6e 75 6d 62 65 72 20 66 6f 72 20  es a number for 
4f760 65 61 63 68 20 6f 70 63 6f 64 65 20 75 73 65 64  each opcode used
4f770 20 62 79 20 74 68 65 20 56 44 42 45 2e 0a 2a 2f   by the VDBE..*/
4f780 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
4f790 20 49 6e 63 6c 75 64 65 20 6f 70 63 6f 64 65 73   Include opcodes
4f7a0 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
4f7b0 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a   of vdbe.h *****
4f7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
4f7d0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
4f7e0 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70 63 6f   Begin file opco
4f7f0 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  des.h **********
4f800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4f810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
4f820 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c  ./* Automaticall
4f830 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20 44 6f  y generated.  Do
4f840 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f 2a 20   not edit */./* 
4f850 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65  See the mkopcode
4f860 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66 6f 72  h.awk script for
4f870 20 64 65 74 61 69 6c 73 20 2a 2f 0a 23 64 65 66   details */.#def
4f880 69 6e 65 20 4f 50 5f 56 4e 65 78 74 20 20 20 20  ine OP_VNext    
4f890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f8a0 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64              1.#d
4f8b0 65 66 69 6e 65 20 4f 50 5f 41 66 66 69 6e 69 74  efine OP_Affinit
4f8c0 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y               
4f8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a                2.
4f8e0 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c 75 6d  #define OP_Colum
4f8f0 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
4f900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f910 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 74  3.#define OP_Set
4f920 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20 20 20  Cookie          
4f930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f940 20 20 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53    4.#define OP_S
4f950 65 65 6b 20 20 20 20 20 20 20 20 20 20 20 20 20  eek             
4f960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f970 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 4f 50      5.#define OP
4f980 5f 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 20  _Real           
4f990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f9a0 20 20 20 20 31 33 30 20 20 20 2f 2a 20 73 61 6d      130   /* sam
4f9b0 65 20 61 73 20 54 4b 5f 46 4c 4f 41 54 20 20 20  e as TK_FLOAT   
4f9c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53   */.#define OP_S
4f9d0 65 71 75 65 6e 63 65 20 20 20 20 20 20 20 20 20  equence         
4f9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4f9f0 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 4f 50      6.#define OP
4fa00 5f 53 61 76 65 70 6f 69 6e 74 20 20 20 20 20 20  _Savepoint      
4fa10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fa20 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20        7.#define 
4fa30 4f 50 5f 47 65 20 20 20 20 20 20 20 20 20 20 20  OP_Ge           
4fa40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fa50 20 20 20 20 20 20 20 38 30 20 20 20 2f 2a 20 73         80   /* s
4fa60 61 6d 65 20 61 73 20 54 4b 5f 47 45 20 20 20 20  ame as TK_GE    
4fa70 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
4fa80 5f 52 6f 77 4b 65 79 20 20 20 20 20 20 20 20 20  _RowKey         
4fa90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4faa0 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20        8.#define 
4fab0 4f 50 5f 53 43 6f 70 79 20 20 20 20 20 20 20 20  OP_SCopy        
4fac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fad0 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e          9.#defin
4fae0 65 20 4f 50 5f 45 71 20 20 20 20 20 20 20 20 20  e OP_Eq         
4faf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fb00 20 20 20 20 20 20 20 20 20 37 36 20 20 20 2f 2a           76   /*
4fb10 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51 20 20   same as TK_EQ  
4fb20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
4fb30 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 20 20 20  OP_OpenWrite    
4fb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fb50 20 20 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e         10.#defin
4fb60 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 20 20 20  e OP_NotNull    
4fb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fb80 20 20 20 20 20 20 20 20 20 37 34 20 20 20 2f 2a           74   /*
4fb90 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 4e   same as TK_NOTN
4fba0 55 4c 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ULL  */.#define 
4fbb0 4f 50 5f 49 66 20 20 20 20 20 20 20 20 20 20 20  OP_If           
4fbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fbd0 20 20 20 20 20 20 20 31 31 0a 23 64 65 66 69 6e         11.#defin
4fbe0 65 20 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 20  e OP_ToInt      
4fbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fc00 20 20 20 20 20 20 20 20 31 34 34 20 20 20 2f 2a          144   /*
4fc10 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49   same as TK_TO_I
4fc20 4e 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  NT   */.#define 
4fc30 4f 50 5f 53 74 72 69 6e 67 38 20 20 20 20 20 20  OP_String8      
4fc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fc50 20 20 20 20 20 20 20 39 34 20 20 20 2f 2a 20 73         94   /* s
4fc60 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 49 4e 47  ame as TK_STRING
4fc70 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
4fc80 5f 43 6f 6c 6c 53 65 71 20 20 20 20 20 20 20 20  _CollSeq        
4fc90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fca0 20 20 20 20 20 31 32 0a 23 64 65 66 69 6e 65 20       12.#define 
4fcb0 4f 50 5f 4f 70 65 6e 52 65 61 64 20 20 20 20 20  OP_OpenRead     
4fcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fcd0 20 20 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e         13.#defin
4fce0 65 20 4f 50 5f 45 78 70 69 72 65 20 20 20 20 20  e OP_Expire     
4fcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd00 20 20 20 20 20 20 20 20 20 31 34 0a 23 64 65 66           14.#def
4fd10 69 6e 65 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69  ine OP_AutoCommi
4fd20 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
4fd30 20 20 20 20 20 20 20 20 20 20 20 31 35 0a 23 64             15.#d
4fd40 65 66 69 6e 65 20 4f 50 5f 47 74 20 20 20 20 20  efine OP_Gt     
4fd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd60 20 20 20 20 20 20 20 20 20 20 20 20 20 37 37 20               77 
4fd70 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
4fd80 47 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  GT       */.#def
4fd90 69 6e 65 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74  ine OP_Pagecount
4fda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fdb0 20 20 20 20 20 20 20 20 20 20 20 31 36 0a 23 64             16.#d
4fdc0 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67 72 69  efine OP_Integri
4fdd0 74 79 43 6b 20 20 20 20 20 20 20 20 20 20 20 20  tyCk            
4fde0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 37 0a               17.
4fdf0 23 64 65 66 69 6e 65 20 4f 50 5f 53 6f 72 74 20  #define OP_Sort 
4fe00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fe10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4fe20 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 70  8.#define OP_Cop
4fe30 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y               
4fe40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fe50 20 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54   20.#define OP_T
4fe60 72 61 63 65 20 20 20 20 20 20 20 20 20 20 20 20  race            
4fe70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fe80 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20 4f 50     21.#define OP
4fe90 5f 46 75 6e 63 74 69 6f 6e 20 20 20 20 20 20 20  _Function       
4fea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4feb0 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e 65 20       22.#define 
4fec0 4f 50 5f 49 66 4e 65 67 20 20 20 20 20 20 20 20  OP_IfNeg        
4fed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fee0 20 20 20 20 20 20 20 32 33 0a 23 64 65 66 69 6e         23.#defin
4fef0 65 20 4f 50 5f 41 6e 64 20 20 20 20 20 20 20 20  e OP_And        
4ff00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff10 20 20 20 20 20 20 20 20 20 36 39 20 20 20 2f 2a           69   /*
4ff20 20 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 20   same as TK_AND 
4ff30 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
4ff40 4f 50 5f 53 75 62 74 72 61 63 74 20 20 20 20 20  OP_Subtract     
4ff50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff60 20 20 20 20 20 20 20 38 37 20 20 20 2f 2a 20 73         87   /* s
4ff70 61 6d 65 20 61 73 20 54 4b 5f 4d 49 4e 55 53 20  ame as TK_MINUS 
4ff80 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
4ff90 5f 4e 6f 6f 70 20 20 20 20 20 20 20 20 20 20 20  _Noop           
4ffa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ffb0 20 20 20 20 20 32 34 0a 23 64 65 66 69 6e 65 20       24.#define 
4ffc0 4f 50 5f 50 72 6f 67 72 61 6d 20 20 20 20 20 20  OP_Program      
4ffd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ffe0 20 20 20 20 20 20 20 32 35 0a 23 64 65 66 69 6e         25.#defin
4fff0 65 20 4f 50 5f 52 65 74 75 72 6e 20 20 20 20 20  e OP_Return     
50000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50010 20 20 20 20 20 20 20 20 20 32 36 0a 23 64 65 66           26.#def
50020 69 6e 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72  ine OP_Remainder
50030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50040 20 20 20 20 20 20 20 20 20 20 20 39 30 20 20 20             90   
50050 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 45  /* same as TK_RE
50060 4d 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  M      */.#defin
50070 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 20 20  e OP_NewRowid   
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 32 37 0a 23 64 65 66           27.#def
500a0 69 6e 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 20  ine OP_Multiply 
500b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
500c0 20 20 20 20 20 20 20 20 20 20 20 38 38 20 20 20             88   
500d0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54  /* same as TK_ST
500e0 41 52 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  AR     */.#defin
500f0 65 20 4f 50 5f 46 6b 43 6f 75 6e 74 65 72 20 20  e OP_FkCounter  
50100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50110 20 20 20 20 20 20 20 20 20 32 38 0a 23 64 65 66           28.#def
50120 69 6e 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20  ine OP_Variable 
50130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50140 20 20 20 20 20 20 20 20 20 20 20 32 39 0a 23 64             29.#d
50150 65 66 69 6e 65 20 4f 50 5f 53 74 72 69 6e 67 20  efine OP_String 
50160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50170 20 20 20 20 20 20 20 20 20 20 20 20 20 33 30 0a               30.
50180 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 41  #define OP_RealA
50190 66 66 69 6e 69 74 79 20 20 20 20 20 20 20 20 20  ffinity         
501a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
501b0 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 52 65  1.#define OP_VRe
501c0 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20  name            
501d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
501e0 20 33 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50   32.#define OP_P
501f0 61 72 73 65 53 63 68 65 6d 61 20 20 20 20 20 20  arseSchema      
50200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50210 20 20 20 33 33 0a 23 64 65 66 69 6e 65 20 4f 50     33.#define OP
50220 5f 56 4f 70 65 6e 20 20 20 20 20 20 20 20 20 20  _VOpen          
50230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50240 20 20 20 20 20 33 34 0a 23 64 65 66 69 6e 65 20       34.#define 
50250 4f 50 5f 43 6c 6f 73 65 20 20 20 20 20 20 20 20  OP_Close        
50260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50270 20 20 20 20 20 20 20 33 35 0a 23 64 65 66 69 6e         35.#defin
50280 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65 78  e OP_CreateIndex
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 33 36 0a 23 64 65 66           36.#def
502b0 69 6e 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 20  ine OP_IsUnique 
502c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
502d0 20 20 20 20 20 20 20 20 20 20 20 33 37 0a 23 64             37.#d
502e0 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e  efine OP_NotFoun
502f0 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
50300 20 20 20 20 20 20 20 20 20 20 20 20 20 33 38 0a               38.
50310 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 36 34  #define OP_Int64
50320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
50340 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 75 73  9.#define OP_Mus
50350 74 42 65 49 6e 74 20 20 20 20 20 20 20 20 20 20  tBeInt          
50360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50370 20 34 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48   40.#define OP_H
50380 61 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 20  alt             
50390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
503a0 20 20 20 34 31 0a 23 64 65 66 69 6e 65 20 4f 50     41.#define OP
503b0 5f 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20  _Rowid          
503c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
503d0 20 20 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20       42.#define 
503e0 4f 50 5f 49 64 78 4c 54 20 20 20 20 20 20 20 20  OP_IdxLT        
503f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50400 20 20 20 20 20 20 20 34 33 0a 23 64 65 66 69 6e         43.#defin
50410 65 20 4f 50 5f 41 64 64 49 6d 6d 20 20 20 20 20  e OP_AddImm     
50420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50430 20 20 20 20 20 20 20 20 20 34 34 0a 23 64 65 66           44.#def
50440 69 6e 65 20 4f 50 5f 52 6f 77 44 61 74 61 20 20  ine OP_RowData  
50450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50460 20 20 20 20 20 20 20 20 20 20 20 34 35 0a 23 64             45.#d
50470 65 66 69 6e 65 20 4f 50 5f 4d 65 6d 4d 61 78 20  efine OP_MemMax 
50480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50490 20 20 20 20 20 20 20 20 20 20 20 20 20 34 36 0a               46.
504a0 23 64 65 66 69 6e 65 20 4f 50 5f 4f 72 20 20 20  #define OP_Or   
504b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
504c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
504d0 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  8   /* same as T
504e0 4b 5f 4f 52 20 20 20 20 20 20 20 2a 2f 0a 23 64  K_OR       */.#d
504f0 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 45 78 69 73  efine OP_NotExis
50500 74 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ts              
50510 20 20 20 20 20 20 20 20 20 20 20 20 20 34 37 0a               47.
50520 23 64 65 66 69 6e 65 20 4f 50 5f 47 6f 73 75 62  #define OP_Gosub
50530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
50550 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 69 76  8.#define OP_Div
50560 69 64 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ide             
50570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50580 20 38 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   89   /* same as
50590 20 54 4b 5f 53 4c 41 53 48 20 20 20 20 2a 2f 0a   TK_SLASH    */.
505a0 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 74 65 67  #define OP_Integ
505b0 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  er              
505c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
505d0 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 4e  9.#define OP_ToN
505e0 75 6d 65 72 69 63 20 20 20 20 20 20 20 20 20 20  umeric          
505f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50600 31 34 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73  143   /* same as
50610 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2a 2f   TK_TO_NUMERIC*/
50620 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 72 65 76  .#define OP_Prev
50630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50650 35 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f  50.#define OP_Ro
50660 77 53 65 74 52 65 61 64 20 20 20 20 20 20 20 20  wSetRead        
50670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50680 20 20 35 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f    51.#define OP_
50690 43 6f 6e 63 61 74 20 20 20 20 20 20 20 20 20 20  Concat          
506a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
506b0 20 20 20 20 39 31 20 20 20 2f 2a 20 73 61 6d 65      91   /* same
506c0 20 61 73 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20   as TK_CONCAT   
506d0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f  */.#define OP_Ro
506e0 77 53 65 74 41 64 64 20 20 20 20 20 20 20 20 20  wSetAdd         
506f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50700 20 20 35 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f    52.#define OP_
50710 42 69 74 41 6e 64 20 20 20 20 20 20 20 20 20 20  BitAnd          
50720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50730 20 20 20 20 38 32 20 20 20 2f 2a 20 73 61 6d 65      82   /* same
50740 20 61 73 20 54 4b 5f 42 49 54 41 4e 44 20 20 20   as TK_BITAND   
50750 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43  */.#define OP_VC
50760 6f 6c 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20  olumn           
50770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50780 20 20 35 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f    53.#define OP_
50790 43 72 65 61 74 65 54 61 62 6c 65 20 20 20 20 20  CreateTable     
507a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507b0 20 20 20 20 35 34 0a 23 64 65 66 69 6e 65 20 4f      54.#define O
507c0 50 5f 4c 61 73 74 20 20 20 20 20 20 20 20 20 20  P_Last          
507d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507e0 20 20 20 20 20 20 35 35 0a 23 64 65 66 69 6e 65        55.#define
507f0 20 4f 50 5f 53 65 65 6b 4c 65 20 20 20 20 20 20   OP_SeekLe      
50800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50810 20 20 20 20 20 20 20 20 35 36 0a 23 64 65 66 69          56.#defi
50820 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 20 20 20  ne OP_IsNull    
50830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50840 20 20 20 20 20 20 20 20 20 20 37 33 20 20 20 2f            73   /
50850 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e  * same as TK_ISN
50860 55 4c 4c 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  ULL   */.#define
50870 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d 20 20   OP_IncrVacuum  
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 35 37 0a 23 64 65 66 69          57.#defi
508a0 6e 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 20 20  ne OP_IdxRowid  
508b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
508c0 20 20 20 20 20 20 20 20 20 20 35 38 0a 23 64 65            58.#de
508d0 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 52 69 67  fine OP_ShiftRig
508e0 68 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ht              
508f0 20 20 20 20 20 20 20 20 20 20 20 20 38 35 20 20              85  
50900 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52   /* same as TK_R
50910 53 48 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69  SHIFT   */.#defi
50920 6e 65 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74  ne OP_ResetCount
50930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50940 20 20 20 20 20 20 20 20 20 20 35 39 0a 23 64 65            59.#de
50950 66 69 6e 65 20 4f 50 5f 59 69 65 6c 64 20 20 20  fine OP_Yield   
50960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50970 20 20 20 20 20 20 20 20 20 20 20 20 36 30 0a 23              60.#
50980 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 72  define OP_DropTr
50990 69 67 67 65 72 20 20 20 20 20 20 20 20 20 20 20  igger           
509a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 31                61
509b0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70  .#define OP_Drop
509c0 49 6e 64 65 78 20 20 20 20 20 20 20 20 20 20 20  Index           
509d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
509e0 36 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 61  62.#define OP_Pa
509f0 72 61 6d 20 20 20 20 20 20 20 20 20 20 20 20 20  ram             
50a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a10 20 20 36 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f    63.#define OP_
50a20 49 64 78 47 45 20 20 20 20 20 20 20 20 20 20 20  IdxGE           
50a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a40 20 20 20 20 36 34 0a 23 64 65 66 69 6e 65 20 4f      64.#define O
50a50 50 5f 49 64 78 44 65 6c 65 74 65 20 20 20 20 20  P_IdxDelete     
50a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a70 20 20 20 20 20 20 36 35 0a 23 64 65 66 69 6e 65        65.#define
50a80 20 4f 50 5f 56 61 63 75 75 6d 20 20 20 20 20 20   OP_Vacuum      
50a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50aa0 20 20 20 20 20 20 20 20 36 36 0a 23 64 65 66 69          66.#defi
50ab0 6e 65 20 4f 50 5f 49 66 4e 6f 74 20 20 20 20 20  ne OP_IfNot     
50ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ad0 20 20 20 20 20 20 20 20 20 20 36 37 0a 23 64 65            67.#de
50ae0 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c  fine OP_DropTabl
50af0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
50b00 20 20 20 20 20 20 20 20 20 20 20 20 37 30 0a 23              70.#
50b10 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 4c 74  define OP_SeekLt
50b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 31                71
50b40 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 61 6b 65  .#define OP_Make
50b50 52 65 63 6f 72 64 20 20 20 20 20 20 20 20 20 20  Record          
50b60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b70 37 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f  72.#define OP_To
50b80 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20  Blob            
50b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ba0 20 31 34 32 20 20 20 2f 2a 20 73 61 6d 65 20 61   142   /* same a
50bb0 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 2a 2f  s TK_TO_BLOB  */
50bc0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 73 75  .#define OP_Resu
50bd0 6c 74 52 6f 77 20 20 20 20 20 20 20 20 20 20 20  ltRow           
50be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50bf0 38 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65  81.#define OP_De
50c00 6c 65 74 65 20 20 20 20 20 20 20 20 20 20 20 20  lete            
50c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c20 20 20 39 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f    92.#define OP_
50c30 41 67 67 46 69 6e 61 6c 20 20 20 20 20 20 20 20  AggFinal        
50c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c50 20 20 20 20 39 35 0a 23 64 65 66 69 6e 65 20 4f      95.#define O
50c60 50 5f 43 6f 6d 70 61 72 65 20 20 20 20 20 20 20  P_Compare       
50c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c80 20 20 20 20 20 20 39 36 0a 23 64 65 66 69 6e 65        96.#define
50c90 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 20 20 20   OP_ShiftLeft   
50ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50cb0 20 20 20 20 20 20 20 20 38 34 20 20 20 2f 2a 20          84   /* 
50cc0 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48 49 46  same as TK_LSHIF
50cd0 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  T   */.#define O
50ce0 50 5f 47 6f 74 6f 20 20 20 20 20 20 20 20 20 20  P_Goto          
50cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d00 20 20 20 20 20 20 39 37 0a 23 64 65 66 69 6e 65        97.#define
50d10 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 20 20   OP_TableLock   
50d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d30 20 20 20 20 20 20 20 20 39 38 0a 23 64 65 66 69          98.#defi
50d40 6e 65 20 4f 50 5f 43 6c 65 61 72 20 20 20 20 20  ne OP_Clear     
50d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d60 20 20 20 20 20 20 20 20 20 20 39 39 0a 23 64 65            99.#de
50d70 66 69 6e 65 20 4f 50 5f 4c 65 20 20 20 20 20 20  fine OP_Le      
50d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d90 20 20 20 20 20 20 20 20 20 20 20 20 37 38 20 20              78  
50da0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c   /* same as TK_L
50db0 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  E       */.#defi
50dc0 6e 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b  ne OP_VerifyCook
50dd0 69 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ie              
50de0 20 20 20 20 20 20 20 20 20 31 30 30 0a 23 64 65           100.#de
50df0 66 69 6e 65 20 4f 50 5f 41 67 67 53 74 65 70 20  fine OP_AggStep 
50e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e10 20 20 20 20 20 20 20 20 20 20 20 31 30 31 0a 23             101.#
50e20 64 65 66 69 6e 65 20 4f 50 5f 54 6f 54 65 78 74  define OP_ToText
50e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e40 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 31               141
50e50 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
50e60 5f 54 4f 5f 54 45 58 54 20 20 2a 2f 0a 23 64 65  _TO_TEXT  */.#de
50e70 66 69 6e 65 20 4f 50 5f 4e 6f 74 20 20 20 20 20  fine OP_Not     
50e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e90 20 20 20 20 20 20 20 20 20 20 20 20 31 39 20 20              19  
50ea0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e   /* same as TK_N
50eb0 4f 54 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  OT      */.#defi
50ec0 6e 65 20 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20  ne OP_ToReal    
50ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ee0 20 20 20 20 20 20 20 20 20 31 34 35 20 20 20 2f           145   /
50ef0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f  * same as TK_TO_
50f00 52 45 41 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65  REAL  */.#define
50f10 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20   OP_Transaction 
50f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f30 20 20 20 20 20 20 20 31 30 32 0a 23 64 65 66 69         102.#defi
50f40 6e 65 20 4f 50 5f 56 46 69 6c 74 65 72 20 20 20  ne OP_VFilter   
50f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f60 20 20 20 20 20 20 20 20 20 31 30 33 0a 23 64 65           103.#de
50f70 66 69 6e 65 20 4f 50 5f 4e 65 20 20 20 20 20 20  fine OP_Ne      
50f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f90 20 20 20 20 20 20 20 20 20 20 20 20 37 35 20 20              75  
50fa0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e   /* same as TK_N
50fb0 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  E       */.#defi
50fc0 6e 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 20 20  ne OP_VDestroy  
50fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50fe0 20 20 20 20 20 20 20 20 20 31 30 34 0a 23 64 65           104.#de
50ff0 66 69 6e 65 20 4f 50 5f 42 69 74 4f 72 20 20 20  fine OP_BitOr   
51000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51010 20 20 20 20 20 20 20 20 20 20 20 20 38 33 20 20              83  
51020 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42   /* same as TK_B
51030 49 54 4f 52 20 20 20 20 2a 2f 0a 23 64 65 66 69  ITOR    */.#defi
51040 6e 65 20 4f 50 5f 4e 65 78 74 20 20 20 20 20 20  ne OP_Next      
51050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51060 20 20 20 20 20 20 20 20 20 31 30 35 0a 23 64 65           105.#de
51070 66 69 6e 65 20 4f 50 5f 43 6f 75 6e 74 20 20 20  fine OP_Count   
51080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51090 20 20 20 20 20 20 20 20 20 20 20 31 30 36 0a 23             106.#
510a0 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 49 6e 73  define OP_IdxIns
510b0 65 72 74 20 20 20 20 20 20 20 20 20 20 20 20 20  ert             
510c0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 37               107
510d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 74 20 20  .#define OP_Lt  
510e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
510f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51100 37 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  79   /* same as 
51110 54 4b 5f 4c 54 20 20 20 20 20 20 20 2a 2f 0a 23  TK_LT       */.#
51120 64 65 66 69 6e 65 20 4f 50 5f 46 6b 49 66 5a 65  define OP_FkIfZe
51130 72 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ro              
51140 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 38               108
51150 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b  .#define OP_Seek
51160 47 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Ge              
51170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51180 30 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e  09.#define OP_In
51190 73 65 72 74 20 20 20 20 20 20 20 20 20 20 20 20  sert            
511a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
511b0 20 31 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f   110.#define OP_
511c0 44 65 73 74 72 6f 79 20 20 20 20 20 20 20 20 20  Destroy         
511d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
511e0 20 20 20 31 31 31 0a 23 64 65 66 69 6e 65 20 4f     111.#define O
511f0 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20 20 20 20  P_ReadCookie    
51200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51210 20 20 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65       112.#define
51220 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 74 20 20   OP_RowSetTest  
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 31 31 33 0a 23 64 65 66 69         113.#defi
51250 6e 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73  ne OP_LoadAnalys
51260 69 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20  is              
51270 20 20 20 20 20 20 20 20 20 31 31 34 0a 23 64 65           114.#de
51280 66 69 6e 65 20 4f 50 5f 45 78 70 6c 61 69 6e 20  fine OP_Explain 
51290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
512a0 20 20 20 20 20 20 20 20 20 20 20 31 31 35 0a 23             115.#
512b0 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74 49 66  define OP_HaltIf
512c0 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20  Null            
512d0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 36               116
512e0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e  .#define OP_Open
512f0 50 73 65 75 64 6f 20 20 20 20 20 20 20 20 20 20  Pseudo          
51300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51310 31 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70  17.#define OP_Op
51320 65 6e 45 70 68 65 6d 65 72 61 6c 20 20 20 20 20  enEphemeral     
51330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51340 20 31 31 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f   118.#define OP_
51350 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20  Null            
51360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51370 20 20 20 31 31 39 0a 23 64 65 66 69 6e 65 20 4f     119.#define O
51380 50 5f 4d 6f 76 65 20 20 20 20 20 20 20 20 20 20  P_Move          
51390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
513a0 20 20 20 20 20 31 32 30 0a 23 64 65 66 69 6e 65       120.#define
513b0 20 4f 50 5f 42 6c 6f 62 20 20 20 20 20 20 20 20   OP_Blob        
513c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
513d0 20 20 20 20 20 20 20 31 32 31 0a 23 64 65 66 69         121.#defi
513e0 6e 65 20 4f 50 5f 41 64 64 20 20 20 20 20 20 20  ne OP_Add       
513f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51400 20 20 20 20 20 20 20 20 20 20 38 36 20 20 20 2f            86   /
51410 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55  * same as TK_PLU
51420 53 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  S     */.#define
51430 20 4f 50 5f 52 65 77 69 6e 64 20 20 20 20 20 20   OP_Rewind      
51440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51450 20 20 20 20 20 20 20 31 32 32 0a 23 64 65 66 69         122.#defi
51460 6e 65 20 4f 50 5f 53 65 65 6b 47 74 20 20 20 20  ne OP_SeekGt    
51470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51480 20 20 20 20 20 20 20 20 20 31 32 33 0a 23 64 65           123.#de
51490 66 69 6e 65 20 4f 50 5f 56 42 65 67 69 6e 20 20  fine OP_VBegin  
514a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
514b0 20 20 20 20 20 20 20 20 20 20 20 31 32 34 0a 23             124.#
514c0 64 65 66 69 6e 65 20 4f 50 5f 56 55 70 64 61 74  define OP_VUpdat
514d0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
514e0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 35               125
514f0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 5a 65  .#define OP_IfZe
51500 72 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ro              
51510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51520 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69  26.#define OP_Bi
51530 74 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 20  tNot            
51540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51550 20 20 39 33 20 20 20 2f 2a 20 73 61 6d 65 20 61    93   /* same a
51560 73 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20 2a 2f  s TK_BITNOT   */
51570 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 72 65  .#define OP_VCre
51580 61 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ate             
51590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
515a0 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 6f  27.#define OP_Fo
515b0 75 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20  und             
515c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
515d0 20 31 32 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f   128.#define OP_
515e0 49 66 50 6f 73 20 20 20 20 20 20 20 20 20 20 20  IfPos           
515f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51600 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65 20 4f     129.#define O
51610 50 5f 4e 75 6c 6c 52 6f 77 20 20 20 20 20 20 20  P_NullRow       
51620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51630 20 20 20 20 20 31 33 31 0a 23 64 65 66 69 6e 65       131.#define
51640 20 4f 50 5f 4a 75 6d 70 20 20 20 20 20 20 20 20   OP_Jump        
51650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51660 20 20 20 20 20 20 20 31 33 32 0a 23 64 65 66 69         132.#defi
51670 6e 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f  ne OP_Permutatio
51680 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
51690 20 20 20 20 20 20 20 20 20 31 33 33 0a 0a 2f 2a           133../*
516a0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f   The following o
516b0 70 63 6f 64 65 20 76 61 6c 75 65 73 20 61 72 65  pcode values are
516c0 20 6e 65 76 65 72 20 75 73 65 64 20 2a 2f 0a 23   never used */.#
516d0 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65  define OP_NotUse
516e0 64 5f 31 33 34 20 20 20 20 20 20 20 20 20 20 20  d_134           
516f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34               134
51700 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55  .#define OP_NotU
51710 73 65 64 5f 31 33 35 20 20 20 20 20 20 20 20 20  sed_135         
51720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51730 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f  35.#define OP_No
51740 74 55 73 65 64 5f 31 33 36 20 20 20 20 20 20 20  tUsed_136       
51750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51760 20 31 33 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f   136.#define OP_
51770 4e 6f 74 55 73 65 64 5f 31 33 37 20 20 20 20 20  NotUsed_137     
51780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51790 20 20 20 31 33 37 0a 23 64 65 66 69 6e 65 20 4f     137.#define O
517a0 50 5f 4e 6f 74 55 73 65 64 5f 31 33 38 20 20 20  P_NotUsed_138   
517b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
517c0 20 20 20 20 20 31 33 38 0a 23 64 65 66 69 6e 65       138.#define
517d0 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 39 20   OP_NotUsed_139 
517e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
517f0 20 20 20 20 20 20 20 31 33 39 0a 23 64 65 66 69         139.#defi
51800 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 34  ne OP_NotUsed_14
51810 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
51820 20 20 20 20 20 20 20 20 20 31 34 30 0a 0a 0a 2f           140.../
51830 2a 20 50 72 6f 70 65 72 74 69 65 73 20 73 75 63  * Properties suc
51840 68 20 61 73 20 22 6f 75 74 32 22 20 6f 72 20 22  h as "out2" or "
51850 6a 75 6d 70 22 20 74 68 61 74 20 61 72 65 20 73  jump" that are s
51860 70 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a 20 63  pecified in.** c
51870 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77 69 6e  omments followin
51880 67 20 74 68 65 20 22 63 61 73 65 22 20 66 6f 72  g the "case" for
51890 20 65 61 63 68 20 6f 70 63 6f 64 65 20 69 6e 20   each opcode in 
518a0 74 68 65 20 76 64 62 65 2e 63 0a 2a 2a 20 61 72  the vdbe.c.** ar
518b0 65 20 65 6e 63 6f 64 65 64 20 69 6e 74 6f 20 62  e encoded into b
518c0 69 74 76 65 63 74 6f 72 73 20 61 73 20 66 6f 6c  itvectors as fol
518d0 6c 6f 77 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65  lows:.*/.#define
518e0 20 4f 50 46 4c 47 5f 4a 55 4d 50 20 20 20 20 20   OPFLG_JUMP     
518f0 20 20 20 20 20 20 20 30 78 30 30 30 31 20 20 2f         0x0001  /
51900 2a 20 6a 75 6d 70 3a 20 20 50 32 20 68 6f 6c 64  * jump:  P2 hold
51910 73 20 6a 6d 70 20 74 61 72 67 65 74 20 2a 2f 0a  s jmp target */.
51920 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 4f 55  #define OPFLG_OU
51930 54 32 5f 50 52 45 52 45 4c 45 41 53 45 20 30 78  T2_PRERELEASE 0x
51940 30 30 30 32 20 20 2f 2a 20 6f 75 74 32 2d 70 72  0002  /* out2-pr
51950 65 72 65 6c 65 61 73 65 3a 20 2a 2f 0a 23 64 65  erelease: */.#de
51960 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 31 20 20  fine OPFLG_IN1  
51970 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30             0x000
51980 34 20 20 2f 2a 20 69 6e 31 3a 20 20 20 50 31 20  4  /* in1:   P1 
51990 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23  is an input */.#
519a0 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 32  define OPFLG_IN2
519b0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30               0x0
519c0 30 30 38 20 20 2f 2a 20 69 6e 32 3a 20 20 20 50  008  /* in2:   P
519d0 32 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f  2 is an input */
519e0 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49  .#define OPFLG_I
519f0 4e 33 20 20 20 20 20 20 20 20 20 20 20 20 20 30  N3             0
51a00 78 30 30 31 30 20 20 2f 2a 20 69 6e 33 3a 20 20  x0010  /* in3:  
51a10 20 50 33 20 69 73 20 61 6e 20 69 6e 70 75 74 20   P3 is an input 
51a20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47  */.#define OPFLG
51a30 5f 4f 55 54 33 20 20 20 20 20 20 20 20 20 20 20  _OUT3           
51a40 20 30 78 30 30 32 30 20 20 2f 2a 20 6f 75 74 33   0x0020  /* out3
51a50 3a 20 20 50 33 20 69 73 20 61 6e 20 6f 75 74 70  :  P3 is an outp
51a60 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  ut */.#define OP
51a70 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20  FLG_INITIALIZER 
51a80 7b 5c 0a 2f 2a 20 20 20 30 20 2a 2f 20 30 78 30  {\./*   0 */ 0x0
51a90 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30  0, 0x01, 0x00, 0
51aa0 78 30 30 2c 20 30 78 31 30 2c 20 30 78 30 38 2c  x00, 0x10, 0x08,
51ab0 20 30 78 30 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a   0x02, 0x00,\./*
51ac0 20 20 20 38 20 2a 2f 20 30 78 30 30 2c 20 30 78     8 */ 0x00, 0x
51ad0 30 34 2c 20 30 78 30 30 2c 20 30 78 30 35 2c 20  04, 0x00, 0x05, 
51ae0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
51af0 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 31 36 20  , 0x00,\./*  16 
51b00 2a 2f 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30  */ 0x02, 0x00, 0
51b10 78 30 31 2c 20 30 78 30 34 2c 20 30 78 30 34 2c  x01, 0x04, 0x04,
51b20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
51b30 35 2c 5c 0a 2f 2a 20 20 32 34 20 2a 2f 20 30 78  5,\./*  24 */ 0x
51b40 30 30 2c 20 30 78 30 31 2c 20 30 78 30 34 2c 20  00, 0x01, 0x04, 
51b50 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30  0x02, 0x00, 0x00
51b60 2c 20 30 78 30 32 2c 20 30 78 30 34 2c 5c 0a 2f  , 0x02, 0x04,\./
51b70 2a 20 20 33 32 20 2a 2f 20 30 78 30 30 2c 20 30  *  32 */ 0x00, 0
51b80 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
51b90 20 30 78 30 32 2c 20 30 78 31 31 2c 20 30 78 31   0x02, 0x11, 0x1
51ba0 31 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 20 34 30  1, 0x02,\./*  40
51bb0 20 2a 2f 20 30 78 30 35 2c 20 30 78 30 30 2c 20   */ 0x05, 0x00, 
51bc0 30 78 30 32 2c 20 30 78 31 31 2c 20 30 78 30 34  0x02, 0x11, 0x04
51bd0 2c 20 30 78 30 30 2c 20 30 78 30 38 2c 20 30 78  , 0x00, 0x08, 0x
51be0 31 31 2c 5c 0a 2f 2a 20 20 34 38 20 2a 2f 20 30  11,\./*  48 */ 0
51bf0 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 31 2c  x01, 0x02, 0x01,
51c00 20 30 78 32 31 2c 20 30 78 30 38 2c 20 30 78 30   0x21, 0x08, 0x0
51c10 30 2c 20 30 78 30 32 2c 20 30 78 30 31 2c 5c 0a  0, 0x02, 0x01,\.
51c20 2f 2a 20 20 35 36 20 2a 2f 20 30 78 31 31 2c 20  /*  56 */ 0x11, 
51c30 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 30  0x01, 0x02, 0x00
51c40 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 20 30 78  , 0x04, 0x00, 0x
51c50 30 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 20 36  00, 0x02,\./*  6
51c60 34 20 2a 2f 20 30 78 31 31 2c 20 30 78 30 30 2c  4 */ 0x11, 0x00,
51c70 20 30 78 30 30 2c 20 30 78 30 35 2c 20 30 78 32   0x00, 0x05, 0x2
51c80 63 2c 20 30 78 32 63 2c 20 30 78 30 30 2c 20 30  c, 0x2c, 0x00, 0
51c90 78 31 31 2c 5c 0a 2f 2a 20 20 37 32 20 2a 2f 20  x11,\./*  72 */ 
51ca0 30 78 30 30 2c 20 30 78 30 35 2c 20 30 78 30 35  0x00, 0x05, 0x05
51cb0 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78  , 0x15, 0x15, 0x
51cc0 31 35 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 5c  15, 0x15, 0x15,\
51cd0 0a 2f 2a 20 20 38 30 20 2a 2f 20 30 78 31 35 2c  ./*  80 */ 0x15,
51ce0 20 30 78 30 30 2c 20 30 78 32 63 2c 20 30 78 32   0x00, 0x2c, 0x2
51cf0 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30  c, 0x2c, 0x2c, 0
51d00 78 32 63 2c 20 30 78 32 63 2c 5c 0a 2f 2a 20 20  x2c, 0x2c,\./*  
51d10 38 38 20 2a 2f 20 30 78 32 63 2c 20 30 78 32 63  88 */ 0x2c, 0x2c
51d20 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78  , 0x2c, 0x2c, 0x
51d30 30 30 2c 20 30 78 30 34 2c 20 30 78 30 32 2c 20  00, 0x04, 0x02, 
51d40 30 78 30 30 2c 5c 0a 2f 2a 20 20 39 36 20 2a 2f  0x00,\./*  96 */
51d50 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30   0x00, 0x01, 0x0
51d60 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
51d70 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 31 2c  x00, 0x00, 0x01,
51d80 5c 0a 2f 2a 20 31 30 34 20 2a 2f 20 30 78 30 30  \./* 104 */ 0x00
51d90 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78  , 0x01, 0x02, 0x
51da0 30 38 2c 20 30 78 30 31 2c 20 30 78 31 31 2c 20  08, 0x01, 0x11, 
51db0 30 78 30 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20  0x00, 0x02,\./* 
51dc0 31 31 32 20 2a 2f 20 30 78 30 32 2c 20 30 78 31  112 */ 0x02, 0x1
51dd0 35 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  5, 0x00, 0x00, 0
51de0 78 31 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x10, 0x00, 0x00,
51df0 20 30 78 30 32 2c 5c 0a 2f 2a 20 31 32 30 20 2a   0x02,\./* 120 *
51e00 2f 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78  / 0x00, 0x02, 0x
51e10 30 31 2c 20 30 78 31 31 2c 20 30 78 30 30 2c 20  01, 0x11, 0x00, 
51e20 30 78 30 30 2c 20 30 78 30 35 2c 20 30 78 30 30  0x00, 0x05, 0x00
51e30 2c 5c 0a 2f 2a 20 31 32 38 20 2a 2f 20 30 78 31  ,\./* 128 */ 0x1
51e40 31 2c 20 30 78 30 35 2c 20 30 78 30 32 2c 20 30  1, 0x05, 0x02, 0
51e50 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c  x00, 0x01, 0x00,
51e60 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f 2a   0x00, 0x00,\./*
51e70 20 31 33 36 20 2a 2f 20 30 78 30 30 2c 20 30 78   136 */ 0x00, 0x
51e80 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
51e90 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30 34  0x00, 0x04, 0x04
51ea0 2c 20 30 78 30 34 2c 5c 0a 2f 2a 20 31 34 34 20  , 0x04,\./* 144 
51eb0 2a 2f 20 30 78 30 34 2c 20 30 78 30 34 2c 7d 0a  */ 0x04, 0x04,}.
51ec0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
51ed0 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65 73 2e   End of opcodes.
51ee0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
51ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
51f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
51f10 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
51f20 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
51f30 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
51f40 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a   vdbe.h ********
51f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
51f60 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f 74 79 70  ../*.** Prototyp
51f70 65 73 20 66 6f 72 20 74 68 65 20 56 44 42 45 20  es for the VDBE 
51f80 69 6e 74 65 72 66 61 63 65 2e 20 20 53 65 65 20  interface.  See 
51f90 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20  comments on the 
51fa0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a  implementation.*
51fb0 2a 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74  * for a descript
51fc0 69 6f 6e 20 6f 66 20 77 68 61 74 20 65 61 63 68  ion of what each
51fd0 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e   of these routin
51fe0 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53 51 4c 49  es does..*/.SQLI
51ff0 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20  TE_PRIVATE Vdbe 
52000 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 61  *sqlite3VdbeCrea
52010 74 65 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  te(sqlite3*);.SQ
52020 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
52030 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
52040 70 30 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53  p0(Vdbe*,int);.S
52050 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
52060 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  t sqlite3VdbeAdd
52070 4f 70 31 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e  Op1(Vdbe*,int,in
52080 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
52090 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
520a0 62 65 41 64 64 4f 70 32 28 56 64 62 65 2a 2c 69  beAddOp2(Vdbe*,i
520b0 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c  nt,int,int);.SQL
520c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
520d0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
520e0 33 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c  3(Vdbe*,int,int,
520f0 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  int,int);.SQLITE
52100 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
52110 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 56  ite3VdbeAddOp4(V
52120 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  dbe*,int,int,int
52130 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 20  ,int,const char 
52140 2a 7a 50 34 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  *zP4,int);.SQLIT
52150 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
52160 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
52170 73 74 28 56 64 62 65 2a 2c 20 69 6e 74 20 6e 4f  st(Vdbe*, int nO
52180 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6f  p, VdbeOpList co
52190 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51 4c 49 54  nst *aOp);.SQLIT
521a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
521b0 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
521c0 50 31 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64  P1(Vdbe*, int ad
521d0 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a 53 51 4c  dr, int P1);.SQL
521e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
521f0 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
52200 67 65 50 32 28 56 64 62 65 2a 2c 20 69 6e 74 20  geP2(Vdbe*, int 
52210 61 64 64 72 2c 20 69 6e 74 20 50 32 29 3b 0a 53  addr, int P2);.S
52220 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
52230 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  id sqlite3VdbeCh
52240 61 6e 67 65 50 33 28 56 64 62 65 2a 2c 20 69 6e  angeP3(Vdbe*, in
52250 74 20 61 64 64 72 2c 20 69 6e 74 20 50 33 29 3b  t addr, int P3);
52260 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
52270 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
52280 43 68 61 6e 67 65 50 35 28 56 64 62 65 2a 2c 20  ChangeP5(Vdbe*, 
52290 75 38 20 50 35 29 3b 0a 53 51 4c 49 54 45 5f 50  u8 P5);.SQLITE_P
522a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
522b0 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
522c0 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 29  Vdbe*, int addr)
522d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
522e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
522f0 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 56 64  eChangeToNoop(Vd
52300 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 69  be*, int addr, i
52310 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f 50 52  nt N);.SQLITE_PR
52320 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
52330 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 56  e3VdbeChangeP4(V
52340 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20  dbe*, int addr, 
52350 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 2c  const char *zP4,
52360 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f   int N);.SQLITE_
52370 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
52380 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65  ite3VdbeUsesBtre
52390 65 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53  e(Vdbe*, int);.S
523a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64  QLITE_PRIVATE Vd
523b0 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 64 62  beOp *sqlite3Vdb
523c0 65 47 65 74 4f 70 28 56 64 62 65 2a 2c 20 69 6e  eGetOp(Vdbe*, in
523d0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
523e0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
523f0 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 62 65  beMakeLabel(Vdbe
52400 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
52410 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
52420 64 62 65 44 65 6c 65 74 65 28 56 64 62 65 2a 29  dbeDelete(Vdbe*)
52430 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
52440 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
52450 65 4d 61 6b 65 52 65 61 64 79 28 56 64 62 65 2a  eMakeReady(Vdbe*
52460 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  ,int,int,int,int
52470 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ,int,int);.SQLIT
52480 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
52490 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a  lite3VdbeFinaliz
524a0 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45  e(Vdbe*);.SQLITE
524b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
524c0 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
524d0 4c 61 62 65 6c 28 56 64 62 65 2a 2c 20 69 6e 74  Label(Vdbe*, int
524e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
524f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
52500 65 43 75 72 72 65 6e 74 41 64 64 72 28 56 64 62  eCurrentAddr(Vdb
52510 65 2a 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  e*);.#ifdef SQLI
52520 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f  TE_DEBUG.SQLITE_
52530 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
52540 6c 69 74 65 33 56 64 62 65 41 73 73 65 72 74 4d  lite3VdbeAssertM
52550 61 79 41 62 6f 72 74 28 56 64 62 65 20 2a 2c 20  ayAbort(Vdbe *, 
52560 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
52570 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
52580 74 65 33 56 64 62 65 54 72 61 63 65 28 56 64 62  te3VdbeTrace(Vdb
52590 65 2a 2c 46 49 4c 45 2a 29 3b 0a 23 65 6e 64 69  e*,FILE*);.#endi
525a0 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  f.SQLITE_PRIVATE
525b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
525c0 65 52 65 73 65 74 53 74 65 70 52 65 73 75 6c 74  eResetStepResult
525d0 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Vdbe*);.SQLITE_
525e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
525f0 74 65 33 56 64 62 65 52 65 73 65 74 28 56 64 62  te3VdbeReset(Vdb
52600 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
52610 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
52620 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 56  VdbeSetNumCols(V
52630 64 62 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  dbe*,int);.SQLIT
52640 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
52650 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
52660 61 6d 65 28 56 64 62 65 2a 2c 20 69 6e 74 2c 20  ame(Vdbe*, int, 
52670 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  int, const char 
52680 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  *, void(*)(void*
52690 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ));.SQLITE_PRIVA
526a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
526b0 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65 73 28  dbeCountChanges(
526c0 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Vdbe*);.SQLITE_P
526d0 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 20 2a  RIVATE sqlite3 *
526e0 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 56 64  sqlite3VdbeDb(Vd
526f0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
52700 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
52710 33 56 64 62 65 53 65 74 53 71 6c 28 56 64 62 65  3VdbeSetSql(Vdbe
52720 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  *, const char *z
52730 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 29 3b 0a 53  , int n, int);.S
52740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
52750 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 77  id sqlite3VdbeSw
52760 61 70 28 56 64 62 65 2a 2c 56 64 62 65 2a 29 3b  ap(Vdbe*,Vdbe*);
52770 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
52780 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56  VdbeOp *sqlite3V
52790 64 62 65 54 61 6b 65 4f 70 41 72 72 61 79 28 56  dbeTakeOpArray(V
527a0 64 62 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 2a  dbe*, int*, int*
527b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
527c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
527d0 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 74 65 28  beProgramDelete(
527e0 73 71 6c 69 74 65 33 20 2a 2c 20 53 75 62 50 72  sqlite3 *, SubPr
527f0 6f 67 72 61 6d 20 2a 2c 20 69 6e 74 29 3b 0a 0a  ogram *, int);..
52800 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
52810 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41  ABLE_MEMORY_MANA
52820 47 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52  GEMENT.SQLITE_PR
52830 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
52840 33 56 64 62 65 52 65 6c 65 61 73 65 4d 65 6d 6f  3VdbeReleaseMemo
52850 72 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a  ry(int);.#endif.
52860 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 55  SQLITE_PRIVATE U
52870 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 73  npackedRecord *s
52880 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64  qlite3VdbeRecord
52890 55 6e 70 61 63 6b 28 4b 65 79 49 6e 66 6f 2a 2c  Unpack(KeyInfo*,
528a0 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
528b0 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  char*,int);.SQLI
528c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
528d0 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74  sqlite3VdbeDelet
528e0 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28  eUnpackedRecord(
528f0 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29  UnpackedRecord*)
52900 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
52910 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
52920 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 69 6e  RecordCompare(in
52930 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 55 6e  t,const void*,Un
52940 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a  packedRecord*);.
52950 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
52960 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
52970 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64    void sqlite3Vd
52980 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65 2a 2c  beComment(Vdbe*,
52990 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e   const char*, ..
529a0 2e 29 3b 0a 23 20 64 65 66 69 6e 65 20 56 64 62  .);.# define Vdb
529b0 65 43 6f 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c  eComment(X)  sql
529c0 69 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 20  ite3VdbeComment 
529d0 58 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  X.SQLITE_PRIVATE
529e0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56     void sqlite3V
529f0 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56  dbeNoopComment(V
52a00 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  dbe*, const char
52a10 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e  *, ...);.# defin
52a20 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e  e VdbeNoopCommen
52a30 74 28 58 29 20 20 73 71 6c 69 74 65 33 56 64 62  t(X)  sqlite3Vdb
52a40 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 20 58 0a 23  eNoopComment X.#
52a50 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56 64  else.# define Vd
52a60 62 65 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 20 64  beComment(X).# d
52a70 65 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f  efine VdbeNoopCo
52a80 6d 6d 65 6e 74 28 58 29 0a 23 65 6e 64 69 66 0a  mment(X).#endif.
52a90 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
52aa0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
52ab0 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  vdbe.h *********
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 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
52af0 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
52b00 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
52b10 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49  t off in sqliteI
52b20 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
52b30 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
52b40 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
52b50 20 70 61 67 65 72 2e 68 20 69 6e 20 74 68 65 20   pager.h in the 
52b60 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65  middle of sqlite
52b70 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
52b80 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
52b90 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
52ba0 69 6c 65 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a  ile pager.h ****
52bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52bd0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
52be0 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
52bf0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
52c00 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
52c10 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
52c20 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
52c30 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
52c40 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
52c50 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
52c60 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
52c70 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
52c80 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
52c90 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
52ca0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
52cb0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
52cc0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
52cd0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
52ce0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
52cf0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
52d00 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
52d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
52d50 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
52d60 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20  ile defines the 
52d70 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74  interface that t
52d80 68 65 20 73 71 6c 69 74 65 20 70 61 67 65 20 63  he sqlite page c
52d90 61 63 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65  ache.** subsyste
52da0 6d 2e 20 20 54 68 65 20 70 61 67 65 20 63 61 63  m.  The page cac
52db0 68 65 20 73 75 62 73 79 73 74 65 6d 20 72 65 61  he subsystem rea
52dc0 64 73 20 61 6e 64 20 77 72 69 74 65 73 20 61 20  ds and writes a 
52dd0 66 69 6c 65 20 61 20 70 61 67 65 0a 2a 2a 20 61  file a page.** a
52de0 74 20 61 20 74 69 6d 65 20 61 6e 64 20 70 72 6f  t a time and pro
52df0 76 69 64 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20  vides a journal 
52e00 66 6f 72 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a  for rollback..**
52e10 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 70 61  .** @(#) $Id: pa
52e20 67 65 72 2e 68 2c 76 20 31 2e 31 30 34 20 32 30  ger.h,v 1.104 20
52e30 30 39 2f 30 37 2f 32 34 20 31 39 3a 30 31 3a 31  09/07/24 19:01:1
52e40 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  9 drh Exp $.*/..
52e50 23 69 66 6e 64 65 66 20 5f 50 41 47 45 52 5f 48  #ifndef _PAGER_H
52e60 5f 0a 23 64 65 66 69 6e 65 20 5f 50 41 47 45 52  _.#define _PAGER
52e70 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61 75  _H_../*.** Defau
52e80 6c 74 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20  lt maximum size 
52e90 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a  for persistent j
52ea0 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 20 41 20  ournal files. A 
52eb0 6e 65 67 61 74 69 76 65 20 0a 2a 2a 20 76 61 6c  negative .** val
52ec0 75 65 20 6d 65 61 6e 73 20 6e 6f 20 6c 69 6d 69  ue means no limi
52ed0 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20 6d 61  t. This value ma
52ee0 79 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20  y be overridden 
52ef0 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20 73 71  using the .** sq
52f00 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61  lite3PagerJourna
52f10 6c 53 69 7a 65 4c 69 6d 69 74 28 29 20 41 50 49  lSizeLimit() API
52f20 2e 20 53 65 65 20 61 6c 73 6f 20 22 50 52 41 47  . See also "PRAG
52f30 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f  MA journal_size_
52f40 6c 69 6d 69 74 22 2e 0a 2a 2f 0a 23 69 66 6e 64  limit"..*/.#ifnd
52f50 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ef SQLITE_DEFAUL
52f60 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c  T_JOURNAL_SIZE_L
52f70 49 4d 49 54 0a 20 20 23 64 65 66 69 6e 65 20 53  IMIT.  #define S
52f80 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4a 4f  QLITE_DEFAULT_JO
52f90 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54  URNAL_SIZE_LIMIT
52fa0 20 2d 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a   -1.#endif../*.*
52fb0 2a 20 54 68 65 20 74 79 70 65 20 75 73 65 64 20  * The type used 
52fc0 74 6f 20 72 65 70 72 65 73 65 6e 74 20 61 20 70  to represent a p
52fd0 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 54 68 65  age number.  The
52fe0 20 66 69 72 73 74 20 70 61 67 65 20 69 6e 20 61   first page in a
52ff0 20 66 69 6c 65 0a 2a 2a 20 69 73 20 63 61 6c 6c   file.** is call
53000 65 64 20 70 61 67 65 20 31 2e 20 20 30 20 69 73  ed page 1.  0 is
53010 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 65   used to represe
53020 6e 74 20 22 6e 6f 74 20 61 20 70 61 67 65 22 2e  nt "not a page".
53030 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 33 32 20  .*/.typedef u32 
53040 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  Pgno;../*.** Eac
53050 68 20 6f 70 65 6e 20 66 69 6c 65 20 69 73 20 6d  h open file is m
53060 61 6e 61 67 65 64 20 62 79 20 61 20 73 65 70 61  anaged by a sepa
53070 72 61 74 65 20 69 6e 73 74 61 6e 63 65 20 6f 66  rate instance of
53080 20 74 68 65 20 22 50 61 67 65 72 22 20 73 74 72   the "Pager" str
53090 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 64  ucture..*/.typed
530a0 65 66 20 73 74 72 75 63 74 20 50 61 67 65 72 20  ef struct Pager 
530b0 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 48 61  Pager;../*.** Ha
530c0 6e 64 6c 65 20 74 79 70 65 20 66 6f 72 20 70 61  ndle type for pa
530d0 67 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ges..*/.typedef 
530e0 73 74 72 75 63 74 20 50 67 48 64 72 20 44 62 50  struct PgHdr DbP
530f0 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 65  age;../*.** Page
53100 20 6e 75 6d 62 65 72 20 50 41 47 45 52 5f 4d 4a   number PAGER_MJ
53110 5f 50 47 4e 4f 20 69 73 20 6e 65 76 65 72 20 75  _PGNO is never u
53120 73 65 64 20 69 6e 20 61 6e 20 53 51 4c 69 74 65  sed in an SQLite
53130 20 64 61 74 61 62 61 73 65 20 28 69 74 20 69 73   database (it is
53140 0a 2a 2a 20 72 65 73 65 72 76 65 64 20 66 6f 72  .** reserved for
53150 20 77 6f 72 6b 69 6e 67 20 61 72 6f 75 6e 64 20   working around 
53160 61 20 77 69 6e 64 6f 77 73 2f 70 6f 73 69 78 20  a windows/posix 
53170 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 29  incompatibility)
53180 2e 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64 20  . It is.** used 
53190 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74  in the journal t
531a0 6f 20 73 69 67 6e 69 66 79 20 74 68 61 74 20 74  o signify that t
531b0 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20  he remainder of 
531c0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
531d0 20 0a 2a 2a 20 69 73 20 64 65 76 6f 74 65 64 20   .** is devoted 
531e0 74 6f 20 73 74 6f 72 69 6e 67 20 61 20 6d 61 73  to storing a mas
531f0 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  ter journal name
53200 20 2d 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20   - there are no 
53210 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 0a 2a 2a  more pages to.**
53220 20 72 6f 6c 6c 20 62 61 63 6b 2e 20 53 65 65 20   roll back. See 
53230 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 66 75 6e  comments for fun
53240 63 74 69 6f 6e 20 77 72 69 74 65 4d 61 73 74 65  ction writeMaste
53250 72 4a 6f 75 72 6e 61 6c 28 29 20 69 6e 20 70 61  rJournal() in pa
53260 67 65 72 2e 63 20 0a 2a 2a 20 66 6f 72 20 64 65  ger.c .** for de
53270 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  tails..*/.#defin
53280 65 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28  e PAGER_MJ_PGNO(
53290 78 29 20 28 28 50 67 6e 6f 29 28 28 50 45 4e 44  x) ((Pgno)((PEND
532a0 49 4e 47 5f 42 59 54 45 2f 28 28 78 29 2d 3e 70  ING_BYTE/((x)->p
532b0 61 67 65 53 69 7a 65 29 29 2b 31 29 29 0a 0a 2f  ageSize))+1))../
532c0 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c  *.** Allowed val
532d0 75 65 73 20 66 6f 72 20 74 68 65 20 66 6c 61 67  ues for the flag
532e0 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73  s parameter to s
532f0 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28  qlite3PagerOpen(
53300 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 54  )..**.** NOTE: T
53310 68 65 73 65 20 76 61 6c 75 65 73 20 6d 75 73 74  hese values must
53320 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72 65   match the corre
53330 73 70 6f 6e 64 69 6e 67 20 42 54 52 45 45 5f 20  sponding BTREE_ 
53340 76 61 6c 75 65 73 20 69 6e 20 62 74 72 65 65 2e  values in btree.
53350 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41  h..*/.#define PA
53360 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c  GER_OMIT_JOURNAL
53370 20 20 30 78 30 30 30 31 20 20 20 20 2f 2a 20 44    0x0001    /* D
53380 6f 20 6e 6f 74 20 75 73 65 20 61 20 72 6f 6c 6c  o not use a roll
53390 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  back journal */.
533a0 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4e 4f  #define PAGER_NO
533b0 5f 52 45 41 44 4c 4f 43 4b 20 20 20 30 78 30 30  _READLOCK   0x00
533c0 30 32 20 20 20 20 2f 2a 20 4f 6d 69 74 20 72 65  02    /* Omit re
533d0 61 64 6c 6f 63 6b 73 20 6f 6e 20 72 65 61 64 6f  adlocks on reado
533e0 6e 6c 79 20 66 69 6c 65 73 20 2a 2f 0a 0a 2f 2a  nly files */../*
533f0 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75 65 73  .** Valid values
53400 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20   for the second 
53410 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69  argument to sqli
53420 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d  te3PagerLockingM
53430 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ode()..*/.#defin
53440 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  e PAGER_LOCKINGM
53450 4f 44 45 5f 51 55 45 52 59 20 20 20 20 20 20 2d  ODE_QUERY      -
53460 31 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f  1.#define PAGER_
53470 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d  LOCKINGMODE_NORM
53480 41 4c 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e  AL      0.#defin
53490 65 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  e PAGER_LOCKINGM
534a0 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 20 20  ODE_EXCLUSIVE   
534b0 31 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76  1../*.** Valid v
534c0 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 65  alues for the se
534d0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  cond argument to
534e0 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75   sqlite3PagerJou
534f0 72 6e 61 6c 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23  rnalMode()..*/.#
53500 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55  define PAGER_JOU
53510 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 20 20  RNALMODE_QUERY  
53520 20 20 20 20 2d 31 0a 23 64 65 66 69 6e 65 20 50      -1.#define P
53530 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
53540 5f 44 45 4c 45 54 45 20 20 20 20 20 20 30 20 20  _DELETE      0  
53550 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 64 65   /* Commit by de
53560 6c 65 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 66  leting journal f
53570 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ile */.#define P
53580 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
53590 5f 50 45 52 53 49 53 54 20 20 20 20 20 31 20 20  _PERSIST     1  
535a0 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 7a 65   /* Commit by ze
535b0 72 6f 69 6e 67 20 6a 6f 75 72 6e 61 6c 20 68 65  roing journal he
535c0 61 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ader */.#define 
535d0 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
535e0 45 5f 4f 46 46 20 20 20 20 20 20 20 20 20 32 20  E_OFF         2 
535f0 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 6f 6d 69    /* Journal omi
53600 74 74 65 64 2e 20 20 2a 2f 0a 23 64 65 66 69 6e  tted.  */.#defin
53610 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  e PAGER_JOURNALM
53620 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 20 20 20  ODE_TRUNCATE    
53630 33 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62 79  3   /* Commit by
53640 20 74 72 75 6e 63 61 74 69 6e 67 20 6a 6f 75 72   truncating jour
53650 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  nal */.#define P
53660 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
53670 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20 34 20 20  _MEMORY      4  
53680 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72 79 20 6a 6f   /* In-memory jo
53690 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 0a 2f  urnal file */../
536a0 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 64  *.** The remaind
536b0 65 72 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20  er of this file 
536c0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 65 63  contains the dec
536d0 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  larations of the
536e0 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 74 68   functions.** th
536f0 61 74 20 6d 61 6b 65 20 75 70 20 74 68 65 20 50  at make up the P
53700 61 67 65 72 20 73 75 62 2d 73 79 73 74 65 6d 20  ager sub-system 
53710 41 50 49 2e 20 53 65 65 20 73 6f 75 72 63 65 20  API. See source 
53720 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f  code comments fo
53730 72 20 0a 2a 2a 20 61 20 64 65 74 61 69 6c 65 64  r .** a detailed
53740 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20   description of 
53750 65 61 63 68 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f  each routine..*/
53760 0a 0a 2f 2a 20 4f 70 65 6e 20 61 6e 64 20 63 6c  ../* Open and cl
53770 6f 73 65 20 61 20 50 61 67 65 72 20 63 6f 6e 6e  ose a Pager conn
53780 65 63 74 69 6f 6e 2e 20 2a 2f 20 0a 53 51 4c 49  ection. */ .SQLI
53790 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
537a0 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 28  qlite3PagerOpen(
537b0 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c  .  sqlite3_vfs*,
537c0 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50 61 67  .  Pager **ppPag
537d0 65 72 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  er,.  const char
537e0 2a 2c 0a 20 20 69 6e 74 2c 0a 20 20 69 6e 74 2c  *,.  int,.  int,
537f0 0a 20 20 69 6e 74 2c 0a 20 20 76 6f 69 64 28 2a  .  int,.  void(*
53800 29 28 44 62 50 61 67 65 2a 29 0a 29 3b 0a 53 51  )(DbPage*).);.SQ
53810 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
53820 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f   sqlite3PagerClo
53830 73 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  se(Pager *pPager
53840 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
53850 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
53860 65 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 72  erReadFileheader
53870 28 50 61 67 65 72 2a 2c 20 69 6e 74 2c 20 75 6e  (Pager*, int, un
53880 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a 0a  signed char*);..
53890 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 73 65  /* Functions use
538a0 64 20 74 6f 20 63 6f 6e 66 69 67 75 72 65 20 61  d to configure a
538b0 20 50 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 2a   Pager object. *
538c0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
538d0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67   void sqlite3Pag
538e0 65 72 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72  erSetBusyhandler
538f0 28 50 61 67 65 72 2a 2c 20 69 6e 74 28 2a 29 28  (Pager*, int(*)(
53900 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64 20 2a 29  void *), void *)
53910 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
53920 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
53930 72 53 65 74 50 61 67 65 73 69 7a 65 28 50 61 67  rSetPagesize(Pag
53940 65 72 2a 2c 20 75 31 36 2a 2c 20 69 6e 74 29 3b  er*, u16*, int);
53950 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
53960 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
53970 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 50 61 67  MaxPageCount(Pag
53980 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  er*, int);.SQLIT
53990 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
539a0 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 61  qlite3PagerSetCa
539b0 63 68 65 73 69 7a 65 28 50 61 67 65 72 2a 2c 20  chesize(Pager*, 
539c0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
539d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
539e0 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79 4c  3PagerSetSafetyL
539f0 65 76 65 6c 28 50 61 67 65 72 2a 2c 69 6e 74 2c  evel(Pager*,int,
53a00 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
53a10 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
53a20 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65  PagerLockingMode
53a30 28 50 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a  (Pager *, int);.
53a40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
53a50 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a  nt sqlite3PagerJ
53a60 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61 67 65 72  ournalMode(Pager
53a70 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45   *, int);.SQLITE
53a80 5f 50 52 49 56 41 54 45 20 69 36 34 20 73 71 6c  _PRIVATE i64 sql
53a90 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c  ite3PagerJournal
53aa0 53 69 7a 65 4c 69 6d 69 74 28 50 61 67 65 72 20  SizeLimit(Pager 
53ab0 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f  *, i64);.SQLITE_
53ac0 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f  PRIVATE sqlite3_
53ad0 62 61 63 6b 75 70 20 2a 2a 73 71 6c 69 74 65 33  backup **sqlite3
53ae0 50 61 67 65 72 42 61 63 6b 75 70 50 74 72 28 50  PagerBackupPtr(P
53af0 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63  ager*);../* Func
53b00 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 6f 62  tions used to ob
53b10 74 61 69 6e 20 61 6e 64 20 72 65 6c 65 61 73 65  tain and release
53b20 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 73   page references
53b30 2e 20 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49  . */ .SQLITE_PRI
53b40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
53b50 50 61 67 65 72 41 63 71 75 69 72 65 28 50 61 67  PagerAcquire(Pag
53b60 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f  er *pPager, Pgno
53b70 20 70 67 6e 6f 2c 20 44 62 50 61 67 65 20 2a 2a   pgno, DbPage **
53b80 70 70 50 61 67 65 2c 20 69 6e 74 20 63 6c 72 46  ppPage, int clrF
53b90 6c 61 67 29 3b 0a 23 64 65 66 69 6e 65 20 73 71  lag);.#define sq
53ba0 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 41 2c  lite3PagerGet(A,
53bb0 42 2c 43 29 20 73 71 6c 69 74 65 33 50 61 67 65  B,C) sqlite3Page
53bc0 72 41 63 71 75 69 72 65 28 41 2c 42 2c 43 2c 30  rAcquire(A,B,C,0
53bd0 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ).SQLITE_PRIVATE
53be0 20 44 62 50 61 67 65 20 2a 73 71 6c 69 74 65 33   DbPage *sqlite3
53bf0 50 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 67 65  PagerLookup(Page
53c00 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20  r *pPager, Pgno 
53c10 70 67 6e 6f 29 3b 0a 53 51 4c 49 54 45 5f 50 52  pgno);.SQLITE_PR
53c20 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
53c30 65 33 50 61 67 65 72 52 65 66 28 44 62 50 61 67  e3PagerRef(DbPag
53c40 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
53c50 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
53c60 50 61 67 65 72 55 6e 72 65 66 28 44 62 50 61 67  PagerUnref(DbPag
53c70 65 2a 29 3b 0a 0a 2f 2a 20 4f 70 65 72 61 74 69  e*);../* Operati
53c80 6f 6e 73 20 6f 6e 20 70 61 67 65 20 72 65 66 65  ons on page refe
53c90 72 65 6e 63 65 73 2e 20 2a 2f 0a 53 51 4c 49 54  rences. */.SQLIT
53ca0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
53cb0 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
53cc0 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45  DbPage*);.SQLITE
53cd0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
53ce0 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72  lite3PagerDontWr
53cf0 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51  ite(DbPage*);.SQ
53d00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
53d10 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76   sqlite3PagerMov
53d20 65 70 61 67 65 28 50 61 67 65 72 2a 2c 44 62 50  epage(Pager*,DbP
53d30 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b 0a  age*,Pgno,int);.
53d40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
53d50 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 50  nt sqlite3PagerP
53d60 61 67 65 52 65 66 63 6f 75 6e 74 28 44 62 50 61  ageRefcount(DbPa
53d70 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ge*);.SQLITE_PRI
53d80 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
53d90 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 44  e3PagerGetData(D
53da0 62 50 61 67 65 20 2a 29 3b 20 0a 53 51 4c 49 54  bPage *); .SQLIT
53db0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
53dc0 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45  sqlite3PagerGetE
53dd0 78 74 72 61 28 44 62 50 61 67 65 20 2a 29 3b 20  xtra(DbPage *); 
53de0 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75  ../* Functions u
53df0 73 65 64 20 74 6f 20 6d 61 6e 61 67 65 20 70 61  sed to manage pa
53e00 67 65 72 20 74 72 61 6e 73 61 63 74 69 6f 6e 73  ger transactions
53e10 20 61 6e 64 20 73 61 76 65 70 6f 69 6e 74 73 2e   and savepoints.
53e20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
53e30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
53e40 67 65 72 50 61 67 65 63 6f 75 6e 74 28 50 61 67  gerPagecount(Pag
53e50 65 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49  er*, int*);.SQLI
53e60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
53e70 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e  qlite3PagerBegin
53e80 28 50 61 67 65 72 2a 2c 20 69 6e 74 20 65 78 46  (Pager*, int exF
53e90 6c 61 67 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  lag, int);.SQLIT
53ea0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
53eb0 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
53ec0 50 68 61 73 65 4f 6e 65 28 50 61 67 65 72 2a 2c  PhaseOne(Pager*,
53ed0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73  const char *zMas
53ee0 74 65 72 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  ter, int);.SQLIT
53ef0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
53f00 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28 50  lite3PagerSync(P
53f10 61 67 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 53  ager *pPager);.S
53f20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
53f30 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  t sqlite3PagerCo
53f40 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50 61 67  mmitPhaseTwo(Pag
53f50 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
53f60 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
53f70 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 50 61  PagerRollback(Pa
53f80 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ger*);.SQLITE_PR
53f90 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
53fa0 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f  3PagerOpenSavepo
53fb0 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65  int(Pager *pPage
53fc0 72 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 4c 49 54  r, int n);.SQLIT
53fd0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
53fe0 6c 69 74 65 33 50 61 67 65 72 53 61 76 65 70 6f  lite3PagerSavepo
53ff0 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65  int(Pager *pPage
54000 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 69  r, int op, int i
54010 53 61 76 65 70 6f 69 6e 74 29 3b 0a 53 51 4c 49  Savepoint);.SQLI
54020 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
54030 71 6c 69 74 65 33 50 61 67 65 72 53 68 61 72 65  qlite3PagerShare
54040 64 4c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61  dLock(Pager *pPa
54050 67 65 72 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69  ger);../* Functi
54060 6f 6e 73 20 75 73 65 64 20 74 6f 20 71 75 65 72  ons used to quer
54070 79 20 70 61 67 65 72 20 73 74 61 74 65 20 61 6e  y pager state an
54080 64 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2e  d configuration.
54090 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
540a0 54 45 20 75 38 20 73 71 6c 69 74 65 33 50 61 67  TE u8 sqlite3Pag
540b0 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50 61 67  erIsreadonly(Pag
540c0 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
540d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
540e0 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 50 61  PagerRefcount(Pa
540f0 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ger*);.SQLITE_PR
54100 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
54110 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69   *sqlite3PagerFi
54120 6c 65 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b 0a  lename(Pager*);.
54130 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
54140 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 66 73  onst sqlite3_vfs
54150 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 56 66   *sqlite3PagerVf
54160 73 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54  s(Pager*);.SQLIT
54170 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65  E_PRIVATE sqlite
54180 33 5f 66 69 6c 65 20 2a 73 71 6c 69 74 65 33 50  3_file *sqlite3P
54190 61 67 65 72 46 69 6c 65 28 50 61 67 65 72 2a 29  agerFile(Pager*)
541a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
541b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
541c0 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c  ite3PagerJournal
541d0 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b 0a 53 51  name(Pager*);.SQ
541e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
541f0 20 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73   sqlite3PagerNos
54200 79 6e 63 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c  ync(Pager*);.SQL
54210 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
54220 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 54 65   *sqlite3PagerTe
54230 6d 70 53 70 61 63 65 28 50 61 67 65 72 2a 29 3b  mpSpace(Pager*);
54240 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
54250 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
54260 49 73 4d 65 6d 64 62 28 50 61 67 65 72 2a 29 3b  IsMemdb(Pager*);
54270 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75  ../* Functions u
54280 73 65 64 20 74 6f 20 74 72 75 6e 63 61 74 65 20  sed to truncate 
54290 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
542a0 65 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e. */.SQLITE_PRI
542b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
542c0 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d  3PagerTruncateIm
542d0 61 67 65 28 50 61 67 65 72 2a 2c 50 67 6e 6f 29  age(Pager*,Pgno)
542e0 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20  ;../* Functions 
542f0 74 6f 20 73 75 70 70 6f 72 74 20 74 65 73 74 69  to support testi
54300 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67  ng and debugging
54310 2e 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  . */.#if !define
54320 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66  d(NDEBUG) || def
54330 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
54340 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ).SQLITE_PRIVATE
54350 20 20 20 50 67 6e 6f 20 73 71 6c 69 74 65 33 50     Pgno sqlite3P
54360 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 28 44  agerPagenumber(D
54370 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  bPage*);.SQLITE_
54380 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
54390 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
543a0 65 61 62 6c 65 28 44 62 50 61 67 65 2a 29 3b 0a  eable(DbPage*);.
543b0 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
543c0 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
543d0 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 2a  _PRIVATE   int *
543e0 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 61 74  sqlite3PagerStat
543f0 73 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54  s(Pager*);.SQLIT
54400 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
54410 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66   sqlite3PagerRef
54420 64 75 6d 70 28 50 61 67 65 72 2a 29 3b 0a 20 20  dump(Pager*);.  
54430 76 6f 69 64 20 64 69 73 61 62 6c 65 5f 73 69 6d  void disable_sim
54440 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73  ulated_io_errors
54450 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 65  (void);.  void e
54460 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  nable_simulated_
54470 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 3b  io_errors(void);
54480 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
54490 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65  disable_simulate
544a0 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 20  d_io_errors().# 
544b0 64 65 66 69 6e 65 20 65 6e 61 62 6c 65 5f 73 69  define enable_si
544c0 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72  mulated_io_error
544d0 73 28 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64  s().#endif..#end
544e0 69 66 20 2f 2a 20 5f 50 41 47 45 52 5f 48 5f 20  if /* _PAGER_H_ 
544f0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
54500 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67 65 72  *** End of pager
54510 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
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 2a 2a 2a 2a 2a 2a 2a  ****************
54540 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
54550 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
54560 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
54570 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   in sqliteInt.h 
54580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54590 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
545a0 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 63 61 63  *** Include pcac
545b0 68 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  he.h in the midd
545c0 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e  le of sqliteInt.
545d0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
545e0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
545f0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70  *** Begin file p
54600 63 61 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  cache.h ********
54610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54630 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41  **/./*.** 2008 A
54640 75 67 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20 54  ugust 05.**.** T
54650 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
54660 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
54670 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
54680 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
54690 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
546a0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
546b0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
546c0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
546d0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
546e0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
546f0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
54700 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
54710 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
54720 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
54730 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
54740 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
54750 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
54760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
547a0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
547b0 68 65 61 64 65 72 20 66 69 6c 65 20 64 65 66 69  header file defi
547c0 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 63  nes the interfac
547d0 65 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74  e that the sqlit
547e0 65 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20  e page cache.** 
547f0 73 75 62 73 79 73 74 65 6d 2e 20 0a 2a 2a 0a 2a  subsystem. .**.*
54800 2a 20 40 28 23 29 20 24 49 64 3a 20 70 63 61 63  * @(#) $Id: pcac
54810 68 65 2e 68 2c 76 20 31 2e 32 30 20 32 30 30 39  he.h,v 1.20 2009
54820 2f 30 37 2f 32 35 20 31 31 3a 34 36 3a 34 39 20  /07/25 11:46:49 
54830 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
54840 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50  $.*/..#ifndef _P
54850 43 41 43 48 45 5f 48 5f 0a 0a 74 79 70 65 64 65  CACHE_H_..typede
54860 66 20 73 74 72 75 63 74 20 50 67 48 64 72 20 50  f struct PgHdr P
54870 67 48 64 72 3b 0a 74 79 70 65 64 65 66 20 73 74  gHdr;.typedef st
54880 72 75 63 74 20 50 43 61 63 68 65 20 50 43 61 63  ruct PCache PCac
54890 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 76 65 72 79  he;../*.** Every
548a0 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 61 63   page in the cac
548b0 68 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64  he is controlled
548c0 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   by an instance 
548d0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
548e0 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  .** structure..*
548f0 2f 0a 73 74 72 75 63 74 20 50 67 48 64 72 20 7b  /.struct PgHdr {
54900 0a 20 20 76 6f 69 64 20 2a 70 44 61 74 61 3b 20  .  void *pData; 
54910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54920 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20    /* Content of 
54930 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 76  this page */.  v
54940 6f 69 64 20 2a 70 45 78 74 72 61 3b 20 20 20 20  oid *pExtra;    
54950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
54960 20 45 78 74 72 61 20 63 6f 6e 74 65 6e 74 20 2a   Extra content *
54970 2f 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74  /.  PgHdr *pDirt
54980 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
54990 20 20 20 2f 2a 20 54 72 61 6e 73 69 65 6e 74 20     /* Transient 
549a0 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 61  list of dirty pa
549b0 67 65 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67  ges */.  Pgno pg
549c0 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  no;             
549d0 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
549e0 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 69 73 20  number for this 
549f0 70 61 67 65 20 2a 2f 0a 20 20 50 61 67 65 72 20  page */.  Pager 
54a00 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 20 20  *pPager;        
54a10 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
54a20 70 61 67 65 72 20 74 68 69 73 20 70 61 67 65 20  pager this page 
54a30 69 73 20 70 61 72 74 20 6f 66 20 2a 2f 0a 23 69  is part of */.#i
54a40 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43  fdef SQLITE_CHEC
54a50 4b 5f 50 41 47 45 53 0a 20 20 75 33 32 20 70 61  K_PAGES.  u32 pa
54a60 67 65 48 61 73 68 3b 20 20 20 20 20 20 20 20 20  geHash;         
54a70 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68           /* Hash
54a80 20 6f 66 20 70 61 67 65 20 63 6f 6e 74 65 6e 74   of page content
54a90 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75 31 36   */.#endif.  u16
54aa0 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
54ab0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
54ac0 47 48 44 52 20 66 6c 61 67 73 20 64 65 66 69 6e  GHDR flags defin
54ad0 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 0a 20 20 2f  ed below */..  /
54ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54b20 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 45 6c 65 6d  ******.  ** Elem
54b30 65 6e 74 73 20 61 62 6f 76 65 20 61 72 65 20 70  ents above are p
54b40 75 62 6c 69 63 2e 20 20 41 6c 6c 20 74 68 61 74  ublic.  All that
54b50 20 66 6f 6c 6c 6f 77 73 20 69 73 20 70 72 69 76   follows is priv
54b60 61 74 65 20 74 6f 20 70 63 61 63 68 65 2e 63 0a  ate to pcache.c.
54b70 20 20 2a 2a 20 61 6e 64 20 73 68 6f 75 6c 64 20    ** and should 
54b80 6e 6f 74 20 62 65 20 61 63 63 65 73 73 65 64 20  not be accessed 
54b90 62 79 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73  by other modules
54ba0 2e 0a 20 20 2a 2f 0a 20 20 69 31 36 20 6e 52 65  ..  */.  i16 nRe
54bb0 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
54bc0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
54bd0 72 20 6f 66 20 75 73 65 72 73 20 6f 66 20 74 68  r of users of th
54be0 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 43 61  is page */.  PCa
54bf0 63 68 65 20 2a 70 43 61 63 68 65 3b 20 20 20 20  che *pCache;    
54c00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
54c10 61 63 68 65 20 74 68 61 74 20 6f 77 6e 73 20 74  ache that owns t
54c20 68 69 73 20 70 61 67 65 20 2a 2f 0a 0a 20 20 50  his page */..  P
54c30 67 48 64 72 20 2a 70 44 69 72 74 79 4e 65 78 74  gHdr *pDirtyNext
54c40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
54c50 20 4e 65 78 74 20 65 6c 65 6d 65 6e 74 20 69 6e   Next element in
54c60 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70   list of dirty p
54c70 61 67 65 73 20 2a 2f 0a 20 20 50 67 48 64 72 20  ages */.  PgHdr 
54c80 2a 70 44 69 72 74 79 50 72 65 76 3b 20 20 20 20  *pDirtyPrev;    
54c90 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76           /* Prev
54ca0 69 6f 75 73 20 65 6c 65 6d 65 6e 74 20 69 6e 20  ious element in 
54cb0 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 61  list of dirty pa
54cc0 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 42 69  ges */.};../* Bi
54cd0 74 20 76 61 6c 75 65 73 20 66 6f 72 20 50 67 48  t values for PgH
54ce0 64 72 2e 66 6c 61 67 73 20 2a 2f 0a 23 64 65 66  dr.flags */.#def
54cf0 69 6e 65 20 50 47 48 44 52 5f 44 49 52 54 59 20  ine PGHDR_DIRTY 
54d00 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30              0x00
54d10 32 20 20 2f 2a 20 50 61 67 65 20 68 61 73 20 63  2  /* Page has c
54d20 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66 69 6e  hanged */.#defin
54d30 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  e PGHDR_NEED_SYN
54d40 43 20 20 20 20 20 20 20 20 20 30 78 30 30 34 20  C         0x004 
54d50 20 2f 2a 20 46 73 79 6e 63 20 74 68 65 20 72 6f   /* Fsync the ro
54d60 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 62  llback journal b
54d70 65 66 6f 72 65 0a 20 20 20 20 20 20 20 20 20 20  efore.          
54d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54d90 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
54da0 77 72 69 74 69 6e 67 20 74 68 69 73 20 70 61 67  writing this pag
54db0 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  e to the databas
54dc0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48  e */.#define PGH
54dd0 44 52 5f 4e 45 45 44 5f 52 45 41 44 20 20 20 20  DR_NEED_READ    
54de0 20 20 20 20 20 30 78 30 30 38 20 20 2f 2a 20 43       0x008  /* C
54df0 6f 6e 74 65 6e 74 20 69 73 20 75 6e 72 65 61 64  ontent is unread
54e00 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44   */.#define PGHD
54e10 52 5f 52 45 55 53 45 5f 55 4e 4c 49 4b 45 4c 59  R_REUSE_UNLIKELY
54e20 20 20 20 20 30 78 30 31 30 20 20 2f 2a 20 41 20      0x010  /* A 
54e30 68 69 6e 74 20 74 68 61 74 20 72 65 75 73 65 20  hint that reuse 
54e40 69 73 20 75 6e 6c 69 6b 65 6c 79 20 2a 2f 0a 23  is unlikely */.#
54e50 64 65 66 69 6e 65 20 50 47 48 44 52 5f 44 4f 4e  define PGHDR_DON
54e60 54 5f 57 52 49 54 45 20 20 20 20 20 20 20 20 30  T_WRITE        0
54e70 78 30 32 30 20 20 2f 2a 20 44 6f 20 6e 6f 74 20  x020  /* Do not 
54e80 77 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 74 6f  write content to
54e90 20 64 69 73 6b 20 2a 2f 0a 0a 2f 2a 20 49 6e 69   disk */../* Ini
54ea0 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 68 75 74  tialize and shut
54eb0 64 6f 77 6e 20 74 68 65 20 70 61 67 65 20 63 61  down the page ca
54ec0 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 2a 2f  che subsystem */
54ed0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
54ee0 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68  int sqlite3Pcach
54ef0 65 49 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64  eInitialize(void
54f00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
54f10 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
54f20 61 63 68 65 53 68 75 74 64 6f 77 6e 28 76 6f 69  acheShutdown(voi
54f30 64 29 3b 0a 0a 2f 2a 20 50 61 67 65 20 63 61 63  d);../* Page cac
54f40 68 65 20 62 75 66 66 65 72 20 6d 61 6e 61 67 65  he buffer manage
54f50 6d 65 6e 74 3a 0a 2a 2a 20 54 68 65 73 65 20 72  ment:.** These r
54f60 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e  outines implemen
54f70 74 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  t SQLITE_CONFIG_
54f80 50 41 47 45 43 41 43 48 45 2e 0a 2a 2f 0a 53 51  PAGECACHE..*/.SQ
54f90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
54fa0 64 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 42  d sqlite3PCacheB
54fb0 75 66 66 65 72 53 65 74 75 70 28 76 6f 69 64 20  ufferSetup(void 
54fc0 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e  *, int sz, int n
54fd0 29 3b 0a 0a 2f 2a 20 43 72 65 61 74 65 20 61 20  );../* Create a 
54fe0 6e 65 77 20 70 61 67 65 72 20 63 61 63 68 65 2e  new pager cache.
54ff0 0a 2a 2a 20 55 6e 64 65 72 20 6d 65 6d 6f 72 79  .** Under memory
55000 20 73 74 72 65 73 73 2c 20 69 6e 76 6f 6b 65 20   stress, invoke 
55010 78 53 74 72 65 73 73 20 74 6f 20 74 72 79 20 74  xStress to try t
55020 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63 6c 65  o make pages cle
55030 61 6e 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 6c 65 61  an..** Only clea
55040 6e 20 61 6e 64 20 75 6e 70 69 6e 6e 65 64 20 70  n and unpinned p
55050 61 67 65 73 20 63 61 6e 20 62 65 20 72 65 63 6c  ages can be recl
55060 61 69 6d 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  aimed..*/.SQLITE
55070 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
55080 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28  lite3PcacheOpen(
55090 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 2c 20 20  .  int szPage,  
550a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
550b0 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65    /* Size of eve
550c0 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74  ry page */.  int
550d0 20 73 7a 45 78 74 72 61 2c 20 20 20 20 20 20 20   szExtra,       
550e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
550f0 78 74 72 61 20 73 70 61 63 65 20 61 73 73 6f 63  xtra space assoc
55100 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68 20  iated with each 
55110 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50  page */.  int bP
55120 75 72 67 65 61 62 6c 65 2c 20 20 20 20 20 20 20  urgeable,       
55130 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
55140 20 69 66 20 70 61 67 65 73 20 61 72 65 20 6f 6e   if pages are on
55150 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a   backing store *
55160 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 65 73  /.  int (*xStres
55170 73 29 28 76 6f 69 64 2a 2c 20 50 67 48 64 72 2a  s)(void*, PgHdr*
55180 29 2c 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72  ), /* Call to tr
55190 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 20  y to make pages 
551a0 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20  clean */.  void 
551b0 2a 70 53 74 72 65 73 73 2c 20 20 20 20 20 20 20  *pStress,       
551c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67            /* Arg
551d0 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73 73  ument to xStress
551e0 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 54   */.  PCache *pT
551f0 6f 49 6e 69 74 20 20 20 20 20 20 20 20 20 20 20  oInit           
55200 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63       /* Prealloc
55210 61 74 65 64 20 73 70 61 63 65 20 66 6f 72 20 74  ated space for t
55220 68 65 20 50 43 61 63 68 65 20 2a 2f 0a 29 3b 0a  he PCache */.);.
55230 0a 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65 20 70  ./* Modify the p
55240 61 67 65 2d 73 69 7a 65 20 61 66 74 65 72 20 74  age-size after t
55250 68 65 20 63 61 63 68 65 20 68 61 73 20 62 65 65  he cache has bee
55260 6e 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a 53 51  n created. */.SQ
55270 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
55280 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53  d sqlite3PcacheS
55290 65 74 50 61 67 65 53 69 7a 65 28 50 43 61 63 68  etPageSize(PCach
552a0 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 20 52  e *, int);../* R
552b0 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 69  eturn the size i
552c0 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50 43 61  n bytes of a PCa
552d0 63 68 65 20 6f 62 6a 65 63 74 2e 20 20 55 73 65  che object.  Use
552e0 64 20 74 6f 20 70 72 65 61 6c 6c 6f 63 61 74 65  d to preallocate
552f0 0a 2a 2a 20 73 74 6f 72 61 67 65 20 73 70 61 63  .** storage spac
55300 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
55310 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
55320 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69 64 29  PcacheSize(void)
55330 3b 0a 0a 2f 2a 20 4f 6e 65 20 72 65 6c 65 61 73  ;../* One releas
55340 65 20 70 65 72 20 73 75 63 63 65 73 73 66 75 6c  e per successful
55350 20 66 65 74 63 68 2e 20 20 50 61 67 65 20 69 73   fetch.  Page is
55360 20 70 69 6e 6e 65 64 20 75 6e 74 69 6c 20 72 65   pinned until re
55370 6c 65 61 73 65 64 2e 0a 2a 2a 20 52 65 66 65 72  leased..** Refer
55380 65 6e 63 65 20 63 6f 75 6e 74 65 64 2e 20 0a 2a  ence counted. .*
55390 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
553a0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63   int sqlite3Pcac
553b0 68 65 46 65 74 63 68 28 50 43 61 63 68 65 2a 2c  heFetch(PCache*,
553c0 20 50 67 6e 6f 2c 20 69 6e 74 20 63 72 65 61 74   Pgno, int creat
553d0 65 46 6c 61 67 2c 20 50 67 48 64 72 2a 2a 29 3b  eFlag, PgHdr**);
553e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
553f0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
55400 68 65 52 65 6c 65 61 73 65 28 50 67 48 64 72 2a  heRelease(PgHdr*
55410 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
55420 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
55430 63 61 63 68 65 44 72 6f 70 28 50 67 48 64 72 2a  cacheDrop(PgHdr*
55440 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  );         /* Re
55450 6d 6f 76 65 20 70 61 67 65 20 66 72 6f 6d 20 63  move page from c
55460 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  ache */.SQLITE_P
55470 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
55480 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69 72  te3PcacheMakeDir
55490 74 79 28 50 67 48 64 72 2a 29 3b 20 20 20 20 2f  ty(PgHdr*);    /
554a0 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 61 67 65  * Make sure page
554b0 20 69 73 20 6d 61 72 6b 65 64 20 64 69 72 74 79   is marked dirty
554c0 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
554d0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
554e0 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 50  cacheMakeClean(P
554f0 67 48 64 72 2a 29 3b 20 20 20 20 2f 2a 20 4d 61  gHdr*);    /* Ma
55500 72 6b 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65  rk a single page
55510 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 53 51 4c   as clean */.SQL
55520 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
55530 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c   sqlite3PcacheCl
55540 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 2a 29 3b  eanAll(PCache*);
55550 20 20 20 20 2f 2a 20 4d 61 72 6b 20 61 6c 6c 20      /* Mark all 
55560 64 69 72 74 79 20 6c 69 73 74 20 70 61 67 65 73  dirty list pages
55570 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 0a 2f 2a   as clean */../*
55580 20 43 68 61 6e 67 65 20 61 20 70 61 67 65 20 6e   Change a page n
55590 75 6d 62 65 72 2e 20 20 55 73 65 64 20 62 79 20  umber.  Used by 
555a0 69 6e 63 72 2d 76 61 63 75 75 6d 2e 20 2a 2f 0a  incr-vacuum. */.
555b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
555c0 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
555d0 65 4d 6f 76 65 28 50 67 48 64 72 2a 2c 20 50 67  eMove(PgHdr*, Pg
555e0 6e 6f 29 3b 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20  no);../* Remove 
555f0 61 6c 6c 20 70 61 67 65 73 20 77 69 74 68 20 70  all pages with p
55600 67 6e 6f 3e 78 2e 20 20 52 65 73 65 74 20 74 68  gno>x.  Reset th
55610 65 20 63 61 63 68 65 20 69 66 20 78 3d 3d 30 20  e cache if x==0 
55620 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
55630 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
55640 61 63 68 65 54 72 75 6e 63 61 74 65 28 50 43 61  acheTruncate(PCa
55650 63 68 65 2a 2c 20 50 67 6e 6f 20 78 29 3b 0a 0a  che*, Pgno x);..
55660 2f 2a 20 47 65 74 20 61 20 6c 69 73 74 20 6f 66  /* Get a list of
55670 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73   all dirty pages
55680 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 73   in the cache, s
55690 6f 72 74 65 64 20 62 79 20 70 61 67 65 20 6e 75  orted by page nu
556a0 6d 62 65 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  mber */.SQLITE_P
556b0 52 49 56 41 54 45 20 50 67 48 64 72 20 2a 73 71  RIVATE PgHdr *sq
556c0 6c 69 74 65 33 50 63 61 63 68 65 44 69 72 74 79  lite3PcacheDirty
556d0 4c 69 73 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a  List(PCache*);..
556e0 2f 2a 20 52 65 73 65 74 20 61 6e 64 20 63 6c 6f  /* Reset and clo
556f0 73 65 20 74 68 65 20 63 61 63 68 65 20 6f 62 6a  se the cache obj
55700 65 63 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ect */.SQLITE_PR
55710 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
55720 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 50 43  e3PcacheClose(PC
55730 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 43 6c 65 61  ache*);../* Clea
55740 72 20 66 6c 61 67 73 20 66 72 6f 6d 20 70 61 67  r flags from pag
55750 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 63  es of the page c
55760 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  ache */.SQLITE_P
55770 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
55780 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 53 79  te3PcacheClearSy
55790 6e 63 46 6c 61 67 73 28 50 43 61 63 68 65 20 2a  ncFlags(PCache *
557a0 29 3b 0a 0a 2f 2a 20 44 69 73 63 61 72 64 20 74  );../* Discard t
557b0 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
557c0 68 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49  he cache */.SQLI
557d0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
557e0 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65  sqlite3PcacheCle
557f0 61 72 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a  ar(PCache*);../*
55800 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61   Return the tota
55810 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74 73  l number of outs
55820 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72 65 66  tanding page ref
55830 65 72 65 6e 63 65 73 20 2a 2f 0a 53 51 4c 49 54  erences */.SQLIT
55840 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
55850 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f  lite3PcacheRefCo
55860 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f  unt(PCache*);../
55870 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20  * Increment the 
55880 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20  reference count 
55890 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 70  of an existing p
558a0 61 67 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  age */.SQLITE_PR
558b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
558c0 65 33 50 63 61 63 68 65 52 65 66 28 50 67 48 64  e3PcacheRef(PgHd
558d0 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  r*);..SQLITE_PRI
558e0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
558f0 50 63 61 63 68 65 50 61 67 65 52 65 66 63 6f 75  PcachePageRefcou
55900 6e 74 28 50 67 48 64 72 2a 29 3b 0a 0a 2f 2a 20  nt(PgHdr*);../* 
55910 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c  Return the total
55920 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
55930 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63   stored in the c
55940 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  ache */.SQLITE_P
55950 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
55960 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 6e  e3PcachePagecoun
55970 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a 23 69 66  t(PCache*);..#if
55980 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
55990 43 48 45 43 4b 5f 50 41 47 45 53 29 20 7c 7c 20  CHECK_PAGES) || 
559a0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
559b0 45 42 55 47 29 0a 2f 2a 20 49 74 65 72 61 74 65  EBUG)./* Iterate
559c0 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 64 69 72   through all dir
559d0 74 79 20 70 61 67 65 73 20 63 75 72 72 65 6e 74  ty pages current
559e0 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  ly stored in the
559f0 20 63 61 63 68 65 2e 20 54 68 69 73 0a 2a 2a 20   cache. This.** 
55a00 69 6e 74 65 72 66 61 63 65 20 69 73 20 6f 6e 6c  interface is onl
55a10 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 53  y available if S
55a20 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45  QLITE_CHECK_PAGE
55a30 53 20 69 73 20 64 65 66 69 6e 65 64 20 77 68 65  S is defined whe
55a40 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 62 72 61 72  n the .** librar
55a50 79 20 69 73 20 62 75 69 6c 74 2e 0a 2a 2f 0a 53  y is built..*/.S
55a60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
55a70 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
55a80 49 74 65 72 61 74 65 44 69 72 74 79 28 50 43 61  IterateDirty(PCa
55a90 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 6f 69  che *pCache, voi
55aa0 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 64 72  d (*xIter)(PgHdr
55ab0 20 2a 29 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a   *));.#endif../*
55ac0 20 53 65 74 20 61 6e 64 20 67 65 74 20 74 68 65   Set and get the
55ad0 20 73 75 67 67 65 73 74 65 64 20 63 61 63 68 65   suggested cache
55ae0 2d 73 69 7a 65 20 66 6f 72 20 74 68 65 20 73 70  -size for the sp
55af0 65 63 69 66 69 65 64 20 70 61 67 65 72 2d 63 61  ecified pager-ca
55b00 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f  che..**.** If no
55b10 20 67 6c 6f 62 61 6c 20 6d 61 78 69 6d 75 6d 20   global maximum 
55b20 69 73 20 63 6f 6e 66 69 67 75 72 65 64 2c 20 74  is configured, t
55b30 68 65 6e 20 74 68 65 20 73 79 73 74 65 6d 20 61  hen the system a
55b40 74 74 65 6d 70 74 73 20 74 6f 20 6c 69 6d 69 74  ttempts to limit
55b50 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20 6e 75  .** the total nu
55b60 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 63 61  mber of pages ca
55b70 63 68 65 64 20 62 79 20 70 75 72 67 65 61 62 6c  ched by purgeabl
55b80 65 20 70 61 67 65 72 2d 63 61 63 68 65 73 20 74  e pager-caches t
55b90 6f 20 74 68 65 20 73 75 6d 0a 2a 2a 20 6f 66 20  o the sum.** of 
55ba0 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63 61  the suggested ca
55bb0 63 68 65 2d 73 69 7a 65 73 2e 0a 2a 2f 0a 53 51  che-sizes..*/.SQ
55bc0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
55bd0 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53  d sqlite3PcacheS
55be0 65 74 43 61 63 68 65 73 69 7a 65 28 50 43 61 63  etCachesize(PCac
55bf0 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 64  he *, int);.#ifd
55c00 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
55c10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
55c20 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 47  t sqlite3PcacheG
55c30 65 74 43 61 63 68 65 73 69 7a 65 28 50 43 61 63  etCachesize(PCac
55c40 68 65 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23  he *);.#endif..#
55c50 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
55c60 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47  BLE_MEMORY_MANAG
55c70 45 4d 45 4e 54 0a 2f 2a 20 54 72 79 20 74 6f 20  EMENT./* Try to 
55c80 72 65 74 75 72 6e 20 6d 65 6d 6f 72 79 20 75 73  return memory us
55c90 65 64 20 62 79 20 74 68 65 20 70 63 61 63 68 65  ed by the pcache
55ca0 20 6d 6f 64 75 6c 65 20 74 6f 20 74 68 65 20 6d   module to the m
55cb0 61 69 6e 20 6d 65 6d 6f 72 79 20 68 65 61 70 20  ain memory heap 
55cc0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
55cd0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61  E int sqlite3Pca
55ce0 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79  cheReleaseMemory
55cf0 28 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 0a 23  (int);.#endif..#
55d00 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
55d10 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
55d20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
55d30 63 68 65 53 74 61 74 73 28 69 6e 74 2a 2c 69 6e  cheStats(int*,in
55d40 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 23  t*,int*,int*);.#
55d50 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
55d60 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
55d70 65 33 50 43 61 63 68 65 53 65 74 44 65 66 61 75  e3PCacheSetDefau
55d80 6c 74 28 76 6f 69 64 29 3b 0a 0a 23 65 6e 64 69  lt(void);..#endi
55d90 66 20 2f 2a 20 5f 50 43 41 43 48 45 5f 48 5f 20  f /* _PCACHE_H_ 
55da0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
55db0 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61 63 68  *** End of pcach
55dc0 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
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 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
55e00 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
55e10 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
55e20 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   in sqliteInt.h 
55e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55e40 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/../**********
55e50 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 2e  **** Include os.
55e60 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
55e70 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  of sqliteInt.h *
55e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55e90 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
55ea0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
55eb0 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  os.h ***********
55ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55ee0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
55ef0 53 65 70 74 65 6d 62 65 72 20 31 36 0a 2a 2a 0a  September 16.**.
55f00 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
55f10 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
55f20 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
55f30 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
55f40 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
55f50 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
55f60 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
55f70 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
55f80 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
55f90 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
55fa0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
55fb0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
55fc0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
55fd0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
55fe0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
55ff0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
56000 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
56010 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
56020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56060 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64  .**.** This head
56070 65 72 20 66 69 6c 65 20 28 74 6f 67 65 74 68 65  er file (togethe
56080 72 20 77 69 74 68 20 69 73 20 63 6f 6d 70 61 6e  r with is compan
56090 69 6f 6e 20 43 20 73 6f 75 72 63 65 2d 63 6f 64  ion C source-cod
560a0 65 20 66 69 6c 65 0a 2a 2a 20 22 6f 73 2e 63 22  e file.** "os.c"
560b0 29 20 61 74 74 65 6d 70 74 20 74 6f 20 61 62 73  ) attempt to abs
560c0 74 72 61 63 74 20 74 68 65 20 75 6e 64 65 72 6c  tract the underl
560d0 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73  ying operating s
560e0 79 73 74 65 6d 20 73 6f 20 74 68 61 74 0a 2a 2a  ystem so that.**
560f0 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
56100 61 72 79 20 77 69 6c 6c 20 77 6f 72 6b 20 6f 6e  ary will work on
56110 20 62 6f 74 68 20 50 4f 53 49 58 20 61 6e 64 20   both POSIX and 
56120 77 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 2e  windows systems.
56130 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64  .**.** This head
56140 65 72 20 66 69 6c 65 20 69 73 20 23 69 6e 63 6c  er file is #incl
56150 75 64 65 2d 65 64 20 62 79 20 73 71 6c 69 74 65  ude-ed by sqlite
56160 49 6e 74 2e 68 20 61 6e 64 20 74 68 75 73 20 65  Int.h and thus e
56170 6e 64 73 20 75 70 0a 2a 2a 20 62 65 69 6e 67 20  nds up.** being 
56180 69 6e 63 6c 75 64 65 64 20 62 79 20 65 76 65 72  included by ever
56190 79 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a 2a  y source file..*
561a0 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 2e 68 2c 76  *.** $Id: os.h,v
561b0 20 31 2e 31 30 38 20 32 30 30 39 2f 30 32 2f 30   1.108 2009/02/0
561c0 35 20 31 36 3a 33 31 3a 34 36 20 64 72 68 20 45  5 16:31:46 drh E
561d0 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  xp $.*/.#ifndef 
561e0 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 0a 23 64  _SQLITE_OS_H_.#d
561f0 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 4f 53  efine _SQLITE_OS
56200 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 46 69 67 75 72  _H_../*.** Figur
56210 65 20 6f 75 74 20 69 66 20 77 65 20 61 72 65 20  e out if we are 
56220 64 65 61 6c 69 6e 67 20 77 69 74 68 20 55 6e 69  dealing with Uni
56230 78 2c 20 57 69 6e 64 6f 77 73 2c 20 6f 72 20 73  x, Windows, or s
56240 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 6f 70 65  ome other.** ope
56250 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20  rating system.  
56260 41 66 74 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77  After the follow
56270 69 6e 67 20 62 6c 6f 63 6b 20 6f 66 20 70 72 65  ing block of pre
56280 70 72 6f 63 65 73 73 20 6d 61 63 72 6f 73 2c 0a  process macros,.
56290 2a 2a 20 61 6c 6c 20 6f 66 20 53 51 4c 49 54 45  ** all of SQLITE
562a0 5f 4f 53 5f 55 4e 49 58 2c 20 53 51 4c 49 54 45  _OS_UNIX, SQLITE
562b0 5f 4f 53 5f 57 49 4e 2c 20 53 51 4c 49 54 45 5f  _OS_WIN, SQLITE_
562c0 4f 53 5f 4f 53 32 2c 20 61 6e 64 20 53 51 4c 49  OS_OS2, and SQLI
562d0 54 45 5f 4f 53 5f 4f 54 48 45 52 20 0a 2a 2a 20  TE_OS_OTHER .** 
562e0 77 69 6c 6c 20 64 65 66 69 6e 65 64 20 74 6f 20  will defined to 
562f0 65 69 74 68 65 72 20 31 20 6f 72 20 30 2e 20 20  either 1 or 0.  
56300 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f 75 72 20  One of the four 
56310 77 69 6c 6c 20 62 65 20 31 2e 20 20 54 68 65 20  will be 1.  The 
56320 6f 74 68 65 72 20 0a 2a 2a 20 74 68 72 65 65 20  other .** three 
56330 77 69 6c 6c 20 62 65 20 30 2e 0a 2a 2f 0a 23 69  will be 0..*/.#i
56340 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
56350 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20 69 66 20  _OS_OTHER).# if 
56360 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 3d  SQLITE_OS_OTHER=
56370 3d 31 0a 23 20 20 20 75 6e 64 65 66 20 53 51 4c  =1.#   undef SQL
56380 49 54 45 5f 4f 53 5f 55 4e 49 58 0a 23 20 20 20  ITE_OS_UNIX.#   
56390 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
563a0 5f 55 4e 49 58 20 30 0a 23 20 20 20 75 6e 64 65  _UNIX 0.#   unde
563b0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a  f SQLITE_OS_WIN.
563c0 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54  #   define SQLIT
563d0 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 75  E_OS_WIN 0.#   u
563e0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f  ndef SQLITE_OS_O
563f0 53 32 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51  S2.#   define SQ
56400 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20  LITE_OS_OS2 0.# 
56410 65 6c 73 65 0a 23 20 20 20 75 6e 64 65 66 20 53  else.#   undef S
56420 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 0a 23  QLITE_OS_OTHER.#
56430 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69   endif.#endif.#i
56440 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
56450 45 5f 4f 53 5f 55 4e 49 58 29 20 26 26 20 21 64  E_OS_UNIX) && !d
56460 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53  efined(SQLITE_OS
56470 5f 4f 54 48 45 52 29 0a 23 20 64 65 66 69 6e 65  _OTHER).# define
56480 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52   SQLITE_OS_OTHER
56490 20 30 0a 23 20 69 66 6e 64 65 66 20 53 51 4c 49   0.# ifndef SQLI
564a0 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 69 66  TE_OS_WIN.#   if
564b0 20 64 65 66 69 6e 65 64 28 5f 57 49 4e 33 32 29   defined(_WIN32)
564c0 20 7c 7c 20 64 65 66 69 6e 65 64 28 57 49 4e 33   || defined(WIN3
564d0 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f  2) || defined(__
564e0 43 59 47 57 49 4e 5f 5f 29 20 7c 7c 20 64 65 66  CYGWIN__) || def
564f0 69 6e 65 64 28 5f 5f 4d 49 4e 47 57 33 32 5f 5f  ined(__MINGW32__
56500 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 42  ) || defined(__B
56510 4f 52 4c 41 4e 44 43 5f 5f 29 0a 23 20 20 20 20  ORLANDC__).#    
56520 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
56530 53 5f 57 49 4e 20 31 0a 23 20 20 20 20 20 64 65  S_WIN 1.#     de
56540 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55  fine SQLITE_OS_U
56550 4e 49 58 20 30 0a 23 20 20 20 20 20 64 65 66 69  NIX 0.#     defi
56560 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32  ne SQLITE_OS_OS2
56570 20 30 0a 23 20 20 20 65 6c 69 66 20 64 65 66 69   0.#   elif defi
56580 6e 65 64 28 5f 5f 45 4d 58 5f 5f 29 20 7c 7c 20  ned(__EMX__) || 
56590 64 65 66 69 6e 65 64 28 5f 4f 53 32 29 20 7c 7c  defined(_OS2) ||
565a0 20 64 65 66 69 6e 65 64 28 4f 53 32 29 20 7c 7c   defined(OS2) ||
565b0 20 64 65 66 69 6e 65 64 28 5f 4f 53 32 5f 29 20   defined(_OS2_) 
565c0 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 4f 53 32  || defined(__OS2
565d0 5f 5f 29 0a 23 20 20 20 20 20 64 65 66 69 6e 65  __).#     define
565e0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30   SQLITE_OS_WIN 0
565f0 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51  .#     define SQ
56600 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23  LITE_OS_UNIX 0.#
56610 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49       define SQLI
56620 54 45 5f 4f 53 5f 4f 53 32 20 31 0a 23 20 20 20  TE_OS_OS2 1.#   
56630 65 6c 73 65 0a 23 20 20 20 20 20 64 65 66 69 6e  else.#     defin
56640 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20  e SQLITE_OS_WIN 
56650 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53  0.#     define S
56660 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 31 0a  QLITE_OS_UNIX 1.
56670 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c  #     define SQL
56680 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 20  ITE_OS_OS2 0.#  
56690 65 6e 64 69 66 0a 23 20 65 6c 73 65 0a 23 20 20  endif.# else.#  
566a0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
566b0 5f 55 4e 49 58 20 30 0a 23 20 20 64 65 66 69 6e  _UNIX 0.#  defin
566c0 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20  e SQLITE_OS_OS2 
566d0 30 0a 23 20 65 6e 64 69 66 0a 23 65 6c 73 65 0a  0.# endif.#else.
566e0 23 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  # ifndef SQLITE_
566f0 4f 53 5f 57 49 4e 0a 23 20 20 64 65 66 69 6e 65  OS_WIN.#  define
56700 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30   SQLITE_OS_WIN 0
56710 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  .# endif.#endif.
56720 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65  ./*.** Determine
56730 20 69 66 20 77 65 20 61 72 65 20 64 65 61 6c 69   if we are deali
56740 6e 67 20 77 69 74 68 20 57 69 6e 64 6f 77 73 43  ng with WindowsC
56750 45 20 2d 20 77 68 69 63 68 20 68 61 73 20 61 20  E - which has a 
56760 6d 75 63 68 0a 2a 2a 20 72 65 64 75 63 65 64 20  much.** reduced 
56770 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69  API..*/.#if defi
56780 6e 65 64 28 5f 57 49 4e 33 32 5f 57 43 45 29 0a  ned(_WIN32_WCE).
56790 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
567a0 4f 53 5f 57 49 4e 43 45 20 31 0a 23 65 6c 73 65  OS_WINCE 1.#else
567b0 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
567c0 5f 4f 53 5f 57 49 4e 43 45 20 30 0a 23 65 6e 64  _OS_WINCE 0.#end
567d0 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e  if.../*.** Defin
567e0 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 73 69  e the maximum si
567f0 7a 65 20 6f 66 20 61 20 74 65 6d 70 6f 72 61 72  ze of a temporar
56800 79 20 66 69 6c 65 6e 61 6d 65 0a 2a 2f 0a 23 69  y filename.*/.#i
56810 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a  f SQLITE_OS_WIN.
56820 23 20 69 6e 63 6c 75 64 65 20 3c 77 69 6e 64 6f  # include <windo
56830 77 73 2e 68 3e 0a 23 20 64 65 66 69 6e 65 20 53  ws.h>.# define S
56840 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53  QLITE_TEMPNAME_S
56850 49 5a 45 20 28 4d 41 58 5f 50 41 54 48 2b 35 30  IZE (MAX_PATH+50
56860 29 0a 23 65 6c 69 66 20 53 51 4c 49 54 45 5f 4f  ).#elif SQLITE_O
56870 53 5f 4f 53 32 0a 23 20 69 66 20 28 5f 5f 47 4e  S_OS2.# if (__GN
56880 55 43 5f 5f 20 3e 20 33 20 7c 7c 20 5f 5f 47 4e  UC__ > 3 || __GN
56890 55 43 5f 5f 20 3d 3d 20 33 20 26 26 20 5f 5f 47  UC__ == 3 && __G
568a0 4e 55 43 5f 4d 49 4e 4f 52 5f 5f 20 3e 3d 20 33  NUC_MINOR__ >= 3
568b0 29 20 26 26 20 64 65 66 69 6e 65 64 28 4f 53 32  ) && defined(OS2
568c0 5f 48 49 47 48 5f 4d 45 4d 4f 52 59 29 0a 23 20  _HIGH_MEMORY).# 
568d0 20 69 6e 63 6c 75 64 65 20 3c 6f 73 32 73 61 66   include <os2saf
568e0 65 2e 68 3e 20 2f 2a 20 68 61 73 20 74 6f 20 62  e.h> /* has to b
568f0 65 20 69 6e 63 6c 75 64 65 64 20 62 65 66 6f 72  e included befor
56900 65 20 6f 73 32 2e 68 20 66 6f 72 20 6c 69 6e 6b  e os2.h for link
56910 69 6e 67 20 74 6f 20 77 6f 72 6b 20 2a 2f 0a 23  ing to work */.#
56920 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20   endif.# define 
56930 49 4e 43 4c 5f 44 4f 53 44 41 54 45 54 49 4d 45  INCL_DOSDATETIME
56940 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44  .# define INCL_D
56950 4f 53 46 49 4c 45 4d 47 52 0a 23 20 64 65 66 69  OSFILEMGR.# defi
56960 6e 65 20 49 4e 43 4c 5f 44 4f 53 45 52 52 4f 52  ne INCL_DOSERROR
56970 53 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f  S.# define INCL_
56980 44 4f 53 4d 49 53 43 0a 23 20 64 65 66 69 6e 65  DOSMISC.# define
56990 20 49 4e 43 4c 5f 44 4f 53 50 52 4f 43 45 53 53   INCL_DOSPROCESS
569a0 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44  .# define INCL_D
569b0 4f 53 4d 4f 44 55 4c 45 4d 47 52 0a 23 20 64 65  OSMODULEMGR.# de
569c0 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 53 45 4d  fine INCL_DOSSEM
569d0 41 50 48 4f 52 45 53 0a 23 20 69 6e 63 6c 75 64  APHORES.# includ
569e0 65 20 3c 6f 73 32 2e 68 3e 0a 23 20 69 6e 63 6c  e <os2.h>.# incl
569f0 75 64 65 20 3c 75 63 6f 6e 76 2e 68 3e 0a 23 20  ude <uconv.h>.# 
56a00 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
56a10 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 28 43 43 48  MPNAME_SIZE (CCH
56a20 4d 41 58 50 41 54 48 43 4f 4d 50 29 0a 23 65 6c  MAXPATHCOMP).#el
56a30 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  se.# define SQLI
56a40 54 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45  TE_TEMPNAME_SIZE
56a50 20 32 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20   200.#endif../* 
56a60 49 66 20 74 68 65 20 53 45 54 5f 46 55 4c 4c 53  If the SET_FULLS
56a70 59 4e 43 20 6d 61 63 72 6f 20 69 73 20 6e 6f 74  YNC macro is not
56a80 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65 2c 20   defined above, 
56a90 74 68 65 6e 20 6d 61 6b 65 20 69 74 0a 2a 2a 20  then make it.** 
56aa0 61 20 6e 6f 2d 6f 70 0a 2a 2f 0a 23 69 66 6e 64  a no-op.*/.#ifnd
56ab0 65 66 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 0a  ef SET_FULLSYNC.
56ac0 23 20 64 65 66 69 6e 65 20 53 45 54 5f 46 55 4c  # define SET_FUL
56ad0 4c 53 59 4e 43 28 78 2c 79 29 0a 23 65 6e 64 69  LSYNC(x,y).#endi
56ae0 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65 66  f../*.** The def
56af0 61 75 6c 74 20 73 69 7a 65 20 6f 66 20 61 20 64  ault size of a d
56b00 69 73 6b 20 73 65 63 74 6f 72 0a 2a 2f 0a 23 69  isk sector.*/.#i
56b10 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46  fndef SQLITE_DEF
56b20 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45  AULT_SECTOR_SIZE
56b30 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
56b40 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f  _DEFAULT_SECTOR_
56b50 53 49 5a 45 20 35 31 32 0a 23 65 6e 64 69 66 0a  SIZE 512.#endif.
56b60 0a 2f 2a 0a 2a 2a 20 54 65 6d 70 6f 72 61 72 79  ./*.** Temporary
56b70 20 66 69 6c 65 73 20 61 72 65 20 6e 61 6d 65 64   files are named
56b80 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 74   starting with t
56b90 68 69 73 20 70 72 65 66 69 78 20 66 6f 6c 6c 6f  his prefix follo
56ba0 77 65 64 20 62 79 20 31 36 20 72 61 6e 64 6f 6d  wed by 16 random
56bb0 0a 2a 2a 20 61 6c 70 68 61 6e 75 6d 65 72 69 63  .** alphanumeric
56bc0 20 63 68 61 72 61 63 74 65 72 73 2c 20 61 6e 64   characters, and
56bd0 20 6e 6f 20 66 69 6c 65 20 65 78 74 65 6e 73 69   no file extensi
56be0 6f 6e 2e 20 54 68 65 79 20 61 72 65 20 73 74 6f  on. They are sto
56bf0 72 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 4f 53  red in the.** OS
56c00 27 73 20 73 74 61 6e 64 61 72 64 20 74 65 6d 70  's standard temp
56c10 6f 72 61 72 79 20 66 69 6c 65 20 64 69 72 65 63  orary file direc
56c20 74 6f 72 79 2c 20 61 6e 64 20 61 72 65 20 64 65  tory, and are de
56c30 6c 65 74 65 64 20 70 72 69 6f 72 20 74 6f 20 65  leted prior to e
56c40 78 69 74 2e 0a 2a 2a 20 49 66 20 73 71 6c 69 74  xit..** If sqlit
56c50 65 20 69 73 20 62 65 69 6e 67 20 65 6d 62 65 64  e is being embed
56c60 64 65 64 20 69 6e 20 61 6e 6f 74 68 65 72 20 70  ded in another p
56c70 72 6f 67 72 61 6d 2c 20 79 6f 75 20 6d 61 79 20  rogram, you may 
56c80 77 69 73 68 20 74 6f 20 63 68 61 6e 67 65 20 74  wish to change t
56c90 68 65 0a 2a 2a 20 70 72 65 66 69 78 20 74 6f 20  he.** prefix to 
56ca0 72 65 66 6c 65 63 74 20 79 6f 75 72 20 70 72 6f  reflect your pro
56cb0 67 72 61 6d 27 73 20 6e 61 6d 65 2c 20 73 6f 20  gram's name, so 
56cc0 74 68 61 74 20 69 66 20 79 6f 75 72 20 70 72 6f  that if your pro
56cd0 67 72 61 6d 20 65 78 69 74 73 0a 2a 2a 20 70 72  gram exits.** pr
56ce0 65 6d 61 74 75 72 65 6c 79 2c 20 6f 6c 64 20 74  ematurely, old t
56cf0 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 63  emporary files c
56d00 61 6e 20 62 65 20 65 61 73 69 6c 79 20 69 64 65  an be easily ide
56d10 6e 74 69 66 69 65 64 2e 20 54 68 69 73 20 63 61  ntified. This ca
56d20 6e 20 62 65 20 64 6f 6e 65 0a 2a 2a 20 75 73 69  n be done.** usi
56d30 6e 67 20 2d 44 53 51 4c 49 54 45 5f 54 45 4d 50  ng -DSQLITE_TEMP
56d40 5f 46 49 4c 45 5f 50 52 45 46 49 58 3d 6d 79 70  _FILE_PREFIX=myp
56d50 72 65 66 69 78 5f 20 6f 6e 20 74 68 65 20 63 6f  refix_ on the co
56d60 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c  mpiler command l
56d70 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 36 2d  ine..**.** 2006-
56d80 31 30 2d 33 31 3a 20 20 54 68 65 20 64 65 66 61  10-31:  The defa
56d90 75 6c 74 20 70 72 65 66 69 78 20 75 73 65 64 20  ult prefix used 
56da0 74 6f 20 62 65 20 22 73 71 6c 69 74 65 5f 22 2e  to be "sqlite_".
56db0 20 20 42 75 74 20 74 68 65 6e 0a 2a 2a 20 4d 63    But then.** Mc
56dc0 61 66 65 65 20 73 74 61 72 74 65 64 20 75 73 69  afee started usi
56dd0 6e 67 20 53 51 4c 69 74 65 20 69 6e 20 74 68 65  ng SQLite in the
56de0 69 72 20 61 6e 74 69 2d 76 69 72 75 73 20 70 72  ir anti-virus pr
56df0 6f 64 75 63 74 20 61 6e 64 20 69 74 0a 2a 2a 20  oduct and it.** 
56e00 73 74 61 72 74 65 64 20 70 75 74 74 69 6e 67 20  started putting 
56e10 66 69 6c 65 73 20 77 69 74 68 20 74 68 65 20 22  files with the "
56e20 73 71 6c 69 74 65 22 20 6e 61 6d 65 20 69 6e 20  sqlite" name in 
56e30 74 68 65 20 63 3a 2f 74 65 6d 70 20 66 6f 6c 64  the c:/temp fold
56e40 65 72 2e 0a 2a 2a 20 54 68 69 73 20 61 6e 6e 6f  er..** This anno
56e50 79 65 64 20 6d 61 6e 79 20 77 69 6e 64 6f 77 73  yed many windows
56e60 20 75 73 65 72 73 2e 20 20 54 68 6f 73 65 20 75   users.  Those u
56e70 73 65 72 73 20 77 6f 75 6c 64 20 74 68 65 6e 20  sers would then 
56e80 64 6f 20 61 20 0a 2a 2a 20 47 6f 6f 67 6c 65 20  do a .** Google 
56e90 73 65 61 72 63 68 20 66 6f 72 20 22 73 71 6c 69  search for "sqli
56ea0 74 65 22 2c 20 66 69 6e 64 20 74 68 65 20 74 65  te", find the te
56eb0 6c 65 70 68 6f 6e 65 20 6e 75 6d 62 65 72 73 20  lephone numbers 
56ec0 6f 66 20 74 68 65 0a 2a 2a 20 64 65 76 65 6c 6f  of the.** develo
56ed0 70 65 72 73 20 61 6e 64 20 63 61 6c 6c 20 74 6f  pers and call to
56ee0 20 77 61 6b 65 20 74 68 65 6d 20 75 70 20 61 74   wake them up at
56ef0 20 6e 69 67 68 74 20 61 6e 64 20 63 6f 6d 70 6c   night and compl
56f00 61 69 6e 2e 0a 2a 2a 20 46 6f 72 20 74 68 69 73  ain..** For this
56f10 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 65 66   reason, the def
56f20 61 75 6c 74 20 6e 61 6d 65 20 70 72 65 66 69 78  ault name prefix
56f30 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20 62   is changed to b
56f40 65 20 22 73 71 6c 69 74 65 22 20 0a 2a 2a 20 73  e "sqlite" .** s
56f50 70 65 6c 6c 65 64 20 62 61 63 6b 77 61 72 64 73  pelled backwards
56f60 2e 20 20 53 6f 20 74 68 65 20 74 65 6d 70 20 66  .  So the temp f
56f70 69 6c 65 73 20 61 72 65 20 73 74 69 6c 6c 20 69  iles are still i
56f80 64 65 6e 74 69 66 69 65 64 2c 20 62 75 74 0a 2a  dentified, but.*
56f90 2a 20 61 6e 79 62 6f 64 79 20 73 6d 61 72 74 20  * anybody smart 
56fa0 65 6e 6f 75 67 68 20 74 6f 20 66 69 67 75 72 65  enough to figure
56fb0 20 6f 75 74 20 74 68 65 20 63 6f 64 65 20 69 73   out the code is
56fc0 20 61 6c 73 6f 20 6c 69 6b 65 6c 79 20 73 6d 61   also likely sma
56fd0 72 74 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20  rt.** enough to 
56fe0 6b 6e 6f 77 20 74 68 61 74 20 63 61 6c 6c 69 6e  know that callin
56ff0 67 20 74 68 65 20 64 65 76 65 6c 6f 70 65 72 20  g the developer 
57000 77 69 6c 6c 20 6e 6f 74 20 68 65 6c 70 20 67 65  will not help ge
57010 74 20 72 69 64 0a 2a 2a 20 6f 66 20 74 68 65 20  t rid.** of the 
57020 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  file..*/.#ifndef
57030 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c   SQLITE_TEMP_FIL
57040 45 5f 50 52 45 46 49 58 0a 23 20 64 65 66 69 6e  E_PREFIX.# defin
57050 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49  e SQLITE_TEMP_FI
57060 4c 45 5f 50 52 45 46 49 58 20 22 65 74 69 6c 71  LE_PREFIX "etilq
57070 73 5f 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  s_".#endif../*.*
57080 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
57090 76 61 6c 75 65 73 20 6d 61 79 20 62 65 20 70 61  values may be pa
570a0 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f  ssed as the seco
570b0 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a  nd argument to.*
570c0 2a 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28  * sqlite3OsLock(
570d0 29 2e 20 54 68 65 20 76 61 72 69 6f 75 73 20 6c  ). The various l
570e0 6f 63 6b 73 20 65 78 68 69 62 69 74 20 74 68 65  ocks exhibit the
570f0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 6d 61 6e   following seman
57100 74 69 63 73 3a 0a 2a 2a 0a 2a 2a 20 53 48 41 52  tics:.**.** SHAR
57110 45 44 3a 20 20 20 20 41 6e 79 20 6e 75 6d 62 65  ED:    Any numbe
57120 72 20 6f 66 20 70 72 6f 63 65 73 73 65 73 20 6d  r of processes m
57130 61 79 20 68 6f 6c 64 20 61 20 53 48 41 52 45 44  ay hold a SHARED
57140 20 6c 6f 63 6b 20 73 69 6d 75 6c 74 61 6e 65 6f   lock simultaneo
57150 75 73 6c 79 2e 0a 2a 2a 20 52 45 53 45 52 56 45  usly..** RESERVE
57160 44 3a 20 20 41 20 73 69 6e 67 6c 65 20 70 72 6f  D:  A single pro
57170 63 65 73 73 20 6d 61 79 20 68 6f 6c 64 20 61 20  cess may hold a 
57180 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e  RESERVED lock on
57190 20 61 20 66 69 6c 65 20 61 74 0a 2a 2a 20 20 20   a file at.**   
571a0 20 20 20 20 20 20 20 20 20 61 6e 79 20 74 69 6d           any tim
571b0 65 2e 20 4f 74 68 65 72 20 70 72 6f 63 65 73 73  e. Other process
571c0 65 73 20 6d 61 79 20 68 6f 6c 64 20 61 6e 64 20  es may hold and 
571d0 6f 62 74 61 69 6e 20 6e 65 77 20 53 48 41 52 45  obtain new SHARE
571e0 44 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 50 45 4e 44  D locks..** PEND
571f0 49 4e 47 3a 20 20 20 41 20 73 69 6e 67 6c 65 20  ING:   A single 
57200 70 72 6f 63 65 73 73 20 6d 61 79 20 68 6f 6c 64  process may hold
57210 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20   a PENDING lock 
57220 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a 2a 2a 20  on a file at.** 
57230 20 20 20 20 20 20 20 20 20 20 20 61 6e 79 20 6f             any o
57240 6e 65 20 74 69 6d 65 2e 20 45 78 69 73 74 69 6e  ne time. Existin
57250 67 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d  g SHARED locks m
57260 61 79 20 70 65 72 73 69 73 74 2c 20 62 75 74 20  ay persist, but 
57270 6e 6f 20 6e 65 77 0a 2a 2a 20 20 20 20 20 20 20  no new.**       
57280 20 20 20 20 20 53 48 41 52 45 44 20 6c 6f 63 6b       SHARED lock
57290 73 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e 65  s may be obtaine
572a0 64 20 62 79 20 6f 74 68 65 72 20 70 72 6f 63 65  d by other proce
572b0 73 73 65 73 2e 0a 2a 2a 20 45 58 43 4c 55 53 49  sses..** EXCLUSI
572c0 56 45 3a 20 41 6e 20 45 58 43 4c 55 53 49 56 45  VE: An EXCLUSIVE
572d0 20 6c 6f 63 6b 20 70 72 65 63 6c 75 64 65 73 20   lock precludes 
572e0 61 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 2e  all other locks.
572f0 0a 2a 2a 0a 2a 2a 20 50 45 4e 44 49 4e 47 5f 4c  .**.** PENDING_L
57300 4f 43 4b 20 6d 61 79 20 6e 6f 74 20 62 65 20 70  OCK may not be p
57310 61 73 73 65 64 20 64 69 72 65 63 74 6c 79 20 74  assed directly t
57320 6f 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28  o sqlite3OsLock(
57330 29 2e 20 49 6e 73 74 65 61 64 2c 20 61 0a 2a 2a  ). Instead, a.**
57340 20 70 72 6f 63 65 73 73 20 74 68 61 74 20 72 65   process that re
57350 71 75 65 73 74 73 20 61 6e 20 45 58 43 4c 55 53  quests an EXCLUS
57360 49 56 45 20 6c 6f 63 6b 20 6d 61 79 20 61 63 74  IVE lock may act
57370 75 61 6c 6c 79 20 6f 62 74 61 69 6e 20 61 20 50  ually obtain a P
57380 45 4e 44 49 4e 47 0a 2a 2a 20 6c 6f 63 6b 2e 20  ENDING.** lock. 
57390 54 68 69 73 20 63 61 6e 20 62 65 20 75 70 67 72  This can be upgr
573a0 61 64 65 64 20 74 6f 20 61 6e 20 45 58 43 4c 55  aded to an EXCLU
573b0 53 49 56 45 20 6c 6f 63 6b 20 62 79 20 61 20 73  SIVE lock by a s
573c0 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74  ubsequent call t
573d0 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4c 6f  o.** sqlite3OsLo
573e0 63 6b 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ck()..*/.#define
573f0 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20   NO_LOCK        
57400 20 30 0a 23 64 65 66 69 6e 65 20 53 48 41 52 45   0.#define SHARE
57410 44 5f 4c 4f 43 4b 20 20 20 20 20 31 0a 23 64 65  D_LOCK     1.#de
57420 66 69 6e 65 20 52 45 53 45 52 56 45 44 5f 4c 4f  fine RESERVED_LO
57430 43 4b 20 20 20 32 0a 23 64 65 66 69 6e 65 20 50  CK   2.#define P
57440 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 20 20 20 33  ENDING_LOCK    3
57450 0a 23 64 65 66 69 6e 65 20 45 58 43 4c 55 53 49  .#define EXCLUSI
57460 56 45 5f 4c 4f 43 4b 20 20 34 0a 0a 2f 2a 0a 2a  VE_LOCK  4../*.*
57470 2a 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 20 4e  * File Locking N
57480 6f 74 65 73 3a 20 20 28 4d 6f 73 74 6c 79 20 61  otes:  (Mostly a
57490 62 6f 75 74 20 77 69 6e 64 6f 77 73 20 62 75 74  bout windows but
574a0 20 61 6c 73 6f 20 73 6f 6d 65 20 69 6e 66 6f 20   also some info 
574b0 66 6f 72 20 55 6e 69 78 29 0a 2a 2a 0a 2a 2a 20  for Unix).**.** 
574c0 57 65 20 63 61 6e 6e 6f 74 20 75 73 65 20 4c 6f  We cannot use Lo
574d0 63 6b 46 69 6c 65 45 78 28 29 20 6f 72 20 55 6e  ckFileEx() or Un
574e0 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20 6f 6e 20  lockFileEx() on 
574f0 57 69 6e 39 35 2f 39 38 2f 4d 45 20 62 65 63 61  Win95/98/ME beca
57500 75 73 65 0a 2a 2a 20 74 68 6f 73 65 20 66 75 6e  use.** those fun
57510 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61  ctions are not a
57520 76 61 69 6c 61 62 6c 65 2e 20 20 53 6f 20 77 65  vailable.  So we
57530 20 75 73 65 20 6f 6e 6c 79 20 4c 6f 63 6b 46 69   use only LockFi
57540 6c 65 28 29 20 61 6e 64 0a 2a 2a 20 55 6e 6c 6f  le() and.** Unlo
57550 63 6b 46 69 6c 65 28 29 2e 0a 2a 2a 0a 2a 2a 20  ckFile()..**.** 
57560 4c 6f 63 6b 46 69 6c 65 28 29 20 70 72 65 76 65  LockFile() preve
57570 6e 74 73 20 6e 6f 74 20 6a 75 73 74 20 77 72 69  nts not just wri
57580 74 69 6e 67 20 62 75 74 20 61 6c 73 6f 20 72 65  ting but also re
57590 61 64 69 6e 67 20 62 79 20 6f 74 68 65 72 20 70  ading by other p
575a0 72 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 41 20 53  rocesses..** A S
575b0 48 41 52 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62  HARED_LOCK is ob
575c0 74 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e  tained by lockin
575d0 67 20 61 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f  g a single rando
575e0 6d 6c 79 2d 63 68 6f 73 65 6e 20 0a 2a 2a 20 62  mly-chosen .** b
575f0 79 74 65 20 6f 75 74 20 6f 66 20 61 20 73 70 65  yte out of a spe
57600 63 69 66 69 63 20 72 61 6e 67 65 20 6f 66 20 62  cific range of b
57610 79 74 65 73 2e 20 54 68 65 20 6c 6f 63 6b 20 62  ytes. The lock b
57620 79 74 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20  yte is obtained 
57630 61 74 20 0a 2a 2a 20 72 61 6e 64 6f 6d 20 73 6f  at .** random so
57640 20 74 77 6f 20 73 65 70 61 72 61 74 65 20 72 65   two separate re
57650 61 64 65 72 73 20 63 61 6e 20 70 72 6f 62 61 62  aders can probab
57660 6c 79 20 61 63 63 65 73 73 20 74 68 65 20 66 69  ly access the fi
57670 6c 65 20 61 74 20 74 68 65 20 0a 2a 2a 20 73 61  le at the .** sa
57680 6d 65 20 74 69 6d 65 2c 20 75 6e 6c 65 73 73 20  me time, unless 
57690 74 68 65 79 20 61 72 65 20 75 6e 6c 75 63 6b 79  they are unlucky
576a0 20 61 6e 64 20 63 68 6f 6f 73 65 20 74 68 65 20   and choose the 
576b0 73 61 6d 65 20 6c 6f 63 6b 20 62 79 74 65 2e 0a  same lock byte..
576c0 2a 2a 20 41 6e 20 45 58 43 4c 55 53 49 56 45 5f  ** An EXCLUSIVE_
576d0 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64  LOCK is obtained
576e0 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20   by locking all 
576f0 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 61 6e  bytes in the ran
57700 67 65 2e 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e  ge..** There can
57710 20 6f 6e 6c 79 20 62 65 20 6f 6e 65 20 77 72 69   only be one wri
57720 74 65 72 2e 20 20 41 20 52 45 53 45 52 56 45 44  ter.  A RESERVED
57730 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65  _LOCK is obtaine
57740 64 20 62 79 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20  d by locking.** 
57750 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 6f 66  a single byte of
57760 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 20 69   the file that i
57770 73 20 64 65 73 69 67 6e 61 74 65 64 20 61 73 20  s designated as 
57780 74 68 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63  the reserved loc
57790 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 20 50 45 4e  k byte..** A PEN
577a0 44 49 4e 47 5f 4c 4f 43 4b 20 69 73 20 6f 62 74  DING_LOCK is obt
577b0 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67  ained by locking
577c0 20 61 20 64 65 73 69 67 6e 61 74 65 64 20 62 79   a designated by
577d0 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  te different fro
577e0 6d 0a 2a 2a 20 74 68 65 20 52 45 53 45 52 56 45  m.** the RESERVE
577f0 44 5f 4c 4f 43 4b 20 62 79 74 65 2e 0a 2a 2a 0a  D_LOCK byte..**.
57800 2a 2a 20 4f 6e 20 57 69 6e 4e 54 2f 32 4b 2f 58  ** On WinNT/2K/X
57810 50 20 73 79 73 74 65 6d 73 2c 20 4c 6f 63 6b 46  P systems, LockF
57820 69 6c 65 45 78 28 29 20 61 6e 64 20 55 6e 6c 6f  ileEx() and Unlo
57830 63 6b 46 69 6c 65 45 78 28 29 20 61 72 65 20 61  ckFileEx() are a
57840 76 61 69 6c 61 62 6c 65 2c 0a 2a 2a 20 77 68 69  vailable,.** whi
57850 63 68 20 6d 65 61 6e 73 20 77 65 20 63 61 6e 20  ch means we can 
57860 75 73 65 20 72 65 61 64 65 72 2f 77 72 69 74 65  use reader/write
57870 72 20 6c 6f 63 6b 73 2e 20 20 57 68 65 6e 20 72  r locks.  When r
57880 65 61 64 65 72 2f 77 72 69 74 65 72 20 6c 6f 63  eader/writer loc
57890 6b 73 0a 2a 2a 20 61 72 65 20 75 73 65 64 2c 20  ks.** are used, 
578a0 74 68 65 20 6c 6f 63 6b 20 69 73 20 70 6c 61 63  the lock is plac
578b0 65 64 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 72  ed on the same r
578c0 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20 74 68  ange of bytes th
578d0 61 74 20 69 73 20 75 73 65 64 0a 2a 2a 20 66 6f  at is used.** fo
578e0 72 20 70 72 6f 62 61 62 69 6c 69 73 74 69 63 20  r probabilistic 
578f0 6c 6f 63 6b 69 6e 67 20 69 6e 20 57 69 6e 39 35  locking in Win95
57900 2f 39 38 2f 4d 45 2e 20 20 48 65 6e 63 65 2c 20  /98/ME.  Hence, 
57910 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65  the locking sche
57920 6d 65 0a 2a 2a 20 77 69 6c 6c 20 73 75 70 70 6f  me.** will suppo
57930 72 74 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 57  rt two or more W
57940 69 6e 39 35 20 72 65 61 64 65 72 73 20 6f 72 20  in95 readers or 
57950 74 77 6f 20 6f 72 20 6d 6f 72 65 20 57 69 6e 4e  two or more WinN
57960 54 20 72 65 61 64 65 72 73 2e 0a 2a 2a 20 42 75  T readers..** Bu
57970 74 20 61 20 73 69 6e 67 6c 65 20 57 69 6e 39 35  t a single Win95
57980 20 72 65 61 64 65 72 20 77 69 6c 6c 20 6c 6f 63   reader will loc
57990 6b 20 6f 75 74 20 61 6c 6c 20 57 69 6e 4e 54 20  k out all WinNT 
579a0 72 65 61 64 65 72 73 20 61 6e 64 20 61 20 73 69  readers and a si
579b0 6e 67 6c 65 0a 2a 2a 20 57 69 6e 4e 54 20 72 65  ngle.** WinNT re
579c0 61 64 65 72 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f  ader will lock o
579d0 75 74 20 61 6c 6c 20 6f 74 68 65 72 20 57 69 6e  ut all other Win
579e0 39 35 20 72 65 61 64 65 72 73 2e 0a 2a 2a 0a 2a  95 readers..**.*
579f0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
57a00 23 64 65 66 69 6e 65 73 20 73 70 65 63 69 66 79  #defines specify
57a10 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 62 79   the range of by
57a20 74 65 73 20 75 73 65 64 20 66 6f 72 20 6c 6f 63  tes used for loc
57a30 6b 69 6e 67 2e 0a 2a 2a 20 53 48 41 52 45 44 5f  king..** SHARED_
57a40 53 49 5a 45 20 69 73 20 74 68 65 20 6e 75 6d 62  SIZE is the numb
57a50 65 72 20 6f 66 20 62 79 74 65 73 20 61 76 61 69  er of bytes avai
57a60 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 70 6f 6f  lable in the poo
57a70 6c 20 66 72 6f 6d 20 77 68 69 63 68 0a 2a 2a 20  l from which.** 
57a80 61 20 72 61 6e 64 6f 6d 20 62 79 74 65 20 69 73  a random byte is
57a90 20 73 65 6c 65 63 74 65 64 20 66 6f 72 20 61 20   selected for a 
57aa0 73 68 61 72 65 64 20 6c 6f 63 6b 2e 20 20 54 68  shared lock.  Th
57ab0 65 20 70 6f 6f 6c 20 6f 66 20 62 79 74 65 73 20  e pool of bytes 
57ac0 66 6f 72 0a 2a 2a 20 73 68 61 72 65 64 20 6c 6f  for.** shared lo
57ad0 63 6b 73 20 62 65 67 69 6e 73 20 61 74 20 53 48  cks begins at SH
57ae0 41 52 45 44 5f 46 49 52 53 54 2e 20 0a 2a 2a 0a  ARED_FIRST. .**.
57af0 2a 2a 20 54 68 65 20 73 61 6d 65 20 6c 6f 63 6b  ** The same lock
57b00 69 6e 67 20 73 74 72 61 74 65 67 79 20 61 6e 64  ing strategy and
57b10 0a 2a 2a 20 62 79 74 65 20 72 61 6e 67 65 73 20  .** byte ranges 
57b20 61 72 65 20 75 73 65 64 20 66 6f 72 20 55 6e 69  are used for Uni
57b30 78 2e 20 20 54 68 69 73 20 6c 65 61 76 65 73 20  x.  This leaves 
57b40 6f 70 65 6e 20 74 68 65 20 70 6f 73 73 69 62 6c  open the possibl
57b50 69 74 79 20 6f 66 20 68 61 76 69 6e 67 0a 2a 2a  ity of having.**
57b60 20 63 6c 69 65 6e 74 73 20 6f 6e 20 77 69 6e 39   clients on win9
57b70 35 2c 20 77 69 6e 4e 54 2c 20 61 6e 64 20 75 6e  5, winNT, and un
57b80 69 78 20 61 6c 6c 20 74 61 6c 6b 69 6e 67 20 74  ix all talking t
57b90 6f 20 74 68 65 20 73 61 6d 65 20 73 68 61 72 65  o the same share
57ba0 64 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 61 6c  d file.** and al
57bb0 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 72 72 65 63  l locking correc
57bc0 74 6c 79 2e 20 20 54 6f 20 64 6f 20 73 6f 20 77  tly.  To do so w
57bd0 6f 75 6c 64 20 72 65 71 75 69 72 65 20 74 68 61  ould require tha
57be0 74 20 73 61 6d 62 61 20 28 6f 72 20 77 68 61 74  t samba (or what
57bf0 65 76 65 72 0a 2a 2a 20 74 6f 6f 6c 20 69 73 20  ever.** tool is 
57c00 62 65 69 6e 67 20 75 73 65 64 20 66 6f 72 20 66  being used for f
57c10 69 6c 65 20 73 68 61 72 69 6e 67 29 20 69 6d 70  ile sharing) imp
57c20 6c 65 6d 65 6e 74 73 20 6c 6f 63 6b 73 20 63 6f  lements locks co
57c30 72 72 65 63 74 6c 79 20 62 65 74 77 65 65 6e 0a  rrectly between.
57c40 2a 2a 20 77 69 6e 64 6f 77 73 20 61 6e 64 20 75  ** windows and u
57c50 6e 69 78 2e 20 20 49 27 6d 20 67 75 65 73 73 69  nix.  I'm guessi
57c60 6e 67 20 74 68 61 74 20 69 73 6e 27 74 20 6c 69  ng that isn't li
57c70 6b 65 6c 79 20 74 6f 20 68 61 70 70 65 6e 2c 20  kely to happen, 
57c80 62 75 74 20 62 79 0a 2a 2a 20 75 73 69 6e 67 20  but by.** using 
57c90 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67  the same locking
57ca0 20 72 61 6e 67 65 20 77 65 20 61 72 65 20 61 74   range we are at
57cb0 20 6c 65 61 73 74 20 6f 70 65 6e 20 74 6f 20 74   least open to t
57cc0 68 65 20 70 6f 73 73 69 62 69 6c 69 74 79 2e 0a  he possibility..
57cd0 2a 2a 0a 2a 2a 20 4c 6f 63 6b 69 6e 67 20 69 6e  **.** Locking in
57ce0 20 77 69 6e 64 6f 77 73 20 69 73 20 6d 61 6e 64   windows is mand
57cf0 69 74 6f 72 79 2e 20 20 46 6f 72 20 74 68 69 73  itory.  For this
57d00 20 72 65 61 73 6f 6e 2c 20 77 65 20 63 61 6e 6e   reason, we cann
57d10 6f 74 20 73 74 6f 72 65 0a 2a 2a 20 61 63 74 75  ot store.** actu
57d20 61 6c 20 64 61 74 61 20 69 6e 20 74 68 65 20 62  al data in the b
57d30 79 74 65 73 20 75 73 65 64 20 66 6f 72 20 6c 6f  ytes used for lo
57d40 63 6b 69 6e 67 2e 20 20 54 68 65 20 70 61 67 65  cking.  The page
57d50 72 20 6e 65 76 65 72 20 61 6c 6c 6f 63 61 74 65  r never allocate
57d60 73 0a 2a 2a 20 74 68 65 20 70 61 67 65 73 20 69  s.** the pages i
57d70 6e 76 6f 6c 76 65 64 20 69 6e 20 6c 6f 63 6b 69  nvolved in locki
57d80 6e 67 20 74 68 65 72 65 66 6f 72 65 2e 20 20 53  ng therefore.  S
57d90 48 41 52 45 44 5f 53 49 5a 45 20 69 73 20 73 65  HARED_SIZE is se
57da0 6c 65 63 74 65 64 20 73 6f 0a 2a 2a 20 74 68 61  lected so.** tha
57db0 74 20 61 6c 6c 20 6c 6f 63 6b 73 20 77 69 6c 6c  t all locks will
57dc0 20 66 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c 65   fit on a single
57dd0 20 70 61 67 65 20 65 76 65 6e 20 61 74 20 74 68   page even at th
57de0 65 20 6d 69 6e 69 6d 75 6d 20 70 61 67 65 20 73  e minimum page s
57df0 69 7a 65 2e 0a 2a 2a 20 50 45 4e 44 49 4e 47 5f  ize..** PENDING_
57e00 42 59 54 45 20 64 65 66 69 6e 65 73 20 74 68 65  BYTE defines the
57e10 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
57e20 65 20 6c 6f 63 6b 73 2e 20 20 42 79 20 64 65 66  e locks.  By def
57e30 61 75 6c 74 20 50 45 4e 44 49 4e 47 5f 42 59 54  ault PENDING_BYT
57e40 45 0a 2a 2a 20 69 73 20 73 65 74 20 68 69 67 68  E.** is set high
57e50 20 73 6f 20 74 68 61 74 20 77 65 20 64 6f 6e 27   so that we don'
57e60 74 20 68 61 76 65 20 74 6f 20 61 6c 6c 6f 63 61  t have to alloca
57e70 74 65 20 61 6e 20 75 6e 75 73 65 64 20 70 61 67  te an unused pag
57e80 65 20 65 78 63 65 70 74 0a 2a 2a 20 66 6f 72 20  e except.** for 
57e90 76 65 72 79 20 6c 61 72 67 65 20 64 61 74 61 62  very large datab
57ea0 61 73 65 73 2e 20 20 42 75 74 20 6f 6e 65 20 73  ases.  But one s
57eb0 68 6f 75 6c 64 20 74 65 73 74 20 74 68 65 20 70  hould test the p
57ec0 61 67 65 20 73 6b 69 70 70 69 6e 67 20 6c 6f 67  age skipping log
57ed0 69 63 20 0a 2a 2a 20 62 79 20 73 65 74 74 69 6e  ic .** by settin
57ee0 67 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 6c  g PENDING_BYTE l
57ef0 6f 77 20 61 6e 64 20 72 75 6e 6e 69 6e 67 20 74  ow and running t
57f00 68 65 20 65 6e 74 69 72 65 20 72 65 67 72 65 73  he entire regres
57f10 73 69 6f 6e 20 73 75 69 74 65 2e 0a 2a 2a 0a 2a  sion suite..**.*
57f20 2a 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20 76  * Changing the v
57f30 61 6c 75 65 20 6f 66 20 50 45 4e 44 49 4e 47 5f  alue of PENDING_
57f40 42 59 54 45 20 72 65 73 75 6c 74 73 20 69 6e 20  BYTE results in 
57f50 61 20 73 75 62 74 6c 79 20 69 6e 63 6f 6d 70 61  a subtly incompa
57f60 74 69 62 6c 65 0a 2a 2a 20 66 69 6c 65 20 66 6f  tible.** file fo
57f70 72 6d 61 74 2e 20 20 44 65 70 65 6e 64 69 6e 67  rmat.  Depending
57f80 20 6f 6e 20 68 6f 77 20 69 74 20 69 73 20 63 68   on how it is ch
57f90 61 6e 67 65 64 2c 20 79 6f 75 20 6d 69 67 68 74  anged, you might
57fa0 20 6e 6f 74 20 6e 6f 74 69 63 65 0a 2a 2a 20 74   not notice.** t
57fb0 68 65 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c 69  he incompatibili
57fc0 74 79 20 72 69 67 68 74 20 61 77 61 79 2c 20 65  ty right away, e
57fd0 76 65 6e 20 72 75 6e 6e 69 6e 67 20 61 20 66 75  ven running a fu
57fe0 6c 6c 20 72 65 67 72 65 73 73 69 6f 6e 20 74 65  ll regression te
57ff0 73 74 2e 0a 2a 2a 20 54 68 65 20 64 65 66 61 75  st..** The defau
58000 6c 74 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 50  lt location of P
58010 45 4e 44 49 4e 47 5f 42 59 54 45 20 69 73 20 74  ENDING_BYTE is t
58020 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61  he first byte pa
58030 73 74 20 74 68 65 0a 2a 2a 20 31 47 42 20 62 6f  st the.** 1GB bo
58040 75 6e 64 61 72 79 2e 0a 2a 2a 0a 2a 2f 0a 23 64  undary..**.*/.#d
58050 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f 42 59  efine PENDING_BY
58060 54 45 20 20 20 20 20 20 73 71 6c 69 74 65 33 50  TE      sqlite3P
58070 65 6e 64 69 6e 67 42 79 74 65 0a 23 64 65 66 69  endingByte.#defi
58080 6e 65 20 52 45 53 45 52 56 45 44 5f 42 59 54 45  ne RESERVED_BYTE
58090 20 20 20 20 20 28 50 45 4e 44 49 4e 47 5f 42 59       (PENDING_BY
580a0 54 45 2b 31 29 0a 23 64 65 66 69 6e 65 20 53 48  TE+1).#define SH
580b0 41 52 45 44 5f 46 49 52 53 54 20 20 20 20 20 20  ARED_FIRST      
580c0 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 32 29  (PENDING_BYTE+2)
580d0 0a 23 64 65 66 69 6e 65 20 53 48 41 52 45 44 5f  .#define SHARED_
580e0 53 49 5a 45 20 20 20 20 20 20 20 35 31 30 0a 0a  SIZE       510..
580f0 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 72 20 61 72  /*.** Wrapper ar
58100 6f 75 6e 64 20 4f 53 20 73 70 65 63 69 66 69 63  ound OS specific
58110 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74   sqlite3_os_init
58120 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  () function..*/.
58130 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
58140 6e 74 20 73 71 6c 69 74 65 33 4f 73 49 6e 69 74  nt sqlite3OsInit
58150 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20  (void);../* .** 
58160 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 63  Functions for ac
58170 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33 5f  cessing sqlite3_
58180 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 0a 2a 2f  file methods .*/
58190 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
581a0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f  int sqlite3OsClo
581b0 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  se(sqlite3_file*
581c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
581d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 52  E int sqlite3OsR
581e0 65 61 64 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ead(sqlite3_file
581f0 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61 6d  *, void*, int am
58200 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 3b 0a  t, i64 offset);.
58210 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
58220 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74  nt sqlite3OsWrit
58230 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  e(sqlite3_file*,
58240 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e   const void*, in
58250 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65  t amt, i64 offse
58260 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
58270 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
58280 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33  Truncate(sqlite3
58290 5f 66 69 6c 65 2a 2c 20 69 36 34 20 73 69 7a 65  _file*, i64 size
582a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
582b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53  E int sqlite3OsS
582c0 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ync(sqlite3_file
582d0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
582e0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
582f0 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 73 71  te3OsFileSize(sq
58300 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34  lite3_file*, i64
58310 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45   *pSize);.SQLITE
58320 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
58330 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71 6c 69 74  ite3OsLock(sqlit
58340 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a  e3_file*, int);.
58350 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
58360 6e 74 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f  nt sqlite3OsUnlo
58370 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  ck(sqlite3_file*
58380 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
58390 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
583a0 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65  e3OsCheckReserve
583b0 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  dLock(sqlite3_fi
583c0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65  le *id, int *pRe
583d0 73 4f 75 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  sOut);.SQLITE_PR
583e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
583f0 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73  3OsFileControl(s
58400 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69 6e 74  qlite3_file*,int
58410 2c 76 6f 69 64 2a 29 3b 0a 23 64 65 66 69 6e 65  ,void*);.#define
58420 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42   SQLITE_FCNTL_DB
58430 5f 55 4e 43 48 41 4e 47 45 44 20 30 78 63 61 30  _UNCHANGED 0xca0
58440 39 33 66 61 30 0a 53 51 4c 49 54 45 5f 50 52 49  93fa0.SQLITE_PRI
58450 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
58460 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c  OsSectorSize(sql
58470 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 3b 0a  ite3_file *id);.
58480 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
58490 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69  nt sqlite3OsDevi
584a0 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
584b0 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  s(sqlite3_file *
584c0 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 46 75 6e  id);../* .** Fun
584d0 63 74 69 6f 6e 73 20 66 6f 72 20 61 63 63 65 73  ctions for acces
584e0 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 76 66 73  sing sqlite3_vfs
584f0 20 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c   methods .*/.SQL
58500 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
58510 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 73 71  sqlite3OsOpen(sq
58520 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e  lite3_vfs *, con
58530 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 74  st char *, sqlit
58540 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2c 20 69  e3_file*, int, i
58550 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  nt *);.SQLITE_PR
58560 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
58570 33 4f 73 44 65 6c 65 74 65 28 73 71 6c 69 74 65  3OsDelete(sqlite
58580 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63  3_vfs *, const c
58590 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  har *, int);.SQL
585a0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
585b0 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28  sqlite3OsAccess(
585c0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63  sqlite3_vfs *, c
585d0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74  onst char *, int
585e0 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 3b  , int *pResOut);
585f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
58600 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 75 6c  int sqlite3OsFul
58610 6c 50 61 74 68 6e 61 6d 65 28 73 71 6c 69 74 65  lPathname(sqlite
58620 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63  3_vfs *, const c
58630 68 61 72 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72  har *, int, char
58640 20 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c   *);.#ifndef SQL
58650 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  ITE_OMIT_LOAD_EX
58660 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f 50  TENSION.SQLITE_P
58670 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
58680 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 6c  ite3OsDlOpen(sql
58690 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73  ite3_vfs *, cons
586a0 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54  t char *);.SQLIT
586b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
586c0 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28  qlite3OsDlError(
586d0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69  sqlite3_vfs *, i
586e0 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c  nt, char *);.SQL
586f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
58700 20 28 2a 73 71 6c 69 74 65 33 4f 73 44 6c 53 79   (*sqlite3OsDlSy
58710 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  m(sqlite3_vfs *,
58720 20 76 6f 69 64 20 2a 2c 20 63 6f 6e 73 74 20 63   void *, const c
58730 68 61 72 20 2a 29 29 28 76 6f 69 64 29 3b 0a 53  har *))(void);.S
58740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
58750 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c  id sqlite3OsDlCl
58760 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ose(sqlite3_vfs 
58770 2a 2c 20 76 6f 69 64 20 2a 29 3b 0a 23 65 6e 64  *, void *);.#end
58780 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
58790 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  T_LOAD_EXTENSION
587a0 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
587b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
587c0 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74  Randomness(sqlit
587d0 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 2c 20 63  e3_vfs *, int, c
587e0 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  har *);.SQLITE_P
587f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
58800 65 33 4f 73 53 6c 65 65 70 28 73 71 6c 69 74 65  e3OsSleep(sqlite
58810 33 5f 76 66 73 20 2a 2c 20 69 6e 74 29 3b 0a 53  3_vfs *, int);.S
58820 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
58830 74 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65  t sqlite3OsCurre
58840 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76  ntTime(sqlite3_v
58850 66 73 20 2a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a  fs *, double*);.
58860 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 6e 69 65 6e  ./*.** Convenien
58870 63 65 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72  ce functions for
58880 20 6f 70 65 6e 69 6e 67 20 61 6e 64 20 63 6c 6f   opening and clo
58890 73 69 6e 67 20 66 69 6c 65 73 20 75 73 69 6e 67  sing files using
588a0 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c   .** sqlite3_mal
588b0 6c 6f 63 28 29 20 74 6f 20 6f 62 74 61 69 6e 20  loc() to obtain 
588c0 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 66 69  space for the fi
588d0 6c 65 2d 68 61 6e 64 6c 65 20 73 74 72 75 63 74  le-handle struct
588e0 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ure..*/.SQLITE_P
588f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
58900 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 73  e3OsOpenMalloc(s
58910 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f  qlite3_vfs *, co
58920 6e 73 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69  nst char *, sqli
58930 74 65 33 5f 66 69 6c 65 20 2a 2a 2c 20 69 6e 74  te3_file **, int
58940 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ,int*);.SQLITE_P
58950 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
58960 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 73 71  e3OsCloseFree(sq
58970 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a  lite3_file *);..
58980 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c 49 54  #endif /* _SQLIT
58990 45 5f 4f 53 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a  E_OS_H_ */../***
589a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
589b0 6f 66 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  of os.h ********
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
589f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
58a00 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
58a10 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69  left off in sqli
58a20 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
58a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
58a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
58a50 75 64 65 20 6d 75 74 65 78 2e 68 20 69 6e 20 74  ude mutex.h in t
58a60 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c  he middle of sql
58a70 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
58a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
58a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
58aa0 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 68 20 2a  n file mutex.h *
58ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
58ae0 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38  * 2007 August 28
58af0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
58b00 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
58b10 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
58b20 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
58b30 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
58b40 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
58b50 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
58b60 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
58b70 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
58b80 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
58b90 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
58ba0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
58bb0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
58bc0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
58bd0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
58be0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
58bf0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
58c00 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
58c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
58c50 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
58c60 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6d  contains the com
58c70 6d 6f 6e 20 68 65 61 64 65 72 20 66 6f 72 20 61  mon header for a
58c80 6c 6c 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65  ll mutex impleme
58c90 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65  ntations..** The
58ca0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 68 65 61   sqliteInt.h hea
58cb0 64 65 72 20 23 69 6e 63 6c 75 64 65 73 20 74 68  der #includes th
58cc0 69 73 20 66 69 6c 65 20 73 6f 20 74 68 61 74 20  is file so that 
58cd0 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 0a  it is available.
58ce0 2a 2a 20 74 6f 20 61 6c 6c 20 73 6f 75 72 63 65  ** to all source
58cf0 20 66 69 6c 65 73 2e 20 20 57 65 20 62 72 65 61   files.  We brea
58d00 6b 20 69 74 20 6f 75 74 20 69 6e 20 61 6e 20 65  k it out in an e
58d10 66 66 6f 72 74 20 74 6f 20 6b 65 65 70 20 74 68  ffort to keep th
58d20 65 20 63 6f 64 65 0a 2a 2a 20 62 65 74 74 65 72  e code.** better
58d30 20 6f 72 67 61 6e 69 7a 65 64 2e 0a 2a 2a 0a 2a   organized..**.*
58d40 2a 20 4e 4f 54 45 3a 20 20 73 6f 75 72 63 65 20  * NOTE:  source 
58d50 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 2a 6e 6f  files should *no
58d60 74 2a 20 23 69 6e 63 6c 75 64 65 20 74 68 69 73  t* #include this
58d70 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 69 72   header file dir
58d80 65 63 74 6c 79 2e 0a 2a 2a 20 53 6f 75 72 63 65  ectly..** Source
58d90 20 66 69 6c 65 73 20 73 68 6f 75 6c 64 20 23 69   files should #i
58da0 6e 63 6c 75 64 65 20 74 68 65 20 73 71 6c 69 74  nclude the sqlit
58db0 65 49 6e 74 2e 68 20 66 69 6c 65 20 61 6e 64 20  eInt.h file and 
58dc0 6c 65 74 20 74 68 61 74 20 66 69 6c 65 0a 2a 2a  let that file.**
58dd0 20 69 6e 63 6c 75 64 65 20 74 68 69 73 20 6f 6e   include this on
58de0 65 20 69 6e 64 69 72 65 63 74 6c 79 2e 0a 2a 2a  e indirectly..**
58df0 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 2e 68  .** $Id: mutex.h
58e00 2c 76 20 31 2e 39 20 32 30 30 38 2f 31 30 2f 30  ,v 1.9 2008/10/0
58e10 37 20 31 35 3a 32 35 3a 34 38 20 64 72 68 20 45  7 15:25:48 drh E
58e20 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  xp $.*/.../*.** 
58e30 46 69 67 75 72 65 20 6f 75 74 20 77 68 61 74 20  Figure out what 
58e40 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 63  version of the c
58e50 6f 64 65 20 74 6f 20 75 73 65 2e 20 20 54 68 65  ode to use.  The
58e60 20 63 68 6f 69 63 65 73 20 61 72 65 0a 2a 2a 0a   choices are.**.
58e70 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  **   SQLITE_MUTE
58e80 58 5f 4f 4d 49 54 20 20 20 20 20 20 20 20 20 4e  X_OMIT         N
58e90 6f 20 6d 75 74 65 78 20 6c 6f 67 69 63 2e 20 20  o mutex logic.  
58ea0 4e 6f 74 20 65 76 65 6e 20 73 74 75 62 73 2e 20  Not even stubs. 
58eb0 20 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   The.**         
58ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58ed0 20 20 20 20 6d 75 74 65 78 65 73 20 69 6d 70 6c      mutexes impl
58ee0 65 6d 65 6e 74 69 6f 6e 20 63 61 6e 6e 6f 74 20  emention cannot 
58ef0 62 65 20 6f 76 65 72 72 69 64 64 65 6e 0a 2a 2a  be overridden.**
58f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58f10 20 20 20 20 20 20 20 20 20 20 20 20 20 61 74 20               at 
58f20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a  start-time..**.*
58f30 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  *   SQLITE_MUTEX
58f40 5f 4e 4f 4f 50 20 20 20 20 20 20 20 20 20 46 6f  _NOOP         Fo
58f50 72 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65  r single-threade
58f60 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 20  d applications. 
58f70 20 4e 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   No.**          
58f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58f90 20 20 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73     mutual exclus
58fa0 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 2e  ion is provided.
58fb0 20 20 42 75 74 20 74 68 69 73 0a 2a 2a 20 20 20    But this.**   
58fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
58fd0 20 20 20 20 20 20 20 20 20 20 69 6d 70 6c 65 6d            implem
58fe0 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20 62 65 20  entation can be 
58ff0 6f 76 65 72 72 69 64 64 65 6e 20 61 74 0a 2a 2a  overridden at.**
59000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59010 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61               sta
59020 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20  rt-time..**.**  
59030 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54   SQLITE_MUTEX_PT
59040 48 52 45 41 44 53 20 20 20 20 20 46 6f 72 20 6d  HREADS     For m
59050 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70  ulti-threaded ap
59060 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 55 6e  plications on Un
59070 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49  ix..**.**   SQLI
59080 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 20 20 20  TE_MUTEX_W32    
59090 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d        For multi-
590a0 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61  threaded applica
590b0 74 69 6f 6e 73 20 6f 6e 20 57 69 6e 33 32 2e 0a  tions on Win32..
590c0 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d  **.**   SQLITE_M
590d0 55 54 45 58 5f 4f 53 32 20 20 20 20 20 20 20 20  UTEX_OS2        
590e0 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65    For multi-thre
590f0 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e  aded application
59100 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a 2f 0a 23 69  s on OS/2..*/.#i
59110 66 20 21 53 51 4c 49 54 45 5f 54 48 52 45 41 44  f !SQLITE_THREAD
59120 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51  SAFE.# define SQ
59130 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a  LITE_MUTEX_OMIT.
59140 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
59150 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20  E_THREADSAFE && 
59160 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
59170 4d 55 54 45 58 5f 4e 4f 4f 50 29 0a 23 20 20 69  MUTEX_NOOP).#  i
59180 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58  f SQLITE_OS_UNIX
59190 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c  .#    define SQL
591a0 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41  ITE_MUTEX_PTHREA
591b0 44 53 0a 23 20 20 65 6c 69 66 20 53 51 4c 49 54  DS.#  elif SQLIT
591c0 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 20 64 65  E_OS_WIN.#    de
591d0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  fine SQLITE_MUTE
591e0 58 5f 57 33 32 0a 23 20 20 65 6c 69 66 20 53 51  X_W32.#  elif SQ
591f0 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 20 20  LITE_OS_OS2.#   
59200 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
59210 55 54 45 58 5f 4f 53 32 0a 23 20 20 65 6c 73 65  UTEX_OS2.#  else
59220 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c  .#    define SQL
59230 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 0a 23  ITE_MUTEX_NOOP.#
59240 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a    endif.#endif..
59250 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55  #ifdef SQLITE_MU
59260 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49  TEX_OMIT./*.** I
59270 66 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f  f this is a no-o
59280 70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  p implementation
59290 2c 20 69 6d 70 6c 65 6d 65 6e 74 20 65 76 65 72  , implement ever
592a0 79 74 68 69 6e 67 20 61 73 20 6d 61 63 72 6f 73  ything as macros
592b0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71 6c  ..*/.#define sql
592c0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
592d0 28 58 29 20 20 20 20 28 28 73 71 6c 69 74 65 33  (X)    ((sqlite3
592e0 5f 6d 75 74 65 78 2a 29 38 29 0a 23 64 65 66 69  _mutex*)8).#defi
592f0 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
59300 5f 66 72 65 65 28 58 29 0a 23 64 65 66 69 6e 65  _free(X).#define
59310 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
59320 6e 74 65 72 28 58 29 0a 23 64 65 66 69 6e 65 20  nter(X).#define 
59330 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
59340 79 28 58 29 20 20 20 20 20 20 53 51 4c 49 54 45  y(X)      SQLITE
59350 5f 4f 4b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  _OK.#define sqli
59360 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
59370 58 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  X).#define sqlit
59380 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 58 29  e3_mutex_held(X)
59390 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 73       1.#define s
593a0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
593b0 68 65 6c 64 28 58 29 20 20 31 0a 23 64 65 66 69  held(X)  1.#defi
593c0 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41  ne sqlite3MutexA
593d0 6c 6c 6f 63 28 58 29 20 20 20 20 20 20 28 28 73  lloc(X)      ((s
593e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29  qlite3_mutex*)8)
593f0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
59400 4d 75 74 65 78 49 6e 69 74 28 29 20 20 20 20 20  MutexInit()     
59410 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 64 65     SQLITE_OK.#de
59420 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65  fine sqlite3Mute
59430 78 45 6e 64 28 29 0a 23 65 6e 64 69 66 20 2f 2a  xEnd().#endif /*
59440 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
59450 4d 55 54 45 58 5f 4f 4d 49 54 29 20 2a 2f 0a 0a  MUTEX_OMIT) */..
59460 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
59470 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e 68 20 2a  End of mutex.h *
59480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
594a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
594b0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
594c0 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
594d0 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
594e0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
594f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
59500 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74  ../*.** Each dat
59510 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 62 65  abase file to be
59520 20 61 63 63 65 73 73 65 64 20 62 79 20 74 68 65   accessed by the
59530 20 73 79 73 74 65 6d 20 69 73 20 61 6e 20 69 6e   system is an in
59540 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65  stance.** of the
59550 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
59560 74 75 72 65 2e 20 20 54 68 65 72 65 20 61 72 65  ture.  There are
59570 20 6e 6f 72 6d 61 6c 6c 79 20 74 77 6f 20 6f 66   normally two of
59580 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65   these structure
59590 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c 69  s.** in the sqli
595a0 74 65 2e 61 44 62 5b 5d 20 61 72 72 61 79 2e 20  te.aDb[] array. 
595b0 20 61 44 62 5b 30 5d 20 69 73 20 74 68 65 20 6d   aDb[0] is the m
595c0 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c  ain database fil
595d0 65 20 61 6e 64 0a 2a 2a 20 61 44 62 5b 31 5d 20  e and.** aDb[1] 
595e0 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  is the database 
595f0 66 69 6c 65 20 75 73 65 64 20 74 6f 20 68 6f 6c  file used to hol
59600 64 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  d temporary tabl
59610 65 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 0a  es.  Additional.
59620 2a 2a 20 64 61 74 61 62 61 73 65 73 20 6d 61 79  ** databases may
59630 20 62 65 20 61 74 74 61 63 68 65 64 2e 0a 2a 2f   be attached..*/
59640 0a 73 74 72 75 63 74 20 44 62 20 7b 0a 20 20 63  .struct Db {.  c
59650 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
59660 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
59670 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  his database */.
59680 20 20 42 74 72 65 65 20 2a 70 42 74 3b 20 20 20    Btree *pBt;   
59690 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 2a         /* The B*
596a0 54 72 65 65 20 73 74 72 75 63 74 75 72 65 20 66  Tree structure f
596b0 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73 65  or this database
596c0 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 69 6e   file */.  u8 in
596d0 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20 20  Trans;          
596e0 2f 2a 20 30 3a 20 6e 6f 74 20 77 72 69 74 61 62  /* 0: not writab
596f0 6c 65 2e 20 20 31 3a 20 54 72 61 6e 73 61 63 74  le.  1: Transact
59700 69 6f 6e 2e 20 20 32 3a 20 43 68 65 63 6b 70 6f  ion.  2: Checkpo
59710 69 6e 74 20 2a 2f 0a 20 20 75 38 20 73 61 66 65  int */.  u8 safe
59720 74 79 5f 6c 65 76 65 6c 3b 20 20 20 20 20 2f 2a  ty_level;     /*
59730 20 48 6f 77 20 61 67 67 72 65 73 73 69 76 65 20   How aggressive 
59740 61 74 20 73 79 6e 63 69 6e 67 20 64 61 74 61 20  at syncing data 
59750 74 6f 20 64 69 73 6b 20 2a 2f 0a 20 20 53 63 68  to disk */.  Sch
59760 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20 20 20  ema *pSchema;   
59770 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
59780 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
59790 28 70 6f 73 73 69 62 6c 79 20 73 68 61 72 65 64  (possibly shared
597a0 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  ) */.};../*.** A
597b0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
597c0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
597d0 63 74 75 72 65 20 73 74 6f 72 65 73 20 61 20 64  cture stores a d
597e0 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 0a  atabase schema..
597f0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61  **.** If there a
59800 72 65 20 6e 6f 20 76 69 72 74 75 61 6c 20 74 61  re no virtual ta
59810 62 6c 65 73 20 63 6f 6e 66 69 67 75 72 65 64 20  bles configured 
59820 69 6e 20 74 68 69 73 20 73 63 68 65 6d 61 2c 20  in this schema, 
59830 74 68 65 0a 2a 2a 20 53 63 68 65 6d 61 2e 64 62  the.** Schema.db
59840 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74   variable is set
59850 20 74 6f 20 4e 55 4c 4c 2e 20 41 66 74 65 72 20   to NULL. After 
59860 74 68 65 20 66 69 72 73 74 20 76 69 72 74 75 61  the first virtua
59870 6c 20 74 61 62 6c 65 0a 2a 2a 20 68 61 73 20 62  l table.** has b
59880 65 65 6e 20 61 64 64 65 64 2c 20 69 74 20 69 73  een added, it is
59890 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f   set to point to
598a0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
598b0 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a 20 75 73 65  nnection .** use
598c0 64 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20  d to create the 
598d0 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f 6e 63 65  connection. Once
598e0 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
598f0 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 61 64 64   has been.** add
59900 65 64 20 74 6f 20 74 68 65 20 53 63 68 65 6d 61  ed to the Schema
59910 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 74   structure and t
59920 68 65 20 53 63 68 65 6d 61 2e 64 62 20 76 61 72  he Schema.db var
59930 69 61 62 6c 65 20 70 6f 70 75 6c 61 74 65 64 2c  iable populated,
59940 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68 61 74 20 64   .** only that d
59950 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
59960 6f 6e 20 6d 61 79 20 75 73 65 20 74 68 65 20 53  on may use the S
59970 63 68 65 6d 61 20 74 6f 20 70 72 65 70 61 72 65  chema to prepare
59980 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 2e   .** statements.
59990 0a 2a 2f 0a 73 74 72 75 63 74 20 53 63 68 65 6d  .*/.struct Schem
599a0 61 20 7b 0a 20 20 69 6e 74 20 73 63 68 65 6d 61  a {.  int schema
599b0 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f 2a 20 44 61  _cookie;   /* Da
599c0 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 76 65  tabase schema ve
599d0 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 66 6f 72  rsion number for
599e0 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20   this file */.  
599f0 48 61 73 68 20 74 62 6c 48 61 73 68 3b 20 20 20  Hash tblHash;   
59a00 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c       /* All tabl
59a10 65 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61  es indexed by na
59a20 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 69 64 78  me */.  Hash idx
59a30 48 61 73 68 3b 20 20 20 20 20 20 20 20 2f 2a 20  Hash;        /* 
59a40 41 6c 6c 20 28 6e 61 6d 65 64 29 20 69 6e 64 69  All (named) indi
59a50 63 65 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e  ces indexed by n
59a60 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68 20 74 72  ame */.  Hash tr
59a70 69 67 48 61 73 68 3b 20 20 20 20 20 20 20 2f 2a  igHash;       /*
59a80 20 41 6c 6c 20 74 72 69 67 67 65 72 73 20 69 6e   All triggers in
59a90 64 65 78 65 64 20 62 79 20 6e 61 6d 65 20 2a 2f  dexed by name */
59aa0 0a 20 20 48 61 73 68 20 66 6b 65 79 48 61 73 68  .  Hash fkeyHash
59ab0 3b 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 66  ;       /* All f
59ac0 6f 72 65 69 67 6e 20 6b 65 79 73 20 62 79 20 72  oreign keys by r
59ad0 65 66 65 72 65 6e 63 65 64 20 74 61 62 6c 65 20  eferenced table 
59ae0 6e 61 6d 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20  name */.  Table 
59af0 2a 70 53 65 71 54 61 62 3b 20 20 20 20 20 20 2f  *pSeqTab;      /
59b00 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 65 71  * The sqlite_seq
59b10 75 65 6e 63 65 20 74 61 62 6c 65 20 75 73 65 64  uence table used
59b20 20 62 79 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e   by AUTOINCREMEN
59b30 54 20 2a 2f 0a 20 20 75 38 20 66 69 6c 65 5f 66  T */.  u8 file_f
59b40 6f 72 6d 61 74 3b 20 20 20 20 20 20 2f 2a 20 53  ormat;      /* S
59b50 63 68 65 6d 61 20 66 6f 72 6d 61 74 20 76 65 72  chema format ver
59b60 73 69 6f 6e 20 66 6f 72 20 74 68 69 73 20 66 69  sion for this fi
59b70 6c 65 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20  le */.  u8 enc; 
59b80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
59b90 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73  Text encoding us
59ba0 65 64 20 62 79 20 74 68 69 73 20 64 61 74 61 62  ed by this datab
59bb0 61 73 65 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61  ase */.  u16 fla
59bc0 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  gs;           /*
59bd0 20 46 6c 61 67 73 20 61 73 73 6f 63 69 61 74 65   Flags associate
59be0 64 20 77 69 74 68 20 74 68 69 73 20 73 63 68 65  d with this sche
59bf0 6d 61 20 2a 2f 0a 20 20 69 6e 74 20 63 61 63 68  ma */.  int cach
59c00 65 5f 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20  e_size;      /* 
59c10 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  Number of pages 
59c20 74 6f 20 75 73 65 20 69 6e 20 74 68 65 20 63 61  to use in the ca
59c30 63 68 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  che */.#ifndef S
59c40 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
59c50 41 4c 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65  ALTABLE.  sqlite
59c60 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 2f  3 *db;         /
59c70 2a 20 22 4f 77 6e 65 72 22 20 63 6f 6e 6e 65 63  * "Owner" connec
59c80 74 69 6f 6e 2e 20 53 65 65 20 63 6f 6d 6d 65 6e  tion. See commen
59c90 74 20 61 62 6f 76 65 20 2a 2f 0a 23 65 6e 64 69  t above */.#endi
59ca0 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73  f.};../*.** Thes
59cb0 65 20 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20  e macros can be 
59cc0 75 73 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65  used to test, se
59cd0 74 2c 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73  t, or clear bits
59ce0 20 69 6e 20 74 68 65 20 0a 2a 2a 20 44 62 2e 66   in the .** Db.f
59cf0 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23  lags field..*/.#
59d00 64 65 66 69 6e 65 20 44 62 48 61 73 50 72 6f 70  define DbHasProp
59d10 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 20 20  erty(D,I,P)     
59d20 28 28 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53  (((D)->aDb[I].pS
59d30 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29  chema->flags&(P)
59d40 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20  )==(P)).#define 
59d50 44 62 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  DbHasAnyProperty
59d60 28 44 2c 49 2c 50 29 20 20 28 28 28 44 29 2d 3e  (D,I,P)  (((D)->
59d70 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e  aDb[I].pSchema->
59d80 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23  flags&(P))!=0).#
59d90 64 65 66 69 6e 65 20 44 62 53 65 74 50 72 6f 70  define DbSetProp
59da0 65 72 74 79 28 44 2c 49 2c 50 29 20 20 20 20 20  erty(D,I,P)     
59db0 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68  (D)->aDb[I].pSch
59dc0 65 6d 61 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a  ema->flags|=(P).
59dd0 23 64 65 66 69 6e 65 20 44 62 43 6c 65 61 72 50  #define DbClearP
59de0 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20 20  roperty(D,I,P)  
59df0 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63   (D)->aDb[I].pSc
59e00 68 65 6d 61 2d 3e 66 6c 61 67 73 26 3d 7e 28 50  hema->flags&=~(P
59e10 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64  )../*.** Allowed
59e20 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20   values for the 
59e30 44 42 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a  DB.flags field..
59e40 2a 2a 0a 2a 2a 20 54 68 65 20 44 42 5f 53 63 68  **.** The DB_Sch
59e50 65 6d 61 4c 6f 61 64 65 64 20 66 6c 61 67 20 69  emaLoaded flag i
59e60 73 20 73 65 74 20 61 66 74 65 72 20 74 68 65 20  s set after the 
59e70 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
59e80 68 61 73 20 62 65 65 6e 0a 2a 2a 20 72 65 61 64  has been.** read
59e90 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68   into internal h
59ea0 61 73 68 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a  ash tables..**.*
59eb0 2a 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77  * DB_UnresetView
59ec0 73 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 65  s means that one
59ed0 20 6f 72 20 6d 6f 72 65 20 76 69 65 77 73 20 68   or more views h
59ee0 61 76 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  ave column names
59ef0 20 74 68 61 74 0a 2a 2a 20 68 61 76 65 20 62 65   that.** have be
59f00 65 6e 20 66 69 6c 6c 65 64 20 6f 75 74 2e 20 20  en filled out.  
59f10 49 66 20 74 68 65 20 73 63 68 65 6d 61 20 63 68  If the schema ch
59f20 61 6e 67 65 73 2c 20 74 68 65 73 65 20 63 6f 6c  anges, these col
59f30 75 6d 6e 20 6e 61 6d 65 73 20 6d 69 67 68 74 0a  umn names might.
59f40 2a 2a 20 63 68 61 6e 67 65 73 20 61 6e 64 20 73  ** changes and s
59f50 6f 20 74 68 65 20 76 69 65 77 20 77 69 6c 6c 20  o the view will 
59f60 6e 65 65 64 20 74 6f 20 62 65 20 72 65 73 65 74  need to be reset
59f70 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f  ..*/.#define DB_
59f80 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 20 20 20  SchemaLoaded    
59f90 30 78 30 30 30 31 20 20 2f 2a 20 54 68 65 20 73  0x0001  /* The s
59fa0 63 68 65 6d 61 20 68 61 73 20 62 65 65 6e 20 6c  chema has been l
59fb0 6f 61 64 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  oaded */.#define
59fc0 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73   DB_UnresetViews
59fd0 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20 53      0x0002  /* S
59fe0 6f 6d 65 20 76 69 65 77 73 20 68 61 76 65 20 64  ome views have d
59ff0 65 66 69 6e 65 64 20 63 6f 6c 75 6d 6e 20 6e 61  efined column na
5a000 6d 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44  mes */.#define D
5a010 42 5f 45 6d 70 74 79 20 20 20 20 20 20 20 20 20  B_Empty         
5a020 20 20 30 78 30 30 30 34 20 20 2f 2a 20 54 68 65    0x0004  /* The
5a030 20 66 69 6c 65 20 69 73 20 65 6d 70 74 79 20 28   file is empty (
5a040 6c 65 6e 67 74 68 20 30 20 62 79 74 65 73 29 20  length 0 bytes) 
5a050 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75  */../*.** The nu
5a060 6d 62 65 72 20 6f 66 20 64 69 66 66 65 72 65 6e  mber of differen
5a070 74 20 6b 69 6e 64 73 20 6f 66 20 74 68 69 6e 67  t kinds of thing
5a080 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 6c 69  s that can be li
5a090 6d 69 74 65 64 0a 2a 2a 20 75 73 69 6e 67 20 74  mited.** using t
5a0a0 68 65 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74  he sqlite3_limit
5a0b0 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f  () interface..*/
5a0c0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5a0d0 4e 5f 4c 49 4d 49 54 20 28 53 51 4c 49 54 45 5f  N_LIMIT (SQLITE_
5a0e0 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45  LIMIT_TRIGGER_DE
5a0f0 50 54 48 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  PTH+1)../*.** Lo
5a100 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 69  okaside malloc i
5a110 73 20 61 20 73 65 74 20 6f 66 20 66 69 78 65 64  s a set of fixed
5a120 2d 73 69 7a 65 20 62 75 66 66 65 72 73 20 74 68  -size buffers th
5a130 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 0a 2a  at can be used.*
5a140 2a 20 74 6f 20 73 61 74 69 73 66 79 20 73 6d 61  * to satisfy sma
5a150 6c 6c 20 74 72 61 6e 73 69 65 6e 74 20 6d 65 6d  ll transient mem
5a160 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72  ory allocation r
5a170 65 71 75 65 73 74 73 20 66 6f 72 20 6f 62 6a 65  equests for obje
5a180 63 74 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65  cts.** associate
5a190 64 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75  d with a particu
5a1a0 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e  lar database con
5a1b0 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 75 73  nection.  The us
5a1c0 65 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64  e of.** lookasid
5a1d0 65 20 6d 61 6c 6c 6f 63 20 70 72 6f 76 69 64 65  e malloc provide
5a1e0 73 20 61 20 73 69 67 6e 69 66 69 63 61 6e 74 20  s a significant 
5a1f0 70 65 72 66 6f 72 6d 61 6e 63 65 20 65 6e 68 61  performance enha
5a200 6e 63 65 6d 65 6e 74 0a 2a 2a 20 28 61 70 70 72  ncement.** (appr
5a210 6f 78 20 31 30 25 29 20 62 79 20 61 76 6f 69 64  ox 10%) by avoid
5a220 69 6e 67 20 6e 75 6d 65 72 6f 75 73 20 6d 61 6c  ing numerous mal
5a230 6c 6f 63 2f 66 72 65 65 20 72 65 71 75 65 73 74  loc/free request
5a240 73 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a  s while parsing.
5a250 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ** SQL statement
5a260 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4c 6f 6f  s..**.** The Loo
5a270 6b 61 73 69 64 65 20 73 74 72 75 63 74 75 72 65  kaside structure
5a280 20 68 6f 6c 64 73 20 63 6f 6e 66 69 67 75 72 61   holds configura
5a290 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  tion information
5a2a0 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a 20 6c 6f   about the.** lo
5a2b0 6f 6b 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 73  okaside malloc s
5a2c0 75 62 73 79 73 74 65 6d 2e 20 20 45 61 63 68 20  ubsystem.  Each 
5a2d0 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79  available memory
5a2e0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 0a 2a   allocation in.*
5a2f0 2a 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20  * the lookaside 
5a300 73 75 62 73 79 73 74 65 6d 20 69 73 20 73 74 6f  subsystem is sto
5a310 72 65 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20  red on a linked 
5a320 6c 69 73 74 20 6f 66 20 4c 6f 6f 6b 61 73 69 64  list of Lookasid
5a330 65 53 6c 6f 74 0a 2a 2a 20 6f 62 6a 65 63 74 73  eSlot.** objects
5a340 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 6f 6b 61 73 69 64  ..**.** Lookasid
5a350 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72  e allocations ar
5a360 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 66  e only allowed f
5a370 6f 72 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20  or objects that 
5a380 61 72 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a  are associated.*
5a390 2a 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75  * with a particu
5a3a0 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e  lar database con
5a3b0 6e 65 63 74 69 6f 6e 2e 20 20 48 65 6e 63 65 2c  nection.  Hence,
5a3c0 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74   schema informat
5a3d0 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65  ion cannot.** be
5a3e0 20 73 74 6f 72 65 64 20 69 6e 20 6c 6f 6f 6b 61   stored in looka
5a3f0 73 69 64 65 20 62 65 63 61 75 73 65 20 69 6e 20  side because in 
5a400 73 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64  shared cache mod
5a410 65 20 74 68 65 20 73 63 68 65 6d 61 20 69 6e 66  e the schema inf
5a420 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73  ormation.** is s
5a430 68 61 72 65 64 20 62 79 20 6d 75 6c 74 69 70 6c  hared by multipl
5a440 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
5a450 63 74 69 6f 6e 73 2e 20 20 54 68 65 72 65 66 6f  ctions.  Therefo
5a460 72 65 2c 20 77 68 69 6c 65 20 70 61 72 73 69 6e  re, while parsin
5a470 67 0a 2a 2a 20 73 63 68 65 6d 61 20 69 6e 66 6f  g.** schema info
5a480 72 6d 61 74 69 6f 6e 2c 20 74 68 65 20 4c 6f 6f  rmation, the Loo
5a490 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20  kaside.bEnabled 
5a4a0 66 6c 61 67 20 69 73 20 63 6c 65 61 72 65 64 20  flag is cleared 
5a4b0 73 6f 20 74 68 61 74 0a 2a 2a 20 6c 6f 6f 6b 61  so that.** looka
5a4c0 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  side allocations
5a4d0 20 61 72 65 20 6e 6f 74 20 75 73 65 64 20 74 6f   are not used to
5a4e0 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 73   construct the s
5a4f0 63 68 65 6d 61 20 6f 62 6a 65 63 74 73 2e 0a 2a  chema objects..*
5a500 2f 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69  /.struct Lookasi
5a510 64 65 20 7b 0a 20 20 75 31 36 20 73 7a 3b 20 20  de {.  u16 sz;  
5a520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5a530 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 62  * Size of each b
5a540 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a  uffer in bytes *
5a550 2f 0a 20 20 75 38 20 62 45 6e 61 62 6c 65 64 3b  /.  u8 bEnabled;
5a560 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
5a570 61 6c 73 65 20 74 6f 20 64 69 73 61 62 6c 65 20  alse to disable 
5a580 6e 65 77 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c  new lookaside al
5a590 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 75  locations */.  u
5a5a0 38 20 62 4d 61 6c 6c 6f 63 65 64 3b 20 20 20 20  8 bMalloced;    
5a5b0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
5a5c0 66 20 70 53 74 61 72 74 20 6f 62 74 61 69 6e 65  f pStart obtaine
5a5d0 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d  d from sqlite3_m
5a5e0 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 69 6e 74  alloc() */.  int
5a5f0 20 6e 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20   nOut;          
5a600 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
5a610 66 20 62 75 66 66 65 72 73 20 63 75 72 72 65 6e  f buffers curren
5a620 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 20  tly checked out 
5a630 2a 2f 0a 20 20 69 6e 74 20 6d 78 4f 75 74 3b 20  */.  int mxOut; 
5a640 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5a650 48 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 66  Highwater mark f
5a660 6f 72 20 6e 4f 75 74 20 2a 2f 0a 20 20 4c 6f 6f  or nOut */.  Loo
5a670 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 46 72 65  kasideSlot *pFre
5a680 65 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20  e;   /* List of 
5a690 61 76 61 69 6c 61 62 6c 65 20 62 75 66 66 65 72  available buffer
5a6a0 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74  s */.  void *pSt
5a6b0 61 72 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  art;           /
5a6c0 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20  * First byte of 
5a6d0 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79  available memory
5a6e0 20 73 70 61 63 65 20 2a 2f 0a 20 20 76 6f 69 64   space */.  void
5a6f0 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20 20   *pEnd;         
5a700 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74      /* First byt
5a710 65 20 70 61 73 74 20 65 6e 64 20 6f 66 20 61 76  e past end of av
5a720 61 69 6c 61 62 6c 65 20 73 70 61 63 65 20 2a 2f  ailable space */
5a730 0a 7d 3b 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61  .};.struct Looka
5a740 73 69 64 65 53 6c 6f 74 20 7b 0a 20 20 4c 6f 6f  sideSlot {.  Loo
5a750 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 4e 65 78  kasideSlot *pNex
5a760 74 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 62 75  t;    /* Next bu
5a770 66 66 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74  ffer in the list
5a780 20 6f 66 20 66 72 65 65 20 62 75 66 66 65 72 73   of free buffers
5a790 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20   */.};../*.** A 
5a7a0 68 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66  hash table for f
5a7b0 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69  unction definiti
5a7c0 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 20  ons..**.** Hash 
5a7d0 65 61 63 68 20 46 75 6e 63 44 65 66 20 73 74 72  each FuncDef str
5a7e0 75 63 74 75 72 65 20 69 6e 74 6f 20 6f 6e 65 20  ucture into one 
5a7f0 6f 66 20 74 68 65 20 46 75 6e 63 44 65 66 48 61  of the FuncDefHa
5a800 73 68 2e 61 5b 5d 20 73 6c 6f 74 73 2e 0a 2a 2a  sh.a[] slots..**
5a810 20 43 6f 6c 6c 69 73 69 6f 6e 73 20 61 72 65 20   Collisions are 
5a820 6f 6e 20 74 68 65 20 46 75 6e 63 44 65 66 2e 70  on the FuncDef.p
5a830 48 61 73 68 20 63 68 61 69 6e 2e 0a 2a 2f 0a 73  Hash chain..*/.s
5a840 74 72 75 63 74 20 46 75 6e 63 44 65 66 48 61 73  truct FuncDefHas
5a850 68 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 61  h {.  FuncDef *a
5a860 5b 32 33 5d 3b 20 20 20 20 20 20 20 2f 2a 20 48  [23];       /* H
5a870 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 75  ash table for fu
5a880 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d 3b 0a 0a 2f  nctions */.};../
5a890 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74 61 62 61  *.** Each databa
5a8a0 73 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  se is an instanc
5a8b0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
5a8c0 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ng structure..**
5a8d0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6c  .** The sqlite.l
5a8e0 61 73 74 52 6f 77 69 64 20 72 65 63 6f 72 64 73  astRowid records
5a8f0 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72 74   the last insert
5a900 20 72 6f 77 69 64 20 67 65 6e 65 72 61 74 65 64   rowid generated
5a910 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 65 72 74   by an.** insert
5a920 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e 73   statement.  Ins
5a930 65 72 74 73 20 6f 6e 20 76 69 65 77 73 20 64 6f  erts on views do
5a940 20 6e 6f 74 20 61 66 66 65 63 74 20 69 74 73 20   not affect its 
5a950 76 61 6c 75 65 2e 20 20 45 61 63 68 0a 2a 2a 20  value.  Each.** 
5a960 74 72 69 67 67 65 72 20 68 61 73 20 69 74 73 20  trigger has its 
5a970 6f 77 6e 20 63 6f 6e 74 65 78 74 2c 20 73 6f 20  own context, so 
5a980 74 68 61 74 20 6c 61 73 74 52 6f 77 69 64 20 63  that lastRowid c
5a990 61 6e 20 62 65 20 75 70 64 61 74 65 64 20 69 6e  an be updated in
5a9a0 73 69 64 65 0a 2a 2a 20 74 72 69 67 67 65 72 73  side.** triggers
5a9b0 20 61 73 20 75 73 75 61 6c 2e 20 20 54 68 65 20   as usual.  The 
5a9c0 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 77  previous value w
5a9d0 69 6c 6c 20 62 65 20 72 65 73 74 6f 72 65 64 20  ill be restored 
5a9e0 6f 6e 63 65 20 74 68 65 20 74 72 69 67 67 65 72  once the trigger
5a9f0 0a 2a 2a 20 65 78 69 74 73 2e 20 20 55 70 6f 6e  .** exits.  Upon
5aa00 20 65 6e 74 65 72 69 6e 67 20 61 20 62 65 66 6f   entering a befo
5aa10 72 65 20 6f 72 20 69 6e 73 74 65 61 64 20 6f 66  re or instead of
5aa20 20 74 72 69 67 67 65 72 2c 20 6c 61 73 74 52 6f   trigger, lastRo
5aa30 77 69 64 20 69 73 20 6e 6f 0a 2a 2a 20 6c 6f 6e  wid is no.** lon
5aa40 67 65 72 20 28 73 69 6e 63 65 20 61 66 74 65 72  ger (since after
5aa50 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 31 32 29   version 2.8.12)
5aa60 20 72 65 73 65 74 20 74 6f 20 2d 31 2e 0a 2a 2a   reset to -1..**
5aa70 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6e  .** The sqlite.n
5aa80 43 68 61 6e 67 65 20 64 6f 65 73 20 6e 6f 74 20  Change does not 
5aa90 63 6f 75 6e 74 20 63 68 61 6e 67 65 73 20 77 69  count changes wi
5aaa0 74 68 69 6e 20 74 72 69 67 67 65 72 73 20 61 6e  thin triggers an
5aab0 64 20 6b 65 65 70 73 20 6e 6f 0a 2a 2a 20 63 6f  d keeps no.** co
5aac0 6e 74 65 78 74 2e 20 20 49 74 20 69 73 20 72 65  ntext.  It is re
5aad0 73 65 74 20 61 74 20 73 74 61 72 74 20 6f 66 20  set at start of 
5aae0 73 71 6c 69 74 65 33 5f 65 78 65 63 2e 0a 2a 2a  sqlite3_exec..**
5aaf0 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 73 43 68   The sqlite.lsCh
5ab00 61 6e 67 65 20 72 65 70 72 65 73 65 6e 74 73 20  ange represents 
5ab10 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  the number of ch
5ab20 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74 68  anges made by th
5ab30 65 20 6c 61 73 74 0a 2a 2a 20 69 6e 73 65 72 74  e last.** insert
5ab40 2c 20 75 70 64 61 74 65 2c 20 6f 72 20 64 65 6c  , update, or del
5ab50 65 74 65 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ete statement.  
5ab60 49 74 20 72 65 6d 61 69 6e 73 20 63 6f 6e 73 74  It remains const
5ab70 61 6e 74 20 74 68 72 6f 75 67 68 6f 75 74 20 74  ant throughout t
5ab80 68 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 6f 66 20  he.** length of 
5ab90 61 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20  a statement and 
5aba0 69 73 20 74 68 65 6e 20 75 70 64 61 74 65 64 20  is then updated 
5abb0 62 79 20 4f 50 5f 53 65 74 43 6f 75 6e 74 73 2e  by OP_SetCounts.
5abc0 20 20 49 74 20 6b 65 65 70 73 20 61 0a 2a 2a 20    It keeps a.** 
5abd0 63 6f 6e 74 65 78 74 20 73 74 61 63 6b 20 6a 75  context stack ju
5abe0 73 74 20 6c 69 6b 65 20 6c 61 73 74 52 6f 77 69  st like lastRowi
5abf0 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f  d so that the co
5ac00 75 6e 74 20 6f 66 20 63 68 61 6e 67 65 73 0a 2a  unt of changes.*
5ac10 2a 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 67  * within a trigg
5ac20 65 72 20 69 73 20 6e 6f 74 20 73 65 65 6e 20 6f  er is not seen o
5ac30 75 74 73 69 64 65 20 74 68 65 20 74 72 69 67 67  utside the trigg
5ac40 65 72 2e 20 20 43 68 61 6e 67 65 73 20 74 6f 20  er.  Changes to 
5ac50 76 69 65 77 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20  views do not.** 
5ac60 61 66 66 65 63 74 20 74 68 65 20 76 61 6c 75 65  affect the value
5ac70 20 6f 66 20 6c 73 43 68 61 6e 67 65 2e 0a 2a 2a   of lsChange..**
5ac80 20 54 68 65 20 73 71 6c 69 74 65 2e 63 73 43 68   The sqlite.csCh
5ac90 61 6e 67 65 20 6b 65 65 70 73 20 74 72 61 63 6b  ange keeps track
5aca0 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   of the number o
5acb0 66 20 63 75 72 72 65 6e 74 20 63 68 61 6e 67 65  f current change
5acc0 73 20 28 73 69 6e 63 65 0a 2a 2a 20 74 68 65 20  s (since.** the 
5acd0 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 29 20  last statement) 
5ace0 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 75  and is used to u
5acf0 70 64 61 74 65 20 73 71 6c 69 74 65 5f 6c 73 43  pdate sqlite_lsC
5ad00 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  hange..**.** The
5ad10 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65   member variable
5ad20 73 20 73 71 6c 69 74 65 2e 65 72 72 43 6f 64 65  s sqlite.errCode
5ad30 2c 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73 67  , sqlite.zErrMsg
5ad40 20 61 6e 64 20 73 71 6c 69 74 65 2e 7a 45 72 72   and sqlite.zErr
5ad50 4d 73 67 31 36 0a 2a 2a 20 73 74 6f 72 65 20 74  Msg16.** store t
5ad60 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65  he most recent e
5ad70 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 2c 20 69  rror code and, i
5ad80 66 20 61 70 70 6c 69 63 61 62 6c 65 2c 20 73 74  f applicable, st
5ad90 72 69 6e 67 2e 20 54 68 65 0a 2a 2a 20 69 6e 74  ring. The.** int
5ada0 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20 73  ernal function s
5adb0 71 6c 69 74 65 33 45 72 72 6f 72 28 29 20 69 73  qlite3Error() is
5adc0 20 75 73 65 64 20 74 6f 20 73 65 74 20 74 68 65   used to set the
5add0 73 65 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20  se variables.** 
5ade0 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 0a 2a 2f  consistently..*/
5adf0 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 20  .struct sqlite3 
5ae00 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  {.  sqlite3_vfs 
5ae10 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 20  *pVfs;          
5ae20 20 20 2f 2a 20 4f 53 20 49 6e 74 65 72 66 61 63    /* OS Interfac
5ae30 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 62 3b 20  e */.  int nDb; 
5ae40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ae50 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
5ae60 66 20 62 61 63 6b 65 6e 64 73 20 63 75 72 72 65  f backends curre
5ae70 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 20  ntly in use */. 
5ae80 20 44 62 20 2a 61 44 62 3b 20 20 20 20 20 20 20   Db *aDb;       
5ae90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5aea0 2a 20 41 6c 6c 20 62 61 63 6b 65 6e 64 73 20 2a  * All backends *
5aeb0 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20  /.  int flags;  
5aec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aed0 20 20 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e 65 6f    /* Miscellaneo
5aee0 75 73 20 66 6c 61 67 73 2e 20 53 65 65 20 62 65  us flags. See be
5aef0 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65  low */.  int ope
5af00 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20  nFlags;         
5af10 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
5af20 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65  passed to sqlite
5af30 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f  3_vfs.xOpen() */
5af40 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20  .  int errCode; 
5af50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5af60 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20   /* Most recent 
5af70 65 72 72 6f 72 20 63 6f 64 65 20 28 53 51 4c 49  error code (SQLI
5af80 54 45 5f 2a 29 20 2a 2f 0a 20 20 69 6e 74 20 65  TE_*) */.  int e
5af90 72 72 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20  rrMask;         
5afa0 20 20 20 20 20 20 20 20 20 2f 2a 20 26 20 72 65           /* & re
5afb0 73 75 6c 74 20 63 6f 64 65 73 20 77 69 74 68 20  sult codes with 
5afc0 74 68 69 73 20 62 65 66 6f 72 65 20 72 65 74 75  this before retu
5afd0 72 6e 69 6e 67 20 2a 2f 0a 20 20 75 38 20 61 75  rning */.  u8 au
5afe0 74 6f 43 6f 6d 6d 69 74 3b 20 20 20 20 20 20 20  toCommit;       
5aff0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
5b000 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67  auto-commit flag
5b010 2e 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70 5f 73  . */.  u8 temp_s
5b020 74 6f 72 65 3b 20 20 20 20 20 20 20 20 20 20 20  tore;           
5b030 20 20 20 20 20 2f 2a 20 31 3a 20 66 69 6c 65 20       /* 1: file 
5b040 32 3a 20 6d 65 6d 6f 72 79 20 30 3a 20 64 65 66  2: memory 0: def
5b050 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6d 61 6c  ault */.  u8 mal
5b060 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20 20 20 20  locFailed;      
5b070 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
5b080 69 66 20 77 65 20 68 61 76 65 20 73 65 65 6e 20  if we have seen 
5b090 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  a malloc failure
5b0a0 20 2a 2f 0a 20 20 75 38 20 64 66 6c 74 4c 6f 63   */.  u8 dfltLoc
5b0b0 6b 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20  kMode;          
5b0c0 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c      /* Default l
5b0d0 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20  ocking-mode for 
5b0e0 61 74 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0a  attached dbs */.
5b0f0 20 20 75 38 20 64 66 6c 74 4a 6f 75 72 6e 61 6c    u8 dfltJournal
5b100 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  Mode;           
5b110 2f 2a 20 44 65 66 61 75 6c 74 20 6a 6f 75 72 6e  /* Default journ
5b120 61 6c 20 6d 6f 64 65 20 66 6f 72 20 61 74 74 61  al mode for atta
5b130 63 68 65 64 20 64 62 73 20 2a 2f 0a 20 20 73 69  ched dbs */.  si
5b140 67 6e 65 64 20 63 68 61 72 20 6e 65 78 74 41 75  gned char nextAu
5b150 74 6f 76 61 63 3b 20 20 20 20 20 20 2f 2a 20 41  tovac;      /* A
5b160 75 74 6f 76 61 63 20 73 65 74 74 69 6e 67 20 61  utovac setting a
5b170 66 74 65 72 20 56 41 43 55 55 4d 20 69 66 20 3e  fter VACUUM if >
5b180 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 78 74  =0 */.  int next
5b190 50 61 67 65 73 69 7a 65 3b 20 20 20 20 20 20 20  Pagesize;       
5b1a0 20 20 20 20 20 20 2f 2a 20 50 61 67 65 73 69 7a        /* Pagesiz
5b1b0 65 20 61 66 74 65 72 20 56 41 43 55 55 4d 20 69  e after VACUUM i
5b1c0 66 20 3e 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  f >0 */.  int nT
5b1d0 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20  able;           
5b1e0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
5b1f0 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74  r of tables in t
5b200 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  he database */. 
5b210 20 43 6f 6c 6c 53 65 71 20 2a 70 44 66 6c 74 43   CollSeq *pDfltC
5b220 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f  oll;           /
5b230 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f  * The default co
5b240 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
5b250 20 28 42 49 4e 41 52 59 29 20 2a 2f 0a 20 20 69   (BINARY) */.  i
5b260 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20  64 lastRowid;   
5b270 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5b280 52 4f 57 49 44 20 6f 66 20 6d 6f 73 74 20 72 65  ROWID of most re
5b290 63 65 6e 74 20 69 6e 73 65 72 74 20 28 73 65 65  cent insert (see
5b2a0 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 75 33 32   above) */.  u32
5b2b0 20 6d 61 67 69 63 3b 20 20 20 20 20 20 20 20 20   magic;         
5b2c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
5b2d0 67 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 64  gic number for d
5b2e0 65 74 65 63 74 20 6c 69 62 72 61 72 79 20 6d 69  etect library mi
5b2f0 73 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  suse */.  int nC
5b300 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20  hange;          
5b310 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
5b320 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c   returned by sql
5b330 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 2a  ite3_changes() *
5b340 2f 0a 20 20 69 6e 74 20 6e 54 6f 74 61 6c 43 68  /.  int nTotalCh
5b350 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  ange;           
5b360 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72    /* Value retur
5b370 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 74  ned by sqlite3_t
5b380 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 2a  otal_changes() *
5b390 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  /.  sqlite3_mute
5b3a0 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20  x *mutex;       
5b3b0 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20    /* Connection 
5b3c0 6d 75 74 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61  mutex */.  int a
5b3d0 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4e 5f 4c  Limit[SQLITE_N_L
5b3e0 49 4d 49 54 5d 3b 20 20 20 2f 2a 20 4c 69 6d 69  IMIT];   /* Limi
5b3f0 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73  ts */.  struct s
5b400 71 6c 69 74 65 33 49 6e 69 74 49 6e 66 6f 20 7b  qlite3InitInfo {
5b410 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61        /* Informa
5b420 74 69 6f 6e 20 75 73 65 64 20 64 75 72 69 6e 67  tion used during
5b430 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
5b440 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 62 3b 20  */.    int iDb; 
5b450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b460 20 20 20 2f 2a 20 57 68 65 6e 20 62 61 63 6b 20     /* When back 
5b470 69 73 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c  is being initial
5b480 69 7a 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ized */.    int 
5b490 6e 65 77 54 6e 75 6d 3b 20 20 20 20 20 20 20 20  newTnum;        
5b4a0 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 70          /* Rootp
5b4b0 61 67 65 20 6f 66 20 74 61 62 6c 65 20 62 65 69  age of table bei
5b4c0 6e 67 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a  ng initialized *
5b4d0 2f 0a 20 20 20 20 75 38 20 62 75 73 79 3b 20 20  /.    u8 busy;  
5b4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b4f0 20 20 2f 2a 20 54 52 55 45 20 69 66 20 63 75 72    /* TRUE if cur
5b500 72 65 6e 74 6c 79 20 69 6e 69 74 69 61 6c 69 7a  rently initializ
5b510 69 6e 67 20 2a 2f 0a 20 20 20 20 75 38 20 6f 72  ing */.    u8 or
5b520 70 68 61 6e 54 72 69 67 67 65 72 3b 20 20 20 20  phanTrigger;    
5b530 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 73         /* Last s
5b540 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 72 70 68  tatement is orph
5b550 61 6e 65 64 20 54 45 4d 50 20 74 72 69 67 67 65  aned TEMP trigge
5b560 72 20 2a 2f 0a 20 20 7d 20 69 6e 69 74 3b 0a 20  r */.  } init;. 
5b570 20 69 6e 74 20 6e 45 78 74 65 6e 73 69 6f 6e 3b   int nExtension;
5b580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5b590 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 6f 61 64  * Number of load
5b5a0 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 2a 2f  ed extensions */
5b5b0 0a 20 20 76 6f 69 64 20 2a 2a 61 45 78 74 65 6e  .  void **aExten
5b5c0 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20  sion;           
5b5d0 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 68 61   /* Array of sha
5b5e0 72 65 64 20 6c 69 62 72 61 72 79 20 68 61 6e 64  red library hand
5b5f0 6c 65 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  les */.  struct 
5b600 56 64 62 65 20 2a 70 56 64 62 65 3b 20 20 20 20  Vdbe *pVdbe;    
5b610 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f         /* List o
5b620 66 20 61 63 74 69 76 65 20 76 69 72 74 75 61 6c  f active virtual
5b630 20 6d 61 63 68 69 6e 65 73 20 2a 2f 0a 20 20 69   machines */.  i
5b640 6e 74 20 61 63 74 69 76 65 56 64 62 65 43 6e 74  nt activeVdbeCnt
5b650 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
5b660 4e 75 6d 62 65 72 20 6f 66 20 56 44 42 45 73 20  Number of VDBEs 
5b670 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74  currently execut
5b680 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 77 72 69  ing */.  int wri
5b690 74 65 56 64 62 65 43 6e 74 3b 20 20 20 20 20 20  teVdbeCnt;      
5b6a0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
5b6b0 20 6f 66 20 61 63 74 69 76 65 20 56 44 42 45 73   of active VDBEs
5b6c0 20 74 68 61 74 20 61 72 65 20 77 72 69 74 69 6e   that are writin
5b6d0 67 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 54  g */.  void (*xT
5b6e0 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73  race)(void*,cons
5b6f0 74 20 63 68 61 72 2a 29 3b 20 20 20 20 20 20 20  t char*);       
5b700 20 2f 2a 20 54 72 61 63 65 20 66 75 6e 63 74 69   /* Trace functi
5b710 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 54  on */.  void *pT
5b720 72 61 63 65 41 72 67 3b 20 20 20 20 20 20 20 20  raceArg;        
5b730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b740 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
5b750 20 74 68 65 20 74 72 61 63 65 20 66 75 6e 63 74   the trace funct
5b760 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  ion */.  void (*
5b770 78 50 72 6f 66 69 6c 65 29 28 76 6f 69 64 2a 2c  xProfile)(void*,
5b780 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 36 34 29  const char*,u64)
5b790 3b 20 20 2f 2a 20 50 72 6f 66 69 6c 69 6e 67 20  ;  /* Profiling 
5b7a0 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f  function */.  vo
5b7b0 69 64 20 2a 70 50 72 6f 66 69 6c 65 41 72 67 3b  id *pProfileArg;
5b7c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b7d0 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d          /* Argum
5b7e0 65 6e 74 20 74 6f 20 70 72 6f 66 69 6c 65 20 66  ent to profile f
5b7f0 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69  unction */.  voi
5b800 64 20 2a 70 43 6f 6d 6d 69 74 41 72 67 3b 20 20  d *pCommitArg;  
5b810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5b820 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 43  * Argument to xC
5b830 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 28 29 20  ommitCallback() 
5b840 2a 2f 20 20 20 0a 20 20 69 6e 74 20 28 2a 78 43  */   .  int (*xC
5b850 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 29 28 76  ommitCallback)(v
5b860 6f 69 64 2a 29 3b 20 20 20 20 2f 2a 20 49 6e 76  oid*);    /* Inv
5b870 6f 6b 65 64 20 61 74 20 65 76 65 72 79 20 63 6f  oked at every co
5b880 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 20  mmit. */.  void 
5b890 2a 70 52 6f 6c 6c 62 61 63 6b 41 72 67 3b 20 20  *pRollbackArg;  
5b8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5b8b0 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 52 6f 6c  Argument to xRol
5b8c0 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 28 29 20  lbackCallback() 
5b8d0 2a 2f 20 20 20 0a 20 20 76 6f 69 64 20 28 2a 78  */   .  void (*x
5b8e0 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b  RollbackCallback
5b8f0 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 49 6e 76  )(void*); /* Inv
5b900 6f 6b 65 64 20 61 74 20 65 76 65 72 79 20 63 6f  oked at every co
5b910 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76 6f 69 64 20  mmit. */.  void 
5b920 2a 70 55 70 64 61 74 65 41 72 67 3b 0a 20 20 76  *pUpdateArg;.  v
5b930 6f 69 64 20 28 2a 78 55 70 64 61 74 65 43 61 6c  oid (*xUpdateCal
5b940 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74  lback)(void*,int
5b950 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  , const char*,co
5b960 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65  nst char*,sqlite
5b970 5f 69 6e 74 36 34 29 3b 0a 20 20 76 6f 69 64 28  _int64);.  void(
5b980 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 29 28 76 6f  *xCollNeeded)(vo
5b990 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74  id*,sqlite3*,int
5b9a0 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20   eTextRep,const 
5b9b0 63 68 61 72 2a 29 3b 0a 20 20 76 6f 69 64 28 2a  char*);.  void(*
5b9c0 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 29 28 76  xCollNeeded16)(v
5b9d0 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e  oid*,sqlite3*,in
5b9e0 74 20 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74  t eTextRep,const
5b9f0 20 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20   void*);.  void 
5ba00 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b  *pCollNeededArg;
5ba10 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
5ba20 20 2a 70 45 72 72 3b 20 20 20 20 20 20 20 20 20   *pErr;         
5ba30 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20   /* Most recent 
5ba40 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f  error message */
5ba50 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
5ba60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5ba70 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20   /* Most recent 
5ba80 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28 55  error message (U
5ba90 54 46 2d 38 20 65 6e 63 6f 64 65 64 29 20 2a 2f  TF-8 encoded) */
5baa0 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
5bab0 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  16;             
5bac0 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20   /* Most recent 
5bad0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 28 55  error message (U
5bae0 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 29 20 2a  TF-16 encoded) *
5baf0 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20  /.  union {.    
5bb00 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 69 73 49  volatile int isI
5bb10 6e 74 65 72 72 75 70 74 65 64 3b 20 2f 2a 20 54  nterrupted; /* T
5bb20 72 75 65 20 69 66 20 73 71 6c 69 74 65 33 5f 69  rue if sqlite3_i
5bb30 6e 74 65 72 72 75 70 74 20 68 61 73 20 62 65 65  nterrupt has bee
5bb40 6e 20 63 61 6c 6c 65 64 20 2a 2f 0a 20 20 20 20  n called */.    
5bb50 64 6f 75 62 6c 65 20 6e 6f 74 55 73 65 64 31 3b  double notUsed1;
5bb60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
5bb70 70 61 63 65 72 20 2a 2f 0a 20 20 7d 20 75 31 3b  pacer */.  } u1;
5bb80 0a 20 20 4c 6f 6f 6b 61 73 69 64 65 20 6c 6f 6f  .  Lookaside loo
5bb90 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 20  kaside;         
5bba0 20 2f 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d 61   /* Lookaside ma
5bbb0 6c 6c 6f 63 20 63 6f 6e 66 69 67 75 72 61 74 69  lloc configurati
5bbc0 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  on */.#ifndef SQ
5bbd0 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
5bbe0 49 5a 41 54 49 4f 4e 0a 20 20 69 6e 74 20 28 2a  IZATION.  int (*
5bbf0 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74  xAuth)(void*,int
5bc00 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
5bc10 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
5bc20 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
5bc30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
5bc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bc50 20 20 20 2f 2a 20 41 63 63 65 73 73 20 61 75 74     /* Access aut
5bc60 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74  horization funct
5bc70 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ion */.  void *p
5bc80 41 75 74 68 41 72 67 3b 20 20 20 20 20 20 20 20  AuthArg;        
5bc90 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20 61 72         /* 1st ar
5bca0 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 61 63  gument to the ac
5bcb0 63 65 73 73 20 61 75 74 68 20 66 75 6e 63 74 69  cess auth functi
5bcc0 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66  on */.#endif.#if
5bcd0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
5bce0 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41  _PROGRESS_CALLBA
5bcf0 43 4b 0a 20 20 69 6e 74 20 28 2a 78 50 72 6f 67  CK.  int (*xProg
5bd00 72 65 73 73 29 28 76 6f 69 64 20 2a 29 3b 20 20  ress)(void *);  
5bd10 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 65     /* The progre
5bd20 73 73 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20  ss callback */. 
5bd30 20 76 6f 69 64 20 2a 70 50 72 6f 67 72 65 73 73   void *pProgress
5bd40 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Arg;           /
5bd50 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  * Argument to th
5bd60 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62  e progress callb
5bd70 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 72  ack */.  int nPr
5bd80 6f 67 72 65 73 73 4f 70 73 3b 20 20 20 20 20 20  ogressOps;      
5bd90 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
5bda0 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f 72 20   of opcodes for 
5bdb0 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
5bdc0 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e  k */.#endif.#ifn
5bdd0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
5bde0 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 48  VIRTUALTABLE.  H
5bdf0 61 73 68 20 61 4d 6f 64 75 6c 65 3b 20 20 20 20  ash aModule;    
5be00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5be10 70 6f 70 75 6c 61 74 65 64 20 62 79 20 73 71 6c  populated by sql
5be20 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
5be30 6c 65 28 29 20 2a 2f 0a 20 20 54 61 62 6c 65 20  le() */.  Table 
5be40 2a 70 56 54 61 62 3b 20 20 20 20 20 20 20 20 20  *pVTab;         
5be50 20 20 20 20 20 20 20 20 2f 2a 20 76 74 61 62 20          /* vtab 
5be60 77 69 74 68 20 61 63 74 69 76 65 20 43 6f 6e 6e  with active Conn
5be70 65 63 74 2f 43 72 65 61 74 65 20 6d 65 74 68 6f  ect/Create metho
5be80 64 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20 2a 2a  d */.  VTable **
5be90 61 56 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20  aVTrans;        
5bea0 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20       /* Virtual 
5beb0 74 61 62 6c 65 73 20 77 69 74 68 20 6f 70 65 6e  tables with open
5bec0 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 2a 2f   transactions */
5bed0 0a 20 20 69 6e 74 20 6e 56 54 72 61 6e 73 3b 20  .  int nVTrans; 
5bee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bef0 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73 69   /* Allocated si
5bf00 7a 65 20 6f 66 20 61 56 54 72 61 6e 73 20 2a 2f  ze of aVTrans */
5bf10 0a 20 20 56 54 61 62 6c 65 20 2a 70 44 69 73 63  .  VTable *pDisc
5bf20 6f 6e 6e 65 63 74 3b 20 20 20 20 2f 2a 20 44 69  onnect;    /* Di
5bf30 73 63 6f 6e 6e 65 63 74 20 74 68 65 73 65 20 69  sconnect these i
5bf40 6e 20 6e 65 78 74 20 73 71 6c 69 74 65 33 5f 70  n next sqlite3_p
5bf50 72 65 70 61 72 65 28 29 20 2a 2f 0a 23 65 6e 64  repare() */.#end
5bf60 69 66 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68  if.  FuncDefHash
5bf70 20 61 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20   aFunc;         
5bf80 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65     /* Hash table
5bf90 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66   of connection f
5bfa0 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 48 61  unctions */.  Ha
5bfb0 73 68 20 61 43 6f 6c 6c 53 65 71 3b 20 20 20 20  sh aCollSeq;    
5bfc0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
5bfd0 6c 6c 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  ll collating seq
5bfe0 75 65 6e 63 65 73 20 2a 2f 0a 20 20 42 75 73 79  uences */.  Busy
5bff0 48 61 6e 64 6c 65 72 20 62 75 73 79 48 61 6e 64  Handler busyHand
5c000 6c 65 72 3b 20 20 20 20 20 20 2f 2a 20 42 75 73  ler;      /* Bus
5c010 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20  y callback */.  
5c020 69 6e 74 20 62 75 73 79 54 69 6d 65 6f 75 74 3b  int busyTimeout;
5c030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5c040 20 42 75 73 79 20 68 61 6e 64 6c 65 72 20 74 69   Busy handler ti
5c050 6d 65 6f 75 74 2c 20 69 6e 20 6d 73 65 63 20 2a  meout, in msec *
5c060 2f 0a 20 20 44 62 20 61 44 62 53 74 61 74 69 63  /.  Db aDbStatic
5c070 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20  [2];            
5c080 20 20 2f 2a 20 53 74 61 74 69 63 20 73 70 61 63    /* Static spac
5c090 65 20 66 6f 72 20 74 68 65 20 32 20 64 65 66 61  e for the 2 defa
5c0a0 75 6c 74 20 62 61 63 6b 65 6e 64 73 20 2a 2f 0a  ult backends */.
5c0b0 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61    Savepoint *pSa
5c0c0 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20  vepoint;        
5c0d0 2f 2a 20 4c 69 73 74 20 6f 66 20 61 63 74 69 76  /* List of activ
5c0e0 65 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a  e savepoints */.
5c0f0 20 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74    int nSavepoint
5c100 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5c110 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e  /* Number of non
5c120 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76  -transaction sav
5c130 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74  epoints */.  int
5c140 20 6e 53 74 61 74 65 6d 65 6e 74 3b 20 20 20 20   nStatement;    
5c150 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
5c160 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 73  mber of nested s
5c170 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63  tatement-transac
5c180 74 69 6f 6e 73 20 20 2a 2f 0a 20 20 75 38 20 69  tions  */.  u8 i
5c190 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65  sTransactionSave
5c1a0 70 6f 69 6e 74 3b 20 20 20 20 2f 2a 20 54 72 75  point;    /* Tru
5c1b0 65 20 69 66 20 74 68 65 20 6f 75 74 65 72 6d 6f  e if the outermo
5c1c0 73 74 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20  st savepoint is 
5c1d0 61 20 54 53 20 2a 2f 0a 20 20 69 36 34 20 6e 44  a TS */.  i64 nD
5c1e0 65 66 65 72 72 65 64 43 6f 6e 73 3b 20 20 20 20  eferredCons;    
5c1f0 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 74 20 64          /* Net d
5c200 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69  eferred constrai
5c210 6e 74 73 20 74 68 69 73 20 74 72 61 6e 73 61 63  nts this transac
5c220 74 69 6f 6e 2e 20 2a 2f 0a 0a 23 69 66 64 65 66  tion. */..#ifdef
5c230 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55   SQLITE_ENABLE_U
5c240 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 20 20 2f  NLOCK_NOTIFY.  /
5c250 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
5c260 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 61 6c  variables are al
5c270 6c 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 74  l protected by t
5c280 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52  he STATIC_MASTER
5c290 20 0a 20 20 2a 2a 20 6d 75 74 65 78 2c 20 6e 6f   .  ** mutex, no
5c2a0 74 20 62 79 20 73 71 6c 69 74 65 33 2e 6d 75 74  t by sqlite3.mut
5c2b0 65 78 2e 20 54 68 65 79 20 61 72 65 20 75 73 65  ex. They are use
5c2c0 64 20 62 79 20 63 6f 64 65 20 69 6e 20 6e 6f 74  d by code in not
5c2d0 69 66 79 2e 63 2e 20 0a 20 20 2a 2a 0a 20 20 2a  ify.c. .  **.  *
5c2e0 2a 20 57 68 65 6e 20 58 2e 70 55 6e 6c 6f 63 6b  * When X.pUnlock
5c2f0 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59 2c 20 74  Connection==Y, t
5c300 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 58  hat means that X
5c310 20 69 73 20 77 61 69 74 69 6e 67 20 66 6f 72 20   is waiting for 
5c320 59 20 74 6f 0a 20 20 2a 2a 20 75 6e 6c 6f 63 6b  Y to.  ** unlock
5c330 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20   so that it can 
5c340 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2a 0a 20 20  proceed..  **.  
5c350 2a 2a 20 57 68 65 6e 20 58 2e 70 42 6c 6f 63 6b  ** When X.pBlock
5c360 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 59  ingConnection==Y
5c370 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61  , that means tha
5c380 74 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74  t something that
5c390 20 58 20 74 72 69 65 64 0a 20 20 2a 2a 20 74 72   X tried.  ** tr
5c3a0 69 65 64 20 74 6f 20 64 6f 20 72 65 63 65 6e 74  ied to do recent
5c3b0 6c 79 20 66 61 69 6c 65 64 20 77 69 74 68 20 61  ly failed with a
5c3c0 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  n SQLITE_LOCKED 
5c3d0 65 72 72 6f 72 20 64 75 65 20 74 6f 20 6c 6f 63  error due to loc
5c3e0 6b 73 0a 20 20 2a 2a 20 68 65 6c 64 20 62 79 20  ks.  ** held by 
5c3f0 59 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  Y..  */.  sqlite
5c400 33 20 2a 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e  3 *pBlockingConn
5c410 65 63 74 69 6f 6e 3b 20 2f 2a 20 43 6f 6e 6e 65  ection; /* Conne
5c420 63 74 69 6f 6e 20 74 68 61 74 20 63 61 75 73 65  ction that cause
5c430 64 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  d SQLITE_LOCKED 
5c440 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 55  */.  sqlite3 *pU
5c450 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3b  nlockConnection;
5c460 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
5c470 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 77 61 74 63  nnection to watc
5c480 68 20 66 6f 72 20 75 6e 6c 6f 63 6b 20 2a 2f 0a  h for unlock */.
5c490 20 20 76 6f 69 64 20 2a 70 55 6e 6c 6f 63 6b 41    void *pUnlockA
5c4a0 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rg;             
5c4b0 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d          /* Argum
5c4c0 65 6e 74 20 74 6f 20 78 55 6e 6c 6f 63 6b 4e 6f  ent to xUnlockNo
5c4d0 74 69 66 79 20 2a 2f 0a 20 20 76 6f 69 64 20 28  tify */.  void (
5c4e0 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 29 28  *xUnlockNotify)(
5c4f0 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 29 3b 20 20  void **, int);  
5c500 2f 2a 20 55 6e 6c 6f 63 6b 20 6e 6f 74 69 66 79  /* Unlock notify
5c510 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 73   callback */.  s
5c520 71 6c 69 74 65 33 20 2a 70 4e 65 78 74 42 6c 6f  qlite3 *pNextBlo
5c530 63 6b 65 64 3b 20 20 20 20 20 20 20 20 2f 2a 20  cked;        /* 
5c540 4e 65 78 74 20 69 6e 20 6c 69 73 74 20 6f 66 20  Next in list of 
5c550 61 6c 6c 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e  all blocked conn
5c560 65 63 74 69 6f 6e 73 20 2a 2f 0a 23 65 6e 64 69  ections */.#endi
5c570 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61  f.};../*.** A ma
5c580 63 72 6f 20 74 6f 20 64 69 73 63 6f 76 65 72 20  cro to discover 
5c590 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20  the encoding of 
5c5a0 61 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 23  a database..*/.#
5c5b0 64 65 66 69 6e 65 20 45 4e 43 28 64 62 29 20 28  define ENC(db) (
5c5c0 28 64 62 29 2d 3e 61 44 62 5b 30 5d 2e 70 53 63  (db)->aDb[0].pSc
5c5d0 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a 2f 2a 0a 2a  hema->enc)../*.*
5c5e0 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65  * Possible value
5c5f0 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65  s for the sqlite
5c600 2e 66 6c 61 67 73 20 61 6e 64 20 6f 72 20 44 62  .flags and or Db
5c610 2e 66 6c 61 67 73 20 66 69 65 6c 64 73 2e 0a 2a  .flags fields..*
5c620 2a 0a 2a 2a 20 4f 6e 20 73 71 6c 69 74 65 2e 66  *.** On sqlite.f
5c630 6c 61 67 73 2c 20 74 68 65 20 53 51 4c 49 54 45  lags, the SQLITE
5c640 5f 49 6e 54 72 61 6e 73 20 76 61 6c 75 65 20 6d  _InTrans value m
5c650 65 61 6e 73 20 74 68 61 74 20 77 65 20 68 61 76  eans that we hav
5c660 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 61 20  e.** executed a 
5c670 42 45 47 49 4e 2e 20 20 4f 6e 20 44 62 2e 66 6c  BEGIN.  On Db.fl
5c680 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 6e 54 72  ags, SQLITE_InTr
5c690 61 6e 73 20 6d 65 61 6e 73 20 61 20 73 74 61 74  ans means a stat
5c6a0 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63  ement.** transac
5c6b0 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 6f  tion is active o
5c6c0 6e 20 74 68 61 74 20 70 61 72 74 69 63 75 6c 61  n that particula
5c6d0 72 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  r database file.
5c6e0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
5c6f0 54 45 5f 56 64 62 65 54 72 61 63 65 20 20 20 20  TE_VdbeTrace    
5c700 20 20 30 78 30 30 30 30 30 30 30 31 20 20 2f 2a    0x00000001  /*
5c710 20 54 72 75 65 20 74 6f 20 74 72 61 63 65 20 56   True to trace V
5c720 44 42 45 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f  DBE execution */
5c730 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5c740 49 6e 54 72 61 6e 73 20 20 20 20 20 20 20 20 30  InTrans        0
5c750 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20 54 72  x00000008  /* Tr
5c760 75 65 20 69 66 20 69 6e 20 61 20 74 72 61 6e 73  ue if in a trans
5c770 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e  action */.#defin
5c780 65 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43  e SQLITE_InternC
5c790 68 61 6e 67 65 73 20 20 30 78 30 30 30 30 30 30  hanges  0x000000
5c7a0 31 30 20 20 2f 2a 20 55 6e 63 6f 6d 6d 69 74 74  10  /* Uncommitt
5c7b0 65 64 20 48 61 73 68 20 74 61 62 6c 65 20 63 68  ed Hash table ch
5c7c0 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65  anges */.#define
5c7d0 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e   SQLITE_FullColN
5c7e0 61 6d 65 73 20 20 20 30 78 30 30 30 30 30 30 32  ames   0x0000002
5c7f0 30 20 20 2f 2a 20 53 68 6f 77 20 66 75 6c 6c 20  0  /* Show full 
5c800 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 6e 20  column names on 
5c810 53 45 4c 45 43 54 20 2a 2f 0a 23 64 65 66 69 6e  SELECT */.#defin
5c820 65 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f  e SQLITE_ShortCo
5c830 6c 4e 61 6d 65 73 20 20 30 78 30 30 30 30 30 30  lNames  0x000000
5c840 34 30 20 20 2f 2a 20 53 68 6f 77 20 73 68 6f 72  40  /* Show shor
5c850 74 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20  t columns names 
5c860 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
5c870 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20 20 20  E_CountRows     
5c880 20 30 78 30 30 30 30 30 30 38 30 20 20 2f 2a 20   0x00000080  /* 
5c890 43 6f 75 6e 74 20 72 6f 77 73 20 63 68 61 6e 67  Count rows chang
5c8a0 65 64 20 62 79 20 49 4e 53 45 52 54 2c 20 2a 2f  ed by INSERT, */
5c8b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
5c8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c8d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20             /*   
5c8e0 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54  DELETE, or UPDAT
5c8f0 45 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a  E and return */.
5c900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c920 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 74            /*   t
5c930 68 65 20 63 6f 75 6e 74 20 75 73 69 6e 67 20 61  he count using a
5c940 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23 64   callback. */.#d
5c950 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 75 6c  efine SQLITE_Nul
5c960 6c 43 61 6c 6c 62 61 63 6b 20 20 20 30 78 30 30  lCallback   0x00
5c970 30 30 30 31 30 30 20 20 2f 2a 20 49 6e 76 6f 6b  000100  /* Invok
5c980 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f  e the callback o
5c990 6e 63 65 20 69 66 20 74 68 65 20 2a 2f 0a 20 20  nce if the */.  
5c9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c9c0 20 20 20 20 20 20 20 20 2f 2a 20 20 20 72 65 73          /*   res
5c9d0 75 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74 79  ult set is empty
5c9e0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5c9f0 54 45 5f 53 71 6c 54 72 61 63 65 20 20 20 20 20  TE_SqlTrace     
5ca00 20 20 30 78 30 30 30 30 30 32 30 30 20 20 2f 2a    0x00000200  /*
5ca10 20 44 65 62 75 67 20 70 72 69 6e 74 20 53 51 4c   Debug print SQL
5ca20 20 61 73 20 69 74 20 65 78 65 63 75 74 65 73 20   as it executes 
5ca30 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
5ca40 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 20 20 20  E_VdbeListing   
5ca50 20 30 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20   0x00000400  /* 
5ca60 44 65 62 75 67 20 6c 69 73 74 69 6e 67 73 20 6f  Debug listings o
5ca70 66 20 56 44 42 45 20 70 72 6f 67 72 61 6d 73 20  f VDBE programs 
5ca80 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
5ca90 45 5f 57 72 69 74 65 53 63 68 65 6d 61 20 20 20  E_WriteSchema   
5caa0 20 30 78 30 30 30 30 30 38 30 30 20 20 2f 2a 20   0x00000800  /* 
5cab0 4f 4b 20 74 6f 20 75 70 64 61 74 65 20 53 51 4c  OK to update SQL
5cac0 49 54 45 5f 4d 41 53 54 45 52 20 2a 2f 0a 23 64  ITE_MASTER */.#d
5cad0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 6f 52  efine SQLITE_NoR
5cae0 65 61 64 6c 6f 63 6b 20 20 20 20 20 30 78 30 30  eadlock     0x00
5caf0 30 30 31 30 30 30 20 20 2f 2a 20 52 65 61 64 6c  001000  /* Readl
5cb00 6f 63 6b 73 20 61 72 65 20 6f 6d 69 74 74 65 64  ocks are omitted
5cb10 20 77 68 65 6e 20 0a 20 20 20 20 20 20 20 20 20   when .         
5cb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cb40 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 72 65   ** accessing re
5cb50 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65  ad-only database
5cb60 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s */.#define SQL
5cb70 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73  ITE_IgnoreChecks
5cb80 20 20 20 30 78 30 30 30 30 32 30 30 30 20 20 2f     0x00002000  /
5cb90 2a 20 44 6f 20 6e 6f 74 20 65 6e 66 6f 72 63 65  * Do not enforce
5cba0 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e   check constrain
5cbb0 74 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ts */.#define SQ
5cbc0 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69  LITE_ReadUncommi
5cbd0 74 74 65 64 20 30 78 30 30 30 30 34 30 30 30 20  tted 0x00004000 
5cbe0 2f 2a 20 46 6f 72 20 73 68 61 72 65 64 2d 63 61  /* For shared-ca
5cbf0 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 64 65 66  che mode */.#def
5cc00 69 6e 65 20 53 51 4c 49 54 45 5f 4c 65 67 61 63  ine SQLITE_Legac
5cc10 79 46 69 6c 65 46 6d 74 20 20 30 78 30 30 30 30  yFileFmt  0x0000
5cc20 38 30 30 30 20 20 2f 2a 20 43 72 65 61 74 65 20  8000  /* Create 
5cc30 6e 65 77 20 64 61 74 61 62 61 73 65 73 20 69 6e  new databases in
5cc40 20 66 6f 72 6d 61 74 20 31 20 2a 2f 0a 23 64 65   format 1 */.#de
5cc50 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c  fine SQLITE_Full
5cc60 46 53 79 6e 63 20 20 20 20 20 20 30 78 30 30 30  FSync      0x000
5cc70 31 30 30 30 30 20 20 2f 2a 20 55 73 65 20 66 75  10000  /* Use fu
5cc80 6c 6c 20 66 73 79 6e 63 20 6f 6e 20 74 68 65 20  ll fsync on the 
5cc90 62 61 63 6b 65 6e 64 20 2a 2f 0a 23 64 65 66 69  backend */.#defi
5cca0 6e 65 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78  ne SQLITE_LoadEx
5ccb0 74 65 6e 73 69 6f 6e 20 20 30 78 30 30 30 32 30  tension  0x00020
5ccc0 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c 65 20 6c  000  /* Enable l
5ccd0 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 20 2a 2f  oad_extension */
5cce0 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
5ccf0 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 20 20 20  _RecoveryMode   
5cd00 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a 20 49  0x00040000  /* I
5cd10 67 6e 6f 72 65 20 73 63 68 65 6d 61 20 65 72 72  gnore schema err
5cd20 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ors */.#define S
5cd30 51 4c 49 54 45 5f 52 65 76 65 72 73 65 4f 72 64  QLITE_ReverseOrd
5cd40 65 72 20 20 20 30 78 30 30 31 30 30 30 30 30 20  er   0x00100000 
5cd50 20 2f 2a 20 52 65 76 65 72 73 65 20 75 6e 6f 72   /* Reverse unor
5cd60 64 65 72 65 64 20 53 45 4c 45 43 54 73 20 2a 2f  dered SELECTs */
5cd70 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5cd80 52 65 63 54 72 69 67 67 65 72 73 20 20 20 20 30  RecTriggers    0
5cd90 78 30 30 32 30 30 30 30 30 20 20 2f 2a 20 45 6e  x00200000  /* En
5cda0 61 62 6c 65 20 72 65 63 75 72 73 69 76 65 20 74  able recursive t
5cdb0 72 69 67 67 65 72 73 20 2a 2f 0a 23 64 65 66 69  riggers */.#defi
5cdc0 6e 65 20 53 51 4c 49 54 45 5f 46 6f 72 65 69 67  ne SQLITE_Foreig
5cdd0 6e 4b 65 79 73 20 20 20 20 30 78 30 30 34 30 30  nKeys    0x00400
5cde0 30 30 30 20 20 2f 2a 20 45 6e 66 6f 72 63 65 20  000  /* Enforce 
5cdf0 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
5ce00 74 72 61 69 6e 74 73 20 20 2a 2f 0a 0a 2f 2a 0a  traints  */../*.
5ce10 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75  ** Possible valu
5ce20 65 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74  es for the sqlit
5ce30 65 2e 6d 61 67 69 63 20 66 69 65 6c 64 2e 0a 2a  e.magic field..*
5ce40 2a 20 54 68 65 20 6e 75 6d 62 65 72 73 20 61 72  * The numbers ar
5ce50 65 20 6f 62 74 61 69 6e 65 64 20 61 74 20 72 61  e obtained at ra
5ce60 6e 64 6f 6d 20 61 6e 64 20 68 61 76 65 20 6e 6f  ndom and have no
5ce70 20 73 70 65 63 69 61 6c 20 6d 65 61 6e 69 6e 67   special meaning
5ce80 2c 20 6f 74 68 65 72 0a 2a 2a 20 74 68 61 6e 20  , other.** than 
5ce90 62 65 69 6e 67 20 64 69 73 74 69 6e 63 74 20 66  being distinct f
5cea0 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74 68 65 72 2e  rom one another.
5ceb0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
5cec0 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 20 20  TE_MAGIC_OPEN   
5ced0 20 20 30 78 61 30 32 39 61 36 39 37 20 20 2f 2a    0xa029a697  /*
5cee0 20 44 61 74 61 62 61 73 65 20 69 73 20 6f 70 65   Database is ope
5cef0 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  n */.#define SQL
5cf00 49 54 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44  ITE_MAGIC_CLOSED
5cf10 20 20 20 30 78 39 66 33 63 32 64 33 33 20 20 2f     0x9f3c2d33  /
5cf20 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 63 6c  * Database is cl
5cf30 6f 73 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  osed */.#define 
5cf40 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49 43  SQLITE_MAGIC_SIC
5cf50 4b 20 20 20 20 20 30 78 34 62 37 37 31 32 39 30  K     0x4b771290
5cf60 20 20 2f 2a 20 45 72 72 6f 72 20 61 6e 64 20 61    /* Error and a
5cf70 77 61 69 74 69 6e 67 20 63 6c 6f 73 65 20 2a 2f  waiting close */
5cf80 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5cf90 4d 41 47 49 43 5f 42 55 53 59 20 20 20 20 20 30  MAGIC_BUSY     0
5cfa0 78 66 30 33 62 37 39 30 36 20 20 2f 2a 20 44 61  xf03b7906  /* Da
5cfb0 74 61 62 61 73 65 20 63 75 72 72 65 6e 74 6c 79  tabase currently
5cfc0 20 69 6e 20 75 73 65 20 2a 2f 0a 23 64 65 66 69   in use */.#defi
5cfd0 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  ne SQLITE_MAGIC_
5cfe0 45 52 52 4f 52 20 20 20 20 30 78 62 35 33 35 37  ERROR    0xb5357
5cff0 39 33 30 20 20 2f 2a 20 41 6e 20 53 51 4c 49 54  930  /* An SQLIT
5d000 45 5f 4d 49 53 55 53 45 20 65 72 72 6f 72 20 6f  E_MISUSE error o
5d010 63 63 75 72 72 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a  ccurred */../*.*
5d020 2a 20 45 61 63 68 20 53 51 4c 20 66 75 6e 63 74  * Each SQL funct
5d030 69 6f 6e 20 69 73 20 64 65 66 69 6e 65 64 20 62  ion is defined b
5d040 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  y an instance of
5d050 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a   the following.*
5d060 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 41 20  * structure.  A 
5d070 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20  pointer to this 
5d080 73 74 72 75 63 74 75 72 65 20 69 73 20 73 74 6f  structure is sto
5d090 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74  red in the sqlit
5d0a0 65 2e 61 46 75 6e 63 0a 2a 2a 20 68 61 73 68 20  e.aFunc.** hash 
5d0b0 74 61 62 6c 65 2e 20 20 57 68 65 6e 20 6d 75 6c  table.  When mul
5d0c0 74 69 70 6c 65 20 66 75 6e 63 74 69 6f 6e 73 20  tiple functions 
5d0d0 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 6e 61  have the same na
5d0e0 6d 65 2c 20 74 68 65 20 68 61 73 68 20 74 61 62  me, the hash tab
5d0f0 6c 65 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20  le.** points to 
5d100 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  a linked list of
5d110 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65   these structure
5d120 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 75 6e  s..*/.struct Fun
5d130 63 44 65 66 20 7b 0a 20 20 69 31 36 20 6e 41 72  cDef {.  i16 nAr
5d140 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  g;            /*
5d150 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   Number of argum
5d160 65 6e 74 73 2e 20 20 2d 31 20 6d 65 61 6e 73 20  ents.  -1 means 
5d170 75 6e 6c 69 6d 69 74 65 64 20 2a 2f 0a 20 20 75  unlimited */.  u
5d180 38 20 69 50 72 65 66 45 6e 63 3b 20 20 20 20 20  8 iPrefEnc;     
5d190 20 20 20 20 2f 2a 20 50 72 65 66 65 72 72 65 64      /* Preferred
5d1a0 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 28   text encoding (
5d1b0 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 31 36 4c  SQLITE_UTF8, 16L
5d1c0 45 2c 20 31 36 42 45 29 20 2a 2f 0a 20 20 75 38  E, 16BE) */.  u8
5d1d0 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
5d1e0 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62 69     /* Some combi
5d1f0 6e 61 74 69 6f 6e 20 6f 66 20 53 51 4c 49 54 45  nation of SQLITE
5d200 5f 46 55 4e 43 5f 2a 20 2a 2f 0a 20 20 76 6f 69  _FUNC_* */.  voi
5d210 64 20 2a 70 55 73 65 72 44 61 74 61 3b 20 20 20  d *pUserData;   
5d220 20 20 2f 2a 20 55 73 65 72 20 64 61 74 61 20 70    /* User data p
5d230 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 46 75  arameter */.  Fu
5d240 6e 63 44 65 66 20 2a 70 4e 65 78 74 3b 20 20 20  ncDef *pNext;   
5d250 20 20 20 2f 2a 20 4e 65 78 74 20 66 75 6e 63 74     /* Next funct
5d260 69 6f 6e 20 77 69 74 68 20 73 61 6d 65 20 6e 61  ion with same na
5d270 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  me */.  void (*x
5d280 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f  Func)(sqlite3_co
5d290 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
5d2a0 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20  e3_value**); /* 
5d2b0 52 65 67 75 6c 61 72 20 66 75 6e 63 74 69 6f 6e  Regular function
5d2c0 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53 74   */.  void (*xSt
5d2d0 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ep)(sqlite3_cont
5d2e0 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
5d2f0 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 41 67  _value**); /* Ag
5d300 67 72 65 67 61 74 65 20 73 74 65 70 20 2a 2f 0a  gregate step */.
5d310 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 69    void (*xFinali
5d320 7a 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ze)(sqlite3_cont
5d330 65 78 74 2a 29 3b 20 20 20 20 20 20 20 20 20 20  ext*);          
5d340 20 20 20 20 20 20 2f 2a 20 41 67 67 72 65 67 61        /* Aggrega
5d350 74 65 20 66 69 6e 61 6c 69 7a 65 72 20 2a 2f 0a  te finalizer */.
5d360 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
5d370 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 6e 61         /* SQL na
5d380 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  me of the functi
5d390 6f 6e 2e 20 2a 2f 0a 20 20 46 75 6e 63 44 65 66  on. */.  FuncDef
5d3a0 20 2a 70 48 61 73 68 3b 20 20 20 20 20 20 2f 2a   *pHash;      /*
5d3b0 20 4e 65 78 74 20 77 69 74 68 20 61 20 64 69 66   Next with a dif
5d3c0 66 65 72 65 6e 74 20 6e 61 6d 65 20 62 75 74 20  ferent name but 
5d3d0 74 68 65 20 73 61 6d 65 20 68 61 73 68 20 2a 2f  the same hash */
5d3e0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69  .};../*.** Possi
5d3f0 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72 20 46  ble values for F
5d400 75 6e 63 44 65 66 2e 66 6c 61 67 73 0a 2a 2f 0a  uncDef.flags.*/.
5d410 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46  #define SQLITE_F
5d420 55 4e 43 5f 4c 49 4b 45 20 20 20 20 20 30 78 30  UNC_LIKE     0x0
5d430 31 20 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 66  1 /* Candidate f
5d440 6f 72 20 74 68 65 20 4c 49 4b 45 20 6f 70 74 69  or the LIKE opti
5d450 6d 69 7a 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66  mization */.#def
5d460 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  ine SQLITE_FUNC_
5d470 43 41 53 45 20 20 20 20 20 30 78 30 32 20 2f 2a  CASE     0x02 /*
5d480 20 43 61 73 65 2d 73 65 6e 73 69 74 69 76 65 20   Case-sensitive 
5d490 4c 49 4b 45 2d 74 79 70 65 20 66 75 6e 63 74 69  LIKE-type functi
5d4a0 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  on */.#define SQ
5d4b0 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 20  LITE_FUNC_EPHEM 
5d4c0 20 20 20 30 78 30 34 20 2f 2a 20 45 70 68 65 6d     0x04 /* Ephem
5d4d0 65 72 61 6c 2e 20 20 44 65 6c 65 74 65 20 77 69  eral.  Delete wi
5d4e0 74 68 20 56 44 42 45 20 2a 2f 0a 23 64 65 66 69  th VDBE */.#defi
5d4f0 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e  ne SQLITE_FUNC_N
5d500 45 45 44 43 4f 4c 4c 20 30 78 30 38 20 2f 2a 20  EEDCOLL 0x08 /* 
5d510 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f  sqlite3GetFuncCo
5d520 6c 6c 53 65 71 28 29 20 6d 69 67 68 74 20 62 65  llSeq() might be
5d530 20 63 61 6c 6c 65 64 20 2a 2f 0a 23 64 65 66 69   called */.#defi
5d540 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 50  ne SQLITE_FUNC_P
5d550 52 49 56 41 54 45 20 20 30 78 31 30 20 2f 2a 20  RIVATE  0x10 /* 
5d560 41 6c 6c 6f 77 65 64 20 66 6f 72 20 69 6e 74 65  Allowed for inte
5d570 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f  rnal use only */
5d580 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5d590 46 55 4e 43 5f 43 4f 55 4e 54 20 20 20 20 30 78  FUNC_COUNT    0x
5d5a0 32 30 20 2f 2a 20 42 75 69 6c 74 2d 69 6e 20 63  20 /* Built-in c
5d5b0 6f 75 6e 74 28 2a 29 20 61 67 67 72 65 67 61 74  ount(*) aggregat
5d5c0 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  e */../*.** The 
5d5d0 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20  following three 
5d5e0 6d 61 63 72 6f 73 2c 20 46 55 4e 43 54 49 4f 4e  macros, FUNCTION
5d5f0 28 29 2c 20 4c 49 4b 45 46 55 4e 43 28 29 20 61  (), LIKEFUNC() a
5d600 6e 64 20 41 47 47 52 45 47 41 54 45 28 29 20 61  nd AGGREGATE() a
5d610 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63 72  re.** used to cr
5d620 65 61 74 65 20 74 68 65 20 69 6e 69 74 69 61 6c  eate the initial
5d630 69 7a 65 72 73 20 66 6f 72 20 74 68 65 20 46 75  izers for the Fu
5d640 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 65 73  ncDef structures
5d650 2e 0a 2a 2a 0a 2a 2a 20 20 20 46 55 4e 43 54 49  ..**.**   FUNCTI
5d660 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20  ON(zName, nArg, 
5d670 69 41 72 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63  iArg, bNC, xFunc
5d680 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 20 74 6f  ).**     Used to
5d690 20 63 72 65 61 74 65 20 61 20 73 63 61 6c 61 72   create a scalar
5d6a0 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69   function defini
5d6b0 74 69 6f 6e 20 6f 66 20 61 20 66 75 6e 63 74 69  tion of a functi
5d6c0 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a 20 20 20 20  on zName .**    
5d6d0 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20   implemented by 
5d6e0 43 20 66 75 6e 63 74 69 6f 6e 20 78 46 75 6e 63  C function xFunc
5d6f0 20 74 68 61 74 20 61 63 63 65 70 74 73 20 6e 41   that accepts nA
5d700 72 67 20 61 72 67 75 6d 65 6e 74 73 2e 20 54 68  rg arguments. Th
5d710 65 0a 2a 2a 20 20 20 20 20 76 61 6c 75 65 20 70  e.**     value p
5d720 61 73 73 65 64 20 61 73 20 69 41 72 67 20 69 73  assed as iArg is
5d730 20 63 61 73 74 20 74 6f 20 61 20 28 76 6f 69 64   cast to a (void
5d740 2a 29 20 61 6e 64 20 6d 61 64 65 20 61 76 61 69  *) and made avai
5d750 6c 61 62 6c 65 0a 2a 2a 20 20 20 20 20 61 73 20  lable.**     as 
5d760 74 68 65 20 75 73 65 72 2d 64 61 74 61 20 28 73  the user-data (s
5d770 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
5d780 28 29 29 20 66 6f 72 20 74 68 65 20 66 75 6e 63  ()) for the func
5d790 74 69 6f 6e 2e 20 49 66 20 0a 2a 2a 20 20 20 20  tion. If .**    
5d7a0 20 61 72 67 75 6d 65 6e 74 20 62 4e 43 20 69 73   argument bNC is
5d7b0 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20   true, then the 
5d7c0 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44  SQLITE_FUNC_NEED
5d7d0 43 4f 4c 4c 20 66 6c 61 67 20 69 73 20 73 65 74  COLL flag is set
5d7e0 2e 0a 2a 2a 0a 2a 2a 20 20 20 41 47 47 52 45 47  ..**.**   AGGREG
5d7f0 41 54 45 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c  ATE(zName, nArg,
5d800 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 53 74 65   iArg, bNC, xSte
5d810 70 2c 20 78 46 69 6e 61 6c 29 0a 2a 2a 20 20 20  p, xFinal).**   
5d820 20 20 55 73 65 64 20 74 6f 20 63 72 65 61 74 65    Used to create
5d830 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 66 75   an aggregate fu
5d840 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f  nction definitio
5d850 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79  n implemented by
5d860 0a 2a 2a 20 20 20 20 20 74 68 65 20 43 20 66 75  .**     the C fu
5d870 6e 63 74 69 6f 6e 73 20 78 53 74 65 70 20 61 6e  nctions xStep an
5d880 64 20 78 46 69 6e 61 6c 2e 20 54 68 65 20 66 69  d xFinal. The fi
5d890 72 73 74 20 66 6f 75 72 20 70 61 72 61 6d 65 74  rst four paramet
5d8a0 65 72 73 0a 2a 2a 20 20 20 20 20 61 72 65 20 69  ers.**     are i
5d8b0 6e 74 65 72 70 72 65 74 65 64 20 69 6e 20 74 68  nterpreted in th
5d8c0 65 20 73 61 6d 65 20 77 61 79 20 61 73 20 74 68  e same way as th
5d8d0 65 20 66 69 72 73 74 20 34 20 70 61 72 61 6d 65  e first 4 parame
5d8e0 74 65 72 73 20 74 6f 0a 2a 2a 20 20 20 20 20 46  ters to.**     F
5d8f0 55 4e 43 54 49 4f 4e 28 29 2e 0a 2a 2a 0a 2a 2a  UNCTION()..**.**
5d900 20 20 20 4c 49 4b 45 46 55 4e 43 28 7a 4e 61 6d     LIKEFUNC(zNam
5d910 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c 20 66  e, nArg, pArg, f
5d920 6c 61 67 73 29 0a 2a 2a 20 20 20 20 20 55 73 65  lags).**     Use
5d930 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 63  d to create a sc
5d940 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 64 65  alar function de
5d950 66 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20 66 75  finition of a fu
5d960 6e 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a  nction zName .**
5d970 20 20 20 20 20 74 68 61 74 20 61 63 63 65 70 74       that accept
5d980 73 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73  s nArg arguments
5d990 20 61 6e 64 20 69 73 20 69 6d 70 6c 65 6d 65 6e   and is implemen
5d9a0 74 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f  ted by a call to
5d9b0 20 43 20 0a 2a 2a 20 20 20 20 20 66 75 6e 63 74   C .**     funct
5d9c0 69 6f 6e 20 6c 69 6b 65 46 75 6e 63 2e 20 41 72  ion likeFunc. Ar
5d9d0 67 75 6d 65 6e 74 20 70 41 72 67 20 69 73 20 63  gument pArg is c
5d9e0 61 73 74 20 74 6f 20 61 20 28 76 6f 69 64 20 2a  ast to a (void *
5d9f0 29 20 61 6e 64 20 6d 61 64 65 0a 2a 2a 20 20 20  ) and made.**   
5da00 20 20 61 76 61 69 6c 61 62 6c 65 20 61 73 20 74    available as t
5da10 68 65 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 72  he function user
5da20 2d 64 61 74 61 20 28 73 71 6c 69 74 65 33 5f 75  -data (sqlite3_u
5da30 73 65 72 5f 64 61 74 61 28 29 29 2e 20 54 68 65  ser_data()). The
5da40 0a 2a 2a 20 20 20 20 20 46 75 6e 63 44 65 66 2e  .**     FuncDef.
5da50 66 6c 61 67 73 20 76 61 72 69 61 62 6c 65 20 69  flags variable i
5da60 73 20 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c  s set to the val
5da70 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ue passed as the
5da80 20 66 6c 61 67 73 0a 2a 2a 20 20 20 20 20 70 61   flags.**     pa
5da90 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 23 64 65 66  rameter..*/.#def
5daa0 69 6e 65 20 46 55 4e 43 54 49 4f 4e 28 7a 4e 61  ine FUNCTION(zNa
5dab0 6d 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20  me, nArg, iArg, 
5dac0 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a 20 20  bNC, xFunc) \.  
5dad0 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54  {nArg, SQLITE_UT
5dae0 46 38 2c 20 62 4e 43 2a 53 51 4c 49 54 45 5f 46  F8, bNC*SQLITE_F
5daf0 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20 5c 0a  UNC_NEEDCOLL, \.
5db00 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f     SQLITE_INT_TO
5db10 5f 50 54 52 28 69 41 72 67 29 2c 20 30 2c 20 78  _PTR(iArg), 0, x
5db20 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61  Func, 0, 0, #zNa
5db30 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 53  me, 0}.#define S
5db40 54 52 5f 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d  TR_FUNCTION(zNam
5db50 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c 20 62  e, nArg, pArg, b
5db60 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a 20 20 7b  NC, xFunc) \.  {
5db70 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46  nArg, SQLITE_UTF
5db80 38 2c 20 62 4e 43 2a 53 51 4c 49 54 45 5f 46 55  8, bNC*SQLITE_FU
5db90 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20 5c 0a 20  NC_NEEDCOLL, \. 
5dba0 20 20 70 41 72 67 2c 20 30 2c 20 78 46 75 6e 63    pArg, 0, xFunc
5dbb0 2c 20 30 2c 20 30 2c 20 23 7a 4e 61 6d 65 2c 20  , 0, 0, #zName, 
5dbc0 30 7d 0a 23 64 65 66 69 6e 65 20 4c 49 4b 45 46  0}.#define LIKEF
5dbd0 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c  UNC(zName, nArg,
5dbe0 20 61 72 67 2c 20 66 6c 61 67 73 29 20 5c 0a 20   arg, flags) \. 
5dbf0 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55   {nArg, SQLITE_U
5dc00 54 46 38 2c 20 66 6c 61 67 73 2c 20 28 76 6f 69  TF8, flags, (voi
5dc10 64 20 2a 29 61 72 67 2c 20 30 2c 20 6c 69 6b 65  d *)arg, 0, like
5dc20 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61  Func, 0, 0, #zNa
5dc30 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 41  me, 0}.#define A
5dc40 47 47 52 45 47 41 54 45 28 7a 4e 61 6d 65 2c 20  GGREGATE(zName, 
5dc50 6e 41 72 67 2c 20 61 72 67 2c 20 6e 63 2c 20 78  nArg, arg, nc, x
5dc60 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 20 5c 0a  Step, xFinal) \.
5dc70 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f    {nArg, SQLITE_
5dc80 55 54 46 38 2c 20 6e 63 2a 53 51 4c 49 54 45 5f  UTF8, nc*SQLITE_
5dc90 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c 20 5c  FUNC_NEEDCOLL, \
5dca0 0a 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54  .   SQLITE_INT_T
5dcb0 4f 5f 50 54 52 28 61 72 67 29 2c 20 30 2c 20 30  O_PTR(arg), 0, 0
5dcc0 2c 20 78 53 74 65 70 2c 78 46 69 6e 61 6c 2c 23  , xStep,xFinal,#
5dcd0 7a 4e 61 6d 65 2c 30 7d 0a 0a 2f 2a 0a 2a 2a 20  zName,0}../*.** 
5dce0 41 6c 6c 20 63 75 72 72 65 6e 74 20 73 61 76 65  All current save
5dcf0 70 6f 69 6e 74 73 20 61 72 65 20 73 74 6f 72 65  points are store
5dd00 64 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69  d in a linked li
5dd10 73 74 20 73 74 61 72 74 69 6e 67 20 61 74 0a 2a  st starting at.*
5dd20 2a 20 73 71 6c 69 74 65 33 2e 70 53 61 76 65 70  * sqlite3.pSavep
5dd30 6f 69 6e 74 2e 20 54 68 65 20 66 69 72 73 74 20  oint. The first 
5dd40 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 6c  element in the l
5dd50 69 73 74 20 69 73 20 74 68 65 20 6d 6f 73 74 20  ist is the most 
5dd60 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 6f 70 65 6e  recently.** open
5dd70 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20 53 61  ed savepoint. Sa
5dd80 76 65 70 6f 69 6e 74 73 20 61 72 65 20 61 64 64  vepoints are add
5dd90 65 64 20 74 6f 20 74 68 65 20 6c 69 73 74 20 62  ed to the list b
5dda0 79 20 74 68 65 20 76 64 62 65 0a 2a 2a 20 4f 50  y the vdbe.** OP
5ddb0 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e 73 74 72  _Savepoint instr
5ddc0 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63  uction..*/.struc
5ddd0 74 20 53 61 76 65 70 6f 69 6e 74 20 7b 0a 20 20  t Savepoint {.  
5dde0 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
5ddf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5de00 20 20 20 20 2f 2a 20 53 61 76 65 70 6f 69 6e 74      /* Savepoint
5de10 20 6e 61 6d 65 20 28 6e 75 6c 2d 74 65 72 6d 69   name (nul-termi
5de20 6e 61 74 65 64 29 20 2a 2f 0a 20 20 69 6e 74 20  nated) */.  int 
5de30 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 20 20  nDeferredCons;  
5de40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5de50 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 65 66  /* Number of def
5de60 65 72 72 65 64 20 66 6b 20 76 69 6f 6c 61 74 69  erred fk violati
5de70 6f 6e 73 20 2a 2f 0a 20 20 53 61 76 65 70 6f 69  ons */.  Savepoi
5de80 6e 74 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20  nt *pNext;      
5de90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5dea0 50 61 72 65 6e 74 20 73 61 76 65 70 6f 69 6e 74  Parent savepoint
5deb0 20 28 69 66 20 61 6e 79 29 20 2a 2f 0a 7d 3b 0a   (if any) */.};.
5dec0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
5ded0 77 69 6e 67 20 61 72 65 20 75 73 65 64 20 61 73  wing are used as
5dee0 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   the second para
5def0 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33  meter to sqlite3
5df00 53 61 76 65 70 6f 69 6e 74 28 29 2c 0a 2a 2a 20  Savepoint(),.** 
5df10 61 6e 64 20 61 73 20 74 68 65 20 50 31 20 61 72  and as the P1 ar
5df20 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 4f 50  gument to the OP
5df30 5f 53 61 76 65 70 6f 69 6e 74 20 69 6e 73 74 72  _Savepoint instr
5df40 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69  uction..*/.#defi
5df50 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 42 45 47  ne SAVEPOINT_BEG
5df60 49 4e 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e  IN      0.#defin
5df70 65 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45  e SAVEPOINT_RELE
5df80 41 53 45 20 20 20 20 31 0a 23 64 65 66 69 6e 65  ASE    1.#define
5df90 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42   SAVEPOINT_ROLLB
5dfa0 41 43 4b 20 20 20 32 0a 0a 0a 2f 2a 0a 2a 2a 20  ACK   2.../*.** 
5dfb0 45 61 63 68 20 53 51 4c 69 74 65 20 6d 6f 64 75  Each SQLite modu
5dfc0 6c 65 20 28 76 69 72 74 75 61 6c 20 74 61 62 6c  le (virtual tabl
5dfd0 65 20 64 65 66 69 6e 69 74 69 6f 6e 29 20 69 73  e definition) is
5dfe0 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e 0a 2a   defined by an.*
5dff0 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  * instance of th
5e000 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
5e010 63 74 75 72 65 2c 20 73 74 6f 72 65 64 20 69 6e  cture, stored in
5e020 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 4d 6f   the sqlite3.aMo
5e030 64 75 6c 65 0a 2a 2a 20 68 61 73 68 20 74 61 62  dule.** hash tab
5e040 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 6f  le..*/.struct Mo
5e050 64 75 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73  dule {.  const s
5e060 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
5e070 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 20 2f 2a  Module;       /*
5e080 20 43 61 6c 6c 62 61 63 6b 20 70 6f 69 6e 74 65   Callback pointe
5e090 72 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  rs */.  const ch
5e0a0 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20  ar *zName;      
5e0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5e0c0 4e 61 6d 65 20 70 61 73 73 65 64 20 74 6f 20 63  Name passed to c
5e0d0 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a  reate_module() *
5e0e0 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 3b 20  /.  void *pAux; 
5e0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e100 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 75 78           /* pAux
5e110 20 70 61 73 73 65 64 20 74 6f 20 63 72 65 61 74   passed to creat
5e120 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20  e_module() */.  
5e130 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29  void (*xDestroy)
5e140 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 20 20  (void *);       
5e150 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64       /* Module d
5e160 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69  estructor functi
5e170 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  on */.};../*.** 
5e180 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
5e190 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f 66  t each column of
5e1a0 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 69 73   an SQL table is
5e1b0 20 68 65 6c 64 20 69 6e 20 61 6e 20 69 6e 73 74   held in an inst
5e1c0 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20  ance.** of this 
5e1d0 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74  structure..*/.st
5e1e0 72 75 63 74 20 43 6f 6c 75 6d 6e 20 7b 0a 20 20  ruct Column {.  
5e1f0 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
5e200 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73   /* Name of this
5e210 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78 70   column */.  Exp
5e220 72 20 2a 70 44 66 6c 74 3b 20 20 20 20 20 2f 2a  r *pDflt;     /*
5e230 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f   Default value o
5e240 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f  f this column */
5e250 0a 20 20 63 68 61 72 20 2a 7a 44 66 6c 74 3b 20  .  char *zDflt; 
5e260 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20      /* Original 
5e270 74 65 78 74 20 6f 66 20 74 68 65 20 64 65 66 61  text of the defa
5e280 75 6c 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20 63  ult value */.  c
5e290 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 20 20 20  har *zType;     
5e2a0 2f 2a 20 44 61 74 61 20 74 79 70 65 20 66 6f 72  /* Data type for
5e2b0 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   this column */.
5e2c0 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 20    char *zColl;  
5e2d0 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20     /* Collating 
5e2e0 73 65 71 75 65 6e 63 65 2e 20 20 49 66 20 4e 55  sequence.  If NU
5e2f0 4c 4c 2c 20 75 73 65 20 74 68 65 20 64 65 66 61  LL, use the defa
5e300 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6e 6f 74 4e  ult */.  u8 notN
5e310 75 6c 6c 3b 20 20 20 20 20 20 2f 2a 20 54 72 75  ull;      /* Tru
5e320 65 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  e if there is a 
5e330 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61  NOT NULL constra
5e340 69 6e 74 20 2a 2f 0a 20 20 75 38 20 69 73 50 72  int */.  u8 isPr
5e350 69 6d 4b 65 79 3b 20 20 20 20 2f 2a 20 54 72 75  imKey;    /* Tru
5e360 65 20 69 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e  e if this column
5e370 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   is part of the 
5e380 50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20  PRIMARY KEY */. 
5e390 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20   char affinity; 
5e3a0 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20    /* One of the 
5e3b0 53 51 4c 49 54 45 5f 41 46 46 5f 2e 2e 2e 20 76  SQLITE_AFF_... v
5e3c0 61 6c 75 65 73 20 2a 2f 0a 23 69 66 6e 64 65 66  alues */.#ifndef
5e3d0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
5e3e0 54 55 41 4c 54 41 42 4c 45 0a 20 20 75 38 20 69  TUALTABLE.  u8 i
5e3f0 73 48 69 64 64 65 6e 3b 20 20 20 20 20 2f 2a 20  sHidden;     /* 
5e400 54 72 75 65 20 69 66 20 74 68 69 73 20 63 6f 6c  True if this col
5e410 75 6d 6e 20 69 73 20 27 68 69 64 64 65 6e 27 20  umn is 'hidden' 
5e420 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a  */.#endif.};../*
5e430 0a 2a 2a 20 41 20 22 43 6f 6c 6c 61 74 69 6e 67  .** A "Collating
5e440 20 53 65 71 75 65 6e 63 65 22 20 69 73 20 64 65   Sequence" is de
5e450 66 69 6e 65 64 20 62 79 20 61 6e 20 69 6e 73 74  fined by an inst
5e460 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
5e470 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75  owing.** structu
5e480 72 65 2e 20 43 6f 6e 63 65 70 74 75 61 6c 6c 79  re. Conceptually
5e490 2c 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  , a collating se
5e4a0 71 75 65 6e 63 65 20 63 6f 6e 73 69 73 74 73 20  quence consists 
5e4b0 6f 66 20 61 20 6e 61 6d 65 20 61 6e 64 0a 2a 2a  of a name and.**
5e4c0 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 72 6f   a comparison ro
5e4d0 75 74 69 6e 65 20 74 68 61 74 20 64 65 66 69 6e  utine that defin
5e4e0 65 73 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20  es the order of 
5e4f0 74 68 61 74 20 73 65 71 75 65 6e 63 65 2e 0a 2a  that sequence..*
5e500 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 61 79 20 74  *.** There may t
5e510 77 6f 20 73 65 70 61 72 61 74 65 20 69 6d 70 6c  wo separate impl
5e520 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74  ementations of t
5e530 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e  he collation fun
5e540 63 74 69 6f 6e 2c 20 6f 6e 65 0a 2a 2a 20 74 68  ction, one.** th
5e550 61 74 20 70 72 6f 63 65 73 73 65 73 20 74 65 78  at processes tex
5e560 74 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64  t in UTF-8 encod
5e570 69 6e 67 20 28 43 6f 6c 6c 53 65 71 2e 78 43 6d  ing (CollSeq.xCm
5e580 70 29 20 61 6e 64 20 61 6e 6f 74 68 65 72 20 74  p) and another t
5e590 68 61 74 0a 2a 2a 20 70 72 6f 63 65 73 73 65 73  hat.** processes
5e5a0 20 74 65 78 74 20 65 6e 63 6f 64 65 64 20 69 6e   text encoded in
5e5b0 20 55 54 46 2d 31 36 20 28 43 6f 6c 6c 53 65 71   UTF-16 (CollSeq
5e5c0 2e 78 43 6d 70 31 36 29 2c 20 75 73 69 6e 67 20  .xCmp16), using 
5e5d0 74 68 65 20 6d 61 63 68 69 6e 65 0a 2a 2a 20 6e  the machine.** n
5e5e0 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72  ative byte order
5e5f0 2e 20 57 68 65 6e 20 61 20 63 6f 6c 6c 61 74 69  . When a collati
5e600 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 69  on sequence is i
5e610 6e 76 6f 6b 65 64 2c 20 53 51 4c 69 74 65 20 73  nvoked, SQLite s
5e620 65 6c 65 63 74 73 0a 2a 2a 20 74 68 65 20 76 65  elects.** the ve
5e630 72 73 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20  rsion that will 
5e640 72 65 71 75 69 72 65 20 74 68 65 20 6c 65 61 73  require the leas
5e650 74 20 65 78 70 65 6e 73 69 76 65 20 65 6e 63 6f  t expensive enco
5e660 64 69 6e 67 0a 2a 2a 20 74 72 61 6e 73 6c 61 74  ding.** translat
5e670 69 6f 6e 73 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a  ions, if any..**
5e680 0a 2a 2a 20 54 68 65 20 43 6f 6c 6c 53 65 71 2e  .** The CollSeq.
5e690 70 55 73 65 72 20 6d 65 6d 62 65 72 20 76 61 72  pUser member var
5e6a0 69 61 62 6c 65 20 69 73 20 61 6e 20 65 78 74 72  iable is an extr
5e6b0 61 20 70 61 72 61 6d 65 74 65 72 20 74 68 61 74  a parameter that
5e6c0 20 70 61 73 73 65 64 20 69 6e 0a 2a 2a 20 61 73   passed in.** as
5e6d0 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
5e6e0 65 6e 74 20 74 6f 20 74 68 65 20 55 54 46 2d 38  ent to the UTF-8
5e6f0 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63   comparison func
5e700 74 69 6f 6e 2c 20 78 43 6d 70 2e 0a 2a 2a 20 43  tion, xCmp..** C
5e710 6f 6c 6c 53 65 71 2e 70 55 73 65 72 31 36 20 69  ollSeq.pUser16 i
5e720 73 20 74 68 65 20 65 71 75 69 76 61 6c 65 6e 74  s the equivalent
5e730 20 66 6f 72 20 74 68 65 20 55 54 46 2d 31 36 20   for the UTF-16 
5e740 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74  comparison funct
5e750 69 6f 6e 2c 0a 2a 2a 20 78 43 6d 70 31 36 2e 0a  ion,.** xCmp16..
5e760 2a 2a 0a 2a 2a 20 49 66 20 62 6f 74 68 20 43 6f  **.** If both Co
5e770 6c 6c 53 65 71 2e 78 43 6d 70 20 61 6e 64 20 43  llSeq.xCmp and C
5e780 6f 6c 6c 53 65 71 2e 78 43 6d 70 31 36 20 61 72  ollSeq.xCmp16 ar
5e790 65 20 4e 55 4c 4c 2c 20 69 74 20 6d 65 61 6e 73  e NULL, it means
5e7a0 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 63 6f 6c   that the.** col
5e7b0 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
5e7c0 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 49  is undefined.  I
5e7d0 6e 64 69 63 65 73 20 62 75 69 6c 74 20 6f 6e 20  ndices built on 
5e7e0 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20  an undefined.** 
5e7f0 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
5e800 63 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 72 65  ce may not be re
5e810 61 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 0a 2a  ad or written..*
5e820 2f 0a 73 74 72 75 63 74 20 43 6f 6c 6c 53 65 71  /.struct CollSeq
5e830 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65   {.  char *zName
5e840 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  ;          /* Na
5e850 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74  me of the collat
5e860 69 6e 67 20 73 65 71 75 65 6e 63 65 2c 20 55 54  ing sequence, UT
5e870 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20  F-8 encoded */. 
5e880 20 75 38 20 65 6e 63 3b 20 20 20 20 20 20 20 20   u8 enc;        
5e890 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 65         /* Text e
5e8a0 6e 63 6f 64 69 6e 67 20 68 61 6e 64 6c 65 64 20  ncoding handled 
5e8b0 62 79 20 78 43 6d 70 28 29 20 2a 2f 0a 20 20 75  by xCmp() */.  u
5e8c0 38 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20  8 type;         
5e8d0 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74       /* One of t
5e8e0 68 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 2e  he SQLITE_COLL_.
5e8f0 2e 2e 20 76 61 6c 75 65 73 20 62 65 6c 6f 77 20  .. values below 
5e900 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72  */.  void *pUser
5e910 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69  ;          /* Fi
5e920 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
5e930 78 43 6d 70 28 29 20 2a 2f 0a 20 20 69 6e 74 20  xCmp() */.  int 
5e940 28 2a 78 43 6d 70 29 28 76 6f 69 64 2a 2c 69 6e  (*xCmp)(void*,in
5e950 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  t, const void*, 
5e960 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  int, const void*
5e970 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c  );.  void (*xDel
5e980 29 28 76 6f 69 64 2a 29 3b 20 20 2f 2a 20 44 65  )(void*);  /* De
5e990 73 74 72 75 63 74 6f 72 20 66 6f 72 20 70 55 73  structor for pUs
5e9a0 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  er */.};../*.** 
5e9b0 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f  Allowed values o
5e9c0 66 20 43 6f 6c 6c 53 65 71 2e 74 79 70 65 3a 0a  f CollSeq.type:.
5e9d0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
5e9e0 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 20 20 31  E_COLL_BINARY  1
5e9f0 20 20 2f 2a 20 54 68 65 20 64 65 66 61 75 6c 74    /* The default
5ea00 20 6d 65 6d 63 6d 70 28 29 20 63 6f 6c 6c 61 74   memcmp() collat
5ea10 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a  ing sequence */.
5ea20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
5ea30 4f 4c 4c 5f 4e 4f 43 41 53 45 20 20 32 20 20 2f  OLL_NOCASE  2  /
5ea40 2a 20 54 68 65 20 62 75 69 6c 74 2d 69 6e 20 4e  * The built-in N
5ea50 4f 43 41 53 45 20 63 6f 6c 6c 61 74 69 6e 67 20  OCASE collating 
5ea60 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66  sequence */.#def
5ea70 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f  ine SQLITE_COLL_
5ea80 52 45 56 45 52 53 45 20 33 20 20 2f 2a 20 54 68  REVERSE 3  /* Th
5ea90 65 20 62 75 69 6c 74 2d 69 6e 20 52 45 56 45 52  e built-in REVER
5eaa0 53 45 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  SE collating seq
5eab0 75 65 6e 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65  uence */.#define
5eac0 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 55 53 45   SQLITE_COLL_USE
5ead0 52 20 20 20 20 30 20 20 2f 2a 20 41 6e 79 20 6f  R    0  /* Any o
5eae0 74 68 65 72 20 75 73 65 72 2d 64 65 66 69 6e 65  ther user-define
5eaf0 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  d collating sequ
5eb00 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  ence */../*.** A
5eb10 20 73 6f 72 74 20 6f 72 64 65 72 20 63 61 6e 20   sort order can 
5eb20 62 65 20 65 69 74 68 65 72 20 41 53 43 20 6f 72  be either ASC or
5eb30 20 44 45 53 43 2e 0a 2a 2f 0a 23 64 65 66 69 6e   DESC..*/.#defin
5eb40 65 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20  e SQLITE_SO_ASC 
5eb50 20 20 20 20 20 20 30 20 20 2f 2a 20 53 6f 72 74        0  /* Sort
5eb60 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72   in ascending or
5eb70 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  der */.#define S
5eb80 51 4c 49 54 45 5f 53 4f 5f 44 45 53 43 20 20 20  QLITE_SO_DESC   
5eb90 20 20 20 31 20 20 2f 2a 20 53 6f 72 74 20 69 6e     1  /* Sort in
5eba0 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72   ascending order
5ebb0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c 75 6d   */../*.** Colum
5ebc0 6e 20 61 66 66 69 6e 69 74 79 20 74 79 70 65 73  n affinity types
5ebd0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 75 73  ..**.** These us
5ebe0 65 64 20 74 6f 20 68 61 76 65 20 6d 6e 65 6d 6f  ed to have mnemo
5ebf0 6e 69 63 20 6e 61 6d 65 20 6c 69 6b 65 20 27 69  nic name like 'i
5ec00 27 20 66 6f 72 20 53 51 4c 49 54 45 5f 41 46 46  ' for SQLITE_AFF
5ec10 5f 49 4e 54 45 47 45 52 20 61 6e 64 0a 2a 2a 20  _INTEGER and.** 
5ec20 27 74 27 20 66 6f 72 20 53 51 4c 49 54 45 5f 41  't' for SQLITE_A
5ec30 46 46 5f 54 45 58 54 2e 20 20 42 75 74 20 77 65  FF_TEXT.  But we
5ec40 20 63 61 6e 20 73 61 76 65 20 61 20 6c 69 74 74   can save a litt
5ec50 6c 65 20 73 70 61 63 65 20 61 6e 64 20 69 6d 70  le space and imp
5ec60 72 6f 76 65 0a 2a 2a 20 74 68 65 20 73 70 65 65  rove.** the spee
5ec70 64 20 61 20 6c 69 74 74 6c 65 20 62 79 20 6e 75  d a little by nu
5ec80 6d 62 65 72 69 6e 67 20 74 68 65 20 76 61 6c 75  mbering the valu
5ec90 65 73 20 63 6f 6e 73 65 63 75 74 69 76 65 6c 79  es consecutively
5eca0 2e 20 20 0a 2a 2a 0a 2a 2a 20 42 75 74 20 72 61  .  .**.** But ra
5ecb0 74 68 65 72 20 74 68 61 6e 20 73 74 61 72 74 20  ther than start 
5ecc0 77 69 74 68 20 30 20 6f 72 20 31 2c 20 77 65 20  with 0 or 1, we 
5ecd0 62 65 67 69 6e 20 77 69 74 68 20 27 61 27 2e 20  begin with 'a'. 
5ece0 20 54 68 61 74 20 77 61 79 2c 0a 2a 2a 20 77 68   That way,.** wh
5ecf0 65 6e 20 6d 75 6c 74 69 70 6c 65 20 61 66 66 69  en multiple affi
5ed00 6e 69 74 79 20 74 79 70 65 73 20 61 72 65 20 63  nity types are c
5ed10 6f 6e 63 61 74 65 6e 61 74 65 64 20 69 6e 74 6f  oncatenated into
5ed20 20 61 20 73 74 72 69 6e 67 20 61 6e 64 0a 2a 2a   a string and.**
5ed30 20 75 73 65 64 20 61 73 20 74 68 65 20 50 34 20   used as the P4 
5ed40 6f 70 65 72 61 6e 64 2c 20 74 68 65 79 20 77 69  operand, they wi
5ed50 6c 6c 20 62 65 20 6d 6f 72 65 20 72 65 61 64 61  ll be more reada
5ed60 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  ble..**.** Note 
5ed70 61 6c 73 6f 20 74 68 61 74 20 74 68 65 20 6e 75  also that the nu
5ed80 6d 65 72 69 63 20 74 79 70 65 73 20 61 72 65 20  meric types are 
5ed90 67 72 6f 75 70 65 64 20 74 6f 67 65 74 68 65 72  grouped together
5eda0 20 73 6f 20 74 68 61 74 20 74 65 73 74 69 6e 67   so that testing
5edb0 0a 2a 2a 20 66 6f 72 20 61 20 6e 75 6d 65 72 69  .** for a numeri
5edc0 63 20 74 79 70 65 20 69 73 20 61 20 73 69 6e 67  c type is a sing
5edd0 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 0a 2a  le comparison..*
5ede0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5edf0 5f 41 46 46 5f 54 45 58 54 20 20 20 20 20 27 61  _AFF_TEXT     'a
5ee00 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  '.#define SQLITE
5ee10 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20 20 27 62  _AFF_NONE     'b
5ee20 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  '.#define SQLITE
5ee30 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 20 27 63  _AFF_NUMERIC  'c
5ee40 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  '.#define SQLITE
5ee50 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 20 27 64  _AFF_INTEGER  'd
5ee60 27 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  '.#define SQLITE
5ee70 5f 41 46 46 5f 52 45 41 4c 20 20 20 20 20 27 65  _AFF_REAL     'e
5ee80 27 0a 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  '..#define sqlit
5ee90 65 33 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e  e3IsNumericAffin
5eea0 69 74 79 28 58 29 20 20 28 28 58 29 3e 3d 53 51  ity(X)  ((X)>=SQ
5eeb0 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
5eec0 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c  )../*.** The SQL
5eed0 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 76 61 6c  ITE_AFF_MASK val
5eee0 75 65 73 20 6d 61 73 6b 73 20 6f 66 66 20 74 68  ues masks off th
5eef0 65 20 73 69 67 6e 69 66 69 63 61 6e 74 20 62 69  e significant bi
5ef00 74 73 20 6f 66 20 61 6e 0a 2a 2a 20 61 66 66 69  ts of an.** affi
5ef10 6e 69 74 79 20 76 61 6c 75 65 2e 20 0a 2a 2f 0a  nity value. .*/.
5ef20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
5ef30 46 46 5f 4d 41 53 4b 20 20 20 20 20 30 78 36 37  FF_MASK     0x67
5ef40 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 69 74 69 6f 6e  ../*.** Addition
5ef50 61 6c 20 62 69 74 20 76 61 6c 75 65 73 20 74 68  al bit values th
5ef60 61 74 20 63 61 6e 20 62 65 20 4f 52 65 64 20 77  at can be ORed w
5ef70 69 74 68 20 61 6e 20 61 66 66 69 6e 69 74 79 20  ith an affinity 
5ef80 77 69 74 68 6f 75 74 0a 2a 2a 20 63 68 61 6e 67  without.** chang
5ef90 69 6e 67 20 74 68 65 20 61 66 66 69 6e 69 74 79  ing the affinity
5efa0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
5efb0 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 20  ITE_JUMPIFNULL  
5efc0 20 30 78 30 38 20 20 2f 2a 20 6a 75 6d 70 73 20   0x08  /* jumps 
5efd0 69 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e  if either operan
5efe0 64 20 69 73 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65  d is NULL */.#de
5eff0 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4f 52  fine SQLITE_STOR
5f000 45 50 32 20 20 20 20 20 20 30 78 31 30 20 20 2f  EP2      0x10  /
5f010 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 69  * Store result i
5f020 6e 20 72 65 67 5b 50 32 5d 20 72 61 74 68 65 72  n reg[P2] rather
5f030 20 74 68 61 6e 20 6a 75 6d 70 20 2a 2f 0a 23 64   than jump */.#d
5f040 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 55 4c  efine SQLITE_NUL
5f050 4c 45 51 20 20 20 20 20 20 20 30 78 38 30 20 20  LEQ       0x80  
5f060 2f 2a 20 4e 55 4c 4c 3d 4e 55 4c 4c 20 2a 2f 0a  /* NULL=NULL */.
5f070 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74  ./*.** An object
5f080 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 69 73   of this type is
5f090 20 63 72 65 61 74 65 64 20 66 6f 72 20 65 61 63   created for eac
5f0a0 68 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  h virtual table 
5f0b0 70 72 65 73 65 6e 74 20 69 6e 0a 2a 2a 20 74 68  present in.** th
5f0c0 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
5f0d0 61 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  a. .**.** If the
5f0e0 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
5f0f0 20 69 73 20 73 68 61 72 65 64 2c 20 74 68 65 6e   is shared, then
5f100 20 74 68 65 72 65 20 69 73 20 6f 6e 65 20 69 6e   there is one in
5f110 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 0a 2a  stance of this.*
5f120 2a 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20  * structure for 
5f130 65 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f  each database co
5f140 6e 6e 65 63 74 69 6f 6e 20 28 73 71 6c 69 74 65  nnection (sqlite
5f150 33 2a 29 20 74 68 61 74 20 75 73 65 73 20 74 68  3*) that uses th
5f160 65 20 73 68 61 72 65 64 0a 2a 2a 20 73 63 68 65  e shared.** sche
5f170 6d 61 2e 20 54 68 69 73 20 69 73 20 62 65 63 61  ma. This is beca
5f180 75 73 65 20 65 61 63 68 20 64 61 74 61 62 61 73  use each databas
5f190 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 72 65 71  e connection req
5f1a0 75 69 72 65 73 20 69 74 73 20 6f 77 6e 20 75 6e  uires its own un
5f1b0 69 71 75 65 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  ique.** instance
5f1c0 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
5f1d0 76 74 61 62 2a 20 68 61 6e 64 6c 65 20 75 73 65  vtab* handle use
5f1e0 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20  d to access the 
5f1f0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 0a 2a  virtual table .*
5f200 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
5f210 2e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 20  . sqlite3_vtab* 
5f220 68 61 6e 64 6c 65 73 20 63 61 6e 20 6e 6f 74 20  handles can not 
5f230 62 65 20 73 68 61 72 65 64 20 62 65 74 77 65 65  be shared betwee
5f240 6e 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  n .** database c
5f250 6f 6e 6e 65 63 74 69 6f 6e 73 2c 20 65 76 65 6e  onnections, even
5f260 20 77 68 65 6e 20 74 68 65 20 72 65 73 74 20 6f   when the rest o
5f270 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  f the in-memory 
5f280 64 61 74 61 62 61 73 65 20 0a 2a 2a 20 73 63 68  database .** sch
5f290 65 6d 61 20 69 73 20 73 68 61 72 65 64 2c 20 61  ema is shared, a
5f2a0 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  s the implementa
5f2b0 74 69 6f 6e 20 6f 66 74 65 6e 20 73 74 6f 72 65  tion often store
5f2c0 73 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a  s the database.*
5f2d0 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e  * connection han
5f2e0 64 6c 65 20 70 61 73 73 65 64 20 74 6f 20 69 74  dle passed to it
5f2f0 20 76 69 61 20 74 68 65 20 78 43 6f 6e 6e 65 63   via the xConnec
5f300 74 28 29 20 6f 72 20 78 43 72 65 61 74 65 28 29  t() or xCreate()
5f310 20 6d 65 74 68 6f 64 0a 2a 2a 20 64 75 72 69 6e   method.** durin
5f320 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  g initialization
5f330 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 54 68 69   internally. Thi
5f340 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  s database conne
5f350 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 6d 61 79  ction handle may
5f360 0a 2a 2a 20 74 68 65 6e 20 75 73 65 64 20 62 79  .** then used by
5f370 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
5f380 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le implementatio
5f390 6e 20 74 6f 20 61 63 63 65 73 73 20 72 65 61 6c  n to access real
5f3a0 20 74 61 62 6c 65 73 20 0a 2a 2a 20 77 69 74 68   tables .** with
5f3b0 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  in the database.
5f3c0 20 53 6f 20 74 68 61 74 20 74 68 65 79 20 61 70   So that they ap
5f3d0 70 65 61 72 20 61 73 20 70 61 72 74 20 6f 66 20  pear as part of 
5f3e0 74 68 65 20 63 61 6c 6c 65 72 73 20 0a 2a 2a 20  the callers .** 
5f3f0 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65  transaction, the
5f400 73 65 20 61 63 63 65 73 73 65 73 20 6e 65 65 64  se accesses need
5f410 20 74 6f 20 62 65 20 6d 61 64 65 20 76 69 61 20   to be made via 
5f420 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
5f430 65 20 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  e .** connection
5f440 20 61 73 20 74 68 61 74 20 75 73 65 64 20 74 6f   as that used to
5f450 20 65 78 65 63 75 74 65 20 53 51 4c 20 6f 70 65   execute SQL ope
5f460 72 61 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20 76  rations on the v
5f470 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a  irtual table..**
5f480 0a 2a 2a 20 41 6c 6c 20 56 54 61 62 6c 65 20 6f  .** All VTable o
5f490 62 6a 65 63 74 73 20 74 68 61 74 20 63 6f 72 72  bjects that corr
5f4a0 65 73 70 6f 6e 64 20 74 6f 20 61 20 73 69 6e 67  espond to a sing
5f4b0 6c 65 20 74 61 62 6c 65 20 69 6e 20 61 20 73 68  le table in a sh
5f4c0 61 72 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65  ared.** database
5f4d0 20 73 63 68 65 6d 61 20 61 72 65 20 69 6e 69 74   schema are init
5f4e0 69 61 6c 6c 79 20 73 74 6f 72 65 64 20 69 6e 20  ially stored in 
5f4f0 61 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 70 6f  a linked-list po
5f500 69 6e 74 65 64 20 74 6f 20 62 79 0a 2a 2a 20 74  inted to by.** t
5f510 68 65 20 54 61 62 6c 65 2e 70 56 54 61 62 6c 65  he Table.pVTable
5f520 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c 65   member variable
5f530 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f   of the correspo
5f540 6e 64 69 6e 67 20 54 61 62 6c 65 20 6f 62 6a 65  nding Table obje
5f550 63 74 2e 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 73  ct..** When an s
5f560 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29  qlite3_prepare()
5f570 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 72 65   operation is re
5f580 71 75 69 72 65 64 20 74 6f 20 61 63 63 65 73 73  quired to access
5f590 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20   the virtual.** 
5f5a0 74 61 62 6c 65 2c 20 69 74 20 73 65 61 72 63 68  table, it search
5f5b0 65 73 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20  es the list for 
5f5c0 74 68 65 20 56 54 61 62 6c 65 20 74 68 61 74 20  the VTable that 
5f5d0 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74  corresponds to t
5f5e0 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  he.** database c
5f5f0 6f 6e 6e 65 63 74 69 6f 6e 20 64 6f 69 6e 67 20  onnection doing 
5f600 74 68 65 20 70 72 65 70 61 72 69 6e 67 20 73 6f  the preparing so
5f610 20 61 73 20 74 6f 20 75 73 65 20 74 68 65 20 63   as to use the c
5f620 6f 72 72 65 63 74 0a 2a 2a 20 73 71 6c 69 74 65  orrect.** sqlite
5f630 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 20 69  3_vtab* handle i
5f640 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 71  n the compiled q
5f650 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  uery..**.** When
5f660 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 54 61   an in-memory Ta
5f670 62 6c 65 20 6f 62 6a 65 63 74 20 69 73 20 64 65  ble object is de
5f680 6c 65 74 65 64 20 28 66 6f 72 20 65 78 61 6d 70  leted (for examp
5f690 6c 65 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 73  le when the.** s
5f6a0 63 68 65 6d 61 20 69 73 20 62 65 69 6e 67 20 72  chema is being r
5f6b0 65 6c 6f 61 64 65 64 20 66 6f 72 20 73 6f 6d 65  eloaded for some
5f6c0 20 72 65 61 73 6f 6e 29 2c 20 74 68 65 20 56 54   reason), the VT
5f6d0 61 62 6c 65 20 6f 62 6a 65 63 74 73 20 61 72 65  able objects are
5f6e0 20 6e 6f 74 20 0a 2a 2a 20 64 65 6c 65 74 65 64   not .** deleted
5f6f0 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33   and the sqlite3
5f700 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65 73 20 61  _vtab* handles a
5f710 72 65 20 6e 6f 74 20 78 44 69 73 63 6f 6e 6e 65  re not xDisconne
5f720 63 74 28 29 65 64 20 0a 2a 2a 20 69 6d 6d 65 64  ct()ed .** immed
5f730 69 61 74 65 6c 79 2e 20 49 6e 73 74 65 61 64 2c  iately. Instead,
5f740 20 74 68 65 79 20 61 72 65 20 6d 6f 76 65 64 20   they are moved 
5f750 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 2e 70  from the Table.p
5f760 56 54 61 62 6c 65 20 6c 69 73 74 20 74 6f 0a 2a  VTable list to.*
5f770 2a 20 61 6e 6f 74 68 65 72 20 6c 69 6e 6b 65 64  * another linked
5f780 20 6c 69 73 74 20 68 65 61 64 65 64 20 62 79 20   list headed by 
5f790 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69 73  the sqlite3.pDis
5f7a0 63 6f 6e 6e 65 63 74 20 6d 65 6d 62 65 72 20 6f  connect member o
5f7b0 66 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70  f the.** corresp
5f7c0 6f 6e 64 69 6e 67 20 73 71 6c 69 74 65 33 20 73  onding sqlite3 s
5f7d0 74 72 75 63 74 75 72 65 2e 20 54 68 65 79 20 61  tructure. They a
5f7e0 72 65 20 74 68 65 6e 20 64 65 6c 65 74 65 64 2f  re then deleted/
5f7f0 78 44 69 73 63 6f 6e 6e 65 63 74 65 64 20 0a 2a  xDisconnected .*
5f800 2a 20 6e 65 78 74 20 74 69 6d 65 20 61 20 73 74  * next time a st
5f810 61 74 65 6d 65 6e 74 20 69 73 20 70 72 65 70 61  atement is prepa
5f820 72 65 64 20 75 73 69 6e 67 20 73 61 69 64 20 73  red using said s
5f830 71 6c 69 74 65 33 2a 2e 20 54 68 69 73 20 69 73  qlite3*. This is
5f840 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20 61 76 6f 69   done.** to avoi
5f850 64 20 64 65 61 64 6c 6f 63 6b 20 69 73 73 75 65  d deadlock issue
5f860 73 20 69 6e 76 6f 6c 76 69 6e 67 20 6d 75 6c 74  s involving mult
5f870 69 70 6c 65 20 73 71 6c 69 74 65 33 2e 6d 75 74  iple sqlite3.mut
5f880 65 78 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 20 52  ex mutexes..** R
5f890 65 66 65 72 20 74 6f 20 63 6f 6d 6d 65 6e 74 73  efer to comments
5f8a0 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f 6e 20   above function 
5f8b0 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63  sqlite3VtabUnloc
5f8c0 6b 4c 69 73 74 28 29 20 66 6f 72 20 61 6e 0a 2a  kList() for an.*
5f8d0 2a 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20 61 73  * explanation as
5f8e0 20 74 6f 20 77 68 79 20 69 74 20 69 73 20 73 61   to why it is sa
5f8f0 66 65 20 74 6f 20 61 64 64 20 61 6e 20 65 6e 74  fe to add an ent
5f900 72 79 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33  ry to an sqlite3
5f910 2e 70 44 69 73 63 6f 6e 6e 65 63 74 0a 2a 2a 20  .pDisconnect.** 
5f920 6c 69 73 74 20 77 69 74 68 6f 75 74 20 68 6f 6c  list without hol
5f930 64 69 6e 67 20 74 68 65 20 63 6f 72 72 65 73 70  ding the corresp
5f940 6f 6e 64 69 6e 67 20 73 71 6c 69 74 65 33 2e 6d  onding sqlite3.m
5f950 75 74 65 78 20 6d 75 74 65 78 2e 0a 2a 2a 0a 2a  utex mutex..**.*
5f960 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 66 6f 72  * The memory for
5f970 20 6f 62 6a 65 63 74 73 20 6f 66 20 74 68 69 73   objects of this
5f980 20 74 79 70 65 20 69 73 20 61 6c 77 61 79 73 20   type is always 
5f990 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 0a 2a 2a  allocated by .**
5f9a0 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
5f9b0 28 29 2c 20 75 73 69 6e 67 20 74 68 65 20 63 6f  (), using the co
5f9c0 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20  nnection handle 
5f9d0 73 74 6f 72 65 64 20 69 6e 20 56 54 61 62 6c 65  stored in VTable
5f9e0 2e 64 62 20 61 73 20 0a 2a 2a 20 74 68 65 20 66  .db as .** the f
5f9f0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a  irst argument..*
5fa00 2f 0a 73 74 72 75 63 74 20 56 54 61 62 6c 65 20  /.struct VTable 
5fa10 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
5fa20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5fa30 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
5fa40 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20  tion associated 
5fa50 77 69 74 68 20 74 68 69 73 20 74 61 62 6c 65 20  with this table 
5fa60 2a 2f 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f  */.  Module *pMo
5fa70 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  d;             /
5fa80 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6d 6f 64  * Pointer to mod
5fa90 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ule implementati
5faa0 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  on */.  sqlite3_
5fab0 76 74 61 62 20 2a 70 56 74 61 62 3b 20 20 20 20  vtab *pVtab;    
5fac0 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
5fad0 76 74 61 62 20 69 6e 73 74 61 6e 63 65 20 2a 2f  vtab instance */
5fae0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20  .  int nRef;    
5faf0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5fb00 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65  Number of pointe
5fb10 72 73 20 74 6f 20 74 68 69 73 20 73 74 72 75 63  rs to this struc
5fb20 74 75 72 65 20 2a 2f 0a 20 20 56 54 61 62 6c 65  ture */.  VTable
5fb30 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20   *pNext;        
5fb40 20 20 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 6c      /* Next in l
5fb50 69 6e 6b 65 64 20 6c 69 73 74 20 28 73 65 65 20  inked list (see 
5fb60 61 62 6f 76 65 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  above) */.};../*
5fb70 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 74 61 62  .** Each SQL tab
5fb80 6c 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 65  le is represente
5fb90 64 20 69 6e 20 6d 65 6d 6f 72 79 20 62 79 20 61  d in memory by a
5fba0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
5fbb0 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73  e.** following s
5fbc0 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
5fbd0 54 61 62 6c 65 2e 7a 4e 61 6d 65 20 69 73 20 74  Table.zName is t
5fbe0 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
5fbf0 61 62 6c 65 2e 20 20 54 68 65 20 63 61 73 65 20  able.  The case 
5fc00 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a  of the original.
5fc10 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  ** CREATE TABLE 
5fc20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 73 74 6f  statement is sto
5fc30 72 65 64 2c 20 62 75 74 20 63 61 73 65 20 69 73  red, but case is
5fc40 20 6e 6f 74 20 73 69 67 6e 69 66 69 63 61 6e 74   not significant
5fc50 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 61 72 69 73   for.** comparis
5fc60 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 61 62 6c 65  ons..**.** Table
5fc70 2e 6e 43 6f 6c 20 69 73 20 74 68 65 20 6e 75 6d  .nCol is the num
5fc80 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
5fc90 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20 20 54  n this table.  T
5fca0 61 62 6c 65 2e 61 43 6f 6c 20 69 73 20 61 0a 2a  able.aCol is a.*
5fcb0 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  * pointer to an 
5fcc0 61 72 72 61 79 20 6f 66 20 43 6f 6c 75 6d 6e 20  array of Column 
5fcd0 73 74 72 75 63 74 75 72 65 73 2c 20 6f 6e 65 20  structures, one 
5fce0 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e  for each column.
5fcf0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 74 61  .**.** If the ta
5fd00 62 6c 65 20 68 61 73 20 61 6e 20 49 4e 54 45 47  ble has an INTEG
5fd10 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20  ER PRIMARY KEY, 
5fd20 74 68 65 6e 20 54 61 62 6c 65 2e 69 50 4b 65 79  then Table.iPKey
5fd30 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66   is the index of
5fd40 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74  .** the column t
5fd50 68 61 74 20 69 73 20 74 68 61 74 20 6b 65 79 2e  hat is that key.
5fd60 20 20 20 4f 74 68 65 72 77 69 73 65 20 54 61 62     Otherwise Tab
5fd70 6c 65 2e 69 50 4b 65 79 20 69 73 20 6e 65 67 61  le.iPKey is nega
5fd80 74 69 76 65 2e 20 20 4e 6f 74 65 0a 2a 2a 20 74  tive.  Note.** t
5fd90 68 61 74 20 74 68 65 20 64 61 74 61 74 79 70 65  hat the datatype
5fda0 20 6f 66 20 74 68 65 20 50 52 49 4d 41 52 59 20   of the PRIMARY 
5fdb0 4b 45 59 20 6d 75 73 74 20 62 65 20 49 4e 54 45  KEY must be INTE
5fdc0 47 45 52 20 66 6f 72 20 74 68 69 73 20 66 69 65  GER for this fie
5fdd0 6c 64 20 74 6f 0a 2a 2a 20 62 65 20 73 65 74 2e  ld to.** be set.
5fde0 20 20 41 6e 20 49 4e 54 45 47 45 52 20 50 52 49    An INTEGER PRI
5fdf0 4d 41 52 59 20 4b 45 59 20 69 73 20 75 73 65 64  MARY KEY is used
5fe00 20 61 73 20 74 68 65 20 72 6f 77 69 64 20 66 6f   as the rowid fo
5fe10 72 20 65 61 63 68 20 72 6f 77 20 6f 66 0a 2a 2a  r each row of.**
5fe20 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20   the table.  If 
5fe30 61 20 74 61 62 6c 65 20 68 61 73 20 6e 6f 20 49  a table has no I
5fe40 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
5fe50 45 59 2c 20 74 68 65 6e 20 61 20 72 61 6e 64 6f  EY, then a rando
5fe60 6d 20 72 6f 77 69 64 0a 2a 2a 20 69 73 20 67 65  m rowid.** is ge
5fe70 6e 65 72 61 74 65 64 20 66 6f 72 20 65 61 63 68  nerated for each
5fe80 20 72 6f 77 20 6f 66 20 74 68 65 20 74 61 62 6c   row of the tabl
5fe90 65 2e 20 20 54 46 5f 48 61 73 50 72 69 6d 61 72  e.  TF_HasPrimar
5fea0 79 4b 65 79 20 69 73 20 73 65 74 20 69 66 0a 2a  yKey is set if.*
5feb0 2a 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20  * the table has 
5fec0 61 6e 79 20 50 52 49 4d 41 52 59 20 4b 45 59 2c  any PRIMARY KEY,
5fed0 20 49 4e 54 45 47 45 52 20 6f 72 20 6f 74 68 65   INTEGER or othe
5fee0 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 61 62  rwise..**.** Tab
5fef0 6c 65 2e 74 6e 75 6d 20 69 73 20 74 68 65 20 70  le.tnum is the p
5ff00 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  age number for t
5ff10 68 65 20 72 6f 6f 74 20 42 54 72 65 65 20 70 61  he root BTree pa
5ff20 67 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ge of the table 
5ff30 69 6e 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  in the.** databa
5ff40 73 65 20 66 69 6c 65 2e 20 20 49 66 20 54 61 62  se file.  If Tab
5ff50 6c 65 2e 69 44 62 20 69 73 20 74 68 65 20 69 6e  le.iDb is the in
5ff60 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62  dex of the datab
5ff70 61 73 65 20 74 61 62 6c 65 20 62 61 63 6b 65 6e  ase table backen
5ff80 64 0a 2a 2a 20 69 6e 20 73 71 6c 69 74 65 2e 61  d.** in sqlite.a
5ff90 44 62 5b 5d 2e 20 20 30 20 69 73 20 66 6f 72 20  Db[].  0 is for 
5ffa0 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
5ffb0 65 20 61 6e 64 20 31 20 69 73 20 66 6f 72 20 74  e and 1 is for t
5ffc0 68 65 20 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20  he file that.** 
5ffd0 68 6f 6c 64 73 20 74 65 6d 70 6f 72 61 72 79 20  holds temporary 
5ffe0 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63  tables and indic
5fff0 65 73 2e 20 20 49 66 20 54 46 5f 45 70 68 65 6d  es.  If TF_Ephem
60000 65 72 61 6c 20 69 73 20 73 65 74 0a 2a 2a 20 74  eral is set.** t
60010 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 69 73  hen the table is
60020 20 73 74 6f 72 65 64 20 69 6e 20 61 20 66 69 6c   stored in a fil
60030 65 20 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61  e that is automa
60040 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 0a  tically deleted.
60050 2a 2a 20 77 68 65 6e 20 74 68 65 20 56 44 42 45  ** when the VDBE
60060 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 74   cursor to the t
60070 61 62 6c 65 20 69 73 20 63 6c 6f 73 65 64 2e 20  able is closed. 
60080 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 54 61   In this case Ta
60090 62 6c 65 2e 74 6e 75 6d 20 0a 2a 2a 20 72 65 66  ble.tnum .** ref
600a0 65 72 73 20 56 44 42 45 20 63 75 72 73 6f 72 20  ers VDBE cursor 
600b0 6e 75 6d 62 65 72 20 74 68 61 74 20 68 6f 6c 64  number that hold
600c0 73 20 74 68 65 20 74 61 62 6c 65 20 6f 70 65 6e  s the table open
600d0 2c 20 6e 6f 74 20 74 6f 20 74 68 65 20 72 6f 6f  , not to the roo
600e0 74 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72  t.** page number
600f0 2e 20 20 54 72 61 6e 73 69 65 6e 74 20 74 61 62  .  Transient tab
60100 6c 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20  les are used to 
60110 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 73  hold the results
60120 20 6f 66 20 61 0a 2a 2a 20 73 75 62 2d 71 75 65   of a.** sub-que
60130 72 79 20 74 68 61 74 20 61 70 70 65 61 72 73 20  ry that appears 
60140 69 6e 73 74 65 61 64 20 6f 66 20 61 20 72 65 61  instead of a rea
60150 6c 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20  l table name in 
60160 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
60170 0a 2a 2a 20 6f 66 20 61 20 53 45 4c 45 43 54 20  .** of a SELECT 
60180 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74  statement..*/.st
60190 72 75 63 74 20 54 61 62 6c 65 20 7b 0a 20 20 73  ruct Table {.  s
601a0 71 6c 69 74 65 33 20 2a 64 62 4d 65 6d 3b 20 20  qlite3 *dbMem;  
601b0 20 20 20 20 2f 2a 20 44 42 20 63 6f 6e 6e 65 63      /* DB connec
601c0 74 69 6f 6e 20 75 73 65 64 20 66 6f 72 20 6c 6f  tion used for lo
601d0 6f 6b 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 69  okaside allocati
601e0 6f 6e 73 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ons. */.  char *
601f0 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 2f  zName;         /
60200 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  * Name of the ta
60210 62 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20  ble or view */. 
60220 20 69 6e 74 20 69 50 4b 65 79 3b 20 20 20 20 20   int iPKey;     
60230 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74 20        /* If not 
60240 6e 65 67 61 74 69 76 65 2c 20 75 73 65 20 61 43  negative, use aC
60250 6f 6c 5b 69 50 4b 65 79 5d 20 61 73 20 74 68 65  ol[iPKey] as the
60260 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a   primary key */.
60270 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20    int nCol;     
60280 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
60290 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
602a0 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 43  his table */.  C
602b0 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 3b 20 20 20 20  olumn *aCol;    
602c0 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69      /* Informati
602d0 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20 63 6f  on about each co
602e0 6c 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 20  lumn */.  Index 
602f0 2a 70 49 6e 64 65 78 3b 20 20 20 20 20 20 20 2f  *pIndex;       /
60300 2a 20 4c 69 73 74 20 6f 66 20 53 51 4c 20 69 6e  * List of SQL in
60310 64 65 78 65 73 20 6f 6e 20 74 68 69 73 20 74 61  dexes on this ta
60320 62 6c 65 2e 20 2a 2f 0a 20 20 69 6e 74 20 74 6e  ble. */.  int tn
60330 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  um;            /
60340 2a 20 52 6f 6f 74 20 42 54 72 65 65 20 6e 6f 64  * Root BTree nod
60350 65 20 66 6f 72 20 74 68 69 73 20 74 61 62 6c 65  e for this table
60360 20 28 73 65 65 20 6e 6f 74 65 20 61 62 6f 76 65   (see note above
60370 29 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  ) */.  Select *p
60380 53 65 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20 4e  Select;     /* N
60390 55 4c 4c 20 66 6f 72 20 74 61 62 6c 65 73 2e 20  ULL for tables. 
603a0 20 50 6f 69 6e 74 73 20 74 6f 20 64 65 66 69 6e   Points to defin
603b0 69 74 69 6f 6e 20 69 66 20 61 20 76 69 65 77 2e  ition if a view.
603c0 20 2a 2f 0a 20 20 75 31 36 20 6e 52 65 66 3b 20   */.  u16 nRef; 
603d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
603e0 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73  mber of pointers
603f0 20 74 6f 20 74 68 69 73 20 54 61 62 6c 65 20 2a   to this Table *
60400 2f 0a 20 20 75 38 20 74 61 62 46 6c 61 67 73 3b  /.  u8 tabFlags;
60410 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b           /* Mask
60420 20 6f 66 20 54 46 5f 2a 20 76 61 6c 75 65 73 20   of TF_* values 
60430 2a 2f 0a 20 20 75 38 20 6b 65 79 43 6f 6e 66 3b  */.  u8 keyConf;
60440 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 61            /* Wha
60450 74 20 74 6f 20 64 6f 20 69 6e 20 63 61 73 65 20  t to do in case 
60460 6f 66 20 75 6e 69 71 75 65 6e 65 73 73 20 63 6f  of uniqueness co
60470 6e 66 6c 69 63 74 20 6f 6e 20 69 50 4b 65 79 20  nflict on iPKey 
60480 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79  */.  FKey *pFKey
60490 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6e  ;         /* Lin
604a0 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  ked list of all 
604b0 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 69 6e 20  foreign keys in 
604c0 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20  this table */.  
604d0 63 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 20 20  char *zColAff;  
604e0 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 64       /* String d
604f0 65 66 69 6e 69 6e 67 20 74 68 65 20 61 66 66 69  efining the affi
60500 6e 69 74 79 20 6f 66 20 65 61 63 68 20 63 6f 6c  nity of each col
60510 75 6d 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  umn */.#ifndef S
60520 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b  QLITE_OMIT_CHECK
60530 0a 20 20 45 78 70 72 20 2a 70 43 68 65 63 6b 3b  .  Expr *pCheck;
60540 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 41          /* The A
60550 4e 44 20 6f 66 20 61 6c 6c 20 43 48 45 43 4b 20  ND of all CHECK 
60560 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 23  constraints */.#
60570 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51  endif.#ifndef SQ
60580 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54  LITE_OMIT_ALTERT
60590 41 42 4c 45 0a 20 20 69 6e 74 20 61 64 64 43 6f  ABLE.  int addCo
605a0 6c 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 4f  lOffset;    /* O
605b0 66 66 73 65 74 20 69 6e 20 43 52 45 41 54 45 20  ffset in CREATE 
605c0 54 41 42 4c 45 20 73 74 6d 74 20 74 6f 20 61 64  TABLE stmt to ad
605d0 64 20 61 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a  d a new column *
605e0 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  /.#endif.#ifndef
605f0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
60600 54 55 41 4c 54 41 42 4c 45 0a 20 20 56 54 61 62  TUALTABLE.  VTab
60610 6c 65 20 2a 70 56 54 61 62 6c 65 3b 20 20 20 20  le *pVTable;    
60620 20 2f 2a 20 4c 69 73 74 20 6f 66 20 56 54 61 62   /* List of VTab
60630 6c 65 20 6f 62 6a 65 63 74 73 2e 20 2a 2f 0a 20  le objects. */. 
60640 20 69 6e 74 20 6e 4d 6f 64 75 6c 65 41 72 67 3b   int nModuleArg;
60650 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
60660 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  of arguments to 
60670 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20  the module */.  
60680 63 68 61 72 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41  char **azModuleA
60690 72 67 3b 20 20 2f 2a 20 54 65 78 74 20 6f 66 20  rg;  /* Text of 
606a0 61 6c 6c 20 6d 6f 64 75 6c 65 20 61 72 67 73 2e  all module args.
606b0 20 5b 30 5d 20 69 73 20 6d 6f 64 75 6c 65 20 6e   [0] is module n
606c0 61 6d 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  ame */.#endif.  
606d0 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
606e0 72 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20  r;   /* List of 
606f0 74 72 69 67 67 65 72 73 20 73 74 6f 72 65 64 20  triggers stored 
60700 69 6e 20 70 53 63 68 65 6d 61 20 2a 2f 0a 20 20  in pSchema */.  
60710 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b  Schema *pSchema;
60720 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 74       /* Schema t
60730 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 69  hat contains thi
60740 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54 61 62  s table */.  Tab
60750 6c 65 20 2a 70 4e 65 78 74 5a 6f 6d 62 69 65 3b  le *pNextZombie;
60760 20 20 2f 2a 20 4e 65 78 74 20 6f 6e 20 74 68 65    /* Next on the
60770 20 50 61 72 73 65 2e 70 5a 6f 6d 62 69 65 54 61   Parse.pZombieTa
60780 62 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  b list */.};../*
60790 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75  .** Allowed valu
607a0 65 73 20 66 6f 72 20 54 61 62 65 2e 74 61 62 46  es for Tabe.tabF
607b0 6c 61 67 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  lags..*/.#define
607c0 20 54 46 5f 52 65 61 64 6f 6e 6c 79 20 20 20 20   TF_Readonly    
607d0 20 20 20 20 30 78 30 31 20 20 20 20 2f 2a 20 52      0x01    /* R
607e0 65 61 64 2d 6f 6e 6c 79 20 73 79 73 74 65 6d 20  ead-only system 
607f0 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65  table */.#define
60800 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 20 20 20   TF_Ephemeral   
60810 20 20 20 20 30 78 30 32 20 20 20 20 2f 2a 20 41      0x02    /* A
60820 6e 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  n ephemeral tabl
60830 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f  e */.#define TF_
60840 48 61 73 50 72 69 6d 61 72 79 4b 65 79 20 20 20  HasPrimaryKey   
60850 30 78 30 34 20 20 20 20 2f 2a 20 54 61 62 6c 65  0x04    /* Table
60860 20 68 61 73 20 61 20 70 72 69 6d 61 72 79 20 6b   has a primary k
60870 65 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46  ey */.#define TF
60880 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 20  _Autoincrement  
60890 20 30 78 30 38 20 20 20 20 2f 2a 20 49 6e 74 65   0x08    /* Inte
608a0 67 65 72 20 70 72 69 6d 61 72 79 20 6b 65 79 20  ger primary key 
608b0 69 73 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74  is autoincrement
608c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 56   */.#define TF_V
608d0 69 72 74 75 61 6c 20 20 20 20 20 20 20 20 20 30  irtual         0
608e0 78 31 30 20 20 20 20 2f 2a 20 49 73 20 61 20 76  x10    /* Is a v
608f0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a  irtual table */.
60900 23 64 65 66 69 6e 65 20 54 46 5f 4e 65 65 64 4d  #define TF_NeedM
60910 65 74 61 64 61 74 61 20 20 20 20 30 78 32 30 20  etadata    0x20 
60920 20 20 20 2f 2a 20 61 43 6f 6c 5b 5d 2e 7a 54 79     /* aCol[].zTy
60930 70 65 20 61 6e 64 20 61 43 6f 6c 5b 5d 2e 70 43  pe and aCol[].pC
60940 6f 6c 6c 20 6d 69 73 73 69 6e 67 20 2a 2f 0a 0a  oll missing */..
60950 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 74 6f 20  ../*.** Test to 
60960 73 65 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e  see whether or n
60970 6f 74 20 61 20 74 61 62 6c 65 20 69 73 20 61 20  ot a table is a 
60980 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20  virtual table.  
60990 54 68 69 73 20 69 73 0a 2a 2a 20 64 6f 6e 65 20  This is.** done 
609a0 61 73 20 61 20 6d 61 63 72 6f 20 73 6f 20 74 68  as a macro so th
609b0 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 6f 70  at it will be op
609c0 74 69 6d 69 7a 65 64 20 6f 75 74 20 77 68 65 6e  timized out when
609d0 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c   virtual.** tabl
609e0 65 20 73 75 70 70 6f 72 74 20 69 73 20 6f 6d 69  e support is omi
609f0 74 74 65 64 20 66 72 6f 6d 20 74 68 65 20 62 75  tted from the bu
60a00 69 6c 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ild..*/.#ifndef 
60a10 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
60a20 55 41 4c 54 41 42 4c 45 0a 23 20 20 64 65 66 69  UALTABLE.#  defi
60a30 6e 65 20 49 73 56 69 72 74 75 61 6c 28 58 29 20  ne IsVirtual(X) 
60a40 20 20 20 20 20 28 28 28 58 29 2d 3e 74 61 62 46       (((X)->tabF
60a50 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74 75 61  lags & TF_Virtua
60a60 6c 29 21 3d 30 29 0a 23 20 20 64 65 66 69 6e 65  l)!=0).#  define
60a70 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28   IsHiddenColumn(
60a80 58 29 20 28 28 58 29 2d 3e 69 73 48 69 64 64 65  X) ((X)->isHidde
60a90 6e 29 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69  n).#else.#  defi
60aa0 6e 65 20 49 73 56 69 72 74 75 61 6c 28 58 29 20  ne IsVirtual(X) 
60ab0 20 20 20 20 20 30 0a 23 20 20 64 65 66 69 6e 65       0.#  define
60ac0 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28   IsHiddenColumn(
60ad0 58 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  X) 0.#endif../*.
60ae0 2a 2a 20 45 61 63 68 20 66 6f 72 65 69 67 6e 20  ** Each foreign 
60af0 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 69  key constraint i
60b00 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  s an instance of
60b10 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
60b20 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
60b30 41 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73  A foreign key is
60b40 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
60b50 20 74 77 6f 20 74 61 62 6c 65 73 2e 20 20 54 68   two tables.  Th
60b60 65 20 22 66 72 6f 6d 22 20 74 61 62 6c 65 20 69  e "from" table i
60b70 73 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 74  s.** the table t
60b80 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  hat contains the
60b90 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75   REFERENCES clau
60ba0 73 65 20 74 68 61 74 20 63 72 65 61 74 65 73 20  se that creates 
60bb0 74 68 65 20 66 6f 72 65 69 67 6e 0a 2a 2a 20 6b  the foreign.** k
60bc0 65 79 2e 20 20 54 68 65 20 22 74 6f 22 20 74 61  ey.  The "to" ta
60bd0 62 6c 65 20 69 73 20 74 68 65 20 74 61 62 6c 65  ble is the table
60be0 20 74 68 61 74 20 69 73 20 6e 61 6d 65 64 20 69   that is named i
60bf0 6e 20 74 68 65 20 52 45 46 45 52 45 4e 43 45 53  n the REFERENCES
60c00 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 43 6f 6e 73   clause..** Cons
60c10 69 64 65 72 20 74 68 69 73 20 65 78 61 6d 70 6c  ider this exampl
60c20 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45  e:.**.**     CRE
60c30 41 54 45 20 54 41 42 4c 45 20 65 78 31 28 0a 2a  ATE TABLE ex1(.*
60c40 2a 20 20 20 20 20 20 20 61 20 49 4e 54 45 47 45  *       a INTEGE
60c50 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 0a 2a  R PRIMARY KEY,.*
60c60 2a 20 20 20 20 20 20 20 62 20 49 4e 54 45 47 45  *       b INTEGE
60c70 52 20 43 4f 4e 53 54 52 41 49 4e 54 20 66 6b 31  R CONSTRAINT fk1
60c80 20 52 45 46 45 52 45 4e 43 45 53 20 65 78 32 28   REFERENCES ex2(
60c90 78 29 0a 2a 2a 20 20 20 20 20 29 3b 0a 2a 2a 0a  x).**     );.**.
60ca0 2a 2a 20 46 6f 72 20 66 6f 72 65 69 67 6e 20 6b  ** For foreign k
60cb0 65 79 20 22 66 6b 31 22 2c 20 74 68 65 20 66 72  ey "fk1", the fr
60cc0 6f 6d 2d 74 61 62 6c 65 20 69 73 20 22 65 78 31  om-table is "ex1
60cd0 22 20 61 6e 64 20 74 68 65 20 74 6f 2d 74 61 62  " and the to-tab
60ce0 6c 65 20 69 73 20 22 65 78 32 22 2e 0a 2a 2a 0a  le is "ex2"..**.
60cf0 2a 2a 20 45 61 63 68 20 52 45 46 45 52 45 4e 43  ** Each REFERENC
60d00 45 53 20 63 6c 61 75 73 65 20 67 65 6e 65 72 61  ES clause genera
60d10 74 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20  tes an instance 
60d20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
60d30 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 77 68   structure.** wh
60d40 69 63 68 20 69 73 20 61 74 74 61 63 68 65 64 20  ich is attached 
60d50 74 6f 20 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c  to the from-tabl
60d60 65 2e 20 20 54 68 65 20 74 6f 2d 74 61 62 6c 65  e.  The to-table
60d70 20 6e 65 65 64 20 6e 6f 74 20 65 78 69 73 74 20   need not exist 
60d80 77 68 65 6e 0a 2a 2a 20 74 68 65 20 66 72 6f 6d  when.** the from
60d90 2d 74 61 62 6c 65 20 69 73 20 63 72 65 61 74 65  -table is create
60da0 64 2e 20 20 54 68 65 20 65 78 69 73 74 65 6e 63  d.  The existenc
60db0 65 20 6f 66 20 74 68 65 20 74 6f 2d 74 61 62 6c  e of the to-tabl
60dc0 65 20 69 73 20 6e 6f 74 20 63 68 65 63 6b 65 64  e is not checked
60dd0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 4b 65 79  ..*/.struct FKey
60de0 20 7b 0a 20 20 54 61 62 6c 65 20 2a 70 46 72 6f   {.  Table *pFro
60df0 6d 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20  m;     /* Table 
60e00 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 52  containing the R
60e10 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65  EFERENCES clause
60e20 20 28 61 6b 61 3a 20 43 68 69 6c 64 29 20 2a 2f   (aka: Child) */
60e30 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 46 72  .  FKey *pNextFr
60e40 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 20 66 6f 72  om;  /* Next for
60e50 65 69 67 6e 20 6b 65 79 20 69 6e 20 70 46 72 6f  eign key in pFro
60e60 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 6f  m */.  char *zTo
60e70 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ;        /* Name
60e80 20 6f 66 20 74 61 62 6c 65 20 74 68 61 74 20 74   of table that t
60e90 68 65 20 6b 65 79 20 70 6f 69 6e 74 73 20 74 6f  he key points to
60ea0 20 28 61 6b 61 3a 20 50 61 72 65 6e 74 29 20 2a   (aka: Parent) *
60eb0 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 54  /.  FKey *pNextT
60ec0 6f 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 66 6f  o;    /* Next fo
60ed0 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74 61 62  reign key on tab
60ee0 6c 65 20 6e 61 6d 65 64 20 7a 54 6f 20 2a 2f 0a  le named zTo */.
60ef0 20 20 46 4b 65 79 20 2a 70 50 72 65 76 54 6f 3b    FKey *pPrevTo;
60f00 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20      /* Previous 
60f10 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74  foreign key on t
60f20 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 6f 20 2a  able named zTo *
60f30 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20  /.  int nCol;   
60f40 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
60f50 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
60f60 69 73 20 6b 65 79 20 2a 2f 0a 20 20 2f 2a 20 45  is key */.  /* E
60f70 56 3a 20 52 2d 33 30 33 32 33 2d 32 31 39 31 37  V: R-30323-21917
60f80 20 2a 2f 0a 20 20 75 38 20 69 73 44 65 66 65 72   */.  u8 isDefer
60f90 72 65 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20  red;    /* True 
60fa0 69 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68  if constraint ch
60fb0 65 63 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72  ecking is deferr
60fc0 65 64 20 74 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a  ed till COMMIT *
60fd0 2f 0a 20 20 75 38 20 61 41 63 74 69 6f 6e 5b 32  /.  u8 aAction[2
60fe0 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  ];          /* O
60ff0 4e 20 44 45 4c 45 54 45 20 61 6e 64 20 4f 4e 20  N DELETE and ON 
61000 55 50 44 41 54 45 20 61 63 74 69 6f 6e 73 2c 20  UPDATE actions, 
61010 72 65 73 70 65 63 74 69 76 65 6c 79 20 2a 2f 0a  respectively */.
61020 20 20 54 72 69 67 67 65 72 20 2a 61 70 54 72 69    Trigger *apTri
61030 67 67 65 72 5b 32 5d 3b 20 20 2f 2a 20 54 72 69  gger[2];  /* Tri
61040 67 67 65 72 73 20 66 6f 72 20 61 41 63 74 69 6f  ggers for aActio
61050 6e 5b 5d 20 61 63 74 69 6f 6e 73 20 2a 2f 0a 20  n[] actions */. 
61060 20 73 74 72 75 63 74 20 73 43 6f 6c 4d 61 70 20   struct sColMap 
61070 7b 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 6f 66  {  /* Mapping of
61080 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 46 72 6f   columns in pFro
61090 6d 20 74 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  m to columns in 
610a0 7a 54 6f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  zTo */.    int i
610b0 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a  From;         /*
610c0 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e   Index of column
610d0 20 69 6e 20 70 46 72 6f 6d 20 2a 2f 0a 20 20 20   in pFrom */.   
610e0 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20   char *zCol;    
610f0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63      /* Name of c
61100 6f 6c 75 6d 6e 20 69 6e 20 7a 54 6f 2e 20 20 49  olumn in zTo.  I
61110 66 20 30 20 75 73 65 20 50 52 49 4d 41 52 59 20  f 0 use PRIMARY 
61120 4b 45 59 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 5b  KEY */.  } aCol[
61130 31 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e  1];        /* On
61140 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68  e entry for each
61150 20 6f 66 20 6e 43 6f 6c 20 63 6f 6c 75 6d 6e 20   of nCol column 
61160 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53  s */.};../*.** S
61170 51 4c 69 74 65 20 73 75 70 70 6f 72 74 73 20 6d  QLite supports m
61180 61 6e 79 20 64 69 66 66 65 72 65 6e 74 20 77 61  any different wa
61190 79 73 20 74 6f 20 72 65 73 6f 6c 76 65 20 61 20  ys to resolve a 
611a0 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72  constraint.** er
611b0 72 6f 72 2e 20 20 52 4f 4c 4c 42 41 43 4b 20 70  ror.  ROLLBACK p
611c0 72 6f 63 65 73 73 69 6e 67 20 6d 65 61 6e 73 20  rocessing means 
611d0 74 68 61 74 20 61 20 63 6f 6e 73 74 72 61 69 6e  that a constrain
611e0 74 20 76 69 6f 6c 61 74 69 6f 6e 0a 2a 2a 20 63  t violation.** c
611f0 61 75 73 65 73 20 74 68 65 20 6f 70 65 72 61 74  auses the operat
61200 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 20 74  ion in process t
61210 6f 20 66 61 69 6c 20 61 6e 64 20 66 6f 72 20 74  o fail and for t
61220 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  he current trans
61230 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20  action.** to be 
61240 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41 42  rolled back.  AB
61250 4f 52 54 20 70 72 6f 63 65 73 73 69 6e 67 20 6d  ORT processing m
61260 65 61 6e 73 20 74 68 65 20 6f 70 65 72 61 74 69  eans the operati
61270 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 0a 2a 2a  on in process.**
61280 20 66 61 69 6c 73 20 61 6e 64 20 61 6e 79 20 70   fails and any p
61290 72 69 6f 72 20 63 68 61 6e 67 65 73 20 66 72 6f  rior changes fro
612a0 6d 20 74 68 61 74 20 6f 6e 65 20 6f 70 65 72 61  m that one opera
612b0 74 69 6f 6e 20 61 72 65 20 62 61 63 6b 65 64 20  tion are backed 
612c0 6f 75 74 2c 0a 2a 2a 20 62 75 74 20 74 68 65 20  out,.** but the 
612d0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e  transaction is n
612e0 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20  ot rolled back. 
612f0 20 46 41 49 4c 20 70 72 6f 63 65 73 73 69 6e 67   FAIL processing
61300 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74   means that.** t
61310 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e 20  he operation in 
61320 70 72 6f 67 72 65 73 73 20 73 74 6f 70 73 20 61  progress stops a
61330 6e 64 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72  nd returns an er
61340 72 6f 72 20 63 6f 64 65 2e 20 20 42 75 74 20 70  ror code.  But p
61350 72 69 6f 72 0a 2a 2a 20 63 68 61 6e 67 65 73 20  rior.** changes 
61360 64 75 65 20 74 6f 20 74 68 65 20 73 61 6d 65 20  due to the same 
61370 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 20 6e 6f  operation are no
61380 74 20 62 61 63 6b 65 64 20 6f 75 74 20 61 6e 64  t backed out and
61390 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20   no rollback.** 
613a0 6f 63 63 75 72 73 2e 20 20 49 47 4e 4f 52 45 20  occurs.  IGNORE 
613b0 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 70  means that the p
613c0 61 72 74 69 63 75 6c 61 72 20 72 6f 77 20 74 68  articular row th
613d0 61 74 20 63 61 75 73 65 64 20 74 68 65 20 63 6f  at caused the co
613e0 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72 6f  nstraint.** erro
613f0 72 20 69 73 20 6e 6f 74 20 69 6e 73 65 72 74 65  r is not inserte
61400 64 20 6f 72 20 75 70 64 61 74 65 64 2e 20 20 50  d or updated.  P
61410 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e  rocessing contin
61420 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72  ues and no error
61430 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e  .** is returned.
61440 20 20 52 45 50 4c 41 43 45 20 6d 65 61 6e 73 20    REPLACE means 
61450 74 68 61 74 20 70 72 65 65 78 69 73 74 69 6e 67  that preexisting
61460 20 64 61 74 61 62 61 73 65 20 72 6f 77 73 20 74   database rows t
61470 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 61 20  hat caused.** a 
61480 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e  UNIQUE constrain
61490 74 20 76 69 6f 6c 61 74 69 6f 6e 20 61 72 65 20  t violation are 
614a0 72 65 6d 6f 76 65 64 20 73 6f 20 74 68 61 74 20  removed so that 
614b0 74 68 65 20 6e 65 77 20 69 6e 73 65 72 74 20 6f  the new insert o
614c0 72 0a 2a 2a 20 75 70 64 61 74 65 20 63 61 6e 20  r.** update can 
614d0 70 72 6f 63 65 65 64 2e 20 20 50 72 6f 63 65 73  proceed.  Proces
614e0 73 69 6e 67 20 63 6f 6e 74 69 6e 75 65 73 20 61  sing continues a
614f0 6e 64 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 72  nd no error is r
61500 65 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52  eported..**.** R
61510 45 53 54 52 49 43 54 2c 20 53 45 54 4e 55 4c 4c  ESTRICT, SETNULL
61520 2c 20 61 6e 64 20 43 41 53 43 41 44 45 20 61 63  , and CASCADE ac
61530 74 69 6f 6e 73 20 61 70 70 6c 79 20 6f 6e 6c 79  tions apply only
61540 20 74 6f 20 66 6f 72 65 69 67 6e 20 6b 65 79 73   to foreign keys
61550 2e 0a 2a 2a 20 52 45 53 54 52 49 43 54 20 69 73  ..** RESTRICT is
61560 20 74 68 65 20 73 61 6d 65 20 61 73 20 41 42 4f   the same as ABO
61570 52 54 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 45  RT for IMMEDIATE
61580 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 6e   foreign keys an
61590 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61 73  d the.** same as
615a0 20 52 4f 4c 4c 42 41 43 4b 20 66 6f 72 20 44 45   ROLLBACK for DE
615b0 46 45 52 52 45 44 20 6b 65 79 73 2e 20 20 53 45  FERRED keys.  SE
615c0 54 4e 55 4c 4c 20 6d 65 61 6e 73 20 74 68 61 74  TNULL means that
615d0 20 74 68 65 20 66 6f 72 65 69 67 6e 0a 2a 2a 20   the foreign.** 
615e0 6b 65 79 20 69 73 20 73 65 74 20 74 6f 20 4e 55  key is set to NU
615f0 4c 4c 2e 20 20 43 41 53 43 41 44 45 20 6d 65 61  LL.  CASCADE mea
61600 6e 73 20 74 68 61 74 20 61 20 44 45 4c 45 54 45  ns that a DELETE
61610 20 6f 72 20 55 50 44 41 54 45 20 6f 66 20 74 68   or UPDATE of th
61620 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 64 20  e.** referenced 
61630 74 61 62 6c 65 20 72 6f 77 20 69 73 20 70 72 6f  table row is pro
61640 70 61 67 61 74 65 64 20 69 6e 74 6f 20 74 68 65  pagated into the
61650 20 72 6f 77 20 74 68 61 74 20 68 6f 6c 64 73 20   row that holds 
61660 74 68 65 0a 2a 2a 20 66 6f 72 65 69 67 6e 20 6b  the.** foreign k
61670 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 66  ey..** .** The f
61680 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d 62 6f 6c 69  ollowing symboli
61690 63 20 76 61 6c 75 65 73 20 61 72 65 20 75 73 65  c values are use
616a0 64 20 74 6f 20 72 65 63 6f 72 64 20 77 68 69 63  d to record whic
616b0 68 20 74 79 70 65 0a 2a 2a 20 6f 66 20 61 63 74  h type.** of act
616c0 69 6f 6e 20 74 6f 20 74 61 6b 65 2e 0a 2a 2f 0a  ion to take..*/.
616d0 23 64 65 66 69 6e 65 20 4f 45 5f 4e 6f 6e 65 20  #define OE_None 
616e0 20 20 20 20 30 20 20 20 2f 2a 20 54 68 65 72 65      0   /* There
616f0 20 69 73 20 6e 6f 20 63 6f 6e 73 74 72 61 69 6e   is no constrain
61700 74 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 23 64  t to check */.#d
61710 65 66 69 6e 65 20 4f 45 5f 52 6f 6c 6c 62 61 63  efine OE_Rollbac
61720 6b 20 31 20 20 20 2f 2a 20 46 61 69 6c 20 74 68  k 1   /* Fail th
61730 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64 20  e operation and 
61740 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61  rollback the tra
61750 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66  nsaction */.#def
61760 69 6e 65 20 4f 45 5f 41 62 6f 72 74 20 20 20 20  ine OE_Abort    
61770 32 20 20 20 2f 2a 20 42 61 63 6b 20 6f 75 74 20  2   /* Back out 
61780 63 68 61 6e 67 65 73 20 62 75 74 20 64 6f 20 6e  changes but do n
61790 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73  o rollback trans
617a0 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e  action */.#defin
617b0 65 20 4f 45 5f 46 61 69 6c 20 20 20 20 20 33 20  e OE_Fail     3 
617c0 20 20 2f 2a 20 53 74 6f 70 20 74 68 65 20 6f 70    /* Stop the op
617d0 65 72 61 74 69 6f 6e 20 62 75 74 20 6c 65 61 76  eration but leav
617e0 65 20 61 6c 6c 20 70 72 69 6f 72 20 63 68 61 6e  e all prior chan
617f0 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  ges */.#define O
61800 45 5f 49 67 6e 6f 72 65 20 20 20 34 20 20 20 2f  E_Ignore   4   /
61810 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 65 72 72  * Ignore the err
61820 6f 72 2e 20 44 6f 20 6e 6f 74 20 64 6f 20 74 68  or. Do not do th
61830 65 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41  e INSERT or UPDA
61840 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45  TE */.#define OE
61850 5f 52 65 70 6c 61 63 65 20 20 35 20 20 20 2f 2a  _Replace  5   /*
61860 20 44 65 6c 65 74 65 20 65 78 69 73 74 69 6e 67   Delete existing
61870 20 72 65 63 6f 72 64 2c 20 74 68 65 6e 20 64 6f   record, then do
61880 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41 54   INSERT or UPDAT
61890 45 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 4f 45  E */..#define OE
618a0 5f 52 65 73 74 72 69 63 74 20 36 20 20 20 2f 2a  _Restrict 6   /*
618b0 20 4f 45 5f 41 62 6f 72 74 20 66 6f 72 20 49 4d   OE_Abort for IM
618c0 4d 45 44 49 41 54 45 2c 20 4f 45 5f 52 6f 6c 6c  MEDIATE, OE_Roll
618d0 62 61 63 6b 20 66 6f 72 20 44 45 46 45 52 52 45  back for DEFERRE
618e0 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f  D */.#define OE_
618f0 53 65 74 4e 75 6c 6c 20 20 37 20 20 20 2f 2a 20  SetNull  7   /* 
61900 53 65 74 20 74 68 65 20 66 6f 72 65 69 67 6e 20  Set the foreign 
61910 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 4e 55 4c  key value to NUL
61920 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f  L */.#define OE_
61930 53 65 74 44 66 6c 74 20 20 38 20 20 20 2f 2a 20  SetDflt  8   /* 
61940 53 65 74 20 74 68 65 20 66 6f 72 65 69 67 6e 20  Set the foreign 
61950 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 69 74 73  key value to its
61960 20 64 65 66 61 75 6c 74 20 2a 2f 0a 23 64 65 66   default */.#def
61970 69 6e 65 20 4f 45 5f 43 61 73 63 61 64 65 20 20  ine OE_Cascade  
61980 39 20 20 20 2f 2a 20 43 61 73 63 61 64 65 20 74  9   /* Cascade t
61990 68 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 0a 23  he changes */..#
619a0 64 65 66 69 6e 65 20 4f 45 5f 44 65 66 61 75 6c  define OE_Defaul
619b0 74 20 20 39 39 20 20 2f 2a 20 44 6f 20 77 68 61  t  99  /* Do wha
619c0 74 65 76 65 72 20 74 68 65 20 64 65 66 61 75 6c  tever the defaul
619d0 74 20 61 63 74 69 6f 6e 20 69 73 20 2a 2f 0a 0a  t action is */..
619e0 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
619f0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
61a00 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73  ing structure is
61a10 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66   passed as the f
61a20 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  irst.** argument
61a30 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65 4b   to sqlite3VdbeK
61a40 65 79 43 6f 6d 70 61 72 65 20 61 6e 64 20 69 73  eyCompare and is
61a50 20 75 73 65 64 20 74 6f 20 63 6f 6e 74 72 6f 6c   used to control
61a60 20 74 68 65 20 0a 2a 2a 20 63 6f 6d 70 61 72 69   the .** compari
61a70 73 6f 6e 20 6f 66 20 74 68 65 20 74 77 6f 20 69  son of the two i
61a80 6e 64 65 78 20 6b 65 79 73 2e 0a 2a 2f 0a 73 74  ndex keys..*/.st
61a90 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 7b 0a 20  ruct KeyInfo {. 
61aa0 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
61ab0 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
61ac0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
61ad0 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20  */.  u8 enc;    
61ae0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74           /* Text
61af0 20 65 6e 63 6f 64 69 6e 67 20 2d 20 6f 6e 65 20   encoding - one 
61b00 6f 66 20 74 68 65 20 54 45 58 54 5f 55 74 66 2a  of the TEXT_Utf*
61b10 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 31 36   values */.  u16
61b20 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20   nField;        
61b30 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
61b40 74 72 69 65 73 20 69 6e 20 61 43 6f 6c 6c 5b 5d  tries in aColl[]
61b50 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72 74 4f   */.  u8 *aSortO
61b60 72 64 65 72 3b 20 20 20 20 20 2f 2a 20 49 66 20  rder;     /* If 
61b70 64 65 66 69 6e 65 64 20 61 6e 20 61 53 6f 72 74  defined an aSort
61b80 4f 72 64 65 72 5b 69 5d 20 69 73 20 74 72 75 65  Order[i] is true
61b90 2c 20 73 6f 72 74 20 44 45 53 43 20 2a 2f 0a 20  , sort DESC */. 
61ba0 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f 6c 6c 5b   CollSeq *aColl[
61bb0 31 5d 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e  1];  /* Collatin
61bc0 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65  g sequence for e
61bd0 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ach term of the 
61be0 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  key */.};../*.**
61bf0 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
61c00 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
61c10 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e  ructure holds in
61c20 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
61c30 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 69 6e 64 65  a.** single inde
61c40 78 20 72 65 63 6f 72 64 20 74 68 61 74 20 68 61  x record that ha
61c50 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70  s already been p
61c60 61 72 73 65 64 20 6f 75 74 20 69 6e 74 6f 20 69  arsed out into i
61c70 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20 76 61 6c  ndividual.** val
61c80 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 63  ues..**.** A rec
61c90 6f 72 64 20 69 73 20 61 6e 20 6f 62 6a 65 63 74  ord is an object
61ca0 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 6f   that contains o
61cb0 6e 65 20 6f 72 20 6d 6f 72 65 20 66 69 65 6c 64  ne or more field
61cc0 73 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 52 65  s of data..** Re
61cd0 63 6f 72 64 73 20 61 72 65 20 75 73 65 64 20 74  cords are used t
61ce0 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6e 74  o store the cont
61cf0 65 6e 74 20 6f 66 20 61 20 74 61 62 6c 65 20 72  ent of a table r
61d00 6f 77 20 61 6e 64 20 74 6f 20 73 74 6f 72 65 0a  ow and to store.
61d10 2a 2a 20 74 68 65 20 6b 65 79 20 6f 66 20 61 6e  ** the key of an
61d20 20 69 6e 64 65 78 2e 20 20 41 20 62 6c 6f 62 20   index.  A blob 
61d30 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 72 65  encoding of a re
61d40 63 6f 72 64 20 69 73 20 63 72 65 61 74 65 64 20  cord is created 
61d50 62 79 0a 2a 2a 20 74 68 65 20 4f 50 5f 4d 61 6b  by.** the OP_Mak
61d60 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f  eRecord opcode o
61d70 66 20 74 68 65 20 56 44 42 45 20 61 6e 64 20 69  f the VDBE and i
61d80 73 20 64 69 73 61 73 73 65 6d 62 6c 65 64 20 62  s disassembled b
61d90 79 20 74 68 65 0a 2a 2a 20 4f 50 5f 43 6f 6c 75  y the.** OP_Colu
61da0 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a  mn opcode..**.**
61db0 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
61dc0 68 6f 6c 64 73 20 61 20 72 65 63 6f 72 64 20 74  holds a record t
61dd0 68 61 74 20 68 61 73 20 61 6c 72 65 61 64 79 20  hat has already 
61de0 62 65 65 6e 20 64 69 73 61 73 73 65 6d 62 6c 65  been disassemble
61df0 64 0a 2a 2a 20 69 6e 74 6f 20 69 74 73 20 63 6f  d.** into its co
61e00 6e 73 74 69 74 75 65 6e 74 20 66 69 65 6c 64 73  nstituent fields
61e10 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 55 6e 70 61  ..*/.struct Unpa
61e20 63 6b 65 64 52 65 63 6f 72 64 20 7b 0a 20 20 4b  ckedRecord {.  K
61e30 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f  eyInfo *pKeyInfo
61e40 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20  ;  /* Collation 
61e50 61 6e 64 20 73 6f 72 74 2d 6f 72 64 65 72 20 69  and sort-order i
61e60 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20  nformation */.  
61e70 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 20 20  u16 nField;     
61e80 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
61e90 20 65 6e 74 72 69 65 73 20 69 6e 20 61 70 4d 65   entries in apMe
61ea0 6d 5b 5d 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61  m[] */.  u16 fla
61eb0 67 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  gs;          /* 
61ec0 42 6f 6f 6c 65 61 6e 20 73 65 74 74 69 6e 67 73  Boolean settings
61ed0 2e 20 20 55 4e 50 41 43 4b 45 44 5f 2e 2e 2e 20  .  UNPACKED_... 
61ee0 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 36 34 20 72  below */.  i64 r
61ef0 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20 20 2f  owid;          /
61f00 2a 20 55 73 65 64 20 62 79 20 55 4e 50 41 43 4b  * Used by UNPACK
61f10 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52 43 48  ED_PREFIX_SEARCH
61f20 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b   */.  Mem *aMem;
61f30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c            /* Val
61f40 75 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ues */.};../*.**
61f50 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20   Allowed values 
61f60 6f 66 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  of UnpackedRecor
61f70 64 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69  d.flags.*/.#defi
61f80 6e 65 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44  ne UNPACKED_NEED
61f90 5f 46 52 45 45 20 20 20 20 20 30 78 30 30 30 31  _FREE     0x0001
61fa0 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 69 73 20 66    /* Memory is f
61fb0 72 6f 6d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  rom sqlite3Mallo
61fc0 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55  c() */.#define U
61fd0 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53  NPACKED_NEED_DES
61fe0 54 52 4f 59 20 20 30 78 30 30 30 32 20 20 2f 2a  TROY  0x0002  /*
61ff0 20 61 70 4d 65 6d 5b 5d 73 20 73 68 6f 75 6c 64   apMem[]s should
62000 20 61 6c 6c 20 62 65 20 64 65 73 74 72 6f 79 65   all be destroye
62010 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50  d */.#define UNP
62020 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57  ACKED_IGNORE_ROW
62030 49 44 20 20 30 78 30 30 30 34 20 20 2f 2a 20 49  ID  0x0004  /* I
62040 67 6e 6f 72 65 20 74 72 61 69 6c 69 6e 67 20 72  gnore trailing r
62050 6f 77 69 64 20 6f 6e 20 6b 65 79 31 20 2a 2f 0a  owid on key1 */.
62060 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44  #define UNPACKED
62070 5f 49 4e 43 52 4b 45 59 20 20 20 20 20 20 20 30  _INCRKEY       0
62080 78 30 30 30 38 20 20 2f 2a 20 4d 61 6b 65 20 74  x0008  /* Make t
62090 68 69 73 20 6b 65 79 20 61 6e 20 65 70 73 69 6c  his key an epsil
620a0 6f 6e 20 6c 61 72 67 65 72 20 2a 2f 0a 23 64 65  on larger */.#de
620b0 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 50 52  fine UNPACKED_PR
620c0 45 46 49 58 5f 4d 41 54 43 48 20 20 30 78 30 30  EFIX_MATCH  0x00
620d0 31 30 20 20 2f 2a 20 41 20 70 72 65 66 69 78 20  10  /* A prefix 
620e0 6d 61 74 63 68 20 69 73 20 63 6f 6e 73 69 64 65  match is conside
620f0 72 65 64 20 4f 4b 20 2a 2f 0a 23 64 65 66 69 6e  red OK */.#defin
62100 65 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49  e UNPACKED_PREFI
62110 58 5f 53 45 41 52 43 48 20 30 78 30 30 32 30 20  X_SEARCH 0x0020 
62120 20 2f 2a 20 41 20 70 72 65 66 69 78 20 6d 61 74   /* A prefix mat
62130 63 68 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  ch is considered
62140 20 4f 4b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61   OK */../*.** Ea
62150 63 68 20 53 51 4c 20 69 6e 64 65 78 20 69 73 20  ch SQL index is 
62160 72 65 70 72 65 73 65 6e 74 65 64 20 69 6e 20 6d  represented in m
62170 65 6d 6f 72 79 20 62 79 20 61 6e 0a 2a 2a 20 69  emory by an.** i
62180 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
62190 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
621a0 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  re..**.** The co
621b0 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61 62  lumns of the tab
621c0 6c 65 20 74 68 61 74 20 61 72 65 20 74 6f 20 62  le that are to b
621d0 65 20 69 6e 64 65 78 65 64 20 61 72 65 20 64 65  e indexed are de
621e0 73 63 72 69 62 65 64 0a 2a 2a 20 62 79 20 74 68  scribed.** by th
621f0 65 20 61 69 43 6f 6c 75 6d 6e 5b 5d 20 66 69 65  e aiColumn[] fie
62200 6c 64 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ld of this struc
62210 74 75 72 65 2e 20 20 46 6f 72 20 65 78 61 6d 70  ture.  For examp
62220 6c 65 2c 20 73 75 70 70 6f 73 65 0a 2a 2a 20 77  le, suppose.** w
62230 65 20 68 61 76 65 20 74 68 65 20 66 6f 6c 6c 6f  e have the follo
62240 77 69 6e 67 20 74 61 62 6c 65 20 61 6e 64 20 69  wing table and i
62250 6e 64 65 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ndex:.**.**     
62260 43 52 45 41 54 45 20 54 41 42 4c 45 20 45 78 31  CREATE TABLE Ex1
62270 28 63 31 20 69 6e 74 2c 20 63 32 20 69 6e 74 2c  (c1 int, c2 int,
62280 20 63 33 20 74 65 78 74 29 3b 0a 2a 2a 20 20 20   c3 text);.**   
62290 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 45    CREATE INDEX E
622a0 78 32 20 4f 4e 20 45 78 31 28 63 33 2c 63 31 29  x2 ON Ex1(c3,c1)
622b0 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 54  ;.**.** In the T
622c0 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 64  able structure d
622d0 65 73 63 72 69 62 69 6e 67 20 45 78 31 2c 20 6e  escribing Ex1, n
622e0 43 6f 6c 3d 3d 33 20 62 65 63 61 75 73 65 20 74  Col==3 because t
622f0 68 65 72 65 20 61 72 65 0a 2a 2a 20 74 68 72 65  here are.** thre
62300 65 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  e columns in the
62310 20 74 61 62 6c 65 2e 20 20 49 6e 20 74 68 65 20   table.  In the 
62320 49 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20  Index structure 
62330 64 65 73 63 72 69 62 69 6e 67 0a 2a 2a 20 45 78  describing.** Ex
62340 32 2c 20 6e 43 6f 6c 75 6d 6e 3d 3d 32 20 73 69  2, nColumn==2 si
62350 6e 63 65 20 32 20 6f 66 20 74 68 65 20 33 20 63  nce 2 of the 3 c
62360 6f 6c 75 6d 6e 73 20 6f 66 20 45 78 31 20 61 72  olumns of Ex1 ar
62370 65 20 69 6e 64 65 78 65 64 2e 0a 2a 2a 20 54 68  e indexed..** Th
62380 65 20 76 61 6c 75 65 20 6f 66 20 61 69 43 6f 6c  e value of aiCol
62390 75 6d 6e 20 69 73 20 7b 32 2c 20 30 7d 2e 20 20  umn is {2, 0}.  
623a0 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 32 20 62  aiColumn[0]==2 b
623b0 65 63 61 75 73 65 20 74 68 65 20 0a 2a 2a 20 66  ecause the .** f
623c0 69 72 73 74 20 63 6f 6c 75 6d 6e 20 74 6f 20 62  irst column to b
623d0 65 20 69 6e 64 65 78 65 64 20 28 63 33 29 20 68  e indexed (c3) h
623e0 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 32  as an index of 2
623f0 20 69 6e 20 45 78 31 2e 61 43 6f 6c 5b 5d 2e 0a   in Ex1.aCol[]..
62400 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 63 6f  ** The second co
62410 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64 65 78  lumn to be index
62420 65 64 20 28 63 31 29 20 68 61 73 20 61 6e 20 69  ed (c1) has an i
62430 6e 64 65 78 20 6f 66 20 30 20 69 6e 0a 2a 2a 20  ndex of 0 in.** 
62440 45 78 31 2e 61 43 6f 6c 5b 5d 2c 20 68 65 6e 63  Ex1.aCol[], henc
62450 65 20 45 78 32 2e 61 69 43 6f 6c 75 6d 6e 5b 31  e Ex2.aiColumn[1
62460 5d 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ]==0..**.** The 
62470 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 20 66 69  Index.onError fi
62480 65 6c 64 20 64 65 74 65 72 6d 69 6e 65 73 20 77  eld determines w
62490 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
624a0 65 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e  e indexed column
624b0 73 0a 2a 2a 20 6d 75 73 74 20 62 65 20 75 6e 69  s.** must be uni
624c0 71 75 65 20 61 6e 64 20 77 68 61 74 20 74 6f 20  que and what to 
624d0 64 6f 20 69 66 20 74 68 65 79 20 61 72 65 20 6e  do if they are n
624e0 6f 74 2e 20 20 57 68 65 6e 20 49 6e 64 65 78 2e  ot.  When Index.
624f0 6f 6e 45 72 72 6f 72 3d 4f 45 5f 4e 6f 6e 65 2c  onError=OE_None,
62500 0a 2a 2a 20 69 74 20 6d 65 61 6e 73 20 74 68 69  .** it means thi
62510 73 20 69 73 20 6e 6f 74 20 61 20 75 6e 69 71 75  s is not a uniqu
62520 65 20 69 6e 64 65 78 2e 20 20 4f 74 68 65 72 77  e index.  Otherw
62530 69 73 65 20 69 74 20 69 73 20 61 20 75 6e 69 71  ise it is a uniq
62540 75 65 20 69 6e 64 65 78 0a 2a 2a 20 61 6e 64 20  ue index.** and 
62550 74 68 65 20 76 61 6c 75 65 20 6f 66 20 49 6e 64  the value of Ind
62560 65 78 2e 6f 6e 45 72 72 6f 72 20 69 6e 64 69 63  ex.onError indic
62570 61 74 65 20 74 68 65 20 77 68 69 63 68 20 63 6f  ate the which co
62580 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f  nflict resolutio
62590 6e 20 0a 2a 2a 20 61 6c 67 6f 72 69 74 68 6d 20  n .** algorithm 
625a0 74 6f 20 65 6d 70 6c 6f 79 20 77 68 65 6e 65 76  to employ whenev
625b0 65 72 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73  er an attempt is
625c0 20 6d 61 64 65 20 74 6f 20 69 6e 73 65 72 74 20   made to insert 
625d0 61 20 6e 6f 6e 2d 75 6e 69 71 75 65 0a 2a 2a 20  a non-unique.** 
625e0 65 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75  element..*/.stru
625f0 63 74 20 49 6e 64 65 78 20 7b 0a 20 20 63 68 61  ct Index {.  cha
62600 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 2f 2a  r *zName;     /*
62610 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 69 6e   Name of this in
62620 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  dex */.  int nCo
62630 6c 75 6d 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d  lumn;     /* Num
62640 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
62650 6e 20 74 68 65 20 74 61 62 6c 65 20 75 73 65 64  n the table used
62660 20 62 79 20 74 68 69 73 20 69 6e 64 65 78 20 2a   by this index *
62670 2f 0a 20 20 69 6e 74 20 2a 61 69 43 6f 6c 75 6d  /.  int *aiColum
62680 6e 3b 20 20 20 2f 2a 20 57 68 69 63 68 20 63 6f  n;   /* Which co
62690 6c 75 6d 6e 73 20 61 72 65 20 75 73 65 64 20 62  lumns are used b
626a0 79 20 74 68 69 73 20 69 6e 64 65 78 2e 20 20 31  y this index.  1
626b0 73 74 20 69 73 20 30 20 2a 2f 0a 20 20 75 6e 73  st is 0 */.  uns
626c0 69 67 6e 65 64 20 2a 61 69 52 6f 77 45 73 74 3b  igned *aiRowEst;
626d0 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 41 4e   /* Result of AN
626e0 41 4c 59 5a 45 3a 20 45 73 74 2e 20 72 6f 77 73  ALYZE: Est. rows
626f0 20 73 65 6c 65 63 74 65 64 20 62 79 20 65 61 63   selected by eac
62700 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 54 61  h column */.  Ta
62710 62 6c 65 20 2a 70 54 61 62 6c 65 3b 20 20 20 2f  ble *pTable;   /
62720 2a 20 54 68 65 20 53 51 4c 20 74 61 62 6c 65 20  * The SQL table 
62730 62 65 69 6e 67 20 69 6e 64 65 78 65 64 20 2a 2f  being indexed */
62740 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 20 20  .  int tnum;    
62750 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74      /* Page cont
62760 61 69 6e 69 6e 67 20 72 6f 6f 74 20 6f 66 20 74  aining root of t
62770 68 69 73 20 69 6e 64 65 78 20 69 6e 20 64 61 74  his index in dat
62780 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20  abase file */.  
62790 75 38 20 6f 6e 45 72 72 6f 72 3b 20 20 20 20 20  u8 onError;     
627a0 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 2c 20 4f 45   /* OE_Abort, OE
627b0 5f 49 67 6e 6f 72 65 2c 20 4f 45 5f 52 65 70 6c  _Ignore, OE_Repl
627c0 61 63 65 2c 20 6f 72 20 4f 45 5f 4e 6f 6e 65 20  ace, or OE_None 
627d0 2a 2f 0a 20 20 75 38 20 61 75 74 6f 49 6e 64 65  */.  u8 autoInde
627e0 78 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  x;    /* True if
627f0 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   is automaticall
62800 79 20 63 72 65 61 74 65 64 20 28 65 78 3a 20 62  y created (ex: b
62810 79 20 55 4e 49 51 55 45 29 20 2a 2f 0a 20 20 63  y UNIQUE) */.  c
62820 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 20 20 20  har *zColAff;   
62830 2f 2a 20 53 74 72 69 6e 67 20 64 65 66 69 6e 69  /* String defini
62840 6e 67 20 74 68 65 20 61 66 66 69 6e 69 74 79 20  ng the affinity 
62850 6f 66 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a  of each column *
62860 2f 0a 20 20 49 6e 64 65 78 20 2a 70 4e 65 78 74  /.  Index *pNext
62870 3b 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74  ;    /* The next
62880 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65   index associate
62890 64 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  d with the same 
628a0 74 61 62 6c 65 20 2a 2f 0a 20 20 53 63 68 65 6d  table */.  Schem
628b0 61 20 2a 70 53 63 68 65 6d 61 3b 20 2f 2a 20 53  a *pSchema; /* S
628c0 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67  chema containing
628d0 20 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20   this index */. 
628e0 20 75 38 20 2a 61 53 6f 72 74 4f 72 64 65 72 3b   u8 *aSortOrder;
628f0 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 69    /* Array of si
62900 7a 65 20 49 6e 64 65 78 2e 6e 43 6f 6c 75 6d 6e  ze Index.nColumn
62910 2e 20 54 72 75 65 3d 3d 44 45 53 43 2c 20 46 61  . True==DESC, Fa
62920 6c 73 65 3d 3d 41 53 43 20 2a 2f 0a 20 20 63 68  lse==ASC */.  ch
62930 61 72 20 2a 2a 61 7a 43 6f 6c 6c 3b 20 20 20 2f  ar **azColl;   /
62940 2a 20 41 72 72 61 79 20 6f 66 20 63 6f 6c 6c 61  * Array of colla
62950 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61  tion sequence na
62960 6d 65 73 20 66 6f 72 20 69 6e 64 65 78 20 2a 2f  mes for index */
62970 0a 20 20 49 6e 64 65 78 53 61 6d 70 6c 65 20 2a  .  IndexSample *
62980 61 53 61 6d 70 6c 65 3b 20 20 20 20 2f 2a 20 41  aSample;    /* A
62990 72 72 61 79 20 6f 66 20 53 51 4c 49 54 45 5f 49  rray of SQLITE_I
629a0 4e 44 45 58 5f 53 41 4d 50 4c 45 53 20 73 61 6d  NDEX_SAMPLES sam
629b0 70 6c 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ples */.};../*.*
629c0 2a 20 45 61 63 68 20 73 61 6d 70 6c 65 20 73 74  * Each sample st
629d0 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69  ored in the sqli
629e0 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65 20 69  te_stat2 table i
629f0 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 69 6e  s represented in
62a00 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 75 73 69 6e   memory .** usin
62a10 67 20 61 20 73 74 72 75 63 74 75 72 65 20 6f 66  g a structure of
62a20 20 74 68 69 73 20 74 79 70 65 2e 0a 2a 2f 0a 73   this type..*/.s
62a30 74 72 75 63 74 20 49 6e 64 65 78 53 61 6d 70 6c  truct IndexSampl
62a40 65 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20  e {.  union {.  
62a50 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20    char *z;      
62a60 20 20 2f 2a 20 56 61 6c 75 65 20 69 66 20 65 54    /* Value if eT
62a70 79 70 65 20 69 73 20 53 51 4c 49 54 45 5f 54 45  ype is SQLITE_TE
62a80 58 54 20 6f 72 20 53 51 4c 49 54 45 5f 42 4c 4f  XT or SQLITE_BLO
62a90 42 20 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20  B */.    double 
62aa0 72 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75  r;       /* Valu
62ab0 65 20 69 66 20 65 54 79 70 65 20 69 73 20 53 51  e if eType is SQ
62ac0 4c 49 54 45 5f 46 4c 4f 41 54 20 6f 72 20 53 51  LITE_FLOAT or SQ
62ad0 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 2a 2f 0a  LITE_INTEGER */.
62ae0 20 20 7d 20 75 3b 0a 20 20 75 38 20 65 54 79 70    } u;.  u8 eTyp
62af0 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51  e;         /* SQ
62b00 4c 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54  LITE_NULL, SQLIT
62b10 45 5f 49 4e 54 45 47 45 52 20 2e 2e 2e 20 65 74  E_INTEGER ... et
62b20 63 2e 20 2a 2f 0a 20 20 75 38 20 6e 42 79 74 65  c. */.  u8 nByte
62b30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a  ;         /* Siz
62b40 65 20 69 6e 20 62 79 74 65 20 6f 66 20 74 65 78  e in byte of tex
62b50 74 20 6f 72 20 62 6c 6f 62 2e 20 2a 2f 0a 7d 3b  t or blob. */.};
62b60 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 6f 6b  ../*.** Each tok
62b70 65 6e 20 63 6f 6d 69 6e 67 20 6f 75 74 20 6f 66  en coming out of
62b80 20 74 68 65 20 6c 65 78 65 72 20 69 73 20 61 6e   the lexer is an
62b90 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20   instance of.** 
62ba0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 20  this structure. 
62bb0 20 54 6f 6b 65 6e 73 20 61 72 65 20 61 6c 73 6f   Tokens are also
62bc0 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f 66   used as part of
62bd0 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a   an expression..
62be0 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 69 66 20 54 6f  **.** Note if To
62bf0 6b 65 6e 2e 7a 3d 3d 30 20 74 68 65 6e 20 54 6f  ken.z==0 then To
62c00 6b 65 6e 2e 64 79 6e 20 61 6e 64 20 54 6f 6b 65  ken.dyn and Toke
62c10 6e 2e 6e 20 61 72 65 20 75 6e 64 65 66 69 6e 65  n.n are undefine
62c20 64 20 61 6e 64 0a 2a 2a 20 6d 61 79 20 63 6f 6e  d and.** may con
62c30 74 61 69 6e 20 72 61 6e 64 6f 6d 20 76 61 6c 75  tain random valu
62c40 65 73 2e 20 20 44 6f 20 6e 6f 74 20 6d 61 6b 65  es.  Do not make
62c50 20 61 6e 79 20 61 73 73 75 6d 70 74 69 6f 6e 73   any assumptions
62c60 20 61 62 6f 75 74 20 54 6f 6b 65 6e 2e 64 79 6e   about Token.dyn
62c70 0a 2a 2a 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20  .** and Token.n 
62c80 77 68 65 6e 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e  when Token.z==0.
62c90 0a 2a 2f 0a 73 74 72 75 63 74 20 54 6f 6b 65 6e  .*/.struct Token
62ca0 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20   {.  const char 
62cb0 2a 7a 3b 20 20 20 20 20 2f 2a 20 54 65 78 74 20  *z;     /* Text 
62cc0 6f 66 20 74 68 65 20 74 6f 6b 65 6e 2e 20 20 4e  of the token.  N
62cd0 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61 74  ot NULL-terminat
62ce0 65 64 21 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ed! */.  unsigne
62cf0 64 20 69 6e 74 20 6e 3b 20 20 20 20 2f 2a 20 4e  d int n;    /* N
62d00 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
62d10 65 72 73 20 69 6e 20 74 68 69 73 20 74 6f 6b 65  ers in this toke
62d20 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  n */.};../*.** A
62d30 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
62d40 69 73 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  is structure con
62d50 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69 6f  tains informatio
62d60 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65  n needed to gene
62d70 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f 72  rate.** code for
62d80 20 61 20 53 45 4c 45 43 54 20 74 68 61 74 20 63   a SELECT that c
62d90 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74  ontains aggregat
62da0 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a  e functions..**.
62db0 2a 2a 20 49 66 20 45 78 70 72 2e 6f 70 3d 3d 54  ** If Expr.op==T
62dc0 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 6f 72 20  K_AGG_COLUMN or 
62dd0 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20  TK_AGG_FUNCTION 
62de0 74 68 65 6e 20 45 78 70 72 2e 70 41 67 67 49 6e  then Expr.pAggIn
62df0 66 6f 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74  fo is a.** point
62e00 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 75 63  er to this struc
62e10 74 75 72 65 2e 20 20 54 68 65 20 45 78 70 72 2e  ture.  The Expr.
62e20 69 43 6f 6c 75 6d 6e 20 66 69 65 6c 64 20 69 73  iColumn field is
62e30 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a 2a 2a   the index in.**
62e40 20 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b 5d 20   AggInfo.aCol[] 
62e50 6f 72 20 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63  or AggInfo.aFunc
62e60 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f  [] of informatio
62e70 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e 65  n needed to gene
62e80 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f 72  rate.** code for
62e90 20 74 68 61 74 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a   that node..**.*
62ea0 2a 20 41 67 67 49 6e 66 6f 2e 70 47 72 6f 75 70  * AggInfo.pGroup
62eb0 42 79 20 61 6e 64 20 41 67 67 49 6e 66 6f 2e 61  By and AggInfo.a
62ec0 46 75 6e 63 2e 70 45 78 70 72 20 70 6f 69 6e 74  Func.pExpr point
62ed0 20 74 6f 20 66 69 65 6c 64 73 20 77 69 74 68 69   to fields withi
62ee0 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61  n the.** origina
62ef0 6c 20 53 65 6c 65 63 74 20 73 74 72 75 63 74 75  l Select structu
62f00 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62 65  re that describe
62f10 73 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  s the SELECT sta
62f20 74 65 6d 65 6e 74 2e 20 20 54 68 65 73 65 0a 2a  tement.  These.*
62f30 2a 20 66 69 65 6c 64 73 20 64 6f 20 6e 6f 74 20  * fields do not 
62f40 6e 65 65 64 20 74 6f 20 62 65 20 66 72 65 65 64  need to be freed
62f50 20 77 68 65 6e 20 64 65 61 6c 6c 6f 63 61 74 69   when deallocati
62f60 6e 67 20 74 68 65 20 41 67 67 49 6e 66 6f 20 73  ng the AggInfo s
62f70 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72  tructure..*/.str
62f80 75 63 74 20 41 67 67 49 6e 66 6f 20 7b 0a 20 20  uct AggInfo {.  
62f90 75 38 20 64 69 72 65 63 74 4d 6f 64 65 3b 20 20  u8 directMode;  
62fa0 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63          /* Direc
62fb0 74 20 72 65 6e 64 65 72 69 6e 67 20 6d 6f 64 65  t rendering mode
62fc0 20 6d 65 61 6e 73 20 74 61 6b 65 20 64 61 74 61   means take data
62fd0 20 64 69 72 65 63 74 6c 79 0a 20 20 20 20 20 20   directly.      
62fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62ff0 20 20 20 20 2a 2a 20 66 72 6f 6d 20 73 6f 75 72      ** from sour
63000 63 65 20 74 61 62 6c 65 73 20 72 61 74 68 65 72  ce tables rather
63010 20 74 68 61 6e 20 66 72 6f 6d 20 61 63 63 75 6d   than from accum
63020 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 20 75 38 20  ulators */.  u8 
63030 75 73 65 53 6f 72 74 69 6e 67 49 64 78 3b 20 20  useSortingIdx;  
63040 20 20 20 20 20 2f 2a 20 49 6e 20 64 69 72 65 63       /* In direc
63050 74 20 6d 6f 64 65 2c 20 72 65 66 65 72 65 6e 63  t mode, referenc
63060 65 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e  e the sorting in
63070 64 65 78 20 72 61 74 68 65 72 0a 20 20 20 20 20  dex rather.     
63080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63090 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 74 68 65       ** than the
630a0 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f   source table */
630b0 0a 20 20 69 6e 74 20 73 6f 72 74 69 6e 67 49 64  .  int sortingId
630c0 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75  x;         /* Cu
630d0 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74  rsor number of t
630e0 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78  he sorting index
630f0 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
63100 70 47 72 6f 75 70 42 79 3b 20 20 20 20 20 2f 2a  pGroupBy;     /*
63110 20 54 68 65 20 67 72 6f 75 70 20 62 79 20 63 6c   The group by cl
63120 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 53  ause */.  int nS
63130 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 3b 20 20 20  ortingColumn;   
63140 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
63150 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 73 6f  olumns in the so
63160 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20  rting index */. 
63170 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f   struct AggInfo_
63180 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 46 6f 72 20  col {    /* For 
63190 65 61 63 68 20 63 6f 6c 75 6d 6e 20 75 73 65 64  each column used
631a0 20 69 6e 20 73 6f 75 72 63 65 20 74 61 62 6c 65   in source table
631b0 73 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65 20 2a  s */.    Table *
631c0 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  pTab;           
631d0 20 20 2f 2a 20 53 6f 75 72 63 65 20 74 61 62 6c    /* Source tabl
631e0 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 54 61  e */.    int iTa
631f0 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ble;            
63200 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62    /* Cursor numb
63210 65 72 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65  er of the source
63220 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e   table */.    in
63230 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  t iColumn;      
63240 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e         /* Column
63250 20 6e 75 6d 62 65 72 20 77 69 74 68 69 6e 20 74   number within t
63260 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20  he source table 
63270 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f 72 74  */.    int iSort
63280 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20  erColumn;       
63290 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  /* Column number
632a0 20 69 6e 20 74 68 65 20 73 6f 72 74 69 6e 67 20   in the sorting 
632b0 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 6e 74  index */.    int
632c0 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20   iMem;          
632d0 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20        /* Memory 
632e0 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 61 63  location that ac
632f0 74 73 20 61 73 20 61 63 63 75 6d 75 6c 61 74 6f  ts as accumulato
63300 72 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70  r */.    Expr *p
63310 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20  Expr;           
63320 20 20 2f 2a 20 54 68 65 20 6f 72 69 67 69 6e 61    /* The origina
63330 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  l expression */.
63340 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 20 69 6e 74    } *aCol;.  int
63350 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20   nColumn;       
63360 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
63370 66 20 75 73 65 64 20 65 6e 74 72 69 65 73 20 69  f used entries i
63380 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e  n aCol[] */.  in
63390 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 3b 20  t nColumnAlloc; 
633a0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
633b0 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74  of slots allocat
633c0 65 64 20 66 6f 72 20 61 43 6f 6c 5b 5d 20 2a 2f  ed for aCol[] */
633d0 0a 20 20 69 6e 74 20 6e 41 63 63 75 6d 75 6c 61  .  int nAccumula
633e0 74 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  tor;       /* Nu
633f0 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
63400 74 68 61 74 20 73 68 6f 77 20 74 68 72 6f 75 67  that show throug
63410 68 20 74 6f 20 74 68 65 20 6f 75 74 70 75 74 2e  h to the output.
63420 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
63430 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 41 64             ** Ad
63440 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d 6e 73  ditional columns
63450 20 61 72 65 20 75 73 65 64 20 6f 6e 6c 79 20 61   are used only a
63460 73 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 0a  s parameters to.
63470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63480 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 67 67            ** agg
63490 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
634a0 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67   */.  struct Agg
634b0 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 20 20 2f 2a  Info_func {   /*
634c0 20 46 6f 72 20 65 61 63 68 20 61 67 67 72 65 67   For each aggreg
634d0 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ate function */.
634e0 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b      Expr *pExpr;
634f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
63500 45 78 70 72 65 73 73 69 6f 6e 20 65 6e 63 6f 64  Expression encod
63510 69 6e 67 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  ing the function
63520 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 66 20   */.    FuncDef 
63530 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20  *pFunc;         
63540 20 2f 2a 20 54 68 65 20 61 67 67 72 65 67 61 74   /* The aggregat
63550 65 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65  e function imple
63560 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20  mentation */.   
63570 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20   int iMem;      
63580 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d            /* Mem
63590 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61  ory location tha
635a0 74 20 61 63 74 73 20 61 73 20 61 63 63 75 6d 75  t acts as accumu
635b0 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69 6e 74  lator */.    int
635c0 20 69 44 69 73 74 69 6e 63 74 3b 20 20 20 20 20   iDistinct;     
635d0 20 20 20 20 20 20 2f 2a 20 45 70 68 65 6d 65 72        /* Ephemer
635e0 61 6c 20 74 61 62 6c 65 20 75 73 65 64 20 74 6f  al table used to
635f0 20 65 6e 66 6f 72 63 65 20 44 49 53 54 49 4e 43   enforce DISTINC
63600 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 75 6e 63 3b  T */.  } *aFunc;
63610 0a 20 20 69 6e 74 20 6e 46 75 6e 63 3b 20 20 20  .  int nFunc;   
63620 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
63630 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
63640 69 6e 20 61 46 75 6e 63 5b 5d 20 2a 2f 0a 20 20  in aFunc[] */.  
63650 69 6e 74 20 6e 46 75 6e 63 41 6c 6c 6f 63 3b 20  int nFuncAlloc; 
63660 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
63670 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63  r of slots alloc
63680 61 74 65 64 20 66 6f 72 20 61 46 75 6e 63 5b 5d  ated for aFunc[]
63690 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61   */.};../*.** Ea
636a0 63 68 20 6e 6f 64 65 20 6f 66 20 61 6e 20 65 78  ch node of an ex
636b0 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20  pression in the 
636c0 70 61 72 73 65 20 74 72 65 65 20 69 73 20 61 6e  parse tree is an
636d0 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20   instance.** of 
636e0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a  this structure..
636f0 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f 70 20 69 73  **.** Expr.op is
63700 20 74 68 65 20 6f 70 63 6f 64 65 2e 20 54 68 65   the opcode. The
63710 20 69 6e 74 65 67 65 72 20 70 61 72 73 65 72 20   integer parser 
63720 74 6f 6b 65 6e 20 63 6f 64 65 73 20 61 72 65 20  token codes are 
63730 72 65 75 73 65 64 0a 2a 2a 20 61 73 20 6f 70 63  reused.** as opc
63740 6f 64 65 73 20 68 65 72 65 2e 20 46 6f 72 20 65  odes here. For e
63750 78 61 6d 70 6c 65 2c 20 74 68 65 20 70 61 72 73  xample, the pars
63760 65 72 20 64 65 66 69 6e 65 73 20 54 4b 5f 47 45  er defines TK_GE
63770 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65   to be an intege
63780 72 0a 2a 2a 20 63 6f 64 65 20 72 65 70 72 65 73  r.** code repres
63790 65 6e 74 69 6e 67 20 74 68 65 20 22 3e 3d 22 20  enting the ">=" 
637a0 6f 70 65 72 61 74 6f 72 2e 20 54 68 69 73 20 73  operator. This s
637b0 61 6d 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65  ame integer code
637c0 20 69 73 20 72 65 75 73 65 64 0a 2a 2a 20 74 6f   is reused.** to
637d0 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20 67   represent the g
637e0 72 65 61 74 65 72 2d 74 68 61 6e 2d 6f 72 2d 65  reater-than-or-e
637f0 71 75 61 6c 2d 74 6f 20 6f 70 65 72 61 74 6f 72  qual-to operator
63800 20 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69   in the expressi
63810 6f 6e 0a 2a 2a 20 74 72 65 65 2e 0a 2a 2a 0a 2a  on.** tree..**.*
63820 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73  * If the express
63830 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 6c 69  ion is an SQL li
63840 74 65 72 61 6c 20 28 54 4b 5f 49 4e 54 45 47 45  teral (TK_INTEGE
63850 52 2c 20 54 4b 5f 46 4c 4f 41 54 2c 20 54 4b 5f  R, TK_FLOAT, TK_
63860 42 4c 4f 42 2c 20 0a 2a 2a 20 6f 72 20 54 4b 5f  BLOB, .** or TK_
63870 53 54 52 49 4e 47 29 2c 20 74 68 65 6e 20 45 78  STRING), then Ex
63880 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e  pr.token contain
63890 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 74 68  s the text of th
638a0 65 20 53 51 4c 20 6c 69 74 65 72 61 6c 2e 20 49  e SQL literal. I
638b0 66 0a 2a 2a 20 74 68 65 20 65 78 70 72 65 73 73  f.** the express
638c0 69 6f 6e 20 69 73 20 61 20 76 61 72 69 61 62 6c  ion is a variabl
638d0 65 20 28 54 4b 5f 56 41 52 49 41 42 4c 45 29 2c  e (TK_VARIABLE),
638e0 20 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 6e   then Expr.token
638f0 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 0a 2a   contains the .*
63900 2a 20 76 61 72 69 61 62 6c 65 20 6e 61 6d 65 2e  * variable name.
63910 20 46 69 6e 61 6c 6c 79 2c 20 69 66 20 74 68 65   Finally, if the
63920 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61   expression is a
63930 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 28  n SQL function (
63940 54 4b 5f 46 55 4e 43 54 49 4f 4e 29 2c 0a 2a 2a  TK_FUNCTION),.**
63950 20 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65 6e   then Expr.token
63960 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 61   contains the na
63970 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  me of the functi
63980 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 70  on..**.** Expr.p
63990 52 69 67 68 74 20 61 6e 64 20 45 78 70 72 2e 70  Right and Expr.p
639a0 4c 65 66 74 20 61 72 65 20 74 68 65 20 6c 65 66  Left are the lef
639b0 74 20 61 6e 64 20 72 69 67 68 74 20 73 75 62 65  t and right sube
639c0 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 61 0a  xpressions of a.
639d0 2a 2a 20 62 69 6e 61 72 79 20 6f 70 65 72 61 74  ** binary operat
639e0 6f 72 2e 20 45 69 74 68 65 72 20 6f 72 20 62 6f  or. Either or bo
639f0 74 68 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 0a  th may be NULL..
63a00 2a 2a 0a 2a 2a 20 45 78 70 72 2e 78 2e 70 4c 69  **.** Expr.x.pLi
63a10 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20  st is a list of 
63a20 61 72 67 75 6d 65 6e 74 73 20 69 66 20 74 68 65  arguments if the
63a30 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61   expression is a
63a40 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2c 0a  n SQL function,.
63a50 2a 2a 20 61 20 43 41 53 45 20 65 78 70 72 65 73  ** a CASE expres
63a60 73 69 6f 6e 20 6f 72 20 61 6e 20 49 4e 20 65 78  sion or an IN ex
63a70 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20  pression of the 
63a80 66 6f 72 6d 20 22 3c 6c 68 73 3e 20 49 4e 20 28  form "<lhs> IN (
63a90 3c 79 3e 2c 20 3c 7a 3e 2e 2e 2e 29 22 2e 0a 2a  <y>, <z>...)"..*
63aa0 2a 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63 74  * Expr.x.pSelect
63ab0 20 69 73 20 75 73 65 64 20 69 66 20 74 68 65 20   is used if the 
63ac0 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20  expression is a 
63ad0 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 6e  sub-select or an
63ae0 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 0a 2a   expression of.*
63af0 2a 20 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68 73  * the form "<lhs
63b00 3e 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e  > IN (SELECT ...
63b10 29 22 2e 20 49 66 20 74 68 65 20 45 50 5f 78 49  )". If the EP_xI
63b20 73 53 65 6c 65 63 74 20 62 69 74 20 69 73 20 73  sSelect bit is s
63b30 65 74 20 69 6e 20 74 68 65 0a 2a 2a 20 45 78 70  et in the.** Exp
63b40 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68  r.flags mask, th
63b50 65 6e 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63  en Expr.x.pSelec
63b60 74 20 69 73 20 76 61 6c 69 64 2e 20 4f 74 68 65  t is valid. Othe
63b70 72 77 69 73 65 2c 20 45 78 70 72 2e 78 2e 70 4c  rwise, Expr.x.pL
63b80 69 73 74 20 69 73 20 0a 2a 2a 20 76 61 6c 69 64  ist is .** valid
63b90 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72 65  ..**.** An expre
63ba0 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72  ssion of the for
63bb0 6d 20 49 44 20 6f 72 20 49 44 2e 49 44 20 72 65  m ID or ID.ID re
63bc0 66 65 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d 6e  fers to a column
63bd0 20 69 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20   in a table..** 
63be0 46 6f 72 20 73 75 63 68 20 65 78 70 72 65 73 73  For such express
63bf0 69 6f 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69 73  ions, Expr.op is
63c00 20 73 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d   set to TK_COLUM
63c10 4e 20 61 6e 64 20 45 78 70 72 2e 69 54 61 62 6c  N and Expr.iTabl
63c20 65 20 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65  e is.** the inte
63c30 67 65 72 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  ger cursor numbe
63c40 72 20 6f 66 20 61 20 56 44 42 45 20 63 75 72 73  r of a VDBE curs
63c50 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74  or pointing to t
63c60 68 61 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a 2a  hat table and.**
63c70 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69 73   Expr.iColumn is
63c80 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62   the column numb
63c90 65 72 20 66 6f 72 20 74 68 65 20 73 70 65 63 69  er for the speci
63ca0 66 69 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20  fic column.  If 
63cb0 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  the.** expressio
63cc0 6e 20 69 73 20 75 73 65 64 20 61 73 20 61 20 72  n is used as a r
63cd0 65 73 75 6c 74 20 69 6e 20 61 6e 20 61 67 67 72  esult in an aggr
63ce0 65 67 61 74 65 20 53 45 4c 45 43 54 2c 20 74 68  egate SELECT, th
63cf0 65 6e 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20  en the.** value 
63d00 69 73 20 61 6c 73 6f 20 73 74 6f 72 65 64 20 69  is also stored i
63d10 6e 20 74 68 65 20 45 78 70 72 2e 69 41 67 67 20  n the Expr.iAgg 
63d20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61 67  column in the ag
63d30 67 72 65 67 61 74 65 20 73 6f 20 74 68 61 74 0a  gregate so that.
63d40 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 61 63 63  ** it can be acc
63d50 65 73 73 65 64 20 61 66 74 65 72 20 61 6c 6c 20  essed after all 
63d60 61 67 67 72 65 67 61 74 65 73 20 61 72 65 20 63  aggregates are c
63d70 6f 6d 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  omputed..**.** I
63d80 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
63d90 20 69 73 20 61 6e 20 75 6e 62 6f 75 6e 64 20 76   is an unbound v
63da0 61 72 69 61 62 6c 65 20 6d 61 72 6b 65 72 20 28  ariable marker (
63db0 61 20 71 75 65 73 74 69 6f 6e 20 6d 61 72 6b 20  a question mark 
63dc0 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 20 27 3f  .** character '?
63dd0 27 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61  ' in the origina
63de0 6c 20 53 51 4c 29 20 74 68 65 6e 20 74 68 65 20  l SQL) then the 
63df0 45 78 70 72 2e 69 54 61 62 6c 65 20 68 6f 6c 64  Expr.iTable hold
63e00 73 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20  s the index .** 
63e10 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74 20  number for that 
63e20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  variable..**.** 
63e30 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  If the expressio
63e40 6e 20 69 73 20 61 20 73 75 62 71 75 65 72 79 20  n is a subquery 
63e50 74 68 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75 6d  then Expr.iColum
63e60 6e 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67  n holds an integ
63e70 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 6e  er.** register n
63e80 75 6d 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  umber containing
63e90 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74   the result of t
63ea0 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 49 66  he subquery.  If
63eb0 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72 79   the.** subquery
63ec0 20 67 69 76 65 73 20 61 20 63 6f 6e 73 74 61 6e   gives a constan
63ed0 74 20 72 65 73 75 6c 74 2c 20 74 68 65 6e 20 69  t result, then i
63ee0 54 61 62 6c 65 20 69 73 20 2d 31 2e 20 20 49 66  Table is -1.  If
63ef0 20 74 68 65 20 73 75 62 71 75 65 72 79 0a 2a 2a   the subquery.**
63f00 20 67 69 76 65 73 20 61 20 64 69 66 66 65 72 65   gives a differe
63f10 6e 74 20 61 6e 73 77 65 72 20 61 74 20 64 69 66  nt answer at dif
63f20 66 65 72 65 6e 74 20 74 69 6d 65 73 20 64 75 72  ferent times dur
63f30 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70 72  ing statement pr
63f40 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 74 68 65 6e  ocessing.** then
63f50 20 69 54 61 62 6c 65 20 69 73 20 74 68 65 20 61   iTable is the a
63f60 64 64 72 65 73 73 20 6f 66 20 61 20 73 75 62 72  ddress of a subr
63f70 6f 75 74 69 6e 65 20 74 68 61 74 20 63 6f 6d 70  outine that comp
63f80 75 74 65 73 20 74 68 65 20 73 75 62 71 75 65 72  utes the subquer
63f90 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  y..**.** If the 
63fa0 45 78 70 72 20 69 73 20 6f 66 20 74 79 70 65 20  Expr is of type 
63fb0 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20 74  OP_Column, and t
63fc0 68 65 20 74 61 62 6c 65 20 69 74 20 69 73 20 73  he table it is s
63fd0 65 6c 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a 2a  electing from.**
63fe0 20 69 73 20 61 20 64 69 73 6b 20 74 61 62 6c 65   is a disk table
63ff0 20 6f 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22 20   or the "old.*" 
64000 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74 68  pseudo-table, th
64010 65 6e 20 70 54 61 62 20 70 6f 69 6e 74 73 20 74  en pTab points t
64020 6f 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70  o the.** corresp
64030 6f 6e 64 69 6e 67 20 74 61 62 6c 65 20 64 65 66  onding table def
64040 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41  inition..**.** A
64050 4c 4c 4f 43 41 54 49 4f 4e 20 4e 4f 54 45 53 3a  LLOCATION NOTES:
64060 0a 2a 2a 0a 2a 2a 20 45 78 70 72 20 6f 62 6a 65  .**.** Expr obje
64070 63 74 73 20 63 61 6e 20 75 73 65 20 61 20 6c 6f  cts can use a lo
64080 74 20 6f 66 20 6d 65 6d 6f 72 79 20 73 70 61 63  t of memory spac
64090 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 73 63  e in database sc
640a0 68 65 6d 61 2e 20 20 54 6f 0a 2a 2a 20 68 65 6c  hema.  To.** hel
640b0 70 20 72 65 64 75 63 65 20 6d 65 6d 6f 72 79 20  p reduce memory 
640c0 72 65 71 75 69 72 65 6d 65 6e 74 73 2c 20 73 6f  requirements, so
640d0 6d 65 74 69 6d 65 73 20 61 6e 20 45 78 70 72 20  metimes an Expr 
640e0 6f 62 6a 65 63 74 20 77 69 6c 6c 20 62 65 0a 2a  object will be.*
640f0 2a 20 74 72 75 6e 63 61 74 65 64 2e 20 20 41 6e  * truncated.  An
64100 64 20 74 6f 20 72 65 64 75 63 65 20 74 68 65 20  d to reduce the 
64110 6e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79  number of memory
64120 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2c 20 73 6f   allocations, so
64130 6d 65 74 69 6d 65 73 0a 2a 2a 20 74 77 6f 20 6f  metimes.** two o
64140 72 20 6d 6f 72 65 20 45 78 70 72 20 6f 62 6a 65  r more Expr obje
64150 63 74 73 20 77 69 6c 6c 20 62 65 20 73 74 6f 72  cts will be stor
64160 65 64 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 6d  ed in a single m
64170 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
64180 2c 0a 2a 2a 20 74 6f 67 65 74 68 65 72 20 77 69  ,.** together wi
64190 74 68 20 45 78 70 72 2e 7a 54 6f 6b 65 6e 20 73  th Expr.zToken s
641a0 74 72 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  trings..**.** If
641b0 20 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 20   the EP_Reduced 
641c0 61 6e 64 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  and EP_TokenOnly
641d0 20 66 6c 61 67 73 20 61 72 65 20 73 65 74 20 77   flags are set w
641e0 68 65 6e 0a 2a 2a 20 61 6e 20 45 78 70 72 20 6f  hen.** an Expr o
641f0 62 6a 65 63 74 20 69 73 20 74 72 75 6e 63 61 74  bject is truncat
64200 65 64 2e 20 20 57 68 65 6e 20 45 50 5f 52 65 64  ed.  When EP_Red
64210 75 63 65 64 20 69 73 20 73 65 74 2c 20 74 68 65  uced is set, the
64220 6e 20 61 6c 6c 0a 2a 2a 20 74 68 65 20 63 68 69  n all.** the chi
64230 6c 64 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20  ld Expr objects 
64240 69 6e 20 74 68 65 20 45 78 70 72 2e 70 4c 65 66  in the Expr.pLef
64250 74 20 61 6e 64 20 45 78 70 72 2e 70 52 69 67 68  t and Expr.pRigh
64260 74 20 73 75 62 74 72 65 65 73 0a 2a 2a 20 61 72  t subtrees.** ar
64270 65 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68  e contained with
64280 69 6e 20 74 68 65 20 73 61 6d 65 20 6d 65 6d 6f  in the same memo
64290 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20  ry allocation.  
642a0 4e 6f 74 65 2c 20 68 6f 77 65 76 65 72 2c 20 74  Note, however, t
642b0 68 61 74 0a 2a 2a 20 74 68 65 20 73 75 62 74 72  hat.** the subtr
642c0 65 65 73 20 69 6e 20 45 78 70 72 2e 78 2e 70 4c  ees in Expr.x.pL
642d0 69 73 74 20 6f 72 20 45 78 70 72 2e 78 2e 70 53  ist or Expr.x.pS
642e0 65 6c 65 63 74 20 61 72 65 20 61 6c 77 61 79 73  elect are always
642f0 20 73 65 70 61 72 61 74 65 6c 79 0a 2a 2a 20 61   separately.** a
64300 6c 6c 6f 63 61 74 65 64 2c 20 72 65 67 61 72 64  llocated, regard
64310 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20  less of whether 
64320 6f 72 20 6e 6f 74 20 45 50 5f 52 65 64 75 63 65  or not EP_Reduce
64330 64 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 73 74 72  d is set..*/.str
64340 75 63 74 20 45 78 70 72 20 7b 0a 20 20 75 38 20  uct Expr {.  u8 
64350 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
64360 20 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e      /* Operation
64370 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68   performed by th
64380 69 73 20 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61  is node */.  cha
64390 72 20 61 66 66 69 6e 69 74 79 3b 20 20 20 20 20  r affinity;     
643a0 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e      /* The affin
643b0 69 74 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  ity of the colum
643c0 6e 20 6f 72 20 30 20 69 66 20 6e 6f 74 20 61 20  n or 0 if not a 
643d0 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 31 36 20  column */.  u16 
643e0 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
643f0 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 66 6c     /* Various fl
64400 61 67 73 2e 20 20 45 50 5f 2a 20 53 65 65 20 62  ags.  EP_* See b
64410 65 6c 6f 77 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20  elow */.  union 
64420 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 6f 6b  {.    char *zTok
64430 65 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  en;          /* 
64440 54 6f 6b 65 6e 20 76 61 6c 75 65 2e 20 5a 65 72  Token value. Zer
64450 6f 20 74 65 72 6d 69 6e 61 74 65 64 20 61 6e 64  o terminated and
64460 20 64 65 71 75 6f 74 65 64 20 2a 2f 0a 20 20 20   dequoted */.   
64470 20 69 6e 74 20 69 56 61 6c 75 65 3b 20 20 20 20   int iValue;    
64480 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67          /* Integ
64490 65 72 20 76 61 6c 75 65 20 69 66 20 45 50 5f 49  er value if EP_I
644a0 6e 74 56 61 6c 75 65 20 2a 2f 0a 20 20 7d 20 75  ntValue */.  } u
644b0 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 45  ;..  /* If the E
644c0 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67  P_TokenOnly flag
644d0 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 45   is set in the E
644e0 78 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20  xpr.flags mask, 
644f0 74 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 61  then no.  ** spa
64500 63 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  ce is allocated 
64510 66 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20 62  for the fields b
64520 65 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 2e  elow this point.
64530 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 20   An attempt to. 
64540 20 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d 20   ** access them 
64550 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61  will result in a
64560 20 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c   segfault or mal
64570 66 75 6e 63 74 69 6f 6e 2e 20 0a 20 20 2a 2a 2a  function. .  ***
64580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
645a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
645b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
645c0 2a 2a 2f 0a 0a 20 20 45 78 70 72 20 2a 70 4c 65  **/..  Expr *pLe
645d0 66 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ft;           /*
645e0 20 4c 65 66 74 20 73 75 62 6e 6f 64 65 20 2a 2f   Left subnode */
645f0 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 3b  .  Expr *pRight;
64600 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67            /* Rig
64610 68 74 20 73 75 62 6e 6f 64 65 20 2a 2f 0a 20 20  ht subnode */.  
64620 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 45 78 70 72  union {.    Expr
64630 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20  List *pList;    
64640 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 61 72 67   /* Function arg
64650 75 6d 65 6e 74 73 20 6f 72 20 69 6e 20 22 3c 65  uments or in "<e
64660 78 70 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d 6c  xpr> IN (<expr-l
64670 69 73 74 29 22 20 2a 2f 0a 20 20 20 20 53 65 6c  ist)" */.    Sel
64680 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20  ect *pSelect;   
64690 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 73 75    /* Used for su
646a0 62 2d 73 65 6c 65 63 74 73 20 61 6e 64 20 22 3c  b-selects and "<
646b0 65 78 70 72 3e 20 49 4e 20 28 3c 73 65 6c 65 63  expr> IN (<selec
646c0 74 3e 29 22 20 2a 2f 0a 20 20 7d 20 78 3b 0a 20  t>)" */.  } x;. 
646d0 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
646e0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63          /* The c
646f0 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6f 66  ollation type of
64700 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20 30   the column or 0
64710 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   */..  /* If the
64720 20 45 50 5f 52 65 64 75 63 65 64 20 66 6c 61 67   EP_Reduced flag
64730 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 45   is set in the E
64740 78 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20  xpr.flags mask, 
64750 74 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70 61  then no.  ** spa
64760 63 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  ce is allocated 
64770 66 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20 62  for the fields b
64780 65 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74 2e  elow this point.
64790 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 20   An attempt to. 
647a0 20 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d 20   ** access them 
647b0 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 61  will result in a
647c0 20 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61 6c   segfault or mal
647d0 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 2a 2a  function..  ****
647e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
647f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64820 2a 2f 0a 0a 20 20 69 6e 74 20 69 54 61 62 6c 65  */..  int iTable
64830 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
64840 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 63 75 72 73 6f  TK_COLUMN: curso
64850 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c  r number of tabl
64860 65 20 68 6f 6c 64 69 6e 67 20 63 6f 6c 75 6d 6e  e holding column
64870 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
64880 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 4b 5f            ** TK_
64890 52 45 47 49 53 54 45 52 3a 20 72 65 67 69 73 74  REGISTER: regist
648a0 65 72 20 6e 75 6d 62 65 72 0a 20 20 20 20 20 20  er number.      
648b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
648c0 20 20 20 2a 2a 20 54 4b 5f 54 52 49 47 47 45 52     ** TK_TRIGGER
648d0 3a 20 31 20 2d 3e 20 6e 65 77 2c 20 30 20 2d 3e  : 1 -> new, 0 ->
648e0 20 6f 6c 64 20 2a 2f 0a 20 20 69 31 36 20 69 43   old */.  i16 iC
648f0 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20  olumn;          
64900 20 2f 2a 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 63   /* TK_COLUMN: c
64910 6f 6c 75 6d 6e 20 69 6e 64 65 78 2e 20 20 2d 31  olumn index.  -1
64920 20 66 6f 72 20 72 6f 77 69 64 20 2a 2f 0a 20 20   for rowid */.  
64930 69 31 36 20 69 41 67 67 3b 20 20 20 20 20 20 20  i16 iAgg;       
64940 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20         /* Which 
64950 65 6e 74 72 79 20 69 6e 20 70 41 67 67 49 6e 66  entry in pAggInf
64960 6f 2d 3e 61 43 6f 6c 5b 5d 20 6f 72 20 2d 3e 61  o->aCol[] or ->a
64970 46 75 6e 63 5b 5d 20 2a 2f 0a 20 20 69 31 36 20  Func[] */.  i16 
64980 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 3b  iRightJoinTable;
64990 20 20 20 2f 2a 20 49 66 20 45 50 5f 46 72 6f 6d     /* If EP_From
649a0 4a 6f 69 6e 2c 20 74 68 65 20 72 69 67 68 74 20  Join, the right 
649b0 74 61 62 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69  table of the joi
649c0 6e 20 2a 2f 0a 20 20 75 38 20 66 6c 61 67 73 32  n */.  u8 flags2
649d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
649e0 20 53 65 63 6f 6e 64 20 73 65 74 20 6f 66 20 66   Second set of f
649f0 6c 61 67 73 2e 20 20 45 50 32 5f 2e 2e 2e 20 2a  lags.  EP2_... *
64a00 2f 0a 20 20 75 38 20 6f 70 32 3b 20 20 20 20 20  /.  u8 op2;     
64a10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
64a20 20 61 20 54 4b 5f 52 45 47 49 53 54 45 52 2c 20   a TK_REGISTER, 
64a30 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 76 61 6c  the original val
64a40 75 65 20 6f 66 20 45 78 70 72 2e 6f 70 20 2a 2f  ue of Expr.op */
64a50 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67  .  AggInfo *pAgg
64a60 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 55 73 65  Info;     /* Use
64a70 64 20 62 79 20 54 4b 5f 41 47 47 5f 43 4f 4c 55  d by TK_AGG_COLU
64a80 4d 4e 20 61 6e 64 20 54 4b 5f 41 47 47 5f 46 55  MN and TK_AGG_FU
64a90 4e 43 54 49 4f 4e 20 2a 2f 0a 20 20 54 61 62 6c  NCTION */.  Tabl
64aa0 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20  e *pTab;        
64ab0 20 20 20 2f 2a 20 54 61 62 6c 65 20 66 6f 72 20     /* Table for 
64ac0 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 72 65 73  TK_COLUMN expres
64ad0 73 69 6f 6e 73 2e 20 2a 2f 0a 23 69 66 20 53 51  sions. */.#if SQ
64ae0 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45  LITE_MAX_EXPR_DE
64af0 50 54 48 3e 30 0a 20 20 69 6e 74 20 6e 48 65 69  PTH>0.  int nHei
64b00 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ght;           /
64b10 2a 20 48 65 69 67 68 74 20 6f 66 20 74 68 65 20  * Height of the 
64b20 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20 74  tree headed by t
64b30 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 23 65 6e 64  his node */.#end
64b40 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  if.};../*.** The
64b50 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74   following are t
64b60 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62  he meanings of b
64b70 69 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e  its in the Expr.
64b80 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a  flags field..*/.
64b90 23 64 65 66 69 6e 65 20 45 50 5f 46 72 6f 6d 4a  #define EP_FromJ
64ba0 6f 69 6e 20 20 20 30 78 30 30 30 31 20 20 2f 2a  oin   0x0001  /*
64bb0 20 4f 72 69 67 69 6e 61 74 65 64 20 69 6e 20 4f   Originated in O
64bc0 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73  N or USING claus
64bd0 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 23  e of a join */.#
64be0 64 65 66 69 6e 65 20 45 50 5f 41 67 67 20 20 20  define EP_Agg   
64bf0 20 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a 20       0x0002  /* 
64c00 43 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20  Contains one or 
64c10 6d 6f 72 65 20 61 67 67 72 65 67 61 74 65 20 66  more aggregate f
64c20 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66  unctions */.#def
64c30 69 6e 65 20 45 50 5f 52 65 73 6f 6c 76 65 64 20  ine EP_Resolved 
64c40 20 20 30 78 30 30 30 34 20 20 2f 2a 20 49 44 73    0x0004  /* IDs
64c50 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c   have been resol
64c60 76 65 64 20 74 6f 20 43 4f 4c 55 4d 4e 73 20 2a  ved to COLUMNs *
64c70 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 45 72 72  /.#define EP_Err
64c80 6f 72 20 20 20 20 20 20 30 78 30 30 30 38 20 20  or      0x0008  
64c90 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 63 6f  /* Expression co
64ca0 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f  ntains one or mo
64cb0 72 65 20 65 72 72 6f 72 73 20 2a 2f 0a 23 64 65  re errors */.#de
64cc0 66 69 6e 65 20 45 50 5f 44 69 73 74 69 6e 63 74  fine EP_Distinct
64cd0 20 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41 67     0x0010  /* Ag
64ce0 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
64cf0 20 77 69 74 68 20 44 49 53 54 49 4e 43 54 20 6b   with DISTINCT k
64d00 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66 69 6e  eyword */.#defin
64d10 65 20 45 50 5f 56 61 72 53 65 6c 65 63 74 20 20  e EP_VarSelect  
64d20 30 78 30 30 32 30 20 20 2f 2a 20 70 53 65 6c 65  0x0020  /* pSele
64d30 63 74 20 69 73 20 63 6f 72 72 65 6c 61 74 65 64  ct is correlated
64d40 2c 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 20 2a  , not constant *
64d50 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 44 62 6c  /.#define EP_Dbl
64d60 51 75 6f 74 65 64 20 20 30 78 30 30 34 30 20 20  Quoted  0x0040  
64d70 2f 2a 20 74 6f 6b 65 6e 2e 7a 20 77 61 73 20 6f  /* token.z was o
64d80 72 69 67 69 6e 61 6c 6c 79 20 69 6e 20 22 2e 2e  riginally in "..
64d90 2e 22 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50  ." */.#define EP
64da0 5f 49 6e 66 69 78 46 75 6e 63 20 20 30 78 30 30  _InfixFunc  0x00
64db0 38 30 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20  80  /* True for 
64dc0 61 6e 20 69 6e 66 69 78 20 66 75 6e 63 74 69 6f  an infix functio
64dd0 6e 3a 20 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 65  n: LIKE, GLOB, e
64de0 74 63 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50  tc */.#define EP
64df0 5f 45 78 70 43 6f 6c 6c 61 74 65 20 30 78 30 31  _ExpCollate 0x01
64e00 30 30 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67  00  /* Collating
64e10 20 73 65 71 75 65 6e 63 65 20 73 70 65 63 69 66   sequence specif
64e20 69 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20 2a  ied explicitly *
64e30 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 41 6e 79  /.#define EP_Any
64e40 41 66 66 20 20 20 20 20 30 78 30 32 30 30 20 20  Aff     0x0200  
64e50 2f 2a 20 43 61 6e 20 74 61 6b 65 20 61 20 63 61  /* Can take a ca
64e60 63 68 65 64 20 63 6f 6c 75 6d 6e 20 6f 66 20 61  ched column of a
64e70 6e 79 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a 23  ny affinity */.#
64e80 64 65 66 69 6e 65 20 45 50 5f 46 69 78 65 64 44  define EP_FixedD
64e90 65 73 74 20 20 30 78 30 34 30 30 20 20 2f 2a 20  est  0x0400  /* 
64ea0 52 65 73 75 6c 74 20 6e 65 65 64 65 64 20 69 6e  Result needed in
64eb0 20 61 20 73 70 65 63 69 66 69 63 20 72 65 67 69   a specific regi
64ec0 73 74 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ster */.#define 
64ed0 45 50 5f 49 6e 74 56 61 6c 75 65 20 20 20 30 78  EP_IntValue   0x
64ee0 30 38 30 30 20 20 2f 2a 20 49 6e 74 65 67 65 72  0800  /* Integer
64ef0 20 76 61 6c 75 65 20 63 6f 6e 74 61 69 6e 65 64   value contained
64f00 20 69 6e 20 75 2e 69 56 61 6c 75 65 20 2a 2f 0a   in u.iValue */.
64f10 23 64 65 66 69 6e 65 20 45 50 5f 78 49 73 53 65  #define EP_xIsSe
64f20 6c 65 63 74 20 20 30 78 31 30 30 30 20 20 2f 2a  lect  0x1000  /*
64f30 20 78 2e 70 53 65 6c 65 63 74 20 69 73 20 76 61   x.pSelect is va
64f40 6c 69 64 20 28 6f 74 68 65 72 77 69 73 65 20 78  lid (otherwise x
64f50 2e 70 4c 69 73 74 20 69 73 29 20 2a 2f 0a 0a 23  .pList is) */..#
64f60 64 65 66 69 6e 65 20 45 50 5f 52 65 64 75 63 65  define EP_Reduce
64f70 64 20 20 20 20 30 78 32 30 30 30 20 20 2f 2a 20  d    0x2000  /* 
64f80 45 78 70 72 20 73 74 72 75 63 74 20 69 73 20 45  Expr struct is E
64f90 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20  XPR_REDUCEDSIZE 
64fa0 62 79 74 65 73 20 6f 6e 6c 79 20 2a 2f 0a 23 64  bytes only */.#d
64fb0 65 66 69 6e 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e  efine EP_TokenOn
64fc0 6c 79 20 20 30 78 34 30 30 30 20 20 2f 2a 20 45  ly  0x4000  /* E
64fd0 78 70 72 20 73 74 72 75 63 74 20 69 73 20 45 58  xpr struct is EX
64fe0 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45  PR_TOKENONLYSIZE
64ff0 20 62 79 74 65 73 20 6f 6e 6c 79 20 2a 2f 0a 23   bytes only */.#
65000 64 65 66 69 6e 65 20 45 50 5f 53 74 61 74 69 63  define EP_Static
65010 20 20 20 20 20 30 78 38 30 30 30 20 20 2f 2a 20       0x8000  /* 
65020 48 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6e  Held in memory n
65030 6f 74 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ot obtained from
65040 20 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 0a 2f 2a   malloc() */../*
65050 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
65060 67 20 61 72 65 20 74 68 65 20 6d 65 61 6e 69 6e  g are the meanin
65070 67 73 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68  gs of bits in th
65080 65 20 45 78 70 72 2e 66 6c 61 67 73 32 20 66 69  e Expr.flags2 fi
65090 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  eld..*/.#define 
650a0 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65  EP2_MallocedToke
650b0 6e 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4e 65  n  0x0001  /* Ne
650c0 65 64 20 74 6f 20 73 71 6c 69 74 65 33 44 62 46  ed to sqlite3DbF
650d0 72 65 65 28 29 20 45 78 70 72 2e 7a 54 6f 6b 65  ree() Expr.zToke
650e0 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 32  n */.#define EP2
650f0 5f 49 72 72 65 64 75 63 69 62 6c 65 20 20 20 20  _Irreducible    
65100 30 78 30 30 30 32 20 20 2f 2a 20 43 61 6e 6e 6f  0x0002  /* Canno
65110 74 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45  t EXPRDUP_REDUCE
65120 20 74 68 69 73 20 45 78 70 72 20 2a 2f 0a 0a 2f   this Expr */../
65130 2a 0a 2a 2a 20 54 68 65 20 70 73 65 75 64 6f 2d  *.** The pseudo-
65140 72 6f 75 74 69 6e 65 20 73 71 6c 69 74 65 33 45  routine sqlite3E
65150 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62 6c  xprSetIrreducibl
65160 65 20 73 65 74 73 20 74 68 65 20 45 50 32 5f 49  e sets the EP2_I
65170 72 72 65 64 75 63 69 62 6c 65 0a 2a 2a 20 66 6c  rreducible.** fl
65180 61 67 20 6f 6e 20 61 6e 20 65 78 70 72 65 73 73  ag on an express
65190 69 6f 6e 20 73 74 72 75 63 74 75 72 65 2e 20 20  ion structure.  
651a0 54 68 69 73 20 66 6c 61 67 20 69 73 20 75 73 65  This flag is use
651b0 64 20 66 6f 72 20 56 56 26 41 20 6f 6e 6c 79 2e  d for VV&A only.
651c0 20 20 54 68 65 0a 2a 2a 20 72 6f 75 74 69 6e 65    The.** routine
651d0 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
651e0 61 73 20 61 20 6d 61 63 72 6f 20 74 68 61 74 20  as a macro that 
651f0 6f 6e 6c 79 20 77 6f 72 6b 73 20 77 68 65 6e 20  only works when 
65200 69 6e 20 64 65 62 75 67 67 69 6e 67 20 6d 6f 64  in debugging mod
65210 65 2c 0a 2a 2a 20 73 6f 20 61 73 20 6e 6f 74 20  e,.** so as not 
65220 74 6f 20 62 75 72 64 65 6e 20 70 72 6f 64 75 63  to burden produc
65230 74 69 6f 6e 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69  tion code..*/.#i
65240 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
65250 47 0a 23 20 64 65 66 69 6e 65 20 45 78 70 72 53  G.# define ExprS
65260 65 74 49 72 72 65 64 75 63 69 62 6c 65 28 58 29  etIrreducible(X)
65270 20 20 28 58 29 2d 3e 66 6c 61 67 73 32 20 7c 3d    (X)->flags2 |=
65280 20 45 50 32 5f 49 72 72 65 64 75 63 69 62 6c 65   EP2_Irreducible
65290 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
652a0 45 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62  ExprSetIrreducib
652b0 6c 65 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  le(X).#endif../*
652c0 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73  .** These macros
652d0 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
652e0 74 65 73 74 2c 20 73 65 74 2c 20 6f 72 20 63 6c  test, set, or cl
652f0 65 61 72 20 62 69 74 73 20 69 6e 20 74 68 65 20  ear bits in the 
65300 0a 2a 2a 20 45 78 70 72 2e 66 6c 61 67 73 20 66  .** Expr.flags f
65310 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ield..*/.#define
65320 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
65330 28 45 2c 50 29 20 20 20 20 20 28 28 28 45 29 2d  (E,P)     (((E)-
65340 3e 66 6c 61 67 73 26 28 50 29 29 3d 3d 28 50 29  >flags&(P))==(P)
65350 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72 48 61  ).#define ExprHa
65360 73 41 6e 79 50 72 6f 70 65 72 74 79 28 45 2c 50  sAnyProperty(E,P
65370 29 20 20 28 28 28 45 29 2d 3e 66 6c 61 67 73 26  )  (((E)->flags&
65380 28 50 29 29 21 3d 30 29 0a 23 64 65 66 69 6e 65  (P))!=0).#define
65390 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79   ExprSetProperty
653a0 28 45 2c 50 29 20 20 20 20 20 28 45 29 2d 3e 66  (E,P)     (E)->f
653b0 6c 61 67 73 7c 3d 28 50 29 0a 23 64 65 66 69 6e  lags|=(P).#defin
653c0 65 20 45 78 70 72 43 6c 65 61 72 50 72 6f 70 65  e ExprClearPrope
653d0 72 74 79 28 45 2c 50 29 20 20 20 28 45 29 2d 3e  rty(E,P)   (E)->
653e0 66 6c 61 67 73 26 3d 7e 28 50 29 0a 0a 2f 2a 0a  flags&=~(P)../*.
653f0 2a 2a 20 4d 61 63 72 6f 73 20 74 6f 20 64 65 74  ** Macros to det
65400 65 72 6d 69 6e 65 20 74 68 65 20 6e 75 6d 62 65  ermine the numbe
65410 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69  r of bytes requi
65420 72 65 64 20 62 79 20 61 20 6e 6f 72 6d 61 6c 20  red by a normal 
65430 45 78 70 72 20 0a 2a 2a 20 73 74 72 75 63 74 2c  Expr .** struct,
65440 20 61 6e 20 45 78 70 72 20 73 74 72 75 63 74 20   an Expr struct 
65450 77 69 74 68 20 74 68 65 20 45 50 5f 52 65 64 75  with the EP_Redu
65460 63 65 64 20 66 6c 61 67 20 73 65 74 20 69 6e 20  ced flag set in 
65470 45 78 70 72 2e 66 6c 61 67 73 20 0a 2a 2a 20 61  Expr.flags .** a
65480 6e 64 20 61 6e 20 45 78 70 72 20 73 74 72 75 63  nd an Expr struc
65490 74 20 77 69 74 68 20 74 68 65 20 45 50 5f 54 6f  t with the EP_To
654a0 6b 65 6e 4f 6e 6c 79 20 66 6c 61 67 20 73 65 74  kenOnly flag set
654b0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50  ..*/.#define EXP
654c0 52 5f 46 55 4c 4c 53 49 5a 45 20 20 20 20 20 20  R_FULLSIZE      
654d0 20 20 20 20 20 73 69 7a 65 6f 66 28 45 78 70 72       sizeof(Expr
654e0 29 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  )           /* F
654f0 75 6c 6c 20 73 69 7a 65 20 2a 2f 0a 23 64 65 66  ull size */.#def
65500 69 6e 65 20 45 58 50 52 5f 52 45 44 55 43 45 44  ine EXPR_REDUCED
65510 53 49 5a 45 20 20 20 20 20 20 20 20 6f 66 66 73  SIZE        offs
65520 65 74 6f 66 28 45 78 70 72 2c 69 54 61 62 6c 65  etof(Expr,iTable
65530 29 20 20 2f 2a 20 43 6f 6d 6d 6f 6e 20 66 65 61  )  /* Common fea
65540 74 75 72 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65  tures */.#define
65550 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53   EXPR_TOKENONLYS
65560 49 5a 45 20 20 20 20 20 20 6f 66 66 73 65 74 6f  IZE      offseto
65570 66 28 45 78 70 72 2c 70 4c 65 66 74 29 20 20 20  f(Expr,pLeft)   
65580 2f 2a 20 46 65 77 65 72 20 66 65 61 74 75 72 65  /* Fewer feature
65590 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 67  s */../*.** Flag
655a0 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20  s passed to the 
655b0 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 29  sqlite3ExprDup()
655c0 20 66 75 6e 63 74 69 6f 6e 2e 20 53 65 65 20 74   function. See t
655d0 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e  he header commen
655e0 74 20 0a 2a 2a 20 61 62 6f 76 65 20 73 71 6c 69  t .** above sqli
655f0 74 65 33 45 78 70 72 44 75 70 28 29 20 66 6f 72  te3ExprDup() for
65600 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65   details..*/.#de
65610 66 69 6e 65 20 45 58 50 52 44 55 50 5f 52 45 44  fine EXPRDUP_RED
65620 55 43 45 20 20 20 20 20 20 20 20 20 30 78 30 30  UCE         0x00
65630 30 31 20 20 2f 2a 20 55 73 65 64 20 72 65 64 75  01  /* Used redu
65640 63 65 64 2d 73 69 7a 65 20 45 78 70 72 20 6e 6f  ced-size Expr no
65650 64 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20  des */../*.** A 
65660 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69  list of expressi
65670 6f 6e 73 2e 20 20 45 61 63 68 20 65 78 70 72 65  ons.  Each expre
65680 73 73 69 6f 6e 20 6d 61 79 20 6f 70 74 69 6f 6e  ssion may option
65690 61 6c 6c 79 20 68 61 76 65 20 61 0a 2a 2a 20 6e  ally have a.** n
656a0 61 6d 65 2e 20 20 41 6e 20 65 78 70 72 2f 6e 61  ame.  An expr/na
656b0 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 63  me combination c
656c0 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 73 65  an be used in se
656d0 76 65 72 61 6c 20 77 61 79 73 2c 20 73 75 63 68  veral ways, such
656e0 0a 2a 2a 20 61 73 20 74 68 65 20 6c 69 73 74 20  .** as the list 
656f0 6f 66 20 22 65 78 70 72 20 41 53 20 49 44 22 20  of "expr AS ID" 
65700 66 69 65 6c 64 73 20 66 6f 6c 6c 6f 77 69 6e 67  fields following
65710 20 61 20 22 53 45 4c 45 43 54 22 20 6f 72 20 69   a "SELECT" or i
65720 6e 20 74 68 65 0a 2a 2a 20 6c 69 73 74 20 6f 66  n the.** list of
65730 20 22 49 44 20 3d 20 65 78 70 72 22 20 69 74 65   "ID = expr" ite
65740 6d 73 20 69 6e 20 61 6e 20 55 50 44 41 54 45 2e  ms in an UPDATE.
65750 20 20 41 20 6c 69 73 74 20 6f 66 20 65 78 70 72    A list of expr
65760 65 73 73 69 6f 6e 73 20 63 61 6e 0a 2a 2a 20 61  essions can.** a
65770 6c 73 6f 20 62 65 20 75 73 65 64 20 61 73 20 74  lso be used as t
65780 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 61  he argument to a
65790 20 66 75 6e 63 74 69 6f 6e 2c 20 69 6e 20 77 68   function, in wh
657a0 69 63 68 20 63 61 73 65 20 74 68 65 20 61 2e 7a  ich case the a.z
657b0 4e 61 6d 65 0a 2a 2a 20 66 69 65 6c 64 20 69 73  Name.** field is
657c0 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 73 74   not used..*/.st
657d0 72 75 63 74 20 45 78 70 72 4c 69 73 74 20 7b 0a  ruct ExprList {.
657e0 20 20 69 6e 74 20 6e 45 78 70 72 3b 20 20 20 20    int nExpr;    
657f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
65800 65 72 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e  er of expression
65810 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f  s on the list */
65820 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20  .  int nAlloc;  
65830 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
65840 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61  ber of entries a
65850 6c 6c 6f 63 61 74 65 64 20 62 65 6c 6f 77 20 2a  llocated below *
65860 2f 0a 20 20 69 6e 74 20 69 45 43 75 72 73 6f 72  /.  int iECursor
65870 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44  ;          /* VD
65880 42 45 20 43 75 72 73 6f 72 20 61 73 73 6f 63 69  BE Cursor associ
65890 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 45  ated with this E
658a0 78 70 72 4c 69 73 74 20 2a 2f 0a 20 20 73 74 72  xprList */.  str
658b0 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
658c0 6d 20 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45  m {.    Expr *pE
658d0 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f  xpr;           /
658e0 2a 20 54 68 65 20 6c 69 73 74 20 6f 66 20 65 78  * The list of ex
658f0 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 20  pressions */.   
65900 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
65910 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e          /* Token
65920 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
65930 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e   this expression
65940 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 53   */.    char *zS
65950 70 61 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f  pan;           /
65960 2a 20 4f 72 69 67 69 6e 61 6c 20 74 65 78 74 20  * Original text 
65970 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
65980 6e 20 2a 2f 0a 20 20 20 20 75 38 20 73 6f 72 74  n */.    u8 sort
65990 4f 72 64 65 72 3b 20 20 20 20 20 20 20 20 20 20  Order;          
659a0 2f 2a 20 31 20 66 6f 72 20 44 45 53 43 20 6f 72  /* 1 for DESC or
659b0 20 30 20 66 6f 72 20 41 53 43 20 2a 2f 0a 20 20   0 for ASC */.  
659c0 20 20 75 38 20 64 6f 6e 65 3b 20 20 20 20 20 20    u8 done;      
659d0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 66 6c           /* A fl
659e0 61 67 20 74 6f 20 69 6e 64 69 63 61 74 65 20 77  ag to indicate w
659f0 68 65 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 69  hen processing i
65a00 73 20 66 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20  s finished */.  
65a10 20 20 75 31 36 20 69 43 6f 6c 3b 20 20 20 20 20    u16 iCol;     
65a20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
65a30 4f 52 44 45 52 20 42 59 2c 20 63 6f 6c 75 6d 6e  ORDER BY, column
65a40 20 6e 75 6d 62 65 72 20 69 6e 20 72 65 73 75 6c   number in resul
65a50 74 20 73 65 74 20 2a 2f 0a 20 20 20 20 75 31 36  t set */.    u16
65a60 20 69 41 6c 69 61 73 3b 20 20 20 20 20 20 20 20   iAlias;        
65a70 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74      /* Index int
65a80 6f 20 50 61 72 73 65 2e 61 41 6c 69 61 73 5b 5d  o Parse.aAlias[]
65a90 20 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20   for zName */.  
65aa0 7d 20 2a 61 3b 20 20 20 20 20 20 20 20 20 20 20  } *a;           
65ab0 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e         /* One en
65ac0 74 72 79 20 66 6f 72 20 65 61 63 68 20 65 78 70  try for each exp
65ad0 72 65 73 73 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f  ression */.};../
65ae0 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
65af0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
65b00 72 65 20 69 73 20 75 73 65 64 20 62 79 20 74 68  re is used by th
65b10 65 20 70 61 72 73 65 72 20 74 6f 20 72 65 63 6f  e parser to reco
65b20 72 64 20 62 6f 74 68 0a 2a 2a 20 74 68 65 20 70  rd both.** the p
65b30 61 72 73 65 20 74 72 65 65 20 66 6f 72 20 61 6e  arse tree for an
65b40 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20   expression and 
65b50 74 68 65 20 73 70 61 6e 20 6f 66 20 69 6e 70 75  the span of inpu
65b60 74 20 74 65 78 74 20 66 6f 72 20 61 6e 0a 2a 2a  t text for an.**
65b70 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a   expression..*/.
65b80 73 74 72 75 63 74 20 45 78 70 72 53 70 61 6e 20  struct ExprSpan 
65b90 7b 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b  {.  Expr *pExpr;
65ba0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
65bb0 20 65 78 70 72 65 73 73 69 6f 6e 20 70 61 72 73   expression pars
65bc0 65 20 74 72 65 65 20 2a 2f 0a 20 20 63 6f 6e 73  e tree */.  cons
65bd0 74 20 63 68 61 72 20 2a 7a 53 74 61 72 74 3b 20  t char *zStart; 
65be0 20 20 2f 2a 20 46 69 72 73 74 20 63 68 61 72 61    /* First chara
65bf0 63 74 65 72 20 6f 66 20 69 6e 70 75 74 20 74 65  cter of input te
65c00 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  xt */.  const ch
65c10 61 72 20 2a 7a 45 6e 64 3b 20 20 20 20 20 2f 2a  ar *zEnd;     /*
65c20 20 4f 6e 65 20 63 68 61 72 61 63 74 65 72 20 70   One character p
65c30 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 69  ast the end of i
65c40 6e 70 75 74 20 74 65 78 74 20 2a 2f 0a 7d 3b 0a  nput text */.};.
65c50 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
65c60 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ce of this struc
65c70 74 75 72 65 20 63 61 6e 20 68 6f 6c 64 20 61 20  ture can hold a 
65c80 73 69 6d 70 6c 65 20 6c 69 73 74 20 6f 66 20 69  simple list of i
65c90 64 65 6e 74 69 66 69 65 72 73 2c 0a 2a 2a 20 73  dentifiers,.** s
65ca0 75 63 68 20 61 73 20 74 68 65 20 6c 69 73 74 20  uch as the list 
65cb0 22 61 2c 62 2c 63 22 20 69 6e 20 74 68 65 20 66  "a,b,c" in the f
65cc0 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65  ollowing stateme
65cd0 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  nts:.**.**      
65ce0 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 28 61 2c  INSERT INTO t(a,
65cf0 62 2c 63 29 20 56 41 4c 55 45 53 20 2e 2e 2e 3b  b,c) VALUES ...;
65d00 0a 2a 2a 20 20 20 20 20 20 43 52 45 41 54 45 20  .**      CREATE 
65d10 49 4e 44 45 58 20 69 64 78 20 4f 4e 20 74 28 61  INDEX idx ON t(a
65d20 2c 62 2c 63 29 3b 0a 2a 2a 20 20 20 20 20 20 43  ,b,c);.**      C
65d30 52 45 41 54 45 20 54 52 49 47 47 45 52 20 74 72  REATE TRIGGER tr
65d40 69 67 20 42 45 46 4f 52 45 20 55 50 44 41 54 45  ig BEFORE UPDATE
65d50 20 4f 4e 20 74 28 61 2c 62 2c 63 29 20 2e 2e 2e   ON t(a,b,c) ...
65d60 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 64 4c 69  ;.**.** The IdLi
65d70 73 74 2e 61 2e 69 64 78 20 66 69 65 6c 64 20 69  st.a.idx field i
65d80 73 20 75 73 65 64 20 77 68 65 6e 20 74 68 65 20  s used when the 
65d90 49 64 4c 69 73 74 20 72 65 70 72 65 73 65 6e 74  IdList represent
65da0 73 20 74 68 65 20 6c 69 73 74 20 6f 66 0a 2a 2a  s the list of.**
65db0 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 66   column names af
65dc0 74 65 72 20 61 20 74 61 62 6c 65 20 6e 61 6d 65  ter a table name
65dd0 20 69 6e 20 61 6e 20 49 4e 53 45 52 54 20 73 74   in an INSERT st
65de0 61 74 65 6d 65 6e 74 2e 20 20 49 6e 20 74 68 65  atement.  In the
65df0 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a   statement.**.**
65e00 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
65e10 20 74 28 61 2c 62 2c 63 29 20 2e 2e 2e 0a 2a 2a   t(a,b,c) ....**
65e20 0a 2a 2a 20 49 66 20 22 61 22 20 69 73 20 74 68  .** If "a" is th
65e30 65 20 6b 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  e k-th column of
65e40 20 74 61 62 6c 65 20 22 74 22 2c 20 74 68 65 6e   table "t", then
65e50 20 49 64 4c 69 73 74 2e 61 5b 30 5d 2e 69 64 78   IdList.a[0].idx
65e60 3d 3d 6b 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49  ==k..*/.struct I
65e70 64 4c 69 73 74 20 7b 0a 20 20 73 74 72 75 63 74  dList {.  struct
65e80 20 49 64 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20   IdList_item {. 
65e90 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20     char *zName; 
65ea0 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
65eb0 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 20 2a  the identifier *
65ec0 2f 0a 20 20 20 20 69 6e 74 20 69 64 78 3b 20 20  /.    int idx;  
65ed0 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
65ee0 20 69 6e 20 73 6f 6d 65 20 54 61 62 6c 65 2e 61   in some Table.a
65ef0 43 6f 6c 5b 5d 20 6f 66 20 61 20 63 6f 6c 75 6d  Col[] of a colum
65f00 6e 20 6e 61 6d 65 64 20 7a 4e 61 6d 65 20 2a 2f  n named zName */
65f10 0a 20 20 7d 20 2a 61 3b 0a 20 20 69 6e 74 20 6e  .  } *a;.  int n
65f20 49 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  Id;         /* N
65f30 75 6d 62 65 72 20 6f 66 20 69 64 65 6e 74 69 66  umber of identif
65f40 69 65 72 73 20 6f 6e 20 74 68 65 20 6c 69 73 74  iers on the list
65f50 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63   */.  int nAlloc
65f60 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  ;      /* Number
65f70 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f   of entries allo
65f80 63 61 74 65 64 20 66 6f 72 20 61 5b 5d 20 62 65  cated for a[] be
65f90 6c 6f 77 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  low */.};../*.**
65fa0 20 54 68 65 20 62 69 74 6d 61 73 6b 20 64 61 74   The bitmask dat
65fb0 61 74 79 70 65 20 64 65 66 69 6e 65 64 20 62 65  atype defined be
65fc0 6c 6f 77 20 69 73 20 75 73 65 64 20 66 6f 72 20  low is used for 
65fd0 76 61 72 69 6f 75 73 20 6f 70 74 69 6d 69 7a 61  various optimiza
65fe0 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43 68 61  tions..**.** Cha
65ff0 6e 67 69 6e 67 20 74 68 69 73 20 66 72 6f 6d 20  nging this from 
66000 61 20 36 34 2d 62 69 74 20 74 6f 20 61 20 33 32  a 64-bit to a 32
66010 2d 62 69 74 20 74 79 70 65 20 6c 69 6d 69 74 73  -bit type limits
66020 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
66030 2a 20 74 61 62 6c 65 73 20 69 6e 20 61 20 6a 6f  * tables in a jo
66040 69 6e 20 74 6f 20 33 32 20 69 6e 73 74 65 61 64  in to 32 instead
66050 20 6f 66 20 36 34 2e 20 20 42 75 74 20 69 74 20   of 64.  But it 
66060 61 6c 73 6f 20 72 65 64 75 63 65 73 20 74 68 65  also reduces the
66070 20 73 69 7a 65 0a 2a 2a 20 6f 66 20 74 68 65 20   size.** of the 
66080 6c 69 62 72 61 72 79 20 62 79 20 37 33 38 20 62  library by 738 b
66090 79 74 65 73 20 6f 6e 20 69 78 38 36 2e 0a 2a 2f  ytes on ix86..*/
660a0 0a 74 79 70 65 64 65 66 20 75 36 34 20 42 69 74  .typedef u64 Bit
660b0 6d 61 73 6b 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  mask;../*.** The
660c0 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20   number of bits 
660d0 69 6e 20 61 20 42 69 74 6d 61 73 6b 2e 20 20 22  in a Bitmask.  "
660e0 42 4d 53 22 20 6d 65 61 6e 73 20 22 42 69 74 4d  BMS" means "BitM
660f0 61 73 6b 20 53 69 7a 65 22 2e 0a 2a 2f 0a 23 64  ask Size"..*/.#d
66100 65 66 69 6e 65 20 42 4d 53 20 20 28 28 69 6e 74  efine BMS  ((int
66110 29 28 73 69 7a 65 6f 66 28 42 69 74 6d 61 73 6b  )(sizeof(Bitmask
66120 29 2a 38 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  )*8))../*.** The
66130 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
66140 74 75 72 65 20 64 65 73 63 72 69 62 65 73 20 74  ture describes t
66150 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  he FROM clause o
66160 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65  f a SELECT state
66170 6d 65 6e 74 2e 0a 2a 2a 20 45 61 63 68 20 74 61  ment..** Each ta
66180 62 6c 65 20 6f 72 20 73 75 62 71 75 65 72 79 20  ble or subquery 
66190 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
661a0 73 65 20 69 73 20 61 20 73 65 70 61 72 61 74 65  se is a separate
661b0 20 65 6c 65 6d 65 6e 74 20 6f 66 0a 2a 2a 20 74   element of.** t
661c0 68 65 20 53 72 63 4c 69 73 74 2e 61 5b 5d 20 61  he SrcList.a[] a
661d0 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68  rray..**.** With
661e0 20 74 68 65 20 61 64 64 69 74 69 6f 6e 20 6f 66   the addition of
661f0 20 6d 75 6c 74 69 70 6c 65 20 64 61 74 61 62 61   multiple databa
66200 73 65 20 73 75 70 70 6f 72 74 2c 20 74 68 65 20  se support, the 
66210 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
66220 75 72 65 0a 2a 2a 20 63 61 6e 20 61 6c 73 6f 20  ure.** can also 
66230 62 65 20 75 73 65 64 20 74 6f 20 64 65 73 63 72  be used to descr
66240 69 62 65 20 61 20 70 61 72 74 69 63 75 6c 61 72  ibe a particular
66250 20 74 61 62 6c 65 20 73 75 63 68 20 61 73 20 74   table such as t
66260 68 65 20 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a  he table that.**
66270 20 69 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20   is modified by 
66280 61 6e 20 49 4e 53 45 52 54 2c 20 44 45 4c 45 54  an INSERT, DELET
66290 45 2c 20 6f 72 20 55 50 44 41 54 45 20 73 74 61  E, or UPDATE sta
662a0 74 65 6d 65 6e 74 2e 20 20 49 6e 20 73 74 61 6e  tement.  In stan
662b0 64 61 72 64 20 53 51 4c 2c 0a 2a 2a 20 73 75 63  dard SQL,.** suc
662c0 68 20 61 20 74 61 62 6c 65 20 6d 75 73 74 20 62  h a table must b
662d0 65 20 61 20 73 69 6d 70 6c 65 20 6e 61 6d 65 3a  e a simple name:
662e0 20 49 44 2e 20 20 42 75 74 20 69 6e 20 53 51 4c   ID.  But in SQL
662f0 69 74 65 2c 20 74 68 65 20 74 61 62 6c 65 20 63  ite, the table c
66300 61 6e 0a 2a 2a 20 6e 6f 77 20 62 65 20 69 64 65  an.** now be ide
66310 6e 74 69 66 69 65 64 20 62 79 20 61 20 64 61 74  ntified by a dat
66320 61 62 61 73 65 20 6e 61 6d 65 2c 20 61 20 64 6f  abase name, a do
66330 74 2c 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c  t, then the tabl
66340 65 20 6e 61 6d 65 3a 20 49 44 2e 49 44 2e 0a 2a  e name: ID.ID..*
66350 2a 0a 2a 2a 20 54 68 65 20 6a 6f 69 6e 74 79 70  *.** The jointyp
66360 65 20 73 74 61 72 74 73 20 6f 75 74 20 73 68 6f  e starts out sho
66370 77 69 6e 67 20 74 68 65 20 6a 6f 69 6e 20 74 79  wing the join ty
66380 70 65 20 62 65 74 77 65 65 6e 20 74 68 65 20 63  pe between the c
66390 75 72 72 65 6e 74 20 74 61 62 6c 65 0a 2a 2a 20  urrent table.** 
663a0 61 6e 64 20 74 68 65 20 6e 65 78 74 20 74 61 62  and the next tab
663b0 6c 65 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e 20  le on the list. 
663c0 20 54 68 65 20 70 61 72 73 65 72 20 62 75 69 6c   The parser buil
663d0 64 73 20 74 68 65 20 6c 69 73 74 20 74 68 69 73  ds the list this
663e0 20 77 61 79 2e 0a 2a 2a 20 42 75 74 20 73 71 6c   way..** But sql
663f0 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74  ite3SrcListShift
66400 4a 6f 69 6e 54 79 70 65 28 29 20 6c 61 74 65 72  JoinType() later
66410 20 73 68 69 66 74 73 20 74 68 65 20 6a 6f 69 6e   shifts the join
66420 74 79 70 65 73 20 73 6f 20 74 68 61 74 20 65 61  types so that ea
66430 63 68 0a 2a 2a 20 6a 6f 69 6e 74 79 70 65 20 65  ch.** jointype e
66440 78 70 72 65 73 73 65 73 20 74 68 65 20 6a 6f 69  xpresses the joi
66450 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 74 61  n between the ta
66460 62 6c 65 20 61 6e 64 20 74 68 65 20 70 72 65 76  ble and the prev
66470 69 6f 75 73 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73  ious table..*/.s
66480 74 72 75 63 74 20 53 72 63 4c 69 73 74 20 7b 0a  truct SrcList {.
66490 20 20 69 31 36 20 6e 53 72 63 3b 20 20 20 20 20    i16 nSrc;     
664a0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
664b0 74 61 62 6c 65 73 20 6f 72 20 73 75 62 71 75 65  tables or subque
664c0 72 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d  ries in the FROM
664d0 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 31 36   clause */.  i16
664e0 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a   nAlloc;      /*
664f0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
66500 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20  es allocated in 
66510 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 73  a[] below */.  s
66520 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
66530 65 6d 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  em {.    char *z
66540 44 61 74 61 62 61 73 65 3b 20 20 2f 2a 20 4e 61  Database;  /* Na
66550 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 68  me of database h
66560 6f 6c 64 69 6e 67 20 74 68 69 73 20 74 61 62 6c  olding this tabl
66570 65 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a  e */.    char *z
66580 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 4e 61  Name;      /* Na
66590 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
665a0 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 41 6c  */.    char *zAl
665b0 69 61 73 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  ias;     /* The 
665c0 22 42 22 20 70 61 72 74 20 6f 66 20 61 20 22 41  "B" part of a "A
665d0 20 41 53 20 42 22 20 70 68 72 61 73 65 2e 20 20   AS B" phrase.  
665e0 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 22 41 22  zName is the "A"
665f0 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65 20 2a 70   */.    Table *p
66600 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 41 6e 20  Tab;      /* An 
66610 53 51 4c 20 74 61 62 6c 65 20 63 6f 72 72 65 73  SQL table corres
66620 70 6f 6e 64 69 6e 67 20 74 6f 20 7a 4e 61 6d 65  ponding to zName
66630 20 2a 2f 0a 20 20 20 20 53 65 6c 65 63 74 20 2a   */.    Select *
66640 70 53 65 6c 65 63 74 3b 20 20 2f 2a 20 41 20 53  pSelect;  /* A S
66650 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
66660 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66  used in place of
66670 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f   a table name */
66680 0a 20 20 20 20 75 38 20 69 73 50 6f 70 75 6c 61  .    u8 isPopula
66690 74 65 64 3b 20 20 20 2f 2a 20 54 65 6d 70 6f 72  ted;   /* Tempor
666a0 61 72 79 20 74 61 62 6c 65 20 61 73 73 6f 63 69  ary table associ
666b0 61 74 65 64 20 77 69 74 68 20 53 45 4c 45 43 54  ated with SELECT
666c0 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 2a 2f   is populated */
666d0 0a 20 20 20 20 75 38 20 6a 6f 69 6e 74 79 70 65  .    u8 jointype
666e0 3b 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f  ;      /* Type o
666f0 66 20 6a 6f 69 6e 20 62 65 74 77 65 65 6e 20 74  f join between t
66700 68 69 73 20 61 62 6c 65 20 61 6e 64 20 74 68 65  his able and the
66710 20 70 72 65 76 69 6f 75 73 20 2a 2f 0a 20 20 20   previous */.   
66720 20 75 38 20 6e 6f 74 49 6e 64 65 78 65 64 3b 20   u8 notIndexed; 
66730 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
66740 65 72 65 20 69 73 20 61 20 4e 4f 54 20 49 4e 44  ere is a NOT IND
66750 45 58 45 44 20 63 6c 61 75 73 65 20 2a 2f 0a 20  EXED clause */. 
66760 20 20 20 69 6e 74 20 69 43 75 72 73 6f 72 3b 20     int iCursor; 
66770 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45       /* The VDBE
66780 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 75   cursor number u
66790 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68  sed to access th
667a0 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  is table */.    
667b0 45 78 70 72 20 2a 70 4f 6e 3b 20 20 20 20 20 20  Expr *pOn;      
667c0 20 20 2f 2a 20 54 68 65 20 4f 4e 20 63 6c 61 75    /* The ON clau
667d0 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a  se of a join */.
667e0 20 20 20 20 49 64 4c 69 73 74 20 2a 70 55 73 69      IdList *pUsi
667f0 6e 67 3b 20 20 20 2f 2a 20 54 68 65 20 55 53 49  ng;   /* The USI
66800 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a  NG clause of a j
66810 6f 69 6e 20 2a 2f 0a 20 20 20 20 42 69 74 6d 61  oin */.    Bitma
66820 73 6b 20 63 6f 6c 55 73 65 64 3b 20 20 2f 2a 20  sk colUsed;  /* 
66830 42 69 74 20 4e 20 28 31 3c 3c 4e 29 20 73 65 74  Bit N (1<<N) set
66840 20 69 66 20 63 6f 6c 75 6d 6e 20 4e 20 6f 66 20   if column N of 
66850 70 54 61 62 20 69 73 20 75 73 65 64 20 2a 2f 0a  pTab is used */.
66860 20 20 20 20 63 68 61 72 20 2a 7a 49 6e 64 65 78      char *zIndex
66870 3b 20 20 20 20 20 2f 2a 20 49 64 65 6e 74 69 66  ;     /* Identif
66880 69 65 72 20 66 72 6f 6d 20 22 49 4e 44 45 58 45  ier from "INDEXE
66890 44 20 42 59 20 3c 7a 49 6e 64 65 78 3e 22 20 63  D BY <zIndex>" c
668a0 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 49 6e 64  lause */.    Ind
668b0 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 20 2f  ex *pIndex;    /
668c0 2a 20 49 6e 64 65 78 20 73 74 72 75 63 74 75 72  * Index structur
668d0 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
668e0 74 6f 20 7a 49 6e 64 65 78 2c 20 69 66 20 61 6e  to zIndex, if an
668f0 79 20 2a 2f 0a 20 20 7d 20 61 5b 31 5d 3b 20 20  y */.  } a[1];  
66900 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
66910 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68  e entry for each
66920 20 69 64 65 6e 74 69 66 69 65 72 20 6f 6e 20 74   identifier on t
66930 68 65 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f  he list */.};../
66940 2a 0a 2a 2a 20 50 65 72 6d 69 74 74 65 64 20 76  *.** Permitted v
66950 61 6c 75 65 73 20 6f 66 20 74 68 65 20 53 72 63  alues of the Src
66960 4c 69 73 74 2e 61 2e 6a 6f 69 6e 74 79 70 65 20  List.a.jointype 
66970 66 69 65 6c 64 0a 2a 2f 0a 23 64 65 66 69 6e 65  field.*/.#define
66980 20 4a 54 5f 49 4e 4e 45 52 20 20 20 20 20 30 78   JT_INNER     0x
66990 30 30 30 31 20 20 20 20 2f 2a 20 41 6e 79 20 6b  0001    /* Any k
669a0 69 6e 64 20 6f 66 20 69 6e 6e 65 72 20 6f 72 20  ind of inner or 
669b0 63 72 6f 73 73 20 6a 6f 69 6e 20 2a 2f 0a 23 64  cross join */.#d
669c0 65 66 69 6e 65 20 4a 54 5f 43 52 4f 53 53 20 20  efine JT_CROSS  
669d0 20 20 20 30 78 30 30 30 32 20 20 20 20 2f 2a 20     0x0002    /* 
669e0 45 78 70 6c 69 63 69 74 20 75 73 65 20 6f 66 20  Explicit use of 
669f0 74 68 65 20 43 52 4f 53 53 20 6b 65 79 77 6f 72  the CROSS keywor
66a00 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f  d */.#define JT_
66a10 4e 41 54 55 52 41 4c 20 20 20 30 78 30 30 30 34  NATURAL   0x0004
66a20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
66a30 61 20 22 6e 61 74 75 72 61 6c 22 20 6a 6f 69 6e  a "natural" join
66a40 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 4c   */.#define JT_L
66a50 45 46 54 20 20 20 20 20 20 30 78 30 30 30 38 20  EFT      0x0008 
66a60 20 20 20 2f 2a 20 4c 65 66 74 20 6f 75 74 65 72     /* Left outer
66a70 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65   join */.#define
66a80 20 4a 54 5f 52 49 47 48 54 20 20 20 20 20 30 78   JT_RIGHT     0x
66a90 30 30 31 30 20 20 20 20 2f 2a 20 52 69 67 68 74  0010    /* Right
66aa0 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f 0a 23   outer join */.#
66ab0 64 65 66 69 6e 65 20 4a 54 5f 4f 55 54 45 52 20  define JT_OUTER 
66ac0 20 20 20 20 30 78 30 30 32 30 20 20 20 20 2f 2a      0x0020    /*
66ad0 20 54 68 65 20 22 4f 55 54 45 52 22 20 6b 65 79   The "OUTER" key
66ae0 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20  word is present 
66af0 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 45 52  */.#define JT_ER
66b00 52 4f 52 20 20 20 20 20 30 78 30 30 34 30 20 20  ROR     0x0040  
66b10 20 20 2f 2a 20 75 6e 6b 6e 6f 77 6e 20 6f 72 20    /* unknown or 
66b20 75 6e 73 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e  unsupported join
66b30 20 74 79 70 65 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a   type */.../*.**
66b40 20 41 20 57 68 65 72 65 50 6c 61 6e 20 6f 62 6a   A WherePlan obj
66b50 65 63 74 20 68 6f 6c 64 73 20 69 6e 66 6f 72 6d  ect holds inform
66b60 61 74 69 6f 6e 20 74 68 61 74 20 64 65 73 63 72  ation that descr
66b70 69 62 65 73 20 61 20 6c 6f 6f 6b 75 70 0a 2a 2a  ibes a lookup.**
66b80 20 73 74 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a   strategy..**.**
66b90 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73 20   This object is 
66ba0 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f  intended to be o
66bb0 70 61 71 75 65 20 6f 75 74 73 69 64 65 20 6f 66  paque outside of
66bc0 20 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64   the where.c mod
66bd0 75 6c 65 2e 0a 2a 2a 20 49 74 20 69 73 20 69 6e  ule..** It is in
66be0 63 6c 75 64 65 64 20 68 65 72 65 20 6f 6e 6c 79  cluded here only
66bf0 20 73 6f 20 74 68 61 74 20 74 68 61 74 20 63 6f   so that that co
66c00 6d 70 69 6c 65 72 20 77 69 6c 6c 20 6b 6e 6f 77  mpiler will know
66c10 20 68 6f 77 20 62 69 67 20 69 74 0a 2a 2a 20 69   how big it.** i
66c20 73 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 65 20  s.  None of the 
66c30 66 69 65 6c 64 73 20 69 6e 20 74 68 69 73 20 6f  fields in this o
66c40 62 6a 65 63 74 20 73 68 6f 75 6c 64 20 62 65 20  bject should be 
66c50 75 73 65 64 20 6f 75 74 73 69 64 65 20 6f 66 0a  used outside of.
66c60 2a 2a 20 74 68 65 20 77 68 65 72 65 2e 63 20 6d  ** the where.c m
66c70 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 69 74  odule..**.** Wit
66c80 68 69 6e 20 74 68 65 20 75 6e 69 6f 6e 2c 20 70  hin the union, p
66c90 49 64 78 20 69 73 20 6f 6e 6c 79 20 75 73 65 64  Idx is only used
66ca0 20 77 68 65 6e 20 77 73 46 6c 61 67 73 26 57 48   when wsFlags&WH
66cb0 45 52 45 5f 49 4e 44 45 58 45 44 20 69 73 20 74  ERE_INDEXED is t
66cc0 72 75 65 2e 0a 2a 2a 20 70 54 65 72 6d 20 69 73  rue..** pTerm is
66cd0 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20   only used when 
66ce0 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 4d 55  wsFlags&WHERE_MU
66cf0 4c 54 49 5f 4f 52 20 69 73 20 74 72 75 65 2e 20  LTI_OR is true. 
66d00 20 41 6e 64 20 70 56 74 61 62 49 64 78 0a 2a 2a   And pVtabIdx.**
66d10 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68   is only used wh
66d20 65 6e 20 77 73 46 6c 61 67 73 26 57 48 45 52 45  en wsFlags&WHERE
66d30 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 69 73  _VIRTUALTABLE is
66d40 20 74 72 75 65 2e 20 20 49 74 20 69 73 20 6e 65   true.  It is ne
66d50 76 65 72 20 74 68 65 0a 2a 2a 20 63 61 73 65 20  ver the.** case 
66d60 74 68 61 74 20 6d 6f 72 65 20 74 68 61 6e 20 6f  that more than o
66d70 6e 65 20 6f 66 20 74 68 65 73 65 20 63 6f 6e 64  ne of these cond
66d80 69 74 69 6f 6e 73 20 69 73 20 74 72 75 65 2e 0a  itions is true..
66d90 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 50  */.struct WhereP
66da0 6c 61 6e 20 7b 0a 20 20 75 33 32 20 77 73 46 6c  lan {.  u32 wsFl
66db0 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
66dc0 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 5f         /* WHERE_
66dd0 2a 20 66 6c 61 67 73 20 74 68 61 74 20 64 65 73  * flags that des
66de0 63 72 69 62 65 20 74 68 65 20 73 74 72 61 74 65  cribe the strate
66df0 67 79 20 2a 2f 0a 20 20 75 33 32 20 6e 45 71 3b  gy */.  u32 nEq;
66e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
66e10 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
66e20 20 6f 66 20 3d 3d 20 63 6f 6e 73 74 72 61 69 6e   of == constrain
66e30 74 73 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a  ts */.  union {.
66e40 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b      Index *pIdx;
66e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
66e60 20 20 20 2f 2a 20 49 6e 64 65 78 20 77 68 65 6e     /* Index when
66e70 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44 20 69   WHERE_INDEXED i
66e80 73 20 74 72 75 65 20 2a 2f 0a 20 20 20 20 73 74  s true */.    st
66e90 72 75 63 74 20 57 68 65 72 65 54 65 72 6d 20 2a  ruct WhereTerm *
66ea0 70 54 65 72 6d 3b 20 20 20 20 20 20 20 2f 2a 20  pTerm;       /* 
66eb0 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72  WHERE clause ter
66ec0 6d 20 66 6f 72 20 4f 52 2d 73 65 61 72 63 68 20  m for OR-search 
66ed0 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69  */.    sqlite3_i
66ee0 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 56 74 61 62  ndex_info *pVtab
66ef0 49 64 78 3b 20 20 2f 2a 20 56 69 72 74 75 61 6c  Idx;  /* Virtual
66f00 20 74 61 62 6c 65 20 69 6e 64 65 78 20 74 6f 20   table index to 
66f10 75 73 65 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d 3b  use */.  } u;.};
66f20 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65 61 63 68  ../*.** For each
66f30 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20 69 6e 20   nested loop in 
66f40 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69  a WHERE clause i
66f50 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 74  mplementation, t
66f60 68 65 20 57 68 65 72 65 49 6e 66 6f 0a 2a 2a 20  he WhereInfo.** 
66f70 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
66f80 6e 73 20 61 20 73 69 6e 67 6c 65 20 69 6e 73 74  ns a single inst
66f90 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
66fa0 75 63 74 75 72 65 2e 20 20 54 68 69 73 20 73 74  ucture.  This st
66fb0 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 69 6e  ructure.** is in
66fc0 74 65 6e 64 65 64 20 74 6f 20 62 65 20 70 72 69  tended to be pri
66fd0 76 61 74 65 20 74 68 65 20 74 68 65 20 77 68 65  vate the the whe
66fe0 72 65 2e 63 20 6d 6f 64 75 6c 65 20 61 6e 64 20  re.c module and 
66ff0 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a  should not be.**
67000 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66   access or modif
67010 69 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64  ied by other mod
67020 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ules..**.** The 
67030 70 49 64 78 49 6e 66 6f 20 66 69 65 6c 64 20 69  pIdxInfo field i
67040 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 70  s used to help p
67050 69 63 6b 20 74 68 65 20 62 65 73 74 20 69 6e 64  ick the best ind
67060 65 78 20 6f 6e 20 61 0a 2a 2a 20 76 69 72 74 75  ex on a.** virtu
67070 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20 70  al table.  The p
67080 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20  IdxInfo pointer 
67090 63 6f 6e 74 61 69 6e 73 20 69 6e 64 65 78 69 6e  contains indexin
670a0 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  g.** information
670b0 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61   for the i-th ta
670c0 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ble in the FROM 
670d0 63 6c 61 75 73 65 20 62 65 66 6f 72 65 20 72 65  clause before re
670e0 6f 72 64 65 72 69 6e 67 2e 0a 2a 2a 20 41 6c 6c  ordering..** All
670f0 20 74 68 65 20 70 49 64 78 49 6e 66 6f 20 70 6f   the pIdxInfo po
67100 69 6e 74 65 72 73 20 61 72 65 20 66 72 65 65 64  inters are freed
67110 20 62 79 20 77 68 65 72 65 49 6e 66 6f 46 72 65   by whereInfoFre
67120 65 28 29 20 69 6e 20 77 68 65 72 65 2e 63 2e 0a  e() in where.c..
67130 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 69 6e 66  ** All other inf
67140 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20  ormation in the 
67150 69 2d 74 68 20 57 68 65 72 65 4c 65 76 65 6c 20  i-th WhereLevel 
67160 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 69  object for the i
67170 2d 74 68 20 74 61 62 6c 65 0a 2a 2a 20 61 66 74  -th table.** aft
67180 65 72 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  er FROM clause o
67190 72 64 65 72 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75  rdering..*/.stru
671a0 63 74 20 57 68 65 72 65 4c 65 76 65 6c 20 7b 0a  ct WhereLevel {.
671b0 20 20 57 68 65 72 65 50 6c 61 6e 20 70 6c 61 6e    WherePlan plan
671c0 3b 20 20 20 20 20 20 20 2f 2a 20 71 75 65 72 79  ;       /* query
671d0 20 70 6c 61 6e 20 66 6f 72 20 74 68 69 73 20 65   plan for this e
671e0 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 46 52  lement of the FR
671f0 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  OM clause */.  i
67200 6e 74 20 69 4c 65 66 74 4a 6f 69 6e 3b 20 20 20  nt iLeftJoin;   
67210 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63       /* Memory c
67220 65 6c 6c 20 75 73 65 64 20 74 6f 20 69 6d 70 6c  ell used to impl
67230 65 6d 65 6e 74 20 4c 45 46 54 20 4f 55 54 45 52  ement LEFT OUTER
67240 20 4a 4f 49 4e 20 2a 2f 0a 20 20 69 6e 74 20 69   JOIN */.  int i
67250 54 61 62 43 75 72 3b 20 20 20 20 20 20 20 20 20  TabCur;         
67260 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72   /* The VDBE cur
67270 73 6f 72 20 75 73 65 64 20 74 6f 20 61 63 63 65  sor used to acce
67280 73 73 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  ss the table */.
67290 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b 20 20    int iIdxCur;  
672a0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
672b0 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 64 20  DBE cursor used 
672c0 74 6f 20 61 63 63 65 73 73 20 70 49 64 78 20 2a  to access pIdx *
672d0 2f 0a 20 20 69 6e 74 20 61 64 64 72 42 72 6b 3b  /.  int addrBrk;
672e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d            /* Jum
672f0 70 20 68 65 72 65 20 74 6f 20 62 72 65 61 6b 20  p here to break 
67300 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  out of the loop 
67310 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4e 78 74  */.  int addrNxt
67320 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75  ;          /* Ju
67330 6d 70 20 68 65 72 65 20 74 6f 20 73 74 61 72 74  mp here to start
67340 20 74 68 65 20 6e 65 78 74 20 49 4e 20 63 6f 6d   the next IN com
67350 62 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  bination */.  in
67360 74 20 61 64 64 72 43 6f 6e 74 3b 20 20 20 20 20  t addrCont;     
67370 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65      /* Jump here
67380 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74   to continue wit
67390 68 20 74 68 65 20 6e 65 78 74 20 6c 6f 6f 70 20  h the next loop 
673a0 63 79 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 61  cycle */.  int a
673b0 64 64 72 46 69 72 73 74 3b 20 20 20 20 20 20 20  ddrFirst;       
673c0 20 2f 2a 20 46 69 72 73 74 20 69 6e 73 74 72 75   /* First instru
673d0 63 74 69 6f 6e 20 6f 66 20 69 6e 74 65 72 69 6f  ction of interio
673e0 72 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f  r of the loop */
673f0 0a 20 20 75 38 20 69 46 72 6f 6d 3b 20 20 20 20  .  u8 iFrom;    
67400 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63           /* Whic
67410 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 46  h entry in the F
67420 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ROM clause */.  
67430 75 38 20 6f 70 2c 20 70 35 3b 20 20 20 20 20 20  u8 op, p5;      
67440 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20        /* Opcode 
67450 61 6e 64 20 50 35 20 6f 66 20 74 68 65 20 6f 70  and P5 of the op
67460 63 6f 64 65 20 74 68 61 74 20 65 6e 64 73 20 74  code that ends t
67470 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  he loop */.  int
67480 20 70 31 2c 20 70 32 3b 20 20 20 20 20 20 20 20   p1, p2;        
67490 20 20 20 2f 2a 20 4f 70 65 72 61 6e 64 73 20 6f     /* Operands o
674a0 66 20 74 68 65 20 6f 70 63 6f 64 65 20 75 73 65  f the opcode use
674b0 64 20 74 6f 20 65 6e 64 73 20 74 68 65 20 6c 6f  d to ends the lo
674c0 6f 70 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20  op */.  union { 
674d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
674e0 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61   Information tha
674f0 74 20 64 65 70 65 6e 64 73 20 6f 6e 20 70 6c 61  t depends on pla
67500 6e 2e 77 73 46 6c 61 67 73 20 2a 2f 0a 20 20 20  n.wsFlags */.   
67510 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20 20 20   struct {.      
67520 69 6e 74 20 6e 49 6e 3b 20 20 20 20 20 20 20 20  int nIn;        
67530 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
67540 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 49  of entries in aI
67550 6e 4c 6f 6f 70 5b 5d 20 2a 2f 0a 20 20 20 20 20  nLoop[] */.     
67560 20 73 74 72 75 63 74 20 49 6e 4c 6f 6f 70 20 7b   struct InLoop {
67570 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 43 75  .        int iCu
67580 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
67590 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73  /* The VDBE curs
675a0 6f 72 20 75 73 65 64 20 62 79 20 74 68 69 73 20  or used by this 
675b0 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20  IN operator */. 
675c0 20 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 49         int addrI
675d0 6e 54 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a  nTop;         /*
675e0 20 54 6f 70 20 6f 66 20 74 68 65 20 49 4e 20 6c   Top of the IN l
675f0 6f 6f 70 20 2a 2f 0a 20 20 20 20 20 20 7d 20 2a  oop */.      } *
67600 61 49 6e 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20  aInLoop;        
67610 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f     /* Informatio
67620 6e 20 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73  n about each nes
67630 74 65 64 20 49 4e 20 6f 70 65 72 61 74 6f 72 20  ted IN operator 
67640 2a 2f 0a 20 20 20 20 7d 20 69 6e 3b 20 20 20 20  */.    } in;    
67650 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
67660 55 73 65 64 20 77 68 65 6e 20 70 6c 61 6e 2e 77  Used when plan.w
67670 73 46 6c 61 67 73 26 57 48 45 52 45 5f 49 4e 5f  sFlags&WHERE_IN_
67680 41 42 4c 45 20 2a 2f 0a 20 20 7d 20 75 3b 0a 0a  ABLE */.  } u;..
67690 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
676a0 6e 67 20 66 69 65 6c 64 20 69 73 20 72 65 61 6c  ng field is real
676b0 6c 79 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74  ly not part of t
676c0 68 65 20 63 75 72 72 65 6e 74 20 6c 65 76 65 6c  he current level
676d0 2e 20 20 42 75 74 0a 20 20 2a 2a 20 77 65 20 6e  .  But.  ** we n
676e0 65 65 64 20 61 20 70 6c 61 63 65 20 74 6f 20 63  eed a place to c
676f0 61 63 68 65 20 76 69 72 74 75 61 6c 20 74 61 62  ache virtual tab
67700 6c 65 20 69 6e 64 65 78 20 69 6e 66 6f 72 6d 61  le index informa
67710 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 0a 20 20  tion for each.  
67720 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
67730 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
67740 75 73 65 20 61 6e 64 20 74 68 65 20 57 68 65 72  use and the Wher
67750 65 4c 65 76 65 6c 20 73 74 72 75 63 74 75 72 65  eLevel structure
67760 20 69 73 0a 20 20 2a 2a 20 61 20 63 6f 6e 76 65   is.  ** a conve
67770 6e 69 65 6e 74 20 70 6c 61 63 65 20 73 69 6e 63  nient place sinc
67780 65 20 74 68 65 72 65 20 69 73 20 6f 6e 65 20 57  e there is one W
67790 68 65 72 65 4c 65 76 65 6c 20 66 6f 72 20 65 61  hereLevel for ea
677a0 63 68 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a 20  ch FROM clause. 
677b0 20 2a 2a 20 65 6c 65 6d 65 6e 74 2e 0a 20 20 2a   ** element..  *
677c0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  /.  sqlite3_inde
677d0 78 5f 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66 6f  x_info *pIdxInfo
677e0 3b 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 66 6f  ;  /* Index info
677f0 20 66 6f 72 20 6e 2d 74 68 20 73 6f 75 72 63 65   for n-th source
67800 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   table */.};../*
67810 0a 2a 2a 20 46 6c 61 67 73 20 61 70 70 72 6f 70  .** Flags approp
67820 72 69 61 74 65 20 66 6f 72 20 74 68 65 20 77 63  riate for the wc
67830 74 72 6c 46 6c 61 67 73 20 70 61 72 61 6d 65 74  trlFlags paramet
67840 65 72 20 6f 66 20 73 71 6c 69 74 65 33 57 68 65  er of sqlite3Whe
67850 72 65 42 65 67 69 6e 28 29 0a 2a 2a 20 61 6e 64  reBegin().** and
67860 20 74 68 65 20 57 68 65 72 65 49 6e 66 6f 2e 77   the WhereInfo.w
67870 63 74 72 6c 46 6c 61 67 73 20 6d 65 6d 62 65 72  ctrlFlags member
67880 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  ..*/.#define WHE
67890 52 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41  RE_ORDERBY_NORMA
678a0 4c 20 20 20 30 78 30 30 30 30 20 2f 2a 20 4e 6f  L   0x0000 /* No
678b0 2d 6f 70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  -op */.#define W
678c0 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e  HERE_ORDERBY_MIN
678d0 20 20 20 20 20 20 30 78 30 30 30 31 20 2f 2a 20        0x0001 /* 
678e0 4f 52 44 45 52 20 42 59 20 70 72 6f 63 65 73 73  ORDER BY process
678f0 69 6e 67 20 66 6f 72 20 6d 69 6e 28 29 20 66 75  ing for min() fu
67900 6e 63 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  nc */.#define WH
67910 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58 20  ERE_ORDERBY_MAX 
67920 20 20 20 20 20 30 78 30 30 30 32 20 2f 2a 20 4f       0x0002 /* O
67930 52 44 45 52 20 42 59 20 70 72 6f 63 65 73 73 69  RDER BY processi
67940 6e 67 20 66 6f 72 20 6d 61 78 28 29 20 66 75 6e  ng for max() fun
67950 63 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  c */.#define WHE
67960 52 45 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49 52  RE_ONEPASS_DESIR
67970 45 44 20 20 30 78 30 30 30 34 20 2f 2a 20 57 61  ED  0x0004 /* Wa
67980 6e 74 20 74 6f 20 64 6f 20 6f 6e 65 2d 70 61 73  nt to do one-pas
67990 73 20 55 50 44 41 54 45 2f 44 45 4c 45 54 45 20  s UPDATE/DELETE 
679a0 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
679b0 5f 44 55 50 4c 49 43 41 54 45 53 5f 4f 4b 20 20  _DUPLICATES_OK  
679c0 20 20 30 78 30 30 30 38 20 2f 2a 20 4f 6b 20 74    0x0008 /* Ok t
679d0 6f 20 72 65 74 75 72 6e 20 61 20 72 6f 77 20 6d  o return a row m
679e0 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 20 2a 2f  ore than once */
679f0 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f  .#define WHERE_O
67a00 4d 49 54 5f 4f 50 45 4e 20 20 20 20 20 20 20 20  MIT_OPEN        
67a10 30 78 30 30 31 30 20 2f 2a 20 54 61 62 6c 65 20  0x0010 /* Table 
67a20 63 75 72 73 6f 72 20 61 72 65 20 61 6c 72 65 61  cursor are alrea
67a30 64 79 20 6f 70 65 6e 20 2a 2f 0a 23 64 65 66 69  dy open */.#defi
67a40 6e 65 20 57 48 45 52 45 5f 4f 4d 49 54 5f 43 4c  ne WHERE_OMIT_CL
67a50 4f 53 45 20 20 20 20 20 20 20 30 78 30 30 32 30  OSE       0x0020
67a60 20 2f 2a 20 4f 6d 69 74 20 63 6c 6f 73 65 20 6f   /* Omit close o
67a70 66 20 74 61 62 6c 65 20 26 20 69 6e 64 65 78 20  f table & index 
67a80 63 75 72 73 6f 72 73 20 2a 2f 0a 23 64 65 66 69  cursors */.#defi
67a90 6e 65 20 57 48 45 52 45 5f 46 4f 52 43 45 5f 54  ne WHERE_FORCE_T
67aa0 41 42 4c 45 20 20 20 20 20 20 30 78 30 30 34 30  ABLE      0x0040
67ab0 20 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20 61   /* Do not use a
67ac0 6e 20 69 6e 64 65 78 2d 6f 6e 6c 79 20 73 65 61  n index-only sea
67ad0 72 63 68 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  rch */../*.** Th
67ae0 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 70  e WHERE clause p
67af0 72 6f 63 65 73 73 69 6e 67 20 72 6f 75 74 69 6e  rocessing routin
67b00 65 20 68 61 73 20 74 77 6f 20 68 61 6c 76 65 73  e has two halves
67b10 2e 20 20 54 68 65 0a 2a 2a 20 66 69 72 73 74 20  .  The.** first 
67b20 70 61 72 74 20 64 6f 65 73 20 74 68 65 20 73 74  part does the st
67b30 61 72 74 20 6f 66 20 74 68 65 20 57 48 45 52 45  art of the WHERE
67b40 20 6c 6f 6f 70 20 61 6e 64 20 74 68 65 20 73 65   loop and the se
67b50 63 6f 6e 64 0a 2a 2a 20 68 61 6c 66 20 64 6f 65  cond.** half doe
67b60 73 20 74 68 65 20 74 61 69 6c 20 6f 66 20 74 68  s the tail of th
67b70 65 20 57 48 45 52 45 20 6c 6f 6f 70 2e 20 20 41  e WHERE loop.  A
67b80 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a  n instance of.**
67b90 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
67ba0 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  is returned by t
67bb0 68 65 20 66 69 72 73 74 20 68 61 6c 66 20 61 6e  he first half an
67bc0 64 20 70 61 73 73 65 64 0a 2a 2a 20 69 6e 74 6f  d passed.** into
67bd0 20 74 68 65 20 73 65 63 6f 6e 64 20 68 61 6c 66   the second half
67be0 20 74 6f 20 67 69 76 65 20 73 6f 6d 65 20 63 6f   to give some co
67bf0 6e 74 69 6e 75 69 74 79 2e 0a 2a 2f 0a 73 74 72  ntinuity..*/.str
67c00 75 63 74 20 57 68 65 72 65 49 6e 66 6f 20 7b 0a  uct WhereInfo {.
67c10 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b    Parse *pParse;
67c20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
67c30 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72  g and code gener
67c40 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  ating context */
67c50 0a 20 20 75 31 36 20 77 63 74 72 6c 46 6c 61 67  .  u16 wctrlFlag
67c60 73 3b 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73  s;      /* Flags
67c70 20 6f 72 69 67 69 6e 61 6c 6c 79 20 70 61 73 73   originally pass
67c80 65 64 20 74 6f 20 73 71 6c 69 74 65 33 57 68 65  ed to sqlite3Whe
67c90 72 65 42 65 67 69 6e 28 29 20 2a 2f 0a 20 20 75  reBegin() */.  u
67ca0 38 20 6f 6b 4f 6e 65 50 61 73 73 3b 20 20 20 20  8 okOnePass;    
67cb0 20 20 20 20 2f 2a 20 4f 6b 20 74 6f 20 75 73 65      /* Ok to use
67cc0 20 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72 69   one-pass algori
67cd0 74 68 6d 20 66 6f 72 20 55 50 44 41 54 45 20 6f  thm for UPDATE o
67ce0 72 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20 53 72  r DELETE */.  Sr
67cf0 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 3b  cList *pTabList;
67d00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
67d10 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 69  List of tables i
67d20 6e 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20  n the join */.  
67d30 69 6e 74 20 69 54 6f 70 3b 20 20 20 20 20 20 20  int iTop;       
67d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
67d50 2a 20 54 68 65 20 76 65 72 79 20 62 65 67 69 6e  * The very begin
67d60 6e 69 6e 67 20 6f 66 20 74 68 65 20 57 48 45 52  ning of the WHER
67d70 45 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20  E loop */.  int 
67d80 69 43 6f 6e 74 69 6e 75 65 3b 20 20 20 20 20 20  iContinue;      
67d90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75             /* Ju
67da0 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69  mp here to conti
67db0 6e 75 65 20 77 69 74 68 20 6e 65 78 74 20 72 65  nue with next re
67dc0 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 69 42  cord */.  int iB
67dd0 72 65 61 6b 3b 20 20 20 20 20 20 20 20 20 20 20  reak;           
67de0 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70           /* Jump
67df0 20 68 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f   here to break o
67e00 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a  ut of the loop *
67e10 2f 0a 20 20 69 6e 74 20 6e 4c 65 76 65 6c 3b 20  /.  int nLevel; 
67e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67e30 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
67e40 6e 65 73 74 65 64 20 6c 6f 6f 70 20 2a 2f 0a 20  nested loop */. 
67e50 20 73 74 72 75 63 74 20 57 68 65 72 65 43 6c 61   struct WhereCla
67e60 75 73 65 20 2a 70 57 43 3b 20 20 20 20 20 20 20  use *pWC;       
67e70 2f 2a 20 44 65 63 6f 6d 70 6f 73 69 74 69 6f 6e  /* Decomposition
67e80 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c   of the WHERE cl
67e90 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 4c  ause */.  WhereL
67ea0 65 76 65 6c 20 61 5b 31 5d 3b 20 20 20 20 20 20  evel a[1];      
67eb0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f           /* Info
67ec0 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61  rmation about ea
67ed0 63 68 20 6e 65 73 74 20 6c 6f 6f 70 20 69 6e 20  ch nest loop in 
67ee0 57 48 45 52 45 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  WHERE */.};../*.
67ef0 2a 2a 20 41 20 4e 61 6d 65 43 6f 6e 74 65 78 74  ** A NameContext
67f00 20 64 65 66 69 6e 65 73 20 61 20 63 6f 6e 74 65   defines a conte
67f10 78 74 20 69 6e 20 77 68 69 63 68 20 74 6f 20 72  xt in which to r
67f20 65 73 6f 6c 76 65 20 74 61 62 6c 65 20 61 6e 64  esolve table and
67f30 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 73   column.** names
67f40 2e 20 20 54 68 65 20 63 6f 6e 74 65 78 74 20 63  .  The context c
67f50 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 6c 69 73  onsists of a lis
67f60 74 20 6f 66 20 74 61 62 6c 65 73 20 28 74 68 65  t of tables (the
67f70 20 70 53 72 63 4c 69 73 74 29 20 66 69 65 6c 64   pSrcList) field
67f80 20 61 6e 64 0a 2a 2a 20 61 20 6c 69 73 74 20 6f   and.** a list o
67f90 66 20 6e 61 6d 65 64 20 65 78 70 72 65 73 73 69  f named expressi
67fa0 6f 6e 20 28 70 45 4c 69 73 74 29 2e 20 20 54 68  on (pEList).  Th
67fb0 65 20 6e 61 6d 65 64 20 65 78 70 72 65 73 73 69  e named expressi
67fc0 6f 6e 20 6c 69 73 74 20 6d 61 79 0a 2a 2a 20 62  on list may.** b
67fd0 65 20 4e 55 4c 4c 2e 20 20 54 68 65 20 70 53 72  e NULL.  The pSr
67fe0 63 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f  c corresponds to
67ff0 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
68000 20 6f 66 20 61 20 53 45 4c 45 43 54 20 6f 72 0a   of a SELECT or.
68010 2a 2a 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  ** to the table 
68020 62 65 69 6e 67 20 6f 70 65 72 61 74 65 64 20 6f  being operated o
68030 6e 20 62 79 20 49 4e 53 45 52 54 2c 20 55 50 44  n by INSERT, UPD
68040 41 54 45 2c 20 6f 72 20 44 45 4c 45 54 45 2e 20  ATE, or DELETE. 
68050 20 54 68 65 0a 2a 2a 20 70 45 4c 69 73 74 20 63   The.** pEList c
68060 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68  orresponds to th
68070 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
68080 61 20 53 45 4c 45 43 54 20 61 6e 64 20 69 73 20  a SELECT and is 
68090 4e 55 4c 4c 20 66 6f 72 0a 2a 2a 20 6f 74 68 65  NULL for.** othe
680a0 72 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a  r statements..**
680b0 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74 73  .** NameContexts
680c0 20 63 61 6e 20 62 65 20 6e 65 73 74 65 64 2e 20   can be nested. 
680d0 20 57 68 65 6e 20 72 65 73 6f 6c 76 69 6e 67 20   When resolving 
680e0 6e 61 6d 65 73 2c 20 74 68 65 20 69 6e 6e 65 72  names, the inner
680f0 2d 6d 6f 73 74 20 0a 2a 2a 20 63 6f 6e 74 65 78  -most .** contex
68100 74 20 69 73 20 73 65 61 72 63 68 65 64 20 66 69  t is searched fi
68110 72 73 74 2e 20 20 49 66 20 6e 6f 20 6d 61 74 63  rst.  If no matc
68120 68 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 20  h is found, the 
68130 6e 65 78 74 20 6f 75 74 65 72 0a 2a 2a 20 63 6f  next outer.** co
68140 6e 74 65 78 74 20 69 73 20 63 68 65 63 6b 65 64  ntext is checked
68150 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20 73  .  If there is s
68160 74 69 6c 6c 20 6e 6f 20 6d 61 74 63 68 2c 20 74  till no match, t
68170 68 65 20 6e 65 78 74 20 63 6f 6e 74 65 78 74 0a  he next context.
68180 2a 2a 20 69 73 20 63 68 65 63 6b 65 64 2e 20 20  ** is checked.  
68190 54 68 69 73 20 70 72 6f 63 65 73 73 20 63 6f 6e  This process con
681a0 74 69 6e 75 65 73 20 75 6e 74 69 6c 20 65 69 74  tinues until eit
681b0 68 65 72 20 61 20 6d 61 74 63 68 20 69 73 20 66  her a match is f
681c0 6f 75 6e 64 0a 2a 2a 20 6f 72 20 61 6c 6c 20 63  ound.** or all c
681d0 6f 6e 74 65 78 74 73 20 61 72 65 20 63 68 65 63  ontexts are chec
681e0 6b 2e 20 20 57 68 65 6e 20 61 20 6d 61 74 63 68  k.  When a match
681f0 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e   is found, the n
68200 52 65 66 20 6d 65 6d 62 65 72 20 6f 66 0a 2a 2a  Ref member of.**
68210 20 74 68 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e   the context con
68220 74 61 69 6e 69 6e 67 20 74 68 65 20 6d 61 74 63  taining the matc
68230 68 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  h is incremented
68240 2e 20 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 73 75  . .**.** Each su
68250 62 71 75 65 72 79 20 67 65 74 73 20 61 20 6e 65  bquery gets a ne
68260 77 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 20 20  w NameContext.  
68270 54 68 65 20 70 4e 65 78 74 20 66 69 65 6c 64 20  The pNext field 
68280 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a  points to the.**
68290 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 69 6e 20   NameContext in 
682a0 74 68 65 20 70 61 72 65 6e 74 20 71 75 65 72 79  the parent query
682b0 2e 20 20 54 68 75 73 20 74 68 65 20 70 72 6f 63  .  Thus the proc
682c0 65 73 73 20 6f 66 20 73 63 61 6e 6e 69 6e 67 20  ess of scanning 
682d0 74 68 65 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65  the.** NameConte
682e0 78 74 20 6c 69 73 74 20 63 6f 72 72 65 73 70 6f  xt list correspo
682f0 6e 64 73 20 74 6f 20 73 65 61 72 63 68 69 6e 67  nds to searching
68300 20 74 68 72 6f 75 67 68 20 73 75 63 63 65 73 73   through success
68310 69 76 65 6c 79 20 6f 75 74 65 72 0a 2a 2a 20 73  ively outer.** s
68320 75 62 71 75 65 72 69 65 73 20 6c 6f 6f 6b 69 6e  ubqueries lookin
68330 67 20 66 6f 72 20 61 20 6d 61 74 63 68 2e 0a 2a  g for a match..*
68340 2f 0a 73 74 72 75 63 74 20 4e 61 6d 65 43 6f 6e  /.struct NameCon
68350 74 65 78 74 20 7b 0a 20 20 50 61 72 73 65 20 2a  text {.  Parse *
68360 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 2f 2a  pParse;       /*
68370 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20   The parser */. 
68380 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69   SrcList *pSrcLi
68390 73 74 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20  st;   /* One or 
683a0 6d 6f 72 65 20 74 61 62 6c 65 73 20 75 73 65 64  more tables used
683b0 20 74 6f 20 72 65 73 6f 6c 76 65 20 6e 61 6d 65   to resolve name
683c0 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  s */.  ExprList 
683d0 2a 70 45 4c 69 73 74 3b 20 20 20 20 2f 2a 20 4f  *pEList;    /* O
683e0 70 74 69 6f 6e 61 6c 20 6c 69 73 74 20 6f 66 20  ptional list of 
683f0 6e 61 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e  named expression
68400 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b  s */.  int nRef;
68410 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
68420 75 6d 62 65 72 20 6f 66 20 6e 61 6d 65 73 20 72  umber of names r
68430 65 73 6f 6c 76 65 64 20 62 79 20 74 68 69 73 20  esolved by this 
68440 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74  context */.  int
68450 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20 20   nErr;          
68460 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
68470 72 72 6f 72 73 20 65 6e 63 6f 75 6e 74 65 72 65  rrors encountere
68480 64 20 77 68 69 6c 65 20 72 65 73 6f 6c 76 69 6e  d while resolvin
68490 67 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 75 38 20  g names */.  u8 
684a0 61 6c 6c 6f 77 41 67 67 3b 20 20 20 20 20 20 20  allowAgg;       
684b0 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20 66    /* Aggregate f
684c0 75 6e 63 74 69 6f 6e 73 20 61 6c 6c 6f 77 65 64  unctions allowed
684d0 20 68 65 72 65 20 2a 2f 0a 20 20 75 38 20 68 61   here */.  u8 ha
684e0 73 41 67 67 3b 20 20 20 20 20 20 20 20 20 20 20  sAgg;           
684f0 2f 2a 20 54 72 75 65 20 69 66 20 61 67 67 72 65  /* True if aggre
68500 67 61 74 65 73 20 61 72 65 20 73 65 65 6e 20 2a  gates are seen *
68510 2f 0a 20 20 75 38 20 69 73 43 68 65 63 6b 3b 20  /.  u8 isCheck; 
68520 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
68530 20 69 66 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61   if resolving na
68540 6d 65 73 20 69 6e 20 61 20 43 48 45 43 4b 20 63  mes in a CHECK c
68550 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 69  onstraint */.  i
68560 6e 74 20 6e 44 65 70 74 68 3b 20 20 20 20 20 20  nt nDepth;      
68570 20 20 20 20 2f 2a 20 44 65 70 74 68 20 6f 66 20      /* Depth of 
68580 73 75 62 71 75 65 72 79 20 72 65 63 75 72 73 69  subquery recursi
68590 6f 6e 2e 20 31 20 66 6f 72 20 6e 6f 20 72 65 63  on. 1 for no rec
685a0 75 72 73 69 6f 6e 20 2a 2f 0a 20 20 41 67 67 49  ursion */.  AggI
685b0 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b 20 20  nfo *pAggInfo;  
685c0 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
685d0 61 62 6f 75 74 20 61 67 67 72 65 67 61 74 65 73  about aggregates
685e0 20 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20 2a   at this level *
685f0 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  /.  NameContext 
68600 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74  *pNext;  /* Next
68610 20 6f 75 74 65 72 20 6e 61 6d 65 20 63 6f 6e 74   outer name cont
68620 65 78 74 2e 20 20 4e 55 4c 4c 20 66 6f 72 20 6f  ext.  NULL for o
68630 75 74 65 72 6d 6f 73 74 20 2a 2f 0a 7d 3b 0a 0a  utermost */.};..
68640 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
68650 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
68660 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  ng structure con
68670 74 61 69 6e 73 20 61 6c 6c 20 69 6e 66 6f 72 6d  tains all inform
68680 61 74 69 6f 6e 0a 2a 2a 20 6e 65 65 64 65 64 20  ation.** needed 
68690 74 6f 20 67 65 6e 65 72 61 74 65 20 63 6f 64 65  to generate code
686a0 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 53 45   for a single SE
686b0 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a  LECT statement..
686c0 2a 2a 0a 2a 2a 20 6e 4c 69 6d 69 74 20 69 73 20  **.** nLimit is 
686d0 73 65 74 20 74 6f 20 2d 31 20 69 66 20 74 68 65  set to -1 if the
686e0 72 65 20 69 73 20 6e 6f 20 4c 49 4d 49 54 20 63  re is no LIMIT c
686f0 6c 61 75 73 65 2e 20 20 6e 4f 66 66 73 65 74 20  lause.  nOffset 
68700 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 20  is set to 0..** 
68710 49 66 20 74 68 65 72 65 20 69 73 20 61 20 4c 49  If there is a LI
68720 4d 49 54 20 63 6c 61 75 73 65 2c 20 74 68 65 20  MIT clause, the 
68730 70 61 72 73 65 72 20 73 65 74 73 20 6e 4c 69 6d  parser sets nLim
68740 69 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  it to the value 
68750 6f 66 20 74 68 65 0a 2a 2a 20 6c 69 6d 69 74 20  of the.** limit 
68760 61 6e 64 20 6e 4f 66 66 73 65 74 20 74 6f 20 74  and nOffset to t
68770 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
68780 6f 66 66 73 65 74 20 28 6f 72 20 30 20 69 66 20  offset (or 0 if 
68790 74 68 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20  there is not.** 
687a0 6f 66 66 73 65 74 29 2e 20 20 42 75 74 20 6c 61  offset).  But la
687b0 74 65 72 20 6f 6e 2c 20 6e 4c 69 6d 69 74 20 61  ter on, nLimit a
687c0 6e 64 20 6e 4f 66 66 73 65 74 20 62 65 63 6f 6d  nd nOffset becom
687d0 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63  e the memory loc
687e0 61 74 69 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65  ations.** in the
687f0 20 56 44 42 45 20 74 68 61 74 20 72 65 63 6f 72   VDBE that recor
68800 64 20 74 68 65 20 6c 69 6d 69 74 20 61 6e 64 20  d the limit and 
68810 6f 66 66 73 65 74 20 63 6f 75 6e 74 65 72 73 2e  offset counters.
68820 0a 2a 2a 0a 2a 2a 20 61 64 64 72 4f 70 65 6e 45  .**.** addrOpenE
68830 70 68 6d 5b 5d 20 65 6e 74 72 69 65 73 20 63 6f  phm[] entries co
68840 6e 74 61 69 6e 20 74 68 65 20 61 64 64 72 65 73  ntain the addres
68850 73 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65  s of OP_OpenEphe
68860 6d 65 72 61 6c 20 6f 70 63 6f 64 65 73 2e 0a 2a  meral opcodes..*
68870 2a 20 54 68 65 73 65 20 61 64 64 72 65 73 73 65  * These addresse
68880 73 20 6d 75 73 74 20 62 65 20 73 74 6f 72 65 64  s must be stored
68890 20 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20   so that we can 
688a0 67 6f 20 62 61 63 6b 20 61 6e 64 20 66 69 6c 6c  go back and fill
688b0 20 69 6e 0a 2a 2a 20 74 68 65 20 50 34 5f 4b 45   in.** the P4_KE
688c0 59 49 4e 46 4f 20 61 6e 64 20 50 32 20 70 61 72  YINFO and P2 par
688d0 61 6d 65 74 65 72 73 20 6c 61 74 65 72 2e 20 20  ameters later.  
688e0 4e 65 69 74 68 65 72 20 74 68 65 20 4b 65 79 49  Neither the KeyI
688f0 6e 66 6f 20 6e 6f 72 0a 2a 2a 20 74 68 65 20 6e  nfo nor.** the n
68900 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
68910 20 69 6e 20 50 32 20 63 61 6e 20 62 65 20 63 6f   in P2 can be co
68920 6d 70 75 74 65 64 20 61 74 20 74 68 65 20 73 61  mputed at the sa
68930 6d 65 20 74 69 6d 65 0a 2a 2a 20 61 73 20 74 68  me time.** as th
68940 65 20 4f 50 5f 4f 70 65 6e 45 70 68 6d 20 69 6e  e OP_OpenEphm in
68950 73 74 72 75 63 74 69 6f 6e 20 69 73 20 63 6f 64  struction is cod
68960 65 64 20 62 65 63 61 75 73 65 20 6e 6f 74 0a 2a  ed because not.*
68970 2a 20 65 6e 6f 75 67 68 20 69 6e 66 6f 72 6d 61  * enough informa
68980 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 63  tion about the c
68990 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79 20 69 73  ompound query is
689a0 20 6b 6e 6f 77 6e 20 61 74 20 74 68 61 74 20 70   known at that p
689b0 6f 69 6e 74 2e 0a 2a 2a 20 54 68 65 20 4b 65 79  oint..** The Key
689c0 49 6e 66 6f 20 66 6f 72 20 61 64 64 72 4f 70 65  Info for addrOpe
689d0 6e 54 72 61 6e 5b 30 5d 20 61 6e 64 20 5b 31 5d  nTran[0] and [1]
689e0 20 63 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 74   contains collat
689f0 69 6e 67 20 73 65 71 75 65 6e 63 65 73 0a 2a 2a  ing sequences.**
68a00 20 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 20   for the result 
68a10 73 65 74 2e 20 20 54 68 65 20 4b 65 79 49 6e 66  set.  The KeyInf
68a20 6f 20 66 6f 72 20 61 64 64 72 4f 70 65 6e 54 72  o for addrOpenTr
68a30 61 6e 5b 32 5d 20 63 6f 6e 74 61 69 6e 73 20 63  an[2] contains c
68a40 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75  ollating.** sequ
68a50 65 6e 63 65 73 20 66 6f 72 20 74 68 65 20 4f 52  ences for the OR
68a60 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a  DER BY clause..*
68a70 2f 0a 73 74 72 75 63 74 20 53 65 6c 65 63 74 20  /.struct Select 
68a80 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  {.  ExprList *pE
68a90 4c 69 73 74 3b 20 20 20 20 20 20 2f 2a 20 54 68  List;      /* Th
68aa0 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20  e fields of the 
68ab0 72 65 73 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6f  result */.  u8 o
68ac0 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
68ad0 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 3a 20 54 4b     /* One of: TK
68ae0 5f 55 4e 49 4f 4e 20 54 4b 5f 41 4c 4c 20 54 4b  _UNION TK_ALL TK
68af0 5f 49 4e 54 45 52 53 45 43 54 20 54 4b 5f 45 58  _INTERSECT TK_EX
68b00 43 45 50 54 20 2a 2f 0a 20 20 63 68 61 72 20 61  CEPT */.  char a
68b10 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20 20  ffinity;        
68b20 20 2f 2a 20 4d 61 6b 65 52 65 63 6f 72 64 20 77   /* MakeRecord w
68b30 69 74 68 20 74 68 69 73 20 61 66 66 69 6e 69 74  ith this affinit
68b40 79 20 66 6f 72 20 53 52 54 5f 53 65 74 20 2a 2f  y for SRT_Set */
68b50 0a 20 20 75 31 36 20 73 65 6c 46 6c 61 67 73 3b  .  u16 selFlags;
68b60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 72            /* Var
68b70 69 6f 75 73 20 53 46 5f 2a 20 76 61 6c 75 65 73  ious SF_* values
68b80 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
68b90 53 72 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Src;         /* 
68ba0 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  The FROM clause 
68bb0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72  */.  Expr *pWher
68bc0 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  e;          /* T
68bd0 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
68be0 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
68bf0 47 72 6f 75 70 42 79 3b 20 20 20 20 2f 2a 20 54  GroupBy;    /* T
68c00 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  he GROUP BY clau
68c10 73 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 48  se */.  Expr *pH
68c20 61 76 69 6e 67 3b 20 20 20 20 20 20 20 20 20 2f  aving;         /
68c30 2a 20 54 68 65 20 48 41 56 49 4e 47 20 63 6c 61  * The HAVING cla
68c40 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  use */.  ExprLis
68c50 74 20 2a 70 4f 72 64 65 72 42 79 3b 20 20 20 20  t *pOrderBy;    
68c60 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  /* The ORDER BY 
68c70 63 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65  clause */.  Sele
68c80 63 74 20 2a 70 50 72 69 6f 72 3b 20 20 20 20 20  ct *pPrior;     
68c90 20 20 20 2f 2a 20 50 72 69 6f 72 20 73 65 6c 65     /* Prior sele
68ca0 63 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64  ct in a compound
68cb0 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e   select statemen
68cc0 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  t */.  Select *p
68cd0 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  Next;         /*
68ce0 20 4e 65 78 74 20 73 65 6c 65 63 74 20 74 6f 20   Next select to 
68cf0 74 68 65 20 6c 65 66 74 20 69 6e 20 61 20 63 6f  the left in a co
68d00 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20 53 65 6c 65  mpound */.  Sele
68d10 63 74 20 2a 70 52 69 67 68 74 6d 6f 73 74 3b 20  ct *pRightmost; 
68d20 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74     /* Right-most
68d30 20 73 65 6c 65 63 74 20 69 6e 20 61 20 63 6f 6d   select in a com
68d40 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 73 74 61  pound select sta
68d50 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72  tement */.  Expr
68d60 20 2a 70 4c 69 6d 69 74 3b 20 20 20 20 20 20 20   *pLimit;       
68d70 20 20 20 2f 2a 20 4c 49 4d 49 54 20 65 78 70 72     /* LIMIT expr
68d80 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61  ession. NULL mea
68d90 6e 73 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a  ns not used. */.
68da0 20 20 45 78 70 72 20 2a 70 4f 66 66 73 65 74 3b    Expr *pOffset;
68db0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 46 46 53           /* OFFS
68dc0 45 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e  ET expression. N
68dd0 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73  ULL means not us
68de0 65 64 2e 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 69  ed. */.  int iLi
68df0 6d 69 74 2c 20 69 4f 66 66 73 65 74 3b 20 20 20  mit, iOffset;   
68e00 2f 2a 20 4d 65 6d 6f 72 79 20 72 65 67 69 73 74  /* Memory regist
68e10 65 72 73 20 68 6f 6c 64 69 6e 67 20 4c 49 4d 49  ers holding LIMI
68e20 54 20 26 20 4f 46 46 53 45 54 20 63 6f 75 6e 74  T & OFFSET count
68e30 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ers */.  int add
68e40 72 4f 70 65 6e 45 70 68 6d 5b 33 5d 3b 20 20 20  rOpenEphm[3];   
68e50 2f 2a 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 20  /* OP_OpenEphem 
68e60 6f 70 63 6f 64 65 73 20 72 65 6c 61 74 65 64 20  opcodes related 
68e70 74 6f 20 74 68 69 73 20 73 65 6c 65 63 74 20 2a  to this select *
68e80 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  /.};../*.** Allo
68e90 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 53  wed values for S
68ea0 65 6c 65 63 74 2e 73 65 6c 46 6c 61 67 73 2e 20  elect.selFlags. 
68eb0 20 54 68 65 20 22 53 46 22 20 70 72 65 66 69 78   The "SF" prefix
68ec0 20 73 74 61 6e 64 73 20 66 6f 72 0a 2a 2a 20 22   stands for.** "
68ed0 53 65 6c 65 63 74 20 46 6c 61 67 22 2e 0a 2a 2f  Select Flag"..*/
68ee0 0a 23 64 65 66 69 6e 65 20 53 46 5f 44 69 73 74  .#define SF_Dist
68ef0 69 6e 63 74 20 20 20 20 20 20 20 20 30 78 30 30  inct        0x00
68f00 30 31 20 20 2f 2a 20 4f 75 74 70 75 74 20 73 68  01  /* Output sh
68f10 6f 75 6c 64 20 62 65 20 44 49 53 54 49 4e 43 54  ould be DISTINCT
68f20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 52   */.#define SF_R
68f30 65 73 6f 6c 76 65 64 20 20 20 20 20 20 20 20 30  esolved        0
68f40 78 30 30 30 32 20 20 2f 2a 20 49 64 65 6e 74 69  x0002  /* Identi
68f50 66 69 65 72 73 20 68 61 76 65 20 62 65 65 6e 20  fiers have been 
68f60 72 65 73 6f 6c 76 65 64 20 2a 2f 0a 23 64 65 66  resolved */.#def
68f70 69 6e 65 20 53 46 5f 41 67 67 72 65 67 61 74 65  ine SF_Aggregate
68f80 20 20 20 20 20 20 20 30 78 30 30 30 34 20 20 2f         0x0004  /
68f90 2a 20 43 6f 6e 74 61 69 6e 73 20 61 67 67 72 65  * Contains aggre
68fa0 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a  gate functions *
68fb0 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 55 73 65  /.#define SF_Use
68fc0 73 45 70 68 65 6d 65 72 61 6c 20 20 20 30 78 30  sEphemeral   0x0
68fd0 30 30 38 20 20 2f 2a 20 55 73 65 73 20 74 68 65  008  /* Uses the
68fe0 20 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 6f   OpenEphemeral o
68ff0 70 63 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65  pcode */.#define
69000 20 53 46 5f 45 78 70 61 6e 64 65 64 20 20 20 20   SF_Expanded    
69010 20 20 20 20 30 78 30 30 31 30 20 20 2f 2a 20 73      0x0010  /* s
69020 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61  qlite3SelectExpa
69030 6e 64 28 29 20 63 61 6c 6c 65 64 20 6f 6e 20 74  nd() called on t
69040 68 69 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  his */.#define S
69050 46 5f 48 61 73 54 79 70 65 49 6e 66 6f 20 20 20  F_HasTypeInfo   
69060 20 20 30 78 30 30 32 30 20 20 2f 2a 20 46 52 4f    0x0020  /* FRO
69070 4d 20 73 75 62 71 75 65 72 69 65 73 20 68 61 76  M subqueries hav
69080 65 20 54 61 62 6c 65 20 6d 65 74 61 64 61 74 61  e Table metadata
69090 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20   */.../*.** The 
690a0 72 65 73 75 6c 74 73 20 6f 66 20 61 20 73 65 6c  results of a sel
690b0 65 63 74 20 63 61 6e 20 62 65 20 64 69 73 74 72  ect can be distr
690c0 69 62 75 74 65 64 20 69 6e 20 73 65 76 65 72 61  ibuted in severa
690d0 6c 20 77 61 79 73 2e 20 20 54 68 65 0a 2a 2a 20  l ways.  The.** 
690e0 22 53 52 54 22 20 70 72 65 66 69 78 20 6d 65 61  "SRT" prefix mea
690f0 6e 73 20 22 53 45 4c 45 43 54 20 52 65 73 75 6c  ns "SELECT Resul
69100 74 20 54 79 70 65 22 2e 0a 2a 2f 0a 23 64 65 66  t Type"..*/.#def
69110 69 6e 65 20 53 52 54 5f 55 6e 69 6f 6e 20 20 20  ine SRT_Union   
69120 20 20 20 20 20 31 20 20 2f 2a 20 53 74 6f 72 65       1  /* Store
69130 20 72 65 73 75 6c 74 20 61 73 20 6b 65 79 73 20   result as keys 
69140 69 6e 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 23  in an index */.#
69150 64 65 66 69 6e 65 20 53 52 54 5f 45 78 63 65 70  define SRT_Excep
69160 74 20 20 20 20 20 20 20 32 20 20 2f 2a 20 52 65  t       2  /* Re
69170 6d 6f 76 65 20 72 65 73 75 6c 74 20 66 72 6f 6d  move result from
69180 20 61 20 55 4e 49 4f 4e 20 69 6e 64 65 78 20 2a   a UNION index *
69190 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 45 78  /.#define SRT_Ex
691a0 69 73 74 73 20 20 20 20 20 20 20 33 20 20 2f 2a  ists       3  /*
691b0 20 53 74 6f 72 65 20 31 20 69 66 20 74 68 65 20   Store 1 if the 
691c0 72 65 73 75 6c 74 20 69 73 20 6e 6f 74 20 65 6d  result is not em
691d0 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  pty */.#define S
691e0 52 54 5f 44 69 73 63 61 72 64 20 20 20 20 20 20  RT_Discard      
691f0 34 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 61 76  4  /* Do not sav
69200 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 6e  e the results an
69210 79 77 68 65 72 65 20 2a 2f 0a 0a 2f 2a 20 54 68  ywhere */../* Th
69220 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
69230 65 20 69 73 20 69 67 6e 6f 72 65 64 20 66 6f 72  e is ignored for
69240 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76   all of the abov
69250 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 49 67 6e  e */.#define Ign
69260 6f 72 61 62 6c 65 4f 72 64 65 72 62 79 28 58 29  orableOrderby(X)
69270 20 28 28 58 2d 3e 65 44 65 73 74 29 3c 3d 53 52   ((X->eDest)<=SR
69280 54 5f 44 69 73 63 61 72 64 29 0a 0a 23 64 65 66  T_Discard)..#def
69290 69 6e 65 20 53 52 54 5f 4f 75 74 70 75 74 20 20  ine SRT_Output  
692a0 20 20 20 20 20 35 20 20 2f 2a 20 4f 75 74 70 75       5  /* Outpu
692b0 74 20 65 61 63 68 20 72 6f 77 20 6f 66 20 72 65  t each row of re
692c0 73 75 6c 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  sult */.#define 
692d0 53 52 54 5f 4d 65 6d 20 20 20 20 20 20 20 20 20  SRT_Mem         
692e0 20 36 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73   6  /* Store res
692f0 75 6c 74 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20  ult in a memory 
69300 63 65 6c 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20  cell */.#define 
69310 53 52 54 5f 53 65 74 20 20 20 20 20 20 20 20 20  SRT_Set         
69320 20 37 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73   7  /* Store res
69330 75 6c 74 73 20 61 73 20 6b 65 79 73 20 69 6e 20  ults as keys in 
69340 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66  an index */.#def
69350 69 6e 65 20 53 52 54 5f 54 61 62 6c 65 20 20 20  ine SRT_Table   
69360 20 20 20 20 20 38 20 20 2f 2a 20 53 74 6f 72 65       8  /* Store
69370 20 72 65 73 75 6c 74 20 61 73 20 64 61 74 61 20   result as data 
69380 77 69 74 68 20 61 6e 20 61 75 74 6f 6d 61 74 69  with an automati
69390 63 20 72 6f 77 69 64 20 2a 2f 0a 23 64 65 66 69  c rowid */.#defi
693a0 6e 65 20 53 52 54 5f 45 70 68 65 6d 54 61 62 20  ne SRT_EphemTab 
693b0 20 20 20 20 39 20 20 2f 2a 20 43 72 65 61 74 65      9  /* Create
693c0 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 20 61   transient tab a
693d0 6e 64 20 73 74 6f 72 65 20 6c 69 6b 65 20 53 52  nd store like SR
693e0 54 5f 54 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69  T_Table */.#defi
693f0 6e 65 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65  ne SRT_Coroutine
69400 20 20 20 31 30 20 20 2f 2a 20 47 65 6e 65 72 61     10  /* Genera
69410 74 65 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  te a single row 
69420 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0a 0a 2f 2a  of result */../*
69430 0a 2a 2a 20 41 20 73 74 72 75 63 74 75 72 65 20  .** A structure 
69440 75 73 65 64 20 74 6f 20 63 75 73 74 6f 6d 69 7a  used to customiz
69450 65 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f  e the behavior o
69460 66 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  f sqlite3Select(
69470 29 2e 20 53 65 65 0a 2a 2a 20 63 6f 6d 6d 65 6e  ). See.** commen
69480 74 73 20 61 62 6f 76 65 20 73 71 6c 69 74 65 33  ts above sqlite3
69490 53 65 6c 65 63 74 28 29 20 66 6f 72 20 64 65 74  Select() for det
694a0 61 69 6c 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ails..*/.typedef
694b0 20 73 74 72 75 63 74 20 53 65 6c 65 63 74 44 65   struct SelectDe
694c0 73 74 20 53 65 6c 65 63 74 44 65 73 74 3b 0a 73  st SelectDest;.s
694d0 74 72 75 63 74 20 53 65 6c 65 63 74 44 65 73 74  truct SelectDest
694e0 20 7b 0a 20 20 75 38 20 65 44 65 73 74 3b 20 20   {.  u8 eDest;  
694f0 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f         /* How to
69500 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68 65 20   dispose of the 
69510 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 75 38 20  results */.  u8 
69520 61 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 2f  affinity;      /
69530 2a 20 41 66 66 69 6e 69 74 79 20 75 73 65 64 20  * Affinity used 
69540 77 68 65 6e 20 65 44 65 73 74 3d 3d 53 52 54 5f  when eDest==SRT_
69550 53 65 74 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61  Set */.  int iPa
69560 72 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20  rm;        /* A 
69570 70 61 72 61 6d 65 74 65 72 20 75 73 65 64 20 62  parameter used b
69580 79 20 74 68 65 20 65 44 65 73 74 20 64 69 73 70  y the eDest disp
69590 6f 73 61 6c 20 6d 65 74 68 6f 64 20 2a 2f 0a 20  osal method */. 
695a0 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20   int iMem;      
695b0 20 20 20 2f 2a 20 42 61 73 65 20 72 65 67 69 73     /* Base regis
695c0 74 65 72 20 77 68 65 72 65 20 72 65 73 75 6c 74  ter where result
695d0 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 2a 2f  s are written */
695e0 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20  .  int nMem;    
695f0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
69600 66 20 72 65 67 69 73 74 65 72 73 20 61 6c 6c 6f  f registers allo
69610 63 61 74 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  cated */.};../*.
69620 2a 2a 20 44 75 72 69 6e 67 20 63 6f 64 65 20 67  ** During code g
69630 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20 73 74 61  eneration of sta
69640 74 65 6d 65 6e 74 73 20 74 68 61 74 20 64 6f 20  tements that do 
69650 69 6e 73 65 72 74 73 20 69 6e 74 6f 20 41 55 54  inserts into AUT
69660 4f 49 4e 43 52 45 4d 45 4e 54 20 0a 2a 2a 20 74  OINCREMENT .** t
69670 61 62 6c 65 73 2c 20 74 68 65 20 66 6f 6c 6c 6f  ables, the follo
69680 77 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  wing information
69690 20 69 73 20 61 74 74 61 63 68 65 64 20 74 6f 20   is attached to 
696a0 74 68 65 20 54 61 62 6c 65 2e 75 2e 61 75 74 6f  the Table.u.auto
696b0 49 6e 63 2e 70 0a 2a 2a 20 70 6f 69 6e 74 65 72  Inc.p.** pointer
696c0 20 6f 66 20 65 61 63 68 20 61 75 74 6f 69 6e 63   of each autoinc
696d0 72 65 6d 65 6e 74 20 74 61 62 6c 65 20 74 6f 20  rement table to 
696e0 72 65 63 6f 72 64 20 73 6f 6d 65 20 73 69 64 65  record some side
696f0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61   information tha
69700 74 0a 2a 2a 20 74 68 65 20 63 6f 64 65 20 67 65  t.** the code ge
69710 6e 65 72 61 74 6f 72 20 6e 65 65 64 73 2e 20 20  nerator needs.  
69720 57 65 20 68 61 76 65 20 74 6f 20 6b 65 65 70 20  We have to keep 
69730 70 65 72 2d 74 61 62 6c 65 20 61 75 74 6f 69 6e  per-table autoin
69740 63 72 65 6d 65 6e 74 0a 2a 2a 20 69 6e 66 6f 72  crement.** infor
69750 6d 61 74 69 6f 6e 20 69 6e 20 63 61 73 65 20 69  mation in case i
69760 6e 73 65 72 74 73 20 61 72 65 20 64 6f 77 6e 20  nserts are down 
69770 77 69 74 68 69 6e 20 74 72 69 67 67 65 72 73 2e  within triggers.
69780 20 20 54 72 69 67 67 65 72 73 20 64 6f 20 6e 6f    Triggers do no
69790 74 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 63 6f  t.** normally co
697a0 6f 72 64 69 6e 61 74 65 20 74 68 65 69 72 20 61  ordinate their a
697b0 63 74 69 76 69 74 69 65 73 2c 20 62 75 74 20 77  ctivities, but w
697c0 65 20 64 6f 20 6e 65 65 64 20 74 6f 20 63 6f 6f  e do need to coo
697d0 72 64 69 6e 61 74 65 20 74 68 65 0a 2a 2a 20 6c  rdinate the.** l
697e0 6f 61 64 69 6e 67 20 61 6e 64 20 73 61 76 69 6e  oading and savin
697f0 67 20 6f 66 20 61 75 74 6f 69 6e 63 72 65 6d 65  g of autoincreme
69800 6e 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  nt information..
69810 2a 2f 0a 73 74 72 75 63 74 20 41 75 74 6f 69 6e  */.struct Autoin
69820 63 49 6e 66 6f 20 7b 0a 20 20 41 75 74 6f 69 6e  cInfo {.  Autoin
69830 63 49 6e 66 6f 20 2a 70 4e 65 78 74 3b 20 20 20  cInfo *pNext;   
69840 2f 2a 20 4e 65 78 74 20 69 6e 66 6f 20 62 6c 6f  /* Next info blo
69850 63 6b 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 20  ck in a list of 
69860 74 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 54 61  them all */.  Ta
69870 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20  ble *pTab;      
69880 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 68 69      /* Table thi
69890 73 20 69 6e 66 6f 20 62 6c 6f 63 6b 20 72 65 66  s info block ref
698a0 65 72 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20  ers to */.  int 
698b0 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
698c0 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 73 71    /* Index in sq
698d0 6c 69 74 65 33 2e 61 44 62 5b 5d 20 6f 66 20 64  lite3.aDb[] of d
698e0 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20  atabase holding 
698f0 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65  pTab */.  int re
69900 67 43 74 72 3b 20 20 20 20 20 20 20 20 20 20 20  gCtr;           
69910 2f 2a 20 4d 65 6d 6f 72 79 20 72 65 67 69 73 74  /* Memory regist
69920 65 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 72  er holding the r
69930 6f 77 69 64 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  owid counter */.
69940 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 65 20 6f  };../*.** Size o
69950 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63  f the column cac
69960 68 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  he.*/.#ifndef SQ
69970 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 0a  LITE_N_COLCACHE.
69980 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
69990 4e 5f 43 4f 4c 43 41 43 48 45 20 31 30 0a 23 65  N_COLCACHE 10.#e
699a0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c  ndif../*.** At l
699b0 65 61 73 74 20 6f 6e 65 20 69 6e 73 74 61 6e 63  east one instanc
699c0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
699d0 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ng structure is 
699e0 63 72 65 61 74 65 64 20 66 6f 72 20 65 61 63 68  created for each
699f0 20 0a 2a 2a 20 74 72 69 67 67 65 72 20 74 68 61   .** trigger tha
69a00 74 20 6d 61 79 20 62 65 20 66 69 72 65 64 20 77  t may be fired w
69a10 68 69 6c 65 20 70 61 72 73 69 6e 67 20 61 6e 20  hile parsing an 
69a20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 20 6f  INSERT, UPDATE o
69a30 72 20 44 45 4c 45 54 45 0a 2a 2a 20 73 74 61 74  r DELETE.** stat
69a40 65 6d 65 6e 74 2e 20 41 6c 6c 20 73 75 63 68 20  ement. All such 
69a50 6f 62 6a 65 63 74 73 20 61 72 65 20 73 74 6f 72  objects are stor
69a60 65 64 20 69 6e 20 74 68 65 20 6c 69 6e 6b 65 64  ed in the linked
69a70 20 6c 69 73 74 20 68 65 61 64 65 64 20 61 74 0a   list headed at.
69a80 2a 2a 20 50 61 72 73 65 2e 70 54 72 69 67 67 65  ** Parse.pTrigge
69a90 72 50 72 67 20 61 6e 64 20 64 65 6c 65 74 65 64  rPrg and deleted
69aa0 20 6f 6e 63 65 20 73 74 61 74 65 6d 65 6e 74 20   once statement 
69ab0 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 68 61 73 20  compilation has 
69ac0 62 65 65 6e 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65  been.** complete
69ad0 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 56 64 62 65 20  d..**.** A Vdbe 
69ae0 73 75 62 2d 70 72 6f 67 72 61 6d 20 74 68 61 74  sub-program that
69af0 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
69b00 62 6f 64 79 20 61 6e 64 20 57 48 45 4e 20 63 6c  body and WHEN cl
69b10 61 75 73 65 20 6f 66 20 74 72 69 67 67 65 72 0a  ause of trigger.
69b20 2a 2a 20 54 72 69 67 67 65 72 50 72 67 2e 70 54  ** TriggerPrg.pT
69b30 72 69 67 67 65 72 2c 20 61 73 73 75 6d 69 6e 67  rigger, assuming
69b40 20 61 20 64 65 66 61 75 6c 74 20 4f 4e 20 43 4f   a default ON CO
69b50 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 20 6f 66  NFLICT clause of
69b60 0a 2a 2a 20 54 72 69 67 67 65 72 50 72 67 2e 6f  .** TriggerPrg.o
69b70 72 63 6f 6e 66 2c 20 69 73 20 73 74 6f 72 65 64  rconf, is stored
69b80 20 69 6e 20 74 68 65 20 54 72 69 67 67 65 72 50   in the TriggerP
69b90 72 67 2e 70 50 72 6f 67 72 61 6d 20 76 61 72 69  rg.pProgram vari
69ba0 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 50 61 72  able..** The Par
69bb0 73 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 6c  se.pTriggerPrg l
69bc0 69 73 74 20 6e 65 76 65 72 20 63 6f 6e 74 61 69  ist never contai
69bd0 6e 73 20 74 77 6f 20 65 6e 74 72 69 65 73 20 77  ns two entries w
69be0 69 74 68 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20  ith the same.** 
69bf0 76 61 6c 75 65 73 20 66 6f 72 20 62 6f 74 68 20  values for both 
69c00 70 54 72 69 67 67 65 72 20 61 6e 64 20 6f 72 63  pTrigger and orc
69c10 6f 6e 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54  onf..**.** The T
69c20 72 69 67 67 65 72 50 72 67 2e 6f 6c 64 6d 61 73  riggerPrg.oldmas
69c30 6b 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65  k variable is se
69c40 74 20 74 6f 20 61 20 6d 61 73 6b 20 6f 66 20 6f  t to a mask of o
69c50 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  ld.* columns.** 
69c60 61 63 63 65 73 73 65 64 20 28 6f 72 20 73 65 74  accessed (or set
69c70 20 74 6f 20 30 20 66 6f 72 20 74 72 69 67 67 65   to 0 for trigge
69c80 72 73 20 66 69 72 65 64 20 61 73 20 61 20 72 65  rs fired as a re
69c90 73 75 6c 74 20 6f 66 20 49 4e 53 45 52 54 20 0a  sult of INSERT .
69ca0 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 29 2e 0a  ** statements)..
69cb0 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65  */.struct Trigge
69cc0 72 50 72 67 20 7b 0a 20 20 54 72 69 67 67 65 72  rPrg {.  Trigger
69cd0 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 20 20   *pTrigger;     
69ce0 20 2f 2a 20 54 72 69 67 67 65 72 20 74 68 69 73   /* Trigger this
69cf0 20 70 72 6f 67 72 61 6d 20 77 61 73 20 63 6f 64   program was cod
69d00 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74  ed from */.  int
69d10 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20   orconf;        
69d20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20       /* Default 
69d30 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69  ON CONFLICT poli
69d40 63 79 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72  cy */.  SubProgr
69d50 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 20 20  am *pProgram;   
69d60 2f 2a 20 50 72 6f 67 72 61 6d 20 69 6d 70 6c 65  /* Program imple
69d70 6d 65 6e 74 69 6e 67 20 70 54 72 69 67 67 65 72  menting pTrigger
69d80 2f 6f 72 63 6f 6e 66 20 2a 2f 0a 20 20 75 33 32  /orconf */.  u32
69d90 20 6f 6c 64 6d 61 73 6b 3b 20 20 20 20 20 20 20   oldmask;       
69da0 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
69db0 6f 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 20 61 63  old.* columns ac
69dc0 63 65 73 73 65 64 20 2a 2f 0a 20 20 54 72 69 67  cessed */.  Trig
69dd0 67 65 72 50 72 67 20 2a 70 4e 65 78 74 3b 20 20  gerPrg *pNext;  
69de0 20 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e 74 72      /* Next entr
69df0 79 20 69 6e 20 50 61 72 73 65 2e 70 54 72 69 67  y in Parse.pTrig
69e00 67 65 72 50 72 67 20 6c 69 73 74 20 2a 2f 0a 7d  gerPrg list */.}
69e10 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20  ;../*.** An SQL 
69e20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e 20  parser context. 
69e30 20 41 20 63 6f 70 79 20 6f 66 20 74 68 69 73 20   A copy of this 
69e40 73 74 72 75 63 74 75 72 65 20 69 73 20 70 61 73  structure is pas
69e50 73 65 64 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74  sed through.** t
69e60 68 65 20 70 61 72 73 65 72 20 61 6e 64 20 64 6f  he parser and do
69e70 77 6e 20 69 6e 74 6f 20 61 6c 6c 20 74 68 65 20  wn into all the 
69e80 70 61 72 73 65 72 20 61 63 74 69 6f 6e 20 72 6f  parser action ro
69e90 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74  utine in order t
69ea0 6f 0a 2a 2a 20 63 61 72 72 79 20 61 72 6f 75 6e  o.** carry aroun
69eb0 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68  d information th
69ec0 61 74 20 69 73 20 67 6c 6f 62 61 6c 20 74 6f 20  at is global to 
69ed0 74 68 65 20 65 6e 74 69 72 65 20 70 61 72 73 65  the entire parse
69ee0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 72 75  ..**.** The stru
69ef0 63 74 75 72 65 20 69 73 20 64 69 76 69 64 65 64  cture is divided
69f00 20 69 6e 74 6f 20 74 77 6f 20 70 61 72 74 73 2e   into two parts.
69f10 20 20 57 68 65 6e 20 74 68 65 20 70 61 72 73 65    When the parse
69f20 72 20 61 6e 64 20 63 6f 64 65 0a 2a 2a 20 67 65  r and code.** ge
69f30 6e 65 72 61 74 65 20 63 61 6c 6c 20 74 68 65 6d  nerate call them
69f40 73 65 6c 76 65 73 20 72 65 63 75 72 73 69 76 65  selves recursive
69f50 6c 79 2c 20 74 68 65 20 66 69 72 73 74 20 70 61  ly, the first pa
69f60 72 74 20 6f 66 20 74 68 65 20 73 74 72 75 63 74  rt of the struct
69f70 75 72 65 0a 2a 2a 20 69 73 20 63 6f 6e 73 74 61  ure.** is consta
69f80 6e 74 20 62 75 74 20 74 68 65 20 73 65 63 6f 6e  nt but the secon
69f90 64 20 70 61 72 74 20 69 73 20 72 65 73 65 74 20  d part is reset 
69fa0 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
69fb0 20 61 6e 64 20 65 6e 64 20 6f 66 0a 2a 2a 20 65   and end of.** e
69fc0 61 63 68 20 72 65 63 75 72 73 69 6f 6e 2e 0a 2a  ach recursion..*
69fd0 2a 0a 2a 2a 20 54 68 65 20 6e 54 61 62 6c 65 4c  *.** The nTableL
69fe0 6f 63 6b 20 61 6e 64 20 61 54 61 62 6c 65 4c 6f  ock and aTableLo
69ff0 63 6b 20 76 61 72 69 61 62 6c 65 73 20 61 72 65  ck variables are
6a000 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 74 68   only used if th
6a010 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 0a  e shared-cache .
6a020 2a 2a 20 66 65 61 74 75 72 65 20 69 73 20 65 6e  ** feature is en
6a030 61 62 6c 65 64 20 28 69 66 20 73 71 6c 69 74 65  abled (if sqlite
6a040 33 54 73 64 28 29 2d 3e 75 73 65 53 68 61 72 65  3Tsd()->useShare
6a050 64 44 61 74 61 20 69 73 20 74 72 75 65 29 2e 20  dData is true). 
6a060 54 68 65 79 20 61 72 65 0a 2a 2a 20 75 73 65 64  They are.** used
6a070 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 65   to store the se
6a080 74 20 6f 66 20 74 61 62 6c 65 2d 6c 6f 63 6b 73  t of table-locks
6a090 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65   required by the
6a0a0 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67   statement being
6a0b0 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2e 20 46 75  .** compiled. Fu
6a0c0 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 54 61  nction sqlite3Ta
6a0d0 62 6c 65 4c 6f 63 6b 28 29 20 69 73 20 75 73 65  bleLock() is use
6a0e0 64 20 74 6f 20 61 64 64 20 65 6e 74 72 69 65 73  d to add entries
6a0f0 20 74 6f 20 74 68 65 0a 2a 2a 20 6c 69 73 74 2e   to the.** list.
6a100 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61 72 73 65  .*/.struct Parse
6a110 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62   {.  sqlite3 *db
6a120 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;         /* The
6a130 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73   main database s
6a140 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e  tructure */.  in
6a150 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
6a160 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
6a170 65 20 66 72 6f 6d 20 65 78 65 63 75 74 69 6f 6e  e from execution
6a180 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72   */.  char *zErr
6a190 4d 73 67 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e  Msg;       /* An
6a1a0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a   error message *
6a1b0 2f 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b  /.  Vdbe *pVdbe;
6a1c0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65           /* An e
6a1d0 6e 67 69 6e 65 20 66 6f 72 20 65 78 65 63 75 74  ngine for execut
6a1e0 69 6e 67 20 64 61 74 61 62 61 73 65 20 62 79 74  ing database byt
6a1f0 65 63 6f 64 65 20 2a 2f 0a 20 20 75 38 20 63 6f  ecode */.  u8 co
6a200 6c 4e 61 6d 65 73 53 65 74 3b 20 20 20 20 20 20  lNamesSet;      
6a210 2f 2a 20 54 52 55 45 20 61 66 74 65 72 20 4f 50  /* TRUE after OP
6a220 5f 43 6f 6c 75 6d 6e 4e 61 6d 65 20 68 61 73 20  _ColumnName has 
6a230 62 65 65 6e 20 69 73 73 75 65 64 20 74 6f 20 70  been issued to p
6a240 56 64 62 65 20 2a 2f 0a 20 20 75 38 20 6e 61 6d  Vdbe */.  u8 nam
6a250 65 43 6c 61 73 68 3b 20 20 20 20 20 20 20 20 2f  eClash;        /
6a260 2a 20 41 20 70 65 72 6d 61 6e 65 6e 74 20 74 61  * A permanent ta
6a270 62 6c 65 20 6e 61 6d 65 20 63 6c 61 73 68 65 73  ble name clashes
6a280 20 77 69 74 68 20 74 65 6d 70 20 74 61 62 6c 65   with temp table
6a290 20 6e 61 6d 65 20 2a 2f 0a 20 20 75 38 20 63 68   name */.  u8 ch
6a2a0 65 63 6b 53 63 68 65 6d 61 3b 20 20 20 20 20 20  eckSchema;      
6a2b0 2f 2a 20 43 61 75 73 65 73 20 73 63 68 65 6d 61  /* Causes schema
6a2c0 20 63 6f 6f 6b 69 65 20 63 68 65 63 6b 20 61 66   cookie check af
6a2d0 74 65 72 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a  ter an error */.
6a2e0 20 20 75 38 20 6e 65 73 74 65 64 3b 20 20 20 20    u8 nested;    
6a2f0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6a300 20 6f 66 20 6e 65 73 74 65 64 20 63 61 6c 6c 73   of nested calls
6a310 20 74 6f 20 74 68 65 20 70 61 72 73 65 72 2f 63   to the parser/c
6a320 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 2a 2f  ode generator */
6a330 0a 20 20 75 38 20 70 61 72 73 65 45 72 72 6f 72  .  u8 parseError
6a340 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  ;       /* True 
6a350 61 66 74 65 72 20 61 20 70 61 72 73 69 6e 67 20  after a parsing 
6a360 65 72 72 6f 72 2e 20 20 54 69 63 6b 65 74 20 23  error.  Ticket #
6a370 31 37 39 34 20 2a 2f 0a 20 20 75 38 20 6e 54 65  1794 */.  u8 nTe
6a380 6d 70 52 65 67 3b 20 20 20 20 20 20 20 20 20 2f  mpReg;         /
6a390 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 6d 70  * Number of temp
6a3a0 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20  orary registers 
6a3b0 69 6e 20 61 54 65 6d 70 52 65 67 5b 5d 20 2a 2f  in aTempReg[] */
6a3c0 0a 20 20 75 38 20 6e 54 65 6d 70 49 6e 55 73 65  .  u8 nTempInUse
6a3d0 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
6a3e0 72 20 6f 66 20 61 54 65 6d 70 52 65 67 5b 5d 20  r of aTempReg[] 
6a3f0 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65  currently checke
6a400 64 20 6f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 61  d out */.  int a
6a410 54 65 6d 70 52 65 67 5b 38 5d 3b 20 20 20 20 20  TempReg[8];     
6a420 2f 2a 20 48 6f 6c 64 69 6e 67 20 61 72 65 61 20  /* Holding area 
6a430 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 72 65  for temporary re
6a440 67 69 73 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74  gisters */.  int
6a450 20 6e 52 61 6e 67 65 52 65 67 3b 20 20 20 20 20   nRangeReg;     
6a460 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
6a470 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73   temporary regis
6a480 74 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69  ter block */.  i
6a490 6e 74 20 69 52 61 6e 67 65 52 65 67 3b 20 20 20  nt iRangeReg;   
6a4a0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 72 65 67      /* First reg
6a4b0 69 73 74 65 72 20 69 6e 20 74 65 6d 70 6f 72 61  ister in tempora
6a4c0 72 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63  ry register bloc
6a4d0 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b  k */.  int nErr;
6a4e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
6a4f0 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20  umber of errors 
6a500 73 65 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  seen */.  int nT
6a510 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ab;            /
6a520 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 72 65 76  * Number of prev
6a530 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64  iously allocated
6a540 20 56 44 42 45 20 63 75 72 73 6f 72 73 20 2a 2f   VDBE cursors */
6a550 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20  .  int nMem;    
6a560 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
6a570 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  r of memory cell
6a580 73 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f  s used so far */
6a590 0a 20 20 69 6e 74 20 6e 53 65 74 3b 20 20 20 20  .  int nSet;    
6a5a0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
6a5b0 72 20 6f 66 20 73 65 74 73 20 75 73 65 64 20 73  r of sets used s
6a5c0 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 63  o far */.  int c
6a5d0 6b 42 61 73 65 3b 20 20 20 20 20 20 20 20 20 20  kBase;          
6a5e0 2f 2a 20 42 61 73 65 20 72 65 67 69 73 74 65 72  /* Base register
6a5f0 20 6f 66 20 64 61 74 61 20 64 75 72 69 6e 67 20   of data during 
6a600 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74  check constraint
6a610 73 20 2a 2f 0a 20 20 69 6e 74 20 69 43 61 63 68  s */.  int iCach
6a620 65 4c 65 76 65 6c 3b 20 20 20 20 20 2f 2a 20 43  eLevel;     /* C
6a630 6f 6c 43 61 63 68 65 20 76 61 6c 69 64 20 77 68  olCache valid wh
6a640 65 6e 20 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 69  en aColCache[].i
6a650 4c 65 76 65 6c 3c 3d 69 43 61 63 68 65 4c 65 76  Level<=iCacheLev
6a660 65 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 43 61 63  el */.  int iCac
6a670 68 65 43 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20  heCnt;       /* 
6a680 43 6f 75 6e 74 65 72 20 75 73 65 64 20 74 6f 20  Counter used to 
6a690 67 65 6e 65 72 61 74 65 20 61 43 6f 6c 43 61 63  generate aColCac
6a6a0 68 65 5b 5d 2e 6c 72 75 20 76 61 6c 75 65 73 20  he[].lru values 
6a6b0 2a 2f 0a 20 20 75 38 20 6e 43 6f 6c 43 61 63 68  */.  u8 nColCach
6a6c0 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  e;        /* Num
6a6d0 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
6a6e0 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63  n the column cac
6a6f0 68 65 20 2a 2f 0a 20 20 75 38 20 69 43 6f 6c 43  he */.  u8 iColC
6a700 61 63 68 65 3b 20 20 20 20 20 20 20 20 2f 2a 20  ache;        /* 
6a710 4e 65 78 74 20 65 6e 74 72 79 20 6f 66 20 74 68  Next entry of th
6a720 65 20 63 61 63 68 65 20 74 6f 20 72 65 70 6c 61  e cache to repla
6a730 63 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 79  ce */.  struct y
6a740 43 6f 6c 43 61 63 68 65 20 7b 0a 20 20 20 20 69  ColCache {.    i
6a750 6e 74 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20  nt iTable;      
6a760 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 75       /* Table cu
6a770 72 73 6f 72 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  rsor number */. 
6a780 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20     int iColumn; 
6a790 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
6a7a0 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20  e column number 
6a7b0 2a 2f 0a 20 20 20 20 75 38 20 61 66 66 43 68 61  */.    u8 affCha
6a7c0 6e 67 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  nge;         /* 
6a7d0 54 72 75 65 20 69 66 20 74 68 69 73 20 72 65 67  True if this reg
6a7e0 69 73 74 65 72 20 68 61 73 20 68 61 64 20 61 6e  ister has had an
6a7f0 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65   affinity change
6a800 20 2a 2f 0a 20 20 20 20 75 38 20 74 65 6d 70 52   */.    u8 tempR
6a810 65 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  eg;           /*
6a820 20 69 52 65 67 20 69 73 20 61 20 74 65 6d 70 20   iReg is a temp 
6a830 72 65 67 69 73 74 65 72 20 74 68 61 74 20 6e 65  register that ne
6a840 65 64 73 20 74 6f 20 62 65 20 66 72 65 65 64 20  eds to be freed 
6a850 2a 2f 0a 20 20 20 20 69 6e 74 20 69 4c 65 76 65  */.    int iLeve
6a860 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  l;           /* 
6a870 4e 65 73 74 69 6e 67 20 6c 65 76 65 6c 20 2a 2f  Nesting level */
6a880 0a 20 20 20 20 69 6e 74 20 69 52 65 67 3b 20 20  .    int iReg;  
6a890 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
6a8a0 67 20 77 69 74 68 20 76 61 6c 75 65 20 6f 66 20  g with value of 
6a8b0 74 68 69 73 20 63 6f 6c 75 6d 6e 2e 20 30 20 6d  this column. 0 m
6a8c0 65 61 6e 73 20 6e 6f 6e 65 2e 20 2a 2f 0a 20 20  eans none. */.  
6a8d0 20 20 69 6e 74 20 6c 72 75 3b 20 20 20 20 20 20    int lru;      
6a8e0 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 61 73 74          /* Least
6a8f0 20 72 65 63 65 6e 74 6c 79 20 75 73 65 64 20 65   recently used e
6a900 6e 74 72 79 20 68 61 73 20 74 68 65 20 73 6d 61  ntry has the sma
6a910 6c 6c 65 73 74 20 76 61 6c 75 65 20 2a 2f 0a 20  llest value */. 
6a920 20 7d 20 61 43 6f 6c 43 61 63 68 65 5b 53 51 4c   } aColCache[SQL
6a930 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 5d 3b  ITE_N_COLCACHE];
6a940 20 20 2f 2a 20 4f 6e 65 20 66 6f 72 20 65 61 63    /* One for eac
6a950 68 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 65  h column cache e
6a960 6e 74 72 79 20 2a 2f 0a 20 20 75 33 32 20 77 72  ntry */.  u32 wr
6a970 69 74 65 4d 61 73 6b 3b 20 20 20 20 20 20 20 2f  iteMask;       /
6a980 2a 20 53 74 61 72 74 20 61 20 77 72 69 74 65 20  * Start a write 
6a990 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74  transaction on t
6a9a0 68 65 73 65 20 64 61 74 61 62 61 73 65 73 20 2a  hese databases *
6a9b0 2f 0a 20 20 75 33 32 20 63 6f 6f 6b 69 65 4d 61  /.  u32 cookieMa
6a9c0 73 6b 3b 20 20 20 20 20 20 2f 2a 20 42 69 74 6d  sk;      /* Bitm
6a9d0 61 73 6b 20 6f 66 20 73 63 68 65 6d 61 20 76 65  ask of schema ve
6a9e0 72 69 66 69 65 64 20 64 61 74 61 62 61 73 65 73  rified databases
6a9f0 20 2a 2f 0a 20 20 75 38 20 69 73 4d 75 6c 74 69   */.  u8 isMulti
6aa00 57 72 69 74 65 3b 20 20 20 20 20 2f 2a 20 54 72  Write;     /* Tr
6aa10 75 65 20 69 66 20 73 74 61 74 65 6d 65 6e 74 20  ue if statement 
6aa20 6d 61 79 20 61 66 66 65 63 74 2f 69 6e 73 65 72  may affect/inser
6aa30 74 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73 20  t multiple rows 
6aa40 2a 2f 0a 20 20 75 38 20 6d 61 79 41 62 6f 72 74  */.  u8 mayAbort
6aa50 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ;         /* Tru
6aa60 65 20 69 66 20 73 74 61 74 65 6d 65 6e 74 20 6d  e if statement m
6aa70 61 79 20 74 68 72 6f 77 20 61 6e 20 41 42 4f 52  ay throw an ABOR
6aa80 54 20 65 78 63 65 70 74 69 6f 6e 20 2a 2f 0a 20  T exception */. 
6aa90 20 69 6e 74 20 63 6f 6f 6b 69 65 47 6f 74 6f 3b   int cookieGoto;
6aaa0 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
6aab0 20 6f 66 20 4f 50 5f 47 6f 74 6f 20 74 6f 20 63   of OP_Goto to c
6aac0 6f 6f 6b 69 65 20 76 65 72 69 66 69 65 72 20 73  ookie verifier s
6aad0 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69  ubroutine */.  i
6aae0 6e 74 20 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 53  nt cookieValue[S
6aaf0 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48  QLITE_MAX_ATTACH
6ab00 45 44 2b 32 5d 3b 20 20 2f 2a 20 56 61 6c 75 65  ED+2];  /* Value
6ab10 73 20 6f 66 20 63 6f 6f 6b 69 65 73 20 74 6f 20  s of cookies to 
6ab20 76 65 72 69 66 79 20 2a 2f 0a 23 69 66 6e 64 65  verify */.#ifnde
6ab30 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
6ab40 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69 6e 74  ARED_CACHE.  int
6ab50 20 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 20 20 20   nTableLock;    
6ab60 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
6ab70 20 6c 6f 63 6b 73 20 69 6e 20 61 54 61 62 6c 65   locks in aTable
6ab80 4c 6f 63 6b 20 2a 2f 0a 20 20 54 61 62 6c 65 4c  Lock */.  TableL
6ab90 6f 63 6b 20 2a 61 54 61 62 6c 65 4c 6f 63 6b 3b  ock *aTableLock;
6aba0 20 2f 2a 20 52 65 71 75 69 72 65 64 20 74 61 62   /* Required tab
6abb0 6c 65 20 6c 6f 63 6b 73 20 66 6f 72 20 73 68 61  le locks for sha
6abc0 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 2a  red-cache mode *
6abd0 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 72  /.#endif.  int r
6abe0 65 67 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20  egRowid;        
6abf0 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64  /* Register hold
6ac00 69 6e 67 20 72 6f 77 69 64 20 6f 66 20 43 52 45  ing rowid of CRE
6ac10 41 54 45 20 54 41 42 4c 45 20 65 6e 74 72 79 20  ATE TABLE entry 
6ac20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 6f 74  */.  int regRoot
6ac30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67  ;         /* Reg
6ac40 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f  ister holding ro
6ac50 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66  ot page number f
6ac60 6f 72 20 6e 65 77 20 6f 62 6a 65 63 74 73 20 2a  or new objects *
6ac70 2f 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20  /.  AutoincInfo 
6ac80 2a 70 41 69 6e 63 3b 20 20 2f 2a 20 49 6e 66 6f  *pAinc;  /* Info
6ac90 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 41 55  rmation about AU
6aca0 54 4f 49 4e 43 52 45 4d 45 4e 54 20 63 6f 75 6e  TOINCREMENT coun
6acb0 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d  ters */.  int nM
6acc0 61 78 41 72 67 3b 20 20 20 20 20 20 20 20 20 2f  axArg;         /
6acd0 2a 20 4d 61 78 20 61 72 67 73 20 70 61 73 73 65  * Max args passe
6ace0 64 20 74 6f 20 75 73 65 72 20 66 75 6e 63 74 69  d to user functi
6acf0 6f 6e 20 62 79 20 73 75 62 2d 70 72 6f 67 72 61  on by sub-progra
6ad00 6d 20 2a 2f 0a 0a 20 20 2f 2a 20 49 6e 66 6f 72  m */..  /* Infor
6ad10 6d 61 74 69 6f 6e 20 75 73 65 64 20 77 68 69 6c  mation used whil
6ad20 65 20 63 6f 64 69 6e 67 20 74 72 69 67 67 65 72  e coding trigger
6ad30 20 70 72 6f 67 72 61 6d 73 2e 20 2a 2f 0a 20 20   programs. */.  
6ad40 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c  Parse *pToplevel
6ad50 3b 20 20 20 20 2f 2a 20 50 61 72 73 65 20 73 74  ;    /* Parse st
6ad60 72 75 63 74 75 72 65 20 66 6f 72 20 6d 61 69 6e  ructure for main
6ad70 20 70 72 6f 67 72 61 6d 20 28 6f 72 20 4e 55 4c   program (or NUL
6ad80 4c 29 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  L) */.  Table *p
6ad90 54 72 69 67 67 65 72 54 61 62 3b 20 20 2f 2a 20  TriggerTab;  /* 
6ada0 54 61 62 6c 65 20 74 72 69 67 67 65 72 73 20 61  Table triggers a
6adb0 72 65 20 62 65 69 6e 67 20 63 6f 64 65 64 20 66  re being coded f
6adc0 6f 72 20 2a 2f 0a 20 20 75 33 32 20 6f 6c 64 6d  or */.  u32 oldm
6add0 61 73 6b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ask;         /* 
6ade0 4d 61 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f  Mask of old.* co
6adf0 6c 75 6d 6e 73 20 72 65 66 65 72 65 6e 63 65 64  lumns referenced
6ae00 20 2a 2f 0a 20 20 75 38 20 65 54 72 69 67 67 65   */.  u8 eTrigge
6ae10 72 4f 70 3b 20 20 20 20 20 20 20 2f 2a 20 54 4b  rOp;       /* TK
6ae20 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45  _UPDATE, TK_INSE
6ae30 52 54 20 6f 72 20 54 4b 5f 44 45 4c 45 54 45 20  RT or TK_DELETE 
6ae40 2a 2f 0a 20 20 75 38 20 65 4f 72 63 6f 6e 66 3b  */.  u8 eOrconf;
6ae50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66            /* Def
6ae60 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54  ault ON CONFLICT
6ae70 20 70 6f 6c 69 63 79 20 66 6f 72 20 74 72 69 67   policy for trig
6ae80 67 65 72 20 73 74 65 70 73 20 2a 2f 0a 20 20 75  ger steps */.  u
6ae90 38 20 64 69 73 61 62 6c 65 54 72 69 67 67 65 72  8 disableTrigger
6aea0 73 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 64  s;  /* True to d
6aeb0 69 73 61 62 6c 65 20 74 72 69 67 67 65 72 73 20  isable triggers 
6aec0 2a 2f 0a 0a 20 20 2f 2a 20 41 62 6f 76 65 20 69  */..  /* Above i
6aed0 73 20 63 6f 6e 73 74 61 6e 74 20 62 65 74 77 65  s constant betwe
6aee0 65 6e 20 72 65 63 75 72 73 69 6f 6e 73 2e 20 20  en recursions.  
6aef0 42 65 6c 6f 77 20 69 73 20 72 65 73 65 74 20 62  Below is reset b
6af00 65 66 6f 72 65 20 61 6e 64 20 61 66 74 65 72 0a  efore and after.
6af10 20 20 2a 2a 20 65 61 63 68 20 72 65 63 75 72 73    ** each recurs
6af20 69 6f 6e 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 56  ion */..  int nV
6af30 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ar;            /
6af40 2a 20 4e 75 6d 62 65 72 20 6f 66 20 27 3f 27 20  * Number of '?' 
6af50 76 61 72 69 61 62 6c 65 73 20 73 65 65 6e 20 69  variables seen i
6af60 6e 20 74 68 65 20 53 51 4c 20 73 6f 20 66 61 72  n the SQL so far
6af70 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 45 78   */.  int nVarEx
6af80 70 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  pr;        /* Nu
6af90 6d 62 65 72 20 6f 66 20 75 73 65 64 20 73 6c 6f  mber of used slo
6afa0 74 73 20 69 6e 20 61 70 56 61 72 45 78 70 72 5b  ts in apVarExpr[
6afb0 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 45  ] */.  int nVarE
6afc0 78 70 72 41 6c 6c 6f 63 3b 20 20 20 2f 2a 20 4e  xprAlloc;   /* N
6afd0 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74  umber of allocat
6afe0 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 56 61  ed slots in apVa
6aff0 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 45 78 70  rExpr[] */.  Exp
6b000 72 20 2a 2a 61 70 56 61 72 45 78 70 72 3b 20 20  r **apVarExpr;  
6b010 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f    /* Pointers to
6b020 20 3a 61 61 61 20 61 6e 64 20 24 61 61 61 61 20   :aaa and $aaaa 
6b030 77 69 6c 64 63 61 72 64 20 65 78 70 72 65 73 73  wildcard express
6b040 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 41  ions */.  int nA
6b050 6c 69 61 73 3b 20 20 20 20 20 20 20 20 20 20 2f  lias;          /
6b060 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c 69 61  * Number of alia
6b070 73 65 64 20 72 65 73 75 6c 74 20 73 65 74 20 63  sed result set c
6b080 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  olumns */.  int 
6b090 6e 41 6c 69 61 73 41 6c 6c 6f 63 3b 20 20 20 20  nAliasAlloc;    
6b0a0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 6c   /* Number of al
6b0b0 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 66 6f  located slots fo
6b0c0 72 20 61 41 6c 69 61 73 5b 5d 20 2a 2f 0a 20 20  r aAlias[] */.  
6b0d0 69 6e 74 20 2a 61 41 6c 69 61 73 3b 20 20 20 20  int *aAlias;    
6b0e0 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
6b0f0 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 61 6c   used to hold al
6b100 69 61 73 65 64 20 72 65 73 75 6c 74 20 2a 2f 0a  iased result */.
6b110 20 20 75 38 20 65 78 70 6c 61 69 6e 3b 20 20 20    u8 explain;   
6b120 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
6b130 66 20 74 68 65 20 45 58 50 4c 41 49 4e 20 66 6c  f the EXPLAIN fl
6b140 61 67 20 69 73 20 66 6f 75 6e 64 20 6f 6e 20 74  ag is found on t
6b150 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 54 6f  he query */.  To
6b160 6b 65 6e 20 73 4e 61 6d 65 54 6f 6b 65 6e 3b 20  ken sNameToken; 
6b170 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 77 69 74 68     /* Token with
6b180 20 75 6e 71 75 61 6c 69 66 69 65 64 20 73 63 68   unqualified sch
6b190 65 6d 61 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20  ema object name 
6b1a0 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 4c 61 73 74  */.  Token sLast
6b1b0 54 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20 54 68 65  Token;    /* The
6b1c0 20 6c 61 73 74 20 74 6f 6b 65 6e 20 70 61 72 73   last token pars
6b1d0 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ed */.  const ch
6b1e0 61 72 20 2a 7a 54 61 69 6c 3b 20 20 20 2f 2a 20  ar *zTail;   /* 
6b1f0 41 6c 6c 20 53 51 4c 20 74 65 78 74 20 70 61 73  All SQL text pas
6b200 74 20 74 68 65 20 6c 61 73 74 20 73 65 6d 69 63  t the last semic
6b210 6f 6c 6f 6e 20 70 61 72 73 65 64 20 2a 2f 0a 20  olon parsed */. 
6b220 20 54 61 62 6c 65 20 2a 70 4e 65 77 54 61 62 6c   Table *pNewTabl
6b230 65 3b 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65  e;    /* A table
6b240 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74   being construct
6b250 65 64 20 62 79 20 43 52 45 41 54 45 20 54 41 42  ed by CREATE TAB
6b260 4c 45 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20  LE */.  Trigger 
6b270 2a 70 4e 65 77 54 72 69 67 67 65 72 3b 20 20 20  *pNewTrigger;   
6b280 20 20 2f 2a 20 54 72 69 67 67 65 72 20 75 6e 64    /* Trigger und
6b290 65 72 20 63 6f 6e 73 74 72 75 63 74 20 62 79 20  er construct by 
6b2a0 61 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52  a CREATE TRIGGER
6b2b0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
6b2c0 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 20   *zAuthContext; 
6b2d0 2f 2a 20 54 68 65 20 36 74 68 20 70 61 72 61 6d  /* The 6th param
6b2e0 65 74 65 72 20 74 6f 20 64 62 2d 3e 78 41 75 74  eter to db->xAut
6b2f0 68 20 63 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a 23  h callbacks */.#
6b300 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
6b310 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
6b320 20 20 54 6f 6b 65 6e 20 73 41 72 67 3b 20 20 20    Token sArg;   
6b330 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6b340 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66  Complete text of
6b350 20 61 20 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65   a module argume
6b360 6e 74 20 2a 2f 0a 20 20 75 38 20 64 65 63 6c 61  nt */.  u8 decla
6b370 72 65 56 74 61 62 3b 20 20 20 20 20 20 20 20 20  reVtab;         
6b380 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e     /* True if in
6b390 73 69 64 65 20 73 71 6c 69 74 65 33 5f 64 65 63  side sqlite3_dec
6b3a0 6c 61 72 65 5f 76 74 61 62 28 29 20 2a 2f 0a 20  lare_vtab() */. 
6b3b0 20 69 6e 74 20 6e 56 74 61 62 4c 6f 63 6b 3b 20   int nVtabLock; 
6b3c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
6b3d0 75 6d 62 65 72 20 6f 66 20 76 69 72 74 75 61 6c  umber of virtual
6b3e0 20 74 61 62 6c 65 73 20 74 6f 20 6c 6f 63 6b 20   tables to lock 
6b3f0 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 2a 61 70 56  */.  Table **apV
6b400 74 61 62 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20  tabLock;        
6b410 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 69  /* Pointer to vi
6b420 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6e 65 65  rtual tables nee
6b430 64 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a  ding locking */.
6b440 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 6e 48 65  #endif.  int nHe
6b450 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20  ight;           
6b460 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74   /* Expression t
6b470 72 65 65 20 68 65 69 67 68 74 20 6f 66 20 63 75  ree height of cu
6b480 72 72 65 6e 74 20 73 75 62 2d 73 65 6c 65 63 74  rrent sub-select
6b490 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 5a 6f   */.  Table *pZo
6b4a0 6d 62 69 65 54 61 62 3b 20 20 20 20 20 20 2f 2a  mbieTab;      /*
6b4b0 20 4c 69 73 74 20 6f 66 20 54 61 62 6c 65 20 6f   List of Table o
6b4c0 62 6a 65 63 74 73 20 74 6f 20 64 65 6c 65 74 65  bjects to delete
6b4d0 20 61 66 74 65 72 20 63 6f 64 65 20 67 65 6e 20   after code gen 
6b4e0 2a 2f 0a 20 20 54 72 69 67 67 65 72 50 72 67 20  */.  TriggerPrg 
6b4f0 2a 70 54 72 69 67 67 65 72 50 72 67 3b 20 20 20  *pTriggerPrg;   
6b500 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20   /* Linked list 
6b510 6f 66 20 63 6f 64 65 64 20 74 72 69 67 67 65 72  of coded trigger
6b520 73 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20  s */.};..#ifdef 
6b530 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
6b540 55 41 4c 54 41 42 4c 45 0a 20 20 23 64 65 66 69  UALTABLE.  #defi
6b550 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54  ne IN_DECLARE_VT
6b560 41 42 20 30 0a 23 65 6c 73 65 0a 20 20 23 64 65  AB 0.#else.  #de
6b570 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45 5f  fine IN_DECLARE_
6b580 56 54 41 42 20 28 70 50 61 72 73 65 2d 3e 64 65  VTAB (pParse->de
6b590 63 6c 61 72 65 56 74 61 62 29 0a 23 65 6e 64 69  clareVtab).#endi
6b5a0 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  f../*.** An inst
6b5b0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
6b5c0 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
6b5d0 63 61 6e 20 62 65 20 64 65 63 6c 61 72 65 64 20  can be declared 
6b5e0 6f 6e 20 61 20 73 74 61 63 6b 20 61 6e 64 20 75  on a stack and u
6b5f0 73 65 64 0a 2a 2a 20 74 6f 20 73 61 76 65 20 74  sed.** to save t
6b600 68 65 20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f  he Parse.zAuthCo
6b610 6e 74 65 78 74 20 76 61 6c 75 65 20 73 6f 20 74  ntext value so t
6b620 68 61 74 20 69 74 20 63 61 6e 20 62 65 20 72 65  hat it can be re
6b630 73 74 6f 72 65 64 20 6c 61 74 65 72 2e 0a 2a 2f  stored later..*/
6b640 0a 73 74 72 75 63 74 20 41 75 74 68 43 6f 6e 74  .struct AuthCont
6b650 65 78 74 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68  ext {.  const ch
6b660 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78 74  ar *zAuthContext
6b670 3b 20 20 20 2f 2a 20 50 75 74 20 73 61 76 65 64  ;   /* Put saved
6b680 20 50 61 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74   Parse.zAuthCont
6b690 65 78 74 20 68 65 72 65 20 2a 2f 0a 20 20 50 61  ext here */.  Pa
6b6a0 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20  rse *pParse;    
6b6b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
6b6c0 20 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65   Parse structure
6b6d0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69   */.};../*.** Bi
6b6e0 74 66 69 65 6c 64 20 66 6c 61 67 73 20 66 6f 72  tfield flags for
6b6f0 20 50 35 20 76 61 6c 75 65 20 69 6e 20 4f 50 5f   P5 value in OP_
6b700 49 6e 73 65 72 74 20 61 6e 64 20 4f 50 5f 44 65  Insert and OP_De
6b710 6c 65 74 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  lete.*/.#define 
6b720 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 20  OPFLAG_NCHANGE  
6b730 20 20 20 20 20 30 78 30 31 20 20 20 20 2f 2a 20       0x01    /* 
6b740 53 65 74 20 74 6f 20 75 70 64 61 74 65 20 64 62  Set to update db
6b750 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a 23 64 65  ->nChange */.#de
6b760 66 69 6e 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54  fine OPFLAG_LAST
6b770 52 4f 57 49 44 20 20 20 20 20 30 78 30 32 20 20  ROWID     0x02  
6b780 20 20 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 61    /* Set to upda
6b790 74 65 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64  te db->lastRowid
6b7a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c   */.#define OPFL
6b7b0 41 47 5f 49 53 55 50 44 41 54 45 20 20 20 20 20  AG_ISUPDATE     
6b7c0 20 30 78 30 34 20 20 20 20 2f 2a 20 54 68 69 73   0x04    /* This
6b7d0 20 4f 50 5f 49 6e 73 65 72 74 20 69 73 20 61 6e   OP_Insert is an
6b7e0 20 73 71 6c 20 55 50 44 41 54 45 20 2a 2f 0a 23   sql UPDATE */.#
6b7f0 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 41 50  define OPFLAG_AP
6b800 50 45 4e 44 20 20 20 20 20 20 20 20 30 78 30 38  PEND        0x08
6b810 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 6c      /* This is l
6b820 69 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61  ikely to be an a
6b830 70 70 65 6e 64 20 2a 2f 0a 23 64 65 66 69 6e 65  ppend */.#define
6b840 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52   OPFLAG_USESEEKR
6b850 45 53 55 4c 54 20 30 78 31 30 20 20 20 20 2f 2a  ESULT 0x10    /*
6b860 20 54 72 79 20 74 6f 20 61 76 6f 69 64 20 61 20   Try to avoid a 
6b870 73 65 65 6b 20 69 6e 20 42 74 72 65 65 49 6e 73  seek in BtreeIns
6b880 65 72 74 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65  ert() */.#define
6b890 20 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43   OPFLAG_CLEARCAC
6b8a0 48 45 20 20 20 20 30 78 32 30 20 20 20 20 2f 2a  HE    0x20    /*
6b8b0 20 43 6c 65 61 72 20 70 73 65 75 64 6f 2d 74 61   Clear pseudo-ta
6b8c0 62 6c 65 20 63 61 63 68 65 20 69 6e 20 4f 50 5f  ble cache in OP_
6b8d0 43 6f 6c 75 6d 6e 20 2a 2f 0a 0a 2f 2a 0a 20 2a  Column */../*. *
6b8e0 20 45 61 63 68 20 74 72 69 67 67 65 72 20 70 72   Each trigger pr
6b8f0 65 73 65 6e 74 20 69 6e 20 74 68 65 20 64 61 74  esent in the dat
6b900 61 62 61 73 65 20 73 63 68 65 6d 61 20 69 73 20  abase schema is 
6b910 73 74 6f 72 65 64 20 61 73 20 61 6e 20 69 6e 73  stored as an ins
6b920 74 61 6e 63 65 20 6f 66 0a 20 2a 20 73 74 72 75  tance of. * stru
6b930 63 74 20 54 72 69 67 67 65 72 2e 20 0a 20 2a 0a  ct Trigger. . *.
6b940 20 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 69   * Pointers to i
6b950 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 74 72 75  nstances of stru
6b960 63 74 20 54 72 69 67 67 65 72 20 61 72 65 20 73  ct Trigger are s
6b970 74 6f 72 65 64 20 69 6e 20 74 77 6f 20 77 61 79  tored in two way
6b980 73 2e 0a 20 2a 20 31 2e 20 49 6e 20 74 68 65 20  s.. * 1. In the 
6b990 22 74 72 69 67 48 61 73 68 22 20 68 61 73 68 20  "trigHash" hash 
6b9a0 74 61 62 6c 65 20 28 70 61 72 74 20 6f 66 20 74  table (part of t
6b9b0 68 65 20 73 71 6c 69 74 65 33 2a 20 74 68 61 74  he sqlite3* that
6b9c0 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 20   represents the 
6b9d0 0a 20 2a 20 20 20 20 64 61 74 61 62 61 73 65 29  . *    database)
6b9e0 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 54 72  . This allows Tr
6b9f0 69 67 67 65 72 20 73 74 72 75 63 74 75 72 65 73  igger structures
6ba00 20 74 6f 20 62 65 20 72 65 74 72 69 65 76 65 64   to be retrieved
6ba10 20 62 79 20 6e 61 6d 65 2e 0a 20 2a 20 32 2e 20   by name.. * 2. 
6ba20 41 6c 6c 20 74 72 69 67 67 65 72 73 20 61 73 73  All triggers ass
6ba30 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 73  ociated with a s
6ba40 69 6e 67 6c 65 20 74 61 62 6c 65 20 66 6f 72 6d  ingle table form
6ba50 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2c 20   a linked list, 
6ba60 75 73 69 6e 67 20 74 68 65 0a 20 2a 20 20 20 20  using the. *    
6ba70 70 4e 65 78 74 20 6d 65 6d 62 65 72 20 6f 66 20  pNext member of 
6ba80 73 74 72 75 63 74 20 54 72 69 67 67 65 72 2e 20  struct Trigger. 
6ba90 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
6baa0 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f   first element o
6bab0 66 20 74 68 65 0a 20 2a 20 20 20 20 6c 69 6e 6b  f the. *    link
6bac0 65 64 20 6c 69 73 74 20 69 73 20 73 74 6f 72 65  ed list is store
6bad0 64 20 61 73 20 74 68 65 20 22 70 54 72 69 67 67  d as the "pTrigg
6bae0 65 72 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 68  er" member of th
6baf0 65 20 61 73 73 6f 63 69 61 74 65 64 0a 20 2a 20  e associated. * 
6bb00 20 20 20 73 74 72 75 63 74 20 54 61 62 6c 65 2e     struct Table.
6bb10 0a 20 2a 0a 20 2a 20 54 68 65 20 22 73 74 65 70  . *. * The "step
6bb20 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72 20 70 6f  _list" member po
6bb30 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73  ints to the firs
6bb40 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 6c  t element of a l
6bb50 69 6e 6b 65 64 20 6c 69 73 74 0a 20 2a 20 63 6f  inked list. * co
6bb60 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 53 51 4c  ntaining the SQL
6bb70 20 73 74 61 74 65 6d 65 6e 74 73 20 73 70 65 63   statements spec
6bb80 69 66 69 65 64 20 61 73 20 74 68 65 20 74 72 69  ified as the tri
6bb90 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 0a 20 2a  gger program.. *
6bba0 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 72  /.struct Trigger
6bbb0 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65   {.  char *zName
6bbc0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
6bbd0 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
6bbe0 74 72 69 67 67 65 72 20 20 20 20 20 20 20 20 20  trigger         
6bbf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
6bc00 2f 0a 20 20 63 68 61 72 20 2a 74 61 62 6c 65 3b  /.  char *table;
6bc10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
6bc20 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77  he table or view
6bc30 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 74 72   to which the tr
6bc40 69 67 67 65 72 20 61 70 70 6c 69 65 73 20 2a 2f  igger applies */
6bc50 0a 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20  .  u8 op;       
6bc60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
6bc70 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20  e of TK_DELETE, 
6bc80 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e  TK_UPDATE, TK_IN
6bc90 53 45 52 54 20 20 20 20 20 20 20 20 20 2a 2f 0a  SERT         */.
6bca0 20 20 75 38 20 74 72 5f 74 6d 3b 20 20 20 20 20    u8 tr_tm;     
6bcb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
6bcc0 20 6f 66 20 54 52 49 47 47 45 52 5f 42 45 46 4f   of TRIGGER_BEFO
6bcd0 52 45 2c 20 54 52 49 47 47 45 52 5f 41 46 54 45  RE, TRIGGER_AFTE
6bce0 52 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  R */.  Expr *pWh
6bcf0 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  en;            /
6bd00 2a 20 54 68 65 20 57 48 45 4e 20 63 6c 61 75 73  * The WHEN claus
6bd10 65 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73  e of the express
6bd20 69 6f 6e 20 28 6d 61 79 20 62 65 20 4e 55 4c 4c  ion (may be NULL
6bd30 29 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70  ) */.  IdList *p
6bd40 43 6f 6c 75 6d 6e 73 3b 20 20 20 20 20 20 20 2f  Columns;       /
6bd50 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  * If this is an 
6bd60 55 50 44 41 54 45 20 4f 46 20 3c 63 6f 6c 75 6d  UPDATE OF <colum
6bd70 6e 2d 6c 69 73 74 3e 20 74 72 69 67 67 65 72 2c  n-list> trigger,
6bd80 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
6bd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
6bda0 65 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e 20  e <column-list> 
6bdb0 69 73 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a  is stored here *
6bdc0 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68  /.  Schema *pSch
6bdd0 65 6d 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 53  ema;        /* S
6bde0 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67  chema containing
6bdf0 20 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a   the trigger */.
6be00 20 20 53 63 68 65 6d 61 20 2a 70 54 61 62 53 63    Schema *pTabSc
6be10 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20 53 63 68  hema;     /* Sch
6be20 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ema containing t
6be30 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54 72  he table */.  Tr
6be40 69 67 67 65 72 53 74 65 70 20 2a 73 74 65 70 5f  iggerStep *step_
6be50 6c 69 73 74 3b 20 2f 2a 20 4c 69 6e 6b 20 6c 69  list; /* Link li
6be60 73 74 20 6f 66 20 74 72 69 67 67 65 72 20 70 72  st of trigger pr
6be70 6f 67 72 61 6d 20 73 74 65 70 73 20 20 20 20 20  ogram steps     
6be80 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 54 72 69          */.  Tri
6be90 67 67 65 72 20 2a 70 4e 65 78 74 3b 20 20 20 20  gger *pNext;    
6bea0 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 74 72 69       /* Next tri
6beb0 67 67 65 72 20 61 73 73 6f 63 69 61 74 65 64 20  gger associated 
6bec0 77 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 2a  with the table *
6bed0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 72  /.};../*.** A tr
6bee0 69 67 67 65 72 20 69 73 20 65 69 74 68 65 72 20  igger is either 
6bef0 61 20 42 45 46 4f 52 45 20 6f 72 20 61 6e 20 41  a BEFORE or an A
6bf00 46 54 45 52 20 74 72 69 67 67 65 72 2e 20 20 54  FTER trigger.  T
6bf10 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e  he following con
6bf20 73 74 61 6e 74 73 0a 2a 2a 20 64 65 74 65 72 6d  stants.** determ
6bf30 69 6e 65 20 77 68 69 63 68 2e 20 0a 2a 2a 0a 2a  ine which. .**.*
6bf40 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6d  * If there are m
6bf50 75 6c 74 69 70 6c 65 20 74 72 69 67 67 65 72 73  ultiple triggers
6bf60 2c 20 79 6f 75 20 6d 69 67 68 74 20 6f 66 20 73  , you might of s
6bf70 6f 6d 65 20 42 45 46 4f 52 45 20 61 6e 64 20 73  ome BEFORE and s
6bf80 6f 6d 65 20 41 46 54 45 52 2e 0a 2a 2a 20 49 6e  ome AFTER..** In
6bf90 20 74 68 61 74 20 63 61 73 65 73 2c 20 74 68 65   that cases, the
6bfa0 20 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77   constants below
6bfb0 20 63 61 6e 20 62 65 20 4f 52 65 64 20 74 6f 67   can be ORed tog
6bfc0 65 74 68 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ether..*/.#defin
6bfd0 65 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45  e TRIGGER_BEFORE
6bfe0 20 20 31 0a 23 64 65 66 69 6e 65 20 54 52 49 47    1.#define TRIG
6bff0 47 45 52 5f 41 46 54 45 52 20 20 20 32 0a 0a 2f  GER_AFTER   2../
6c000 2a 0a 20 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *. * An instance
6c010 20 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 67   of struct Trigg
6c020 65 72 53 74 65 70 20 69 73 20 75 73 65 64 20 74  erStep is used t
6c030 6f 20 73 74 6f 72 65 20 61 20 73 69 6e 67 6c 65  o store a single
6c040 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 20   SQL statement. 
6c050 2a 20 74 68 61 74 20 69 73 20 61 20 70 61 72 74  * that is a part
6c060 20 6f 66 20 61 20 74 72 69 67 67 65 72 2d 70 72   of a trigger-pr
6c070 6f 67 72 61 6d 2e 20 0a 20 2a 0a 20 2a 20 49 6e  ogram. . *. * In
6c080 73 74 61 6e 63 65 73 20 6f 66 20 73 74 72 75 63  stances of struc
6c090 74 20 54 72 69 67 67 65 72 53 74 65 70 20 61 72  t TriggerStep ar
6c0a0 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 73 69  e stored in a si
6c0b0 6e 67 6c 79 20 6c 69 6e 6b 65 64 20 6c 69 73 74  ngly linked list
6c0c0 20 28 6c 69 6e 6b 65 64 0a 20 2a 20 75 73 69 6e   (linked. * usin
6c0d0 67 20 74 68 65 20 22 70 4e 65 78 74 22 20 6d 65  g the "pNext" me
6c0e0 6d 62 65 72 29 20 72 65 66 65 72 65 6e 63 65 64  mber) referenced
6c0f0 20 62 79 20 74 68 65 20 22 73 74 65 70 5f 6c 69   by the "step_li
6c100 73 74 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 68  st" member of th
6c110 65 20 0a 20 2a 20 61 73 73 6f 63 69 61 74 65 64  e . * associated
6c120 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20   struct Trigger 
6c130 69 6e 73 74 61 6e 63 65 2e 20 54 68 65 20 66 69  instance. The fi
6c140 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74  rst element of t
6c150 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69  he linked list i
6c160 73 0a 20 2a 20 74 68 65 20 66 69 72 73 74 20 73  s. * the first s
6c170 74 65 70 20 6f 66 20 74 68 65 20 74 72 69 67 67  tep of the trigg
6c180 65 72 2d 70 72 6f 67 72 61 6d 2e 0a 20 2a 20 0a  er-program.. * .
6c190 20 2a 20 54 68 65 20 22 6f 70 22 20 6d 65 6d 62   * The "op" memb
6c1a0 65 72 20 69 6e 64 69 63 61 74 65 73 20 77 68 65  er indicates whe
6c1b0 74 68 65 72 20 74 68 69 73 20 69 73 20 61 20 22  ther this is a "
6c1c0 44 45 4c 45 54 45 22 2c 20 22 49 4e 53 45 52 54  DELETE", "INSERT
6c1d0 22 2c 20 22 55 50 44 41 54 45 22 20 6f 72 0a 20  ", "UPDATE" or. 
6c1e0 2a 20 22 53 45 4c 45 43 54 22 20 73 74 61 74 65  * "SELECT" state
6c1f0 6d 65 6e 74 2e 20 54 68 65 20 6d 65 61 6e 69 6e  ment. The meanin
6c200 67 73 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20  gs of the other 
6c210 6d 65 6d 62 65 72 73 20 69 73 20 64 65 74 65 72  members is deter
6c220 6d 69 6e 65 64 20 62 79 20 74 68 65 20 0a 20 2a  mined by the . *
6c230 20 76 61 6c 75 65 20 6f 66 20 22 6f 70 22 20 61   value of "op" a
6c240 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 2a 0a 20 2a  s follows:. *. *
6c250 20 28 6f 70 20 3d 3d 20 54 4b 5f 49 4e 53 45 52   (op == TK_INSER
6c260 54 29 0a 20 2a 20 6f 72 63 6f 6e 66 20 20 20 20  T). * orconf    
6c270 2d 3e 20 73 74 6f 72 65 73 20 74 68 65 20 4f 4e  -> stores the ON
6c280 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69   CONFLICT algori
6c290 74 68 6d 0a 20 2a 20 70 53 65 6c 65 63 74 20 20  thm. * pSelect  
6c2a0 20 2d 3e 20 49 66 20 74 68 69 73 20 69 73 20 61   -> If this is a
6c2b0 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e  n INSERT INTO ..
6c2c0 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 73 74 61  . SELECT ... sta
6c2d0 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a 20 2a 20  tement, then. * 
6c2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 69               thi
6c2f0 73 20 73 74 6f 72 65 73 20 61 20 70 6f 69 6e 74  s stores a point
6c300 65 72 20 74 6f 20 74 68 65 20 53 45 4c 45 43 54  er to the SELECT
6c310 20 73 74 61 74 65 6d 65 6e 74 2e 20 4f 74 68 65   statement. Othe
6c320 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 74  rwise NULL.. * t
6c330 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f  arget    -> A to
6c340 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20  ken holding the 
6c350 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 74  quoted name of t
6c360 68 65 20 74 61 62 6c 65 20 74 6f 20 69 6e 73 65  he table to inse
6c370 72 74 20 69 6e 74 6f 2e 0a 20 2a 20 70 45 78 70  rt into.. * pExp
6c380 72 4c 69 73 74 20 2d 3e 20 49 66 20 74 68 69 73  rList -> If this
6c390 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e   is an INSERT IN
6c3a0 54 4f 20 2e 2e 2e 20 56 41 4c 55 45 53 20 2e 2e  TO ... VALUES ..
6c3b0 2e 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65  . statement, the
6c3c0 6e 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20  n. *            
6c3d0 20 20 74 68 69 73 20 73 74 6f 72 65 73 20 76 61    this stores va
6c3e0 6c 75 65 73 20 74 6f 20 62 65 20 69 6e 73 65 72  lues to be inser
6c3f0 74 65 64 2e 20 4f 74 68 65 72 77 69 73 65 20 4e  ted. Otherwise N
6c400 55 4c 4c 2e 0a 20 2a 20 70 49 64 4c 69 73 74 20  ULL.. * pIdList 
6c410 20 20 2d 3e 20 49 66 20 74 68 69 73 20 69 73 20    -> If this is 
6c420 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e  an INSERT INTO .
6c430 2e 2e 20 28 3c 63 6f 6c 75 6d 6e 2d 6e 61 6d 65  .. (<column-name
6c440 73 3e 29 20 56 41 4c 55 45 53 20 2e 2e 2e 20 0a  s>) VALUES ... .
6c450 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   *              
6c460 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20  statement, then 
6c470 74 68 69 73 20 73 74 6f 72 65 73 20 74 68 65 20  this stores the 
6c480 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 20 74 6f 20  column-names to 
6c490 62 65 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20  be. *           
6c4a0 20 20 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f     inserted into
6c4b0 2e 0a 20 2a 0a 20 2a 20 28 6f 70 20 3d 3d 20 54  .. *. * (op == T
6c4c0 4b 5f 44 45 4c 45 54 45 29 0a 20 2a 20 74 61 72  K_DELETE). * tar
6c4d0 67 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65  get    -> A toke
6c4e0 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 71 75  n holding the qu
6c4f0 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65  oted name of the
6c500 20 74 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65   table to delete
6c510 20 66 72 6f 6d 2e 0a 20 2a 20 70 57 68 65 72 65   from.. * pWhere
6c520 20 20 20 20 2d 3e 20 54 68 65 20 57 48 45 52 45      -> The WHERE
6c530 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 44   clause of the D
6c540 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20  ELETE statement 
6c550 69 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66  if one is specif
6c560 69 65 64 2e 0a 20 2a 20 20 20 20 20 20 20 20 20  ied.. *         
6c570 20 20 20 20 20 4f 74 68 65 72 77 69 73 65 20 4e       Otherwise N
6c580 55 4c 4c 2e 0a 20 2a 20 0a 20 2a 20 28 6f 70 20  ULL.. * . * (op 
6c590 3d 3d 20 54 4b 5f 55 50 44 41 54 45 29 0a 20 2a  == TK_UPDATE). *
6c5a0 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20   target    -> A 
6c5b0 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68  token holding th
6c5c0 65 20 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66  e quoted name of
6c5d0 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 75 70   the table to up
6c5e0 64 61 74 65 20 72 6f 77 73 20 6f 66 2e 0a 20 2a  date rows of.. *
6c5f0 20 70 57 68 65 72 65 20 20 20 20 2d 3e 20 54 68   pWhere    -> Th
6c600 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f  e WHERE clause o
6c610 66 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61  f the UPDATE sta
6c620 74 65 6d 65 6e 74 20 69 66 20 6f 6e 65 20 69 73  tement if one is
6c630 20 73 70 65 63 69 66 69 65 64 2e 0a 20 2a 20 20   specified.. *  
6c640 20 20 20 20 20 20 20 20 20 20 20 20 4f 74 68 65              Othe
6c650 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70  rwise NULL.. * p
6c660 45 78 70 72 4c 69 73 74 20 2d 3e 20 41 20 6c 69  ExprList -> A li
6c670 73 74 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  st of the column
6c680 73 20 74 6f 20 75 70 64 61 74 65 20 61 6e 64 20  s to update and 
6c690 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  the expressions 
6c6a0 74 6f 20 75 70 64 61 74 65 0a 20 2a 20 20 20 20  to update. *    
6c6b0 20 20 20 20 20 20 20 20 20 20 74 68 65 6d 20 74            them t
6c6c0 6f 2e 20 53 65 65 20 73 71 6c 69 74 65 33 55 70  o. See sqlite3Up
6c6d0 64 61 74 65 28 29 20 64 6f 63 75 6d 65 6e 74 61  date() documenta
6c6e0 74 69 6f 6e 20 6f 66 20 22 70 43 68 61 6e 67 65  tion of "pChange
6c6f0 73 22 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20  s". *           
6c700 20 20 20 61 72 67 75 6d 65 6e 74 2e 0a 20 2a 20     argument.. * 
6c710 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67  . */.struct Trig
6c720 67 65 72 53 74 65 70 20 7b 0a 20 20 75 38 20 6f  gerStep {.  u8 o
6c730 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
6c740 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45   /* One of TK_DE
6c750 4c 45 54 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c  LETE, TK_UPDATE,
6c760 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 53   TK_INSERT, TK_S
6c770 45 4c 45 43 54 20 2a 2f 0a 20 20 75 38 20 6f 72  ELECT */.  u8 or
6c780 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 20 20 20  conf;           
6c790 2f 2a 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 65  /* OE_Rollback e
6c7a0 74 63 2e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  tc. */.  Trigger
6c7b0 20 2a 70 54 72 69 67 3b 20 20 20 20 20 20 2f 2a   *pTrig;      /*
6c7c0 20 54 68 65 20 74 72 69 67 67 65 72 20 74 68 61   The trigger tha
6c7d0 74 20 74 68 69 73 20 73 74 65 70 20 69 73 20 61  t this step is a
6c7e0 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 20 53 65   part of */.  Se
6c7f0 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20  lect *pSelect;  
6c800 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 73 74 61     /* SELECT sta
6c810 74 6d 65 6e 74 20 6f 72 20 52 48 53 20 6f 66 20  tment or RHS of 
6c820 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 20 53  INSERT INTO .. S
6c830 45 4c 45 43 54 20 2e 2e 2e 20 2a 2f 0a 20 20 54  ELECT ... */.  T
6c840 6f 6b 65 6e 20 74 61 72 67 65 74 3b 20 20 20 20  oken target;    
6c850 20 20 20 20 2f 2a 20 54 61 72 67 65 74 20 74 61      /* Target ta
6c860 62 6c 65 20 66 6f 72 20 44 45 4c 45 54 45 2c 20  ble for DELETE, 
6c870 55 50 44 41 54 45 2c 20 49 4e 53 45 52 54 20 2a  UPDATE, INSERT *
6c880 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65  /.  Expr *pWhere
6c890 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
6c8a0 57 48 45 52 45 20 63 6c 61 75 73 65 20 66 6f 72  WHERE clause for
6c8b0 20 44 45 4c 45 54 45 20 6f 72 20 55 50 44 41 54   DELETE or UPDAT
6c8c0 45 20 73 74 65 70 73 20 2a 2f 0a 20 20 45 78 70  E steps */.  Exp
6c8d0 72 4c 69 73 74 20 2a 70 45 78 70 72 4c 69 73 74  rList *pExprList
6c8e0 3b 20 2f 2a 20 53 45 54 20 63 6c 61 75 73 65 20  ; /* SET clause 
6c8f0 66 6f 72 20 55 50 44 41 54 45 2e 20 20 56 41 4c  for UPDATE.  VAL
6c900 55 45 53 20 63 6c 61 75 73 65 20 66 6f 72 20 49  UES clause for I
6c910 4e 53 45 52 54 20 2a 2f 0a 20 20 49 64 4c 69 73  NSERT */.  IdLis
6c920 74 20 2a 70 49 64 4c 69 73 74 3b 20 20 20 20 20  t *pIdList;     
6c930 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  /* Column names 
6c940 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20  for INSERT */.  
6c950 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 4e 65  TriggerStep *pNe
6c960 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20  xt;  /* Next in 
6c970 74 68 65 20 6c 69 6e 6b 2d 6c 69 73 74 20 2a 2f  the link-list */
6c980 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  .  TriggerStep *
6c990 70 4c 61 73 74 3b 20 20 2f 2a 20 4c 61 73 74 20  pLast;  /* Last 
6c9a0 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69 6e 6b 2d  element in link-
6c9b0 6c 69 73 74 2e 20 56 61 6c 69 64 20 66 6f 72 20  list. Valid for 
6c9c0 31 73 74 20 65 6c 65 6d 20 6f 6e 6c 79 20 2a 2f  1st elem only */
6c9d0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  .};../*.** The f
6c9e0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
6c9f0 72 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f  re contains info
6ca00 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20  rmation used by 
6ca10 74 68 65 20 73 71 6c 69 74 65 46 69 78 2e 2e 2e  the sqliteFix...
6ca20 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61 73 20  .** routines as 
6ca30 74 68 65 79 20 77 61 6c 6b 20 74 68 65 20 70 61  they walk the pa
6ca40 72 73 65 20 74 72 65 65 20 74 6f 20 6d 61 6b 65  rse tree to make
6ca50 20 64 61 74 61 62 61 73 65 20 72 65 66 65 72 65   database refere
6ca60 6e 63 65 73 0a 2a 2a 20 65 78 70 6c 69 63 69 74  nces.** explicit
6ca70 2e 20 20 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  .  .*/.typedef s
6ca80 74 72 75 63 74 20 44 62 46 69 78 65 72 20 44 62  truct DbFixer Db
6ca90 46 69 78 65 72 3b 0a 73 74 72 75 63 74 20 44 62  Fixer;.struct Db
6caa0 46 69 78 65 72 20 7b 0a 20 20 50 61 72 73 65 20  Fixer {.  Parse 
6cab0 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 2f 2a  *pParse;      /*
6cac0 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e   The parsing con
6cad0 74 65 78 74 2e 20 20 45 72 72 6f 72 20 6d 65 73  text.  Error mes
6cae0 73 61 67 65 73 20 77 72 69 74 74 65 6e 20 68 65  sages written he
6caf0 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  re */.  const ch
6cb00 61 72 20 2a 7a 44 62 3b 20 20 20 20 2f 2a 20 4d  ar *zDb;    /* M
6cb10 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 6f 62 6a  ake sure all obj
6cb20 65 63 74 73 20 61 72 65 20 63 6f 6e 74 61 69 6e  ects are contain
6cb30 65 64 20 69 6e 20 74 68 69 73 20 64 61 74 61 62  ed in this datab
6cb40 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ase */.  const c
6cb50 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 2f 2a 20  har *zType;  /* 
6cb60 54 79 70 65 20 6f 66 20 74 68 65 20 63 6f 6e 74  Type of the cont
6cb70 61 69 6e 65 72 20 2d 20 75 73 65 64 20 66 6f 72  ainer - used for
6cb80 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   error messages 
6cb90 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e  */.  const Token
6cba0 20 2a 70 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65   *pName; /* Name
6cbb0 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 6e 65   of the containe
6cbc0 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65 72 72  r - used for err
6cbd0 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 7d  or messages */.}
6cbe0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65  ;../*.** An obje
6cbf0 63 74 65 64 20 75 73 65 64 20 74 6f 20 61 63 63  cted used to acc
6cc00 75 6d 75 6c 61 74 65 20 74 68 65 20 74 65 78 74  umulate the text
6cc10 20 6f 66 20 61 20 73 74 72 69 6e 67 20 77 68 65   of a string whe
6cc20 72 65 20 77 65 0a 2a 2a 20 64 6f 20 6e 6f 74 20  re we.** do not 
6cc30 6e 65 63 65 73 73 61 72 69 6c 79 20 6b 6e 6f 77  necessarily know
6cc40 20 68 6f 77 20 62 69 67 20 74 68 65 20 73 74 72   how big the str
6cc50 69 6e 67 20 77 69 6c 6c 20 62 65 20 69 6e 20 74  ing will be in t
6cc60 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 72 75 63  he end..*/.struc
6cc70 74 20 53 74 72 41 63 63 75 6d 20 7b 0a 20 20 73  t StrAccum {.  s
6cc80 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
6cc90 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20      /* Optional 
6cca0 64 61 74 61 62 61 73 65 20 66 6f 72 20 6c 6f 6f  database for loo
6ccb0 6b 61 73 69 64 65 2e 20 20 43 61 6e 20 62 65 20  kaside.  Can be 
6ccc0 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68 61 72 20 2a  NULL */.  char *
6ccd0 7a 42 61 73 65 3b 20 20 20 20 20 20 20 20 20 2f  zBase;         /
6cce0 2a 20 41 20 62 61 73 65 20 61 6c 6c 6f 63 61 74  * A base allocat
6ccf0 69 6f 6e 2e 20 20 4e 6f 74 20 66 72 6f 6d 20 6d  ion.  Not from m
6cd00 61 6c 6c 6f 63 2e 20 2a 2f 0a 20 20 63 68 61 72  alloc. */.  char
6cd10 20 2a 7a 54 65 78 74 3b 20 20 20 20 20 20 20 20   *zText;        
6cd20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e 67 20 63   /* The string c
6cd30 6f 6c 6c 65 63 74 65 64 20 73 6f 20 66 61 72 20  ollected so far 
6cd40 2a 2f 0a 20 20 69 6e 74 20 20 6e 43 68 61 72 3b  */.  int  nChar;
6cd50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
6cd60 67 74 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e  gth of the strin
6cd70 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e  g so far */.  in
6cd80 74 20 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20  t  nAlloc;      
6cd90 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20     /* Amount of 
6cda0 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20  space allocated 
6cdb0 69 6e 20 7a 54 65 78 74 20 2a 2f 0a 20 20 69 6e  in zText */.  in
6cdc0 74 20 20 6d 78 41 6c 6c 6f 63 3b 20 20 20 20 20  t  mxAlloc;     
6cdd0 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c     /* Maximum al
6cde0 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 6c 65 6e  lowed string len
6cdf0 67 74 68 20 2a 2f 0a 20 20 75 38 20 20 20 6d 61  gth */.  u8   ma
6ce00 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20 2f 2a  llocFailed;   /*
6ce10 20 42 65 63 6f 6d 65 73 20 74 72 75 65 20 69 66   Becomes true if
6ce20 20 61 6e 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   any memory allo
6ce30 63 61 74 69 6f 6e 20 66 61 69 6c 73 20 2a 2f 0a  cation fails */.
6ce40 20 20 75 38 20 20 20 75 73 65 4d 61 6c 6c 6f 63    u8   useMalloc
6ce50 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  ;      /* True i
6ce60 66 20 7a 54 65 78 74 20 69 73 20 65 6e 6c 61 72  f zText is enlar
6ce70 67 65 61 62 6c 65 20 75 73 69 6e 67 20 72 65 61  geable using rea
6ce80 6c 6c 6f 63 20 2a 2f 0a 20 20 75 38 20 20 20 74  lloc */.  u8   t
6ce90 6f 6f 42 69 67 3b 20 20 20 20 20 20 20 20 20 2f  ooBig;         /
6cea0 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65 20 69  * Becomes true i
6ceb0 66 20 73 74 72 69 6e 67 20 73 69 7a 65 20 65 78  f string size ex
6cec0 63 65 65 64 73 20 6c 69 6d 69 74 73 20 2a 2f 0a  ceeds limits */.
6ced0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 70 6f 69 6e  };../*.** A poin
6cee0 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72 75  ter to this stru
6cef0 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f  cture is used to
6cf00 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20 69 6e 66   communicate inf
6cf10 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 72 6f 6d  ormation.** from
6cf20 20 73 71 6c 69 74 65 33 49 6e 69 74 20 61 6e 64   sqlite3Init and
6cf30 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 20   OP_ParseSchema 
6cf40 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  into the sqlite3
6cf50 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2e 0a 2a 2f  InitCallback..*/
6cf60 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
6cf70 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
6cf80 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
6cf90 61 74 61 62 61 73 65 20 62 65 69 6e 67 20 69 6e  atabase being in
6cfa0 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69  itialized */.  i
6cfb0 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
6cfc0 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d 61 69 6e     /* 0 for main
6cfd0 20 64 61 74 61 62 61 73 65 2e 20 20 31 20 66 6f   database.  1 fo
6cfe0 72 20 54 45 4d 50 2c 20 32 2e 2e 20 66 6f 72 20  r TEMP, 2.. for 
6cff0 41 54 54 41 43 48 65 64 20 2a 2f 0a 20 20 63 68  ATTACHed */.  ch
6d000 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 3b 20 20  ar **pzErrMsg;  
6d010 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 61    /* Error messa
6d020 67 65 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a  ge stored here *
6d030 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
6d040 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c          /* Resul
6d050 74 20 63 6f 64 65 20 73 74 6f 72 65 64 20 68 65  t code stored he
6d060 72 65 20 2a 2f 0a 7d 20 49 6e 69 74 44 61 74 61  re */.} InitData
6d070 3b 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75  ;../*.** Structu
6d080 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 67 6c  re containing gl
6d090 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61 74 69  obal configurati
6d0a0 6f 6e 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  on data for the 
6d0b0 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 0a  SQLite library..
6d0c0 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63  **.** This struc
6d0d0 74 75 72 65 20 61 6c 73 6f 20 63 6f 6e 74 61 69  ture also contai
6d0e0 6e 73 20 73 6f 6d 65 20 73 74 61 74 65 20 69 6e  ns some state in
6d0f0 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  formation..*/.st
6d100 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66  ruct Sqlite3Conf
6d110 69 67 20 7b 0a 20 20 69 6e 74 20 62 4d 65 6d 73  ig {.  int bMems
6d120 74 61 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  tat;            
6d130 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
6d140 20 74 6f 20 65 6e 61 62 6c 65 20 6d 65 6d 6f 72   to enable memor
6d150 79 20 73 74 61 74 75 73 20 2a 2f 0a 20 20 69 6e  y status */.  in
6d160 74 20 62 43 6f 72 65 4d 75 74 65 78 3b 20 20 20  t bCoreMutex;   
6d170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d180 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 62 6c  /* True to enabl
6d190 65 20 63 6f 72 65 20 6d 75 74 65 78 69 6e 67 20  e core mutexing 
6d1a0 2a 2f 0a 20 20 69 6e 74 20 62 46 75 6c 6c 4d 75  */.  int bFullMu
6d1b0 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  tex;            
6d1c0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
6d1d0 6f 20 65 6e 61 62 6c 65 20 66 75 6c 6c 20 6d 75  o enable full mu
6d1e0 74 65 78 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  texing */.  int 
6d1f0 6d 78 53 74 72 6c 65 6e 3b 20 20 20 20 20 20 20  mxStrlen;       
6d200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6d210 20 4d 61 78 69 6d 75 6d 20 73 74 72 69 6e 67 20   Maximum string 
6d220 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 69 6e 74 20  length */.  int 
6d230 73 7a 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20  szLookaside;    
6d240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6d250 20 44 65 66 61 75 6c 74 20 6c 6f 6f 6b 61 73 69   Default lookasi
6d260 64 65 20 62 75 66 66 65 72 20 73 69 7a 65 20 2a  de buffer size *
6d270 2f 0a 20 20 69 6e 74 20 6e 4c 6f 6f 6b 61 73 69  /.  int nLookasi
6d280 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  de;             
6d290 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74        /* Default
6d2a0 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65   lookaside buffe
6d2b0 72 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 73 71 6c  r count */.  sql
6d2c0 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
6d2d0 20 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   m;            /
6d2e0 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f  * Low-level memo
6d2f0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e  ry allocation in
6d300 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 73 71 6c  terface */.  sql
6d310 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
6d320 64 73 20 6d 75 74 65 78 3b 20 20 20 20 20 20 2f  ds mutex;      /
6d330 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d 75 74 65  * Low-level mute
6d340 78 20 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20  x interface */. 
6d350 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f   sqlite3_pcache_
6d360 6d 65 74 68 6f 64 73 20 70 63 61 63 68 65 3b 20  methods pcache; 
6d370 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20     /* Low-level 
6d380 70 61 67 65 2d 63 61 63 68 65 20 69 6e 74 65 72  page-cache inter
6d390 66 61 63 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  face */.  void *
6d3a0 70 48 65 61 70 3b 20 20 20 20 20 20 20 20 20 20  pHeap;          
6d3b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
6d3c0 65 61 70 20 73 74 6f 72 61 67 65 20 73 70 61 63  eap storage spac
6d3d0 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 65 61 70  e */.  int nHeap
6d3e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6d3f0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
6d400 20 6f 66 20 70 48 65 61 70 5b 5d 20 2a 2f 0a 20   of pHeap[] */. 
6d410 20 69 6e 74 20 6d 6e 52 65 71 2c 20 6d 78 52 65   int mnReq, mxRe
6d420 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  q;              
6d430 20 20 20 2f 2a 20 4d 69 6e 20 61 6e 64 20 6d 61     /* Min and ma
6d440 78 20 68 65 61 70 20 72 65 71 75 65 73 74 73 20  x heap requests 
6d450 73 69 7a 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20  sizes */.  void 
6d460 2a 70 53 63 72 61 74 63 68 3b 20 20 20 20 20 20  *pScratch;      
6d470 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6d480 53 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 2a  Scratch memory *
6d490 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63  /.  int szScratc
6d4a0 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
6d4b0 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
6d4c0 20 65 61 63 68 20 73 63 72 61 74 63 68 20 62 75   each scratch bu
6d4d0 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53  ffer */.  int nS
6d4e0 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20 20  cratch;         
6d4f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
6d500 75 6d 62 65 72 20 6f 66 20 73 63 72 61 74 63 68  umber of scratch
6d510 20 62 75 66 66 65 72 73 20 2a 2f 0a 20 20 76 6f   buffers */.  vo
6d520 69 64 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20  id *pPage;      
6d530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d540 2f 2a 20 50 61 67 65 20 63 61 63 68 65 20 6d 65  /* Page cache me
6d550 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  mory */.  int sz
6d560 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
6d570 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
6d580 69 7a 65 20 6f 66 20 65 61 63 68 20 70 61 67 65  ize of each page
6d590 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a 2f 0a 20   in pPage[] */. 
6d5a0 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20   int nPage;     
6d5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d5c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
6d5d0 70 61 67 65 73 20 69 6e 20 70 50 61 67 65 5b 5d  pages in pPage[]
6d5e0 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61 72 73   */.  int mxPars
6d5f0 65 72 53 74 61 63 6b 3b 20 20 20 20 20 20 20 20  erStack;        
6d600 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 78 69 6d          /* maxim
6d610 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65 20  um depth of the 
6d620 70 61 72 73 65 72 20 73 74 61 63 6b 20 2a 2f 0a  parser stack */.
6d630 20 20 69 6e 74 20 73 68 61 72 65 64 43 61 63 68    int sharedCach
6d640 65 45 6e 61 62 6c 65 64 3b 20 20 20 20 20 20 20  eEnabled;       
6d650 20 20 20 20 2f 2a 20 74 72 75 65 20 69 66 20 73      /* true if s
6d660 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65  hared-cache mode
6d670 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 2f 2a   enabled */.  /*
6d680 20 54 68 65 20 61 62 6f 76 65 20 6d 69 67 68 74   The above might
6d690 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   be initialized 
6d6a0 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 54 68  to non-zero.  Th
6d6b0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 65 64  e following need
6d6c0 20 74 6f 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20   to always.  ** 
6d6d0 69 6e 69 74 69 61 6c 6c 79 20 62 65 20 7a 65 72  initially be zer
6d6e0 6f 2c 20 68 6f 77 65 76 65 72 2e 20 2a 2f 0a 20  o, however. */. 
6d6f0 20 69 6e 74 20 69 73 49 6e 69 74 3b 20 20 20 20   int isInit;    
6d700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d710 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72     /* True after
6d720 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
6d730 68 61 73 20 66 69 6e 69 73 68 65 64 20 2a 2f 0a  has finished */.
6d740 20 20 69 6e 74 20 69 6e 50 72 6f 67 72 65 73 73    int inProgress
6d750 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
6d760 20 20 20 20 2f 2a 20 54 72 75 65 20 77 68 69 6c      /* True whil
6d770 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  e initialization
6d780 20 69 6e 20 70 72 6f 67 72 65 73 73 20 2a 2f 0a   in progress */.
6d790 20 20 69 6e 74 20 69 73 4d 75 74 65 78 49 6e 69    int isMutexIni
6d7a0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
6d7b0 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65      /* True afte
6d7c0 72 20 6d 75 74 65 78 65 73 20 61 72 65 20 69 6e  r mutexes are in
6d7d0 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69  itialized */.  i
6d7e0 6e 74 20 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 3b  nt isMallocInit;
6d7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d800 20 2f 2a 20 54 72 75 65 20 61 66 74 65 72 20 6d   /* True after m
6d810 61 6c 6c 6f 63 20 69 73 20 69 6e 69 74 69 61 6c  alloc is initial
6d820 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 73  ized */.  int is
6d830 50 43 61 63 68 65 49 6e 69 74 3b 20 20 20 20 20  PCacheInit;     
6d840 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
6d850 72 75 65 20 61 66 74 65 72 20 6d 61 6c 6c 6f 63  rue after malloc
6d860 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   is initialized 
6d870 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  */.  sqlite3_mut
6d880 65 78 20 2a 70 49 6e 69 74 4d 75 74 65 78 3b 20  ex *pInitMutex; 
6d890 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20         /* Mutex 
6d8a0 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  used by sqlite3_
6d8b0 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 2a 2f 0a  initialize() */.
6d8c0 20 20 69 6e 74 20 6e 52 65 66 49 6e 69 74 4d 75    int nRefInitMu
6d8d0 74 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  tex;            
6d8e0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
6d8f0 20 75 73 65 72 73 20 6f 66 20 70 49 6e 69 74 4d   users of pInitM
6d900 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  utex */.};../*.*
6d910 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65  * Context pointe
6d920 72 20 70 61 73 73 65 64 20 64 6f 77 6e 20 74 68  r passed down th
6d930 72 6f 75 67 68 20 74 68 65 20 74 72 65 65 2d 77  rough the tree-w
6d940 61 6c 6b 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57  alk..*/.struct W
6d950 61 6c 6b 65 72 20 7b 0a 20 20 69 6e 74 20 28 2a  alker {.  int (*
6d960 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 29 28 57  xExprCallback)(W
6d970 61 6c 6b 65 72 2a 2c 20 45 78 70 72 2a 29 3b 20  alker*, Expr*); 
6d980 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20      /* Callback 
6d990 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 73 20  for expressions 
6d9a0 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 65 6c 65  */.  int (*xSele
6d9b0 63 74 43 61 6c 6c 62 61 63 6b 29 28 57 61 6c 6b  ctCallback)(Walk
6d9c0 65 72 2a 2c 53 65 6c 65 63 74 2a 29 3b 20 20 2f  er*,Select*);  /
6d9d0 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f 72 20 53  * Callback for S
6d9e0 45 4c 45 43 54 73 20 2a 2f 0a 20 20 50 61 72 73  ELECTs */.  Pars
6d9f0 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20  e *pParse;      
6da00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6da10 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20        /* Parser 
6da20 63 6f 6e 74 65 78 74 2e 20 20 2a 2f 0a 20 20 75  context.  */.  u
6da30 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20  nion {          
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 2f 2a 20 45 78 74 72           /* Extr
6da60 61 20 64 61 74 61 20 66 6f 72 20 63 61 6c 6c 62  a data for callb
6da70 61 63 6b 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43  ack */.    NameC
6da80 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 20 20 20 20  ontext *pNC;    
6da90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6daa0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 69 6e 67 20        /* Naming 
6dab0 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20 69  context */.    i
6dac0 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
6dad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dae0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74            /* Int
6daf0 65 67 65 72 20 76 61 6c 75 65 20 2a 2f 0a 20 20  eger value */.  
6db00 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46 6f 72 77  } u;.};../* Forw
6db10 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73  ard declarations
6db20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
6db30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61  TE int sqlite3Wa
6db40 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20  lkExpr(Walker*, 
6db50 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
6db60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6db70 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 57  e3WalkExprList(W
6db80 61 6c 6b 65 72 2a 2c 20 45 78 70 72 4c 69 73 74  alker*, ExprList
6db90 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6dba0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61  TE int sqlite3Wa
6dbb0 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 65 72 2a  lkSelect(Walker*
6dbc0 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49  , Select*);.SQLI
6dbd0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6dbe0 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74  qlite3WalkSelect
6dbf0 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 53 65  Expr(Walker*, Se
6dc00 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  lect*);.SQLITE_P
6dc10 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6dc20 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d  e3WalkSelectFrom
6dc30 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63 74  (Walker*, Select
6dc40 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  *);../*.** Retur
6dc50 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 65 20  n code from the 
6dc60 70 61 72 73 65 2d 74 72 65 65 20 77 61 6c 6b 69  parse-tree walki
6dc70 6e 67 20 70 72 69 6d 69 74 69 76 65 73 20 61 6e  ng primitives an
6dc80 64 20 74 68 65 69 72 0a 2a 2a 20 63 61 6c 6c 62  d their.** callb
6dc90 61 63 6b 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  acks..*/.#define
6dca0 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 20 20   WRC_Continue   
6dcb0 20 30 20 20 20 2f 2a 20 43 6f 6e 74 69 6e 75 65   0   /* Continue
6dcc0 20 64 6f 77 6e 20 69 6e 74 6f 20 63 68 69 6c 64   down into child
6dcd0 72 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  ren */.#define W
6dce0 52 43 5f 50 72 75 6e 65 20 20 20 20 20 20 20 31  RC_Prune       1
6dcf0 20 20 20 2f 2a 20 4f 6d 69 74 20 63 68 69 6c 64     /* Omit child
6dd00 72 65 6e 20 62 75 74 20 63 6f 6e 74 69 6e 75 65  ren but continue
6dd10 20 77 61 6c 6b 69 6e 67 20 73 69 62 6c 69 6e 67   walking sibling
6dd20 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 52 43  s */.#define WRC
6dd30 5f 41 62 6f 72 74 20 20 20 20 20 20 20 32 20 20  _Abort       2  
6dd40 20 2f 2a 20 41 62 61 6e 64 6f 6e 20 74 68 65 20   /* Abandon the 
6dd50 74 72 65 65 20 77 61 6c 6b 20 2a 2f 0a 0a 2f 2a  tree walk */../*
6dd60 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20 7a 49 6e  .** Assuming zIn
6dd70 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66   points to the f
6dd80 69 72 73 74 20 62 79 74 65 20 6f 66 20 61 20 55  irst byte of a U
6dd90 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 2c 0a  TF-8 character,.
6dda0 2a 2a 20 61 64 76 61 6e 63 65 20 7a 49 6e 20 74  ** advance zIn t
6ddb0 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66  o point to the f
6ddc0 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65  irst byte of the
6ddd0 20 6e 65 78 74 20 55 54 46 2d 38 20 63 68 61 72   next UTF-8 char
6dde0 61 63 74 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e  acter..*/.#defin
6ddf0 65 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54  e SQLITE_SKIP_UT
6de00 46 38 28 7a 49 6e 29 20 7b 20 20 20 20 20 20 20  F8(zIn) {       
6de10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6de20 20 5c 0a 20 20 69 66 28 20 28 2a 28 7a 49 6e 2b   \.  if( (*(zIn+
6de30 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20 20 20 20  +))>=0xc0 ){    
6de40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6de50 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
6de60 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20 26 20 30  while( (*zIn & 0
6de70 78 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 7a 49  xc0)==0x80 ){ zI
6de80 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20 20 20 20  n++; }          
6de90 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20     \.  }        
6dea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6deb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dec0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a              \.}.
6ded0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54  ./*.** The SQLIT
6dee0 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20 6d  E_CORRUPT_BKPT m
6def0 61 63 72 6f 20 63 61 6e 20 62 65 20 65 69 74 68  acro can be eith
6df00 65 72 20 61 20 63 6f 6e 73 74 61 6e 74 20 28 66  er a constant (f
6df10 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e 0a 2a 2a  or production.**
6df20 20 62 75 69 6c 64 73 29 20 6f 72 20 61 20 66 75   builds) or a fu
6df30 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 28 66 6f 72  nction call (for
6df40 20 64 65 62 75 67 67 69 6e 67 29 2e 20 20 49 66   debugging).  If
6df50 20 69 74 20 69 73 20 61 20 66 75 6e 63 74 69 6f   it is a functio
6df60 6e 20 63 61 6c 6c 2c 0a 2a 2a 20 69 74 20 61 6c  n call,.** it al
6df70 6c 6f 77 73 20 74 68 65 20 6f 70 65 72 61 74 6f  lows the operato
6df80 72 20 74 6f 20 73 65 74 20 61 20 62 72 65 61 6b  r to set a break
6df90 70 6f 69 6e 74 20 61 74 20 74 68 65 20 73 70 6f  point at the spo
6dfa0 74 20 77 68 65 72 65 20 64 61 74 61 62 61 73 65  t where database
6dfb0 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20 69  .** corruption i
6dfc0 73 20 66 69 72 73 74 20 64 65 74 65 63 74 65 64  s first detected
6dfd0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
6dfe0 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f  TE_DEBUG.SQLITE_
6dff0 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
6e000 6c 69 74 65 33 43 6f 72 72 75 70 74 28 76 6f 69  lite3Corrupt(voi
6e010 64 29 3b 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  d);.# define SQL
6e020 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
6e030 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28   sqlite3Corrupt(
6e040 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ).#else.# define
6e050 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
6e060 42 4b 50 54 20 53 51 4c 49 54 45 5f 43 4f 52 52  BKPT SQLITE_CORR
6e070 55 50 54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  UPT.#endif../*.*
6e080 2a 20 54 68 65 20 63 74 79 70 65 2e 68 20 68 65  * The ctype.h he
6e090 61 64 65 72 20 69 73 20 6e 65 65 64 65 64 20 66  ader is needed f
6e0a0 6f 72 20 6e 6f 6e 2d 41 53 43 49 49 20 73 79 73  or non-ASCII sys
6e0b0 74 65 6d 73 2e 20 20 49 74 20 69 73 20 61 6c 73  tems.  It is als
6e0c0 6f 0a 2a 2a 20 6e 65 65 64 65 64 20 62 79 20 46  o.** needed by F
6e0d0 54 53 33 20 77 68 65 6e 20 46 54 53 33 20 69 73  TS3 when FTS3 is
6e0e0 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65   included in the
6e0f0 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a   amalgamation..*
6e100 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
6e110 51 4c 49 54 45 5f 41 53 43 49 49 29 20 7c 7c 20  QLITE_ASCII) || 
6e120 5c 0a 20 20 20 20 28 64 65 66 69 6e 65 64 28 53  \.    (defined(S
6e130 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
6e140 33 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51  3) && defined(SQ
6e150 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f  LITE_AMALGAMATIO
6e160 4e 29 29 0a 23 20 69 6e 63 6c 75 64 65 20 3c 63  N)).# include <c
6e170 74 79 70 65 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a  type.h>.#endif..
6e180 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
6e190 69 6e 67 20 6d 61 63 72 6f 73 20 6d 69 6d 69 63  ing macros mimic
6e1a0 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 6c 69   the standard li
6e1b0 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73 20  brary functions 
6e1c0 74 6f 75 70 70 65 72 28 29 2c 0a 2a 2a 20 69 73  toupper(),.** is
6e1d0 73 70 61 63 65 28 29 2c 20 69 73 61 6c 6e 75 6d  space(), isalnum
6e1e0 28 29 2c 20 69 73 64 69 67 69 74 28 29 20 61 6e  (), isdigit() an
6e1f0 64 20 69 73 78 64 69 67 69 74 28 29 2c 20 72 65  d isxdigit(), re
6e200 73 70 65 63 74 69 76 65 6c 79 2e 20 54 68 65 0a  spectively. The.
6e210 2a 2a 20 73 71 6c 69 74 65 20 76 65 72 73 69 6f  ** sqlite versio
6e220 6e 73 20 6f 6e 6c 79 20 77 6f 72 6b 20 66 6f 72  ns only work for
6e230 20 41 53 43 49 49 20 63 68 61 72 61 63 74 65 72   ASCII character
6e240 73 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66  s, regardless of
6e250 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 23 69 66 64   locale..*/.#ifd
6e260 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a  ef SQLITE_ASCII.
6e270 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
6e280 54 6f 75 70 70 65 72 28 78 29 20 20 28 28 78 29  Toupper(x)  ((x)
6e290 26 7e 28 73 71 6c 69 74 65 33 43 74 79 70 65 4d  &~(sqlite3CtypeM
6e2a0 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61  ap[(unsigned cha
6e2b0 72 29 28 78 29 5d 26 30 78 32 30 29 29 0a 23 20  r)(x)]&0x20)).# 
6e2c0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73  define sqlite3Is
6e2d0 73 70 61 63 65 28 78 29 20 20 20 28 73 71 6c 69  space(x)   (sqli
6e2e0 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73  te3CtypeMap[(uns
6e2f0 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26  igned char)(x)]&
6e300 30 78 30 31 29 0a 23 20 64 65 66 69 6e 65 20 73  0x01).# define s
6e310 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 78 29  qlite3Isalnum(x)
6e320 20 20 20 28 73 71 6c 69 74 65 33 43 74 79 70 65     (sqlite3Ctype
6e330 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68  Map[(unsigned ch
6e340 61 72 29 28 78 29 5d 26 30 78 30 36 29 0a 23 20  ar)(x)]&0x06).# 
6e350 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73  define sqlite3Is
6e360 61 6c 70 68 61 28 78 29 20 20 20 28 73 71 6c 69  alpha(x)   (sqli
6e370 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73  te3CtypeMap[(uns
6e380 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26  igned char)(x)]&
6e390 30 78 30 32 29 0a 23 20 64 65 66 69 6e 65 20 73  0x02).# define s
6e3a0 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 78 29  qlite3Isdigit(x)
6e3b0 20 20 20 28 73 71 6c 69 74 65 33 43 74 79 70 65     (sqlite3Ctype
6e3c0 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20 63 68  Map[(unsigned ch
6e3d0 61 72 29 28 78 29 5d 26 30 78 30 34 29 0a 23 20  ar)(x)]&0x04).# 
6e3e0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73  define sqlite3Is
6e3f0 78 64 69 67 69 74 28 78 29 20 20 28 73 71 6c 69  xdigit(x)  (sqli
6e400 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73  te3CtypeMap[(uns
6e410 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 26  igned char)(x)]&
6e420 30 78 30 38 29 0a 23 20 64 65 66 69 6e 65 20 73  0x08).# define s
6e430 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72 28 78 29  qlite3Tolower(x)
6e440 20 20 20 28 73 71 6c 69 74 65 33 55 70 70 65 72     (sqlite3Upper
6e450 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65  ToLower[(unsigne
6e460 64 20 63 68 61 72 29 28 78 29 5d 29 0a 23 65 6c  d char)(x)]).#el
6e470 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  se.# define sqli
6e480 74 65 33 54 6f 75 70 70 65 72 28 78 29 20 20 20  te3Toupper(x)   
6e490 74 6f 75 70 70 65 72 28 28 75 6e 73 69 67 6e 65  toupper((unsigne
6e4a0 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64 65  d char)(x)).# de
6e4b0 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73 73 70  fine sqlite3Issp
6e4c0 61 63 65 28 78 29 20 20 20 69 73 73 70 61 63 65  ace(x)   isspace
6e4d0 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29  ((unsigned char)
6e4e0 28 78 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71  (x)).# define sq
6e4f0 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 78 29 20  lite3Isalnum(x) 
6e500 20 20 69 73 61 6c 6e 75 6d 28 28 75 6e 73 69 67    isalnum((unsig
6e510 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20  ned char)(x)).# 
6e520 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49 73  define sqlite3Is
6e530 61 6c 70 68 61 28 78 29 20 20 20 69 73 61 6c 70  alpha(x)   isalp
6e540 68 61 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61  ha((unsigned cha
6e550 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65 20  r)(x)).# define 
6e560 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 78  sqlite3Isdigit(x
6e570 29 20 20 20 69 73 64 69 67 69 74 28 28 75 6e 73  )   isdigit((uns
6e580 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a  igned char)(x)).
6e590 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
6e5a0 49 73 78 64 69 67 69 74 28 78 29 20 20 69 73 78  Isxdigit(x)  isx
6e5b0 64 69 67 69 74 28 28 75 6e 73 69 67 6e 65 64 20  digit((unsigned 
6e5c0 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69  char)(x)).# defi
6e5d0 6e 65 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65  ne sqlite3Tolowe
6e5e0 72 28 78 29 20 20 20 74 6f 6c 6f 77 65 72 28 28  r(x)   tolower((
6e5f0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78  unsigned char)(x
6e600 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  )).#endif../*.**
6e610 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 69   Internal functi
6e620 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73 0a 2a 2f  on prototypes.*/
6e630 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6e640 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 49 43  int sqlite3StrIC
6e650 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  mp(const char *,
6e660 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a   const char *);.
6e670 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
6e680 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62  nt sqlite3IsNumb
6e690 65 72 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  er(const char*, 
6e6a0 69 6e 74 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54  int*, u8);.SQLIT
6e6b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6e6c0 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 63 6f  lite3Strlen30(co
6e6d0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 64 65 66  nst char*);.#def
6e6e0 69 6e 65 20 73 71 6c 69 74 65 33 53 74 72 4e 49  ine sqlite3StrNI
6e6f0 43 6d 70 20 73 71 6c 69 74 65 33 5f 73 74 72 6e  Cmp sqlite3_strn
6e700 69 63 6d 70 0a 0a 53 51 4c 49 54 45 5f 50 52 49  icmp..SQLITE_PRI
6e710 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6e720 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64 29  MallocInit(void)
6e730 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6e740 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 6c   void sqlite3Mal
6e750 6c 6f 63 45 6e 64 28 76 6f 69 64 29 3b 0a 53 51  locEnd(void);.SQ
6e760 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6e770 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  d *sqlite3Malloc
6e780 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  (int);.SQLITE_PR
6e790 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
6e7a0 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e  te3MallocZero(in
6e7b0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6e7c0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
6e7d0 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c  DbMallocZero(sql
6e7e0 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  ite3*, int);.SQL
6e7f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6e800 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f   *sqlite3DbMallo
6e810 63 52 61 77 28 73 71 6c 69 74 65 33 2a 2c 20 69  cRaw(sqlite3*, i
6e820 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6e830 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
6e840 33 44 62 53 74 72 44 75 70 28 73 71 6c 69 74 65  3DbStrDup(sqlite
6e850 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  3*,const char*);
6e860 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6e870 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53  char *sqlite3DbS
6e880 74 72 4e 44 75 70 28 73 71 6c 69 74 65 33 2a 2c  trNDup(sqlite3*,
6e890 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74  const char*, int
6e8a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6e8b0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 52  E void *sqlite3R
6e8c0 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e  ealloc(void*, in
6e8d0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6e8e0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
6e8f0 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28  DbReallocOrFree(
6e900 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20  sqlite3 *, void 
6e910 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
6e920 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
6e930 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 73  lite3DbRealloc(s
6e940 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a  qlite3 *, void *
6e950 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
6e960 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6e970 74 65 33 44 62 46 72 65 65 28 73 71 6c 69 74 65  te3DbFree(sqlite
6e980 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49  3*, void*);.SQLI
6e990 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6e9a0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65  qlite3MallocSize
6e9b0 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f  (void*);.SQLITE_
6e9c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6e9d0 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28  te3DbMallocSize(
6e9e0 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29  sqlite3*, void*)
6e9f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6ea00 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 53 63   void *sqlite3Sc
6ea10 72 61 74 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 29  ratchMalloc(int)
6ea20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6ea30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 72   void sqlite3Scr
6ea40 61 74 63 68 46 72 65 65 28 76 6f 69 64 2a 29 3b  atchFree(void*);
6ea50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ea60 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67  void *sqlite3Pag
6ea70 65 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51  eMalloc(int);.SQ
6ea80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6ea90 64 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65  d sqlite3PageFre
6eaa0 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45  e(void*);.SQLITE
6eab0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6eac0 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75  lite3MemSetDefau
6ead0 6c 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45  lt(void);.SQLITE
6eae0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6eaf0 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f  lite3BenignMallo
6eb00 63 48 6f 6f 6b 73 28 76 6f 69 64 20 28 2a 29 28  cHooks(void (*)(
6eb10 76 6f 69 64 29 2c 20 76 6f 69 64 20 28 2a 29 28  void), void (*)(
6eb20 76 6f 69 64 29 29 3b 0a 53 51 4c 49 54 45 5f 50  void));.SQLITE_P
6eb30 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6eb40 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 76 6f  e3MemoryAlarm(vo
6eb50 69 64 20 28 2a 29 28 76 6f 69 64 2a 2c 20 73 71  id (*)(void*, sq
6eb60 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 69 6e 74  lite3_int64, int
6eb70 29 2c 20 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65  ), void*, sqlite
6eb80 33 5f 69 6e 74 36 34 29 3b 0a 0a 2f 2a 0a 2a 2a  3_int64);../*.**
6eb90 20 4f 6e 20 73 79 73 74 65 6d 73 20 77 69 74 68   On systems with
6eba0 20 61 6d 70 6c 65 20 73 74 61 63 6b 20 73 70 61   ample stack spa
6ebb0 63 65 20 61 6e 64 20 74 68 61 74 20 73 75 70 70  ce and that supp
6ebc0 6f 72 74 20 61 6c 6c 6f 63 61 28 29 2c 20 6d 61  ort alloca(), ma
6ebd0 6b 65 0a 2a 2a 20 75 73 65 20 6f 66 20 61 6c 6c  ke.** use of all
6ebe0 6f 63 61 28 29 20 74 6f 20 6f 62 74 61 69 6e 20  oca() to obtain 
6ebf0 73 70 61 63 65 20 66 6f 72 20 6c 61 72 67 65 20  space for large 
6ec00 61 75 74 6f 6d 61 74 69 63 20 6f 62 6a 65 63 74  automatic object
6ec10 73 2e 20 20 42 79 20 64 65 66 61 75 6c 74 2c 0a  s.  By default,.
6ec20 2a 2a 20 6f 62 74 61 69 6e 20 73 70 61 63 65 20  ** obtain space 
6ec30 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a  from malloc()..*
6ec40 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f 63 61 28  *.** The alloca(
6ec50 29 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20  ) routine never 
6ec60 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 20 20 54  returns NULL.  T
6ec70 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65 20 63  his will cause c
6ec80 6f 64 65 20 70 61 74 68 73 0a 2a 2a 20 74 68 61  ode paths.** tha
6ec90 74 20 64 65 61 6c 20 77 69 74 68 20 73 71 6c 69  t deal with sqli
6eca0 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 28 29 20  te3StackAlloc() 
6ecb0 66 61 69 6c 75 72 65 73 20 74 6f 20 62 65 20 75  failures to be u
6ecc0 6e 72 65 61 63 68 61 62 6c 65 2e 0a 2a 2f 0a 23  nreachable..*/.#
6ecd0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 55 53 45  ifdef SQLITE_USE
6ece0 5f 41 4c 4c 4f 43 41 0a 23 20 64 65 66 69 6e 65  _ALLOCA.# define
6ecf0 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c   sqlite3StackAll
6ed00 6f 63 52 61 77 28 44 2c 4e 29 20 20 20 61 6c 6c  ocRaw(D,N)   all
6ed10 6f 63 61 28 4e 29 0a 23 20 64 65 66 69 6e 65 20  oca(N).# define 
6ed20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f  sqlite3StackAllo
6ed30 63 5a 65 72 6f 28 44 2c 4e 29 20 20 6d 65 6d 73  cZero(D,N)  mems
6ed40 65 74 28 61 6c 6c 6f 63 61 28 4e 29 2c 20 30 2c  et(alloca(N), 0,
6ed50 20 4e 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c   N).# define sql
6ed60 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 44 2c  ite3StackFree(D,
6ed70 50 29 20 20 20 20 20 20 20 0a 23 65 6c 73 65 0a  P)       .#else.
6ed80 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
6ed90 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77 28 44 2c  StackAllocRaw(D,
6eda0 4e 29 20 20 20 73 71 6c 69 74 65 33 44 62 4d 61  N)   sqlite3DbMa
6edb0 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 0a 23 20 64  llocRaw(D,N).# d
6edc0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61  efine sqlite3Sta
6edd0 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29  ckAllocZero(D,N)
6ede0 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f    sqlite3DbMallo
6edf0 63 5a 65 72 6f 28 44 2c 4e 29 0a 23 20 64 65 66  cZero(D,N).# def
6ee00 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b  ine sqlite3Stack
6ee10 46 72 65 65 28 44 2c 50 29 20 20 20 20 20 20 20  Free(D,P)       
6ee20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 44 2c  sqlite3DbFree(D,
6ee30 50 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65  P).#endif..#ifde
6ee40 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
6ee50 4d 45 4d 53 59 53 33 0a 53 51 4c 49 54 45 5f 50  MEMSYS3.SQLITE_P
6ee60 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c  RIVATE const sql
6ee70 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
6ee80 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d   *sqlite3MemGetM
6ee90 65 6d 73 79 73 33 28 76 6f 69 64 29 3b 0a 23 65  emsys3(void);.#e
6eea0 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
6eeb0 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
6eec0 35 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  5.SQLITE_PRIVATE
6eed0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
6eee0 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69  em_methods *sqli
6eef0 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35  te3MemGetMemsys5
6ef00 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a  (void);.#endif..
6ef10 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
6ef20 4d 55 54 45 58 5f 4f 4d 49 54 0a 53 51 4c 49 54  MUTEX_OMIT.SQLIT
6ef30 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69  E_PRIVATE   sqli
6ef40 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
6ef50 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c  s *sqlite3Defaul
6ef60 74 4d 75 74 65 78 28 76 6f 69 64 29 3b 0a 53 51  tMutex(void);.SQ
6ef70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73  LITE_PRIVATE   s
6ef80 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71  qlite3_mutex *sq
6ef90 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
6efa0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6efb0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
6efc0 65 33 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64  e3MutexInit(void
6efd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6efe0 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  E   int sqlite3M
6eff0 75 74 65 78 45 6e 64 28 76 6f 69 64 29 3b 0a 23  utexEnd(void);.#
6f000 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
6f010 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6f020 33 53 74 61 74 75 73 56 61 6c 75 65 28 69 6e 74  3StatusValue(int
6f030 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6f040 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74  E void sqlite3St
6f050 61 74 75 73 41 64 64 28 69 6e 74 2c 20 69 6e 74  atusAdd(int, int
6f060 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6f070 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74  E void sqlite3St
6f080 61 74 75 73 53 65 74 28 69 6e 74 2c 20 69 6e 74  atusSet(int, int
6f090 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
6f0a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73  TE int sqlite3Is
6f0b0 4e 61 4e 28 64 6f 75 62 6c 65 29 3b 0a 0a 53 51  NaN(double);..SQ
6f0c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6f0d0 64 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74  d sqlite3VXPrint
6f0e0 66 28 53 74 72 41 63 63 75 6d 2a 2c 20 69 6e 74  f(StrAccum*, int
6f0f0 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76  , const char*, v
6f100 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f  a_list);.SQLITE_
6f110 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
6f120 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 73 71 6c  lite3MPrintf(sql
6f130 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
6f140 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f  *, ...);.SQLITE_
6f150 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
6f160 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 73 71  lite3VMPrintf(sq
6f170 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
6f180 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51  r*, va_list);.SQ
6f190 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
6f1a0 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e  r *sqlite3MAppen
6f1b0 64 66 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72  df(sqlite3*,char
6f1c0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e  *,const char*,..
6f1d0 2e 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .);.#if defined(
6f1e0 53 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20  SQLITE_TEST) || 
6f1f0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
6f200 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49  EBUG).SQLITE_PRI
6f210 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
6f220 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 63  te3DebugPrintf(c
6f230 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29  onst char*, ...)
6f240 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66  ;.#endif.#if def
6f250 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
6f260 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ).SQLITE_PRIVATE
6f270 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33     void *sqlite3
6f280 54 65 73 74 54 65 78 74 54 6f 50 74 72 28 63 6f  TestTextToPtr(co
6f290 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64  nst char*);.#end
6f2a0 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  if.SQLITE_PRIVAT
6f2b0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65  E void sqlite3Se
6f2c0 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c  tString(char **,
6f2d0 20 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74   sqlite3*, const
6f2e0 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51   char*, ...);.SQ
6f2f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6f300 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73  d sqlite3ErrorMs
6f310 67 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20  g(Parse*, const 
6f320 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c  char*, ...);.SQL
6f330 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6f340 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65   sqlite3ErrorCle
6f350 61 72 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49  ar(Parse*);.SQLI
6f360 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6f370 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63 68  qlite3Dequote(ch
6f380 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ar*);.SQLITE_PRI
6f390 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6f3a0 4b 65 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73  KeywordCode(cons
6f3b0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a  t unsigned char*
6f3c0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
6f3d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
6f3e0 65 33 52 75 6e 50 61 72 73 65 72 28 50 61 72 73  e3RunParser(Pars
6f3f0 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e*, const char*,
6f400 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54   char **);.SQLIT
6f410 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6f420 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69  qlite3FinishCodi
6f430 6e 67 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49  ng(Parse*);.SQLI
6f440 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6f450 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
6f460 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45  (Parse*);.SQLITE
6f470 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6f480 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
6f490 52 65 67 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b  Reg(Parse*,int);
6f4a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f4b0 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65  int sqlite3GetTe
6f4c0 6d 70 52 61 6e 67 65 28 50 61 72 73 65 2a 2c 69  mpRange(Parse*,i
6f4d0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6f4e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6f4f0 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65  ReleaseTempRange
6f500 28 50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29  (Parse*,int,int)
6f510 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6f520 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78   Expr *sqlite3Ex
6f530 70 72 41 6c 6c 6f 63 28 73 71 6c 69 74 65 33 2a  prAlloc(sqlite3*
6f540 2c 69 6e 74 2c 63 6f 6e 73 74 20 54 6f 6b 65 6e  ,int,const Token
6f550 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *,int);.SQLITE_P
6f560 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
6f570 69 74 65 33 45 78 70 72 28 73 71 6c 69 74 65 33  ite3Expr(sqlite3
6f580 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72  *,int,const char
6f590 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6f5a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
6f5b0 78 70 72 41 74 74 61 63 68 53 75 62 74 72 65 65  xprAttachSubtree
6f5c0 73 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a  s(sqlite3*,Expr*
6f5d0 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29 3b 0a 53  ,Expr*,Expr*);.S
6f5e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
6f5f0 70 72 20 2a 73 71 6c 69 74 65 33 50 45 78 70 72  pr *sqlite3PExpr
6f600 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 45 78  (Parse*, int, Ex
6f610 70 72 2a 2c 20 45 78 70 72 2a 2c 20 63 6f 6e 73  pr*, Expr*, cons
6f620 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  t Token*);.SQLIT
6f630 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
6f640 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73  sqlite3ExprAnd(s
6f650 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 20 45  qlite3*,Expr*, E
6f660 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
6f670 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
6f680 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28  te3ExprFunction(
6f690 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a  Parse*,ExprList*
6f6a0 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  , Token*);.SQLIT
6f6b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6f6c0 71 6c 69 74 65 33 45 78 70 72 41 73 73 69 67 6e  qlite3ExprAssign
6f6d0 56 61 72 4e 75 6d 62 65 72 28 50 61 72 73 65 2a  VarNumber(Parse*
6f6e0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
6f6f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6f700 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 73  lite3ExprClear(s
6f710 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b  qlite3*, Expr*);
6f720 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f730 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
6f740 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c  Delete(sqlite3*,
6f750 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
6f760 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74  PRIVATE ExprList
6f770 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73   *sqlite3ExprLis
6f780 74 41 70 70 65 6e 64 28 50 61 72 73 65 2a 2c 45  tAppend(Parse*,E
6f790 78 70 72 4c 69 73 74 2a 2c 45 78 70 72 2a 29 3b  xprList*,Expr*);
6f7a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f7b0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
6f7c0 4c 69 73 74 53 65 74 4e 61 6d 65 28 50 61 72 73  ListSetName(Pars
6f7d0 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 54 6f 6b  e*,ExprList*,Tok
6f7e0 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  en*,int);.SQLITE
6f7f0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6f800 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74  lite3ExprListSet
6f810 53 70 61 6e 28 50 61 72 73 65 2a 2c 45 78 70 72  Span(Parse*,Expr
6f820 4c 69 73 74 2a 2c 45 78 70 72 53 70 61 6e 2a 29  List*,ExprSpan*)
6f830 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6f840 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
6f850 72 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69  rListDelete(sqli
6f860 74 65 33 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29  te3*, ExprList*)
6f870 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6f880 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74   int sqlite3Init
6f890 28 73 71 6c 69 74 65 33 2a 2c 20 63 68 61 72 2a  (sqlite3*, char*
6f8a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6f8b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e  TE int sqlite3In
6f8c0 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 2a  itCallback(void*
6f8d0 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a 2c 20 63  , int, char**, c
6f8e0 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  har**);.SQLITE_P
6f8f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6f900 74 65 33 50 72 61 67 6d 61 28 50 61 72 73 65 2a  te3Pragma(Parse*
6f910 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54  ,Token*,Token*,T
6f920 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  oken*,int);.SQLI
6f930 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6f940 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65  sqlite3ResetInte
6f950 72 6e 61 6c 53 63 68 65 6d 61 28 73 71 6c 69 74  rnalSchema(sqlit
6f960 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  e3*, int);.SQLIT
6f970 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6f980 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73 65  qlite3BeginParse
6f990 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51  (Parse*,int);.SQ
6f9a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6f9b0 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49  d sqlite3CommitI
6f9c0 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 73  nternalChanges(s
6f9d0 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45  qlite3*);.SQLITE
6f9e0 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a  _PRIVATE Table *
6f9f0 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74  sqlite3ResultSet
6fa00 4f 66 53 65 6c 65 63 74 28 50 61 72 73 65 2a 2c  OfSelect(Parse*,
6fa10 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45  Select*);.SQLITE
6fa20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6fa30 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54  lite3OpenMasterT
6fa40 61 62 6c 65 28 50 61 72 73 65 20 2a 2c 20 69 6e  able(Parse *, in
6fa50 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6fa60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
6fa70 74 61 72 74 54 61 62 6c 65 28 50 61 72 73 65 2a  tartTable(Parse*
6fa80 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69  ,Token*,Token*,i
6fa90 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b  nt,int,int,int);
6faa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6fab0 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 43  void sqlite3AddC
6fac0 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 54 6f 6b  olumn(Parse*,Tok
6fad0 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
6fae0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6faf0 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 73  3AddNotNull(Pars
6fb00 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  e*, int);.SQLITE
6fb10 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6fb20 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b  lite3AddPrimaryK
6fb30 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c  ey(Parse*, ExprL
6fb40 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  ist*, int, int, 
6fb50 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6fb60 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6fb70 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61  3AddCheckConstra
6fb80 69 6e 74 28 50 61 72 73 65 2a 2c 20 45 78 70 72  int(Parse*, Expr
6fb90 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6fba0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
6fbb0 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61 72  ddColumnType(Par
6fbc0 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  se*,Token*);.SQL
6fbd0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6fbe0 20 73 71 6c 69 74 65 33 41 64 64 44 65 66 61 75   sqlite3AddDefau
6fbf0 6c 74 56 61 6c 75 65 28 50 61 72 73 65 2a 2c 45  ltValue(Parse*,E
6fc00 78 70 72 53 70 61 6e 2a 29 3b 0a 53 51 4c 49 54  xprSpan*);.SQLIT
6fc10 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6fc20 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65  qlite3AddCollate
6fc30 54 79 70 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b  Type(Parse*, Tok
6fc40 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
6fc50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6fc60 33 45 6e 64 54 61 62 6c 65 28 50 61 72 73 65 2a  3EndTable(Parse*
6fc70 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53  ,Token*,Token*,S
6fc80 65 6c 65 63 74 2a 29 3b 0a 0a 53 51 4c 49 54 45  elect*);..SQLITE
6fc90 5f 50 52 49 56 41 54 45 20 42 69 74 76 65 63 20  _PRIVATE Bitvec 
6fca0 2a 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72  *sqlite3BitvecCr
6fcb0 65 61 74 65 28 75 33 32 29 3b 0a 53 51 4c 49 54  eate(u32);.SQLIT
6fcc0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6fcd0 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28  lite3BitvecTest(
6fce0 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53  Bitvec*, u32);.S
6fcf0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6fd00 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53  t sqlite3BitvecS
6fd10 65 74 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29  et(Bitvec*, u32)
6fd20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6fd30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74   void sqlite3Bit
6fd40 76 65 63 43 6c 65 61 72 28 42 69 74 76 65 63 2a  vecClear(Bitvec*
6fd50 2c 20 75 33 32 2c 20 76 6f 69 64 2a 29 3b 0a 53  , u32, void*);.S
6fd60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6fd70 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  id sqlite3Bitvec
6fd80 44 65 73 74 72 6f 79 28 42 69 74 76 65 63 2a 29  Destroy(Bitvec*)
6fd90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6fda0 20 75 33 32 20 73 71 6c 69 74 65 33 42 69 74 76   u32 sqlite3Bitv
6fdb0 65 63 53 69 7a 65 28 42 69 74 76 65 63 2a 29 3b  ecSize(Bitvec*);
6fdc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6fdd0 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65  int sqlite3Bitve
6fde0 63 42 75 69 6c 74 69 6e 54 65 73 74 28 69 6e 74  cBuiltinTest(int
6fdf0 2c 69 6e 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f  ,int*);..SQLITE_
6fe00 50 52 49 56 41 54 45 20 52 6f 77 53 65 74 20 2a  PRIVATE RowSet *
6fe10 73 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 69  sqlite3RowSetIni
6fe20 74 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64  t(sqlite3*, void
6fe30 2a 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 29  *, unsigned int)
6fe40 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6fe50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 77   void sqlite3Row
6fe60 53 65 74 43 6c 65 61 72 28 52 6f 77 53 65 74 2a  SetClear(RowSet*
6fe70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6fe80 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f  E void sqlite3Ro
6fe90 77 53 65 74 49 6e 73 65 72 74 28 52 6f 77 53 65  wSetInsert(RowSe
6fea0 74 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 45  t*, i64);.SQLITE
6feb0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6fec0 69 74 65 33 52 6f 77 53 65 74 54 65 73 74 28 52  ite3RowSetTest(R
6fed0 6f 77 53 65 74 2a 2c 20 75 38 20 69 42 61 74 63  owSet*, u8 iBatc
6fee0 68 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f  h, i64);.SQLITE_
6fef0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6ff00 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28 52 6f  te3RowSetNext(Ro
6ff10 77 53 65 74 2a 2c 20 69 36 34 2a 29 3b 0a 0a 53  wSet*, i64*);..S
6ff20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6ff30 69 64 20 73 71 6c 69 74 65 33 43 72 65 61 74 65  id sqlite3Create
6ff40 56 69 65 77 28 50 61 72 73 65 2a 2c 54 6f 6b 65  View(Parse*,Toke
6ff50 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a  n*,Token*,Token*
6ff60 2c 53 65 6c 65 63 74 2a 2c 69 6e 74 2c 69 6e 74  ,Select*,int,int
6ff70 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  );..#if !defined
6ff80 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45  (SQLITE_OMIT_VIE
6ff90 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53  W) || !defined(S
6ffa0 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
6ffb0 41 4c 54 41 42 4c 45 29 0a 53 51 4c 49 54 45 5f  ALTABLE).SQLITE_
6ffc0 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
6ffd0 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75  lite3ViewGetColu
6ffe0 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65 2a 2c 54  mnNames(Parse*,T
6fff0 61 62 6c 65 2a 29 3b 0a 23 65 6c 73 65 0a 23 20  able*);.#else.# 
70000 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 69  define sqlite3Vi
70010 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73  ewGetColumnNames
70020 28 41 2c 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a  (A,B) 0.#endif..
70030 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70040 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54  oid sqlite3DropT
70050 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63  able(Parse*, Src
70060 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29  List*, int, int)
70070 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70080 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c   void sqlite3Del
70090 65 74 65 54 61 62 6c 65 28 54 61 62 6c 65 2a 29  eteTable(Table*)
700a0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
700b0 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d  _OMIT_AUTOINCREM
700c0 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ENT.SQLITE_PRIVA
700d0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
700e0 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65  3AutoincrementBe
700f0 67 69 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73  gin(Parse *pPars
70100 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  e);.SQLITE_PRIVA
70110 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
70120 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e  3AutoincrementEn
70130 64 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  d(Parse *pParse)
70140 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ;.#else.# define
70150 20 73 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 72   sqlite3Autoincr
70160 65 6d 65 6e 74 42 65 67 69 6e 28 58 29 0a 23 20  ementBegin(X).# 
70170 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75  define sqlite3Au
70180 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 58  toincrementEnd(X
70190 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f  ).#endif.SQLITE_
701a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
701b0 69 74 65 33 49 6e 73 65 72 74 28 50 61 72 73 65  ite3Insert(Parse
701c0 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70  *, SrcList*, Exp
701d0 72 4c 69 73 74 2a 2c 20 53 65 6c 65 63 74 2a 2c  rList*, Select*,
701e0 20 49 64 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a   IdList*, int);.
701f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70200 6f 69 64 20 2a 73 71 6c 69 74 65 33 41 72 72 61  oid *sqlite3Arra
70210 79 41 6c 6c 6f 63 61 74 65 28 73 71 6c 69 74 65  yAllocate(sqlite
70220 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74  3*,void*,int,int
70230 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29  ,int*,int*,int*)
70240 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70250 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33   IdList *sqlite3
70260 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c  IdListAppend(sql
70270 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 2c 20  ite3*, IdList*, 
70280 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
70290 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
702a0 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28 49  te3IdListIndex(I
702b0 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 20 63 68 61  dList*,const cha
702c0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
702d0 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c  ATE SrcList *sql
702e0 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72  ite3SrcListEnlar
702f0 67 65 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63  ge(sqlite3*, Src
70300 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29  List*, int, int)
70310 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70320 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65   SrcList *sqlite
70330 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 73  3SrcListAppend(s
70340 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74  qlite3*, SrcList
70350 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e  *, Token*, Token
70360 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
70370 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69  TE SrcList *sqli
70380 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
70390 46 72 6f 6d 54 65 72 6d 28 50 61 72 73 65 2a 2c  FromTerm(Parse*,
703a0 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e   SrcList*, Token
703b0 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20  *, Token*,.     
703c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
703d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
703e0 20 54 6f 6b 65 6e 2a 2c 20 53 65 6c 65 63 74 2a   Token*, Select*
703f0 2c 20 45 78 70 72 2a 2c 20 49 64 4c 69 73 74 2a  , Expr*, IdList*
70400 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
70410 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72  E void sqlite3Sr
70420 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28 50  cListIndexedBy(P
70430 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20  arse *, SrcList 
70440 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c  *, Token *);.SQL
70450 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
70460 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79  sqlite3IndexedBy
70470 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 2a 2c 20  Lookup(Parse *, 
70480 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
70490 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tem *);.SQLITE_P
704a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
704b0 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a  te3SrcListShiftJ
704c0 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 73 74 2a  oinType(SrcList*
704d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
704e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72  E void sqlite3Sr
704f0 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f  cListAssignCurso
70500 72 73 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69  rs(Parse*, SrcLi
70510 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
70520 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
70530 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 73 71  3IdListDelete(sq
70540 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 29  lite3*, IdList*)
70550 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70560 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63   void sqlite3Src
70570 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74  ListDelete(sqlit
70580 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a  e3*, SrcList*);.
70590 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49  SQLITE_PRIVATE I
705a0 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 43 72 65  ndex *sqlite3Cre
705b0 61 74 65 49 6e 64 65 78 28 50 61 72 73 65 2a 2c  ateIndex(Parse*,
705c0 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 72  Token*,Token*,Sr
705d0 63 4c 69 73 74 2a 2c 45 78 70 72 4c 69 73 74 2a  cList*,ExprList*
705e0 2c 69 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 20  ,int,Token*,.   
705f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70600 20 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e 74       Token*, int
70610 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
70620 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
70630 74 65 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72  te3DropIndex(Par
70640 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69  se*, SrcList*, i
70650 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
70660 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53  ATE int sqlite3S
70670 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 20 53 65  elect(Parse*, Se
70680 6c 65 63 74 2a 2c 20 53 65 6c 65 63 74 44 65 73  lect*, SelectDes
70690 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
706a0 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69  ATE Select *sqli
706b0 74 65 33 53 65 6c 65 63 74 4e 65 77 28 50 61 72  te3SelectNew(Par
706c0 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 53 72  se*,ExprList*,Sr
706d0 63 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 45 78 70  cList*,Expr*,Exp
706e0 72 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20  rList*,.        
706f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70700 20 45 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a   Expr*,ExprList*
70710 2c 69 6e 74 2c 45 78 70 72 2a 2c 45 78 70 72 2a  ,int,Expr*,Expr*
70720 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
70730 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65  E void sqlite3Se
70740 6c 65 63 74 44 65 6c 65 74 65 28 73 71 6c 69 74  lectDelete(sqlit
70750 65 33 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53  e3*, Select*);.S
70760 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 61  QLITE_PRIVATE Ta
70770 62 6c 65 20 2a 73 71 6c 69 74 65 33 53 72 63 4c  ble *sqlite3SrcL
70780 69 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 2a  istLookup(Parse*
70790 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c  , SrcList*);.SQL
707a0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
707b0 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c  sqlite3IsReadOnl
707c0 79 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a  y(Parse*, Table*
707d0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
707e0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
707f0 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 50 61 72  te3OpenTable(Par
70800 73 65 2a 2c 20 69 6e 74 20 69 43 75 72 2c 20 69  se*, int iCur, i
70810 6e 74 20 69 44 62 2c 20 54 61 62 6c 65 2a 2c 20  nt iDb, Table*, 
70820 69 6e 74 29 3b 0a 23 69 66 20 64 65 66 69 6e 65  int);.#if define
70830 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
70840 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49  UPDATE_DELETE_LI
70850 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64  MIT) && !defined
70860 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42  (SQLITE_OMIT_SUB
70870 51 55 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52  QUERY).SQLITE_PR
70880 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
70890 74 65 33 4c 69 6d 69 74 57 68 65 72 65 28 50 61  te3LimitWhere(Pa
708a0 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a  rse *, SrcList *
708b0 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72 4c 69  , Expr *, ExprLi
708c0 73 74 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78  st *, Expr *, Ex
708d0 70 72 20 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23  pr *, char *);.#
708e0 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49  endif.SQLITE_PRI
708f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
70900 33 44 65 6c 65 74 65 46 72 6f 6d 28 50 61 72 73  3DeleteFrom(Pars
70910 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78  e*, SrcList*, Ex
70920 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
70930 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
70940 33 55 70 64 61 74 65 28 50 61 72 73 65 2a 2c 20  3Update(Parse*, 
70950 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69  SrcList*, ExprLi
70960 73 74 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29  st*, Expr*, int)
70970 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70980 20 57 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69   WhereInfo *sqli
70990 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 50 61  te3WhereBegin(Pa
709a0 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20  rse*, SrcList*, 
709b0 45 78 70 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a  Expr*, ExprList*
709c0 2a 2c 20 75 31 36 29 3b 0a 53 51 4c 49 54 45 5f  *, u16);.SQLITE_
709d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
709e0 69 74 65 33 57 68 65 72 65 45 6e 64 28 57 68 65  ite3WhereEnd(Whe
709f0 72 65 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 45  reInfo*);.SQLITE
70a00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
70a10 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43  ite3ExprCodeGetC
70a20 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c 20 54 61  olumn(Parse*, Ta
70a30 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  ble*, int, int, 
70a40 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  int, int);.SQLIT
70a50 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
70a60 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f  qlite3ExprCodeMo
70a70 76 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20  ve(Parse*, int, 
70a80 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  int, int);.SQLIT
70a90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
70aa0 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f  qlite3ExprCodeCo
70ab0 70 79 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20  py(Parse*, int, 
70ac0 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  int, int);.SQLIT
70ad0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
70ae0 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 53  qlite3ExprCacheS
70af0 74 6f 72 65 28 50 61 72 73 65 2a 2c 20 69 6e 74  tore(Parse*, int
70b00 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
70b10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
70b20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
70b30 65 50 75 73 68 28 50 61 72 73 65 2a 29 3b 0a 53  ePush(Parse*);.S
70b40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
70b50 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61  id sqlite3ExprCa
70b60 63 68 65 50 6f 70 28 50 61 72 73 65 2a 2c 20 69  chePop(Parse*, i
70b70 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
70b80 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
70b90 45 78 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28  ExprCacheRemove(
70ba0 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51  Parse*, int);.SQ
70bb0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
70bc0 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63  d sqlite3ExprCac
70bd0 68 65 43 6c 65 61 72 28 50 61 72 73 65 2a 29 3b  heClear(Parse*);
70be0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70bf0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
70c00 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
70c10 6e 67 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c  nge(Parse*, int,
70c20 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
70c30 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
70c40 65 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 50  e3ExprHardCopy(P
70c50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a  arse*,int,int);.
70c60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
70c70 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  nt sqlite3ExprCo
70c80 64 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a  de(Parse*, Expr*
70c90 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
70ca0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
70cb0 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 50  e3ExprCodeTemp(P
70cc0 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e  arse*, Expr*, in
70cd0 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
70ce0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
70cf0 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 50 61  xprCodeTarget(Pa
70d00 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74  rse*, Expr*, int
70d10 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
70d20 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
70d30 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28 50 61  rCodeAndCache(Pa
70d40 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74  rse*, Expr*, int
70d50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
70d60 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
70d70 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28  prCodeConstants(
70d80 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a  Parse*, Expr*);.
70d90 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
70da0 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  nt sqlite3ExprCo
70db0 64 65 45 78 70 72 4c 69 73 74 28 50 61 72 73 65  deExprList(Parse
70dc0 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e  *, ExprList*, in
70dd0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
70de0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
70df0 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 50  ite3ExprIfTrue(P
70e00 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e  arse*, Expr*, in
70e10 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
70e20 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
70e30 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
70e40 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69  Parse*, Expr*, i
70e50 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
70e60 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a  _PRIVATE Table *
70e70 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
70e80 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
70e90 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61  char*, const cha
70ea0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
70eb0 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74  ATE Table *sqlit
70ec0 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 50 61  e3LocateTable(Pa
70ed0 72 73 65 2a 2c 69 6e 74 20 69 73 56 69 65 77 2c  rse*,int isView,
70ee0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e  const char*, con
70ef0 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  st char*);.SQLIT
70f00 45 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78 20  E_PRIVATE Index 
70f10 2a 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65  *sqlite3FindInde
70f20 78 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  x(sqlite3*,const
70f30 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68   char*, const ch
70f40 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ar*);.SQLITE_PRI
70f50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
70f60 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65  3UnlinkAndDelete
70f70 54 61 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 69  Table(sqlite3*,i
70f80 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  nt,const char*);
70f90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70fa0 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69  void sqlite3Unli
70fb0 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78  nkAndDeleteIndex
70fc0 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f  (sqlite3*,int,co
70fd0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
70fe0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
70ff0 73 71 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61  sqlite3Vacuum(Pa
71000 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  rse*);.SQLITE_PR
71010 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
71020 33 52 75 6e 56 61 63 75 75 6d 28 63 68 61 72 2a  3RunVacuum(char*
71030 2a 2c 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  *, sqlite3*);.SQ
71040 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
71050 72 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72  r *sqlite3NameFr
71060 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 2a  omToken(sqlite3*
71070 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  , Token*);.SQLIT
71080 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
71090 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65  lite3ExprCompare
710a0 28 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a  (Expr*, Expr*);.
710b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
710c0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41  oid sqlite3ExprA
710d0 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73  nalyzeAggregates
710e0 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45  (NameContext*, E
710f0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
71100 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
71110 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
71120 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74  List(NameContext
71130 2a 2c 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51  *,ExprList*);.SQ
71140 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62  LITE_PRIVATE Vdb
71150 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56 64 62  e *sqlite3GetVdb
71160 65 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54  e(Parse*);.SQLIT
71170 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
71180 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 45  sqlite3CreateIdE
71190 78 70 72 28 50 61 72 73 65 20 2a 2c 20 63 6f 6e  xpr(Parse *, con
711a0 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  st char*);.SQLIT
711b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
711c0 71 6c 69 74 65 33 50 72 6e 67 53 61 76 65 53 74  qlite3PrngSaveSt
711d0 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  ate(void);.SQLIT
711e0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
711f0 71 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72  qlite3PrngRestor
71200 65 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51  eState(void);.SQ
71210 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
71220 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73  d sqlite3PrngRes
71230 65 74 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53  etState(void);.S
71240 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
71250 69 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61  id sqlite3Rollba
71260 63 6b 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b  ckAll(sqlite3*);
71270 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71280 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65  void sqlite3Code
71290 56 65 72 69 66 79 53 63 68 65 6d 61 28 50 61 72  VerifySchema(Par
712a0 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  se*, int);.SQLIT
712b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
712c0 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73  qlite3BeginTrans
712d0 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69  action(Parse*, i
712e0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
712f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
71300 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f  CommitTransactio
71310 6e 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54  n(Parse*);.SQLIT
71320 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
71330 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72  qlite3RollbackTr
71340 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a  ansaction(Parse*
71350 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71360 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 61  E void sqlite3Sa
71370 76 65 70 6f 69 6e 74 28 50 61 72 73 65 2a 2c 20  vepoint(Parse*, 
71380 69 6e 74 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  int, Token*);.SQ
71390 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
713a0 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61  d sqlite3CloseSa
713b0 76 65 70 6f 69 6e 74 73 28 73 71 6c 69 74 65 33  vepoints(sqlite3
713c0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
713d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
713e0 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78  xprIsConstant(Ex
713f0 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
71400 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
71410 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f  ExprIsConstantNo
71420 74 4a 6f 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51  tJoin(Expr*);.SQ
71430 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
71440 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f   sqlite3ExprIsCo
71450 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e  nstantOrFunction
71460 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Expr*);.SQLITE_
71470 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
71480 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72  te3ExprIsInteger
71490 28 45 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53  (Expr*, int*);.S
714a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
714b0 74 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64  t sqlite3IsRowid
714c0 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53  (const char*);.S
714d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
714e0 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61  id sqlite3Genera
714f0 74 65 52 6f 77 44 65 6c 65 74 65 28 50 61 72 73  teRowDelete(Pars
71500 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c  e*, Table*, int,
71510 20 69 6e 74 2c 20 69 6e 74 2c 20 54 72 69 67 67   int, int, Trigg
71520 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  er *, int);.SQLI
71530 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
71540 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52  sqlite3GenerateR
71550 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 28 50 61  owIndexDelete(Pa
71560 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e  rse*, Table*, in
71570 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45  t, int*);.SQLITE
71580 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
71590 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65  ite3GenerateInde
715a0 78 4b 65 79 28 50 61 72 73 65 2a 2c 20 49 6e 64  xKey(Parse*, Ind
715b0 65 78 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69  ex*, int, int, i
715c0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
715d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
715e0 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69  GenerateConstrai
715f0 6e 74 43 68 65 63 6b 73 28 50 61 72 73 65 2a 2c  ntChecks(Parse*,
71600 54 61 62 6c 65 2a 2c 69 6e 74 2c 69 6e 74 2c 0a  Table*,int,int,.
71610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71630 20 20 20 20 20 69 6e 74 2a 2c 69 6e 74 2c 69 6e       int*,int,in
71640 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2a 29 3b  t,int,int,int*);
71650 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71660 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 70  void sqlite3Comp
71670 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 50 61  leteInsertion(Pa
71680 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e  rse*, Table*, in
71690 74 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c 20 69 6e  t, int, int*, in
716a0 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  t, int, int);.SQ
716b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
716c0 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c   sqlite3OpenTabl
716d0 65 41 6e 64 49 6e 64 69 63 65 73 28 50 61 72 73  eAndIndices(Pars
716e0 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c  e*, Table*, int,
716f0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
71700 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
71710 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
71720 61 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e  ation(Parse*, in
71730 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
71740 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
71750 69 74 65 33 4d 75 6c 74 69 57 72 69 74 65 28 50  ite3MultiWrite(P
71760 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  arse*);.SQLITE_P
71770 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
71780 74 65 33 4d 61 79 41 62 6f 72 74 28 50 61 72 73  te3MayAbort(Pars
71790 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
717a0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
717b0 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 50  HaltConstraint(P
717c0 61 72 73 65 2a 2c 20 69 6e 74 2c 20 63 68 61 72  arse*, int, char
717d0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
717e0 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
717f0 6c 69 74 65 33 45 78 70 72 44 75 70 28 73 71 6c  lite3ExprDup(sql
71800 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69 6e 74 29  ite3*,Expr*,int)
71810 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
71820 20 45 78 70 72 4c 69 73 74 20 2a 73 71 6c 69 74   ExprList *sqlit
71830 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 73 71  e3ExprListDup(sq
71840 6c 69 74 65 33 2a 2c 45 78 70 72 4c 69 73 74 2a  lite3*,ExprList*
71850 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
71860 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73  IVATE SrcList *s
71870 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70  qlite3SrcListDup
71880 28 73 71 6c 69 74 65 33 2a 2c 53 72 63 4c 69 73  (sqlite3*,SrcLis
71890 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t*,int);.SQLITE_
718a0 50 52 49 56 41 54 45 20 49 64 4c 69 73 74 20 2a  PRIVATE IdList *
718b0 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70  sqlite3IdListDup
718c0 28 73 71 6c 69 74 65 33 2a 2c 49 64 4c 69 73 74  (sqlite3*,IdList
718d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
718e0 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74  TE Select *sqlit
718f0 65 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c 69  e3SelectDup(sqli
71900 74 65 33 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e 74  te3*,Select*,int
71910 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71920 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 75  E void sqlite3Fu
71930 6e 63 44 65 66 49 6e 73 65 72 74 28 46 75 6e 63  ncDefInsert(Func
71940 44 65 66 48 61 73 68 2a 2c 20 46 75 6e 63 44 65  DefHash*, FuncDe
71950 66 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  f*);.SQLITE_PRIV
71960 41 54 45 20 46 75 6e 63 44 65 66 20 2a 73 71 6c  ATE FuncDef *sql
71970 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e  ite3FindFunction
71980 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
71990 63 68 61 72 2a 2c 69 6e 74 2c 69 6e 74 2c 75 38  char*,int,int,u8
719a0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
719b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
719c0 65 33 52 65 67 69 73 74 65 72 42 75 69 6c 74 69  e3RegisterBuilti
719d0 6e 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74  nFunctions(sqlit
719e0 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e3*);.SQLITE_PRI
719f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
71a00 33 52 65 67 69 73 74 65 72 44 61 74 65 54 69 6d  3RegisterDateTim
71a10 65 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29  eFunctions(void)
71a20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
71a30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67   void sqlite3Reg
71a40 69 73 74 65 72 47 6c 6f 62 61 6c 46 75 6e 63 74  isterGlobalFunct
71a50 69 6f 6e 73 28 76 6f 69 64 29 3b 0a 23 69 66 64  ions(void);.#ifd
71a60 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
71a70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
71a80 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65   int sqlite3Safe
71a90 74 79 4f 6e 28 73 71 6c 69 74 65 33 2a 29 3b 0a  tyOn(sqlite3*);.
71aa0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
71ab0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65   int sqlite3Safe
71ac0 74 79 4f 66 66 28 73 71 6c 69 74 65 33 2a 29 3b  tyOff(sqlite3*);
71ad0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
71ae0 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
71af0 41 29 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71  A) 0.# define sq
71b00 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 41  lite3SafetyOff(A
71b10 29 20 30 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54  ) 0.#endif.SQLIT
71b20 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
71b30 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b  lite3SafetyCheck
71b40 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  Ok(sqlite3*);.SQ
71b50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
71b60 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68   sqlite3SafetyCh
71b70 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73 71 6c 69  eckSickOrOk(sqli
71b80 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  te3*);.SQLITE_PR
71b90 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
71ba0 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 50  e3ChangeCookie(P
71bb0 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69  arse*, int);..#i
71bc0 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
71bd0 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 20  E_OMIT_VIEW) && 
71be0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
71bf0 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 53 51  OMIT_TRIGGER).SQ
71c00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
71c10 64 20 73 71 6c 69 74 65 33 4d 61 74 65 72 69 61  d sqlite3Materia
71c20 6c 69 7a 65 56 69 65 77 28 50 61 72 73 65 2a 2c  lizeView(Parse*,
71c30 20 54 61 62 6c 65 2a 2c 20 45 78 70 72 2a 2c 20   Table*, Expr*, 
71c40 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69  int);.#endif..#i
71c50 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
71c60 54 5f 54 52 49 47 47 45 52 0a 53 51 4c 49 54 45  T_TRIGGER.SQLITE
71c70 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
71c80 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 69 67  sqlite3BeginTrig
71c90 67 65 72 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65  ger(Parse*, Toke
71ca0 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 2c 69 6e  n*,Token*,int,in
71cb0 74 2c 49 64 4c 69 73 74 2a 2c 53 72 63 4c 69 73  t,IdList*,SrcLis
71cc0 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  t*,.            
71cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45                 E
71ce0 78 70 72 2a 2c 69 6e 74 2c 20 69 6e 74 29 3b 0a  xpr*,int, int);.
71cf0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
71d00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 69 6e   void sqlite3Fin
71d10 69 73 68 54 72 69 67 67 65 72 28 50 61 72 73 65  ishTrigger(Parse
71d20 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 2c  *, TriggerStep*,
71d30 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
71d40 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
71d50 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67  sqlite3DropTrigg
71d60 65 72 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69  er(Parse*, SrcLi
71d70 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  st*, int);.SQLIT
71d80 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
71d90 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67   sqlite3DropTrig
71da0 67 65 72 50 74 72 28 50 61 72 73 65 2a 2c 20 54  gerPtr(Parse*, T
71db0 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  rigger*);.SQLITE
71dc0 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67  _PRIVATE   Trigg
71dd0 65 72 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67  er *sqlite3Trigg
71de0 65 72 73 45 78 69 73 74 28 50 61 72 73 65 20 2a  ersExist(Parse *
71df0 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 45  , Table*, int, E
71e00 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 20 2a 70  xprList*, int *p
71e10 4d 61 73 6b 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Mask);.SQLITE_PR
71e20 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72 20  IVATE   Trigger 
71e30 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c  *sqlite3TriggerL
71e40 69 73 74 28 50 61 72 73 65 20 2a 2c 20 54 61 62  ist(Parse *, Tab
71e50 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  le *);.SQLITE_PR
71e60 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
71e70 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67  ite3CodeRowTrigg
71e80 65 72 28 50 61 72 73 65 2a 2c 20 54 72 69 67 67  er(Parse*, Trigg
71e90 65 72 20 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c  er *, int, ExprL
71ea0 69 73 74 2a 2c 20 69 6e 74 2c 20 54 61 62 6c 65  ist*, int, Table
71eb0 20 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   *,.            
71ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71ed0 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  int, int, int);.
71ee0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
71ef0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64   void sqlite3Cod
71f00 65 52 6f 77 54 72 69 67 67 65 72 44 69 72 65 63  eRowTriggerDirec
71f10 74 28 50 61 72 73 65 20 2a 2c 20 54 72 69 67 67  t(Parse *, Trigg
71f20 65 72 20 2a 2c 20 54 61 62 6c 65 20 2a 2c 20 69  er *, Table *, i
71f30 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 20  nt, int, int);. 
71f40 20 76 6f 69 64 20 73 71 6c 69 74 65 56 69 65 77   void sqliteView
71f50 54 72 69 67 67 65 72 73 28 50 61 72 73 65 2a 2c  Triggers(Parse*,
71f60 20 54 61 62 6c 65 2a 2c 20 45 78 70 72 2a 2c 20   Table*, Expr*, 
71f70 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b  int, ExprList*);
71f80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71f90 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65    void sqlite3De
71fa0 6c 65 74 65 54 72 69 67 67 65 72 53 74 65 70 28  leteTriggerStep(
71fb0 73 71 6c 69 74 65 33 2a 2c 20 54 72 69 67 67 65  sqlite3*, Trigge
71fc0 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54 45 5f  rStep*);.SQLITE_
71fd0 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65  PRIVATE   Trigge
71fe0 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72  rStep *sqlite3Tr
71ff0 69 67 67 65 72 53 65 6c 65 63 74 53 74 65 70 28  iggerSelectStep(
72000 73 71 6c 69 74 65 33 2a 2c 53 65 6c 65 63 74 2a  sqlite3*,Select*
72010 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
72020 45 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20  E   TriggerStep 
72030 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 49  *sqlite3TriggerI
72040 6e 73 65 72 74 53 74 65 70 28 73 71 6c 69 74 65  nsertStep(sqlite
72050 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 49 64 4c 69 73  3*,Token*, IdLis
72060 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  t*,.            
72070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72080 20 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72              Expr
72090 4c 69 73 74 2a 2c 53 65 6c 65 63 74 2a 2c 75 38  List*,Select*,u8
720a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
720b0 45 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20  E   TriggerStep 
720c0 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 55  *sqlite3TriggerU
720d0 70 64 61 74 65 53 74 65 70 28 73 71 6c 69 74 65  pdateStep(sqlite
720e0 33 2a 2c 54 6f 6b 65 6e 2a 2c 45 78 70 72 4c 69  3*,Token*,ExprLi
720f0 73 74 2a 2c 20 45 78 70 72 2a 2c 20 75 38 29 3b  st*, Expr*, u8);
72100 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72110 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73    TriggerStep *s
72120 71 6c 69 74 65 33 54 72 69 67 67 65 72 44 65 6c  qlite3TriggerDel
72130 65 74 65 53 74 65 70 28 73 71 6c 69 74 65 33 2a  eteStep(sqlite3*
72140 2c 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 2a 29 3b  ,Token*, Expr*);
72150 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72160 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65    void sqlite3De
72170 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c 69  leteTrigger(sqli
72180 74 65 33 2a 2c 20 54 72 69 67 67 65 72 2a 29 3b  te3*, Trigger*);
72190 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
721a0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e    void sqlite3Un
721b0 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72 69  linkAndDeleteTri
721c0 67 67 65 72 28 73 71 6c 69 74 65 33 2a 2c 69 6e  gger(sqlite3*,in
721d0 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  t,const char*);.
721e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
721f0 20 75 33 32 20 73 71 6c 69 74 65 33 54 72 69 67   u32 sqlite3Trig
72200 67 65 72 4f 6c 64 6d 61 73 6b 28 50 61 72 73 65  gerOldmask(Parse
72210 2a 2c 54 72 69 67 67 65 72 2a 2c 45 78 70 72 4c  *,Trigger*,ExprL
72220 69 73 74 2a 2c 54 61 62 6c 65 2a 2c 69 6e 74 29  ist*,Table*,int)
72230 3b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ;.# define sqlit
72240 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28  e3ParseToplevel(
72250 70 29 20 28 28 70 29 2d 3e 70 54 6f 70 6c 65 76  p) ((p)->pToplev
72260 65 6c 20 3f 20 28 70 29 2d 3e 70 54 6f 70 6c 65  el ? (p)->pTople
72270 76 65 6c 20 3a 20 28 70 29 29 0a 23 65 6c 73 65  vel : (p)).#else
72280 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
72290 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28 42  3TriggersExist(B
722a0 2c 43 2c 44 2c 45 2c 46 29 20 30 0a 23 20 64 65  ,C,D,E,F) 0.# de
722b0 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65 6c 65  fine sqlite3Dele
722c0 74 65 54 72 69 67 67 65 72 28 41 2c 42 29 0a 23  teTrigger(A,B).#
722d0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44   define sqlite3D
722e0 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 41 2c  ropTriggerPtr(A,
722f0 42 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  B).# define sqli
72300 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65  te3UnlinkAndDele
72310 74 65 54 72 69 67 67 65 72 28 41 2c 42 2c 43 29  teTrigger(A,B,C)
72320 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
72330 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28  3CodeRowTrigger(
72340 41 2c 42 2c 43 2c 44 2c 45 2c 46 2c 47 2c 48 2c  A,B,C,D,E,F,G,H,
72350 49 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  I).# define sqli
72360 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65  te3CodeRowTrigge
72370 72 44 69 72 65 63 74 28 41 2c 42 2c 43 2c 44 2c  rDirect(A,B,C,D,
72380 45 2c 46 29 0a 23 20 64 65 66 69 6e 65 20 73 71  E,F).# define sq
72390 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 73 74  lite3TriggerList
723a0 28 58 2c 20 59 29 20 30 0a 23 20 64 65 66 69 6e  (X, Y) 0.# defin
723b0 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f  e sqlite3ParseTo
723c0 70 6c 65 76 65 6c 28 70 29 20 70 0a 23 20 64 65  plevel(p) p.# de
723d0 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 69 67  fine sqlite3Trig
723e0 67 65 72 4f 6c 64 6d 61 73 6b 28 41 2c 42 2c 43  gerOldmask(A,B,C
723f0 2c 44 2c 45 29 20 30 0a 23 65 6e 64 69 66 0a 0a  ,D,E) 0.#endif..
72400 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
72410 6e 74 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79  nt sqlite3JoinTy
72420 70 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e  pe(Parse*, Token
72430 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e  *, Token*, Token
72440 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
72450 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
72460 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 28  reateForeignKey(
72470 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74  Parse*, ExprList
72480 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 4c  *, Token*, ExprL
72490 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ist*, int);.SQLI
724a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
724b0 73 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65  sqlite3DeferFore
724c0 69 67 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20 69  ignKey(Parse*, i
724d0 6e 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  nt);.#ifndef SQL
724e0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
724f0 5a 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52  ZATION.SQLITE_PR
72500 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
72510 69 74 65 33 41 75 74 68 52 65 61 64 28 50 61 72  ite3AuthRead(Par
72520 73 65 2a 2c 45 78 70 72 2a 2c 53 63 68 65 6d 61  se*,Expr*,Schema
72530 2a 2c 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c  *,SrcList*);.SQL
72540 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
72550 74 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  t sqlite3AuthChe
72560 63 6b 28 50 61 72 73 65 2a 2c 69 6e 74 2c 20 63  ck(Parse*,int, c
72570 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73  onst char*, cons
72580 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63  t char*, const c
72590 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
725a0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
725b0 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
725c0 75 73 68 28 50 61 72 73 65 2a 2c 20 41 75 74 68  ush(Parse*, Auth
725d0 43 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20  Context*, const 
725e0 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  char*);.SQLITE_P
725f0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
72600 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
72610 50 6f 70 28 41 75 74 68 43 6f 6e 74 65 78 74 2a  Pop(AuthContext*
72620 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
72630 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 41  E   int sqlite3A
72640 75 74 68 52 65 61 64 43 6f 6c 28 50 61 72 73 65  uthReadCol(Parse
72650 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  *, const char *,
72660 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69   const char *, i
72670 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  nt);.#else.# def
72680 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 52  ine sqlite3AuthR
72690 65 61 64 28 61 2c 62 2c 63 2c 64 29 0a 23 20 64  ead(a,b,c,d).# d
726a0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74  efine sqlite3Aut
726b0 68 43 68 65 63 6b 28 61 2c 62 2c 63 2c 64 2c 65  hCheck(a,b,c,d,e
726c0 29 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 23  )    SQLITE_OK.#
726d0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41   define sqlite3A
726e0 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28 61  uthContextPush(a
726f0 2c 62 2c 63 29 0a 23 20 64 65 66 69 6e 65 20 73  ,b,c).# define s
72700 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78  qlite3AuthContex
72710 74 50 6f 70 28 61 29 20 20 28 28 76 6f 69 64 29  tPop(a)  ((void)
72720 28 61 29 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49  (a)).#endif.SQLI
72730 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
72740 73 71 6c 69 74 65 33 41 74 74 61 63 68 28 50 61  sqlite3Attach(Pa
72750 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70  rse*, Expr*, Exp
72760 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49  r*, Expr*);.SQLI
72770 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
72780 73 71 6c 69 74 65 33 44 65 74 61 63 68 28 50 61  sqlite3Detach(Pa
72790 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  rse*, Expr*);.SQ
727a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
727b0 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63   sqlite3BtreeFac
727c0 74 6f 72 79 28 63 6f 6e 73 74 20 73 71 6c 69 74  tory(const sqlit
727d0 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68  e3 *db, const ch
727e0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20  ar *zFilename,. 
727f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72800 20 20 20 20 20 20 69 6e 74 20 6f 6d 69 74 4a 6f        int omitJo
72810 75 72 6e 61 6c 2c 20 69 6e 74 20 6e 43 61 63 68  urnal, int nCach
72820 65 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20 42 74  e, int flags, Bt
72830 72 65 65 20 2a 2a 70 70 42 74 72 65 65 29 3b 0a  ree **ppBtree);.
72840 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
72850 6e 74 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69  nt sqlite3FixIni
72860 74 28 44 62 46 69 78 65 72 2a 2c 20 50 61 72 73  t(DbFixer*, Pars
72870 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63  e*, int, const c
72880 68 61 72 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65  har*, const Toke
72890 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
728a0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
728b0 69 78 53 72 63 4c 69 73 74 28 44 62 46 69 78 65  ixSrcList(DbFixe
728c0 72 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53  r*, SrcList*);.S
728d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
728e0 74 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65  t sqlite3FixSele
728f0 63 74 28 44 62 46 69 78 65 72 2a 2c 20 53 65 6c  ct(DbFixer*, Sel
72900 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ect*);.SQLITE_PR
72910 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
72920 33 46 69 78 45 78 70 72 28 44 62 46 69 78 65 72  3FixExpr(DbFixer
72930 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  *, Expr*);.SQLIT
72940 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
72950 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69 73 74  lite3FixExprList
72960 28 44 62 46 69 78 65 72 2a 2c 20 45 78 70 72 4c  (DbFixer*, ExprL
72970 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ist*);.SQLITE_PR
72980 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
72990 33 46 69 78 54 72 69 67 67 65 72 53 74 65 70 28  3FixTriggerStep(
729a0 44 62 46 69 78 65 72 2a 2c 20 54 72 69 67 67 65  DbFixer*, Trigge
729b0 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54 45 5f  rStep*);.SQLITE_
729c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
729d0 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 20 63 68  te3AtoF(const ch
729e0 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 2a 29 3b  ar *z, double*);
729f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72a00 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 49 6e  int sqlite3GetIn
72a10 74 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  t32(const char *
72a20 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  , int*);.SQLITE_
72a30 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
72a40 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28  te3FitsIn64Bits(
72a50 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e  const char *, in
72a60 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
72a70 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74  TE int sqlite3Ut
72a80 66 31 36 42 79 74 65 4c 65 6e 28 63 6f 6e 73 74  f16ByteLen(const
72a90 20 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e   void *pData, in
72aa0 74 20 6e 43 68 61 72 29 3b 0a 53 51 4c 49 54 45  t nChar);.SQLITE
72ab0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
72ac0 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28  ite3Utf8CharLen(
72ad0 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
72ae0 61 2c 20 69 6e 74 20 6e 42 79 74 65 29 3b 0a 53  a, int nByte);.S
72af0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
72b00 74 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  t sqlite3Utf8Rea
72b10 64 28 63 6f 6e 73 74 20 75 38 2a 2c 20 63 6f 6e  d(const u8*, con
72b20 73 74 20 75 38 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  st u8**);../*.**
72b30 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 72 65 61   Routines to rea
72b40 64 20 61 6e 64 20 77 72 69 74 65 20 76 61 72 69  d and write vari
72b50 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65  able-length inte
72b60 67 65 72 73 2e 20 20 54 68 65 73 65 20 75 73 65  gers.  These use
72b70 64 20 74 6f 0a 2a 2a 20 62 65 20 64 65 66 69 6e  d to.** be defin
72b80 65 64 20 6c 6f 63 61 6c 6c 79 2c 20 62 75 74 20  ed locally, but 
72b90 6e 6f 77 20 77 65 20 75 73 65 20 74 68 65 20 76  now we use the v
72ba0 61 72 69 6e 74 20 72 6f 75 74 69 6e 65 73 20 69  arint routines i
72bb0 6e 20 74 68 65 20 75 74 69 6c 2e 63 0a 2a 2a 20  n the util.c.** 
72bc0 66 69 6c 65 2e 20 20 43 6f 64 65 20 73 68 6f 75  file.  Code shou
72bd0 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43 52 4f  ld use the MACRO
72be0 20 66 6f 72 6d 73 20 62 65 6c 6f 77 2c 20 61 73   forms below, as
72bf0 20 74 68 65 20 56 61 72 69 6e 74 33 32 20 76 65   the Varint32 ve
72c00 72 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 63 6f  rsions.** are co
72c10 64 65 64 20 74 6f 20 61 73 73 75 6d 65 20 74 68  ded to assume th
72c20 65 20 73 69 6e 67 6c 65 20 62 79 74 65 20 63 61  e single byte ca
72c30 73 65 20 69 73 20 61 6c 72 65 61 64 79 20 68 61  se is already ha
72c40 6e 64 6c 65 64 20 28 77 68 69 63 68 20 0a 2a 2a  ndled (which .**
72c50 20 74 68 65 20 4d 41 43 52 4f 20 66 6f 72 6d 20   the MACRO form 
72c60 64 6f 65 73 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  does)..*/.SQLITE
72c70 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
72c80 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 75 6e  ite3PutVarint(un
72c90 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 36  signed char*, u6
72ca0 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  4);.SQLITE_PRIVA
72cb0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75  TE int sqlite3Pu
72cc0 74 56 61 72 69 6e 74 33 32 28 75 6e 73 69 67 6e  tVarint32(unsign
72cd0 65 64 20 63 68 61 72 2a 2c 20 75 33 32 29 3b 0a  ed char*, u32);.
72ce0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75  SQLITE_PRIVATE u
72cf0 38 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69  8 sqlite3GetVari
72d00 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  nt(const unsigne
72d10 64 20 63 68 61 72 20 2a 2c 20 75 36 34 20 2a 29  d char *, u64 *)
72d20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
72d30 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61   u8 sqlite3GetVa
72d40 72 69 6e 74 33 32 28 63 6f 6e 73 74 20 75 6e 73  rint32(const uns
72d50 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 75 33  igned char *, u3
72d60 32 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  2 *);.SQLITE_PRI
72d70 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
72d80 56 61 72 69 6e 74 4c 65 6e 28 75 36 34 20 76 29  VarintLen(u64 v)
72d90 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61  ;../*.** The hea
72da0 64 65 72 20 6f 66 20 61 20 72 65 63 6f 72 64 20  der of a record 
72db0 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73 65  consists of a se
72dc0 71 75 65 6e 63 65 20 76 61 72 69 61 62 6c 65 2d  quence variable-
72dd0 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e  length integers.
72de0 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65  .** These intege
72df0 72 73 20 61 72 65 20 61 6c 6d 6f 73 74 20 61 6c  rs are almost al
72e00 77 61 79 73 20 73 6d 61 6c 6c 20 61 6e 64 20 61  ways small and a
72e10 72 65 20 65 6e 63 6f 64 65 64 20 61 73 20 61 20  re encoded as a 
72e20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a 2a 20  single byte..** 
72e30 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61  The following ma
72e40 63 72 6f 73 20 74 61 6b 65 20 61 64 76 61 6e 74  cros take advant
72e50 61 67 65 20 74 68 69 73 20 66 61 63 74 20 74 6f  age this fact to
72e60 20 70 72 6f 76 69 64 65 20 61 20 66 61 73 74 20   provide a fast 
72e70 65 6e 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 64 65  encode.** and de
72e80 63 6f 64 65 20 6f 66 20 74 68 65 20 69 6e 74 65  code of the inte
72e90 67 65 72 73 20 69 6e 20 61 20 72 65 63 6f 72 64  gers in a record
72ea0 20 68 65 61 64 65 72 2e 20 20 49 74 20 69 73 20   header.  It is 
72eb0 66 61 73 74 65 72 20 66 6f 72 20 74 68 65 20 63  faster for the c
72ec0 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68  ommon.** case wh
72ed0 65 72 65 20 74 68 65 20 69 6e 74 65 67 65 72 20  ere the integer 
72ee0 69 73 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65  is a single byte
72ef0 2e 20 20 49 74 20 69 73 20 61 20 6c 69 74 74 6c  .  It is a littl
72f00 65 20 73 6c 6f 77 65 72 20 77 68 65 6e 20 74 68  e slower when th
72f10 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 69 73 20  e.** integer is 
72f20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 79 74 65  two or more byte
72f30 73 2e 20 20 42 75 74 20 6f 76 65 72 61 6c 6c 20  s.  But overall 
72f40 69 74 20 69 73 20 66 61 73 74 65 72 2e 0a 2a 2a  it is faster..**
72f50 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
72f60 67 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72  g expressions ar
72f70 65 20 65 71 75 69 76 61 6c 65 6e 74 3a 0a 2a 2a  e equivalent:.**
72f80 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 71 6c 69  .**     x = sqli
72f90 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 20  te3GetVarint32( 
72fa0 41 2c 20 26 42 20 29 3b 0a 2a 2a 20 20 20 20 20  A, &B );.**     
72fb0 78 20 3d 20 73 71 6c 69 74 65 33 50 75 74 56 61  x = sqlite3PutVa
72fc0 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29 3b 0a  rint32( A, B );.
72fd0 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 67 65  **.**     x = ge
72fe0 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20  tVarint32( A, B 
72ff0 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d 20 70 75  );.**     x = pu
73000 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20  tVarint32( A, B 
73010 29 3b 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65  );.**.*/.#define
73020 20 67 65 74 56 61 72 69 6e 74 33 32 28 41 2c 42   getVarint32(A,B
73030 29 20 20 28 75 38 29 28 28 2a 28 41 29 3c 28 75  )  (u8)((*(A)<(u
73040 38 29 30 78 38 30 29 20 3f 20 28 28 42 29 20 3d  8)0x80) ? ((B) =
73050 20 28 75 33 32 29 2a 28 41 29 29 2c 31 20 3a 20   (u32)*(A)),1 : 
73060 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74  sqlite3GetVarint
73070 33 32 28 28 41 29 2c 20 28 75 33 32 20 2a 29 26  32((A), (u32 *)&
73080 28 42 29 29 29 0a 23 64 65 66 69 6e 65 20 70 75  (B))).#define pu
73090 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29 20 20  tVarint32(A,B)  
730a0 28 75 38 29 28 28 28 75 33 32 29 28 42 29 3c 28  (u8)(((u32)(B)<(
730b0 75 33 32 29 30 78 38 30 29 20 3f 20 28 2a 28 41  u32)0x80) ? (*(A
730c0 29 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  ) = (unsigned ch
730d0 61 72 29 28 42 29 29 2c 31 20 3a 20 73 71 6c 69  ar)(B)),1 : sqli
730e0 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 28  te3PutVarint32((
730f0 41 29 2c 20 28 42 29 29 29 0a 23 64 65 66 69 6e  A), (B))).#defin
73100 65 20 67 65 74 56 61 72 69 6e 74 20 20 20 20 73  e getVarint    s
73110 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 0a  qlite3GetVarint.
73120 23 64 65 66 69 6e 65 20 70 75 74 56 61 72 69 6e  #define putVarin
73130 74 20 20 20 20 73 71 6c 69 74 65 33 50 75 74 56  t    sqlite3PutV
73140 61 72 69 6e 74 0a 0a 0a 53 51 4c 49 54 45 5f 50  arint...SQLITE_P
73150 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
73160 72 20 2a 73 71 6c 69 74 65 33 49 6e 64 65 78 41  r *sqlite3IndexA
73170 66 66 69 6e 69 74 79 53 74 72 28 56 64 62 65 20  ffinityStr(Vdbe 
73180 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c  *, Index *);.SQL
73190 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
731a0 20 73 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66   sqlite3TableAff
731b0 69 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a 2c  inityStr(Vdbe *,
731c0 20 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54   Table *);.SQLIT
731d0 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73  E_PRIVATE char s
731e0 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66  qlite3CompareAff
731f0 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70  inity(Expr *pExp
73200 72 2c 20 63 68 61 72 20 61 66 66 32 29 3b 0a 53  r, char aff2);.S
73210 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
73220 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66  t sqlite3IndexAf
73230 66 69 6e 69 74 79 4f 6b 28 45 78 70 72 20 2a 70  finityOk(Expr *p
73240 45 78 70 72 2c 20 63 68 61 72 20 69 64 78 5f 61  Expr, char idx_a
73250 66 66 69 6e 69 74 79 29 3b 0a 53 51 4c 49 54 45  ffinity);.SQLITE
73260 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71  _PRIVATE char sq
73270 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74  lite3ExprAffinit
73280 79 28 45 78 70 72 20 2a 70 45 78 70 72 29 3b 0a  y(Expr *pExpr);.
73290 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
732a0 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34  nt sqlite3Atoi64
732b0 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 36  (const char*, i6
732c0 34 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  4*);.SQLITE_PRIV
732d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
732e0 45 72 72 6f 72 28 73 71 6c 69 74 65 33 2a 2c 20  Error(sqlite3*, 
732f0 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  int, const char*
73300 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,...);.SQLITE_PR
73310 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
73320 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c  te3HexToBlob(sql
73330 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61  ite3*, const cha
73340 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 3b 0a 53 51  r *z, int n);.SQ
73350 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
73360 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e   sqlite3TwoPartN
73370 61 6d 65 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b  ame(Parse *, Tok
73380 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c 20 54  en *, Token *, T
73390 6f 6b 65 6e 20 2a 2a 29 3b 0a 53 51 4c 49 54 45  oken **);.SQLITE
733a0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63  _PRIVATE const c
733b0 68 61 72 20 2a 73 71 6c 69 74 65 33 45 72 72 53  har *sqlite3ErrS
733c0 74 72 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  tr(int);.SQLITE_
733d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
733e0 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 50 61  te3ReadSchema(Pa
733f0 72 73 65 20 2a 70 50 61 72 73 65 29 3b 0a 53 51  rse *pParse);.SQ
73400 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c  LITE_PRIVATE Col
73410 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 46 69 6e  lSeq *sqlite3Fin
73420 64 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33  dCollSeq(sqlite3
73430 2a 2c 75 38 20 65 6e 63 2c 20 63 6f 6e 73 74 20  *,u8 enc, const 
73440 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  char*,int);.SQLI
73450 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53  TE_PRIVATE CollS
73460 65 71 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74  eq *sqlite3Locat
73470 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a  eCollSeq(Parse *
73480 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68  pParse, const ch
73490 61 72 2a 7a 4e 61 6d 65 29 3b 0a 53 51 4c 49 54  ar*zName);.SQLIT
734a0 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65  E_PRIVATE CollSe
734b0 71 20 2a 73 71 6c 69 74 65 33 45 78 70 72 43 6f  q *sqlite3ExprCo
734c0 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61  llSeq(Parse *pPa
734d0 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72  rse, Expr *pExpr
734e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
734f0 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45  E Expr *sqlite3E
73500 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73 65  xprSetColl(Parse
73510 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
73520 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49  , Token *);.SQLI
73530 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
73540 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53  qlite3CheckCollS
73550 65 71 28 50 61 72 73 65 20 2a 2c 20 43 6f 6c 6c  eq(Parse *, Coll
73560 53 65 71 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Seq *);.SQLITE_P
73570 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
73580 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d  e3CheckObjectNam
73590 65 28 50 61 72 73 65 20 2a 2c 20 63 6f 6e 73 74  e(Parse *, const
735a0 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45   char *);.SQLITE
735b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
735c0 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e  lite3VdbeSetChan
735d0 67 65 73 28 73 71 6c 69 74 65 33 20 2a 2c 20 69  ges(sqlite3 *, i
735e0 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  nt);..SQLITE_PRI
735f0 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20  VATE const void 
73600 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78  *sqlite3ValueTex
73610 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t(sqlite3_value*
73620 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52  , u8);.SQLITE_PR
73630 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
73640 33 56 61 6c 75 65 42 79 74 65 73 28 73 71 6c 69  3ValueBytes(sqli
73650 74 65 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29 3b  te3_value*, u8);
73660 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73670 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75  void sqlite3Valu
73680 65 53 65 74 53 74 72 28 73 71 6c 69 74 65 33 5f  eSetStr(sqlite3_
73690 76 61 6c 75 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e  value*, int, con
736a0 73 74 20 76 6f 69 64 20 2a 2c 75 38 2c 20 0a 20  st void *,u8, . 
736b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
736c0 20 20 20 20 20 20 20 76 6f 69 64 28 2a 29 28 76         void(*)(v
736d0 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50  oid*));.SQLITE_P
736e0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
736f0 74 65 33 56 61 6c 75 65 46 72 65 65 28 73 71 6c  te3ValueFree(sql
73700 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51  ite3_value*);.SQ
73710 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
73720 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69  ite3_value *sqli
73730 74 65 33 56 61 6c 75 65 4e 65 77 28 73 71 6c 69  te3ValueNew(sqli
73740 74 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  te3 *);.SQLITE_P
73750 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
73760 69 74 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c  ite3Utf16to8(sql
73770 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 76 6f  ite3 *, const vo
73780 69 64 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 64 65  id*, int);.#ifde
73790 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
737a0 53 54 41 54 32 0a 53 51 4c 49 54 45 5f 50 52 49  STAT2.SQLITE_PRI
737b0 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
737c0 65 33 55 74 66 38 74 6f 31 36 28 73 71 6c 69 74  e3Utf8to16(sqlit
737d0 65 33 20 2a 2c 20 75 38 2c 20 63 68 61 72 20 2a  e3 *, u8, char *
737e0 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 23  , int, int *);.#
737f0 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49  endif.SQLITE_PRI
73800 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
73810 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 73 71  ValueFromExpr(sq
73820 6c 69 74 65 33 20 2a 2c 20 45 78 70 72 20 2a 2c  lite3 *, Expr *,
73830 20 75 38 2c 20 75 38 2c 20 73 71 6c 69 74 65 33   u8, u8, sqlite3
73840 5f 76 61 6c 75 65 20 2a 2a 29 3b 0a 53 51 4c 49  _value **);.SQLI
73850 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
73860 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c  sqlite3ValueAppl
73870 79 41 66 66 69 6e 69 74 79 28 73 71 6c 69 74 65  yAffinity(sqlite
73880 33 5f 76 61 6c 75 65 20 2a 2c 20 75 38 2c 20 75  3_value *, u8, u
73890 38 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  8);.#ifndef SQLI
738a0 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a  TE_AMALGAMATION.
738b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
738c0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
738d0 61 72 20 73 71 6c 69 74 65 33 55 70 70 65 72 54  ar sqlite3UpperT
738e0 6f 4c 6f 77 65 72 5b 5d 3b 0a 53 51 4c 49 54 45  oLower[];.SQLITE
738f0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75  _PRIVATE const u
73900 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c  nsigned char sql
73910 69 74 65 33 43 74 79 70 65 4d 61 70 5b 5d 3b 0a  ite3CtypeMap[];.
73920 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
73930 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74  QLITE_WSD struct
73940 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 73   Sqlite3Config s
73950 71 6c 69 74 65 33 43 6f 6e 66 69 67 3b 0a 53 51  qlite3Config;.SQ
73960 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c  LITE_PRIVATE SQL
73970 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 48  ITE_WSD FuncDefH
73980 61 73 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  ash sqlite3Globa
73990 6c 46 75 6e 63 74 69 6f 6e 73 3b 0a 53 51 4c 49  lFunctions;.SQLI
739a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
739b0 71 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74  qlite3PendingByt
739c0 65 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45  e;.#endif.SQLITE
739d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
739e0 6c 69 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76  lite3RootPageMov
739f0 65 64 28 44 62 2a 2c 20 69 6e 74 2c 20 69 6e 74  ed(Db*, int, int
73a00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73a10 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
73a20 69 6e 64 65 78 28 50 61 72 73 65 2a 2c 20 54 6f  index(Parse*, To
73a30 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53  ken*, Token*);.S
73a40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
73a50 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46  id sqlite3AlterF
73a60 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33  unctions(sqlite3
73a70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
73a80 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
73a90 6c 74 65 72 52 65 6e 61 6d 65 54 61 62 6c 65 28  lterRenameTable(
73aa0 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a  Parse*, SrcList*
73ab0 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  , Token*);.SQLIT
73ac0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
73ad0 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 63 6f  lite3GetToken(co
73ae0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
73af0 72 20 2a 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c  r *, int *);.SQL
73b00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
73b10 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
73b20 72 73 65 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73  rse(Parse*, cons
73b30 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53  t char*, ...);.S
73b40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
73b50 69 64 20 73 71 6c 69 74 65 33 45 78 70 69 72 65  id sqlite3Expire
73b60 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
73b70 74 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51  ts(sqlite3*);.SQ
73b80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
73b90 64 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62  d sqlite3CodeSub
73ba0 73 65 6c 65 63 74 28 50 61 72 73 65 20 2a 2c 20  select(Parse *, 
73bb0 45 78 70 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74  Expr *, int, int
73bc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73bd0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65  E void sqlite3Se
73be0 6c 65 63 74 50 72 65 70 28 50 61 72 73 65 2a 2c  lectPrep(Parse*,
73bf0 20 53 65 6c 65 63 74 2a 2c 20 4e 61 6d 65 43 6f   Select*, NameCo
73c00 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  ntext*);.SQLITE_
73c10 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
73c20 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
73c30 6d 65 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a  mes(NameContext*
73c40 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
73c50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
73c60 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c 65  lite3ResolveSele
73c70 63 74 4e 61 6d 65 73 28 50 61 72 73 65 2a 2c 20  ctNames(Parse*, 
73c80 53 65 6c 65 63 74 2a 2c 20 4e 61 6d 65 43 6f 6e  Select*, NameCon
73c90 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  text*);.SQLITE_P
73ca0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
73cb0 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72  e3ResolveOrderGr
73cc0 6f 75 70 42 79 28 50 61 72 73 65 2a 2c 20 53 65  oupBy(Parse*, Se
73cd0 6c 65 63 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a  lect*, ExprList*
73ce0 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  , const char*);.
73cf0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
73d00 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d  oid sqlite3Colum
73d10 6e 44 65 66 61 75 6c 74 28 56 64 62 65 20 2a 2c  nDefault(Vdbe *,
73d20 20 54 61 62 6c 65 20 2a 2c 20 69 6e 74 2c 20 69   Table *, int, i
73d30 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
73d40 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
73d50 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f  AlterFinishAddCo
73d60 6c 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20 54 6f  lumn(Parse *, To
73d70 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ken *);.SQLITE_P
73d80 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
73d90 74 65 33 41 6c 74 65 72 42 65 67 69 6e 41 64 64  te3AlterBeginAdd
73da0 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20  Column(Parse *, 
73db0 53 72 63 4c 69 73 74 20 2a 29 3b 0a 53 51 4c 49  SrcList *);.SQLI
73dc0 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53  TE_PRIVATE CollS
73dd0 65 71 20 2a 73 71 6c 69 74 65 33 47 65 74 43 6f  eq *sqlite3GetCo
73de0 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 2a 2c 20  llSeq(sqlite3*, 
73df0 75 38 2c 20 43 6f 6c 6c 53 65 71 20 2a 2c 20 63  u8, CollSeq *, c
73e00 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
73e10 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
73e20 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79   sqlite3Affinity
73e30 54 79 70 65 28 63 6f 6e 73 74 20 63 68 61 72 2a  Type(const char*
73e40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73e50 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6e  E void sqlite3An
73e60 61 6c 79 7a 65 28 50 61 72 73 65 2a 2c 20 54 6f  alyze(Parse*, To
73e70 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53  ken*, Token*);.S
73e80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
73e90 74 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42  t sqlite3InvokeB
73ea0 75 73 79 48 61 6e 64 6c 65 72 28 42 75 73 79 48  usyHandler(BusyH
73eb0 61 6e 64 6c 65 72 2a 29 3b 0a 53 51 4c 49 54 45  andler*);.SQLITE
73ec0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
73ed0 69 74 65 33 46 69 6e 64 44 62 28 73 71 6c 69 74  ite3FindDb(sqlit
73ee0 65 33 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  e3*, Token*);.SQ
73ef0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
73f00 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61   sqlite3FindDbNa
73f10 6d 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f  me(sqlite3 *, co
73f20 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c  nst char *);.SQL
73f30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
73f40 73 71 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c  sqlite3AnalysisL
73f50 6f 61 64 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74  oad(sqlite3*,int
73f60 20 69 44 42 29 3b 0a 53 51 4c 49 54 45 5f 50 52   iDB);.SQLITE_PR
73f70 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
73f80 65 33 44 65 6c 65 74 65 49 6e 64 65 78 53 61 6d  e3DeleteIndexSam
73f90 70 6c 65 73 28 49 6e 64 65 78 2a 29 3b 0a 53 51  ples(Index*);.SQ
73fa0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
73fb0 64 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74  d sqlite3Default
73fc0 52 6f 77 45 73 74 28 49 6e 64 65 78 2a 29 3b 0a  RowEst(Index*);.
73fd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
73fe0 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73  oid sqlite3Regis
73ff0 74 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73  terLikeFunctions
74000 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b  (sqlite3*, int);
74010 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74020 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4c 69 6b  int sqlite3IsLik
74030 65 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65  eFunction(sqlite
74040 33 2a 2c 45 78 70 72 2a 2c 69 6e 74 2a 2c 63 68  3*,Expr*,int*,ch
74050 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ar*);.SQLITE_PRI
74060 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
74070 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d  3MinimumFileForm
74080 61 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20  at(Parse*, int, 
74090 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
740a0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
740b0 33 53 63 68 65 6d 61 46 72 65 65 28 76 6f 69 64  3SchemaFree(void
740c0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
740d0 41 54 45 20 53 63 68 65 6d 61 20 2a 73 71 6c 69  ATE Schema *sqli
740e0 74 65 33 53 63 68 65 6d 61 47 65 74 28 73 71 6c  te3SchemaGet(sql
740f0 69 74 65 33 20 2a 2c 20 42 74 72 65 65 20 2a 29  ite3 *, Btree *)
74100 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74110 20 69 6e 74 20 73 71 6c 69 74 65 33 53 63 68 65   int sqlite3Sche
74120 6d 61 54 6f 49 6e 64 65 78 28 73 71 6c 69 74 65  maToIndex(sqlite
74130 33 20 2a 64 62 2c 20 53 63 68 65 6d 61 20 2a 29  3 *db, Schema *)
74140 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74150 20 4b 65 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65   KeyInfo *sqlite
74160 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 50 61  3IndexKeyinfo(Pa
74170 72 73 65 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b  rse *, Index *);
74180 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74190 69 6e 74 20 73 71 6c 69 74 65 33 43 72 65 61 74  int sqlite3Creat
741a0 65 46 75 6e 63 28 73 71 6c 69 74 65 33 20 2a 2c  eFunc(sqlite3 *,
741b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69   const char *, i
741c0 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64 20 2a 2c  nt, int, void *,
741d0 20 0a 20 20 76 6f 69 64 20 28 2a 29 28 73 71 6c   .  void (*)(sql
741e0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
741f0 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  t,sqlite3_value 
74200 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 29 28  **),.  void (*)(
74210 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
74220 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
74230 75 65 20 2a 2a 29 2c 20 76 6f 69 64 20 28 2a 29  ue **), void (*)
74240 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
74250 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  *));.SQLITE_PRIV
74260 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41  ATE int sqlite3A
74270 70 69 45 78 69 74 28 73 71 6c 69 74 65 33 20 2a  piExit(sqlite3 *
74280 64 62 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  db, int);.SQLITE
74290 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
742a0 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61  ite3OpenTempData
742b0 62 61 73 65 28 50 61 72 73 65 20 2a 29 3b 0a 0a  base(Parse *);..
742c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
742d0 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63  oid sqlite3StrAc
742e0 63 75 6d 49 6e 69 74 28 53 74 72 41 63 63 75 6d  cumInit(StrAccum
742f0 2a 2c 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 69  *, char*, int, i
74300 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
74310 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
74320 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 53  StrAccumAppend(S
74330 74 72 41 63 63 75 6d 2a 2c 63 6f 6e 73 74 20 63  trAccum*,const c
74340 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  har*,int);.SQLIT
74350 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
74360 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46  sqlite3StrAccumF
74370 69 6e 69 73 68 28 53 74 72 41 63 63 75 6d 2a 29  inish(StrAccum*)
74380 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74390 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72   void sqlite3Str
743a0 41 63 63 75 6d 52 65 73 65 74 28 53 74 72 41 63  AccumReset(StrAc
743b0 63 75 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  cum*);.SQLITE_PR
743c0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
743d0 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
743e0 28 53 65 6c 65 63 74 44 65 73 74 2a 2c 69 6e 74  (SelectDest*,int
743f0 2c 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50  ,int);..SQLITE_P
74400 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
74410 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74  te3BackupRestart
74420 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
74430 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
74440 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
74450 61 63 6b 75 70 55 70 64 61 74 65 28 73 71 6c 69  ackupUpdate(sqli
74460 74 65 33 5f 62 61 63 6b 75 70 20 2a 2c 20 50 67  te3_backup *, Pg
74470 6e 6f 2c 20 63 6f 6e 73 74 20 75 38 20 2a 29 3b  no, const u8 *);
74480 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65  ../*.** The inte
74490 72 66 61 63 65 20 74 6f 20 74 68 65 20 4c 45 4d  rface to the LEM
744a0 4f 4e 2d 67 65 6e 65 72 61 74 65 64 20 70 61 72  ON-generated par
744b0 73 65 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ser.*/.SQLITE_PR
744c0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
744d0 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 28 76  te3ParserAlloc(v
744e0 6f 69 64 2a 28 2a 29 28 73 69 7a 65 5f 74 29 29  oid*(*)(size_t))
744f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74500 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72   void sqlite3Par
74510 73 65 72 46 72 65 65 28 76 6f 69 64 2a 2c 20 76  serFree(void*, v
74520 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
74530 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
74540 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65  oid sqlite3Parse
74550 72 28 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 54 6f  r(void*, int, To
74560 6b 65 6e 2c 20 50 61 72 73 65 2a 29 3b 0a 23 69  ken, Parse*);.#i
74570 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53  fdef YYTRACKMAXS
74580 54 41 43 4b 44 45 50 54 48 0a 53 51 4c 49 54 45  TACKDEPTH.SQLITE
74590 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
745a0 71 6c 69 74 65 33 50 61 72 73 65 72 53 74 61 63  qlite3ParserStac
745b0 6b 50 65 61 6b 28 76 6f 69 64 2a 29 3b 0a 23 65  kPeak(void*);.#e
745c0 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49  ndif..SQLITE_PRI
745d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
745e0 33 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69  3AutoLoadExtensi
745f0 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23  ons(sqlite3*);.#
74600 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
74610 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f  IT_LOAD_EXTENSIO
74620 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  N.SQLITE_PRIVATE
74630 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43     void sqlite3C
74640 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 73  loseExtensions(s
74650 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a  qlite3*);.#else.
74660 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
74670 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28  CloseExtensions(
74680 58 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  X).#endif..#ifnd
74690 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
746a0 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49  HARED_CACHE.SQLI
746b0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
746c0 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f  d sqlite3TableLo
746d0 63 6b 28 50 61 72 73 65 20 2a 2c 20 69 6e 74 2c  ck(Parse *, int,
746e0 20 69 6e 74 2c 20 75 38 2c 20 63 6f 6e 73 74 20   int, u8, const 
746f0 63 68 61 72 20 2a 29 3b 0a 23 65 6c 73 65 0a 20  char *);.#else. 
74700 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
74710 54 61 62 6c 65 4c 6f 63 6b 28 76 2c 77 2c 78 2c  TableLock(v,w,x,
74720 79 2c 7a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66  y,z).#endif..#if
74730 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
74740 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
74750 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38   int sqlite3Utf8
74760 54 6f 38 28 75 6e 73 69 67 6e 65 64 20 63 68 61  To8(unsigned cha
74770 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  r*);.#endif..#if
74780 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
74790 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 20  VIRTUALTABLE.#  
747a0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74  define sqlite3Vt
747b0 61 62 43 6c 65 61 72 28 59 29 0a 23 20 20 64 65  abClear(Y).#  de
747c0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62  fine sqlite3Vtab
747d0 53 79 6e 63 28 58 2c 59 29 20 53 51 4c 49 54 45  Sync(X,Y) SQLITE
747e0 5f 4f 4b 0a 23 20 20 64 65 66 69 6e 65 20 73 71  _OK.#  define sq
747f0 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63  lite3VtabRollbac
74800 6b 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73  k(X).#  define s
74810 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74  qlite3VtabCommit
74820 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71  (X).#  define sq
74830 6c 69 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28  lite3VtabInSync(
74840 64 62 29 20 30 0a 23 20 20 64 65 66 69 6e 65 20  db) 0.#  define 
74850 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28  sqlite3VtabLock(
74860 58 29 20 0a 23 20 20 64 65 66 69 6e 65 20 73 71  X) .#  define sq
74870 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28  lite3VtabUnlock(
74880 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c  X).#  define sql
74890 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69  ite3VtabUnlockLi
748a0 73 74 28 58 29 0a 23 65 6c 73 65 0a 53 51 4c 49  st(X).#else.SQLI
748b0 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f  TE_PRIVATE    vo
748c0 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c  id sqlite3VtabCl
748d0 65 61 72 28 54 61 62 6c 65 2a 29 3b 0a 53 51 4c  ear(Table*);.SQL
748e0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69  ITE_PRIVATE    i
748f0 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 53 79  nt sqlite3VtabSy
74900 6e 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  nc(sqlite3 *db, 
74910 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45  char **);.SQLITE
74920 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e 74 20  _PRIVATE    int 
74930 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62  sqlite3VtabRollb
74940 61 63 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ack(sqlite3 *db)
74950 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74960 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56      int sqlite3V
74970 74 61 62 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65  tabCommit(sqlite
74980 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 50  3 *db);.SQLITE_P
74990 52 49 56 41 54 45 20 20 20 20 76 6f 69 64 20 73  RIVATE    void s
749a0 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 56  qlite3VtabLock(V
749b0 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45  Table *);.SQLITE
749c0 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f 69 64  _PRIVATE    void
749d0 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f   sqlite3VtabUnlo
749e0 63 6b 28 56 54 61 62 6c 65 20 2a 29 3b 0a 53 51  ck(VTable *);.SQ
749f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20  LITE_PRIVATE    
74a00 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
74a10 55 6e 6c 6f 63 6b 4c 69 73 74 28 73 71 6c 69 74  UnlockList(sqlit
74a20 65 33 2a 29 3b 0a 23 20 20 64 65 66 69 6e 65 20  e3*);.#  define 
74a30 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53 79 6e  sqlite3VtabInSyn
74a40 63 28 64 62 29 20 28 28 64 62 29 2d 3e 6e 56 54  c(db) ((db)->nVT
74a50 72 61 6e 73 3e 30 20 26 26 20 28 64 62 29 2d 3e  rans>0 && (db)->
74a60 61 56 54 72 61 6e 73 3d 3d 30 29 0a 23 65 6e 64  aVTrans==0).#end
74a70 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  if.SQLITE_PRIVAT
74a80 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
74a90 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 50  abMakeWritable(P
74aa0 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a 53  arse*,Table*);.S
74ab0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
74ac0 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 42 65  id sqlite3VtabBe
74ad0 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 2a 2c  ginParse(Parse*,
74ae0 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c   Token*, Token*,
74af0 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
74b00 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
74b10 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73 68 50  lite3VtabFinishP
74b20 61 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b  arse(Parse*, Tok
74b30 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
74b40 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
74b50 33 56 74 61 62 41 72 67 49 6e 69 74 28 50 61 72  3VtabArgInit(Par
74b60 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  se*);.SQLITE_PRI
74b70 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
74b80 33 56 74 61 62 41 72 67 45 78 74 65 6e 64 28 50  3VtabArgExtend(P
74b90 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  arse*, Token*);.
74ba0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
74bb0 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61  nt sqlite3VtabCa
74bc0 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74 65 33  llCreate(sqlite3
74bd0 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68  *, int, const ch
74be0 61 72 20 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a  ar *, char **);.
74bf0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
74c00 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61  nt sqlite3VtabCa
74c10 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72 73 65 2a  llConnect(Parse*
74c20 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54  , Table*);.SQLIT
74c30 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
74c40 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44 65 73  lite3VtabCallDes
74c50 74 72 6f 79 28 73 71 6c 69 74 65 33 2a 2c 20 69  troy(sqlite3*, i
74c60 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  nt, const char *
74c70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
74c80 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61  E int sqlite3Vta
74c90 62 42 65 67 69 6e 28 73 71 6c 69 74 65 33 20 2a  bBegin(sqlite3 *
74ca0 2c 20 56 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c  , VTable *);.SQL
74cb0 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63  ITE_PRIVATE Func
74cc0 44 65 66 20 2a 73 71 6c 69 74 65 33 56 74 61 62  Def *sqlite3Vtab
74cd0 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e  OverloadFunction
74ce0 28 73 71 6c 69 74 65 33 20 2a 2c 46 75 6e 63 44  (sqlite3 *,FuncD
74cf0 65 66 2a 2c 20 69 6e 74 20 6e 41 72 67 2c 20 45  ef*, int nArg, E
74d00 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
74d10 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
74d20 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f  e3InvalidFunctio
74d30 6e 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  n(sqlite3_contex
74d40 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
74d50 61 6c 75 65 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f  alue**);.SQLITE_
74d60 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
74d70 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69  te3TransferBindi
74d80 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ngs(sqlite3_stmt
74d90 20 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74   *, sqlite3_stmt
74da0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
74db0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
74dc0 65 70 72 65 70 61 72 65 28 56 64 62 65 2a 29 3b  eprepare(Vdbe*);
74dd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74de0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
74df0 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28  ListCheckLength(
74e00 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74  Parse*, ExprList
74e10 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  *, const char*);
74e20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74e30 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33  CollSeq *sqlite3
74e40 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c  BinaryCompareCol
74e50 6c 53 65 71 28 50 61 72 73 65 20 2a 2c 20 45 78  lSeq(Parse *, Ex
74e60 70 72 20 2a 2c 20 45 78 70 72 20 2a 29 3b 0a 53  pr *, Expr *);.S
74e70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
74e80 74 20 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d  t sqlite3TempInM
74e90 65 6d 6f 72 79 28 63 6f 6e 73 74 20 73 71 6c 69  emory(const sqli
74ea0 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  te3*);.SQLITE_PR
74eb0 49 56 41 54 45 20 56 54 61 62 6c 65 20 2a 73 71  IVATE VTable *sq
74ec0 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 73  lite3GetVTable(s
74ed0 71 6c 69 74 65 33 2a 2c 20 54 61 62 6c 65 2a 29  qlite3*, Table*)
74ee0 3b 0a 0a 2f 2a 20 44 65 63 6c 61 72 61 74 69 6f  ;../* Declaratio
74ef0 6e 73 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 73  ns for functions
74f00 20 69 6e 20 66 6b 65 79 2e 63 2e 20 41 6c 6c 20   in fkey.c. All 
74f10 6f 66 20 74 68 65 73 65 20 61 72 65 20 72 65 70  of these are rep
74f20 6c 61 63 65 64 20 62 79 0a 2a 2a 20 6e 6f 2d 6f  laced by.** no-o
74f30 70 20 6d 61 63 72 6f 73 20 69 66 20 4f 4d 49 54  p macros if OMIT
74f40 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 20 69 73 20  _FOREIGN_KEY is 
74f50 64 65 66 69 6e 65 64 2e 20 49 6e 20 74 68 69 73  defined. In this
74f60 20 63 61 73 65 20 6e 6f 20 66 6f 72 65 69 67 6e   case no foreign
74f70 0a 2a 2a 20 6b 65 79 20 66 75 6e 63 74 69 6f 6e  .** key function
74f80 61 6c 69 74 79 20 69 73 20 61 76 61 69 6c 61 62  ality is availab
74f90 6c 65 2e 20 49 66 20 4f 4d 49 54 5f 54 52 49 47  le. If OMIT_TRIG
74fa0 47 45 52 20 69 73 20 64 65 66 69 6e 65 64 20 62  GER is defined b
74fb0 75 74 0a 2a 2a 20 4f 4d 49 54 5f 46 4f 52 45 49  ut.** OMIT_FOREI
74fc0 47 4e 5f 4b 45 59 20 69 73 20 6e 6f 74 2c 20 6f  GN_KEY is not, o
74fd0 6e 6c 79 20 73 6f 6d 65 20 6f 66 20 74 68 65 20  nly some of the 
74fe0 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f  functions are no
74ff0 2d 6f 70 65 64 2e 20 49 6e 0a 2a 2a 20 74 68 69  -oped. In.** thi
75000 73 20 63 61 73 65 20 66 6f 72 65 69 67 6e 20 6b  s case foreign k
75010 65 79 73 20 61 72 65 20 70 61 72 73 65 64 2c 20  eys are parsed, 
75020 62 75 74 20 6e 6f 20 6f 74 68 65 72 20 66 75 6e  but no other fun
75030 63 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 0a 2a  ctionality is .*
75040 2a 20 70 72 6f 76 69 64 65 64 20 28 65 6e 66 6f  * provided (enfo
75050 72 63 65 6d 65 6e 74 20 6f 66 20 46 4b 20 63 6f  rcement of FK co
75060 6e 73 74 72 61 69 6e 74 73 20 72 65 71 75 69 72  nstraints requir
75070 65 73 20 74 68 65 20 74 72 69 67 67 65 72 73 20  es the triggers 
75080 73 75 62 2d 73 79 73 74 65 6d 29 2e 0a 2a 2f 0a  sub-system)..*/.
75090 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
750a0 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
750b0 5f 4b 45 59 29 20 26 26 20 21 64 65 66 69 6e 65  _KEY) && !define
750c0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
750d0 49 47 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52  IGGER).SQLITE_PR
750e0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
750f0 69 74 65 33 46 6b 43 68 65 63 6b 28 50 61 72 73  ite3FkCheck(Pars
75100 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c  e*, Table*, int,
75110 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
75120 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
75130 69 74 65 33 46 6b 44 72 6f 70 54 61 62 6c 65 28  ite3FkDropTable(
75140 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 20  Parse*, SrcList 
75150 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49  *, Table*);.SQLI
75160 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
75170 64 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69 6f  d sqlite3FkActio
75180 6e 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65  ns(Parse*, Table
75190 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e  *, ExprList*, in
751a0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
751b0 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33  TE   int sqlite3
751c0 46 6b 52 65 71 75 69 72 65 64 28 50 61 72 73 65  FkRequired(Parse
751d0 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2a 2c  *, Table*, int*,
751e0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
751f0 49 56 41 54 45 20 20 20 75 33 32 20 73 71 6c 69  IVATE   u32 sqli
75200 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 50 61 72  te3FkOldmask(Par
75210 73 65 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51  se*, Table*);.SQ
75220 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 46  LITE_PRIVATE   F
75230 4b 65 79 20 2a 73 71 6c 69 74 65 33 46 6b 52 65  Key *sqlite3FkRe
75240 66 65 72 65 6e 63 65 73 28 54 61 62 6c 65 20 2a  ferences(Table *
75250 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69  );.#else.  #defi
75260 6e 65 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69  ne sqlite3FkActi
75270 6f 6e 73 28 61 2c 62 2c 63 2c 64 29 0a 20 20 23  ons(a,b,c,d).  #
75280 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 6b  define sqlite3Fk
75290 43 68 65 63 6b 28 61 2c 62 2c 63 2c 64 29 0a 20  Check(a,b,c,d). 
752a0 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
752b0 46 6b 44 72 6f 70 54 61 62 6c 65 28 61 2c 62 2c  FkDropTable(a,b,
752c0 63 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c  c).  #define sql
752d0 69 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 61 2c  ite3FkOldmask(a,
752e0 62 29 20 20 20 20 20 20 30 0a 20 20 23 64 65 66  b)      0.  #def
752f0 69 6e 65 20 73 71 6c 69 74 65 33 46 6b 52 65 71  ine sqlite3FkReq
75300 75 69 72 65 64 28 61 2c 62 2c 63 2c 64 29 20 30  uired(a,b,c,d) 0
75310 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
75320 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45  SQLITE_OMIT_FORE
75330 49 47 4e 5f 4b 45 59 0a 53 51 4c 49 54 45 5f 50  IGN_KEY.SQLITE_P
75340 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
75350 6c 69 74 65 33 46 6b 44 65 6c 65 74 65 28 54 61  lite3FkDelete(Ta
75360 62 6c 65 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23  ble*);.#else.  #
75370 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 6b  define sqlite3Fk
75380 44 65 6c 65 74 65 28 61 29 0a 23 65 6e 64 69 66  Delete(a).#endif
75390 0a 0a 0a 2f 2a 0a 2a 2a 20 41 76 61 69 6c 61 62  .../*.** Availab
753a0 6c 65 20 66 61 75 6c 74 20 69 6e 6a 65 63 74 6f  le fault injecto
753b0 72 73 2e 20 20 53 68 6f 75 6c 64 20 62 65 20 6e  rs.  Should be n
753c0 75 6d 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e  umbered beginnin
753d0 67 20 77 69 74 68 20 30 2e 0a 2a 2f 0a 23 64 65  g with 0..*/.#de
753e0 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 41 55 4c  fine SQLITE_FAUL
753f0 54 49 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c 4f 43  TINJECTOR_MALLOC
75400 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53       0.#define S
75410 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43  QLITE_FAULTINJEC
75420 54 4f 52 5f 43 4f 55 4e 54 20 20 20 20 20 20 31  TOR_COUNT      1
75430 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65  ../*.** The inte
75440 72 66 61 63 65 20 74 6f 20 74 68 65 20 63 6f 64  rface to the cod
75450 65 20 69 6e 20 66 61 75 6c 74 2e 63 20 75 73 65  e in fault.c use
75460 64 20 66 6f 72 20 69 64 65 6e 74 69 66 79 69 6e  d for identifyin
75470 67 20 22 62 65 6e 69 67 6e 22 0a 2a 2a 20 6d 61  g "benign".** ma
75480 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 2e 20 54  lloc failures. T
75490 68 69 73 20 69 73 20 6f 6e 6c 79 20 70 72 65 73  his is only pres
754a0 65 6e 74 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d  ent if SQLITE_OM
754b0 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a  IT_BUILTIN_TEST.
754c0 2a 2a 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65  ** is not define
754d0 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  d..*/.#ifndef SQ
754e0 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49  LITE_OMIT_BUILTI
754f0 4e 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52  N_TEST.SQLITE_PR
75500 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
75510 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
75520 61 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a 53 51 4c  alloc(void);.SQL
75530 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
75540 69 64 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e  id sqlite3EndBen
75550 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 3b  ignMalloc(void);
75560 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
75570 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e   sqlite3BeginBen
75580 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 20 20 23 64  ignMalloc().  #d
75590 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 6e 64  efine sqlite3End
755a0 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 23  BenignMalloc().#
755b0 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 49  endif..#define I
755c0 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 20 20  N_INDEX_ROWID   
755d0 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e          1.#defin
755e0 65 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20 20  e IN_INDEX_EPH  
755f0 20 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65             2.#de
75600 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 49 4e  fine IN_INDEX_IN
75610 44 45 58 20 20 20 20 20 20 20 20 20 20 20 33 0a  DEX           3.
75620 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
75630 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e  nt sqlite3FindIn
75640 49 6e 64 65 78 28 50 61 72 73 65 20 2a 2c 20 45  Index(Parse *, E
75650 78 70 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a 0a 23  xpr *, int*);..#
75660 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
75670 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45  BLE_ATOMIC_WRITE
75680 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
75690 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75    int sqlite3Jou
756a0 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33  rnalOpen(sqlite3
756b0 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68  _vfs *, const ch
756c0 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69  ar *, sqlite3_fi
756d0 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  le *, int, int);
756e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
756f0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75    int sqlite3Jou
75700 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 65 33  rnalSize(sqlite3
75710 5f 76 66 73 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  _vfs *);.SQLITE_
75720 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
75730 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61  lite3JournalCrea
75740 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  te(sqlite3_file 
75750 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66  *);.#else.  #def
75760 69 6e 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e  ine sqlite3Journ
75770 61 6c 53 69 7a 65 28 70 56 66 73 29 20 28 28 70  alSize(pVfs) ((p
75780 56 66 73 29 2d 3e 73 7a 4f 73 46 69 6c 65 29 0a  Vfs)->szOsFile).
75790 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50  #endif..SQLITE_P
757a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
757b0 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65  te3MemJournalOpe
757c0 6e 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  n(sqlite3_file *
757d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
757e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d  E int sqlite3Mem
757f0 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64  JournalSize(void
75800 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
75810 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4d  E int sqlite3IsM
75820 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65  emJournal(sqlite
75830 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 69 66 20  3_file *);..#if 
75840 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f  SQLITE_MAX_EXPR_
75850 44 45 50 54 48 3e 30 0a 53 51 4c 49 54 45 5f 50  DEPTH>0.SQLITE_P
75860 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
75870 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67  lite3ExprSetHeig
75880 68 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ht(Parse *pParse
75890 2c 20 45 78 70 72 20 2a 70 29 3b 0a 53 51 4c 49  , Expr *p);.SQLI
758a0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
758b0 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78   sqlite3SelectEx
758c0 70 72 48 65 69 67 68 74 28 53 65 6c 65 63 74 20  prHeight(Select 
758d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
758e0 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33  TE   int sqlite3
758f0 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28  ExprCheckHeight(
75900 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 65  Parse*, int);.#e
75910 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71  lse.  #define sq
75920 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67  lite3ExprSetHeig
75930 68 74 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e  ht(x,y).  #defin
75940 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45  e sqlite3SelectE
75950 78 70 72 48 65 69 67 68 74 28 78 29 20 30 0a 20  xprHeight(x) 0. 
75960 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
75970 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28  ExprCheckHeight(
75980 78 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c  x,y).#endif..SQL
75990 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20  ITE_PRIVATE u32 
759a0 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28  sqlite3Get4byte(
759b0 63 6f 6e 73 74 20 75 38 2a 29 3b 0a 53 51 4c 49  const u8*);.SQLI
759c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
759d0 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28  sqlite3Put4byte(
759e0 75 38 2a 2c 20 75 33 32 29 3b 0a 0a 23 69 66 64  u8*, u32);..#ifd
759f0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
75a00 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 53  _UNLOCK_NOTIFY.S
75a10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
75a20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e  void sqlite3Conn
75a30 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 73 71  ectionBlocked(sq
75a40 6c 69 74 65 33 20 2a 2c 20 73 71 6c 69 74 65 33  lite3 *, sqlite3
75a50 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
75a60 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
75a70 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f  e3ConnectionUnlo
75a80 63 6b 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62  cked(sqlite3 *db
75a90 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
75aa0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
75ab0 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64  ConnectionClosed
75ac0 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23  (sqlite3 *db);.#
75ad0 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73  else.  #define s
75ae0 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e  qlite3Connection
75af0 42 6c 6f 63 6b 65 64 28 78 2c 79 29 0a 20 20 23  Blocked(x,y).  #
75b00 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f  define sqlite3Co
75b10 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64  nnectionUnlocked
75b20 28 78 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71  (x).  #define sq
75b30 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43  lite3ConnectionC
75b40 6c 6f 73 65 64 28 78 29 0a 23 65 6e 64 69 66 0a  losed(x).#endif.
75b50 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
75b60 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56  EBUG.SQLITE_PRIV
75b70 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
75b80 65 33 50 61 72 73 65 72 54 72 61 63 65 28 46 49  e3ParserTrace(FI
75b90 4c 45 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65  LE*, char *);.#e
75ba0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  ndif../*.** If t
75bb0 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  he SQLITE_ENABLE
75bc0 20 49 4f 54 52 41 43 45 20 65 78 69 73 74 73 20   IOTRACE exists 
75bd0 74 68 65 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20  then the global 
75be0 76 61 72 69 61 62 6c 65 0a 2a 2a 20 73 71 6c 69  variable.** sqli
75bf0 74 65 33 49 6f 54 72 61 63 65 20 69 73 20 61 20  te3IoTrace is a 
75c00 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 72 69  pointer to a pri
75c10 6e 74 66 2d 6c 69 6b 65 20 72 6f 75 74 69 6e 65  ntf-like routine
75c20 20 75 73 65 64 20 74 6f 0a 2a 2a 20 70 72 69 6e   used to.** prin
75c30 74 20 49 2f 4f 20 74 72 61 63 69 6e 67 20 6d 65  t I/O tracing me
75c40 73 73 61 67 65 73 2e 20 0a 2a 2f 0a 23 69 66 64  ssages. .*/.#ifd
75c50 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
75c60 5f 49 4f 54 52 41 43 45 0a 23 20 64 65 66 69 6e  _IOTRACE.# defin
75c70 65 20 49 4f 54 52 41 43 45 28 41 29 20 20 69 66  e IOTRACE(A)  if
75c80 28 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65  ( sqlite3IoTrace
75c90 20 29 7b 20 73 71 6c 69 74 65 33 49 6f 54 72 61   ){ sqlite3IoTra
75ca0 63 65 20 41 3b 20 7d 0a 53 51 4c 49 54 45 5f 50  ce A; }.SQLITE_P
75cb0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
75cc0 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 63 65  lite3VdbeIOTrace
75cd0 53 71 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49  Sql(Vdbe*);.SQLI
75ce0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
75cf0 28 2a 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65  (*sqlite3IoTrace
75d00 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e  )(const char*,..
75d10 2e 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  .);.#else.# defi
75d20 6e 65 20 49 4f 54 52 41 43 45 28 41 29 0a 23 20  ne IOTRACE(A).# 
75d30 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64  define sqlite3Vd
75d40 62 65 49 4f 54 72 61 63 65 53 71 6c 28 58 29 0a  beIOTraceSql(X).
75d50 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a  #endif..#endif..
75d60 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
75d70 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 49 6e 74  End of sqliteInt
75d80 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
75d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
75da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
75db0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
75dc0 42 65 67 69 6e 20 66 69 6c 65 20 67 6c 6f 62 61  Begin file globa
75dd0 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  l.c ************
75de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
75df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
75e00 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20  /*.** 2008 June 
75e10 31 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  13.**.** The aut
75e20 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
75e30 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
75e40 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
75e50 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
75e60 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
75e70 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
75e80 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
75e90 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
75ea0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
75eb0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
75ec0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
75ed0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
75ee0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
75ef0 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
75f00 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
75f10 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
75f20 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
75f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
75f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
75f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
75f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
75f70 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
75f80 65 20 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e  e contains defin
75f90 69 74 69 6f 6e 73 20 6f 66 20 67 6c 6f 62 61 6c  itions of global
75fa0 20 76 61 72 69 61 62 6c 65 73 20 61 6e 64 20 63   variables and c
75fb0 6f 6e 74 61 6e 74 73 2e 0a 2a 2f 0a 0a 0a 2f 2a  ontants..*/.../*
75fc0 20 41 6e 20 61 72 72 61 79 20 74 6f 20 6d 61 70   An array to map
75fd0 20 61 6c 6c 20 75 70 70 65 72 2d 63 61 73 65 20   all upper-case 
75fe0 63 68 61 72 61 63 74 65 72 73 20 69 6e 74 6f 20  characters into 
75ff0 74 68 65 69 72 20 63 6f 72 72 65 73 70 6f 6e 64  their correspond
76000 69 6e 67 0a 2a 2a 20 6c 6f 77 65 72 2d 63 61 73  ing.** lower-cas
76010 65 20 63 68 61 72 61 63 74 65 72 2e 20 0a 2a 2a  e character. .**
76020 0a 2a 2a 20 53 51 4c 69 74 65 20 6f 6e 6c 79 20  .** SQLite only 
76030 63 6f 6e 73 69 64 65 72 73 20 55 53 2d 41 53 43  considers US-ASC
76040 49 49 20 28 6f 72 20 45 42 43 44 49 43 29 20 63  II (or EBCDIC) c
76050 68 61 72 61 63 74 65 72 73 2e 20 20 57 65 20 64  haracters.  We d
76060 6f 20 6e 6f 74 0a 2a 2a 20 68 61 6e 64 6c 65 20  o not.** handle 
76070 63 61 73 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73  case conversions
76080 20 66 6f 72 20 74 68 65 20 55 54 46 20 63 68 61   for the UTF cha
76090 72 61 63 74 65 72 20 73 65 74 20 73 69 6e 63 65  racter set since
760a0 20 74 68 65 20 74 61 62 6c 65 73 0a 2a 2a 20 69   the tables.** i
760b0 6e 76 6f 6c 76 65 64 20 61 72 65 20 6e 65 61 72  nvolved are near
760c0 6c 79 20 61 73 20 62 69 67 20 6f 72 20 62 69 67  ly as big or big
760d0 67 65 72 20 74 68 61 6e 20 53 51 4c 69 74 65 20  ger than SQLite 
760e0 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54  itself..*/.SQLIT
760f0 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
76100 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71  unsigned char sq
76110 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
76120 72 5b 5d 20 3d 20 7b 0a 23 69 66 64 65 66 20 53  r[] = {.#ifdef S
76130 51 4c 49 54 45 5f 41 53 43 49 49 0a 20 20 20 20  QLITE_ASCII.    
76140 20 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 2c    0,  1,  2,  3,
76150 20 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 2c    4,  5,  6,  7,
76160 20 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 2c    8,  9, 10, 11,
76170 20 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 2c   12, 13, 14, 15,
76180 20 31 36 2c 20 31 37 2c 0a 20 20 20 20 20 31 38   16, 17,.     18
76190 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32  , 19, 20, 21, 22
761a0 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36  , 23, 24, 25, 26
761b0 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30  , 27, 28, 29, 30
761c0 2c 20 33 31 2c 20 33 32 2c 20 33 33 2c 20 33 34  , 31, 32, 33, 34
761d0 2c 20 33 35 2c 0a 20 20 20 20 20 33 36 2c 20 33  , 35,.     36, 3
761e0 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c 20 34  7, 38, 39, 40, 4
761f0 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c 20 34  1, 42, 43, 44, 4
76200 35 2c 20 34 36 2c 20 34 37 2c 20 34 38 2c 20 34  5, 46, 47, 48, 4
76210 39 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 35  9, 50, 51, 52, 5
76220 33 2c 0a 20 20 20 20 20 35 34 2c 20 35 35 2c 20  3,.     54, 55, 
76230 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c 20  56, 57, 58, 59, 
76240 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c 20  60, 61, 62, 63, 
76250 36 34 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c 31  64, 97, 98, 99,1
76260 30 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c 0a  00,101,102,103,.
76270 20 20 20 20 31 30 34 2c 31 30 35 2c 31 30 36 2c      104,105,106,
76280 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c  107,108,109,110,
76290 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c  111,112,113,114,
762a0 31 31 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c  115,116,117,118,
762b0 31 31 39 2c 31 32 30 2c 31 32 31 2c 0a 20 20 20  119,120,121,.   
762c0 20 31 32 32 2c 20 39 31 2c 20 39 32 2c 20 39 33   122, 91, 92, 93
762d0 2c 20 39 34 2c 20 39 35 2c 20 39 36 2c 20 39 37  , 94, 95, 96, 97
762e0 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31  , 98, 99,100,101
762f0 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30 35  ,102,103,104,105
76300 2c 31 30 36 2c 31 30 37 2c 0a 20 20 20 20 31 30  ,106,107,.    10
76310 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31  8,109,110,111,11
76320 32 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 31 31  2,113,114,115,11
76330 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32  6,117,118,119,12
76340 30 2c 31 32 31 2c 31 32 32 2c 31 32 33 2c 31 32  0,121,122,123,12
76350 34 2c 31 32 35 2c 0a 20 20 20 20 31 32 36 2c 31  4,125,.    126,1
76360 32 37 2c 31 32 38 2c 31 32 39 2c 31 33 30 2c 31  27,128,129,130,1
76370 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31  31,132,133,134,1
76380 33 35 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c 31  35,136,137,138,1
76390 33 39 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31  39,140,141,142,1
763a0 34 33 2c 0a 20 20 20 20 31 34 34 2c 31 34 35 2c  43,.    144,145,
763b0 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c  146,147,148,149,
763c0 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c  150,151,152,153,
763d0 31 35 34 2c 31 35 35 2c 31 35 36 2c 31 35 37 2c  154,155,156,157,
763e0 31 35 38 2c 31 35 39 2c 31 36 30 2c 31 36 31 2c  158,159,160,161,
763f0 0a 20 20 20 20 31 36 32 2c 31 36 33 2c 31 36 34  .    162,163,164
76400 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38  ,165,166,167,168
76410 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c 31 37 32  ,169,170,171,172
76420 2c 31 37 33 2c 31 37 34 2c 31 37 35 2c 31 37 36  ,173,174,175,176
76430 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c 0a 20 20  ,177,178,179,.  
76440 20 20 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 38    180,181,182,18
76450 33 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 38  3,184,185,186,18
76460 37 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 39  7,188,189,190,19
76470 31 2c 31 39 32 2c 31 39 33 2c 31 39 34 2c 31 39  1,192,193,194,19
76480 35 2c 31 39 36 2c 31 39 37 2c 0a 20 20 20 20 31  5,196,197,.    1
76490 39 38 2c 31 39 39 2c 32 30 30 2c 32 30 31 2c 32  98,199,200,201,2
764a0 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32  02,203,204,205,2
764b0 30 36 2c 32 30 37 2c 32 30 38 2c 32 30 39 2c 32  06,207,208,209,2
764c0 31 30 2c 32 31 31 2c 32 31 32 2c 32 31 33 2c 32  10,211,212,213,2
764d0 31 34 2c 32 31 35 2c 0a 20 20 20 20 32 31 36 2c  14,215,.    216,
764e0 32 31 37 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c  217,218,219,220,
764f0 32 32 31 2c 32 32 32 2c 32 32 33 2c 32 32 34 2c  221,222,223,224,
76500 32 32 35 2c 32 32 36 2c 32 32 37 2c 32 32 38 2c  225,226,227,228,
76510 32 32 39 2c 32 33 30 2c 32 33 31 2c 32 33 32 2c  229,230,231,232,
76520 32 33 33 2c 0a 20 20 20 20 32 33 34 2c 32 33 35  233,.    234,235
76530 2c 32 33 36 2c 32 33 37 2c 32 33 38 2c 32 33 39  ,236,237,238,239
76540 2c 32 34 30 2c 32 34 31 2c 32 34 32 2c 32 34 33  ,240,241,242,243
76550 2c 32 34 34 2c 32 34 35 2c 32 34 36 2c 32 34 37  ,244,245,246,247
76560 2c 32 34 38 2c 32 34 39 2c 32 35 30 2c 32 35 31  ,248,249,250,251
76570 2c 0a 20 20 20 20 32 35 32 2c 32 35 33 2c 32 35  ,.    252,253,25
76580 34 2c 32 35 35 0a 23 65 6e 64 69 66 0a 23 69 66  4,255.#endif.#if
76590 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49  def SQLITE_EBCDI
765a0 43 0a 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20  C.      0,  1,  
765b0 32 2c 20 20 33 2c 20 20 34 2c 20 20 35 2c 20 20  2,  3,  4,  5,  
765c0 36 2c 20 20 37 2c 20 20 38 2c 20 20 39 2c 20 31  6,  7,  8,  9, 1
765d0 30 2c 20 31 31 2c 20 31 32 2c 20 31 33 2c 20 31  0, 11, 12, 13, 1
765e0 34 2c 20 31 35 2c 20 2f 2a 20 30 78 20 2a 2f 0a  4, 15, /* 0x */.
765f0 20 20 20 20 20 31 36 2c 20 31 37 2c 20 31 38 2c       16, 17, 18,
76600 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 2c   19, 20, 21, 22,
76610 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 2c   23, 24, 25, 26,
76620 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 2c   27, 28, 29, 30,
76630 20 33 31 2c 20 2f 2a 20 31 78 20 2a 2f 0a 20 20   31, /* 1x */.  
76640 20 20 20 33 32 2c 20 33 33 2c 20 33 34 2c 20 33     32, 33, 34, 3
76650 35 2c 20 33 36 2c 20 33 37 2c 20 33 38 2c 20 33  5, 36, 37, 38, 3
76660 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c 20 34  9, 40, 41, 42, 4
76670 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c 20 34  3, 44, 45, 46, 4
76680 37 2c 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 20  7, /* 2x */.    
76690 20 34 38 2c 20 34 39 2c 20 35 30 2c 20 35 31 2c   48, 49, 50, 51,
766a0 20 35 32 2c 20 35 33 2c 20 35 34 2c 20 35 35 2c   52, 53, 54, 55,
766b0 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c   56, 57, 58, 59,
766c0 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c   60, 61, 62, 63,
766d0 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20 20 36   /* 3x */.     6
766e0 34 2c 20 36 35 2c 20 36 36 2c 20 36 37 2c 20 36  4, 65, 66, 67, 6
766f0 38 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c 20 37  8, 69, 70, 71, 7
76700 32 2c 20 37 33 2c 20 37 34 2c 20 37 35 2c 20 37  2, 73, 74, 75, 7
76710 36 2c 20 37 37 2c 20 37 38 2c 20 37 39 2c 20 2f  6, 77, 78, 79, /
76720 2a 20 34 78 20 2a 2f 0a 20 20 20 20 20 38 30 2c  * 4x */.     80,
76730 20 38 31 2c 20 38 32 2c 20 38 33 2c 20 38 34 2c   81, 82, 83, 84,
76740 20 38 35 2c 20 38 36 2c 20 38 37 2c 20 38 38 2c   85, 86, 87, 88,
76750 20 38 39 2c 20 39 30 2c 20 39 31 2c 20 39 32 2c   89, 90, 91, 92,
76760 20 39 33 2c 20 39 34 2c 20 39 35 2c 20 2f 2a 20   93, 94, 95, /* 
76770 35 78 20 2a 2f 0a 20 20 20 20 20 39 36 2c 20 39  5x */.     96, 9
76780 37 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c 20 36  7, 66, 67, 68, 6
76790 39 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c 20 37  9, 70, 71, 72, 7
767a0 33 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c 31 30  3,106,107,108,10
767b0 39 2c 31 31 30 2c 31 31 31 2c 20 2f 2a 20 36 78  9,110,111, /* 6x
767c0 20 2a 2f 0a 20 20 20 20 31 31 32 2c 20 38 31 2c   */.    112, 81,
767d0 20 38 32 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c   82, 83, 84, 85,
767e0 20 38 36 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c   86, 87, 88, 89,
767f0 31 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 35 2c  122,123,124,125,
76800 31 32 36 2c 31 32 37 2c 20 2f 2a 20 37 78 20 2a  126,127, /* 7x *
76810 2f 0a 20 20 20 20 31 32 38 2c 31 32 39 2c 31 33  /.    128,129,13
76820 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33  0,131,132,133,13
76830 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 31 33  4,135,136,137,13
76840 38 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c 31 34  8,139,140,141,14
76850 32 2c 31 34 33 2c 20 2f 2a 20 38 78 20 2a 2f 0a  2,143, /* 8x */.
76860 20 20 20 20 31 34 34 2c 31 34 35 2c 31 34 36 2c      144,145,146,
76870 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c  147,148,149,150,
76880 31 35 31 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c  151,152,153,154,
76890 31 35 35 2c 31 35 36 2c 31 35 37 2c 31 35 36 2c  155,156,157,156,
768a0 31 35 39 2c 20 2f 2a 20 39 78 20 2a 2f 0a 20 20  159, /* 9x */.  
768b0 20 20 31 36 30 2c 31 36 31 2c 31 36 32 2c 31 36    160,161,162,16
768c0 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 36  3,164,165,166,16
768d0 37 2c 31 36 38 2c 31 36 39 2c 31 37 30 2c 31 37  7,168,169,170,17
768e0 31 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 37  1,140,141,142,17
768f0 35 2c 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20 20  5, /* Ax */.    
76900 31 37 36 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c  176,177,178,179,
76910 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 38 33 2c  180,181,182,183,
76920 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 38 37 2c  184,185,186,187,
76930 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 39 31 2c  188,189,190,191,
76940 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 20 31 39   /* Bx */.    19
76950 32 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33  2,129,130,131,13
76960 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33  2,133,134,135,13
76970 36 2c 31 33 37 2c 32 30 32 2c 32 30 33 2c 32 30  6,137,202,203,20
76980 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 20 2f  4,205,206,207, /
76990 2a 20 43 78 20 2a 2f 0a 20 20 20 20 32 30 38 2c  * Cx */.    208,
769a0 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c  145,146,147,148,
769b0 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c  149,150,151,152,
769c0 31 35 33 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c  153,218,219,220,
769d0 32 32 31 2c 32 32 32 2c 32 32 33 2c 20 2f 2a 20  221,222,223, /* 
769e0 44 78 20 2a 2f 0a 20 20 20 20 32 32 34 2c 32 32  Dx */.    224,22
769f0 35 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 36  5,162,163,164,16
76a00 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 36  5,166,167,168,16
76a10 39 2c 32 33 32 2c 32 30 33 2c 32 30 34 2c 32 30  9,232,203,204,20
76a20 35 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20 45 78  5,206,207, /* Ex
76a30 20 2a 2f 0a 20 20 20 20 32 33 39 2c 32 34 30 2c   */.    239,240,
76a40 32 34 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c  241,242,243,244,
76a50 32 34 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c  245,246,247,248,
76a60 32 34 39 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c  249,219,220,221,
76a70 32 32 32 2c 32 35 35 2c 20 2f 2a 20 46 78 20 2a  222,255, /* Fx *
76a80 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a  /.#endif.};../*.
76a90 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
76aa0 20 32 35 36 20 62 79 74 65 20 6c 6f 6f 6b 75 70   256 byte lookup
76ab0 20 74 61 62 6c 65 20 69 73 20 75 73 65 64 20 74   table is used t
76ac0 6f 20 73 75 70 70 6f 72 74 20 53 51 4c 69 74 65  o support SQLite
76ad0 73 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 65 71  s built-in.** eq
76ae0 75 69 76 61 6c 65 6e 74 73 20 74 6f 20 74 68 65  uivalents to the
76af0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 6e 64   following stand
76b00 61 72 64 20 6c 69 62 72 61 72 79 20 66 75 6e 63  ard library func
76b10 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 69  tions:.**.**   i
76b20 73 73 70 61 63 65 28 29 20 20 20 20 20 20 20 20  sspace()        
76b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76b40 30 78 30 31 0a 2a 2a 20 20 20 69 73 61 6c 70 68  0x01.**   isalph
76b50 61 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20  a()             
76b60 20 20 20 20 20 20 20 20 20 20 20 30 78 30 32 0a             0x02.
76b70 2a 2a 20 20 20 69 73 64 69 67 69 74 28 29 20 20  **   isdigit()  
76b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76b90 20 20 20 20 20 20 30 78 30 34 0a 2a 2a 20 20 20        0x04.**   
76ba0 69 73 61 6c 6e 75 6d 28 29 20 20 20 20 20 20 20  isalnum()       
76bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76bc0 20 30 78 30 36 0a 2a 2a 20 20 20 69 73 78 64 69   0x06.**   isxdi
76bd0 67 69 74 28 29 20 20 20 20 20 20 20 20 20 20 20  git()           
76be0 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 38              0x08
76bf0 0a 2a 2a 20 20 20 74 6f 75 70 70 65 72 28 29 20  .**   toupper() 
76c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76c10 20 20 20 20 20 20 20 30 78 32 30 0a 2a 2a 0a 2a         0x20.**.*
76c20 2a 20 42 69 74 20 30 78 32 30 20 69 73 20 73 65  * Bit 0x20 is se
76c30 74 20 69 66 20 74 68 65 20 6d 61 70 70 65 64 20  t if the mapped 
76c40 63 68 61 72 61 63 74 65 72 20 72 65 71 75 69 72  character requir
76c50 65 73 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 74  es translation t
76c60 6f 20 75 70 70 65 72 0a 2a 2a 20 63 61 73 65 2e  o upper.** case.
76c70 20 69 2e 65 2e 20 69 66 20 74 68 65 20 63 68 61   i.e. if the cha
76c80 72 61 63 74 65 72 20 69 73 20 61 20 6c 6f 77 65  racter is a lowe
76c90 72 2d 63 61 73 65 20 41 53 43 49 49 20 63 68 61  r-case ASCII cha
76ca0 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20 78 20  racter..** If x 
76cb0 69 73 20 61 20 6c 6f 77 65 72 2d 63 61 73 65 20  is a lower-case 
76cc0 41 53 43 49 49 20 63 68 61 72 61 63 74 65 72 2c  ASCII character,
76cd0 20 74 68 65 6e 20 69 74 73 20 75 70 70 65 72 2d   then its upper-
76ce0 63 61 73 65 20 65 71 75 69 76 61 6c 65 6e 74 0a  case equivalent.
76cf0 2a 2a 20 69 73 20 28 78 20 2d 20 30 78 32 30 29  ** is (x - 0x20)
76d00 2e 20 54 68 65 72 65 66 6f 72 65 20 74 6f 75 70  . Therefore toup
76d10 70 65 72 28 29 20 63 61 6e 20 62 65 20 69 6d 70  per() can be imp
76d20 6c 65 6d 65 6e 74 65 64 20 61 73 3a 0a 2a 2a 0a  lemented as:.**.
76d30 2a 2a 20 20 20 28 78 20 26 20 7e 28 6d 61 70 5b  **   (x & ~(map[
76d40 78 5d 26 30 78 32 30 29 29 0a 2a 2a 0a 2a 2a 20  x]&0x20)).**.** 
76d50 53 74 61 6e 64 61 72 64 20 66 75 6e 63 74 69 6f  Standard functio
76d60 6e 20 74 6f 6c 6f 77 65 72 28 29 20 69 73 20 69  n tolower() is i
76d70 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67  mplemented using
76d80 20 74 68 65 20 73 71 6c 69 74 65 33 55 70 70 65   the sqlite3Uppe
76d90 72 54 6f 4c 6f 77 65 72 5b 5d 0a 2a 2a 20 61 72  rToLower[].** ar
76da0 72 61 79 2e 20 74 6f 6c 6f 77 65 72 28 29 20 69  ray. tolower() i
76db0 73 20 75 73 65 64 20 6d 6f 72 65 20 6f 66 74 65  s used more ofte
76dc0 6e 20 74 68 61 6e 20 74 6f 75 70 70 65 72 28 29  n than toupper()
76dd0 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a   by SQLite..**.*
76de0 2a 20 53 51 4c 69 74 65 27 73 20 76 65 72 73 69  * SQLite's versi
76df0 6f 6e 73 20 61 72 65 20 69 64 65 6e 74 69 63 61  ons are identica
76e00 6c 20 74 6f 20 74 68 65 20 73 74 61 6e 64 61 72  l to the standar
76e10 64 20 76 65 72 73 69 6f 6e 73 20 61 73 73 75 6d  d versions assum
76e20 69 6e 67 20 61 0a 2a 2a 20 6c 6f 63 61 6c 65 20  ing a.** locale 
76e30 6f 66 20 22 43 22 2e 20 54 68 65 79 20 61 72 65  of "C". They are
76e40 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20   implemented as 
76e50 6d 61 63 72 6f 73 20 69 6e 20 73 71 6c 69 74 65  macros in sqlite
76e60 49 6e 74 2e 68 2e 0a 2a 2f 0a 23 69 66 64 65 66  Int.h..*/.#ifdef
76e70 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 53 51   SQLITE_ASCII.SQ
76e80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
76e90 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
76ea0 20 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70   sqlite3CtypeMap
76eb0 5b 32 35 36 5d 20 3d 20 7b 0a 20 20 30 78 30 30  [256] = {.  0x00
76ec0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
76ed0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
76ee0 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20  0x00, 0x00,  /* 
76ef0 30 30 2e 2e 30 37 20 20 20 20 2e 2e 2e 2e 2e 2e  00..07    ......
76f00 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78  .. */.  0x00, 0x
76f10 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20  01, 0x01, 0x01, 
76f20 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 30  0x01, 0x01, 0x00
76f30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 30 38 2e 2e  , 0x00,  /* 08..
76f40 30 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a  0f    ........ *
76f50 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20  /.  0x00, 0x00, 
76f60 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
76f70 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
76f80 30 30 2c 20 20 2f 2a 20 31 30 2e 2e 31 37 20 20  00,  /* 10..17  
76f90 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20    ........ */.  
76fa0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
76fb0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
76fc0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
76fd0 20 2f 2a 20 31 38 2e 2e 31 66 20 20 20 20 2e 2e   /* 18..1f    ..
76fe0 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 31  ...... */.  0x01
76ff0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77000 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77010 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20  0x00, 0x00,  /* 
77020 32 30 2e 2e 32 37 20 20 20 20 20 21 22 23 24 25  20..27     !"#$%
77030 26 27 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78  &' */.  0x00, 0x
77040 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77050 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
77060 2c 20 30 78 30 30 2c 20 20 2f 2a 20 32 38 2e 2e  , 0x00,  /* 28..
77070 32 66 20 20 20 20 28 29 2a 2b 2c 2d 2e 2f 20 2a  2f    ()*+,-./ *
77080 2f 0a 20 20 30 78 30 63 2c 20 30 78 30 63 2c 20  /.  0x0c, 0x0c, 
77090 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63  0x0c, 0x0c, 0x0c
770a0 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78  , 0x0c, 0x0c, 0x
770b0 30 63 2c 20 20 2f 2a 20 33 30 2e 2e 33 37 20 20  0c,  /* 30..37  
770c0 20 20 30 31 32 33 34 35 36 37 20 2a 2f 0a 20 20    01234567 */.  
770d0 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 30  0x0c, 0x0c, 0x00
770e0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
770f0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77100 20 2f 2a 20 33 38 2e 2e 33 66 20 20 20 20 38 39   /* 38..3f    89
77110 3a 3b 3c 3d 3e 3f 20 2a 2f 0a 0a 20 20 30 78 30  :;<=>? */..  0x0
77120 30 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30  0, 0x0a, 0x0a, 0
77130 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c  x0a, 0x0a, 0x0a,
77140 20 30 78 30 61 2c 20 30 78 30 32 2c 20 20 2f 2a   0x0a, 0x02,  /*
77150 20 34 30 2e 2e 34 37 20 20 20 20 40 41 42 43 44   40..47    @ABCD
77160 45 46 47 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30  EFG */.  0x02, 0
77170 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c  x02, 0x02, 0x02,
77180 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30   0x02, 0x02, 0x0
77190 32 2c 20 30 78 30 32 2c 20 20 2f 2a 20 34 38 2e  2, 0x02,  /* 48.
771a0 2e 34 66 20 20 20 20 48 49 4a 4b 4c 4d 4e 4f 20  .4f    HIJKLMNO 
771b0 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 32 2c  */.  0x02, 0x02,
771c0 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30   0x02, 0x02, 0x0
771d0 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30  2, 0x02, 0x02, 0
771e0 78 30 32 2c 20 20 2f 2a 20 35 30 2e 2e 35 37 20  x02,  /* 50..57 
771f0 20 20 20 50 51 52 53 54 55 56 57 20 2a 2f 0a 20     PQRSTUVW */. 
77200 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30   0x02, 0x02, 0x0
77210 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  2, 0x00, 0x00, 0
77220 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77230 20 20 2f 2a 20 35 38 2e 2e 35 66 20 20 20 20 58    /* 58..5f    X
77240 59 5a 5b 5c 5d 5e 5f 20 2a 2f 0a 20 20 30 78 30  YZ[\]^_ */.  0x0
77250 30 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30  0, 0x2a, 0x2a, 0
77260 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c  x2a, 0x2a, 0x2a,
77270 20 30 78 32 61 2c 20 30 78 32 32 2c 20 20 2f 2a   0x2a, 0x22,  /*
77280 20 36 30 2e 2e 36 37 20 20 20 20 60 61 62 63 64   60..67    `abcd
77290 65 66 67 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30  efg */.  0x22, 0
772a0 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c  x22, 0x22, 0x22,
772b0 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32   0x22, 0x22, 0x2
772c0 32 2c 20 30 78 32 32 2c 20 20 2f 2a 20 36 38 2e  2, 0x22,  /* 68.
772d0 2e 36 66 20 20 20 20 68 69 6a 6b 6c 6d 6e 6f 20  .6f    hijklmno 
772e0 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 32 2c  */.  0x22, 0x22,
772f0 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32   0x22, 0x22, 0x2
77300 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30  2, 0x22, 0x22, 0
77310 78 32 32 2c 20 20 2f 2a 20 37 30 2e 2e 37 37 20  x22,  /* 70..77 
77320 20 20 20 70 71 72 73 74 75 76 77 20 2a 2f 0a 20     pqrstuvw */. 
77330 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32   0x22, 0x22, 0x2
77340 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  2, 0x00, 0x00, 0
77350 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77360 20 20 2f 2a 20 37 38 2e 2e 37 66 20 20 20 20 78    /* 78..7f    x
77370 79 7a 7b 7c 7d 7e 2e 20 2a 2f 0a 0a 20 20 30 78  yz{|}~. */..  0x
77380 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77390 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
773a0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f  , 0x00, 0x00,  /
773b0 2a 20 38 30 2e 2e 38 37 20 20 20 20 2e 2e 2e 2e  * 80..87    ....
773c0 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20  .... */.  0x00, 
773d0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
773e0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
773f0 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 38 38  00, 0x00,  /* 88
77400 2e 2e 38 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e  ..8f    ........
77410 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30   */.  0x00, 0x00
77420 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77430 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77440 30 78 30 30 2c 20 20 2f 2a 20 39 30 2e 2e 39 37  0x00,  /* 90..97
77450 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a      ........ */.
77460 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78    0x00, 0x00, 0x
77470 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77480 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
77490 2c 20 20 2f 2a 20 39 38 2e 2e 39 66 20 20 20 20  ,  /* 98..9f    
774a0 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78  ........ */.  0x
774b0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
774c0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
774d0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f  , 0x00, 0x00,  /
774e0 2a 20 61 30 2e 2e 61 37 20 20 20 20 2e 2e 2e 2e  * a0..a7    ....
774f0 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20  .... */.  0x00, 
77500 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
77510 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77520 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 61 38  00, 0x00,  /* a8
77530 2e 2e 61 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e  ..af    ........
77540 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30   */.  0x00, 0x00
77550 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77560 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77570 30 78 30 30 2c 20 20 2f 2a 20 62 30 2e 2e 62 37  0x00,  /* b0..b7
77580 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a      ........ */.
77590 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78    0x00, 0x00, 0x
775a0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
775b0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
775c0 2c 20 20 2f 2a 20 62 38 2e 2e 62 66 20 20 20 20  ,  /* b8..bf    
775d0 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 0a 20 20 30  ........ */..  0
775e0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
775f0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77600 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20  0, 0x00, 0x00,  
77610 2f 2a 20 63 30 2e 2e 63 37 20 20 20 20 2e 2e 2e  /* c0..c7    ...
77620 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c  ..... */.  0x00,
77630 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77640 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77650 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 63  x00, 0x00,  /* c
77660 38 2e 2e 63 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e  8..cf    .......
77670 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30  . */.  0x00, 0x0
77680 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77690 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
776a0 20 30 78 30 30 2c 20 20 2f 2a 20 64 30 2e 2e 64   0x00,  /* d0..d
776b0 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f  7    ........ */
776c0 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  .  0x00, 0x00, 0
776d0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
776e0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
776f0 30 2c 20 20 2f 2a 20 64 38 2e 2e 64 66 20 20 20  0,  /* d8..df   
77700 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30   ........ */.  0
77710 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77720 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77730 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20  0, 0x00, 0x00,  
77740 2f 2a 20 65 30 2e 2e 65 37 20 20 20 20 2e 2e 2e  /* e0..e7    ...
77750 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c  ..... */.  0x00,
77760 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77770 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77780 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 65  x00, 0x00,  /* e
77790 38 2e 2e 65 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e  8..ef    .......
777a0 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30  . */.  0x00, 0x0
777b0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
777c0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
777d0 20 30 78 30 30 2c 20 20 2f 2a 20 66 30 2e 2e 66   0x00,  /* f0..f
777e0 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f  7    ........ */
777f0 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  .  0x00, 0x00, 0
77800 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77810 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77820 30 20 20 20 2f 2a 20 66 38 2e 2e 66 66 20 20 20  0   /* f8..ff   
77830 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 7d 3b 0a   ........ */.};.
77840 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20  #endif..../*.** 
77850 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 69  The following si
77860 6e 67 6c 65 74 6f 6e 20 63 6f 6e 74 61 69 6e 73  ngleton contains
77870 20 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66   the global conf
77880 69 67 75 72 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a  iguration for.**
77890 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
778a0 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ary..*/.SQLITE_P
778b0 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53  RIVATE SQLITE_WS
778c0 44 20 73 74 72 75 63 74 20 53 71 6c 69 74 65 33  D struct Sqlite3
778d0 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f  Config sqlite3Co
778e0 6e 66 69 67 20 3d 20 7b 0a 20 20 20 53 51 4c 49  nfig = {.   SQLI
778f0 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54  TE_DEFAULT_MEMST
77900 41 54 55 53 2c 20 20 2f 2a 20 62 4d 65 6d 73 74  ATUS,  /* bMemst
77910 61 74 20 2a 2f 0a 20 20 20 31 2c 20 20 20 20 20  at */.   1,     
77920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77930 20 20 20 20 2f 2a 20 62 43 6f 72 65 4d 75 74 65      /* bCoreMute
77940 78 20 2a 2f 0a 20 20 20 53 51 4c 49 54 45 5f 54  x */.   SQLITE_T
77950 48 52 45 41 44 53 41 46 45 3d 3d 31 2c 20 20 20  HREADSAFE==1,   
77960 20 20 20 2f 2a 20 62 46 75 6c 6c 4d 75 74 65 78     /* bFullMutex
77970 20 2a 2f 0a 20 20 20 30 78 37 66 66 66 66 66 66   */.   0x7ffffff
77980 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
77990 20 20 2f 2a 20 6d 78 53 74 72 6c 65 6e 20 2a 2f    /* mxStrlen */
779a0 0a 20 20 20 31 30 30 2c 20 20 20 20 20 20 20 20  .   100,        
779b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
779c0 2a 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f  * szLookaside */
779d0 0a 20 20 20 35 30 30 2c 20 20 20 20 20 20 20 20  .   500,        
779e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
779f0 2a 20 6e 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a  * nLookaside */.
77a00 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c     {0,0,0,0,0,0,
77a10 30 2c 30 7d 2c 20 20 20 20 20 20 20 20 20 2f 2a  0,0},         /*
77a20 20 6d 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c   m */.   {0,0,0,
77a30 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20  0,0,0,0,0,0},   
77a40 20 20 20 20 2f 2a 20 6d 75 74 65 78 20 2a 2f 0a      /* mutex */.
77a50 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c     {0,0,0,0,0,0,
77a60 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 2f 2a  0,0,0,0,0},   /*
77a70 20 70 63 61 63 68 65 20 2a 2f 0a 20 20 20 28 76   pcache */.   (v
77a80 6f 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20  oid*)0,         
77a90 20 20 20 20 20 20 20 20 20 2f 2a 20 70 48 65 61           /* pHea
77aa0 70 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20  p */.   0,      
77ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77ac0 20 20 20 2f 2a 20 6e 48 65 61 70 20 2a 2f 0a 20     /* nHeap */. 
77ad0 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 20    0, 0,         
77ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
77af0 6d 6e 48 65 61 70 2c 20 6d 78 48 65 61 70 20 2a  mnHeap, mxHeap *
77b00 2f 0a 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 20  /.   (void*)0,  
77b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77b20 2f 2a 20 70 53 63 72 61 74 63 68 20 2a 2f 0a 20  /* pScratch */. 
77b30 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
77b40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
77b50 73 7a 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20  szScratch */.   
77b60 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
77b70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 53             /* nS
77b80 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 28 76 6f  cratch */.   (vo
77b90 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20  id*)0,          
77ba0 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65          /* pPage
77bb0 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20   */.   0,       
77bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77bd0 20 20 2f 2a 20 73 7a 50 61 67 65 20 2a 2f 0a 20    /* szPage */. 
77be0 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
77bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
77c00 6e 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20  nPage */.   0,  
77c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77c20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 72 73         /* mxPars
77c30 65 72 53 74 61 63 6b 20 2a 2f 0a 20 20 20 30 2c  erStack */.   0,
77c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77c50 20 20 20 20 20 20 20 20 20 2f 2a 20 73 68 61 72           /* shar
77c60 65 64 43 61 63 68 65 45 6e 61 62 6c 65 64 20 2a  edCacheEnabled *
77c70 2f 0a 20 20 20 2f 2a 20 41 6c 6c 20 74 68 65 20  /.   /* All the 
77c80 72 65 73 74 20 73 68 6f 75 6c 64 20 61 6c 77 61  rest should alwa
77c90 79 73 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65  ys be initialize
77ca0 64 20 74 6f 20 7a 65 72 6f 20 2a 2f 0a 20 20 20  d to zero */.   
77cb0 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
77cc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73             /* is
77cd0 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20  Init */.   0,   
77ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77cf0 20 20 20 20 20 20 2f 2a 20 69 6e 50 72 6f 67 72        /* inProgr
77d00 65 73 73 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20  ess */.   0,    
77d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77d20 20 20 20 20 20 2f 2a 20 69 73 4d 75 74 65 78 49       /* isMutexI
77d30 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20  nit */.   0,    
77d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77d50 20 20 20 20 20 2f 2a 20 69 73 4d 61 6c 6c 6f 63       /* isMalloc
77d60 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20  Init */.   0,   
77d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77d80 20 20 20 20 20 20 2f 2a 20 69 73 50 43 61 63 68        /* isPCach
77d90 65 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20  eInit */.   0,  
77da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77db0 20 20 20 20 20 20 20 2f 2a 20 70 49 6e 69 74 4d         /* pInitM
77dc0 75 74 65 78 20 2a 2f 0a 20 20 20 30 2c 20 20 20  utex */.   0,   
77dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77de0 20 20 20 20 20 20 2f 2a 20 6e 52 65 66 49 6e 69        /* nRefIni
77df0 74 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 0a 2f  tMutex */.};.../
77e00 2a 0a 2a 2a 20 48 61 73 68 20 74 61 62 6c 65 20  *.** Hash table 
77e10 66 6f 72 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74  for global funct
77e20 69 6f 6e 73 20 2d 20 66 75 6e 63 74 69 6f 6e 73  ions - functions
77e30 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a   common to all.*
77e40 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
77e50 63 74 69 6f 6e 73 2e 20 20 41 66 74 65 72 20 69  ctions.  After i
77e60 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2c 20 74  nitialization, t
77e70 68 69 73 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20  his table is.** 
77e80 72 65 61 64 2d 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51  read-only..*/.SQ
77e90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c  LITE_PRIVATE SQL
77ea0 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 48  ITE_WSD FuncDefH
77eb0 61 73 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  ash sqlite3Globa
77ec0 6c 46 75 6e 63 74 69 6f 6e 73 3b 0a 0a 2f 2a 0a  lFunctions;../*.
77ed0 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  ** The value of 
77ee0 74 68 65 20 22 70 65 6e 64 69 6e 67 22 20 62 79  the "pending" by
77ef0 74 65 20 6d 75 73 74 20 62 65 20 30 78 34 30 30  te must be 0x400
77f00 30 30 30 30 30 20 28 31 20 62 79 74 65 20 70 61  00000 (1 byte pa
77f10 73 74 20 74 68 65 0a 2a 2a 20 31 2d 67 69 62 61  st the.** 1-giba
77f20 62 79 74 65 20 62 6f 75 6e 64 61 72 79 29 20 69  byte boundary) i
77f30 6e 20 61 20 63 6f 6d 70 61 74 69 62 6c 65 20 64  n a compatible d
77f40 61 74 61 62 61 73 65 2e 20 20 53 51 4c 69 74 65  atabase.  SQLite
77f50 20 6e 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 74   never uses.** t
77f60 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  he database page
77f70 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
77f80 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 2e  he pending byte.
77f90 20 20 49 74 20 6e 65 76 65 72 20 61 74 74 65 6d    It never attem
77fa0 70 74 73 0a 2a 2a 20 74 6f 20 72 65 61 64 20 6f  pts.** to read o
77fb0 72 20 77 72 69 74 65 20 74 68 61 74 20 70 61 67  r write that pag
77fc0 65 2e 20 20 54 68 65 20 70 65 6e 64 69 6e 67 20  e.  The pending 
77fd0 62 79 74 65 20 70 61 67 65 20 69 73 20 73 65 74  byte page is set
77fe0 20 61 73 73 69 67 6e 0a 2a 2a 20 66 6f 72 20 75   assign.** for u
77ff0 73 65 20 62 79 20 74 68 65 20 56 46 53 20 6c 61  se by the VFS la
78000 79 65 72 73 20 61 73 20 73 70 61 63 65 20 66 6f  yers as space fo
78010 72 20 6d 61 6e 61 67 69 6e 67 20 66 69 6c 65 20  r managing file 
78020 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 44 75 72  locks..**.** Dur
78030 69 6e 67 20 74 65 73 74 69 6e 67 2c 20 69 74 20  ing testing, it 
78040 69 73 20 6f 66 74 65 6e 20 64 65 73 69 72 61 62  is often desirab
78050 6c 65 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 70  le to move the p
78060 65 6e 64 69 6e 67 20 62 79 74 65 20 74 6f 0a 2a  ending byte to.*
78070 2a 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 6f  * a different po
78080 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 66 69  sition in the fi
78090 6c 65 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77 73  le.  This allows
780a0 20 63 6f 64 65 20 74 68 61 74 20 68 61 73 20 74   code that has t
780b0 6f 0a 2a 2a 20 64 65 61 6c 20 77 69 74 68 20 74  o.** deal with t
780c0 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20  he pending byte 
780d0 74 6f 20 72 75 6e 20 6f 6e 20 66 69 6c 65 73 20  to run on files 
780e0 74 68 61 74 20 61 72 65 20 6d 75 63 68 20 73 6d  that are much sm
780f0 61 6c 6c 65 72 0a 2a 2a 20 74 68 61 6e 20 31 20  aller.** than 1 
78100 47 69 42 2e 20 20 54 68 65 20 73 71 6c 69 74 65  GiB.  The sqlite
78110 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29  3_test_control()
78120 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62   interface can b
78130 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 6d 6f 76  e used to.** mov
78140 65 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79  e the pending by
78150 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 4f 52 54  te..**.** IMPORT
78160 41 4e 54 3a 20 20 43 68 61 6e 67 69 6e 67 20 74  ANT:  Changing t
78170 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20  he pending byte 
78180 74 6f 20 61 6e 79 20 76 61 6c 75 65 20 6f 74 68  to any value oth
78190 65 72 20 74 68 61 6e 0a 2a 2a 20 30 78 34 30 30  er than.** 0x400
781a0 30 30 30 30 30 20 72 65 73 75 6c 74 73 20 69 6e  00000 results in
781b0 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65   an incompatible
781c0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66   database file f
781d0 6f 72 6d 61 74 21 0a 2a 2a 20 43 68 61 6e 67 69  ormat!.** Changi
781e0 6e 67 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62  ng the pending b
781f0 79 74 65 20 64 75 72 69 6e 67 20 6f 70 65 72 61  yte during opera
78200 74 69 6e 67 20 72 65 73 75 6c 74 73 20 69 6e 20  ting results in 
78210 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 61 6e 64  undefined.** and
78220 20 64 69 6c 65 74 65 72 69 6f 75 73 20 62 65 68   dileterious beh
78230 61 76 69 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  avior..*/.SQLITE
78240 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
78250 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 20  ite3PendingByte 
78260 3d 20 30 78 34 30 30 30 30 30 30 30 3b 0a 0a 2f  = 0x40000000;../
78270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
78280 6e 64 20 6f 66 20 67 6c 6f 62 61 6c 2e 63 20 2a  nd of global.c *
78290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
782a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
782b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
782c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
782d0 65 67 69 6e 20 66 69 6c 65 20 73 74 61 74 75 73  egin file status
782e0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
782f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
78310 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20 31  *.** 2008 June 1
78320 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  8.**.** The auth
78330 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
78340 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
78350 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
78360 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
78370 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
78380 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
78390 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
783a0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
783b0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
783c0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
783d0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
783e0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
783f0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
78400 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
78410 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
78420 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
78430 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
78440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78480 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75  .**.** This modu
78490 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  le implements th
784a0 65 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73  e sqlite3_status
784b0 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64  () interface and
784c0 20 72 65 6c 61 74 65 64 0a 2a 2a 20 66 75 6e 63   related.** func
784d0 74 69 6f 6e 61 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a  tionality..**.**
784e0 20 24 49 64 3a 20 73 74 61 74 75 73 2e 63 2c 76   $Id: status.c,v
784f0 20 31 2e 39 20 32 30 30 38 2f 30 39 2f 30 32 20   1.9 2008/09/02 
78500 30 30 3a 35 32 3a 35 32 20 64 72 68 20 45 78 70  00:52:52 drh Exp
78510 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 72   $.*/../*.** Var
78520 69 61 62 6c 65 73 20 69 6e 20 77 68 69 63 68 20  iables in which 
78530 74 6f 20 72 65 63 6f 72 64 20 73 74 61 74 75 73  to record status
78540 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
78550 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
78560 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 20  sqlite3StatType 
78570 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 3b  sqlite3StatType;
78580 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57  .static SQLITE_W
78590 53 44 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  SD struct sqlite
785a0 33 53 74 61 74 54 79 70 65 20 7b 0a 20 20 69 6e  3StatType {.  in
785b0 74 20 6e 6f 77 56 61 6c 75 65 5b 39 5d 3b 20 20  t nowValue[9];  
785c0 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
785d0 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74  t value */.  int
785e0 20 6d 78 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20   mxValue[9];    
785f0 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d        /* Maximum
78600 20 76 61 6c 75 65 20 2a 2f 0a 7d 20 73 71 6c 69   value */.} sqli
78610 74 65 33 53 74 61 74 20 3d 20 7b 20 7b 30 2c 7d  te3Stat = { {0,}
78620 2c 20 7b 30 2c 7d 20 7d 3b 0a 0a 0a 2f 2a 20 54  , {0,} };.../* T
78630 68 65 20 22 77 73 64 53 74 61 74 22 20 6d 61 63  he "wsdStat" mac
78640 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20  ro will resolve 
78650 74 6f 20 74 68 65 20 73 74 61 74 75 73 20 69 6e  to the status in
78660 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 74 61  formation.** sta
78670 74 65 20 76 65 63 74 6f 72 2e 20 20 49 66 20 77  te vector.  If w
78680 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64  ritable static d
78690 61 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74  ata is unsupport
786a0 65 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74  ed on the target
786b0 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20  ,.** we have to 
786c0 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65  locate the state
786d0 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74   vector at run-t
786e0 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72  ime.  In the mor
786f0 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65  e common.** case
78700 20 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 20   where writable 
78710 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 73  static data is s
78720 75 70 70 6f 72 74 65 64 2c 20 77 73 64 53 74 61  upported, wsdSta
78730 74 20 63 61 6e 20 72 65 66 65 72 20 64 69 72 65  t can refer dire
78740 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22  ctly.** to the "
78750 73 71 6c 69 74 65 33 53 74 61 74 22 20 73 74 61  sqlite3Stat" sta
78760 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72  te vector declar
78770 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66  ed above..*/.#if
78780 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
78790 57 53 44 0a 23 20 64 65 66 69 6e 65 20 77 73 64  WSD.# define wsd
787a0 53 74 61 74 49 6e 69 74 20 20 73 71 6c 69 74 65  StatInit  sqlite
787b0 33 53 74 61 74 54 79 70 65 20 2a 78 20 3d 20 26  3StatType *x = &
787c0 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33 53 74  GLOBAL(sqlite3St
787d0 61 74 54 79 70 65 2c 73 71 6c 69 74 65 33 53 74  atType,sqlite3St
787e0 61 74 29 0a 23 20 64 65 66 69 6e 65 20 77 73 64  at).# define wsd
787f0 53 74 61 74 20 78 5b 30 5d 0a 23 65 6c 73 65 0a  Stat x[0].#else.
78800 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74  # define wsdStat
78810 49 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 77 73  Init.# define ws
78820 64 53 74 61 74 20 73 71 6c 69 74 65 33 53 74 61  dStat sqlite3Sta
78830 74 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  t.#endif../*.** 
78840 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65  Return the curre
78850 6e 74 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74  nt value of a st
78860 61 74 75 73 20 70 61 72 61 6d 65 74 65 72 2e 0a  atus parameter..
78870 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
78880 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 61  E int sqlite3Sta
78890 74 75 73 56 61 6c 75 65 28 69 6e 74 20 6f 70 29  tusValue(int op)
788a0 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b  {.  wsdStatInit;
788b0 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d 30  .  assert( op>=0
788c0 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 65   && op<ArraySize
788d0 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75  (wsdStat.nowValu
788e0 65 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 77  e) );.  return w
788f0 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b  sdStat.nowValue[
78900 6f 70 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  op];.}../*.** Ad
78910 64 20 4e 20 74 6f 20 74 68 65 20 76 61 6c 75 65  d N to the value
78920 20 6f 66 20 61 20 73 74 61 74 75 73 20 72 65 63   of a status rec
78930 6f 72 64 2e 20 20 49 74 20 69 73 20 61 73 73 75  ord.  It is assu
78940 6d 65 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  med that the.** 
78950 63 61 6c 6c 65 72 20 68 6f 6c 64 73 20 61 70 70  caller holds app
78960 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 73 2e 0a  ropriate locks..
78970 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
78980 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74  E void sqlite3St
78990 61 74 75 73 41 64 64 28 69 6e 74 20 6f 70 2c 20  atusAdd(int op, 
789a0 69 6e 74 20 4e 29 7b 0a 20 20 77 73 64 53 74 61  int N){.  wsdSta
789b0 74 49 6e 69 74 3b 0a 20 20 61 73 73 65 72 74 28  tInit;.  assert(
789c0 20 6f 70 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72   op>=0 && op<Arr
789d0 61 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e 6e  aySize(wsdStat.n
789e0 6f 77 56 61 6c 75 65 29 20 29 3b 0a 20 20 77 73  owValue) );.  ws
789f0 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f  dStat.nowValue[o
78a00 70 5d 20 2b 3d 20 4e 3b 0a 20 20 69 66 28 20 77  p] += N;.  if( w
78a10 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b  sdStat.nowValue[
78a20 6f 70 5d 3e 77 73 64 53 74 61 74 2e 6d 78 56 61  op]>wsdStat.mxVa
78a30 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 20 20 20 77  lue[op] ){.    w
78a40 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f  sdStat.mxValue[o
78a50 70 5d 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77  p] = wsdStat.now
78a60 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d  Value[op];.  }.}
78a70 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
78a80 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75  value of a statu
78a90 73 20 74 6f 20 58 2e 0a 2a 2f 0a 53 51 4c 49 54  s to X..*/.SQLIT
78aa0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
78ab0 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28  qlite3StatusSet(
78ac0 69 6e 74 20 6f 70 2c 20 69 6e 74 20 58 29 7b 0a  int op, int X){.
78ad0 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a 20    wsdStatInit;. 
78ae0 20 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 26   assert( op>=0 &
78af0 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 77  & op<ArraySize(w
78b00 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29  sdStat.nowValue)
78b10 20 29 3b 0a 20 20 77 73 64 53 74 61 74 2e 6e 6f   );.  wsdStat.no
78b20 77 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 58 3b 0a  wValue[op] = X;.
78b30 20 20 69 66 28 20 77 73 64 53 74 61 74 2e 6e 6f    if( wsdStat.no
78b40 77 56 61 6c 75 65 5b 6f 70 5d 3e 77 73 64 53 74  wValue[op]>wsdSt
78b50 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 29  at.mxValue[op] )
78b60 7b 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78  {.    wsdStat.mx
78b70 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53  Value[op] = wsdS
78b80 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d  tat.nowValue[op]
78b90 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51  ;.  }.}../*.** Q
78ba0 75 65 72 79 20 73 74 61 74 75 73 20 69 6e 66 6f  uery status info
78bb0 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  rmation..**.** T
78bc0 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  his implementati
78bd0 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  on assumes that 
78be0 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69  reading or writi
78bf0 6e 67 20 61 6e 20 61 6c 69 67 6e 65 64 0a 2a 2a  ng an aligned.**
78c00 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20   32-bit integer 
78c10 69 73 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65  is an atomic ope
78c20 72 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 61 74  ration.  If that
78c30 20 61 73 73 75 6d 70 74 69 6f 6e 20 69 73 20 6e   assumption is n
78c40 6f 74 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e  ot true,.** then
78c50 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
78c60 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e   not threadsafe.
78c70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
78c80 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75  nt sqlite3_statu
78c90 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70  s(int op, int *p
78ca0 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48  Current, int *pH
78cb0 69 67 68 77 61 74 65 72 2c 20 69 6e 74 20 72 65  ighwater, int re
78cc0 73 65 74 46 6c 61 67 29 7b 0a 20 20 77 73 64 53  setFlag){.  wsdS
78cd0 74 61 74 49 6e 69 74 3b 0a 20 20 69 66 28 20 6f  tatInit;.  if( o
78ce0 70 3c 30 20 7c 7c 20 6f 70 3e 3d 41 72 72 61 79  p<0 || op>=Array
78cf0 53 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77  Size(wsdStat.now
78d00 56 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 72 65  Value) ){.    re
78d10 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
78d20 53 45 3b 0a 20 20 7d 0a 20 20 2a 70 43 75 72 72  SE;.  }.  *pCurr
78d30 65 6e 74 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f  ent = wsdStat.no
78d40 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 2a 70  wValue[op];.  *p
78d50 48 69 67 68 77 61 74 65 72 20 3d 20 77 73 64 53  Highwater = wsdS
78d60 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 3b  tat.mxValue[op];
78d70 0a 20 20 69 66 28 20 72 65 73 65 74 46 6c 61 67  .  if( resetFlag
78d80 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61 74 2e   ){.    wsdStat.
78d90 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73  mxValue[op] = ws
78da0 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f  dStat.nowValue[o
78db0 70 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  p];.  }.  return
78dc0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
78dd0 2a 0a 2a 2a 20 51 75 65 72 79 20 73 74 61 74 75  *.** Query statu
78de0 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  s information fo
78df0 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62  r a single datab
78e00 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a  ase connection.*
78e10 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
78e20 20 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74   sqlite3_db_stat
78e30 75 73 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  us(.  sqlite3 *d
78e40 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  b,          /* T
78e50 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
78e60 65 63 74 69 6f 6e 20 77 68 6f 73 65 20 73 74 61  ection whose sta
78e70 74 75 73 20 69 73 20 64 65 73 69 72 65 64 20 2a  tus is desired *
78e80 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20  /.  int op,     
78e90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61            /* Sta
78ea0 74 75 73 20 76 65 72 62 20 2a 2f 0a 20 20 69 6e  tus verb */.  in
78eb0 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 20 20 20  t *pCurrent,    
78ec0 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 75 72      /* Write cur
78ed0 72 65 6e 74 20 76 61 6c 75 65 20 68 65 72 65 20  rent value here 
78ee0 2a 2f 0a 20 20 69 6e 74 20 2a 70 48 69 67 68 77  */.  int *pHighw
78ef0 61 74 65 72 2c 20 20 20 20 20 20 2f 2a 20 57 72  ater,      /* Wr
78f00 69 74 65 20 68 69 67 68 2d 77 61 74 65 72 20 6d  ite high-water m
78f10 61 72 6b 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e  ark here */.  in
78f20 74 20 72 65 73 65 74 46 6c 61 67 20 20 20 20 20  t resetFlag     
78f30 20 20 20 20 2f 2a 20 52 65 73 65 74 20 68 69 67      /* Reset hig
78f40 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 69 66 20  h-water mark if 
78f50 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 73 77 69  true */.){.  swi
78f60 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63  tch( op ){.    c
78f70 61 73 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41  ase SQLITE_DBSTA
78f80 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53  TUS_LOOKASIDE_US
78f90 45 44 3a 20 7b 0a 20 20 20 20 20 20 2a 70 43 75  ED: {.      *pCu
78fa0 72 72 65 6e 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b  rrent = db->look
78fb0 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20  aside.nOut;.    
78fc0 20 20 2a 70 48 69 67 68 77 61 74 65 72 20 3d 20    *pHighwater = 
78fd0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78  db->lookaside.mx
78fe0 4f 75 74 3b 0a 20 20 20 20 20 20 69 66 28 20 72  Out;.      if( r
78ff0 65 73 65 74 46 6c 61 67 20 29 7b 0a 20 20 20 20  esetFlag ){.    
79000 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
79010 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f  e.mxOut = db->lo
79020 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20  okaside.nOut;.  
79030 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
79040 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
79050 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 65  ault: {.      re
79060 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
79070 52 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  R;.    }.  }.  r
79080 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
79090 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
790a0 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 74 61 74 75  *** End of statu
790b0 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s.c ************
790c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
790d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
790e0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
790f0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 64  *** Begin file d
79100 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ate.c **********
79110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79130 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4f  **/./*.** 2003 O
79140 63 74 6f 62 65 72 20 33 31 0a 2a 2a 0a 2a 2a 20  ctober 31.**.** 
79150 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
79160 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
79170 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
79180 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
79190 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
791a0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
791b0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
791c0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
791d0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
791e0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
791f0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
79200 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
79210 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
79220 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
79230 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
79240 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
79250 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
79260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
792a0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
792b0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74   file contains t
792c0 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74  he C functions t
792d0 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 64 61  hat implement da
792e0 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 66  te and time.** f
792f0 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 53 51 4c  unctions for SQL
79300 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65  ite.  .**.** The
79310 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65  re is only one e
79320 78 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c 20 69  xported symbol i
79330 6e 20 74 68 69 73 20 66 69 6c 65 20 2d 20 74 68  n this file - th
79340 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71  e function.** sq
79350 6c 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74  lite3RegisterDat
79360 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 29  eTimeFunctions()
79370 20 66 6f 75 6e 64 20 61 74 20 74 68 65 20 62 6f   found at the bo
79380 74 74 6f 6d 20 6f 66 20 74 68 65 20 66 69 6c 65  ttom of the file
79390 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63  ..** All other c
793a0 6f 64 65 20 68 61 73 20 66 69 6c 65 20 73 63 6f  ode has file sco
793b0 70 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64  pe..**.** $Id: d
793c0 61 74 65 2e 63 2c 76 20 31 2e 31 30 37 20 32 30  ate.c,v 1.107 20
793d0 30 39 2f 30 35 2f 30 33 20 32 30 3a 32 33 3a 35  09/05/03 20:23:5
793e0 33 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a  3 drh Exp $.**.*
793f0 2a 20 53 51 4c 69 74 65 20 70 72 6f 63 65 73 73  * SQLite process
79400 65 73 20 61 6c 6c 20 74 69 6d 65 73 20 61 6e 64  es all times and
79410 20 64 61 74 65 73 20 61 73 20 4a 75 6c 69 61 6e   dates as Julian
79420 20 44 61 79 20 6e 75 6d 62 65 72 73 2e 20 20 54   Day numbers.  T
79430 68 65 0a 2a 2a 20 64 61 74 65 73 20 61 6e 64 20  he.** dates and 
79440 74 69 6d 65 73 20 61 72 65 20 73 74 6f 72 65 64  times are stored
79450 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   as the number o
79460 66 20 64 61 79 73 20 73 69 6e 63 65 20 6e 6f 6f  f days since noo
79470 6e 0a 2a 2a 20 69 6e 20 47 72 65 65 6e 77 69 63  n.** in Greenwic
79480 68 20 6f 6e 20 4e 6f 76 65 6d 62 65 72 20 32 34  h on November 24
79490 2c 20 34 37 31 34 20 42 2e 43 2e 20 61 63 63 6f  , 4714 B.C. acco
794a0 72 64 69 6e 67 20 74 6f 20 74 68 65 20 47 72 65  rding to the Gre
794b0 67 6f 72 69 61 6e 0a 2a 2a 20 63 61 6c 65 6e 64  gorian.** calend
794c0 61 72 20 73 79 73 74 65 6d 2e 20 0a 2a 2a 0a 2a  ar system. .**.*
794d0 2a 20 31 39 37 30 2d 30 31 2d 30 31 20 30 30 3a  * 1970-01-01 00:
794e0 30 30 3a 30 30 20 69 73 20 4a 44 20 32 34 34 30  00:00 is JD 2440
794f0 35 38 37 2e 35 0a 2a 2a 20 32 30 30 30 2d 30 31  587.5.** 2000-01
79500 2d 30 31 20 30 30 3a 30 30 3a 30 30 20 69 73 20  -01 00:00:00 is 
79510 4a 44 20 32 34 35 31 35 34 34 2e 35 0a 2a 2a 0a  JD 2451544.5.**.
79520 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e  ** This implemen
79530 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20 79 65  tion requires ye
79540 61 72 73 20 74 6f 20 62 65 20 65 78 70 72 65 73  ars to be expres
79550 73 65 64 20 61 73 20 61 20 34 2d 64 69 67 69 74  sed as a 4-digit
79560 20 6e 75 6d 62 65 72 0a 2a 2a 20 77 68 69 63 68   number.** which
79570 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 6c 79   means that only
79580 20 64 61 74 65 73 20 62 65 74 77 65 65 6e 20 30   dates between 0
79590 30 30 30 2d 30 31 2d 30 31 20 61 6e 64 20 39 39  000-01-01 and 99
795a0 39 39 2d 31 32 2d 33 31 20 63 61 6e 0a 2a 2a 20  99-12-31 can.** 
795b0 62 65 20 72 65 70 72 65 73 65 6e 74 65 64 2c 20  be represented, 
795c0 65 76 65 6e 20 74 68 6f 75 67 68 20 6a 75 6c 69  even though juli
795d0 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 73 20 61  an day numbers a
795e0 6c 6c 6f 77 20 61 20 6d 75 63 68 20 77 69 64 65  llow a much wide
795f0 72 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 61  r.** range of da
79600 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 47  tes..**.** The G
79610 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61  regorian calenda
79620 72 20 73 79 73 74 65 6d 20 69 73 20 75 73 65 64  r system is used
79630 20 66 6f 72 20 61 6c 6c 20 64 61 74 65 73 20 61   for all dates a
79640 6e 64 20 74 69 6d 65 73 2c 0a 2a 2a 20 65 76 65  nd times,.** eve
79650 6e 20 74 68 6f 73 65 20 74 68 61 74 20 70 72 65  n those that pre
79660 64 61 74 65 20 74 68 65 20 47 72 65 67 6f 72 69  date the Gregori
79670 61 6e 20 63 61 6c 65 6e 64 61 72 2e 20 20 48 69  an calendar.  Hi
79680 73 74 6f 72 69 61 6e 73 20 75 73 75 61 6c 6c 79  storians usually
79690 0a 2a 2a 20 75 73 65 20 74 68 65 20 4a 75 6c 69  .** use the Juli
796a0 61 6e 20 63 61 6c 65 6e 64 61 72 20 66 6f 72 20  an calendar for 
796b0 64 61 74 65 73 20 70 72 69 6f 72 20 74 6f 20 31  dates prior to 1
796c0 35 38 32 2d 31 30 2d 31 35 20 61 6e 64 20 66 6f  582-10-15 and fo
796d0 72 20 73 6f 6d 65 0a 2a 2a 20 64 61 74 65 73 20  r some.** dates 
796e0 61 66 74 65 72 77 61 72 64 73 2c 20 64 65 70 65  afterwards, depe
796f0 6e 64 69 6e 67 20 6f 6e 20 6c 6f 63 61 6c 65 2e  nding on locale.
79700 20 20 42 65 77 61 72 65 20 6f 66 20 74 68 69 73    Beware of this
79710 20 64 69 66 66 65 72 65 6e 63 65 2e 0a 2a 2a 0a   difference..**.
79720 2a 2a 20 54 68 65 20 63 6f 6e 76 65 72 73 69 6f  ** The conversio
79730 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72 65  n algorithms are
79740 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 61 73   implemented bas
79750 65 64 20 6f 6e 20 64 65 73 63 72 69 70 74 69 6f  ed on descriptio
79760 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c  ns.** in the fol
79770 6c 6f 77 69 6e 67 20 74 65 78 74 3a 0a 2a 2a 0a  lowing text:.**.
79780 2a 2a 20 20 20 20 20 20 4a 65 61 6e 20 4d 65 65  **      Jean Mee
79790 75 73 0a 2a 2a 20 20 20 20 20 20 41 73 74 72 6f  us.**      Astro
797a0 6e 6f 6d 69 63 61 6c 20 41 6c 67 6f 72 69 74 68  nomical Algorith
797b0 6d 73 2c 20 32 6e 64 20 45 64 69 74 69 6f 6e 2c  ms, 2nd Edition,
797c0 20 31 39 39 38 0a 2a 2a 20 20 20 20 20 20 49 53   1998.**      IS
797d0 42 4d 20 30 2d 39 34 33 33 39 36 2d 36 31 2d 31  BM 0-943396-61-1
797e0 0a 2a 2a 20 20 20 20 20 20 57 69 6c 6c 6d 61 6e  .**      Willman
797f0 6e 2d 42 65 6c 6c 2c 20 49 6e 63 0a 2a 2a 20 20  n-Bell, Inc.**  
79800 20 20 20 20 52 69 63 68 6d 6f 6e 64 2c 20 56 69      Richmond, Vi
79810 72 67 69 6e 69 61 20 28 55 53 41 29 0a 2a 2f 0a  rginia (USA).*/.
79820 23 69 6e 63 6c 75 64 65 20 3c 74 69 6d 65 2e 68  #include <time.h
79830 3e 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  >..#ifndef SQLIT
79840 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f  E_OMIT_DATETIME_
79850 46 55 4e 43 53 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20  FUNCS../*.** On 
79860 72 65 63 65 6e 74 20 57 69 6e 64 6f 77 73 20 70  recent Windows p
79870 6c 61 74 66 6f 72 6d 73 2c 20 74 68 65 20 6c 6f  latforms, the lo
79880 63 61 6c 74 69 6d 65 5f 73 28 29 20 66 75 6e 63  caltime_s() func
79890 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c  tion is availabl
798a0 65 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20  e.** as part of 
798b0 74 68 65 20 22 53 65 63 75 72 65 20 43 52 54 22  the "Secure CRT"
798c0 2e 20 49 74 20 69 73 20 65 73 73 65 6e 74 69 61  . It is essentia
798d0 6c 6c 79 20 65 71 75 69 76 61 6c 65 6e 74 20 74  lly equivalent t
798e0 6f 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f  o .** localtime_
798f0 72 28 29 20 61 76 61 69 6c 61 62 6c 65 20 75 6e  r() available un
79900 64 65 72 20 6d 6f 73 74 20 50 4f 53 49 58 20 70  der most POSIX p
79910 6c 61 74 66 6f 72 6d 73 2c 20 65 78 63 65 70 74  latforms, except
79920 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 6f 72   that the .** or
79930 64 65 72 20 6f 66 20 74 68 65 20 70 61 72 61 6d  der of the param
79940 65 74 65 72 73 20 69 73 20 72 65 76 65 72 73 65  eters is reverse
79950 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 68 74 74  d..**.** See htt
79960 70 3a 2f 2f 6d 73 64 6e 2e 6d 69 63 72 6f 73 6f  p://msdn.microso
79970 66 74 2e 63 6f 6d 2f 65 6e 2d 75 73 2f 6c 69 62  ft.com/en-us/lib
79980 72 61 72 79 2f 61 34 34 32 78 33 79 65 28 56 53  rary/a442x3ye(VS
79990 2e 38 30 29 2e 61 73 70 78 2e 0a 2a 2a 0a 2a 2a  .80).aspx..**.**
799a0 20 49 66 20 74 68 65 20 75 73 65 72 20 68 61 73   If the user has
799b0 20 6e 6f 74 20 69 6e 64 69 63 61 74 65 64 20 74   not indicated t
799c0 6f 20 75 73 65 20 6c 6f 63 61 6c 74 69 6d 65 5f  o use localtime_
799d0 72 28 29 20 6f 72 20 6c 6f 63 61 6c 74 69 6d 65  r() or localtime
799e0 5f 73 28 29 0a 2a 2a 20 61 6c 72 65 61 64 79 2c  _s().** already,
799f0 20 63 68 65 63 6b 20 66 6f 72 20 61 6e 20 4d 53   check for an MS
79a00 56 43 20 62 75 69 6c 64 20 65 6e 76 69 72 6f 6e  VC build environ
79a10 6d 65 6e 74 20 74 68 61 74 20 70 72 6f 76 69 64  ment that provid
79a20 65 73 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65  es .** localtime
79a30 5f 73 28 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65  _s()..*/.#if !de
79a40 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c  fined(HAVE_LOCAL
79a50 54 49 4d 45 5f 52 29 20 26 26 20 21 64 65 66 69  TIME_R) && !defi
79a60 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49  ned(HAVE_LOCALTI
79a70 4d 45 5f 53 29 20 26 26 20 5c 0a 20 20 20 20 20  ME_S) && \.     
79a80 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52  defined(_MSC_VER
79a90 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 43 52  ) && defined(_CR
79aa0 54 5f 49 4e 53 45 43 55 52 45 5f 44 45 50 52 45  T_INSECURE_DEPRE
79ab0 43 41 54 45 29 0a 23 64 65 66 69 6e 65 20 48 41  CATE).#define HA
79ac0 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 20 31  VE_LOCALTIME_S 1
79ad0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41  .#endif../*.** A
79ae0 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 68   structure for h
79af0 6f 6c 64 69 6e 67 20 61 20 73 69 6e 67 6c 65 20  olding a single 
79b00 64 61 74 65 20 61 6e 64 20 74 69 6d 65 2e 0a 2a  date and time..*
79b10 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
79b20 20 44 61 74 65 54 69 6d 65 20 44 61 74 65 54 69   DateTime DateTi
79b30 6d 65 3b 0a 73 74 72 75 63 74 20 44 61 74 65 54  me;.struct DateT
79b40 69 6d 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  ime {.  sqlite3_
79b50 69 6e 74 36 34 20 69 4a 44 3b 20 2f 2a 20 54 68  int64 iJD; /* Th
79b60 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d  e julian day num
79b70 62 65 72 20 74 69 6d 65 73 20 38 36 34 30 30 30  ber times 864000
79b80 30 30 20 2a 2f 0a 20 20 69 6e 74 20 59 2c 20 4d  00 */.  int Y, M
79b90 2c 20 44 3b 20 20 20 20 20 20 20 2f 2a 20 59 65  , D;       /* Ye
79ba0 61 72 2c 20 6d 6f 6e 74 68 2c 20 61 6e 64 20 64  ar, month, and d
79bb0 61 79 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 6d  ay */.  int h, m
79bc0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f  ;          /* Ho
79bd0 75 72 20 61 6e 64 20 6d 69 6e 75 74 65 73 20 2a  ur and minutes *
79be0 2f 0a 20 20 69 6e 74 20 74 7a 3b 20 20 20 20 20  /.  int tz;     
79bf0 20 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 7a 6f         /* Timezo
79c00 6e 65 20 6f 66 66 73 65 74 20 69 6e 20 6d 69 6e  ne offset in min
79c10 75 74 65 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65  utes */.  double
79c20 20 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20   s;          /* 
79c30 53 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 63 68 61  Seconds */.  cha
79c40 72 20 76 61 6c 69 64 59 4d 44 3b 20 20 20 20 20  r validYMD;     
79c50 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 59  /* True (1) if Y
79c60 2c 4d 2c 44 20 61 72 65 20 76 61 6c 69 64 20 2a  ,M,D are valid *
79c70 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 48 4d  /.  char validHM
79c80 53 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28  S;     /* True (
79c90 31 29 20 69 66 20 68 2c 6d 2c 73 20 61 72 65 20  1) if h,m,s are 
79ca0 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72 20  valid */.  char 
79cb0 76 61 6c 69 64 4a 44 3b 20 20 20 20 20 20 2f 2a  validJD;      /*
79cc0 20 54 72 75 65 20 28 31 29 20 69 66 20 69 4a 44   True (1) if iJD
79cd0 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63   is valid */.  c
79ce0 68 61 72 20 76 61 6c 69 64 54 5a 3b 20 20 20 20  har validTZ;    
79cf0 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66    /* True (1) if
79d00 20 74 7a 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a   tz is valid */.
79d10 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  };.../*.** Conve
79d20 72 74 20 7a 44 61 74 65 20 69 6e 74 6f 20 6f 6e  rt zDate into on
79d30 65 20 6f 72 20 6d 6f 72 65 20 69 6e 74 65 67 65  e or more intege
79d40 72 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 20  rs.  Additional 
79d50 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 63 6f 6d  arguments.** com
79d60 65 20 69 6e 20 67 72 6f 75 70 73 20 6f 66 20 35  e in groups of 5
79d70 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
79d80 2a 2a 20 20 20 20 20 20 20 4e 20 20 20 20 20 20  **       N      
79d90 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 67 69 74   number of digit
79da0 73 20 69 6e 20 74 68 65 20 69 6e 74 65 67 65 72  s in the integer
79db0 0a 2a 2a 20 20 20 20 20 20 20 6d 69 6e 20 20 20  .**       min   
79dc0 20 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 65    minimum allowe
79dd0 64 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69  d value of the i
79de0 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20  nteger.**       
79df0 6d 61 78 20 20 20 20 20 6d 61 78 69 6d 75 6d 20  max     maximum 
79e00 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66  allowed value of
79e10 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20   the integer.** 
79e20 20 20 20 20 20 20 6e 65 78 74 43 20 20 20 66 69        nextC   fi
79e30 72 73 74 20 63 68 61 72 61 63 74 65 72 20 61 66  rst character af
79e40 74 65 72 20 74 68 65 20 69 6e 74 65 67 65 72 0a  ter the integer.
79e50 2a 2a 20 20 20 20 20 20 20 70 56 61 6c 20 20 20  **       pVal   
79e60 20 77 68 65 72 65 20 74 6f 20 77 72 69 74 65 20   where to write 
79e70 74 68 65 20 69 6e 74 65 67 65 72 73 20 76 61 6c  the integers val
79e80 75 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72  ue..**.** Conver
79e90 73 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75  sions continue u
79ea0 6e 74 69 6c 20 6f 6e 65 20 77 69 74 68 20 6e 65  ntil one with ne
79eb0 78 74 43 3d 3d 30 20 69 73 20 65 6e 63 6f 75 6e  xtC==0 is encoun
79ec0 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 75  tered..** The fu
79ed0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
79ee0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 75 63  he number of suc
79ef0 63 65 73 73 66 75 6c 20 63 6f 6e 76 65 72 73 69  cessful conversi
79f00 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ons..*/.static i
79f10 6e 74 20 67 65 74 44 69 67 69 74 73 28 63 6f 6e  nt getDigits(con
79f20 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20  st char *zDate, 
79f30 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
79f40 61 70 3b 0a 20 20 69 6e 74 20 76 61 6c 3b 0a 20  ap;.  int val;. 
79f50 20 69 6e 74 20 4e 3b 0a 20 20 69 6e 74 20 6d 69   int N;.  int mi
79f60 6e 3b 0a 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20  n;.  int max;.  
79f70 69 6e 74 20 6e 65 78 74 43 3b 0a 20 20 69 6e 74  int nextC;.  int
79f80 20 2a 70 56 61 6c 3b 0a 20 20 69 6e 74 20 63 6e   *pVal;.  int cn
79f90 74 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 72  t = 0;.  va_star
79fa0 74 28 61 70 2c 20 7a 44 61 74 65 29 3b 0a 20 20  t(ap, zDate);.  
79fb0 64 6f 7b 0a 20 20 20 20 4e 20 3d 20 76 61 5f 61  do{.    N = va_a
79fc0 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20  rg(ap, int);.   
79fd0 20 6d 69 6e 20 3d 20 76 61 5f 61 72 67 28 61 70   min = va_arg(ap
79fe0 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6d 61 78 20  , int);.    max 
79ff0 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
7a000 29 3b 0a 20 20 20 20 6e 65 78 74 43 20 3d 20 76  );.    nextC = v
7a010 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a  a_arg(ap, int);.
7a020 20 20 20 20 70 56 61 6c 20 3d 20 76 61 5f 61 72      pVal = va_ar
7a030 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20  g(ap, int*);.   
7a040 20 76 61 6c 20 3d 20 30 3b 0a 20 20 20 20 77 68   val = 0;.    wh
7a050 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20  ile( N-- ){.    
7a060 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73    if( !sqlite3Is
7a070 64 69 67 69 74 28 2a 7a 44 61 74 65 29 20 29 7b  digit(*zDate) ){
7a080 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e  .        goto en
7a090 64 5f 67 65 74 44 69 67 69 74 73 3b 0a 20 20 20  d_getDigits;.   
7a0a0 20 20 20 7d 0a 20 20 20 20 20 20 76 61 6c 20 3d     }.      val =
7a0b0 20 76 61 6c 2a 31 30 20 2b 20 2a 7a 44 61 74 65   val*10 + *zDate
7a0c0 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 7a 44   - '0';.      zD
7a0d0 61 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20  ate++;.    }.   
7a0e0 20 69 66 28 20 76 61 6c 3c 6d 69 6e 20 7c 7c 20   if( val<min || 
7a0f0 76 61 6c 3e 6d 61 78 20 7c 7c 20 28 6e 65 78 74  val>max || (next
7a100 43 21 3d 30 20 26 26 20 6e 65 78 74 43 21 3d 2a  C!=0 && nextC!=*
7a110 7a 44 61 74 65 29 20 29 7b 0a 20 20 20 20 20 20  zDate) ){.      
7a120 67 6f 74 6f 20 65 6e 64 5f 67 65 74 44 69 67 69  goto end_getDigi
7a130 74 73 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70  ts;.    }.    *p
7a140 56 61 6c 20 3d 20 76 61 6c 3b 0a 20 20 20 20 7a  Val = val;.    z
7a150 44 61 74 65 2b 2b 3b 0a 20 20 20 20 63 6e 74 2b  Date++;.    cnt+
7a160 2b 3b 0a 20 20 7d 77 68 69 6c 65 28 20 6e 65 78  +;.  }while( nex
7a170 74 43 20 29 3b 0a 65 6e 64 5f 67 65 74 44 69 67  tC );.end_getDig
7a180 69 74 73 3a 0a 20 20 76 61 5f 65 6e 64 28 61 70  its:.  va_end(ap
7a190 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6e 74 3b  );.  return cnt;
7a1a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74  .}../*.** Read t
7a1b0 65 78 74 20 66 72 6f 6d 20 7a 5b 5d 20 61 6e 64  ext from z[] and
7a1c0 20 63 6f 6e 76 65 72 74 20 69 6e 74 6f 20 61 20   convert into a 
7a1d0 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e  floating point n
7a1e0 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a  umber.  Return.*
7a1f0 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
7a200 64 69 67 69 74 73 20 63 6f 6e 76 65 72 74 65 64  digits converted
7a210 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74  ..*/.#define get
7a220 56 61 6c 75 65 20 73 71 6c 69 74 65 33 41 74 6f  Value sqlite3Ato
7a230 46 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 61  F../*.** Parse a
7a240 20 74 69 6d 65 7a 6f 6e 65 20 65 78 74 65 6e 73   timezone extens
7a250 69 6f 6e 20 6f 6e 20 74 68 65 20 65 6e 64 20 6f  ion on the end o
7a260 66 20 61 20 64 61 74 65 2d 74 69 6d 65 2e 0a 2a  f a date-time..*
7a270 2a 20 54 68 65 20 65 78 74 65 6e 73 69 6f 6e 20  * The extension 
7a280 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a  is of the form:.
7a290 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 28 2b 2f  **.**        (+/
7a2a0 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a 2a 2a 20 4f 72  -)HH:MM.**.** Or
7a2b0 20 74 68 65 20 22 7a 75 6c 75 22 20 6e 6f 74 61   the "zulu" nota
7a2c0 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  tion:.**.**     
7a2d0 20 20 20 5a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68     Z.**.** If th
7a2e0 65 20 70 61 72 73 65 20 69 73 20 73 75 63 63 65  e parse is succe
7a2f0 73 73 66 75 6c 2c 20 77 72 69 74 65 20 74 68 65  ssful, write the
7a300 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6e 75 74   number of minut
7a310 65 73 0a 2a 2a 20 6f 66 20 63 68 61 6e 67 65 20  es.** of change 
7a320 69 6e 20 70 2d 3e 74 7a 20 61 6e 64 20 72 65 74  in p->tz and ret
7a330 75 72 6e 20 30 2e 20 20 49 66 20 61 20 70 61 72  urn 0.  If a par
7a340 73 65 72 20 65 72 72 6f 72 20 6f 63 63 75 72 73  ser error occurs
7a350 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f 6e 2d  ,.** return non-
7a360 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 69  zero..**.** A mi
7a370 73 73 69 6e 67 20 73 70 65 63 69 66 69 65 72 20  ssing specifier 
7a380 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65  is not considere
7a390 64 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73  d an error..*/.s
7a3a0 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 54  tatic int parseT
7a3b0 69 6d 65 7a 6f 6e 65 28 63 6f 6e 73 74 20 63 68  imezone(const ch
7a3c0 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54  ar *zDate, DateT
7a3d0 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 73  ime *p){.  int s
7a3e0 67 6e 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 48  gn = 0;.  int nH
7a3f0 72 2c 20 6e 4d 6e 3b 0a 20 20 69 6e 74 20 63 3b  r, nMn;.  int c;
7a400 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65  .  while( sqlite
7a410 33 49 73 73 70 61 63 65 28 2a 7a 44 61 74 65 29  3Isspace(*zDate)
7a420 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20   ){ zDate++; }. 
7a430 20 70 2d 3e 74 7a 20 3d 20 30 3b 0a 20 20 63 20   p->tz = 0;.  c 
7a440 3d 20 2a 7a 44 61 74 65 3b 0a 20 20 69 66 28 20  = *zDate;.  if( 
7a450 63 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 73 67  c=='-' ){.    sg
7a460 6e 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 20  n = -1;.  }else 
7a470 69 66 28 20 63 3d 3d 27 2b 27 20 29 7b 0a 20 20  if( c=='+' ){.  
7a480 20 20 73 67 6e 20 3d 20 2b 31 3b 0a 20 20 7d 65    sgn = +1;.  }e
7a490 6c 73 65 20 69 66 28 20 63 3d 3d 27 5a 27 20 7c  lse if( c=='Z' |
7a4a0 7c 20 63 3d 3d 27 7a 27 20 29 7b 0a 20 20 20 20  | c=='z' ){.    
7a4b0 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 67 6f 74  zDate++;.    got
7a4c0 6f 20 7a 75 6c 75 5f 74 69 6d 65 3b 0a 20 20 7d  o zulu_time;.  }
7a4d0 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
7a4e0 20 63 21 3d 30 3b 0a 20 20 7d 0a 20 20 7a 44 61   c!=0;.  }.  zDa
7a4f0 74 65 2b 2b 3b 0a 20 20 69 66 28 20 67 65 74 44  te++;.  if( getD
7a500 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c 20  igits(zDate, 2, 
7a510 30 2c 20 31 34 2c 20 27 3a 27 2c 20 26 6e 48 72  0, 14, ':', &nHr
7a520 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26  , 2, 0, 59, 0, &
7a530 6e 4d 6e 29 21 3d 32 20 29 7b 0a 20 20 20 20 72  nMn)!=2 ){.    r
7a540 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a  eturn 1;.  }.  z
7a550 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20 70 2d 3e  Date += 5;.  p->
7a560 74 7a 20 3d 20 73 67 6e 2a 28 6e 4d 6e 20 2b 20  tz = sgn*(nMn + 
7a570 6e 48 72 2a 36 30 29 3b 0a 7a 75 6c 75 5f 74 69  nHr*60);.zulu_ti
7a580 6d 65 3a 0a 20 20 77 68 69 6c 65 28 20 73 71 6c  me:.  while( sql
7a590 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 44 61  ite3Isspace(*zDa
7a5a0 74 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20  te) ){ zDate++; 
7a5b0 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 44 61 74  }.  return *zDat
7a5c0 65 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  e!=0;.}../*.** P
7a5d0 61 72 73 65 20 74 69 6d 65 73 20 6f 66 20 74 68  arse times of th
7a5e0 65 20 66 6f 72 6d 20 48 48 3a 4d 4d 20 6f 72 20  e form HH:MM or 
7a5f0 48 48 3a 4d 4d 3a 53 53 20 6f 72 20 48 48 3a 4d  HH:MM:SS or HH:M
7a600 4d 3a 53 53 2e 46 46 46 46 2e 0a 2a 2a 20 54 68  M:SS.FFFF..** Th
7a610 65 20 48 48 2c 20 4d 4d 2c 20 61 6e 64 20 53 53  e HH, MM, and SS
7a620 20 6d 75 73 74 20 65 61 63 68 20 62 65 20 65 78   must each be ex
7a630 61 63 74 6c 79 20 32 20 64 69 67 69 74 73 2e 20  actly 2 digits. 
7a640 20 54 68 65 0a 2a 2a 20 66 72 61 63 74 69 6f 6e   The.** fraction
7a650 61 6c 20 73 65 63 6f 6e 64 73 20 46 46 46 46 20  al seconds FFFF 
7a660 63 61 6e 20 62 65 20 6f 6e 65 20 6f 72 20 6d 6f  can be one or mo
7a670 72 65 20 64 69 67 69 74 73 2e 0a 2a 2a 0a 2a 2a  re digits..**.**
7a680 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   Return 1 if the
7a690 72 65 20 69 73 20 61 20 70 61 72 73 69 6e 67 20  re is a parsing 
7a6a0 65 72 72 6f 72 20 61 6e 64 20 30 20 6f 6e 20 73  error and 0 on s
7a6b0 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69  uccess..*/.stati
7a6c0 63 20 69 6e 74 20 70 61 72 73 65 48 68 4d 6d 53  c int parseHhMmS
7a6d0 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  s(const char *zD
7a6e0 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70  ate, DateTime *p
7a6f0 29 7b 0a 20 20 69 6e 74 20 68 2c 20 6d 2c 20 73  ){.  int h, m, s
7a700 3b 0a 20 20 64 6f 75 62 6c 65 20 6d 73 20 3d 20  ;.  double ms = 
7a710 30 2e 30 3b 0a 20 20 69 66 28 20 67 65 74 44 69  0.0;.  if( getDi
7a720 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30  gits(zDate, 2, 0
7a730 2c 20 32 34 2c 20 27 3a 27 2c 20 26 68 2c 20 32  , 24, ':', &h, 2
7a740 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 6d 29 21  , 0, 59, 0, &m)!
7a750 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =2 ){.    return
7a760 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 20   1;.  }.  zDate 
7a770 2b 3d 20 35 3b 0a 20 20 69 66 28 20 2a 7a 44 61  += 5;.  if( *zDa
7a780 74 65 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 7a  te==':' ){.    z
7a790 44 61 74 65 2b 2b 3b 0a 20 20 20 20 69 66 28 20  Date++;.    if( 
7a7a0 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c  getDigits(zDate,
7a7b0 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 73   2, 0, 59, 0, &s
7a7c0 29 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 65  )!=1 ){.      re
7a7d0 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
7a7e0 20 20 7a 44 61 74 65 20 2b 3d 20 32 3b 0a 20 20    zDate += 2;.  
7a7f0 20 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 27 2e    if( *zDate=='.
7a800 27 20 26 26 20 73 71 6c 69 74 65 33 49 73 64 69  ' && sqlite3Isdi
7a810 67 69 74 28 7a 44 61 74 65 5b 31 5d 29 20 29 7b  git(zDate[1]) ){
7a820 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 53  .      double rS
7a830 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 20 20 20  cale = 1.0;.    
7a840 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 20    zDate++;.     
7a850 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49   while( sqlite3I
7a860 73 64 69 67 69 74 28 2a 7a 44 61 74 65 29 20 29  sdigit(*zDate) )
7a870 7b 0a 20 20 20 20 20 20 20 20 6d 73 20 3d 20 6d  {.        ms = m
7a880 73 2a 31 30 2e 30 20 2b 20 2a 7a 44 61 74 65 20  s*10.0 + *zDate 
7a890 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 72  - '0';.        r
7a8a0 53 63 61 6c 65 20 2a 3d 20 31 30 2e 30 3b 0a 20  Scale *= 10.0;. 
7a8b0 20 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a         zDate++;.
7a8c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 73        }.      ms
7a8d0 20 2f 3d 20 72 53 63 61 6c 65 3b 0a 20 20 20 20   /= rScale;.    
7a8e0 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  }.  }else{.    s
7a8f0 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76   = 0;.  }.  p->v
7a900 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 70 2d  alidJD = 0;.  p-
7a910 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20  >validHMS = 1;. 
7a920 20 70 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 2d 3e   p->h = h;.  p->
7a930 6d 20 3d 20 6d 3b 0a 20 20 70 2d 3e 73 20 3d 20  m = m;.  p->s = 
7a940 73 20 2b 20 6d 73 3b 0a 20 20 69 66 28 20 70 61  s + ms;.  if( pa
7a950 72 73 65 54 69 6d 65 7a 6f 6e 65 28 7a 44 61 74  rseTimezone(zDat
7a960 65 2c 20 70 29 20 29 20 72 65 74 75 72 6e 20 31  e, p) ) return 1
7a970 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d  ;.  p->validTZ =
7a980 20 28 70 2d 3e 74 7a 21 3d 30 29 3f 31 3a 30 3b   (p->tz!=0)?1:0;
7a990 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
7a9a0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 66 72  /*.** Convert fr
7a9b0 6f 6d 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48  om YYYY-MM-DD HH
7a9c0 3a 4d 4d 3a 53 53 20 74 6f 20 6a 75 6c 69 61 6e  :MM:SS to julian
7a9d0 20 64 61 79 2e 20 20 57 65 20 61 6c 77 61 79 73   day.  We always
7a9e0 20 61 73 73 75 6d 65 0a 2a 2a 20 74 68 61 74 20   assume.** that 
7a9f0 74 68 65 20 59 59 59 59 2d 4d 4d 2d 44 44 20 69  the YYYY-MM-DD i
7aa00 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74  s according to t
7aa10 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c  he Gregorian cal
7aa20 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 66  endar..**.** Ref
7aa30 65 72 65 6e 63 65 3a 20 20 4d 65 65 75 73 20 70  erence:  Meeus p
7aa40 61 67 65 20 36 31 0a 2a 2f 0a 73 74 61 74 69 63  age 61.*/.static
7aa50 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 4a 44 28   void computeJD(
7aa60 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20  DateTime *p){.  
7aa70 69 6e 74 20 59 2c 20 4d 2c 20 44 2c 20 41 2c 20  int Y, M, D, A, 
7aa80 42 2c 20 58 31 2c 20 58 32 3b 0a 0a 20 20 69 66  B, X1, X2;..  if
7aa90 28 20 70 2d 3e 76 61 6c 69 64 4a 44 20 29 20 72  ( p->validJD ) r
7aaa0 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e  eturn;.  if( p->
7aab0 76 61 6c 69 64 59 4d 44 20 29 7b 0a 20 20 20 20  validYMD ){.    
7aac0 59 20 3d 20 70 2d 3e 59 3b 0a 20 20 20 20 4d 20  Y = p->Y;.    M 
7aad0 3d 20 70 2d 3e 4d 3b 0a 20 20 20 20 44 20 3d 20  = p->M;.    D = 
7aae0 70 2d 3e 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  p->D;.  }else{. 
7aaf0 20 20 20 59 20 3d 20 32 30 30 30 3b 20 20 2f 2a     Y = 2000;  /*
7ab00 20 49 66 20 6e 6f 20 59 4d 44 20 73 70 65 63 69   If no YMD speci
7ab10 66 69 65 64 2c 20 61 73 73 75 6d 65 20 32 30 30  fied, assume 200
7ab20 30 2d 4a 61 6e 2d 30 31 20 2a 2f 0a 20 20 20 20  0-Jan-01 */.    
7ab30 4d 20 3d 20 31 3b 0a 20 20 20 20 44 20 3d 20 31  M = 1;.    D = 1
7ab40 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4d 3c 3d 32  ;.  }.  if( M<=2
7ab50 20 29 7b 0a 20 20 20 20 59 2d 2d 3b 0a 20 20 20   ){.    Y--;.   
7ab60 20 4d 20 2b 3d 20 31 32 3b 0a 20 20 7d 0a 20 20   M += 12;.  }.  
7ab70 41 20 3d 20 59 2f 31 30 30 3b 0a 20 20 42 20 3d  A = Y/100;.  B =
7ab80 20 32 20 2d 20 41 20 2b 20 28 41 2f 34 29 3b 0a   2 - A + (A/4);.
7ab90 20 20 58 31 20 3d 20 33 36 35 32 35 2a 28 59 2b    X1 = 36525*(Y+
7aba0 34 37 31 36 29 2f 31 30 30 3b 0a 20 20 58 32 20  4716)/100;.  X2 
7abb0 3d 20 33 30 36 30 30 31 2a 28 4d 2b 31 29 2f 31  = 306001*(M+1)/1
7abc0 30 30 30 30 3b 0a 20 20 70 2d 3e 69 4a 44 20 3d  0000;.  p->iJD =
7abd0 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29   (sqlite3_int64)
7abe0 28 28 58 31 20 2b 20 58 32 20 2b 20 44 20 2b 20  ((X1 + X2 + D + 
7abf0 42 20 2d 20 31 35 32 34 2e 35 20 29 20 2a 20 38  B - 1524.5 ) * 8
7ac00 36 34 30 30 30 30 30 29 3b 0a 20 20 70 2d 3e 76  6400000);.  p->v
7ac10 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 69 66  alidJD = 1;.  if
7ac20 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29 7b  ( p->validHMS ){
7ac30 0a 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 70  .    p->iJD += p
7ac40 2d 3e 68 2a 33 36 30 30 30 30 30 20 2b 20 70 2d  ->h*3600000 + p-
7ac50 3e 6d 2a 36 30 30 30 30 20 2b 20 28 73 71 6c 69  >m*60000 + (sqli
7ac60 74 65 33 5f 69 6e 74 36 34 29 28 70 2d 3e 73 2a  te3_int64)(p->s*
7ac70 31 30 30 30 29 3b 0a 20 20 20 20 69 66 28 20 70  1000);.    if( p
7ac80 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20  ->validTZ ){.   
7ac90 20 20 20 70 2d 3e 69 4a 44 20 2d 3d 20 70 2d 3e     p->iJD -= p->
7aca0 74 7a 2a 36 30 30 30 30 3b 0a 20 20 20 20 20 20  tz*60000;.      
7acb0 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 30 3b  p->validYMD = 0;
7acc0 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48  .      p->validH
7acd0 4d 53 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d  MS = 0;.      p-
7ace0 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20  >validTZ = 0;.  
7acf0 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
7ad00 20 50 61 72 73 65 20 64 61 74 65 73 20 6f 66 20   Parse dates of 
7ad10 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20  the form.**.**  
7ad20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48     YYYY-MM-DD HH
7ad30 3a 4d 4d 3a 53 53 2e 46 46 46 0a 2a 2a 20 20 20  :MM:SS.FFF.**   
7ad40 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a    YYYY-MM-DD HH:
7ad50 4d 4d 3a 53 53 0a 2a 2a 20 20 20 20 20 59 59 59  MM:SS.**     YYY
7ad60 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 0a 2a 2a  Y-MM-DD HH:MM.**
7ad70 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 0a       YYYY-MM-DD.
7ad80 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20  **.** Write the 
7ad90 72 65 73 75 6c 74 20 69 6e 74 6f 20 74 68 65 20  result into the 
7ada0 44 61 74 65 54 69 6d 65 20 73 74 72 75 63 74 75  DateTime structu
7adb0 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20 30 0a  re and return 0.
7adc0 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e  ** on success an
7add0 64 20 31 20 69 66 20 74 68 65 20 69 6e 70 75 74  d 1 if the input
7ade0 20 73 74 72 69 6e 67 20 69 73 20 6e 6f 74 20 61   string is not a
7adf0 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20   well-formed.** 
7ae00 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  date..*/.static 
7ae10 69 6e 74 20 70 61 72 73 65 59 79 79 79 4d 6d 44  int parseYyyyMmD
7ae20 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  d(const char *zD
7ae30 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70  ate, DateTime *p
7ae40 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44  ){.  int Y, M, D
7ae50 2c 20 6e 65 67 3b 0a 0a 20 20 69 66 28 20 7a 44  , neg;..  if( zD
7ae60 61 74 65 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20  ate[0]=='-' ){. 
7ae70 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20     zDate++;.    
7ae80 6e 65 67 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  neg = 1;.  }else
7ae90 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20  {.    neg = 0;. 
7aea0 20 7d 0a 20 20 69 66 28 20 67 65 74 44 69 67 69   }.  if( getDigi
7aeb0 74 73 28 7a 44 61 74 65 2c 34 2c 30 2c 39 39 39  ts(zDate,4,0,999
7aec0 39 2c 27 2d 27 2c 26 59 2c 32 2c 31 2c 31 32 2c  9,'-',&Y,2,1,12,
7aed0 27 2d 27 2c 26 4d 2c 32 2c 31 2c 33 31 2c 30 2c  '-',&M,2,1,31,0,
7aee0 26 44 29 21 3d 33 20 29 7b 0a 20 20 20 20 72 65  &D)!=3 ){.    re
7aef0 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44  turn 1;.  }.  zD
7af00 61 74 65 20 2b 3d 20 31 30 3b 0a 20 20 77 68 69  ate += 10;.  whi
7af10 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61  le( sqlite3Isspa
7af20 63 65 28 2a 7a 44 61 74 65 29 20 7c 7c 20 27 54  ce(*zDate) || 'T
7af30 27 3d 3d 2a 28 75 38 2a 29 7a 44 61 74 65 20 29  '==*(u8*)zDate )
7af40 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 69  { zDate++; }.  i
7af50 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a  f( parseHhMmSs(z
7af60 44 61 74 65 2c 20 70 29 3d 3d 30 20 29 7b 0a 20  Date, p)==0 ){. 
7af70 20 20 20 2f 2a 20 57 65 20 67 6f 74 20 74 68 65     /* We got the
7af80 20 74 69 6d 65 20 2a 2f 0a 20 20 7d 65 6c 73 65   time */.  }else
7af90 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 30 20 29   if( *zDate==0 )
7afa0 7b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d  {.    p->validHM
7afb0 53 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  S = 0;.  }else{.
7afc0 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
7afd0 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d  }.  p->validJD =
7afe0 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d   0;.  p->validYM
7aff0 44 20 3d 20 31 3b 0a 20 20 70 2d 3e 59 20 3d 20  D = 1;.  p->Y = 
7b000 6e 65 67 20 3f 20 2d 59 20 3a 20 59 3b 0a 20 20  neg ? -Y : Y;.  
7b010 70 2d 3e 4d 20 3d 20 4d 3b 0a 20 20 70 2d 3e 44  p->M = M;.  p->D
7b020 20 3d 20 44 3b 0a 20 20 69 66 28 20 70 2d 3e 76   = D;.  if( p->v
7b030 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20 20 63 6f  alidTZ ){.    co
7b040 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 7d 0a  mputeJD(p);.  }.
7b050 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
7b060 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74 69 6d  *.** Set the tim
7b070 65 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74  e to the current
7b080 20 74 69 6d 65 20 72 65 70 6f 72 74 65 64 20 62   time reported b
7b090 79 20 74 68 65 20 56 46 53 0a 2a 2f 0a 73 74 61  y the VFS.*/.sta
7b0a0 74 69 63 20 76 6f 69 64 20 73 65 74 44 61 74 65  tic void setDate
7b0b0 54 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 73 71  TimeToCurrent(sq
7b0c0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
7b0d0 6f 6e 74 65 78 74 2c 20 44 61 74 65 54 69 6d 65  ontext, DateTime
7b0e0 20 2a 70 29 7b 0a 20 20 64 6f 75 62 6c 65 20 72   *p){.  double r
7b0f0 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
7b100 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  = sqlite3_contex
7b110 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
7b120 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f  ext);.  sqlite3O
7b130 73 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d  sCurrentTime(db-
7b140 3e 70 56 66 73 2c 20 26 72 29 3b 0a 20 20 70 2d  >pVfs, &r);.  p-
7b150 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f  >iJD = (sqlite3_
7b160 69 6e 74 36 34 29 28 72 2a 38 36 34 30 30 30 30  int64)(r*8640000
7b170 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 70 2d  0.0 + 0.5);.  p-
7b180 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 7d 0a  >validJD = 1;.}.
7b190 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74  ./*.** Attempt t
7b1a0 6f 20 70 61 72 73 65 20 74 68 65 20 67 69 76 65  o parse the give
7b1b0 6e 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20  n string into a 
7b1c0 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75 6d 62 65  Julian Day Numbe
7b1d0 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68  r.  Return.** th
7b1e0 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f  e number of erro
7b1f0 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  rs..**.** The fo
7b200 6c 6c 6f 77 69 6e 67 20 61 72 65 20 61 63 63 65  llowing are acce
7b210 70 74 61 62 6c 65 20 66 6f 72 6d 73 20 66 6f 72  ptable forms for
7b220 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e   the input strin
7b230 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 59  g:.**.**      YY
7b240 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53  YY-MM-DD HH:MM:S
7b250 53 2e 46 46 46 20 20 2b 2f 2d 48 48 3a 4d 4d 0a  S.FFF  +/-HH:MM.
7b260 2a 2a 20 20 20 20 20 20 44 44 44 44 2e 44 44 20  **      DDDD.DD 
7b270 0a 2a 2a 20 20 20 20 20 20 6e 6f 77 0a 2a 2a 0a  .**      now.**.
7b280 2a 2a 20 49 6e 20 74 68 65 20 66 69 72 73 74 20  ** In the first 
7b290 66 6f 72 6d 2c 20 74 68 65 20 2b 2f 2d 48 48 3a  form, the +/-HH:
7b2a0 4d 4d 20 69 73 20 61 6c 77 61 79 73 20 6f 70 74  MM is always opt
7b2b0 69 6f 6e 61 6c 2e 20 20 54 68 65 20 66 72 61 63  ional.  The frac
7b2c0 74 69 6f 6e 61 6c 0a 2a 2a 20 73 65 63 6f 6e 64  tional.** second
7b2d0 73 20 65 78 74 65 6e 73 69 6f 6e 20 28 74 68 65  s extension (the
7b2e0 20 22 2e 46 46 46 22 29 20 69 73 20 6f 70 74 69   ".FFF") is opti
7b2f0 6f 6e 61 6c 2e 20 20 54 68 65 20 73 65 63 6f 6e  onal.  The secon
7b300 64 73 20 70 6f 72 74 69 6f 6e 0a 2a 2a 20 28 22  ds portion.** ("
7b310 3a 53 53 2e 46 46 46 22 29 20 69 73 20 6f 70 74  :SS.FFF") is opt
7b320 69 6f 6e 2e 20 20 54 68 65 20 79 65 61 72 20 61  ion.  The year a
7b330 6e 64 20 64 61 74 65 20 63 61 6e 20 62 65 20 6f  nd date can be o
7b340 6d 69 74 74 65 64 20 61 73 20 6c 6f 6e 67 0a 2a  mitted as long.*
7b350 2a 20 61 73 20 74 68 65 72 65 20 69 73 20 61 20  * as there is a 
7b360 74 69 6d 65 20 73 74 72 69 6e 67 2e 20 20 54 68  time string.  Th
7b370 65 20 74 69 6d 65 20 73 74 72 69 6e 67 20 63 61  e time string ca
7b380 6e 20 62 65 20 6f 6d 69 74 74 65 64 20 61 73 20  n be omitted as 
7b390 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 65  long.** as there
7b3a0 20 69 73 20 61 20 79 65 61 72 20 61 6e 64 20 64   is a year and d
7b3b0 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ate..*/.static i
7b3c0 6e 74 20 70 61 72 73 65 44 61 74 65 4f 72 54 69  nt parseDateOrTi
7b3d0 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  me(.  sqlite3_co
7b3e0 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
7b3f0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
7b400 44 61 74 65 2c 20 0a 20 20 44 61 74 65 54 69 6d  Date, .  DateTim
7b410 65 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 20 69 73  e *p.){.  int is
7b420 52 65 61 6c 4e 75 6d 3b 20 20 20 20 2f 2a 20 52  RealNum;    /* R
7b430 65 74 75 72 6e 20 66 72 6f 6d 20 73 71 6c 69 74  eturn from sqlit
7b440 65 33 49 73 4e 75 6d 62 65 72 28 29 2e 20 20 4e  e3IsNumber().  N
7b450 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 69 66 28  ot used */.  if(
7b460 20 70 61 72 73 65 59 79 79 79 4d 6d 44 64 28 7a   parseYyyyMmDd(z
7b470 44 61 74 65 2c 70 29 3d 3d 30 20 29 7b 0a 20 20  Date,p)==0 ){.  
7b480 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65    return 0;.  }e
7b490 6c 73 65 20 69 66 28 20 70 61 72 73 65 48 68 4d  lse if( parseHhM
7b4a0 6d 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30  mSs(zDate, p)==0
7b4b0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
7b4c0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71  ;.  }else if( sq
7b4d0 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 61  lite3StrICmp(zDa
7b4e0 74 65 2c 22 6e 6f 77 22 29 3d 3d 30 29 7b 0a 20  te,"now")==0){. 
7b4f0 20 20 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f     setDateTimeTo
7b500 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 2c  Current(context,
7b510 20 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20   p);.    return 
7b520 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  0;.  }else if( s
7b530 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 7a  qlite3IsNumber(z
7b540 44 61 74 65 2c 20 26 69 73 52 65 61 6c 4e 75 6d  Date, &isRealNum
7b550 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 20 29  , SQLITE_UTF8) )
7b560 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 3b 0a  {.    double r;.
7b570 20 20 20 20 67 65 74 56 61 6c 75 65 28 7a 44 61      getValue(zDa
7b580 74 65 2c 20 26 72 29 3b 0a 20 20 20 20 70 2d 3e  te, &r);.    p->
7b590 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69  iJD = (sqlite3_i
7b5a0 6e 74 36 34 29 28 72 2a 38 36 34 30 30 30 30 30  nt64)(r*86400000
7b5b0 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 20 20 70  .0 + 0.5);.    p
7b5c0 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20  ->validJD = 1;. 
7b5d0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
7b5e0 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
7b5f0 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68  /*.** Compute th
7b600 65 20 59 65 61 72 2c 20 4d 6f 6e 74 68 2c 20 61  e Year, Month, a
7b610 6e 64 20 44 61 79 20 66 72 6f 6d 20 74 68 65 20  nd Day from the 
7b620 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65  julian day numbe
7b630 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  r..*/.static voi
7b640 64 20 63 6f 6d 70 75 74 65 59 4d 44 28 44 61 74  d computeYMD(Dat
7b650 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74  eTime *p){.  int
7b660 20 5a 2c 20 41 2c 20 42 2c 20 43 2c 20 44 2c 20   Z, A, B, C, D, 
7b670 45 2c 20 58 31 3b 0a 20 20 69 66 28 20 70 2d 3e  E, X1;.  if( p->
7b680 76 61 6c 69 64 59 4d 44 20 29 20 72 65 74 75 72  validYMD ) retur
7b690 6e 3b 0a 20 20 69 66 28 20 21 70 2d 3e 76 61 6c  n;.  if( !p->val
7b6a0 69 64 4a 44 20 29 7b 0a 20 20 20 20 70 2d 3e 59  idJD ){.    p->Y
7b6b0 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 70 2d 3e   = 2000;.    p->
7b6c0 4d 20 3d 20 31 3b 0a 20 20 20 20 70 2d 3e 44 20  M = 1;.    p->D 
7b6d0 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 1;.  }else{.  
7b6e0 20 20 5a 20 3d 20 28 69 6e 74 29 28 28 70 2d 3e    Z = (int)((p->
7b6f0 69 4a 44 20 2b 20 34 33 32 30 30 30 30 30 29 2f  iJD + 43200000)/
7b700 38 36 34 30 30 30 30 30 29 3b 0a 20 20 20 20 41  86400000);.    A
7b710 20 3d 20 28 69 6e 74 29 28 28 5a 20 2d 20 31 38   = (int)((Z - 18
7b720 36 37 32 31 36 2e 32 35 29 2f 33 36 35 32 34 2e  67216.25)/36524.
7b730 32 35 29 3b 0a 20 20 20 20 41 20 3d 20 5a 20 2b  25);.    A = Z +
7b740 20 31 20 2b 20 41 20 2d 20 28 41 2f 34 29 3b 0a   1 + A - (A/4);.
7b750 20 20 20 20 42 20 3d 20 41 20 2b 20 31 35 32 34      B = A + 1524
7b760 3b 0a 20 20 20 20 43 20 3d 20 28 69 6e 74 29 28  ;.    C = (int)(
7b770 28 42 20 2d 20 31 32 32 2e 31 29 2f 33 36 35 2e  (B - 122.1)/365.
7b780 32 35 29 3b 0a 20 20 20 20 44 20 3d 20 28 33 36  25);.    D = (36
7b790 35 32 35 2a 43 29 2f 31 30 30 3b 0a 20 20 20 20  525*C)/100;.    
7b7a0 45 20 3d 20 28 69 6e 74 29 28 28 42 2d 44 29 2f  E = (int)((B-D)/
7b7b0 33 30 2e 36 30 30 31 29 3b 0a 20 20 20 20 58 31  30.6001);.    X1
7b7c0 20 3d 20 28 69 6e 74 29 28 33 30 2e 36 30 30 31   = (int)(30.6001
7b7d0 2a 45 29 3b 0a 20 20 20 20 70 2d 3e 44 20 3d 20  *E);.    p->D = 
7b7e0 42 20 2d 20 44 20 2d 20 58 31 3b 0a 20 20 20 20  B - D - X1;.    
7b7f0 70 2d 3e 4d 20 3d 20 45 3c 31 34 20 3f 20 45 2d  p->M = E<14 ? E-
7b800 31 20 3a 20 45 2d 31 33 3b 0a 20 20 20 20 70 2d  1 : E-13;.    p-
7b810 3e 59 20 3d 20 70 2d 3e 4d 3e 32 20 3f 20 43 20  >Y = p->M>2 ? C 
7b820 2d 20 34 37 31 36 20 3a 20 43 20 2d 20 34 37 31  - 4716 : C - 471
7b830 35 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69  5;.  }.  p->vali
7b840 64 59 4d 44 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a  dYMD = 1;.}../*.
7b850 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 48  ** Compute the H
7b860 6f 75 72 2c 20 4d 69 6e 75 74 65 2c 20 61 6e 64  our, Minute, and
7b870 20 53 65 63 6f 6e 64 73 20 66 72 6f 6d 20 74 68   Seconds from th
7b880 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d  e julian day num
7b890 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ber..*/.static v
7b8a0 6f 69 64 20 63 6f 6d 70 75 74 65 48 4d 53 28 44  oid computeHMS(D
7b8b0 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69  ateTime *p){.  i
7b8c0 6e 74 20 73 3b 0a 20 20 69 66 28 20 70 2d 3e 76  nt s;.  if( p->v
7b8d0 61 6c 69 64 48 4d 53 20 29 20 72 65 74 75 72 6e  alidHMS ) return
7b8e0 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29  ;.  computeJD(p)
7b8f0 3b 0a 20 20 73 20 3d 20 28 69 6e 74 29 28 28 70  ;.  s = (int)((p
7b900 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 30 30 30  ->iJD + 43200000
7b910 29 20 25 20 38 36 34 30 30 30 30 30 29 3b 0a 20  ) % 86400000);. 
7b920 20 70 2d 3e 73 20 3d 20 73 2f 31 30 30 30 2e 30   p->s = s/1000.0
7b930 3b 0a 20 20 73 20 3d 20 28 69 6e 74 29 70 2d 3e  ;.  s = (int)p->
7b940 73 3b 0a 20 20 70 2d 3e 73 20 2d 3d 20 73 3b 0a  s;.  p->s -= s;.
7b950 20 20 70 2d 3e 68 20 3d 20 73 2f 33 36 30 30 3b    p->h = s/3600;
7b960 0a 20 20 73 20 2d 3d 20 70 2d 3e 68 2a 33 36 30  .  s -= p->h*360
7b970 30 3b 0a 20 20 70 2d 3e 6d 20 3d 20 73 2f 36 30  0;.  p->m = s/60
7b980 3b 0a 20 20 70 2d 3e 73 20 2b 3d 20 73 20 2d 20  ;.  p->s += s - 
7b990 70 2d 3e 6d 2a 36 30 3b 0a 20 20 70 2d 3e 76 61  p->m*60;.  p->va
7b9a0 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 7d 0a 0a 2f  lidHMS = 1;.}../
7b9b0 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 62 6f 74  *.** Compute bot
7b9c0 68 20 59 4d 44 20 61 6e 64 20 48 4d 53 0a 2a 2f  h YMD and HMS.*/
7b9d0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d  .static void com
7b9e0 70 75 74 65 59 4d 44 5f 48 4d 53 28 44 61 74 65  puteYMD_HMS(Date
7b9f0 54 69 6d 65 20 2a 70 29 7b 0a 20 20 63 6f 6d 70  Time *p){.  comp
7ba00 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 63 6f 6d  uteYMD(p);.  com
7ba10 70 75 74 65 48 4d 53 28 70 29 3b 0a 7d 0a 0a 2f  puteHMS(p);.}../
7ba20 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 59  *.** Clear the Y
7ba30 4d 44 20 61 6e 64 20 48 4d 53 20 61 6e 64 20 74  MD and HMS and t
7ba40 68 65 20 54 5a 0a 2a 2f 0a 73 74 61 74 69 63 20  he TZ.*/.static 
7ba50 76 6f 69 64 20 63 6c 65 61 72 59 4d 44 5f 48 4d  void clearYMD_HM
7ba60 53 5f 54 5a 28 44 61 74 65 54 69 6d 65 20 2a 70  S_TZ(DateTime *p
7ba70 29 7b 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44  ){.  p->validYMD
7ba80 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64   = 0;.  p->valid
7ba90 48 4d 53 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61  HMS = 0;.  p->va
7baa0 6c 69 64 54 5a 20 3d 20 30 3b 0a 7d 0a 0a 23 69  lidTZ = 0;.}..#i
7bab0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
7bac0 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 2f 2a 0a 2a  T_LOCALTIME./*.*
7bad0 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 64 69  * Compute the di
7bae0 66 66 65 72 65 6e 63 65 20 28 69 6e 20 6d 69 6c  fference (in mil
7baf0 6c 69 73 65 63 6f 6e 64 73 29 0a 2a 2a 20 62 65  liseconds).** be
7bb00 74 77 65 65 6e 20 6c 6f 63 61 6c 74 69 6d 65 20  tween localtime 
7bb10 61 6e 64 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20  and UTC (a.k.a. 
7bb20 47 4d 54 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20  GMT).** for the 
7bb30 74 69 6d 65 20 76 61 6c 75 65 20 70 20 77 68 65  time value p whe
7bb40 72 65 20 70 20 69 73 20 69 6e 20 55 54 43 2e 0a  re p is in UTC..
7bb50 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65  */.static sqlite
7bb60 33 5f 69 6e 74 36 34 20 6c 6f 63 61 6c 74 69 6d  3_int64 localtim
7bb70 65 4f 66 66 73 65 74 28 44 61 74 65 54 69 6d 65  eOffset(DateTime
7bb80 20 2a 70 29 7b 0a 20 20 44 61 74 65 54 69 6d 65   *p){.  DateTime
7bb90 20 78 2c 20 79 3b 0a 20 20 74 69 6d 65 5f 74 20   x, y;.  time_t 
7bba0 74 3b 0a 20 20 78 20 3d 20 2a 70 3b 0a 20 20 63  t;.  x = *p;.  c
7bbb0 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 78  omputeYMD_HMS(&x
7bbc0 29 3b 0a 20 20 69 66 28 20 78 2e 59 3c 31 39 37  );.  if( x.Y<197
7bbd0 31 20 7c 7c 20 78 2e 59 3e 3d 32 30 33 38 20 29  1 || x.Y>=2038 )
7bbe0 7b 0a 20 20 20 20 78 2e 59 20 3d 20 32 30 30 30  {.    x.Y = 2000
7bbf0 3b 0a 20 20 20 20 78 2e 4d 20 3d 20 31 3b 0a 20  ;.    x.M = 1;. 
7bc00 20 20 20 78 2e 44 20 3d 20 31 3b 0a 20 20 20 20     x.D = 1;.    
7bc10 78 2e 68 20 3d 20 30 3b 0a 20 20 20 20 78 2e 6d  x.h = 0;.    x.m
7bc20 20 3d 20 30 3b 0a 20 20 20 20 78 2e 73 20 3d 20   = 0;.    x.s = 
7bc30 30 2e 30 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a  0.0;.  } else {.
7bc40 20 20 20 20 69 6e 74 20 73 20 3d 20 28 69 6e 74      int s = (int
7bc50 29 28 78 2e 73 20 2b 20 30 2e 35 29 3b 0a 20 20  )(x.s + 0.5);.  
7bc60 20 20 78 2e 73 20 3d 20 73 3b 0a 20 20 7d 0a 20    x.s = s;.  }. 
7bc70 20 78 2e 74 7a 20 3d 20 30 3b 0a 20 20 78 2e 76   x.tz = 0;.  x.v
7bc80 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 63 6f  alidJD = 0;.  co
7bc90 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20 74  mputeJD(&x);.  t
7bca0 20 3d 20 28 74 69 6d 65 5f 74 29 28 78 2e 69 4a   = (time_t)(x.iJ
7bcb0 44 2f 31 30 30 30 20 2d 20 32 31 30 38 36 36 37  D/1000 - 2108667
7bcc0 36 2a 28 69 36 34 29 31 30 30 30 30 29 3b 0a 23  6*(i64)10000);.#
7bcd0 69 66 64 65 66 20 48 41 56 45 5f 4c 4f 43 41 4c  ifdef HAVE_LOCAL
7bce0 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73  TIME_R.  {.    s
7bcf0 74 72 75 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b  truct tm sLocal;
7bd00 0a 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 72  .    localtime_r
7bd10 28 26 74 2c 20 26 73 4c 6f 63 61 6c 29 3b 0a 20  (&t, &sLocal);. 
7bd20 20 20 20 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e     y.Y = sLocal.
7bd30 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30 3b 0a  tm_year + 1900;.
7bd40 20 20 20 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c      y.M = sLocal
7bd50 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20  .tm_mon + 1;.   
7bd60 20 79 2e 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d   y.D = sLocal.tm
7bd70 5f 6d 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d  _mday;.    y.h =
7bd80 20 73 4c 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b   sLocal.tm_hour;
7bd90 0a 20 20 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61  .    y.m = sLoca
7bda0 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e  l.tm_min;.    y.
7bdb0 73 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65  s = sLocal.tm_se
7bdc0 63 3b 0a 20 20 7d 0a 23 65 6c 69 66 20 64 65 66  c;.  }.#elif def
7bdd0 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54  ined(HAVE_LOCALT
7bde0 49 4d 45 5f 53 29 20 26 26 20 48 41 56 45 5f 4c  IME_S) && HAVE_L
7bdf0 4f 43 41 4c 54 49 4d 45 5f 53 0a 20 20 7b 0a 20  OCALTIME_S.  {. 
7be00 20 20 20 73 74 72 75 63 74 20 74 6d 20 73 4c 6f     struct tm sLo
7be10 63 61 6c 3b 0a 20 20 20 20 6c 6f 63 61 6c 74 69  cal;.    localti
7be20 6d 65 5f 73 28 26 73 4c 6f 63 61 6c 2c 20 26 74  me_s(&sLocal, &t
7be30 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 73 4c 6f  );.    y.Y = sLo
7be40 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b 20 31 39  cal.tm_year + 19
7be50 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 73 4c  00;.    y.M = sL
7be60 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b  ocal.tm_mon + 1;
7be70 0a 20 20 20 20 79 2e 44 20 3d 20 73 4c 6f 63 61  .    y.D = sLoca
7be80 6c 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 79  l.tm_mday;.    y
7be90 2e 68 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 68  .h = sLocal.tm_h
7bea0 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 73  our;.    y.m = s
7beb0 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 20  Local.tm_min;.  
7bec0 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e 74    y.s = sLocal.t
7bed0 6d 5f 73 65 63 3b 0a 20 20 7d 0a 23 65 6c 73 65  m_sec;.  }.#else
7bee0 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20  .  {.    struct 
7bef0 74 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71 6c  tm *pTm;.    sql
7bf00 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
7bf10 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c  (sqlite3MutexAll
7bf20 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
7bf30 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b  STATIC_MASTER));
7bf40 0a 20 20 20 20 70 54 6d 20 3d 20 6c 6f 63 61 6c  .    pTm = local
7bf50 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 79 2e  time(&t);.    y.
7bf60 59 20 3d 20 70 54 6d 2d 3e 74 6d 5f 79 65 61 72  Y = pTm->tm_year
7bf70 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d   + 1900;.    y.M
7bf80 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 6f 6e 20 2b   = pTm->tm_mon +
7bf90 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20 70 54   1;.    y.D = pT
7bfa0 6d 2d 3e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20  m->tm_mday;.    
7bfb0 79 2e 68 20 3d 20 70 54 6d 2d 3e 74 6d 5f 68 6f  y.h = pTm->tm_ho
7bfc0 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 70 54  ur;.    y.m = pT
7bfd0 6d 2d 3e 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79  m->tm_min;.    y
7bfe0 2e 73 20 3d 20 70 54 6d 2d 3e 74 6d 5f 73 65 63  .s = pTm->tm_sec
7bff0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
7c000 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65  tex_leave(sqlite
7c010 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
7c020 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
7c030 4d 41 53 54 45 52 29 29 3b 0a 20 20 7d 0a 23 65  MASTER));.  }.#e
7c040 6e 64 69 66 0a 20 20 79 2e 76 61 6c 69 64 59 4d  ndif.  y.validYM
7c050 44 20 3d 20 31 3b 0a 20 20 79 2e 76 61 6c 69 64  D = 1;.  y.valid
7c060 48 4d 53 20 3d 20 31 3b 0a 20 20 79 2e 76 61 6c  HMS = 1;.  y.val
7c070 69 64 4a 44 20 3d 20 30 3b 0a 20 20 79 2e 76 61  idJD = 0;.  y.va
7c080 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 63 6f 6d  lidTZ = 0;.  com
7c090 70 75 74 65 4a 44 28 26 79 29 3b 0a 20 20 72 65  puteJD(&y);.  re
7c0a0 74 75 72 6e 20 79 2e 69 4a 44 20 2d 20 78 2e 69  turn y.iJD - x.i
7c0b0 4a 44 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  JD;.}.#endif /* 
7c0c0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41  SQLITE_OMIT_LOCA
7c0d0 4c 54 49 4d 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  LTIME */../*.** 
7c0e0 50 72 6f 63 65 73 73 20 61 20 6d 6f 64 69 66 69  Process a modifi
7c0f0 65 72 20 74 6f 20 61 20 64 61 74 65 2d 74 69 6d  er to a date-tim
7c100 65 20 73 74 61 6d 70 2e 20 20 54 68 65 20 6d 6f  e stamp.  The mo
7c110 64 69 66 69 65 72 73 20 61 72 65 0a 2a 2a 20 61  difiers are.** a
7c120 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
7c130 20 20 20 20 20 4e 4e 4e 20 64 61 79 73 0a 2a 2a       NNN days.**
7c140 20 20 20 20 20 4e 4e 4e 20 68 6f 75 72 73 0a 2a       NNN hours.*
7c150 2a 20 20 20 20 20 4e 4e 4e 20 6d 69 6e 75 74 65  *     NNN minute
7c160 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 2e 4e 4e 4e  s.**     NNN.NNN
7c170 4e 20 73 65 63 6f 6e 64 73 0a 2a 2a 20 20 20 20  N seconds.**    
7c180 20 4e 4e 4e 20 6d 6f 6e 74 68 73 0a 2a 2a 20 20   NNN months.**  
7c190 20 20 20 4e 4e 4e 20 79 65 61 72 73 0a 2a 2a 20     NNN years.** 
7c1a0 20 20 20 20 73 74 61 72 74 20 6f 66 20 6d 6f 6e      start of mon
7c1b0 74 68 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20  th.**     start 
7c1c0 6f 66 20 79 65 61 72 0a 2a 2a 20 20 20 20 20 73  of year.**     s
7c1d0 74 61 72 74 20 6f 66 20 77 65 65 6b 0a 2a 2a 20  tart of week.** 
7c1e0 20 20 20 20 73 74 61 72 74 20 6f 66 20 64 61 79      start of day
7c1f0 0a 2a 2a 20 20 20 20 20 77 65 65 6b 64 61 79 20  .**     weekday 
7c200 4e 0a 2a 2a 20 20 20 20 20 75 6e 69 78 65 70 6f  N.**     unixepo
7c210 63 68 0a 2a 2a 20 20 20 20 20 6c 6f 63 61 6c 74  ch.**     localt
7c220 69 6d 65 0a 2a 2a 20 20 20 20 20 75 74 63 0a 2a  ime.**     utc.*
7c230 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 6f 6e  *.** Return 0 on
7c240 20 73 75 63 63 65 73 73 20 61 6e 64 20 31 20 69   success and 1 i
7c250 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 6b  f there is any k
7c260 69 6e 64 20 6f 66 20 65 72 72 6f 72 2e 0a 2a 2f  ind of error..*/
7c270 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73  .static int pars
7c280 65 4d 6f 64 69 66 69 65 72 28 63 6f 6e 73 74 20  eModifier(const 
7c290 63 68 61 72 20 2a 7a 4d 6f 64 2c 20 44 61 74 65  char *zMod, Date
7c2a0 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Time *p){.  int 
7c2b0 72 63 20 3d 20 31 3b 0a 20 20 69 6e 74 20 6e 3b  rc = 1;.  int n;
7c2c0 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a 20 20 63  .  double r;.  c
7c2d0 68 61 72 20 2a 7a 2c 20 7a 42 75 66 5b 33 30 5d  har *z, zBuf[30]
7c2e0 3b 0a 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20  ;.  z = zBuf;.  
7c2f0 66 6f 72 28 6e 3d 30 3b 20 6e 3c 41 72 72 61 79  for(n=0; n<Array
7c300 53 69 7a 65 28 7a 42 75 66 29 2d 31 20 26 26 20  Size(zBuf)-1 && 
7c310 7a 4d 6f 64 5b 6e 5d 3b 20 6e 2b 2b 29 7b 0a 20  zMod[n]; n++){. 
7c320 20 20 20 7a 5b 6e 5d 20 3d 20 28 63 68 61 72 29     z[n] = (char)
7c330 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f  sqlite3UpperToLo
7c340 77 65 72 5b 28 75 38 29 7a 4d 6f 64 5b 6e 5d 5d  wer[(u8)zMod[n]]
7c350 3b 0a 20 20 7d 0a 20 20 7a 5b 6e 5d 20 3d 20 30  ;.  }.  z[n] = 0
7c360 3b 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 30 5d  ;.  switch( z[0]
7c370 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   ){.#ifndef SQLI
7c380 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d  TE_OMIT_LOCALTIM
7c390 45 0a 20 20 20 20 63 61 73 65 20 27 6c 27 3a 20  E.    case 'l': 
7c3a0 7b 0a 20 20 20 20 20 20 2f 2a 20 20 20 20 6c 6f  {.      /*    lo
7c3b0 63 61 6c 74 69 6d 65 0a 20 20 20 20 20 20 2a 2a  caltime.      **
7c3c0 0a 20 20 20 20 20 20 2a 2a 20 41 73 73 75 6d 69  .      ** Assumi
7c3d0 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  ng the current t
7c3e0 69 6d 65 20 76 61 6c 75 65 20 69 73 20 55 54 43  ime value is UTC
7c3f0 20 28 61 2e 6b 2e 61 2e 20 47 4d 54 29 2c 20 73   (a.k.a. GMT), s
7c400 68 69 66 74 20 69 74 20 74 6f 0a 20 20 20 20 20  hift it to.     
7c410 20 2a 2a 20 73 68 6f 77 20 6c 6f 63 61 6c 20 74   ** show local t
7c420 69 6d 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ime..      */.  
7c430 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a      if( strcmp(z
7c440 2c 20 22 6c 6f 63 61 6c 74 69 6d 65 22 29 3d 3d  , "localtime")==
7c450 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6d  0 ){.        com
7c460 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20  puteJD(p);.     
7c470 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 6c 6f 63     p->iJD += loc
7c480 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b  altimeOffset(p);
7c490 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d  .        clearYM
7c4a0 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20  D_HMS_TZ(p);.   
7c4b0 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20       rc = 0;.   
7c4c0 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
7c4d0 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
7c4e0 20 20 20 63 61 73 65 20 27 75 27 3a 20 7b 0a 20     case 'u': {. 
7c4f0 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a       /*.      **
7c500 20 20 20 20 75 6e 69 78 65 70 6f 63 68 0a 20 20      unixepoch.  
7c510 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
7c520 54 72 65 61 74 20 74 68 65 20 63 75 72 72 65 6e  Treat the curren
7c530 74 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e 69 4a  t value of p->iJ
7c540 44 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20  D as the number 
7c550 6f 66 0a 20 20 20 20 20 20 2a 2a 20 73 65 63 6f  of.      ** seco
7c560 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 2e 20  nds since 1970. 
7c570 20 43 6f 6e 76 65 72 74 20 74 6f 20 61 20 72 65   Convert to a re
7c580 61 6c 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75  al julian day nu
7c590 6d 62 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  mber..      */. 
7c5a0 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28       if( strcmp(
7c5b0 7a 2c 20 22 75 6e 69 78 65 70 6f 63 68 22 29 3d  z, "unixepoch")=
7c5c0 3d 30 20 26 26 20 70 2d 3e 76 61 6c 69 64 4a 44  =0 && p->validJD
7c5d0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69   ){.        p->i
7c5e0 4a 44 20 3d 20 28 70 2d 3e 69 4a 44 20 2b 20 34  JD = (p->iJD + 4
7c5f0 33 32 30 30 29 2f 38 36 34 30 30 20 2b 20 32 31  3200)/86400 + 21
7c600 30 38 36 36 37 36 2a 28 69 36 34 29 31 30 30 30  086676*(i64)1000
7c610 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 63 6c  0000;.        cl
7c620 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29  earYMD_HMS_TZ(p)
7c630 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30  ;.        rc = 0
7c640 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65  ;.      }.#ifnde
7c650 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
7c660 43 41 4c 54 49 4d 45 0a 20 20 20 20 20 20 65 6c  CALTIME.      el
7c670 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c  se if( strcmp(z,
7c680 20 22 75 74 63 22 29 3d 3d 30 20 29 7b 0a 20 20   "utc")==0 ){.  
7c690 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
7c6a0 74 36 34 20 63 31 3b 0a 20 20 20 20 20 20 20 20  t64 c1;.        
7c6b0 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20  computeJD(p);.  
7c6c0 20 20 20 20 20 20 63 31 20 3d 20 6c 6f 63 61 6c        c1 = local
7c6d0 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a 20  timeOffset(p);. 
7c6e0 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d         p->iJD -=
7c6f0 20 63 31 3b 0a 20 20 20 20 20 20 20 20 63 6c 65   c1;.        cle
7c700 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b  arYMD_HMS_TZ(p);
7c710 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20  .        p->iJD 
7c720 2b 3d 20 63 31 20 2d 20 6c 6f 63 61 6c 74 69 6d  += c1 - localtim
7c730 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20  eOffset(p);.    
7c740 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20      rc = 0;.    
7c750 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
7c760 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
7c770 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20 20    case 'w': {.  
7c780 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a 20      /*.      ** 
7c790 20 20 20 77 65 65 6b 64 61 79 20 4e 0a 20 20 20     weekday N.   
7c7a0 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d     **.      ** M
7c7b0 6f 76 65 20 74 68 65 20 64 61 74 65 20 74 6f 20  ove the date to 
7c7c0 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 6f 6e  the same time on
7c7d0 20 74 68 65 20 6e 65 78 74 20 6f 63 63 75 72 72   the next occurr
7c7e0 65 6e 63 65 20 6f 66 0a 20 20 20 20 20 20 2a 2a  ence of.      **
7c7f0 20 77 65 65 6b 64 61 79 20 4e 20 77 68 65 72 65   weekday N where
7c800 20 30 3d 3d 53 75 6e 64 61 79 2c 20 31 3d 3d 4d   0==Sunday, 1==M
7c810 6f 6e 64 61 79 2c 20 61 6e 64 20 73 6f 20 66 6f  onday, and so fo
7c820 72 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20 20  rth.  If the.   
7c830 20 20 20 2a 2a 20 64 61 74 65 20 69 73 20 61 6c     ** date is al
7c840 72 65 61 64 79 20 6f 6e 20 74 68 65 20 61 70 70  ready on the app
7c850 72 6f 70 72 69 61 74 65 20 77 65 65 6b 64 61 79  ropriate weekday
7c860 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f  , this is a no-o
7c870 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  p..      */.    
7c880 20 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c    if( strncmp(z,
7c890 20 22 77 65 65 6b 64 61 79 20 22 2c 20 38 29 3d   "weekday ", 8)=
7c8a0 3d 30 20 26 26 20 67 65 74 56 61 6c 75 65 28 26  =0 && getValue(&
7c8b0 7a 5b 38 5d 2c 26 72 29 3e 30 0a 20 20 20 20 20  z[8],&r)>0.     
7c8c0 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28              && (
7c8d0 6e 3d 28 69 6e 74 29 72 29 3d 3d 72 20 26 26 20  n=(int)r)==r && 
7c8e0 6e 3e 3d 30 20 26 26 20 72 3c 37 20 29 7b 0a 20  n>=0 && r<7 ){. 
7c8f0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69         sqlite3_i
7c900 6e 74 36 34 20 5a 3b 0a 20 20 20 20 20 20 20 20  nt64 Z;.        
7c910 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70  computeYMD_HMS(p
7c920 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61  );.        p->va
7c930 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20  lidTZ = 0;.     
7c940 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20     p->validJD = 
7c950 30 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75  0;.        compu
7c960 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20  teJD(p);.       
7c970 20 5a 20 3d 20 28 28 70 2d 3e 69 4a 44 20 2b 20   Z = ((p->iJD + 
7c980 31 32 39 36 30 30 30 30 30 29 2f 38 36 34 30 30  129600000)/86400
7c990 30 30 30 29 20 25 20 37 3b 0a 20 20 20 20 20 20  000) % 7;.      
7c9a0 20 20 69 66 28 20 5a 3e 6e 20 29 20 5a 20 2d 3d    if( Z>n ) Z -=
7c9b0 20 37 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69   7;.        p->i
7c9c0 4a 44 20 2b 3d 20 28 6e 20 2d 20 5a 29 2a 38 36  JD += (n - Z)*86
7c9d0 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20  400000;.        
7c9e0 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28  clearYMD_HMS_TZ(
7c9f0 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  p);.        rc =
7ca00 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
7ca10 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
7ca20 20 20 20 63 61 73 65 20 27 73 27 3a 20 7b 0a 20     case 's': {. 
7ca30 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a       /*.      **
7ca40 20 20 20 20 73 74 61 72 74 20 6f 66 20 54 54 54      start of TTT
7ca50 54 54 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  TT.      **.    
7ca60 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61    ** Move the da
7ca70 74 65 20 62 61 63 6b 77 61 72 64 73 20 74 6f 20  te backwards to 
7ca80 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
7ca90 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 79   the current day
7caa0 2c 0a 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d 6f  ,.      ** or mo
7cab0 6e 74 68 20 6f 72 20 79 65 61 72 2e 0a 20 20 20  nth or year..   
7cac0 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
7cad0 73 74 72 6e 63 6d 70 28 7a 2c 20 22 73 74 61 72  strncmp(z, "star
7cae0 74 20 6f 66 20 22 2c 20 39 29 21 3d 30 20 29 20  t of ", 9)!=0 ) 
7caf0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7a 20 2b  break;.      z +
7cb00 3d 20 39 3b 0a 20 20 20 20 20 20 63 6f 6d 70 75  = 9;.      compu
7cb10 74 65 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 20  teYMD(p);.      
7cb20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b  p->validHMS = 1;
7cb30 0a 20 20 20 20 20 20 70 2d 3e 68 20 3d 20 70 2d  .      p->h = p-
7cb40 3e 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d  >m = 0;.      p-
7cb50 3e 73 20 3d 20 30 2e 30 3b 0a 20 20 20 20 20 20  >s = 0.0;.      
7cb60 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a  p->validTZ = 0;.
7cb70 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44        p->validJD
7cb80 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20   = 0;.      if( 
7cb90 73 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22  strcmp(z,"month"
7cba0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
7cbb0 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 20 20 20  p->D = 1;.      
7cbc0 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20    rc = 0;.      
7cbd0 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70  }else if( strcmp
7cbe0 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30 20 29 7b  (z,"year")==0 ){
7cbf0 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65  .        compute
7cc00 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20  YMD(p);.        
7cc10 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20  p->M = 1;.      
7cc20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 20    p->D = 1;.    
7cc30 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20      rc = 0;.    
7cc40 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63    }else if( strc
7cc50 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d 30 20 29  mp(z,"day")==0 )
7cc60 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30  {.        rc = 0
7cc70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
7cc80 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
7cc90 20 63 61 73 65 20 27 2b 27 3a 0a 20 20 20 20 63   case '+':.    c
7cca0 61 73 65 20 27 2d 27 3a 0a 20 20 20 20 63 61 73  ase '-':.    cas
7ccb0 65 20 27 30 27 3a 0a 20 20 20 20 63 61 73 65 20  e '0':.    case 
7ccc0 27 31 27 3a 0a 20 20 20 20 63 61 73 65 20 27 32  '1':.    case '2
7ccd0 27 3a 0a 20 20 20 20 63 61 73 65 20 27 33 27 3a  ':.    case '3':
7cce0 0a 20 20 20 20 63 61 73 65 20 27 34 27 3a 0a 20  .    case '4':. 
7ccf0 20 20 20 63 61 73 65 20 27 35 27 3a 0a 20 20 20     case '5':.   
7cd00 20 63 61 73 65 20 27 36 27 3a 0a 20 20 20 20 63   case '6':.    c
7cd10 61 73 65 20 27 37 27 3a 0a 20 20 20 20 63 61 73  ase '7':.    cas
7cd20 65 20 27 38 27 3a 0a 20 20 20 20 63 61 73 65 20  e '8':.    case 
7cd30 27 39 27 3a 20 7b 0a 20 20 20 20 20 20 64 6f 75  '9': {.      dou
7cd40 62 6c 65 20 72 52 6f 75 6e 64 65 72 3b 0a 20 20  ble rRounder;.  
7cd50 20 20 20 20 6e 20 3d 20 67 65 74 56 61 6c 75 65      n = getValue
7cd60 28 7a 2c 20 26 72 29 3b 0a 20 20 20 20 20 20 61  (z, &r);.      a
7cd70 73 73 65 72 74 28 20 6e 3e 3d 31 20 29 3b 0a 20  ssert( n>=1 );. 
7cd80 20 20 20 20 20 69 66 28 20 7a 5b 6e 5d 3d 3d 27       if( z[n]=='
7cd90 3a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  :' ){.        /*
7cda0 20 41 20 6d 6f 64 69 66 69 65 72 20 6f 66 20 74   A modifier of t
7cdb0 68 65 20 66 6f 72 6d 20 28 2b 7c 2d 29 48 48 3a  he form (+|-)HH:
7cdc0 4d 4d 3a 53 53 2e 46 46 46 20 61 64 64 73 20 28  MM:SS.FFF adds (
7cdd0 6f 72 20 73 75 62 74 72 61 63 74 73 29 20 74 68  or subtracts) th
7cde0 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 70 65  e.        ** spe
7cdf0 63 69 66 69 65 64 20 6e 75 6d 62 65 72 20 6f 66  cified number of
7ce00 20 68 6f 75 72 73 2c 20 6d 69 6e 75 74 65 73 2c   hours, minutes,
7ce10 20 73 65 63 6f 6e 64 73 2c 20 61 6e 64 20 66 72   seconds, and fr
7ce20 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73  actional seconds
7ce30 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 74  .        ** to t
7ce40 68 65 20 74 69 6d 65 2e 20 20 54 68 65 20 22 2e  he time.  The ".
7ce50 46 46 46 22 20 6d 61 79 20 62 65 20 6f 6d 69 74  FFF" may be omit
7ce60 74 65 64 2e 20 20 54 68 65 20 22 3a 53 53 2e 46  ted.  The ":SS.F
7ce70 46 46 22 20 6d 61 79 20 62 65 0a 20 20 20 20 20  FF" may be.     
7ce80 20 20 20 2a 2a 20 6f 6d 69 74 74 65 64 2e 0a 20     ** omitted.. 
7ce90 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
7cea0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32    const char *z2
7ceb0 20 3d 20 7a 3b 0a 20 20 20 20 20 20 20 20 44 61   = z;.        Da
7cec0 74 65 54 69 6d 65 20 74 78 3b 0a 20 20 20 20 20  teTime tx;.     
7ced0 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34     sqlite3_int64
7cee0 20 64 61 79 3b 0a 20 20 20 20 20 20 20 20 69 66   day;.        if
7cef0 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 69  ( !sqlite3Isdigi
7cf00 74 28 2a 7a 32 29 20 29 20 7a 32 2b 2b 3b 0a 20  t(*z2) ) z2++;. 
7cf10 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 74         memset(&t
7cf20 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 74 78 29  x, 0, sizeof(tx)
7cf30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
7cf40 61 72 73 65 48 68 4d 6d 53 73 28 7a 32 2c 20 26  arseHhMmSs(z2, &
7cf50 74 78 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  tx) ) break;.   
7cf60 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26       computeJD(&
7cf70 74 78 29 3b 0a 20 20 20 20 20 20 20 20 74 78 2e  tx);.        tx.
7cf80 69 4a 44 20 2d 3d 20 34 33 32 30 30 30 30 30 3b  iJD -= 43200000;
7cf90 0a 20 20 20 20 20 20 20 20 64 61 79 20 3d 20 74  .        day = t
7cfa0 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 3b 0a  x.iJD/86400000;.
7cfb0 20 20 20 20 20 20 20 20 74 78 2e 69 4a 44 20 2d          tx.iJD -
7cfc0 3d 20 64 61 79 2a 38 36 34 30 30 30 30 30 3b 0a  = day*86400000;.
7cfd0 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b 30 5d          if( z[0]
7cfe0 3d 3d 27 2d 27 20 29 20 74 78 2e 69 4a 44 20 3d  =='-' ) tx.iJD =
7cff0 20 2d 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20 20   -tx.iJD;.      
7d000 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a    computeJD(p);.
7d010 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44          clearYMD
7d020 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20  _HMS_TZ(p);.    
7d030 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 74 78      p->iJD += tx
7d040 2e 69 4a 44 3b 0a 20 20 20 20 20 20 20 20 72 63  .iJD;.        rc
7d050 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72   = 0;.        br
7d060 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
7d070 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 20 20 20     z += n;.     
7d080 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49   while( sqlite3I
7d090 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b  sspace(*z) ) z++
7d0a0 3b 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c 69  ;.      n = sqli
7d0b0 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0a  te3Strlen30(z);.
7d0c0 20 20 20 20 20 20 69 66 28 20 6e 3e 31 30 20 7c        if( n>10 |
7d0d0 7c 20 6e 3c 33 20 29 20 62 72 65 61 6b 3b 0a 20  | n<3 ) break;. 
7d0e0 20 20 20 20 20 69 66 28 20 7a 5b 6e 2d 31 5d 3d       if( z[n-1]=
7d0f0 3d 27 73 27 20 29 7b 20 7a 5b 6e 2d 31 5d 20 3d  ='s' ){ z[n-1] =
7d100 20 30 3b 20 6e 2d 2d 3b 20 7d 0a 20 20 20 20 20   0; n--; }.     
7d110 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20   computeJD(p);. 
7d120 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20       rc = 0;.   
7d130 20 20 20 72 52 6f 75 6e 64 65 72 20 3d 20 72 3c     rRounder = r<
7d140 30 20 3f 20 2d 30 2e 35 20 3a 20 2b 30 2e 35 3b  0 ? -0.5 : +0.5;
7d150 0a 20 20 20 20 20 20 69 66 28 20 6e 3d 3d 33 20  .      if( n==3 
7d160 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 64 61 79  && strcmp(z,"day
7d170 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ")==0 ){.       
7d180 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69   p->iJD += (sqli
7d190 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34  te3_int64)(r*864
7d1a0 30 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64  00000.0 + rRound
7d1b0 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  er);.      }else
7d1c0 20 69 66 28 20 6e 3d 3d 34 20 26 26 20 73 74 72   if( n==4 && str
7d1d0 63 6d 70 28 7a 2c 22 68 6f 75 72 22 29 3d 3d 30  cmp(z,"hour")==0
7d1e0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69   ){.        p->i
7d1f0 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69  JD += (sqlite3_i
7d200 6e 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 30  nt64)(r*(8640000
7d210 30 2e 30 2f 32 34 2e 30 29 20 2b 20 72 52 6f 75  0.0/24.0) + rRou
7d220 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c  nder);.      }el
7d230 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26 20 73  se if( n==6 && s
7d240 74 72 63 6d 70 28 7a 2c 22 6d 69 6e 75 74 65 22  trcmp(z,"minute"
7d250 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
7d260 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74  p->iJD += (sqlit
7d270 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 34  e3_int64)(r*(864
7d280 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36 30  00000.0/(24.0*60
7d290 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72 29  .0)) + rRounder)
7d2a0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
7d2b0 28 20 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d 70  ( n==6 && strcmp
7d2c0 28 7a 2c 22 73 65 63 6f 6e 64 22 29 3d 3d 30 20  (z,"second")==0 
7d2d0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a  ){.        p->iJ
7d2e0 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e  D += (sqlite3_in
7d2f0 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 30 30  t64)(r*(86400000
7d300 2e 30 2f 28 32 34 2e 30 2a 36 30 2e 30 2a 36 30  .0/(24.0*60.0*60
7d310 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72 29  .0)) + rRounder)
7d320 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
7d330 28 20 6e 3d 3d 35 20 26 26 20 73 74 72 63 6d 70  ( n==5 && strcmp
7d340 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20 29  (z,"month")==0 )
7d350 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78 2c  {.        int x,
7d360 20 79 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70   y;.        comp
7d370 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20  uteYMD_HMS(p);. 
7d380 20 20 20 20 20 20 20 70 2d 3e 4d 20 2b 3d 20 28         p->M += (
7d390 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 78  int)r;.        x
7d3a0 20 3d 20 70 2d 3e 4d 3e 30 20 3f 20 28 70 2d 3e   = p->M>0 ? (p->
7d3b0 4d 2d 31 29 2f 31 32 20 3a 20 28 70 2d 3e 4d 2d  M-1)/12 : (p->M-
7d3c0 31 32 29 2f 31 32 3b 0a 20 20 20 20 20 20 20 20  12)/12;.        
7d3d0 70 2d 3e 59 20 2b 3d 20 78 3b 0a 20 20 20 20 20  p->Y += x;.     
7d3e0 20 20 20 70 2d 3e 4d 20 2d 3d 20 78 2a 31 32 3b     p->M -= x*12;
7d3f0 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69  .        p->vali
7d400 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  dJD = 0;.       
7d410 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20   computeJD(p);. 
7d420 20 20 20 20 20 20 20 79 20 3d 20 28 69 6e 74 29         y = (int)
7d430 72 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 79  r;.        if( y
7d440 21 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20  !=r ){.         
7d450 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69   p->iJD += (sqli
7d460 74 65 33 5f 69 6e 74 36 34 29 28 28 72 20 2d 20  te3_int64)((r - 
7d470 79 29 2a 33 30 2e 30 2a 38 36 34 30 30 30 30 30  y)*30.0*86400000
7d480 2e 30 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a  .0 + rRounder);.
7d490 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
7d4a0 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 26  }else if( n==4 &
7d4b0 26 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61 72  & strcmp(z,"year
7d4c0 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ")==0 ){.       
7d4d0 20 69 6e 74 20 79 20 3d 20 28 69 6e 74 29 72 3b   int y = (int)r;
7d4e0 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65  .        compute
7d4f0 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20  YMD_HMS(p);.    
7d500 20 20 20 20 70 2d 3e 59 20 2b 3d 20 79 3b 0a 20      p->Y += y;. 
7d510 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a         p->validJ
7d520 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 63  D = 0;.        c
7d530 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20  omputeJD(p);.   
7d540 20 20 20 20 20 69 66 28 20 79 21 3d 72 20 29 7b       if( y!=r ){
7d550 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 4a  .          p->iJ
7d560 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e  D += (sqlite3_in
7d570 74 36 34 29 28 28 72 20 2d 20 79 29 2a 33 36 35  t64)((r - y)*365
7d580 2e 30 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20  .0*86400000.0 + 
7d590 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20  rRounder);.     
7d5a0 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
7d5b0 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 31  {.        rc = 1
7d5c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
7d5d0 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28  clearYMD_HMS_TZ(
7d5e0 70 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  p);.      break;
7d5f0 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
7d600 6c 74 3a 20 7b 0a 20 20 20 20 20 20 62 72 65 61  lt: {.      brea
7d610 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  k;.    }.  }.  r
7d620 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
7d630 2a 2a 20 50 72 6f 63 65 73 73 20 74 69 6d 65 20  ** Process time 
7d640 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e  function argumen
7d650 74 73 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 20  ts.  argv[0] is 
7d660 61 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d  a date-time stam
7d670 70 2e 0a 2a 2a 20 61 72 67 76 5b 31 5d 20 61 6e  p..** argv[1] an
7d680 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20  d following are 
7d690 6d 6f 64 69 66 69 65 72 73 2e 20 20 50 61 72 73  modifiers.  Pars
7d6a0 65 20 74 68 65 6d 20 61 6c 6c 20 61 6e 64 20 77  e them all and w
7d6b0 72 69 74 65 0a 2a 2a 20 74 68 65 20 72 65 73 75  rite.** the resu
7d6c0 6c 74 69 6e 67 20 74 69 6d 65 20 69 6e 74 6f 20  lting time into 
7d6d0 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 72  the DateTime str
7d6e0 75 63 74 75 72 65 20 70 2e 20 20 52 65 74 75 72  ucture p.  Retur
7d6f0 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73  n 0.** on succes
7d700 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 65  s and 1 if there
7d710 20 61 72 65 20 61 6e 79 20 65 72 72 6f 72 73 2e   are any errors.
7d720 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  .**.** If there 
7d730 61 72 65 20 7a 65 72 6f 20 70 61 72 61 6d 65 74  are zero paramet
7d740 65 72 73 20 28 69 66 20 65 76 65 6e 20 61 72 67  ers (if even arg
7d750 76 5b 30 5d 20 69 73 20 75 6e 64 65 66 69 6e 65  v[0] is undefine
7d760 64 29 0a 2a 2a 20 74 68 65 6e 20 61 73 73 75 6d  d).** then assum
7d770 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75  e a default valu
7d780 65 20 6f 66 20 22 6e 6f 77 22 20 66 6f 72 20 61  e of "now" for a
7d790 72 67 76 5b 30 5d 2e 0a 2a 2f 0a 73 74 61 74 69  rgv[0]..*/.stati
7d7a0 63 20 69 6e 74 20 69 73 44 61 74 65 28 0a 20 20  c int isDate(.  
7d7b0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
7d7c0 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74  *context, .  int
7d7d0 20 61 72 67 63 2c 20 0a 20 20 73 71 6c 69 74 65   argc, .  sqlite
7d7e0 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 2c 20  3_value **argv, 
7d7f0 0a 20 20 44 61 74 65 54 69 6d 65 20 2a 70 0a 29  .  DateTime *p.)
7d800 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e  {.  int i;.  con
7d810 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
7d820 20 2a 7a 3b 0a 20 20 69 6e 74 20 65 54 79 70 65   *z;.  int eType
7d830 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c  ;.  memset(p, 0,
7d840 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20   sizeof(*p));.  
7d850 69 66 28 20 61 72 67 63 3d 3d 30 20 29 7b 0a 20  if( argc==0 ){. 
7d860 20 20 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f     setDateTimeTo
7d870 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 2c  Current(context,
7d880 20 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28   p);.  }else if(
7d890 20 28 65 54 79 70 65 20 3d 20 73 71 6c 69 74 65   (eType = sqlite
7d8a0 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
7d8b0 76 5b 30 5d 29 29 3d 3d 53 51 4c 49 54 45 5f 46  v[0]))==SQLITE_F
7d8c0 4c 4f 41 54 0a 20 20 20 20 20 20 20 20 20 20 20  LOAT.           
7d8d0 20 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65          || eType
7d8e0 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  ==SQLITE_INTEGER
7d8f0 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 3d   ){.    p->iJD =
7d900 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29   (sqlite3_int64)
7d910 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64  (sqlite3_value_d
7d920 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 2a 38  ouble(argv[0])*8
7d930 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29  6400000.0 + 0.5)
7d940 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44  ;.    p->validJD
7d950 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 1;.  }else{. 
7d960 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76     z = sqlite3_v
7d970 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
7d980 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 20 7c  ]);.    if( !z |
7d990 7c 20 70 61 72 73 65 44 61 74 65 4f 72 54 69 6d  | parseDateOrTim
7d9a0 65 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72  e(context, (char
7d9b0 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 20 20 20  *)z, p) ){.     
7d9c0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
7d9d0 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 3b 20  .  }.  for(i=1; 
7d9e0 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20  i<argc; i++){.  
7d9f0 20 20 69 66 28 20 28 7a 20 3d 20 73 71 6c 69 74    if( (z = sqlit
7da00 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
7da10 67 76 5b 69 5d 29 29 3d 3d 30 20 7c 7c 20 70 61  gv[i]))==0 || pa
7da20 72 73 65 4d 6f 64 69 66 69 65 72 28 28 63 68 61  rseModifier((cha
7da30 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 20 20  r*)z, p) ){.    
7da40 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
7da50 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
7da60 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ;.}.../*.** The 
7da70 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e  following routin
7da80 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  es implement the
7da90 20 76 61 72 69 6f 75 73 20 64 61 74 65 20 61 6e   various date an
7daa0 64 20 74 69 6d 65 20 66 75 6e 63 74 69 6f 6e 73  d time functions
7dab0 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  .** of SQLite..*
7dac0 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 6a 75 6c 69  /../*.**    juli
7dad0 61 6e 64 61 79 28 20 54 49 4d 45 53 54 52 49 4e  anday( TIMESTRIN
7dae0 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e  G, MOD, MOD, ...
7daf0 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ).**.** Return t
7db00 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75  he julian day nu
7db10 6d 62 65 72 20 6f 66 20 74 68 65 20 64 61 74 65  mber of the date
7db20 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68   specified in th
7db30 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2f 0a 73  e arguments.*/.s
7db40 74 61 74 69 63 20 76 6f 69 64 20 6a 75 6c 69 61  tatic void julia
7db50 6e 64 61 79 46 75 6e 63 28 0a 20 20 73 71 6c 69  ndayFunc(.  sqli
7db60 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
7db70 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
7db80 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
7db90 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61  e **argv.){.  Da
7dba0 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20  teTime x;.  if( 
7dbb0 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20  isDate(context, 
7dbc0 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d  argc, argv, &x)=
7dbd0 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6d 70 75 74  =0 ){.    comput
7dbe0 65 4a 44 28 26 78 29 3b 0a 20 20 20 20 73 71 6c  eJD(&x);.    sql
7dbf0 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
7dc00 6c 65 28 63 6f 6e 74 65 78 74 2c 20 78 2e 69 4a  le(context, x.iJ
7dc10 44 2f 38 36 34 30 30 30 30 30 2e 30 29 3b 0a 20  D/86400000.0);. 
7dc20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 64   }.}../*.**    d
7dc30 61 74 65 74 69 6d 65 28 20 54 49 4d 45 53 54 52  atetime( TIMESTR
7dc40 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e  ING, MOD, MOD, .
7dc50 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ..).**.** Return
7dc60 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d   YYYY-MM-DD HH:M
7dc70 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76  M:SS.*/.static v
7dc80 6f 69 64 20 64 61 74 65 74 69 6d 65 46 75 6e 63  oid datetimeFunc
7dc90 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
7dca0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
7dcb0 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
7dcc0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
7dcd0 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78  .){.  DateTime x
7dce0 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63  ;.  if( isDate(c
7dcf0 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72  ontext, argc, ar
7dd00 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20  gv, &x)==0 ){.  
7dd10 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d    char zBuf[100]
7dd20 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44  ;.    computeYMD
7dd30 5f 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 73 71  _HMS(&x);.    sq
7dd40 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
7dd50 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75  izeof(zBuf), zBu
7dd60 66 2c 20 22 25 30 34 64 2d 25 30 32 64 2d 25 30  f, "%04d-%02d-%0
7dd70 32 64 20 25 30 32 64 3a 25 30 32 64 3a 25 30 32  2d %02d:%02d:%02
7dd80 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  d",.            
7dd90 20 20 20 20 20 20 20 20 20 78 2e 59 2c 20 78 2e           x.Y, x.
7dda0 4d 2c 20 78 2e 44 2c 20 78 2e 68 2c 20 78 2e 6d  M, x.D, x.h, x.m
7ddb0 2c 20 28 69 6e 74 29 28 78 2e 73 29 29 3b 0a 20  , (int)(x.s));. 
7ddc0 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
7ddd0 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
7dde0 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45  zBuf, -1, SQLITE
7ddf0 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d  _TRANSIENT);.  }
7de00 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 74 69 6d  .}../*.**    tim
7de10 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d  e( TIMESTRING, M
7de20 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a  OD, MOD, ...).**
7de30 0a 2a 2a 20 52 65 74 75 72 6e 20 48 48 3a 4d 4d  .** Return HH:MM
7de40 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  :SS.*/.static vo
7de50 69 64 20 74 69 6d 65 46 75 6e 63 28 0a 20 20 73  id timeFunc(.  s
7de60 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
7de70 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
7de80 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
7de90 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
7dea0 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69   DateTime x;.  i
7deb0 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78  f( isDate(contex
7dec0 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26  t, argc, argv, &
7ded0 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61  x)==0 ){.    cha
7dee0 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20  r zBuf[100];.   
7def0 20 63 6f 6d 70 75 74 65 48 4d 53 28 26 78 29 3b   computeHMS(&x);
7df00 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
7df10 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75  rintf(sizeof(zBu
7df20 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 32 64 3a  f), zBuf, "%02d:
7df30 25 30 32 64 3a 25 30 32 64 22 2c 20 78 2e 68 2c  %02d:%02d", x.h,
7df40 20 78 2e 6d 2c 20 28 69 6e 74 29 78 2e 73 29 3b   x.m, (int)x.s);
7df50 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
7df60 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
7df70 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49  , zBuf, -1, SQLI
7df80 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
7df90 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 64   }.}../*.**    d
7dfa0 61 74 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c  ate( TIMESTRING,
7dfb0 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a   MOD, MOD, ...).
7dfc0 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 59 59 59  **.** Return YYY
7dfd0 59 2d 4d 4d 2d 44 44 0a 2a 2f 0a 73 74 61 74 69  Y-MM-DD.*/.stati
7dfe0 63 20 76 6f 69 64 20 64 61 74 65 46 75 6e 63 28  c void dateFunc(
7dff0 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
7e000 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
7e010 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
7e020 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
7e030 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b  ){.  DateTime x;
7e040 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63 6f  .  if( isDate(co
7e050 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67  ntext, argc, arg
7e060 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20  v, &x)==0 ){.   
7e070 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b   char zBuf[100];
7e080 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 28  .    computeYMD(
7e090 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  &x);.    sqlite3
7e0a0 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
7e0b0 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25  (zBuf), zBuf, "%
7e0c0 30 34 64 2d 25 30 32 64 2d 25 30 32 64 22 2c 20  04d-%02d-%02d", 
7e0d0 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 29 3b 0a  x.Y, x.M, x.D);.
7e0e0 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
7e0f0 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
7e100 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54   zBuf, -1, SQLIT
7e110 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
7e120 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 73 74  }.}../*.**    st
7e130 72 66 74 69 6d 65 28 20 46 4f 52 4d 41 54 2c 20  rftime( FORMAT, 
7e140 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c  TIMESTRING, MOD,
7e150 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a   MOD, ...).**.**
7e160 20 52 65 74 75 72 6e 20 61 20 73 74 72 69 6e 67   Return a string
7e170 20 64 65 73 63 72 69 62 65 64 20 62 79 20 46 4f   described by FO
7e180 52 4d 41 54 2e 20 20 43 6f 6e 76 65 72 73 69 6f  RMAT.  Conversio
7e190 6e 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  ns as follows:.*
7e1a0 2a 0a 2a 2a 20 20 20 25 64 20 20 64 61 79 20 6f  *.**   %d  day o
7e1b0 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 25 66 20  f month.**   %f 
7e1c0 20 2a 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20 73   ** fractional s
7e1d0 65 63 6f 6e 64 73 20 20 53 53 2e 53 53 53 0a 2a  econds  SS.SSS.*
7e1e0 2a 20 20 20 25 48 20 20 68 6f 75 72 20 30 30 2d  *   %H  hour 00-
7e1f0 32 34 0a 2a 2a 20 20 20 25 6a 20 20 64 61 79 20  24.**   %j  day 
7e200 6f 66 20 79 65 61 72 20 30 30 30 2d 33 36 36 0a  of year 000-366.
7e210 2a 2a 20 20 20 25 4a 20 20 2a 2a 20 4a 75 6c 69  **   %J  ** Juli
7e220 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 0a 2a 2a  an day number.**
7e230 20 20 20 25 6d 20 20 6d 6f 6e 74 68 20 30 31 2d     %m  month 01-
7e240 31 32 0a 2a 2a 20 20 20 25 4d 20 20 6d 69 6e 75  12.**   %M  minu
7e250 74 65 20 30 30 2d 35 39 0a 2a 2a 20 20 20 25 73  te 00-59.**   %s
7e260 20 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20    seconds since 
7e270 31 39 37 30 2d 30 31 2d 30 31 0a 2a 2a 20 20 20  1970-01-01.**   
7e280 25 53 20 20 73 65 63 6f 6e 64 73 20 30 30 2d 35  %S  seconds 00-5
7e290 39 0a 2a 2a 20 20 20 25 77 20 20 64 61 79 20 6f  9.**   %w  day o
7e2a0 66 20 77 65 65 6b 20 30 2d 36 20 20 73 75 6e 64  f week 0-6  sund
7e2b0 61 79 3d 3d 30 0a 2a 2a 20 20 20 25 57 20 20 77  ay==0.**   %W  w
7e2c0 65 65 6b 20 6f 66 20 79 65 61 72 20 30 30 2d 35  eek of year 00-5
7e2d0 33 0a 2a 2a 20 20 20 25 59 20 20 79 65 61 72 20  3.**   %Y  year 
7e2e0 30 30 30 30 2d 39 39 39 39 0a 2a 2a 20 20 20 25  0000-9999.**   %
7e2f0 25 20 20 25 0a 2a 2f 0a 73 74 61 74 69 63 20 76  %  %.*/.static v
7e300 6f 69 64 20 73 74 72 66 74 69 6d 65 46 75 6e 63  oid strftimeFunc
7e310 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
7e320 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
7e330 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
7e340 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
7e350 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78  .){.  DateTime x
7e360 3b 0a 20 20 75 36 34 20 6e 3b 0a 20 20 73 69 7a  ;.  u64 n;.  siz
7e370 65 5f 74 20 69 2c 6a 3b 0a 20 20 63 68 61 72 20  e_t i,j;.  char 
7e380 2a 7a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  *z;.  sqlite3 *d
7e390 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  b;.  const char 
7e3a0 2a 7a 46 6d 74 20 3d 20 28 63 6f 6e 73 74 20 63  *zFmt = (const c
7e3b0 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  har*)sqlite3_val
7e3c0 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
7e3d0 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30  ;.  char zBuf[10
7e3e0 30 5d 3b 0a 20 20 69 66 28 20 7a 46 6d 74 3d 3d  0];.  if( zFmt==
7e3f0 30 20 7c 7c 20 69 73 44 61 74 65 28 63 6f 6e 74  0 || isDate(cont
7e400 65 78 74 2c 20 61 72 67 63 2d 31 2c 20 61 72 67  ext, argc-1, arg
7e410 76 2b 31 2c 20 26 78 29 20 29 20 72 65 74 75 72  v+1, &x) ) retur
7e420 6e 3b 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65  n;.  db = sqlite
7e430 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
7e440 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  dle(context);.  
7e450 66 6f 72 28 69 3d 30 2c 20 6e 3d 31 3b 20 7a 46  for(i=0, n=1; zF
7e460 6d 74 5b 69 5d 3b 20 69 2b 2b 2c 20 6e 2b 2b 29  mt[i]; i++, n++)
7e470 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69  {.    if( zFmt[i
7e480 5d 3d 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20  ]=='%' ){.      
7e490 73 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 2b 31  switch( zFmt[i+1
7e4a0 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73  ] ){.        cas
7e4b0 65 20 27 64 27 3a 0a 20 20 20 20 20 20 20 20 63  e 'd':.        c
7e4c0 61 73 65 20 27 48 27 3a 0a 20 20 20 20 20 20 20  ase 'H':.       
7e4d0 20 63 61 73 65 20 27 6d 27 3a 0a 20 20 20 20 20   case 'm':.     
7e4e0 20 20 20 63 61 73 65 20 27 4d 27 3a 0a 20 20 20     case 'M':.   
7e4f0 20 20 20 20 20 63 61 73 65 20 27 53 27 3a 0a 20       case 'S':. 
7e500 20 20 20 20 20 20 20 63 61 73 65 20 27 57 27 3a         case 'W':
7e510 0a 20 20 20 20 20 20 20 20 20 20 6e 2b 2b 3b 0a  .          n++;.
7e520 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 61 6c            /* fal
7e530 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20 20  l thru */.      
7e540 20 20 63 61 73 65 20 27 77 27 3a 0a 20 20 20 20    case 'w':.    
7e550 20 20 20 20 63 61 73 65 20 27 25 27 3a 0a 20 20      case '%':.  
7e560 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
7e570 20 20 20 20 20 20 20 63 61 73 65 20 27 66 27 3a         case 'f':
7e580 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20  .          n += 
7e590 38 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  8;.          bre
7e5a0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
7e5b0 20 27 6a 27 3a 0a 20 20 20 20 20 20 20 20 20 20   'j':.          
7e5c0 6e 20 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20 20  n += 3;.        
7e5d0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
7e5e0 20 63 61 73 65 20 27 59 27 3a 0a 20 20 20 20 20   case 'Y':.     
7e5f0 20 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 20       n += 8;.   
7e600 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
7e610 20 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 0a        case 's':.
7e620 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4a 27          case 'J'
7e630 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d  :.          n +=
7e640 20 35 30 3b 0a 20 20 20 20 20 20 20 20 20 20 62   50;.          b
7e650 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65  reak;.        de
7e660 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 20  fault:.         
7e670 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 45 52 52   return;  /* ERR
7e680 4f 52 2e 20 20 72 65 74 75 72 6e 20 61 20 4e 55  OR.  return a NU
7e690 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20  LL */.      }.  
7e6a0 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 7d 0a 20      i++;.    }. 
7e6b0 20 7d 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e   }.  testcase( n
7e6c0 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 66 29 2d 31  ==sizeof(zBuf)-1
7e6d0 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
7e6e0 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 66 29 20  n==sizeof(zBuf) 
7e6f0 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e  );.  testcase( n
7e700 3d 3d 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69  ==(u64)db->aLimi
7e710 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
7e720 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20 20 74 65  ENGTH]+1 );.  te
7e730 73 74 63 61 73 65 28 20 6e 3d 3d 28 75 36 34 29  stcase( n==(u64)
7e740 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
7e750 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20  E_LIMIT_LENGTH] 
7e760 29 3b 0a 20 20 69 66 28 20 6e 3c 73 69 7a 65 6f  );.  if( n<sizeo
7e770 66 28 7a 42 75 66 29 20 29 7b 0a 20 20 20 20 7a  f(zBuf) ){.    z
7e780 20 3d 20 7a 42 75 66 3b 0a 20 20 7d 65 6c 73 65   = zBuf;.  }else
7e790 20 69 66 28 20 6e 3e 28 75 36 34 29 64 62 2d 3e   if( n>(u64)db->
7e7a0 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
7e7b0 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20  MIT_LENGTH] ){. 
7e7c0 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
7e7d0 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63  t_error_toobig(c
7e7e0 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 72 65 74  ontext);.    ret
7e7f0 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  urn;.  }else{.  
7e800 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 4d    z = sqlite3DbM
7e810 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 28 69 6e  allocRaw(db, (in
7e820 74 29 6e 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d  t)n);.    if( z=
7e830 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
7e840 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
7e850 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b  _nomem(context);
7e860 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
7e870 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6d 70 75     }.  }.  compu
7e880 74 65 4a 44 28 26 78 29 3b 0a 20 20 63 6f 6d 70  teJD(&x);.  comp
7e890 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a  uteYMD_HMS(&x);.
7e8a0 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 7a 46 6d    for(i=j=0; zFm
7e8b0 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  t[i]; i++){.    
7e8c0 69 66 28 20 7a 46 6d 74 5b 69 5d 21 3d 27 25 27  if( zFmt[i]!='%'
7e8d0 20 29 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d   ){.      z[j++]
7e8e0 20 3d 20 7a 46 6d 74 5b 69 5d 3b 0a 20 20 20 20   = zFmt[i];.    
7e8f0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 2b 2b  }else{.      i++
7e900 3b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 20  ;.      switch( 
7e910 7a 46 6d 74 5b 69 5d 20 29 7b 0a 20 20 20 20 20  zFmt[i] ){.     
7e920 20 20 20 63 61 73 65 20 27 64 27 3a 20 20 73 71     case 'd':  sq
7e930 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33  lite3_snprintf(3
7e940 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78  , &z[j],"%02d",x
7e950 2e 44 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b  .D); j+=2; break
7e960 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ;.        case '
7e970 66 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20  f': {.          
7e980 64 6f 75 62 6c 65 20 73 20 3d 20 78 2e 73 3b 0a  double s = x.s;.
7e990 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 3e            if( s>
7e9a0 35 39 2e 39 39 39 20 29 20 73 20 3d 20 35 39 2e  59.999 ) s = 59.
7e9b0 39 39 39 3b 0a 20 20 20 20 20 20 20 20 20 20 73  999;.          s
7e9c0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
7e9d0 37 2c 20 26 7a 5b 6a 5d 2c 22 25 30 36 2e 33 66  7, &z[j],"%06.3f
7e9e0 22 2c 20 73 29 3b 0a 20 20 20 20 20 20 20 20 20  ", s);.         
7e9f0 20 6a 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72   j += sqlite3Str
7ea00 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20  len30(&z[j]);.  
7ea10 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
7ea20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
7ea30 20 63 61 73 65 20 27 48 27 3a 20 20 73 71 6c 69   case 'H':  sqli
7ea40 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20  te3_snprintf(3, 
7ea50 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 68  &z[j],"%02d",x.h
7ea60 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a  ); j+=2; break;.
7ea70 20 20 20 20 20 20 20 20 63 61 73 65 20 27 57 27          case 'W'
7ea80 3a 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 2a  : /* Fall thru *
7ea90 2f 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  /.        case '
7eaa0 6a 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20  j': {.          
7eab0 69 6e 74 20 6e 44 61 79 3b 20 20 20 20 20 20 20  int nDay;       
7eac0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
7ead0 6f 66 20 64 61 79 73 20 73 69 6e 63 65 20 31 73  of days since 1s
7eae0 74 20 64 61 79 20 6f 66 20 79 65 61 72 20 2a 2f  t day of year */
7eaf0 0a 20 20 20 20 20 20 20 20 20 20 44 61 74 65 54  .          DateT
7eb00 69 6d 65 20 79 20 3d 20 78 3b 0a 20 20 20 20 20  ime y = x;.     
7eb10 20 20 20 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d       y.validJD =
7eb20 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 79 2e   0;.          y.
7eb30 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  M = 1;.         
7eb40 20 79 2e 44 20 3d 20 31 3b 0a 20 20 20 20 20 20   y.D = 1;.      
7eb50 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 79      computeJD(&y
7eb60 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 44 61  );.          nDa
7eb70 79 20 3d 20 28 69 6e 74 29 28 28 78 2e 69 4a 44  y = (int)((x.iJD
7eb80 2d 79 2e 69 4a 44 2b 34 33 32 30 30 30 30 30 29  -y.iJD+43200000)
7eb90 2f 38 36 34 30 30 30 30 30 29 3b 0a 20 20 20 20  /86400000);.    
7eba0 20 20 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69        if( zFmt[i
7ebb0 5d 3d 3d 27 57 27 20 29 7b 0a 20 20 20 20 20 20  ]=='W' ){.      
7ebc0 20 20 20 20 20 20 69 6e 74 20 77 64 3b 20 20 20        int wd;   
7ebd0 2f 2a 20 30 3d 4d 6f 6e 64 61 79 2c 20 31 3d 54  /* 0=Monday, 1=T
7ebe0 75 65 73 64 61 79 2c 20 2e 2e 2e 20 36 3d 53 75  uesday, ... 6=Su
7ebf0 6e 64 61 79 20 2a 2f 0a 20 20 20 20 20 20 20 20  nday */.        
7ec00 20 20 20 20 77 64 20 3d 20 28 69 6e 74 29 28 28      wd = (int)((
7ec10 28 78 2e 69 4a 44 2b 34 33 32 30 30 30 30 30 29  (x.iJD+43200000)
7ec20 2f 38 36 34 30 30 30 30 30 29 25 37 29 3b 0a 20  /86400000)%7);. 
7ec30 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
7ec40 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26  e3_snprintf(3, &
7ec50 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28 6e 44 61  z[j],"%02d",(nDa
7ec60 79 2b 37 2d 77 64 29 2f 37 29 3b 0a 20 20 20 20  y+7-wd)/7);.    
7ec70 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 32 3b 0a          j += 2;.
7ec80 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
7ec90 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
7eca0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 34 2c  ite3_snprintf(4,
7ecb0 20 26 7a 5b 6a 5d 2c 22 25 30 33 64 22 2c 6e 44   &z[j],"%03d",nD
7ecc0 61 79 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20  ay+1);.         
7ecd0 20 20 20 6a 20 2b 3d 20 33 3b 0a 20 20 20 20 20     j += 3;.     
7ece0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
7ecf0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
7ed00 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  }.        case '
7ed10 4a 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20  J': {.          
7ed20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
7ed30 28 32 30 2c 20 26 7a 5b 6a 5d 2c 22 25 2e 31 36  (20, &z[j],"%.16
7ed40 67 22 2c 78 2e 69 4a 44 2f 38 36 34 30 30 30 30  g",x.iJD/8640000
7ed50 30 2e 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  0.0);.          
7ed60 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  j+=sqlite3Strlen
7ed70 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20  30(&z[j]);.     
7ed80 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
7ed90 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61      }.        ca
7eda0 73 65 20 27 6d 27 3a 20 20 73 71 6c 69 74 65 33  se 'm':  sqlite3
7edb0 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b  _snprintf(3, &z[
7edc0 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 4d 29 3b 20  j],"%02d",x.M); 
7edd0 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20  j+=2; break;.   
7ede0 20 20 20 20 20 63 61 73 65 20 27 4d 27 3a 20 20       case 'M':  
7edf0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
7ee00 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22  (3, &z[j],"%02d"
7ee10 2c 78 2e 6d 29 3b 20 6a 2b 3d 32 3b 20 62 72 65  ,x.m); j+=2; bre
7ee20 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
7ee30 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 20 20   's': {.        
7ee40 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
7ee50 74 66 28 33 30 2c 26 7a 5b 6a 5d 2c 22 25 6c 6c  tf(30,&z[j],"%ll
7ee60 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  d",.            
7ee70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
7ee80 69 36 34 29 28 78 2e 69 4a 44 2f 31 30 30 30 20  i64)(x.iJD/1000 
7ee90 2d 20 32 31 30 38 36 36 37 36 2a 28 69 36 34 29  - 21086676*(i64)
7eea0 31 30 30 30 30 29 29 3b 0a 20 20 20 20 20 20 20  10000));.       
7eeb0 20 20 20 6a 20 2b 3d 20 73 71 6c 69 74 65 33 53     j += sqlite3S
7eec0 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a  trlen30(&z[j]);.
7eed0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
7eee0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
7eef0 20 20 20 63 61 73 65 20 27 53 27 3a 20 20 73 71     case 'S':  sq
7ef00 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33  lite3_snprintf(3
7ef10 2c 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28 69  ,&z[j],"%02d",(i
7ef20 6e 74 29 78 2e 73 29 3b 20 6a 2b 3d 32 3b 20 62  nt)x.s); j+=2; b
7ef30 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61  reak;.        ca
7ef40 73 65 20 27 77 27 3a 20 7b 0a 20 20 20 20 20 20  se 'w': {.      
7ef50 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 28 63 68      z[j++] = (ch
7ef60 61 72 29 28 28 28 78 2e 69 4a 44 2b 31 32 39 36  ar)(((x.iJD+1296
7ef70 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29  00000)/86400000)
7ef80 20 25 20 37 29 20 2b 20 27 30 27 3b 0a 20 20 20   % 7) + '0';.   
7ef90 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
7efa0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
7efb0 63 61 73 65 20 27 59 27 3a 20 7b 0a 20 20 20 20  case 'Y': {.    
7efc0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
7efd0 70 72 69 6e 74 66 28 35 2c 26 7a 5b 6a 5d 2c 22  printf(5,&z[j],"
7efe0 25 30 34 64 22 2c 78 2e 59 29 3b 20 6a 2b 3d 73  %04d",x.Y); j+=s
7eff0 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26  qlite3Strlen30(&
7f000 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20  z[j]);.         
7f010 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
7f020 7d 0a 20 20 20 20 20 20 20 20 64 65 66 61 75 6c  }.        defaul
7f030 74 3a 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 25  t:   z[j++] = '%
7f040 27 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  '; break;.      
7f050 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 7a 5b  }.    }.  }.  z[
7f060 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  j] = 0;.  sqlite
7f070 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
7f080 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c 0a 20 20  ntext, z, -1,.  
7f090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7f0a0 20 20 20 20 7a 3d 3d 7a 42 75 66 20 3f 20 53 51      z==zBuf ? SQ
7f0b0 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 3a  LITE_TRANSIENT :
7f0c0 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29   SQLITE_DYNAMIC)
7f0d0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65  ;.}../*.** curre
7f0e0 6e 74 5f 74 69 6d 65 28 29 0a 2a 2a 0a 2a 2a 20  nt_time().**.** 
7f0f0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
7f100 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76  turns the same v
7f110 61 6c 75 65 20 61 73 20 74 69 6d 65 28 27 6e 6f  alue as time('no
7f120 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  w')..*/.static v
7f130 6f 69 64 20 63 74 69 6d 65 46 75 6e 63 28 0a 20  oid ctimeFunc(. 
7f140 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
7f150 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
7f160 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69   NotUsed,.  sqli
7f170 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55  te3_value **NotU
7f180 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44  sed2.){.  UNUSED
7f190 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
7f1a0 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a  sed, NotUsed2);.
7f1b0 20 20 74 69 6d 65 46 75 6e 63 28 63 6f 6e 74 65    timeFunc(conte
7f1c0 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a  xt, 0, 0);.}../*
7f1d0 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 64 61 74 65  .** current_date
7f1e0 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  ().**.** This fu
7f1f0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
7f200 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73  he same value as
7f210 20 64 61 74 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f   date('now')..*/
7f220 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 64 61  .static void cda
7f230 74 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  teFunc(.  sqlite
7f240 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
7f250 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  xt,.  int NotUse
7f260 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  d,.  sqlite3_val
7f270 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b  ue **NotUsed2.){
7f280 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
7f290 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
7f2a0 74 55 73 65 64 32 29 3b 0a 20 20 64 61 74 65 46  tUsed2);.  dateF
7f2b0 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20  unc(context, 0, 
7f2c0 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72  0);.}../*.** cur
7f2d0 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28 29  rent_timestamp()
7f2e0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
7f2f0 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65  tion returns the
7f300 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 20 64   same value as d
7f310 61 74 65 74 69 6d 65 28 27 6e 6f 77 27 29 2e 0a  atetime('now')..
7f320 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
7f330 74 69 6d 65 73 74 61 6d 70 46 75 6e 63 28 0a 20  timestampFunc(. 
7f340 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
7f350 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
7f360 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69   NotUsed,.  sqli
7f370 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55  te3_value **NotU
7f380 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44  sed2.){.  UNUSED
7f390 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
7f3a0 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a  sed, NotUsed2);.
7f3b0 20 20 64 61 74 65 74 69 6d 65 46 75 6e 63 28 63    datetimeFunc(c
7f3c0 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d  ontext, 0, 0);.}
7f3d0 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
7f3e0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
7f3f0 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 29 20  DATETIME_FUNCS) 
7f400 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
7f410 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f  E_OMIT_DATETIME_
7f420 46 55 4e 43 53 0a 2f 2a 0a 2a 2a 20 49 66 20 74  FUNCS./*.** If t
7f430 68 65 20 6c 69 62 72 61 72 79 20 69 73 20 63 6f  he library is co
7f440 6d 70 69 6c 65 64 20 74 6f 20 6f 6d 69 74 20 74  mpiled to omit t
7f450 68 65 20 66 75 6c 6c 2d 73 63 61 6c 65 20 64 61  he full-scale da
7f460 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 68  te and time.** h
7f470 61 6e 64 6c 69 6e 67 20 28 74 6f 20 67 65 74 20  andling (to get 
7f480 61 20 73 6d 61 6c 6c 65 72 20 62 69 6e 61 72 79  a smaller binary
7f490 29 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ), the following
7f4a0 20 6d 69 6e 69 6d 61 6c 20 76 65 72 73 69 6f 6e   minimal version
7f4b0 0a 2a 2a 20 6f 66 20 74 68 65 20 66 75 6e 63 74  .** of the funct
7f4c0 69 6f 6e 73 20 63 75 72 72 65 6e 74 5f 74 69 6d  ions current_tim
7f4d0 65 28 29 2c 20 63 75 72 72 65 6e 74 5f 64 61 74  e(), current_dat
7f4e0 65 28 29 20 61 6e 64 20 63 75 72 72 65 6e 74 5f  e() and current_
7f4f0 74 69 6d 65 73 74 61 6d 70 28 29 0a 2a 2a 20 61  timestamp().** a
7f500 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 73 74  re included inst
7f510 65 61 64 2e 20 54 68 69 73 20 69 73 20 74 6f 20  ead. This is to 
7f520 73 75 70 70 6f 72 74 20 63 6f 6c 75 6d 6e 20 64  support column d
7f530 65 63 6c 61 72 61 74 69 6f 6e 73 20 74 68 61 74  eclarations that
7f540 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 22 44 45 46  .** include "DEF
7f550 41 55 4c 54 20 43 55 52 52 45 4e 54 5f 54 49 4d  AULT CURRENT_TIM
7f560 45 22 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 54 68  E" etc..**.** Th
7f570 69 73 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 73  is function uses
7f580 20 74 68 65 20 43 2d 6c 69 62 72 61 72 79 20 66   the C-library f
7f590 75 6e 63 74 69 6f 6e 73 20 74 69 6d 65 28 29 2c  unctions time(),
7f5a0 20 67 6d 74 69 6d 65 28 29 0a 2a 2a 20 61 6e 64   gmtime().** and
7f5b0 20 73 74 72 66 74 69 6d 65 28 29 2e 20 54 68 65   strftime(). The
7f5c0 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 74   format string t
7f5d0 6f 20 70 61 73 73 20 74 6f 20 73 74 72 66 74 69  o pass to strfti
7f5e0 6d 65 28 29 20 69 73 20 73 75 70 70 6c 69 65 64  me() is supplied
7f5f0 0a 2a 2a 20 61 73 20 74 68 65 20 75 73 65 72 2d  .** as the user-
7f600 64 61 74 61 20 66 6f 72 20 74 68 65 20 66 75 6e  data for the fun
7f610 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ction..*/.static
7f620 20 76 6f 69 64 20 63 75 72 72 65 6e 74 54 69 6d   void currentTim
7f630 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  eFunc(.  sqlite3
7f640 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
7f650 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
7f660 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
7f670 2a 61 72 67 76 0a 29 7b 0a 20 20 74 69 6d 65 5f  *argv.){.  time_
7f680 74 20 74 3b 0a 20 20 63 68 61 72 20 2a 7a 46 6f  t t;.  char *zFo
7f690 72 6d 61 74 20 3d 20 28 63 68 61 72 20 2a 29 73  rmat = (char *)s
7f6a0 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
7f6b0 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c  (context);.  sql
7f6c0 69 74 65 33 20 2a 64 62 3b 0a 20 20 64 6f 75 62  ite3 *db;.  doub
7f6d0 6c 65 20 72 54 3b 0a 20 20 63 68 61 72 20 7a 42  le rT;.  char zB
7f6e0 75 66 5b 32 30 5d 3b 0a 0a 20 20 55 4e 55 53 45  uf[20];..  UNUSE
7f6f0 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63  D_PARAMETER(argc
7f700 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
7f710 4d 45 54 45 52 28 61 72 67 76 29 3b 0a 0a 20 20  METER(argv);..  
7f720 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
7f730 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
7f740 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74  ontext);.  sqlit
7f750 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28  e3OsCurrentTime(
7f760 64 62 2d 3e 70 56 66 73 2c 20 26 72 54 29 3b 0a  db->pVfs, &rT);.
7f770 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
7f780 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
7f790 4e 54 0a 20 20 74 20 3d 20 38 36 34 30 30 2e 30  NT.  t = 86400.0
7f7a0 2a 28 72 54 20 2d 20 32 34 34 30 35 38 37 2e 35  *(rT - 2440587.5
7f7b0 29 20 2b 20 30 2e 35 3b 0a 23 65 6c 73 65 0a 20  ) + 0.5;.#else. 
7f7c0 20 2f 2a 20 77 69 74 68 6f 75 74 20 66 6c 6f 61   /* without floa
7f7d0 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f  ting point suppo
7f7e0 72 74 2c 20 72 54 20 77 69 6c 6c 20 68 61 76 65  rt, rT will have
7f7f0 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 6c 6f  .  ** already lo
7f800 73 74 20 66 72 61 63 74 69 6f 6e 61 6c 20 64 61  st fractional da
7f810 79 20 70 72 65 63 69 73 69 6f 6e 2e 0a 20 20 2a  y precision..  *
7f820 2f 0a 20 20 74 20 3d 20 38 36 34 30 30 20 2a 20  /.  t = 86400 * 
7f830 28 72 54 20 2d 20 32 34 34 30 35 38 37 29 20 2d  (rT - 2440587) -
7f840 20 34 33 32 30 30 3b 0a 23 65 6e 64 69 66 0a 23   43200;.#endif.#
7f850 69 66 64 65 66 20 48 41 56 45 5f 47 4d 54 49 4d  ifdef HAVE_GMTIM
7f860 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73 74 72 75  E_R.  {.    stru
7f870 63 74 20 74 6d 20 73 4e 6f 77 3b 0a 20 20 20 20  ct tm sNow;.    
7f880 67 6d 74 69 6d 65 5f 72 28 26 74 2c 20 26 73 4e  gmtime_r(&t, &sN
7f890 6f 77 29 3b 0a 20 20 20 20 73 74 72 66 74 69 6d  ow);.    strftim
7f8a0 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a 46 6f 72  e(zBuf, 20, zFor
7f8b0 6d 61 74 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 7d  mat, &sNow);.  }
7f8c0 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20 20 20 73  .#else.  {.    s
7f8d0 74 72 75 63 74 20 74 6d 20 2a 70 54 6d 3b 0a 20  truct tm *pTm;. 
7f8e0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
7f8f0 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75  _enter(sqlite3Mu
7f900 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
7f910 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
7f920 54 45 52 29 29 3b 0a 20 20 20 20 70 54 6d 20 3d  TER));.    pTm =
7f930 20 67 6d 74 69 6d 65 28 26 74 29 3b 0a 20 20 20   gmtime(&t);.   
7f940 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66 2c 20   strftime(zBuf, 
7f950 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 70 54 6d  20, zFormat, pTm
7f960 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
7f970 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74  utex_leave(sqlit
7f980 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
7f990 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
7f9a0 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 7d 0a 23  _MASTER));.  }.#
7f9b0 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33  endif..  sqlite3
7f9c0 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
7f9d0 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20  text, zBuf, -1, 
7f9e0 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
7f9f0 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  );.}.#endif../*.
7fa00 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
7fa10 20 72 65 67 69 73 74 65 72 65 64 20 61 6c 6c 20   registered all 
7fa20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 43 20 66  of the above C f
7fa30 75 6e 63 74 69 6f 6e 73 20 61 73 20 53 51 4c 0a  unctions as SQL.
7fa40 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54  ** functions.  T
7fa50 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 74 68  his should be th
7fa60 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69  e only routine i
7fa70 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 74 68  n this file with
7fa80 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e  .** external lin
7fa90 6b 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  kage..*/.SQLITE_
7faa0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
7fab0 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74 65  ite3RegisterDate
7fac0 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 76 6f  TimeFunctions(vo
7fad0 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 53 51  id){.  static SQ
7fae0 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66  LITE_WSD FuncDef
7faf0 20 61 44 61 74 65 54 69 6d 65 46 75 6e 63 73 5b   aDateTimeFuncs[
7fb00 5d 20 3d 20 7b 0a 23 69 66 6e 64 65 66 20 53 51  ] = {.#ifndef SQ
7fb10 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49  LITE_OMIT_DATETI
7fb20 4d 45 5f 46 55 4e 43 53 0a 20 20 20 20 46 55 4e  ME_FUNCS.    FUN
7fb30 43 54 49 4f 4e 28 6a 75 6c 69 61 6e 64 61 79 2c  CTION(julianday,
7fb40 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30          -1, 0, 0
7fb50 2c 20 6a 75 6c 69 61 6e 64 61 79 46 75 6e 63 20  , juliandayFunc 
7fb60 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
7fb70 64 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  date,           
7fb80 20 20 2d 31 2c 20 30 2c 20 30 2c 20 64 61 74 65    -1, 0, 0, date
7fb90 46 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20  Func      ),.   
7fba0 20 46 55 4e 43 54 49 4f 4e 28 74 69 6d 65 2c 20   FUNCTION(time, 
7fbb0 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20              -1, 
7fbc0 30 2c 20 30 2c 20 74 69 6d 65 46 75 6e 63 20 20  0, 0, timeFunc  
7fbd0 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
7fbe0 49 4f 4e 28 64 61 74 65 74 69 6d 65 2c 20 20 20  ION(datetime,   
7fbf0 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20        -1, 0, 0, 
7fc00 64 61 74 65 74 69 6d 65 46 75 6e 63 20 20 29 2c  datetimeFunc  ),
7fc10 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 74  .    FUNCTION(st
7fc20 72 66 74 69 6d 65 2c 20 20 20 20 20 20 20 20 20  rftime,         
7fc30 2d 31 2c 20 30 2c 20 30 2c 20 73 74 72 66 74 69  -1, 0, 0, strfti
7fc40 6d 65 46 75 6e 63 20 20 29 2c 0a 20 20 20 20 46  meFunc  ),.    F
7fc50 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f  UNCTION(current_
7fc60 74 69 6d 65 2c 20 20 20 20 20 20 30 2c 20 30 2c  time,      0, 0,
7fc70 20 30 2c 20 63 74 69 6d 65 46 75 6e 63 20 20 20   0, ctimeFunc   
7fc80 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
7fc90 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74  N(current_timest
7fca0 61 6d 70 2c 20 30 2c 20 30 2c 20 30 2c 20 63 74  amp, 0, 0, 0, ct
7fcb0 69 6d 65 73 74 61 6d 70 46 75 6e 63 29 2c 0a 20  imestampFunc),. 
7fcc0 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72     FUNCTION(curr
7fcd0 65 6e 74 5f 64 61 74 65 2c 20 20 20 20 20 20 30  ent_date,      0
7fce0 2c 20 30 2c 20 30 2c 20 63 64 61 74 65 46 75 6e  , 0, 0, cdateFun
7fcf0 63 20 20 20 20 20 29 2c 0a 23 65 6c 73 65 0a 20  c     ),.#else. 
7fd00 20 20 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28     STR_FUNCTION(
7fd10 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c 20 20 20  current_time,   
7fd20 20 20 20 30 2c 20 22 25 48 3a 25 4d 3a 25 53 22     0, "%H:%M:%S"
7fd30 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20 63 75  ,          0, cu
7fd40 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 29 2c 0a  rrentTimeFunc),.
7fd50 20 20 20 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e      STR_FUNCTION
7fd60 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61  (current_timesta
7fd70 6d 70 2c 20 30 2c 20 22 25 59 2d 25 6d 2d 25 64  mp, 0, "%Y-%m-%d
7fd80 22 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20 63  ",          0, c
7fd90 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 29 2c  urrentTimeFunc),
7fda0 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 54 49 4f  .    STR_FUNCTIO
7fdb0 4e 28 63 75 72 72 65 6e 74 5f 64 61 74 65 2c 20  N(current_date, 
7fdc0 20 20 20 20 20 30 2c 20 22 25 59 2d 25 6d 2d 25       0, "%Y-%m-%
7fdd0 64 20 25 48 3a 25 4d 3a 25 53 22 2c 20 30 2c 20  d %H:%M:%S", 0, 
7fde0 63 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 29  currentTimeFunc)
7fdf0 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 20 20  ,.#endif.  };.  
7fe00 69 6e 74 20 69 3b 0a 20 20 46 75 6e 63 44 65 66  int i;.  FuncDef
7fe10 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26 47  Hash *pHash = &G
7fe20 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 48 61 73  LOBAL(FuncDefHas
7fe30 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  h, sqlite3Global
7fe40 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20 20 46 75  Functions);.  Fu
7fe50 6e 63 44 65 66 20 2a 61 46 75 6e 63 20 3d 20 28  ncDef *aFunc = (
7fe60 46 75 6e 63 44 65 66 2a 29 26 47 4c 4f 42 41 4c  FuncDef*)&GLOBAL
7fe70 28 46 75 6e 63 44 65 66 2c 20 61 44 61 74 65 54  (FuncDef, aDateT
7fe80 69 6d 65 46 75 6e 63 73 29 3b 0a 0a 20 20 66 6f  imeFuncs);..  fo
7fe90 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69  r(i=0; i<ArraySi
7fea0 7a 65 28 61 44 61 74 65 54 69 6d 65 46 75 6e 63  ze(aDateTimeFunc
7feb0 73 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71  s); i++){.    sq
7fec0 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73 65  lite3FuncDefInse
7fed0 72 74 28 70 48 61 73 68 2c 20 26 61 46 75 6e 63  rt(pHash, &aFunc
7fee0 5b 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a  [i]);.  }.}../**
7fef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
7ff00 20 6f 66 20 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a   of date.c *****
7ff10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ff20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ff30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
7ff40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
7ff50 69 6e 20 66 69 6c 65 20 6f 73 2e 63 20 2a 2a 2a  in file os.c ***
7ff60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ff70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7ff80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
7ff90 2a 2a 20 32 30 30 35 20 4e 6f 76 65 6d 62 65 72  ** 2005 November
7ffa0 20 32 39 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   29.**.** The au
7ffb0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
7ffc0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
7ffd0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
7ffe0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
7fff0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
80000 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
80010 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
80020 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
80030 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
80040 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
80050 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
80060 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
80070 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
80080 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
80090 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
800a0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
800b0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
800c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
800d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
800e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
800f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80100 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
80110 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
80120 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 63 6f   OS interface co
80130 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f  de that is commo
80140 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 61 72 63 68  n to all.** arch
80150 69 74 65 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a  itectures..**.**
80160 20 24 49 64 3a 20 6f 73 2e 63 2c 76 20 31 2e 31   $Id: os.c,v 1.1
80170 32 37 20 32 30 30 39 2f 30 37 2f 32 37 20 31 31  27 2009/07/27 11
80180 3a 34 31 3a 32 31 20 64 61 6e 69 65 6c 6b 31 39  :41:21 danielk19
80190 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 64 65 66  77 Exp $.*/.#def
801a0 69 6e 65 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 43  ine _SQLITE_OS_C
801b0 5f 20 31 0a 23 75 6e 64 65 66 20 5f 53 51 4c 49  _ 1.#undef _SQLI
801c0 54 45 5f 4f 53 5f 43 5f 0a 0a 2f 2a 0a 2a 2a 20  TE_OS_C_../*.** 
801d0 54 68 65 20 64 65 66 61 75 6c 74 20 53 51 4c 69  The default SQLi
801e0 74 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 69  te sqlite3_vfs i
801f0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64  mplementations d
80200 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 0a 2a  o not allocate.*
80210 2a 20 6d 65 6d 6f 72 79 20 28 61 63 74 75 61 6c  * memory (actual
80220 6c 79 2c 20 6f 73 5f 75 6e 69 78 2e 63 20 61 6c  ly, os_unix.c al
80230 6c 6f 63 61 74 65 73 20 61 20 73 6d 61 6c 6c 20  locates a small 
80240 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79  amount of memory
80250 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  .** from within 
80260 4f 73 4f 70 65 6e 28 29 29 2c 20 62 75 74 20 73  OsOpen()), but s
80270 6f 6d 65 20 74 68 69 72 64 2d 70 61 72 74 79 20  ome third-party 
80280 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
80290 6d 61 79 2e 0a 2a 2a 20 53 6f 20 77 65 20 74 65  may..** So we te
802a0 73 74 20 74 68 65 20 65 66 66 65 63 74 73 20 6f  st the effects o
802b0 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  f a malloc() fai
802c0 6c 69 6e 67 20 61 6e 64 20 74 68 65 20 73 71 6c  ling and the sql
802d0 69 74 65 33 4f 73 58 58 58 28 29 0a 2a 2a 20 66  ite3OsXXX().** f
802e0 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 69 6e  unction returnin
802f0 67 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e  g SQLITE_IOERR_N
80300 4f 4d 45 4d 20 75 73 69 6e 67 20 74 68 65 20 44  OMEM using the D
80310 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
80320 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68   macro..**.** Th
80330 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63  e following func
80340 74 69 6f 6e 73 20 61 72 65 20 69 6e 73 74 72 75  tions are instru
80350 6d 65 6e 74 65 64 20 66 6f 72 20 6d 61 6c 6c 6f  mented for mallo
80360 63 28 29 20 66 61 69 6c 75 72 65 20 0a 2a 2a 20  c() failure .** 
80370 74 65 73 74 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  testing:.**.**  
80380 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e     sqlite3OsOpen
80390 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  ().**     sqlite
803a0 33 4f 73 52 65 61 64 28 29 0a 2a 2a 20 20 20 20  3OsRead().**    
803b0 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
803c0 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  ).**     sqlite3
803d0 4f 73 53 79 6e 63 28 29 0a 2a 2a 20 20 20 20 20  OsSync().**     
803e0 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 0a  sqlite3OsLock().
803f0 2a 2a 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  **.*/.#if define
80400 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26  d(SQLITE_TEST) &
80410 26 20 28 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  & (SQLITE_OS_WIN
80420 3d 3d 30 29 0a 20 20 23 64 65 66 69 6e 65 20 44  ==0).  #define D
80430 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
80440 28 78 29 20 69 66 20 28 21 78 20 7c 7c 20 21 73  (x) if (!x || !s
80450 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e  qlite3IsMemJourn
80460 61 6c 28 78 29 29 20 7b 20 20 20 20 20 5c 0a 20  al(x)) {     \. 
80470 20 20 20 76 6f 69 64 20 2a 70 54 73 74 41 6c 6c     void *pTstAll
80480 6f 63 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  oc = sqlite3Mall
80490 6f 63 28 31 30 29 3b 20 20 20 20 20 20 20 20 20  oc(10);         
804a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
804b0 20 20 20 20 5c 0a 20 20 20 20 69 66 20 28 21 70      \.    if (!p
804c0 54 73 74 41 6c 6c 6f 63 29 20 72 65 74 75 72 6e  TstAlloc) return
804d0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f   SQLITE_IOERR_NO
804e0 4d 45 4d 3b 20 20 20 20 20 20 20 20 20 20 20 20  MEM;            
804f0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
80500 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54   sqlite3_free(pT
80510 73 74 41 6c 6c 6f 63 29 3b 20 20 20 20 20 20 20  stAlloc);       
80520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80540 20 20 5c 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20    \.  }.#else.  
80550 23 64 65 66 69 6e 65 20 44 4f 5f 4f 53 5f 4d 41  #define DO_OS_MA
80560 4c 4c 4f 43 5f 54 45 53 54 28 78 29 0a 23 65 6e  LLOC_TEST(x).#en
80570 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  dif../*.** The f
80580 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
80590 73 20 61 72 65 20 63 6f 6e 76 65 6e 69 65 6e 63  s are convenienc
805a0 65 20 77 72 61 70 70 65 72 73 20 61 72 6f 75 6e  e wrappers aroun
805b0 64 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20  d methods.** of 
805c0 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  the sqlite3_file
805d0 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20 69   object.  This i
805e0 73 20 6d 6f 73 74 6c 79 20 6a 75 73 74 20 73 79  s mostly just sy
805f0 6e 74 61 63 74 69 63 20 73 75 67 61 72 2e 20 41  ntactic sugar. A
80600 6c 6c 0a 2a 2a 20 6f 66 20 74 68 69 73 20 77 6f  ll.** of this wo
80610 75 6c 64 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c  uld be completel
80620 79 20 61 75 74 6f 6d 61 74 69 63 20 69 66 20 53  y automatic if S
80630 51 4c 69 74 65 20 77 65 72 65 20 63 6f 64 65 64  QLite were coded
80640 20 75 73 69 6e 67 0a 2a 2a 20 43 2b 2b 20 69 6e   using.** C++ in
80650 73 74 65 61 64 20 6f 66 20 70 6c 61 69 6e 20 6f  stead of plain o
80660 6c 64 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ld C..*/.SQLITE_
80670 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
80680 74 65 33 4f 73 43 6c 6f 73 65 28 73 71 6c 69 74  te3OsClose(sqlit
80690 65 33 5f 66 69 6c 65 20 2a 70 49 64 29 7b 0a 20  e3_file *pId){. 
806a0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
806b0 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 49 64 2d 3e  _OK;.  if( pId->
806c0 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  pMethods ){.    
806d0 72 63 20 3d 20 70 49 64 2d 3e 70 4d 65 74 68 6f  rc = pId->pMetho
806e0 64 73 2d 3e 78 43 6c 6f 73 65 28 70 49 64 29 3b  ds->xClose(pId);
806f0 0a 20 20 20 20 70 49 64 2d 3e 70 4d 65 74 68 6f  .    pId->pMetho
80700 64 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  ds = 0;.  }.  re
80710 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54  turn rc;.}.SQLIT
80720 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
80730 6c 69 74 65 33 4f 73 52 65 61 64 28 73 71 6c 69  lite3OsRead(sqli
80740 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 76 6f  te3_file *id, vo
80750 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d  id *pBuf, int am
80760 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 7b 0a  t, i64 offset){.
80770 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54    DO_OS_MALLOC_T
80780 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72  EST(id);.  retur
80790 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e  n id->pMethods->
807a0 78 52 65 61 64 28 69 64 2c 20 70 42 75 66 2c 20  xRead(id, pBuf, 
807b0 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a  amt, offset);.}.
807c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
807d0 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74  nt sqlite3OsWrit
807e0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
807f0 69 64 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  id, const void *
80800 70 42 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20 69  pBuf, int amt, i
80810 36 34 20 6f 66 66 73 65 74 29 7b 0a 20 20 44 4f  64 offset){.  DO
80820 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28  _OS_MALLOC_TEST(
80830 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 64  id);.  return id
80840 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 57 72 69  ->pMethods->xWri
80850 74 65 28 69 64 2c 20 70 42 75 66 2c 20 61 6d 74  te(id, pBuf, amt
80860 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51 4c  , offset);.}.SQL
80870 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
80880 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74  sqlite3OsTruncat
80890 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
808a0 69 64 2c 20 69 36 34 20 73 69 7a 65 29 7b 0a 20  id, i64 size){. 
808b0 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74   return id->pMet
808c0 68 6f 64 73 2d 3e 78 54 72 75 6e 63 61 74 65 28  hods->xTruncate(
808d0 69 64 2c 20 73 69 7a 65 29 3b 0a 7d 0a 53 51 4c  id, size);.}.SQL
808e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
808f0 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 73 71  sqlite3OsSync(sq
80900 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
80910 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 44 4f  int flags){.  DO
80920 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28  _OS_MALLOC_TEST(
80930 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 64  id);.  return id
80940 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 79 6e  ->pMethods->xSyn
80950 63 28 69 64 2c 20 66 6c 61 67 73 29 3b 0a 7d 0a  c(id, flags);.}.
80960 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
80970 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65  nt sqlite3OsFile
80980 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  Size(sqlite3_fil
80990 65 20 2a 69 64 2c 20 69 36 34 20 2a 70 53 69 7a  e *id, i64 *pSiz
809a0 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c  e){.  DO_OS_MALL
809b0 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72  OC_TEST(id);.  r
809c0 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f  eturn id->pMetho
809d0 64 73 2d 3e 78 46 69 6c 65 53 69 7a 65 28 69 64  ds->xFileSize(id
809e0 2c 20 70 53 69 7a 65 29 3b 0a 7d 0a 53 51 4c 49  , pSize);.}.SQLI
809f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
80a00 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71 6c  qlite3OsLock(sql
80a10 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
80a20 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20  nt lockType){.  
80a30 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53  DO_OS_MALLOC_TES
80a40 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  T(id);.  return 
80a50 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 4c  id->pMethods->xL
80a60 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 70 65  ock(id, lockType
80a70 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
80a80 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
80a90 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  sUnlock(sqlite3_
80aa0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f  file *id, int lo
80ab0 63 6b 54 79 70 65 29 7b 0a 20 20 72 65 74 75 72  ckType){.  retur
80ac0 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e  n id->pMethods->
80ad0 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b  xUnlock(id, lock
80ae0 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  Type);.}.SQLITE_
80af0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
80b00 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76  te3OsCheckReserv
80b10 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  edLock(sqlite3_f
80b20 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52  ile *id, int *pR
80b30 65 73 4f 75 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f  esOut){.  DO_OS_
80b40 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b  MALLOC_TEST(id);
80b50 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d  .  return id->pM
80b60 65 74 68 6f 64 73 2d 3e 78 43 68 65 63 6b 52 65  ethods->xCheckRe
80b70 73 65 72 76 65 64 4c 6f 63 6b 28 69 64 2c 20 70  servedLock(id, p
80b80 52 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54  ResOut);.}.SQLIT
80b90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
80ba0 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72  lite3OsFileContr
80bb0 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ol(sqlite3_file 
80bc0 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69  *id, int op, voi
80bd0 64 20 2a 70 41 72 67 29 7b 0a 20 20 72 65 74 75  d *pArg){.  retu
80be0 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d  rn id->pMethods-
80bf0 3e 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 64  >xFileControl(id
80c00 2c 20 6f 70 2c 20 70 41 72 67 29 3b 0a 7d 0a 53  , op, pArg);.}.S
80c10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
80c20 74 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f  t sqlite3OsSecto
80c30 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69  rSize(sqlite3_fi
80c40 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 28  le *id){.  int (
80c50 2a 78 53 65 63 74 6f 72 53 69 7a 65 29 28 73 71  *xSectorSize)(sq
80c60 6c 69 74 65 33 5f 66 69 6c 65 2a 29 20 3d 20 69  lite3_file*) = i
80c70 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 65  d->pMethods->xSe
80c80 63 74 6f 72 53 69 7a 65 3b 0a 20 20 72 65 74 75  ctorSize;.  retu
80c90 72 6e 20 28 78 53 65 63 74 6f 72 53 69 7a 65 20  rn (xSectorSize 
80ca0 3f 20 78 53 65 63 74 6f 72 53 69 7a 65 28 69 64  ? xSectorSize(id
80cb0 29 20 3a 20 53 51 4c 49 54 45 5f 44 45 46 41 55  ) : SQLITE_DEFAU
80cc0 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 29 3b  LT_SECTOR_SIZE);
80cd0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
80ce0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44  E int sqlite3OsD
80cf0 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
80d00 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c  tics(sqlite3_fil
80d10 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e  e *id){.  return
80d20 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78   id->pMethods->x
80d30 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
80d40 73 74 69 63 73 28 69 64 29 3b 0a 7d 0a 0a 2f 2a  stics(id);.}../*
80d50 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 72 6f  .** The next gro
80d60 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61  up of routines a
80d70 72 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77  re convenience w
80d80 72 61 70 70 65 72 73 20 61 72 6f 75 6e 64 20 74  rappers around t
80d90 68 65 0a 2a 2a 20 56 46 53 20 6d 65 74 68 6f 64  he.** VFS method
80da0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
80db0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
80dc0 4f 73 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65  OsOpen(.  sqlite
80dd0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20  3_vfs *pVfs, .  
80de0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74  const char *zPat
80df0 68 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69  h, .  sqlite3_fi
80e00 6c 65 20 2a 70 46 69 6c 65 2c 20 0a 20 20 69 6e  le *pFile, .  in
80e10 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20  t flags, .  int 
80e20 2a 70 46 6c 61 67 73 4f 75 74 0a 29 7b 0a 20 20  *pFlagsOut.){.  
80e30 69 6e 74 20 72 63 3b 0a 20 20 44 4f 5f 4f 53 5f  int rc;.  DO_OS_
80e40 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 30 29 3b 0a  MALLOC_TEST(0);.
80e50 20 20 2f 2a 20 30 78 37 66 31 66 20 69 73 20 61    /* 0x7f1f is a
80e60 20 6d 61 73 6b 20 6f 66 20 53 51 4c 49 54 45 5f   mask of SQLITE_
80e70 4f 50 45 4e 5f 20 66 6c 61 67 73 20 74 68 61 74  OPEN_ flags that
80e80 20 61 72 65 20 76 61 6c 69 64 20 74 6f 20 62 65   are valid to be
80e90 20 70 61 73 73 65 64 0a 20 20 2a 2a 20 64 6f 77   passed.  ** dow
80ea0 6e 20 69 6e 74 6f 20 74 68 65 20 56 46 53 20 6c  n into the VFS l
80eb0 61 79 65 72 2e 20 20 53 6f 6d 65 20 53 51 4c 49  ayer.  Some SQLI
80ec0 54 45 5f 4f 50 45 4e 5f 20 66 6c 61 67 73 20 28  TE_OPEN_ flags (
80ed0 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 20 20 2a  for example,.  *
80ee0 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55  * SQLITE_OPEN_FU
80ef0 4c 4c 4d 55 54 45 58 20 6f 72 20 53 51 4c 49 54  LLMUTEX or SQLIT
80f00 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43  E_OPEN_SHAREDCAC
80f10 48 45 29 20 61 72 65 20 62 6c 6f 63 6b 65 64 20  HE) are blocked 
80f20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 72 65 61 63  before.  ** reac
80f30 68 69 6e 67 20 74 68 65 20 56 46 53 2e 20 2a 2f  hing the VFS. */
80f40 0a 20 20 72 63 20 3d 20 70 56 66 73 2d 3e 78 4f  .  rc = pVfs->xO
80f50 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 2c  pen(pVfs, zPath,
80f60 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 20 26 20   pFile, flags & 
80f70 30 78 37 66 31 66 2c 20 70 46 6c 61 67 73 4f 75  0x7f1f, pFlagsOu
80f80 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  t);.  assert( rc
80f90 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70  ==SQLITE_OK || p
80fa0 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d  File->pMethods==
80fb0 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  0 );.  return rc
80fc0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
80fd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
80fe0 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 5f 76  Delete(sqlite3_v
80ff0 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20  fs *pVfs, const 
81000 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 69 6e 74  char *zPath, int
81010 20 64 69 72 53 79 6e 63 29 7b 0a 20 20 72 65 74   dirSync){.  ret
81020 75 72 6e 20 70 56 66 73 2d 3e 78 44 65 6c 65 74  urn pVfs->xDelet
81030 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 64  e(pVfs, zPath, d
81040 69 72 53 79 6e 63 29 3b 0a 7d 0a 53 51 4c 49 54  irSync);.}.SQLIT
81050 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
81060 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 0a 20  lite3OsAccess(. 
81070 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
81080 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  fs, .  const cha
81090 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 69 6e 74  r *zPath, .  int
810a0 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20 2a   flags, .  int *
810b0 70 52 65 73 4f 75 74 0a 29 7b 0a 20 20 44 4f 5f  pResOut.){.  DO_
810c0 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 30  OS_MALLOC_TEST(0
810d0 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73  );.  return pVfs
810e0 2d 3e 78 41 63 63 65 73 73 28 70 56 66 73 2c 20  ->xAccess(pVfs, 
810f0 7a 50 61 74 68 2c 20 66 6c 61 67 73 2c 20 70 52  zPath, flags, pR
81100 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45  esOut);.}.SQLITE
81110 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
81120 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61  ite3OsFullPathna
81130 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  me(.  sqlite3_vf
81140 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73  s *pVfs, .  cons
81150 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a  t char *zPath, .
81160 20 20 69 6e 74 20 6e 50 61 74 68 4f 75 74 2c 20    int nPathOut, 
81170 0a 20 20 63 68 61 72 20 2a 7a 50 61 74 68 4f 75  .  char *zPathOu
81180 74 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56  t.){.  return pV
81190 66 73 2d 3e 78 46 75 6c 6c 50 61 74 68 6e 61 6d  fs->xFullPathnam
811a0 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 6e  e(pVfs, zPath, n
811b0 50 61 74 68 4f 75 74 2c 20 7a 50 61 74 68 4f 75  PathOut, zPathOu
811c0 74 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51  t);.}.#ifndef SQ
811d0 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
811e0 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f  XTENSION.SQLITE_
811f0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
81200 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71  lite3OsDlOpen(sq
81210 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
81220 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61   const char *zPa
81230 74 68 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56  th){.  return pV
81240 66 73 2d 3e 78 44 6c 4f 70 65 6e 28 70 56 66 73  fs->xDlOpen(pVfs
81250 2c 20 7a 50 61 74 68 29 3b 0a 7d 0a 53 51 4c 49  , zPath);.}.SQLI
81260 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
81270 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72  sqlite3OsDlError
81280 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
81290 66 73 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63  fs, int nByte, c
812a0 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20  har *zBufOut){. 
812b0 20 70 56 66 73 2d 3e 78 44 6c 45 72 72 6f 72 28   pVfs->xDlError(
812c0 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75  pVfs, nByte, zBu
812d0 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  fOut);.}.SQLITE_
812e0 50 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73  PRIVATE void (*s
812f0 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 73 71  qlite3OsDlSym(sq
81300 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
81310 20 76 6f 69 64 20 2a 70 48 64 6c 65 2c 20 63 6f   void *pHdle, co
81320 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 29 29  nst char *zSym))
81330 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e  (void){.  return
81340 20 70 56 66 73 2d 3e 78 44 6c 53 79 6d 28 70 56   pVfs->xDlSym(pV
81350 66 73 2c 20 70 48 64 6c 65 2c 20 7a 53 79 6d 29  fs, pHdle, zSym)
81360 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
81370 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f  TE void sqlite3O
81380 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  sDlClose(sqlite3
81390 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64  _vfs *pVfs, void
813a0 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 70 56   *pHandle){.  pV
813b0 66 73 2d 3e 78 44 6c 43 6c 6f 73 65 28 70 56 66  fs->xDlClose(pVf
813c0 73 2c 20 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23  s, pHandle);.}.#
813d0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
813e0 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53  OMIT_LOAD_EXTENS
813f0 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ION */.SQLITE_PR
81400 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
81410 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71  3OsRandomness(sq
81420 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
81430 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72   int nByte, char
81440 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 72 65   *zBufOut){.  re
81450 74 75 72 6e 20 70 56 66 73 2d 3e 78 52 61 6e 64  turn pVfs->xRand
81460 6f 6d 6e 65 73 73 28 70 56 66 73 2c 20 6e 42 79  omness(pVfs, nBy
81470 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a  te, zBufOut);.}.
81480 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
81490 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65  nt sqlite3OsSlee
814a0 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  p(sqlite3_vfs *p
814b0 56 66 73 2c 20 69 6e 74 20 6e 4d 69 63 72 6f 29  Vfs, int nMicro)
814c0 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d  {.  return pVfs-
814d0 3e 78 53 6c 65 65 70 28 70 56 66 73 2c 20 6e 4d  >xSleep(pVfs, nM
814e0 69 63 72 6f 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  icro);.}.SQLITE_
814f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
81500 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65  te3OsCurrentTime
81510 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
81520 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 54 69 6d  fs, double *pTim
81530 65 4f 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20  eOut){.  return 
81540 70 56 66 73 2d 3e 78 43 75 72 72 65 6e 74 54 69  pVfs->xCurrentTi
81550 6d 65 28 70 56 66 73 2c 20 70 54 69 6d 65 4f 75  me(pVfs, pTimeOu
81560 74 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52  t);.}..SQLITE_PR
81570 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
81580 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 0a 20  3OsOpenMalloc(. 
81590 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
815a0 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  fs, .  const cha
815b0 72 20 2a 7a 46 69 6c 65 2c 20 0a 20 20 73 71 6c  r *zFile, .  sql
815c0 69 74 65 33 5f 66 69 6c 65 20 2a 2a 70 70 46 69  ite3_file **ppFi
815d0 6c 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73  le, .  int flags
815e0 2c 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61  ,.  int *pOutFla
815f0 67 73 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  gs.){.  int rc =
81600 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
81610 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
81620 46 69 6c 65 3b 0a 20 20 70 46 69 6c 65 20 3d 20  File;.  pFile = 
81630 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29  (sqlite3_file *)
81640 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 70 56  sqlite3Malloc(pV
81650 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20  fs->szOsFile);. 
81660 20 69 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20   if( pFile ){.  
81670 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
81680 4f 70 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65  Open(pVfs, zFile
81690 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20  , pFile, flags, 
816a0 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20  pOutFlags);.    
816b0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
816c0 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  K ){.      sqlit
816d0 65 33 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a  e3_free(pFile);.
816e0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
816f0 20 2a 70 70 46 69 6c 65 20 3d 20 70 46 69 6c 65   *ppFile = pFile
81700 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
81710 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54  turn rc;.}.SQLIT
81720 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
81730 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65  lite3OsCloseFree
81740 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
81750 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 20  File){.  int rc 
81760 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61  = SQLITE_OK;.  a
81770 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a  ssert( pFile );.
81780 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
81790 43 6c 6f 73 65 28 70 46 69 6c 65 29 3b 0a 20 20  Close(pFile);.  
817a0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69  sqlite3_free(pFi
817b0 6c 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  le);.  return rc
817c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
817d0 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 77 72  function is a wr
817e0 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65  apper around the
817f0 20 4f 53 20 73 70 65 63 69 66 69 63 20 69 6d 70   OS specific imp
81800 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 0a 2a  lementation of.*
81810 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69  * sqlite3_os_ini
81820 74 28 29 2e 20 54 68 65 20 70 75 72 70 6f 73 65  t(). The purpose
81830 20 6f 66 20 74 68 65 20 77 72 61 70 70 65 72 20   of the wrapper 
81840 69 73 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68  is to provide th
81850 65 0a 2a 2a 20 61 62 69 6c 69 74 79 20 74 6f 20  e.** ability to 
81860 73 69 6d 75 6c 61 74 65 20 61 20 6d 61 6c 6c 6f  simulate a mallo
81870 63 20 66 61 69 6c 75 72 65 2c 20 73 6f 20 74 68  c failure, so th
81880 61 74 20 74 68 65 20 68 61 6e 64 6c 69 6e 67 20  at the handling 
81890 6f 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 69  of an.** error i
818a0 6e 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69  n sqlite3_os_ini
818b0 74 28 29 20 62 79 20 74 68 65 20 75 70 70 65 72  t() by the upper
818c0 20 6c 61 79 65 72 73 20 63 61 6e 20 62 65 20 74   layers can be t
818d0 65 73 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ested..*/.SQLITE
818e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
818f0 69 74 65 33 4f 73 49 6e 69 74 28 76 6f 69 64 29  ite3OsInit(void)
81900 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 71  {.  void *p = sq
81910 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 31 30 29  lite3_malloc(10)
81920 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
81930 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
81940 45 4d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  EM;.  sqlite3_fr
81950 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ee(p);.  return 
81960 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28  sqlite3_os_init(
81970 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  );.}../*.** The 
81980 6c 69 73 74 20 6f 66 20 61 6c 6c 20 72 65 67 69  list of all regi
81990 73 74 65 72 65 64 20 56 46 53 20 69 6d 70 6c 65  stered VFS imple
819a0 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73  mentations..*/.s
819b0 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66  tatic sqlite3_vf
819c0 73 20 2a 20 53 51 4c 49 54 45 5f 57 53 44 20 76  s * SQLITE_WSD v
819d0 66 73 4c 69 73 74 20 3d 20 30 3b 0a 23 64 65 66  fsList = 0;.#def
819e0 69 6e 65 20 76 66 73 4c 69 73 74 20 47 4c 4f 42  ine vfsList GLOB
819f0 41 4c 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  AL(sqlite3_vfs *
81a00 2c 20 76 66 73 4c 69 73 74 29 0a 0a 2f 2a 0a 2a  , vfsList)../*.*
81a10 2a 20 4c 6f 63 61 74 65 20 61 20 56 46 53 20 62  * Locate a VFS b
81a20 79 20 6e 61 6d 65 2e 20 20 49 66 20 6e 6f 20 6e  y name.  If no n
81a30 61 6d 65 20 69 73 20 67 69 76 65 6e 2c 20 73 69  ame is given, si
81a40 6d 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65 0a  mply return the.
81a50 2a 2a 20 66 69 72 73 74 20 56 46 53 20 6f 6e 20  ** first VFS on 
81a60 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c  the list..*/.SQL
81a70 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
81a80 76 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 73  vfs *sqlite3_vfs
81a90 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 72  _find(const char
81aa0 20 2a 7a 56 66 73 29 7b 0a 20 20 73 71 6c 69 74   *zVfs){.  sqlit
81ab0 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 30  e3_vfs *pVfs = 0
81ac0 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  ;.#if SQLITE_THR
81ad0 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65  EADSAFE.  sqlite
81ae0 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a  3_mutex *mutex;.
81af0 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53  #endif.#ifndef S
81b00 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
81b10 4e 49 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 73  NIT.  int rc = s
81b20 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
81b30 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  e();.  if( rc ) 
81b40 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66  return 0;.#endif
81b50 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
81b60 41 44 53 41 46 45 0a 20 20 6d 75 74 65 78 20 3d  ADSAFE.  mutex =
81b70 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
81b80 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
81b90 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a  STATIC_MASTER);.
81ba0 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33  #endif.  sqlite3
81bb0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74  _mutex_enter(mut
81bc0 65 78 29 3b 0a 20 20 66 6f 72 28 70 56 66 73 20  ex);.  for(pVfs 
81bd0 3d 20 76 66 73 4c 69 73 74 3b 20 70 56 66 73 3b  = vfsList; pVfs;
81be0 20 70 56 66 73 3d 70 56 66 73 2d 3e 70 4e 65 78   pVfs=pVfs->pNex
81bf0 74 29 7b 0a 20 20 20 20 69 66 28 20 7a 56 66 73  t){.    if( zVfs
81c00 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ==0 ) break;.   
81c10 20 69 66 28 20 73 74 72 63 6d 70 28 7a 56 66 73   if( strcmp(zVfs
81c20 2c 20 70 56 66 73 2d 3e 7a 4e 61 6d 65 29 3d 3d  , pVfs->zName)==
81c30 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20  0 ) break;.  }. 
81c40 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
81c50 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72  eave(mutex);.  r
81c60 65 74 75 72 6e 20 70 56 66 73 3b 0a 7d 0a 0a 2f  eturn pVfs;.}../
81c70 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 61 20 56 46  *.** Unlink a VF
81c80 53 20 66 72 6f 6d 20 74 68 65 20 6c 69 6e 6b 65  S from the linke
81c90 64 20 6c 69 73 74 0a 2a 2f 0a 73 74 61 74 69 63  d list.*/.static
81ca0 20 76 6f 69 64 20 76 66 73 55 6e 6c 69 6e 6b 28   void vfsUnlink(
81cb0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
81cc0 73 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  s){.  assert( sq
81cd0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
81ce0 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c  (sqlite3MutexAll
81cf0 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
81d00 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 20  STATIC_MASTER)) 
81d10 29 3b 0a 20 20 69 66 28 20 70 56 66 73 3d 3d 30  );.  if( pVfs==0
81d20 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70   ){.    /* No-op
81d30 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   */.  }else if( 
81d40 76 66 73 4c 69 73 74 3d 3d 70 56 66 73 20 29 7b  vfsList==pVfs ){
81d50 0a 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20 70  .    vfsList = p
81d60 56 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65  Vfs->pNext;.  }e
81d70 6c 73 65 20 69 66 28 20 76 66 73 4c 69 73 74 20  lse if( vfsList 
81d80 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  ){.    sqlite3_v
81d90 66 73 20 2a 70 20 3d 20 76 66 73 4c 69 73 74 3b  fs *p = vfsList;
81da0 0a 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 70  .    while( p->p
81db0 4e 65 78 74 20 26 26 20 70 2d 3e 70 4e 65 78 74  Next && p->pNext
81dc0 21 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20  !=pVfs ){.      
81dd0 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20  p = p->pNext;.  
81de0 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70    }.    if( p->p
81df0 4e 65 78 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20  Next==pVfs ){.  
81e00 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70      p->pNext = p
81e10 56 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  Vfs->pNext;.    
81e20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  }.  }.}../*.** R
81e30 65 67 69 73 74 65 72 20 61 20 56 46 53 20 77 69  egister a VFS wi
81e40 74 68 20 74 68 65 20 73 79 73 74 65 6d 2e 20 20  th the system.  
81e50 49 74 20 69 73 20 68 61 72 6d 6c 65 73 73 20 74  It is harmless t
81e60 6f 20 72 65 67 69 73 74 65 72 20 74 68 65 20 73  o register the s
81e70 61 6d 65 0a 2a 2a 20 56 46 53 20 6d 75 6c 74 69  ame.** VFS multi
81e80 70 6c 65 20 74 69 6d 65 73 2e 20 20 54 68 65 20  ple times.  The 
81e90 6e 65 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20  new VFS becomes 
81ea0 74 68 65 20 64 65 66 61 75 6c 74 20 69 66 20 6d  the default if m
81eb0 61 6b 65 44 66 6c 74 20 69 73 0a 2a 2a 20 74 72  akeDflt is.** tr
81ec0 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ue..*/.SQLITE_AP
81ed0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66  I int sqlite3_vf
81ee0 73 5f 72 65 67 69 73 74 65 72 28 73 71 6c 69 74  s_register(sqlit
81ef0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
81f00 74 20 6d 61 6b 65 44 66 6c 74 29 7b 0a 20 20 73  t makeDflt){.  s
81f10 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
81f20 74 65 78 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66  tex = 0;.#ifndef
81f30 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
81f40 4f 49 4e 49 54 0a 20 20 69 6e 74 20 72 63 20 3d  OINIT.  int rc =
81f50 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
81f60 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20  ize();.  if( rc 
81f70 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e  ) return rc;.#en
81f80 64 69 66 0a 20 20 6d 75 74 65 78 20 3d 20 73 71  dif.  mutex = sq
81f90 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
81fa0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
81fb0 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73  TIC_MASTER);.  s
81fc0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
81fd0 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 76 66 73  er(mutex);.  vfs
81fe0 55 6e 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20  Unlink(pVfs);.  
81ff0 69 66 28 20 6d 61 6b 65 44 66 6c 74 20 7c 7c 20  if( makeDflt || 
82000 76 66 73 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20  vfsList==0 ){.  
82010 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20    pVfs->pNext = 
82020 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 76 66 73  vfsList;.    vfs
82030 4c 69 73 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d  List = pVfs;.  }
82040 65 6c 73 65 7b 0a 20 20 20 20 70 56 66 73 2d 3e  else{.    pVfs->
82050 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74 2d  pNext = vfsList-
82060 3e 70 4e 65 78 74 3b 0a 20 20 20 20 76 66 73 4c  >pNext;.    vfsL
82070 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 56 66  ist->pNext = pVf
82080 73 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  s;.  }.  assert(
82090 76 66 73 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69  vfsList);.  sqli
820a0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
820b0 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
820c0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
820d0 2a 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 20  *.** Unregister 
820e0 61 20 56 46 53 20 73 6f 20 74 68 61 74 20 69 74  a VFS so that it
820f0 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 61 63   is no longer ac
82100 63 65 73 73 69 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  cessible..*/.SQL
82110 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
82120 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74  te3_vfs_unregist
82130 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  er(sqlite3_vfs *
82140 70 56 66 73 29 7b 0a 23 69 66 20 53 51 4c 49 54  pVfs){.#if SQLIT
82150 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73  E_THREADSAFE.  s
82160 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
82170 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  tex = sqlite3Mut
82180 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
82190 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
821a0 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71  ER);.#endif.  sq
821b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
821c0 72 28 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 55  r(mutex);.  vfsU
821d0 6e 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 73  nlink(pVfs);.  s
821e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
821f0 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  ve(mutex);.  ret
82200 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
82210 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
82220 2a 20 45 6e 64 20 6f 66 20 6f 73 2e 63 20 2a 2a  * End of os.c **
82230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82260 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
82270 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 61 75  * Begin file fau
82280 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  lt.c ***********
82290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
822a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
822b0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 61 6e  /./*.** 2008 Jan
822c0 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   22.**.** The au
822d0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
822e0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
822f0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
82300 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
82310 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
82320 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
82330 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
82340 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
82350 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
82360 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
82370 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
82380 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
82390 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
823a0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
823b0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
823c0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
823d0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
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 2a  ****************
82400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82420 2a 2a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 66 61  **.**.** $Id: fa
82430 75 6c 74 2e 63 2c 76 20 31 2e 31 31 20 32 30 30  ult.c,v 1.11 200
82440 38 2f 30 39 2f 30 32 20 30 30 3a 35 32 3a 35 32  8/09/02 00:52:52
82450 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f   drh Exp $.*/../
82460 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
82470 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f 20  ontains code to 
82480 73 75 70 70 6f 72 74 20 74 68 65 20 63 6f 6e 63  support the conc
82490 65 70 74 20 6f 66 20 22 62 65 6e 69 67 6e 22 20  ept of "benign" 
824a0 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75  .** malloc failu
824b0 72 65 73 20 28 77 68 65 6e 20 74 68 65 20 78 4d  res (when the xM
824c0 61 6c 6c 6f 63 28 29 20 6f 72 20 78 52 65 61 6c  alloc() or xReal
824d0 6c 6f 63 28 29 20 6d 65 74 68 6f 64 20 6f 66 20  loc() method of 
824e0 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d  the.** sqlite3_m
824f0 65 6d 5f 6d 65 74 68 6f 64 73 20 73 74 72 75 63  em_methods struc
82500 74 75 72 65 20 66 61 69 6c 73 20 74 6f 20 61 6c  ture fails to al
82510 6c 6f 63 61 74 65 20 61 20 62 6c 6f 63 6b 20 6f  locate a block o
82520 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6e 64 20  f memory.** and 
82530 72 65 74 75 72 6e 73 20 30 29 2e 20 0a 2a 2a 0a  returns 0). .**.
82540 2a 2a 20 4d 6f 73 74 20 6d 61 6c 6c 6f 63 20 66  ** Most malloc f
82550 61 69 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e 2d  ailures are non-
82560 62 65 6e 69 67 6e 2e 20 41 66 74 65 72 20 74 68  benign. After th
82570 65 79 20 6f 63 63 75 72 2c 20 53 51 4c 69 74 65  ey occur, SQLite
82580 0a 2a 2a 20 61 62 61 6e 64 6f 6e 73 20 74 68 65  .** abandons the
82590 20 63 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69   current operati
825a0 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61  on and returns a
825b0 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 28 75 73  n error code (us
825c0 75 61 6c 6c 79 0a 2a 2a 20 53 51 4c 49 54 45 5f  ually.** SQLITE_
825d0 4e 4f 4d 45 4d 29 20 74 6f 20 74 68 65 20 75 73  NOMEM) to the us
825e0 65 72 2e 20 48 6f 77 65 76 65 72 2c 20 73 6f 6d  er. However, som
825f0 65 74 69 6d 65 73 20 61 20 66 61 75 6c 74 20 69  etimes a fault i
82600 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c  s not necessaril
82610 79 0a 2a 2a 20 66 61 74 61 6c 2e 20 46 6f 72 20  y.** fatal. For 
82620 65 78 61 6d 70 6c 65 2c 20 69 66 20 61 20 6d 61  example, if a ma
82630 6c 6c 6f 63 20 66 61 69 6c 73 20 77 68 69 6c 65  lloc fails while
82640 20 72 65 73 69 7a 69 6e 67 20 61 20 68 61 73 68   resizing a hash
82650 20 74 61 62 6c 65 2c 20 74 68 69 73 20 0a 2a 2a   table, this .**
82660 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 72   is completely r
82670 65 63 6f 76 65 72 61 62 6c 65 20 73 69 6d 70 6c  ecoverable simpl
82680 79 20 62 79 20 6e 6f 74 20 63 61 72 72 79 69 6e  y by not carryin
82690 67 20 6f 75 74 20 74 68 65 20 72 65 73 69 7a 65  g out the resize
826a0 2e 20 54 68 65 20 0a 2a 2a 20 68 61 73 68 20 74  . The .** hash t
826b0 61 62 6c 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e  able will contin
826c0 75 65 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 6e  ue to function n
826d0 6f 72 6d 61 6c 6c 79 2e 20 20 53 6f 20 61 20 6d  ormally.  So a m
826e0 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 0a 2a  alloc failure .*
826f0 2a 20 64 75 72 69 6e 67 20 61 20 68 61 73 68 20  * during a hash 
82700 74 61 62 6c 65 20 72 65 73 69 7a 65 20 69 73 20  table resize is 
82710 61 20 62 65 6e 69 67 6e 20 66 61 75 6c 74 2e 0a  a benign fault..
82720 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  */...#ifndef SQL
82730 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e  ITE_OMIT_BUILTIN
82740 5f 54 45 53 54 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f  _TEST../*.** Glo
82750 62 61 6c 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a  bal variables..*
82760 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
82770 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f   BenignMallocHoo
82780 6b 73 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48  ks BenignMallocH
82790 6f 6f 6b 73 3b 0a 73 74 61 74 69 63 20 53 51 4c  ooks;.static SQL
827a0 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 42  ITE_WSD struct B
827b0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73  enignMallocHooks
827c0 20 7b 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e   {.  void (*xBen
827d0 69 67 6e 42 65 67 69 6e 29 28 76 6f 69 64 29 3b  ignBegin)(void);
827e0 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67  .  void (*xBenig
827f0 6e 45 6e 64 29 28 76 6f 69 64 29 3b 0a 7d 20 73  nEnd)(void);.} s
82800 71 6c 69 74 65 33 48 6f 6f 6b 73 20 3d 20 7b 20  qlite3Hooks = { 
82810 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 20 54 68 65 20  0, 0 };../* The 
82820 22 77 73 64 48 6f 6f 6b 73 22 20 6d 61 63 72 6f  "wsdHooks" macro
82830 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f   will resolve to
82840 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
82850 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f   BenignMallocHoo
82860 6b 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  ks.** structure.
82870 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 73 74    If writable st
82880 61 74 69 63 20 64 61 74 61 20 69 73 20 75 6e 73  atic data is uns
82890 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 20  upported on the 
828a0 74 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68 61  target,.** we ha
828b0 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65  ve to locate the
828c0 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61 74   state vector at
828d0 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74   run-time.  In t
828e0 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a  he more common.*
828f0 2a 20 63 61 73 65 20 77 68 65 72 65 20 77 72 69  * case where wri
82900 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74  table static dat
82910 61 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c 20  a is supported, 
82920 77 73 64 48 6f 6f 6b 73 20 63 61 6e 20 72 65 66  wsdHooks can ref
82930 65 72 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74  er directly.** t
82940 6f 20 74 68 65 20 22 73 71 6c 69 74 65 33 48 6f  o the "sqlite3Ho
82950 6f 6b 73 22 20 73 74 61 74 65 20 76 65 63 74 6f  oks" state vecto
82960 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f 76 65  r declared above
82970 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
82980 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 64 65  TE_OMIT_WSD.# de
82990 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69  fine wsdHooksIni
829a0 74 20 5c 0a 20 20 42 65 6e 69 67 6e 4d 61 6c 6c  t \.  BenignMall
829b0 6f 63 48 6f 6f 6b 73 20 2a 78 20 3d 20 26 47 4c  ocHooks *x = &GL
829c0 4f 42 41 4c 28 42 65 6e 69 67 6e 4d 61 6c 6c 6f  OBAL(BenignMallo
829d0 63 48 6f 6f 6b 73 2c 73 71 6c 69 74 65 33 48 6f  cHooks,sqlite3Ho
829e0 6f 6b 73 29 0a 23 20 64 65 66 69 6e 65 20 77 73  oks).# define ws
829f0 64 48 6f 6f 6b 73 20 78 5b 30 5d 0a 23 65 6c 73  dHooks x[0].#els
82a00 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f  e.# define wsdHo
82a10 6f 6b 73 49 6e 69 74 0a 23 20 64 65 66 69 6e 65  oksInit.# define
82a20 20 77 73 64 48 6f 6f 6b 73 20 73 71 6c 69 74 65   wsdHooks sqlite
82a30 33 48 6f 6f 6b 73 0a 23 65 6e 64 69 66 0a 0a 0a  3Hooks.#endif...
82a40 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 68  /*.** Register h
82a50 6f 6f 6b 73 20 74 6f 20 63 61 6c 6c 20 77 68 65  ooks to call whe
82a60 6e 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65  n sqlite3BeginBe
82a70 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 6e 64  nignMalloc() and
82a80 0a 2a 2a 20 73 71 6c 69 74 65 33 45 6e 64 42 65  .** sqlite3EndBe
82a90 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 72 65  nignMalloc() are
82aa0 20 63 61 6c 6c 65 64 2c 20 72 65 73 70 65 63 74   called, respect
82ab0 69 76 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ively..*/.SQLITE
82ac0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
82ad0 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f  lite3BenignMallo
82ae0 63 48 6f 6f 6b 73 28 0a 20 20 76 6f 69 64 20 28  cHooks(.  void (
82af0 2a 78 42 65 6e 69 67 6e 42 65 67 69 6e 29 28 76  *xBenignBegin)(v
82b00 6f 69 64 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78  oid),.  void (*x
82b10 42 65 6e 69 67 6e 45 6e 64 29 28 76 6f 69 64 29  BenignEnd)(void)
82b20 0a 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e  .){.  wsdHooksIn
82b30 69 74 3b 0a 20 20 77 73 64 48 6f 6f 6b 73 2e 78  it;.  wsdHooks.x
82b40 42 65 6e 69 67 6e 42 65 67 69 6e 20 3d 20 78 42  BenignBegin = xB
82b50 65 6e 69 67 6e 42 65 67 69 6e 3b 0a 20 20 77 73  enignBegin;.  ws
82b60 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e  dHooks.xBenignEn
82b70 64 20 3d 20 78 42 65 6e 69 67 6e 45 6e 64 3b 0a  d = xBenignEnd;.
82b80 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 28 73  }../*.** This (s
82b90 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
82ba0 61 6c 6c 6f 63 28 29 29 20 69 73 20 63 61 6c 6c  alloc()) is call
82bb0 65 64 20 62 79 20 53 51 4c 69 74 65 20 63 6f 64  ed by SQLite cod
82bc0 65 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68  e to indicate th
82bd0 61 74 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74  at.** subsequent
82be0 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73   malloc failures
82bf0 20 61 72 65 20 62 65 6e 69 67 6e 2e 20 41 20 63   are benign. A c
82c00 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 45 6e  all to sqlite3En
82c10 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a  dBenignMalloc().
82c20 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 74 68 61  ** indicates tha
82c30 74 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c  t subsequent mal
82c40 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72 65  loc failures are
82c50 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e 0a 2a 2f 0a   non-benign..*/.
82c60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
82c70 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e  oid sqlite3Begin
82c80 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69  BenignMalloc(voi
82c90 64 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e  d){.  wsdHooksIn
82ca0 69 74 3b 0a 20 20 69 66 28 20 77 73 64 48 6f 6f  it;.  if( wsdHoo
82cb0 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e 20  ks.xBenignBegin 
82cc0 29 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73 2e  ){.    wsdHooks.
82cd0 78 42 65 6e 69 67 6e 42 65 67 69 6e 28 29 3b 0a  xBenignBegin();.
82ce0 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49    }.}.SQLITE_PRI
82cf0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
82d00 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  3EndBenignMalloc
82d10 28 76 6f 69 64 29 7b 0a 20 20 77 73 64 48 6f 6f  (void){.  wsdHoo
82d20 6b 73 49 6e 69 74 3b 0a 20 20 69 66 28 20 77 73  ksInit;.  if( ws
82d30 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e  dHooks.xBenignEn
82d40 64 20 29 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b  d ){.    wsdHook
82d50 73 2e 78 42 65 6e 69 67 6e 45 6e 64 28 29 3b 0a  s.xBenignEnd();.
82d60 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 20 20    }.}..#endif   
82d70 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /* #ifndef SQLIT
82d80 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54  E_OMIT_BUILTIN_T
82d90 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  EST */../*******
82da0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66  ******* End of f
82db0 61 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ault.c *********
82dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82de0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
82df0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
82e00 6c 65 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a  le mem0.c ******
82e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82e30 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
82e40 30 38 20 4f 63 74 6f 62 65 72 20 32 38 0a 2a 2a  08 October 28.**
82e50 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
82e60 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
82e70 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
82e80 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
82e90 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
82ea0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
82eb0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
82ec0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
82ed0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
82ee0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
82ef0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
82f00 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
82f10 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
82f20 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
82f30 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
82f40 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
82f50 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
82f60 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
82f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
82fb0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
82fc0 74 61 69 6e 73 20 61 20 6e 6f 2d 6f 70 20 6d 65  tains a no-op me
82fd0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
82fe0 64 72 69 76 65 72 73 20 66 6f 72 20 75 73 65 20  drivers for use 
82ff0 77 68 65 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 5a  when.** SQLITE_Z
83000 45 52 4f 5f 4d 41 4c 4c 4f 43 20 69 73 20 64 65  ERO_MALLOC is de
83010 66 69 6e 65 64 2e 20 20 54 68 65 20 61 6c 6c 6f  fined.  The allo
83020 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20 69  cation drivers i
83030 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 68 65  mplemented.** he
83040 72 65 20 61 6c 77 61 79 73 20 66 61 69 6c 2e 20  re always fail. 
83050 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74   SQLite will not
83060 20 6f 70 65 72 61 74 65 20 77 69 74 68 20 74 68   operate with th
83070 65 73 65 20 64 72 69 76 65 72 73 2e 20 20 54 68  ese drivers.  Th
83080 65 73 65 0a 2a 2a 20 61 72 65 20 6d 65 72 65 6c  ese.** are merel
83090 79 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 2e 20  y placeholders. 
830a0 20 52 65 61 6c 20 64 72 69 76 65 72 73 20 6d 75   Real drivers mu
830b0 73 74 20 62 65 20 73 75 62 73 74 69 74 75 74 65  st be substitute
830c0 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74  d using.** sqlit
830d0 65 33 5f 63 6f 6e 66 69 67 28 29 20 62 65 66 6f  e3_config() befo
830e0 72 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f  re SQLite will o
830f0 70 65 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  perate..**.** $I
83100 64 3a 20 6d 65 6d 30 2e 63 2c 76 20 31 2e 31 20  d: mem0.c,v 1.1 
83110 32 30 30 38 2f 31 30 2f 32 38 20 31 38 3a 35 38  2008/10/28 18:58
83120 3a 32 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :20 drh Exp $.*/
83130 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72  ../*.** This ver
83140 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f  sion of the memo
83150 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20  ry allocator is 
83160 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 49 74  the default.  It
83170 20 69 73 0a 2a 2a 20 75 73 65 64 20 77 68 65 6e   is.** used when
83180 20 6e 6f 20 6f 74 68 65 72 20 6d 65 6d 6f 72 79   no other memory
83190 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 73 70   allocator is sp
831a0 65 63 69 66 69 65 64 20 75 73 69 6e 67 20 63 6f  ecified using co
831b0 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6d 61  mpile-time.** ma
831c0 63 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  cros..*/.#ifdef 
831d0 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c  SQLITE_ZERO_MALL
831e0 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20  OC../*.** No-op 
831f0 76 65 72 73 69 6f 6e 73 20 6f 66 20 61 6c 6c 20  versions of all 
83200 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
83210 6e 20 72 6f 75 74 69 6e 65 73 0a 2a 2f 0a 73 74  n routines.*/.st
83220 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74  atic void *sqlit
83230 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20  e3MemMalloc(int 
83240 6e 42 79 74 65 29 7b 20 72 65 74 75 72 6e 20 30  nByte){ return 0
83250 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ; }.static void 
83260 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 76  sqlite3MemFree(v
83270 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 20 72 65  oid *pPrior){ re
83280 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69 63 20 76  turn; }.static v
83290 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52  oid *sqlite3MemR
832a0 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72  ealloc(void *pPr
832b0 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b  ior, int nByte){
832c0 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61   return 0; }.sta
832d0 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  tic int sqlite3M
832e0 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 50 72  emSize(void *pPr
832f0 69 6f 72 29 7b 20 72 65 74 75 72 6e 20 30 3b 20  ior){ return 0; 
83300 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  }.static int sql
83310 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69  ite3MemRoundup(i
83320 6e 74 20 6e 29 7b 20 72 65 74 75 72 6e 20 6e 3b  nt n){ return n;
83330 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71   }.static int sq
83340 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69  lite3MemInit(voi
83350 64 20 2a 4e 6f 74 55 73 65 64 29 7b 20 72 65 74  d *NotUsed){ ret
83360 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d  urn SQLITE_OK; }
83370 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
83380 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28  ite3MemShutdown(
83390 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 20  void *NotUsed){ 
833a0 72 65 74 75 72 6e 3b 20 7d 0a 0a 2f 2a 0a 2a 2a  return; }../*.**
833b0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
833c0 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e   the only routin
833d0 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77  e in this file w
833e0 69 74 68 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e  ith external lin
833f0 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75  kage..**.** Popu
83400 6c 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76  late the low-lev
83410 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  el memory alloca
83420 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f  tion function po
83430 69 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c  inters in.** sql
83440 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
83450 2e 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73  .m with pointers
83460 20 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73   to the routines
83470 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a   in this file..*
83480 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
83490 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
834a0 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29  SetDefault(void)
834b0 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  {.  static const
834c0 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
834d0 68 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68  hods defaultMeth
834e0 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c  ods = {.     sql
834f0 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20  ite3MemMalloc,. 
83500 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72      sqlite3MemFr
83510 65 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  ee,.     sqlite3
83520 4d 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20  MemRealloc,.    
83530 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c   sqlite3MemSize,
83540 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
83550 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71  Roundup,.     sq
83560 6c 69 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20  lite3MemInit,.  
83570 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75     sqlite3MemShu
83580 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20  tdown,.     0.  
83590 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  };.  sqlite3_con
835a0 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49  fig(SQLITE_CONFI
835b0 47 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75  G_MALLOC, &defau
835c0 6c 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23  ltMethods);.}..#
835d0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
835e0 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a  ZERO_MALLOC */..
835f0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
83600 45 6e 64 20 6f 66 20 6d 65 6d 30 2e 63 20 2a 2a  End of mem0.c **
83610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
83640 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
83650 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 31 2e  Begin file mem1.
83660 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
83670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
83690 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73  /*.** 2007 Augus
836a0 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  t 14.**.** The a
836b0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
836c0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
836d0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
836e0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
836f0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
83700 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
83710 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
83720 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
83730 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
83740 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
83750 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
83760 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
83770 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
83780 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
83790 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
837a0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
837b0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
837c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
837d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
837e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
837f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83800 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
83810 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6c 6f 77  ile contains low
83820 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c  -level memory al
83830 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73  location drivers
83840 20 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 53 51 4c   for when.** SQL
83850 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65  ite will use the
83860 20 73 74 61 6e 64 61 72 64 20 43 2d 6c 69 62 72   standard C-libr
83870 61 72 79 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c  ary malloc/reall
83880 6f 63 2f 66 72 65 65 20 69 6e 74 65 72 66 61 63  oc/free interfac
83890 65 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 74  e.** to obtain t
838a0 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65  he memory it nee
838b0 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ds..**.** This f
838c0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70  ile contains imp
838d0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20  lementations of 
838e0 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65  the low-level me
838f0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a  mory allocation.
83900 2a 2a 20 72 6f 75 74 69 6e 65 73 20 73 70 65 63  ** routines spec
83910 69 66 69 65 64 20 69 6e 20 74 68 65 20 73 71 6c  ified in the sql
83920 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
83930 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 24   object..**.** $
83940 49 64 3a 20 6d 65 6d 31 2e 63 2c 76 20 31 2e 33  Id: mem1.c,v 1.3
83950 30 20 32 30 30 39 2f 30 33 2f 32 33 20 30 34 3a  0 2009/03/23 04:
83960 33 33 3a 33 33 20 64 61 6e 69 65 6c 6b 31 39 37  33:33 danielk197
83970 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  7 Exp $.*/../*.*
83980 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * This version o
83990 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  f the memory all
839a0 6f 63 61 74 6f 72 20 69 73 20 74 68 65 20 64 65  ocator is the de
839b0 66 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a  fault.  It is.**
839c0 20 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74   used when no ot
839d0 68 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  her memory alloc
839e0 61 74 6f 72 20 69 73 20 73 70 65 63 69 66 69 65  ator is specifie
839f0 64 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d  d using compile-
83a00 74 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a  time.** macros..
83a10 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
83a20 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 0a 0a  _SYSTEM_MALLOC..
83a30 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 6d 61 6c 6c 6f  /*.** Like mallo
83a40 63 28 29 2c 20 62 75 74 20 72 65 6d 65 6d 62 65  c(), but remembe
83a50 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  r the size of th
83a60 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20  e allocation.** 
83a70 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 66  so that we can f
83a80 69 6e 64 20 69 74 20 6c 61 74 65 72 20 75 73 69  ind it later usi
83a90 6e 67 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a  ng sqlite3MemSiz
83aa0 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74  e()..**.** For t
83ab0 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f  his low-level ro
83ac0 75 74 69 6e 65 2c 20 77 65 20 61 72 65 20 67 75  utine, we are gu
83ad0 61 72 61 6e 74 65 65 64 20 74 68 61 74 20 6e 42  aranteed that nB
83ae0 79 74 65 3e 30 20 62 65 63 61 75 73 65 0a 2a 2a  yte>0 because.**
83af0 20 63 61 73 65 73 20 6f 66 20 6e 42 79 74 65 3c   cases of nByte<
83b00 3d 30 20 77 69 6c 6c 20 62 65 20 69 6e 74 65 72  =0 will be inter
83b10 63 65 70 74 65 64 20 61 6e 64 20 64 65 61 6c 74  cepted and dealt
83b20 20 77 69 74 68 20 62 79 20 68 69 67 68 65 72 20   with by higher 
83b30 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65  level.** routine
83b40 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
83b50 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c  d *sqlite3MemMal
83b60 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a  loc(int nByte){.
83b70 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
83b80 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 42  *p;.  assert( nB
83b90 79 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74 65  yte>0 );.  nByte
83ba0 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29   = ROUND8(nByte)
83bb0 3b 0a 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 20  ;.  p = malloc( 
83bc0 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 28  nByte+8 );.  if(
83bd0 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d   p ){.    p[0] =
83be0 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b   nByte;.    p++;
83bf0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76  .  }.  return (v
83c00 6f 69 64 20 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  oid *)p;.}../*.*
83c10 2a 20 4c 69 6b 65 20 66 72 65 65 28 29 20 62 75  * Like free() bu
83c20 74 20 77 6f 72 6b 73 20 66 6f 72 20 61 6c 6c 6f  t works for allo
83c30 63 61 74 69 6f 6e 73 20 6f 62 74 61 69 6e 65 64  cations obtained
83c40 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 65 6d   from sqlite3Mem
83c50 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 73  Malloc().** or s
83c60 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63  qlite3MemRealloc
83c70 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68  ()..**.** For th
83c80 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75  is low-level rou
83c90 74 69 6e 65 2c 20 77 65 20 61 6c 72 65 61 64 79  tine, we already
83ca0 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69 6f   know that pPrio
83cb0 72 21 3d 30 20 73 69 6e 63 65 0a 2a 2a 20 63 61  r!=0 since.** ca
83cc0 73 65 73 20 77 68 65 72 65 20 70 50 72 69 6f 72  ses where pPrior
83cd0 3d 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62 65  ==0 will have be
83ce0 65 6e 20 69 6e 74 65 63 65 70 74 65 64 20 61 6e  en intecepted an
83cf0 64 20 64 65 61 6c 74 20 77 69 74 68 0a 2a 2a 20  d dealt with.** 
83d00 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20  by higher-level 
83d10 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61  routines..*/.sta
83d20 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
83d30 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50  MemFree(void *pP
83d40 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 74 65 33  rior){.  sqlite3
83d50 5f 69 6e 74 36 34 20 2a 70 20 3d 20 28 73 71 6c  _int64 *p = (sql
83d60 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 69  ite3_int64*)pPri
83d70 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  or;.  assert( pP
83d80 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 70 2d 2d  rior!=0 );.  p--
83d90 3b 0a 20 20 66 72 65 65 28 70 29 3b 0a 7d 0a 0a  ;.  free(p);.}..
83da0 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 72 65 61 6c 6c  /*.** Like reall
83db0 6f 63 28 29 2e 20 20 52 65 73 69 7a 65 20 61 6e  oc().  Resize an
83dc0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76   allocation prev
83dd0 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20  iously obtained 
83de0 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d  from.** sqlite3M
83df0 65 6d 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a  emMalloc()..**.*
83e00 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c  * For this low-l
83e10 65 76 65 6c 20 69 6e 74 65 72 66 61 63 65 2c 20  evel interface, 
83e20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 72  we know that pPr
83e30 69 6f 72 21 3d 30 2e 20 20 43 61 73 65 73 20 77  ior!=0.  Cases w
83e40 68 65 72 65 0a 2a 2a 20 70 50 72 69 6f 72 3d 3d  here.** pPrior==
83e50 30 20 77 68 69 6c 65 20 68 61 76 65 20 62 65 65  0 while have bee
83e60 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20 62 79  n intercepted by
83e70 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f   higher-level ro
83e80 75 74 69 6e 65 20 61 6e 64 0a 2a 2a 20 72 65 64  utine and.** red
83e90 69 72 65 63 74 65 64 20 74 6f 20 78 4d 61 6c 6c  irected to xMall
83ea0 6f 63 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20  oc.  Similarly, 
83eb0 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 6e 42 79  we know that nBy
83ec0 74 65 3e 30 20 62 65 63 61 75 73 65 73 0a 2a 2a  te>0 becauses.**
83ed0 20 63 61 73 65 73 20 77 68 65 72 65 20 6e 42 79   cases where nBy
83ee0 74 65 3c 3d 30 20 77 69 6c 6c 20 68 61 76 65 20  te<=0 will have 
83ef0 62 65 65 6e 20 69 6e 74 65 72 63 65 70 74 65 64  been intercepted
83f00 20 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c   by higher-level
83f10 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61 6e 64  .** routines and
83f20 20 72 65 64 69 72 65 63 74 65 64 20 74 6f 20 78   redirected to x
83f30 46 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Free..*/.static 
83f40 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d  void *sqlite3Mem
83f50 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50  Realloc(void *pP
83f60 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29  rior, int nByte)
83f70 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  {.  sqlite3_int6
83f80 34 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f  4 *p = (sqlite3_
83f90 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20  int64*)pPrior;. 
83fa0 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21   assert( pPrior!
83fb0 3d 30 20 26 26 20 6e 42 79 74 65 3e 30 20 29 3b  =0 && nByte>0 );
83fc0 0a 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44  .  nByte = ROUND
83fd0 38 28 6e 42 79 74 65 29 3b 0a 20 20 70 20 3d 20  8(nByte);.  p = 
83fe0 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29  (sqlite3_int64*)
83ff0 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20  pPrior;.  p--;. 
84000 20 70 20 3d 20 72 65 61 6c 6c 6f 63 28 70 2c 20   p = realloc(p, 
84010 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 28  nByte+8 );.  if(
84020 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d   p ){.    p[0] =
84030 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b   nByte;.    p++;
84040 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76  .  }.  return (v
84050 6f 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  oid*)p;.}../*.**
84060 20 52 65 70 6f 72 74 20 74 68 65 20 61 6c 6c 6f   Report the allo
84070 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 20  cated size of a 
84080 70 72 69 6f 72 20 72 65 74 75 72 6e 20 66 72 6f  prior return fro
84090 6d 20 78 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f  m xMalloc().** o
840a0 72 20 78 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2f  r xRealloc()..*/
840b0 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
840c0 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20  te3MemSize(void 
840d0 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69  *pPrior){.  sqli
840e0 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20  te3_int64 *p;.  
840f0 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 20  if( pPrior==0 ) 
84100 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20  return 0;.  p = 
84110 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29  (sqlite3_int64*)
84120 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20  pPrior;.  p--;. 
84130 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 5b 30   return (int)p[0
84140 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e  ];.}../*.** Roun
84150 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20 73  d up a request s
84160 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ize to the next 
84170 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e  valid allocation
84180 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   size..*/.static
84190 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52   int sqlite3MemR
841a0 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20  oundup(int n){. 
841b0 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e   return ROUND8(n
841c0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  );.}../*.** Init
841d0 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75  ialize this modu
841e0 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
841f0 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74  t sqlite3MemInit
84200 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b  (void *NotUsed){
84210 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
84220 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
84230 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
84240 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69  ;.}../*.** Deini
84250 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64  tialize this mod
84260 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ule..*/.static v
84270 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68  oid sqlite3MemSh
84280 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74  utdown(void *Not
84290 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f  Used){.  UNUSED_
842a0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
842b0 64 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a  d);.  return;.}.
842c0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
842d0 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20  ine is the only 
842e0 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20  routine in this 
842f0 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e  file with extern
84300 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a  al linkage..**.*
84310 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c  * Populate the l
84320 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20  ow-level memory 
84330 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74  allocation funct
84340 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a  ion pointers in.
84350 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ** sqlite3Global
84360 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f  Config.m with po
84370 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f  inters to the ro
84380 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66  utines in this f
84390 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ile..*/.SQLITE_P
843a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
843b0 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74  te3MemSetDefault
843c0 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63  (void){.  static
843d0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
843e0 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75  em_methods defau
843f0 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20  ltMethods = {.  
84400 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c     sqlite3MemMal
84410 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65  loc,.     sqlite
84420 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73  3MemFree,.     s
84430 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63  qlite3MemRealloc
84440 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  ,.     sqlite3Me
84450 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69  mSize,.     sqli
84460 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20  te3MemRoundup,. 
84470 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e      sqlite3MemIn
84480 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  it,.     sqlite3
84490 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20  MemShutdown,.   
844a0 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74    0.  };.  sqlit
844b0 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45  e3_config(SQLITE
844c0 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20  _CONFIG_MALLOC, 
844d0 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29  &defaultMethods)
844e0 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
844f0 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c  QLITE_SYSTEM_MAL
84500 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  LOC */../*******
84510 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d  ******* End of m
84520 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  em1.c **********
84530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84550 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
84560 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
84570 6c 65 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a  le mem2.c ******
84580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
845a0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
845b0 30 37 20 41 75 67 75 73 74 20 31 35 0a 2a 2a 0a  07 August 15.**.
845c0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
845d0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
845e0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
845f0 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
84600 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
84610 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
84620 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
84630 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
84640 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
84650 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
84660 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
84670 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
84680 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
84690 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
846a0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
846b0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
846c0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
846d0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
846e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
846f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
84720 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
84730 61 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d  ains low-level m
84740 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
84750 20 64 72 69 76 65 72 73 20 66 6f 72 20 77 68 65   drivers for whe
84760 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c  n.** SQLite will
84770 20 75 73 65 20 74 68 65 20 73 74 61 6e 64 61 72   use the standar
84780 64 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c  d C-library mall
84790 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20  oc/realloc/free 
847a0 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20  interface.** to 
847b0 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72  obtain the memor
847c0 79 20 69 74 20 6e 65 65 64 73 20 77 68 69 6c 65  y it needs while
847d0 20 61 64 64 69 6e 67 20 6c 6f 74 73 20 6f 66 20   adding lots of 
847e0 61 64 64 69 74 69 6f 6e 61 6c 20 64 65 62 75 67  additional debug
847f0 67 69 6e 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74  ging.** informat
84800 69 6f 6e 20 74 6f 20 65 61 63 68 20 61 6c 6c 6f  ion to each allo
84810 63 61 74 69 6f 6e 20 69 6e 20 6f 72 64 65 72 20  cation in order 
84820 74 6f 20 68 65 6c 70 20 64 65 74 65 63 74 20 61  to help detect a
84830 6e 64 20 66 69 78 20 6d 65 6d 6f 72 79 0a 2a 2a  nd fix memory.**
84840 20 6c 65 61 6b 73 20 61 6e 64 20 6d 65 6d 6f 72   leaks and memor
84850 79 20 75 73 61 67 65 20 65 72 72 6f 72 73 2e 0a  y usage errors..
84860 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
84870 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65  contains impleme
84880 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20  ntations of the 
84890 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79  low-level memory
848a0 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72   allocation.** r
848b0 6f 75 74 69 6e 65 73 20 73 70 65 63 69 66 69 65  outines specifie
848c0 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33  d in the sqlite3
848d0 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a  _mem_methods obj
848e0 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  ect..**.** $Id: 
848f0 6d 65 6d 32 2e 63 2c 76 20 31 2e 34 35 20 32 30  mem2.c,v 1.45 20
84900 30 39 2f 30 33 2f 32 33 20 30 34 3a 33 33 3a 33  09/03/23 04:33:3
84910 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  3 danielk1977 Ex
84920 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  p $.*/../*.** Th
84930 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68  is version of th
84940 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
84950 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20  or is used only 
84960 69 66 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45  if the.** SQLITE
84970 5f 4d 45 4d 44 45 42 55 47 20 6d 61 63 72 6f 20  _MEMDEBUG macro 
84980 69 73 20 64 65 66 69 6e 65 64 0a 2a 2f 0a 23 69  is defined.*/.#i
84990 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 45 4d 44  fdef SQLITE_MEMD
849a0 45 42 55 47 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  EBUG../*.** The 
849b0 62 61 63 6b 74 72 61 63 65 20 66 75 6e 63 74 69  backtrace functi
849c0 6f 6e 61 6c 69 74 79 20 69 73 20 6f 6e 6c 79 20  onality is only 
849d0 61 76 61 69 6c 61 62 6c 65 20 77 69 74 68 20 47  available with G
849e0 4c 49 42 43 0a 2a 2f 0a 23 69 66 64 65 66 20 5f  LIBC.*/.#ifdef _
849f0 5f 47 4c 49 42 43 5f 5f 0a 20 20 65 78 74 65 72  _GLIBC__.  exter
84a00 6e 20 69 6e 74 20 62 61 63 6b 74 72 61 63 65 28  n int backtrace(
84a10 76 6f 69 64 2a 2a 2c 69 6e 74 29 3b 0a 20 20 65  void**,int);.  e
84a20 78 74 65 72 6e 20 76 6f 69 64 20 62 61 63 6b 74  xtern void backt
84a30 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28  race_symbols_fd(
84a40 76 6f 69 64 2a 63 6f 6e 73 74 2a 2c 69 6e 74 2c  void*const*,int,
84a50 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65  int);.#else.# de
84a60 66 69 6e 65 20 62 61 63 6b 74 72 61 63 65 28 41  fine backtrace(A
84a70 2c 42 29 20 31 0a 23 20 64 65 66 69 6e 65 20 62  ,B) 1.# define b
84a80 61 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73  acktrace_symbols
84a90 5f 66 64 28 41 2c 42 2c 43 29 0a 23 65 6e 64 69  _fd(A,B,C).#endi
84aa0 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6d 65  f../*.** Each me
84ab0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
84ac0 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a  looks like this:
84ad0 0a 2a 2a 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d  .**.**  --------
84ae0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84af0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84b00 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84b10 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84b20 0a 2a 2a 20 20 7c 20 54 69 74 6c 65 20 7c 20 20  .**  | Title |  
84b30 62 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65  backtrace pointe
84b40 72 73 20 7c 20 20 4d 65 6d 42 6c 6f 63 6b 48 64  rs |  MemBlockHd
84b50 72 20 7c 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  r |  allocation 
84b60 7c 20 20 45 6e 64 47 75 61 72 64 20 7c 0a 2a 2a  |  EndGuard |.**
84b70 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
84b80 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84b90 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84ba0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84bb0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a  ----------.**.**
84bc0 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   The application
84bd0 20 63 6f 64 65 20 73 65 65 73 20 6f 6e 6c 79 20   code sees only 
84be0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
84bf0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 57 65   allocation.  We
84c00 20 68 61 76 65 0a 2a 2a 20 74 6f 20 62 61 63 6b   have.** to back
84c10 20 75 70 20 66 72 6f 6d 20 74 68 65 20 61 6c 6c   up from the all
84c20 6f 63 61 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20  ocation pointer 
84c30 74 6f 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42  to find the MemB
84c40 6c 6f 63 6b 48 64 72 2e 20 20 54 68 65 0a 2a 2a  lockHdr.  The.**
84c50 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 74 65 6c   MemBlockHdr tel
84c60 6c 73 20 75 73 20 74 68 65 20 73 69 7a 65 20 6f  ls us the size o
84c70 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  f the allocation
84c80 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20   and the number 
84c90 6f 66 0a 2a 2a 20 62 61 63 6b 74 72 61 63 65 20  of.** backtrace 
84ca0 70 6f 69 6e 74 65 72 73 2e 20 20 54 68 65 72 65  pointers.  There
84cb0 20 69 73 20 61 6c 73 6f 20 61 20 67 75 61 72 64   is also a guard
84cc0 20 77 6f 72 64 20 61 74 20 74 68 65 20 65 6e 64   word at the end
84cd0 20 6f 66 20 74 68 65 0a 2a 2a 20 4d 65 6d 42 6c   of the.** MemBl
84ce0 6f 63 6b 48 64 72 2e 0a 2a 2f 0a 73 74 72 75 63  ockHdr..*/.struc
84cf0 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 7b 0a  t MemBlockHdr {.
84d00 20 20 69 36 34 20 69 53 69 7a 65 3b 20 20 20 20    i64 iSize;    
84d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
84d20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
84d30 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e   this allocation
84d40 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 6d   */.  struct Mem
84d50 42 6c 6f 63 6b 48 64 72 20 2a 70 4e 65 78 74 2c  BlockHdr *pNext,
84d60 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 4c 69 6e   *pPrev;  /* Lin
84d70 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  ked list of all 
84d80 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 2a  unfreed memory *
84d90 2f 0a 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72  /.  char nBacktr
84da0 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ace;            
84db0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
84dc0 72 20 6f 66 20 62 61 63 6b 74 72 61 63 65 73 20  r of backtraces 
84dd0 6f 6e 20 74 68 69 73 20 61 6c 6c 6f 63 20 2a 2f  on this alloc */
84de0 0a 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72 61  .  char nBacktra
84df0 63 65 53 6c 6f 74 73 3b 20 20 20 20 20 20 20 20  ceSlots;        
84e00 20 20 20 20 20 20 20 2f 2a 20 41 76 61 69 6c 61         /* Availa
84e10 62 6c 65 20 62 61 63 6b 74 72 61 63 65 20 73 6c  ble backtrace sl
84e20 6f 74 73 20 2a 2f 0a 20 20 73 68 6f 72 74 20 6e  ots */.  short n
84e30 54 69 74 6c 65 3b 20 20 20 20 20 20 20 20 20 20  Title;          
84e40 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
84e50 42 79 74 65 73 20 6f 66 20 74 69 74 6c 65 3b 20  Bytes of title; 
84e60 69 6e 63 6c 75 64 65 73 20 27 5c 30 27 20 2a 2f  includes '\0' */
84e70 0a 20 20 69 6e 74 20 69 46 6f 72 65 47 75 61 72  .  int iForeGuar
84e80 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
84e90 20 20 20 20 20 20 20 2f 2a 20 47 75 61 72 64 20         /* Guard 
84ea0 77 6f 72 64 20 66 6f 72 20 73 61 6e 69 74 79 20  word for sanity 
84eb0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 75 61  */.};../*.** Gua
84ec0 72 64 20 77 6f 72 64 73 0a 2a 2f 0a 23 64 65 66  rd words.*/.#def
84ed0 69 6e 65 20 46 4f 52 45 47 55 41 52 44 20 30 78  ine FOREGUARD 0x
84ee0 38 30 46 35 45 31 35 33 0a 23 64 65 66 69 6e 65  80F5E153.#define
84ef0 20 52 45 41 52 47 55 41 52 44 20 30 78 45 34 36   REARGUARD 0xE46
84f00 37 36 42 35 33 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d  76B53../*.** Num
84f10 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 73 69  ber of malloc si
84f20 7a 65 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 6f  ze increments to
84f30 20 74 72 61 63 6b 2e 0a 2a 2f 0a 23 64 65 66 69   track..*/.#defi
84f40 6e 65 20 4e 43 53 49 5a 45 20 20 31 30 30 30 0a  ne NCSIZE  1000.
84f50 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68  ./*.** All of th
84f60 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c  e static variabl
84f70 65 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20  es used by this 
84f80 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65  module are colle
84f90 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73  cted.** into a s
84fa0 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 65 20  ingle structure 
84fb0 6e 61 6d 65 64 20 22 6d 65 6d 22 2e 20 20 54 68  named "mem".  Th
84fc0 69 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68  is is to keep th
84fd0 65 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69  e.** static vari
84fe0 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20  ables organized 
84ff0 61 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61  and to reduce na
85000 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f  mespace pollutio
85010 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d  n.** when this m
85020 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65  odule is combine
85030 64 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20  d with other in 
85040 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e  the amalgamation
85050 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75  ..*/.static stru
85060 63 74 20 7b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a  ct {.  .  /*.  *
85070 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72  * Mutex to contr
85080 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65  ol access to the
85090 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
850a0 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20  on subsystem..  
850b0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  */.  sqlite3_mut
850c0 65 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a  ex *mutex;..  /*
850d0 0a 20 20 2a 2a 20 48 65 61 64 20 61 6e 64 20 74  .  ** Head and t
850e0 61 69 6c 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20  ail of a linked 
850f0 6c 69 73 74 20 6f 66 20 61 6c 6c 20 6f 75 74 73  list of all outs
85100 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69  tanding allocati
85110 6f 6e 73 0a 20 20 2a 2f 0a 20 20 73 74 72 75 63  ons.  */.  struc
85120 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70  t MemBlockHdr *p
85130 46 69 72 73 74 3b 0a 20 20 73 74 72 75 63 74 20  First;.  struct 
85140 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4c 61  MemBlockHdr *pLa
85150 73 74 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a  st;.  .  /*.  **
85160 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c   The number of l
85170 65 76 65 6c 73 20 6f 66 20 62 61 63 6b 74 72 61  evels of backtra
85180 63 65 20 74 6f 20 73 61 76 65 20 69 6e 20 6e 65  ce to save in ne
85190 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20  w allocations.. 
851a0 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b 74   */.  int nBackt
851b0 72 61 63 65 3b 0a 20 20 76 6f 69 64 20 28 2a 78  race;.  void (*x
851c0 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20  Backtrace)(int, 
851d0 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 3b 0a 0a  int, void **);..
851e0 20 20 2f 2a 0a 20 20 2a 2a 20 54 69 74 6c 65 20    /*.  ** Title 
851f0 74 65 78 74 20 74 6f 20 69 6e 73 65 72 74 20 69  text to insert i
85200 6e 20 66 72 6f 6e 74 20 6f 66 20 65 61 63 68 20  n front of each 
85210 62 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 6e 74  block.  */.  int
85220 20 6e 54 69 74 6c 65 3b 20 20 20 20 20 20 20 20   nTitle;        
85230 2f 2a 20 42 79 74 65 73 20 6f 66 20 7a 54 69 74  /* Bytes of zTit
85240 6c 65 20 74 6f 20 73 61 76 65 2e 20 20 49 6e 63  le to save.  Inc
85250 6c 75 64 65 73 20 27 5c 30 27 20 61 6e 64 20 70  ludes '\0' and p
85260 61 64 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72  adding */.  char
85270 20 7a 54 69 74 6c 65 5b 31 30 30 5d 3b 20 20 2f   zTitle[100];  /
85280 2a 20 54 68 65 20 74 69 74 6c 65 20 74 65 78 74  * The title text
85290 20 2a 2f 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20   */..  /* .  ** 
852a0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 44 69 73  sqlite3MallocDis
852b0 61 6c 6c 6f 77 28 29 20 69 6e 63 72 65 6d 65 6e  allow() incremen
852c0 74 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ts the following
852d0 20 63 6f 75 6e 74 65 72 2e 0a 20 20 2a 2a 20 73   counter..  ** s
852e0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f  qlite3MallocAllo
852f0 77 28 29 20 64 65 63 72 65 6d 65 6e 74 73 20 69  w() decrements i
85300 74 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 64 69  t..  */.  int di
85310 73 61 6c 6c 6f 77 3b 20 2f 2a 20 44 6f 20 6e 6f  sallow; /* Do no
85320 74 20 61 6c 6c 6f 77 20 6d 65 6d 6f 72 79 20 61  t allow memory a
85330 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20  llocation */..  
85340 2f 2a 0a 20 20 2a 2a 20 47 61 74 68 65 72 20 73  /*.  ** Gather s
85350 74 61 74 69 73 74 69 63 73 20 6f 6e 20 74 68 65  tatistics on the
85360 20 73 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79   sizes of memory
85370 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20   allocations..  
85380 2a 2a 20 6e 41 6c 6c 6f 63 5b 69 5d 20 69 73 20  ** nAlloc[i] is 
85390 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c  the number of al
853a0 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74  location attempt
853b0 73 20 6f 66 20 69 2a 38 0a 20 20 2a 2a 20 62 79  s of i*8.  ** by
853c0 74 65 73 2e 20 20 69 3d 3d 4e 43 53 49 5a 45 20  tes.  i==NCSIZE 
853d0 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
853e0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65   allocation atte
853f0 6d 70 74 73 20 66 6f 72 0a 20 20 2a 2a 20 73 69  mpts for.  ** si
85400 7a 65 73 20 6d 6f 72 65 20 74 68 61 6e 20 4e 43  zes more than NC
85410 53 49 5a 45 2a 38 20 62 79 74 65 73 2e 0a 20 20  SIZE*8 bytes..  
85420 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 5b  */.  int nAlloc[
85430 4e 43 53 49 5a 45 5d 3b 20 20 20 20 20 20 2f 2a  NCSIZE];      /*
85440 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
85450 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a   allocations */.
85460 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 5b 4e    int nCurrent[N
85470 43 53 49 5a 45 5d 3b 20 20 20 20 2f 2a 20 43 75  CSIZE];    /* Cu
85480 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20  rrent number of 
85490 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20  allocations */. 
854a0 20 69 6e 74 20 6d 78 43 75 72 72 65 6e 74 5b 4e   int mxCurrent[N
854b0 43 53 49 5a 45 5d 3b 20 20 20 2f 2a 20 48 69 67  CSIZE];   /* Hig
854c0 68 77 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 20  hwater mark for 
854d0 6e 43 75 72 72 65 6e 74 20 2a 2f 0a 0a 7d 20 6d  nCurrent */..} m
854e0 65 6d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75  em;.../*.** Adju
854f0 73 74 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20  st memory usage 
85500 73 74 61 74 69 73 74 69 63 73 0a 2a 2f 0a 73 74  statistics.*/.st
85510 61 74 69 63 20 76 6f 69 64 20 61 64 6a 75 73 74  atic void adjust
85520 53 74 61 74 73 28 69 6e 74 20 69 53 69 7a 65 2c  Stats(int iSize,
85530 20 69 6e 74 20 69 6e 63 72 65 6d 65 6e 74 29 7b   int increment){
85540 0a 20 20 69 6e 74 20 69 20 3d 20 52 4f 55 4e 44  .  int i = ROUND
85550 38 28 69 53 69 7a 65 29 2f 38 3b 0a 20 20 69 66  8(iSize)/8;.  if
85560 28 20 69 3e 4e 43 53 49 5a 45 2d 31 20 29 7b 0a  ( i>NCSIZE-1 ){.
85570 20 20 20 20 69 20 3d 20 4e 43 53 49 5a 45 20 2d      i = NCSIZE -
85580 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 6e   1;.  }.  if( in
85590 63 72 65 6d 65 6e 74 3e 30 20 29 7b 0a 20 20 20  crement>0 ){.   
855a0 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2b 2b   mem.nAlloc[i]++
855b0 3b 0a 20 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65  ;.    mem.nCurre
855c0 6e 74 5b 69 5d 2b 2b 3b 0a 20 20 20 20 69 66 28  nt[i]++;.    if(
855d0 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d   mem.nCurrent[i]
855e0 3e 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69  >mem.mxCurrent[i
855f0 5d 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 6d  ] ){.      mem.m
85600 78 43 75 72 72 65 6e 74 5b 69 5d 20 3d 20 6d 65  xCurrent[i] = me
85610 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3b 0a 20  m.nCurrent[i];. 
85620 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
85630 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69    mem.nCurrent[i
85640 5d 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ]--;.    assert(
85650 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d   mem.nCurrent[i]
85660 3e 3d 30 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  >=0 );.  }.}../*
85670 0a 2a 2a 20 47 69 76 65 6e 20 61 6e 20 61 6c 6c  .** Given an all
85680 6f 63 61 74 69 6f 6e 2c 20 66 69 6e 64 20 74 68  ocation, find th
85690 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 66 6f  e MemBlockHdr fo
856a0 72 20 74 68 61 74 20 61 6c 6c 6f 63 61 74 69 6f  r that allocatio
856b0 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  n..**.** This ro
856c0 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68 65  utine checks the
856d0 20 67 75 61 72 64 73 20 61 74 20 65 69 74 68 65   guards at eithe
856e0 72 20 65 6e 64 20 6f 66 20 74 68 65 20 61 6c 6c  r end of the all
856f0 6f 63 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 69  ocation and.** i
85700 66 20 74 68 65 79 20 61 72 65 20 69 6e 63 6f 72  f they are incor
85710 72 65 63 74 20 69 74 20 61 73 73 65 72 74 73 2e  rect it asserts.
85720 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63  .*/.static struc
85730 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 73  t MemBlockHdr *s
85740 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48  qlite3MemsysGetH
85750 65 61 64 65 72 28 76 6f 69 64 20 2a 70 41 6c 6c  eader(void *pAll
85760 6f 63 61 74 69 6f 6e 29 7b 0a 20 20 73 74 72 75  ocation){.  stru
85770 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
85780 70 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74 3b 0a  p;.  int *pInt;.
85790 20 20 75 38 20 2a 70 55 38 3b 0a 20 20 69 6e 74    u8 *pU8;.  int
857a0 20 6e 52 65 73 65 72 76 65 3b 0a 0a 20 20 70 20   nReserve;..  p 
857b0 3d 20 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  = (struct MemBlo
857c0 63 6b 48 64 72 2a 29 70 41 6c 6c 6f 63 61 74 69  ckHdr*)pAllocati
857d0 6f 6e 3b 0a 20 20 70 2d 2d 3b 0a 20 20 61 73 73  on;.  p--;.  ass
857e0 65 72 74 28 20 70 2d 3e 69 46 6f 72 65 47 75 61  ert( p->iForeGua
857f0 72 64 3d 3d 28 69 6e 74 29 46 4f 52 45 47 55 41  rd==(int)FOREGUA
85800 52 44 20 29 3b 0a 20 20 6e 52 65 73 65 72 76 65  RD );.  nReserve
85810 20 3d 20 52 4f 55 4e 44 38 28 70 2d 3e 69 53 69   = ROUND8(p->iSi
85820 7a 65 29 3b 0a 20 20 70 49 6e 74 20 3d 20 28 69  ze);.  pInt = (i
85830 6e 74 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b  nt*)pAllocation;
85840 0a 20 20 70 55 38 20 3d 20 28 75 38 2a 29 70 41  .  pU8 = (u8*)pA
85850 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 61 73 73  llocation;.  ass
85860 65 72 74 28 20 70 49 6e 74 5b 6e 52 65 73 65 72  ert( pInt[nReser
85870 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 3d  ve/sizeof(int)]=
85880 3d 28 69 6e 74 29 52 45 41 52 47 55 41 52 44 20  =(int)REARGUARD 
85890 29 3b 0a 20 20 2f 2a 20 54 68 69 73 20 63 68 65  );.  /* This che
858a0 63 6b 73 20 61 6e 79 20 6f 66 20 74 68 65 20 22  cks any of the "
858b0 65 78 74 72 61 22 20 62 79 74 65 73 20 61 6c 6c  extra" bytes all
858c0 6f 63 61 74 65 64 20 64 75 65 0a 20 20 2a 2a 20  ocated due.  ** 
858d0 74 6f 20 72 6f 75 6e 64 69 6e 67 20 75 70 20 74  to rounding up t
858e0 6f 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e  o an 8 byte boun
858f0 64 61 72 79 20 74 6f 20 65 6e 73 75 72 65 20 0a  dary to ensure .
85900 20 20 2a 2a 20 74 68 65 79 20 68 61 76 65 6e 27    ** they haven'
85910 74 20 62 65 65 6e 20 6f 76 65 72 77 72 69 74 74  t been overwritt
85920 65 6e 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65  en..  */.  while
85930 28 20 6e 52 65 73 65 72 76 65 2d 2d 20 3e 20 70  ( nReserve-- > p
85940 2d 3e 69 53 69 7a 65 20 29 20 61 73 73 65 72 74  ->iSize ) assert
85950 28 20 70 55 38 5b 6e 52 65 73 65 72 76 65 5d 3d  ( pU8[nReserve]=
85960 3d 30 78 36 35 20 29 3b 0a 20 20 72 65 74 75 72  =0x65 );.  retur
85970 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  n p;.}../*.** Re
85980 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
85990 6f 66 20 62 79 74 65 73 20 63 75 72 72 65 6e 74  of bytes current
859a0 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20  ly allocated at 
859b0 61 64 64 72 65 73 73 20 70 2e 0a 2a 2f 0a 73 74  address p..*/.st
859c0 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
859d0 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 29  MemSize(void *p)
859e0 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c  {.  struct MemBl
859f0 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20  ockHdr *pHdr;.  
85a00 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65  if( !p ){.    re
85a10 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 48  turn 0;.  }.  pH
85a20 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73  dr = sqlite3Mems
85a30 79 73 47 65 74 48 65 61 64 65 72 28 70 29 3b 0a  ysGetHeader(p);.
85a40 20 20 72 65 74 75 72 6e 20 70 48 64 72 2d 3e 69    return pHdr->i
85a50 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  Size;.}../*.** I
85a60 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65  nitialize the me
85a70 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
85a80 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74  subsystem..*/.st
85a90 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
85aa0 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f  MemInit(void *No
85ab0 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44  tUsed){.  UNUSED
85ac0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
85ad0 65 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  ed);.  assert( (
85ae0 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 4d 65  sizeof(struct Me
85af0 6d 42 6c 6f 63 6b 48 64 72 29 26 37 29 20 3d 3d  mBlockHdr)&7) ==
85b00 20 30 20 29 3b 0a 20 20 69 66 28 20 21 73 71 6c   0 );.  if( !sql
85b10 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
85b20 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20  .bMemstat ){.   
85b30 20 2f 2a 20 49 66 20 6d 65 6d 6f 72 79 20 73 74   /* If memory st
85b40 61 74 75 73 20 69 73 20 65 6e 61 62 6c 65 64 2c  atus is enabled,
85b50 20 74 68 65 6e 20 74 68 65 20 6d 61 6c 6c 6f 63   then the malloc
85b60 2e 63 20 77 72 61 70 70 65 72 20 77 69 6c 6c 20  .c wrapper will 
85b70 61 6c 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 68  already.    ** h
85b80 6f 6c 64 20 74 68 65 20 53 54 41 54 49 43 5f 4d  old the STATIC_M
85b90 45 4d 20 6d 75 74 65 78 20 77 68 65 6e 20 74 68  EM mutex when th
85ba0 65 20 72 6f 75 74 69 6e 65 73 20 68 65 72 65 20  e routines here 
85bb0 61 72 65 20 69 6e 76 6f 6b 65 64 2e 20 2a 2f 0a  are invoked. */.
85bc0 20 20 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20      mem.mutex = 
85bd0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
85be0 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
85bf0 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a  TATIC_MEM);.  }.
85c00 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
85c10 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69  OK;.}../*.** Dei
85c20 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65  nitialize the me
85c30 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
85c40 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74  subsystem..*/.st
85c50 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65  atic void sqlite
85c60 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69  3MemShutdown(voi
85c70 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55  d *NotUsed){.  U
85c80 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
85c90 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 2e  NotUsed);.  mem.
85ca0 6d 75 74 65 78 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  mutex = 0;.}../*
85cb0 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72  .** Round up a r
85cc0 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74  equest size to t
85cd0 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c  he next valid al
85ce0 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a  location size..*
85cf0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
85d00 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69  ite3MemRoundup(i
85d10 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20  nt n){.  return 
85d20 52 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f 2a  ROUND8(n);.}../*
85d30 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79  .** Allocate nBy
85d40 74 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f  te bytes of memo
85d50 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ry..*/.static vo
85d60 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61  id *sqlite3MemMa
85d70 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b  lloc(int nByte){
85d80 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  .  struct MemBlo
85d90 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 76  ckHdr *pHdr;.  v
85da0 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68 61  oid **pBt;.  cha
85db0 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 2a 70 49 6e  r *z;.  int *pIn
85dc0 74 3b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 30  t;.  void *p = 0
85dd0 3b 0a 20 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a  ;.  int totalSiz
85de0 65 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 76  e;.  int nReserv
85df0 65 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  e;.  sqlite3_mut
85e00 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74  ex_enter(mem.mut
85e10 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d  ex);.  assert( m
85e20 65 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 20 29  em.disallow==0 )
85e30 3b 0a 20 20 6e 52 65 73 65 72 76 65 20 3d 20 52  ;.  nReserve = R
85e40 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 20  OUND8(nByte);.  
85e50 74 6f 74 61 6c 53 69 7a 65 20 3d 20 6e 52 65 73  totalSize = nRes
85e60 65 72 76 65 20 2b 20 73 69 7a 65 6f 66 28 2a 70  erve + sizeof(*p
85e70 48 64 72 29 20 2b 20 73 69 7a 65 6f 66 28 69 6e  Hdr) + sizeof(in
85e80 74 29 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20  t) +.           
85e90 20 20 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61      mem.nBacktra
85ea0 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29  ce*sizeof(void*)
85eb0 20 2b 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20   + mem.nTitle;. 
85ec0 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 74 6f 74 61   p = malloc(tota
85ed0 6c 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 20  lSize);.  if( p 
85ee0 29 7b 0a 20 20 20 20 7a 20 3d 20 70 3b 0a 20 20  ){.    z = p;.  
85ef0 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29    pBt = (void**)
85f00 26 7a 5b 6d 65 6d 2e 6e 54 69 74 6c 65 5d 3b 0a  &z[mem.nTitle];.
85f10 20 20 20 20 70 48 64 72 20 3d 20 28 73 74 72 75      pHdr = (stru
85f20 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29  ct MemBlockHdr*)
85f30 26 70 42 74 5b 6d 65 6d 2e 6e 42 61 63 6b 74 72  &pBt[mem.nBacktr
85f40 61 63 65 5d 3b 0a 20 20 20 20 70 48 64 72 2d 3e  ace];.    pHdr->
85f50 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70  pNext = 0;.    p
85f60 48 64 72 2d 3e 70 50 72 65 76 20 3d 20 6d 65 6d  Hdr->pPrev = mem
85f70 2e 70 4c 61 73 74 3b 0a 20 20 20 20 69 66 28 20  .pLast;.    if( 
85f80 6d 65 6d 2e 70 4c 61 73 74 20 29 7b 0a 20 20 20  mem.pLast ){.   
85f90 20 20 20 6d 65 6d 2e 70 4c 61 73 74 2d 3e 70 4e     mem.pLast->pN
85fa0 65 78 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20  ext = pHdr;.    
85fb0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d  }else{.      mem
85fc0 2e 70 46 69 72 73 74 20 3d 20 70 48 64 72 3b 0a  .pFirst = pHdr;.
85fd0 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 2e 70 4c      }.    mem.pL
85fe0 61 73 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20  ast = pHdr;.    
85ff0 70 48 64 72 2d 3e 69 46 6f 72 65 47 75 61 72 64  pHdr->iForeGuard
86000 20 3d 20 46 4f 52 45 47 55 41 52 44 3b 0a 20 20   = FOREGUARD;.  
86010 20 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61    pHdr->nBacktra
86020 63 65 53 6c 6f 74 73 20 3d 20 6d 65 6d 2e 6e 42  ceSlots = mem.nB
86030 61 63 6b 74 72 61 63 65 3b 0a 20 20 20 20 70 48  acktrace;.    pH
86040 64 72 2d 3e 6e 54 69 74 6c 65 20 3d 20 6d 65 6d  dr->nTitle = mem
86050 2e 6e 54 69 74 6c 65 3b 0a 20 20 20 20 69 66 28  .nTitle;.    if(
86060 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 20   mem.nBacktrace 
86070 29 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 61  ){.      void *a
86080 41 64 64 72 5b 34 30 5d 3b 0a 20 20 20 20 20 20  Addr[40];.      
86090 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
860a0 20 3d 20 62 61 63 6b 74 72 61 63 65 28 61 41 64   = backtrace(aAd
860b0 64 72 2c 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61  dr, mem.nBacktra
860c0 63 65 2b 31 29 2d 31 3b 0a 20 20 20 20 20 20 6d  ce+1)-1;.      m
860d0 65 6d 63 70 79 28 70 42 74 2c 20 26 61 41 64 64  emcpy(pBt, &aAdd
860e0 72 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e 42 61 63  r[1], pHdr->nBac
860f0 6b 74 72 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f  ktrace*sizeof(vo
86100 69 64 2a 29 29 3b 0a 20 20 20 20 20 20 61 73 73  id*));.      ass
86110 65 72 74 28 70 42 74 5b 30 5d 29 3b 0a 20 20 20  ert(pBt[0]);.   
86120 20 20 20 69 66 28 20 6d 65 6d 2e 78 42 61 63 6b     if( mem.xBack
86130 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20  trace ){.       
86140 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28   mem.xBacktrace(
86150 6e 42 79 74 65 2c 20 70 48 64 72 2d 3e 6e 42 61  nByte, pHdr->nBa
86160 63 6b 74 72 61 63 65 2d 31 2c 20 26 61 41 64 64  cktrace-1, &aAdd
86170 72 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20  r[1]);.      }. 
86180 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
86190 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
861a0 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
861b0 69 66 28 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29  if( mem.nTitle )
861c0 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a  {.      memcpy(z
861d0 2c 20 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65  , mem.zTitle, me
861e0 6d 2e 6e 54 69 74 6c 65 29 3b 0a 20 20 20 20 7d  m.nTitle);.    }
861f0 0a 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65  .    pHdr->iSize
86200 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 64   = nByte;.    ad
86210 6a 75 73 74 53 74 61 74 73 28 6e 42 79 74 65 2c  justStats(nByte,
86220 20 2b 31 29 3b 0a 20 20 20 20 70 49 6e 74 20 3d   +1);.    pInt =
86230 20 28 69 6e 74 2a 29 26 70 48 64 72 5b 31 5d 3b   (int*)&pHdr[1];
86240 0a 20 20 20 20 70 49 6e 74 5b 6e 52 65 73 65 72  .    pInt[nReser
86250 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 20  ve/sizeof(int)] 
86260 3d 20 52 45 41 52 47 55 41 52 44 3b 0a 20 20 20  = REARGUARD;.   
86270 20 6d 65 6d 73 65 74 28 70 49 6e 74 2c 20 30 78   memset(pInt, 0x
86280 36 35 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20  65, nReserve);. 
86290 20 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 70 49     p = (void*)pI
862a0 6e 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  nt;.  }.  sqlite
862b0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
862c0 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  m.mutex);.  retu
862d0 72 6e 20 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn p; .}../*.** 
862e0 46 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a  Free memory..*/.
862f0 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
86300 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20  te3MemFree(void 
86310 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 74 72 75  *pPrior){.  stru
86320 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
86330 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70  pHdr;.  void **p
86340 42 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20  Bt;.  char *z;. 
86350 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
86360 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
86370 6d 73 74 61 74 20 7c 7c 20 6d 65 6d 2e 6d 75 74  mstat || mem.mut
86380 65 78 21 3d 30 20 29 3b 0a 20 20 70 48 64 72 20  ex!=0 );.  pHdr 
86390 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47  = sqlite3MemsysG
863a0 65 74 48 65 61 64 65 72 28 70 50 72 69 6f 72 29  etHeader(pPrior)
863b0 3b 0a 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a  ;.  pBt = (void*
863c0 2a 29 70 48 64 72 3b 0a 20 20 70 42 74 20 2d 3d  *)pHdr;.  pBt -=
863d0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63   pHdr->nBacktrac
863e0 65 53 6c 6f 74 73 3b 0a 20 20 73 71 6c 69 74 65  eSlots;.  sqlite
863f0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65  3_mutex_enter(me
86400 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  m.mutex);.  if( 
86410 70 48 64 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20  pHdr->pPrev ){. 
86420 20 20 20 61 73 73 65 72 74 28 20 70 48 64 72 2d     assert( pHdr-
86430 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70  >pPrev->pNext==p
86440 48 64 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d  Hdr );.    pHdr-
86450 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20  >pPrev->pNext = 
86460 70 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  pHdr->pNext;.  }
86470 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
86480 28 20 6d 65 6d 2e 70 46 69 72 73 74 3d 3d 70 48  ( mem.pFirst==pH
86490 64 72 20 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 46  dr );.    mem.pF
864a0 69 72 73 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65  irst = pHdr->pNe
864b0 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48  xt;.  }.  if( pH
864c0 64 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20  dr->pNext ){.   
864d0 20 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70   assert( pHdr->p
864e0 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 48 64  Next->pPrev==pHd
864f0 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70  r );.    pHdr->p
86500 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 48  Next->pPrev = pH
86510 64 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 65 6c  dr->pPrev;.  }el
86520 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
86530 6d 65 6d 2e 70 4c 61 73 74 3d 3d 70 48 64 72 20  mem.pLast==pHdr 
86540 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74  );.    mem.pLast
86550 20 3d 20 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a   = pHdr->pPrev;.
86560 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a    }.  z = (char*
86570 29 70 42 74 3b 0a 20 20 7a 20 2d 3d 20 70 48 64  )pBt;.  z -= pHd
86580 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 61 64 6a  r->nTitle;.  adj
86590 75 73 74 53 74 61 74 73 28 70 48 64 72 2d 3e 69  ustStats(pHdr->i
865a0 53 69 7a 65 2c 20 2d 31 29 3b 0a 20 20 6d 65 6d  Size, -1);.  mem
865b0 73 65 74 28 7a 2c 20 30 78 32 62 2c 20 73 69 7a  set(z, 0x2b, siz
865c0 65 6f 66 28 76 6f 69 64 2a 29 2a 70 48 64 72 2d  eof(void*)*pHdr-
865d0 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73  >nBacktraceSlots
865e0 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48 64 72 29   + sizeof(*pHdr)
865f0 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   +.             
86600 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65       pHdr->iSize
86610 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b   + sizeof(int) +
86620 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 29 3b 0a   pHdr->nTitle);.
86630 20 20 66 72 65 65 28 7a 29 3b 0a 20 20 73 71 6c    free(z);.  sql
86640 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
86650 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 7d  (mem.mutex);  .}
86660 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
86670 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78  he size of an ex
86680 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c  isting memory al
86690 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  location..**.** 
866a0 46 6f 72 20 74 68 69 73 20 64 65 62 75 67 67 69  For this debuggi
866b0 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ng implementatio
866c0 6e 2c 20 77 65 20 2a 61 6c 77 61 79 73 2a 20 6d  n, we *always* m
866d0 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ake a copy of th
866e0 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  e.** allocation 
866f0 69 6e 74 6f 20 61 20 6e 65 77 20 70 6c 61 63 65  into a new place
86700 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 49 6e 20   in memory.  In 
86710 74 68 69 73 20 77 61 79 2c 20 69 66 20 74 68 65  this way, if the
86720 20 0a 2a 2a 20 68 69 67 68 65 72 20 6c 65 76 65   .** higher leve
86730 6c 20 63 6f 64 65 20 69 73 20 75 73 69 6e 67 20  l code is using 
86740 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f  pointer to the o
86750 6c 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69  ld allocation, i
86760 74 20 69 73 20 0a 2a 2a 20 6d 75 63 68 20 6d 6f  t is .** much mo
86770 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 72 65  re likely to bre
86780 61 6b 20 61 6e 64 20 77 65 20 61 72 65 20 6d 75  ak and we are mu
86790 63 68 20 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 74  ch more liking t
867a0 6f 20 66 69 6e 64 0a 2a 2a 20 74 68 65 20 65 72  o find.** the er
867b0 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ror..*/.static v
867c0 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52  oid *sqlite3MemR
867d0 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72  ealloc(void *pPr
867e0 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b  ior, int nByte){
867f0 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  .  struct MemBlo
86800 63 6b 48 64 72 20 2a 70 4f 6c 64 48 64 72 3b 0a  ckHdr *pOldHdr;.
86810 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20    void *pNew;.  
86820 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61  assert( mem.disa
86830 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 70 4f 6c  llow==0 );.  pOl
86840 64 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65  dHdr = sqlite3Me
86850 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 70 50  msysGetHeader(pP
86860 72 69 6f 72 29 3b 0a 20 20 70 4e 65 77 20 3d 20  rior);.  pNew = 
86870 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63  sqlite3MemMalloc
86880 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 70  (nByte);.  if( p
86890 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70  New ){.    memcp
868a0 79 28 70 4e 65 77 2c 20 70 50 72 69 6f 72 2c 20  y(pNew, pPrior, 
868b0 6e 42 79 74 65 3c 70 4f 6c 64 48 64 72 2d 3e 69  nByte<pOldHdr->i
868c0 53 69 7a 65 20 3f 20 6e 42 79 74 65 20 3a 20 70  Size ? nByte : p
868d0 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a  OldHdr->iSize);.
868e0 20 20 20 20 69 66 28 20 6e 42 79 74 65 3e 70 4f      if( nByte>pO
868f0 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 29 7b 0a  ldHdr->iSize ){.
86900 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28        memset(&((
86910 63 68 61 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64  char*)pNew)[pOld
86920 48 64 72 2d 3e 69 53 69 7a 65 5d 2c 20 30 78 32  Hdr->iSize], 0x2
86930 62 2c 20 6e 42 79 74 65 20 2d 20 70 4f 6c 64 48  b, nByte - pOldH
86940 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20  dr->iSize);.    
86950 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  }.    sqlite3Mem
86960 46 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20  Free(pPrior);.  
86970 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b  }.  return pNew;
86980 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c 61  .}../*.** Popula
86990 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c  te the low-level
869a0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
869b0 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e  on function poin
869c0 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74  ters in.** sqlit
869d0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
869e0 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74   with pointers t
869f0 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69  o the routines i
86a00 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a  n this file..*/.
86a10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
86a20 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65  oid sqlite3MemSe
86a30 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a  tDefault(void){.
86a40 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
86a50 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
86a60 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64  ds defaultMethod
86a70 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 74  s = {.     sqlit
86a80 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20  e3MemMalloc,.   
86a90 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65    sqlite3MemFree
86aa0 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  ,.     sqlite3Me
86ab0 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73  mRealloc,.     s
86ac0 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20  qlite3MemSize,. 
86ad0 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f      sqlite3MemRo
86ae0 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c 69  undup,.     sqli
86af0 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20  te3MemInit,.    
86b00 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64   sqlite3MemShutd
86b10 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b  own,.     0.  };
86b20 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  .  sqlite3_confi
86b30 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  g(SQLITE_CONFIG_
86b40 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74  MALLOC, &default
86b50 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 2f 2a 0a  Methods);.}../*.
86b60 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65  ** Set the numbe
86b70 72 20 6f 66 20 62 61 63 6b 74 72 61 63 65 20 6c  r of backtrace l
86b80 65 76 65 6c 73 20 6b 65 70 74 20 66 6f 72 20 65  evels kept for e
86b90 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a  ach allocation..
86ba0 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 7a 65  ** A value of ze
86bb0 72 6f 20 74 75 72 6e 73 20 6f 66 66 20 62 61 63  ro turns off bac
86bc0 6b 74 72 61 63 69 6e 67 2e 20 20 54 68 65 20 6e  ktracing.  The n
86bd0 75 6d 62 65 72 20 69 73 20 61 6c 77 61 79 73 20  umber is always 
86be0 72 6f 75 6e 64 65 64 0a 2a 2a 20 75 70 20 74 6f  rounded.** up to
86bf0 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32   a multiple of 2
86c00 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
86c10 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
86c20 4d 65 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63  MemdebugBacktrac
86c30 65 28 69 6e 74 20 64 65 70 74 68 29 7b 0a 20 20  e(int depth){.  
86c40 69 66 28 20 64 65 70 74 68 3c 30 20 29 7b 20 64  if( depth<0 ){ d
86c50 65 70 74 68 20 3d 20 30 3b 20 7d 0a 20 20 69 66  epth = 0; }.  if
86c60 28 20 64 65 70 74 68 3e 32 30 20 29 7b 20 64 65  ( depth>20 ){ de
86c70 70 74 68 20 3d 20 32 30 3b 20 7d 0a 20 20 64 65  pth = 20; }.  de
86c80 70 74 68 20 3d 20 28 64 65 70 74 68 2b 31 29 26  pth = (depth+1)&
86c90 30 78 66 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 63  0xfe;.  mem.nBac
86ca0 6b 74 72 61 63 65 20 3d 20 64 65 70 74 68 3b 0a  ktrace = depth;.
86cb0 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  }..SQLITE_PRIVAT
86cc0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65  E void sqlite3Me
86cd0 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 43  mdebugBacktraceC
86ce0 61 6c 6c 62 61 63 6b 28 76 6f 69 64 20 28 2a 78  allback(void (*x
86cf0 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20  Backtrace)(int, 
86d00 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0a  int, void **)){.
86d10 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65    mem.xBacktrace
86d20 20 3d 20 78 42 61 63 6b 74 72 61 63 65 3b 0a 7d   = xBacktrace;.}
86d30 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
86d40 74 69 74 6c 65 20 73 74 72 69 6e 67 20 66 6f 72  title string for
86d50 20 73 75 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f   subsequent allo
86d60 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49  cations..*/.SQLI
86d70 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
86d80 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53  sqlite3MemdebugS
86d90 65 74 74 69 74 6c 65 28 63 6f 6e 73 74 20 63 68  ettitle(const ch
86da0 61 72 20 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 75  ar *zTitle){.  u
86db0 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 20 3d 20  nsigned int n = 
86dc0 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
86dd0 7a 54 69 74 6c 65 29 20 2b 20 31 3b 0a 20 20 73  zTitle) + 1;.  s
86de0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
86df0 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20  er(mem.mutex);. 
86e00 20 69 66 28 20 6e 3e 3d 73 69 7a 65 6f 66 28 6d   if( n>=sizeof(m
86e10 65 6d 2e 7a 54 69 74 6c 65 29 20 29 20 6e 20 3d  em.zTitle) ) n =
86e20 20 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74   sizeof(mem.zTit
86e30 6c 65 29 2d 31 3b 0a 20 20 6d 65 6d 63 70 79 28  le)-1;.  memcpy(
86e40 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 7a 54 69 74  mem.zTitle, zTit
86e50 6c 65 2c 20 6e 29 3b 0a 20 20 6d 65 6d 2e 7a 54  le, n);.  mem.zT
86e60 69 74 6c 65 5b 6e 5d 20 3d 20 30 3b 0a 20 20 6d  itle[n] = 0;.  m
86e70 65 6d 2e 6e 54 69 74 6c 65 20 3d 20 52 4f 55 4e  em.nTitle = ROUN
86e80 44 38 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33  D8(n);.  sqlite3
86e90 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
86ea0 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49  .mutex);.}..SQLI
86eb0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
86ec0 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53  sqlite3MemdebugS
86ed0 79 6e 63 28 29 7b 0a 20 20 73 74 72 75 63 74 20  ync(){.  struct 
86ee0 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64  MemBlockHdr *pHd
86ef0 72 3b 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65  r;.  for(pHdr=me
86f00 6d 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20  m.pFirst; pHdr; 
86f10 70 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74  pHdr=pHdr->pNext
86f20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 2a 70 42  ){.    void **pB
86f30 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72  t = (void**)pHdr
86f40 3b 0a 20 20 20 20 70 42 74 20 2d 3d 20 70 48 64  ;.    pBt -= pHd
86f50 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f  r->nBacktraceSlo
86f60 74 73 3b 0a 20 20 20 20 6d 65 6d 2e 78 42 61 63  ts;.    mem.xBac
86f70 6b 74 72 61 63 65 28 70 48 64 72 2d 3e 69 53 69  ktrace(pHdr->iSi
86f80 7a 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74  ze, pHdr->nBackt
86f90 72 61 63 65 2d 31 2c 20 26 70 42 74 5b 31 5d 29  race-1, &pBt[1])
86fa0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f  ;.  }.}../*.** O
86fb0 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64  pen the file ind
86fc0 69 63 61 74 65 64 20 61 6e 64 20 77 72 69 74 65  icated and write
86fd0 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e   a log of all un
86fe0 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a  freed memory .**
86ff0 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74   allocations int
87000 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53  o that log..*/.S
87010 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
87020 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62  id sqlite3Memdeb
87030 75 67 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61  ugDump(const cha
87040 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20  r *zFilename){. 
87050 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 73 74   FILE *out;.  st
87060 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72  ruct MemBlockHdr
87070 20 2a 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a   *pHdr;.  void *
87080 2a 70 42 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  *pBt;.  int i;. 
87090 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69   out = fopen(zFi
870a0 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20  lename, "w");.  
870b0 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20  if( out==0 ){.  
870c0 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72    fprintf(stderr
870d0 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20  , "** Unable to 
870e0 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65  output memory de
870f0 62 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20  bug output log: 
87100 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20  %s **\n",.      
87110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 46                zF
87120 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65  ilename);.    re
87130 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  turn;.  }.  for(
87140 70 48 64 72 3d 6d 65 6d 2e 70 46 69 72 73 74 3b  pHdr=mem.pFirst;
87150 20 70 48 64 72 3b 20 70 48 64 72 3d 70 48 64 72   pHdr; pHdr=pHdr
87160 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 63 68  ->pNext){.    ch
87170 61 72 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29 70  ar *z = (char*)p
87180 48 64 72 3b 0a 20 20 20 20 7a 20 2d 3d 20 70 48  Hdr;.    z -= pH
87190 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c  dr->nBacktraceSl
871a0 6f 74 73 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a  ots*sizeof(void*
871b0 29 20 2b 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65  ) + pHdr->nTitle
871c0 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  ;.    fprintf(ou
871d0 74 2c 20 22 2a 2a 2a 2a 20 25 6c 6c 64 20 62 79  t, "**** %lld by
871e0 74 65 73 20 61 74 20 25 70 20 66 72 6f 6d 20 25  tes at %p from %
871f0 73 20 2a 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20 20  s ****\n", .    
87200 20 20 20 20 20 20 20 20 70 48 64 72 2d 3e 69 53          pHdr->iS
87210 69 7a 65 2c 20 26 70 48 64 72 5b 31 5d 2c 20 70  ize, &pHdr[1], p
87220 48 64 72 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a 20  Hdr->nTitle ? z 
87230 3a 20 22 3f 3f 3f 22 29 3b 0a 20 20 20 20 69 66  : "???");.    if
87240 28 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61  ( pHdr->nBacktra
87250 63 65 20 29 7b 0a 20 20 20 20 20 20 66 66 6c 75  ce ){.      fflu
87260 73 68 28 6f 75 74 29 3b 0a 20 20 20 20 20 20 70  sh(out);.      p
87270 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64  Bt = (void**)pHd
87280 72 3b 0a 20 20 20 20 20 20 70 42 74 20 2d 3d 20  r;.      pBt -= 
87290 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
872a0 53 6c 6f 74 73 3b 0a 20 20 20 20 20 20 62 61 63  Slots;.      bac
872b0 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66  ktrace_symbols_f
872c0 64 28 70 42 74 2c 20 70 48 64 72 2d 3e 6e 42 61  d(pBt, pHdr->nBa
872d0 63 6b 74 72 61 63 65 2c 20 66 69 6c 65 6e 6f 28  cktrace, fileno(
872e0 6f 75 74 29 29 3b 0a 20 20 20 20 20 20 66 70 72  out));.      fpr
872f0 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b  intf(out, "\n");
87300 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 70 72  .    }.  }.  fpr
87310 69 6e 74 66 28 6f 75 74 2c 20 22 43 4f 55 4e 54  intf(out, "COUNT
87320 53 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d  S:\n");.  for(i=
87330 30 3b 20 69 3c 4e 43 53 49 5a 45 2d 31 3b 20 69  0; i<NCSIZE-1; i
87340 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d  ++){.    if( mem
87350 2e 6e 41 6c 6c 6f 63 5b 69 5d 20 29 7b 0a 20 20  .nAlloc[i] ){.  
87360 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
87370 20 22 20 20 20 25 35 64 3a 20 25 31 30 64 20 25   "   %5d: %10d %
87380 31 30 64 20 25 31 30 64 5c 6e 22 2c 20 0a 20 20  10d %10d\n", .  
87390 20 20 20 20 20 20 20 20 20 20 69 2a 38 2c 20 6d            i*8, m
873a0 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2c 20 6d 65  em.nAlloc[i], me
873b0 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2c 20 6d  m.nCurrent[i], m
873c0 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d 29  em.mxCurrent[i])
873d0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
873e0 28 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53  ( mem.nAlloc[NCS
873f0 49 5a 45 2d 31 5d 20 29 7b 0a 20 20 20 20 66 70  IZE-1] ){.    fp
87400 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25  rintf(out, "   %
87410 35 64 3a 20 25 31 30 64 20 25 31 30 64 20 25 31  5d: %10d %10d %1
87420 30 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  0d\n",.         
87430 20 20 20 20 4e 43 53 49 5a 45 2a 38 2d 38 2c 20      NCSIZE*8-8, 
87440 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a  mem.nAlloc[NCSIZ
87450 45 2d 31 5d 2c 0a 20 20 20 20 20 20 20 20 20 20  E-1],.          
87460 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b     mem.nCurrent[
87470 4e 43 53 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e 6d  NCSIZE-1], mem.m
87480 78 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d  xCurrent[NCSIZE-
87490 31 5d 29 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f 73  1]);.  }.  fclos
874a0 65 28 6f 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  e(out);.}../*.**
874b0 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
874c0 65 72 20 6f 66 20 74 69 6d 65 73 20 73 71 6c 69  er of times sqli
874d0 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 20 68  te3MemMalloc() h
874e0 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e 0a  as been called..
874f0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
87500 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d  E int sqlite3Mem
87510 64 65 62 75 67 4d 61 6c 6c 6f 63 43 6f 75 6e 74  debugMallocCount
87520 28 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  (){.  int i;.  i
87530 6e 74 20 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20  nt nTotal = 0;. 
87540 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49   for(i=0; i<NCSI
87550 5a 45 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 54  ZE; i++){.    nT
87560 6f 74 61 6c 20 2b 3d 20 6d 65 6d 2e 6e 41 6c 6c  otal += mem.nAll
87570 6f 63 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74  oc[i];.  }.  ret
87580 75 72 6e 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a 0a  urn nTotal;.}...
87590 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
875a0 5f 4d 45 4d 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a  _MEMDEBUG */../*
875b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
875c0 64 20 6f 66 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a  d of mem2.c ****
875d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
875e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
875f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
87600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
87610 67 69 6e 20 66 69 6c 65 20 6d 65 6d 33 2e 63 20  gin file mem3.c 
87620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
87650 0a 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 72  .** 2007 October
87660 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   14.**.** The au
87670 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
87680 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
87690 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
876a0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
876b0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
876c0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
876d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
876e0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
876f0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
87700 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
87710 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
87720 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
87730 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
87740 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
87750 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
87760 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
87770 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
87780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
877a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
877b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
877c0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
877d0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66  contains the C f
877e0 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d  unctions that im
877f0 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79  plement a memory
87800 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  .** allocation s
87810 75 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73 65  ubsystem for use
87820 20 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a   by SQLite. .**.
87830 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20  ** This version 
87840 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  of the memory al
87850 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
87860 65 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20  em omits all.** 
87870 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e  use of malloc().
87880 20 54 68 65 20 53 51 4c 69 74 65 20 75 73 65 72   The SQLite user
87890 20 73 75 70 70 6c 69 65 73 20 61 20 62 6c 6f 63   supplies a bloc
878a0 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62  k of memory.** b
878b0 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71  efore calling sq
878c0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
878d0 28 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61 6c  () from which al
878e0 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65  locations.** are
878f0 20 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72 6e   made and return
87900 65 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f  ed by the xMallo
87910 63 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63  c() and xRealloc
87920 28 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  () .** implement
87930 61 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c  ations. Once sql
87940 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
87950 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65  ) has been calle
87960 64 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74  d,.** the amount
87970 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c   of memory avail
87980 61 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 69  able to SQLite i
87990 73 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e 6e  s fixed and cann
879a0 6f 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 64  ot.** be changed
879b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72  ..**.** This ver
879c0 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f  sion of the memo
879d0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75  ry allocation su
879e0 62 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c 75  bsystem is inclu
879f0 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75  ded.** in the bu
87a00 69 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49  ild only if SQLI
87a10 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
87a20 33 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a  3 is defined..**
87a30 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 33 2e 63 2c  .** $Id: mem3.c,
87a40 76 20 31 2e 32 35 20 32 30 30 38 2f 31 31 2f 31  v 1.25 2008/11/1
87a50 39 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69 65  9 16:52:44 danie
87a60 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
87a70 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73  ./*.** This vers
87a80 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  ion of the memor
87a90 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 6f  y allocator is o
87aa0 6e 6c 79 20 62 75 69 6c 74 20 69 6e 74 6f 20 74  nly built into t
87ab0 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 53 51  he library.** SQ
87ac0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53  LITE_ENABLE_MEMS
87ad0 59 53 33 20 69 73 20 64 65 66 69 6e 65 64 2e 20  YS3 is defined. 
87ae0 44 65 66 69 6e 69 6e 67 20 74 68 69 73 20 73 79  Defining this sy
87af0 6d 62 6f 6c 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a  mbol does not.**
87b00 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 6c   mean that the l
87b10 69 62 72 61 72 79 20 77 69 6c 6c 20 75 73 65 20  ibrary will use 
87b20 61 20 6d 65 6d 6f 72 79 2d 70 6f 6f 6c 20 62 79  a memory-pool by
87b30 20 64 65 66 61 75 6c 74 2c 20 6a 75 73 74 20 74   default, just t
87b40 68 61 74 0a 2a 2a 20 69 74 20 69 73 20 61 76 61  hat.** it is ava
87b50 69 6c 61 62 6c 65 2e 20 54 68 65 20 6d 65 6d 70  ilable. The memp
87b60 6f 6f 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73  ool allocator is
87b70 20 61 63 74 69 76 61 74 65 64 20 62 79 20 63 61   activated by ca
87b80 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33  lling.** sqlite3
87b90 5f 63 6f 6e 66 69 67 28 29 2e 0a 2a 2f 0a 23 69  _config()..*/.#i
87ba0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
87bb0 4c 45 5f 4d 45 4d 53 59 53 33 0a 0a 2f 2a 0a 2a  LE_MEMSYS3../*.*
87bc0 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 28  * Maximum size (
87bd0 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 6f  in Mem3Blocks) o
87be0 66 20 61 20 22 73 6d 61 6c 6c 22 20 63 68 75 6e  f a "small" chun
87bf0 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58  k..*/.#define MX
87c00 5f 53 4d 41 4c 4c 20 31 30 0a 0a 0a 2f 2a 0a 2a  _SMALL 10.../*.*
87c10 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65  * Number of free
87c20 6c 69 73 74 20 68 61 73 68 20 73 6c 6f 74 73 0a  list hash slots.
87c30 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 48 41 53  */.#define N_HAS
87c40 48 20 20 36 31 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d  H  61../*.** A m
87c50 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
87c60 20 28 61 6c 73 6f 20 63 61 6c 6c 65 64 20 61 20   (also called a 
87c70 22 63 68 75 6e 6b 22 29 20 63 6f 6e 73 69 73 74  "chunk") consist
87c80 73 20 6f 66 20 74 77 6f 20 6f 72 20 0a 2a 2a 20  s of two or .** 
87c90 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 77 68 65 72  more blocks wher
87ca0 65 20 65 61 63 68 20 62 6c 6f 63 6b 20 69 73 20  e each block is 
87cb0 38 20 62 79 74 65 73 2e 20 20 54 68 65 20 66 69  8 bytes.  The fi
87cc0 72 73 74 20 38 20 62 79 74 65 73 20 61 72 65 20  rst 8 bytes are 
87cd0 0a 2a 2a 20 61 20 68 65 61 64 65 72 20 74 68 61  .** a header tha
87ce0 74 20 69 73 20 6e 6f 74 20 72 65 74 75 72 6e 65  t is not returne
87cf0 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 2a  d to the user..*
87d00 2a 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 73 20  *.** A chunk is 
87d10 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 6c 6f 63  two or more bloc
87d20 6b 73 20 74 68 61 74 20 69 73 20 65 69 74 68 65  ks that is eithe
87d30 72 20 63 68 65 63 6b 65 64 20 6f 75 74 20 6f 72  r checked out or
87d40 0a 2a 2a 20 66 72 65 65 2e 20 20 54 68 65 20 66  .** free.  The f
87d50 69 72 73 74 20 62 6c 6f 63 6b 20 68 61 73 20 66  irst block has f
87d60 6f 72 6d 61 74 20 75 2e 68 64 72 2e 20 20 75 2e  ormat u.hdr.  u.
87d70 68 64 72 2e 73 69 7a 65 34 78 20 69 73 20 34 20  hdr.size4x is 4 
87d80 74 69 6d 65 73 20 74 68 65 0a 2a 2a 20 73 69 7a  times the.** siz
87d90 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74  e of the allocat
87da0 69 6f 6e 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66  ion in blocks if
87db0 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
87dc0 69 73 20 66 72 65 65 2e 0a 2a 2a 20 54 68 65 20  is free..** The 
87dd0 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 20 62  u.hdr.size4x&1 b
87de0 69 74 20 69 73 20 74 72 75 65 20 69 66 20 74 68  it is true if th
87df0 65 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b  e chunk is check
87e00 65 64 20 6f 75 74 20 61 6e 64 0a 2a 2a 20 66 61  ed out and.** fa
87e10 6c 73 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b  lse if the chunk
87e20 20 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c   is on the freel
87e30 69 73 74 2e 20 20 54 68 65 20 75 2e 68 64 72 2e  ist.  The u.hdr.
87e40 73 69 7a 65 34 78 26 32 20 62 69 74 0a 2a 2a 20  size4x&2 bit.** 
87e50 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 70  is true if the p
87e60 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73  revious chunk is
87e70 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64   checked out and
87e80 20 66 61 6c 73 65 20 69 66 20 74 68 65 0a 2a 2a   false if the.**
87e90 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20   previous chunk 
87ea0 69 73 20 66 72 65 65 2e 20 20 54 68 65 20 75 2e  is free.  The u.
87eb0 68 64 72 2e 70 72 65 76 53 69 7a 65 20 66 69 65  hdr.prevSize fie
87ec0 6c 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f  ld is the size o
87ed0 66 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75  f.** the previou
87ee0 73 20 63 68 75 6e 6b 20 69 6e 20 62 6c 6f 63 6b  s chunk in block
87ef0 73 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75  s if the previou
87f00 73 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68  s chunk is on th
87f10 65 0a 2a 2a 20 66 72 65 65 6c 69 73 74 2e 20 49  e.** freelist. I
87f20 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63  f the previous c
87f30 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20  hunk is checked 
87f40 6f 75 74 2c 20 74 68 65 6e 0a 2a 2a 20 75 2e 68  out, then.** u.h
87f50 64 72 2e 70 72 65 76 53 69 7a 65 20 63 61 6e 20  dr.prevSize can 
87f60 62 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 64  be part of the d
87f70 61 74 61 20 66 6f 72 20 74 68 61 74 20 63 68 75  ata for that chu
87f80 6e 6b 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a  nk and should.**
87f90 20 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20   not be read or 
87fa0 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 57  written..**.** W
87fb0 65 20 6f 66 74 65 6e 20 69 64 65 6e 74 69 66 79  e often identify
87fc0 20 61 20 63 68 75 6e 6b 20 62 79 20 69 74 73 20   a chunk by its 
87fd0 69 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50  index in mem3.aP
87fe0 6f 6f 6c 5b 5d 2e 20 20 57 68 65 6e 0a 2a 2a 20  ool[].  When.** 
87ff0 74 68 69 73 20 69 73 20 64 6f 6e 65 2c 20 74 68  this is done, th
88000 65 20 63 68 75 6e 6b 20 69 6e 64 65 78 20 72 65  e chunk index re
88010 66 65 72 73 20 74 6f 20 74 68 65 20 73 65 63 6f  fers to the seco
88020 6e 64 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 74  nd block of.** t
88030 68 65 20 63 68 75 6e 6b 2e 20 20 49 6e 20 74 68  he chunk.  In th
88040 69 73 20 77 61 79 2c 20 74 68 65 20 66 69 72 73  is way, the firs
88050 74 20 63 68 75 6e 6b 20 68 61 73 20 61 6e 20 69  t chunk has an i
88060 6e 64 65 78 20 6f 66 20 31 2e 0a 2a 2a 20 41 20  ndex of 1..** A 
88070 63 68 75 6e 6b 20 69 6e 64 65 78 20 6f 66 20 30  chunk index of 0
88080 20 6d 65 61 6e 73 20 22 6e 6f 20 73 75 63 68 20   means "no such 
88090 63 68 75 6e 6b 22 20 61 6e 64 20 69 73 20 74 68  chunk" and is th
880a0 65 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a 2a 20  e equivalent.** 
880b0 6f 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  of a NULL pointe
880c0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63  r..**.** The sec
880d0 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 66 72 65  ond block of fre
880e0 65 20 63 68 75 6e 6b 73 20 69 73 20 6f 66 20 74  e chunks is of t
880f0 68 65 20 66 6f 72 6d 20 75 2e 6c 69 73 74 2e 20  he form u.list. 
88100 20 54 68 65 0a 2a 2a 20 74 77 6f 20 66 69 65 6c   The.** two fiel
88110 64 73 20 66 6f 72 6d 20 61 20 64 6f 75 62 6c 65  ds form a double
88120 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20  -linked list of 
88130 63 68 75 6e 6b 73 20 6f 66 20 72 65 6c 61 74 65  chunks of relate
88140 64 20 73 69 7a 65 73 2e 0a 2a 2a 20 50 6f 69 6e  d sizes..** Poin
88150 74 65 72 73 20 74 6f 20 74 68 65 20 68 65 61 64  ters to the head
88160 20 6f 66 20 74 68 65 20 6c 69 73 74 20 61 72 65   of the list are
88170 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 33 2e   stored in mem3.
88180 61 69 53 6d 61 6c 6c 5b 5d 20 0a 2a 2a 20 66 6f  aiSmall[] .** fo
88190 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73  r smaller chunks
881a0 20 61 6e 64 20 6d 65 6d 33 2e 61 69 48 61 73 68   and mem3.aiHash
881b0 5b 5d 20 66 6f 72 20 6c 61 72 67 65 72 20 63 68  [] for larger ch
881c0 75 6e 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  unks..**.** The 
881d0 73 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20  second block of 
881e0 61 20 63 68 75 6e 6b 20 69 73 20 75 73 65 72 20  a chunk is user 
881f0 64 61 74 61 20 69 66 20 74 68 65 20 63 68 75 6e  data if the chun
88200 6b 20 69 73 20 63 68 65 63 6b 65 64 20 0a 2a 2a  k is checked .**
88210 20 6f 75 74 2e 20 20 49 66 20 61 20 63 68 75 6e   out.  If a chun
88220 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74  k is checked out
88230 2c 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20  , the user data 
88240 6d 61 79 20 65 78 74 65 6e 64 20 69 6e 74 6f 0a  may extend into.
88250 2a 2a 20 74 68 65 20 75 2e 68 64 72 2e 70 72 65  ** the u.hdr.pre
88260 76 53 69 7a 65 20 76 61 6c 75 65 20 6f 66 20 74  vSize value of t
88270 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 75  he following chu
88280 6e 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  nk..*/.typedef s
88290 74 72 75 63 74 20 4d 65 6d 33 42 6c 6f 63 6b 20  truct Mem3Block 
882a0 4d 65 6d 33 42 6c 6f 63 6b 3b 0a 73 74 72 75 63  Mem3Block;.struc
882b0 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 7b 0a 20 20  t Mem3Block {.  
882c0 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75  union {.    stru
882d0 63 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 70  ct {.      u32 p
882e0 72 65 76 53 69 7a 65 3b 20 20 20 2f 2a 20 53 69  revSize;   /* Si
882f0 7a 65 20 6f 66 20 70 72 65 76 69 6f 75 73 20 63  ze of previous c
88300 68 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63  hunk in Mem3Bloc
88310 6b 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20  k elements */.  
88320 20 20 20 20 75 33 32 20 73 69 7a 65 34 78 3b 20      u32 size4x; 
88330 20 20 20 20 2f 2a 20 34 78 20 74 68 65 20 73 69      /* 4x the si
88340 7a 65 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68  ze of current ch
88350 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b  unk in Mem3Block
88360 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20   elements */.   
88370 20 7d 20 68 64 72 3b 0a 20 20 20 20 73 74 72 75   } hdr;.    stru
88380 63 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 6e  ct {.      u32 n
88390 65 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e  ext;       /* In
883a0 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f  dex in mem3.aPoo
883b0 6c 5b 5d 20 6f 66 20 6e 65 78 74 20 66 72 65 65  l[] of next free
883c0 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20   chunk */.      
883d0 75 33 32 20 70 72 65 76 3b 20 20 20 20 20 20 20  u32 prev;       
883e0 2f 2a 20 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33  /* Index in mem3
883f0 2e 61 50 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 76  .aPool[] of prev
88400 69 6f 75 73 20 66 72 65 65 20 63 68 75 6e 6b 20  ious free chunk 
88410 2a 2f 0a 20 20 20 20 7d 20 6c 69 73 74 3b 0a 20  */.    } list;. 
88420 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20   } u;.};../*.** 
88430 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69  All of the stati
88440 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64  c variables used
88450 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20   by this module 
88460 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a  are collected.**
88470 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73   into a single s
88480 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22  tructure named "
88490 6d 65 6d 33 22 2e 20 20 54 68 69 73 20 69 73 20  mem3".  This is 
884a0 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73  to keep the.** s
884b0 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20  tatic variables 
884c0 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f  organized and to
884d0 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63   reduce namespac
884e0 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77  e pollution.** w
884f0 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20  hen this module 
88500 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68  is combined with
88510 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d   other in the am
88520 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73  algamation..*/.s
88530 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44  tatic SQLITE_WSD
88540 20 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62   struct Mem3Glob
88550 61 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d  al {.  /*.  ** M
88560 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20  emory available 
88570 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  for allocation. 
88580 6e 50 6f 6f 6c 20 69 73 20 74 68 65 20 73 69 7a  nPool is the siz
88590 65 20 6f 66 20 74 68 65 20 61 72 72 61 79 0a 20  e of the array. 
885a0 20 2a 2a 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63   ** (in Mem3Bloc
885b0 6b 73 29 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  ks) pointed to b
885c0 79 20 61 50 6f 6f 6c 20 6c 65 73 73 20 32 2e 0a  y aPool less 2..
885d0 20 20 2a 2f 0a 20 20 75 33 32 20 6e 50 6f 6f 6c    */.  u32 nPool
885e0 3b 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 61  ;.  Mem3Block *a
885f0 50 6f 6f 6c 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  Pool;..  /*.  **
88600 20 54 72 75 65 20 69 66 20 77 65 20 61 72 65 20   True if we are 
88610 65 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 6f 75  evaluating an ou
88620 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 61 6c 6c  t-of-memory call
88630 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74  back..  */.  int
88640 20 61 6c 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20   alarmBusy;.  . 
88650 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74   /*.  ** Mutex t
88660 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73  o control access
88670 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   to the memory a
88680 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
88690 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  tem..  */.  sqli
886a0 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
886b0 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54  ;.  .  /*.  ** T
886c0 68 65 20 6d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e  he minimum amoun
886d0 74 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 20  t of free space 
886e0 74 68 61 74 20 77 65 20 68 61 76 65 20 73 65 65  that we have see
886f0 6e 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 6d 6e  n..  */.  u32 mn
88700 4d 61 73 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20  Master;..  /*.  
88710 2a 2a 20 69 4d 61 73 74 65 72 20 69 73 20 74 68  ** iMaster is th
88720 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d  e index of the m
88730 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 4d 6f  aster chunk.  Mo
88740 73 74 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f  st new allocatio
88750 6e 73 0a 20 20 2a 2a 20 6f 63 63 75 72 20 6f 66  ns.  ** occur of
88760 66 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b 2e  f of this chunk.
88770 20 20 73 7a 4d 61 73 74 65 72 20 69 73 20 74 68    szMaster is th
88780 65 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42  e size (in Mem3B
88790 6c 6f 63 6b 73 29 0a 20 20 2a 2a 20 6f 66 20 74  locks).  ** of t
887a0 68 65 20 63 75 72 72 65 6e 74 20 6d 61 73 74 65  he current maste
887b0 72 2e 20 20 69 4d 61 73 74 65 72 20 69 73 20 30  r.  iMaster is 0
887c0 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74   if there is not
887d0 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 0a 20   master chunk.. 
887e0 20 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 63   ** The master c
887f0 68 75 6e 6b 20 69 73 20 6e 6f 74 20 69 6e 20 65  hunk is not in e
88800 69 74 68 65 72 20 74 68 65 20 61 69 48 61 73 68  ither the aiHash
88810 5b 5d 20 6f 72 20 61 69 53 6d 61 6c 6c 5b 5d 2e  [] or aiSmall[].
88820 0a 20 20 2a 2f 0a 20 20 75 33 32 20 69 4d 61 73  .  */.  u32 iMas
88830 74 65 72 3b 0a 20 20 75 33 32 20 73 7a 4d 61 73  ter;.  u32 szMas
88840 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ter;..  /*.  ** 
88850 41 72 72 61 79 20 6f 66 20 6c 69 73 74 73 20 6f  Array of lists o
88860 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 61 63  f free blocks ac
88870 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 62  cording to the b
88880 6c 6f 63 6b 20 73 69 7a 65 20 0a 20 20 2a 2a 20  lock size .  ** 
88890 66 6f 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e  for smaller chun
888a0 6b 73 2c 20 6f 72 20 61 20 68 61 73 68 20 6f 6e  ks, or a hash on
888b0 20 74 68 65 20 62 6c 6f 63 6b 20 73 69 7a 65 20   the block size 
888c0 66 6f 72 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20  for larger.  ** 
888d0 63 68 75 6e 6b 73 2e 0a 20 20 2a 2f 0a 20 20 75  chunks..  */.  u
888e0 33 32 20 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53 4d  32 aiSmall[MX_SM
888f0 41 4c 4c 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f 72  ALL-1];   /* For
88900 20 73 69 7a 65 73 20 32 20 74 68 72 6f 75 67 68   sizes 2 through
88910 20 4d 58 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c 75   MX_SMALL, inclu
88920 73 69 76 65 20 2a 2f 0a 20 20 75 33 32 20 61 69  sive */.  u32 ai
88930 48 61 73 68 5b 4e 5f 48 41 53 48 5d 3b 20 20 20  Hash[N_HASH];   
88940 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65       /* For size
88950 73 20 4d 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e 64  s MX_SMALL+1 and
88960 20 6c 61 72 67 65 72 20 2a 2f 0a 7d 20 6d 65 6d   larger */.} mem
88970 33 20 3d 20 7b 20 39 37 35 33 35 35 37 35 20 7d  3 = { 97535575 }
88980 3b 0a 0a 23 64 65 66 69 6e 65 20 6d 65 6d 33 20  ;..#define mem3 
88990 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65  GLOBAL(struct Me
889a0 6d 33 47 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29 0a  m3Global, mem3).
889b0 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68  ./*.** Unlink th
889c0 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 33 2e  e chunk at mem3.
889d0 61 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69  aPool[i] from li
889e0 73 74 20 69 74 20 69 73 20 63 75 72 72 65 6e 74  st it is current
889f0 6c 79 0a 2a 2a 20 6f 6e 2e 20 20 2a 70 52 6f 6f  ly.** on.  *pRoo
88a00 74 20 69 73 20 74 68 65 20 6c 69 73 74 20 74 68  t is the list th
88a10 61 74 20 69 20 69 73 20 61 20 6d 65 6d 62 65 72  at i is a member
88a20 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76   of..*/.static v
88a30 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e  oid memsys3Unlin
88a40 6b 46 72 6f 6d 4c 69 73 74 28 75 33 32 20 69 2c  kFromList(u32 i,
88a50 20 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20   u32 *pRoot){.  
88a60 75 33 32 20 6e 65 78 74 20 3d 20 6d 65 6d 33 2e  u32 next = mem3.
88a70 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e  aPool[i].u.list.
88a80 6e 65 78 74 3b 0a 20 20 75 33 32 20 70 72 65 76  next;.  u32 prev
88a90 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d   = mem3.aPool[i]
88aa0 2e 75 2e 6c 69 73 74 2e 70 72 65 76 3b 0a 20 20  .u.list.prev;.  
88ab0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
88ac0 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e  mutex_held(mem3.
88ad0 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
88ae0 70 72 65 76 3d 3d 30 20 29 7b 0a 20 20 20 20 2a  prev==0 ){.    *
88af0 70 52 6f 6f 74 20 3d 20 6e 65 78 74 3b 0a 20 20  pRoot = next;.  
88b00 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 33 2e  }else{.    mem3.
88b10 61 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69  aPool[prev].u.li
88b20 73 74 2e 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a  st.next = next;.
88b30 20 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 20 29    }.  if( next )
88b40 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c  {.    mem3.aPool
88b50 5b 6e 65 78 74 5d 2e 75 2e 6c 69 73 74 2e 70 72  [next].u.list.pr
88b60 65 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 20  ev = prev;.  }. 
88b70 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75   mem3.aPool[i].u
88b80 2e 6c 69 73 74 2e 6e 65 78 74 20 3d 20 30 3b 0a  .list.next = 0;.
88b90 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e    mem3.aPool[i].
88ba0 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 30 3b  u.list.prev = 0;
88bb0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b  .}../*.** Unlink
88bc0 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 6e   the chunk at in
88bd0 64 65 78 20 69 20 66 72 6f 6d 20 0a 2a 2a 20 77  dex i from .** w
88be0 68 61 74 65 76 65 72 20 6c 69 73 74 20 69 73 20  hatever list is 
88bf0 63 75 72 72 65 6e 74 6c 79 20 61 20 6d 65 6d 62  currently a memb
88c00 65 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63  er of..*/.static
88c10 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c   void memsys3Unl
88c20 69 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 33  ink(u32 i){.  u3
88c30 32 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20  2 size, hash;.  
88c40 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
88c50 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e  mutex_held(mem3.
88c60 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
88c70 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  rt( (mem3.aPool[
88c80 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  i-1].u.hdr.size4
88c90 78 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 61  x & 1)==0 );.  a
88ca0 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20  ssert( i>=1 );. 
88cb0 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f   size = mem3.aPo
88cc0 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ol[i-1].u.hdr.si
88cd0 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74  ze4x/4;.  assert
88ce0 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f  ( size==mem3.aPo
88cf0 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68  ol[i+size-1].u.h
88d00 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20  dr.prevSize );. 
88d10 20 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 32   assert( size>=2
88d20 20 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 3c   );.  if( size <
88d30 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20  = MX_SMALL ){.  
88d40 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46    memsys3UnlinkF
88d50 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33  romList(i, &mem3
88d60 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d  .aiSmall[size-2]
88d70 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
88d80 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f  hash = size % N_
88d90 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73  HASH;.    memsys
88da0 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28  3UnlinkFromList(
88db0 69 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b  i, &mem3.aiHash[
88dc0 68 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  hash]);.  }.}../
88dd0 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68  *.** Link the ch
88de0 75 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f  unk at mem3.aPoo
88df0 6c 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20  l[i] so that is 
88e00 6f 6e 20 74 68 65 20 6c 69 73 74 20 72 6f 6f 74  on the list root
88e10 65 64 0a 2a 2a 20 61 74 20 2a 70 52 6f 6f 74 2e  ed.** at *pRoot.
88e20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
88e30 6d 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c  memsys3LinkIntoL
88e40 69 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a  ist(u32 i, u32 *
88e50 70 52 6f 6f 74 29 7b 0a 20 20 61 73 73 65 72 74  pRoot){.  assert
88e60 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
88e70 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29  held(mem3.mutex)
88e80 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c   );.  mem3.aPool
88e90 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20  [i].u.list.next 
88ea0 3d 20 2a 70 52 6f 6f 74 3b 0a 20 20 6d 65 6d 33  = *pRoot;.  mem3
88eb0 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74  .aPool[i].u.list
88ec0 2e 70 72 65 76 20 3d 20 30 3b 0a 20 20 69 66 28  .prev = 0;.  if(
88ed0 20 2a 70 52 6f 6f 74 20 29 7b 0a 20 20 20 20 6d   *pRoot ){.    m
88ee0 65 6d 33 2e 61 50 6f 6f 6c 5b 2a 70 52 6f 6f 74  em3.aPool[*pRoot
88ef0 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20  ].u.list.prev = 
88f00 69 3b 0a 20 20 7d 0a 20 20 2a 70 52 6f 6f 74 20  i;.  }.  *pRoot 
88f10 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69  = i;.}../*.** Li
88f20 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20  nk the chunk at 
88f30 69 6e 64 65 78 20 69 20 69 6e 74 6f 20 65 69 74  index i into eit
88f40 68 65 72 20 74 68 65 20 61 70 70 72 6f 70 72 69  her the appropri
88f50 61 74 65 0a 2a 2a 20 73 6d 61 6c 6c 20 63 68 75  ate.** small chu
88f60 6e 6b 20 6c 69 73 74 2c 20 6f 72 20 69 6e 74 6f  nk list, or into
88f70 20 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b   the large chunk
88f80 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a   hash table..*/.
88f90 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
88fa0 79 73 33 4c 69 6e 6b 28 75 33 32 20 69 29 7b 0a  ys3Link(u32 i){.
88fb0 20 20 75 33 32 20 73 69 7a 65 2c 20 68 61 73 68    u32 size, hash
88fc0 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
88fd0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d  te3_mutex_held(m
88fe0 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20  em3.mutex) );.  
88ff0 61 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a  assert( i>=1 );.
89000 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e    assert( (mem3.
89010 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72  aPool[i-1].u.hdr
89020 2e 73 69 7a 65 34 78 20 26 20 31 29 3d 3d 30 20  .size4x & 1)==0 
89030 29 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33  );.  size = mem3
89040 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64  .aPool[i-1].u.hd
89050 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73  r.size4x/4;.  as
89060 73 65 72 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33  sert( size==mem3
89070 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d  .aPool[i+size-1]
89080 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20  .u.hdr.prevSize 
89090 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a  );.  assert( siz
890a0 65 3e 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 69  e>=2 );.  if( si
890b0 7a 65 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29  ze <= MX_SMALL )
890c0 7b 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e  {.    memsys3Lin
890d0 6b 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65  kIntoList(i, &me
890e0 6d 33 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d  m3.aiSmall[size-
890f0 32 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  2]);.  }else{.  
89100 20 20 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20    hash = size % 
89110 4e 5f 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73  N_HASH;.    mems
89120 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28  ys3LinkIntoList(
89130 69 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b  i, &mem3.aiHash[
89140 68 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  hash]);.  }.}../
89150 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54  *.** If the STAT
89160 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20  IC_MEM mutex is 
89170 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64  not already held
89180 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e  , obtain it now.
89190 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69   The mutex.** wi
891a0 6c 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65  ll already be he
891b0 6c 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20  ld (obtained by 
891c0 63 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63  code in malloc.c
891d0 29 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47  ) if.** sqlite3G
891e0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d  lobalConfig.bMem
891f0 53 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f  Stat is true..*/
89200 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
89210 73 79 73 33 45 6e 74 65 72 28 76 6f 69 64 29 7b  sys3Enter(void){
89220 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
89230 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73  obalConfig.bMems
89240 74 61 74 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 6d  tat==0 && mem3.m
89250 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d  utex==0 ){.    m
89260 65 6d 33 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69  em3.mutex = sqli
89270 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
89280 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
89290 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71  C_MEM);.  }.  sq
892a0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
892b0 72 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d  r(mem3.mutex);.}
892c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
892d0 73 79 73 33 4c 65 61 76 65 28 76 6f 69 64 29 7b  sys3Leave(void){
892e0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
892f0 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65  _leave(mem3.mute
89300 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c  x);.}../*.** Cal
89310 6c 65 64 20 77 68 65 6e 20 77 65 20 61 72 65 20  led when we are 
89320 75 6e 61 62 6c 65 20 74 6f 20 73 61 74 69 73 66  unable to satisf
89330 79 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  y an allocation 
89340 6f 66 20 6e 42 79 74 65 73 2e 0a 2a 2f 0a 73 74  of nBytes..*/.st
89350 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73  atic void memsys
89360 33 4f 75 74 4f 66 4d 65 6d 6f 72 79 28 69 6e 74  3OutOfMemory(int
89370 20 6e 42 79 74 65 29 7b 0a 20 20 69 66 28 20 21   nByte){.  if( !
89380 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 29  mem3.alarmBusy )
89390 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d  {.    mem3.alarm
893a0 42 75 73 79 20 3d 20 31 3b 0a 20 20 20 20 61 73  Busy = 1;.    as
893b0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
893c0 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75  tex_held(mem3.mu
893d0 74 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c 69  tex) );.    sqli
893e0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
893f0 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 20  mem3.mutex);.   
89400 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
89410 5f 6d 65 6d 6f 72 79 28 6e 42 79 74 65 29 3b 0a  _memory(nByte);.
89420 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
89430 78 5f 65 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74  x_enter(mem3.mut
89440 65 78 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c  ex);.    mem3.al
89450 61 72 6d 42 75 73 79 20 3d 20 30 3b 0a 20 20 7d  armBusy = 0;.  }
89460 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 75 6e 6b  .}.../*.** Chunk
89470 20 69 20 69 73 20 61 20 66 72 65 65 20 63 68 75   i is a free chu
89480 6e 6b 20 74 68 61 74 20 68 61 73 20 62 65 65 6e  nk that has been
89490 20 75 6e 6c 69 6e 6b 65 64 2e 20 20 41 64 6a 75   unlinked.  Adju
894a0 73 74 20 69 74 73 20 0a 2a 2a 20 73 69 7a 65 20  st its .** size 
894b0 70 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 63  parameters for c
894c0 68 65 63 6b 2d 6f 75 74 20 61 6e 64 20 72 65 74  heck-out and ret
894d0 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
894e0 20 74 68 65 20 0a 2a 2a 20 75 73 65 72 20 70 6f   the .** user po
894f0 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 68 75  rtion of the chu
89500 6e 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  nk..*/.static vo
89510 69 64 20 2a 6d 65 6d 73 79 73 33 43 68 65 63 6b  id *memsys3Check
89520 6f 75 74 28 75 33 32 20 69 2c 20 75 33 32 20 6e  out(u32 i, u32 n
89530 42 6c 6f 63 6b 29 7b 0a 20 20 75 33 32 20 78 3b  Block){.  u32 x;
89540 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
89550 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65  e3_mutex_held(me
89560 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  m3.mutex) );.  a
89570 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20  ssert( i>=1 );. 
89580 20 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 50   assert( mem3.aP
89590 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73  ool[i-1].u.hdr.s
895a0 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20  ize4x/4==nBlock 
895b0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d  );.  assert( mem
895c0 33 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b  3.aPool[i+nBlock
895d0 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69  -1].u.hdr.prevSi
895e0 7a 65 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20  ze==nBlock );.  
895f0 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  x = mem3.aPool[i
89600 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
89610 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  ;.  mem3.aPool[i
89620 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
89630 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31 20   = nBlock*4 | 1 
89640 7c 20 28 78 26 32 29 3b 0a 20 20 6d 65 6d 33 2e  | (x&2);.  mem3.
89650 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31  aPool[i+nBlock-1
89660 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65  ].u.hdr.prevSize
89670 20 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65 6d   = nBlock;.  mem
89680 33 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b  3.aPool[i+nBlock
89690 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
896a0 20 7c 3d 20 32 3b 0a 20 20 72 65 74 75 72 6e 20   |= 2;.  return 
896b0 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a  &mem3.aPool[i];.
896c0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 72 76 65 20 61  }../*.** Carve a
896d0 20 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68   piece off of th
896e0 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 65 6d  e end of the mem
896f0 33 2e 69 4d 61 73 74 65 72 20 66 72 65 65 20 63  3.iMaster free c
89700 68 75 6e 6b 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  hunk..** Return 
89710 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
89720 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e   new allocation.
89730 20 20 4f 72 2c 20 69 66 20 74 68 65 20 6d 61 73    Or, if the mas
89740 74 65 72 20 63 68 75 6e 6b 0a 2a 2a 20 69 73 20  ter chunk.** is 
89750 6e 6f 74 20 6c 61 72 67 65 20 65 6e 6f 75 67 68  not large enough
89760 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73  , return 0..*/.s
89770 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73  tatic void *mems
89780 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 75 33  ys3FromMaster(u3
89790 32 20 6e 42 6c 6f 63 6b 29 7b 0a 20 20 61 73 73  2 nBlock){.  ass
897a0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
897b0 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74  ex_held(mem3.mut
897c0 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
897d0 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d   mem3.szMaster>=
897e0 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 69 66 28 20  nBlock );.  if( 
897f0 6e 42 6c 6f 63 6b 3e 3d 6d 65 6d 33 2e 73 7a 4d  nBlock>=mem3.szM
89800 61 73 74 65 72 2d 31 20 29 7b 0a 20 20 20 20 2f  aster-1 ){.    /
89810 2a 20 55 73 65 20 74 68 65 20 65 6e 74 69 72 65  * Use the entire
89820 20 6d 61 73 74 65 72 20 2a 2f 0a 20 20 20 20 76   master */.    v
89830 6f 69 64 20 2a 70 20 3d 20 6d 65 6d 73 79 73 33  oid *p = memsys3
89840 43 68 65 63 6b 6f 75 74 28 6d 65 6d 33 2e 69 4d  Checkout(mem3.iM
89850 61 73 74 65 72 2c 20 6d 65 6d 33 2e 73 7a 4d 61  aster, mem3.szMa
89860 73 74 65 72 29 3b 0a 20 20 20 20 6d 65 6d 33 2e  ster);.    mem3.
89870 69 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20  iMaster = 0;.   
89880 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d   mem3.szMaster =
89890 20 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d   0;.    mem3.mnM
898a0 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 72  aster = 0;.    r
898b0 65 74 75 72 6e 20 70 3b 0a 20 20 7d 65 6c 73 65  eturn p;.  }else
898c0 7b 0a 20 20 20 20 2f 2a 20 53 70 6c 69 74 20 74  {.    /* Split t
898d0 68 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e  he master block.
898e0 20 20 52 65 74 75 72 6e 20 74 68 65 20 74 61 69    Return the tai
898f0 6c 2e 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 65  l. */.    u32 ne
89900 77 69 2c 20 78 3b 0a 20 20 20 20 6e 65 77 69 20  wi, x;.    newi 
89910 3d 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 2b  = mem3.iMaster +
89920 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d   mem3.szMaster -
89930 20 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 61 73 73   nBlock;.    ass
89940 65 72 74 28 20 6e 65 77 69 20 3e 20 6d 65 6d 33  ert( newi > mem3
89950 2e 69 4d 61 73 74 65 72 2b 31 20 29 3b 0a 20 20  .iMaster+1 );.  
89960 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d    mem3.aPool[mem
89970 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73  3.iMaster+mem3.s
89980 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72  zMaster-1].u.hdr
89990 2e 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f  .prevSize = nBlo
899a0 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f  ck;.    mem3.aPo
899b0 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b  ol[mem3.iMaster+
899c0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d  mem3.szMaster-1]
899d0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d  .u.hdr.size4x |=
899e0 20 32 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f   2;.    mem3.aPo
899f0 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72  ol[newi-1].u.hdr
89a00 2e 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b  .size4x = nBlock
89a10 2a 34 20 2b 20 31 3b 0a 20 20 20 20 6d 65 6d 33  *4 + 1;.    mem3
89a20 2e 73 7a 4d 61 73 74 65 72 20 2d 3d 20 6e 42 6c  .szMaster -= nBl
89a30 6f 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50  ock;.    mem3.aP
89a40 6f 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64  ool[newi-1].u.hd
89a50 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d  r.prevSize = mem
89a60 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20  3.szMaster;.    
89a70 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d  x = mem3.aPool[m
89a80 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75  em3.iMaster-1].u
89a90 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b  .hdr.size4x & 2;
89aa0 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  .    mem3.aPool[
89ab0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e  mem3.iMaster-1].
89ac0 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d  u.hdr.size4x = m
89ad0 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c  em3.szMaster*4 |
89ae0 20 78 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33   x;.    if( mem3
89af0 2e 73 7a 4d 61 73 74 65 72 20 3c 20 6d 65 6d 33  .szMaster < mem3
89b00 2e 6d 6e 4d 61 73 74 65 72 20 29 7b 0a 20 20 20  .mnMaster ){.   
89b10 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72     mem3.mnMaster
89b20 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72   = mem3.szMaster
89b30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
89b40 72 6e 20 28 76 6f 69 64 2a 29 26 6d 65 6d 33 2e  rn (void*)&mem3.
89b50 61 50 6f 6f 6c 5b 6e 65 77 69 5d 3b 0a 20 20 7d  aPool[newi];.  }
89b60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f 74  .}../*.** *pRoot
89b70 20 69 73 20 74 68 65 20 68 65 61 64 20 6f 66 20   is the head of 
89b80 61 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 63  a list of free c
89b90 68 75 6e 6b 73 20 6f 66 20 74 68 65 20 73 61 6d  hunks of the sam
89ba0 65 20 73 69 7a 65 0a 2a 2a 20 6f 72 20 73 61 6d  e size.** or sam
89bb0 65 20 73 69 7a 65 20 68 61 73 68 2e 20 20 49 6e  e size hash.  In
89bc0 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 2a 70   other words, *p
89bd0 52 6f 6f 74 20 69 73 20 61 6e 20 65 6e 74 72 79  Root is an entry
89be0 20 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 6d 65   in either.** me
89bf0 6d 33 2e 61 69 53 6d 61 6c 6c 5b 5d 20 6f 72 20  m3.aiSmall[] or 
89c00 6d 65 6d 33 2e 61 69 48 61 73 68 5b 5d 2e 20 20  mem3.aiHash[].  
89c10 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
89c20 69 6e 65 20 65 78 61 6d 69 6e 65 73 20 61 6c 6c  ine examines all
89c30 20 65 6e 74 72 69 65 73 20 6f 6e 20 74 68 65 20   entries on the 
89c40 67 69 76 65 6e 20 6c 69 73 74 20 61 6e 64 20 74  given list and t
89c50 72 69 65 73 0a 2a 2a 20 74 6f 20 63 6f 61 6c 65  ries.** to coale
89c60 73 63 65 20 65 61 63 68 20 65 6e 74 72 69 65 73  sce each entries
89c70 20 77 69 74 68 20 61 64 6a 61 63 65 6e 74 20 66   with adjacent f
89c80 72 65 65 20 63 68 75 6e 6b 73 2e 20 20 0a 2a 2a  ree chunks.  .**
89c90 0a 2a 2a 20 49 66 20 69 74 20 73 65 65 73 20 61  .** If it sees a
89ca0 20 63 68 75 6e 6b 20 74 68 61 74 20 69 73 20 6c   chunk that is l
89cb0 61 72 67 65 72 20 74 68 61 6e 20 6d 65 6d 33 2e  arger than mem3.
89cc0 69 4d 61 73 74 65 72 2c 20 69 74 20 72 65 70 6c  iMaster, it repl
89cd0 61 63 65 73 20 0a 2a 2a 20 74 68 65 20 63 75 72  aces .** the cur
89ce0 72 65 6e 74 20 6d 65 6d 33 2e 69 4d 61 73 74 65  rent mem3.iMaste
89cf0 72 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 6c  r with the new l
89d00 61 72 67 65 72 20 63 68 75 6e 6b 2e 20 20 49 6e  arger chunk.  In
89d10 20 6f 72 64 65 72 20 66 6f 72 0a 2a 2a 20 74 68   order for.** th
89d20 69 73 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20  is mem3.iMaster 
89d30 72 65 70 6c 61 63 65 6d 65 6e 74 20 74 6f 20 77  replacement to w
89d40 6f 72 6b 2c 20 74 68 65 20 6d 61 73 74 65 72 20  ork, the master 
89d50 63 68 75 6e 6b 20 6d 75 73 74 20 62 65 0a 2a 2a  chunk must be.**
89d60 20 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 74 68 65   linked into the
89d70 20 68 61 73 68 20 74 61 62 6c 65 73 2e 20 20 54   hash tables.  T
89d80 68 61 74 20 69 73 20 6e 6f 74 20 74 68 65 20 6e  hat is not the n
89d90 6f 72 6d 61 6c 20 73 74 61 74 65 20 6f 66 0a 2a  ormal state of.*
89da0 2a 20 61 66 66 61 69 72 73 2c 20 6f 66 20 63 6f  * affairs, of co
89db0 75 72 73 65 2e 20 20 54 68 65 20 63 61 6c 6c 69  urse.  The calli
89dc0 6e 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20  ng routine must 
89dd0 6c 69 6e 6b 20 74 68 65 20 6d 61 73 74 65 72 0a  link the master.
89de0 2a 2a 20 63 68 75 6e 6b 20 62 65 66 6f 72 65 20  ** chunk before 
89df0 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f  invoking this ro
89e00 75 74 69 6e 65 2c 20 74 68 65 6e 20 6d 75 73 74  utine, then must
89e10 20 75 6e 6c 69 6e 6b 20 74 68 65 20 28 70 6f 73   unlink the (pos
89e20 73 69 62 6c 79 0a 2a 2a 20 63 68 61 6e 67 65 64  sibly.** changed
89e30 29 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6f  ) master chunk o
89e40 6e 63 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65  nce this routine
89e50 20 68 61 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a   has finished..*
89e60 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65  /.static void me
89e70 6d 73 79 73 33 4d 65 72 67 65 28 75 33 32 20 2a  msys3Merge(u32 *
89e80 70 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 69 4e  pRoot){.  u32 iN
89e90 65 78 74 2c 20 70 72 65 76 2c 20 73 69 7a 65 2c  ext, prev, size,
89ea0 20 69 2c 20 78 3b 0a 0a 20 20 61 73 73 65 72 74   i, x;..  assert
89eb0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
89ec0 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29  held(mem3.mutex)
89ed0 20 29 3b 0a 20 20 66 6f 72 28 69 3d 2a 70 52 6f   );.  for(i=*pRo
89ee0 6f 74 3b 20 69 3e 30 3b 20 69 3d 69 4e 65 78 74  ot; i>0; i=iNext
89ef0 29 7b 0a 20 20 20 20 69 4e 65 78 74 20 3d 20 6d  ){.    iNext = m
89f00 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c  em3.aPool[i].u.l
89f10 69 73 74 2e 6e 65 78 74 3b 0a 20 20 20 20 73 69  ist.next;.    si
89f20 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  ze = mem3.aPool[
89f30 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  i-1].u.hdr.size4
89f40 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  x;.    assert( (
89f50 73 69 7a 65 26 31 29 3d 3d 30 20 29 3b 0a 20 20  size&1)==0 );.  
89f60 20 20 69 66 28 20 28 73 69 7a 65 26 32 29 3d 3d    if( (size&2)==
89f70 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79  0 ){.      memsy
89f80 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74  s3UnlinkFromList
89f90 28 69 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20  (i, pRoot);.    
89fa0 20 20 61 73 73 65 72 74 28 20 69 20 3e 20 6d 65    assert( i > me
89fb0 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e  m3.aPool[i-1].u.
89fc0 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a  hdr.prevSize );.
89fd0 20 20 20 20 20 20 70 72 65 76 20 3d 20 69 20 2d        prev = i -
89fe0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d   mem3.aPool[i-1]
89ff0 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b  .u.hdr.prevSize;
8a000 0a 20 20 20 20 20 20 69 66 28 20 70 72 65 76 3d  .      if( prev=
8a010 3d 69 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20  =iNext ){.      
8a020 20 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61    iNext = mem3.a
8a030 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73  Pool[prev].u.lis
8a040 74 2e 6e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a  t.next;.      }.
8a050 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c        memsys3Unl
8a060 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20  ink(prev);.     
8a070 20 73 69 7a 65 20 3d 20 69 20 2b 20 73 69 7a 65   size = i + size
8a080 2f 34 20 2d 20 70 72 65 76 3b 0a 20 20 20 20 20  /4 - prev;.     
8a090 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b   x = mem3.aPool[
8a0a0 70 72 65 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  prev-1].u.hdr.si
8a0b0 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20  ze4x & 2;.      
8a0c0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d  mem3.aPool[prev-
8a0d0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
8a0e0 3d 20 73 69 7a 65 2a 34 20 7c 20 78 3b 0a 20 20  = size*4 | x;.  
8a0f0 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70      mem3.aPool[p
8a100 72 65 76 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64  rev+size-1].u.hd
8a110 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 73 69 7a  r.prevSize = siz
8a120 65 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33  e;.      memsys3
8a130 4c 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20  Link(prev);.    
8a140 20 20 69 20 3d 20 70 72 65 76 3b 0a 20 20 20 20    i = prev;.    
8a150 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 69 7a  }else{.      siz
8a160 65 20 2f 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20  e /= 4;.    }.  
8a170 20 20 69 66 28 20 73 69 7a 65 3e 6d 65 6d 33 2e    if( size>mem3.
8a180 73 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20  szMaster ){.    
8a190 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d    mem3.iMaster =
8a1a0 20 69 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73   i;.      mem3.s
8a1b0 7a 4d 61 73 74 65 72 20 3d 20 73 69 7a 65 3b 0a  zMaster = size;.
8a1c0 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
8a1d0 2a 2a 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 63  ** Return a bloc
8a1e0 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61  k of memory of a
8a1f0 74 20 6c 65 61 73 74 20 6e 42 79 74 65 73 20 69  t least nBytes i
8a200 6e 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72  n size..** Retur
8a210 6e 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62 6c 65  n NULL if unable
8a220 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
8a230 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68  ction assumes th
8a240 61 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79  at the necessary
8a250 20 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79   mutexes, if any
8a260 2c 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79  , are.** already
8a270 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c   held by the cal
8a280 6c 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61  ler. Hence "Unsa
8a290 66 65 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  fe"..*/.static v
8a2a0 6f 69 64 20 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c  oid *memsys3Mall
8a2b0 6f 63 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 79  ocUnsafe(int nBy
8a2c0 74 65 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20  te){.  u32 i;.  
8a2d0 75 33 32 20 6e 42 6c 6f 63 6b 3b 0a 20 20 75 33  u32 nBlock;.  u3
8a2e0 32 20 74 6f 46 72 65 65 3b 0a 0a 20 20 61 73 73  2 toFree;..  ass
8a2f0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
8a300 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74  ex_held(mem3.mut
8a310 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
8a320 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63   sizeof(Mem3Bloc
8a330 6b 29 3d 3d 38 20 29 3b 0a 20 20 69 66 28 20 6e  k)==8 );.  if( n
8a340 42 79 74 65 3c 3d 31 32 20 29 7b 0a 20 20 20 20  Byte<=12 ){.    
8a350 6e 42 6c 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d 65  nBlock = 2;.  }e
8a360 6c 73 65 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20  lse{.    nBlock 
8a370 3d 20 28 6e 42 79 74 65 20 2b 20 31 31 29 2f 38  = (nByte + 11)/8
8a380 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
8a390 6e 42 6c 6f 63 6b 3e 3d 32 20 29 3b 0a 0a 20 20  nBlock>=2 );..  
8a3a0 2f 2a 20 53 54 45 50 20 31 3a 0a 20 20 2a 2a 20  /* STEP 1:.  ** 
8a3b0 4c 6f 6f 6b 20 66 6f 72 20 61 6e 20 65 6e 74 72  Look for an entr
8a3c0 79 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74  y of the correct
8a3d0 20 73 69 7a 65 20 69 6e 20 65 69 74 68 65 72 20   size in either 
8a3e0 74 68 65 20 73 6d 61 6c 6c 0a 20 20 2a 2a 20 63  the small.  ** c
8a3f0 68 75 6e 6b 20 74 61 62 6c 65 20 6f 72 20 69 6e  hunk table or in
8a400 20 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b   the large chunk
8a410 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68   hash table.  Th
8a420 69 73 20 69 73 0a 20 20 2a 2a 20 73 75 63 63 65  is is.  ** succe
8a430 73 73 66 75 6c 20 6d 6f 73 74 20 6f 66 20 74 68  ssful most of th
8a440 65 20 74 69 6d 65 20 28 61 62 6f 75 74 20 39 20  e time (about 9 
8a450 74 69 6d 65 73 20 6f 75 74 20 6f 66 20 31 30 29  times out of 10)
8a460 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 6c  ..  */.  if( nBl
8a470 6f 63 6b 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20  ock <= MX_SMALL 
8a480 29 7b 0a 20 20 20 20 69 20 3d 20 6d 65 6d 33 2e  ){.    i = mem3.
8a490 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32  aiSmall[nBlock-2
8a4a0 5d 3b 0a 20 20 20 20 69 66 28 20 69 3e 30 20 29  ];.    if( i>0 )
8a4b0 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55  {.      memsys3U
8a4c0 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c  nlinkFromList(i,
8a4d0 20 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e   &mem3.aiSmall[n
8a4e0 42 6c 6f 63 6b 2d 32 5d 29 3b 0a 20 20 20 20 20  Block-2]);.     
8a4f0 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 43   return memsys3C
8a500 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63  heckout(i, nBloc
8a510 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  k);.    }.  }els
8a520 65 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 68 20  e{.    int hash 
8a530 3d 20 6e 42 6c 6f 63 6b 20 25 20 4e 5f 48 41 53  = nBlock % N_HAS
8a540 48 3b 0a 20 20 20 20 66 6f 72 28 69 3d 6d 65 6d  H;.    for(i=mem
8a550 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 3b 20  3.aiHash[hash]; 
8a560 69 3e 30 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f 6f  i>0; i=mem3.aPoo
8a570 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74  l[i].u.list.next
8a580 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d  ){.      if( mem
8a590 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
8a5a0 64 72 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c  dr.size4x/4==nBl
8a5b0 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  ock ){.        m
8a5c0 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d  emsys3UnlinkFrom
8a5d0 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69  List(i, &mem3.ai
8a5e0 48 61 73 68 5b 68 61 73 68 5d 29 3b 0a 20 20 20  Hash[hash]);.   
8a5f0 20 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73       return mems
8a600 79 73 33 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e  ys3Checkout(i, n
8a610 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a  Block);.      }.
8a620 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
8a630 53 54 45 50 20 32 3a 0a 20 20 2a 2a 20 54 72 79  STEP 2:.  ** Try
8a640 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20   to satisfy the 
8a650 61 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20 63 61  allocation by ca
8a660 72 76 69 6e 67 20 61 20 70 69 65 63 65 20 6f 66  rving a piece of
8a670 66 20 6f 66 20 74 68 65 20 65 6e 64 0a 20 20 2a  f of the end.  *
8a680 2a 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20  * of the master 
8a690 63 68 75 6e 6b 2e 20 20 54 68 69 73 20 73 74 65  chunk.  This ste
8a6a0 70 20 75 73 75 61 6c 6c 79 20 77 6f 72 6b 73 20  p usually works 
8a6b0 69 66 20 73 74 65 70 20 31 20 66 61 69 6c 73 2e  if step 1 fails.
8a6c0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33  .  */.  if( mem3
8a6d0 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63  .szMaster>=nBloc
8a6e0 6b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  k ){.    return 
8a6f0 6d 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65  memsys3FromMaste
8a700 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a  r(nBlock);.  }..
8a710 0a 20 20 2f 2a 20 53 54 45 50 20 33 3a 20 20 0a  .  /* STEP 3:  .
8a720 20 20 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67    ** Loop throug
8a730 68 20 74 68 65 20 65 6e 74 69 72 65 20 6d 65 6d  h the entire mem
8a740 6f 72 79 20 70 6f 6f 6c 2e 20 20 43 6f 61 6c 65  ory pool.  Coale
8a750 73 63 65 20 61 64 6a 61 63 65 6e 74 20 66 72 65  sce adjacent fre
8a760 65 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e 20 20  e.  ** chunks.  
8a770 52 65 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61  Recompute the ma
8a780 73 74 65 72 20 63 68 75 6e 6b 20 61 73 20 74 68  ster chunk as th
8a790 65 20 6c 61 72 67 65 73 74 20 66 72 65 65 20 63  e largest free c
8a7a0 68 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 6e 20  hunk..  ** Then 
8a7b0 74 72 79 20 61 67 61 69 6e 20 74 6f 20 73 61 74  try again to sat
8a7c0 69 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74  isfy the allocat
8a7d0 69 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 61  ion by carving a
8a7e0 20 70 69 65 63 65 20 6f 66 66 0a 20 20 2a 2a 20   piece off.  ** 
8a7f0 6f 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  of the end of th
8a800 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20  e master chunk. 
8a810 20 54 68 69 73 20 73 74 65 70 20 68 61 70 70 65   This step happe
8a820 6e 73 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 72  ns very.  ** rar
8a830 65 6c 79 20 28 77 65 20 68 6f 70 65 21 29 0a 20  ely (we hope!). 
8a840 20 2a 2f 0a 20 20 66 6f 72 28 74 6f 46 72 65 65   */.  for(toFree
8a850 3d 6e 42 6c 6f 63 6b 2a 31 36 3b 20 74 6f 46 72  =nBlock*16; toFr
8a860 65 65 3c 28 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 31  ee<(mem3.nPool*1
8a870 36 29 3b 20 74 6f 46 72 65 65 20 2a 3d 20 32 29  6); toFree *= 2)
8a880 7b 0a 20 20 20 20 6d 65 6d 73 79 73 33 4f 75 74  {.    memsys3Out
8a890 4f 66 4d 65 6d 6f 72 79 28 74 6f 46 72 65 65 29  OfMemory(toFree)
8a8a0 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 69  ;.    if( mem3.i
8a8b0 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20  Master ){.      
8a8c0 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 6d 65 6d 33  memsys3Link(mem3
8a8d0 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20  .iMaster);.     
8a8e0 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20   mem3.iMaster = 
8a8f0 30 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a  0;.      mem3.sz
8a900 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20  Master = 0;.    
8a910 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  }.    for(i=0; i
8a920 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20  <N_HASH; i++){. 
8a930 20 20 20 20 20 6d 65 6d 73 79 73 33 4d 65 72 67       memsys3Merg
8a940 65 28 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69  e(&mem3.aiHash[i
8a950 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f  ]);.    }.    fo
8a960 72 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c  r(i=0; i<MX_SMAL
8a970 4c 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  L-1; i++){.     
8a980 20 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26 6d   memsys3Merge(&m
8a990 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 29 3b  em3.aiSmall[i]);
8a9a0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d  .    }.    if( m
8a9b0 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 29 7b 0a  em3.szMaster ){.
8a9c0 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c        memsys3Unl
8a9d0 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ink(mem3.iMaster
8a9e0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d  );.      if( mem
8a9f0 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f  3.szMaster>=nBlo
8aa00 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  ck ){.        re
8aa10 74 75 72 6e 20 6d 65 6d 73 79 73 33 46 72 6f 6d  turn memsys3From
8aa20 4d 61 73 74 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a  Master(nBlock);.
8aa30 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
8aa40 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20  }..  /* If none 
8aa50 6f 66 20 74 68 65 20 61 62 6f 76 65 20 77 6f 72  of the above wor
8aa60 6b 65 64 2c 20 74 68 65 6e 20 77 65 20 66 61 69  ked, then we fai
8aa70 6c 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30  l. */.  return 0
8aa80 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  ;.}../*.** Free 
8aa90 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d  an outstanding m
8aaa0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
8aab0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
8aac0 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68  ction assumes th
8aad0 61 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79  at the necessary
8aae0 20 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79   mutexes, if any
8aaf0 2c 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79  , are.** already
8ab00 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c   held by the cal
8ab10 6c 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61  ler. Hence "Unsa
8ab20 66 65 22 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d  fe"..*/.void mem
8ab30 73 79 73 33 46 72 65 65 55 6e 73 61 66 65 28 76  sys3FreeUnsafe(v
8ab40 6f 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 4d 65  oid *pOld){.  Me
8ab50 6d 33 42 6c 6f 63 6b 20 2a 70 20 3d 20 28 4d 65  m3Block *p = (Me
8ab60 6d 33 42 6c 6f 63 6b 2a 29 70 4f 6c 64 3b 0a 20  m3Block*)pOld;. 
8ab70 20 69 6e 74 20 69 3b 0a 20 20 75 33 32 20 73 69   int i;.  u32 si
8ab80 7a 65 2c 20 78 3b 0a 20 20 61 73 73 65 72 74 28  ze, x;.  assert(
8ab90 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
8aba0 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20  eld(mem3.mutex) 
8abb0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 3e 6d  );.  assert( p>m
8abc0 65 6d 33 2e 61 50 6f 6f 6c 20 26 26 20 70 3c 26  em3.aPool && p<&
8abd0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e  mem3.aPool[mem3.
8abe0 6e 50 6f 6f 6c 5d 20 29 3b 0a 20 20 69 20 3d 20  nPool] );.  i = 
8abf0 70 20 2d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 3b 0a  p - mem3.aPool;.
8ac00 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e    assert( (mem3.
8ac10 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72  aPool[i-1].u.hdr
8ac20 2e 73 69 7a 65 34 78 26 31 29 3d 3d 31 20 29 3b  .size4x&1)==1 );
8ac30 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61  .  size = mem3.a
8ac40 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
8ac50 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65  size4x/4;.  asse
8ac60 72 74 28 20 69 2b 73 69 7a 65 3c 3d 6d 65 6d 33  rt( i+size<=mem3
8ac70 2e 6e 50 6f 6f 6c 2b 31 20 29 3b 0a 20 20 6d 65  .nPool+1 );.  me
8ac80 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e  m3.aPool[i-1].u.
8ac90 68 64 72 2e 73 69 7a 65 34 78 20 26 3d 20 7e 31  hdr.size4x &= ~1
8aca0 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  ;.  mem3.aPool[i
8acb0 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70  +size-1].u.hdr.p
8acc0 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a  revSize = size;.
8acd0 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73    mem3.aPool[i+s
8ace0 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  ize-1].u.hdr.siz
8acf0 65 34 78 20 26 3d 20 7e 32 3b 0a 20 20 6d 65 6d  e4x &= ~2;.  mem
8ad00 73 79 73 33 4c 69 6e 6b 28 69 29 3b 0a 0a 20 20  sys3Link(i);..  
8ad10 2f 2a 20 54 72 79 20 74 6f 20 65 78 70 61 6e 64  /* Try to expand
8ad20 20 74 68 65 20 6d 61 73 74 65 72 20 75 73 69 6e   the master usin
8ad30 67 20 74 68 65 20 6e 65 77 6c 79 20 66 72 65 65  g the newly free
8ad40 64 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 66 28  d chunk */.  if(
8ad50 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 29 7b   mem3.iMaster ){
8ad60 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 6d  .    while( (mem
8ad70 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61  3.aPool[mem3.iMa
8ad80 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ster-1].u.hdr.si
8ad90 7a 65 34 78 26 32 29 3d 3d 30 20 29 7b 0a 20 20  ze4x&2)==0 ){.  
8ada0 20 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e      size = mem3.
8adb0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74  aPool[mem3.iMast
8adc0 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76  er-1].u.hdr.prev
8add0 53 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 33  Size;.      mem3
8ade0 2e 69 4d 61 73 74 65 72 20 2d 3d 20 73 69 7a 65  .iMaster -= size
8adf0 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d  ;.      mem3.szM
8ae00 61 73 74 65 72 20 2b 3d 20 73 69 7a 65 3b 0a 20  aster += size;. 
8ae10 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69       memsys3Unli
8ae20 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29  nk(mem3.iMaster)
8ae30 3b 0a 20 20 20 20 20 20 78 20 3d 20 6d 65 6d 33  ;.      x = mem3
8ae40 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73  .aPool[mem3.iMas
8ae50 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  ter-1].u.hdr.siz
8ae60 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d  e4x & 2;.      m
8ae70 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69  em3.aPool[mem3.i
8ae80 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e  Master-1].u.hdr.
8ae90 73 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a  size4x = mem3.sz
8aea0 4d 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20  Master*4 | x;.  
8aeb0 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d      mem3.aPool[m
8aec0 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33  em3.iMaster+mem3
8aed0 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68  .szMaster-1].u.h
8aee0 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65  dr.prevSize = me
8aef0 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20  m3.szMaster;.   
8af00 20 7d 0a 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e   }.    x = mem3.
8af10 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74  aPool[mem3.iMast
8af20 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  er-1].u.hdr.size
8af30 34 78 20 26 20 32 3b 0a 20 20 20 20 77 68 69 6c  4x & 2;.    whil
8af40 65 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d  e( (mem3.aPool[m
8af50 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33  em3.iMaster+mem3
8af60 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68  .szMaster-1].u.h
8af70 64 72 2e 73 69 7a 65 34 78 26 31 29 3d 3d 30 20  dr.size4x&1)==0 
8af80 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33  ){.      memsys3
8af90 55 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73  Unlink(mem3.iMas
8afa0 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  ter+mem3.szMaste
8afb0 72 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73  r);.      mem3.s
8afc0 7a 4d 61 73 74 65 72 20 2b 3d 20 6d 65 6d 33 2e  zMaster += mem3.
8afd0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74  aPool[mem3.iMast
8afe0 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72  er+mem3.szMaster
8aff0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
8b000 2f 34 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61  /4;.      mem3.a
8b010 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
8b020 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  r-1].u.hdr.size4
8b030 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  x = mem3.szMaste
8b040 72 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d  r*4 | x;.      m
8b050 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69  em3.aPool[mem3.i
8b060 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61  Master+mem3.szMa
8b070 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72  ster-1].u.hdr.pr
8b080 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a  evSize = mem3.sz
8b090 4d 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20  Master;.    }.  
8b0a0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  }.}../*.** Retur
8b0b0 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e  n the size of an
8b0c0 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c   outstanding all
8b0d0 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65  ocation, in byte
8b0e0 73 2e 20 20 54 68 65 0a 2a 2a 20 73 69 7a 65 20  s.  The.** size 
8b0f0 72 65 74 75 72 6e 65 64 20 6f 6d 69 74 73 20 74  returned omits t
8b100 68 65 20 38 2d 62 79 74 65 20 68 65 61 64 65 72  he 8-byte header
8b110 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68 69 73   overhead.  This
8b120 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66   only.** works f
8b130 6f 72 20 63 68 75 6e 6b 73 20 74 68 61 74 20 61  or chunks that a
8b140 72 65 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65  re currently che
8b150 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61  cked out..*/.sta
8b160 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 53  tic int memsys3S
8b170 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20  ize(void *p){.  
8b180 4d 65 6d 33 42 6c 6f 63 6b 20 2a 70 42 6c 6f 63  Mem3Block *pBloc
8b190 6b 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  k;.  if( p==0 ) 
8b1a0 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 42 6c 6f  return 0;.  pBlo
8b1b0 63 6b 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a  ck = (Mem3Block*
8b1c0 29 70 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70  )p;.  assert( (p
8b1d0 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e  Block[-1].u.hdr.
8b1e0 73 69 7a 65 34 78 26 31 29 21 3d 30 20 29 3b 0a  size4x&1)!=0 );.
8b1f0 20 20 72 65 74 75 72 6e 20 28 70 42 6c 6f 63 6b    return (pBlock
8b200 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  [-1].u.hdr.size4
8b210 78 26 7e 33 29 2a 32 20 2d 20 34 3b 0a 7d 0a 0a  x&~3)*2 - 4;.}..
8b220 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61  /*.** Round up a
8b230 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f   request size to
8b240 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20   the next valid 
8b250 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e  allocation size.
8b260 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
8b270 65 6d 73 79 73 33 52 6f 75 6e 64 75 70 28 69 6e  emsys3Roundup(in
8b280 74 20 6e 29 7b 0a 20 20 69 66 28 20 6e 3c 3d 31  t n){.  if( n<=1
8b290 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  2 ){.    return 
8b2a0 31 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  12;.  }else{.   
8b2b0 20 72 65 74 75 72 6e 20 28 28 6e 2b 31 31 29 26   return ((n+11)&
8b2c0 7e 37 29 20 2d 20 34 3b 0a 20 20 7d 0a 7d 0a 0a  ~7) - 4;.  }.}..
8b2d0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e  /*.** Allocate n
8b2e0 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e  Bytes of memory.
8b2f0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
8b300 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 28 69  *memsys3Malloc(i
8b310 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71  nt nBytes){.  sq
8b320 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a  lite3_int64 *p;.
8b330 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 73    assert( nBytes
8b340 3e 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 2f  >0 );          /
8b350 2a 20 6d 61 6c 6c 6f 63 2e 63 20 66 69 6c 74 65  * malloc.c filte
8b360 72 73 20 6f 75 74 20 30 20 62 79 74 65 20 72 65  rs out 0 byte re
8b370 71 75 65 73 74 73 20 2a 2f 0a 20 20 6d 65 6d 73  quests */.  mems
8b380 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20  ys3Enter();.  p 
8b390 3d 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55  = memsys3MallocU
8b3a0 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20  nsafe(nBytes);. 
8b3b0 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b   memsys3Leave();
8b3c0 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a  .  return (void*
8b3d0 29 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72  )p; .}../*.** Fr
8b3e0 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 76 6f  ee memory..*/.vo
8b3f0 69 64 20 6d 65 6d 73 79 73 33 46 72 65 65 28 76  id memsys3Free(v
8b400 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20  oid *pPrior){.  
8b410 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 20 29  assert( pPrior )
8b420 3b 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 72  ;.  memsys3Enter
8b430 28 29 3b 0a 20 20 6d 65 6d 73 79 73 33 46 72 65  ();.  memsys3Fre
8b440 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b  eUnsafe(pPrior);
8b450 0a 20 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28  .  memsys3Leave(
8b460 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  );.}../*.** Chan
8b470 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61  ge the size of a
8b480 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72  n existing memor
8b490 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a  y allocation.*/.
8b4a0 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 52 65 61  void *memsys3Rea
8b4b0 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f  lloc(void *pPrio
8b4c0 72 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a  r, int nBytes){.
8b4d0 20 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f    int nOld;.  vo
8b4e0 69 64 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 72  id *p;.  if( pPr
8b4f0 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ior==0 ){.    re
8b500 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c  turn sqlite3_mal
8b510 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d  loc(nBytes);.  }
8b520 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30  .  if( nBytes<=0
8b530 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
8b540 66 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20  free(pPrior);.  
8b550 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
8b560 20 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 33    nOld = memsys3
8b570 53 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20 20  Size(pPrior);.  
8b580 69 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64  if( nBytes<=nOld
8b590 20 26 26 20 6e 42 79 74 65 73 3e 3d 6e 4f 6c 64   && nBytes>=nOld
8b5a0 2d 31 32 38 20 29 7b 0a 20 20 20 20 72 65 74 75  -128 ){.    retu
8b5b0 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20  rn pPrior;.  }. 
8b5c0 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b   memsys3Enter();
8b5d0 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 33 4d 61  .  p = memsys3Ma
8b5e0 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65  llocUnsafe(nByte
8b5f0 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  s);.  if( p ){. 
8b600 20 20 20 69 66 28 20 6e 4f 6c 64 3c 6e 42 79 74     if( nOld<nByt
8b610 65 73 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63  es ){.      memc
8b620 70 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 4f  py(p, pPrior, nO
8b630 6c 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ld);.    }else{.
8b640 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20        memcpy(p, 
8b650 70 50 72 69 6f 72 2c 20 6e 42 79 74 65 73 29 3b  pPrior, nBytes);
8b660 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 79  .    }.    memsy
8b670 73 33 46 72 65 65 55 6e 73 61 66 65 28 70 50 72  s3FreeUnsafe(pPr
8b680 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  ior);.  }.  mems
8b690 79 73 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65  ys3Leave();.  re
8b6a0 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
8b6b0 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73   Initialize this
8b6c0 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74   module..*/.stat
8b6d0 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 49 6e  ic int memsys3In
8b6e0 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64  it(void *NotUsed
8b6f0 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
8b700 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
8b710 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c    if( !sqlite3Gl
8b720 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70  obalConfig.pHeap
8b730 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
8b740 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
8b750 0a 0a 20 20 2f 2a 20 53 74 6f 72 65 20 61 20 70  ..  /* Store a p
8b760 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65  ointer to the me
8b770 6d 6f 72 79 20 62 6c 6f 63 6b 20 69 6e 20 67 6c  mory block in gl
8b780 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 20 6d  obal structure m
8b790 65 6d 33 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  em3. */.  assert
8b7a0 28 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f  ( sizeof(Mem3Blo
8b7b0 63 6b 29 3d 3d 38 20 29 3b 0a 20 20 6d 65 6d 33  ck)==8 );.  mem3
8b7c0 2e 61 50 6f 6f 6c 20 3d 20 28 4d 65 6d 33 42 6c  .aPool = (Mem3Bl
8b7d0 6f 63 6b 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f  ock *)sqlite3Glo
8b7e0 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b  balConfig.pHeap;
8b7f0 0a 20 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 20 3d 20  .  mem3.nPool = 
8b800 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  (sqlite3GlobalCo
8b810 6e 66 69 67 2e 6e 48 65 61 70 20 2f 20 73 69 7a  nfig.nHeap / siz
8b820 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 29 20  eof(Mem3Block)) 
8b830 2d 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69  - 2;..  /* Initi
8b840 61 6c 69 7a 65 20 74 68 65 20 6d 61 73 74 65 72  alize the master
8b850 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6d 65 6d   block. */.  mem
8b860 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 6d 65 6d  3.szMaster = mem
8b870 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e  3.nPool;.  mem3.
8b880 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e  mnMaster = mem3.
8b890 73 7a 4d 61 73 74 65 72 3b 0a 20 20 6d 65 6d 33  szMaster;.  mem3
8b8a0 2e 69 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20  .iMaster = 1;.  
8b8b0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 30 5d 2e 75 2e  mem3.aPool[0].u.
8b8c0 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 28 6d 65  hdr.size4x = (me
8b8d0 6d 33 2e 73 7a 4d 61 73 74 65 72 3c 3c 32 29 20  m3.szMaster<<2) 
8b8e0 2b 20 32 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f  + 2;.  mem3.aPoo
8b8f0 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e  l[mem3.nPool].u.
8b900 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d  hdr.prevSize = m
8b910 65 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d  em3.nPool;.  mem
8b920 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f  3.aPool[mem3.nPo
8b930 6f 6c 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  ol].u.hdr.size4x
8b940 20 3d 20 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20   = 1;..  return 
8b950 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
8b960 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65  .** Deinitialize
8b970 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f   this module..*/
8b980 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
8b990 73 79 73 33 53 68 75 74 64 6f 77 6e 28 76 6f 69  sys3Shutdown(voi
8b9a0 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55  d *NotUsed){.  U
8b9b0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
8b9c0 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 33  NotUsed);.  mem3
8b9d0 2e 6d 75 74 65 78 20 3d 20 30 3b 0a 20 20 72 65  .mutex = 0;.  re
8b9e0 74 75 72 6e 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a  turn;.}..../*.**
8b9f0 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69   Open the file i
8ba00 6e 64 69 63 61 74 65 64 20 61 6e 64 20 77 72 69  ndicated and wri
8ba10 74 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20  te a log of all 
8ba20 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a  unfreed memory .
8ba30 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69  ** allocations i
8ba40 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f  nto that log..*/
8ba50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
8ba60 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 73  void sqlite3Mems
8ba70 79 73 33 44 75 6d 70 28 63 6f 6e 73 74 20 63 68  ys3Dump(const ch
8ba80 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a  ar *zFilename){.
8ba90 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
8baa0 42 55 47 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b  BUG.  FILE *out;
8bab0 0a 20 20 75 33 32 20 69 2c 20 6a 3b 0a 20 20 75  .  u32 i, j;.  u
8bac0 33 32 20 73 69 7a 65 3b 0a 20 20 69 66 28 20 7a  32 size;.  if( z
8bad0 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a  Filename==0 || z
8bae0 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29  Filename[0]==0 )
8baf0 7b 0a 20 20 20 20 6f 75 74 20 3d 20 73 74 64 6f  {.    out = stdo
8bb00 75 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ut;.  }else{.   
8bb10 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69   out = fopen(zFi
8bb20 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20  lename, "w");.  
8bb30 20 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a    if( out==0 ){.
8bb40 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74        fprintf(st
8bb50 64 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65  derr, "** Unable
8bb60 20 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72   to output memor
8bb70 79 20 64 65 62 75 67 20 6f 75 74 70 75 74 20 6c  y debug output l
8bb80 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20  og: %s **\n",.  
8bb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8bba0 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a      zFilename);.
8bbb0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
8bbc0 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73    }.  }.  memsys
8bbd0 33 45 6e 74 65 72 28 29 3b 0a 20 20 66 70 72 69  3Enter();.  fpri
8bbe0 6e 74 66 28 6f 75 74 2c 20 22 43 48 55 4e 4b 53  ntf(out, "CHUNKS
8bbf0 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 31  :\n");.  for(i=1
8bc00 3b 20 69 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b  ; i<=mem3.nPool;
8bc10 20 69 2b 3d 73 69 7a 65 2f 34 29 7b 0a 20 20 20   i+=size/4){.   
8bc20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f   size = mem3.aPo
8bc30 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ol[i-1].u.hdr.si
8bc40 7a 65 34 78 3b 0a 20 20 20 20 69 66 28 20 73 69  ze4x;.    if( si
8bc50 7a 65 2f 34 3c 3d 31 20 29 7b 0a 20 20 20 20 20  ze/4<=1 ){.     
8bc60 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25   fprintf(out, "%
8bc70 70 20 73 69 7a 65 20 65 72 72 6f 72 5c 6e 22 2c  p size error\n",
8bc80 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29   &mem3.aPool[i])
8bc90 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
8bca0 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  0 );.      break
8bcb0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
8bcc0 28 73 69 7a 65 26 31 29 3d 3d 30 20 26 26 20 6d  (size&1)==0 && m
8bcd0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65  em3.aPool[i+size
8bce0 2f 34 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76  /4-1].u.hdr.prev
8bcf0 53 69 7a 65 21 3d 73 69 7a 65 2f 34 20 29 7b 0a  Size!=size/4 ){.
8bd00 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75        fprintf(ou
8bd10 74 2c 20 22 25 70 20 74 61 69 6c 20 73 69 7a 65  t, "%p tail size
8bd20 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 5c   does not match\
8bd30 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  n", &mem3.aPool[
8bd40 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  i]);.      asser
8bd50 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 62 72  t( 0 );.      br
8bd60 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  eak;.    }.    i
8bd70 66 28 20 28 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  f( ((mem3.aPool[
8bd80 69 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 64  i+size/4-1].u.hd
8bd90 72 2e 73 69 7a 65 34 78 26 32 29 3e 3e 31 29 21  r.size4x&2)>>1)!
8bda0 3d 28 73 69 7a 65 26 31 29 20 29 7b 0a 20 20 20  =(size&1) ){.   
8bdb0 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
8bdc0 22 25 70 20 74 61 69 6c 20 63 68 65 63 6b 6f 75  "%p tail checkou
8bdd0 74 20 62 69 74 20 69 73 20 69 6e 63 6f 72 72 65  t bit is incorre
8bde0 63 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f  ct\n", &mem3.aPo
8bdf0 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 73  ol[i]);.      as
8be00 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20 20  sert( 0 );.     
8be10 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
8be20 20 20 69 66 28 20 73 69 7a 65 26 31 20 29 7b 0a    if( size&1 ){.
8be30 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75        fprintf(ou
8be40 74 2c 20 22 25 70 20 25 36 64 20 62 79 74 65 73  t, "%p %6d bytes
8be50 20 63 68 65 63 6b 65 64 20 6f 75 74 5c 6e 22 2c   checked out\n",
8be60 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c   &mem3.aPool[i],
8be70 20 28 73 69 7a 65 2f 34 29 2a 38 2d 38 29 3b 0a   (size/4)*8-8);.
8be80 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
8be90 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25   fprintf(out, "%
8bea0 70 20 25 36 64 20 62 79 74 65 73 20 66 72 65 65  p %6d bytes free
8beb0 25 73 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f  %s\n", &mem3.aPo
8bec0 6f 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29 2a  ol[i], (size/4)*
8bed0 38 2d 38 2c 0a 20 20 20 20 20 20 20 20 20 20 20  8-8,.           
8bee0 20 20 20 20 20 20 20 69 3d 3d 6d 65 6d 33 2e 69         i==mem3.i
8bef0 4d 61 73 74 65 72 20 3f 20 22 20 2a 2a 6d 61 73  Master ? " **mas
8bf00 74 65 72 2a 2a 22 20 3a 20 22 22 29 3b 0a 20 20  ter**" : "");.  
8bf10 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d    }.  }.  for(i=
8bf20 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b  0; i<MX_SMALL-1;
8bf30 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d   i++){.    if( m
8bf40 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3d 3d  em3.aiSmall[i]==
8bf50 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
8bf60 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
8bf70 73 6d 61 6c 6c 28 25 32 64 29 3a 22 2c 20 69 29  small(%2d):", i)
8bf80 3b 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20 6d 65  ;.    for(j = me
8bf90 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3b 20 6a  m3.aiSmall[i]; j
8bfa0 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c  >0; j=mem3.aPool
8bfb0 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29  [j].u.list.next)
8bfc0 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  {.      fprintf(
8bfd0 6f 75 74 2c 20 22 20 25 70 28 25 64 29 22 2c 20  out, " %p(%d)", 
8bfe0 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a  &mem3.aPool[j],.
8bff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6d                (m
8c000 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75  em3.aPool[j-1].u
8c010 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38  .hdr.size4x/4)*8
8c020 2d 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  -8);.    }.    f
8c030 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22  printf(out, "\n"
8c040 29 3b 20 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  ); .  }.  for(i=
8c050 30 3b 20 69 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b  0; i<N_HASH; i++
8c060 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e  ){.    if( mem3.
8c070 61 69 48 61 73 68 5b 69 5d 3d 3d 30 20 29 20 63  aiHash[i]==0 ) c
8c080 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 70 72  ontinue;.    fpr
8c090 69 6e 74 66 28 6f 75 74 2c 20 22 68 61 73 68 28  intf(out, "hash(
8c0a0 25 32 64 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20  %2d):", i);.    
8c0b0 66 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 48  for(j = mem3.aiH
8c0c0 61 73 68 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d  ash[i]; j>0; j=m
8c0d0 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c  em3.aPool[j].u.l
8c0e0 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20  ist.next){.     
8c0f0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20   fprintf(out, " 
8c100 25 70 28 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61  %p(%d)", &mem3.a
8c110 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20  Pool[j],.       
8c120 20 20 20 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f         (mem3.aPo
8c130 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ol[j-1].u.hdr.si
8c140 7a 65 34 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20  ze4x/4)*8-8);.  
8c150 20 20 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28    }.    fprintf(
8c160 6f 75 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d  out, "\n"); .  }
8c170 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20  .  fprintf(out, 
8c180 22 6d 61 73 74 65 72 3d 25 64 5c 6e 22 2c 20 6d  "master=%d\n", m
8c190 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20  em3.iMaster);.  
8c1a0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6e 6f  fprintf(out, "no
8c1b0 77 55 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d  wUsed=%d\n", mem
8c1c0 33 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33  3.nPool*8 - mem3
8c1d0 2e 73 7a 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20  .szMaster*8);.  
8c1e0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 78  fprintf(out, "mx
8c1f0 55 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33  Used=%d\n", mem3
8c200 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e  .nPool*8 - mem3.
8c210 6d 6e 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 73  mnMaster*8);.  s
8c220 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
8c230 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a  ve(mem3.mutex);.
8c240 20 20 69 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75    if( out==stdou
8c250 74 20 29 7b 0a 20 20 20 20 66 66 6c 75 73 68 28  t ){.    fflush(
8c260 73 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65  stdout);.  }else
8c270 7b 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 74  {.    fclose(out
8c280 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55  );.  }.#else.  U
8c290 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
8c2a0 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64  zFilename);.#end
8c2b0 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  if.}../*.** This
8c2c0 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20   routine is the 
8c2d0 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20  only routine in 
8c2e0 74 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65  this file with e
8c2f0 78 74 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b  xternal .** link
8c300 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c  age..**.** Popul
8c310 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65  ate the low-leve
8c320 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  l memory allocat
8c330 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69  ion function poi
8c340 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69  nters in.** sqli
8c350 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
8c360 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20  m with pointers 
8c370 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20  to the routines 
8c380 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 20 54 68  in this file. Th
8c390 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 73  e.** arguments s
8c3a0 70 65 63 69 66 79 20 74 68 65 20 62 6c 6f 63 6b  pecify the block
8c3b0 20 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 6d 61   of memory to ma
8c3c0 6e 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  nage..**.** This
8c3d0 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79   routine is only
8c3e0 20 63 61 6c 6c 65 64 20 62 79 20 73 71 6c 69 74   called by sqlit
8c3f0 65 33 5f 63 6f 6e 66 69 67 28 29 2c 20 61 6e 64  e3_config(), and
8c400 20 74 68 65 72 65 66 6f 72 65 0a 2a 2a 20 69 73   therefore.** is
8c410 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f   not required to
8c420 20 62 65 20 74 68 72 65 61 64 73 61 66 65 20 28   be threadsafe (
8c430 69 74 20 69 73 20 6e 6f 74 29 2e 0a 2a 2f 0a 53  it is not)..*/.S
8c440 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
8c450 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  nst sqlite3_mem_
8c460 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33  methods *sqlite3
8c470 4d 65 6d 47 65 74 4d 65 6d 73 79 73 33 28 76 6f  MemGetMemsys3(vo
8c480 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f  id){.  static co
8c490 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  nst sqlite3_mem_
8c4a0 6d 65 74 68 6f 64 73 20 6d 65 6d 70 6f 6f 6c 4d  methods mempoolM
8c4b0 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20  ethods = {.     
8c4c0 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 2c 0a 20  memsys3Malloc,. 
8c4d0 20 20 20 20 6d 65 6d 73 79 73 33 46 72 65 65 2c      memsys3Free,
8c4e0 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 52 65 61  .     memsys3Rea
8c4f0 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79  lloc,.     memsy
8c500 73 33 53 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d  s3Size,.     mem
8c510 73 79 73 33 52 6f 75 6e 64 75 70 2c 0a 20 20 20  sys3Roundup,.   
8c520 20 20 6d 65 6d 73 79 73 33 49 6e 69 74 2c 0a 20    memsys3Init,. 
8c530 20 20 20 20 6d 65 6d 73 79 73 33 53 68 75 74 64      memsys3Shutd
8c540 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b  own,.     0.  };
8c550 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 70 6f  .  return &mempo
8c560 6f 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65  olMethods;.}..#e
8c570 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45  ndif /* SQLITE_E
8c580 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 2a 2f  NABLE_MEMSYS3 */
8c590 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
8c5a0 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 33 2e 63 20  * End of mem3.c 
8c5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c5e0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
8c5f0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d  * Begin file mem
8c600 35 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  5.c ************
8c610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c630 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4f 63 74  /./*.** 2007 Oct
8c640 6f 62 65 72 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68  ober 14.**.** Th
8c650 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
8c660 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
8c670 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
8c680 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
8c690 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
8c6a0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
8c6b0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
8c6c0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
8c6d0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
8c6e0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
8c6f0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
8c700 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
8c710 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
8c720 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
8c730 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
8c740 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
8c750 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
8c760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c7a0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
8c7b0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ile contains the
8c7c0 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61   C functions tha
8c7d0 74 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 6d 65  t implement a me
8c7e0 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69  mory.** allocati
8c7f0 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 66 6f 72  on subsystem for
8c800 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 2e 20   use by SQLite. 
8c810 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73  .**.** This vers
8c820 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  ion of the memor
8c830 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
8c840 73 79 73 74 65 6d 20 6f 6d 69 74 73 20 61 6c 6c  system omits all
8c850 0a 2a 2a 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f  .** use of mallo
8c860 63 28 29 2e 20 54 68 65 20 61 70 70 6c 69 63 61  c(). The applica
8c870 74 69 6f 6e 20 67 69 76 65 73 20 53 51 4c 69 74  tion gives SQLit
8c880 65 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d  e a block of mem
8c890 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61  ory.** before ca
8c8a0 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e  lling sqlite3_in
8c8b0 69 74 69 61 6c 69 7a 65 28 29 20 66 72 6f 6d 20  itialize() from 
8c8c0 77 68 69 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e  which allocation
8c8d0 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 61 6e  s.** are made an
8c8e0 64 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  d returned by th
8c8f0 65 20 78 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20  e xMalloc() and 
8c900 78 52 65 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 69  xRealloc() .** i
8c910 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 20  mplementations. 
8c920 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f 69 6e 69  Once sqlite3_ini
8c930 74 69 61 6c 69 7a 65 28 29 20 68 61 73 20 62 65  tialize() has be
8c940 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 68  en called,.** th
8c950 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f  e amount of memo
8c960 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20  ry available to 
8c970 53 51 4c 69 74 65 20 69 73 20 66 69 78 65 64 20  SQLite is fixed 
8c980 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65  and cannot.** be
8c990 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20   changed..**.** 
8c9a0 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  This version of 
8c9b0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
8c9c0 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20  ation subsystem 
8c9d0 69 73 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 69  is included.** i
8c9e0 6e 20 74 68 65 20 62 75 69 6c 64 20 6f 6e 6c 79  n the build only
8c9f0 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   if SQLITE_ENABL
8ca00 45 5f 4d 45 4d 53 59 53 35 20 69 73 20 64 65 66  E_MEMSYS5 is def
8ca10 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ined..**.** This
8ca20 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
8ca30 72 20 75 73 65 73 20 74 68 65 20 66 6f 6c 6c 6f  r uses the follo
8ca40 77 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 3a 0a  wing algorithm:.
8ca50 2a 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 6c 6c 20  **.**   1.  All 
8ca60 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
8ca70 6e 73 20 73 69 7a 65 73 20 61 72 65 20 72 6f 75  ns sizes are rou
8ca80 6e 64 65 64 20 75 70 20 74 6f 20 61 20 70 6f 77  nded up to a pow
8ca90 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 20  er of 2..**.**  
8caa0 20 32 2e 20 20 49 66 20 74 77 6f 20 61 64 6a 61   2.  If two adja
8cab0 63 65 6e 74 20 66 72 65 65 20 62 6c 6f 63 6b 73  cent free blocks
8cac0 20 61 72 65 20 74 68 65 20 68 61 6c 76 65 73 20   are the halves 
8cad0 6f 66 20 61 20 6c 61 72 67 65 72 20 62 6c 6f 63  of a larger bloc
8cae0 6b 2c 0a 2a 2a 20 20 20 20 20 20 20 74 68 65 6e  k,.**       then
8caf0 20 74 68 65 20 74 77 6f 20 62 6c 6f 63 6b 73 20   the two blocks 
8cb00 61 72 65 20 63 6f 61 6c 65 73 65 64 20 69 6e 74  are coalesed int
8cb10 6f 20 74 68 65 20 73 69 6e 67 6c 65 20 6c 61 72  o the single lar
8cb20 67 65 72 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a  ger block..**.**
8cb30 20 20 20 33 2e 20 20 4e 65 77 20 6d 65 6d 6f 72     3.  New memor
8cb40 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  y is allocated f
8cb50 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 61 76  rom the first av
8cb60 61 69 6c 61 62 6c 65 20 66 72 65 65 20 62 6c 6f  ailable free blo
8cb70 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 61  ck..**.** This a
8cb80 6c 67 6f 72 69 74 68 6d 20 69 73 20 64 65 73 63  lgorithm is desc
8cb90 72 69 62 65 64 20 69 6e 3a 20 4a 2e 20 4d 2e 20  ribed in: J. M. 
8cba0 52 6f 62 73 6f 6e 2e 20 22 42 6f 75 6e 64 73 20  Robson. "Bounds 
8cbb0 66 6f 72 20 53 6f 6d 65 20 46 75 6e 63 74 69 6f  for Some Functio
8cbc0 6e 73 0a 2a 2a 20 43 6f 6e 63 65 72 6e 69 6e 67  ns.** Concerning
8cbd0 20 44 79 6e 61 6d 69 63 20 53 74 6f 72 61 67 65   Dynamic Storage
8cbe0 20 41 6c 6c 6f 63 61 74 69 6f 6e 22 2e 20 4a 6f   Allocation". Jo
8cbf0 75 72 6e 61 6c 20 6f 66 20 74 68 65 20 41 73 73  urnal of the Ass
8cc00 6f 63 69 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20  ociation for.** 
8cc10 43 6f 6d 70 75 74 69 6e 67 20 4d 61 63 68 69 6e  Computing Machin
8cc20 65 72 79 2c 20 56 6f 6c 75 6d 65 20 32 31 2c 20  ery, Volume 21, 
8cc30 4e 75 6d 62 65 72 20 38 2c 20 4a 75 6c 79 20 31  Number 8, July 1
8cc40 39 37 34 2c 20 70 61 67 65 73 20 34 39 31 2d 34  974, pages 491-4
8cc50 39 39 2e 0a 2a 2a 20 0a 2a 2a 20 4c 65 74 20 6e  99..** .** Let n
8cc60 20 62 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20   be the size of 
8cc70 74 68 65 20 6c 61 72 67 65 73 74 20 61 6c 6c 6f  the largest allo
8cc80 63 61 74 69 6f 6e 20 64 69 76 69 64 65 64 20 62  cation divided b
8cc90 79 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 2a 2a  y the minimum.**
8cca0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65   allocation size
8ccb0 20 28 61 66 74 65 72 20 72 6f 75 6e 64 69 6e 67   (after rounding
8ccc0 20 61 6c 6c 20 73 69 7a 65 73 20 75 70 20 74 6f   all sizes up to
8ccd0 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 29 20   a power of 2.) 
8cce0 20 4c 65 74 20 4d 0a 2a 2a 20 62 65 20 74 68 65   Let M.** be the
8ccf0 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20   maximum amount 
8cd00 6f 66 20 6d 65 6d 6f 72 79 20 65 76 65 72 20 6f  of memory ever o
8cd10 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e  utstanding at on
8cd20 65 20 74 69 6d 65 2e 20 20 4c 65 74 0a 2a 2a 20  e time.  Let.** 
8cd30 4e 20 62 65 20 74 68 65 20 74 6f 74 61 6c 20 61  N be the total a
8cd40 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20  mount of memory 
8cd50 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c  available for al
8cd60 6c 6f 63 61 74 69 6f 6e 2e 20 20 52 6f 62 73 6f  location.  Robso
8cd70 6e 0a 2a 2a 20 70 72 6f 76 65 64 20 74 68 61 74  n.** proved that
8cd80 20 74 68 69 73 20 6d 65 6d 6f 72 79 20 61 6c 6c   this memory all
8cd90 6f 63 61 74 6f 72 20 77 69 6c 6c 20 6e 65 76 65  ocator will neve
8cda0 72 20 62 72 65 61 6b 64 6f 77 6e 20 64 75 65 20  r breakdown due 
8cdb0 74 6f 20 0a 2a 2a 20 66 72 61 67 6d 65 6e 74 61  to .** fragmenta
8cdc0 74 69 6f 6e 20 61 73 20 6c 6f 6e 67 20 61 73 20  tion as long as 
8cdd0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  the following co
8cde0 6e 73 74 72 61 69 6e 74 20 68 6f 6c 64 73 3a 0a  nstraint holds:.
8cdf0 2a 2a 0a 2a 2a 20 20 20 20 20 20 4e 20 3e 3d 20  **.**      N >= 
8ce00 20 4d 2a 28 31 20 2b 20 6c 6f 67 32 28 6e 29 2f   M*(1 + log2(n)/
8ce10 32 29 20 2d 20 6e 20 2b 20 31 0a 2a 2a 0a 2a 2a  2) - n + 1.**.**
8ce20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 74 61   The sqlite3_sta
8ce30 74 75 73 28 29 20 6c 6f 67 69 63 20 74 72 61 63  tus() logic trac
8ce40 6b 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76  ks the maximum v
8ce50 61 6c 75 65 73 20 6f 66 20 6e 20 61 6e 64 20 4d  alues of n and M
8ce60 20 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6e 20 61   so.** that an a
8ce70 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 2c 20  pplication can, 
8ce80 61 74 20 61 6e 79 20 74 69 6d 65 2c 20 76 65 72  at any time, ver
8ce90 69 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61  ify this constra
8cea0 69 6e 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  int..*/../*.** T
8ceb0 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  his version of t
8cec0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
8ced0 74 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79  tor is used only
8cee0 20 77 68 65 6e 20 0a 2a 2a 20 53 51 4c 49 54 45   when .** SQLITE
8cef0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20  _ENABLE_MEMSYS5 
8cf00 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23  is defined..*/.#
8cf10 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
8cf20 42 4c 45 5f 4d 45 4d 53 59 53 35 0a 0a 2f 2a 0a  BLE_MEMSYS5../*.
8cf30 2a 2a 20 41 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c  ** A minimum all
8cf40 6f 63 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e  ocation is an in
8cf50 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
8cf60 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
8cf70 65 2e 0a 2a 2a 20 4c 61 72 67 65 72 20 61 6c 6c  e..** Larger all
8cf80 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 61 6e 20  ocations are an 
8cf90 61 72 72 61 79 20 6f 66 20 74 68 65 73 65 20 73  array of these s
8cfa0 74 72 75 63 74 75 72 65 73 20 77 68 65 72 65 20  tructures where 
8cfb0 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74  the.** size of t
8cfc0 68 65 20 61 72 72 61 79 20 69 73 20 61 20 70 6f  he array is a po
8cfd0 77 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20  wer of 2..**.** 
8cfe0 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73  The size of this
8cff0 20 6f 62 6a 65 63 74 20 6d 75 73 74 20 62 65 20   object must be 
8d000 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20  a power of two. 
8d010 20 54 68 61 74 20 66 61 63 74 20 69 73 0a 2a 2a   That fact is.**
8d020 20 76 65 72 69 66 69 65 64 20 69 6e 20 6d 65 6d   verified in mem
8d030 73 79 73 35 49 6e 69 74 28 29 2e 0a 2a 2f 0a 74  sys5Init()..*/.t
8d040 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65  ypedef struct Me
8d050 6d 35 4c 69 6e 6b 20 4d 65 6d 35 4c 69 6e 6b 3b  m5Link Mem5Link;
8d060 0a 73 74 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b  .struct Mem5Link
8d070 20 7b 0a 20 20 69 6e 74 20 6e 65 78 74 3b 20 20   {.  int next;  
8d080 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
8d090 20 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b   next free chunk
8d0a0 20 2a 2f 0a 20 20 69 6e 74 20 70 72 65 76 3b 20   */.  int prev; 
8d0b0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
8d0c0 66 20 70 72 65 76 69 6f 75 73 20 66 72 65 65 20  f previous free 
8d0d0 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  chunk */.};../*.
8d0e0 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20  ** Maximum size 
8d0f0 6f 66 20 61 6e 79 20 61 6c 6c 6f 63 61 74 69 6f  of any allocatio
8d100 6e 20 69 73 20 28 28 31 3c 3c 4c 4f 47 4d 41 58  n is ((1<<LOGMAX
8d110 29 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 2e 20  )*mem5.szAtom). 
8d120 53 69 6e 63 65 0a 2a 2a 20 6d 65 6d 35 2e 73 7a  Since.** mem5.sz
8d130 41 74 6f 6d 20 69 73 20 61 6c 77 61 79 73 20 61  Atom is always a
8d140 74 20 6c 65 61 73 74 20 38 20 61 6e 64 20 33 32  t least 8 and 32
8d150 2d 62 69 74 20 69 6e 74 65 67 65 72 73 20 61 72  -bit integers ar
8d160 65 20 75 73 65 64 2c 0a 2a 2a 20 69 74 20 69 73  e used,.** it is
8d170 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 70 6f   not actually po
8d180 73 73 69 62 6c 65 20 74 6f 20 72 65 61 63 68 20  ssible to reach 
8d190 74 68 69 73 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23  this limit..*/.#
8d1a0 64 65 66 69 6e 65 20 4c 4f 47 4d 41 58 20 33 30  define LOGMAX 30
8d1b0 0a 0a 2f 2a 0a 2a 2a 20 4d 61 73 6b 73 20 75 73  ../*.** Masks us
8d1c0 65 64 20 66 6f 72 20 6d 65 6d 35 2e 61 43 74 72  ed for mem5.aCtr
8d1d0 6c 5b 5d 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f  l[] elements..*/
8d1e0 0a 23 64 65 66 69 6e 65 20 43 54 52 4c 5f 4c 4f  .#define CTRL_LO
8d1f0 47 53 49 5a 45 20 20 30 78 31 66 20 20 20 20 2f  GSIZE  0x1f    /
8d200 2a 20 4c 6f 67 32 20 53 69 7a 65 20 6f 66 20 74  * Log2 Size of t
8d210 68 69 73 20 62 6c 6f 63 6b 20 2a 2f 0a 23 64 65  his block */.#de
8d220 66 69 6e 65 20 43 54 52 4c 5f 46 52 45 45 20 20  fine CTRL_FREE  
8d230 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 54 72     0x20    /* Tr
8d240 75 65 20 69 66 20 6e 6f 74 20 63 68 65 63 6b 65  ue if not checke
8d250 64 20 6f 75 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  d out */../*.** 
8d260 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69  All of the stati
8d270 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64  c variables used
8d280 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20   by this module 
8d290 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a  are collected.**
8d2a0 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73   into a single s
8d2b0 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22  tructure named "
8d2c0 6d 65 6d 35 22 2e 20 20 54 68 69 73 20 69 73 20  mem5".  This is 
8d2d0 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73  to keep the.** s
8d2e0 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20  tatic variables 
8d2f0 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f  organized and to
8d300 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63   reduce namespac
8d310 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77  e pollution.** w
8d320 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20  hen this module 
8d330 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68  is combined with
8d340 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d   other in the am
8d350 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73  algamation..*/.s
8d360 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44  tatic SQLITE_WSD
8d370 20 73 74 72 75 63 74 20 4d 65 6d 35 47 6c 6f 62   struct Mem5Glob
8d380 61 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d  al {.  /*.  ** M
8d390 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20  emory available 
8d3a0 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 20  for allocation. 
8d3b0 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 41 74 6f 6d   */.  int szAtom
8d3c0 3b 20 20 20 20 20 20 2f 2a 20 53 6d 61 6c 6c 65  ;      /* Smalle
8d3d0 73 74 20 70 6f 73 73 69 62 6c 65 20 61 6c 6c 6f  st possible allo
8d3e0 63 61 74 69 6f 6e 20 69 6e 20 62 79 74 65 73 20  cation in bytes 
8d3f0 2a 2f 0a 20 20 69 6e 74 20 6e 42 6c 6f 63 6b 3b  */.  int nBlock;
8d400 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
8d410 6f 66 20 73 7a 41 74 6f 6d 20 73 69 7a 65 64 20  of szAtom sized 
8d420 62 6c 6f 63 6b 73 20 69 6e 20 7a 50 6f 6f 6c 20  blocks in zPool 
8d430 2a 2f 0a 20 20 75 38 20 2a 7a 50 6f 6f 6c 3b 20  */.  u8 *zPool; 
8d440 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20        /* Memory 
8d450 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 62 65 20  available to be 
8d460 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 0a  allocated */.  .
8d470 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20    /*.  ** Mutex 
8d480 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73  to control acces
8d490 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20  s to the memory 
8d4a0 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
8d4b0 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  stem..  */.  sql
8d4c0 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
8d4d0 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 65  x;..  /*.  ** Pe
8d4e0 72 66 6f 72 6d 61 6e 63 65 20 73 74 61 74 69 73  rformance statis
8d4f0 74 69 63 73 0a 20 20 2a 2f 0a 20 20 75 36 34 20  tics.  */.  u64 
8d500 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20  nAlloc;         
8d510 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20  /* Total number 
8d520 6f 66 20 63 61 6c 6c 73 20 74 6f 20 6d 61 6c 6c  of calls to mall
8d530 6f 63 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61  oc */.  u64 tota
8d540 6c 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a 20 54  lAlloc;     /* T
8d550 6f 74 61 6c 20 6f 66 20 61 6c 6c 20 6d 61 6c 6c  otal of all mall
8d560 6f 63 20 63 61 6c 6c 73 20 2d 20 69 6e 63 6c 75  oc calls - inclu
8d570 64 65 73 20 69 6e 74 65 72 6e 61 6c 20 66 72 61  des internal fra
8d580 67 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61 6c  g */.  u64 total
8d590 45 78 63 65 73 73 3b 20 20 20 20 2f 2a 20 54 6f  Excess;    /* To
8d5a0 74 61 6c 20 69 6e 74 65 72 6e 61 6c 20 66 72 61  tal internal fra
8d5b0 67 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20  gmentation */.  
8d5c0 75 33 32 20 63 75 72 72 65 6e 74 4f 75 74 3b 20  u32 currentOut; 
8d5d0 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 63      /* Current c
8d5e0 68 65 63 6b 6f 75 74 2c 20 69 6e 63 6c 75 64 69  heckout, includi
8d5f0 6e 67 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67  ng internal frag
8d600 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 75  mentation */.  u
8d610 33 32 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 3b  32 currentCount;
8d620 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75     /* Current nu
8d630 6d 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63 74  mber of distinct
8d640 20 63 68 65 63 6b 6f 75 74 73 20 2a 2f 0a 20 20   checkouts */.  
8d650 75 33 32 20 6d 61 78 4f 75 74 3b 20 20 20 20 20  u32 maxOut;     
8d660 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 69      /* Maximum i
8d670 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 63 75 72  nstantaneous cur
8d680 72 65 6e 74 4f 75 74 20 2a 2f 0a 20 20 75 33 32  rentOut */.  u32
8d690 20 6d 61 78 43 6f 75 6e 74 3b 20 20 20 20 20 20   maxCount;      
8d6a0 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 6e 73 74   /* Maximum inst
8d6b0 61 6e 74 61 6e 65 6f 75 73 20 63 75 72 72 65 6e  antaneous curren
8d6c0 74 43 6f 75 6e 74 20 2a 2f 0a 20 20 75 33 32 20  tCount */.  u32 
8d6d0 6d 61 78 52 65 71 75 65 73 74 3b 20 20 20 20 20  maxRequest;     
8d6e0 2f 2a 20 4c 61 72 67 65 73 74 20 61 6c 6c 6f 63  /* Largest alloc
8d6f0 61 74 69 6f 6e 20 28 65 78 63 6c 75 73 69 76 65  ation (exclusive
8d700 20 6f 66 20 69 6e 74 65 72 6e 61 6c 20 66 72 61   of internal fra
8d710 67 29 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20  g) */.  .  /*.  
8d720 2a 2a 20 4c 69 73 74 73 20 6f 66 20 66 72 65 65  ** Lists of free
8d730 20 62 6c 6f 63 6b 73 2e 20 20 61 69 46 72 65 65   blocks.  aiFree
8d740 6c 69 73 74 5b 30 5d 20 69 73 20 61 20 6c 69 73  list[0] is a lis
8d750 74 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73  t of free blocks
8d760 20 6f 66 0a 20 20 2a 2a 20 73 69 7a 65 20 6d 65   of.  ** size me
8d770 6d 35 2e 73 7a 41 74 6f 6d 2e 20 20 61 69 46 72  m5.szAtom.  aiFr
8d780 65 65 6c 69 73 74 5b 31 5d 20 68 6f 6c 64 73 20  eelist[1] holds 
8d790 62 6c 6f 63 6b 73 20 6f 66 20 73 69 7a 65 20 73  blocks of size s
8d7a0 7a 41 74 6f 6d 2a 32 2e 0a 20 20 2a 2a 20 61 6e  zAtom*2..  ** an
8d7b0 64 20 73 6f 20 66 6f 72 74 68 2e 0a 20 20 2a 2f  d so forth..  */
8d7c0 0a 20 20 69 6e 74 20 61 69 46 72 65 65 6c 69 73  .  int aiFreelis
8d7d0 74 5b 4c 4f 47 4d 41 58 2b 31 5d 3b 0a 0a 20 20  t[LOGMAX+1];..  
8d7e0 2f 2a 0a 20 20 2a 2a 20 53 70 61 63 65 20 66 6f  /*.  ** Space fo
8d7f0 72 20 74 72 61 63 6b 69 6e 67 20 77 68 69 63 68  r tracking which
8d800 20 62 6c 6f 63 6b 73 20 61 72 65 20 63 68 65 63   blocks are chec
8d810 6b 65 64 20 6f 75 74 20 61 6e 64 20 74 68 65 20  ked out and the 
8d820 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 65 61 63  size.  ** of eac
8d830 68 20 62 6c 6f 63 6b 2e 20 20 4f 6e 65 20 62 79  h block.  One by
8d840 74 65 20 70 65 72 20 62 6c 6f 63 6b 2e 0a 20 20  te per block..  
8d850 2a 2f 0a 20 20 75 38 20 2a 61 43 74 72 6c 3b 0a  */.  u8 *aCtrl;.
8d860 0a 7d 20 6d 65 6d 35 20 3d 20 7b 20 30 20 7d 3b  .} mem5 = { 0 };
8d870 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74  ../*.** Access t
8d880 68 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62  he static variab
8d890 6c 65 20 74 68 72 6f 75 67 68 20 61 20 6d 61 63  le through a mac
8d8a0 72 6f 20 66 6f 72 20 53 51 4c 49 54 45 5f 4f 4d  ro for SQLITE_OM
8d8b0 49 54 5f 57 53 44 0a 2a 2f 0a 23 64 65 66 69 6e  IT_WSD.*/.#defin
8d8c0 65 20 6d 65 6d 35 20 47 4c 4f 42 41 4c 28 73 74  e mem5 GLOBAL(st
8d8d0 72 75 63 74 20 4d 65 6d 35 47 6c 6f 62 61 6c 2c  ruct Mem5Global,
8d8e0 20 6d 65 6d 35 29 0a 0a 2f 2a 0a 2a 2a 20 41 73   mem5)../*.** As
8d8f0 73 75 6d 69 6e 67 20 6d 65 6d 35 2e 7a 50 6f 6f  suming mem5.zPoo
8d900 6c 20 69 73 20 64 69 76 69 64 65 64 20 75 70 20  l is divided up 
8d910 69 6e 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66  into an array of
8d920 20 4d 65 6d 35 4c 69 6e 6b 0a 2a 2a 20 73 74 72   Mem5Link.** str
8d930 75 63 74 75 72 65 73 2c 20 72 65 74 75 72 6e 20  uctures, return 
8d940 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
8d950 20 69 64 78 2d 74 68 20 73 75 63 68 20 6c 69 6b   idx-th such lik
8d960 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  ..*/.#define MEM
8d970 35 4c 49 4e 4b 28 69 64 78 29 20 28 28 4d 65 6d  5LINK(idx) ((Mem
8d980 35 4c 69 6e 6b 20 2a 29 28 26 6d 65 6d 35 2e 7a  5Link *)(&mem5.z
8d990 50 6f 6f 6c 5b 28 69 64 78 29 2a 6d 65 6d 35 2e  Pool[(idx)*mem5.
8d9a0 73 7a 41 74 6f 6d 5d 29 29 0a 0a 2f 2a 0a 2a 2a  szAtom]))../*.**
8d9b0 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e   Unlink the chun
8d9c0 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b  k at mem5.aPool[
8d9d0 69 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 20  i] from list it 
8d9e0 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20  is currently.** 
8d9f0 6f 6e 2e 20 20 49 74 20 73 68 6f 75 6c 64 20 62  on.  It should b
8da00 65 20 66 6f 75 6e 64 20 6f 6e 20 6d 65 6d 35 2e  e found on mem5.
8da10 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73  aiFreelist[iLogs
8da20 69 7a 65 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ize]..*/.static 
8da30 76 6f 69 64 20 6d 65 6d 73 79 73 35 55 6e 6c 69  void memsys5Unli
8da40 6e 6b 28 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c  nk(int i, int iL
8da50 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 6e  ogsize){.  int n
8da60 65 78 74 2c 20 70 72 65 76 3b 0a 20 20 61 73 73  ext, prev;.  ass
8da70 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d  ert( i>=0 && i<m
8da80 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20  em5.nBlock );.  
8da90 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65  assert( iLogsize
8daa0 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c  >=0 && iLogsize<
8dab0 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73 73  =LOGMAX );.  ass
8dac0 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c  ert( (mem5.aCtrl
8dad0 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49  [i] & CTRL_LOGSI
8dae0 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 3b  ZE)==iLogsize );
8daf0 0a 0a 20 20 6e 65 78 74 20 3d 20 4d 45 4d 35 4c  ..  next = MEM5L
8db00 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20  INK(i)->next;.  
8db10 70 72 65 76 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28  prev = MEM5LINK(
8db20 69 29 2d 3e 70 72 65 76 3b 0a 20 20 69 66 28 20  i)->prev;.  if( 
8db30 70 72 65 76 3c 30 20 29 7b 0a 20 20 20 20 6d 65  prev<0 ){.    me
8db40 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c  m5.aiFreelist[iL
8db50 6f 67 73 69 7a 65 5d 20 3d 20 6e 65 78 74 3b 0a  ogsize] = next;.
8db60 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 45 4d    }else{.    MEM
8db70 35 4c 49 4e 4b 28 70 72 65 76 29 2d 3e 6e 65 78  5LINK(prev)->nex
8db80 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 20  t = next;.  }.  
8db90 69 66 28 20 6e 65 78 74 3e 3d 30 20 29 7b 0a 20  if( next>=0 ){. 
8dba0 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 6e 65 78 74     MEM5LINK(next
8dbb0 29 2d 3e 70 72 65 76 20 3d 20 70 72 65 76 3b 0a  )->prev = prev;.
8dbc0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e    }.}../*.** Lin
8dbd0 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 6d  k the chunk at m
8dbe0 65 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f 20  em5.aPool[i] so 
8dbf0 74 68 61 74 20 69 73 20 6f 6e 20 74 68 65 20 69  that is on the i
8dc00 4c 6f 67 73 69 7a 65 0a 2a 2a 20 66 72 65 65 20  Logsize.** free 
8dc10 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  list..*/.static 
8dc20 76 6f 69 64 20 6d 65 6d 73 79 73 35 4c 69 6e 6b  void memsys5Link
8dc30 28 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f 67  (int i, int iLog
8dc40 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 78 3b 0a  size){.  int x;.
8dc50 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
8dc60 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d  3_mutex_held(mem
8dc70 35 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  5.mutex) );.  as
8dc80 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
8dc90 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20  mem5.nBlock );. 
8dca0 20 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a   assert( iLogsiz
8dcb0 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65  e>=0 && iLogsize
8dcc0 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73  <=LOGMAX );.  as
8dcd0 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72  sert( (mem5.aCtr
8dce0 6c 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53  l[i] & CTRL_LOGS
8dcf0 49 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29  IZE)==iLogsize )
8dd00 3b 0a 0a 20 20 78 20 3d 20 4d 45 4d 35 4c 49 4e  ;..  x = MEM5LIN
8dd10 4b 28 69 29 2d 3e 6e 65 78 74 20 3d 20 6d 65 6d  K(i)->next = mem
8dd20 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f  5.aiFreelist[iLo
8dd30 67 73 69 7a 65 5d 3b 0a 20 20 4d 45 4d 35 4c 49  gsize];.  MEM5LI
8dd40 4e 4b 28 69 29 2d 3e 70 72 65 76 20 3d 20 2d 31  NK(i)->prev = -1
8dd50 3b 0a 20 20 69 66 28 20 78 3e 3d 30 20 29 7b 0a  ;.  if( x>=0 ){.
8dd60 20 20 20 20 61 73 73 65 72 74 28 20 78 3c 6d 65      assert( x<me
8dd70 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 20  m5.nBlock );.   
8dd80 20 4d 45 4d 35 4c 49 4e 4b 28 78 29 2d 3e 70 72   MEM5LINK(x)->pr
8dd90 65 76 20 3d 20 69 3b 0a 20 20 7d 0a 20 20 6d 65  ev = i;.  }.  me
8dda0 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c  m5.aiFreelist[iL
8ddb0 6f 67 73 69 7a 65 5d 20 3d 20 69 3b 0a 7d 0a 0a  ogsize] = i;.}..
8ddc0 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41  /*.** If the STA
8ddd0 54 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73  TIC_MEM mutex is
8dde0 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c   not already hel
8ddf0 64 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77  d, obtain it now
8de00 2e 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77  . The mutex.** w
8de10 69 6c 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68  ill already be h
8de20 65 6c 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79  eld (obtained by
8de30 20 63 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e   code in malloc.
8de40 63 29 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33  c) if.** sqlite3
8de50 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
8de60 6d 53 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a  mStat is true..*
8de70 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65  /.static void me
8de80 6d 73 79 73 35 45 6e 74 65 72 28 76 6f 69 64 29  msys5Enter(void)
8de90 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
8dea0 78 5f 65 6e 74 65 72 28 6d 65 6d 35 2e 6d 75 74  x_enter(mem5.mut
8deb0 65 78 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  ex);.}.static vo
8dec0 69 64 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28  id memsys5Leave(
8ded0 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33  void){.  sqlite3
8dee0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
8def0 35 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a  5.mutex);.}../*.
8df00 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69  ** Return the si
8df10 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e  ze of an outstan
8df20 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c  ding allocation,
8df30 20 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65 0a   in bytes.  The.
8df40 2a 2a 20 73 69 7a 65 20 72 65 74 75 72 6e 65 64  ** size returned
8df50 20 6f 6d 69 74 73 20 74 68 65 20 38 2d 62 79 74   omits the 8-byt
8df60 65 20 68 65 61 64 65 72 20 6f 76 65 72 68 65 61  e header overhea
8df70 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a  d.  This only.**
8df80 20 77 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b   works for chunk
8df90 73 20 74 68 61 74 20 61 72 65 20 63 75 72 72 65  s that are curre
8dfa0 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74  ntly checked out
8dfb0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
8dfc0 6d 65 6d 73 79 73 35 53 69 7a 65 28 76 6f 69 64  memsys5Size(void
8dfd0 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 53 69 7a   *p){.  int iSiz
8dfe0 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29  e = 0;.  if( p )
8dff0 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 28 28  {.    int i = ((
8e000 75 38 20 2a 29 70 2d 6d 65 6d 35 2e 7a 50 6f 6f  u8 *)p-mem5.zPoo
8e010 6c 29 2f 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a  l)/mem5.szAtom;.
8e020 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30      assert( i>=0
8e030 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63   && i<mem5.nBloc
8e040 6b 20 29 3b 0a 20 20 20 20 69 53 69 7a 65 20 3d  k );.    iSize =
8e050 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 2a 20 28   mem5.szAtom * (
8e060 31 20 3c 3c 20 28 6d 65 6d 35 2e 61 43 74 72 6c  1 << (mem5.aCtrl
8e070 5b 69 5d 26 43 54 52 4c 5f 4c 4f 47 53 49 5a 45  [i]&CTRL_LOGSIZE
8e080 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ));.  }.  return
8e090 20 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   iSize;.}../*.**
8e0a0 20 46 69 6e 64 20 74 68 65 20 66 69 72 73 74 20   Find the first 
8e0b0 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 66 72 65  entry on the fre
8e0c0 65 6c 69 73 74 20 69 4c 6f 67 73 69 7a 65 2e 20  elist iLogsize. 
8e0d0 20 55 6e 6c 69 6e 6b 20 74 68 61 74 0a 2a 2a 20   Unlink that.** 
8e0e0 65 6e 74 72 79 20 61 6e 64 20 72 65 74 75 72 6e  entry and return
8e0f0 20 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2f 0a   its index. .*/.
8e100 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79  static int memsy
8e110 73 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69 6e  s5UnlinkFirst(in
8e120 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69  t iLogsize){.  i
8e130 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 46 69 72  nt i;.  int iFir
8e140 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69  st;..  assert( i
8e150 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c  Logsize>=0 && iL
8e160 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29  ogsize<=LOGMAX )
8e170 3b 0a 20 20 69 20 3d 20 69 46 69 72 73 74 20 3d  ;.  i = iFirst =
8e180 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74   mem5.aiFreelist
8e190 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 61 73  [iLogsize];.  as
8e1a0 73 65 72 74 28 20 69 46 69 72 73 74 3e 3d 30 20  sert( iFirst>=0 
8e1b0 29 3b 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20  );.  while( i>0 
8e1c0 29 7b 0a 20 20 20 20 69 66 28 20 69 3c 69 46 69  ){.    if( i<iFi
8e1d0 72 73 74 20 29 20 69 46 69 72 73 74 20 3d 20 69  rst ) iFirst = i
8e1e0 3b 0a 20 20 20 20 69 20 3d 20 4d 45 4d 35 4c 49  ;.    i = MEM5LI
8e1f0 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 7d  NK(i)->next;.  }
8e200 0a 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b  .  memsys5Unlink
8e210 28 69 46 69 72 73 74 2c 20 69 4c 6f 67 73 69 7a  (iFirst, iLogsiz
8e220 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46 69  e);.  return iFi
8e230 72 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  rst;.}../*.** Re
8e240 74 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20  turn a block of 
8e250 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61  memory of at lea
8e260 73 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a  st nBytes in siz
8e270 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c  e..** Return NUL
8e280 4c 20 69 66 20 75 6e 61 62 6c 65 2e 20 20 52 65  L if unable.  Re
8e290 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 42 79  turn NULL if nBy
8e2a0 74 65 73 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68  tes==0..**.** Th
8e2b0 65 20 63 61 6c 6c 65 72 20 67 75 61 72 61 6e 74  e caller guarant
8e2c0 65 65 73 20 74 68 61 74 20 6e 42 79 74 65 20 70  ees that nByte p
8e2d0 6f 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54  ositive..**.** T
8e2e0 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f 62  he caller has ob
8e2f0 74 61 69 6e 65 64 20 61 20 6d 75 74 65 78 20 70  tained a mutex p
8e300 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67  rior to invoking
8e310 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   this.** routine
8e320 20 73 6f 20 74 68 65 72 65 20 69 73 20 6e 65 76   so there is nev
8e330 65 72 20 61 6e 79 20 63 68 61 6e 63 65 20 74 68  er any chance th
8e340 61 74 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a  at two or more.*
8e350 2a 20 74 68 72 65 61 64 73 20 63 61 6e 20 62 65  * threads can be
8e360 20 69 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65   in this routine
8e370 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
8e380 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
8e390 64 20 2a 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63  d *memsys5Malloc
8e3a0 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74 65  Unsafe(int nByte
8e3b0 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  ){.  int i;     
8e3c0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
8e3d0 66 20 61 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 5d  f a mem5.aPool[]
8e3e0 20 73 6c 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 69   slot */.  int i
8e3f0 42 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 49  Bin;        /* I
8e400 6e 64 65 78 20 69 6e 74 6f 20 6d 65 6d 35 2e 61  ndex into mem5.a
8e410 69 46 72 65 65 6c 69 73 74 5b 5d 20 2a 2f 0a 20  iFreelist[] */. 
8e420 20 69 6e 74 20 69 46 75 6c 6c 53 7a 3b 20 20 20   int iFullSz;   
8e430 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c    /* Size of all
8e440 6f 63 61 74 69 6f 6e 20 72 6f 75 6e 64 65 64 20  ocation rounded 
8e450 75 70 20 74 6f 20 70 6f 77 65 72 20 6f 66 20 32  up to power of 2
8e460 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 6f 67 73 69   */.  int iLogsi
8e470 7a 65 3b 20 20 20 20 2f 2a 20 4c 6f 67 32 20 6f  ze;    /* Log2 o
8e480 66 20 69 46 75 6c 6c 53 7a 2f 50 4f 57 32 5f 4d  f iFullSz/POW2_M
8e490 49 4e 20 2a 2f 0a 0a 20 20 2f 2a 20 6e 42 79 74  IN */..  /* nByt
8e4a0 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f 73 69  e must be a posi
8e4b0 74 69 76 65 20 2a 2f 0a 20 20 61 73 73 65 72 74  tive */.  assert
8e4c0 28 20 6e 42 79 74 65 3e 30 20 29 3b 0a 0a 20 20  ( nByte>0 );..  
8e4d0 2f 2a 20 4b 65 65 70 20 74 72 61 63 6b 20 6f 66  /* Keep track of
8e4e0 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c   the maximum all
8e4f0 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 2e  ocation request.
8e500 20 20 45 76 65 6e 20 75 6e 66 75 6c 66 69 6c 6c    Even unfulfill
8e510 65 64 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 73  ed.  ** requests
8e520 20 61 72 65 20 63 6f 75 6e 74 65 64 20 2a 2f 0a   are counted */.
8e530 20 20 69 66 28 20 28 75 33 32 29 6e 42 79 74 65    if( (u32)nByte
8e540 3e 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74  >mem5.maxRequest
8e550 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 61 78   ){.    mem5.max
8e560 52 65 71 75 65 73 74 20 3d 20 6e 42 79 74 65 3b  Request = nByte;
8e570 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 62 6f 72 74  .  }..  /* Abort
8e580 20 69 66 20 74 68 65 20 72 65 71 75 65 73 74 65   if the requeste
8e590 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a  d allocation siz
8e5a0 65 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  e is larger than
8e5b0 20 74 68 65 20 6c 61 72 67 65 73 74 0a 20 20 2a   the largest.  *
8e5c0 2a 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 74  * power of two t
8e5d0 68 61 74 20 77 65 20 63 61 6e 20 72 65 70 72 65  hat we can repre
8e5e0 73 65 6e 74 20 75 73 69 6e 67 20 33 32 2d 62 69  sent using 32-bi
8e5f0 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
8e600 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42  s..  */.  if( nB
8e610 79 74 65 20 3e 20 30 78 34 30 30 30 30 30 30 30  yte > 0x40000000
8e620 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
8e630 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 75 6e  ;.  }..  /* Roun
8e640 64 20 6e 42 79 74 65 20 75 70 20 74 6f 20 74 68  d nByte up to th
8e650 65 20 6e 65 78 74 20 76 61 6c 69 64 20 70 6f 77  e next valid pow
8e660 65 72 20 6f 66 20 74 77 6f 20 2a 2f 0a 20 20 66  er of two */.  f
8e670 6f 72 28 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35 2e  or(iFullSz=mem5.
8e680 73 7a 41 74 6f 6d 2c 20 69 4c 6f 67 73 69 7a 65  szAtom, iLogsize
8e690 3d 30 3b 20 69 46 75 6c 6c 53 7a 3c 6e 42 79 74  =0; iFullSz<nByt
8e6a0 65 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d 20 32 2c  e; iFullSz *= 2,
8e6b0 20 69 4c 6f 67 73 69 7a 65 2b 2b 29 7b 7d 0a 0a   iLogsize++){}..
8e6c0 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 6d    /* Make sure m
8e6d0 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69  em5.aiFreelist[i
8e6e0 4c 6f 67 73 69 7a 65 5d 20 63 6f 6e 74 61 69 6e  Logsize] contain
8e6f0 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 66  s at least one f
8e700 72 65 65 0a 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20  ree.  ** block. 
8e710 20 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20 73 70   If not, then sp
8e720 6c 69 74 20 61 20 62 6c 6f 63 6b 20 6f 66 20 74  lit a block of t
8e730 68 65 20 6e 65 78 74 20 6c 61 72 67 65 72 20 70  he next larger p
8e740 6f 77 65 72 20 6f 66 0a 20 20 2a 2a 20 74 77 6f  ower of.  ** two
8e750 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65   in order to cre
8e760 61 74 65 20 61 20 6e 65 77 20 66 72 65 65 20 62  ate a new free b
8e770 6c 6f 63 6b 20 6f 66 20 73 69 7a 65 20 69 4c 6f  lock of size iLo
8e780 67 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f  gsize..  */.  fo
8e790 72 28 69 42 69 6e 3d 69 4c 6f 67 73 69 7a 65 3b  r(iBin=iLogsize;
8e7a0 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74   mem5.aiFreelist
8e7b0 5b 69 42 69 6e 5d 3c 30 20 26 26 20 69 42 69 6e  [iBin]<0 && iBin
8e7c0 3c 3d 4c 4f 47 4d 41 58 3b 20 69 42 69 6e 2b 2b  <=LOGMAX; iBin++
8e7d0 29 7b 7d 0a 20 20 69 66 28 20 69 42 69 6e 3e 4c  ){}.  if( iBin>L
8e7e0 4f 47 4d 41 58 20 29 20 72 65 74 75 72 6e 20 30  OGMAX ) return 0
8e7f0 3b 0a 20 20 69 20 3d 20 6d 65 6d 73 79 73 35 55  ;.  i = memsys5U
8e800 6e 6c 69 6e 6b 46 69 72 73 74 28 69 42 69 6e 29  nlinkFirst(iBin)
8e810 3b 0a 20 20 77 68 69 6c 65 28 20 69 42 69 6e 3e  ;.  while( iBin>
8e820 69 4c 6f 67 73 69 7a 65 20 29 7b 0a 20 20 20 20  iLogsize ){.    
8e830 69 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 0a 20 20  int newSize;..  
8e840 20 20 69 42 69 6e 2d 2d 3b 0a 20 20 20 20 6e 65    iBin--;.    ne
8e850 77 53 69 7a 65 20 3d 20 31 20 3c 3c 20 69 42 69  wSize = 1 << iBi
8e860 6e 3b 0a 20 20 20 20 6d 65 6d 35 2e 61 43 74 72  n;.    mem5.aCtr
8e870 6c 5b 69 2b 6e 65 77 53 69 7a 65 5d 20 3d 20 43  l[i+newSize] = C
8e880 54 52 4c 5f 46 52 45 45 20 7c 20 69 42 69 6e 3b  TRL_FREE | iBin;
8e890 0a 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b  .    memsys5Link
8e8a0 28 69 2b 6e 65 77 53 69 7a 65 2c 20 69 42 69 6e  (i+newSize, iBin
8e8b0 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 43  );.  }.  mem5.aC
8e8c0 74 72 6c 5b 69 5d 20 3d 20 69 4c 6f 67 73 69 7a  trl[i] = iLogsiz
8e8d0 65 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20  e;..  /* Update 
8e8e0 61 6c 6c 6f 63 61 74 6f 72 20 70 65 72 66 6f 72  allocator perfor
8e8f0 6d 61 6e 63 65 20 73 74 61 74 69 73 74 69 63 73  mance statistics
8e900 2e 20 2a 2f 0a 20 20 6d 65 6d 35 2e 6e 41 6c 6c  . */.  mem5.nAll
8e910 6f 63 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74  oc++;.  mem5.tot
8e920 61 6c 41 6c 6c 6f 63 20 2b 3d 20 69 46 75 6c 6c  alAlloc += iFull
8e930 53 7a 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c  Sz;.  mem5.total
8e940 45 78 63 65 73 73 20 2b 3d 20 69 46 75 6c 6c 53  Excess += iFullS
8e950 7a 20 2d 20 6e 42 79 74 65 3b 0a 20 20 6d 65 6d  z - nByte;.  mem
8e960 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 2b 2b  5.currentCount++
8e970 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74  ;.  mem5.current
8e980 4f 75 74 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a  Out += iFullSz;.
8e990 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78 43 6f    if( mem5.maxCo
8e9a0 75 6e 74 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74  unt<mem5.current
8e9b0 43 6f 75 6e 74 20 29 20 6d 65 6d 35 2e 6d 61 78  Count ) mem5.max
8e9c0 43 6f 75 6e 74 20 3d 20 6d 65 6d 35 2e 63 75 72  Count = mem5.cur
8e9d0 72 65 6e 74 43 6f 75 6e 74 3b 0a 20 20 69 66 28  rentCount;.  if(
8e9e0 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 3c 6d 65 6d   mem5.maxOut<mem
8e9f0 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 29 20 6d  5.currentOut ) m
8ea00 65 6d 35 2e 6d 61 78 4f 75 74 20 3d 20 6d 65 6d  em5.maxOut = mem
8ea10 35 2e 63 75 72 72 65 6e 74 4f 75 74 3b 0a 0a 20  5.currentOut;.. 
8ea20 20 2f 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69   /* Return a poi
8ea30 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f  nter to the allo
8ea40 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20 2a 2f  cated memory. */
8ea50 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a  .  return (void*
8ea60 29 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 69 2a 6d  )&mem5.zPool[i*m
8ea70 65 6d 35 2e 73 7a 41 74 6f 6d 5d 3b 0a 7d 0a 0a  em5.szAtom];.}..
8ea80 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75  /*.** Free an ou
8ea90 74 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79  tstanding memory
8eaa0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a   allocation..*/.
8eab0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
8eac0 79 73 35 46 72 65 65 55 6e 73 61 66 65 28 76 6f  ys5FreeUnsafe(vo
8ead0 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 75 33 32  id *pOld){.  u32
8eae0 20 73 69 7a 65 2c 20 69 4c 6f 67 73 69 7a 65 3b   size, iLogsize;
8eaf0 0a 20 20 69 6e 74 20 69 42 6c 6f 63 6b 3b 0a 0a  .  int iBlock;..
8eb00 20 20 2f 2a 20 53 65 74 20 69 42 6c 6f 63 6b 20    /* Set iBlock 
8eb10 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  to the index of 
8eb20 74 68 65 20 62 6c 6f 63 6b 20 70 6f 69 6e 74 65  the block pointe
8eb30 64 20 74 6f 20 62 79 20 70 4f 6c 64 20 69 6e 20  d to by pOld in 
8eb40 0a 20 20 2a 2a 20 74 68 65 20 61 72 72 61 79 20  .  ** the array 
8eb50 6f 66 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 62  of mem5.szAtom b
8eb60 79 74 65 20 62 6c 6f 63 6b 73 20 70 6f 69 6e 74  yte blocks point
8eb70 65 64 20 74 6f 20 62 79 20 6d 65 6d 35 2e 7a 50  ed to by mem5.zP
8eb80 6f 6f 6c 2e 0a 20 20 2a 2f 0a 20 20 69 42 6c 6f  ool..  */.  iBlo
8eb90 63 6b 20 3d 20 28 28 75 38 20 2a 29 70 4f 6c 64  ck = ((u8 *)pOld
8eba0 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d  -mem5.zPool)/mem
8ebb0 35 2e 73 7a 41 74 6f 6d 3b 0a 0a 20 20 2f 2a 20  5.szAtom;..  /* 
8ebc0 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70  Check that the p
8ebd0 6f 69 6e 74 65 72 20 70 4f 6c 64 20 70 6f 69 6e  ointer pOld poin
8ebe0 74 73 20 74 6f 20 61 20 76 61 6c 69 64 2c 20 6e  ts to a valid, n
8ebf0 6f 6e 2d 66 72 65 65 20 62 6c 6f 63 6b 2e 20 2a  on-free block. *
8ec00 2f 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f  /.  assert( iBlo
8ec10 63 6b 3e 3d 30 20 26 26 20 69 42 6c 6f 63 6b 3c  ck>=0 && iBlock<
8ec20 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20  mem5.nBlock );. 
8ec30 20 61 73 73 65 72 74 28 20 28 28 75 38 20 2a 29   assert( ((u8 *)
8ec40 70 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29  pOld-mem5.zPool)
8ec50 25 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3d 3d 30 20  %mem5.szAtom==0 
8ec60 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65  );.  assert( (me
8ec70 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d  m5.aCtrl[iBlock]
8ec80 20 26 20 43 54 52 4c 5f 46 52 45 45 29 3d 3d 30   & CTRL_FREE)==0
8ec90 20 29 3b 0a 0a 20 20 69 4c 6f 67 73 69 7a 65 20   );..  iLogsize 
8eca0 3d 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c  = mem5.aCtrl[iBl
8ecb0 6f 63 6b 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53  ock] & CTRL_LOGS
8ecc0 49 5a 45 3b 0a 20 20 73 69 7a 65 20 3d 20 31 3c  IZE;.  size = 1<
8ecd0 3c 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 61 73 73  <iLogsize;.  ass
8ece0 65 72 74 28 20 69 42 6c 6f 63 6b 2b 73 69 7a 65  ert( iBlock+size
8ecf0 2d 31 3c 28 75 33 32 29 6d 65 6d 35 2e 6e 42 6c  -1<(u32)mem5.nBl
8ed00 6f 63 6b 20 29 3b 0a 0a 20 20 6d 65 6d 35 2e 61  ock );..  mem5.a
8ed10 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 7c 3d 20  Ctrl[iBlock] |= 
8ed20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 6d 65 6d  CTRL_FREE;.  mem
8ed30 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 2b 73  5.aCtrl[iBlock+s
8ed40 69 7a 65 2d 31 5d 20 7c 3d 20 43 54 52 4c 5f 46  ize-1] |= CTRL_F
8ed50 52 45 45 3b 0a 20 20 61 73 73 65 72 74 28 20 6d  REE;.  assert( m
8ed60 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74  em5.currentCount
8ed70 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
8ed80 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3e  mem5.currentOut>
8ed90 3d 28 73 69 7a 65 2a 6d 65 6d 35 2e 73 7a 41 74  =(size*mem5.szAt
8eda0 6f 6d 29 20 29 3b 0a 20 20 6d 65 6d 35 2e 63 75  om) );.  mem5.cu
8edb0 72 72 65 6e 74 43 6f 75 6e 74 2d 2d 3b 0a 20 20  rrentCount--;.  
8edc0 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20  mem5.currentOut 
8edd0 2d 3d 20 73 69 7a 65 2a 6d 65 6d 35 2e 73 7a 41  -= size*mem5.szA
8ede0 74 6f 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 6d  tom;.  assert( m
8edf0 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3e 30  em5.currentOut>0
8ee00 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74   || mem5.current
8ee10 43 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73  Count==0 );.  as
8ee20 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65  sert( mem5.curre
8ee30 6e 74 43 6f 75 6e 74 3e 30 20 7c 7c 20 6d 65 6d  ntCount>0 || mem
8ee40 35 2e 63 75 72 72 65 6e 74 4f 75 74 3d 3d 30 20  5.currentOut==0 
8ee50 29 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c  );..  mem5.aCtrl
8ee60 5b 69 42 6c 6f 63 6b 5d 20 3d 20 43 54 52 4c 5f  [iBlock] = CTRL_
8ee70 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65 3b  FREE | iLogsize;
8ee80 0a 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53  .  while( ALWAYS
8ee90 28 69 4c 6f 67 73 69 7a 65 3c 4c 4f 47 4d 41 58  (iLogsize<LOGMAX
8eea0 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42 75  ) ){.    int iBu
8eeb0 64 64 79 3b 0a 20 20 20 20 69 66 28 20 28 69 42  ddy;.    if( (iB
8eec0 6c 6f 63 6b 3e 3e 69 4c 6f 67 73 69 7a 65 29 20  lock>>iLogsize) 
8eed0 26 20 31 20 29 7b 0a 20 20 20 20 20 20 69 42 75  & 1 ){.      iBu
8eee0 64 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2d 20 73  ddy = iBlock - s
8eef0 69 7a 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ize;.    }else{.
8ef00 20 20 20 20 20 20 69 42 75 64 64 79 20 3d 20 69        iBuddy = i
8ef10 42 6c 6f 63 6b 20 2b 20 73 69 7a 65 3b 0a 20 20  Block + size;.  
8ef20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
8ef30 69 42 75 64 64 79 3e 3d 30 20 29 3b 0a 20 20 20  iBuddy>=0 );.   
8ef40 20 69 66 28 20 28 69 42 75 64 64 79 2b 28 31 3c   if( (iBuddy+(1<
8ef50 3c 69 4c 6f 67 73 69 7a 65 29 29 3e 6d 65 6d 35  <iLogsize))>mem5
8ef60 2e 6e 42 6c 6f 63 6b 20 29 20 62 72 65 61 6b 3b  .nBlock ) break;
8ef70 0a 20 20 20 20 69 66 28 20 6d 65 6d 35 2e 61 43  .    if( mem5.aC
8ef80 74 72 6c 5b 69 42 75 64 64 79 5d 21 3d 28 43 54  trl[iBuddy]!=(CT
8ef90 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69  RL_FREE | iLogsi
8efa0 7a 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ze) ) break;.   
8efb0 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69   memsys5Unlink(i
8efc0 42 75 64 64 79 2c 20 69 4c 6f 67 73 69 7a 65 29  Buddy, iLogsize)
8efd0 3b 0a 20 20 20 20 69 4c 6f 67 73 69 7a 65 2b 2b  ;.    iLogsize++
8efe0 3b 0a 20 20 20 20 69 66 28 20 69 42 75 64 64 79  ;.    if( iBuddy
8eff0 3c 69 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20  <iBlock ){.     
8f000 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64   mem5.aCtrl[iBud
8f010 64 79 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20  dy] = CTRL_FREE 
8f020 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20  | iLogsize;.    
8f030 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c    mem5.aCtrl[iBl
8f040 6f 63 6b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20  ock] = 0;.      
8f050 69 42 6c 6f 63 6b 20 3d 20 69 42 75 64 64 79 3b  iBlock = iBuddy;
8f060 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
8f070 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c    mem5.aCtrl[iBl
8f080 6f 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45  ock] = CTRL_FREE
8f090 20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20   | iLogsize;.   
8f0a0 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42     mem5.aCtrl[iB
8f0b0 75 64 64 79 5d 20 3d 20 30 3b 0a 20 20 20 20 7d  uddy] = 0;.    }
8f0c0 0a 20 20 20 20 73 69 7a 65 20 2a 3d 20 32 3b 0a  .    size *= 2;.
8f0d0 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 4c 69 6e    }.  memsys5Lin
8f0e0 6b 28 69 42 6c 6f 63 6b 2c 20 69 4c 6f 67 73 69  k(iBlock, iLogsi
8f0f0 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c  ze);.}../*.** Al
8f100 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f 66  locate nBytes of
8f110 20 6d 65 6d 6f 72 79 0a 2a 2f 0a 73 74 61 74 69   memory.*/.stati
8f120 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d  c void *memsys5M
8f130 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 73  alloc(int nBytes
8f140 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  ){.  sqlite3_int
8f150 36 34 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28  64 *p = 0;.  if(
8f160 20 6e 42 79 74 65 73 3e 30 20 29 7b 0a 20 20 20   nBytes>0 ){.   
8f170 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b   memsys5Enter();
8f180 0a 20 20 20 20 70 20 3d 20 6d 65 6d 73 79 73 35  .    p = memsys5
8f190 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79  MallocUnsafe(nBy
8f1a0 74 65 73 29 3b 0a 20 20 20 20 6d 65 6d 73 79 73  tes);.    memsys
8f1b0 35 4c 65 61 76 65 28 29 3b 0a 20 20 7d 0a 20 20  5Leave();.  }.  
8f1c0 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b  return (void*)p;
8f1d0 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20   .}../*.** Free 
8f1e0 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  memory..**.** Th
8f1f0 65 20 6f 75 74 65 72 20 6c 61 79 65 72 20 6d 65  e outer layer me
8f200 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 70  mory allocator p
8f210 72 65 76 65 6e 74 73 20 74 68 69 73 20 72 6f 75  revents this rou
8f220 74 69 6e 65 20 66 72 6f 6d 0a 2a 2a 20 62 65 69  tine from.** bei
8f230 6e 67 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70  ng called with p
8f240 50 72 69 6f 72 3d 3d 30 2e 0a 2a 2f 0a 73 74 61  Prior==0..*/.sta
8f250 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35  tic void memsys5
8f260 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f  Free(void *pPrio
8f270 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50  r){.  assert( pP
8f280 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 6d 65 6d  rior!=0 );.  mem
8f290 73 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 6d  sys5Enter();.  m
8f2a0 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61 66 65  emsys5FreeUnsafe
8f2b0 28 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d 73  (pPrior);.  mems
8f2c0 79 73 35 4c 65 61 76 65 28 29 3b 20 20 0a 7d 0a  ys5Leave();  .}.
8f2d0 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
8f2e0 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69  e size of an exi
8f2f0 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c  sting memory all
8f300 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  ocation..**.** T
8f310 68 65 20 6f 75 74 65 72 20 6c 61 79 65 72 20 6d  he outer layer m
8f320 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
8f330 70 72 65 76 65 6e 74 73 20 74 68 69 73 20 72 6f  prevents this ro
8f340 75 74 69 6e 65 20 66 72 6f 6d 0a 2a 2a 20 62 65  utine from.** be
8f350 69 6e 67 20 63 61 6c 6c 65 64 20 77 69 74 68 20  ing called with 
8f360 70 50 72 69 6f 72 3d 3d 30 2e 20 20 0a 2a 2a 0a  pPrior==0.  .**.
8f370 2a 2a 20 6e 42 79 74 65 73 20 69 73 20 61 6c 77  ** nBytes is alw
8f380 61 79 73 20 61 20 76 61 6c 75 65 20 6f 62 74 61  ays a value obta
8f390 69 6e 65 64 20 66 72 6f 6d 20 61 20 70 72 69 6f  ined from a prio
8f3a0 72 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 6d 65 6d  r call to.** mem
8f3b0 73 79 73 35 52 6f 75 6e 64 28 29 2e 20 20 48 65  sys5Round().  He
8f3c0 6e 63 65 20 6e 42 79 74 65 73 20 69 73 20 61 6c  nce nBytes is al
8f3d0 77 61 79 73 20 61 20 6e 6f 6e 2d 6e 65 67 61 74  ways a non-negat
8f3e0 69 76 65 20 70 6f 77 65 72 0a 2a 2a 20 6f 66 20  ive power.** of 
8f3f0 74 77 6f 2e 20 20 49 66 20 6e 42 79 74 65 73 3d  two.  If nBytes=
8f400 3d 30 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  =0 that means th
8f410 61 74 20 61 6e 20 6f 76 65 72 73 69 7a 65 20 61  at an oversize a
8f420 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 28 61 6e  llocation.** (an
8f430 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67   allocation larg
8f440 65 72 20 74 68 61 6e 20 30 78 34 30 30 30 30 30  er than 0x400000
8f450 30 30 29 20 77 61 73 20 72 65 71 75 65 73 74 65  00) was requeste
8f460 64 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 72 6f  d and this.** ro
8f470 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 65 74  utine should ret
8f480 75 72 6e 20 30 20 77 69 74 68 6f 75 74 20 66 72  urn 0 without fr
8f490 65 65 69 6e 67 20 70 50 72 69 6f 72 2e 0a 2a 2f  eeing pPrior..*/
8f4a0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65  .static void *me
8f4b0 6d 73 79 73 35 52 65 61 6c 6c 6f 63 28 76 6f 69  msys5Realloc(voi
8f4c0 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e  d *pPrior, int n
8f4d0 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f  Bytes){.  int nO
8f4e0 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20  ld;.  void *p;. 
8f4f0 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21   assert( pPrior!
8f500 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
8f510 28 6e 42 79 74 65 73 26 28 6e 42 79 74 65 73 2d  (nBytes&(nBytes-
8f520 31 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  1))==0 );.  asse
8f530 72 74 28 20 6e 42 79 74 65 73 3e 3d 30 20 29 3b  rt( nBytes>=0 );
8f540 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3d 3d 30  .  if( nBytes==0
8f550 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
8f560 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d  ;.  }.  nOld = m
8f570 65 6d 73 79 73 35 53 69 7a 65 28 70 50 72 69 6f  emsys5Size(pPrio
8f580 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73  r);.  if( nBytes
8f590 3c 3d 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 72 65  <=nOld ){.    re
8f5a0 74 75 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d  turn pPrior;.  }
8f5b0 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28  .  memsys5Enter(
8f5c0 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35  );.  p = memsys5
8f5d0 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79  MallocUnsafe(nBy
8f5e0 74 65 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  tes);.  if( p ){
8f5f0 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70  .    memcpy(p, p
8f600 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20  Prior, nOld);.  
8f610 20 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73    memsys5FreeUns
8f620 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d  afe(pPrior);.  }
8f630 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28  .  memsys5Leave(
8f640 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d  );.  return p;.}
8f650 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70  ../*.** Round up
8f660 20 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20   a request size 
8f670 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69  to the next vali
8f680 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a  d allocation siz
8f690 65 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 61 6c  e.  If.** the al
8f6a0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 74 6f 6f 20  location is too 
8f6b0 6c 61 72 67 65 20 74 6f 20 62 65 20 68 61 6e 64  large to be hand
8f6c0 6c 65 64 20 62 79 20 74 68 69 73 20 61 6c 6c 6f  led by this allo
8f6d0 63 61 74 69 6f 6e 20 73 79 73 74 65 6d 2c 0a 2a  cation system,.*
8f6e0 2a 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a  * return 0..**.*
8f6f0 2a 20 41 6c 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e  * All allocation
8f700 73 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65  s must be a powe
8f710 72 20 6f 66 20 74 77 6f 20 61 6e 64 20 6d 75 73  r of two and mus
8f720 74 20 62 65 20 65 78 70 72 65 73 73 65 64 20 62  t be expressed b
8f730 79 20 61 0a 2a 2a 20 33 32 2d 62 69 74 20 73 69  y a.** 32-bit si
8f740 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 20 20 48  gned integer.  H
8f750 65 6e 63 65 20 74 68 65 20 6c 61 72 67 65 73 74  ence the largest
8f760 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 30   allocation is 0
8f770 78 34 30 30 30 30 30 30 30 0a 2a 2a 20 6f 72 20  x40000000.** or 
8f780 31 30 37 33 37 34 31 38 32 34 20 62 79 74 65 73  1073741824 bytes
8f790 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
8f7a0 6d 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28 69  memsys5Roundup(i
8f7b0 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 46 75  nt n){.  int iFu
8f7c0 6c 6c 53 7a 3b 0a 20 20 69 66 28 20 6e 20 3e 20  llSz;.  if( n > 
8f7d0 30 78 34 30 30 30 30 30 30 30 20 29 20 72 65 74  0x40000000 ) ret
8f7e0 75 72 6e 20 30 3b 0a 20 20 66 6f 72 28 69 46 75  urn 0;.  for(iFu
8f7f0 6c 6c 53 7a 3d 6d 65 6d 35 2e 73 7a 41 74 6f 6d  llSz=mem5.szAtom
8f800 3b 20 69 46 75 6c 6c 53 7a 3c 6e 3b 20 69 46 75  ; iFullSz<n; iFu
8f810 6c 6c 53 7a 20 2a 3d 20 32 29 3b 0a 20 20 72 65  llSz *= 2);.  re
8f820 74 75 72 6e 20 69 46 75 6c 6c 53 7a 3b 0a 7d 0a  turn iFullSz;.}.
8f830 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
8f840 65 20 63 65 69 6c 69 6e 67 20 6f 66 20 74 68 65  e ceiling of the
8f850 20 6c 6f 67 61 72 69 74 68 6d 20 62 61 73 65 20   logarithm base 
8f860 32 20 6f 66 20 69 56 61 6c 75 65 2e 0a 2a 2a 0a  2 of iValue..**.
8f870 2a 2a 20 45 78 61 6d 70 6c 65 73 3a 20 20 20 6d  ** Examples:   m
8f880 65 6d 73 79 73 35 4c 6f 67 28 31 29 20 2d 3e 20  emsys5Log(1) -> 
8f890 30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  0.**            
8f8a0 20 6d 65 6d 73 79 73 35 4c 6f 67 28 32 29 20 2d   memsys5Log(2) -
8f8b0 3e 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  > 1.**          
8f8c0 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 34 29     memsys5Log(4)
8f8d0 20 2d 3e 20 32 0a 2a 2a 20 20 20 20 20 20 20 20   -> 2.**        
8f8e0 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28       memsys5Log(
8f8f0 35 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20 20 20  5) -> 3.**      
8f900 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f         memsys5Lo
8f910 67 28 38 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20  g(8) -> 3.**    
8f920 20 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35           memsys5
8f930 4c 6f 67 28 39 29 20 2d 3e 20 34 0a 2a 2f 0a 73  Log(9) -> 4.*/.s
8f940 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73  tatic int memsys
8f950 35 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65 29  5Log(int iValue)
8f960 7b 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0a 20 20  {.  int iLog;.  
8f970 66 6f 72 28 69 4c 6f 67 3d 30 3b 20 28 31 3c 3c  for(iLog=0; (1<<
8f980 69 4c 6f 67 29 3c 69 56 61 6c 75 65 3b 20 69 4c  iLog)<iValue; iL
8f990 6f 67 2b 2b 29 3b 0a 20 20 72 65 74 75 72 6e 20  og++);.  return 
8f9a0 69 4c 6f 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  iLog;.}../*.** I
8f9b0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65  nitialize the me
8f9c0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 0a  mory allocator..
8f9d0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
8f9e0 6e 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64  ne is not thread
8f9f0 73 61 66 65 2e 20 20 54 68 65 20 63 61 6c 6c 65  safe.  The calle
8fa00 72 20 6d 75 73 74 20 62 65 20 68 6f 6c 64 69 6e  r must be holdin
8fa10 67 20 61 20 6d 75 74 65 78 0a 2a 2a 20 74 6f 20  g a mutex.** to 
8fa20 70 72 65 76 65 6e 74 20 6d 75 6c 74 69 70 6c 65  prevent multiple
8fa30 20 74 68 72 65 61 64 73 20 66 72 6f 6d 20 65 6e   threads from en
8fa40 74 65 72 69 6e 67 20 61 74 20 74 68 65 20 73 61  tering at the sa
8fa50 6d 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61 74  me time..*/.stat
8fa60 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 49 6e  ic int memsys5In
8fa70 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64  it(void *NotUsed
8fa80 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20 20 20 20  ){.  int ii;    
8fa90 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
8faa0 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  counter */.  int
8fab0 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20   nByte;         
8fac0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
8fad0 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61  es of memory ava
8fae0 69 6c 61 62 6c 65 20 74 6f 20 74 68 69 73 20 61  ilable to this a
8faf0 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 75 38  llocator */.  u8
8fb00 20 2a 7a 42 79 74 65 3b 20 20 20 20 20 20 20 20   *zByte;        
8fb10 20 2f 2a 20 4d 65 6d 6f 72 79 20 75 73 61 62 6c   /* Memory usabl
8fb20 65 20 62 79 20 74 68 69 73 20 61 6c 6c 6f 63 61  e by this alloca
8fb30 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 69  tor */.  int nMi
8fb40 6e 4c 6f 67 3b 20 20 20 20 20 20 20 2f 2a 20 4c  nLog;       /* L
8fb50 6f 67 20 62 61 73 65 20 32 20 6f 66 20 6d 69 6e  og base 2 of min
8fb60 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  imum allocation 
8fb70 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 2a 2f  size in bytes */
8fb80 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20  .  int iOffset; 
8fb90 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 66 66 73        /* An offs
8fba0 65 74 20 69 6e 74 6f 20 6d 65 6d 35 2e 61 43 74  et into mem5.aCt
8fbb0 72 6c 5b 5d 20 2a 2f 0a 0a 20 20 55 4e 55 53 45  rl[] */..  UNUSE
8fbc0 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
8fbd0 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20  sed);..  /* For 
8fbe0 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20  the purposes of 
8fbf0 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 64 69  this routine, di
8fc00 73 61 62 6c 65 20 74 68 65 20 6d 75 74 65 78 20  sable the mutex 
8fc10 2a 2f 0a 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20  */.  mem5.mutex 
8fc20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 73  = 0;..  /* The s
8fc30 69 7a 65 20 6f 66 20 61 20 4d 65 6d 35 4c 69 6e  ize of a Mem5Lin
8fc40 6b 20 6f 62 6a 65 63 74 20 6d 75 73 74 20 62 65  k object must be
8fc50 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e   a power of two.
8fc60 20 20 56 65 72 69 66 79 20 74 68 61 74 0a 20 20    Verify that.  
8fc70 2a 2a 20 74 68 69 73 20 69 73 20 63 61 73 65 2e  ** this is case.
8fc80 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
8fc90 28 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e 6b  (sizeof(Mem5Link
8fca0 29 26 28 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69  )&(sizeof(Mem5Li
8fcb0 6e 6b 29 2d 31 29 29 3d 3d 30 20 29 3b 0a 0a 20  nk)-1))==0 );.. 
8fcc0 20 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65 33   nByte = sqlite3
8fcd0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65  GlobalConfig.nHe
8fce0 61 70 3b 0a 20 20 7a 42 79 74 65 20 3d 20 28 75  ap;.  zByte = (u
8fcf0 38 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  8*)sqlite3Global
8fd00 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a 20 20  Config.pHeap;.  
8fd10 61 73 73 65 72 74 28 20 7a 42 79 74 65 21 3d 30  assert( zByte!=0
8fd20 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f   );  /* sqlite3_
8fd30 63 6f 6e 66 69 67 28 29 20 64 6f 65 73 20 6e 6f  config() does no
8fd40 74 20 61 6c 6c 6f 77 20 6f 74 68 65 72 77 69 73  t allow otherwis
8fd50 65 20 2a 2f 0a 0a 20 20 6e 4d 69 6e 4c 6f 67 20  e */..  nMinLog 
8fd60 3d 20 6d 65 6d 73 79 73 35 4c 6f 67 28 73 71 6c  = memsys5Log(sql
8fd70 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
8fd80 2e 6d 6e 52 65 71 29 3b 0a 20 20 6d 65 6d 35 2e  .mnReq);.  mem5.
8fd90 73 7a 41 74 6f 6d 20 3d 20 28 31 3c 3c 6e 4d 69  szAtom = (1<<nMi
8fda0 6e 4c 6f 67 29 3b 0a 20 20 77 68 69 6c 65 28 20  nLog);.  while( 
8fdb0 28 69 6e 74 29 73 69 7a 65 6f 66 28 4d 65 6d 35  (int)sizeof(Mem5
8fdc0 4c 69 6e 6b 29 3e 6d 65 6d 35 2e 73 7a 41 74 6f  Link)>mem5.szAto
8fdd0 6d 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 73 7a  m ){.    mem5.sz
8fde0 41 74 6f 6d 20 3d 20 6d 65 6d 35 2e 73 7a 41 74  Atom = mem5.szAt
8fdf0 6f 6d 20 3c 3c 20 31 3b 0a 20 20 7d 0a 0a 20 20  om << 1;.  }..  
8fe00 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 3d 20 28 6e  mem5.nBlock = (n
8fe10 42 79 74 65 20 2f 20 28 6d 65 6d 35 2e 73 7a 41  Byte / (mem5.szA
8fe20 74 6f 6d 2b 73 69 7a 65 6f 66 28 75 38 29 29 29  tom+sizeof(u8)))
8fe30 3b 0a 20 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 3d  ;.  mem5.zPool =
8fe40 20 7a 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e 61   zByte;.  mem5.a
8fe50 43 74 72 6c 20 3d 20 28 75 38 20 2a 29 26 6d 65  Ctrl = (u8 *)&me
8fe60 6d 35 2e 7a 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42  m5.zPool[mem5.nB
8fe70 6c 6f 63 6b 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d  lock*mem5.szAtom
8fe80 5d 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  ];..  for(ii=0; 
8fe90 69 69 3c 3d 4c 4f 47 4d 41 58 3b 20 69 69 2b 2b  ii<=LOGMAX; ii++
8fea0 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72  ){.    mem5.aiFr
8feb0 65 65 6c 69 73 74 5b 69 69 5d 20 3d 20 2d 31 3b  eelist[ii] = -1;
8fec0 0a 20 20 7d 0a 0a 20 20 69 4f 66 66 73 65 74 20  .  }..  iOffset 
8fed0 3d 20 30 3b 0a 20 20 66 6f 72 28 69 69 3d 4c 4f  = 0;.  for(ii=LO
8fee0 47 4d 41 58 3b 20 69 69 3e 3d 30 3b 20 69 69 2d  GMAX; ii>=0; ii-
8fef0 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c  -){.    int nAll
8ff00 6f 63 20 3d 20 28 31 3c 3c 69 69 29 3b 0a 20 20  oc = (1<<ii);.  
8ff10 20 20 69 66 28 20 28 69 4f 66 66 73 65 74 2b 6e    if( (iOffset+n
8ff20 41 6c 6c 6f 63 29 3c 3d 6d 65 6d 35 2e 6e 42 6c  Alloc)<=mem5.nBl
8ff30 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  ock ){.      mem
8ff40 35 2e 61 43 74 72 6c 5b 69 4f 66 66 73 65 74 5d  5.aCtrl[iOffset]
8ff50 20 3d 20 69 69 20 7c 20 43 54 52 4c 5f 46 52 45   = ii | CTRL_FRE
8ff60 45 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 35  E;.      memsys5
8ff70 4c 69 6e 6b 28 69 4f 66 66 73 65 74 2c 20 69 69  Link(iOffset, ii
8ff80 29 3b 0a 20 20 20 20 20 20 69 4f 66 66 73 65 74  );.      iOffset
8ff90 20 2b 3d 20 6e 41 6c 6c 6f 63 3b 0a 20 20 20 20   += nAlloc;.    
8ffa0 7d 0a 20 20 20 20 61 73 73 65 72 74 28 28 69 4f  }.    assert((iO
8ffb0 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 29 3e 6d 65  ffset+nAlloc)>me
8ffc0 6d 35 2e 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a  m5.nBlock);.  }.
8ffd0 0a 20 20 2f 2a 20 49 66 20 61 20 6d 75 74 65 78  .  /* If a mutex
8ffe0 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72   is required for
8fff0 20 6e 6f 72 6d 61 6c 20 6f 70 65 72 61 74 69 6f   normal operatio
90000 6e 2c 20 61 6c 6c 6f 63 61 74 65 20 6f 6e 65 20  n, allocate one 
90010 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
90020 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
90030 6d 73 74 61 74 3d 3d 30 20 29 7b 0a 20 20 20 20  mstat==0 ){.    
90040 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20 73 71 6c  mem5.mutex = sql
90050 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
90060 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
90070 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 0a 20 20  IC_MEM);.  }..  
90080 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
90090 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69  ;.}../*.** Deini
900a0 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64  tialize this mod
900b0 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ule..*/.static v
900c0 6f 69 64 20 6d 65 6d 73 79 73 35 53 68 75 74 64  oid memsys5Shutd
900d0 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65  own(void *NotUse
900e0 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  d){.  UNUSED_PAR
900f0 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
90100 0a 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20  .  mem5.mutex = 
90110 30 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a  0;.  return;.}..
90120 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
90130 53 54 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68  ST./*.** Open th
90140 65 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64  e file indicated
90150 20 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67   and write a log
90160 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20   of all unfreed 
90170 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63  memory .** alloc
90180 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74  ations into that
90190 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   log..*/.SQLITE_
901a0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
901b0 69 74 65 33 4d 65 6d 73 79 73 35 44 75 6d 70 28  ite3Memsys5Dump(
901c0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
901d0 65 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45 20 2a  ename){.  FILE *
901e0 6f 75 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c  out;.  int i, j,
901f0 20 6e 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f   n;.  int nMinLo
90200 67 3b 0a 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e  g;..  if( zFilen
90210 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e  ame==0 || zFilen
90220 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20  ame[0]==0 ){.   
90230 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20   out = stdout;. 
90240 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20   }else{.    out 
90250 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d  = fopen(zFilenam
90260 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28  e, "w");.    if(
90270 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20   out==0 ){.     
90280 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
90290 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f   "** Unable to o
902a0 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62  utput memory deb
902b0 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25  ug output log: %
902c0 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20  s **\n",.       
902d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a                 z
902e0 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20  Filename);.     
902f0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
90300 20 7d 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65   }.  memsys5Ente
90310 72 28 29 3b 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d  r();.  nMinLog =
90320 20 6d 65 6d 73 79 73 35 4c 6f 67 28 6d 65 6d 35   memsys5Log(mem5
90330 2e 73 7a 41 74 6f 6d 29 3b 0a 20 20 66 6f 72 28  .szAtom);.  for(
90340 69 3d 30 3b 20 69 3c 3d 4c 4f 47 4d 41 58 20 26  i=0; i<=LOGMAX &
90350 26 20 69 2b 6e 4d 69 6e 4c 6f 67 3c 33 32 3b 20  & i+nMinLog<32; 
90360 69 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6e 3d  i++){.    for(n=
90370 30 2c 20 6a 3d 6d 65 6d 35 2e 61 69 46 72 65 65  0, j=mem5.aiFree
90380 6c 69 73 74 5b 69 5d 3b 20 6a 3e 3d 30 3b 20 6a  list[i]; j>=0; j
90390 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 6a 29 2d 3e   = MEM5LINK(j)->
903a0 6e 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20  next, n++){}.   
903b0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 66   fprintf(out, "f
903c0 72 65 65 6c 69 73 74 20 69 74 65 6d 73 20 6f 66  reelist items of
903d0 20 73 69 7a 65 20 25 64 3a 20 25 64 5c 6e 22 2c   size %d: %d\n",
903e0 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3c 3c 20   mem5.szAtom << 
903f0 69 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 66 70 72  i, n);.  }.  fpr
90400 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e  intf(out, "mem5.
90410 6e 41 6c 6c 6f 63 20 20 20 20 20 20 20 3d 20 25  nAlloc       = %
90420 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6e 41 6c  llu\n", mem5.nAl
90430 6c 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28  loc);.  fprintf(
90440 6f 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c  out, "mem5.total
90450 41 6c 6c 6f 63 20 20 20 3d 20 25 6c 6c 75 5c 6e  Alloc   = %llu\n
90460 22 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c  ", mem5.totalAll
90470 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  oc);.  fprintf(o
90480 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 45  ut, "mem5.totalE
90490 78 63 65 73 73 20 20 3d 20 25 6c 6c 75 5c 6e 22  xcess  = %llu\n"
904a0 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65  , mem5.totalExce
904b0 73 73 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  ss);.  fprintf(o
904c0 75 74 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e  ut, "mem5.curren
904d0 74 4f 75 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20  tOut   = %u\n", 
904e0 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 29  mem5.currentOut)
904f0 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c  ;.  fprintf(out,
90500 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f   "mem5.currentCo
90510 75 6e 74 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d  unt = %u\n", mem
90520 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 29 3b  5.currentCount);
90530 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20  .  fprintf(out, 
90540 22 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 20 20 20  "mem5.maxOut    
90550 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35     = %u\n", mem5
90560 2e 6d 61 78 4f 75 74 29 3b 0a 20 20 66 70 72 69  .maxOut);.  fpri
90570 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d  ntf(out, "mem5.m
90580 61 78 43 6f 75 6e 74 20 20 20 20 20 3d 20 25 75  axCount     = %u
90590 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 43 6f 75  \n", mem5.maxCou
905a0 6e 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  nt);.  fprintf(o
905b0 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 52 65 71  ut, "mem5.maxReq
905c0 75 65 73 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20  uest   = %u\n", 
905d0 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 29  mem5.maxRequest)
905e0 3b 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65  ;.  memsys5Leave
905f0 28 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73  ();.  if( out==s
90600 74 64 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 6c  tdout ){.    ffl
90610 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d  ush(stdout);.  }
90620 65 6c 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65  else{.    fclose
90630 28 6f 75 74 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  (out);.  }.}.#en
90640 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  dif../*.** This 
90650 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f  routine is the o
90660 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74  nly routine in t
90670 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78  his file with ex
90680 74 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61  ternal .** linka
90690 67 65 2e 20 49 74 20 72 65 74 75 72 6e 73 20 61  ge. It returns a
906a0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   pointer to a st
906b0 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 65 6d  atic sqlite3_mem
906c0 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 73 74 72 75  _methods.** stru
906d0 63 74 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74  ct populated wit
906e0 68 20 74 68 65 20 6d 65 6d 73 79 73 35 20 6d 65  h the memsys5 me
906f0 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  thods..*/.SQLITE
90700 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73  _PRIVATE const s
90710 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
90720 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65  ds *sqlite3MemGe
90730 74 4d 65 6d 73 79 73 35 28 76 6f 69 64 29 7b 0a  tMemsys5(void){.
90740 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
90750 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
90760 64 73 20 6d 65 6d 73 79 73 35 4d 65 74 68 6f 64  ds memsys5Method
90770 73 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79  s = {.     memsy
90780 73 35 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d  s5Malloc,.     m
90790 65 6d 73 79 73 35 46 72 65 65 2c 0a 20 20 20 20  emsys5Free,.    
907a0 20 6d 65 6d 73 79 73 35 52 65 61 6c 6c 6f 63 2c   memsys5Realloc,
907b0 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 53 69 7a  .     memsys5Siz
907c0 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52  e,.     memsys5R
907d0 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d  oundup,.     mem
907e0 73 79 73 35 49 6e 69 74 2c 0a 20 20 20 20 20 6d  sys5Init,.     m
907f0 65 6d 73 79 73 35 53 68 75 74 64 6f 77 6e 2c 0a  emsys5Shutdown,.
90800 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65       0.  };.  re
90810 74 75 72 6e 20 26 6d 65 6d 73 79 73 35 4d 65 74  turn &memsys5Met
90820 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  hods;.}..#endif 
90830 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  /* SQLITE_ENABLE
90840 5f 4d 45 4d 53 59 53 35 20 2a 2f 0a 0a 2f 2a 2a  _MEMSYS5 */../**
90850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
90860 20 6f 66 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a   of mem5.c *****
90870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
908a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
908b0 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 63 20  in file mutex.c 
908c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
908d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
908e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
908f0 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31  ** 2007 August 1
90900 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  4.**.** The auth
90910 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
90920 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
90930 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
90940 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
90950 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
90960 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
90970 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
90980 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
90990 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
909a0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
909b0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
909c0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
909d0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
909e0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
909f0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
90a00 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
90a10 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
90a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90a60 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
90a70 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e  ntains the C fun
90a80 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c  ctions that impl
90a90 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a  ement mutexes..*
90aa0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
90ab0 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61  ontains code tha
90ac0 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 61 63 72 6f  t is common acro
90ad0 73 73 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70  ss all mutex imp
90ae0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 0a 2a  lementations...*
90af0 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 2e  *.** $Id: mutex.
90b00 63 2c 76 20 31 2e 33 31 20 32 30 30 39 2f 30 37  c,v 1.31 2009/07
90b10 2f 31 36 20 31 38 3a 32 31 3a 31 38 20 64 72 68  /16 18:21:18 drh
90b20 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 64   Exp $.*/..#if d
90b30 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
90b40 42 55 47 29 20 26 26 20 21 64 65 66 69 6e 65 64  BUG) && !defined
90b50 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d  (SQLITE_MUTEX_OM
90b60 49 54 29 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 64 65  IT)./*.** For de
90b70 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 65 73  bugging purposes
90b80 2c 20 72 65 63 6f 72 64 20 77 68 65 6e 20 74 68  , record when th
90b90 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65  e mutex subsyste
90ba0 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  m is initialized
90bb0 0a 2a 2a 20 61 6e 64 20 75 6e 69 6e 69 74 69 61  .** and uninitia
90bc0 6c 69 7a 65 64 20 73 6f 20 74 68 61 74 20 77 65  lized so that we
90bd0 20 63 61 6e 20 61 73 73 65 72 74 28 29 20 69 66   can assert() if
90be0 20 74 68 65 72 65 20 69 73 20 61 6e 20 61 74 74   there is an att
90bf0 65 6d 70 74 20 74 6f 0a 2a 2a 20 61 6c 6c 6f 63  empt to.** alloc
90c00 61 74 65 20 61 20 6d 75 74 65 78 20 77 68 69 6c  ate a mutex whil
90c10 65 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20  e the system is 
90c20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a  uninitialized..*
90c30 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f  /.static SQLITE_
90c40 57 53 44 20 69 6e 74 20 6d 75 74 65 78 49 73 49  WSD int mutexIsI
90c50 6e 69 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 20  nit = 0;.#endif 
90c60 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  /* SQLITE_DEBUG 
90c70 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  */...#ifndef SQL
90c80 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f  ITE_MUTEX_OMIT./
90c90 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
90ca0 74 68 65 20 6d 75 74 65 78 20 73 79 73 74 65 6d  the mutex system
90cb0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
90cc0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  ATE int sqlite3M
90cd0 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20  utexInit(void){ 
90ce0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
90cf0 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 73 71 6c  TE_OK;.  if( sql
90d00 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
90d10 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20  .bCoreMutex ){. 
90d20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47     if( !sqlite3G
90d30 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
90d40 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 29 7b  x.xMutexAlloc ){
90d50 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
90d60 20 78 4d 75 74 65 78 41 6c 6c 6f 63 20 6d 65 74   xMutexAlloc met
90d70 68 6f 64 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  hod has not been
90d80 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 75   set, then the u
90d90 73 65 72 20 64 69 64 20 6e 6f 74 0a 20 20 20 20  ser did not.    
90da0 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 61 20 6d    ** install a m
90db0 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74  utex implementat
90dc0 69 6f 6e 20 76 69 61 20 73 71 6c 69 74 65 33 5f  ion via sqlite3_
90dd0 63 6f 6e 66 69 67 28 29 20 70 72 69 6f 72 20 74  config() prior t
90de0 6f 20 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69  o .      ** sqli
90df0 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
90e00 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 2e 20 54   being called. T
90e10 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 65 73  his block copies
90e20 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 20 20 20   pointers to.   
90e30 20 20 20 2a 2a 20 74 68 65 20 64 65 66 61 75 6c     ** the defaul
90e40 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  t implementation
90e50 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65   into the sqlite
90e60 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 73 74  3GlobalConfig st
90e70 72 75 63 74 75 72 65 2e 0a 20 20 20 20 20 20 2a  ructure..      *
90e80 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  /.      sqlite3_
90e90 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 70  mutex_methods *p
90ea0 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33 44 65  From = sqlite3De
90eb0 66 61 75 6c 74 4d 75 74 65 78 28 29 3b 0a 20 20  faultMutex();.  
90ec0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
90ed0 78 5f 6d 65 74 68 6f 64 73 20 2a 70 54 6f 20 3d  x_methods *pTo =
90ee0 20 26 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43   &sqlite3GlobalC
90ef0 6f 6e 66 69 67 2e 6d 75 74 65 78 3b 0a 0a 20 20  onfig.mutex;..  
90f00 20 20 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20      memcpy(pTo, 
90f10 70 46 72 6f 6d 2c 20 6f 66 66 73 65 74 6f 66 28  pFrom, offsetof(
90f20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
90f30 74 68 6f 64 73 2c 20 78 4d 75 74 65 78 41 6c 6c  thods, xMutexAll
90f40 6f 63 29 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63  oc));.      memc
90f50 70 79 28 26 70 54 6f 2d 3e 78 4d 75 74 65 78 46  py(&pTo->xMutexF
90f60 72 65 65 2c 20 26 70 46 72 6f 6d 2d 3e 78 4d 75  ree, &pFrom->xMu
90f70 74 65 78 46 72 65 65 2c 0a 20 20 20 20 20 20 20  texFree,.       
90f80 20 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70 54        sizeof(*pT
90f90 6f 29 20 2d 20 6f 66 66 73 65 74 6f 66 28 73 71  o) - offsetof(sq
90fa0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
90fb0 6f 64 73 2c 20 78 4d 75 74 65 78 46 72 65 65 29  ods, xMutexFree)
90fc0 29 3b 0a 20 20 20 20 20 20 70 54 6f 2d 3e 78 4d  );.      pTo->xM
90fd0 75 74 65 78 41 6c 6c 6f 63 20 3d 20 70 46 72 6f  utexAlloc = pFro
90fe0 6d 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 3b 0a  m->xMutexAlloc;.
90ff0 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73      }.    rc = s
91000 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
91010 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 49  ig.mutex.xMutexI
91020 6e 69 74 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 64  nit();.  }..#ifd
91030 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
91040 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d 75    GLOBAL(int, mu
91050 74 65 78 49 73 49 6e 69 74 29 20 3d 20 31 3b 0a  texIsInit) = 1;.
91060 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e  #endif..  return
91070 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68   rc;.}../*.** Sh
91080 75 74 64 6f 77 6e 20 74 68 65 20 6d 75 74 65 78  utdown the mutex
91090 20 73 79 73 74 65 6d 2e 20 54 68 69 73 20 63 61   system. This ca
910a0 6c 6c 20 66 72 65 65 73 20 72 65 73 6f 75 72 63  ll frees resourc
910b0 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a  es allocated by.
910c0 2a 2a 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49  ** sqlite3MutexI
910d0 6e 69 74 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nit()..*/.SQLITE
910e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
910f0 69 74 65 33 4d 75 74 65 78 45 6e 64 28 76 6f 69  ite3MutexEnd(voi
91100 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  d){.  int rc = S
91110 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
91120 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
91130 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78  fig.mutex.xMutex
91140 45 6e 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  End ){.    rc = 
91150 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
91160 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78  fig.mutex.xMutex
91170 45 6e 64 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 64  End();.  }..#ifd
91180 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
91190 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d 75    GLOBAL(int, mu
911a0 74 65 78 49 73 49 6e 69 74 29 20 3d 20 30 3b 0a  texIsInit) = 0;.
911b0 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e  #endif..  return
911c0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
911d0 74 72 69 65 76 65 20 61 20 70 6f 69 6e 74 65 72  trieve a pointer
911e0 20 74 6f 20 61 20 73 74 61 74 69 63 20 6d 75 74   to a static mut
911f0 65 78 20 6f 72 20 61 6c 6c 6f 63 61 74 65 20 61  ex or allocate a
91200 20 6e 65 77 20 64 79 6e 61 6d 69 63 20 6f 6e 65   new dynamic one
91210 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
91220 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73  sqlite3_mutex *s
91230 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
91240 6f 63 28 69 6e 74 20 69 64 29 7b 0a 23 69 66 6e  oc(int id){.#ifn
91250 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
91260 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73  AUTOINIT.  if( s
91270 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
91280 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  e() ) return 0;.
91290 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
912a0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
912b0 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78  fig.mutex.xMutex
912c0 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a 53 51  Alloc(id);.}..SQ
912d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
912e0 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69  ite3_mutex *sqli
912f0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e  te3MutexAlloc(in
91300 74 20 69 64 29 7b 0a 20 20 69 66 28 20 21 73 71  t id){.  if( !sq
91310 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
91320 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a  g.bCoreMutex ){.
91330 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
91340 7d 0a 20 20 61 73 73 65 72 74 28 20 47 4c 4f 42  }.  assert( GLOB
91350 41 4c 28 69 6e 74 2c 20 6d 75 74 65 78 49 73 49  AL(int, mutexIsI
91360 6e 69 74 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  nit) );.  return
91370 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
91380 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65  nfig.mutex.xMute
91390 78 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a 2f  xAlloc(id);.}../
913a0 2a 0a 2a 2a 20 46 72 65 65 20 61 20 64 79 6e 61  *.** Free a dyna
913b0 6d 69 63 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51  mic mutex..*/.SQ
913c0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
913d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65  lite3_mutex_free
913e0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
913f0 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  p){.  if( p ){. 
91400 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
91410 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75  Config.mutex.xMu
91420 74 65 78 46 72 65 65 28 70 29 3b 0a 20 20 7d 0a  texFree(p);.  }.
91430 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20  }../*.** Obtain 
91440 74 68 65 20 6d 75 74 65 78 20 70 2e 20 49 66 20  the mutex p. If 
91450 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 65 61  some other threa
91460 64 20 61 6c 72 65 61 64 79 20 68 61 73 20 74 68  d already has th
91470 65 20 6d 75 74 65 78 2c 20 62 6c 6f 63 6b 0a 2a  e mutex, block.*
91480 2a 20 75 6e 74 69 6c 20 69 74 20 63 61 6e 20 62  * until it can b
91490 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f 0a 53  e obtained..*/.S
914a0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
914b0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
914c0 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  er(sqlite3_mutex
914d0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
914e0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
914f0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
91500 4d 75 74 65 78 45 6e 74 65 72 28 70 29 3b 0a 20  MutexEnter(p);. 
91510 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61   }.}../*.** Obta
91520 69 6e 20 74 68 65 20 6d 75 74 65 78 20 70 2e 20  in the mutex p. 
91530 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72  If successful, r
91540 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
91550 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61   Otherwise, if a
91560 6e 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65 61 64  nother.** thread
91570 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74 65 78   holds the mutex
91580 20 61 6e 64 20 69 74 20 63 61 6e 6e 6f 74 20 62   and it cannot b
91590 65 20 6f 62 74 61 69 6e 65 64 2c 20 72 65 74 75  e obtained, retu
915a0 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a  rn SQLITE_BUSY..
915b0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
915c0 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  t sqlite3_mutex_
915d0 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  try(sqlite3_mute
915e0 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20  x *p){.  int rc 
915f0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
91600 66 28 20 70 20 29 7b 0a 20 20 20 20 72 65 74 75  f( p ){.    retu
91610 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  rn sqlite3Global
91620 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75  Config.mutex.xMu
91630 74 65 78 54 72 79 28 70 29 3b 0a 20 20 7d 0a 20  texTry(p);.  }. 
91640 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
91650 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
91660 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72  _mutex_leave() r
91670 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d  outine exits a m
91680 75 74 65 78 20 74 68 61 74 20 77 61 73 20 70 72  utex that was pr
91690 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 65 6e 74 65  eviously.** ente
916a0 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20  red by the same 
916b0 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 68  thread.  The beh
916c0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
916d0 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20  ed if the mutex 
916e0 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65  .** is not curre
916f0 6e 74 6c 79 20 65 6e 74 65 72 65 64 2e 20 49 66  ntly entered. If
91700 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20   a NULL pointer 
91710 69 73 20 70 61 73 73 65 64 20 61 73 20 61 6e 20  is passed as an 
91720 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 68 69 73  argument.** this
91730 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
91740 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  o-op..*/.SQLITE_
91750 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
91760 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c  _mutex_leave(sql
91770 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
91780 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73    if( p ){.    s
91790 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
917a0 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 4c  ig.mutex.xMutexL
917b0 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  eave(p);.  }.}..
917c0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f  #ifndef NDEBUG./
917d0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
917e0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e  _mutex_held() an
917f0 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
91800 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e  notheld() routin
91810 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65  e are.** intende
91820 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65  d for use inside
91830 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
91840 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ents..*/.SQLITE_
91850 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
91860 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74  mutex_held(sqlit
91870 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
91880 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 73  return p==0 || s
91890 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
918a0 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 48  ig.mutex.xMutexH
918b0 65 6c 64 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45  eld(p);.}.SQLITE
918c0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
918d0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 73  _mutex_notheld(s
918e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
918f0 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20  {.  return p==0 
91900 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  || sqlite3Global
91910 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75  Config.mutex.xMu
91920 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 3b 0a 7d  texNotheld(p);.}
91930 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20  .#endif..#endif 
91940 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  /* SQLITE_MUTEX_
91950 4f 4d 49 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  OMIT */../******
91960 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
91970 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  mutex.c ********
91980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
919a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
919b0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
919c0 69 6c 65 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63  ile mutex_noop.c
919d0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
919e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
919f0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
91a00 30 30 38 20 4f 63 74 6f 62 65 72 20 30 37 0a 2a  008 October 07.*
91a10 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
91a20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
91a30 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
91a40 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
91a50 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
91a60 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
91a70 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
91a80 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
91a90 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
91aa0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
91ab0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
91ac0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
91ad0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
91ae0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
91af0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
91b00 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
91b10 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
91b20 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
91b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
91b70 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
91b80 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69  ins the C functi
91b90 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ons that impleme
91ba0 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a  nt mutexes..**.*
91bb0 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74  * This implement
91bc0 61 74 69 6f 6e 20 69 6e 20 74 68 69 73 20 66 69  ation in this fi
91bd0 6c 65 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76  le does not prov
91be0 69 64 65 20 61 6e 79 20 6d 75 74 75 61 6c 0a 2a  ide any mutual.*
91bf0 2a 20 65 78 63 6c 75 73 69 6f 6e 20 61 6e 64 20  * exclusion and 
91c00 69 73 20 74 68 75 73 20 73 75 69 74 61 62 6c 65  is thus suitable
91c10 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e   for use only in
91c20 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a   applications.**
91c30 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65   that use SQLite
91c40 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 74 68 72   in a single thr
91c50 65 61 64 2e 20 20 54 68 65 20 72 6f 75 74 69 6e  ead.  The routin
91c60 65 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20 68 65  es defined.** he
91c70 72 65 20 61 72 65 20 70 6c 61 63 65 2d 68 6f 6c  re are place-hol
91c80 64 65 72 73 2e 20 20 41 70 70 6c 69 63 61 74 69  ders.  Applicati
91c90 6f 6e 73 20 63 61 6e 20 73 75 62 73 74 69 74 75  ons can substitu
91ca0 74 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 6d 75  te working.** mu
91cb0 74 65 78 20 72 6f 75 74 69 6e 65 73 20 61 74 20  tex routines at 
91cc0 73 74 61 72 74 2d 74 69 6d 65 20 75 73 69 6e 67  start-time using
91cd0 20 74 68 65 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73   the.**.**     s
91ce0 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51  qlite3_config(SQ
91cf0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45  LITE_CONFIG_MUTE
91d00 58 2c 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 69 6e 74  X,...).**.** int
91d10 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  erface..**.** If
91d20 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 53   compiled with S
91d30 51 4c 49 54 45 5f 44 45 42 55 47 2c 20 74 68 65  QLITE_DEBUG, the
91d40 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 67  n additional log
91d50 69 63 20 69 73 20 69 6e 73 65 72 74 65 64 0a 2a  ic is inserted.*
91d60 2a 20 74 68 61 74 20 64 6f 65 73 20 65 72 72 6f  * that does erro
91d70 72 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 6d 75  r checking on mu
91d80 74 65 78 65 73 20 74 6f 20 6d 61 6b 65 20 73 75  texes to make su
91d90 72 65 20 74 68 65 79 20 61 72 65 20 62 65 69 6e  re they are bein
91da0 67 0a 2a 2a 20 63 61 6c 6c 65 64 20 63 6f 72 72  g.** called corr
91db0 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  ectly..**.** $Id
91dc0 3a 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 2c 76  : mutex_noop.c,v
91dd0 20 31 2e 33 20 32 30 30 38 2f 31 32 2f 30 35 20   1.3 2008/12/05 
91de0 31 37 3a 31 37 3a 30 38 20 64 72 68 20 45 78 70  17:17:08 drh Exp
91df0 20 24 0a 2a 2f 0a 0a 0a 23 69 66 20 64 65 66 69   $.*/...#if defi
91e00 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  ned(SQLITE_MUTEX
91e10 5f 4e 4f 4f 50 29 20 26 26 20 21 64 65 66 69 6e  _NOOP) && !defin
91e20 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
91e30 0a 2f 2a 0a 2a 2a 20 53 74 75 62 20 72 6f 75 74  ./*.** Stub rout
91e40 69 6e 65 73 20 66 6f 72 20 61 6c 6c 20 6d 75 74  ines for all mut
91e50 65 78 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 0a 2a  ex methods..**.*
91e60 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 73 20  * This routines 
91e70 70 72 6f 76 69 64 65 20 6e 6f 20 6d 75 74 75 61  provide no mutua
91e80 6c 20 65 78 63 6c 75 73 69 6f 6e 20 6f 72 20 65  l exclusion or e
91e90 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 2e 0a 2a  rror checking..*
91ea0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f  /.static int noo
91eb0 70 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74  pMutexHeld(sqlit
91ec0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 65  e3_mutex *p){ re
91ed0 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74 69 63  turn 1; }.static
91ee0 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 4e 6f   int noopMutexNo
91ef0 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75  theld(sqlite3_mu
91f00 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 20  tex *p){ return 
91f10 31 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  1; }.static int 
91f20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 28 76 6f  noopMutexInit(vo
91f30 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49  id){ return SQLI
91f40 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20  TE_OK; }.static 
91f50 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 64  int noopMutexEnd
91f60 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53  (void){ return S
91f70 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74  QLITE_OK; }.stat
91f80 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
91f90 20 2a 6e 6f 6f 70 4d 75 74 65 78 41 6c 6c 6f 63   *noopMutexAlloc
91fa0 28 69 6e 74 20 69 64 29 7b 20 72 65 74 75 72 6e  (int id){ return
91fb0 20 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a   (sqlite3_mutex*
91fc0 29 38 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69  )8; }.static voi
91fd0 64 20 6e 6f 6f 70 4d 75 74 65 78 46 72 65 65 28  d noopMutexFree(
91fe0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
91ff0 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61  ){ return; }.sta
92000 74 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74  tic void noopMut
92010 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f  exEnter(sqlite3_
92020 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72  mutex *p){ retur
92030 6e 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  n; }.static int 
92040 6e 6f 6f 70 4d 75 74 65 78 54 72 79 28 73 71 6c  noopMutexTry(sql
92050 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20  ite3_mutex *p){ 
92060 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
92070 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ; }.static void 
92080 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 76 65 28 73  noopMutexLeave(s
92090 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
920a0 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 0a 53 51 4c  { return; }..SQL
920b0 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69  ITE_PRIVATE sqli
920c0 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
920d0 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c  s *sqlite3Defaul
920e0 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20  tMutex(void){.  
920f0 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d  static sqlite3_m
92100 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75  utex_methods sMu
92110 74 65 78 20 3d 20 7b 0a 20 20 20 20 6e 6f 6f 70  tex = {.    noop
92120 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 6e  MutexInit,.    n
92130 6f 6f 70 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20  oopMutexEnd,.   
92140 20 6e 6f 6f 70 4d 75 74 65 78 41 6c 6c 6f 63 2c   noopMutexAlloc,
92150 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 46 72  .    noopMutexFr
92160 65 65 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65  ee,.    noopMute
92170 78 45 6e 74 65 72 2c 0a 20 20 20 20 6e 6f 6f 70  xEnter,.    noop
92180 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 6e 6f  MutexTry,.    no
92190 6f 70 4d 75 74 65 78 4c 65 61 76 65 2c 0a 0a 20  opMutexLeave,.. 
921a0 20 20 20 6e 6f 6f 70 4d 75 74 65 78 48 65 6c 64     noopMutexHeld
921b0 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4e  ,.    noopMutexN
921c0 6f 74 68 65 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72  otheld.  };..  r
921d0 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d  eturn &sMutex;.}
921e0 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e  .#endif /* defin
921f0 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ed(SQLITE_MUTEX_
92200 4e 4f 4f 50 29 20 26 26 20 21 64 65 66 69 6e 65  NOOP) && !define
92210 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20  d(SQLITE_DEBUG) 
92220 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  */..#if defined(
92230 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f  SQLITE_MUTEX_NOO
92240 50 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51  P) && defined(SQ
92250 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a  LITE_DEBUG)./*.*
92260 2a 20 49 6e 20 74 68 69 73 20 69 6d 70 6c 65 6d  * In this implem
92270 65 6e 74 61 74 69 6f 6e 2c 20 65 72 72 6f 72 20  entation, error 
92280 63 68 65 63 6b 69 6e 67 20 69 73 20 70 72 6f 76  checking is prov
92290 69 64 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  ided for testing
922a0 0a 2a 2a 20 61 6e 64 20 64 65 62 75 67 67 69 6e  .** and debuggin
922b0 67 20 70 75 72 70 6f 73 65 73 2e 20 20 54 68 65  g purposes.  The
922c0 20 6d 75 74 65 78 65 73 20 73 74 69 6c 6c 20 64   mutexes still d
922d0 6f 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 6e  o not provide an
922e0 79 0a 2a 2a 20 6d 75 74 75 61 6c 20 65 78 63 6c  y.** mutual excl
922f0 75 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  usion..*/../*.**
92300 20 54 68 65 20 6d 75 74 65 78 20 6f 62 6a 65 63   The mutex objec
92310 74 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  t.*/.struct sqli
92320 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 69 6e  te3_mutex {.  in
92330 74 20 69 64 3b 20 20 20 20 20 2f 2a 20 54 68 65  t id;     /* The
92340 20 6d 75 74 65 78 20 74 79 70 65 20 2a 2f 0a 20   mutex type */. 
92350 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 2f 2a 20   int cnt;    /* 
92360 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
92370 73 20 77 69 74 68 6f 75 74 20 61 20 6d 61 74 63  s without a matc
92380 68 69 6e 67 20 6c 65 61 76 65 20 2a 2f 0a 7d 3b  hing leave */.};
92390 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ../*.** The sqli
923a0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29  te3_mutex_held()
923b0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74   and sqlite3_mut
923c0 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75  ex_notheld() rou
923d0 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65  tine are.** inte
923e0 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73  nded for use ins
923f0 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61  ide assert() sta
92400 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74  tements..*/.stat
92410 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65  ic int debugMute
92420 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75  xHeld(sqlite3_mu
92430 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  tex *p){.  retur
92440 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 63 6e 74  n p==0 || p->cnt
92450 3e 30 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  >0;.}.static int
92460 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65   debugMutexNothe
92470 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
92480 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70   *p){.  return p
92490 3d 3d 30 20 7c 7c 20 70 2d 3e 63 6e 74 3d 3d 30  ==0 || p->cnt==0
924a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  ;.}../*.** Initi
924b0 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74  alize and deinit
924c0 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78  ialize the mutex
924d0 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73   subsystem..*/.s
924e0 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d  tatic int debugM
924f0 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20  utexInit(void){ 
92500 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
92510 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64  ; }.static int d
92520 65 62 75 67 4d 75 74 65 78 45 6e 64 28 76 6f 69  ebugMutexEnd(voi
92530 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54  d){ return SQLIT
92540 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK; }../*.** T
92550 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
92560 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65  _alloc() routine
92570 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77   allocates a new
92580 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65  .** mutex and re
92590 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
925a0 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72 65  to it.  If it re
925b0 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68  turns NULL.** th
925c0 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20  at means that a 
925d0 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20  mutex could not 
925e0 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 0a 2a  be allocated. .*
925f0 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33  /.static sqlite3
92600 5f 6d 75 74 65 78 20 2a 64 65 62 75 67 4d 75 74  _mutex *debugMut
92610 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b  exAlloc(int id){
92620 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65  .  static sqlite
92630 33 5f 6d 75 74 65 78 20 61 53 74 61 74 69 63 5b  3_mutex aStatic[
92640 36 5d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  6];.  sqlite3_mu
92650 74 65 78 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20  tex *pNew = 0;. 
92660 20 73 77 69 74 63 68 28 20 69 64 20 29 7b 0a 20   switch( id ){. 
92670 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d     case SQLITE_M
92680 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63  UTEX_FAST:.    c
92690 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ase SQLITE_MUTEX
926a0 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20  _RECURSIVE: {.  
926b0 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74      pNew = sqlit
926c0 65 33 4d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  e3Malloc(sizeof(
926d0 2a 70 4e 65 77 29 29 3b 0a 20 20 20 20 20 20 69  *pNew));.      i
926e0 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20  f( pNew ){.     
926f0 20 20 20 70 4e 65 77 2d 3e 69 64 20 3d 20 69 64     pNew->id = id
92700 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e  ;.        pNew->
92710 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  cnt = 0;.      }
92720 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
92730 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a    }.    default:
92740 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
92750 20 69 64 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20   id-2 >= 0 );.  
92760 20 20 20 20 61 73 73 65 72 74 28 20 69 64 2d 32      assert( id-2
92770 20 3c 20 28 69 6e 74 29 28 73 69 7a 65 6f 66 28   < (int)(sizeof(
92780 61 53 74 61 74 69 63 29 2f 73 69 7a 65 6f 66 28  aStatic)/sizeof(
92790 61 53 74 61 74 69 63 5b 30 5d 29 29 20 29 3b 0a  aStatic[0])) );.
927a0 20 20 20 20 20 20 70 4e 65 77 20 3d 20 26 61 53        pNew = &aS
927b0 74 61 74 69 63 5b 69 64 2d 32 5d 3b 0a 20 20 20  tatic[id-2];.   
927c0 20 20 20 70 4e 65 77 2d 3e 69 64 20 3d 20 69 64     pNew->id = id
927d0 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
927e0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
927f0 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pNew;.}../*.**
92800 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65   This routine de
92810 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76  allocates a prev
92820 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64  iously allocated
92830 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 69   mutex..*/.stati
92840 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65  c void debugMute
92850 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75  xFree(sqlite3_mu
92860 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  tex *p){.  asser
92870 74 28 20 70 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a  t( p->cnt==0 );.
92880 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d    assert( p->id=
92890 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41  =SQLITE_MUTEX_FA
928a0 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c  ST || p->id==SQL
928b0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
928c0 49 56 45 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  IVE );.  sqlite3
928d0 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a  _free(p);.}../*.
928e0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  ** The sqlite3_m
928f0 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64  utex_enter() and
92900 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
92910 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74  ry() routines at
92920 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65  tempt.** to ente
92930 72 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61  r a mutex.  If a
92940 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73  nother thread is
92950 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20   already within 
92960 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71  the mutex,.** sq
92970 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
92980 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61  r() will block a
92990 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nd sqlite3_mutex
929a0 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75  _try() will retu
929b0 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53  rn.** SQLITE_BUS
929c0 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f  Y.  The sqlite3_
929d0 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65  mutex_try() inte
929e0 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 51  rface returns SQ
929f0 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20  LITE_OK.** upon 
92a00 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79  successful entry
92a10 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65 61 74  .  Mutexes creat
92a20 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f  ed using SQLITE_
92a30 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
92a40 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65  can.** be entere
92a50 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73  d multiple times
92a60 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72   by the same thr
92a70 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61  ead.  In such ca
92a80 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65  ses the,.** mute
92a90 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 64  x must be exited
92aa0 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72   an equal number
92ab0 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65   of times before
92ac0 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a   another thread.
92ad0 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49  ** can enter.  I
92ae0 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61  f the same threa
92af0 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72  d tries to enter
92b00 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20   any other kind 
92b10 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65  of mutex.** more
92b20 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20   than once, the 
92b30 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65  behavior is unde
92b40 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  fined..*/.static
92b50 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65 78   void debugMutex
92b60 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75  Enter(sqlite3_mu
92b70 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  tex *p){.  asser
92b80 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  t( p->id==SQLITE
92b90 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
92ba0 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 4e 6f   || debugMutexNo
92bb0 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d  theld(p) );.  p-
92bc0 3e 63 6e 74 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63  >cnt++;.}.static
92bd0 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 54   int debugMutexT
92be0 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ry(sqlite3_mutex
92bf0 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
92c00 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
92c10 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c  TEX_RECURSIVE ||
92c20 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65   debugMutexNothe
92c30 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e  ld(p) );.  p->cn
92c40 74 2b 2b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  t++;.  return SQ
92c50 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
92c60 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
92c70 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74  tex_leave() rout
92c80 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65  ine exits a mute
92c90 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72  x that was.** pr
92ca0 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64  eviously entered
92cb0 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72   by the same thr
92cc0 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69  ead.  The behavi
92cd0 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e  or.** is undefin
92ce0 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20  ed if the mutex 
92cf0 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  is not currently
92d00 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69   entered or.** i
92d10 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
92d20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69  allocated.  SQLi
92d30 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f  te will never do
92d40 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74   either..*/.stat
92d50 69 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74  ic void debugMut
92d60 65 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f  exLeave(sqlite3_
92d70 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73  mutex *p){.  ass
92d80 65 72 74 28 20 64 65 62 75 67 4d 75 74 65 78 48  ert( debugMutexH
92d90 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63  eld(p) );.  p->c
92da0 6e 74 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20  nt--;.  assert( 
92db0 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
92dc0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c  TEX_RECURSIVE ||
92dd0 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65   debugMutexNothe
92de0 6c 64 28 70 29 20 29 3b 0a 7d 0a 0a 53 51 4c 49  ld(p) );.}..SQLI
92df0 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74  TE_PRIVATE sqlit
92e00 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
92e10 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74   *sqlite3Default
92e20 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73  Mutex(void){.  s
92e30 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75  tatic sqlite3_mu
92e40 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74  tex_methods sMut
92e50 65 78 20 3d 20 7b 0a 20 20 20 20 64 65 62 75 67  ex = {.    debug
92e60 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 64  MutexInit,.    d
92e70 65 62 75 67 4d 75 74 65 78 45 6e 64 2c 0a 20 20  ebugMutexEnd,.  
92e80 20 20 64 65 62 75 67 4d 75 74 65 78 41 6c 6c 6f    debugMutexAllo
92e90 63 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65  c,.    debugMute
92ea0 78 46 72 65 65 2c 0a 20 20 20 20 64 65 62 75 67  xFree,.    debug
92eb0 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20  MutexEnter,.    
92ec0 64 65 62 75 67 4d 75 74 65 78 54 72 79 2c 0a 20  debugMutexTry,. 
92ed0 20 20 20 64 65 62 75 67 4d 75 74 65 78 4c 65 61     debugMutexLea
92ee0 76 65 2c 0a 0a 20 20 20 20 64 65 62 75 67 4d 75  ve,..    debugMu
92ef0 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 64 65 62  texHeld,.    deb
92f00 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20  ugMutexNotheld. 
92f10 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73   };..  return &s
92f20 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20  Mutex;.}.#endif 
92f30 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  /* defined(SQLIT
92f40 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26  E_MUTEX_NOOP) &&
92f50 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
92f60 44 45 42 55 47 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  DEBUG) */../****
92f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
92f80 66 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 20 2a  f mutex_noop.c *
92f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
92fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
92fd0 20 66 69 6c 65 20 6d 75 74 65 78 5f 6f 73 32 2e   file mutex_os2.
92fe0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
92ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
93010 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38 0a   2007 August 28.
93020 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
93030 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
93040 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
93050 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
93060 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
93070 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
93080 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
93090 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
930a0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
930b0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
930c0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
930d0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
930e0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
930f0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
93100 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
93110 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
93120 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
93130 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
93140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
93180 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
93190 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74  ains the C funct
931a0 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d  ions that implem
931b0 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20  ent mutexes for 
931c0 4f 53 2f 32 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  OS/2.**.** $Id: 
931d0 6d 75 74 65 78 5f 6f 73 32 2e 63 2c 76 20 31 2e  mutex_os2.c,v 1.
931e0 31 31 20 32 30 30 38 2f 31 31 2f 32 32 20 31 39  11 2008/11/22 19
931f0 3a 35 30 3a 35 34 20 70 77 65 69 6c 62 61 63 68  :50:54 pweilbach
93200 65 72 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a  er Exp $.*/../*.
93210 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74  ** The code in t
93220 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79  his file is only
93230 20 75 73 65 64 20 69 66 20 53 51 4c 49 54 45 5f   used if SQLITE_
93240 4d 55 54 45 58 5f 4f 53 32 20 69 73 20 64 65 66  MUTEX_OS2 is def
93250 69 6e 65 64 2e 0a 2a 2a 20 53 65 65 20 74 68 65  ined..** See the
93260 20 6d 75 74 65 78 2e 68 20 66 69 6c 65 20 66 6f   mutex.h file fo
93270 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 69  r details..*/.#i
93280 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45  fdef SQLITE_MUTE
93290 58 5f 4f 53 32 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  X_OS2../********
932a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4f  ************** O
932b0 53 2f 32 20 4d 75 74 65 78 20 49 6d 70 6c 65 6d  S/2 Mutex Implem
932c0 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a  entation *******
932d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
932e0 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65  **.** This imple
932f0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 75 74  mentation of mut
93300 65 78 65 73 20 69 73 20 62 75 69 6c 74 20 75 73  exes is built us
93310 69 6e 67 20 74 68 65 20 4f 53 2f 32 20 41 50 49  ing the OS/2 API
93320 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ..*/../*.** The 
93330 6d 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a 2a 20  mutex object.** 
93340 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d  Each recursive m
93350 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61  utex is an insta
93360 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
93370 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
93380 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  */.struct sqlite
93390 33 5f 6d 75 74 65 78 20 7b 0a 20 20 48 4d 54 58  3_mutex {.  HMTX
933a0 20 6d 75 74 65 78 3b 20 20 20 20 20 20 20 2f 2a   mutex;       /*
933b0 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69   Mutex controlli
933c0 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20  ng the lock */. 
933d0 20 69 6e 74 20 20 69 64 3b 20 20 20 20 20 20 20   int  id;       
933e0 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65     /* Mutex type
933f0 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 52 65 66 3b   */.  int  nRef;
93400 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
93410 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20  r of references 
93420 2a 2f 0a 20 20 54 49 44 20 20 6f 77 6e 65 72 3b  */.  TID  owner;
93430 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61 64         /* Thread
93440 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6d 75   holding this mu
93450 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69  tex */.};..#defi
93460 6e 65 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49  ne OS2_MUTEX_INI
93470 54 49 41 4c 49 5a 45 52 20 20 20 30 2c 30 2c 30  TIALIZER   0,0,0
93480 2c 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  ,0../*.** Initia
93490 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69  lize and deiniti
934a0 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20  alize the mutex 
934b0 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74  subsystem..*/.st
934c0 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65  atic int os2Mute
934d0 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74  xInit(void){ ret
934e0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d  urn SQLITE_OK; }
934f0 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d  .static int os2M
93500 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72  utexEnd(void){ r
93510 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
93520 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71   }../*.** The sq
93530 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
93540 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f  c() routine allo
93550 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d  cates a new.** m
93560 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73  utex and returns
93570 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74   a pointer to it
93580 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73  .  If it returns
93590 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65   NULL.** that me
935a0 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78  ans that a mutex
935b0 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c   could not be al
935c0 6c 6f 63 61 74 65 64 2e 20 0a 2a 2a 20 53 51 4c  located. .** SQL
935d0 69 74 65 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20  ite will unwind 
935e0 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 65  its stack and re
935f0 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20  turn an error.  
93600 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  The argument.** 
93610 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  to sqlite3_mutex
93620 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20  _alloc() is one 
93630 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72  of these integer
93640 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a   constants:.**.*
93650 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20  * <ul>.** <li>  
93660 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
93670 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
93680 30 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  0.** <li>  SQLIT
93690 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
936a0 45 20 20 20 20 20 20 20 20 20 20 31 0a 2a 2a 20  E          1.** 
936b0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
936c0 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
936d0 20 20 20 20 20 20 32 0a 2a 2a 20 3c 6c 69 3e 20        2.** <li> 
936e0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
936f0 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20 20  ATIC_MEM        
93700 20 33 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49   3.** <li>  SQLI
93710 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
93720 50 52 4e 47 20 20 20 20 20 20 20 20 34 0a 2a 2a  PRNG        4.**
93730 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65   </ul>.**.** The
93740 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74   first two const
93750 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74  ants cause sqlit
93760 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
93770 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20   to create.** a 
93780 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20  new mutex.  The 
93790 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63  new mutex is rec
937a0 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49  ursive when SQLI
937b0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
937c0 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75  VE.** is used bu
937d0 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c  t not necessaril
937e0 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45  y so when SQLITE
937f0 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75  _MUTEX_FAST is u
93800 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65  sed..** The mute
93810 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
93820 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
93830 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63  o make a distinc
93840 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20  tion.** between 
93850 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
93860 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54  URSIVE and SQLIT
93870 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20  E_MUTEX_FAST if 
93880 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77  it does.** not w
93890 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c  ant to.  But SQL
938a0 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65  ite will only re
938b0 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76  quest a recursiv
938c0 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61  e mutex in.** ca
938d0 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61  ses where it rea
938e0 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20  lly needs one.  
938f0 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d  If a faster non-
93900 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a  recursive mutex.
93910 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
93920 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f  n is available o
93930 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66  n the host platf
93940 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73  orm, the mutex s
93950 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68  ubsystem.** migh
93960 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20  t return such a 
93970 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73  mutex in respons
93980 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45  e to SQLITE_MUTE
93990 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68  X_FAST..**.** Th
939a0 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20  e other allowed 
939b0 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71  parameters to sq
939c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
939d0 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a  c() each return.
939e0 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
939f0 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73  a static preexis
93a00 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68 72  ting mutex.  Thr
93a10 65 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 65  ee static mutexe
93a20 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79  s are.** used by
93a30 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72   the current ver
93a40 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20  sion of SQLite. 
93a50 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73   Future versions
93a60 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61   of SQLite.** ma
93a70 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c  y add additional
93a80 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e   static mutexes.
93a90 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73    Static mutexes
93aa0 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61   are for interna
93ab0 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69  l.** use by SQLi
93ac0 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63  te only.  Applic
93ad0 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20  ations that use 
93ae0 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73  SQLite mutexes s
93af0 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c  hould.** use onl
93b00 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75  y the dynamic mu
93b10 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62  texes returned b
93b20 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  y SQLITE_MUTEX_F
93b30 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45  AST or.** SQLITE
93b40 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
93b50 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
93b60 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20  t if one of the 
93b70 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61  dynamic mutex pa
93b80 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45  rameters (SQLITE
93b90 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f  _MUTEX_FAST.** o
93ba0 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  r SQLITE_MUTEX_R
93bb0 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65  ECURSIVE) is use
93bc0 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d  d then sqlite3_m
93bd0 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20  utex_alloc().** 
93be0 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72  returns a differ
93bf0 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65  ent mutex on eve
93c00 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f  ry call.  But fo
93c10 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20  r the static.** 
93c20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65  mutex types, the
93c30 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72   same mutex is r
93c40 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79  eturned on every
93c50 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a   call that has.*
93c60 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20  * the same type 
93c70 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  number..*/.stati
93c80 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  c sqlite3_mutex 
93c90 2a 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63 28 69  *os2MutexAlloc(i
93ca0 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 71 6c  nt iType){.  sql
93cb0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 20 3d 20  ite3_mutex *p = 
93cc0 4e 55 4c 4c 3b 0a 20 20 73 77 69 74 63 68 28 20  NULL;.  switch( 
93cd0 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73  iType ){.    cas
93ce0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  e SQLITE_MUTEX_F
93cf0 41 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51  AST:.    case SQ
93d00 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
93d10 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 20  SIVE: {.      p 
93d20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a  = sqlite3MallocZ
93d30 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20  ero( sizeof(*p) 
93d40 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 29  );.      if( p )
93d50 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20  {.        p->id 
93d60 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 20  = iType;.       
93d70 20 69 66 28 20 44 6f 73 43 72 65 61 74 65 4d 75   if( DosCreateMu
93d80 74 65 78 53 65 6d 28 20 30 2c 20 26 70 2d 3e 6d  texSem( 0, &p->m
93d90 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 20 29  utex, 0, FALSE )
93da0 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   != NO_ERROR ){.
93db0 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
93dc0 33 5f 66 72 65 65 28 20 70 20 29 3b 0a 20 20 20  3_free( p );.   
93dd0 20 20 20 20 20 20 20 70 20 3d 20 4e 55 4c 4c 3b         p = NULL;
93de0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
93df0 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
93e00 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
93e10 74 3a 20 7b 0a 20 20 20 20 20 20 73 74 61 74 69  t: {.      stati
93e20 63 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 69  c volatile int i
93e30 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 20  sInit = 0;.     
93e40 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f   static sqlite3_
93e50 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 65  mutex staticMute
93e60 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20  xes[] = {.      
93e70 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e    { OS2_MUTEX_IN
93e80 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20  ITIALIZER, },.  
93e90 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45        { OS2_MUTE
93ea0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d  X_INITIALIZER, }
93eb0 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f  ,.        { OS2_
93ec0 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
93ed0 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20  R, },.        { 
93ee0 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41  OS2_MUTEX_INITIA
93ef0 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20  LIZER, },.      
93f00 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e    { OS2_MUTEX_IN
93f10 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20  ITIALIZER, },.  
93f20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45        { OS2_MUTE
93f30 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d  X_INITIALIZER, }
93f40 2c 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20  ,.      };.     
93f50 20 69 66 20 28 20 21 69 73 49 6e 69 74 20 29 7b   if ( !isInit ){
93f60 0a 20 20 20 20 20 20 20 20 41 50 49 52 45 54 20  .        APIRET 
93f70 72 63 3b 0a 20 20 20 20 20 20 20 20 50 54 49 42  rc;.        PTIB
93f80 20 70 74 69 62 3b 0a 20 20 20 20 20 20 20 20 50   ptib;.        P
93f90 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20 20 20  PIB ppib;.      
93fa0 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 0a 20 20    HMTX mutex;.  
93fb0 20 20 20 20 20 20 63 68 61 72 20 6e 61 6d 65 5b        char name[
93fc0 33 32 5d 3b 0a 20 20 20 20 20 20 20 20 44 6f 73  32];.        Dos
93fd0 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 20 26  GetInfoBlocks( &
93fe0 70 74 69 62 2c 20 26 70 70 69 62 20 29 3b 0a 20  ptib, &ppib );. 
93ff0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
94000 6e 70 72 69 6e 74 66 28 20 73 69 7a 65 6f 66 28  nprintf( sizeof(
94010 6e 61 6d 65 29 2c 20 6e 61 6d 65 2c 20 22 5c 5c  name), name, "\\
94020 53 45 4d 33 32 5c 5c 53 51 4c 49 54 45 25 30 34  SEM32\\SQLITE%04
94030 78 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  x",.            
94040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 70                pp
94050 69 62 2d 3e 70 69 62 5f 75 6c 70 69 64 20 29 3b  ib->pib_ulpid );
94060 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
94070 21 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20  !isInit ){.     
94080 20 20 20 20 20 6d 75 74 65 78 20 3d 20 30 3b 0a       mutex = 0;.
94090 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 44            rc = D
940a0 6f 73 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d  osCreateMutexSem
940b0 28 20 6e 61 6d 65 2c 20 26 6d 75 74 65 78 2c 20  ( name, &mutex, 
940c0 30 2c 20 46 41 4c 53 45 29 3b 0a 20 20 20 20 20  0, FALSE);.     
940d0 20 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20 4e       if( rc == N
940e0 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20  O_ERROR ){.     
940f0 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20         unsigned 
94100 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 20  int i;.         
94110 20 20 20 69 66 28 20 21 69 73 49 6e 69 74 20 29     if( !isInit )
94120 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
94130 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20 3c 20  for( i = 0; i < 
94140 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74  sizeof(staticMut
94150 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61  exes)/sizeof(sta
94160 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 3b 20  ticMutexes[0]); 
94170 69 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  i++ ){.         
94180 20 20 20 20 20 20 20 44 6f 73 43 72 65 61 74 65         DosCreate
94190 4d 75 74 65 78 53 65 6d 28 20 30 2c 20 26 73 74  MutexSem( 0, &st
941a0 61 74 69 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d  aticMutexes[i].m
941b0 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 20 29  utex, 0, FALSE )
941c0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
941d0 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
941e0 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20  isInit = 1;.    
941f0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
94200 20 20 20 20 20 20 44 6f 73 43 6c 6f 73 65 4d 75        DosCloseMu
94210 74 65 78 53 65 6d 28 20 6d 75 74 65 78 20 29 3b  texSem( mutex );
94220 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
94230 20 69 66 28 20 72 63 20 3d 3d 20 45 52 52 4f 52   if( rc == ERROR
94240 5f 44 55 50 4c 49 43 41 54 45 5f 4e 41 4d 45 20  _DUPLICATE_NAME 
94250 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 44  ){.            D
94260 6f 73 53 6c 65 65 70 28 20 31 20 29 3b 0a 20 20  osSleep( 1 );.  
94270 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
94280 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
94290 6e 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  n p;.          }
942a0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
942b0 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
942c0 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b   iType-2 >= 0 );
942d0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
942e0 54 79 70 65 2d 32 20 3c 20 73 69 7a 65 6f 66 28  Type-2 < sizeof(
942f0 73 74 61 74 69 63 4d 75 74 65 78 65 73 29 2f 73  staticMutexes)/s
94300 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65  izeof(staticMute
94310 78 65 73 5b 30 5d 29 20 29 3b 0a 20 20 20 20 20  xes[0]) );.     
94320 20 70 20 3d 20 26 73 74 61 74 69 63 4d 75 74 65   p = &staticMute
94330 78 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20  xes[iType-2];.  
94340 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70      p->id = iTyp
94350 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  e;.      break;.
94360 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
94370 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  rn p;.}.../*.** 
94380 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 61  This routine dea
94390 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 69  llocates a previ
943a0 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  ously allocated 
943b0 6d 75 74 65 78 2e 0a 2a 2a 20 53 51 4c 69 74 65  mutex..** SQLite
943c0 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64   is careful to d
943d0 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 20  eallocate every 
943e0 6d 75 74 65 78 20 74 68 61 74 20 69 74 20 61 6c  mutex that it al
943f0 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74  locates..*/.stat
94400 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 65 78  ic void os2Mutex
94410 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  Free(sqlite3_mut
94420 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d  ex *p){.  if( p=
94430 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61  =0 ) return;.  a
94440 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d  ssert( p->nRef==
94450 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
94460 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
94470 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64  EX_FAST || p->id
94480 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
94490 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 44 6f  ECURSIVE );.  Do
944a0 73 43 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20  sCloseMutexSem( 
944b0 70 2d 3e 6d 75 74 65 78 20 29 3b 0a 20 20 73 71  p->mutex );.  sq
944c0 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20 29 3b  lite3_free( p );
944d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  .}../*.** The sq
944e0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
944f0 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  r() and sqlite3_
94500 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74  mutex_try() rout
94510 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20  ines attempt.** 
94520 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78  to enter a mutex
94530 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68  .  If another th
94540 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20  read is already 
94550 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78  within the mutex
94560 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74  ,.** sqlite3_mut
94570 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20  ex_enter() will 
94580 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65  block and sqlite
94590 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69  3_mutex_try() wi
945a0 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  ll return.** SQL
945b0 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73  ITE_BUSY.  The s
945c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
945d0 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  () interface ret
945e0 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a  urns SQLITE_OK.*
945f0 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75  * upon successfu
94600 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65  l entry.  Mutexe
94610 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20  s created using 
94620 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
94630 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65  URSIVE can.** be
94640 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c   entered multipl
94650 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73  e times by the s
94660 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20  ame thread.  In 
94670 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a  such cases the,.
94680 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65  ** mutex must be
94690 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c   exited an equal
946a0 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73   number of times
946b0 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20   before another 
946c0 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e  thread.** can en
946d0 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d  ter.  If the sam
946e0 65 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74  e thread tries t
946f0 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65  o enter any othe
94700 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a  r kind of mutex.
94710 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63  ** more than onc
94720 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  e, the behavior 
94730 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f  is undefined..*/
94740 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32  .static void os2
94750 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74  MutexEnter(sqlit
94760 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
94770 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68  TID tid;.  PID h
94780 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20  older1;.  ULONG 
94790 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 70  holder2;.  if( p
947a0 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
947b0 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53  assert( p->id==S
947c0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
947d0 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d 75 74 65  RSIVE || os2Mute
947e0 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20  xNotheld(p) );. 
947f0 20 44 6f 73 52 65 71 75 65 73 74 4d 75 74 65 78   DosRequestMutex
94800 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 53 45  Sem(p->mutex, SE
94810 4d 5f 49 4e 44 45 46 49 4e 49 54 45 5f 57 41 49  M_INDEFINITE_WAI
94820 54 29 3b 0a 20 20 44 6f 73 51 75 65 72 79 4d 75  T);.  DosQueryMu
94830 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c  texSem(p->mutex,
94840 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 64 2c   &holder1, &tid,
94850 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 70 2d   &holder2);.  p-
94860 3e 6f 77 6e 65 72 20 3d 20 74 69 64 3b 0a 20 20  >owner = tid;.  
94870 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61  p->nRef++;.}.sta
94880 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 78  tic int os2Mutex
94890 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  Try(sqlite3_mute
948a0 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b  x *p){.  int rc;
948b0 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 20 50 49  .  TID tid;.  PI
948c0 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f  D holder1;.  ULO
948d0 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66  NG holder2;.  if
948e0 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
948f0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73  SQLITE_OK;.  ass
94900 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  ert( p->id==SQLI
94910 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
94920 56 45 20 7c 7c 20 6f 73 32 4d 75 74 65 78 4e 6f  VE || os2MutexNo
94930 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 69 66  theld(p) );.  if
94940 28 20 44 6f 73 52 65 71 75 65 73 74 4d 75 74 65  ( DosRequestMute
94950 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 53  xSem(p->mutex, S
94960 45 4d 5f 49 4d 4d 45 44 49 41 54 45 5f 52 45 54  EM_IMMEDIATE_RET
94970 55 52 4e 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52  URN) == NO_ERROR
94980 29 20 7b 0a 20 20 20 20 44 6f 73 51 75 65 72 79  ) {.    DosQuery
94990 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65  MutexSem(p->mute
949a0 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69  x, &holder1, &ti
949b0 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20  d, &holder2);.  
949c0 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74 69 64    p->owner = tid
949d0 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b  ;.    p->nRef++;
949e0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
949f0 5f 4f 4b 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a  _OK;.  } else {.
94a00 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
94a10 42 55 53 59 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  BUSY;.  }..  ret
94a20 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
94a30 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
94a40 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69  ex_leave() routi
94a50 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78  ne exits a mutex
94a60 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65   that was.** pre
94a70 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20  viously entered 
94a80 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  by the same thre
94a90 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f  ad.  The behavio
94aa0 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65  r.** is undefine
94ab0 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69  d if the mutex i
94ac0 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
94ad0 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73  entered or.** is
94ae0 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61   not currently a
94af0 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74  llocated.  SQLit
94b00 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20  e will never do 
94b10 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  either..*/.stati
94b20 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 65 78 4c  c void os2MutexL
94b30 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  eave(sqlite3_mut
94b40 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69  ex *p){.  TID ti
94b50 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31  d;.  PID holder1
94b60 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72  ;.  ULONG holder
94b70 32 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  2;.  if( p==0 ) 
94b80 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74  return;.  assert
94b90 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  ( p->nRef>0 );. 
94ba0 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65   DosQueryMutexSe
94bb0 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c  m(p->mutex, &hol
94bc0 64 65 72 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c  der1, &tid, &hol
94bd0 64 65 72 32 29 3b 0a 20 20 61 73 73 65 72 74 28  der2);.  assert(
94be0 20 70 2d 3e 6f 77 6e 65 72 3d 3d 74 69 64 20 29   p->owner==tid )
94bf0 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20  ;.  p->nRef--;. 
94c00 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66   assert( p->nRef
94c10 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51  ==0 || p->id==SQ
94c20 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
94c30 53 49 56 45 20 29 3b 0a 20 20 44 6f 73 52 65 6c  SIVE );.  DosRel
94c40 65 61 73 65 4d 75 74 65 78 53 65 6d 28 70 2d 3e  easeMutexSem(p->
94c50 6d 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 64 65  mutex);.}..#ifde
94c60 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f  f SQLITE_DEBUG./
94c70 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
94c80 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e  _mutex_held() an
94c90 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
94ca0 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e  notheld() routin
94cb0 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65  e are.** intende
94cc0 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65  d for use inside
94cd0 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
94ce0 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ents..*/.static 
94cf0 69 6e 74 20 6f 73 32 4d 75 74 65 78 48 65 6c 64  int os2MutexHeld
94d00 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
94d10 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20  p){.  TID tid;. 
94d20 20 50 49 44 20 70 69 64 3b 0a 20 20 55 4c 4f 4e   PID pid;.  ULON
94d30 47 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50 54 49  G ulCount;.  PTI
94d40 42 20 70 74 69 62 3b 0a 20 20 69 66 28 20 70 21  B ptib;.  if( p!
94d50 3d 30 20 29 20 7b 0a 20 20 20 20 44 6f 73 51 75  =0 ) {.    DosQu
94d60 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d  eryMutexSem(p->m
94d70 75 74 65 78 2c 20 26 70 69 64 2c 20 26 74 69 64  utex, &pid, &tid
94d80 2c 20 26 75 6c 43 6f 75 6e 74 29 3b 0a 20 20 7d  , &ulCount);.  }
94d90 20 65 6c 73 65 20 7b 0a 20 20 20 20 44 6f 73 47   else {.    DosG
94da0 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 26 70 74  etInfoBlocks(&pt
94db0 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 74  ib, NULL);.    t
94dc0 69 64 20 3d 20 70 74 69 62 2d 3e 74 69 62 5f 70  id = ptib->tib_p
94dd0 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74 69 64  tib2->tib2_ultid
94de0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
94df0 3d 3d 30 20 7c 7c 20 28 70 2d 3e 6e 52 65 66 21  ==0 || (p->nRef!
94e00 3d 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d  =0 && p->owner==
94e10 74 69 64 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69  tid);.}.static i
94e20 6e 74 20 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65  nt os2MutexNothe
94e30 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
94e40 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b   *p){.  TID tid;
94e50 0a 20 20 50 49 44 20 70 69 64 3b 0a 20 20 55 4c  .  PID pid;.  UL
94e60 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50  ONG ulCount;.  P
94e70 54 49 42 20 70 74 69 62 3b 0a 20 20 69 66 28 20  TIB ptib;.  if( 
94e80 70 21 3d 20 30 20 29 20 7b 0a 20 20 20 20 44 6f  p!= 0 ) {.    Do
94e90 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70  sQueryMutexSem(p
94ea0 2d 3e 6d 75 74 65 78 2c 20 26 70 69 64 2c 20 26  ->mutex, &pid, &
94eb0 74 69 64 2c 20 26 75 6c 43 6f 75 6e 74 29 3b 0a  tid, &ulCount);.
94ec0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 44    } else {.    D
94ed0 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28  osGetInfoBlocks(
94ee0 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20  &ptib, NULL);.  
94ef0 20 20 74 69 64 20 3d 20 70 74 69 62 2d 3e 74 69    tid = ptib->ti
94f00 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c  b_ptib2->tib2_ul
94f10 74 69 64 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  tid;.  }.  retur
94f20 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52 65  n p==0 || p->nRe
94f30 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72  f==0 || p->owner
94f40 21 3d 74 69 64 3b 0a 7d 0a 23 65 6e 64 69 66 0a  !=tid;.}.#endif.
94f50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
94f60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
94f70 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65  thods *sqlite3De
94f80 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29  faultMutex(void)
94f90 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74  {.  static sqlit
94fa0 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
94fb0 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20   sMutex = {.    
94fc0 6f 73 32 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20  os2MutexInit,.  
94fd0 20 20 6f 73 32 4d 75 74 65 78 45 6e 64 2c 0a 20    os2MutexEnd,. 
94fe0 20 20 20 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63     os2MutexAlloc
94ff0 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 46 72  ,.    os2MutexFr
95000 65 65 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78  ee,.    os2Mutex
95010 45 6e 74 65 72 2c 0a 20 20 20 20 6f 73 32 4d 75  Enter,.    os2Mu
95020 74 65 78 54 72 79 2c 0a 20 20 20 20 6f 73 32 4d  texTry,.    os2M
95030 75 74 65 78 4c 65 61 76 65 2c 0a 23 69 66 64 65  utexLeave,.#ifde
95040 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
95050 20 20 20 6f 73 32 4d 75 74 65 78 48 65 6c 64 2c     os2MutexHeld,
95060 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 4e 6f 74  .    os2MutexNot
95070 68 65 6c 64 0a 23 65 6e 64 69 66 0a 20 20 7d 3b  held.#endif.  };
95080 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74  ..  return &sMut
95090 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ex;.}.#endif /* 
950a0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32  SQLITE_MUTEX_OS2
950b0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
950c0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65  **** End of mute
950d0 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  x_os2.c ********
950e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
950f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95100 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
95110 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
95120 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a 2a 2a  mutex_unix.c ***
95130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95150 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20  ***/./*.** 2007 
95160 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20  August 28.**.** 
95170 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
95180 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
95190 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
951a0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
951b0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
951c0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
951d0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
951e0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
951f0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
95200 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
95210 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
95220 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
95230 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
95240 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
95250 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
95260 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
95270 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
95280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
952a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
952b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
952c0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
952d0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74   file contains t
952e0 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74  he C functions t
952f0 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75  hat implement mu
95300 74 65 78 65 73 20 66 6f 72 20 70 74 68 72 65 61  texes for pthrea
95310 64 73 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75  ds.**.** $Id: mu
95320 74 65 78 5f 75 6e 69 78 2e 63 2c 76 20 31 2e 31  tex_unix.c,v 1.1
95330 36 20 32 30 30 38 2f 31 32 2f 30 38 20 31 38 3a  6 2008/12/08 18:
95340 31 39 3a 31 38 20 64 72 68 20 45 78 70 20 24 0a  19:18 drh Exp $.
95350 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f  */../*.** The co
95360 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
95370 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20  is only used if 
95380 77 65 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67  we are compiling
95390 20 74 68 72 65 61 64 73 61 66 65 0a 2a 2a 20 75   threadsafe.** u
953a0 6e 64 65 72 20 75 6e 69 78 20 77 69 74 68 20 70  nder unix with p
953b0 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e  threads..**.** N
953c0 6f 74 65 20 74 68 61 74 20 74 68 69 73 20 69 6d  ote that this im
953d0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 72 65 71  plementation req
953e0 75 69 72 65 73 20 61 20 76 65 72 73 69 6f 6e 20  uires a version 
953f0 6f 66 20 70 74 68 72 65 61 64 73 20 74 68 61 74  of pthreads that
95400 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20 72 65 63  .** supports rec
95410 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 2e 0a  ursive mutexes..
95420 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
95430 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 53 0a  _MUTEX_PTHREADS.
95440 0a 23 69 6e 63 6c 75 64 65 20 3c 70 74 68 72 65  .#include <pthre
95450 61 64 2e 68 3e 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61  ad.h>.../*.** Ea
95460 63 68 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  ch recursive mut
95470 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  ex is an instanc
95480 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
95490 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  ng structure..*/
954a0 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
954b0 6d 75 74 65 78 20 7b 0a 20 20 70 74 68 72 65 61  mutex {.  pthrea
954c0 64 5f 6d 75 74 65 78 5f 74 20 6d 75 74 65 78 3b  d_mutex_t mutex;
954d0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f       /* Mutex co
954e0 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f  ntrolling the lo
954f0 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64 3b 20  ck */.  int id; 
95500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
95510 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65     /* Mutex type
95520 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20   */.  int nRef; 
95530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
95540 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
95550 74 72 61 6e 63 65 73 20 2a 2f 0a 20 20 70 74 68  trances */.  pth
95560 72 65 61 64 5f 74 20 6f 77 6e 65 72 3b 20 20 20  read_t owner;   
95570 20 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61          /* Threa
95580 64 20 74 68 61 74 20 69 73 20 77 69 74 68 69 6e  d that is within
95590 20 74 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 23   this mutex */.#
955a0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
955b0 55 47 0a 20 20 69 6e 74 20 74 72 61 63 65 3b 20  UG.  int trace; 
955c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
955d0 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61 63 65  /* True to trace
955e0 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 65 6e 64   changes */.#end
955f0 69 66 0a 7d 3b 0a 23 69 66 64 65 66 20 53 51 4c  if.};.#ifdef SQL
95600 49 54 45 5f 44 45 42 55 47 0a 23 64 65 66 69 6e  ITE_DEBUG.#defin
95610 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f  e SQLITE3_MUTEX_
95620 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 50 54  INITIALIZER { PT
95630 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54  HREAD_MUTEX_INIT
95640 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28  IALIZER, 0, 0, (
95650 70 74 68 72 65 61 64 5f 74 29 30 2c 20 30 20 7d  pthread_t)0, 0 }
95660 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53  .#else.#define S
95670 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49  QLITE3_MUTEX_INI
95680 54 49 41 4c 49 5a 45 52 20 7b 20 50 54 48 52 45  TIALIZER { PTHRE
95690 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c  AD_MUTEX_INITIAL
956a0 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28 70 74 68  IZER, 0, 0, (pth
956b0 72 65 61 64 5f 74 29 30 20 7d 0a 23 65 6e 64 69  read_t)0 }.#endi
956c0 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  f../*.** The sql
956d0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
956e0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75  ) and sqlite3_mu
956f0 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f  tex_notheld() ro
95700 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74  utine are.** int
95710 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 6f 6e  ended for use on
95720 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  ly inside assert
95730 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20  () statements.  
95740 4f 6e 20 73 6f 6d 65 20 70 6c 61 74 66 6f 72 6d  On some platform
95750 73 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 69 67 68  s,.** there migh
95760 74 20 62 65 20 72 61 63 65 20 63 6f 6e 64 69 74  t be race condit
95770 69 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 63 61  ions that can ca
95780 75 73 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e  use these routin
95790 65 73 20 74 6f 0a 2a 2a 20 64 65 6c 69 76 65 72  es to.** deliver
957a0 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75 6c   incorrect resul
957b0 74 73 2e 20 20 49 6e 20 70 61 72 74 69 63 75 6c  ts.  In particul
957c0 61 72 2c 20 69 66 20 70 74 68 72 65 61 64 5f 65  ar, if pthread_e
957d0 71 75 61 6c 28 29 20 69 73 0a 2a 2a 20 6e 6f 74  qual() is.** not
957e0 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 61   an atomic opera
957f0 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 73 65  tion, then these
95800 20 72 6f 75 74 69 6e 65 73 20 6d 69 67 68 74 20   routines might 
95810 64 65 6c 69 76 65 72 79 0a 2a 2a 20 69 6e 63 6f  delivery.** inco
95820 72 72 65 63 74 20 72 65 73 75 6c 74 73 2e 20 20  rrect results.  
95830 4f 6e 20 6d 6f 73 74 20 70 6c 61 74 66 6f 72 6d  On most platform
95840 73 2c 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c  s, pthread_equal
95850 28 29 20 69 73 20 61 20 0a 2a 2a 20 63 6f 6d 70  () is a .** comp
95860 61 72 69 73 6f 6e 20 6f 66 20 74 77 6f 20 69 6e  arison of two in
95870 74 65 67 65 72 73 20 61 6e 64 20 69 73 20 74 68  tegers and is th
95880 65 72 65 66 6f 72 65 20 61 74 6f 6d 69 63 2e 20  erefore atomic. 
95890 20 42 75 74 20 77 65 20 61 72 65 0a 2a 2a 20 74   But we are.** t
958a0 6f 6c 64 20 74 68 61 74 20 48 50 55 58 20 69 73  old that HPUX is
958b0 20 6e 6f 74 20 73 75 63 68 20 61 20 70 6c 61 74   not such a plat
958c0 66 6f 72 6d 2e 20 20 49 66 20 73 6f 2c 20 74 68  form.  If so, th
958d0 65 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  en these routine
958e0 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 61 6c  s.** will not al
958f0 77 61 79 73 20 77 6f 72 6b 20 63 6f 72 72 65 63  ways work correc
95900 74 6c 79 20 6f 6e 20 48 50 55 58 2e 0a 2a 2a 0a  tly on HPUX..**.
95910 2a 2a 20 4f 6e 20 74 68 6f 73 65 20 70 6c 61 74  ** On those plat
95920 66 6f 72 6d 73 20 77 68 65 72 65 20 70 74 68 72  forms where pthr
95930 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 20 6e  ead_equal() is n
95940 6f 74 20 61 74 6f 6d 69 63 2c 20 53 51 4c 69 74  ot atomic, SQLit
95950 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63  e.** should be c
95960 6f 6d 70 69 6c 65 64 20 77 69 74 68 6f 75 74 20  ompiled without 
95970 2d 44 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61  -DSQLITE_DEBUG a
95980 6e 64 20 77 69 74 68 20 2d 44 4e 44 45 42 55 47  nd with -DNDEBUG
95990 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 73 75 72 65   to.** make sure
959a0 20 6e 6f 20 61 73 73 65 72 74 28 29 20 73 74 61   no assert() sta
959b0 74 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c  tements are eval
959c0 75 61 74 65 64 20 61 6e 64 20 68 65 6e 63 65 20  uated and hence 
959d0 74 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65  these.** routine
959e0 73 20 61 72 65 20 6e 65 76 65 72 20 63 61 6c 6c  s are never call
959f0 65 64 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69  ed..*/.#if !defi
95a00 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64  ned(NDEBUG) || d
95a10 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
95a20 42 55 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20  BUG).static int 
95a30 70 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64  pthreadMutexHeld
95a40 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
95a50 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 2d  p){.  return (p-
95a60 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 74 68 72  >nRef!=0 && pthr
95a70 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e  ead_equal(p->own
95a80 65 72 2c 20 70 74 68 72 65 61 64 5f 73 65 6c 66  er, pthread_self
95a90 28 29 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69  ()));.}.static i
95aa0 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e  nt pthreadMutexN
95ab0 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  otheld(sqlite3_m
95ac0 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75  utex *p){.  retu
95ad0 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c  rn p->nRef==0 ||
95ae0 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70   pthread_equal(p
95af0 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 61 64  ->owner, pthread
95b00 5f 73 65 6c 66 28 29 29 3d 3d 30 3b 0a 7d 0a 23  _self())==0;.}.#
95b10 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69  endif../*.** Ini
95b20 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e  tialize and dein
95b30 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74  itialize the mut
95b40 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f  ex subsystem..*/
95b50 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72  .static int pthr
95b60 65 61 64 4d 75 74 65 78 49 6e 69 74 28 76 6f 69  eadMutexInit(voi
95b70 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54  d){ return SQLIT
95b80 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69  E_OK; }.static i
95b90 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 45  nt pthreadMutexE
95ba0 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  nd(void){ return
95bb0 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f   SQLITE_OK; }../
95bc0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
95bd0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72  _mutex_alloc() r
95be0 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73  outine allocates
95bf0 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20   a new.** mutex 
95c00 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f  and returns a po
95c10 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66  inter to it.  If
95c20 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c   it returns NULL
95c30 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74  .** that means t
95c40 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c  hat a mutex coul
95c50 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74  d not be allocat
95c60 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77  ed.  SQLite.** w
95c70 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73  ill unwind its s
95c80 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20  tack and return 
95c90 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 61  an error.  The a
95ca0 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71  rgument.** to sq
95cb0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
95cc0 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68  c() is one of th
95cd0 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73  ese integer cons
95ce0 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c  tants:.**.** <ul
95cf0 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  >.** <li>  SQLIT
95d00 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20  E_MUTEX_FAST.** 
95d10 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
95d20 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20  EX_RECURSIVE.** 
95d30 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
95d40 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
95d50 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
95d60 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45  _MUTEX_STATIC_ME
95d70 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  M.** <li>  SQLIT
95d80 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
95d90 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  EM2.** <li>  SQL
95da0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
95db0 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53  _PRNG.** <li>  S
95dc0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
95dd0 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e 20 20  IC_LRU.** <li>  
95de0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
95df0 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f 75 6c  TIC_LRU2.** </ul
95e00 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  >.**.** The firs
95e10 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20  t two constants 
95e20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75  cause sqlite3_mu
95e30 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63  tex_alloc() to c
95e40 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d  reate.** a new m
95e50 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d  utex.  The new m
95e60 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76  utex is recursiv
95e70 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55  e when SQLITE_MU
95e80 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a  TEX_RECURSIVE.**
95e90 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74   is used but not
95ea0 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20   necessarily so 
95eb0 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45  when SQLITE_MUTE
95ec0 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a  X_FAST is used..
95ed0 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70  ** The mutex imp
95ee0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73  lementation does
95ef0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b   not need to mak
95f00 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a  e a distinction.
95f10 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54  ** between SQLIT
95f20 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
95f30 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54  E and SQLITE_MUT
95f40 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f  EX_FAST if it do
95f50 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74  es.** not want t
95f60 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 77  o.  But SQLite w
95f70 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74  ill only request
95f80 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74   a recursive mut
95f90 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77  ex in.** cases w
95fa0 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e  here it really n
95fb0 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20  eeds one.  If a 
95fc0 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72  faster non-recur
95fd0 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d  sive mutex.** im
95fe0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20  plementation is 
95ff0 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65  available on the
96000 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20   host platform, 
96010 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73  the mutex subsys
96020 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74  tem.** might ret
96030 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78  urn such a mutex
96040 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20   in response to 
96050 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
96060 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68  T..**.** The oth
96070 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d  er allowed param
96080 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33  eters to sqlite3
96090 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65  _mutex_alloc() e
960a0 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20  ach return.** a 
960b0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61  pointer to a sta
960c0 74 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20  tic preexisting 
960d0 6d 75 74 65 78 2e 20 20 53 69 78 20 73 74 61 74  mutex.  Six stat
960e0 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a  ic mutexes are.*
960f0 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75  * used by the cu
96100 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66  rrent version of
96110 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65   SQLite.  Future
96120 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
96130 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61  ite.** may add a
96140 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63  dditional static
96150 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69   mutexes.  Stati
96160 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f  c mutexes are fo
96170 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73  r internal.** us
96180 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79  e by SQLite only
96190 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20  .  Applications 
961a0 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20  that use SQLite 
961b0 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a  mutexes should.*
961c0 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64  * use only the d
961d0 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72  ynamic mutexes r
961e0 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54  eturned by SQLIT
961f0 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a  E_MUTEX_FAST or.
96200 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ** SQLITE_MUTEX_
96210 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a  RECURSIVE..**.**
96220 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e   Note that if on
96230 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63  e of the dynamic
96240 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72   mutex parameter
96250 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  s (SQLITE_MUTEX_
96260 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54  FAST.** or SQLIT
96270 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
96280 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20  E) is used then 
96290 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
962a0 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73  loc().** returns
962b0 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74   a different mut
962c0 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c  ex on every call
962d0 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 73  .  But for the s
962e0 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20  tatic .** mutex 
962f0 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20  types, the same 
96300 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e 65  mutex is returne
96310 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20  d on every call 
96320 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20  that has.** the 
96330 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72  same type number
96340 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69  ..*/.static sqli
96350 74 65 33 5f 6d 75 74 65 78 20 2a 70 74 68 72 65  te3_mutex *pthre
96360 61 64 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74  adMutexAlloc(int
96370 20 69 54 79 70 65 29 7b 0a 20 20 73 74 61 74 69   iType){.  stati
96380 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  c sqlite3_mutex 
96390 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 5d 20  staticMutexes[] 
963a0 3d 20 7b 0a 20 20 20 20 53 51 4c 49 54 45 33 5f  = {.    SQLITE3_
963b0 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
963c0 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d  R,.    SQLITE3_M
963d0 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52  UTEX_INITIALIZER
963e0 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55  ,.    SQLITE3_MU
963f0 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
96400 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54  .    SQLITE3_MUT
96410 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a  EX_INITIALIZER,.
96420 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45      SQLITE3_MUTE
96430 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20  X_INITIALIZER,. 
96440 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58     SQLITE3_MUTEX
96450 5f 49 4e 49 54 49 41 4c 49 5a 45 52 0a 20 20 7d  _INITIALIZER.  }
96460 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
96470 78 20 2a 70 3b 0a 20 20 73 77 69 74 63 68 28 20  x *p;.  switch( 
96480 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73  iType ){.    cas
96490 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  e SQLITE_MUTEX_R
964a0 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20  ECURSIVE: {.    
964b0 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c    p = sqlite3Mal
964c0 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28  locZero( sizeof(
964d0 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28  *p) );.      if(
964e0 20 70 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c   p ){.#ifdef SQL
964f0 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45  ITE_HOMEGROWN_RE
96500 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 20  CURSIVE_MUTEX.  
96510 20 20 20 20 20 20 2f 2a 20 49 66 20 72 65 63 75        /* If recu
96520 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 61 72  rsive mutexes ar
96530 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c  e not available,
96540 20 77 65 20 77 69 6c 6c 20 68 61 76 65 20 74 6f   we will have to
96550 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 75 69 6c  .        ** buil
96560 64 20 6f 75 72 20 6f 77 6e 2e 20 20 53 65 65 20  d our own.  See 
96570 62 65 6c 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20  below. */.      
96580 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f    pthread_mutex_
96590 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20  init(&p->mutex, 
965a0 30 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20  0);.#else.      
965b0 20 20 2f 2a 20 55 73 65 20 61 20 72 65 63 75 72    /* Use a recur
965c0 73 69 76 65 20 6d 75 74 65 78 20 69 66 20 69 74  sive mutex if it
965d0 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f   is available */
965e0 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64  .        pthread
965f0 5f 6d 75 74 65 78 61 74 74 72 5f 74 20 72 65 63  _mutexattr_t rec
96600 75 72 73 69 76 65 41 74 74 72 3b 0a 20 20 20 20  ursiveAttr;.    
96610 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65      pthread_mute
96620 78 61 74 74 72 5f 69 6e 69 74 28 26 72 65 63 75  xattr_init(&recu
96630 72 73 69 76 65 41 74 74 72 29 3b 0a 20 20 20 20  rsiveAttr);.    
96640 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65      pthread_mute
96650 78 61 74 74 72 5f 73 65 74 74 79 70 65 28 26 72  xattr_settype(&r
96660 65 63 75 72 73 69 76 65 41 74 74 72 2c 20 50 54  ecursiveAttr, PT
96670 48 52 45 41 44 5f 4d 55 54 45 58 5f 52 45 43 55  HREAD_MUTEX_RECU
96680 52 53 49 56 45 29 3b 0a 20 20 20 20 20 20 20 20  RSIVE);.        
96690 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e  pthread_mutex_in
966a0 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 26 72  it(&p->mutex, &r
966b0 65 63 75 72 73 69 76 65 41 74 74 72 29 3b 0a 20  ecursiveAttr);. 
966c0 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d         pthread_m
966d0 75 74 65 78 61 74 74 72 5f 64 65 73 74 72 6f 79  utexattr_destroy
966e0 28 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29  (&recursiveAttr)
966f0 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  ;.#endif.       
96700 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a   p->id = iType;.
96710 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
96720 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
96730 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ase SQLITE_MUTEX
96740 5f 46 41 53 54 3a 20 7b 0a 20 20 20 20 20 20 70  _FAST: {.      p
96750 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
96760 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29  Zero( sizeof(*p)
96770 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20   );.      if( p 
96780 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64  ){.        p->id
96790 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20   = iType;.      
967a0 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f    pthread_mutex_
967b0 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20  init(&p->mutex, 
967c0 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
967d0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
967e0 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
967f0 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70      assert( iTyp
96800 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20  e-2 >= 0 );.    
96810 20 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d    assert( iType-
96820 32 20 3c 20 41 72 72 61 79 53 69 7a 65 28 73 74  2 < ArraySize(st
96830 61 74 69 63 4d 75 74 65 78 65 73 29 20 29 3b 0a  aticMutexes) );.
96840 20 20 20 20 20 20 70 20 3d 20 26 73 74 61 74 69        p = &stati
96850 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d 32  cMutexes[iType-2
96860 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d  ];.      p->id =
96870 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 72   iType;.      br
96880 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
96890 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f   return p;.}.../
968a0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
968b0 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20  e deallocates a 
968c0 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c  previously.** al
968d0 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20 20  located mutex.  
968e0 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75  SQLite is carefu
968f0 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20  l to deallocate 
96900 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74  every.** mutex t
96910 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73  hat it allocates
96920 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
96930 20 70 74 68 72 65 61 64 4d 75 74 65 78 46 72 65   pthreadMutexFre
96940 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
96950 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
96960 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61  ->nRef==0 );.  a
96970 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
96980 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20  LITE_MUTEX_FAST 
96990 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  || p->id==SQLITE
969a0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
969b0 20 29 3b 0a 20 20 70 74 68 72 65 61 64 5f 6d 75   );.  pthread_mu
969c0 74 65 78 5f 64 65 73 74 72 6f 79 28 26 70 2d 3e  tex_destroy(&p->
969d0 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65  mutex);.  sqlite
969e0 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a  3_free(p);.}../*
969f0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
96a00 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e  mutex_enter() an
96a10 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
96a20 74 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61  try() routines a
96a30 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74  ttempt.** to ent
96a40 65 72 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20  er a mutex.  If 
96a50 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69  another thread i
96a60 73 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e  s already within
96a70 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73   the mutex,.** s
96a80 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
96a90 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20  er() will block 
96aa0 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  and sqlite3_mute
96ab0 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74  x_try() will ret
96ac0 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55  urn.** SQLITE_BU
96ad0 53 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  SY.  The sqlite3
96ae0 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74  _mutex_try() int
96af0 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 53  erface returns S
96b00 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e  QLITE_OK.** upon
96b10 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72   successful entr
96b20 79 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65 61  y.  Mutexes crea
96b30 74 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45  ted using SQLITE
96b40 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
96b50 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72   can.** be enter
96b60 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  ed multiple time
96b70 73 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68  s by the same th
96b80 72 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63  read.  In such c
96b90 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74  ases the,.** mut
96ba0 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65  ex must be exite
96bb0 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65  d an equal numbe
96bc0 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72  r of times befor
96bd0 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64  e another thread
96be0 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20  .** can enter.  
96bf0 49 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  If the same thre
96c00 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65  ad tries to ente
96c10 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64  r any other kind
96c20 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72   of mutex.** mor
96c30 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65  e than once, the
96c40 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64   behavior is und
96c50 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  efined..*/.stati
96c60 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75  c void pthreadMu
96c70 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33  texEnter(sqlite3
96c80 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73  _mutex *p){.  as
96c90 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c  sert( p->id==SQL
96ca0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
96cb0 49 56 45 20 7c 7c 20 70 74 68 72 65 61 64 4d 75  IVE || pthreadMu
96cc0 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b  texNotheld(p) );
96cd0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
96ce0 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53  HOMEGROWN_RECURS
96cf0 49 56 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49  IVE_MUTEX.  /* I
96d00 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  f recursive mute
96d10 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69  xes are not avai
96d20 6c 61 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 68  lable, then we h
96d30 61 76 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a  ave to grow.  **
96d40 20 6f 75 72 20 6f 77 6e 2e 20 20 54 68 69 73 20   our own.  This 
96d50 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61  implementation a
96d60 73 73 75 6d 65 73 20 74 68 61 74 20 70 74 68 72  ssumes that pthr
96d70 65 61 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a  ead_equal().  **
96d80 20 69 73 20 61 74 6f 6d 69 63 20 2d 20 74 68 61   is atomic - tha
96d90 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 64  t it cannot be d
96da0 65 63 65 69 76 65 64 20 69 6e 74 6f 20 74 68 69  eceived into thi
96db0 6e 6b 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20  nking self.  ** 
96dc0 61 6e 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65  and p->owner are
96dd0 20 65 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e   equal if p->own
96de0 65 72 20 63 68 61 6e 67 65 73 20 62 65 74 77 65  er changes betwe
96df0 65 6e 20 74 77 6f 20 76 61 6c 75 65 73 0a 20 20  en two values.  
96e00 2a 2a 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20  ** that are not 
96e10 65 71 75 61 6c 20 74 6f 20 73 65 6c 66 20 77 68  equal to self wh
96e20 69 6c 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73  ile the comparis
96e30 6f 6e 20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61  on is taking pla
96e40 63 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d  ce..  ** This im
96e50 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73  plementation als
96e60 6f 20 61 73 73 75 6d 65 73 20 61 20 63 6f 68 65  o assumes a cohe
96e70 72 65 6e 74 20 63 61 63 68 65 20 2d 20 74 68 61  rent cache - tha
96e80 74 20 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 65  t .  ** separate
96e90 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f   processes canno
96ea0 74 20 72 65 61 64 20 64 69 66 66 65 72 65 6e 74  t read different
96eb0 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65   values from the
96ec0 20 73 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65   same.  ** addre
96ed0 73 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  ss at the same t
96ee0 69 6d 65 2e 20 20 49 66 20 65 69 74 68 65 72 20  ime.  If either 
96ef0 6f 66 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e  of these two con
96f00 64 69 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65  ditions.  ** are
96f10 20 6e 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20 74   not met, then t
96f20 68 65 20 6d 75 74 65 78 65 73 20 77 69 6c 6c 20  he mutexes will 
96f30 66 61 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d  fail and problem
96f40 73 20 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20  s will result.. 
96f50 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68 72   */.  {.    pthr
96f60 65 61 64 5f 74 20 73 65 6c 66 20 3d 20 70 74 68  ead_t self = pth
96f70 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20  read_self();.   
96f80 20 69 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26   if( p->nRef>0 &
96f90 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28  & pthread_equal(
96fa0 70 2d 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20  p->owner, self) 
96fb0 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66  ){.      p->nRef
96fc0 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ++;.    }else{. 
96fd0 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74       pthread_mut
96fe0 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65  ex_lock(&p->mute
96ff0 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  x);.      assert
97000 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a  ( p->nRef==0 );.
97010 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d        p->owner =
97020 20 73 65 6c 66 3b 0a 20 20 20 20 20 20 70 2d 3e   self;.      p->
97030 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  nRef = 1;.    }.
97040 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 55    }.#else.  /* U
97050 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  se the built-in 
97060 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65  recursive mutexe
97070 73 20 69 66 20 74 68 65 79 20 61 72 65 20 61 76  s if they are av
97080 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  ailable..  */.  
97090 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f  pthread_mutex_lo
970a0 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20  ck(&p->mutex);. 
970b0 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72   p->owner = pthr
970c0 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 70 2d  ead_self();.  p-
970d0 3e 6e 52 65 66 2b 2b 3b 0a 23 65 6e 64 69 66 0a  >nRef++;.#endif.
970e0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
970f0 45 42 55 47 0a 20 20 69 66 28 20 70 2d 3e 74 72  EBUG.  if( p->tr
97100 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e 74  ace ){.    print
97110 66 28 22 65 6e 74 65 72 20 6d 75 74 65 78 20 25  f("enter mutex %
97120 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 65 66  p (%d) with nRef
97130 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72  =%d\n", p, p->tr
97140 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20  ace, p->nRef);. 
97150 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74   }.#endif.}.stat
97160 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75  ic int pthreadMu
97170 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d  texTry(sqlite3_m
97180 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20  utex *p){.  int 
97190 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  rc;.  assert( p-
971a0 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
971b0 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 70  X_RECURSIVE || p
971c0 74 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65  threadMutexNothe
971d0 6c 64 28 70 29 20 29 3b 0a 0a 23 69 66 64 65 66  ld(p) );..#ifdef
971e0 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57   SQLITE_HOMEGROW
971f0 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45  N_RECURSIVE_MUTE
97200 58 0a 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73  X.  /* If recurs
97210 69 76 65 20 6d 75 74 65 78 65 73 20 61 72 65 20  ive mutexes are 
97220 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 74  not available, t
97230 68 65 6e 20 77 65 20 68 61 76 65 20 74 6f 20 67  hen we have to g
97240 72 6f 77 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e  row.  ** our own
97250 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e  .  This implemen
97260 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74  tation assumes t
97270 68 61 74 20 70 74 68 72 65 61 64 5f 65 71 75 61  hat pthread_equa
97280 6c 28 29 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d  l().  ** is atom
97290 69 63 20 2d 20 74 68 61 74 20 69 74 20 63 61 6e  ic - that it can
972a0 6e 6f 74 20 62 65 20 64 65 63 65 69 76 65 64 20  not be deceived 
972b0 69 6e 74 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65  into thinking se
972c0 6c 66 0a 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f  lf.  ** and p->o
972d0 77 6e 65 72 20 61 72 65 20 65 71 75 61 6c 20 69  wner are equal i
972e0 66 20 70 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67  f p->owner chang
972f0 65 73 20 62 65 74 77 65 65 6e 20 74 77 6f 20 76  es between two v
97300 61 6c 75 65 73 0a 20 20 2a 2a 20 74 68 61 74 20  alues.  ** that 
97310 61 72 65 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f  are not equal to
97320 20 73 65 6c 66 20 77 68 69 6c 65 20 74 68 65 20   self while the 
97330 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 61  comparison is ta
97340 6b 69 6e 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a  king place..  **
97350 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61   This implementa
97360 74 69 6f 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65  tion also assume
97370 73 20 61 20 63 6f 68 65 72 65 6e 74 20 63 61 63  s a coherent cac
97380 68 65 20 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20  he - that .  ** 
97390 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 73 73  separate process
973a0 65 73 20 63 61 6e 6e 6f 74 20 72 65 61 64 20 64  es cannot read d
973b0 69 66 66 65 72 65 6e 74 20 76 61 6c 75 65 73 20  ifferent values 
973c0 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 0a 20 20  from the same.  
973d0 2a 2a 20 61 64 64 72 65 73 73 20 61 74 20 74 68  ** address at th
973e0 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 49 66  e same time.  If
973f0 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65   either of these
97400 20 74 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a   two conditions.
97410 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74    ** are not met
97420 2c 20 74 68 65 6e 20 74 68 65 20 6d 75 74 65 78  , then the mutex
97430 65 73 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64  es will fail and
97440 20 70 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72   problems will r
97450 65 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a  esult..  */.  {.
97460 20 20 20 20 70 74 68 72 65 61 64 5f 74 20 73 65      pthread_t se
97470 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c  lf = pthread_sel
97480 66 28 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  f();.    if( p->
97490 6e 52 65 66 3e 30 20 26 26 20 70 74 68 72 65 61  nRef>0 && pthrea
974a0 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72  d_equal(p->owner
974b0 2c 20 73 65 6c 66 29 20 29 7b 0a 20 20 20 20 20  , self) ){.     
974c0 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20   p->nRef++;.    
974d0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
974e0 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
974f0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 72  pthread_mutex_tr
97500 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29  ylock(&p->mutex)
97510 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ==0 ){.      ass
97520 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20  ert( p->nRef==0 
97530 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65  );.      p->owne
97540 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 20 20 20  r = self;.      
97550 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20  p->nRef = 1;.   
97560 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
97570 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  K;.    }else{.  
97580 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
97590 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  BUSY;.    }.  }.
975a0 23 65 6c 73 65 0a 20 20 2f 2a 20 55 73 65 20 74  #else.  /* Use t
975b0 68 65 20 62 75 69 6c 74 2d 69 6e 20 72 65 63 75  he built-in recu
975c0 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 69 66  rsive mutexes if
975d0 20 74 68 65 79 20 61 72 65 20 61 76 61 69 6c 61   they are availa
975e0 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ble..  */.  if( 
975f0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 72  pthread_mutex_tr
97600 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29  ylock(&p->mutex)
97610 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 6f 77  ==0 ){.    p->ow
97620 6e 65 72 20 3d 20 70 74 68 72 65 61 64 5f 73 65  ner = pthread_se
97630 6c 66 28 29 3b 0a 20 20 20 20 70 2d 3e 6e 52 65  lf();.    p->nRe
97640 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  f++;.    rc = SQ
97650 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65  LITE_OK;.  }else
97660 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
97670 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64  E_BUSY;.  }.#end
97680 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
97690 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 72 63  E_DEBUG.  if( rc
976a0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
976b0 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70  ->trace ){.    p
976c0 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d 75 74  rintf("enter mut
976d0 65 78 20 25 70 20 28 25 64 29 20 77 69 74 68 20  ex %p (%d) with 
976e0 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70  nRef=%d\n", p, p
976f0 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66  ->trace, p->nRef
97700 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
97710 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
97720 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
97730 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f  mutex_leave() ro
97740 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75  utine exits a mu
97750 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20  tex that was.** 
97760 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72  previously enter
97770 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74  ed by the same t
97780 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61  hread.  The beha
97790 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66  vior.** is undef
977a0 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65  ined if the mute
977b0 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74  x is not current
977c0 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a  ly entered or.**
977d0 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
977e0 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51  y allocated.  SQ
977f0 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20  Lite will never 
97800 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74  do either..*/.st
97810 61 74 69 63 20 76 6f 69 64 20 70 74 68 72 65 61  atic void pthrea
97820 64 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69  dMutexLeave(sqli
97830 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
97840 20 61 73 73 65 72 74 28 20 70 74 68 72 65 61 64   assert( pthread
97850 4d 75 74 65 78 48 65 6c 64 28 70 29 20 29 3b 0a  MutexHeld(p) );.
97860 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61    p->nRef--;.  a
97870 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d  ssert( p->nRef==
97880 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  0 || p->id==SQLI
97890 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
978a0 56 45 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51  VE );..#ifdef SQ
978b0 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52  LITE_HOMEGROWN_R
978c0 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20  ECURSIVE_MUTEX. 
978d0 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20   if( p->nRef==0 
978e0 29 7b 0a 20 20 20 20 70 74 68 72 65 61 64 5f 6d  ){.    pthread_m
978f0 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e  utex_unlock(&p->
97900 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 23 65 6c 73  mutex);.  }.#els
97910 65 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65  e.  pthread_mute
97920 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74  x_unlock(&p->mut
97930 65 78 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  ex);.#endif..#if
97940 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
97950 0a 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20  .  if( p->trace 
97960 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 6c  ){.    printf("l
97970 65 61 76 65 20 6d 75 74 65 78 20 25 70 20 28 25  eave mutex %p (%
97980 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64 5c  d) with nRef=%d\
97990 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c  n", p, p->trace,
979a0 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23   p->nRef);.  }.#
979b0 65 6e 64 69 66 0a 7d 0a 0a 53 51 4c 49 54 45 5f  endif.}..SQLITE_
979c0 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f  PRIVATE sqlite3_
979d0 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73  mutex_methods *s
979e0 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74  qlite3DefaultMut
979f0 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  ex(void){.  stat
97a00 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
97a10 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20  _methods sMutex 
97a20 3d 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64 4d  = {.    pthreadM
97a30 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 70 74  utexInit,.    pt
97a40 68 72 65 61 64 4d 75 74 65 78 45 6e 64 2c 0a 20  hreadMutexEnd,. 
97a50 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 41     pthreadMutexA
97a60 6c 6c 6f 63 2c 0a 20 20 20 20 70 74 68 72 65 61  lloc,.    pthrea
97a70 64 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20  dMutexFree,.    
97a80 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e 74 65  pthreadMutexEnte
97a90 72 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75  r,.    pthreadMu
97aa0 74 65 78 54 72 79 2c 0a 20 20 20 20 70 74 68 72  texTry,.    pthr
97ab0 65 61 64 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23  eadMutexLeave,.#
97ac0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
97ad0 55 47 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75  UG.    pthreadMu
97ae0 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 70 74 68  texHeld,.    pth
97af0 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64  readMutexNotheld
97b00 0a 23 65 6c 73 65 0a 20 20 20 20 30 2c 0a 20 20  .#else.    0,.  
97b10 20 20 30 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a    0.#endif.  };.
97b20 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74 65  .  return &sMute
97b30 78 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  x;.}..#endif /* 
97b40 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48  SQLITE_MUTEX_PTH
97b50 52 45 41 44 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  READ */../******
97b60 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
97b70 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a 2a 2a  mutex_unix.c ***
97b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97ba0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
97bb0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
97bc0 69 6c 65 20 6d 75 74 65 78 5f 77 33 32 2e 63 20  ile mutex_w32.c 
97bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97bf0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
97c00 30 30 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2a  007 August 14.**
97c10 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
97c20 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
97c30 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
97c40 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
97c50 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
97c60 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
97c70 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
97c80 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
97c90 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
97ca0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
97cb0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
97cc0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
97cd0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
97ce0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
97cf0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
97d00 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
97d10 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
97d20 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
97d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
97d70 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
97d80 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f  ns the C functio
97d90 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  ns that implemen
97da0 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20 77 69  t mutexes for wi
97db0 6e 33 32 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d  n32.**.** $Id: m
97dc0 75 74 65 78 5f 77 33 32 2e 63 2c 76 20 31 2e 31  utex_w32.c,v 1.1
97dd0 38 20 32 30 30 39 2f 30 38 2f 31 30 20 30 33 3a  8 2009/08/10 03:
97de0 32 33 3a 32 31 20 73 68 61 6e 65 20 45 78 70 20  23:21 shane Exp 
97df0 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  $.*/../*.** The 
97e00 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
97e10 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69  e is only used i
97e20 66 20 77 65 20 61 72 65 20 63 6f 6d 70 69 6c 69  f we are compili
97e30 6e 67 20 6d 75 6c 74 69 74 68 72 65 61 64 65 64  ng multithreaded
97e40 0a 2a 2a 20 6f 6e 20 61 20 77 69 6e 33 32 20 73  .** on a win32 s
97e50 79 73 74 65 6d 2e 0a 2a 2f 0a 23 69 66 64 65 66  ystem..*/.#ifdef
97e60 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33   SQLITE_MUTEX_W3
97e70 32 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 72 65  2../*.** Each re
97e80 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 73  cursive mutex is
97e90 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
97ea0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
97eb0 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75  ructure..*/.stru
97ec0 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ct sqlite3_mutex
97ed0 20 7b 0a 20 20 43 52 49 54 49 43 41 4c 5f 53 45   {.  CRITICAL_SE
97ee0 43 54 49 4f 4e 20 6d 75 74 65 78 3b 20 20 20 20  CTION mutex;    
97ef0 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c  /* Mutex control
97f00 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f  ling the lock */
97f10 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20 20  .  int id;      
97f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
97f30 20 4d 75 74 65 78 20 74 79 70 65 20 2a 2f 0a 20   Mutex type */. 
97f40 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20   int nRef;      
97f50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
97f60 75 6d 62 65 72 20 6f 66 20 65 6e 74 65 72 61 6e  umber of enteran
97f70 63 65 73 20 2a 2f 0a 20 20 44 57 4f 52 44 20 6f  ces */.  DWORD o
97f80 77 6e 65 72 3b 20 20 20 20 20 20 20 20 20 20 20  wner;           
97f90 20 20 20 20 2f 2a 20 54 68 72 65 61 64 20 68 6f      /* Thread ho
97fa0 6c 64 69 6e 67 20 74 68 69 73 20 6d 75 74 65 78  lding this mutex
97fb0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65   */.};../*.** Re
97fc0 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e 2d 7a  turn true (non-z
97fd0 65 72 6f 29 20 69 66 20 77 65 20 61 72 65 20 72  ero) if we are r
97fe0 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57 69 6e  unning under Win
97ff0 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e 58 50  NT, Win2K, WinXP
98000 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e 20 20  ,.** or WinCE.  
98010 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28 7a 65  Return false (ze
98020 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c 20 57  ro) for Win95, W
98030 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45 2e 0a  in98, or WinME..
98040 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20 61 6e  **.** Here is an
98050 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f 62 73   interesting obs
98060 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e 39 35  ervation:  Win95
98070 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57 69 6e  , Win98, and Win
98080 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65 20 4c  ME lack.** the L
98090 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e  ockFileEx() API.
980a0 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 74 69    But we can sti
980b0 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69  ll statically li
980c0 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61 74 0a  nk against that.
980d0 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67 20 61  ** API as long a
980e0 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c 6c 20  s we don't call 
980f0 69 74 20 77 69 6e 20 72 75 6e 6e 69 6e 67 20 57  it win running W
98100 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 41 20 63  in95/98/ME.  A c
98110 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69 73 20 72  all to.** this r
98120 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74  outine is used t
98130 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74  o determine if t
98140 68 65 20 68 6f 73 74 20 69 73 20 57 69 6e 39 35  he host is Win95
98150 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20 57 69 6e  /98/ME or.** Win
98160 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 68 61 74  NT/2K/XP so that
98170 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68   we will know wh
98180 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 77 65 20  ether or not we 
98190 63 61 6e 20 73 61 66 65 6c 79 20 63 61 6c 6c 0a  can safely call.
981a0 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45  ** the LockFileE
981b0 78 28 29 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 6d  x() API..**.** m
981c0 75 74 65 78 49 73 4e 54 28 29 20 69 73 20 6f 6e  utexIsNT() is on
981d0 6c 79 20 75 73 65 64 20 66 6f 72 20 74 68 65 20  ly used for the 
981e0 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 61 6c  TryEnterCritical
981f0 53 65 63 74 69 6f 6e 28 29 20 41 50 49 20 63 61  Section() API ca
98200 6c 6c 2c 0a 2a 2a 20 77 68 69 63 68 20 69 73 20  ll,.** which is 
98210 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69  only available i
98220 66 20 79 6f 75 72 20 61 70 70 6c 69 63 61 74 69  f your applicati
98230 6f 6e 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20  on was compiled 
98240 77 69 74 68 20 0a 2a 2a 20 5f 57 49 4e 33 32 5f  with .** _WIN32_
98250 57 49 4e 4e 54 20 64 65 66 69 6e 65 64 20 74 6f  WINNT defined to
98260 20 61 20 76 61 6c 75 65 20 3e 3d 20 30 78 30 34   a value >= 0x04
98270 30 30 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c 20  00.  Currently, 
98280 74 68 65 20 6f 6e 6c 79 0a 2a 2a 20 63 61 6c 6c  the only.** call
98290 20 74 6f 20 54 72 79 45 6e 74 65 72 43 72 69 74   to TryEnterCrit
982a0 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 69 73  icalSection() is
982b0 20 23 69 66 64 65 66 27 65 64 20 6f 75 74 2c 20   #ifdef'ed out, 
982c0 73 6f 20 23 69 66 64 65 66 20 0a 2a 2a 20 74 68  so #ifdef .** th
982d0 69 73 20 6f 75 74 20 61 73 20 77 65 6c 6c 2e 0a  is out as well..
982e0 2a 2f 0a 23 69 66 20 30 0a 23 69 66 20 53 51 4c  */.#if 0.#if SQL
982f0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 20 64  ITE_OS_WINCE.# d
98300 65 66 69 6e 65 20 6d 75 74 65 78 49 73 4e 54 28  efine mutexIsNT(
98310 29 20 20 28 31 29 0a 23 65 6c 73 65 0a 20 20 73  )  (1).#else.  s
98320 74 61 74 69 63 20 69 6e 74 20 6d 75 74 65 78 49  tatic int mutexI
98330 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20 20 73  sNT(void){.    s
98340 74 61 74 69 63 20 69 6e 74 20 6f 73 54 79 70 65  tatic int osType
98350 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 6f 73   = 0;.    if( os
98360 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Type==0 ){.     
98370 20 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73   OSVERSIONINFO s
98380 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66  Info;.      sInf
98390 6f 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66  o.dwOSVersionInf
983a0 6f 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73  oSize = sizeof(s
983b0 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74  Info);.      Get
983c0 56 65 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f  VersionEx(&sInfo
983d0 29 3b 0a 20 20 20 20 20 20 6f 73 54 79 70 65 20  );.      osType 
983e0 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61 74 66 6f  = sInfo.dwPlatfo
983f0 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41 54 46 4f  rmId==VER_PLATFO
98400 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f 20 32 20  RM_WIN32_NT ? 2 
98410 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  : 1;.    }.    r
98420 65 74 75 72 6e 20 6f 73 54 79 70 65 3d 3d 32 3b  eturn osType==2;
98430 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
98440 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 2a  QLITE_OS_WINCE *
98450 2f 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  /.#endif..#ifdef
98460 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a   SQLITE_DEBUG./*
98470 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
98480 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64  mutex_held() and
98490 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e   sqlite3_mutex_n
984a0 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65  otheld() routine
984b0 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64   are.** intended
984c0 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e   for use only in
984d0 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74  side assert() st
984e0 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61  atements..*/.sta
984f0 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78  tic int winMutex
98500 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74  Held(sqlite3_mut
98510 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  ex *p){.  return
98520 20 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26 20 70   p->nRef!=0 && p
98530 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74 43 75 72 72  ->owner==GetCurr
98540 65 6e 74 54 68 72 65 61 64 49 64 28 29 3b 0a 7d  entThreadId();.}
98550 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d  .static int winM
98560 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69  utexNotheld(sqli
98570 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
98580 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d   return p->nRef=
98590 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72 21 3d  =0 || p->owner!=
985a0 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 64  GetCurrentThread
985b0 49 64 28 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  Id();.}.#endif..
985c0 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  ./*.** Initializ
985d0 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c 69  e and deinitiali
985e0 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 75 62  ze the mutex sub
985f0 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69  system..*/.stati
98600 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  c sqlite3_mutex 
98610 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d  winMutex_staticM
98620 75 74 65 78 65 73 5b 36 5d 3b 0a 73 74 61 74 69  utexes[6];.stati
98630 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 5f 69  c int winMutex_i
98640 73 49 6e 69 74 20 3d 20 30 3b 0a 2f 2a 20 41 73  sInit = 0;./* As
98650 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 28 29 20   winMutexInit() 
98660 61 6e 64 20 77 69 6e 4d 75 74 65 78 45 6e 64 28  and winMutexEnd(
98670 29 20 61 72 65 20 63 61 6c 6c 65 64 20 61 73 20  ) are called as 
98680 70 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20 73  part.** of the s
98690 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
986a0 65 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 68  e and sqlite3_sh
986b0 75 74 64 6f 77 6e 28 29 0a 2a 2a 20 70 72 6f 63  utdown().** proc
986c0 65 73 73 69 6e 67 2c 20 74 68 65 20 22 69 6e 74  essing, the "int
986d0 65 72 6c 6f 63 6b 65 64 22 20 6d 61 67 69 63 20  erlocked" magic 
986e0 69 73 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 0a  is probably not.
986f0 2a 2a 20 73 74 72 69 63 74 6c 79 20 6e 65 63 65  ** strictly nece
98700 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  ssary..*/.static
98710 20 6c 6f 6e 67 20 77 69 6e 4d 75 74 65 78 5f 6c   long winMutex_l
98720 6f 63 6b 20 3d 20 30 3b 0a 0a 73 74 61 74 69 63  ock = 0;..static
98730 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 49 6e 69   int winMutexIni
98740 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 54  t(void){ .  /* T
98750 68 65 20 66 69 72 73 74 20 74 6f 20 69 6e 63 72  he first to incr
98760 65 6d 65 6e 74 20 74 6f 20 31 20 64 6f 65 73 20  ement to 1 does 
98770 61 63 74 75 61 6c 20 69 6e 69 74 69 61 6c 69 7a  actual initializ
98780 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 49  ation */.  if( I
98790 6e 74 65 72 6c 6f 63 6b 65 64 43 6f 6d 70 61 72  nterlockedCompar
987a0 65 45 78 63 68 61 6e 67 65 28 26 77 69 6e 4d 75  eExchange(&winMu
987b0 74 65 78 5f 6c 6f 63 6b 2c 20 31 2c 20 30 29 3d  tex_lock, 1, 0)=
987c0 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  =0 ){.    int i;
987d0 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
987e0 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d 75 74  ArraySize(winMut
987f0 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73  ex_staticMutexes
98800 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 49  ); i++){.      I
98810 6e 69 74 69 61 6c 69 7a 65 43 72 69 74 69 63 61  nitializeCritica
98820 6c 53 65 63 74 69 6f 6e 28 26 77 69 6e 4d 75 74  lSection(&winMut
98830 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73  ex_staticMutexes
98840 5b 69 5d 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  [i].mutex);.    
98850 7d 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 5f 69  }.    winMutex_i
98860 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 65 6c  sInit = 1;.  }el
98870 73 65 7b 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 6f  se{.    /* Someo
98880 6e 65 20 65 6c 73 65 20 69 73 20 69 6e 20 74 68  ne else is in th
98890 65 20 70 72 6f 63 65 73 73 20 6f 66 20 69 6e 69  e process of ini
988a0 74 69 6e 67 20 74 68 65 20 73 74 61 74 69 63 20  ting the static 
988b0 6d 75 74 65 78 65 73 20 2a 2f 0a 20 20 20 20 77  mutexes */.    w
988c0 68 69 6c 65 28 20 21 77 69 6e 4d 75 74 65 78 5f  hile( !winMutex_
988d0 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20  isInit ){.      
988e0 53 6c 65 65 70 28 31 29 3b 0a 20 20 20 20 7d 0a  Sleep(1);.    }.
988f0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
98900 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 73 74 61 74  ITE_OK; .}..stat
98910 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 45  ic int winMutexE
98920 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20  nd(void){ .  /* 
98930 54 68 65 20 66 69 72 73 74 20 74 6f 20 64 65 63  The first to dec
98940 72 65 6d 65 6e 74 20 74 6f 20 30 20 64 6f 65 73  rement to 0 does
98950 20 61 63 74 75 61 6c 20 73 68 75 74 64 6f 77 6e   actual shutdown
98960 20 0a 20 20 2a 2a 20 28 77 68 69 63 68 20 73 68   .  ** (which sh
98970 6f 75 6c 64 20 62 65 20 74 68 65 20 6c 61 73 74  ould be the last
98980 20 74 6f 20 73 68 75 74 64 6f 77 6e 2e 29 20 2a   to shutdown.) *
98990 2f 0a 20 20 69 66 28 20 49 6e 74 65 72 6c 6f 63  /.  if( Interloc
989a0 6b 65 64 43 6f 6d 70 61 72 65 45 78 63 68 61 6e  kedCompareExchan
989b0 67 65 28 26 77 69 6e 4d 75 74 65 78 5f 6c 6f 63  ge(&winMutex_loc
989c0 6b 2c 20 30 2c 20 31 29 3d 3d 31 20 29 7b 0a 20  k, 0, 1)==1 ){. 
989d0 20 20 20 69 66 28 20 77 69 6e 4d 75 74 65 78 5f     if( winMutex_
989e0 69 73 49 6e 69 74 3d 3d 31 20 29 7b 0a 20 20 20  isInit==1 ){.   
989f0 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
98a00 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79  for(i=0; i<Array
98a10 53 69 7a 65 28 77 69 6e 4d 75 74 65 78 5f 73 74  Size(winMutex_st
98a20 61 74 69 63 4d 75 74 65 78 65 73 29 3b 20 69 2b  aticMutexes); i+
98a30 2b 29 7b 0a 20 20 20 20 20 20 20 20 44 65 6c 65  +){.        Dele
98a40 74 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f  teCriticalSectio
98a50 6e 28 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74  n(&winMutex_stat
98a60 69 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74  icMutexes[i].mut
98a70 65 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ex);.      }.   
98a80 20 20 20 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e     winMutex_isIn
98a90 69 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  it = 0;.    }.  
98aa0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
98ab0 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  E_OK; .}../*.** 
98ac0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
98ad0 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e  x_alloc() routin
98ae0 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65  e allocates a ne
98af0 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72  w.** mutex and r
98b00 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
98b10 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72   to it.  If it r
98b20 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74  eturns NULL.** t
98b30 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61  hat means that a
98b40 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74   mutex could not
98b50 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20   be allocated.  
98b60 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 75  SQLite.** will u
98b70 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20  nwind its stack 
98b80 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72  and return an er
98b90 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65  ror.  The argume
98ba0 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33  nt.** to sqlite3
98bb0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69  _mutex_alloc() i
98bc0 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69  s one of these i
98bd0 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73  nteger constants
98be0 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  :.**.** <ul>.** 
98bf0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
98c00 45 58 5f 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20  EX_FAST.** <li> 
98c10 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
98c20 43 55 52 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20  CURSIVE.** <li> 
98c30 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
98c40 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c  ATIC_MASTER.** <
98c50 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
98c60 58 5f 53 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20  X_STATIC_MEM.** 
98c70 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
98c80 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32 0a 2a  EX_STATIC_MEM2.*
98c90 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
98ca0 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47  UTEX_STATIC_PRNG
98cb0 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
98cc0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52  _MUTEX_STATIC_LR
98cd0 55 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  U.** <li>  SQLIT
98ce0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c  E_MUTEX_STATIC_L
98cf0 52 55 32 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a  RU2.** </ul>.**.
98d00 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 6f  ** The first two
98d10 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65   constants cause
98d20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
98d30 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65  lloc() to create
98d40 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e  .** a new mutex.
98d50 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20    The new mutex 
98d60 69 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65  is recursive whe
98d70 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  n SQLITE_MUTEX_R
98d80 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75  ECURSIVE.** is u
98d90 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65  sed but not nece
98da0 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20  ssarily so when 
98db0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
98dc0 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68  T is used..** Th
98dd0 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e  e mutex implemen
98de0 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  tation does not 
98df0 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64  need to make a d
98e00 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65  istinction.** be
98e10 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54  tween SQLITE_MUT
98e20 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64  EX_RECURSIVE and
98e30 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
98e40 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a  ST if it does.**
98e50 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42   not want to.  B
98e60 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f  ut SQLite will o
98e70 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65  nly request a re
98e80 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e  cursive mutex in
98e90 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20  .** cases where 
98ea0 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20  it really needs 
98eb0 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 65  one.  If a faste
98ec0 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20  r non-recursive 
98ed0 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65  mutex.** impleme
98ee0 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c  ntation is avail
98ef0 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74  able on the host
98f00 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d   platform, the m
98f10 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a  utex subsystem.*
98f20 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73  * might return s
98f30 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72  uch a mutex in r
98f40 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54  esponse to SQLIT
98f50 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a  E_MUTEX_FAST..**
98f60 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c  .** The other al
98f70 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73  lowed parameters
98f80 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   to sqlite3_mute
98f90 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72  x_alloc() each r
98fa0 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74  eturn.** a point
98fb0 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70  er to a static p
98fc0 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78  reexisting mutex
98fd0 2e 20 20 53 69 78 20 73 74 61 74 69 63 20 6d 75  .  Six static mu
98fe0 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65  texes are.** use
98ff0 64 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74  d by the current
99000 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69   version of SQLi
99010 74 65 2e 20 20 46 75 74 75 72 65 20 76 65 72 73  te.  Future vers
99020 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a  ions of SQLite.*
99030 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69 74 69  * may add additi
99040 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65  onal static mute
99050 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74  xes.  Static mut
99060 65 78 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74  exes are for int
99070 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20  ernal.** use by 
99080 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70  SQLite only.  Ap
99090 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20  plications that 
990a0 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78  use SQLite mutex
990b0 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65  es should.** use
990c0 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69   only the dynami
990d0 63 20 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e  c mutexes return
990e0 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54  ed by SQLITE_MUT
990f0 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51  EX_FAST or.** SQ
99100 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
99110 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  SIVE..**.** Note
99120 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20   that if one of 
99130 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65  the dynamic mute
99140 78 20 70 61 72 61 6d 65 74 65 72 73 20 28 53 51  x parameters (SQ
99150 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a  LITE_MUTEX_FAST.
99160 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54  ** or SQLITE_MUT
99170 45 58 5f 52 45 43 55 52 53 49 56 45 29 20 69 73  EX_RECURSIVE) is
99180 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74   used then sqlit
99190 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
991a0 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69  .** returns a di
991b0 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e  fferent mutex on
991c0 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 42 75   every call.  Bu
991d0 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 69 63  t for the static
991e0 20 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 73   .** mutex types
991f0 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65 78  , the same mutex
99200 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20   is returned on 
99210 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74 20  every call that 
99220 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20  has.** the same 
99230 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a  type number..*/.
99240 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d  static sqlite3_m
99250 75 74 65 78 20 2a 77 69 6e 4d 75 74 65 78 41 6c  utex *winMutexAl
99260 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a  loc(int iType){.
99270 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
99280 2a 70 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 69  *p;..  switch( i
99290 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65  Type ){.    case
992a0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
992b0 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c  ST:.    case SQL
992c0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
992d0 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d  IVE: {.      p =
992e0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65   sqlite3MallocZe
992f0 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29  ro( sizeof(*p) )
99300 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 29 7b  ;.      if( p ){
99310 20 20 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64    .        p->id
99320 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20   = iType;.      
99330 20 20 49 6e 69 74 69 61 6c 69 7a 65 43 72 69 74    InitializeCrit
99340 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e  icalSection(&p->
99350 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 7d 0a  mutex);.      }.
99360 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
99370 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20   }.    default: 
99380 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
99390 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 3d  winMutex_isInit=
993a0 3d 31 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  =1 );.      asse
993b0 72 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30  rt( iType-2 >= 0
993c0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
993d0 28 20 69 54 79 70 65 2d 32 20 3c 20 41 72 72 61  ( iType-2 < Arra
993e0 79 53 69 7a 65 28 77 69 6e 4d 75 74 65 78 5f 73  ySize(winMutex_s
993f0 74 61 74 69 63 4d 75 74 65 78 65 73 29 20 29 3b  taticMutexes) );
99400 0a 20 20 20 20 20 20 70 20 3d 20 26 77 69 6e 4d  .      p = &winM
99410 75 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78  utex_staticMutex
99420 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20 20  es[iType-2];.   
99430 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65     p->id = iType
99440 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
99450 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
99460 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54  n p;.}.../*.** T
99470 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 61 6c  his routine deal
99480 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 69 6f  locates a previo
99490 75 73 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65  usly.** allocate
994a0 64 20 6d 75 74 65 78 2e 20 20 53 51 4c 69 74 65  d mutex.  SQLite
994b0 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64   is careful to d
994c0 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 0a  eallocate every.
994d0 2a 2a 20 6d 75 74 65 78 20 74 68 61 74 20 69 74  ** mutex that it
994e0 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73   allocates..*/.s
994f0 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 4d 75  tatic void winMu
99500 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f  texFree(sqlite3_
99510 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73  mutex *p){.  ass
99520 65 72 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65  ert( p );.  asse
99530 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29  rt( p->nRef==0 )
99540 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  ;.  assert( p->i
99550 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
99560 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53  FAST || p->id==S
99570 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
99580 52 53 49 56 45 20 29 3b 0a 20 20 44 65 6c 65 74  RSIVE );.  Delet
99590 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e  eCriticalSection
995a0 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73  (&p->mutex);.  s
995b0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
995c0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
995d0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
995e0 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  () and sqlite3_m
995f0 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69  utex_try() routi
99600 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74  nes attempt.** t
99610 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e  o enter a mutex.
99620 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72    If another thr
99630 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77  ead is already w
99640 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c  ithin the mutex,
99650 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  .** sqlite3_mute
99660 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62  x_enter() will b
99670 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33  lock and sqlite3
99680 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c  _mutex_try() wil
99690 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49  l return.** SQLI
996a0 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71  TE_BUSY.  The sq
996b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
996c0 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ) interface retu
996d0 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a  rns SQLITE_OK.**
996e0 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c   upon successful
996f0 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73   entry.  Mutexes
99700 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53   created using S
99710 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
99720 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20  RSIVE can.** be 
99730 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65  entered multiple
99740 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61   times by the sa
99750 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73  me thread.  In s
99760 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a  uch cases the,.*
99770 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  * mutex must be 
99780 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20  exited an equal 
99790 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  number of times 
997a0 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74  before another t
997b0 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74  hread.** can ent
997c0 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65  er.  If the same
997d0 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f   thread tries to
997e0 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72   enter any other
997f0 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a   kind of mutex.*
99800 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  * more than once
99810 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  , the behavior i
99820 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  s undefined..*/.
99830 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 4d  static void winM
99840 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65  utexEnter(sqlite
99850 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61  3_mutex *p){.  a
99860 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
99870 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
99880 53 49 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78  SIVE || winMutex
99890 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20  Notheld(p) );.  
998a0 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63  EnterCriticalSec
998b0 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b  tion(&p->mutex);
998c0 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 47 65  .  p->owner = Ge
998d0 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64  tCurrentThreadId
998e0 28 29 3b 20 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b  (); .  p->nRef++
998f0 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77  ;.}.static int w
99900 69 6e 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74  inMutexTry(sqlit
99910 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
99920 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
99930 42 55 53 59 3b 0a 20 20 61 73 73 65 72 74 28 20  BUSY;.  assert( 
99940 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
99950 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c  TEX_RECURSIVE ||
99960 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64   winMutexNotheld
99970 28 70 29 20 29 3b 0a 20 20 2f 2a 0a 20 20 2a 2a  (p) );.  /*.  **
99980 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
99990 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65  ex_try() routine
999a0 20 69 73 20 76 65 72 79 20 72 61 72 65 6c 79 20   is very rarely 
999b0 75 73 65 64 2c 20 61 6e 64 20 77 68 65 6e 20 69  used, and when i
999c0 74 0a 20 20 2a 2a 20 69 73 20 75 73 65 64 20 69  t.  ** is used i
999d0 74 20 69 73 20 6d 65 72 65 6c 79 20 61 6e 20 6f  t is merely an o
999e0 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 53 6f  ptimization.  So
999f0 20 69 74 20 69 73 20 4f 4b 20 66 6f 72 20 69 74   it is OK for it
99a00 20 74 6f 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20   to always.  ** 
99a10 66 61 69 6c 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a  fail.  .  **.  *
99a20 2a 20 54 68 65 20 54 72 79 45 6e 74 65 72 43 72  * The TryEnterCr
99a30 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20  iticalSection() 
99a40 69 6e 74 65 72 66 61 63 65 20 69 73 20 6f 6e 6c  interface is onl
99a50 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 57  y available on W
99a60 69 6e 4e 54 2e 0a 20 20 2a 2a 20 41 6e 64 20 73  inNT..  ** And s
99a70 6f 6d 65 20 77 69 6e 64 6f 77 73 20 63 6f 6d 70  ome windows comp
99a80 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 69  ilers complain i
99a90 66 20 79 6f 75 20 74 72 79 20 74 6f 20 75 73 65  f you try to use
99aa0 20 69 74 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a   it without.  **
99ab0 20 66 69 72 73 74 20 64 6f 69 6e 67 20 73 6f 6d   first doing som
99ac0 65 20 23 64 65 66 69 6e 65 73 20 74 68 61 74 20  e #defines that 
99ad0 70 72 65 76 65 6e 74 20 53 51 4c 69 74 65 20 66  prevent SQLite f
99ae0 72 6f 6d 20 62 75 69 6c 64 69 6e 67 20 6f 6e 20  rom building on 
99af0 57 69 6e 39 38 2e 0a 20 20 2a 2a 20 46 6f 72 20  Win98..  ** For 
99b00 74 68 61 74 20 72 65 61 73 6f 6e 2c 20 77 65 20  that reason, we 
99b10 77 69 6c 6c 20 6f 6d 69 74 20 74 68 69 73 20 6f  will omit this o
99b20 70 74 69 6d 69 7a 61 74 69 6f 6e 20 66 6f 72 20  ptimization for 
99b30 6e 6f 77 2e 20 20 53 65 65 0a 20 20 2a 2a 20 74  now.  See.  ** t
99b40 69 63 6b 65 74 20 23 32 36 38 35 2e 0a 20 20 2a  icket #2685..  *
99b50 2f 0a 23 69 66 20 30 0a 20 20 69 66 28 20 6d 75  /.#if 0.  if( mu
99b60 74 65 78 49 73 4e 54 28 29 20 26 26 20 54 72 79  texIsNT() && Try
99b70 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63  EnterCriticalSec
99b80 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 20  tion(&p->mutex) 
99b90 29 7b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20  ){.    p->owner 
99ba0 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68 72 65  = GetCurrentThre
99bb0 61 64 49 64 28 29 3b 0a 20 20 20 20 70 2d 3e 6e  adId();.    p->n
99bc0 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20  Ref++;.    rc = 
99bd0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 23  SQLITE_OK;.  }.#
99be0 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41  else.  UNUSED_PA
99bf0 52 41 4d 45 54 45 52 28 70 29 3b 0a 23 65 6e 64  RAMETER(p);.#end
99c00 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  if.  return rc;.
99c10 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
99c20 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
99c30 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73  () routine exits
99c40 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61   a mutex that wa
99c50 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20  s.** previously 
99c60 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73  entered by the s
99c70 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65  ame thread.  The
99c80 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20   behavior.** is 
99c90 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65  undefined if the
99ca0 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75   mutex is not cu
99cb0 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20  rrently entered 
99cc0 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72  or.** is not cur
99cd0 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64  rently allocated
99ce0 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e  .  SQLite will n
99cf0 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a  ever do either..
99d00 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
99d10 69 6e 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c  inMutexLeave(sql
99d20 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
99d30 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
99d40 66 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  f>0 );.  assert(
99d50 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74 43 75   p->owner==GetCu
99d60 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29 20  rrentThreadId() 
99d70 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a  );.  p->nRef--;.
99d80 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
99d90 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53  f==0 || p->id==S
99da0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
99db0 52 53 49 56 45 20 29 3b 0a 20 20 4c 65 61 76 65  RSIVE );.  Leave
99dc0 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28  CriticalSection(
99dd0 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53  &p->mutex);.}..S
99de0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
99df0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
99e00 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61  ods *sqlite3Defa
99e10 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a  ultMutex(void){.
99e20 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33    static sqlite3
99e30 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73  _mutex_methods s
99e40 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 77 69  Mutex = {.    wi
99e50 6e 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20  nMutexInit,.    
99e60 77 69 6e 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20  winMutexEnd,.   
99e70 20 77 69 6e 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a   winMutexAlloc,.
99e80 20 20 20 20 77 69 6e 4d 75 74 65 78 46 72 65 65      winMutexFree
99e90 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 45 6e  ,.    winMutexEn
99ea0 74 65 72 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65  ter,.    winMute
99eb0 78 54 72 79 2c 0a 20 20 20 20 77 69 6e 4d 75 74  xTry,.    winMut
99ec0 65 78 4c 65 61 76 65 2c 0a 23 69 66 64 65 66 20  exLeave,.#ifdef 
99ed0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
99ee0 20 77 69 6e 4d 75 74 65 78 48 65 6c 64 2c 0a 20   winMutexHeld,. 
99ef0 20 20 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65     winMutexNothe
99f00 6c 64 0a 23 65 6c 73 65 0a 20 20 20 20 30 2c 0a  ld.#else.    0,.
99f10 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 20 20 7d      0.#endif.  }
99f20 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75  ;..  return &sMu
99f30 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  tex;.}.#endif /*
99f40 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33   SQLITE_MUTEX_W3
99f50 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  2 */../*********
99f60 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74  ***** End of mut
99f70 65 78 5f 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a  ex_w32.c *******
99f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99fa0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
99fb0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
99fc0 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a   malloc.c ******
99fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
99ff0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
9a000 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
9a010 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
9a020 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
9a030 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
9a040 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
9a050 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
9a060 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
9a070 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
9a080 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
9a090 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
9a0a0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
9a0b0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
9a0c0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
9a0d0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
9a0e0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
9a0f0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
9a100 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
9a110 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
9a120 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
9a130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
9a170 2a 2a 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ** Memory alloca
9a180 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 75  tion functions u
9a190 73 65 64 20 74 68 72 6f 75 67 68 6f 75 74 20 73  sed throughout s
9a1a0 71 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  qlite..**.** $Id
9a1b0 3a 20 6d 61 6c 6c 6f 63 2e 63 2c 76 20 31 2e 36  : malloc.c,v 1.6
9a1c0 36 20 32 30 30 39 2f 30 37 2f 31 37 20 31 31 3a  6 2009/07/17 11:
9a1d0 34 34 3a 30 37 20 64 72 68 20 45 78 70 20 24 0a  44:07 drh Exp $.
9a1e0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  */../*.** This r
9a1f0 6f 75 74 69 6e 65 20 72 75 6e 73 20 77 68 65 6e  outine runs when
9a200 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
9a210 63 61 74 6f 72 20 73 65 65 73 20 74 68 61 74 20  cator sees that 
9a220 74 68 65 0a 2a 2a 20 74 6f 74 61 6c 20 6d 65 6d  the.** total mem
9a230 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  ory allocation i
9a240 73 20 61 62 6f 75 74 20 74 6f 20 65 78 63 65 65  s about to excee
9a250 64 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 0a  d the soft heap.
9a260 2a 2a 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 73 74 61  ** limit..*/.sta
9a270 74 69 63 20 76 6f 69 64 20 73 6f 66 74 48 65 61  tic void softHea
9a280 70 4c 69 6d 69 74 45 6e 66 6f 72 63 65 72 28 0a  pLimitEnforcer(.
9a290 20 20 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c    void *NotUsed,
9a2a0 20 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36   .  sqlite3_int6
9a2b0 34 20 4e 6f 74 55 73 65 64 32 2c 0a 20 20 69 6e  4 NotUsed2,.  in
9a2c0 74 20 61 6c 6c 6f 63 53 69 7a 65 0a 29 7b 0a 20  t allocSize.){. 
9a2d0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
9a2e0 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
9a2f0 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33  sed2);.  sqlite3
9a300 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
9a310 61 6c 6c 6f 63 53 69 7a 65 29 3b 0a 7d 0a 0a 2f  allocSize);.}../
9a320 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 6f 66  *.** Set the sof
9a330 74 20 68 65 61 70 2d 73 69 7a 65 20 6c 69 6d 69  t heap-size limi
9a340 74 20 66 6f 72 20 74 68 65 20 6c 69 62 72 61 72  t for the librar
9a350 79 2e 20 50 61 73 73 69 6e 67 20 61 20 7a 65 72  y. Passing a zer
9a360 6f 20 6f 72 20 0a 2a 2a 20 6e 65 67 61 74 69 76  o or .** negativ
9a370 65 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65  e value indicate
9a380 73 20 6e 6f 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 53  s no limit..*/.S
9a390 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
9a3a0 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70  qlite3_soft_heap
9a3b0 5f 6c 69 6d 69 74 28 69 6e 74 20 6e 29 7b 0a 20  _limit(int n){. 
9a3c0 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 20   sqlite3_uint64 
9a3d0 69 4c 69 6d 69 74 3b 0a 20 20 69 6e 74 20 6f 76  iLimit;.  int ov
9a3e0 65 72 61 67 65 3b 0a 20 20 69 66 28 20 6e 3c 30  erage;.  if( n<0
9a3f0 20 29 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d   ){.    iLimit =
9a400 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
9a410 20 69 4c 69 6d 69 74 20 3d 20 6e 3b 0a 20 20 7d   iLimit = n;.  }
9a420 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
9a430 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20  OMIT_AUTOINIT.  
9a440 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
9a450 7a 65 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69  ze();.#endif.  i
9a460 66 28 20 69 4c 69 6d 69 74 3e 30 20 29 7b 0a 20  f( iLimit>0 ){. 
9a470 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79     sqlite3Memory
9a480 41 6c 61 72 6d 28 73 6f 66 74 48 65 61 70 4c 69  Alarm(softHeapLi
9a490 6d 69 74 45 6e 66 6f 72 63 65 72 2c 20 30 2c 20  mitEnforcer, 0, 
9a4a0 69 4c 69 6d 69 74 29 3b 0a 20 20 7d 65 6c 73 65  iLimit);.  }else
9a4b0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  {.    sqlite3Mem
9a4c0 6f 72 79 41 6c 61 72 6d 28 30 2c 20 30 2c 20 30  oryAlarm(0, 0, 0
9a4d0 29 3b 0a 20 20 7d 0a 20 20 6f 76 65 72 61 67 65  );.  }.  overage
9a4e0 20 3d 20 28 69 6e 74 29 28 73 71 6c 69 74 65 33   = (int)(sqlite3
9a4f0 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 20 2d  _memory_used() -
9a500 20 28 69 36 34 29 6e 29 3b 0a 20 20 69 66 28 20   (i64)n);.  if( 
9a510 6f 76 65 72 61 67 65 3e 30 20 29 7b 0a 20 20 20  overage>0 ){.   
9a520 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
9a530 5f 6d 65 6d 6f 72 79 28 6f 76 65 72 61 67 65 29  _memory(overage)
9a540 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
9a550 74 74 65 6d 70 74 20 74 6f 20 72 65 6c 65 61 73  ttempt to releas
9a560 65 20 75 70 20 74 6f 20 6e 20 62 79 74 65 73 20  e up to n bytes 
9a570 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c  of non-essential
9a580 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c   memory currentl
9a590 79 0a 2a 2a 20 68 65 6c 64 20 62 79 20 53 51 4c  y.** held by SQL
9a5a0 69 74 65 2e 20 41 6e 20 65 78 61 6d 70 6c 65 20  ite. An example 
9a5b0 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c  of non-essential
9a5c0 20 6d 65 6d 6f 72 79 20 69 73 20 6d 65 6d 6f 72   memory is memor
9a5d0 79 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 61 63  y used to.** cac
9a5e0 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  he database page
9a5f0 73 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 63  s that are not c
9a600 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e  urrently in use.
9a610 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
9a620 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61  nt sqlite3_relea
9a630 73 65 5f 6d 65 6d 6f 72 79 28 69 6e 74 20 6e 29  se_memory(int n)
9a640 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  {.#ifdef SQLITE_
9a650 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
9a660 4e 41 47 45 4d 45 4e 54 0a 20 20 69 6e 74 20 6e  NAGEMENT.  int n
9a670 52 65 74 20 3d 20 30 3b 0a 23 69 66 20 30 0a 20  Ret = 0;.#if 0. 
9a680 20 6e 52 65 74 20 2b 3d 20 73 71 6c 69 74 65 33   nRet += sqlite3
9a690 56 64 62 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72  VdbeReleaseMemor
9a6a0 79 28 6e 29 3b 0a 23 65 6e 64 69 66 0a 20 20 6e  y(n);.#endif.  n
9a6b0 52 65 74 20 2b 3d 20 73 71 6c 69 74 65 33 50 63  Ret += sqlite3Pc
9a6c0 61 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72  acheReleaseMemor
9a6d0 79 28 6e 2d 6e 52 65 74 29 3b 0a 20 20 72 65 74  y(n-nRet);.  ret
9a6e0 75 72 6e 20 6e 52 65 74 3b 0a 23 65 6c 73 65 0a  urn nRet;.#else.
9a6f0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
9a700 45 52 28 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20  ER(n);.  return 
9a710 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69  SQLITE_OK;.#endi
9a720 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 74 65  f.}../*.** State
9a730 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6c 6f 63   information loc
9a740 61 6c 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79  al to the memory
9a750 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
9a760 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63  ystem..*/.static
9a770 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75   SQLITE_WSD stru
9a780 63 74 20 4d 65 6d 30 47 6c 6f 62 61 6c 20 7b 0a  ct Mem0Global {.
9a790 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66    /* Number of f
9a7a0 72 65 65 20 70 61 67 65 73 20 66 6f 72 20 73 63  ree pages for sc
9a7b0 72 61 74 63 68 20 61 6e 64 20 70 61 67 65 2d 63  ratch and page-c
9a7c0 61 63 68 65 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20  ache memory */. 
9a7d0 20 75 33 32 20 6e 53 63 72 61 74 63 68 46 72 65   u32 nScratchFre
9a7e0 65 3b 0a 20 20 75 33 32 20 6e 50 61 67 65 46 72  e;.  u32 nPageFr
9a7f0 65 65 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ee;..  sqlite3_m
9a800 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20  utex *mutex;    
9a810 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 6f       /* Mutex to
9a820 20 73 65 72 69 61 6c 69 7a 65 20 61 63 63 65 73   serialize acces
9a830 73 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  s */..  /*.  ** 
9a840 54 68 65 20 61 6c 61 72 6d 20 63 61 6c 6c 62 61  The alarm callba
9a850 63 6b 20 61 6e 64 20 69 74 73 20 61 72 67 75 6d  ck and its argum
9a860 65 6e 74 73 2e 20 20 54 68 65 20 6d 65 6d 30 2e  ents.  The mem0.
9a870 6d 75 74 65 78 20 6c 6f 63 6b 20 77 69 6c 6c 0a  mutex lock will.
9a880 20 20 2a 2a 20 62 65 20 68 65 6c 64 20 77 68 69    ** be held whi
9a890 6c 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  le the callback 
9a8a0 69 73 20 72 75 6e 6e 69 6e 67 2e 20 20 52 65 63  is running.  Rec
9a8b0 75 72 73 69 76 65 20 63 61 6c 6c 73 20 69 6e 74  ursive calls int
9a8c0 6f 0a 20 20 2a 2a 20 74 68 65 20 6d 65 6d 6f 72  o.  ** the memor
9a8d0 79 20 73 75 62 73 79 73 74 65 6d 20 61 72 65 20  y subsystem are 
9a8e0 61 6c 6c 6f 77 65 64 2c 20 62 75 74 20 6e 6f 20  allowed, but no 
9a8f0 6e 65 77 20 63 61 6c 6c 62 61 63 6b 73 20 77 69  new callbacks wi
9a900 6c 6c 20 62 65 0a 20 20 2a 2a 20 69 73 73 75 65  ll be.  ** issue
9a910 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  d..  */.  sqlite
9a920 33 5f 69 6e 74 36 34 20 61 6c 61 72 6d 54 68 72  3_int64 alarmThr
9a930 65 73 68 6f 6c 64 3b 0a 20 20 76 6f 69 64 20 28  eshold;.  void (
9a940 2a 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 29 28  *alarmCallback)(
9a950 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69  void*, sqlite3_i
9a960 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 76 6f 69  nt64,int);.  voi
9a970 64 20 2a 61 6c 61 72 6d 41 72 67 3b 0a 0a 20 20  d *alarmArg;..  
9a980 2f 2a 0a 20 20 2a 2a 20 50 6f 69 6e 74 65 72 73  /*.  ** Pointers
9a990 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 73   to the end of s
9a9a0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9a9b0 69 67 2e 70 53 63 72 61 74 63 68 20 61 6e 64 0a  ig.pScratch and.
9a9c0 20 20 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62    ** sqlite3Glob
9a9d0 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 74  alConfig.pPage t
9a9e0 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d  o a block of mem
9a9f0 6f 72 79 20 74 68 61 74 20 72 65 63 6f 72 64 73  ory that records
9aa00 0a 20 20 2a 2a 20 77 68 69 63 68 20 70 61 67 65  .  ** which page
9aa10 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e  s are available.
9aa20 0a 20 20 2a 2f 0a 20 20 75 33 32 20 2a 61 53 63  .  */.  u32 *aSc
9aa30 72 61 74 63 68 46 72 65 65 3b 0a 20 20 75 33 32  ratchFree;.  u32
9aa40 20 2a 61 50 61 67 65 46 72 65 65 3b 0a 7d 20 6d   *aPageFree;.} m
9aa50 65 6d 30 20 3d 20 7b 20 30 2c 20 30 2c 20 30 2c  em0 = { 0, 0, 0,
9aa60 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 20 7d   0, 0, 0, 0, 0 }
9aa70 3b 0a 0a 23 64 65 66 69 6e 65 20 6d 65 6d 30 20  ;..#define mem0 
9aa80 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65  GLOBAL(struct Me
9aa90 6d 30 47 6c 6f 62 61 6c 2c 20 6d 65 6d 30 29 0a  m0Global, mem0).
9aaa0 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  ./*.** Initializ
9aab0 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  e the memory all
9aac0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
9aad0 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  m..*/.SQLITE_PRI
9aae0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
9aaf0 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64 29  MallocInit(void)
9ab00 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  {.  if( sqlite3G
9ab10 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d  lobalConfig.m.xM
9ab20 61 6c 6c 6f 63 3d 3d 30 20 29 7b 0a 20 20 20 20  alloc==0 ){.    
9ab30 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66  sqlite3MemSetDef
9ab40 61 75 6c 74 28 29 3b 0a 20 20 7d 0a 20 20 6d 65  ault();.  }.  me
9ab50 6d 73 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73  mset(&mem0, 0, s
9ab60 69 7a 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 20 20  izeof(mem0));.  
9ab70 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
9ab80 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74  lConfig.bCoreMut
9ab90 65 78 20 29 7b 0a 20 20 20 20 6d 65 6d 30 2e 6d  ex ){.    mem0.m
9aba0 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75  utex = sqlite3Mu
9abb0 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
9abc0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d  MUTEX_STATIC_MEM
9abd0 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  );.  }.  if( sql
9abe0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9abf0 2e 70 53 63 72 61 74 63 68 20 26 26 20 73 71 6c  .pScratch && sql
9ac00 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9ac10 2e 73 7a 53 63 72 61 74 63 68 3e 3d 31 30 30 0a  .szScratch>=100.
9ac20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33        && sqlite3
9ac30 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63  GlobalConfig.nSc
9ac40 72 61 74 63 68 3e 3d 30 20 29 7b 0a 20 20 20 20  ratch>=0 ){.    
9ac50 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74  int i;.    sqlit
9ac60 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73  e3GlobalConfig.s
9ac70 7a 53 63 72 61 74 63 68 20 3d 20 52 4f 55 4e 44  zScratch = ROUND
9ac80 44 4f 57 4e 38 28 73 71 6c 69 74 65 33 47 6c 6f  DOWN8(sqlite3Glo
9ac90 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61  balConfig.szScra
9aca0 74 63 68 2d 34 29 3b 0a 20 20 20 20 6d 65 6d 30  tch-4);.    mem0
9acb0 2e 61 53 63 72 61 74 63 68 46 72 65 65 20 3d 20  .aScratchFree = 
9acc0 28 75 33 32 2a 29 26 28 28 63 68 61 72 2a 29 73  (u32*)&((char*)s
9acd0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9ace0 69 67 2e 70 53 63 72 61 74 63 68 29 0a 20 20 20  ig.pScratch).   
9acf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5b                 [
9ad00 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9ad10 66 69 67 2e 73 7a 53 63 72 61 74 63 68 2a 73 71  fig.szScratch*sq
9ad20 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9ad30 67 2e 6e 53 63 72 61 74 63 68 5d 3b 0a 20 20 20  g.nScratch];.   
9ad40 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 71 6c 69   for(i=0; i<sqli
9ad50 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9ad60 6e 53 63 72 61 74 63 68 3b 20 69 2b 2b 29 7b 20  nScratch; i++){ 
9ad70 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65  mem0.aScratchFre
9ad80 65 5b 69 5d 20 3d 20 69 3b 20 7d 0a 20 20 20 20  e[i] = i; }.    
9ad90 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65  mem0.nScratchFre
9ada0 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  e = sqlite3Globa
9adb0 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68  lConfig.nScratch
9adc0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
9add0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9ade0 69 67 2e 70 53 63 72 61 74 63 68 20 3d 20 30 3b  ig.pScratch = 0;
9adf0 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
9ae00 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74  alConfig.szScrat
9ae10 63 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66  ch = 0;.  }.  if
9ae20 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
9ae30 6f 6e 66 69 67 2e 70 50 61 67 65 20 26 26 20 73  onfig.pPage && s
9ae40 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9ae50 69 67 2e 73 7a 50 61 67 65 3e 3d 35 31 32 0a 20  ig.szPage>=512. 
9ae60 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 47       && sqlite3G
9ae70 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67  lobalConfig.nPag
9ae80 65 3e 3d 31 20 29 7b 0a 20 20 20 20 69 6e 74 20  e>=1 ){.    int 
9ae90 69 3b 0a 20 20 20 20 69 6e 74 20 6f 76 65 72 68  i;.    int overh
9aea0 65 61 64 3b 0a 20 20 20 20 69 6e 74 20 73 7a 20  ead;.    int sz 
9aeb0 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73 71 6c  = ROUNDDOWN8(sql
9aec0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9aed0 2e 73 7a 50 61 67 65 29 3b 0a 20 20 20 20 69 6e  .szPage);.    in
9aee0 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f  t n = sqlite3Glo
9aef0 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3b  balConfig.nPage;
9af00 0a 20 20 20 20 6f 76 65 72 68 65 61 64 20 3d 20  .    overhead = 
9af10 28 34 2a 6e 20 2b 20 73 7a 20 2d 20 31 29 2f 73  (4*n + sz - 1)/s
9af20 7a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  z;.    sqlite3Gl
9af30 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65  obalConfig.nPage
9af40 20 2d 3d 20 6f 76 65 72 68 65 61 64 3b 0a 20 20   -= overhead;.  
9af50 20 20 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65    mem0.aPageFree
9af60 20 3d 20 28 75 33 32 2a 29 26 28 28 63 68 61 72   = (u32*)&((char
9af70 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  *)sqlite3GlobalC
9af80 6f 6e 66 69 67 2e 70 50 61 67 65 29 0a 20 20 20  onfig.pPage).   
9af90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5b                 [
9afa0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9afb0 66 69 67 2e 73 7a 50 61 67 65 2a 73 71 6c 69 74  fig.szPage*sqlit
9afc0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
9afd0 50 61 67 65 5d 3b 0a 20 20 20 20 66 6f 72 28 69  Page];.    for(i
9afe0 3d 30 3b 20 69 3c 73 71 6c 69 74 65 33 47 6c 6f  =0; i<sqlite3Glo
9aff0 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3b  balConfig.nPage;
9b000 20 69 2b 2b 29 7b 20 6d 65 6d 30 2e 61 50 61 67   i++){ mem0.aPag
9b010 65 46 72 65 65 5b 69 5d 20 3d 20 69 3b 20 7d 0a  eFree[i] = i; }.
9b020 20 20 20 20 6d 65 6d 30 2e 6e 50 61 67 65 46 72      mem0.nPageFr
9b030 65 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  ee = sqlite3Glob
9b040 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3b 0a  alConfig.nPage;.
9b050 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
9b060 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9b070 2e 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20  .pPage = 0;.    
9b080 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9b090 66 69 67 2e 73 7a 50 61 67 65 20 3d 20 30 3b 0a  fig.szPage = 0;.
9b0a0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c    }.  return sql
9b0b0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9b0c0 2e 6d 2e 78 49 6e 69 74 28 73 71 6c 69 74 65 33  .m.xInit(sqlite3
9b0d0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70  GlobalConfig.m.p
9b0e0 41 70 70 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a  AppData);.}../*.
9b0f0 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20  ** Deinitialize 
9b100 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
9b110 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e  ation subsystem.
9b120 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
9b130 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  TE void sqlite3M
9b140 61 6c 6c 6f 63 45 6e 64 28 76 6f 69 64 29 7b 0a  allocEnd(void){.
9b150 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
9b160 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 68 75  balConfig.m.xShu
9b170 74 64 6f 77 6e 20 29 7b 0a 20 20 20 20 73 71 6c  tdown ){.    sql
9b180 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9b190 2e 6d 2e 78 53 68 75 74 64 6f 77 6e 28 73 71 6c  .m.xShutdown(sql
9b1a0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9b1b0 2e 6d 2e 70 41 70 70 44 61 74 61 29 3b 0a 20 20  .m.pAppData);.  
9b1c0 7d 0a 20 20 6d 65 6d 73 65 74 28 26 6d 65 6d 30  }.  memset(&mem0
9b1d0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 65 6d 30  , 0, sizeof(mem0
9b1e0 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ));.}../*.** Ret
9b1f0 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f  urn the amount o
9b200 66 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e 74  f memory current
9b210 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a  ly checked out..
9b220 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
9b230 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69  lite3_int64 sqli
9b240 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28  te3_memory_used(
9b250 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20 6e 2c 20  void){.  int n, 
9b260 6d 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  mx;.  sqlite3_in
9b270 74 36 34 20 72 65 73 3b 0a 20 20 73 71 6c 69 74  t64 res;.  sqlit
9b280 65 33 5f 73 74 61 74 75 73 28 53 51 4c 49 54 45  e3_status(SQLITE
9b290 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55  _STATUS_MEMORY_U
9b2a0 53 45 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 30 29  SED, &n, &mx, 0)
9b2b0 3b 0a 20 20 72 65 73 20 3d 20 28 73 71 6c 69 74  ;.  res = (sqlit
9b2c0 65 33 5f 69 6e 74 36 34 29 6e 3b 20 20 2f 2a 20  e3_int64)n;  /* 
9b2d0 57 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 20  Work around bug 
9b2e0 69 6e 20 42 6f 72 6c 61 6e 64 20 43 2e 20 54 69  in Borland C. Ti
9b2f0 63 6b 65 74 20 23 33 32 31 36 20 2a 2f 0a 20 20  cket #3216 */.  
9b300 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f  return res;.}../
9b310 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
9b320 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f  maximum amount o
9b330 66 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 68 61  f memory that ha
9b340 73 20 65 76 65 72 20 62 65 65 6e 0a 2a 2a 20 63  s ever been.** c
9b350 68 65 63 6b 65 64 20 6f 75 74 20 73 69 6e 63 65  hecked out since
9b360 20 65 69 74 68 65 72 20 74 68 65 20 62 65 67 69   either the begi
9b370 6e 6e 69 6e 67 20 6f 66 20 74 68 69 73 20 70 72  nning of this pr
9b380 6f 63 65 73 73 0a 2a 2a 20 6f 72 20 73 69 6e 63  ocess.** or sinc
9b390 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  e the most recen
9b3a0 74 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49  t reset..*/.SQLI
9b3b0 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69  TE_API sqlite3_i
9b3c0 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d  nt64 sqlite3_mem
9b3d0 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 69 6e  ory_highwater(in
9b3e0 74 20 72 65 73 65 74 46 6c 61 67 29 7b 0a 20 20  t resetFlag){.  
9b3f0 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 71 6c  int n, mx;.  sql
9b400 69 74 65 33 5f 69 6e 74 36 34 20 72 65 73 3b 0a  ite3_int64 res;.
9b410 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73    sqlite3_status
9b420 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d  (SQLITE_STATUS_M
9b430 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c 20  EMORY_USED, &n, 
9b440 26 6d 78 2c 20 72 65 73 65 74 46 6c 61 67 29 3b  &mx, resetFlag);
9b450 0a 20 20 72 65 73 20 3d 20 28 73 71 6c 69 74 65  .  res = (sqlite
9b460 33 5f 69 6e 74 36 34 29 6d 78 3b 20 20 2f 2a 20  3_int64)mx;  /* 
9b470 57 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 20  Work around bug 
9b480 69 6e 20 42 6f 72 6c 61 6e 64 20 43 2e 20 54 69  in Borland C. Ti
9b490 63 6b 65 74 20 23 33 32 31 36 20 2a 2f 0a 20 20  cket #3216 */.  
9b4a0 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f  return res;.}../
9b4b0 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
9b4c0 61 6c 61 72 6d 20 63 61 6c 6c 62 61 63 6b 0a 2a  alarm callback.*
9b4d0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
9b4e0 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 6f   int sqlite3Memo
9b4f0 72 79 41 6c 61 72 6d 28 0a 20 20 76 6f 69 64 28  ryAlarm(.  void(
9b500 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64  *xCallback)(void
9b510 20 2a 70 41 72 67 2c 20 73 71 6c 69 74 65 33 5f   *pArg, sqlite3_
9b520 69 6e 74 36 34 20 75 73 65 64 2c 69 6e 74 20 4e  int64 used,int N
9b530 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 2c  ),.  void *pArg,
9b540 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
9b550 20 69 54 68 72 65 73 68 6f 6c 64 0a 29 7b 0a 20   iThreshold.){. 
9b560 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
9b570 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29  nter(mem0.mutex)
9b580 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61  ;.  mem0.alarmCa
9b590 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61  llback = xCallba
9b5a0 63 6b 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d  ck;.  mem0.alarm
9b5b0 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 6d 65  Arg = pArg;.  me
9b5c0 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c  m0.alarmThreshol
9b5d0 64 20 3d 20 69 54 68 72 65 73 68 6f 6c 64 3b 0a  d = iThreshold;.
9b5e0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
9b5f0 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78  leave(mem0.mutex
9b600 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
9b610 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65  TE_OK;.}..#ifnde
9b620 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
9b630 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 44  PRECATED./*.** D
9b640 65 70 72 65 63 61 74 65 64 20 65 78 74 65 72 6e  eprecated extern
9b650 61 6c 20 69 6e 74 65 72 66 61 63 65 2e 20 20 49  al interface.  I
9b660 6e 74 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51 4c  nternal/core SQL
9b670 69 74 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f 75  ite code.** shou
9b680 6c 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4d  ld call sqlite3M
9b690 65 6d 6f 72 79 41 6c 61 72 6d 2e 0a 2a 2f 0a 53  emoryAlarm..*/.S
9b6a0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
9b6b0 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61  lite3_memory_ala
9b6c0 72 6d 28 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c  rm(.  void(*xCal
9b6d0 6c 62 61 63 6b 29 28 76 6f 69 64 20 2a 70 41 72  lback)(void *pAr
9b6e0 67 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  g, sqlite3_int64
9b6f0 20 75 73 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20   used,int N),.  
9b700 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20 73 71  void *pArg,.  sq
9b710 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 54 68 72  lite3_int64 iThr
9b720 65 73 68 6f 6c 64 0a 29 7b 0a 20 20 72 65 74 75  eshold.){.  retu
9b730 72 6e 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79  rn sqlite3Memory
9b740 41 6c 61 72 6d 28 78 43 61 6c 6c 62 61 63 6b 2c  Alarm(xCallback,
9b750 20 70 41 72 67 2c 20 69 54 68 72 65 73 68 6f 6c   pArg, iThreshol
9b760 64 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  d);.}.#endif../*
9b770 0a 2a 2a 20 54 72 69 67 67 65 72 20 74 68 65 20  .** Trigger the 
9b780 61 6c 61 72 6d 20 0a 2a 2f 0a 73 74 61 74 69 63  alarm .*/.static
9b790 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 6c   void sqlite3Mal
9b7a0 6c 6f 63 41 6c 61 72 6d 28 69 6e 74 20 6e 42 79  locAlarm(int nBy
9b7b0 74 65 29 7b 0a 20 20 76 6f 69 64 20 28 2a 78 43  te){.  void (*xC
9b7c0 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 73  allback)(void*,s
9b7d0 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74  qlite3_int64,int
9b7e0 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  );.  sqlite3_int
9b7f0 36 34 20 6e 6f 77 55 73 65 64 3b 0a 20 20 76 6f  64 nowUsed;.  vo
9b800 69 64 20 2a 70 41 72 67 3b 0a 20 20 69 66 28 20  id *pArg;.  if( 
9b810 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61  mem0.alarmCallba
9b820 63 6b 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  ck==0 ) return;.
9b830 20 20 78 43 61 6c 6c 62 61 63 6b 20 3d 20 6d 65    xCallback = me
9b840 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b  m0.alarmCallback
9b850 3b 0a 20 20 6e 6f 77 55 73 65 64 20 3d 20 73 71  ;.  nowUsed = sq
9b860 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65  lite3StatusValue
9b870 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d  (SQLITE_STATUS_M
9b880 45 4d 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20 70  EMORY_USED);.  p
9b890 41 72 67 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d  Arg = mem0.alarm
9b8a0 41 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72  Arg;.  mem0.alar
9b8b0 6d 43 61 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20  mCallback = 0;. 
9b8c0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
9b8d0 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29  eave(mem0.mutex)
9b8e0 3b 0a 20 20 78 43 61 6c 6c 62 61 63 6b 28 70 41  ;.  xCallback(pA
9b8f0 72 67 2c 20 6e 6f 77 55 73 65 64 2c 20 6e 42 79  rg, nowUsed, nBy
9b900 74 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  te);.  sqlite3_m
9b910 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e  utex_enter(mem0.
9b920 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d 30 2e 61  mutex);.  mem0.a
9b930 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d 20 78  larmCallback = x
9b940 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65 6d 30  Callback;.  mem0
9b950 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70 41 72 67  .alarmArg = pArg
9b960 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20  ;.}../*.** Do a 
9b970 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
9b980 6e 20 77 69 74 68 20 73 74 61 74 69 73 74 69 63  n with statistic
9b990 73 20 61 6e 64 20 61 6c 61 72 6d 73 2e 20 20 41  s and alarms.  A
9b9a0 73 73 75 6d 65 20 74 68 65 0a 2a 2a 20 6c 6f 63  ssume the.** loc
9b9b0 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c  k is already hel
9b9c0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
9b9d0 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d   mallocWithAlarm
9b9e0 28 69 6e 74 20 6e 2c 20 76 6f 69 64 20 2a 2a 70  (int n, void **p
9b9f0 70 29 7b 0a 20 20 69 6e 74 20 6e 46 75 6c 6c 3b  p){.  int nFull;
9ba00 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73  .  void *p;.  as
9ba10 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
9ba20 74 65 78 5f 68 65 6c 64 28 6d 65 6d 30 2e 6d 75  tex_held(mem0.mu
9ba30 74 65 78 29 20 29 3b 0a 20 20 6e 46 75 6c 6c 20  tex) );.  nFull 
9ba40 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
9ba50 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 70  onfig.m.xRoundup
9ba60 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 74  (n);.  sqlite3St
9ba70 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53  atusSet(SQLITE_S
9ba80 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a  TATUS_MALLOC_SIZ
9ba90 45 2c 20 6e 29 3b 0a 20 20 69 66 28 20 6d 65 6d  E, n);.  if( mem
9baa0 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 21  0.alarmCallback!
9bab0 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 55  =0 ){.    int nU
9bac0 73 65 64 20 3d 20 73 71 6c 69 74 65 33 53 74 61  sed = sqlite3Sta
9bad0 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54 45 5f  tusValue(SQLITE_
9bae0 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53  STATUS_MEMORY_US
9baf0 45 44 29 3b 0a 20 20 20 20 69 66 28 20 6e 55 73  ED);.    if( nUs
9bb00 65 64 2b 6e 46 75 6c 6c 20 3e 3d 20 6d 65 6d 30  ed+nFull >= mem0
9bb10 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20  .alarmThreshold 
9bb20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
9bb30 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 75 6c  MallocAlarm(nFul
9bb40 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  l);.    }.  }.  
9bb50 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  p = sqlite3Globa
9bb60 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f  lConfig.m.xMallo
9bb70 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 69 66 28 20  c(nFull);.  if( 
9bb80 70 3d 3d 30 20 26 26 20 6d 65 6d 30 2e 61 6c 61  p==0 && mem0.ala
9bb90 72 6d 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20  rmCallback ){.  
9bba0 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41    sqlite3MallocA
9bbb0 6c 61 72 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20 20  larm(nFull);.   
9bbc0 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62   p = sqlite3Glob
9bbd0 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c  alConfig.m.xMall
9bbe0 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20  oc(nFull);.  }. 
9bbf0 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6e 46   if( p ){.    nF
9bc00 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  ull = sqlite3Mal
9bc10 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20  locSize(p);.    
9bc20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64  sqlite3StatusAdd
9bc30 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d  (SQLITE_STATUS_M
9bc40 45 4d 4f 52 59 5f 55 53 45 44 2c 20 6e 46 75 6c  EMORY_USED, nFul
9bc50 6c 29 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d 20  l);.  }.  *pp = 
9bc60 70 3b 0a 20 20 72 65 74 75 72 6e 20 6e 46 75 6c  p;.  return nFul
9bc70 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  l;.}../*.** Allo
9bc80 63 61 74 65 20 6d 65 6d 6f 72 79 2e 20 20 54 68  cate memory.  Th
9bc90 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6c 69  is routine is li
9bca0 6b 65 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  ke sqlite3_mallo
9bcb0 63 28 29 20 65 78 63 65 70 74 20 74 68 61 74 20  c() except that 
9bcc0 69 74 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68  it.** assumes th
9bcd0 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74  e memory subsyst
9bce0 65 6d 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  em has already b
9bcf0 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e  een initialized.
9bd00 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
9bd10 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
9bd20 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20  Malloc(int n){. 
9bd30 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69 66 28 20   void *p;.  if( 
9bd40 6e 3c 3d 30 20 7c 7c 20 6e 3e 3d 30 78 37 66 66  n<=0 || n>=0x7ff
9bd50 66 66 66 30 30 20 29 7b 0a 20 20 20 20 2f 2a 20  fff00 ){.    /* 
9bd60 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  A memory allocat
9bd70 69 6f 6e 20 6f 66 20 61 20 6e 75 6d 62 65 72 20  ion of a number 
9bd80 6f 66 20 62 79 74 65 73 20 77 68 69 63 68 20 69  of bytes which i
9bd90 73 20 6e 65 61 72 20 74 68 65 20 6d 61 78 69 6d  s near the maxim
9bda0 75 6d 0a 20 20 20 20 2a 2a 20 73 69 67 6e 65 64  um.    ** signed
9bdb0 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6d   integer value m
9bdc0 69 67 68 74 20 63 61 75 73 65 20 61 6e 20 69 6e  ight cause an in
9bdd0 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 20 69  teger overflow i
9bde0 6e 73 69 64 65 20 6f 66 20 74 68 65 0a 20 20 20  nside of the.   
9bdf0 20 2a 2a 20 78 4d 61 6c 6c 6f 63 28 29 2e 20 20   ** xMalloc().  
9be00 48 65 6e 63 65 20 77 65 20 6c 69 6d 69 74 20 74  Hence we limit t
9be10 68 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20  he maximum size 
9be20 74 6f 20 30 78 37 66 66 66 66 66 30 30 2c 20 67  to 0x7fffff00, g
9be30 69 76 69 6e 67 0a 20 20 20 20 2a 2a 20 32 35 35  iving.    ** 255
9be40 20 62 79 74 65 73 20 6f 66 20 6f 76 65 72 68 65   bytes of overhe
9be50 61 64 2e 20 20 53 51 4c 69 74 65 20 69 74 73 65  ad.  SQLite itse
9be60 6c 66 20 77 69 6c 6c 20 6e 65 76 65 72 20 75 73  lf will never us
9be70 65 20 61 6e 79 74 68 69 6e 67 20 6e 65 61 72 0a  e anything near.
9be80 20 20 20 20 2a 2a 20 74 68 69 73 20 61 6d 6f 75      ** this amou
9be90 6e 74 2e 20 20 54 68 65 20 6f 6e 6c 79 20 77 61  nt.  The only wa
9bea0 79 20 74 6f 20 72 65 61 63 68 20 74 68 65 20 6c  y to reach the l
9beb0 69 6d 69 74 20 69 73 20 77 69 74 68 20 73 71 6c  imit is with sql
9bec0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f  ite3_malloc() */
9bed0 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d 65  .    p = 0;.  }e
9bee0 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 47  lse if( sqlite3G
9bef0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d  lobalConfig.bMem
9bf00 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  stat ){.    sqli
9bf10 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
9bf20 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20  mem0.mutex);.   
9bf30 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d   mallocWithAlarm
9bf40 28 6e 2c 20 26 70 29 3b 0a 20 20 20 20 73 71 6c  (n, &p);.    sql
9bf50 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
9bf60 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
9bf70 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 73  }else{.    p = s
9bf80 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9bf90 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 29 3b  ig.m.xMalloc(n);
9bfa0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
9bfb0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76  .}../*.** This v
9bfc0 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65  ersion of the me
9bfd0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
9bfe0 69 73 20 66 6f 72 20 75 73 65 20 62 79 20 74 68  is for use by th
9bff0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 0a 2a  e application..*
9c000 2a 20 46 69 72 73 74 20 6d 61 6b 65 20 73 75 72  * First make sur
9c010 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 62  e the memory sub
9c020 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 69 61  system is initia
9c030 6c 69 7a 65 64 2c 20 74 68 65 6e 20 64 6f 20 74  lized, then do t
9c040 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e  he.** allocation
9c050 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
9c060 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 6d 61  void *sqlite3_ma
9c070 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 23 69 66  lloc(int n){.#if
9c080 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
9c090 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20  _AUTOINIT.  if( 
9c0a0 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
9c0b0 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b  ze() ) return 0;
9c0c0 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
9c0d0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e   sqlite3Malloc(n
9c0e0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68  );.}../*.** Each
9c0f0 20 74 68 72 65 61 64 20 6d 61 79 20 6f 6e 6c 79   thread may only
9c100 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 20 6f   have a single o
9c110 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63  utstanding alloc
9c120 61 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 78 53  ation from.** xS
9c130 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 29 2e 20  cratchMalloc(). 
9c140 20 57 65 20 76 65 72 69 66 79 20 74 68 69 73 20   We verify this 
9c150 63 6f 6e 73 74 72 61 69 6e 74 20 69 6e 20 74 68  constraint in th
9c160 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65  e single-threade
9c170 64 0a 2a 2a 20 63 61 73 65 20 62 79 20 73 65 74  d.** case by set
9c180 74 69 6e 67 20 73 63 72 61 74 63 68 41 6c 6c 6f  ting scratchAllo
9c190 63 4f 75 74 20 74 6f 20 31 20 77 68 65 6e 20 61  cOut to 1 when a
9c1a0 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20  n allocation.** 
9c1b0 69 73 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 63  is outstanding c
9c1c0 6c 65 61 72 69 6e 67 20 69 74 20 77 68 65 6e 20  learing it when 
9c1d0 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  the allocation i
9c1e0 73 20 66 72 65 65 64 2e 0a 2a 2f 0a 23 69 66 20  s freed..*/.#if 
9c1f0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
9c200 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64  E==0 && !defined
9c210 28 4e 44 45 42 55 47 29 0a 73 74 61 74 69 63 20  (NDEBUG).static 
9c220 69 6e 74 20 73 63 72 61 74 63 68 41 6c 6c 6f 63  int scratchAlloc
9c230 4f 75 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  Out = 0;.#endif.
9c240 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
9c250 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 69 73 20   memory that is 
9c260 74 6f 20 62 65 20 75 73 65 64 20 61 6e 64 20 72  to be used and r
9c270 65 6c 65 61 73 65 64 20 72 69 67 68 74 20 61 77  eleased right aw
9c280 61 79 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ay..** This rout
9c290 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74  ine is similar t
9c2a0 6f 20 61 6c 6c 6f 63 61 28 29 20 69 6e 20 74 68  o alloca() in th
9c2b0 61 74 20 69 74 20 69 73 20 6e 6f 74 20 69 6e 74  at it is not int
9c2c0 65 6e 64 65 64 0a 2a 2a 20 66 6f 72 20 73 69 74  ended.** for sit
9c2d0 75 61 74 69 6f 6e 73 20 77 68 65 72 65 20 74 68  uations where th
9c2e0 65 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 62  e memory might b
9c2f0 65 20 68 65 6c 64 20 6c 6f 6e 67 2d 74 65 72 6d  e held long-term
9c300 2e 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69  .  This.** routi
9c310 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74  ne is intended t
9c320 6f 20 67 65 74 20 6d 65 6d 6f 72 79 20 74 6f 20  o get memory to 
9c330 6f 6c 64 20 6c 61 72 67 65 20 74 72 61 6e 73 69  old large transi
9c340 65 6e 74 20 64 61 74 61 0a 2a 2a 20 73 74 72 75  ent data.** stru
9c350 63 74 75 72 65 73 20 74 68 61 74 20 77 6f 75 6c  ctures that woul
9c360 64 20 6e 6f 74 20 6e 6f 72 6d 61 6c 6c 79 20 66  d not normally f
9c370 69 74 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20  it on the stack 
9c380 6f 66 20 61 6e 0a 2a 2a 20 65 6d 62 65 64 64 65  of an.** embedde
9c390 64 20 70 72 6f 63 65 73 73 6f 72 2e 0a 2a 2f 0a  d processor..*/.
9c3a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
9c3b0 6f 69 64 20 2a 73 71 6c 69 74 65 33 53 63 72 61  oid *sqlite3Scra
9c3c0 74 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29  tchMalloc(int n)
9c3d0 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61  {.  void *p;.  a
9c3e0 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 0a 23  ssert( n>0 );..#
9c3f0 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
9c400 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66 69  SAFE==0 && !defi
9c410 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 20 2f 2a  ned(NDEBUG).  /*
9c420 20 56 65 72 69 66 79 20 74 68 61 74 20 6e 6f 20   Verify that no 
9c430 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 73 63  more than one sc
9c440 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e  ratch allocation
9c450 20 70 65 72 20 74 68 72 65 61 64 0a 20 20 2a 2a   per thread.  **
9c460 20 69 73 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   is outstanding 
9c470 61 74 20 6f 6e 65 20 74 69 6d 65 2e 20 20 28 54  at one time.  (T
9c480 68 69 73 20 69 73 20 6f 6e 6c 79 20 63 68 65 63  his is only chec
9c490 6b 65 64 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20  ked in the.  ** 
9c4a0 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20  single-threaded 
9c4b0 63 61 73 65 20 73 69 6e 63 65 20 63 68 65 63 6b  case since check
9c4c0 69 6e 67 20 69 6e 20 74 68 65 20 6d 75 6c 74 69  ing in the multi
9c4d0 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 0a 20  -threaded case. 
9c4e0 20 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d 75 63   ** would be muc
9c4f0 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74  h more complicat
9c500 65 64 2e 29 20 2a 2f 0a 20 20 61 73 73 65 72 74  ed.) */.  assert
9c510 28 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75  ( scratchAllocOu
9c520 74 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a 0a  t==0 );.#endif..
9c530 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
9c540 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61  balConfig.szScra
9c550 74 63 68 3c 6e 20 29 7b 0a 20 20 20 20 67 6f 74  tch<n ){.    got
9c560 6f 20 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c  o scratch_overfl
9c570 6f 77 3b 0a 20 20 7d 65 6c 73 65 7b 20 20 0a 20  ow;.  }else{  . 
9c580 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
9c590 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65  _enter(mem0.mute
9c5a0 78 29 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 30  x);.    if( mem0
9c5b0 2e 6e 53 63 72 61 74 63 68 46 72 65 65 3d 3d 30  .nScratchFree==0
9c5c0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
9c5d0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
9c5e0 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  m0.mutex);.     
9c5f0 20 67 6f 74 6f 20 73 63 72 61 74 63 68 5f 6f 76   goto scratch_ov
9c600 65 72 66 6c 6f 77 3b 0a 20 20 20 20 7d 65 6c 73  erflow;.    }els
9c610 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  e{.      int i;.
9c620 20 20 20 20 20 20 69 20 3d 20 6d 65 6d 30 2e 61        i = mem0.a
9c630 53 63 72 61 74 63 68 46 72 65 65 5b 2d 2d 6d 65  ScratchFree[--me
9c640 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 5d  m0.nScratchFree]
9c650 3b 0a 20 20 20 20 20 20 69 20 2a 3d 20 73 71 6c  ;.      i *= sql
9c660 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9c670 2e 73 7a 53 63 72 61 74 63 68 3b 0a 20 20 20 20  .szScratch;.    
9c680 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41    sqlite3StatusA
9c690 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dd(SQLITE_STATUS
9c6a0 5f 53 43 52 41 54 43 48 5f 55 53 45 44 2c 20 31  _SCRATCH_USED, 1
9c6b0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
9c6c0 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45  StatusSet(SQLITE
9c6d0 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f  _STATUS_SCRATCH_
9c6e0 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 20 20  SIZE, n);.      
9c6f0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
9c700 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b  ave(mem0.mutex);
9c710 0a 20 20 20 20 20 20 70 20 3d 20 28 76 6f 69 64  .      p = (void
9c720 2a 29 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74  *)&((char*)sqlit
9c730 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
9c740 53 63 72 61 74 63 68 29 5b 69 5d 3b 0a 20 20 20  Scratch)[i];.   
9c750 20 20 20 61 73 73 65 72 74 28 20 20 28 28 28 75     assert(  (((u
9c760 38 2a 29 70 20 2d 20 28 75 38 2a 29 30 29 20 26  8*)p - (u8*)0) &
9c770 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 0a   7)==0 );.    }.
9c780 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 54    }.#if SQLITE_T
9c790 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20  HREADSAFE==0 && 
9c7a0 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29  !defined(NDEBUG)
9c7b0 0a 20 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f  .  scratchAllocO
9c7c0 75 74 20 3d 20 70 21 3d 30 3b 0a 23 65 6e 64 69  ut = p!=0;.#endi
9c7d0 66 0a 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 0a  f..  return p;..
9c7e0 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77  scratch_overflow
9c7f0 3a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  :.  if( sqlite3G
9c800 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d  lobalConfig.bMem
9c810 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  stat ){.    sqli
9c820 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
9c830 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20  mem0.mutex);.   
9c840 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65   sqlite3StatusSe
9c850 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  t(SQLITE_STATUS_
9c860 53 43 52 41 54 43 48 5f 53 49 5a 45 2c 20 6e 29  SCRATCH_SIZE, n)
9c870 3b 0a 20 20 20 20 6e 20 3d 20 6d 61 6c 6c 6f 63  ;.    n = malloc
9c880 57 69 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29  WithAlarm(n, &p)
9c890 3b 0a 20 20 20 20 69 66 28 20 70 20 29 20 73 71  ;.    if( p ) sq
9c8a0 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53  lite3StatusAdd(S
9c8b0 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52  QLITE_STATUS_SCR
9c8c0 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57 2c 20 6e  ATCH_OVERFLOW, n
9c8d0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
9c8e0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e  utex_leave(mem0.
9c8f0 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b  mutex);.  }else{
9c900 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
9c910 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
9c920 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 23  Malloc(n);.  }.#
9c930 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
9c940 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66 69  SAFE==0 && !defi
9c950 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 20 73 63  ned(NDEBUG).  sc
9c960 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20  ratchAllocOut = 
9c970 70 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72  p!=0;.#endif.  r
9c980 65 74 75 72 6e 20 70 3b 20 20 20 20 0a 7d 0a 53  eturn p;    .}.S
9c990 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
9c9a0 69 64 20 73 71 6c 69 74 65 33 53 63 72 61 74 63  id sqlite3Scratc
9c9b0 68 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a  hFree(void *p){.
9c9c0 20 20 69 66 28 20 70 20 29 7b 0a 0a 23 69 66 20    if( p ){..#if 
9c9d0 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
9c9e0 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64  E==0 && !defined
9c9f0 28 4e 44 45 42 55 47 29 0a 20 20 20 20 2f 2a 20  (NDEBUG).    /* 
9ca00 56 65 72 69 66 79 20 74 68 61 74 20 6e 6f 20 6d  Verify that no m
9ca10 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 73 63 72  ore than one scr
9ca20 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  atch allocation 
9ca30 70 65 72 20 74 68 72 65 61 64 0a 20 20 20 20 2a  per thread.    *
9ca40 2a 20 69 73 20 6f 75 74 73 74 61 6e 64 69 6e 67  * is outstanding
9ca50 20 61 74 20 6f 6e 65 20 74 69 6d 65 2e 20 20 28   at one time.  (
9ca60 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 63 68 65  This is only che
9ca70 63 6b 65 64 20 69 6e 20 74 68 65 0a 20 20 20 20  cked in the.    
9ca80 2a 2a 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64  ** single-thread
9ca90 65 64 20 63 61 73 65 20 73 69 6e 63 65 20 63 68  ed case since ch
9caa0 65 63 6b 69 6e 67 20 69 6e 20 74 68 65 20 6d 75  ecking in the mu
9cab0 6c 74 69 2d 74 68 72 65 61 64 65 64 20 63 61 73  lti-threaded cas
9cac0 65 0a 20 20 20 20 2a 2a 20 77 6f 75 6c 64 20 62  e.    ** would b
9cad0 65 20 6d 75 63 68 20 6d 6f 72 65 20 63 6f 6d 70  e much more comp
9cae0 6c 69 63 61 74 65 64 2e 29 20 2a 2f 0a 20 20 20  licated.) */.   
9caf0 20 61 73 73 65 72 74 28 20 73 63 72 61 74 63 68   assert( scratch
9cb00 41 6c 6c 6f 63 4f 75 74 3d 3d 31 20 29 3b 0a 20  AllocOut==1 );. 
9cb10 20 20 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f     scratchAllocO
9cb20 75 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  ut = 0;.#endif..
9cb30 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47      if( sqlite3G
9cb40 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72  lobalConfig.pScr
9cb50 61 74 63 68 3d 3d 30 0a 20 20 20 20 20 20 20 20  atch==0.        
9cb60 20 20 20 7c 7c 20 70 3c 73 71 6c 69 74 65 33 47     || p<sqlite3G
9cb70 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72  lobalConfig.pScr
9cb80 61 74 63 68 0a 20 20 20 20 20 20 20 20 20 20 20  atch.           
9cb90 7c 7c 20 70 3e 3d 28 76 6f 69 64 2a 29 6d 65 6d  || p>=(void*)mem
9cba0 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 20 29  0.aScratchFree )
9cbb0 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
9cbc0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9cbd0 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20  bMemstat ){.    
9cbe0 20 20 20 20 69 6e 74 20 69 53 69 7a 65 20 3d 20      int iSize = 
9cbf0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a  sqlite3MallocSiz
9cc00 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71  e(p);.        sq
9cc10 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
9cc20 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20  r(mem0.mutex);. 
9cc30 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
9cc40 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53  atusAdd(SQLITE_S
9cc50 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56  TATUS_SCRATCH_OV
9cc60 45 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 3b  ERFLOW, -iSize);
9cc70 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
9cc80 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45  StatusAdd(SQLITE
9cc90 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55  _STATUS_MEMORY_U
9cca0 53 45 44 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20  SED, -iSize);.  
9ccb0 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
9ccc0 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65  balConfig.m.xFre
9ccd0 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71  e(p);.        sq
9cce0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
9ccf0 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20  e(mem0.mutex);. 
9cd00 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
9cd10 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
9cd20 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28  lConfig.m.xFree(
9cd30 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  p);.      }.    
9cd40 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74  }else{.      int
9cd50 20 69 3b 0a 20 20 20 20 20 20 69 20 3d 20 28 69   i;.      i = (i
9cd60 6e 74 29 28 28 75 38 2a 29 70 20 2d 20 28 75 38  nt)((u8*)p - (u8
9cd70 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  *)sqlite3GlobalC
9cd80 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 29 3b  onfig.pScratch);
9cd90 0a 20 20 20 20 20 20 69 20 2f 3d 20 73 71 6c 69  .      i /= sqli
9cda0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9cdb0 73 7a 53 63 72 61 74 63 68 3b 0a 20 20 20 20 20  szScratch;.     
9cdc0 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26   assert( i>=0 &&
9cdd0 20 69 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   i<sqlite3Global
9cde0 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 20  Config.nScratch 
9cdf0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
9ce00 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d  _mutex_enter(mem
9ce10 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  0.mutex);.      
9ce20 61 73 73 65 72 74 28 20 6d 65 6d 30 2e 6e 53 63  assert( mem0.nSc
9ce30 72 61 74 63 68 46 72 65 65 3c 28 75 33 32 29 73  ratchFree<(u32)s
9ce40 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9ce50 69 67 2e 6e 53 63 72 61 74 63 68 20 29 3b 0a 20  ig.nScratch );. 
9ce60 20 20 20 20 20 6d 65 6d 30 2e 61 53 63 72 61 74       mem0.aScrat
9ce70 63 68 46 72 65 65 5b 6d 65 6d 30 2e 6e 53 63 72  chFree[mem0.nScr
9ce80 61 74 63 68 46 72 65 65 2b 2b 5d 20 3d 20 69 3b  atchFree++] = i;
9ce90 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74  .      sqlite3St
9cea0 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53  atusAdd(SQLITE_S
9ceb0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53  TATUS_SCRATCH_US
9cec0 45 44 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 73  ED, -1);.      s
9ced0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
9cee0 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a  ve(mem0.mutex);.
9cef0 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
9cf00 2a 2a 20 54 52 55 45 20 69 66 20 70 20 69 73 20  ** TRUE if p is 
9cf10 61 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f  a lookaside memo
9cf20 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 72  ry allocation fr
9cf30 6f 6d 20 64 62 0a 2a 2f 0a 23 69 66 6e 64 65 66  om db.*/.#ifndef
9cf40 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 4f   SQLITE_OMIT_LOO
9cf50 4b 41 53 49 44 45 0a 73 74 61 74 69 63 20 69 6e  KASIDE.static in
9cf60 74 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 73 71  t isLookaside(sq
9cf70 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20  lite3 *db, void 
9cf80 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62  *p){.  return db
9cf90 20 26 26 20 70 20 26 26 20 70 3e 3d 64 62 2d 3e   && p && p>=db->
9cfa0 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61 72 74  lookaside.pStart
9cfb0 20 26 26 20 70 3c 64 62 2d 3e 6c 6f 6f 6b 61 73   && p<db->lookas
9cfc0 69 64 65 2e 70 45 6e 64 3b 0a 7d 0a 23 65 6c 73  ide.pEnd;.}.#els
9cfd0 65 0a 23 64 65 66 69 6e 65 20 69 73 4c 6f 6f 6b  e.#define isLook
9cfe0 61 73 69 64 65 28 41 2c 42 29 20 30 0a 23 65 6e  aside(A,B) 0.#en
9cff0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  dif../*.** Retur
9d000 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20  n the size of a 
9d010 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
9d020 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74  n previously obt
9d030 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71  ained from.** sq
9d040 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 20 6f 72  lite3Malloc() or
9d050 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
9d060 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
9d070 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
9d080 4d 61 6c 6c 6f 63 53 69 7a 65 28 76 6f 69 64 20  MallocSize(void 
9d090 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  *p){.  return sq
9d0a0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9d0b0 67 2e 6d 2e 78 53 69 7a 65 28 70 29 3b 0a 7d 0a  g.m.xSize(p);.}.
9d0c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
9d0d0 6e 74 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  nt sqlite3DbMall
9d0e0 6f 63 53 69 7a 65 28 73 71 6c 69 74 65 33 20 2a  ocSize(sqlite3 *
9d0f0 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20  db, void *p){.  
9d100 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c  assert( db==0 ||
9d110 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
9d120 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
9d130 3b 0a 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73  ;.  if( isLookas
9d140 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20  ide(db, p) ){.  
9d150 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 6c 6f 6f    return db->loo
9d160 6b 61 73 69 64 65 2e 73 7a 3b 0a 20 20 7d 65 6c  kaside.sz;.  }el
9d170 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73  se{.    return s
9d180 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9d190 69 67 2e 6d 2e 78 53 69 7a 65 28 70 29 3b 0a 20  ig.m.xSize(p);. 
9d1a0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65   }.}../*.** Free
9d1b0 20 6d 65 6d 6f 72 79 20 70 72 65 76 69 6f 75 73   memory previous
9d1c0 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ly obtained from
9d1d0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29   sqlite3Malloc()
9d1e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
9d1f0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65  void sqlite3_fre
9d200 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66  e(void *p){.  if
9d210 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( p==0 ) return;
9d220 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
9d230 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73  obalConfig.bMems
9d240 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  tat ){.    sqlit
9d250 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
9d260 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  em0.mutex);.    
9d270 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64  sqlite3StatusAdd
9d280 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d  (SQLITE_STATUS_M
9d290 45 4d 4f 52 59 5f 55 53 45 44 2c 20 2d 73 71 6c  EMORY_USED, -sql
9d2a0 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70  ite3MallocSize(p
9d2b0 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47  ));.    sqlite3G
9d2c0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46  lobalConfig.m.xF
9d2d0 72 65 65 28 70 29 3b 0a 20 20 20 20 73 71 6c 69  ree(p);.    sqli
9d2e0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
9d2f0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d  mem0.mutex);.  }
9d300 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
9d310 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
9d320 78 46 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a  xFree(p);.  }.}.
9d330 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f  ./*.** Free memo
9d340 72 79 20 74 68 61 74 20 6d 69 67 68 74 20 62 65  ry that might be
9d350 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
9d360 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61   a particular da
9d370 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63  tabase.** connec
9d380 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
9d390 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
9d3a0 69 74 65 33 44 62 46 72 65 65 28 73 71 6c 69 74  ite3DbFree(sqlit
9d3b0 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29  e3 *db, void *p)
9d3c0 7b 0a 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d  {.  assert( db==
9d3d0 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
9d3e0 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
9d3f0 78 29 20 29 3b 0a 20 20 69 66 28 20 69 73 4c 6f  x) );.  if( isLo
9d400 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29 20 29  okaside(db, p) )
9d410 7b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 64 65 53  {.    LookasideS
9d420 6c 6f 74 20 2a 70 42 75 66 20 3d 20 28 4c 6f 6f  lot *pBuf = (Loo
9d430 6b 61 73 69 64 65 53 6c 6f 74 2a 29 70 3b 0a 20  kasideSlot*)p;. 
9d440 20 20 20 70 42 75 66 2d 3e 70 4e 65 78 74 20 3d     pBuf->pNext =
9d450 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70   db->lookaside.p
9d460 46 72 65 65 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f  Free;.    db->lo
9d470 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20  okaside.pFree = 
9d480 70 42 75 66 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f  pBuf;.    db->lo
9d490 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 2d 2d 3b 0a  okaside.nOut--;.
9d4a0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
9d4b0 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20  ite3_free(p);.  
9d4c0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  }.}../*.** Chang
9d4d0 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e  e the size of an
9d4e0 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79   existing memory
9d4f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 53   allocation.*/.S
9d500 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
9d510 69 64 20 2a 73 71 6c 69 74 65 33 52 65 61 6c 6c  id *sqlite3Reall
9d520 6f 63 28 76 6f 69 64 20 2a 70 4f 6c 64 2c 20 69  oc(void *pOld, i
9d530 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e  nt nBytes){.  in
9d540 74 20 6e 4f 6c 64 2c 20 6e 4e 65 77 3b 0a 20 20  t nOld, nNew;.  
9d550 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 69 66  void *pNew;.  if
9d560 28 20 70 4f 6c 64 3d 3d 30 20 29 7b 0a 20 20 20  ( pOld==0 ){.   
9d570 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4d   return sqlite3M
9d580 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20  alloc(nBytes);. 
9d590 20 7d 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c   }.  if( nBytes<
9d5a0 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
9d5b0 33 5f 66 72 65 65 28 70 4f 6c 64 29 3b 0a 20 20  3_free(pOld);.  
9d5c0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
9d5d0 20 20 69 66 28 20 6e 42 79 74 65 73 3e 3d 30 78    if( nBytes>=0x
9d5e0 37 66 66 66 66 66 30 30 20 29 7b 0a 20 20 20 20  7fffff00 ){.    
9d5f0 2f 2a 20 54 68 65 20 30 78 37 66 66 66 66 30 30  /* The 0x7ffff00
9d600 20 6c 69 6d 69 74 20 74 65 72 6d 20 69 73 20 65   limit term is e
9d610 78 70 6c 61 69 6e 65 64 20 69 6e 20 63 6f 6d 6d  xplained in comm
9d620 65 6e 74 73 20 6f 6e 20 73 71 6c 69 74 65 33 4d  ents on sqlite3M
9d630 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 20 20 72  alloc() */.    r
9d640 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e  eturn 0;.  }.  n
9d650 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  Old = sqlite3Mal
9d660 6c 6f 63 53 69 7a 65 28 70 4f 6c 64 29 3b 0a 20  locSize(pOld);. 
9d670 20 6e 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47   nNew = sqlite3G
9d680 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52  lobalConfig.m.xR
9d690 6f 75 6e 64 75 70 28 6e 42 79 74 65 73 29 3b 0a  oundup(nBytes);.
9d6a0 20 20 69 66 28 20 6e 4f 6c 64 3d 3d 6e 4e 65 77    if( nOld==nNew
9d6b0 20 29 7b 0a 20 20 20 20 70 4e 65 77 20 3d 20 70   ){.    pNew = p
9d6c0 4f 6c 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  Old;.  }else if(
9d6d0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
9d6e0 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b  nfig.bMemstat ){
9d6f0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
9d700 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75  ex_enter(mem0.mu
9d710 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  tex);.    sqlite
9d720 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54  3StatusSet(SQLIT
9d730 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f  E_STATUS_MALLOC_
9d740 53 49 5a 45 2c 20 6e 42 79 74 65 73 29 3b 0a 20  SIZE, nBytes);. 
9d750 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
9d760 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54 45  atusValue(SQLITE
9d770 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55  _STATUS_MEMORY_U
9d780 53 45 44 29 2b 6e 4e 65 77 2d 6e 4f 6c 64 20 3e  SED)+nNew-nOld >
9d790 3d 20 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d  = .          mem
9d7a0 30 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64  0.alarmThreshold
9d7b0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
9d7c0 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 4e 65  3MallocAlarm(nNe
9d7d0 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 20 7d 0a 20  w-nOld);.    }. 
9d7e0 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65     pNew = sqlite
9d7f0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
9d800 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e  xRealloc(pOld, n
9d810 4e 65 77 29 3b 0a 20 20 20 20 69 66 28 20 70 4e  New);.    if( pN
9d820 65 77 3d 3d 30 20 26 26 20 6d 65 6d 30 2e 61 6c  ew==0 && mem0.al
9d830 61 72 6d 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20  armCallback ){. 
9d840 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c       sqlite3Mall
9d850 6f 63 41 6c 61 72 6d 28 6e 42 79 74 65 73 29 3b  ocAlarm(nBytes);
9d860 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71  .      pNew = sq
9d870 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9d880 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c  g.m.xRealloc(pOl
9d890 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 7d 0a  d, nNew);.    }.
9d8a0 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a      if( pNew ){.
9d8b0 20 20 20 20 20 20 6e 4e 65 77 20 3d 20 73 71 6c        nNew = sql
9d8c0 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70  ite3MallocSize(p
9d8d0 4e 65 77 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  New);.      sqli
9d8e0 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c  te3StatusAdd(SQL
9d8f0 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52  ITE_STATUS_MEMOR
9d900 59 5f 55 53 45 44 2c 20 6e 4e 65 77 2d 6e 4f 6c  Y_USED, nNew-nOl
9d910 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  d);.    }.    sq
9d920 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
9d930 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20  e(mem0.mutex);. 
9d940 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77   }else{.    pNew
9d950 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
9d960 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f  Config.m.xReallo
9d970 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20  c(pOld, nNew);. 
9d980 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77   }.  return pNew
9d990 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70  ;.}../*.** The p
9d9a0 75 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65 20  ublic interface 
9d9b0 74 6f 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f  to sqlite3Reallo
9d9c0 63 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68  c.  Make sure th
9d9d0 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a  at the memory.**
9d9e0 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e   subsystem is in
9d9f0 69 74 69 61 6c 69 7a 65 64 20 70 72 69 6f 72 20  itialized prior 
9da00 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69  to invoking sqli
9da10 74 65 52 65 61 6c 6c 6f 63 2e 0a 2a 2f 0a 53 51  teRealloc..*/.SQ
9da20 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
9da30 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 76  qlite3_realloc(v
9da40 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e  oid *pOld, int n
9da50 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ){.#ifndef SQLIT
9da60 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a  E_OMIT_AUTOINIT.
9da70 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e    if( sqlite3_in
9da80 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74  itialize() ) ret
9da90 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20  urn 0;.#endif.  
9daa0 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52 65  return sqlite3Re
9dab0 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 29 3b 0a  alloc(pOld, n);.
9dac0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  }.../*.** Alloca
9dad0 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f  te and zero memo
9dae0 72 79 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50  ry..*/ .SQLITE_P
9daf0 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
9db00 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69  ite3MallocZero(i
9db10 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70  nt n){.  void *p
9db20 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
9db30 28 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a  (n);.  if( p ){.
9db40 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c      memset(p, 0,
9db50 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   n);.  }.  retur
9db60 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c  n p;.}../*.** Al
9db70 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20  locate and zero 
9db80 6d 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 20  memory.  If the 
9db90 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73  allocation fails
9dba0 2c 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61  , make.** the ma
9dbb0 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20  llocFailed flag 
9dbc0 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  in the connectio
9dbd0 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51  n pointer..*/.SQ
9dbe0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
9dbf0 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  d *sqlite3DbMall
9dc00 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65 33 20 2a  ocZero(sqlite3 *
9dc10 64 62 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f  db, int n){.  vo
9dc20 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 44  id *p = sqlite3D
9dc30 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e  bMallocRaw(db, n
9dc40 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  );.  if( p ){.  
9dc50 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e    memset(p, 0, n
9dc60 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
9dc70 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  p;.}../*.** Allo
9dc80 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65  cate and zero me
9dc90 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 20 61 6c  mory.  If the al
9dca0 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20  location fails, 
9dcb0 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c  make.** the mall
9dcc0 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e  ocFailed flag in
9dcd0 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20   the connection 
9dce0 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49  pointer..**.** I
9dcf0 66 20 64 62 21 3d 30 20 61 6e 64 20 64 62 2d 3e  f db!=0 and db->
9dd00 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 69 73 20  mallocFailed is 
9dd10 74 72 75 65 20 28 69 6e 64 69 63 61 74 69 6e 67  true (indicating
9dd20 20 61 20 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 0a   a prior malloc.
9dd30 2a 2a 20 66 61 69 6c 75 72 65 20 6f 6e 20 74 68  ** failure on th
9dd40 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
9dd50 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 74 68 65 6e  connection) then
9dd60 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 30   always return 0
9dd70 2e 0a 2a 2a 20 48 65 6e 63 65 20 66 6f 72 20 61  ..** Hence for a
9dd80 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61   particular data
9dd90 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c  base connection,
9dda0 20 6f 6e 63 65 20 6d 61 6c 6c 6f 63 20 73 74 61   once malloc sta
9ddb0 72 74 73 0a 2a 2a 20 66 61 69 6c 69 6e 67 2c 20  rts.** failing, 
9ddc0 69 74 20 66 61 69 6c 73 20 63 6f 6e 73 69 73 74  it fails consist
9ddd0 65 6e 74 6c 79 20 75 6e 74 69 6c 20 6d 61 6c 6c  ently until mall
9dde0 6f 63 46 61 69 6c 65 64 20 69 73 20 72 65 73 65  ocFailed is rese
9ddf0 74 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e  t..** This is an
9de00 20 69 6d 70 6f 72 74 61 6e 74 20 61 73 73 75 6d   important assum
9de10 70 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 61 72  ption.  There ar
9de20 65 20 6d 61 6e 79 20 70 6c 61 63 65 73 20 69 6e  e many places in
9de30 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20 74 68 61   the.** code tha
9de40 74 20 64 6f 20 74 68 69 6e 67 73 20 6c 69 6b 65  t do things like
9de50 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
9de60 20 20 20 20 20 69 6e 74 20 2a 61 20 3d 20 28 69       int *a = (i
9de70 6e 74 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c  nt*)sqlite3DbMal
9de80 6c 6f 63 52 61 77 28 64 62 2c 20 31 30 30 29 3b  locRaw(db, 100);
9de90 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 74 20  .**         int 
9dea0 2a 62 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69 74  *b = (int*)sqlit
9deb0 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
9dec0 2c 20 32 30 30 29 3b 0a 2a 2a 20 20 20 20 20 20  , 200);.**      
9ded0 20 20 20 69 66 28 20 62 20 29 20 61 5b 31 30 5d     if( b ) a[10]
9dee0 20 3d 20 39 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f   = 9;.**.** In o
9def0 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 61  ther words, if a
9df00 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c   subsequent mall
9df10 6f 63 20 28 65 78 3a 20 22 62 22 29 20 77 6f 72  oc (ex: "b") wor
9df20 6b 65 64 2c 20 69 74 20 69 73 20 61 73 73 75 6d  ked, it is assum
9df30 65 64 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20 70  ed.** that all p
9df40 72 69 6f 72 20 6d 61 6c 6c 6f 63 73 20 28 65 78  rior mallocs (ex
9df50 3a 20 22 61 22 29 20 77 6f 72 6b 65 64 20 74 6f  : "a") worked to
9df60 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  o..*/.SQLITE_PRI
9df70 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
9df80 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71  e3DbMallocRaw(sq
9df90 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e  lite3 *db, int n
9dfa0 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20  ){.  void *p;.  
9dfb0 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c  assert( db==0 ||
9dfc0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
9dfd0 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
9dfe0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
9dff0 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a  _OMIT_LOOKASIDE.
9e000 20 20 69 66 28 20 64 62 20 29 7b 0a 20 20 20 20    if( db ){.    
9e010 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70  LookasideSlot *p
9e020 42 75 66 3b 0a 20 20 20 20 69 66 28 20 64 62 2d  Buf;.    if( db-
9e030 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
9e040 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
9e050 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64  .    }.    if( d
9e060 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e  b->lookaside.bEn
9e070 61 62 6c 65 64 20 26 26 20 6e 3c 3d 64 62 2d 3e  abled && n<=db->
9e080 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 0a 20 20 20  lookaside.sz.   
9e090 20 20 20 20 20 20 26 26 20 28 70 42 75 66 20 3d        && (pBuf =
9e0a0 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70   db->lookaside.p
9e0b0 46 72 65 65 29 21 3d 30 20 29 7b 0a 20 20 20 20  Free)!=0 ){.    
9e0c0 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
9e0d0 70 46 72 65 65 20 3d 20 70 42 75 66 2d 3e 70 4e  pFree = pBuf->pN
9e0e0 65 78 74 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c  ext;.      db->l
9e0f0 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 2b 2b 3b  ookaside.nOut++;
9e100 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6c  .      if( db->l
9e110 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3e 64 62  ookaside.nOut>db
9e120 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75  ->lookaside.mxOu
9e130 74 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d  t ){.        db-
9e140 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74  >lookaside.mxOut
9e150 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65   = db->lookaside
9e160 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20  .nOut;.      }. 
9e170 20 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f 69       return (voi
9e180 64 2a 29 70 42 75 66 3b 0a 20 20 20 20 7d 0a 20  d*)pBuf;.    }. 
9e190 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 64   }.#else.  if( d
9e1a0 62 20 26 26 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  b && db->mallocF
9e1b0 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74  ailed ){.    ret
9e1c0 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69  urn 0;.  }.#endi
9e1d0 66 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d  f.  p = sqlite3M
9e1e0 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 28 20  alloc(n);.  if( 
9e1f0 21 70 20 26 26 20 64 62 20 29 7b 0a 20 20 20 20  !p && db ){.    
9e200 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
9e210 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 1;.  }.  retu
9e220 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn p;.}../*.** R
9e230 65 73 69 7a 65 20 74 68 65 20 62 6c 6f 63 6b 20  esize the block 
9e240 6f 66 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65  of memory pointe
9e250 64 20 74 6f 20 62 79 20 70 20 74 6f 20 6e 20 62  d to by p to n b
9e260 79 74 65 73 2e 20 49 66 20 74 68 65 0a 2a 2a 20  ytes. If the.** 
9e270 72 65 73 69 7a 65 20 66 61 69 6c 73 2c 20 73 65  resize fails, se
9e280 74 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c  t the mallocFail
9e290 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65 20 63  ed flag in the c
9e2a0 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74  onnection object
9e2b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
9e2c0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
9e2d0 33 44 62 52 65 61 6c 6c 6f 63 28 73 71 6c 69 74  3DbRealloc(sqlit
9e2e0 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 2c  e3 *db, void *p,
9e2f0 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20   int n){.  void 
9e300 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 61 73 73  *pNew = 0;.  ass
9e310 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20  ert( db!=0 );.  
9e320 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
9e330 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
9e340 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 64  utex) );.  if( d
9e350 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d  b->mallocFailed=
9e360 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 3d  =0 ){.    if( p=
9e370 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =0 ){.      retu
9e380 72 6e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  rn sqlite3DbMall
9e390 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20  ocRaw(db, n);.  
9e3a0 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 4c 6f    }.    if( isLo
9e3b0 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29 20 29  okaside(db, p) )
9e3c0 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 3c 3d 64  {.      if( n<=d
9e3d0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 20  b->lookaside.sz 
9e3e0 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
9e3f0 6e 20 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n p;.      }.   
9e400 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65     pNew = sqlite
9e410 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
9e420 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70   n);.      if( p
9e430 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  New ){.        m
9e440 65 6d 63 70 79 28 70 4e 65 77 2c 20 70 2c 20 64  emcpy(pNew, p, d
9e450 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 29  b->lookaside.sz)
9e460 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
9e470 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a  3DbFree(db, p);.
9e480 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
9e490 65 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20  e{.      pNew = 
9e4a0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
9e4b0 70 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 28  p, n);.      if(
9e4c0 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20   !pNew ){.      
9e4d0 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
9e4e0 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  ed = 1;.      }.
9e4f0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
9e500 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pNew;.}../*.*
9e510 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65 61  * Attempt to rea
9e520 6c 6c 6f 63 61 74 65 20 70 2e 20 20 49 66 20 74  llocate p.  If t
9e530 68 65 20 72 65 61 6c 6c 6f 63 61 74 69 6f 6e 20  he reallocation 
9e540 66 61 69 6c 73 2c 20 74 68 65 6e 20 66 72 65 65  fails, then free
9e550 20 70 0a 2a 2a 20 61 6e 64 20 73 65 74 20 74 68   p.** and set th
9e560 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66  e mallocFailed f
9e570 6c 61 67 20 69 6e 20 74 68 65 20 64 61 74 61 62  lag in the datab
9e580 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a  ase connection..
9e590 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
9e5a0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44  E void *sqlite3D
9e5b0 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 73  bReallocOrFree(s
9e5c0 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64  qlite3 *db, void
9e5d0 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76   *p, int n){.  v
9e5e0 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 70 4e 65  oid *pNew;.  pNe
9e5f0 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  w = sqlite3DbRea
9e600 6c 6c 6f 63 28 64 62 2c 20 70 2c 20 6e 29 3b 0a  lloc(db, p, n);.
9e610 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20    if( !pNew ){. 
9e620 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
9e630 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a 20 20 72  (db, p);.  }.  r
9e640 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f  eturn pNew;.}../
9e650 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79  *.** Make a copy
9e660 20 6f 66 20 61 20 73 74 72 69 6e 67 20 69 6e 20   of a string in 
9e670 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20  memory obtained 
9e680 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  from sqliteMallo
9e690 63 28 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20 66  c(). These .** f
9e6a0 75 6e 63 74 69 6f 6e 73 20 63 61 6c 6c 20 73 71  unctions call sq
9e6b0 6c 69 74 65 33 4d 61 6c 6c 6f 63 52 61 77 28 29  lite3MallocRaw()
9e6c0 20 64 69 72 65 63 74 6c 79 20 69 6e 73 74 65 61   directly instea
9e6d0 64 20 6f 66 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  d of sqliteMallo
9e6e0 63 28 29 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20  c(). This.** is 
9e6f0 62 65 63 61 75 73 65 20 77 68 65 6e 20 6d 65 6d  because when mem
9e700 6f 72 79 20 64 65 62 75 67 67 69 6e 67 20 69 73  ory debugging is
9e710 20 74 75 72 6e 65 64 20 6f 6e 2c 20 74 68 65 73   turned on, thes
9e720 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20  e two functions 
9e730 61 72 65 20 0a 2a 2a 20 63 61 6c 6c 65 64 20 76  are .** called v
9e740 69 61 20 6d 61 63 72 6f 73 20 74 68 61 74 20 72  ia macros that r
9e750 65 63 6f 72 64 20 74 68 65 20 63 75 72 72 65 6e  ecord the curren
9e760 74 20 66 69 6c 65 20 61 6e 64 20 6c 69 6e 65 20  t file and line 
9e770 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 0a 2a 2a  number in the.**
9e780 20 54 68 72 65 61 64 44 61 74 61 20 73 74 72 75   ThreadData stru
9e790 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cture..*/.SQLITE
9e7a0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
9e7b0 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 73  qlite3DbStrDup(s
9e7c0 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73  qlite3 *db, cons
9e7d0 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63 68  t char *z){.  ch
9e7e0 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 73 69 7a 65  ar *zNew;.  size
9e7f0 5f 74 20 6e 3b 0a 20 20 69 66 28 20 7a 3d 3d 30  _t n;.  if( z==0
9e800 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
9e810 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 73 71 6c 69  ;.  }.  n = sqli
9e820 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 20 2b  te3Strlen30(z) +
9e830 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6e   1;.  assert( (n
9e840 26 30 78 37 66 66 66 66 66 66 66 29 3d 3d 6e 20  &0x7fffffff)==n 
9e850 29 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69  );.  zNew = sqli
9e860 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
9e870 62 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 69 66  b, (int)n);.  if
9e880 28 20 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65  ( zNew ){.    me
9e890 6d 63 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29  mcpy(zNew, z, n)
9e8a0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  ;.  }.  return z
9e8b0 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  New;.}.SQLITE_PR
9e8c0 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69  IVATE char *sqli
9e8d0 74 65 33 44 62 53 74 72 4e 44 75 70 28 73 71 6c  te3DbStrNDup(sql
9e8e0 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
9e8f0 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b  char *z, int n){
9e900 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20  .  char *zNew;. 
9e910 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20   if( z==0 ){.   
9e920 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
9e930 20 61 73 73 65 72 74 28 20 28 6e 26 30 78 37 66   assert( (n&0x7f
9e940 66 66 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 20 20  ffffff)==n );.  
9e950 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  zNew = sqlite3Db
9e960 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2b  MallocRaw(db, n+
9e970 31 29 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20 29  1);.  if( zNew )
9e980 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65  {.    memcpy(zNe
9e990 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a 4e  w, z, n);.    zN
9e9a0 65 77 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20  ew[n] = 0;.  }. 
9e9b0 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a   return zNew;.}.
9e9c0 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
9e9d0 73 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 20  string from the 
9e9e0 7a 46 72 6f 6d 61 74 20 61 72 67 75 6d 65 6e 74  zFromat argument
9e9f0 20 61 6e 64 20 74 68 65 20 76 61 5f 6c 69 73 74   and the va_list
9ea00 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a 2a   that follows..*
9ea10 2a 20 53 74 6f 72 65 20 74 68 65 20 73 74 72 69  * Store the stri
9ea20 6e 67 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74  ng in memory obt
9ea30 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
9ea40 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 61  eMalloc() and ma
9ea50 6b 65 20 2a 70 7a 0a 2a 2a 20 70 6f 69 6e 74 20  ke *pz.** point 
9ea60 74 6f 20 74 68 61 74 20 73 74 72 69 6e 67 2e 0a  to that string..
9ea70 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
9ea80 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65  E void sqlite3Se
9ea90 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 70  tString(char **p
9eaa0 7a 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  z, sqlite3 *db, 
9eab0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
9eac0 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f  mat, ...){.  va_
9ead0 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20  list ap;.  char 
9eae0 2a 7a 3b 0a 0a 20 20 76 61 5f 73 74 61 72 74 28  *z;..  va_start(
9eaf0 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20  ap, zFormat);.  
9eb00 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69  z = sqlite3VMPri
9eb10 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c  ntf(db, zFormat,
9eb20 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61   ap);.  va_end(a
9eb30 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  p);.  sqlite3DbF
9eb40 72 65 65 28 64 62 2c 20 2a 70 7a 29 3b 0a 20 20  ree(db, *pz);.  
9eb50 2a 70 7a 20 3d 20 7a 3b 0a 7d 0a 0a 0a 2f 2a 0a  *pz = z;.}.../*.
9eb60 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
9eb70 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20   must be called 
9eb80 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 61  before exiting a
9eb90 6e 79 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20  ny API function 
9eba0 28 69 2e 65 2e 20 0a 2a 2a 20 72 65 74 75 72 6e  (i.e. .** return
9ebb0 69 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74  ing control to t
9ebc0 68 65 20 75 73 65 72 29 20 74 68 61 74 20 68 61  he user) that ha
9ebd0 73 20 63 61 6c 6c 65 64 20 73 71 6c 69 74 65 33  s called sqlite3
9ebe0 5f 6d 61 6c 6c 6f 63 20 6f 72 0a 2a 2a 20 73 71  _malloc or.** sq
9ebf0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 2e 0a 2a  lite3_realloc..*
9ec00 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65  *.** The returne
9ec10 64 20 76 61 6c 75 65 20 69 73 20 6e 6f 72 6d 61  d value is norma
9ec20 6c 6c 79 20 61 20 63 6f 70 79 20 6f 66 20 74 68  lly a copy of th
9ec30 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
9ec40 74 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75 6e  t to this.** fun
9ec50 63 74 69 6f 6e 2e 20 48 6f 77 65 76 65 72 2c 20  ction. However, 
9ec60 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  if a malloc() fa
9ec70 69 6c 75 72 65 20 68 61 73 20 6f 63 63 75 72 72  ilure has occurr
9ec80 65 64 20 73 69 6e 63 65 20 74 68 65 20 70 72 65  ed since the pre
9ec90 76 69 6f 75 73 0a 2a 2a 20 69 6e 76 6f 63 61 74  vious.** invocat
9eca0 69 6f 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  ion SQLITE_NOMEM
9ecb0 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 73   is returned ins
9ecc0 74 65 61 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  tead. .**.** If 
9ecd0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
9ece0 6e 74 2c 20 64 62 2c 20 69 73 20 6e 6f 74 20 4e  nt, db, is not N
9ecf0 55 4c 4c 20 61 6e 64 20 61 20 6d 61 6c 6c 6f 63  ULL and a malloc
9ed00 28 29 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63  () error has occ
9ed10 75 72 72 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74  urred,.** then t
9ed20 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 65 72  he connection er
9ed30 72 6f 72 2d 63 6f 64 65 20 28 74 68 65 20 76 61  ror-code (the va
9ed40 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
9ed50 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28  sqlite3_errcode(
9ed60 29 29 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20  )).** is set to 
9ed70 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f  SQLITE_NOMEM..*/
9ed80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
9ed90 69 6e 74 20 73 71 6c 69 74 65 33 41 70 69 45 78  int sqlite3ApiEx
9eda0 69 74 28 73 71 6c 69 74 65 33 2a 20 64 62 2c 20  it(sqlite3* db, 
9edb0 69 6e 74 20 72 63 29 7b 0a 20 20 2f 2a 20 49 66  int rc){.  /* If
9edc0 20 74 68 65 20 64 62 20 68 61 6e 64 6c 65 20 69   the db handle i
9edd0 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
9ede0 20 77 65 20 6d 75 73 74 20 68 6f 6c 64 20 74 68   we must hold th
9edf0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e  e connection han
9ee00 64 6c 65 0a 20 20 2a 2a 20 6d 75 74 65 78 20 68  dle.  ** mutex h
9ee10 65 72 65 2e 20 4f 74 68 65 72 77 69 73 65 20 74  ere. Otherwise t
9ee20 68 65 20 72 65 61 64 20 28 61 6e 64 20 70 6f 73  he read (and pos
9ee30 73 69 62 6c 65 20 77 72 69 74 65 29 20 6f 66 20  sible write) of 
9ee40 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
9ee50 20 0a 20 20 2a 2a 20 69 73 20 75 6e 73 61 66 65   .  ** is unsafe
9ee60 2c 20 61 73 20 69 73 20 74 68 65 20 63 61 6c 6c  , as is the call
9ee70 20 74 6f 20 73 71 6c 69 74 65 33 45 72 72 6f 72   to sqlite3Error
9ee80 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ()..  */.  asser
9ee90 74 28 20 21 64 62 20 7c 7c 20 73 71 6c 69 74 65  t( !db || sqlite
9eea0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
9eeb0 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28  >mutex) );.  if(
9eec0 20 64 62 20 26 26 20 28 64 62 2d 3e 6d 61 6c 6c   db && (db->mall
9eed0 6f 63 46 61 69 6c 65 64 20 7c 7c 20 72 63 3d 3d  ocFailed || rc==
9eee0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d  SQLITE_IOERR_NOM
9eef0 45 4d 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  EM) ){.    sqlit
9ef00 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49  e3Error(db, SQLI
9ef10 54 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20  TE_NOMEM, 0);.  
9ef20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
9ef30 65 64 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d  ed = 0;.    rc =
9ef40 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
9ef50 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26   }.  return rc &
9ef60 20 28 64 62 20 3f 20 64 62 2d 3e 65 72 72 4d 61   (db ? db->errMa
9ef70 73 6b 20 3a 20 30 78 66 66 29 3b 0a 7d 0a 0a 2f  sk : 0xff);.}../
9ef80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
9ef90 6e 64 20 6f 66 20 6d 61 6c 6c 6f 63 2e 63 20 2a  nd of malloc.c *
9efa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9efb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9efc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
9efd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
9efe0 65 67 69 6e 20 66 69 6c 65 20 70 72 69 6e 74 66  egin file printf
9eff0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
9f000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
9f020 2a 0a 2a 2a 20 54 68 65 20 22 70 72 69 6e 74 66  *.** The "printf
9f030 22 20 63 6f 64 65 20 74 68 61 74 20 66 6f 6c 6c  " code that foll
9f040 6f 77 73 20 64 61 74 65 73 20 66 72 6f 6d 20 74  ows dates from t
9f050 68 65 20 31 39 38 30 27 73 2e 20 20 49 74 20 69  he 1980's.  It i
9f060 73 20 69 6e 0a 2a 2a 20 74 68 65 20 70 75 62 6c  s in.** the publ
9f070 69 63 20 64 6f 6d 61 69 6e 2e 20 20 54 68 65 20  ic domain.  The 
9f080 6f 72 69 67 69 6e 61 6c 20 63 6f 6d 6d 65 6e 74  original comment
9f090 73 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20 68  s are included h
9f0a0 65 72 65 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 6c  ere for.** compl
9f0b0 65 74 65 6e 65 73 73 2e 20 20 54 68 65 79 20 61  eteness.  They a
9f0c0 72 65 20 76 65 72 79 20 6f 75 74 2d 6f 66 2d 64  re very out-of-d
9f0d0 61 74 65 20 62 75 74 20 6d 69 67 68 74 20 62 65  ate but might be
9f0e0 20 75 73 65 66 75 6c 20 61 73 0a 2a 2a 20 61 6e   useful as.** an
9f0f0 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65 66 65   historical refe
9f100 72 65 6e 63 65 2e 20 20 4d 6f 73 74 20 6f 66 20  rence.  Most of 
9f110 74 68 65 20 22 65 6e 68 61 6e 63 65 6d 65 6e 74  the "enhancement
9f120 73 22 20 68 61 76 65 20 62 65 65 6e 20 62 61 63  s" have been bac
9f130 6b 65 64 0a 2a 2a 20 6f 75 74 20 73 6f 20 74 68  ked.** out so th
9f140 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 61  at the functiona
9f150 6c 69 74 79 20 69 73 20 6e 6f 77 20 74 68 65 20  lity is now the 
9f160 73 61 6d 65 20 61 73 20 73 74 61 6e 64 61 72 64  same as standard
9f170 20 70 72 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a 2a   printf()..**.**
9f180 20 24 49 64 3a 20 70 72 69 6e 74 66 2e 63 2c 76   $Id: printf.c,v
9f190 20 31 2e 31 30 34 20 32 30 30 39 2f 30 36 2f 30   1.104 2009/06/0
9f1a0 33 20 30 31 3a 32 34 3a 35 34 20 64 72 68 20 45  3 01:24:54 drh E
9f1b0 78 70 20 24 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  xp $.**.********
9f1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f200 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  **.**.** The fol
9f210 6c 6f 77 69 6e 67 20 6d 6f 64 75 6c 65 73 20 69  lowing modules i
9f220 73 20 61 6e 20 65 6e 68 61 6e 63 65 64 20 72 65  s an enhanced re
9f230 70 6c 61 63 65 6d 65 6e 74 20 66 6f 72 20 74 68  placement for th
9f240 65 20 22 70 72 69 6e 74 66 22 20 73 75 62 72 6f  e "printf" subro
9f250 75 74 69 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64 20  utines.** found 
9f260 69 6e 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  in the standard 
9f270 43 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20  C library.  The 
9f280 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 68 61 6e 63  following enhanc
9f290 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 73 75  ements are.** su
9f2a0 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20  pported:.**.**  
9f2b0 20 20 20 20 2b 20 20 41 64 64 69 74 69 6f 6e 61      +  Additiona
9f2c0 6c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68  l functions.  Th
9f2d0 65 20 73 74 61 6e 64 61 72 64 20 73 65 74 20 6f  e standard set o
9f2e0 66 20 22 70 72 69 6e 74 66 22 20 66 75 6e 63 74  f "printf" funct
9f2f0 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ions.**         
9f300 69 6e 63 6c 75 64 65 73 20 70 72 69 6e 74 66 2c  includes printf,
9f310 20 66 70 72 69 6e 74 66 2c 20 73 70 72 69 6e 74   fprintf, sprint
9f320 66 2c 20 76 70 72 69 6e 74 66 2c 20 76 66 70 72  f, vprintf, vfpr
9f330 69 6e 74 66 2c 20 61 6e 64 0a 2a 2a 20 20 20 20  intf, and.**    
9f340 20 20 20 20 20 76 73 70 72 69 6e 74 66 2e 20 20       vsprintf.  
9f350 54 68 69 73 20 6d 6f 64 75 6c 65 20 61 64 64 73  This module adds
9f360 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
9f370 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  **.**           
9f380 2a 20 20 73 6e 70 72 69 6e 74 66 20 2d 2d 20 57  *  snprintf -- W
9f390 6f 72 6b 73 20 6c 69 6b 65 20 73 70 72 69 6e 74  orks like sprint
9f3a0 66 2c 20 62 75 74 20 68 61 73 20 61 6e 20 65 78  f, but has an ex
9f3b0 74 72 61 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  tra argument.** 
9f3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9f3d0 20 20 20 20 20 20 20 20 20 77 68 69 63 68 20 69           which i
9f3e0 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  s the size of th
9f3f0 65 20 62 75 66 66 65 72 20 77 72 69 74 74 65 6e  e buffer written
9f400 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20   to..**.**      
9f410 20 20 20 20 20 2a 20 20 6d 70 72 69 6e 74 66 20       *  mprintf 
9f420 2d 2d 20 20 53 69 6d 69 6c 61 72 20 74 6f 20 73  --  Similar to s
9f430 70 72 69 6e 74 66 2e 20 20 57 72 69 74 65 73 20  printf.  Writes 
9f440 6f 75 74 70 75 74 20 74 6f 20 6d 65 6d 6f 72 79  output to memory
9f450 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
9f460 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 62 74               obt
9f470 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f  ained from mallo
9f480 63 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  c..**.**        
9f490 20 20 20 2a 20 20 78 70 72 69 6e 74 66 20 2d 2d     *  xprintf --
9f4a0 20 20 43 61 6c 6c 73 20 61 20 66 75 6e 63 74 69    Calls a functi
9f4b0 6f 6e 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66  on to dispose of
9f4c0 20 6f 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20   output..**.**  
9f4d0 20 20 20 20 20 20 20 20 20 2a 20 20 6e 70 72 69           *  npri
9f4e0 6e 74 66 20 2d 2d 20 20 4e 6f 20 6f 75 74 70 75  ntf --  No outpu
9f4f0 74 2c 20 62 75 74 20 72 65 74 75 72 6e 73 20 74  t, but returns t
9f500 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61  he number of cha
9f510 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 20  racters.**      
9f520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9f530 20 20 20 20 74 68 61 74 20 77 6f 75 6c 64 20 68      that would h
9f540 61 76 65 20 62 65 65 6e 20 6f 75 74 70 75 74 20  ave been output 
9f550 62 79 20 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a 2a  by printf..**.**
9f560 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 41 20             *  A 
9f570 76 2d 20 76 65 72 73 69 6f 6e 20 28 65 78 3a 20  v- version (ex: 
9f580 76 73 6e 70 72 69 6e 74 66 29 20 6f 66 20 65 76  vsnprintf) of ev
9f590 65 72 79 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  ery function is 
9f5a0 61 6c 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20  also.**         
9f5b0 20 20 20 20 20 73 75 70 70 6c 69 65 64 2e 0a 2a       supplied..*
9f5c0 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 20 66  *.**      +  A f
9f5d0 65 77 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 6f  ew extensions to
9f5e0 20 74 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 20   the formatting 
9f5f0 6e 6f 74 61 74 69 6f 6e 20 61 72 65 20 73 75 70  notation are sup
9f600 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20  ported:.**.**   
9f610 20 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 22          *  The "
9f620 3d 22 20 66 6c 61 67 20 28 73 69 6d 69 6c 61 72  =" flag (similar
9f630 20 74 6f 20 22 2d 22 29 20 63 61 75 73 65 73 20   to "-") causes 
9f640 74 68 65 20 6f 75 74 70 75 74 20 74 6f 20 62 65  the output to be
9f650 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
9f660 20 62 65 20 63 65 6e 74 65 72 65 64 20 69 6e 20   be centered in 
9f670 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c  the appropriatel
9f680 79 20 73 69 7a 65 64 20 66 69 65 6c 64 2e 0a 2a  y sized field..*
9f690 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a  *.**           *
9f6a0 20 20 54 68 65 20 25 62 20 66 69 65 6c 64 20 6f    The %b field o
9f6b0 75 74 70 75 74 73 20 61 6e 20 69 6e 74 65 67 65  utputs an intege
9f6c0 72 20 69 6e 20 62 69 6e 61 72 79 20 6e 6f 74 61  r in binary nota
9f6d0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  tion..**.**     
9f6e0 20 20 20 20 20 20 2a 20 20 54 68 65 20 25 63 20        *  The %c 
9f6f0 66 69 65 6c 64 20 6e 6f 77 20 61 63 63 65 70 74  field now accept
9f700 73 20 61 20 70 72 65 63 69 73 69 6f 6e 2e 20 20  s a precision.  
9f710 54 68 65 20 63 68 61 72 61 63 74 65 72 20 6f 75  The character ou
9f720 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  tput.**         
9f730 20 20 20 20 20 69 73 20 72 65 70 65 61 74 65 64       is repeated
9f740 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   by the number o
9f750 66 20 74 69 6d 65 73 20 74 68 65 20 70 72 65 63  f times the prec
9f760 69 73 69 6f 6e 20 73 70 65 63 69 66 69 65 73 2e  ision specifies.
9f770 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
9f780 20 2a 20 20 54 68 65 20 25 27 20 66 69 65 6c 64   *  The %' field
9f790 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 63 2c 20   works like %c, 
9f7a0 62 75 74 20 74 61 6b 65 73 20 61 73 20 69 74 73  but takes as its
9f7b0 20 63 68 61 72 61 63 74 65 72 20 74 68 65 0a 2a   character the.*
9f7c0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e  *              n
9f7d0 65 78 74 20 63 68 61 72 61 63 74 65 72 20 6f 66  ext character of
9f7e0 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69   the format stri
9f7f0 6e 67 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 74  ng, instead of t
9f800 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20 20  he next.**      
9f810 20 20 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74          argument
9f820 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  .  For example, 
9f830 20 70 72 69 6e 74 66 28 22 25 2e 37 38 27 2d 22   printf("%.78'-"
9f840 29 20 20 70 72 69 6e 74 73 20 37 38 20 6d 69 6e  )  prints 78 min
9f850 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  us.**           
9f860 20 20 20 73 69 67 6e 73 2c 20 74 68 65 20 73 61     signs, the sa
9f870 6d 65 20 61 73 20 20 70 72 69 6e 74 66 28 22 25  me as  printf("%
9f880 2e 37 38 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a 2a  .78c",'-')..**.*
9f890 2a 20 20 20 20 20 20 2b 20 20 57 68 65 6e 20 63  *      +  When c
9f8a0 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 47 43  ompiled using GC
9f8b0 43 20 6f 6e 20 61 20 53 50 41 52 43 2c 20 74 68  C on a SPARC, th
9f8c0 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 72  is version of pr
9f8d0 69 6e 74 66 20 69 73 0a 2a 2a 20 20 20 20 20 20  intf is.**      
9f8e0 20 20 20 66 61 73 74 65 72 20 74 68 61 6e 20 74     faster than t
9f8f0 68 65 20 6c 69 62 72 61 72 79 20 70 72 69 6e 74  he library print
9f900 66 20 66 6f 72 20 53 55 4e 20 4f 53 20 34 2e 31  f for SUN OS 4.1
9f910 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20  ..**.**      +  
9f920 41 6c 6c 20 66 75 6e 63 74 69 6f 6e 73 20 61 72  All functions ar
9f930 65 20 66 75 6c 6c 79 20 72 65 65 6e 74 72 61 6e  e fully reentran
9f940 74 2e 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  t..**.*/../*.** 
9f950 43 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73  Conversion types
9f960 20 66 61 6c 6c 20 69 6e 74 6f 20 76 61 72 69 6f   fall into vario
9f970 75 73 20 63 61 74 65 67 6f 72 69 65 73 20 61 73  us categories as
9f980 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 65 0a   defined by the.
9f990 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 75  ** following enu
9f9a0 6d 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65  meration..*/.#de
9f9b0 66 69 6e 65 20 65 74 52 41 44 49 58 20 20 20 20  fine etRADIX    
9f9c0 20 20 20 31 20 2f 2a 20 49 6e 74 65 67 65 72 20     1 /* Integer 
9f9d0 74 79 70 65 73 2e 20 20 25 64 2c 20 25 78 2c 20  types.  %d, %x, 
9f9e0 25 6f 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68  %o, and so forth
9f9f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 46 4c   */.#define etFL
9fa00 4f 41 54 20 20 20 20 20 20 20 32 20 2f 2a 20 46  OAT       2 /* F
9fa10 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20  loating point.  
9fa20 25 66 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74  %f */.#define et
9fa30 45 58 50 20 20 20 20 20 20 20 20 20 33 20 2f 2a  EXP         3 /*
9fa40 20 45 78 70 6f 6e 65 6e 74 69 6f 6e 61 6c 20 6e   Exponentional n
9fa50 6f 74 61 74 69 6f 6e 2e 20 25 65 20 61 6e 64 20  otation. %e and 
9fa60 25 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74  %E */.#define et
9fa70 47 45 4e 45 52 49 43 20 20 20 20 20 34 20 2f 2a  GENERIC     4 /*
9fa80 20 46 6c 6f 61 74 69 6e 67 20 6f 72 20 65 78 70   Floating or exp
9fa90 6f 6e 65 6e 74 69 61 6c 2c 20 64 65 70 65 6e 64  onential, depend
9faa0 69 6e 67 20 6f 6e 20 65 78 70 6f 6e 65 6e 74 2e  ing on exponent.
9fab0 20 25 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %g */.#define e
9fac0 74 53 49 5a 45 20 20 20 20 20 20 20 20 35 20 2f  tSIZE        5 /
9fad0 2a 20 52 65 74 75 72 6e 20 6e 75 6d 62 65 72 20  * Return number 
9fae0 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 70 72  of characters pr
9faf0 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72 2e 20  ocessed so far. 
9fb00 25 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74  %n */.#define et
9fb10 53 54 52 49 4e 47 20 20 20 20 20 20 36 20 2f 2a  STRING      6 /*
9fb20 20 53 74 72 69 6e 67 73 2e 20 25 73 20 2a 2f 0a   Strings. %s */.
9fb30 23 64 65 66 69 6e 65 20 65 74 44 59 4e 53 54 52  #define etDYNSTR
9fb40 49 4e 47 20 20 20 37 20 2f 2a 20 44 79 6e 61 6d  ING   7 /* Dynam
9fb50 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64  ically allocated
9fb60 20 73 74 72 69 6e 67 73 2e 20 25 7a 20 2a 2f 0a   strings. %z */.
9fb70 23 64 65 66 69 6e 65 20 65 74 50 45 52 43 45 4e  #define etPERCEN
9fb80 54 20 20 20 20 20 38 20 2f 2a 20 50 65 72 63 65  T     8 /* Perce
9fb90 6e 74 20 73 79 6d 62 6f 6c 2e 20 25 25 20 2a 2f  nt symbol. %% */
9fba0 0a 23 64 65 66 69 6e 65 20 65 74 43 48 41 52 58  .#define etCHARX
9fbb0 20 20 20 20 20 20 20 39 20 2f 2a 20 43 68 61 72         9 /* Char
9fbc0 61 63 74 65 72 73 2e 20 25 63 20 2a 2f 0a 2f 2a  acters. %c */./*
9fbd0 20 54 68 65 20 72 65 73 74 20 61 72 65 20 65 78   The rest are ex
9fbe0 74 65 6e 73 69 6f 6e 73 2c 20 6e 6f 74 20 6e 6f  tensions, not no
9fbf0 72 6d 61 6c 6c 79 20 66 6f 75 6e 64 20 69 6e 20  rmally found in 
9fc00 70 72 69 6e 74 66 28 29 20 2a 2f 0a 23 64 65 66  printf() */.#def
9fc10 69 6e 65 20 65 74 53 51 4c 45 53 43 41 50 45 20  ine etSQLESCAPE 
9fc20 20 31 30 20 2f 2a 20 53 74 72 69 6e 67 73 20 77   10 /* Strings w
9fc30 69 74 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 64  ith '\'' doubled
9fc40 2e 20 20 25 71 20 2a 2f 0a 23 64 65 66 69 6e 65  .  %q */.#define
9fc50 20 65 74 53 51 4c 45 53 43 41 50 45 32 20 31 31   etSQLESCAPE2 11
9fc60 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68   /* Strings with
9fc70 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 20 61 6e   '\'' doubled an
9fc80 64 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 27 27  d enclosed in ''
9fc90 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
9fca0 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c              NULL
9fcb0 20 70 6f 69 6e 74 65 72 73 20 72 65 70 6c 61 63   pointers replac
9fcc0 65 64 20 62 79 20 53 51 4c 20 4e 55 4c 4c 2e 20  ed by SQL NULL. 
9fcd0 20 25 51 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %Q */.#define e
9fce0 74 54 4f 4b 45 4e 20 20 20 20 20 20 31 32 20 2f  tTOKEN      12 /
9fcf0 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  * a pointer to a
9fd00 20 54 6f 6b 65 6e 20 73 74 72 75 63 74 75 72 65   Token structure
9fd10 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 52   */.#define etSR
9fd20 43 4c 49 53 54 20 20 20 20 31 33 20 2f 2a 20 61  CLIST    13 /* a
9fd30 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53 72   pointer to a Sr
9fd40 63 4c 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e 65  cList */.#define
9fd50 20 65 74 50 4f 49 4e 54 45 52 20 20 20 20 31 34   etPOINTER    14
9fd60 20 2f 2a 20 54 68 65 20 25 70 20 63 6f 6e 76 65   /* The %p conve
9fd70 72 73 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65  rsion */.#define
9fd80 20 65 74 53 51 4c 45 53 43 41 50 45 33 20 31 35   etSQLESCAPE3 15
9fd90 20 2f 2a 20 25 77 20 2d 3e 20 53 74 72 69 6e 67   /* %w -> String
9fda0 73 20 77 69 74 68 20 27 5c 22 27 20 64 6f 75 62  s with '\"' doub
9fdb0 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65  led */.#define e
9fdc0 74 4f 52 44 49 4e 41 4c 20 20 20 20 31 36 20 2f  tORDINAL    16 /
9fdd0 2a 20 25 72 20 2d 3e 20 31 73 74 2c 20 32 6e 64  * %r -> 1st, 2nd
9fde0 2c 20 33 72 64 2c 20 34 74 68 2c 20 65 74 63 2e  , 3rd, 4th, etc.
9fdf0 20 20 45 6e 67 6c 69 73 68 20 6f 6e 6c 79 20 2a    English only *
9fe00 2f 0a 0a 23 64 65 66 69 6e 65 20 65 74 49 4e 56  /..#define etINV
9fe10 41 4c 49 44 20 20 20 20 20 30 20 2f 2a 20 41 6e  ALID     0 /* An
9fe20 79 20 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20 63  y unrecognized c
9fe30 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 20 2a  onversion type *
9fe40 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65 74  /.../*.** An "et
9fe50 42 79 74 65 22 20 69 73 20 61 6e 20 38 2d 62 69  Byte" is an 8-bi
9fe60 74 20 75 6e 73 69 67 6e 65 64 20 76 61 6c 75 65  t unsigned value
9fe70 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e 73  ..*/.typedef uns
9fe80 69 67 6e 65 64 20 63 68 61 72 20 65 74 42 79 74  igned char etByt
9fe90 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 62  e;../*.** Each b
9fea0 75 69 6c 74 69 6e 20 63 6f 6e 76 65 72 73 69 6f  uiltin conversio
9feb0 6e 20 63 68 61 72 61 63 74 65 72 20 28 65 78 3a  n character (ex:
9fec0 20 74 68 65 20 27 64 27 20 69 6e 20 22 25 64 22   the 'd' in "%d"
9fed0 29 20 69 73 20 64 65 73 63 72 69 62 65 64 0a 2a  ) is described.*
9fee0 2a 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65  * by an instance
9fef0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
9ff00 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 74  g structure.*/.t
9ff10 79 70 65 64 65 66 20 73 74 72 75 63 74 20 65 74  ypedef struct et
9ff20 5f 69 6e 66 6f 20 7b 20 20 20 2f 2a 20 49 6e 66  _info {   /* Inf
9ff30 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65  ormation about e
9ff40 61 63 68 20 66 6f 72 6d 61 74 20 66 69 65 6c 64  ach format field
9ff50 20 2a 2f 0a 20 20 63 68 61 72 20 66 6d 74 74 79   */.  char fmtty
9ff60 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  pe;            /
9ff70 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 66 69 65  * The format fie
9ff80 6c 64 20 63 6f 64 65 20 6c 65 74 74 65 72 20 2a  ld code letter *
9ff90 2f 0a 20 20 65 74 42 79 74 65 20 62 61 73 65 3b  /.  etByte base;
9ffa0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
9ffb0 54 68 65 20 62 61 73 65 20 66 6f 72 20 72 61 64  The base for rad
9ffc0 69 78 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f  ix conversion */
9ffd0 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 73 3b  .  etByte flags;
9ffe0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
9fff0 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 46 4c  ne or more of FL
a0000 41 47 5f 20 63 6f 6e 73 74 61 6e 74 73 20 62 65  AG_ constants be
a0010 6c 6f 77 20 2a 2f 0a 20 20 65 74 42 79 74 65 20  low */.  etByte 
a0020 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20  type;           
a0030 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20    /* Conversion 
a0040 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 65 74  paradigm */.  et
a0050 42 79 74 65 20 63 68 61 72 73 65 74 3b 20 20 20  Byte charset;   
a0060 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
a0070 20 69 6e 74 6f 20 61 44 69 67 69 74 73 5b 5d 20   into aDigits[] 
a0080 6f 66 20 74 68 65 20 64 69 67 69 74 73 20 73 74  of the digits st
a0090 72 69 6e 67 20 2a 2f 0a 20 20 65 74 42 79 74 65  ring */.  etByte
a00a0 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 20   prefix;        
a00b0 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74     /* Offset int
a00c0 6f 20 61 50 72 65 66 69 78 5b 5d 20 6f 66 20 74  o aPrefix[] of t
a00d0 68 65 20 70 72 65 66 69 78 20 73 74 72 69 6e 67  he prefix string
a00e0 20 2a 2f 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a 0a   */.} et_info;..
a00f0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61  /*.** Allowed va
a0100 6c 75 65 73 20 66 6f 72 20 65 74 5f 69 6e 66 6f  lues for et_info
a0110 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e  .flags.*/.#defin
a0120 65 20 46 4c 41 47 5f 53 49 47 4e 45 44 20 20 31  e FLAG_SIGNED  1
a0130 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a0140 74 68 65 20 76 61 6c 75 65 20 74 6f 20 63 6f 6e  the value to con
a0150 76 65 72 74 20 69 73 20 73 69 67 6e 65 64 20 2a  vert is signed *
a0160 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 49  /.#define FLAG_I
a0170 4e 54 45 52 4e 20 20 32 20 20 20 20 20 2f 2a 20  NTERN  2     /* 
a0180 54 72 75 65 20 69 66 20 66 6f 72 20 69 6e 74 65  True if for inte
a0190 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a 2f  rnal use only */
a01a0 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 53 54  .#define FLAG_ST
a01b0 52 49 4e 47 20 20 34 20 20 20 20 20 2f 2a 20 41  RING  4     /* A
a01c0 6c 6c 6f 77 20 69 6e 66 69 6e 69 74 79 20 70 72  llow infinity pr
a01d0 65 63 69 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 0a  ecision */.../*.
a01e0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
a01f0 20 74 61 62 6c 65 20 69 73 20 73 65 61 72 63 68   table is search
a0200 65 64 20 6c 69 6e 65 61 72 6c 79 2c 20 73 6f 20  ed linearly, so 
a0210 69 74 20 69 73 20 67 6f 6f 64 20 74 6f 20 70 75  it is good to pu
a0220 74 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 66 72  t the.** most fr
a0230 65 71 75 65 6e 74 6c 79 20 75 73 65 64 20 63 6f  equently used co
a0240 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66  nversion types f
a0250 69 72 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  irst..*/.static 
a0260 63 6f 6e 73 74 20 63 68 61 72 20 61 44 69 67 69  const char aDigi
a0270 74 73 5b 5d 20 3d 20 22 30 31 32 33 34 35 36 37  ts[] = "01234567
a0280 38 39 41 42 43 44 45 46 30 31 32 33 34 35 36 37  89ABCDEF01234567
a0290 38 39 61 62 63 64 65 66 22 3b 0a 73 74 61 74 69  89abcdef";.stati
a02a0 63 20 63 6f 6e 73 74 20 63 68 61 72 20 61 50 72  c const char aPr
a02b0 65 66 69 78 5b 5d 20 3d 20 22 2d 78 30 5c 30 30  efix[] = "-x0\00
a02c0 30 58 30 22 3b 0a 73 74 61 74 69 63 20 63 6f 6e  0X0";.static con
a02d0 73 74 20 65 74 5f 69 6e 66 6f 20 66 6d 74 69 6e  st et_info fmtin
a02e0 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20 20 27 64  fo[] = {.  {  'd
a02f0 27 2c 20 31 30 2c 20 31 2c 20 65 74 52 41 44 49  ', 10, 1, etRADI
a0300 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c  X,      0,  0 },
a0310 0a 20 20 7b 20 20 27 73 27 2c 20 20 30 2c 20 34  .  {  's',  0, 4
a0320 2c 20 65 74 53 54 52 49 4e 47 2c 20 20 20 20 20  , etSTRING,     
a0330 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 67  0,  0 },.  {  'g
a0340 27 2c 20 20 30 2c 20 31 2c 20 65 74 47 45 4e 45  ',  0, 1, etGENE
a0350 52 49 43 2c 20 20 20 20 33 30 2c 20 30 20 7d 2c  RIC,    30, 0 },
a0360 0a 20 20 7b 20 20 27 7a 27 2c 20 20 30 2c 20 34  .  {  'z',  0, 4
a0370 2c 20 65 74 44 59 4e 53 54 52 49 4e 47 2c 20 20  , etDYNSTRING,  
a0380 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 71  0,  0 },.  {  'q
a0390 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45  ',  0, 4, etSQLE
a03a0 53 43 41 50 45 2c 20 20 30 2c 20 20 30 20 7d 2c  SCAPE,  0,  0 },
a03b0 0a 20 20 7b 20 20 27 51 27 2c 20 20 30 2c 20 34  .  {  'Q',  0, 4
a03c0 2c 20 65 74 53 51 4c 45 53 43 41 50 45 32 2c 20  , etSQLESCAPE2, 
a03d0 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 77  0,  0 },.  {  'w
a03e0 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c 45  ',  0, 4, etSQLE
a03f0 53 43 41 50 45 33 2c 20 30 2c 20 20 30 20 7d 2c  SCAPE3, 0,  0 },
a0400 0a 20 20 7b 20 20 27 63 27 2c 20 20 30 2c 20 30  .  {  'c',  0, 0
a0410 2c 20 65 74 43 48 41 52 58 2c 20 20 20 20 20 20  , etCHARX,      
a0420 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6f  0,  0 },.  {  'o
a0430 27 2c 20 20 38 2c 20 30 2c 20 65 74 52 41 44 49  ',  8, 0, etRADI
a0440 58 2c 20 20 20 20 20 20 30 2c 20 20 32 20 7d 2c  X,      0,  2 },
a0450 0a 20 20 7b 20 20 27 75 27 2c 20 31 30 2c 20 30  .  {  'u', 10, 0
a0460 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20  , etRADIX,      
a0470 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 78  0,  0 },.  {  'x
a0480 27 2c 20 31 36 2c 20 30 2c 20 65 74 52 41 44 49  ', 16, 0, etRADI
a0490 58 2c 20 20 20 20 20 20 31 36 2c 20 31 20 7d 2c  X,      16, 1 },
a04a0 0a 20 20 7b 20 20 27 58 27 2c 20 31 36 2c 20 30  .  {  'X', 16, 0
a04b0 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20 20  , etRADIX,      
a04c0 30 2c 20 20 34 20 7d 2c 0a 23 69 66 6e 64 65 66  0,  4 },.#ifndef
a04d0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
a04e0 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b 20  ATING_POINT.  { 
a04f0 20 27 66 27 2c 20 20 30 2c 20 31 2c 20 65 74 46   'f',  0, 1, etF
a0500 4c 4f 41 54 2c 20 20 20 20 20 20 30 2c 20 20 30  LOAT,      0,  0
a0510 20 7d 2c 0a 20 20 7b 20 20 27 65 27 2c 20 20 30   },.  {  'e',  0
a0520 2c 20 31 2c 20 65 74 45 58 50 2c 20 20 20 20 20  , 1, etEXP,     
a0530 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20     30, 0 },.  { 
a0540 20 27 45 27 2c 20 20 30 2c 20 31 2c 20 65 74 45   'E',  0, 1, etE
a0550 58 50 2c 20 20 20 20 20 20 20 20 31 34 2c 20 30  XP,        14, 0
a0560 20 7d 2c 0a 20 20 7b 20 20 27 47 27 2c 20 20 30   },.  {  'G',  0
a0570 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43 2c 20  , 1, etGENERIC, 
a0580 20 20 20 31 34 2c 20 30 20 7d 2c 0a 23 65 6e 64     14, 0 },.#end
a0590 69 66 0a 20 20 7b 20 20 27 69 27 2c 20 31 30 2c  if.  {  'i', 10,
a05a0 20 31 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20   1, etRADIX,    
a05b0 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20    0,  0 },.  {  
a05c0 27 6e 27 2c 20 20 30 2c 20 30 2c 20 65 74 53 49  'n',  0, 0, etSI
a05d0 5a 45 2c 20 20 20 20 20 20 20 30 2c 20 20 30 20  ZE,       0,  0 
a05e0 7d 2c 0a 20 20 7b 20 20 27 25 27 2c 20 20 30 2c  },.  {  '%',  0,
a05f0 20 30 2c 20 65 74 50 45 52 43 45 4e 54 2c 20 20   0, etPERCENT,  
a0600 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20    0,  0 },.  {  
a0610 27 70 27 2c 20 31 36 2c 20 30 2c 20 65 74 50 4f  'p', 16, 0, etPO
a0620 49 4e 54 45 52 2c 20 20 20 20 30 2c 20 20 31 20  INTER,    0,  1 
a0630 7d 2c 0a 0a 2f 2a 20 41 6c 6c 20 74 68 65 20 72  },../* All the r
a0640 65 73 74 20 68 61 76 65 20 74 68 65 20 46 4c 41  est have the FLA
a0650 47 5f 49 4e 54 45 52 4e 20 62 69 74 20 73 65 74  G_INTERN bit set
a0660 20 61 6e 64 20 61 72 65 20 74 68 75 73 20 66 6f   and are thus fo
a0670 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73  r internal.** us
a0680 65 20 6f 6e 6c 79 20 2a 2f 0a 20 20 7b 20 20 27  e only */.  {  '
a0690 54 27 2c 20 20 30 2c 20 32 2c 20 65 74 54 4f 4b  T',  0, 2, etTOK
a06a0 45 4e 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d  EN,      0,  0 }
a06b0 2c 0a 20 20 7b 20 20 27 53 27 2c 20 20 30 2c 20  ,.  {  'S',  0, 
a06c0 32 2c 20 65 74 53 52 43 4c 49 53 54 2c 20 20 20  2, etSRCLIST,   
a06d0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
a06e0 72 27 2c 20 31 30 2c 20 33 2c 20 65 74 4f 52 44  r', 10, 3, etORD
a06f0 49 4e 41 4c 2c 20 20 20 20 30 2c 20 20 30 20 7d  INAL,    0,  0 }
a0700 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53  ,.};../*.** If S
a0710 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
a0720 49 4e 47 5f 50 4f 49 4e 54 20 69 73 20 64 65 66  ING_POINT is def
a0730 69 6e 65 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20  ined, then none 
a0740 6f 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20  of the floating 
a0750 70 6f 69 6e 74 0a 2a 2a 20 63 6f 6e 76 65 72 73  point.** convers
a0760 69 6f 6e 73 20 77 69 6c 6c 20 77 6f 72 6b 2e 0a  ions will work..
a0770 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
a0780 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
a0790 50 4f 49 4e 54 0a 2f 2a 0a 2a 2a 20 22 2a 76 61  POINT./*.** "*va
a07a0 6c 22 20 69 73 20 61 20 64 6f 75 62 6c 65 20 73  l" is a double s
a07b0 75 63 68 20 74 68 61 74 20 30 2e 31 20 3c 3d 20  uch that 0.1 <= 
a07c0 2a 76 61 6c 20 3c 20 31 30 2e 30 0a 2a 2a 20 52  *val < 10.0.** R
a07d0 65 74 75 72 6e 20 74 68 65 20 61 73 63 69 69 20  eturn the ascii 
a07e0 63 6f 64 65 20 66 6f 72 20 74 68 65 20 6c 65 61  code for the lea
a07f0 64 69 6e 67 20 64 69 67 69 74 20 6f 66 20 2a 76  ding digit of *v
a0800 61 6c 2c 20 74 68 65 6e 0a 2a 2a 20 6d 75 6c 74  al, then.** mult
a0810 69 70 6c 79 20 22 2a 76 61 6c 22 20 62 79 20 31  iply "*val" by 1
a0820 30 2e 30 20 74 6f 20 72 65 6e 6f 72 6d 61 6c 69  0.0 to renormali
a0830 7a 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c  ze..**.** Exampl
a0840 65 3a 0a 2a 2a 20 20 20 20 20 69 6e 70 75 74 3a  e:.**     input:
a0850 20 20 20 20 20 2a 76 61 6c 20 3d 20 33 2e 31 34       *val = 3.14
a0860 31 35 39 0a 2a 2a 20 20 20 20 20 6f 75 74 70 75  159.**     outpu
a0870 74 3a 20 20 20 20 2a 76 61 6c 20 3d 20 31 2e 34  t:    *val = 1.4
a0880 31 35 39 20 20 20 20 66 75 6e 63 74 69 6f 6e 20  159    function 
a0890 72 65 74 75 72 6e 20 3d 20 27 33 27 0a 2a 2a 0a  return = '3'.**.
a08a0 2a 2a 20 54 68 65 20 63 6f 75 6e 74 65 72 20 2a  ** The counter *
a08b0 63 6e 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74  cnt is increment
a08c0 65 64 20 65 61 63 68 20 74 69 6d 65 2e 20 20 41  ed each time.  A
a08d0 66 74 65 72 20 63 6f 75 6e 74 65 72 20 65 78 63  fter counter exc
a08e0 65 65 64 73 0a 2a 2a 20 31 36 20 28 74 68 65 20  eeds.** 16 (the 
a08f0 6e 75 6d 62 65 72 20 6f 66 20 73 69 67 6e 69 66  number of signif
a0900 69 63 61 6e 74 20 64 69 67 69 74 73 20 69 6e 20  icant digits in 
a0910 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 29 20  a 64-bit float) 
a0920 27 30 27 20 69 73 0a 2a 2a 20 61 6c 77 61 79 73  '0' is.** always
a0930 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74   returned..*/.st
a0940 61 74 69 63 20 63 68 61 72 20 65 74 5f 67 65 74  atic char et_get
a0950 64 69 67 69 74 28 4c 4f 4e 47 44 4f 55 42 4c 45  digit(LONGDOUBLE
a0960 5f 54 59 50 45 20 2a 76 61 6c 2c 20 69 6e 74 20  _TYPE *val, int 
a0970 2a 63 6e 74 29 7b 0a 20 20 69 6e 74 20 64 69 67  *cnt){.  int dig
a0980 69 74 3b 0a 20 20 4c 4f 4e 47 44 4f 55 42 4c 45  it;.  LONGDOUBLE
a0990 5f 54 59 50 45 20 64 3b 0a 20 20 69 66 28 20 28  _TYPE d;.  if( (
a09a0 2a 63 6e 74 29 2b 2b 20 3e 3d 20 31 36 20 29 20  *cnt)++ >= 16 ) 
a09b0 72 65 74 75 72 6e 20 27 30 27 3b 0a 20 20 64 69  return '0';.  di
a09c0 67 69 74 20 3d 20 28 69 6e 74 29 2a 76 61 6c 3b  git = (int)*val;
a09d0 0a 20 20 64 20 3d 20 64 69 67 69 74 3b 0a 20 20  .  d = digit;.  
a09e0 64 69 67 69 74 20 2b 3d 20 27 30 27 3b 0a 20 20  digit += '0';.  
a09f0 2a 76 61 6c 20 3d 20 28 2a 76 61 6c 20 2d 20 64  *val = (*val - d
a0a00 29 2a 31 30 2e 30 3b 0a 20 20 72 65 74 75 72 6e  )*10.0;.  return
a0a10 20 28 63 68 61 72 29 64 69 67 69 74 3b 0a 7d 0a   (char)digit;.}.
a0a20 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
a0a30 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
a0a40 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  OINT */../*.** A
a0a50 70 70 65 6e 64 20 4e 20 73 70 61 63 65 20 63 68  ppend N space ch
a0a60 61 72 61 63 74 65 72 73 20 74 6f 20 74 68 65 20  aracters to the 
a0a70 67 69 76 65 6e 20 73 74 72 69 6e 67 20 62 75 66  given string buf
a0a80 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  fer..*/.static v
a0a90 6f 69 64 20 61 70 70 65 6e 64 53 70 61 63 65 28  oid appendSpace(
a0aa0 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d  StrAccum *pAccum
a0ab0 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 73 74 61 74  , int N){.  stat
a0ac0 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 53  ic const char zS
a0ad0 70 61 63 65 73 5b 5d 20 3d 20 22 20 20 20 20 20  paces[] = "     
a0ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0af0 20 20 20 20 20 20 20 20 22 3b 0a 20 20 77 68 69          ";.  whi
a0b00 6c 65 28 20 4e 3e 3d 28 69 6e 74 29 73 69 7a 65  le( N>=(int)size
a0b10 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 20 29 7b  of(zSpaces)-1 ){
a0b20 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41  .    sqlite3StrA
a0b30 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75  ccumAppend(pAccu
a0b40 6d 2c 20 7a 53 70 61 63 65 73 2c 20 73 69 7a 65  m, zSpaces, size
a0b50 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 29 3b 0a  of(zSpaces)-1);.
a0b60 20 20 20 20 4e 20 2d 3d 20 73 69 7a 65 6f 66 28      N -= sizeof(
a0b70 7a 53 70 61 63 65 73 29 2d 31 3b 0a 20 20 7d 0a  zSpaces)-1;.  }.
a0b80 20 20 69 66 28 20 4e 3e 30 20 29 7b 0a 20 20 20    if( N>0 ){.   
a0b90 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
a0ba0 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a  Append(pAccum, z
a0bb0 53 70 61 63 65 73 2c 20 4e 29 3b 0a 20 20 7d 0a  Spaces, N);.  }.
a0bc0 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 6d 61 63 68  }../*.** On mach
a0bd0 69 6e 65 73 20 77 69 74 68 20 61 20 73 6d 61 6c  ines with a smal
a0be0 6c 20 73 74 61 63 6b 20 73 69 7a 65 2c 20 79 6f  l stack size, yo
a0bf0 75 20 63 61 6e 20 72 65 64 65 66 69 6e 65 20 74  u can redefine t
a0c00 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 50 52 49  he.** SQLITE_PRI
a0c10 4e 54 5f 42 55 46 5f 53 49 5a 45 20 74 6f 20 62  NT_BUF_SIZE to b
a0c20 65 20 6c 65 73 73 20 74 68 61 6e 20 33 35 30 2e  e less than 350.
a0c30 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
a0c40 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a  TE_PRINT_BUF_SIZ
a0c50 45 0a 23 20 69 66 20 64 65 66 69 6e 65 64 28 53  E.# if defined(S
a0c60 51 4c 49 54 45 5f 53 4d 41 4c 4c 5f 53 54 41 43  QLITE_SMALL_STAC
a0c70 4b 29 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51  K).#   define SQ
a0c80 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53  LITE_PRINT_BUF_S
a0c90 49 5a 45 20 35 30 0a 23 20 65 6c 73 65 0a 23 20  IZE 50.# else.# 
a0ca0 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
a0cb0 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 33  PRINT_BUF_SIZE 3
a0cc0 35 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69  50.# endif.#endi
a0cd0 66 0a 23 64 65 66 69 6e 65 20 65 74 42 55 46 53  f.#define etBUFS
a0ce0 49 5a 45 20 53 51 4c 49 54 45 5f 50 52 49 4e 54  IZE SQLITE_PRINT
a0cf0 5f 42 55 46 5f 53 49 5a 45 20 20 2f 2a 20 53 69  _BUF_SIZE  /* Si
a0d00 7a 65 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74  ze of the output
a0d10 20 62 75 66 66 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a   buffer */../*.*
a0d20 2a 20 54 68 65 20 72 6f 6f 74 20 70 72 6f 67 72  * The root progr
a0d30 61 6d 2e 20 20 41 6c 6c 20 76 61 72 69 61 74 69  am.  All variati
a0d40 6f 6e 73 20 63 61 6c 6c 20 74 68 69 73 20 63 6f  ons call this co
a0d50 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e 50 55 54 53  re..**.** INPUTS
a0d60 3a 0a 2a 2a 20 20 20 66 75 6e 63 20 20 20 54 68  :.**   func   Th
a0d70 69 73 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  is is a pointer 
a0d80 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 61  to a function ta
a0d90 6b 69 6e 67 20 74 68 72 65 65 20 61 72 67 75 6d  king three argum
a0da0 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ents.**         
a0db0 20 20 20 31 2e 20 41 20 70 6f 69 6e 74 65 72 20     1. A pointer 
a0dc0 74 6f 20 61 6e 79 74 68 69 6e 67 2e 20 20 53 61  to anything.  Sa
a0dd0 6d 65 20 61 73 20 74 68 65 20 22 61 72 67 22 20  me as the "arg" 
a0de0 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 20 20  parameter..**   
a0df0 20 20 20 20 20 20 20 20 20 32 2e 20 41 20 70 6f           2. A po
a0e00 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6c 69 73  inter to the lis
a0e10 74 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  t of characters 
a0e20 74 6f 20 62 65 20 6f 75 74 70 75 74 0a 2a 2a 20  to be output.** 
a0e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 4e                (N
a0e40 6f 74 65 2c 20 74 68 69 73 20 6c 69 73 74 20 69  ote, this list i
a0e50 73 20 4e 4f 54 20 6e 75 6c 6c 20 74 65 72 6d 69  s NOT null termi
a0e60 6e 61 74 65 64 2e 29 0a 2a 2a 20 20 20 20 20 20  nated.).**      
a0e70 20 20 20 20 20 20 33 2e 20 41 6e 20 69 6e 74 65        3. An inte
a0e80 67 65 72 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  ger number of ch
a0e90 61 72 61 63 74 65 72 73 20 74 6f 20 62 65 20 6f  aracters to be o
a0ea0 75 74 70 75 74 2e 0a 2a 2a 20 20 20 20 20 20 20  utput..**       
a0eb0 20 20 20 20 20 20 20 20 28 4e 6f 74 65 3a 20 54          (Note: T
a0ec0 68 69 73 20 6e 75 6d 62 65 72 20 6d 69 67 68 74  his number might
a0ed0 20 62 65 20 7a 65 72 6f 2e 29 0a 2a 2a 0a 2a 2a   be zero.).**.**
a0ee0 20 20 20 61 72 67 20 20 20 20 54 68 69 73 20 69     arg    This i
a0ef0 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f  s the pointer to
a0f00 20 61 6e 79 74 68 69 6e 67 20 77 68 69 63 68 20   anything which 
a0f10 77 69 6c 6c 20 62 65 20 70 61 73 73 65 64 20 61  will be passed a
a0f20 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  s the.**        
a0f30 20 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74    first argument
a0f40 20 74 6f 20 22 66 75 6e 63 22 2e 20 20 55 73 65   to "func".  Use
a0f50 20 69 74 20 66 6f 72 20 77 68 61 74 65 76 65 72   it for whatever
a0f60 20 79 6f 75 20 6c 69 6b 65 2e 0a 2a 2a 0a 2a 2a   you like..**.**
a0f70 20 20 20 66 6d 74 20 20 20 20 54 68 69 73 20 69     fmt    This i
a0f80 73 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72  s the format str
a0f90 69 6e 67 2c 20 61 73 20 69 6e 20 74 68 65 20 75  ing, as in the u
a0fa0 73 75 61 6c 20 70 72 69 6e 74 2e 0a 2a 2a 0a 2a  sual print..**.*
a0fb0 2a 20 20 20 61 70 20 20 20 20 20 54 68 69 73 20  *   ap     This 
a0fc0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
a0fd0 61 20 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65  a list of argume
a0fe0 6e 74 73 2e 20 20 53 61 6d 65 20 61 73 20 69 6e  nts.  Same as in
a0ff0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 66 70  .**          vfp
a1000 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 55 54 50  rint..**.** OUTP
a1010 55 54 53 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20  UTS:.**         
a1020 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
a1030 65 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e  e is the total n
a1040 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
a1050 65 72 73 20 73 65 6e 74 20 74 6f 0a 2a 2a 20 20  ers sent to.**  
a1060 20 20 20 20 20 20 20 20 74 68 65 20 66 75 6e 63          the func
a1070 74 69 6f 6e 20 22 66 75 6e 63 22 2e 20 20 52 65  tion "func".  Re
a1080 74 75 72 6e 73 20 2d 31 20 6f 6e 20 61 20 65 72  turns -1 on a er
a1090 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  ror..**.** Note 
a10a0 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 69  that the order i
a10b0 6e 20 77 68 69 63 68 20 61 75 74 6f 6d 61 74 69  n which automati
a10c0 63 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20  c variables are 
a10d0 64 65 63 6c 61 72 65 64 20 62 65 6c 6f 77 0a 2a  declared below.*
a10e0 2a 20 73 65 65 6d 73 20 74 6f 20 6d 61 6b 65 20  * seems to make 
a10f0 61 20 62 69 67 20 64 69 66 66 65 72 65 6e 63 65  a big difference
a1100 20 69 6e 20 64 65 74 65 72 6d 69 6e 69 6e 67 20   in determining 
a1110 68 6f 77 20 66 61 73 74 20 74 68 69 73 20 62 65  how fast this be
a1120 61 73 74 0a 2a 2a 20 77 69 6c 6c 20 72 75 6e 2e  ast.** will run.
a1130 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a1140 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
a1150 58 50 72 69 6e 74 66 28 0a 20 20 53 74 72 41 63  XPrintf(.  StrAc
a1160 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20 20 20 20  cum *pAccum,    
a1170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a1180 20 41 63 63 75 6d 75 6c 61 74 65 20 72 65 73 75   Accumulate resu
a1190 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e  lts here */.  in
a11a0 74 20 75 73 65 45 78 74 65 6e 64 65 64 2c 20 20  t useExtended,  
a11b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a11c0 20 2f 2a 20 41 6c 6c 6f 77 20 65 78 74 65 6e 64   /* Allow extend
a11d0 65 64 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 73  ed %-conversions
a11e0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
a11f0 20 2a 66 6d 74 2c 20 20 20 20 20 20 20 20 20 20   *fmt,          
a1200 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 6d           /* Form
a1210 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 76  at string */.  v
a1220 61 5f 6c 69 73 74 20 61 70 20 20 20 20 20 20 20  a_list ap       
a1230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1240 20 20 2f 2a 20 61 72 67 75 6d 65 6e 74 73 20 2a    /* arguments *
a1250 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 20 20 20  /.){.  int c;   
a1260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1270 20 20 2f 2a 20 4e 65 78 74 20 63 68 61 72 61 63    /* Next charac
a1280 74 65 72 20 69 6e 20 74 68 65 20 66 6f 72 6d 61  ter in the forma
a1290 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 68  t string */.  ch
a12a0 61 72 20 2a 62 75 66 70 74 3b 20 20 20 20 20 20  ar *bufpt;      
a12b0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
a12c0 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76 65  ter to the conve
a12d0 72 73 69 6f 6e 20 62 75 66 66 65 72 20 2a 2f 0a  rsion buffer */.
a12e0 20 20 69 6e 74 20 70 72 65 63 69 73 69 6f 6e 3b    int precision;
a12f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a1300 50 72 65 63 69 73 69 6f 6e 20 6f 66 20 74 68 65  Precision of the
a1310 20 63 75 72 72 65 6e 74 20 66 69 65 6c 64 20 2a   current field *
a1320 2f 0a 20 20 69 6e 74 20 6c 65 6e 67 74 68 3b 20  /.  int length; 
a1330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a1340 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  * Length of the 
a1350 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 69  field */.  int i
a1360 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
a1370 20 20 20 20 20 20 2f 2a 20 41 20 67 65 6e 65 72        /* A gener
a1380 61 6c 20 70 75 72 70 6f 73 65 20 6c 6f 6f 70 20  al purpose loop 
a1390 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  counter */.  int
a13a0 20 77 69 64 74 68 3b 20 20 20 20 20 20 20 20 20   width;         
a13b0 20 20 20 20 20 20 20 20 2f 2a 20 57 69 64 74 68          /* Width
a13c0 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   of the current 
a13d0 66 69 65 6c 64 20 2a 2f 0a 20 20 65 74 42 79 74  field */.  etByt
a13e0 65 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69  e flag_leftjusti
a13f0 66 79 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66  fy;   /* True if
a1400 20 22 2d 22 20 66 6c 61 67 20 69 73 20 70 72 65   "-" flag is pre
a1410 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65  sent */.  etByte
a1420 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 3b 20   flag_plussign; 
a1430 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a1440 22 2b 22 20 66 6c 61 67 20 69 73 20 70 72 65 73  "+" flag is pres
a1450 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20  ent */.  etByte 
a1460 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 3b 20  flag_blanksign; 
a1470 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22      /* True if "
a1480 20 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65   " flag is prese
a1490 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66  nt */.  etByte f
a14a0 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72  lag_alternatefor
a14b0 6d 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 22 23  m; /* True if "#
a14c0 22 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e  " flag is presen
a14d0 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  t */.  etByte fl
a14e0 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 20 20 20 20  ag_altform2;    
a14f0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 21 22    /* True if "!"
a1500 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74   flag is present
a1510 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61   */.  etByte fla
a1520 67 5f 7a 65 72 6f 70 61 64 3b 20 20 20 20 20 20  g_zeropad;      
a1530 20 2f 2a 20 54 72 75 65 20 69 66 20 66 69 65 6c   /* True if fiel
a1540 64 20 77 69 64 74 68 20 63 6f 6e 73 74 61 6e 74  d width constant
a1550 20 73 74 61 72 74 73 20 77 69 74 68 20 7a 65 72   starts with zer
a1560 6f 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  o */.  etByte fl
a1570 61 67 5f 6c 6f 6e 67 3b 20 20 20 20 20 20 20 20  ag_long;        
a1580 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 6c 22    /* True if "l"
a1590 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74   flag is present
a15a0 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61   */.  etByte fla
a15b0 67 5f 6c 6f 6e 67 6c 6f 6e 67 3b 20 20 20 20 20  g_longlong;     
a15c0 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
a15d0 22 6c 6c 22 20 66 6c 61 67 20 69 73 20 70 72 65  "ll" flag is pre
a15e0 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65  sent */.  etByte
a15f0 20 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20   done;          
a1600 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 65 72       /* Loop ter
a1610 6d 69 6e 61 74 69 6f 6e 20 66 6c 61 67 20 2a 2f  mination flag */
a1620 0a 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  .  sqlite_uint64
a1630 20 6c 6f 6e 67 76 61 6c 75 65 3b 20 20 20 2f 2a   longvalue;   /*
a1640 20 56 61 6c 75 65 20 66 6f 72 20 69 6e 74 65 67   Value for integ
a1650 65 72 20 74 79 70 65 73 20 2a 2f 0a 20 20 4c 4f  er types */.  LO
a1660 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 72 65  NGDOUBLE_TYPE re
a1670 61 6c 76 61 6c 75 65 3b 20 2f 2a 20 56 61 6c 75  alvalue; /* Valu
a1680 65 20 66 6f 72 20 72 65 61 6c 20 74 79 70 65 73  e for real types
a1690 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 65 74 5f 69   */.  const et_i
a16a0 6e 66 6f 20 2a 69 6e 66 6f 70 3b 20 20 20 20 20  nfo *infop;     
a16b0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74   /* Pointer to t
a16c0 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 69  he appropriate i
a16d0 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f  nfo structure */
a16e0 0a 20 20 63 68 61 72 20 62 75 66 5b 65 74 42 55  .  char buf[etBU
a16f0 46 53 49 5a 45 5d 3b 20 20 20 20 20 20 20 2f 2a  FSIZE];       /*
a1700 20 43 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66 66   Conversion buff
a1710 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 70 72 65  er */.  char pre
a1720 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  fix;            
a1730 20 20 20 2f 2a 20 50 72 65 66 69 78 20 63 68 61     /* Prefix cha
a1740 72 61 63 74 65 72 2e 20 20 22 2b 22 20 6f 72 20  racter.  "+" or 
a1750 22 2d 22 20 6f 72 20 22 20 22 20 6f 72 20 27 5c  "-" or " " or '\
a1760 30 27 2e 20 2a 2f 0a 20 20 65 74 42 79 74 65 20  0'. */.  etByte 
a1770 78 74 79 70 65 20 3d 20 30 3b 20 20 20 20 20 20  xtype = 0;      
a1780 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f      /* Conversio
a1790 6e 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20  n paradigm */.  
a17a0 63 68 61 72 20 2a 7a 45 78 74 72 61 3b 20 20 20  char *zExtra;   
a17b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78             /* Ex
a17c0 74 72 61 20 6d 65 6d 6f 72 79 20 75 73 65 64 20  tra memory used 
a17d0 66 6f 72 20 65 74 54 43 4c 45 53 43 41 50 45 20  for etTCLESCAPE 
a17e0 63 6f 6e 76 65 72 73 69 6f 6e 73 20 2a 2f 0a 23  conversions */.#
a17f0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
a1800 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
a1810 54 0a 20 20 69 6e 74 20 20 65 78 70 2c 20 65 32  T.  int  exp, e2
a1820 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
a1830 2a 20 65 78 70 6f 6e 65 6e 74 20 6f 66 20 72 65  * exponent of re
a1840 61 6c 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20  al numbers */.  
a1850 64 6f 75 62 6c 65 20 72 6f 75 6e 64 65 72 3b 20  double rounder; 
a1860 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
a1870 65 64 20 66 6f 72 20 72 6f 75 6e 64 69 6e 67 20  ed for rounding 
a1880 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76  floating point v
a1890 61 6c 75 65 73 20 2a 2f 0a 20 20 65 74 42 79 74  alues */.  etByt
a18a0 65 20 66 6c 61 67 5f 64 70 3b 20 20 20 20 20 20  e flag_dp;      
a18b0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
a18c0 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 73   decimal point s
a18d0 68 6f 75 6c 64 20 62 65 20 73 68 6f 77 6e 20 2a  hould be shown *
a18e0 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f  /.  etByte flag_
a18f0 72 74 7a 3b 20 20 20 20 20 20 20 20 20 20 20 2f  rtz;           /
a1900 2a 20 54 72 75 65 20 69 66 20 74 72 61 69 6c 69  * True if traili
a1910 6e 67 20 7a 65 72 6f 73 20 73 68 6f 75 6c 64 20  ng zeros should 
a1920 62 65 20 72 65 6d 6f 76 65 64 20 2a 2f 0a 20 20  be removed */.  
a1930 65 74 42 79 74 65 20 66 6c 61 67 5f 65 78 70 3b  etByte flag_exp;
a1940 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
a1950 75 65 20 74 6f 20 66 6f 72 63 65 20 64 69 73 70  ue to force disp
a1960 6c 61 79 20 6f 66 20 74 68 65 20 65 78 70 6f 6e  lay of the expon
a1970 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 73 64  ent */.  int nsd
a1980 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a1990 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
a19a0 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67   significant dig
a19b0 69 74 73 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a  its returned */.
a19c0 23 65 6e 64 69 66 0a 0a 20 20 6c 65 6e 67 74 68  #endif..  length
a19d0 20 3d 20 30 3b 0a 20 20 62 75 66 70 74 20 3d 20   = 0;.  bufpt = 
a19e0 30 3b 0a 20 20 66 6f 72 28 3b 20 28 63 3d 28 2a  0;.  for(; (c=(*
a19f0 66 6d 74 29 29 21 3d 30 3b 20 2b 2b 66 6d 74 29  fmt))!=0; ++fmt)
a1a00 7b 0a 20 20 20 20 69 66 28 20 63 21 3d 27 25 27  {.    if( c!='%'
a1a10 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 6d   ){.      int am
a1a20 74 3b 0a 20 20 20 20 20 20 62 75 66 70 74 20 3d  t;.      bufpt =
a1a30 20 28 63 68 61 72 20 2a 29 66 6d 74 3b 0a 20 20   (char *)fmt;.  
a1a40 20 20 20 20 61 6d 74 20 3d 20 31 3b 0a 20 20 20      amt = 1;.   
a1a50 20 20 20 77 68 69 6c 65 28 20 28 63 3d 28 2a 2b     while( (c=(*+
a1a60 2b 66 6d 74 29 29 21 3d 27 25 27 20 26 26 20 63  +fmt))!='%' && c
a1a70 21 3d 30 20 29 20 61 6d 74 2b 2b 3b 0a 20 20 20  !=0 ) amt++;.   
a1a80 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
a1a90 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c  umAppend(pAccum,
a1aa0 20 62 75 66 70 74 2c 20 61 6d 74 29 3b 0a 20 20   bufpt, amt);.  
a1ab0 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 20 62      if( c==0 ) b
a1ac0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
a1ad0 69 66 28 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29  if( (c=(*++fmt))
a1ae0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
a1af0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
a1b00 6e 64 28 70 41 63 63 75 6d 2c 20 22 25 22 2c 20  nd(pAccum, "%", 
a1b10 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  1);.      break;
a1b20 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 69  .    }.    /* Fi
a1b30 6e 64 20 6f 75 74 20 77 68 61 74 20 66 6c 61 67  nd out what flag
a1b40 73 20 61 72 65 20 70 72 65 73 65 6e 74 20 2a 2f  s are present */
a1b50 0a 20 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75  .    flag_leftju
a1b60 73 74 69 66 79 20 3d 20 66 6c 61 67 5f 70 6c 75  stify = flag_plu
a1b70 73 73 69 67 6e 20 3d 20 66 6c 61 67 5f 62 6c 61  ssign = flag_bla
a1b80 6e 6b 73 69 67 6e 20 3d 20 0a 20 20 20 20 20 66  nksign = .     f
a1b90 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72  lag_alternatefor
a1ba0 6d 20 3d 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d  m = flag_altform
a1bb0 32 20 3d 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64  2 = flag_zeropad
a1bc0 20 3d 20 30 3b 0a 20 20 20 20 64 6f 6e 65 20 3d   = 0;.    done =
a1bd0 20 30 3b 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20   0;.    do{.    
a1be0 20 20 73 77 69 74 63 68 28 20 63 20 29 7b 0a 20    switch( c ){. 
a1bf0 20 20 20 20 20 20 20 63 61 73 65 20 27 2d 27 3a         case '-':
a1c00 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74     flag_leftjust
a1c10 69 66 79 20 3d 20 31 3b 20 20 20 20 20 62 72 65  ify = 1;     bre
a1c20 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
a1c30 20 27 2b 27 3a 20 20 20 66 6c 61 67 5f 70 6c 75   '+':   flag_plu
a1c40 73 73 69 67 6e 20 3d 20 31 3b 20 20 20 20 20 20  ssign = 1;      
a1c50 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
a1c60 20 63 61 73 65 20 27 20 27 3a 20 20 20 66 6c 61   case ' ':   fla
a1c70 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 31 3b  g_blanksign = 1;
a1c80 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
a1c90 20 20 20 20 20 20 63 61 73 65 20 27 23 27 3a 20        case '#': 
a1ca0 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65    flag_alternate
a1cb0 66 6f 72 6d 20 3d 20 31 3b 20 20 20 62 72 65 61  form = 1;   brea
a1cc0 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
a1cd0 27 21 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74 66  '!':   flag_altf
a1ce0 6f 72 6d 32 20 3d 20 31 3b 20 20 20 20 20 20 20  orm2 = 1;       
a1cf0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
a1d00 63 61 73 65 20 27 30 27 3a 20 20 20 66 6c 61 67  case '0':   flag
a1d10 5f 7a 65 72 6f 70 61 64 20 3d 20 31 3b 20 20 20  _zeropad = 1;   
a1d20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
a1d30 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20       default:   
a1d40 20 64 6f 6e 65 20 3d 20 31 3b 20 20 20 20 20 20   done = 1;      
a1d50 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
a1d60 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 77  ;.      }.    }w
a1d70 68 69 6c 65 28 20 21 64 6f 6e 65 20 26 26 20 28  hile( !done && (
a1d80 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 30 20 29  c=(*++fmt))!=0 )
a1d90 3b 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68 65  ;.    /* Get the
a1da0 20 66 69 65 6c 64 20 77 69 64 74 68 20 2a 2f 0a   field width */.
a1db0 20 20 20 20 77 69 64 74 68 20 3d 20 30 3b 0a 20      width = 0;. 
a1dc0 20 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b     if( c=='*' ){
a1dd0 0a 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 76  .      width = v
a1de0 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20  a_arg(ap,int);. 
a1df0 20 20 20 20 20 69 66 28 20 77 69 64 74 68 3c 30       if( width<0
a1e00 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67   ){.        flag
a1e10 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 3d 20 31  _leftjustify = 1
a1e20 3b 0a 20 20 20 20 20 20 20 20 77 69 64 74 68 20  ;.        width 
a1e30 3d 20 2d 77 69 64 74 68 3b 0a 20 20 20 20 20 20  = -width;.      
a1e40 7d 0a 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66  }.      c = *++f
a1e50 6d 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  mt;.    }else{. 
a1e60 20 20 20 20 20 77 68 69 6c 65 28 20 63 3e 3d 27       while( c>='
a1e70 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a  0' && c<='9' ){.
a1e80 20 20 20 20 20 20 20 20 77 69 64 74 68 20 3d 20          width = 
a1e90 77 69 64 74 68 2a 31 30 20 2b 20 63 20 2d 20 27  width*10 + c - '
a1ea0 30 27 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20  0';.        c = 
a1eb0 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 0a  *++fmt;.      }.
a1ec0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 77 69      }.    if( wi
a1ed0 64 74 68 20 3e 20 65 74 42 55 46 53 49 5a 45 2d  dth > etBUFSIZE-
a1ee0 31 30 20 29 7b 0a 20 20 20 20 20 20 77 69 64 74  10 ){.      widt
a1ef0 68 20 3d 20 65 74 42 55 46 53 49 5a 45 2d 31 30  h = etBUFSIZE-10
a1f00 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47  ;.    }.    /* G
a1f10 65 74 20 74 68 65 20 70 72 65 63 69 73 69 6f 6e  et the precision
a1f20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 27   */.    if( c=='
a1f30 2e 27 20 29 7b 0a 20 20 20 20 20 20 70 72 65 63  .' ){.      prec
a1f40 69 73 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 20  ision = 0;.     
a1f50 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20   c = *++fmt;.   
a1f60 20 20 20 69 66 28 20 63 3d 3d 27 2a 27 20 29 7b     if( c=='*' ){
a1f70 0a 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69  .        precisi
a1f80 6f 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69  on = va_arg(ap,i
a1f90 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  nt);.        if(
a1fa0 20 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 70   precision<0 ) p
a1fb0 72 65 63 69 73 69 6f 6e 20 3d 20 2d 70 72 65 63  recision = -prec
a1fc0 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 63  ision;.        c
a1fd0 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20   = *++fmt;.     
a1fe0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
a1ff0 77 68 69 6c 65 28 20 63 3e 3d 27 30 27 20 26 26  while( c>='0' &&
a2000 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20 20   c<='9' ){.     
a2010 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d       precision =
a2020 20 70 72 65 63 69 73 69 6f 6e 2a 31 30 20 2b 20   precision*10 + 
a2030 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20  c - '0';.       
a2040 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20     c = *++fmt;. 
a2050 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
a2060 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
a2070 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d 31    precision = -1
a2080 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 47  ;.    }.    /* G
a2090 65 74 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f  et the conversio
a20a0 6e 20 74 79 70 65 20 6d 6f 64 69 66 69 65 72 20  n type modifier 
a20b0 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 6c  */.    if( c=='l
a20c0 27 20 29 7b 0a 20 20 20 20 20 20 66 6c 61 67 5f  ' ){.      flag_
a20d0 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20  long = 1;.      
a20e0 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20  c = *++fmt;.    
a20f0 20 20 69 66 28 20 63 3d 3d 27 6c 27 20 29 7b 0a    if( c=='l' ){.
a2100 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e          flag_lon
a2110 67 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20  glong = 1;.     
a2120 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20     c = *++fmt;. 
a2130 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a2140 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e      flag_longlon
a2150 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  g = 0;.      }. 
a2160 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a2170 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 66 6c 61 67  flag_long = flag
a2180 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20  _longlong = 0;. 
a2190 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46 65 74 63     }.    /* Fetc
a21a0 68 20 74 68 65 20 69 6e 66 6f 20 65 6e 74 72 79  h the info entry
a21b0 20 66 6f 72 20 74 68 65 20 66 69 65 6c 64 20 2a   for the field *
a21c0 2f 0a 20 20 20 20 69 6e 66 6f 70 20 3d 20 26 66  /.    infop = &f
a21d0 6d 74 69 6e 66 6f 5b 30 5d 3b 0a 20 20 20 20 78  mtinfo[0];.    x
a21e0 74 79 70 65 20 3d 20 65 74 49 4e 56 41 4c 49 44  type = etINVALID
a21f0 3b 0a 20 20 20 20 66 6f 72 28 69 64 78 3d 30 3b  ;.    for(idx=0;
a2200 20 69 64 78 3c 41 72 72 61 79 53 69 7a 65 28 66   idx<ArraySize(f
a2210 6d 74 69 6e 66 6f 29 3b 20 69 64 78 2b 2b 29 7b  mtinfo); idx++){
a2220 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 66 6d  .      if( c==fm
a2230 74 69 6e 66 6f 5b 69 64 78 5d 2e 66 6d 74 74 79  tinfo[idx].fmtty
a2240 70 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  pe ){.        in
a2250 66 6f 70 20 3d 20 26 66 6d 74 69 6e 66 6f 5b 69  fop = &fmtinfo[i
a2260 64 78 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28  dx];.        if(
a2270 20 75 73 65 45 78 74 65 6e 64 65 64 20 7c 7c 20   useExtended || 
a2280 28 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20  (infop->flags & 
a2290 46 4c 41 47 5f 49 4e 54 45 52 4e 29 3d 3d 30 20  FLAG_INTERN)==0 
a22a0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 78 74 79  ){.          xty
a22b0 70 65 20 3d 20 69 6e 66 6f 70 2d 3e 74 79 70 65  pe = infop->type
a22c0 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
a22d0 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
a22e0 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  n;.        }.   
a22f0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
a2300 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 45    }.    }.    zE
a2310 78 74 72 61 20 3d 20 30 3b 0a 0a 0a 20 20 20 20  xtra = 0;...    
a2320 2f 2a 20 4c 69 6d 69 74 20 74 68 65 20 70 72 65  /* Limit the pre
a2330 63 69 73 69 6f 6e 20 74 6f 20 70 72 65 76 65 6e  cision to preven
a2340 74 20 6f 76 65 72 66 6c 6f 77 69 6e 67 20 62 75  t overflowing bu
a2350 66 5b 5d 20 64 75 72 69 6e 67 20 63 6f 6e 76 65  f[] during conve
a2360 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28  rsion */.    if(
a2370 20 70 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 46   precision>etBUF
a2380 53 49 5a 45 2d 34 30 20 26 26 20 28 69 6e 66 6f  SIZE-40 && (info
a2390 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f  p->flags & FLAG_
a23a0 53 54 52 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 20  STRING)==0 ){.  
a23b0 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20      precision = 
a23c0 65 74 42 55 46 53 49 5a 45 2d 34 30 3b 0a 20 20  etBUFSIZE-40;.  
a23d0 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20    }..    /*.    
a23e0 2a 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  ** At this point
a23f0 2c 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20  , variables are 
a2400 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 73 20 66  initialized as f
a2410 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a 0a 20  ollows:.    **. 
a2420 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74     **   flag_alt
a2430 65 72 6e 61 74 65 66 6f 72 6d 20 20 20 20 20 20  ernateform      
a2440 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27 23      TRUE if a '#
a2450 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20  ' is present..  
a2460 20 20 2a 2a 20 20 20 66 6c 61 67 5f 61 6c 74 66    **   flag_altf
a2470 6f 72 6d 32 20 20 20 20 20 20 20 20 20 20 20 20  orm2            
a2480 20 20 20 54 52 55 45 20 69 66 20 61 20 27 21 27     TRUE if a '!'
a2490 20 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20   is present..   
a24a0 20 2a 2a 20 20 20 66 6c 61 67 5f 70 6c 75 73 73   **   flag_pluss
a24b0 69 67 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ign             
a24c0 20 20 54 52 55 45 20 69 66 20 61 20 27 2b 27 20    TRUE if a '+' 
a24d0 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20  is present..    
a24e0 2a 2a 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a 75  **   flag_leftju
a24f0 73 74 69 66 79 20 20 20 20 20 20 20 20 20 20 20  stify           
a2500 20 54 52 55 45 20 69 66 20 61 20 27 2d 27 20 69   TRUE if a '-' i
a2510 73 20 70 72 65 73 65 6e 74 20 6f 72 20 69 66 20  s present or if 
a2520 74 68 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  the.    **      
a2530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2540 20 20 20 20 20 20 20 20 20 66 69 65 6c 64 20 77           field w
a2550 69 64 74 68 20 77 61 73 20 6e 65 67 61 74 69 76  idth was negativ
a2560 65 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67  e..    **   flag
a2570 5f 7a 65 72 6f 70 61 64 20 20 20 20 20 20 20 20  _zeropad        
a2580 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20          TRUE if 
a2590 74 68 65 20 77 69 64 74 68 20 62 65 67 61 6e 20  the width began 
a25a0 77 69 74 68 20 30 2e 0a 20 20 20 20 2a 2a 20 20  with 0..    **  
a25b0 20 66 6c 61 67 5f 6c 6f 6e 67 20 20 20 20 20 20   flag_long      
a25c0 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55               TRU
a25d0 45 20 69 66 20 74 68 65 20 6c 65 74 74 65 72 20  E if the letter 
a25e0 27 6c 27 20 28 65 6c 6c 29 20 70 72 65 66 69 78  'l' (ell) prefix
a25f0 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  ed.    **       
a2600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2610 20 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76          the conv
a2620 65 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72  ersion character
a2630 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f  ..    **   flag_
a2640 6c 6f 6e 67 6c 6f 6e 67 20 20 20 20 20 20 20 20  longlong        
a2650 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 74         TRUE if t
a2660 68 65 20 6c 65 74 74 65 72 20 27 6c 6c 27 20 28  he letter 'll' (
a2670 65 6c 6c 20 65 6c 6c 29 20 70 72 65 66 69 78 65  ell ell) prefixe
a2680 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  d.    **        
a2690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a26a0 20 20 20 20 20 20 20 74 68 65 20 63 6f 6e 76 65         the conve
a26b0 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72 2e  rsion character.
a26c0 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 62  .    **   flag_b
a26d0 6c 61 6e 6b 73 69 67 6e 20 20 20 20 20 20 20 20  lanksign        
a26e0 20 20 20 20 20 20 54 52 55 45 20 69 66 20 61 20        TRUE if a 
a26f0 27 20 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a  ' ' is present..
a2700 20 20 20 20 2a 2a 20 20 20 77 69 64 74 68 20 20      **   width  
a2710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2720 20 20 20 20 20 54 68 65 20 73 70 65 63 69 66 69       The specifi
a2730 65 64 20 66 69 65 6c 64 20 77 69 64 74 68 2e 20  ed field width. 
a2740 20 54 68 69 73 20 69 73 0a 20 20 20 20 2a 2a 20   This is.    ** 
a2750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6c                al
a2770 77 61 79 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76  ways non-negativ
a2780 65 2e 20 20 5a 65 72 6f 20 69 73 20 74 68 65 20  e.  Zero is the 
a2790 64 65 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2a 20  default..    ** 
a27a0 20 20 70 72 65 63 69 73 69 6f 6e 20 20 20 20 20    precision     
a27b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68                Th
a27c0 65 20 73 70 65 63 69 66 69 65 64 20 70 72 65 63  e specified prec
a27d0 69 73 69 6f 6e 2e 20 20 54 68 65 20 64 65 66 61  ision.  The defa
a27e0 75 6c 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  ult.    **      
a27f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2800 20 20 20 20 20 20 20 20 20 69 73 20 2d 31 2e 0a           is -1..
a2810 20 20 20 20 2a 2a 20 20 20 78 74 79 70 65 20 20      **   xtype  
a2820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2830 20 20 20 20 20 54 68 65 20 63 6c 61 73 73 20 6f       The class o
a2840 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  f the conversion
a2850 2e 0a 20 20 20 20 2a 2a 20 20 20 69 6e 66 6f 70  ..    **   infop
a2860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2870 20 20 20 20 20 20 20 50 6f 69 6e 74 65 72 20 74         Pointer t
a2880 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  o the appropriat
a2890 65 20 69 6e 66 6f 20 73 74 72 75 63 74 2e 0a 20  e info struct.. 
a28a0 20 20 20 2a 2f 0a 20 20 20 20 73 77 69 74 63 68     */.    switch
a28b0 28 20 78 74 79 70 65 20 29 7b 0a 20 20 20 20 20  ( xtype ){.     
a28c0 20 63 61 73 65 20 65 74 50 4f 49 4e 54 45 52 3a   case etPOINTER:
a28d0 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f  .        flag_lo
a28e0 6e 67 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28  nglong = sizeof(
a28f0 63 68 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 69  char*)==sizeof(i
a2900 36 34 29 3b 0a 20 20 20 20 20 20 20 20 66 6c 61  64);.        fla
a2910 67 5f 6c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28  g_long = sizeof(
a2920 63 68 61 72 2a 29 3d 3d 73 69 7a 65 6f 66 28 6c  char*)==sizeof(l
a2930 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ong int);.      
a2940 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67    /* Fall throug
a2950 68 20 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20  h into the next 
a2960 63 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 63 61  case */.      ca
a2970 73 65 20 65 74 4f 52 44 49 4e 41 4c 3a 0a 20 20  se etORDINAL:.  
a2980 20 20 20 20 63 61 73 65 20 65 74 52 41 44 49 58      case etRADIX
a2990 3a 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 6e  :.        if( in
a29a0 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41  fop->flags & FLA
a29b0 47 5f 53 49 47 4e 45 44 20 29 7b 0a 20 20 20 20  G_SIGNED ){.    
a29c0 20 20 20 20 20 20 69 36 34 20 76 3b 0a 20 20 20        i64 v;.   
a29d0 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f         if( flag_
a29e0 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 20  longlong ){.    
a29f0 20 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f 61          v = va_a
a2a00 72 67 28 61 70 2c 69 36 34 29 3b 0a 20 20 20 20  rg(ap,i64);.    
a2a10 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
a2a20 66 6c 61 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20 20  flag_long ){.   
a2a30 20 20 20 20 20 20 20 20 20 76 20 3d 20 76 61 5f           v = va_
a2a40 61 72 67 28 61 70 2c 6c 6f 6e 67 20 69 6e 74 29  arg(ap,long int)
a2a50 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
a2a60 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 76  e{.            v
a2a70 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74   = va_arg(ap,int
a2a80 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
a2a90 20 20 20 20 20 20 20 20 20 69 66 28 20 76 3c 30           if( v<0
a2aa0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
a2ab0 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 2d 76 3b 0a  longvalue = -v;.
a2ac0 20 20 20 20 20 20 20 20 20 20 20 20 70 72 65 66              pref
a2ad0 69 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20  ix = '-';.      
a2ae0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
a2af0 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65         longvalue
a2b00 20 3d 20 76 3b 0a 20 20 20 20 20 20 20 20 20 20   = v;.          
a2b10 20 20 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 73    if( flag_pluss
a2b20 69 67 6e 20 29 20 20 20 20 20 20 20 20 70 72 65  ign )        pre
a2b30 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20  fix = '+';.     
a2b40 20 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20         else if( 
a2b50 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29  flag_blanksign )
a2b60 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b 0a    prefix = ' ';.
a2b70 20 20 20 20 20 20 20 20 20 20 20 20 65 6c 73 65              else
a2b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2b90 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20         prefix = 
a2ba0 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  0;.          }. 
a2bb0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
a2bc0 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67          if( flag
a2bd0 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0a 20 20 20  _longlong ){.   
a2be0 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c           longval
a2bf0 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75  ue = va_arg(ap,u
a2c00 36 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  64);.          }
a2c10 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f  else if( flag_lo
a2c20 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ng ){.          
a2c30 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 61    longvalue = va
a2c40 5f 61 72 67 28 61 70 2c 75 6e 73 69 67 6e 65 64  _arg(ap,unsigned
a2c50 20 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20 20   long int);.    
a2c60 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
a2c70 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c           longval
a2c80 75 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75  ue = va_arg(ap,u
a2c90 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a 20 20  nsigned int);.  
a2ca0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a2cb0 20 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a      prefix = 0;.
a2cc0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a2cd0 20 20 69 66 28 20 6c 6f 6e 67 76 61 6c 75 65 3d    if( longvalue=
a2ce0 3d 30 20 29 20 66 6c 61 67 5f 61 6c 74 65 72 6e  =0 ) flag_altern
a2cf0 61 74 65 66 6f 72 6d 20 3d 20 30 3b 0a 20 20 20  ateform = 0;.   
a2d00 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 7a 65       if( flag_ze
a2d10 72 6f 70 61 64 20 26 26 20 70 72 65 63 69 73 69  ropad && precisi
a2d20 6f 6e 3c 77 69 64 74 68 2d 28 70 72 65 66 69 78  on<width-(prefix
a2d30 21 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20  !=0) ){.        
a2d40 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 77 69    precision = wi
a2d50 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30 29 3b  dth-(prefix!=0);
a2d60 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a2d70 20 20 20 62 75 66 70 74 20 3d 20 26 62 75 66 5b     bufpt = &buf[
a2d80 65 74 42 55 46 53 49 5a 45 2d 31 5d 3b 0a 20 20  etBUFSIZE-1];.  
a2d90 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d        if( xtype=
a2da0 3d 65 74 4f 52 44 49 4e 41 4c 20 29 7b 0a 20 20  =etORDINAL ){.  
a2db0 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20 63          static c
a2dc0 6f 6e 73 74 20 63 68 61 72 20 7a 4f 72 64 5b 5d  onst char zOrd[]
a2dd0 20 3d 20 22 74 68 73 74 6e 64 72 64 22 3b 0a 20   = "thstndrd";. 
a2de0 20 20 20 20 20 20 20 20 20 69 6e 74 20 78 20 3d           int x =
a2df0 20 28 69 6e 74 29 28 6c 6f 6e 67 76 61 6c 75 65   (int)(longvalue
a2e00 20 25 20 31 30 29 3b 0a 20 20 20 20 20 20 20 20   % 10);.        
a2e10 20 20 69 66 28 20 78 3e 3d 34 20 7c 7c 20 28 6c    if( x>=4 || (l
a2e20 6f 6e 67 76 61 6c 75 65 2f 31 30 29 25 31 30 3d  ongvalue/10)%10=
a2e30 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =1 ){.          
a2e40 20 20 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20    x = 0;.       
a2e50 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62     }.          b
a2e60 75 66 5b 65 74 42 55 46 53 49 5a 45 2d 33 5d 20  uf[etBUFSIZE-3] 
a2e70 3d 20 7a 4f 72 64 5b 78 2a 32 5d 3b 0a 20 20 20  = zOrd[x*2];.   
a2e80 20 20 20 20 20 20 20 62 75 66 5b 65 74 42 55 46         buf[etBUF
a2e90 53 49 5a 45 2d 32 5d 20 3d 20 7a 4f 72 64 5b 78  SIZE-2] = zOrd[x
a2ea0 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 20  *2+1];.         
a2eb0 20 62 75 66 70 74 20 2d 3d 20 32 3b 0a 20 20 20   bufpt -= 2;.   
a2ec0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7b       }.        {
a2ed0 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 69 73  .          regis
a2ee0 74 65 72 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ter const char *
a2ef0 63 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 55 73  cset;      /* Us
a2f00 65 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 20  e registers for 
a2f10 73 70 65 65 64 20 2a 2f 0a 20 20 20 20 20 20 20  speed */.       
a2f20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74 20     register int 
a2f30 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20  base;.          
a2f40 63 73 65 74 20 3d 20 26 61 44 69 67 69 74 73 5b  cset = &aDigits[
a2f50 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b  infop->charset];
a2f60 0a 20 20 20 20 20 20 20 20 20 20 62 61 73 65 20  .          base 
a2f70 3d 20 69 6e 66 6f 70 2d 3e 62 61 73 65 3b 0a 20  = infop->base;. 
a2f80 20 20 20 20 20 20 20 20 20 64 6f 7b 20 20 20 20           do{    
a2f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2fb0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72         /* Conver
a2fc0 74 20 74 6f 20 61 73 63 69 69 20 2a 2f 0a 20 20  t to ascii */.  
a2fd0 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75            *(--bu
a2fe0 66 70 74 29 20 3d 20 63 73 65 74 5b 6c 6f 6e 67  fpt) = cset[long
a2ff0 76 61 6c 75 65 25 62 61 73 65 5d 3b 0a 20 20 20  value%base];.   
a3000 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c           longval
a3010 75 65 20 3d 20 6c 6f 6e 67 76 61 6c 75 65 2f 62  ue = longvalue/b
a3020 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ase;.          }
a3030 77 68 69 6c 65 28 20 6c 6f 6e 67 76 61 6c 75 65  while( longvalue
a3040 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  >0 );.        }.
a3050 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d          length =
a3060 20 28 69 6e 74 29 28 26 62 75 66 5b 65 74 42 55   (int)(&buf[etBU
a3070 46 53 49 5a 45 2d 31 5d 2d 62 75 66 70 74 29 3b  FSIZE-1]-bufpt);
a3080 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 78  .        for(idx
a3090 3d 70 72 65 63 69 73 69 6f 6e 2d 6c 65 6e 67 74  =precision-lengt
a30a0 68 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 29  h; idx>0; idx--)
a30b0 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d  {.          *(--
a30c0 62 75 66 70 74 29 20 3d 20 27 30 27 3b 20 20 20  bufpt) = '0';   
a30d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a30e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 5a 65 72            /* Zer
a30f0 6f 20 70 61 64 20 2a 2f 0a 20 20 20 20 20 20 20  o pad */.       
a3100 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70   }.        if( p
a3110 72 65 66 69 78 20 29 20 2a 28 2d 2d 62 75 66 70  refix ) *(--bufp
a3120 74 29 20 3d 20 70 72 65 66 69 78 3b 20 20 20 20  t) = prefix;    
a3130 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
a3140 64 20 73 69 67 6e 20 2a 2f 0a 20 20 20 20 20 20  d sign */.      
a3150 20 20 69 66 28 20 66 6c 61 67 5f 61 6c 74 65 72    if( flag_alter
a3160 6e 61 74 65 66 6f 72 6d 20 26 26 20 69 6e 66 6f  nateform && info
a3170 70 2d 3e 70 72 65 66 69 78 20 29 7b 20 20 20 20  p->prefix ){    
a3180 20 20 2f 2a 20 41 64 64 20 22 30 22 20 6f 72 20    /* Add "0" or 
a3190 22 30 78 22 20 2a 2f 0a 20 20 20 20 20 20 20 20  "0x" */.        
a31a0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 72    const char *pr
a31b0 65 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61  e;.          cha
a31c0 72 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20 70  r x;.          p
a31d0 72 65 20 3d 20 26 61 50 72 65 66 69 78 5b 69 6e  re = &aPrefix[in
a31e0 66 6f 70 2d 3e 70 72 65 66 69 78 5d 3b 0a 20 20  fop->prefix];.  
a31f0 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20 28 78          for(; (x
a3200 3d 28 2a 70 72 65 29 29 21 3d 30 3b 20 70 72 65  =(*pre))!=0; pre
a3210 2b 2b 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d  ++) *(--bufpt) =
a3220 20 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   x;.        }.  
a3230 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28        length = (
a3240 69 6e 74 29 28 26 62 75 66 5b 65 74 42 55 46 53  int)(&buf[etBUFS
a3250 49 5a 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a 20  IZE-1]-bufpt);. 
a3260 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
a3270 20 20 20 20 63 61 73 65 20 65 74 46 4c 4f 41 54      case etFLOAT
a3280 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 45  :.      case etE
a3290 58 50 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65  XP:.      case e
a32a0 74 47 45 4e 45 52 49 43 3a 0a 20 20 20 20 20 20  tGENERIC:.      
a32b0 20 20 72 65 61 6c 76 61 6c 75 65 20 3d 20 76 61    realvalue = va
a32c0 5f 61 72 67 28 61 70 2c 64 6f 75 62 6c 65 29 3b  _arg(ap,double);
a32d0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
a32e0 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
a32f0 49 4e 54 0a 20 20 20 20 20 20 20 20 69 66 28 20  INT.        if( 
a3300 70 72 65 63 69 73 69 6f 6e 3c 30 20 29 20 70 72  precision<0 ) pr
a3310 65 63 69 73 69 6f 6e 20 3d 20 36 3b 20 20 20 20  ecision = 6;    
a3320 20 20 20 20 20 2f 2a 20 53 65 74 20 64 65 66 61       /* Set defa
a3330 75 6c 74 20 70 72 65 63 69 73 69 6f 6e 20 2a 2f  ult precision */
a3340 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65  .        if( pre
a3350 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a 45  cision>etBUFSIZE
a3360 2f 32 2d 31 30 20 29 20 70 72 65 63 69 73 69 6f  /2-10 ) precisio
a3370 6e 20 3d 20 65 74 42 55 46 53 49 5a 45 2f 32 2d  n = etBUFSIZE/2-
a3380 31 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  10;.        if( 
a3390 72 65 61 6c 76 61 6c 75 65 3c 30 2e 30 20 29 7b  realvalue<0.0 ){
a33a0 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c 76  .          realv
a33b0 61 6c 75 65 20 3d 20 2d 72 65 61 6c 76 61 6c 75  alue = -realvalu
a33c0 65 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65  e;.          pre
a33d0 66 69 78 20 3d 20 27 2d 27 3b 0a 20 20 20 20 20  fix = '-';.     
a33e0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a33f0 20 20 20 20 69 66 28 20 66 6c 61 67 5f 70 6c 75      if( flag_plu
a3400 73 73 69 67 6e 20 29 20 20 20 20 20 20 20 20 20  ssign )         
a3410 20 70 72 65 66 69 78 20 3d 20 27 2b 27 3b 0a 20   prefix = '+';. 
a3420 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 69 66           else if
a3430 28 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e  ( flag_blanksign
a3440 20 29 20 20 20 20 70 72 65 66 69 78 20 3d 20 27   )    prefix = '
a3450 20 27 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c   ';.          el
a3460 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  se              
a3470 20 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69             prefi
a3480 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  x = 0;.        }
a3490 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74 79  .        if( xty
a34a0 70 65 3d 3d 65 74 47 45 4e 45 52 49 43 20 26 26  pe==etGENERIC &&
a34b0 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 29 20 70   precision>0 ) p
a34c0 72 65 63 69 73 69 6f 6e 2d 2d 3b 0a 23 69 66 20  recision--;.#if 
a34d0 30 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 75  0.        /* Rou
a34e0 6e 64 69 6e 67 20 77 6f 72 6b 73 20 6c 69 6b 65  nding works like
a34f0 20 42 53 44 20 77 68 65 6e 20 74 68 65 20 63 6f   BSD when the co
a3500 6e 73 74 61 6e 74 20 30 2e 34 39 39 39 20 69 73  nstant 0.4999 is
a3510 20 75 73 65 64 2e 20 20 57 69 65 72 64 21 20 2a   used.  Wierd! *
a3520 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 64  /.        for(id
a3530 78 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72 6f 75  x=precision, rou
a3540 6e 64 65 72 3d 30 2e 34 39 39 39 3b 20 69 64 78  nder=0.4999; idx
a3550 3e 30 3b 20 69 64 78 2d 2d 2c 20 72 6f 75 6e 64  >0; idx--, round
a3560 65 72 2a 3d 30 2e 31 29 3b 0a 23 65 6c 73 65 0a  er*=0.1);.#else.
a3570 20 20 20 20 20 20 20 20 2f 2a 20 49 74 20 6d 61          /* It ma
a3580 6b 65 73 20 6d 6f 72 65 20 73 65 6e 73 65 20 74  kes more sense t
a3590 6f 20 75 73 65 20 30 2e 35 20 2a 2f 0a 20 20 20  o use 0.5 */.   
a35a0 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72 65       for(idx=pre
a35b0 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d  cision, rounder=
a35c0 30 2e 35 3b 20 69 64 78 3e 30 3b 20 69 64 78 2d  0.5; idx>0; idx-
a35d0 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d 30 2e 31 29  -, rounder*=0.1)
a35e0 7b 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  {}.#endif.      
a35f0 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 46    if( xtype==etF
a3600 4c 4f 41 54 20 29 20 72 65 61 6c 76 61 6c 75 65  LOAT ) realvalue
a3610 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20 20   += rounder;.   
a3620 20 20 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 69 7a       /* Normaliz
a3630 65 20 72 65 61 6c 76 61 6c 75 65 20 74 6f 20 77  e realvalue to w
a3640 69 74 68 69 6e 20 31 30 2e 30 20 3e 20 72 65 61  ithin 10.0 > rea
a3650 6c 76 61 6c 75 65 20 3e 3d 20 31 2e 30 20 2a 2f  lvalue >= 1.0 */
a3660 0a 20 20 20 20 20 20 20 20 65 78 70 20 3d 20 30  .        exp = 0
a3670 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71  ;.        if( sq
a3680 6c 69 74 65 33 49 73 4e 61 4e 28 28 64 6f 75 62  lite3IsNaN((doub
a3690 6c 65 29 72 65 61 6c 76 61 6c 75 65 29 20 29 7b  le)realvalue) ){
a36a0 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74  .          bufpt
a36b0 20 3d 20 22 4e 61 4e 22 3b 0a 20 20 20 20 20 20   = "NaN";.      
a36c0 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 33 3b 0a      length = 3;.
a36d0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
a36e0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a36f0 20 20 20 69 66 28 20 72 65 61 6c 76 61 6c 75 65     if( realvalue
a3700 3e 30 2e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  >0.0 ){.        
a3710 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c    while( realval
a3720 75 65 3e 3d 31 65 33 32 20 26 26 20 65 78 70 3c  ue>=1e32 && exp<
a3730 3d 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75  =350 ){ realvalu
a3740 65 20 2a 3d 20 31 65 2d 33 32 3b 20 65 78 70 2b  e *= 1e-32; exp+
a3750 3d 33 32 3b 20 7d 0a 20 20 20 20 20 20 20 20 20  =32; }.         
a3760 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75   while( realvalu
a3770 65 3e 3d 31 65 38 20 26 26 20 65 78 70 3c 3d 33  e>=1e8 && exp<=3
a3780 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20  50 ){ realvalue 
a3790 2a 3d 20 31 65 2d 38 3b 20 65 78 70 2b 3d 38 3b  *= 1e-8; exp+=8;
a37a0 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69   }.          whi
a37b0 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31  le( realvalue>=1
a37c0 30 2e 30 20 26 26 20 65 78 70 3c 3d 33 35 30 20  0.0 && exp<=350 
a37d0 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20  ){ realvalue *= 
a37e0 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20  0.1; exp++; }.  
a37f0 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72          while( r
a3800 65 61 6c 76 61 6c 75 65 3c 31 65 2d 38 20 29 7b  ealvalue<1e-8 ){
a3810 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 65   realvalue *= 1e
a3820 38 3b 20 65 78 70 2d 3d 38 3b 20 7d 0a 20 20 20  8; exp-=8; }.   
a3830 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 65         while( re
a3840 61 6c 76 61 6c 75 65 3c 31 2e 30 20 29 7b 20 72  alvalue<1.0 ){ r
a3850 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 30 2e 30  ealvalue *= 10.0
a3860 3b 20 65 78 70 2d 2d 3b 20 7d 0a 20 20 20 20 20  ; exp--; }.     
a3870 20 20 20 20 20 69 66 28 20 65 78 70 3e 33 35 30       if( exp>350
a3880 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
a3890 69 66 28 20 70 72 65 66 69 78 3d 3d 27 2d 27 20  if( prefix=='-' 
a38a0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
a38b0 20 62 75 66 70 74 20 3d 20 22 2d 49 6e 66 22 3b   bufpt = "-Inf";
a38c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c  .            }el
a38d0 73 65 20 69 66 28 20 70 72 65 66 69 78 3d 3d 27  se if( prefix=='
a38e0 2b 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  +' ){.          
a38f0 20 20 20 20 62 75 66 70 74 20 3d 20 22 2b 49 6e      bufpt = "+In
a3900 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  f";.            
a3910 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
a3920 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 49 6e       bufpt = "In
a3930 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  f";.            
a3940 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 65  }.            le
a3950 6e 67 74 68 20 3d 20 73 71 6c 69 74 65 33 53 74  ngth = sqlite3St
a3960 72 6c 65 6e 33 30 28 62 75 66 70 74 29 3b 0a 20  rlen30(bufpt);. 
a3970 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
a3980 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
a3990 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a39a0 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20  bufpt = buf;.   
a39b0 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 20 20       /*.        
a39c0 2a 2a 20 49 66 20 74 68 65 20 66 69 65 6c 64 20  ** If the field 
a39d0 74 79 70 65 20 69 73 20 65 74 47 45 4e 45 52 49  type is etGENERI
a39e0 43 2c 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 20  C, then convert 
a39f0 74 6f 20 65 69 74 68 65 72 20 65 74 45 58 50 0a  to either etEXP.
a3a00 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 65 74          ** or et
a3a10 46 4c 4f 41 54 2c 20 61 73 20 61 70 70 72 6f 70  FLOAT, as approp
a3a20 72 69 61 74 65 2e 0a 20 20 20 20 20 20 20 20 2a  riate..        *
a3a30 2f 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 65  /.        flag_e
a3a40 78 70 20 3d 20 78 74 79 70 65 3d 3d 65 74 45 58  xp = xtype==etEX
a3a50 50 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 78  P;.        if( x
a3a60 74 79 70 65 21 3d 65 74 46 4c 4f 41 54 20 29 7b  type!=etFLOAT ){
a3a70 0a 20 20 20 20 20 20 20 20 20 20 72 65 61 6c 76  .          realv
a3a80 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b  alue += rounder;
a3a90 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
a3aa0 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20 29  ealvalue>=10.0 )
a3ab0 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30  { realvalue *= 0
a3ac0 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20  .1; exp++; }.   
a3ad0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
a3ae0 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45 4e 45  f( xtype==etGENE
a3af0 52 49 43 20 29 7b 0a 20 20 20 20 20 20 20 20 20  RIC ){.         
a3b00 20 66 6c 61 67 5f 72 74 7a 20 3d 20 21 66 6c 61   flag_rtz = !fla
a3b10 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 3b  g_alternateform;
a3b20 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65  .          if( e
a3b30 78 70 3c 2d 34 20 7c 7c 20 65 78 70 3e 70 72 65  xp<-4 || exp>pre
a3b40 63 69 73 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20  cision ){.      
a3b50 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 65 74        xtype = et
a3b60 45 58 50 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  EXP;.          }
a3b70 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
a3b80 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 70 72    precision = pr
a3b90 65 63 69 73 69 6f 6e 20 2d 20 65 78 70 3b 0a 20  ecision - exp;. 
a3ba0 20 20 20 20 20 20 20 20 20 20 20 78 74 79 70 65             xtype
a3bb0 20 3d 20 65 74 46 4c 4f 41 54 3b 0a 20 20 20 20   = etFLOAT;.    
a3bc0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a3bd0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
a3be0 20 66 6c 61 67 5f 72 74 7a 20 3d 20 30 3b 0a 20   flag_rtz = 0;. 
a3bf0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a3c00 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 45 58   if( xtype==etEX
a3c10 50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 65  P ){.          e
a3c20 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  2 = 0;.        }
a3c30 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
a3c40 65 32 20 3d 20 65 78 70 3b 0a 20 20 20 20 20 20  e2 = exp;.      
a3c50 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 73 64 20    }.        nsd 
a3c60 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6c 61  = 0;.        fla
a3c70 67 5f 64 70 20 3d 20 28 70 72 65 63 69 73 69 6f  g_dp = (precisio
a3c80 6e 3e 30 20 3f 31 3a 30 29 20 7c 20 66 6c 61 67  n>0 ?1:0) | flag
a3c90 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 7c  _alternateform |
a3ca0 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a   flag_altform2;.
a3cb0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73          /* The s
a3cc0 69 67 6e 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20  ign in front of 
a3cd0 74 68 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  the number */.  
a3ce0 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69 78        if( prefix
a3cf0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28   ){.          *(
a3d00 62 75 66 70 74 2b 2b 29 20 3d 20 70 72 65 66 69  bufpt++) = prefi
a3d10 78 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  x;.        }.   
a3d20 20 20 20 20 20 2f 2a 20 44 69 67 69 74 73 20 70       /* Digits p
a3d30 72 69 6f 72 20 74 6f 20 74 68 65 20 64 65 63 69  rior to the deci
a3d40 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20  mal point */.   
a3d50 20 20 20 20 20 69 66 28 20 65 32 3c 30 20 29 7b       if( e2<0 ){
a3d60 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66  .          *(buf
a3d70 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20  pt++) = '0';.   
a3d80 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a3d90 20 20 20 20 20 20 66 6f 72 28 3b 20 65 32 3e 3d        for(; e2>=
a3da0 30 3b 20 65 32 2d 2d 29 7b 0a 20 20 20 20 20 20  0; e2--){.      
a3db0 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29        *(bufpt++)
a3dc0 20 3d 20 65 74 5f 67 65 74 64 69 67 69 74 28 26   = et_getdigit(&
a3dd0 72 65 61 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b  realvalue,&nsd);
a3de0 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
a3df0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f       }.        /
a3e00 2a 20 54 68 65 20 64 65 63 69 6d 61 6c 20 70 6f  * The decimal po
a3e10 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  int */.        i
a3e20 66 28 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20  f( flag_dp ){.  
a3e30 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b          *(bufpt+
a3e40 2b 29 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20  +) = '.';.      
a3e50 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 22    }.        /* "
a3e60 30 22 20 64 69 67 69 74 73 20 61 66 74 65 72 20  0" digits after 
a3e70 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e  the decimal poin
a3e80 74 20 62 75 74 20 62 65 66 6f 72 65 20 74 68 65  t but before the
a3e90 20 66 69 72 73 74 0a 20 20 20 20 20 20 20 20 2a   first.        *
a3ea0 2a 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69  * significant di
a3eb0 67 69 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  git of the numbe
a3ec0 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72  r */.        for
a3ed0 28 65 32 2b 2b 3b 20 65 32 3c 30 3b 20 70 72 65  (e2++; e2<0; pre
a3ee0 63 69 73 69 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b  cision--, e2++){
a3ef0 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
a3f00 74 28 20 70 72 65 63 69 73 69 6f 6e 3e 30 20 29  t( precision>0 )
a3f10 3b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75  ;.          *(bu
a3f20 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20  fpt++) = '0';.  
a3f30 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a3f40 2f 2a 20 53 69 67 6e 69 66 69 63 61 6e 74 20 64  /* Significant d
a3f50 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65 20  igits after the 
a3f60 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f  decimal point */
a3f70 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
a3f80 28 70 72 65 63 69 73 69 6f 6e 2d 2d 29 3e 30 20  (precision--)>0 
a3f90 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62  ){.          *(b
a3fa0 75 66 70 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74  ufpt++) = et_get
a3fb0 64 69 67 69 74 28 26 72 65 61 6c 76 61 6c 75 65  digit(&realvalue
a3fc0 2c 26 6e 73 64 29 3b 0a 20 20 20 20 20 20 20 20  ,&nsd);.        
a3fd0 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d  }.        /* Rem
a3fe0 6f 76 65 20 74 72 61 69 6c 69 6e 67 20 7a 65 72  ove trailing zer
a3ff0 6f 73 20 61 6e 64 20 74 68 65 20 22 2e 22 20 69  os and the "." i
a4000 66 20 6e 6f 20 64 69 67 69 74 73 20 66 6f 6c 6c  f no digits foll
a4010 6f 77 20 74 68 65 20 22 2e 22 20 2a 2f 0a 20 20  ow the "." */.  
a4020 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 72        if( flag_r
a4030 74 7a 20 26 26 20 66 6c 61 67 5f 64 70 20 29 7b  tz && flag_dp ){
a4040 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65  .          while
a4050 28 20 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 30 27  ( bufpt[-1]=='0'
a4060 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20   ) *(--bufpt) = 
a4070 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  0;.          ass
a4080 65 72 74 28 20 62 75 66 70 74 3e 62 75 66 20 29  ert( bufpt>buf )
a4090 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
a40a0 62 75 66 70 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29  bufpt[-1]=='.' )
a40b0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  {.            if
a40c0 28 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20  ( flag_altform2 
a40d0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
a40e0 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 30   *(bufpt++) = '0
a40f0 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  ';.            }
a4100 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
a4110 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d      *(--bufpt) =
a4120 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   0;.            
a4130 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  }.          }.  
a4140 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a4150 2f 2a 20 41 64 64 20 74 68 65 20 22 65 4e 4e 4e  /* Add the "eNNN
a4160 22 20 73 75 66 66 69 78 20 2a 2f 0a 20 20 20 20  " suffix */.    
a4170 20 20 20 20 69 66 28 20 66 6c 61 67 5f 65 78 70      if( flag_exp
a4180 20 7c 7c 20 78 74 79 70 65 3d 3d 65 74 45 58 50   || xtype==etEXP
a4190 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28   ){.          *(
a41a0 62 75 66 70 74 2b 2b 29 20 3d 20 61 44 69 67 69  bufpt++) = aDigi
a41b0 74 73 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65  ts[infop->charse
a41c0 74 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  t];.          if
a41d0 28 20 65 78 70 3c 30 20 29 7b 0a 20 20 20 20 20  ( exp<0 ){.     
a41e0 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b         *(bufpt++
a41f0 29 20 3d 20 27 2d 27 3b 20 65 78 70 20 3d 20 2d  ) = '-'; exp = -
a4200 65 78 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  exp;.          }
a4210 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
a4220 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27    *(bufpt++) = '
a4230 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  +';.          }.
a4240 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78            if( ex
a4250 70 3e 3d 31 30 30 20 29 7b 0a 20 20 20 20 20 20  p>=100 ){.      
a4260 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29        *(bufpt++)
a4270 20 3d 20 28 63 68 61 72 29 28 28 65 78 70 2f 31   = (char)((exp/1
a4280 30 30 29 2b 27 30 27 29 3b 20 20 20 20 20 20 20  00)+'0');       
a4290 20 2f 2a 20 31 30 30 27 73 20 64 69 67 69 74 20   /* 100's digit 
a42a0 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 65  */.            e
a42b0 78 70 20 25 3d 20 31 30 30 3b 0a 20 20 20 20 20  xp %= 100;.     
a42c0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
a42d0 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 28 63   *(bufpt++) = (c
a42e0 68 61 72 29 28 65 78 70 2f 31 30 2b 27 30 27 29  har)(exp/10+'0')
a42f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
a4300 20 31 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 20   10's digit */. 
a4310 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74           *(bufpt
a4320 2b 2b 29 20 3d 20 28 63 68 61 72 29 28 65 78 70  ++) = (char)(exp
a4330 25 31 30 2b 27 30 27 29 3b 20 20 20 20 20 20 20  %10+'0');       
a4340 20 20 20 20 20 20 2f 2a 20 31 27 73 20 64 69 67        /* 1's dig
a4350 69 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a  it */.        }.
a4360 20 20 20 20 20 20 20 20 2a 62 75 66 70 74 20 3d          *bufpt =
a4370 20 30 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20   0;..        /* 
a4380 54 68 65 20 63 6f 6e 76 65 72 74 65 64 20 6e 75  The converted nu
a4390 6d 62 65 72 20 69 73 20 69 6e 20 62 75 66 5b 5d  mber is in buf[]
a43a0 20 61 6e 64 20 7a 65 72 6f 20 74 65 72 6d 69 6e   and zero termin
a43b0 61 74 65 64 2e 20 4f 75 74 70 75 74 20 69 74 2e  ated. Output it.
a43c0 0a 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65  .        ** Note
a43d0 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72   that the number
a43e0 20 69 73 20 69 6e 20 74 68 65 20 75 73 75 61 6c   is in the usual
a43f0 20 6f 72 64 65 72 2c 20 6e 6f 74 20 72 65 76 65   order, not reve
a4400 72 73 65 64 20 61 73 20 77 69 74 68 0a 20 20 20  rsed as with.   
a4410 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 20       ** integer 
a4420 63 6f 6e 76 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a  conversions. */.
a4430 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d          length =
a4440 20 28 69 6e 74 29 28 62 75 66 70 74 2d 62 75 66   (int)(bufpt-buf
a4450 29 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 74  );.        bufpt
a4460 20 3d 20 62 75 66 3b 0a 0a 20 20 20 20 20 20 20   = buf;..       
a4470 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65   /* Special case
a4480 3a 20 20 41 64 64 20 6c 65 61 64 69 6e 67 20 7a  :  Add leading z
a4490 65 72 6f 73 20 69 66 20 74 68 65 20 66 6c 61 67  eros if the flag
a44a0 5f 7a 65 72 6f 70 61 64 20 66 6c 61 67 20 69 73  _zeropad flag is
a44b0 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20  .        ** set 
a44c0 61 6e 64 20 77 65 20 61 72 65 20 6e 6f 74 20 6c  and we are not l
a44d0 65 66 74 20 6a 75 73 74 69 66 69 65 64 20 2a 2f  eft justified */
a44e0 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61  .        if( fla
a44f0 67 5f 7a 65 72 6f 70 61 64 20 26 26 20 21 66 6c  g_zeropad && !fl
a4500 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20 26  ag_leftjustify &
a4510 26 20 6c 65 6e 67 74 68 20 3c 20 77 69 64 74 68  & length < width
a4520 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  ){.          int
a4530 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   i;.          in
a4540 74 20 6e 50 61 64 20 3d 20 77 69 64 74 68 20 2d  t nPad = width -
a4550 20 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 20   length;.       
a4560 20 20 20 66 6f 72 28 69 3d 77 69 64 74 68 3b 20     for(i=width; 
a4570 69 3e 3d 6e 50 61 64 3b 20 69 2d 2d 29 7b 0a 20  i>=nPad; i--){. 
a4580 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70 74             bufpt
a4590 5b 69 5d 20 3d 20 62 75 66 70 74 5b 69 2d 6e 50  [i] = bufpt[i-nP
a45a0 61 64 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ad];.          }
a45b0 0a 20 20 20 20 20 20 20 20 20 20 69 20 3d 20 70  .          i = p
a45c0 72 65 66 69 78 21 3d 30 3b 0a 20 20 20 20 20 20  refix!=0;.      
a45d0 20 20 20 20 77 68 69 6c 65 28 20 6e 50 61 64 2d      while( nPad-
a45e0 2d 20 29 20 62 75 66 70 74 5b 69 2b 2b 5d 20 3d  - ) bufpt[i++] =
a45f0 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20   '0';.          
a4600 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68 3b 0a  length = width;.
a4610 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66          }.#endif
a4620 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
a4630 20 20 20 20 20 20 63 61 73 65 20 65 74 53 49 5a        case etSIZ
a4640 45 3a 0a 20 20 20 20 20 20 20 20 2a 28 76 61 5f  E:.        *(va_
a4650 61 72 67 28 61 70 2c 69 6e 74 2a 29 29 20 3d 20  arg(ap,int*)) = 
a4660 70 41 63 63 75 6d 2d 3e 6e 43 68 61 72 3b 0a 20  pAccum->nChar;. 
a4670 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20         length = 
a4680 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 20  width = 0;.     
a4690 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
a46a0 63 61 73 65 20 65 74 50 45 52 43 45 4e 54 3a 0a  case etPERCENT:.
a46b0 20 20 20 20 20 20 20 20 62 75 66 5b 30 5d 20 3d          buf[0] =
a46c0 20 27 25 27 3b 0a 20 20 20 20 20 20 20 20 62 75   '%';.        bu
a46d0 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20  fpt = buf;.     
a46e0 20 20 20 6c 65 6e 67 74 68 20 3d 20 31 3b 0a 20     length = 1;. 
a46f0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
a4700 20 20 20 20 63 61 73 65 20 65 74 43 48 41 52 58      case etCHARX
a4710 3a 0a 20 20 20 20 20 20 20 20 63 20 3d 20 76 61  :.        c = va
a4720 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20  _arg(ap,int);.  
a4730 20 20 20 20 20 20 62 75 66 5b 30 5d 20 3d 20 28        buf[0] = (
a4740 63 68 61 72 29 63 3b 0a 20 20 20 20 20 20 20 20  char)c;.        
a4750 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30  if( precision>=0
a4760 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f   ){.          fo
a4770 72 28 69 64 78 3d 31 3b 20 69 64 78 3c 70 72 65  r(idx=1; idx<pre
a4780 63 69 73 69 6f 6e 3b 20 69 64 78 2b 2b 29 20 62  cision; idx++) b
a4790 75 66 5b 69 64 78 5d 20 3d 20 28 63 68 61 72 29  uf[idx] = (char)
a47a0 63 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e  c;.          len
a47b0 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b  gth = precision;
a47c0 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
a47d0 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68            length
a47e0 20 3d 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   =1;.        }. 
a47f0 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62         bufpt = b
a4800 75 66 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  uf;.        brea
a4810 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  k;.      case et
a4820 53 54 52 49 4e 47 3a 0a 20 20 20 20 20 20 63 61  STRING:.      ca
a4830 73 65 20 65 74 44 59 4e 53 54 52 49 4e 47 3a 0a  se etDYNSTRING:.
a4840 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20          bufpt = 
a4850 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a 29  va_arg(ap,char*)
a4860 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 62 75  ;.        if( bu
a4870 66 70 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  fpt==0 ){.      
a4880 20 20 20 20 62 75 66 70 74 20 3d 20 22 22 3b 0a      bufpt = "";.
a4890 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
a48a0 28 20 78 74 79 70 65 3d 3d 65 74 44 59 4e 53 54  ( xtype==etDYNST
a48b0 52 49 4e 47 20 29 7b 0a 20 20 20 20 20 20 20 20  RING ){.        
a48c0 20 20 7a 45 78 74 72 61 20 3d 20 62 75 66 70 74    zExtra = bufpt
a48d0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
a48e0 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f      if( precisio
a48f0 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  n>=0 ){.        
a4900 20 20 66 6f 72 28 6c 65 6e 67 74 68 3d 30 3b 20    for(length=0; 
a4910 6c 65 6e 67 74 68 3c 70 72 65 63 69 73 69 6f 6e  length<precision
a4920 20 26 26 20 62 75 66 70 74 5b 6c 65 6e 67 74 68   && bufpt[length
a4930 5d 3b 20 6c 65 6e 67 74 68 2b 2b 29 7b 7d 0a 20  ]; length++){}. 
a4940 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
a4950 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d          length =
a4960 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
a4970 28 62 75 66 70 74 29 3b 0a 20 20 20 20 20 20 20  (bufpt);.       
a4980 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
a4990 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53  ;.      case etS
a49a0 51 4c 45 53 43 41 50 45 3a 0a 20 20 20 20 20 20  QLESCAPE:.      
a49b0 63 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 45  case etSQLESCAPE
a49c0 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74  2:.      case et
a49d0 53 51 4c 45 53 43 41 50 45 33 3a 20 7b 0a 20 20  SQLESCAPE3: {.  
a49e0 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6a 2c 20        int i, j, 
a49f0 6e 2c 20 69 73 6e 75 6c 6c 3b 0a 20 20 20 20 20  n, isnull;.     
a4a00 20 20 20 69 6e 74 20 6e 65 65 64 51 75 6f 74 65     int needQuote
a4a10 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 63  ;.        char c
a4a20 68 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  h;.        char 
a4a30 71 20 3d 20 28 28 78 74 79 70 65 3d 3d 65 74 53  q = ((xtype==etS
a4a40 51 4c 45 53 43 41 50 45 33 29 3f 27 22 27 3a 27  QLESCAPE3)?'"':'
a4a50 5c 27 27 29 3b 20 20 20 2f 2a 20 51 75 6f 74 65  \'');   /* Quote
a4a60 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20   character */.  
a4a70 20 20 20 20 20 20 63 68 61 72 20 2a 65 73 63 61        char *esca
a4a80 72 67 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 63  rg = va_arg(ap,c
a4a90 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 69  har*);.        i
a4aa0 73 6e 75 6c 6c 20 3d 20 65 73 63 61 72 67 3d 3d  snull = escarg==
a4ab0 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  0;.        if( i
a4ac0 73 6e 75 6c 6c 20 29 20 65 73 63 61 72 67 20 3d  snull ) escarg =
a4ad0 20 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53   (xtype==etSQLES
a4ae0 43 41 50 45 32 20 3f 20 22 4e 55 4c 4c 22 20 3a  CAPE2 ? "NULL" :
a4af0 20 22 28 4e 55 4c 4c 29 22 29 3b 0a 20 20 20 20   "(NULL)");.    
a4b00 20 20 20 20 66 6f 72 28 69 3d 6e 3d 30 3b 20 28      for(i=n=0; (
a4b10 63 68 3d 65 73 63 61 72 67 5b 69 5d 29 21 3d 30  ch=escarg[i])!=0
a4b20 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
a4b30 20 20 69 66 28 20 63 68 3d 3d 71 20 29 20 20 6e    if( ch==q )  n
a4b40 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ++;.        }.  
a4b50 20 20 20 20 20 20 6e 65 65 64 51 75 6f 74 65 20        needQuote 
a4b60 3d 20 21 69 73 6e 75 6c 6c 20 26 26 20 78 74 79  = !isnull && xty
a4b70 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 32  pe==etSQLESCAPE2
a4b80 3b 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 69  ;.        n += i
a4b90 20 2b 20 31 20 2b 20 6e 65 65 64 51 75 6f 74 65   + 1 + needQuote
a4ba0 2a 32 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  *2;.        if( 
a4bb0 6e 3e 65 74 42 55 46 53 49 5a 45 20 29 7b 0a 20  n>etBUFSIZE ){. 
a4bc0 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d           bufpt =
a4bd0 20 7a 45 78 74 72 61 20 3d 20 73 71 6c 69 74 65   zExtra = sqlite
a4be0 33 4d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 20  3Malloc( n );.  
a4bf0 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 70          if( bufp
a4c00 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  t==0 ){.        
a4c10 20 20 20 20 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c      pAccum->mall
a4c20 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
a4c30 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
a4c40 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
a4c50 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
a4c60 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62         bufpt = b
a4c70 75 66 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  uf;.        }.  
a4c80 20 20 20 20 20 20 6a 20 3d 20 30 3b 0a 20 20 20        j = 0;.   
a4c90 20 20 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f       if( needQuo
a4ca0 74 65 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20  te ) bufpt[j++] 
a4cb0 3d 20 71 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  = q;.        for
a4cc0 28 69 3d 30 3b 20 28 63 68 3d 65 73 63 61 72 67  (i=0; (ch=escarg
a4cd0 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20  [i])!=0; i++){. 
a4ce0 20 20 20 20 20 20 20 20 20 62 75 66 70 74 5b 6a           bufpt[j
a4cf0 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 20 20 20  ++] = ch;.      
a4d00 20 20 20 20 69 66 28 20 63 68 3d 3d 71 20 29 20      if( ch==q ) 
a4d10 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b  bufpt[j++] = ch;
a4d20 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a4d30 20 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65     if( needQuote
a4d40 20 29 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20   ) bufpt[j++] = 
a4d50 71 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 74  q;.        bufpt
a4d60 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  [j] = 0;.       
a4d70 20 6c 65 6e 67 74 68 20 3d 20 6a 3b 0a 20 20 20   length = j;.   
a4d80 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 65 63       /* The prec
a4d90 69 73 69 6f 6e 20 69 73 20 69 67 6e 6f 72 65 64  ision is ignored
a4da0 20 6f 6e 20 25 71 20 61 6e 64 20 25 51 20 2a 2f   on %q and %Q */
a4db0 0a 20 20 20 20 20 20 20 20 2f 2a 20 69 66 28 20  .        /* if( 
a4dc0 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 26 26 20  precision>=0 && 
a4dd0 70 72 65 63 69 73 69 6f 6e 3c 6c 65 6e 67 74 68  precision<length
a4de0 20 29 20 6c 65 6e 67 74 68 20 3d 20 70 72 65 63   ) length = prec
a4df0 69 73 69 6f 6e 3b 20 2a 2f 0a 20 20 20 20 20 20  ision; */.      
a4e00 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
a4e10 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 54 4f  .      case etTO
a4e20 4b 45 4e 3a 20 7b 0a 20 20 20 20 20 20 20 20 54  KEN: {.        T
a4e30 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20 76  oken *pToken = v
a4e40 61 5f 61 72 67 28 61 70 2c 20 54 6f 6b 65 6e 2a  a_arg(ap, Token*
a4e50 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
a4e60 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 20  Token ){.       
a4e70 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
a4e80 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c  umAppend(pAccum,
a4e90 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 54   (const char*)pT
a4ea0 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f 6b 65 6e 2d  oken->z, pToken-
a4eb0 3e 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  >n);.        }. 
a4ec0 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20         length = 
a4ed0 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 20  width = 0;.     
a4ee0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
a4ef0 7d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53  }.      case etS
a4f00 52 43 4c 49 53 54 3a 20 7b 0a 20 20 20 20 20 20  RCLIST: {.      
a4f10 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 20    SrcList *pSrc 
a4f20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 53 72 63  = va_arg(ap, Src
a4f30 4c 69 73 74 2a 29 3b 0a 20 20 20 20 20 20 20 20  List*);.        
a4f40 69 6e 74 20 6b 20 3d 20 76 61 5f 61 72 67 28 61  int k = va_arg(a
a4f50 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20  p, int);.       
a4f60 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
a4f70 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70  item *pItem = &p
a4f80 53 72 63 2d 3e 61 5b 6b 5d 3b 0a 20 20 20 20 20  Src->a[k];.     
a4f90 20 20 20 61 73 73 65 72 74 28 20 6b 3e 3d 30 20     assert( k>=0 
a4fa0 26 26 20 6b 3c 70 53 72 63 2d 3e 6e 53 72 63 20  && k<pSrc->nSrc 
a4fb0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
a4fc0 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20  Item->zDatabase 
a4fd0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
a4fe0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
a4ff0 6e 64 28 70 41 63 63 75 6d 2c 20 70 49 74 65 6d  nd(pAccum, pItem
a5000 2d 3e 7a 44 61 74 61 62 61 73 65 2c 20 2d 31 29  ->zDatabase, -1)
a5010 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
a5020 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
a5030 64 28 70 41 63 63 75 6d 2c 20 22 2e 22 2c 20 31  d(pAccum, ".", 1
a5040 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
a5050 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41       sqlite3StrA
a5060 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75  ccumAppend(pAccu
a5070 6d 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c  m, pItem->zName,
a5080 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 6c 65   -1);.        le
a5090 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30  ngth = width = 0
a50a0 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
a50b0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64  .      }.      d
a50c0 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
a50d0 20 20 61 73 73 65 72 74 28 20 78 74 79 70 65 3d    assert( xtype=
a50e0 3d 65 74 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20  =etINVALID );.  
a50f0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
a5100 20 20 20 20 7d 0a 20 20 20 20 7d 2f 2a 20 45 6e      }.    }/* En
a5110 64 20 73 77 69 74 63 68 20 6f 76 65 72 20 74 68  d switch over th
a5120 65 20 66 6f 72 6d 61 74 20 74 79 70 65 20 2a 2f  e format type */
a5130 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54  .    /*.    ** T
a5140 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 63  he text of the c
a5150 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f 69  onversion is poi
a5160 6e 74 65 64 20 74 6f 20 62 79 20 22 62 75 66 70  nted to by "bufp
a5170 74 22 20 61 6e 64 20 69 73 0a 20 20 20 20 2a 2a  t" and is.    **
a5180 20 22 6c 65 6e 67 74 68 22 20 63 68 61 72 61 63   "length" charac
a5190 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65 20  ters long.  The 
a51a0 66 69 65 6c 64 20 77 69 64 74 68 20 69 73 20 22  field width is "
a51b0 77 69 64 74 68 22 2e 20 20 44 6f 0a 20 20 20 20  width".  Do.    
a51c0 2a 2a 20 74 68 65 20 6f 75 74 70 75 74 2e 0a 20  ** the output.. 
a51d0 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 66     */.    if( !f
a51e0 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20  lag_leftjustify 
a51f0 29 7b 0a 20 20 20 20 20 20 72 65 67 69 73 74 65  ){.      registe
a5200 72 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20 20  r int nspace;.  
a5210 20 20 20 20 6e 73 70 61 63 65 20 3d 20 77 69 64      nspace = wid
a5220 74 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20 20  th-length;.     
a5230 20 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29 7b   if( nspace>0 ){
a5240 0a 20 20 20 20 20 20 20 20 61 70 70 65 6e 64 53  .        appendS
a5250 70 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73 70  pace(pAccum, nsp
a5260 61 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ace);.      }.  
a5270 20 20 7d 0a 20 20 20 20 69 66 28 20 6c 65 6e 67    }.    if( leng
a5280 74 68 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71  th>0 ){.      sq
a5290 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
a52a0 65 6e 64 28 70 41 63 63 75 6d 2c 20 62 75 66 70  end(pAccum, bufp
a52b0 74 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 20 20 20  t, length);.    
a52c0 7d 0a 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c  }.    if( flag_l
a52d0 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20 20  eftjustify ){.  
a52e0 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74      register int
a52f0 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20 6e   nspace;.      n
a5300 73 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c 65  space = width-le
a5310 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28 20  ngth;.      if( 
a5320 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20 20  nspace>0 ){.    
a5330 20 20 20 20 61 70 70 65 6e 64 53 70 61 63 65 28      appendSpace(
a5340 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29 3b  pAccum, nspace);
a5350 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
a5360 20 20 20 69 66 28 20 7a 45 78 74 72 61 20 29 7b     if( zExtra ){
a5370 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
a5380 72 65 65 28 7a 45 78 74 72 61 29 3b 0a 20 20 20  ree(zExtra);.   
a5390 20 7d 0a 20 20 7d 2f 2a 20 45 6e 64 20 66 6f 72   }.  }/* End for
a53a0 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 66   loop over the f
a53b0 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a  ormat string */.
a53c0 7d 20 2f 2a 20 45 6e 64 20 6f 66 20 66 75 6e 63  } /* End of func
a53d0 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  tion */../*.** A
a53e0 70 70 65 6e 64 20 4e 20 62 79 74 65 73 20 6f 66  ppend N bytes of
a53f0 20 74 65 78 74 20 66 72 6f 6d 20 7a 20 74 6f 20   text from z to 
a5400 74 68 65 20 53 74 72 41 63 63 75 6d 20 6f 62 6a  the StrAccum obj
a5410 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ect..*/.SQLITE_P
a5420 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
a5430 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
a5440 64 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 63  d(StrAccum *p, c
a5450 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e  onst char *z, in
a5460 74 20 4e 29 7b 0a 20 20 61 73 73 65 72 74 28 20  t N){.  assert( 
a5470 7a 21 3d 30 20 7c 7c 20 4e 3d 3d 30 20 29 3b 0a  z!=0 || N==0 );.
a5480 20 20 69 66 28 20 70 2d 3e 74 6f 6f 42 69 67 20    if( p->tooBig 
a5490 7c 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  | p->mallocFaile
a54a0 64 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61 73  d ){.    testcas
a54b0 65 28 70 2d 3e 74 6f 6f 42 69 67 29 3b 0a 20 20  e(p->tooBig);.  
a54c0 20 20 74 65 73 74 63 61 73 65 28 70 2d 3e 6d 61    testcase(p->ma
a54d0 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 20 20 20  llocFailed);.   
a54e0 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
a54f0 66 28 20 4e 3c 30 20 29 7b 0a 20 20 20 20 4e 20  f( N<0 ){.    N 
a5500 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
a5510 30 28 7a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  0(z);.  }.  if( 
a5520 4e 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 7a 3d  N==0 || NEVER(z=
a5530 3d 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  =0) ){.    retur
a5540 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  n;.  }.  if( p->
a5550 6e 43 68 61 72 2b 4e 20 3e 3d 20 70 2d 3e 6e 41  nChar+N >= p->nA
a5560 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 63 68 61 72  lloc ){.    char
a5570 20 2a 7a 4e 65 77 3b 0a 20 20 20 20 69 66 28 20   *zNew;.    if( 
a5580 21 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 29 7b  !p->useMalloc ){
a5590 0a 20 20 20 20 20 20 70 2d 3e 74 6f 6f 42 69 67  .      p->tooBig
a55a0 20 3d 20 31 3b 0a 20 20 20 20 20 20 4e 20 3d 20   = 1;.      N = 
a55b0 70 2d 3e 6e 41 6c 6c 6f 63 20 2d 20 70 2d 3e 6e  p->nAlloc - p->n
a55c0 43 68 61 72 20 2d 20 31 3b 0a 20 20 20 20 20 20  Char - 1;.      
a55d0 69 66 28 20 4e 3c 3d 30 20 29 7b 0a 20 20 20 20  if( N<=0 ){.    
a55e0 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
a55f0 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
a5600 20 20 20 20 20 69 36 34 20 73 7a 4e 65 77 20 3d       i64 szNew =
a5610 20 70 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 20 20   p->nChar;.     
a5620 20 73 7a 4e 65 77 20 2b 3d 20 4e 20 2b 20 31 3b   szNew += N + 1;
a5630 0a 20 20 20 20 20 20 69 66 28 20 73 7a 4e 65 77  .      if( szNew
a5640 20 3e 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 29 7b   > p->mxAlloc ){
a5650 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
a5660 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 70 29  StrAccumReset(p)
a5670 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 74 6f 6f  ;.        p->too
a5680 42 69 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  Big = 1;.       
a5690 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
a56a0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d  else{.        p-
a56b0 3e 6e 41 6c 6c 6f 63 20 3d 20 28 69 6e 74 29 73  >nAlloc = (int)s
a56c0 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20  zNew;.      }.  
a56d0 20 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74      zNew = sqlit
a56e0 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d  e3DbMallocRaw(p-
a56f0 3e 64 62 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29  >db, p->nAlloc )
a5700 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 4e 65 77  ;.      if( zNew
a5710 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63   ){.        memc
a5720 70 79 28 7a 4e 65 77 2c 20 70 2d 3e 7a 54 65 78  py(zNew, p->zTex
a5730 74 2c 20 70 2d 3e 6e 43 68 61 72 29 3b 0a 20 20  t, p->nChar);.  
a5740 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
a5750 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a 20  AccumReset(p);. 
a5760 20 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74 20         p->zText 
a5770 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65  = zNew;.      }e
a5780 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  lse{.        p->
a5790 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
a57a0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
a57b0 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 70  3StrAccumReset(p
a57c0 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
a57d0 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  n;.      }.    }
a57e0 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26 70  .  }.  memcpy(&p
a57f0 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72  ->zText[p->nChar
a5800 5d 2c 20 7a 2c 20 4e 29 3b 0a 20 20 70 2d 3e 6e  ], z, N);.  p->n
a5810 43 68 61 72 20 2b 3d 20 4e 3b 0a 7d 0a 0a 2f 2a  Char += N;.}../*
a5820 0a 2a 2a 20 46 69 6e 69 73 68 20 6f 66 66 20 61  .** Finish off a
a5830 20 73 74 72 69 6e 67 20 62 79 20 6d 61 6b 69 6e   string by makin
a5840 67 20 73 75 72 65 20 69 74 20 69 73 20 7a 65 72  g sure it is zer
a5850 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2a  o-terminated..**
a5860 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
a5870 72 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74 69  r to the resulti
a5880 6e 67 20 73 74 72 69 6e 67 2e 20 20 52 65 74 75  ng string.  Retu
a5890 72 6e 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69  rn a NULL.** poi
a58a0 6e 74 65 72 20 69 66 20 61 6e 79 20 6b 69 6e 64  nter if any kind
a58b0 20 6f 66 20 65 72 72 6f 72 20 77 61 73 20 65 6e   of error was en
a58c0 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f 0a 53 51  countered..*/.SQ
a58d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
a58e0 72 20 2a 73 71 6c 69 74 65 33 53 74 72 41 63 63  r *sqlite3StrAcc
a58f0 75 6d 46 69 6e 69 73 68 28 53 74 72 41 63 63 75  umFinish(StrAccu
a5900 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e  m *p){.  if( p->
a5910 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 70 2d 3e  zText ){.    p->
a5920 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d 20  zText[p->nChar] 
a5930 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  = 0;.    if( p->
a5940 75 73 65 4d 61 6c 6c 6f 63 20 26 26 20 70 2d 3e  useMalloc && p->
a5950 7a 54 65 78 74 3d 3d 70 2d 3e 7a 42 61 73 65 20  zText==p->zBase 
a5960 29 7b 0a 20 20 20 20 20 20 70 2d 3e 7a 54 65 78  ){.      p->zTex
a5970 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  t = sqlite3DbMal
a5980 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 70 2d  locRaw(p->db, p-
a5990 3e 6e 43 68 61 72 2b 31 20 29 3b 0a 20 20 20 20  >nChar+1 );.    
a59a0 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74 20 29    if( p->zText )
a59b0 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  {.        memcpy
a59c0 28 70 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 7a 42  (p->zText, p->zB
a59d0 61 73 65 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 29  ase, p->nChar+1)
a59e0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
a59f0 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63         p->malloc
a5a00 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
a5a10 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
a5a20 72 65 74 75 72 6e 20 70 2d 3e 7a 54 65 78 74 3b  return p->zText;
a5a30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20  .}../*.** Reset 
a5a40 61 6e 20 53 74 72 41 63 63 75 6d 20 73 74 72 69  an StrAccum stri
a5a50 6e 67 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c  ng.  Reclaim all
a5a60 20 6d 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79   malloced memory
a5a70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
a5a80 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
a5a90 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 53 74  StrAccumReset(St
a5aa0 72 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69 66  rAccum *p){.  if
a5ab0 28 20 70 2d 3e 7a 54 65 78 74 21 3d 70 2d 3e 7a  ( p->zText!=p->z
a5ac0 42 61 73 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  Base ){.    sqli
a5ad0 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c  te3DbFree(p->db,
a5ae0 20 70 2d 3e 7a 54 65 78 74 29 3b 0a 20 20 7d 0a   p->zText);.  }.
a5af0 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 30 3b 0a    p->zText = 0;.
a5b00 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  }../*.** Initial
a5b10 69 7a 65 20 61 20 73 74 72 69 6e 67 20 61 63 63  ize a string acc
a5b20 75 6d 75 6c 61 74 6f 72 0a 2a 2f 0a 53 51 4c 49  umulator.*/.SQLI
a5b30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
a5b40 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49  sqlite3StrAccumI
a5b50 6e 69 74 28 53 74 72 41 63 63 75 6d 20 2a 70 2c  nit(StrAccum *p,
a5b60 20 63 68 61 72 20 2a 7a 42 61 73 65 2c 20 69 6e   char *zBase, in
a5b70 74 20 6e 2c 20 69 6e 74 20 6d 78 29 7b 0a 20 20  t n, int mx){.  
a5b80 70 2d 3e 7a 54 65 78 74 20 3d 20 70 2d 3e 7a 42  p->zText = p->zB
a5b90 61 73 65 20 3d 20 7a 42 61 73 65 3b 0a 20 20 70  ase = zBase;.  p
a5ba0 2d 3e 64 62 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e  ->db = 0;.  p->n
a5bb0 43 68 61 72 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e  Char = 0;.  p->n
a5bc0 41 6c 6c 6f 63 20 3d 20 6e 3b 0a 20 20 70 2d 3e  Alloc = n;.  p->
a5bd0 6d 78 41 6c 6c 6f 63 20 3d 20 6d 78 3b 0a 20 20  mxAlloc = mx;.  
a5be0 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 31  p->useMalloc = 1
a5bf0 3b 0a 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20  ;.  p->tooBig = 
a5c00 30 3b 0a 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61  0;.  p->mallocFa
a5c10 69 6c 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  iled = 0;.}../*.
a5c20 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65  ** Print into me
a5c30 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
a5c40 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  om sqliteMalloc(
a5c50 29 2e 20 20 55 73 65 20 74 68 65 20 69 6e 74 65  ).  Use the inte
a5c60 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72  rnal.** %-conver
a5c70 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e  sion extensions.
a5c80 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a5c90 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
a5ca0 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65 33  VMPrintf(sqlite3
a5cb0 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *db, const char
a5cc0 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69   *zFormat, va_li
a5cd0 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a  st ap){.  char *
a5ce0 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b  z;.  char zBase[
a5cf0 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46  SQLITE_PRINT_BUF
a5d00 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63  _SIZE];.  StrAcc
a5d10 75 6d 20 61 63 63 3b 0a 20 20 61 73 73 65 72 74  um acc;.  assert
a5d20 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 73 71 6c  ( db!=0 );.  sql
a5d30 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74  ite3StrAccumInit
a5d40 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 73 69  (&acc, zBase, si
a5d50 7a 65 6f 66 28 7a 42 61 73 65 29 2c 0a 20 20 20  zeof(zBase),.   
a5d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5d70 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51     db->aLimit[SQ
a5d80 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
a5d90 48 5d 29 3b 0a 20 20 61 63 63 2e 64 62 20 3d 20  H]);.  acc.db = 
a5da0 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50  db;.  sqlite3VXP
a5db0 72 69 6e 74 66 28 26 61 63 63 2c 20 31 2c 20 7a  rintf(&acc, 1, z
a5dc0 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 7a  Format, ap);.  z
a5dd0 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41 63 63   = sqlite3StrAcc
a5de0 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b 0a  umFinish(&acc);.
a5df0 20 20 69 66 28 20 61 63 63 2e 6d 61 6c 6c 6f 63    if( acc.malloc
a5e00 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 64 62  Failed ){.    db
a5e10 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
a5e20 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   1;.  }.  return
a5e30 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69   z;.}../*.** Pri
a5e40 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f  nt into memory o
a5e50 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
a5e60 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 55 73  iteMalloc().  Us
a5e70 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a  e the internal.*
a5e80 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65  * %-conversion e
a5e90 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51  xtensions..*/.SQ
a5ea0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
a5eb0 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 69 6e 74  r *sqlite3MPrint
a5ec0 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  f(sqlite3 *db, c
a5ed0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
a5ee0 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c  at, ...){.  va_l
a5ef0 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a  ist ap;.  char *
a5f00 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70  z;.  va_start(ap
a5f10 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20  , zFormat);.  z 
a5f20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74  = sqlite3VMPrint
a5f30 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61  f(db, zFormat, a
a5f40 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29  p);.  va_end(ap)
a5f50 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a  ;.  return z;.}.
a5f60 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 73 71 6c 69  ./*.** Like sqli
a5f70 74 65 33 4d 50 72 69 6e 74 66 28 29 2c 20 62 75  te3MPrintf(), bu
a5f80 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62  t call sqlite3Db
a5f90 46 72 65 65 28 29 20 6f 6e 20 7a 53 74 72 20 61  Free() on zStr a
a5fa0 66 74 65 72 20 66 6f 72 6d 61 74 74 69 6e 67 0a  fter formatting.
a5fb0 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 61 6e  ** the string an
a5fc0 64 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 6e  d before returnn
a5fd0 69 6e 67 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ing.  This routi
a5fe0 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74  ne is intended t
a5ff0 6f 20 62 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20  o be used.** to 
a6000 6d 6f 64 69 66 79 20 61 6e 20 65 78 69 73 74 69  modify an existi
a6010 6e 67 20 73 74 72 69 6e 67 2e 20 20 46 6f 72 20  ng string.  For 
a6020 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20  example:.**.**  
a6030 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 65 33       x = sqlite3
a6040 4d 50 72 69 6e 74 66 28 64 62 2c 20 78 2c 20 22  MPrintf(db, x, "
a6050 70 72 65 66 69 78 20 25 73 20 73 75 66 66 69 78  prefix %s suffix
a6060 22 2c 20 78 29 3b 0a 2a 2a 0a 2a 2f 0a 53 51 4c  ", x);.**.*/.SQL
a6070 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
a6080 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64   *sqlite3MAppend
a6090 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  f(sqlite3 *db, c
a60a0 68 61 72 20 2a 7a 53 74 72 2c 20 63 6f 6e 73 74  har *zStr, const
a60b0 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20   char *zFormat, 
a60c0 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
a60d0 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20  ap;.  char *z;. 
a60e0 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46   va_start(ap, zF
a60f0 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71  ormat);.  z = sq
a6100 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62  lite3VMPrintf(db
a6110 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  , zFormat, ap);.
a6120 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
a6130 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
a6140 2c 20 7a 53 74 72 29 3b 0a 20 20 72 65 74 75 72  , zStr);.  retur
a6150 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72  n z;.}../*.** Pr
a6160 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20  int into memory 
a6170 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
a6180 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20  lite3_malloc(). 
a6190 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65 72 6e   Omit the intern
a61a0 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69  al.** %-conversi
a61b0 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a  on extensions..*
a61c0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61  /.SQLITE_API cha
a61d0 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 72 69  r *sqlite3_vmpri
a61e0 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ntf(const char *
a61f0 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74  zFormat, va_list
a6200 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b   ap){.  char *z;
a6210 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b 53 51  .  char zBase[SQ
a6220 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53  LITE_PRINT_BUF_S
a6230 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 75 6d  IZE];.  StrAccum
a6240 20 61 63 63 3b 0a 23 69 66 6e 64 65 66 20 53 51   acc;.#ifndef SQ
a6250 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
a6260 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  IT.  if( sqlite3
a6270 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20  _initialize() ) 
a6280 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66  return 0;.#endif
a6290 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63  .  sqlite3StrAcc
a62a0 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 61  umInit(&acc, zBa
a62b0 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 73 65  se, sizeof(zBase
a62c0 29 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45  ), SQLITE_MAX_LE
a62d0 4e 47 54 48 29 3b 0a 20 20 73 71 6c 69 74 65 33  NGTH);.  sqlite3
a62e0 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20 30  VXPrintf(&acc, 0
a62f0 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  , zFormat, ap);.
a6300 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72    z = sqlite3Str
a6310 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63  AccumFinish(&acc
a6320 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d  );.  return z;.}
a6330 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e  ../*.** Print in
a6340 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  to memory obtain
a6350 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ed from sqlite3_
a6360 6d 61 6c 6c 6f 63 28 29 28 29 2e 20 20 4f 6d 69  malloc()().  Omi
a6370 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a  t the internal.*
a6380 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65  * %-conversion e
a6390 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51  xtensions..*/.SQ
a63a0 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73  LITE_API char *s
a63b0 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 63  qlite3_mprintf(c
a63c0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
a63d0 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c  at, ...){.  va_l
a63e0 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a  ist ap;.  char *
a63f0 7a 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  z;.#ifndef SQLIT
a6400 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a  E_OMIT_AUTOINIT.
a6410 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e    if( sqlite3_in
a6420 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74  itialize() ) ret
a6430 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20  urn 0;.#endif.  
a6440 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f  va_start(ap, zFo
a6450 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c  rmat);.  z = sql
a6460 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 7a 46  ite3_vmprintf(zF
a6470 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61  ormat, ap);.  va
a6480 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75  _end(ap);.  retu
a6490 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73  rn z;.}../*.** s
a64a0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
a64b0 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 6e 70  ) works like snp
a64c0 72 69 6e 74 66 28 29 20 65 78 63 65 70 74 20 74  rintf() except t
a64d0 68 61 74 20 69 74 20 69 67 6e 6f 72 65 73 20 74  hat it ignores t
a64e0 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c 6f  he.** current lo
a64f0 63 61 6c 65 20 73 65 74 74 69 6e 67 73 2e 20 20  cale settings.  
a6500 54 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61 6e  This is importan
a6510 74 20 66 6f 72 20 53 51 4c 69 74 65 20 62 65 63  t for SQLite bec
a6520 61 75 73 65 20 77 65 0a 2a 2a 20 61 72 65 20 6e  ause we.** are n
a6530 6f 74 20 61 62 6c 65 20 74 6f 20 75 73 65 20 61  ot able to use a
a6540 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 63 69   "," as the deci
a6550 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 20 70 6c 61  mal point in pla
a6560 63 65 20 6f 66 20 22 2e 22 20 61 73 0a 2a 2a 20  ce of "." as.** 
a6570 73 70 65 63 69 66 69 65 64 20 62 79 20 73 6f 6d  specified by som
a6580 65 20 6c 6f 63 61 6c 65 73 2e 0a 2a 2f 0a 53 51  e locales..*/.SQ
a6590 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73  LITE_API char *s
a65a0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
a65b0 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a 42 75  int n, char *zBu
a65c0 66 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  f, const char *z
a65d0 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20  Format, ...){.  
a65e0 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 6c 69  char *z;.  va_li
a65f0 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63 63 75  st ap;.  StrAccu
a6600 6d 20 61 63 63 3b 0a 0a 20 20 69 66 28 20 6e 3c  m acc;..  if( n<
a6610 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
a6620 20 7a 42 75 66 3b 0a 20 20 7d 0a 20 20 73 71 6c   zBuf;.  }.  sql
a6630 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74  ite3StrAccumInit
a6640 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 6e 2c 20  (&acc, zBuf, n, 
a6650 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 6c  0);.  acc.useMal
a6660 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74  loc = 0;.  va_st
a6670 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b  art(ap,zFormat);
a6680 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e  .  sqlite3VXPrin
a6690 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72  tf(&acc, 0, zFor
a66a0 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65  mat, ap);.  va_e
a66b0 6e 64 28 61 70 29 3b 0a 20 20 7a 20 3d 20 73 71  nd(ap);.  z = sq
a66c0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e  lite3StrAccumFin
a66d0 69 73 68 28 26 61 63 63 29 3b 0a 20 20 72 65 74  ish(&acc);.  ret
a66e0 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 20 64 65  urn z;.}..#if de
a66f0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
a6700 55 47 29 0a 2f 2a 0a 2a 2a 20 41 20 76 65 72 73  UG)./*.** A vers
a6710 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66 28 29 20  ion of printf() 
a6720 74 68 61 74 20 75 6e 64 65 72 73 74 61 6e 64 73  that understands
a6730 20 25 6c 6c 64 2e 20 20 55 73 65 64 20 66 6f 72   %lld.  Used for
a6740 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2a 20 54   debugging..** T
a6750 68 65 20 70 72 69 6e 74 66 28 29 20 62 75 69 6c  he printf() buil
a6760 74 20 69 6e 74 6f 20 73 6f 6d 65 20 76 65 72 73  t into some vers
a6770 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73 20  ions of windows 
a6780 64 6f 65 73 20 6e 6f 74 20 75 6e 64 65 72 73 74  does not underst
a6790 61 6e 64 20 25 6c 6c 64 0a 2a 2a 20 61 6e 64 20  and %lld.** and 
a67a0 73 65 67 66 61 75 6c 74 73 20 69 66 20 79 6f 75  segfaults if you
a67b0 20 67 69 76 65 20 69 74 20 61 20 6c 6f 6e 67 20   give it a long 
a67c0 6c 6f 6e 67 20 69 6e 74 2e 0a 2a 2f 0a 53 51 4c  long int..*/.SQL
a67d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
a67e0 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
a67f0 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ntf(const char *
a6800 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20  zFormat, ...){. 
a6810 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 53   va_list ap;.  S
a6820 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 20 63  trAccum acc;.  c
a6830 68 61 72 20 7a 42 75 66 5b 35 30 30 5d 3b 0a 20  har zBuf[500];. 
a6840 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
a6850 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75 66 2c  Init(&acc, zBuf,
a6860 20 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 30   sizeof(zBuf), 0
a6870 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 6c 6c  );.  acc.useMall
a6880 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61  oc = 0;.  va_sta
a6890 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a  rt(ap,zFormat);.
a68a0 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74    sqlite3VXPrint
a68b0 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d  f(&acc, 0, zForm
a68c0 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e  at, ap);.  va_en
a68d0 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  d(ap);.  sqlite3
a68e0 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26  StrAccumFinish(&
a68f0 61 63 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28  acc);.  fprintf(
a6900 73 74 64 6f 75 74 2c 22 25 73 22 2c 20 7a 42 75  stdout,"%s", zBu
a6910 66 29 3b 0a 20 20 66 66 6c 75 73 68 28 73 74 64  f);.  fflush(std
a6920 6f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  out);.}.#endif..
a6930 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
a6940 45 6e 64 20 6f 66 20 70 72 69 6e 74 66 2e 63 20  End of printf.c 
a6950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
a6980 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
a6990 42 65 67 69 6e 20 66 69 6c 65 20 72 61 6e 64 6f  Begin file rando
a69a0 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  m.c ************
a69b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a69c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
a69d0 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
a69e0 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
a69f0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
a6a00 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
a6a10 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
a6a20 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
a6a30 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
a6a40 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
a6a50 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
a6a60 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
a6a70 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
a6a80 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
a6a90 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
a6aa0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
a6ab0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
a6ac0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
a6ad0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
a6ae0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
a6af0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
a6b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6b40 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
a6b50 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64  ile contains cod
a6b60 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61  e to implement a
a6b70 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e   pseudo-random n
a6b80 75 6d 62 65 72 0a 2a 2a 20 67 65 6e 65 72 61 74  umber.** generat
a6b90 6f 72 20 28 50 52 4e 47 29 20 66 6f 72 20 53 51  or (PRNG) for SQ
a6ba0 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 61 6e 64  Lite..**.** Rand
a6bb0 6f 6d 20 6e 75 6d 62 65 72 73 20 61 72 65 20 75  om numbers are u
a6bc0 73 65 64 20 62 79 20 73 6f 6d 65 20 6f 66 20 74  sed by some of t
a6bd0 68 65 20 64 61 74 61 62 61 73 65 20 62 61 63 6b  he database back
a6be0 65 6e 64 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a  ends in order.**
a6bf0 20 74 6f 20 67 65 6e 65 72 61 74 65 20 72 61 6e   to generate ran
a6c00 64 6f 6d 20 69 6e 74 65 67 65 72 20 6b 65 79 73  dom integer keys
a6c10 20 66 6f 72 20 74 61 62 6c 65 73 20 6f 72 20 72   for tables or r
a6c20 61 6e 64 6f 6d 20 66 69 6c 65 6e 61 6d 65 73 2e  andom filenames.
a6c30 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72 61 6e 64  .**.** $Id: rand
a6c40 6f 6d 2e 63 2c 76 20 31 2e 32 39 20 32 30 30 38  om.c,v 1.29 2008
a6c50 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 32 34 20  /12/10 19:26:24 
a6c60 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f  drh Exp $.*/.../
a6c70 2a 20 41 6c 6c 20 74 68 72 65 61 64 73 20 73 68  * All threads sh
a6c80 61 72 65 20 61 20 73 69 6e 67 6c 65 20 72 61 6e  are a single ran
a6c90 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 72  dom number gener
a6ca0 61 74 6f 72 2e 0a 2a 2a 20 54 68 69 73 20 73 74  ator..** This st
a6cb0 72 75 63 74 75 72 65 20 69 73 20 74 68 65 20 63  ructure is the c
a6cc0 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20  urrent state of 
a6cd0 74 68 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a  the generator..*
a6ce0 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f  /.static SQLITE_
a6cf0 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69 74  WSD struct sqlit
a6d00 65 33 50 72 6e 67 54 79 70 65 20 7b 0a 20 20 75  e3PrngType {.  u
a6d10 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 73 49  nsigned char isI
a6d20 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  nit;          /*
a6d30 20 54 72 75 65 20 69 66 20 69 6e 69 74 69 61 6c   True if initial
a6d40 69 7a 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  ized */.  unsign
a6d50 65 64 20 63 68 61 72 20 69 2c 20 6a 3b 20 20 20  ed char i, j;   
a6d60 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74           /* Stat
a6d70 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20  e variables */. 
a6d80 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73   unsigned char s
a6d90 5b 32 35 36 5d 3b 20 20 20 20 20 20 20 20 20 20  [256];          
a6da0 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61 62 6c  /* State variabl
a6db0 65 73 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 50  es */.} sqlite3P
a6dc0 72 6e 67 3b 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20  rng;../*.** Get 
a6dd0 61 20 73 69 6e 67 6c 65 20 38 2d 62 69 74 20 72  a single 8-bit r
a6de0 61 6e 64 6f 6d 20 76 61 6c 75 65 20 66 72 6f 6d  andom value from
a6df0 20 74 68 65 20 52 43 34 20 50 52 4e 47 2e 20 20   the RC4 PRNG.  
a6e00 54 68 65 20 4d 75 74 65 78 0a 2a 2a 20 6d 75 73  The Mutex.** mus
a6e10 74 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65 20  t be held while 
a6e20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 72  executing this r
a6e30 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 57 68  outine..**.** Wh
a6e40 79 20 6e 6f 74 20 6a 75 73 74 20 75 73 65 20 61  y not just use a
a6e50 20 6c 69 62 72 61 72 79 20 72 61 6e 64 6f 6d 20   library random 
a6e60 67 65 6e 65 72 61 74 6f 72 20 6c 69 6b 65 20 6c  generator like l
a6e70 72 61 6e 64 34 38 28 29 20 66 6f 72 20 74 68 69  rand48() for thi
a6e80 73 3f 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68  s?.** Because th
a6e90 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 6f 70  e OP_NewRowid op
a6ea0 63 6f 64 65 20 69 6e 20 74 68 65 20 56 44 42 45  code in the VDBE
a6eb0 20 64 65 70 65 6e 64 73 20 6f 6e 20 68 61 76 69   depends on havi
a6ec0 6e 67 20 61 20 76 65 72 79 0a 2a 2a 20 67 6f 6f  ng a very.** goo
a6ed0 64 20 73 6f 75 72 63 65 20 6f 66 20 72 61 6e 64  d source of rand
a6ee0 6f 6d 20 6e 75 6d 62 65 72 73 2e 20 20 54 68 65  om numbers.  The
a6ef0 20 6c 72 61 6e 64 34 38 28 29 20 6c 69 62 72 61   lrand48() libra
a6f00 72 79 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 0a  ry function may.
a6f10 2a 2a 20 77 65 6c 6c 20 62 65 20 67 6f 6f 64 20  ** well be good 
a6f20 65 6e 6f 75 67 68 2e 20 20 42 75 74 20 6d 61 79  enough.  But may
a6f30 62 65 20 6e 6f 74 2e 20 20 4f 72 20 6d 61 79 62  be not.  Or mayb
a6f40 65 20 6c 72 61 6e 64 34 38 28 29 20 68 61 73 20  e lrand48() has 
a6f50 73 6f 6d 65 0a 2a 2a 20 73 75 62 74 6c 65 20 70  some.** subtle p
a6f60 72 6f 62 6c 65 6d 73 20 6f 6e 20 73 6f 6d 65 20  roblems on some 
a6f70 73 79 73 74 65 6d 73 20 74 68 61 74 20 63 6f 75  systems that cou
a6f80 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c 65 6d  ld cause problem
a6f90 73 2e 20 20 49 74 20 69 73 20 68 61 72 64 0a 2a  s.  It is hard.*
a6fa0 2a 20 74 6f 20 6b 6e 6f 77 2e 20 20 54 6f 20 6d  * to know.  To m
a6fb0 69 6e 69 6d 69 7a 65 20 74 68 65 20 72 69 73 6b  inimize the risk
a6fc0 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 64 75 65   of problems due
a6fd0 20 74 6f 20 62 61 64 20 6c 72 61 6e 64 34 38 28   to bad lrand48(
a6fe0 29 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ).** implementat
a6ff0 69 6f 6e 73 2c 20 53 51 4c 69 74 65 20 75 73 65  ions, SQLite use
a7000 73 20 74 68 69 73 20 72 61 6e 64 6f 6d 20 6e 75  s this random nu
a7010 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 62  mber generator b
a7020 61 73 65 64 0a 2a 2a 20 6f 6e 20 52 43 34 2c 20  ased.** on RC4, 
a7030 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20 77 6f  which we know wo
a7040 72 6b 73 20 76 65 72 79 20 77 65 6c 6c 2e 0a 2a  rks very well..*
a7050 2a 0a 2a 2a 20 28 4c 61 74 65 72 29 3a 20 20 41  *.** (Later):  A
a7060 63 74 75 61 6c 6c 79 2c 20 4f 50 5f 4e 65 77 52  ctually, OP_NewR
a7070 6f 77 69 64 20 64 6f 65 73 20 6e 6f 74 20 64 65  owid does not de
a7080 70 65 6e 64 20 6f 6e 20 61 20 67 6f 6f 64 20 73  pend on a good s
a7090 6f 75 72 63 65 20 6f 66 0a 2a 2a 20 72 61 6e 64  ource of.** rand
a70a0 6f 6d 6e 65 73 73 20 61 6e 79 20 6d 6f 72 65 2e  omness any more.
a70b0 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20 6c 65    But we will le
a70c0 61 76 65 20 74 68 69 73 20 63 6f 64 65 20 69 6e  ave this code in
a70d0 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 2e 0a 2a   all the same..*
a70e0 2f 0a 73 74 61 74 69 63 20 75 38 20 72 61 6e 64  /.static u8 rand
a70f0 6f 6d 42 79 74 65 28 76 6f 69 64 29 7b 0a 20 20  omByte(void){.  
a7100 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 3b  unsigned char t;
a7110 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 22 77 73 64  ...  /* The "wsd
a7120 50 72 6e 67 22 20 6d 61 63 72 6f 20 77 69 6c 6c  Prng" macro will
a7130 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20   resolve to the 
a7140 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75  pseudo-random nu
a7150 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 0a 20  mber generator. 
a7160 20 2a 2a 20 73 74 61 74 65 20 76 65 63 74 6f 72   ** state vector
a7170 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 73  .  If writable s
a7180 74 61 74 69 63 20 64 61 74 61 20 69 73 20 75 6e  tatic data is un
a7190 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65  supported on the
a71a0 20 74 61 72 67 65 74 2c 0a 20 20 2a 2a 20 77 65   target,.  ** we
a71b0 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 20   have to locate 
a71c0 74 68 65 20 73 74 61 74 65 20 76 65 63 74 6f 72  the state vector
a71d0 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49   at run-time.  I
a71e0 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f  n the more commo
a71f0 6e 0a 20 20 2a 2a 20 63 61 73 65 20 77 68 65 72  n.  ** case wher
a7200 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69  e writable stati
a7210 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f 72  c data is suppor
a7220 74 65 64 2c 20 77 73 64 50 72 6e 67 20 63 61 6e  ted, wsdPrng can
a7230 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79 0a   refer directly.
a7240 20 20 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c    ** to the "sql
a7250 69 74 65 33 50 72 6e 67 22 20 73 74 61 74 65 20  ite3Prng" state 
a7260 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20  vector declared 
a7270 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a 23 69 66 64  above..  */.#ifd
a7280 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57  ef SQLITE_OMIT_W
a7290 53 44 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69  SD.  struct sqli
a72a0 74 65 33 50 72 6e 67 54 79 70 65 20 2a 70 20 3d  te3PrngType *p =
a72b0 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20   &GLOBAL(struct 
a72c0 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c  sqlite3PrngType,
a72d0 20 73 71 6c 69 74 65 33 50 72 6e 67 29 3b 0a 23   sqlite3Prng);.#
a72e0 20 64 65 66 69 6e 65 20 77 73 64 50 72 6e 67 20   define wsdPrng 
a72f0 70 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66  p[0].#else.# def
a7300 69 6e 65 20 77 73 64 50 72 6e 67 20 73 71 6c 69  ine wsdPrng sqli
a7310 74 65 33 50 72 6e 67 0a 23 65 6e 64 69 66 0a 0a  te3Prng.#endif..
a7320 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  .  /* Initialize
a7330 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68   the state of th
a7340 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20  e random number 
a7350 67 65 6e 65 72 61 74 6f 72 20 6f 6e 63 65 2c 0a  generator once,.
a7360 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74    ** the first t
a7370 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ime this routine
a7380 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 65   is called.  The
a7390 20 73 65 65 64 20 76 61 6c 75 65 20 64 6f 65 73   seed value does
a73a0 0a 20 20 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74  .  ** not need t
a73b0 6f 20 63 6f 6e 74 61 69 6e 20 61 20 6c 6f 74 20  o contain a lot 
a73c0 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 73 69  of randomness si
a73d0 6e 63 65 20 77 65 20 61 72 65 20 6e 6f 74 0a 20  nce we are not. 
a73e0 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 64 6f   ** trying to do
a73f0 20 73 65 63 75 72 65 20 65 6e 63 72 79 70 74 69   secure encrypti
a7400 6f 6e 20 6f 72 20 61 6e 79 74 68 69 6e 67 20 6c  on or anything l
a7410 69 6b 65 20 74 68 61 74 2e 2e 2e 0a 20 20 2a 2a  ike that....  **
a7420 0a 20 20 2a 2a 20 4e 6f 74 68 69 6e 67 20 69 6e  .  ** Nothing in
a7430 20 74 68 69 73 20 66 69 6c 65 20 6f 72 20 61 6e   this file or an
a7440 79 77 68 65 72 65 20 65 6c 73 65 20 69 6e 20 53  ywhere else in S
a7450 51 4c 69 74 65 20 64 6f 65 73 20 61 6e 79 20 6b  QLite does any k
a7460 69 6e 64 20 6f 66 0a 20 20 2a 2a 20 65 6e 63 72  ind of.  ** encr
a7470 79 70 74 69 6f 6e 2e 20 20 54 68 65 20 52 43 34  yption.  The RC4
a7480 20 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 62 65   algorithm is be
a7490 69 6e 67 20 75 73 65 64 20 61 73 20 61 20 50 52  ing used as a PR
a74a0 4e 47 20 28 70 73 65 75 64 6f 2d 72 61 6e 64 6f  NG (pseudo-rando
a74b0 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67 65  m.  ** number ge
a74c0 6e 65 72 61 74 6f 72 29 20 6e 6f 74 20 61 73 20  nerator) not as 
a74d0 61 6e 20 65 6e 63 72 79 70 74 69 6f 6e 20 64 65  an encryption de
a74e0 76 69 63 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  vice..  */.  if(
a74f0 20 21 77 73 64 50 72 6e 67 2e 69 73 49 6e 69 74   !wsdPrng.isInit
a7500 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
a7510 20 20 20 63 68 61 72 20 6b 5b 32 35 36 5d 3b 0a     char k[256];.
a7520 20 20 20 20 77 73 64 50 72 6e 67 2e 6a 20 3d 20      wsdPrng.j = 
a7530 30 3b 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 69  0;.    wsdPrng.i
a7540 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 0;.    sqlite
a7550 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71  3OsRandomness(sq
a7560 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 30  lite3_vfs_find(0
a7570 29 2c 20 32 35 36 2c 20 6b 29 3b 0a 20 20 20 20  ), 256, k);.    
a7580 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20  for(i=0; i<256; 
a7590 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77 73 64 50  i++){.      wsdP
a75a0 72 6e 67 2e 73 5b 69 5d 20 3d 20 28 75 38 29 69  rng.s[i] = (u8)i
a75b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  ;.    }.    for(
a75c0 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b 2b 29  i=0; i<256; i++)
a75d0 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e  {.      wsdPrng.
a75e0 6a 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73 5b 69  j += wsdPrng.s[i
a75f0 5d 20 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20 20 20  ] + k[i];.      
a7600 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73  t = wsdPrng.s[ws
a7610 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20 20 20  dPrng.j];.      
a7620 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e  wsdPrng.s[wsdPrn
a7630 67 2e 6a 5d 20 3d 20 77 73 64 50 72 6e 67 2e 73  g.j] = wsdPrng.s
a7640 5b 69 5d 3b 0a 20 20 20 20 20 20 77 73 64 50 72  [i];.      wsdPr
a7650 6e 67 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20 20 20  ng.s[i] = t;.   
a7660 20 7d 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 69   }.    wsdPrng.i
a7670 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a  sInit = 1;.  }..
a7680 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 6e    /* Generate an
a7690 64 20 72 65 74 75 72 6e 20 73 69 6e 67 6c 65 20  d return single 
a76a0 72 61 6e 64 6f 6d 20 62 79 74 65 0a 20 20 2a 2f  random byte.  */
a76b0 0a 20 20 77 73 64 50 72 6e 67 2e 69 2b 2b 3b 0a  .  wsdPrng.i++;.
a76c0 20 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b    t = wsdPrng.s[
a76d0 77 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 77 73  wsdPrng.i];.  ws
a76e0 64 50 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a 20 20  dPrng.j += t;.  
a76f0 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e  wsdPrng.s[wsdPrn
a7700 67 2e 69 5d 20 3d 20 77 73 64 50 72 6e 67 2e 73  g.i] = wsdPrng.s
a7710 5b 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 77  [wsdPrng.j];.  w
a7720 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67  sdPrng.s[wsdPrng
a7730 2e 6a 5d 20 3d 20 74 3b 0a 20 20 74 20 2b 3d 20  .j] = t;.  t += 
a7740 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e  wsdPrng.s[wsdPrn
a7750 67 2e 69 5d 3b 0a 20 20 72 65 74 75 72 6e 20 77  g.i];.  return w
a7760 73 64 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a 0a  sdPrng.s[t];.}..
a7770 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 20 72  /*.** Return N r
a7780 61 6e 64 6f 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a  andom bytes..*/.
a7790 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
a77a0 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
a77b0 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a  ss(int N, void *
a77c0 70 42 75 66 29 7b 0a 20 20 75 6e 73 69 67 6e 65  pBuf){.  unsigne
a77d0 64 20 63 68 61 72 20 2a 7a 42 75 66 20 3d 20 70  d char *zBuf = p
a77e0 42 75 66 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f  Buf;.#if SQLITE_
a77f0 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c  THREADSAFE.  sql
a7800 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
a7810 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78  x = sqlite3Mutex
a7820 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
a7830 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 29 3b  EX_STATIC_PRNG);
a7840 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65  .#endif.  sqlite
a7850 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75  3_mutex_enter(mu
a7860 74 65 78 29 3b 0a 20 20 77 68 69 6c 65 28 20 4e  tex);.  while( N
a7870 2d 2d 20 29 7b 0a 20 20 20 20 2a 28 7a 42 75 66  -- ){.    *(zBuf
a7880 2b 2b 29 20 3d 20 72 61 6e 64 6f 6d 42 79 74 65  ++) = randomByte
a7890 28 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ();.  }.  sqlite
a78a0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75  3_mutex_leave(mu
a78b0 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  tex);.}..#ifndef
a78c0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49   SQLITE_OMIT_BUI
a78d0 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20  LTIN_TEST./*.** 
a78e0 46 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 70  For testing purp
a78f0 6f 73 65 73 2c 20 77 65 20 73 6f 6d 65 74 69 6d  oses, we sometim
a7900 65 73 20 77 61 6e 74 20 74 6f 20 70 72 65 73 65  es want to prese
a7910 72 76 65 20 74 68 65 20 73 74 61 74 65 20 6f 66  rve the state of
a7920 0a 2a 2a 20 50 52 4e 47 20 61 6e 64 20 72 65 73  .** PRNG and res
a7930 74 6f 72 65 20 74 68 65 20 50 52 4e 47 20 74 6f  tore the PRNG to
a7940 20 69 74 73 20 73 61 76 65 64 20 73 74 61 74 65   its saved state
a7950 20 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65   at a later time
a7960 2c 20 6f 72 0a 2a 2a 20 74 6f 20 72 65 73 65 74  , or.** to reset
a7970 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69 74 73   the PRNG to its
a7980 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2e 20   initial state. 
a7990 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
a79a0 61 63 63 6f 6d 70 6c 69 73 68 0a 2a 2a 20 74 68  accomplish.** th
a79b0 6f 73 65 20 74 61 73 6b 73 2e 0a 2a 2a 0a 2a 2a  ose tasks..**.**
a79c0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 74 65 73   The sqlite3_tes
a79d0 74 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65  t_control() inte
a79e0 72 66 61 63 65 20 63 61 6c 6c 73 20 74 68 65 73  rface calls thes
a79f0 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a  e routines to.**
a7a00 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 50 52 4e   control the PRN
a7a10 47 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c  G..*/.static SQL
a7a20 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 73  ITE_WSD struct s
a7a30 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20 73  qlite3PrngType s
a7a40 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 3b  qlite3SavedPrng;
a7a50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a7a60 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67  void sqlite3Prng
a7a70 53 61 76 65 53 74 61 74 65 28 76 6f 69 64 29 7b  SaveState(void){
a7a80 0a 20 20 6d 65 6d 63 70 79 28 0a 20 20 20 20 26  .  memcpy(.    &
a7a90 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71  GLOBAL(struct sq
a7aa0 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73  lite3PrngType, s
a7ab0 71 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 29  qlite3SavedPrng)
a7ac0 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74  ,.    &GLOBAL(st
a7ad0 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67  ruct sqlite3Prng
a7ae0 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e  Type, sqlite3Prn
a7af0 67 29 2c 0a 20 20 20 20 73 69 7a 65 6f 66 28 73  g),.    sizeof(s
a7b00 71 6c 69 74 65 33 50 72 6e 67 29 0a 20 20 29 3b  qlite3Prng).  );
a7b10 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
a7b20 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72  E void sqlite3Pr
a7b30 6e 67 52 65 73 74 6f 72 65 53 74 61 74 65 28 76  ngRestoreState(v
a7b40 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70 79 28 0a  oid){.  memcpy(.
a7b50 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 75      &GLOBAL(stru
a7b60 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79  ct sqlite3PrngTy
a7b70 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29  pe, sqlite3Prng)
a7b80 2c 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74  ,.    &GLOBAL(st
a7b90 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67  ruct sqlite3Prng
a7ba0 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53 61 76  Type, sqlite3Sav
a7bb0 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 73 69 7a  edPrng),.    siz
a7bc0 65 6f 66 28 73 71 6c 69 74 65 33 50 72 6e 67 29  eof(sqlite3Prng)
a7bd0 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  .  );.}.SQLITE_P
a7be0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
a7bf0 74 65 33 50 72 6e 67 52 65 73 65 74 53 74 61 74  te3PrngResetStat
a7c00 65 28 76 6f 69 64 29 7b 0a 20 20 47 4c 4f 42 41  e(void){.  GLOBA
a7c10 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  L(struct sqlite3
a7c20 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65  PrngType, sqlite
a7c30 33 50 72 6e 67 29 2e 69 73 49 6e 69 74 20 3d 20  3Prng).isInit = 
a7c40 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  0;.}.#endif /* S
a7c50 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54  QLITE_OMIT_BUILT
a7c60 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a  IN_TEST */../***
a7c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
a7c80 6f 66 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a 2a  of random.c ****
a7c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
a7cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
a7cd0 6e 20 66 69 6c 65 20 75 74 66 2e 63 20 2a 2a 2a  n file utf.c ***
a7ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
a7d10 2a 20 32 30 30 34 20 41 70 72 69 6c 20 31 33 0a  * 2004 April 13.
a7d20 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
a7d30 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
a7d40 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
a7d50 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
a7d60 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
a7d70 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
a7d80 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
a7d90 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
a7da0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
a7db0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
a7dc0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
a7dd0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
a7de0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
a7df0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
a7e00 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
a7e10 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
a7e20 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
a7e30 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
a7e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
a7e80 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
a7e90 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73  ains routines us
a7ea0 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 65 20  ed to translate 
a7eb0 62 65 74 77 65 65 6e 20 55 54 46 2d 38 2c 20 0a  between UTF-8, .
a7ec0 2a 2a 20 55 54 46 2d 31 36 2c 20 55 54 46 2d 31  ** UTF-16, UTF-1
a7ed0 36 42 45 2c 20 61 6e 64 20 55 54 46 2d 31 36 4c  6BE, and UTF-16L
a7ee0 45 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 75 74  E..**.** $Id: ut
a7ef0 66 2e 63 2c 76 20 31 2e 37 33 20 32 30 30 39 2f  f.c,v 1.73 2009/
a7f00 30 34 2f 30 31 20 31 38 3a 34 30 3a 33 32 20 64  04/01 18:40:32 d
a7f10 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 4e  rh Exp $.**.** N
a7f20 6f 74 65 73 20 6f 6e 20 55 54 46 2d 38 3a 0a 2a  otes on UTF-8:.*
a7f30 2a 0a 2a 2a 20 20 20 42 79 74 65 2d 30 20 20 20  *.**   Byte-0   
a7f40 20 42 79 74 65 2d 31 20 20 20 20 42 79 74 65 2d   Byte-1    Byte-
a7f50 32 20 20 20 20 42 79 74 65 2d 33 20 20 20 20 56  2    Byte-3    V
a7f60 61 6c 75 65 0a 2a 2a 20 20 30 78 78 78 78 78 78  alue.**  0xxxxxx
a7f70 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x               
a7f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a7f90 20 20 30 30 30 30 30 30 30 30 20 30 30 30 30 30    00000000 00000
a7fa0 30 30 30 20 30 78 78 78 78 78 78 78 0a 2a 2a 20  000 0xxxxxxx.** 
a7fb0 20 31 31 30 79 79 79 79 79 20 20 31 30 78 78 78   110yyyyy  10xxx
a7fc0 78 78 78 20 20 20 20 20 20 20 20 20 20 20 20 20  xxx             
a7fd0 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30            000000
a7fe0 30 30 20 30 30 30 30 30 79 79 79 20 79 79 78 78  00 00000yyy yyxx
a7ff0 78 78 78 78 0a 2a 2a 20 20 31 31 31 30 7a 7a 7a  xxxx.**  1110zzz
a8000 7a 20 20 31 30 79 79 79 79 79 79 20 20 31 30 78  z  10yyyyyy  10x
a8010 78 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20  xxxxx           
a8020 20 20 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a 79    00000000 zzzzy
a8030 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 20  yyy yyxxxxxx.** 
a8040 20 31 31 31 31 30 75 75 75 20 20 31 30 75 75 7a   11110uuu  10uuz
a8050 7a 7a 7a 20 20 31 30 79 79 79 79 79 79 20 20 31  zzz  10yyyyyy  1
a8060 30 78 78 78 78 78 78 20 20 20 30 30 30 75 75 75  0xxxxxx   000uuu
a8070 75 75 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78  uu zzzzyyyy yyxx
a8080 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 6f  xxxx.**.**.** No
a8090 74 65 73 20 6f 6e 20 55 54 46 2d 31 36 3a 20 20  tes on UTF-16:  
a80a0 28 77 69 74 68 20 77 77 77 77 2b 31 3d 3d 75 75  (with wwww+1==uu
a80b0 75 75 75 29 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  uuu).**.**      
a80c0 57 6f 72 64 2d 30 20 20 20 20 20 20 20 20 20 20  Word-0          
a80d0 20 20 20 20 20 57 6f 72 64 2d 31 20 20 20 20 20       Word-1     
a80e0 20 20 20 20 20 56 61 6c 75 65 0a 2a 2a 20 20 31       Value.**  1
a80f0 31 30 31 31 30 77 77 20 77 77 7a 7a 7a 7a 79 79  10110ww wwzzzzyy
a8100 20 20 20 31 31 30 31 31 31 79 79 20 79 79 78 78     110111yy yyxx
a8110 78 78 78 78 20 20 20 20 30 30 30 75 75 75 75 75  xxxx    000uuuuu
a8120 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78   zzzzyyyy yyxxxx
a8130 78 78 0a 2a 2a 20 20 7a 7a 7a 7a 79 79 79 79 20  xx.**  zzzzyyyy 
a8140 79 79 78 78 78 78 78 78 20 20 20 20 20 20 20 20  yyxxxxxx        
a8150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a8160 30 30 30 30 30 30 30 30 20 7a 7a 7a 7a 79 79 79  00000000 zzzzyyy
a8170 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 0a 2a 2a  y yyxxxxxx.**.**
a8180 0a 2a 2a 20 42 4f 4d 20 6f 72 20 42 79 74 65 20  .** BOM or Byte 
a8190 4f 72 64 65 72 20 4d 61 72 6b 3a 0a 2a 2a 20 20  Order Mark:.**  
a81a0 20 20 20 30 78 66 66 20 30 78 66 65 20 20 20 6c     0xff 0xfe   l
a81b0 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 75 74 66  ittle-endian utf
a81c0 2d 31 36 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 20 20  -16 follows.**  
a81d0 20 20 20 30 78 66 65 20 30 78 66 66 20 20 20 62     0xfe 0xff   b
a81e0 69 67 2d 65 6e 64 69 61 6e 20 75 74 66 2d 31 36  ig-endian utf-16
a81f0 20 66 6f 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2f 0a 2f   follows.**.*/./
a8200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
a8210 6e 63 6c 75 64 65 20 76 64 62 65 49 6e 74 2e 68  nclude vdbeInt.h
a8220 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
a8230 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  f utf.c ********
a8240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
a8250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
a8260 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 49 6e  egin file vdbeIn
a8270 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
a8280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
a82a0 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 65 6d  *.** 2003 Septem
a82b0 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ber 6.**.** The 
a82c0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
a82d0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
a82e0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
a82f0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
a8300 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
a8310 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
a8320 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
a8330 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
a8340 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
a8350 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
a8360 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
a8370 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
a8380 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
a8390 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
a83a0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
a83b0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
a83c0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
a83d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a83e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a83f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8410 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ****.** This is 
a8420 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 20  the header file 
a8430 66 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  for information 
a8440 74 68 61 74 20 69 73 20 70 72 69 76 61 74 65 20  that is private 
a8450 74 6f 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e 20  to the.** VDBE. 
a8460 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f   This informatio
a8470 6e 20 75 73 65 64 20 74 6f 20 61 6c 6c 20 62 65  n used to all be
a8480 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74   at the top of t
a8490 68 65 20 73 69 6e 67 6c 65 0a 2a 2a 20 73 6f 75  he single.** sou
a84a0 72 63 65 20 63 6f 64 65 20 66 69 6c 65 20 22 76  rce code file "v
a84b0 64 62 65 2e 63 22 2e 20 20 57 68 65 6e 20 74 68  dbe.c".  When th
a84c0 61 74 20 66 69 6c 65 20 62 65 63 61 6d 65 20 74  at file became t
a84d0 6f 6f 20 62 69 67 20 28 6f 76 65 72 0a 2a 2a 20  oo big (over.** 
a84e0 36 30 30 30 20 6c 69 6e 65 73 20 6c 6f 6e 67 29  6000 lines long)
a84f0 20 69 74 20 77 61 73 20 73 70 6c 69 74 20 75 70   it was split up
a8500 20 69 6e 74 6f 20 73 65 76 65 72 61 6c 20 73 6d   into several sm
a8510 61 6c 6c 65 72 20 66 69 6c 65 73 20 61 6e 64 0a  aller files and.
a8520 2a 2a 20 74 68 69 73 20 68 65 61 64 65 72 20 69  ** this header i
a8530 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 61 73 20 66  nformation was f
a8540 61 63 74 6f 72 65 64 20 6f 75 74 2e 0a 2a 2a 0a  actored out..**.
a8550 2a 2a 20 24 49 64 3a 20 76 64 62 65 49 6e 74 2e  ** $Id: vdbeInt.
a8560 68 2c 76 20 31 2e 31 37 34 20 32 30 30 39 2f 30  h,v 1.174 2009/0
a8570 36 2f 32 33 20 31 34 3a 31 35 3a 30 34 20 64 72  6/23 14:15:04 dr
a8580 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64  h Exp $.*/.#ifnd
a8590 65 66 20 5f 56 44 42 45 49 4e 54 5f 48 5f 0a 23  ef _VDBEINT_H_.#
a85a0 64 65 66 69 6e 65 20 5f 56 44 42 45 49 4e 54 5f  define _VDBEINT_
a85b0 48 5f 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 69 73  H_../*.** SQL is
a85c0 20 74 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f   translated into
a85d0 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20 69   a sequence of i
a85e0 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 6f 20 62  nstructions to b
a85f0 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 62 79  e.** executed by
a8600 20 61 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69   a virtual machi
a8610 6e 65 2e 20 20 45 61 63 68 20 69 6e 73 74 72 75  ne.  Each instru
a8620 63 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 73 74  ction is an inst
a8630 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66  ance.** of the f
a8640 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
a8650 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  re..*/.typedef s
a8660 74 72 75 63 74 20 56 64 62 65 4f 70 20 4f 70 3b  truct VdbeOp Op;
a8670 0a 0a 2f 2a 0a 2a 2a 20 42 6f 6f 6c 65 61 6e 20  ../*.** Boolean 
a8680 76 61 6c 75 65 73 0a 2a 2f 0a 74 79 70 65 64 65  values.*/.typede
a8690 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  f unsigned char 
a86a0 42 6f 6f 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63  Bool;../*.** A c
a86b0 75 72 73 6f 72 20 69 73 20 61 20 70 6f 69 6e 74  ursor is a point
a86c0 65 72 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65  er into a single
a86d0 20 42 54 72 65 65 20 77 69 74 68 69 6e 20 61 20   BTree within a 
a86e0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
a86f0 2a 20 54 68 65 20 63 75 72 73 6f 72 20 63 61 6e  * The cursor can
a8700 20 73 65 65 6b 20 74 6f 20 61 20 42 54 72 65 65   seek to a BTree
a8710 20 65 6e 74 72 79 20 77 69 74 68 20 61 20 70 61   entry with a pa
a8720 72 74 69 63 75 6c 61 72 20 6b 65 79 2c 20 6f 72  rticular key, or
a8730 0a 2a 2a 20 6c 6f 6f 70 20 6f 76 65 72 20 61 6c  .** loop over al
a8740 6c 20 65 6e 74 72 69 65 73 20 6f 66 20 74 68 65  l entries of the
a8750 20 42 74 72 65 65 2e 20 20 59 6f 75 20 63 61 6e   Btree.  You can
a8760 20 61 6c 73 6f 20 69 6e 73 65 72 74 20 6e 65 77   also insert new
a8770 20 42 54 72 65 65 0a 2a 2a 20 65 6e 74 72 69 65   BTree.** entrie
a8780 73 20 6f 72 20 72 65 74 72 69 65 76 65 20 74 68  s or retrieve th
a8790 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20 66 72  e key or data fr
a87a0 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68 61  om the entry tha
a87b0 74 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20  t the cursor.** 
a87c0 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  is currently poi
a87d0 6e 74 69 6e 67 20 74 6f 2e 0a 2a 2a 20 0a 2a 2a  nting to..** .**
a87e0 20 45 76 65 72 79 20 63 75 72 73 6f 72 20 74 68   Every cursor th
a87f0 61 74 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  at the virtual m
a8800 61 63 68 69 6e 65 20 68 61 73 20 6f 70 65 6e 20  achine has open 
a8810 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 62  is represented b
a8820 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  y an.** instance
a8830 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
a8840 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  g structure..**.
a8850 2a 2a 20 49 66 20 74 68 65 20 56 64 62 65 43 75  ** If the VdbeCu
a8860 72 73 6f 72 2e 69 73 54 72 69 67 67 65 72 52 6f  rsor.isTriggerRo
a8870 77 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 74  w flag is set it
a8880 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69 73   means that this
a8890 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 72 65   cursor is.** re
a88a0 61 6c 6c 79 20 61 20 73 69 6e 67 6c 65 20 72 6f  ally a single ro
a88b0 77 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74  w that represent
a88c0 73 20 74 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44  s the NEW or OLD
a88d0 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 6f 66   pseudo-table of
a88e0 0a 2a 2a 20 61 20 72 6f 77 20 74 72 69 67 67 65  .** a row trigge
a88f0 72 2e 20 20 54 68 65 20 64 61 74 61 20 66 6f 72  r.  The data for
a8900 20 74 68 65 20 72 6f 77 20 69 73 20 73 74 6f 72   the row is stor
a8910 65 64 20 69 6e 20 56 64 62 65 43 75 72 73 6f 72  ed in VdbeCursor
a8920 2e 70 44 61 74 61 20 61 6e 64 0a 2a 2a 20 74 68  .pData and.** th
a8930 65 20 72 6f 77 69 64 20 69 73 20 69 6e 20 56 64  e rowid is in Vd
a8940 62 65 43 75 72 73 6f 72 2e 69 4b 65 79 2e 0a 2a  beCursor.iKey..*
a8950 2f 0a 73 74 72 75 63 74 20 56 64 62 65 43 75 72  /.struct VdbeCur
a8960 73 6f 72 20 7b 0a 20 20 42 74 43 75 72 73 6f 72  sor {.  BtCursor
a8970 20 2a 70 43 75 72 73 6f 72 3b 20 20 20 20 2f 2a   *pCursor;    /*
a8980 20 54 68 65 20 63 75 72 73 6f 72 20 73 74 72 75   The cursor stru
a8990 63 74 75 72 65 20 6f 66 20 74 68 65 20 62 61 63  cture of the bac
a89a0 6b 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44  kend */.  int iD
a89b0 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
a89c0 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 73  /* Index of curs
a89d0 6f 72 20 64 61 74 61 62 61 73 65 20 69 6e 20 64  or database in d
a89e0 62 2d 3e 61 44 62 5b 5d 20 28 6f 72 20 2d 31 29  b->aDb[] (or -1)
a89f0 20 2a 2f 0a 20 20 69 36 34 20 6c 61 73 74 52 6f   */.  i64 lastRo
a8a00 77 69 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c  wid;        /* L
a8a10 61 73 74 20 72 6f 77 69 64 20 66 72 6f 6d 20 61  ast rowid from a
a8a20 20 4e 65 78 74 20 6f 72 20 4e 65 78 74 49 64 78   Next or NextIdx
a8a30 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20   operation */.  
a8a40 42 6f 6f 6c 20 7a 65 72 6f 65 64 3b 20 20 20 20  Bool zeroed;    
a8a50 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
a8a60 20 7a 65 72 6f 65 64 20 6f 75 74 20 61 6e 64 20   zeroed out and 
a8a70 72 65 61 64 79 20 66 6f 72 20 72 65 75 73 65 20  ready for reuse 
a8a80 2a 2f 0a 20 20 42 6f 6f 6c 20 72 6f 77 69 64 49  */.  Bool rowidI
a8a90 73 56 61 6c 69 64 3b 20 20 20 20 2f 2a 20 54 72  sValid;    /* Tr
a8aa0 75 65 20 69 66 20 6c 61 73 74 52 6f 77 69 64 20  ue if lastRowid 
a8ab0 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 42 6f  is valid */.  Bo
a8ac0 6f 6c 20 61 74 46 69 72 73 74 3b 20 20 20 20 20  ol atFirst;     
a8ad0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70      /* True if p
a8ae0 6f 69 6e 74 69 6e 67 20 74 6f 20 66 69 72 73 74  ointing to first
a8af0 20 65 6e 74 72 79 20 2a 2f 0a 20 20 42 6f 6f 6c   entry */.  Bool
a8b00 20 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 3b   useRandomRowid;
a8b10 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 6e 65    /* Generate ne
a8b20 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73  w record numbers
a8b30 20 73 65 6d 69 2d 72 61 6e 64 6f 6d 6c 79 20 2a   semi-randomly *
a8b40 2f 0a 20 20 42 6f 6f 6c 20 6e 75 6c 6c 52 6f 77  /.  Bool nullRow
a8b50 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ;         /* Tru
a8b60 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f  e if pointing to
a8b70 20 61 20 72 6f 77 20 77 69 74 68 20 6e 6f 20 64   a row with no d
a8b80 61 74 61 20 2a 2f 0a 20 20 42 6f 6f 6c 20 64 65  ata */.  Bool de
a8b90 66 65 72 72 65 64 4d 6f 76 65 74 6f 3b 20 20 2f  ferredMoveto;  /
a8ba0 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  * A call to sqli
a8bb0 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 29  te3BtreeMoveto()
a8bc0 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20   is needed */.  
a8bd0 42 6f 6f 6c 20 69 73 54 61 62 6c 65 3b 20 20 20  Bool isTable;   
a8be0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
a8bf0 20 61 20 74 61 62 6c 65 20 72 65 71 75 69 72 69   a table requiri
a8c00 6e 67 20 69 6e 74 65 67 65 72 20 6b 65 79 73 20  ng integer keys 
a8c10 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 49 6e 64 65  */.  Bool isInde
a8c20 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  x;         /* Tr
a8c30 75 65 20 69 66 20 61 6e 20 69 6e 64 65 78 20 63  ue if an index c
a8c40 6f 6e 74 61 69 6e 69 6e 67 20 6b 65 79 73 20 6f  ontaining keys o
a8c50 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20 2a 2f  nly - no data */
a8c60 0a 20 20 69 36 34 20 6d 6f 76 65 74 6f 54 61 72  .  i64 movetoTar
a8c70 67 65 74 3b 20 20 20 20 20 2f 2a 20 41 72 67 75  get;     /* Argu
a8c80 6d 65 6e 74 20 74 6f 20 74 68 65 20 64 65 66 65  ment to the defe
a8c90 72 72 65 64 20 73 71 6c 69 74 65 33 42 74 72 65  rred sqlite3Btre
a8ca0 65 4d 6f 76 65 74 6f 28 29 20 2a 2f 0a 20 20 42  eMoveto() */.  B
a8cb0 74 72 65 65 20 2a 70 42 74 3b 20 20 20 20 20 20  tree *pBt;      
a8cc0 20 20 20 20 20 2f 2a 20 53 65 70 61 72 61 74 65       /* Separate
a8cd0 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 65   file holding te
a8ce0 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 2a 2f  mporary table */
a8cf0 0a 20 20 69 6e 74 20 70 73 65 75 64 6f 54 61 62  .  int pseudoTab
a8d00 6c 65 52 65 67 3b 20 20 20 2f 2a 20 52 65 67 69  leReg;   /* Regi
a8d10 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 70 73 65  ster holding pse
a8d20 75 64 6f 74 61 62 6c 65 20 63 6f 6e 74 65 6e 74  udotable content
a8d30 2e 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a  . */.  KeyInfo *
a8d40 70 4b 65 79 49 6e 66 6f 3b 20 20 20 20 2f 2a 20  pKeyInfo;    /* 
a8d50 49 6e 66 6f 20 61 62 6f 75 74 20 69 6e 64 65 78  Info about index
a8d60 20 6b 65 79 73 20 6e 65 65 64 65 64 20 62 79 20   keys needed by 
a8d70 69 6e 64 65 78 20 63 75 72 73 6f 72 73 20 2a 2f  index cursors */
a8d80 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20  .  int nField;  
a8d90 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
a8da0 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20  er of fields in 
a8db0 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  the header */.  
a8dc0 69 36 34 20 73 65 71 43 6f 75 6e 74 3b 20 20 20  i64 seqCount;   
a8dd0 20 20 20 20 20 20 2f 2a 20 53 65 71 75 65 6e 63        /* Sequenc
a8de0 65 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 73  e counter */.  s
a8df0 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
a8e00 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b  or *pVtabCursor;
a8e10 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20    /* The cursor 
a8e20 66 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74 61  for a virtual ta
a8e30 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73  ble */.  const s
a8e40 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
a8e50 4d 6f 64 75 6c 65 3b 20 20 20 20 20 2f 2a 20 4d  Module;     /* M
a8e60 6f 64 75 6c 65 20 66 6f 72 20 63 75 72 73 6f 72  odule for cursor
a8e70 20 70 56 74 61 62 43 75 72 73 6f 72 20 2a 2f 0a   pVtabCursor */.
a8e80 0a 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20  .  /* Result of 
a8e90 6c 61 73 74 20 73 71 6c 69 74 65 33 42 74 72 65  last sqlite3Btre
a8ea0 65 4d 6f 76 65 74 6f 28 29 20 64 6f 6e 65 20 62  eMoveto() done b
a8eb0 79 20 61 6e 20 4f 50 5f 4e 6f 74 45 78 69 73 74  y an OP_NotExist
a8ec0 73 20 6f 72 20 0a 20 20 2a 2a 20 4f 50 5f 49 73  s or .  ** OP_Is
a8ed0 55 6e 69 71 75 65 20 6f 70 63 6f 64 65 20 6f 6e  Unique opcode on
a8ee0 20 74 68 69 73 20 63 75 72 73 6f 72 2e 20 2a 2f   this cursor. */
a8ef0 0a 20 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c  .  int seekResul
a8f00 74 3b 0a 0a 20 20 2f 2a 20 43 61 63 68 65 64 20  t;..  /* Cached 
a8f10 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
a8f20 74 20 74 68 65 20 68 65 61 64 65 72 20 66 6f 72  t the header for
a8f30 20 74 68 65 20 64 61 74 61 20 72 65 63 6f 72 64   the data record
a8f40 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 63   that the.  ** c
a8f50 75 72 73 6f 72 20 69 73 20 63 75 72 72 65 6e 74  ursor is current
a8f60 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20  ly pointing to. 
a8f70 20 4f 6e 6c 79 20 76 61 6c 69 64 20 69 66 20 63   Only valid if c
a8f80 61 63 68 65 53 74 61 74 75 73 20 6d 61 74 63 68  acheStatus match
a8f90 65 73 0a 20 20 2a 2a 20 56 64 62 65 2e 63 61 63  es.  ** Vdbe.cac
a8fa0 68 65 43 74 72 2e 20 20 56 64 62 65 2e 63 61 63  heCtr.  Vdbe.cac
a8fb0 68 65 43 74 72 20 77 69 6c 6c 20 6e 65 76 65 72  heCtr will never
a8fc0 20 74 61 6b 65 20 6f 6e 20 74 68 65 20 76 61 6c   take on the val
a8fd0 75 65 20 6f 66 0a 20 20 2a 2a 20 43 41 43 48 45  ue of.  ** CACHE
a8fe0 5f 53 54 41 4c 45 20 61 6e 64 20 73 6f 20 73 65  _STALE and so se
a8ff0 74 74 69 6e 67 20 63 61 63 68 65 53 74 61 74 75  tting cacheStatu
a9000 73 3d 43 41 43 48 45 5f 53 54 41 4c 45 20 67 75  s=CACHE_STALE gu
a9010 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a 20 20  arantees that.  
a9020 2a 2a 20 74 68 65 20 63 61 63 68 65 20 69 73 20  ** the cache is 
a9030 6f 75 74 20 6f 66 20 64 61 74 65 2e 0a 20 20 2a  out of date..  *
a9040 2a 0a 20 20 2a 2a 20 61 52 6f 77 20 6d 69 67 68  *.  ** aRow migh
a9050 74 20 70 6f 69 6e 74 20 74 6f 20 28 65 70 68 65  t point to (ephe
a9060 6d 65 72 61 6c 29 20 64 61 74 61 20 66 6f 72 20  meral) data for 
a9070 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2c  the current row,
a9080 20 6f 72 20 69 74 20 6d 69 67 68 74 0a 20 20 2a   or it might.  *
a9090 2a 20 62 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a  * be NULL..  */.
a90a0 20 20 75 33 32 20 63 61 63 68 65 53 74 61 74 75    u32 cacheStatu
a90b0 73 3b 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65  s;      /* Cache
a90c0 20 69 73 20 76 61 6c 69 64 20 69 66 20 74 68 69   is valid if thi
a90d0 73 20 6d 61 74 63 68 65 73 20 56 64 62 65 2e 63  s matches Vdbe.c
a90e0 61 63 68 65 43 74 72 20 2a 2f 0a 20 20 69 6e 74  acheCtr */.  int
a90f0 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20   payloadSize;   
a9100 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62     /* Total numb
a9110 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
a9120 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75  he record */.  u
a9130 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20 20 20  32 *aType;      
a9140 20 20 20 20 20 2f 2a 20 54 79 70 65 20 76 61 6c       /* Type val
a9150 75 65 73 20 66 6f 72 20 61 6c 6c 20 65 6e 74 72  ues for all entr
a9160 69 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72  ies in the recor
a9170 64 20 2a 2f 0a 20 20 75 33 32 20 2a 61 4f 66 66  d */.  u32 *aOff
a9180 73 65 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  set;         /* 
a9190 43 61 63 68 65 64 20 6f 66 66 73 65 74 73 20 74  Cached offsets t
a91a0 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 65  o the start of e
a91b0 61 63 68 20 63 6f 6c 75 6d 6e 73 20 64 61 74 61  ach columns data
a91c0 20 2a 2f 0a 20 20 75 38 20 2a 61 52 6f 77 3b 20   */.  u8 *aRow; 
a91d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
a91e0 61 74 61 20 66 6f 72 20 74 68 65 20 63 75 72 72  ata for the curr
a91f0 65 6e 74 20 72 6f 77 2c 20 69 66 20 61 6c 6c 20  ent row, if all 
a9200 6f 6e 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a 7d  on one page */.}
a9210 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
a9220 20 56 64 62 65 43 75 72 73 6f 72 20 56 64 62 65   VdbeCursor Vdbe
a9230 43 75 72 73 6f 72 3b 0a 0a 2f 2a 0a 2a 2a 20 57  Cursor;../*.** W
a9240 68 65 6e 20 61 20 73 75 62 2d 70 72 6f 67 72 61  hen a sub-progra
a9250 6d 20 69 73 20 65 78 65 63 75 74 65 64 20 28 4f  m is executed (O
a9260 50 5f 50 72 6f 67 72 61 6d 29 2c 20 61 20 73 74  P_Program), a st
a9270 72 75 63 74 75 72 65 20 6f 66 20 74 68 69 73 20  ructure of this 
a9280 74 79 70 65 0a 2a 2a 20 69 73 20 61 6c 6c 6f 63  type.** is alloc
a9290 61 74 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68  ated to store th
a92a0 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20  e current value 
a92b0 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 63  of the program c
a92c0 6f 75 6e 74 65 72 2c 20 61 73 0a 2a 2a 20 77 65  ounter, as.** we
a92d0 6c 6c 20 61 73 20 74 68 65 20 63 75 72 72 65 6e  ll as the curren
a92e0 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 72  t memory cell ar
a92f0 72 61 79 20 61 6e 64 20 76 61 72 69 6f 75 73 20  ray and various 
a9300 6f 74 68 65 72 20 66 72 61 6d 65 20 73 70 65 63  other frame spec
a9310 69 66 69 63 0a 2a 2a 20 76 61 6c 75 65 73 20 73  ific.** values s
a9320 74 6f 72 65 64 20 69 6e 20 74 68 65 20 56 64 62  tored in the Vdb
a9330 65 20 73 74 72 75 63 74 2e 20 57 68 65 6e 20 74  e struct. When t
a9340 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 69  he sub-program i
a9350 73 20 66 69 6e 69 73 68 65 64 2c 20 0a 2a 2a 20  s finished, .** 
a9360 74 68 65 73 65 20 76 61 6c 75 65 73 20 61 72 65  these values are
a9370 20 63 6f 70 69 65 64 20 62 61 63 6b 20 74 6f 20   copied back to 
a9380 74 68 65 20 56 64 62 65 20 66 72 6f 6d 20 74 68  the Vdbe from th
a9390 65 20 56 64 62 65 46 72 61 6d 65 20 73 74 72 75  e VdbeFrame stru
a93a0 63 74 75 72 65 2c 0a 2a 2a 20 72 65 73 74 6f 72  cture,.** restor
a93b0 69 6e 67 20 74 68 65 20 73 74 61 74 65 20 6f 66  ing the state of
a93c0 20 74 68 65 20 56 4d 20 74 6f 20 61 73 20 69 74   the VM to as it
a93d0 20 77 61 73 20 62 65 66 6f 72 65 20 74 68 65 20   was before the 
a93e0 73 75 62 2d 70 72 6f 67 72 61 6d 0a 2a 2a 20 62  sub-program.** b
a93f0 65 67 61 6e 20 65 78 65 63 75 74 69 6e 67 2e 0a  egan executing..
a9400 2a 2a 0a 2a 2a 20 46 72 61 6d 65 73 20 61 72 65  **.** Frames are
a9410 20 73 74 6f 72 65 64 20 69 6e 20 61 20 6c 69 6e   stored in a lin
a9420 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 64 20  ked list headed 
a9430 61 74 20 56 64 62 65 2e 70 50 61 72 65 6e 74 2e  at Vdbe.pParent.
a9440 20 56 64 62 65 2e 70 50 61 72 65 6e 74 0a 2a 2a   Vdbe.pParent.**
a9450 20 69 73 20 74 68 65 20 70 61 72 65 6e 74 20 6f   is the parent o
a9460 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 72  f the current fr
a9470 61 6d 65 2c 20 6f 72 20 7a 65 72 6f 20 69 66 20  ame, or zero if 
a9480 74 68 65 20 63 75 72 72 65 6e 74 20 66 72 61 6d  the current fram
a9490 65 0a 2a 2a 20 69 73 20 74 68 65 20 6d 61 69 6e  e.** is the main
a94a0 20 56 64 62 65 20 70 72 6f 67 72 61 6d 2e 0a 2a   Vdbe program..*
a94b0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
a94c0 20 56 64 62 65 46 72 61 6d 65 20 56 64 62 65 46   VdbeFrame VdbeF
a94d0 72 61 6d 65 3b 0a 73 74 72 75 63 74 20 56 64 62  rame;.struct Vdb
a94e0 65 46 72 61 6d 65 20 7b 0a 20 20 56 64 62 65 20  eFrame {.  Vdbe 
a94f0 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *v;             
a9500 20 20 20 2f 2a 20 56 4d 20 74 68 69 73 20 66 72     /* VM this fr
a9510 61 6d 65 20 62 65 6c 6f 6e 67 73 20 74 6f 20 2a  ame belongs to *
a9520 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 20  /.  int pc;     
a9530 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
a9540 72 6f 67 72 61 6d 20 43 6f 75 6e 74 65 72 20 2a  rogram Counter *
a9550 2f 0a 20 20 4f 70 20 2a 61 4f 70 3b 20 20 20 20  /.  Op *aOp;    
a9560 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
a9570 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69  rogram instructi
a9580 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70  ons */.  int nOp
a9590 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a95a0 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 4f 70 20   /* Size of aOp 
a95b0 61 72 72 61 79 20 2a 2f 0a 20 20 4d 65 6d 20 2a  array */.  Mem *
a95c0 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20  aMem;           
a95d0 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 6d     /* Array of m
a95e0 65 6d 6f 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20  emory cells */. 
a95f0 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20   int nMem;      
a9600 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
a9610 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
a9620 20 61 4d 65 6d 20 2a 2f 0a 20 20 56 64 62 65 43   aMem */.  VdbeC
a9630 75 72 73 6f 72 20 2a 2a 61 70 43 73 72 3b 20 20  ursor **apCsr;  
a9640 20 20 20 2f 2a 20 45 6c 65 6d 65 6e 74 20 6f 66     /* Element of
a9650 20 56 64 62 65 20 63 75 72 73 6f 72 73 20 2a 2f   Vdbe cursors */
a9660 0a 20 20 75 31 36 20 6e 43 75 72 73 6f 72 3b 20  .  u16 nCursor; 
a9670 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
a9680 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
a9690 69 6e 20 61 70 43 73 72 20 2a 2f 0a 20 20 76 6f  in apCsr */.  vo
a96a0 69 64 20 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 20  id *token;      
a96b0 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66        /* Copy of
a96c0 20 53 75 62 50 72 6f 67 72 61 6d 2e 74 6f 6b 65   SubProgram.toke
a96d0 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 69 6c  n */.  int nChil
a96e0 64 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f  dMem;          /
a96f0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f  * Number of memo
a9700 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 63 68 69  ry cells for chi
a9710 6c 64 20 66 72 61 6d 65 20 2a 2f 0a 20 20 69 6e  ld frame */.  in
a9720 74 20 6e 43 68 69 6c 64 43 73 72 3b 20 20 20 20  t nChildCsr;    
a9730 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
a9740 6f 66 20 63 75 72 73 6f 72 73 20 66 6f 72 20 63  of cursors for c
a9750 68 69 6c 64 20 66 72 61 6d 65 20 2a 2f 0a 20 20  hild frame */.  
a9760 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20 20  i64 lastRowid;  
a9770 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20          /* Last 
a9780 69 6e 73 65 72 74 20 72 6f 77 69 64 20 28 73 71  insert rowid (sq
a9790 6c 69 74 65 33 2e 6c 61 73 74 52 6f 77 69 64 29  lite3.lastRowid)
a97a0 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67   */.  int nChang
a97b0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
a97c0 20 53 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67   Statement chang
a97d0 65 73 20 28 56 64 62 65 2e 6e 43 68 61 6e 67 65  es (Vdbe.nChange
a97e0 73 29 20 20 20 20 20 2a 2f 0a 20 20 56 64 62 65  s)     */.  Vdbe
a97f0 46 72 61 6d 65 20 2a 70 50 61 72 65 6e 74 3b 20  Frame *pParent; 
a9800 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 6f 66      /* Parent of
a9810 20 74 68 69 73 20 66 72 61 6d 65 20 2a 2f 0a 7d   this frame */.}
a9820 3b 0a 0a 23 64 65 66 69 6e 65 20 56 64 62 65 46  ;..#define VdbeF
a9830 72 61 6d 65 4d 65 6d 28 70 29 20 28 28 4d 65 6d  rameMem(p) ((Mem
a9840 20 2a 29 26 28 28 75 38 20 2a 29 70 29 5b 52 4f   *)&((u8 *)p)[RO
a9850 55 4e 44 38 28 73 69 7a 65 6f 66 28 56 64 62 65  UND8(sizeof(Vdbe
a9860 46 72 61 6d 65 29 29 5d 29 0a 0a 2f 2a 0a 2a 2a  Frame))])../*.**
a9870 20 41 20 76 61 6c 75 65 20 66 6f 72 20 56 64 62   A value for Vdb
a9880 65 43 75 72 73 6f 72 2e 63 61 63 68 65 56 61 6c  eCursor.cacheVal
a9890 69 64 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  id that means th
a98a0 65 20 63 61 63 68 65 20 69 73 20 61 6c 77 61 79  e cache is alway
a98b0 73 20 69 6e 76 61 6c 69 64 2e 0a 2a 2f 0a 23 64  s invalid..*/.#d
a98c0 65 66 69 6e 65 20 43 41 43 48 45 5f 53 54 41 4c  efine CACHE_STAL
a98d0 45 20 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72  E 0../*.** Inter
a98e0 6e 61 6c 6c 79 2c 20 74 68 65 20 76 64 62 65 20  nally, the vdbe 
a98f0 6d 61 6e 69 70 75 6c 61 74 65 73 20 6e 65 61 72  manipulates near
a9900 6c 79 20 61 6c 6c 20 53 51 4c 20 76 61 6c 75 65  ly all SQL value
a9910 73 20 61 73 20 4d 65 6d 0a 2a 2a 20 73 74 72 75  s as Mem.** stru
a9920 63 74 75 72 65 73 2e 20 45 61 63 68 20 4d 65 6d  ctures. Each Mem
a9930 20 73 74 72 75 63 74 20 6d 61 79 20 63 61 63 68   struct may cach
a9940 65 20 6d 75 6c 74 69 70 6c 65 20 72 65 70 72 65  e multiple repre
a9950 73 65 6e 74 61 74 69 6f 6e 73 20 28 73 74 72 69  sentations (stri
a9960 6e 67 2c 0a 2a 2a 20 69 6e 74 65 67 65 72 20 65  ng,.** integer e
a9970 74 63 2e 29 20 6f 66 20 74 68 65 20 73 61 6d 65  tc.) of the same
a9980 20 76 61 6c 75 65 2e 20 20 41 20 76 61 6c 75 65   value.  A value
a9990 20 28 61 6e 64 20 74 68 65 72 65 66 6f 72 65 20   (and therefore 
a99a0 4d 65 6d 20 73 74 72 75 63 74 75 72 65 29 0a 2a  Mem structure).*
a99b0 2a 20 68 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77  * has the follow
a99c0 69 6e 67 20 70 72 6f 70 65 72 74 69 65 73 3a 0a  ing properties:.
a99d0 2a 2a 0a 2a 2a 20 45 61 63 68 20 76 61 6c 75 65  **.** Each value
a99e0 20 68 61 73 20 61 20 6d 61 6e 69 66 65 73 74 20   has a manifest 
a99f0 74 79 70 65 2e 20 54 68 65 20 6d 61 6e 69 66 65  type. The manife
a9a00 73 74 20 74 79 70 65 20 6f 66 20 74 68 65 20 76  st type of the v
a9a10 61 6c 75 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69  alue stored.** i
a9a20 6e 20 61 20 4d 65 6d 20 73 74 72 75 63 74 20 69  n a Mem struct i
a9a30 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  s returned by th
a9a40 65 20 4d 65 6d 54 79 70 65 28 4d 65 6d 2a 29 20  e MemType(Mem*) 
a9a50 6d 61 63 72 6f 2e 20 54 68 65 20 74 79 70 65 20  macro. The type 
a9a60 69 73 0a 2a 2a 20 6f 6e 65 20 6f 66 20 53 51 4c  is.** one of SQL
a9a70 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45  ITE_NULL, SQLITE
a9a80 5f 49 4e 54 45 47 45 52 2c 20 53 51 4c 49 54 45  _INTEGER, SQLITE
a9a90 5f 52 45 41 4c 2c 20 53 51 4c 49 54 45 5f 54 45  _REAL, SQLITE_TE
a9aa0 58 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f  XT or.** SQLITE_
a9ab0 42 4c 4f 42 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  BLOB..*/.struct 
a9ac0 4d 65 6d 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a  Mem {.  union {.
a9ad0 20 20 20 20 69 36 34 20 69 3b 20 20 20 20 20 20      i64 i;      
a9ae0 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67          /* Integ
a9af0 65 72 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20  er value. */.   
a9b00 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20   int nZero;     
a9b10 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65       /* Used whe
a9b20 6e 20 62 69 74 20 4d 45 4d 5f 5a 65 72 6f 20 69  n bit MEM_Zero i
a9b30 73 20 73 65 74 20 69 6e 20 66 6c 61 67 73 20 2a  s set in flags *
a9b40 2f 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70  /.    FuncDef *p
a9b50 44 65 66 3b 20 20 20 20 20 20 2f 2a 20 55 73 65  Def;      /* Use
a9b60 64 20 6f 6e 6c 79 20 77 68 65 6e 20 66 6c 61 67  d only when flag
a9b70 73 3d 3d 4d 45 4d 5f 41 67 67 20 2a 2f 0a 20 20  s==MEM_Agg */.  
a9b80 20 20 52 6f 77 53 65 74 20 2a 70 52 6f 77 53 65    RowSet *pRowSe
a9b90 74 3b 20 20 20 20 2f 2a 20 55 73 65 64 20 6f 6e  t;    /* Used on
a9ba0 6c 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d  ly when flags==M
a9bb0 45 4d 5f 52 6f 77 53 65 74 20 2a 2f 0a 20 20 20  EM_RowSet */.   
a9bc0 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61   VdbeFrame *pFra
a9bd0 6d 65 3b 20 20 2f 2a 20 55 73 65 64 20 77 68 65  me;  /* Used whe
a9be0 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 46 72 61  n flags==MEM_Fra
a9bf0 6d 65 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 64  me */.  } u;.  d
a9c00 6f 75 62 6c 65 20 72 3b 20 20 20 20 20 20 20 20  ouble r;        
a9c10 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65     /* Real value
a9c20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
a9c30 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  b;        /* The
a9c40 20 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 61   associated data
a9c50 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
a9c60 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20  */.  char *z;   
a9c70 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69           /* Stri
a9c80 6e 67 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75 65  ng or BLOB value
a9c90 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20   */.  int n;    
a9ca0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
a9cb0 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72  ber of character
a9cc0 73 20 69 6e 20 73 74 72 69 6e 67 20 76 61 6c 75  s in string valu
a9cd0 65 2c 20 65 78 63 6c 75 64 69 6e 67 20 27 5c 30  e, excluding '\0
a9ce0 27 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67 73  ' */.  u16 flags
a9cf0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f  ;          /* So
a9d00 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f  me combination o
a9d10 66 20 4d 45 4d 5f 4e 75 6c 6c 2c 20 4d 45 4d 5f  f MEM_Null, MEM_
a9d20 53 74 72 2c 20 4d 45 4d 5f 44 79 6e 2c 20 65 74  Str, MEM_Dyn, et
a9d30 63 2e 20 2a 2f 0a 20 20 75 38 20 20 74 79 70 65  c. */.  u8  type
a9d40 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  ;           /* O
a9d50 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c  ne of SQLITE_NUL
a9d60 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58 54 2c 20  L, SQLITE_TEXT, 
a9d70 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c 20  SQLITE_INTEGER, 
a9d80 65 74 63 20 2a 2f 0a 20 20 75 38 20 20 65 6e 63  etc */.  u8  enc
a9d90 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
a9da0 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c  SQLITE_UTF8, SQL
a9db0 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53 51 4c  ITE_UTF16BE, SQL
a9dc0 49 54 45 5f 55 54 46 31 36 4c 45 20 2a 2f 0a 20  ITE_UTF16LE */. 
a9dd0 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
a9de0 69 64 20 2a 29 3b 20 20 2f 2a 20 49 66 20 6e 6f  id *);  /* If no
a9df0 74 20 6e 75 6c 6c 2c 20 63 61 6c 6c 20 74 68 69  t null, call thi
a9e00 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64 65  s function to de
a9e10 6c 65 74 65 20 4d 65 6d 2e 7a 20 2a 2f 0a 20 20  lete Mem.z */.  
a9e20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20  char *zMalloc;  
a9e30 20 20 20 20 2f 2a 20 44 79 6e 61 6d 69 63 20 62      /* Dynamic b
a9e40 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20  uffer allocated 
a9e50 62 79 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  by sqlite3_mallo
a9e60 63 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 4f 6e  c() */.};../* On
a9e70 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65  e or more of the
a9e80 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73   following flags
a9e90 20 61 72 65 20 73 65 74 20 74 6f 20 69 6e 64 69   are set to indi
a9ea0 63 61 74 65 20 74 68 65 20 76 61 6c 69 64 4f 4b  cate the validOK
a9eb0 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 69  .** representati
a9ec0 6f 6e 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65  ons of the value
a9ed0 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 4d   stored in the M
a9ee0 65 6d 20 73 74 72 75 63 74 2e 0a 2a 2a 0a 2a 2a  em struct..**.**
a9ef0 20 49 66 20 74 68 65 20 4d 45 4d 5f 4e 75 6c 6c   If the MEM_Null
a9f00 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68   flag is set, th
a9f10 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  en the value is 
a9f20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76 61 6c 75  an SQL NULL valu
a9f30 65 2e 0a 2a 2a 20 4e 6f 20 6f 74 68 65 72 20 66  e..** No other f
a9f40 6c 61 67 73 20 6d 61 79 20 62 65 20 73 65 74 20  lags may be set 
a9f50 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a  in this case..**
a9f60 0a 2a 2a 20 49 66 20 74 68 65 20 4d 45 4d 5f 53  .** If the MEM_S
a9f70 74 72 20 66 6c 61 67 20 69 73 20 73 65 74 20 74  tr flag is set t
a9f80 68 65 6e 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73  hen Mem.z points
a9f90 20 61 74 20 61 20 73 74 72 69 6e 67 20 72 65 70   at a string rep
a9fa0 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20  resentation..** 
a9fb0 55 73 75 61 6c 6c 79 20 74 68 69 73 20 69 73 20  Usually this is 
a9fc0 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65 20 73  encoded in the s
a9fd0 61 6d 65 20 75 6e 69 63 6f 64 65 20 65 6e 63 6f  ame unicode enco
a9fe0 64 69 6e 67 20 61 73 20 74 68 65 20 6d 61 69 6e  ding as the main
a9ff0 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 28 73 65  .** database (se
aa000 65 20 62 65 6c 6f 77 20 66 6f 72 20 65 78 63 65  e below for exce
aa010 70 74 69 6f 6e 73 29 2e 20 49 66 20 74 68 65 20  ptions). If the 
aa020 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67 20 69 73  MEM_Term flag is
aa030 20 61 6c 73 6f 0a 2a 2a 20 73 65 74 2c 20 74 68   also.** set, th
aa040 65 6e 20 74 68 65 20 73 74 72 69 6e 67 20 69 73  en the string is
aa050 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 2e   nul terminated.
aa060 20 54 68 65 20 4d 45 4d 5f 49 6e 74 20 61 6e 64   The MEM_Int and
aa070 20 4d 45 4d 5f 52 65 61 6c 20 0a 2a 2a 20 66 6c   MEM_Real .** fl
aa080 61 67 73 20 6d 61 79 20 63 6f 65 78 69 73 74 20  ags may coexist 
aa090 77 69 74 68 20 74 68 65 20 4d 45 4d 5f 53 74 72  with the MEM_Str
aa0a0 20 66 6c 61 67 2e 0a 2a 2a 0a 2a 2a 20 4d 75 6c   flag..**.** Mul
aa0b0 74 69 70 6c 65 20 6f 66 20 74 68 65 73 65 20 76  tiple of these v
aa0c0 61 6c 75 65 73 20 63 61 6e 20 61 70 70 65 61 72  alues can appear
aa0d0 20 69 6e 20 4d 65 6d 2e 66 6c 61 67 73 2e 20 20   in Mem.flags.  
aa0e0 42 75 74 20 6f 6e 6c 79 20 6f 6e 65 0a 2a 2a 20  But only one.** 
aa0f0 61 74 20 61 20 74 69 6d 65 20 63 61 6e 20 61 70  at a time can ap
aa100 70 65 61 72 20 69 6e 20 4d 65 6d 2e 74 79 70 65  pear in Mem.type
aa110 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  ..*/.#define MEM
aa120 5f 4e 75 6c 6c 20 20 20 20 20 20 30 78 30 30 30  _Null      0x000
aa130 31 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20  1   /* Value is 
aa140 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20  NULL */.#define 
aa150 4d 45 4d 5f 53 74 72 20 20 20 20 20 20 20 30 78  MEM_Str       0x
aa160 30 30 30 32 20 20 20 2f 2a 20 56 61 6c 75 65 20  0002   /* Value 
aa170 69 73 20 61 20 73 74 72 69 6e 67 20 2a 2f 0a 23  is a string */.#
aa180 64 65 66 69 6e 65 20 4d 45 4d 5f 49 6e 74 20 20  define MEM_Int  
aa190 20 20 20 20 20 30 78 30 30 30 34 20 20 20 2f 2a       0x0004   /*
aa1a0 20 56 61 6c 75 65 20 69 73 20 61 6e 20 69 6e 74   Value is an int
aa1b0 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20  eger */.#define 
aa1c0 4d 45 4d 5f 52 65 61 6c 20 20 20 20 20 20 30 78  MEM_Real      0x
aa1d0 30 30 30 38 20 20 20 2f 2a 20 56 61 6c 75 65 20  0008   /* Value 
aa1e0 69 73 20 61 20 72 65 61 6c 20 6e 75 6d 62 65 72  is a real number
aa1f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f   */.#define MEM_
aa200 42 6c 6f 62 20 20 20 20 20 20 30 78 30 30 31 30  Blob      0x0010
aa210 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61     /* Value is a
aa220 20 42 4c 4f 42 20 2a 2f 0a 23 64 65 66 69 6e 65   BLOB */.#define
aa230 20 4d 45 4d 5f 52 6f 77 53 65 74 20 20 20 20 30   MEM_RowSet    0
aa240 78 30 30 32 30 20 20 20 2f 2a 20 56 61 6c 75 65  x0020   /* Value
aa250 20 69 73 20 61 20 52 6f 77 53 65 74 20 6f 62 6a   is a RowSet obj
aa260 65 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  ect */.#define M
aa270 45 4d 5f 46 72 61 6d 65 20 20 20 20 20 30 78 30  EM_Frame     0x0
aa280 30 34 30 20 20 20 2f 2a 20 56 61 6c 75 65 20 69  040   /* Value i
aa290 73 20 61 20 56 64 62 65 46 72 61 6d 65 20 6f 62  s a VdbeFrame ob
aa2a0 6a 65 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ject */.#define 
aa2b0 4d 45 4d 5f 54 79 70 65 4d 61 73 6b 20 20 30 78  MEM_TypeMask  0x
aa2c0 30 30 66 66 20 20 20 2f 2a 20 4d 61 73 6b 20 6f  00ff   /* Mask o
aa2d0 66 20 74 79 70 65 20 62 69 74 73 20 2a 2f 0a 0a  f type bits */..
aa2e0 2f 2a 20 57 68 65 6e 65 76 65 72 20 4d 65 6d 20  /* Whenever Mem 
aa2f0 63 6f 6e 74 61 69 6e 73 20 61 20 76 61 6c 69 64  contains a valid
aa300 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20   string or blob 
aa310 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c 20  representation, 
aa320 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f  one of.** the fo
aa330 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 6d 75  llowing flags mu
aa340 73 74 20 62 65 20 73 65 74 20 74 6f 20 64 65 74  st be set to det
aa350 65 72 6d 69 6e 65 20 74 68 65 20 6d 65 6d 6f 72  ermine the memor
aa360 79 20 6d 61 6e 61 67 65 6d 65 6e 74 0a 2a 2a 20  y management.** 
aa370 70 6f 6c 69 63 79 20 66 6f 72 20 4d 65 6d 2e 7a  policy for Mem.z
aa380 2e 20 20 54 68 65 20 4d 45 4d 5f 54 65 72 6d 20  .  The MEM_Term 
aa390 66 6c 61 67 20 74 65 6c 6c 73 20 75 73 20 77 68  flag tells us wh
aa3a0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
aa3b0 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 5c 30  .** string is \0
aa3c0 30 30 20 6f 72 20 5c 75 30 30 30 30 20 74 65 72  00 or \u0000 ter
aa3d0 6d 69 6e 61 74 65 64 0a 2a 2f 0a 23 64 65 66 69  minated.*/.#defi
aa3e0 6e 65 20 4d 45 4d 5f 54 65 72 6d 20 20 20 20 20  ne MEM_Term     
aa3f0 20 30 78 30 32 30 30 20 20 20 2f 2a 20 53 74 72   0x0200   /* Str
aa400 69 6e 67 20 72 65 70 20 69 73 20 6e 75 6c 20 74  ing rep is nul t
aa410 65 72 6d 69 6e 61 74 65 64 20 2a 2f 0a 23 64 65  erminated */.#de
aa420 66 69 6e 65 20 4d 45 4d 5f 44 79 6e 20 20 20 20  fine MEM_Dyn    
aa430 20 20 20 30 78 30 34 30 30 20 20 20 2f 2a 20 4e     0x0400   /* N
aa440 65 65 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69  eed to call sqli
aa450 74 65 46 72 65 65 28 29 20 6f 6e 20 4d 65 6d 2e  teFree() on Mem.
aa460 7a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  z */.#define MEM
aa470 5f 53 74 61 74 69 63 20 20 20 20 30 78 30 38 30  _Static    0x080
aa480 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69  0   /* Mem.z poi
aa490 6e 74 73 20 74 6f 20 61 20 73 74 61 74 69 63 20  nts to a static 
aa4a0 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e  string */.#defin
aa4b0 65 20 4d 45 4d 5f 45 70 68 65 6d 20 20 20 20 20  e MEM_Ephem     
aa4c0 30 78 31 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e  0x1000   /* Mem.
aa4d0 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65  z points to an e
aa4e0 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e 67 20  phemeral string 
aa4f0 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 41  */.#define MEM_A
aa500 67 67 20 20 20 20 20 20 20 30 78 32 30 30 30 20  gg       0x2000 
aa510 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74    /* Mem.z point
aa520 73 20 74 6f 20 61 6e 20 61 67 67 20 66 75 6e 63  s to an agg func
aa530 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  tion context */.
aa540 23 64 65 66 69 6e 65 20 4d 45 4d 5f 5a 65 72 6f  #define MEM_Zero
aa550 20 20 20 20 20 20 30 78 34 30 30 30 20 20 20 2f        0x4000   /
aa560 2a 20 4d 65 6d 2e 69 20 63 6f 6e 74 61 69 6e 73  * Mem.i contains
aa570 20 63 6f 75 6e 74 20 6f 66 20 30 73 20 61 70 70   count of 0s app
aa580 65 6e 64 65 64 20 74 6f 20 62 6c 6f 62 20 2a 2f  ended to blob */
aa590 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
aa5a0 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20  OMIT_INCRBLOB.  
aa5b0 23 75 6e 64 65 66 20 4d 45 4d 5f 5a 65 72 6f 0a  #undef MEM_Zero.
aa5c0 20 20 23 64 65 66 69 6e 65 20 4d 45 4d 5f 5a 65    #define MEM_Ze
aa5d0 72 6f 20 30 78 30 30 30 30 0a 23 65 6e 64 69 66  ro 0x0000.#endif
aa5e0 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61  .../*.** Clear a
aa5f0 6e 79 20 65 78 69 73 74 69 6e 67 20 74 79 70 65  ny existing type
aa600 20 66 6c 61 67 73 20 66 72 6f 6d 20 61 20 4d 65   flags from a Me
aa610 6d 20 61 6e 64 20 72 65 70 6c 61 63 65 20 74 68  m and replace th
aa620 65 6d 20 77 69 74 68 20 66 0a 2a 2f 0a 23 64 65  em with f.*/.#de
aa630 66 69 6e 65 20 4d 65 6d 53 65 74 54 79 70 65 46  fine MemSetTypeF
aa640 6c 61 67 28 70 2c 20 66 29 20 5c 0a 20 20 20 28  lag(p, f) \.   (
aa650 28 70 29 2d 3e 66 6c 61 67 73 20 3d 20 28 28 70  (p)->flags = ((p
aa660 29 2d 3e 66 6c 61 67 73 26 7e 28 4d 45 4d 5f 54  )->flags&~(MEM_T
aa670 79 70 65 4d 61 73 6b 7c 4d 45 4d 5f 5a 65 72 6f  ypeMask|MEM_Zero
aa680 29 29 7c 66 29 0a 0a 0a 2f 2a 20 41 20 56 64 62  ))|f).../* A Vdb
aa690 65 46 75 6e 63 20 69 73 20 6a 75 73 74 20 61 20  eFunc is just a 
aa6a0 46 75 6e 63 44 65 66 20 28 64 65 66 69 6e 65 64  FuncDef (defined
aa6b0 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 29   in sqliteInt.h)
aa6c0 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a   that contains.*
aa6d0 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  * additional inf
aa6e0 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61  ormation about a
aa6f0 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61  uxiliary informa
aa700 74 69 6f 6e 20 62 6f 75 6e 64 20 74 6f 20 61 72  tion bound to ar
aa710 67 75 6d 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68  guments.** of th
aa720 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69  e function.  Thi
aa730 73 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70  s is used to imp
aa740 6c 65 6d 65 6e 74 20 74 68 65 20 73 71 6c 69 74  lement the sqlit
aa750 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29  e3_get_auxdata()
aa760 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  .** and sqlite3_
aa770 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 41 50  set_auxdata() AP
aa780 49 73 2e 20 20 54 68 65 20 22 61 75 78 64 61 74  Is.  The "auxdat
aa790 61 22 20 69 73 20 73 6f 6d 65 20 61 75 78 69 6c  a" is some auxil
aa7a0 69 61 72 79 20 64 61 74 61 0a 2a 2a 20 74 68 61  iary data.** tha
aa7b0 74 20 63 61 6e 20 62 65 20 61 73 73 6f 63 69 61  t can be associa
aa7c0 74 65 64 20 77 69 74 68 20 61 20 63 6f 6e 73 74  ted with a const
aa7d0 61 6e 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ant argument to 
aa7e0 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69  a function.  Thi
aa7f0 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 66 75 6e 63  s.** allows func
aa800 74 69 6f 6e 73 20 73 75 63 68 20 61 73 20 22 72  tions such as "r
aa810 65 67 65 78 70 22 20 74 6f 20 63 6f 6d 70 69 6c  egexp" to compil
aa820 65 20 74 68 65 69 72 20 63 6f 6e 73 74 61 6e 74  e their constant
aa830 20 72 65 67 75 6c 61 72 0a 2a 2a 20 65 78 70 72   regular.** expr
aa840 65 73 73 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20  ession argument 
aa850 6f 6e 63 65 20 61 6e 64 20 72 65 75 73 65 64 20  once and reused 
aa860 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 63 6f 64  the compiled cod
aa870 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c 65 0a 2a  e for multiple.*
aa880 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a 2a  * invocations..*
aa890 2f 0a 73 74 72 75 63 74 20 56 64 62 65 46 75 6e  /.struct VdbeFun
aa8a0 63 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70  c {.  FuncDef *p
aa8b0 46 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20  Func;           
aa8c0 20 20 20 20 2f 2a 20 54 68 65 20 64 65 66 69 6e      /* The defin
aa8d0 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 75 6e  ition of the fun
aa8e0 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e  ction */.  int n
aa8f0 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Aux;            
aa900 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
aa910 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c  er of entries al
aa920 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 70 41 75  located for apAu
aa930 78 5b 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  x[] */.  struct 
aa940 41 75 78 44 61 74 61 20 7b 0a 20 20 20 20 76 6f  AuxData {.    vo
aa950 69 64 20 2a 70 41 75 78 3b 20 20 20 20 20 20 20  id *pAux;       
aa960 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
aa970 75 78 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  ux data for the 
aa980 69 2d 74 68 20 61 72 67 75 6d 65 6e 74 20 2a 2f  i-th argument */
aa990 0a 20 20 20 20 76 6f 69 64 20 28 2a 78 44 65 6c  .    void (*xDel
aa9a0 65 74 65 29 28 76 6f 69 64 20 2a 29 3b 20 20 20  ete)(void *);   
aa9b0 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72     /* Destructor
aa9c0 20 66 6f 72 20 74 68 65 20 61 75 78 20 64 61 74   for the aux dat
aa9d0 61 20 2a 2f 0a 20 20 7d 20 61 70 41 75 78 5b 31  a */.  } apAux[1
aa9e0 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ];              
aa9f0 20 20 20 20 20 2f 2a 20 4f 6e 65 20 73 6c 6f 74       /* One slot
aaa00 20 66 6f 72 20 65 61 63 68 20 66 75 6e 63 74 69   for each functi
aaa10 6f 6e 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 7d  on argument */.}
aaa20 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 63 6f  ;../*.** The "co
aaa30 6e 74 65 78 74 22 20 61 72 67 75 6d 65 6e 74 20  ntext" argument 
aaa40 66 6f 72 20 61 20 69 6e 73 74 61 6c 6c 61 62 6c  for a installabl
aaa50 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 20 70  e function.  A p
aaa60 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20  ointer to an.** 
aaa70 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
aaa80 20 73 74 72 75 63 74 75 72 65 20 69 73 20 74 68   structure is th
aaa90 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
aaaa0 20 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73   to the routines
aaab0 20 75 73 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d 65   used.** impleme
aaac0 6e 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74  nt the SQL funct
aaad0 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  ions..**.** Ther
aaae0 65 20 69 73 20 61 20 74 79 70 65 64 65 66 20 66  e is a typedef f
aaaf0 6f 72 20 74 68 69 73 20 73 74 72 75 63 74 75 72  or this structur
aab00 65 20 69 6e 20 73 71 6c 69 74 65 2e 68 2e 20 20  e in sqlite.h.  
aab10 53 6f 20 61 6c 6c 20 72 6f 75 74 69 6e 65 73 2c  So all routines,
aab20 0a 2a 2a 20 65 76 65 6e 20 74 68 65 20 70 75 62  .** even the pub
aab30 6c 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 6f  lic interface to
aab40 20 53 51 4c 69 74 65 2c 20 63 61 6e 20 75 73 65   SQLite, can use
aab50 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
aab60 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  is structure..**
aab70 20 42 75 74 20 74 68 69 73 20 66 69 6c 65 20 69   But this file i
aab80 73 20 74 68 65 20 6f 6e 6c 79 20 70 6c 61 63 65  s the only place
aab90 20 77 68 65 72 65 20 74 68 65 20 69 6e 74 65 72   where the inter
aaba0 6e 61 6c 20 64 65 74 61 69 6c 73 20 6f 66 20 74  nal details of t
aabb0 68 69 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  his.** structure
aabc0 20 61 72 65 20 6b 6e 6f 77 6e 2e 0a 2a 2a 0a 2a   are known..**.*
aabd0 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65  * This structure
aabe0 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e 73 69   is defined insi
aabf0 64 65 20 6f 66 20 76 64 62 65 49 6e 74 2e 68 20  de of vdbeInt.h 
aac00 62 65 63 61 75 73 65 20 69 74 20 75 73 65 73 20  because it uses 
aac10 73 75 62 73 74 72 75 63 74 75 72 65 73 0a 2a 2a  substructures.**
aac20 20 28 4d 65 6d 29 20 77 68 69 63 68 20 61 72 65   (Mem) which are
aac30 20 6f 6e 6c 79 20 64 65 66 69 6e 65 64 20 74 68   only defined th
aac40 65 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73  ere..*/.struct s
aac50 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 7b  qlite3_context {
aac60 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e  .  FuncDef *pFun
aac70 63 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  c;       /* Poin
aac80 74 65 72 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20  ter to function 
aac90 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 4d 55  information.  MU
aaca0 53 54 20 42 45 20 46 49 52 53 54 20 2a 2f 0a 20  ST BE FIRST */. 
aacb0 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65   VdbeFunc *pVdbe
aacc0 46 75 6e 63 3b 20 20 2f 2a 20 41 75 78 69 6c 61  Func;  /* Auxila
aacd0 72 79 20 64 61 74 61 2c 20 69 66 20 63 72 65 61  ry data, if crea
aace0 74 65 64 2e 20 2a 2f 0a 20 20 4d 65 6d 20 73 3b  ted. */.  Mem s;
aacf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aad00 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  /* The return va
aad10 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 68 65  lue is stored he
aad20 72 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65  re */.  Mem *pMe
aad30 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  m;            /*
aad40 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65   Memory cell use
aad50 64 20 74 6f 20 73 74 6f 72 65 20 61 67 67 72 65  d to store aggre
aad60 67 61 74 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  gate context */.
aad70 20 20 69 6e 74 20 69 73 45 72 72 6f 72 3b 20 20    int isError;  
aad80 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72          /* Error
aad90 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20 62   code returned b
aada0 79 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20  y the function. 
aadb0 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43  */.  CollSeq *pC
aadc0 6f 6c 6c 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f  oll;       /* Co
aadd0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
aade0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20   */.};../*.** A 
aadf0 53 65 74 20 73 74 72 75 63 74 75 72 65 20 69 73  Set structure is
aae00 20 75 73 65 64 20 66 6f 72 20 71 75 69 63 6b 20   used for quick 
aae10 74 65 73 74 69 6e 67 20 74 6f 20 73 65 65 20 69  testing to see i
aae20 66 20 61 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20  f a value.** is 
aae30 70 61 72 74 20 6f 66 20 61 20 73 6d 61 6c 6c 20  part of a small 
aae40 73 65 74 2e 20 20 53 65 74 73 20 61 72 65 20 75  set.  Sets are u
aae50 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  sed to implement
aae60 20 63 6f 64 65 20 6c 69 6b 65 0a 2a 2a 20 74 68   code like.** th
aae70 69 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  is:.**          
aae80 20 20 78 2e 79 20 49 4e 20 28 27 68 69 27 2c 27    x.y IN ('hi','
aae90 68 6f 6f 27 2c 27 68 75 6d 27 29 0a 2a 2f 0a 74  hoo','hum').*/.t
aaea0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53 65  ypedef struct Se
aaeb0 74 20 53 65 74 3b 0a 73 74 72 75 63 74 20 53 65  t Set;.struct Se
aaec0 74 20 7b 0a 20 20 48 61 73 68 20 68 61 73 68 3b  t {.  Hash hash;
aaed0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
aaee0 41 20 73 65 74 20 69 73 20 6a 75 73 74 20 61 20  A set is just a 
aaef0 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hash table */.  
aaf00 48 61 73 68 45 6c 65 6d 20 2a 70 72 65 76 3b 20  HashElem *prev; 
aaf10 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f         /* Previo
aaf20 75 73 6c 79 20 61 63 63 65 73 73 65 64 20 68 61  usly accessed ha
aaf30 73 68 20 65 6c 65 6d 65 6e 20 2a 2f 0a 7d 3b 0a  sh elemen */.};.
aaf40 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
aaf50 63 65 20 6f 66 20 74 68 65 20 76 69 72 74 75 61  ce of the virtua
aaf60 6c 20 6d 61 63 68 69 6e 65 2e 20 20 54 68 69 73  l machine.  This
aaf70 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
aaf80 69 6e 73 20 74 68 65 20 63 6f 6d 70 6c 65 74 65  ins the complete
aaf90 0a 2a 2a 20 73 74 61 74 65 20 6f 66 20 74 68 65  .** state of the
aafa0 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
aafb0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 73 71 6c  ..**.** The "sql
aafc0 69 74 65 33 5f 73 74 6d 74 22 20 73 74 72 75 63  ite3_stmt" struc
aafd0 74 75 72 65 20 70 6f 69 6e 74 65 72 20 74 68 61  ture pointer tha
aafe0 74 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 79  t is returned by
aaff0 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65   sqlite3_compile
ab000 28 29 0a 2a 2a 20 69 73 20 72 65 61 6c 6c 79 20  ().** is really 
ab010 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
ab020 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
ab030 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a   structure..**.*
ab040 2a 20 54 68 65 20 56 64 62 65 2e 69 6e 56 74 61  * The Vdbe.inVta
ab050 62 4d 65 74 68 6f 64 20 76 61 72 69 61 62 6c 65  bMethod variable
ab060 20 69 73 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a   is set to non-z
ab070 65 72 6f 20 66 6f 72 20 74 68 65 20 64 75 72 61  ero for the dura
ab080 74 69 6f 6e 20 6f 66 0a 2a 2a 20 61 6e 79 20 76  tion of.** any v
ab090 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 65 74  irtual table met
ab0a0 68 6f 64 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20  hod invocations 
ab0b0 6d 61 64 65 20 62 79 20 74 68 65 20 76 64 62 65  made by the vdbe
ab0c0 20 70 72 6f 67 72 61 6d 2e 20 49 74 20 69 73 0a   program. It is.
ab0d0 2a 2a 20 73 65 74 20 74 6f 20 32 20 66 6f 72 20  ** set to 2 for 
ab0e0 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 20  xDestroy method 
ab0f0 63 61 6c 6c 73 20 61 6e 64 20 31 20 66 6f 72 20  calls and 1 for 
ab100 61 6c 6c 20 6f 74 68 65 72 20 6d 65 74 68 6f 64  all other method
ab110 73 2e 20 54 68 69 73 0a 2a 2a 20 76 61 72 69 61  s. This.** varia
ab120 62 6c 65 20 69 73 20 75 73 65 64 20 66 6f 72 20  ble is used for 
ab130 74 77 6f 20 70 75 72 70 6f 73 65 73 3a 20 74 6f  two purposes: to
ab140 20 61 6c 6c 6f 77 20 78 44 65 73 74 72 6f 79 20   allow xDestroy 
ab150 6d 65 74 68 6f 64 73 20 74 6f 20 65 78 65 63 75  methods to execu
ab160 74 65 0a 2a 2a 20 22 44 52 4f 50 20 54 41 42 4c  te.** "DROP TABL
ab170 45 22 20 73 74 61 74 65 6d 65 6e 74 73 20 61 6e  E" statements an
ab180 64 20 74 6f 20 70 72 65 76 65 6e 74 20 73 6f 6d  d to prevent som
ab190 65 20 6e 61 73 74 79 20 73 69 64 65 20 65 66 66  e nasty side eff
ab1a0 65 63 74 73 20 6f 66 0a 2a 2a 20 6d 61 6c 6c 6f  ects of.** mallo
ab1b0 63 20 66 61 69 6c 75 72 65 20 77 68 65 6e 20 53  c failure when S
ab1c0 51 4c 69 74 65 20 69 73 20 69 6e 76 6f 6b 65 64  QLite is invoked
ab1d0 20 72 65 63 75 72 73 69 76 65 6c 79 20 62 79 20   recursively by 
ab1e0 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
ab1f0 0a 2a 2a 20 6d 65 74 68 6f 64 20 66 75 6e 63 74  .** method funct
ab200 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56  ion..*/.struct V
ab210 64 62 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20  dbe {.  sqlite3 
ab220 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  *db;            
ab230 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
ab240 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20  connection that 
ab250 6f 77 6e 73 20 74 68 69 73 20 73 74 61 74 65 6d  owns this statem
ab260 65 6e 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70  ent */.  Vdbe *p
ab270 50 72 65 76 2c 2a 70 4e 65 78 74 3b 20 20 20 20  Prev,*pNext;    
ab280 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20   /* Linked list 
ab290 6f 66 20 56 44 42 45 73 20 77 69 74 68 20 74 68  of VDBEs with th
ab2a0 65 20 73 61 6d 65 20 56 64 62 65 2e 64 62 20 2a  e same Vdbe.db *
ab2b0 2f 0a 20 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20  /.  int nOp;    
ab2c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
ab2d0 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 63  umber of instruc
ab2e0 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 70 72 6f  tions in the pro
ab2f0 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f  gram */.  int nO
ab300 70 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20  pAlloc;         
ab310 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73    /* Number of s
ab320 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  lots allocated f
ab330 6f 72 20 61 4f 70 5b 5d 20 2a 2f 0a 20 20 4f 70  or aOp[] */.  Op
ab340 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20 20 20   *aOp;          
ab350 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74        /* Space t
ab360 6f 20 68 6f 6c 64 20 74 68 65 20 76 69 72 74 75  o hold the virtu
ab370 61 6c 20 6d 61 63 68 69 6e 65 27 73 20 70 72 6f  al machine's pro
ab380 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c  gram */.  int nL
ab390 61 62 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20  abel;           
ab3a0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c    /* Number of l
ab3b0 61 62 65 6c 73 20 75 73 65 64 20 2a 2f 0a 20 20  abels used */.  
ab3c0 69 6e 74 20 6e 4c 61 62 65 6c 41 6c 6c 6f 63 3b  int nLabelAlloc;
ab3d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
ab3e0 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63  r of slots alloc
ab3f0 61 74 65 64 20 69 6e 20 61 4c 61 62 65 6c 5b 5d  ated in aLabel[]
ab400 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 4c 61 62 65   */.  int *aLabe
ab410 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  l;            /*
ab420 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
ab430 68 65 20 6c 61 62 65 6c 73 20 2a 2f 0a 20 20 4d  he labels */.  M
ab440 65 6d 20 2a 2a 61 70 41 72 67 3b 20 20 20 20 20  em **apArg;     
ab450 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
ab460 6e 74 73 20 74 6f 20 63 75 72 72 65 6e 74 6c 79  nts to currently
ab470 20 65 78 65 63 75 74 69 6e 67 20 75 73 65 72 20   executing user 
ab480 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 4d 65  function */.  Me
ab490 6d 20 2a 61 43 6f 6c 4e 61 6d 65 3b 20 20 20 20  m *aColName;    
ab4a0 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20        /* Column 
ab4b0 6e 61 6d 65 73 20 74 6f 20 72 65 74 75 72 6e 20  names to return 
ab4c0 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 73 75 6c  */.  Mem *pResul
ab4d0 74 53 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  tSet;        /* 
ab4e0 50 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72  Pointer to an ar
ab4f0 72 61 79 20 6f 66 20 72 65 73 75 6c 74 73 20 2a  ray of results *
ab500 2f 0a 20 20 75 31 36 20 6e 52 65 73 43 6f 6c 75  /.  u16 nResColu
ab510 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  mn;         /* N
ab520 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
ab530 20 69 6e 20 6f 6e 65 20 72 6f 77 20 6f 66 20 74   in one row of t
ab540 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f  he result set */
ab550 0a 20 20 75 31 36 20 6e 43 75 72 73 6f 72 3b 20  .  u16 nCursor; 
ab560 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
ab570 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 69 6e  mber of slots in
ab580 20 61 70 43 73 72 5b 5d 20 2a 2f 0a 20 20 56 64   apCsr[] */.  Vd
ab590 62 65 43 75 72 73 6f 72 20 2a 2a 61 70 43 73 72  beCursor **apCsr
ab5a0 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6c 65  ;     /* One ele
ab5b0 6d 65 6e 74 20 6f 66 20 74 68 69 73 20 61 72 72  ment of this arr
ab5c0 61 79 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e  ay for each open
ab5d0 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 75 38 20   cursor */.  u8 
ab5e0 65 72 72 6f 72 41 63 74 69 6f 6e 3b 20 20 20 20  errorAction;    
ab5f0 20 20 20 20 20 2f 2a 20 52 65 63 6f 76 65 72 79       /* Recovery
ab600 20 61 63 74 69 6f 6e 20 74 6f 20 64 6f 20 69 6e   action to do in
ab610 20 63 61 73 65 20 6f 66 20 61 6e 20 65 72 72 6f   case of an erro
ab620 72 20 2a 2f 0a 20 20 75 38 20 6f 6b 56 61 72 3b  r */.  u8 okVar;
ab630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ab640 2a 20 54 72 75 65 20 69 66 20 61 7a 56 61 72 5b  * True if azVar[
ab650 5d 20 68 61 73 20 62 65 65 6e 20 69 6e 69 74 69  ] has been initi
ab660 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 75 31 36 20  alized */.  u16 
ab670 6e 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20  nVar;           
ab680 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
ab690 20 65 6e 74 72 69 65 73 20 69 6e 20 61 56 61 72   entries in aVar
ab6a0 5b 5d 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 56 61  [] */.  Mem *aVa
ab6b0 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
ab6c0 2f 2a 20 56 61 6c 75 65 73 20 66 6f 72 20 74 68  /* Values for th
ab6d0 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 6f 70  e OP_Variable op
ab6e0 63 6f 64 65 2e 20 2a 2f 0a 20 20 63 68 61 72 20  code. */.  char 
ab6f0 2a 2a 61 7a 56 61 72 3b 20 20 20 20 20 20 20 20  **azVar;        
ab700 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 76 61     /* Name of va
ab710 72 69 61 62 6c 65 73 20 2a 2f 0a 20 20 75 33 32  riables */.  u32
ab720 20 6d 61 67 69 63 3b 20 20 20 20 20 20 20 20 20   magic;         
ab730 20 20 20 20 20 2f 2a 20 4d 61 67 69 63 20 6e 75       /* Magic nu
ab740 6d 62 65 72 20 66 6f 72 20 73 61 6e 69 74 79 20  mber for sanity 
ab750 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 69 6e  checking */.  in
ab760 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20  t nMem;         
ab770 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
ab780 6f 66 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69  of memory locati
ab790 6f 6e 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6c  ons currently al
ab7a0 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 4d 65 6d  located */.  Mem
ab7b0 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 20   *aMem;         
ab7c0 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 65 6d 6f       /* The memo
ab7d0 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a  ry locations */.
ab7e0 20 20 75 33 32 20 63 61 63 68 65 43 74 72 3b 20    u32 cacheCtr; 
ab7f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 64 62            /* Vdb
ab800 65 43 75 72 73 6f 72 20 72 6f 77 20 63 61 63 68  eCursor row cach
ab810 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 63 6f 75  e generation cou
ab820 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63  nter */.  int pc
ab830 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ab840 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 6d    /* The program
ab850 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
ab860 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
ab870 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74        /* Value t
ab880 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 63 68  o return */.  ch
ab890 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20  ar *zErrMsg;    
ab8a0 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d        /* Error m
ab8b0 65 73 73 61 67 65 20 77 72 69 74 74 65 6e 20 68  essage written h
ab8c0 65 72 65 20 2a 2f 0a 20 20 75 38 20 65 78 70 6c  ere */.  u8 expl
ab8d0 61 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  ain;            
ab8e0 20 2f 2a 20 54 72 75 65 20 69 66 20 45 58 50 4c   /* True if EXPL
ab8f0 41 49 4e 20 70 72 65 73 65 6e 74 20 6f 6e 20 53  AIN present on S
ab900 51 4c 20 63 6f 6d 6d 61 6e 64 20 2a 2f 0a 20 20  QL command */.  
ab910 75 38 20 63 68 61 6e 67 65 43 6e 74 4f 6e 3b 20  u8 changeCntOn; 
ab920 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
ab930 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 68  to update the ch
ab940 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a  ange-counter */.
ab950 20 20 75 38 20 65 78 70 69 72 65 64 3b 20 20 20    u8 expired;   
ab960 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
ab970 65 20 69 66 20 74 68 65 20 56 4d 20 6e 65 65 64  e if the VM need
ab980 73 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c  s to be recompil
ab990 65 64 20 2a 2f 0a 20 20 75 38 20 6d 69 6e 57 72  ed */.  u8 minWr
ab9a0 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 3b 20 20  iteFileFormat;  
ab9b0 2f 2a 20 4d 69 6e 69 6d 75 6d 20 66 69 6c 65 20  /* Minimum file 
ab9c0 66 6f 72 6d 61 74 20 66 6f 72 20 77 72 69 74 61  format for writa
ab9d0 62 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  ble database fil
ab9e0 65 73 20 2a 2f 0a 20 20 75 38 20 69 6e 56 74 61  es */.  u8 inVta
ab9f0 62 4d 65 74 68 6f 64 3b 20 20 20 20 20 20 20 20  bMethod;        
aba00 2f 2a 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20  /* See comments 
aba10 61 62 6f 76 65 20 2a 2f 0a 20 20 75 38 20 75 73  above */.  u8 us
aba20 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 3b 20 20  esStmtJournal;  
aba30 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 75 73     /* True if us
aba40 65 73 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6a  es a statement j
aba50 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 72  ournal */.  u8 r
aba60 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20  eadOnly;        
aba70 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
aba80 72 65 61 64 2d 6f 6e 6c 79 20 73 74 61 74 65 6d  read-only statem
aba90 65 6e 74 73 20 2a 2f 0a 20 20 75 38 20 69 73 50  ents */.  u8 isP
abaa0 72 65 70 61 72 65 56 32 3b 20 20 20 20 20 20 20  repareV2;       
abab0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 72 65    /* True if pre
abac0 70 61 72 65 64 20 77 69 74 68 20 70 72 65 70 61  pared with prepa
abad0 72 65 5f 76 32 28 29 20 2a 2f 0a 20 20 69 6e 74  re_v2() */.  int
abae0 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20   nChange;       
abaf0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
abb00 66 20 64 62 20 63 68 61 6e 67 65 73 20 6d 61 64  f db changes mad
abb10 65 20 73 69 6e 63 65 20 6c 61 73 74 20 72 65 73  e since last res
abb20 65 74 20 2a 2f 0a 20 20 69 6e 74 20 62 74 72 65  et */.  int btre
abb30 65 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20  eMask;          
abb40 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 64 62  /* Bitmask of db
abb50 2d 3e 61 44 62 5b 5d 20 65 6e 74 72 69 65 73 20  ->aDb[] entries 
abb60 72 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20  referenced */.  
abb70 69 36 34 20 73 74 61 72 74 54 69 6d 65 3b 20 20  i64 startTime;  
abb80 20 20 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 20          /* Time 
abb90 77 68 65 6e 20 71 75 65 72 79 20 73 74 61 72 74  when query start
abba0 65 64 20 2d 20 75 73 65 64 20 66 6f 72 20 70 72  ed - used for pr
abbb0 6f 66 69 6c 69 6e 67 20 2a 2f 0a 20 20 42 74 72  ofiling */.  Btr
abbc0 65 65 4d 75 74 65 78 41 72 72 61 79 20 61 4d 75  eeMutexArray aMu
abbd0 74 65 78 3b 20 2f 2a 20 41 6e 20 61 72 72 61 79  tex; /* An array
abbe0 20 6f 66 20 42 74 72 65 65 20 75 73 65 64 20 68   of Btree used h
abbf0 65 72 65 20 61 6e 64 20 6e 65 65 64 69 6e 67 20  ere and needing 
abc00 6c 6f 63 6b 73 20 2a 2f 0a 20 20 69 6e 74 20 61  locks */.  int a
abc10 43 6f 75 6e 74 65 72 5b 32 5d 3b 20 20 20 20 20  Counter[2];     
abc20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 73 20 75     /* Counters u
abc30 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 73  sed by sqlite3_s
abc40 74 6d 74 5f 73 74 61 74 75 73 28 29 20 2a 2f 0a  tmt_status() */.
abc50 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20 20    char *zSql;   
abc60 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
abc70 74 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61  t of the SQL sta
abc80 74 65 6d 65 6e 74 20 74 68 61 74 20 67 65 6e 65  tement that gene
abc90 72 61 74 65 64 20 74 68 69 73 20 2a 2f 0a 20 20  rated this */.  
abca0 76 6f 69 64 20 2a 70 46 72 65 65 3b 20 20 20 20  void *pFree;    
abcb0 20 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20          /* Free 
abcc0 74 68 69 73 20 77 68 65 6e 20 64 65 6c 65 74 69  this when deleti
abcd0 6e 67 20 74 68 65 20 76 64 62 65 20 2a 2f 0a 20  ng the vdbe */. 
abce0 20 69 36 34 20 6e 46 6b 43 6f 6e 73 74 72 61 69   i64 nFkConstrai
abcf0 6e 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  nt;      /* Numb
abd00 65 72 20 6f 66 20 69 6d 6d 2e 20 46 4b 20 63 6f  er of imm. FK co
abd10 6e 73 74 72 61 69 6e 74 73 20 74 68 69 73 20 56  nstraints this V
abd20 4d 20 2a 2f 0a 20 20 69 36 34 20 6e 53 74 6d 74  M */.  i64 nStmt
abd30 44 65 66 43 6f 6e 73 3b 20 20 20 20 20 20 20 2f  DefCons;       /
abd40 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 65 66 2e  * Number of def.
abd50 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 77 68 65   constraints whe
abd60 6e 20 73 74 6d 74 20 73 74 61 72 74 65 64 20 2a  n stmt started *
abd70 2f 0a 20 20 69 6e 74 20 69 53 74 61 74 65 6d 65  /.  int iStateme
abd80 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53  nt;         /* S
abd90 74 61 74 65 6d 65 6e 74 20 6e 75 6d 62 65 72 20  tatement number 
abda0 28 6f 72 20 30 20 69 66 20 68 61 73 20 6e 6f 74  (or 0 if has not
abdb0 20 6f 70 65 6e 65 64 20 73 74 6d 74 29 20 2a 2f   opened stmt) */
abdc0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
abdd0 45 42 55 47 0a 20 20 46 49 4c 45 20 2a 74 72 61  EBUG.  FILE *tra
abde0 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ce;            /
abdf0 2a 20 57 72 69 74 65 20 61 6e 20 65 78 65 63 75  * Write an execu
abe00 74 69 6f 6e 20 74 72 61 63 65 20 68 65 72 65 2c  tion trace here,
abe10 20 69 66 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a   if not NULL */.
abe20 23 65 6e 64 69 66 0a 20 20 56 64 62 65 46 72 61  #endif.  VdbeFra
abe30 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20  me *pFrame;     
abe40 20 2f 2a 20 50 61 72 65 6e 74 20 66 72 61 6d 65   /* Parent frame
abe50 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 61 6d 65   */.  int nFrame
abe60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
abe70 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 61 6d 65   Number of frame
abe80 73 20 69 6e 20 70 46 72 61 6d 65 20 6c 69 73 74  s in pFrame list
abe90 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
abea0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20  e following are 
abeb0 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 66  allowed values f
abec0 6f 72 20 56 64 62 65 2e 6d 61 67 69 63 0a 2a 2f  or Vdbe.magic.*/
abed0 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41  .#define VDBE_MA
abee0 47 49 43 5f 49 4e 49 54 20 20 20 20 20 30 78 32  GIC_INIT     0x2
abef0 36 62 63 65 61 61 35 20 20 20 20 2f 2a 20 42 75  6bceaa5    /* Bu
abf00 69 6c 64 69 6e 67 20 61 20 56 44 42 45 20 70 72  ilding a VDBE pr
abf10 6f 67 72 61 6d 20 2a 2f 0a 23 64 65 66 69 6e 65  ogram */.#define
abf20 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20   VDBE_MAGIC_RUN 
abf30 20 20 20 20 20 30 78 62 64 66 32 30 64 61 33 20       0xbdf20da3 
abf40 20 20 20 2f 2a 20 56 44 42 45 20 69 73 20 72 65     /* VDBE is re
abf50 61 64 79 20 74 6f 20 65 78 65 63 75 74 65 20 2a  ady to execute *
abf60 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d  /.#define VDBE_M
abf70 41 47 49 43 5f 48 41 4c 54 20 20 20 20 20 30 78  AGIC_HALT     0x
abf80 35 31 39 63 32 39 37 33 20 20 20 20 2f 2a 20 56  519c2973    /* V
abf90 44 42 45 20 68 61 73 20 63 6f 6d 70 6c 65 74 65  DBE has complete
abfa0 64 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a 23  d execution */.#
abfb0 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47 49  define VDBE_MAGI
abfc0 43 5f 44 45 41 44 20 20 20 20 20 30 78 62 36 30  C_DEAD     0xb60
abfd0 36 63 33 63 38 20 20 20 20 2f 2a 20 54 68 65 20  6c3c8    /* The 
abfe0 56 44 42 45 20 68 61 73 20 62 65 65 6e 20 64 65  VDBE has been de
abff0 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 2f 2a  allocated */../*
ac000 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 70 72 6f  .** Function pro
ac010 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 4c 49 54  totypes.*/.SQLIT
ac020 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
ac030 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75  qlite3VdbeFreeCu
ac040 72 73 6f 72 28 56 64 62 65 20 2a 2c 20 56 64 62  rsor(Vdbe *, Vdb
ac050 65 43 75 72 73 6f 72 2a 29 3b 0a 76 6f 69 64 20  eCursor*);.void 
ac060 73 71 6c 69 74 65 56 64 62 65 50 6f 70 53 74 61  sqliteVdbePopSta
ac070 63 6b 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a 53  ck(Vdbe*,int);.S
ac080 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
ac090 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  t sqlite3VdbeCur
ac0a0 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65 43 75  sorMoveto(VdbeCu
ac0b0 72 73 6f 72 2a 29 3b 0a 23 69 66 20 64 65 66 69  rsor*);.#if defi
ac0c0 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
ac0d0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42  ) || defined(VDB
ac0e0 45 5f 50 52 4f 46 49 4c 45 29 0a 53 51 4c 49 54  E_PROFILE).SQLIT
ac0f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
ac100 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f  qlite3VdbePrintO
ac110 70 28 46 49 4c 45 2a 2c 20 69 6e 74 2c 20 4f 70  p(FILE*, int, Op
ac120 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54  *);.#endif.SQLIT
ac130 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71  E_PRIVATE u32 sq
ac140 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54  lite3VdbeSerialT
ac150 79 70 65 4c 65 6e 28 75 33 32 29 3b 0a 53 51 4c  ypeLen(u32);.SQL
ac160 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20  ITE_PRIVATE u32 
ac170 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
ac180 6c 54 79 70 65 28 4d 65 6d 2a 2c 20 69 6e 74 29  lType(Mem*, int)
ac190 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
ac1a0 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 65   u32 sqlite3Vdbe
ac1b0 53 65 72 69 61 6c 50 75 74 28 75 6e 73 69 67 6e  SerialPut(unsign
ac1c0 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 4d  ed char*, int, M
ac1d0 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  em*, int);.SQLIT
ac1e0 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71  E_PRIVATE u32 sq
ac1f0 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47  lite3VdbeSerialG
ac200 65 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  et(const unsigne
ac210 64 20 63 68 61 72 2a 2c 20 75 33 32 2c 20 4d 65  d char*, u32, Me
ac220 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
ac230 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
ac240 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61 74  VdbeDeleteAuxDat
ac250 61 28 56 64 62 65 46 75 6e 63 2a 2c 20 69 6e 74  a(VdbeFunc*, int
ac260 29 3b 0a 0a 69 6e 74 20 73 71 6c 69 74 65 32 42  );..int sqlite2B
ac270 74 72 65 65 4b 65 79 43 6f 6d 70 61 72 65 28 42  treeKeyCompare(B
ac280 74 43 75 72 73 6f 72 20 2a 2c 20 63 6f 6e 73 74  tCursor *, const
ac290 20 76 6f 69 64 20 2a 2c 20 69 6e 74 2c 20 69 6e   void *, int, in
ac2a0 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54  t, int *);.SQLIT
ac2b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
ac2c0 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 79 43  lite3VdbeIdxKeyC
ac2d0 6f 6d 70 61 72 65 28 56 64 62 65 43 75 72 73 6f  ompare(VdbeCurso
ac2e0 72 2a 2c 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  r*,UnpackedRecor
ac2f0 64 2a 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45  d*,int*);.SQLITE
ac300 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
ac310 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 64  ite3VdbeIdxRowid
ac320 28 73 71 6c 69 74 65 33 2a 2c 20 42 74 43 75 72  (sqlite3*, BtCur
ac330 73 6f 72 20 2a 2c 20 69 36 34 20 2a 29 3b 0a 53  sor *, i64 *);.S
ac340 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
ac350 74 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70  t sqlite3MemComp
ac360 61 72 65 28 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20  are(const Mem*, 
ac370 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73  const Mem*, cons
ac380 74 20 43 6f 6c 6c 53 65 71 2a 29 3b 0a 53 51 4c  t CollSeq*);.SQL
ac390 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
ac3a0 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28  sqlite3VdbeExec(
ac3b0 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Vdbe*);.SQLITE_P
ac3c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
ac3d0 65 33 56 64 62 65 4c 69 73 74 28 56 64 62 65 2a  e3VdbeList(Vdbe*
ac3e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
ac3f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
ac400 65 48 61 6c 74 28 56 64 62 65 2a 29 3b 0a 53 51  eHalt(Vdbe*);.SQ
ac410 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
ac420 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
ac430 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d 20 2a  geEncoding(Mem *
ac440 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
ac450 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
ac460 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28  e3VdbeMemTooBig(
ac470 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Mem*);.SQLITE_PR
ac480 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
ac490 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 4d 65 6d  3VdbeMemCopy(Mem
ac4a0 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 29 3b 0a  *, const Mem*);.
ac4b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
ac4c0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
ac4d0 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 4d 65  emShallowCopy(Me
ac4e0 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20  m*, const Mem*, 
ac4f0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
ac500 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
ac510 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 4d 65 6d  3VdbeMemMove(Mem
ac520 2a 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  *, Mem*);.SQLITE
ac530 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
ac540 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65  ite3VdbeMemNulTe
ac550 72 6d 69 6e 61 74 65 28 4d 65 6d 2a 29 3b 0a 53  rminate(Mem*);.S
ac560 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
ac570 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
ac580 53 65 74 53 74 72 28 4d 65 6d 2a 2c 20 63 6f 6e  SetStr(Mem*, con
ac590 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 75  st char*, int, u
ac5a0 38 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  8, void(*)(void*
ac5b0 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ));.SQLITE_PRIVA
ac5c0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
ac5d0 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 4d  dbeMemSetInt64(M
ac5e0 65 6d 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54  em*, i64);.SQLIT
ac5f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
ac600 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
ac610 44 6f 75 62 6c 65 28 4d 65 6d 2a 2c 20 64 6f 75  Double(Mem*, dou
ac620 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ble);.SQLITE_PRI
ac630 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
ac640 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28  3VdbeMemSetNull(
ac650 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Mem*);.SQLITE_PR
ac660 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
ac670 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f  e3VdbeMemSetZero
ac680 42 6c 6f 62 28 4d 65 6d 2a 2c 69 6e 74 29 3b 0a  Blob(Mem*,int);.
ac690 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
ac6a0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
ac6b0 65 6d 53 65 74 52 6f 77 53 65 74 28 4d 65 6d 2a  emSetRowSet(Mem*
ac6c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
ac6d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
ac6e0 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c  eMemMakeWriteabl
ac6f0 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  e(Mem*);.SQLITE_
ac700 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
ac710 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67  te3VdbeMemString
ac720 69 66 79 28 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a  ify(Mem*, int);.
ac730 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
ac740 36 34 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e  64 sqlite3VdbeIn
ac750 74 56 61 6c 75 65 28 4d 65 6d 2a 29 3b 0a 53 51  tValue(Mem*);.SQ
ac760 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
ac770 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49   sqlite3VdbeMemI
ac780 6e 74 65 67 65 72 69 66 79 28 4d 65 6d 2a 29 3b  ntegerify(Mem*);
ac790 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ac7a0 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 56 64  double sqlite3Vd
ac7b0 62 65 52 65 61 6c 56 61 6c 75 65 28 4d 65 6d 2a  beRealValue(Mem*
ac7c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
ac7d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
ac7e0 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69 74  beIntegerAffinit
ac7f0 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  y(Mem*);.SQLITE_
ac800 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
ac810 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66  te3VdbeMemRealif
ac820 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  y(Mem*);.SQLITE_
ac830 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
ac840 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69  te3VdbeMemNumeri
ac850 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  fy(Mem*);.SQLITE
ac860 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
ac870 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42  ite3VdbeMemFromB
ac880 74 72 65 65 28 42 74 43 75 72 73 6f 72 2a 2c 69  tree(BtCursor*,i
ac890 6e 74 2c 69 6e 74 2c 69 6e 74 2c 4d 65 6d 2a 29  nt,int,int,Mem*)
ac8a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
ac8b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
ac8c0 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d 65 6d 20  eMemRelease(Mem 
ac8d0 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  *p);.SQLITE_PRIV
ac8e0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
ac8f0 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78  VdbeMemReleaseEx
ac900 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 29 3b 0a  ternal(Mem *p);.
ac910 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
ac920 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
ac930 6d 46 69 6e 61 6c 69 7a 65 28 4d 65 6d 2a 2c 20  mFinalize(Mem*, 
ac940 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 4c 49 54  FuncDef*);.SQLIT
ac950 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
ac960 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4f 70 63  char *sqlite3Opc
ac970 6f 64 65 4e 61 6d 65 28 69 6e 74 29 3b 0a 53 51  odeName(int);.SQ
ac980 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
ac990 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f   sqlite3VdbeOpco
ac9a0 64 65 48 61 73 50 72 6f 70 65 72 74 79 28 69 6e  deHasProperty(in
ac9b0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
ac9c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
ac9d0 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 4d  te3VdbeMemGrow(M
ac9e0 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 2c  em *pMem, int n,
ac9f0 20 69 6e 74 20 70 72 65 73 65 72 76 65 29 3b 0a   int preserve);.
aca00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
aca10 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c  nt sqlite3VdbeCl
aca20 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 56 64 62  oseStatement(Vdb
aca30 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  e *, int);.SQLIT
aca40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
aca50 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65 44  qlite3VdbeFrameD
aca60 65 6c 65 74 65 28 56 64 62 65 46 72 61 6d 65 2a  elete(VdbeFrame*
aca70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
aca80 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
aca90 65 46 72 61 6d 65 52 65 73 74 6f 72 65 28 56 64  eFrameRestore(Vd
acaa0 62 65 46 72 61 6d 65 20 2a 29 3b 0a 23 69 66 64  beFrame *);.#ifd
acab0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
acac0 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45  _MEMORY_MANAGEME
acad0 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  NT.SQLITE_PRIVAT
acae0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
acaf0 65 52 65 6c 65 61 73 65 42 75 66 66 65 72 73 28  eReleaseBuffers(
acb00 56 64 62 65 20 2a 70 29 3b 0a 23 65 6e 64 69 66  Vdbe *p);.#endif
acb10 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
acb20 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45  _OMIT_FOREIGN_KE
acb30 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  Y.SQLITE_PRIVATE
acb40 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
acb50 43 68 65 63 6b 46 6b 28 56 64 62 65 20 2a 2c 20  CheckFk(Vdbe *, 
acb60 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65  int);.#else.# de
acb70 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65  fine sqlite3Vdbe
acb80 43 68 65 63 6b 46 6b 28 70 2c 69 29 20 30 0a 23  CheckFk(p,i) 0.#
acb90 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
acba0 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
acbb0 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50  D_CACHE.SQLITE_P
acbc0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
acbd0 74 65 33 56 64 62 65 4d 75 74 65 78 41 72 72 61  te3VdbeMutexArra
acbe0 79 45 6e 74 65 72 28 56 64 62 65 20 2a 70 29 3b  yEnter(Vdbe *p);
acbf0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
acc00 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78  sqlite3VdbeMutex
acc10 41 72 72 61 79 45 6e 74 65 72 28 70 29 0a 23 65  ArrayEnter(p).#e
acc20 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49  ndif..SQLITE_PRI
acc30 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
acc40 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65  VdbeMemTranslate
acc50 28 4d 65 6d 2a 2c 20 75 38 29 3b 0a 23 69 66 64  (Mem*, u8);.#ifd
acc60 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
acc70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
acc80 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
acc90 65 50 72 69 6e 74 53 71 6c 28 56 64 62 65 2a 29  ePrintSql(Vdbe*)
acca0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
accb0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56     void sqlite3V
accc0 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e  dbeMemPrettyPrin
accd0 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68 61  t(Mem *pMem, cha
acce0 72 20 2a 7a 42 75 66 29 3b 0a 23 65 6e 64 69 66  r *zBuf);.#endif
accf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
acd00 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
acd10 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20  emHandleBom(Mem 
acd20 2a 70 4d 65 6d 29 3b 0a 0a 23 69 66 6e 64 65 66  *pMem);..#ifndef
acd30 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
acd40 52 42 4c 4f 42 0a 53 51 4c 49 54 45 5f 50 52 49  RBLOB.SQLITE_PRI
acd50 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
acd60 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42  e3VdbeMemExpandB
acd70 6c 6f 62 28 4d 65 6d 20 2a 29 3b 0a 23 65 6c 73  lob(Mem *);.#els
acd80 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69  e.  #define sqli
acd90 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64  te3VdbeMemExpand
acda0 42 6c 6f 62 28 78 29 20 53 51 4c 49 54 45 5f 4f  Blob(x) SQLITE_O
acdb0 4b 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66  K.#endif..#endif
acdc0 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 56 44   /* !defined(_VD
acdd0 42 45 49 4e 54 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a  BEINT_H_) */../*
acde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
acdf0 64 20 6f 66 20 76 64 62 65 49 6e 74 2e 68 20 2a  d of vdbeInt.h *
ace00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ace10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ace20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
ace30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
ace40 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
ace50 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 75 74  e left off in ut
ace60 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f.c ************
ace70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23  ************/..#
ace80 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d  ifndef SQLITE_AM
ace90 41 4c 47 41 4d 41 54 49 4f 4e 0a 2f 2a 0a 2a 2a  ALGAMATION./*.**
acea0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
aceb0 6f 6e 73 74 61 6e 74 20 76 61 6c 75 65 20 69 73  onstant value is
acec0 20 75 73 65 64 20 62 79 20 74 68 65 20 53 51 4c   used by the SQL
aced0 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20 61 6e  ITE_BIGENDIAN an
acee0 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4c 49 54 54  d.** SQLITE_LITT
acef0 4c 45 45 4e 44 49 41 4e 20 6d 61 63 72 6f 73 2e  LEENDIAN macros.
acf00 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
acf10 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c  TE const int sql
acf20 69 74 65 33 6f 6e 65 20 3d 20 31 3b 0a 23 65 6e  ite3one = 1;.#en
acf30 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4d  dif /* SQLITE_AM
acf40 41 4c 47 41 4d 41 54 49 4f 4e 20 2a 2f 0a 0a 2f  ALGAMATION */../
acf50 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f 6f 6b 75 70  *.** This lookup
acf60 20 74 61 62 6c 65 20 69 73 20 75 73 65 64 20 74   table is used t
acf70 6f 20 68 65 6c 70 20 64 65 63 6f 64 65 20 74 68  o help decode th
acf80 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 0a  e first byte of.
acf90 2a 2a 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20  ** a multi-byte 
acfa0 55 54 46 38 20 63 68 61 72 61 63 74 65 72 2e 0a  UTF8 character..
acfb0 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
acfc0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71  unsigned char sq
acfd0 6c 69 74 65 33 55 74 66 38 54 72 61 6e 73 31 5b  lite3Utf8Trans1[
acfe0 5d 20 3d 20 7b 0a 20 20 30 78 30 30 2c 20 30 78  ] = {.  0x00, 0x
acff0 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20  01, 0x02, 0x03, 
ad000 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36  0x04, 0x05, 0x06
ad010 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 38 2c 20  , 0x07,.  0x08, 
ad020 30 78 30 39 2c 20 30 78 30 61 2c 20 30 78 30 62  0x09, 0x0a, 0x0b
ad030 2c 20 30 78 30 63 2c 20 30 78 30 64 2c 20 30 78  , 0x0c, 0x0d, 0x
ad040 30 65 2c 20 30 78 30 66 2c 0a 20 20 30 78 31 30  0e, 0x0f,.  0x10
ad050 2c 20 30 78 31 31 2c 20 30 78 31 32 2c 20 30 78  , 0x11, 0x12, 0x
ad060 31 33 2c 20 30 78 31 34 2c 20 30 78 31 35 2c 20  13, 0x14, 0x15, 
ad070 30 78 31 36 2c 20 30 78 31 37 2c 0a 20 20 30 78  0x16, 0x17,.  0x
ad080 31 38 2c 20 30 78 31 39 2c 20 30 78 31 61 2c 20  18, 0x19, 0x1a, 
ad090 30 78 31 62 2c 20 30 78 31 63 2c 20 30 78 31 64  0x1b, 0x1c, 0x1d
ad0a0 2c 20 30 78 31 65 2c 20 30 78 31 66 2c 0a 20 20  , 0x1e, 0x1f,.  
ad0b0 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32  0x00, 0x01, 0x02
ad0c0 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20 30 78  , 0x03, 0x04, 0x
ad0d0 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37 2c 0a  05, 0x06, 0x07,.
ad0e0 20 20 30 78 30 38 2c 20 30 78 30 39 2c 20 30 78    0x08, 0x09, 0x
ad0f0 30 61 2c 20 30 78 30 62 2c 20 30 78 30 63 2c 20  0a, 0x0b, 0x0c, 
ad100 30 78 30 64 2c 20 30 78 30 65 2c 20 30 78 30 66  0x0d, 0x0e, 0x0f
ad110 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20  ,.  0x00, 0x01, 
ad120 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34  0x02, 0x03, 0x04
ad130 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78  , 0x05, 0x06, 0x
ad140 30 37 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31  07,.  0x00, 0x01
ad150 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78  , 0x02, 0x03, 0x
ad160 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c 20  00, 0x01, 0x00, 
ad170 30 78 30 30 2c 0a 7d 3b 0a 0a 0a 23 64 65 66 69  0x00,.};...#defi
ad180 6e 65 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f  ne WRITE_UTF8(zO
ad190 75 74 2c 20 63 29 20 7b 20 20 20 20 20 20 20 20  ut, c) {        
ad1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad1b0 20 20 5c 0a 20 20 69 66 28 20 63 3c 30 78 30 30    \.  if( c<0x00
ad1c0 30 38 30 20 29 7b 20 20 20 20 20 20 20 20 20 20  080 ){          
ad1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad1e0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
ad1f0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28   *zOut++ = (u8)(
ad200 63 26 30 78 46 46 29 3b 20 20 20 20 20 20 20 20  c&0xFF);        
ad210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad220 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20      \.  }       
ad230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
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 5c 0a 20               \. 
ad260 20 65 6c 73 65 20 69 66 28 20 63 3c 30 78 30 30   else if( c<0x00
ad270 38 30 30 20 29 7b 20 20 20 20 20 20 20 20 20 20  800 ){          
ad280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad290 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
ad2a0 74 2b 2b 20 3d 20 30 78 43 30 20 2b 20 28 75 38  t++ = 0xC0 + (u8
ad2b0 29 28 28 63 3e 3e 36 29 26 30 78 31 46 29 3b 20  )((c>>6)&0x1F); 
ad2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ad2d0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30  .    *zOut++ = 0
ad2e0 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 20 30  x80 + (u8)(c & 0
ad2f0 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  x3F);           
ad300 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20          \.  }   
ad310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
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 5c 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c   \.  else if( c<
ad350 30 78 31 30 30 30 30 20 29 7b 20 20 20 20 20 20  0x10000 ){      
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 5c 0a 20 20 20 20            \.    
ad380 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 45 30 20 2b  *zOut++ = 0xE0 +
ad390 20 28 75 38 29 28 28 63 3e 3e 31 32 29 26 30 78   (u8)((c>>12)&0x
ad3a0 30 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  0F);            
ad3b0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
ad3c0 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 28   = 0x80 + (u8)((
ad3d0 63 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 20 20  c>>6) & 0x3F);  
ad3e0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ad3f0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30    *zOut++ = 0x80
ad400 20 2b 20 28 75 38 29 28 63 20 26 20 30 78 33 46   + (u8)(c & 0x3F
ad410 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
ad420 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20       \.  }else{ 
ad430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ad460 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78      *zOut++ = 0x
ad470 46 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 38  F0 + (u8)((c>>18
ad480 29 20 26 20 30 78 30 37 29 3b 20 20 20 20 20 20  ) & 0x07);      
ad490 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
ad4a0 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75  ut++ = 0x80 + (u
ad4b0 38 29 28 28 63 3e 3e 31 32 29 20 26 20 30 78 33  8)((c>>12) & 0x3
ad4c0 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  F);             
ad4d0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
ad4e0 30 78 38 30 20 2b 20 28 75 38 29 28 28 63 3e 3e  0x80 + (u8)((c>>
ad4f0 36 29 20 26 20 30 78 33 46 29 3b 20 20 20 20 20  6) & 0x3F);     
ad500 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
ad510 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20  zOut++ = 0x80 + 
ad520 28 75 38 29 28 63 20 26 20 30 78 33 46 29 3b 20  (u8)(c & 0x3F); 
ad530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad540 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20    \.  }         
ad550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad570 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a             \.}..
ad580 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54  #define WRITE_UT
ad590 46 31 36 4c 45 28 7a 4f 75 74 2c 20 63 29 20 7b  F16LE(zOut, c) {
ad5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad5c0 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30      \.  if( c<=0
ad5d0 78 46 46 46 46 20 29 7b 20 20 20 20 20 20 20 20  xFFFF ){        
ad5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad600 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
ad610 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63  *zOut++ = (u8)(c
ad620 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20  &0x00FF);       
ad630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad650 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
ad660 28 75 38 29 28 28 63 3e 3e 38 29 26 30 78 30 30  (u8)((c>>8)&0x00
ad670 46 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  FF);            
ad680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad690 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b        \.  }else{
ad6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad6d0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ad6e0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29    *zOut++ = (u8)
ad6f0 28 28 28 63 3e 3e 31 30 29 26 30 78 30 30 33 46  (((c>>10)&0x003F
ad700 29 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30  ) + (((c-0x10000
ad710 29 3e 3e 31 30 29 26 30 78 30 30 43 30 29 29 3b  )>>10)&0x00C0));
ad720 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
ad730 3d 20 28 75 38 29 28 30 78 30 30 44 38 20 2b 20  = (u8)(0x00D8 + 
ad740 28 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31  (((c-0x10000)>>1
ad750 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 20 20  8)&0x03));      
ad760 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
ad770 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30  Out++ = (u8)(c&0
ad780 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20  x00FF);         
ad790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ad7b0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75      *zOut++ = (u
ad7c0 38 29 28 30 78 30 30 44 43 20 2b 20 28 28 63 3e  8)(0x00DC + ((c>
ad7d0 3e 38 29 26 30 78 30 33 29 29 3b 20 20 20 20 20  >8)&0x03));     
ad7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad7f0 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20      \.  }       
ad800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad830 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23            \.}..#
ad840 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46  define WRITE_UTF
ad850 31 36 42 45 28 7a 4f 75 74 2c 20 63 29 20 7b 20  16BE(zOut, c) { 
ad860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad880 20 20 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30 78     \.  if( c<=0x
ad890 46 46 46 46 20 29 7b 20 20 20 20 20 20 20 20 20  FFFF ){         
ad8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad8c0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
ad8d0 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 28 63  zOut++ = (u8)((c
ad8e0 3e 3e 38 29 26 30 78 30 30 46 46 29 3b 20 20 20  >>8)&0x00FF);   
ad8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ad910 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
ad920 75 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 20  u8)(c&0x00FF);  
ad930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad950 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20       \.  }else{ 
ad960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad990 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
ad9a0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28   *zOut++ = (u8)(
ad9b0 30 78 30 30 44 38 20 2b 20 28 28 28 63 2d 30 78  0x00D8 + (((c-0x
ad9c0 31 30 30 30 30 29 3e 3e 31 38 29 26 30 78 30 33  10000)>>18)&0x03
ad9d0 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ));             
ad9e0 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
ad9f0 20 28 75 38 29 28 28 28 63 3e 3e 31 30 29 26 30   (u8)(((c>>10)&0
ada00 78 30 30 33 46 29 20 2b 20 28 28 28 63 2d 30 78  x003F) + (((c-0x
ada10 31 30 30 30 30 29 3e 3e 31 30 29 26 30 78 30 30  10000)>>10)&0x00
ada20 43 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f  C0));  \.    *zO
ada30 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 30  ut++ = (u8)(0x00
ada40 44 43 20 2b 20 28 28 63 3e 3e 38 29 26 30 78 30  DC + ((c>>8)&0x0
ada50 33 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  3));            
ada60 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ada70 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38     *zOut++ = (u8
ada80 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20  )(c&0x00FF);    
ada90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adab0 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20     \.  }        
adac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adaf0 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64           \.}..#d
adb00 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46 31 36  efine READ_UTF16
adb10 4c 45 28 7a 49 6e 2c 20 63 29 7b 20 20 20 20 20  LE(zIn, c){     
adb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb40 20 20 20 20 5c 0a 20 20 63 20 3d 20 28 2a 7a 49      \.  c = (*zI
adb50 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20  n++);           
adb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb80 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
adb90 63 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c  c += ((*zIn++)<<
adba0 38 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  8);             
adbb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adbd0 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30      \.  if( c>=0
adbe0 78 44 38 30 30 20 26 26 20 63 3c 30 78 45 30 30  xD800 && c<0xE00
adbf0 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  0 ){            
adc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc10 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
adc20 20 20 69 6e 74 20 63 32 20 3d 20 28 2a 7a 49 6e    int c2 = (*zIn
adc30 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  ++);            
adc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc60 20 20 20 20 5c 0a 20 20 20 20 63 32 20 2b 3d 20      \.    c2 += 
adc70 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20  ((*zIn++)<<8);  
adc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adca0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
adcb0 20 20 63 20 3d 20 28 63 32 26 30 78 30 33 46 46    c = (c2&0x03FF
adcc0 29 20 2b 20 28 28 63 26 30 78 30 30 33 46 29 3c  ) + ((c&0x003F)<
adcd0 3c 31 30 29 20 2b 20 28 28 28 63 26 30 78 30 33  <10) + (((c&0x03
adce0 43 30 29 2b 30 78 30 30 34 30 29 3c 3c 31 30 29  C0)+0x0040)<<10)
adcf0 3b 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20  ;   \.  }       
add00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
add10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
add20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
add30 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a              \.}.
add40 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55 54  .#define READ_UT
add50 46 31 36 42 45 28 7a 49 6e 2c 20 63 29 7b 20 20  F16BE(zIn, c){  
add60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
add70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
add80 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20 28         \.  c = (
add90 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20  (*zIn++)<<8);   
adda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
addb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
addc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
addd0 0a 20 20 63 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29  .  c += (*zIn++)
adde0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
addf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade10 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63         \.  if( c
ade20 3e 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30 78  >=0xD800 && c<0x
ade30 45 30 30 30 20 29 7b 20 20 20 20 20 20 20 20 20  E000 ){         
ade40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ade60 0a 20 20 20 20 69 6e 74 20 63 32 20 3d 20 28 28  .    int c2 = ((
ade70 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20  *zIn++)<<8);    
ade80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adea0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 32 20         \.    c2 
adeb0 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20  += (*zIn++);    
adec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aded0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
adef0 0a 20 20 20 20 63 20 3d 20 28 63 32 26 30 78 30  .    c = (c2&0x0
adf00 33 46 46 29 20 2b 20 28 28 63 26 30 78 30 30 33  3FF) + ((c&0x003
adf10 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 30  F)<<10) + (((c&0
adf20 78 30 33 43 30 29 2b 30 78 30 30 34 30 29 3c 3c  x03C0)+0x0040)<<
adf30 31 30 29 3b 20 20 20 5c 0a 20 20 7d 20 20 20 20  10);   \.  }    
adf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
adf80 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c  .}../*.** Transl
adf90 61 74 65 20 61 20 73 69 6e 67 6c 65 20 55 54 46  ate a single UTF
adfa0 2d 38 20 63 68 61 72 61 63 74 65 72 2e 20 20 52  -8 character.  R
adfb0 65 74 75 72 6e 20 74 68 65 20 75 6e 69 63 6f 64  eturn the unicod
adfc0 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 44  e value..**.** D
adfd0 75 72 69 6e 67 20 74 72 61 6e 73 6c 61 74 69 6f  uring translatio
adfe0 6e 2c 20 61 73 73 75 6d 65 20 74 68 61 74 20 74  n, assume that t
adff0 68 65 20 62 79 74 65 20 74 68 61 74 20 7a 54 65  he byte that zTe
ae000 72 6d 20 70 6f 69 6e 74 73 0a 2a 2a 20 69 73 20  rm points.** is 
ae010 61 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 57 72  a 0x00..**.** Wr
ae020 69 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ite a pointer to
ae030 20 74 68 65 20 6e 65 78 74 20 75 6e 72 65 61 64   the next unread
ae040 20 62 79 74 65 20 62 61 63 6b 20 69 6e 74 6f 20   byte back into 
ae050 2a 70 7a 4e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 4e  *pzNext..**.** N
ae060 6f 74 65 73 20 4f 6e 20 49 6e 76 61 6c 69 64 20  otes On Invalid 
ae070 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20  UTF-8:.**.**  * 
ae080 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65   This routine ne
ae090 76 65 72 20 61 6c 6c 6f 77 73 20 61 20 37 2d 62  ver allows a 7-b
ae0a0 69 74 20 63 68 61 72 61 63 74 65 72 20 28 30 78  it character (0x
ae0b0 30 30 20 74 68 72 6f 75 67 68 20 30 78 37 66 29  00 through 0x7f)
ae0c0 20 74 6f 0a 2a 2a 20 20 20 20 20 62 65 20 65 6e   to.**     be en
ae0d0 63 6f 64 65 64 20 61 73 20 61 20 6d 75 6c 74 69  coded as a multi
ae0e0 2d 62 79 74 65 20 63 68 61 72 61 63 74 65 72 2e  -byte character.
ae0f0 20 20 41 6e 79 20 6d 75 6c 74 69 2d 62 79 74 65    Any multi-byte
ae100 20 63 68 61 72 61 63 74 65 72 20 74 68 61 74 0a   character that.
ae110 2a 2a 20 20 20 20 20 61 74 74 65 6d 70 74 73 20  **     attempts 
ae120 74 6f 20 65 6e 63 6f 64 65 20 61 20 76 61 6c 75  to encode a valu
ae130 65 20 62 65 74 77 65 65 6e 20 30 78 30 30 20 61  e between 0x00 a
ae140 6e 64 20 30 78 37 66 20 69 73 20 72 65 6e 64 65  nd 0x7f is rende
ae150 72 65 64 20 61 73 20 30 78 66 66 66 64 2e 0a 2a  red as 0xfffd..*
ae160 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 6f  *.**  *  This ro
ae170 75 74 69 6e 65 20 6e 65 76 65 72 20 61 6c 6c 6f  utine never allo
ae180 77 73 20 61 20 55 54 46 31 36 20 73 75 72 72 6f  ws a UTF16 surro
ae190 67 61 74 65 20 76 61 6c 75 65 20 74 6f 20 62 65  gate value to be
ae1a0 20 65 6e 63 6f 64 65 64 2e 0a 2a 2a 20 20 20 20   encoded..**    
ae1b0 20 49 66 20 61 20 6d 75 6c 74 69 2d 62 79 74 65   If a multi-byte
ae1c0 20 63 68 61 72 61 63 74 65 72 20 61 74 74 65 6d   character attem
ae1d0 70 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20  pts to encode a 
ae1e0 76 61 6c 75 65 20 62 65 74 77 65 65 6e 0a 2a 2a  value between.**
ae1f0 20 20 20 20 20 30 78 64 38 30 30 20 61 6e 64 20       0xd800 and 
ae200 30 78 65 30 30 30 20 74 68 65 6e 20 69 74 20 69  0xe000 then it i
ae210 73 20 72 65 6e 64 65 72 65 64 20 61 73 20 30 78  s rendered as 0x
ae220 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20  fffd..**.**  *  
ae230 42 79 74 65 73 20 69 6e 20 74 68 65 20 72 61 6e  Bytes in the ran
ae240 67 65 20 6f 66 20 30 78 38 30 20 74 68 72 6f 75  ge of 0x80 throu
ae250 67 68 20 30 78 62 66 20 77 68 69 63 68 20 6f 63  gh 0xbf which oc
ae260 63 75 72 20 61 73 20 74 68 65 20 66 69 72 73 74  cur as the first
ae270 0a 2a 2a 20 20 20 20 20 62 79 74 65 20 6f 66 20  .**     byte of 
ae280 61 20 63 68 61 72 61 63 74 65 72 20 61 72 65 20  a character are 
ae290 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 73  interpreted as s
ae2a0 69 6e 67 6c 65 2d 62 79 74 65 20 63 68 61 72 61  ingle-byte chara
ae2b0 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 61 6e 64  cters.**     and
ae2c0 20 72 65 6e 64 65 72 65 64 20 61 73 20 74 68 65   rendered as the
ae2d0 6d 73 65 6c 76 65 73 20 65 76 65 6e 20 74 68 6f  mselves even tho
ae2e0 75 67 68 20 74 68 65 79 20 61 72 65 20 74 65 63  ugh they are tec
ae2f0 68 6e 69 63 61 6c 6c 79 0a 2a 2a 20 20 20 20 20  hnically.**     
ae300 69 6e 76 61 6c 69 64 20 63 68 61 72 61 63 74 65  invalid characte
ae310 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68  rs..**.**  *  Th
ae320 69 73 20 72 6f 75 74 69 6e 65 20 61 63 63 65 70  is routine accep
ae330 74 73 20 61 6e 20 69 6e 66 69 6e 69 74 65 20 6e  ts an infinite n
ae340 75 6d 62 65 72 20 6f 66 20 64 69 66 66 65 72 65  umber of differe
ae350 6e 74 20 55 54 46 38 20 65 6e 63 6f 64 69 6e 67  nt UTF8 encoding
ae360 73 0a 2a 2a 20 20 20 20 20 66 6f 72 20 75 6e 69  s.**     for uni
ae370 63 6f 64 65 20 76 61 6c 75 65 73 20 30 78 38 30  code values 0x80
ae380 20 61 6e 64 20 67 72 65 61 74 65 72 2e 20 20 49   and greater.  I
ae390 74 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20  t do not change 
ae3a0 6f 76 65 72 2d 6c 65 6e 67 74 68 0a 2a 2a 20 20  over-length.**  
ae3b0 20 20 20 65 6e 63 6f 64 69 6e 67 73 20 74 6f 20     encodings to 
ae3c0 30 78 66 66 66 64 20 61 73 20 73 6f 6d 65 20 73  0xfffd as some s
ae3d0 79 73 74 65 6d 73 20 72 65 63 6f 6d 6d 65 6e 64  ystems recommend
ae3e0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 45 41  ..*/.#define REA
ae3f0 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72  D_UTF8(zIn, zTer
ae400 6d 2c 20 63 29 20 20 20 20 20 20 20 20 20 20 20  m, c)           
ae410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae420 5c 0a 20 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29  \.  c = *(zIn++)
ae430 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ae440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae450 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ae460 20 69 66 28 20 63 3e 3d 30 78 63 30 20 29 7b 20   if( c>=0xc0 ){ 
ae470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae490 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
ae4a0 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 54  c = sqlite3Utf8T
ae4b0 72 61 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 20 20  rans1[c-0xc0];  
ae4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae4d0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 77 68 69         \.    whi
ae4e0 6c 65 28 20 7a 49 6e 21 3d 7a 54 65 72 6d 20 26  le( zIn!=zTerm &
ae4f0 26 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d  & (*zIn & 0xc0)=
ae500 3d 30 78 38 30 20 29 7b 20 20 20 20 20 20 20 20  =0x80 ){        
ae510 20 20 20 20 5c 0a 20 20 20 20 20 20 63 20 3d 20      \.      c = 
ae520 28 63 3c 3c 36 29 20 2b 20 28 30 78 33 66 20 26  (c<<6) + (0x3f &
ae530 20 2a 28 7a 49 6e 2b 2b 29 29 3b 20 20 20 20 20   *(zIn++));     
ae540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae550 20 5c 0a 20 20 20 20 7d 20 20 20 20 20 20 20 20   \.    }        
ae560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ae590 20 20 20 20 69 66 28 20 63 3c 30 78 38 30 20 20      if( c<0x80  
ae5a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae5c0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
ae5d0 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46       || (c&0xFFF
ae5e0 46 46 38 30 30 29 3d 3d 30 78 44 38 30 30 20 20  FF800)==0xD800  
ae5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae600 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20          \.      
ae610 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46 46 46    || (c&0xFFFFFF
ae620 46 45 29 3d 3d 30 78 46 46 46 45 20 29 7b 20 20  FE)==0xFFFE ){  
ae630 63 20 3d 20 30 78 46 46 46 44 3b 20 7d 20 20 20  c = 0xFFFD; }   
ae640 20 20 20 20 20 5c 0a 20 20 7d 0a 53 51 4c 49 54       \.  }.SQLIT
ae650 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
ae660 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 0a 20  lite3Utf8Read(. 
ae670 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
ae680 63 68 61 72 20 2a 7a 49 6e 2c 20 20 20 20 20 20  char *zIn,      
ae690 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f   /* First byte o
ae6a0 66 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65  f UTF-8 characte
ae6b0 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73  r */.  const uns
ae6c0 69 67 6e 65 64 20 63 68 61 72 20 2a 2a 70 7a 4e  igned char **pzN
ae6d0 65 78 74 20 20 20 20 2f 2a 20 57 72 69 74 65 20  ext    /* Write 
ae6e0 66 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20  first byte past 
ae6f0 55 54 46 2d 38 20 63 68 61 72 20 68 65 72 65 20  UTF-8 char here 
ae700 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 0a  */.){.  int c;..
ae710 20 20 2f 2a 20 53 61 6d 65 20 61 73 20 52 45 41    /* Same as REA
ae720 44 5f 55 54 46 38 28 29 20 61 62 6f 76 65 20 62  D_UTF8() above b
ae730 75 74 20 77 69 74 68 6f 75 74 20 74 68 65 20 7a  ut without the z
ae740 54 65 72 6d 20 70 61 72 61 6d 65 74 65 72 2e 0a  Term parameter..
ae750 20 20 2a 2a 20 46 6f 72 20 74 68 69 73 20 72 6f    ** For this ro
ae760 75 74 69 6e 65 2c 20 77 65 20 61 73 73 75 6d 65  utine, we assume
ae770 20 74 68 65 20 55 54 46 38 20 73 74 72 69 6e 67   the UTF8 string
ae780 20 69 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d   is always zero-
ae790 74 65 72 6d 69 6e 61 74 65 64 2e 0a 20 20 2a 2f  terminated..  */
ae7a0 0a 20 20 63 20 3d 20 2a 28 7a 49 6e 2b 2b 29 3b  .  c = *(zIn++);
ae7b0 0a 20 20 69 66 28 20 63 3e 3d 30 78 63 30 20 29  .  if( c>=0xc0 )
ae7c0 7b 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65  {.    c = sqlite
ae7d0 33 55 74 66 38 54 72 61 6e 73 31 5b 63 2d 30 78  3Utf8Trans1[c-0x
ae7e0 63 30 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  c0];.    while( 
ae7f0 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d 3d 30  (*zIn & 0xc0)==0
ae800 78 38 30 20 29 7b 0a 20 20 20 20 20 20 63 20 3d  x80 ){.      c =
ae810 20 28 63 3c 3c 36 29 20 2b 20 28 30 78 33 66 20   (c<<6) + (0x3f 
ae820 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 0a 20 20 20  & *(zIn++));.   
ae830 20 7d 0a 20 20 20 20 69 66 28 20 63 3c 30 78 38   }.    if( c<0x8
ae840 30 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26  0.        || (c&
ae850 30 78 46 46 46 46 46 38 30 30 29 3d 3d 30 78 44  0xFFFFF800)==0xD
ae860 38 30 30 0a 20 20 20 20 20 20 20 20 7c 7c 20 28  800.        || (
ae870 63 26 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30  c&0xFFFFFFFE)==0
ae880 78 46 46 46 45 20 29 7b 20 20 63 20 3d 20 30 78  xFFFE ){  c = 0x
ae890 46 46 46 44 3b 20 7d 0a 20 20 7d 0a 20 20 2a 70  FFFD; }.  }.  *p
ae8a0 7a 4e 65 78 74 20 3d 20 7a 49 6e 3b 0a 20 20 72  zNext = zIn;.  r
ae8b0 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 0a 0a 2f  eturn c;.}...../
ae8c0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 54 52 41 4e  *.** If the TRAN
ae8d0 53 4c 41 54 45 5f 54 52 41 43 45 20 6d 61 63 72  SLATE_TRACE macr
ae8e0 6f 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68  o is defined, th
ae8f0 65 20 76 61 6c 75 65 20 6f 66 20 65 61 63 68 20  e value of each 
ae900 4d 65 6d 20 69 73 0a 2a 2a 20 70 72 69 6e 74 65  Mem is.** printe
ae910 64 20 6f 6e 20 73 74 64 65 72 72 20 6f 6e 20 74  d on stderr on t
ae920 68 65 20 77 61 79 20 69 6e 74 6f 20 61 6e 64 20  he way into and 
ae930 6f 75 74 20 6f 66 20 73 71 6c 69 74 65 33 56 64  out of sqlite3Vd
ae940 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 29  beMemTranslate()
ae950 2e 0a 2a 2f 20 0a 2f 2a 20 23 64 65 66 69 6e 65  ..*/ ./* #define
ae960 20 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45   TRANSLATE_TRACE
ae970 20 31 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53   1 */..#ifndef S
ae980 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
ae990 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
ae9a0 69 6e 65 20 74 72 61 6e 73 66 6f 72 6d 73 20 74  ine transforms t
ae9b0 68 65 20 69 6e 74 65 72 6e 61 6c 20 74 65 78 74  he internal text
ae9c0 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62   encoding used b
ae9d0 79 20 70 4d 65 6d 20 74 6f 0a 2a 2a 20 64 65 73  y pMem to.** des
ae9e0 69 72 65 64 45 6e 63 2e 20 49 74 20 69 73 20 61  iredEnc. It is a
ae9f0 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 73  n error if the s
aea00 74 72 69 6e 67 20 69 73 20 61 6c 72 65 61 64 79  tring is already
aea10 20 6f 66 20 74 68 65 20 64 65 73 69 72 65 64 0a   of the desired.
aea20 2a 2a 20 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 20  ** encoding, or 
aea30 69 66 20 2a 70 4d 65 6d 20 64 6f 65 73 20 6e 6f  if *pMem does no
aea40 74 20 63 6f 6e 74 61 69 6e 20 61 20 73 74 72 69  t contain a stri
aea50 6e 67 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c  ng value..*/.SQL
aea60 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
aea70 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72  sqlite3VdbeMemTr
aea80 61 6e 73 6c 61 74 65 28 4d 65 6d 20 2a 70 4d 65  anslate(Mem *pMe
aea90 6d 2c 20 75 38 20 64 65 73 69 72 65 64 45 6e 63  m, u8 desiredEnc
aeaa0 29 7b 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20  ){.  int len;   
aeab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aeac0 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67   /* Maximum leng
aead0 74 68 20 6f 66 20 6f 75 74 70 75 74 20 73 74 72  th of output str
aeae0 69 6e 67 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  ing in bytes */.
aeaf0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
aeb00 2a 7a 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20  *zOut;          
aeb10 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75          /* Outpu
aeb20 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 75 6e  t buffer */.  un
aeb30 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e  signed char *zIn
aeb40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
aeb50 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 69 74 65      /* Input ite
aeb60 72 61 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67  rator */.  unsig
aeb70 6e 65 64 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b  ned char *zTerm;
aeb80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aeb90 20 2f 2a 20 45 6e 64 20 6f 66 20 69 6e 70 75 74   /* End of input
aeba0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   */.  unsigned c
aebb0 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20  har *z;         
aebc0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
aebd0 75 74 70 75 74 20 69 74 65 72 61 74 6f 72 20 2a  utput iterator *
aebe0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  /.  unsigned int
aebf0 20 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70   c;..  assert( p
aec00 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
aec10 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
aec20 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
aec30 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
aec40 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53  Mem->flags&MEM_S
aec50 74 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  tr );.  assert( 
aec60 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65 73 69 72  pMem->enc!=desir
aec70 65 64 45 6e 63 20 29 3b 0a 20 20 61 73 73 65 72  edEnc );.  asser
aec80 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 30 20  t( pMem->enc!=0 
aec90 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  );.  assert( pMe
aeca0 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 0a 23 69 66 20  m->n>=0 );..#if 
aecb0 64 65 66 69 6e 65 64 28 54 52 41 4e 53 4c 41 54  defined(TRANSLAT
aecc0 45 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 69  E_TRACE) && defi
aecd0 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
aece0 29 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 20 7a  ).  {.    char z
aecf0 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73 71  Buf[100];.    sq
aed00 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74  lite3VdbeMemPret
aed10 74 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a 42  tyPrint(pMem, zB
aed20 75 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66  uf);.    fprintf
aed30 28 73 74 64 65 72 72 2c 20 22 49 4e 50 55 54 3a  (stderr, "INPUT:
aed40 20 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a    %s\n", zBuf);.
aed50 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
aed60 20 49 66 20 74 68 65 20 74 72 61 6e 73 6c 61 74   If the translat
aed70 69 6f 6e 20 69 73 20 62 65 74 77 65 65 6e 20 55  ion is between U
aed80 54 46 2d 31 36 20 6c 69 74 74 6c 65 20 61 6e 64  TF-16 little and
aed90 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 74 68 65   big endian, the
aeda0 6e 20 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61 74  n .  ** all that
aedb0 20 69 73 20 72 65 71 75 69 72 65 64 20 69 73 20   is required is 
aedc0 74 6f 20 73 77 61 70 20 74 68 65 20 62 79 74 65  to swap the byte
aedd0 20 6f 72 64 65 72 2e 20 54 68 69 73 20 63 61 73   order. This cas
aede0 65 20 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 2a  e is handled.  *
aedf0 2a 20 64 69 66 66 65 72 65 6e 74 6c 79 20 66 72  * differently fr
aee00 6f 6d 20 74 68 65 20 6f 74 68 65 72 73 2e 0a 20  om the others.. 
aee10 20 2a 2f 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e   */.  if( pMem->
aee20 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38  enc!=SQLITE_UTF8
aee30 20 26 26 20 64 65 73 69 72 65 64 45 6e 63 21 3d   && desiredEnc!=
aee40 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20  SQLITE_UTF8 ){. 
aee50 20 20 20 75 38 20 74 65 6d 70 3b 0a 20 20 20 20     u8 temp;.    
aee60 69 6e 74 20 72 63 3b 0a 20 20 20 20 72 63 20 3d  int rc;.    rc =
aee70 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
aee80 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 4d 65  akeWriteable(pMe
aee90 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  m);.    if( rc!=
aeea0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
aeeb0 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
aeec0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20  QLITE_NOMEM );. 
aeed0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
aeee0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
aeef0 20 20 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70      zIn = (u8*)p
aef00 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 7a 54 65 72  Mem->z;.    zTer
aef10 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e  m = &zIn[pMem->n
aef20 26 7e 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28  &~1];.    while(
aef30 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20   zIn<zTerm ){.  
aef40 20 20 20 20 74 65 6d 70 20 3d 20 2a 7a 49 6e 3b      temp = *zIn;
aef50 0a 20 20 20 20 20 20 2a 7a 49 6e 20 3d 20 2a 28  .      *zIn = *(
aef60 7a 49 6e 2b 31 29 3b 0a 20 20 20 20 20 20 7a 49  zIn+1);.      zI
aef70 6e 2b 2b 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 2b  n++;.      *zIn+
aef80 2b 20 3d 20 74 65 6d 70 3b 0a 20 20 20 20 7d 0a  + = temp;.    }.
aef90 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20      pMem->enc = 
aefa0 64 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 20 20  desiredEnc;.    
aefb0 67 6f 74 6f 20 74 72 61 6e 73 6c 61 74 65 5f 6f  goto translate_o
aefc0 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65  ut;.  }..  /* Se
aefd0 74 20 6c 65 6e 20 74 6f 20 74 68 65 20 6d 61 78  t len to the max
aefe0 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 62  imum number of b
aeff0 79 74 65 73 20 72 65 71 75 69 72 65 64 20 69 6e  ytes required in
af000 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66   the output buff
af010 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 64 65 73  er. */.  if( des
af020 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f  iredEnc==SQLITE_
af030 55 54 46 38 20 29 7b 0a 20 20 20 20 2f 2a 20 57  UTF8 ){.    /* W
af040 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 20 66  hen converting f
af050 72 6f 6d 20 55 54 46 2d 31 36 2c 20 74 68 65 20  rom UTF-16, the 
af060 6d 61 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 72  maximum growth r
af070 65 73 75 6c 74 73 20 66 72 6f 6d 0a 20 20 20 20  esults from.    
af080 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6e 67 20 61  ** translating a
af090 20 32 2d 62 79 74 65 20 63 68 61 72 61 63 74 65   2-byte characte
af0a0 72 20 74 6f 20 61 20 34 2d 62 79 74 65 20 55 54  r to a 4-byte UT
af0b0 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a 20  F-8 character.. 
af0c0 20 20 20 2a 2a 20 41 20 73 69 6e 67 6c 65 20 62     ** A single b
af0d0 79 74 65 20 69 73 20 72 65 71 75 69 72 65 64 20  yte is required 
af0e0 66 6f 72 20 74 68 65 20 6f 75 74 70 75 74 20 73  for the output s
af0f0 74 72 69 6e 67 0a 20 20 20 20 2a 2a 20 6e 75 6c  tring.    ** nul
af100 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20  -terminator..   
af110 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20   */.    pMem->n 
af120 26 3d 20 7e 31 3b 0a 20 20 20 20 6c 65 6e 20 3d  &= ~1;.    len =
af130 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 31   pMem->n * 2 + 1
af140 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
af150 2a 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 69 6e  * When convertin
af160 67 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20  g from UTF-8 to 
af170 55 54 46 2d 31 36 20 74 68 65 20 6d 61 78 69 6d  UTF-16 the maxim
af180 75 6d 20 67 72 6f 77 74 68 20 69 73 20 63 61 75  um growth is cau
af190 73 65 64 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20  sed.    ** when 
af1a0 61 20 31 2d 62 79 74 65 20 55 54 46 2d 38 20 63  a 1-byte UTF-8 c
af1b0 68 61 72 61 63 74 65 72 20 69 73 20 74 72 61 6e  haracter is tran
af1c0 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 32 2d  slated into a 2-
af1d0 62 79 74 65 20 55 54 46 2d 31 36 0a 20 20 20 20  byte UTF-16.    
af1e0 2a 2a 20 63 68 61 72 61 63 74 65 72 2e 20 54 77  ** character. Tw
af1f0 6f 20 62 79 74 65 73 20 61 72 65 20 72 65 71 75  o bytes are requ
af200 69 72 65 64 20 69 6e 20 74 68 65 20 6f 75 74 70  ired in the outp
af210 75 74 20 62 75 66 66 65 72 20 66 6f 72 20 74 68  ut buffer for th
af220 65 0a 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72  e.    ** nul-ter
af230 6d 69 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a  minator..    */.
af240 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e      len = pMem->
af250 6e 20 2a 20 32 20 2b 20 32 3b 0a 20 20 7d 0a 0a  n * 2 + 2;.  }..
af260 20 20 2f 2a 20 53 65 74 20 7a 49 6e 20 74 6f 20    /* Set zIn to 
af270 70 6f 69 6e 74 20 61 74 20 74 68 65 20 73 74 61  point at the sta
af280 72 74 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20  rt of the input 
af290 62 75 66 66 65 72 20 61 6e 64 20 7a 54 65 72 6d  buffer and zTerm
af2a0 20 74 6f 20 70 6f 69 6e 74 20 31 0a 20 20 2a 2a   to point 1.  **
af2b0 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65   byte past the e
af2c0 6e 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61  nd..  **.  ** Va
af2d0 72 69 61 62 6c 65 20 7a 4f 75 74 20 69 73 20 73  riable zOut is s
af2e0 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74  et to point at t
af2f0 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72  he output buffer
af300 2c 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64  , space obtained
af310 0a 20 20 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74  .  ** from sqlit
af320 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 20 20 2a  e3_malloc()..  *
af330 2f 0a 20 20 7a 49 6e 20 3d 20 28 75 38 2a 29 70  /.  zIn = (u8*)p
af340 4d 65 6d 2d 3e 7a 3b 0a 20 20 7a 54 65 72 6d 20  Mem->z;.  zTerm 
af350 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d 3e 6e 5d 3b  = &zIn[pMem->n];
af360 0a 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65  .  zOut = sqlite
af370 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65  3DbMallocRaw(pMe
af380 6d 2d 3e 64 62 2c 20 6c 65 6e 29 3b 0a 20 20 69  m->db, len);.  i
af390 66 28 20 21 7a 4f 75 74 20 29 7b 0a 20 20 20 20  f( !zOut ){.    
af3a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
af3b0 4d 45 4d 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 7a  MEM;.  }.  z = z
af3c0 4f 75 74 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d  Out;..  if( pMem
af3d0 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  ->enc==SQLITE_UT
af3e0 46 38 20 29 7b 0a 20 20 20 20 69 66 28 20 64 65  F8 ){.    if( de
af3f0 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45  siredEnc==SQLITE
af400 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 20  _UTF16LE ){.    
af410 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54    /* UTF-8 -> UT
af420 46 2d 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69  F-16 Little-endi
af430 61 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c  an */.      whil
af440 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a  e( zIn<zTerm ){.
af450 20 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 73          /* c = s
af460 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
af470 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73  In, zTerm, (cons
af480 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f  t u8**)&zIn); */
af490 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54  .        READ_UT
af4a0 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63  F8(zIn, zTerm, c
af4b0 29 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54 45  );.        WRITE
af4c0 5f 55 54 46 31 36 4c 45 28 7a 2c 20 63 29 3b 0a  _UTF16LE(z, c);.
af4d0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
af4e0 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e{.      assert(
af4f0 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c   desiredEnc==SQL
af500 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20  ITE_UTF16BE );. 
af510 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d 3e       /* UTF-8 ->
af520 20 55 54 46 2d 31 36 20 42 69 67 2d 65 6e 64 69   UTF-16 Big-endi
af530 61 6e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c  an */.      whil
af540 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a  e( zIn<zTerm ){.
af550 20 20 20 20 20 20 20 20 2f 2a 20 63 20 3d 20 73          /* c = s
af560 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
af570 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73  In, zTerm, (cons
af580 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 20 2a 2f  t u8**)&zIn); */
af590 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54  .        READ_UT
af5a0 46 38 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63  F8(zIn, zTerm, c
af5b0 29 3b 0a 20 20 20 20 20 20 20 20 57 52 49 54 45  );.        WRITE
af5c0 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a  _UTF16BE(z, c);.
af5d0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
af5e0 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 28 69 6e 74    pMem->n = (int
af5f0 29 28 7a 20 2d 20 7a 4f 75 74 29 3b 0a 20 20 20  )(z - zOut);.   
af600 20 2a 7a 2b 2b 20 3d 20 30 3b 0a 20 20 7d 65 6c   *z++ = 0;.  }el
af610 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
af620 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49  desiredEnc==SQLI
af630 54 45 5f 55 54 46 38 20 29 3b 0a 20 20 20 20 69  TE_UTF8 );.    i
af640 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51  f( pMem->enc==SQ
af650 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a  LITE_UTF16LE ){.
af660 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20        /* UTF-16 
af670 4c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2d 3e  Little-endian ->
af680 20 55 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20   UTF-8 */.      
af690 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d  while( zIn<zTerm
af6a0 20 29 7b 0a 20 20 20 20 20 20 20 20 52 45 41 44   ){.        READ
af6b0 5f 55 54 46 31 36 4c 45 28 7a 49 6e 2c 20 63 29  _UTF16LE(zIn, c)
af6c0 3b 20 0a 20 20 20 20 20 20 20 20 57 52 49 54 45  ; .        WRITE
af6d0 5f 55 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20  _UTF8(z, c);.   
af6e0 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
af6f0 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20        /* UTF-16 
af700 42 69 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54  Big-endian -> UT
af710 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69  F-8 */.      whi
af720 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b  le( zIn<zTerm ){
af730 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54  .        READ_UT
af740 46 31 36 42 45 28 7a 49 6e 2c 20 63 29 3b 20 0a  F16BE(zIn, c); .
af750 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54          WRITE_UT
af760 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20  F8(z, c);.      
af770 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d  }.    }.    pMem
af780 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d 20  ->n = (int)(z - 
af790 7a 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 2a 7a 20  zOut);.  }.  *z 
af7a0 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 28  = 0;.  assert( (
af7b0 70 4d 65 6d 2d 3e 6e 2b 28 64 65 73 69 72 65 64  pMem->n+(desired
af7c0 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38  Enc==SQLITE_UTF8
af7d0 3f 31 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a 0a  ?1:2))<=len );..
af7e0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
af7f0 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20  Release(pMem);. 
af800 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20   pMem->flags &= 
af810 7e 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d  ~(MEM_Static|MEM
af820 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b  _Dyn|MEM_Ephem);
af830 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64  .  pMem->enc = d
af840 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 70 4d 65  esiredEnc;.  pMe
af850 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d 45 4d  m->flags |= (MEM
af860 5f 54 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b 0a  _Term|MEM_Dyn);.
af870 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61    pMem->z = (cha
af880 72 2a 29 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d 2d  r*)zOut;.  pMem-
af890 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d  >zMalloc = pMem-
af8a0 3e 7a 3b 0a 0a 74 72 61 6e 73 6c 61 74 65 5f 6f  >z;..translate_o
af8b0 75 74 3a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  ut:.#if defined(
af8c0 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 29  TRANSLATE_TRACE)
af8d0 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49   && defined(SQLI
af8e0 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20 20  TE_DEBUG).  {.  
af8f0 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d    char zBuf[100]
af900 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
af910 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28  eMemPrettyPrint(
af920 70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 20  pMem, zBuf);.   
af930 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
af940 20 22 4f 55 54 50 55 54 3a 20 25 73 5c 6e 22 2c   "OUTPUT: %s\n",
af950 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e 64   zBuf);.  }.#end
af960 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  if.  return SQLI
af970 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
af980 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65  This routine che
af990 63 6b 73 20 66 6f 72 20 61 20 62 79 74 65 2d 6f  cks for a byte-o
af9a0 72 64 65 72 20 6d 61 72 6b 20 61 74 20 74 68 65  rder mark at the
af9b0 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
af9c0 65 20 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72  e .** UTF-16 str
af9d0 69 6e 67 20 73 74 6f 72 65 64 20 69 6e 20 2a 70  ing stored in *p
af9e0 4d 65 6d 2e 20 49 66 20 6f 6e 65 20 69 73 20 70  Mem. If one is p
af9f0 72 65 73 65 6e 74 2c 20 69 74 20 69 73 20 72 65  resent, it is re
afa00 6d 6f 76 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65  moved and.** the
afa10 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65   encoding of the
afa20 20 4d 65 6d 20 61 64 6a 75 73 74 65 64 2e 20 54   Mem adjusted. T
afa30 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73  his routine does
afa40 20 6e 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a 20 62   not do any.** b
afa50 79 74 65 2d 73 77 61 70 70 69 6e 67 2c 20 69 74  yte-swapping, it
afa60 20 6a 75 73 74 20 73 65 74 73 20 4d 65 6d 2e 65   just sets Mem.e
afa70 6e 63 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79  nc appropriately
afa80 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f  ..**.** The allo
afa90 63 61 74 69 6f 6e 20 28 73 74 61 74 69 63 2c 20  cation (static, 
afaa0 64 79 6e 61 6d 69 63 20 65 74 63 2e 29 20 61 6e  dynamic etc.) an
afab0 64 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68  d encoding of th
afac0 65 20 4d 65 6d 20 6d 61 79 20 62 65 0a 2a 2a 20  e Mem may be.** 
afad0 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20  changed by this 
afae0 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  function..*/.SQL
afaf0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
afb00 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 61  sqlite3VdbeMemHa
afb10 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65  ndleBom(Mem *pMe
afb20 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  m){.  int rc = S
afb30 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 62  QLITE_OK;.  u8 b
afb40 6f 6d 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72  om = 0;..  asser
afb50 74 28 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b  t( pMem->n>=0 );
afb60 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 6e 3e 31  .  if( pMem->n>1
afb70 20 29 7b 0a 20 20 20 20 75 38 20 62 31 20 3d 20   ){.    u8 b1 = 
afb80 2a 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a  *(u8 *)pMem->z;.
afb90 20 20 20 20 75 38 20 62 32 20 3d 20 2a 28 28 28      u8 b2 = *(((
afba0 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 29 20 2b 20  u8 *)pMem->z) + 
afbb0 31 29 3b 0a 20 20 20 20 69 66 28 20 62 31 3d 3d  1);.    if( b1==
afbc0 30 78 46 45 20 26 26 20 62 32 3d 3d 30 78 46 46  0xFE && b2==0xFF
afbd0 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20   ){.      bom = 
afbe0 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b 0a  SQLITE_UTF16BE;.
afbf0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 62 31      }.    if( b1
afc00 3d 3d 30 78 46 46 20 26 26 20 62 32 3d 3d 30 78  ==0xFF && b2==0x
afc10 46 45 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20  FE ){.      bom 
afc20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  = SQLITE_UTF16LE
afc30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20  ;.    }.  }.  . 
afc40 20 69 66 28 20 62 6f 6d 20 29 7b 0a 20 20 20 20   if( bom ){.    
afc50 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
afc60 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65  MemMakeWriteable
afc70 28 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20  (pMem);.    if( 
afc80 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
afc90 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 2d  .      pMem->n -
afca0 3d 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 6d 6f  = 2;.      memmo
afcb0 76 65 28 70 4d 65 6d 2d 3e 7a 2c 20 26 70 4d 65  ve(pMem->z, &pMe
afcc0 6d 2d 3e 7a 5b 32 5d 2c 20 70 4d 65 6d 2d 3e 6e  m->z[2], pMem->n
afcd0 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a  );.      pMem->z
afce0 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 27 5c 30 27  [pMem->n] = '\0'
afcf0 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b  ;.      pMem->z[
afd00 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 27 5c 30  pMem->n+1] = '\0
afd10 27 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  ';.      pMem->f
afd20 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d  lags |= MEM_Term
afd30 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e  ;.      pMem->en
afd40 63 20 3d 20 62 6f 6d 3b 0a 20 20 20 20 7d 0a 20  c = bom;.    }. 
afd50 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
afd60 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
afd70 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
afd80 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61 20  ../*.** pZ is a 
afd90 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 75 6e  UTF-8 encoded un
afda0 69 63 6f 64 65 20 73 74 72 69 6e 67 2e 20 49 66  icode string. If
afdb0 20 6e 42 79 74 65 20 69 73 20 6c 65 73 73 20 74   nByte is less t
afdc0 68 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74  han zero,.** ret
afdd0 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
afde0 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63  f unicode charac
afdf0 74 65 72 73 20 69 6e 20 70 5a 20 75 70 20 74 6f  ters in pZ up to
afe00 20 28 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64   (but not includ
afe10 69 6e 67 29 0a 2a 2a 20 74 68 65 20 66 69 72 73  ing).** the firs
afe20 74 20 30 78 30 30 20 62 79 74 65 2e 20 49 66 20  t 0x00 byte. If 
afe30 6e 42 79 74 65 20 69 73 20 6e 6f 74 20 6c 65 73  nByte is not les
afe40 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 72 65 74  s than zero, ret
afe50 75 72 6e 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65  urn the.** numbe
afe60 72 20 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 61  r of unicode cha
afe70 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 66  racters in the f
afe80 69 72 73 74 20 6e 42 79 74 65 20 6f 66 20 70 5a  irst nByte of pZ
afe90 20 28 6f 72 20 75 70 20 74 6f 20 0a 2a 2a 20 74   (or up to .** t
afea0 68 65 20 66 69 72 73 74 20 30 78 30 30 2c 20 77  he first 0x00, w
afeb0 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66  hichever comes f
afec0 69 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  irst)..*/.SQLITE
afed0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
afee0 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28  ite3Utf8CharLen(
afef0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c  const char *zIn,
aff00 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 69   int nByte){.  i
aff10 6e 74 20 72 20 3d 20 30 3b 0a 20 20 63 6f 6e 73  nt r = 0;.  cons
aff20 74 20 75 38 20 2a 7a 20 3d 20 28 63 6f 6e 73 74  t u8 *z = (const
aff30 20 75 38 2a 29 7a 49 6e 3b 0a 20 20 63 6f 6e 73   u8*)zIn;.  cons
aff40 74 20 75 38 20 2a 7a 54 65 72 6d 3b 0a 20 20 69  t u8 *zTerm;.  i
aff50 66 28 20 6e 42 79 74 65 3e 3d 30 20 29 7b 0a 20  f( nByte>=0 ){. 
aff60 20 20 20 7a 54 65 72 6d 20 3d 20 26 7a 5b 6e 42     zTerm = &z[nB
aff70 79 74 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  yte];.  }else{. 
aff80 20 20 20 7a 54 65 72 6d 20 3d 20 28 63 6f 6e 73     zTerm = (cons
aff90 74 20 75 38 2a 29 28 2d 31 29 3b 0a 20 20 7d 0a  t u8*)(-1);.  }.
affa0 20 20 61 73 73 65 72 74 28 20 7a 3c 3d 7a 54 65    assert( z<=zTe
affb0 72 6d 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 2a  rm );.  while( *
affc0 7a 21 3d 30 20 26 26 20 7a 3c 7a 54 65 72 6d 20  z!=0 && z<zTerm 
affd0 29 7b 0a 20 20 20 20 53 51 4c 49 54 45 5f 53 4b  ){.    SQLITE_SK
affe0 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20  IP_UTF8(z);.    
afff0 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  r++;.  }.  retur
b0000 6e 20 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20  n r;.}../* This 
b0010 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 69 73  test function is
b0020 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 75   not currently u
b0030 73 65 64 20 62 79 20 74 68 65 20 61 75 74 6f 6d  sed by the autom
b0040 61 74 65 64 20 74 65 73 74 2d 73 75 69 74 65 2e  ated test-suite.
b0050 20 0a 2a 2a 20 48 65 6e 63 65 20 69 74 20 69 73   .** Hence it is
b0060 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
b0070 69 6e 20 64 65 62 75 67 20 62 75 69 6c 64 73 2e  in debug builds.
b0080 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
b0090 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20  SQLITE_TEST) && 
b00a0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
b00b0 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e  EBUG)./*.** Tran
b00c0 73 6c 61 74 65 20 55 54 46 2d 38 20 74 6f 20 55  slate UTF-8 to U
b00d0 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  TF-8..**.** This
b00e0 20 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20   has the effect 
b00f0 6f 66 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 74  of making sure t
b0100 68 61 74 20 74 68 65 20 73 74 72 69 6e 67 20 69  hat the string i
b0110 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a  s well-formed.**
b0120 20 55 54 46 2d 38 2e 20 20 4d 69 73 63 6f 64 65   UTF-8.  Miscode
b0130 64 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65  d characters are
b0140 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20   removed..**.** 
b0150 54 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20  The translation 
b0160 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65  is done in-place
b0170 20 28 73 69 6e 63 65 20 69 74 20 69 73 20 69 6d   (since it is im
b0180 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65  possible for the
b0190 0a 2a 2a 20 63 6f 72 72 65 63 74 20 55 54 46 2d  .** correct UTF-
b01a0 38 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65  8 encoding to be
b01b0 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 61 20 6d   longer than a m
b01c0 61 6c 66 6f 72 6d 65 64 20 65 6e 63 6f 64 69 6e  alformed encodin
b01d0 67 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  g)..*/.SQLITE_PR
b01e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b01f0 33 55 74 66 38 54 6f 38 28 75 6e 73 69 67 6e 65  3Utf8To8(unsigne
b0200 64 20 63 68 61 72 20 2a 7a 49 6e 29 7b 0a 20 20  d char *zIn){.  
b0210 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
b0220 4f 75 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e 73  Out = zIn;.  uns
b0230 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 61  igned char *zSta
b0240 72 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 33 32 20  rt = zIn;.  u32 
b0250 63 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e  c;..  while( zIn
b0260 5b 30 5d 20 29 7b 0a 20 20 20 20 63 20 3d 20 73  [0] ){.    c = s
b0270 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
b0280 49 6e 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29  In, (const u8**)
b0290 26 7a 49 6e 29 3b 0a 20 20 20 20 69 66 28 20 63  &zIn);.    if( c
b02a0 21 3d 30 78 66 66 66 64 20 29 7b 0a 20 20 20 20  !=0xfffd ){.    
b02b0 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f 75    WRITE_UTF8(zOu
b02c0 74 2c 20 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  t, c);.    }.  }
b02d0 0a 20 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 20 20  .  *zOut = 0;.  
b02e0 72 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 4f 75  return (int)(zOu
b02f0 74 20 2d 20 7a 53 74 61 72 74 29 3b 0a 7d 0a 23  t - zStart);.}.#
b0300 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
b0310 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
b0320 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61  ./*.** Convert a
b0330 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 69   UTF-16 string i
b0340 6e 20 74 68 65 20 6e 61 74 69 76 65 20 65 6e 63  n the native enc
b0350 6f 64 69 6e 67 20 69 6e 74 6f 20 61 20 55 54 46  oding into a UTF
b0360 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 4d 65  -8 string..** Me
b0370 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65  mory to hold the
b0380 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69 73   UTF-8 string is
b0390 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
b03a0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 61 6e  qlite3_malloc an
b03b0 64 20 6d 75 73 74 0a 2a 2a 20 62 65 20 66 72 65  d must.** be fre
b03c0 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e  ed by the callin
b03d0 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  g function..**.*
b03e0 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e  * NULL is return
b03f0 65 64 20 69 66 20 74 68 65 72 65 20 69 73 20 61  ed if there is a
b0400 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  n allocation err
b0410 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  or..*/.SQLITE_PR
b0420 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69  IVATE char *sqli
b0430 74 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69  te3Utf16to8(sqli
b0440 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 76  te3 *db, const v
b0450 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 42 79 74  oid *z, int nByt
b0460 65 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d  e){.  Mem m;.  m
b0470 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a  emset(&m, 0, siz
b0480 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20  eof(m));.  m.db 
b0490 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56  = db;.  sqlite3V
b04a0 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c  dbeMemSetStr(&m,
b04b0 20 7a 2c 20 6e 42 79 74 65 2c 20 53 51 4c 49 54   z, nByte, SQLIT
b04c0 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53  E_UTF16NATIVE, S
b04d0 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
b04e0 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
b04f0 67 65 45 6e 63 6f 64 69 6e 67 28 26 6d 2c 20 53  geEncoding(&m, S
b0500 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69  QLITE_UTF8);.  i
b0510 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
b0520 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  led ){.    sqlit
b0530 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
b0540 28 26 6d 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d 20  (&m);.    m.z = 
b0550 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  0;.  }.  assert(
b0560 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f   (m.flags & MEM_
b0570 54 65 72 6d 29 21 3d 30 20 7c 7c 20 64 62 2d 3e  Term)!=0 || db->
b0580 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
b0590 20 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c 61    assert( (m.fla
b05a0 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 21 3d 30  gs & MEM_Str)!=0
b05b0 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
b05c0 69 6c 65 64 20 29 3b 0a 20 20 72 65 74 75 72 6e  iled );.  return
b05d0 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f   (m.flags & MEM_
b05e0 44 79 6e 29 21 3d 30 20 3f 20 6d 2e 7a 20 3a 20  Dyn)!=0 ? m.z : 
b05f0 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
b0600 64 62 2c 20 6d 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a  db, m.z);.}../*.
b0610 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46  ** Convert a UTF
b0620 2d 38 20 73 74 72 69 6e 67 20 74 6f 20 74 68 65  -8 string to the
b0630 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67   UTF-16 encoding
b0640 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61   specified by pa
b0650 72 61 6d 65 74 65 72 0a 2a 2a 20 65 6e 63 2e 20  rameter.** enc. 
b0660 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
b0670 20 6e 65 77 20 73 74 72 69 6e 67 20 69 73 20 72   new string is r
b0680 65 74 75 72 6e 65 64 2c 20 61 6e 64 20 74 68 65  eturned, and the
b0690 20 76 61 6c 75 65 20 6f 66 20 2a 70 6e 4f 75 74   value of *pnOut
b06a0 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68  .** is set to th
b06b0 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
b06c0 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20  returned string 
b06d0 69 6e 20 62 79 74 65 73 2e 20 54 68 65 20 63 61  in bytes. The ca
b06e0 6c 6c 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 72 72  ll should.** arr
b06f0 61 6e 67 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c  ange to call sql
b0700 69 74 65 33 44 62 46 72 65 65 28 29 20 6f 6e 20  ite3DbFree() on 
b0710 74 68 65 20 72 65 74 75 72 6e 65 64 20 70 6f 69  the returned poi
b0720 6e 74 65 72 20 77 68 65 6e 20 69 74 20 69 73 0a  nter when it is.
b0730 2a 2a 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 71  ** no longer req
b0740 75 69 72 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 49 66  uired..** .** If
b0750 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72   a malloc failur
b0760 65 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69  e occurs, NULL i
b0770 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74  s returned and t
b0780 68 65 20 64 62 2e 6d 61 6c 6c 6f 63 46 61 69 6c  he db.mallocFail
b0790 65 64 0a 2a 2a 20 66 6c 61 67 20 73 65 74 2e 0a  ed.** flag set..
b07a0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
b07b0 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 53 51  _ENABLE_STAT2.SQ
b07c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
b07d0 72 20 2a 73 71 6c 69 74 65 33 55 74 66 38 74 6f  r *sqlite3Utf8to
b07e0 31 36 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  16(sqlite3 *db, 
b07f0 75 38 20 65 6e 63 2c 20 63 68 61 72 20 2a 7a 2c  u8 enc, char *z,
b0800 20 69 6e 74 20 6e 2c 20 69 6e 74 20 2a 70 6e 4f   int n, int *pnO
b0810 75 74 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20  ut){.  Mem m;.  
b0820 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69  memset(&m, 0, si
b0830 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62  zeof(m));.  m.db
b0840 20 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33   = db;.  sqlite3
b0850 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d  VdbeMemSetStr(&m
b0860 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55  , z, n, SQLITE_U
b0870 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  TF8, SQLITE_STAT
b0880 49 43 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  IC);.  if( sqlit
b0890 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61  e3VdbeMemTransla
b08a0 74 65 28 26 6d 2c 20 65 6e 63 29 20 29 7b 0a 20  te(&m, enc) ){. 
b08b0 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d     assert( db->m
b08c0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
b08d0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
b08e0 0a 20 20 61 73 73 65 72 74 28 20 6d 2e 7a 3d 3d  .  assert( m.z==
b08f0 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 2a  m.zMalloc );.  *
b0900 70 6e 4f 75 74 20 3d 20 6d 2e 6e 3b 0a 20 20 72  pnOut = m.n;.  r
b0910 65 74 75 72 6e 20 6d 2e 7a 3b 0a 7d 0a 23 65 6e  eturn m.z;.}.#en
b0920 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73  dif../*.** pZ is
b0930 20 61 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65   a UTF-16 encode
b0940 64 20 75 6e 69 63 6f 64 65 20 73 74 72 69 6e 67  d unicode string
b0950 20 61 74 20 6c 65 61 73 74 20 6e 43 68 61 72 20   at least nChar 
b0960 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e  characters long.
b0970 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
b0980 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
b0990 6e 20 74 68 65 20 66 69 72 73 74 20 6e 43 68 61  n the first nCha
b09a0 72 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63  r unicode charac
b09b0 74 65 72 73 0a 2a 2a 20 69 6e 20 70 5a 2e 20 20  ters.** in pZ.  
b09c0 6e 43 68 61 72 20 6d 75 73 74 20 62 65 20 6e 6f  nChar must be no
b09d0 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a 2f 0a 53  n-negative..*/.S
b09e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
b09f0 74 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79  t sqlite3Utf16By
b0a00 74 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64  teLen(const void
b0a10 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 43 68 61 72   *zIn, int nChar
b0a20 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 20 20 75 6e  ){.  int c;.  un
b0a30 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73  signed char cons
b0a40 74 20 2a 7a 20 3d 20 7a 49 6e 3b 0a 20 20 69 6e  t *z = zIn;.  in
b0a50 74 20 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 53  t n = 0;.  if( S
b0a60 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
b0a70 45 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42  E==SQLITE_UTF16B
b0a80 45 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 69 6e  E ){.    /* Usin
b0a90 67 20 61 6e 20 22 69 66 20 28 53 51 4c 49 54 45  g an "if (SQLITE
b0aa0 5f 55 54 46 31 36 4e 41 54 49 56 45 3d 3d 53 51  _UTF16NATIVE==SQ
b0ab0 4c 49 54 45 5f 55 54 46 31 36 42 45 29 22 20 63  LITE_UTF16BE)" c
b0ac0 6f 6e 73 74 72 75 63 74 20 68 65 72 65 0a 20 20  onstruct here.  
b0ad0 20 20 2a 2a 20 61 6e 64 20 69 6e 20 6f 74 68 65    ** and in othe
b0ae0 72 20 70 61 72 74 73 20 6f 66 20 74 68 69 73 20  r parts of this 
b0af0 66 69 6c 65 20 6d 65 61 6e 73 20 74 68 61 74 20  file means that 
b0b00 61 74 20 6f 6e 65 20 62 72 61 6e 63 68 20 77 69  at one branch wi
b0b10 6c 6c 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 62 65  ll.    ** not be
b0b20 20 63 6f 76 65 72 65 64 20 62 79 20 63 6f 76 65   covered by cove
b0b30 72 61 67 65 20 74 65 73 74 69 6e 67 20 6f 6e 20  rage testing on 
b0b40 61 6e 79 20 73 69 6e 67 6c 65 20 68 6f 73 74 2e  any single host.
b0b50 20 42 75 74 20 63 6f 76 65 72 61 67 65 0a 20 20   But coverage.  
b0b60 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63 6f 6d    ** will be com
b0b70 70 6c 65 74 65 20 69 66 20 74 68 65 20 74 65 73  plete if the tes
b0b80 74 73 20 61 72 65 20 72 75 6e 20 6f 6e 20 62 6f  ts are run on bo
b0b90 74 68 20 61 20 6c 69 74 74 6c 65 2d 65 6e 64 69  th a little-endi
b0ba0 61 6e 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 62  an and .    ** b
b0bb0 69 67 2d 65 6e 64 69 61 6e 20 68 6f 73 74 2e 20  ig-endian host. 
b0bc0 42 65 63 61 75 73 65 20 62 6f 74 68 20 74 68 65  Because both the
b0bd0 20 55 54 46 31 36 4e 41 54 49 56 45 20 61 6e 64   UTF16NATIVE and
b0be0 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 0a   SQLITE_UTF16BE.
b0bf0 20 20 20 20 2a 2a 20 6d 61 63 72 6f 73 20 61 72      ** macros ar
b0c00 65 20 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f  e constant at co
b0c10 6d 70 69 6c 65 20 74 69 6d 65 20 74 68 65 20 63  mpile time the c
b0c20 6f 6d 70 69 6c 65 72 20 63 61 6e 20 64 65 74 65  ompiler can dete
b0c30 72 6d 69 6e 65 0a 20 20 20 20 2a 2a 20 77 68 69  rmine.    ** whi
b0c40 63 68 20 62 72 61 6e 63 68 20 77 69 6c 6c 20 62  ch branch will b
b0c50 65 20 66 6f 6c 6c 6f 77 65 64 2e 20 49 74 20 69  e followed. It i
b0c60 73 20 74 68 65 72 65 66 6f 72 65 20 61 73 73 75  s therefore assu
b0c70 6d 65 64 20 74 68 61 74 20 6e 6f 20 72 75 6e 74  med that no runt
b0c80 69 6d 65 0a 20 20 20 20 2a 2a 20 70 65 6e 61 6c  ime.    ** penal
b0c90 74 79 20 69 73 20 70 61 69 64 20 66 6f 72 20 74  ty is paid for t
b0ca0 68 69 73 20 22 69 66 22 20 73 74 61 74 65 6d 65  his "if" stateme
b0cb0 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77  nt..    */.    w
b0cc0 68 69 6c 65 28 20 6e 3c 6e 43 68 61 72 20 29 7b  hile( n<nChar ){
b0cd0 0a 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31  .      READ_UTF1
b0ce0 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20  6BE(z, c);.     
b0cf0 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65   n++;.    }.  }e
b0d00 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c 65 28 20  lse{.    while( 
b0d10 6e 3c 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20  n<nChar ){.     
b0d20 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 2c   READ_UTF16LE(z,
b0d30 20 63 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a   c);.      n++;.
b0d40 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
b0d50 72 6e 20 28 69 6e 74 29 28 7a 2d 28 75 6e 73 69  rn (int)(z-(unsi
b0d60 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20  gned char const 
b0d70 2a 29 7a 49 6e 29 3b 0a 7d 0a 0a 23 69 66 20 64  *)zIn);.}..#if d
b0d80 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45  efined(SQLITE_TE
b0d90 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  ST)./*.** This r
b0da0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
b0db0 20 66 72 6f 6d 20 74 68 65 20 54 43 4c 20 74 65   from the TCL te
b0dc0 73 74 20 66 75 6e 63 74 69 6f 6e 20 22 74 72 61  st function "tra
b0dd0 6e 73 6c 61 74 65 5f 73 65 6c 66 74 65 73 74 22  nslate_selftest"
b0de0 2e 0a 2a 2a 20 49 74 20 63 68 65 63 6b 73 20 74  ..** It checks t
b0df0 68 61 74 20 74 68 65 20 70 72 69 6d 69 74 69 76  hat the primitiv
b0e00 65 73 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69  es for serializi
b0e10 6e 67 20 61 6e 64 20 64 65 73 65 72 69 61 6c 69  ng and deseriali
b0e20 7a 69 6e 67 0a 2a 2a 20 63 68 61 72 61 63 74 65  zing.** characte
b0e30 72 73 20 69 6e 20 65 61 63 68 20 65 6e 63 6f 64  rs in each encod
b0e40 69 6e 67 20 61 72 65 20 69 6e 76 65 72 73 65 73  ing are inverses
b0e50 20 6f 66 20 65 61 63 68 20 6f 74 68 65 72 2e 0a   of each other..
b0e60 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b0e70 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 74  E void sqlite3Ut
b0e80 66 53 65 6c 66 54 65 73 74 28 76 6f 69 64 29 7b  fSelfTest(void){
b0e90 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
b0ea0 69 2c 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64  i, t;.  unsigned
b0eb0 20 63 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a   char zBuf[20];.
b0ec0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
b0ed0 2a 7a 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75  *z;.  int n;.  u
b0ee0 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a  nsigned int c;..
b0ef0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30    for(i=0; i<0x0
b0f00 30 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20  0110000; i++){. 
b0f10 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20     z = zBuf;.   
b0f20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 69   WRITE_UTF8(z, i
b0f30 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29  );.    n = (int)
b0f40 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73  (z-zBuf);.    as
b0f50 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d  sert( n>0 && n<=
b0f60 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20  4 );.    z[0] = 
b0f70 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b  0;.    z = zBuf;
b0f80 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33  .    c = sqlite3
b0f90 55 74 66 38 52 65 61 64 28 7a 2c 20 28 63 6f 6e  Utf8Read(z, (con
b0fa0 73 74 20 75 38 2a 2a 29 26 7a 29 3b 0a 20 20 20  st u8**)&z);.   
b0fb0 20 74 20 3d 20 69 3b 0a 20 20 20 20 69 66 28 20   t = i;.    if( 
b0fc0 69 3e 3d 30 78 44 38 30 30 20 26 26 20 69 3c 3d  i>=0xD800 && i<=
b0fd0 30 78 44 46 46 46 20 29 20 74 20 3d 20 30 78 46  0xDFFF ) t = 0xF
b0fe0 46 46 44 3b 0a 20 20 20 20 69 66 28 20 28 69 26  FFD;.    if( (i&
b0ff0 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46  0xFFFFFFFE)==0xF
b1000 46 46 45 20 29 20 74 20 3d 20 30 78 46 46 46 44  FFE ) t = 0xFFFD
b1010 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 3d  ;.    assert( c=
b1020 3d 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =t );.    assert
b1030 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b  ( (z-zBuf)==n );
b1040 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
b1050 69 3c 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b  i<0x00110000; i+
b1060 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30  +){.    if( i>=0
b1070 78 44 38 30 30 20 26 26 20 69 3c 30 78 45 30 30  xD800 && i<0xE00
b1080 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
b1090 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20    z = zBuf;.    
b10a0 57 52 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c  WRITE_UTF16LE(z,
b10b0 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e   i);.    n = (in
b10c0 74 29 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20  t)(z-zBuf);.    
b10d0 61 73 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e  assert( n>0 && n
b10e0 3c 3d 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20  <=4 );.    z[0] 
b10f0 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75  = 0;.    z = zBu
b1100 66 3b 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31  f;.    READ_UTF1
b1110 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 61  6LE(z, c);.    a
b1120 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20  ssert( c==i );. 
b1130 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42     assert( (z-zB
b1140 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20  uf)==n );.  }.  
b1150 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31  for(i=0; i<0x001
b1160 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20  10000; i++){.   
b1170 20 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26   if( i>=0xD800 &
b1180 26 20 69 3c 30 78 45 30 30 30 20 29 20 63 6f 6e  & i<0xE000 ) con
b1190 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 7a  tinue;.    z = z
b11a0 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55  Buf;.    WRITE_U
b11b0 54 46 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20 20  TF16BE(z, i);.  
b11c0 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a 42    n = (int)(z-zB
b11d0 75 66 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  uf);.    assert(
b11e0 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b 0a   n>0 && n<=4 );.
b11f0 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20      z[0] = 0;.  
b1200 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20    z = zBuf;.    
b1210 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20  READ_UTF16BE(z, 
b1220 63 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  c);.    assert( 
b1230 63 3d 3d 69 20 29 3b 0a 20 20 20 20 61 73 73 65  c==i );.    asse
b1240 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20  rt( (z-zBuf)==n 
b1250 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20  );.  }.}.#endif 
b1260 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a  /* SQLITE_TEST *
b1270 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  /.#endif /* SQLI
b1280 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
b1290 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
b12a0 2a 20 45 6e 64 20 6f 66 20 75 74 66 2e 63 20 2a  * End of utf.c *
b12b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b12c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b12d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b12e0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
b12f0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 75 74 69  * Begin file uti
b1300 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  l.c ************
b1310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1330 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
b1340 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
b1350 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
b1360 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
b1370 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
b1380 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
b1390 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
b13a0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
b13b0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
b13c0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
b13d0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
b13e0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
b13f0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
b1400 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
b1410 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
b1420 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
b1430 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
b1440 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
b1450 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
b1460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b14a0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 55 74 69 6c  ********.** Util
b14b0 69 74 79 20 66 75 6e 63 74 69 6f 6e 73 20 75 73  ity functions us
b14c0 65 64 20 74 68 72 6f 75 67 68 6f 75 74 20 73 71  ed throughout sq
b14d0 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  lite..**.** This
b14e0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 66   file contains f
b14f0 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c  unctions for all
b1500 6f 63 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20  ocating memory, 
b1510 63 6f 6d 70 61 72 69 6e 67 0a 2a 2a 20 73 74 72  comparing.** str
b1520 69 6e 67 73 2c 20 61 6e 64 20 73 74 75 66 66 20  ings, and stuff 
b1530 6c 69 6b 65 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2f  like that..**.*/
b1540 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48  .#ifdef SQLITE_H
b1550 41 56 45 5f 49 53 4e 41 4e 0a 23 20 69 6e 63 6c  AVE_ISNAN.# incl
b1560 75 64 65 20 3c 6d 61 74 68 2e 68 3e 0a 23 65 6e  ude <math.h>.#en
b1570 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69  dif../*.** Routi
b1580 6e 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75 70  ne needed to sup
b1590 70 6f 72 74 20 74 68 65 20 74 65 73 74 63 61 73  port the testcas
b15a0 65 28 29 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69  e() macro..*/.#i
b15b0 66 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45  fdef SQLITE_COVE
b15c0 52 41 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  RAGE_TEST.SQLITE
b15d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
b15e0 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e  lite3Coverage(in
b15f0 74 20 78 29 7b 0a 20 20 73 74 61 74 69 63 20 69  t x){.  static i
b1600 6e 74 20 64 75 6d 6d 79 20 3d 20 30 3b 0a 20 20  nt dummy = 0;.  
b1610 64 75 6d 6d 79 20 2b 3d 20 78 3b 0a 7d 0a 23 65  dummy += x;.}.#e
b1620 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ndif../*.** Retu
b1630 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 66  rn true if the f
b1640 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61  loating point va
b1650 6c 75 65 20 69 73 20 4e 6f 74 20 61 20 4e 75 6d  lue is Not a Num
b1660 62 65 72 20 28 4e 61 4e 29 2e 0a 2a 2a 0a 2a 2a  ber (NaN)..**.**
b1670 20 55 73 65 20 74 68 65 20 6d 61 74 68 20 6c 69   Use the math li
b1680 62 72 61 72 79 20 69 73 6e 61 6e 28 29 20 66 75  brary isnan() fu
b1690 6e 63 74 69 6f 6e 20 69 66 20 63 6f 6d 70 69 6c  nction if compil
b16a0 65 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 48  ed with SQLITE_H
b16b0 41 56 45 5f 49 53 4e 41 4e 2e 0a 2a 2a 20 4f 74  AVE_ISNAN..** Ot
b16c0 68 65 72 77 69 73 65 2c 20 77 65 20 68 61 76 65  herwise, we have
b16d0 20 6f 75 72 20 6f 77 6e 20 69 6d 70 6c 65 6d 65   our own impleme
b16e0 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 77 6f 72  ntation that wor
b16f0 6b 73 20 6f 6e 20 6d 6f 73 74 20 73 79 73 74 65  ks on most syste
b1700 6d 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ms..*/.SQLITE_PR
b1710 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b1720 33 49 73 4e 61 4e 28 64 6f 75 62 6c 65 20 78 29  3IsNaN(double x)
b1730 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 2f 2a  {.  int rc;   /*
b1740 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   The value retur
b1750 6e 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  n */.#if !define
b1760 64 28 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53  d(SQLITE_HAVE_IS
b1770 4e 41 4e 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53  NAN).  /*.  ** S
b1780 79 73 74 65 6d 73 20 74 68 61 74 20 73 75 70 70  ystems that supp
b1790 6f 72 74 20 74 68 65 20 69 73 6e 61 6e 28 29 20  ort the isnan() 
b17a0 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e  library function
b17b0 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79   should probably
b17c0 0a 20 20 2a 2a 20 6d 61 6b 65 20 75 73 65 20 6f  .  ** make use o
b17d0 66 20 69 74 20 62 79 20 63 6f 6d 70 69 6c 69 6e  f it by compilin
b17e0 67 20 77 69 74 68 20 2d 44 53 51 4c 49 54 45 5f  g with -DSQLITE_
b17f0 48 41 56 45 5f 49 53 4e 41 4e 2e 20 20 42 75 74  HAVE_ISNAN.  But
b1800 20 77 65 20 68 61 76 65 0a 20 20 2a 2a 20 66 6f   we have.  ** fo
b1810 75 6e 64 20 74 68 61 74 20 6d 61 6e 79 20 73 79  und that many sy
b1820 73 74 65 6d 73 20 64 6f 20 6e 6f 74 20 68 61 76  stems do not hav
b1830 65 20 61 20 77 6f 72 6b 69 6e 67 20 69 73 6e 61  e a working isna
b1840 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 73 6f 0a  n() function so.
b1850 20 20 2a 2a 20 74 68 69 73 20 69 6d 70 6c 65 6d    ** this implem
b1860 65 6e 74 61 74 69 6f 6e 20 69 73 20 70 72 6f 76  entation is prov
b1870 69 64 65 64 20 61 73 20 61 6e 20 61 6c 74 65 72  ided as an alter
b1880 6e 61 74 69 76 65 2e 0a 20 20 2a 2a 0a 20 20 2a  native..  **.  *
b1890 2a 20 54 68 69 73 20 4e 61 4e 20 74 65 73 74 20  * This NaN test 
b18a0 73 6f 6d 65 74 69 6d 65 73 20 66 61 69 6c 73 20  sometimes fails 
b18b0 69 66 20 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 47  if compiled on G
b18c0 43 43 20 77 69 74 68 20 2d 66 66 61 73 74 2d 6d  CC with -ffast-m
b18d0 61 74 68 2e 0a 20 20 2a 2a 20 4f 6e 20 74 68 65  ath..  ** On the
b18e0 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 74 68 65   other hand, the
b18f0 20 75 73 65 20 6f 66 20 2d 66 66 61 73 74 2d 6d   use of -ffast-m
b1900 61 74 68 20 63 6f 6d 65 73 20 77 69 74 68 20 74  ath comes with t
b1910 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a  he following.  *
b1920 2a 20 77 61 72 6e 69 6e 67 3a 0a 20 20 2a 2a 0a  * warning:.  **.
b1930 20 20 2a 2a 20 20 20 20 20 20 54 68 69 73 20 6f    **      This o
b1940 70 74 69 6f 6e 20 5b 2d 66 66 61 73 74 2d 6d 61  ption [-ffast-ma
b1950 74 68 5d 20 73 68 6f 75 6c 64 20 6e 65 76 65 72  th] should never
b1960 20 62 65 20 74 75 72 6e 65 64 20 6f 6e 20 62 79   be turned on by
b1970 20 61 6e 79 0a 20 20 2a 2a 20 20 20 20 20 20 2d   any.  **      -
b1980 4f 20 6f 70 74 69 6f 6e 20 73 69 6e 63 65 20 69  O option since i
b1990 74 20 63 61 6e 20 72 65 73 75 6c 74 20 69 6e 20  t can result in 
b19a0 69 6e 63 6f 72 72 65 63 74 20 6f 75 74 70 75 74  incorrect output
b19b0 20 66 6f 72 20 70 72 6f 67 72 61 6d 73 0a 20 20   for programs.  
b19c0 2a 2a 20 20 20 20 20 20 77 68 69 63 68 20 64 65  **      which de
b19d0 70 65 6e 64 20 6f 6e 20 61 6e 20 65 78 61 63 74  pend on an exact
b19e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
b19f0 6f 66 20 49 45 45 45 20 6f 72 20 49 53 4f 20 0a  of IEEE or ISO .
b1a00 20 20 2a 2a 20 20 20 20 20 20 72 75 6c 65 73 2f    **      rules/
b1a10 73 70 65 63 69 66 69 63 61 74 69 6f 6e 73 20 66  specifications f
b1a20 6f 72 20 6d 61 74 68 20 66 75 6e 63 74 69 6f 6e  or math function
b1a30 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 55 6e 64  s..  **.  ** Und
b1a40 65 72 20 4d 53 56 43 2c 20 74 68 69 73 20 4e 61  er MSVC, this Na
b1a50 4e 20 74 65 73 74 20 6d 61 79 20 66 61 69 6c 20  N test may fail 
b1a60 69 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  if compiled with
b1a70 20 61 20 66 6c 6f 61 74 69 6e 67 2d 0a 20 20 2a   a floating-.  *
b1a80 2a 20 70 6f 69 6e 74 20 70 72 65 63 69 73 69 6f  * point precisio
b1a90 6e 20 6d 6f 64 65 20 6f 74 68 65 72 20 74 68 61  n mode other tha
b1aa0 6e 20 2f 66 70 3a 70 72 65 63 69 73 65 2e 20 20  n /fp:precise.  
b1ab0 46 72 6f 6d 20 74 68 65 20 4d 53 44 4e 20 0a 20  From the MSDN . 
b1ac0 20 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f   ** documentatio
b1ad0 6e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  n:.  **.  **    
b1ae0 20 20 54 68 65 20 63 6f 6d 70 69 6c 65 72 20 5b    The compiler [
b1af0 77 69 74 68 20 2f 66 70 3a 70 72 65 63 69 73 65  with /fp:precise
b1b00 5d 20 77 69 6c 6c 20 70 72 6f 70 65 72 6c 79 20  ] will properly 
b1b10 68 61 6e 64 6c 65 20 63 6f 6d 70 61 72 69 73 6f  handle compariso
b1b20 6e 73 20 0a 20 20 2a 2a 20 20 20 20 20 20 69 6e  ns .  **      in
b1b30 76 6f 6c 76 69 6e 67 20 4e 61 4e 2e 20 46 6f 72  volving NaN. For
b1b40 20 65 78 61 6d 70 6c 65 2c 20 78 20 21 3d 20 78   example, x != x
b1b50 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 74 72   evaluates to tr
b1b60 75 65 20 69 66 20 78 20 69 73 20 4e 61 4e 20 0a  ue if x is NaN .
b1b70 20 20 2a 2a 20 20 20 20 20 20 2e 2e 2e 0a 20 20    **      ....  
b1b80 2a 2f 0a 23 69 66 64 65 66 20 5f 5f 46 41 53 54  */.#ifdef __FAST
b1b90 5f 4d 41 54 48 5f 5f 0a 23 20 65 72 72 6f 72 20  _MATH__.# error 
b1ba0 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20  SQLite will not 
b1bb0 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 77  work correctly w
b1bc0 69 74 68 20 74 68 65 20 2d 66 66 61 73 74 2d 6d  ith the -ffast-m
b1bd0 61 74 68 20 6f 70 74 69 6f 6e 20 6f 66 20 47 43  ath option of GC
b1be0 43 2e 0a 23 65 6e 64 69 66 0a 20 20 76 6f 6c 61  C..#endif.  vola
b1bf0 74 69 6c 65 20 64 6f 75 62 6c 65 20 79 20 3d 20  tile double y = 
b1c00 78 3b 0a 20 20 76 6f 6c 61 74 69 6c 65 20 64 6f  x;.  volatile do
b1c10 75 62 6c 65 20 7a 20 3d 20 79 3b 0a 20 20 72 63  uble z = y;.  rc
b1c20 20 3d 20 28 79 21 3d 7a 29 3b 0a 23 65 6c 73 65   = (y!=z);.#else
b1c30 20 20 2f 2a 20 69 66 20 64 65 66 69 6e 65 64 28    /* if defined(
b1c40 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e 41  SQLITE_HAVE_ISNA
b1c50 4e 29 20 2a 2f 0a 20 20 72 63 20 3d 20 69 73 6e  N) */.  rc = isn
b1c60 61 6e 28 78 29 3b 0a 23 65 6e 64 69 66 20 2f 2a  an(x);.#endif /*
b1c70 20 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53 4e   SQLITE_HAVE_ISN
b1c80 41 4e 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65  AN */.  testcase
b1c90 28 20 72 63 20 29 3b 0a 20 20 72 65 74 75 72 6e  ( rc );.  return
b1ca0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f   rc;.}../*.** Co
b1cb0 6d 70 75 74 65 20 61 20 73 74 72 69 6e 67 20 6c  mpute a string l
b1cc0 65 6e 67 74 68 20 74 68 61 74 20 69 73 20 6c 69  ength that is li
b1cd0 6d 69 74 65 64 20 74 6f 20 77 68 61 74 20 63 61  mited to what ca
b1ce0 6e 20 62 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a  n be stored in.*
b1cf0 2a 20 6c 6f 77 65 72 20 33 30 20 62 69 74 73 20  * lower 30 bits 
b1d00 6f 66 20 61 20 33 32 2d 62 69 74 20 73 69 67 6e  of a 32-bit sign
b1d10 65 64 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a  ed integer..**.*
b1d20 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75  * The value retu
b1d30 72 6e 65 64 20 77 69 6c 6c 20 6e 65 76 65 72 20  rned will never 
b1d40 62 65 20 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f  be negative.  No
b1d50 72 20 77 69 6c 6c 20 69 74 20 65 76 65 72 20 62  r will it ever b
b1d60 65 20 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61  e greater.** tha
b1d70 6e 20 74 68 65 20 61 63 74 75 61 6c 20 6c 65 6e  n the actual len
b1d80 67 74 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e  gth of the strin
b1d90 67 2e 20 20 46 6f 72 20 76 65 72 79 20 6c 6f 6e  g.  For very lon
b1da0 67 20 73 74 72 69 6e 67 73 20 28 67 72 65 61 74  g strings (great
b1db0 65 72 0a 2a 2a 20 74 68 61 6e 20 31 47 69 42 29  er.** than 1GiB)
b1dc0 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
b1dd0 6e 65 64 20 6d 69 67 68 74 20 62 65 20 6c 65 73  ned might be les
b1de0 73 20 74 68 61 6e 20 74 68 65 20 74 72 75 65 20  s than the true 
b1df0 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 2e 0a 2a  string length..*
b1e00 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b1e10 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72 6c   int sqlite3Strl
b1e20 65 6e 33 30 28 63 6f 6e 73 74 20 63 68 61 72 20  en30(const char 
b1e30 2a 7a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61  *z){.  const cha
b1e40 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20 20 69 66 28  r *z2 = z;.  if(
b1e50 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   z==0 ) return 0
b1e60 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 32 20 29  ;.  while( *z2 )
b1e70 7b 20 7a 32 2b 2b 3b 20 7d 0a 20 20 72 65 74 75  { z2++; }.  retu
b1e80 72 6e 20 30 78 33 66 66 66 66 66 66 66 20 26 20  rn 0x3fffffff & 
b1e90 28 69 6e 74 29 28 7a 32 20 2d 20 7a 29 3b 0a 7d  (int)(z2 - z);.}
b1ea0 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
b1eb0 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f  most recent erro
b1ec0 72 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f 72  r code and error
b1ed0 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20   string for the 
b1ee0 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64 6c 65  sqlite.** handle
b1ef0 20 22 64 62 22 2e 20 54 68 65 20 65 72 72 6f 72   "db". The error
b1f00 20 63 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20   code is set to 
b1f10 22 65 72 72 5f 63 6f 64 65 22 2e 0a 2a 2a 0a 2a  "err_code"..**.*
b1f20 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 4e  * If it is not N
b1f30 55 4c 4c 2c 20 73 74 72 69 6e 67 20 7a 46 6f 72  ULL, string zFor
b1f40 6d 61 74 20 73 70 65 63 69 66 69 65 73 20 74 68  mat specifies th
b1f50 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 0a  e format of the.
b1f60 2a 2a 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20  ** error string 
b1f70 69 6e 20 74 68 65 20 73 74 79 6c 65 20 6f 66 20  in the style of 
b1f80 74 68 65 20 70 72 69 6e 74 66 20 66 75 6e 63 74  the printf funct
b1f90 69 6f 6e 73 3a 20 54 68 65 20 66 6f 6c 6c 6f 77  ions: The follow
b1fa0 69 6e 67 0a 2a 2a 20 66 6f 72 6d 61 74 20 63 68  ing.** format ch
b1fb0 61 72 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c  aracters are all
b1fc0 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  owed:.**.**     
b1fd0 20 25 73 20 20 20 20 20 20 49 6e 73 65 72 74 20   %s      Insert 
b1fe0 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20  a string.**     
b1ff0 20 25 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e   %z      A strin
b2000 67 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65  g that should be
b2010 20 66 72 65 65 64 20 61 66 74 65 72 20 75 73 65   freed after use
b2020 0a 2a 2a 20 20 20 20 20 20 25 64 20 20 20 20 20  .**      %d     
b2030 20 49 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67   Insert an integ
b2040 65 72 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20  er.**      %T   
b2050 20 20 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65     Insert a toke
b2060 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20  n.**      %S    
b2070 20 20 49 6e 73 65 72 74 20 74 68 65 20 66 69 72    Insert the fir
b2080 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20  st element of a 
b2090 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 7a 46  SrcList.**.** zF
b20a0 6f 72 6d 61 74 20 61 6e 64 20 61 6e 79 20 73 74  ormat and any st
b20b0 72 69 6e 67 20 74 6f 6b 65 6e 73 20 74 68 61 74  ring tokens that
b20c0 20 66 6f 6c 6c 6f 77 20 69 74 20 61 72 65 20 61   follow it are a
b20d0 73 73 75 6d 65 64 20 74 6f 20 62 65 0a 2a 2a 20  ssumed to be.** 
b20e0 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38  encoded in UTF-8
b20f0 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 63 6c 65 61 72  ..**.** To clear
b2100 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
b2110 20 65 72 72 6f 72 20 66 6f 72 20 73 71 6c 69 74   error for sqlit
b2120 65 20 68 61 6e 64 6c 65 20 22 64 62 22 2c 20 73  e handle "db", s
b2130 71 6c 69 74 65 33 45 72 72 6f 72 0a 2a 2a 20 73  qlite3Error.** s
b2140 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20  hould be called 
b2150 77 69 74 68 20 65 72 72 5f 63 6f 64 65 20 73 65  with err_code se
b2160 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 61  t to SQLITE_OK a
b2170 6e 64 20 7a 46 6f 72 6d 61 74 20 73 65 74 0a 2a  nd zFormat set.*
b2180 2a 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51  * to NULL..*/.SQ
b2190 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
b21a0 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 73  d sqlite3Error(s
b21b0 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
b21c0 65 72 72 5f 63 6f 64 65 2c 20 63 6f 6e 73 74 20  err_code, const 
b21d0 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e  char *zFormat, .
b21e0 2e 2e 29 7b 0a 20 20 69 66 28 20 64 62 20 26 26  ..){.  if( db &&
b21f0 20 28 64 62 2d 3e 70 45 72 72 20 7c 7c 20 28 64   (db->pErr || (d
b2200 62 2d 3e 70 45 72 72 20 3d 20 73 71 6c 69 74 65  b->pErr = sqlite
b2210 33 56 61 6c 75 65 4e 65 77 28 64 62 29 29 21 3d  3ValueNew(db))!=
b2220 30 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e 65 72  0) ){.    db->er
b2230 72 43 6f 64 65 20 3d 20 65 72 72 5f 63 6f 64 65  rCode = err_code
b2240 3b 0a 20 20 20 20 69 66 28 20 7a 46 6f 72 6d 61  ;.    if( zForma
b2250 74 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20  t ){.      char 
b2260 2a 7a 3b 0a 20 20 20 20 20 20 76 61 5f 6c 69 73  *z;.      va_lis
b2270 74 20 61 70 3b 0a 20 20 20 20 20 20 76 61 5f 73  t ap;.      va_s
b2280 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74  tart(ap, zFormat
b2290 29 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c  );.      z = sql
b22a0 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c  ite3VMPrintf(db,
b22b0 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20   zFormat, ap);. 
b22c0 20 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b       va_end(ap);
b22d0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61  .      sqlite3Va
b22e0 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70 45  lueSetStr(db->pE
b22f0 72 72 2c 20 2d 31 2c 20 7a 2c 20 53 51 4c 49 54  rr, -1, z, SQLIT
b2300 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 44  E_UTF8, SQLITE_D
b2310 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d 65 6c  YNAMIC);.    }el
b2320 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
b2330 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d  3ValueSetStr(db-
b2340 3e 70 45 72 72 2c 20 30 2c 20 30 2c 20 53 51 4c  >pErr, 0, 0, SQL
b2350 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45  ITE_UTF8, SQLITE
b2360 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a  _STATIC);.    }.
b2370 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64    }.}../*.** Add
b2380 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
b2390 65 20 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72  e to pParse->zEr
b23a0 72 4d 73 67 20 61 6e 64 20 69 6e 63 72 65 6d 65  rMsg and increme
b23b0 6e 74 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2e  nt pParse->nErr.
b23c0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
b23d0 67 20 66 6f 72 6d 61 74 74 69 6e 67 20 63 68 61  g formatting cha
b23e0 72 61 63 74 65 72 73 20 61 72 65 20 61 6c 6c 6f  racters are allo
b23f0 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  wed:.**.**      
b2400 25 73 20 20 20 20 20 20 49 6e 73 65 72 74 20 61  %s      Insert a
b2410 20 73 74 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20   string.**      
b2420 25 7a 20 20 20 20 20 20 41 20 73 74 72 69 6e 67  %z      A string
b2430 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
b2440 66 72 65 65 64 20 61 66 74 65 72 20 75 73 65 0a  freed after use.
b2450 2a 2a 20 20 20 20 20 20 25 64 20 20 20 20 20 20  **      %d      
b2460 49 6e 73 65 72 74 20 61 6e 20 69 6e 74 65 67 65  Insert an intege
b2470 72 0a 2a 2a 20 20 20 20 20 20 25 54 20 20 20 20  r.**      %T    
b2480 20 20 49 6e 73 65 72 74 20 61 20 74 6f 6b 65 6e    Insert a token
b2490 0a 2a 2a 20 20 20 20 20 20 25 53 20 20 20 20 20  .**      %S     
b24a0 20 49 6e 73 65 72 74 20 74 68 65 20 66 69 72 73   Insert the firs
b24b0 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 53  t element of a S
b24c0 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a 20 54 68 69  rcList.**.** Thi
b24d0 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c  s function shoul
b24e0 64 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 70  d be used to rep
b24f0 6f 72 74 20 61 6e 79 20 65 72 72 6f 72 20 74 68  ort any error th
b2500 61 74 20 6f 63 63 75 72 73 20 77 68 69 6c 73 74  at occurs whilst
b2510 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 61 6e  .** compiling an
b2520 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 28   SQL statement (
b2530 69 2e 65 2e 20 77 69 74 68 69 6e 20 73 71 6c 69  i.e. within sqli
b2540 74 65 33 5f 70 72 65 70 61 72 65 28 29 29 2e 20  te3_prepare()). 
b2550 54 68 65 0a 2a 2a 20 6c 61 73 74 20 74 68 69 6e  The.** last thin
b2560 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72  g the sqlite3_pr
b2570 65 70 61 72 65 28 29 20 66 75 6e 63 74 69 6f 6e  epare() function
b2580 20 64 6f 65 73 20 69 73 20 63 6f 70 79 20 74 68   does is copy th
b2590 65 20 65 72 72 6f 72 0a 2a 2a 20 73 74 6f 72 65  e error.** store
b25a0 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  d by this functi
b25b0 6f 6e 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61  on into the data
b25c0 62 61 73 65 20 68 61 6e 64 6c 65 20 75 73 69 6e  base handle usin
b25d0 67 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29  g sqlite3Error()
b25e0 2e 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 73 71  ..** Function sq
b25f0 6c 69 74 65 33 45 72 72 6f 72 28 29 20 73 68 6f  lite3Error() sho
b2600 75 6c 64 20 62 65 20 75 73 65 64 20 64 75 72 69  uld be used duri
b2610 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 65 78 65  ng statement exe
b2620 63 75 74 69 6f 6e 0a 2a 2a 20 28 73 71 6c 69 74  cution.** (sqlit
b2630 65 33 5f 73 74 65 70 28 29 20 65 74 63 2e 29 2e  e3_step() etc.).
b2640 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b2650 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
b2660 72 72 6f 72 4d 73 67 28 50 61 72 73 65 20 2a 70  rrorMsg(Parse *p
b2670 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61  Parse, const cha
b2680 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29  r *zFormat, ...)
b2690 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a  {.  va_list ap;.
b26a0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
b26b0 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 50  pParse->db;.  pP
b26c0 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20  arse->nErr++;.  
b26d0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
b26e0 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73  , pParse->zErrMs
b26f0 67 29 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61  g);.  va_start(a
b2700 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 70  p, zFormat);.  p
b2710 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d  Parse->zErrMsg =
b2720 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66   sqlite3VMPrintf
b2730 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70  (db, zFormat, ap
b2740 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b  );.  va_end(ap);
b2750 0a 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20  .  pParse->rc = 
b2760 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a  SQLITE_ERROR;.}.
b2770 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65  ./*.** Clear the
b2780 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
b2790 6e 20 70 50 61 72 73 65 2c 20 69 66 20 61 6e 79  n pParse, if any
b27a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b27b0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
b27c0 72 72 6f 72 43 6c 65 61 72 28 50 61 72 73 65 20  rrorClear(Parse 
b27d0 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69  *pParse){.  sqli
b27e0 74 65 33 44 62 46 72 65 65 28 70 50 61 72 73 65  te3DbFree(pParse
b27f0 2d 3e 64 62 2c 20 70 50 61 72 73 65 2d 3e 7a 45  ->db, pParse->zE
b2800 72 72 4d 73 67 29 3b 0a 20 20 70 50 61 72 73 65  rrMsg);.  pParse
b2810 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  ->zErrMsg = 0;. 
b2820 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 3d 20   pParse->nErr = 
b2830 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  0;.}../*.** Conv
b2840 65 72 74 20 61 6e 20 53 51 4c 2d 73 74 79 6c 65  ert an SQL-style
b2850 20 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 69   quoted string i
b2860 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c 20 73 74 72  nto a normal str
b2870 69 6e 67 20 62 79 20 72 65 6d 6f 76 69 6e 67 0a  ing by removing.
b2880 2a 2a 20 74 68 65 20 71 75 6f 74 65 20 63 68 61  ** the quote cha
b2890 72 61 63 74 65 72 73 2e 20 20 54 68 65 20 63 6f  racters.  The co
b28a0 6e 76 65 72 73 69 6f 6e 20 69 73 20 64 6f 6e 65  nversion is done
b28b0 20 69 6e 2d 70 6c 61 63 65 2e 20 20 49 66 20 74   in-place.  If t
b28c0 68 65 0a 2a 2a 20 69 6e 70 75 74 20 64 6f 65 73  he.** input does
b28d0 20 6e 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20   not begin with 
b28e0 61 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 65  a quote characte
b28f0 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  r, then this rou
b2900 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d  tine.** is a no-
b2910 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e  op..**.** The in
b2920 70 75 74 20 73 74 72 69 6e 67 20 6d 75 73 74 20  put string must 
b2930 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  be zero-terminat
b2940 65 64 2e 20 20 41 20 6e 65 77 20 7a 65 72 6f 2d  ed.  A new zero-
b2950 74 65 72 6d 69 6e 61 74 6f 72 0a 2a 2a 20 69 73  terminator.** is
b2960 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 64 65   added to the de
b2970 71 75 6f 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a  quoted string..*
b2980 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20  *.** The return 
b2990 76 61 6c 75 65 20 69 73 20 2d 31 20 69 66 20 6e  value is -1 if n
b29a0 6f 20 64 65 71 75 6f 74 69 6e 67 20 6f 63 63 75  o dequoting occu
b29b0 72 73 20 6f 72 20 74 68 65 20 6c 65 6e 67 74 68  rs or the length
b29c0 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65 71 75 6f   of the.** dequo
b29d0 74 65 64 20 73 74 72 69 6e 67 2c 20 65 78 63 6c  ted string, excl
b29e0 75 73 69 76 65 20 6f 66 20 74 68 65 20 7a 65 72  usive of the zer
b29f0 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2c 20 69 66  o terminator, if
b2a00 20 64 65 71 75 6f 74 69 6e 67 20 64 6f 65 73 0a   dequoting does.
b2a10 2a 2a 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a 20  ** occur..**.** 
b2a20 32 30 30 32 2d 46 65 62 2d 31 34 3a 20 54 68 69  2002-Feb-14: Thi
b2a30 73 20 72 6f 75 74 69 6e 65 20 69 73 20 65 78 74  s routine is ext
b2a40 65 6e 64 65 64 20 74 6f 20 72 65 6d 6f 76 65 20  ended to remove 
b2a50 4d 53 2d 41 63 63 65 73 73 20 73 74 79 6c 65 0a  MS-Access style.
b2a60 2a 2a 20 62 72 61 63 6b 65 74 73 20 66 72 6f 6d  ** brackets from
b2a70 20 61 72 6f 75 6e 64 20 69 64 65 6e 74 69 66 65   around identife
b2a80 72 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  rs.  For example
b2a90 3a 20 20 22 5b 61 2d 62 2d 63 5d 22 20 62 65 63  :  "[a-b-c]" bec
b2aa0 6f 6d 65 73 0a 2a 2a 20 22 61 2d 62 2d 63 22 2e  omes.** "a-b-c".
b2ab0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b2ac0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 44 65  TE int sqlite3De
b2ad0 71 75 6f 74 65 28 63 68 61 72 20 2a 7a 29 7b 0a  quote(char *z){.
b2ae0 20 20 63 68 61 72 20 71 75 6f 74 65 3b 0a 20 20    char quote;.  
b2af0 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28 20  int i, j;.  if( 
b2b00 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 2d 31  z==0 ) return -1
b2b10 3b 0a 20 20 71 75 6f 74 65 20 3d 20 7a 5b 30 5d  ;.  quote = z[0]
b2b20 3b 0a 20 20 73 77 69 74 63 68 28 20 71 75 6f 74  ;.  switch( quot
b2b30 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 27 5c  e ){.    case '\
b2b40 27 27 3a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20  '':  break;.    
b2b50 63 61 73 65 20 27 22 27 3a 20 20 20 62 72 65 61  case '"':   brea
b2b60 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a  k;.    case '`':
b2b70 20 20 20 62 72 65 61 6b 3b 20 20 20 20 20 20 20     break;       
b2b80 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
b2b90 4d 79 53 51 4c 20 63 6f 6d 70 61 74 69 62 69 6c  MySQL compatibil
b2ba0 69 74 79 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ity */.    case 
b2bb0 27 5b 27 3a 20 20 20 71 75 6f 74 65 20 3d 20 27  '[':   quote = '
b2bc0 5d 27 3b 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20  ]';  break;  /* 
b2bd0 46 6f 72 20 4d 53 20 53 71 6c 53 65 72 76 65 72  For MS SqlServer
b2be0 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2a   compatibility *
b2bf0 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20  /.    default:  
b2c00 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d    return -1;.  }
b2c10 0a 20 20 66 6f 72 28 69 3d 31 2c 20 6a 3d 30 3b  .  for(i=1, j=0;
b2c20 20 41 4c 57 41 59 53 28 7a 5b 69 5d 29 3b 20 69   ALWAYS(z[i]); i
b2c30 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69  ++){.    if( z[i
b2c40 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20  ]==quote ){.    
b2c50 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 71 75    if( z[i+1]==qu
b2c60 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  ote ){.        z
b2c70 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74 65 3b 0a 20  [j++] = quote;. 
b2c80 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20         i++;.    
b2c90 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
b2ca0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
b2cb0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
b2cc0 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a   z[j++] = z[i];.
b2cd0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 7a 5b 6a 5d      }.  }.  z[j]
b2ce0 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 6a   = 0;.  return j
b2cf0 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76 65 6e 69 65  ;.}../* Convenie
b2d00 6e 74 20 73 68 6f 72 74 2d 68 61 6e 64 20 2a 2f  nt short-hand */
b2d10 0a 23 64 65 66 69 6e 65 20 55 70 70 65 72 54 6f  .#define UpperTo
b2d20 4c 6f 77 65 72 20 73 71 6c 69 74 65 33 55 70 70  Lower sqlite3Upp
b2d30 65 72 54 6f 4c 6f 77 65 72 0a 0a 2f 2a 0a 2a 2a  erToLower../*.**
b2d40 20 53 6f 6d 65 20 73 79 73 74 65 6d 73 20 68 61   Some systems ha
b2d50 76 65 20 73 74 72 69 63 6d 70 28 29 2e 20 20 4f  ve stricmp().  O
b2d60 74 68 65 72 73 20 68 61 76 65 20 73 74 72 63 61  thers have strca
b2d70 73 65 63 6d 70 28 29 2e 20 20 42 65 63 61 75 73  secmp().  Becaus
b2d80 65 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f  e.** there is no
b2d90 20 63 6f 6e 73 69 73 74 65 6e 63 79 2c 20 77 65   consistency, we
b2da0 20 77 69 6c 6c 20 64 65 66 69 6e 65 20 6f 75 72   will define our
b2db0 20 6f 77 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   own..*/.SQLITE_
b2dc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
b2dd0 74 65 33 53 74 72 49 43 6d 70 28 63 6f 6e 73 74  te3StrICmp(const
b2de0 20 63 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f   char *zLeft, co
b2df0 6e 73 74 20 63 68 61 72 20 2a 7a 52 69 67 68 74  nst char *zRight
b2e00 29 7b 0a 20 20 72 65 67 69 73 74 65 72 20 75 6e  ){.  register un
b2e10 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 2c 20  signed char *a, 
b2e20 2a 62 3b 0a 20 20 61 20 3d 20 28 75 6e 73 69 67  *b;.  a = (unsig
b2e30 6e 65 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74  ned char *)zLeft
b2e40 3b 0a 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65  ;.  b = (unsigne
b2e50 64 20 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b  d char *)zRight;
b2e60 0a 20 20 77 68 69 6c 65 28 20 2a 61 21 3d 30 20  .  while( *a!=0 
b2e70 26 26 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  && UpperToLower[
b2e80 2a 61 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65  *a]==UpperToLowe
b2e90 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b  r[*b]){ a++; b++
b2ea0 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 55 70 70  ; }.  return Upp
b2eb0 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20  erToLower[*a] - 
b2ec0 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d  UpperToLower[*b]
b2ed0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
b2ee0 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69  nt sqlite3_strni
b2ef0 63 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  cmp(const char *
b2f00 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68 61  zLeft, const cha
b2f10 72 20 2a 7a 52 69 67 68 74 2c 20 69 6e 74 20 4e  r *zRight, int N
b2f20 29 7b 0a 20 20 72 65 67 69 73 74 65 72 20 75 6e  ){.  register un
b2f30 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 2c 20  signed char *a, 
b2f40 2a 62 3b 0a 20 20 61 20 3d 20 28 75 6e 73 69 67  *b;.  a = (unsig
b2f50 6e 65 64 20 63 68 61 72 20 2a 29 7a 4c 65 66 74  ned char *)zLeft
b2f60 3b 0a 20 20 62 20 3d 20 28 75 6e 73 69 67 6e 65  ;.  b = (unsigne
b2f70 64 20 63 68 61 72 20 2a 29 7a 52 69 67 68 74 3b  d char *)zRight;
b2f80 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20  .  while( N-- > 
b2f90 30 20 26 26 20 2a 61 21 3d 30 20 26 26 20 55 70  0 && *a!=0 && Up
b2fa0 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d  perToLower[*a]==
b2fb0 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d  UpperToLower[*b]
b2fc0 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20  ){ a++; b++; }. 
b2fd0 20 72 65 74 75 72 6e 20 4e 3c 30 20 3f 20 30 20   return N<0 ? 0 
b2fe0 3a 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a  : UpperToLower[*
b2ff0 61 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65  a] - UpperToLowe
b3000 72 5b 2a 62 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  r[*b];.}../*.** 
b3010 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 7a  Return TRUE if z
b3020 20 69 73 20 61 20 70 75 72 65 20 6e 75 6d 65 72   is a pure numer
b3030 69 63 20 73 74 72 69 6e 67 2e 20 20 52 65 74 75  ic string.  Retu
b3040 72 6e 20 46 41 4c 53 45 20 61 6e 64 20 6c 65 61  rn FALSE and lea
b3050 76 65 0a 2a 2a 20 2a 72 65 61 6c 6e 75 6d 20 75  ve.** *realnum u
b3060 6e 63 68 61 6e 67 65 64 20 69 66 20 74 68 65 20  nchanged if the 
b3070 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 73 20  string contains 
b3080 61 6e 79 20 63 68 61 72 61 63 74 65 72 20 77 68  any character wh
b3090 69 63 68 20 69 73 20 6e 6f 74 0a 2a 2a 20 70 61  ich is not.** pa
b30a0 72 74 20 6f 66 20 61 20 6e 75 6d 62 65 72 2e 0a  rt of a number..
b30b0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 72  **.** If the str
b30c0 69 6e 67 20 69 73 20 70 75 72 65 20 6e 75 6d 65  ing is pure nume
b30d0 72 69 63 2c 20 73 65 74 20 2a 72 65 61 6c 6e 75  ric, set *realnu
b30e0 6d 20 74 6f 20 54 52 55 45 20 69 66 20 74 68 65  m to TRUE if the
b30f0 20 73 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e 74 61   string.** conta
b3100 69 6e 73 20 74 68 65 20 27 2e 27 20 63 68 61 72  ins the '.' char
b3110 61 63 74 65 72 20 6f 72 20 61 6e 20 22 45 2b 30  acter or an "E+0
b3120 30 30 22 20 73 74 79 6c 65 20 65 78 70 6f 6e 65  00" style expone
b3130 6e 74 69 61 74 69 6f 6e 20 73 75 66 66 69 78 2e  ntiation suffix.
b3140 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65  .** Otherwise se
b3150 74 20 2a 72 65 61 6c 6e 75 6d 20 74 6f 20 46 41  t *realnum to FA
b3160 4c 53 45 2e 20 20 4e 6f 74 65 20 74 68 61 74 20  LSE.  Note that 
b3170 6a 75 73 74 20 62 65 63 61 75 65 20 2a 72 65 61  just becaue *rea
b3180 6c 6e 75 6d 20 69 73 0a 2a 2a 20 66 61 6c 73 65  lnum is.** false
b3190 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e 20 74   does not mean t
b31a0 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 63  hat the number c
b31b0 61 6e 20 62 65 20 73 75 63 63 65 73 73 66 75 6c  an be successful
b31c0 6c 79 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74  ly converted int
b31d0 6f 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 20  o.** an integer 
b31e0 2d 20 69 74 20 6d 69 67 68 74 20 62 65 20 74 6f  - it might be to
b31f0 6f 20 62 69 67 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20  o big..**.** An 
b3200 65 6d 70 74 79 20 73 74 72 69 6e 67 20 69 73 20  empty string is 
b3210 63 6f 6e 73 69 64 65 72 65 64 20 6e 6f 6e 2d 6e  considered non-n
b3220 75 6d 65 72 69 63 2e 0a 2a 2f 0a 53 51 4c 49 54  umeric..*/.SQLIT
b3230 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
b3240 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 63 6f  lite3IsNumber(co
b3250 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
b3260 20 2a 72 65 61 6c 6e 75 6d 2c 20 75 38 20 65 6e   *realnum, u8 en
b3270 63 29 7b 0a 20 20 69 6e 74 20 69 6e 63 72 20 3d  c){.  int incr =
b3280 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54   (enc==SQLITE_UT
b3290 46 38 3f 31 3a 32 29 3b 0a 20 20 69 66 28 20 65  F8?1:2);.  if( e
b32a0 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  nc==SQLITE_UTF16
b32b0 42 45 20 29 20 7a 2b 2b 3b 0a 20 20 69 66 28 20  BE ) z++;.  if( 
b32c0 2a 7a 3d 3d 27 2d 27 20 7c 7c 20 2a 7a 3d 3d 27  *z=='-' || *z=='
b32d0 2b 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a  +' ) z += incr;.
b32e0 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73    if( !sqlite3Is
b32f0 64 69 67 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20  digit(*z) ){.   
b3300 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
b3310 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 2a 72   z += incr;.  *r
b3320 65 61 6c 6e 75 6d 20 3d 20 30 3b 0a 20 20 77 68  ealnum = 0;.  wh
b3330 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69  ile( sqlite3Isdi
b3340 67 69 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20  git(*z) ){ z += 
b3350 69 6e 63 72 3b 20 7d 0a 20 20 69 66 28 20 2a 7a  incr; }.  if( *z
b3360 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 7a 20 2b  =='.' ){.    z +
b3370 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 20  = incr;.    if( 
b3380 21 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28  !sqlite3Isdigit(
b3390 2a 7a 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  *z) ) return 0;.
b33a0 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74      while( sqlit
b33b0 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b  e3Isdigit(*z) ){
b33c0 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20   z += incr; }.  
b33d0 20 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a    *realnum = 1;.
b33e0 20 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 65    }.  if( *z=='e
b33f0 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29 7b 0a  ' || *z=='E' ){.
b3400 20 20 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20      z += incr;. 
b3410 20 20 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 7c     if( *z=='+' |
b3420 7c 20 2a 7a 3d 3d 27 2d 27 20 29 20 7a 20 2b 3d  | *z=='-' ) z +=
b3430 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 28 20 21   incr;.    if( !
b3440 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a  sqlite3Isdigit(*
b3450 7a 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  z) ) return 0;. 
b3460 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65     while( sqlite
b3470 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 20  3Isdigit(*z) ){ 
b3480 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 20  z += incr; }.   
b3490 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20   *realnum = 1;. 
b34a0 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 3d 3d   }.  return *z==
b34b0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  0;.}../*.** The 
b34c0 73 74 72 69 6e 67 20 7a 5b 5d 20 69 73 20 61 6e  string z[] is an
b34d0 20 41 53 43 49 49 20 72 65 70 72 65 73 65 6e 74   ASCII represent
b34e0 61 74 69 6f 6e 20 6f 66 20 61 20 72 65 61 6c 20  ation of a real 
b34f0 6e 75 6d 62 65 72 2e 0a 2a 2a 20 43 6f 6e 76 65  number..** Conve
b3500 72 74 20 74 68 69 73 20 73 74 72 69 6e 67 20 74  rt this string t
b3510 6f 20 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 0a 2a  o a double..**.*
b3520 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
b3530 73 73 75 6d 65 73 20 74 68 61 74 20 7a 5b 5d 20  ssumes that z[] 
b3540 72 65 61 6c 6c 79 20 69 73 20 61 20 76 61 6c 69  really is a vali
b3550 64 20 6e 75 6d 62 65 72 2e 20 20 49 66 20 69 74  d number.  If it
b3560 0a 2a 2a 20 69 73 20 6e 6f 74 2c 20 74 68 65 20  .** is not, the 
b3570 72 65 73 75 6c 74 20 69 73 20 75 6e 64 65 66 69  result is undefi
b3580 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ned..**.** This 
b3590 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
b35a0 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6c  instead of the l
b35b0 69 62 72 61 72 79 20 61 74 6f 66 28 29 20 66 75  ibrary atof() fu
b35c0 6e 63 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a  nction because.*
b35d0 2a 20 74 68 65 20 6c 69 62 72 61 72 79 20 61 74  * the library at
b35e0 6f 66 28 29 20 6d 69 67 68 74 20 77 61 6e 74 20  of() might want 
b35f0 74 6f 20 75 73 65 20 22 2c 22 20 61 73 20 74 68  to use "," as th
b3600 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20  e decimal point 
b3610 69 6e 73 74 65 61 64 0a 2a 2a 20 6f 66 20 22 2e  instead.** of ".
b3620 22 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68  " depending on h
b3630 6f 77 20 6c 6f 63 61 6c 65 20 69 73 20 73 65 74  ow locale is set
b3640 2e 20 20 42 75 74 20 74 68 61 74 20 77 6f 75 6c  .  But that woul
b3650 64 20 63 61 75 73 65 20 70 72 6f 62 6c 65 6d 73  d cause problems
b3660 0a 2a 2a 20 66 6f 72 20 53 51 4c 2e 20 20 53 6f  .** for SQL.  So
b3670 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c   this routine al
b3680 77 61 79 73 20 75 73 65 73 20 22 2e 22 20 72 65  ways uses "." re
b3690 67 61 72 64 6c 65 73 73 20 6f 66 20 6c 6f 63 61  gardless of loca
b36a0 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
b36b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b36c0 33 41 74 6f 46 28 63 6f 6e 73 74 20 63 68 61 72  3AtoF(const char
b36d0 20 2a 7a 2c 20 64 6f 75 62 6c 65 20 2a 70 52 65   *z, double *pRe
b36e0 73 75 6c 74 29 7b 0a 23 69 66 6e 64 65 66 20 53  sult){.#ifndef S
b36f0 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
b3700 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 63 6f 6e 73  ING_POINT.  cons
b3710 74 20 63 68 61 72 20 2a 7a 42 65 67 69 6e 20 3d  t char *zBegin =
b3720 20 7a 3b 0a 20 20 2f 2a 20 73 69 67 6e 20 2a 20   z;.  /* sign * 
b3730 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 20 28 31  significand * (1
b3740 30 20 5e 20 28 65 73 69 67 6e 20 2a 20 65 78 70  0 ^ (esign * exp
b3750 6f 6e 65 6e 74 29 29 20 2a 2f 0a 20 20 69 6e 74  onent)) */.  int
b3760 20 73 69 67 6e 20 3d 20 31 3b 20 20 20 2f 2a 20   sign = 1;   /* 
b3770 73 69 67 6e 20 6f 66 20 73 69 67 6e 69 66 69 63  sign of signific
b3780 61 6e 64 20 2a 2f 0a 20 20 69 36 34 20 73 20 3d  and */.  i64 s =
b3790 20 30 3b 20 20 20 20 20 20 2f 2a 20 73 69 67 6e   0;      /* sign
b37a0 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 69 6e 74  ificand */.  int
b37b0 20 64 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20   d = 0;      /* 
b37c0 61 64 6a 75 73 74 20 65 78 70 6f 6e 65 6e 74 20  adjust exponent 
b37d0 66 6f 72 20 73 68 69 66 74 69 6e 67 20 64 65 63  for shifting dec
b37e0 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20  imal point */.  
b37f0 69 6e 74 20 65 73 69 67 6e 20 3d 20 31 3b 20 20  int esign = 1;  
b3800 2f 2a 20 73 69 67 6e 20 6f 66 20 65 78 70 6f 6e  /* sign of expon
b3810 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 65 20 3d  ent */.  int e =
b3820 20 30 3b 20 20 20 20 20 20 2f 2a 20 65 78 70 6f   0;      /* expo
b3830 6e 65 6e 74 20 2a 2f 0a 20 20 64 6f 75 62 6c 65  nent */.  double
b3840 20 72 65 73 75 6c 74 3b 0a 20 20 69 6e 74 20 6e   result;.  int n
b3850 44 69 67 69 74 73 20 3d 20 30 3b 0a 0a 20 20 2f  Digits = 0;..  /
b3860 2a 20 73 6b 69 70 20 6c 65 61 64 69 6e 67 20 73  * skip leading s
b3870 70 61 63 65 73 20 2a 2f 0a 20 20 77 68 69 6c 65  paces */.  while
b3880 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65  ( sqlite3Isspace
b3890 28 2a 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 2f 2a  (*z) ) z++;.  /*
b38a0 20 67 65 74 20 73 69 67 6e 20 6f 66 20 73 69 67   get sign of sig
b38b0 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 69 66  nificand */.  if
b38c0 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20  ( *z=='-' ){.   
b38d0 20 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20   sign = -1;.    
b38e0 7a 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  z++;.  }else if(
b38f0 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20   *z=='+' ){.    
b3900 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 2f 2a 20 73 6b  z++;.  }.  /* sk
b3910 69 70 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 65  ip leading zeroe
b3920 73 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 7a 5b  s */.  while( z[
b3930 30 5d 3d 3d 27 30 27 20 29 20 7a 2b 2b 2c 20 6e  0]=='0' ) z++, n
b3940 44 69 67 69 74 73 2b 2b 3b 0a 0a 20 20 2f 2a 20  Digits++;..  /* 
b3950 63 6f 70 79 20 6d 61 78 20 73 69 67 6e 69 66 69  copy max signifi
b3960 63 61 6e 74 20 64 69 67 69 74 73 20 74 6f 20 73  cant digits to s
b3970 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20  ignificand */.  
b3980 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73  while( sqlite3Is
b3990 64 69 67 69 74 28 2a 7a 29 20 26 26 20 73 3c 28  digit(*z) && s<(
b39a0 28 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 2d 39  (LARGEST_INT64-9
b39b0 29 2f 31 30 29 20 29 7b 0a 20 20 20 20 73 20 3d  )/10) ){.    s =
b39c0 20 73 2a 31 30 20 2b 20 28 2a 7a 20 2d 20 27 30   s*10 + (*z - '0
b39d0 27 29 3b 0a 20 20 20 20 7a 2b 2b 2c 20 6e 44 69  ');.    z++, nDi
b39e0 67 69 74 73 2b 2b 3b 0a 20 20 7d 0a 20 20 2f 2a  gits++;.  }.  /*
b39f0 20 73 6b 69 70 20 6e 6f 6e 2d 73 69 67 6e 69 66   skip non-signif
b3a00 69 63 61 6e 74 20 73 69 67 6e 69 66 69 63 61 6e  icant significan
b3a10 64 20 64 69 67 69 74 73 0a 20 20 2a 2a 20 28 69  d digits.  ** (i
b3a20 6e 63 72 65 61 73 65 20 65 78 70 6f 6e 65 6e 74  ncrease exponent
b3a30 20 62 79 20 64 20 74 6f 20 73 68 69 66 74 20 64   by d to shift d
b3a40 65 63 69 6d 61 6c 20 6c 65 66 74 29 20 2a 2f 0a  ecimal left) */.
b3a50 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33    while( sqlite3
b3a60 49 73 64 69 67 69 74 28 2a 7a 29 20 29 20 7a 2b  Isdigit(*z) ) z+
b3a70 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 2c 20 64 2b  +, nDigits++, d+
b3a80 2b 3b 0a 0a 20 20 2f 2a 20 69 66 20 64 65 63 69  +;..  /* if deci
b3a90 6d 61 6c 20 70 6f 69 6e 74 20 69 73 20 70 72 65  mal point is pre
b3aa0 73 65 6e 74 20 2a 2f 0a 20 20 69 66 28 20 2a 7a  sent */.  if( *z
b3ab0 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 7a 2b 2b  =='.' ){.    z++
b3ac0 3b 0a 20 20 20 20 2f 2a 20 63 6f 70 79 20 64 69  ;.    /* copy di
b3ad0 67 69 74 73 20 66 72 6f 6d 20 61 66 74 65 72 20  gits from after 
b3ae0 64 65 63 69 6d 61 6c 20 74 6f 20 73 69 67 6e 69  decimal to signi
b3af0 66 69 63 61 6e 64 0a 20 20 20 20 2a 2a 20 28 64  ficand.    ** (d
b3b00 65 63 72 65 61 73 65 20 65 78 70 6f 6e 65 6e 74  ecrease exponent
b3b10 20 62 79 20 64 20 74 6f 20 73 68 69 66 74 20 64   by d to shift d
b3b20 65 63 69 6d 61 6c 20 72 69 67 68 74 29 20 2a 2f  ecimal right) */
b3b30 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69  .    while( sqli
b3b40 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 26  te3Isdigit(*z) &
b3b50 26 20 73 3c 28 28 4c 41 52 47 45 53 54 5f 49 4e  & s<((LARGEST_IN
b3b60 54 36 34 2d 39 29 2f 31 30 29 20 29 7b 0a 20 20  T64-9)/10) ){.  
b3b70 20 20 20 20 73 20 3d 20 73 2a 31 30 20 2b 20 28      s = s*10 + (
b3b80 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 20  *z - '0');.     
b3b90 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 2c   z++, nDigits++,
b3ba0 20 64 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20   d--;.    }.    
b3bb0 2f 2a 20 73 6b 69 70 20 6e 6f 6e 2d 73 69 67 6e  /* skip non-sign
b3bc0 69 66 69 63 61 6e 74 20 64 69 67 69 74 73 20 2a  ificant digits *
b3bd0 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c  /.    while( sql
b3be0 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20  ite3Isdigit(*z) 
b3bf0 29 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b  ) z++, nDigits++
b3c00 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 65  ;.  }..  /* if e
b3c10 78 70 6f 6e 65 6e 74 20 69 73 20 70 72 65 73 65  xponent is prese
b3c20 6e 74 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 3d 3d  nt */.  if( *z==
b3c30 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27 20 29  'e' || *z=='E' )
b3c40 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 2f  {.    z++;.    /
b3c50 2a 20 67 65 74 20 73 69 67 6e 20 6f 66 20 65 78  * get sign of ex
b3c60 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 69 66  ponent */.    if
b3c70 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20 20 20  ( *z=='-' ){.   
b3c80 20 20 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20     esign = -1;. 
b3c90 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 65       z++;.    }e
b3ca0 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20  lse if( *z=='+' 
b3cb0 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20  ){.      z++;.  
b3cc0 20 20 7d 0a 20 20 20 20 2f 2a 20 63 6f 70 79 20    }.    /* copy 
b3cd0 64 69 67 69 74 73 20 74 6f 20 65 78 70 6f 6e 65  digits to expone
b3ce0 6e 74 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28  nt */.    while(
b3cf0 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28   sqlite3Isdigit(
b3d00 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20 65 20 3d  *z) ){.      e =
b3d10 20 65 2a 31 30 20 2b 20 28 2a 7a 20 2d 20 27 30   e*10 + (*z - '0
b3d20 27 29 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20  ');.      z++;. 
b3d30 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61     }.  }..  /* a
b3d40 64 6a 75 73 74 20 65 78 70 6f 6e 65 6e 74 20 62  djust exponent b
b3d50 79 20 64 2c 20 61 6e 64 20 75 70 64 61 74 65 20  y d, and update 
b3d60 73 69 67 6e 20 2a 2f 0a 20 20 65 20 3d 20 28 65  sign */.  e = (e
b3d70 2a 65 73 69 67 6e 29 20 2b 20 64 3b 0a 20 20 69  *esign) + d;.  i
b3d80 66 28 20 65 3c 30 20 29 20 7b 0a 20 20 20 20 65  f( e<0 ) {.    e
b3d90 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 65  sign = -1;.    e
b3da0 20 2a 3d 20 2d 31 3b 0a 20 20 7d 20 65 6c 73 65   *= -1;.  } else
b3db0 20 7b 0a 20 20 20 20 65 73 69 67 6e 20 3d 20 31   {.    esign = 1
b3dc0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 30  ;.  }..  /* if 0
b3dd0 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a   significand */.
b3de0 20 20 69 66 28 20 21 73 20 29 20 7b 0a 20 20 20    if( !s ) {.   
b3df0 20 2f 2a 20 49 6e 20 74 68 65 20 49 45 45 45 20   /* In the IEEE 
b3e00 37 35 34 20 73 74 61 6e 64 61 72 64 2c 20 7a 65  754 standard, ze
b3e10 72 6f 20 69 73 20 73 69 67 6e 65 64 2e 0a 20 20  ro is signed..  
b3e20 20 20 2a 2a 20 41 64 64 20 74 68 65 20 73 69 67    ** Add the sig
b3e30 6e 20 69 66 20 77 65 27 76 65 20 73 65 65 6e 20  n if we've seen 
b3e40 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 64 69 67  at least one dig
b3e50 69 74 20 2a 2f 0a 20 20 20 20 72 65 73 75 6c 74  it */.    result
b3e60 20 3d 20 28 73 69 67 6e 3c 30 20 26 26 20 6e 44   = (sign<0 && nD
b3e70 69 67 69 74 73 29 20 3f 20 2d 28 64 6f 75 62 6c  igits) ? -(doubl
b3e80 65 29 30 20 3a 20 28 64 6f 75 62 6c 65 29 30 3b  e)0 : (double)0;
b3e90 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
b3ea0 2f 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65  /* attempt to re
b3eb0 64 75 63 65 20 65 78 70 6f 6e 65 6e 74 20 2a 2f  duce exponent */
b3ec0 0a 20 20 20 20 69 66 28 20 65 73 69 67 6e 3e 30  .    if( esign>0
b3ed0 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28   ){.      while(
b3ee0 20 73 3c 28 4c 41 52 47 45 53 54 5f 49 4e 54 36   s<(LARGEST_INT6
b3ef0 34 2f 31 30 29 20 26 26 20 65 3e 30 20 29 20 65  4/10) && e>0 ) e
b3f00 2d 2d 2c 73 2a 3d 31 30 3b 0a 20 20 20 20 7d 65  --,s*=10;.    }e
b3f10 6c 73 65 7b 0a 20 20 20 20 20 20 77 68 69 6c 65  lse{.      while
b3f20 28 20 21 28 73 25 31 30 29 20 26 26 20 65 3e 30  ( !(s%10) && e>0
b3f30 20 29 20 65 2d 2d 2c 73 2f 3d 31 30 3b 0a 20 20   ) e--,s/=10;.  
b3f40 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 61 64 6a 75    }..    /* adju
b3f50 73 74 20 74 68 65 20 73 69 67 6e 20 6f 66 20 73  st the sign of s
b3f60 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20  ignificand */.  
b3f70 20 20 73 20 3d 20 73 69 67 6e 3c 30 20 3f 20 2d    s = sign<0 ? -
b3f80 73 20 3a 20 73 3b 0a 0a 20 20 20 20 2f 2a 20 69  s : s;..    /* i
b3f90 66 20 65 78 70 6f 6e 65 6e 74 2c 20 73 63 61 6c  f exponent, scal
b3fa0 65 20 73 69 67 6e 69 66 69 63 61 6e 64 20 61 73  e significand as
b3fb0 20 61 70 70 72 6f 70 72 69 61 74 65 0a 20 20 20   appropriate.   
b3fc0 20 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 69 6e   ** and store in
b3fd0 20 72 65 73 75 6c 74 2e 20 2a 2f 0a 20 20 20 20   result. */.    
b3fe0 69 66 28 20 65 20 29 7b 0a 20 20 20 20 20 20 64  if( e ){.      d
b3ff0 6f 75 62 6c 65 20 73 63 61 6c 65 20 3d 20 31 2e  ouble scale = 1.
b4000 30 3b 0a 20 20 20 20 20 20 2f 2a 20 61 74 74 65  0;.      /* atte
b4010 6d 70 74 20 74 6f 20 68 61 6e 64 6c 65 20 65 78  mpt to handle ex
b4020 74 72 65 6d 65 6c 79 20 73 6d 61 6c 6c 2f 6c 61  tremely small/la
b4030 72 67 65 20 6e 75 6d 62 65 72 73 20 62 65 74 74  rge numbers bett
b4040 65 72 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  er */.      if( 
b4050 65 3e 33 30 37 20 26 26 20 65 3c 33 34 32 20 29  e>307 && e<342 )
b4060 7b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  {.        while(
b4070 20 65 25 33 30 38 20 29 20 7b 20 73 63 61 6c 65   e%308 ) { scale
b4080 20 2a 3d 20 31 2e 30 65 2b 31 3b 20 65 20 2d 3d   *= 1.0e+1; e -=
b4090 20 31 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66   1; }.        if
b40a0 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a 20 20 20  ( esign<0 ){.   
b40b0 20 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20         result = 
b40c0 73 20 2f 20 73 63 61 6c 65 3b 0a 20 20 20 20 20  s / scale;.     
b40d0 20 20 20 20 20 72 65 73 75 6c 74 20 2f 3d 20 31       result /= 1
b40e0 2e 30 65 2b 33 30 38 3b 0a 20 20 20 20 20 20 20  .0e+308;.       
b40f0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
b4100 20 20 72 65 73 75 6c 74 20 3d 20 73 20 2a 20 73    result = s * s
b4110 63 61 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20  cale;.          
b4120 72 65 73 75 6c 74 20 2a 3d 20 31 2e 30 65 2b 33  result *= 1.0e+3
b4130 30 38 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  08;.        }.  
b4140 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
b4150 20 20 20 2f 2a 20 31 2e 30 65 2b 32 32 20 69 73     /* 1.0e+22 is
b4160 20 74 68 65 20 6c 61 72 67 65 73 74 20 70 6f 77   the largest pow
b4170 65 72 20 6f 66 20 31 30 20 74 68 61 6e 20 63 61  er of 10 than ca
b4180 6e 20 62 65 20 0a 20 20 20 20 20 20 20 20 2a 2a  n be .        **
b4190 20 72 65 70 72 65 73 65 6e 74 65 64 20 65 78 61   represented exa
b41a0 63 74 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20  ctly. */.       
b41b0 20 77 68 69 6c 65 28 20 65 25 32 32 20 29 20 7b   while( e%22 ) {
b41c0 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31   scale *= 1.0e+1
b41d0 3b 20 65 20 2d 3d 20 31 3b 20 7d 0a 20 20 20 20  ; e -= 1; }.    
b41e0 20 20 20 20 77 68 69 6c 65 28 20 65 3e 30 20 29      while( e>0 )
b41f0 20 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e 30 65   { scale *= 1.0e
b4200 2b 32 32 3b 20 65 20 2d 3d 20 32 32 3b 20 7d 0a  +22; e -= 22; }.
b4210 20 20 20 20 20 20 20 20 69 66 28 20 65 73 69 67          if( esig
b4220 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  n<0 ){.         
b4230 20 72 65 73 75 6c 74 20 3d 20 73 20 2f 20 73 63   result = s / sc
b4240 61 6c 65 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  ale;.        }el
b4250 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65  se{.          re
b4260 73 75 6c 74 20 3d 20 73 20 2a 20 73 63 61 6c 65  sult = s * scale
b4270 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
b4280 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b    }.    } else {
b4290 0a 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20  .      result = 
b42a0 28 64 6f 75 62 6c 65 29 73 3b 0a 20 20 20 20 7d  (double)s;.    }
b42b0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 73 74 6f 72 65  .  }..  /* store
b42c0 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20   the result */. 
b42d0 20 2a 70 52 65 73 75 6c 74 20 3d 20 72 65 73 75   *pResult = resu
b42e0 6c 74 3b 0a 0a 20 20 2f 2a 20 72 65 74 75 72 6e  lt;..  /* return
b42f0 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   number of chara
b4300 63 74 65 72 73 20 75 73 65 64 20 2a 2f 0a 20 20  cters used */.  
b4310 72 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 20 2d  return (int)(z -
b4320 20 7a 42 65 67 69 6e 29 3b 0a 23 65 6c 73 65 0a   zBegin);.#else.
b4330 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
b4340 41 74 6f 69 36 34 28 7a 2c 20 70 52 65 73 75 6c  Atoi64(z, pResul
b4350 74 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  t);.#endif /* SQ
b4360 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
b4370 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 7d 0a 0a 2f  NG_POINT */.}../
b4380 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65  *.** Compare the
b4390 20 31 39 2d 63 68 61 72 61 63 74 65 72 20 73 74   19-character st
b43a0 72 69 6e 67 20 7a 4e 75 6d 20 61 67 61 69 6e 73  ring zNum agains
b43b0 74 20 74 68 65 20 74 65 78 74 20 72 65 70 72 65  t the text repre
b43c0 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 76 61 6c  sentation.** val
b43d0 75 65 20 32 5e 36 33 3a 20 20 39 32 32 33 33 37  ue 2^63:  922337
b43e0 32 30 33 36 38 35 34 37 37 35 38 30 38 2e 20 20  2036854775808.  
b43f0 52 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65 2c  Return negative,
b4400 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73 69 74 69   zero, or positi
b4410 76 65 0a 2a 2a 20 69 66 20 7a 4e 75 6d 20 69 73  ve.** if zNum is
b4420 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61   less than, equa
b4430 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72  l to, or greater
b4440 20 74 68 61 6e 20 74 68 65 20 73 74 72 69 6e 67   than the string
b4450 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d  ..**.** Unlike m
b4460 65 6d 63 6d 70 28 29 20 74 68 69 73 20 72 6f 75  emcmp() this rou
b4470 74 69 6e 65 20 69 73 20 67 75 61 72 61 6e 74 65  tine is guarante
b4480 65 64 20 74 6f 20 72 65 74 75 72 6e 20 74 68 65  ed to return the
b4490 20 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a 20 69   difference.** i
b44a0 6e 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20  n the values of 
b44b0 74 68 65 20 6c 61 73 74 20 64 69 67 69 74 20 69  the last digit i
b44c0 66 20 74 68 65 20 6f 6e 6c 79 20 64 69 66 66 65  f the only diffe
b44d0 72 65 6e 63 65 20 69 73 20 69 6e 20 74 68 65 0a  rence is in the.
b44e0 2a 2a 20 6c 61 73 74 20 64 69 67 69 74 2e 20 20  ** last digit.  
b44f0 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c  So, for example,
b4500 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 63 6f 6d 70  .**.**      comp
b4510 61 72 65 32 70 6f 77 36 33 28 22 39 32 32 33 33  are2pow63("92233
b4520 37 32 30 33 36 38 35 34 37 37 35 38 30 30 22 29  72036854775800")
b4530 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75  .**.** will retu
b4540 72 6e 20 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63  rn -8..*/.static
b4550 20 69 6e 74 20 63 6f 6d 70 61 72 65 32 70 6f 77   int compare2pow
b4560 36 33 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  63(const char *z
b4570 4e 75 6d 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 20  Num){.  int c;. 
b4580 20 63 20 3d 20 6d 65 6d 63 6d 70 28 7a 4e 75 6d   c = memcmp(zNum
b4590 2c 22 39 32 32 33 33 37 32 30 33 36 38 35 34 37  ,"92233720368547
b45a0 37 35 38 30 22 2c 31 38 29 2a 31 30 3b 0a 20 20  7580",18)*10;.  
b45b0 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20  if( c==0 ){.    
b45c0 63 20 3d 20 7a 4e 75 6d 5b 31 38 5d 20 2d 20 27  c = zNum[18] - '
b45d0 38 27 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  8';.  }.  return
b45e0 20 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65   c;.}.../*.** Re
b45f0 74 75 72 6e 20 54 52 55 45 20 69 66 20 7a 4e 75  turn TRUE if zNu
b4600 6d 20 69 73 20 61 20 36 34 2d 62 69 74 20 73 69  m is a 64-bit si
b4610 67 6e 65 64 20 69 6e 74 65 67 65 72 20 61 6e 64  gned integer and
b4620 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 76 61   write.** the va
b4630 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67  lue of the integ
b4640 65 72 20 69 6e 74 6f 20 2a 70 4e 75 6d 2e 20 20  er into *pNum.  
b4650 49 66 20 7a 4e 75 6d 20 69 73 20 6e 6f 74 20 61  If zNum is not a
b4660 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 6f 72 20  n integer.** or 
b4670 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68  is an integer th
b4680 61 74 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20  at is too large 
b4690 74 6f 20 62 65 20 65 78 70 72 65 73 73 65 64 20  to be expressed 
b46a0 77 69 74 68 20 36 34 20 62 69 74 73 2c 0a 2a 2a  with 64 bits,.**
b46b0 20 74 68 65 6e 20 72 65 74 75 72 6e 20 66 61 6c   then return fal
b46c0 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74  se..**.** When t
b46d0 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20  his routine was 
b46e0 6f 72 69 67 69 6e 61 6c 6c 79 20 77 72 69 74 74  originally writt
b46f0 65 6e 20 69 74 20 64 65 61 6c 74 20 77 69 74 68  en it dealt with
b4700 20 6f 6e 6c 79 0a 2a 2a 20 33 32 2d 62 69 74 20   only.** 32-bit 
b4710 6e 75 6d 62 65 72 73 2e 20 20 41 74 20 74 68 61  numbers.  At tha
b4720 74 20 74 69 6d 65 2c 20 69 74 20 77 61 73 20 6d  t time, it was m
b4730 75 63 68 20 66 61 73 74 65 72 20 74 68 61 6e 20  uch faster than 
b4740 74 68 65 0a 2a 2a 20 61 74 6f 69 28 29 20 6c 69  the.** atoi() li
b4750 62 72 61 72 79 20 72 6f 75 74 69 6e 65 20 69 6e  brary routine in
b4760 20 52 65 64 48 61 74 20 37 2e 32 2e 0a 2a 2f 0a   RedHat 7.2..*/.
b4770 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
b4780 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34  nt sqlite3Atoi64
b4790 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75  (const char *zNu
b47a0 6d 2c 20 69 36 34 20 2a 70 4e 75 6d 29 7b 0a 20  m, i64 *pNum){. 
b47b0 20 69 36 34 20 76 20 3d 20 30 3b 0a 20 20 69 6e   i64 v = 0;.  in
b47c0 74 20 6e 65 67 3b 0a 20 20 69 6e 74 20 69 2c 20  t neg;.  int i, 
b47d0 63 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  c;.  const char 
b47e0 2a 7a 53 74 61 72 74 3b 0a 20 20 77 68 69 6c 65  *zStart;.  while
b47f0 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65  ( sqlite3Isspace
b4800 28 2a 7a 4e 75 6d 29 20 29 20 7a 4e 75 6d 2b 2b  (*zNum) ) zNum++
b4810 3b 0a 20 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27  ;.  if( *zNum=='
b4820 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20  -' ){.    neg = 
b4830 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20  1;.    zNum++;. 
b4840 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 4e 75 6d   }else if( *zNum
b4850 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 6e 65 67  =='+' ){.    neg
b4860 20 3d 20 30 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b   = 0;.    zNum++
b4870 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e  ;.  }else{.    n
b4880 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 7a 53  eg = 0;.  }.  zS
b4890 74 61 72 74 20 3d 20 7a 4e 75 6d 3b 0a 20 20 77  tart = zNum;.  w
b48a0 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27  hile( zNum[0]=='
b48b0 30 27 20 29 7b 20 7a 4e 75 6d 2b 2b 3b 20 7d 20  0' ){ zNum++; } 
b48c0 2f 2a 20 53 6b 69 70 20 6f 76 65 72 20 6c 65 61  /* Skip over lea
b48d0 64 69 6e 67 20 7a 65 72 6f 73 2e 20 54 69 63 6b  ding zeros. Tick
b48e0 65 74 20 23 32 34 35 34 20 2a 2f 0a 20 20 66 6f  et #2454 */.  fo
b48f0 72 28 69 3d 30 3b 20 28 63 3d 7a 4e 75 6d 5b 69  r(i=0; (c=zNum[i
b4900 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39  ])>='0' && c<='9
b4910 27 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d  '; i++){.    v =
b4920 20 76 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b   v*10 + c - '0';
b4930 0a 20 20 7d 0a 20 20 2a 70 4e 75 6d 20 3d 20 6e  .  }.  *pNum = n
b4940 65 67 20 3f 20 2d 76 20 3a 20 76 3b 0a 20 20 69  eg ? -v : v;.  i
b4950 66 28 20 63 21 3d 30 20 7c 7c 20 28 69 3d 3d 30  f( c!=0 || (i==0
b4960 20 26 26 20 7a 53 74 61 72 74 3d 3d 7a 4e 75 6d   && zStart==zNum
b4970 29 20 7c 7c 20 69 3e 31 39 20 29 7b 0a 20 20 20  ) || i>19 ){.   
b4980 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 65 6d 70 74   /* zNum is empt
b4990 79 20 6f 72 20 63 6f 6e 74 61 69 6e 73 20 6e 6f  y or contains no
b49a0 6e 2d 6e 75 6d 65 72 69 63 20 74 65 78 74 20 6f  n-numeric text o
b49b0 72 20 69 73 20 6c 6f 6e 67 65 72 0a 20 20 20 20  r is longer.    
b49c0 2a 2a 20 74 68 61 6e 20 31 39 20 64 69 67 69 74  ** than 19 digit
b49d0 73 20 28 74 68 75 73 20 67 75 61 72 61 6e 74 69  s (thus guaranti
b49e0 6e 67 20 74 68 61 74 20 69 74 20 69 73 20 74 6f  ng that it is to
b49f0 6f 20 6c 61 72 67 65 29 20 2a 2f 0a 20 20 20 20  o large) */.    
b4a00 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73  return 0;.  }els
b4a10 65 20 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 20  e if( i<19 ){.  
b4a20 20 20 2f 2a 20 4c 65 73 73 20 74 68 61 6e 20 31    /* Less than 1
b4a30 39 20 64 69 67 69 74 73 2c 20 73 6f 20 77 65 20  9 digits, so we 
b4a40 6b 6e 6f 77 20 74 68 61 74 20 69 74 20 66 69 74  know that it fit
b4a50 73 20 69 6e 20 36 34 20 62 69 74 73 20 2a 2f 0a  s in 64 bits */.
b4a60 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
b4a70 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 31 39  }else{.    /* 19
b4a80 2d 64 69 67 69 74 20 6e 75 6d 62 65 72 73 20 6d  -digit numbers m
b4a90 75 73 74 20 62 65 20 6e 6f 20 6c 61 72 67 65 72  ust be no larger
b4aa0 20 74 68 61 6e 20 39 32 32 33 33 37 32 30 33 36   than 9223372036
b4ab0 38 35 34 37 37 35 38 30 37 20 69 66 20 70 6f 73  854775807 if pos
b4ac0 69 74 69 76 65 0a 20 20 20 20 2a 2a 20 6f 72 20  itive.    ** or 
b4ad0 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35  9223372036854775
b4ae0 38 30 38 20 69 66 20 6e 65 67 61 74 69 76 65 2e  808 if negative.
b4af0 20 20 4e 6f 74 65 20 74 68 61 74 20 39 32 32 33    Note that 9223
b4b00 33 37 32 30 33 36 38 35 34 36 36 35 38 30 38 0a  372036854665808.
b4b10 20 20 20 20 2a 2a 20 69 73 20 32 5e 36 33 2e 20      ** is 2^63. 
b4b20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 63 6f  */.    return co
b4b30 6d 70 61 72 65 32 70 6f 77 36 33 28 7a 4e 75 6d  mpare2pow63(zNum
b4b40 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  )<neg;.  }.}../*
b4b50 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 7a  .** The string z
b4b60 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61  Num represents a
b4b70 6e 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67  n unsigned integ
b4b80 65 72 2e 20 20 54 68 65 20 7a 4e 75 6d 20 73 74  er.  The zNum st
b4b90 72 69 6e 67 0a 2a 2a 20 63 6f 6e 73 69 73 74 73  ring.** consists
b4ba0 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20   of one or more 
b4bb0 64 69 67 69 74 20 63 68 61 72 61 63 74 65 72 73  digit characters
b4bc0 20 61 6e 64 20 69 73 20 74 65 72 6d 69 6e 61 74   and is terminat
b4bd0 65 64 20 62 79 0a 2a 2a 20 61 20 7a 65 72 6f 20  ed by.** a zero 
b4be0 63 68 61 72 61 63 74 65 72 2e 20 20 41 6e 79 20  character.  Any 
b4bf0 73 74 72 61 79 20 63 68 61 72 61 63 74 65 72 73  stray characters
b4c00 20 69 6e 20 7a 4e 75 6d 20 72 65 73 75 6c 74 20   in zNum result 
b4c10 69 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20  in undefined.** 
b4c20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20  behavior..**.** 
b4c30 49 66 20 74 68 65 20 75 6e 73 69 67 6e 65 64 20  If the unsigned 
b4c40 69 6e 74 65 67 65 72 20 74 68 61 74 20 7a 4e 75  integer that zNu
b4c50 6d 20 72 65 70 72 65 73 65 6e 74 73 20 77 69 6c  m represents wil
b4c60 6c 20 66 69 74 20 69 6e 20 61 0a 2a 2a 20 36 34  l fit in a.** 64
b4c70 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65  -bit signed inte
b4c80 67 65 72 2c 20 72 65 74 75 72 6e 20 54 52 55 45  ger, return TRUE
b4c90 2e 20 20 4f 74 68 65 72 77 69 73 65 20 72 65 74  .  Otherwise ret
b4ca0 75 72 6e 20 46 41 4c 53 45 2e 0a 2a 2a 0a 2a 2a  urn FALSE..**.**
b4cb0 20 49 66 20 74 68 65 20 6e 65 67 46 6c 61 67 20   If the negFlag 
b4cc0 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 72 75  parameter is tru
b4cd0 65 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  e, that means th
b4ce0 61 74 20 7a 4e 75 6d 20 72 65 61 6c 6c 79 20 72  at zNum really r
b4cf0 65 70 72 65 73 65 6e 74 73 0a 2a 2a 20 61 20 6e  epresents.** a n
b4d00 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 2e 20  egative number. 
b4d10 20 28 54 68 65 20 6c 65 61 64 69 6e 67 20 22 2d   (The leading "-
b4d20 22 20 69 73 20 6f 6d 69 74 74 65 64 20 66 72 6f  " is omitted fro
b4d30 6d 20 7a 4e 75 6d 2e 29 20 20 54 68 69 73 0a 2a  m zNum.)  This.*
b4d40 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e  * parameter is n
b4d50 65 65 64 65 64 20 74 6f 20 64 65 74 65 72 6d 69  eeded to determi
b4d60 6e 65 20 61 20 62 6f 75 6e 64 61 72 79 20 63 61  ne a boundary ca
b4d70 73 65 2e 20 20 41 20 73 74 72 69 6e 67 0a 2a 2a  se.  A string.**
b4d80 20 6f 66 20 22 39 32 32 33 33 37 33 30 33 36 38   of "92233730368
b4d90 35 34 37 37 35 38 30 38 22 20 72 65 74 75 72 6e  54775808" return
b4da0 73 20 66 61 6c 73 65 20 69 66 20 6e 65 67 46 6c  s false if negFl
b4db0 61 67 20 69 73 20 66 61 6c 73 65 20 6f 72 20 74  ag is false or t
b4dc0 72 75 65 0a 2a 2a 20 69 66 20 6e 65 67 46 6c 61  rue.** if negFla
b4dd0 67 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a  g is true..**.**
b4de0 20 4c 65 61 64 69 6e 67 20 7a 65 72 6f 73 20 61   Leading zeros a
b4df0 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53  re ignored..*/.S
b4e00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
b4e10 74 20 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36  t sqlite3FitsIn6
b4e20 34 42 69 74 73 28 63 6f 6e 73 74 20 63 68 61 72  4Bits(const char
b4e30 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20 6e 65 67 46   *zNum, int negF
b4e40 6c 61 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  lag){.  int i;. 
b4e50 20 69 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 0a 20   int neg = 0;.. 
b4e60 20 61 73 73 65 72 74 28 20 7a 4e 75 6d 5b 30 5d   assert( zNum[0]
b4e70 3e 3d 27 30 27 20 26 26 20 7a 4e 75 6d 5b 30 5d  >='0' && zNum[0]
b4e80 3c 3d 27 39 27 20 29 3b 20 2f 2a 20 7a 4e 75 6d  <='9' ); /* zNum
b4e90 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20   is an unsigned 
b4ea0 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20 20 69 66 28  number */..  if(
b4eb0 20 6e 65 67 46 6c 61 67 20 29 20 6e 65 67 20 3d   negFlag ) neg =
b4ec0 20 31 2d 6e 65 67 3b 0a 20 20 77 68 69 6c 65 28   1-neg;.  while(
b4ed0 20 2a 7a 4e 75 6d 3d 3d 27 30 27 20 29 7b 0a 20   *zNum=='0' ){. 
b4ee0 20 20 20 7a 4e 75 6d 2b 2b 3b 20 20 20 2f 2a 20     zNum++;   /* 
b4ef0 53 6b 69 70 20 6c 65 61 64 69 6e 67 20 7a 65 72  Skip leading zer
b4f00 6f 73 2e 20 20 54 69 63 6b 65 74 20 23 32 34 35  os.  Ticket #245
b4f10 34 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 69  4 */.  }.  for(i
b4f20 3d 30 3b 20 7a 4e 75 6d 5b 69 5d 3b 20 69 2b 2b  =0; zNum[i]; i++
b4f30 29 7b 20 61 73 73 65 72 74 28 20 7a 4e 75 6d 5b  ){ assert( zNum[
b4f40 69 5d 3e 3d 27 30 27 20 26 26 20 7a 4e 75 6d 5b  i]>='0' && zNum[
b4f50 69 5d 3c 3d 27 39 27 20 29 3b 20 7d 0a 20 20 69  i]<='9' ); }.  i
b4f60 66 28 20 69 3c 31 39 20 29 7b 0a 20 20 20 20 2f  f( i<19 ){.    /
b4f70 2a 20 47 75 61 72 61 6e 74 65 65 64 20 74 6f 20  * Guaranteed to 
b4f80 66 69 74 20 69 66 20 6c 65 73 73 20 74 68 61 6e  fit if less than
b4f90 20 31 39 20 64 69 67 69 74 73 20 2a 2f 0a 20 20   19 digits */.  
b4fa0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65    return 1;.  }e
b4fb0 6c 73 65 20 69 66 28 20 69 3e 31 39 20 29 7b 0a  lse if( i>19 ){.
b4fc0 20 20 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 65      /* Guarantee
b4fd0 64 20 74 6f 20 62 65 20 74 6f 6f 20 62 69 67 20  d to be too big 
b4fe0 69 66 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  if greater than 
b4ff0 31 39 20 64 69 67 69 74 73 20 2a 2f 0a 20 20 20  19 digits */.   
b5000 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c   return 0;.  }el
b5010 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 61  se{.    /* Compa
b5020 72 65 20 61 67 61 69 6e 73 74 20 32 5e 36 33 2e  re against 2^63.
b5030 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 63   */.    return c
b5040 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 7a 4e 75  ompare2pow63(zNu
b5050 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f  m)<neg;.  }.}../
b5060 2a 0a 2a 2a 20 49 66 20 7a 4e 75 6d 20 72 65 70  *.** If zNum rep
b5070 72 65 73 65 6e 74 73 20 61 6e 20 69 6e 74 65 67  resents an integ
b5080 65 72 20 74 68 61 74 20 77 69 6c 6c 20 66 69 74  er that will fit
b5090 20 69 6e 20 33 32 2d 62 69 74 73 2c 20 74 68 65   in 32-bits, the
b50a0 6e 20 73 65 74 0a 2a 2a 20 2a 70 56 61 6c 75 65  n set.** *pValue
b50b0 20 74 6f 20 74 68 61 74 20 69 6e 74 65 67 65 72   to that integer
b50c0 20 61 6e 64 20 72 65 74 75 72 6e 20 74 72 75 65   and return true
b50d0 2e 20 20 4f 74 68 65 72 77 69 73 65 20 72 65 74  .  Otherwise ret
b50e0 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a  urn false..**.**
b50f0 20 41 6e 79 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63   Any non-numeric
b5100 20 63 68 61 72 61 63 74 65 72 73 20 74 68 61 74   characters that
b5110 20 66 6f 6c 6c 6f 77 69 6e 67 20 7a 4e 75 6d 20   following zNum 
b5120 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20  are ignored..** 
b5130 54 68 69 73 20 69 73 20 64 69 66 66 65 72 65 6e  This is differen
b5140 74 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 41 74  t from sqlite3At
b5150 6f 69 36 34 28 29 20 77 68 69 63 68 20 72 65 71  oi64() which req
b5160 75 69 72 65 73 20 74 68 65 0a 2a 2a 20 69 6e 70  uires the.** inp
b5170 75 74 20 6e 75 6d 62 65 72 20 74 6f 20 62 65 20  ut number to be 
b5180 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e  zero-terminated.
b5190 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b51a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65  TE int sqlite3Ge
b51b0 74 49 6e 74 33 32 28 63 6f 6e 73 74 20 63 68 61  tInt32(const cha
b51c0 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20 2a 70 56  r *zNum, int *pV
b51d0 61 6c 75 65 29 7b 0a 20 20 73 71 6c 69 74 65 5f  alue){.  sqlite_
b51e0 69 6e 74 36 34 20 76 20 3d 20 30 3b 0a 20 20 69  int64 v = 0;.  i
b51f0 6e 74 20 69 2c 20 63 3b 0a 20 20 69 6e 74 20 6e  nt i, c;.  int n
b5200 65 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 7a 4e  eg = 0;.  if( zN
b5210 75 6d 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20  um[0]=='-' ){.  
b5220 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 20 20 7a    neg = 1;.    z
b5230 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 69  Num++;.  }else i
b5240 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2b 27 20  f( zNum[0]=='+' 
b5250 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20  ){.    zNum++;. 
b5260 20 7d 0a 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d   }.  while( zNum
b5270 5b 30 5d 3d 3d 27 30 27 20 29 20 7a 4e 75 6d 2b  [0]=='0' ) zNum+
b5280 2b 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  +;.  for(i=0; i<
b5290 31 31 20 26 26 20 28 63 20 3d 20 7a 4e 75 6d 5b  11 && (c = zNum[
b52a0 69 5d 20 2d 20 27 30 27 29 3e 3d 30 20 26 26 20  i] - '0')>=0 && 
b52b0 63 3c 3d 39 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  c<=9; i++){.    
b52c0 76 20 3d 20 76 2a 31 30 20 2b 20 63 3b 0a 20 20  v = v*10 + c;.  
b52d0 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 6c 6f 6e 67  }..  /* The long
b52e0 65 73 74 20 64 65 63 69 6d 61 6c 20 72 65 70 72  est decimal repr
b52f0 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20  esentation of a 
b5300 33 32 20 62 69 74 20 69 6e 74 65 67 65 72 20 69  32 bit integer i
b5310 73 20 31 30 20 64 69 67 69 74 73 3a 0a 20 20 2a  s 10 digits:.  *
b5320 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20  *.  **          
b5330 20 20 20 31 32 33 34 35 36 37 38 39 30 0a 20 20     1234567890.  
b5340 2a 2a 20 20 20 20 20 32 5e 33 31 20 2d 3e 20 32  **     2^31 -> 2
b5350 31 34 37 34 38 33 36 34 38 0a 20 20 2a 2f 0a 20  147483648.  */. 
b5360 20 69 66 28 20 69 3e 31 30 20 29 7b 0a 20 20 20   if( i>10 ){.   
b5370 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
b5380 20 69 66 28 20 76 2d 6e 65 67 3e 32 31 34 37 34   if( v-neg>21474
b5390 38 33 36 34 37 20 29 7b 0a 20 20 20 20 72 65 74  83647 ){.    ret
b53a0 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  urn 0;.  }.  if(
b53b0 20 6e 65 67 20 29 7b 0a 20 20 20 20 76 20 3d 20   neg ){.    v = 
b53c0 2d 76 3b 0a 20 20 7d 0a 20 20 2a 70 56 61 6c 75  -v;.  }.  *pValu
b53d0 65 20 3d 20 28 69 6e 74 29 76 3b 0a 20 20 72 65  e = (int)v;.  re
b53e0 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 1;.}../*.**
b53f0 20 54 68 65 20 76 61 72 69 61 62 6c 65 2d 6c 65   The variable-le
b5400 6e 67 74 68 20 69 6e 74 65 67 65 72 20 65 6e 63  ngth integer enc
b5410 6f 64 69 6e 67 20 69 73 20 61 73 20 66 6f 6c 6c  oding is as foll
b5420 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b 45 59 3a 0a  ows:.**.** KEY:.
b5430 2a 2a 20 20 20 20 20 20 20 20 20 41 20 3d 20 30  **         A = 0
b5440 78 78 78 78 78 78 78 20 20 20 20 37 20 62 69 74  xxxxxxx    7 bit
b5450 73 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e  s of data and on
b5460 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20  e flag bit.**   
b5470 20 20 20 20 20 20 42 20 3d 20 31 78 78 78 78 78        B = 1xxxxx
b5480 78 78 20 20 20 20 37 20 62 69 74 73 20 6f 66 20  xx    7 bits of 
b5490 64 61 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61  data and one fla
b54a0 67 20 62 69 74 0a 2a 2a 20 20 20 20 20 20 20 20  g bit.**        
b54b0 20 43 20 3d 20 78 78 78 78 78 78 78 78 20 20 20   C = xxxxxxxx   
b54c0 20 38 20 62 69 74 73 20 6f 66 20 64 61 74 61 0a   8 bits of data.
b54d0 2a 2a 0a 2a 2a 20 20 37 20 62 69 74 73 20 2d 20  **.**  7 bits - 
b54e0 41 0a 2a 2a 20 31 34 20 62 69 74 73 20 2d 20 42  A.** 14 bits - B
b54f0 41 0a 2a 2a 20 32 31 20 62 69 74 73 20 2d 20 42  A.** 21 bits - B
b5500 42 41 0a 2a 2a 20 32 38 20 62 69 74 73 20 2d 20  BA.** 28 bits - 
b5510 42 42 42 41 0a 2a 2a 20 33 35 20 62 69 74 73 20  BBBA.** 35 bits 
b5520 2d 20 42 42 42 42 41 0a 2a 2a 20 34 32 20 62 69  - BBBBA.** 42 bi
b5530 74 73 20 2d 20 42 42 42 42 42 41 0a 2a 2a 20 34  ts - BBBBBA.** 4
b5540 39 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 41  9 bits - BBBBBBA
b5550 0a 2a 2a 20 35 36 20 62 69 74 73 20 2d 20 42 42  .** 56 bits - BB
b5560 42 42 42 42 42 41 0a 2a 2a 20 36 34 20 62 69 74  BBBBBA.** 64 bit
b5570 73 20 2d 20 42 42 42 42 42 42 42 42 43 0a 2a 2f  s - BBBBBBBBC.*/
b5580 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20  ../*.** Write a 
b5590 36 34 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d  64-bit variable-
b55a0 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 74  length integer t
b55b0 6f 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e  o memory startin
b55c0 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 54 68  g at p[0]..** Th
b55d0 65 20 6c 65 6e 67 74 68 20 6f 66 20 64 61 74 61  e length of data
b55e0 20 77 72 69 74 65 20 77 69 6c 6c 20 62 65 20 62   write will be b
b55f0 65 74 77 65 65 6e 20 31 20 61 6e 64 20 39 20 62  etween 1 and 9 b
b5600 79 74 65 73 2e 20 20 54 68 65 20 6e 75 6d 62 65  ytes.  The numbe
b5610 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 77 72  r.** of bytes wr
b5620 69 74 74 65 6e 20 69 73 20 72 65 74 75 72 6e 65  itten is returne
b5630 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61 72 69 61  d..**.** A varia
b5640 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67  ble-length integ
b5650 65 72 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74  er consists of t
b5660 68 65 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20  he lower 7 bits 
b5670 6f 66 20 65 61 63 68 20 62 79 74 65 0a 2a 2a 20  of each byte.** 
b5680 66 6f 72 20 61 6c 6c 20 62 79 74 65 73 20 74 68  for all bytes th
b5690 61 74 20 68 61 76 65 20 74 68 65 20 38 74 68 20  at have the 8th 
b56a0 62 69 74 20 73 65 74 20 61 6e 64 20 6f 6e 65 20  bit set and one 
b56b0 62 79 74 65 20 77 69 74 68 20 74 68 65 20 38 74  byte with the 8t
b56c0 68 0a 2a 2a 20 62 69 74 20 63 6c 65 61 72 2e 20  h.** bit clear. 
b56d0 20 45 78 63 65 70 74 2c 20 69 66 20 77 65 20 67   Except, if we g
b56e0 65 74 20 74 6f 20 74 68 65 20 39 74 68 20 62 79  et to the 9th by
b56f0 74 65 2c 20 69 74 20 73 74 6f 72 65 73 20 74 68  te, it stores th
b5700 65 20 66 75 6c 6c 0a 2a 2a 20 38 20 62 69 74 73  e full.** 8 bits
b5710 20 61 6e 64 20 69 73 20 74 68 65 20 6c 61 73 74   and is the last
b5720 20 62 79 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   byte..*/.SQLITE
b5730 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
b5740 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 75 6e  ite3PutVarint(un
b5750 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20  signed char *p, 
b5760 75 36 34 20 76 29 7b 0a 20 20 69 6e 74 20 69 2c  u64 v){.  int i,
b5770 20 6a 2c 20 6e 3b 0a 20 20 75 38 20 62 75 66 5b   j, n;.  u8 buf[
b5780 31 30 5d 3b 0a 20 20 69 66 28 20 76 20 26 20 28  10];.  if( v & (
b5790 28 28 75 36 34 29 30 78 66 66 30 30 30 30 30 30  ((u64)0xff000000
b57a0 29 3c 3c 33 32 29 20 29 7b 0a 20 20 20 20 70 5b  )<<32) ){.    p[
b57b0 38 5d 20 3d 20 28 75 38 29 76 3b 0a 20 20 20 20  8] = (u8)v;.    
b57c0 76 20 3e 3e 3d 20 38 3b 0a 20 20 20 20 66 6f 72  v >>= 8;.    for
b57d0 28 69 3d 37 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29  (i=7; i>=0; i--)
b57e0 7b 0a 20 20 20 20 20 20 70 5b 69 5d 20 3d 20 28  {.      p[i] = (
b57f0 75 38 29 28 28 76 20 26 20 30 78 37 66 29 20 7c  u8)((v & 0x7f) |
b5800 20 30 78 38 30 29 3b 0a 20 20 20 20 20 20 76 20   0x80);.      v 
b5810 3e 3e 3d 20 37 3b 0a 20 20 20 20 7d 0a 20 20 20  >>= 7;.    }.   
b5820 20 72 65 74 75 72 6e 20 39 3b 0a 20 20 7d 20 20   return 9;.  }  
b5830 20 20 0a 20 20 6e 20 3d 20 30 3b 0a 20 20 64 6f    .  n = 0;.  do
b5840 7b 0a 20 20 20 20 62 75 66 5b 6e 2b 2b 5d 20 3d  {.    buf[n++] =
b5850 20 28 75 38 29 28 28 76 20 26 20 30 78 37 66 29   (u8)((v & 0x7f)
b5860 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 76 20   | 0x80);.    v 
b5870 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28  >>= 7;.  }while(
b5880 20 76 21 3d 30 20 29 3b 0a 20 20 62 75 66 5b 30   v!=0 );.  buf[0
b5890 5d 20 26 3d 20 30 78 37 66 3b 0a 20 20 61 73 73  ] &= 0x7f;.  ass
b58a0 65 72 74 28 20 6e 3c 3d 39 20 29 3b 0a 20 20 66  ert( n<=9 );.  f
b58b0 6f 72 28 69 3d 30 2c 20 6a 3d 6e 2d 31 3b 20 6a  or(i=0, j=n-1; j
b58c0 3e 3d 30 3b 20 6a 2d 2d 2c 20 69 2b 2b 29 7b 0a  >=0; j--, i++){.
b58d0 20 20 20 20 70 5b 69 5d 20 3d 20 62 75 66 5b 6a      p[i] = buf[j
b58e0 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  ];.  }.  return 
b58f0 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  n;.}../*.** This
b5900 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 66 61   routine is a fa
b5910 73 74 65 72 20 76 65 72 73 69 6f 6e 20 6f 66 20  ster version of 
b5920 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74  sqlite3PutVarint
b5930 28 29 20 74 68 61 74 20 6f 6e 6c 79 0a 2a 2a 20  () that only.** 
b5940 77 6f 72 6b 73 20 66 6f 72 20 33 32 2d 62 69 74  works for 32-bit
b5950 20 70 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65   positive intege
b5960 72 73 20 61 6e 64 20 77 68 69 63 68 20 69 73 20  rs and which is 
b5970 6f 70 74 69 6d 69 7a 65 64 20 66 6f 72 0a 2a 2a  optimized for.**
b5980 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65   the common case
b5990 20 6f 66 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65   of small intege
b59a0 72 73 2e 20 20 41 20 4d 41 43 52 4f 20 76 65 72  rs.  A MACRO ver
b59b0 73 69 6f 6e 2c 20 70 75 74 56 61 72 69 6e 74 33  sion, putVarint3
b59c0 32 2c 0a 2a 2a 20 69 73 20 70 72 6f 76 69 64 65  2,.** is provide
b59d0 64 20 77 68 69 63 68 20 69 6e 6c 69 6e 65 73 20  d which inlines 
b59e0 74 68 65 20 73 69 6e 67 6c 65 2d 62 79 74 65 20  the single-byte 
b59f0 63 61 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20  case.  All code 
b5a00 73 68 6f 75 6c 64 20 75 73 65 0a 2a 2a 20 74 68  should use.** th
b5a10 65 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 20  e MACRO version 
b5a20 61 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  as this function
b5a30 20 61 73 73 75 6d 65 73 20 74 68 65 20 73 69 6e   assumes the sin
b5a40 67 6c 65 2d 62 79 74 65 20 63 61 73 65 20 68 61  gle-byte case ha
b5a50 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65  s.** already bee
b5a60 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51  n handled..*/.SQ
b5a70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
b5a80 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e   sqlite3PutVarin
b5a90 74 33 32 28 75 6e 73 69 67 6e 65 64 20 63 68 61  t32(unsigned cha
b5aa0 72 20 2a 70 2c 20 75 33 32 20 76 29 7b 0a 23 69  r *p, u32 v){.#i
b5ab0 66 6e 64 65 66 20 70 75 74 56 61 72 69 6e 74 33  fndef putVarint3
b5ac0 32 0a 20 20 69 66 28 20 28 76 20 26 20 7e 30 78  2.  if( (v & ~0x
b5ad0 37 66 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 5b  7f)==0 ){.    p[
b5ae0 30 5d 20 3d 20 76 3b 0a 20 20 20 20 72 65 74 75  0] = v;.    retu
b5af0 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  rn 1;.  }.#endif
b5b00 0a 20 20 69 66 28 20 28 76 20 26 20 7e 30 78 33  .  if( (v & ~0x3
b5b10 66 66 66 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70  fff)==0 ){.    p
b5b20 5b 30 5d 20 3d 20 28 75 38 29 28 28 76 3e 3e 37  [0] = (u8)((v>>7
b5b30 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 70  ) | 0x80);.    p
b5b40 5b 31 5d 20 3d 20 28 75 38 29 28 76 20 26 20 30  [1] = (u8)(v & 0
b5b50 78 37 66 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  x7f);.    return
b5b60 20 32 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   2;.  }.  return
b5b70 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e   sqlite3PutVarin
b5b80 74 28 70 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  t(p, v);.}../*.*
b5b90 2a 20 52 65 61 64 20 61 20 36 34 2d 62 69 74 20  * Read a 64-bit 
b5ba0 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20  variable-length 
b5bb0 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d 65 6d  integer from mem
b5bc0 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20  ory starting at 
b5bd0 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  p[0]..** Return 
b5be0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
b5bf0 74 65 73 20 72 65 61 64 2e 20 20 54 68 65 20 76  tes read.  The v
b5c00 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 69  alue is stored i
b5c10 6e 20 2a 76 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  n *v..*/.SQLITE_
b5c20 50 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74  PRIVATE u8 sqlit
b5c30 65 33 47 65 74 56 61 72 69 6e 74 28 63 6f 6e 73  e3GetVarint(cons
b5c40 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
b5c50 2a 70 2c 20 75 36 34 20 2a 76 29 7b 0a 20 20 75  *p, u64 *v){.  u
b5c60 33 32 20 61 2c 62 2c 73 3b 0a 0a 20 20 61 20 3d  32 a,b,s;..  a =
b5c70 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 20   *p;.  /* a: p0 
b5c80 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20  (unmasked) */.  
b5c90 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20  if (!(a&0x80)). 
b5ca0 20 7b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20   {.    *v = a;. 
b5cb0 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
b5cc0 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a  ..  p++;.  b = *
b5cd0 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 20 28 75  p;.  /* b: p1 (u
b5ce0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66  nmasked) */.  if
b5cf0 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b   (!(b&0x80)).  {
b5d00 0a 20 20 20 20 61 20 26 3d 20 30 78 37 66 3b 0a  .    a &= 0x7f;.
b5d10 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20      a = a<<7;.  
b5d20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76    a |= b;.    *v
b5d30 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e   = a;.    return
b5d40 20 32 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a   2;.  }..  p++;.
b5d50 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61    a = a<<14;.  a
b5d60 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20   |= *p;.  /* a: 
b5d70 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 75 6e 6d  p0<<14 | p2 (unm
b5d80 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28  asked) */.  if (
b5d90 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20  !(a&0x80)).  {. 
b5da0 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31     a &= (0x7f<<1
b5db0 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62  4)|(0x7f);.    b
b5dc0 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 20   &= 0x7f;.    b 
b5dd0 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d  = b<<7;.    a |=
b5de0 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a   b;.    *v = a;.
b5df0 20 20 20 20 72 65 74 75 72 6e 20 33 3b 0a 20 20      return 3;.  
b5e00 7d 0a 0a 20 20 2f 2a 20 43 53 45 31 20 66 72 6f  }..  /* CSE1 fro
b5e10 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 20 26  m below */.  a &
b5e20 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  = (0x7f<<14)|(0x
b5e30 37 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20 20 62 20  7f);.  p++;.  b 
b5e40 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20  = b<<14;.  b |= 
b5e50 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c  *p;.  /* b: p1<<
b5e60 31 34 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65  14 | p3 (unmaske
b5e70 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26  d) */.  if (!(b&
b5e80 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 62  0x80)).  {.    b
b5e90 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28   &= (0x7f<<14)|(
b5ea0 30 78 37 66 29 3b 0a 20 20 20 20 2f 2a 20 6d 6f  0x7f);.    /* mo
b5eb0 76 65 64 20 43 53 45 31 20 75 70 20 2a 2f 0a 20  ved CSE1 up */. 
b5ec0 20 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66     /* a &= (0x7f
b5ed0 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f  <<14)|(0x7f); */
b5ee0 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20  .    a = a<<7;. 
b5ef0 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a     a |= b;.    *
b5f00 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72  v = a;.    retur
b5f10 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61  n 4;.  }..  /* a
b5f20 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 6d  : p0<<14 | p2 (m
b5f30 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 62  asked) */.  /* b
b5f40 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75  : p1<<14 | p3 (u
b5f50 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a  nmasked) */.  /*
b5f60 20 31 3a 73 61 76 65 20 6f 66 66 20 70 30 3c 3c   1:save off p0<<
b5f70 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32  21 | p1<<14 | p2
b5f80 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64  <<7 | p3 (masked
b5f90 29 20 2a 2f 0a 20 20 2f 2a 20 6d 6f 76 65 64 20  ) */.  /* moved 
b5fa0 43 53 45 31 20 75 70 20 2a 2f 0a 20 20 2f 2a 20  CSE1 up */.  /* 
b5fb0 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c  a &= (0x7f<<14)|
b5fc0 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 62 20 26  (0x7f); */.  b &
b5fd0 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  = (0x7f<<14)|(0x
b5fe0 37 66 29 3b 0a 20 20 73 20 3d 20 61 3b 0a 20 20  7f);.  s = a;.  
b5ff0 2f 2a 20 73 3a 20 70 30 3c 3c 31 34 20 7c 20 70  /* s: p0<<14 | p
b6000 32 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20  2 (masked) */.. 
b6010 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31   p++;.  a = a<<1
b6020 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20  4;.  a |= *p;.  
b6030 2f 2a 20 61 3a 20 70 30 3c 3c 32 38 20 7c 20 70  /* a: p0<<28 | p
b6040 32 3c 3c 31 34 20 7c 20 70 34 20 28 75 6e 6d 61  2<<14 | p4 (unma
b6050 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21  sked) */.  if (!
b6060 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20  (a&0x80)).  {.  
b6070 20 20 2f 2a 20 77 65 20 63 61 6e 20 73 6b 69 70    /* we can skip
b6080 20 74 68 65 73 65 20 63 61 75 73 65 20 74 68 65   these cause the
b6090 79 20 77 65 72 65 20 28 65 66 66 65 63 74 69 76  y were (effectiv
b60a0 65 6c 79 29 20 64 6f 6e 65 20 61 62 6f 76 65 20  ely) done above 
b60b0 69 6e 20 63 61 6c 63 27 69 6e 67 20 73 20 2a 2f  in calc'ing s */
b60c0 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28 30 78  .    /* a &= (0x
b60d0 37 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31  7f<<28)|(0x7f<<1
b60e0 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20  4)|(0x7f); */.  
b60f0 20 20 2f 2a 20 62 20 26 3d 20 28 30 78 37 66 3c    /* b &= (0x7f<
b6100 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a  <14)|(0x7f); */.
b6110 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20      b = b<<7;.  
b6120 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20    a |= b;.    s 
b6130 3d 20 73 3e 3e 31 38 3b 0a 20 20 20 20 2a 76 20  = s>>18;.    *v 
b6140 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c  = ((u64)s)<<32 |
b6150 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 35   a;.    return 5
b6160 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 32 3a 73 61  ;.  }..  /* 2:sa
b6170 76 65 20 6f 66 66 20 70 30 3c 3c 32 31 20 7c 20  ve off p0<<21 | 
b6180 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c  p1<<14 | p2<<7 |
b6190 20 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a   p3 (masked) */.
b61a0 20 20 73 20 3d 20 73 3c 3c 37 3b 0a 20 20 73 20    s = s<<7;.  s 
b61b0 7c 3d 20 62 3b 0a 20 20 2f 2a 20 73 3a 20 70 30  |= b;.  /* s: p0
b61c0 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20  <<21 | p1<<14 | 
b61d0 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b  p2<<7 | p3 (mask
b61e0 65 64 29 20 2a 2f 0a 0a 20 20 70 2b 2b 3b 0a 20  ed) */..  p++;. 
b61f0 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20   b = b<<14;.  b 
b6200 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70  |= *p;.  /* b: p
b6210 31 3c 3c 32 38 20 7c 20 70 33 3c 3c 31 34 20 7c  1<<28 | p3<<14 |
b6220 20 70 35 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a   p5 (unmasked) *
b6230 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30  /.  if (!(b&0x80
b6240 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 77 65  )).  {.    /* we
b6250 20 63 61 6e 20 73 6b 69 70 20 74 68 69 73 20 63   can skip this c
b6260 61 75 73 65 20 69 74 20 77 61 73 20 28 65 66 66  ause it was (eff
b6270 65 63 74 69 76 65 6c 79 29 20 64 6f 6e 65 20 61  ectively) done a
b6280 62 6f 76 65 20 69 6e 20 63 61 6c 63 27 69 6e 67  bove in calc'ing
b6290 20 73 20 2a 2f 0a 20 20 20 20 2f 2a 20 62 20 26   s */.    /* b &
b62a0 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 78  = (0x7f<<28)|(0x
b62b0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20  7f<<14)|(0x7f); 
b62c0 2a 2f 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37  */.    a &= (0x7
b62d0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20  f<<14)|(0x7f);. 
b62e0 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20     a = a<<7;.   
b62f0 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d   a |= b;.    s =
b6300 20 73 3e 3e 31 38 3b 0a 20 20 20 20 2a 76 20 3d   s>>18;.    *v =
b6310 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20   ((u64)s)<<32 | 
b6320 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 36 3b  a;.    return 6;
b6330 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61  .  }..  p++;.  a
b6340 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d   = a<<14;.  a |=
b6350 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 32 3c   *p;.  /* a: p2<
b6360 3c 32 38 20 7c 20 70 34 3c 3c 31 34 20 7c 20 70  <28 | p4<<14 | p
b6370 36 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  6 (unmasked) */.
b6380 20 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29    if (!(a&0x80))
b6390 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30  .  {.    a &= (0
b63a0 78 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c  x1f<<28)|(0x7f<<
b63b0 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20  14)|(0x7f);.    
b63c0 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c  b &= (0x7f<<14)|
b63d0 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20 3d 20  (0x7f);.    b = 
b63e0 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62  b<<7;.    a |= b
b63f0 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31 31 3b  ;.    s = s>>11;
b6400 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29  .    *v = ((u64)
b6410 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20  s)<<32 | a;.    
b6420 72 65 74 75 72 6e 20 37 3b 0a 20 20 7d 0a 0a 20  return 7;.  }.. 
b6430 20 2f 2a 20 43 53 45 32 20 66 72 6f 6d 20 62 65   /* CSE2 from be
b6440 6c 6f 77 20 2a 2f 0a 20 20 61 20 26 3d 20 28 30  low */.  a &= (0
b6450 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
b6460 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c  .  p++;.  b = b<
b6470 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a  <14;.  b |= *p;.
b6480 20 20 2f 2a 20 62 3a 20 70 33 3c 3c 32 38 20 7c    /* b: p3<<28 |
b6490 20 70 35 3c 3c 31 34 20 7c 20 70 37 20 28 75 6e   p5<<14 | p7 (un
b64a0 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20  masked) */.  if 
b64b0 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a  (!(b&0x80)).  {.
b64c0 20 20 20 20 62 20 26 3d 20 28 30 78 31 66 3c 3c      b &= (0x1f<<
b64d0 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28  28)|(0x7f<<14)|(
b64e0 30 78 37 66 29 3b 0a 20 20 20 20 2f 2a 20 6d 6f  0x7f);.    /* mo
b64f0 76 65 64 20 43 53 45 32 20 75 70 20 2a 2f 0a 20  ved CSE2 up */. 
b6500 20 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66     /* a &= (0x7f
b6510 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f  <<14)|(0x7f); */
b6520 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20  .    a = a<<7;. 
b6530 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73     a |= b;.    s
b6540 20 3d 20 73 3e 3e 34 3b 0a 20 20 20 20 2a 76 20   = s>>4;.    *v 
b6550 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c  = ((u64)s)<<32 |
b6560 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 38   a;.    return 8
b6570 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20  ;.  }..  p++;.  
b6580 61 20 3d 20 61 3c 3c 31 35 3b 0a 20 20 61 20 7c  a = a<<15;.  a |
b6590 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 34  = *p;.  /* a: p4
b65a0 3c 3c 32 39 20 7c 20 70 36 3c 3c 31 35 20 7c 20  <<29 | p6<<15 | 
b65b0 70 38 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f  p8 (unmasked) */
b65c0 0a 0a 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45  ..  /* moved CSE
b65d0 32 20 75 70 20 2a 2f 0a 20 20 2f 2a 20 61 20 26  2 up */.  /* a &
b65e0 3d 20 28 30 78 37 66 3c 3c 32 39 29 7c 28 30 78  = (0x7f<<29)|(0x
b65f0 37 66 3c 3c 31 35 29 7c 28 30 78 66 66 29 3b 20  7f<<15)|(0xff); 
b6600 2a 2f 0a 20 20 62 20 26 3d 20 28 30 78 37 66 3c  */.  b &= (0x7f<
b6610 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 62  <14)|(0x7f);.  b
b6620 20 3d 20 62 3c 3c 38 3b 0a 20 20 61 20 7c 3d 20   = b<<8;.  a |= 
b6630 62 3b 0a 0a 20 20 73 20 3d 20 73 3c 3c 34 3b 0a  b;..  s = s<<4;.
b6640 20 20 62 20 3d 20 70 5b 2d 34 5d 3b 0a 20 20 62    b = p[-4];.  b
b6650 20 26 3d 20 30 78 37 66 3b 0a 20 20 62 20 3d 20   &= 0x7f;.  b = 
b6660 62 3e 3e 33 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a  b>>3;.  s |= b;.
b6670 0a 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29  .  *v = ((u64)s)
b6680 3c 3c 33 32 20 7c 20 61 3b 0a 0a 20 20 72 65 74  <<32 | a;..  ret
b6690 75 72 6e 20 39 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 9;.}../*.** 
b66a0 52 65 61 64 20 61 20 33 32 2d 62 69 74 20 76 61  Read a 32-bit va
b66b0 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e  riable-length in
b66c0 74 65 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f 72  teger from memor
b66d0 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 5b  y starting at p[
b66e0 30 5d 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  0]..** Return th
b66f0 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
b6700 73 20 72 65 61 64 2e 20 20 54 68 65 20 76 61 6c  s read.  The val
b6710 75 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  ue is stored in 
b6720 2a 76 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  *v..**.** If the
b6730 20 76 61 72 69 6e 74 20 73 74 6f 72 65 64 20 69   varint stored i
b6740 6e 20 70 5b 30 5d 20 69 73 20 6c 61 72 67 65 72  n p[0] is larger
b6750 20 74 68 61 6e 20 63 61 6e 20 66 69 74 20 69 6e   than can fit in
b6760 20 61 20 33 32 2d 62 69 74 20 75 6e 73 69 67 6e   a 32-bit unsign
b6770 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 74  ed.** integer, t
b6780 68 65 6e 20 73 65 74 20 2a 76 20 74 6f 20 30 78  hen set *v to 0x
b6790 66 66 66 66 66 66 66 66 2e 0a 2a 2a 0a 2a 2a 20  ffffffff..**.** 
b67a0 41 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c  A MACRO version,
b67b0 20 67 65 74 56 61 72 69 6e 74 33 32 2c 20 69 73   getVarint32, is
b67c0 20 70 72 6f 76 69 64 65 64 20 77 68 69 63 68 20   provided which 
b67d0 69 6e 6c 69 6e 65 73 20 74 68 65 20 0a 2a 2a 20  inlines the .** 
b67e0 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65  single-byte case
b67f0 2e 20 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75  .  All code shou
b6800 6c 64 20 75 73 65 20 74 68 65 20 4d 41 43 52 4f  ld use the MACRO
b6810 20 76 65 72 73 69 6f 6e 20 61 73 20 0a 2a 2a 20   version as .** 
b6820 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73  this function as
b6830 73 75 6d 65 73 20 74 68 65 20 73 69 6e 67 6c 65  sumes the single
b6840 2d 62 79 74 65 20 63 61 73 65 20 68 61 73 20 61  -byte case has a
b6850 6c 72 65 61 64 79 20 62 65 65 6e 20 68 61 6e 64  lready been hand
b6860 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  led..*/.SQLITE_P
b6870 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65  RIVATE u8 sqlite
b6880 33 47 65 74 56 61 72 69 6e 74 33 32 28 63 6f 6e  3GetVarint32(con
b6890 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
b68a0 20 2a 70 2c 20 75 33 32 20 2a 76 29 7b 0a 20 20   *p, u32 *v){.  
b68b0 75 33 32 20 61 2c 62 3b 0a 0a 20 20 2f 2a 20 54  u32 a,b;..  /* T
b68c0 68 65 20 31 2d 62 79 74 65 20 63 61 73 65 2e 20  he 1-byte case. 
b68d0 20 4f 76 65 72 77 68 65 6c 6d 69 6e 67 6c 79 20   Overwhelmingly 
b68e0 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 2e  the most common.
b68f0 20 20 48 61 6e 64 6c 65 64 20 69 6e 6c 69 6e 65    Handled inline
b6900 0a 20 20 2a 2a 20 62 79 20 74 68 65 20 67 65 74  .  ** by the get
b6910 56 61 72 69 6e 33 32 28 29 20 6d 61 63 72 6f 20  Varin32() macro 
b6920 2a 2f 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20 2f  */.  a = *p;.  /
b6930 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b 65  * a: p0 (unmaske
b6940 64 29 20 2a 2f 0a 23 69 66 6e 64 65 66 20 67 65  d) */.#ifndef ge
b6950 74 56 61 72 69 6e 74 33 32 0a 20 20 69 66 20 28  tVarint32.  if (
b6960 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20  !(a&0x80)).  {. 
b6970 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74     /* Values bet
b6980 77 65 65 6e 20 30 20 61 6e 64 20 31 32 37 20 2a  ween 0 and 127 *
b6990 2f 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20  /.    *v = a;.  
b69a0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
b69b0 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 54 68 65  #endif..  /* The
b69c0 20 32 2d 62 79 74 65 20 63 61 73 65 20 2a 2f 0a   2-byte case */.
b69d0 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b    p++;.  b = *p;
b69e0 0a 20 20 2f 2a 20 62 3a 20 70 31 20 28 75 6e 6d  .  /* b: p1 (unm
b69f0 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28  asked) */.  if (
b6a00 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20  !(b&0x80)).  {. 
b6a10 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74     /* Values bet
b6a20 77 65 65 6e 20 31 32 38 20 61 6e 64 20 31 36 33  ween 128 and 163
b6a30 38 33 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 30  83 */.    a &= 0
b6a40 78 37 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c  x7f;.    a = a<<
b6a50 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20  7;.    *v = a | 
b6a60 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b  b;.    return 2;
b6a70 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 33  .  }..  /* The 3
b6a80 2d 62 79 74 65 20 63 61 73 65 20 2a 2f 0a 20 20  -byte case */.  
b6a90 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34  p++;.  a = a<<14
b6aa0 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f  ;.  a |= *p;.  /
b6ab0 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32  * a: p0<<14 | p2
b6ac0 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20   (unmasked) */. 
b6ad0 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a   if (!(a&0x80)).
b6ae0 20 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65    {.    /* Value
b6af0 73 20 62 65 74 77 65 65 6e 20 31 36 33 38 34 20  s between 16384 
b6b00 61 6e 64 20 32 30 39 37 31 35 31 20 2a 2f 0a 20  and 2097151 */. 
b6b10 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31     a &= (0x7f<<1
b6b20 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62  4)|(0x7f);.    b
b6b30 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 20   &= 0x7f;.    b 
b6b40 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d  = b<<7;.    *v =
b6b50 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75   a | b;.    retu
b6b60 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rn 3;.  }..  /* 
b6b70 41 20 33 32 2d 62 69 74 20 76 61 72 69 6e 74 20  A 32-bit varint 
b6b80 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  is used to store
b6b90 20 73 69 7a 65 20 69 6e 66 6f 72 6d 61 74 69 6f   size informatio
b6ba0 6e 20 69 6e 20 62 74 72 65 65 73 2e 0a 20 20 2a  n in btrees..  *
b6bb0 2a 20 4f 62 6a 65 63 74 73 20 61 72 65 20 72 61  * Objects are ra
b6bc0 72 65 6c 79 20 6c 61 72 67 65 72 20 74 68 61 6e  rely larger than
b6bd0 20 32 4d 69 42 20 6c 69 6d 69 74 20 6f 66 20 61   2MiB limit of a
b6be0 20 33 2d 62 79 74 65 20 76 61 72 69 6e 74 2e 0a   3-byte varint..
b6bf0 20 20 2a 2a 20 41 20 33 2d 62 79 74 65 20 76 61    ** A 3-byte va
b6c00 72 69 6e 74 20 69 73 20 73 75 66 66 69 63 69 65  rint is sufficie
b6c10 6e 74 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c  nt, for example,
b6c20 20 74 6f 20 72 65 63 6f 72 64 20 74 68 65 20 73   to record the s
b6c30 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 61 20 31 30  ize.  ** of a 10
b6c40 34 38 35 36 39 2d 62 79 74 65 20 42 4c 4f 42 20  48569-byte BLOB 
b6c50 6f 72 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2a 0a  or string..  **.
b6c60 20 20 2a 2a 20 57 65 20 6f 6e 6c 79 20 75 6e 72    ** We only unr
b6c70 6f 6c 6c 20 74 68 65 20 66 69 72 73 74 20 31 2d  oll the first 1-
b6c80 2c 20 32 2d 2c 20 61 6e 64 20 33 2d 20 62 79 74  , 2-, and 3- byt
b6c90 65 20 63 61 73 65 73 2e 20 20 54 68 65 20 76 65  e cases.  The ve
b6ca0 72 79 0a 20 20 2a 2a 20 72 61 72 65 20 6c 61 72  ry.  ** rare lar
b6cb0 67 65 72 20 63 61 73 65 73 20 63 61 6e 20 62 65  ger cases can be
b6cc0 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20   handled by the 
b6cd0 73 6c 6f 77 65 72 20 36 34 2d 62 69 74 20 76 61  slower 64-bit va
b6ce0 72 69 6e 74 0a 20 20 2a 2a 20 72 6f 75 74 69 6e  rint.  ** routin
b6cf0 65 2e 0a 20 20 2a 2f 0a 23 69 66 20 31 0a 20 20  e..  */.#if 1.  
b6d00 7b 0a 20 20 20 20 75 36 34 20 76 36 34 3b 0a 20  {.    u64 v64;. 
b6d10 20 20 20 75 38 20 6e 3b 0a 0a 20 20 20 20 70 20     u8 n;..    p 
b6d20 2d 3d 20 32 3b 0a 20 20 20 20 6e 20 3d 20 73 71  -= 2;.    n = sq
b6d30 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 70  lite3GetVarint(p
b6d40 2c 20 26 76 36 34 29 3b 0a 20 20 20 20 61 73 73  , &v64);.    ass
b6d50 65 72 74 28 20 6e 3e 33 20 26 26 20 6e 3c 3d 39  ert( n>3 && n<=9
b6d60 20 29 3b 0a 20 20 20 20 69 66 28 20 28 76 36 34   );.    if( (v64
b6d70 20 26 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33   & SQLITE_MAX_U3
b6d80 32 29 21 3d 76 36 34 20 29 7b 0a 20 20 20 20 20  2)!=v64 ){.     
b6d90 20 2a 76 20 3d 20 30 78 66 66 66 66 66 66 66 66   *v = 0xffffffff
b6da0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
b6db0 20 20 20 2a 76 20 3d 20 28 75 33 32 29 76 36 34     *v = (u32)v64
b6dc0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
b6dd0 72 6e 20 6e 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65  rn n;.  }..#else
b6de0 0a 20 20 2f 2a 20 46 6f 72 20 66 6f 6c 6c 6f 77  .  /* For follow
b6df0 69 6e 67 20 63 6f 64 65 20 28 6b 65 70 74 20 66  ing code (kept f
b6e00 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65  or historical re
b6e10 63 6f 72 64 20 6f 6e 6c 79 29 20 73 68 6f 77 73  cord only) shows
b6e20 20 61 6e 0a 20 20 2a 2a 20 75 6e 72 6f 6c 6c 69   an.  ** unrolli
b6e30 6e 67 20 66 6f 72 20 74 68 65 20 33 2d 20 61 6e  ng for the 3- an
b6e40 64 20 34 2d 62 79 74 65 20 76 61 72 69 6e 74 20  d 4-byte varint 
b6e50 63 61 73 65 73 2e 20 20 54 68 69 73 20 63 6f 64  cases.  This cod
b6e60 65 20 69 73 0a 20 20 2a 2a 20 73 6c 69 67 68 74  e is.  ** slight
b6e70 6c 79 20 66 61 73 74 65 72 2c 20 62 75 74 20 69  ly faster, but i
b6e80 74 20 69 73 20 61 6c 73 6f 20 6c 61 72 67 65 72  t is also larger
b6e90 20 61 6e 64 20 6d 75 63 68 20 68 61 72 64 65 72   and much harder
b6ea0 20 74 6f 20 74 65 73 74 2e 0a 20 20 2a 2f 0a 20   to test..  */. 
b6eb0 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31   p++;.  b = b<<1
b6ec0 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20  4;.  b |= *p;.  
b6ed0 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70  /* b: p1<<14 | p
b6ee0 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  3 (unmasked) */.
b6ef0 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29    if (!(b&0x80))
b6f00 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75  .  {.    /* Valu
b6f10 65 73 20 62 65 74 77 65 65 6e 20 32 30 39 37 31  es between 20971
b6f20 35 32 20 61 6e 64 20 32 36 38 34 33 35 34 35 35  52 and 268435455
b6f30 20 2a 2f 0a 20 20 20 20 62 20 26 3d 20 28 30 78   */.    b &= (0x
b6f40 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
b6f50 20 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c      a &= (0x7f<<
b6f60 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20  14)|(0x7f);.    
b6f70 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76  a = a<<7;.    *v
b6f80 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65   = a | b;.    re
b6f90 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 70  turn 4;.  }..  p
b6fa0 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b  ++;.  a = a<<14;
b6fb0 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a  .  a |= *p;.  /*
b6fc0 20 61 3a 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c   a: p0<<28 | p2<
b6fd0 3c 31 34 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b  <14 | p4 (unmask
b6fe0 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61  ed) */.  if (!(a
b6ff0 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20  &0x80)).  {.    
b7000 2f 2a 20 57 61 6c 75 65 73 20 20 62 65 74 77 65  /* Walues  betwe
b7010 65 6e 20 32 36 38 34 33 35 34 35 36 20 61 6e 64  en 268435456 and
b7020 20 33 34 33 35 39 37 33 38 33 36 37 20 2a 2f 0a   34359738367 */.
b7030 20 20 20 20 61 20 26 3d 20 28 30 78 31 66 3c 3c      a &= (0x1f<<
b7040 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28  28)|(0x7f<<14)|(
b7050 30 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20  0x7f);.    b &= 
b7060 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 78 37 66  (0x1f<<28)|(0x7f
b7070 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20  <<14)|(0x7f);.  
b7080 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20    b = b<<7;.    
b7090 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20  *v = a | b;.    
b70a0 72 65 74 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20  return 5;.  }.. 
b70b0 20 2f 2a 20 57 65 20 63 61 6e 20 6f 6e 6c 79 20   /* We can only 
b70c0 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74  reach this point
b70d0 20 77 68 65 6e 20 72 65 61 64 69 6e 67 20 61 20   when reading a 
b70e0 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65  corrupt database
b70f0 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 20 49 6e 20  .  ** file.  In 
b7100 74 68 61 74 20 63 61 73 65 20 77 65 20 61 72 65  that case we are
b7110 20 6e 6f 74 20 69 6e 20 61 6e 79 20 68 75 72 72   not in any hurr
b7120 79 2e 20 20 55 73 65 20 74 68 65 20 28 72 65 6c  y.  Use the (rel
b7130 61 74 69 76 65 6c 79 0a 20 20 2a 2a 20 73 6c 6f  atively.  ** slo
b7140 77 29 20 67 65 6e 65 72 61 6c 2d 70 75 72 70 6f  w) general-purpo
b7150 73 65 20 73 71 6c 69 74 65 33 47 65 74 56 61 72  se sqlite3GetVar
b7160 69 6e 74 28 29 20 72 6f 75 74 69 6e 65 20 74 6f  int() routine to
b7170 20 65 78 74 72 61 63 74 20 74 68 65 0a 20 20 2a   extract the.  *
b7180 2a 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 7b 0a  * value. */.  {.
b7190 20 20 20 20 75 36 34 20 76 36 34 3b 0a 20 20 20      u64 v64;.   
b71a0 20 75 38 20 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d   u8 n;..    p -=
b71b0 20 34 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69   4;.    n = sqli
b71c0 74 65 33 47 65 74 56 61 72 69 6e 74 28 70 2c 20  te3GetVarint(p, 
b71d0 26 76 36 34 29 3b 0a 20 20 20 20 61 73 73 65 72  &v64);.    asser
b71e0 74 28 20 6e 3e 35 20 26 26 20 6e 3c 3d 39 20 29  t( n>5 && n<=9 )
b71f0 3b 0a 20 20 20 20 2a 76 20 3d 20 28 75 33 32 29  ;.    *v = (u32)
b7200 76 36 34 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  v64;.    return 
b7210 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a  n;.  }.#endif.}.
b7220 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
b7230 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
b7240 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 6e  s that will be n
b7250 65 65 64 65 64 20 74 6f 20 73 74 6f 72 65 20 74  eeded to store t
b7260 68 65 20 67 69 76 65 6e 0a 2a 2a 20 36 34 2d 62  he given.** 64-b
b7270 69 74 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53  it integer..*/.S
b7280 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
b7290 74 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c  t sqlite3VarintL
b72a0 65 6e 28 75 36 34 20 76 29 7b 0a 20 20 69 6e 74  en(u64 v){.  int
b72b0 20 69 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20   i = 0;.  do{.  
b72c0 20 20 69 2b 2b 3b 0a 20 20 20 20 76 20 3e 3e 3d    i++;.    v >>=
b72d0 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 21   7;.  }while( v!
b72e0 3d 30 20 26 26 20 41 4c 57 41 59 53 28 69 3c 39  =0 && ALWAYS(i<9
b72f0 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b  ) );.  return i;
b7300 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  .}.../*.** Read 
b7310 6f 72 20 77 72 69 74 65 20 61 20 66 6f 75 72 2d  or write a four-
b7320 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20  byte big-endian 
b7330 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a  integer value..*
b7340 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b7350 20 75 33 32 20 73 71 6c 69 74 65 33 47 65 74 34   u32 sqlite3Get4
b7360 62 79 74 65 28 63 6f 6e 73 74 20 75 38 20 2a 70  byte(const u8 *p
b7370 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30  ){.  return (p[0
b7380 5d 3c 3c 32 34 29 20 7c 20 28 70 5b 31 5d 3c 3c  ]<<24) | (p[1]<<
b7390 31 36 29 20 7c 20 28 70 5b 32 5d 3c 3c 38 29 20  16) | (p[2]<<8) 
b73a0 7c 20 70 5b 33 5d 3b 0a 7d 0a 53 51 4c 49 54 45  | p[3];.}.SQLITE
b73b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
b73c0 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 75 6e  lite3Put4byte(un
b73d0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20  signed char *p, 
b73e0 75 33 32 20 76 29 7b 0a 20 20 70 5b 30 5d 20 3d  u32 v){.  p[0] =
b73f0 20 28 75 38 29 28 76 3e 3e 32 34 29 3b 0a 20 20   (u8)(v>>24);.  
b7400 70 5b 31 5d 20 3d 20 28 75 38 29 28 76 3e 3e 31  p[1] = (u8)(v>>1
b7410 36 29 3b 0a 20 20 70 5b 32 5d 20 3d 20 28 75 38  6);.  p[2] = (u8
b7420 29 28 76 3e 3e 38 29 3b 0a 20 20 70 5b 33 5d 20  )(v>>8);.  p[3] 
b7430 3d 20 28 75 38 29 76 3b 0a 7d 0a 0a 0a 0a 23 69  = (u8)v;.}....#i
b7440 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
b7450 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45  E_OMIT_BLOB_LITE
b7460 52 41 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  RAL) || defined(
b7470 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
b7480 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74  )./*.** Translat
b7490 65 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20  e a single byte 
b74a0 6f 66 20 48 65 78 20 69 6e 74 6f 20 61 6e 20 69  of Hex into an i
b74b0 6e 74 65 67 65 72 2e 0a 2a 2a 20 54 68 69 73 20  nteger..** This 
b74c0 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72  routine only wor
b74d0 6b 73 20 69 66 20 68 20 72 65 61 6c 6c 79 20 69  ks if h really i
b74e0 73 20 61 20 76 61 6c 69 64 20 68 65 78 61 64 65  s a valid hexade
b74f0 63 69 6d 61 6c 0a 2a 2a 20 63 68 61 72 61 63 74  cimal.** charact
b7500 65 72 3a 20 20 30 2e 2e 39 61 2e 2e 66 41 2e 2e  er:  0..9a..fA..
b7510 46 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 68  F.*/.static u8 h
b7520 65 78 54 6f 49 6e 74 28 69 6e 74 20 68 29 7b 0a  exToInt(int h){.
b7530 20 20 61 73 73 65 72 74 28 20 28 68 3e 3d 27 30    assert( (h>='0
b7540 27 20 26 26 20 68 3c 3d 27 39 27 29 20 7c 7c 20  ' && h<='9') || 
b7550 20 28 68 3e 3d 27 61 27 20 26 26 20 68 3c 3d 27   (h>='a' && h<='
b7560 66 27 29 20 7c 7c 20 20 28 68 3e 3d 27 41 27 20  f') ||  (h>='A' 
b7570 26 26 20 68 3c 3d 27 46 27 29 20 29 3b 0a 23 69  && h<='F') );.#i
b7580 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49  fdef SQLITE_ASCI
b7590 49 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 28 68  I.  h += 9*(1&(h
b75a0 3e 3e 36 29 29 3b 0a 23 65 6e 64 69 66 0a 23 69  >>6));.#endif.#i
b75b0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44  fdef SQLITE_EBCD
b75c0 49 43 0a 20 20 68 20 2b 3d 20 39 2a 28 31 26 7e  IC.  h += 9*(1&~
b75d0 28 68 3e 3e 34 29 29 3b 0a 23 65 6e 64 69 66 0a  (h>>4));.#endif.
b75e0 20 20 72 65 74 75 72 6e 20 28 75 38 29 28 68 20    return (u8)(h 
b75f0 26 20 30 78 66 29 3b 0a 7d 0a 23 65 6e 64 69 66  & 0xf);.}.#endif
b7600 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54   /* !SQLITE_OMIT
b7610 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c 7c  _BLOB_LITERAL ||
b7620 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45   SQLITE_HAS_CODE
b7630 43 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e  C */..#if !defin
b7640 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ed(SQLITE_OMIT_B
b7650 4c 4f 42 5f 4c 49 54 45 52 41 4c 29 20 7c 7c 20  LOB_LITERAL) || 
b7660 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48  defined(SQLITE_H
b7670 41 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20  AS_CODEC)./*.** 
b7680 43 6f 6e 76 65 72 74 20 61 20 42 4c 4f 42 20 6c  Convert a BLOB l
b7690 69 74 65 72 61 6c 20 6f 66 20 74 68 65 20 66 6f  iteral of the fo
b76a0 72 6d 20 22 78 27 68 68 68 68 68 68 27 22 20 69  rm "x'hhhhhh'" i
b76b0 6e 74 6f 20 69 74 73 20 62 69 6e 61 72 79 0a 2a  nto its binary.*
b76c0 2a 20 76 61 6c 75 65 2e 20 20 52 65 74 75 72 6e  * value.  Return
b76d0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74   a pointer to it
b76e0 73 20 62 69 6e 61 72 79 20 76 61 6c 75 65 2e 20  s binary value. 
b76f0 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
b7700 68 65 0a 2a 2a 20 62 69 6e 61 72 79 20 76 61 6c  he.** binary val
b7710 75 65 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61  ue has been obta
b7720 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63  ined from malloc
b7730 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65   and must be fre
b7740 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 63 61 6c  ed by.** the cal
b7750 6c 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f  ling routine..*/
b7760 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b7770 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65 78  void *sqlite3Hex
b7780 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 20 2a  ToBlob(sqlite3 *
b7790 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
b77a0 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68 61  z, int n){.  cha
b77b0 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 69 6e 74 20  r *zBlob;.  int 
b77c0 69 3b 0a 0a 20 20 7a 42 6c 6f 62 20 3d 20 28 63  i;..  zBlob = (c
b77d0 68 61 72 20 2a 29 73 71 6c 69 74 65 33 44 62 4d  har *)sqlite3DbM
b77e0 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2f 32  allocRaw(db, n/2
b77f0 20 2b 20 31 29 3b 0a 20 20 6e 2d 2d 3b 0a 20 20   + 1);.  n--;.  
b7800 69 66 28 20 7a 42 6c 6f 62 20 29 7b 0a 20 20 20  if( zBlob ){.   
b7810 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69   for(i=0; i<n; i
b7820 2b 3d 32 29 7b 0a 20 20 20 20 20 20 7a 42 6c 6f  +=2){.      zBlo
b7830 62 5b 69 2f 32 5d 20 3d 20 28 68 65 78 54 6f 49  b[i/2] = (hexToI
b7840 6e 74 28 7a 5b 69 5d 29 3c 3c 34 29 20 7c 20 68  nt(z[i])<<4) | h
b7850 65 78 54 6f 49 6e 74 28 7a 5b 69 2b 31 5d 29 3b  exToInt(z[i+1]);
b7860 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 6c 6f 62  .    }.    zBlob
b7870 5b 69 2f 32 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20  [i/2] = 0;.  }. 
b7880 20 72 65 74 75 72 6e 20 7a 42 6c 6f 62 3b 0a 7d   return zBlob;.}
b7890 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49  .#endif /* !SQLI
b78a0 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54  TE_OMIT_BLOB_LIT
b78b0 45 52 41 4c 20 7c 7c 20 53 51 4c 49 54 45 5f 48  ERAL || SQLITE_H
b78c0 41 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a 0a 2f 2a  AS_CODEC */.../*
b78d0 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73  .** Change the s
b78e0 71 6c 69 74 65 2e 6d 61 67 69 63 20 66 72 6f 6d  qlite.magic from
b78f0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50   SQLITE_MAGIC_OP
b7900 45 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47  EN to SQLITE_MAG
b7910 49 43 5f 42 55 53 59 2e 0a 2a 2a 20 52 65 74 75  IC_BUSY..** Retu
b7920 72 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f 6e  rn an error (non
b7930 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d 61  -zero) if the ma
b7940 67 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c 49  gic was not SQLI
b7950 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a  TE_MAGIC_OPEN.**
b7960 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   when this routi
b7970 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a  ne is called..**
b7980 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
b7990 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   is called when 
b79a0 65 6e 74 65 72 69 6e 67 20 61 6e 20 53 51 4c 69  entering an SQLi
b79b0 74 65 20 41 50 49 2e 20 20 54 68 65 20 53 51 4c  te API.  The SQL
b79c0 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a  ITE_MAGIC_OPEN.*
b79d0 2a 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65  * value indicate
b79e0 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62  s that the datab
b79f0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70  ase connection p
b7a00 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 20 41  assed into the A
b7a10 50 49 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 61 6e  PI is.** open an
b7a20 64 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 75  d is not being u
b7a30 73 65 64 20 62 79 20 61 6e 6f 74 68 65 72 20 74  sed by another t
b7a40 68 72 65 61 64 2e 20 20 42 79 20 63 68 61 6e 67  hread.  By chang
b7a50 69 6e 67 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a  ing the value.**
b7a60 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43   to SQLITE_MAGIC
b7a70 5f 42 55 53 59 20 77 65 20 69 6e 64 69 63 61 74  _BUSY we indicat
b7a80 65 20 74 68 61 74 20 74 68 65 20 63 6f 6e 6e 65  e that the conne
b7a90 63 74 69 6f 6e 20 69 73 20 69 6e 20 75 73 65 2e  ction is in use.
b7aa0 0a 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 65 74  .** sqlite3Safet
b7ab0 79 4f 66 66 28 29 20 62 65 6c 6f 77 20 77 69 6c  yOff() below wil
b7ac0 6c 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c  l change the val
b7ad0 75 65 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 54  ue back to SQLIT
b7ae0 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20  E_MAGIC_OPEN.** 
b7af0 77 68 65 6e 20 74 68 65 20 41 50 49 20 65 78 69  when the API exi
b7b00 74 73 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ts. .**.** This 
b7b10 72 6f 75 74 69 6e 65 20 69 73 20 61 20 61 74 74  routine is a att
b7b20 65 6d 70 74 20 74 6f 20 64 65 74 65 63 74 20 69  empt to detect i
b7b30 66 20 74 77 6f 20 74 68 72 65 61 64 73 20 75 73  f two threads us
b7b40 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 73 71  e the.** same sq
b7b50 6c 69 74 65 2a 20 70 6f 69 6e 74 65 72 20 61 74  lite* pointer at
b7b60 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20   the same time. 
b7b70 20 54 68 65 72 65 20 69 73 20 61 20 72 61 63 65   There is a race
b7b80 20 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 20 73   .** condition s
b7b90 6f 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65  o it is possible
b7ba0 20 74 68 61 74 20 74 68 65 20 65 72 72 6f 72 20   that the error 
b7bb0 69 73 20 6e 6f 74 20 64 65 74 65 63 74 65 64 2e  is not detected.
b7bc0 0a 2a 2a 20 42 75 74 20 75 73 75 61 6c 6c 79 20  .** But usually 
b7bd0 74 68 65 20 70 72 6f 62 6c 65 6d 20 77 69 6c 6c  the problem will
b7be0 20 62 65 20 73 65 65 6e 2e 20 20 54 68 65 20 72   be seen.  The r
b7bf0 65 73 75 6c 74 20 77 69 6c 6c 20 62 65 20 61 6e  esult will be an
b7c00 0a 2a 2a 20 65 72 72 6f 72 20 77 68 69 63 68 20  .** error which 
b7c10 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 64  can be used to d
b7c20 65 62 75 67 20 74 68 65 20 61 70 70 6c 69 63 61  ebug the applica
b7c30 74 69 6f 6e 20 74 68 61 74 20 69 73 0a 2a 2a 20  tion that is.** 
b7c40 75 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e 63  using SQLite inc
b7c50 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20  orrectly..**.** 
b7c60 54 69 63 6b 65 74 20 23 32 30 32 3a 20 20 49 66  Ticket #202:  If
b7c70 20 64 62 2d 3e 6d 61 67 69 63 20 69 73 20 6e 6f   db->magic is no
b7c80 74 20 61 20 76 61 6c 69 64 20 6f 70 65 6e 20 76  t a valid open v
b7c90 61 6c 75 65 2c 20 74 61 6b 65 20 63 61 72 65 20  alue, take care 
b7ca0 6e 6f 74 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79  not.** to modify
b7cb0 20 74 68 65 20 64 62 20 73 74 72 75 63 74 75 72   the db structur
b7cc0 65 20 61 74 20 61 6c 6c 2e 20 20 49 74 20 63 6f  e at all.  It co
b7cd0 75 6c 64 20 62 65 20 74 68 61 74 20 64 62 20 69  uld be that db i
b7ce0 73 20 61 20 73 74 61 6c 65 0a 2a 2a 20 70 6f 69  s a stale.** poi
b7cf0 6e 74 65 72 2e 20 20 49 6e 20 6f 74 68 65 72 20  nter.  In other 
b7d00 77 6f 72 64 73 2c 20 69 74 20 63 6f 75 6c 64 20  words, it could 
b7d10 62 65 20 74 68 61 74 20 74 68 65 72 65 20 68 61  be that there ha
b7d20 73 20 62 65 65 6e 20 61 20 70 72 69 6f 72 0a 2a  s been a prior.*
b7d30 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  * call to sqlite
b7d40 33 5f 63 6c 6f 73 65 28 64 62 29 20 61 6e 64 20  3_close(db) and 
b7d50 64 62 20 68 61 73 20 62 65 65 6e 20 64 65 61 6c  db has been deal
b7d60 6c 6f 63 61 74 65 64 2e 20 20 41 6e 64 20 77 65  located.  And we
b7d70 20 64 6f 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20   do.** not want 
b7d80 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 64 65  to write into de
b7d90 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79  allocated memory
b7da0 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
b7db0 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f  TE_DEBUG.SQLITE_
b7dc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
b7dd0 74 65 33 53 61 66 65 74 79 4f 6e 28 73 71 6c 69  te3SafetyOn(sqli
b7de0 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20  te3 *db){.  if( 
b7df0 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54  db->magic==SQLIT
b7e00 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 29 7b 0a  E_MAGIC_OPEN ){.
b7e10 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20      db->magic = 
b7e20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53  SQLITE_MAGIC_BUS
b7e30 59 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  Y;.    assert( s
b7e40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
b7e50 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
b7e60 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
b7e70 7d 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 6d 61  }else if( db->ma
b7e80 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49  gic==SQLITE_MAGI
b7e90 43 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 64 62  C_BUSY ){.    db
b7ea0 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45  ->magic = SQLITE
b7eb0 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20 20  _MAGIC_ERROR;.  
b7ec0 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72    db->u1.isInter
b7ed0 72 75 70 74 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  rupted = 1;.  }.
b7ee0 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65    return 1;.}.#e
b7ef0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  ndif../*.** Chan
b7f00 67 65 20 74 68 65 20 6d 61 67 69 63 20 66 72 6f  ge the magic fro
b7f10 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42  m SQLITE_MAGIC_B
b7f20 55 53 59 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41  USY to SQLITE_MA
b7f30 47 49 43 5f 4f 50 45 4e 2e 0a 2a 2a 20 52 65 74  GIC_OPEN..** Ret
b7f40 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f  urn an error (no
b7f50 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d  n-zero) if the m
b7f60 61 67 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c  agic was not SQL
b7f70 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 0a 2a  ITE_MAGIC_BUSY.*
b7f80 2a 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74  * when this rout
b7f90 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a  ine is called..*
b7fa0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
b7fb0 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49  DEBUG.SQLITE_PRI
b7fc0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
b7fd0 53 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 65  SafetyOff(sqlite
b7fe0 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62  3 *db){.  if( db
b7ff0 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f  ->magic==SQLITE_
b8000 4d 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20  MAGIC_BUSY ){.  
b8010 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51    db->magic = SQ
b8020 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 3b  LITE_MAGIC_OPEN;
b8030 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
b8040 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
b8050 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
b8060 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65    return 0;.  }e
b8070 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67  lse{.    db->mag
b8080 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49  ic = SQLITE_MAGI
b8090 43 5f 45 52 52 4f 52 3b 0a 20 20 20 20 64 62 2d  C_ERROR;.    db-
b80a0 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65  >u1.isInterrupte
b80b0 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72  d = 1;.    retur
b80c0 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  n 1;.  }.}.#endi
b80d0 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  f../*.** Check t
b80e0 6f 20 6d 61 6b 65 20 73 75 72 65 20 77 65 20 68  o make sure we h
b80f0 61 76 65 20 61 20 76 61 6c 69 64 20 64 62 20 70  ave a valid db p
b8100 6f 69 6e 74 65 72 2e 20 20 54 68 69 73 20 74 65  ointer.  This te
b8110 73 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 66 6f 6f  st is not.** foo
b8120 6c 70 72 6f 6f 66 20 62 75 74 20 69 74 20 64 6f  lproof but it do
b8130 65 73 20 70 72 6f 76 69 64 65 20 73 6f 6d 65 20  es provide some 
b8140 6d 65 61 73 75 72 65 20 6f 66 20 70 72 6f 74 65  measure of prote
b8150 63 74 69 6f 6e 20 61 67 61 69 6e 73 74 0a 2a 2a  ction against.**
b8160 20 6d 69 73 75 73 65 20 6f 66 20 74 68 65 20 69   misuse of the i
b8170 6e 74 65 72 66 61 63 65 20 73 75 63 68 20 61 73  nterface such as
b8180 20 70 61 73 73 69 6e 67 20 69 6e 20 64 62 20 70   passing in db p
b8190 6f 69 6e 74 65 72 73 20 74 68 61 74 20 61 72 65  ointers that are
b81a0 0a 2a 2a 20 4e 55 4c 4c 20 6f 72 20 77 68 69 63  .** NULL or whic
b81b0 68 20 68 61 76 65 20 62 65 65 6e 20 70 72 65 76  h have been prev
b81c0 69 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e 20 20  iously closed.  
b81d0 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  If this routine 
b81e0 72 65 74 75 72 6e 73 0a 2a 2a 20 31 20 69 74 20  returns.** 1 it 
b81f0 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 64  means that the d
b8200 62 20 70 6f 69 6e 74 65 72 20 69 73 20 76 61 6c  b pointer is val
b8210 69 64 20 61 6e 64 20 30 20 69 66 20 69 74 20 73  id and 0 if it s
b8220 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20  hould not be.** 
b8230 64 65 72 65 66 65 72 65 6e 63 65 64 20 66 6f 72  dereferenced for
b8240 20 61 6e 79 20 72 65 61 73 6f 6e 2e 20 20 54 68   any reason.  Th
b8250 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
b8260 6f 6e 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65  on should invoke
b8270 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  .** SQLITE_MISUS
b8280 45 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a  E immediately..*
b8290 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 65  *.** sqlite3Safe
b82a0 74 79 43 68 65 63 6b 4f 6b 28 29 20 72 65 71 75  tyCheckOk() requ
b82b0 69 72 65 73 20 74 68 61 74 20 74 68 65 20 64 62  ires that the db
b82c0 20 70 6f 69 6e 74 65 72 20 62 65 20 76 61 6c 69   pointer be vali
b82d0 64 20 66 6f 72 0a 2a 2a 20 75 73 65 2e 20 20 73  d for.** use.  s
b82e0 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
b82f0 6b 53 69 63 6b 4f 72 4f 6b 28 29 20 61 6c 6c 6f  kSickOrOk() allo
b8300 77 73 20 61 20 64 62 20 70 6f 69 6e 74 65 72 20  ws a db pointer 
b8310 74 68 61 74 20 66 61 69 6c 65 64 20 74 6f 0a 2a  that failed to.*
b8320 2a 20 6f 70 65 6e 20 70 72 6f 70 65 72 6c 79 20  * open properly 
b8330 61 6e 64 20 69 73 20 6e 6f 74 20 66 69 74 20 66  and is not fit f
b8340 6f 72 20 67 65 6e 65 72 61 6c 20 75 73 65 20 62  or general use b
b8350 75 74 20 77 68 69 63 68 20 63 61 6e 20 62 65 0a  ut which can be.
b8360 2a 2a 20 75 73 65 64 20 61 73 20 61 6e 20 61 72  ** used as an ar
b8370 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65  gument to sqlite
b8380 33 5f 65 72 72 6d 73 67 28 29 20 6f 72 20 73 71  3_errmsg() or sq
b8390 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 2e 0a 2a  lite3_close()..*
b83a0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b83b0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65   int sqlite3Safe
b83c0 74 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74 65  tyCheckOk(sqlite
b83d0 33 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d 61  3 *db){.  u32 ma
b83e0 67 69 63 3b 0a 20 20 69 66 28 20 64 62 3d 3d 30  gic;.  if( db==0
b83f0 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6d   ) return 0;.  m
b8400 61 67 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63  agic = db->magic
b8410 3b 0a 20 20 69 66 28 20 6d 61 67 69 63 21 3d 53  ;.  if( magic!=S
b8420 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e  QLITE_MAGIC_OPEN
b8430 20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f   .#ifdef SQLITE_
b8440 44 45 42 55 47 0a 20 20 20 20 20 26 26 20 6d 61  DEBUG.     && ma
b8450 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49  gic!=SQLITE_MAGI
b8460 43 5f 42 55 53 59 0a 23 65 6e 64 69 66 0a 20 20  C_BUSY.#endif.  
b8470 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
b8480 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
b8490 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 53 51  turn 1;.  }.}.SQ
b84a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
b84b0 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68   sqlite3SafetyCh
b84c0 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73 71 6c 69  eckSickOrOk(sqli
b84d0 74 65 33 20 2a 64 62 29 7b 0a 20 20 75 33 32 20  te3 *db){.  u32 
b84e0 6d 61 67 69 63 3b 0a 20 20 6d 61 67 69 63 20 3d  magic;.  magic =
b84f0 20 64 62 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66   db->magic;.  if
b8500 28 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f  ( magic!=SQLITE_
b8510 4d 41 47 49 43 5f 53 49 43 4b 20 26 26 0a 20 20  MAGIC_SICK &&.  
b8520 20 20 20 20 6d 61 67 69 63 21 3d 53 51 4c 49 54      magic!=SQLIT
b8530 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 26 26 0a  E_MAGIC_OPEN &&.
b8540 20 20 20 20 20 20 6d 61 67 69 63 21 3d 53 51 4c        magic!=SQL
b8550 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29  ITE_MAGIC_BUSY )
b8560 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65 74   return 0;.  ret
b8570 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  urn 1;.}../*****
b8580 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
b8590 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   util.c ********
b85a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b85b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b85c0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
b85d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
b85e0 66 69 6c 65 20 68 61 73 68 2e 63 20 2a 2a 2a 2a  file hash.c ****
b85f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8610 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
b8620 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 32  2001 September 2
b8630 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
b8640 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
b8650 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
b8660 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
b8670 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
b8680 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
b8690 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
b86a0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
b86b0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
b86c0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
b86d0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
b86e0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
b86f0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
b8700 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
b8710 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
b8720 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
b8730 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
b8740 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
b8750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8790 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
b87a0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
b87b0 66 20 67 65 6e 65 72 69 63 20 68 61 73 68 2d 74  f generic hash-t
b87c0 61 62 6c 65 73 0a 2a 2a 20 75 73 65 64 20 69 6e  ables.** used in
b87d0 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24   SQLite..**.** $
b87e0 49 64 3a 20 68 61 73 68 2e 63 2c 76 20 31 2e 33  Id: hash.c,v 1.3
b87f0 38 20 32 30 30 39 2f 30 35 2f 30 39 20 32 33 3a  8 2009/05/09 23:
b8800 32 39 3a 31 32 20 64 72 68 20 45 78 70 20 24 0a  29:12 drh Exp $.
b8810 2a 2f 0a 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b  */../* Turn bulk
b8820 20 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68   memory into a h
b8830 61 73 68 20 74 61 62 6c 65 20 6f 62 6a 65 63 74  ash table object
b8840 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67   by initializing
b8850 20 74 68 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6f   the.** fields o
b8860 66 20 74 68 65 20 48 61 73 68 20 73 74 72 75 63  f the Hash struc
b8870 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65  ture..**.** "pNe
b8880 77 22 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  w" is a pointer 
b8890 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  to the hash tabl
b88a0 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20  e that is to be 
b88b0 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a  initialized..*/.
b88c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
b88d0 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 49  oid sqlite3HashI
b88e0 6e 69 74 28 48 61 73 68 20 2a 70 4e 65 77 29 7b  nit(Hash *pNew){
b88f0 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 21  .  assert( pNew!
b8900 3d 30 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 66 69  =0 );.  pNew->fi
b8910 72 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d  rst = 0;.  pNew-
b8920 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 70 4e  >count = 0;.  pN
b8930 65 77 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a  ew->htsize = 0;.
b8940 20 20 70 4e 65 77 2d 3e 68 74 20 3d 20 30 3b 0a    pNew->ht = 0;.
b8950 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61 6c 6c  }../* Remove all
b8960 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 61 20   entries from a 
b8970 68 61 73 68 20 74 61 62 6c 65 2e 20 20 52 65 63  hash table.  Rec
b8980 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e  laim all memory.
b8990 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f  .** Call this ro
b89a0 75 74 69 6e 65 20 74 6f 20 64 65 6c 65 74 65 20  utine to delete 
b89b0 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f 72 20  a hash table or 
b89c0 74 6f 20 72 65 73 65 74 20 61 20 68 61 73 68 20  to reset a hash 
b89d0 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 74 68 65 20  table.** to the 
b89e0 65 6d 70 74 79 20 73 74 61 74 65 2e 0a 2a 2f 0a  empty state..*/.
b89f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
b8a00 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68 43  oid sqlite3HashC
b8a10 6c 65 61 72 28 48 61 73 68 20 2a 70 48 29 7b 0a  lear(Hash *pH){.
b8a20 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d    HashElem *elem
b8a30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72  ;         /* For
b8a40 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c   looping over al
b8a50 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68  l elements of th
b8a60 65 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 73  e table */..  as
b8a70 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20  sert( pH!=0 );. 
b8a80 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73   elem = pH->firs
b8a90 74 3b 0a 20 20 70 48 2d 3e 66 69 72 73 74 20 3d  t;.  pH->first =
b8aa0 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72   0;.  sqlite3_fr
b8ab0 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48  ee(pH->ht);.  pH
b8ac0 2d 3e 68 74 20 3d 20 30 3b 0a 20 20 70 48 2d 3e  ->ht = 0;.  pH->
b8ad0 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 77 68  htsize = 0;.  wh
b8ae0 69 6c 65 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20  ile( elem ){.   
b8af0 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74 5f   HashElem *next_
b8b00 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78  elem = elem->nex
b8b10 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  t;.    sqlite3_f
b8b20 72 65 65 28 65 6c 65 6d 29 3b 0a 20 20 20 20 65  ree(elem);.    e
b8b30 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b  lem = next_elem;
b8b40 0a 20 20 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74  .  }.  pH->count
b8b50 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54   = 0;.}../*.** T
b8b60 68 65 20 68 61 73 68 69 6e 67 20 66 75 6e 63 74  he hashing funct
b8b70 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75  ion..*/.static u
b8b80 6e 73 69 67 6e 65 64 20 69 6e 74 20 73 74 72 48  nsigned int strH
b8b90 61 73 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ash(const char *
b8ba0 7a 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20  z, int nKey){.  
b8bb0 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20 61 73 73  int h = 0;.  ass
b8bc0 65 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a  ert( nKey>=0 );.
b8bd0 20 20 77 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20    while( nKey > 
b8be0 30 20 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68  0  ){.    h = (h
b8bf0 3c 3c 33 29 20 5e 20 68 20 5e 20 73 71 6c 69 74  <<3) ^ h ^ sqlit
b8c00 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28  e3UpperToLower[(
b8c10 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 2a 7a  unsigned char)*z
b8c20 2b 2b 5d 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b  ++];.    nKey--;
b8c30 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 3b  .  }.  return h;
b8c40 0a 7d 0a 0a 0a 2f 2a 20 4c 69 6e 6b 20 70 4e 65  .}.../* Link pNe
b8c50 77 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74  w element into t
b8c60 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48  he hash table pH
b8c70 2e 20 20 49 66 20 70 45 6e 74 72 79 21 3d 30 20  .  If pEntry!=0 
b8c80 74 68 65 6e 20 61 6c 73 6f 0a 2a 2a 20 69 6e 73  then also.** ins
b8c90 65 72 74 20 70 4e 65 77 20 69 6e 74 6f 20 74 68  ert pNew into th
b8ca0 65 20 70 45 6e 74 72 79 20 68 61 73 68 20 62 75  e pEntry hash bu
b8cb0 63 6b 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  cket..*/.static 
b8cc0 76 6f 69 64 20 69 6e 73 65 72 74 45 6c 65 6d 65  void insertEleme
b8cd0 6e 74 28 0a 20 20 48 61 73 68 20 2a 70 48 2c 20  nt(.  Hash *pH, 
b8ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
b8cf0 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 68 61 73  The complete has
b8d00 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72  h table */.  str
b8d10 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 2c  uct _ht *pEntry,
b8d20 20 20 20 20 2f 2a 20 54 68 65 20 65 6e 74 72 79      /* The entry
b8d30 20 69 6e 74 6f 20 77 68 69 63 68 20 70 4e 65 77   into which pNew
b8d40 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   is inserted */.
b8d50 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 4e 65 77    HashElem *pNew
b8d60 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
b8d70 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e  element to be in
b8d80 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 48  serted */.){.  H
b8d90 61 73 68 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20  ashElem *pHead; 
b8da0 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 65        /* First e
b8db0 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 69  lement already i
b8dc0 6e 20 70 45 6e 74 72 79 20 2a 2f 0a 20 20 69 66  n pEntry */.  if
b8dd0 28 20 70 45 6e 74 72 79 20 29 7b 0a 20 20 20 20  ( pEntry ){.    
b8de0 70 48 65 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e  pHead = pEntry->
b8df0 63 6f 75 6e 74 20 3f 20 70 45 6e 74 72 79 2d 3e  count ? pEntry->
b8e00 63 68 61 69 6e 20 3a 20 30 3b 0a 20 20 20 20 70  chain : 0;.    p
b8e10 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a  Entry->count++;.
b8e20 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69      pEntry->chai
b8e30 6e 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73  n = pNew;.  }els
b8e40 65 7b 0a 20 20 20 20 70 48 65 61 64 20 3d 20 30  e{.    pHead = 0
b8e50 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 65 61  ;.  }.  if( pHea
b8e60 64 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e  d ){.    pNew->n
b8e70 65 78 74 20 3d 20 70 48 65 61 64 3b 0a 20 20 20  ext = pHead;.   
b8e80 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 70 48   pNew->prev = pH
b8e90 65 61 64 2d 3e 70 72 65 76 3b 0a 20 20 20 20 69  ead->prev;.    i
b8ea0 66 28 20 70 48 65 61 64 2d 3e 70 72 65 76 20 29  f( pHead->prev )
b8eb0 7b 20 70 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e  { pHead->prev->n
b8ec0 65 78 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20  ext = pNew; }.  
b8ed0 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20    else          
b8ee0 20 20 20 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d     { pH->first =
b8ef0 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 48 65   pNew; }.    pHe
b8f00 61 64 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b  ad->prev = pNew;
b8f10 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e  .  }else{.    pN
b8f20 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 2d 3e 66  ew->next = pH->f
b8f30 69 72 73 74 3b 0a 20 20 20 20 69 66 28 20 70 48  irst;.    if( pH
b8f40 2d 3e 66 69 72 73 74 20 29 7b 20 70 48 2d 3e 66  ->first ){ pH->f
b8f50 69 72 73 74 2d 3e 70 72 65 76 20 3d 20 70 4e 65  irst->prev = pNe
b8f60 77 3b 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70  w; }.    pNew->p
b8f70 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 48 2d  rev = 0;.    pH-
b8f80 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20  >first = pNew;. 
b8f90 20 7d 0a 7d 0a 0a 0a 2f 2a 20 52 65 73 69 7a 65   }.}.../* Resize
b8fa0 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
b8fb0 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 74 61  so that it canta
b8fc0 69 6e 73 20 22 6e 65 77 5f 73 69 7a 65 22 20 62  ins "new_size" b
b8fd0 75 63 6b 65 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  uckets..**.** Th
b8fe0 65 20 68 61 73 68 20 74 61 62 6c 65 20 6d 69 67  e hash table mig
b8ff0 68 74 20 66 61 69 6c 20 74 6f 20 72 65 73 69 7a  ht fail to resiz
b9000 65 20 69 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c  e if sqlite3_mal
b9010 6c 6f 63 28 29 20 66 61 69 6c 73 20 6f 72 0a 2a  loc() fails or.*
b9020 2a 20 69 66 20 74 68 65 20 6e 65 77 20 73 69 7a  * if the new siz
b9030 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  e is the same as
b9040 20 74 68 65 20 70 72 69 6f 72 20 73 69 7a 65 2e   the prior size.
b9050 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20  .** Return TRUE 
b9060 69 66 20 74 68 65 20 72 65 73 69 7a 65 20 6f 63  if the resize oc
b9070 63 75 72 73 20 61 6e 64 20 66 61 6c 73 65 20 69  curs and false i
b9080 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  f not..*/.static
b9090 20 69 6e 74 20 72 65 68 61 73 68 28 48 61 73 68   int rehash(Hash
b90a0 20 2a 70 48 2c 20 75 6e 73 69 67 6e 65 64 20 69   *pH, unsigned i
b90b0 6e 74 20 6e 65 77 5f 73 69 7a 65 29 7b 0a 20 20  nt new_size){.  
b90c0 73 74 72 75 63 74 20 5f 68 74 20 2a 6e 65 77 5f  struct _ht *new_
b90d0 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ht;            /
b90e0 2a 20 54 68 65 20 6e 65 77 20 68 61 73 68 20 74  * The new hash t
b90f0 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c  able */.  HashEl
b9100 65 6d 20 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74 5f  em *elem, *next_
b9110 65 6c 65 6d 3b 20 20 20 20 2f 2a 20 46 6f 72 20  elem;    /* For 
b9120 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 69  looping over exi
b9130 73 74 69 6e 67 20 65 6c 65 6d 65 6e 74 73 20 2a  sting elements *
b9140 2f 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41  /..#if SQLITE_MA
b9150 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 3e  LLOC_SOFT_LIMIT>
b9160 30 0a 20 20 69 66 28 20 6e 65 77 5f 73 69 7a 65  0.  if( new_size
b9170 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f  *sizeof(struct _
b9180 68 74 29 3e 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f  ht)>SQLITE_MALLO
b9190 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 20 29 7b 0a  C_SOFT_LIMIT ){.
b91a0 20 20 20 20 6e 65 77 5f 73 69 7a 65 20 3d 20 53      new_size = S
b91b0 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46  QLITE_MALLOC_SOF
b91c0 54 5f 4c 49 4d 49 54 2f 73 69 7a 65 6f 66 28 73  T_LIMIT/sizeof(s
b91d0 74 72 75 63 74 20 5f 68 74 29 3b 0a 20 20 7d 0a  truct _ht);.  }.
b91e0 20 20 69 66 28 20 6e 65 77 5f 73 69 7a 65 3d 3d    if( new_size==
b91f0 70 48 2d 3e 68 74 73 69 7a 65 20 29 20 72 65 74  pH->htsize ) ret
b9200 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20  urn 0;.#endif.. 
b9210 20 2f 2a 20 54 68 65 20 69 6e 61 62 69 6c 69 74   /* The inabilit
b9220 79 20 74 6f 20 61 6c 6c 6f 63 61 74 65 73 20 73  y to allocates s
b9230 70 61 63 65 20 66 6f 72 20 61 20 6c 61 72 67 65  pace for a large
b9240 72 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 0a  r hash table is.
b9250 20 20 2a 2a 20 61 20 70 65 72 66 6f 72 6d 61 6e    ** a performan
b9260 63 65 20 68 69 74 20 62 75 74 20 69 74 20 69 73  ce hit but it is
b9270 20 6e 6f 74 20 61 20 66 61 74 61 6c 20 65 72 72   not a fatal err
b9280 6f 72 2e 20 20 53 6f 20 6d 61 72 6b 20 74 68 65  or.  So mark the
b9290 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e  .  ** allocation
b92a0 20 61 73 20 61 20 62 65 6e 69 67 6e 2e 0a 20 20   as a benign..  
b92b0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69  */.  sqlite3Begi
b92c0 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  nBenignMalloc();
b92d0 0a 20 20 6e 65 77 5f 68 74 20 3d 20 28 73 74 72  .  new_ht = (str
b92e0 75 63 74 20 5f 68 74 20 2a 29 73 71 6c 69 74 65  uct _ht *)sqlite
b92f0 33 4d 61 6c 6c 6f 63 28 20 6e 65 77 5f 73 69 7a  3Malloc( new_siz
b9300 65 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20  e*sizeof(struct 
b9310 5f 68 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  _ht) );.  sqlite
b9320 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  3EndBenignMalloc
b9330 28 29 3b 0a 0a 20 20 69 66 28 20 6e 65 77 5f 68  ();..  if( new_h
b9340 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  t==0 ) return 0;
b9350 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
b9360 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68  pH->ht);.  pH->h
b9370 74 20 3d 20 6e 65 77 5f 68 74 3b 0a 20 20 70 48  t = new_ht;.  pH
b9380 2d 3e 68 74 73 69 7a 65 20 3d 20 6e 65 77 5f 73  ->htsize = new_s
b9390 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  ize = sqlite3Mal
b93a0 6c 6f 63 53 69 7a 65 28 6e 65 77 5f 68 74 29 2f  locSize(new_ht)/
b93b0 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f 68  sizeof(struct _h
b93c0 74 29 3b 0a 20 20 6d 65 6d 73 65 74 28 6e 65 77  t);.  memset(new
b93d0 5f 68 74 2c 20 30 2c 20 6e 65 77 5f 73 69 7a 65  _ht, 0, new_size
b93e0 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f  *sizeof(struct _
b93f0 68 74 29 29 3b 0a 20 20 66 6f 72 28 65 6c 65 6d  ht));.  for(elem
b9400 3d 70 48 2d 3e 66 69 72 73 74 2c 20 70 48 2d 3e  =pH->first, pH->
b9410 66 69 72 73 74 3d 30 3b 20 65 6c 65 6d 3b 20 65  first=0; elem; e
b9420 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 29  lem = next_elem)
b9430 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69  {.    unsigned i
b9440 6e 74 20 68 20 3d 20 73 74 72 48 61 73 68 28 65  nt h = strHash(e
b9450 6c 65 6d 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d  lem->pKey, elem-
b9460 3e 6e 4b 65 79 29 20 25 20 6e 65 77 5f 73 69 7a  >nKey) % new_siz
b9470 65 3b 0a 20 20 20 20 6e 65 78 74 5f 65 6c 65 6d  e;.    next_elem
b9480 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20   = elem->next;. 
b9490 20 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74     insertElement
b94a0 28 70 48 2c 20 26 6e 65 77 5f 68 74 5b 68 5d 2c  (pH, &new_ht[h],
b94b0 20 65 6c 65 6d 29 3b 0a 20 20 7d 0a 20 20 72 65   elem);.  }.  re
b94c0 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 20 54 68  turn 1;.}../* Th
b94d0 69 73 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72  is function (for
b94e0 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e   internal use on
b94f0 6c 79 29 20 6c 6f 63 61 74 65 73 20 61 6e 20 65  ly) locates an e
b9500 6c 65 6d 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20  lement in an.** 
b9510 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20  hash table that 
b9520 6d 61 74 63 68 65 73 20 74 68 65 20 67 69 76 65  matches the give
b9530 6e 20 6b 65 79 2e 20 20 54 68 65 20 68 61 73 68  n key.  The hash
b9540 20 66 6f 72 20 74 68 69 73 20 6b 65 79 20 68 61   for this key ha
b9550 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65  s.** already bee
b9560 6e 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 69  n computed and i
b9570 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  s passed as the 
b9580 34 74 68 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  4th parameter..*
b9590 2f 0a 73 74 61 74 69 63 20 48 61 73 68 45 6c 65  /.static HashEle
b95a0 6d 20 2a 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69  m *findElementGi
b95b0 76 65 6e 48 61 73 68 28 0a 20 20 63 6f 6e 73 74  venHash(.  const
b95c0 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 2f   Hash *pH,     /
b95d0 2a 20 54 68 65 20 70 48 20 74 6f 20 62 65 20 73  * The pH to be s
b95e0 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e  earched */.  con
b95f0 73 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 20  st char *pKey,  
b9600 20 2f 2a 20 54 68 65 20 6b 65 79 20 77 65 20 61   /* The key we a
b9610 72 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72  re searching for
b9620 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20   */.  int nKey, 
b9630 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
b9640 65 73 20 69 6e 20 6b 65 79 20 28 6e 6f 74 20 63  es in key (not c
b9650 6f 75 6e 74 69 6e 67 20 7a 65 72 6f 20 74 65 72  ounting zero ter
b9660 6d 69 6e 61 74 6f 72 29 20 2a 2f 0a 20 20 75 6e  minator) */.  un
b9670 73 69 67 6e 65 64 20 69 6e 74 20 68 20 20 20 20  signed int h    
b9680 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 6f    /* The hash fo
b9690 72 20 74 68 69 73 20 6b 65 79 2e 20 2a 2f 0a 29  r this key. */.)
b96a0 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c  {.  HashElem *el
b96b0 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  em;             
b96c0 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f     /* Used to lo
b96d0 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c 65 6d  op thru the elem
b96e0 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e  ent list */.  in
b96f0 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20  t count;        
b9700 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
b9710 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e  Number of elemen
b9720 74 73 20 6c 65 66 74 20 74 6f 20 74 65 73 74 20  ts left to test 
b9730 2a 2f 0a 0a 20 20 69 66 28 20 70 48 2d 3e 68 74  */..  if( pH->ht
b9740 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 5f   ){.    struct _
b9750 68 74 20 2a 70 45 6e 74 72 79 20 3d 20 26 70 48  ht *pEntry = &pH
b9760 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 20 65 6c 65  ->ht[h];.    ele
b9770 6d 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61 69  m = pEntry->chai
b9780 6e 3b 0a 20 20 20 20 63 6f 75 6e 74 20 3d 20 70  n;.    count = p
b9790 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20  Entry->count;.  
b97a0 7d 65 6c 73 65 7b 0a 20 20 20 20 65 6c 65 6d 20  }else{.    elem 
b97b0 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 20  = pH->first;.   
b97c0 20 63 6f 75 6e 74 20 3d 20 70 48 2d 3e 63 6f 75   count = pH->cou
b97d0 6e 74 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28  nt;.  }.  while(
b97e0 20 63 6f 75 6e 74 2d 2d 20 26 26 20 41 4c 57 41   count-- && ALWA
b97f0 59 53 28 65 6c 65 6d 29 20 29 7b 0a 20 20 20 20  YS(elem) ){.    
b9800 69 66 28 20 65 6c 65 6d 2d 3e 6e 4b 65 79 3d 3d  if( elem->nKey==
b9810 6e 4b 65 79 20 26 26 20 73 71 6c 69 74 65 33 53  nKey && sqlite3S
b9820 74 72 4e 49 43 6d 70 28 65 6c 65 6d 2d 3e 70 4b  trNICmp(elem->pK
b9830 65 79 2c 70 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30  ey,pKey,nKey)==0
b9840 20 29 7b 20 0a 20 20 20 20 20 20 72 65 74 75 72   ){ .      retur
b9850 6e 20 65 6c 65 6d 3b 0a 20 20 20 20 7d 0a 20 20  n elem;.    }.  
b9860 20 20 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e    elem = elem->n
b9870 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ext;.  }.  retur
b9880 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76  n 0;.}../* Remov
b9890 65 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79  e a single entry
b98a0 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74   from the hash t
b98b0 61 62 6c 65 20 67 69 76 65 6e 20 61 20 70 6f 69  able given a poi
b98c0 6e 74 65 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20  nter to that.** 
b98d0 65 6c 65 6d 65 6e 74 20 61 6e 64 20 61 20 68 61  element and a ha
b98e0 73 68 20 6f 6e 20 74 68 65 20 65 6c 65 6d 65 6e  sh on the elemen
b98f0 74 27 73 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74  t's key..*/.stat
b9900 69 63 20 76 6f 69 64 20 72 65 6d 6f 76 65 45 6c  ic void removeEl
b9910 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 0a  ementGivenHash(.
b9920 20 20 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20    Hash *pH,     
b9930 20 20 20 20 2f 2a 20 54 68 65 20 70 48 20 63 6f      /* The pH co
b9940 6e 74 61 69 6e 69 6e 67 20 22 65 6c 65 6d 22 20  ntaining "elem" 
b9950 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 2a 20 65  */.  HashElem* e
b9960 6c 65 6d 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c  lem,   /* The el
b9970 65 6d 65 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f  ement to be remo
b9980 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 48 20  ved from the pH 
b9990 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  */.  unsigned in
b99a0 74 20 68 20 20 20 20 2f 2a 20 48 61 73 68 20 76  t h    /* Hash v
b99b0 61 6c 75 65 20 66 6f 72 20 74 68 65 20 65 6c 65  alue for the ele
b99c0 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72  ment */.){.  str
b99d0 75 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 3b  uct _ht *pEntry;
b99e0 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 70 72 65  .  if( elem->pre
b99f0 76 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 70  v ){.    elem->p
b9a00 72 65 76 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 6d  rev->next = elem
b9a10 2d 3e 6e 65 78 74 3b 20 0a 20 20 7d 65 6c 73 65  ->next; .  }else
b9a20 7b 0a 20 20 20 20 70 48 2d 3e 66 69 72 73 74 20  {.    pH->first 
b9a30 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20  = elem->next;.  
b9a40 7d 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65  }.  if( elem->ne
b9a50 78 74 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e  xt ){.    elem->
b9a60 6e 65 78 74 2d 3e 70 72 65 76 20 3d 20 65 6c 65  next->prev = ele
b9a70 6d 2d 3e 70 72 65 76 3b 0a 20 20 7d 0a 20 20 69  m->prev;.  }.  i
b9a80 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20  f( pH->ht ){.   
b9a90 20 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68   pEntry = &pH->h
b9aa0 74 5b 68 5d 3b 0a 20 20 20 20 69 66 28 20 70 45  t[h];.    if( pE
b9ab0 6e 74 72 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65  ntry->chain==ele
b9ac0 6d 20 29 7b 0a 20 20 20 20 20 20 70 45 6e 74 72  m ){.      pEntr
b9ad0 79 2d 3e 63 68 61 69 6e 20 3d 20 65 6c 65 6d 2d  y->chain = elem-
b9ae0 3e 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20  >next;.    }.   
b9af0 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d   pEntry->count--
b9b00 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45  ;.    assert( pE
b9b10 6e 74 72 79 2d 3e 63 6f 75 6e 74 3e 3d 30 20 29  ntry->count>=0 )
b9b20 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
b9b30 66 72 65 65 28 20 65 6c 65 6d 20 29 3b 0a 20 20  free( elem );.  
b9b40 70 48 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69  pH->count--;.  i
b9b50 66 28 20 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20  f( pH->count<=0 
b9b60 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
b9b70 48 2d 3e 66 69 72 73 74 3d 3d 30 20 29 3b 0a 20  H->first==0 );. 
b9b80 20 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 63     assert( pH->c
b9b90 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 73  ount==0 );.    s
b9ba0 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28  qlite3HashClear(
b9bb0 70 48 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41  pH);.  }.}../* A
b9bc0 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 61 74 65  ttempt to locate
b9bd0 20 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74   an element of t
b9be0 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48  he hash table pH
b9bf0 20 77 69 74 68 20 61 20 6b 65 79 0a 2a 2a 20 74   with a key.** t
b9c00 68 61 74 20 6d 61 74 63 68 65 73 20 70 4b 65 79  hat matches pKey
b9c10 2c 6e 4b 65 79 2e 20 20 52 65 74 75 72 6e 20 74  ,nKey.  Return t
b9c20 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 69 73  he data for this
b9c30 20 65 6c 65 6d 65 6e 74 20 69 66 20 69 74 20 69   element if it i
b9c40 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 6f 72 20 4e  s.** found, or N
b9c50 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73 20  ULL if there is 
b9c60 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c  no match..*/.SQL
b9c70 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
b9c80 20 2a 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e   *sqlite3HashFin
b9c90 64 28 63 6f 6e 73 74 20 48 61 73 68 20 2a 70 48  d(const Hash *pH
b9ca0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b  , const char *pK
b9cb0 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20  ey, int nKey){. 
b9cc0 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b   HashElem *elem;
b9cd0 20 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65      /* The eleme
b9ce0 6e 74 20 74 68 61 74 20 6d 61 74 63 68 65 73 20  nt that matches 
b9cf0 6b 65 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  key */.  unsigne
b9d00 64 20 69 6e 74 20 68 3b 20 20 20 20 2f 2a 20 41  d int h;    /* A
b9d10 20 68 61 73 68 20 6f 6e 20 6b 65 79 20 2a 2f 0a   hash on key */.
b9d20 0a 20 20 61 73 73 65 72 74 28 20 70 48 21 3d 30  .  assert( pH!=0
b9d30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b   );.  assert( pK
b9d40 65 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ey!=0 );.  asser
b9d50 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20  t( nKey>=0 );.  
b9d60 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20  if( pH->ht ){.  
b9d70 20 20 68 20 3d 20 73 74 72 48 61 73 68 28 70 4b    h = strHash(pK
b9d80 65 79 2c 20 6e 4b 65 79 29 20 25 20 70 48 2d 3e  ey, nKey) % pH->
b9d90 68 74 73 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b  htsize;.  }else{
b9da0 0a 20 20 20 20 68 20 3d 20 30 3b 0a 20 20 7d 0a  .    h = 0;.  }.
b9db0 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65    elem = findEle
b9dc0 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70 48  mentGivenHash(pH
b9dd0 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 68 29  , pKey, nKey, h)
b9de0 3b 0a 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 20  ;.  return elem 
b9df0 3f 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 30  ? elem->data : 0
b9e00 3b 0a 7d 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61  ;.}../* Insert a
b9e10 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74  n element into t
b9e20 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48  he hash table pH
b9e30 2e 20 20 54 68 65 20 6b 65 79 20 69 73 20 70 4b  .  The key is pK
b9e40 65 79 2c 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 74  ey,nKey.** and t
b9e50 68 65 20 64 61 74 61 20 69 73 20 22 64 61 74 61  he data is "data
b9e60 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65  "..**.** If no e
b9e70 6c 65 6d 65 6e 74 20 65 78 69 73 74 73 20 77 69  lement exists wi
b9e80 74 68 20 61 20 6d 61 74 63 68 69 6e 67 20 6b 65  th a matching ke
b9e90 79 2c 20 74 68 65 6e 20 61 20 6e 65 77 0a 2a 2a  y, then a new.**
b9ea0 20 65 6c 65 6d 65 6e 74 20 69 73 20 63 72 65 61   element is crea
b9eb0 74 65 64 20 61 6e 64 20 4e 55 4c 4c 20 69 73 20  ted and NULL is 
b9ec0 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
b9ed0 49 66 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65  If another eleme
b9ee0 6e 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  nt already exist
b9ef0 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  s with the same 
b9f00 6b 65 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a  key, then the.**
b9f10 20 6e 65 77 20 64 61 74 61 20 72 65 70 6c 61 63   new data replac
b9f20 65 73 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20  es the old data 
b9f30 61 6e 64 20 74 68 65 20 6f 6c 64 20 64 61 74 61  and the old data
b9f40 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
b9f50 20 54 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20   The key is not 
b9f60 63 6f 70 69 65 64 20 69 6e 20 74 68 69 73 20 69  copied in this i
b9f70 6e 73 74 61 6e 63 65 2e 20 20 49 66 20 61 20 6d  nstance.  If a m
b9f80 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68 65  alloc fails, the
b9f90 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74  n.** the new dat
b9fa0 61 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  a is returned an
b9fb0 64 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  d the hash table
b9fc0 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a   is unchanged..*
b9fd0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 64 61 74  *.** If the "dat
b9fe0 61 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  a" parameter to 
b9ff0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
ba000 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a   NULL, then the.
ba010 2a 2a 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65  ** element corre
ba020 73 70 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79  sponding to "key
ba030 22 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f  " is removed fro
ba040 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  m the hash table
ba050 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
ba060 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
ba070 33 48 61 73 68 49 6e 73 65 72 74 28 48 61 73 68  3HashInsert(Hash
ba080 20 2a 70 48 2c 20 63 6f 6e 73 74 20 63 68 61 72   *pH, const char
ba090 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79   *pKey, int nKey
ba0a0 2c 20 76 6f 69 64 20 2a 64 61 74 61 29 7b 0a 20  , void *data){. 
ba0b0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 3b   unsigned int h;
ba0c0 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61         /* the ha
ba0d0 73 68 20 6f 66 20 74 68 65 20 6b 65 79 20 6d 6f  sh of the key mo
ba0e0 64 75 6c 6f 20 68 61 73 68 20 74 61 62 6c 65 20  dulo hash table 
ba0f0 73 69 7a 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c  size */.  HashEl
ba100 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20  em *elem;       
ba110 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20  /* Used to loop 
ba120 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74  thru the element
ba130 20 6c 69 73 74 20 2a 2f 0a 20 20 48 61 73 68 45   list */.  HashE
ba140 6c 65 6d 20 2a 6e 65 77 5f 65 6c 65 6d 3b 20 20  lem *new_elem;  
ba150 20 2f 2a 20 4e 65 77 20 65 6c 65 6d 65 6e 74 20   /* New element 
ba160 61 64 64 65 64 20 74 6f 20 74 68 65 20 70 48 20  added to the pH 
ba170 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 48  */..  assert( pH
ba180 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
ba190 20 70 4b 65 79 21 3d 30 20 29 3b 0a 20 20 61 73   pKey!=0 );.  as
ba1a0 73 65 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b  sert( nKey>=0 );
ba1b0 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a  .  if( pH->htsiz
ba1c0 65 20 29 7b 0a 20 20 20 20 68 20 3d 20 73 74 72  e ){.    h = str
ba1d0 48 61 73 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29  Hash(pKey, nKey)
ba1e0 20 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20   % pH->htsize;. 
ba1f0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20   }else{.    h = 
ba200 30 3b 0a 20 20 7d 0a 20 20 65 6c 65 6d 20 3d 20  0;.  }.  elem = 
ba210 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e  findElementGiven
ba220 48 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65  Hash(pH,pKey,nKe
ba230 79 2c 68 29 3b 0a 20 20 69 66 28 20 65 6c 65 6d  y,h);.  if( elem
ba240 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c   ){.    void *ol
ba250 64 5f 64 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64  d_data = elem->d
ba260 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64 61 74  ata;.    if( dat
ba270 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  a==0 ){.      re
ba280 6d 6f 76 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e  moveElementGiven
ba290 48 61 73 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b  Hash(pH,elem,h);
ba2a0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
ba2b0 20 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64    elem->data = d
ba2c0 61 74 61 3b 0a 20 20 20 20 20 20 65 6c 65 6d 2d  ata;.      elem-
ba2d0 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20  >pKey = pKey;.  
ba2e0 20 20 20 20 61 73 73 65 72 74 28 6e 4b 65 79 3d      assert(nKey=
ba2f0 3d 65 6c 65 6d 2d 3e 6e 4b 65 79 29 3b 0a 20 20  =elem->nKey);.  
ba300 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6f    }.    return o
ba310 6c 64 5f 64 61 74 61 3b 0a 20 20 7d 0a 20 20 69  ld_data;.  }.  i
ba320 66 28 20 64 61 74 61 3d 3d 30 20 29 20 72 65 74  f( data==0 ) ret
ba330 75 72 6e 20 30 3b 0a 20 20 6e 65 77 5f 65 6c 65  urn 0;.  new_ele
ba340 6d 20 3d 20 28 48 61 73 68 45 6c 65 6d 2a 29 73  m = (HashElem*)s
ba350 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73 69  qlite3Malloc( si
ba360 7a 65 6f 66 28 48 61 73 68 45 6c 65 6d 29 20 29  zeof(HashElem) )
ba370 3b 0a 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d  ;.  if( new_elem
ba380 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 64 61 74  ==0 ) return dat
ba390 61 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70  a;.  new_elem->p
ba3a0 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 6e 65  Key = pKey;.  ne
ba3b0 77 5f 65 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e  w_elem->nKey = n
ba3c0 4b 65 79 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d  Key;.  new_elem-
ba3d0 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 20  >data = data;.  
ba3e0 70 48 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69  pH->count++;.  i
ba3f0 66 28 20 70 48 2d 3e 63 6f 75 6e 74 3e 3d 31 30  f( pH->count>=10
ba400 20 26 26 20 70 48 2d 3e 63 6f 75 6e 74 20 3e 20   && pH->count > 
ba410 32 2a 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a  2*pH->htsize ){.
ba420 20 20 20 20 69 66 28 20 72 65 68 61 73 68 28 70      if( rehash(p
ba430 48 2c 20 70 48 2d 3e 63 6f 75 6e 74 2a 32 29 20  H, pH->count*2) 
ba440 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
ba450 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29 3b   pH->htsize>0 );
ba460 0a 20 20 20 20 20 20 68 20 3d 20 73 74 72 48 61  .      h = strHa
ba470 73 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25  sh(pKey, nKey) %
ba480 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 20   pH->htsize;.   
ba490 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d   }.  }.  if( pH-
ba4a0 3e 68 74 20 29 7b 0a 20 20 20 20 69 6e 73 65 72  >ht ){.    inser
ba4b0 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 70 48  tElement(pH, &pH
ba4c0 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c 65  ->ht[h], new_ele
ba4d0 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  m);.  }else{.   
ba4e0 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70   insertElement(p
ba4f0 48 2c 20 30 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b  H, 0, new_elem);
ba500 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
ba510 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
ba520 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73 68 2e  *** End of hash.
ba530 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
ba540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba560 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
ba570 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f  *** Begin file o
ba580 70 63 6f 64 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a  pcodes.c *******
ba590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba5b0 2a 2a 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63  **/./* Automatic
ba5c0 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20  ally generated. 
ba5d0 20 44 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a   Do not edit */.
ba5e0 2f 2a 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63  /* See the mkopc
ba5f0 6f 64 65 63 2e 61 77 6b 20 73 63 72 69 70 74 20  odec.awk script 
ba600 66 6f 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a  for details. */.
ba610 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
ba620 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e  ITE_OMIT_EXPLAIN
ba630 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44  ) || !defined(ND
ba640 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64  EBUG) || defined
ba650 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c  (VDBE_PROFILE) |
ba660 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
ba670 5f 44 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50  _DEBUG).SQLITE_P
ba680 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
ba690 72 20 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 65  r *sqlite3Opcode
ba6a0 4e 61 6d 65 28 69 6e 74 20 69 29 7b 0a 20 73 74  Name(int i){. st
ba6b0 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
ba6c0 2a 63 6f 6e 73 74 20 61 7a 4e 61 6d 65 5b 5d 20  *const azName[] 
ba6d0 3d 20 7b 20 22 3f 22 2c 0a 20 20 20 20 20 2f 2a  = { "?",.     /*
ba6e0 20 20 20 31 20 2a 2f 20 22 56 4e 65 78 74 22 2c     1 */ "VNext",
ba6f0 0a 20 20 20 20 20 2f 2a 20 20 20 32 20 2a 2f 20  .     /*   2 */ 
ba700 22 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 20  "Affinity",.    
ba710 20 2f 2a 20 20 20 33 20 2a 2f 20 22 43 6f 6c 75   /*   3 */ "Colu
ba720 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 34  mn",.     /*   4
ba730 20 2a 2f 20 22 53 65 74 43 6f 6f 6b 69 65 22 2c   */ "SetCookie",
ba740 0a 20 20 20 20 20 2f 2a 20 20 20 35 20 2a 2f 20  .     /*   5 */ 
ba750 22 53 65 65 6b 22 2c 0a 20 20 20 20 20 2f 2a 20  "Seek",.     /* 
ba760 20 20 36 20 2a 2f 20 22 53 65 71 75 65 6e 63 65    6 */ "Sequence
ba770 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 37 20 2a  ",.     /*   7 *
ba780 2f 20 22 53 61 76 65 70 6f 69 6e 74 22 2c 0a 20  / "Savepoint",. 
ba790 20 20 20 20 2f 2a 20 20 20 38 20 2a 2f 20 22 52      /*   8 */ "R
ba7a0 6f 77 4b 65 79 22 2c 0a 20 20 20 20 20 2f 2a 20  owKey",.     /* 
ba7b0 20 20 39 20 2a 2f 20 22 53 43 6f 70 79 22 2c 0a    9 */ "SCopy",.
ba7c0 20 20 20 20 20 2f 2a 20 20 31 30 20 2a 2f 20 22       /*  10 */ "
ba7d0 4f 70 65 6e 57 72 69 74 65 22 2c 0a 20 20 20 20  OpenWrite",.    
ba7e0 20 2f 2a 20 20 31 31 20 2a 2f 20 22 49 66 22 2c   /*  11 */ "If",
ba7f0 0a 20 20 20 20 20 2f 2a 20 20 31 32 20 2a 2f 20  .     /*  12 */ 
ba800 22 43 6f 6c 6c 53 65 71 22 2c 0a 20 20 20 20 20  "CollSeq",.     
ba810 2f 2a 20 20 31 33 20 2a 2f 20 22 4f 70 65 6e 52  /*  13 */ "OpenR
ba820 65 61 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31  ead",.     /*  1
ba830 34 20 2a 2f 20 22 45 78 70 69 72 65 22 2c 0a 20  4 */ "Expire",. 
ba840 20 20 20 20 2f 2a 20 20 31 35 20 2a 2f 20 22 41      /*  15 */ "A
ba850 75 74 6f 43 6f 6d 6d 69 74 22 2c 0a 20 20 20 20  utoCommit",.    
ba860 20 2f 2a 20 20 31 36 20 2a 2f 20 22 50 61 67 65   /*  16 */ "Page
ba870 63 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20  count",.     /* 
ba880 20 31 37 20 2a 2f 20 22 49 6e 74 65 67 72 69 74   17 */ "Integrit
ba890 79 43 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31  yCk",.     /*  1
ba8a0 38 20 2a 2f 20 22 53 6f 72 74 22 2c 0a 20 20 20  8 */ "Sort",.   
ba8b0 20 20 2f 2a 20 20 31 39 20 2a 2f 20 22 4e 6f 74    /*  19 */ "Not
ba8c0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 30 20 2a  ",.     /*  20 *
ba8d0 2f 20 22 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f  / "Copy",.     /
ba8e0 2a 20 20 32 31 20 2a 2f 20 22 54 72 61 63 65 22  *  21 */ "Trace"
ba8f0 2c 0a 20 20 20 20 20 2f 2a 20 20 32 32 20 2a 2f  ,.     /*  22 */
ba900 20 22 46 75 6e 63 74 69 6f 6e 22 2c 0a 20 20 20   "Function",.   
ba910 20 20 2f 2a 20 20 32 33 20 2a 2f 20 22 49 66 4e    /*  23 */ "IfN
ba920 65 67 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 34  eg",.     /*  24
ba930 20 2a 2f 20 22 4e 6f 6f 70 22 2c 0a 20 20 20 20   */ "Noop",.    
ba940 20 2f 2a 20 20 32 35 20 2a 2f 20 22 50 72 6f 67   /*  25 */ "Prog
ba950 72 61 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32  ram",.     /*  2
ba960 36 20 2a 2f 20 22 52 65 74 75 72 6e 22 2c 0a 20  6 */ "Return",. 
ba970 20 20 20 20 2f 2a 20 20 32 37 20 2a 2f 20 22 4e      /*  27 */ "N
ba980 65 77 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f  ewRowid",.     /
ba990 2a 20 20 32 38 20 2a 2f 20 22 46 6b 43 6f 75 6e  *  28 */ "FkCoun
ba9a0 74 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32  ter",.     /*  2
ba9b0 39 20 2a 2f 20 22 56 61 72 69 61 62 6c 65 22 2c  9 */ "Variable",
ba9c0 0a 20 20 20 20 20 2f 2a 20 20 33 30 20 2a 2f 20  .     /*  30 */ 
ba9d0 22 53 74 72 69 6e 67 22 2c 0a 20 20 20 20 20 2f  "String",.     /
ba9e0 2a 20 20 33 31 20 2a 2f 20 22 52 65 61 6c 41 66  *  31 */ "RealAf
ba9f0 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 20 2f 2a  finity",.     /*
baa00 20 20 33 32 20 2a 2f 20 22 56 52 65 6e 61 6d 65    32 */ "VRename
baa10 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 33 20 2a  ",.     /*  33 *
baa20 2f 20 22 50 61 72 73 65 53 63 68 65 6d 61 22 2c  / "ParseSchema",
baa30 0a 20 20 20 20 20 2f 2a 20 20 33 34 20 2a 2f 20  .     /*  34 */ 
baa40 22 56 4f 70 65 6e 22 2c 0a 20 20 20 20 20 2f 2a  "VOpen",.     /*
baa50 20 20 33 35 20 2a 2f 20 22 43 6c 6f 73 65 22 2c    35 */ "Close",
baa60 0a 20 20 20 20 20 2f 2a 20 20 33 36 20 2a 2f 20  .     /*  36 */ 
baa70 22 43 72 65 61 74 65 49 6e 64 65 78 22 2c 0a 20  "CreateIndex",. 
baa80 20 20 20 20 2f 2a 20 20 33 37 20 2a 2f 20 22 49      /*  37 */ "I
baa90 73 55 6e 69 71 75 65 22 2c 0a 20 20 20 20 20 2f  sUnique",.     /
baaa0 2a 20 20 33 38 20 2a 2f 20 22 4e 6f 74 46 6f 75  *  38 */ "NotFou
baab0 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 39  nd",.     /*  39
baac0 20 2a 2f 20 22 49 6e 74 36 34 22 2c 0a 20 20 20   */ "Int64",.   
baad0 20 20 2f 2a 20 20 34 30 20 2a 2f 20 22 4d 75 73    /*  40 */ "Mus
baae0 74 42 65 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a  tBeInt",.     /*
baaf0 20 20 34 31 20 2a 2f 20 22 48 61 6c 74 22 2c 0a    41 */ "Halt",.
bab00 20 20 20 20 20 2f 2a 20 20 34 32 20 2a 2f 20 22       /*  42 */ "
bab10 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a 20  Rowid",.     /* 
bab20 20 34 33 20 2a 2f 20 22 49 64 78 4c 54 22 2c 0a   43 */ "IdxLT",.
bab30 20 20 20 20 20 2f 2a 20 20 34 34 20 2a 2f 20 22       /*  44 */ "
bab40 41 64 64 49 6d 6d 22 2c 0a 20 20 20 20 20 2f 2a  AddImm",.     /*
bab50 20 20 34 35 20 2a 2f 20 22 52 6f 77 44 61 74 61    45 */ "RowData
bab60 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 36 20 2a  ",.     /*  46 *
bab70 2f 20 22 4d 65 6d 4d 61 78 22 2c 0a 20 20 20 20  / "MemMax",.    
bab80 20 2f 2a 20 20 34 37 20 2a 2f 20 22 4e 6f 74 45   /*  47 */ "NotE
bab90 78 69 73 74 73 22 2c 0a 20 20 20 20 20 2f 2a 20  xists",.     /* 
baba0 20 34 38 20 2a 2f 20 22 47 6f 73 75 62 22 2c 0a   48 */ "Gosub",.
babb0 20 20 20 20 20 2f 2a 20 20 34 39 20 2a 2f 20 22       /*  49 */ "
babc0 49 6e 74 65 67 65 72 22 2c 0a 20 20 20 20 20 2f  Integer",.     /
babd0 2a 20 20 35 30 20 2a 2f 20 22 50 72 65 76 22 2c  *  50 */ "Prev",
babe0 0a 20 20 20 20 20 2f 2a 20 20 35 31 20 2a 2f 20  .     /*  51 */ 
babf0 22 52 6f 77 53 65 74 52 65 61 64 22 2c 0a 20 20  "RowSetRead",.  
bac00 20 20 20 2f 2a 20 20 35 32 20 2a 2f 20 22 52 6f     /*  52 */ "Ro
bac10 77 53 65 74 41 64 64 22 2c 0a 20 20 20 20 20 2f  wSetAdd",.     /
bac20 2a 20 20 35 33 20 2a 2f 20 22 56 43 6f 6c 75 6d  *  53 */ "VColum
bac30 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 34 20  n",.     /*  54 
bac40 2a 2f 20 22 43 72 65 61 74 65 54 61 62 6c 65 22  */ "CreateTable"
bac50 2c 0a 20 20 20 20 20 2f 2a 20 20 35 35 20 2a 2f  ,.     /*  55 */
bac60 20 22 4c 61 73 74 22 2c 0a 20 20 20 20 20 2f 2a   "Last",.     /*
bac70 20 20 35 36 20 2a 2f 20 22 53 65 65 6b 4c 65 22    56 */ "SeekLe"
bac80 2c 0a 20 20 20 20 20 2f 2a 20 20 35 37 20 2a 2f  ,.     /*  57 */
bac90 20 22 49 6e 63 72 56 61 63 75 75 6d 22 2c 0a 20   "IncrVacuum",. 
baca0 20 20 20 20 2f 2a 20 20 35 38 20 2a 2f 20 22 49      /*  58 */ "I
bacb0 64 78 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f  dxRowid",.     /
bacc0 2a 20 20 35 39 20 2a 2f 20 22 52 65 73 65 74 43  *  59 */ "ResetC
bacd0 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  ount",.     /*  
bace0 36 30 20 2a 2f 20 22 59 69 65 6c 64 22 2c 0a 20  60 */ "Yield",. 
bacf0 20 20 20 20 2f 2a 20 20 36 31 20 2a 2f 20 22 44      /*  61 */ "D
bad00 72 6f 70 54 72 69 67 67 65 72 22 2c 0a 20 20 20  ropTrigger",.   
bad10 20 20 2f 2a 20 20 36 32 20 2a 2f 20 22 44 72 6f    /*  62 */ "Dro
bad20 70 49 6e 64 65 78 22 2c 0a 20 20 20 20 20 2f 2a  pIndex",.     /*
bad30 20 20 36 33 20 2a 2f 20 22 50 61 72 61 6d 22 2c    63 */ "Param",
bad40 0a 20 20 20 20 20 2f 2a 20 20 36 34 20 2a 2f 20  .     /*  64 */ 
bad50 22 49 64 78 47 45 22 2c 0a 20 20 20 20 20 2f 2a  "IdxGE",.     /*
bad60 20 20 36 35 20 2a 2f 20 22 49 64 78 44 65 6c 65    65 */ "IdxDele
bad70 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 36  te",.     /*  66
bad80 20 2a 2f 20 22 56 61 63 75 75 6d 22 2c 0a 20 20   */ "Vacuum",.  
bad90 20 20 20 2f 2a 20 20 36 37 20 2a 2f 20 22 49 66     /*  67 */ "If
bada0 4e 6f 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36  Not",.     /*  6
badb0 38 20 2a 2f 20 22 4f 72 22 2c 0a 20 20 20 20 20  8 */ "Or",.     
badc0 2f 2a 20 20 36 39 20 2a 2f 20 22 41 6e 64 22 2c  /*  69 */ "And",
badd0 0a 20 20 20 20 20 2f 2a 20 20 37 30 20 2a 2f 20  .     /*  70 */ 
bade0 22 44 72 6f 70 54 61 62 6c 65 22 2c 0a 20 20 20  "DropTable",.   
badf0 20 20 2f 2a 20 20 37 31 20 2a 2f 20 22 53 65 65    /*  71 */ "See
bae00 6b 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37  kLt",.     /*  7
bae10 32 20 2a 2f 20 22 4d 61 6b 65 52 65 63 6f 72 64  2 */ "MakeRecord
bae20 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 33 20 2a  ",.     /*  73 *
bae30 2f 20 22 49 73 4e 75 6c 6c 22 2c 0a 20 20 20 20  / "IsNull",.    
bae40 20 2f 2a 20 20 37 34 20 2a 2f 20 22 4e 6f 74 4e   /*  74 */ "NotN
bae50 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37  ull",.     /*  7
bae60 35 20 2a 2f 20 22 4e 65 22 2c 0a 20 20 20 20 20  5 */ "Ne",.     
bae70 2f 2a 20 20 37 36 20 2a 2f 20 22 45 71 22 2c 0a  /*  76 */ "Eq",.
bae80 20 20 20 20 20 2f 2a 20 20 37 37 20 2a 2f 20 22       /*  77 */ "
bae90 47 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 38  Gt",.     /*  78
baea0 20 2a 2f 20 22 4c 65 22 2c 0a 20 20 20 20 20 2f   */ "Le",.     /
baeb0 2a 20 20 37 39 20 2a 2f 20 22 4c 74 22 2c 0a 20  *  79 */ "Lt",. 
baec0 20 20 20 20 2f 2a 20 20 38 30 20 2a 2f 20 22 47      /*  80 */ "G
baed0 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 31 20  e",.     /*  81 
baee0 2a 2f 20 22 52 65 73 75 6c 74 52 6f 77 22 2c 0a  */ "ResultRow",.
baef0 20 20 20 20 20 2f 2a 20 20 38 32 20 2a 2f 20 22       /*  82 */ "
baf00 42 69 74 41 6e 64 22 2c 0a 20 20 20 20 20 2f 2a  BitAnd",.     /*
baf10 20 20 38 33 20 2a 2f 20 22 42 69 74 4f 72 22 2c    83 */ "BitOr",
baf20 0a 20 20 20 20 20 2f 2a 20 20 38 34 20 2a 2f 20  .     /*  84 */ 
baf30 22 53 68 69 66 74 4c 65 66 74 22 2c 0a 20 20 20  "ShiftLeft",.   
baf40 20 20 2f 2a 20 20 38 35 20 2a 2f 20 22 53 68 69    /*  85 */ "Shi
baf50 66 74 52 69 67 68 74 22 2c 0a 20 20 20 20 20 2f  ftRight",.     /
baf60 2a 20 20 38 36 20 2a 2f 20 22 41 64 64 22 2c 0a  *  86 */ "Add",.
baf70 20 20 20 20 20 2f 2a 20 20 38 37 20 2a 2f 20 22       /*  87 */ "
baf80 53 75 62 74 72 61 63 74 22 2c 0a 20 20 20 20 20  Subtract",.     
baf90 2f 2a 20 20 38 38 20 2a 2f 20 22 4d 75 6c 74 69  /*  88 */ "Multi
bafa0 70 6c 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38  ply",.     /*  8
bafb0 39 20 2a 2f 20 22 44 69 76 69 64 65 22 2c 0a 20  9 */ "Divide",. 
bafc0 20 20 20 20 2f 2a 20 20 39 30 20 2a 2f 20 22 52      /*  90 */ "R
bafd0 65 6d 61 69 6e 64 65 72 22 2c 0a 20 20 20 20 20  emainder",.     
bafe0 2f 2a 20 20 39 31 20 2a 2f 20 22 43 6f 6e 63 61  /*  91 */ "Conca
baff0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 32 20  t",.     /*  92 
bb000 2a 2f 20 22 44 65 6c 65 74 65 22 2c 0a 20 20 20  */ "Delete",.   
bb010 20 20 2f 2a 20 20 39 33 20 2a 2f 20 22 42 69 74    /*  93 */ "Bit
bb020 4e 6f 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39  Not",.     /*  9
bb030 34 20 2a 2f 20 22 53 74 72 69 6e 67 38 22 2c 0a  4 */ "String8",.
bb040 20 20 20 20 20 2f 2a 20 20 39 35 20 2a 2f 20 22       /*  95 */ "
bb050 41 67 67 46 69 6e 61 6c 22 2c 0a 20 20 20 20 20  AggFinal",.     
bb060 2f 2a 20 20 39 36 20 2a 2f 20 22 43 6f 6d 70 61  /*  96 */ "Compa
bb070 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 37  re",.     /*  97
bb080 20 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20 20 20 20   */ "Goto",.    
bb090 20 2f 2a 20 20 39 38 20 2a 2f 20 22 54 61 62 6c   /*  98 */ "Tabl
bb0a0 65 4c 6f 63 6b 22 2c 0a 20 20 20 20 20 2f 2a 20  eLock",.     /* 
bb0b0 20 39 39 20 2a 2f 20 22 43 6c 65 61 72 22 2c 0a   99 */ "Clear",.
bb0c0 20 20 20 20 20 2f 2a 20 31 30 30 20 2a 2f 20 22       /* 100 */ "
bb0d0 56 65 72 69 66 79 43 6f 6f 6b 69 65 22 2c 0a 20  VerifyCookie",. 
bb0e0 20 20 20 20 2f 2a 20 31 30 31 20 2a 2f 20 22 41      /* 101 */ "A
bb0f0 67 67 53 74 65 70 22 2c 0a 20 20 20 20 20 2f 2a  ggStep",.     /*
bb100 20 31 30 32 20 2a 2f 20 22 54 72 61 6e 73 61 63   102 */ "Transac
bb110 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31  tion",.     /* 1
bb120 30 33 20 2a 2f 20 22 56 46 69 6c 74 65 72 22 2c  03 */ "VFilter",
bb130 0a 20 20 20 20 20 2f 2a 20 31 30 34 20 2a 2f 20  .     /* 104 */ 
bb140 22 56 44 65 73 74 72 6f 79 22 2c 0a 20 20 20 20  "VDestroy",.    
bb150 20 2f 2a 20 31 30 35 20 2a 2f 20 22 4e 65 78 74   /* 105 */ "Next
bb160 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 36 20 2a  ",.     /* 106 *
bb170 2f 20 22 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20  / "Count",.     
bb180 2f 2a 20 31 30 37 20 2a 2f 20 22 49 64 78 49 6e  /* 107 */ "IdxIn
bb190 73 65 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31  sert",.     /* 1
bb1a0 30 38 20 2a 2f 20 22 46 6b 49 66 5a 65 72 6f 22  08 */ "FkIfZero"
bb1b0 2c 0a 20 20 20 20 20 2f 2a 20 31 30 39 20 2a 2f  ,.     /* 109 */
bb1c0 20 22 53 65 65 6b 47 65 22 2c 0a 20 20 20 20 20   "SeekGe",.     
bb1d0 2f 2a 20 31 31 30 20 2a 2f 20 22 49 6e 73 65 72  /* 110 */ "Inser
bb1e0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 31 20  t",.     /* 111 
bb1f0 2a 2f 20 22 44 65 73 74 72 6f 79 22 2c 0a 20 20  */ "Destroy",.  
bb200 20 20 20 2f 2a 20 31 31 32 20 2a 2f 20 22 52 65     /* 112 */ "Re
bb210 61 64 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20  adCookie",.     
bb220 2f 2a 20 31 31 33 20 2a 2f 20 22 52 6f 77 53 65  /* 113 */ "RowSe
bb230 74 54 65 73 74 22 2c 0a 20 20 20 20 20 2f 2a 20  tTest",.     /* 
bb240 31 31 34 20 2a 2f 20 22 4c 6f 61 64 41 6e 61 6c  114 */ "LoadAnal
bb250 79 73 69 73 22 2c 0a 20 20 20 20 20 2f 2a 20 31  ysis",.     /* 1
bb260 31 35 20 2a 2f 20 22 45 78 70 6c 61 69 6e 22 2c  15 */ "Explain",
bb270 0a 20 20 20 20 20 2f 2a 20 31 31 36 20 2a 2f 20  .     /* 116 */ 
bb280 22 48 61 6c 74 49 66 4e 75 6c 6c 22 2c 0a 20 20  "HaltIfNull",.  
bb290 20 20 20 2f 2a 20 31 31 37 20 2a 2f 20 22 4f 70     /* 117 */ "Op
bb2a0 65 6e 50 73 65 75 64 6f 22 2c 0a 20 20 20 20 20  enPseudo",.     
bb2b0 2f 2a 20 31 31 38 20 2a 2f 20 22 4f 70 65 6e 45  /* 118 */ "OpenE
bb2c0 70 68 65 6d 65 72 61 6c 22 2c 0a 20 20 20 20 20  phemeral",.     
bb2d0 2f 2a 20 31 31 39 20 2a 2f 20 22 4e 75 6c 6c 22  /* 119 */ "Null"
bb2e0 2c 0a 20 20 20 20 20 2f 2a 20 31 32 30 20 2a 2f  ,.     /* 120 */
bb2f0 20 22 4d 6f 76 65 22 2c 0a 20 20 20 20 20 2f 2a   "Move",.     /*
bb300 20 31 32 31 20 2a 2f 20 22 42 6c 6f 62 22 2c 0a   121 */ "Blob",.
bb310 20 20 20 20 20 2f 2a 20 31 32 32 20 2a 2f 20 22       /* 122 */ "
bb320 52 65 77 69 6e 64 22 2c 0a 20 20 20 20 20 2f 2a  Rewind",.     /*
bb330 20 31 32 33 20 2a 2f 20 22 53 65 65 6b 47 74 22   123 */ "SeekGt"
bb340 2c 0a 20 20 20 20 20 2f 2a 20 31 32 34 20 2a 2f  ,.     /* 124 */
bb350 20 22 56 42 65 67 69 6e 22 2c 0a 20 20 20 20 20   "VBegin",.     
bb360 2f 2a 20 31 32 35 20 2a 2f 20 22 56 55 70 64 61  /* 125 */ "VUpda
bb370 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 36  te",.     /* 126
bb380 20 2a 2f 20 22 49 66 5a 65 72 6f 22 2c 0a 20 20   */ "IfZero",.  
bb390 20 20 20 2f 2a 20 31 32 37 20 2a 2f 20 22 56 43     /* 127 */ "VC
bb3a0 72 65 61 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20  reate",.     /* 
bb3b0 31 32 38 20 2a 2f 20 22 46 6f 75 6e 64 22 2c 0a  128 */ "Found",.
bb3c0 20 20 20 20 20 2f 2a 20 31 32 39 20 2a 2f 20 22       /* 129 */ "
bb3d0 49 66 50 6f 73 22 2c 0a 20 20 20 20 20 2f 2a 20  IfPos",.     /* 
bb3e0 31 33 30 20 2a 2f 20 22 52 65 61 6c 22 2c 0a 20  130 */ "Real",. 
bb3f0 20 20 20 20 2f 2a 20 31 33 31 20 2a 2f 20 22 4e      /* 131 */ "N
bb400 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a  ullRow",.     /*
bb410 20 31 33 32 20 2a 2f 20 22 4a 75 6d 70 22 2c 0a   132 */ "Jump",.
bb420 20 20 20 20 20 2f 2a 20 31 33 33 20 2a 2f 20 22       /* 133 */ "
bb430 50 65 72 6d 75 74 61 74 69 6f 6e 22 2c 0a 20 20  Permutation",.  
bb440 20 20 20 2f 2a 20 31 33 34 20 2a 2f 20 22 4e 6f     /* 134 */ "No
bb450 74 55 73 65 64 5f 31 33 34 22 2c 0a 20 20 20 20  tUsed_134",.    
bb460 20 2f 2a 20 31 33 35 20 2a 2f 20 22 4e 6f 74 55   /* 135 */ "NotU
bb470 73 65 64 5f 31 33 35 22 2c 0a 20 20 20 20 20 2f  sed_135",.     /
bb480 2a 20 31 33 36 20 2a 2f 20 22 4e 6f 74 55 73 65  * 136 */ "NotUse
bb490 64 5f 31 33 36 22 2c 0a 20 20 20 20 20 2f 2a 20  d_136",.     /* 
bb4a0 31 33 37 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f  137 */ "NotUsed_
bb4b0 31 33 37 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33  137",.     /* 13
bb4c0 38 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33  8 */ "NotUsed_13
bb4d0 38 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 39 20  8",.     /* 139 
bb4e0 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 39 22  */ "NotUsed_139"
bb4f0 2c 0a 20 20 20 20 20 2f 2a 20 31 34 30 20 2a 2f  ,.     /* 140 */
bb500 20 22 4e 6f 74 55 73 65 64 5f 31 34 30 22 2c 0a   "NotUsed_140",.
bb510 20 20 20 20 20 2f 2a 20 31 34 31 20 2a 2f 20 22       /* 141 */ "
bb520 54 6f 54 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a  ToText",.     /*
bb530 20 31 34 32 20 2a 2f 20 22 54 6f 42 6c 6f 62 22   142 */ "ToBlob"
bb540 2c 0a 20 20 20 20 20 2f 2a 20 31 34 33 20 2a 2f  ,.     /* 143 */
bb550 20 22 54 6f 4e 75 6d 65 72 69 63 22 2c 0a 20 20   "ToNumeric",.  
bb560 20 20 20 2f 2a 20 31 34 34 20 2a 2f 20 22 54 6f     /* 144 */ "To
bb570 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34  Int",.     /* 14
bb580 35 20 2a 2f 20 22 54 6f 52 65 61 6c 22 2c 0a 20  5 */ "ToReal",. 
bb590 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20 61 7a 4e   };.  return azN
bb5a0 61 6d 65 5b 69 5d 3b 0a 7d 0a 23 65 6e 64 69 66  ame[i];.}.#endif
bb5b0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
bb5c0 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65 73  * End of opcodes
bb5d0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
bb5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb600 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
bb610 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f  * Begin file os_
bb620 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  os2.c **********
bb630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb650 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 46 65 62  /./*.** 2006 Feb
bb660 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   14.**.** The au
bb670 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
bb680 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
bb690 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
bb6a0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
bb6b0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
bb6c0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
bb6d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
bb6e0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
bb6f0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
bb700 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
bb710 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
bb720 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
bb730 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
bb740 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
bb750 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
bb760 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
bb770 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
bb780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb7c0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
bb7d0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
bb7e0 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 73 70   code that is sp
bb7f0 65 63 69 66 69 63 20 74 6f 20 4f 53 2f 32 2e 0a  ecific to OS/2..
bb800 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 6f 73  **.** $Id: os_os
bb810 32 2e 63 2c 76 20 31 2e 36 33 20 32 30 30 38 2f  2.c,v 1.63 2008/
bb820 31 32 2f 31 30 20 31 39 3a 32 36 3a 32 34 20 64  12/10 19:26:24 d
bb830 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 23 69  rh Exp $.*/...#i
bb840 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a  f SQLITE_OS_OS2.
bb850 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62  ./*.** A Note Ab
bb860 6f 75 74 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63  out Memory Alloc
bb870 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69  ation:.**.** Thi
bb880 73 20 64 72 69 76 65 72 20 75 73 65 73 20 6d 61  s driver uses ma
bb890 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 20 64 69  lloc()/free() di
bb8a0 72 65 63 74 6c 79 20 72 61 74 68 65 72 20 74 68  rectly rather th
bb8b0 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75 67 68  an going through
bb8c0 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 2d 77  .** the SQLite-w
bb8d0 72 61 70 70 65 72 73 20 73 71 6c 69 74 65 33 5f  rappers sqlite3_
bb8e0 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33  malloc()/sqlite3
bb8f0 5f 66 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20  _free().  Those 
bb900 77 72 61 70 70 65 72 73 0a 2a 2a 20 61 72 65 20  wrappers.** are 
bb910 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75 73 65  designed for use
bb920 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73   on embedded sys
bb930 74 65 6d 73 20 77 68 65 72 65 20 6d 65 6d 6f 72  tems where memor
bb940 79 20 69 73 20 73 63 61 72 63 65 20 61 6e 64 0a  y is scarce and.
bb950 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72  ** malloc failur
bb960 65 73 20 68 61 70 70 65 6e 20 66 72 65 71 75 65  es happen freque
bb970 6e 74 6c 79 2e 20 20 4f 53 2f 32 20 64 6f 65 73  ntly.  OS/2 does
bb980 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 72   not typically r
bb990 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 65  un on.** embedde
bb9a0 64 20 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 77  d systems, and w
bb9b0 68 65 6e 20 69 74 20 64 6f 65 73 20 74 68 65 20  hen it does the 
bb9c0 64 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d 61  developers norma
bb9d0 6c 6c 79 20 68 61 76 65 20 62 69 67 67 65 72 0a  lly have bigger.
bb9e0 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 77  ** problems to w
bb9f0 6f 72 72 79 20 61 62 6f 75 74 20 74 68 61 6e 20  orry about than 
bba00 72 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 6d  running out of m
bba10 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 65  emory.  So there
bba20 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f 6d   is not.** a com
bba30 70 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f 20  pelling need to 
bba40 75 73 65 20 74 68 65 20 77 72 61 70 70 65 72 73  use the wrappers
bba50 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 72  ..**.** But ther
bba60 65 20 69 73 20 61 20 67 6f 6f 64 20 72 65 61 73  e is a good reas
bba70 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 68  on to not use th
bba80 65 20 77 72 61 70 70 65 72 73 2e 20 20 49 66 20  e wrappers.  If 
bba90 77 65 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 72  we use the.** wr
bbaa0 61 70 70 65 72 73 20 74 68 65 6e 20 77 65 20 77  appers then we w
bbab0 69 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 65  ill get simulate
bbac0 64 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75  d malloc() failu
bbad0 72 65 73 20 77 69 74 68 69 6e 20 74 68 69 73 0a  res within this.
bbae0 2a 2a 20 64 72 69 76 65 72 2e 20 20 41 6e 64 20  ** driver.  And 
bbaf0 74 68 61 74 20 63 61 75 73 65 73 20 61 6c 6c 20  that causes all 
bbb00 6b 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d  kinds of problem
bbb10 73 20 66 6f 72 20 6f 75 72 20 74 65 73 74 73 2e  s for our tests.
bbb20 20 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 6e    We.** could en
bbb30 68 61 6e 63 65 20 53 51 4c 69 74 65 20 74 6f 20  hance SQLite to 
bbb40 64 65 61 6c 20 77 69 74 68 20 73 69 6d 75 6c 61  deal with simula
bbb50 74 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75  ted malloc failu
bbb60 72 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68  res within.** th
bbb70 65 20 4f 53 20 64 72 69 76 65 72 2c 20 62 75 74  e OS driver, but
bbb80 20 74 68 65 20 63 6f 64 65 20 74 6f 20 64 65 61   the code to dea
bbb90 6c 20 77 69 74 68 20 74 68 6f 73 65 20 66 61 69  l with those fai
bbba0 6c 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a  lure would not.*
bbbb0 2a 20 62 65 20 65 78 65 72 63 69 73 65 64 20 6f  * be exercised o
bbbc0 6e 20 4c 69 6e 75 78 20 28 77 68 69 63 68 20 64  n Linux (which d
bbbd0 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20  oes not need to 
bbbe0 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 20  malloc() in the 
bbbf0 64 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 73  driver).** and s
bbc00 6f 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20  o we would have 
bbc10 64 69 66 66 69 63 75 6c 74 79 20 77 72 69 74 69  difficulty writi
bbc20 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65 73 74  ng coverage test
bbc30 73 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f  s for that.** co
bbc40 64 65 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c  de.  Better to l
bbc50 65 61 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75  eave the code ou
bbc60 74 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a 0a  t, we think..**.
bbc70 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20  ** The point of 
bbc80 74 68 69 73 20 64 69 73 63 75 73 73 69 6f 6e 20  this discussion 
bbc90 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 20  is as follows:  
bbca0 57 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20  When creating a 
bbcb0 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 20  new.** OS layer 
bbcc0 66 6f 72 20 61 6e 20 65 6d 62 65 64 64 65 64 20  for an embedded 
bbcd0 73 79 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 75  system, if you u
bbce0 73 65 20 74 68 69 73 20 66 69 6c 65 20 61 73 20  se this file as 
bbcf0 61 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 61  an example,.** a
bbd00 76 6f 69 64 20 74 68 65 20 75 73 65 20 6f 66 20  void the use of 
bbd10 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 2e  malloc()/free().
bbd20 20 20 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 73    Those routines
bbd30 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 4f 53 2f 32   work ok on OS/2
bbd40 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62 75 74  .** desktops but
bbd50 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69 6e 20   not so well in 
bbd60 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 73  embedded systems
bbd70 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72  ..*/../*.** Macr
bbd80 6f 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72  os used to deter
bbd90 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20  mine whether or 
bbda0 6e 6f 74 20 74 6f 20 75 73 65 20 74 68 72 65 61  not to use threa
bbdb0 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  ds..*/.#if defin
bbdc0 65 64 28 53 51 4c 49 54 45 5f 54 48 52 45 41 44  ed(SQLITE_THREAD
bbdd0 53 41 46 45 29 20 26 26 20 53 51 4c 49 54 45 5f  SAFE) && SQLITE_
bbde0 54 48 52 45 41 44 53 41 46 45 0a 23 20 64 65 66  THREADSAFE.# def
bbdf0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 32 5f 54  ine SQLITE_OS2_T
bbe00 48 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66 0a  HREADS 1.#endif.
bbe10 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63  ./*.** Include c
bbe20 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d  ode that is comm
bbe30 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63  on to all os_*.c
bbe40 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a   files.*/./*****
bbe50 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
bbe60 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e  e os_common.h in
bbe70 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f   the middle of o
bbe80 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  s_os2.c ********
bbe90 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
bbea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
bbeb0 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68  file os_common.h
bbec0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
bbed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbee0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
bbef0 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a  2004 May 22.**.*
bbf00 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
bbf10 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
bbf20 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
bbf30 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
bbf40 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
bbf50 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
bbf60 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
bbf70 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
bbf80 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
bbf90 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
bbfa0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
bbfb0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
bbfc0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
bbfd0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
bbfe0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
bbff0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
bc000 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
bc010 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
bc020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
bc060 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
bc070 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f 73 20  contains macros 
bc080 61 6e 64 20 61 20 6c 69 74 74 6c 65 20 62 69 74  and a little bit
bc090 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20 69 73   of code that is
bc0a0 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c   common to.** al
bc0b0 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 66 6f 72  l of the platfor
bc0c0 6d 2d 73 70 65 63 69 66 69 63 20 66 69 6c 65 73  m-specific files
bc0d0 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73   (os_*.c) and is
bc0e0 20 23 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20   #included into 
bc0f0 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a  those.** files..
bc100 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
bc110 73 68 6f 75 6c 64 20 62 65 20 23 69 6e 63 6c 75  should be #inclu
bc120 64 65 64 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e  ded by the os_*.
bc130 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49  c files only.  I
bc140 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65  t is not a.** ge
bc150 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20 68 65  neral purpose he
bc160 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  ader file..**.**
bc170 20 24 49 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e   $Id: os_common.
bc180 68 2c 76 20 31 2e 33 38 20 32 30 30 39 2f 30 32  h,v 1.38 2009/02
bc190 2f 32 34 20 31 38 3a 34 30 3a 35 30 20 64 61 6e  /24 18:40:50 dan
bc1a0 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a  ielk1977 Exp $.*
bc1b0 2f 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f  /.#ifndef _OS_CO
bc1c0 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20  MMON_H_.#define 
bc1d0 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f  _OS_COMMON_H_../
bc1e0 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 74 77  *.** At least tw
bc1f0 6f 20 62 75 67 73 20 68 61 76 65 20 73 6c 69 70  o bugs have slip
bc200 70 65 64 20 69 6e 20 62 65 63 61 75 73 65 20 77  ped in because w
bc210 65 20 63 68 61 6e 67 65 64 20 74 68 65 20 4d 45  e changed the ME
bc220 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61  MORY_DEBUG.** ma
bc230 63 72 6f 20 74 6f 20 53 51 4c 49 54 45 5f 44 45  cro to SQLITE_DE
bc240 42 55 47 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64  BUG and some old
bc250 65 72 20 6d 61 6b 65 66 69 6c 65 73 20 68 61 76  er makefiles hav
bc260 65 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 74  e not yet made t
bc270 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e 20 20 54  he.** switch.  T
bc280 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64  he following cod
bc290 65 20 73 68 6f 75 6c 64 20 63 61 74 63 68 20 74  e should catch t
bc2a0 68 69 73 20 70 72 6f 62 6c 65 6d 20 61 74 20 63  his problem at c
bc2b0 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a  ompile-time..*/.
bc2c0 23 69 66 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45  #ifdef MEMORY_DE
bc2d0 42 55 47 0a 23 20 65 72 72 6f 72 20 22 54 68 65  BUG.# error "The
bc2e0 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61   MEMORY_DEBUG ma
bc2f0 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e  cro is obsolete.
bc300 20 20 55 73 65 20 53 51 4c 49 54 45 5f 44 45 42    Use SQLITE_DEB
bc310 55 47 20 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e  UG instead.".#en
bc320 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
bc330 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f  TE_DEBUG.SQLITE_
bc340 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
bc350 74 65 33 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a  te3OSTrace = 0;.
bc360 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31  #define OSTRACE1
bc370 28 58 29 20 20 20 20 20 20 20 20 20 69 66 28 20  (X)         if( 
bc380 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29  sqlite3OSTrace )
bc390 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
bc3a0 6e 74 66 28 58 29 0a 23 64 65 66 69 6e 65 20 4f  ntf(X).#define O
bc3b0 53 54 52 41 43 45 32 28 58 2c 59 29 20 20 20 20  STRACE2(X,Y)    
bc3c0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53     if( sqlite3OS
bc3d0 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44  Trace ) sqlite3D
bc3e0 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 29 0a  ebugPrintf(X,Y).
bc3f0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33  #define OSTRACE3
bc400 28 58 2c 59 2c 5a 29 20 20 20 20 20 69 66 28 20  (X,Y,Z)     if( 
bc410 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29  sqlite3OSTrace )
bc420 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
bc430 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69  ntf(X,Y,Z).#defi
bc440 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c  ne OSTRACE4(X,Y,
bc450 5a 2c 41 29 20 20 20 69 66 28 20 73 71 6c 69 74  Z,A)   if( sqlit
bc460 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
bc470 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
bc480 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20  ,Y,Z,A).#define 
bc490 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41  OSTRACE5(X,Y,Z,A
bc4a0 2c 42 29 20 69 66 28 20 73 71 6c 69 74 65 33 4f  ,B) if( sqlite3O
bc4b0 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33  STrace ) sqlite3
bc4c0 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
bc4d0 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f  Z,A,B).#define O
bc4e0 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c  STRACE6(X,Y,Z,A,
bc4f0 42 2c 43 29 20 5c 0a 20 20 20 20 69 66 28 73 71  B,C) \.    if(sq
bc500 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71  lite3OSTrace) sq
bc510 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
bc520 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64  (X,Y,Z,A,B,C).#d
bc530 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58  efine OSTRACE7(X
bc540 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a  ,Y,Z,A,B,C,D) \.
bc550 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53      if(sqlite3OS
bc560 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65  Trace) sqlite3De
bc570 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c  bugPrintf(X,Y,Z,
bc580 41 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23  A,B,C,D).#else.#
bc590 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28  define OSTRACE1(
bc5a0 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  X).#define OSTRA
bc5b0 43 45 32 28 58 2c 59 29 0a 23 64 65 66 69 6e 65  CE2(X,Y).#define
bc5c0 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29   OSTRACE3(X,Y,Z)
bc5d0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
bc5e0 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69  4(X,Y,Z,A).#defi
bc5f0 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c  ne OSTRACE5(X,Y,
bc600 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f  Z,A,B).#define O
bc610 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c  STRACE6(X,Y,Z,A,
bc620 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  B,C).#define OST
bc630 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c  RACE7(X,Y,Z,A,B,
bc640 43 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  C,D).#endif../*.
bc650 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 70 65  ** Macros for pe
bc660 72 66 6f 72 6d 61 6e 63 65 20 74 72 61 63 69 6e  rformance tracin
bc670 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72  g.  Normally tur
bc680 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77  ned off.  Only w
bc690 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20  orks.** on i486 
bc6a0 68 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66  hardware..*/.#if
bc6b0 64 65 66 20 53 51 4c 49 54 45 5f 50 45 52 46 4f  def SQLITE_PERFO
bc6c0 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a  RMANCE_TRACE../*
bc6d0 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f   .** hwtime.h co
bc6e0 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73  ntains inline as
bc6f0 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 66 6f 72  sembler code for
bc700 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a   implementing .*
bc710 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e  * high-performan
bc720 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e  ce timing routin
bc730 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  es..*/./********
bc740 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68  ****** Include h
bc750 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d  wtime.h in the m
bc760 69 64 64 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d  iddle of os_comm
bc770 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on.h ***********
bc780 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
bc790 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
bc7a0 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a  e hwtime.h *****
bc7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc7d0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
bc7e0 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54  8 May 27.**.** T
bc7f0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
bc800 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
bc810 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
bc820 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
bc830 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
bc840 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
bc850 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
bc860 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
bc870 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
bc880 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
bc890 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
bc8a0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
bc8b0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
bc8c0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
bc8d0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
bc8e0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
bc8f0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
bc900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
bc950 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
bc960 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d  tains inline asm
bc970 20 63 6f 64 65 20 66 6f 72 20 72 65 74 72 69 65   code for retrie
bc980 76 69 6e 67 20 22 68 69 67 68 2d 70 65 72 66 6f  ving "high-perfo
bc990 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74  rmance".** count
bc9a0 65 72 73 20 66 6f 72 20 78 38 36 20 63 6c 61 73  ers for x86 clas
bc9b0 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49  s CPUs..**.** $I
bc9c0 64 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e  d: hwtime.h,v 1.
bc9d0 33 20 32 30 30 38 2f 30 38 2f 30 31 20 31 34 3a  3 2008/08/01 14:
bc9e0 33 33 3a 31 35 20 73 68 61 6e 65 20 45 78 70 20  33:15 shane Exp 
bc9f0 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57  $.*/.#ifndef _HW
bca00 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20  TIME_H_.#define 
bca10 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a  _HWTIME_H_../*.*
bca20 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
bca30 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72  routine only wor
bca40 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c  ks on pentium-cl
bca50 61 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70  ass (or newer) p
bca60 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74  rocessors..** It
bca70 20 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20   uses the RDTSC 
bca80 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74  opcode to read t
bca90 68 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76  he cycle count v
bcaa0 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a  alue out of the.
bcab0 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64  ** processor and
bcac0 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61   returns that va
bcad0 6c 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62  lue.  This can b
bcae0 65 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d  e used for high-
bcaf0 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67  res.** profiling
bcb00 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65  ..*/.#if (define
bcb10 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64  d(__GNUC__) || d
bcb20 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29  efined(_MSC_VER)
bcb30 29 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65  ) && \.      (de
bcb40 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64  fined(i386) || d
bcb50 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29  efined(__i386__)
bcb60 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49   || defined(_M_I
bcb70 58 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66  X86))..  #if def
bcb80 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a  ined(__GNUC__)..
bcb90 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c    __inline__ sql
bcba0 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74  ite_uint64 sqlit
bcbb0 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a  e3Hwtime(void){.
bcbc0 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e       unsigned in
bcbd0 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f  t lo, hi;.     _
bcbe0 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c  _asm__ __volatil
bcbf0 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22  e__ ("rdtsc" : "
bcc00 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28  =a" (lo), "=d" (
bcc10 68 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72  hi));.     retur
bcc20 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  n (sqlite_uint64
bcc30 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a  )hi << 32 | lo;.
bcc40 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66    }..  #elif def
bcc50 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a  ined(_MSC_VER)..
bcc60 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b    __declspec(nak
bcc70 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c  ed) __inline sql
bcc80 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65  ite_uint64 __cde
bcc90 63 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  cl sqlite3Hwtime
bcca0 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61  (void){.     __a
bccb0 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74  sm {.        rdt
bccc0 73 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20  sc.        ret  
bccd0 20 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61       ; return va
bcce0 6c 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20  lue at EDX:EAX. 
bccf0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e      }.  }..  #en
bcd00 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69  dif..#elif (defi
bcd10 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26  ned(__GNUC__) &&
bcd20 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36   defined(__x86_6
bcd30 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e  4__))..  __inlin
bcd40 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  e__ sqlite_uint6
bcd50 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  4 sqlite3Hwtime(
bcd60 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73  void){.      uns
bcd70 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a  igned long val;.
bcd80 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f        __asm__ __
bcd90 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74  volatile__ ("rdt
bcda0 73 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29  sc" : "=A" (val)
bcdb0 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
bcdc0 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66  val;.  }. .#elif
bcdd0 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43   (defined(__GNUC
bcde0 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f  __) && defined(_
bcdf0 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e  _ppc__))..  __in
bce00 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69  line__ sqlite_ui
bce10 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69  nt64 sqlite3Hwti
bce20 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20  me(void){.      
bce30 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
bce40 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20  ng retval;.     
bce50 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a   unsigned long j
bce60 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d  unk;.      __asm
bce70 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20  __ __volatile__ 
bce80 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  ("\n\.          
bce90 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20  1:      mftbu   
bcea0 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  %1\n\.          
bceb0 20 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20          mftb    
bcec0 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20  %L0\n\.         
bced0 20 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20           mftbu  
bcee0 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20   %0\n\.         
bcef0 20 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20           cmpw   
bcf00 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20   %0,%1\n\.      
bcf10 20 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20              bne 
bcf20 20 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20      1b".        
bcf30 20 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22            : "=r"
bcf40 20 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20   (retval), "=r" 
bcf50 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72  (junk));.      r
bcf60 65 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20  eturn retval;.  
bcf70 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72  }..#else..  #err
bcf80 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e  or Need implemen
bcf90 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65  tation of sqlite
bcfa0 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f  3Hwtime() for yo
bcfb0 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20  ur platform...  
bcfc0 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69  /*.  ** To compi
bcfd0 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65  le without imple
bcfe0 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48  menting sqlite3H
bcff0 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72  wtime() for your
bd000 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20   platform,.  ** 
bd010 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74  you can remove t
bd020 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20  he above #error 
bd030 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c  and use the foll
bd040 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20  owing.  ** stub 
bd050 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77  function.  You w
bd060 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20  ill lose timing 
bd070 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79  support for many
bd080 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62  .  ** of the deb
bd090 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69  ugging and testi
bd0a0 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75  ng utilities, bu
bd0b0 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20  t it should at. 
bd0c0 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c   ** least compil
bd0d0 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a  e and run..  */.
bd0e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
bd0f0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
bd100 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
bd110 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c  d){ return ((sql
bd120 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d  ite_uint64)0); }
bd130 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66  ..#endif..#endif
bd140 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57   /* !defined(_HW
bd150 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a  TIME_H_) */../**
bd160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
bd170 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a   of hwtime.h ***
bd180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
bd1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
bd1c0 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
bd1d0 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f   left off in os_
bd1e0 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
bd1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74  ***********/..st
bd200 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74  atic sqlite_uint
bd210 36 34 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 74  64 g_start;.stat
bd220 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  ic sqlite_uint64
bd230 20 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64 65 66   g_elapsed;.#def
bd240 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20  ine TIMER_START 
bd250 20 20 20 20 20 20 67 5f 73 74 61 72 74 3d 73 71        g_start=sq
bd260 6c 69 74 65 33 48 77 74 69 6d 65 28 29 0a 23 64  lite3Hwtime().#d
bd270 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20  efine TIMER_END 
bd280 20 20 20 20 20 20 20 20 67 5f 65 6c 61 70 73 65          g_elapse
bd290 64 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  d=sqlite3Hwtime(
bd2a0 29 2d 67 5f 73 74 61 72 74 0a 23 64 65 66 69 6e  )-g_start.#defin
bd2b0 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20  e TIMER_ELAPSED 
bd2c0 20 20 20 20 67 5f 65 6c 61 70 73 65 64 0a 23 65      g_elapsed.#e
bd2d0 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 49 4d 45  lse.#define TIME
bd2e0 52 5f 53 54 41 52 54 0a 23 64 65 66 69 6e 65 20  R_START.#define 
bd2f0 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e  TIMER_END.#defin
bd300 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20  e TIMER_ELAPSED 
bd310 20 20 20 20 28 28 73 71 6c 69 74 65 5f 75 69 6e      ((sqlite_uin
bd320 74 36 34 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f  t64)0).#endif../
bd330 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69  *.** If we compi
bd340 6c 65 20 77 69 74 68 20 74 68 65 20 53 51 4c 49  le with the SQLI
bd350 54 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 65  TE_TEST macro se
bd360 74 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c  t, then the foll
bd370 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f  owing block.** o
bd380 66 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 65  f code will give
bd390 20 75 73 20 74 68 65 20 61 62 69 6c 69 74 79 20   us the ability 
bd3a0 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 64 69  to simulate a di
bd3b0 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54  sk I/O error.  T
bd3c0 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 66  his.** is used f
bd3d0 6f 72 20 74 65 73 74 69 6e 67 20 74 68 65 20 49  or testing the I
bd3e0 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69  /O recovery logi
bd3f0 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  c..*/.#ifdef SQL
bd400 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
bd410 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
bd420 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30  io_error_hit = 0
bd430 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
bd440 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  Total number of 
bd450 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51  I/O Errors */.SQ
bd460 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
bd470 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61  ite3_io_error_ha
bd480 72 64 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20  rdhit = 0;      
bd490 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e    /* Number of n
bd4a0 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 73  on-benign errors
bd4b0 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
bd4c0 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
bd4d0 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b  ror_pending = 0;
bd4e0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74          /* Count
bd4f0 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 20 49   down to first I
bd500 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49  /O error */.SQLI
bd510 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
bd520 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73  e3_io_error_pers
bd530 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ist = 0;        
bd540 2f 2a 20 54 72 75 65 20 69 66 20 49 2f 4f 20 65  /* True if I/O e
bd550 72 72 6f 72 73 20 70 65 72 73 69 73 74 20 2a 2f  rrors persist */
bd560 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
bd570 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
bd580 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20  _benign = 0;    
bd590 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
bd5a0 65 72 72 6f 72 73 20 61 72 65 20 62 65 6e 69 67  errors are benig
bd5b0 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  n */.SQLITE_API 
bd5c0 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b  int sqlite3_disk
bd5d0 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30  full_pending = 0
bd5e0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
bd5f0 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
bd600 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53  l = 0;.#define S
bd610 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65  imulateIOErrorBe
bd620 6e 69 67 6e 28 58 29 20 73 71 6c 69 74 65 33 5f  nign(X) sqlite3_
bd630 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d  io_error_benign=
bd640 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  (X).#define Simu
bd650 6c 61 74 65 49 4f 45 72 72 6f 72 28 43 4f 44 45  lateIOError(CODE
bd660 29 20 20 5c 0a 20 20 69 66 28 20 28 73 71 6c 69  )  \.  if( (sqli
bd670 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72  te3_io_error_per
bd680 73 69 73 74 20 26 26 20 73 71 6c 69 74 65 33 5f  sist && sqlite3_
bd690 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a  io_error_hit) \.
bd6a0 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65         || sqlite
bd6b0 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69  3_io_error_pendi
bd6c0 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20  ng-- == 1 )  \. 
bd6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 6c               { l
bd6e0 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f  ocal_ioerr(); CO
bd6f0 44 45 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69  DE; }.static voi
bd700 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b  d local_ioerr(){
bd710 0a 20 20 49 4f 54 52 41 43 45 28 28 22 49 4f 45  .  IOTRACE(("IOE
bd720 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74  RR\n"));.  sqlit
bd730 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b  e3_io_error_hit+
bd740 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65  +;.  if( !sqlite
bd750 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67  3_io_error_benig
bd760 6e 20 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  n ) sqlite3_io_e
bd770 72 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a  rror_hardhit++;.
bd780 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  }.#define Simula
bd790 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28  teDiskfullError(
bd7a0 43 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73  CODE) \.   if( s
bd7b0 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f  qlite3_diskfull_
bd7c0 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20  pending ){ \.   
bd7d0 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69    if( sqlite3_di
bd7e0 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d  skfull_pending =
bd7f0 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20  = 1 ){ \.       
bd800 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c  local_ioerr(); \
bd810 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .       sqlite3_
bd820 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a  diskfull = 1; \.
bd830 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69         sqlite3_i
bd840 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b  o_error_hit = 1;
bd850 20 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20   \.       CODE; 
bd860 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a  \.     }else{ \.
bd870 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64         sqlite3_d
bd880 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d  iskfull_pending-
bd890 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20  -; \.     } \.  
bd8a0 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65   }.#else.#define
bd8b0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
bd8c0 42 65 6e 69 67 6e 28 58 29 0a 23 64 65 66 69 6e  Benign(X).#defin
bd8d0 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f  e SimulateIOErro
bd8e0 72 28 41 29 0a 23 64 65 66 69 6e 65 20 53 69 6d  r(A).#define Sim
bd8f0 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72  ulateDiskfullErr
bd900 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  or(A).#endif../*
bd910 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67  .** When testing
bd920 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 6f  , keep a count o
bd930 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  f the number of 
bd940 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23  open files..*/.#
bd950 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
bd960 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
bd970 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69   sqlite3_open_fi
bd980 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64  le_count = 0;.#d
bd990 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65  efine OpenCounte
bd9a0 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f 6f 70  r(X)  sqlite3_op
bd9b0 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28  en_file_count+=(
bd9c0 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65  X).#else.#define
bd9d0 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a   OpenCounter(X).
bd9e0 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f  #endif..#endif /
bd9f0 2a 20 21 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43  * !defined(_OS_C
bda00 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a  OMMON_H_) */../*
bda10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
bda20 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68  d of os_common.h
bda30 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
bda40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bda50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
bda60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
bda70 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
bda80 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73  e left off in os
bda90 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  _os2.c *********
bdaa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
bdab0 2a 0a 2a 2a 20 54 68 65 20 6f 73 32 46 69 6c 65  *.** The os2File
bdac0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 73 75   structure is su
bdad0 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65  bclass of sqlite
bdae0 33 5f 66 69 6c 65 20 73 70 65 63 69 66 69 63 20  3_file specific 
bdaf0 66 6f 72 20 74 68 65 20 4f 53 2f 32 0a 2a 2a 20  for the OS/2.** 
bdb00 70 72 6f 74 61 62 69 6c 69 74 79 20 6c 61 79 65  protability laye
bdb10 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  r..*/.typedef st
bdb20 72 75 63 74 20 6f 73 32 46 69 6c 65 20 6f 73 32  ruct os2File os2
bdb30 46 69 6c 65 3b 0a 73 74 72 75 63 74 20 6f 73 32  File;.struct os2
bdb40 46 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73  File {.  const s
bdb50 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
bdb60 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 2f 2a 20  s *pMethod;  /* 
bdb70 41 6c 77 61 79 73 20 74 68 65 20 66 69 72 73 74  Always the first
bdb80 20 65 6e 74 72 79 20 2a 2f 0a 20 20 48 46 49 4c   entry */.  HFIL
bdb90 45 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  E h;            
bdba0 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20        /* Handle 
bdbb0 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74 68  for accessing th
bdbc0 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72  e file */.  char
bdbd0 2a 20 70 61 74 68 54 6f 44 65 6c 3b 20 20 20 20  * pathToDel;    
bdbe0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
bdbf0 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 20   file to delete 
bdc00 6f 6e 20 63 6c 6f 73 65 2c 20 4e 55 4c 4c 20 69  on close, NULL i
bdc10 66 20 6e 6f 74 20 2a 2f 0a 20 20 75 6e 73 69 67  f not */.  unsig
bdc20 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70  ned char locktyp
bdc30 65 3b 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20  e;   /* Type of 
bdc40 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20 68  lock currently h
bdc50 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c 65  eld on this file
bdc60 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20   */.};..#define 
bdc70 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 31 30 4c  LOCK_TIMEOUT 10L
bdc80 20 2f 2a 20 74 68 65 20 64 65 66 61 75 6c 74 20   /* the default 
bdc90 6c 6f 63 6b 69 6e 67 20 74 69 6d 65 6f 75 74 20  locking timeout 
bdca0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
bdcb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdcc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdcf0 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67  **.** The next g
bdd00 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73  roup of routines
bdd10 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 49   implement the I
bdd20 2f 4f 20 6d 65 74 68 6f 64 73 20 73 70 65 63 69  /O methods speci
bdd30 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 73  fied.** by the s
bdd40 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
bdd50 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a 2a 2a 2a  s object..******
bdd60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdd70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdd80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdd90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdda0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ********/../*.**
bddb0 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a   Close a file..*
bddc0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  /.static int os2
bddd0 43 6c 6f 73 65 28 20 73 71 6c 69 74 65 33 5f 66  Close( sqlite3_f
bdde0 69 6c 65 20 2a 69 64 20 29 7b 0a 20 20 41 50 49  ile *id ){.  API
bddf0 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f  RET rc = NO_ERRO
bde00 52 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46  R;.  os2File *pF
bde10 69 6c 65 3b 0a 20 20 69 66 28 20 69 64 20 26 26  ile;.  if( id &&
bde20 20 28 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69   (pFile = (os2Fi
bde30 6c 65 2a 29 69 64 29 20 21 3d 20 30 20 29 7b 0a  le*)id) != 0 ){.
bde40 20 20 20 20 4f 53 54 52 41 43 45 32 28 20 22 43      OSTRACE2( "C
bde50 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70 46 69 6c  LOSE %d\n", pFil
bde60 65 2d 3e 68 20 29 3b 0a 20 20 20 20 72 63 20 3d  e->h );.    rc =
bde70 20 44 6f 73 43 6c 6f 73 65 28 20 70 46 69 6c 65   DosClose( pFile
bde80 2d 3e 68 20 29 3b 0a 20 20 20 20 70 46 69 6c 65  ->h );.    pFile
bde90 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f  ->locktype = NO_
bdea0 4c 4f 43 4b 3b 0a 20 20 20 20 69 66 28 20 70 46  LOCK;.    if( pF
bdeb0 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 21  ile->pathToDel !
bdec0 3d 20 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20  = NULL ){.      
bded0 72 63 20 3d 20 44 6f 73 46 6f 72 63 65 44 65 6c  rc = DosForceDel
bdee0 65 74 65 28 20 28 50 53 5a 29 70 46 69 6c 65 2d  ete( (PSZ)pFile-
bdef0 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a 20 20  >pathToDel );.  
bdf00 20 20 20 20 66 72 65 65 28 20 70 46 69 6c 65 2d      free( pFile-
bdf10 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a 20 20  >pathToDel );.  
bdf20 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54      pFile->pathT
bdf30 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  oDel = NULL;.   
bdf40 20 7d 0a 20 20 20 20 69 64 20 3d 20 30 3b 0a 20   }.    id = 0;. 
bdf50 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 20     OpenCounter( 
bdf60 2d 31 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  -1 );.  }..  ret
bdf70 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52  urn rc == NO_ERR
bdf80 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a  OR ? SQLITE_OK :
bdf90 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d   SQLITE_IOERR;.}
bdfa0 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74  ../*.** Read dat
bdfb0 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e  a from a file in
bdfc0 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 65  to a buffer.  Re
bdfd0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69  turn SQLITE_OK i
bdfe0 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77  f all.** bytes w
bdff0 65 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73  ere read success
be000 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45  fully and SQLITE
be010 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69  _IOERR if anythi
be020 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67  ng goes.** wrong
be030 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
be040 6f 73 32 52 65 61 64 28 0a 20 20 73 71 6c 69 74  os2Read(.  sqlit
be050 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20  e3_file *id,    
be060 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
be070 6c 65 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20  le to read from 
be080 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66 2c  */.  void *pBuf,
be090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
be0a0 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 6f       /* Write co
be0b0 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 69 73 20  ntent into this 
be0c0 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20  buffer */.  int 
be0d0 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  amt,            
be0e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
be0f0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
be100 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69  o read */.  sqli
be110 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74  te3_int64 offset
be120 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
be130 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61 74 20  egin reading at 
be140 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f 0a 29  this offset */.)
be150 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65 4c 6f  {.  ULONG fileLo
be160 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 20 55  cation = 0L;.  U
be170 4c 4f 4e 47 20 67 6f 74 3b 0a 20 20 6f 73 32 46  LONG got;.  os2F
be180 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73  ile *pFile = (os
be190 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73  2File*)id;.  ass
be1a0 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20  ert( id!=0 );.  
be1b0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
be1c0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
be1d0 4f 45 52 52 5f 52 45 41 44 20 29 3b 0a 20 20 4f  OERR_READ );.  O
be1e0 53 54 52 41 43 45 33 28 20 22 52 45 41 44 20 25  STRACE3( "READ %
be1f0 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46  d lock=%d\n", pF
be200 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c  ile->h, pFile->l
be210 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 69 66 28  ocktype );.  if(
be220 20 44 6f 73 53 65 74 46 69 6c 65 50 74 72 28 70   DosSetFilePtr(p
be230 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 65 74 2c  File->h, offset,
be240 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26 66 69   FILE_BEGIN, &fi
be250 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 21 3d 20 4e  leLocation) != N
be260 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72  O_ERROR ){.    r
be270 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
be280 52 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20 44 6f  RR;.  }.  if( Do
be290 73 52 65 61 64 28 20 70 46 69 6c 65 2d 3e 68 2c  sRead( pFile->h,
be2a0 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 67 6f 74   pBuf, amt, &got
be2b0 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29   ) != NO_ERROR )
be2c0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
be2d0 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a  ITE_IOERR_READ;.
be2e0 20 20 7d 0a 20 20 69 66 28 20 67 6f 74 20 3d 3d    }.  if( got ==
be2f0 20 28 55 4c 4f 4e 47 29 61 6d 74 20 29 0a 20 20   (ULONG)amt ).  
be300 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
be310 4f 4b 3b 0a 20 20 65 6c 73 65 20 7b 0a 20 20 20  OK;.  else {.   
be320 20 2f 2a 20 55 6e 72 65 61 64 20 70 6f 72 74 69   /* Unread porti
be330 6f 6e 73 20 6f 66 20 74 68 65 20 69 6e 70 75 74  ons of the input
be340 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 20   buffer must be 
be350 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20  zero-filled */. 
be360 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61     memset(&((cha
be370 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30  r*)pBuf)[got], 0
be380 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20  , amt-got);.    
be390 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
be3a0 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a  ERR_SHORT_READ;.
be3b0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69    }.}../*.** Wri
be3c0 74 65 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62  te data from a b
be3d0 75 66 66 65 72 20 69 6e 74 6f 20 61 20 66 69 6c  uffer into a fil
be3e0 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54  e.  Return SQLIT
be3f0 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a  E_OK on success.
be400 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72  ** or some other
be410 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66   error code on f
be420 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69  ailure..*/.stati
be430 63 20 69 6e 74 20 6f 73 32 57 72 69 74 65 28 0a  c int os2Write(.
be440 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
be450 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  id,             
be460 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 77 72 69    /* File to wri
be470 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 6f 6e  te into */.  con
be480 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20  st void *pBuf,  
be490 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
be4a0 54 68 65 20 62 79 74 65 73 20 74 6f 20 62 65 20  The bytes to be 
be4b0 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74  written */.  int
be4c0 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20   amt,           
be4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
be4e0 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
be4f0 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71  to write */.  sq
be500 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73  lite3_int64 offs
be510 65 74 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  et            /*
be520 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65   Offset into the
be530 20 66 69 6c 65 20 74 6f 20 62 65 67 69 6e 20 77   file to begin w
be540 72 69 74 69 6e 67 20 61 74 20 2a 2f 0a 29 7b 0a  riting at */.){.
be550 20 20 55 4c 4f 4e 47 20 66 69 6c 65 4c 6f 63 61    ULONG fileLoca
be560 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 20 41 50 49  tion = 0L;.  API
be570 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f  RET rc = NO_ERRO
be580 52 3b 0a 20 20 55 4c 4f 4e 47 20 77 72 6f 74 65  R;.  ULONG wrote
be590 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69  ;.  os2File *pFi
be5a0 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69  le = (os2File*)i
be5b0 64 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 21  d;.  assert( id!
be5c0 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65  =0 );.  Simulate
be5d0 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20  IOError( return 
be5e0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49  SQLITE_IOERR_WRI
be5f0 54 45 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65  TE );.  Simulate
be600 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 20 72  DiskfullError( r
be610 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c  eturn SQLITE_FUL
be620 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28  L );.  OSTRACE3(
be630 20 22 57 52 49 54 45 20 25 64 20 6c 6f 63 6b 3d   "WRITE %d lock=
be640 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
be650 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
be660 20 29 3b 0a 20 20 69 66 28 20 44 6f 73 53 65 74   );.  if( DosSet
be670 46 69 6c 65 50 74 72 28 70 46 69 6c 65 2d 3e 68  FilePtr(pFile->h
be680 2c 20 6f 66 66 73 65 74 2c 20 46 49 4c 45 5f 42  , offset, FILE_B
be690 45 47 49 4e 2c 20 26 66 69 6c 65 4c 6f 63 61 74  EGIN, &fileLocat
be6a0 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52  ion) != NO_ERROR
be6b0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
be6c0 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d  QLITE_IOERR;.  }
be6d0 0a 20 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30  .  assert( amt>0
be6e0 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 61 6d 74   );.  while( amt
be6f0 20 3e 20 30 20 26 26 0a 20 20 20 20 20 20 20 20   > 0 &&.        
be700 20 28 20 72 63 20 3d 20 44 6f 73 57 72 69 74 65   ( rc = DosWrite
be710 28 20 70 46 69 6c 65 2d 3e 68 2c 20 28 50 56 4f  ( pFile->h, (PVO
be720 49 44 29 70 42 75 66 2c 20 61 6d 74 2c 20 26 77  ID)pBuf, amt, &w
be730 72 6f 74 65 20 29 20 29 20 3d 3d 20 4e 4f 5f 45  rote ) ) == NO_E
be740 52 52 4f 52 20 26 26 0a 20 20 20 20 20 20 20 20  RROR &&.        
be750 20 77 72 6f 74 65 20 3e 20 30 0a 20 20 29 7b 0a   wrote > 0.  ){.
be760 20 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f 74 65      amt -= wrote
be770 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28  ;.    pBuf = &((
be780 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74  char*)pBuf)[wrot
be790 65 5d 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  e];.  }..  retur
be7a0 6e 20 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52  n ( rc != NO_ERR
be7b0 4f 52 20 7c 7c 20 61 6d 74 20 3e 20 28 69 6e 74  OR || amt > (int
be7c0 29 77 72 6f 74 65 20 29 20 3f 20 53 51 4c 49 54  )wrote ) ? SQLIT
be7d0 45 5f 46 55 4c 4c 20 3a 20 53 51 4c 49 54 45 5f  E_FULL : SQLITE_
be7e0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75  OK;.}../*.** Tru
be7f0 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69  ncate an open fi
be800 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 69 65  le to a specifie
be810 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63  d size.*/.static
be820 20 69 6e 74 20 6f 73 32 54 72 75 6e 63 61 74 65   int os2Truncate
be830 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  ( sqlite3_file *
be840 69 64 2c 20 69 36 34 20 6e 42 79 74 65 20 29 7b  id, i64 nByte ){
be850 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e  .  APIRET rc = N
be860 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f 73 32 46 69  O_ERROR;.  os2Fi
be870 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32  le *pFile = (os2
be880 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f 53 54 52  File*)id;.  OSTR
be890 41 43 45 33 28 20 22 54 52 55 4e 43 41 54 45 20  ACE3( "TRUNCATE 
be8a0 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46 69 6c  %d %lld\n", pFil
be8b0 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a 20  e->h, nByte );. 
be8c0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
be8d0 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
be8e0 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 20 29  IOERR_TRUNCATE )
be8f0 3b 0a 20 20 72 63 20 3d 20 44 6f 73 53 65 74 46  ;.  rc = DosSetF
be900 69 6c 65 53 69 7a 65 28 20 70 46 69 6c 65 2d 3e  ileSize( pFile->
be910 68 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20 72 65  h, nByte );.  re
be920 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52  turn rc == NO_ER
be930 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20  ROR ? SQLITE_OK 
be940 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54  : SQLITE_IOERR_T
be950 52 55 4e 43 41 54 45 3b 0a 7d 0a 0a 23 69 66 64  RUNCATE;.}..#ifd
be960 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f  ef SQLITE_TEST./
be970 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e  *.** Count the n
be980 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73 79 6e  umber of fullsyn
be990 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 73 79  cs and normal sy
be9a0 6e 63 73 2e 20 20 54 68 69 73 20 69 73 20 75 73  ncs.  This is us
be9b0 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74 68  ed to test.** th
be9c0 61 74 20 73 79 6e 63 73 20 61 6e 64 20 66 75 6c  at syncs and ful
be9d0 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 63 75 72  lsyncs are occur
be9e0 69 6e 67 20 61 74 20 74 68 65 20 72 69 67 68 74  ing at the right
be9f0 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54   times..*/.SQLIT
bea00 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
bea10 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30  3_sync_count = 0
bea20 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
bea30 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e   sqlite3_fullsyn
bea40 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e  c_count = 0;.#en
bea50 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  dif../*.** Make 
bea60 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20  sure all writes 
bea70 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  to a particular 
bea80 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74  file are committ
bea90 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f 0a 73  ed to disk..*/.s
beaa0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 53 79 6e  tatic int os2Syn
beab0 63 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  c( sqlite3_file 
beac0 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 20 29  *id, int flags )
bead0 7b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69  {.  os2File *pFi
beae0 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69  le = (os2File*)i
beaf0 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22  d;.  OSTRACE3( "
beb00 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d 25 64 5c  SYNC %d lock=%d\
beb10 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46  n", pFile->h, pF
beb20 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b  ile->locktype );
beb30 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
beb40 45 53 54 0a 20 20 69 66 28 20 66 6c 61 67 73 20  EST.  if( flags 
beb50 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55  & SQLITE_SYNC_FU
beb60 4c 4c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  LL){.    sqlite3
beb70 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b  _fullsync_count+
beb80 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  +;.  }.  sqlite3
beb90 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23  _sync_count++;.#
beba0 65 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20 77 65  endif.  /* If we
bebb0 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74   compiled with t
bebc0 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e  he SQLITE_NO_SYN
bebd0 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e  C flag, then syn
bebe0 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e  cing is a.  ** n
bebf0 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66  o-op.  */.#ifdef
bec00 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a   SQLITE_NO_SYNC.
bec10 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
bec20 45 52 28 70 46 69 6c 65 29 3b 0a 20 20 72 65 74  ER(pFile);.  ret
bec30 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23  urn SQLITE_OK;.#
bec40 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 44 6f  else.  return Do
bec50 73 52 65 73 65 74 42 75 66 66 65 72 28 20 70 46  sResetBuffer( pF
bec60 69 6c 65 2d 3e 68 20 29 20 3d 3d 20 4e 4f 5f 45  ile->h ) == NO_E
bec70 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  RROR ? SQLITE_OK
bec80 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b   : SQLITE_IOERR;
bec90 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
beca0 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63   Determine the c
becb0 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61  urrent size of a
becc0 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a   file in bytes.*
becd0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  /.static int os2
bece0 46 69 6c 65 53 69 7a 65 28 20 73 71 6c 69 74 65  FileSize( sqlite
becf0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69  3_file *id, sqli
bed00 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65  te3_int64 *pSize
bed10 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20   ){.  APIRET rc 
bed20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 46 49  = NO_ERROR;.  FI
bed30 4c 45 53 54 41 54 55 53 33 20 66 73 74 73 33 46  LESTATUS3 fsts3F
bed40 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d 65 6d 73 65  ileInfo;.  memse
bed50 74 28 26 66 73 74 73 33 46 69 6c 65 49 6e 66 6f  t(&fsts3FileInfo
bed60 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 73  , 0, sizeof(fsts
bed70 33 46 69 6c 65 49 6e 66 6f 29 29 3b 0a 20 20 61  3FileInfo));.  a
bed80 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a  ssert( id!=0 );.
bed90 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
beda0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
bedb0 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 29 3b 0a  _IOERR_FSTAT );.
bedc0 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 46    rc = DosQueryF
bedd0 69 6c 65 49 6e 66 6f 28 20 28 28 6f 73 32 46 69  ileInfo( ((os2Fi
bede0 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 46 49 4c 5f  le*)id)->h, FIL_
bedf0 53 54 41 4e 44 41 52 44 2c 20 26 66 73 74 73 33  STANDARD, &fsts3
bee00 46 69 6c 65 49 6e 66 6f 2c 20 73 69 7a 65 6f 66  FileInfo, sizeof
bee10 28 46 49 4c 45 53 54 41 54 55 53 33 29 20 29 3b  (FILESTATUS3) );
bee20 0a 20 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f  .  if( rc == NO_
bee30 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 2a 70 53  ERROR ){.    *pS
bee40 69 7a 65 20 3d 20 66 73 74 73 33 46 69 6c 65 49  ize = fsts3FileI
bee50 6e 66 6f 2e 63 62 46 69 6c 65 3b 0a 20 20 20 20  nfo.cbFile;.    
bee60 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
bee70 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
bee80 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
bee90 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 7d 0a  RR_FSTAT;.  }.}.
beea0 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61  ./*.** Acquire a
beeb0 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e 0a 2a 2f   reader lock..*/
beec0 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 52  .static int getR
beed0 65 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65  eadLock( os2File
beee0 20 2a 70 46 69 6c 65 20 29 7b 0a 20 20 46 49 4c   *pFile ){.  FIL
beef0 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c  ELOCK  LockArea,
bef00 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c  .            Unl
bef10 6f 63 6b 41 72 65 61 3b 0a 20 20 41 50 49 52 45  ockArea;.  APIRE
bef20 54 20 72 65 73 3b 0a 20 20 6d 65 6d 73 65 74 28  T res;.  memset(
bef30 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69  &LockArea, 0, si
bef40 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b  zeof(LockArea));
bef50 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63  .  memset(&Unloc
bef60 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66  kArea, 0, sizeof
bef70 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20  (UnlockArea));. 
bef80 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65   LockArea.lOffse
bef90 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54  t = SHARED_FIRST
befa0 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61  ;.  LockArea.lRa
befb0 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a  nge = SHARED_SIZ
befc0 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e  E;.  UnlockArea.
befd0 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20  lOffset = 0L;.  
befe0 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67  UnlockArea.lRang
beff0 65 20 3d 20 30 4c 3b 0a 20 20 72 65 73 20 3d 20  e = 0L;.  res = 
bf000 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
bf010 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
bf020 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
bf030 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
bf040 20 31 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45   1L );.  OSTRACE
bf050 33 28 20 22 47 45 54 52 45 41 44 4c 4f 43 4b 20  3( "GETREADLOCK 
bf060 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46  %d res=%d\n", pF
bf070 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20  ile->h, res );. 
bf080 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a   return res;.}..
bf090 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65 61  /*.** Undo a rea
bf0a0 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 63 20  dlock.*/.static 
bf0b0 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f  int unlockReadLo
bf0c0 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a 69 64 20  ck( os2File *id 
bf0d0 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c  ){.  FILELOCK  L
bf0e0 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20  ockArea,.       
bf0f0 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b       UnlockArea;
bf100 0a 20 20 41 50 49 52 45 54 20 72 65 73 3b 0a 20  .  APIRET res;. 
bf110 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65   memset(&LockAre
bf120 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63  a, 0, sizeof(Loc
bf130 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65  kArea));.  memse
bf140 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30  t(&UnlockArea, 0
bf150 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41  , sizeof(UnlockA
bf160 72 65 61 29 29 3b 0a 20 20 4c 6f 63 6b 41 72 65  rea));.  LockAre
bf170 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a  a.lOffset = 0L;.
bf180 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67    LockArea.lRang
bf190 65 20 3d 20 30 4c 3b 0a 20 20 55 6e 6c 6f 63 6b  e = 0L;.  Unlock
bf1a0 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53  Area.lOffset = S
bf1b0 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 55  HARED_FIRST;.  U
bf1c0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
bf1d0 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a   = SHARED_SIZE;.
bf1e0 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69    res = DosSetFi
bf1f0 6c 65 4c 6f 63 6b 73 28 20 69 64 2d 3e 68 2c 20  leLocks( id->h, 
bf200 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f  &UnlockArea, &Lo
bf210 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d  ckArea, LOCK_TIM
bf220 45 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53  EOUT, 1L );.  OS
bf230 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 2d  TRACE3( "UNLOCK-
bf240 52 45 41 44 4c 4f 43 4b 20 66 69 6c 65 20 68 61  READLOCK file ha
bf250 6e 64 6c 65 3d 25 64 20 72 65 73 3d 25 64 3f 5c  ndle=%d res=%d?\
bf260 6e 22 2c 20 69 64 2d 3e 68 2c 20 72 65 73 20 29  n", id->h, res )
bf270 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a  ;.  return res;.
bf280 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68  }../*.** Lock th
bf290 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20  e file with the 
bf2a0 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62  lock specified b
bf2b0 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b  y parameter lock
bf2c0 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66  type - one.** of
bf2d0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
bf2e0 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48  **.**     (1) SH
bf2f0 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  ARED_LOCK.**    
bf300 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f   (2) RESERVED_LO
bf310 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45  CK.**     (3) PE
bf320 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  NDING_LOCK.**   
bf330 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f    (4) EXCLUSIVE_
bf340 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74  LOCK.**.** Somet
bf350 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73  imes when reques
bf360 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74  ting one lock st
bf370 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20  ate, additional 
bf380 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61  lock states.** a
bf390 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62  re inserted in b
bf3a0 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63  etween.  The loc
bf3b0 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20  king might fail 
bf3c0 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61  on one of the la
bf3d0 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  ter.** transitio
bf3e0 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c  ns leaving the l
bf3f0 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72  ock state differ
bf400 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74  ent from what it
bf410 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20   started but.** 
bf420 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69  still short of i
bf430 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f  ts goal.  The fo
bf440 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68  llowing chart sh
bf450 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a  ows the allowed.
bf460 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61  ** transitions a
bf470 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20  nd the inserted 
bf480 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61  intermediate sta
bf490 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e  tes:.**.**    UN
bf4a0 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44  LOCKED -> SHARED
bf4b0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
bf4c0 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20   RESERVED.**    
bf4d0 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49  SHARED -> (PENDI
bf4e0 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
bf4f0 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20  .**    RESERVED 
bf500 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
bf510 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
bf520 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55  PENDING -> EXCLU
bf530 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  SIVE.**.** This 
bf540 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c  routine will onl
bf550 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63  y increase a loc
bf560 6b 2e 20 20 54 68 65 20 6f 73 32 55 6e 6c 6f 63  k.  The os2Unloc
bf570 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65  k() routine.** e
bf580 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20  rases all locks 
bf590 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 65 74 75  at once and retu
bf5a0 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 61 74 65  rns us immediate
bf5b0 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65  ly to locking le
bf5c0 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20  vel 0..** It is 
bf5d0 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  not possible to 
bf5e0 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e  lower the lockin
bf5f0 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65 70  g level one step
bf600 20 61 74 20 61 20 74 69 6d 65 2e 20 20 59 6f 75   at a time.  You
bf610 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74 72 61  .** must go stra
bf620 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20  ight to locking 
bf630 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74  level 0..*/.stat
bf640 69 63 20 69 6e 74 20 6f 73 32 4c 6f 63 6b 28 20  ic int os2Lock( 
bf650 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
bf660 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 20 29  , int locktype )
bf670 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
bf680 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a  ITE_OK;       /*
bf690 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f   Return code fro
bf6a0 6d 20 73 75 62 72 6f 75 74 69 6e 65 73 20 2a 2f  m subroutines */
bf6b0 0a 20 20 41 50 49 52 45 54 20 72 65 73 20 3d 20  .  APIRET res = 
bf6c0 4e 4f 5f 45 52 52 4f 52 3b 20 20 20 20 2f 2a 20  NO_ERROR;    /* 
bf6d0 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 4f 53 2f  Result of an OS/
bf6e0 32 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a 2f 0a 20  2 lock call */. 
bf6f0 20 69 6e 74 20 6e 65 77 4c 6f 63 6b 74 79 70 65   int newLocktype
bf700 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 70  ;       /* Set p
bf710 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 74  File->locktype t
bf720 6f 20 74 68 69 73 20 76 61 6c 75 65 20 62 65 66  o this value bef
bf730 6f 72 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a 20  ore exiting */. 
bf740 20 69 6e 74 20 67 6f 74 50 65 6e 64 69 6e 67 4c   int gotPendingL
bf750 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 75 65 20  ock = 0;/* True 
bf760 69 66 20 77 65 20 61 63 71 75 69 72 65 64 20 61  if we acquired a
bf770 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68   PENDING lock th
bf780 69 73 20 74 69 6d 65 20 2a 2f 0a 20 20 46 49 4c  is time */.  FIL
bf790 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c  ELOCK  LockArea,
bf7a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c  .            Unl
bf7b0 6f 63 6b 41 72 65 61 3b 0a 20 20 6f 73 32 46 69  ockArea;.  os2Fi
bf7c0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32  le *pFile = (os2
bf7d0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 6d 65 6d 73  File*)id;.  mems
bf7e0 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c  et(&LockArea, 0,
bf7f0 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61   sizeof(LockArea
bf800 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e  ));.  memset(&Un
bf810 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a  lockArea, 0, siz
bf820 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29  eof(UnlockArea))
bf830 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ;.  assert( pFil
bf840 65 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43  e!=0 );.  OSTRAC
bf850 45 34 28 20 22 4c 4f 43 4b 20 25 64 20 25 64 20  E4( "LOCK %d %d 
bf860 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65  was %d\n", pFile
bf870 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70  ->h, locktype, p
bf880 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29  File->locktype )
bf890 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  ;..  /* If there
bf8a0 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f   is already a lo
bf8b0 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20  ck of this type 
bf8c0 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74  or more restrict
bf8d0 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20  ive on the.  ** 
bf8e0 6f 73 32 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68  os2File, do noth
bf8f0 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74  ing. Don't use t
bf900 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69  he end_lock: exi
bf910 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20  t path, as.  ** 
bf920 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
bf930 74 65 72 28 29 20 68 61 73 6e 27 74 20 62 65 65  ter() hasn't bee
bf940 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20  n called yet..  
bf950 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  */.  if( pFile->
bf960 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79  locktype>=lockty
bf970 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43  pe ){.    OSTRAC
bf980 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 25 64 20  E3( "LOCK %d %d 
bf990 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64  ok (already held
bf9a0 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  )\n", pFile->h, 
bf9b0 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20  locktype );.    
bf9c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
bf9d0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65  ;.  }..  /* Make
bf9e0 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e   sure the lockin
bf9f0 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f  g sequence is co
bfa00 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73  rrect.  */.  ass
bfa10 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ert( pFile->lock
bfa20 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c  type!=NO_LOCK ||
bfa30 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45   locktype==SHARE
bfa40 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65  D_LOCK );.  asse
bfa50 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45  rt( locktype!=PE
bfa60 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20  NDING_LOCK );.  
bfa70 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
bfa80 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  !=RESERVED_LOCK 
bfa90 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  || pFile->lockty
bfaa0 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
bfab0 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 68  );..  /* Lock th
bfac0 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62  e PENDING_LOCK b
bfad0 79 74 65 20 69 66 20 77 65 20 6e 65 65 64 20 74  yte if we need t
bfae0 6f 20 61 63 71 75 69 72 65 20 61 20 50 45 4e 44  o acquire a PEND
bfaf0 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a 2a  ING lock or.  **
bfb00 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20   a SHARED lock. 
bfb10 20 49 66 20 77 65 20 61 72 65 20 61 63 71 75 69   If we are acqui
bfb20 72 69 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f  ring a SHARED lo
bfb30 63 6b 2c 20 74 68 65 20 61 63 71 75 69 73 69 74  ck, the acquisit
bfb40 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20  ion of.  ** the 
bfb50 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74  PENDING_LOCK byt
bfb60 65 20 69 73 20 74 65 6d 70 6f 72 61 72 79 2e 0a  e is temporary..
bfb70 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74 79    */.  newLockty
bfb80 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  pe = pFile->lock
bfb90 74 79 70 65 3b 0a 20 20 69 66 28 20 70 46 69 6c  type;.  if( pFil
bfba0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f  e->locktype==NO_
bfbb0 4c 4f 43 4b 0a 20 20 20 20 20 20 7c 7c 20 28 6c  LOCK.      || (l
bfbc0 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49  ocktype==EXCLUSI
bfbd0 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65  VE_LOCK && pFile
bfbe0 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45  ->locktype==RESE
bfbf0 52 56 45 44 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a  RVED_LOCK).  ){.
bfc00 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66      LockArea.lOf
bfc10 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42  fset = PENDING_B
bfc20 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65  YTE;.    LockAre
bfc30 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20  a.lRange = 1L;. 
bfc40 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f     UnlockArea.lO
bfc50 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20  ffset = 0L;.    
bfc60 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67  UnlockArea.lRang
bfc70 65 20 3d 20 30 4c 3b 0a 0a 20 20 20 20 2f 2a 20  e = 0L;..    /* 
bfc80 77 61 69 74 20 6c 6f 6e 67 65 72 20 74 68 61 6e  wait longer than
bfc90 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 68 65   LOCK_TIMEOUT he
bfca0 72 65 20 6e 6f 74 20 74 6f 20 68 61 76 65 20 74  re not to have t
bfcb0 6f 20 74 72 79 20 6d 75 6c 74 69 70 6c 65 20 74  o try multiple t
bfcc0 69 6d 65 73 20 2a 2f 0a 20 20 20 20 72 65 73 20  imes */.    res 
bfcd0 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  = DosSetFileLock
bfce0 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e  s( pFile->h, &Un
bfcf0 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41  lockArea, &LockA
bfd00 72 65 61 2c 20 31 30 30 4c 2c 20 30 4c 20 29 3b  rea, 100L, 0L );
bfd10 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d 3d 20  .    if( res == 
bfd20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
bfd30 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b    gotPendingLock
bfd40 20 3d 20 31 3b 0a 20 20 20 20 20 20 4f 53 54 52   = 1;.      OSTR
bfd50 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 70  ACE3( "LOCK %d p
bfd60 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 6f 6f 6c  ending lock bool
bfd70 65 61 6e 20 73 65 74 2e 20 20 72 65 73 3d 25 64  ean set.  res=%d
bfd80 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
bfd90 65 73 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  es );.    }.  }.
bfda0 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20  .  /* Acquire a 
bfdb0 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20 20 2a 2f  shared lock.  */
bfdc0 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  .  if( locktype=
bfdd0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20  =SHARED_LOCK && 
bfde0 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20  res == NO_ERROR 
bfdf0 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
bfe00 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
bfe10 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72  NO_LOCK );.    r
bfe20 65 73 20 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b  es = getReadLock
bfe30 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 69 66 28  (pFile);.    if(
bfe40 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52   res == NO_ERROR
bfe50 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63   ){.      newLoc
bfe60 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c  ktype = SHARED_L
bfe70 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f  OCK;.    }.    O
bfe80 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25  STRACE3( "LOCK %
bfe90 64 20 61 63 71 75 69 72 65 20 73 68 61 72 65 64  d acquire shared
bfea0 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e 22   lock. res=%d\n"
bfeb0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20  , pFile->h, res 
bfec0 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71  );.  }..  /* Acq
bfed0 75 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20  uire a RESERVED 
bfee0 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20  lock.  */.  if( 
bfef0 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56  locktype==RESERV
bff00 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d  ED_LOCK && res =
bff10 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
bff20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
bff30 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45  >locktype==SHARE
bff40 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 4c 6f  D_LOCK );.    Lo
bff50 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
bff60 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a   RESERVED_BYTE;.
bff70 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61      LockArea.lRa
bff80 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e  nge = 1L;.    Un
bff90 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  lockArea.lOffset
bffa0 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63   = 0L;.    Unloc
bffb0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
bffc0 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73  L;.    res = Dos
bffd0 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46  SetFileLocks( pF
bffe0 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41  ile->h, &UnlockA
bfff0 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20  rea, &LockArea, 
c0000 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c  LOCK_TIMEOUT, 0L
c0010 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20   );.    if( res 
c0020 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  == NO_ERROR ){. 
c0030 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65       newLocktype
c0040 20 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b   = RESERVED_LOCK
c0050 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52  ;.    }.    OSTR
c0060 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 61  ACE3( "LOCK %d a
c0070 63 71 75 69 72 65 20 72 65 73 65 72 76 65 64 20  cquire reserved 
c0080 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e 22 2c  lock. res=%d\n",
c0090 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29   pFile->h, res )
c00a0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75  ;.  }..  /* Acqu
c00b0 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f  ire a PENDING lo
c00c0 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f  ck.  */.  if( lo
c00d0 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56  cktype==EXCLUSIV
c00e0 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d  E_LOCK && res ==
c00f0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
c0100 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 50   newLocktype = P
c0110 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20  ENDING_LOCK;.   
c0120 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20   gotPendingLock 
c0130 3d 20 30 3b 0a 20 20 20 20 4f 53 54 52 41 43 45  = 0;.    OSTRACE
c0140 32 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75  2( "LOCK %d acqu
c0150 69 72 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b  ire pending lock
c0160 2e 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62  . pending lock b
c0170 6f 6f 6c 65 61 6e 20 75 6e 73 65 74 2e 5c 6e 22  oolean unset.\n"
c0180 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20  , pFile->h );.  
c0190 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20  }..  /* Acquire 
c01a0 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  an EXCLUSIVE loc
c01b0 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63  k.  */.  if( loc
c01c0 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45  ktype==EXCLUSIVE
c01d0 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d 20  _LOCK && res == 
c01e0 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
c01f0 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
c0200 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 45 44 5f  ocktype>=SHARED_
c0210 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20  LOCK );.    res 
c0220 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b  = unlockReadLock
c0230 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54  (pFile);.    OST
c0240 52 41 43 45 32 28 20 22 75 6e 72 65 61 64 6c 6f  RACE2( "unreadlo
c0250 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20  ck = %d\n", res 
c0260 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e  );.    LockArea.
c0270 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44  lOffset = SHARED
c0280 5f 46 49 52 53 54 3b 0a 20 20 20 20 4c 6f 63 6b  _FIRST;.    Lock
c0290 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48  Area.lRange = SH
c02a0 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 55  ARED_SIZE;.    U
c02b0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65  nlockArea.lOffse
c02c0 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f  t = 0L;.    Unlo
c02d0 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
c02e0 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f  0L;.    res = Do
c02f0 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70  sSetFileLocks( p
c0300 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b  File->h, &Unlock
c0310 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c  Area, &LockArea,
c0320 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30   LOCK_TIMEOUT, 0
c0330 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73  L );.    if( res
c0340 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   == NO_ERROR ){.
c0350 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70        newLocktyp
c0360 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f  e = EXCLUSIVE_LO
c0370 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  CK;.    }else{. 
c0380 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 20 22       OSTRACE2( "
c0390 4f 53 2f 32 20 65 72 72 6f 72 2d 63 6f 64 65 20  OS/2 error-code 
c03a0 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20 29 3b 0a  = %d\n", res );.
c03b0 20 20 20 20 20 20 67 65 74 52 65 61 64 4c 6f 63        getReadLoc
c03c0 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a  k(pFile);.    }.
c03d0 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c      OSTRACE3( "L
c03e0 4f 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 65  OCK %d acquire e
c03f0 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20  xclusive lock.  
c0400 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  res=%d\n", pFile
c0410 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a  ->h, res );.  }.
c0420 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20  .  /* If we are 
c0430 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e  holding a PENDIN
c0440 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68  G lock that ough
c0450 74 20 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64  t to be released
c0460 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65  , then.  ** rele
c0470 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f  ase it now..  */
c0480 0a 20 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e  .  if( gotPendin
c0490 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70  gLock && locktyp
c04a0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
c04b0 7b 0a 20 20 20 20 69 6e 74 20 72 3b 0a 20 20 20  {.    int r;.   
c04c0 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65   LockArea.lOffse
c04d0 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b  t = 0L;.    Lock
c04e0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c  Area.lRange = 0L
c04f0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ;.    UnlockArea
c0500 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49  .lOffset = PENDI
c0510 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c  NG_BYTE;.    Unl
c0520 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
c0530 20 31 4c 3b 0a 20 20 20 20 72 20 3d 20 44 6f 73   1L;.    r = Dos
c0540 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46  SetFileLocks( pF
c0550 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41  ile->h, &UnlockA
c0560 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20  rea, &LockArea, 
c0570 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c  LOCK_TIMEOUT, 0L
c0580 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33   );.    OSTRACE3
c0590 28 20 22 4c 4f 43 4b 20 25 64 20 75 6e 6c 6f 63  ( "LOCK %d unloc
c05a0 6b 69 6e 67 20 70 65 6e 64 69 6e 67 2f 69 73 20  king pending/is 
c05b0 73 68 61 72 65 64 2e 20 72 3d 25 64 5c 6e 22 2c  shared. r=%d\n",
c05c0 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a   pFile->h, r );.
c05d0 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65    }..  /* Update
c05e0 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68   the state of th
c05f0 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64 20  e lock has held 
c0600 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  in the file desc
c0610 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a 2a  riptor then.  **
c0620 20 72 65 74 75 72 6e 20 74 68 65 20 61 70 70 72   return the appr
c0630 6f 70 72 69 61 74 65 20 72 65 73 75 6c 74 20 63  opriate result c
c0640 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ode..  */.  if( 
c0650 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20  res == NO_ERROR 
c0660 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
c0670 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a  TE_OK;.  }else{.
c0680 20 20 20 20 4f 53 54 52 41 43 45 34 28 20 22 4c      OSTRACE4( "L
c0690 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 20 74 72  OCK FAILED %d tr
c06a0 79 69 6e 67 20 66 6f 72 20 25 64 20 62 75 74 20  ying for %d but 
c06b0 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65  got %d\n", pFile
c06c0 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ->h,.           
c06d0 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 77     locktype, new
c06e0 4c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20  Locktype );.    
c06f0 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
c0700 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c  ;.  }.  pFile->l
c0710 6f 63 6b 74 79 70 65 20 3d 20 6e 65 77 4c 6f 63  ocktype = newLoc
c0720 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45  ktype;.  OSTRACE
c0730 33 28 20 22 4c 4f 43 4b 20 25 64 20 6e 6f 77 20  3( "LOCK %d now 
c0740 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
c0750 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
c0760 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   );.  return rc;
c0770 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
c0780 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66  outine checks if
c0790 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45   there is a RESE
c07a0 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f  RVED lock held o
c07b0 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a  n the specified.
c07c0 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20  ** file by this 
c07d0 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f  or any other pro
c07e0 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20  cess. If such a 
c07f0 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65  lock is held, re
c0800 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f  turn.** non-zero
c0810 2c 20 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f  , otherwise zero
c0820 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
c0830 6f 73 32 43 68 65 63 6b 52 65 73 65 72 76 65 64  os2CheckReserved
c0840 4c 6f 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69  Lock( sqlite3_fi
c0850 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 4f 75  le *id, int *pOu
c0860 74 20 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20 30  t ){.  int r = 0
c0870 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69  ;.  os2File *pFi
c0880 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69  le = (os2File*)i
c0890 64 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  d;.  assert( pFi
c08a0 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  le!=0 );.  if( p
c08b0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d  File->locktype>=
c08c0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b  RESERVED_LOCK ){
c08d0 0a 20 20 20 20 72 20 3d 20 31 3b 0a 20 20 20 20  .    r = 1;.    
c08e0 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20  OSTRACE3( "TEST 
c08f0 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 6c  WR-LOCK %d %d (l
c0900 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65 2d  ocal)\n", pFile-
c0910 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 65 6c 73 65  >h, r );.  }else
c0920 7b 0a 20 20 20 20 46 49 4c 45 4c 4f 43 4b 20 20  {.    FILELOCK  
c0930 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20  LockArea,.      
c0940 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72          UnlockAr
c0950 65 61 3b 0a 20 20 20 20 41 50 49 52 45 54 20 72  ea;.    APIRET r
c0960 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20  c = NO_ERROR;.  
c0970 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72    memset(&LockAr
c0980 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f  ea, 0, sizeof(Lo
c0990 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 20 6d 65  ckArea));.    me
c09a0 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61  mset(&UnlockArea
c09b0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f  , 0, sizeof(Unlo
c09c0 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 20 4c 6f  ckArea));.    Lo
c09d0 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
c09e0 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a   RESERVED_BYTE;.
c09f0 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61      LockArea.lRa
c0a00 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e  nge = 1L;.    Un
c0a10 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  lockArea.lOffset
c0a20 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63   = 0L;.    Unloc
c0a30 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30  kArea.lRange = 0
c0a40 4c 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 53  L;.    rc = DosS
c0a50 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69  etFileLocks( pFi
c0a60 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72  le->h, &UnlockAr
c0a70 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c  ea, &LockArea, L
c0a80 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20  OCK_TIMEOUT, 0L 
c0a90 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28  );.    OSTRACE3(
c0aa0 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25   "TEST WR-LOCK %
c0ab0 64 20 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20  d lock reserved 
c0ac0 62 79 74 65 20 72 63 3d 25 64 5c 6e 22 2c 20 70  byte rc=%d\n", p
c0ad0 46 69 6c 65 2d 3e 68 2c 20 72 63 20 29 3b 0a 20  File->h, rc );. 
c0ae0 20 20 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f     if( rc == NO_
c0af0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 41  ERROR ){.      A
c0b00 50 49 52 45 54 20 72 63 75 20 3d 20 4e 4f 5f 45  PIRET rcu = NO_E
c0b10 52 52 4f 52 3b 20 2f 2a 20 72 65 74 75 72 6e 20  RROR; /* return 
c0b20 63 6f 64 65 20 66 6f 72 20 75 6e 6c 6f 63 6b 69  code for unlocki
c0b30 6e 67 20 2a 2f 0a 20 20 20 20 20 20 4c 6f 63 6b  ng */.      Lock
c0b40 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30  Area.lOffset = 0
c0b50 4c 3b 0a 20 20 20 20 20 20 4c 6f 63 6b 41 72 65  L;.      LockAre
c0b60 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20  a.lRange = 0L;. 
c0b70 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e       UnlockArea.
c0b80 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52 56  lOffset = RESERV
c0b90 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 55  ED_BYTE;.      U
c0ba0 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
c0bb0 20 3d 20 31 4c 3b 0a 20 20 20 20 20 20 72 63 75   = 1L;.      rcu
c0bc0 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63   = DosSetFileLoc
c0bd0 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55  ks( pFile->h, &U
c0be0 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b  nlockArea, &Lock
c0bf0 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f  Area, LOCK_TIMEO
c0c00 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 20 20  UT, 0L );.      
c0c10 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20  OSTRACE3( "TEST 
c0c20 57 52 2d 4c 4f 43 4b 20 25 64 20 75 6e 6c 6f 63  WR-LOCK %d unloc
c0c30 6b 20 72 65 73 65 72 76 65 64 20 62 79 74 65 20  k reserved byte 
c0c40 72 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  r=%d\n", pFile->
c0c50 68 2c 20 72 63 75 20 29 3b 0a 20 20 20 20 7d 0a  h, rcu );.    }.
c0c60 20 20 20 20 72 20 3d 20 21 28 72 63 20 3d 3d 20      r = !(rc == 
c0c70 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 20 20 4f  NO_ERROR);.    O
c0c80 53 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57  STRACE3( "TEST W
c0c90 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 72 65  R-LOCK %d %d (re
c0ca0 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c 65 2d  mote)\n", pFile-
c0cb0 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 0a 20 20 2a  >h, r );.  }.  *
c0cc0 70 4f 75 74 20 3d 20 72 3b 0a 20 20 72 65 74 75  pOut = r;.  retu
c0cd0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
c0ce0 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65  ./*.** Lower the
c0cf0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
c0d00 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
c0d10 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65  r id to locktype
c0d20 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d  .  locktype.** m
c0d30 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f  ust be either NO
c0d40 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f  _LOCK or SHARED_
c0d50 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  LOCK..**.** If t
c0d60 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
c0d70 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73   of the file des
c0d80 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61  criptor is alrea
c0d90 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a  dy at or below.*
c0da0 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  * the requested 
c0db0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74  locking level, t
c0dc0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
c0dd0 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74   no-op..**.** It
c0de0 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
c0df0 20 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e   for this routin
c0e00 65 20 74 6f 20 66 61 69 6c 20 69 66 20 74 68 65  e to fail if the
c0e10 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
c0e20 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20  .** is NO_LOCK. 
c0e30 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61   If the second a
c0e40 72 67 75 6d 65 6e 74 20 69 73 20 53 48 41 52 45  rgument is SHARE
c0e50 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 73  D_LOCK then this
c0e60 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68   routine.** migh
c0e70 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  t return SQLITE_
c0e80 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 69 63  IOERR;.*/.static
c0e90 20 69 6e 74 20 6f 73 32 55 6e 6c 6f 63 6b 28 20   int os2Unlock( 
c0ea0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
c0eb0 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 20 29  , int locktype )
c0ec0 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20  {.  int type;.  
c0ed0 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  os2File *pFile =
c0ee0 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20   (os2File*)id;. 
c0ef0 20 41 50 49 52 45 54 20 72 63 20 3d 20 53 51 4c   APIRET rc = SQL
c0f00 49 54 45 5f 4f 4b 3b 0a 20 20 41 50 49 52 45 54  ITE_OK;.  APIRET
c0f10 20 72 65 73 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b   res = NO_ERROR;
c0f20 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63  .  FILELOCK  Loc
c0f30 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20  kArea,.         
c0f40 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20     UnlockArea;. 
c0f50 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65   memset(&LockAre
c0f60 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63  a, 0, sizeof(Loc
c0f70 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65  kArea));.  memse
c0f80 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30  t(&UnlockArea, 0
c0f90 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41  , sizeof(UnlockA
c0fa0 72 65 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28  rea));.  assert(
c0fb0 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 61   pFile!=0 );.  a
c0fc0 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c  ssert( locktype<
c0fd0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
c0fe0 20 20 4f 53 54 52 41 43 45 34 28 20 22 55 4e 4c    OSTRACE4( "UNL
c0ff0 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 77 61 73  OCK %d to %d was
c1000 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68   %d\n", pFile->h
c1010 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c  , locktype, pFil
c1020 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20  e->locktype );. 
c1030 20 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c   type = pFile->l
c1040 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74  ocktype;.  if( t
c1050 79 70 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ype>=EXCLUSIVE_L
c1060 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41  OCK ){.    LockA
c1070 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c  rea.lOffset = 0L
c1080 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  ;.    LockArea.l
c1090 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20  Range = 0L;.    
c10a0 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73  UnlockArea.lOffs
c10b0 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53  et = SHARED_FIRS
c10c0 54 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  T;.    UnlockAre
c10d0 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45  a.lRange = SHARE
c10e0 44 5f 53 49 5a 45 3b 0a 20 20 20 20 72 65 73 20  D_SIZE;.    res 
c10f0 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  = DosSetFileLock
c1100 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e  s( pFile->h, &Un
c1110 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41  lockArea, &LockA
c1120 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55  rea, LOCK_TIMEOU
c1130 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54  T, 0L );.    OST
c1140 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25  RACE3( "UNLOCK %
c1150 64 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  d exclusive lock
c1160 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c   res=%d\n", pFil
c1170 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 20  e->h, res );.   
c1180 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53   if( locktype==S
c1190 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 67 65  HARED_LOCK && ge
c11a0 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29  tReadLock(pFile)
c11b0 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   != NO_ERROR ){.
c11c0 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 68        /* This sh
c11d0 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65  ould never happe
c11e0 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64 20 61 6c  n.  We should al
c11f0 77 61 79 73 20 62 65 20 61 62 6c 65 20 74 6f 0a  ways be able to.
c1200 20 20 20 20 20 20 2a 2a 20 72 65 61 63 71 75 69        ** reacqui
c1210 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b  re the read lock
c1220 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54 52 41 43   */.      OSTRAC
c1230 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 74  E3( "UNLOCK %d t
c1240 6f 20 25 64 20 67 65 74 52 65 61 64 4c 6f 63 6b  o %d getReadLock
c1250 28 29 20 66 61 69 6c 65 64 5c 6e 22 2c 20 70 46  () failed\n", pF
c1260 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65  ile->h, locktype
c1270 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   );.      rc = S
c1280 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f  QLITE_IOERR_UNLO
c1290 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  CK;.    }.  }.  
c12a0 69 66 28 20 74 79 70 65 3e 3d 52 45 53 45 52 56  if( type>=RESERV
c12b0 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c  ED_LOCK ){.    L
c12c0 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
c12d0 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72  = 0L;.    LockAr
c12e0 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a  ea.lRange = 0L;.
c12f0 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
c1300 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52 56 45  Offset = RESERVE
c1310 44 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f  D_BYTE;.    Unlo
c1320 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
c1330 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f  1L;.    res = Do
c1340 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70  sSetFileLocks( p
c1350 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b  File->h, &Unlock
c1360 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c  Area, &LockArea,
c1370 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30   LOCK_TIMEOUT, 0
c1380 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45  L );.    OSTRACE
c1390 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 72 65  3( "UNLOCK %d re
c13a0 73 65 72 76 65 64 20 72 65 73 3d 25 64 5c 6e 22  served res=%d\n"
c13b0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20  , pFile->h, res 
c13c0 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63  );.  }.  if( loc
c13d0 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26  ktype==NO_LOCK &
c13e0 26 20 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c  & type>=SHARED_L
c13f0 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 20 3d  OCK ){.    res =
c1400 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28   unlockReadLock(
c1410 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52  pFile);.    OSTR
c1420 41 43 45 35 28 20 22 55 4e 4c 4f 43 4b 20 25 64  ACE5( "UNLOCK %d
c1430 20 69 73 20 25 64 20 77 61 6e 74 20 25 64 20 72   is %d want %d r
c1440 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  es=%d\n", pFile-
c1450 3e 68 2c 20 74 79 70 65 2c 20 6c 6f 63 6b 74 79  >h, type, lockty
c1460 70 65 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 20  pe, res );.  }. 
c1470 20 69 66 28 20 74 79 70 65 3e 3d 50 45 4e 44 49   if( type>=PENDI
c1480 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c  NG_LOCK ){.    L
c1490 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
c14a0 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72  = 0L;.    LockAr
c14b0 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a  ea.lRange = 0L;.
c14c0 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
c14d0 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47  Offset = PENDING
c14e0 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f 63  _BYTE;.    Unloc
c14f0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31  kArea.lRange = 1
c1500 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73  L;.    res = Dos
c1510 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46  SetFileLocks( pF
c1520 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41  ile->h, &UnlockA
c1530 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20  rea, &LockArea, 
c1540 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c  LOCK_TIMEOUT, 0L
c1550 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33   );.    OSTRACE3
c1560 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 70 65 6e  ( "UNLOCK %d pen
c1570 64 69 6e 67 20 72 65 73 3d 25 64 5c 6e 22 2c 20  ding res=%d\n", 
c1580 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b  pFile->h, res );
c1590 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f  .  }.  pFile->lo
c15a0 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70  cktype = locktyp
c15b0 65 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22  e;.  OSTRACE3( "
c15c0 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f 77 20 25 64  UNLOCK %d now %d
c15d0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70  \n", pFile->h, p
c15e0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29  File->locktype )
c15f0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
c1600 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c 20  ../*.** Control 
c1610 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74 68 65  and query of the
c1620 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c   open file handl
c1630 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
c1640 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72 6f 6c 28   os2FileControl(
c1650 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
c1660 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a  , int op, void *
c1670 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28  pArg){.  switch(
c1680 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20   op ){.    case 
c1690 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43  SQLITE_FCNTL_LOC
c16a0 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20  KSTATE: {.      
c16b0 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28  *(int*)pArg = ((
c16c0 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f  os2File*)id)->lo
c16d0 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 4f 53  cktype;.      OS
c16e0 54 52 41 43 45 33 28 20 22 46 43 4e 54 4c 5f 4c  TRACE3( "FCNTL_L
c16f0 4f 43 4b 53 54 41 54 45 20 25 64 20 6c 6f 63 6b  OCKSTATE %d lock
c1700 3d 25 64 5c 6e 22 2c 20 28 28 6f 73 32 46 69 6c  =%d\n", ((os2Fil
c1710 65 2a 29 69 64 29 2d 3e 68 2c 20 28 28 6f 73 32  e*)id)->h, ((os2
c1720 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74  File*)id)->lockt
c1730 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72 65 74  ype );.      ret
c1740 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
c1750 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
c1760 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
c1770 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
c1780 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20  the sector size 
c1790 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20  in bytes of the 
c17a0 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b  underlying block
c17b0 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74   device for.** t
c17c0 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c  he specified fil
c17d0 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73  e. This is almos
c17e0 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74  t always 512 byt
c17f0 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a  es, but may be.*
c1800 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d  * larger for som
c1810 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a  e devices..**.**
c1820 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73   SQLite code ass
c1830 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69  umes this functi
c1840 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20  on cannot fail. 
c1850 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20  It also assumes 
c1860 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66  that.** if two f
c1870 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 64  iles are created
c1880 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c   in the same fil
c1890 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f  e-system directo
c18a0 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61  ry (i.e..** a da
c18b0 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a  tabase and its j
c18c0 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61  ournal file) tha
c18d0 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a  t the sector siz
c18e0 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a  e will be the.**
c18f0 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a   same for both..
c1900 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
c1910 32 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69  2SectorSize(sqli
c1920 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20  te3_file *id){. 
c1930 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44   return SQLITE_D
c1940 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49  EFAULT_SECTOR_SI
c1950 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ZE;.}../*.** Ret
c1960 75 72 6e 20 61 20 76 65 63 74 6f 72 20 6f 66 20  urn a vector of 
c1970 64 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72  device character
c1980 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 61 74 69  istics..*/.stati
c1990 63 20 69 6e 74 20 6f 73 32 44 65 76 69 63 65 43  c int os2DeviceC
c19a0 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73  haracteristics(s
c19b0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
c19c0 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  {.  return 0;.}.
c19d0 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61 63 74 65  ../*.** Characte
c19e0 72 20 73 65 74 20 63 6f 6e 76 65 72 73 69 6f 6e  r set conversion
c19f0 20 6f 62 6a 65 63 74 73 20 75 73 65 64 20 62 79   objects used by
c1a00 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72 6f 75 74   conversion rout
c1a10 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ines..*/.static 
c1a20 55 63 6f 6e 76 4f 62 6a 65 63 74 20 75 63 55 74  UconvObject ucUt
c1a30 66 38 20 3d 20 4e 55 4c 4c 3b 20 2f 2a 20 63 6f  f8 = NULL; /* co
c1a40 6e 76 65 72 74 20 62 65 74 77 65 65 6e 20 55 54  nvert between UT
c1a50 46 2d 38 20 61 6e 64 20 55 43 53 2d 32 20 2a 2f  F-8 and UCS-2 */
c1a60 0a 73 74 61 74 69 63 20 55 63 6f 6e 76 4f 62 6a  .static UconvObj
c1a70 65 63 74 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c  ect uclCp = NULL
c1a80 3b 20 20 2f 2a 20 63 6f 6e 76 65 72 74 20 62 65  ;  /* convert be
c1a90 74 77 65 65 6e 20 6c 6f 63 61 6c 20 63 6f 64 65  tween local code
c1aa0 70 61 67 65 20 61 6e 64 20 55 43 53 2d 32 20 2a  page and UCS-2 *
c1ab0 2f 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20  /../*.** Helper 
c1ac0 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 69 74  function to init
c1ad0 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e 76 65  ialize the conve
c1ae0 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 66 72  rsion objects fr
c1af0 6f 6d 20 61 6e 64 20 74 6f 20 55 54 46 2d 38 2e  om and to UTF-8.
c1b00 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
c1b10 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73  initUconvObjects
c1b20 28 20 76 6f 69 64 20 29 7b 0a 20 20 69 66 28 20  ( void ){.  if( 
c1b30 55 6e 69 43 72 65 61 74 65 55 63 6f 6e 76 4f 62  UniCreateUconvOb
c1b40 6a 65 63 74 28 20 55 54 46 5f 38 2c 20 26 75 63  ject( UTF_8, &uc
c1b50 55 74 66 38 20 29 20 21 3d 20 55 4c 53 5f 53 55  Utf8 ) != ULS_SU
c1b60 43 43 45 53 53 20 29 0a 20 20 20 20 75 63 55 74  CCESS ).    ucUt
c1b70 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 20 69 66 20  f8 = NULL;.  if 
c1b80 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f 6e 76  ( UniCreateUconv
c1b90 4f 62 6a 65 63 74 28 20 28 55 6e 69 43 68 61 72  Object( (UniChar
c1ba0 20 2a 29 4c 22 40 70 61 74 68 3d 79 65 73 22 2c   *)L"@path=yes",
c1bb0 20 26 75 63 6c 43 70 20 29 20 21 3d 20 55 4c 53   &uclCp ) != ULS
c1bc0 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 75  _SUCCESS ).    u
c1bd0 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0a 7d 0a 0a  clCp = NULL;.}..
c1be0 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e  /*.** Helper fun
c1bf0 63 74 69 6f 6e 20 74 6f 20 66 72 65 65 20 74 68  ction to free th
c1c00 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a  e conversion obj
c1c10 65 63 74 73 20 66 72 6f 6d 20 61 6e 64 20 74 6f  ects from and to
c1c20 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69   UTF-8..*/.stati
c1c30 63 20 76 6f 69 64 20 66 72 65 65 55 63 6f 6e 76  c void freeUconv
c1c40 4f 62 6a 65 63 74 73 28 20 76 6f 69 64 20 29 7b  Objects( void ){
c1c50 0a 20 20 69 66 20 28 20 75 63 55 74 66 38 20 29  .  if ( ucUtf8 )
c1c60 0a 20 20 20 20 55 6e 69 46 72 65 65 55 63 6f 6e  .    UniFreeUcon
c1c70 76 4f 62 6a 65 63 74 28 20 75 63 55 74 66 38 20  vObject( ucUtf8 
c1c80 29 3b 0a 20 20 69 66 20 28 20 75 63 6c 43 70 20  );.  if ( uclCp 
c1c90 29 0a 20 20 20 20 55 6e 69 46 72 65 65 55 63 6f  ).    UniFreeUco
c1ca0 6e 76 4f 62 6a 65 63 74 28 20 75 63 6c 43 70 20  nvObject( uclCp 
c1cb0 29 3b 0a 20 20 75 63 55 74 66 38 20 3d 20 4e 55  );.  ucUtf8 = NU
c1cc0 4c 4c 3b 0a 20 20 75 63 6c 43 70 20 3d 20 4e 55  LL;.  uclCp = NU
c1cd0 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c  LL;.}../*.** Hel
c1ce0 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  per function to 
c1cf0 63 6f 6e 76 65 72 74 20 55 54 46 2d 38 20 66 69  convert UTF-8 fi
c1d00 6c 65 6e 61 6d 65 73 20 74 6f 20 6c 6f 63 61 6c  lenames to local
c1d10 20 4f 53 2f 32 20 63 6f 64 65 70 61 67 65 2e 0a   OS/2 codepage..
c1d20 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 70 20  ** The two-step 
c1d30 70 72 6f 63 65 73 73 3a 20 66 69 72 73 74 20 63  process: first c
c1d40 6f 6e 76 65 72 74 20 74 68 65 20 69 6e 63 6f 6d  onvert the incom
c1d50 69 6e 67 20 55 54 46 2d 38 20 73 74 72 69 6e 67  ing UTF-8 string
c1d60 0a 2a 2a 20 69 6e 74 6f 20 55 43 53 2d 32 20 61  .** into UCS-2 a
c1d70 6e 64 20 74 68 65 6e 20 66 72 6f 6d 20 55 43 53  nd then from UCS
c1d80 2d 32 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e  -2 to the curren
c1d90 74 20 63 6f 64 65 70 61 67 65 2e 0a 2a 2a 20 54  t codepage..** T
c1da0 68 65 20 72 65 74 75 72 6e 65 64 20 63 68 61 72  he returned char
c1db0 20 70 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20   pointer has to 
c1dc0 62 65 20 66 72 65 65 64 2e 0a 2a 2f 0a 73 74 61  be freed..*/.sta
c1dd0 74 69 63 20 63 68 61 72 20 2a 63 6f 6e 76 65 72  tic char *conver
c1de0 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 63  tUtf8PathToCp( c
c1df0 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 20 29 7b  onst char *in ){
c1e00 0a 20 20 55 6e 69 43 68 61 72 20 74 65 6d 70 50  .  UniChar tempP
c1e10 61 74 68 5b 43 43 48 4d 41 58 50 41 54 48 5d 3b  ath[CCHMAXPATH];
c1e20 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 3d 20 28  .  char *out = (
c1e30 63 68 61 72 20 2a 29 63 61 6c 6c 6f 63 28 20 43  char *)calloc( C
c1e40 43 48 4d 41 58 50 41 54 48 2c 20 31 20 29 3b 0a  CHMAXPATH, 1 );.
c1e50 0a 20 20 69 66 28 20 21 6f 75 74 20 29 0a 20 20  .  if( !out ).  
c1e60 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b 0a 0a    return NULL;..
c1e70 20 20 69 66 28 20 21 75 63 55 74 66 38 20 7c 7c    if( !ucUtf8 ||
c1e80 20 21 75 63 6c 43 70 20 29 0a 20 20 20 20 69 6e   !uclCp ).    in
c1e90 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29  itUconvObjects()
c1ea0 3b 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e  ;..  /* determin
c1eb0 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65  e string for the
c1ec0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55   conversion of U
c1ed0 54 46 2d 38 20 77 68 69 63 68 20 69 73 20 43 50  TF-8 which is CP
c1ee0 31 32 30 38 20 2a 2f 0a 20 20 69 66 28 20 55 6e  1208 */.  if( Un
c1ef0 69 53 74 72 54 6f 55 63 73 28 20 75 63 55 74 66  iStrToUcs( ucUtf
c1f00 38 2c 20 74 65 6d 70 50 61 74 68 2c 20 28 63 68  8, tempPath, (ch
c1f10 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d 41 58 50  ar *)in, CCHMAXP
c1f20 41 54 48 20 29 20 21 3d 20 55 4c 53 5f 53 55 43  ATH ) != ULS_SUC
c1f30 43 45 53 53 20 29 0a 20 20 20 20 72 65 74 75 72  CESS ).    retur
c1f40 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 20 63 6f 6e  n out; /* if con
c1f50 76 65 72 73 69 6f 6e 20 66 61 69 6c 73 2c 20 72  version fails, r
c1f60 65 74 75 72 6e 20 74 68 65 20 65 6d 70 74 79 20  eturn the empty 
c1f70 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 2f 2a 20  string */..  /* 
c1f80 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20 63  conversion for c
c1f90 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20  urrent codepage 
c1fa0 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65  which can be use
c1fb0 64 20 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a 20  d for paths */. 
c1fc0 20 55 6e 69 53 74 72 46 72 6f 6d 55 63 73 28 20   UniStrFromUcs( 
c1fd0 75 63 6c 43 70 2c 20 6f 75 74 2c 20 74 65 6d 70  uclCp, out, temp
c1fe0 50 61 74 68 2c 20 43 43 48 4d 41 58 50 41 54 48  Path, CCHMAXPATH
c1ff0 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6f 75   );..  return ou
c2000 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70  t;.}../*.** Help
c2010 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63  er function to c
c2020 6f 6e 76 65 72 74 20 66 69 6c 65 6e 61 6d 65 73  onvert filenames
c2030 20 66 72 6f 6d 20 6c 6f 63 61 6c 20 63 6f 64 65   from local code
c2040 70 61 67 65 20 74 6f 20 55 54 46 2d 38 2e 0a 2a  page to UTF-8..*
c2050 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 70 20 70  * The two-step p
c2060 72 6f 63 65 73 73 3a 20 66 69 72 73 74 20 63 6f  rocess: first co
c2070 6e 76 65 72 74 20 74 68 65 20 69 6e 63 6f 6d 69  nvert the incomi
c2080 6e 67 20 63 6f 64 65 70 61 67 65 2d 73 70 65 63  ng codepage-spec
c2090 69 66 69 63 0a 2a 2a 20 73 74 72 69 6e 67 20 69  ific.** string i
c20a0 6e 74 6f 20 55 43 53 2d 32 20 61 6e 64 20 74 68  nto UCS-2 and th
c20b0 65 6e 20 66 72 6f 6d 20 55 43 53 2d 32 20 74 6f  en from UCS-2 to
c20c0 20 74 68 65 20 63 6f 64 65 70 61 67 65 20 6f 66   the codepage of
c20d0 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 72   UTF-8..** The r
c20e0 65 74 75 72 6e 65 64 20 63 68 61 72 20 70 6f 69  eturned char poi
c20f0 6e 74 65 72 20 68 61 73 20 74 6f 20 62 65 20 66  nter has to be f
c2100 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  reed..**.** This
c2110 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 6e   function is non
c2120 2d 73 74 61 74 69 63 20 74 6f 20 62 65 20 61 62  -static to be ab
c2130 6c 65 20 74 6f 20 75 73 65 20 74 68 69 73 20 69  le to use this i
c2140 6e 20 73 68 65 6c 6c 2e 63 20 61 6e 64 0a 2a 2a  n shell.c and.**
c2150 20 73 69 6d 69 6c 61 72 20 61 70 70 6c 69 63 61   similar applica
c2160 74 69 6f 6e 73 20 74 68 61 74 20 74 61 6b 65 20  tions that take 
c2170 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 20 61 72 67  command line arg
c2180 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 63 68 61 72 20  uments..*/.char 
c2190 2a 63 6f 6e 76 65 72 74 43 70 50 61 74 68 54 6f  *convertCpPathTo
c21a0 55 74 66 38 28 20 63 6f 6e 73 74 20 63 68 61 72  Utf8( const char
c21b0 20 2a 69 6e 20 29 7b 0a 20 20 55 6e 69 43 68 61   *in ){.  UniCha
c21c0 72 20 74 65 6d 70 50 61 74 68 5b 43 43 48 4d 41  r tempPath[CCHMA
c21d0 58 50 41 54 48 5d 3b 0a 20 20 63 68 61 72 20 2a  XPATH];.  char *
c21e0 6f 75 74 20 3d 20 28 63 68 61 72 20 2a 29 63 61  out = (char *)ca
c21f0 6c 6c 6f 63 28 20 43 43 48 4d 41 58 50 41 54 48  lloc( CCHMAXPATH
c2200 2c 20 31 20 29 3b 0a 0a 20 20 69 66 28 20 21 6f  , 1 );..  if( !o
c2210 75 74 20 29 0a 20 20 20 20 72 65 74 75 72 6e 20  ut ).    return 
c2220 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 20 21 75 63  NULL;..  if( !uc
c2230 55 74 66 38 20 7c 7c 20 21 75 63 6c 43 70 20 29  Utf8 || !uclCp )
c2240 0a 20 20 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62  .    initUconvOb
c2250 6a 65 63 74 73 28 29 3b 0a 0a 20 20 2f 2a 20 63  jects();..  /* c
c2260 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20 63 75  onversion for cu
c2270 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 77  rrent codepage w
c2280 68 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64  hich can be used
c2290 20 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a 20 20   for paths */.  
c22a0 69 66 28 20 55 6e 69 53 74 72 54 6f 55 63 73 28  if( UniStrToUcs(
c22b0 20 75 63 6c 43 70 2c 20 74 65 6d 70 50 61 74 68   uclCp, tempPath
c22c0 2c 20 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43  , (char *)in, CC
c22d0 48 4d 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c  HMAXPATH ) != UL
c22e0 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20 20  S_SUCCESS ).    
c22f0 72 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69  return out; /* i
c2300 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69  f conversion fai
c2310 6c 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65  ls, return the e
c2320 6d 70 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a  mpty string */..
c2330 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e 65 20 73    /* determine s
c2340 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 63 6f  tring for the co
c2350 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55 54 46 2d  nversion of UTF-
c2360 38 20 77 68 69 63 68 20 69 73 20 43 50 31 32 30  8 which is CP120
c2370 38 20 2a 2f 0a 20 20 55 6e 69 53 74 72 46 72 6f  8 */.  UniStrFro
c2380 6d 55 63 73 28 20 75 63 55 74 66 38 2c 20 6f 75  mUcs( ucUtf8, ou
c2390 74 2c 20 74 65 6d 70 50 61 74 68 2c 20 43 43 48  t, tempPath, CCH
c23a0 4d 41 58 50 41 54 48 20 29 3b 0a 0a 20 20 72 65  MAXPATH );..  re
c23b0 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a  turn out;.}../*.
c23c0 2a 2a 20 54 68 69 73 20 76 65 63 74 6f 72 20 64  ** This vector d
c23d0 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d  efines all the m
c23e0 65 74 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20  ethods that can 
c23f0 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a  operate on an.**
c2400 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 66 6f   sqlite3_file fo
c2410 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74 61 74 69 63  r os2..*/.static
c2420 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
c2430 6f 5f 6d 65 74 68 6f 64 73 20 6f 73 32 49 6f 4d  o_methods os2IoM
c2440 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20  ethod = {.  1,  
c2450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2460 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
c2470 6e 20 2a 2f 0a 20 20 6f 73 32 43 6c 6f 73 65 2c  n */.  os2Close,
c2480 0a 20 20 6f 73 32 52 65 61 64 2c 0a 20 20 6f 73  .  os2Read,.  os
c2490 32 57 72 69 74 65 2c 0a 20 20 6f 73 32 54 72 75  2Write,.  os2Tru
c24a0 6e 63 61 74 65 2c 0a 20 20 6f 73 32 53 79 6e 63  ncate,.  os2Sync
c24b0 2c 0a 20 20 6f 73 32 46 69 6c 65 53 69 7a 65 2c  ,.  os2FileSize,
c24c0 0a 20 20 6f 73 32 4c 6f 63 6b 2c 0a 20 20 6f 73  .  os2Lock,.  os
c24d0 32 55 6e 6c 6f 63 6b 2c 0a 20 20 6f 73 32 43 68  2Unlock,.  os2Ch
c24e0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c  eckReservedLock,
c24f0 0a 20 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72 6f  .  os2FileContro
c2500 6c 2c 0a 20 20 6f 73 32 53 65 63 74 6f 72 53 69  l,.  os2SectorSi
c2510 7a 65 2c 0a 20 20 6f 73 32 44 65 76 69 63 65 43  ze,.  os2DeviceC
c2520 68 61 72 61 63 74 65 72 69 73 74 69 63 73 0a 7d  haracteristics.}
c2530 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;../************
c2540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
c2580 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 74 68 65  ** Here ends the
c2590 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 74 68 61   I/O methods tha
c25a0 74 20 66 6f 72 6d 20 74 68 65 20 73 71 6c 69 74  t form the sqlit
c25b0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62  e3_io_methods ob
c25c0 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ject..**.** The 
c25d0 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f  next block of co
c25e0 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  de implements th
c25f0 65 20 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a  e VFS methods..*
c2600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
c2650 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d  .** Create a tem
c2660 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65  porary file name
c2670 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20   in zBuf.  zBuf 
c2680 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75  must be big enou
c2690 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20 61 74  gh to.** hold at
c26a0 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d   pVfs->mxPathnam
c26b0 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2f  e characters..*/
c26c0 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 54  .static int getT
c26d0 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66  empname(int nBuf
c26e0 2c 20 63 68 61 72 20 2a 7a 42 75 66 20 29 7b 0a  , char *zBuf ){.
c26f0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
c2700 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a 43 68  nsigned char zCh
c2710 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63  ars[] =.    "abc
c2720 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73  defghijklmnopqrs
c2730 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42  tuvwxyz".    "AB
c2740 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52  CDEFGHIJKLMNOPQR
c2750 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30  STUVWXYZ".    "0
c2760 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20 69 6e  123456789";.  in
c2770 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61 72 20 7a  t i, j;.  char z
c2780 54 65 6d 70 50 61 74 68 42 75 66 5b 33 5d 3b 0a  TempPathBuf[3];.
c2790 20 20 50 53 5a 20 7a 54 65 6d 70 50 61 74 68 20    PSZ zTempPath 
c27a0 3d 20 28 50 53 5a 29 26 7a 54 65 6d 70 50 61 74  = (PSZ)&zTempPat
c27b0 68 42 75 66 3b 0a 20 20 69 66 28 20 73 71 6c 69  hBuf;.  if( sqli
c27c0 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f  te3_temp_directo
c27d0 72 79 20 29 7b 0a 20 20 20 20 7a 54 65 6d 70 50  ry ){.    zTempP
c27e0 61 74 68 20 3d 20 73 71 6c 69 74 65 33 5f 74 65  ath = sqlite3_te
c27f0 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 20 20  mp_directory;.  
c2800 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 44  }else{.    if( D
c2810 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53 5a 29  osScanEnv( (PSZ)
c2820 22 54 45 4d 50 22 2c 20 26 7a 54 65 6d 70 50 61  "TEMP", &zTempPa
c2830 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20 69 66  th ) ){.      if
c2840 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50  ( DosScanEnv( (P
c2850 53 5a 29 22 54 4d 50 22 2c 20 26 7a 54 65 6d 70  SZ)"TMP", &zTemp
c2860 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20  Path ) ){.      
c2870 20 20 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76    if( DosScanEnv
c2880 28 20 28 50 53 5a 29 22 54 4d 50 44 49 52 22 2c  ( (PSZ)"TMPDIR",
c2890 20 26 7a 54 65 6d 70 50 61 74 68 20 29 20 29 7b   &zTempPath ) ){
c28a0 0a 20 20 20 20 20 20 20 20 20 20 20 55 4c 4f 4e  .           ULON
c28b0 47 20 75 6c 44 72 69 76 65 4e 75 6d 20 3d 20 30  G ulDriveNum = 0
c28c0 2c 20 75 6c 44 72 69 76 65 4d 61 70 20 3d 20 30  , ulDriveMap = 0
c28d0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 44 6f 73  ;.           Dos
c28e0 51 75 65 72 79 43 75 72 72 65 6e 74 44 69 73 6b  QueryCurrentDisk
c28f0 28 20 26 75 6c 44 72 69 76 65 4e 75 6d 2c 20 26  ( &ulDriveNum, &
c2900 75 6c 44 72 69 76 65 4d 61 70 20 29 3b 0a 20 20  ulDriveMap );.  
c2910 20 20 20 20 20 20 20 20 20 73 70 72 69 6e 74 66           sprintf
c2920 28 20 28 63 68 61 72 2a 29 7a 54 65 6d 70 50 61  ( (char*)zTempPa
c2930 74 68 2c 20 22 25 63 3a 22 2c 20 28 63 68 61 72  th, "%c:", (char
c2940 29 28 20 27 41 27 20 2b 20 75 6c 44 72 69 76 65  )( 'A' + ulDrive
c2950 4e 75 6d 20 2d 20 31 20 29 20 29 3b 0a 20 20 20  Num - 1 ) );.   
c2960 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
c2970 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 53 74     }.  }.  /* St
c2980 72 69 70 20 6f 66 66 20 61 20 74 72 61 69 6c 69  rip off a traili
c2990 6e 67 20 73 6c 61 73 68 65 73 20 6f 72 20 62 61  ng slashes or ba
c29a0 63 6b 73 6c 61 73 68 65 73 2c 20 6f 74 68 65 72  ckslashes, other
c29b0 77 69 73 65 20 77 65 20 77 6f 75 6c 64 20 67 65  wise we would ge
c29c0 74 20 2a 0a 20 20 20 2a 20 6d 75 6c 74 69 70 6c  t *.   * multipl
c29d0 65 20 28 62 61 63 6b 29 73 6c 61 73 68 65 73 20  e (back)slashes 
c29e0 77 68 69 63 68 20 63 61 75 73 65 73 20 44 6f 73  which causes Dos
c29f0 4f 70 65 6e 28 29 20 74 6f 20 66 61 69 6c 2e 20  Open() to fail. 
c2a00 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 0a 20               *. 
c2a10 20 20 2a 20 54 72 61 69 6c 69 6e 67 20 73 70 61    * Trailing spa
c2a20 63 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f  ces are not allo
c2a30 77 65 64 2c 20 65 69 74 68 65 72 2e 20 20 20 20  wed, either.    
c2a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2a50 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 6a 20 3d          */.  j =
c2a60 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
c2a70 28 7a 54 65 6d 70 50 61 74 68 29 3b 0a 20 20 77  (zTempPath);.  w
c2a80 68 69 6c 65 28 20 6a 20 3e 20 30 20 26 26 20 28  hile( j > 0 && (
c2a90 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20   zTempPath[j-1] 
c2aa0 3d 3d 20 27 5c 5c 27 20 7c 7c 20 7a 54 65 6d 70  == '\\' || zTemp
c2ab0 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 2f 27  Path[j-1] == '/'
c2ac0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
c2ad0 20 20 20 20 20 7c 7c 20 7a 54 65 6d 70 50 61 74       || zTempPat
c2ae0 68 5b 6a 2d 31 5d 20 3d 3d 20 27 20 27 20 29 20  h[j-1] == ' ' ) 
c2af0 29 7b 0a 20 20 20 20 6a 2d 2d 3b 0a 20 20 7d 0a  ){.    j--;.  }.
c2b00 20 20 7a 54 65 6d 70 50 61 74 68 5b 6a 5d 20 3d    zTempPath[j] =
c2b10 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 21 73 71   '\0';.  if( !sq
c2b20 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63  lite3_temp_direc
c2b30 74 6f 72 79 20 29 7b 0a 20 20 20 20 63 68 61 72  tory ){.    char
c2b40 20 2a 7a 54 65 6d 70 50 61 74 68 55 54 46 20 3d   *zTempPathUTF =
c2b50 20 63 6f 6e 76 65 72 74 43 70 50 61 74 68 54 6f   convertCpPathTo
c2b60 55 74 66 38 28 20 7a 54 65 6d 70 50 61 74 68 20  Utf8( zTempPath 
c2b70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  );.    sqlite3_s
c2b80 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 2d 33 30  nprintf( nBuf-30
c2b90 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20  , zBuf,.        
c2ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25                "%
c2bb0 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f  s\\"SQLITE_TEMP_
c2bc0 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 65  FILE_PREFIX, zTe
c2bd0 6d 70 50 61 74 68 55 54 46 20 29 3b 0a 20 20 20  mpPathUTF );.   
c2be0 20 66 72 65 65 28 20 7a 54 65 6d 70 50 61 74 68   free( zTempPath
c2bf0 55 54 46 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  UTF );.  }else{.
c2c00 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
c2c10 69 6e 74 66 28 20 6e 42 75 66 2d 33 30 2c 20 7a  intf( nBuf-30, z
c2c20 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Buf,.           
c2c30 20 20 20 20 20 20 20 20 20 20 20 22 25 73 5c 5c             "%s\\
c2c40 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c  "SQLITE_TEMP_FIL
c2c50 45 5f 50 52 45 46 49 58 2c 20 7a 54 65 6d 70 50  E_PREFIX, zTempP
c2c60 61 74 68 20 29 3b 0a 20 20 7d 0a 20 20 6a 20 3d  ath );.  }.  j =
c2c70 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
c2c80 28 20 7a 42 75 66 20 29 3b 0a 20 20 73 71 6c 69  ( zBuf );.  sqli
c2c90 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 20  te3_randomness( 
c2ca0 32 30 2c 20 26 7a 42 75 66 5b 6a 5d 20 29 3b 0a  20, &zBuf[j] );.
c2cb0 20 20 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20    for( i = 0; i 
c2cc0 3c 20 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b 20 29  < 20; i++, j++ )
c2cd0 7b 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20  {.    zBuf[j] = 
c2ce0 28 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28  (char)zChars[ ((
c2cf0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42  unsigned char)zB
c2d00 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a  uf[j])%(sizeof(z
c2d10 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 7d  Chars)-1) ];.  }
c2d20 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a  .  zBuf[j] = 0;.
c2d30 20 20 4f 53 54 52 41 43 45 32 28 20 22 54 45 4d    OSTRACE2( "TEM
c2d40 50 20 46 49 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e  P FILENAME: %s\n
c2d50 22 2c 20 7a 42 75 66 20 29 3b 0a 20 20 72 65 74  ", zBuf );.  ret
c2d60 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
c2d70 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20  .../*.** Turn a 
c2d80 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d  relative pathnam
c2d90 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61  e into a full pa
c2da0 74 68 6e 61 6d 65 2e 20 20 57 72 69 74 65 20 74  thname.  Write t
c2db0 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e  he full.** pathn
c2dc0 61 6d 65 20 69 6e 74 6f 20 7a 46 75 6c 6c 5b 5d  ame into zFull[]
c2dd0 2e 20 20 7a 46 75 6c 6c 5b 5d 20 77 69 6c 6c 20  .  zFull[] will 
c2de0 62 65 20 61 74 20 6c 65 61 73 74 20 70 56 66 73  be at least pVfs
c2df0 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 2a 2a 20  ->mxPathname.** 
c2e00 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a  bytes in size..*
c2e10 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  /.static int os2
c2e20 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20  FullPathname(.  
c2e30 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
c2e40 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  s,          /* P
c2e50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62  ointer to vfs ob
c2e60 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ject */.  const 
c2e70 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 2c  char *zRelative,
c2e80 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c        /* Possibl
c2e90 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70 75 74  y relative input
c2ea0 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74 20 6e   path */.  int n
c2eb0 46 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20  Full,           
c2ec0 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
c2ed0 66 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20  f output buffer 
c2ee0 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 68  in bytes */.  ch
c2ef0 61 72 20 2a 7a 46 75 6c 6c 20 20 20 20 20 20 20  ar *zFull       
c2f00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74            /* Out
c2f10 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 29 7b  put buffer */.){
c2f20 0a 20 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69  .  char *zRelati
c2f30 76 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74  veCp = convertUt
c2f40 66 38 50 61 74 68 54 6f 43 70 28 20 7a 52 65 6c  f8PathToCp( zRel
c2f50 61 74 69 76 65 20 29 3b 0a 20 20 63 68 61 72 20  ative );.  char 
c2f60 7a 46 75 6c 6c 43 70 5b 43 43 48 4d 41 58 50 41  zFullCp[CCHMAXPA
c2f70 54 48 5d 20 3d 20 22 5c 30 22 3b 0a 20 20 63 68  TH] = "\0";.  ch
c2f80 61 72 20 2a 7a 46 75 6c 6c 55 54 46 3b 0a 20 20  ar *zFullUTF;.  
c2f90 41 50 49 52 45 54 20 72 63 20 3d 20 44 6f 73 51  APIRET rc = DosQ
c2fa0 75 65 72 79 50 61 74 68 49 6e 66 6f 28 20 7a 52  ueryPathInfo( zR
c2fb0 65 6c 61 74 69 76 65 43 70 2c 20 46 49 4c 5f 51  elativeCp, FIL_Q
c2fc0 55 45 52 59 46 55 4c 4c 4e 41 4d 45 2c 20 7a 46  UERYFULLNAME, zF
c2fd0 75 6c 6c 43 70 2c 0a 20 20 20 20 20 20 20 20 20  ullCp,.         
c2fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2ff0 20 20 20 20 20 20 20 43 43 48 4d 41 58 50 41 54         CCHMAXPAT
c3000 48 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 52 65  H );.  free( zRe
c3010 6c 61 74 69 76 65 43 70 20 29 3b 0a 20 20 7a 46  lativeCp );.  zF
c3020 75 6c 6c 55 54 46 20 3d 20 63 6f 6e 76 65 72 74  ullUTF = convert
c3030 43 70 50 61 74 68 54 6f 55 74 66 38 28 20 7a 46  CpPathToUtf8( zF
c3040 75 6c 6c 43 70 20 29 3b 0a 20 20 73 71 6c 69 74  ullCp );.  sqlit
c3050 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 46 75  e3_snprintf( nFu
c3060 6c 6c 2c 20 7a 46 75 6c 6c 2c 20 7a 46 75 6c 6c  ll, zFull, zFull
c3070 55 54 46 20 29 3b 0a 20 20 66 72 65 65 28 20 7a  UTF );.  free( z
c3080 46 75 6c 6c 55 54 46 20 29 3b 0a 20 20 72 65 74  FullUTF );.  ret
c3090 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52  urn rc == NO_ERR
c30a0 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a  OR ? SQLITE_OK :
c30b0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d   SQLITE_IOERR;.}
c30c0 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20  .../*.** Open a 
c30d0 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
c30e0 69 6e 74 20 6f 73 32 4f 70 65 6e 28 0a 20 20 73  int os2Open(.  s
c30f0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
c3100 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
c3110 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 63 6f  Not used */.  co
c3120 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
c3130 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
c3140 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ame of the file 
c3150 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  */.  sqlite3_fil
c3160 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20  e *id,          
c3170 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
c3180 53 51 4c 69 74 65 20 66 69 6c 65 20 68 61 6e 64  SQLite file hand
c3190 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  le here */.  int
c31a0 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
c31b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70             /* Op
c31c0 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20 2a 2f  en mode flags */
c31d0 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67  .  int *pOutFlag
c31e0 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s               
c31f0 20 2f 2a 20 53 74 61 74 75 73 20 72 65 74 75 72   /* Status retur
c3200 6e 20 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a 20 20  n flags */.){.  
c3210 48 46 49 4c 45 20 68 3b 0a 20 20 55 4c 4f 4e 47  HFILE h;.  ULONG
c3220 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65   ulFileAttribute
c3230 20 3d 20 46 49 4c 45 5f 4e 4f 52 4d 41 4c 3b 0a   = FILE_NORMAL;.
c3240 20 20 55 4c 4f 4e 47 20 75 6c 4f 70 65 6e 46 6c    ULONG ulOpenFl
c3250 61 67 73 20 3d 20 30 3b 0a 20 20 55 4c 4f 4e 47  ags = 0;.  ULONG
c3260 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 3d 20 30 3b   ulOpenMode = 0;
c3270 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c  .  os2File *pFil
c3280 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64  e = (os2File*)id
c3290 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20  ;.  APIRET rc = 
c32a0 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e  NO_ERROR;.  ULON
c32b0 47 20 75 6c 41 63 74 69 6f 6e 3b 0a 20 20 63 68  G ulAction;.  ch
c32c0 61 72 20 2a 7a 4e 61 6d 65 43 70 3b 0a 20 20 63  ar *zNameCp;.  c
c32d0 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b 43 43 48  har zTmpname[CCH
c32e0 4d 41 58 50 41 54 48 2b 31 5d 3b 20 20 20 20 2f  MAXPATH+1];    /
c32f0 2a 20 42 75 66 66 65 72 20 74 6f 20 68 6f 6c 64  * Buffer to hold
c3300 20 6e 61 6d 65 20 6f 66 20 74 65 6d 70 20 66 69   name of temp fi
c3310 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74  le */..  /* If t
c3320 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
c3330 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  nt to this funct
c3340 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 65 6e  ion is NULL, gen
c3350 65 72 61 74 65 20 61 20 0a 20 20 2a 2a 20 74 65  erate a .  ** te
c3360 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d  mporary file nam
c3370 65 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f 0a 20  e to use .  */. 
c3380 20 69 66 28 20 21 7a 4e 61 6d 65 20 29 7b 0a 20   if( !zName ){. 
c3390 20 20 20 69 6e 74 20 72 63 20 3d 20 67 65 74 54     int rc = getT
c33a0 65 6d 70 6e 61 6d 65 28 43 43 48 4d 41 58 50 41  empname(CCHMAXPA
c33b0 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b  TH+1, zTmpname);
c33c0 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
c33d0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
c33e0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
c33f0 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 7a 54 6d  .    zName = zTm
c3400 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 0a 20 20 6d  pname;.  }...  m
c3410 65 6d 73 65 74 28 20 70 46 69 6c 65 2c 20 30 2c  emset( pFile, 0,
c3420 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c 65 29 20   sizeof(*pFile) 
c3430 29 3b 0a 0a 20 20 4f 53 54 52 41 43 45 32 28 20  );..  OSTRACE2( 
c3440 22 4f 50 45 4e 20 77 61 6e 74 20 25 64 5c 6e 22  "OPEN want %d\n"
c3450 2c 20 66 6c 61 67 73 20 29 3b 0a 0a 20 20 69 66  , flags );..  if
c3460 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ( flags & SQLITE
c3470 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20  _OPEN_READWRITE 
c3480 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64  ){.    ulOpenMod
c3490 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 43 45 53 53  e |= OPEN_ACCESS
c34a0 5f 52 45 41 44 57 52 49 54 45 3b 0a 20 20 20 20  _READWRITE;.    
c34b0 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20  OSTRACE1( "OPEN 
c34c0 72 65 61 64 2f 77 72 69 74 65 5c 6e 22 20 29 3b  read/write\n" );
c34d0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c  .  }else{.    ul
c34e0 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e  OpenMode |= OPEN
c34f0 5f 41 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59  _ACCESS_READONLY
c3500 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20  ;.    OSTRACE1( 
c3510 22 4f 50 45 4e 20 72 65 61 64 20 6f 6e 6c 79 5c  "OPEN read only\
c3520 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  n" );.  }..  if(
c3530 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f   flags & SQLITE_
c3540 4f 50 45 4e 5f 43 52 45 41 54 45 20 29 7b 0a 20  OPEN_CREATE ){. 
c3550 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 7c     ulOpenFlags |
c3560 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50  = OPEN_ACTION_OP
c3570 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c 20 4f  EN_IF_EXISTS | O
c3580 50 45 4e 5f 41 43 54 49 4f 4e 5f 43 52 45 41 54  PEN_ACTION_CREAT
c3590 45 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53  E_IF_NEW;.    OS
c35a0 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70  TRACE1( "OPEN op
c35b0 65 6e 20 6e 65 77 2f 63 72 65 61 74 65 5c 6e 22  en new/create\n"
c35c0 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   );.  }else{.   
c35d0 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 7c 3d 20   ulOpenFlags |= 
c35e0 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e  OPEN_ACTION_OPEN
c35f0 5f 49 46 5f 45 58 49 53 54 53 20 7c 20 4f 50 45  _IF_EXISTS | OPE
c3600 4e 5f 41 43 54 49 4f 4e 5f 46 41 49 4c 5f 49 46  N_ACTION_FAIL_IF
c3610 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 54 52 41 43  _NEW;.    OSTRAC
c3620 45 31 28 20 22 4f 50 45 4e 20 6f 70 65 6e 20 65  E1( "OPEN open e
c3630 78 69 73 74 69 6e 67 5c 6e 22 20 29 3b 0a 20 20  xisting\n" );.  
c3640 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26  }..  if( flags &
c3650 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49   SQLITE_OPEN_MAI
c3660 4e 5f 44 42 20 29 7b 0a 20 20 20 20 75 6c 4f 70  N_DB ){.    ulOp
c3670 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53  enMode |= OPEN_S
c3680 48 41 52 45 5f 44 45 4e 59 4e 4f 4e 45 3b 0a 20  HARE_DENYNONE;. 
c3690 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50     OSTRACE1( "OP
c36a0 45 4e 20 73 68 61 72 65 20 72 65 61 64 2f 77 72  EN share read/wr
c36b0 69 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73  ite\n" );.  }els
c36c0 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64  e{.    ulOpenMod
c36d0 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 41 52 45 5f  e |= OPEN_SHARE_
c36e0 44 45 4e 59 57 52 49 54 45 3b 0a 20 20 20 20 4f  DENYWRITE;.    O
c36f0 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 73  STRACE1( "OPEN s
c3700 68 61 72 65 20 72 65 61 64 20 6f 6e 6c 79 5c 6e  hare read only\n
c3710 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  " );.  }..  if( 
c3720 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
c3730 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53  PEN_DELETEONCLOS
c3740 45 20 29 7b 0a 20 20 20 20 63 68 61 72 20 70 61  E ){.    char pa
c3750 74 68 55 74 66 38 5b 43 43 48 4d 41 58 50 41 54  thUtf8[CCHMAXPAT
c3760 48 5d 3b 0a 23 69 66 64 65 66 20 4e 44 45 42 55  H];.#ifdef NDEBU
c3770 47 20 2f 2a 20 77 68 65 6e 20 64 65 62 75 67 67  G /* when debugg
c3780 69 6e 67 20 77 65 20 77 61 6e 74 20 74 6f 20 6d  ing we want to m
c3790 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 64  ake sure it is d
c37a0 65 6c 65 74 65 64 20 2a 2f 0a 20 20 20 20 75 6c  eleted */.    ul
c37b0 46 69 6c 65 41 74 74 72 69 62 75 74 65 20 3d 20  FileAttribute = 
c37c0 46 49 4c 45 5f 48 49 44 44 45 4e 3b 0a 23 65 6e  FILE_HIDDEN;.#en
c37d0 64 69 66 0a 20 20 20 20 6f 73 32 46 75 6c 6c 50  dif.    os2FullP
c37e0 61 74 68 6e 61 6d 65 28 20 70 56 66 73 2c 20 7a  athname( pVfs, z
c37f0 4e 61 6d 65 2c 20 43 43 48 4d 41 58 50 41 54 48  Name, CCHMAXPATH
c3800 2c 20 70 61 74 68 55 74 66 38 20 29 3b 0a 20 20  , pathUtf8 );.  
c3810 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44    pFile->pathToD
c3820 65 6c 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38  el = convertUtf8
c3830 50 61 74 68 54 6f 43 70 28 20 70 61 74 68 55 74  PathToCp( pathUt
c3840 66 38 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43  f8 );.    OSTRAC
c3850 45 31 28 20 22 4f 50 45 4e 20 68 69 64 64 65 6e  E1( "OPEN hidden
c3860 2f 64 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65  /delete on close
c3870 20 66 69 6c 65 20 61 74 74 72 69 62 75 74 65 73   file attributes
c3880 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  \n" );.  }else{.
c3890 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54      pFile->pathT
c38a0 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  oDel = NULL;.   
c38b0 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e   OSTRACE1( "OPEN
c38c0 20 6e 6f 72 6d 61 6c 20 66 69 6c 65 20 61 74 74   normal file att
c38d0 72 69 62 75 74 65 5c 6e 22 20 29 3b 0a 20 20 7d  ribute\n" );.  }
c38e0 0a 0a 20 20 2f 2a 20 61 6c 77 61 79 73 20 6f 70  ..  /* always op
c38f0 65 6e 20 69 6e 20 72 61 6e 64 6f 6d 20 61 63 63  en in random acc
c3900 65 73 73 20 6d 6f 64 65 20 66 6f 72 20 70 6f 73  ess mode for pos
c3910 73 69 62 6c 79 20 62 65 74 74 65 72 20 73 70 65  sibly better spe
c3920 65 64 20 2a 2f 0a 20 20 75 6c 4f 70 65 6e 4d 6f  ed */.  ulOpenMo
c3930 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53  de |= OPEN_FLAGS
c3940 5f 52 41 4e 44 4f 4d 3b 0a 20 20 75 6c 4f 70 65  _RANDOM;.  ulOpe
c3950 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c  nMode |= OPEN_FL
c3960 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f 45 52 52 4f  AGS_FAIL_ON_ERRO
c3970 52 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20  R;.  ulOpenMode 
c3980 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 4e 4f  |= OPEN_FLAGS_NO
c3990 49 4e 48 45 52 49 54 3b 0a 0a 20 20 7a 4e 61 6d  INHERIT;..  zNam
c39a0 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66  eCp = convertUtf
c39b0 38 50 61 74 68 54 6f 43 70 28 20 7a 4e 61 6d 65  8PathToCp( zName
c39c0 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 4f 70   );.  rc = DosOp
c39d0 65 6e 28 20 28 50 53 5a 29 7a 4e 61 6d 65 43 70  en( (PSZ)zNameCp
c39e0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
c39f0 20 20 26 68 2c 0a 20 20 20 20 20 20 20 20 20 20    &h,.          
c3a00 20 20 20 20 20 20 26 75 6c 41 63 74 69 6f 6e 2c        &ulAction,
c3a10 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
c3a20 20 30 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20   0L,.           
c3a30 20 20 20 20 20 75 6c 46 69 6c 65 41 74 74 72 69       ulFileAttri
c3a40 62 75 74 65 2c 0a 20 20 20 20 20 20 20 20 20 20  bute,.          
c3a50 20 20 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67        ulOpenFlag
c3a60 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  s,.             
c3a70 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 2c 0a 20     ulOpenMode,. 
c3a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
c3a90 50 45 41 4f 50 32 29 4e 55 4c 4c 20 29 3b 0a 20  PEAOP2)NULL );. 
c3aa0 20 66 72 65 65 28 20 7a 4e 61 6d 65 43 70 20 29   free( zNameCp )
c3ab0 3b 0a 20 20 69 66 28 20 72 63 20 21 3d 20 4e 4f  ;.  if( rc != NO
c3ac0 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 4f 53  _ERROR ){.    OS
c3ad0 54 52 41 43 45 37 28 20 22 4f 50 45 4e 20 49 6e  TRACE7( "OPEN In
c3ae0 76 61 6c 69 64 20 68 61 6e 64 6c 65 20 72 63 3d  valid handle rc=
c3af0 25 64 3a 20 7a 4e 61 6d 65 3d 25 73 2c 20 75 6c  %d: zName=%s, ul
c3b00 41 63 74 69 6f 6e 3d 25 23 6c 78 2c 20 75 6c 41  Action=%#lx, ulA
c3b10 74 74 72 3d 25 23 6c 78 2c 20 75 6c 46 6c 61 67  ttr=%#lx, ulFlag
c3b20 73 3d 25 23 6c 78 2c 20 75 6c 4d 6f 64 65 3d 25  s=%#lx, ulMode=%
c3b30 23 6c 78 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20  #lx\n",.        
c3b40 20 20 20 20 20 20 72 63 2c 20 7a 4e 61 6d 65 2c        rc, zName,
c3b50 20 75 6c 41 63 74 69 6f 6e 2c 20 75 6c 46 69 6c   ulAction, ulFil
c3b60 65 41 74 74 72 69 62 75 74 65 2c 20 75 6c 4f 70  eAttribute, ulOp
c3b70 65 6e 46 6c 61 67 73 2c 20 75 6c 4f 70 65 6e 4d  enFlags, ulOpenM
c3b80 6f 64 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70  ode );.    if( p
c3b90 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20  File->pathToDel 
c3ba0 29 0a 20 20 20 20 20 20 66 72 65 65 28 20 70 46  ).      free( pF
c3bb0 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29  ile->pathToDel )
c3bc0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74  ;.    pFile->pat
c3bd0 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20  hToDel = NULL;. 
c3be0 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53     if( flags & S
c3bf0 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
c3c00 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 4f 53  RITE ){.      OS
c3c10 54 52 41 43 45 32 28 20 22 4f 50 45 4e 20 25 64  TRACE2( "OPEN %d
c3c20 20 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65 5c   Invalid handle\
c3c30 6e 22 2c 20 28 28 66 6c 61 67 73 20 7c 20 53 51  n", ((flags | SQ
c3c40 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e  LITE_OPEN_READON
c3c50 4c 59 29 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50  LY) & ~SQLITE_OP
c3c60 45 4e 5f 52 45 41 44 57 52 49 54 45 29 20 29 3b  EN_READWRITE) );
c3c70 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 6f 73  .      return os
c3c80 32 4f 70 65 6e 28 20 70 56 66 73 2c 20 7a 4e 61  2Open( pVfs, zNa
c3c90 6d 65 2c 20 69 64 2c 0a 20 20 20 20 20 20 20 20  me, id,.        
c3ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 28                ((
c3cb0 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 5f 4f  flags | SQLITE_O
c3cc0 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20 26 20  PEN_READONLY) & 
c3cd0 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  ~SQLITE_OPEN_REA
c3ce0 44 57 52 49 54 45 29 2c 0a 20 20 20 20 20 20 20  DWRITE),.       
c3cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
c3d00 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20 20 20 20  OutFlags );.    
c3d10 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74  }else{.      ret
c3d20 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  urn SQLITE_CANTO
c3d30 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  PEN;.    }.  }..
c3d40 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67 73 20    if( pOutFlags 
c3d50 29 7b 0a 20 20 20 20 2a 70 4f 75 74 46 6c 61 67  ){.    *pOutFlag
c3d60 73 20 3d 20 66 6c 61 67 73 20 26 20 53 51 4c 49  s = flags & SQLI
c3d70 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
c3d80 45 20 3f 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  E ? SQLITE_OPEN_
c3d90 52 45 41 44 57 52 49 54 45 20 3a 20 53 51 4c 49  READWRITE : SQLI
c3da0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
c3db0 3b 0a 20 20 7d 0a 0a 20 20 70 46 69 6c 65 2d 3e  ;.  }..  pFile->
c3dc0 70 4d 65 74 68 6f 64 20 3d 20 26 6f 73 32 49 6f  pMethod = &os2Io
c3dd0 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69 6c 65 2d  Method;.  pFile-
c3de0 3e 68 20 3d 20 68 3b 0a 20 20 4f 70 65 6e 43 6f  >h = h;.  OpenCo
c3df0 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 4f 53 54  unter(+1);.  OST
c3e00 52 41 43 45 33 28 20 22 4f 50 45 4e 20 25 64 20  RACE3( "OPEN %d 
c3e10 70 4f 75 74 46 6c 61 67 73 3d 25 64 5c 6e 22 2c  pOutFlags=%d\n",
c3e20 20 70 46 69 6c 65 2d 3e 68 2c 20 70 4f 75 74 46   pFile->h, pOutF
c3e30 6c 61 67 73 20 29 3b 0a 20 20 72 65 74 75 72 6e  lags );.  return
c3e40 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
c3e50 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20  *.** Delete the 
c3e60 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2f 0a 73  named file..*/.s
c3e70 74 61 74 69 63 20 69 6e 74 20 6f 73 32 44 65 6c  tatic int os2Del
c3e80 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  ete(.  sqlite3_v
c3e90 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20  fs *pVfs,       
c3ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c3eb0 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32   Not used on os2
c3ec0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
c3ed0 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20   *zFilename,    
c3ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c3ef0 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20  Name of file to 
c3f00 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20  delete */.  int 
c3f10 73 79 6e 63 44 69 72 20 20 20 20 20 20 20 20 20  syncDir         
c3f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3f30 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f     /* Not used o
c3f40 6e 20 6f 73 32 20 2a 2f 0a 29 7b 0a 20 20 41 50  n os2 */.){.  AP
c3f50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52  IRET rc = NO_ERR
c3f60 4f 52 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c  OR;.  char *zFil
c3f70 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72  enameCp = conver
c3f80 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a  tUtf8PathToCp( z
c3f90 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 20 53 69  Filename );.  Si
c3fa0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72  mulateIOError( r
c3fb0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
c3fc0 52 52 5f 44 45 4c 45 54 45 20 29 3b 0a 20 20 72  RR_DELETE );.  r
c3fd0 63 20 3d 20 44 6f 73 44 65 6c 65 74 65 28 20 28  c = DosDelete( (
c3fe0 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 43 70 20  PSZ)zFilenameCp 
c3ff0 29 3b 0a 20 20 66 72 65 65 28 20 7a 46 69 6c 65  );.  free( zFile
c4000 6e 61 6d 65 43 70 20 29 3b 0a 20 20 4f 53 54 52  nameCp );.  OSTR
c4010 41 43 45 32 28 20 22 44 45 4c 45 54 45 20 5c 22  ACE2( "DELETE \"
c4020 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61  %s\"\n", zFilena
c4030 6d 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  me );.  return r
c4040 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20  c == NO_ERROR ? 
c4050 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49  SQLITE_OK : SQLI
c4060 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b  TE_IOERR_DELETE;
c4070 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  .}../*.** Check 
c4080 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 61 6e  the existance an
c4090 64 20 73 74 61 74 75 73 20 6f 66 20 61 20 66 69  d status of a fi
c40a0 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
c40b0 74 20 6f 73 32 41 63 63 65 73 73 28 0a 20 20 73  t os2Access(.  s
c40c0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
c40d0 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20  ,        /* Not 
c40e0 75 73 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 20  used on os2 */. 
c40f0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
c4100 6c 65 6e 61 6d 65 2c 20 20 20 20 2f 2a 20 4e 61  lename,    /* Na
c4110 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 63 68  me of file to ch
c4120 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61  eck */.  int fla
c4130 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gs,             
c4140 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 74 65     /* Type of te
c4150 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e 20 74 68  st to make on th
c4160 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74  is file */.  int
c4170 20 2a 70 4f 75 74 20 20 20 20 20 20 20 20 20 20   *pOut          
c4180 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
c4190 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a  results here */.
c41a0 29 7b 0a 20 20 46 49 4c 45 53 54 41 54 55 53 33  ){.  FILESTATUS3
c41b0 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f   fsts3ConfigInfo
c41c0 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20  ;.  APIRET rc = 
c41d0 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 68 61 72  NO_ERROR;.  char
c41e0 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70 20 3d 20   *zFilenameCp = 
c41f0 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 54  convertUtf8PathT
c4200 6f 43 70 28 20 7a 46 69 6c 65 6e 61 6d 65 20 29  oCp( zFilename )
c4210 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 20 26 66 73  ;..  memset( &fs
c4220 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2c 20 30  ts3ConfigInfo, 0
c4230 2c 20 73 69 7a 65 6f 66 28 66 73 74 73 33 43 6f  , sizeof(fsts3Co
c4240 6e 66 69 67 49 6e 66 6f 29 20 29 3b 0a 20 20 72  nfigInfo) );.  r
c4250 63 20 3d 20 44 6f 73 51 75 65 72 79 50 61 74 68  c = DosQueryPath
c4260 49 6e 66 6f 28 20 28 50 53 5a 29 7a 46 69 6c 65  Info( (PSZ)zFile
c4270 6e 61 6d 65 43 70 2c 20 46 49 4c 5f 53 54 41 4e  nameCp, FIL_STAN
c4280 44 41 52 44 2c 0a 20 20 20 20 20 20 20 20 20 20  DARD,.          
c4290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
c42a0 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2c  fsts3ConfigInfo,
c42b0 20 73 69 7a 65 6f 66 28 46 49 4c 45 53 54 41 54   sizeof(FILESTAT
c42c0 55 53 33 29 20 29 3b 0a 20 20 66 72 65 65 28 20  US3) );.  free( 
c42d0 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20  zFilenameCp );. 
c42e0 20 4f 53 54 52 41 43 45 34 28 20 22 41 43 43 45   OSTRACE4( "ACCE
c42f0 53 53 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e  SS fsts3ConfigIn
c4300 66 6f 2e 61 74 74 72 46 69 6c 65 3d 25 64 20 66  fo.attrFile=%d f
c4310 6c 61 67 73 3d 25 64 20 72 63 3d 25 64 5c 6e 22  lags=%d rc=%d\n"
c4320 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 73  ,.            fs
c4330 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61 74  ts3ConfigInfo.at
c4340 74 72 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 72  trFile, flags, r
c4350 63 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20 66  c );.  switch( f
c4360 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73 65  lags ){.    case
c4370 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52   SQLITE_ACCESS_R
c4380 45 41 44 3a 0a 20 20 20 20 63 61 73 65 20 53 51  EAD:.    case SQ
c4390 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53  LITE_ACCESS_EXIS
c43a0 54 53 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28  TS:.      rc = (
c43b0 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b  rc == NO_ERROR);
c43c0 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28  .      OSTRACE3(
c43d0 20 22 41 43 43 45 53 53 20 25 73 20 61 63 63 65   "ACCESS %s acce
c43e0 73 73 20 6f 66 20 72 65 61 64 20 61 6e 64 20 65  ss of read and e
c43f0 78 69 73 74 73 20 20 72 63 3d 25 64 5c 6e 22 2c  xists  rc=%d\n",
c4400 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63 20 29   zFilename, rc )
c4410 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
c4420 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41     case SQLITE_A
c4430 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a  CCESS_READWRITE:
c4440 0a 20 20 20 20 20 20 72 63 20 3d 20 28 72 63 20  .      rc = (rc 
c4450 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 26 26 20  == NO_ERROR) && 
c4460 28 20 28 66 73 74 73 33 43 6f 6e 66 69 67 49 6e  ( (fsts3ConfigIn
c4470 66 6f 2e 61 74 74 72 46 69 6c 65 20 26 20 46 49  fo.attrFile & FI
c4480 4c 45 5f 52 45 41 44 4f 4e 4c 59 29 20 3d 3d 20  LE_READONLY) == 
c4490 30 20 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41  0 );.      OSTRA
c44a0 43 45 33 28 20 22 41 43 43 45 53 53 20 25 73 20  CE3( "ACCESS %s 
c44b0 61 63 63 65 73 73 20 6f 66 20 72 65 61 64 2f 77  access of read/w
c44c0 72 69 74 65 20 20 72 63 3d 25 64 5c 6e 22 2c 20  rite  rc=%d\n", 
c44d0 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63 20 29 3b  zFilename, rc );
c44e0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
c44f0 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20    default:.     
c4500 20 61 73 73 65 72 74 28 20 21 22 49 6e 76 61 6c   assert( !"Inval
c4510 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e  id flags argumen
c4520 74 22 20 29 3b 0a 20 20 7d 0a 20 20 2a 70 4f 75  t" );.  }.  *pOu
c4530 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 75 72 6e  t = rc;.  return
c4540 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a   SQLITE_OK;.}...
c4550 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
c4560 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
c4570 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61  ON./*.** Interfa
c4580 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20  ces for opening 
c4590 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  a shared library
c45a0 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20  , finding entry 
c45b0 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e  points.** within
c45c0 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
c45d0 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67  ary, and closing
c45e0 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
c45f0 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e  ary..*/./*.** In
c4600 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65  terfaces for ope
c4610 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69  ning a shared li
c4620 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65  brary, finding e
c4630 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77  ntry points.** w
c4640 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64  ithin the shared
c4650 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c   library, and cl
c4660 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64  osing the shared
c4670 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61   library..*/.sta
c4680 74 69 63 20 76 6f 69 64 20 2a 6f 73 32 44 6c 4f  tic void *os2DlO
c4690 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20  pen(sqlite3_vfs 
c46a0 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61  *pVfs, const cha
c46b0 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20  r *zFilename){. 
c46c0 20 55 43 48 41 52 20 6c 6f 61 64 45 72 72 5b 32   UCHAR loadErr[2
c46d0 35 36 5d 3b 0a 20 20 48 4d 4f 44 55 4c 45 20 68  56];.  HMODULE h
c46e0 6d 6f 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63  mod;.  APIRET rc
c46f0 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  ;.  char *zFilen
c4700 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55  ameCp = convertU
c4710 74 66 38 50 61 74 68 54 6f 43 70 28 7a 46 69 6c  tf8PathToCp(zFil
c4720 65 6e 61 6d 65 29 3b 0a 20 20 72 63 20 3d 20 44  ename);.  rc = D
c4730 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 28 28 50 53  osLoadModule((PS
c4740 5a 29 6c 6f 61 64 45 72 72 2c 20 73 69 7a 65 6f  Z)loadErr, sizeo
c4750 66 28 6c 6f 61 64 45 72 72 29 2c 20 7a 46 69 6c  f(loadErr), zFil
c4760 65 6e 61 6d 65 43 70 2c 20 26 68 6d 6f 64 29 3b  enameCp, &hmod);
c4770 0a 20 20 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d  .  free(zFilenam
c4780 65 43 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  eCp);.  return r
c4790 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20  c != NO_ERROR ? 
c47a0 30 20 3a 20 28 76 6f 69 64 2a 29 68 6d 6f 64 3b  0 : (void*)hmod;
c47b0 0a 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e 6f 2d 6f 70  .}./*.** A no-op
c47c0 20 73 69 6e 63 65 20 74 68 65 20 65 72 72 6f 72   since the error
c47d0 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
c47e0 64 20 6f 6e 20 74 68 65 20 44 6f 73 4c 6f 61 64  d on the DosLoad
c47f0 4d 6f 64 75 6c 65 20 63 61 6c 6c 2e 0a 2a 2a 20  Module call..** 
c4800 6f 73 32 44 6c 6f 70 65 6e 20 72 65 74 75 72 6e  os2Dlopen return
c4810 73 20 7a 65 72 6f 20 69 66 20 44 6f 73 4c 6f 61  s zero if DosLoa
c4820 64 4d 6f 64 75 6c 65 20 69 73 20 6e 6f 74 20 73  dModule is not s
c4830 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2f 0a 73 74  uccessful..*/.st
c4840 61 74 69 63 20 76 6f 69 64 20 6f 73 32 44 6c 45  atic void os2DlE
c4850 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73  rror(sqlite3_vfs
c4860 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66   *pVfs, int nBuf
c4870 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29  , char *zBufOut)
c4880 7b 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a 7d 0a  {./* no-op */.}.
c4890 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6f 73 32  static void *os2
c48a0 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66  DlSym(sqlite3_vf
c48b0 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70  s *pVfs, void *p
c48c0 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68  Handle, const ch
c48d0 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a 20 20  ar *zSymbol){.  
c48e0 50 46 4e 20 70 66 6e 3b 0a 20 20 41 50 49 52 45  PFN pfn;.  APIRE
c48f0 54 20 72 63 3b 0a 20 20 72 63 20 3d 20 44 6f 73  T rc;.  rc = Dos
c4900 51 75 65 72 79 50 72 6f 63 41 64 64 72 28 28 48  QueryProcAddr((H
c4910 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20  MODULE)pHandle, 
c4920 30 4c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 26 70 66  0L, zSymbol, &pf
c4930 6e 29 3b 0a 20 20 69 66 28 20 72 63 20 21 3d 20  n);.  if( rc != 
c4940 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
c4950 2f 2a 20 69 66 20 74 68 65 20 73 79 6d 62 6f 6c  /* if the symbol
c4960 20 69 74 73 65 6c 66 20 77 61 73 20 6e 6f 74 20   itself was not 
c4970 66 6f 75 6e 64 2c 20 73 65 61 72 63 68 20 61 67  found, search ag
c4980 61 69 6e 20 66 6f 72 20 74 68 65 20 73 61 6d 65  ain for the same
c4990 0a 20 20 20 20 20 2a 20 73 79 6d 62 6f 6c 20 77  .     * symbol w
c49a0 69 74 68 20 61 6e 20 65 78 74 72 61 20 75 6e 64  ith an extra und
c49b0 65 72 73 63 6f 72 65 2c 20 74 68 61 74 20 6d 69  erscore, that mi
c49c0 67 68 74 20 62 65 20 6e 65 65 64 65 64 20 64 65  ght be needed de
c49d0 70 65 6e 64 69 6e 67 0a 20 20 20 20 20 2a 20 6f  pending.     * o
c49e0 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 63 6f  n the calling co
c49f0 6e 76 65 6e 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  nvention */.    
c4a00 63 68 61 72 20 5f 7a 53 79 6d 62 6f 6c 5b 32 35  char _zSymbol[25
c4a10 36 5d 20 3d 20 22 5f 22 3b 0a 20 20 20 20 73 74  6] = "_";.    st
c4a20 72 6e 63 61 74 28 5f 7a 53 79 6d 62 6f 6c 2c 20  rncat(_zSymbol, 
c4a30 7a 53 79 6d 62 6f 6c 2c 20 32 35 35 29 3b 0a 20  zSymbol, 255);. 
c4a40 20 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79     rc = DosQuery
c4a50 50 72 6f 63 41 64 64 72 28 28 48 4d 4f 44 55 4c  ProcAddr((HMODUL
c4a60 45 29 70 48 61 6e 64 6c 65 2c 20 30 4c 2c 20 5f  E)pHandle, 0L, _
c4a70 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 6e 29 3b 0a  zSymbol, &pfn);.
c4a80 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20    }.  return rc 
c4a90 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 30 20  != NO_ERROR ? 0 
c4aa0 3a 20 28 76 6f 69 64 2a 29 70 66 6e 3b 0a 7d 0a  : (void*)pfn;.}.
c4ab0 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 44  static void os2D
c4ac0 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76  lClose(sqlite3_v
c4ad0 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a  fs *pVfs, void *
c4ae0 70 48 61 6e 64 6c 65 29 7b 0a 20 20 44 6f 73 46  pHandle){.  DosF
c4af0 72 65 65 4d 6f 64 75 6c 65 28 28 48 4d 4f 44 55  reeModule((HMODU
c4b00 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23  LE)pHandle);.}.#
c4b10 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 54  else /* if SQLIT
c4b20 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
c4b30 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 64  NSION is defined
c4b40 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 6f  : */.  #define o
c4b50 73 32 44 6c 4f 70 65 6e 20 30 0a 20 20 23 64 65  s2DlOpen 0.  #de
c4b60 66 69 6e 65 20 6f 73 32 44 6c 45 72 72 6f 72 20  fine os2DlError 
c4b70 30 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 32 44  0.  #define os2D
c4b80 6c 53 79 6d 20 30 0a 20 20 23 64 65 66 69 6e 65  lSym 0.  #define
c4b90 20 6f 73 32 44 6c 43 6c 6f 73 65 20 30 0a 23 65   os2DlClose 0.#e
c4ba0 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69  ndif.../*.** Wri
c4bb0 74 65 20 75 70 20 74 6f 20 6e 42 75 66 20 62 79  te up to nBuf by
c4bc0 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73  tes of randomnes
c4bd0 73 20 69 6e 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a  s into zBuf..*/.
c4be0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 52 61  static int os2Ra
c4bf0 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33  ndomness(sqlite3
c4c00 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20  _vfs *pVfs, int 
c4c10 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66  nBuf, char *zBuf
c4c20 20 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b   ){.  int n = 0;
c4c30 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
c4c40 49 54 45 5f 54 45 53 54 29 0a 20 20 6e 20 3d 20  ITE_TEST).  n = 
c4c50 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 65 74 28 7a  nBuf;.  memset(z
c4c60 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b 0a 23  Buf, 0, nBuf);.#
c4c70 65 6c 73 65 0a 20 20 69 6e 74 20 73 69 7a 65 6f  else.  int sizeo
c4c80 66 55 4c 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28  fULong = sizeof(
c4c90 55 4c 4f 4e 47 29 3b 0a 20 20 69 66 28 20 28 69  ULONG);.  if( (i
c4ca0 6e 74 29 73 69 7a 65 6f 66 28 44 41 54 45 54 49  nt)sizeof(DATETI
c4cb0 4d 45 29 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20  ME) <= nBuf - n 
c4cc0 29 7b 0a 20 20 20 20 44 41 54 45 54 49 4d 45 20  ){.    DATETIME 
c4cd0 78 3b 0a 20 20 20 20 44 6f 73 47 65 74 44 61 74  x;.    DosGetDat
c4ce0 65 54 69 6d 65 28 26 78 29 3b 0a 20 20 20 20 6d  eTime(&x);.    m
c4cf0 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20  emcpy(&zBuf[n], 
c4d00 26 78 2c 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a  &x, sizeof(x));.
c4d10 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28      n += sizeof(
c4d20 78 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73  x);.  }..  if( s
c4d30 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42  izeofULong <= nB
c4d40 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 50 50  uf - n ){.    PP
c4d50 49 42 20 70 70 69 62 3b 0a 20 20 20 20 44 6f 73  IB ppib;.    Dos
c4d60 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 4e 55  GetInfoBlocks(NU
c4d70 4c 4c 2c 20 26 70 70 69 62 29 3b 0a 20 20 20 20  LL, &ppib);.    
c4d80 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c  memcpy(&zBuf[n],
c4d90 20 26 70 70 69 62 2d 3e 70 69 62 5f 75 6c 70 69   &ppib->pib_ulpi
c4da0 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b  d, sizeofULong);
c4db0 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66  .    n += sizeof
c4dc0 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20 69 66  ULong;.  }..  if
c4dd0 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d  ( sizeofULong <=
c4de0 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20   nBuf - n ){.   
c4df0 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 20 20   PTIB ptib;.    
c4e00 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73  DosGetInfoBlocks
c4e10 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20  (&ptib, NULL);. 
c4e20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b     memcpy(&zBuf[
c4e30 6e 5d 2c 20 26 70 74 69 62 2d 3e 74 69 62 5f 70  n], &ptib->tib_p
c4e40 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74 69 64  tib2->tib2_ultid
c4e50 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a  , sizeofULong);.
c4e60 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55      n += sizeofU
c4e70 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Long;.  }..  /* 
c4e80 69 66 20 77 65 20 73 74 69 6c 6c 20 68 61 76 65  if we still have
c4e90 6e 27 74 20 66 69 6c 6c 65 64 20 74 68 65 20 62  n't filled the b
c4ea0 75 66 66 65 72 20 79 65 74 20 74 68 65 20 66 6f  uffer yet the fo
c4eb0 6c 6c 6f 77 69 6e 67 20 77 69 6c 6c 20 2a 2f 0a  llowing will */.
c4ec0 20 20 2f 2a 20 67 72 61 62 20 65 76 65 72 79 74    /* grab everyt
c4ed0 68 69 6e 67 20 6f 6e 63 65 20 69 6e 73 74 65 61  hing once instea
c4ee0 64 20 6f 66 20 6d 61 6b 69 6e 67 20 73 65 76 65  d of making seve
c4ef0 72 61 6c 20 63 61 6c 6c 73 20 66 6f 72 20 61 20  ral calls for a 
c4f00 73 69 6e 67 6c 65 20 69 74 65 6d 20 2a 2f 0a 20  single item */. 
c4f10 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67   if( sizeofULong
c4f20 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a   <= nBuf - n ){.
c4f30 20 20 20 20 55 4c 4f 4e 47 20 75 6c 53 79 73 49      ULONG ulSysI
c4f40 6e 66 6f 5b 51 53 56 5f 4d 41 58 5d 3b 0a 20 20  nfo[QSV_MAX];.  
c4f50 20 20 44 6f 73 51 75 65 72 79 53 79 73 49 6e 66    DosQuerySysInf
c4f60 6f 28 31 4c 2c 20 51 53 56 5f 4d 41 58 2c 20 75  o(1L, QSV_MAX, u
c4f70 6c 53 79 73 49 6e 66 6f 2c 20 73 69 7a 65 6f 66  lSysInfo, sizeof
c4f80 55 4c 6f 6e 67 20 2a 20 51 53 56 5f 4d 41 58 29  ULong * QSV_MAX)
c4f90 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a  ;..    memcpy(&z
c4fa0 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e  Buf[n], &ulSysIn
c4fb0 66 6f 5b 51 53 56 5f 4d 53 5f 43 4f 55 4e 54 20  fo[QSV_MS_COUNT 
c4fc0 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e  - 1], sizeofULon
c4fd0 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a  g);.    n += siz
c4fe0 65 6f 66 55 4c 6f 6e 67 3b 0a 0a 20 20 20 20 69  eofULong;..    i
c4ff0 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c  f( sizeofULong <
c5000 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20  = nBuf - n ){.  
c5010 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66      memcpy(&zBuf
c5020 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b  [n], &ulSysInfo[
c5030 51 53 56 5f 54 49 4d 45 52 5f 49 4e 54 45 52 56  QSV_TIMER_INTERV
c5040 41 4c 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55  AL - 1], sizeofU
c5050 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b  Long);.      n +
c5060 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20  = sizeofULong;. 
c5070 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a     }.    if( siz
c5080 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66  eofULong <= nBuf
c5090 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65   - n ){.      me
c50a0 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26  mcpy(&zBuf[n], &
c50b0 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54 49  ulSysInfo[QSV_TI
c50c0 4d 45 5f 4c 4f 57 20 2d 20 31 5d 2c 20 73 69 7a  ME_LOW - 1], siz
c50d0 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20  eofULong);.     
c50e0 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e   n += sizeofULon
c50f0 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  g;.    }.    if(
c5100 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20   sizeofULong <= 
c5110 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20  nBuf - n ){.    
c5120 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e    memcpy(&zBuf[n
c5130 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53  ], &ulSysInfo[QS
c5140 56 5f 54 49 4d 45 5f 48 49 47 48 20 2d 20 31 5d  V_TIME_HIGH - 1]
c5150 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a  , sizeofULong);.
c5160 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f        n += sizeo
c5170 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20  fULong;.    }.  
c5180 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e    if( sizeofULon
c5190 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b  g <= nBuf - n ){
c51a0 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a  .      memcpy(&z
c51b0 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e  Buf[n], &ulSysIn
c51c0 66 6f 5b 51 53 56 5f 54 4f 54 41 56 41 49 4c 4d  fo[QSV_TOTAVAILM
c51d0 45 4d 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55  EM - 1], sizeofU
c51e0 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b  Long);.      n +
c51f0 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20  = sizeofULong;. 
c5200 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
c5210 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a  .  return n;.}..
c5220 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20  /*.** Sleep for 
c5230 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20  a little while. 
c5240 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75   Return the amou
c5250 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74  nt of time slept
c5260 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e  ..** The argumen
c5270 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  t is the number 
c5280 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20  of microseconds 
c5290 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65 65 70  we want to sleep
c52a0 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20  ..** The return 
c52b0 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d  value is the num
c52c0 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f  ber of microseco
c52d0 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74  nds of sleep act
c52e0 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74  ually.** request
c52f0 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65  ed from the unde
c5300 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67  rlying operating
c5310 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65   system, a numbe
c5320 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74  r which.** might
c5330 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e   be greater than
c5340 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
c5350 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e   argument, but n
c5360 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20  ot less.** than 
c5370 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  the argument..*/
c5380 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 53  .static int os2S
c5390 6c 65 65 70 28 20 73 71 6c 69 74 65 33 5f 76 66  leep( sqlite3_vf
c53a0 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63  s *pVfs, int mic
c53b0 72 6f 73 65 63 20 29 7b 0a 20 20 44 6f 73 53 6c  rosec ){.  DosSl
c53c0 65 65 70 28 20 28 6d 69 63 72 6f 73 65 63 2f 31  eep( (microsec/1
c53d0 30 30 30 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  000) );.  return
c53e0 20 6d 69 63 72 6f 73 65 63 3b 0a 7d 0a 0a 2f 2a   microsec;.}../*
c53f0 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
c5400 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73  g variable, if s
c5410 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f  et to a non-zero
c5420 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20   value, becomes 
c5430 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65  the result.** re
c5440 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  turned from sqli
c5450 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65  te3OsCurrentTime
c5460 28 29 2e 20 20 54 68 69 73 20 69 73 20 75 73 65  ().  This is use
c5470 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a  d for testing..*
c5480 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
c5490 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20  TEST.SQLITE_API 
c54a0 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72  int sqlite3_curr
c54b0 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65  ent_time = 0;.#e
c54c0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64  ndif../*.** Find
c54d0 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d   the current tim
c54e0 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20  e (in Universal 
c54f0 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65  Coordinated Time
c5500 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a  ).  Write the.**
c5510 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e   current time an
c5520 64 20 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69  d date as a Juli
c5530 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e  an Day number in
c5540 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a  to *prNow and.**
c5550 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75   return 0.  Retu
c5560 72 6e 20 31 20 69 66 20 74 68 65 20 74 69 6d 65  rn 1 if the time
c5570 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74   and date cannot
c5580 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e   be found..*/.in
c5590 74 20 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65  t os2CurrentTime
c55a0 28 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  ( sqlite3_vfs *p
c55b0 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 72 4e  Vfs, double *prN
c55c0 6f 77 20 29 7b 0a 20 20 64 6f 75 62 6c 65 20 6e  ow ){.  double n
c55d0 6f 77 3b 0a 20 20 53 48 4f 52 54 20 6d 69 6e 75  ow;.  SHORT minu
c55e0 74 65 3b 20 2f 2a 20 6e 65 65 64 73 20 74 6f 20  te; /* needs to 
c55f0 62 65 20 61 62 6c 65 20 74 6f 20 63 6f 70 65 20  be able to cope 
c5600 77 69 74 68 20 6e 65 67 61 74 69 76 65 20 74 69  with negative ti
c5610 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 2a 2f  mezone offset */
c5620 0a 20 20 55 53 48 4f 52 54 20 73 65 63 6f 6e 64  .  USHORT second
c5630 2c 20 68 6f 75 72 2c 0a 20 20 20 20 20 20 20 20  , hour,.        
c5640 20 64 61 79 2c 20 6d 6f 6e 74 68 2c 20 79 65 61   day, month, yea
c5650 72 3b 0a 20 20 44 41 54 45 54 49 4d 45 20 64 74  r;.  DATETIME dt
c5660 3b 0a 20 20 44 6f 73 47 65 74 44 61 74 65 54 69  ;.  DosGetDateTi
c5670 6d 65 28 20 26 64 74 20 29 3b 0a 20 20 73 65 63  me( &dt );.  sec
c5680 6f 6e 64 20 3d 20 28 55 53 48 4f 52 54 29 64 74  ond = (USHORT)dt
c5690 2e 73 65 63 6f 6e 64 73 3b 0a 20 20 6d 69 6e 75  .seconds;.  minu
c56a0 74 65 20 3d 20 28 53 48 4f 52 54 29 64 74 2e 6d  te = (SHORT)dt.m
c56b0 69 6e 75 74 65 73 20 2b 20 64 74 2e 74 69 6d 65  inutes + dt.time
c56c0 7a 6f 6e 65 3b 0a 20 20 68 6f 75 72 20 3d 20 28  zone;.  hour = (
c56d0 55 53 48 4f 52 54 29 64 74 2e 68 6f 75 72 73 3b  USHORT)dt.hours;
c56e0 0a 20 20 64 61 79 20 3d 20 28 55 53 48 4f 52 54  .  day = (USHORT
c56f0 29 64 74 2e 64 61 79 3b 0a 20 20 6d 6f 6e 74 68  )dt.day;.  month
c5700 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 6d 6f   = (USHORT)dt.mo
c5710 6e 74 68 3b 0a 20 20 79 65 61 72 20 3d 20 28 55  nth;.  year = (U
c5720 53 48 4f 52 54 29 64 74 2e 79 65 61 72 3b 0a 0a  SHORT)dt.year;..
c5730 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 69 6f 6e    /* Calculation
c5740 73 20 66 72 6f 6d 20 68 74 74 70 3a 2f 2f 77 77  s from http://ww
c5750 77 2e 61 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63  w.astro.keele.ac
c5760 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f  .uk/~rno/Astrono
c5770 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0a 20 20 20 20  my/hjd.html.    
c5780 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61 73 74 72   http://www.astr
c5790 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b 2f 7e 72  o.keele.ac.uk/~r
c57a0 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f 68 6a 64  no/Astronomy/hjd
c57b0 2d 30 2e 31 2e 63 20 2a 2f 0a 20 20 2f 2a 20 43  -0.1.c */.  /* C
c57c0 61 6c 63 75 6c 61 74 65 20 74 68 65 20 4a 75 6c  alculate the Jul
c57d0 69 61 6e 20 64 61 79 73 20 2a 2f 0a 20 20 6e 6f  ian days */.  no
c57e0 77 20 3d 20 64 61 79 20 2d 20 33 32 30 37 36 20  w = day - 32076 
c57f0 2b 0a 20 20 20 20 31 34 36 31 2a 28 79 65 61 72  +.    1461*(year
c5800 20 2b 20 34 38 30 30 20 2b 20 28 6d 6f 6e 74 68   + 4800 + (month
c5810 20 2d 20 31 34 29 2f 31 32 29 2f 34 20 2b 0a 20   - 14)/12)/4 +. 
c5820 20 20 20 33 36 37 2a 28 6d 6f 6e 74 68 20 2d 20     367*(month - 
c5830 32 20 2d 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29  2 - (month - 14)
c5840 2f 31 32 2a 31 32 29 2f 31 32 20 2d 0a 20 20 20  /12*12)/12 -.   
c5850 20 33 2a 28 28 79 65 61 72 20 2b 20 34 39 30 30   3*((year + 4900
c5860 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2f   + (month - 14)/
c5870 31 32 29 2f 31 30 30 29 2f 34 3b 0a 0a 20 20 2f  12)/100)/4;..  /
c5880 2a 20 41 64 64 20 74 68 65 20 66 72 61 63 74 69  * Add the fracti
c5890 6f 6e 61 6c 20 68 6f 75 72 73 2c 20 6d 69 6e 73  onal hours, mins
c58a0 20 61 6e 64 20 73 65 63 6f 6e 64 73 20 2a 2f 0a   and seconds */.
c58b0 20 20 6e 6f 77 20 2b 3d 20 28 68 6f 75 72 20 2b    now += (hour +
c58c0 20 31 32 2e 30 29 2f 32 34 2e 30 3b 0a 20 20 6e   12.0)/24.0;.  n
c58d0 6f 77 20 2b 3d 20 6d 69 6e 75 74 65 2f 31 34 34  ow += minute/144
c58e0 30 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d 20 73 65  0.0;.  now += se
c58f0 63 6f 6e 64 2f 38 36 34 30 30 2e 30 3b 0a 20 20  cond/86400.0;.  
c5900 2a 70 72 4e 6f 77 20 3d 20 6e 6f 77 3b 0a 23 69  *prNow = now;.#i
c5910 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
c5920 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63  .  if( sqlite3_c
c5930 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20  urrent_time ){. 
c5940 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69     *prNow = sqli
c5950 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65  te3_current_time
c5960 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35  /86400.0 + 24405
c5970 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  87.5;.  }.#endif
c5980 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
c5990 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 47 65  static int os2Ge
c59a0 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74  tLastError(sqlit
c59b0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
c59c0 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42  t nBuf, char *zB
c59d0 75 66 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b  uf){.  return 0;
c59e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  .}../*.** Initia
c59f0 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69  lize and deiniti
c5a00 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74  alize the operat
c5a10 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72  ing system inter
c5a20 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  face..*/.SQLITE_
c5a30 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
c5a40 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20  os_init(void){. 
c5a50 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f   static sqlite3_
c5a60 76 66 73 20 6f 73 32 56 66 73 20 3d 20 7b 0a 20  vfs os2Vfs = {. 
c5a70 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20     1,           
c5a80 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
c5a90 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28  n */.    sizeof(
c5aa0 6f 73 32 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73  os2File),   /* s
c5ab0 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 43  zOsFile */.    C
c5ac0 43 48 4d 41 58 50 41 54 48 2c 20 20 20 20 20 20  CHMAXPATH,      
c5ad0 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20    /* mxPathname 
c5ae0 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20  */.    0,       
c5af0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65            /* pNe
c5b00 78 74 20 2a 2f 0a 20 20 20 20 22 6f 73 32 22 2c  xt */.    "os2",
c5b10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c5b20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20  zName */.    0, 
c5b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c5b40 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 0a  /* pAppData */..
c5b50 20 20 20 20 6f 73 32 4f 70 65 6e 2c 20 20 20 20      os2Open,    
c5b60 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20         /* xOpen 
c5b70 2a 2f 0a 20 20 20 20 6f 73 32 44 65 6c 65 74 65  */.    os2Delete
c5b80 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65  ,         /* xDe
c5b90 6c 65 74 65 20 2a 2f 0a 20 20 20 20 6f 73 32 41  lete */.    os2A
c5ba0 63 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f  ccess,         /
c5bb0 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20  * xAccess */.   
c5bc0 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65   os2FullPathname
c5bd0 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68  ,   /* xFullPath
c5be0 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 44  name */.    os2D
c5bf0 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f  lOpen,         /
c5c00 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20  * xDlOpen */.   
c5c10 20 6f 73 32 44 6c 45 72 72 6f 72 2c 20 20 20 20   os2DlError,    
c5c20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20      /* xDlError 
c5c30 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 53 79 6d 2c  */.    os2DlSym,
c5c40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c            /* xDl
c5c50 53 79 6d 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c  Sym */.    os2Dl
c5c60 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a  Close,        /*
c5c70 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20   xDlClose */.   
c5c80 20 6f 73 32 52 61 6e 64 6f 6d 6e 65 73 73 2c 20   os2Randomness, 
c5c90 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65      /* xRandomne
c5ca0 73 73 20 2a 2f 0a 20 20 20 20 6f 73 32 53 6c 65  ss */.    os2Sle
c5cb0 65 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ep,          /* 
c5cc0 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 6f 73  xSleep */.    os
c5cd0 32 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20  2CurrentTime,   
c5ce0 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65   /* xCurrentTime
c5cf0 20 2a 2f 0a 20 20 20 20 6f 73 32 47 65 74 4c 61   */.    os2GetLa
c5d00 73 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47  stError    /* xG
c5d10 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20  etLastError */. 
c5d20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66   };.  sqlite3_vf
c5d30 73 5f 72 65 67 69 73 74 65 72 28 26 6f 73 32 56  s_register(&os2V
c5d40 66 73 2c 20 31 29 3b 0a 20 20 69 6e 69 74 55 63  fs, 1);.  initUc
c5d50 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 20 20  onvObjects();.  
c5d60 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
c5d70 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
c5d80 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e  nt sqlite3_os_en
c5d90 64 28 76 6f 69 64 29 7b 0a 20 20 66 72 65 65 55  d(void){.  freeU
c5da0 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 20  convObjects();. 
c5db0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
c5dc0 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  K;.}..#endif /* 
c5dd0 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 2a 2f  SQLITE_OS_OS2 */
c5de0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
c5df0 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 6f 73 32 2e  * End of os_os2.
c5e00 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
c5e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5e30 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
c5e40 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f  * Begin file os_
c5e50 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  unix.c *********
c5e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5e80 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79  /./*.** 2004 May
c5e90 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   22.**.** The au
c5ea0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
c5eb0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
c5ec0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
c5ed0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
c5ee0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
c5ef0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
c5f00 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
c5f10 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
c5f20 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
c5f30 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
c5f40 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
c5f50 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
c5f60 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
c5f70 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
c5f80 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
c5f90 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
c5fa0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
c5fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5ff0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
c6000 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
c6010 20 74 68 65 20 56 46 53 20 69 6d 70 6c 65 6d 65   the VFS impleme
c6020 6e 74 61 74 69 6f 6e 20 66 6f 72 20 75 6e 69 78  ntation for unix
c6030 2d 6c 69 6b 65 20 6f 70 65 72 61 74 69 6e 67 20  -like operating 
c6040 73 79 73 74 65 6d 73 0a 2a 2a 20 69 6e 63 6c 75  systems.** inclu
c6050 64 65 20 4c 69 6e 75 78 2c 20 4d 61 63 4f 53 58  de Linux, MacOSX
c6060 2c 20 2a 42 53 44 2c 20 51 4e 58 2c 20 56 78 57  , *BSD, QNX, VxW
c6070 6f 72 6b 73 2c 20 41 49 58 2c 20 48 50 55 58 2c  orks, AIX, HPUX,
c6080 20 61 6e 64 20 6f 74 68 65 72 73 2e 0a 2a 2a 0a   and others..**.
c6090 2a 2a 20 54 68 65 72 65 20 61 72 65 20 61 63 74  ** There are act
c60a0 75 61 6c 6c 79 20 73 65 76 65 72 61 6c 20 64 69  ually several di
c60b0 66 66 65 72 65 6e 74 20 56 46 53 20 69 6d 70 6c  fferent VFS impl
c60c0 65 6d 65 6e 74 61 74 69 6f 6e 73 20 69 6e 20 74  ementations in t
c60d0 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 54 68 65  his file..** The
c60e0 20 64 69 66 66 65 72 65 6e 63 65 73 20 61 72 65   differences are
c60f0 20 69 6e 20 74 68 65 20 77 61 79 20 74 68 61 74   in the way that
c6100 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 73   file locking is
c6110 20 64 6f 6e 65 2e 20 20 54 68 65 20 64 65 66 61   done.  The defa
c6120 75 6c 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  ult.** implement
c6130 61 74 69 6f 6e 20 75 73 65 73 20 50 6f 73 69 78  ation uses Posix
c6140 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b 73 2e   Advisory Locks.
c6150 20 20 41 6c 74 65 72 6e 61 74 69 76 65 20 69 6d    Alternative im
c6160 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a  plementations.**
c6170 20 75 73 65 20 66 6c 6f 63 6b 28 29 2c 20 64 6f   use flock(), do
c6180 74 2d 66 69 6c 65 73 2c 20 76 61 72 69 6f 75 73  t-files, various
c6190 20 70 72 6f 70 72 69 65 74 61 72 79 20 6c 6f 63   proprietary loc
c61a0 6b 69 6e 67 20 73 63 68 65 6d 61 73 2c 20 6f 72  king schemas, or
c61b0 20 73 69 6d 70 6c 79 0a 2a 2a 20 73 6b 69 70 20   simply.** skip 
c61c0 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 74 6f 67 65  locking all toge
c61d0 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ther..**.** This
c61e0 20 73 6f 75 72 63 65 20 66 69 6c 65 20 69 73 20   source file is 
c61f0 6f 72 67 61 6e 69 7a 65 64 20 69 6e 74 6f 20 64  organized into d
c6200 69 76 69 73 69 6f 6e 73 20 77 68 65 72 65 20 74  ivisions where t
c6210 68 65 20 6c 6f 67 69 63 20 66 6f 72 20 76 61 72  he logic for var
c6220 69 6f 75 73 0a 2a 2a 20 73 75 62 66 75 6e 63 74  ious.** subfunct
c6230 69 6f 6e 73 20 69 73 20 63 6f 6e 74 61 69 6e 65  ions is containe
c6240 64 20 77 69 74 68 69 6e 20 74 68 65 20 61 70 70  d within the app
c6250 72 6f 70 72 69 61 74 65 20 64 69 76 69 73 69 6f  ropriate divisio
c6260 6e 2e 20 20 50 4c 45 41 53 45 0a 2a 2a 20 4b 45  n.  PLEASE.** KE
c6270 45 50 20 54 48 45 20 53 54 52 55 43 54 55 52 45  EP THE STRUCTURE
c6280 20 4f 46 20 54 48 49 53 20 46 49 4c 45 20 49 4e   OF THIS FILE IN
c6290 54 41 43 54 2e 20 20 4e 65 77 20 63 6f 64 65 20  TACT.  New code 
c62a0 73 68 6f 75 6c 64 20 62 65 20 70 6c 61 63 65 64  should be placed
c62b0 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 72 65  .** in the corre
c62c0 63 74 20 64 69 76 69 73 69 6f 6e 20 61 6e 64 20  ct division and 
c62d0 73 68 6f 75 6c 64 20 62 65 20 63 6c 65 61 72 6c  should be clearl
c62e0 79 20 6c 61 62 65 6c 65 64 2e 0a 2a 2a 0a 2a 2a  y labeled..**.**
c62f0 20 54 68 65 20 6c 61 79 6f 75 74 20 6f 66 20 64   The layout of d
c6300 69 76 69 73 69 6f 6e 73 20 69 73 20 61 73 20 66  ivisions is as f
c6310 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
c6320 2a 20 20 47 65 6e 65 72 61 6c 2d 70 75 72 70 6f  *  General-purpo
c6330 73 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20  se declarations 
c6340 61 6e 64 20 75 74 69 6c 69 74 79 20 66 75 6e 63  and utility func
c6350 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a 20 20 55  tions..**   *  U
c6360 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20 6c 6f  nique file ID lo
c6370 67 69 63 20 75 73 65 64 20 62 79 20 56 78 57 6f  gic used by VxWo
c6380 72 6b 73 2e 0a 2a 2a 20 20 20 2a 20 20 56 61 72  rks..**   *  Var
c6390 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 70 72 69  ious locking pri
c63a0 6d 69 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74  mitive implement
c63b0 61 74 69 6f 6e 73 20 28 61 6c 6c 20 65 78 63 65  ations (all exce
c63c0 70 74 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67  pt proxy locking
c63d0 29 3a 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72  ):.**      + for
c63e0 20 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20   Posix Advisory 
c63f0 4c 6f 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b 20  Locks.**      + 
c6400 66 6f 72 20 6e 6f 2d 6f 70 20 6c 6f 63 6b 73 0a  for no-op locks.
c6410 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 64 6f  **      + for do
c6420 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 0a 2a 2a 20  t-file locks.** 
c6430 20 20 20 20 20 2b 20 66 6f 72 20 66 6c 6f 63 6b       + for flock
c6440 28 29 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 20 20  () locking.**   
c6450 20 20 20 2b 20 66 6f 72 20 6e 61 6d 65 64 20 73     + for named s
c6460 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 73 20 28  emaphore locks (
c6470 56 78 57 6f 72 6b 73 20 6f 6e 6c 79 29 0a 2a 2a  VxWorks only).**
c6480 20 20 20 20 20 20 2b 20 66 6f 72 20 41 46 50 20        + for AFP 
c6490 66 69 6c 65 73 79 73 74 65 6d 20 6c 6f 63 6b 73  filesystem locks
c64a0 20 28 4d 61 63 4f 53 58 20 6f 6e 6c 79 29 0a 2a   (MacOSX only).*
c64b0 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 5f 66  *   *  sqlite3_f
c64c0 69 6c 65 20 6d 65 74 68 6f 64 73 20 6e 6f 74 20  ile methods not 
c64d0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
c64e0 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 20 2a 20  locking..**   * 
c64f0 20 44 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20   Definitions of 
c6500 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
c6510 64 73 20 6f 62 6a 65 63 74 73 20 66 6f 72 20 61  ds objects for a
c6520 6c 6c 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 20 20  ll locking.**   
c6530 20 20 20 6d 65 74 68 6f 64 73 20 70 6c 75 73 20     methods plus 
c6540 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f  "finder" functio
c6550 6e 73 20 66 6f 72 20 65 61 63 68 20 6c 6f 63 6b  ns for each lock
c6560 69 6e 67 20 6d 65 74 68 6f 64 2e 0a 2a 2a 20 20  ing method..**  
c6570 20 2a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20   *  sqlite3_vfs 
c6580 6d 65 74 68 6f 64 20 69 6d 70 6c 65 6d 65 6e 74  method implement
c6590 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a 20 20  ations..**   *  
c65a0 4c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 76  Locking primitiv
c65b0 65 73 20 66 6f 72 20 74 68 65 20 70 72 6f 78 79  es for the proxy
c65c0 20 75 62 65 72 2d 6c 6f 63 6b 69 6e 67 2d 6d 65   uber-locking-me
c65d0 74 68 6f 64 2e 20 28 4d 61 63 4f 53 58 20 6f 6e  thod. (MacOSX on
c65e0 6c 79 29 0a 2a 2a 20 20 20 2a 20 20 44 65 66 69  ly).**   *  Defi
c65f0 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74  nitions of sqlit
c6600 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 73 20 66  e3_vfs objects f
c6610 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6d  or all locking m
c6620 65 74 68 6f 64 73 0a 2a 2a 20 20 20 20 20 20 70  ethods.**      p
c6630 6c 75 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  lus implementati
c6640 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 6f  ons of sqlite3_o
c6650 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c  s_init() and sql
c6660 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 0a 2a  ite3_os_end()..*
c6670 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
c6680 55 4e 49 58 20 20 20 20 20 20 20 20 20 20 20 20  UNIX            
c6690 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65 20 69    /* This file i
c66a0 73 20 75 73 65 64 20 6f 6e 20 75 6e 69 78 20 6f  s used on unix o
c66b0 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  nly */../*.** Th
c66c0 65 72 65 20 61 72 65 20 76 61 72 69 6f 75 73 20  ere are various 
c66d0 6d 65 74 68 6f 64 73 20 66 6f 72 20 66 69 6c 65  methods for file
c66e0 20 6c 6f 63 6b 69 6e 67 20 75 73 65 64 20 66 6f   locking used fo
c66f0 72 20 63 6f 6e 63 75 72 72 65 6e 63 79 0a 2a 2a  r concurrency.**
c6700 20 63 6f 6e 74 72 6f 6c 3a 0a 2a 2a 0a 2a 2a 20   control:.**.** 
c6710 20 20 31 2e 20 50 4f 53 49 58 20 6c 6f 63 6b 69    1. POSIX locki
c6720 6e 67 20 28 74 68 65 20 64 65 66 61 75 6c 74 29  ng (the default)
c6730 2c 0a 2a 2a 20 20 20 32 2e 20 4e 6f 20 6c 6f 63  ,.**   2. No loc
c6740 6b 69 6e 67 2c 0a 2a 2a 20 20 20 33 2e 20 44 6f  king,.**   3. Do
c6750 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 2c 0a  t-file locking,.
c6760 2a 2a 20 20 20 34 2e 20 66 6c 6f 63 6b 28 29 20  **   4. flock() 
c6770 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 35 2e  locking,.**   5.
c6780 20 41 46 50 20 6c 6f 63 6b 69 6e 67 20 28 4f 53   AFP locking (OS
c6790 58 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 20 36 2e  X only),.**   6.
c67a0 20 4e 61 6d 65 64 20 50 4f 53 49 58 20 73 65 6d   Named POSIX sem
c67b0 61 70 68 6f 72 65 73 20 28 56 58 57 6f 72 6b 73  aphores (VXWorks
c67c0 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 20 37 2e 20   only),.**   7. 
c67d0 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 20 28  proxy locking. (
c67e0 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a 0a 2a 2a 20  OSX only).**.** 
c67f0 53 74 79 6c 65 73 20 34 2c 20 35 2c 20 61 6e 64  Styles 4, 5, and
c6800 20 37 20 61 72 65 20 6f 6e 6c 79 20 61 76 61 69   7 are only avai
c6810 6c 61 62 6c 65 20 6f 66 20 53 51 4c 49 54 45 5f  lable of SQLITE_
c6820 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
c6830 54 59 4c 45 0a 2a 2a 20 69 73 20 64 65 66 69 6e  TYLE.** is defin
c6840 65 64 20 74 6f 20 31 2e 20 20 54 68 65 20 53 51  ed to 1.  The SQ
c6850 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
c6860 49 4e 47 5f 53 54 59 4c 45 20 61 6c 73 6f 20 65  ING_STYLE also e
c6870 6e 61 62 6c 65 73 20 61 75 74 6f 6d 61 74 69 63  nables automatic
c6880 0a 2a 2a 20 73 65 6c 65 63 74 69 6f 6e 20 6f 66  .** selection of
c6890 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
c68a0 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20 62   locking style b
c68b0 61 73 65 64 20 6f 6e 20 74 68 65 20 66 69 6c 65  ased on the file
c68c0 73 79 73 74 65 6d 0a 2a 2a 20 77 68 65 72 65 20  system.** where 
c68d0 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
c68e0 6c 6f 63 61 74 65 64 2e 20 20 0a 2a 2f 0a 23 69  located.  .*/.#i
c68f0 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
c6900 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
c6910 5f 53 54 59 4c 45 29 0a 23 20 20 69 66 20 64 65  _STYLE).#  if de
c6920 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
c6930 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c  .#    define SQL
c6940 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
c6950 4e 47 5f 53 54 59 4c 45 20 31 0a 23 20 20 65 6c  NG_STYLE 1.#  el
c6960 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53  se.#    define S
c6970 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
c6980 4b 49 4e 47 5f 53 54 59 4c 45 20 30 0a 23 20 20  KING_STYLE 0.#  
c6990 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a  endif.#endif../*
c69a0 0a 2a 2a 20 44 65 66 69 6e 65 20 74 68 65 20 4f  .** Define the O
c69b0 53 5f 56 58 57 4f 52 4b 53 20 70 72 65 2d 70 72  S_VXWORKS pre-pr
c69c0 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 20 74 6f  ocessor macro to
c69d0 20 31 20 69 66 20 62 75 69 6c 64 69 6e 67 20 6f   1 if building o
c69e0 6e 20 0a 2a 2a 20 76 78 77 6f 72 6b 73 2c 20 6f  n .** vxworks, o
c69f0 72 20 30 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a  r 0 otherwise..*
c6a00 2f 0a 23 69 66 6e 64 65 66 20 4f 53 5f 56 58 57  /.#ifndef OS_VXW
c6a10 4f 52 4b 53 0a 23 20 20 69 66 20 64 65 66 69 6e  ORKS.#  if defin
c6a20 65 64 28 5f 5f 52 54 50 5f 5f 29 20 7c 7c 20 64  ed(__RTP__) || d
c6a30 65 66 69 6e 65 64 28 5f 57 52 53 5f 4b 45 52 4e  efined(_WRS_KERN
c6a40 45 4c 29 0a 23 20 20 20 20 64 65 66 69 6e 65 20  EL).#    define 
c6a50 4f 53 5f 56 58 57 4f 52 4b 53 20 31 0a 23 20 20  OS_VXWORKS 1.#  
c6a60 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65  else.#    define
c6a70 20 4f 53 5f 56 58 57 4f 52 4b 53 20 30 0a 23 20   OS_VXWORKS 0.# 
c6a80 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f   endif.#endif../
c6a90 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 65 66 69  *.** These #defi
c6aa0 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e 61 62 6c  nes should enabl
c6ab0 65 20 3e 32 47 42 20 66 69 6c 65 20 73 75 70 70  e >2GB file supp
c6ac0 6f 72 74 20 6f 6e 20 50 6f 73 69 78 20 69 66 20  ort on Posix if 
c6ad0 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 6e  the.** underlyin
c6ae0 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  g operating syst
c6af0 65 6d 20 73 75 70 70 6f 72 74 73 20 69 74 2e 20  em supports it. 
c6b00 20 49 66 20 74 68 65 20 4f 53 20 6c 61 63 6b 73   If the OS lacks
c6b10 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c 65 20 73  .** large file s
c6b20 75 70 70 6f 72 74 2c 20 74 68 65 73 65 20 73 68  upport, these sh
c6b30 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a  ould be no-ops..
c6b40 2a 2a 0a 2a 2a 20 4c 61 72 67 65 20 66 69 6c 65  **.** Large file
c6b50 20 73 75 70 70 6f 72 74 20 63 61 6e 20 62 65 20   support can be 
c6b60 64 69 73 61 62 6c 65 64 20 75 73 69 6e 67 20 74  disabled using t
c6b70 68 65 20 2d 44 53 51 4c 49 54 45 5f 44 49 53 41  he -DSQLITE_DISA
c6b80 42 4c 45 5f 4c 46 53 20 73 77 69 74 63 68 0a 2a  BLE_LFS switch.*
c6b90 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65  * on the compile
c6ba0 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 20  r command line. 
c6bb0 20 54 68 69 73 20 69 73 20 6e 65 63 65 73 73 61   This is necessa
c6bc0 72 79 20 69 66 20 79 6f 75 20 61 72 65 20 63 6f  ry if you are co
c6bd0 6d 70 69 6c 69 6e 67 0a 2a 2a 20 6f 6e 20 61 20  mpiling.** on a 
c6be0 72 65 63 65 6e 74 20 6d 61 63 68 69 6e 65 20 28  recent machine (
c6bf0 65 78 3a 20 52 65 64 48 61 74 20 37 2e 32 29 20  ex: RedHat 7.2) 
c6c00 62 75 74 20 79 6f 75 20 77 61 6e 74 20 79 6f 75  but you want you
c6c10 72 20 63 6f 64 65 20 74 6f 20 77 6f 72 6b 0a 2a  r code to work.*
c6c20 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65 72 20 6d 61  * on an older ma
c6c30 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48 61  chine (ex: RedHa
c6c40 74 20 36 2e 30 29 2e 20 20 49 66 20 79 6f 75 20  t 6.0).  If you 
c6c50 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 65 64 48 61  compile on RedHa
c6c60 74 20 37 2e 32 0a 2a 2a 20 77 69 74 68 6f 75 74  t 7.2.** without
c6c70 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 4c 46   this option, LF
c6c80 53 20 69 73 20 65 6e 61 62 6c 65 2e 20 20 42 75  S is enable.  Bu
c6c90 74 20 4c 46 53 20 64 6f 65 73 20 6e 6f 74 20 65  t LFS does not e
c6ca0 78 69 73 74 20 69 6e 20 74 68 65 20 6b 65 72 6e  xist in the kern
c6cb0 65 6c 0a 2a 2a 20 69 6e 20 52 65 64 48 61 74 20  el.** in RedHat 
c6cc0 36 2e 30 2c 20 73 6f 20 74 68 65 20 63 6f 64 65  6.0, so the code
c6cd0 20 77 6f 6e 27 74 20 77 6f 72 6b 2e 20 20 48 65   won't work.  He
c6ce0 6e 63 65 2c 20 66 6f 72 20 6d 61 78 69 6d 75 6d  nce, for maximum
c6cf0 20 62 69 6e 61 72 79 0a 2a 2a 20 70 6f 72 74 61   binary.** porta
c6d00 62 69 6c 69 74 79 20 79 6f 75 20 73 68 6f 75 6c  bility you shoul
c6d10 64 20 6f 6d 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a  d omit LFS..**.*
c6d20 2a 20 54 68 65 20 70 72 65 76 69 6f 75 73 20 70  * The previous p
c6d30 61 72 61 67 72 61 70 68 20 77 61 73 20 77 72 69  aragraph was wri
c6d40 74 74 65 6e 20 69 6e 20 32 30 30 35 2e 20 20 28  tten in 2005.  (
c6d50 54 68 69 73 20 70 61 72 61 67 72 61 70 68 20 69  This paragraph i
c6d60 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 6f 6e 20  s written.** on 
c6d70 32 30 30 38 2d 31 31 2d 32 38 2e 29 20 54 68 65  2008-11-28.) The
c6d80 73 65 20 64 61 79 73 2c 20 61 6c 6c 20 4c 69 6e  se days, all Lin
c6d90 75 78 20 6b 65 72 6e 65 6c 73 20 73 75 70 70 6f  ux kernels suppo
c6da0 72 74 20 6c 61 72 67 65 20 66 69 6c 65 73 2c 20  rt large files, 
c6db0 73 6f 0a 2a 2a 20 79 6f 75 20 73 68 6f 75 6c 64  so.** you should
c6dc0 20 70 72 6f 62 61 62 6c 79 20 6c 65 61 76 65 20   probably leave 
c6dd0 4c 46 53 20 65 6e 61 62 6c 65 64 2e 20 20 42 75  LFS enabled.  Bu
c6de0 74 20 73 6f 6d 65 20 65 6d 62 65 64 64 65 64 20  t some embedded 
c6df0 70 6c 61 74 66 6f 72 6d 73 20 6d 69 67 68 74 0a  platforms might.
c6e00 2a 2a 20 6c 61 63 6b 20 4c 46 53 20 69 6e 20 77  ** lack LFS in w
c6e10 68 69 63 68 20 63 61 73 65 20 74 68 65 20 53 51  hich case the SQ
c6e20 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53  LITE_DISABLE_LFS
c6e30 20 6d 61 63 72 6f 20 6d 69 67 68 74 20 73 74 69   macro might sti
c6e40 6c 6c 20 62 65 20 75 73 65 66 75 6c 2e 0a 2a 2f  ll be useful..*/
c6e50 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
c6e60 44 49 53 41 42 4c 45 5f 4c 46 53 0a 23 20 64 65  DISABLE_LFS.# de
c6e70 66 69 6e 65 20 5f 4c 41 52 47 45 5f 46 49 4c 45  fine _LARGE_FILE
c6e80 20 20 20 20 20 20 20 31 0a 23 20 69 66 6e 64 65         1.# ifnde
c6e90 66 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42  f _FILE_OFFSET_B
c6ea0 49 54 53 0a 23 20 20 20 64 65 66 69 6e 65 20 5f  ITS.#   define _
c6eb0 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53  FILE_OFFSET_BITS
c6ec0 20 36 34 0a 23 20 65 6e 64 69 66 0a 23 20 64 65   64.# endif.# de
c6ed0 66 69 6e 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f  fine _LARGEFILE_
c6ee0 53 4f 55 52 43 45 20 31 0a 23 65 6e 64 69 66 0a  SOURCE 1.#endif.
c6ef0 0a 2f 2a 0a 2a 2a 20 73 74 61 6e 64 61 72 64 20  ./*.** standard 
c6f00 69 6e 63 6c 75 64 65 20 66 69 6c 65 73 2e 0a 2a  include files..*
c6f10 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f  /.#include <sys/
c6f20 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64  types.h>.#includ
c6f30 65 20 3c 73 79 73 2f 73 74 61 74 2e 68 3e 0a 23  e <sys/stat.h>.#
c6f40 69 6e 63 6c 75 64 65 20 3c 66 63 6e 74 6c 2e 68  include <fcntl.h
c6f50 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 73  >.#include <unis
c6f60 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  td.h>.#include <
c6f70 73 79 73 2f 74 69 6d 65 2e 68 3e 0a 23 69 6e 63  sys/time.h>.#inc
c6f80 6c 75 64 65 20 3c 65 72 72 6e 6f 2e 68 3e 0a 0a  lude <errno.h>..
c6f90 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  #if SQLITE_ENABL
c6fa0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
c6fb0 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 69  # include <sys/i
c6fc0 6f 63 74 6c 2e 68 3e 0a 23 20 69 66 20 4f 53 5f  octl.h>.# if OS_
c6fd0 56 58 57 4f 52 4b 53 0a 23 20 20 69 6e 63 6c 75  VXWORKS.#  inclu
c6fe0 64 65 20 3c 73 65 6d 61 70 68 6f 72 65 2e 68 3e  de <semaphore.h>
c6ff0 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 6c 69 6d  .#  include <lim
c7000 69 74 73 2e 68 3e 0a 23 20 65 6c 73 65 0a 23 20  its.h>.# else.# 
c7010 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 66 69   include <sys/fi
c7020 6c 65 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65  le.h>.#  include
c7030 20 3c 73 79 73 2f 70 61 72 61 6d 2e 68 3e 0a 23   <sys/param.h>.#
c7040 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 6d    include <sys/m
c7050 6f 75 6e 74 2e 68 3e 0a 23 20 65 6e 64 69 66 0a  ount.h>.# endif.
c7060 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
c7070 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
c7080 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  STYLE */../*.** 
c7090 49 66 20 77 65 20 61 72 65 20 74 6f 20 62 65 20  If we are to be 
c70a0 74 68 72 65 61 64 2d 73 61 66 65 2c 20 69 6e 63  thread-safe, inc
c70b0 6c 75 64 65 20 74 68 65 20 70 74 68 72 65 61 64  lude the pthread
c70c0 73 20 68 65 61 64 65 72 20 61 6e 64 20 64 65 66  s header and def
c70d0 69 6e 65 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54  ine.** the SQLIT
c70e0 45 5f 55 4e 49 58 5f 54 48 52 45 41 44 53 20 6d  E_UNIX_THREADS m
c70f0 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c  acro..*/.#if SQL
c7100 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23  ITE_THREADSAFE.#
c7110 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55   define SQLITE_U
c7120 4e 49 58 5f 54 48 52 45 41 44 53 20 31 0a 23 65  NIX_THREADS 1.#e
c7130 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61  ndif../*.** Defa
c7140 75 6c 74 20 70 65 72 6d 69 73 73 69 6f 6e 73 20  ult permissions 
c7150 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20  when creating a 
c7160 6e 65 77 20 66 69 6c 65 0a 2a 2f 0a 23 69 66 6e  new file.*/.#ifn
c7170 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55  def SQLITE_DEFAU
c7180 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53 53 49  LT_FILE_PERMISSI
c7190 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  ONS.# define SQL
c71a0 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45  ITE_DEFAULT_FILE
c71b0 5f 50 45 52 4d 49 53 53 49 4f 4e 53 20 30 36 34  _PERMISSIONS 064
c71c0 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 20 2a 2a  4.#endif../*. **
c71d0 20 44 65 66 61 75 6c 74 20 70 65 72 6d 69 73 73   Default permiss
c71e0 69 6f 6e 73 20 77 68 65 6e 20 63 72 65 61 74 69  ions when creati
c71f0 6e 67 20 61 75 74 6f 20 70 72 6f 78 79 20 64 69  ng auto proxy di
c7200 72 0a 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  r. */.#ifndef SQ
c7210 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f  LITE_DEFAULT_PRO
c7220 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e  XYDIR_PERMISSION
c7230 53 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  S.# define SQLIT
c7240 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 44  E_DEFAULT_PROXYD
c7250 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 20 30  IR_PERMISSIONS 0
c7260 37 35 35 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  755.#endif../*.*
c7270 2a 20 4d 61 78 69 6d 75 6d 20 73 75 70 70 6f 72  * Maximum suppor
c7280 74 65 64 20 70 61 74 68 2d 6c 65 6e 67 74 68 2e  ted path-length.
c7290 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 58 5f  .*/.#define MAX_
c72a0 50 41 54 48 4e 41 4d 45 20 35 31 32 0a 0a 2f 2a  PATHNAME 512../*
c72b0 0a 2a 2a 20 4f 6e 6c 79 20 73 65 74 20 74 68 65  .** Only set the
c72c0 20 6c 61 73 74 45 72 72 6e 6f 20 69 66 20 74 68   lastErrno if th
c72d0 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  e error code is 
c72e0 61 20 72 65 61 6c 20 65 72 72 6f 72 20 61 6e 64  a real error and
c72f0 20 6e 6f 74 20 0a 2a 2a 20 61 20 6e 6f 72 6d 61   not .** a norma
c7300 6c 20 65 78 70 65 63 74 65 64 20 72 65 74 75 72  l expected retur
c7310 6e 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45  n code of SQLITE
c7320 5f 42 55 53 59 20 6f 72 20 53 51 4c 49 54 45 5f  _BUSY or SQLITE_
c7330 4f 4b 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 49 53  OK.*/.#define IS
c7340 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 78 29 20 20  _LOCK_ERROR(x)  
c7350 28 28 78 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b  ((x != SQLITE_OK
c7360 29 20 26 26 20 28 78 20 21 3d 20 53 51 4c 49 54  ) && (x != SQLIT
c7370 45 5f 42 55 53 59 29 29 0a 0a 0a 2f 2a 0a 2a 2a  E_BUSY)).../*.**
c7380 20 53 6f 6d 65 74 69 6d 65 73 2c 20 61 66 74 65   Sometimes, afte
c7390 72 20 61 20 66 69 6c 65 20 68 61 6e 64 6c 65 20  r a file handle 
c73a0 69 73 20 63 6c 6f 73 65 64 20 62 79 20 53 51 4c  is closed by SQL
c73b0 69 74 65 2c 20 74 68 65 20 66 69 6c 65 20 64 65  ite, the file de
c73c0 73 63 72 69 70 74 6f 72 0a 2a 2a 20 63 61 6e 6e  scriptor.** cann
c73d0 6f 74 20 62 65 20 63 6c 6f 73 65 64 20 69 6d 6d  ot be closed imm
c73e0 65 64 69 61 74 65 6c 79 2e 20 49 6e 20 74 68 65  ediately. In the
c73f0 73 65 20 63 61 73 65 73 2c 20 69 6e 73 74 61 6e  se cases, instan
c7400 63 65 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  ces of the follo
c7410 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72  wing.** structur
c7420 65 20 61 72 65 20 75 73 65 64 20 74 6f 20 73 74  e are used to st
c7430 6f 72 65 20 74 68 65 20 66 69 6c 65 20 64 65 73  ore the file des
c7440 63 72 69 70 74 6f 72 20 77 68 69 6c 65 20 77 61  criptor while wa
c7450 69 74 69 6e 67 20 66 6f 72 20 61 6e 0a 2a 2a 20  iting for an.** 
c7460 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 65  opportunity to e
c7470 69 74 68 65 72 20 63 6c 6f 73 65 20 6f 72 20 72  ither close or r
c7480 65 75 73 65 20 69 74 2e 0a 2a 2f 0a 74 79 70 65  euse it..*/.type
c7490 64 65 66 20 73 74 72 75 63 74 20 55 6e 69 78 55  def struct UnixU
c74a0 6e 75 73 65 64 46 64 20 55 6e 69 78 55 6e 75 73  nusedFd UnixUnus
c74b0 65 64 46 64 3b 0a 73 74 72 75 63 74 20 55 6e 69  edFd;.struct Uni
c74c0 78 55 6e 75 73 65 64 46 64 20 7b 0a 20 20 69 6e  xUnusedFd {.  in
c74d0 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 20 20  t fd;           
c74e0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20          /* File 
c74f0 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 63 6c  descriptor to cl
c7500 6f 73 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61  ose */.  int fla
c7510 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gs;             
c7520 20 20 20 2f 2a 20 46 6c 61 67 73 20 74 68 69 73     /* Flags this
c7530 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
c7540 20 77 61 73 20 6f 70 65 6e 65 64 20 77 69 74 68   was opened with
c7550 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 73 65 64   */.  UnixUnused
c7560 46 64 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20  Fd *pNext;      
c7570 2f 2a 20 4e 65 78 74 20 75 6e 75 73 65 64 20 66  /* Next unused f
c7580 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f  ile descriptor o
c7590 6e 20 73 61 6d 65 20 66 69 6c 65 20 2a 2f 0a 7d  n same file */.}
c75a0 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 75 6e 69  ;../*.** The uni
c75b0 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20  xFile structure 
c75c0 69 73 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73  is subclass of s
c75d0 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 70 65 63  qlite3_file spec
c75e0 69 66 69 63 20 74 6f 20 74 68 65 20 75 6e 69 78  ific to the unix
c75f0 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e  .** VFS implemen
c7600 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 70 65  tations..*/.type
c7610 64 65 66 20 73 74 72 75 63 74 20 75 6e 69 78 46  def struct unixF
c7620 69 6c 65 20 75 6e 69 78 46 69 6c 65 3b 0a 73 74  ile unixFile;.st
c7630 72 75 63 74 20 75 6e 69 78 46 69 6c 65 20 7b 0a  ruct unixFile {.
c7640 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74    sqlite3_io_met
c7650 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 4d 65 74  hods const *pMet
c7660 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20  hod;  /* Always 
c7670 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20  the first entry 
c7680 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78  */.  struct unix
c7690 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 20  OpenCnt *pOpen; 
c76a0 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62        /* Info ab
c76b0 6f 75 74 20 61 6c 6c 20 6f 70 65 6e 20 66 64 27  out all open fd'
c76c0 73 20 6f 6e 20 74 68 69 73 20 69 6e 6f 64 65 20  s on this inode 
c76d0 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78  */.  struct unix
c76e0 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b  LockInfo *pLock;
c76f0 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62        /* Info ab
c7700 6f 75 74 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 69  out locks on thi
c7710 73 20 69 6e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74  s inode */.  int
c7720 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   h;             
c7730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c7740 20 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   The file descri
c7750 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 64 69  ptor */.  int di
c7760 72 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  rfd;            
c7770 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
c7780 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f  le descriptor fo
c7790 72 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  r the directory 
c77a0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
c77b0 61 72 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 20  ar locktype;    
c77c0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 79 70        /* The typ
c77d0 65 20 6f 66 20 6c 6f 63 6b 20 68 65 6c 64 20 6f  e of lock held o
c77e0 6e 20 74 68 69 73 20 66 64 20 2a 2f 0a 20 20 69  n this fd */.  i
c77f0 6e 74 20 6c 61 73 74 45 72 72 6e 6f 3b 20 20 20  nt lastErrno;   
c7800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7810 2f 2a 20 54 68 65 20 75 6e 69 78 20 65 72 72 6e  /* The unix errn
c7820 6f 20 66 72 6f 6d 20 74 68 65 20 6c 61 73 74 20  o from the last 
c7830 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 20 20 76  I/O error */.  v
c7840 6f 69 64 20 2a 6c 6f 63 6b 69 6e 67 43 6f 6e 74  oid *lockingCont
c7850 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ext;            
c7860 2f 2a 20 4c 6f 63 6b 69 6e 67 20 73 74 79 6c 65  /* Locking style
c7870 20 73 70 65 63 69 66 69 63 20 73 74 61 74 65 20   specific state 
c7880 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46  */.  UnixUnusedF
c7890 64 20 2a 70 55 6e 75 73 65 64 3b 20 20 20 20 20  d *pUnused;     
c78a0 20 20 20 20 20 20 2f 2a 20 50 72 65 2d 61 6c 6c        /* Pre-all
c78b0 6f 63 61 74 65 64 20 55 6e 69 78 55 6e 75 73 65  ocated UnixUnuse
c78c0 64 46 64 20 2a 2f 0a 20 20 69 6e 74 20 66 69 6c  dFd */.  int fil
c78d0 65 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20  eFlags;         
c78e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 73            /* Mis
c78f0 63 65 6c 6c 61 6e 6f 75 73 20 66 6c 61 67 73 20  cellanous flags 
c7900 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  */.#if SQLITE_EN
c7910 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
c7920 4c 45 0a 20 20 69 6e 74 20 6f 70 65 6e 46 6c 61  LE.  int openFla
c7930 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gs;             
c7940 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 6c 61        /* The fla
c7950 67 73 20 73 70 65 63 69 66 69 65 64 20 61 74 20  gs specified at 
c7960 6f 70 65 6e 28 29 20 2a 2f 0a 23 65 6e 64 69 66  open() */.#endif
c7970 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
c7980 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65  ADSAFE && define
c7990 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 70  d(__linux__).  p
c79a0 74 68 72 65 61 64 5f 74 20 74 69 64 3b 20 20 20  thread_t tid;   
c79b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c79c0 2f 2a 20 54 68 65 20 74 68 72 65 61 64 20 74 68  /* The thread th
c79d0 61 74 20 22 6f 77 6e 73 22 20 74 68 69 73 20 75  at "owns" this u
c79e0 6e 69 78 46 69 6c 65 20 2a 2f 0a 23 65 6e 64 69  nixFile */.#endi
c79f0 66 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53  f.#if OS_VXWORKS
c7a00 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74 65 3b  .  int isDelete;
c7a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7a20 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 6f 6e      /* Delete on
c7a30 20 63 6c 6f 73 65 20 69 66 20 74 72 75 65 20 2a   close if true *
c7a40 2f 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72  /.  struct vxwor
c7a50 6b 73 46 69 6c 65 49 64 20 2a 70 49 64 3b 20 20  ksFileId *pId;  
c7a60 20 20 20 20 20 2f 2a 20 55 6e 69 71 75 65 20 66       /* Unique f
c7a70 69 6c 65 20 49 44 20 2a 2f 0a 23 65 6e 64 69 66  ile ID */.#endif
c7a80 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
c7a90 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 67 72    /* The next gr
c7aa0 6f 75 70 20 6f 66 20 76 61 72 69 61 62 6c 65 73  oup of variables
c7ab0 20 61 72 65 20 75 73 65 64 20 74 6f 20 74 72 61   are used to tra
c7ac0 63 6b 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ck whether or no
c7ad0 74 20 74 68 65 0a 20 20 2a 2a 20 74 72 61 6e 73  t the.  ** trans
c7ae0 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69  action counter i
c7af0 6e 20 62 79 74 65 73 20 32 34 2d 32 37 20 6f 66  n bytes 24-27 of
c7b00 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20   database files 
c7b10 61 72 65 20 75 70 64 61 74 65 64 0a 20 20 2a 2a  are updated.  **
c7b20 20 77 68 65 6e 65 76 65 72 20 61 6e 79 20 70 61   whenever any pa
c7b30 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  rt of the databa
c7b40 73 65 20 63 68 61 6e 67 65 73 2e 20 20 41 6e 20  se changes.  An 
c7b50 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c 74 20  assertion fault 
c7b60 77 69 6c 6c 0a 20 20 2a 2a 20 6f 63 63 75 72 20  will.  ** occur 
c7b70 69 66 20 61 20 66 69 6c 65 20 69 73 20 75 70 64  if a file is upd
c7b80 61 74 65 64 20 77 69 74 68 6f 75 74 20 61 6c 73  ated without als
c7b90 6f 20 75 70 64 61 74 69 6e 67 20 74 68 65 20 74  o updating the t
c7ba0 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20  ransaction.  ** 
c7bb0 63 6f 75 6e 74 65 72 2e 20 20 54 68 69 73 20 74  counter.  This t
c7bc0 65 73 74 20 69 73 20 6d 61 64 65 20 74 6f 20 61  est is made to a
c7bd0 76 6f 69 64 20 6e 65 77 20 70 72 6f 62 6c 65 6d  void new problem
c7be0 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68 65  s similar to the
c7bf0 0a 20 20 2a 2a 20 6f 6e 65 20 64 65 73 63 72 69  .  ** one descri
c7c00 62 65 64 20 62 79 20 74 69 63 6b 65 74 20 23 33  bed by ticket #3
c7c10 35 38 34 2e 20 0a 20 20 2a 2f 0a 20 20 75 6e 73  584. .  */.  uns
c7c20 69 67 6e 65 64 20 63 68 61 72 20 74 72 61 6e 73  igned char trans
c7c30 43 6e 74 72 43 68 6e 67 3b 20 20 20 2f 2a 20 54  CntrChng;   /* T
c7c40 72 75 65 20 69 66 20 74 68 65 20 74 72 61 6e 73  rue if the trans
c7c50 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 63  action counter c
c7c60 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 6e 73 69  hanged */.  unsi
c7c70 67 6e 65 64 20 63 68 61 72 20 64 62 55 70 64 61  gned char dbUpda
c7c80 74 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72  te;        /* Tr
c7c90 75 65 20 69 66 20 61 6e 79 20 70 61 72 74 20 6f  ue if any part o
c7ca0 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  f database file 
c7cb0 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 6e 73  changed */.  uns
c7cc0 69 67 6e 65 64 20 63 68 61 72 20 69 6e 4e 6f 72  igned char inNor
c7cd0 6d 61 6c 57 72 69 74 65 3b 20 20 20 2f 2a 20 54  malWrite;   /* T
c7ce0 72 75 65 20 69 66 20 69 6e 20 61 20 6e 6f 72 6d  rue if in a norm
c7cf0 61 6c 20 77 72 69 74 65 20 6f 70 65 72 61 74 69  al write operati
c7d00 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66  on */.#endif.#if
c7d10 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
c7d20 20 20 2f 2a 20 49 6e 20 74 65 73 74 20 6d 6f 64    /* In test mod
c7d30 65 2c 20 69 6e 63 72 65 61 73 65 20 74 68 65 20  e, increase the 
c7d40 73 69 7a 65 20 6f 66 20 74 68 69 73 20 73 74 72  size of this str
c7d50 75 63 74 75 72 65 20 61 20 62 69 74 20 73 6f 20  ucture a bit so 
c7d60 74 68 61 74 20 0a 20 20 2a 2a 20 69 74 20 69 73  that .  ** it is
c7d70 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65   larger than the
c7d80 20 73 74 72 75 63 74 20 43 72 61 73 68 46 69 6c   struct CrashFil
c7d90 65 20 64 65 66 69 6e 65 64 20 69 6e 20 74 65 73  e defined in tes
c7da0 74 36 2e 63 2e 0a 20 20 2a 2f 0a 20 20 63 68 61  t6.c..  */.  cha
c7db0 72 20 61 50 61 64 64 69 6e 67 5b 33 32 5d 3b 0a  r aPadding[32];.
c7dc0 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  #endif.};../*.**
c7dd0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   The following m
c7de0 61 63 72 6f 73 20 64 65 66 69 6e 65 20 62 69 74  acros define bit
c7df0 73 20 69 6e 20 75 6e 69 78 46 69 6c 65 2e 66 69  s in unixFile.fi
c7e00 6c 65 46 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69  leFlags.*/.#defi
c7e10 6e 65 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f  ne SQLITE_WHOLE_
c7e20 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 20 30 78  FILE_LOCKING  0x
c7e30 30 30 30 31 20 20 20 2f 2a 20 55 73 65 20 77 68  0001   /* Use wh
c7e40 6f 6c 65 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e 67  ole-file locking
c7e50 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75   */../*.** Inclu
c7e60 64 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  de code that is 
c7e70 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73  common to all os
c7e80 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a  _*.c files.*/./*
c7e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
c7ea0 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e  clude os_common.
c7eb0 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
c7ec0 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a  of os_unix.c ***
c7ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
c7ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
c7ef0 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d  gin file os_comm
c7f00 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on.h ***********
c7f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
c7f30 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a  .** 2004 May 22.
c7f40 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
c7f50 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
c7f60 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
c7f70 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
c7f80 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
c7f90 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
c7fa0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
c7fb0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
c7fc0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
c7fd0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
c7fe0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
c7ff0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
c8000 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
c8010 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
c8020 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
c8030 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
c8040 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
c8050 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
c8060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c80a0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
c80b0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63  ile contains mac
c80c0 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65  ros and a little
c80d0 20 62 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61   bit of code tha
c80e0 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a  t is common to.*
c80f0 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61  * all of the pla
c8100 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66  tform-specific f
c8110 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e  iles (os_*.c) an
c8120 64 20 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69  d is #included i
c8130 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c  nto those.** fil
c8140 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  es..**.** This f
c8150 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69  ile should be #i
c8160 6e 63 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f  ncluded by the o
c8170 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79  s_*.c files only
c8180 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a  .  It is not a.*
c8190 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73  * general purpos
c81a0 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a  e header file..*
c81b0 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d  *.** $Id: os_com
c81c0 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38 20 32 30 30  mon.h,v 1.38 200
c81d0 39 2f 30 32 2f 32 34 20 31 38 3a 34 30 3a 35 30  9/02/24 18:40:50
c81e0 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
c81f0 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f   $.*/.#ifndef _O
c8200 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66  S_COMMON_H_.#def
c8210 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48  ine _OS_COMMON_H
c8220 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73  _../*.** At leas
c8230 74 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20  t two bugs have 
c8240 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75  slipped in becau
c8250 73 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68  se we changed th
c8260 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a  e MEMORY_DEBUG.*
c8270 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54  * macro to SQLIT
c8280 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65  E_DEBUG and some
c8290 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73   older makefiles
c82a0 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61   have not yet ma
c82b0 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68  de the.** switch
c82c0 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
c82d0 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74   code should cat
c82e0 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20  ch this problem 
c82f0 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e  at compile-time.
c8300 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52  .*/.#ifdef MEMOR
c8310 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20  Y_DEBUG.# error 
c8320 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55  "The MEMORY_DEBU
c8330 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c  G macro is obsol
c8340 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45  ete.  Use SQLITE
c8350 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22  _DEBUG instead."
c8360 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
c8370 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c  SQLITE_DEBUG.SQL
c8380 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
c8390 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d  sqlite3OSTrace =
c83a0 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52   0;.#define OSTR
c83b0 41 43 45 31 28 58 29 20 20 20 20 20 20 20 20 20  ACE1(X)         
c83c0 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
c83d0 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
c83e0 67 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69  gPrintf(X).#defi
c83f0 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29  ne OSTRACE2(X,Y)
c8400 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
c8410 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
c8420 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
c8430 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,Y).#define OSTR
c8440 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20  ACE3(X,Y,Z)     
c8450 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
c8460 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
c8470 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23  gPrintf(X,Y,Z).#
c8480 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28  define OSTRACE4(
c8490 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73  X,Y,Z,A)   if( s
c84a0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
c84b0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
c84c0 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66  tf(X,Y,Z,A).#def
c84d0 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59  ine OSTRACE5(X,Y
c84e0 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69  ,Z,A,B) if( sqli
c84f0 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c  te3OSTrace ) sql
c8500 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
c8510 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69  X,Y,Z,A,B).#defi
c8520 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c  ne OSTRACE6(X,Y,
c8530 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69  Z,A,B,C) \.    i
c8540 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  f(sqlite3OSTrace
c8550 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
c8560 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  intf(X,Y,Z,A,B,C
c8570 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
c8580 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44  E7(X,Y,Z,A,B,C,D
c8590 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74  ) \.    if(sqlit
c85a0 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74  e3OSTrace) sqlit
c85b0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
c85c0 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c  Y,Z,A,B,C,D).#el
c85d0 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  se.#define OSTRA
c85e0 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f  CE1(X).#define O
c85f0 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65  STRACE2(X,Y).#de
c8600 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c  fine OSTRACE3(X,
c8610 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  Y,Z).#define OST
c8620 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23  RACE4(X,Y,Z,A).#
c8630 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28  define OSTRACE5(
c8640 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69  X,Y,Z,A,B).#defi
c8650 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c  ne OSTRACE6(X,Y,
c8660 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65  Z,A,B,C).#define
c8670 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c   OSTRACE7(X,Y,Z,
c8680 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a  A,B,C,D).#endif.
c8690 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f  ./*.** Macros fo
c86a0 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72  r performance tr
c86b0 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79  acing.  Normally
c86c0 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e   turned off.  On
c86d0 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69  ly works.** on i
c86e0 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f  486 hardware..*/
c86f0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50  .#ifdef SQLITE_P
c8700 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45  ERFORMANCE_TRACE
c8710 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e  ../* .** hwtime.
c8720 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e  h contains inlin
c8730 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65  e assembler code
c8740 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e   for implementin
c8750 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f  g .** high-perfo
c8760 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f  rmance timing ro
c8770 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a  utines..*/./****
c8780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
c8790 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74  de hwtime.h in t
c87a0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f  he middle of os_
c87b0 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
c87c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
c87d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
c87e0 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a   file hwtime.h *
c87f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
c8820 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a   2008 May 27.**.
c8830 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
c8840 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
c8850 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
c8860 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
c8870 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
c8880 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
c8890 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
c88a0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
c88b0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
c88c0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
c88d0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
c88e0 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
c88f0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
c8900 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
c8910 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
c8920 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
c8930 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
c8940 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
c8950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8990 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
c89a0 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65   contains inline
c89b0 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65   asm code for re
c89c0 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70  trieving "high-p
c89d0 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63  erformance".** c
c89e0 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20  ounters for x86 
c89f0 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a  class CPUs..**.*
c8a00 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c  * $Id: hwtime.h,
c8a10 76 20 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31  v 1.3 2008/08/01
c8a20 20 31 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20   14:33:15 shane 
c8a30 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66  Exp $.*/.#ifndef
c8a40 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66   _HWTIME_H_.#def
c8a50 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a  ine _HWTIME_H_..
c8a60 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
c8a70 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79  ing routine only
c8a80 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75   works on pentiu
c8a90 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65  m-class (or newe
c8aa0 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a  r) processors..*
c8ab0 2a 20 49 74 20 75 73 65 73 20 74 68 65 20 52 44  * It uses the RD
c8ac0 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65  TSC opcode to re
c8ad0 61 64 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75  ad the cycle cou
c8ae0 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20  nt value out of 
c8af0 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72  the.** processor
c8b00 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61   and returns tha
c8b10 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63  t value.  This c
c8b20 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 68  an be used for h
c8b30 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69  igh-res.** profi
c8b40 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65  ling..*/.#if (de
c8b50 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20  fined(__GNUC__) 
c8b60 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f  || defined(_MSC_
c8b70 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20  VER)) && \.     
c8b80 20 28 64 65 66 69 6e 65 64 28 69 33 38 36 29 20   (defined(i386) 
c8b90 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38  || defined(__i38
c8ba0 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  6__) || defined(
c8bb0 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66  _M_IX86))..  #if
c8bc0 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f   defined(__GNUC_
c8bd0 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f  _)..  __inline__
c8be0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
c8bf0 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
c8c00 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65  d){.     unsigne
c8c10 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20  d int lo, hi;.  
c8c20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
c8c30 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22  atile__ ("rdtsc"
c8c40 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d   : "=a" (lo), "=
c8c50 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72  d" (hi));.     r
c8c60 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69  eturn (sqlite_ui
c8c70 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20  nt64)hi << 32 | 
c8c80 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66  lo;.  }..  #elif
c8c90 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45   defined(_MSC_VE
c8ca0 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63  R)..  __declspec
c8cb0 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65  (naked) __inline
c8cc0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f   sqlite_uint64 _
c8cd0 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77  _cdecl sqlite3Hw
c8ce0 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20  time(void){.    
c8cf0 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20   __asm {.       
c8d00 20 72 64 74 73 63 0a 20 20 20 20 20 20 20 20 72   rdtsc.        r
c8d10 65 74 20 20 20 20 20 20 20 3b 20 72 65 74 75 72  et       ; retur
c8d20 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45  n value at EDX:E
c8d30 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  AX.     }.  }.. 
c8d40 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28   #endif..#elif (
c8d50 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f  defined(__GNUC__
c8d60 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78  ) && defined(__x
c8d70 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69  86_64__))..  __i
c8d80 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75  nline__ sqlite_u
c8d90 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74  int64 sqlite3Hwt
c8da0 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20  ime(void){.     
c8db0 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76   unsigned long v
c8dc0 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f  al;.      __asm_
c8dd0 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
c8de0 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28  "rdtsc" : "=A" (
c8df0 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74  val));.      ret
c8e00 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23  urn val;.  }. .#
c8e10 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f  elif (defined(__
c8e20 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e  GNUC__) && defin
c8e30 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20  ed(__ppc__))..  
c8e40 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74  __inline__ sqlit
c8e50 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e_uint64 sqlite3
c8e60 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20  Hwtime(void){.  
c8e70 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e      unsigned lon
c8e80 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20  g long retval;. 
c8e90 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f       unsigned lo
c8ea0 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f  ng junk;.      _
c8eb0 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c  _asm__ __volatil
c8ec0 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20  e__ ("\n\.      
c8ed0 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62      1:      mftb
c8ee0 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20  u   %1\n\.      
c8ef0 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62              mftb
c8f00 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20      %L0\n\.     
c8f10 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66 74               mft
c8f20 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20  bu   %0\n\.     
c8f30 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6d 70               cmp
c8f40 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20  w    %0,%1\n\.  
c8f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c8f60 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20 20 20  bne     1b".    
c8f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3a 20                : 
c8f80 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22  "=r" (retval), "
c8f90 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20  =r" (junk));.   
c8fa0 20 20 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c     return retval
c8fb0 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20  ;.  }..#else..  
c8fc0 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c  #error Need impl
c8fd0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71  ementation of sq
c8fe0 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f  lite3Hwtime() fo
c8ff0 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e  r your platform.
c9000 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63  ..  /*.  ** To c
c9010 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69  ompile without i
c9020 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69  mplementing sqli
c9030 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20  te3Hwtime() for 
c9040 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20  your platform,. 
c9050 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f   ** you can remo
c9060 76 65 20 74 68 65 20 61 62 6f 76 65 20 23 65 72  ve the above #er
c9070 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68 65 20  ror and use the 
c9080 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73  following.  ** s
c9090 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59  tub function.  Y
c90a0 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d  ou will lose tim
c90b0 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20  ing support for 
c90c0 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65  many.  ** of the
c90d0 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74   debugging and t
c90e0 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73  esting utilities
c90f0 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20  , but it should 
c9100 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f  at.  ** least co
c9110 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20  mpile and run.. 
c9120 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
c9130 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74  TE   sqlite_uint
c9140 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  64 sqlite3Hwtime
c9150 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28  (void){ return (
c9160 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30  (sqlite_uint64)0
c9170 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65  ); }..#endif..#e
c9180 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
c9190 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a  (_HWTIME_H_) */.
c91a0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
c91b0 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68   End of hwtime.h
c91c0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
c91d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c91e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
c91f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
c9200 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
c9210 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
c9220 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a   os_common.h ***
c9230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
c9240 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f  ..static sqlite_
c9250 75 69 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a  uint64 g_start;.
c9260 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69  static sqlite_ui
c9270 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a  nt64 g_elapsed;.
c9280 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54  #define TIMER_ST
c9290 41 52 54 20 20 20 20 20 20 20 67 5f 73 74 61 72  ART       g_star
c92a0 74 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  t=sqlite3Hwtime(
c92b0 29 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  ).#define TIMER_
c92c0 45 4e 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c  END         g_el
c92d0 61 70 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74  apsed=sqlite3Hwt
c92e0 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64  ime()-g_start.#d
c92f0 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50  efine TIMER_ELAP
c9300 53 45 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65  SED     g_elapse
c9310 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  d.#else.#define 
c9320 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66  TIMER_START.#def
c9330 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64  ine TIMER_END.#d
c9340 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50  efine TIMER_ELAP
c9350 53 45 44 20 20 20 20 20 28 28 73 71 6c 69 74 65  SED     ((sqlite
c9360 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69  _uint64)0).#endi
c9370 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63  f../*.** If we c
c9380 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68 65 20  ompile with the 
c9390 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72  SQLITE_TEST macr
c93a0 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20  o set, then the 
c93b0 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a  following block.
c93c0 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20  ** of code will 
c93d0 67 69 76 65 20 75 73 20 74 68 65 20 61 62 69 6c  give us the abil
c93e0 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20  ity to simulate 
c93f0 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72  a disk I/O error
c9400 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73  .  This.** is us
c9410 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74  ed for testing t
c9420 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20  he I/O recovery 
c9430 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66  logic..*/.#ifdef
c9440 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
c9450 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
c9460 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
c9470 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
c9480 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72   /* Total number
c9490 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a   of I/O Errors *
c94a0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
c94b0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
c94c0 72 5f 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20  r_hardhit = 0;  
c94d0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
c94e0 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72  of non-benign er
c94f0 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  rors */.SQLITE_A
c9500 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
c9510 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20  o_error_pending 
c9520 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43  = 0;        /* C
c9530 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72  ount down to fir
c9540 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a  st I/O error */.
c9550 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
c9560 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
c9570 70 65 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20  persist = 0;    
c9580 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49      /* True if I
c9590 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 73 69 73  /O errors persis
c95a0 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  t */.SQLITE_API 
c95b0 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
c95c0 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b  rror_benign = 0;
c95d0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
c95e0 20 69 66 20 65 72 72 6f 72 73 20 61 72 65 20 62   if errors are b
c95f0 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f  enign */.SQLITE_
c9600 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
c9610 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67  diskfull_pending
c9620 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49   = 0;.SQLITE_API
c9630 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73   int sqlite3_dis
c9640 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69  kfull = 0;.#defi
c9650 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  ne SimulateIOErr
c9660 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69  orBenign(X) sqli
c9670 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e  te3_io_error_ben
c9680 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20  ign=(X).#define 
c9690 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
c96a0 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28  CODE)  \.  if( (
c96b0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
c96c0 5f 70 65 72 73 69 73 74 20 26 26 20 73 71 6c 69  _persist && sqli
c96d0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
c96e0 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71  ) \.       || sq
c96f0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
c9700 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20  ending-- == 1 ) 
c9710 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   \.             
c9720 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29   { local_ioerr()
c9730 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63  ; CODE; }.static
c9740 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72   void local_ioer
c9750 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28  r(){.  IOTRACE((
c9760 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73  "IOERR\n"));.  s
c9770 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
c9780 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71  hit++;.  if( !sq
c9790 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62  lite3_io_error_b
c97a0 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f  enign ) sqlite3_
c97b0 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74  io_error_hardhit
c97c0 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69  ++;.}.#define Si
c97d0 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72  mulateDiskfullEr
c97e0 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69  ror(CODE) \.   i
c97f0 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  f( sqlite3_diskf
c9800 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c  ull_pending ){ \
c9810 0a 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  .     if( sqlite
c9820 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69  3_diskfull_pendi
c9830 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20  ng == 1 ){ \.   
c9840 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28      local_ioerr(
c9850 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69  ); \.       sqli
c9860 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31  te3_diskfull = 1
c9870 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74  ; \.       sqlit
c9880 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20  e3_io_error_hit 
c9890 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f  = 1; \.       CO
c98a0 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65  DE; \.     }else
c98b0 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74  { \.       sqlit
c98c0 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64  e3_diskfull_pend
c98d0 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20  ing--; \.     } 
c98e0 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65  \.   }.#else.#de
c98f0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45  fine SimulateIOE
c9900 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64  rrorBenign(X).#d
c9910 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f  efine SimulateIO
c9920 45 72 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65  Error(A).#define
c9930 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c   SimulateDiskful
c9940 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66  lError(A).#endif
c9950 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73  ../*.** When tes
c9960 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75  ting, keep a cou
c9970 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  nt of the number
c9980 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a   of open files..
c9990 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
c99a0 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
c99b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65   int sqlite3_ope
c99c0 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30  n_file_count = 0
c99d0 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f  ;.#define OpenCo
c99e0 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69 74 65  unter(X)  sqlite
c99f0 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e  3_open_file_coun
c9a00 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65  t+=(X).#else.#de
c9a10 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72  fine OpenCounter
c9a20 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64  (X).#endif..#end
c9a30 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f  if /* !defined(_
c9a40 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f  OS_COMMON_H_) */
c9a50 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
c9a60 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d  * End of os_comm
c9a70 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on.h ***********
c9a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9aa0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
c9ab0 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
c9ac0 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
c9ad0 6e 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a  n os_unix.c ****
c9ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9af0 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 20  /../*.** Define 
c9b00 76 61 72 69 6f 75 73 20 6d 61 63 72 6f 73 20 74  various macros t
c9b10 68 61 74 20 61 72 65 20 6d 69 73 73 69 6e 67 20  hat are missing 
c9b20 66 72 6f 6d 20 73 6f 6d 65 20 73 79 73 74 65 6d  from some system
c9b30 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4f 5f  s..*/.#ifndef O_
c9b40 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 65 66 69  LARGEFILE.# defi
c9b50 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30  ne O_LARGEFILE 0
c9b60 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
c9b70 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46  QLITE_DISABLE_LF
c9b80 53 0a 23 20 75 6e 64 65 66 20 4f 5f 4c 41 52 47  S.# undef O_LARG
c9b90 45 46 49 4c 45 0a 23 20 64 65 66 69 6e 65 20 4f  EFILE.# define O
c9ba0 5f 4c 41 52 47 45 46 49 4c 45 20 30 0a 23 65 6e  _LARGEFILE 0.#en
c9bb0 64 69 66 0a 23 69 66 6e 64 65 66 20 4f 5f 4e 4f  dif.#ifndef O_NO
c9bc0 46 4f 4c 4c 4f 57 0a 23 20 64 65 66 69 6e 65 20  FOLLOW.# define 
c9bd0 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20 30 0a 23 65 6e  O_NOFOLLOW 0.#en
c9be0 64 69 66 0a 23 69 66 6e 64 65 66 20 4f 5f 42 49  dif.#ifndef O_BI
c9bf0 4e 41 52 59 0a 23 20 64 65 66 69 6e 65 20 4f 5f  NARY.# define O_
c9c00 42 49 4e 41 52 59 20 30 0a 23 65 6e 64 69 66 0a  BINARY 0.#endif.
c9c10 0a 2f 2a 0a 2a 2a 20 54 68 65 20 44 4a 47 50 50  ./*.** The DJGPP
c9c20 20 63 6f 6d 70 69 6c 65 72 20 65 6e 76 69 72 6f   compiler enviro
c9c30 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 20 6d 6f 73 74  nment looks most
c9c40 6c 79 20 6c 69 6b 65 20 55 6e 69 78 2c 20 62 75  ly like Unix, bu
c9c50 74 20 69 74 0a 2a 2a 20 6c 61 63 6b 73 20 74 68  t it.** lacks th
c9c60 65 20 66 63 6e 74 6c 28 29 20 73 79 73 74 65 6d  e fcntl() system
c9c70 20 63 61 6c 6c 2e 20 20 53 6f 20 72 65 64 65 66   call.  So redef
c9c80 69 6e 65 20 66 63 6e 74 6c 28 29 20 74 6f 20 62  ine fcntl() to b
c9c90 65 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 74  e something.** t
c9ca0 68 61 74 20 61 6c 77 61 79 73 20 73 75 63 63 65  hat always succe
c9cb0 65 64 73 2e 20 20 54 68 69 73 20 6d 65 61 6e 73  eds.  This means
c9cc0 20 74 68 61 74 20 6c 6f 63 6b 69 6e 67 20 64 6f   that locking do
c9cd0 65 73 20 6e 6f 74 20 6f 63 63 75 72 20 75 6e 64  es not occur und
c9ce0 65 72 0a 2a 2a 20 44 4a 47 50 50 2e 20 20 42 75  er.** DJGPP.  Bu
c9cf0 74 20 69 74 20 69 73 20 44 4f 53 20 2d 20 77 68  t it is DOS - wh
c9d00 61 74 20 64 69 64 20 79 6f 75 20 65 78 70 65 63  at did you expec
c9d10 74 3f 0a 2a 2f 0a 23 69 66 64 65 66 20 5f 5f 44  t?.*/.#ifdef __D
c9d20 4a 47 50 50 5f 5f 0a 23 20 64 65 66 69 6e 65 20  JGPP__.# define 
c9d30 66 63 6e 74 6c 28 41 2c 42 2c 43 29 20 30 0a 23  fcntl(A,B,C) 0.#
c9d40 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
c9d50 20 74 68 72 65 61 64 69 64 20 6d 61 63 72 6f 20   threadid macro 
c9d60 72 65 73 6f 6c 76 65 73 20 74 6f 20 74 68 65 20  resolves to the 
c9d70 74 68 72 65 61 64 2d 69 64 20 6f 72 20 74 6f 20  thread-id or to 
c9d80 30 2e 20 20 55 73 65 64 20 66 6f 72 0a 2a 2a 20  0.  Used for.** 
c9d90 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75  testing and debu
c9da0 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23  gging only..*/.#
c9db0 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
c9dc0 53 41 46 45 0a 23 64 65 66 69 6e 65 20 74 68 72  SAFE.#define thr
c9dd0 65 61 64 69 64 20 70 74 68 72 65 61 64 5f 73 65  eadid pthread_se
c9de0 6c 66 28 29 0a 23 65 6c 73 65 0a 23 64 65 66 69  lf().#else.#defi
c9df0 6e 65 20 74 68 72 65 61 64 69 64 20 30 0a 23 65  ne threadid 0.#e
c9e00 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c  ndif.../*.** Hel
c9e10 70 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f  per functions to
c9e20 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c 69   obtain and reli
c9e30 6e 71 75 69 73 68 20 74 68 65 20 67 6c 6f 62 61  nquish the globa
c9e40 6c 20 6d 75 74 65 78 2e 20 54 68 65 0a 2a 2a 20  l mutex. The.** 
c9e50 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 69 73 20  global mutex is 
c9e60 75 73 65 64 20 74 6f 20 70 72 6f 74 65 63 74 20  used to protect 
c9e70 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 2c  the unixOpenCnt,
c9e80 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 61 6e   unixLockInfo an
c9e90 64 0a 2a 2a 20 76 78 77 6f 72 6b 73 46 69 6c 65  d.** vxworksFile
c9ea0 49 64 20 6f 62 6a 65 63 74 73 20 75 73 65 64 20  Id objects used 
c9eb0 62 79 20 74 68 69 73 20 66 69 6c 65 2c 20 61 6c  by this file, al
c9ec0 6c 20 6f 66 20 77 68 69 63 68 20 6d 61 79 20 62  l of which may b
c9ed0 65 20 0a 2a 2a 20 73 68 61 72 65 64 20 62 79 20  e .** shared by 
c9ee0 6d 75 6c 74 69 70 6c 65 20 74 68 72 65 61 64 73  multiple threads
c9ef0 2e 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e  ..**.** Function
c9f00 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28 29   unixMutexHeld()
c9f10 20 69 73 20 75 73 65 64 20 74 6f 20 61 73 73 65   is used to asse
c9f20 72 74 28 29 20 74 68 61 74 20 74 68 65 20 67 6c  rt() that the gl
c9f30 6f 62 61 6c 20 6d 75 74 65 78 20 0a 2a 2a 20 69  obal mutex .** i
c9f40 73 20 68 65 6c 64 20 77 68 65 6e 20 72 65 71 75  s held when requ
c9f50 69 72 65 64 2e 20 54 68 69 73 20 66 75 6e 63 74  ired. This funct
c9f60 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64  ion is only used
c9f70 20 61 73 20 70 61 72 74 20 6f 66 20 61 73 73 65   as part of asse
c9f80 72 74 28 29 20 0a 2a 2a 20 73 74 61 74 65 6d 65  rt() .** stateme
c9f90 6e 74 73 2e 20 65 2e 67 2e 0a 2a 2a 0a 2a 2a 20  nts. e.g..**.** 
c9fa0 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78    unixEnterMutex
c9fb0 28 29 0a 2a 2a 20 20 20 20 20 61 73 73 65 72 74  ().**     assert
c9fc0 28 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28  ( unixMutexHeld(
c9fd0 29 20 29 3b 0a 2a 2a 20 20 20 75 6e 69 78 45 6e  ) );.**   unixEn
c9fe0 74 65 72 4c 65 61 76 65 28 29 0a 2a 2f 0a 73 74  terLeave().*/.st
c9ff0 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 45 6e  atic void unixEn
ca000 74 65 72 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a  terMutex(void){.
ca010 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
ca020 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74  enter(sqlite3Mut
ca030 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
ca040 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
ca050 45 52 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76  ER));.}.static v
ca060 6f 69 64 20 75 6e 69 78 4c 65 61 76 65 4d 75 74  oid unixLeaveMut
ca070 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69  ex(void){.  sqli
ca080 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
ca090 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
ca0a0 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
ca0b0 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a  TATIC_MASTER));.
ca0c0 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  }.#ifdef SQLITE_
ca0d0 44 45 42 55 47 0a 73 74 61 74 69 63 20 69 6e 74  DEBUG.static int
ca0e0 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28 76   unixMutexHeld(v
ca0f0 6f 69 64 29 20 7b 0a 20 20 72 65 74 75 72 6e 20  oid) {.  return 
ca100 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
ca110 6c 64 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41  ld(sqlite3MutexA
ca120 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
ca130 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
ca140 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69  );.}.#endif...#i
ca150 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
ca160 47 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66  G./*.** Helper f
ca170 75 6e 63 74 69 6f 6e 20 66 6f 72 20 70 72 69 6e  unction for prin
ca180 74 69 6e 67 20 6f 75 74 20 74 72 61 63 65 20 69  ting out trace i
ca190 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20  nformation from 
ca1a0 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20 62 69 6e  debugging.** bin
ca1b0 61 72 69 65 73 2e 20 54 68 69 73 20 72 65 74 75  aries. This retu
ca1c0 72 6e 73 20 74 68 65 20 73 74 72 69 6e 67 20 72  rns the string r
ca1d0 65 70 72 65 73 65 74 61 74 69 6f 6e 20 6f 66 20  epresetation of 
ca1e0 74 68 65 20 73 75 70 70 6c 69 65 64 0a 2a 2a 20  the supplied.** 
ca1f0 69 6e 74 65 67 65 72 20 6c 6f 63 6b 2d 74 79 70  integer lock-typ
ca200 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  e..*/.static con
ca210 73 74 20 63 68 61 72 20 2a 6c 6f 63 6b 74 79 70  st char *locktyp
ca220 65 4e 61 6d 65 28 69 6e 74 20 6c 6f 63 6b 74 79  eName(int lockty
ca230 70 65 29 7b 0a 20 20 73 77 69 74 63 68 28 20 6c  pe){.  switch( l
ca240 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 63  ocktype ){.    c
ca250 61 73 65 20 4e 4f 5f 4c 4f 43 4b 3a 20 72 65 74  ase NO_LOCK: ret
ca260 75 72 6e 20 22 4e 4f 4e 45 22 3b 0a 20 20 20 20  urn "NONE";.    
ca270 63 61 73 65 20 53 48 41 52 45 44 5f 4c 4f 43 4b  case SHARED_LOCK
ca280 3a 20 72 65 74 75 72 6e 20 22 53 48 41 52 45 44  : return "SHARED
ca290 22 3b 0a 20 20 20 20 63 61 73 65 20 52 45 53 45  ";.    case RESE
ca2a0 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75 72  RVED_LOCK: retur
ca2b0 6e 20 22 52 45 53 45 52 56 45 44 22 3b 0a 20 20  n "RESERVED";.  
ca2c0 20 20 63 61 73 65 20 50 45 4e 44 49 4e 47 5f 4c    case PENDING_L
ca2d0 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 50 45 4e  OCK: return "PEN
ca2e0 44 49 4e 47 22 3b 0a 20 20 20 20 63 61 73 65 20  DING";.    case 
ca2f0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 20  EXCLUSIVE_LOCK: 
ca300 72 65 74 75 72 6e 20 22 45 58 43 4c 55 53 49 56  return "EXCLUSIV
ca310 45 22 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  E";.  }.  return
ca320 20 22 45 52 52 4f 52 22 3b 0a 7d 0a 23 65 6e 64   "ERROR";.}.#end
ca330 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
ca340 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a 2f 2a 0a  E_LOCK_TRACE./*.
ca350 2a 2a 20 50 72 69 6e 74 20 6f 75 74 20 69 6e 66  ** Print out inf
ca360 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61  ormation about a
ca370 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61  ll locking opera
ca380 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  tions..**.** Thi
ca390 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
ca3a0 64 20 66 6f 72 20 74 72 6f 75 62 6c 65 73 68 6f  d for troublesho
ca3b0 6f 74 69 6e 67 20 6c 6f 63 6b 73 20 6f 6e 20 6d  oting locks on m
ca3c0 75 6c 74 69 74 68 72 65 61 64 65 64 0a 2a 2a 20  ultithreaded.** 
ca3d0 70 6c 61 74 66 6f 72 6d 73 2e 20 20 45 6e 61 62  platforms.  Enab
ca3e0 6c 65 20 62 79 20 63 6f 6d 70 69 6c 69 6e 67 20  le by compiling 
ca3f0 77 69 74 68 20 74 68 65 20 2d 44 53 51 4c 49 54  with the -DSQLIT
ca400 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a 2a 2a 20  E_LOCK_TRACE.** 
ca410 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 6f 70 74  command-line opt
ca420 69 6f 6e 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69  ion on the compi
ca430 6c 65 72 2e 20 20 54 68 69 73 20 63 6f 64 65 20  ler.  This code 
ca440 69 73 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 74  is normally.** t
ca450 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2f 0a 73 74  urned off..*/.st
ca460 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 54 72 61  atic int lockTra
ca470 63 65 28 69 6e 74 20 66 64 2c 20 69 6e 74 20 6f  ce(int fd, int o
ca480 70 2c 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20  p, struct flock 
ca490 2a 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4f 70  *p){.  char *zOp
ca4a0 4e 61 6d 65 2c 20 2a 7a 54 79 70 65 3b 0a 20 20  Name, *zType;.  
ca4b0 69 6e 74 20 73 3b 0a 20 20 69 6e 74 20 73 61 76  int s;.  int sav
ca4c0 65 64 45 72 72 6e 6f 3b 0a 20 20 69 66 28 20 6f  edErrno;.  if( o
ca4d0 70 3d 3d 46 5f 47 45 54 4c 4b 20 29 7b 0a 20 20  p==F_GETLK ){.  
ca4e0 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 47 45 54    zOpName = "GET
ca4f0 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  LK";.  }else if(
ca500 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 29 7b 0a   op==F_SETLK ){.
ca510 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 53      zOpName = "S
ca520 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ETLK";.  }else{.
ca530 20 20 20 20 73 20 3d 20 66 63 6e 74 6c 28 66 64      s = fcntl(fd
ca540 2c 20 6f 70 2c 20 70 29 3b 0a 20 20 20 20 73 71  , op, p);.    sq
ca550 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
ca560 28 22 66 63 6e 74 6c 20 75 6e 6b 6e 6f 77 6e 20  ("fcntl unknown 
ca570 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 66 64 2c  %d %d %d\n", fd,
ca580 20 6f 70 2c 20 73 29 3b 0a 20 20 20 20 72 65 74   op, s);.    ret
ca590 75 72 6e 20 73 3b 0a 20 20 7d 0a 20 20 69 66 28  urn s;.  }.  if(
ca5a0 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44   p->l_type==F_RD
ca5b0 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70 65  LCK ){.    zType
ca5c0 20 3d 20 22 52 44 4c 43 4b 22 3b 0a 20 20 7d 65   = "RDLCK";.  }e
ca5d0 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70  lse if( p->l_typ
ca5e0 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a 20 20  e==F_WRLCK ){.  
ca5f0 20 20 7a 54 79 70 65 20 3d 20 22 57 52 4c 43 4b    zType = "WRLCK
ca600 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  ";.  }else if( p
ca610 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43  ->l_type==F_UNLC
ca620 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d  K ){.    zType =
ca630 20 22 55 4e 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73   "UNLCK";.  }els
ca640 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 30  e{.    assert( 0
ca650 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74   );.  }.  assert
ca660 28 20 70 2d 3e 6c 5f 77 68 65 6e 63 65 3d 3d 53  ( p->l_whence==S
ca670 45 45 4b 5f 53 45 54 20 29 3b 0a 20 20 73 20 3d  EEK_SET );.  s =
ca680 20 66 63 6e 74 6c 28 66 64 2c 20 6f 70 2c 20 70   fcntl(fd, op, p
ca690 29 3b 0a 20 20 73 61 76 65 64 45 72 72 6e 6f 20  );.  savedErrno 
ca6a0 3d 20 65 72 72 6e 6f 3b 0a 20 20 73 71 6c 69 74  = errno;.  sqlit
ca6b0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 66  e3DebugPrintf("f
ca6c0 63 6e 74 6c 20 25 64 20 25 64 20 25 73 20 25 73  cntl %d %d %s %s
ca6d0 20 25 64 20 25 64 20 25 64 20 25 64 5c 6e 22 2c   %d %d %d %d\n",
ca6e0 0a 20 20 20 20 20 74 68 72 65 61 64 69 64 2c 20  .     threadid, 
ca6f0 66 64 2c 20 7a 4f 70 4e 61 6d 65 2c 20 7a 54 79  fd, zOpName, zTy
ca700 70 65 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f 73 74  pe, (int)p->l_st
ca710 61 72 74 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f 6c  art, (int)p->l_l
ca720 65 6e 2c 0a 20 20 20 20 20 28 69 6e 74 29 70 2d  en,.     (int)p-
ca730 3e 6c 5f 70 69 64 2c 20 73 29 3b 0a 20 20 69 66  >l_pid, s);.  if
ca740 28 20 73 3d 3d 28 2d 31 29 20 26 26 20 6f 70 3d  ( s==(-1) && op=
ca750 3d 46 5f 53 45 54 4c 4b 20 26 26 20 28 70 2d 3e  =F_SETLK && (p->
ca760 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20  l_type==F_RDLCK 
ca770 7c 7c 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f  || p->l_type==F_
ca780 57 52 4c 43 4b 29 20 29 7b 0a 20 20 20 20 73 74  WRLCK) ){.    st
ca790 72 75 63 74 20 66 6c 6f 63 6b 20 6c 32 3b 0a 20  ruct flock l2;. 
ca7a0 20 20 20 6c 32 20 3d 20 2a 70 3b 0a 20 20 20 20     l2 = *p;.    
ca7b0 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 4c  fcntl(fd, F_GETL
ca7c0 4b 2c 20 26 6c 32 29 3b 0a 20 20 20 20 69 66 28  K, &l2);.    if(
ca7d0 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44   l2.l_type==F_RD
ca7e0 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 79  LCK ){.      zTy
ca7f0 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b 0a 20 20  pe = "RDLCK";.  
ca800 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c    }else if( l2.l
ca810 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29  _type==F_WRLCK )
ca820 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20  {.      zType = 
ca830 22 57 52 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c  "WRLCK";.    }el
ca840 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65  se if( l2.l_type
ca850 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20  ==F_UNLCK ){.   
ca860 20 20 20 7a 54 79 70 65 20 3d 20 22 55 4e 4c 43     zType = "UNLC
ca870 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  K";.    }else{. 
ca880 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29       assert( 0 )
ca890 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
ca8a0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
ca8b0 66 63 6e 74 6c 2d 66 61 69 6c 75 72 65 2d 72 65  fcntl-failure-re
ca8c0 61 73 6f 6e 3a 20 25 73 20 25 64 20 25 64 20 25  ason: %s %d %d %
ca8d0 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 7a 54 79  d\n",.       zTy
ca8e0 70 65 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 73 74  pe, (int)l2.l_st
ca8f0 61 72 74 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 6c  art, (int)l2.l_l
ca900 65 6e 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f 70 69  en, (int)l2.l_pi
ca910 64 29 3b 0a 20 20 7d 0a 20 20 65 72 72 6e 6f 20  d);.  }.  errno 
ca920 3d 20 73 61 76 65 64 45 72 72 6e 6f 3b 0a 20 20  = savedErrno;.  
ca930 72 65 74 75 72 6e 20 73 3b 0a 7d 0a 23 64 65 66  return s;.}.#def
ca940 69 6e 65 20 66 63 6e 74 6c 20 6c 6f 63 6b 54 72  ine fcntl lockTr
ca950 61 63 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ace.#endif /* SQ
ca960 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 20  LITE_LOCK_TRACE 
ca970 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  */..../*.** This
ca980 20 72 6f 75 74 69 6e 65 20 74 72 61 6e 73 6c 61   routine transla
ca990 74 65 73 20 61 20 73 74 61 6e 64 61 72 64 20 50  tes a standard P
ca9a0 4f 53 49 58 20 65 72 72 6e 6f 20 63 6f 64 65 20  OSIX errno code 
ca9b0 69 6e 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a 2a  into something.*
ca9c0 2a 20 75 73 65 66 75 6c 20 74 6f 20 74 68 65 20  * useful to the 
ca9d0 63 6c 69 65 6e 74 73 20 6f 66 20 74 68 65 20 73  clients of the s
ca9e0 71 6c 69 74 65 33 20 66 75 6e 63 74 69 6f 6e 73  qlite3 functions
ca9f0 2e 20 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c  .  Specifically,
caa00 20 69 74 20 69 73 0a 2a 2a 20 69 6e 74 65 6e 64   it is.** intend
caa10 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74 65 20  ed to translate 
caa20 61 20 76 61 72 69 65 74 79 20 6f 66 20 22 74 72  a variety of "tr
caa30 79 20 61 67 61 69 6e 22 20 65 72 72 6f 72 73 20  y again" errors 
caa40 69 6e 74 6f 20 53 51 4c 49 54 45 5f 42 55 53 59  into SQLITE_BUSY
caa50 0a 2a 2a 20 61 6e 64 20 61 20 76 61 72 69 65 74  .** and a variet
caa60 79 20 6f 66 20 22 70 6c 65 61 73 65 20 63 6c 6f  y of "please clo
caa70 73 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  se the file desc
caa80 72 69 70 74 6f 72 20 4e 4f 57 22 20 65 72 72 6f  riptor NOW" erro
caa90 72 73 20 69 6e 74 6f 20 0a 2a 2a 20 53 51 4c 49  rs into .** SQLI
caaa0 54 45 5f 49 4f 45 52 52 0a 2a 2a 20 0a 2a 2a 20  TE_IOERR.** .** 
caab0 45 72 72 6f 72 73 20 64 75 72 69 6e 67 20 69 6e  Errors during in
caac0 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20  itialization of 
caad0 6c 6f 63 6b 73 2c 20 6f 72 20 66 69 6c 65 20 73  locks, or file s
caae0 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 66 6f  ystem support fo
caaf0 72 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 73 68 6f 75  r locks,.** shou
cab00 6c 64 20 68 61 6e 64 6c 65 20 45 4e 4f 4c 43 4b  ld handle ENOLCK
cab10 2c 20 45 4e 4f 54 53 55 50 2c 20 45 4f 50 4e 4f  , ENOTSUP, EOPNO
cab20 54 53 55 50 50 20 73 65 70 61 72 61 74 65 6c 79  TSUPP separately
cab30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
cab40 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50  sqliteErrorFromP
cab50 6f 73 69 78 45 72 72 6f 72 28 69 6e 74 20 70 6f  osixError(int po
cab60 73 69 78 45 72 72 6f 72 2c 20 69 6e 74 20 73 71  sixError, int sq
cab70 6c 69 74 65 49 4f 45 72 72 29 20 7b 0a 20 20 73  liteIOErr) {.  s
cab80 77 69 74 63 68 20 28 70 6f 73 69 78 45 72 72 6f  witch (posixErro
cab90 72 29 20 7b 0a 20 20 63 61 73 65 20 30 3a 20 0a  r) {.  case 0: .
caba0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
cabb0 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 20 63 61 73  E_OK;.    .  cas
cabc0 65 20 45 41 47 41 49 4e 3a 0a 20 20 63 61 73 65  e EAGAIN:.  case
cabd0 20 45 54 49 4d 45 44 4f 55 54 3a 0a 20 20 63 61   ETIMEDOUT:.  ca
cabe0 73 65 20 45 42 55 53 59 3a 0a 20 20 63 61 73 65  se EBUSY:.  case
cabf0 20 45 49 4e 54 52 3a 0a 20 20 63 61 73 65 20 45   EINTR:.  case E
cac00 4e 4f 4c 43 4b 3a 20 20 0a 20 20 20 20 2f 2a 20  NOLCK:  .    /* 
cac10 72 61 6e 64 6f 6d 20 4e 46 53 20 72 65 74 72 79  random NFS retry
cac20 20 65 72 72 6f 72 2c 20 75 6e 6c 65 73 73 20 64   error, unless d
cac30 75 72 69 6e 67 20 66 69 6c 65 20 73 79 73 74 65  uring file syste
cac40 6d 20 73 75 70 70 6f 72 74 20 0a 20 20 20 20 20  m support .     
cac50 2a 20 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c  * introspection,
cac60 20 69 6e 20 77 68 69 63 68 20 69 74 20 61 63 74   in which it act
cac70 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 68 61 74  ually means what
cac80 20 69 74 20 73 61 79 73 20 2a 2f 0a 20 20 20 20   it says */.    
cac90 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
caca0 53 59 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 20  SY;.    .  case 
cacb0 45 41 43 43 45 53 3a 20 0a 20 20 20 20 2f 2a 20  EACCES: .    /* 
cacc0 45 41 43 43 45 53 20 69 73 20 6c 69 6b 65 20 45  EACCES is like E
cacd0 41 47 41 49 4e 20 64 75 72 69 6e 67 20 6c 6f 63  AGAIN during loc
cace0 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 2c  king operations,
cacf0 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 74 68   but not any oth
cad00 65 72 20 74 69 6d 65 2a 2f 0a 20 20 20 20 69 66  er time*/.    if
cad10 28 20 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d  ( (sqliteIOErr =
cad20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c  = SQLITE_IOERR_L
cad30 4f 43 4b 29 20 7c 7c 20 0a 09 28 73 71 6c 69 74  OCK) || ..(sqlit
cad40 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45  eIOErr == SQLITE
cad50 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 20 7c  _IOERR_UNLOCK) |
cad60 7c 20 0a 09 28 73 71 6c 69 74 65 49 4f 45 72 72  | ..(sqliteIOErr
cad70 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52   == SQLITE_IOERR
cad80 5f 52 44 4c 4f 43 4b 29 20 7c 7c 0a 09 28 73 71  _RDLOCK) ||..(sq
cad90 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c  liteIOErr == SQL
cada0 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52  ITE_IOERR_CHECKR
cadb0 45 53 45 52 56 45 44 4c 4f 43 4b 29 20 29 7b 0a  ESERVEDLOCK) ){.
cadc0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
cadd0 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a  ITE_BUSY;.    }.
cade0 20 20 20 20 2f 2a 20 65 6c 73 65 20 66 61 6c 6c      /* else fall
cadf0 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20 63 61   through */.  ca
cae00 73 65 20 45 50 45 52 4d 3a 20 0a 20 20 20 20 72  se EPERM: .    r
cae10 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52  eturn SQLITE_PER
cae20 4d 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 20 45  M;.    .  case E
cae30 44 45 41 44 4c 4b 3a 0a 20 20 20 20 72 65 74 75  DEADLK:.    retu
cae40 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
cae50 42 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 0a 23 69  BLOCKED;.    .#i
cae60 66 20 45 4f 50 4e 4f 54 53 55 50 50 21 3d 45 4e  f EOPNOTSUPP!=EN
cae70 4f 54 53 55 50 0a 20 20 63 61 73 65 20 45 4f 50  OTSUP.  case EOP
cae80 4e 4f 54 53 55 50 50 3a 20 0a 20 20 20 20 2f 2a  NOTSUPP: .    /*
cae90 20 73 6f 6d 65 74 68 69 6e 67 20 77 65 6e 74 20   something went 
caea0 74 65 72 72 69 62 6c 79 20 61 77 72 79 2c 20 75  terribly awry, u
caeb0 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c  nless during fil
caec0 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74  e system support
caed0 20 0a 20 20 20 20 20 2a 20 69 6e 74 72 6f 73 70   .     * introsp
caee0 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68  ection, in which
caef0 20 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61   it actually mea
caf00 6e 73 20 77 68 61 74 20 69 74 20 73 61 79 73 20  ns what it says 
caf10 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  */.#endif.#ifdef
caf20 20 45 4e 4f 54 53 55 50 0a 20 20 63 61 73 65 20   ENOTSUP.  case 
caf30 45 4e 4f 54 53 55 50 3a 20 0a 20 20 20 20 2f 2a  ENOTSUP: .    /*
caf40 20 69 6e 76 61 6c 69 64 20 66 64 2c 20 75 6e 6c   invalid fd, unl
caf50 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20  ess during file 
caf60 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20 69  system support i
caf70 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e  ntrospection, in
caf80 20 77 68 69 63 68 20 0a 20 20 20 20 20 2a 20 69   which .     * i
caf90 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73  t actually means
cafa0 20 77 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f   what it says */
cafb0 0a 23 65 6e 64 69 66 0a 20 20 63 61 73 65 20 45  .#endif.  case E
cafc0 49 4f 3a 0a 20 20 63 61 73 65 20 45 42 41 44 46  IO:.  case EBADF
cafd0 3a 0a 20 20 63 61 73 65 20 45 49 4e 56 41 4c 3a  :.  case EINVAL:
cafe0 0a 20 20 63 61 73 65 20 45 4e 4f 54 43 4f 4e 4e  .  case ENOTCONN
caff0 3a 0a 20 20 63 61 73 65 20 45 4e 4f 44 45 56 3a  :.  case ENODEV:
cb000 0a 20 20 63 61 73 65 20 45 4e 58 49 4f 3a 0a 20  .  case ENXIO:. 
cb010 20 63 61 73 65 20 45 4e 4f 45 4e 54 3a 0a 20 20   case ENOENT:.  
cb020 63 61 73 65 20 45 53 54 41 4c 45 3a 0a 20 20 63  case ESTALE:.  c
cb030 61 73 65 20 45 4e 4f 53 59 53 3a 0a 20 20 20 20  ase ENOSYS:.    
cb040 2f 2a 20 74 68 65 73 65 20 73 68 6f 75 6c 64 20  /* these should 
cb050 66 6f 72 63 65 20 74 68 65 20 63 6c 69 65 6e 74  force the client
cb060 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20 66 69   to close the fi
cb070 6c 65 20 61 6e 64 20 72 65 63 6f 6e 6e 65 63 74  le and reconnect
cb080 20 2a 2f 0a 20 20 20 20 0a 20 20 64 65 66 61 75   */.    .  defau
cb090 6c 74 3a 20 0a 20 20 20 20 72 65 74 75 72 6e 20  lt: .    return 
cb0a0 73 71 6c 69 74 65 49 4f 45 72 72 3b 0a 20 20 7d  sqliteIOErr;.  }
cb0b0 0a 7d 0a 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}..../*********
cb0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb100 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *****.**********
cb110 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 55  ******** Begin U
cb120 6e 69 71 75 65 20 46 69 6c 65 20 49 44 20 55 74  nique File ID Ut
cb130 69 6c 69 74 79 20 55 73 65 64 20 42 79 20 56 78  ility Used By Vx
cb140 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Works **********
cb150 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 6e 20 6d  *****.**.** On m
cb160 6f 73 74 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  ost versions of 
cb170 75 6e 69 78 2c 20 77 65 20 63 61 6e 20 67 65 74  unix, we can get
cb180 20 61 20 75 6e 69 71 75 65 20 49 44 20 66 6f 72   a unique ID for
cb190 20 61 20 66 69 6c 65 20 62 79 20 63 6f 6e 63 61   a file by conca
cb1a0 74 65 6e 61 74 69 6e 67 0a 2a 2a 20 74 68 65 20  tenating.** the 
cb1b0 64 65 76 69 63 65 20 6e 75 6d 62 65 72 20 61 6e  device number an
cb1c0 64 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62  d the inode numb
cb1d0 65 72 2e 20 20 42 75 74 20 74 68 69 73 20 64 6f  er.  But this do
cb1e0 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 56  es not work on V
cb1f0 78 57 6f 72 6b 73 2e 0a 2a 2a 20 4f 6e 20 56 78  xWorks..** On Vx
cb200 57 6f 72 6b 73 2c 20 61 20 75 6e 69 71 75 65 20  Works, a unique 
cb210 66 69 6c 65 20 69 64 20 6d 75 73 74 20 62 65 20  file id must be 
cb220 62 61 73 65 64 20 6f 6e 20 74 68 65 20 63 61 6e  based on the can
cb230 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 2e  onical filename.
cb240 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72  .**.** A pointer
cb250 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   to an instance 
cb260 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
cb270 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20 62   structure can b
cb280 65 20 75 73 65 64 20 61 73 20 61 0a 2a 2a 20 75  e used as a.** u
cb290 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20 69 6e  nique file ID in
cb2a0 20 56 78 57 6f 72 6b 73 2e 20 20 45 61 63 68 20   VxWorks.  Each 
cb2b0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
cb2c0 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
cb2d0 69 6e 73 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66  ins.** a copy of
cb2e0 20 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 66   the canonical f
cb2f0 69 6c 65 6e 61 6d 65 2e 20 20 54 68 65 72 65 20  ilename.  There 
cb300 69 73 20 61 6c 73 6f 20 61 20 72 65 66 65 72 65  is also a refere
cb310 6e 63 65 20 63 6f 75 6e 74 2e 20 20 0a 2a 2a 20  nce count.  .** 
cb320 54 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73  The structure is
cb330 20 72 65 63 6c 61 69 6d 65 64 20 77 68 65 6e 20   reclaimed when 
cb340 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 6f  the number of po
cb350 69 6e 74 65 72 73 20 74 6f 20 69 74 20 64 72 6f  inters to it dro
cb360 70 73 20 74 6f 0a 2a 2a 20 7a 65 72 6f 2e 0a 2a  ps to.** zero..*
cb370 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 6e  *.** There are n
cb380 65 76 65 72 20 76 65 72 79 20 6d 61 6e 79 20 66  ever very many f
cb390 69 6c 65 73 20 6f 70 65 6e 20 61 74 20 6f 6e 65  iles open at one
cb3a0 20 74 69 6d 65 20 61 6e 64 20 6c 6f 6f 6b 75 70   time and lookup
cb3b0 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 61 20 70  s are not.** a p
cb3c0 65 72 66 6f 72 6d 61 6e 63 65 2d 63 72 69 74 69  erformance-criti
cb3d0 63 61 6c 20 70 61 74 68 2c 20 73 6f 20 69 74 20  cal path, so it 
cb3e0 69 73 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f  is sufficient to
cb3f0 20 70 75 74 20 74 68 65 73 65 0a 2a 2a 20 73 74   put these.** st
cb400 72 75 63 74 75 72 65 73 20 6f 6e 20 61 20 6c 69  ructures on a li
cb410 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74  nked list..*/.st
cb420 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65  ruct vxworksFile
cb430 49 64 20 7b 0a 20 20 73 74 72 75 63 74 20 76 78  Id {.  struct vx
cb440 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 4e 65  worksFileId *pNe
cb450 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20  xt;  /* Next in 
cb460 61 20 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 61  a list of them a
cb470 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66  ll */.  int nRef
cb480 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
cb490 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
cb4a0 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  of references to
cb4b0 20 74 68 69 73 20 6f 6e 65 20 2a 2f 0a 20 20 69   this one */.  i
cb4c0 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20  nt nName;       
cb4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
cb4e0 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 7a 43  Length of the zC
cb4f0 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 5b 5d 20 73  anonicalName[] s
cb500 74 72 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20  tring */.  char 
cb510 2a 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 3b  *zCanonicalName;
cb520 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6e 6f           /* Cano
cb530 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65 20 2a  nical filename *
cb540 2f 0a 7d 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 57  /.};..#if OS_VXW
cb550 4f 52 4b 53 0a 2f 2a 20 0a 2a 2a 20 41 6c 6c 20  ORKS./* .** All 
cb560 75 6e 69 71 75 65 20 66 69 6c 65 6e 61 6d 65 73  unique filenames
cb570 20 61 72 65 20 68 65 6c 64 20 6f 6e 20 61 20 6c   are held on a l
cb580 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65  inked list heade
cb590 64 20 62 79 20 74 68 69 73 0a 2a 2a 20 76 61 72  d by this.** var
cb5a0 69 61 62 6c 65 3a 0a 2a 2f 0a 73 74 61 74 69 63  iable:.*/.static
cb5b0 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46   struct vxworksF
cb5c0 69 6c 65 49 64 20 2a 76 78 77 6f 72 6b 73 46 69  ileId *vxworksFi
cb5d0 6c 65 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a  leList = 0;../*.
cb5e0 2a 2a 20 53 69 6d 70 6c 69 66 79 20 61 20 66 69  ** Simplify a fi
cb5f0 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 69 74 73 20  lename into its 
cb600 63 61 6e 6f 6e 69 63 61 6c 20 66 6f 72 6d 0a 2a  canonical form.*
cb610 2a 20 62 79 20 6d 61 6b 69 6e 67 20 74 68 65 20  * by making the 
cb620 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65  following change
cb630 73 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 72 65 6d 6f  s:.**.**  * remo
cb640 76 69 6e 67 20 61 6e 79 20 74 72 61 69 6c 69 6e  ving any trailin
cb650 67 20 61 6e 64 20 64 75 70 6c 69 63 61 74 65 20  g and duplicate 
cb660 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 72 74 20  /.**  * convert 
cb670 2f 2e 2f 20 69 6e 74 6f 20 6a 75 73 74 20 2f 0a  /./ into just /.
cb680 2a 2a 20 20 2a 20 63 6f 6e 76 65 72 74 20 2f 41  **  * convert /A
cb690 2f 2e 2e 2f 20 77 68 65 72 65 20 41 20 69 73 20  /../ where A is 
cb6a0 61 6e 79 20 73 69 6d 70 6c 65 20 6e 61 6d 65 20  any simple name 
cb6b0 69 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a 0a 2a  into just /.**.*
cb6c0 2a 20 43 68 61 6e 67 65 73 20 61 72 65 20 6d 61  * Changes are ma
cb6d0 64 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20 52 65  de in-place.  Re
cb6e0 74 75 72 6e 20 74 68 65 20 6e 65 77 20 6e 61 6d  turn the new nam
cb6f0 65 20 6c 65 6e 67 74 68 2e 0a 2a 2a 0a 2a 2a 20  e length..**.** 
cb700 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c  The original fil
cb710 65 6e 61 6d 65 20 69 73 20 69 6e 20 7a 5b 30 2e  ename is in z[0.
cb720 2e 6e 2d 31 5d 2e 20 20 52 65 74 75 72 6e 20 74  .n-1].  Return t
cb730 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20  he number of.** 
cb740 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 74 68  characters in th
cb750 65 20 73 69 6d 70 6c 69 66 69 65 64 20 6e 61 6d  e simplified nam
cb760 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
cb770 20 76 78 77 6f 72 6b 73 53 69 6d 70 6c 69 66 79   vxworksSimplify
cb780 4e 61 6d 65 28 63 68 61 72 20 2a 7a 2c 20 69 6e  Name(char *z, in
cb790 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a  t n){.  int i, j
cb7a0 3b 0a 20 20 77 68 69 6c 65 28 20 6e 3e 31 20 26  ;.  while( n>1 &
cb7b0 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 2f 27 20 29 7b  & z[n-1]=='/' ){
cb7c0 20 6e 2d 2d 3b 20 7d 0a 20 20 66 6f 72 28 69 3d   n--; }.  for(i=
cb7d0 6a 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a  j=0; i<n; i++){.
cb7e0 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2f      if( z[i]=='/
cb7f0 27 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a  ' ){.      if( z
cb800 5b 69 2b 31 5d 3d 3d 27 2f 27 20 29 20 63 6f 6e  [i+1]=='/' ) con
cb810 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66 28  tinue;.      if(
cb820 20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 20   z[i+1]=='.' && 
cb830 69 2b 32 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d  i+2<n && z[i+2]=
cb840 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='/' ){.        
cb850 69 20 2b 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  i += 1;.        
cb860 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
cb870 7d 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b  }.      if( z[i+
cb880 31 5d 3d 3d 27 2e 27 20 26 26 20 69 2b 33 3c 6e  1]=='.' && i+3<n
cb890 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27 2e 27 20   && z[i+2]=='.' 
cb8a0 26 26 20 7a 5b 69 2b 33 5d 3d 3d 27 2f 27 20 29  && z[i+3]=='/' )
cb8b0 7b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  {.        while(
cb8c0 20 6a 3e 30 20 26 26 20 7a 5b 6a 2d 31 5d 21 3d   j>0 && z[j-1]!=
cb8d0 27 2f 27 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20  '/' ){ j--; }.  
cb8e0 20 20 20 20 20 20 69 66 28 20 6a 3e 30 20 29 7b        if( j>0 ){
cb8f0 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20   j--; }.        
cb900 69 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 20  i += 2;.        
cb910 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
cb920 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 5b 6a 2b  }.    }.    z[j+
cb930 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 7d 0a 20  +] = z[i];.  }. 
cb940 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 72 65 74   z[j] = 0;.  ret
cb950 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn j;.}../*.** 
cb960 46 69 6e 64 20 61 20 75 6e 69 71 75 65 20 66 69  Find a unique fi
cb970 6c 65 20 49 44 20 66 6f 72 20 74 68 65 20 67 69  le ID for the gi
cb980 76 65 6e 20 61 62 73 6f 6c 75 74 65 20 70 61 74  ven absolute pat
cb990 68 6e 61 6d 65 2e 20 20 52 65 74 75 72 6e 0a 2a  hname.  Return.*
cb9a0 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  * a pointer to t
cb9b0 68 65 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64  he vxworksFileId
cb9c0 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20 70   object.  This p
cb9d0 6f 69 6e 74 65 72 20 69 73 20 74 68 65 20 75 6e  ointer is the un
cb9e0 69 71 75 65 0a 2a 2a 20 66 69 6c 65 20 49 44 2e  ique.** file ID.
cb9f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 52 65 66 20  .**.** The nRef 
cba00 66 69 65 6c 64 20 6f 66 20 74 68 65 20 76 78 77  field of the vxw
cba10 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63  orksFileId objec
cba20 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  t is incremented
cba30 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6f   before.** the o
cba40 62 6a 65 63 74 20 69 73 20 72 65 74 75 72 6e 65  bject is returne
cba50 64 2e 20 20 41 20 6e 65 77 20 76 78 77 6f 72 6b  d.  A new vxwork
cba60 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 20 69  sFileId object i
cba70 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e 64  s created.** and
cba80 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 67 6c   added to the gl
cba90 6f 62 61 6c 20 6c 69 73 74 20 69 66 20 6e 65 63  obal list if nec
cbaa0 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66  essary..**.** If
cbab0 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
cbac0 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  tion error occur
cbad0 73 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a  s, return NULL..
cbae0 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74  */.static struct
cbaf0 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a   vxworksFileId *
cbb00 76 78 77 6f 72 6b 73 46 69 6e 64 46 69 6c 65 49  vxworksFindFileI
cbb10 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41  d(const char *zA
cbb20 62 73 6f 6c 75 74 65 4e 61 6d 65 29 7b 0a 20 20  bsoluteName){.  
cbb30 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69  struct vxworksFi
cbb40 6c 65 49 64 20 2a 70 4e 65 77 3b 20 20 20 20 20  leId *pNew;     
cbb50 20 20 20 20 2f 2a 20 73 65 61 72 63 68 20 6b 65      /* search ke
cbb60 79 20 61 6e 64 20 6e 65 77 20 66 69 6c 65 20 49  y and new file I
cbb70 44 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 76 78  D */.  struct vx
cbb80 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 43 61  worksFileId *pCa
cbb90 6e 64 69 64 61 74 65 3b 20 20 20 2f 2a 20 46 6f  ndidate;   /* Fo
cbba0 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65  r looping over e
cbbb0 78 69 73 74 69 6e 67 20 66 69 6c 65 20 49 44 73  xisting file IDs
cbbc0 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20   */.  int n;    
cbbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cbbe0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
cbbf0 67 74 68 20 6f 66 20 7a 41 62 73 6f 6c 75 74 65  gth of zAbsolute
cbc00 4e 61 6d 65 20 73 74 72 69 6e 67 20 2a 2f 0a 0a  Name string */..
cbc10 20 20 61 73 73 65 72 74 28 20 7a 41 62 73 6f 6c    assert( zAbsol
cbc20 75 74 65 4e 61 6d 65 5b 30 5d 3d 3d 27 2f 27 20  uteName[0]=='/' 
cbc30 29 3b 0a 20 20 6e 20 3d 20 28 69 6e 74 29 73 74  );.  n = (int)st
cbc40 72 6c 65 6e 28 7a 41 62 73 6f 6c 75 74 65 4e 61  rlen(zAbsoluteNa
cbc50 6d 65 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71  me);.  pNew = sq
cbc60 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69  lite3_malloc( si
cbc70 7a 65 6f 66 28 2a 70 4e 65 77 29 20 2b 20 28 6e  zeof(*pNew) + (n
cbc80 2b 31 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65  +1) );.  if( pNe
cbc90 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  w==0 ) return 0;
cbca0 0a 20 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69  .  pNew->zCanoni
cbcb0 63 61 6c 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a  calName = (char*
cbcc0 29 26 70 4e 65 77 5b 31 5d 3b 0a 20 20 6d 65 6d  )&pNew[1];.  mem
cbcd0 63 70 79 28 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e  cpy(pNew->zCanon
cbce0 69 63 61 6c 4e 61 6d 65 2c 20 7a 41 62 73 6f 6c  icalName, zAbsol
cbcf0 75 74 65 4e 61 6d 65 2c 20 6e 2b 31 29 3b 0a 20  uteName, n+1);. 
cbd00 20 6e 20 3d 20 76 78 77 6f 72 6b 73 53 69 6d 70   n = vxworksSimp
cbd10 6c 69 66 79 4e 61 6d 65 28 70 4e 65 77 2d 3e 7a  lifyName(pNew->z
cbd20 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e  CanonicalName, n
cbd30 29 3b 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 20  );..  /* Search 
cbd40 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e 67 20  for an existing 
cbd50 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68  entry that match
cbd60 69 6e 67 20 74 68 65 20 63 61 6e 6f 6e 69 63 61  ing the canonica
cbd70 6c 20 6e 61 6d 65 2e 0a 20 20 2a 2a 20 49 66 20  l name..  ** If 
cbd80 66 6f 75 6e 64 2c 20 69 6e 63 72 65 6d 65 6e 74  found, increment
cbd90 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63   the reference c
cbda0 6f 75 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 20  ount and return 
cbdb0 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 20 20 2a  a pointer to.  *
cbdc0 2a 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 66  * the existing f
cbdd0 69 6c 65 20 49 44 2e 0a 20 20 2a 2f 0a 20 20 75  ile ID..  */.  u
cbde0 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  nixEnterMutex();
cbdf0 0a 20 20 66 6f 72 28 70 43 61 6e 64 69 64 61 74  .  for(pCandidat
cbe00 65 3d 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73  e=vxworksFileLis
cbe10 74 3b 20 70 43 61 6e 64 69 64 61 74 65 3b 20 70  t; pCandidate; p
cbe20 43 61 6e 64 69 64 61 74 65 3d 70 43 61 6e 64 69  Candidate=pCandi
cbe30 64 61 74 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  date->pNext){.  
cbe40 20 20 69 66 28 20 70 43 61 6e 64 69 64 61 74 65    if( pCandidate
cbe50 2d 3e 6e 4e 61 6d 65 3d 3d 6e 20 0a 20 20 20 20  ->nName==n .    
cbe60 20 26 26 20 6d 65 6d 63 6d 70 28 70 43 61 6e 64   && memcmp(pCand
cbe70 69 64 61 74 65 2d 3e 7a 43 61 6e 6f 6e 69 63 61  idate->zCanonica
cbe80 6c 4e 61 6d 65 2c 20 70 4e 65 77 2d 3e 7a 43 61  lName, pNew->zCa
cbe90 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 3d  nonicalName, n)=
cbea0 3d 30 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  =0.    ){.      
cbeb0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e   sqlite3_free(pN
cbec0 65 77 29 3b 0a 20 20 20 20 20 20 20 70 43 61 6e  ew);.       pCan
cbed0 64 69 64 61 74 65 2d 3e 6e 52 65 66 2b 2b 3b 0a  didate->nRef++;.
cbee0 20 20 20 20 20 20 20 75 6e 69 78 4c 65 61 76 65         unixLeave
cbef0 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 20  Mutex();.       
cbf00 72 65 74 75 72 6e 20 70 43 61 6e 64 69 64 61 74  return pCandidat
cbf10 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  e;.    }.  }..  
cbf20 2f 2a 20 4e 6f 20 6d 61 74 63 68 20 77 61 73 20  /* No match was 
cbf30 66 6f 75 6e 64 2e 20 20 57 65 20 77 69 6c 6c 20  found.  We will 
cbf40 6d 61 6b 65 20 61 20 6e 65 77 20 66 69 6c 65 20  make a new file 
cbf50 49 44 20 2a 2f 0a 20 20 70 4e 65 77 2d 3e 6e 52  ID */.  pNew->nR
cbf60 65 66 20 3d 20 31 3b 0a 20 20 70 4e 65 77 2d 3e  ef = 1;.  pNew->
cbf70 6e 4e 61 6d 65 20 3d 20 6e 3b 0a 20 20 70 4e 65  nName = n;.  pNe
cbf80 77 2d 3e 70 4e 65 78 74 20 3d 20 76 78 77 6f 72  w->pNext = vxwor
cbf90 6b 73 46 69 6c 65 4c 69 73 74 3b 0a 20 20 76 78  ksFileList;.  vx
cbfa0 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 20 3d 20  worksFileList = 
cbfb0 70 4e 65 77 3b 0a 20 20 75 6e 69 78 4c 65 61 76  pNew;.  unixLeav
cbfc0 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75  eMutex();.  retu
cbfd0 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pNew;.}../*.*
cbfe0 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20  * Decrement the 
cbff0 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20  reference count 
cc000 6f 6e 20 61 20 76 78 77 6f 72 6b 73 46 69 6c 65  on a vxworksFile
cc010 49 64 20 6f 62 6a 65 63 74 2e 20 20 46 72 65 65  Id object.  Free
cc020 0a 2a 2a 20 74 68 65 20 6f 62 6a 65 63 74 20 77  .** the object w
cc030 68 65 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63  hen the referenc
cc040 65 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20  e count reaches 
cc050 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  zero..*/.static 
cc060 76 6f 69 64 20 76 78 77 6f 72 6b 73 52 65 6c 65  void vxworksRele
cc070 61 73 65 46 69 6c 65 49 64 28 73 74 72 75 63 74  aseFileId(struct
cc080 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a   vxworksFileId *
cc090 70 49 64 29 7b 0a 20 20 75 6e 69 78 45 6e 74 65  pId){.  unixEnte
cc0a0 72 4d 75 74 65 78 28 29 3b 0a 20 20 61 73 73 65  rMutex();.  asse
cc0b0 72 74 28 20 70 49 64 2d 3e 6e 52 65 66 3e 30 20  rt( pId->nRef>0 
cc0c0 29 3b 0a 20 20 70 49 64 2d 3e 6e 52 65 66 2d 2d  );.  pId->nRef--
cc0d0 3b 0a 20 20 69 66 28 20 70 49 64 2d 3e 6e 52 65  ;.  if( pId->nRe
cc0e0 66 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 72 75  f==0 ){.    stru
cc0f0 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64  ct vxworksFileId
cc100 20 2a 2a 70 70 3b 0a 20 20 20 20 66 6f 72 28 70   **pp;.    for(p
cc110 70 3d 26 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69  p=&vxworksFileLi
cc120 73 74 3b 20 2a 70 70 20 26 26 20 2a 70 70 21 3d  st; *pp && *pp!=
cc130 70 49 64 3b 20 70 70 20 3d 20 26 28 28 2a 70 70  pId; pp = &((*pp
cc140 29 2d 3e 70 4e 65 78 74 29 29 7b 7d 0a 20 20 20  )->pNext)){}.   
cc150 20 61 73 73 65 72 74 28 20 2a 70 70 3d 3d 70 49   assert( *pp==pI
cc160 64 20 29 3b 0a 20 20 20 20 2a 70 70 20 3d 20 70  d );.    *pp = p
cc170 49 64 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73  Id->pNext;.    s
cc180 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49 64 29  qlite3_free(pId)
cc190 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65 61 76  ;.  }.  unixLeav
cc1a0 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 23 65 6e 64  eMutex();.}.#end
cc1b0 69 66 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53  if /* OS_VXWORKS
cc1c0 20 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */./***********
cc1d0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 55 6e 69 71  **** End of Uniq
cc1e0 75 65 20 46 69 6c 65 20 49 44 20 55 74 69 6c 69  ue File ID Utili
cc1f0 74 79 20 55 73 65 64 20 42 79 20 56 78 57 6f 72  ty Used By VxWor
cc200 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ks *************
cc210 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***.************
cc220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc260 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/.../*********
cc270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc2b0 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *****.**********
cc2c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc2d0 2a 20 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79  * Posix Advisory
cc2e0 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a   Locking *******
cc2f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc300 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50 4f 53 49  *****.**.** POSI
cc310 58 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73  X advisory locks
cc320 20 61 72 65 20 62 72 6f 6b 65 6e 20 62 79 20 64   are broken by d
cc330 65 73 69 67 6e 2e 20 20 41 4e 53 49 20 53 54 44  esign.  ANSI STD
cc340 20 31 30 30 33 2e 31 20 28 31 39 39 36 29 0a 2a   1003.1 (1996).*
cc350 2a 20 73 65 63 74 69 6f 6e 20 36 2e 35 2e 32 2e  * section 6.5.2.
cc360 32 20 6c 69 6e 65 73 20 34 38 33 20 74 68 72 6f  2 lines 483 thro
cc370 75 67 68 20 34 39 30 20 73 70 65 63 69 66 79 20  ugh 490 specify 
cc380 74 68 61 74 20 77 68 65 6e 20 61 20 70 72 6f 63  that when a proc
cc390 65 73 73 0a 2a 2a 20 73 65 74 73 20 6f 72 20 63  ess.** sets or c
cc3a0 6c 65 61 72 73 20 61 20 6c 6f 63 6b 2c 20 74 68  lears a lock, th
cc3b0 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 76 65  at operation ove
cc3c0 72 72 69 64 65 73 20 61 6e 79 20 70 72 69 6f 72  rrides any prior
cc3d0 20 6c 6f 63 6b 73 20 73 65 74 0a 2a 2a 20 62 79   locks set.** by
cc3e0 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73   the same proces
cc3f0 73 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20  s.  It does not 
cc400 65 78 70 6c 69 63 69 74 6c 79 20 73 61 79 20 73  explicitly say s
cc410 6f 2c 20 62 75 74 20 74 68 69 73 20 69 6d 70 6c  o, but this impl
cc420 69 65 73 0a 2a 2a 20 74 68 61 74 20 69 74 20 6f  ies.** that it o
cc430 76 65 72 72 69 64 65 73 20 6c 6f 63 6b 73 20 73  verrides locks s
cc440 65 74 20 62 79 20 74 68 65 20 73 61 6d 65 20 70  et by the same p
cc450 72 6f 63 65 73 73 20 75 73 69 6e 67 20 61 20 64  rocess using a d
cc460 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65  ifferent.** file
cc470 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 43 6f   descriptor.  Co
cc480 6e 73 69 64 65 72 20 74 68 69 73 20 74 65 73 74  nsider this test
cc490 20 63 61 73 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   case:.**.**    
cc4a0 20 20 20 69 6e 74 20 66 64 31 20 3d 20 6f 70 65     int fd1 = ope
cc4b0 6e 28 22 2e 2f 66 69 6c 65 31 22 2c 20 4f 5f 52  n("./file1", O_R
cc4c0 44 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34  DWR|O_CREAT, 064
cc4d0 34 29 3b 0a 2a 2a 20 20 20 20 20 20 20 69 6e 74  4);.**       int
cc4e0 20 66 64 32 20 3d 20 6f 70 65 6e 28 22 2e 2f 66   fd2 = open("./f
cc4f0 69 6c 65 32 22 2c 20 4f 5f 52 44 57 52 7c 4f 5f  ile2", O_RDWR|O_
cc500 43 52 45 41 54 2c 20 30 36 34 34 29 3b 0a 2a 2a  CREAT, 0644);.**
cc510 0a 2a 2a 20 53 75 70 70 6f 73 65 20 2e 2f 66 69  .** Suppose ./fi
cc520 6c 65 31 20 61 6e 64 20 2e 2f 66 69 6c 65 32 20  le1 and ./file2 
cc530 61 72 65 20 72 65 61 6c 6c 79 20 74 68 65 20 73  are really the s
cc540 61 6d 65 20 66 69 6c 65 20 28 62 65 63 61 75 73  ame file (becaus
cc550 65 0a 2a 2a 20 6f 6e 65 20 69 73 20 61 20 68 61  e.** one is a ha
cc560 72 64 20 6f 72 20 73 79 6d 62 6f 6c 69 63 20 6c  rd or symbolic l
cc570 69 6e 6b 20 74 6f 20 74 68 65 20 6f 74 68 65 72  ink to the other
cc580 29 20 74 68 65 6e 20 69 66 20 79 6f 75 20 73 65  ) then if you se
cc590 74 0a 2a 2a 20 61 6e 20 65 78 63 6c 75 73 69 76  t.** an exclusiv
cc5a0 65 20 6c 6f 63 6b 20 6f 6e 20 66 64 31 2c 20 74  e lock on fd1, t
cc5b0 68 65 6e 20 74 72 79 20 74 6f 20 67 65 74 20 61  hen try to get a
cc5c0 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
cc5d0 0a 2a 2a 20 6f 6e 20 66 64 32 2c 20 69 74 20 77  .** on fd2, it w
cc5e0 6f 72 6b 73 2e 20 20 49 20 77 6f 75 6c 64 20 68  orks.  I would h
cc5f0 61 76 65 20 65 78 70 65 63 74 65 64 20 74 68 65  ave expected the
cc600 20 73 65 63 6f 6e 64 20 6c 6f 63 6b 20 74 6f 0a   second lock to.
cc610 2a 2a 20 66 61 69 6c 20 73 69 6e 63 65 20 74 68  ** fail since th
cc620 65 72 65 20 77 61 73 20 61 6c 72 65 61 64 79 20  ere was already 
cc630 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69  a lock on the fi
cc640 6c 65 20 64 75 65 20 74 6f 20 66 64 31 2e 0a 2a  le due to fd1..*
cc650 2a 20 42 75 74 20 6e 6f 74 20 73 6f 2e 20 20 53  * But not so.  S
cc660 69 6e 63 65 20 62 6f 74 68 20 6c 6f 63 6b 73 20  ince both locks 
cc670 63 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 73 61  came from the sa
cc680 6d 65 20 70 72 6f 63 65 73 73 2c 20 74 68 65 0a  me process, the.
cc690 2a 2a 20 73 65 63 6f 6e 64 20 6f 76 65 72 72 69  ** second overri
cc6a0 64 65 73 20 74 68 65 20 66 69 72 73 74 2c 20 65  des the first, e
cc6b0 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 79 20  ven though they 
cc6c0 77 65 72 65 20 6f 6e 20 64 69 66 66 65 72 65 6e  were on differen
cc6d0 74 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69  t.** file descri
cc6e0 70 74 6f 72 73 20 6f 70 65 6e 65 64 20 6f 6e 20  ptors opened on 
cc6f0 64 69 66 66 65 72 65 6e 74 20 66 69 6c 65 20 6e  different file n
cc700 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ames..**.** This
cc710 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 63   means that we c
cc720 61 6e 6e 6f 74 20 75 73 65 20 50 4f 53 49 58 20  annot use POSIX 
cc730 6c 6f 63 6b 73 20 74 6f 20 73 79 6e 63 68 72 6f  locks to synchro
cc740 6e 69 7a 65 20 66 69 6c 65 20 61 63 63 65 73 73  nize file access
cc750 0a 2a 2a 20 61 6d 6f 6e 67 20 63 6f 6d 70 65 74  .** among compet
cc760 69 6e 67 20 74 68 72 65 61 64 73 20 6f 66 20 74  ing threads of t
cc770 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e  he same process.
cc780 20 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 77 69    POSIX locks wi
cc790 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 0a 2a 2a 20  ll work fine.** 
cc7a0 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a 65 20 61  to synchronize a
cc7b0 63 63 65 73 73 20 66 6f 72 20 74 68 72 65 61 64  ccess for thread
cc7c0 73 20 69 6e 20 73 65 70 61 72 61 74 65 20 70 72  s in separate pr
cc7d0 6f 63 65 73 73 65 73 2c 20 62 75 74 20 6e 6f 74  ocesses, but not
cc7e0 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 69 74 68  .** threads with
cc7f0 69 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63  in the same proc
cc800 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 6f  ess..**.** To wo
cc810 72 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 70 72  rk around the pr
cc820 6f 62 6c 65 6d 2c 20 53 51 4c 69 74 65 20 68 61  oblem, SQLite ha
cc830 73 20 74 6f 20 6d 61 6e 61 67 65 20 66 69 6c 65  s to manage file
cc840 20 6c 6f 63 6b 73 20 69 6e 74 65 72 6e 61 6c 6c   locks internall
cc850 79 0a 2a 2a 20 6f 6e 20 69 74 73 20 6f 77 6e 2e  y.** on its own.
cc860 20 20 57 68 65 6e 65 76 65 72 20 61 20 6e 65 77    Whenever a new
cc870 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65   database is ope
cc880 6e 65 64 2c 20 77 65 20 68 61 76 65 20 74 6f 20  ned, we have to 
cc890 66 69 6e 64 20 74 68 65 0a 2a 2a 20 73 70 65 63  find the.** spec
cc8a0 69 66 69 63 20 69 6e 6f 64 65 20 6f 66 20 74 68  ific inode of th
cc8b0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
cc8c0 28 74 68 65 20 69 6e 6f 64 65 20 69 73 20 64 65  (the inode is de
cc8d0 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 0a  termined by the.
cc8e0 2a 2a 20 73 74 5f 64 65 76 20 61 6e 64 20 73 74  ** st_dev and st
cc8f0 5f 69 6e 6f 20 66 69 65 6c 64 73 20 6f 66 20 74  _ino fields of t
cc900 68 65 20 73 74 61 74 20 73 74 72 75 63 74 75 72  he stat structur
cc910 65 20 74 68 61 74 20 66 73 74 61 74 28 29 20 66  e that fstat() f
cc920 69 6c 6c 73 20 69 6e 29 0a 2a 2a 20 61 6e 64 20  ills in).** and 
cc930 63 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 73 20  check for locks 
cc940 61 6c 72 65 61 64 79 20 65 78 69 73 74 69 6e 67  already existing
cc950 20 6f 6e 20 74 68 61 74 20 69 6e 6f 64 65 2e 20   on that inode. 
cc960 20 57 68 65 6e 20 6c 6f 63 6b 73 20 61 72 65 0a   When locks are.
cc970 2a 2a 20 63 72 65 61 74 65 64 20 6f 72 20 72 65  ** created or re
cc980 6d 6f 76 65 64 2c 20 77 65 20 68 61 76 65 20 74  moved, we have t
cc990 6f 20 6c 6f 6f 6b 20 61 74 20 6f 75 72 20 6f 77  o look at our ow
cc9a0 6e 20 69 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72  n internal recor
cc9b0 64 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b  d of the.** lock
cc9c0 73 20 74 6f 20 73 65 65 20 69 66 20 61 6e 6f 74  s to see if anot
cc9d0 68 65 72 20 74 68 72 65 61 64 20 68 61 73 20 70  her thread has p
cc9e0 72 65 76 69 6f 75 73 6c 79 20 73 65 74 20 61 20  reviously set a 
cc9f0 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 20 73 61 6d  lock on that sam
cca00 65 0a 2a 2a 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a  e.** inode..**.*
cca10 2a 20 28 41 73 69 64 65 3a 20 54 68 65 20 75 73  * (Aside: The us
cca20 65 20 6f 66 20 69 6e 6f 64 65 20 6e 75 6d 62 65  e of inode numbe
cca30 72 73 20 61 73 20 75 6e 69 71 75 65 20 49 44 73  rs as unique IDs
cca40 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f   does not work o
cca50 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 46 6f  n VxWorks..** Fo
cca60 72 20 56 78 57 6f 72 6b 73 2c 20 77 65 20 68 61  r VxWorks, we ha
cca70 76 65 20 74 6f 20 75 73 65 20 74 68 65 20 61 6c  ve to use the al
cca80 74 65 72 6e 61 74 69 76 65 20 75 6e 69 71 75 65  ternative unique
cca90 20 49 44 20 73 79 73 74 65 6d 20 62 61 73 65 64   ID system based
ccaa0 20 6f 6e 0a 2a 2a 20 63 61 6e 6f 6e 69 63 61 6c   on.** canonical
ccab0 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6d   filename and im
ccac0 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74 68 65  plemented in the
ccad0 20 70 72 65 76 69 6f 75 73 20 64 69 76 69 73 69   previous divisi
ccae0 6f 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  on.).**.** The s
ccaf0 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74 72 75  qlite3_file stru
ccb00 63 74 75 72 65 20 66 6f 72 20 50 4f 53 49 58 20  cture for POSIX 
ccb10 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6a 75 73  is no longer jus
ccb20 74 20 61 6e 20 69 6e 74 65 67 65 72 20 66 69 6c  t an integer fil
ccb30 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 2e  e.** descriptor.
ccb40 20 20 49 74 20 69 73 20 6e 6f 77 20 61 20 73 74    It is now a st
ccb50 72 75 63 74 75 72 65 20 74 68 61 74 20 68 6f 6c  ructure that hol
ccb60 64 73 20 74 68 65 20 69 6e 74 65 67 65 72 20 66  ds the integer f
ccb70 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f  ile.** descripto
ccb80 72 20 61 6e 64 20 61 20 70 6f 69 6e 74 65 72 20  r and a pointer 
ccb90 74 6f 20 61 20 73 74 72 75 63 74 75 72 65 20 74  to a structure t
ccba0 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68  hat describes th
ccbb0 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 6c 6f  e internal.** lo
ccbc0 63 6b 73 20 6f 6e 20 74 68 65 20 63 6f 72 72 65  cks on the corre
ccbd0 73 70 6f 6e 64 69 6e 67 20 69 6e 6f 64 65 2e 20  sponding inode. 
ccbe0 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 6c 6f   There is one lo
ccbf0 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a  cking structure.
ccc00 2a 2a 20 70 65 72 20 69 6e 6f 64 65 2c 20 73 6f  ** per inode, so
ccc10 20 69 66 20 74 68 65 20 73 61 6d 65 20 69 6e 6f   if the same ino
ccc20 64 65 20 69 73 20 6f 70 65 6e 65 64 20 74 77 69  de is opened twi
ccc30 63 65 2c 20 62 6f 74 68 20 75 6e 69 78 46 69 6c  ce, both unixFil
ccc40 65 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20  e structures.** 
ccc50 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 61 6d  point to the sam
ccc60 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74  e locking struct
ccc70 75 72 65 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e  ure.  The lockin
ccc80 67 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70  g structure keep
ccc90 73 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e 63 65  s.** a reference
ccca0 20 63 6f 75 6e 74 20 28 73 6f 20 77 65 20 77 69   count (so we wi
cccb0 6c 6c 20 6b 6e 6f 77 20 77 68 65 6e 20 74 6f 20  ll know when to 
cccc0 64 65 6c 65 74 65 20 69 74 29 20 61 6e 64 20 61  delete it) and a
cccd0 20 22 63 6e 74 22 0a 2a 2a 20 66 69 65 6c 64 20   "cnt".** field 
ccce0 74 68 61 74 20 74 65 6c 6c 73 20 75 73 20 69 74  that tells us it
cccf0 73 20 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b 20  s internal lock 
ccd00 73 74 61 74 75 73 2e 20 20 63 6e 74 3d 3d 30 20  status.  cnt==0 
ccd10 6d 65 61 6e 73 20 74 68 65 0a 2a 2a 20 66 69 6c  means the.** fil
ccd20 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2e 20 20  e is unlocked.  
ccd30 63 6e 74 3d 3d 2d 31 20 6d 65 61 6e 73 20 74 68  cnt==-1 means th
ccd40 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20 65 78  e file has an ex
ccd50 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 0a 2a 2a  clusive lock..**
ccd60 20 63 6e 74 3e 30 20 6d 65 61 6e 73 20 74 68 65   cnt>0 means the
ccd70 72 65 20 61 72 65 20 63 6e 74 20 73 68 61 72 65  re are cnt share
ccd80 64 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 66  d locks on the f
ccd90 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 61  ile..**.** Any a
ccda0 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 6b 20 6f  ttempt to lock o
ccdb0 72 20 75 6e 6c 6f 63 6b 20 61 20 66 69 6c 65 20  r unlock a file 
ccdc0 66 69 72 73 74 20 63 68 65 63 6b 73 20 74 68 65  first checks the
ccdd0 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 72 75   locking.** stru
ccde0 63 74 75 72 65 2e 20 20 54 68 65 20 66 63 6e 74  cture.  The fcnt
ccdf0 6c 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 20  l() system call 
cce00 69 73 20 6f 6e 6c 79 20 69 6e 76 6f 6b 65 64 20  is only invoked 
cce10 74 6f 20 73 65 74 20 61 20 0a 2a 2a 20 50 4f 53  to set a .** POS
cce20 49 58 20 6c 6f 63 6b 20 69 66 20 74 68 65 20 69  IX lock if the i
cce30 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 72  nternal lock str
cce40 75 63 74 75 72 65 20 74 72 61 6e 73 69 74 69 6f  ucture transitio
cce50 6e 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 61 20  ns between.** a 
cce60 6c 6f 63 6b 65 64 20 61 6e 64 20 61 6e 20 75 6e  locked and an un
cce70 6c 6f 63 6b 65 64 20 73 74 61 74 65 2e 0a 2a 2a  locked state..**
cce80 0a 2a 2a 20 42 75 74 20 77 61 69 74 3a 20 20 74  .** But wait:  t
cce90 68 65 72 65 20 61 72 65 20 79 65 74 20 6d 6f 72  here are yet mor
ccea0 65 20 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 20  e problems with 
cceb0 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20 6c  POSIX advisory l
ccec0 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 79  ocks..**.** If y
cced0 6f 75 20 63 6c 6f 73 65 20 61 20 66 69 6c 65 20  ou close a file 
ccee0 64 65 73 63 72 69 70 74 6f 72 20 74 68 61 74 20  descriptor that 
ccef0 70 6f 69 6e 74 73 20 74 6f 20 61 20 66 69 6c 65  points to a file
ccf00 20 74 68 61 74 20 68 61 73 20 6c 6f 63 6b 73 2c   that has locks,
ccf10 0a 2a 2a 20 61 6c 6c 20 6c 6f 63 6b 73 20 6f 6e  .** all locks on
ccf20 20 74 68 61 74 20 66 69 6c 65 20 74 68 61 74 20   that file that 
ccf30 61 72 65 20 6f 77 6e 65 64 20 62 79 20 74 68 65  are owned by the
ccf40 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65 73 73   current process
ccf50 20 61 72 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64   are.** released
ccf60 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e  .  To work aroun
ccf70 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 2c 20  d this problem, 
ccf80 65 61 63 68 20 75 6e 69 78 46 69 6c 65 20 73 74  each unixFile st
ccf90 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73  ructure contains
ccfa0 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  .** a pointer to
ccfb0 20 61 6e 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20   an unixOpenCnt 
ccfc0 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 72  structure.  Ther
ccfd0 65 20 69 73 20 6f 6e 65 20 75 6e 69 78 4f 70 65  e is one unixOpe
ccfe0 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 0a 2a  nCnt structure.*
ccff0 2a 20 70 65 72 20 6f 70 65 6e 20 69 6e 6f 64 65  * per open inode
cd000 2c 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68  , which means th
cd010 61 74 20 6d 75 6c 74 69 70 6c 65 20 75 6e 69 78  at multiple unix
cd020 46 69 6c 65 20 63 61 6e 20 70 6f 69 6e 74 20 74  File can point t
cd030 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 75 6e  o a single.** un
cd040 69 78 4f 70 65 6e 43 6e 74 2e 20 20 57 68 65 6e  ixOpenCnt.  When
cd050 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d   an attempt is m
cd060 61 64 65 20 74 6f 20 63 6c 6f 73 65 20 61 6e 20  ade to close an 
cd070 75 6e 69 78 46 69 6c 65 2c 20 69 66 20 74 68 65  unixFile, if the
cd080 72 65 20 61 72 65 0a 2a 2a 20 6f 74 68 65 72 20  re are.** other 
cd090 75 6e 69 78 46 69 6c 65 20 6f 70 65 6e 20 6f 6e  unixFile open on
cd0a0 20 74 68 65 20 73 61 6d 65 20 69 6e 6f 64 65 20   the same inode 
cd0b0 74 68 61 74 20 61 72 65 20 68 6f 6c 64 69 6e 67  that are holding
cd0c0 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 61 6c 6c   locks, the call
cd0d0 0a 2a 2a 20 74 6f 20 63 6c 6f 73 65 28 29 20 74  .** to close() t
cd0e0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
cd0f0 6f 72 20 69 73 20 64 65 66 65 72 72 65 64 20 75  or is deferred u
cd100 6e 74 69 6c 20 61 6c 6c 20 6f 66 20 74 68 65 20  ntil all of the 
cd110 6c 6f 63 6b 73 20 63 6c 65 61 72 2e 0a 2a 2a 20  locks clear..** 
cd120 54 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20  The unixOpenCnt 
cd130 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 20  structure keeps 
cd140 61 20 6c 69 73 74 20 6f 66 20 66 69 6c 65 20 64  a list of file d
cd150 65 73 63 72 69 70 74 6f 72 73 20 74 68 61 74 20  escriptors that 
cd160 6e 65 65 64 20 74 6f 0a 2a 2a 20 62 65 20 63 6c  need to.** be cl
cd170 6f 73 65 64 20 61 6e 64 20 74 68 61 74 20 6c 69  osed and that li
cd180 73 74 20 69 73 20 77 61 6c 6b 65 64 20 28 61 6e  st is walked (an
cd190 64 20 63 6c 65 61 72 65 64 29 20 77 68 65 6e 20  d cleared) when 
cd1a0 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 0a 2a 2a  the last lock.**
cd1b0 20 63 6c 65 61 72 73 2e 0a 2a 2a 0a 2a 2a 20 59   clears..**.** Y
cd1c0 65 74 20 61 6e 6f 74 68 65 72 20 70 72 6f 62 6c  et another probl
cd1d0 65 6d 3a 20 20 4c 69 6e 75 78 54 68 72 65 61 64  em:  LinuxThread
cd1e0 73 20 64 6f 20 6e 6f 74 20 70 6c 61 79 20 77 65  s do not play we
cd1f0 6c 6c 20 77 69 74 68 20 70 6f 73 69 78 20 6c 6f  ll with posix lo
cd200 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20  cks..**.** Many 
cd210 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73 20 6f  older versions o
cd220 66 20 6c 69 6e 75 78 20 75 73 65 20 74 68 65 20  f linux use the 
cd230 4c 69 6e 75 78 54 68 72 65 61 64 73 20 6c 69 62  LinuxThreads lib
cd240 72 61 72 79 20 77 68 69 63 68 20 69 73 0a 2a 2a  rary which is.**
cd250 20 6e 6f 74 20 70 6f 73 69 78 20 63 6f 6d 70 6c   not posix compl
cd260 69 61 6e 74 2e 20 20 55 6e 64 65 72 20 4c 69 6e  iant.  Under Lin
cd270 75 78 54 68 72 65 61 64 73 2c 20 61 20 6c 6f 63  uxThreads, a loc
cd280 6b 20 63 72 65 61 74 65 64 20 62 79 20 74 68 72  k created by thr
cd290 65 61 64 0a 2a 2a 20 41 20 63 61 6e 6e 6f 74 20  ead.** A cannot 
cd2a0 62 65 20 6d 6f 64 69 66 69 65 64 20 6f 72 20 6f  be modified or o
cd2b0 76 65 72 72 69 64 64 65 6e 20 62 79 20 61 20 64  verridden by a d
cd2c0 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 20  ifferent thread 
cd2d0 42 2e 0a 2a 2a 20 4f 6e 6c 79 20 74 68 72 65 61  B..** Only threa
cd2e0 64 20 41 20 63 61 6e 20 6d 6f 64 69 66 79 20 74  d A can modify t
cd2f0 68 65 20 6c 6f 63 6b 2e 20 20 4c 6f 63 6b 69 6e  he lock.  Lockin
cd300 67 20 62 65 68 61 76 69 6f 72 20 69 73 20 63 6f  g behavior is co
cd310 72 72 65 63 74 0a 2a 2a 20 69 66 20 74 68 65 20  rrect.** if the 
cd320 61 70 70 6c 69 61 74 69 6f 6e 20 75 73 65 73 20  appliation uses 
cd330 74 68 65 20 6e 65 77 65 72 20 4e 61 74 69 76 65  the newer Native
cd340 20 50 6f 73 69 78 20 54 68 72 65 61 64 20 4c 69   Posix Thread Li
cd350 62 72 61 72 79 20 28 4e 50 54 4c 29 0a 2a 2a 20  brary (NPTL).** 
cd360 6f 6e 20 6c 69 6e 75 78 20 2d 20 77 69 74 68 20  on linux - with 
cd370 4e 50 54 4c 20 61 20 6c 6f 63 6b 20 63 72 65 61  NPTL a lock crea
cd380 74 65 64 20 62 79 20 74 68 72 65 61 64 20 41 20  ted by thread A 
cd390 63 61 6e 20 6f 76 65 72 72 69 64 65 20 6c 6f 63  can override loc
cd3a0 6b 73 0a 2a 2a 20 69 6e 20 74 68 72 65 61 64 20  ks.** in thread 
cd3b0 42 2e 20 20 42 75 74 20 74 68 65 72 65 20 69 73  B.  But there is
cd3c0 20 6e 6f 20 77 61 79 20 74 6f 20 6b 6e 6f 77 20   no way to know 
cd3d0 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20  at compile-time 
cd3e0 77 68 69 63 68 0a 2a 2a 20 74 68 72 65 61 64 69  which.** threadi
cd3f0 6e 67 20 6c 69 62 72 61 72 79 20 69 73 20 62 65  ng library is be
cd400 69 6e 67 20 75 73 65 64 2e 20 20 53 6f 20 74 68  ing used.  So th
cd410 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f  ere is no way to
cd420 20 6b 6e 6f 77 20 61 74 0a 2a 2a 20 63 6f 6d 70   know at.** comp
cd430 69 6c 65 2d 74 69 6d 65 20 77 68 65 74 68 65 72  ile-time whether
cd440 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 20 41   or not thread A
cd450 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 6c 6f   can override lo
cd460 63 6b 73 20 6f 6e 20 74 68 72 65 61 64 20 42 2e  cks on thread B.
cd470 0a 2a 2a 20 57 65 20 68 61 76 65 20 74 6f 20 64  .** We have to d
cd480 6f 20 61 20 72 75 6e 2d 74 69 6d 65 20 63 68 65  o a run-time che
cd490 63 6b 20 74 6f 20 64 69 73 63 6f 76 65 72 20 74  ck to discover t
cd4a0 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74  he behavior of t
cd4b0 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 70 72  he.** current pr
cd4c0 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20  ocess..**.** On 
cd4d0 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 74 68  systems where th
cd4e0 72 65 61 64 20 41 20 69 73 20 75 6e 61 62 6c 65  read A is unable
cd4f0 20 74 6f 20 6d 6f 64 69 66 79 20 6c 6f 63 6b 73   to modify locks
cd500 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 74   created by.** t
cd510 68 72 65 61 64 20 42 2c 20 77 65 20 68 61 76 65  hread B, we have
cd520 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b 20 6f   to keep track o
cd530 66 20 77 68 69 63 68 20 74 68 72 65 61 64 20 63  f which thread c
cd540 72 65 61 74 65 64 20 65 61 63 68 0a 2a 2a 20 6c  reated each.** l
cd550 6f 63 6b 2e 20 20 48 65 6e 63 65 20 74 68 65 72  ock.  Hence ther
cd560 65 20 69 73 20 61 6e 20 65 78 74 72 61 20 66 69  e is an extra fi
cd570 65 6c 64 20 69 6e 20 74 68 65 20 6b 65 79 20 74  eld in the key t
cd580 6f 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e  o the unixLockIn
cd590 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20  fo.** structure 
cd5a0 74 6f 20 72 65 63 6f 72 64 20 74 68 69 73 20 69  to record this i
cd5b0 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 41 6e 64  nformation.  And
cd5c0 20 6f 6e 20 74 68 6f 73 65 20 73 79 73 74 65 6d   on those system
cd5d0 73 20 69 74 0a 2a 2a 20 69 73 20 69 6c 6c 65 67  s it.** is illeg
cd5e0 61 6c 20 74 6f 20 62 65 67 69 6e 20 61 20 74 72  al to begin a tr
cd5f0 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 6f 6e 65  ansaction in one
cd600 20 74 68 72 65 61 64 20 61 6e 64 20 66 69 6e 69   thread and fini
cd610 73 68 20 69 74 0a 2a 2a 20 69 6e 20 61 6e 6f 74  sh it.** in anot
cd620 68 65 72 2e 20 20 46 6f 72 20 74 68 69 73 20 6c  her.  For this l
cd630 61 74 74 65 72 20 72 65 73 74 72 69 63 74 69 6f  atter restrictio
cd640 6e 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77  n, there is no w
cd650 6f 72 6b 2d 61 72 6f 75 6e 64 2e 0a 2a 2a 20 49  ork-around..** I
cd660 74 20 69 73 20 61 20 6c 69 6d 69 74 61 74 69 6f  t is a limitatio
cd670 6e 20 6f 66 20 4c 69 6e 75 78 54 68 72 65 61 64  n of LinuxThread
cd680 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  s..*/../*.** Set
cd690 20 6f 72 20 63 68 65 63 6b 20 74 68 65 20 75 6e   or check the un
cd6a0 69 78 46 69 6c 65 2e 74 69 64 20 66 69 65 6c 64  ixFile.tid field
cd6b0 2e 20 20 54 68 69 73 20 66 69 65 6c 64 20 69 73  .  This field is
cd6c0 20 73 65 74 20 77 68 65 6e 20 61 6e 20 75 6e 69   set when an uni
cd6d0 78 46 69 6c 65 0a 2a 2a 20 69 73 20 66 69 72 73  xFile.** is firs
cd6e0 74 20 6f 70 65 6e 65 64 2e 20 20 41 6c 6c 20 73  t opened.  All s
cd6f0 75 62 73 65 71 75 65 6e 74 20 75 73 65 73 20 6f  ubsequent uses o
cd700 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 76  f the unixFile v
cd710 65 72 69 66 79 20 74 68 61 74 20 74 68 65 0a 2a  erify that the.*
cd720 2a 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 73  * same thread is
cd730 20 6f 70 65 72 61 74 69 6e 67 20 6f 6e 20 74 68   operating on th
cd740 65 20 75 6e 69 78 46 69 6c 65 2e 20 20 53 6f 6d  e unixFile.  Som
cd750 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  e operating syst
cd760 65 6d 73 20 64 6f 0a 2a 2a 20 6e 6f 74 20 61 6c  ems do.** not al
cd770 6c 6f 77 20 6c 6f 63 6b 73 20 74 6f 20 62 65 20  low locks to be 
cd780 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20 6f 74  overridden by ot
cd790 68 65 72 20 74 68 72 65 61 64 73 20 61 6e 64 20  her threads and 
cd7a0 74 68 61 74 20 72 65 73 74 72 69 63 74 69 6f 6e  that restriction
cd7b0 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 73  .** means that s
cd7c0 71 6c 69 74 65 33 2a 20 64 61 74 61 62 61 73 65  qlite3* database
cd7d0 20 68 61 6e 64 6c 65 73 20 63 61 6e 6e 6f 74 20   handles cannot 
cd7e0 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20 6f 6e  be moved from on
cd7f0 65 20 74 68 72 65 61 64 0a 2a 2a 20 74 6f 20 61  e thread.** to a
cd800 6e 6f 74 68 65 72 20 77 68 69 6c 65 20 6c 6f 63  nother while loc
cd810 6b 73 20 61 72 65 20 68 65 6c 64 2e 0a 2a 2a 0a  ks are held..**.
cd820 2a 2a 20 56 65 72 73 69 6f 6e 20 33 2e 33 2e 31  ** Version 3.3.1
cd830 20 28 32 30 30 36 2d 30 31 2d 31 35 29 3a 20 20   (2006-01-15):  
cd840 75 6e 69 78 46 69 6c 65 20 63 61 6e 20 62 65 20  unixFile can be 
cd850 6d 6f 76 65 64 20 66 72 6f 6d 20 6f 6e 65 20 74  moved from one t
cd860 68 72 65 61 64 20 74 6f 0a 2a 2a 20 61 6e 6f 74  hread to.** anot
cd870 68 65 72 20 61 73 20 6c 6f 6e 67 20 61 73 20 77  her as long as w
cd880 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 6f 6e  e are running on
cd890 20 61 20 73 79 73 74 65 6d 20 74 68 61 74 20 73   a system that s
cd8a0 75 70 70 6f 72 74 73 20 74 68 72 65 61 64 73 0a  upports threads.
cd8b0 2a 2a 20 6f 76 65 72 72 69 64 69 6e 67 20 65 61  ** overriding ea
cd8c0 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20  ch others locks 
cd8d0 28 77 68 69 63 68 20 69 73 20 6e 6f 77 20 74 68  (which is now th
cd8e0 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 20 62 65  e most common be
cd8f0 68 61 76 69 6f 72 29 0a 2a 2a 20 6f 72 20 69 66  havior).** or if
cd900 20 6e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 68 65   no locks are he
cd910 6c 64 2e 20 20 42 75 74 20 74 68 65 20 75 6e 69  ld.  But the uni
cd920 78 46 69 6c 65 2e 70 4c 6f 63 6b 20 66 69 65 6c  xFile.pLock fiel
cd930 64 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 2a 2a  d needs to be.**
cd940 20 72 65 63 6f 6d 70 75 74 65 64 20 62 65 63 61   recomputed beca
cd950 75 73 65 20 69 74 73 20 6b 65 79 20 69 6e 63 6c  use its key incl
cd960 75 64 65 73 20 74 68 65 20 74 68 72 65 61 64 2d  udes the thread-
cd970 69 64 2e 20 20 53 65 65 20 74 68 65 20 0a 2a 2a  id.  See the .**
cd980 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68   transferOwnersh
cd990 69 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65  ip() function be
cd9a0 6c 6f 77 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  low for addition
cd9b0 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a  al information.*
cd9c0 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  /.#if SQLITE_THR
cd9d0 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e  EADSAFE && defin
cd9e0 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 23 20  ed(__linux__).# 
cd9f0 64 65 66 69 6e 65 20 53 45 54 5f 54 48 52 45 41  define SET_THREA
cda00 44 49 44 28 58 29 20 20 20 28 58 29 2d 3e 74 69  DID(X)   (X)->ti
cda10 64 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c 66  d = pthread_self
cda20 28 29 0a 23 20 64 65 66 69 6e 65 20 43 48 45 43  ().# define CHEC
cda30 4b 5f 54 48 52 45 41 44 49 44 28 58 29 20 28 74  K_THREADID(X) (t
cda40 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61  hreadsOverrideEa
cda50 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3d 3d 30  chOthersLocks==0
cda60 20 26 26 20 5c 0a 20 20 20 20 20 20 20 20 20 20   && \.          
cda70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cda80 20 20 21 70 74 68 72 65 61 64 5f 65 71 75 61 6c    !pthread_equal
cda90 28 28 58 29 2d 3e 74 69 64 2c 20 70 74 68 72 65  ((X)->tid, pthre
cdaa0 61 64 5f 73 65 6c 66 28 29 29 29 0a 23 65 6c 73  ad_self())).#els
cdab0 65 0a 23 20 64 65 66 69 6e 65 20 53 45 54 5f 54  e.# define SET_T
cdac0 48 52 45 41 44 49 44 28 58 29 0a 23 20 64 65 66  HREADID(X).# def
cdad0 69 6e 65 20 43 48 45 43 4b 5f 54 48 52 45 41 44  ine CHECK_THREAD
cdae0 49 44 28 58 29 20 30 0a 23 65 6e 64 69 66 0a 0a  ID(X) 0.#endif..
cdaf0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
cdb00 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
cdb10 6e 67 20 73 74 72 75 63 74 75 72 65 20 73 65 72  ng structure ser
cdb20 76 65 73 20 61 73 20 74 68 65 20 6b 65 79 20 75  ves as the key u
cdb30 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65  sed.** to locate
cdb40 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 75 6e   a particular un
cdb50 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74  ixOpenCnt struct
cdb60 75 72 65 20 67 69 76 65 6e 20 69 74 73 20 69 6e  ure given its in
cdb70 6f 64 65 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73  ode.  This.** is
cdb80 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
cdb90 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 65 78 63   unixLockKey exc
cdba0 65 70 74 20 74 68 61 74 20 74 68 65 20 74 68 72  ept that the thr
cdbb0 65 61 64 20 49 44 20 69 73 20 6f 6d 69 74 74 65  ead ID is omitte
cdbc0 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69  d..*/.struct uni
cdbd0 78 46 69 6c 65 49 64 20 7b 0a 20 20 64 65 76 5f  xFileId {.  dev_
cdbe0 74 20 64 65 76 3b 20 20 20 20 20 20 20 20 20 20  t dev;          
cdbf0 20 20 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63          /* Devic
cdc00 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 69 66 20  e number */.#if 
cdc10 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72  OS_VXWORKS.  str
cdc20 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49  uct vxworksFileI
cdc30 64 20 2a 70 49 64 3b 20 20 2f 2a 20 55 6e 69 71  d *pId;  /* Uniq
cdc40 75 65 20 66 69 6c 65 20 49 44 20 66 6f 72 20 76  ue file ID for v
cdc50 78 77 6f 72 6b 73 2e 20 2a 2f 0a 23 65 6c 73 65  xworks. */.#else
cdc60 0a 20 20 69 6e 6f 5f 74 20 69 6e 6f 3b 20 20 20  .  ino_t ino;   
cdc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
cdc80 2a 20 49 6e 6f 64 65 20 6e 75 6d 62 65 72 20 2a  * Inode number *
cdc90 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a  /.#endif.};../*.
cdca0 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
cdcb0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
cdcc0 73 74 72 75 63 74 75 72 65 20 73 65 72 76 65 73  structure serves
cdcd0 20 61 73 20 74 68 65 20 6b 65 79 20 75 73 65 64   as the key used
cdce0 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61 20  .** to locate a 
cdcf0 70 61 72 74 69 63 75 6c 61 72 20 75 6e 69 78 4c  particular unixL
cdd00 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72  ockInfo structur
cdd10 65 20 67 69 76 65 6e 20 69 74 73 20 69 6e 6f 64  e given its inod
cdd20 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 72 65  e..**.** If thre
cdd30 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76 65 72 72  ads cannot overr
cdd40 69 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 20  ide each others 
cdd50 6c 6f 63 6b 73 20 28 4c 69 6e 75 78 54 68 72 65  locks (LinuxThre
cdd60 61 64 73 29 2c 20 74 68 65 6e 20 77 65 0a 2a 2a  ads), then we.**
cdd70 20 73 65 74 20 74 68 65 20 75 6e 69 78 4c 6f 63   set the unixLoc
cdd80 6b 4b 65 79 2e 74 69 64 20 66 69 65 6c 64 20 74  kKey.tid field t
cdd90 6f 20 74 68 65 20 74 68 72 65 61 64 20 49 44 2e  o the thread ID.
cdda0 20 20 49 66 20 74 68 72 65 61 64 73 20 63 61 6e    If threads can
cddb0 20 6f 76 65 72 72 69 64 65 0a 2a 2a 20 65 61 63   override.** eac
cddc0 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 28  h others locks (
cddd0 50 6f 73 69 78 20 61 6e 64 20 4e 50 54 4c 29 20  Posix and NPTL) 
cdde0 74 68 65 6e 20 74 69 64 20 69 73 20 61 6c 77 61  then tid is alwa
cddf0 79 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a  ys set to zero..
cde00 2a 2a 20 74 69 64 20 69 73 20 6f 6d 69 74 74 65  ** tid is omitte
cde10 64 20 69 66 20 77 65 20 63 6f 6d 70 69 6c 65 20  d if we compile 
cde20 77 69 74 68 6f 75 74 20 74 68 72 65 61 64 69 6e  without threadin
cde30 67 20 73 75 70 70 6f 72 74 20 6f 72 20 6f 6e 20  g support or on 
cde40 61 6e 20 4f 53 0a 2a 2a 20 6f 74 68 65 72 20 74  an OS.** other t
cde50 68 61 6e 20 6c 69 6e 75 78 2e 0a 2a 2f 0a 73 74  han linux..*/.st
cde60 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79  ruct unixLockKey
cde70 20 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78   {.  struct unix
cde80 46 69 6c 65 49 64 20 66 69 64 3b 20 20 2f 2a 20  FileId fid;  /* 
cde90 55 6e 69 71 75 65 20 69 64 65 6e 74 69 66 69 65  Unique identifie
cdea0 72 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 2a  r for the file *
cdeb0 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  /.#if SQLITE_THR
cdec0 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e  EADSAFE && defin
cded0 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20  ed(__linux__).  
cdee0 70 74 68 72 65 61 64 5f 74 20 74 69 64 3b 20 20  pthread_t tid;  
cdef0 2f 2a 20 54 68 72 65 61 64 20 49 44 20 6f 66 20  /* Thread ID of 
cdf00 6c 6f 63 6b 20 6f 77 6e 65 72 2e 20 5a 65 72 6f  lock owner. Zero
cdf10 20 69 66 20 6e 6f 74 20 75 73 69 6e 67 20 4c 69   if not using Li
cdf20 6e 75 78 54 68 72 65 61 64 73 20 2a 2f 0a 23 65  nuxThreads */.#e
cdf30 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  ndif.};../*.** A
cdf40 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
cdf50 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
cdf60 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74  cture is allocat
cdf70 65 64 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e  ed for each open
cdf80 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 4f 72 2c 20  .** inode.  Or, 
cdf90 6f 6e 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2c  on LinuxThreads,
cdfa0 20 74 68 65 72 65 20 69 73 20 6f 6e 65 20 6f 66   there is one of
cdfb0 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65   these structure
cdfc0 73 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 69 6e  s for.** each in
cdfd0 6f 64 65 20 6f 70 65 6e 65 64 20 62 79 20 65 61  ode opened by ea
cdfe0 63 68 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a  ch thread..**.**
cdff0 20 41 20 73 69 6e 67 6c 65 20 69 6e 6f 64 65 20   A single inode 
ce000 63 61 6e 20 68 61 76 65 20 6d 75 6c 74 69 70 6c  can have multipl
ce010 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
ce020 72 73 2c 20 73 6f 20 65 61 63 68 20 75 6e 69 78  rs, so each unix
ce030 46 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 75 72  File.** structur
ce040 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69  e contains a poi
ce050 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61  nter to an insta
ce060 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65  nce of this obje
ce070 63 74 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 6f  ct and this.** o
ce080 62 6a 65 63 74 20 6b 65 65 70 73 20 61 20 63 6f  bject keeps a co
ce090 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62 65  unt of the numbe
ce0a0 72 20 6f 66 20 75 6e 69 78 46 69 6c 65 20 70 6f  r of unixFile po
ce0b0 69 6e 74 69 6e 67 20 74 6f 20 69 74 2e 0a 2a 2f  inting to it..*/
ce0c0 0a 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b  .struct unixLock
ce0d0 49 6e 66 6f 20 7b 0a 20 20 73 74 72 75 63 74 20  Info {.  struct 
ce0e0 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 6c 6f 63 6b  unixLockKey lock
ce0f0 4b 65 79 3b 20 20 20 20 20 2f 2a 20 54 68 65 20  Key;     /* The 
ce100 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a 20 20  lookup key */.  
ce110 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20 20  int cnt;        
ce120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce130 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 53 48 41  /* Number of SHA
ce140 52 45 44 20 6c 6f 63 6b 73 20 68 65 6c 64 20 2a  RED locks held *
ce150 2f 0a 20 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  /.  int locktype
ce160 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ce170 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 48      /* One of SH
ce180 41 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 53 45 52  ARED_LOCK, RESER
ce190 56 45 44 5f 4c 4f 43 4b 20 65 74 63 2e 20 2a 2f  VED_LOCK etc. */
ce1a0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20  .  int nRef;    
ce1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce1c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
ce1d0 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73  pointers to this
ce1e0 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20   structure */.  
ce1f0 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49  struct unixLockI
ce200 6e 66 6f 20 2a 70 4e 65 78 74 3b 20 20 20 20 20  nfo *pNext;     
ce210 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c 20 75  /* List of all u
ce220 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 6f 62 6a 65  nixLockInfo obje
ce230 63 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  cts */.  struct 
ce240 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 50  unixLockInfo *pP
ce250 72 65 76 3b 20 20 20 20 20 2f 2a 20 20 20 20 2e  rev;     /*    .
ce260 2e 2e 2e 20 64 6f 75 62 6c 79 20 6c 69 6e 6b 65  ... doubly linke
ce270 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  d */.};../*.** A
ce280 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
ce290 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
ce2a0 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74  cture is allocat
ce2b0 65 64 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e  ed for each open
ce2c0 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 54 68 69 73  .** inode.  This
ce2d0 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73   structure keeps
ce2e0 20 74 72 61 63 6b 20 6f 66 20 74 68 65 20 6e 75   track of the nu
ce2f0 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 6f 6e  mber of locks on
ce300 20 74 68 61 74 0a 2a 2a 20 69 6e 6f 64 65 2e 20   that.** inode. 
ce310 20 49 66 20 61 20 63 6c 6f 73 65 20 69 73 20 61   If a close is a
ce320 74 74 65 6d 70 74 65 64 20 61 67 61 69 6e 73 74  ttempted against
ce330 20 61 6e 20 69 6e 6f 64 65 20 74 68 61 74 20 69   an inode that i
ce340 73 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6c 6f 63  s holding.** loc
ce350 6b 73 2c 20 74 68 65 20 63 6c 6f 73 65 20 69 73  ks, the close is
ce360 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20   deferred until 
ce370 61 6c 6c 20 6c 6f 63 6b 73 20 63 6c 65 61 72 20  all locks clear 
ce380 62 79 20 61 64 64 69 6e 67 20 74 68 65 0a 2a 2a  by adding the.**
ce390 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
ce3a0 20 74 6f 20 62 65 20 63 6c 6f 73 65 64 20 74 6f   to be closed to
ce3b0 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c 69 73   the pending lis
ce3c0 74 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 20  t..**.** TODO:  
ce3d0 43 6f 6e 73 69 64 65 72 20 63 68 61 6e 67 69 6e  Consider changin
ce3e0 67 20 74 68 69 73 20 73 6f 20 74 68 61 74 20 74  g this so that t
ce3f0 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 73  here is only a s
ce400 69 6e 67 6c 65 20 66 69 6c 65 0a 2a 2a 20 64 65  ingle file.** de
ce410 73 63 72 69 70 74 6f 72 20 66 6f 72 20 65 61 63  scriptor for eac
ce420 68 20 6f 70 65 6e 20 66 69 6c 65 2c 20 65 76 65  h open file, eve
ce430 6e 20 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65  n when it is ope
ce440 6e 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d  ned multiple tim
ce450 65 73 2e 0a 2a 2a 20 54 68 65 20 63 6c 6f 73 65  es..** The close
ce460 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 77  () system call w
ce470 6f 75 6c 64 20 6f 6e 6c 79 20 6f 63 63 75 72 20  ould only occur 
ce480 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 64 61  when the last da
ce490 74 61 62 61 73 65 0a 2a 2a 20 75 73 69 6e 67 20  tabase.** using 
ce4a0 74 68 65 20 66 69 6c 65 20 63 6c 6f 73 65 73 2e  the file closes.
ce4b0 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 4f  .*/.struct unixO
ce4c0 70 65 6e 43 6e 74 20 7b 0a 20 20 73 74 72 75 63  penCnt {.  struc
ce4d0 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66 69 6c  t unixFileId fil
ce4e0 65 49 64 3b 20 20 20 2f 2a 20 54 68 65 20 6c 6f  eId;   /* The lo
ce4f0 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a 20 20 69 6e  okup key */.  in
ce500 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20  t nRef;         
ce510 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
ce520 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20  ber of pointers 
ce530 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72  to this structur
ce540 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 63 6b  e */.  int nLock
ce550 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ce560 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
ce570 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b  outstanding lock
ce580 73 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 73 65  s */.  UnixUnuse
ce590 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 20 20 20  dFd *pUnused;   
ce5a0 20 20 20 2f 2a 20 55 6e 75 73 65 64 20 66 69 6c     /* Unused fil
ce5b0 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 74 6f  e descriptors to
ce5c0 20 63 6c 6f 73 65 20 2a 2f 0a 23 69 66 20 4f 53   close */.#if OS
ce5d0 5f 56 58 57 4f 52 4b 53 0a 20 20 73 65 6d 5f 74  _VXWORKS.  sem_t
ce5e0 20 2a 70 53 65 6d 3b 20 20 20 20 20 20 20 20 20   *pSem;         
ce5f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
ce600 61 6d 65 64 20 50 4f 53 49 58 20 73 65 6d 61 70  amed POSIX semap
ce610 68 6f 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 61  hore */.  char a
ce620 53 65 6d 4e 61 6d 65 5b 4d 41 58 5f 50 41 54 48  SemName[MAX_PATH
ce630 4e 41 4d 45 2b 32 5d 3b 20 20 20 2f 2a 20 4e 61  NAME+2];   /* Na
ce640 6d 65 20 6f 66 20 74 68 61 74 20 73 65 6d 61 70  me of that semap
ce650 68 6f 72 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  hore */.#endif. 
ce660 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e   struct unixOpen
ce670 43 6e 74 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72  Cnt *pNext, *pPr
ce680 65 76 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66  ev;   /* List of
ce690 20 61 6c 6c 20 75 6e 69 78 4f 70 65 6e 43 6e 74   all unixOpenCnt
ce6a0 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 7d 3b 0a 0a   objects */.};..
ce6b0 2f 2a 0a 2a 2a 20 4c 69 73 74 73 20 6f 66 20 61  /*.** Lists of a
ce6c0 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  ll unixLockInfo 
ce6d0 61 6e 64 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20  and unixOpenCnt 
ce6e0 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 73 65 20  objects.  These 
ce6f0 75 73 65 64 20 74 6f 20 62 65 20 68 61 73 68 0a  used to be hash.
ce700 2a 2a 20 74 61 62 6c 65 73 2e 20 20 42 75 74 20  ** tables.  But 
ce710 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 62  the number of ob
ce720 6a 65 63 74 73 20 69 73 20 72 61 72 65 6c 79 20  jects is rarely 
ce730 6d 6f 72 65 20 74 68 61 6e 20 61 20 64 6f 7a 65  more than a doze
ce740 6e 20 61 6e 64 0a 2a 2a 20 6e 65 76 65 72 20 65  n and.** never e
ce750 78 63 65 65 64 73 20 61 20 66 65 77 20 74 68 6f  xceeds a few tho
ce760 75 73 61 6e 64 2e 20 20 41 6e 64 20 6c 6f 6f 6b  usand.  And look
ce770 75 70 20 69 73 20 6e 6f 74 20 6f 6e 20 61 20 63  up is not on a c
ce780 72 69 74 69 63 61 6c 0a 2a 2a 20 70 61 74 68 20  ritical.** path 
ce790 73 6f 20 61 20 73 69 6d 70 6c 65 20 6c 69 6e 6b  so a simple link
ce7a0 65 64 20 6c 69 73 74 20 77 69 6c 6c 20 73 75 66  ed list will suf
ce7b0 66 69 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  fice..*/.static 
ce7c0 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49  struct unixLockI
ce7d0 6e 66 6f 20 2a 6c 6f 63 6b 4c 69 73 74 20 3d 20  nfo *lockList = 
ce7e0 30 3b 0a 73 74 61 74 69 63 20 73 74 72 75 63 74  0;.static struct
ce7f0 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 6f 70   unixOpenCnt *op
ce800 65 6e 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a  enList = 0;../*.
ce810 2a 2a 20 54 68 69 73 20 76 61 72 69 61 62 6c 65  ** This variable
ce820 20 72 65 6d 65 6d 62 65 72 73 20 77 68 65 74 68   remembers wheth
ce830 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64  er or not thread
ce840 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65  s can override e
ce850 61 63 68 20 6f 74 68 65 72 73 0a 2a 2a 20 6c 6f  ach others.** lo
ce860 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 3a  cks..**.**    0:
ce870 20 20 4e 6f 2e 20 20 54 68 72 65 61 64 73 20 63    No.  Threads c
ce880 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64 65 20 65  annot override e
ce890 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73  ach others locks
ce8a0 2e 20 20 28 4c 69 6e 75 78 54 68 72 65 61 64 73  .  (LinuxThreads
ce8b0 29 0a 2a 2a 20 20 20 20 31 3a 20 20 59 65 73 2e  ).**    1:  Yes.
ce8c0 20 20 54 68 72 65 61 64 73 20 63 61 6e 20 6f 76    Threads can ov
ce8d0 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65  erride each othe
ce8e0 72 73 20 6c 6f 63 6b 73 2e 20 20 28 50 6f 73 69  rs locks.  (Posi
ce8f0 78 20 26 20 4e 4c 50 54 29 0a 2a 2a 20 20 20 2d  x & NLPT).**   -
ce900 31 3a 20 20 57 65 20 64 6f 6e 27 74 20 6b 6e 6f  1:  We don't kno
ce910 77 20 79 65 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20  w yet..**.** On 
ce920 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20 77 65  some systems, we
ce930 20 6b 6e 6f 77 20 61 74 20 63 6f 6d 70 69 6c 65   know at compile
ce940 2d 74 69 6d 65 20 69 66 20 74 68 72 65 61 64 73  -time if threads
ce950 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61   can override ea
ce960 63 68 0a 2a 2a 20 6f 74 68 65 72 73 20 6c 6f 63  ch.** others loc
ce970 6b 73 2e 20 20 4f 6e 20 74 68 6f 73 65 20 73 79  ks.  On those sy
ce980 73 74 65 6d 73 2c 20 74 68 65 20 53 51 4c 49 54  stems, the SQLIT
ce990 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44  E_THREAD_OVERRID
ce9a0 45 5f 4c 4f 43 4b 20 6d 61 63 72 6f 0a 2a 2a 20  E_LOCK macro.** 
ce9b0 77 69 6c 6c 20 62 65 20 73 65 74 20 61 70 70 72  will be set appr
ce9c0 6f 70 72 69 61 74 65 6c 79 2e 20 20 4f 6e 20 6f  opriately.  On o
ce9d0 74 68 65 72 20 73 79 73 74 65 6d 73 2c 20 77 65  ther systems, we
ce9e0 20 68 61 76 65 20 74 6f 20 63 68 65 63 6b 20 61   have to check a
ce9f0 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 2e 20 20 4f  t.** runtime.  O
cea00 6e 20 74 68 65 73 65 20 6c 61 74 74 65 72 20 73  n these latter s
cea10 79 73 74 65 6d 73 2c 20 53 51 4c 54 49 45 5f 54  ystems, SQLTIE_T
cea20 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c  HREAD_OVERRIDE_L
cea30 4f 43 4b 20 69 73 0a 2a 2a 20 75 6e 64 65 66 69  OCK is.** undefi
cea40 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ned..**.** This 
cea50 76 61 72 69 61 62 6c 65 20 6e 6f 72 6d 61 6c 6c  variable normall
cea60 79 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65  y has file scope
cea70 20 6f 6e 6c 79 2e 20 20 42 75 74 20 64 75 72 69   only.  But duri
cea80 6e 67 20 74 65 73 74 69 6e 67 2c 20 77 65 20 6d  ng testing, we m
cea90 61 6b 65 0a 2a 2a 20 69 74 20 61 20 67 6c 6f 62  ake.** it a glob
ceaa0 61 6c 20 73 6f 20 74 68 61 74 20 74 68 65 20 74  al so that the t
ceab0 65 73 74 20 63 6f 64 65 20 63 61 6e 20 63 68 61  est code can cha
ceac0 6e 67 65 20 69 74 73 20 76 61 6c 75 65 20 69 6e  nge its value in
cead0 20 6f 72 64 65 72 20 74 6f 20 76 65 72 69 66 79   order to verify
ceae0 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 72 69 67  .** that the rig
ceaf0 68 74 20 73 74 75 66 66 20 68 61 70 70 65 6e 73  ht stuff happens
ceb00 20 69 6e 20 65 69 74 68 65 72 20 63 61 73 65 2e   in either case.
ceb10 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  .*/.#if SQLITE_T
ceb20 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66  HREADSAFE && def
ceb30 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a  ined(__linux__).
ceb40 23 20 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45  #  ifndef SQLITE
ceb50 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45  _THREAD_OVERRIDE
ceb60 5f 4c 4f 43 4b 0a 23 20 20 20 20 64 65 66 69 6e  _LOCK.#    defin
ceb70 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f  e SQLITE_THREAD_
ceb80 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20 2d 31  OVERRIDE_LOCK -1
ceb90 0a 23 20 20 65 6e 64 69 66 0a 23 20 20 69 66 64  .#  endif.#  ifd
ceba0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 69  ef SQLITE_TEST.i
cebb0 6e 74 20 74 68 72 65 61 64 73 4f 76 65 72 72 69  nt threadsOverri
cebc0 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b  deEachOthersLock
cebd0 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 52 45 41  s = SQLITE_THREA
cebe0 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 3b  D_OVERRIDE_LOCK;
cebf0 0a 23 20 20 65 6c 73 65 0a 73 74 61 74 69 63 20  .#  else.static 
cec00 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65 72 72  int threadsOverr
cec10 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63  ideEachOthersLoc
cec20 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 52 45  ks = SQLITE_THRE
cec30 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b  AD_OVERRIDE_LOCK
cec40 3b 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69  ;.#  endif.#endi
cec50 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74  f../*.** This st
cec60 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e  ructure holds in
cec70 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73 65 64  formation passed
cec80 20 69 6e 74 6f 20 69 6e 64 69 76 69 64 75 61 6c   into individual
cec90 20 74 65 73 74 0a 2a 2a 20 74 68 72 65 61 64 73   test.** threads
ceca0 20 62 79 20 74 68 65 20 74 65 73 74 54 68 72 65   by the testThre
cecb0 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f  adLockingBehavio
cecc0 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a  r() routine..*/.
cecd0 73 74 72 75 63 74 20 74 68 72 65 61 64 54 65 73  struct threadTes
cece0 74 44 61 74 61 20 7b 0a 20 20 69 6e 74 20 66 64  tData {.  int fd
cecf0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ced00 20 2f 2a 20 46 69 6c 65 20 74 6f 20 62 65 20 6c   /* File to be l
ced10 6f 63 6b 65 64 20 2a 2f 0a 20 20 73 74 72 75 63  ocked */.  struc
ced20 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20  t flock lock;   
ced30 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 69 6e 67    /* The locking
ced40 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20   operation */.  
ced50 69 6e 74 20 72 65 73 75 6c 74 3b 20 20 20 20 20  int result;     
ced60 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
ced70 20 6f 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20   of the locking 
ced80 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a  operation */.};.
ced90 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
ceda0 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65  ADSAFE && define
cedb0 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 2f 2a 0a  d(__linux__)./*.
cedc0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
cedd0 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65 20   is used as the 
cede0 6d 61 69 6e 20 72 6f 75 74 69 6e 65 20 66 6f 72  main routine for
cedf0 20 61 20 74 68 72 65 61 64 20 6c 61 75 6e 63 68   a thread launch
cee00 65 64 20 62 79 0a 2a 2a 20 74 65 73 74 54 68 72  ed by.** testThr
cee10 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69  eadLockingBehavi
cee20 6f 72 28 29 2e 20 49 74 20 74 65 73 74 73 20 77  or(). It tests w
cee30 68 65 74 68 65 72 20 74 68 65 20 73 68 61 72 65  hether the share
cee40 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65 64 0a  d-lock obtained.
cee50 2a 2a 20 62 79 20 74 68 65 20 6d 61 69 6e 20 74  ** by the main t
cee60 68 72 65 61 64 20 69 6e 20 74 65 73 74 54 68 72  hread in testThr
cee70 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69  eadLockingBehavi
cee80 6f 72 28 29 20 63 6f 6e 66 6c 69 63 74 73 20 77  or() conflicts w
cee90 69 74 68 20 61 0a 2a 2a 20 68 79 70 6f 74 68 65  ith a.** hypothe
ceea0 74 69 63 61 6c 20 77 72 69 74 65 2d 6c 6f 63 6b  tical write-lock
ceeb0 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74 68 69   obtained by thi
ceec0 73 20 74 68 72 65 61 64 20 6f 6e 20 74 68 65 20  s thread on the 
ceed0 73 61 6d 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  same file..**.**
ceee0 20 54 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20   The write-lock 
ceef0 69 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20  is not actually 
cef00 61 63 71 75 69 72 65 64 2c 20 61 73 20 74 68 69  acquired, as thi
cef10 73 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  s is not possibl
cef20 65 20 69 66 20 0a 2a 2a 20 74 68 65 20 66 69 6c  e if .** the fil
cef30 65 20 69 73 20 6f 70 65 6e 20 69 6e 20 72 65 61  e is open in rea
cef40 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 28 73 65 65  d-only mode (see
cef50 20 74 69 63 6b 65 74 20 23 33 34 37 32 29 2e 0a   ticket #3472)..
cef60 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */ .static void 
cef70 2a 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67 54 65  *threadLockingTe
cef80 73 74 28 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a  st(void *pArg){.
cef90 20 20 73 74 72 75 63 74 20 74 68 72 65 61 64 54    struct threadT
cefa0 65 73 74 44 61 74 61 20 2a 70 44 61 74 61 20 3d  estData *pData =
cefb0 20 28 73 74 72 75 63 74 20 74 68 72 65 61 64 54   (struct threadT
cefc0 65 73 74 44 61 74 61 2a 29 70 41 72 67 3b 0a 20  estData*)pArg;. 
cefd0 20 70 44 61 74 61 2d 3e 72 65 73 75 6c 74 20 3d   pData->result =
cefe0 20 66 63 6e 74 6c 28 70 44 61 74 61 2d 3e 66 64   fcntl(pData->fd
ceff0 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 70 44 61 74  , F_GETLK, &pDat
cf000 61 2d 3e 6c 6f 63 6b 29 3b 0a 20 20 72 65 74 75  a->lock);.  retu
cf010 72 6e 20 70 41 72 67 3b 0a 7d 0a 23 65 6e 64 69  rn pArg;.}.#endi
cf020 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 48 52 45  f /* SQLITE_THRE
cf030 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65  ADSAFE && define
cf040 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 20 2a 2f 0a  d(__linux__) */.
cf050 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  ..#if SQLITE_THR
cf060 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e  EADSAFE && defin
cf070 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 2f 2a  ed(__linux__)./*
cf080 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 65 64 75  .** This procedu
cf090 72 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 64  re attempts to d
cf0a0 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72  etermine whether
cf0b0 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64 73 0a   or not threads.
cf0c0 2a 2a 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20  ** can override 
cf0d0 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b  each others lock
cf0e0 73 20 74 68 65 6e 20 73 65 74 73 20 74 68 65 20  s then sets the 
cf0f0 0a 2a 2a 20 74 68 72 65 61 64 73 4f 76 65 72 72  .** threadsOverr
cf100 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63  ideEachOthersLoc
cf110 6b 73 20 76 61 72 69 61 62 6c 65 20 61 70 70 72  ks variable appr
cf120 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a 73 74  opriately..*/.st
cf130 61 74 69 63 20 76 6f 69 64 20 74 65 73 74 54 68  atic void testTh
cf140 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76  readLockingBehav
cf150 69 6f 72 28 69 6e 74 20 66 64 5f 6f 72 69 67 29  ior(int fd_orig)
cf160 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 69 6e  {.  int fd;.  in
cf170 74 20 72 63 3b 0a 20 20 73 74 72 75 63 74 20 74  t rc;.  struct t
cf180 68 72 65 61 64 54 65 73 74 44 61 74 61 20 64 3b  hreadTestData d;
cf190 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20  .  struct flock 
cf1a0 6c 3b 0a 20 20 70 74 68 72 65 61 64 5f 74 20 74  l;.  pthread_t t
cf1b0 3b 0a 0a 20 20 66 64 20 3d 20 64 75 70 28 66 64  ;..  fd = dup(fd
cf1c0 5f 6f 72 69 67 29 3b 0a 20 20 69 66 28 20 66 64  _orig);.  if( fd
cf1d0 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d  <0 ) return;.  m
cf1e0 65 6d 73 65 74 28 26 6c 2c 20 30 2c 20 73 69 7a  emset(&l, 0, siz
cf1f0 65 6f 66 28 6c 29 29 3b 0a 20 20 6c 2e 6c 5f 74  eof(l));.  l.l_t
cf200 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20  ype = F_RDLCK;. 
cf210 20 6c 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20   l.l_len = 1;.  
cf220 6c 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20  l.l_start = 0;. 
cf230 20 6c 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45   l.l_whence = SE
cf240 45 4b 5f 53 45 54 3b 0a 20 20 72 63 20 3d 20 66  EK_SET;.  rc = f
cf250 63 6e 74 6c 28 66 64 5f 6f 72 69 67 2c 20 46 5f  cntl(fd_orig, F_
cf260 53 45 54 4c 4b 2c 20 26 6c 29 3b 0a 20 20 69 66  SETLK, &l);.  if
cf270 28 20 72 63 21 3d 30 20 29 20 72 65 74 75 72 6e  ( rc!=0 ) return
cf280 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 2c 20 30  ;.  memset(&d, 0
cf290 2c 20 73 69 7a 65 6f 66 28 64 29 29 3b 0a 20 20  , sizeof(d));.  
cf2a0 64 2e 66 64 20 3d 20 66 64 3b 0a 20 20 64 2e 6c  d.fd = fd;.  d.l
cf2b0 6f 63 6b 20 3d 20 6c 3b 0a 20 20 64 2e 6c 6f 63  ock = l;.  d.loc
cf2c0 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c  k.l_type = F_WRL
cf2d0 43 4b 3b 0a 20 20 69 66 28 20 70 74 68 72 65 61  CK;.  if( pthrea
cf2e0 64 5f 63 72 65 61 74 65 28 26 74 2c 20 30 2c 20  d_create(&t, 0, 
cf2f0 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67 54 65 73  threadLockingTes
cf300 74 2c 20 26 64 29 3d 3d 30 20 29 7b 0a 20 20 20  t, &d)==0 ){.   
cf310 20 70 74 68 72 65 61 64 5f 6a 6f 69 6e 28 74 2c   pthread_join(t,
cf320 20 30 29 3b 0a 20 20 7d 0a 20 20 63 6c 6f 73 65   0);.  }.  close
cf330 28 66 64 29 3b 0a 20 20 69 66 28 20 64 2e 72 65  (fd);.  if( d.re
cf340 73 75 6c 74 21 3d 30 20 29 20 72 65 74 75 72 6e  sult!=0 ) return
cf350 3b 0a 20 20 74 68 72 65 61 64 73 4f 76 65 72 72  ;.  threadsOverr
cf360 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63  ideEachOthersLoc
cf370 6b 73 20 3d 20 28 64 2e 6c 6f 63 6b 2e 6c 5f 74  ks = (d.lock.l_t
cf380 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 29 3b 0a 7d  ype==F_UNLCK);.}
cf390 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
cf3a0 45 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20  E_THREADSAFE && 
cf3b0 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f  defined(__linux_
cf3c0 5f 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c  _) */../*.** Rel
cf3d0 65 61 73 65 20 61 20 75 6e 69 78 4c 6f 63 6b 49  ease a unixLockI
cf3e0 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 70 72  nfo structure pr
cf3f0 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74  eviously allocat
cf400 65 64 20 62 79 20 66 69 6e 64 4c 6f 63 6b 49 6e  ed by findLockIn
cf410 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  fo()..**.** The 
cf420 6d 75 74 65 78 20 65 6e 74 65 72 65 64 20 75 73  mutex entered us
cf430 69 6e 67 20 74 68 65 20 75 6e 69 78 45 6e 74 65  ing the unixEnte
cf440 72 4d 75 74 65 78 28 29 20 66 75 6e 63 74 69 6f  rMutex() functio
cf450 6e 20 6d 75 73 74 20 62 65 20 68 65 6c 64 0a 2a  n must be held.*
cf460 2a 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63  * when this func
cf470 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a  tion is called..
cf480 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
cf490 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 73  eleaseLockInfo(s
cf4a0 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e  truct unixLockIn
cf4b0 66 6f 20 2a 70 4c 6f 63 6b 29 7b 0a 20 20 61 73  fo *pLock){.  as
cf4c0 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48  sert( unixMutexH
cf4d0 65 6c 64 28 29 20 29 3b 0a 20 20 69 66 28 20 70  eld() );.  if( p
cf4e0 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63  Lock ){.    pLoc
cf4f0 6b 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69  k->nRef--;.    i
cf500 66 28 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66 3d 3d  f( pLock->nRef==
cf510 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  0 ){.      if( p
cf520 4c 6f 63 6b 2d 3e 70 50 72 65 76 20 29 7b 0a 20  Lock->pPrev ){. 
cf530 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
cf540 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e 70 4e 65  Lock->pPrev->pNe
cf550 78 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20  xt==pLock );.   
cf560 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 50 72 65       pLock->pPre
cf570 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4c 6f 63 6b  v->pNext = pLock
cf580 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d  ->pNext;.      }
cf590 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73  else{.        as
cf5a0 73 65 72 74 28 20 6c 6f 63 6b 4c 69 73 74 3d 3d  sert( lockList==
cf5b0 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 20  pLock );.       
cf5c0 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 4c 6f 63   lockList = pLoc
cf5d0 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  k->pNext;.      
cf5e0 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63  }.      if( pLoc
cf5f0 6b 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20  k->pNext ){.    
cf600 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63      assert( pLoc
cf610 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d  k->pNext->pPrev=
cf620 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20  =pLock );.      
cf630 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e    pLock->pNext->
cf640 70 50 72 65 76 20 3d 20 70 4c 6f 63 6b 2d 3e 70  pPrev = pLock->p
cf650 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Prev;.      }.  
cf660 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
cf670 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 7d 0a 20  (pLock);.    }. 
cf680 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65   }.}../*.** Rele
cf690 61 73 65 20 61 20 75 6e 69 78 4f 70 65 6e 43 6e  ase a unixOpenCn
cf6a0 74 20 73 74 72 75 63 74 75 72 65 20 70 72 65 76  t structure prev
cf6b0 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64  iously allocated
cf6c0 20 62 79 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f   by findLockInfo
cf6d0 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 75  ()..**.** The mu
cf6e0 74 65 78 20 65 6e 74 65 72 65 64 20 75 73 69 6e  tex entered usin
cf6f0 67 20 74 68 65 20 75 6e 69 78 45 6e 74 65 72 4d  g the unixEnterM
cf700 75 74 65 78 28 29 20 66 75 6e 63 74 69 6f 6e 20  utex() function 
cf710 6d 75 73 74 20 62 65 20 68 65 6c 64 0a 2a 2a 20  must be held.** 
cf720 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  when this functi
cf730 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f  on is called..*/
cf740 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c  .static void rel
cf750 65 61 73 65 4f 70 65 6e 43 6e 74 28 73 74 72 75  easeOpenCnt(stru
cf760 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a  ct unixOpenCnt *
cf770 70 4f 70 65 6e 29 7b 0a 20 20 61 73 73 65 72 74  pOpen){.  assert
cf780 28 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28  ( unixMutexHeld(
cf790 29 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 65 6e  ) );.  if( pOpen
cf7a0 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e 2d 3e 6e   ){.    pOpen->n
cf7b0 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70  Ref--;.    if( p
cf7c0 4f 70 65 6e 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b  Open->nRef==0 ){
cf7d0 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e  .      if( pOpen
cf7e0 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 20  ->pPrev ){.     
cf7f0 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 65 6e     assert( pOpen
cf800 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d  ->pPrev->pNext==
cf810 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20  pOpen );.       
cf820 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70   pOpen->pPrev->p
cf830 4e 65 78 74 20 3d 20 70 4f 70 65 6e 2d 3e 70 4e  Next = pOpen->pN
cf840 65 78 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ext;.      }else
cf850 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
cf860 28 20 6f 70 65 6e 4c 69 73 74 3d 3d 70 4f 70 65  ( openList==pOpe
cf870 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 6f 70 65  n );.        ope
cf880 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e 2d 3e 70  nList = pOpen->p
cf890 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Next;.      }.  
cf8a0 20 20 20 20 69 66 28 20 70 4f 70 65 6e 2d 3e 70      if( pOpen->p
cf8b0 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Next ){.        
cf8c0 61 73 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 70  assert( pOpen->p
cf8d0 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 4f 70  Next->pPrev==pOp
cf8e0 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4f  en );.        pO
cf8f0 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65  pen->pNext->pPre
cf900 76 20 3d 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76  v = pOpen->pPrev
cf910 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 20 53 51  ;.      }.#if SQ
cf920 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20  LITE_THREADSAFE 
cf930 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e  && defined(__lin
cf940 75 78 5f 5f 29 0a 20 20 20 20 20 20 61 73 73 65  ux__).      asse
cf950 72 74 28 20 21 70 4f 70 65 6e 2d 3e 70 55 6e 75  rt( !pOpen->pUnu
cf960 73 65 64 20 7c 7c 20 74 68 72 65 61 64 73 4f 76  sed || threadsOv
cf970 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73  errideEachOthers
cf980 4c 6f 63 6b 73 3d 3d 30 20 29 3b 0a 23 65 6e 64  Locks==0 );.#end
cf990 69 66 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  if..      /* If 
cf9a0 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 20 69  pOpen->pUnused i
cf9b0 73 20 6e 6f 74 20 6e 75 6c 6c 2c 20 74 68 65 6e  s not null, then
cf9c0 20 6d 65 6d 6f 72 79 20 61 6e 64 20 66 69 6c 65   memory and file
cf9d0 2d 64 65 73 63 72 69 70 74 6f 72 73 0a 20 20 20  -descriptors.   
cf9e0 20 20 20 2a 2a 20 61 72 65 20 6c 65 61 6b 65 64     ** are leaked
cf9f0 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
cfa00 20 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 6f 6e   ** This will on
cfa10 6c 79 20 68 61 70 70 65 6e 20 69 66 2c 20 75 6e  ly happen if, un
cfa20 64 65 72 20 4c 69 6e 75 78 74 68 72 65 61 64 73  der Linuxthreads
cfa30 2c 20 74 68 65 20 75 73 65 72 20 68 61 73 20 6f  , the user has o
cfa40 70 65 6e 65 64 0a 20 20 20 20 20 20 2a 2a 20 61  pened.      ** a
cfa50 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20   transaction in 
cfa60 6f 6e 65 20 74 68 72 65 61 64 2c 20 74 68 65 6e  one thread, then
cfa70 20 61 74 74 65 6d 70 74 73 20 74 6f 20 63 6c 6f   attempts to clo
cfa80 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  se the database.
cfa90 20 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 20        ** handle 
cfaa0 66 72 6f 6d 20 61 6e 6f 74 68 65 72 20 74 68 72  from another thr
cfab0 65 61 64 20 28 77 69 74 68 6f 75 74 20 66 69 72  ead (without fir
cfac0 73 74 20 75 6e 6c 6f 63 6b 69 6e 67 20 74 68 65  st unlocking the
cfad0 20 64 62 20 66 69 6c 65 29 2e 0a 20 20 20 20 20   db file)..     
cfae0 20 2a 2a 20 54 68 69 73 20 69 73 20 61 20 6d 69   ** This is a mi
cfaf0 73 75 73 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20  suse.  */.      
cfb00 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4f 70  sqlite3_free(pOp
cfb10 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  en);.    }.  }.}
cfb20 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20  ../*.** Given a 
cfb30 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2c  file descriptor,
cfb40 20 6c 6f 63 61 74 65 20 75 6e 69 78 4c 6f 63 6b   locate unixLock
cfb50 49 6e 66 6f 20 61 6e 64 20 75 6e 69 78 4f 70 65  Info and unixOpe
cfb60 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 73 20  nCnt structures 
cfb70 74 68 61 74 0a 2a 2a 20 64 65 73 63 72 69 62 65  that.** describe
cfb80 73 20 74 68 61 74 20 66 69 6c 65 20 64 65 73 63  s that file desc
cfb90 72 69 70 74 6f 72 2e 20 20 43 72 65 61 74 65 20  riptor.  Create 
cfba0 6e 65 77 20 6f 6e 65 73 20 69 66 20 6e 65 63 65  new ones if nece
cfbb0 73 73 61 72 79 2e 20 20 54 68 65 0a 2a 2a 20 72  ssary.  The.** r
cfbc0 65 74 75 72 6e 20 76 61 6c 75 65 73 20 6d 69 67  eturn values mig
cfbd0 68 74 20 62 65 20 75 6e 69 6e 69 74 69 61 6c 69  ht be uninitiali
cfbe0 7a 65 64 20 69 66 20 61 6e 20 65 72 72 6f 72 20  zed if an error 
cfbf0 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  occurs..**.** Th
cfc00 65 20 6d 75 74 65 78 20 65 6e 74 65 72 65 64 20  e mutex entered 
cfc10 75 73 69 6e 67 20 74 68 65 20 75 6e 69 78 45 6e  using the unixEn
cfc20 74 65 72 4d 75 74 65 78 28 29 20 66 75 6e 63 74  terMutex() funct
cfc30 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 65 6c 64  ion must be held
cfc40 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 66 75  .** when this fu
cfc50 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
cfc60 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ..**.** Return a
cfc70 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72  n appropriate er
cfc80 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61  ror code..*/.sta
cfc90 74 69 63 20 69 6e 74 20 66 69 6e 64 4c 6f 63 6b  tic int findLock
cfca0 49 6e 66 6f 28 0a 20 20 75 6e 69 78 46 69 6c 65  Info(.  unixFile
cfcb0 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 20 20 20   *pFile,        
cfcc0 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 78 20 66         /* Unix f
cfcd0 69 6c 65 20 77 69 74 68 20 66 69 6c 65 20 64 65  ile with file de
cfce0 73 63 20 75 73 65 64 20 69 6e 20 74 68 65 20 6b  sc used in the k
cfcf0 65 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75  ey */.  struct u
cfd00 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 2a 70 70  nixLockInfo **pp
cfd10 4c 6f 63 6b 2c 20 20 2f 2a 20 52 65 74 75 72 6e  Lock,  /* Return
cfd20 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66   the unixLockInf
cfd30 6f 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65  o structure here
cfd40 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69   */.  struct uni
cfd50 78 4f 70 65 6e 43 6e 74 20 2a 2a 70 70 4f 70 65  xOpenCnt **ppOpe
cfd60 6e 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74  n    /* Return t
cfd70 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73  he unixOpenCnt s
cfd80 74 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f  tructure here */
cfd90 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  .){.  int rc;   
cfda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cfdb0 20 20 20 20 20 2f 2a 20 53 79 73 74 65 6d 20 63       /* System c
cfdc0 61 6c 6c 20 72 65 74 75 72 6e 20 63 6f 64 65 20  all return code 
cfdd0 2a 2f 0a 20 20 69 6e 74 20 66 64 3b 20 20 20 20  */.  int fd;    
cfde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cfdf0 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20      /* The file 
cfe00 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 70  descriptor for p
cfe10 46 69 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  File */.  struct
cfe20 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 6c 6f 63   unixLockKey loc
cfe30 6b 4b 65 79 3b 20 20 20 20 2f 2a 20 4c 6f 6f 6b  kKey;    /* Look
cfe40 75 70 20 6b 65 79 20 66 6f 72 20 74 68 65 20 75  up key for the u
cfe50 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75  nixLockInfo stru
cfe60 63 74 75 72 65 20 2a 2f 0a 20 20 73 74 72 75 63  cture */.  struc
cfe70 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66 69 6c  t unixFileId fil
cfe80 65 49 64 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 6f  eId;      /* Loo
cfe90 6b 75 70 20 6b 65 79 20 66 6f 72 20 74 68 65 20  kup key for the 
cfea0 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75  unixOpenCnt stru
cfeb0 63 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73  ct */.  struct s
cfec0 74 61 74 20 73 74 61 74 62 75 66 3b 20 20 20 20  tat statbuf;    
cfed0 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65         /* Low-le
cfee0 76 65 6c 20 66 69 6c 65 20 69 6e 66 6f 72 6d 61  vel file informa
cfef0 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74  tion */.  struct
cff00 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70   unixLockInfo *p
cff10 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 43 61 6e 64  Lock = 0;/* Cand
cff20 69 64 61 74 65 20 75 6e 69 78 4c 6f 63 6b 49 6e  idate unixLockIn
cff30 66 6f 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73  fo object */.  s
cff40 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e  truct unixOpenCn
cff50 74 20 2a 70 4f 70 65 6e 3b 20 20 20 20 20 2f 2a  t *pOpen;     /*
cff60 20 43 61 6e 64 69 64 61 74 65 20 75 6e 69 78 4f   Candidate unixO
cff70 70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 20 2a 2f  penCnt object */
cff80 0a 0a 20 20 61 73 73 65 72 74 28 20 75 6e 69 78  ..  assert( unix
cff90 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b 0a 0a  MutexHeld() );..
cffa0 20 20 2f 2a 20 47 65 74 20 6c 6f 77 2d 6c 65 76    /* Get low-lev
cffb0 65 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  el information a
cffc0 62 6f 75 74 20 74 68 65 20 66 69 6c 65 20 74 68  bout the file th
cffd0 61 74 20 77 65 20 63 61 6e 20 75 73 65 64 20 74  at we can used t
cffe0 6f 0a 20 20 2a 2a 20 63 72 65 61 74 65 20 61 20  o.  ** create a 
cfff0 75 6e 69 71 75 65 20 6e 61 6d 65 20 66 6f 72 20  unique name for 
d0000 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20  the file..  */. 
d0010 20 66 64 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a   fd = pFile->h;.
d0020 20 20 72 63 20 3d 20 66 73 74 61 74 28 66 64 2c    rc = fstat(fd,
d0030 20 26 73 74 61 74 62 75 66 29 3b 0a 20 20 69 66   &statbuf);.  if
d0040 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 70  ( rc!=0 ){.    p
d0050 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
d0060 3d 20 65 72 72 6e 6f 3b 0a 23 69 66 64 65 66 20  = errno;.#ifdef 
d0070 45 4f 56 45 52 46 4c 4f 57 0a 20 20 20 20 69 66  EOVERFLOW.    if
d0080 28 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72  ( pFile->lastErr
d0090 6e 6f 3d 3d 45 4f 56 45 52 46 4c 4f 57 20 29 20  no==EOVERFLOW ) 
d00a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
d00b0 4c 46 53 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  LFS;.#endif.    
d00c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
d00d0 45 52 52 3b 0a 20 20 7d 0a 0a 23 69 66 64 65 66  ERR;.  }..#ifdef
d00e0 20 5f 5f 41 50 50 4c 45 5f 5f 0a 20 20 2f 2a 20   __APPLE__.  /* 
d00f0 4f 6e 20 4f 53 20 58 20 6f 6e 20 61 6e 20 6d 73  On OS X on an ms
d0100 64 6f 73 20 66 69 6c 65 73 79 73 74 65 6d 2c 20  dos filesystem, 
d0110 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72  the inode number
d0120 20 69 73 20 72 65 70 6f 72 74 65 64 0a 20 20 2a   is reported.  *
d0130 2a 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 66 6f  * incorrectly fo
d0140 72 20 7a 65 72 6f 2d 73 69 7a 65 20 66 69 6c 65  r zero-size file
d0150 73 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 23  s.  See ticket #
d0160 33 32 36 30 2e 20 20 54 6f 20 77 6f 72 6b 0a 20  3260.  To work. 
d0170 20 2a 2a 20 61 72 6f 75 6e 64 20 74 68 69 73 20   ** around this 
d0180 70 72 6f 62 6c 65 6d 20 28 77 65 20 63 6f 6e 73  problem (we cons
d0190 69 64 65 72 20 69 74 20 61 20 62 75 67 20 69 6e  ider it a bug in
d01a0 20 4f 53 20 58 2c 20 6e 6f 74 20 53 51 4c 69 74   OS X, not SQLit
d01b0 65 29 0a 20 20 2a 2a 20 77 65 20 61 6c 77 61 79  e).  ** we alway
d01c0 73 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 66  s increase the f
d01d0 69 6c 65 20 73 69 7a 65 20 74 6f 20 31 20 62 79  ile size to 1 by
d01e0 20 77 72 69 74 69 6e 67 20 61 20 73 69 6e 67 6c   writing a singl
d01f0 65 20 62 79 74 65 0a 20 20 2a 2a 20 70 72 69 6f  e byte.  ** prio
d0200 72 20 74 6f 20 61 63 63 65 73 73 69 6e 67 20 74  r to accessing t
d0210 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 2e  he inode number.
d0220 20 20 54 68 65 20 6f 6e 65 20 62 79 74 65 20 77    The one byte w
d0230 72 69 74 74 65 6e 20 69 73 0a 20 20 2a 2a 20 61  ritten is.  ** a
d0240 6e 20 41 53 43 49 49 20 27 53 27 20 63 68 61 72  n ASCII 'S' char
d0250 61 63 74 65 72 20 77 68 69 63 68 20 61 6c 73 6f  acter which also
d0260 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20 74   happens to be t
d0270 68 65 20 66 69 72 73 74 20 62 79 74 65 0a 20 20  he first byte.  
d0280 2a 2a 20 69 6e 20 74 68 65 20 68 65 61 64 65 72  ** in the header
d0290 20 6f 66 20 65 76 65 72 79 20 53 51 4c 69 74 65   of every SQLite
d02a0 20 64 61 74 61 62 61 73 65 2e 20 20 49 6e 20 74   database.  In t
d02b0 68 69 73 20 77 61 79 2c 20 69 66 20 74 68 65 72  his way, if ther
d02c0 65 0a 20 20 2a 2a 20 69 73 20 61 20 72 61 63 65  e.  ** is a race
d02d0 20 63 6f 6e 64 69 74 69 6f 6e 20 73 75 63 68 20   condition such 
d02e0 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68 72  that another thr
d02f0 65 61 64 20 68 61 73 20 61 6c 72 65 61 64 79 20  ead has already 
d0300 70 6f 70 75 6c 61 74 65 64 0a 20 20 2a 2a 20 74  populated.  ** t
d0310 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66  he first page of
d0320 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 6e   the database, n
d0330 6f 20 64 61 6d 61 67 65 20 69 73 20 64 6f 6e 65  o damage is done
d0340 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 74 61  ..  */.  if( sta
d0350 74 62 75 66 2e 73 74 5f 73 69 7a 65 3d 3d 30 20  tbuf.st_size==0 
d0360 29 7b 0a 20 20 20 20 72 63 20 3d 20 77 72 69 74  ){.    rc = writ
d0370 65 28 66 64 2c 20 22 53 22 2c 20 31 29 3b 0a 20  e(fd, "S", 1);. 
d0380 20 20 20 69 66 28 20 72 63 21 3d 31 20 29 7b 0a     if( rc!=1 ){.
d0390 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
d03a0 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 20 20 7d  ITE_IOERR;.    }
d03b0 0a 20 20 20 20 72 63 20 3d 20 66 73 74 61 74 28  .    rc = fstat(
d03c0 66 64 2c 20 26 73 74 61 74 62 75 66 29 3b 0a 20  fd, &statbuf);. 
d03d0 20 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a     if( rc!=0 ){.
d03e0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
d03f0 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
d0400 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
d0410 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 20 20 7d  ITE_IOERR;.    }
d0420 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 6d  .  }.#endif..  m
d0430 65 6d 73 65 74 28 26 6c 6f 63 6b 4b 65 79 2c 20  emset(&lockKey, 
d0440 30 2c 20 73 69 7a 65 6f 66 28 6c 6f 63 6b 4b 65  0, sizeof(lockKe
d0450 79 29 29 3b 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66  y));.  lockKey.f
d0460 69 64 2e 64 65 76 20 3d 20 73 74 61 74 62 75 66  id.dev = statbuf
d0470 2e 73 74 5f 64 65 76 3b 0a 23 69 66 20 4f 53 5f  .st_dev;.#if OS_
d0480 56 58 57 4f 52 4b 53 0a 20 20 6c 6f 63 6b 4b 65  VXWORKS.  lockKe
d0490 79 2e 66 69 64 2e 70 49 64 20 3d 20 70 46 69 6c  y.fid.pId = pFil
d04a0 65 2d 3e 70 49 64 3b 0a 23 65 6c 73 65 0a 20 20  e->pId;.#else.  
d04b0 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e 69 6e 6f 20  lockKey.fid.ino 
d04c0 3d 20 73 74 61 74 62 75 66 2e 73 74 5f 69 6e 6f  = statbuf.st_ino
d04d0 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c  ;.#endif.#if SQL
d04e0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26  ITE_THREADSAFE &
d04f0 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75  & defined(__linu
d0500 78 5f 5f 29 0a 20 20 69 66 28 20 74 68 72 65 61  x__).  if( threa
d0510 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74  dsOverrideEachOt
d0520 68 65 72 73 4c 6f 63 6b 73 3c 30 20 29 7b 0a 20  hersLocks<0 ){. 
d0530 20 20 20 74 65 73 74 54 68 72 65 61 64 4c 6f 63     testThreadLoc
d0540 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 66 64 29  kingBehavior(fd)
d0550 3b 0a 20 20 7d 0a 20 20 6c 6f 63 6b 4b 65 79 2e  ;.  }.  lockKey.
d0560 74 69 64 20 3d 20 74 68 72 65 61 64 73 4f 76 65  tid = threadsOve
d0570 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c  rrideEachOthersL
d0580 6f 63 6b 73 20 3f 20 30 20 3a 20 70 74 68 72 65  ocks ? 0 : pthre
d0590 61 64 5f 73 65 6c 66 28 29 3b 0a 23 65 6e 64 69  ad_self();.#endi
d05a0 66 0a 20 20 66 69 6c 65 49 64 20 3d 20 6c 6f 63  f.  fileId = loc
d05b0 6b 4b 65 79 2e 66 69 64 3b 0a 20 20 69 66 28 20  kKey.fid;.  if( 
d05c0 70 70 4c 6f 63 6b 21 3d 30 20 29 7b 0a 20 20 20  ppLock!=0 ){.   
d05d0 20 70 4c 6f 63 6b 20 3d 20 6c 6f 63 6b 4c 69 73   pLock = lockLis
d05e0 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 4c  t;.    while( pL
d05f0 6f 63 6b 20 26 26 20 6d 65 6d 63 6d 70 28 26 6c  ock && memcmp(&l
d0600 6f 63 6b 4b 65 79 2c 20 26 70 4c 6f 63 6b 2d 3e  ockKey, &pLock->
d0610 6c 6f 63 6b 4b 65 79 2c 20 73 69 7a 65 6f 66 28  lockKey, sizeof(
d0620 6c 6f 63 6b 4b 65 79 29 29 20 29 7b 0a 20 20 20  lockKey)) ){.   
d0630 20 20 20 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b     pLock = pLock
d0640 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20  ->pNext;.    }. 
d0650 20 20 20 69 66 28 20 70 4c 6f 63 6b 3d 3d 30 20     if( pLock==0 
d0660 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d  ){.      pLock =
d0670 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
d0680 20 73 69 7a 65 6f 66 28 2a 70 4c 6f 63 6b 29 20   sizeof(*pLock) 
d0690 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f  );.      if( pLo
d06a0 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ck==0 ){.       
d06b0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
d06c0 45 4d 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  EM;.        goto
d06d0 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e   exit_findlockin
d06e0 66 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  fo;.      }.    
d06f0 20 20 6d 65 6d 63 70 79 28 26 70 4c 6f 63 6b 2d    memcpy(&pLock-
d0700 3e 6c 6f 63 6b 4b 65 79 2c 26 6c 6f 63 6b 4b 65  >lockKey,&lockKe
d0710 79 2c 73 69 7a 65 6f 66 28 6c 6f 63 6b 4b 65 79  y,sizeof(lockKey
d0720 29 29 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d  ));.      pLock-
d0730 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20  >nRef = 1;.     
d0740 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 3d 20 30 3b   pLock->cnt = 0;
d0750 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f  .      pLock->lo
d0760 63 6b 74 79 70 65 20 3d 20 30 3b 0a 20 20 20 20  cktype = 0;.    
d0770 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d    pLock->pNext =
d0780 20 6c 6f 63 6b 4c 69 73 74 3b 0a 20 20 20 20 20   lockList;.     
d0790 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 20 3d 20   pLock->pPrev = 
d07a0 30 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 6f 63  0;.      if( loc
d07b0 6b 4c 69 73 74 20 29 20 6c 6f 63 6b 4c 69 73 74  kList ) lockList
d07c0 2d 3e 70 50 72 65 76 20 3d 20 70 4c 6f 63 6b 3b  ->pPrev = pLock;
d07d0 0a 20 20 20 20 20 20 6c 6f 63 6b 4c 69 73 74 20  .      lockList 
d07e0 3d 20 70 4c 6f 63 6b 3b 0a 20 20 20 20 7d 65 6c  = pLock;.    }el
d07f0 73 65 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d  se{.      pLock-
d0800 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20  >nRef++;.    }. 
d0810 20 20 20 2a 70 70 4c 6f 63 6b 20 3d 20 70 4c 6f     *ppLock = pLo
d0820 63 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 70  ck;.  }.  if( pp
d0830 4f 70 65 6e 21 3d 30 20 29 7b 0a 20 20 20 20 70  Open!=0 ){.    p
d0840 4f 70 65 6e 20 3d 20 6f 70 65 6e 4c 69 73 74 3b  Open = openList;
d0850 0a 20 20 20 20 77 68 69 6c 65 28 20 70 4f 70 65  .    while( pOpe
d0860 6e 20 26 26 20 6d 65 6d 63 6d 70 28 26 66 69 6c  n && memcmp(&fil
d0870 65 49 64 2c 20 26 70 4f 70 65 6e 2d 3e 66 69 6c  eId, &pOpen->fil
d0880 65 49 64 2c 20 73 69 7a 65 6f 66 28 66 69 6c 65  eId, sizeof(file
d0890 49 64 29 29 20 29 7b 0a 20 20 20 20 20 20 70 4f  Id)) ){.      pO
d08a0 70 65 6e 20 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65  pen = pOpen->pNe
d08b0 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  xt;.    }.    if
d08c0 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20  ( pOpen==0 ){.  
d08d0 20 20 20 20 70 4f 70 65 6e 20 3d 20 73 71 6c 69      pOpen = sqli
d08e0 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65  te3_malloc( size
d08f0 6f 66 28 2a 70 4f 70 65 6e 29 20 29 3b 0a 20 20  of(*pOpen) );.  
d0900 20 20 20 20 69 66 28 20 70 4f 70 65 6e 3d 3d 30      if( pOpen==0
d0910 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65   ){.        rele
d0920 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 4c 6f 63  aseLockInfo(pLoc
d0930 6b 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  k);.        rc =
d0940 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
d0950 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74         goto exit
d0960 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20  _findlockinfo;. 
d0970 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d       }.      mem
d0980 73 65 74 28 70 4f 70 65 6e 2c 20 30 2c 20 73 69  set(pOpen, 0, si
d0990 7a 65 6f 66 28 2a 70 4f 70 65 6e 29 29 3b 0a 20  zeof(*pOpen));. 
d09a0 20 20 20 20 20 70 4f 70 65 6e 2d 3e 66 69 6c 65       pOpen->file
d09b0 49 64 20 3d 20 66 69 6c 65 49 64 3b 0a 20 20 20  Id = fileId;.   
d09c0 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 20 3d     pOpen->nRef =
d09d0 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d   1;.      pOpen-
d09e0 3e 70 4e 65 78 74 20 3d 20 6f 70 65 6e 4c 69 73  >pNext = openLis
d09f0 74 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 65  t;.      if( ope
d0a00 6e 4c 69 73 74 20 29 20 6f 70 65 6e 4c 69 73 74  nList ) openList
d0a10 2d 3e 70 50 72 65 76 20 3d 20 70 4f 70 65 6e 3b  ->pPrev = pOpen;
d0a20 0a 20 20 20 20 20 20 6f 70 65 6e 4c 69 73 74 20  .      openList 
d0a30 3d 20 70 4f 70 65 6e 3b 0a 20 20 20 20 7d 65 6c  = pOpen;.    }el
d0a40 73 65 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d  se{.      pOpen-
d0a50 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20  >nRef++;.    }. 
d0a60 20 20 20 2a 70 70 4f 70 65 6e 20 3d 20 70 4f 70     *ppOpen = pOp
d0a70 65 6e 3b 0a 20 20 7d 0a 0a 65 78 69 74 5f 66 69  en;.  }..exit_fi
d0a80 6e 64 6c 6f 63 6b 69 6e 66 6f 3a 0a 20 20 72 65  ndlockinfo:.  re
d0a90 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
d0aa0 2a 20 49 66 20 77 65 20 61 72 65 20 63 75 72 72  * If we are curr
d0ab0 65 6e 74 6c 79 20 69 6e 20 61 20 64 69 66 66 65  ently in a diffe
d0ac0 72 65 6e 74 20 74 68 72 65 61 64 20 74 68 61 6e  rent thread than
d0ad0 20 74 68 65 20 74 68 72 65 61 64 20 74 68 61 74   the thread that
d0ae0 20 74 68 65 0a 2a 2a 20 75 6e 69 78 46 69 6c 65   the.** unixFile
d0af0 20 61 72 67 75 6d 65 6e 74 20 62 65 6c 6f 6e 67   argument belong
d0b00 73 20 74 6f 2c 20 74 68 65 6e 20 74 72 61 6e 73  s to, then trans
d0b10 66 65 72 20 6f 77 6e 65 72 73 68 69 70 20 6f 66  fer ownership of
d0b20 20 74 68 65 20 75 6e 69 78 46 69 6c 65 0a 2a 2a   the unixFile.**
d0b30 20 6f 76 65 72 20 74 6f 20 74 68 65 20 63 75 72   over to the cur
d0b40 72 65 6e 74 20 74 68 72 65 61 64 2e 0a 2a 2a 0a  rent thread..**.
d0b50 2a 2a 20 41 20 75 6e 69 78 46 69 6c 65 20 69 73  ** A unixFile is
d0b60 20 6f 6e 6c 79 20 6f 77 6e 65 64 20 62 79 20 61   only owned by a
d0b70 20 74 68 72 65 61 64 20 6f 6e 20 73 79 73 74 65   thread on syste
d0b80 6d 73 20 74 68 61 74 20 75 73 65 20 4c 69 6e 75  ms that use Linu
d0b90 78 54 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20  xThreads..**.** 
d0ba0 4f 77 6e 65 72 73 68 69 70 20 74 72 61 6e 73 66  Ownership transf
d0bb0 65 72 20 69 73 20 6f 6e 6c 79 20 61 6c 6c 6f 77  er is only allow
d0bc0 65 64 20 69 66 20 74 68 65 20 75 6e 69 78 46 69  ed if the unixFi
d0bd0 6c 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  le is currently 
d0be0 75 6e 6c 6f 63 6b 65 64 2e 0a 2a 2a 20 49 66 20  unlocked..** If 
d0bf0 74 68 65 20 75 6e 69 78 46 69 6c 65 20 69 73 20  the unixFile is 
d0c00 6c 6f 63 6b 65 64 20 61 6e 64 20 61 6e 20 6f 77  locked and an ow
d0c10 6e 65 72 73 68 69 70 20 69 73 20 77 72 6f 6e 67  nership is wrong
d0c20 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 0a 2a 2a  , then return.**
d0c30 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2e 20   SQLITE_MISUSE. 
d0c40 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
d0c50 74 75 72 6e 65 64 20 69 66 20 65 76 65 72 79 74  turned if everyt
d0c60 68 69 6e 67 20 77 6f 72 6b 73 2e 0a 2a 2f 0a 23  hing works..*/.#
d0c70 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
d0c80 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28  SAFE && defined(
d0c90 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 73 74 61 74 69  __linux__).stati
d0ca0 63 20 69 6e 74 20 74 72 61 6e 73 66 65 72 4f 77  c int transferOw
d0cb0 6e 65 72 73 68 69 70 28 75 6e 69 78 46 69 6c 65  nership(unixFile
d0cc0 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20   *pFile){.  int 
d0cd0 72 63 3b 0a 20 20 70 74 68 72 65 61 64 5f 74 20  rc;.  pthread_t 
d0ce0 68 53 65 6c 66 3b 0a 20 20 69 66 28 20 74 68 72  hSelf;.  if( thr
d0cf0 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68  eadsOverrideEach
d0d00 4f 74 68 65 72 73 4c 6f 63 6b 73 20 29 7b 0a 20  OthersLocks ){. 
d0d10 20 20 20 2f 2a 20 4f 77 6e 65 72 73 68 69 70 20     /* Ownership 
d0d20 74 72 61 6e 73 66 65 72 73 20 6e 6f 74 20 6e 65  transfers not ne
d0d30 65 64 65 64 20 6f 6e 20 74 68 69 73 20 73 79 73  eded on this sys
d0d40 74 65 6d 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  tem */.    retur
d0d50 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
d0d60 0a 20 20 68 53 65 6c 66 20 3d 20 70 74 68 72 65  .  hSelf = pthre
d0d70 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 69 66 28  ad_self();.  if(
d0d80 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70   pthread_equal(p
d0d90 46 69 6c 65 2d 3e 74 69 64 2c 20 68 53 65 6c 66  File->tid, hSelf
d0da0 29 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 61  ) ){.    /* We a
d0db0 72 65 20 73 74 69 6c 6c 20 69 6e 20 74 68 65 20  re still in the 
d0dc0 73 61 6d 65 20 74 68 72 65 61 64 20 2a 2f 0a 20  same thread */. 
d0dd0 20 20 20 4f 53 54 52 41 43 45 31 28 22 4e 6f 2d     OSTRACE1("No-
d0de0 74 72 61 6e 73 66 65 72 2c 20 73 61 6d 65 20 74  transfer, same t
d0df0 68 72 65 61 64 5c 6e 22 29 3b 0a 20 20 20 20 72  hread\n");.    r
d0e00 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
d0e10 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 69 6c 65  .  }.  if( pFile
d0e20 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c  ->locktype!=NO_L
d0e30 4f 43 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65  OCK ){.    /* We
d0e40 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20 6f   cannot change o
d0e50 77 6e 65 72 73 68 69 70 20 77 68 69 6c 65 20 77  wnership while w
d0e60 65 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 61 20  e are holding a 
d0e70 6c 6f 63 6b 21 20 2a 2f 0a 20 20 20 20 72 65 74  lock! */.    ret
d0e80 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
d0e90 45 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45  E;.  }.  OSTRACE
d0ea0 34 28 22 54 72 61 6e 73 66 65 72 20 6f 77 6e 65  4("Transfer owne
d0eb0 72 73 68 69 70 20 6f 66 20 25 64 20 66 72 6f 6d  rship of %d from
d0ec0 20 25 64 20 74 6f 20 25 64 5c 6e 22 2c 0a 20 20   %d to %d\n",.  
d0ed0 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d            pFile-
d0ee0 3e 68 2c 20 70 46 69 6c 65 2d 3e 74 69 64 2c 20  >h, pFile->tid, 
d0ef0 68 53 65 6c 66 29 3b 0a 20 20 70 46 69 6c 65 2d  hSelf);.  pFile-
d0f00 3e 74 69 64 20 3d 20 68 53 65 6c 66 3b 0a 20 20  >tid = hSelf;.  
d0f10 69 66 20 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b  if (pFile->pLock
d0f20 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 20 20 20 20   != NULL) {.    
d0f30 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28  releaseLockInfo(
d0f40 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20  pFile->pLock);. 
d0f50 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b     rc = findLock
d0f60 49 6e 66 6f 28 70 46 69 6c 65 2c 20 26 70 46 69  Info(pFile, &pFi
d0f70 6c 65 2d 3e 70 4c 6f 63 6b 2c 20 30 29 3b 0a 20  le->pLock, 0);. 
d0f80 20 20 20 4f 53 54 52 41 43 45 35 28 22 4c 4f 43     OSTRACE5("LOC
d0f90 4b 20 20 20 20 25 64 20 69 73 20 6e 6f 77 20 25  K    %d is now %
d0fa0 73 28 25 73 2c 25 64 29 5c 6e 22 2c 20 70 46 69  s(%s,%d)\n", pFi
d0fb0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20  le->h,.         
d0fc0 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70    locktypeName(p
d0fd0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c  File->locktype),
d0fe0 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b  .           lock
d0ff0 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e  typeName(pFile->
d1000 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29  pLock->locktype)
d1010 2c 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e  , pFile->pLock->
d1020 63 6e 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  cnt);.    return
d1030 20 72 63 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a   rc;.  } else {.
d1040 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d1050 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73  E_OK;.  }.}.#els
d1060 65 20 20 2f 2a 20 69 66 20 6e 6f 74 20 53 51 4c  e  /* if not SQL
d1070 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 2a  ITE_THREADSAFE *
d1080 2f 0a 20 20 2f 2a 20 4f 6e 20 73 69 6e 67 6c 65  /.  /* On single
d1090 2d 74 68 72 65 61 64 65 64 20 62 75 69 6c 64 73  -threaded builds
d10a0 2c 20 6f 77 6e 65 72 73 68 69 70 20 74 72 61 6e  , ownership tran
d10b0 73 66 65 72 20 69 73 20 61 20 6e 6f 2d 6f 70 20  sfer is a no-op 
d10c0 2a 2f 0a 23 20 64 65 66 69 6e 65 20 74 72 61 6e  */.# define tran
d10d0 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 58 29  sferOwnership(X)
d10e0 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69   SQLITE_OK.#endi
d10f0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 48 52 45  f /* SQLITE_THRE
d1100 41 44 53 41 46 45 20 2a 2f 0a 0a 0a 2f 2a 0a 2a  ADSAFE */.../*.*
d1110 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
d1120 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69  hecks if there i
d1130 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  s a RESERVED loc
d1140 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70  k held on the sp
d1150 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20  ecified.** file 
d1160 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f  by this or any o
d1170 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66  ther process. If
d1180 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20   such a lock is 
d1190 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f  held, set *pResO
d11a0 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a  ut.** to a non-z
d11b0 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77  ero value otherw
d11c0 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20  ise *pResOut is 
d11d0 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68  set to zero.  Th
d11e0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a  e return value.*
d11f0 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49  * is set to SQLI
d1200 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20  TE_OK unless an 
d1210 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73  I/O error occurs
d1220 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65   during lock che
d1230 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  cking..*/.static
d1240 20 69 6e 74 20 75 6e 69 78 43 68 65 63 6b 52 65   int unixCheckRe
d1250 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74  servedLock(sqlit
d1260 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
d1270 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e   *pResOut){.  in
d1280 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
d1290 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64  ;.  int reserved
d12a0 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65   = 0;.  unixFile
d12b0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
d12c0 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 53 69 6d 75  ile*)id;..  Simu
d12d0 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74  lateIOError( ret
d12e0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
d12f0 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f  _CHECKRESERVEDLO
d1300 43 4b 3b 20 29 3b 0a 0a 20 20 61 73 73 65 72 74  CK; );..  assert
d1310 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 75 6e 69  ( pFile );.  uni
d1320 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 20 2f  xEnterMutex(); /
d1330 2a 20 42 65 63 61 75 73 65 20 70 46 69 6c 65 2d  * Because pFile-
d1340 3e 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 64  >pLock is shared
d1350 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 20   across threads 
d1360 2a 2f 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69  */..  /* Check i
d1370 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 68  f a thread in th
d1380 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73  is process holds
d1390 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a   such a lock */.
d13a0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4c 6f    if( pFile->pLo
d13b0 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41  ck->locktype>SHA
d13c0 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  RED_LOCK ){.    
d13d0 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20  reserved = 1;.  
d13e0 7d 0a 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73  }..  /* Otherwis
d13f0 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74  e see if some ot
d1400 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64  her process hold
d1410 73 20 69 74 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64  s it..  */.#ifnd
d1420 65 66 20 5f 5f 44 4a 47 50 50 5f 5f 0a 20 20 69  ef __DJGPP__.  i
d1430 66 28 20 21 72 65 73 65 72 76 65 64 20 29 7b 0a  f( !reserved ){.
d1440 20 20 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b      struct flock
d1450 20 6c 6f 63 6b 3b 0a 20 20 20 20 6c 6f 63 6b 2e   lock;.    lock.
d1460 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f  l_whence = SEEK_
d1470 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f  SET;.    lock.l_
d1480 73 74 61 72 74 20 3d 20 52 45 53 45 52 56 45 44  start = RESERVED
d1490 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e  _BYTE;.    lock.
d14a0 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 20 20 6c  l_len = 1;.    l
d14b0 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57  ock.l_type = F_W
d14c0 52 4c 43 4b 3b 0a 20 20 20 20 69 66 20 28 2d 31  RLCK;.    if (-1
d14d0 20 3d 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d   == fcntl(pFile-
d14e0 3e 68 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f  >h, F_GETLK, &lo
d14f0 63 6b 29 29 20 7b 0a 20 20 20 20 20 20 69 6e 74  ck)) {.      int
d1500 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b   tErrno = errno;
d1510 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
d1520 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78  teErrorFromPosix
d1530 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51  Error(tErrno, SQ
d1540 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b  LITE_IOERR_CHECK
d1550 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 3b 0a 20  RESERVEDLOCK);. 
d1560 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
d1570 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a  Errno = tErrno;.
d1580 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28 20 6c      } else if( l
d1590 6f 63 6b 2e 6c 5f 74 79 70 65 21 3d 46 5f 55 4e  ock.l_type!=F_UN
d15a0 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 73  LCK ){.      res
d15b0 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d  erved = 1;.    }
d15c0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 0a 20  .  }.#endif.  . 
d15d0 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28   unixLeaveMutex(
d15e0 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54  );.  OSTRACE4("T
d15f0 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25  EST WR-LOCK %d %
d1600 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  d %d\n", pFile->
d1610 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29  h, rc, reserved)
d1620 3b 0a 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20  ;..  *pResOut = 
d1630 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75  reserved;.  retu
d1640 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
d1650 50 65 72 66 6f 72 6d 20 61 20 66 69 6c 65 20 6c  Perform a file l
d1660 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  ocking operation
d1670 20 6f 6e 20 61 20 72 61 6e 67 65 20 6f 66 20 62   on a range of b
d1680 79 74 65 73 20 69 6e 20 61 20 66 69 6c 65 2e 0a  ytes in a file..
d1690 2a 2a 20 54 68 65 20 22 6f 70 22 20 70 61 72 61  ** The "op" para
d16a0 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20  meter should be 
d16b0 6f 6e 65 20 6f 66 20 46 5f 52 44 4c 43 4b 2c 20  one of F_RDLCK, 
d16c0 46 5f 57 52 4c 43 4b 2c 20 6f 72 20 46 5f 55 4e  F_WRLCK, or F_UN
d16d0 4c 43 4b 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30  LCK..** Return 0
d16e0 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 2d   on success or -
d16f0 31 20 66 6f 72 20 66 61 69 6c 75 72 65 2e 20 20  1 for failure.  
d1700 4f 6e 20 66 61 69 6c 75 72 65 2c 20 77 72 69 74  On failure, writ
d1710 65 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63  e the error.** c
d1720 6f 64 65 20 69 6e 74 6f 20 2a 70 45 72 72 63 6f  ode into *pErrco
d1730 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  de..**.** If the
d1740 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49   SQLITE_WHOLE_FI
d1750 4c 45 5f 4c 4f 43 4b 49 4e 47 20 62 69 74 20 69  LE_LOCKING bit i
d1760 73 20 63 6c 65 61 72 2c 20 74 68 65 6e 20 6f 6e  s clear, then on
d1770 6c 79 20 6c 6f 63 6b 0a 2a 2a 20 74 68 65 20 72  ly lock.** the r
d1780 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20 6f 6e  ange of bytes on
d1790 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70 61 67   the locking pag
d17a0 65 20 62 65 74 77 65 65 6e 20 53 48 41 52 45 44  e between SHARED
d17b0 5f 46 49 52 53 54 20 61 6e 64 0a 2a 2a 20 53 48  _FIRST and.** SH
d17c0 41 52 45 44 5f 53 49 5a 45 2e 20 20 49 66 20 53  ARED_SIZE.  If S
d17d0 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45  QLITE_WHOLE_FILE
d17e0 5f 4c 4f 43 4b 49 4e 47 20 69 73 20 73 65 74 2c  _LOCKING is set,
d17f0 20 74 68 65 6e 20 6c 6f 63 6b 20 61 6c 6c 0a 2a   then lock all.*
d1800 2a 20 62 79 74 65 73 20 66 72 6f 6d 20 30 20 75  * bytes from 0 u
d1810 70 20 74 6f 20 62 75 74 20 6e 6f 74 20 69 6e 63  p to but not inc
d1820 6c 75 64 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42  luding PENDING_B
d1830 59 54 45 2c 20 61 6e 64 20 61 6c 6c 20 62 79 74  YTE, and all byt
d1840 65 73 0a 2a 2a 20 74 68 61 74 20 66 6f 6c 6c 6f  es.** that follo
d1850 77 20 53 48 41 52 45 44 5f 46 49 52 53 54 2e 0a  w SHARED_FIRST..
d1860 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77  **.** In other w
d1870 6f 72 64 73 2c 20 6f 66 20 53 51 4c 49 54 45 5f  ords, of SQLITE_
d1880 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49  WHOLE_FILE_LOCKI
d1890 4e 47 20 69 66 20 66 61 6c 73 65 20 28 74 68 65  NG if false (the
d18a0 20 68 69 73 74 6f 72 69 63 61 6c 0a 2a 2a 20 64   historical.** d
d18b0 65 66 61 75 6c 74 20 63 61 73 65 29 20 74 68 65  efault case) the
d18c0 6e 20 6f 6e 6c 79 20 6c 6f 63 6b 20 61 20 73 6d  n only lock a sm
d18d0 61 6c 6c 20 72 61 6e 67 65 20 6f 66 20 62 79 74  all range of byt
d18e0 65 73 20 66 72 6f 6d 20 53 48 41 52 45 44 5f 46  es from SHARED_F
d18f0 49 52 53 54 0a 2a 2a 20 74 68 72 6f 75 67 68 20  IRST.** through 
d1900 53 48 41 52 45 44 5f 46 49 52 53 54 2b 53 48 41  SHARED_FIRST+SHA
d1910 52 45 44 5f 53 49 5a 45 2d 31 2e 20 20 42 75 74  RED_SIZE-1.  But
d1920 20 69 66 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45   if SQLITE_WHOLE
d1930 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 69 73  _FILE_LOCKING is
d1940 0a 2a 2a 20 74 72 75 65 20 74 68 65 6e 20 6c 6f  .** true then lo
d1950 63 6b 20 65 76 65 72 79 20 62 79 74 65 20 69 6e  ck every byte in
d1960 20 74 68 65 20 66 69 6c 65 20 65 78 63 65 70 74   the file except
d1970 20 66 6f 72 20 50 45 4e 44 49 4e 47 5f 42 59 54   for PENDING_BYT
d1980 45 20 61 6e 64 0a 2a 2a 20 52 45 53 45 52 56 45  E and.** RESERVE
d1990 44 5f 42 59 54 45 2e 0a 2a 2a 0a 2a 2a 20 53 51  D_BYTE..**.** SQ
d19a0 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f  LITE_WHOLE_FILE_
d19b0 4c 4f 43 4b 49 4e 47 3d 74 72 75 65 20 6f 76 65  LOCKING=true ove
d19c0 72 6c 61 70 73 20 53 51 4c 49 54 45 5f 57 48 4f  rlaps SQLITE_WHO
d19d0 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3d  LE_FILE_LOCKING=
d19e0 66 61 6c 73 65 0a 2a 2a 20 61 6e 64 20 73 6f 20  false.** and so 
d19f0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 63 68 65  the locking sche
d1a00 6d 65 73 20 61 72 65 20 63 6f 6d 70 61 74 69 62  mes are compatib
d1a10 6c 65 2e 20 20 4f 6e 65 20 74 79 70 65 20 6f 66  le.  One type of
d1a20 20 6c 6f 63 6b 20 77 69 6c 6c 0a 2a 2a 20 65 66   lock will.** ef
d1a30 66 65 63 74 69 76 65 6c 79 20 65 78 63 6c 75 64  fectively exclud
d1a40 65 20 74 68 65 20 6f 74 68 65 72 20 74 79 70 65  e the other type
d1a50 2e 20 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f  .  The reason fo
d1a60 72 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 53  r using the.** S
d1a70 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45  QLITE_WHOLE_FILE
d1a80 5f 4c 4f 43 4b 49 4e 47 3d 74 72 75 65 20 69 73  _LOCKING=true is
d1a90 20 74 68 61 74 20 62 79 20 69 6e 64 69 63 61 74   that by indicat
d1aa0 69 6e 67 20 74 68 65 20 66 75 6c 6c 20 72 61 6e  ing the full ran
d1ab0 67 65 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 74  ge.** of bytes t
d1ac0 6f 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 69  o be read or wri
d1ad0 74 74 65 6e 2c 20 77 65 20 67 69 76 65 20 68 69  tten, we give hi
d1ae0 6e 74 73 20 74 6f 20 4e 46 53 20 74 6f 20 68 65  nts to NFS to he
d1af0 6c 70 20 69 74 0a 2a 2a 20 6d 61 69 6e 74 61 69  lp it.** maintai
d1b00 6e 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63  n cache coherenc
d1b10 79 2e 20 20 4f 6e 20 74 68 65 20 6f 74 68 65 72  y.  On the other
d1b20 20 68 61 6e 64 2c 20 77 68 6f 6c 65 20 66 69 6c   hand, whole fil
d1b30 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 69 73 20  e locking.** is 
d1b40 73 6c 6f 77 65 72 2c 20 73 6f 20 77 65 20 64 6f  slower, so we do
d1b50 6e 27 74 20 77 61 6e 74 20 74 6f 20 75 73 65 20  n't want to use 
d1b60 69 74 20 65 78 63 65 70 74 20 66 6f 72 20 4e 46  it except for NF
d1b70 53 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  S..*/.static int
d1b80 20 72 61 6e 67 65 4c 6f 63 6b 28 75 6e 69 78 46   rangeLock(unixF
d1b90 69 6c 65 20 2a 70 46 69 6c 65 2c 20 69 6e 74 20  ile *pFile, int 
d1ba0 6f 70 2c 20 69 6e 74 20 2a 70 45 72 72 63 6f 64  op, int *pErrcod
d1bb0 65 29 7b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f  e){.  struct flo
d1bc0 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 72  ck lock;.  int r
d1bd0 63 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65  c;.  lock.l_type
d1be0 20 3d 20 6f 70 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f   = op;.  lock.l_
d1bf0 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46  start = SHARED_F
d1c00 49 52 53 54 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 77  IRST;.  lock.l_w
d1c10 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54  hence = SEEK_SET
d1c20 3b 0a 20 20 69 66 28 20 28 70 46 69 6c 65 2d 3e  ;.  if( (pFile->
d1c30 66 69 6c 65 46 6c 61 67 73 20 26 20 53 51 4c 49  fileFlags & SQLI
d1c40 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f  TE_WHOLE_FILE_LO
d1c50 43 4b 49 4e 47 29 3d 3d 30 20 29 7b 0a 20 20 20  CKING)==0 ){.   
d1c60 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48   lock.l_len = SH
d1c70 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 72  ARED_SIZE;.    r
d1c80 63 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d  c = fcntl(pFile-
d1c90 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f  >h, F_SETLK, &lo
d1ca0 63 6b 29 3b 0a 20 20 20 20 2a 70 45 72 72 63 6f  ck);.    *pErrco
d1cb0 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 65  de = errno;.  }e
d1cc0 6c 73 65 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f  lse{.    lock.l_
d1cd0 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 72 63 20  len = 0;.    rc 
d1ce0 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68  = fcntl(pFile->h
d1cf0 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b  , F_SETLK, &lock
d1d00 29 3b 0a 20 20 20 20 2a 70 45 72 72 63 6f 64 65  );.    *pErrcode
d1d10 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66   = errno;.    if
d1d20 28 20 4e 45 56 45 52 28 6f 70 3d 3d 46 5f 55 4e  ( NEVER(op==F_UN
d1d30 4c 43 4b 29 20 7c 7c 20 72 63 21 3d 28 2d 31 29  LCK) || rc!=(-1)
d1d40 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c   ){.      lock.l
d1d50 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 20 20 20  _start = 0;.    
d1d60 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 50    lock.l_len = P
d1d70 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20  ENDING_BYTE;.   
d1d80 20 20 20 72 63 20 3d 20 66 63 6e 74 6c 28 70 46     rc = fcntl(pF
d1d90 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c  ile->h, F_SETLK,
d1da0 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 69   &lock);.      i
d1db0 66 28 20 41 4c 57 41 59 53 28 6f 70 21 3d 46 5f  f( ALWAYS(op!=F_
d1dc0 55 4e 4c 43 4b 29 20 26 26 20 72 63 3d 3d 28 2d  UNLCK) && rc==(-
d1dd0 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70  1) ){.        *p
d1de0 45 72 72 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b  Errcode = errno;
d1df0 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f  .        lock.l_
d1e00 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a  type = F_UNLCK;.
d1e10 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73          lock.l_s
d1e20 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f 46 49  tart = SHARED_FI
d1e30 52 53 54 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63  RST;.        loc
d1e40 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 3b 0a 20 20 20  k.l_len = 0;.   
d1e50 20 20 20 20 20 66 63 6e 74 6c 28 70 46 69 6c 65       fcntl(pFile
d1e60 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c  ->h, F_SETLK, &l
d1e70 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ock);.      }.  
d1e80 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
d1e90 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f   rc;.}../*.** Lo
d1ea0 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68  ck the file with
d1eb0 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66   the lock specif
d1ec0 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72  ied by parameter
d1ed0 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a   locktype - one.
d1ee0 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ** of the follow
d1ef0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28  ing:.**.**     (
d1f00 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a  1) SHARED_LOCK.*
d1f10 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56  *     (2) RESERV
d1f20 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28  ED_LOCK.**     (
d1f30 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a  3) PENDING_LOCK.
d1f40 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55  **     (4) EXCLU
d1f50 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20  SIVE_LOCK.**.** 
d1f60 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72  Sometimes when r
d1f70 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f  equesting one lo
d1f80 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69  ck state, additi
d1f90 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73  onal lock states
d1fa0 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64  .** are inserted
d1fb0 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68   in between.  Th
d1fc0 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20  e locking might 
d1fd0 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74  fail on one of t
d1fe0 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e  he later.** tran
d1ff0 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20  sitions leaving 
d2000 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64  the lock state d
d2010 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68  ifferent from wh
d2020 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75  at it started bu
d2030 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74  t.** still short
d2040 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54   of its goal.  T
d2050 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61  he following cha
d2060 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c  rt shows the all
d2070 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69  owed.** transiti
d2080 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65  ons and the inse
d2090 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74  rted intermediat
d20a0 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20  e states:.**.** 
d20b0 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53     UNLOCKED -> S
d20c0 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52  HARED.**    SHAR
d20d0 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a  ED -> RESERVED.*
d20e0 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28  *    SHARED -> (
d20f0 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c  PENDING) -> EXCL
d2100 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45  USIVE.**    RESE
d2110 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47  RVED -> (PENDING
d2120 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  ) -> EXCLUSIVE.*
d2130 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20  *    PENDING -> 
d2140 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20  EXCLUSIVE.**.** 
d2150 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  This routine wil
d2160 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20  l only increase 
d2170 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65  a lock.  Use the
d2180 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b   sqlite3OsUnlock
d2190 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f  ().** routine to
d21a0 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67   lower a locking
d21b0 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69   level..*/.stati
d21c0 63 20 69 6e 74 20 75 6e 69 78 4c 6f 63 6b 28 73  c int unixLock(s
d21d0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
d21e0 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a   int locktype){.
d21f0 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
d2200 6e 67 20 64 65 73 63 72 69 62 65 73 20 74 68 65  ng describes the
d2210 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
d2220 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 20 6c  of the various l
d2230 6f 63 6b 73 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f  ocks and.  ** lo
d2240 63 6b 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 69  ck transitions i
d2250 6e 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 50  n terms of the P
d2260 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20 73 68  OSIX advisory sh
d2270 61 72 65 64 20 61 6e 64 20 65 78 63 6c 75 73 69  ared and exclusi
d2280 76 65 0a 20 20 2a 2a 20 6c 6f 63 6b 20 70 72 69  ve.  ** lock pri
d2290 6d 69 74 69 76 65 73 20 28 63 61 6c 6c 65 64 20  mitives (called 
d22a0 72 65 61 64 2d 6c 6f 63 6b 73 20 61 6e 64 20 77  read-locks and w
d22b0 72 69 74 65 2d 6c 6f 63 6b 73 20 62 65 6c 6f 77  rite-locks below
d22c0 2c 20 74 6f 20 61 76 6f 69 64 0a 20 20 2a 2a 20  , to avoid.  ** 
d22d0 63 6f 6e 66 75 73 69 6f 6e 20 77 69 74 68 20 53  confusion with S
d22e0 51 4c 69 74 65 20 6c 6f 63 6b 20 6e 61 6d 65 73  QLite lock names
d22f0 29 2e 20 54 68 65 20 61 6c 67 6f 72 69 74 68 6d  ). The algorithm
d2300 73 20 61 72 65 20 63 6f 6d 70 6c 69 63 61 74 65  s are complicate
d2310 64 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c 79 20  d.  ** slightly 
d2320 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 65 20 63  in order to be c
d2330 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68 20 77  ompatible with w
d2340 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 20 73  indows systems s
d2350 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 0a 20 20  imultaneously.  
d2360 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74 68 65  ** accessing the
d2370 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 66   same database f
d2380 69 6c 65 2c 20 69 6e 20 63 61 73 65 20 74 68 61  ile, in case tha
d2390 74 20 69 73 20 65 76 65 72 20 72 65 71 75 69 72  t is ever requir
d23a0 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 79  ed..  **.  ** Sy
d23b0 6d 62 6f 6c 73 20 64 65 66 69 6e 65 64 20 69 6e  mbols defined in
d23c0 20 6f 73 2e 68 20 69 6e 64 65 6e 74 69 66 79 20   os.h indentify 
d23d0 74 68 65 20 27 70 65 6e 64 69 6e 67 20 62 79 74  the 'pending byt
d23e0 65 27 20 61 6e 64 20 74 68 65 20 27 72 65 73 65  e' and the 'rese
d23f0 72 76 65 64 0a 20 20 2a 2a 20 62 79 74 65 27 2c  rved.  ** byte',
d2400 20 65 61 63 68 20 73 69 6e 67 6c 65 20 62 79 74   each single byt
d2410 65 73 20 61 74 20 77 65 6c 6c 20 6b 6e 6f 77 6e  es at well known
d2420 20 6f 66 66 73 65 74 73 2c 20 61 6e 64 20 74 68   offsets, and th
d2430 65 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20  e 'shared byte. 
d2440 20 2a 2a 20 72 61 6e 67 65 27 2c 20 61 20 72 61   ** range', a ra
d2450 6e 67 65 20 6f 66 20 35 31 30 20 62 79 74 65 73  nge of 510 bytes
d2460 20 61 74 20 61 20 77 65 6c 6c 20 6b 6e 6f 77 6e   at a well known
d2470 20 6f 66 66 73 65 74 2e 0a 20 20 2a 2a 0a 20 20   offset..  **.  
d2480 2a 2a 20 54 6f 20 6f 62 74 61 69 6e 20 61 20 53  ** To obtain a S
d2490 48 41 52 45 44 20 6c 6f 63 6b 2c 20 61 20 72 65  HARED lock, a re
d24a0 61 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69  ad-lock is obtai
d24b0 6e 65 64 20 6f 6e 20 74 68 65 20 27 70 65 6e 64  ned on the 'pend
d24c0 69 6e 67 0a 20 20 2a 2a 20 62 79 74 65 27 2e 20  ing.  ** byte'. 
d24d0 20 49 66 20 74 68 69 73 20 69 73 20 73 75 63 63   If this is succ
d24e0 65 73 73 66 75 6c 2c 20 61 20 72 61 6e 64 6f 6d  essful, a random
d24f0 20 62 79 74 65 20 66 72 6f 6d 20 74 68 65 20 27   byte from the '
d2500 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a  shared byte.  **
d2510 20 72 61 6e 67 65 27 20 69 73 20 72 65 61 64 2d   range' is read-
d2520 6c 6f 63 6b 65 64 20 61 6e 64 20 74 68 65 20 6c  locked and the l
d2530 6f 63 6b 20 6f 6e 20 74 68 65 20 27 70 65 6e 64  ock on the 'pend
d2540 69 6e 67 20 62 79 74 65 27 20 72 65 6c 65 61 73  ing byte' releas
d2550 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20  ed..  **.  ** A 
d2560 70 72 6f 63 65 73 73 20 6d 61 79 20 6f 6e 6c 79  process may only
d2570 20 6f 62 74 61 69 6e 20 61 20 52 45 53 45 52 56   obtain a RESERV
d2580 45 44 20 6c 6f 63 6b 20 61 66 74 65 72 20 69 74  ED lock after it
d2590 20 68 61 73 20 61 20 53 48 41 52 45 44 20 6c 6f   has a SHARED lo
d25a0 63 6b 2e 0a 20 20 2a 2a 20 41 20 52 45 53 45 52  ck..  ** A RESER
d25b0 56 45 44 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c  VED lock is impl
d25c0 65 6d 65 6e 74 65 64 20 62 79 20 67 72 61 62 62  emented by grabb
d25d0 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b  ing a write-lock
d25e0 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 27 72 65   on the.  ** 're
d25f0 73 65 72 76 65 64 20 62 79 74 65 27 2e 20 0a 20  served byte'. . 
d2600 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63 65   **.  ** A proce
d2610 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74 61  ss may only obta
d2620 69 6e 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  in a PENDING loc
d2630 6b 20 61 66 74 65 72 20 69 74 20 68 61 73 20 6f  k after it has o
d2640 62 74 61 69 6e 65 64 20 61 0a 20 20 2a 2a 20 53  btained a.  ** S
d2650 48 41 52 45 44 20 6c 6f 63 6b 2e 20 41 20 50 45  HARED lock. A PE
d2660 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 69 6d  NDING lock is im
d2670 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 6f 62 74  plemented by obt
d2680 61 69 6e 69 6e 67 20 61 20 77 72 69 74 65 2d 6c  aining a write-l
d2690 6f 63 6b 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20  ock.  ** on the 
d26a0 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 2e 20  'pending byte'. 
d26b0 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61  This ensures tha
d26c0 74 20 6e 6f 20 6e 65 77 20 53 48 41 52 45 44 20  t no new SHARED 
d26d0 6c 6f 63 6b 73 20 63 61 6e 20 62 65 0a 20 20 2a  locks can be.  *
d26e0 2a 20 6f 62 74 61 69 6e 65 64 2c 20 62 75 74 20  * obtained, but 
d26f0 65 78 69 73 74 69 6e 67 20 53 48 41 52 45 44 20  existing SHARED 
d2700 6c 6f 63 6b 73 20 61 72 65 20 61 6c 6c 6f 77 65  locks are allowe
d2710 64 20 74 6f 20 70 65 72 73 69 73 74 2e 20 41 20  d to persist. A 
d2720 70 72 6f 63 65 73 73 0a 20 20 2a 2a 20 64 6f 65  process.  ** doe
d2730 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 6f 62  s not have to ob
d2740 74 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20  tain a RESERVED 
d2750 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 77 61 79 20  lock on the way 
d2760 74 6f 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  to a PENDING loc
d2770 6b 2e 0a 20 20 2a 2a 20 54 68 69 73 20 70 72 6f  k..  ** This pro
d2780 70 65 72 74 79 20 69 73 20 75 73 65 64 20 62 79  perty is used by
d2790 20 74 68 65 20 61 6c 67 6f 72 69 74 68 6d 20 66   the algorithm f
d27a0 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20  or rolling back 
d27b0 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20  a journal file. 
d27c0 20 2a 2a 20 61 66 74 65 72 20 61 20 63 72 61 73   ** after a cras
d27d0 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20  h..  **.  ** An 
d27e0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2c 20  EXCLUSIVE lock, 
d27f0 6f 62 74 61 69 6e 65 64 20 61 66 74 65 72 20 61  obtained after a
d2800 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73   PENDING lock is
d2810 20 68 65 6c 64 2c 20 69 73 0a 20 20 2a 2a 20 69   held, is.  ** i
d2820 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 6f 62  mplemented by ob
d2830 74 61 69 6e 69 6e 67 20 61 20 77 72 69 74 65 2d  taining a write-
d2840 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 65 6e 74 69  lock on the enti
d2850 72 65 20 27 73 68 61 72 65 64 20 62 79 74 65 0a  re 'shared byte.
d2860 20 20 2a 2a 20 72 61 6e 67 65 27 2e 20 53 69 6e    ** range'. Sin
d2870 63 65 20 61 6c 6c 20 6f 74 68 65 72 20 6c 6f 63  ce all other loc
d2880 6b 73 20 72 65 71 75 69 72 65 20 61 20 72 65 61  ks require a rea
d2890 64 2d 6c 6f 63 6b 20 6f 6e 20 6f 6e 65 20 6f 66  d-lock on one of
d28a0 20 74 68 65 20 62 79 74 65 73 0a 20 20 2a 2a 20   the bytes.  ** 
d28b0 77 69 74 68 69 6e 20 74 68 69 73 20 72 61 6e 67  within this rang
d28c0 65 2c 20 74 68 69 73 20 65 6e 73 75 72 65 73 20  e, this ensures 
d28d0 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 6c 6f  that no other lo
d28e0 63 6b 73 20 61 72 65 20 68 65 6c 64 20 6f 6e 20  cks are held on 
d28f0 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  the.  ** databas
d2900 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  e. .  **.  ** Th
d2910 65 20 72 65 61 73 6f 6e 20 61 20 73 69 6e 67 6c  e reason a singl
d2920 65 20 62 79 74 65 20 63 61 6e 6e 6f 74 20 62 65  e byte cannot be
d2930 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66   used instead of
d2940 20 74 68 65 20 27 73 68 61 72 65 64 20 62 79 74   the 'shared byt
d2950 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 20 69 73  e.  ** range' is
d2960 20 74 68 61 74 20 73 6f 6d 65 20 76 65 72 73 69   that some versi
d2970 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73 20 64  ons of windows d
d2980 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 72 65  o not support re
d2990 61 64 2d 6c 6f 63 6b 73 2e 20 42 79 0a 20 20 2a  ad-locks. By.  *
d29a0 2a 20 6c 6f 63 6b 69 6e 67 20 61 20 72 61 6e 64  * locking a rand
d29b0 6f 6d 20 62 79 74 65 20 66 72 6f 6d 20 61 20 72  om byte from a r
d29c0 61 6e 67 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74  ange, concurrent
d29d0 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61   SHARED locks ma
d29e0 79 20 65 78 69 73 74 0a 20 20 2a 2a 20 65 76 65  y exist.  ** eve
d29f0 6e 20 69 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67  n if the locking
d2a00 20 70 72 69 6d 69 74 69 76 65 20 75 73 65 64 20   primitive used 
d2a10 69 73 20 61 6c 77 61 79 73 20 61 20 77 72 69 74  is always a writ
d2a20 65 2d 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69  e-lock..  */.  i
d2a30 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
d2a40 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  K;.  unixFile *p
d2a50 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
d2a60 2a 29 69 64 3b 0a 20 20 73 74 72 75 63 74 20 75  *)id;.  struct u
d2a70 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f  nixLockInfo *pLo
d2a80 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f 63  ck = pFile->pLoc
d2a90 6b 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63  k;.  struct floc
d2aa0 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20 73 20  k lock;.  int s 
d2ab0 3d 20 30 3b 0a 20 20 69 6e 74 20 74 45 72 72 6e  = 0;.  int tErrn
d2ac0 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46  o;..  assert( pF
d2ad0 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  ile );.  OSTRACE
d2ae0 37 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73  7("LOCK    %d %s
d2af0 20 77 61 73 20 25 73 28 25 73 2c 25 64 29 20 70   was %s(%s,%d) p
d2b00 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  id=%d\n", pFile-
d2b10 3e 68 2c 0a 20 20 20 20 20 20 6c 6f 63 6b 74 79  >h,.      lockty
d2b20 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29  peName(locktype)
d2b30 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70  , locktypeName(p
d2b40 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c  File->locktype),
d2b50 0a 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e  .      locktypeN
d2b60 61 6d 65 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74  ame(pLock->lockt
d2b70 79 70 65 29 2c 20 70 4c 6f 63 6b 2d 3e 63 6e 74  ype), pLock->cnt
d2b80 20 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20   , getpid());.. 
d2b90 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
d2ba0 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f  already a lock o
d2bb0 66 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d  f this type or m
d2bc0 6f 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20  ore restrictive 
d2bd0 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 78  on the.  ** unix
d2be0 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67  File, do nothing
d2bf0 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20  . Don't use the 
d2c00 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70  end_lock: exit p
d2c10 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 75 6e 69  ath, as.  ** uni
d2c20 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61  xEnterMutex() ha
d2c30 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64  sn't been called
d2c40 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28   yet..  */.  if(
d2c50 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d2c60 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20  >=locktype ){.  
d2c70 20 20 4f 53 54 52 41 43 45 33 28 22 4c 4f 43 4b    OSTRACE3("LOCK
d2c80 20 20 20 20 25 64 20 25 73 20 6f 6b 20 28 61 6c      %d %s ok (al
d2c90 72 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20  ready held)\n", 
d2ca0 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20  pFile->h,.      
d2cb0 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61        locktypeNa
d2cc0 6d 65 28 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20  me(locktype));. 
d2cd0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
d2ce0 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d  _OK;.  }..  /* M
d2cf0 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63  ake sure the loc
d2d00 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73  king sequence is
d2d10 20 63 6f 72 72 65 63 74 2e 0a 20 20 2a 2a 20 20   correct..  **  
d2d20 28 31 29 20 57 65 20 6e 65 76 65 72 20 6d 6f 76  (1) We never mov
d2d30 65 20 66 72 6f 6d 20 75 6e 6c 6f 63 6b 65 64 20  e from unlocked 
d2d40 74 6f 20 61 6e 79 74 68 69 6e 67 20 68 69 67 68  to anything high
d2d50 65 72 20 74 68 61 6e 20 73 68 61 72 65 64 20 6c  er than shared l
d2d60 6f 63 6b 2e 0a 20 20 2a 2a 20 20 28 32 29 20 53  ock..  **  (2) S
d2d70 51 4c 69 74 65 20 6e 65 76 65 72 20 65 78 70 6c  QLite never expl
d2d80 69 63 69 74 6c 79 20 72 65 71 75 65 73 74 73 20  icitly requests 
d2d90 61 20 70 65 6e 64 69 67 20 6c 6f 63 6b 2e 0a 20  a pendig lock.. 
d2da0 20 2a 2a 20 20 28 33 29 20 41 20 73 68 61 72 65   **  (3) A share
d2db0 64 20 6c 6f 63 6b 20 69 73 20 61 6c 77 61 79 73  d lock is always
d2dc0 20 68 65 6c 64 20 77 68 65 6e 20 61 20 72 65 73   held when a res
d2dd0 65 72 76 65 20 6c 6f 63 6b 20 69 73 20 72 65 71  erve lock is req
d2de0 75 65 73 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 61  uested..  */.  a
d2df0 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f  ssert( pFile->lo
d2e00 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20  cktype!=NO_LOCK 
d2e10 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  || locktype==SHA
d2e20 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73  RED_LOCK );.  as
d2e30 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d  sert( locktype!=
d2e40 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a  PENDING_LOCK );.
d2e50 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
d2e60 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43  pe!=RESERVED_LOC
d2e70 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  K || pFile->lock
d2e80 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
d2e90 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20  K );..  /* This 
d2ea0 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65 64 20  mutex is needed 
d2eb0 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70  because pFile->p
d2ec0 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 64 20 61  Lock is shared a
d2ed0 63 72 6f 73 73 20 74 68 72 65 61 64 73 0a 20 20  cross threads.  
d2ee0 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75  */.  unixEnterMu
d2ef0 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b  tex();..  /* Mak
d2f00 65 20 73 75 72 65 20 74 68 65 20 63 75 72 72 65  e sure the curre
d2f10 6e 74 20 74 68 72 65 61 64 20 6f 77 6e 73 20 74  nt thread owns t
d2f20 68 65 20 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20  he pFile..  */. 
d2f30 20 72 63 20 3d 20 74 72 61 6e 73 66 65 72 4f 77   rc = transferOw
d2f40 6e 65 72 73 68 69 70 28 70 46 69 6c 65 29 3b 0a  nership(pFile);.
d2f50 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
d2f60 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 69 78 4c  _OK ){.    unixL
d2f70 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20  eaveMutex();.   
d2f80 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
d2f90 20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d    pLock = pFile-
d2fa0 3e 70 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 49 66  >pLock;..  /* If
d2fb0 20 73 6f 6d 65 20 74 68 72 65 61 64 20 75 73 69   some thread usi
d2fc0 6e 67 20 74 68 69 73 20 50 49 44 20 68 61 73 20  ng this PID has 
d2fd0 61 20 6c 6f 63 6b 20 76 69 61 20 61 20 64 69 66  a lock via a dif
d2fe0 66 65 72 65 6e 74 20 75 6e 69 78 46 69 6c 65 2a  ferent unixFile*
d2ff0 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20 74 68 61  .  ** handle tha
d3000 74 20 70 72 65 63 6c 75 64 65 73 20 74 68 65 20  t precludes the 
d3010 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 2c 20  requested lock, 
d3020 72 65 74 75 72 6e 20 42 55 53 59 2e 0a 20 20 2a  return BUSY..  *
d3030 2f 0a 20 20 69 66 28 20 28 70 46 69 6c 65 2d 3e  /.  if( (pFile->
d3040 6c 6f 63 6b 74 79 70 65 21 3d 70 4c 6f 63 6b 2d  locktype!=pLock-
d3050 3e 6c 6f 63 6b 74 79 70 65 20 26 26 20 0a 20 20  >locktype && .  
d3060 20 20 20 20 20 20 20 20 28 70 4c 6f 63 6b 2d 3e          (pLock->
d3070 6c 6f 63 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e  locktype>=PENDIN
d3080 47 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79  G_LOCK || lockty
d3090 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 29 29  pe>SHARED_LOCK))
d30a0 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  .  ){.    rc = S
d30b0 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
d30c0 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20  goto end_lock;. 
d30d0 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 53 48   }..  /* If a SH
d30e0 41 52 45 44 20 6c 6f 63 6b 20 69 73 20 72 65 71  ARED lock is req
d30f0 75 65 73 74 65 64 2c 20 61 6e 64 20 73 6f 6d 65  uested, and some
d3100 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 68   thread using th
d3110 69 73 20 50 49 44 20 61 6c 72 65 61 64 79 0a 20  is PID already. 
d3120 20 2a 2a 20 68 61 73 20 61 20 53 48 41 52 45 44   ** has a SHARED
d3130 20 6f 72 20 52 45 53 45 52 56 45 44 20 6c 6f 63   or RESERVED loc
d3140 6b 2c 20 74 68 65 6e 20 69 6e 63 72 65 6d 65 6e  k, then incremen
d3150 74 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  t reference coun
d3160 74 73 20 61 6e 64 0a 20 20 2a 2a 20 72 65 74 75  ts and.  ** retu
d3170 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20  rn SQLITE_OK..  
d3180 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  */.  if( locktyp
d3190 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26  e==SHARED_LOCK &
d31a0 26 20 0a 20 20 20 20 20 20 28 70 4c 6f 63 6b 2d  & .      (pLock-
d31b0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45  >locktype==SHARE
d31c0 44 5f 4c 4f 43 4b 20 7c 7c 20 70 4c 6f 63 6b 2d  D_LOCK || pLock-
d31d0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52  >locktype==RESER
d31e0 56 45 44 5f 4c 4f 43 4b 29 20 29 7b 0a 20 20 20  VED_LOCK) ){.   
d31f0 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
d3200 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
d3210 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46  ;.    assert( pF
d3220 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 30  ile->locktype==0
d3230 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
d3240 70 4c 6f 63 6b 2d 3e 63 6e 74 3e 30 20 29 3b 0a  pLock->cnt>0 );.
d3250 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
d3260 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43  ype = SHARED_LOC
d3270 4b 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e  K;.    pLock->cn
d3280 74 2b 2b 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  t++;.    pFile->
d3290 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a  pOpen->nLock++;.
d32a0 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63      goto end_loc
d32b0 6b 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41 20  k;.  }...  /* A 
d32c0 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20  PENDING lock is 
d32d0 6e 65 65 64 65 64 20 62 65 66 6f 72 65 20 61 63  needed before ac
d32e0 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44  quiring a SHARED
d32f0 20 6c 6f 63 6b 20 61 6e 64 20 62 65 66 6f 72 65   lock and before
d3300 0a 20 20 2a 2a 20 61 63 71 75 69 72 69 6e 67 20  .  ** acquiring 
d3310 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  an EXCLUSIVE loc
d3320 6b 2e 20 20 46 6f 72 20 74 68 65 20 53 48 41 52  k.  For the SHAR
d3330 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 50 45 4e  ED lock, the PEN
d3340 44 49 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a 20 62  DING will.  ** b
d3350 65 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f  e released..  */
d3360 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20  .  lock.l_len = 
d3370 31 4c 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 77 68 65  1L;.  lock.l_whe
d3380 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a  nce = SEEK_SET;.
d3390 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d    if( locktype==
d33a0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 0a 20 20 20  SHARED_LOCK .   
d33b0 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d     || (locktype=
d33c0 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20  =EXCLUSIVE_LOCK 
d33d0 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  && pFile->lockty
d33e0 70 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29  pe<PENDING_LOCK)
d33f0 0a 20 20 29 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c  .  ){.    lock.l
d3400 5f 74 79 70 65 20 3d 20 28 6c 6f 63 6b 74 79 70  _type = (locktyp
d3410 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 3f 46  e==SHARED_LOCK?F
d3420 5f 52 44 4c 43 4b 3a 46 5f 57 52 4c 43 4b 29 3b  _RDLCK:F_WRLCK);
d3430 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72  .    lock.l_star
d3440 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  t = PENDING_BYTE
d3450 3b 0a 20 20 20 20 73 20 3d 20 66 63 6e 74 6c 28  ;.    s = fcntl(
d3460 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c  pFile->h, F_SETL
d3470 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 69  K, &lock);.    i
d3480 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20  f( s==(-1) ){.  
d3490 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72      tErrno = err
d34a0 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  no;.      rc = s
d34b0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
d34c0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
d34d0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f   SQLITE_IOERR_LO
d34e0 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49  CK);.      if( I
d34f0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29  S_LOCK_ERROR(rc)
d3500 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c   ){.        pFil
d3510 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74  e->lastErrno = t
d3520 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20  Errno;.      }. 
d3530 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f       goto end_lo
d3540 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a  ck;.    }.  }...
d3550 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20    /* If control 
d3560 67 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f 69  gets to this poi
d3570 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c 6c  nt, then actuall
d3580 79 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 6d  y go ahead and m
d3590 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69  ake.  ** operati
d35a0 6e 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20  ng system calls 
d35b0 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65  for the specifie
d35c0 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69  d lock..  */.  i
d35d0 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  f( locktype==SHA
d35e0 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  RED_LOCK ){.    
d35f0 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 63  assert( pLock->c
d3600 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  nt==0 );.    ass
d3610 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b  ert( pLock->lock
d3620 74 79 70 65 3d 3d 30 20 29 3b 0a 0a 20 20 20 20  type==0 );..    
d3630 2f 2a 20 4e 6f 77 20 67 65 74 20 74 68 65 20 72  /* Now get the r
d3640 65 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20  ead-lock */.    
d3650 73 20 3d 20 72 61 6e 67 65 4c 6f 63 6b 28 70 46  s = rangeLock(pF
d3660 69 6c 65 2c 20 46 5f 52 44 4c 43 4b 2c 20 26 74  ile, F_RDLCK, &t
d3670 45 72 72 6e 6f 29 3b 0a 0a 20 20 20 20 2f 2a 20  Errno);..    /* 
d3680 44 72 6f 70 20 74 68 65 20 74 65 6d 70 6f 72 61  Drop the tempora
d3690 72 79 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20  ry PENDING lock 
d36a0 2a 2f 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74  */.    lock.l_st
d36b0 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59  art = PENDING_BY
d36c0 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c  TE;.    lock.l_l
d36d0 65 6e 20 3d 20 31 4c 3b 0a 20 20 20 20 6c 6f 63  en = 1L;.    loc
d36e0 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c  k.l_type = F_UNL
d36f0 43 4b 3b 0a 20 20 20 20 69 66 28 20 66 63 6e 74  CK;.    if( fcnt
d3700 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45  l(pFile->h, F_SE
d3710 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 30 20 29  TLK, &lock)!=0 )
d3720 7b 0a 20 20 20 20 20 20 69 66 28 20 73 20 21 3d  {.      if( s !=
d3730 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 2f   -1 ){.        /
d3740 2a 20 54 68 69 73 20 63 6f 75 6c 64 20 68 61 70  * This could hap
d3750 70 65 6e 20 77 69 74 68 20 61 20 6e 65 74 77 6f  pen with a netwo
d3760 72 6b 20 6d 6f 75 6e 74 20 2a 2f 0a 20 20 20 20  rk mount */.    
d3770 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72      tErrno = err
d3780 6e 6f 3b 20 0a 20 20 20 20 20 20 20 20 72 63 20  no; .        rc 
d3790 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f  = sqliteErrorFro
d37a0 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72  mPosixError(tErr
d37b0 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  no, SQLITE_IOERR
d37c0 5f 55 4e 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 20  _UNLOCK); .     
d37d0 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
d37e0 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20  RROR(rc) ){.    
d37f0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
d3800 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
d3810 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
d3820 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b     goto end_lock
d3830 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
d3840 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20      if( s==(-1) 
d3850 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
d3860 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
d3870 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
d3880 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43  SQLITE_IOERR_LOC
d3890 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53  K);.      if( IS
d38a0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20  _LOCK_ERROR(rc) 
d38b0 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65  ){.        pFile
d38c0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45  ->lastErrno = tE
d38d0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rrno;.      }.  
d38e0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
d38f0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
d3900 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20   SHARED_LOCK;.  
d3910 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e      pFile->pOpen
d3920 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20  ->nLock++;.     
d3930 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 3d 20 31 3b   pLock->cnt = 1;
d3940 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
d3950 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  f( locktype==EXC
d3960 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70  LUSIVE_LOCK && p
d3970 4c 6f 63 6b 2d 3e 63 6e 74 3e 31 20 29 7b 0a 20  Lock->cnt>1 ){. 
d3980 20 20 20 2f 2a 20 57 65 20 61 72 65 20 74 72 79     /* We are try
d3990 69 6e 67 20 66 6f 72 20 61 6e 20 65 78 63 6c 75  ing for an exclu
d39a0 73 69 76 65 20 6c 6f 63 6b 20 62 75 74 20 61 6e  sive lock but an
d39b0 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 6e 20  other thread in 
d39c0 74 68 69 73 0a 20 20 20 20 2a 2a 20 73 61 6d 65  this.    ** same
d39d0 20 70 72 6f 63 65 73 73 20 69 73 20 73 74 69 6c   process is stil
d39e0 6c 20 68 6f 6c 64 69 6e 67 20 61 20 73 68 61 72  l holding a shar
d39f0 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 20 20  ed lock. */.    
d3a00 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
d3a10 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
d3a20 2a 20 54 68 65 20 72 65 71 75 65 73 74 20 77 61  * The request wa
d3a30 73 20 66 6f 72 20 61 20 52 45 53 45 52 56 45 44  s for a RESERVED
d3a40 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   or EXCLUSIVE lo
d3a50 63 6b 2e 20 20 49 74 20 69 73 0a 20 20 20 20 2a  ck.  It is.    *
d3a60 2a 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74  * assumed that t
d3a70 68 65 72 65 20 69 73 20 61 20 53 48 41 52 45 44  here is a SHARED
d3a80 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b   or greater lock
d3a90 20 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 20   on the file.   
d3aa0 20 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20 20 20   ** already..   
d3ab0 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
d3ac0 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  0!=pFile->lockty
d3ad0 70 65 20 29 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c  pe );.    lock.l
d3ae0 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b  _type = F_WRLCK;
d3af0 0a 20 20 20 20 73 77 69 74 63 68 28 20 6c 6f 63  .    switch( loc
d3b00 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63  ktype ){.      c
d3b10 61 73 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  ase RESERVED_LOC
d3b20 4b 3a 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e  K:.        lock.
d3b30 6c 5f 73 74 61 72 74 20 3d 20 52 45 53 45 52 56  l_start = RESERV
d3b40 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 20  ED_BYTE;.       
d3b50 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65   s = fcntl(pFile
d3b60 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c  ->h, F_SETLK, &l
d3b70 6f 63 6b 29 3b 0a 20 20 20 20 20 20 20 20 74 45  ock);.        tE
d3b80 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
d3b90 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
d3ba0 20 20 20 63 61 73 65 20 45 58 43 4c 55 53 49 56     case EXCLUSIV
d3bb0 45 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20 20 20 20  E_LOCK:.        
d3bc0 73 20 3d 20 72 61 6e 67 65 4c 6f 63 6b 28 70 46  s = rangeLock(pF
d3bd0 69 6c 65 2c 20 46 5f 57 52 4c 43 4b 2c 20 26 74  ile, F_WRLCK, &t
d3be0 45 72 72 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20  Errno);.        
d3bf0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 64 65 66  break;.      def
d3c00 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 61 73  ault:.        as
d3c10 73 65 72 74 28 30 29 3b 0a 20 20 20 20 7d 0a 20  sert(0);.    }. 
d3c20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 29     if( s==(-1) )
d3c30 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
d3c40 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
d3c50 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53  xError(tErrno, S
d3c60 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b  QLITE_IOERR_LOCK
d3c70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f  );.      if( IS_
d3c80 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29  LOCK_ERROR(rc) )
d3c90 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  {.        pFile-
d3ca0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
d3cb0 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rno;.      }.   
d3cc0 20 7d 0a 20 20 7d 0a 20 20 0a 0a 23 69 66 6e 64   }.  }.  ..#ifnd
d3cd0 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 53  ef NDEBUG.  /* S
d3ce0 65 74 20 75 70 20 74 68 65 20 74 72 61 6e 73 61  et up the transa
d3cf0 63 74 69 6f 6e 2d 63 6f 75 6e 74 65 72 20 63 68  ction-counter ch
d3d00 61 6e 67 65 20 63 68 65 63 6b 69 6e 67 20 66 6c  ange checking fl
d3d10 61 67 73 20 77 68 65 6e 0a 20 20 2a 2a 20 74 72  ags when.  ** tr
d3d20 61 6e 73 69 74 69 6f 6e 69 6e 67 20 66 72 6f 6d  ansitioning from
d3d30 20 61 20 53 48 41 52 45 44 20 74 6f 20 61 20 52   a SHARED to a R
d3d40 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e 20 20 54  ESERVED lock.  T
d3d50 68 65 20 63 68 61 6e 67 65 0a 20 20 2a 2a 20 66  he change.  ** f
d3d60 72 6f 6d 20 53 48 41 52 45 44 20 74 6f 20 52 45  rom SHARED to RE
d3d70 53 45 52 56 45 44 20 6d 61 72 6b 73 20 74 68 65  SERVED marks the
d3d80 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20   beginning of a 
d3d90 6e 6f 72 6d 61 6c 0a 20 20 2a 2a 20 77 72 69 74  normal.  ** writ
d3da0 65 20 6f 70 65 72 61 74 69 6f 6e 20 28 6e 6f 74  e operation (not
d3db0 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 72   a hot journal r
d3dc0 6f 6c 6c 62 61 63 6b 29 2e 0a 20 20 2a 2f 0a 20  ollback)..  */. 
d3dd0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
d3de0 4f 4b 0a 20 20 20 26 26 20 70 46 69 6c 65 2d 3e  OK.   && pFile->
d3df0 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44  locktype<=SHARED
d3e00 5f 4c 4f 43 4b 0a 20 20 20 26 26 20 6c 6f 63 6b  _LOCK.   && lock
d3e10 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c  type==RESERVED_L
d3e20 4f 43 4b 0a 20 20 29 7b 0a 20 20 20 20 70 46 69  OCK.  ){.    pFi
d3e30 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68 6e  le->transCntrChn
d3e40 67 20 3d 20 30 3b 0a 20 20 20 20 70 46 69 6c 65  g = 0;.    pFile
d3e50 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30 3b 0a  ->dbUpdate = 0;.
d3e60 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72      pFile->inNor
d3e70 6d 61 6c 57 72 69 74 65 20 3d 20 31 3b 0a 20 20  malWrite = 1;.  
d3e80 7d 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 69 66 28  }.#endif...  if(
d3e90 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
d3ea0 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  {.    pFile->loc
d3eb0 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65  ktype = locktype
d3ec0 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63  ;.    pLock->loc
d3ed0 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65  ktype = locktype
d3ee0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f  ;.  }else if( lo
d3ef0 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56  cktype==EXCLUSIV
d3f00 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46  E_LOCK ){.    pF
d3f10 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
d3f20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20  PENDING_LOCK;.  
d3f30 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70    pLock->locktyp
d3f40 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  e = PENDING_LOCK
d3f50 3b 0a 20 20 7d 0a 0a 65 6e 64 5f 6c 6f 63 6b 3a  ;.  }..end_lock:
d3f60 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65  .  unixLeaveMute
d3f70 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28  x();.  OSTRACE4(
d3f80 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 25  "LOCK    %d %s %
d3f90 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  s\n", pFile->h, 
d3fa0 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63  locktypeName(loc
d3fb0 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20 20 72  ktype), .      r
d3fc0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22  c==SQLITE_OK ? "
d3fd0 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29 3b  ok" : "failed");
d3fe0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
d3ff0 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c  ./*.** Close all
d4000 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
d4010 73 20 61 63 63 75 6d 75 61 74 65 64 20 69 6e 20  s accumuated in 
d4020 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 2d  the unixOpenCnt-
d4030 3e 70 55 6e 75 73 65 64 20 6c 69 73 74 2e 0a 2a  >pUnused list..*
d4040 2a 20 49 66 20 61 6c 6c 20 73 75 63 68 20 66 69  * If all such fi
d4050 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 61  le descriptors a
d4060 72 65 20 63 6c 6f 73 65 64 20 77 69 74 68 6f 75  re closed withou
d4070 74 20 65 72 72 6f 72 2c 20 74 68 65 20 6c 69 73  t error, the lis
d4080 74 20 69 73 0a 2a 2a 20 63 6c 65 61 72 65 64 20  t is.** cleared 
d4090 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65  and SQLITE_OK re
d40a0 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 74  turned..**.** Ot
d40b0 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e 20 65  herwise, if an e
d40c0 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65  rror occurs, the
d40d0 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63  n successfully c
d40e0 6c 6f 73 65 64 20 66 69 6c 65 20 64 65 73 63 72  losed file descr
d40f0 69 70 74 6f 72 0a 2a 2a 20 65 6e 74 72 69 65 73  iptor.** entries
d4100 20 61 72 65 20 72 65 6d 6f 76 65 64 20 66 72 6f   are removed fro
d4110 6d 20 74 68 65 20 6c 69 73 74 2c 20 61 6e 64 20  m the list, and 
d4120 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f  SQLITE_IOERR_CLO
d4130 53 45 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a  SE returned. .**
d4140 20 6e 6f 74 20 64 65 6c 65 74 65 64 20 61 6e 64   not deleted and
d4150 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c   SQLITE_IOERR_CL
d4160 4f 53 45 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f  OSE returned..*/
d4170 20 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c 6f   .static int clo
d4180 73 65 50 65 6e 64 69 6e 67 46 64 73 28 75 6e 69  sePendingFds(uni
d4190 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20  xFile *pFile){. 
d41a0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
d41b0 5f 4f 4b 3b 0a 20 20 73 74 72 75 63 74 20 75 6e  _OK;.  struct un
d41c0 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e  ixOpenCnt *pOpen
d41d0 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b   = pFile->pOpen;
d41e0 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20  .  UnixUnusedFd 
d41f0 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 55  *pError = 0;.  U
d4200 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 3b 0a  nixUnusedFd *p;.
d4210 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a    UnixUnusedFd *
d4220 70 4e 65 78 74 3b 0a 20 20 66 6f 72 28 70 3d 70  pNext;.  for(p=p
d4230 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 3b 20 70  Open->pUnused; p
d4240 3b 20 70 3d 70 4e 65 78 74 29 7b 0a 20 20 20 20  ; p=pNext){.    
d4250 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74  pNext = p->pNext
d4260 3b 0a 20 20 20 20 69 66 28 20 63 6c 6f 73 65 28  ;.    if( close(
d4270 70 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 20 20  p->fd) ){.      
d4280 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
d4290 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
d42a0 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  rc = SQLITE_IOER
d42b0 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 70  R_CLOSE;.      p
d42c0 2d 3e 70 4e 65 78 74 20 3d 20 70 45 72 72 6f 72  ->pNext = pError
d42d0 3b 0a 20 20 20 20 20 20 70 45 72 72 6f 72 20 3d  ;.      pError =
d42e0 20 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   p;.    }else{. 
d42f0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
d4300 65 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  e(p);.    }.  }.
d4310 20 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64    pOpen->pUnused
d4320 20 3d 20 70 45 72 72 6f 72 3b 0a 20 20 72 65 74   = pError;.  ret
d4330 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
d4340 20 41 64 64 20 74 68 65 20 66 69 6c 65 20 64 65   Add the file de
d4350 73 63 72 69 70 74 6f 72 20 75 73 65 64 20 62 79  scriptor used by
d4360 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 70 46 69   file handle pFi
d4370 6c 65 20 74 6f 20 74 68 65 20 63 6f 72 72 65 73  le to the corres
d4380 70 6f 6e 64 69 6e 67 0a 2a 2a 20 70 55 6e 75 73  ponding.** pUnus
d4390 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74  ed list..*/.stat
d43a0 69 63 20 76 6f 69 64 20 73 65 74 50 65 6e 64 69  ic void setPendi
d43b0 6e 67 46 64 28 75 6e 69 78 46 69 6c 65 20 2a 70  ngFd(unixFile *p
d43c0 46 69 6c 65 29 7b 0a 20 20 73 74 72 75 63 74 20  File){.  struct 
d43d0 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70  unixOpenCnt *pOp
d43e0 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65  en = pFile->pOpe
d43f0 6e 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46  n;.  UnixUnusedF
d4400 64 20 2a 70 20 3d 20 70 46 69 6c 65 2d 3e 70 55  d *p = pFile->pU
d4410 6e 75 73 65 64 3b 0a 20 20 70 2d 3e 70 4e 65 78  nused;.  p->pNex
d4420 74 20 3d 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73  t = pOpen->pUnus
d4430 65 64 3b 0a 20 20 70 4f 70 65 6e 2d 3e 70 55 6e  ed;.  pOpen->pUn
d4440 75 73 65 64 20 3d 20 70 3b 0a 20 20 70 46 69 6c  used = p;.  pFil
d4450 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20 20 70 46 69  e->h = -1;.  pFi
d4460 6c 65 2d 3e 70 55 6e 75 73 65 64 20 3d 20 30 3b  le->pUnused = 0;
d4470 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20  .}../*.** Lower 
d4480 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
d4490 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69  l on file descri
d44a0 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f  ptor pFile to lo
d44b0 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70  cktype.  locktyp
d44c0 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74  e.** must be eit
d44d0 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53  her NO_LOCK or S
d44e0 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a  HARED_LOCK..**.*
d44f0 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67  * If the locking
d4500 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69   level of the fi
d4510 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73  le descriptor is
d4520 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62   already at or b
d4530 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75  elow.** the requ
d4540 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65  ested locking le
d4550 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  vel, this routin
d4560 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  e is a no-op..*/
d4570 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
d4580 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  Unlock(sqlite3_f
d4590 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63  ile *id, int loc
d45a0 6b 74 79 70 65 29 7b 0a 20 20 75 6e 69 78 46 69  ktype){.  unixFi
d45b0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
d45c0 78 46 69 6c 65 2a 29 69 64 3b 20 2f 2a 20 54 68  xFile*)id; /* Th
d45d0 65 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 20  e open file */. 
d45e0 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b   struct unixLock
d45f0 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b 20 20 20 20  Info *pLock;    
d4600 20 20 2f 2a 20 53 74 72 75 63 74 75 72 65 20 64    /* Structure d
d4610 65 73 63 72 69 62 69 6e 67 20 63 75 72 72 65 6e  escribing curren
d4620 74 20 6c 6f 63 6b 20 73 74 61 74 65 20 2a 2f 0a  t lock state */.
d4630 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c    struct flock l
d4640 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  ock;            
d4650 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f     /* Informatio
d4660 6e 20 70 61 73 73 65 64 20 69 6e 74 6f 20 66 63  n passed into fc
d4670 6e 74 6c 28 29 20 2a 2f 0a 20 20 69 6e 74 20 72  ntl() */.  int r
d4680 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
d4690 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
d46a0 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20  eturn code from 
d46b0 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 2a  this interface *
d46c0 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20  /.  int h;      
d46d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d46e0 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 64 65       /* The unde
d46f0 72 6c 79 69 6e 67 20 66 69 6c 65 20 64 65 73 63  rlying file desc
d4700 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20  riptor */.  int 
d4710 74 45 72 72 6e 6f 3b 20 20 20 20 20 20 20 20 20  tErrno;         
d4720 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
d4730 45 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20  Error code from 
d4740 73 79 73 74 65 6d 20 63 61 6c 6c 20 65 72 72 6f  system call erro
d4750 72 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  rs */..  assert(
d4760 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52   pFile );.  OSTR
d4770 41 43 45 37 28 22 55 4e 4c 4f 43 4b 20 20 25 64  ACE7("UNLOCK  %d
d4780 20 25 64 20 77 61 73 20 25 64 28 25 64 2c 25 64   %d was %d(%d,%d
d4790 29 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69  ) pid=%d\n", pFi
d47a0 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c  le->h, locktype,
d47b0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  .      pFile->lo
d47c0 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 70  cktype, pFile->p
d47d0 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20  Lock->locktype, 
d47e0 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e  pFile->pLock->cn
d47f0 74 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20  t, getpid());.. 
d4800 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
d4810 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e<=SHARED_LOCK )
d4820 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  ;.  if( pFile->l
d4830 6f 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 70  ocktype<=locktyp
d4840 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
d4850 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
d4860 20 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45 41   if( CHECK_THREA
d4870 44 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 20  DID(pFile) ){.  
d4880 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d4890 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 75 6e  MISUSE;.  }.  un
d48a0 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a  ixEnterMutex();.
d48b0 20 20 68 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a    h = pFile->h;.
d48c0 20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d    pLock = pFile-
d48d0 3e 70 4c 6f 63 6b 3b 0a 20 20 61 73 73 65 72 74  >pLock;.  assert
d48e0 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 21 3d 30 20  ( pLock->cnt!=0 
d48f0 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  );.  if( pFile->
d4900 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f  locktype>SHARED_
d4910 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73 73 65  LOCK ){.    asse
d4920 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74  rt( pLock->lockt
d4930 79 70 65 3d 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ype==pFile->lock
d4940 74 79 70 65 20 29 3b 0a 20 20 20 20 53 69 6d 75  type );.    Simu
d4950 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67  lateIOErrorBenig
d4960 6e 28 31 29 3b 0a 20 20 20 20 53 69 6d 75 6c 61  n(1);.    Simula
d4970 74 65 49 4f 45 72 72 6f 72 28 20 68 3d 28 2d 31  teIOError( h=(-1
d4980 29 20 29 0a 20 20 20 20 53 69 6d 75 6c 61 74 65  ) ).    Simulate
d4990 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29  IOErrorBenign(0)
d49a0 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  ;..#ifndef NDEBU
d49b0 47 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 72 65  G.    /* When re
d49c0 64 75 63 69 6e 67 20 61 20 6c 6f 63 6b 20 73 75  ducing a lock su
d49d0 63 68 20 74 68 61 74 20 6f 74 68 65 72 20 70 72  ch that other pr
d49e0 6f 63 65 73 73 65 73 20 63 61 6e 20 73 74 61 72  ocesses can star
d49f0 74 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e 67  t.    ** reading
d4a00 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
d4a10 6c 65 20 61 67 61 69 6e 2c 20 6d 61 6b 65 20 73  le again, make s
d4a20 75 72 65 20 74 68 61 74 20 74 68 65 0a 20 20 20  ure that the.   
d4a30 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   ** transaction 
d4a40 63 6f 75 6e 74 65 72 20 77 61 73 20 75 70 64 61  counter was upda
d4a50 74 65 64 20 69 66 20 61 6e 79 20 70 61 72 74 20  ted if any part 
d4a60 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  of the database.
d4a70 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 68 61 6e      ** file chan
d4a80 67 65 64 2e 20 20 49 66 20 74 68 65 20 74 72 61  ged.  If the tra
d4a90 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72  nsaction counter
d4aa0 20 69 73 20 6e 6f 74 20 75 70 64 61 74 65 64 2c   is not updated,
d4ab0 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 63 6f  .    ** other co
d4ac0 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74 68 65  nnections to the
d4ad0 20 73 61 6d 65 20 66 69 6c 65 20 6d 69 67 68 74   same file might
d4ae0 20 6e 6f 74 20 72 65 61 6c 69 7a 65 20 74 68 61   not realize tha
d4af0 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 69 6c  t.    ** the fil
d4b00 65 20 68 61 73 20 63 68 61 6e 67 65 64 20 61 6e  e has changed an
d4b10 64 20 68 65 6e 63 65 20 6d 69 67 68 74 20 6e 6f  d hence might no
d4b20 74 20 6b 6e 6f 77 20 74 6f 20 66 6c 75 73 68 20  t know to flush 
d4b30 74 68 65 69 72 0a 20 20 20 20 2a 2a 20 63 61 63  their.    ** cac
d4b40 68 65 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20  he.  The use of 
d4b50 61 20 73 74 61 6c 65 20 63 61 63 68 65 20 63 61  a stale cache ca
d4b60 6e 20 6c 65 61 64 20 74 6f 20 64 61 74 61 62 61  n lead to databa
d4b70 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20  se corruption.. 
d4b80 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
d4b90 28 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61  ( pFile->inNorma
d4ba0 6c 57 72 69 74 65 3d 3d 30 0a 20 20 20 20 20 20  lWrite==0.      
d4bb0 20 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 64 62 55     || pFile->dbU
d4bc0 70 64 61 74 65 3d 3d 30 0a 20 20 20 20 20 20 20  pdate==0.       
d4bd0 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 74 72 61 6e    || pFile->tran
d4be0 73 43 6e 74 72 43 68 6e 67 3d 3d 31 20 29 3b 0a  sCntrChng==1 );.
d4bf0 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72      pFile->inNor
d4c00 6d 61 6c 57 72 69 74 65 20 3d 20 30 3b 0a 23 65  malWrite = 0;.#e
d4c10 6e 64 69 66 0a 0a 0a 20 20 20 20 69 66 28 20 6c  ndif...    if( l
d4c20 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
d4c30 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 69 66  LOCK ){.      if
d4c40 28 20 72 61 6e 67 65 4c 6f 63 6b 28 70 46 69 6c  ( rangeLock(pFil
d4c50 65 2c 20 46 5f 52 44 4c 43 4b 2c 20 26 74 45 72  e, F_RDLCK, &tEr
d4c60 72 6e 6f 29 3d 3d 28 2d 31 29 20 29 7b 0a 20 20  rno)==(-1) ){.  
d4c70 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
d4c80 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45  eErrorFromPosixE
d4c90 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c  rror(tErrno, SQL
d4ca0 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b  ITE_IOERR_RDLOCK
d4cb0 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 49  );.        if( I
d4cc0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29  S_LOCK_ERROR(rc)
d4cd0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46   ){.          pF
d4ce0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
d4cf0 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20   tErrno;.       
d4d00 20 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20   }.        goto 
d4d10 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 20 20  end_unlock;.    
d4d20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 6c 6f    }.    }.    lo
d4d30 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e  ck.l_type = F_UN
d4d40 4c 43 4b 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f  LCK;.    lock.l_
d4d50 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45  whence = SEEK_SE
d4d60 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74  T;.    lock.l_st
d4d70 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59  art = PENDING_BY
d4d80 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c  TE;.    lock.l_l
d4d90 65 6e 20 3d 20 32 4c 3b 20 20 61 73 73 65 72 74  en = 2L;  assert
d4da0 28 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 31  ( PENDING_BYTE+1
d4db0 3d 3d 52 45 53 45 52 56 45 44 5f 42 59 54 45 20  ==RESERVED_BYTE 
d4dc0 29 3b 0a 20 20 20 20 69 66 28 20 66 63 6e 74 6c  );.    if( fcntl
d4dd0 28 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f  (h, F_SETLK, &lo
d4de0 63 6b 29 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20  ck)!=(-1) ){.   
d4df0 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79     pLock->lockty
d4e00 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b  pe = SHARED_LOCK
d4e10 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
d4e20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e     tErrno = errn
d4e30 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  o;.      rc = sq
d4e40 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
d4e50 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
d4e60 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
d4e70 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20  OCK);.      if( 
d4e80 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63  IS_LOCK_ERROR(rc
d4e90 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69  ) ){.        pFi
d4ea0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d4eb0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a  tErrno;.      }.
d4ec0 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 75        goto end_u
d4ed0 6e 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  nlock;.    }.  }
d4ee0 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  .  if( locktype=
d4ef0 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  =NO_LOCK ){.    
d4f00 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43  struct unixOpenC
d4f10 6e 74 20 2a 70 4f 70 65 6e 3b 0a 0a 20 20 20 20  nt *pOpen;..    
d4f20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65  /* Decrement the
d4f30 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 63 6f 75   shared lock cou
d4f40 6e 74 65 72 2e 20 20 52 65 6c 65 61 73 65 20 74  nter.  Release t
d4f50 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 61 6e  he lock using an
d4f60 0a 20 20 20 20 2a 2a 20 4f 53 20 63 61 6c 6c 20  .    ** OS call 
d4f70 6f 6e 6c 79 20 77 68 65 6e 20 61 6c 6c 20 74 68  only when all th
d4f80 72 65 61 64 73 20 69 6e 20 74 68 69 73 20 73 61  reads in this sa
d4f90 6d 65 20 70 72 6f 63 65 73 73 20 68 61 76 65 20  me process have 
d4fa0 72 65 6c 65 61 73 65 64 0a 20 20 20 20 2a 2a 20  released.    ** 
d4fb0 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f  the lock..    */
d4fc0 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2d  .    pLock->cnt-
d4fd0 2d 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b  -;.    if( pLock
d4fe0 2d 3e 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20  ->cnt==0 ){.    
d4ff0 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20    lock.l_type = 
d5000 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 20 20 6c  F_UNLCK;.      l
d5010 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53  ock.l_whence = S
d5020 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 20 20 6c  EEK_SET;.      l
d5030 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 6c 6f  ock.l_start = lo
d5040 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 4c 3b 0a 20  ck.l_len = 0L;. 
d5050 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45       SimulateIOE
d5060 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20  rrorBenign(1);. 
d5070 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45       SimulateIOE
d5080 72 72 6f 72 28 20 68 3d 28 2d 31 29 20 29 0a 20  rror( h=(-1) ). 
d5090 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45       SimulateIOE
d50a0 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20  rrorBenign(0);. 
d50b0 20 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 68       if( fcntl(h
d50c0 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b  , F_SETLK, &lock
d50d0 29 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20  )!=(-1) ){.     
d50e0 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79     pLock->lockty
d50f0 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20  pe = NO_LOCK;.  
d5100 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
d5110 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e     tErrno = errn
d5120 6f 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  o;.        rc = 
d5130 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50  sqliteErrorFromP
d5140 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f  osixError(tErrno
d5150 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55  , SQLITE_IOERR_U
d5160 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20  NLOCK);.        
d5170 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f  if( IS_LOCK_ERRO
d5180 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20  R(rc) ){.       
d5190 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
d51a0 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20  rno = tErrno;.  
d51b0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
d51c0 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20  pLock->locktype 
d51d0 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20  = NO_LOCK;.     
d51e0 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
d51f0 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20  pe = NO_LOCK;.  
d5200 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
d5210 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68   /* Decrement th
d5220 65 20 63 6f 75 6e 74 20 6f 66 20 6c 6f 63 6b 73  e count of locks
d5230 20 61 67 61 69 6e 73 74 20 74 68 69 73 20 73 61   against this sa
d5240 6d 65 20 66 69 6c 65 2e 20 20 57 68 65 6e 20 74  me file.  When t
d5250 68 65 0a 20 20 20 20 2a 2a 20 63 6f 75 6e 74 20  he.    ** count 
d5260 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 63 6c  reaches zero, cl
d5270 6f 73 65 20 61 6e 79 20 6f 74 68 65 72 20 66 69  ose any other fi
d5280 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 77  le descriptors w
d5290 68 6f 73 65 20 63 6c 6f 73 65 0a 20 20 20 20 2a  hose close.    *
d52a0 2a 20 77 61 73 20 64 65 66 65 72 72 65 64 20 62  * was deferred b
d52b0 65 63 61 75 73 65 20 6f 66 20 6f 75 74 73 74 61  ecause of outsta
d52c0 6e 64 69 6e 67 20 6c 6f 63 6b 73 2e 0a 20 20 20  nding locks..   
d52d0 20 2a 2f 0a 20 20 20 20 70 4f 70 65 6e 20 3d 20   */.    pOpen = 
d52e0 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20  pFile->pOpen;.  
d52f0 20 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d 2d    pOpen->nLock--
d5300 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  ;.    assert( pO
d5310 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d 30 20 29 3b  pen->nLock>=0 );
d5320 0a 20 20 20 20 69 66 28 20 70 4f 70 65 6e 2d 3e  .    if( pOpen->
d5330 6e 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20  nLock==0 ){.    
d5340 20 20 69 6e 74 20 72 63 32 20 3d 20 63 6c 6f 73    int rc2 = clos
d5350 65 50 65 6e 64 69 6e 67 46 64 73 28 70 46 69 6c  ePendingFds(pFil
d5360 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  e);.      if( rc
d5370 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
d5380 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b         rc = rc2;
d5390 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
d53a0 20 7d 0a 09 0a 65 6e 64 5f 75 6e 6c 6f 63 6b 3a   }...end_unlock:
d53b0 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65  .  unixLeaveMute
d53c0 78 28 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  x();.  if( rc==S
d53d0 51 4c 49 54 45 5f 4f 4b 20 29 20 70 46 69 6c 65  QLITE_OK ) pFile
d53e0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63  ->locktype = loc
d53f0 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20  ktype;.  return 
d5400 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
d5410 73 20 66 75 6e 63 74 69 6f 6e 20 70 65 72 66 6f  s function perfo
d5420 72 6d 73 20 74 68 65 20 70 61 72 74 73 20 6f 66  rms the parts of
d5430 20 74 68 65 20 22 63 6c 6f 73 65 20 66 69 6c 65   the "close file
d5440 22 20 6f 70 65 72 61 74 69 6f 6e 20 0a 2a 2a 20  " operation .** 
d5450 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f  common to all lo
d5460 63 6b 69 6e 67 20 73 63 68 65 6d 65 73 2e 20 49  cking schemes. I
d5470 74 20 63 6c 6f 73 65 73 20 74 68 65 20 64 69 72  t closes the dir
d5480 65 63 74 6f 72 79 20 61 6e 64 20 66 69 6c 65 0a  ectory and file.
d5490 2a 2a 20 68 61 6e 64 6c 65 73 2c 20 69 66 20 74  ** handles, if t
d54a0 68 65 79 20 61 72 65 20 76 61 6c 69 64 2c 20 61  hey are valid, a
d54b0 6e 64 20 73 65 74 73 20 61 6c 6c 20 66 69 65 6c  nd sets all fiel
d54c0 64 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69  ds of the unixFi
d54d0 6c 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20  le.** structure 
d54e0 74 6f 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69  to 0..**.** It i
d54f0 73 20 2a 6e 6f 74 2a 20 6e 65 63 65 73 73 61 72  s *not* necessar
d5500 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 75  y to hold the mu
d5510 74 65 78 20 77 68 65 6e 20 74 68 69 73 20 72 6f  tex when this ro
d5520 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
d5530 0a 2a 2a 20 65 76 65 6e 20 6f 6e 20 56 78 57 6f  .** even on VxWo
d5540 72 6b 73 2e 20 20 41 20 6d 75 74 65 78 20 77 69  rks.  A mutex wi
d5550 6c 6c 20 62 65 20 61 63 71 75 69 72 65 64 20 6f  ll be acquired o
d5560 6e 20 56 78 57 6f 72 6b 73 20 62 79 20 74 68 65  n VxWorks by the
d5570 0a 2a 2a 20 76 78 77 6f 72 6b 73 52 65 6c 65 61  .** vxworksRelea
d5580 73 65 46 69 6c 65 49 64 28 29 20 72 6f 75 74 69  seFileId() routi
d5590 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ne..*/.static in
d55a0 74 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28  t closeUnixFile(
d55b0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
d55c0 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  ){.  unixFile *p
d55d0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
d55e0 2a 29 69 64 3b 0a 20 20 69 66 28 20 70 46 69 6c  *)id;.  if( pFil
d55f0 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70 46 69  e ){.    if( pFi
d5600 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a  le->dirfd>=0 ){.
d5610 20 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20        int err = 
d5620 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64 69 72  close(pFile->dir
d5630 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65  fd);.      if( e
d5640 72 72 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46  rr ){.        pF
d5650 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
d5660 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20   errno;.        
d5670 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
d5680 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20  ERR_DIR_CLOSE;. 
d5690 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
d56a0 20 20 20 20 70 46 69 6c 65 2d 3e 64 69 72 66 64      pFile->dirfd
d56b0 3d 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  =-1;.      }.   
d56c0 20 7d 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65   }.    if( pFile
d56d0 2d 3e 68 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  ->h>=0 ){.      
d56e0 69 6e 74 20 65 72 72 20 3d 20 63 6c 6f 73 65 28  int err = close(
d56f0 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 20  pFile->h);.     
d5700 20 69 66 28 20 65 72 72 20 29 7b 0a 20 20 20 20   if( err ){.    
d5710 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
d5720 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
d5730 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
d5740 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b  ITE_IOERR_CLOSE;
d5750 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
d5760 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20  if OS_VXWORKS.  
d5770 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 49 64    if( pFile->pId
d5780 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 46   ){.      if( pF
d5790 69 6c 65 2d 3e 69 73 44 65 6c 65 74 65 20 29 7b  ile->isDelete ){
d57a0 0a 20 20 20 20 20 20 20 20 75 6e 6c 69 6e 6b 28  .        unlink(
d57b0 70 46 69 6c 65 2d 3e 70 49 64 2d 3e 7a 43 61 6e  pFile->pId->zCan
d57c0 6f 6e 69 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20  onicalName);.   
d57d0 20 20 20 7d 0a 20 20 20 20 20 20 76 78 77 6f 72     }.      vxwor
d57e0 6b 73 52 65 6c 65 61 73 65 46 69 6c 65 49 64 28  ksReleaseFileId(
d57f0 70 46 69 6c 65 2d 3e 70 49 64 29 3b 0a 20 20 20  pFile->pId);.   
d5800 20 20 20 70 46 69 6c 65 2d 3e 70 49 64 20 3d 20     pFile->pId = 
d5810 30 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  0;.    }.#endif.
d5820 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 43 4c      OSTRACE2("CL
d5830 4f 53 45 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70  OSE   %-3d\n", p
d5840 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 4f 70  File->h);.    Op
d5850 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20  enCounter(-1);. 
d5860 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
d5870 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64 29 3b  pFile->pUnused);
d5880 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 46 69 6c  .    memset(pFil
d5890 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69  e, 0, sizeof(uni
d58a0 78 46 69 6c 65 29 29 3b 0a 20 20 7d 0a 20 20 72  xFile));.  }.  r
d58b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
d58c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
d58d0 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  a file..*/.stati
d58e0 63 20 69 6e 74 20 75 6e 69 78 43 6c 6f 73 65 28  c int unixClose(
d58f0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
d5900 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
d5910 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69  LITE_OK;.  if( i
d5920 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c  d ){.    unixFil
d5930 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
d5940 46 69 6c 65 20 2a 29 69 64 3b 0a 20 20 20 20 75  File *)id;.    u
d5950 6e 69 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f  nixUnlock(id, NO
d5960 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 75 6e 69 78  _LOCK);.    unix
d5970 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20  EnterMutex();.  
d5980 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4f 70    if( pFile->pOp
d5990 65 6e 20 26 26 20 70 46 69 6c 65 2d 3e 70 4f 70  en && pFile->pOp
d59a0 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a 20 20 20  en->nLock ){.   
d59b0 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61     /* If there a
d59c0 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c  re outstanding l
d59d0 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61 63 74  ocks, do not act
d59e0 75 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68 65 20  ually close the 
d59f0 66 69 6c 65 20 6a 75 73 74 0a 20 20 20 20 20 20  file just.      
d5a00 2a 2a 20 79 65 74 20 62 65 63 61 75 73 65 20 74  ** yet because t
d5a10 68 61 74 20 77 6f 75 6c 64 20 63 6c 65 61 72 20  hat would clear 
d5a20 74 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 49 6e  those locks.  In
d5a30 73 74 65 61 64 2c 20 61 64 64 20 74 68 65 20 66  stead, add the f
d5a40 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64 65 73  ile.      ** des
d5a50 63 72 69 70 74 6f 72 20 74 6f 20 70 4f 70 65 6e  criptor to pOpen
d5a60 2d 3e 70 55 6e 75 73 65 64 20 6c 69 73 74 2e 20  ->pUnused list. 
d5a70 20 49 74 20 77 69 6c 6c 20 62 65 20 61 75 74 6f   It will be auto
d5a80 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 64  matically closed
d5a90 20 0a 20 20 20 20 20 20 2a 2a 20 77 68 65 6e 20   .      ** when 
d5aa0 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20 69 73  the last lock is
d5ab0 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 20 20   cleared..      
d5ac0 2a 2f 0a 20 20 20 20 20 20 73 65 74 50 65 6e 64  */.      setPend
d5ad0 69 6e 67 46 64 28 70 46 69 6c 65 29 3b 0a 20 20  ingFd(pFile);.  
d5ae0 20 20 7d 0a 20 20 20 20 72 65 6c 65 61 73 65 4c    }.    releaseL
d5af0 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70  ockInfo(pFile->p
d5b00 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 65 6c 65 61  Lock);.    relea
d5b10 73 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d  seOpenCnt(pFile-
d5b20 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 72 63 20  >pOpen);.    rc 
d5b30 3d 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28  = closeUnixFile(
d5b40 69 64 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61  id);.    unixLea
d5b50 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20  veMutex();.  }. 
d5b60 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
d5b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
d5b80 6e 64 20 6f 66 20 74 68 65 20 70 6f 73 69 78 20  nd of the posix 
d5b90 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 20 69 6d  advisory lock im
d5ba0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a  plementation ***
d5bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
d5bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
d5c10 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
d5c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
d5c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e  ************** N
d5c80 6f 2d 6f 70 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a  o-op Locking ***
d5c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
d5cb0 2a 2a 0a 2a 2a 20 4f 66 20 74 68 65 20 76 61 72  **.** Of the var
d5cc0 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 69 6d 70  ious locking imp
d5cd0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 61 76 61  lementations ava
d5ce0 69 6c 61 62 6c 65 2c 20 74 68 69 73 20 69 73 20  ilable, this is 
d5cf0 62 79 20 66 61 72 20 74 68 65 0a 2a 2a 20 73 69  by far the.** si
d5d00 6d 70 6c 65 73 74 3a 20 20 6c 6f 63 6b 69 6e 67  mplest:  locking
d5d10 20 69 73 20 69 67 6e 6f 72 65 64 2e 20 20 4e 6f   is ignored.  No
d5d20 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
d5d30 20 74 6f 20 6c 6f 63 6b 20 74 68 65 20 64 61 74   to lock the dat
d5d40 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 66 6f  abase.** file fo
d5d50 72 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69  r reading or wri
d5d60 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ting..**.** This
d5d70 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 69 73   locking mode is
d5d80 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72   appropriate for
d5d90 20 75 73 65 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c   use on read-onl
d5da0 79 20 64 61 74 61 62 61 73 65 73 0a 2a 2a 20 28  y databases.** (
d5db0 65 78 3a 20 64 61 74 61 62 61 73 65 73 20 74 68  ex: databases th
d5dc0 61 74 20 61 72 65 20 62 75 72 6e 65 64 20 69 6e  at are burned in
d5dd0 74 6f 20 43 44 2d 52 4f 4d 2c 20 66 6f 72 20 65  to CD-ROM, for e
d5de0 78 61 6d 70 6c 65 2e 29 20 20 49 74 20 63 61 6e  xample.)  It can
d5df0 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 75 73 65 64  .** also be used
d5e00 20 69 66 20 74 68 65 20 61 70 70 6c 69 63 61 74   if the applicat
d5e10 69 6f 6e 20 65 6d 70 6c 6f 79 73 20 73 6f 6d 65  ion employs some
d5e20 20 65 78 74 65 72 6e 61 6c 20 6d 65 63 68 61 6e   external mechan
d5e30 69 73 6d 20 74 6f 0a 2a 2a 20 70 72 65 76 65 6e  ism to.** preven
d5e40 74 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 20 61  t simultaneous a
d5e50 63 63 65 73 73 20 6f 66 20 74 68 65 20 73 61 6d  ccess of the sam
d5e60 65 20 64 61 74 61 62 61 73 65 20 62 79 20 74 77  e database by tw
d5e70 6f 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 64 61 74  o or more.** dat
d5e80 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
d5e90 73 2e 20 20 42 75 74 20 74 68 65 72 65 20 69 73  s.  But there is
d5ea0 20 61 20 73 65 72 69 6f 75 73 20 72 69 73 6b 20   a serious risk 
d5eb0 6f 66 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63  of database.** c
d5ec0 6f 72 72 75 70 74 69 6f 6e 20 69 66 20 74 68 69  orruption if thi
d5ed0 73 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 69  s locking mode i
d5ee0 73 20 75 73 65 64 20 69 6e 20 73 69 74 75 61 74  s used in situat
d5ef0 69 6f 6e 73 20 77 68 65 72 65 20 6d 75 6c 74 69  ions where multi
d5f00 70 6c 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  ple.** database 
d5f10 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 20  connections are 
d5f20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61  accessing the sa
d5f30 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  me database file
d5f40 20 61 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20   at the same.** 
d5f50 74 69 6d 65 20 61 6e 64 20 6f 6e 65 20 6f 72 20  time and one or 
d5f60 6d 6f 72 65 20 6f 66 20 74 68 6f 73 65 20 63 6f  more of those co
d5f70 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 20 77 72  nnections are wr
d5f80 69 74 69 6e 67 2e 0a 2a 2f 0a 0a 73 74 61 74 69  iting..*/..stati
d5f90 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 43 68 65 63  c int nolockChec
d5fa0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71  kReservedLock(sq
d5fb0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55  lite3_file *NotU
d5fc0 73 65 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75  sed, int *pResOu
d5fd0 74 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  t){.  UNUSED_PAR
d5fe0 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
d5ff0 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 30 3b  .  *pResOut = 0;
d6000 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
d6010 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  _OK;.}.static in
d6020 74 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c  t nolockLock(sql
d6030 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73  ite3_file *NotUs
d6040 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32  ed, int NotUsed2
d6050 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
d6060 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20  METER2(NotUsed, 
d6070 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 72 65 74  NotUsed2);.  ret
d6080 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
d6090 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f  .static int nolo
d60a0 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33  ckUnlock(sqlite3
d60b0 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20  _file *NotUsed, 
d60c0 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20  int NotUsed2){. 
d60d0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
d60e0 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
d60f0 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20  sed2);.  return 
d6100 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
d6110 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69  .** Close the fi
d6120 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
d6130 74 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 28 73 71  t nolockClose(sq
d6140 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20  lite3_file *id) 
d6150 7b 0a 20 20 72 65 74 75 72 6e 20 63 6c 6f 73 65  {.  return close
d6160 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 7d 0a  UnixFile(id);.}.
d6170 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
d6180 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65  ***** End of the
d6190 20 6e 6f 2d 6f 70 20 6c 6f 63 6b 20 69 6d 70 6c   no-op lock impl
d61a0 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a  ementation *****
d61b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d61c0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
d61d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d61e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d61f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
d6210 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
d6220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6260 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
d6270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
d6280 6e 20 64 6f 74 2d 66 69 6c 65 20 4c 6f 63 6b 69  n dot-file Locki
d6290 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ng *************
d62a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d62b0 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 6f 74 66  *.**.** The dotf
d62c0 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c  ile locking impl
d62d0 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20  ementation uses 
d62e0 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66  the existance of
d62f0 20 73 65 70 61 72 61 74 65 20 6c 6f 63 6b 0a 2a   separate lock.*
d6300 2a 20 66 69 6c 65 73 20 69 6e 20 6f 72 64 65 72  * files in order
d6310 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65   to control acce
d6320 73 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ss to the databa
d6330 73 65 2e 20 20 54 68 69 73 20 77 6f 72 6b 73 20  se.  This works 
d6340 6f 6e 20 6a 75 73 74 0a 2a 2a 20 61 62 6f 75 74  on just.** about
d6350 20 65 76 65 72 79 20 66 69 6c 65 73 79 73 74 65   every filesyste
d6360 6d 20 69 6d 61 67 69 6e 61 62 6c 65 2e 20 20 42  m imaginable.  B
d6370 75 74 20 74 68 65 72 65 20 61 72 65 20 73 65 72  ut there are ser
d6380 69 6f 75 73 20 64 6f 77 6e 73 69 64 65 73 3a 0a  ious downsides:.
d6390 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20 54 68  **.**    (1)  Th
d63a0 65 72 65 20 69 73 20 7a 65 72 6f 20 63 6f 6e 63  ere is zero conc
d63b0 75 72 72 65 6e 63 79 2e 20 20 41 20 73 69 6e 67  urrency.  A sing
d63c0 6c 65 20 72 65 61 64 65 72 20 62 6c 6f 63 6b 73  le reader blocks
d63d0 20 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 20 20   all other.**   
d63e0 20 20 20 20 20 20 63 6f 6e 6e 65 63 74 69 6f 6e        connection
d63f0 73 20 66 72 6f 6d 20 72 65 61 64 69 6e 67 20 6f  s from reading o
d6400 72 20 77 72 69 74 69 6e 67 20 74 68 65 20 64 61  r writing the da
d6410 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  tabase..**.**   
d6420 20 28 32 29 20 20 41 6e 20 61 70 70 6c 69 63 61   (2)  An applica
d6430 74 69 6f 6e 20 63 72 61 73 68 20 6f 72 20 70 6f  tion crash or po
d6440 77 65 72 20 6c 6f 73 73 20 63 61 6e 20 6c 65 61  wer loss can lea
d6450 76 65 20 73 74 61 6c 65 20 6c 6f 63 6b 20 66 69  ve stale lock fi
d6460 6c 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 73  les.**         s
d6470 69 74 74 69 6e 67 20 61 72 6f 75 6e 64 20 74 68  itting around th
d6480 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20 63 6c  at need to be cl
d6490 65 61 72 65 64 20 6d 61 6e 75 61 6c 6c 79 2e 0a  eared manually..
d64a0 2a 2a 0a 2a 2a 20 4e 65 76 65 72 74 68 65 6c 65  **.** Neverthele
d64b0 73 73 2c 20 61 20 64 6f 74 6c 6f 63 6b 20 69 73  ss, a dotlock is
d64c0 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20   an appropriate 
d64d0 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 66 6f 72  locking mode for
d64e0 20 75 73 65 20 69 66 20 6e 6f 0a 2a 2a 20 6f 74   use if no.** ot
d64f0 68 65 72 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61  her locking stra
d6500 74 65 67 79 20 69 73 20 61 76 61 69 6c 61 62 6c  tegy is availabl
d6510 65 2e 0a 2a 2a 0a 2a 2a 20 44 6f 74 66 69 6c 65  e..**.** Dotfile
d6520 20 6c 6f 63 6b 69 6e 67 20 77 6f 72 6b 73 20 62   locking works b
d6530 79 20 63 72 65 61 74 69 6e 67 20 61 20 66 69 6c  y creating a fil
d6540 65 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 69  e in the same di
d6550 72 65 63 74 6f 72 79 20 61 73 20 74 68 65 0a 2a  rectory as the.*
d6560 2a 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 77  * database and w
d6570 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d  ith the same nam
d6580 65 20 62 75 74 20 77 69 74 68 20 61 20 22 2e 6c  e but with a ".l
d6590 6f 63 6b 22 20 65 78 74 65 6e 73 69 6f 6e 20 61  ock" extension a
d65a0 64 64 65 64 2e 0a 2a 2a 20 54 68 65 20 65 78 69  dded..** The exi
d65b0 73 74 61 6e 63 65 20 6f 66 20 61 20 6c 6f 63 6b  stance of a lock
d65c0 20 66 69 6c 65 20 69 6d 70 6c 69 65 73 20 61 6e   file implies an
d65d0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e   EXCLUSIVE lock.
d65e0 20 20 41 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b    All other lock
d65f0 0a 2a 2a 20 74 79 70 65 73 20 28 53 48 41 52 45  .** types (SHARE
d6600 44 2c 20 52 45 53 45 52 56 45 44 2c 20 50 45 4e  D, RESERVED, PEN
d6610 44 49 4e 47 29 20 61 72 65 20 6d 61 70 70 65 64  DING) are mapped
d6620 20 69 6e 74 6f 20 45 58 43 4c 55 53 49 56 45 2e   into EXCLUSIVE.
d6630 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  .*/../*.** The f
d6640 69 6c 65 20 73 75 66 66 69 78 20 61 64 64 65 64  ile suffix added
d6650 20 74 6f 20 74 68 65 20 64 61 74 61 20 62 61 73   to the data bas
d6660 65 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6f 72  e filename in or
d6670 64 65 72 20 74 6f 20 63 72 65 61 74 65 20 74 68  der to create th
d6680 65 0a 2a 2a 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a  e.** lock file..
d6690 2a 2f 0a 23 64 65 66 69 6e 65 20 44 4f 54 4c 4f  */.#define DOTLO
d66a0 43 4b 5f 53 55 46 46 49 58 20 22 2e 6c 6f 63 6b  CK_SUFFIX ".lock
d66b0 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  "../*.** This ro
d66c0 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20  utine checks if 
d66d0 74 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52  there is a RESER
d66e0 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e  VED lock held on
d66f0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a   the specified.*
d6700 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f  * file by this o
d6710 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63  r any other proc
d6720 65 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c  ess. If such a l
d6730 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74  ock is held, set
d6740 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20   *pResOut.** to 
d6750 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65  a non-zero value
d6760 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73   otherwise *pRes
d6770 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65  Out is set to ze
d6780 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20  ro.  The return 
d6790 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20  value.** is set 
d67a0 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c  to SQLITE_OK unl
d67b0 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72  ess an I/O error
d67c0 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c   occurs during l
d67d0 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2a  ock checking..**
d67e0 0a 2a 2a 20 49 6e 20 64 6f 74 66 69 6c 65 20 6c  .** In dotfile l
d67f0 6f 63 6b 69 6e 67 2c 20 65 69 74 68 65 72 20 61  ocking, either a
d6800 20 6c 6f 63 6b 20 65 78 69 73 74 73 20 6f 72 20   lock exists or 
d6810 69 74 20 64 6f 65 73 20 6e 6f 74 2e 20 20 53 6f  it does not.  So
d6820 20 69 6e 20 74 68 69 73 0a 2a 2a 20 76 61 72 69   in this.** vari
d6830 61 74 69 6f 6e 20 6f 66 20 43 68 65 63 6b 52 65  ation of CheckRe
d6840 73 65 72 76 65 64 4c 6f 63 6b 28 29 2c 20 2a 70  servedLock(), *p
d6850 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f  ResOut is set to
d6860 20 74 72 75 65 20 69 66 20 61 6e 79 20 6c 6f 63   true if any loc
d6870 6b 0a 2a 2a 20 69 73 20 68 65 6c 64 20 6f 6e 20  k.** is held on 
d6880 74 68 65 20 66 69 6c 65 20 61 6e 64 20 66 61 6c  the file and fal
d6890 73 65 20 69 66 20 74 68 65 20 66 69 6c 65 20 69  se if the file i
d68a0 73 20 75 6e 6c 6f 63 6b 65 64 2e 0a 2a 2f 0a 73  s unlocked..*/.s
d68b0 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63  tatic int dotloc
d68c0 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  kCheckReservedLo
d68d0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
d68e0 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75  *id, int *pResOu
d68f0 74 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  t) {.  int rc = 
d6900 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74  SQLITE_OK;.  int
d6910 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20   reserved = 0;. 
d6920 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
d6930 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
d6940 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  ;..  SimulateIOE
d6950 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
d6960 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52  ITE_IOERR_CHECKR
d6970 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a  ESERVEDLOCK; );.
d6980 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69    .  assert( pFi
d6990 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  le );..  /* Chec
d69a0 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e  k if a thread in
d69b0 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f   this process ho
d69c0 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20  lds such a lock 
d69d0 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  */.  if( pFile->
d69e0 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f  locktype>SHARED_
d69f0 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 45  LOCK ){.    /* E
d6a00 69 74 68 65 72 20 74 68 69 73 20 63 6f 6e 6e 65  ither this conne
d6a10 63 74 69 6f 6e 20 6f 72 20 73 6f 6d 65 20 6f 74  ction or some ot
d6a20 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  her connection i
d6a30 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65  n the same proce
d6a40 73 73 0a 20 20 20 20 2a 2a 20 68 6f 6c 64 73 20  ss.    ** holds 
d6a50 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69  a lock on the fi
d6a60 6c 65 2e 20 20 4e 6f 20 6e 65 65 64 20 74 6f 20  le.  No need to 
d6a70 63 68 65 63 6b 20 66 75 72 74 68 65 72 2e 20 2a  check further. *
d6a80 2f 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d  /.    reserved =
d6a90 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   1;.  }else{.   
d6aa0 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 20 69 73 20   /* The lock is 
d6ab0 68 65 6c 64 20 69 66 20 61 6e 64 20 6f 6e 6c 79  held if and only
d6ac0 20 69 66 20 74 68 65 20 6c 6f 63 6b 66 69 6c 65   if the lockfile
d6ad0 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 63   exists */.    c
d6ae0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 6f 63 6b  onst char *zLock
d6af0 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68  File = (const ch
d6b00 61 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  ar*)pFile->locki
d6b10 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 72  ngContext;.    r
d6b20 65 73 65 72 76 65 64 20 3d 20 61 63 63 65 73 73  eserved = access
d6b30 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 30 29 3d 3d  (zLockFile, 0)==
d6b40 30 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45  0;.  }.  OSTRACE
d6b50 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20  4("TEST WR-LOCK 
d6b60 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69  %d %d %d\n", pFi
d6b70 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72  le->h, rc, reser
d6b80 76 65 64 29 3b 0a 20 20 2a 70 52 65 73 4f 75 74  ved);.  *pResOut
d6b90 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72   = reserved;.  r
d6ba0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
d6bb0 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65  ** Lock the file
d6bc0 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73   with the lock s
d6bd0 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61  pecified by para
d6be0 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d  meter locktype -
d6bf0 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66   one.** of the f
d6c00 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
d6c10 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c      (1) SHARED_L
d6c20 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52  OCK.**     (2) R
d6c30 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20  ESERVED_LOCK.** 
d6c40 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f      (3) PENDING_
d6c50 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20  LOCK.**     (4) 
d6c60 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a  EXCLUSIVE_LOCK.*
d6c70 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77  *.** Sometimes w
d6c80 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f  hen requesting o
d6c90 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61  ne lock state, a
d6ca0 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73  dditional lock s
d6cb0 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73  tates.** are ins
d6cc0 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e  erted in between
d6cd0 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d  .  The locking m
d6ce0 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65  ight fail on one
d6cf0 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a   of the later.**
d6d00 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61   transitions lea
d6d10 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74  ving the lock st
d6d20 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72  ate different fr
d6d30 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74  om what it start
d6d40 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20  ed but.** still 
d6d50 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61  short of its goa
d6d60 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  l.  The followin
d6d70 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68  g chart shows th
d6d80 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61  e allowed.** tra
d6d90 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65  nsitions and the
d6da0 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d   inserted interm
d6db0 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a  ediate states:.*
d6dc0 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44  *.**    UNLOCKED
d6dd0 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20   -> SHARED.**   
d6de0 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52   SHARED -> RESER
d6df0 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44  VED.**    SHARED
d6e00 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
d6e10 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
d6e20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45   RESERVED -> (PE
d6e30 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53  NDING) -> EXCLUS
d6e40 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e  IVE.**    PENDIN
d6e50 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  G -> EXCLUSIVE.*
d6e60 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
d6e70 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72  e will only incr
d6e80 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73  ease a lock.  Us
d6e90 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55  e the sqlite3OsU
d6ea0 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69  nlock().** routi
d6eb0 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f  ne to lower a lo
d6ec0 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2a 0a  cking level..**.
d6ed0 2a 2a 20 57 69 74 68 20 64 6f 74 66 69 6c 65 20  ** With dotfile 
d6ee0 6c 6f 63 6b 69 6e 67 2c 20 77 65 20 72 65 61 6c  locking, we real
d6ef0 6c 79 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 20  ly only support 
d6f00 73 74 61 74 65 20 28 34 29 3a 20 45 58 43 4c 55  state (4): EXCLU
d6f10 53 49 56 45 2e 0a 2a 2a 20 42 75 74 20 77 65 20  SIVE..** But we 
d6f20 74 72 61 63 6b 20 74 68 65 20 6f 74 68 65 72 20  track the other 
d6f30 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 73 20 69  locking levels i
d6f40 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a 2f 0a 73 74  nternally..*/.st
d6f50 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b  atic int dotlock
d6f60 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
d6f70 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74  e *id, int lockt
d6f80 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c  ype) {.  unixFil
d6f90 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
d6fa0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20  File*)id;.  int 
d6fb0 66 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63  fd;.  char *zLoc
d6fc0 6b 46 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29  kFile = (char *)
d6fd0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
d6fe0 6e 74 65 78 74 3b 0a 20 20 69 6e 74 20 72 63 20  ntext;.  int rc 
d6ff0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 0a 20  = SQLITE_OK;... 
d7000 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 61   /* If we have a
d7010 6e 79 20 6c 6f 63 6b 2c 20 74 68 65 6e 20 74 68  ny lock, then th
d7020 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 6c 72 65  e lock file alre
d7030 61 64 79 20 65 78 69 73 74 73 2e 20 20 41 6c 6c  ady exists.  All
d7040 20 77 65 20 68 61 76 65 0a 20 20 2a 2a 20 74 6f   we have.  ** to
d7050 20 64 6f 20 69 73 20 61 64 6a 75 73 74 20 6f 75   do is adjust ou
d7060 72 20 69 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72  r internal recor
d7070 64 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 6c 65  d of the lock le
d7080 76 65 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  vel..  */.  if( 
d7090 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
d70a0 3e 20 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  > NO_LOCK ){.   
d70b0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d70c0 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 23 69 66   = locktype;.#if
d70d0 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20   !OS_VXWORKS.   
d70e0 20 2f 2a 20 41 6c 77 61 79 73 20 75 70 64 61 74   /* Always updat
d70f0 65 20 74 68 65 20 74 69 6d 65 73 74 61 6d 70 20  e the timestamp 
d7100 6f 6e 20 74 68 65 20 6f 6c 64 20 66 69 6c 65 20  on the old file 
d7110 2a 2f 0a 20 20 20 20 75 74 69 6d 65 73 28 7a 4c  */.    utimes(zL
d7120 6f 63 6b 46 69 6c 65 2c 20 4e 55 4c 4c 29 3b 0a  ockFile, NULL);.
d7130 23 65 6e 64 69 66 0a 20 20 20 20 72 65 74 75 72  #endif.    retur
d7140 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
d7150 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62 20 61 6e  .  .  /* grab an
d7160 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
d7170 2a 2f 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a  */.  fd = open(z
d7180 4c 6f 63 6b 46 69 6c 65 2c 4f 5f 52 44 4f 4e 4c  LockFile,O_RDONL
d7190 59 7c 4f 5f 43 52 45 41 54 7c 4f 5f 45 58 43 4c  Y|O_CREAT|O_EXCL
d71a0 2c 30 36 30 30 29 3b 0a 20 20 69 66 28 20 66 64  ,0600);.  if( fd
d71b0 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 66 61 69  <0 ){.    /* fai
d71c0 6c 65 64 20 74 6f 20 6f 70 65 6e 2f 63 72 65 61  led to open/crea
d71d0 74 65 20 74 68 65 20 66 69 6c 65 2c 20 73 6f 6d  te the file, som
d71e0 65 6f 6e 65 20 65 6c 73 65 20 6d 61 79 20 68 61  eone else may ha
d71f0 76 65 20 73 74 6f 6c 65 6e 20 74 68 65 20 6c 6f  ve stolen the lo
d7200 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 74 45  ck */.    int tE
d7210 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
d7220 20 20 69 66 28 20 45 45 58 49 53 54 20 3d 3d 20    if( EEXIST == 
d7230 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20 20 20 20  tErrno ){.      
d7240 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
d7250 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20  ;.    } else {. 
d7260 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
d7270 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
d7280 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
d7290 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a  TE_IOERR_LOCK);.
d72a0 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43        if( IS_LOC
d72b0 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20  K_ERROR(rc) ){. 
d72c0 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
d72d0 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
d72e0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
d72f0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
d7300 20 7d 20 0a 20 20 69 66 28 20 63 6c 6f 73 65 28   } .  if( close(
d7310 66 64 29 20 29 7b 0a 20 20 20 20 70 46 69 6c 65  fd) ){.    pFile
d7320 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
d7330 72 6e 6f 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  rno;.    rc = SQ
d7340 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45  LITE_IOERR_CLOSE
d7350 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 6f  ;.  }.  .  /* go
d7360 74 20 69 74 2c 20 73 65 74 20 74 68 65 20 74 79  t it, set the ty
d7370 70 65 20 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b  pe and return ok
d7380 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63   */.  pFile->loc
d7390 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65  ktype = locktype
d73a0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
d73b0 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68  ../*.** Lower th
d73c0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
d73d0 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  on file descript
d73e0 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b  or pFile to lock
d73f0 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a  type.  locktype.
d7400 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74 68 65  ** must be eithe
d7410 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41  r NO_LOCK or SHA
d7420 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20  RED_LOCK..**.** 
d7430 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  If the locking l
d7440 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65  evel of the file
d7450 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 61   descriptor is a
d7460 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c  lready at or bel
d7470 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73  ow.** the reques
d7480 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  ted locking leve
d7490 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  l, this routine 
d74a0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
d74b0 2a 20 57 68 65 6e 20 74 68 65 20 6c 6f 63 6b 69  * When the locki
d74c0 6e 67 20 6c 65 76 65 6c 20 72 65 61 63 68 65 73  ng level reaches
d74d0 20 4e 4f 5f 4c 4f 43 4b 2c 20 64 65 6c 65 74 65   NO_LOCK, delete
d74e0 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a   the lock file..
d74f0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f  */.static int do
d7500 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69  tlockUnlock(sqli
d7510 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
d7520 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20  t locktype) {.  
d7530 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
d7540 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
d7550 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69  .  char *zLockFi
d7560 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 70 46 69  le = (char *)pFi
d7570 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
d7580 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  xt;..  assert( p
d7590 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43  File );.  OSTRAC
d75a0 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25  E5("UNLOCK  %d %
d75b0 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c  d was %d pid=%d\
d75c0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
d75d0 63 6b 74 79 70 65 2c 0a 09 20 20 20 70 46 69 6c  cktype,..   pFil
d75e0 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74  e->locktype, get
d75f0 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65 72 74  pid());.  assert
d7600 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52  ( locktype<=SHAR
d7610 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20  ED_LOCK );.  .  
d7620 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73  /* no-op if poss
d7630 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46  ible */.  if( pF
d7640 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c  ile->locktype==l
d7650 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72  ocktype ){.    r
d7660 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
d7670 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 20 64 6f  .  }..  /* To do
d7680 77 6e 67 72 61 64 65 20 74 6f 20 73 68 61 72 65  wngrade to share
d7690 64 2c 20 73 69 6d 70 6c 79 20 75 70 64 61 74 65  d, simply update
d76a0 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f   our internal no
d76b0 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 2a 2a  tion of the.  **
d76c0 20 6c 6f 63 6b 20 73 74 61 74 65 2e 20 20 4e 6f   lock state.  No
d76d0 20 6e 65 65 64 20 74 6f 20 6d 65 73 73 20 77 69   need to mess wi
d76e0 74 68 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64  th the file on d
d76f0 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  isk..  */.  if( 
d7700 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
d7710 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69  _LOCK ){.    pFi
d7720 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53  le->locktype = S
d7730 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20  HARED_LOCK;.    
d7740 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d7750 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 54 6f  ;.  }.  .  /* To
d7760 20 66 75 6c 6c 79 20 75 6e 6c 6f 63 6b 20 74 68   fully unlock th
d7770 65 20 64 61 74 61 62 61 73 65 2c 20 64 65 6c 65  e database, dele
d7780 74 65 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65  te the lock file
d7790 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6c 6f   */.  assert( lo
d77a0 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20  cktype==NO_LOCK 
d77b0 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6e 6b 28  );.  if( unlink(
d77c0 7a 4c 6f 63 6b 46 69 6c 65 29 20 29 7b 0a 20 20  zLockFile) ){.  
d77d0 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20    int rc = 0;.  
d77e0 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65    int tErrno = e
d77f0 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20 45 4e  rrno;.    if( EN
d7800 4f 45 4e 54 20 21 3d 20 74 45 72 72 6e 6f 20 29  OENT != tErrno )
d7810 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
d7820 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
d7830 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53  xError(tErrno, S
d7840 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f  QLITE_IOERR_UNLO
d7850 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  CK);.    }.    i
d7860 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  f( IS_LOCK_ERROR
d7870 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46  (rc) ){.      pF
d7880 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
d7890 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20   tErrno;.    }. 
d78a0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a 20     return rc; . 
d78b0 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   }.  pFile->lock
d78c0 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a  type = NO_LOCK;.
d78d0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d78e0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f  OK;.}../*.** Clo
d78f0 73 65 20 61 20 66 69 6c 65 2e 20 20 4d 61 6b 65  se a file.  Make
d7900 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 20 68   sure the lock h
d7910 61 73 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64  as been released
d7920 20 62 65 66 6f 72 65 20 63 6c 6f 73 69 6e 67 2e   before closing.
d7930 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
d7940 6f 74 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69  otlockClose(sqli
d7950 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a  te3_file *id) {.
d7960 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20    int rc;.  if( 
d7970 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69  id ){.    unixFi
d7980 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
d7990 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 64  xFile*)id;.    d
d79a0 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c  otlockUnlock(id,
d79b0 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 73   NO_LOCK);.    s
d79c0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c  qlite3_free(pFil
d79d0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
d79e0 74 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 63  t);.  }.  rc = c
d79f0 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29  loseUnixFile(id)
d7a00 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
d7a10 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
d7a20 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20  **** End of the 
d7a30 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 20 69 6d  dot-file lock im
d7a40 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a  plementation ***
d7a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7a60 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
d7a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
d7ab0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
d7ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7b00 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
d7b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
d7b20 69 6e 20 66 6c 6f 63 6b 20 4c 6f 63 6b 69 6e 67  in flock Locking
d7b30 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
d7b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7b50 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20  *.**.** Use the 
d7b60 66 6c 6f 63 6b 28 29 20 73 79 73 74 65 6d 20 63  flock() system c
d7b70 61 6c 6c 20 74 6f 20 64 6f 20 66 69 6c 65 20 6c  all to do file l
d7b80 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 66 6c  ocking..**.** fl
d7b90 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20 69 73  ock() locking is
d7ba0 20 6c 69 6b 65 20 64 6f 74 2d 66 69 6c 65 20 6c   like dot-file l
d7bb0 6f 63 6b 69 6e 67 20 69 6e 20 74 68 61 74 20 74  ocking in that t
d7bc0 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 66 69  he various.** fi
d7bd0 6e 65 2d 67 72 61 69 6e 20 6c 6f 63 6b 69 6e 67  ne-grain locking
d7be0 20 6c 65 76 65 6c 73 20 73 75 70 70 6f 72 74 65   levels supporte
d7bf0 64 20 62 79 20 53 51 4c 69 74 65 20 61 72 65 20  d by SQLite are 
d7c00 63 6f 6c 6c 61 70 73 65 64 20 69 6e 74 6f 0a 2a  collapsed into.*
d7c10 2a 20 61 20 73 69 6e 67 6c 65 20 65 78 63 6c 75  * a single exclu
d7c20 73 69 76 65 20 6c 6f 63 6b 2e 20 20 49 6e 20 6f  sive lock.  In o
d7c30 74 68 65 72 20 77 6f 72 64 73 2c 20 53 48 41 52  ther words, SHAR
d7c40 45 44 2c 20 52 45 53 45 52 56 45 44 2c 20 61 6e  ED, RESERVED, an
d7c50 64 0a 2a 2a 20 50 45 4e 44 49 4e 47 20 6c 6f 63  d.** PENDING loc
d7c60 6b 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 20  ks are the same 
d7c70 74 68 69 6e 67 20 61 73 20 61 6e 20 45 58 43 4c  thing as an EXCL
d7c80 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 53 51 4c  USIVE lock.  SQL
d7c90 69 74 65 0a 2a 2a 20 73 74 69 6c 6c 20 77 6f 72  ite.** still wor
d7ca0 6b 73 20 77 68 65 6e 20 79 6f 75 20 64 6f 20 74  ks when you do t
d7cb0 68 69 73 2c 20 62 75 74 20 63 6f 6e 63 75 72 72  his, but concurr
d7cc0 65 6e 63 79 20 69 73 20 72 65 64 75 63 65 64 20  ency is reduced 
d7cd0 73 69 6e 63 65 0a 2a 2a 20 6f 6e 6c 79 20 61 20  since.** only a 
d7ce0 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 63  single process c
d7cf0 61 6e 20 62 65 20 72 65 61 64 69 6e 67 20 74 68  an be reading th
d7d00 65 20 64 61 74 61 62 61 73 65 20 61 74 20 61 20  e database at a 
d7d10 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6d 69 74  time..**.** Omit
d7d20 20 74 68 69 73 20 73 65 63 74 69 6f 6e 20 69 66   this section if
d7d30 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
d7d40 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 69 73 20  OCKING_STYLE is 
d7d50 74 75 72 6e 65 64 20 6f 66 66 20 6f 72 20 69 66  turned off or if
d7d60 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f  .** compiling fo
d7d70 72 20 56 58 57 4f 52 4b 53 2e 0a 2a 2f 0a 23 69  r VXWORKS..*/.#i
d7d80 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
d7d90 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26  LOCKING_STYLE &&
d7da0 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 0a 2f 2a   !OS_VXWORKS../*
d7db0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
d7dc0 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65   checks if there
d7dd0 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c   is a RESERVED l
d7de0 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20  ock held on the 
d7df0 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c  specified.** fil
d7e00 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79  e by this or any
d7e10 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20   other process. 
d7e20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69  If such a lock i
d7e30 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65  s held, set *pRe
d7e40 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e  sOut.** to a non
d7e50 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65  -zero value othe
d7e60 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69  rwise *pResOut i
d7e70 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20  s set to zero.  
d7e80 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
d7e90 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51  .** is set to SQ
d7ea0 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61  LITE_OK unless a
d7eb0 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75  n I/O error occu
d7ec0 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63  rs during lock c
d7ed0 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74  hecking..*/.stat
d7ee0 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43 68 65 63  ic int flockChec
d7ef0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71  kReservedLock(sq
d7f00 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
d7f10 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20  int *pResOut){. 
d7f20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
d7f30 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72  _OK;.  int reser
d7f40 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46  ved = 0;.  unixF
d7f50 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
d7f60 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20  ixFile*)id;.  . 
d7f70 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d7f80 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
d7f90 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52  IOERR_CHECKRESER
d7fa0 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20  VEDLOCK; );.  . 
d7fb0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
d7fc0 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20  ;.  .  /* Check 
d7fd0 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20 74  if a thread in t
d7fe0 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64  his process hold
d7ff0 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f  s such a lock */
d8000 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
d8010 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f  cktype>SHARED_LO
d8020 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76  CK ){.    reserv
d8030 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20  ed = 1;.  }.  . 
d8040 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65   /* Otherwise se
d8050 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20  e if some other 
d8060 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74  process holds it
d8070 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65  . */.  if( !rese
d8080 72 76 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 61  rved ){.    /* a
d8090 74 74 65 6d 70 74 20 74 6f 20 67 65 74 20 74 68  ttempt to get th
d80a0 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e  e lock */.    in
d80b0 74 20 6c 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46  t lrc = flock(pF
d80c0 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20  ile->h, LOCK_EX 
d80d0 7c 20 4c 4f 43 4b 5f 4e 42 29 3b 0a 20 20 20 20  | LOCK_NB);.    
d80e0 69 66 28 20 21 6c 72 63 20 29 7b 0a 20 20 20 20  if( !lrc ){.    
d80f0 20 20 2f 2a 20 67 6f 74 20 74 68 65 20 6c 6f 63    /* got the loc
d8100 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 2a 2f 0a  k, unlock it */.
d8110 20 20 20 20 20 20 6c 72 63 20 3d 20 66 6c 6f 63        lrc = floc
d8120 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b  k(pFile->h, LOCK
d8130 5f 55 4e 29 3b 0a 20 20 20 20 20 20 69 66 20 28  _UN);.      if (
d8140 20 6c 72 63 20 29 20 7b 0a 20 20 20 20 20 20 20   lrc ) {.       
d8150 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72   int tErrno = er
d8160 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20  rno;.        /* 
d8170 75 6e 6c 6f 63 6b 20 66 61 69 6c 65 64 20 77 69  unlock failed wi
d8180 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20  th an error */. 
d8190 20 20 20 20 20 20 20 6c 72 63 20 3d 20 73 71 6c         lrc = sql
d81a0 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
d81b0 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53  xError(tErrno, S
d81c0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f  QLITE_IOERR_UNLO
d81d0 43 4b 29 3b 20 0a 20 20 20 20 20 20 20 20 69 66  CK); .        if
d81e0 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  ( IS_LOCK_ERROR(
d81f0 6c 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20  lrc) ){.        
d8200 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d8210 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
d8220 20 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 3b         rc = lrc;
d8230 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
d8240 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a   }.    } else {.
d8250 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f        int tErrno
d8260 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
d8270 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20  reserved = 1;.  
d8280 20 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65      /* someone e
d8290 6c 73 65 20 6d 69 67 68 74 20 68 61 76 65 20 69  lse might have i
d82a0 74 20 72 65 73 65 72 76 65 64 20 2a 2f 0a 20 20  t reserved */.  
d82b0 20 20 20 20 6c 72 63 20 3d 20 73 71 6c 69 74 65      lrc = sqlite
d82c0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
d82d0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
d82e0 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 20  TE_IOERR_LOCK); 
d82f0 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f  .      if( IS_LO
d8300 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20 29 7b  CK_ERROR(lrc) ){
d8310 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  .        pFile->
d8320 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72  lastErrno = tErr
d8330 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  no;.        rc =
d8340 20 6c 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20   lrc;.      }.  
d8350 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43    }.  }.  OSTRAC
d8360 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b  E4("TEST WR-LOCK
d8370 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46   %d %d %d\n", pF
d8380 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65  ile->h, rc, rese
d8390 72 76 65 64 29 3b 0a 0a 23 69 66 64 65 66 20 53  rved);..#ifdef S
d83a0 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f  QLITE_IGNORE_FLO
d83b0 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20  CK_LOCK_ERRORS. 
d83c0 20 69 66 28 20 28 72 63 20 26 20 53 51 4c 49 54   if( (rc & SQLIT
d83d0 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49  E_IOERR) == SQLI
d83e0 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20  TE_IOERR ){.    
d83f0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
d8400 20 20 20 20 72 65 73 65 72 76 65 64 3d 31 3b 0a      reserved=1;.
d8410 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51    }.#endif /* SQ
d8420 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43  LITE_IGNORE_FLOC
d8430 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f  K_LOCK_ERRORS */
d8440 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65  .  *pResOut = re
d8450 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e  served;.  return
d8460 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f   rc;.}../*.** Lo
d8470 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68  ck the file with
d8480 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66   the lock specif
d8490 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72  ied by parameter
d84a0 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a   locktype - one.
d84b0 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ** of the follow
d84c0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28  ing:.**.**     (
d84d0 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a  1) SHARED_LOCK.*
d84e0 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56  *     (2) RESERV
d84f0 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28  ED_LOCK.**     (
d8500 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a  3) PENDING_LOCK.
d8510 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55  **     (4) EXCLU
d8520 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20  SIVE_LOCK.**.** 
d8530 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72  Sometimes when r
d8540 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f  equesting one lo
d8550 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69  ck state, additi
d8560 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73  onal lock states
d8570 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64  .** are inserted
d8580 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68   in between.  Th
d8590 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20  e locking might 
d85a0 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74  fail on one of t
d85b0 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e  he later.** tran
d85c0 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20  sitions leaving 
d85d0 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64  the lock state d
d85e0 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68  ifferent from wh
d85f0 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75  at it started bu
d8600 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74  t.** still short
d8610 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54   of its goal.  T
d8620 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61  he following cha
d8630 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c  rt shows the all
d8640 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69  owed.** transiti
d8650 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65  ons and the inse
d8660 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74  rted intermediat
d8670 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20  e states:.**.** 
d8680 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53     UNLOCKED -> S
d8690 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52  HARED.**    SHAR
d86a0 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a  ED -> RESERVED.*
d86b0 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28  *    SHARED -> (
d86c0 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c  PENDING) -> EXCL
d86d0 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45  USIVE.**    RESE
d86e0 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47  RVED -> (PENDING
d86f0 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  ) -> EXCLUSIVE.*
d8700 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20  *    PENDING -> 
d8710 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20  EXCLUSIVE.**.** 
d8720 66 6c 6f 63 6b 28 29 20 6f 6e 6c 79 20 72 65 61  flock() only rea
d8730 6c 6c 79 20 73 75 70 70 6f 72 74 20 45 58 43 4c  lly support EXCL
d8740 55 53 49 56 45 20 6c 6f 63 6b 73 2e 20 20 57 65  USIVE locks.  We
d8750 20 74 72 61 63 6b 20 69 6e 74 65 72 6d 65 64 69   track intermedi
d8760 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20 73 74 61 74  ate.** lock stat
d8770 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  es in the sqlite
d8780 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75 72 65  3_file structure
d8790 2c 20 62 75 74 20 61 6c 6c 20 6c 6f 63 6b 73 20  , but all locks 
d87a0 53 48 41 52 45 44 20 6f 72 0a 2a 2a 20 61 62 6f  SHARED or.** abo
d87b0 76 65 20 61 72 65 20 72 65 61 6c 6c 79 20 45 58  ve are really EX
d87c0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 20 61 6e  CLUSIVE locks an
d87d0 64 20 65 78 63 6c 75 64 65 20 61 6c 6c 20 6f 74  d exclude all ot
d87e0 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 66 72  her processes fr
d87f0 6f 6d 0a 2a 2a 20 61 63 63 65 73 73 20 74 68 65  om.** access the
d8800 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   file..**.** Thi
d8810 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f  s routine will o
d8820 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c  nly increase a l
d8830 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71  ock.  Use the sq
d8840 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a  lite3OsUnlock().
d8850 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f  ** routine to lo
d8860 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65  wer a locking le
d8870 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  vel..*/.static i
d8880 6e 74 20 66 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c  nt flockLock(sql
d8890 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
d88a0 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20  nt locktype) {. 
d88b0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
d88c0 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20  _OK;.  unixFile 
d88d0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
d88e0 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72  le*)id;..  asser
d88f0 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f  t( pFile );..  /
d8900 2a 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 20  * if we already 
d8910 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20  have a lock, it 
d8920 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a  is exclusive.  .
d8930 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74    ** Just adjust
d8940 20 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20   level and punt 
d8950 6f 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a  on outta here. *
d8960 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c  /.  if (pFile->l
d8970 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43  ocktype > NO_LOC
d8980 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  K) {.    pFile->
d8990 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74  locktype = lockt
d89a0 79 70 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ype;.    return 
d89b0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
d89c0 20 0a 20 20 2f 2a 20 67 72 61 62 20 61 6e 20 65   .  /* grab an e
d89d0 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f  xclusive lock */
d89e0 0a 20 20 0a 20 20 69 66 20 28 66 6c 6f 63 6b 28  .  .  if (flock(
d89f0 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45  pFile->h, LOCK_E
d8a00 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 29 20 7b 0a  X | LOCK_NB)) {.
d8a10 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d      int tErrno =
d8a20 20 65 72 72 6e 6f 3b 0a 20 20 20 20 2f 2a 20 64   errno;.    /* d
d8a30 69 64 6e 27 74 20 67 65 74 2c 20 6d 75 73 74 20  idn't get, must 
d8a40 62 65 20 62 75 73 79 20 2a 2f 0a 20 20 20 20 72  be busy */.    r
d8a50 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46  c = sqliteErrorF
d8a60 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45  romPosixError(tE
d8a70 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45  rrno, SQLITE_IOE
d8a80 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66  RR_LOCK);.    if
d8a90 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  ( IS_LOCK_ERROR(
d8aa0 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69  rc) ){.      pFi
d8ab0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d8ac0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20  tErrno;.    }.  
d8ad0 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20  } else {.    /* 
d8ae0 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65 20  got it, set the 
d8af0 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e 20  type and return 
d8b00 6f 6b 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65 2d  ok */.    pFile-
d8b10 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b  >locktype = lock
d8b20 74 79 70 65 3b 0a 20 20 7d 0a 20 20 4f 53 54 52  type;.  }.  OSTR
d8b30 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20 25 64  ACE4("LOCK    %d
d8b40 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65   %s %s\n", pFile
d8b50 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d  ->h, locktypeNam
d8b60 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20  e(locktype), .  
d8b70 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c           rc==SQL
d8b80 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20  ITE_OK ? "ok" : 
d8b90 22 66 61 69 6c 65 64 22 29 3b 0a 23 69 66 64 65  "failed");.#ifde
d8ba0 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f  f SQLITE_IGNORE_
d8bb0 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52  FLOCK_LOCK_ERROR
d8bc0 53 0a 20 20 69 66 28 20 28 72 63 20 26 20 53 51  S.  if( (rc & SQ
d8bd0 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53  LITE_IOERR) == S
d8be0 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20  QLITE_IOERR ){. 
d8bf0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
d8c00 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20  USY;.  }.#endif 
d8c10 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  /* SQLITE_IGNORE
d8c20 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f  _FLOCK_LOCK_ERRO
d8c30 52 53 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  RS */.  return r
d8c40 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77  c;.}.../*.** Low
d8c50 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  er the locking l
d8c60 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73  evel on file des
d8c70 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f  criptor pFile to
d8c80 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b   locktype.  lock
d8c90 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20  type.** must be 
d8ca0 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f  either NO_LOCK o
d8cb0 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a  r SHARED_LOCK..*
d8cc0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b  *.** If the lock
d8cd0 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65  ing level of the
d8ce0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
d8cf0 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f   is already at o
d8d00 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72  r below.** the r
d8d10 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67  equested locking
d8d20 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75   level, this rou
d8d30 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tine is a no-op.
d8d40 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
d8d50 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74  lockUnlock(sqlit
d8d60 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
d8d70 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75   locktype) {.  u
d8d80 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
d8d90 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
d8da0 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69    .  assert( pFi
d8db0 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35  le );.  OSTRACE5
d8dc0 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20  ("UNLOCK  %d %d 
d8dd0 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e 22  was %d pid=%d\n"
d8de0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b  , pFile->h, lock
d8df0 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20 20  type,.          
d8e00 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d8e10 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 61  , getpid());.  a
d8e20 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c  ssert( locktype<
d8e30 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
d8e40 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66    .  /* no-op if
d8e50 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69   possible */.  i
d8e60 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
d8e70 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  pe==locktype ){.
d8e80 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d8e90 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f  E_OK;.  }.  .  /
d8ea0 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a 75 73  * shared can jus
d8eb0 74 20 62 65 20 73 65 74 20 62 65 63 61 75 73 65  t be set because
d8ec0 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 65 20   we always have 
d8ed0 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a 2f 0a  an exclusive */.
d8ee0 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 3d 3d    if (locktype==
d8ef0 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20  SHARED_LOCK) {. 
d8f00 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
d8f10 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20  pe = locktype;. 
d8f20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
d8f30 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a  _OK;.  }.  .  /*
d8f40 20 6e 6f 2c 20 72 65 61 6c 6c 79 2c 20 75 6e 6c   no, really, unl
d8f50 6f 63 6b 2e 20 2a 2f 0a 20 20 69 6e 74 20 72 63  ock. */.  int rc
d8f60 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e   = flock(pFile->
d8f70 68 2c 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20 20 69  h, LOCK_UN);.  i
d8f80 66 20 28 72 63 29 20 7b 0a 20 20 20 20 69 6e 74  f (rc) {.    int
d8f90 20 72 2c 20 74 45 72 72 6e 6f 20 3d 20 65 72 72   r, tErrno = err
d8fa0 6e 6f 3b 0a 20 20 20 20 72 20 3d 20 73 71 6c 69  no;.    r = sqli
d8fb0 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78  teErrorFromPosix
d8fc0 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51  Error(tErrno, SQ
d8fd0 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43  LITE_IOERR_UNLOC
d8fe0 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f 4c  K);.    if( IS_L
d8ff0 4f 43 4b 5f 45 52 52 4f 52 28 72 29 20 29 7b 0a  OCK_ERROR(r) ){.
d9000 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
d9010 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
d9020 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51  .    }.#ifdef SQ
d9030 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43  LITE_IGNORE_FLOC
d9040 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20  K_LOCK_ERRORS.  
d9050 20 20 69 66 28 20 28 72 20 26 20 53 51 4c 49 54    if( (r & SQLIT
d9060 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49  E_IOERR) == SQLI
d9070 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20  TE_IOERR ){.    
d9080 20 20 72 20 3d 20 53 51 4c 49 54 45 5f 42 55 53    r = SQLITE_BUS
d9090 59 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20  Y;.    }.#endif 
d90a0 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  /* SQLITE_IGNORE
d90b0 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f  _FLOCK_LOCK_ERRO
d90c0 52 53 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 72  RS */.    .    r
d90d0 65 74 75 72 6e 20 72 3b 0a 20 20 7d 20 65 6c 73  eturn r;.  } els
d90e0 65 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c  e {.    pFile->l
d90f0 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43  ocktype = NO_LOC
d9100 4b 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  K;.    return SQ
d9110 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a  LITE_OK;.  }.}..
d9120 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69  /*.** Close a fi
d9130 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
d9140 74 20 66 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c  t flockClose(sql
d9150 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b  ite3_file *id) {
d9160 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20  .  if( id ){.   
d9170 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c   flockUnlock(id,
d9180 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 7d 0a 20   NO_LOCK);.  }. 
d9190 20 72 65 74 75 72 6e 20 63 6c 6f 73 65 55 6e 69   return closeUni
d91a0 78 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a 23 65  xFile(id);.}..#e
d91b0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45  ndif /* SQLITE_E
d91c0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
d91d0 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52  YLE && !OS_VXWOR
d91e0 4b 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  K */../*********
d91f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
d9200 66 20 74 68 65 20 66 6c 6f 63 6b 20 6c 6f 63 6b  f the flock lock
d9210 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
d9220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9230 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *****.**********
d9240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9280 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
d9290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d92a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d92b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d92c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d92d0 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ******.*********
d92e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
d92f0 42 65 67 69 6e 20 4e 61 6d 65 64 20 53 65 6d 61  Begin Named Sema
d9300 70 68 6f 72 65 20 4c 6f 63 6b 69 6e 67 20 2a 2a  phore Locking **
d9310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9320 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 61 6d  ******.**.** Nam
d9330 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63  ed semaphore loc
d9340 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 73 75 70  king is only sup
d9350 70 6f 72 74 65 64 20 6f 6e 20 56 78 57 6f 72 6b  ported on VxWork
d9360 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70 68 6f  s..**.** Semapho
d9370 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6c 69  re locking is li
d9380 6b 65 20 64 6f 74 2d 6c 6f 63 6b 20 61 6e 64 20  ke dot-lock and 
d9390 66 6c 6f 63 6b 20 69 6e 20 74 68 61 74 20 69 74  flock in that it
d93a0 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 0a 2a 2a 20   really only.** 
d93b0 73 75 70 70 6f 72 74 73 20 45 58 43 4c 55 53 49  supports EXCLUSI
d93c0 56 45 20 6c 6f 63 6b 69 6e 67 2e 20 20 4f 6e 6c  VE locking.  Onl
d93d0 79 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 63 65  y a single proce
d93e0 73 73 20 63 61 6e 20 72 65 61 64 20 6f 72 20 77  ss can read or w
d93f0 72 69 74 65 0a 2a 2a 20 74 68 65 20 64 61 74 61  rite.** the data
d9400 62 61 73 65 20 66 69 6c 65 20 61 74 20 61 20 74  base file at a t
d9410 69 6d 65 2e 20 20 54 68 69 73 20 72 65 64 75 63  ime.  This reduc
d9420 65 73 20 70 6f 74 65 6e 74 69 61 6c 20 63 6f 6e  es potential con
d9430 63 75 72 72 65 6e 63 79 2c 20 62 75 74 0a 2a 2a  currency, but.**
d9440 20 6d 61 6b 65 73 20 74 68 65 20 6c 6f 63 6b 20   makes the lock 
d9450 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d  implementation m
d9460 75 63 68 20 65 61 73 69 65 72 2e 0a 2a 2f 0a 23  uch easier..*/.#
d9470 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 0a 2f  if OS_VXWORKS../
d9480 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
d9490 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72  e checks if ther
d94a0 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20  e is a RESERVED 
d94b0 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65  lock held on the
d94c0 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69   specified.** fi
d94d0 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e  le by this or an
d94e0 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e  y other process.
d94f0 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20   If such a lock 
d9500 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52  is held, set *pR
d9510 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f  esOut.** to a no
d9520 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68  n-zero value oth
d9530 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20  erwise *pResOut 
d9540 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20  is set to zero. 
d9550 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
d9560 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53  e.** is set to S
d9570 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20  QLITE_OK unless 
d9580 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63  an I/O error occ
d9590 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20  urs during lock 
d95a0 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61  checking..*/.sta
d95b0 74 69 63 20 69 6e 74 20 73 65 6d 43 68 65 63 6b  tic int semCheck
d95c0 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c  ReservedLock(sql
d95d0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
d95e0 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a 20  nt *pResOut) {. 
d95f0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
d9600 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72  _OK;.  int reser
d9610 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46  ved = 0;.  unixF
d9620 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
d9630 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 53  ixFile*)id;..  S
d9640 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
d9650 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
d9660 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45  ERR_CHECKRESERVE
d9670 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61  DLOCK; );.  .  a
d9680 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a  ssert( pFile );.
d9690 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61  .  /* Check if a
d96a0 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20   thread in this 
d96b0 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75  process holds su
d96c0 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69  ch a lock */.  i
d96d0 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
d96e0 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  pe>SHARED_LOCK )
d96f0 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d  {.    reserved =
d9700 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20   1;.  }.  .  /* 
d9710 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69 66  Otherwise see if
d9720 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63   some other proc
d9730 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 20 2a 2f  ess holds it. */
d9740 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65 64  .  if( !reserved
d9750 20 29 7b 0a 20 20 20 20 73 65 6d 5f 74 20 2a 70   ){.    sem_t *p
d9760 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70  Sem = pFile->pOp
d9770 65 6e 2d 3e 70 53 65 6d 3b 0a 20 20 20 20 73 74  en->pSem;.    st
d9780 72 75 63 74 20 73 74 61 74 20 73 74 61 74 42 75  ruct stat statBu
d9790 66 3b 0a 0a 20 20 20 20 69 66 28 20 73 65 6d 5f  f;..    if( sem_
d97a0 74 72 79 77 61 69 74 28 70 53 65 6d 29 3d 3d 2d  trywait(pSem)==-
d97b0 31 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 74  1 ){.      int t
d97c0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
d97d0 20 20 20 20 20 69 66 28 20 45 41 47 41 49 4e 20       if( EAGAIN 
d97e0 21 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20  != tErrno ){.   
d97f0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
d9800 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
d9810 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
d9820 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45  TE_IOERR_CHECKRE
d9830 53 45 52 56 45 44 4c 4f 43 4b 29 3b 0a 20 20 20  SERVEDLOCK);.   
d9840 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
d9850 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a  Errno = tErrno;.
d9860 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20        } else {. 
d9870 20 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e         /* someon
d9880 65 20 65 6c 73 65 20 68 61 73 20 74 68 65 20 6c  e else has the l
d9890 6f 63 6b 20 77 68 65 6e 20 77 65 20 61 72 65 20  ock when we are 
d98a0 69 6e 20 4e 4f 5f 4c 4f 43 4b 20 2a 2f 0a 20 20  in NO_LOCK */.  
d98b0 20 20 20 20 20 20 72 65 73 65 72 76 65 64 20 3d        reserved =
d98c0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70   (pFile->locktyp
d98d0 65 20 3c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29  e < SHARED_LOCK)
d98e0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
d98f0 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 77 65  lse{.      /* we
d9900 20 63 6f 75 6c 64 20 68 61 76 65 20 69 74 20 69   could have it i
d9910 66 20 77 65 20 77 61 6e 74 20 69 74 20 2a 2f 0a  f we want it */.
d9920 20 20 20 20 20 20 73 65 6d 5f 70 6f 73 74 28 70        sem_post(p
d9930 53 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Sem);.    }.  }.
d9940 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 54    OSTRACE4("TEST
d9950 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25   WR-LOCK %d %d %
d9960 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
d9970 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 0a  rc, reserved);..
d9980 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73    *pResOut = res
d9990 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20  erved;.  return 
d99a0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63  rc;.}../*.** Loc
d99b0 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20  k the file with 
d99c0 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69  the lock specifi
d99d0 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20  ed by parameter 
d99e0 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a  locktype - one.*
d99f0 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * of the followi
d9a00 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31  ng:.**.**     (1
d9a10 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a  ) SHARED_LOCK.**
d9a20 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45       (2) RESERVE
d9a30 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33  D_LOCK.**     (3
d9a40 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a  ) PENDING_LOCK.*
d9a50 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53  *     (4) EXCLUS
d9a60 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53  IVE_LOCK.**.** S
d9a70 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65  ometimes when re
d9a80 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63  questing one loc
d9a90 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f  k state, additio
d9aa0 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a  nal lock states.
d9ab0 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20  ** are inserted 
d9ac0 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65  in between.  The
d9ad0 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66   locking might f
d9ae0 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68  ail on one of th
d9af0 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73  e later.** trans
d9b00 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74  itions leaving t
d9b10 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69  he lock state di
d9b20 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61  fferent from wha
d9b30 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74  t it started but
d9b40 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20  .** still short 
d9b50 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68  of its goal.  Th
d9b60 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72  e following char
d9b70 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f  t shows the allo
d9b80 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  wed.** transitio
d9b90 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72  ns and the inser
d9ba0 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65  ted intermediate
d9bb0 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20   states:.**.**  
d9bc0 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48    UNLOCKED -> SH
d9bd0 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45  ARED.**    SHARE
d9be0 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a  D -> RESERVED.**
d9bf0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50      SHARED -> (P
d9c00 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55  ENDING) -> EXCLU
d9c10 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52  SIVE.**    RESER
d9c20 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29  VED -> (PENDING)
d9c30 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
d9c40 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45      PENDING -> E
d9c50 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 53  XCLUSIVE.**.** S
d9c60 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 73 20 6f  emaphore locks o
d9c70 6e 6c 79 20 72 65 61 6c 6c 79 20 73 75 70 70 6f  nly really suppo
d9c80 72 74 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  rt EXCLUSIVE loc
d9c90 6b 73 2e 20 20 57 65 20 74 72 61 63 6b 20 69 6e  ks.  We track in
d9ca0 74 65 72 6d 65 64 69 61 74 65 0a 2a 2a 20 6c 6f  termediate.** lo
d9cb0 63 6b 20 73 74 61 74 65 73 20 69 6e 20 74 68 65  ck states in the
d9cc0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74   sqlite3_file st
d9cd0 72 75 63 74 75 72 65 2c 20 62 75 74 20 61 6c 6c  ructure, but all
d9ce0 20 6c 6f 63 6b 73 20 53 48 41 52 45 44 20 6f 72   locks SHARED or
d9cf0 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 72 65  .** above are re
d9d00 61 6c 6c 79 20 45 58 43 4c 55 53 49 56 45 20 6c  ally EXCLUSIVE l
d9d10 6f 63 6b 73 20 61 6e 64 20 65 78 63 6c 75 64 65  ocks and exclude
d9d20 20 61 6c 6c 20 6f 74 68 65 72 20 70 72 6f 63 65   all other proce
d9d30 73 73 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 63 63  sses from.** acc
d9d40 65 73 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a  ess the file..**
d9d50 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
d9d60 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65   will only incre
d9d70 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65  ase a lock.  Use
d9d80 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e   the sqlite3OsUn
d9d90 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e  lock().** routin
d9da0 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63  e to lower a loc
d9db0 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73  king level..*/.s
d9dc0 74 61 74 69 63 20 69 6e 74 20 73 65 6d 4c 6f 63  tatic int semLoc
d9dd0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
d9de0 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
d9df0 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  ) {.  unixFile *
d9e00 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
d9e10 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 66 64 3b  e*)id;.  int fd;
d9e20 0a 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d  .  sem_t *pSem =
d9e30 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70   pFile->pOpen->p
d9e40 53 65 6d 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  Sem;.  int rc = 
d9e50 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a  SQLITE_OK;..  /*
d9e60 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 20 68   if we already h
d9e70 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20 69  ave a lock, it i
d9e80 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a 20  s exclusive.  . 
d9e90 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74 20   ** Just adjust 
d9ea0 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20 6f  level and punt o
d9eb0 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a 2f  n outta here. */
d9ec0 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f  .  if (pFile->lo
d9ed0 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b  cktype > NO_LOCK
d9ee0 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ) {.    pFile->l
d9ef0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79  ocktype = lockty
d9f00 70 65 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  pe;.    rc = SQL
d9f10 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 67 6f 74 6f  ITE_OK;.    goto
d9f20 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20   sem_end_lock;. 
d9f30 20 7d 0a 20 20 0a 20 20 2f 2a 20 6c 6f 63 6b 20   }.  .  /* lock 
d9f40 73 65 6d 61 70 68 6f 72 65 20 6e 6f 77 20 62 75  semaphore now bu
d9f50 74 20 62 61 69 6c 20 6f 75 74 20 77 68 65 6e 20  t bail out when 
d9f60 61 6c 72 65 61 64 79 20 6c 6f 63 6b 65 64 2e 20  already locked. 
d9f70 2a 2f 0a 20 20 69 66 28 20 73 65 6d 5f 74 72 79  */.  if( sem_try
d9f80 77 61 69 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29  wait(pSem)==-1 )
d9f90 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
d9fa0 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f  E_BUSY;.    goto
d9fb0 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20   sem_end_lock;. 
d9fc0 20 7d 0a 0a 20 20 2f 2a 20 67 6f 74 20 69 74 2c   }..  /* got it,
d9fd0 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61 6e   set the type an
d9fe0 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20  d return ok */. 
d9ff0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
da000 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 0a 20 73   = locktype;.. s
da010 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 72  em_end_lock:.  r
da020 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
da030 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63  ** Lower the loc
da040 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69  king level on fi
da050 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 46  le descriptor pF
da060 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e  ile to locktype.
da070 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75    locktype.** mu
da080 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f  st be either NO_
da090 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c  LOCK or SHARED_L
da0a0 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  OCK..**.** If th
da0b0 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
da0c0 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  of the file desc
da0d0 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64  riptor is alread
da0e0 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a  y at or below.**
da0f0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c   the requested l
da100 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68  ocking level, th
da110 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
da120 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63  no-op..*/.static
da130 20 69 6e 74 20 73 65 6d 55 6e 6c 6f 63 6b 28 73   int semUnlock(s
da140 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
da150 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b   int locktype) {
da160 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
da170 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
da180 69 64 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 53 65  id;.  sem_t *pSe
da190 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e  m = pFile->pOpen
da1a0 2d 3e 70 53 65 6d 3b 0a 0a 20 20 61 73 73 65 72  ->pSem;..  asser
da1b0 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 61 73  t( pFile );.  as
da1c0 73 65 72 74 28 20 70 53 65 6d 20 29 3b 0a 20 20  sert( pSem );.  
da1d0 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b  OSTRACE5("UNLOCK
da1e0 20 20 25 64 20 25 64 20 77 61 73 20 25 64 20 70    %d %d was %d p
da1f0 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  id=%d\n", pFile-
da200 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a 09 20  >h, locktype,.. 
da210 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
da220 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20  e, getpid());.  
da230 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
da240 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  <=SHARED_LOCK );
da250 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69  .  .  /* no-op i
da260 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20  f possible */.  
da270 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
da280 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b  ype==locktype ){
da290 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
da2a0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20  TE_OK;.  }.  .  
da2b0 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a 75  /* shared can ju
da2c0 73 74 20 62 65 20 73 65 74 20 62 65 63 61 75 73  st be set becaus
da2d0 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 65  e we always have
da2e0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a 2f   an exclusive */
da2f0 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 3d  .  if (locktype=
da300 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a  =SHARED_LOCK) {.
da310 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
da320 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ype = locktype;.
da330 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
da340 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f  E_OK;.  }.  .  /
da350 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 20 75 6e 6c  * no, really unl
da360 6f 63 6b 2e 20 2a 2f 0a 20 20 69 66 20 28 20 73  ock. */.  if ( s
da370 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29 3d 3d 2d  em_post(pSem)==-
da380 31 20 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 63  1 ) {.    int rc
da390 2c 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  , tErrno = errno
da3a0 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
da3b0 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45  eErrorFromPosixE
da3c0 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c  rror(tErrno, SQL
da3d0 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b  ITE_IOERR_UNLOCK
da3e0 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f  );.    if( IS_LO
da3f0 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a  CK_ERROR(rc) ){.
da400 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
da410 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
da420 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
da430 6e 20 72 63 3b 20 0a 20 20 7d 0a 20 20 70 46 69  n rc; .  }.  pFi
da440 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e  le->locktype = N
da450 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65 74 75 72 6e  O_LOCK;.  return
da460 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
da470 2a 0a 20 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69  *. ** Close a fi
da480 6c 65 2e 0a 20 2a 2f 0a 73 74 61 74 69 63 20 69  le.. */.static i
da490 6e 74 20 73 65 6d 43 6c 6f 73 65 28 73 71 6c 69  nt semClose(sqli
da4a0 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a  te3_file *id) {.
da4b0 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20    if( id ){.    
da4c0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
da4d0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
da4e0 0a 20 20 20 20 73 65 6d 55 6e 6c 6f 63 6b 28 69  .    semUnlock(i
da4f0 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20  d, NO_LOCK);.   
da500 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
da510 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d  ;.    unixEnterM
da520 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 6c 65  utex();.    rele
da530 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c  aseLockInfo(pFil
da540 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72  e->pLock);.    r
da550 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 70 46  eleaseOpenCnt(pF
da560 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20  ile->pOpen);.   
da570 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28   unixLeaveMutex(
da580 29 3b 0a 20 20 20 20 63 6c 6f 73 65 55 6e 69 78  );.    closeUnix
da590 46 69 6c 65 28 69 64 29 3b 0a 20 20 7d 0a 20 20  File(id);.  }.  
da5a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
da5b0 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 4f  ;.}..#endif /* O
da5c0 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 2f 2a 0a  S_VXWORKS */./*.
da5d0 2a 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 6f  ** Named semapho
da5e0 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6f 6e  re locking is on
da5f0 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20  ly available on 
da600 56 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 2a 2a  VxWorks..**.****
da610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
da620 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 73 65 6d  of the named sem
da630 61 70 68 6f 72 65 20 6c 6f 63 6b 20 69 6d 70 6c  aphore lock impl
da640 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a  ementation *****
da650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
da660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a  **********/.../*
da6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
da700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da710 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
da720 41 46 50 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a  AFP Locking ****
da730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
da750 0a 2a 2a 20 41 46 50 20 69 73 20 74 68 65 20 41  .** AFP is the A
da760 70 70 6c 65 20 46 69 6c 69 6e 67 20 50 72 6f 74  pple Filing Prot
da770 6f 63 6f 6c 2e 20 20 41 46 50 20 69 73 20 61 20  ocol.  AFP is a 
da780 6e 65 74 77 6f 72 6b 20 66 69 6c 65 73 79 73 74  network filesyst
da790 65 6d 20 66 6f 75 6e 64 0a 2a 2a 20 6f 6e 20 41  em found.** on A
da7a0 70 70 6c 65 20 4d 61 63 69 6e 74 6f 73 68 20 63  pple Macintosh c
da7b0 6f 6d 70 75 74 65 72 73 20 2d 20 62 6f 74 68 20  omputers - both 
da7c0 4f 53 39 20 61 6e 64 20 4f 53 58 2e 0a 2a 2a 0a  OS9 and OSX..**.
da7d0 2a 2a 20 54 68 69 72 64 2d 70 61 72 74 79 20 69  ** Third-party i
da7e0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f  mplementations o
da7f0 66 20 41 46 50 20 61 72 65 20 61 76 61 69 6c 61  f AFP are availa
da800 62 6c 65 2e 20 20 42 75 74 20 74 68 69 73 20 63  ble.  But this c
da810 6f 64 65 20 68 65 72 65 0a 2a 2a 20 6f 6e 6c 79  ode here.** only
da820 20 77 6f 72 6b 73 20 6f 6e 20 4f 53 58 2e 0a 2a   works on OSX..*
da830 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f  /..#if defined(_
da840 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c  _APPLE__) && SQL
da850 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
da860 4e 47 5f 53 54 59 4c 45 0a 2f 2a 0a 2a 2a 20 54  NG_STYLE./*.** T
da870 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e  he afpLockingCon
da880 74 65 78 74 20 73 74 72 75 63 74 75 72 65 20 63  text structure c
da890 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 61 66 70 20  ontains all afp 
da8a0 6c 6f 63 6b 20 73 70 65 63 69 66 69 63 20 73 74  lock specific st
da8b0 61 74 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ate.*/.typedef s
da8c0 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e 67  truct afpLocking
da8d0 43 6f 6e 74 65 78 74 20 61 66 70 4c 6f 63 6b 69  Context afpLocki
da8e0 6e 67 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63  ngContext;.struc
da8f0 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74  t afpLockingCont
da900 65 78 74 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64  ext {.  unsigned
da910 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73 68 61 72 65   long long share
da920 64 42 79 74 65 3b 0a 20 20 63 6f 6e 73 74 20 63  dByte;.  const c
da930 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 20 20  har *dbPath;    
da940 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
da950 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66 69 6c   of the open fil
da960 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 72 75 63 74 20  e */.};..struct 
da970 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32  ByteRangeLockPB2
da980 0a 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f  .{.  unsigned lo
da990 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 3b 20  ng long offset; 
da9a0 20 20 20 20 20 20 20 2f 2a 20 6f 66 66 73 65 74         /* offset
da9b0 20 74 6f 20 66 69 72 73 74 20 62 79 74 65 20 74   to first byte t
da9c0 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69  o lock */.  unsi
da9d0 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6c  gned long long l
da9e0 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 2f 2a  ength;        /*
da9f0 20 6e 62 72 20 6f 66 20 62 79 74 65 73 20 74 6f   nbr of bytes to
daa00 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67   lock */.  unsig
daa10 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65  ned long long re
daa20 74 52 61 6e 67 65 53 74 61 72 74 3b 20 2f 2a 20  tRangeStart; /* 
daa30 6e 62 72 20 6f 66 20 31 73 74 20 62 79 74 65 20  nbr of 1st byte 
daa40 6c 6f 63 6b 65 64 20 69 66 20 73 75 63 63 65 73  locked if succes
daa50 73 66 75 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  sful */.  unsign
daa60 65 64 20 63 68 61 72 20 75 6e 4c 6f 63 6b 46 6c  ed char unLockFl
daa70 61 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 31  ag;         /* 1
daa80 20 3d 20 75 6e 6c 6f 63 6b 2c 20 30 20 3d 20 6c   = unlock, 0 = l
daa90 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ock */.  unsigne
daaa0 64 20 63 68 61 72 20 73 74 61 72 74 45 6e 64 46  d char startEndF
daab0 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20 31 3d  lag;       /* 1=
daac0 72 65 6c 20 74 6f 20 65 6e 64 20 6f 66 20 66 6f  rel to end of fo
daad0 72 6b 2c 20 30 3d 72 65 6c 20 74 6f 20 73 74 61  rk, 0=rel to sta
daae0 72 74 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b 20  rt */.  int fd; 
daaf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dab00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 69 6c            /* fil
dab10 65 20 64 65 73 63 20 74 6f 20 61 73 73 6f 63 20  e desc to assoc 
dab20 74 68 69 73 20 6c 6f 63 6b 20 77 69 74 68 20 2a  this lock with *
dab30 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20 61 66  /.};..#define af
dab40 70 66 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b  pfsByteRangeLock
dab50 32 46 53 43 54 4c 20 20 20 20 20 20 20 20 5f 49  2FSCTL        _I
dab60 4f 57 52 28 27 7a 27 2c 20 32 33 2c 20 73 74 72  OWR('z', 23, str
dab70 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63  uct ByteRangeLoc
dab80 6b 50 42 32 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  kPB2)../*.** Thi
dab90 73 20 69 73 20 61 20 75 74 69 6c 69 74 79 20 66  s is a utility f
daba0 6f 72 20 73 65 74 74 69 6e 67 20 6f 72 20 63 6c  or setting or cl
dabb0 65 61 72 69 6e 67 20 61 20 62 69 74 2d 72 61 6e  earing a bit-ran
dabc0 67 65 20 6c 6f 63 6b 20 6f 6e 20 61 6e 0a 2a 2a  ge lock on an.**
dabd0 20 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d 2e   AFP filesystem.
dabe0 0a 2a 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 53  .** .** Return S
dabf0 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
dac00 65 73 73 2c 20 53 51 4c 49 54 45 5f 42 55 53 59  ess, SQLITE_BUSY
dac10 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a   on failure..*/.
dac20 73 74 61 74 69 63 20 69 6e 74 20 61 66 70 53 65  static int afpSe
dac30 74 4c 6f 63 6b 28 0a 20 20 63 6f 6e 73 74 20 63  tLock(.  const c
dac40 68 61 72 20 2a 70 61 74 68 2c 20 20 20 20 20 20  har *path,      
dac50 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
dac60 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62  of the file to b
dac70 65 20 6c 6f 63 6b 65 64 20 6f 72 20 75 6e 6c 6f  e locked or unlo
dac80 63 6b 65 64 20 2a 2f 0a 20 20 75 6e 69 78 46 69  cked */.  unixFi
dac90 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 20  le *pFile,      
daca0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e           /* Open
dacb0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
dacc0 20 6f 6e 20 70 61 74 68 20 2a 2f 0a 20 20 75 6e   on path */.  un
dacd0 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
dace0 20 6f 66 66 73 65 74 2c 20 20 20 20 20 2f 2a 20   offset,     /* 
dacf0 46 69 72 73 74 20 62 79 74 65 20 74 6f 20 62 65  First byte to be
dad00 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e 73   locked */.  uns
dad10 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
dad20 6c 65 6e 67 74 68 2c 20 20 20 20 20 2f 2a 20 4e  length,     /* N
dad30 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
dad40 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20  o lock */.  int 
dad50 73 65 74 4c 6f 63 6b 46 6c 61 67 20 20 20 20 20  setLockFlag     
dad60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
dad70 75 65 20 74 6f 20 73 65 74 20 6c 6f 63 6b 2e 20  ue to set lock. 
dad80 20 46 61 6c 73 65 20 74 6f 20 63 6c 65 61 72 20   False to clear 
dad90 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a 20 20 73 74 72  lock */.){.  str
dada0 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63  uct ByteRangeLoc
dadb0 6b 50 42 32 20 70 62 3b 0a 20 20 69 6e 74 20 65  kPB2 pb;.  int e
dadc0 72 72 3b 0a 20 20 0a 20 20 70 62 2e 75 6e 4c 6f  rr;.  .  pb.unLo
dadd0 63 6b 46 6c 61 67 20 3d 20 73 65 74 4c 6f 63 6b  ckFlag = setLock
dade0 46 6c 61 67 20 3f 20 30 20 3a 20 31 3b 0a 20 20  Flag ? 0 : 1;.  
dadf0 70 62 2e 73 74 61 72 74 45 6e 64 46 6c 61 67 20  pb.startEndFlag 
dae00 3d 20 30 3b 0a 20 20 70 62 2e 6f 66 66 73 65 74  = 0;.  pb.offset
dae10 20 3d 20 6f 66 66 73 65 74 3b 0a 20 20 70 62 2e   = offset;.  pb.
dae20 6c 65 6e 67 74 68 20 3d 20 6c 65 6e 67 74 68 3b  length = length;
dae30 20 0a 20 20 70 62 2e 66 64 20 3d 20 70 46 69 6c   .  pb.fd = pFil
dae40 65 2d 3e 68 3b 0a 20 20 0a 20 20 4f 53 54 52 41  e->h;.  .  OSTRA
dae50 43 45 36 28 22 41 46 50 53 45 54 4c 4f 43 4b 20  CE6("AFPSETLOCK 
dae60 5b 25 73 5d 20 66 6f 72 20 25 64 25 73 20 69 6e  [%s] for %d%s in
dae70 20 72 61 6e 67 65 20 25 6c 6c 78 3a 25 6c 6c 78   range %llx:%llx
dae80 5c 6e 22 2c 20 0a 20 20 20 20 28 73 65 74 4c 6f  \n", .    (setLo
dae90 63 6b 46 6c 61 67 3f 22 4f 4e 22 3a 22 4f 46 46  ckFlag?"ON":"OFF
daea0 22 29 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 28 70  "), pFile->h, (p
daeb0 62 2e 66 64 3d 3d 2d 31 3f 22 5b 74 65 73 74 76  b.fd==-1?"[testv
daec0 61 6c 2d 31 5d 22 3a 22 22 29 2c 0a 20 20 20 20  al-1]":""),.    
daed0 6f 66 66 73 65 74 2c 20 6c 65 6e 67 74 68 29 3b  offset, length);
daee0 0a 20 20 65 72 72 20 3d 20 66 73 63 74 6c 28 70  .  err = fsctl(p
daef0 61 74 68 2c 20 61 66 70 66 73 42 79 74 65 52 61  ath, afpfsByteRa
daf00 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c 2c 20 26  ngeLock2FSCTL, &
daf10 70 62 2c 20 30 29 3b 0a 20 20 69 66 20 28 20 65  pb, 0);.  if ( e
daf20 72 72 3d 3d 2d 31 20 29 20 7b 0a 20 20 20 20 69  rr==-1 ) {.    i
daf30 6e 74 20 72 63 3b 0a 20 20 20 20 69 6e 74 20 74  nt rc;.    int t
daf40 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
daf50 20 20 20 4f 53 54 52 41 43 45 34 28 22 41 46 50     OSTRACE4("AFP
daf60 53 45 54 4c 4f 43 4b 20 66 61 69 6c 65 64 20 74  SETLOCK failed t
daf70 6f 20 66 73 63 74 6c 28 29 20 27 25 73 27 20 25  o fsctl() '%s' %
daf80 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20  d %s\n",.       
daf90 20 20 20 20 20 20 70 61 74 68 2c 20 74 45 72 72        path, tErr
dafa0 6e 6f 2c 20 73 74 72 65 72 72 6f 72 28 74 45 72  no, strerror(tEr
dafb0 72 6e 6f 29 29 3b 0a 23 69 66 64 65 66 20 53 51  rno));.#ifdef SQ
dafc0 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41 46 50 5f  LITE_IGNORE_AFP_
dafd0 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20 20 20  LOCK_ERRORS.    
dafe0 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
daff0 3b 0a 23 65 6c 73 65 0a 20 20 20 20 72 63 20 3d  ;.#else.    rc =
db000 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d   sqliteErrorFrom
db010 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e  PosixError(tErrn
db020 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  o,.             
db030 20 20 20 20 20 20 20 73 65 74 4c 6f 63 6b 46 6c         setLockFl
db040 61 67 20 3f 20 53 51 4c 49 54 45 5f 49 4f 45 52  ag ? SQLITE_IOER
db050 52 5f 4c 4f 43 4b 20 3a 20 53 51 4c 49 54 45 5f  R_LOCK : SQLITE_
db060 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 23  IOERR_UNLOCK);.#
db070 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
db080 49 47 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f  IGNORE_AFP_LOCK_
db090 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20 69 66  ERRORS */.    if
db0a0 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  ( IS_LOCK_ERROR(
db0b0 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69  rc) ){.      pFi
db0c0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
db0d0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20  tErrno;.    }.  
db0e0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
db0f0 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75   else {.    retu
db100 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
db110 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
db120 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69  routine checks i
db130 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53  f there is a RES
db140 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20  ERVED lock held 
db150 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64  on the specified
db160 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73  .** file by this
db170 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72   or any other pr
db180 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61  ocess. If such a
db190 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73   lock is held, s
db1a0 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74  et *pResOut.** t
db1b0 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c  o a non-zero val
db1c0 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52  ue otherwise *pR
db1d0 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20  esOut is set to 
db1e0 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72  zero.  The retur
db1f0 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65  n value.** is se
db200 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75  t to SQLITE_OK u
db210 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72  nless an I/O err
db220 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
db230 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a   lock checking..
db240 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66  */.static int af
db250 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  pCheckReservedLo
db260 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
db270 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75  *id, int *pResOu
db280 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  t){.  int rc = S
db290 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
db2a0 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20  reserved = 0;.  
db2b0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
db2c0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
db2d0 0a 20 20 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f  .  .  SimulateIO
db2e0 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
db2f0 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b  LITE_IOERR_CHECK
db300 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b  RESERVEDLOCK; );
db310 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46  .  .  assert( pF
db320 69 6c 65 20 29 3b 0a 20 20 61 66 70 4c 6f 63 6b  ile );.  afpLock
db330 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  ingContext *cont
db340 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69 6e  ext = (afpLockin
db350 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 6c  gContext *) pFil
db360 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
db370 74 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b  t;.  .  /* Check
db380 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20   if a thread in 
db390 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c  this process hol
db3a0 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a  ds such a lock *
db3b0 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  /.  if( pFile->l
db3c0 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c  ocktype>SHARED_L
db3d0 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65 72  OCK ){.    reser
db3e0 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a  ved = 1;.  }.  .
db3f0 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73    /* Otherwise s
db400 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72  ee if some other
db410 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69   process holds i
db420 74 2e 0a 20 20 20 2a 2f 0a 20 20 69 66 28 20 21  t..   */.  if( !
db430 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20  reserved ){.    
db440 2f 2a 20 6c 6f 63 6b 20 74 68 65 20 52 45 53 45  /* lock the RESE
db450 52 56 45 44 20 62 79 74 65 20 2a 2f 0a 20 20 20  RVED byte */.   
db460 20 69 6e 74 20 6c 72 63 20 3d 20 61 66 70 53 65   int lrc = afpSe
db470 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64  tLock(context->d
db480 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52 45  bPath, pFile, RE
db490 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c 31  SERVED_BYTE, 1,1
db4a0 29 3b 20 20 0a 20 20 20 20 69 66 28 20 53 51 4c  );  .    if( SQL
db4b0 49 54 45 5f 4f 4b 3d 3d 6c 72 63 20 29 7b 0a 20  ITE_OK==lrc ){. 
db4c0 20 20 20 20 20 2f 2a 20 69 66 20 77 65 20 73 75       /* if we su
db4d0 63 63 65 65 64 65 64 20 69 6e 20 74 61 6b 69 6e  cceeded in takin
db4e0 67 20 74 68 65 20 72 65 73 65 72 76 65 64 20 6c  g the reserved l
db4f0 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 74  ock, unlock it t
db500 6f 20 72 65 73 74 6f 72 65 0a 20 20 20 20 20 20  o restore.      
db510 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  ** the original 
db520 73 74 61 74 65 20 2a 2f 0a 20 20 20 20 20 20 6c  state */.      l
db530 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28  rc = afpSetLock(
db540 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c  context->dbPath,
db550 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45 44   pFile, RESERVED
db560 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a 20 20  _BYTE, 1, 0);.  
db570 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
db580 20 2f 2a 20 69 66 20 77 65 20 66 61 69 6c 65 64   /* if we failed
db590 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f 63 6b   to get the lock
db5a0 20 74 68 65 6e 20 73 6f 6d 65 6f 6e 65 20 65 6c   then someone el
db5b0 73 65 20 6d 75 73 74 20 68 61 76 65 20 69 74 20  se must have it 
db5c0 2a 2f 0a 20 20 20 20 20 20 72 65 73 65 72 76 65  */.      reserve
db5d0 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  d = 1;.    }.   
db5e0 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52   if( IS_LOCK_ERR
db5f0 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20 20  OR(lrc) ){.     
db600 20 72 63 3d 6c 72 63 3b 0a 20 20 20 20 7d 0a 20   rc=lrc;.    }. 
db610 20 7d 0a 20 20 0a 20 20 4f 53 54 52 41 43 45 34   }.  .  OSTRACE4
db620 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25  ("TEST WR-LOCK %
db630 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c  d %d %d\n", pFil
db640 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76  e->h, rc, reserv
db650 65 64 29 3b 0a 20 20 0a 20 20 2a 70 52 65 73 4f  ed);.  .  *pResO
db660 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20  ut = reserved;. 
db670 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
db680 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69  *.** Lock the fi
db690 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b  le with the lock
db6a0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61   specified by pa
db6b0 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65  rameter locktype
db6c0 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65   - one.** of the
db6d0 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
db6e0 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44  *     (1) SHARED
db6f0 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29  _LOCK.**     (2)
db700 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a   RESERVED_LOCK.*
db710 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e  *     (3) PENDIN
db720 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34  G_LOCK.**     (4
db730 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ) EXCLUSIVE_LOCK
db740 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73  .**.** Sometimes
db750 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67   when requesting
db760 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c   one lock state,
db770 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b   additional lock
db780 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69   states.** are i
db790 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65  nserted in betwe
db7a0 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67  en.  The locking
db7b0 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f   might fail on o
db7c0 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a  ne of the later.
db7d0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c  ** transitions l
db7e0 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20  eaving the lock 
db7f0 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20  state different 
db800 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61  from what it sta
db810 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c  rted but.** stil
db820 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67  l short of its g
db830 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  oal.  The follow
db840 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20  ing chart shows 
db850 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74  the allowed.** t
db860 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74  ransitions and t
db870 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65  he inserted inte
db880 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a  rmediate states:
db890 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b  .**.**    UNLOCK
db8a0 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20  ED -> SHARED.** 
db8b0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53     SHARED -> RES
db8c0 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52  ERVED.**    SHAR
db8d0 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20  ED -> (PENDING) 
db8e0 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20  -> EXCLUSIVE.** 
db8f0 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28     RESERVED -> (
db900 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c  PENDING) -> EXCL
db910 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44  USIVE.**    PEND
db920 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  ING -> EXCLUSIVE
db930 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
db940 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e  ine will only in
db950 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20  crease a lock.  
db960 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f  Use the sqlite3O
db970 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75  sUnlock().** rou
db980 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20  tine to lower a 
db990 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a  locking level..*
db9a0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 70  /.static int afp
db9b0 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
db9c0 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74  e *id, int lockt
db9d0 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ype){.  int rc =
db9e0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e   SQLITE_OK;.  un
db9f0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
dba00 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
dba10 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65   afpLockingConte
dba20 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 28 61  xt *context = (a
dba30 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  fpLockingContext
dba40 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69   *) pFile->locki
dba50 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 20  ngContext;.  .  
dba60 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
dba70 0a 20 20 4f 53 54 52 41 43 45 35 28 22 4c 4f 43  .  OSTRACE5("LOC
dba80 4b 20 20 20 20 25 64 20 25 73 20 77 61 73 20 25  K    %d %s was %
dba90 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69  s pid=%d\n", pFi
dbaa0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20  le->h,.         
dbab0 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63  locktypeName(loc
dbac0 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74 79 70 65  ktype), locktype
dbad0 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b  Name(pFile->lock
dbae0 74 79 70 65 29 2c 20 67 65 74 70 69 64 28 29 29  type), getpid())
dbaf0 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  ;..  /* If there
dbb00 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f   is already a lo
dbb10 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70 65 20  ck of this type 
dbb20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74  or more restrict
dbb30 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20  ive on the.  ** 
dbb40 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e 6f 74  unixFile, do not
dbb50 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20  hing. Don't use 
dbb60 74 68 65 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b  the afp_end_lock
dbb70 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a  : exit path, as.
dbb80 20 20 2a 2a 20 75 6e 69 78 45 6e 74 65 72 4d 75    ** unixEnterMu
dbb90 74 65 78 28 29 20 68 61 73 6e 27 74 20 62 65 65  tex() hasn't bee
dbba0 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20  n called yet..  
dbbb0 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  */.  if( pFile->
dbbc0 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79  locktype>=lockty
dbbd0 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43  pe ){.    OSTRAC
dbbe0 45 33 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25  E3("LOCK    %d %
dbbf0 73 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65  s ok (already he
dbc00 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  ld)\n", pFile->h
dbc10 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63  ,.           loc
dbc20 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79  ktypeName(lockty
dbc30 70 65 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  pe));.    return
dbc40 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
dbc50 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  .  /* Make sure 
dbc60 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75  the locking sequ
dbc70 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74 0a  ence is correct.
dbc80 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
dbc90 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d  File->locktype!=
dbca0 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74  NO_LOCK || lockt
dbcb0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
dbcc0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f   );.  assert( lo
dbcd0 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f  cktype!=PENDING_
dbce0 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  LOCK );.  assert
dbcf0 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 45  ( locktype!=RESE
dbd00 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69  RVED_LOCK || pFi
dbd10 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  le->locktype==SH
dbd20 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a  ARED_LOCK );.  .
dbd30 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65 78 20    /* This mutex 
dbd40 69 73 20 6e 65 65 64 65 64 20 62 65 63 61 75 73  is needed becaus
dbd50 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69  e pFile->pLock i
dbd60 73 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 20  s shared across 
dbd70 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20 20 75  threads.  */.  u
dbd80 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  nixEnterMutex();
dbd90 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
dbda0 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72   the current thr
dbdb0 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70 46 69  ead owns the pFi
dbdc0 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  le..  */.  rc = 
dbdd0 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69  transferOwnershi
dbde0 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20  p(pFile);.  if( 
dbdf0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
dbe00 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75  .    unixLeaveMu
dbe10 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75 72  tex();.    retur
dbe20 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 20 20 0a 20  n rc;.  }.    . 
dbe30 20 2f 2a 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f   /* A PENDING lo
dbe40 63 6b 20 69 73 20 6e 65 65 64 65 64 20 62 65 66  ck is needed bef
dbe50 6f 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 20  ore acquiring a 
dbe60 53 48 41 52 45 44 20 6c 6f 63 6b 20 61 6e 64 20  SHARED lock and 
dbe70 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61 63 71 75  before.  ** acqu
dbe80 69 72 69 6e 67 20 61 6e 20 45 58 43 4c 55 53 49  iring an EXCLUSI
dbe90 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 74 68  VE lock.  For th
dbea0 65 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74  e SHARED lock, t
dbeb0 68 65 20 50 45 4e 44 49 4e 47 20 77 69 6c 6c 0a  he PENDING will.
dbec0 20 20 2a 2a 20 62 65 20 72 65 6c 65 61 73 65 64    ** be released
dbed0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63  ..  */.  if( loc
dbee0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
dbef0 43 4b 20 0a 20 20 20 20 20 20 7c 7c 20 28 6c 6f  CK .      || (lo
dbf00 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56  cktype==EXCLUSIV
dbf10 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d  E_LOCK && pFile-
dbf20 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 4e 44 49 4e  >locktype<PENDIN
dbf30 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 20  G_LOCK).  ){.   
dbf40 20 69 6e 74 20 66 61 69 6c 65 64 3b 0a 20 20 20   int failed;.   
dbf50 20 66 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74   failed = afpSet
dbf60 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62  Lock(context->db
dbf70 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e  Path, pFile, PEN
dbf80 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 31 29  DING_BYTE, 1, 1)
dbf90 3b 0a 20 20 20 20 69 66 20 28 66 61 69 6c 65 64  ;.    if (failed
dbfa0 29 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66  ) {.      rc = f
dbfb0 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 67 6f 74  ailed;.      got
dbfc0 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a  o afp_end_lock;.
dbfd0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 2f      }.  }.  .  /
dbfe0 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 67 65 74  * If control get
dbff0 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c  s to this point,
dc000 20 74 68 65 6e 20 61 63 74 75 61 6c 6c 79 20 67   then actually g
dc010 6f 20 61 68 65 61 64 20 61 6e 64 20 6d 61 6b 65  o ahead and make
dc020 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20  .  ** operating 
dc030 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 66 6f 72  system calls for
dc040 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 6c   the specified l
dc050 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ock..  */.  if( 
dc060 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
dc070 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 69 6e 74  _LOCK ){.    int
dc080 20 6c 6b 2c 20 6c 72 63 31 2c 20 6c 72 63 32 2c   lk, lrc1, lrc2,
dc090 20 6c 72 63 31 45 72 72 6e 6f 3b 0a 20 20 20 20   lrc1Errno;.    
dc0a0 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20  .    /* Now get 
dc0b0 74 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 53 48  the read-lock SH
dc0c0 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20  ARED_LOCK */.   
dc0d0 20 2f 2a 20 6e 6f 74 65 20 74 68 61 74 20 74 68   /* note that th
dc0e0 65 20 71 75 61 6c 69 74 79 20 6f 66 20 74 68 65  e quality of the
dc0f0 20 72 61 6e 64 6f 6d 6e 65 73 73 20 64 6f 65 73   randomness does
dc100 6e 27 74 20 6d 61 74 74 65 72 20 74 68 61 74 20  n't matter that 
dc110 6d 75 63 68 20 2a 2f 0a 20 20 20 20 6c 6b 20 3d  much */.    lk =
dc120 20 72 61 6e 64 6f 6d 28 29 3b 20 0a 20 20 20 20   random(); .    
dc130 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 42  context->sharedB
dc140 79 74 65 20 3d 20 28 6c 6b 20 26 20 30 78 37 66  yte = (lk & 0x7f
dc150 66 66 66 66 66 66 29 25 28 53 48 41 52 45 44 5f  ffffff)%(SHARED_
dc160 53 49 5a 45 20 2d 20 31 29 3b 0a 20 20 20 20 6c  SIZE - 1);.    l
dc170 72 63 31 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b  rc1 = afpSetLock
dc180 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68  (context->dbPath
dc190 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20 20  , pFile, .      
dc1a0 20 20 20 20 53 48 41 52 45 44 5f 46 49 52 53 54      SHARED_FIRST
dc1b0 2b 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64  +context->shared
dc1c0 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20 20  Byte, 1, 1);.   
dc1d0 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52   if( IS_LOCK_ERR
dc1e0 4f 52 28 6c 72 63 31 29 20 29 7b 0a 20 20 20 20  OR(lrc1) ){.    
dc1f0 20 20 6c 72 63 31 45 72 72 6e 6f 20 3d 20 70 46    lrc1Errno = pF
dc200 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a  ile->lastErrno;.
dc210 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72 6f      }.    /* Dro
dc220 70 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  p the temporary 
dc230 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a  PENDING lock */.
dc240 20 20 20 20 6c 72 63 32 20 3d 20 61 66 70 53 65      lrc2 = afpSe
dc250 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64  tLock(context->d
dc260 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45  bPath, pFile, PE
dc270 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30  NDING_BYTE, 1, 0
dc280 29 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20  );.    .    if( 
dc290 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72  IS_LOCK_ERROR(lr
dc2a0 63 31 29 20 29 20 7b 0a 20 20 20 20 20 20 70 46  c1) ) {.      pF
dc2b0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
dc2c0 20 6c 72 63 31 45 72 72 6e 6f 3b 0a 20 20 20 20   lrc1Errno;.    
dc2d0 20 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20    rc = lrc1;.   
dc2e0 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f     goto afp_end_
dc2f0 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65  lock;.    } else
dc300 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52   if( IS_LOCK_ERR
dc310 4f 52 28 6c 72 63 32 29 20 29 7b 0a 20 20 20 20  OR(lrc2) ){.    
dc320 20 20 72 63 20 3d 20 6c 72 63 32 3b 0a 20 20 20    rc = lrc2;.   
dc330 20 20 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f     goto afp_end_
dc340 6c 6f 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65  lock;.    } else
dc350 20 69 66 28 20 6c 72 63 31 20 21 3d 20 53 51 4c   if( lrc1 != SQL
dc360 49 54 45 5f 4f 4b 20 29 20 7b 0a 20 20 20 20 20  ITE_OK ) {.     
dc370 20 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20   rc = lrc1;.    
dc380 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 70  } else {.      p
dc390 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
dc3a0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20   SHARED_LOCK;.  
dc3b0 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e      pFile->pOpen
dc3c0 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 7d  ->nLock++;.    }
dc3d0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
dc3e0 20 54 68 65 20 72 65 71 75 65 73 74 20 77 61 73   The request was
dc3f0 20 66 6f 72 20 61 20 52 45 53 45 52 56 45 44 20   for a RESERVED 
dc400 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  or EXCLUSIVE loc
dc410 6b 2e 20 20 49 74 20 69 73 0a 20 20 20 20 2a 2a  k.  It is.    **
dc420 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68   assumed that th
dc430 65 72 65 20 69 73 20 61 20 53 48 41 52 45 44 20  ere is a SHARED 
dc440 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20  or greater lock 
dc450 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20  on the file.    
dc460 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20  ** already..    
dc470 2a 2f 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65  */.    int faile
dc480 64 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72  d = 0;.    asser
dc490 74 28 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63  t( 0!=pFile->loc
dc4a0 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 69 66 20  ktype );.    if 
dc4b0 28 6c 6f 63 6b 74 79 70 65 20 3e 3d 20 52 45 53  (locktype >= RES
dc4c0 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 70 46  ERVED_LOCK && pF
dc4d0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20  ile->locktype < 
dc4e0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20 7b  RESERVED_LOCK) {
dc4f0 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 63 71 75  .        /* Acqu
dc500 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20 6c  ire a RESERVED l
dc510 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 66  ock */.        f
dc520 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f  ailed = afpSetLo
dc530 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61  ck(context->dbPa
dc540 74 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52  th, pFile, RESER
dc550 56 45 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 0a  VED_BYTE, 1,1);.
dc560 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28 21 66      }.    if (!f
dc570 61 69 6c 65 64 20 26 26 20 6c 6f 63 6b 74 79 70  ailed && locktyp
dc580 65 20 3d 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c  e == EXCLUSIVE_L
dc590 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20 2f 2a 20  OCK) {.      /* 
dc5a0 41 63 71 75 69 72 65 20 61 6e 20 45 58 43 4c 55  Acquire an EXCLU
dc5b0 53 49 56 45 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20  SIVE lock */.   
dc5c0 20 20 20 20 20 0a 20 20 20 20 20 20 2f 2a 20 52       .      /* R
dc5d0 65 6d 6f 76 65 20 74 68 65 20 73 68 61 72 65 64  emove the shared
dc5e0 20 6c 6f 63 6b 20 62 65 66 6f 72 65 20 74 72 79   lock before try
dc5f0 69 6e 67 20 74 68 65 20 72 61 6e 67 65 2e 20 20  ing the range.  
dc600 77 65 27 6c 6c 20 6e 65 65 64 20 74 6f 20 0a 20  we'll need to . 
dc610 20 20 20 20 20 2a 2a 20 72 65 65 73 74 61 62 6c       ** reestabl
dc620 69 73 68 20 74 68 65 20 73 68 61 72 65 64 20 6c  ish the shared l
dc630 6f 63 6b 20 69 66 20 77 65 20 63 61 6e 27 74 20  ock if we can't 
dc640 67 65 74 20 74 68 65 20 20 61 66 70 55 6e 6c 6f  get the  afpUnlo
dc650 63 6b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  ck.      */.    
dc660 20 20 69 66 28 20 21 28 66 61 69 6c 65 64 20 3d    if( !(failed =
dc670 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74   afpSetLock(cont
dc680 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69  ext->dbPath, pFi
dc690 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54  le, SHARED_FIRST
dc6a0 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   +.             
dc6b0 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74              cont
dc6c0 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74 65 2c  ext->sharedByte,
dc6d0 20 31 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 20   1, 0)) ){.     
dc6e0 20 20 20 69 6e 74 20 66 61 69 6c 65 64 32 20 3d     int failed2 =
dc6f0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
dc700 20 20 20 20 2f 2a 20 6e 6f 77 20 61 74 74 65 6d      /* now attem
dc710 6d 70 74 20 74 6f 20 67 65 74 20 74 68 65 20 65  mpt to get the e
dc720 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 72 61  xclusive lock ra
dc730 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 66  nge */.        f
dc740 61 69 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f  ailed = afpSetLo
dc750 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61  ck(context->dbPa
dc760 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 45  th, pFile, SHARE
dc770 44 5f 46 49 52 53 54 2c 20 0a 20 20 20 20 20 20  D_FIRST, .      
dc780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dc790 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 5f           SHARED_
dc7a0 53 49 5a 45 2c 20 31 29 3b 0a 20 20 20 20 20 20  SIZE, 1);.      
dc7b0 20 20 69 66 28 20 66 61 69 6c 65 64 20 26 26 20    if( failed && 
dc7c0 28 66 61 69 6c 65 64 32 20 3d 20 61 66 70 53 65  (failed2 = afpSe
dc7d0 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64  tLock(context->d
dc7e0 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a 20  bPath, pFile, . 
dc7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dc800 20 20 20 20 20 20 53 48 41 52 45 44 5f 46 49 52        SHARED_FIR
dc810 53 54 20 2b 20 63 6f 6e 74 65 78 74 2d 3e 73 68  ST + context->sh
dc820 61 72 65 64 42 79 74 65 2c 20 31 2c 20 31 29 29  aredByte, 1, 1))
dc830 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
dc840 20 43 61 6e 27 74 20 72 65 65 73 74 61 62 6c 69   Can't reestabli
dc850 73 68 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f  sh the shared lo
dc860 63 6b 2e 20 20 53 71 6c 69 74 65 20 63 61 6e 27  ck.  Sqlite can'
dc870 74 20 64 65 61 6c 2c 20 74 68 69 73 20 69 73 0a  t deal, this is.
dc880 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 20 63            ** a c
dc890 72 69 74 69 63 61 6c 20 49 2f 4f 20 65 72 72 6f  ritical I/O erro
dc8a0 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  r.          */. 
dc8b0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 28 28           rc = ((
dc8c0 66 61 69 6c 65 64 20 26 20 53 51 4c 49 54 45 5f  failed & SQLITE_
dc8d0 49 4f 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45  IOERR) == SQLITE
dc8e0 5f 49 4f 45 52 52 29 20 3f 20 66 61 69 6c 65 64  _IOERR) ? failed
dc8f0 32 20 3a 20 0a 20 20 20 20 20 20 20 20 20 20 20  2 : .           
dc900 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 52 52      SQLITE_IOERR
dc910 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 20  _LOCK;.         
dc920 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f   goto afp_end_lo
dc930 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 20 0a 20  ck;.        } . 
dc940 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
dc950 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b      rc = failed;
dc960 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a   .      }.    }.
dc970 20 20 20 20 69 66 28 20 66 61 69 6c 65 64 20 29      if( failed )
dc980 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 61 69  {.      rc = fai
dc990 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  led;.    }.  }. 
dc9a0 20 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49   .  if( rc==SQLI
dc9b0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69  TE_OK ){.    pFi
dc9c0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c  le->locktype = l
dc9d0 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73 65  ocktype;.  }else
dc9e0 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45   if( locktype==E
dc9f0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b  XCLUSIVE_LOCK ){
dca00 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  .    pFile->lock
dca10 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c  type = PENDING_L
dca20 4f 43 4b 3b 0a 20 20 7d 0a 20 20 0a 61 66 70 5f  OCK;.  }.  .afp_
dca30 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78  end_lock:.  unix
dca40 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
dca50 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20  OSTRACE4("LOCK  
dca60 20 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70    %d %s %s\n", p
dca70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70  File->h, locktyp
dca80 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c  eName(locktype),
dca90 20 0a 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53   .         rc==S
dcaa0 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20  QLITE_OK ? "ok" 
dcab0 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 72  : "failed");.  r
dcac0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
dcad0 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63  ** Lower the loc
dcae0 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69  king level on fi
dcaf0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 46  le descriptor pF
dcb00 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e  ile to locktype.
dcb10 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75    locktype.** mu
dcb20 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f  st be either NO_
dcb30 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c  LOCK or SHARED_L
dcb40 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  OCK..**.** If th
dcb50 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
dcb60 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  of the file desc
dcb70 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64  riptor is alread
dcb80 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a  y at or below.**
dcb90 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c   the requested l
dcba0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68  ocking level, th
dcbb0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
dcbc0 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63  no-op..*/.static
dcbd0 20 69 6e 74 20 61 66 70 55 6e 6c 6f 63 6b 28 73   int afpUnlock(s
dcbe0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
dcbf0 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b   int locktype) {
dcc00 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
dcc10 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c  TE_OK;.  unixFil
dcc20 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
dcc30 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 4c  File*)id;.  afpL
dcc40 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70  ockingContext *p
dcc50 43 74 78 20 3d 20 28 61 66 70 4c 6f 63 6b 69 6e  Ctx = (afpLockin
dcc60 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 6c  gContext *) pFil
dcc70 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
dcc80 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46  t;..  assert( pF
dcc90 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  ile );.  OSTRACE
dcca0 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64  5("UNLOCK  %d %d
dccb0 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e   was %d pid=%d\n
dccc0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63  ", pFile->h, loc
dccd0 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20  ktype,.         
dcce0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c  pFile->locktype,
dccf0 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 61   getpid());..  a
dcd00 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c  ssert( locktype<
dcd10 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a  =SHARED_LOCK );.
dcd20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63    if( pFile->loc
dcd30 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65 20  ktype<=locktype 
dcd40 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
dcd50 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69  LITE_OK;.  }.  i
dcd60 66 28 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49  f( CHECK_THREADI
dcd70 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 20  D(pFile) ){.    
dcd80 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
dcd90 53 55 53 45 3b 0a 20 20 7d 0a 20 20 75 6e 69 78  SUSE;.  }.  unix
dcda0 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20  EnterMutex();.  
dcdb0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
dcdc0 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ype>SHARED_LOCK 
dcdd0 29 7b 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20  ){.    .    if( 
dcde0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
dcdf0 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20  =EXCLUSIVE_LOCK 
dce00 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61 66  ){.      rc = af
dce10 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64  pSetLock(pCtx->d
dce20 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48  bPath, pFile, SH
dce30 41 52 45 44 5f 46 49 52 53 54 2c 20 53 48 41 52  ARED_FIRST, SHAR
dce40 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 20  ED_SIZE, 0);.   
dce50 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
dce60 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b 74 79 70 65  E_OK && locktype
dce70 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b  ==SHARED_LOCK ){
dce80 0a 20 20 20 20 20 20 20 20 2f 2a 20 6f 6e 6c 79  .        /* only
dce90 20 72 65 2d 65 73 74 61 62 6c 69 73 68 20 74 68   re-establish th
dcea0 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69 66  e shared lock if
dceb0 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a 20 20   necessary */.  
dcec0 20 20 20 20 20 20 69 6e 74 20 73 68 61 72 65 64        int shared
dced0 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52 45  LockByte = SHARE
dcee0 44 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 68  D_FIRST+pCtx->sh
dcef0 61 72 65 64 42 79 74 65 3b 0a 20 20 20 20 20 20  aredByte;.      
dcf00 20 20 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63    rc = afpSetLoc
dcf10 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20  k(pCtx->dbPath, 
dcf20 70 46 69 6c 65 2c 20 73 68 61 72 65 64 4c 6f 63  pFile, sharedLoc
dcf30 6b 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20  kByte, 1, 1);.  
dcf40 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
dcf50 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
dcf60 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  K && pFile->lock
dcf70 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f  type>=PENDING_LO
dcf80 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  CK ){.      rc =
dcf90 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78   afpSetLock(pCtx
dcfa0 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c  ->dbPath, pFile,
dcfb0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31   PENDING_BYTE, 1
dcfc0 2c 20 30 29 3b 0a 20 20 20 20 7d 20 0a 20 20 20  , 0);.    } .   
dcfd0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
dcfe0 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63  OK && pFile->loc
dcff0 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f  ktype>=RESERVED_
dd000 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  LOCK ){.      rc
dd010 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43   = afpSetLock(pC
dd020 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c  tx->dbPath, pFil
dd030 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45  e, RESERVED_BYTE
dd040 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  , 1, 0);.    }. 
dd050 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74   }else if( lockt
dd060 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a  ype==NO_LOCK ){.
dd070 20 20 20 20 2f 2a 20 63 6c 65 61 72 20 74 68 65      /* clear the
dd080 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a   shared lock */.
dd090 20 20 20 20 69 6e 74 20 73 68 61 72 65 64 4c 6f      int sharedLo
dd0a0 63 6b 42 79 74 65 20 3d 20 53 48 41 52 45 44 5f  ckByte = SHARED_
dd0b0 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 68 61 72  FIRST+pCtx->shar
dd0c0 65 64 42 79 74 65 3b 0a 20 20 20 20 72 63 20 3d  edByte;.    rc =
dd0d0 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78   afpSetLock(pCtx
dd0e0 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c  ->dbPath, pFile,
dd0f0 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c   sharedLockByte,
dd100 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 69   1, 0);.  }..  i
dd110 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
dd120 20 29 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b   ){.    if( lock
dd130 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b  type==NO_LOCK ){
dd140 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 75 6e  .      struct un
dd150 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e  ixOpenCnt *pOpen
dd160 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b   = pFile->pOpen;
dd170 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c  .      pOpen->nL
dd180 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 61 73 73  ock--;.      ass
dd190 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63  ert( pOpen->nLoc
dd1a0 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66  k>=0 );.      if
dd1b0 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d  ( pOpen->nLock==
dd1c0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  0 ){.        rc 
dd1d0 3d 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67 46 64  = closePendingFd
dd1e0 73 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 20  s(pFile);.      
dd1f0 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 75 6e  }.    }.  }.  un
dd200 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  ixLeaveMutex();.
dd210 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
dd220 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65  _OK ){.    pFile
dd230 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63  ->locktype = loc
dd240 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74  ktype;.  }.  ret
dd250 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
dd260 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20 26 20   Close a file & 
dd270 63 6c 65 61 6e 75 70 20 41 46 50 20 73 70 65 63  cleanup AFP spec
dd280 69 66 69 63 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e  ific locking con
dd290 74 65 78 74 20 0a 2a 2f 0a 73 74 61 74 69 63 20  text .*/.static 
dd2a0 69 6e 74 20 61 66 70 43 6c 6f 73 65 28 73 71 6c  int afpClose(sql
dd2b0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b  ite3_file *id) {
dd2c0 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20  .  if( id ){.   
dd2d0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
dd2e0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
dd2f0 3b 0a 20 20 20 20 61 66 70 55 6e 6c 6f 63 6b 28  ;.    afpUnlock(
dd300 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20  id, NO_LOCK);.  
dd310 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78    unixEnterMutex
dd320 28 29 3b 0a 20 20 20 20 69 66 28 20 70 46 69 6c  ();.    if( pFil
dd330 65 2d 3e 70 4f 70 65 6e 20 26 26 20 70 46 69 6c  e->pOpen && pFil
dd340 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20  e->pOpen->nLock 
dd350 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  ){.      /* If t
dd360 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e  here are outstan
dd370 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20 6e  ding locks, do n
dd380 6f 74 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f 73  ot actually clos
dd390 65 20 74 68 65 20 66 69 6c 65 20 6a 75 73 74 0a  e the file just.
dd3a0 20 20 20 20 20 20 2a 2a 20 79 65 74 20 62 65 63        ** yet bec
dd3b0 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64 20  ause that would 
dd3c0 63 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f 63 6b  clear those lock
dd3d0 73 2e 20 20 49 6e 73 74 65 61 64 2c 20 61 64 64  s.  Instead, add
dd3e0 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 20 20   the file.      
dd3f0 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f  ** descriptor to
dd400 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67   pOpen->aPending
dd410 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 20 61 75  .  It will be au
dd420 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73  tomatically clos
dd430 65 64 20 77 68 65 6e 0a 20 20 20 20 20 20 2a 2a  ed when.      **
dd440 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20 69   the last lock i
dd450 73 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 20  s cleared..     
dd460 20 2a 2f 0a 20 20 20 20 20 20 73 65 74 50 65 6e   */.      setPen
dd470 64 69 6e 67 46 64 28 70 46 69 6c 65 29 3b 0a 20  dingFd(pFile);. 
dd480 20 20 20 7d 0a 20 20 20 20 72 65 6c 65 61 73 65     }.    release
dd490 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70  OpenCnt(pFile->p
dd4a0 4f 70 65 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74  Open);.    sqlit
dd4b0 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e 6c  e3_free(pFile->l
dd4c0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b 0a  ockingContext);.
dd4d0 20 20 20 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c      closeUnixFil
dd4e0 65 28 69 64 29 3b 0a 20 20 20 20 75 6e 69 78 4c  e(id);.    unixL
dd4f0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d  eaveMutex();.  }
dd500 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
dd510 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  _OK;.}..#endif /
dd520 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c  * defined(__APPL
dd530 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45  E__) && SQLITE_E
dd540 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
dd550 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65  YLE */./*.** The
dd560 20 63 6f 64 65 20 61 62 6f 76 65 20 69 73 20 74   code above is t
dd570 68 65 20 41 46 50 20 6c 6f 63 6b 20 69 6d 70 6c  he AFP lock impl
dd580 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65  ementation.  The
dd590 20 63 6f 64 65 20 69 73 20 73 70 65 63 69 66 69   code is specifi
dd5a0 63 0a 2a 2a 20 74 6f 20 4d 61 63 4f 53 58 20 61  c.** to MacOSX a
dd5b0 6e 64 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b  nd does not work
dd5c0 20 6f 6e 20 6f 74 68 65 72 20 75 6e 69 78 20 70   on other unix p
dd5d0 6c 61 74 66 6f 72 6d 73 2e 20 20 4e 6f 20 61 6c  latforms.  No al
dd5e0 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 69 73 20  ternative.** is 
dd5f0 61 76 61 69 6c 61 62 6c 65 2e 20 20 49 66 20 79  available.  If y
dd600 6f 75 20 64 6f 6e 27 74 20 63 6f 6d 70 69 6c 65  ou don't compile
dd610 20 66 6f 72 20 61 20 6d 61 63 2c 20 74 68 65 6e   for a mac, then
dd620 20 74 68 65 20 22 75 6e 69 78 2d 61 66 70 22 0a   the "unix-afp".
dd630 2a 2a 20 56 46 53 20 69 73 20 6e 6f 74 20 61 76  ** VFS is not av
dd640 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ailable..**.****
dd650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd660 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 41 46 50  * End of the AFP
dd670 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61   lock implementa
dd680 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tion ***********
dd690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
dd6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a  **********/.../*
dd6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
dd740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e  ************** N
dd750 6f 6e 2d 6c 6f 63 6b 69 6e 67 20 73 71 6c 69 74  on-locking sqlit
dd760 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20  e3_file methods 
dd770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
dd790 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 64 69 76  .** The next div
dd7a0 69 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 69  ision contains i
dd7b0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66  mplementations f
dd7c0 6f 72 20 61 6c 6c 20 6d 65 74 68 6f 64 73 20 6f  or all methods o
dd7d0 66 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65  f the .** sqlite
dd7e0 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 20 6f 74  3_file object ot
dd7f0 68 65 72 20 74 68 61 6e 20 74 68 65 20 6c 6f 63  her than the loc
dd800 6b 69 6e 67 20 6d 65 74 68 6f 64 73 2e 20 20 54  king methods.  T
dd810 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 6d 65  he locking.** me
dd820 74 68 6f 64 73 20 77 65 72 65 20 64 65 66 69 6e  thods were defin
dd830 65 64 20 69 6e 20 64 69 76 69 73 69 6f 6e 73 20  ed in divisions 
dd840 61 62 6f 76 65 20 28 6f 6e 65 20 6c 6f 63 6b 69  above (one locki
dd850 6e 67 20 6d 65 74 68 6f 64 20 70 65 72 0a 2a 2a  ng method per.**
dd860 20 64 69 76 69 73 69 6f 6e 29 2e 20 20 54 68 6f   division).  Tho
dd870 73 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20  se methods that 
dd880 61 72 65 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c  are common to al
dd890 6c 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 73 0a  l locking modes.
dd8a0 2a 2a 20 61 72 65 20 67 61 74 68 65 72 20 74 6f  ** are gather to
dd8b0 67 65 74 68 65 72 20 69 6e 74 6f 20 74 68 69 73  gether into this
dd8c0 20 64 69 76 69 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f   division..*/../
dd8d0 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f 20 74 68 65  *.** Seek to the
dd8e0 20 6f 66 66 73 65 74 20 70 61 73 73 65 64 20 61   offset passed a
dd8f0 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
dd900 75 6d 65 6e 74 2c 20 74 68 65 6e 20 72 65 61 64  ument, then read
dd910 20 63 6e 74 20 0a 2a 2a 20 62 79 74 65 73 20 69   cnt .** bytes i
dd920 6e 74 6f 20 70 42 75 66 2e 20 52 65 74 75 72 6e  nto pBuf. Return
dd930 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
dd940 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 72 65  ytes actually re
dd950 61 64 2e 0a 2a 2a 0a 2a 2a 20 4e 42 3a 20 20 49  ad..**.** NB:  I
dd960 66 20 79 6f 75 20 64 65 66 69 6e 65 20 55 53 45  f you define USE
dd970 5f 50 52 45 41 44 20 6f 72 20 55 53 45 5f 50 52  _PREAD or USE_PR
dd980 45 41 44 36 34 2c 20 74 68 65 6e 20 69 74 20 6d  EAD64, then it m
dd990 69 67 68 74 20 61 6c 73 6f 0a 2a 2a 20 62 65 20  ight also.** be 
dd9a0 6e 65 63 65 73 73 61 72 79 20 74 6f 20 64 65 66  necessary to def
dd9b0 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43  ine _XOPEN_SOURC
dd9c0 45 20 74 6f 20 62 65 20 35 30 30 2e 20 20 54 68  E to be 500.  Th
dd9d0 69 73 20 76 61 72 69 65 73 20 66 72 6f 6d 0a 2a  is varies from.*
dd9e0 2a 20 6f 6e 65 20 73 79 73 74 65 6d 20 74 6f 20  * one system to 
dd9f0 61 6e 6f 74 68 65 72 2e 20 20 53 69 6e 63 65 20  another.  Since 
dda00 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20  SQLite does not 
dda10 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45 41 44  define USE_PREAD
dda20 0a 2a 2a 20 61 6e 79 20 61 6e 79 20 66 6f 72 6d  .** any any form
dda30 20 62 79 20 64 65 66 61 75 6c 74 2c 20 77 65 20   by default, we 
dda40 77 69 6c 6c 20 6e 6f 74 20 61 74 74 65 6d 70 74  will not attempt
dda50 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50 45   to define _XOPE
dda60 4e 5f 53 4f 55 52 43 45 2e 0a 2a 2a 20 53 65 65  N_SOURCE..** See
dda70 20 74 69 63 6b 65 74 73 20 23 32 37 34 31 20 61   tickets #2741 a
dda80 6e 64 20 23 32 36 38 31 2e 0a 2a 2a 0a 2a 2a 20  nd #2681..**.** 
dda90 54 6f 20 61 76 6f 69 64 20 73 74 6f 6d 70 69 6e  To avoid stompin
ddaa0 67 20 74 68 65 20 65 72 72 6e 6f 20 76 61 6c 75  g the errno valu
ddab0 65 20 6f 6e 20 61 20 66 61 69 6c 65 64 20 72 65  e on a failed re
ddac0 61 64 20 74 68 65 20 6c 61 73 74 45 72 72 6e 6f  ad the lastErrno
ddad0 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74   value.** is set
ddae0 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
ddaf0 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
ddb00 20 73 65 65 6b 41 6e 64 52 65 61 64 28 75 6e 69   seekAndRead(uni
ddb10 78 46 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74  xFile *id, sqlit
ddb20 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 2c  e3_int64 offset,
ddb30 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74   void *pBuf, int
ddb40 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f 74   cnt){.  int got
ddb50 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66 73 65  ;.  i64 newOffse
ddb60 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41 52 54  t;.  TIMER_START
ddb70 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 55 53  ;.#if defined(US
ddb80 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74 20 3d  E_PREAD).  got =
ddb90 20 70 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 42   pread(id->h, pB
ddba0 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29  uf, cnt, offset)
ddbb0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
ddbc0 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29 3b  ror( got = -1 );
ddbd0 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 55  .#elif defined(U
ddbe0 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 67 6f  SE_PREAD64).  go
ddbf0 74 20 3d 20 70 72 65 61 64 36 34 28 69 64 2d 3e  t = pread64(id->
ddc00 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66  h, pBuf, cnt, of
ddc10 66 73 65 74 29 3b 0a 20 20 53 69 6d 75 6c 61 74  fset);.  Simulat
ddc20 65 49 4f 45 72 72 6f 72 28 20 67 6f 74 20 3d 20  eIOError( got = 
ddc30 2d 31 20 29 3b 0a 23 65 6c 73 65 0a 20 20 6e 65  -1 );.#else.  ne
ddc40 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 65 6b 28  wOffset = lseek(
ddc50 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 53  id->h, offset, S
ddc60 45 45 4b 5f 53 45 54 29 3b 0a 20 20 53 69 6d 75  EEK_SET);.  Simu
ddc70 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 6e 65 77  lateIOError( new
ddc80 4f 66 66 73 65 74 2d 2d 20 29 3b 0a 20 20 69 66  Offset-- );.  if
ddc90 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66 66  ( newOffset!=off
ddca0 73 65 74 20 29 7b 0a 20 20 20 20 69 66 28 20 6e  set ){.    if( n
ddcb0 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20 29  ewOffset == -1 )
ddcc0 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69  {.      ((unixFi
ddcd0 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72  le*)id)->lastErr
ddce0 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
ddcf0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 28 75  }else{.      ((u
ddd00 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61  nixFile*)id)->la
ddd10 73 74 45 72 72 6e 6f 20 3d 20 30 3b 09 09 09 0a  stErrno = 0;....
ddd20 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
ddd30 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 20 3d   -1;.  }.  got =
ddd40 20 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 42 75   read(id->h, pBu
ddd50 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66 0a  f, cnt);.#endif.
ddd60 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20 69    TIMER_END;.  i
ddd70 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20 20  f( got<0 ){.    
ddd80 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d  ((unixFile*)id)-
ddd90 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
ddda0 6e 6f 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43  no;.  }.  OSTRAC
dddb0 45 35 28 22 52 45 41 44 20 20 20 20 25 2d 33 64  E5("READ    %-3d
dddc0 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c 75 5c   %5d %7lld %llu\
dddd0 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74 2c 20  n", id->h, got, 
ddde0 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f 45 4c  offset, TIMER_EL
dddf0 41 50 53 45 44 29 3b 0a 20 20 72 65 74 75 72 6e  APSED);.  return
dde00 20 67 6f 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   got;.}../*.** R
dde10 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 20  ead data from a 
dde20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 66  file into a buff
dde30 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49  er.  Return SQLI
dde40 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20  TE_OK if all.** 
dde50 62 79 74 65 73 20 77 65 72 65 20 72 65 61 64 20  bytes were read 
dde60 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e 64  successfully and
dde70 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 66   SQLITE_IOERR if
dde80 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a   anything goes.*
dde90 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74  * wrong..*/.stat
ddea0 69 63 20 69 6e 74 20 75 6e 69 78 52 65 61 64 28  ic int unixRead(
ddeb0 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
ddec0 2a 69 64 2c 20 0a 20 20 76 6f 69 64 20 2a 70 42  *id, .  void *pB
dded0 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c 0a  uf, .  int amt,.
ddee0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
ddef0 6f 66 66 73 65 74 0a 29 7b 0a 20 20 75 6e 69 78  offset.){.  unix
ddf00 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
ddf10 6e 69 78 46 69 6c 65 20 2a 29 69 64 3b 0a 20 20  nixFile *)id;.  
ddf20 69 6e 74 20 67 6f 74 3b 0a 20 20 61 73 73 65 72  int got;.  asser
ddf30 74 28 20 69 64 20 29 3b 0a 0a 20 20 2f 2a 20 49  t( id );..  /* I
ddf40 66 20 74 68 69 73 20 69 73 20 61 20 64 61 74 61  f this is a data
ddf50 62 61 73 65 20 66 69 6c 65 20 28 6e 6f 74 20 61  base file (not a
ddf60 20 6a 6f 75 72 6e 61 6c 2c 20 6d 61 73 74 65 72   journal, master
ddf70 2d 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 65 6d 70  -journal or temp
ddf80 0a 20 20 2a 2a 20 66 69 6c 65 29 2c 20 74 68 65  .  ** file), the
ddf90 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6c 6f   bytes in the lo
ddfa0 63 6b 69 6e 67 20 72 61 6e 67 65 20 73 68 6f 75  cking range shou
ddfb0 6c 64 20 6e 65 76 65 72 20 62 65 20 72 65 61 64  ld never be read
ddfc0 20 6f 72 20 77 72 69 74 74 65 6e 2e 20 2a 2f 0a   or written. */.
ddfd0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
ddfe0 3e 70 55 6e 75 73 65 64 3d 3d 30 0a 20 20 20 20  >pUnused==0.    
ddff0 20 20 20 7c 7c 20 6f 66 66 73 65 74 3e 3d 50 45     || offset>=PE
de000 4e 44 49 4e 47 5f 42 59 54 45 2b 35 31 32 0a 20  NDING_BYTE+512. 
de010 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 2b        || offset+
de020 61 6d 74 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54  amt<=PENDING_BYT
de030 45 20 0a 20 20 29 3b 0a 0a 20 20 67 6f 74 20 3d  E .  );..  got =
de040 20 73 65 65 6b 41 6e 64 52 65 61 64 28 70 46 69   seekAndRead(pFi
de050 6c 65 2c 20 6f 66 66 73 65 74 2c 20 70 42 75 66  le, offset, pBuf
de060 2c 20 61 6d 74 29 3b 0a 20 20 69 66 28 20 67 6f  , amt);.  if( go
de070 74 3d 3d 61 6d 74 20 29 7b 0a 20 20 20 20 72 65  t==amt ){.    re
de080 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
de090 20 20 7d 65 6c 73 65 20 69 66 28 20 67 6f 74 3c    }else if( got<
de0a0 30 20 29 7b 0a 20 20 20 20 2f 2a 20 6c 61 73 74  0 ){.    /* last
de0b0 45 72 72 6e 6f 20 73 65 74 20 62 79 20 73 65 65  Errno set by see
de0c0 6b 41 6e 64 52 65 61 64 20 2a 2f 0a 20 20 20 20  kAndRead */.    
de0d0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
de0e0 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d 65 6c 73  ERR_READ;.  }els
de0f0 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  e{.    pFile->la
de100 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20  stErrno = 0; /* 
de110 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72 72  not a system err
de120 6f 72 20 2a 2f 0a 20 20 20 20 2f 2a 20 55 6e 72  or */.    /* Unr
de130 65 61 64 20 70 61 72 74 73 20 6f 66 20 74 68 65  ead parts of the
de140 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 65 20   buffer must be 
de150 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20  zero-filled */. 
de160 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61     memset(&((cha
de170 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30  r*)pBuf)[got], 0
de180 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20  , amt-got);.    
de190 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
de1a0 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a  ERR_SHORT_READ;.
de1b0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 65    }.}../*.** See
de1c0 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20  k to the offset 
de1d0 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74 20 74 68  in id->offset th
de1e0 65 6e 20 72 65 61 64 20 63 6e 74 20 62 79 74 65  en read cnt byte
de1f0 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a 2a 2a 20  s into pBuf..** 
de200 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
de210 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 61  r of bytes actua
de220 6c 6c 79 20 72 65 61 64 2e 20 20 55 70 64 61 74  lly read.  Updat
de230 65 20 74 68 65 20 6f 66 66 73 65 74 2e 0a 2a 2a  e the offset..**
de240 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 73 74 6f  .** To avoid sto
de250 6d 70 69 6e 67 20 74 68 65 20 65 72 72 6e 6f 20  mping the errno 
de260 76 61 6c 75 65 20 6f 6e 20 61 20 66 61 69 6c 65  value on a faile
de270 64 20 77 72 69 74 65 20 74 68 65 20 6c 61 73 74  d write the last
de280 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a 2a 20 69  Errno value.** i
de290 73 20 73 65 74 20 62 65 66 6f 72 65 20 72 65 74  s set before ret
de2a0 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69  urning..*/.stati
de2b0 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 57 72 69  c int seekAndWri
de2c0 74 65 28 75 6e 69 78 46 69 6c 65 20 2a 69 64 2c  te(unixFile *id,
de2d0 20 69 36 34 20 6f 66 66 73 65 74 2c 20 63 6f 6e   i64 offset, con
de2e0 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69  st void *pBuf, i
de2f0 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67  nt cnt){.  int g
de300 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66  ot;.  i64 newOff
de310 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41  set;.  TIMER_STA
de320 52 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28  RT;.#if defined(
de330 55 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74  USE_PREAD).  got
de340 20 3d 20 70 77 72 69 74 65 28 69 64 2d 3e 68 2c   = pwrite(id->h,
de350 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73   pBuf, cnt, offs
de360 65 74 29 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e  et);.#elif defin
de370 65 64 28 55 53 45 5f 50 52 45 41 44 36 34 29 0a  ed(USE_PREAD64).
de380 20 20 67 6f 74 20 3d 20 70 77 72 69 74 65 36 34    got = pwrite64
de390 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e  (id->h, pBuf, cn
de3a0 74 2c 20 6f 66 66 73 65 74 29 3b 0a 23 65 6c 73  t, offset);.#els
de3b0 65 0a 20 20 6e 65 77 4f 66 66 73 65 74 20 3d 20  e.  newOffset = 
de3c0 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 6f 66 66  lseek(id->h, off
de3d0 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 29 3b 0a  set, SEEK_SET);.
de3e0 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74 21    if( newOffset!
de3f0 3d 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 69  =offset ){.    i
de400 66 28 20 6e 65 77 4f 66 66 73 65 74 20 3d 3d 20  f( newOffset == 
de410 2d 31 20 29 7b 0a 20 20 20 20 20 20 28 28 75 6e  -1 ){.      ((un
de420 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73  ixFile*)id)->las
de430 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
de440 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
de450 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29   ((unixFile*)id)
de460 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b  ->lastErrno = 0;
de470 09 09 09 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  ....    }.    re
de480 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 67  turn -1;.  }.  g
de490 6f 74 20 3d 20 77 72 69 74 65 28 69 64 2d 3e 68  ot = write(id->h
de4a0 2c 20 70 42 75 66 2c 20 63 6e 74 29 3b 0a 23 65  , pBuf, cnt);.#e
de4b0 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f 45 4e 44  ndif.  TIMER_END
de4c0 3b 0a 20 20 69 66 28 20 67 6f 74 3c 30 20 29 7b  ;.  if( got<0 ){
de4d0 0a 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a  .    ((unixFile*
de4e0 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  )id)->lastErrno 
de4f0 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 0a 0a 20 20  = errno;.  }..  
de500 4f 53 54 52 41 43 45 35 28 22 57 52 49 54 45 20  OSTRACE5("WRITE 
de510 20 20 25 2d 33 64 20 25 35 64 20 25 37 6c 6c 64    %-3d %5d %7lld
de520 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d 3e 68 2c   %llu\n", id->h,
de530 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c 20 54 49   got, offset, TI
de540 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b 0a 20 20  MER_ELAPSED);.  
de550 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a  return got;.}...
de560 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61  /*.** Write data
de570 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20 69   from a buffer i
de580 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 65 74  nto a file.  Ret
de590 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e  urn SQLITE_OK on
de5a0 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73   success.** or s
de5b0 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20  ome other error 
de5c0 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e  code on failure.
de5d0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
de5e0 6e 69 78 57 72 69 74 65 28 0a 20 20 73 71 6c 69  nixWrite(.  sqli
de5f0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20  te3_file *id, . 
de600 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75   const void *pBu
de610 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c 0a 20  f, .  int amt,. 
de620 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f   sqlite3_int64 o
de630 66 66 73 65 74 20 0a 29 7b 0a 20 20 75 6e 69 78  ffset .){.  unix
de640 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
de650 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69  nixFile*)id;.  i
de660 6e 74 20 77 72 6f 74 65 20 3d 20 30 3b 0a 20 20  nt wrote = 0;.  
de670 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20  assert( id );.  
de680 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b  assert( amt>0 );
de690 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69  ..  /* If this i
de6a0 73 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  s a database fil
de6b0 65 20 28 6e 6f 74 20 61 20 6a 6f 75 72 6e 61 6c  e (not a journal
de6c0 2c 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c  , master-journal
de6d0 20 6f 72 20 74 65 6d 70 0a 20 20 2a 2a 20 66 69   or temp.  ** fi
de6e0 6c 65 29 2c 20 74 68 65 20 62 79 74 65 73 20 69  le), the bytes i
de6f0 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 72 61  n the locking ra
de700 6e 67 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72  nge should never
de710 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 74   be read or writ
de720 74 65 6e 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  ten. */.  assert
de730 28 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64  ( pFile->pUnused
de740 3d 3d 30 0a 20 20 20 20 20 20 20 7c 7c 20 6f 66  ==0.       || of
de750 66 73 65 74 3e 3d 50 45 4e 44 49 4e 47 5f 42 59  fset>=PENDING_BY
de760 54 45 2b 35 31 32 0a 20 20 20 20 20 20 20 7c 7c  TE+512.       ||
de770 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 50 45 4e   offset+amt<=PEN
de780 44 49 4e 47 5f 42 59 54 45 20 0a 20 20 29 3b 0a  DING_BYTE .  );.
de790 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
de7a0 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 64    /* If we are d
de7b0 6f 69 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 77 72  oing a normal wr
de7c0 69 74 65 20 74 6f 20 61 20 64 61 74 61 62 61 73  ite to a databas
de7d0 65 20 66 69 6c 65 20 28 61 73 20 6f 70 70 6f 73  e file (as oppos
de7e0 65 64 20 74 6f 0a 20 20 2a 2a 20 64 6f 69 6e 67  ed to.  ** doing
de7f0 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72   a hot-journal r
de800 6f 6c 6c 62 61 63 6b 20 6f 72 20 61 20 77 72 69  ollback or a wri
de810 74 65 20 74 6f 20 73 6f 6d 65 20 66 69 6c 65 20  te to some file 
de820 6f 74 68 65 72 20 74 68 61 6e 20 61 0a 20 20 2a  other than a.  *
de830 2a 20 6e 6f 72 6d 61 6c 20 64 61 74 61 62 61 73  * normal databas
de840 65 20 66 69 6c 65 29 20 74 68 65 6e 20 72 65 63  e file) then rec
de850 6f 72 64 20 74 68 65 20 66 61 63 74 20 74 68 61  ord the fact tha
de860 74 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20  t the database. 
de870 20 2a 2a 20 68 61 73 20 63 68 61 6e 67 65 64 2e   ** has changed.
de880 20 20 49 66 20 74 68 65 20 74 72 61 6e 73 61 63    If the transac
de890 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69 73 20  tion counter is 
de8a0 6d 6f 64 69 66 69 65 64 2c 20 72 65 63 6f 72 64  modified, record
de8b0 20 74 68 61 74 0a 20 20 2a 2a 20 66 61 63 74 20   that.  ** fact 
de8c0 74 6f 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  too..  */.  if( 
de8d0 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57  pFile->inNormalW
de8e0 72 69 74 65 20 29 7b 0a 20 20 20 20 70 46 69 6c  rite ){.    pFil
de8f0 65 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 31 3b  e->dbUpdate = 1;
de900 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
de910 65 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66  e has been modif
de920 69 65 64 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f  ied */.    if( o
de930 66 66 73 65 74 3c 3d 32 34 20 26 26 20 6f 66 66  ffset<=24 && off
de940 73 65 74 2b 61 6d 74 3e 3d 32 37 20 29 7b 0a 20  set+amt>=27 ){. 
de950 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20       int rc;.   
de960 20 20 20 63 68 61 72 20 6f 6c 64 43 6e 74 72 5b     char oldCntr[
de970 34 5d 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61  4];.      Simula
de980 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
de990 31 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  1);.      rc = s
de9a0 65 65 6b 41 6e 64 52 65 61 64 28 70 46 69 6c 65  eekAndRead(pFile
de9b0 2c 20 32 34 2c 20 6f 6c 64 43 6e 74 72 2c 20 34  , 24, oldCntr, 4
de9c0 29 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74  );.      Simulat
de9d0 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30  eIOErrorBenign(0
de9e0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
de9f0 3d 34 20 7c 7c 20 6d 65 6d 63 6d 70 28 6f 6c 64  =4 || memcmp(old
dea00 43 6e 74 72 2c 20 26 28 28 63 68 61 72 2a 29 70  Cntr, &((char*)p
dea10 42 75 66 29 5b 32 34 2d 6f 66 66 73 65 74 5d 2c  Buf)[24-offset],
dea20 20 34 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20   4)!=0 ){.      
dea30 20 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e    pFile->transCn
dea40 74 72 43 68 6e 67 20 3d 20 31 3b 20 20 2f 2a 20  trChng = 1;  /* 
dea50 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  The transaction 
dea60 63 6f 75 6e 74 65 72 20 68 61 73 20 63 68 61 6e  counter has chan
dea70 67 65 64 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20  ged */.      }. 
dea80 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
dea90 0a 20 20 77 68 69 6c 65 28 20 61 6d 74 3e 30 20  .  while( amt>0 
deaa0 26 26 20 28 77 72 6f 74 65 20 3d 20 73 65 65 6b  && (wrote = seek
deab0 41 6e 64 57 72 69 74 65 28 70 46 69 6c 65 2c 20  AndWrite(pFile, 
deac0 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61 6d  offset, pBuf, am
dead0 74 29 29 3e 30 20 29 7b 0a 20 20 20 20 61 6d 74  t))>0 ){.    amt
deae0 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 6f   -= wrote;.    o
deaf0 66 66 73 65 74 20 2b 3d 20 77 72 6f 74 65 3b 0a  ffset += wrote;.
deb00 20 20 20 20 70 42 75 66 20 3d 20 26 28 28 63 68      pBuf = &((ch
deb10 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 65 5d  ar*)pBuf)[wrote]
deb20 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c 61 74 65  ;.  }.  Simulate
deb30 49 4f 45 72 72 6f 72 28 28 20 77 72 6f 74 65 3d  IOError(( wrote=
deb40 28 2d 31 29 2c 20 61 6d 74 3d 31 20 29 29 3b 0a  (-1), amt=1 ));.
deb50 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75    SimulateDiskfu
deb60 6c 6c 45 72 72 6f 72 28 28 20 77 72 6f 74 65 3d  llError(( wrote=
deb70 30 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20 69  0, amt=1 ));.  i
deb80 66 28 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20  f( amt>0 ){.    
deb90 69 66 28 20 77 72 6f 74 65 3c 30 20 29 7b 0a 20  if( wrote<0 ){. 
deba0 20 20 20 20 20 2f 2a 20 6c 61 73 74 45 72 72 6e       /* lastErrn
debb0 6f 20 73 65 74 20 62 79 20 73 65 65 6b 41 6e 64  o set by seekAnd
debc0 57 72 69 74 65 20 2a 2f 0a 20 20 20 20 20 20 72  Write */.      r
debd0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
debe0 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d 65  RR_WRITE;.    }e
debf0 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69 6c 65  lse{.      pFile
dec00 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b  ->lastErrno = 0;
dec10 20 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74 65 6d   /* not a system
dec20 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20   error */.      
dec30 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55  return SQLITE_FU
dec40 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  LL;.    }.  }.  
dec50 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
dec60 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
dec70 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f  TE_TEST./*.** Co
dec80 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  unt the number o
dec90 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20  f fullsyncs and 
deca0 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54  normal syncs.  T
decb0 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 74  his is used to t
decc0 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63  est.** that sync
decd0 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20  s and fullsyncs 
dece0 61 72 65 20 6f 63 63 75 72 72 69 6e 67 20 61 74  are occurring at
decf0 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65 73   the right times
ded00 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
ded10 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e 63  int sqlite3_sync
ded20 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49  _count = 0;.SQLI
ded30 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
ded40 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e  e3_fullsync_coun
ded50 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f  t = 0;.#endif../
ded60 2a 0a 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 74  *.** We do not t
ded70 72 75 73 74 20 73 79 73 74 65 6d 73 20 74 6f 20  rust systems to 
ded80 70 72 6f 76 69 64 65 20 61 20 77 6f 72 6b 69 6e  provide a workin
ded90 67 20 66 64 61 74 61 73 79 6e 63 28 29 2e 20 20  g fdatasync().  
deda0 53 6f 6d 65 20 64 6f 2e 0a 2a 2a 20 4f 74 68 65  Some do..** Othe
dedb0 72 73 20 64 6f 20 6e 6f 2e 20 20 54 6f 20 62 65  rs do no.  To be
dedc0 20 73 61 66 65 2c 20 77 65 20 77 69 6c 6c 20 73   safe, we will s
dedd0 74 69 63 6b 20 77 69 74 68 20 74 68 65 20 28 73  tick with the (s
dede0 6c 6f 77 65 72 29 20 66 73 79 6e 63 28 29 2e 0a  lower) fsync()..
dedf0 2a 2a 20 49 66 20 79 6f 75 20 6b 6e 6f 77 20 74  ** If you know t
dee00 68 61 74 20 79 6f 75 72 20 73 79 73 74 65 6d 20  hat your system 
dee10 64 6f 65 73 20 73 75 70 70 6f 72 74 20 66 64 61  does support fda
dee20 74 61 73 79 6e 63 28 29 20 63 6f 72 72 65 63 74  tasync() correct
dee30 6c 79 2c 0a 2a 2a 20 74 68 65 6e 20 73 69 6d 70  ly,.** then simp
dee40 6c 79 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20  ly compile with 
dee50 2d 44 66 64 61 74 61 73 79 6e 63 3d 66 64 61 74  -Dfdatasync=fdat
dee60 61 73 79 6e 63 0a 2a 2f 0a 23 69 66 20 21 64 65  async.*/.#if !de
dee70 66 69 6e 65 64 28 66 64 61 74 61 73 79 6e 63 29  fined(fdatasync)
dee80 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f 6c   && !defined(__l
dee90 69 6e 75 78 5f 5f 29 0a 23 20 64 65 66 69 6e 65  inux__).# define
deea0 20 66 64 61 74 61 73 79 6e 63 20 66 73 79 6e 63   fdatasync fsync
deeb0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44  .#endif../*.** D
deec0 65 66 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46  efine HAVE_FULLF
deed0 53 59 4e 43 20 74 6f 20 30 20 6f 72 20 31 20 64  SYNC to 0 or 1 d
deee0 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74  epending on whet
deef0 68 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 74 68  her or not.** th
def00 65 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20 6d 61  e F_FULLFSYNC ma
def10 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2e 20  cro is defined. 
def20 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20 69 73 20   F_FULLFSYNC is 
def30 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c  currently.** onl
def40 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d  y available on M
def50 61 63 20 4f 53 20 58 2e 20 20 42 75 74 20 74 68  ac OS X.  But th
def60 61 74 20 63 6f 75 6c 64 20 63 68 61 6e 67 65 2e  at could change.
def70 0a 2a 2f 0a 23 69 66 64 65 66 20 46 5f 46 55 4c  .*/.#ifdef F_FUL
def80 4c 46 53 59 4e 43 0a 23 20 64 65 66 69 6e 65 20  LFSYNC.# define 
def90 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 31  HAVE_FULLFSYNC 1
defa0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
defb0 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 30  HAVE_FULLFSYNC 0
defc0 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
defd0 54 68 65 20 66 73 79 6e 63 28 29 20 73 79 73 74  The fsync() syst
defe0 65 6d 20 63 61 6c 6c 20 64 6f 65 73 20 6e 6f 74  em call does not
deff0 20 77 6f 72 6b 20 61 73 20 61 64 76 65 72 74 69   work as adverti
df000 73 65 64 20 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 75  sed on many.** u
df010 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 20 54 68  nix systems.  Th
df020 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 63  e following proc
df030 65 64 75 72 65 20 69 73 20 61 6e 20 61 74 74 65  edure is an atte
df040 6d 70 74 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 69  mpt to make.** i
df050 74 20 77 6f 72 6b 20 62 65 74 74 65 72 2e 0a 2a  t work better..*
df060 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f  *.** The SQLITE_
df070 4e 4f 5f 53 59 4e 43 20 6d 61 63 72 6f 20 64 69  NO_SYNC macro di
df080 73 61 62 6c 65 73 20 61 6c 6c 20 66 73 79 6e 63  sables all fsync
df090 28 29 73 2e 20 20 54 68 69 73 20 69 73 20 75 73  ()s.  This is us
df0a0 65 66 75 6c 0a 2a 2a 20 66 6f 72 20 74 65 73 74  eful.** for test
df0b0 69 6e 67 20 77 68 65 6e 20 77 65 20 77 61 6e 74  ing when we want
df0c0 20 74 6f 20 72 75 6e 20 74 68 72 6f 75 67 68 20   to run through 
df0d0 74 68 65 20 74 65 73 74 20 73 75 69 74 65 20 71  the test suite q
df0e0 75 69 63 6b 6c 79 2e 0a 2a 2a 20 59 6f 75 20 61  uickly..** You a
df0f0 72 65 20 73 74 72 6f 6e 67 6c 79 20 61 64 76 69  re strongly advi
df100 73 65 64 20 2a 6e 6f 74 2a 20 74 6f 20 64 65 70  sed *not* to dep
df110 6c 6f 79 20 77 69 74 68 20 53 51 4c 49 54 45 5f  loy with SQLITE_
df120 4e 4f 5f 53 59 4e 43 0a 2a 2a 20 65 6e 61 62 6c  NO_SYNC.** enabl
df130 65 64 2c 20 68 6f 77 65 76 65 72 2c 20 73 69 6e  ed, however, sin
df140 63 65 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4e  ce with SQLITE_N
df150 4f 5f 53 59 4e 43 20 65 6e 61 62 6c 65 64 2c 20  O_SYNC enabled, 
df160 61 6e 20 4f 53 20 63 72 61 73 68 0a 2a 2a 20 6f  an OS crash.** o
df170 72 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20  r power failure 
df180 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 63 6f 72 72  will likely corr
df190 75 70 74 20 74 68 65 20 64 61 74 61 62 61 73 65  upt the database
df1a0 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c   file..**.** SQL
df1b0 69 74 65 20 73 65 74 73 20 74 68 65 20 64 61 74  ite sets the dat
df1c0 61 4f 6e 6c 79 20 66 6c 61 67 20 69 66 20 74 68  aOnly flag if th
df1d0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69  e size of the fi
df1e0 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e  le is unchanged.
df1f0 0a 2a 2a 20 54 68 65 20 69 64 65 61 20 62 65 68  .** The idea beh
df200 69 6e 64 20 64 61 74 61 4f 6e 6c 79 20 69 73 20  ind dataOnly is 
df210 74 68 61 74 20 69 74 20 73 68 6f 75 6c 64 20 6f  that it should o
df220 6e 6c 79 20 77 72 69 74 65 20 74 68 65 20 66 69  nly write the fi
df230 6c 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 74 6f  le content.** to
df240 20 64 69 73 6b 2c 20 6e 6f 74 20 74 68 65 20 69   disk, not the i
df250 6e 6f 64 65 2e 20 20 57 65 20 6f 6e 6c 79 20 73  node.  We only s
df260 65 74 20 64 61 74 61 4f 6e 6c 79 20 69 66 20 74  et dataOnly if t
df270 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69 73 20  he file size is 
df280 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 73 69  .** unchanged si
df290 6e 63 65 20 74 68 65 20 66 69 6c 65 20 73 69 7a  nce the file siz
df2a0 65 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65  e is part of the
df2b0 20 69 6e 6f 64 65 2e 20 20 48 6f 77 65 76 65 72   inode.  However
df2c0 2c 20 0a 2a 2a 20 54 65 64 20 54 73 27 6f 20 74  , .** Ted Ts'o t
df2d0 65 6c 6c 73 20 75 73 20 74 68 61 74 20 66 64 61  ells us that fda
df2e0 74 61 73 79 6e 63 28 29 20 77 69 6c 6c 20 61 6c  tasync() will al
df2f0 73 6f 20 77 72 69 74 65 20 74 68 65 20 69 6e 6f  so write the ino
df300 64 65 20 69 66 20 74 68 65 0a 2a 2a 20 66 69 6c  de if the.** fil
df310 65 20 73 69 7a 65 20 68 61 73 20 63 68 61 6e 67  e size has chang
df320 65 64 2e 20 20 54 68 65 20 6f 6e 6c 79 20 72 65  ed.  The only re
df330 61 6c 20 64 69 66 66 65 72 65 6e 63 65 20 62 65  al difference be
df340 74 77 65 65 6e 20 66 64 61 74 61 73 79 6e 63 28  tween fdatasync(
df350 29 0a 2a 2a 20 61 6e 64 20 66 73 79 6e 63 28 29  ).** and fsync()
df360 2c 20 54 65 64 20 74 65 6c 6c 73 20 75 73 2c 20  , Ted tells us, 
df370 69 73 20 74 68 61 74 20 66 64 61 74 61 73 79 6e  is that fdatasyn
df380 63 28 29 20 77 69 6c 6c 20 6e 6f 74 20 66 6c 75  c() will not flu
df390 73 68 20 74 68 65 0a 2a 2a 20 69 6e 6f 64 65 20  sh the.** inode 
df3a0 69 66 20 74 68 65 20 6d 74 69 6d 65 20 6f 72 20  if the mtime or 
df3b0 6f 77 6e 65 72 20 6f 72 20 6f 74 68 65 72 20 69  owner or other i
df3c0 6e 6f 64 65 20 61 74 74 72 69 62 75 74 65 73 20  node attributes 
df3d0 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a  have changed..**
df3e0 20 57 65 20 6f 6e 6c 79 20 63 61 72 65 20 61 62   We only care ab
df3f0 6f 75 74 20 74 68 65 20 66 69 6c 65 20 73 69 7a  out the file siz
df400 65 2c 20 6e 6f 74 20 74 68 65 20 6f 74 68 65 72  e, not the other
df410 20 66 69 6c 65 20 61 74 74 72 69 62 75 74 65 73   file attributes
df420 2c 20 73 6f 0a 2a 2a 20 61 73 20 66 61 72 20 61  , so.** as far a
df430 73 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6e 63  s SQLite is conc
df440 65 72 6e 65 64 2c 20 61 6e 20 66 64 61 74 61 73  erned, an fdatas
df450 79 6e 63 28 29 20 69 73 20 61 6c 77 61 79 73 20  ync() is always 
df460 61 64 65 71 75 61 74 65 2e 0a 2a 2a 20 53 6f 2c  adequate..** So,
df470 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 66   we always use f
df480 64 61 74 61 73 79 6e 63 28 29 20 69 66 20 69 74  datasync() if it
df490 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 72   is available, r
df4a0 65 67 61 72 64 6c 65 73 73 20 6f 66 0a 2a 2a 20  egardless of.** 
df4b0 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
df4c0 20 64 61 74 61 4f 6e 6c 79 20 66 6c 61 67 2e 0a   dataOnly flag..
df4d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75  */.static int fu
df4e0 6c 6c 5f 66 73 79 6e 63 28 69 6e 74 20 66 64 2c  ll_fsync(int fd,
df4f0 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 2c 20 69   int fullSync, i
df500 6e 74 20 64 61 74 61 4f 6e 6c 79 29 7b 0a 20 20  nt dataOnly){.  
df510 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68  int rc;..  /* Th
df520 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 22 69 66 64  e following "ifd
df530 65 66 2f 65 6c 69 66 2f 65 6c 73 65 2f 22 20 62  ef/elif/else/" b
df540 6c 6f 63 6b 20 68 61 73 20 74 68 65 20 73 61 6d  lock has the sam
df550 65 20 73 74 72 75 63 74 75 72 65 20 61 73 0a 20  e structure as. 
df560 20 2a 2a 20 74 68 65 20 6f 6e 65 20 62 65 6c 6f   ** the one belo
df570 77 2e 20 49 74 20 69 73 20 72 65 70 6c 69 63 61  w. It is replica
df580 74 65 64 20 68 65 72 65 20 73 6f 6c 65 6c 79 20  ted here solely 
df590 74 6f 20 61 76 6f 69 64 20 63 6c 75 74 74 65 72  to avoid clutter
df5a0 69 6e 67 20 0a 20 20 2a 2a 20 75 70 20 74 68 65  ing .  ** up the
df5b0 20 72 65 61 6c 20 63 6f 64 65 20 77 69 74 68 20   real code with 
df5c0 74 68 65 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  the UNUSED_PARAM
df5d0 45 54 45 52 28 29 20 6d 61 63 72 6f 73 2e 0a 20  ETER() macros.. 
df5e0 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
df5f0 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 55 4e 55 53  E_NO_SYNC.  UNUS
df600 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66 64 29  ED_PARAMETER(fd)
df610 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
df620 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b 0a  ETER(fullSync);.
df630 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
df640 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65  ER(dataOnly);.#e
df650 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59  lif HAVE_FULLFSY
df660 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  NC.  UNUSED_PARA
df670 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b  METER(dataOnly);
df680 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f  .#else.  UNUSED_
df690 50 41 52 41 4d 45 54 45 52 28 66 75 6c 6c 53 79  PARAMETER(fullSy
df6a0 6e 63 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  nc);.  UNUSED_PA
df6b0 52 41 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79  RAMETER(dataOnly
df6c0 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20  );.#endif..  /* 
df6d0 52 65 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 65  Record the numbe
df6e0 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20  r of times that 
df6f0 77 65 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20 66  we do a normal f
df700 73 79 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a 2a  sync() and .  **
df710 20 46 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69 73   FULLSYNC.  This
df720 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67 20   is used during 
df730 74 65 73 74 69 6e 67 20 74 6f 20 76 65 72 69 66  testing to verif
df740 79 20 74 68 61 74 20 74 68 69 73 20 70 72 6f 63  y that this proc
df750 65 64 75 72 65 0a 20 20 2a 2a 20 67 65 74 73 20  edure.  ** gets 
df760 63 61 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20  called with the 
df770 63 6f 72 72 65 63 74 20 61 72 67 75 6d 65 6e 74  correct argument
df780 73 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53  s..  */.#ifdef S
df790 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28  QLITE_TEST.  if(
df7a0 20 66 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c 69   fullSync ) sqli
df7b0 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75  te3_fullsync_cou
df7c0 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f  nt++;.  sqlite3_
df7d0 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65  sync_count++;.#e
df7e0 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65  ndif..  /* If we
df7f0 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74   compiled with t
df800 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e  he SQLITE_NO_SYN
df810 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e  C flag, then syn
df820 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e  cing is a.  ** n
df830 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66  o-op.  */.#ifdef
df840 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a   SQLITE_NO_SYNC.
df850 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
df860 3b 0a 23 65 6c 69 66 20 48 41 56 45 5f 46 55 4c  ;.#elif HAVE_FUL
df870 4c 46 53 59 4e 43 0a 20 20 69 66 28 20 66 75 6c  LFSYNC.  if( ful
df880 6c 53 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20  lSync ){.    rc 
df890 3d 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 46 55  = fcntl(fd, F_FU
df8a0 4c 4c 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20 7d  LLFSYNC, 0);.  }
df8b0 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 31  else{.    rc = 1
df8c0 3b 0a 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74 68  ;.  }.  /* If th
df8d0 65 20 46 55 4c 4c 46 53 59 4e 43 20 66 61 69 6c  e FULLFSYNC fail
df8e0 65 64 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f  ed, fall back to
df8f0 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 20 66   attempting an f
df900 73 79 6e 63 28 29 2e 0a 20 20 2a 2a 20 49 74 20  sync()..  ** It 
df910 73 68 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73  shouldn't be pos
df920 73 69 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66 73  sible for fullfs
df930 79 6e 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20 74  ync to fail on t
df940 68 65 20 6c 6f 63 61 6c 20 0a 20 20 2a 2a 20 66  he local .  ** f
df950 69 6c 65 20 73 79 73 74 65 6d 20 28 6f 6e 20 4f  ile system (on O
df960 53 58 29 2c 20 73 6f 20 66 61 69 6c 75 72 65 20  SX), so failure 
df970 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 46  indicates that F
df980 55 4c 4c 46 53 59 4e 43 0a 20 20 2a 2a 20 69 73  ULLFSYNC.  ** is
df990 6e 27 74 20 73 75 70 70 6f 72 74 65 64 20 66 6f  n't supported fo
df9a0 72 20 74 68 69 73 20 66 69 6c 65 20 73 79 73 74  r this file syst
df9b0 65 6d 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74 20  em. So, attempt 
df9c0 61 6e 20 66 73 79 6e 63 20 0a 20 20 2a 2a 20 61  an fsync .  ** a
df9d0 6e 64 20 28 66 6f 72 20 6e 6f 77 29 20 69 67 6e  nd (for now) ign
df9e0 6f 72 65 20 74 68 65 20 6f 76 65 72 68 65 61 64  ore the overhead
df9f0 20 6f 66 20 61 20 73 75 70 65 72 66 6c 75 6f 75   of a superfluou
dfa00 73 20 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a  s fcntl call.  .
dfa10 20 20 2a 2a 20 49 74 27 64 20 62 65 20 62 65 74    ** It'd be bet
dfa20 74 65 72 20 74 6f 20 64 65 74 65 63 74 20 66 75  ter to detect fu
dfa30 6c 6c 66 73 79 6e 63 20 73 75 70 70 6f 72 74 20  llfsync support 
dfa40 6f 6e 63 65 20 61 6e 64 20 61 76 6f 69 64 20 0a  once and avoid .
dfa50 20 20 2a 2a 20 74 68 65 20 66 63 6e 74 6c 20 63    ** the fcntl c
dfa60 61 6c 6c 20 65 76 65 72 79 20 74 69 6d 65 20 73  all every time s
dfa70 79 6e 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20  ync is called.. 
dfa80 20 2a 2f 0a 20 20 69 66 28 20 72 63 20 29 20 72   */.  if( rc ) r
dfa90 63 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 0a  c = fsync(fd);..
dfaa0 23 65 6c 73 65 20 0a 20 20 72 63 20 3d 20 66 64  #else .  rc = fd
dfab0 61 74 61 73 79 6e 63 28 66 64 29 3b 0a 23 69 66  atasync(fd);.#if
dfac0 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66   OS_VXWORKS.  if
dfad0 28 20 72 63 3d 3d 2d 31 20 26 26 20 65 72 72 6e  ( rc==-1 && errn
dfae0 6f 3d 3d 45 4e 4f 54 53 55 50 20 29 7b 0a 20 20  o==ENOTSUP ){.  
dfaf0 20 20 72 63 20 3d 20 66 73 79 6e 63 28 66 64 29    rc = fsync(fd)
dfb00 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;.  }.#endif /* 
dfb10 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 23 65  OS_VXWORKS */.#e
dfb20 6e 64 69 66 20 2f 2a 20 69 66 64 65 66 20 53 51  ndif /* ifdef SQ
dfb30 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6c 69  LITE_NO_SYNC eli
dfb40 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43  f HAVE_FULLFSYNC
dfb50 20 2a 2f 0a 0a 20 20 69 66 28 20 4f 53 5f 56 58   */..  if( OS_VX
dfb60 57 4f 52 4b 53 20 26 26 20 72 63 21 3d 20 2d 31  WORKS && rc!= -1
dfb70 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 30 3b 0a   ){.    rc = 0;.
dfb80 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
dfb90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73  .}../*.** Make s
dfba0 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20 74  ure all writes t
dfbb0 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66  o a particular f
dfbc0 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65  ile are committe
dfbd0 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a  d to disk..**.**
dfbe0 20 49 66 20 64 61 74 61 4f 6e 6c 79 3d 3d 30 20   If dataOnly==0 
dfbf0 74 68 65 6e 20 62 6f 74 68 20 74 68 65 20 66 69  then both the fi
dfc00 6c 65 20 69 74 73 65 6c 66 20 61 6e 64 20 69 74  le itself and it
dfc10 73 20 6d 65 74 61 64 61 74 61 20 28 66 69 6c 65  s metadata (file
dfc20 0a 2a 2a 20 73 69 7a 65 2c 20 61 63 63 65 73 73  .** size, access
dfc30 20 74 69 6d 65 2c 20 65 74 63 29 20 61 72 65 20   time, etc) are 
dfc40 73 79 6e 63 65 64 2e 20 20 49 66 20 64 61 74 61  synced.  If data
dfc50 4f 6e 6c 79 21 3d 30 20 74 68 65 6e 20 6f 6e 6c  Only!=0 then onl
dfc60 79 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 61  y the.** file da
dfc70 74 61 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a  ta is synced..**
dfc80 0a 2a 2a 20 55 6e 64 65 72 20 55 6e 69 78 2c 20  .** Under Unix, 
dfc90 61 6c 73 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  also make sure t
dfca0 68 61 74 20 74 68 65 20 64 69 72 65 63 74 6f 72  hat the director
dfcb0 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20  y entry for the 
dfcc0 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e  file.** has been
dfcd0 20 63 72 65 61 74 65 64 20 62 79 20 66 73 79 6e   created by fsyn
dfce0 63 2d 69 6e 67 20 74 68 65 20 64 69 72 65 63 74  c-ing the direct
dfcf0 6f 72 79 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ory that contain
dfd00 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 49  s the file..** I
dfd10 66 20 77 65 20 64 6f 20 6e 6f 74 20 64 6f 20 74  f we do not do t
dfd20 68 69 73 20 61 6e 64 20 77 65 20 65 6e 63 6f 75  his and we encou
dfd30 6e 74 65 72 20 61 20 70 6f 77 65 72 20 66 61 69  nter a power fai
dfd40 6c 75 72 65 2c 20 74 68 65 20 64 69 72 65 63 74  lure, the direct
dfd50 6f 72 79 0a 2a 2a 20 65 6e 74 72 79 20 66 6f 72  ory.** entry for
dfd60 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67   the journal mig
dfd70 68 74 20 6e 6f 74 20 65 78 69 73 74 20 61 66 74  ht not exist aft
dfd80 65 72 20 77 65 20 72 65 62 6f 6f 74 2e 20 20 54  er we reboot.  T
dfd90 68 65 20 6e 65 78 74 0a 2a 2a 20 53 51 4c 69 74  he next.** SQLit
dfda0 65 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20  e to access the 
dfdb0 66 69 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e  file will not kn
dfdc0 6f 77 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72  ow that the jour
dfdd0 6e 61 6c 20 65 78 69 73 74 73 20 28 62 65 63 61  nal exists (beca
dfde0 75 73 65 0a 2a 2a 20 74 68 65 20 64 69 72 65 63  use.** the direc
dfdf0 74 6f 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74  tory entry for t
dfe00 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6e  he journal was n
dfe10 65 76 65 72 20 63 72 65 61 74 65 64 29 20 61 6e  ever created) an
dfe20 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  d the transactio
dfe30 6e 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 6f  n.** will not ro
dfe40 6c 6c 20 62 61 63 6b 20 2d 20 70 6f 73 73 69 62  ll back - possib
dfe50 6c 79 20 6c 65 61 64 69 6e 67 20 74 6f 20 64 61  ly leading to da
dfe60 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f  tabase corruptio
dfe70 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
dfe80 20 75 6e 69 78 53 79 6e 63 28 73 71 6c 69 74 65   unixSync(sqlite
dfe90 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
dfea0 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63  flags){.  int rc
dfeb0 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  ;.  unixFile *pF
dfec0 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
dfed0 29 69 64 3b 0a 0a 20 20 69 6e 74 20 69 73 44 61  )id;..  int isDa
dfee0 74 61 4f 6e 6c 79 20 3d 20 28 66 6c 61 67 73 26  taOnly = (flags&
dfef0 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41  SQLITE_SYNC_DATA
dff00 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 46  ONLY);.  int isF
dff10 75 6c 6c 73 79 6e 63 20 3d 20 28 66 6c 61 67 73  ullsync = (flags
dff20 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53  &0x0F)==SQLITE_S
dff30 59 4e 43 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20  YNC_FULL;..  /* 
dff40 43 68 65 63 6b 20 74 68 61 74 20 6f 6e 65 20 6f  Check that one o
dff50 66 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f  f SQLITE_SYNC_NO
dff60 52 4d 41 4c 20 6f 72 20 46 55 4c 4c 20 77 61 73  RMAL or FULL was
dff70 20 70 61 73 73 65 64 20 2a 2f 0a 20 20 61 73 73   passed */.  ass
dff80 65 72 74 28 28 66 6c 61 67 73 26 30 78 30 46 29  ert((flags&0x0F)
dff90 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f  ==SQLITE_SYNC_NO
dffa0 52 4d 41 4c 0a 20 20 20 20 20 20 7c 7c 20 28 66  RMAL.      || (f
dffb0 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c 49  lags&0x0F)==SQLI
dffc0 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a 20 20 29  TE_SYNC_FULL.  )
dffd0 3b 0a 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61 6e  ;..  /* Unix can
dffe0 6e 6f 74 2c 20 62 75 74 20 73 6f 6d 65 20 73 79  not, but some sy
dfff0 73 74 65 6d 73 20 6d 61 79 20 72 65 74 75 72 6e  stems may return
e0000 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72 6f   SQLITE_FULL fro
e0010 6d 20 68 65 72 65 2e 20 54 68 69 73 0a 20 20 2a  m here. This.  *
e0020 2a 20 6c 69 6e 65 20 69 73 20 74 6f 20 74 65 73  * line is to tes
e0030 74 20 74 68 61 74 20 64 6f 69 6e 67 20 73 6f 20  t that doing so 
e0040 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 65 20 61  does not cause a
e0050 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a  ny problems..  *
e0060 2f 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b  /.  SimulateDisk
e0070 66 75 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 72  fullError( retur
e0080 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b  n SQLITE_FULL );
e0090 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ..  assert( pFil
e00a0 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28  e );.  OSTRACE2(
e00b0 22 53 59 4e 43 20 20 20 20 25 2d 33 64 5c 6e 22  "SYNC    %-3d\n"
e00c0 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 72  , pFile->h);.  r
e00d0 63 20 3d 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70  c = full_fsync(p
e00e0 46 69 6c 65 2d 3e 68 2c 20 69 73 46 75 6c 6c 73  File->h, isFulls
e00f0 79 6e 63 2c 20 69 73 44 61 74 61 4f 6e 6c 79 29  ync, isDataOnly)
e0100 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
e0110 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 69  ror( rc=1 );.  i
e0120 66 28 20 72 63 20 29 7b 0a 20 20 20 20 70 46 69  f( rc ){.    pFi
e0130 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
e0140 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72  errno;.    retur
e0150 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46  n SQLITE_IOERR_F
e0160 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 69 66 28 20  SYNC;.  }.  if( 
e0170 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 20  pFile->dirfd>=0 
e0180 29 7b 0a 20 20 20 20 69 6e 74 20 65 72 72 3b 0a  ){.    int err;.
e0190 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 44 49      OSTRACE4("DI
e01a0 52 53 59 4e 43 20 25 2d 33 64 20 28 68 61 76 65  RSYNC %-3d (have
e01b0 5f 66 75 6c 6c 66 73 79 6e 63 3d 25 64 20 66 75  _fullfsync=%d fu
e01c0 6c 6c 73 79 6e 63 3d 25 64 29 5c 6e 22 2c 20 70  llsync=%d)\n", p
e01d0 46 69 6c 65 2d 3e 64 69 72 66 64 2c 0a 20 20 20  File->dirfd,.   
e01e0 20 20 20 20 20 20 20 20 20 48 41 56 45 5f 46 55           HAVE_FU
e01f0 4c 4c 46 53 59 4e 43 2c 20 69 73 46 75 6c 6c 73  LLFSYNC, isFulls
e0200 79 6e 63 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  ync);.#ifndef SQ
e0210 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 52  LITE_DISABLE_DIR
e0220 53 59 4e 43 0a 20 20 20 20 2f 2a 20 54 68 65 20  SYNC.    /* The 
e0230 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20 69  directory sync i
e0240 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65 64  s only attempted
e0250 20 69 66 20 66 75 6c 6c 5f 66 73 79 6e 63 20 69   if full_fsync i
e0260 73 0a 20 20 20 20 2a 2a 20 74 75 72 6e 65 64 20  s.    ** turned 
e0270 6f 66 66 20 6f 72 20 75 6e 61 76 61 69 6c 61 62  off or unavailab
e0280 6c 65 2e 20 20 49 66 20 61 20 66 75 6c 6c 5f 66  le.  If a full_f
e0290 73 79 6e 63 20 6f 63 63 75 72 72 65 64 20 61 62  sync occurred ab
e02a0 6f 76 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e  ove,.    ** then
e02b0 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 73   the directory s
e02c0 79 6e 63 20 69 73 20 73 75 70 65 72 66 6c 75 6f  ync is superfluo
e02d0 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  us..    */.    i
e02e0 66 28 20 28 21 48 41 56 45 5f 46 55 4c 4c 46 53  f( (!HAVE_FULLFS
e02f0 59 4e 43 20 7c 7c 20 21 69 73 46 75 6c 6c 73 79  YNC || !isFullsy
e0300 6e 63 29 20 26 26 20 66 75 6c 6c 5f 66 73 79 6e  nc) && full_fsyn
e0310 63 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c 30  c(pFile->dirfd,0
e0320 2c 30 29 20 29 7b 0a 20 20 20 20 20 20 20 2f 2a  ,0) ){.       /*
e0330 0a 20 20 20 20 20 20 20 2a 2a 20 57 65 20 68 61  .       ** We ha
e0340 76 65 20 72 65 63 65 69 76 65 64 20 6d 75 6c 74  ve received mult
e0350 69 70 6c 65 20 72 65 70 6f 72 74 73 20 6f 66 20  iple reports of 
e0360 66 73 79 6e 63 28 29 20 72 65 74 75 72 6e 69 6e  fsync() returnin
e0370 67 0a 20 20 20 20 20 20 20 2a 2a 20 65 72 72 6f  g.       ** erro
e0380 72 73 20 77 68 65 6e 20 61 70 70 6c 69 65 64 20  rs when applied 
e0390 74 6f 20 64 69 72 65 63 74 6f 72 69 65 73 20 6f  to directories o
e03a0 6e 20 63 65 72 74 61 69 6e 20 66 69 6c 65 20 73  n certain file s
e03b0 79 73 74 65 6d 73 2e 0a 20 20 20 20 20 20 20 2a  ystems..       *
e03c0 2a 20 41 20 66 61 69 6c 65 64 20 64 69 72 65 63  * A failed direc
e03d0 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6e 6f 74  tory sync is not
e03e0 20 61 20 62 69 67 20 64 65 61 6c 2e 20 20 53 6f   a big deal.  So
e03f0 20 69 74 20 73 65 65 6d 73 0a 20 20 20 20 20 20   it seems.      
e0400 20 2a 2a 20 62 65 74 74 65 72 20 74 6f 20 69 67   ** better to ig
e0410 6e 6f 72 65 20 74 68 65 20 65 72 72 6f 72 2e 20  nore the error. 
e0420 20 54 69 63 6b 65 74 20 23 31 36 35 37 0a 20 20   Ticket #1657.  
e0430 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 2f       */.       /
e0440 2a 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72  * pFile->lastErr
e0450 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 2a 2f 0a 20  no = errno; */. 
e0460 20 20 20 20 20 20 2f 2a 20 72 65 74 75 72 6e 20        /* return 
e0470 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 20 2a 2f  SQLITE_IOERR; */
e0480 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
e0490 20 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 46    err = close(pF
e04a0 69 6c 65 2d 3e 64 69 72 66 64 29 3b 20 2f 2a 20  ile->dirfd); /* 
e04b0 4f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 73 79 6e  Only need to syn
e04c0 63 20 6f 6e 63 65 2c 20 73 6f 20 63 6c 6f 73 65  c once, so close
e04d0 20 74 68 65 20 2a 2f 0a 20 20 20 20 69 66 28 20   the */.    if( 
e04e0 65 72 72 3d 3d 30 20 29 7b 20 20 20 20 20 20 20  err==0 ){       
e04f0 20 20 20 20 20 20 20 2f 2a 20 64 69 72 65 63 74         /* direct
e0500 6f 72 79 20 77 68 65 6e 20 77 65 20 61 72 65 20  ory when we are 
e0510 64 6f 6e 65 20 2a 2f 0a 20 20 20 20 20 20 70 46  done */.      pF
e0520 69 6c 65 2d 3e 64 69 72 66 64 20 3d 20 2d 31 3b  ile->dirfd = -1;
e0530 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
e0540 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
e0550 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
e0560 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
e0570 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20  ERR_DIR_CLOSE;. 
e0580 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
e0590 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
e05a0 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20  runcate an open 
e05b0 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66  file to a specif
e05c0 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74  ied size.*/.stat
e05d0 69 63 20 69 6e 74 20 75 6e 69 78 54 72 75 6e 63  ic int unixTrunc
e05e0 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ate(sqlite3_file
e05f0 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65 29   *id, i64 nByte)
e0600 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73  {.  int rc;.  as
e0610 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 53 69  sert( id );.  Si
e0620 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72  mulateIOError( r
e0630 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
e0640 52 52 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a 20  RR_TRUNCATE );. 
e0650 20 72 63 20 3d 20 66 74 72 75 6e 63 61 74 65 28   rc = ftruncate(
e0660 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d  ((unixFile*)id)-
e0670 3e 68 2c 20 28 6f 66 66 5f 74 29 6e 42 79 74 65  >h, (off_t)nByte
e0680 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20  );.  if( rc ){. 
e0690 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69     ((unixFile*)i
e06a0 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  d)->lastErrno = 
e06b0 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72  errno;.    retur
e06c0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54  n SQLITE_IOERR_T
e06d0 52 55 4e 43 41 54 45 3b 0a 20 20 7d 65 6c 73 65  RUNCATE;.  }else
e06e0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
e06f0 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ITE_OK;.  }.}../
e0700 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74  *.** Determine t
e0710 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20  he current size 
e0720 6f 66 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74  of a file in byt
e0730 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  es.*/.static int
e0740 20 75 6e 69 78 46 69 6c 65 53 69 7a 65 28 73 71   unixFileSize(sq
e0750 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
e0760 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69  i64 *pSize){.  i
e0770 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 63 74 20  nt rc;.  struct 
e0780 73 74 61 74 20 62 75 66 3b 0a 20 20 61 73 73 65  stat buf;.  asse
e0790 72 74 28 20 69 64 20 29 3b 0a 20 20 72 63 20 3d  rt( id );.  rc =
e07a0 20 66 73 74 61 74 28 28 28 75 6e 69 78 46 69 6c   fstat(((unixFil
e07b0 65 2a 29 69 64 29 2d 3e 68 2c 20 26 62 75 66 29  e*)id)->h, &buf)
e07c0 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
e07d0 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 69  ror( rc=1 );.  i
e07e0 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20  f( rc!=0 ){.    
e07f0 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d  ((unixFile*)id)-
e0800 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
e0810 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  no;.    return S
e0820 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41  QLITE_IOERR_FSTA
e0830 54 3b 0a 20 20 7d 0a 20 20 2a 70 53 69 7a 65 20  T;.  }.  *pSize 
e0840 3d 20 62 75 66 2e 73 74 5f 73 69 7a 65 3b 0a 0a  = buf.st_size;..
e0850 20 20 2f 2a 20 57 68 65 6e 20 6f 70 65 6e 69 6e    /* When openin
e0860 67 20 61 20 7a 65 72 6f 2d 73 69 7a 65 20 64 61  g a zero-size da
e0870 74 61 62 61 73 65 2c 20 74 68 65 20 66 69 6e 64  tabase, the find
e0880 4c 6f 63 6b 49 6e 66 6f 28 29 20 70 72 6f 63 65  LockInfo() proce
e0890 64 75 72 65 0a 20 20 2a 2a 20 77 72 69 74 65 73  dure.  ** writes
e08a0 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 69   a single byte i
e08b0 6e 74 6f 20 74 68 61 74 20 66 69 6c 65 20 69 6e  nto that file in
e08c0 20 6f 72 64 65 72 20 74 6f 20 77 6f 72 6b 20 61   order to work a
e08d0 72 6f 75 6e 64 20 61 20 62 75 67 0a 20 20 2a 2a  round a bug.  **
e08e0 20 69 6e 20 74 68 65 20 4f 53 2d 58 20 6d 73 64   in the OS-X msd
e08f0 6f 73 20 66 69 6c 65 73 79 73 74 65 6d 2e 20 20  os filesystem.  
e0900 49 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69  In order to avoi
e0910 64 20 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 20  d problems with 
e0920 75 70 70 65 72 0a 20 20 2a 2a 20 6c 61 79 65 72  upper.  ** layer
e0930 73 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65  s, we need to re
e0940 70 6f 72 74 20 74 68 69 73 20 66 69 6c 65 20 73  port this file s
e0950 69 7a 65 20 61 73 20 7a 65 72 6f 20 65 76 65 6e  ize as zero even
e0960 20 74 68 6f 75 67 68 20 69 74 20 69 73 0a 20 20   though it is.  
e0970 2a 2a 20 72 65 61 6c 6c 79 20 31 2e 20 20 20 54  ** really 1.   T
e0980 69 63 6b 65 74 20 23 33 32 36 30 2e 0a 20 20 2a  icket #3260..  *
e0990 2f 0a 20 20 69 66 28 20 2a 70 53 69 7a 65 3d 3d  /.  if( *pSize==
e09a0 31 20 29 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a  1 ) *pSize = 0;.
e09b0 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
e09c0 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c  E_OK;.}..#if SQL
e09d0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
e09e0 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69  NG_STYLE && defi
e09f0 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f  ned(__APPLE__)./
e0a00 2a 0a 2a 2a 20 48 61 6e 64 6c 65 72 20 66 6f 72  *.** Handler for
e0a10 20 70 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 66   proxy-locking f
e0a20 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 76 65 72 62  ile-control verb
e0a30 73 2e 20 20 44 65 66 69 6e 65 64 20 62 65 6c 6f  s.  Defined belo
e0a40 77 20 69 6e 20 74 68 65 0a 2a 2a 20 70 72 6f 78  w in the.** prox
e0a50 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 64 69 76  ying locking div
e0a60 69 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ision..*/.static
e0a70 20 69 6e 74 20 70 72 6f 78 79 46 69 6c 65 43 6f   int proxyFileCo
e0a80 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69  ntrol(sqlite3_fi
e0a90 6c 65 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a  le*,int,void*);.
e0aa0 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49  #endif.../*.** I
e0ab0 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 20 63  nformation and c
e0ac0 6f 6e 74 72 6f 6c 20 6f 66 20 61 6e 20 6f 70 65  ontrol of an ope
e0ad0 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a  n file handle..*
e0ae0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
e0af0 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c  xFileControl(sql
e0b00 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
e0b10 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72  nt op, void *pAr
e0b20 67 29 7b 0a 20 20 73 77 69 74 63 68 28 20 6f 70  g){.  switch( op
e0b30 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   ){.    case SQL
e0b40 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54  ITE_FCNTL_LOCKST
e0b50 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69  ATE: {.      *(i
e0b60 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 75 6e 69  nt*)pArg = ((uni
e0b70 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b  xFile*)id)->lock
e0b80 74 79 70 65 3b 0a 20 20 20 20 20 20 72 65 74 75  type;.      retu
e0b90 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
e0ba0 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
e0bb0 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 3a 20  ITE_LAST_ERRNO: 
e0bc0 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70  {.      *(int*)p
e0bd0 41 72 67 20 3d 20 28 28 75 6e 69 78 46 69 6c 65  Arg = ((unixFile
e0be0 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f  *)id)->lastErrno
e0bf0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
e0c00 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
e0c10 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
e0c20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20     /* The pager 
e0c30 63 61 6c 6c 73 20 74 68 69 73 20 6d 65 74 68 6f  calls this metho
e0c40 64 20 74 6f 20 73 69 67 6e 61 6c 20 74 68 61 74  d to signal that
e0c50 20 69 74 20 68 61 73 20 64 6f 6e 65 0a 20 20 20   it has done.   
e0c60 20 2a 2a 20 61 20 72 6f 6c 6c 62 61 63 6b 20 61   ** a rollback a
e0c70 6e 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61  nd that the data
e0c80 62 61 73 65 20 69 73 20 74 68 65 72 65 66 6f 72  base is therefor
e0c90 65 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 0a  e unchanged and.
e0ca0 20 20 20 20 2a 2a 20 69 74 20 68 65 6e 63 65 20      ** it hence 
e0cb0 69 74 20 69 73 20 4f 4b 20 66 6f 72 20 74 68 65  it is OK for the
e0cc0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 68 61   transaction cha
e0cd0 6e 67 65 20 63 6f 75 6e 74 65 72 20 74 6f 20 62  nge counter to b
e0ce0 65 0a 20 20 20 20 2a 2a 20 75 6e 63 68 61 6e 67  e.    ** unchang
e0cf0 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  ed..    */.    c
e0d00 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c  ase SQLITE_FCNTL
e0d10 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 3a 20 7b  _DB_UNCHANGED: {
e0d20 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c  .      ((unixFil
e0d30 65 2a 29 69 64 29 2d 3e 64 62 55 70 64 61 74 65  e*)id)->dbUpdate
e0d40 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75   = 0;.      retu
e0d50 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
e0d60 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 20 53    }.#endif.#if S
e0d70 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
e0d80 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65  KING_STYLE && de
e0d90 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
e0da0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
e0db0 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _SET_LOCKPROXYFI
e0dc0 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c  LE:.    case SQL
e0dd0 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58  ITE_GET_LOCKPROX
e0de0 59 46 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20 72  YFILE: {.      r
e0df0 65 74 75 72 6e 20 70 72 6f 78 79 46 69 6c 65 43  eturn proxyFileC
e0e00 6f 6e 74 72 6f 6c 28 69 64 2c 6f 70 2c 70 41 72  ontrol(id,op,pAr
e0e10 67 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  g);.    }.#endif
e0e20 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
e0e30 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
e0e40 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  && defined(__APP
e0e50 4c 45 5f 5f 29 20 2a 2f 0a 20 20 7d 0a 20 20 72  LE__) */.  }.  r
e0e60 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
e0e70 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  OR;.}../*.** Ret
e0e80 75 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73  urn the sector s
e0e90 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20  ize in bytes of 
e0ea0 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62  the underlying b
e0eb0 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a  lock device for.
e0ec0 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64  ** the specified
e0ed0 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61   file. This is a
e0ee0 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32  lmost always 512
e0ef0 20 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20   bytes, but may 
e0f00 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72  be.** larger for
e0f10 20 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a   some devices..*
e0f20 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65  *.** SQLite code
e0f30 20 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75   assumes this fu
e0f40 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61  nction cannot fa
e0f50 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75  il. It also assu
e0f60 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74  mes that.** if t
e0f70 77 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65  wo files are cre
e0f80 61 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65  ated in the same
e0f90 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72   file-system dir
e0fa0 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20  ectory (i.e..** 
e0fb0 61 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69  a database and i
e0fc0 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29  ts journal file)
e0fd0 20 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72   that the sector
e0fe0 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68   size will be th
e0ff0 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f  e.** same for bo
e1000 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  th..*/.static in
e1010 74 20 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65  t unixSectorSize
e1020 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e  (sqlite3_file *N
e1030 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45  otUsed){.  UNUSE
e1040 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
e1050 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  sed);.  return S
e1060 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45  QLITE_DEFAULT_SE
e1070 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a  CTOR_SIZE;.}../*
e1080 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64  .** Return the d
e1090 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69  evice characteri
e10a0 73 74 69 63 73 20 66 6f 72 20 74 68 65 20 66 69  stics for the fi
e10b0 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 77 61  le. This is alwa
e10c0 79 73 20 30 20 66 6f 72 20 75 6e 69 78 2e 0a 2a  ys 0 for unix..*
e10d0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
e10e0 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72  xDeviceCharacter
e10f0 69 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66  istics(sqlite3_f
e1100 69 6c 65 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20  ile *NotUsed){. 
e1110 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
e1120 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65  R(NotUsed);.  re
e1130 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
e1140 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 20 69   Here ends the i
e1150 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
e1160 20 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c   all sqlite3_fil
e1170 65 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a  e methods..**.**
e1180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1190 2a 2a 2a 2a 20 45 6e 64 20 73 71 6c 69 74 65 33  **** End sqlite3
e11a0 5f 66 69 6c 65 20 4d 65 74 68 6f 64 73 20 2a 2a  _file Methods **
e11b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e11c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
e11d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e11e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e11f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
e1220 2a 0a 2a 2a 20 54 68 69 73 20 64 69 76 69 73 69  *.** This divisi
e1230 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 64 65 66 69  on contains defi
e1240 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74  nitions of sqlit
e1250 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62  e3_io_methods ob
e1260 6a 65 63 74 73 20 74 68 61 74 0a 2a 2a 20 69 6d  jects that.** im
e1270 70 6c 65 6d 65 6e 74 20 76 61 72 69 6f 75 73 20  plement various 
e1280 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72  file locking str
e1290 61 74 65 67 69 65 73 2e 20 20 49 74 20 61 6c 73  ategies.  It als
e12a0 6f 20 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e  o contains defin
e12b0 69 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20 22 66 69  itions.** of "fi
e12c0 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 73 2e  nder" functions.
e12d0 20 20 41 20 66 69 6e 64 65 72 2d 66 75 6e 63 74    A finder-funct
e12e0 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 6c  ion is used to l
e12f0 6f 63 61 74 65 20 74 68 65 20 61 70 70 72 6f 70  ocate the approp
e1300 72 69 61 74 65 0a 2a 2a 20 73 71 6c 69 74 65 33  riate.** sqlite3
e1310 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  _io_methods obje
e1320 63 74 20 66 6f 72 20 61 20 70 61 72 74 69 63 75  ct for a particu
e1330 6c 61 72 20 64 61 74 61 62 61 73 65 20 66 69 6c  lar database fil
e1340 65 2e 20 20 54 68 65 20 70 41 70 70 44 61 74 61  e.  The pAppData
e1350 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65  .** field of the
e1360 20 73 71 6c 69 74 65 33 5f 76 66 73 20 56 46 53   sqlite3_vfs VFS
e1370 20 6f 62 6a 65 63 74 73 20 61 72 65 20 69 6e 69   objects are ini
e1380 74 69 61 6c 69 7a 65 64 20 74 6f 20 62 65 20 70  tialized to be p
e1390 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68  ointers to.** th
e13a0 65 20 63 6f 72 72 65 63 74 20 66 69 6e 64 65 72  e correct finder
e13b0 2d 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68  -function for th
e13c0 61 74 20 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 4d 6f  at VFS..**.** Mo
e13d0 73 74 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69  st finder functi
e13e0 6f 6e 73 20 72 65 74 75 72 6e 20 61 20 70 6f 69  ons return a poi
e13f0 6e 74 65 72 20 74 6f 20 61 20 66 69 78 65 64 20  nter to a fixed 
e1400 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
e1410 64 73 0a 2a 2a 20 6f 62 6a 65 63 74 2e 20 20 54  ds.** object.  T
e1420 68 65 20 6f 6e 6c 79 20 69 6e 74 65 72 65 73 74  he only interest
e1430 69 6e 67 20 66 69 6e 64 65 72 2d 66 75 6e 63 74  ing finder-funct
e1440 69 6f 6e 20 69 73 20 61 75 74 6f 6c 6f 63 6b 49  ion is autolockI
e1450 6f 46 69 6e 64 65 72 2c 20 77 68 69 63 68 0a 2a  oFinder, which.*
e1460 2a 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 66  * looks at the f
e1470 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65 20 61  ilesystem type a
e1480 6e 64 20 74 72 69 65 73 20 74 6f 20 67 75 65 73  nd tries to gues
e1490 73 20 74 68 65 20 62 65 73 74 20 6c 6f 63 6b 69  s the best locki
e14a0 6e 67 0a 2a 2a 20 73 74 72 61 74 65 67 79 20 66  ng.** strategy f
e14b0 72 6f 6d 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20  rom that..**.** 
e14c0 46 6f 72 20 66 69 6e 64 65 72 2d 66 75 6e 74 69  For finder-funti
e14d0 6f 6e 20 46 2c 20 74 77 6f 20 6f 62 6a 65 63 74  on F, two object
e14e0 73 20 61 72 65 20 63 72 65 61 74 65 64 3a 0a 2a  s are created:.*
e14f0 2a 0a 2a 2a 20 20 20 20 28 31 29 20 54 68 65 20  *.**    (1) The 
e1500 72 65 61 6c 20 66 69 6e 64 65 72 2d 66 75 6e 63  real finder-func
e1510 74 69 6f 6e 20 6e 61 6d 65 64 20 22 46 49 6d 70  tion named "FImp
e1520 74 28 29 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28  t()"..**.**    (
e1530 32 29 20 41 20 63 6f 6e 73 74 61 6e 74 20 70 6f  2) A constant po
e1540 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 66 75  inter to this fu
e1550 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 6a 75 73  nction named jus
e1560 74 20 22 46 22 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20  t "F"..**.**.** 
e1570 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
e1580 20 46 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73   F pointer is us
e1590 65 64 20 61 73 20 74 68 65 20 70 41 70 70 44 61  ed as the pAppDa
e15a0 74 61 20 76 61 6c 75 65 20 66 6f 72 20 56 46 53  ta value for VFS
e15b0 0a 2a 2a 20 6f 62 6a 65 63 74 73 2e 20 20 57 65  .** objects.  We
e15c0 20 68 61 76 65 20 74 6f 20 64 6f 20 74 68 69 73   have to do this
e15d0 20 69 6e 73 74 65 61 64 20 6f 66 20 6c 65 74 74   instead of lett
e15e0 69 6e 67 20 70 41 70 70 44 61 74 61 20 70 6f 69  ing pAppData poi
e15f0 6e 74 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 61  nt.** directly a
e1600 74 20 74 68 65 20 66 69 6e 64 65 72 2d 66 75 6e  t the finder-fun
e1610 63 74 69 6f 6e 20 73 69 6e 63 65 20 43 39 30 20  ction since C90 
e1620 72 75 6c 65 73 20 70 72 65 76 65 6e 74 20 61 20  rules prevent a 
e1630 76 6f 69 64 2a 0a 2a 2a 20 66 72 6f 6d 20 62 65  void*.** from be
e1640 20 63 61 73 74 20 69 6e 74 6f 20 61 20 66 75 6e   cast into a fun
e1650 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a  ction pointer..*
e1660 2a 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 69 6e 73  *.**.** Each ins
e1670 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6d 61  tance of this ma
e1680 63 72 6f 20 67 65 6e 65 72 61 74 65 73 20 74 77  cro generates tw
e1690 6f 20 6f 62 6a 65 63 74 73 3a 0a 2a 2a 0a 2a 2a  o objects:.**.**
e16a0 20 20 20 2a 20 20 41 20 63 6f 6e 73 74 61 6e 74     *  A constant
e16b0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
e16c0 6f 64 73 20 6f 62 6a 65 63 74 20 63 61 6c 6c 20  ods object call 
e16d0 4d 45 54 48 4f 44 20 74 68 61 74 20 68 61 73 20  METHOD that has 
e16e0 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20  locking.**      
e16f0 6d 65 74 68 6f 64 73 20 43 4c 4f 53 45 2c 20 4c  methods CLOSE, L
e1700 4f 43 4b 2c 20 55 4e 4c 4f 43 4b 2c 20 43 4b 52  OCK, UNLOCK, CKR
e1710 45 53 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20  ESLOCK..**.**   
e1720 2a 20 20 41 6e 20 49 2f 4f 20 6d 65 74 68 6f 64  *  An I/O method
e1730 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e   finder function
e1740 20 63 61 6c 6c 65 64 20 46 49 4e 44 45 52 20 74   called FINDER t
e1750 68 61 74 20 72 65 74 75 72 6e 73 20 61 20 70 6f  hat returns a po
e1760 69 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 74 6f  inter.**      to
e1770 20 74 68 65 20 4d 45 54 48 4f 44 20 6f 62 6a 65   the METHOD obje
e1780 63 74 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f  ct in the previo
e1790 75 73 20 62 75 6c 6c 65 74 2e 0a 2a 2f 0a 23 64  us bullet..*/.#d
e17a0 65 66 69 6e 65 20 49 4f 4d 45 54 48 4f 44 53 28  efine IOMETHODS(
e17b0 46 49 4e 44 45 52 2c 20 4d 45 54 48 4f 44 2c 20  FINDER, METHOD, 
e17c0 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c  CLOSE, LOCK, UNL
e17d0 4f 43 4b 2c 20 43 4b 4c 4f 43 4b 29 20 20 20 20  OCK, CKLOCK)    
e17e0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 73 74 61             \.sta
e17f0 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
e1800 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 45 54  3_io_methods MET
e1810 48 4f 44 20 3d 20 7b 20 20 20 20 20 20 20 20 20  HOD = {         
e1820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1830 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 31            \.   1
e1840 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e1850 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56             /* iV
e1860 65 72 73 69 6f 6e 20 2a 2f 20 20 20 20 20 20 20  ersion */       
e1870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1880 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 43 4c           \.   CL
e1890 4f 53 45 2c 20 20 20 20 20 20 20 20 20 20 20 20  OSE,            
e18a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c            /* xCl
e18b0 6f 73 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ose */          
e18c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e18d0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69          \.   uni
e18e0 78 52 65 61 64 2c 20 20 20 20 20 20 20 20 20 20  xRead,          
e18f0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 52 65 61           /* xRea
e1900 64 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  d */            
e1910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1920 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78         \.   unix
e1930 57 72 69 74 65 2c 20 20 20 20 20 20 20 20 20 20  Write,          
e1940 20 20 20 20 20 20 20 20 2f 2a 20 78 57 72 69 74          /* xWrit
e1950 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  e */            
e1960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1970 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 54        \.   unixT
e1980 72 75 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20  runcate,        
e1990 20 20 20 20 20 20 20 2f 2a 20 78 54 72 75 6e 63         /* xTrunc
e19a0 61 74 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ate */          
e19b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e19c0 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 53 79       \.   unixSy
e19d0 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  nc,             
e19e0 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a        /* xSync *
e19f0 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
e1a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1a10 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 46 69 6c      \.   unixFil
e1a20 65 53 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20  eSize,          
e1a30 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a       /* xFileSiz
e1a40 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  e */            
e1a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1a60 20 20 20 5c 0a 20 20 20 4c 4f 43 4b 2c 20 20 20     \.   LOCK,   
e1a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1a80 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 20      /* xLock */ 
e1a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1ab0 20 20 5c 0a 20 20 20 55 4e 4c 4f 43 4b 2c 20 20    \.   UNLOCK,  
e1ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1ad0 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f     /* xUnlock */
e1ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1b00 20 5c 0a 20 20 20 43 4b 4c 4f 43 4b 2c 20 20 20   \.   CKLOCK,   
e1b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1b20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72    /* xCheckReser
e1b30 76 65 64 4c 6f 63 6b 20 2a 2f 20 20 20 20 20 20  vedLock */      
e1b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1b50 5c 0a 20 20 20 75 6e 69 78 46 69 6c 65 43 6f 6e  \.   unixFileCon
e1b60 74 72 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20  trol,           
e1b70 20 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c   /* xFileControl
e1b80 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
e1b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
e1ba0 0a 20 20 20 75 6e 69 78 53 65 63 74 6f 72 53 69  .   unixSectorSi
e1bb0 7a 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ze,             
e1bc0 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a  /* xSectorSize *
e1bd0 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
e1be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
e1bf0 20 20 20 75 6e 69 78 44 65 76 69 63 65 43 68 61     unixDeviceCha
e1c00 72 61 63 74 65 72 69 73 74 69 63 73 20 20 20 2f  racteristics   /
e1c10 2a 20 78 44 65 76 69 63 65 43 61 70 61 62 69 6c  * xDeviceCapabil
e1c20 69 74 69 65 73 20 2a 2f 20 20 20 20 20 20 20 20  ities */        
e1c30 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d               \.}
e1c40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
e1c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1c80 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 73 74              \.st
e1c90 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
e1ca0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 46  e3_io_methods *F
e1cb0 49 4e 44 45 52 23 23 49 6d 70 6c 28 63 6f 6e 73  INDER##Impl(cons
e1cc0 74 20 63 68 61 72 20 2a 7a 2c 20 75 6e 69 78 46  t char *z, unixF
e1cd0 69 6c 65 20 2a 70 29 7b 20 20 20 5c 0a 20 20 55  ile *p){   \.  U
e1ce0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
e1cf0 7a 29 3b 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  z); UNUSED_PARAM
e1d00 45 54 45 52 28 70 29 3b 20 20 20 20 20 20 20 20  ETER(p);        
e1d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1d20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 72 65            \.  re
e1d30 74 75 72 6e 20 26 4d 45 54 48 4f 44 3b 20 20 20  turn &METHOD;   
e1d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1d70 20 20 20 20 20 20 20 20 20 5c 0a 7d 20 20 20 20           \.}    
e1d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1dc0 20 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63          \.static
e1dd0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
e1de0 6f 5f 6d 65 74 68 6f 64 73 20 2a 28 2a 63 6f 6e  o_methods *(*con
e1df0 73 74 20 46 49 4e 44 45 52 29 28 63 6f 6e 73 74  st FINDER)(const
e1e00 20 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 20   char*,unixFile 
e1e10 2a 70 29 20 20 20 20 5c 0a 20 20 20 20 3d 20 46  *p)    \.    = F
e1e20 49 4e 44 45 52 23 23 49 6d 70 6c 3b 0a 0a 2f 2a  INDER##Impl;../*
e1e30 0a 2a 2a 20 48 65 72 65 20 61 72 65 20 61 6c 6c  .** Here are all
e1e40 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
e1e50 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63  io_methods objec
e1e60 74 73 20 66 6f 72 20 65 61 63 68 20 6f 66 20 74  ts for each of t
e1e70 68 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 73 74  he.** locking st
e1e80 72 61 74 65 67 69 65 73 2e 20 20 46 75 6e 63 74  rategies.  Funct
e1e90 69 6f 6e 73 20 74 68 61 74 20 72 65 74 75 72 6e  ions that return
e1ea0 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   pointers to the
e1eb0 73 65 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 61 72  se methods.** ar
e1ec0 65 20 61 6c 73 6f 20 63 72 65 61 74 65 64 2e 0a  e also created..
e1ed0 2a 2f 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20  */.IOMETHODS(.  
e1ee0 70 6f 73 69 78 49 6f 46 69 6e 64 65 72 2c 20 20  posixIoFinder,  
e1ef0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e            /* Fin
e1f00 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d  der function nam
e1f10 65 20 2a 2f 0a 20 20 70 6f 73 69 78 49 6f 4d 65  e */.  posixIoMe
e1f20 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20  thods,          
e1f30 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d   /* sqlite3_io_m
e1f40 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61  ethods object na
e1f50 6d 65 20 2a 2f 0a 20 20 75 6e 69 78 43 6c 6f 73  me */.  unixClos
e1f60 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
e1f70 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68    /* xClose meth
e1f80 6f 64 20 2a 2f 0a 20 20 75 6e 69 78 4c 6f 63 6b  od */.  unixLock
e1f90 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e1fa0 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f    /* xLock metho
e1fb0 64 20 2a 2f 0a 20 20 75 6e 69 78 55 6e 6c 6f 63  d */.  unixUnloc
e1fc0 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k,              
e1fd0 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68   /* xUnlock meth
e1fe0 6f 64 20 2a 2f 0a 20 20 75 6e 69 78 43 68 65 63  od */.  unixChec
e1ff0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20  kReservedLock   
e2000 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72    /* xCheckReser
e2010 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  vedLock method *
e2020 2f 0a 29 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20  /.).IOMETHODS(. 
e2030 20 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c   nolockIoFinder,
e2040 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
e2050 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61  nder function na
e2060 6d 65 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 49 6f  me */.  nolockIo
e2070 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20  Methods,        
e2080 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f    /* sqlite3_io_
e2090 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e  methods object n
e20a0 61 6d 65 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43  ame */.  nolockC
e20b0 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  lose,           
e20c0 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74     /* xClose met
e20d0 68 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 4c  hod */.  nolockL
e20e0 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20  ock,            
e20f0 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68     /* xLock meth
e2100 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 55 6e  od */.  nolockUn
e2110 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20  lock,           
e2120 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74    /* xUnlock met
e2130 68 6f 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43  hod */.  nolockC
e2140 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
e2150 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65     /* xCheckRese
e2160 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20  rvedLock method 
e2170 2a 2f 0a 29 0a 49 4f 4d 45 54 48 4f 44 53 28 0a  */.).IOMETHODS(.
e2180 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64 65    dotlockIoFinde
e2190 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  r,          /* F
e21a0 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e  inder function n
e21b0 61 6d 65 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b  ame */.  dotlock
e21c0 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20  IoMethods,      
e21d0 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f     /* sqlite3_io
e21e0 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20  _methods object 
e21f0 6e 61 6d 65 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63  name */.  dotloc
e2200 6b 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20  kClose,         
e2210 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65      /* xClose me
e2220 74 68 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63  thod */.  dotloc
e2230 6b 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20  kLock,          
e2240 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74      /* xLock met
e2250 68 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b  hod */.  dotlock
e2260 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20  Unlock,         
e2270 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65     /* xUnlock me
e2280 74 68 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63  thod */.  dotloc
e2290 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  kCheckReservedLo
e22a0 63 6b 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73  ck  /* xCheckRes
e22b0 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64  ervedLock method
e22c0 20 2a 2f 0a 29 0a 0a 23 69 66 20 53 51 4c 49 54   */.)..#if SQLIT
e22d0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
e22e0 5f 53 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 58  _STYLE && !OS_VX
e22f0 57 4f 52 4b 53 0a 49 4f 4d 45 54 48 4f 44 53 28  WORKS.IOMETHODS(
e2300 0a 20 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 72  .  flockIoFinder
e2310 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
e2320 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20  Finder function 
e2330 6e 61 6d 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b 49  name */.  flockI
e2340 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20  oMethods,       
e2350 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69      /* sqlite3_i
e2360 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  o_methods object
e2370 20 6e 61 6d 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b   name */.  flock
e2380 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20  Close,          
e2390 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d       /* xClose m
e23a0 65 74 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b  ethod */.  flock
e23b0 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20  Lock,           
e23c0 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65       /* xLock me
e23d0 74 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 55  thod */.  flockU
e23e0 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20  nlock,          
e23f0 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d      /* xUnlock m
e2400 65 74 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b  ethod */.  flock
e2410 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
e2420 6b 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65  k    /* xCheckRe
e2430 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f  servedLock metho
e2440 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 23  d */.).#endif..#
e2450 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 49 4f  if OS_VXWORKS.IO
e2460 4d 45 54 48 4f 44 53 28 0a 20 20 73 65 6d 49 6f  METHODS(.  semIo
e2470 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20  Finder,         
e2480 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66       /* Finder f
e2490 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a  unction name */.
e24a0 20 20 73 65 6d 49 6f 4d 65 74 68 6f 64 73 2c 20    semIoMethods, 
e24b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
e24c0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e24d0 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f  s object name */
e24e0 0a 20 20 73 65 6d 43 6c 6f 73 65 2c 20 20 20 20  .  semClose,    
e24f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e2500 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f  xClose method */
e2510 0a 20 20 73 65 6d 4c 6f 63 6b 2c 20 20 20 20 20  .  semLock,     
e2520 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e2530 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  xLock method */.
e2540 20 20 73 65 6d 55 6e 6c 6f 63 6b 2c 20 20 20 20    semUnlock,    
e2550 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
e2560 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f  Unlock method */
e2570 0a 20 20 73 65 6d 43 68 65 63 6b 52 65 73 65 72  .  semCheckReser
e2580 76 65 64 4c 6f 63 6b 20 20 20 20 20 20 2f 2a 20  vedLock      /* 
e2590 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
e25a0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23  ck method */.).#
e25b0 65 6e 64 69 66 0a 0a 23 69 66 20 64 65 66 69 6e  endif..#if defin
e25c0 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26  ed(__APPLE__) &&
e25d0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
e25e0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 49 4f 4d  OCKING_STYLE.IOM
e25f0 45 54 48 4f 44 53 28 0a 20 20 61 66 70 49 6f 46  ETHODS(.  afpIoF
e2600 69 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20  inder,          
e2610 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75      /* Finder fu
e2620 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20  nction name */. 
e2630 20 61 66 70 49 6f 4d 65 74 68 6f 64 73 2c 20 20   afpIoMethods,  
e2640 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71             /* sq
e2650 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
e2660 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a   object name */.
e2670 20 20 61 66 70 43 6c 6f 73 65 2c 20 20 20 20 20    afpClose,     
e2680 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
e2690 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a  Close method */.
e26a0 20 20 61 66 70 4c 6f 63 6b 2c 20 20 20 20 20 20    afpLock,      
e26b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
e26c0 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20  Lock method */. 
e26d0 20 61 66 70 55 6e 6c 6f 63 6b 2c 20 20 20 20 20   afpUnlock,     
e26e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55             /* xU
e26f0 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  nlock method */.
e2700 20 20 61 66 70 43 68 65 63 6b 52 65 73 65 72 76    afpCheckReserv
e2710 65 64 4c 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78  edLock      /* x
e2720 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
e2730 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65  k method */.).#e
e2740 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
e2750 22 57 68 6f 6c 65 20 46 69 6c 65 20 4c 6f 63 6b  "Whole File Lock
e2760 69 6e 67 22 20 66 69 6e 64 65 72 20 72 65 74 75  ing" finder retu
e2770 72 6e 73 20 74 68 65 20 73 61 6d 65 20 73 65 74  rns the same set
e2780 20 6f 66 20 6d 65 74 68 6f 64 73 20 61 73 0a 2a   of methods as.*
e2790 2a 20 74 68 65 20 70 6f 73 69 78 20 6c 6f 63 6b  * the posix lock
e27a0 69 6e 67 20 66 69 6e 64 65 72 2e 20 20 42 75 74  ing finder.  But
e27b0 20 69 74 20 61 6c 73 6f 20 73 65 74 73 20 74 68   it also sets th
e27c0 65 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46  e SQLITE_WHOLE_F
e27d0 49 4c 45 5f 4c 4f 43 4b 49 4e 47 0a 2a 2a 20 66  ILE_LOCKING.** f
e27e0 6c 61 67 20 74 6f 20 66 6f 72 63 65 20 74 68 65  lag to force the
e27f0 20 70 6f 73 69 78 20 61 64 76 69 73 6f 72 79 20   posix advisory 
e2800 6c 6f 63 6b 73 20 74 6f 20 63 6f 76 65 72 20 74  locks to cover t
e2810 68 65 20 77 68 6f 6c 65 20 66 69 6c 65 20 69 6e  he whole file in
e2820 73 74 65 61 64 0a 2a 2a 20 6f 66 20 6a 75 73 74  stead.** of just
e2830 20 61 20 73 6d 61 6c 6c 20 73 70 61 6e 20 6f 66   a small span of
e2840 20 62 79 74 65 73 20 6e 65 61 72 20 74 68 65 20   bytes near the 
e2850 31 47 69 42 20 62 6f 75 6e 64 61 72 79 2e 20 20  1GiB boundary.  
e2860 57 68 6f 6c 65 20 46 69 6c 65 20 4c 6f 63 6b 69  Whole File Locki
e2870 6e 67 0a 2a 2a 20 69 73 20 75 73 65 66 75 6c 20  ng.** is useful 
e2880 6f 6e 20 4e 46 53 2d 6d 6f 75 6e 74 65 64 20 66  on NFS-mounted f
e2890 69 6c 65 73 20 73 69 6e 63 65 20 69 74 20 68 65  iles since it he
e28a0 6c 70 73 20 4e 46 53 20 74 6f 20 6d 61 69 6e 74  lps NFS to maint
e28b0 61 69 6e 20 63 61 63 68 65 0a 2a 2a 20 63 6f 68  ain cache.** coh
e28c0 65 72 65 6e 63 79 2e 20 20 42 75 74 20 69 74 20  erency.  But it 
e28d0 69 73 20 61 20 64 65 74 72 69 6d 65 6e 74 20 74  is a detriment t
e28e0 6f 20 6f 74 68 65 72 20 66 69 6c 65 73 79 73 74  o other filesyst
e28f0 65 6d 73 20 73 69 6e 63 65 20 69 74 20 72 75 6e  ems since it run
e2900 73 0a 2a 2a 20 73 6c 6f 77 65 72 2e 0a 2a 2f 0a  s.** slower..*/.
e2910 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
e2920 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
e2930 2a 70 6f 73 69 78 57 66 6c 49 6f 46 69 6e 64 65  *posixWflIoFinde
e2940 72 49 6d 70 6c 28 63 6f 6e 73 74 20 63 68 61 72  rImpl(const char
e2950 2a 7a 2c 20 75 6e 69 78 46 69 6c 65 2a 70 29 7b  *z, unixFile*p){
e2960 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
e2970 54 45 52 28 7a 29 3b 0a 20 20 70 2d 3e 66 69 6c  TER(z);.  p->fil
e2980 65 46 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f  eFlags = SQLITE_
e2990 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49  WHOLE_FILE_LOCKI
e29a0 4e 47 3b 0a 20 20 72 65 74 75 72 6e 20 26 70 6f  NG;.  return &po
e29b0 73 69 78 49 6f 4d 65 74 68 6f 64 73 3b 0a 7d 0a  sixIoMethods;.}.
e29c0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
e29d0 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
e29e0 0a 20 20 2a 28 2a 63 6f 6e 73 74 20 70 6f 73 69  .  *(*const posi
e29f0 78 57 66 6c 49 6f 46 69 6e 64 65 72 29 28 63 6f  xWflIoFinder)(co
e2a00 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69 78 46 69  nst char*,unixFi
e2a10 6c 65 20 2a 70 29 20 3d 20 70 6f 73 69 78 57 66  le *p) = posixWf
e2a20 6c 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b 0a 0a  lIoFinderImpl;..
e2a30 2f 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20  /*.** The proxy 
e2a40 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 69  locking method i
e2a50 73 20 61 20 22 73 75 70 65 72 2d 6d 65 74 68 6f  s a "super-metho
e2a60 64 22 20 69 6e 20 74 68 65 20 73 65 6e 73 65 20  d" in the sense 
e2a70 74 68 61 74 20 69 74 0a 2a 2a 20 6f 70 65 6e 73  that it.** opens
e2a80 20 73 65 63 6f 6e 64 61 72 79 20 66 69 6c 65 20   secondary file 
e2a90 64 65 73 63 72 69 70 74 6f 72 73 20 66 6f 72 20  descriptors for 
e2aa0 74 68 65 20 63 6f 6e 63 68 20 61 6e 64 20 6c 6f  the conch and lo
e2ab0 63 6b 20 66 69 6c 65 73 20 61 6e 64 0a 2a 2a 20  ck files and.** 
e2ac0 69 74 20 75 73 65 73 20 70 72 6f 78 79 2c 20 64  it uses proxy, d
e2ad0 6f 74 2d 66 69 6c 65 2c 20 41 46 50 2c 20 61 6e  ot-file, AFP, an
e2ae0 64 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e  d flock() lockin
e2af0 67 20 6d 65 74 68 6f 64 73 20 6f 6e 20 74 68 6f  g methods on tho
e2b00 73 65 0a 2a 2a 20 73 65 63 6f 6e 64 61 72 79 20  se.** secondary 
e2b10 66 69 6c 65 73 2e 20 20 46 6f 72 20 74 68 69 73  files.  For this
e2b20 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76   reason, the div
e2b30 69 73 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65  ision that imple
e2b40 6d 65 6e 74 73 0a 2a 2a 20 70 72 6f 78 79 20 6c  ments.** proxy l
e2b50 6f 63 6b 69 6e 67 20 69 73 20 6c 6f 63 61 74 65  ocking is locate
e2b60 64 20 6d 75 63 68 20 66 75 72 74 68 65 72 20 64  d much further d
e2b70 6f 77 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 2e  own in the file.
e2b80 20 20 42 75 74 20 77 65 20 6e 65 65 64 0a 2a 2a    But we need.**
e2b90 20 74 6f 20 67 6f 20 61 68 65 61 64 20 61 6e 64   to go ahead and
e2ba0 20 64 65 66 69 6e 65 20 74 68 65 20 73 71 6c 69   define the sqli
e2bb0 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 61  te3_io_methods a
e2bc0 6e 64 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69  nd finder functi
e2bd0 6f 6e 0a 2a 2a 20 66 6f 72 20 70 72 6f 78 79 20  on.** for proxy 
e2be0 6c 6f 63 6b 69 6e 67 20 68 65 72 65 2e 20 20 53  locking here.  S
e2bf0 6f 20 77 65 20 66 6f 72 77 61 72 64 20 64 65 63  o we forward dec
e2c00 6c 61 72 65 20 74 68 65 20 49 2f 4f 20 6d 65 74  lare the I/O met
e2c10 68 6f 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66  hods..*/.#if def
e2c20 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20  ined(__APPLE__) 
e2c30 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  && SQLITE_ENABLE
e2c40 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 73  _LOCKING_STYLE.s
e2c50 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43  tatic int proxyC
e2c60 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  lose(sqlite3_fil
e2c70 65 2a 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20  e*);.static int 
e2c80 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74 65  proxyLock(sqlite
e2c90 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73  3_file*, int);.s
e2ca0 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 55  tatic int proxyU
e2cb0 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  nlock(sqlite3_fi
e2cc0 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74 69  le*, int);.stati
e2cd0 63 20 69 6e 74 20 70 72 6f 78 79 43 68 65 63 6b  c int proxyCheck
e2ce0 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c  ReservedLock(sql
e2cf0 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2a  ite3_file*, int*
e2d00 29 3b 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20  );.IOMETHODS(.  
e2d10 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 2c 20 20  proxyIoFinder,  
e2d20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e            /* Fin
e2d30 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d  der function nam
e2d40 65 20 2a 2f 0a 20 20 70 72 6f 78 79 49 6f 4d 65  e */.  proxyIoMe
e2d50 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20  thods,          
e2d60 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d   /* sqlite3_io_m
e2d70 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61  ethods object na
e2d80 6d 65 20 2a 2f 0a 20 20 70 72 6f 78 79 43 6c 6f  me */.  proxyClo
e2d90 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
e2da0 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68    /* xClose meth
e2db0 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79 4c 6f 63  od */.  proxyLoc
e2dc0 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k,              
e2dd0 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f    /* xLock metho
e2de0 64 20 2a 2f 0a 20 20 70 72 6f 78 79 55 6e 6c 6f  d */.  proxyUnlo
e2df0 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ck,             
e2e00 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68   /* xUnlock meth
e2e10 6f 64 20 2a 2f 0a 20 20 70 72 6f 78 79 43 68 65  od */.  proxyChe
e2e20 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20  ckReservedLock  
e2e30 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72    /* xCheckReser
e2e40 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  vedLock method *
e2e50 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66  /.).#endif...#if
e2e60 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45   defined(__APPLE
e2e70 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e  __) && SQLITE_EN
e2e80 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
e2e90 4c 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22  LE./* .** This "
e2ea0 66 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e  finder" function
e2eb0 20 61 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74   attempts to det
e2ec0 65 72 6d 69 6e 65 20 74 68 65 20 62 65 73 74 20  ermine the best 
e2ed0 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79  locking strategy
e2ee0 20 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 74   .** for the dat
e2ef0 61 62 61 73 65 20 66 69 6c 65 20 22 66 69 6c 65  abase file "file
e2f00 50 61 74 68 22 2e 20 20 49 74 20 74 68 65 6e 20  Path".  It then 
e2f10 72 65 74 75 72 6e 73 20 74 68 65 20 73 71 6c 69  returns the sqli
e2f20 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a  te3_io_methods.*
e2f30 2a 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d  * object that im
e2f40 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 73 74  plements that st
e2f50 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  rategy..**.** Th
e2f60 69 73 20 69 73 20 66 6f 72 20 4d 61 63 4f 53 58  is is for MacOSX
e2f70 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63   only..*/.static
e2f80 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
e2f90 6f 5f 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f 6c  o_methods *autol
e2fa0 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 28  ockIoFinderImpl(
e2fb0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66  .  const char *f
e2fc0 69 6c 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 6e  ilePath,    /* n
e2fd0 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
e2fe0 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e  ase file */.  un
e2ff0 69 78 46 69 6c 65 20 2a 70 4e 65 77 20 20 20 20  ixFile *pNew    
e3000 20 20 20 20 20 20 20 2f 2a 20 6f 70 65 6e 20 66         /* open f
e3010 69 6c 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 74  ile object for t
e3020 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
e3030 20 2a 2f 0a 29 7b 0a 20 20 73 74 61 74 69 63 20   */.){.  static 
e3040 63 6f 6e 73 74 20 73 74 72 75 63 74 20 4d 61 70  const struct Map
e3050 70 69 6e 67 20 7b 0a 20 20 20 20 63 6f 6e 73 74  ping {.    const
e3060 20 63 68 61 72 20 2a 7a 46 69 6c 65 73 79 73 74   char *zFilesyst
e3070 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  em;             
e3080 20 2f 2a 20 46 69 6c 65 73 79 73 74 65 6d 20 74   /* Filesystem t
e3090 79 70 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20  ype name */.    
e30a0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f  const sqlite3_io
e30b0 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f  _methods *pMetho
e30c0 64 73 3b 20 20 20 2f 2a 20 41 70 70 72 6f 70 72  ds;   /* Appropr
e30d0 69 61 74 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74  iate locking met
e30e0 68 6f 64 20 2a 2f 0a 20 20 7d 20 61 4d 61 70 5b  hod */.  } aMap[
e30f0 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 68 66 73  ] = {.    { "hfs
e3100 22 2c 20 20 20 20 26 70 6f 73 69 78 49 6f 4d 65  ",    &posixIoMe
e3110 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 22  thods },.    { "
e3120 75 66 73 22 2c 20 20 20 20 26 70 6f 73 69 78 49  ufs",    &posixI
e3130 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20  oMethods },.    
e3140 7b 20 22 61 66 70 66 73 22 2c 20 20 26 61 66 70  { "afpfs",  &afp
e3150 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 69 66  IoMethods },.#if
e3160 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
e3170 45 5f 41 46 50 5f 4c 4f 43 4b 49 4e 47 5f 53 4d  E_AFP_LOCKING_SM
e3180 42 0a 20 20 20 20 7b 20 22 73 6d 62 66 73 22 2c  B.    { "smbfs",
e3190 20 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20    &afpIoMethods 
e31a0 7d 2c 0a 23 65 6c 73 65 0a 20 20 20 20 7b 20 22  },.#else.    { "
e31b0 73 6d 62 66 73 22 2c 20 20 26 66 6c 6f 63 6b 49  smbfs",  &flockI
e31c0 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 65 6e 64  oMethods },.#end
e31d0 69 66 0a 20 20 20 20 7b 20 22 77 65 62 64 61 76  if.    { "webdav
e31e0 22 2c 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68  ", &nolockIoMeth
e31f0 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 30 2c 20  ods },.    { 0, 
e3200 30 20 7d 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69  0 }.  };.  int i
e3210 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 66  ;.  struct statf
e3220 73 20 66 73 49 6e 66 6f 3b 0a 20 20 73 74 72 75  s fsInfo;.  stru
e3230 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 49 6e 66  ct flock lockInf
e3240 6f 3b 0a 0a 20 20 69 66 28 20 21 66 69 6c 65 50  o;..  if( !fileP
e3250 61 74 68 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  ath ){.    /* If
e3260 20 66 69 6c 65 50 61 74 68 3d 3d 4e 55 4c 4c 20   filePath==NULL 
e3270 74 68 61 74 20 6d 65 61 6e 73 20 77 65 20 61 72  that means we ar
e3280 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61  e dealing with a
e3290 20 74 72 61 6e 73 69 65 6e 74 20 66 69 6c 65 0a   transient file.
e32a0 20 20 20 20 2a 2a 20 74 68 61 74 20 64 6f 65 73      ** that does
e32b0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20   not need to be 
e32c0 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 72  locked. */.    r
e32d0 65 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d  eturn &nolockIoM
e32e0 65 74 68 6f 64 73 3b 0a 20 20 7d 0a 20 20 69 66  ethods;.  }.  if
e32f0 28 20 73 74 61 74 66 73 28 66 69 6c 65 50 61 74  ( statfs(filePat
e3300 68 2c 20 26 66 73 49 6e 66 6f 29 20 21 3d 20 2d  h, &fsInfo) != -
e3310 31 20 29 7b 0a 20 20 20 20 69 66 28 20 66 73 49  1 ){.    if( fsI
e3320 6e 66 6f 2e 66 5f 66 6c 61 67 73 20 26 20 4d 4e  nfo.f_flags & MN
e3330 54 5f 52 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20  T_RDONLY ){.    
e3340 20 20 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b    return &nolock
e3350 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 20 20 7d  IoMethods;.    }
e3360 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 61 4d  .    for(i=0; aM
e3370 61 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 73 74 65  ap[i].zFilesyste
e3380 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  m; i++){.      i
e3390 66 28 20 73 74 72 63 6d 70 28 66 73 49 6e 66 6f  f( strcmp(fsInfo
e33a0 2e 66 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20 61  .f_fstypename, a
e33b0 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 73 74  Map[i].zFilesyst
e33c0 65 6d 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  em)==0 ){.      
e33d0 20 20 72 65 74 75 72 6e 20 61 4d 61 70 5b 69 5d    return aMap[i]
e33e0 2e 70 4d 65 74 68 6f 64 73 3b 0a 20 20 20 20 20  .pMethods;.     
e33f0 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
e3400 2f 2a 20 44 65 66 61 75 6c 74 20 63 61 73 65 2e  /* Default case.
e3410 20 48 61 6e 64 6c 65 73 2c 20 61 6d 6f 6e 67 73   Handles, amongs
e3420 74 20 6f 74 68 65 72 73 2c 20 22 6e 66 73 22 2e  t others, "nfs".
e3430 0a 20 20 2a 2a 20 54 65 73 74 20 62 79 74 65 2d  .  ** Test byte-
e3440 72 61 6e 67 65 20 6c 6f 63 6b 20 75 73 69 6e 67  range lock using
e3450 20 66 63 6e 74 6c 28 29 2e 20 49 66 20 74 68 65   fcntl(). If the
e3460 20 63 61 6c 6c 20 73 75 63 63 65 65 64 73 2c 20   call succeeds, 
e3470 0a 20 20 2a 2a 20 61 73 73 75 6d 65 20 74 68 61  .  ** assume tha
e3480 74 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65  t the file-syste
e3490 6d 20 73 75 70 70 6f 72 74 73 20 50 4f 53 49 58  m supports POSIX
e34a0 20 73 74 79 6c 65 20 6c 6f 63 6b 73 2e 20 0a 20   style locks. . 
e34b0 20 2a 2f 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c   */.  lockInfo.l
e34c0 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b  _len = 1;.  lock
e34d0 49 6e 66 6f 2e 6c 5f 73 74 61 72 74 20 3d 20 30  Info.l_start = 0
e34e0 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77  ;.  lockInfo.l_w
e34f0 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54  hence = SEEK_SET
e3500 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74  ;.  lockInfo.l_t
e3510 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20  ype = F_RDLCK;. 
e3520 20 69 66 28 20 66 63 6e 74 6c 28 70 4e 65 77 2d   if( fcntl(pNew-
e3530 3e 68 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f  >h, F_GETLK, &lo
e3540 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a  ckInfo)!=-1 ) {.
e3550 20 20 20 20 70 4e 65 77 2d 3e 66 69 6c 65 46 6c      pNew->fileFl
e3560 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 57 48 4f  ags = SQLITE_WHO
e3570 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3b  LE_FILE_LOCKING;
e3580 0a 20 20 20 20 72 65 74 75 72 6e 20 26 70 6f 73  .    return &pos
e3590 69 78 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d  ixIoMethods;.  }
e35a0 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
e35b0 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f   &dotlockIoMetho
e35c0 64 73 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63  ds;.  }.}.static
e35d0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
e35e0 6f 5f 6d 65 74 68 6f 64 73 20 0a 20 20 2a 28 2a  o_methods .  *(*
e35f0 63 6f 6e 73 74 20 61 75 74 6f 6c 6f 63 6b 49 6f  const autolockIo
e3600 46 69 6e 64 65 72 29 28 63 6f 6e 73 74 20 63 68  Finder)(const ch
e3610 61 72 2a 2c 75 6e 69 78 46 69 6c 65 2a 29 20 3d  ar*,unixFile*) =
e3620 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65   autolockIoFinde
e3630 72 49 6d 70 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f  rImpl;..#endif /
e3640 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c  * defined(__APPL
e3650 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45  E__) && SQLITE_E
e3660 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
e3670 59 4c 45 20 2a 2f 0a 0a 23 69 66 20 4f 53 5f 56  YLE */..#if OS_V
e3680 58 57 4f 52 4b 53 20 26 26 20 53 51 4c 49 54 45  XWORKS && SQLITE
e3690 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
e36a0 53 54 59 4c 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69  STYLE./* .** Thi
e36b0 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74  s "finder" funct
e36c0 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ion attempts to 
e36d0 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 62 65  determine the be
e36e0 73 74 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74  st locking strat
e36f0 65 67 79 20 0a 2a 2a 20 66 6f 72 20 74 68 65 20  egy .** for the 
e3700 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 22 66  database file "f
e3710 69 6c 65 50 61 74 68 22 2e 20 20 49 74 20 74 68  ilePath".  It th
e3720 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 73  en returns the s
e3730 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e3740 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 68 61 74  s.** object that
e3750 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 61 74   implements that
e3760 20 73 74 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a   strategy..**.**
e3770 20 54 68 69 73 20 69 73 20 66 6f 72 20 56 58 57   This is for VXW
e3780 6f 72 6b 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74  orks only..*/.st
e3790 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
e37a0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 61  e3_io_methods *a
e37b0 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49  utolockIoFinderI
e37c0 6d 70 6c 28 0a 20 20 63 6f 6e 73 74 20 63 68 61  mpl(.  const cha
e37d0 72 20 2a 66 69 6c 65 50 61 74 68 2c 20 20 20 20  r *filePath,    
e37e0 2f 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64  /* name of the d
e37f0 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
e3800 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77    unixFile *pNew
e3810 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68             /* th
e3820 65 20 6f 70 65 6e 20 66 69 6c 65 20 6f 62 6a 65  e open file obje
e3830 63 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63  ct */.){.  struc
e3840 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f  t flock lockInfo
e3850 3b 0a 0a 20 20 69 66 28 20 21 66 69 6c 65 50 61  ;..  if( !filePa
e3860 74 68 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  th ){.    /* If 
e3870 66 69 6c 65 50 61 74 68 3d 3d 4e 55 4c 4c 20 74  filePath==NULL t
e3880 68 61 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65  hat means we are
e3890 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20   dealing with a 
e38a0 74 72 61 6e 73 69 65 6e 74 20 66 69 6c 65 0a 20  transient file. 
e38b0 20 20 20 2a 2a 20 74 68 61 74 20 64 6f 65 73 20     ** that does 
e38c0 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6c  not need to be l
e38d0 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 72 65  ocked. */.    re
e38e0 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65  turn &nolockIoMe
e38f0 74 68 6f 64 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  thods;.  }..  /*
e3900 20 54 65 73 74 20 69 66 20 66 63 6e 74 6c 28 29   Test if fcntl()
e3910 20 69 73 20 73 75 70 70 6f 72 74 65 64 20 61 6e   is supported an
e3920 64 20 75 73 65 20 50 4f 53 49 58 20 73 74 79 6c  d use POSIX styl
e3930 65 20 6c 6f 63 6b 73 2e 0a 20 20 2a 2a 20 4f 74  e locks..  ** Ot
e3940 68 65 72 77 69 73 65 20 66 61 6c 6c 20 62 61 63  herwise fall bac
e3950 6b 20 74 6f 20 74 68 65 20 6e 61 6d 65 64 20 73  k to the named s
e3960 65 6d 61 70 68 6f 72 65 20 6d 65 74 68 6f 64 2e  emaphore method.
e3970 0a 20 20 2a 2f 0a 20 20 6c 6f 63 6b 49 6e 66 6f  .  */.  lockInfo
e3980 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 6c 6f  .l_len = 1;.  lo
e3990 63 6b 49 6e 66 6f 2e 6c 5f 73 74 61 72 74 20 3d  ckInfo.l_start =
e39a0 20 30 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c   0;.  lockInfo.l
e39b0 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53  _whence = SEEK_S
e39c0 45 54 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c  ET;.  lockInfo.l
e39d0 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b  _type = F_RDLCK;
e39e0 0a 20 20 69 66 28 20 66 63 6e 74 6c 28 70 4e 65  .  if( fcntl(pNe
e39f0 77 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b 2c 20 26  w->h, F_GETLK, &
e3a00 6c 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 20  lockInfo)!=-1 ) 
e3a10 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 70 6f  {.    return &po
e3a20 73 69 78 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20  sixIoMethods;.  
e3a30 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
e3a40 6e 20 26 73 65 6d 49 6f 4d 65 74 68 6f 64 73 3b  n &semIoMethods;
e3a50 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f  .  }.}.static co
e3a60 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  nst sqlite3_io_m
e3a70 65 74 68 6f 64 73 20 0a 20 20 2a 28 2a 63 6f 6e  ethods .  *(*con
e3a80 73 74 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e  st autolockIoFin
e3a90 64 65 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a  der)(const char*
e3aa0 2c 75 6e 69 78 46 69 6c 65 2a 29 20 3d 20 61 75  ,unixFile*) = au
e3ab0 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d  tolockIoFinderIm
e3ac0 70 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 4f  pl;..#endif /* O
e3ad0 53 5f 56 58 57 4f 52 4b 53 20 26 26 20 53 51 4c  S_VXWORKS && SQL
e3ae0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
e3af0 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a  NG_STYLE */../*.
e3b00 2a 2a 20 41 6e 20 61 62 73 74 72 61 63 74 20 74  ** An abstract t
e3b10 79 70 65 20 66 6f 72 20 61 20 70 6f 69 6e 74 65  ype for a pointe
e3b20 72 20 74 6f 20 61 20 49 4f 20 6d 65 74 68 6f 64  r to a IO method
e3b30 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e   finder function
e3b40 3a 0a 2a 2f 0a 74 79 70 65 64 65 66 20 63 6f 6e  :.*/.typedef con
e3b50 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  st sqlite3_io_me
e3b60 74 68 6f 64 73 20 2a 28 2a 66 69 6e 64 65 72 5f  thods *(*finder_
e3b70 74 79 70 65 29 28 63 6f 6e 73 74 20 63 68 61 72  type)(const char
e3b80 2a 2c 75 6e 69 78 46 69 6c 65 2a 29 3b 0a 0a 0a  *,unixFile*);...
e3b90 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
e3ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
e3be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74  ********** sqlit
e3c00 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 2a  e3_vfs methods *
e3c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
e3c30 2a 20 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20  * This division 
e3c40 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d 70  contains the imp
e3c50 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d  lementation of m
e3c60 65 74 68 6f 64 73 20 6f 6e 20 74 68 65 0a 2a 2a  ethods on the.**
e3c70 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a   sqlite3_vfs obj
e3c80 65 63 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49  ect..*/../*.** I
e3c90 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f  nitialize the co
e3ca0 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 75 6e  ntents of the un
e3cb0 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 65  ixFile structure
e3cc0 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70   pointed to by p
e3cd0 49 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  Id..*/.static in
e3ce0 74 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65  t fillInUnixFile
e3cf0 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  (.  sqlite3_vfs 
e3d00 2a 70 56 66 73 2c 20 20 20 20 20 20 2f 2a 20 50  *pVfs,      /* P
e3d10 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62  ointer to vfs ob
e3d20 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 68 2c  ject */.  int h,
e3d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3d40 20 20 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64    /* Open file d
e3d50 65 73 63 72 69 70 74 6f 72 20 6f 66 20 66 69 6c  escriptor of fil
e3d60 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a  e being opened *
e3d70 2f 0a 20 20 69 6e 74 20 64 69 72 66 64 2c 20 20  /.  int dirfd,  
e3d80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
e3d90 69 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65  irectory file de
e3da0 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71  scriptor */.  sq
e3db0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 49 64 2c  lite3_file *pId,
e3dc0 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
e3dd0 6f 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 73  o the unixFile s
e3de0 74 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f  tructure here */
e3df0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
e3e00 46 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61  Filename,  /* Na
e3e10 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 62  me of the file b
e3e20 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20  eing opened */. 
e3e30 20 69 6e 74 20 6e 6f 4c 6f 63 6b 2c 20 20 20 20   int noLock,    
e3e40 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6d 69 74           /* Omit
e3e50 20 6c 6f 63 6b 69 6e 67 20 69 66 20 74 72 75 65   locking if true
e3e60 20 2a 2f 0a 20 20 69 6e 74 20 69 73 44 65 6c 65   */.  int isDele
e3e70 74 65 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  te            /*
e3e80 20 44 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65   Delete on close
e3e90 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20   if true */.){. 
e3ea0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69   const sqlite3_i
e3eb0 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4c 6f 63 6b  o_methods *pLock
e3ec0 69 6e 67 53 74 79 6c 65 3b 0a 20 20 75 6e 69 78  ingStyle;.  unix
e3ed0 46 69 6c 65 20 2a 70 4e 65 77 20 3d 20 28 75 6e  File *pNew = (un
e3ee0 69 78 46 69 6c 65 20 2a 29 70 49 64 3b 0a 20 20  ixFile *)pId;.  
e3ef0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
e3f00 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  OK;..  assert( p
e3f10 4e 65 77 2d 3e 70 4c 6f 63 6b 3d 3d 4e 55 4c 4c  New->pLock==NULL
e3f20 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e   );.  assert( pN
e3f30 65 77 2d 3e 70 4f 70 65 6e 3d 3d 4e 55 4c 4c 20  ew->pOpen==NULL 
e3f40 29 3b 0a 0a 20 20 2f 2a 20 50 61 72 61 6d 65 74  );..  /* Paramet
e3f50 65 72 20 69 73 44 65 6c 65 74 65 20 69 73 20 6f  er isDelete is o
e3f60 6e 6c 79 20 75 73 65 64 20 6f 6e 20 76 78 77 6f  nly used on vxwo
e3f70 72 6b 73 2e 20 45 78 70 72 65 73 73 20 74 68 69  rks. Express thi
e3f80 73 20 65 78 70 6c 69 63 69 74 6c 79 20 0a 20 20  s explicitly .  
e3f90 2a 2a 20 68 65 72 65 20 74 6f 20 70 72 65 76 65  ** here to preve
e3fa0 6e 74 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e  nt compiler warn
e3fb0 69 6e 67 73 20 61 62 6f 75 74 20 75 6e 75 73 65  ings about unuse
e3fc0 64 20 70 61 72 61 6d 65 74 65 72 73 2e 0a 20 20  d parameters..  
e3fd0 2a 2f 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  */.  UNUSED_PARA
e3fe0 4d 45 54 45 52 28 69 73 44 65 6c 65 74 65 29 3b  METER(isDelete);
e3ff0 0a 0a 20 20 4f 53 54 52 41 43 45 33 28 22 4f 50  ..  OSTRACE3("OP
e4000 45 4e 20 20 20 20 25 2d 33 64 20 25 73 5c 6e 22  EN    %-3d %s\n"
e4010 2c 20 68 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b  , h, zFilename);
e4020 20 20 20 20 0a 20 20 70 4e 65 77 2d 3e 68 20 3d      .  pNew->h =
e4030 20 68 3b 0a 20 20 70 4e 65 77 2d 3e 64 69 72 66   h;.  pNew->dirf
e4040 64 20 3d 20 64 69 72 66 64 3b 0a 20 20 53 45 54  d = dirfd;.  SET
e4050 5f 54 48 52 45 41 44 49 44 28 70 4e 65 77 29 3b  _THREADID(pNew);
e4060 0a 20 20 70 4e 65 77 2d 3e 66 69 6c 65 46 6c 61  .  pNew->fileFla
e4070 67 73 20 3d 20 30 3b 0a 0a 23 69 66 20 4f 53 5f  gs = 0;..#if OS_
e4080 56 58 57 4f 52 4b 53 0a 20 20 70 4e 65 77 2d 3e  VXWORKS.  pNew->
e4090 70 49 64 20 3d 20 76 78 77 6f 72 6b 73 46 69 6e  pId = vxworksFin
e40a0 64 46 69 6c 65 49 64 28 7a 46 69 6c 65 6e 61 6d  dFileId(zFilenam
e40b0 65 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e  e);.  if( pNew->
e40c0 70 49 64 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 6f  pId==0 ){.    no
e40d0 4c 6f 63 6b 20 3d 20 31 3b 0a 20 20 20 20 72 63  Lock = 1;.    rc
e40e0 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
e40f0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69  .  }.#endif..  i
e4100 66 28 20 6e 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20  f( noLock ){.   
e4110 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d   pLockingStyle =
e4120 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64   &nolockIoMethod
e4130 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  s;.  }else{.    
e4140 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20  pLockingStyle = 
e4150 28 2a 2a 28 66 69 6e 64 65 72 5f 74 79 70 65 2a  (**(finder_type*
e4160 29 70 56 66 73 2d 3e 70 41 70 70 44 61 74 61 29  )pVfs->pAppData)
e4170 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 70 4e 65 77  (zFilename, pNew
e4180 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  );.#if SQLITE_EN
e4190 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
e41a0 4c 45 0a 20 20 20 20 2f 2a 20 43 61 63 68 65 20  LE.    /* Cache 
e41b0 7a 46 69 6c 65 6e 61 6d 65 20 69 6e 20 74 68 65  zFilename in the
e41c0 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74   locking context
e41d0 20 28 41 46 50 20 61 6e 64 20 64 6f 74 6c 6f 63   (AFP and dotloc
e41e0 6b 20 6f 76 65 72 72 69 64 65 29 20 66 6f 72 0a  k override) for.
e41f0 20 20 20 20 2a 2a 20 70 72 6f 78 79 4c 6f 63 6b      ** proxyLock
e4200 20 61 63 74 69 76 61 74 69 6f 6e 20 69 73 20 70   activation is p
e4210 6f 73 73 69 62 6c 65 20 28 72 65 6d 6f 74 65 20  ossible (remote 
e4220 70 72 6f 78 79 20 69 73 20 62 61 73 65 64 20 6f  proxy is based o
e4230 6e 20 64 62 20 6e 61 6d 65 29 0a 20 20 20 20 2a  n db name).    *
e4240 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 72 65 6d 61  * zFilename rema
e4250 69 6e 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20  ins valid until 
e4260 66 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 2c 20  file is closed, 
e4270 74 6f 20 73 75 70 70 6f 72 74 20 2a 2f 0a 20 20  to support */.  
e4280 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43    pNew->lockingC
e4290 6f 6e 74 65 78 74 20 3d 20 28 76 6f 69 64 2a 29  ontext = (void*)
e42a0 7a 46 69 6c 65 6e 61 6d 65 3b 0a 23 65 6e 64 69  zFilename;.#endi
e42b0 66 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4c 6f  f.  }..  if( pLo
e42c0 63 6b 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 70  ckingStyle == &p
e42d0 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73 20 29 7b  osixIoMethods ){
e42e0 0a 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75  .    unixEnterMu
e42f0 74 65 78 28 29 3b 0a 20 20 20 20 72 63 20 3d 20  tex();.    rc = 
e4300 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65  findLockInfo(pNe
e4310 77 2c 20 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c  w, &pNew->pLock,
e4320 20 26 70 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a   &pNew->pOpen);.
e4330 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
e4340 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f  TE_OK ){.      /
e4350 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
e4360 63 75 72 65 64 20 69 6e 20 66 69 6e 64 4c 6f 63  cured in findLoc
e4370 6b 49 6e 66 6f 28 29 2c 20 63 6c 6f 73 65 20 74  kInfo(), close t
e4380 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
e4390 6f 72 0a 20 20 20 20 20 20 2a 2a 20 69 6d 6d 65  or.      ** imme
e43a0 64 69 61 74 65 6c 79 2c 20 62 65 66 6f 72 65 20  diately, before 
e43b0 72 65 6c 65 61 73 69 6e 67 20 74 68 65 20 6d 75  releasing the mu
e43c0 74 65 78 2e 20 66 69 6e 64 4c 6f 63 6b 49 6e 66  tex. findLockInf
e43d0 6f 28 29 20 6d 61 79 20 66 61 69 6c 0a 20 20 20  o() may fail.   
e43e0 20 20 20 2a 2a 20 69 6e 20 74 77 6f 20 73 63 65     ** in two sce
e43f0 6e 61 72 69 6f 73 3a 0a 20 20 20 20 20 20 2a 2a  narios:.      **
e4400 0a 20 20 20 20 20 20 2a 2a 20 20 20 28 61 29 20  .      **   (a) 
e4410 41 20 63 61 6c 6c 20 74 6f 20 66 73 74 61 74 28  A call to fstat(
e4420 29 20 66 61 69 6c 65 64 2e 0a 20 20 20 20 20 20  ) failed..      
e4430 2a 2a 20 20 20 28 62 29 20 41 20 6d 61 6c 6c 6f  **   (b) A mallo
e4440 63 20 66 61 69 6c 65 64 2e 0a 20 20 20 20 20 20  c failed..      
e4450 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 53 63 65 6e  **.      ** Scen
e4460 61 72 69 6f 20 28 62 29 20 6d 61 79 20 6f 6e 6c  ario (b) may onl
e4470 79 20 6f 63 63 75 72 20 69 66 20 74 68 65 20 70  y occur if the p
e4480 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e  rocess is holdin
e4490 67 20 6e 6f 20 6f 74 68 65 72 0a 20 20 20 20 20  g no other.     
e44a0 20 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70   ** file descrip
e44b0 74 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 65  tors open on the
e44c0 20 73 61 6d 65 20 66 69 6c 65 2e 20 49 66 20 74   same file. If t
e44d0 68 65 72 65 20 77 65 72 65 20 6f 74 68 65 72 20  here were other 
e44e0 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64 65  file.      ** de
e44f0 73 63 72 69 70 74 6f 72 73 20 6f 6e 20 74 68 69  scriptors on thi
e4500 73 20 66 69 6c 65 2c 20 74 68 65 6e 20 6e 6f 20  s file, then no 
e4510 6d 61 6c 6c 6f 63 20 77 6f 75 6c 64 20 62 65 20  malloc would be 
e4520 72 65 71 75 69 72 65 64 20 62 79 0a 20 20 20 20  required by.    
e4530 20 20 2a 2a 20 66 69 6e 64 4c 6f 63 6b 49 6e 66    ** findLockInf
e4540 6f 28 29 2e 20 49 66 20 74 68 69 73 20 69 73 20  o(). If this is 
e4550 74 68 65 20 63 61 73 65 2c 20 69 74 20 69 73 20  the case, it is 
e4560 71 75 69 74 65 20 73 61 66 65 20 74 6f 20 63 6c  quite safe to cl
e4570 6f 73 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 6e  ose.      ** han
e4580 64 6c 65 20 68 20 2d 20 61 73 20 69 74 20 69 73  dle h - as it is
e4590 20 67 75 61 72 61 6e 74 65 65 64 20 74 68 61 74   guaranteed that
e45a0 20 6e 6f 20 70 6f 73 69 78 20 6c 6f 63 6b 73 20   no posix locks 
e45b0 77 69 6c 6c 20 62 65 20 72 65 6c 65 61 73 65 64  will be released
e45c0 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 64 6f 69  .      ** by doi
e45d0 6e 67 20 73 6f 2e 0a 20 20 20 20 20 20 2a 2a 0a  ng so..      **.
e45e0 20 20 20 20 20 20 2a 2a 20 49 66 20 73 63 65 6e        ** If scen
e45f0 61 72 69 6f 20 28 61 29 20 63 61 75 73 65 64 20  ario (a) caused 
e4600 74 68 65 20 65 72 72 6f 72 20 74 68 65 6e 20 74  the error then t
e4610 68 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 73 6f  hings are not so
e4620 20 73 61 66 65 2e 20 54 68 65 0a 20 20 20 20 20   safe. The.     
e4630 20 2a 2a 20 69 6d 70 6c 69 63 69 74 20 61 73 73   ** implicit ass
e4640 75 6d 70 74 69 6f 6e 20 68 65 72 65 20 69 73 20  umption here is 
e4650 74 68 61 74 20 69 66 20 66 73 74 61 74 28 29 20  that if fstat() 
e4660 66 61 69 6c 73 2c 20 74 68 69 6e 67 73 20 61 72  fails, things ar
e4670 65 20 69 6e 0a 20 20 20 20 20 20 2a 2a 20 73 75  e in.      ** su
e4680 63 68 20 62 61 64 20 73 68 61 70 65 20 74 68 61  ch bad shape tha
e4690 74 20 64 72 6f 70 70 69 6e 67 20 61 20 6c 6f 63  t dropping a loc
e46a0 6b 20 6f 72 20 74 77 6f 20 64 6f 65 73 6e 27 74  k or two doesn't
e46b0 20 6d 61 74 74 65 72 20 6d 75 63 68 2e 0a 20 20   matter much..  
e46c0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6c 6f      */.      clo
e46d0 73 65 28 68 29 3b 0a 20 20 20 20 20 20 68 20 3d  se(h);.      h =
e46e0 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75   -1;.    }.    u
e46f0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  nixLeaveMutex();
e4700 0a 20 20 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45  .  }..#if SQLITE
e4710 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
e4720 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64  STYLE && defined
e4730 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 65 6c  (__APPLE__).  el
e4740 73 65 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53  se if( pLockingS
e4750 74 79 6c 65 20 3d 3d 20 26 61 66 70 49 6f 4d 65  tyle == &afpIoMe
e4760 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20  thods ){.    /* 
e4770 41 46 50 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73  AFP locking uses
e4780 20 74 68 65 20 66 69 6c 65 20 70 61 74 68 20 73   the file path s
e4790 6f 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65  o it needs to be
e47a0 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 20 20 20   included in.   
e47b0 20 2a 2a 20 74 68 65 20 61 66 70 4c 6f 63 6b 69   ** the afpLocki
e47c0 6e 67 43 6f 6e 74 65 78 74 2e 0a 20 20 20 20 2a  ngContext..    *
e47d0 2f 0a 20 20 20 20 61 66 70 4c 6f 63 6b 69 6e 67  /.    afpLocking
e47e0 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 3b 0a 20  Context *pCtx;. 
e47f0 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67     pNew->locking
e4800 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78 20 3d  Context = pCtx =
e4810 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
e4820 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29 20 29   sizeof(*pCtx) )
e4830 3b 0a 20 20 20 20 69 66 28 20 70 43 74 78 3d 3d  ;.    if( pCtx==
e4840 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
e4850 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
e4860 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f    }else{.      /
e4870 2a 20 4e 42 3a 20 7a 46 69 6c 65 6e 61 6d 65 20  * NB: zFilename 
e4880 65 78 69 73 74 73 20 61 6e 64 20 72 65 6d 61 69  exists and remai
e4890 6e 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74  ns valid until t
e48a0 68 65 20 66 69 6c 65 20 69 73 20 63 6c 6f 73 65  he file is close
e48b0 64 0a 20 20 20 20 20 20 2a 2a 20 61 63 63 6f 72  d.      ** accor
e48c0 64 69 6e 67 20 74 6f 20 72 65 71 75 69 72 65 6d  ding to requirem
e48d0 65 6e 74 20 46 31 31 31 34 31 2e 20 20 53 6f 20  ent F11141.  So 
e48e0 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74  we do not need t
e48f0 6f 20 6d 61 6b 65 20 61 0a 20 20 20 20 20 20 2a  o make a.      *
e4900 2a 20 63 6f 70 79 20 6f 66 20 74 68 65 20 66 69  * copy of the fi
e4910 6c 65 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20  lename. */.     
e4920 20 70 43 74 78 2d 3e 64 62 50 61 74 68 20 3d 20   pCtx->dbPath = 
e4930 7a 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 20 20 20  zFilename;.     
e4940 20 73 72 61 6e 64 6f 6d 64 65 76 28 29 3b 0a 20   srandomdev();. 
e4950 20 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75       unixEnterMu
e4960 74 65 78 28 29 3b 0a 20 20 20 20 20 20 72 63 20  tex();.      rc 
e4970 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70  = findLockInfo(p
e4980 4e 65 77 2c 20 4e 55 4c 4c 2c 20 26 70 4e 65 77  New, NULL, &pNew
e4990 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20  ->pOpen);.      
e49a0 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29  unixLeaveMutex()
e49b0 3b 20 20 20 20 20 20 20 20 0a 20 20 20 20 7d 0a  ;        .    }.
e49c0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 65 6c    }.#endif..  el
e49d0 73 65 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53  se if( pLockingS
e49e0 74 79 6c 65 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b  tyle == &dotlock
e49f0 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20  IoMethods ){.   
e4a00 20 2f 2a 20 44 6f 74 66 69 6c 65 20 6c 6f 63 6b   /* Dotfile lock
e4a10 69 6e 67 20 75 73 65 73 20 74 68 65 20 66 69 6c  ing uses the fil
e4a20 65 20 70 61 74 68 20 73 6f 20 69 74 20 6e 65 65  e path so it nee
e4a30 64 73 20 74 6f 20 62 65 20 69 6e 63 6c 75 64 65  ds to be include
e4a40 64 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20  d in.    ** the 
e4a50 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f  dotlockLockingCo
e4a60 6e 74 65 78 74 20 0a 20 20 20 20 2a 2f 0a 20 20  ntext .    */.  
e4a70 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c    char *zLockFil
e4a80 65 3b 0a 20 20 20 20 69 6e 74 20 6e 46 69 6c 65  e;.    int nFile
e4a90 6e 61 6d 65 3b 0a 20 20 20 20 6e 46 69 6c 65 6e  name;.    nFilen
e4aa0 61 6d 65 20 3d 20 28 69 6e 74 29 73 74 72 6c 65  ame = (int)strle
e4ab0 6e 28 7a 46 69 6c 65 6e 61 6d 65 29 20 2b 20 36  n(zFilename) + 6
e4ac0 3b 0a 20 20 20 20 7a 4c 6f 63 6b 46 69 6c 65 20  ;.    zLockFile 
e4ad0 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65  = (char *)sqlite
e4ae0 33 5f 6d 61 6c 6c 6f 63 28 6e 46 69 6c 65 6e 61  3_malloc(nFilena
e4af0 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 7a 4c 6f  me);.    if( zLo
e4b00 63 6b 46 69 6c 65 3d 3d 30 20 29 7b 0a 20 20 20  ckFile==0 ){.   
e4b10 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
e4b20 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  OMEM;.    }else{
e4b30 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
e4b40 6e 70 72 69 6e 74 66 28 6e 46 69 6c 65 6e 61 6d  nprintf(nFilenam
e4b50 65 2c 20 7a 4c 6f 63 6b 46 69 6c 65 2c 20 22 25  e, zLockFile, "%
e4b60 73 22 20 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49  s" DOTLOCK_SUFFI
e4b70 58 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  X, zFilename);. 
e4b80 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 6c     }.    pNew->l
e4b90 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20  ockingContext = 
e4ba0 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20 7d 0a 0a  zLockFile;.  }..
e4bb0 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20  #if OS_VXWORKS. 
e4bc0 20 65 6c 73 65 20 69 66 28 20 70 4c 6f 63 6b 69   else if( pLocki
e4bd0 6e 67 53 74 79 6c 65 20 3d 3d 20 26 73 65 6d 49  ngStyle == &semI
e4be0 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  oMethods ){.    
e4bf0 2f 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 6f  /* Named semapho
e4c00 72 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20  re locking uses 
e4c10 74 68 65 20 66 69 6c 65 20 70 61 74 68 20 73 6f  the file path so
e4c20 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 0a   it needs to be.
e4c30 20 20 20 20 2a 2a 20 69 6e 63 6c 75 64 65 64 20      ** included 
e4c40 69 6e 20 74 68 65 20 73 65 6d 4c 6f 63 6b 69 6e  in the semLockin
e4c50 67 43 6f 6e 74 65 78 74 0a 20 20 20 20 2a 2f 0a  gContext.    */.
e4c60 20 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74      unixEnterMut
e4c70 65 78 28 29 3b 0a 20 20 20 20 72 63 20 3d 20 66  ex();.    rc = f
e4c80 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77  indLockInfo(pNew
e4c90 2c 20 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20  , &pNew->pLock, 
e4ca0 26 70 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20  &pNew->pOpen);. 
e4cb0 20 20 20 69 66 28 20 28 72 63 3d 3d 53 51 4c 49     if( (rc==SQLI
e4cc0 54 45 5f 4f 4b 29 20 26 26 20 28 70 4e 65 77 2d  TE_OK) && (pNew-
e4cd0 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3d 3d 4e 55  >pOpen->pSem==NU
e4ce0 4c 4c 29 20 29 7b 0a 20 20 20 20 20 20 63 68 61  LL) ){.      cha
e4cf0 72 20 2a 7a 53 65 6d 4e 61 6d 65 20 3d 20 70 4e  r *zSemName = pN
e4d00 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e  ew->pOpen->aSemN
e4d10 61 6d 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  ame;.      int n
e4d20 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
e4d30 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54  snprintf(MAX_PAT
e4d40 48 4e 41 4d 45 2c 20 7a 53 65 6d 4e 61 6d 65 2c  HNAME, zSemName,
e4d50 20 22 2f 25 73 2e 73 65 6d 22 2c 0a 20 20 20 20   "/%s.sem",.    
e4d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e4d70 20 20 20 70 4e 65 77 2d 3e 70 49 64 2d 3e 7a 43     pNew->pId->zC
e4d80 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 29 3b 0a 20  anonicalName);. 
e4d90 20 20 20 20 20 66 6f 72 28 20 6e 3d 31 3b 20 7a       for( n=1; z
e4da0 53 65 6d 4e 61 6d 65 5b 6e 5d 3b 20 6e 2b 2b 20  SemName[n]; n++ 
e4db0 29 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 53  ).        if( zS
e4dc0 65 6d 4e 61 6d 65 5b 6e 5d 3d 3d 27 2f 27 20 29  emName[n]=='/' )
e4dd0 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d 20 3d 20 27   zSemName[n] = '
e4de0 5f 27 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e  _';.      pNew->
e4df0 70 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d 20 73 65  pOpen->pSem = se
e4e00 6d 5f 6f 70 65 6e 28 7a 53 65 6d 4e 61 6d 65 2c  m_open(zSemName,
e4e10 20 4f 5f 43 52 45 41 54 2c 20 30 36 36 36 2c 20   O_CREAT, 0666, 
e4e20 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e  1);.      if( pN
e4e30 65 77 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 20  ew->pOpen->pSem 
e4e40 3d 3d 20 53 45 4d 5f 46 41 49 4c 45 44 20 29 7b  == SEM_FAILED ){
e4e50 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
e4e60 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
e4e70 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d      pNew->pOpen-
e4e80 3e 61 53 65 6d 4e 61 6d 65 5b 30 5d 20 3d 20 27  >aSemName[0] = '
e4e90 5c 30 27 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  \0';.      }.   
e4ea0 20 7d 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65   }.    unixLeave
e4eb0 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 23 65 6e  Mutex();.  }.#en
e4ec0 64 69 66 0a 20 20 0a 20 20 70 4e 65 77 2d 3e 6c  dif.  .  pNew->l
e4ed0 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 0a 23 69  astErrno = 0;.#i
e4ee0 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69  f OS_VXWORKS.  i
e4ef0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
e4f00 20 29 7b 0a 20 20 20 20 75 6e 6c 69 6e 6b 28 7a   ){.    unlink(z
e4f10 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 69  Filename);.    i
e4f20 73 44 65 6c 65 74 65 20 3d 20 30 3b 0a 20 20 7d  sDelete = 0;.  }
e4f30 0a 20 20 70 4e 65 77 2d 3e 69 73 44 65 6c 65 74  .  pNew->isDelet
e4f40 65 20 3d 20 69 73 44 65 6c 65 74 65 3b 0a 23 65  e = isDelete;.#e
e4f50 6e 64 69 66 0a 20 20 69 66 28 20 72 63 21 3d 53  ndif.  if( rc!=S
e4f60 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
e4f70 69 66 28 20 64 69 72 66 64 3e 3d 30 20 29 20 63  if( dirfd>=0 ) c
e4f80 6c 6f 73 65 28 64 69 72 66 64 29 3b 20 2f 2a 20  lose(dirfd); /* 
e4f90 73 69 6c 65 6e 74 20 6c 65 61 6b 20 69 66 20 66  silent leak if f
e4fa0 61 69 6c 2c 20 61 6c 72 65 61 64 79 20 69 6e 20  ail, already in 
e4fb0 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 69 66 28  error */.    if(
e4fc0 20 68 3e 3d 30 20 29 20 63 6c 6f 73 65 28 68 29   h>=0 ) close(h)
e4fd0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
e4fe0 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70  New->pMethod = p
e4ff0 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20  LockingStyle;.  
e5000 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31    OpenCounter(+1
e5010 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
e5020 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65  rc;.}../*.** Ope
e5030 6e 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70  n a file descrip
e5040 74 6f 72 20 74 6f 20 74 68 65 20 64 69 72 65 63  tor to the direc
e5050 74 6f 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20  tory containing 
e5060 66 69 6c 65 20 7a 46 69 6c 65 6e 61 6d 65 2e 0a  file zFilename..
e5070 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  ** If successful
e5080 2c 20 2a 70 46 64 20 69 73 20 73 65 74 20 74 6f  , *pFd is set to
e5090 20 74 68 65 20 6f 70 65 6e 65 64 20 66 69 6c 65   the opened file
e50a0 20 64 65 73 63 72 69 70 74 6f 72 20 61 6e 64 0a   descriptor and.
e50b0 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
e50c0 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20  returned. If an 
e50d0 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 65 69  error occurs, ei
e50e0 74 68 65 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  ther SQLITE_NOME
e50f0 4d 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 43  M.** or SQLITE_C
e5100 41 4e 54 4f 50 45 4e 20 69 73 20 72 65 74 75 72  ANTOPEN is retur
e5110 6e 65 64 20 61 6e 64 20 2a 70 46 64 20 69 73 20  ned and *pFd is 
e5120 73 65 74 20 74 6f 20 61 6e 20 75 6e 64 65 66 69  set to an undefi
e5130 6e 65 64 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2a  ned.** value..**
e5140 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b  .** If SQLITE_OK
e5150 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68   is returned, th
e5160 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70  e caller is resp
e5170 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 6c 6f 73  onsible for clos
e5180 69 6e 67 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20  ing.** the file 
e5190 64 65 73 63 72 69 70 74 6f 72 20 2a 70 46 64 20  descriptor *pFd 
e51a0 75 73 69 6e 67 20 63 6c 6f 73 65 28 29 2e 0a 2a  using close()..*
e51b0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65  /.static int ope
e51c0 6e 44 69 72 65 63 74 6f 72 79 28 63 6f 6e 73 74  nDirectory(const
e51d0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
e51e0 2c 20 69 6e 74 20 2a 70 46 64 29 7b 0a 20 20 69  , int *pFd){.  i
e51f0 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 66 64 20  nt ii;.  int fd 
e5200 3d 20 2d 31 3b 0a 20 20 63 68 61 72 20 7a 44 69  = -1;.  char zDi
e5210 72 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41  rname[MAX_PATHNA
e5220 4d 45 2b 31 5d 3b 0a 0a 20 20 73 71 6c 69 74 65  ME+1];..  sqlite
e5230 33 5f 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50  3_snprintf(MAX_P
e5240 41 54 48 4e 41 4d 45 2c 20 7a 44 69 72 6e 61 6d  ATHNAME, zDirnam
e5250 65 2c 20 22 25 73 22 2c 20 7a 46 69 6c 65 6e 61  e, "%s", zFilena
e5260 6d 65 29 3b 0a 20 20 66 6f 72 28 69 69 3d 28 69  me);.  for(ii=(i
e5270 6e 74 29 73 74 72 6c 65 6e 28 7a 44 69 72 6e 61  nt)strlen(zDirna
e5280 6d 65 29 3b 20 69 69 3e 31 20 26 26 20 7a 44 69  me); ii>1 && zDi
e5290 72 6e 61 6d 65 5b 69 69 5d 21 3d 27 2f 27 3b 20  rname[ii]!='/'; 
e52a0 69 69 2d 2d 29 3b 0a 20 20 69 66 28 20 69 69 3e  ii--);.  if( ii>
e52b0 30 20 29 7b 0a 20 20 20 20 7a 44 69 72 6e 61 6d  0 ){.    zDirnam
e52c0 65 5b 69 69 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  e[ii] = '\0';.  
e52d0 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a 44 69 72    fd = open(zDir
e52e0 6e 61 6d 65 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f  name, O_RDONLY|O
e52f0 5f 42 49 4e 41 52 59 2c 20 30 29 3b 0a 20 20 20  _BINARY, 0);.   
e5300 20 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a 23 69   if( fd>=0 ){.#i
e5310 66 64 65 66 20 46 44 5f 43 4c 4f 45 58 45 43 0a  fdef FD_CLOEXEC.
e5320 20 20 20 20 20 20 66 63 6e 74 6c 28 66 64 2c 20        fcntl(fd, 
e5330 46 5f 53 45 54 46 44 2c 20 66 63 6e 74 6c 28 66  F_SETFD, fcntl(f
e5340 64 2c 20 46 5f 47 45 54 46 44 2c 20 30 29 20 7c  d, F_GETFD, 0) |
e5350 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b 0a 23 65   FD_CLOEXEC);.#e
e5360 6e 64 69 66 0a 20 20 20 20 20 20 4f 53 54 52 41  ndif.      OSTRA
e5370 43 45 33 28 22 4f 50 45 4e 44 49 52 20 25 2d 33  CE3("OPENDIR %-3
e5380 64 20 25 73 5c 6e 22 2c 20 66 64 2c 20 7a 44 69  d %s\n", fd, zDi
e5390 72 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20  rname);.    }.  
e53a0 7d 0a 20 20 2a 70 46 64 20 3d 20 66 64 3b 0a 20  }.  *pFd = fd;. 
e53b0 20 72 65 74 75 72 6e 20 28 66 64 3e 3d 30 3f 53   return (fd>=0?S
e53c0 51 4c 49 54 45 5f 4f 4b 3a 53 51 4c 49 54 45 5f  QLITE_OK:SQLITE_
e53d0 43 41 4e 54 4f 50 45 4e 29 3b 0a 7d 0a 0a 2f 2a  CANTOPEN);.}../*
e53e0 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d  .** Create a tem
e53f0 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65  porary file name
e5400 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20   in zBuf.  zBuf 
e5410 6d 75 73 74 20 62 65 20 61 6c 6c 6f 63 61 74 65  must be allocate
e5420 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c  d.** by the call
e5430 69 6e 67 20 70 72 6f 63 65 73 73 20 61 6e 64 20  ing process and 
e5440 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75  must be big enou
e5450 67 68 20 74 6f 20 68 6f 6c 64 20 61 74 20 6c 65  gh to hold at le
e5460 61 73 74 0a 2a 2a 20 70 56 66 73 2d 3e 6d 78 50  ast.** pVfs->mxP
e5470 61 74 68 6e 61 6d 65 20 62 79 74 65 73 2e 0a 2a  athname bytes..*
e5480 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
e5490 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 75  Tempname(int nBu
e54a0 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a  f, char *zBuf){.
e54b0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
e54c0 68 61 72 20 2a 61 7a 44 69 72 73 5b 5d 20 3d 20  har *azDirs[] = 
e54d0 7b 0a 20 20 20 20 20 30 2c 0a 20 20 20 20 20 30  {.     0,.     0
e54e0 2c 0a 20 20 20 20 20 22 2f 76 61 72 2f 74 6d 70  ,.     "/var/tmp
e54f0 22 2c 0a 20 20 20 20 20 22 2f 75 73 72 2f 74 6d  ",.     "/usr/tm
e5500 70 22 2c 0a 20 20 20 20 20 22 2f 74 6d 70 22 2c  p",.     "/tmp",
e5510 0a 20 20 20 20 20 22 2e 22 2c 0a 20 20 7d 3b 0a  .     ".",.  };.
e5520 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
e5530 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a 43 68  nsigned char zCh
e5540 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63  ars[] =.    "abc
e5550 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73  defghijklmnopqrs
e5560 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42  tuvwxyz".    "AB
e5570 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52  CDEFGHIJKLMNOPQR
e5580 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30  STUVWXYZ".    "0
e5590 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20 75 6e  123456789";.  un
e55a0 73 69 67 6e 65 64 20 69 6e 74 20 69 2c 20 6a 3b  signed int i, j;
e55b0 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 20 62  .  struct stat b
e55c0 75 66 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  uf;.  const char
e55d0 20 2a 7a 44 69 72 20 3d 20 22 2e 22 3b 0a 0a 20   *zDir = ".";.. 
e55e0 20 2f 2a 20 49 74 27 73 20 6f 64 64 20 74 6f 20   /* It's odd to 
e55f0 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65  simulate an io-e
e5600 72 72 6f 72 20 68 65 72 65 2c 20 62 75 74 20 72  rror here, but r
e5610 65 61 6c 6c 79 20 74 68 69 73 20 69 73 20 6a 75  eally this is ju
e5620 73 74 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68  st.  ** using th
e5630 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61  e io-error infra
e5640 73 74 72 75 63 74 75 72 65 20 74 6f 20 74 65 73  structure to tes
e5650 74 20 74 68 61 74 20 53 51 4c 69 74 65 20 68 61  t that SQLite ha
e5660 6e 64 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a 20  ndles this.  ** 
e5670 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67  function failing
e5680 2e 20 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61  . .  */.  Simula
e5690 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
e56a0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29  n SQLITE_IOERR )
e56b0 3b 0a 0a 20 20 61 7a 44 69 72 73 5b 30 5d 20 3d  ;..  azDirs[0] =
e56c0 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69   sqlite3_temp_di
e56d0 72 65 63 74 6f 72 79 3b 0a 20 20 69 66 20 28 4e  rectory;.  if (N
e56e0 55 4c 4c 20 3d 3d 20 61 7a 44 69 72 73 5b 31 5d  ULL == azDirs[1]
e56f0 29 20 7b 0a 20 20 20 20 61 7a 44 69 72 73 5b 31  ) {.    azDirs[1
e5700 5d 20 3d 20 67 65 74 65 6e 76 28 22 54 4d 50 44  ] = getenv("TMPD
e5710 49 52 22 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 66  IR");.  }.  .  f
e5720 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66  or(i=0; i<sizeof
e5730 28 61 7a 44 69 72 73 29 2f 73 69 7a 65 6f 66 28  (azDirs)/sizeof(
e5740 61 7a 44 69 72 73 5b 30 5d 29 3b 20 69 2b 2b 29  azDirs[0]); i++)
e5750 7b 0a 20 20 20 20 69 66 28 20 61 7a 44 69 72 73  {.    if( azDirs
e5760 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  [i]==0 ) continu
e5770 65 3b 0a 20 20 20 20 69 66 28 20 73 74 61 74 28  e;.    if( stat(
e5780 61 7a 44 69 72 73 5b 69 5d 2c 20 26 62 75 66 29  azDirs[i], &buf)
e5790 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
e57a0 20 69 66 28 20 21 53 5f 49 53 44 49 52 28 62 75   if( !S_ISDIR(bu
e57b0 66 2e 73 74 5f 6d 6f 64 65 29 20 29 20 63 6f 6e  f.st_mode) ) con
e57c0 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 61  tinue;.    if( a
e57d0 63 63 65 73 73 28 61 7a 44 69 72 73 5b 69 5d 2c  ccess(azDirs[i],
e57e0 20 30 37 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b   07) ) continue;
e57f0 0a 20 20 20 20 7a 44 69 72 20 3d 20 61 7a 44 69  .    zDir = azDi
e5800 72 73 5b 69 5d 3b 0a 20 20 20 20 62 72 65 61 6b  rs[i];.    break
e5810 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63  ;.  }..  /* Chec
e5820 6b 20 74 68 61 74 20 74 68 65 20 6f 75 74 70 75  k that the outpu
e5830 74 20 62 75 66 66 65 72 20 69 73 20 6c 61 72 67  t buffer is larg
e5840 65 20 65 6e 6f 75 67 68 20 66 6f 72 20 74 68 65  e enough for the
e5850 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
e5860 0a 20 20 2a 2a 20 6e 61 6d 65 2e 20 49 66 20 69  .  ** name. If i
e5870 74 20 69 73 20 6e 6f 74 2c 20 72 65 74 75 72 6e  t is not, return
e5880 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 20   SQLITE_ERROR.. 
e5890 20 2a 2f 0a 20 20 69 66 28 20 28 73 74 72 6c 65   */.  if( (strle
e58a0 6e 28 7a 44 69 72 29 20 2b 20 73 74 72 6c 65 6e  n(zDir) + strlen
e58b0 28 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c  (SQLITE_TEMP_FIL
e58c0 45 5f 50 52 45 46 49 58 29 20 2b 20 31 37 29 20  E_PREFIX) + 17) 
e58d0 3e 3d 20 28 73 69 7a 65 5f 74 29 6e 42 75 66 20  >= (size_t)nBuf 
e58e0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
e58f0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
e5900 0a 20 20 64 6f 7b 0a 20 20 20 20 73 71 6c 69 74  .  do{.    sqlit
e5910 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66  e3_snprintf(nBuf
e5920 2d 31 37 2c 20 7a 42 75 66 2c 20 22 25 73 2f 22  -17, zBuf, "%s/"
e5930 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45  SQLITE_TEMP_FILE
e5940 5f 50 52 45 46 49 58 2c 20 7a 44 69 72 29 3b 0a  _PREFIX, zDir);.
e5950 20 20 20 20 6a 20 3d 20 28 69 6e 74 29 73 74 72      j = (int)str
e5960 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20 20 20 73  len(zBuf);.    s
e5970 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
e5980 73 28 31 35 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b  s(15, &zBuf[j]);
e5990 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
e59a0 31 35 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20  15; i++, j++){. 
e59b0 20 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28       zBuf[j] = (
e59c0 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75  char)zChars[ ((u
e59d0 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75  nsigned char)zBu
e59e0 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43  f[j])%(sizeof(zC
e59f0 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 20 20  hars)-1) ];.    
e5a00 7d 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20  }.    zBuf[j] = 
e5a10 30 3b 0a 20 20 7d 77 68 69 6c 65 28 20 61 63 63  0;.  }while( acc
e5a20 65 73 73 28 7a 42 75 66 2c 30 29 3d 3d 30 20 29  ess(zBuf,0)==0 )
e5a30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
e5a40 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c  E_OK;.}..#if SQL
e5a50 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
e5a60 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69  NG_STYLE && defi
e5a70 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f  ned(__APPLE__)./
e5a80 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 74 6f 20  *.** Routine to 
e5a90 74 72 61 6e 73 66 6f 72 6d 20 61 20 75 6e 69 78  transform a unix
e5aa0 46 69 6c 65 20 69 6e 74 6f 20 61 20 70 72 6f 78  File into a prox
e5ab0 79 2d 6c 6f 63 6b 69 6e 67 20 75 6e 69 78 46 69  y-locking unixFi
e5ac0 6c 65 2e 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  le..** Implement
e5ad0 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 70 72 6f  ation in the pro
e5ae0 78 79 2d 6c 6f 63 6b 20 64 69 76 69 73 69 6f 6e  xy-lock division
e5af0 2c 20 62 75 74 20 75 73 65 64 20 62 79 20 75 6e  , but used by un
e5b00 69 78 4f 70 65 6e 28 29 0a 2a 2a 20 69 66 20 53  ixOpen().** if S
e5b10 51 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f  QLITE_PREFER_PRO
e5b20 58 59 5f 4c 4f 43 4b 49 4e 47 20 69 73 20 64 65  XY_LOCKING is de
e5b30 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  fined..*/.static
e5b40 20 69 6e 74 20 70 72 6f 78 79 54 72 61 6e 73 66   int proxyTransf
e5b50 6f 72 6d 55 6e 69 78 46 69 6c 65 28 75 6e 69 78  ormUnixFile(unix
e5b60 46 69 6c 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61  File*, const cha
e5b70 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  r*);.#endif../*.
e5b80 2a 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61 6e  ** Search for an
e5b90 20 75 6e 75 73 65 64 20 66 69 6c 65 20 64 65 73   unused file des
e5ba0 63 72 69 70 74 6f 72 20 74 68 61 74 20 77 61 73  criptor that was
e5bb0 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65 20 64   opened on the d
e5bc0 61 74 61 62 61 73 65 20 0a 2a 2a 20 66 69 6c 65  atabase .** file
e5bd0 20 28 6e 6f 74 20 61 20 6a 6f 75 72 6e 61 6c 20   (not a journal 
e5be0 6f 72 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61  or master-journa
e5bf0 6c 20 66 69 6c 65 29 20 69 64 65 6e 74 69 66 69  l file) identifi
e5c00 65 64 20 62 79 20 70 61 74 68 6e 61 6d 65 0a 2a  ed by pathname.*
e5c10 2a 20 7a 50 61 74 68 20 77 69 74 68 20 53 51 4c  * zPath with SQL
e5c20 49 54 45 5f 4f 50 45 4e 5f 58 58 58 20 66 6c 61  ITE_OPEN_XXX fla
e5c30 67 73 20 6d 61 74 63 68 69 6e 67 20 74 68 6f 73  gs matching thos
e5c40 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
e5c50 73 65 63 6f 6e 64 0a 2a 2a 20 61 72 67 75 6d 65  second.** argume
e5c60 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  nt to this funct
e5c70 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 75 63 68 20  ion..**.** Such 
e5c80 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  a file descripto
e5c90 72 20 6d 61 79 20 65 78 69 73 74 20 69 66 20 61  r may exist if a
e5ca0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
e5cb0 74 69 6f 6e 20 77 61 73 20 63 6c 6f 73 65 64 0a  tion was closed.
e5cc0 2a 2a 20 62 75 74 20 74 68 65 20 61 73 73 6f 63  ** but the assoc
e5cd0 69 61 74 65 64 20 66 69 6c 65 20 64 65 73 63 72  iated file descr
e5ce0 69 70 74 6f 72 20 63 6f 75 6c 64 20 6e 6f 74 20  iptor could not 
e5cf0 62 65 20 63 6c 6f 73 65 64 20 62 65 63 61 75 73  be closed becaus
e5d00 65 20 73 6f 6d 65 0a 2a 2a 20 6f 74 68 65 72 20  e some.** other 
e5d10 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
e5d20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65  open on the same
e5d30 20 66 69 6c 65 20 69 73 20 68 6f 6c 64 69 6e 67   file is holding
e5d40 20 61 20 66 69 6c 65 2d 6c 6f 63 6b 2e 0a 2a 2a   a file-lock..**
e5d50 20 52 65 66 65 72 20 74 6f 20 63 6f 6d 6d 65 6e   Refer to commen
e5d60 74 73 20 69 6e 20 74 68 65 20 75 6e 69 78 43 6c  ts in the unixCl
e5d70 6f 73 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 61  ose() function a
e5d80 6e 64 20 74 68 65 20 6c 65 6e 67 74 68 79 20 63  nd the lengthy c
e5d90 6f 6d 6d 65 6e 74 0a 2a 2a 20 64 65 73 63 72 69  omment.** descri
e5da0 62 69 6e 67 20 22 50 6f 73 69 78 20 41 64 76 69  bing "Posix Advi
e5db0 73 6f 72 79 20 4c 6f 63 6b 69 6e 67 22 20 61 74  sory Locking" at
e5dc0 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
e5dd0 69 73 20 66 69 6c 65 20 66 6f 72 20 0a 2a 2a 20  is file for .** 
e5de0 66 75 72 74 68 65 72 20 64 65 74 61 69 6c 73 2e  further details.
e5df0 20 41 6c 73 6f 2c 20 74 69 63 6b 65 74 20 23 34   Also, ticket #4
e5e00 30 31 38 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  018..**.** If a 
e5e10 73 75 69 74 61 62 6c 65 20 66 69 6c 65 20 64 65  suitable file de
e5e20 73 63 72 69 70 74 6f 72 20 69 73 20 66 6f 75 6e  scriptor is foun
e5e30 64 2c 20 74 68 65 6e 20 69 74 20 69 73 20 72 65  d, then it is re
e5e40 74 75 72 6e 65 64 2e 20 49 66 20 6e 6f 0a 2a 2a  turned. If no.**
e5e50 20 73 75 63 68 20 66 69 6c 65 20 64 65 73 63 72   such file descr
e5e60 69 70 74 6f 72 20 69 73 20 6c 6f 63 61 74 65 64  iptor is located
e5e70 2c 20 2d 31 20 69 73 20 72 65 74 75 72 6e 65 64  , -1 is returned
e5e80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 55 6e 69 78  ..*/.static Unix
e5e90 55 6e 75 73 65 64 46 64 20 2a 66 69 6e 64 52 65  UnusedFd *findRe
e5ea0 75 73 61 62 6c 65 46 64 28 63 6f 6e 73 74 20 63  usableFd(const c
e5eb0 68 61 72 20 2a 7a 50 61 74 68 2c 20 69 6e 74 20  har *zPath, int 
e5ec0 66 6c 61 67 73 29 7b 0a 20 20 55 6e 69 78 55 6e  flags){.  UnixUn
e5ed0 75 73 65 64 46 64 20 2a 70 55 6e 75 73 65 64 20  usedFd *pUnused 
e5ee0 3d 20 30 3b 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f  = 0;..  /* Do no
e5ef0 74 20 73 65 61 72 63 68 20 66 6f 72 20 61 6e 20  t search for an 
e5f00 75 6e 75 73 65 64 20 66 69 6c 65 20 64 65 73 63  unused file desc
e5f10 72 69 70 74 6f 72 20 6f 6e 20 76 78 77 6f 72 6b  riptor on vxwork
e5f20 73 2e 20 4e 6f 74 20 62 65 63 61 75 73 65 0a 20  s. Not because. 
e5f30 20 2a 2a 20 76 78 77 6f 72 6b 73 20 77 6f 75 6c   ** vxworks woul
e5f40 64 20 6e 6f 74 20 62 65 6e 65 66 69 74 20 66 72  d not benefit fr
e5f50 6f 6d 20 74 68 65 20 63 68 61 6e 67 65 20 28 69  om the change (i
e5f60 74 20 6d 69 67 68 74 2c 20 77 65 27 72 65 20 6e  t might, we're n
e5f70 6f 74 20 73 75 72 65 29 2c 0a 20 20 2a 2a 20 62  ot sure),.  ** b
e5f80 75 74 20 62 65 63 61 75 73 65 20 6e 6f 20 77 61  ut because no wa
e5f90 79 20 74 6f 20 74 65 73 74 20 69 74 20 69 73 20  y to test it is 
e5fa0 63 75 72 72 65 6e 74 6c 79 20 61 76 61 69 6c 61  currently availa
e5fb0 62 6c 65 2e 20 49 74 20 69 73 20 62 65 74 74 65  ble. It is bette
e5fc0 72 20 0a 20 20 2a 2a 20 6e 6f 74 20 74 6f 20 72  r .  ** not to r
e5fd0 69 73 6b 20 62 72 65 61 6b 69 6e 67 20 76 78 77  isk breaking vxw
e5fe0 6f 72 6b 73 20 73 75 70 70 6f 72 74 20 66 6f 72  orks support for
e5ff0 20 74 68 65 20 73 61 6b 65 20 6f 66 20 73 75 63   the sake of suc
e6000 68 20 61 6e 20 6f 62 73 63 75 72 65 20 0a 20 20  h an obscure .  
e6010 2a 2a 20 66 65 61 74 75 72 65 2e 20 20 2a 2f 0a  ** feature.  */.
e6020 23 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a  #if !OS_VXWORKS.
e6030 20 20 73 74 72 75 63 74 20 73 74 61 74 20 73 53    struct stat sS
e6040 74 61 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  tat;            
e6050 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
e6060 73 20 6f 66 20 73 74 61 74 28 29 20 63 61 6c 6c  s of stat() call
e6070 20 2a 2f 0a 0a 20 20 2f 2a 20 41 20 73 74 61 74   */..  /* A stat
e6080 28 29 20 63 61 6c 6c 20 6d 61 79 20 66 61 69 6c  () call may fail
e6090 20 66 6f 72 20 76 61 72 69 6f 75 73 20 72 65 61   for various rea
e60a0 73 6f 6e 73 2e 20 49 66 20 74 68 69 73 20 68 61  sons. If this ha
e60b0 70 70 65 6e 73 2c 20 69 74 20 69 73 0a 20 20 2a  ppens, it is.  *
e60c0 2a 20 61 6c 6d 6f 73 74 20 63 65 72 74 61 69 6e  * almost certain
e60d0 20 74 68 61 74 20 61 6e 20 6f 70 65 6e 28 29 20   that an open() 
e60e0 63 61 6c 6c 20 6f 6e 20 74 68 65 20 73 61 6d 65  call on the same
e60f0 20 70 61 74 68 20 77 69 6c 6c 20 61 6c 73 6f 20   path will also 
e6100 66 61 69 6c 2e 0a 20 20 2a 2a 20 46 6f 72 20 74  fail..  ** For t
e6110 68 69 73 20 72 65 61 73 6f 6e 2c 20 69 66 20 61  his reason, if a
e6120 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 69  n error occurs i
e6130 6e 20 74 68 65 20 73 74 61 74 28 29 20 63 61 6c  n the stat() cal
e6140 6c 20 68 65 72 65 2c 20 69 74 20 69 73 0a 20 20  l here, it is.  
e6150 2a 2a 20 69 67 6e 6f 72 65 64 20 61 6e 64 20 2d  ** ignored and -
e6160 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 54  1 is returned. T
e6170 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 74  he caller will t
e6180 72 79 20 74 6f 20 6f 70 65 6e 20 61 20 6e 65 77  ry to open a new
e6190 20 66 69 6c 65 0a 20 20 2a 2a 20 64 65 73 63 72   file.  ** descr
e61a0 69 70 74 6f 72 20 6f 6e 20 74 68 65 20 73 61 6d  iptor on the sam
e61b0 65 20 70 61 74 68 2c 20 66 61 69 6c 2c 20 61 6e  e path, fail, an
e61c0 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  d return an erro
e61d0 72 20 74 6f 20 53 51 4c 69 74 65 2e 0a 20 20 2a  r to SQLite..  *
e61e0 2a 0a 20 20 2a 2a 20 45 76 65 6e 20 69 66 20 61  *.  ** Even if a
e61f0 20 73 75 62 73 65 71 75 65 6e 74 20 6f 70 65 6e   subsequent open
e6200 28 29 20 63 61 6c 6c 20 64 6f 65 73 20 73 75 63  () call does suc
e6210 63 65 65 64 2c 20 74 68 65 20 63 6f 6e 73 65 71  ceed, the conseq
e6220 75 65 6e 63 65 73 20 6f 66 0a 20 20 2a 2a 20 6e  uences of.  ** n
e6230 6f 74 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72  ot searching for
e6240 20 61 20 72 65 73 75 73 61 62 6c 65 20 66 69 6c   a resusable fil
e6250 65 20 64 65 73 63 72 69 70 74 6f 72 20 61 72 65  e descriptor are
e6260 20 6e 6f 74 20 64 69 72 65 2e 20 20 2a 2f 0a 20   not dire.  */. 
e6270 20 69 66 28 20 30 3d 3d 73 74 61 74 28 7a 50 61   if( 0==stat(zPa
e6280 74 68 2c 20 26 73 53 74 61 74 29 20 29 7b 0a 20  th, &sStat) ){. 
e6290 20 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70     struct unixOp
e62a0 65 6e 43 6e 74 20 2a 70 4f 3b 0a 20 20 20 20 73  enCnt *pO;.    s
e62b0 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64  truct unixFileId
e62c0 20 69 64 3b 0a 20 20 20 20 69 64 2e 64 65 76 20   id;.    id.dev 
e62d0 3d 20 73 53 74 61 74 2e 73 74 5f 64 65 76 3b 0a  = sStat.st_dev;.
e62e0 20 20 20 20 69 64 2e 69 6e 6f 20 3d 20 73 53 74      id.ino = sSt
e62f0 61 74 2e 73 74 5f 69 6e 6f 3b 0a 0a 20 20 20 20  at.st_ino;..    
e6300 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
e6310 3b 0a 20 20 20 20 66 6f 72 28 70 4f 3d 6f 70 65  ;.    for(pO=ope
e6320 6e 4c 69 73 74 3b 20 70 4f 20 26 26 20 6d 65 6d  nList; pO && mem
e6330 63 6d 70 28 26 69 64 2c 20 26 70 4f 2d 3e 66 69  cmp(&id, &pO->fi
e6340 6c 65 49 64 2c 20 73 69 7a 65 6f 66 28 69 64 29  leId, sizeof(id)
e6350 29 3b 20 70 4f 3d 70 4f 2d 3e 70 4e 65 78 74 29  ); pO=pO->pNext)
e6360 3b 0a 20 20 20 20 69 66 28 20 70 4f 20 29 7b 0a  ;.    if( pO ){.
e6370 20 20 20 20 20 20 55 6e 69 78 55 6e 75 73 65 64        UnixUnused
e6380 46 64 20 2a 2a 70 70 3b 0a 20 20 20 20 20 20 66  Fd **pp;.      f
e6390 6f 72 28 70 70 3d 26 70 4f 2d 3e 70 55 6e 75 73  or(pp=&pO->pUnus
e63a0 65 64 3b 20 2a 70 70 20 26 26 20 28 2a 70 70 29  ed; *pp && (*pp)
e63b0 2d 3e 66 6c 61 67 73 21 3d 66 6c 61 67 73 3b 20  ->flags!=flags; 
e63c0 70 70 3d 26 28 28 2a 70 70 29 2d 3e 70 4e 65 78  pp=&((*pp)->pNex
e63d0 74 29 29 3b 0a 20 20 20 20 20 20 70 55 6e 75 73  t));.      pUnus
e63e0 65 64 20 3d 20 2a 70 70 3b 0a 20 20 20 20 20 20  ed = *pp;.      
e63f0 69 66 28 20 70 55 6e 75 73 65 64 20 29 7b 0a 20  if( pUnused ){. 
e6400 20 20 20 20 20 20 20 2a 70 70 20 3d 20 70 55 6e         *pp = pUn
e6410 75 73 65 64 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  used->pNext;.   
e6420 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 75     }.    }.    u
e6430 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  nixLeaveMutex();
e6440 0a 20 20 7d 0a 23 65 6e 64 69 66 20 20 20 20 2f  .  }.#endif    /
e6450 2a 20 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53  * if !OS_VXWORKS
e6460 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 70 55 6e   */.  return pUn
e6470 75 73 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f  used;.}../*.** O
e6480 70 65 6e 20 74 68 65 20 66 69 6c 65 20 7a 50 61  pen the file zPa
e6490 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 50 72 65 76 69  th..** .** Previ
e64a0 6f 75 73 6c 79 2c 20 74 68 65 20 53 51 4c 69 74  ously, the SQLit
e64b0 65 20 4f 53 20 6c 61 79 65 72 20 75 73 65 64 20  e OS layer used 
e64c0 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 20  three functions 
e64d0 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 69 73  in place of this
e64e0 0a 2a 2a 20 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20  .** one:.**.**  
e64f0 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e     sqlite3OsOpen
e6500 52 65 61 64 57 72 69 74 65 28 29 3b 0a 2a 2a 20  ReadWrite();.** 
e6510 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65      sqlite3OsOpe
e6520 6e 52 65 61 64 4f 6e 6c 79 28 29 3b 0a 2a 2a 20  nReadOnly();.** 
e6530 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65      sqlite3OsOpe
e6540 6e 45 78 63 6c 75 73 69 76 65 28 29 3b 0a 2a 2a  nExclusive();.**
e6550 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c 6c 73 20  .** These calls 
e6560 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68  correspond to th
e6570 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62  e following comb
e6580 69 6e 61 74 69 6f 6e 73 20 6f 66 20 66 6c 61 67  inations of flag
e6590 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 52 65 61  s:.**.**     Rea
e65a0 64 57 72 69 74 65 28 29 20 2d 3e 20 20 20 20 20  dWrite() ->     
e65b0 28 52 45 41 44 57 52 49 54 45 20 7c 20 43 52 45  (READWRITE | CRE
e65c0 41 54 45 29 0a 2a 2a 20 20 20 20 20 52 65 61 64  ATE).**     Read
e65d0 4f 6e 6c 79 28 29 20 20 2d 3e 20 20 20 20 20 28  Only()  ->     (
e65e0 52 45 41 44 4f 4e 4c 59 29 20 0a 2a 2a 20 20 20  READONLY) .**   
e65f0 20 20 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28    OpenExclusive(
e6600 29 20 2d 3e 20 28 52 45 41 44 57 52 49 54 45 20  ) -> (READWRITE 
e6610 7c 20 43 52 45 41 54 45 20 7c 20 45 58 43 4c 55  | CREATE | EXCLU
e6620 53 49 56 45 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20  SIVE).**.** The 
e6630 6f 6c 64 20 4f 70 65 6e 45 78 63 6c 75 73 69 76  old OpenExclusiv
e6640 65 28 29 20 61 63 63 65 70 74 65 64 20 61 20 62  e() accepted a b
e6650 6f 6f 6c 65 61 6e 20 61 72 67 75 6d 65 6e 74 20  oolean argument 
e6660 2d 20 22 64 65 6c 46 6c 61 67 22 2e 20 49 66 0a  - "delFlag". If.
e6670 2a 2a 20 74 72 75 65 2c 20 74 68 65 20 66 69 6c  ** true, the fil
e6680 65 20 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64  e was configured
e6690 20 74 6f 20 62 65 20 61 75 74 6f 6d 61 74 69 63   to be automatic
e66a0 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 77 68 65  ally deleted whe
e66b0 6e 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 68 61  n the.** file ha
e66c0 6e 64 6c 65 20 63 6c 6f 73 65 64 2e 20 54 6f 20  ndle closed. To 
e66d0 61 63 68 69 65 76 65 20 74 68 65 20 73 61 6d 65  achieve the same
e66e0 20 65 66 66 65 63 74 20 75 73 69 6e 67 20 74 68   effect using th
e66f0 69 73 20 6e 65 77 20 0a 2a 2a 20 69 6e 74 65 72  is new .** inter
e6700 66 61 63 65 2c 20 61 64 64 20 74 68 65 20 44 45  face, add the DE
e6710 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 66 6c 61 67  LETEONCLOSE flag
e6720 20 74 6f 20 74 68 6f 73 65 20 73 70 65 63 69 66   to those specif
e6730 69 65 64 20 61 62 6f 76 65 20 66 6f 72 20 0a 2a  ied above for .*
e6740 2a 20 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28  * OpenExclusive(
e6750 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
e6760 20 75 6e 69 78 4f 70 65 6e 28 0a 20 20 73 71 6c   unixOpen(.  sql
e6770 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
e6780 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
e6790 20 56 46 53 20 66 6f 72 20 77 68 69 63 68 20 74   VFS for which t
e67a0 68 69 73 20 69 73 20 74 68 65 20 78 4f 70 65 6e  his is the xOpen
e67b0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e   method */.  con
e67c0 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20  st char *zPath, 
e67d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 74            /* Pat
e67e0 68 6e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f  hname of file to
e67f0 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20   be opened */.  
e6800 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46  sqlite3_file *pF
e6810 69 6c 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ile,         /* 
e6820 54 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  The file descrip
e6830 74 6f 72 20 74 6f 20 62 65 20 66 69 6c 6c 65 64  tor to be filled
e6840 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61   in */.  int fla
e6850 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gs,             
e6860 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 66        /* Input f
e6870 6c 61 67 73 20 74 6f 20 63 6f 6e 74 72 6f 6c 20  lags to control 
e6880 74 68 65 20 6f 70 65 6e 69 6e 67 20 2a 2f 0a 20  the opening */. 
e6890 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20   int *pOutFlags 
e68a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e68b0 20 4f 75 74 70 75 74 20 66 6c 61 67 73 20 72 65   Output flags re
e68c0 74 75 72 6e 65 64 20 74 6f 20 53 51 4c 69 74 65  turned to SQLite
e68d0 20 63 6f 72 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e   core */.){.  un
e68e0 69 78 46 69 6c 65 20 2a 70 20 3d 20 28 75 6e 69  ixFile *p = (uni
e68f0 78 46 69 6c 65 20 2a 29 70 46 69 6c 65 3b 0a 20  xFile *)pFile;. 
e6900 20 69 6e 74 20 66 64 20 3d 20 2d 31 3b 20 20 20   int fd = -1;   
e6910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e6920 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74  /* File descript
e6930 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 6f  or returned by o
e6940 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 64  pen() */.  int d
e6950 69 72 66 64 20 3d 20 2d 31 3b 20 20 20 20 20 20  irfd = -1;      
e6960 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72            /* Dir
e6970 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65 73 63  ectory file desc
e6980 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20  riptor */.  int 
e6990 6f 70 65 6e 46 6c 61 67 73 20 3d 20 30 3b 20 20  openFlags = 0;  
e69a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c             /* Fl
e69b0 61 67 73 20 74 6f 20 70 61 73 73 20 74 6f 20 6f  ags to pass to o
e69c0 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 65  pen() */.  int e
e69d0 54 79 70 65 20 3d 20 66 6c 61 67 73 26 30 78 46  Type = flags&0xF
e69e0 46 46 46 46 46 30 30 3b 20 20 2f 2a 20 54 79 70  FFFFF00;  /* Typ
e69f0 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 6f 70 65  e of file to ope
e6a00 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63  n */.  int noLoc
e6a10 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
e6a20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
e6a30 20 6f 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 70 72   omit locking pr
e6a40 69 6d 69 74 69 76 65 73 20 2a 2f 0a 20 20 69 6e  imitives */.  in
e6a50 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
e6a60 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
e6a70 46 75 6e 63 74 69 6f 6e 20 52 65 74 75 72 6e 20  Function Return 
e6a80 43 6f 64 65 20 2a 2f 0a 0a 20 20 69 6e 74 20 69  Code */..  int i
e6a90 73 45 78 63 6c 75 73 69 76 65 20 20 3d 20 28 66  sExclusive  = (f
e6aa0 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50  lags & SQLITE_OP
e6ab0 45 4e 5f 45 58 43 4c 55 53 49 56 45 29 3b 0a 20  EN_EXCLUSIVE);. 
e6ac0 20 69 6e 74 20 69 73 44 65 6c 65 74 65 20 20 20   int isDelete   
e6ad0 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c    = (flags & SQL
e6ae0 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f  ITE_OPEN_DELETEO
e6af0 4e 43 4c 4f 53 45 29 3b 0a 20 20 69 6e 74 20 69  NCLOSE);.  int i
e6b00 73 43 72 65 61 74 65 20 20 20 20 20 3d 20 28 66  sCreate     = (f
e6b10 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50  lags & SQLITE_OP
e6b20 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20 20 69 6e  EN_CREATE);.  in
e6b30 74 20 69 73 52 65 61 64 6f 6e 6c 79 20 20 20 3d  t isReadonly   =
e6b40 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45   (flags & SQLITE
e6b50 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b  _OPEN_READONLY);
e6b60 0a 20 20 69 6e 74 20 69 73 52 65 61 64 57 72 69  .  int isReadWri
e6b70 74 65 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53  te  = (flags & S
e6b80 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
e6b90 52 49 54 45 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  RITE);..  /* If 
e6ba0 63 72 65 61 74 69 6e 67 20 61 20 6d 61 73 74 65  creating a maste
e6bb0 72 20 6f 72 20 6d 61 69 6e 2d 66 69 6c 65 20 6a  r or main-file j
e6bc0 6f 75 72 6e 61 6c 2c 20 74 68 69 73 20 66 75 6e  ournal, this fun
e6bd0 63 74 69 6f 6e 20 77 69 6c 6c 20 6f 70 65 6e 0a  ction will open.
e6be0 20 20 2a 2a 20 61 20 66 69 6c 65 2d 64 65 73 63    ** a file-desc
e6bf0 72 69 70 74 6f 72 20 6f 6e 20 74 68 65 20 64 69  riptor on the di
e6c00 72 65 63 74 6f 72 79 20 74 6f 6f 2e 20 54 68 65  rectory too. The
e6c10 20 66 69 72 73 74 20 74 69 6d 65 20 75 6e 69 78   first time unix
e6c20 53 79 6e 63 28 29 0a 20 20 2a 2a 20 69 73 20 63  Sync().  ** is c
e6c30 61 6c 6c 65 64 20 74 68 65 20 64 69 72 65 63 74  alled the direct
e6c40 6f 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 70  ory file descrip
e6c50 74 6f 72 20 77 69 6c 6c 20 62 65 20 66 73 79 6e  tor will be fsyn
e6c60 63 28 29 65 64 20 61 6e 64 20 63 6c 6f 73 65 28  c()ed and close(
e6c70 29 64 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69  )d..  */.  int i
e6c80 73 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 20 3d  sOpenDirectory =
e6c90 20 28 69 73 43 72 65 61 74 65 20 26 26 20 0a 20   (isCreate && . 
e6ca0 20 20 20 20 20 28 65 54 79 70 65 3d 3d 53 51 4c       (eType==SQL
e6cb0 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f  ITE_OPEN_MASTER_
e6cc0 4a 4f 55 52 4e 41 4c 20 7c 7c 20 65 54 79 70 65  JOURNAL || eType
e6cd0 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  ==SQLITE_OPEN_MA
e6ce0 49 4e 5f 4a 4f 55 52 4e 41 4c 29 0a 20 20 29 3b  IN_JOURNAL).  );
e6cf0 0a 0a 20 20 2f 2a 20 49 66 20 61 72 67 75 6d 65  ..  /* If argume
e6d00 6e 74 20 7a 50 61 74 68 20 69 73 20 61 20 4e 55  nt zPath is a NU
e6d10 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 69 73  LL pointer, this
e6d20 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 71   function is req
e6d30 75 69 72 65 64 20 74 6f 20 6f 70 65 6e 0a 20 20  uired to open.  
e6d40 2a 2a 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66  ** a temporary f
e6d50 69 6c 65 2e 20 55 73 65 20 74 68 69 73 20 62 75  ile. Use this bu
e6d60 66 66 65 72 20 74 6f 20 73 74 6f 72 65 20 74 68  ffer to store th
e6d70 65 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 2e 0a  e file name in..
e6d80 20 20 2a 2f 0a 20 20 63 68 61 72 20 7a 54 6d 70    */.  char zTmp
e6d90 6e 61 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d  name[MAX_PATHNAM
e6da0 45 2b 31 5d 3b 0a 20 20 63 6f 6e 73 74 20 63 68  E+1];.  const ch
e6db0 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 7a 50 61 74  ar *zName = zPat
e6dc0 68 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74  h;..  /* Check t
e6dd0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61  he following sta
e6de0 74 65 6d 65 6e 74 73 20 61 72 65 20 74 72 75 65  tements are true
e6df0 3a 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 28  : .  **.  **   (
e6e00 61 29 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 6f  a) Exactly one o
e6e10 66 20 74 68 65 20 52 45 41 44 57 52 49 54 45 20  f the READWRITE 
e6e20 61 6e 64 20 52 45 41 44 4f 4e 4c 59 20 66 6c 61  and READONLY fla
e6e30 67 73 20 6d 75 73 74 20 62 65 20 73 65 74 2c 20  gs must be set, 
e6e40 61 6e 64 20 0a 20 20 2a 2a 20 20 20 28 62 29 20  and .  **   (b) 
e6e50 69 66 20 43 52 45 41 54 45 20 69 73 20 73 65 74  if CREATE is set
e6e60 2c 20 74 68 65 6e 20 52 45 41 44 57 52 49 54 45  , then READWRITE
e6e70 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65   must also be se
e6e80 74 2c 20 61 6e 64 0a 20 20 2a 2a 20 20 20 28 63  t, and.  **   (c
e6e90 29 20 69 66 20 45 58 43 4c 55 53 49 56 45 20 69  ) if EXCLUSIVE i
e6ea0 73 20 73 65 74 2c 20 74 68 65 6e 20 43 52 45 41  s set, then CREA
e6eb0 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20  TE must also be 
e6ec0 73 65 74 2e 0a 20 20 2a 2a 20 20 20 28 64 29 20  set..  **   (d) 
e6ed0 69 66 20 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  if DELETEONCLOSE
e6ee0 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 43 52   is set, then CR
e6ef0 45 41 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 62  EATE must also b
e6f00 65 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 20 61 73  e set..  */.  as
e6f10 73 65 72 74 28 28 69 73 52 65 61 64 6f 6e 6c 79  sert((isReadonly
e6f20 3d 3d 30 20 7c 7c 20 69 73 52 65 61 64 57 72 69  ==0 || isReadWri
e6f30 74 65 3d 3d 30 29 20 26 26 20 28 69 73 52 65 61  te==0) && (isRea
e6f40 64 57 72 69 74 65 20 7c 7c 20 69 73 52 65 61 64  dWrite || isRead
e6f50 6f 6e 6c 79 29 29 3b 0a 20 20 61 73 73 65 72 74  only));.  assert
e6f60 28 69 73 43 72 65 61 74 65 3d 3d 30 20 7c 7c 20  (isCreate==0 || 
e6f70 69 73 52 65 61 64 57 72 69 74 65 29 3b 0a 20 20  isReadWrite);.  
e6f80 61 73 73 65 72 74 28 69 73 45 78 63 6c 75 73 69  assert(isExclusi
e6f90 76 65 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74  ve==0 || isCreat
e6fa0 65 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 44  e);.  assert(isD
e6fb0 65 6c 65 74 65 3d 3d 30 20 7c 7c 20 69 73 43 72  elete==0 || isCr
e6fc0 65 61 74 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 65  eate);..  /* The
e6fd0 20 6d 61 69 6e 20 44 42 2c 20 6d 61 69 6e 20 6a   main DB, main j
e6fe0 6f 75 72 6e 61 6c 2c 20 61 6e 64 20 6d 61 73 74  ournal, and mast
e6ff0 65 72 20 6a 6f 75 72 6e 61 6c 20 61 72 65 20 6e  er journal are n
e7000 65 76 65 72 20 61 75 74 6f 6d 61 74 69 63 61 6c  ever automatical
e7010 6c 79 0a 20 20 2a 2a 20 64 65 6c 65 74 65 64 2e  ly.  ** deleted.
e7020 20 4e 6f 72 20 61 72 65 20 74 68 65 79 20 65 76   Nor are they ev
e7030 65 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  er temporary fil
e7040 65 73 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  es.  */.  assert
e7050 28 20 28 21 69 73 44 65 6c 65 74 65 20 26 26 20  ( (!isDelete && 
e7060 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21  zName) || eType!
e7070 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49  =SQLITE_OPEN_MAI
e7080 4e 5f 44 42 20 29 3b 0a 20 20 61 73 73 65 72 74  N_DB );.  assert
e7090 28 20 28 21 69 73 44 65 6c 65 74 65 20 26 26 20  ( (!isDelete && 
e70a0 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21  zName) || eType!
e70b0 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49  =SQLITE_OPEN_MAI
e70c0 4e 5f 4a 4f 55 52 4e 41 4c 20 29 3b 0a 20 20 61  N_JOURNAL );.  a
e70d0 73 73 65 72 74 28 20 28 21 69 73 44 65 6c 65 74  ssert( (!isDelet
e70e0 65 20 26 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65  e && zName) || e
e70f0 54 79 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45  Type!=SQLITE_OPE
e7100 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c  N_MASTER_JOURNAL
e7110 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74   );..  /* Assert
e7120 20 74 68 61 74 20 74 68 65 20 75 70 70 65 72 20   that the upper 
e7130 6c 61 79 65 72 20 68 61 73 20 73 65 74 20 6f 6e  layer has set on
e7140 65 20 6f 66 20 74 68 65 20 22 66 69 6c 65 2d 74  e of the "file-t
e7150 79 70 65 22 20 66 6c 61 67 73 2e 20 2a 2f 0a 20  ype" flags. */. 
e7160 20 61 73 73 65 72 74 28 20 65 54 79 70 65 3d 3d   assert( eType==
e7170 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
e7180 5f 44 42 20 20 20 20 20 20 7c 7c 20 65 54 79 70  _DB      || eTyp
e7190 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54  e==SQLITE_OPEN_T
e71a0 45 4d 50 5f 44 42 20 0a 20 20 20 20 20 20 20 7c  EMP_DB .       |
e71b0 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  | eType==SQLITE_
e71c0 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41  OPEN_MAIN_JOURNA
e71d0 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49  L || eType==SQLI
e71e0 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55  TE_OPEN_TEMP_JOU
e71f0 52 4e 41 4c 20 0a 20 20 20 20 20 20 20 7c 7c 20  RNAL .       || 
e7200 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50  eType==SQLITE_OP
e7210 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 20 20  EN_SUBJOURNAL   
e7220 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45  || eType==SQLITE
e7230 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55  _OPEN_MASTER_JOU
e7240 52 4e 41 4c 20 0a 20 20 20 20 20 20 20 7c 7c 20  RNAL .       || 
e7250 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50  eType==SQLITE_OP
e7260 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 0a  EN_TRANSIENT_DB.
e7270 20 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 70    );..  memset(p
e7280 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78  , 0, sizeof(unix
e7290 46 69 6c 65 29 29 3b 0a 0a 20 20 69 66 28 20 65  File));..  if( e
e72a0 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45  Type==SQLITE_OPE
e72b0 4e 5f 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20  N_MAIN_DB ){.   
e72c0 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70   UnixUnusedFd *p
e72d0 55 6e 75 73 65 64 3b 0a 20 20 20 20 70 55 6e 75  Unused;.    pUnu
e72e0 73 65 64 20 3d 20 66 69 6e 64 52 65 75 73 61 62  sed = findReusab
e72f0 6c 65 46 64 28 7a 4e 61 6d 65 2c 20 66 6c 61 67  leFd(zName, flag
e7300 73 29 3b 0a 20 20 20 20 69 66 28 20 70 55 6e 75  s);.    if( pUnu
e7310 73 65 64 20 29 7b 0a 20 20 20 20 20 20 66 64 20  sed ){.      fd 
e7320 3d 20 70 55 6e 75 73 65 64 2d 3e 66 64 3b 0a 20  = pUnused->fd;. 
e7330 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
e7340 70 55 6e 75 73 65 64 20 3d 20 73 71 6c 69 74 65  pUnused = sqlite
e7350 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
e7360 2a 70 55 6e 75 73 65 64 29 29 3b 0a 20 20 20 20  *pUnused));.    
e7370 20 20 69 66 28 20 21 70 55 6e 75 73 65 64 20 29    if( !pUnused )
e7380 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
e7390 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
e73a0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
e73b0 20 70 2d 3e 70 55 6e 75 73 65 64 20 3d 20 70 55   p->pUnused = pU
e73c0 6e 75 73 65 64 3b 0a 20 20 7d 65 6c 73 65 20 69  nused;.  }else i
e73d0 66 28 20 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 20  f( !zName ){.   
e73e0 20 2f 2a 20 49 66 20 7a 4e 61 6d 65 20 69 73 20   /* If zName is 
e73f0 4e 55 4c 4c 2c 20 74 68 65 20 75 70 70 65 72 20  NULL, the upper 
e7400 6c 61 79 65 72 20 69 73 20 72 65 71 75 65 73 74  layer is request
e7410 69 6e 67 20 61 20 74 65 6d 70 20 66 69 6c 65 2e  ing a temp file.
e7420 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 69   */.    assert(i
e7430 73 44 65 6c 65 74 65 20 26 26 20 21 69 73 4f 70  sDelete && !isOp
e7440 65 6e 44 69 72 65 63 74 6f 72 79 29 3b 0a 20 20  enDirectory);.  
e7450 20 20 72 63 20 3d 20 67 65 74 54 65 6d 70 6e 61    rc = getTempna
e7460 6d 65 28 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b  me(MAX_PATHNAME+
e7470 31 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20  1, zTmpname);.  
e7480 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
e7490 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74  _OK ){.      ret
e74a0 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
e74b0 20 20 7a 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e 61    zName = zTmpna
e74c0 6d 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65  me;.  }..  /* De
e74d0 74 65 72 6d 69 6e 65 20 74 68 65 20 76 61 6c 75  termine the valu
e74e0 65 20 6f 66 20 74 68 65 20 66 6c 61 67 73 20 70  e of the flags p
e74f0 61 72 61 6d 65 74 65 72 20 70 61 73 73 65 64 20  arameter passed 
e7500 74 6f 20 50 4f 53 49 58 20 66 75 6e 63 74 69 6f  to POSIX functio
e7510 6e 0a 20 20 2a 2a 20 6f 70 65 6e 28 29 2e 20 54  n.  ** open(). T
e7520 68 65 73 65 20 6d 75 73 74 20 62 65 20 63 61 6c  hese must be cal
e7530 63 75 6c 61 74 65 64 20 65 76 65 6e 20 69 66 20  culated even if 
e7540 6f 70 65 6e 28 29 20 69 73 20 6e 6f 74 20 63 61  open() is not ca
e7550 6c 6c 65 64 2c 20 61 73 0a 20 20 2a 2a 20 74 68  lled, as.  ** th
e7560 65 79 20 6d 61 79 20 62 65 20 73 74 6f 72 65 64  ey may be stored
e7570 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   as part of the 
e7580 66 69 6c 65 20 68 61 6e 64 6c 65 20 61 6e 64 20  file handle and 
e7590 75 73 65 64 20 62 79 20 74 68 65 20 0a 20 20 2a  used by the .  *
e75a0 2a 20 27 63 6f 6e 63 68 20 66 69 6c 65 27 20 6c  * 'conch file' l
e75b0 6f 63 6b 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73  ocking functions
e75c0 20 6c 61 74 65 72 20 6f 6e 2e 20 20 2a 2f 0a 20   later on.  */. 
e75d0 20 69 66 28 20 69 73 52 65 61 64 6f 6e 6c 79 20   if( isReadonly 
e75e0 29 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20  )  openFlags |= 
e75f0 4f 5f 52 44 4f 4e 4c 59 3b 0a 20 20 69 66 28 20  O_RDONLY;.  if( 
e7600 69 73 52 65 61 64 57 72 69 74 65 20 29 20 6f 70  isReadWrite ) op
e7610 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 57  enFlags |= O_RDW
e7620 52 3b 0a 20 20 69 66 28 20 69 73 43 72 65 61 74  R;.  if( isCreat
e7630 65 20 29 20 20 20 20 6f 70 65 6e 46 6c 61 67 73  e )    openFlags
e7640 20 7c 3d 20 4f 5f 43 52 45 41 54 3b 0a 20 20 69   |= O_CREAT;.  i
e7650 66 28 20 69 73 45 78 63 6c 75 73 69 76 65 20 29  f( isExclusive )
e7660 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f   openFlags |= (O
e7670 5f 45 58 43 4c 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57  _EXCL|O_NOFOLLOW
e7680 29 3b 0a 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c  );.  openFlags |
e7690 3d 20 28 4f 5f 4c 41 52 47 45 46 49 4c 45 7c 4f  = (O_LARGEFILE|O
e76a0 5f 42 49 4e 41 52 59 29 3b 0a 0a 20 20 69 66 28  _BINARY);..  if(
e76b0 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 6d 6f 64   fd<0 ){.    mod
e76c0 65 5f 74 20 6f 70 65 6e 4d 6f 64 65 20 3d 20 28  e_t openMode = (
e76d0 69 73 44 65 6c 65 74 65 3f 30 36 30 30 3a 53 51  isDelete?0600:SQ
e76e0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c  LITE_DEFAULT_FIL
e76f0 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a  E_PERMISSIONS);.
e7700 20 20 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a 4e      fd = open(zN
e7710 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 20  ame, openFlags, 
e7720 6f 70 65 6e 4d 6f 64 65 29 3b 0a 20 20 20 20 4f  openMode);.    O
e7730 53 54 52 41 43 45 34 28 22 4f 50 45 4e 58 20 20  STRACE4("OPENX  
e7740 20 25 2d 33 64 20 25 73 20 30 25 6f 5c 6e 22 2c   %-3d %s 0%o\n",
e7750 20 66 64 2c 20 7a 4e 61 6d 65 2c 20 6f 70 65 6e   fd, zName, open
e7760 46 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20  Flags);.    if( 
e7770 66 64 3c 30 20 26 26 20 65 72 72 6e 6f 21 3d 45  fd<0 && errno!=E
e7780 49 53 44 49 52 20 26 26 20 69 73 52 65 61 64 57  ISDIR && isReadW
e7790 72 69 74 65 20 26 26 20 21 69 73 45 78 63 6c 75  rite && !isExclu
e77a0 73 69 76 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a  sive ){.      /*
e77b0 20 46 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e 20   Failed to open 
e77c0 74 68 65 20 66 69 6c 65 20 66 6f 72 20 72 65 61  the file for rea
e77d0 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2e 20  d/write access. 
e77e0 54 72 79 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 2a  Try read-only. *
e77f0 2f 0a 20 20 20 20 20 20 66 6c 61 67 73 20 26 3d  /.      flags &=
e7800 20 7e 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   ~(SQLITE_OPEN_R
e7810 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f  EADWRITE|SQLITE_
e7820 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20 20  OPEN_CREATE);.  
e7830 20 20 20 20 6f 70 65 6e 46 6c 61 67 73 20 26 3d      openFlags &=
e7840 20 7e 28 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41   ~(O_RDWR|O_CREA
e7850 54 29 3b 0a 20 20 20 20 20 20 66 6c 61 67 73 20  T);.      flags 
e7860 7c 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  |= SQLITE_OPEN_R
e7870 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 6f  EADONLY;.      o
e7880 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44  penFlags |= O_RD
e7890 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 66 64 20 3d  ONLY;.      fd =
e78a0 20 6f 70 65 6e 28 7a 4e 61 6d 65 2c 20 6f 70 65   open(zName, ope
e78b0 6e 46 6c 61 67 73 2c 20 6f 70 65 6e 4d 6f 64 65  nFlags, openMode
e78c0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
e78d0 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 20 20 72   fd<0 ){.      r
e78e0 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  c = SQLITE_CANTO
e78f0 50 45 4e 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  PEN;.      goto 
e7900 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20  open_finished;. 
e7910 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72     }.  }.  asser
e7920 74 28 20 66 64 3e 3d 30 20 29 3b 0a 20 20 69 66  t( fd>=0 );.  if
e7930 28 20 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20  ( pOutFlags ){. 
e7940 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20     *pOutFlags = 
e7950 66 6c 61 67 73 3b 0a 20 20 7d 0a 0a 20 20 69 66  flags;.  }..  if
e7960 28 20 70 2d 3e 70 55 6e 75 73 65 64 20 29 7b 0a  ( p->pUnused ){.
e7970 20 20 20 20 70 2d 3e 70 55 6e 75 73 65 64 2d 3e      p->pUnused->
e7980 66 64 20 3d 20 66 64 3b 0a 20 20 20 20 70 2d 3e  fd = fd;.    p->
e7990 70 55 6e 75 73 65 64 2d 3e 66 6c 61 67 73 20 3d  pUnused->flags =
e79a0 20 66 6c 61 67 73 3b 0a 20 20 7d 0a 0a 20 20 69   flags;.  }..  i
e79b0 66 28 20 69 73 44 65 6c 65 74 65 20 29 7b 0a 23  f( isDelete ){.#
e79c0 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20  if OS_VXWORKS.  
e79d0 20 20 7a 50 61 74 68 20 3d 20 7a 4e 61 6d 65 3b    zPath = zName;
e79e0 0a 23 65 6c 73 65 0a 20 20 20 20 75 6e 6c 69 6e  .#else.    unlin
e79f0 6b 28 7a 4e 61 6d 65 29 3b 0a 23 65 6e 64 69 66  k(zName);.#endif
e7a00 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f  .  }.#if SQLITE_
e7a10 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
e7a20 54 59 4c 45 0a 20 20 65 6c 73 65 7b 0a 20 20 20  TYLE.  else{.   
e7a30 20 70 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 3d 20   p->openFlags = 
e7a40 6f 70 65 6e 46 6c 61 67 73 3b 0a 20 20 7d 0a 23  openFlags;.  }.#
e7a50 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 69 73 4f  endif..  if( isO
e7a60 70 65 6e 44 69 72 65 63 74 6f 72 79 20 29 7b 0a  penDirectory ){.
e7a70 20 20 20 20 72 63 20 3d 20 6f 70 65 6e 44 69 72      rc = openDir
e7a80 65 63 74 6f 72 79 28 7a 50 61 74 68 2c 20 26 64  ectory(zPath, &d
e7a90 69 72 66 64 29 3b 0a 20 20 20 20 69 66 28 20 72  irfd);.    if( r
e7aa0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
e7ab0 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73 20 73        /* It is s
e7ac0 61 66 65 20 74 6f 20 63 6c 6f 73 65 20 66 64 20  afe to close fd 
e7ad0 61 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 62  at this point, b
e7ae0 65 63 61 75 73 65 20 69 74 20 69 73 20 67 75 61  ecause it is gua
e7af0 72 61 6e 74 65 65 64 20 6e 6f 74 0a 20 20 20 20  ranteed not.    
e7b00 20 20 2a 2a 20 74 6f 20 62 65 20 6f 70 65 6e 20    ** to be open 
e7b10 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69  on a database fi
e7b20 6c 65 2e 20 49 66 20 69 74 20 77 65 72 65 20 6f  le. If it were o
e7b30 70 65 6e 20 6f 6e 20 61 20 64 61 74 61 62 61 73  pen on a databas
e7b40 65 20 66 69 6c 65 2c 0a 20 20 20 20 20 20 2a 2a  e file,.      **
e7b50 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65   it would not be
e7b60 20 73 61 66 65 20 74 6f 20 63 6c 6f 73 65 20 61   safe to close a
e7b70 73 20 74 68 69 73 20 77 6f 75 6c 64 20 72 65 6c  s this would rel
e7b80 65 61 73 65 20 61 6e 79 20 6c 6f 63 6b 73 20 68  ease any locks h
e7b90 65 6c 64 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 20  eld.      ** on 
e7ba0 74 68 65 20 66 69 6c 65 20 62 79 20 74 68 69 73  the file by this
e7bb0 20 70 72 6f 63 65 73 73 2e 20 20 2a 2f 0a 20 20   process.  */.  
e7bc0 20 20 20 20 61 73 73 65 72 74 28 20 65 54 79 70      assert( eTyp
e7bd0 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  e!=SQLITE_OPEN_M
e7be0 41 49 4e 5f 44 42 20 29 3b 0a 20 20 20 20 20 20  AIN_DB );.      
e7bf0 63 6c 6f 73 65 28 66 64 29 3b 20 20 20 20 20 20  close(fd);      
e7c00 20 20 20 20 20 20 20 2f 2a 20 73 69 6c 65 6e 74         /* silent
e7c10 6c 79 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c  ly leak if fail,
e7c20 20 61 6c 72 65 61 64 79 20 69 6e 20 65 72 72 6f   already in erro
e7c30 72 20 2a 2f 0a 20 20 20 20 20 20 67 6f 74 6f 20  r */.      goto 
e7c40 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20  open_finished;. 
e7c50 20 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 64 65 66     }.  }..#ifdef
e7c60 20 46 44 5f 43 4c 4f 45 58 45 43 0a 20 20 66 63   FD_CLOEXEC.  fc
e7c70 6e 74 6c 28 66 64 2c 20 46 5f 53 45 54 46 44 2c  ntl(fd, F_SETFD,
e7c80 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54   fcntl(fd, F_GET
e7c90 46 44 2c 20 30 29 20 7c 20 46 44 5f 43 4c 4f 45  FD, 0) | FD_CLOE
e7ca0 58 45 43 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  XEC);.#endif..  
e7cb0 6e 6f 4c 6f 63 6b 20 3d 20 65 54 79 70 65 21 3d  noLock = eType!=
e7cc0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
e7cd0 5f 44 42 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45  _DB;..#if SQLITE
e7ce0 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f  _PREFER_PROXY_LO
e7cf0 43 4b 49 4e 47 0a 20 20 69 66 28 20 7a 50 61 74  CKING.  if( zPat
e7d00 68 21 3d 4e 55 4c 4c 20 26 26 20 21 6e 6f 4c 6f  h!=NULL && !noLo
e7d10 63 6b 20 26 26 20 70 56 66 73 2d 3e 78 4f 70 65  ck && pVfs->xOpe
e7d20 6e 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 65  n ){.    char *e
e7d30 6e 76 66 6f 72 63 65 20 3d 20 67 65 74 65 6e 76  nvforce = getenv
e7d40 28 22 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50  ("SQLITE_FORCE_P
e7d50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47 22 29 3b 0a  ROXY_LOCKING");.
e7d60 20 20 20 20 69 6e 74 20 75 73 65 50 72 6f 78 79      int useProxy
e7d70 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 53 51   = 0;..    /* SQ
e7d80 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59  LITE_FORCE_PROXY
e7d90 5f 4c 4f 43 4b 49 4e 47 3d 3d 31 20 6d 65 61 6e  _LOCKING==1 mean
e7da0 73 20 66 6f 72 63 65 20 61 6c 77 61 79 73 20 75  s force always u
e7db0 73 65 20 70 72 6f 78 79 2c 20 30 20 6d 65 61 6e  se proxy, 0 mean
e7dc0 73 20 0a 20 20 20 20 2a 2a 20 6e 65 76 65 72 20  s .    ** never 
e7dd0 75 73 65 20 70 72 6f 78 79 2c 20 4e 55 4c 4c 20  use proxy, NULL 
e7de0 6d 65 61 6e 73 20 75 73 65 20 70 72 6f 78 79 20  means use proxy 
e7df0 66 6f 72 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69  for non-local fi
e7e00 6c 65 73 20 6f 6e 6c 79 2e 20 20 2a 2f 0a 20 20  les only.  */.  
e7e10 20 20 69 66 28 20 65 6e 76 66 6f 72 63 65 21 3d    if( envforce!=
e7e20 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 75 73  NULL ){.      us
e7e30 65 50 72 6f 78 79 20 3d 20 61 74 6f 69 28 65 6e  eProxy = atoi(en
e7e40 76 66 6f 72 63 65 29 3e 30 3b 0a 20 20 20 20 7d  vforce)>0;.    }
e7e50 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 74 72 75  else{.      stru
e7e60 63 74 20 73 74 61 74 66 73 20 66 73 49 6e 66 6f  ct statfs fsInfo
e7e70 3b 0a 20 20 20 20 20 20 69 66 28 20 73 74 61 74  ;.      if( stat
e7e80 66 73 28 7a 50 61 74 68 2c 20 26 66 73 49 6e 66  fs(zPath, &fsInf
e7e90 6f 29 20 3d 3d 20 2d 31 20 29 7b 0a 20 20 20 20  o) == -1 ){.    
e7ea0 20 20 20 20 2f 2a 20 49 6e 20 74 68 65 6f 72 79      /* In theory
e7eb0 2c 20 74 68 65 20 63 6c 6f 73 65 28 66 64 29 20  , the close(fd) 
e7ec0 63 61 6c 6c 20 69 73 20 73 75 62 2d 6f 70 74 69  call is sub-opti
e7ed0 6d 61 6c 2e 20 49 66 20 74 68 65 20 66 69 6c 65  mal. If the file
e7ee0 20 6f 70 65 6e 65 64 0a 20 20 20 20 20 20 20 20   opened.        
e7ef0 2a 2a 20 77 69 74 68 20 66 64 20 69 73 20 61 20  ** with fd is a 
e7f00 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 61  database file, a
e7f10 6e 64 20 74 68 65 72 65 20 61 72 65 20 6f 74 68  nd there are oth
e7f20 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f  er connections o
e7f30 70 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f  pen.        ** o
e7f40 6e 20 74 68 61 74 20 66 69 6c 65 20 74 68 61 74  n that file that
e7f50 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 68   are currently h
e7f60 6f 6c 64 69 6e 67 20 61 64 76 69 73 6f 72 79 20  olding advisory 
e7f70 6c 6f 63 6b 73 20 6f 6e 20 69 74 2c 0a 20 20 20  locks on it,.   
e7f80 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65       ** then the
e7f90 20 63 61 6c 6c 20 74 6f 20 63 6c 6f 73 65 28 29   call to close()
e7fa0 20 77 69 6c 6c 20 63 61 6e 63 65 6c 20 74 68 6f   will cancel tho
e7fb0 73 65 20 6c 6f 63 6b 73 2e 20 49 6e 20 70 72 61  se locks. In pra
e7fc0 63 74 69 63 65 2c 0a 20 20 20 20 20 20 20 20 2a  ctice,.        *
e7fd0 2a 20 77 65 27 72 65 20 61 73 73 75 6d 69 6e 67  * we're assuming
e7fe0 20 74 68 61 74 20 73 74 61 74 66 73 28 29 20 64   that statfs() d
e7ff0 6f 65 73 6e 27 74 20 66 61 69 6c 20 76 65 72 79  oesn't fail very
e8000 20 6f 66 74 65 6e 2e 20 41 74 20 6c 65 61 73 74   often. At least
e8010 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 6f 74 20  .        ** not 
e8020 77 68 69 6c 65 20 6f 74 68 65 72 20 66 69 6c 65  while other file
e8030 20 64 65 73 63 72 69 70 74 6f 72 73 20 6f 70 65   descriptors ope
e8040 6e 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20  ned by the same 
e8050 70 72 6f 63 65 73 73 20 6f 6e 0a 20 20 20 20 20  process on.     
e8060 20 20 20 2a 2a 20 74 68 65 20 73 61 6d 65 20 66     ** the same f
e8070 69 6c 65 20 61 72 65 20 77 6f 72 6b 69 6e 67 2e  ile are working.
e8080 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 2d 3e    */.        p->
e8090 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  lastErrno = errn
e80a0 6f 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 64  o;.        if( d
e80b0 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 20  irfd>=0 ){.     
e80c0 20 20 20 20 20 63 6c 6f 73 65 28 64 69 72 66 64       close(dirfd
e80d0 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c  ); /* silently l
e80e0 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 69 6e 20  eak if fail, in 
e80f0 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20  error */.       
e8100 20 7d 0a 20 20 20 20 20 20 20 20 63 6c 6f 73 65   }.        close
e8110 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c  (fd); /* silentl
e8120 79 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20  y leak if fail, 
e8130 69 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20  in error */.    
e8140 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
e8150 49 4f 45 52 52 5f 41 43 43 45 53 53 3b 0a 20 20  IOERR_ACCESS;.  
e8160 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f        goto open_
e8170 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 20 20  finished;.      
e8180 7d 0a 20 20 20 20 20 20 75 73 65 50 72 6f 78 79  }.      useProxy
e8190 20 3d 20 21 28 66 73 49 6e 66 6f 2e 66 5f 66 6c   = !(fsInfo.f_fl
e81a0 61 67 73 26 4d 4e 54 5f 4c 4f 43 41 4c 29 3b 0a  ags&MNT_LOCAL);.
e81b0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 75 73      }.    if( us
e81c0 65 50 72 6f 78 79 20 29 7b 0a 20 20 20 20 20 20  eProxy ){.      
e81d0 72 63 20 3d 20 66 69 6c 6c 49 6e 55 6e 69 78 46  rc = fillInUnixF
e81e0 69 6c 65 28 70 56 66 73 2c 20 66 64 2c 20 64 69  ile(pVfs, fd, di
e81f0 72 66 64 2c 20 70 46 69 6c 65 2c 20 7a 50 61 74  rfd, pFile, zPat
e8200 68 2c 20 6e 6f 4c 6f 63 6b 2c 20 69 73 44 65 6c  h, noLock, isDel
e8210 65 74 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ete);.      if( 
e8220 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
e8230 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 72  .        rc = pr
e8240 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78  oxyTransformUnix
e8250 46 69 6c 65 28 28 75 6e 69 78 46 69 6c 65 2a 29  File((unixFile*)
e8260 70 46 69 6c 65 2c 20 22 3a 61 75 74 6f 3a 22 29  pFile, ":auto:")
e8270 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
e8280 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73 68  goto open_finish
e8290 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  ed;.    }.  }.#e
e82a0 6e 64 69 66 0a 20 20 0a 20 20 72 63 20 3d 20 66  ndif.  .  rc = f
e82b0 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 70 56  illInUnixFile(pV
e82c0 66 73 2c 20 66 64 2c 20 64 69 72 66 64 2c 20 70  fs, fd, dirfd, p
e82d0 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20 6e 6f 4c  File, zPath, noL
e82e0 6f 63 6b 2c 20 69 73 44 65 6c 65 74 65 29 3b 0a  ock, isDelete);.
e82f0 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 3a 0a 20  open_finished:. 
e8300 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
e8310 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
e8320 33 5f 66 72 65 65 28 70 2d 3e 70 55 6e 75 73 65  3_free(p->pUnuse
e8330 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  d);.  }.  return
e8340 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44   rc;.}.../*.** D
e8350 65 6c 65 74 65 20 74 68 65 20 66 69 6c 65 20 61  elete the file a
e8360 74 20 7a 50 61 74 68 2e 20 49 66 20 74 68 65 20  t zPath. If the 
e8370 64 69 72 53 79 6e 63 20 61 72 67 75 6d 65 6e 74  dirSync argument
e8380 20 69 73 20 74 72 75 65 2c 20 66 73 79 6e 63 28   is true, fsync(
e8390 29 0a 2a 2a 20 74 68 65 20 64 69 72 65 63 74 6f  ).** the directo
e83a0 72 79 20 61 66 74 65 72 20 64 65 6c 65 74 69 6e  ry after deletin
e83b0 67 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73  g the file..*/.s
e83c0 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65  tatic int unixDe
e83d0 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  lete(.  sqlite3_
e83e0 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 20 20  vfs *NotUsed,   
e83f0 20 20 2f 2a 20 56 46 53 20 63 6f 6e 74 61 69 6e    /* VFS contain
e8400 69 6e 67 20 74 68 69 73 20 61 73 20 74 68 65 20  ing this as the 
e8410 78 44 65 6c 65 74 65 20 6d 65 74 68 6f 64 20 2a  xDelete method *
e8420 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
e8430 7a 50 61 74 68 2c 20 20 20 20 20 20 20 20 2f 2a  zPath,        /*
e8440 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f   Name of file to
e8450 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20   be deleted */. 
e8460 20 69 6e 74 20 64 69 72 53 79 6e 63 20 20 20 20   int dirSync    
e8470 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
e8480 20 74 72 75 65 2c 20 66 73 79 6e 63 28 29 20 64   true, fsync() d
e8490 69 72 65 63 74 6f 72 79 20 61 66 74 65 72 20 64  irectory after d
e84a0 65 6c 65 74 69 6e 67 20 66 69 6c 65 20 2a 2f 0a  eleting file */.
e84b0 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
e84c0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 55 4e 55 53 45  LITE_OK;.  UNUSE
e84d0 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
e84e0 73 65 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65  sed);.  Simulate
e84f0 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53  IOError(return S
e8500 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45  QLITE_IOERR_DELE
e8510 54 45 29 3b 0a 20 20 75 6e 6c 69 6e 6b 28 7a 50  TE);.  unlink(zP
e8520 61 74 68 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  ath);.#ifndef SQ
e8530 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 52  LITE_DISABLE_DIR
e8540 53 59 4e 43 0a 20 20 69 66 28 20 64 69 72 53 79  SYNC.  if( dirSy
e8550 6e 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 64  nc ){.    int fd
e8560 3b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e 44  ;.    rc = openD
e8570 69 72 65 63 74 6f 72 79 28 7a 50 61 74 68 2c 20  irectory(zPath, 
e8580 26 66 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63  &fd);.    if( rc
e8590 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23  ==SQLITE_OK ){.#
e85a0 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20  if OS_VXWORKS.  
e85b0 20 20 20 20 69 66 28 20 66 73 79 6e 63 28 66 64      if( fsync(fd
e85c0 29 3d 3d 2d 31 20 29 0a 23 65 6c 73 65 0a 20 20  )==-1 ).#else.  
e85d0 20 20 20 20 69 66 28 20 66 73 79 6e 63 28 66 64      if( fsync(fd
e85e0 29 20 29 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  ) ).#endif.     
e85f0 20 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20   {.        rc = 
e8600 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52  SQLITE_IOERR_DIR
e8610 5f 46 53 59 4e 43 3b 0a 20 20 20 20 20 20 7d 0a  _FSYNC;.      }.
e8620 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 28        if( close(
e8630 66 64 29 26 26 21 72 63 20 29 7b 0a 20 20 20 20  fd)&&!rc ){.    
e8640 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
e8650 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b  IOERR_DIR_CLOSE;
e8660 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
e8670 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75   }.#endif.  retu
e8680 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
e8690 54 65 73 74 20 74 68 65 20 65 78 69 73 74 61 6e  Test the existan
e86a0 63 65 20 6f 66 20 6f 72 20 61 63 63 65 73 73 20  ce of or access 
e86b0 70 65 72 6d 69 73 73 69 6f 6e 73 20 6f 66 20 66  permissions of f
e86c0 69 6c 65 20 7a 50 61 74 68 2e 20 54 68 65 0a 2a  ile zPath. The.*
e86d0 2a 20 74 65 73 74 20 70 65 72 66 6f 72 6d 65 64  * test performed
e86e0 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20   depends on the 
e86f0 76 61 6c 75 65 20 6f 66 20 66 6c 61 67 73 3a 0a  value of flags:.
e8700 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  **.**     SQLITE
e8710 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 20  _ACCESS_EXISTS: 
e8720 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  Return 1 if the 
e8730 66 69 6c 65 20 65 78 69 73 74 73 0a 2a 2a 20 20  file exists.**  
e8740 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53     SQLITE_ACCESS
e8750 5f 52 45 41 44 57 52 49 54 45 3a 20 52 65 74 75  _READWRITE: Retu
e8760 72 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65  rn 1 if the file
e8770 20 69 73 20 72 65 61 64 20 61 6e 64 20 77 72 69   is read and wri
e8780 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 20 53 51  table..**     SQ
e8790 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
e87a0 4f 4e 4c 59 3a 20 52 65 74 75 72 6e 20 31 20 69  ONLY: Return 1 i
e87b0 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 72 65  f the file is re
e87c0 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74  adable..**.** Ot
e87d0 68 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 30  herwise return 0
e87e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e87f0 75 6e 69 78 41 63 63 65 73 73 28 0a 20 20 73 71  unixAccess(.  sq
e8800 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73  lite3_vfs *NotUs
e8810 65 64 2c 20 20 20 2f 2a 20 54 68 65 20 56 46 53  ed,   /* The VFS
e8820 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73   containing this
e8830 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20   xAccess method 
e8840 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
e8850 2a 7a 50 61 74 68 2c 20 20 20 20 20 20 2f 2a 20  *zPath,      /* 
e8860 50 61 74 68 20 6f 66 20 74 68 65 20 66 69 6c 65  Path of the file
e8870 20 74 6f 20 65 78 61 6d 69 6e 65 20 2a 2f 0a 20   to examine */. 
e8880 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20   int flags,     
e8890 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74           /* What
e88a0 20 64 6f 20 77 65 20 77 61 6e 74 20 74 6f 20 6c   do we want to l
e88b0 65 61 72 6e 20 61 62 6f 75 74 20 74 68 65 20 7a  earn about the z
e88c0 50 61 74 68 20 66 69 6c 65 3f 20 2a 2f 0a 20 20  Path file? */.  
e88d0 69 6e 74 20 2a 70 52 65 73 4f 75 74 20 20 20 20  int *pResOut    
e88e0 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
e88f0 20 72 65 73 75 6c 74 20 62 6f 6f 6c 65 61 6e 20   result boolean 
e8900 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  here */.){.  int
e8910 20 61 6d 6f 64 65 20 3d 20 30 3b 0a 20 20 55 4e   amode = 0;.  UN
e8920 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
e8930 6f 74 55 73 65 64 29 3b 0a 20 20 53 69 6d 75 6c  otUsed);.  Simul
e8940 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75  ateIOError( retu
e8950 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
e8960 41 43 43 45 53 53 3b 20 29 3b 0a 20 20 73 77 69  ACCESS; );.  swi
e8970 74 63 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 20  tch( flags ){.  
e8980 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43    case SQLITE_AC
e8990 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 20 20  CESS_EXISTS:.   
e89a0 20 20 20 61 6d 6f 64 65 20 3d 20 46 5f 4f 4b 3b     amode = F_OK;
e89b0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
e89c0 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43    case SQLITE_AC
e89d0 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a 0a  CESS_READWRITE:.
e89e0 20 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 57 5f        amode = W_
e89f0 4f 4b 7c 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 62  OK|R_OK;.      b
e8a00 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
e8a10 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
e8a20 44 3a 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 3d  D:.      amode =
e8a30 20 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65   R_OK;.      bre
e8a40 61 6b 3b 0a 0a 20 20 20 20 64 65 66 61 75 6c 74  ak;..    default
e8a50 3a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 21  :.      assert(!
e8a60 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61  "Invalid flags a
e8a70 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20  rgument");.  }. 
e8a80 20 2a 70 52 65 73 4f 75 74 20 3d 20 28 61 63 63   *pResOut = (acc
e8a90 65 73 73 28 7a 50 61 74 68 2c 20 61 6d 6f 64 65  ess(zPath, amode
e8aa0 29 3d 3d 30 29 3b 0a 20 20 72 65 74 75 72 6e 20  )==0);.  return 
e8ab0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
e8ac0 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61  *.** Turn a rela
e8ad0 74 69 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e  tive pathname in
e8ae0 74 6f 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61  to a full pathna
e8af0 6d 65 2e 20 54 68 65 20 72 65 6c 61 74 69 76 65  me. The relative
e8b00 20 70 61 74 68 0a 2a 2a 20 69 73 20 73 74 6f 72   path.** is stor
e8b10 65 64 20 61 73 20 61 20 6e 75 6c 2d 74 65 72 6d  ed as a nul-term
e8b20 69 6e 61 74 65 64 20 73 74 72 69 6e 67 20 69 6e  inated string in
e8b30 20 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e   the buffer poin
e8b40 74 65 64 20 74 6f 20 62 79 0a 2a 2a 20 7a 50 61  ted to by.** zPa
e8b50 74 68 2e 20 0a 2a 2a 0a 2a 2a 20 7a 4f 75 74 20  th. .**.** zOut 
e8b60 70 6f 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66  points to a buff
e8b70 65 72 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73  er of at least s
e8b80 71 6c 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74  qlite3_vfs.mxPat
e8b90 68 6e 61 6d 65 20 62 79 74 65 73 20 0a 2a 2a 20  hname bytes .** 
e8ba0 28 69 6e 20 74 68 69 73 20 63 61 73 65 2c 20 4d  (in this case, M
e8bb0 41 58 5f 50 41 54 48 4e 41 4d 45 20 62 79 74 65  AX_PATHNAME byte
e8bc0 73 29 2e 20 54 68 65 20 66 75 6c 6c 2d 70 61 74  s). The full-pat
e8bd0 68 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 0a  h is written to.
e8be0 2a 2a 20 74 68 69 73 20 62 75 66 66 65 72 20 62  ** this buffer b
e8bf0 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e  efore returning.
e8c00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
e8c10 6e 69 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28  nixFullPathname(
e8c20 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
e8c30 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20  pVfs,           
e8c40 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76   /* Pointer to v
e8c50 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63  fs object */.  c
e8c60 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68  onst char *zPath
e8c70 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
e8c80 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76  Possibly relativ
e8c90 65 20 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a  e input path */.
e8ca0 20 20 69 6e 74 20 6e 4f 75 74 2c 20 20 20 20 20    int nOut,     
e8cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8cc0 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75  /* Size of outpu
e8cd0 74 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65  t buffer in byte
e8ce0 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 75  s */.  char *zOu
e8cf0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
e8d00 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62       /* Output b
e8d10 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 0a 20 20 2f  uffer */.){..  /
e8d20 2a 20 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69  * It's odd to si
e8d30 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72  mulate an io-err
e8d40 6f 72 20 68 65 72 65 2c 20 62 75 74 20 72 65 61  or here, but rea
e8d50 6c 6c 79 20 74 68 69 73 20 69 73 20 6a 75 73 74  lly this is just
e8d60 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20  .  ** using the 
e8d70 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74  io-error infrast
e8d80 72 75 63 74 75 72 65 20 74 6f 20 74 65 73 74 20  ructure to test 
e8d90 74 68 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64  that SQLite hand
e8da0 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75  les this.  ** fu
e8db0 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20  nction failing. 
e8dc0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f  This function co
e8dd0 75 6c 64 20 66 61 69 6c 20 69 66 2c 20 66 6f 72  uld fail if, for
e8de0 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 0a 20 20   example, the.  
e8df0 2a 2a 20 63 75 72 72 65 6e 74 20 77 6f 72 6b 69  ** current worki
e8e00 6e 67 20 64 69 72 65 63 74 6f 72 79 20 68 61 73  ng directory has
e8e10 20 62 65 65 6e 20 75 6e 6c 69 6e 6b 65 64 2e 0a   been unlinked..
e8e20 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49    */.  SimulateI
e8e30 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53  OError( return S
e8e40 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a 0a  QLITE_ERROR );..
e8e50 20 20 61 73 73 65 72 74 28 20 70 56 66 73 2d 3e    assert( pVfs->
e8e60 6d 78 50 61 74 68 6e 61 6d 65 3d 3d 4d 41 58 5f  mxPathname==MAX_
e8e70 50 41 54 48 4e 41 4d 45 20 29 3b 0a 20 20 55 4e  PATHNAME );.  UN
e8e80 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70  USED_PARAMETER(p
e8e90 56 66 73 29 3b 0a 0a 20 20 7a 4f 75 74 5b 6e 4f  Vfs);..  zOut[nO
e8ea0 75 74 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  ut-1] = '\0';.  
e8eb0 69 66 28 20 7a 50 61 74 68 5b 30 5d 3d 3d 27 2f  if( zPath[0]=='/
e8ec0 27 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ' ){.    sqlite3
e8ed0 5f 73 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2c 20  _snprintf(nOut, 
e8ee0 7a 4f 75 74 2c 20 22 25 73 22 2c 20 7a 50 61 74  zOut, "%s", zPat
e8ef0 68 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  h);.  }else{.   
e8f00 20 69 6e 74 20 6e 43 77 64 3b 0a 20 20 20 20 69   int nCwd;.    i
e8f10 66 28 20 67 65 74 63 77 64 28 7a 4f 75 74 2c 20  f( getcwd(zOut, 
e8f20 6e 4f 75 74 2d 31 29 3d 3d 30 20 29 7b 0a 20 20  nOut-1)==0 ){.  
e8f30 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
e8f40 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20  E_CANTOPEN;.    
e8f50 7d 0a 20 20 20 20 6e 43 77 64 20 3d 20 28 69 6e  }.    nCwd = (in
e8f60 74 29 73 74 72 6c 65 6e 28 7a 4f 75 74 29 3b 0a  t)strlen(zOut);.
e8f70 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
e8f80 69 6e 74 66 28 6e 4f 75 74 2d 6e 43 77 64 2c 20  intf(nOut-nCwd, 
e8f90 26 7a 4f 75 74 5b 6e 43 77 64 5d 2c 20 22 2f 25  &zOut[nCwd], "/%
e8fa0 73 22 2c 20 7a 50 61 74 68 29 3b 0a 20 20 7d 0a  s", zPath);.  }.
e8fb0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e8fc0 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20  OK;.}...#ifndef 
e8fd0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44  SQLITE_OMIT_LOAD
e8fe0 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a  _EXTENSION./*.**
e8ff0 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20   Interfaces for 
e9000 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64  opening a shared
e9010 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e   library, findin
e9020 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a  g entry points.*
e9030 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61  * within the sha
e9040 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64  red library, and
e9050 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61   closing the sha
e9060 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a  red library..*/.
e9070 23 69 6e 63 6c 75 64 65 20 3c 64 6c 66 63 6e 2e  #include <dlfcn.
e9080 68 3e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  h>.static void *
e9090 75 6e 69 78 44 6c 4f 70 65 6e 28 73 71 6c 69 74  unixDlOpen(sqlit
e90a0 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c  e3_vfs *NotUsed,
e90b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
e90c0 6c 65 6e 61 6d 65 29 7b 0a 20 20 55 4e 55 53 45  lename){.  UNUSE
e90d0 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
e90e0 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 64  sed);.  return d
e90f0 6c 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c  lopen(zFilename,
e9100 20 52 54 4c 44 5f 4e 4f 57 20 7c 20 52 54 4c 44   RTLD_NOW | RTLD
e9110 5f 47 4c 4f 42 41 4c 29 3b 0a 7d 0a 0a 2f 2a 0a  _GLOBAL);.}../*.
e9120 2a 2a 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20  ** SQLite calls 
e9130 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d  this function im
e9140 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20  mediately after 
e9150 61 20 63 61 6c 6c 20 74 6f 20 75 6e 69 78 44 6c  a call to unixDl
e9160 53 79 6d 28 29 20 6f 72 0a 2a 2a 20 75 6e 69 78  Sym() or.** unix
e9170 44 6c 4f 70 65 6e 28 29 20 66 61 69 6c 73 20 28  DlOpen() fails (
e9180 72 65 74 75 72 6e 73 20 61 20 6e 75 6c 6c 20 70  returns a null p
e9190 6f 69 6e 74 65 72 29 2e 20 49 66 20 61 20 6d 6f  ointer). If a mo
e91a0 72 65 20 64 65 74 61 69 6c 65 64 20 65 72 72 6f  re detailed erro
e91b0 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 69 73 20  r.** message is 
e91c0 61 76 61 69 6c 61 62 6c 65 2c 20 69 74 20 69 73  available, it is
e91d0 20 77 72 69 74 74 65 6e 20 74 6f 20 7a 42 75 66   written to zBuf
e91e0 4f 75 74 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72  Out. If no error
e91f0 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 61   message.** is a
e9200 76 61 69 6c 61 62 6c 65 2c 20 7a 42 75 66 4f 75  vailable, zBufOu
e9210 74 20 69 73 20 6c 65 66 74 20 75 6e 6d 6f 64 69  t is left unmodi
e9220 66 69 65 64 20 61 6e 64 20 53 51 4c 69 74 65 20  fied and SQLite 
e9230 75 73 65 73 20 61 20 64 65 66 61 75 6c 74 0a 2a  uses a default.*
e9240 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e  * error message.
e9250 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
e9260 75 6e 69 78 44 6c 45 72 72 6f 72 28 73 71 6c 69  unixDlError(sqli
e9270 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64  te3_vfs *NotUsed
e9280 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72  , int nBuf, char
e9290 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 63 68   *zBufOut){.  ch
e92a0 61 72 20 2a 7a 45 72 72 3b 0a 20 20 55 4e 55 53  ar *zErr;.  UNUS
e92b0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
e92c0 55 73 65 64 29 3b 0a 20 20 75 6e 69 78 45 6e 74  Used);.  unixEnt
e92d0 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 7a 45 72  erMutex();.  zEr
e92e0 72 20 3d 20 64 6c 65 72 72 6f 72 28 29 3b 0a 20  r = dlerror();. 
e92f0 20 69 66 28 20 7a 45 72 72 20 29 7b 0a 20 20 20   if( zErr ){.   
e9300 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
e9310 66 28 6e 42 75 66 2c 20 7a 42 75 66 4f 75 74 2c  f(nBuf, zBufOut,
e9320 20 22 25 73 22 2c 20 7a 45 72 72 29 3b 0a 20 20   "%s", zErr);.  
e9330 7d 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74  }.  unixLeaveMut
e9340 65 78 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76  ex();.}.static v
e9350 6f 69 64 20 28 2a 75 6e 69 78 44 6c 53 79 6d 28  oid (*unixDlSym(
e9360 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74  sqlite3_vfs *Not
e9370 55 73 65 64 2c 20 76 6f 69 64 20 2a 70 2c 20 63  Used, void *p, c
e9380 6f 6e 73 74 20 63 68 61 72 2a 7a 53 79 6d 29 29  onst char*zSym))
e9390 28 76 6f 69 64 29 7b 0a 20 20 2f 2a 20 0a 20 20  (void){.  /* .  
e93a0 2a 2a 20 47 43 43 20 77 69 74 68 20 2d 70 65 64  ** GCC with -ped
e93b0 61 6e 74 69 63 2d 65 72 72 6f 72 73 20 73 61 79  antic-errors say
e93c0 73 20 74 68 61 74 20 43 39 30 20 64 6f 65 73 20  s that C90 does 
e93d0 6e 6f 74 20 61 6c 6c 6f 77 20 61 20 76 6f 69 64  not allow a void
e93e0 2a 20 74 6f 20 62 65 0a 20 20 2a 2a 20 63 61 73  * to be.  ** cas
e93f0 74 20 69 6e 74 6f 20 61 20 70 6f 69 6e 74 65 72  t into a pointer
e9400 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20   to a function. 
e9410 20 41 6e 64 20 79 65 74 20 74 68 65 20 6c 69 62   And yet the lib
e9420 72 61 72 79 20 64 6c 73 79 6d 28 29 20 72 6f 75  rary dlsym() rou
e9430 74 69 6e 65 0a 20 20 2a 2a 20 72 65 74 75 72 6e  tine.  ** return
e9440 73 20 61 20 76 6f 69 64 2a 20 77 68 69 63 68 20  s a void* which 
e9450 69 73 20 72 65 61 6c 6c 79 20 61 20 70 6f 69 6e  is really a poin
e9460 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  ter to a functio
e9470 6e 2e 20 20 53 6f 20 68 6f 77 20 64 6f 20 77 65  n.  So how do we
e9480 0a 20 20 2a 2a 20 75 73 65 20 64 6c 73 79 6d 28  .  ** use dlsym(
e9490 29 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63  ) with -pedantic
e94a0 2d 65 72 72 6f 72 73 3f 0a 20 20 2a 2a 0a 20 20  -errors?.  **.  
e94b0 2a 2a 20 56 61 72 69 61 62 6c 65 20 78 20 62 65  ** Variable x be
e94c0 6c 6f 77 20 69 73 20 64 65 66 69 6e 65 64 20 74  low is defined t
e94d0 6f 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74  o be a pointer t
e94e0 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b  o a function tak
e94f0 69 6e 67 0a 20 20 2a 2a 20 70 61 72 61 6d 65 74  ing.  ** paramet
e9500 65 72 73 20 76 6f 69 64 2a 20 61 6e 64 20 63 6f  ers void* and co
e9510 6e 73 74 20 63 68 61 72 2a 20 61 6e 64 20 72 65  nst char* and re
e9520 74 75 72 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65  turning a pointe
e9530 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e  r to a function.
e9540 0a 20 20 2a 2a 20 57 65 20 69 6e 69 74 69 61 6c  .  ** We initial
e9550 69 7a 65 20 78 20 62 79 20 61 73 73 69 67 6e 69  ize x by assigni
e9560 6e 67 20 69 74 20 61 20 70 6f 69 6e 74 65 72 20  ng it a pointer 
e9570 74 6f 20 74 68 65 20 64 6c 73 79 6d 28 29 20 66  to the dlsym() f
e9580 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 28 54  unction..  ** (T
e9590 68 61 74 20 61 73 73 69 67 6e 6d 65 6e 74 20 72  hat assignment r
e95a0 65 71 75 69 72 65 73 20 61 20 63 61 73 74 2e 29  equires a cast.)
e95b0 20 20 54 68 65 6e 20 77 65 20 63 61 6c 6c 20 74    Then we call t
e95c0 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74  he function that
e95d0 0a 20 20 2a 2a 20 78 20 70 6f 69 6e 74 73 20 74  .  ** x points t
e95e0 6f 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  o.  .  **.  ** T
e95f0 68 69 73 20 77 6f 72 6b 2d 61 72 6f 75 6e 64 20  his work-around 
e9600 69 73 20 75 6e 6c 69 6b 65 6c 79 20 74 6f 20 77  is unlikely to w
e9610 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e  ork correctly on
e9620 20 61 6e 79 20 73 79 73 74 65 6d 20 77 68 65 72   any system wher
e9630 65 0a 20 20 2a 2a 20 79 6f 75 20 72 65 61 6c 6c  e.  ** you reall
e9640 79 20 63 61 6e 6e 6f 74 20 63 61 73 74 20 61 20  y cannot cast a 
e9650 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72  function pointer
e9660 20 69 6e 74 6f 20 76 6f 69 64 2a 2e 20 20 42 75   into void*.  Bu
e9670 74 20 74 68 65 6e 2c 20 6f 6e 20 74 68 65 0a 20  t then, on the. 
e9680 20 2a 2a 20 6f 74 68 65 72 20 68 61 6e 64 2c 20   ** other hand, 
e9690 64 6c 73 79 6d 28 29 20 77 69 6c 6c 20 6e 6f 74  dlsym() will not
e96a0 20 77 6f 72 6b 20 6f 6e 20 73 75 63 68 20 61 20   work on such a 
e96b0 73 79 73 74 65 6d 20 65 69 74 68 65 72 2c 20 73  system either, s
e96c0 6f 20 77 65 20 68 61 76 65 0a 20 20 2a 2a 20 6e  o we have.  ** n
e96d0 6f 74 20 72 65 61 6c 6c 79 20 6c 6f 73 74 20 61  ot really lost a
e96e0 6e 79 74 68 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20  nything..  */.  
e96f0 76 6f 69 64 20 28 2a 28 2a 78 29 28 76 6f 69 64  void (*(*x)(void
e9700 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 29 28  *,const char*))(
e9710 76 6f 69 64 29 3b 0a 20 20 55 4e 55 53 45 44 5f  void);.  UNUSED_
e9720 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
e9730 64 29 3b 0a 20 20 78 20 3d 20 28 76 6f 69 64 28  d);.  x = (void(
e9740 2a 28 2a 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74  *(*)(void*,const
e9750 20 63 68 61 72 2a 29 29 28 76 6f 69 64 29 29 64   char*))(void))d
e9760 6c 73 79 6d 3b 0a 20 20 72 65 74 75 72 6e 20 28  lsym;.  return (
e9770 2a 78 29 28 70 2c 20 7a 53 79 6d 29 3b 0a 7d 0a  *x)(p, zSym);.}.
e9780 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78  static void unix
e9790 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  DlClose(sqlite3_
e97a0 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 76 6f  vfs *NotUsed, vo
e97b0 69 64 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20  id *pHandle){.  
e97c0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
e97d0 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 64 6c 63  (NotUsed);.  dlc
e97e0 6c 6f 73 65 28 70 48 61 6e 64 6c 65 29 3b 0a 7d  lose(pHandle);.}
e97f0 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c  .#else /* if SQL
e9800 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  ITE_OMIT_LOAD_EX
e9810 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e  TENSION is defin
e9820 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65  ed: */.  #define
e9830 20 75 6e 69 78 44 6c 4f 70 65 6e 20 20 30 0a 20   unixDlOpen  0. 
e9840 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 45   #define unixDlE
e9850 72 72 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65  rror 0.  #define
e9860 20 75 6e 69 78 44 6c 53 79 6d 20 20 20 30 0a 20   unixDlSym   0. 
e9870 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 43   #define unixDlC
e9880 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a 2f  lose 0.#endif../
e9890 2a 0a 2a 2a 20 57 72 69 74 65 20 6e 42 75 66 20  *.** Write nBuf 
e98a0 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 20  bytes of random 
e98b0 64 61 74 61 20 74 6f 20 74 68 65 20 73 75 70 70  data to the supp
e98c0 6c 69 65 64 20 62 75 66 66 65 72 20 7a 42 75 66  lied buffer zBuf
e98d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e98e0 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 73 28 73  unixRandomness(s
e98f0 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55  qlite3_vfs *NotU
e9900 73 65 64 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63  sed, int nBuf, c
e9910 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 55 4e  har *zBuf){.  UN
e9920 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
e9930 6f 74 55 73 65 64 29 3b 0a 20 20 61 73 73 65 72  otUsed);.  asser
e9940 74 28 28 73 69 7a 65 5f 74 29 6e 42 75 66 3e 3d  t((size_t)nBuf>=
e9950 28 73 69 7a 65 6f 66 28 74 69 6d 65 5f 74 29 2b  (sizeof(time_t)+
e9960 73 69 7a 65 6f 66 28 69 6e 74 29 29 29 3b 0a 0a  sizeof(int)));..
e9970 20 20 2f 2a 20 57 65 20 68 61 76 65 20 74 6f 20    /* We have to 
e9980 69 6e 69 74 69 61 6c 69 7a 65 20 7a 42 75 66 20  initialize zBuf 
e9990 74 6f 20 70 72 65 76 65 6e 74 20 76 61 6c 67 72  to prevent valgr
e99a0 69 6e 64 20 66 72 6f 6d 20 72 65 70 6f 72 74 69  ind from reporti
e99b0 6e 67 0a 20 20 2a 2a 20 65 72 72 6f 72 73 2e 20  ng.  ** errors. 
e99c0 20 54 68 65 20 72 65 70 6f 72 74 73 20 69 73 73   The reports iss
e99d0 75 65 64 20 62 79 20 76 61 6c 67 72 69 6e 64 20  ued by valgrind 
e99e0 61 72 65 20 69 6e 63 6f 72 72 65 63 74 20 2d 20  are incorrect - 
e99f0 77 65 20 77 6f 75 6c 64 0a 20 20 2a 2a 20 70 72  we would.  ** pr
e9a00 65 66 65 72 20 74 68 61 74 20 74 68 65 20 72 61  efer that the ra
e9a10 6e 64 6f 6d 6e 65 73 73 20 62 65 20 69 6e 63 72  ndomness be incr
e9a20 65 61 73 65 64 20 62 79 20 6d 61 6b 69 6e 67 20  eased by making 
e9a30 75 73 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  use of the.  ** 
e9a40 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 70  uninitialized sp
e9a50 61 63 65 20 69 6e 20 7a 42 75 66 20 2d 20 62 75  ace in zBuf - bu
e9a60 74 20 76 61 6c 67 72 69 6e 64 20 65 72 72 6f 72  t valgrind error
e9a70 73 20 74 65 6e 64 20 74 6f 20 77 6f 72 72 79 0a  s tend to worry.
e9a80 20 20 2a 2a 20 73 6f 6d 65 20 75 73 65 72 73 2e    ** some users.
e9a90 20 20 52 61 74 68 65 72 20 74 68 61 6e 20 61 72    Rather than ar
e9aa0 67 75 65 2c 20 69 74 20 73 65 65 6d 73 20 65 61  gue, it seems ea
e9ab0 73 69 65 72 20 6a 75 73 74 20 74 6f 20 69 6e 69  sier just to ini
e9ac0 74 69 61 6c 69 7a 65 0a 20 20 2a 2a 20 74 68 65  tialize.  ** the
e9ad0 20 77 68 6f 6c 65 20 61 72 72 61 79 20 61 6e 64   whole array and
e9ae0 20 73 69 6c 65 6e 63 65 20 76 61 6c 67 72 69 6e   silence valgrin
e9af0 64 2c 20 65 76 65 6e 20 69 66 20 74 68 61 74 20  d, even if that 
e9b00 6d 65 61 6e 73 20 6c 65 73 73 20 72 61 6e 64 6f  means less rando
e9b10 6d 6e 65 73 73 0a 20 20 2a 2a 20 69 6e 20 74 68  mness.  ** in th
e9b20 65 20 72 61 6e 64 6f 6d 20 73 65 65 64 2e 0a 20  e random seed.. 
e9b30 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 74 65   **.  ** When te
e9b40 73 74 69 6e 67 2c 20 69 6e 69 74 69 61 6c 69 7a  sting, initializ
e9b50 69 6e 67 20 7a 42 75 66 5b 5d 20 74 6f 20 7a 65  ing zBuf[] to ze
e9b60 72 6f 20 69 73 20 61 6c 6c 20 77 65 20 64 6f 2e  ro is all we do.
e9b70 20 20 54 68 61 74 20 6d 65 61 6e 73 0a 20 20 2a    That means.  *
e9b80 2a 20 74 68 61 74 20 77 65 20 61 6c 77 61 79 73  * that we always
e9b90 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 72 61   use the same ra
e9ba0 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 73 65 71 75  ndom number sequ
e9bb0 65 6e 63 65 2e 20 20 54 68 69 73 20 6d 61 6b 65  ence.  This make
e9bc0 73 20 74 68 65 0a 20 20 2a 2a 20 74 65 73 74 73  s the.  ** tests
e9bd0 20 72 65 70 65 61 74 61 62 6c 65 2e 0a 20 20 2a   repeatable..  *
e9be0 2f 0a 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 2c  /.  memset(zBuf,
e9bf0 20 30 2c 20 6e 42 75 66 29 3b 0a 23 69 66 20 21   0, nBuf);.#if !
e9c00 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54  defined(SQLITE_T
e9c10 45 53 54 29 0a 20 20 7b 0a 20 20 20 20 69 6e 74  EST).  {.    int
e9c20 20 70 69 64 2c 20 66 64 3b 0a 20 20 20 20 66 64   pid, fd;.    fd
e9c30 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 75 72   = open("/dev/ur
e9c40 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59  andom", O_RDONLY
e9c50 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c 30 20  );.    if( fd<0 
e9c60 29 7b 0a 20 20 20 20 20 20 74 69 6d 65 5f 74 20  ){.      time_t 
e9c70 74 3b 0a 20 20 20 20 20 20 74 69 6d 65 28 26 74  t;.      time(&t
e9c80 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  );.      memcpy(
e9c90 7a 42 75 66 2c 20 26 74 2c 20 73 69 7a 65 6f 66  zBuf, &t, sizeof
e9ca0 28 74 29 29 3b 0a 20 20 20 20 20 20 70 69 64 20  (t));.      pid 
e9cb0 3d 20 67 65 74 70 69 64 28 29 3b 0a 20 20 20 20  = getpid();.    
e9cc0 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 73    memcpy(&zBuf[s
e9cd0 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70 69 64 2c  izeof(t)], &pid,
e9ce0 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20   sizeof(pid));. 
e9cf0 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a       assert( siz
e9d00 65 6f 66 28 74 29 2b 73 69 7a 65 6f 66 28 70 69  eof(t)+sizeof(pi
e9d10 64 29 3c 3d 28 73 69 7a 65 5f 74 29 6e 42 75 66  d)<=(size_t)nBuf
e9d20 20 29 3b 0a 20 20 20 20 20 20 6e 42 75 66 20 3d   );.      nBuf =
e9d30 20 73 69 7a 65 6f 66 28 74 29 20 2b 20 73 69 7a   sizeof(t) + siz
e9d40 65 6f 66 28 70 69 64 29 3b 0a 20 20 20 20 7d 65  eof(pid);.    }e
e9d50 6c 73 65 7b 0a 20 20 20 20 20 20 6e 42 75 66 20  lse{.      nBuf 
e9d60 3d 20 72 65 61 64 28 66 64 2c 20 7a 42 75 66 2c  = read(fd, zBuf,
e9d70 20 6e 42 75 66 29 3b 0a 20 20 20 20 20 20 63 6c   nBuf);.      cl
e9d80 6f 73 65 28 66 64 29 3b 0a 20 20 20 20 7d 0a 20  ose(fd);.    }. 
e9d90 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75   }.#endif.  retu
e9da0 72 6e 20 6e 42 75 66 3b 0a 7d 0a 0a 0a 2f 2a 0a  rn nBuf;.}.../*.
e9db0 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c  ** Sleep for a l
e9dc0 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65  ittle while.  Re
e9dd0 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20  turn the amount 
e9de0 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a  of time slept..*
e9df0 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69  * The argument i
e9e00 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
e9e10 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 77 65 20  microseconds we 
e9e20 77 61 6e 74 20 74 6f 20 73 6c 65 65 70 2e 0a 2a  want to sleep..*
e9e30 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c  * The return val
e9e40 75 65 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ue is the number
e9e50 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73   of microseconds
e9e60 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75 61 6c   of sleep actual
e9e70 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 20  ly.** requested 
e9e80 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c 79  from the underly
e9e90 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79  ing operating sy
e9ea0 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65 72 20 77  stem, a number w
e9eb0 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74 20 62 65  hich.** might be
e9ec0 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72   greater than or
e9ed0 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 61 72   equal to the ar
e9ee0 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e 6f 74 20  gument, but not 
e9ef0 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68 65  less.** than the
e9f00 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74   argument..*/.st
e9f10 61 74 69 63 20 69 6e 74 20 75 6e 69 78 53 6c 65  atic int unixSle
e9f20 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  ep(sqlite3_vfs *
e9f30 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 6d 69 63  NotUsed, int mic
e9f40 72 6f 73 65 63 6f 6e 64 73 29 7b 0a 23 69 66 20  roseconds){.#if 
e9f50 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72  OS_VXWORKS.  str
e9f60 75 63 74 20 74 69 6d 65 73 70 65 63 20 73 70 3b  uct timespec sp;
e9f70 0a 0a 20 20 73 70 2e 74 76 5f 73 65 63 20 3d 20  ..  sp.tv_sec = 
e9f80 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 2f 20 31  microseconds / 1
e9f90 30 30 30 30 30 30 3b 0a 20 20 73 70 2e 74 76 5f  000000;.  sp.tv_
e9fa0 6e 73 65 63 20 3d 20 28 6d 69 63 72 6f 73 65 63  nsec = (microsec
e9fb0 6f 6e 64 73 20 25 20 31 30 30 30 30 30 30 29 20  onds % 1000000) 
e9fc0 2a 20 31 30 30 30 3b 0a 20 20 6e 61 6e 6f 73 6c  * 1000;.  nanosl
e9fd0 65 65 70 28 26 73 70 2c 20 4e 55 4c 4c 29 3b 0a  eep(&sp, NULL);.
e9fe0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
e9ff0 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72  ER(NotUsed);.  r
ea000 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 6f 6e  eturn microsecon
ea010 64 73 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65  ds;.#elif define
ea020 64 28 48 41 56 45 5f 55 53 4c 45 45 50 29 20 26  d(HAVE_USLEEP) &
ea030 26 20 48 41 56 45 5f 55 53 4c 45 45 50 0a 20 20  & HAVE_USLEEP.  
ea040 75 73 6c 65 65 70 28 6d 69 63 72 6f 73 65 63 6f  usleep(microseco
ea050 6e 64 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  nds);.  UNUSED_P
ea060 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
ea070 29 3b 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72  );.  return micr
ea080 6f 73 65 63 6f 6e 64 73 3b 0a 23 65 6c 73 65 0a  oseconds;.#else.
ea090 20 20 69 6e 74 20 73 65 63 6f 6e 64 73 20 3d 20    int seconds = 
ea0a0 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 2b 39 39  (microseconds+99
ea0b0 39 39 39 39 29 2f 31 30 30 30 30 30 30 3b 0a 20  9999)/1000000;. 
ea0c0 20 73 6c 65 65 70 28 73 65 63 6f 6e 64 73 29 3b   sleep(seconds);
ea0d0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
ea0e0 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
ea0f0 72 65 74 75 72 6e 20 73 65 63 6f 6e 64 73 2a 31  return seconds*1
ea100 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a 7d  000000;.#endif.}
ea110 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
ea120 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20  owing variable, 
ea130 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d  if set to a non-
ea140 7a 65 72 6f 20 76 61 6c 75 65 2c 20 69 73 20 69  zero value, is i
ea150 6e 74 65 72 70 72 65 74 65 64 20 61 73 0a 2a 2a  nterpreted as.**
ea160 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73   the number of s
ea170 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37  econds since 197
ea180 30 20 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f  0 and is used to
ea190 20 73 65 74 20 74 68 65 20 72 65 73 75 6c 74 20   set the result 
ea1a0 6f 66 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 43  of.** sqlite3OsC
ea1b0 75 72 72 65 6e 74 54 69 6d 65 28 29 20 64 75 72  urrentTime() dur
ea1c0 69 6e 67 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a  ing testing..*/.
ea1d0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
ea1e0 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
ea1f0 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e  t sqlite3_curren
ea200 74 5f 74 69 6d 65 20 3d 20 30 3b 20 20 2f 2a 20  t_time = 0;  /* 
ea210 46 61 6b 65 20 73 79 73 74 65 6d 20 74 69 6d 65  Fake system time
ea220 20 69 6e 20 73 65 63 6f 6e 64 73 20 73 69 6e 63   in seconds sinc
ea230 65 20 31 39 37 30 2e 20 2a 2f 0a 23 65 6e 64 69  e 1970. */.#endi
ea240 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68  f../*.** Find th
ea250 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 28  e current time (
ea260 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f  in Universal Coo
ea270 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29 2e 20  rdinated Time). 
ea280 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 63 75   Write the.** cu
ea290 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64  rrent time and d
ea2a0 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20  ate as a Julian 
ea2b0 44 61 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20  Day number into 
ea2c0 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65  *prNow and.** re
ea2d0 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20  turn 0.  Return 
ea2e0 31 20 69 66 20 74 68 65 20 74 69 6d 65 20 61 6e  1 if the time an
ea2f0 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 20 62 65  d date cannot be
ea300 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69   found..*/.stati
ea310 63 20 69 6e 74 20 75 6e 69 78 43 75 72 72 65 6e  c int unixCurren
ea320 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66  tTime(sqlite3_vf
ea330 73 20 2a 4e 6f 74 55 73 65 64 2c 20 64 6f 75 62  s *NotUsed, doub
ea340 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 23 69 66 20  le *prNow){.#if 
ea350 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
ea360 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
ea370 4e 54 29 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a  NT).  time_t t;.
ea380 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 2a 70    time(&t);.  *p
ea390 72 4e 6f 77 20 3d 20 28 28 28 73 71 6c 69 74 65  rNow = (((sqlite
ea3a0 33 5f 69 6e 74 36 34 29 74 29 2f 38 36 34 30 20  3_int64)t)/8640 
ea3b0 2b 20 32 34 34 30 35 38 37 35 29 2f 31 30 3b 0a  + 24405875)/10;.
ea3c0 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 4e 4f  #elif defined(NO
ea3d0 5f 47 45 54 54 4f 44 29 0a 20 20 74 69 6d 65 5f  _GETTOD).  time_
ea3e0 74 20 74 3b 0a 20 20 74 69 6d 65 28 26 74 29 3b  t t;.  time(&t);
ea3f0 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 74 2f 38 36  .  *prNow = t/86
ea400 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38 37 2e  400.0 + 2440587.
ea410 35 3b 0a 23 65 6c 69 66 20 4f 53 5f 56 58 57 4f  5;.#elif OS_VXWO
ea420 52 4b 53 0a 20 20 73 74 72 75 63 74 20 74 69 6d  RKS.  struct tim
ea430 65 73 70 65 63 20 73 4e 6f 77 3b 0a 20 20 63 6c  espec sNow;.  cl
ea440 6f 63 6b 5f 67 65 74 74 69 6d 65 28 43 4c 4f 43  ock_gettime(CLOC
ea450 4b 5f 52 45 41 4c 54 49 4d 45 2c 20 26 73 4e 6f  K_REALTIME, &sNo
ea460 77 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 32  w);.  *prNow = 2
ea470 34 34 30 35 38 37 2e 35 20 2b 20 73 4e 6f 77 2e  440587.5 + sNow.
ea480 74 76 5f 73 65 63 2f 38 36 34 30 30 2e 30 20 2b  tv_sec/86400.0 +
ea490 20 73 4e 6f 77 2e 74 76 5f 6e 73 65 63 2f 38 36   sNow.tv_nsec/86
ea4a0 34 30 30 30 30 30 30 30 30 30 30 30 2e 30 3b 0a  400000000000.0;.
ea4b0 23 65 6c 73 65 0a 20 20 73 74 72 75 63 74 20 74  #else.  struct t
ea4c0 69 6d 65 76 61 6c 20 73 4e 6f 77 3b 0a 20 20 67  imeval sNow;.  g
ea4d0 65 74 74 69 6d 65 6f 66 64 61 79 28 26 73 4e 6f  ettimeofday(&sNo
ea4e0 77 2c 20 30 29 3b 0a 20 20 2a 70 72 4e 6f 77 20  w, 0);.  *prNow 
ea4f0 3d 20 32 34 34 30 35 38 37 2e 35 20 2b 20 73 4e  = 2440587.5 + sN
ea500 6f 77 2e 74 76 5f 73 65 63 2f 38 36 34 30 30 2e  ow.tv_sec/86400.
ea510 30 20 2b 20 73 4e 6f 77 2e 74 76 5f 75 73 65 63  0 + sNow.tv_usec
ea520 2f 38 36 34 30 30 30 30 30 30 30 30 2e 30 3b 0a  /86400000000.0;.
ea530 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
ea540 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28  QLITE_TEST.  if(
ea550 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74   sqlite3_current
ea560 5f 74 69 6d 65 20 29 7b 0a 20 20 20 20 2a 70 72  _time ){.    *pr
ea570 4e 6f 77 20 3d 20 73 71 6c 69 74 65 33 5f 63 75  Now = sqlite3_cu
ea580 72 72 65 6e 74 5f 74 69 6d 65 2f 38 36 34 30 30  rrent_time/86400
ea590 2e 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a  .0 + 2440587.5;.
ea5a0 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 55 4e 55    }.#endif.  UNU
ea5b0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
ea5c0 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e  tUsed);.  return
ea5d0 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 65 20   0;.}../*.** We 
ea5e0 61 64 64 65 64 20 74 68 65 20 78 47 65 74 4c 61  added the xGetLa
ea5f0 73 74 45 72 72 6f 72 28 29 20 6d 65 74 68 6f 64  stError() method
ea600 20 77 69 74 68 20 74 68 65 20 69 6e 74 65 6e 74   with the intent
ea610 69 6f 6e 20 6f 66 20 70 72 6f 76 69 64 69 6e 67  ion of providing
ea620 0a 2a 2a 20 62 65 74 74 65 72 20 6c 6f 77 2d 6c  .** better low-l
ea630 65 76 65 6c 20 65 72 72 6f 72 20 6d 65 73 73 61  evel error messa
ea640 67 65 73 20 77 68 65 6e 20 6f 70 65 72 61 74 69  ges when operati
ea650 6e 67 2d 73 79 73 74 65 6d 20 70 72 6f 62 6c 65  ng-system proble
ea660 6d 73 20 63 6f 6d 65 20 75 70 0a 2a 2a 20 64 75  ms come up.** du
ea670 72 69 6e 67 20 53 51 4c 69 74 65 20 6f 70 65 72  ring SQLite oper
ea680 61 74 69 6f 6e 2e 20 20 42 75 74 20 73 6f 20 66  ation.  But so f
ea690 61 72 2c 20 6e 6f 6e 65 20 6f 66 20 74 68 61 74  ar, none of that
ea6a0 20 68 61 73 20 62 65 65 6e 20 69 6d 70 6c 65 6d   has been implem
ea6b0 65 6e 74 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20  ented.** in the 
ea6c0 63 6f 72 65 2e 20 20 53 6f 20 74 68 69 73 20 72  core.  So this r
ea6d0 6f 75 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20  outine is never 
ea6e0 63 61 6c 6c 65 64 2e 20 20 46 6f 72 20 6e 6f 77  called.  For now
ea6f0 2c 20 69 74 20 69 73 20 6d 65 72 65 6c 79 0a 2a  , it is merely.*
ea700 2a 20 61 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72  * a place-holder
ea710 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
ea720 75 6e 69 78 47 65 74 4c 61 73 74 45 72 72 6f 72  unixGetLastError
ea730 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f  (sqlite3_vfs *No
ea740 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73  tUsed, int NotUs
ea750 65 64 32 2c 20 63 68 61 72 20 2a 4e 6f 74 55 73  ed2, char *NotUs
ea760 65 64 33 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  ed3){.  UNUSED_P
ea770 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
ea780 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
ea790 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 32 29 3b  METER(NotUsed2);
ea7a0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
ea7b0 54 45 52 28 4e 6f 74 55 73 65 64 33 29 3b 0a 20  TER(NotUsed3);. 
ea7c0 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
ea7d0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
ea7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
ea7f0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74   sqlite3_vfs met
ea800 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  hods ***********
ea810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea820 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
ea830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
ea870 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
ea880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea8c0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
ea8d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
ea8e0 69 6e 20 50 72 6f 78 79 20 4c 6f 63 6b 69 6e 67  in Proxy Locking
ea8f0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
ea900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea910 2a 0a 2a 2a 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f  *.**.** Proxy lo
ea920 63 6b 69 6e 67 20 69 73 20 61 20 22 75 62 65 72  cking is a "uber
ea930 2d 6c 6f 63 6b 69 6e 67 2d 6d 65 74 68 6f 64 22  -locking-method"
ea940 20 69 6e 20 74 68 69 73 20 73 65 6e 73 65 3a 20   in this sense: 
ea950 20 49 74 20 75 73 65 73 20 74 68 65 0a 2a 2a 20   It uses the.** 
ea960 6f 74 68 65 72 20 6c 6f 63 6b 69 6e 67 20 6d 65  other locking me
ea970 74 68 6f 64 73 20 6f 6e 20 73 65 63 6f 6e 64 61  thods on seconda
ea980 72 79 20 6c 6f 63 6b 20 66 69 6c 65 73 2e 20 20  ry lock files.  
ea990 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73  Proxy locking is
ea9a0 20 61 0a 2a 2a 20 6d 65 74 61 2d 6c 61 79 65 72   a.** meta-layer
ea9b0 20 6f 76 65 72 20 74 6f 70 20 6f 66 20 74 68 65   over top of the
ea9c0 20 70 72 69 6d 69 74 69 76 65 20 6c 6f 63 6b 69   primitive locki
ea9d0 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61  ng implemented a
ea9e0 62 6f 76 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68  bove.  For.** th
ea9f0 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 64  is reason, the d
eaa00 69 76 69 73 69 6f 6e 20 74 68 61 74 20 69 6d 70  ivision that imp
eaa10 6c 65 6d 65 6e 74 73 20 6f 66 20 70 72 6f 78 79  lements of proxy
eaa20 20 6c 6f 63 6b 69 6e 67 20 69 73 20 64 65 66 65   locking is defe
eaa30 72 72 65 64 0a 2a 2a 20 75 6e 74 69 6c 20 6c 61  rred.** until la
eaa40 74 65 20 69 6e 20 74 68 65 20 66 69 6c 65 20 28  te in the file (
eaa50 68 65 72 65 29 20 61 66 74 65 72 20 61 6c 6c 20  here) after all 
eaa60 6f 66 20 74 68 65 20 6f 74 68 65 72 20 49 2f 4f  of the other I/O
eaa70 20 6d 65 74 68 6f 64 73 20 68 61 76 65 0a 2a 2a   methods have.**
eaa80 20 62 65 65 6e 20 64 65 66 69 6e 65 64 20 2d 20   been defined - 
eaa90 73 6f 20 74 68 61 74 20 74 68 65 20 70 72 69 6d  so that the prim
eaaa0 69 74 69 76 65 20 6c 6f 63 6b 69 6e 67 20 6d 65  itive locking me
eaab0 74 68 6f 64 73 20 61 72 65 20 61 76 61 69 6c 61  thods are availa
eaac0 62 6c 65 0a 2a 2a 20 61 73 20 73 65 72 76 69 63  ble.** as servic
eaad0 65 73 20 74 6f 20 68 65 6c 70 20 77 69 74 68 20  es to help with 
eaae0 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
eaaf0 6f 6e 20 6f 66 20 70 72 6f 78 79 20 6c 6f 63 6b  on of proxy lock
eab00 69 6e 67 2e 0a 2a 2a 0a 2a 2a 2a 2a 0a 2a 2a 0a  ing..**.****.**.
eab10 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 6c  ** The default l
eab20 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 73 20 69  ocking schemes i
eab30 6e 20 53 51 4c 69 74 65 20 75 73 65 20 62 79 74  n SQLite use byt
eab40 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 20 6f 6e  e-range locks on
eab50 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
eab60 20 66 69 6c 65 20 74 6f 20 63 6f 6f 72 64 69 6e   file to coordin
eab70 61 74 65 20 73 61 66 65 2c 20 63 6f 6e 63 75 72  ate safe, concur
eab80 72 65 6e 74 20 61 63 63 65 73 73 20 62 79 20 6d  rent access by m
eab90 75 6c 74 69 70 6c 65 20 72 65 61 64 65 72 73 0a  ultiple readers.
eaba0 2a 2a 20 61 6e 64 20 77 72 69 74 65 72 73 20 5b  ** and writers [
eabb0 68 74 74 70 3a 2f 2f 73 71 6c 69 74 65 2e 6f 72  http://sqlite.or
eabc0 67 2f 6c 6f 63 6b 69 6e 67 76 33 2e 68 74 6d 6c  g/lockingv3.html
eabd0 5d 2e 20 20 54 68 65 20 66 69 76 65 20 66 69 6c  ].  The five fil
eabe0 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 61  e locking.** sta
eabf0 74 65 73 20 28 55 4e 4c 4f 43 4b 45 44 2c 20 50  tes (UNLOCKED, P
eac00 45 4e 44 49 4e 47 2c 20 53 48 41 52 45 44 2c 20  ENDING, SHARED, 
eac10 52 45 53 45 52 56 45 44 2c 20 45 58 43 4c 55 53  RESERVED, EXCLUS
eac20 49 56 45 29 20 61 72 65 20 69 6d 70 6c 65 6d 65  IVE) are impleme
eac30 6e 74 65 64 0a 2a 2a 20 61 73 20 50 4f 53 49 58  nted.** as POSIX
eac40 20 72 65 61 64 20 26 20 77 72 69 74 65 20 6c 6f   read & write lo
eac50 63 6b 73 20 6f 76 65 72 20 66 69 78 65 64 20 73  cks over fixed s
eac60 65 74 20 6f 66 20 6c 6f 63 61 74 69 6f 6e 73 20  et of locations 
eac70 28 76 69 61 20 66 73 63 74 6c 29 2c 0a 2a 2a 20  (via fsctl),.** 
eac80 6f 6e 20 41 46 50 20 61 6e 64 20 53 4d 42 20 6f  on AFP and SMB o
eac90 6e 6c 79 20 65 78 63 6c 75 73 69 76 65 20 62 79  nly exclusive by
eaca0 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 20 61  te-range locks a
eacb0 72 65 20 61 76 61 69 6c 61 62 6c 65 20 76 69 61  re available via
eacc0 20 66 73 63 74 6c 0a 2a 2a 20 77 69 74 68 20 5f   fsctl.** with _
eacd0 49 4f 57 52 28 27 7a 27 2c 20 32 33 2c 20 73 74  IOWR('z', 23, st
eace0 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f  ruct ByteRangeLo
eacf0 63 6b 50 42 32 29 20 74 6f 20 74 72 61 63 6b 20  ckPB2) to track 
ead00 74 68 65 20 73 61 6d 65 20 35 20 73 74 61 74 65  the same 5 state
ead10 73 2e 0a 2a 2a 20 54 6f 20 73 69 6d 75 6c 61 74  s..** To simulat
ead20 65 20 61 20 46 5f 52 44 4c 43 4b 20 6f 6e 20 74  e a F_RDLCK on t
ead30 68 65 20 73 68 61 72 65 64 20 72 61 6e 67 65 2c  he shared range,
ead40 20 6f 6e 20 41 46 50 20 61 20 72 61 6e 64 6f 6d   on AFP a random
ead50 6c 79 20 73 65 6c 65 63 74 65 64 0a 2a 2a 20 61  ly selected.** a
ead60 64 64 72 65 73 73 20 69 6e 20 74 68 65 20 73 68  ddress in the sh
ead70 61 72 65 64 20 72 61 6e 67 65 20 69 73 20 74 61  ared range is ta
ead80 6b 65 6e 20 66 6f 72 20 61 20 53 48 41 52 45 44  ken for a SHARED
ead90 20 6c 6f 63 6b 2c 20 74 68 65 20 65 6e 74 69 72   lock, the entir
eada0 65 0a 2a 2a 20 73 68 61 72 65 64 20 72 61 6e 67  e.** shared rang
eadb0 65 20 69 73 20 74 61 6b 65 6e 20 66 6f 72 20 61  e is taken for a
eadc0 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  n EXCLUSIVE lock
eadd0 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 50 45  ):.**.**      PE
eade0 4e 44 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20  NDING_BYTE      
eadf0 20 20 30 78 34 30 30 30 30 30 30 30 09 09 20 20    0x40000000..  
eae00 20 09 0a 2a 2a 20 20 20 20 20 20 52 45 53 45 52   ..**      RESER
eae10 56 45 44 5f 42 59 54 45 20 20 20 20 20 20 20 30  VED_BYTE       0
eae20 78 34 30 30 30 30 30 30 31 0a 2a 2a 20 20 20 20  x40000001.**    
eae30 20 20 53 48 41 52 45 44 5f 52 41 4e 47 45 20 20    SHARED_RANGE  
eae40 20 20 20 20 20 20 30 78 34 30 30 30 30 30 30 32        0x40000002
eae50 20 2d 3e 20 30 78 34 30 30 30 30 32 30 30 0a 2a   -> 0x40000200.*
eae60 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20  *.** This works 
eae70 77 65 6c 6c 20 6f 6e 20 74 68 65 20 6c 6f 63 61  well on the loca
eae80 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 2c 20 62  l file system, b
eae90 75 74 20 73 68 6f 77 73 20 61 20 6e 65 61 72 6c  ut shows a nearl
eaea0 79 20 31 30 30 78 0a 2a 2a 20 73 6c 6f 77 64 6f  y 100x.** slowdo
eaeb0 77 6e 20 69 6e 20 72 65 61 64 20 70 65 72 66 6f  wn in read perfo
eaec0 72 6d 61 6e 63 65 20 6f 6e 20 41 46 50 20 62 65  rmance on AFP be
eaed0 63 61 75 73 65 20 74 68 65 20 41 46 50 20 63 6c  cause the AFP cl
eaee0 69 65 6e 74 20 64 69 73 61 62 6c 65 73 0a 2a 2a  ient disables.**
eaef0 20 74 68 65 20 72 65 61 64 20 63 61 63 68 65 20   the read cache 
eaf00 77 68 65 6e 20 62 79 74 65 2d 72 61 6e 67 65 20  when byte-range 
eaf10 6c 6f 63 6b 73 20 61 72 65 20 70 72 65 73 65 6e  locks are presen
eaf20 74 2e 20 20 45 6e 61 62 6c 69 6e 67 20 74 68 65  t.  Enabling the
eaf30 20 72 65 61 64 0a 2a 2a 20 63 61 63 68 65 20 65   read.** cache e
eaf40 78 70 6f 73 65 73 20 61 20 63 61 63 68 65 20 63  xposes a cache c
eaf50 6f 68 65 72 65 6e 63 79 20 70 72 6f 62 6c 65 6d  oherency problem
eaf60 20 74 68 61 74 20 69 73 20 70 72 65 73 65 6e 74   that is present
eaf70 20 6f 6e 20 61 6c 6c 20 4f 53 20 58 0a 2a 2a 20   on all OS X.** 
eaf80 73 75 70 70 6f 72 74 65 64 20 6e 65 74 77 6f 72  supported networ
eaf90 6b 20 66 69 6c 65 20 73 79 73 74 65 6d 73 2e 20  k file systems. 
eafa0 20 4e 46 53 20 61 6e 64 20 41 46 50 20 62 6f 74   NFS and AFP bot
eafb0 68 20 6f 62 73 65 72 76 65 20 74 68 65 0a 2a 2a  h observe the.**
eafc0 20 63 6c 6f 73 65 2d 74 6f 2d 6f 70 65 6e 20 73   close-to-open s
eafd0 65 6d 61 6e 74 69 63 73 20 66 6f 72 20 65 6e 73  emantics for ens
eafe0 75 72 69 6e 67 20 63 61 63 68 65 20 63 6f 68 65  uring cache cohe
eaff0 72 65 6e 63 79 0a 2a 2a 20 5b 68 74 74 70 3a 2f  rency.** [http:/
eb000 2f 6e 66 73 2e 73 6f 75 72 63 65 66 6f 72 67 65  /nfs.sourceforge
eb010 2e 6e 65 74 2f 23 66 61 71 5f 61 38 5d 2c 20 77  .net/#faq_a8], w
eb020 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 65 66  hich does not ef
eb030 66 65 63 74 69 76 65 6c 79 0a 2a 2a 20 61 64 64  fectively.** add
eb040 72 65 73 73 20 74 68 65 20 72 65 71 75 69 72 65  ress the require
eb050 6d 65 6e 74 73 20 66 6f 72 20 63 6f 6e 63 75 72  ments for concur
eb060 72 65 6e 74 20 64 61 74 61 62 61 73 65 20 61 63  rent database ac
eb070 63 65 73 73 20 62 79 20 6d 75 6c 74 69 70 6c 65  cess by multiple
eb080 0a 2a 2a 20 72 65 61 64 65 72 73 20 61 6e 64 20  .** readers and 
eb090 77 72 69 74 65 72 73 0a 2a 2a 20 5b 68 74 74 70  writers.** [http
eb0a0 3a 2f 2f 77 77 77 2e 6e 61 62 62 6c 65 2e 63 6f  ://www.nabble.co
eb0b0 6d 2f 53 51 4c 69 74 65 2d 6f 6e 2d 4e 46 53 2d  m/SQLite-on-NFS-
eb0c0 63 61 63 68 65 2d 63 6f 68 65 72 65 6e 63 79 2d  cache-coherency-
eb0d0 74 64 31 35 36 35 35 37 30 31 2e 68 74 6d 6c 5d  td15655701.html]
eb0e0 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 64 64 72 65  ..**.** To addre
eb0f0 73 73 20 74 68 65 20 70 65 72 66 6f 72 6d 61 6e  ss the performan
eb100 63 65 20 61 6e 64 20 63 61 63 68 65 20 63 6f 68  ce and cache coh
eb110 65 72 65 6e 63 79 20 69 73 73 75 65 73 2c 20 70  erency issues, p
eb120 72 6f 78 79 20 66 69 6c 65 20 6c 6f 63 6b 69 6e  roxy file lockin
eb130 67 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 68 65  g.** changes the
eb140 20 77 61 79 20 64 61 74 61 62 61 73 65 20 61 63   way database ac
eb150 63 65 73 73 20 69 73 20 63 6f 6e 74 72 6f 6c 6c  cess is controll
eb160 65 64 20 62 79 20 6c 69 6d 69 74 69 6e 67 20 61  ed by limiting a
eb170 63 63 65 73 73 20 74 6f 20 61 0a 2a 2a 20 73 69  ccess to a.** si
eb180 6e 67 6c 65 20 68 6f 73 74 20 61 74 20 61 20 74  ngle host at a t
eb190 69 6d 65 20 61 6e 64 20 6d 6f 76 69 6e 67 20 66  ime and moving f
eb1a0 69 6c 65 20 6c 6f 63 6b 73 20 6f 66 66 20 6f 66  ile locks off of
eb1b0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
eb1c0 6c 65 0a 2a 2a 20 61 6e 64 20 6f 6e 74 6f 20 61  le.** and onto a
eb1d0 20 70 72 6f 78 79 20 66 69 6c 65 20 6f 6e 20 74   proxy file on t
eb1e0 68 65 20 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79  he local file sy
eb1f0 73 74 65 6d 2e 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2a  stem.  .**.**.**
eb200 20 55 73 69 6e 67 20 70 72 6f 78 79 20 6c 6f 63   Using proxy loc
eb210 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ks.** ----------
eb220 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 43 20  -------.**.** C 
eb230 41 50 49 73 0a 2a 2a 0a 2a 2a 20 20 73 71 6c 69  APIs.**.**  sqli
eb240 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c  te3_file_control
eb250 28 64 62 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c  (db, dbname, SQL
eb260 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58  ITE_SET_LOCKPROX
eb270 59 46 49 4c 45 2c 0a 2a 2a 20 20 20 20 20 20 20  YFILE,.**       
eb280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eb290 3c 70 72 6f 78 79 5f 70 61 74 68 3e 20 7c 20 22  <proxy_path> | "
eb2a0 3a 61 75 74 6f 3a 22 29 3b 0a 2a 2a 20 20 73 71  :auto:");.**  sq
eb2b0 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72  lite3_file_contr
eb2c0 6f 6c 28 64 62 2c 20 64 62 6e 61 6d 65 2c 20 53  ol(db, dbname, S
eb2d0 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52  QLITE_GET_LOCKPR
eb2e0 4f 58 59 46 49 4c 45 2c 20 26 3c 70 72 6f 78 79  OXYFILE, &<proxy
eb2f0 5f 70 61 74 68 3e 29 3b 0a 2a 2a 0a 2a 2a 0a 2a  _path>);.**.**.*
eb300 2a 20 53 51 4c 20 70 72 61 67 6d 61 73 0a 2a 2a  * SQL pragmas.**
eb310 0a 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74  .**  PRAGMA [dat
eb320 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78  abase.]lock_prox
eb330 79 5f 66 69 6c 65 3d 3c 70 72 6f 78 79 5f 70 61  y_file=<proxy_pa
eb340 74 68 3e 20 7c 20 3a 61 75 74 6f 3a 0a 2a 2a 20  th> | :auto:.** 
eb350 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
eb360 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69  e.]lock_proxy_fi
eb370 6c 65 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 79  le.**.** Specify
eb380 69 6e 67 20 22 3a 61 75 74 6f 3a 22 20 6d 65 61  ing ":auto:" mea
eb390 6e 73 20 74 68 61 74 20 69 66 20 74 68 65 72 65  ns that if there
eb3a0 20 69 73 20 61 20 63 6f 6e 63 68 20 66 69 6c 65   is a conch file
eb3b0 20 77 69 74 68 20 61 20 6d 61 74 63 68 69 6e 67   with a matching
eb3c0 0a 2a 2a 20 68 6f 73 74 20 49 44 20 69 6e 20 69  .** host ID in i
eb3d0 74 2c 20 74 68 65 20 70 72 6f 78 79 20 70 61 74  t, the proxy pat
eb3e0 68 20 69 6e 20 74 68 65 20 63 6f 6e 63 68 20 66  h in the conch f
eb3f0 69 6c 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64  ile will be used
eb400 2c 20 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20 61  , otherwise.** a
eb410 20 70 72 6f 78 79 20 70 61 74 68 20 62 61 73 65   proxy path base
eb420 64 20 6f 6e 20 74 68 65 20 75 73 65 72 27 73 20  d on the user's 
eb430 74 65 6d 70 20 64 69 72 0a 2a 2a 20 28 76 69 61  temp dir.** (via
eb440 20 63 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41 52   confstr(_CS_DAR
eb450 57 49 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49  WIN_USER_TEMP_DI
eb460 52 2c 2e 2e 2e 29 29 20 77 69 6c 6c 20 62 65 20  R,...)) will be 
eb470 75 73 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20  used and the.** 
eb480 61 63 74 75 61 6c 20 70 72 6f 78 79 20 66 69 6c  actual proxy fil
eb490 65 20 6e 61 6d 65 20 69 73 20 67 65 6e 65 72 61  e name is genera
eb4a0 74 65 64 20 66 72 6f 6d 20 74 68 65 20 6e 61 6d  ted from the nam
eb4b0 65 20 61 6e 64 20 70 61 74 68 20 6f 66 20 74 68  e and path of th
eb4c0 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69  e.** database fi
eb4d0 6c 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  le.  For example
eb4e0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 46 6f  :.**.**       Fo
eb4f0 72 20 64 61 74 61 62 61 73 65 20 70 61 74 68 20  r database path 
eb500 22 2f 55 73 65 72 73 2f 6d 65 2f 66 6f 6f 2e 64  "/Users/me/foo.d
eb510 62 22 20 0a 2a 2a 20 20 20 20 20 20 20 54 68 65  b" .**       The
eb520 20 6c 6f 63 6b 20 70 61 74 68 20 77 69 6c 6c 20   lock path will 
eb530 62 65 20 22 3c 74 6d 70 64 69 72 3e 2f 73 71 6c  be "<tmpdir>/sql
eb540 69 74 65 70 6c 6f 63 6b 73 2f 5f 55 73 65 72 73  iteplocks/_Users
eb550 5f 6d 65 5f 66 6f 6f 2e 64 62 3a 61 75 74 6f 3a  _me_foo.db:auto:
eb560 22 29 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 61 20  ").**.** Once a 
eb570 6c 6f 63 6b 20 70 72 6f 78 79 20 69 73 20 63 6f  lock proxy is co
eb580 6e 66 69 67 75 72 65 64 20 66 6f 72 20 61 20 64  nfigured for a d
eb590 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
eb5a0 6f 6e 2c 20 69 74 20 63 61 6e 20 6e 6f 74 0a 2a  on, it can not.*
eb5b0 2a 20 62 65 20 72 65 6d 6f 76 65 64 2c 20 68 6f  * be removed, ho
eb5c0 77 65 76 65 72 20 69 74 20 6d 61 79 20 62 65 20  wever it may be 
eb5d0 73 77 69 74 63 68 65 64 20 74 6f 20 61 20 64 69  switched to a di
eb5e0 66 66 65 72 65 6e 74 20 70 72 6f 78 79 20 70 61  fferent proxy pa
eb5f0 74 68 20 76 69 61 0a 2a 2a 20 74 68 65 20 61 62  th via.** the ab
eb600 6f 76 65 20 41 50 49 73 20 28 61 73 73 75 6d 69  ove APIs (assumi
eb610 6e 67 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c  ng the conch fil
eb620 65 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 68  e is not being h
eb630 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 0a 2a  eld by another.*
eb640 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20  * connection or 
eb650 70 72 6f 63 65 73 73 29 2e 20 0a 2a 2a 0a 2a 2a  process). .**.**
eb660 0a 2a 2a 20 48 6f 77 20 70 72 6f 78 79 20 6c 6f  .** How proxy lo
eb670 63 6b 69 6e 67 20 77 6f 72 6b 73 0a 2a 2a 20 2d  cking works.** -
eb680 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
eb690 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 50 72 6f  ------.**.** Pro
eb6a0 78 79 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20  xy file locking 
eb6b0 72 65 6c 69 65 73 20 70 72 69 6d 61 72 69 6c 79  relies primarily
eb6c0 20 6f 6e 20 74 77 6f 20 6e 65 77 20 73 75 70 70   on two new supp
eb6d0 6f 72 74 69 6e 67 20 66 69 6c 65 73 3a 20 0a 2a  orting files: .*
eb6e0 2a 0a 2a 2a 20 20 20 2a 20 20 63 6f 6e 63 68 20  *.**   *  conch 
eb6f0 66 69 6c 65 20 74 6f 20 6c 69 6d 69 74 20 61 63  file to limit ac
eb700 63 65 73 73 20 74 6f 20 74 68 65 20 64 61 74 61  cess to the data
eb710 62 61 73 65 20 66 69 6c 65 20 74 6f 20 61 20 73  base file to a s
eb720 69 6e 67 6c 65 20 68 6f 73 74 0a 2a 2a 20 20 20  ingle host.**   
eb730 20 20 20 61 74 20 61 20 74 69 6d 65 0a 2a 2a 0a     at a time.**.
eb740 2a 2a 20 20 20 2a 20 20 70 72 6f 78 79 20 66 69  **   *  proxy fi
eb750 6c 65 20 74 6f 20 61 63 74 20 61 73 20 61 20 70  le to act as a p
eb760 72 6f 78 79 20 66 6f 72 20 74 68 65 20 61 64 76  roxy for the adv
eb770 69 73 6f 72 79 20 6c 6f 63 6b 73 20 6e 6f 72 6d  isory locks norm
eb780 61 6c 6c 79 0a 2a 2a 20 20 20 20 20 20 74 61 6b  ally.**      tak
eb790 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  en on the databa
eb7a0 73 65 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e  se.**.** The con
eb7b0 63 68 20 66 69 6c 65 20 2d 20 74 6f 20 75 73 65  ch file - to use
eb7c0 20 61 20 70 72 6f 78 79 20 66 69 6c 65 2c 20 73   a proxy file, s
eb7d0 71 6c 69 74 65 20 6d 75 73 74 20 66 69 72 73 74  qlite must first
eb7e0 20 22 68 6f 6c 64 20 74 68 65 20 63 6f 6e 63 68   "hold the conch
eb7f0 22 0a 2a 2a 20 62 79 20 74 61 6b 69 6e 67 20 61  ".** by taking a
eb800 6e 20 73 71 6c 69 74 65 2d 73 74 79 6c 65 20 73  n sqlite-style s
eb810 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74 68  hared lock on th
eb820 65 20 63 6f 6e 63 68 20 66 69 6c 65 2c 20 72 65  e conch file, re
eb830 61 64 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e  ading the.** con
eb840 74 65 6e 74 73 20 61 6e 64 20 63 6f 6d 70 61 72  tents and compar
eb850 69 6e 67 20 74 68 65 20 68 6f 73 74 27 73 20 75  ing the host's u
eb860 6e 69 71 75 65 20 68 6f 73 74 20 49 44 20 28 73  nique host ID (s
eb870 65 65 20 62 65 6c 6f 77 29 20 61 6e 64 20 6c 6f  ee below) and lo
eb880 63 6b 0a 2a 2a 20 70 72 6f 78 79 20 70 61 74 68  ck.** proxy path
eb890 20 61 67 61 69 6e 73 74 20 74 68 65 20 76 61 6c   against the val
eb8a0 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68  ues stored in th
eb8b0 65 20 63 6f 6e 63 68 2e 20 20 54 68 65 20 63 6f  e conch.  The co
eb8c0 6e 63 68 20 66 69 6c 65 20 69 73 0a 2a 2a 20 73  nch file is.** s
eb8d0 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73 61 6d  tored in the sam
eb8e0 65 20 64 69 72 65 63 74 6f 72 79 20 61 73 20 74  e directory as t
eb8f0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
eb900 20 61 6e 64 20 74 68 65 20 66 69 6c 65 20 6e 61   and the file na
eb910 6d 65 0a 2a 2a 20 69 73 20 70 61 74 74 65 72 6e  me.** is pattern
eb920 65 64 20 61 66 74 65 72 20 74 68 65 20 64 61 74  ed after the dat
eb930 61 62 61 73 65 20 66 69 6c 65 20 6e 61 6d 65 20  abase file name 
eb940 61 73 20 22 2e 3c 64 61 74 61 62 61 73 65 6e 61  as ".<databasena
eb950 6d 65 3e 2d 63 6f 6e 63 68 22 2e 0a 2a 2a 20 49  me>-conch"..** I
eb960 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65  f the conch file
eb970 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c   does not exist,
eb980 20 6f 72 20 69 74 27 73 20 63 6f 6e 74 65 6e 74   or it's content
eb990 73 20 64 6f 20 6e 6f 74 20 6d 61 74 63 68 20 74  s do not match t
eb9a0 68 65 0a 2a 2a 20 68 6f 73 74 20 49 44 20 61 6e  he.** host ID an
eb9b0 64 2f 6f 72 20 70 72 6f 78 79 20 70 61 74 68 2c  d/or proxy path,
eb9c0 20 74 68 65 6e 20 74 68 65 20 6c 6f 63 6b 20 69   then the lock i
eb9d0 73 20 65 73 63 61 6c 61 74 65 64 20 74 6f 20 61  s escalated to a
eb9e0 6e 20 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20 6c  n exclusive.** l
eb9f0 6f 63 6b 20 61 6e 64 20 74 68 65 20 63 6f 6e 63  ock and the conc
eba00 68 20 66 69 6c 65 20 63 6f 6e 74 65 6e 74 73 20  h file contents 
eba10 69 73 20 75 70 64 61 74 65 64 20 77 69 74 68 20  is updated with 
eba20 74 68 65 20 68 6f 73 74 20 49 44 20 61 6e 64 20  the host ID and 
eba30 70 72 6f 78 79 0a 2a 2a 20 70 61 74 68 20 61 6e  proxy.** path an
eba40 64 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 64 6f  d the lock is do
eba50 77 6e 67 72 61 64 65 64 20 74 6f 20 61 20 73 68  wngraded to a sh
eba60 61 72 65 64 20 6c 6f 63 6b 20 61 67 61 69 6e 2e  ared lock again.
eba70 20 20 49 66 20 74 68 65 20 63 6f 6e 63 68 0a 2a    If the conch.*
eba80 2a 20 69 73 20 68 65 6c 64 20 62 79 20 61 6e 6f  * is held by ano
eba90 74 68 65 72 20 70 72 6f 63 65 73 73 20 28 77 69  ther process (wi
ebaa0 74 68 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b  th a shared lock
ebab0 29 2c 20 74 68 65 20 65 78 63 6c 75 73 69 76 65  ), the exclusive
ebac0 20 6c 6f 63 6b 0a 2a 2a 20 77 69 6c 6c 20 66 61   lock.** will fa
ebad0 69 6c 20 61 6e 64 20 53 51 4c 49 54 45 5f 42 55  il and SQLITE_BU
ebae0 53 59 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  SY is returned..
ebaf0 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20  **.** The proxy 
ebb00 66 69 6c 65 20 2d 20 61 20 73 69 6e 67 6c 65 2d  file - a single-
ebb10 62 79 74 65 20 66 69 6c 65 20 75 73 65 64 20 66  byte file used f
ebb20 6f 72 20 61 6c 6c 20 61 64 76 69 73 6f 72 79 20  or all advisory 
ebb30 66 69 6c 65 20 6c 6f 63 6b 73 0a 2a 2a 20 6e 6f  file locks.** no
ebb40 72 6d 61 6c 6c 79 20 74 61 6b 65 6e 20 6f 6e 20  rmally taken on 
ebb50 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
ebb60 65 2e 20 20 20 54 68 69 73 20 61 6c 6c 6f 77 73  e.   This allows
ebb70 20 66 6f 72 20 73 61 66 65 20 73 68 61 72 69 6e   for safe sharin
ebb80 67 0a 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61  g.** of the data
ebb90 62 61 73 65 20 66 69 6c 65 20 66 6f 72 20 6d 75  base file for mu
ebba0 6c 74 69 70 6c 65 20 72 65 61 64 65 72 73 20 61  ltiple readers a
ebbb0 6e 64 20 77 72 69 74 65 72 73 20 6f 6e 20 74 68  nd writers on th
ebbc0 65 20 73 61 6d 65 0a 2a 2a 20 68 6f 73 74 20 28  e same.** host (
ebbd0 74 68 65 20 63 6f 6e 63 68 20 65 6e 73 75 72 65  the conch ensure
ebbe0 73 20 74 68 61 74 20 74 68 65 79 20 61 6c 6c 20  s that they all 
ebbf0 75 73 65 20 74 68 65 20 73 61 6d 65 20 6c 6f 63  use the same loc
ebc00 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 29 2e 0a 2a  al lock file)..*
ebc10 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20  *.** There is a 
ebc20 74 68 69 72 64 20 66 69 6c 65 20 2d 20 74 68 65  third file - the
ebc30 20 68 6f 73 74 20 49 44 20 66 69 6c 65 20 2d 20   host ID file - 
ebc40 75 73 65 64 20 61 73 20 61 20 70 65 72 73 69 73  used as a persis
ebc50 74 65 6e 74 20 72 65 63 6f 72 64 0a 2a 2a 20 6f  tent record.** o
ebc60 66 20 61 20 75 6e 69 71 75 65 20 69 64 65 6e 74  f a unique ident
ebc70 69 66 69 65 72 20 66 6f 72 20 74 68 65 20 68 6f  ifier for the ho
ebc80 73 74 2c 20 61 20 31 32 38 2d 62 79 74 65 20 75  st, a 128-byte u
ebc90 6e 69 71 75 65 20 68 6f 73 74 20 69 64 20 66 69  nique host id fi
ebca0 6c 65 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 74  le.** in the pat
ebcb0 68 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 65  h defined by the
ebcc0 20 48 4f 53 54 49 44 50 41 54 48 20 6d 61 63 72   HOSTIDPATH macr
ebcd0 6f 20 28 64 65 66 61 75 6c 74 20 76 61 6c 75 65  o (default value
ebce0 20 69 73 0a 2a 2a 20 2f 4c 69 62 72 61 72 79 2f   is.** /Library/
ebcf0 43 61 63 68 65 73 2f 2e 63 6f 6d 2e 61 70 70 6c  Caches/.com.appl
ebd00 65 2e 73 71 6c 69 74 65 43 6f 6e 63 68 48 6f 73  e.sqliteConchHos
ebd10 74 49 64 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  tId)..**.** Requ
ebd20 65 73 74 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20  esting the lock 
ebd30 70 72 6f 78 79 20 64 6f 65 73 20 6e 6f 74 20 69  proxy does not i
ebd40 6d 6d 65 64 69 61 74 65 6c 79 20 74 61 6b 65 20  mmediately take 
ebd50 74 68 65 20 63 6f 6e 63 68 2c 20 69 74 20 69 73  the conch, it is
ebd60 0a 2a 2a 20 6f 6e 6c 79 20 74 61 6b 65 6e 20 77  .** only taken w
ebd70 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 72 65  hen the first re
ebd80 71 75 65 73 74 20 74 6f 20 6c 6f 63 6b 20 64 61  quest to lock da
ebd90 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6d  tabase file is m
ebda0 61 64 65 2e 20 20 0a 2a 2a 20 54 68 69 73 20 6d  ade.  .** This m
ebdb0 61 74 63 68 65 73 20 74 68 65 20 73 65 6d 61 6e  atches the seman
ebdc0 74 69 63 73 20 6f 66 20 74 68 65 20 74 72 61 64  tics of the trad
ebdd0 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20  itional locking 
ebde0 62 65 68 61 76 69 6f 72 2c 20 77 68 65 72 65 0a  behavior, where.
ebdf0 2a 2a 20 6f 70 65 6e 69 6e 67 20 61 20 63 6f 6e  ** opening a con
ebe00 6e 65 63 74 69 6f 6e 20 74 6f 20 61 20 64 61 74  nection to a dat
ebe10 61 62 61 73 65 20 66 69 6c 65 20 64 6f 65 73 20  abase file does 
ebe20 6e 6f 74 20 74 61 6b 65 20 61 20 6c 6f 63 6b 20  not take a lock 
ebe30 6f 6e 20 69 74 2e 0a 2a 2a 20 54 68 65 20 73 68  on it..** The sh
ebe40 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6e  ared lock and an
ebe50 20 6f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72   open file descr
ebe60 69 70 74 6f 72 20 61 72 65 20 6d 61 69 6e 74 61  iptor are mainta
ebe70 69 6e 65 64 20 75 6e 74 69 6c 20 0a 2a 2a 20 74  ined until .** t
ebe80 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f  he connection to
ebe90 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
ebea0 20 63 6c 6f 73 65 64 2e 20 0a 2a 2a 0a 2a 2a 20   closed. .**.** 
ebeb0 54 68 65 20 70 72 6f 78 79 20 66 69 6c 65 20 61  The proxy file a
ebec0 6e 64 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65  nd the lock file
ebed0 20 61 72 65 20 6e 65 76 65 72 20 64 65 6c 65 74   are never delet
ebee0 65 64 20 73 6f 20 74 68 65 79 20 6f 6e 6c 79 20  ed so they only 
ebef0 6e 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 63 72  need.** to be cr
ebf00 65 61 74 65 64 20 74 68 65 20 66 69 72 73 74 20  eated the first 
ebf10 74 69 6d 65 20 74 68 65 79 20 61 72 65 20 75 73  time they are us
ebf20 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 66 69 67  ed..**.** Config
ebf30 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 0a  uration options.
ebf40 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ** -------------
ebf50 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20  --------.**.**  
ebf60 53 51 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52  SQLITE_PREFER_PR
ebf70 4f 58 59 5f 4c 4f 43 4b 49 4e 47 0a 2a 2a 0a 2a  OXY_LOCKING.**.*
ebf80 2a 20 20 20 20 20 20 20 44 61 74 61 62 61 73 65  *       Database
ebf90 20 66 69 6c 65 73 20 61 63 63 65 73 73 65 64 20   files accessed 
ebfa0 6f 6e 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c  on non-local fil
ebfb0 65 20 73 79 73 74 65 6d 73 20 61 72 65 0a 2a 2a  e systems are.**
ebfc0 20 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63         automatic
ebfd0 61 6c 6c 79 20 63 6f 6e 66 69 67 75 72 65 64 20  ally configured 
ebfe0 66 6f 72 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e  for proxy lockin
ebff0 67 2c 20 6c 6f 63 6b 20 66 69 6c 65 73 20 61 72  g, lock files ar
ec000 65 0a 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 64  e.**       named
ec010 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 75   automatically u
ec020 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f  sing the same lo
ec030 67 69 63 20 61 73 0a 2a 2a 20 20 20 20 20 20 20  gic as.**       
ec040 50 52 41 47 4d 41 20 6c 6f 63 6b 5f 70 72 6f 78  PRAGMA lock_prox
ec050 79 5f 66 69 6c 65 3d 22 3a 61 75 74 6f 3a 22 0a  y_file=":auto:".
ec060 2a 2a 20 20 20 20 0a 2a 2a 20 20 53 51 4c 49 54  **    .**  SQLIT
ec070 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 0a 2a 2a  E_PROXY_DEBUG.**
ec080 0a 2a 2a 20 20 20 20 20 20 20 45 6e 61 62 6c 65  .**       Enable
ec090 73 20 74 68 65 20 6c 6f 67 67 69 6e 67 20 6f 66  s the logging of
ec0a0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   error messages 
ec0b0 64 75 72 69 6e 67 20 68 6f 73 74 20 69 64 20 66  during host id f
ec0c0 69 6c 65 0a 2a 2a 20 20 20 20 20 20 20 72 65 74  ile.**       ret
ec0d0 72 69 65 76 61 6c 20 61 6e 64 20 63 72 65 61 74  rieval and creat
ec0e0 69 6f 6e 0a 2a 2a 0a 2a 2a 20 20 48 4f 53 54 49  ion.**.**  HOSTI
ec0f0 44 50 41 54 48 0a 2a 2a 0a 2a 2a 20 20 20 20 20  DPATH.**.**     
ec100 20 20 4f 76 65 72 72 69 64 65 73 20 74 68 65 20    Overrides the 
ec110 64 65 66 61 75 6c 74 20 68 6f 73 74 20 49 44 20  default host ID 
ec120 66 69 6c 65 20 70 61 74 68 20 6c 6f 63 61 74 69  file path locati
ec130 6f 6e 0a 2a 2a 0a 2a 2a 20 20 4c 4f 43 4b 50 52  on.**.**  LOCKPR
ec140 4f 58 59 44 49 52 0a 2a 2a 0a 2a 2a 20 20 20 20  OXYDIR.**.**    
ec150 20 20 20 4f 76 65 72 72 69 64 65 73 20 74 68 65     Overrides the
ec160 20 64 65 66 61 75 6c 74 20 64 69 72 65 63 74 6f   default directo
ec170 72 79 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b  ry used for lock
ec180 20 70 72 6f 78 79 20 66 69 6c 65 73 20 74 68 61   proxy files tha
ec190 74 0a 2a 2a 20 20 20 20 20 20 20 61 72 65 20 6e  t.**       are n
ec1a0 61 6d 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c  amed automatical
ec1b0 6c 79 20 76 69 61 20 74 68 65 20 22 3a 61 75 74  ly via the ":aut
ec1c0 6f 3a 22 20 73 65 74 74 69 6e 67 0a 2a 2a 0a 2a  o:" setting.**.*
ec1d0 2a 20 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  *  SQLITE_DEFAUL
ec1e0 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49  T_PROXYDIR_PERMI
ec1f0 53 53 49 4f 4e 53 0a 2a 2a 0a 2a 2a 20 20 20 20  SSIONS.**.**    
ec200 20 20 20 50 65 72 6d 69 73 73 69 6f 6e 73 20 74     Permissions t
ec210 6f 20 75 73 65 20 77 68 65 6e 20 63 72 65 61 74  o use when creat
ec220 69 6e 67 20 61 20 64 69 72 65 63 74 6f 72 79 20  ing a directory 
ec230 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 0a  for storing the.
ec240 2a 2a 20 20 20 20 20 20 20 6c 6f 63 6b 20 70 72  **       lock pr
ec250 6f 78 79 20 66 69 6c 65 73 2c 20 6f 6e 6c 79 20  oxy files, only 
ec260 75 73 65 64 20 77 68 65 6e 20 4c 4f 43 4b 50 52  used when LOCKPR
ec270 4f 58 59 44 49 52 20 69 73 20 6e 6f 74 20 73 65  OXYDIR is not se
ec280 74 2e 0a 2a 2a 20 20 20 20 0a 2a 2a 20 20 20 20  t..**    .**    
ec290 0a 2a 2a 20 41 73 20 6d 65 6e 74 69 6f 6e 65 64  .** As mentioned
ec2a0 20 61 62 6f 76 65 2c 20 77 68 65 6e 20 63 6f 6d   above, when com
ec2b0 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49 54  piled with SQLIT
ec2c0 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c  E_PREFER_PROXY_L
ec2d0 4f 43 4b 49 4e 47 2c 0a 2a 2a 20 73 65 74 74 69  OCKING,.** setti
ec2e0 6e 67 20 74 68 65 20 65 6e 76 69 72 6f 6e 6d 65  ng the environme
ec2f0 6e 74 20 76 61 72 69 61 62 6c 65 20 53 51 4c 49  nt variable SQLI
ec300 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c  TE_FORCE_PROXY_L
ec310 4f 43 4b 49 4e 47 20 74 6f 20 31 20 77 69 6c 6c  OCKING to 1 will
ec320 0a 2a 2a 20 66 6f 72 63 65 20 70 72 6f 78 79 20  .** force proxy 
ec330 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65 20 75 73  locking to be us
ec340 65 64 20 66 6f 72 20 65 76 65 72 79 20 64 61 74  ed for every dat
ec350 61 62 61 73 65 20 66 69 6c 65 20 6f 70 65 6e 65  abase file opene
ec360 64 2c 20 61 6e 64 20 30 0a 2a 2a 20 77 69 6c 6c  d, and 0.** will
ec370 20 66 6f 72 63 65 20 61 75 74 6f 6d 61 74 69 63   force automatic
ec380 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 74   proxy locking t
ec390 6f 20 62 65 20 64 69 73 61 62 6c 65 64 20 66 6f  o be disabled fo
ec3a0 72 20 61 6c 6c 20 64 61 74 61 62 61 73 65 0a 2a  r all database.*
ec3b0 2a 20 66 69 6c 65 73 20 28 65 78 70 6c 69 63 69  * files (explici
ec3c0 74 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 53  ty calling the S
ec3d0 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52  QLITE_SET_LOCKPR
ec3e0 4f 58 59 46 49 4c 45 20 70 72 61 67 6d 61 20 6f  OXYFILE pragma o
ec3f0 72 0a 2a 2a 20 73 71 6c 69 74 65 5f 66 69 6c 65  r.** sqlite_file
ec400 5f 63 6f 6e 74 72 6f 6c 20 41 50 49 20 69 73 20  _control API is 
ec410 6e 6f 74 20 61 66 66 65 63 74 65 64 20 62 79 20  not affected by 
ec420 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f  SQLITE_FORCE_PRO
ec430 58 59 5f 4c 4f 43 4b 49 4e 47 29 2e 0a 2a 2f 0a  XY_LOCKING)..*/.
ec440 0a 2f 2a 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f 63  ./*.** Proxy loc
ec450 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 61 76 61  king is only ava
ec460 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63 4f 53 58  ilable on MacOSX
ec470 20 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64   .*/.#if defined
ec480 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53  (__APPLE__) && S
ec490 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
ec4a0 4b 49 4e 47 5f 53 54 59 4c 45 0a 0a 23 69 66 64  KING_STYLE..#ifd
ec4b0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f  ef SQLITE_TEST./
ec4c0 2a 20 73 69 6d 75 6c 61 74 65 20 6d 75 6c 74 69  * simulate multi
ec4d0 70 6c 65 20 68 6f 73 74 73 20 62 79 20 63 72 65  ple hosts by cre
ec4e0 61 74 69 6e 67 20 75 6e 69 71 75 65 20 68 6f 73  ating unique hos
ec4f0 74 69 64 20 66 69 6c 65 20 70 61 74 68 73 20 2a  tid file paths *
ec500 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
ec510 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f   sqlite3_hostid_
ec520 6e 75 6d 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  num = 0;.#endif.
ec530 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79  ./*.** The proxy
ec540 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 68  LockingContext h
ec550 61 73 20 74 68 65 20 70 61 74 68 20 61 6e 64 20  as the path and 
ec560 66 69 6c 65 20 73 74 72 75 63 74 75 72 65 73 20  file structures 
ec570 66 6f 72 20 74 68 65 20 72 65 6d 6f 74 65 20 0a  for the remote .
ec580 2a 2a 20 61 6e 64 20 6c 6f 63 61 6c 20 70 72 6f  ** and local pro
ec590 78 79 20 66 69 6c 65 73 20 69 6e 20 69 74 0a 2a  xy files in it.*
ec5a0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
ec5b0 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e   proxyLockingCon
ec5c0 74 65 78 74 20 70 72 6f 78 79 4c 6f 63 6b 69 6e  text proxyLockin
ec5d0 67 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74  gContext;.struct
ec5e0 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e   proxyLockingCon
ec5f0 74 65 78 74 20 7b 0a 20 20 75 6e 69 78 46 69 6c  text {.  unixFil
ec600 65 20 2a 63 6f 6e 63 68 46 69 6c 65 3b 20 20 20  e *conchFile;   
ec610 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 63 6f        /* Open co
ec620 6e 63 68 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68  nch file */.  ch
ec630 61 72 20 2a 63 6f 6e 63 68 46 69 6c 65 50 61 74  ar *conchFilePat
ec640 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  h;         /* Na
ec650 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 63 68 20  me of the conch 
ec660 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69  file */.  unixFi
ec670 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 3b 20 20  le *lockProxy;  
ec680 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70         /* Open p
ec690 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 20 2a  roxy lock file *
ec6a0 2f 0a 20 20 63 68 61 72 20 2a 6c 6f 63 6b 50 72  /.  char *lockPr
ec6b0 6f 78 79 50 61 74 68 3b 20 20 20 20 20 20 20 20  oxyPath;        
ec6c0 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
ec6d0 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 20  proxy lock file 
ec6e0 2a 2f 0a 20 20 63 68 61 72 20 2a 64 62 50 61 74  */.  char *dbPat
ec6f0 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
ec700 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
ec710 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 20 20   open file */.  
ec720 69 6e 74 20 63 6f 6e 63 68 48 65 6c 64 3b 20 20  int conchHeld;  
ec730 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ec740 54 72 75 65 20 69 66 20 74 68 65 20 63 6f 6e 63  True if the conc
ec750 68 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 68  h is currently h
ec760 65 6c 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 6f  eld */.  void *o
ec770 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ldLockingContext
ec780 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61  ;     /* Origina
ec790 6c 20 6c 6f 63 6b 69 6e 67 63 6f 6e 74 65 78 74  l lockingcontext
ec7a0 20 74 6f 20 72 65 73 74 6f 72 65 20 6f 6e 20 63   to restore on c
ec7b0 6c 6f 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  lose */.  sqlite
ec7c0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 63 6f 6e  3_io_methods con
ec7d0 73 74 20 2a 70 4f 6c 64 4d 65 74 68 6f 64 3b 20  st *pOldMethod; 
ec7e0 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20      /* Original 
ec7f0 49 2f 4f 20 6d 65 74 68 6f 64 73 20 66 6f 72 20  I/O methods for 
ec800 63 6c 6f 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20  close */.};../* 
ec810 48 4f 53 54 49 44 4c 45 4e 20 61 6e 64 20 43 4f  HOSTIDLEN and CO
ec820 4e 43 48 4c 45 4e 20 62 6f 74 68 20 69 6e 63 6c  NCHLEN both incl
ec830 75 64 65 20 73 70 61 63 65 20 66 6f 72 20 74 68  ude space for th
ec840 65 20 73 74 72 69 6e 67 20 0a 2a 2a 20 74 65 72  e string .** ter
ec850 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 20 0a 2a 2f  minating nul .*/
ec860 0a 23 64 65 66 69 6e 65 20 48 4f 53 54 49 44 4c  .#define HOSTIDL
ec870 45 4e 20 20 20 20 20 20 20 20 20 31 32 38 0a 23  EN         128.#
ec880 64 65 66 69 6e 65 20 43 4f 4e 43 48 4c 45 4e 20  define CONCHLEN 
ec890 20 20 20 20 20 20 20 20 20 28 4d 41 58 50 41 54           (MAXPAT
ec8a0 48 4c 45 4e 2b 48 4f 53 54 49 44 4c 45 4e 2b 31  HLEN+HOSTIDLEN+1
ec8b0 29 0a 23 69 66 6e 64 65 66 20 48 4f 53 54 49 44  ).#ifndef HOSTID
ec8c0 50 41 54 48 0a 23 20 64 65 66 69 6e 65 20 48 4f  PATH.# define HO
ec8d0 53 54 49 44 50 41 54 48 20 20 20 20 20 20 20 22  STIDPATH       "
ec8e0 2f 4c 69 62 72 61 72 79 2f 43 61 63 68 65 73 2f  /Library/Caches/
ec8f0 2e 63 6f 6d 2e 61 70 70 6c 65 2e 73 71 6c 69 74  .com.apple.sqlit
ec900 65 43 6f 6e 63 68 48 6f 73 74 49 64 22 0a 23 65  eConchHostId".#e
ec910 6e 64 69 66 0a 0a 2f 2a 20 62 61 73 69 63 61 6c  ndif../* basical
ec920 6c 79 20 61 20 63 6f 70 79 20 6f 66 20 75 6e 69  ly a copy of uni
ec930 78 52 61 6e 64 6f 6d 6e 65 73 73 20 77 69 74 68  xRandomness with
ec940 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 74 65   different.** te
ec950 73 74 20 62 65 68 61 76 69 6f 72 20 62 75 69 6c  st behavior buil
ec960 74 20 69 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69  t in */.static i
ec970 6e 74 20 70 72 6f 78 79 47 65 6e 65 72 61 74 65  nt proxyGenerate
ec980 48 6f 73 74 49 44 28 63 68 61 72 20 2a 70 48 6f  HostID(char *pHo
ec990 73 74 49 44 29 7b 0a 20 20 69 6e 74 20 70 69 64  stID){.  int pid
ec9a0 2c 20 66 64 2c 20 6c 65 6e 3b 0a 20 20 75 6e 73  , fd, len;.  uns
ec9b0 69 67 6e 65 64 20 63 68 61 72 20 2a 6b 65 79 20  igned char *key 
ec9c0 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
ec9d0 20 2a 29 70 48 6f 73 74 49 44 3b 0a 20 20 0a 20   *)pHostID;.  . 
ec9e0 20 6d 65 6d 73 65 74 28 6b 65 79 2c 20 30 2c 20   memset(key, 0, 
ec9f0 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 20 6c 65  HOSTIDLEN);.  le
eca00 6e 20 3d 20 30 3b 0a 20 20 66 64 20 3d 20 6f 70  n = 0;.  fd = op
eca10 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d  en("/dev/urandom
eca20 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20  ", O_RDONLY);.  
eca30 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a 20 20 20  if( fd>=0 ){.   
eca40 20 6c 65 6e 20 3d 20 72 65 61 64 28 66 64 2c 20   len = read(fd, 
eca50 6b 65 79 2c 20 48 4f 53 54 49 44 4c 45 4e 29 3b  key, HOSTIDLEN);
eca60 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20  .    close(fd); 
eca70 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b  /* silently leak
eca80 20 74 68 65 20 66 64 20 69 66 20 69 74 20 66 61   the fd if it fa
eca90 69 6c 73 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28  ils */.  }.  if(
ecaa0 20 6c 65 6e 20 3c 20 48 4f 53 54 49 44 4c 45 4e   len < HOSTIDLEN
ecab0 20 29 7b 0a 20 20 20 20 74 69 6d 65 5f 74 20 74   ){.    time_t t
ecac0 3b 0a 20 20 20 20 74 69 6d 65 28 26 74 29 3b 0a  ;.    time(&t);.
ecad0 20 20 20 20 6d 65 6d 63 70 79 28 6b 65 79 2c 20      memcpy(key, 
ecae0 26 74 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b 0a  &t, sizeof(t));.
ecaf0 20 20 20 20 70 69 64 20 3d 20 67 65 74 70 69 64      pid = getpid
ecb00 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  ();.    memcpy(&
ecb10 6b 65 79 5b 73 69 7a 65 6f 66 28 74 29 5d 2c 20  key[sizeof(t)], 
ecb20 26 70 69 64 2c 20 73 69 7a 65 6f 66 28 70 69 64  &pid, sizeof(pid
ecb30 29 29 3b 0a 20 20 7d 0a 20 20 0a 23 69 66 64 65  ));.  }.  .#ifde
ecb40 66 20 4d 41 4b 45 5f 50 52 45 54 54 59 5f 48 4f  f MAKE_PRETTY_HO
ecb50 53 54 49 44 0a 20 20 7b 0a 20 20 20 20 69 6e 74  STID.  {.    int
ecb60 20 69 3b 0a 20 20 20 20 2f 2a 20 66 69 6c 74 65   i;.    /* filte
ecb70 72 20 74 68 65 20 62 79 74 65 73 20 69 6e 74 6f  r the bytes into
ecb80 20 70 72 69 6e 74 61 62 6c 65 20 61 73 63 69 69   printable ascii
ecb90 20 63 68 61 72 61 63 74 65 72 73 20 61 6e 64 20   characters and 
ecba0 4e 55 4c 20 74 65 72 6d 69 6e 61 74 65 20 2a 2f  NUL terminate */
ecbb0 0a 20 20 20 20 6b 65 79 5b 28 48 4f 53 54 49 44  .    key[(HOSTID
ecbc0 4c 45 4e 2d 31 29 5d 20 3d 20 30 78 30 30 3b 0a  LEN-1)] = 0x00;.
ecbd0 20 20 20 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c      for( i=0; i<
ecbe0 28 48 4f 53 54 49 44 4c 45 4e 2d 31 29 3b 20 69  (HOSTIDLEN-1); i
ecbf0 2b 2b 20 29 7b 0a 20 20 20 20 20 20 75 6e 73 69  ++ ){.      unsi
ecc00 67 6e 65 64 20 63 68 61 72 20 70 61 20 3d 20 6b  gned char pa = k
ecc10 65 79 5b 69 5d 26 30 78 37 46 3b 0a 20 20 20 20  ey[i]&0x7F;.    
ecc20 20 20 69 66 28 20 70 61 3c 30 78 32 30 20 29 7b    if( pa<0x20 ){
ecc30 0a 20 20 20 20 20 20 20 20 6b 65 79 5b 69 5d 20  .        key[i] 
ecc40 3d 20 28 6b 65 79 5b 69 5d 26 30 78 38 30 20 3d  = (key[i]&0x80 =
ecc50 3d 20 30 78 38 30 29 20 3f 20 70 61 2b 30 78 34  = 0x80) ? pa+0x4
ecc60 30 20 3a 20 70 61 2b 30 78 32 30 3b 0a 20 20 20  0 : pa+0x20;.   
ecc70 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 61 3d     }else if( pa=
ecc80 3d 30 78 37 46 20 29 7b 0a 20 20 20 20 20 20 20  =0x7F ){.       
ecc90 20 6b 65 79 5b 69 5d 20 3d 20 28 6b 65 79 5b 69   key[i] = (key[i
ecca0 5d 26 30 78 38 30 20 3d 3d 20 30 78 38 30 29 20  ]&0x80 == 0x80) 
eccb0 3f 20 70 61 3d 30 78 32 30 20 3a 20 70 61 2b 30  ? pa=0x20 : pa+0
eccc0 78 37 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  x7E;.      }.   
eccd0 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20   }.  }.#endif.  
ecce0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
eccf0 3b 0a 7d 0a 0a 2f 2a 20 77 72 69 74 65 73 20 74  ;.}../* writes t
ecd00 68 65 20 68 6f 73 74 20 69 64 20 70 61 74 68 20  he host id path 
ecd10 74 6f 20 70 61 74 68 2c 20 70 61 74 68 20 73 68  to path, path sh
ecd20 6f 75 6c 64 20 62 65 20 61 6e 20 70 72 65 2d 61  ould be an pre-a
ecd30 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 0a  llocated buffer.
ecd40 2a 2a 20 77 69 74 68 20 65 6e 6f 75 67 68 20 73  ** with enough s
ecd50 70 61 63 65 20 66 6f 72 20 61 20 70 61 74 68 20  pace for a path 
ecd60 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
ecd70 70 72 6f 78 79 47 65 74 48 6f 73 74 49 44 50 61  proxyGetHostIDPa
ecd80 74 68 28 63 68 61 72 20 2a 70 61 74 68 2c 20 73  th(char *path, s
ecd90 69 7a 65 5f 74 20 6c 65 6e 29 7b 0a 20 20 73 74  ize_t len){.  st
ecda0 72 6c 63 70 79 28 70 61 74 68 2c 20 48 4f 53 54  rlcpy(path, HOST
ecdb0 49 44 50 41 54 48 2c 20 6c 65 6e 29 3b 0a 23 69  IDPATH, len);.#i
ecdc0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
ecdd0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 68  .  if( sqlite3_h
ecde0 6f 73 74 69 64 5f 6e 75 6d 3e 30 20 29 7b 0a 20  ostid_num>0 ){. 
ecdf0 20 20 20 63 68 61 72 20 73 75 66 66 69 78 5b 32     char suffix[2
ece00 5d 20 3d 20 22 31 22 3b 0a 20 20 20 20 73 75 66  ] = "1";.    suf
ece10 66 69 78 5b 30 5d 20 3d 20 73 75 66 66 69 78 5b  fix[0] = suffix[
ece20 30 5d 20 2b 20 73 71 6c 69 74 65 33 5f 68 6f 73  0] + sqlite3_hos
ece30 74 69 64 5f 6e 75 6d 3b 0a 20 20 20 20 73 74 72  tid_num;.    str
ece40 6c 63 61 74 28 70 61 74 68 2c 20 73 75 66 66 69  lcat(path, suffi
ece50 78 2c 20 6c 65 6e 29 3b 0a 20 20 7d 0a 23 65 6e  x, len);.  }.#en
ece60 64 69 66 0a 20 20 4f 53 54 52 41 43 45 33 28 22  dif.  OSTRACE3("
ece70 47 45 54 48 4f 53 54 49 44 50 41 54 48 20 20 25  GETHOSTIDPATH  %
ece80 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 61 74  s pid=%d\n", pat
ece90 68 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 7d 0a  h, getpid());.}.
ecea0 0a 2f 2a 20 67 65 74 20 74 68 65 20 68 6f 73 74  ./* get the host
eceb0 20 49 44 20 66 72 6f 6d 20 61 20 73 71 6c 69 74   ID from a sqlit
ecec0 65 20 68 6f 73 74 69 64 20 66 69 6c 65 20 73 74  e hostid file st
eced0 6f 72 65 64 20 69 6e 20 74 68 65 20 0a 2a 2a 20  ored in the .** 
ecee0 75 73 65 72 2d 73 70 65 63 69 66 69 63 20 74 6d  user-specific tm
ecef0 70 20 64 69 72 65 63 74 6f 72 79 2c 20 63 72 65  p directory, cre
ecf00 61 74 65 20 74 68 65 20 49 44 20 69 66 20 69 74  ate the ID if it
ecf10 27 73 20 6e 6f 74 20 74 68 65 72 65 20 61 6c 72  's not there alr
ecf20 65 61 64 79 20 0a 2a 2f 0a 73 74 61 74 69 63 20  eady .*/.static 
ecf30 69 6e 74 20 70 72 6f 78 79 47 65 74 48 6f 73 74  int proxyGetHost
ecf40 49 44 28 63 68 61 72 20 2a 70 48 6f 73 74 49 44  ID(char *pHostID
ecf50 2c 20 69 6e 74 20 2a 70 45 72 72 6f 72 29 7b 0a  , int *pError){.
ecf60 20 20 69 6e 74 20 66 64 3b 0a 20 20 63 68 61 72    int fd;.  char
ecf70 20 70 61 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e   path[MAXPATHLEN
ecf80 5d 3b 20 0a 20 20 73 69 7a 65 5f 74 20 6c 65 6e  ]; .  size_t len
ecf90 3b 0a 20 20 69 6e 74 20 72 63 3d 53 51 4c 49 54  ;.  int rc=SQLIT
ecfa0 45 5f 4f 4b 3b 0a 0a 20 20 70 72 6f 78 79 47 65  E_OK;..  proxyGe
ecfb0 74 48 6f 73 74 49 44 50 61 74 68 28 70 61 74 68  tHostIDPath(path
ecfc0 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20  , MAXPATHLEN);. 
ecfd0 20 2f 2a 20 74 72 79 20 74 6f 20 63 72 65 61 74   /* try to creat
ecfe0 65 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 69  e the host ID fi
ecff0 6c 65 2c 20 69 66 20 69 74 20 61 6c 72 65 61 64  le, if it alread
ed000 79 20 65 78 69 73 74 73 20 72 65 61 64 20 74 68  y exists read th
ed010 65 20 63 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20  e contents */.  
ed020 66 64 20 3d 20 6f 70 65 6e 28 70 61 74 68 2c 20  fd = open(path, 
ed030 4f 5f 43 52 45 41 54 7c 4f 5f 57 52 4f 4e 4c 59  O_CREAT|O_WRONLY
ed040 7c 4f 5f 45 58 43 4c 2c 20 30 36 34 34 29 3b 0a  |O_EXCL, 0644);.
ed050 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20    if( fd<0 ){.  
ed060 20 20 69 6e 74 20 65 72 72 3d 65 72 72 6e 6f 3b    int err=errno;
ed070 0a 09 09 0a 20 20 20 20 69 66 28 20 65 72 72 21  ....    if( err!
ed080 3d 45 45 58 49 53 54 20 29 7b 0a 23 69 66 64 65  =EEXIST ){.#ifde
ed090 66 20 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44  f SQLITE_PROXY_D
ed0a0 45 42 55 47 20 2f 2a 20 73 65 74 20 74 68 65 20  EBUG /* set the 
ed0b0 73 71 6c 69 74 65 20 65 72 72 6f 72 20 6d 65 73  sqlite error mes
ed0c0 73 61 67 65 20 69 6e 73 74 65 61 64 20 2a 2f 0a  sage instead */.
ed0d0 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74        fprintf(st
ed0e0 64 65 72 72 2c 20 22 73 71 6c 69 74 65 20 65 72  derr, "sqlite er
ed0f0 72 6f 72 20 63 72 65 61 74 69 6e 67 20 68 6f 73  ror creating hos
ed100 74 20 49 44 20 66 69 6c 65 20 25 73 3a 20 25 73  t ID file %s: %s
ed110 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
ed120 20 20 20 70 61 74 68 2c 20 73 74 72 65 72 72 6f     path, strerro
ed130 72 28 65 72 72 29 29 3b 0a 23 65 6e 64 69 66 0a  r(err));.#endif.
ed140 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
ed150 49 54 45 5f 50 45 52 4d 3b 0a 20 20 20 20 7d 0a  ITE_PERM;.    }.
ed160 20 20 20 20 2f 2a 20 63 6f 75 6c 64 6e 27 74 20      /* couldn't 
ed170 63 72 65 61 74 65 20 74 68 65 20 66 69 6c 65 2c  create the file,
ed180 20 72 65 61 64 20 69 74 20 69 6e 73 74 65 61 64   read it instead
ed190 20 2a 2f 0a 20 20 20 20 66 64 20 3d 20 6f 70 65   */.    fd = ope
ed1a0 6e 28 70 61 74 68 2c 20 4f 5f 52 44 4f 4e 4c 59  n(path, O_RDONLY
ed1b0 7c 4f 5f 45 58 43 4c 29 3b 0a 20 20 20 20 69 66  |O_EXCL);.    if
ed1c0 28 20 66 64 3c 30 20 29 7b 0a 23 69 66 64 65 66  ( fd<0 ){.#ifdef
ed1d0 20 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45   SQLITE_PROXY_DE
ed1e0 42 55 47 20 2f 2a 20 73 65 74 20 74 68 65 20 73  BUG /* set the s
ed1f0 71 6c 69 74 65 20 65 72 72 6f 72 20 6d 65 73 73  qlite error mess
ed200 61 67 65 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20  age instead */. 
ed210 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 65       int err = e
ed220 72 72 6e 6f 3b 0a 20 20 20 20 20 20 66 70 72 69  rrno;.      fpri
ed230 6e 74 66 28 73 74 64 65 72 72 2c 20 22 73 71 6c  ntf(stderr, "sql
ed240 69 74 65 20 65 72 72 6f 72 20 6f 70 65 6e 69 6e  ite error openin
ed250 67 20 68 6f 73 74 20 49 44 20 66 69 6c 65 20 25  g host ID file %
ed260 73 3a 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20  s: %s\n",.      
ed270 20 20 20 20 20 20 20 20 70 61 74 68 2c 20 73 74          path, st
ed280 72 65 72 72 6f 72 28 65 72 72 29 29 3b 0a 23 65  rerror(err));.#e
ed290 6e 64 69 66 0a 20 20 20 20 20 20 72 65 74 75 72  ndif.      retur
ed2a0 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20  n SQLITE_PERM;. 
ed2b0 20 20 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 70     }.    len = p
ed2c0 72 65 61 64 28 66 64 2c 20 70 48 6f 73 74 49 44  read(fd, pHostID
ed2d0 2c 20 48 4f 53 54 49 44 4c 45 4e 2c 20 30 29 3b  , HOSTIDLEN, 0);
ed2e0 0a 20 20 20 20 69 66 28 20 6c 65 6e 3c 30 20 29  .    if( len<0 )
ed2f0 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20  {.      *pError 
ed300 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72  = errno;.      r
ed310 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
ed320 5f 52 45 41 44 3b 0a 20 20 20 20 7d 65 6c 73 65  _READ;.    }else
ed330 20 69 66 28 20 6c 65 6e 3c 48 4f 53 54 49 44 4c   if( len<HOSTIDL
ed340 45 4e 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72  EN ){.      *pEr
ed350 72 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 72  ror = 0;.      r
ed360 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
ed370 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 20  _SHORT_READ;.   
ed380 20 7d 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 29   }.    close(fd)
ed390 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65  ; /* silently le
ed3a0 61 6b 20 74 68 65 20 66 64 20 69 66 20 69 74 20  ak the fd if it 
ed3b0 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 4f 53 54  fails */.    OST
ed3c0 52 41 43 45 33 28 22 47 45 54 48 4f 53 54 49 44  RACE3("GETHOSTID
ed3d0 20 20 72 65 61 64 20 25 73 20 70 69 64 3d 25 64    read %s pid=%d
ed3e0 5c 6e 22 2c 20 70 48 6f 73 74 49 44 2c 20 67 65  \n", pHostID, ge
ed3f0 74 70 69 64 28 29 29 3b 0a 20 20 20 20 72 65 74  tpid());.    ret
ed400 75 72 6e 20 72 63 3b 0a 20 20 7d 65 6c 73 65 7b  urn rc;.  }else{
ed410 0a 20 20 20 20 2f 2a 20 77 65 27 72 65 20 63 72  .    /* we're cr
ed420 65 61 74 69 6e 67 20 74 68 65 20 68 6f 73 74 20  eating the host 
ed430 49 44 20 66 69 6c 65 20 28 75 73 65 20 61 20 72  ID file (use a r
ed440 61 6e 64 6f 6d 20 73 74 72 69 6e 67 20 6f 66 20  andom string of 
ed450 62 79 74 65 73 29 20 2a 2f 0a 20 20 20 20 70 72  bytes) */.    pr
ed460 6f 78 79 47 65 6e 65 72 61 74 65 48 6f 73 74 49  oxyGenerateHostI
ed470 44 28 70 48 6f 73 74 49 44 29 3b 0a 20 20 20 20  D(pHostID);.    
ed480 6c 65 6e 20 3d 20 70 77 72 69 74 65 28 66 64 2c  len = pwrite(fd,
ed490 20 70 48 6f 73 74 49 44 2c 20 48 4f 53 54 49 44   pHostID, HOSTID
ed4a0 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  LEN, 0);.    if(
ed4b0 20 6c 65 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20   len<0 ){.      
ed4c0 2a 70 45 72 72 6f 72 20 3d 20 65 72 72 6e 6f 3b  *pError = errno;
ed4d0 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
ed4e0 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a  TE_IOERR_WRITE;.
ed4f0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65      }else if( le
ed500 6e 3c 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a 20  n<HOSTIDLEN ){. 
ed510 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 30       *pError = 0
ed520 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ;.      rc = SQL
ed530 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b  ITE_IOERR_WRITE;
ed540 0a 20 20 20 20 7d 0a 20 20 20 20 63 6c 6f 73 65  .    }.    close
ed550 28 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c  (fd); /* silentl
ed560 79 20 6c 65 61 6b 20 74 68 65 20 66 64 20 69 66  y leak the fd if
ed570 20 69 74 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20   it fails */.   
ed580 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 48 4f   OSTRACE3("GETHO
ed590 53 54 49 44 20 20 77 72 6f 74 65 20 25 73 20 70  STID  wrote %s p
ed5a0 69 64 3d 25 64 5c 6e 22 2c 20 70 48 6f 73 74 49  id=%d\n", pHostI
ed5b0 44 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20  D, getpid());.  
ed5c0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
ed5d0 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .}..static int p
ed5e0 72 6f 78 79 47 65 74 4c 6f 63 6b 50 61 74 68 28  roxyGetLockPath(
ed5f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 62 50 61  const char *dbPa
ed600 74 68 2c 20 63 68 61 72 20 2a 6c 50 61 74 68 2c  th, char *lPath,
ed610 20 73 69 7a 65 5f 74 20 6d 61 78 4c 65 6e 29 7b   size_t maxLen){
ed620 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e  .  int len;.  in
ed630 74 20 64 62 4c 65 6e 3b 0a 20 20 69 6e 74 20 69  t dbLen;.  int i
ed640 3b 0a 0a 23 69 66 64 65 66 20 4c 4f 43 4b 50 52  ;..#ifdef LOCKPR
ed650 4f 58 59 44 49 52 0a 20 20 6c 65 6e 20 3d 20 73  OXYDIR.  len = s
ed660 74 72 6c 63 70 79 28 6c 50 61 74 68 2c 20 4c 4f  trlcpy(lPath, LO
ed670 43 4b 50 52 4f 58 59 44 49 52 2c 20 6d 61 78 4c  CKPROXYDIR, maxL
ed680 65 6e 29 3b 0a 23 65 6c 73 65 0a 23 20 69 66 64  en);.#else.# ifd
ed690 65 66 20 5f 43 53 5f 44 41 52 57 49 4e 5f 55 53  ef _CS_DARWIN_US
ed6a0 45 52 5f 54 45 4d 50 5f 44 49 52 0a 20 20 7b 0a  ER_TEMP_DIR.  {.
ed6b0 20 20 20 20 63 6f 6e 66 73 74 72 28 5f 43 53 5f      confstr(_CS_
ed6c0 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45 4d 50  DARWIN_USER_TEMP
ed6d0 5f 44 49 52 2c 20 6c 50 61 74 68 2c 20 6d 61 78  _DIR, lPath, max
ed6e0 4c 65 6e 29 3b 0a 20 20 20 20 6c 65 6e 20 3d 20  Len);.    len = 
ed6f0 73 74 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 22  strlcat(lPath, "
ed700 73 71 6c 69 74 65 70 6c 6f 63 6b 73 22 2c 20 6d  sqliteplocks", m
ed710 61 78 4c 65 6e 29 3b 0a 20 20 20 20 69 66 28 20  axLen);.    if( 
ed720 6d 6b 64 69 72 28 6c 50 61 74 68 2c 20 53 51 4c  mkdir(lPath, SQL
ed730 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58  ITE_DEFAULT_PROX
ed740 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53  YDIR_PERMISSIONS
ed750 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66  ) ){.      /* if
ed760 20 6d 6b 64 69 72 20 66 61 69 6c 73 2c 20 68 61   mkdir fails, ha
ed770 6e 64 6c 65 20 61 73 20 6c 6f 63 6b 20 66 69 6c  ndle as lock fil
ed780 65 20 63 72 65 61 74 69 6f 6e 20 66 61 69 6c 75  e creation failu
ed790 72 65 20 2a 2f 0a 23 20 20 69 66 64 65 66 20 53  re */.#  ifdef S
ed7a0 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20  QLITE_DEBUG.    
ed7b0 20 20 69 6e 74 20 65 72 72 20 3d 20 65 72 72 6e    int err = errn
ed7c0 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 65 72 72  o;.      if( err
ed7d0 21 3d 45 45 58 49 53 54 20 29 7b 0a 20 20 20 20  !=EEXIST ){.    
ed7e0 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65      fprintf(stde
ed7f0 72 72 2c 20 22 70 72 6f 78 79 47 65 74 4c 6f 63  rr, "proxyGetLoc
ed800 6b 50 61 74 68 3a 20 6d 6b 64 69 72 28 25 73 2c  kPath: mkdir(%s,
ed810 30 25 6f 29 20 65 72 72 6f 72 20 25 64 20 25 73  0%o) error %d %s
ed820 5c 6e 22 2c 20 6c 50 61 74 68 2c 0a 20 20 20 20  \n", lPath,.    
ed830 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
ed840 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 59  TE_DEFAULT_PROXY
ed850 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 2c  DIR_PERMISSIONS,
ed860 20 65 72 72 2c 20 73 74 72 65 72 72 6f 72 28 65   err, strerror(e
ed870 72 72 29 29 3b 0a 20 20 20 20 20 20 7d 0a 23 20  rr));.      }.# 
ed880 20 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65   endif.    }else
ed890 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33  {.      OSTRACE3
ed8a0 28 22 47 45 54 4c 4f 43 4b 50 41 54 48 20 20 6d  ("GETLOCKPATH  m
ed8b0 6b 64 69 72 20 25 73 20 70 69 64 3d 25 64 5c 6e  kdir %s pid=%d\n
ed8c0 22 2c 20 6c 50 61 74 68 2c 20 67 65 74 70 69 64  ", lPath, getpid
ed8d0 28 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a  ());.    }.    .
ed8e0 20 20 7d 0a 23 20 65 6c 73 65 0a 20 20 6c 65 6e    }.# else.  len
ed8f0 20 3d 20 73 74 72 6c 63 70 79 28 6c 50 61 74 68   = strlcpy(lPath
ed900 2c 20 22 2f 74 6d 70 2f 22 2c 20 6d 61 78 4c 65  , "/tmp/", maxLe
ed910 6e 29 3b 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64  n);.# endif.#end
ed920 69 66 0a 0a 20 20 69 66 28 20 6c 50 61 74 68 5b  if..  if( lPath[
ed930 6c 65 6e 2d 31 5d 21 3d 27 2f 27 20 29 7b 0a 20  len-1]!='/' ){. 
ed940 20 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 61 74     len = strlcat
ed950 28 6c 50 61 74 68 2c 20 22 2f 22 2c 20 6d 61 78  (lPath, "/", max
ed960 4c 65 6e 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f  Len);.  }.  .  /
ed970 2a 20 74 72 61 6e 73 66 6f 72 6d 20 74 68 65 20  * transform the 
ed980 64 62 20 70 61 74 68 20 74 6f 20 61 20 75 6e 69  db path to a uni
ed990 71 75 65 20 63 61 63 68 65 20 6e 61 6d 65 20 2a  que cache name *
ed9a0 2f 0a 20 20 64 62 4c 65 6e 20 3d 20 28 69 6e 74  /.  dbLen = (int
ed9b0 29 73 74 72 6c 65 6e 28 64 62 50 61 74 68 29 3b  )strlen(dbPath);
ed9c0 0a 20 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 64  .  for( i=0; i<d
ed9d0 62 4c 65 6e 20 26 26 20 28 69 2b 6c 65 6e 2b 37  bLen && (i+len+7
ed9e0 29 3c 6d 61 78 4c 65 6e 3b 20 69 2b 2b 29 7b 0a  )<maxLen; i++){.
ed9f0 20 20 20 20 63 68 61 72 20 63 20 3d 20 64 62 50      char c = dbP
eda00 61 74 68 5b 69 5d 3b 0a 20 20 20 20 6c 50 61 74  ath[i];.    lPat
eda10 68 5b 69 2b 6c 65 6e 5d 20 3d 20 28 63 3d 3d 27  h[i+len] = (c=='
eda20 2f 27 29 3f 27 5f 27 3a 63 3b 0a 20 20 7d 0a 20  /')?'_':c;.  }. 
eda30 20 6c 50 61 74 68 5b 69 2b 6c 65 6e 5d 3d 27 5c   lPath[i+len]='\
eda40 30 27 3b 0a 20 20 73 74 72 6c 63 61 74 28 6c 50  0';.  strlcat(lP
eda50 61 74 68 2c 20 22 3a 61 75 74 6f 3a 22 2c 20 6d  ath, ":auto:", m
eda60 61 78 4c 65 6e 29 3b 0a 20 20 72 65 74 75 72 6e  axLen);.  return
eda70 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
eda80 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
eda90 77 20 56 46 53 20 66 69 6c 65 20 64 65 73 63 72  w VFS file descr
edaa0 69 70 74 6f 72 20 28 73 74 6f 72 65 64 20 69 6e  iptor (stored in
edab0 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
edac0 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33   from.** sqlite3
edad0 5f 6d 61 6c 6c 6f 63 29 20 61 6e 64 20 6f 70 65  _malloc) and ope
edae0 6e 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 64  n the file named
edaf0 20 22 70 61 74 68 22 20 69 6e 20 74 68 65 20 66   "path" in the f
edb00 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 0a  ile descriptor..
edb10 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72  **.** The caller
edb20 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
edb30 6e 6f 74 20 6f 6e 6c 79 20 66 6f 72 20 63 6c 6f  not only for clo
edb40 73 69 6e 67 20 74 68 65 20 66 69 6c 65 20 64 65  sing the file de
edb50 73 63 72 69 70 74 6f 72 0a 2a 2a 20 62 75 74 20  scriptor.** but 
edb60 61 6c 73 6f 20 66 6f 72 20 66 72 65 65 69 6e 67  also for freeing
edb70 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 73 73 6f   the memory asso
edb80 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
edb90 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e  file descriptor.
edba0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
edbb0 72 6f 78 79 43 72 65 61 74 65 55 6e 69 78 46 69  roxyCreateUnixFi
edbc0 6c 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  le(const char *p
edbd0 61 74 68 2c 20 75 6e 69 78 46 69 6c 65 20 2a 2a  ath, unixFile **
edbe0 70 70 46 69 6c 65 29 20 7b 0a 20 20 75 6e 69 78  ppFile) {.  unix
edbf0 46 69 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 69 6e  File *pNew;.  in
edc00 74 20 66 6c 61 67 73 20 3d 20 53 51 4c 49 54 45  t flags = SQLITE
edc10 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 7c 53 51  _OPEN_MAIN_DB|SQ
edc20 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
edc30 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  |SQLITE_OPEN_REA
edc40 44 57 52 49 54 45 3b 0a 20 20 69 6e 74 20 72 63  DWRITE;.  int rc
edc50 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
edc60 73 71 6c 69 74 65 33 5f 76 66 73 20 64 75 6d 6d  sqlite3_vfs dumm
edc70 79 56 66 73 3b 0a 0a 20 20 70 4e 65 77 20 3d 20  yVfs;..  pNew = 
edc80 28 75 6e 69 78 46 69 6c 65 20 2a 29 73 71 6c 69  (unixFile *)sqli
edc90 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
edca0 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 20 20  f(unixFile));.  
edcb0 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20  if( !pNew ){.   
edcc0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
edcd0 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  OMEM;.  }.  mems
edce0 65 74 28 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65  et(pNew, 0, size
edcf0 6f 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 0a  of(unixFile));..
edd00 20 20 2f 2a 20 43 61 6c 6c 20 75 6e 69 78 4f 70    /* Call unixOp
edd10 65 6e 28 29 20 74 6f 20 6f 70 65 6e 20 74 68 65  en() to open the
edd20 20 70 72 6f 78 79 20 66 69 6c 65 2e 20 54 68 65   proxy file. The
edd30 20 66 6c 61 67 73 20 70 61 73 73 65 64 20 74 6f   flags passed to
edd40 20 75 6e 69 78 4f 70 65 6e 28 29 0a 20 20 2a 2a   unixOpen().  **
edd50 20 73 75 67 67 65 73 74 20 74 68 61 74 20 74 68   suggest that th
edd60 65 20 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65  e file being ope
edd70 6e 65 64 20 69 73 20 61 20 22 6d 61 69 6e 20 64  ned is a "main d
edd80 61 74 61 62 61 73 65 22 2e 20 54 68 69 73 20 69  atabase". This i
edd90 73 0a 20 20 2a 2a 20 6e 65 63 65 73 73 61 72 79  s.  ** necessary
edda0 20 61 73 20 6f 74 68 65 72 20 66 69 6c 65 20 74   as other file t
eddb0 79 70 65 73 20 64 6f 20 6e 6f 74 20 6e 65 63 65  ypes do not nece
eddc0 73 73 61 72 69 6c 79 20 73 75 70 70 6f 72 74 20  ssarily support 
eddd0 6c 6f 63 6b 69 6e 67 2e 20 49 74 0a 20 20 2a 2a  locking. It.  **
edde0 20 69 73 20 62 65 74 74 65 72 20 74 6f 20 75 73   is better to us
eddf0 65 20 75 6e 69 78 4f 70 65 6e 28 29 20 69 6e 73  e unixOpen() ins
ede00 74 65 61 64 20 6f 66 20 6f 70 65 6e 69 6e 67 20  tead of opening 
ede10 74 68 65 20 66 69 6c 65 20 64 69 72 65 63 74 6c  the file directl
ede20 79 20 77 69 74 68 0a 20 20 2a 2a 20 6f 70 65 6e  y with.  ** open
ede30 28 29 2c 20 61 73 20 75 6e 69 78 4f 70 65 6e 28  (), as unixOpen(
ede40 29 20 73 65 74 73 20 75 70 20 74 68 65 20 76 61  ) sets up the va
ede50 72 69 6f 75 73 20 6d 65 63 68 61 6e 69 73 6d 73  rious mechanisms
ede60 20 72 65 71 75 69 72 65 64 20 74 6f 0a 20 20 2a   required to.  *
ede70 2a 20 6d 61 6b 65 20 73 75 72 65 20 61 20 63 61  * make sure a ca
ede80 6c 6c 20 74 6f 20 63 6c 6f 73 65 28 29 20 64 6f  ll to close() do
ede90 65 73 20 6e 6f 74 20 63 61 75 73 65 20 74 68 65  es not cause the
edea0 20 73 79 73 74 65 6d 20 74 6f 20 64 69 73 63 61   system to disca
edeb0 72 64 0a 20 20 2a 2a 20 50 4f 53 49 58 20 6c 6f  rd.  ** POSIX lo
edec0 63 6b 73 20 70 72 65 6d 61 74 75 72 65 6c 79 2e  cks prematurely.
eded0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 74 20 69 73  .  **.  ** It is
edee0 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20   important that 
edef0 74 68 65 20 78 4f 70 65 6e 20 6d 65 6d 62 65 72  the xOpen member
edf00 20 6f 66 20 74 68 65 20 56 46 53 20 6f 62 6a 65   of the VFS obje
edf10 63 74 20 70 61 73 73 65 64 20 74 6f 20 0a 20 20  ct passed to .  
edf20 2a 2a 20 75 6e 69 78 4f 70 65 6e 28 29 20 69 73  ** unixOpen() is
edf30 20 4e 55 4c 4c 2e 20 54 68 69 73 20 74 65 6c 6c   NULL. This tell
edf40 73 20 75 6e 69 78 4f 70 65 6e 28 29 20 6d 61 79  s unixOpen() may
edf50 20 74 72 79 20 74 6f 20 6f 70 65 6e 20 61 20 70   try to open a p
edf60 72 6f 78 79 2d 66 69 6c 65 20 0a 20 20 2a 2a 20  roxy-file .  ** 
edf70 66 6f 72 20 74 68 65 20 70 72 6f 78 79 2d 66 69  for the proxy-fi
edf80 6c 65 20 28 63 72 65 61 74 69 6e 67 20 61 20 70  le (creating a p
edf90 6f 74 65 6e 74 69 61 6c 20 69 6e 66 69 6e 69 74  otential infinit
edfa0 65 20 6c 6f 6f 70 29 2e 0a 20 20 2a 2f 0a 20 20  e loop)..  */.  
edfb0 64 75 6d 6d 79 56 66 73 2e 70 41 70 70 44 61 74  dummyVfs.pAppDat
edfc0 61 20 3d 20 28 76 6f 69 64 2a 29 26 61 75 74 6f  a = (void*)&auto
edfd0 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 3b 0a 20 20  lockIoFinder;.  
edfe0 64 75 6d 6d 79 56 66 73 2e 78 4f 70 65 6e 20 3d  dummyVfs.xOpen =
edff0 20 30 3b 0a 20 20 72 63 20 3d 20 75 6e 69 78 4f   0;.  rc = unixO
ee000 70 65 6e 28 26 64 75 6d 6d 79 56 66 73 2c 20 70  pen(&dummyVfs, p
ee010 61 74 68 2c 20 28 73 71 6c 69 74 65 33 5f 66 69  ath, (sqlite3_fi
ee020 6c 65 20 2a 29 70 4e 65 77 2c 20 66 6c 61 67 73  le *)pNew, flags
ee030 2c 20 26 66 6c 61 67 73 29 3b 0a 20 20 69 66 28  , &flags);.  if(
ee040 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
ee050 26 20 28 66 6c 61 67 73 26 53 51 4c 49 54 45 5f  & (flags&SQLITE_
ee060 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20 29  OPEN_READONLY) )
ee070 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4d 65 74  {.    pNew->pMet
ee080 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c  hod->xClose((sql
ee090 69 74 65 33 5f 66 69 6c 65 20 2a 29 70 4e 65 77  ite3_file *)pNew
ee0a0 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
ee0b0 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d  TE_CANTOPEN;.  }
ee0c0 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ..  if( rc!=SQLI
ee0d0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
ee0e0 69 74 65 33 5f 66 72 65 65 28 70 4e 65 77 29 3b  ite3_free(pNew);
ee0f0 0a 20 20 20 20 70 4e 65 77 20 3d 20 30 3b 0a 20  .    pNew = 0;. 
ee100 20 7d 0a 0a 20 20 2a 70 70 46 69 6c 65 20 3d 20   }..  *ppFile = 
ee110 70 4e 65 77 3b 0a 20 20 72 65 74 75 72 6e 20 72  pNew;.  return r
ee120 63 3b 0a 7d 0a 0a 2f 2a 20 74 61 6b 65 73 20 74  c;.}../* takes t
ee130 68 65 20 63 6f 6e 63 68 20 62 79 20 74 61 6b 69  he conch by taki
ee140 6e 67 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b  ng a shared lock
ee150 20 61 6e 64 20 72 65 61 64 20 74 68 65 20 63 6f   and read the co
ee160 6e 74 65 6e 74 73 20 63 6f 6e 63 68 2c 20 69 66  ntents conch, if
ee170 20 0a 2a 2a 20 6c 6f 63 6b 50 61 74 68 20 69 73   .** lockPath is
ee180 20 6e 6f 6e 2d 4e 55 4c 4c 2c 20 74 68 65 20 68   non-NULL, the h
ee190 6f 73 74 20 49 44 20 61 6e 64 20 6c 6f 63 6b 20  ost ID and lock 
ee1a0 66 69 6c 65 20 70 61 74 68 20 6d 75 73 74 20 6d  file path must m
ee1b0 61 74 63 68 2e 20 20 41 20 4e 55 4c 4c 20 0a 2a  atch.  A NULL .*
ee1c0 2a 20 6c 6f 63 6b 50 61 74 68 20 6d 65 61 6e 73  * lockPath means
ee1d0 20 74 68 61 74 20 74 68 65 20 6c 6f 63 6b 50 61   that the lockPa
ee1e0 74 68 20 69 6e 20 74 68 65 20 63 6f 6e 63 68 20  th in the conch 
ee1f0 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 75 73 65  file will be use
ee200 64 20 69 66 20 74 68 65 20 0a 2a 2a 20 68 6f 73  d if the .** hos
ee210 74 20 49 44 73 20 6d 61 74 63 68 2c 20 6f 72 20  t IDs match, or 
ee220 61 20 6e 65 77 20 6c 6f 63 6b 20 70 61 74 68 20  a new lock path 
ee230 77 69 6c 6c 20 62 65 20 67 65 6e 65 72 61 74 65  will be generate
ee240 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  d automatically 
ee250 0a 2a 2a 20 61 6e 64 20 77 72 69 74 74 65 6e 20  .** and written 
ee260 74 6f 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c  to the conch fil
ee270 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
ee280 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28   proxyTakeConch(
ee290 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29  unixFile *pFile)
ee2a0 7b 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67  {.  proxyLocking
ee2b0 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20  Context *pCtx = 
ee2c0 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e  (proxyLockingCon
ee2d0 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f  text *)pFile->lo
ee2e0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 0a 20  ckingContext; . 
ee2f0 20 0a 20 20 69 66 28 20 70 43 74 78 2d 3e 63 6f   .  if( pCtx->co
ee300 6e 63 68 48 65 6c 64 3e 30 20 29 7b 0a 20 20 20  nchHeld>0 ){.   
ee310 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
ee320 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  K;.  }else{.    
ee330 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46  unixFile *conchF
ee340 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63  ile = pCtx->conc
ee350 68 46 69 6c 65 3b 0a 20 20 20 20 63 68 61 72 20  hFile;.    char 
ee360 74 65 73 74 56 61 6c 75 65 5b 43 4f 4e 43 48 4c  testValue[CONCHL
ee370 45 4e 5d 3b 0a 20 20 20 20 63 68 61 72 20 63 6f  EN];.    char co
ee380 6e 63 68 56 61 6c 75 65 5b 43 4f 4e 43 48 4c 45  nchValue[CONCHLE
ee390 4e 5d 3b 0a 20 20 20 20 63 68 61 72 20 6c 6f 63  N];.    char loc
ee3a0 6b 50 61 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e  kPath[MAXPATHLEN
ee3b0 5d 3b 0a 20 20 20 20 63 68 61 72 20 2a 74 4c 6f  ];.    char *tLo
ee3c0 63 6b 50 61 74 68 20 3d 20 4e 55 4c 4c 3b 0a 20  ckPath = NULL;. 
ee3d0 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49     int rc = SQLI
ee3e0 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 6e 74 20 72  TE_OK;.    int r
ee3f0 65 61 64 52 63 20 3d 20 53 51 4c 49 54 45 5f 4f  eadRc = SQLITE_O
ee400 4b 3b 0a 20 20 20 20 69 6e 74 20 73 79 6e 63 50  K;.    int syncP
ee410 65 72 6d 73 20 3d 20 30 3b 0a 0a 20 20 20 20 4f  erms = 0;..    O
ee420 53 54 52 41 43 45 34 28 22 54 41 4b 45 43 4f 4e  STRACE4("TAKECON
ee430 43 48 20 20 25 64 20 66 6f 72 20 25 73 20 70 69  CH  %d for %s pi
ee440 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 68 46 69  d=%d\n", conchFi
ee450 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20  le->h,.         
ee460 20 20 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50      (pCtx->lockP
ee470 72 6f 78 79 50 61 74 68 20 3f 20 70 43 74 78 2d  roxyPath ? pCtx-
ee480 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3a  >lockProxyPath :
ee490 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67 65 74 70   ":auto:"), getp
ee4a0 69 64 28 29 29 3b 0a 0a 20 20 20 20 72 63 20 3d  id());..    rc =
ee4b0 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74   conchFile->pMet
ee4c0 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c 69  hod->xLock((sqli
ee4d0 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46  te3_file*)conchF
ee4e0 69 6c 65 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b  ile, SHARED_LOCK
ee4f0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
ee500 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
ee510 20 20 69 6e 74 20 70 45 72 72 6f 72 20 3d 20 30    int pError = 0
ee520 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 74  ;.      memset(t
ee530 65 73 74 56 61 6c 75 65 2c 20 30 2c 20 43 4f 4e  estValue, 0, CON
ee540 43 48 4c 45 4e 29 3b 20 2f 2a 20 63 6f 6e 63 68  CHLEN); /* conch
ee550 20 69 73 20 66 69 78 65 64 20 73 69 7a 65 20 2a   is fixed size *
ee560 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f  /.      rc = pro
ee570 78 79 47 65 74 48 6f 73 74 49 44 28 74 65 73 74  xyGetHostID(test
ee580 56 61 6c 75 65 2c 20 26 70 45 72 72 6f 72 29 3b  Value, &pError);
ee590 0a 20 20 20 20 20 20 69 66 28 20 28 72 63 26 30  .      if( (rc&0
ee5a0 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45  xff)==SQLITE_IOE
ee5b0 52 52 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46  RR ){.        pF
ee5c0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
ee5d0 20 70 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d   pError;.      }
ee5e0 0a 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d  .      if( pCtx-
ee5f0 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 29  >lockProxyPath )
ee600 7b 0a 20 20 20 20 20 20 20 20 73 74 72 6c 63 70  {.        strlcp
ee610 79 28 26 74 65 73 74 56 61 6c 75 65 5b 48 4f 53  y(&testValue[HOS
ee620 54 49 44 4c 45 4e 5d 2c 20 70 43 74 78 2d 3e 6c  TIDLEN], pCtx->l
ee630 6f 63 6b 50 72 6f 78 79 50 61 74 68 2c 20 4d 41  ockProxyPath, MA
ee640 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 20  XPATHLEN);.     
ee650 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
ee660 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
ee670 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64  {.      goto end
ee680 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20  _takeconch;.    
ee690 7d 0a 20 20 20 20 0a 20 20 20 20 72 65 61 64 52  }.    .    readR
ee6a0 63 20 3d 20 75 6e 69 78 52 65 61 64 28 28 73 71  c = unixRead((sq
ee6b0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 63 6f 6e  lite3_file *)con
ee6c0 63 68 46 69 6c 65 2c 20 63 6f 6e 63 68 56 61 6c  chFile, conchVal
ee6d0 75 65 2c 20 43 4f 4e 43 48 4c 45 4e 2c 20 30 29  ue, CONCHLEN, 0)
ee6e0 3b 0a 20 20 20 20 69 66 28 20 72 65 61 64 52 63  ;.    if( readRc
ee6f0 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53  !=SQLITE_IOERR_S
ee700 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20  HORT_READ ){.   
ee710 20 20 20 69 66 28 20 72 65 61 64 52 63 21 3d 53     if( readRc!=S
ee720 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
ee730 20 20 20 20 69 66 28 20 28 72 63 26 30 78 66 66      if( (rc&0xff
ee740 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20  )==SQLITE_IOERR 
ee750 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 69  ){.          pFi
ee760 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
ee770 63 6f 6e 63 68 46 69 6c 65 2d 3e 6c 61 73 74 45  conchFile->lastE
ee780 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a  rrno;.        }.
ee790 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 61          rc = rea
ee7a0 64 52 63 3b 0a 20 20 20 20 20 20 20 20 67 6f 74  dRc;.        got
ee7b0 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b  o end_takeconch;
ee7c0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f  .      }.      /
ee7d0 2a 20 69 66 20 74 68 65 20 63 6f 6e 63 68 20 68  * if the conch h
ee7e0 61 73 20 64 61 74 61 20 63 6f 6d 70 61 72 65 20  as data compare 
ee7f0 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 2a 2f 0a  the contents */.
ee800 20 20 20 20 20 20 69 66 28 20 21 70 43 74 78 2d        if( !pCtx-
ee810 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 29  >lockProxyPath )
ee820 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 66 6f 72  {.        /* for
ee830 20 61 75 74 6f 2d 6e 61 6d 65 64 20 6c 6f 63 61   auto-named loca
ee840 6c 20 6c 6f 63 6b 20 66 69 6c 65 2c 20 6a 75 73  l lock file, jus
ee850 74 20 63 68 65 63 6b 20 74 68 65 20 68 6f 73 74  t check the host
ee860 20 49 44 20 61 6e 64 20 77 65 27 6c 6c 0a 20 20   ID and we'll.  
ee870 20 20 20 20 20 20 20 2a 2a 20 75 73 65 20 74 68         ** use th
ee880 65 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c  e local lock fil
ee890 65 20 70 61 74 68 20 74 68 61 74 27 73 20 61 6c  e path that's al
ee8a0 72 65 61 64 79 20 69 6e 20 74 68 65 72 65 20 2a  ready in there *
ee8b0 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 6d  /.        if( !m
ee8c0 65 6d 63 6d 70 28 74 65 73 74 56 61 6c 75 65 2c  emcmp(testValue,
ee8d0 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20 48 4f 53   conchValue, HOS
ee8e0 54 49 44 4c 45 4e 29 20 29 7b 0a 20 20 20 20 20  TIDLEN) ){.     
ee8f0 20 20 20 20 20 74 4c 6f 63 6b 50 61 74 68 20 3d       tLockPath =
ee900 20 28 63 68 61 72 20 2a 29 26 63 6f 6e 63 68 56   (char *)&conchV
ee910 61 6c 75 65 5b 48 4f 53 54 49 44 4c 45 4e 5d 3b  alue[HOSTIDLEN];
ee920 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
ee930 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20  end_takeconch;. 
ee940 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
ee950 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
ee960 20 77 65 27 76 65 20 67 6f 74 20 74 68 65 20 63   we've got the c
ee970 6f 6e 63 68 20 69 66 20 63 6f 6e 63 68 56 61 6c  onch if conchVal
ee980 75 65 20 6d 61 74 63 68 65 73 20 6f 75 72 20 70  ue matches our p
ee990 61 74 68 20 61 6e 64 20 68 6f 73 74 20 49 44 20  ath and host ID 
ee9a0 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  */.        if( !
ee9b0 6d 65 6d 63 6d 70 28 74 65 73 74 56 61 6c 75 65  memcmp(testValue
ee9c0 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20 43 4f  , conchValue, CO
ee9d0 4e 43 48 4c 45 4e 29 20 29 7b 0a 20 20 20 20 20  NCHLEN) ){.     
ee9e0 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61       goto end_ta
ee9f0 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 20 20 20  keconch;.       
eea00 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
eea10 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 61  else{.      /* a
eea20 20 73 68 6f 72 74 20 72 65 61 64 20 6d 65 61 6e   short read mean
eea30 73 20 77 65 27 72 65 20 22 63 72 65 61 74 69 6e  s we're "creatin
eea40 67 22 20 74 68 65 20 63 6f 6e 63 68 20 28 65 76  g" the conch (ev
eea50 65 6e 20 74 68 6f 75 67 68 20 69 74 20 63 6f 75  en though it cou
eea60 6c 64 20 0a 20 20 20 20 20 20 2a 2a 20 68 61 76  ld .      ** hav
eea70 65 20 62 65 65 6e 20 75 73 65 72 2d 69 6e 74 65  e been user-inte
eea80 72 76 65 6e 74 69 6f 6e 29 2c 20 69 66 20 77 65  rvention), if we
eea90 20 61 63 71 75 69 72 65 20 74 68 65 20 65 78 63   acquire the exc
eeaa0 6c 75 73 69 76 65 20 6c 6f 63 6b 2c 0a 20 20 20  lusive lock,.   
eeab0 20 20 20 2a 2a 20 77 65 27 6c 6c 20 74 72 79 20     ** we'll try 
eeac0 74 6f 20 6d 61 74 63 68 20 74 68 65 20 63 75 72  to match the cur
eead0 72 65 6e 74 20 6f 6e 2d 64 69 73 6b 20 70 65 72  rent on-disk per
eeae0 6d 69 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20  missions of the 
eeaf0 64 61 74 61 62 61 73 65 0a 20 20 20 20 20 20 2a  database.      *
eeb00 2f 0a 20 20 20 20 20 20 73 79 6e 63 50 65 72 6d  /.      syncPerm
eeb10 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  s = 1;.    }.   
eeb20 20 0a 20 20 20 20 2f 2a 20 65 69 74 68 65 72 20   .    /* either 
eeb30 63 6f 6e 63 68 20 77 61 73 20 65 6d 74 70 79 20  conch was emtpy 
eeb40 6f 72 20 64 69 64 6e 27 74 20 6d 61 74 63 68 20  or didn't match 
eeb50 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 43 74 78  */.    if( !pCtx
eeb60 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20  ->lockProxyPath 
eeb70 29 7b 0a 20 20 20 20 20 20 70 72 6f 78 79 47 65  ){.      proxyGe
eeb80 74 4c 6f 63 6b 50 61 74 68 28 70 43 74 78 2d 3e  tLockPath(pCtx->
eeb90 64 62 50 61 74 68 2c 20 6c 6f 63 6b 50 61 74 68  dbPath, lockPath
eeba0 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20  , MAXPATHLEN);. 
eebb0 20 20 20 20 20 74 4c 6f 63 6b 50 61 74 68 20 3d       tLockPath =
eebc0 20 6c 6f 63 6b 50 61 74 68 3b 0a 20 20 20 20 20   lockPath;.     
eebd0 20 73 74 72 6c 63 70 79 28 26 74 65 73 74 56 61   strlcpy(&testVa
eebe0 6c 75 65 5b 48 4f 53 54 49 44 4c 45 4e 5d 2c 20  lue[HOSTIDLEN], 
eebf0 6c 6f 63 6b 50 61 74 68 2c 20 4d 41 58 50 41 54  lockPath, MAXPAT
eec00 48 4c 45 4e 29 3b 0a 20 20 20 20 7d 0a 20 20 20  HLEN);.    }.   
eec10 20 0a 20 20 20 20 2f 2a 20 75 70 64 61 74 65 20   .    /* update 
eec20 63 6f 6e 63 68 20 77 69 74 68 20 68 6f 73 74 20  conch with host 
eec30 61 6e 64 20 70 61 74 68 20 28 74 68 69 73 20 77  and path (this w
eec40 69 6c 6c 20 66 61 69 6c 20 69 66 20 6f 74 68 65  ill fail if othe
eec50 72 20 70 72 6f 63 65 73 73 0a 20 20 20 20 20 2a  r process.     *
eec60 2a 20 68 61 73 20 61 20 73 68 61 72 65 64 20 6c  * has a shared l
eec70 6f 63 6b 20 61 6c 72 65 61 64 79 29 20 2a 2f 0a  ock already) */.
eec80 20 20 20 20 72 63 20 3d 20 63 6f 6e 63 68 46 69      rc = conchFi
eec90 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f  le->pMethod->xLo
eeca0 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck((sqlite3_file
eecb0 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 45 58 43  *)conchFile, EXC
eecc0 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20  LUSIVE_LOCK);.  
eecd0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
eece0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  _OK ){.      rc 
eecf0 3d 20 75 6e 69 78 57 72 69 74 65 28 28 73 71 6c  = unixWrite((sql
eed00 69 74 65 33 5f 66 69 6c 65 20 2a 29 63 6f 6e 63  ite3_file *)conc
eed10 68 46 69 6c 65 2c 20 74 65 73 74 56 61 6c 75 65  hFile, testValue
eed20 2c 20 43 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a  , CONCHLEN, 0);.
eed30 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
eed40 4c 49 54 45 5f 4f 4b 20 26 26 20 73 79 6e 63 50  LITE_OK && syncP
eed50 65 72 6d 73 20 29 7b 0a 20 20 20 20 20 20 20 20  erms ){.        
eed60 73 74 72 75 63 74 20 73 74 61 74 20 62 75 66 3b  struct stat buf;
eed70 0a 20 20 20 20 20 20 20 20 69 6e 74 20 65 72 72  .        int err
eed80 20 3d 20 66 73 74 61 74 28 70 46 69 6c 65 2d 3e   = fstat(pFile->
eed90 68 2c 20 26 62 75 66 29 3b 0a 20 20 20 20 20 20  h, &buf);.      
eeda0 20 20 69 66 28 20 65 72 72 3d 3d 30 20 29 7b 0a    if( err==0 ){.
eedb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 72 79            /* try
eedc0 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 64 61   to match the da
eedd0 74 61 62 61 73 65 20 66 69 6c 65 20 70 65 72 6d  tabase file perm
eede0 69 73 73 69 6f 6e 73 2c 20 69 67 6e 6f 72 65 20  issions, ignore 
eedf0 66 61 69 6c 75 72 65 20 2a 2f 0a 23 69 66 6e 64  failure */.#ifnd
eee00 65 66 20 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f  ef SQLITE_PROXY_
eee10 44 45 42 55 47 0a 20 20 20 20 20 20 20 20 20 20  DEBUG.          
eee20 66 63 68 6d 6f 64 28 63 6f 6e 63 68 46 69 6c 65  fchmod(conchFile
eee30 2d 3e 68 2c 20 62 75 66 2e 73 74 5f 6d 6f 64 65  ->h, buf.st_mode
eee40 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20  );.#else.       
eee50 20 20 20 69 66 28 20 66 63 68 6d 6f 64 28 63 6f     if( fchmod(co
eee60 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 62 75 66 2e  nchFile->h, buf.
eee70 73 74 5f 6d 6f 64 65 29 21 3d 30 20 29 7b 0a 20  st_mode)!=0 ){. 
eee80 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 63             int c
eee90 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  ode = errno;.   
eeea0 20 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66           fprintf
eeeb0 28 73 74 64 65 72 72 2c 20 22 66 63 68 6d 6f 64  (stderr, "fchmod
eeec0 20 25 6f 20 46 41 49 4c 45 44 20 77 69 74 68 20   %o FAILED with 
eeed0 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20  %d %s\n",.      
eeee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eeef0 20 20 20 20 20 20 20 62 75 66 2e 73 74 5f 6d 6f         buf.st_mo
eef00 64 65 2c 20 63 6f 64 65 2c 20 73 74 72 65 72 72  de, code, strerr
eef10 6f 72 28 63 6f 64 65 29 29 3b 0a 20 20 20 20 20  or(code));.     
eef20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20       } else {.  
eef30 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e 74            fprint
eef40 66 28 73 74 64 65 72 72 2c 20 22 66 63 68 6d 6f  f(stderr, "fchmo
eef50 64 20 25 6f 20 53 55 43 43 45 44 45 44 5c 6e 22  d %o SUCCEDED\n"
eef60 2c 62 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a 20  ,buf.st_mode);. 
eef70 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
eef80 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
eef90 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 65      int code = e
eefa0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20  rrno;.          
eefb0 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20  fprintf(stderr, 
eefc0 22 53 54 41 54 20 46 41 49 4c 45 44 5b 25 64 5d  "STAT FAILED[%d]
eefd0 20 77 69 74 68 20 25 64 20 25 73 5c 6e 22 2c 20   with %d %s\n", 
eefe0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
eeff0 20 20 20 20 20 20 20 20 20 20 20 65 72 72 2c 20             err, 
ef000 63 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28 63  code, strerror(c
ef010 6f 64 65 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20  ode));.#endif.  
ef020 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
ef030 20 20 20 20 7d 0a 20 20 20 20 63 6f 6e 63 68 46      }.    conchF
ef040 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55  ile->pMethod->xU
ef050 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66  nlock((sqlite3_f
ef060 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20  ile*)conchFile, 
ef070 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20  SHARED_LOCK);.  
ef080 0a 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3a 0a  .end_takeconch:.
ef090 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 54 52      OSTRACE2("TR
ef0a0 41 4e 53 50 52 4f 58 59 3a 20 43 4c 4f 53 45 20  ANSPROXY: CLOSE 
ef0b0 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68   %d\n", pFile->h
ef0c0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
ef0d0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 46 69 6c  QLITE_OK && pFil
ef0e0 65 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 29 7b 0a  e->openFlags ){.
ef0f0 20 20 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d        if( pFile-
ef100 3e 68 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66 20  >h>=0 ){.#ifdef 
ef110 53 54 52 49 43 54 5f 43 4c 4f 53 45 5f 45 52 52  STRICT_CLOSE_ERR
ef120 4f 52 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  OR.        if( c
ef130 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 20 29  lose(pFile->h) )
ef140 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c  {.          pFil
ef150 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65  e->lastErrno = e
ef160 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20  rrno;.          
ef170 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
ef180 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20  ERR_CLOSE;.     
ef190 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 20     }.#else.     
ef1a0 20 20 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e     close(pFile->
ef1b0 68 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20  h); /* silently 
ef1c0 6c 65 61 6b 20 66 64 20 69 66 20 66 61 69 6c 20  leak fd if fail 
ef1d0 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  */.#endif.      
ef1e0 7d 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68  }.      pFile->h
ef1f0 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 69 6e 74   = -1;.      int
ef200 20 66 64 20 3d 20 6f 70 65 6e 28 70 43 74 78 2d   fd = open(pCtx-
ef210 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e  >dbPath, pFile->
ef220 6f 70 65 6e 46 6c 61 67 73 2c 0a 20 20 20 20 20  openFlags,.     
ef230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
ef240 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49  QLITE_DEFAULT_FI
ef250 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29 3b  LE_PERMISSIONS);
ef260 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28  .      OSTRACE2(
ef270 22 54 52 41 4e 53 50 52 4f 58 59 3a 20 4f 50 45  "TRANSPROXY: OPE
ef280 4e 20 20 25 64 5c 6e 22 2c 20 66 64 29 3b 0a 20  N  %d\n", fd);. 
ef290 20 20 20 20 20 69 66 28 20 66 64 3e 3d 30 20 29       if( fd>=0 )
ef2a0 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  {.        pFile-
ef2b0 3e 68 20 3d 20 66 64 3b 0a 20 20 20 20 20 20 7d  >h = fd;.      }
ef2c0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63  else{.        rc
ef2d0 3d 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e  =SQLITE_CANTOPEN
ef2e0 3b 20 2f 2a 20 53 51 4c 49 54 45 5f 42 55 53 59  ; /* SQLITE_BUSY
ef2f0 3f 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68  ? proxyTakeConch
ef300 20 63 61 6c 6c 65 64 0a 20 20 20 20 20 20 20 20   called.        
ef310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef320 20 20 20 20 20 20 20 64 75 72 69 6e 67 20 6c 6f         during lo
ef330 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 7d  cking */.      }
ef340 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
ef350 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
ef360 21 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79  !pCtx->lockProxy
ef370 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a   ){.      char *
ef380 70 61 74 68 20 3d 20 74 4c 6f 63 6b 50 61 74 68  path = tLockPath
ef390 20 3f 20 74 4c 6f 63 6b 50 61 74 68 20 3a 20 70   ? tLockPath : p
ef3a0 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61  Ctx->lockProxyPa
ef3b0 74 68 3b 0a 20 20 20 20 20 20 2f 2a 20 41 43 53  th;.      /* ACS
ef3c0 3a 20 4e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61  : Need to make a
ef3d0 20 63 6f 70 79 20 6f 66 20 70 61 74 68 20 73 6f   copy of path so
ef3e0 6d 65 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20 20  metimes */.     
ef3f0 20 72 63 20 3d 20 70 72 6f 78 79 43 72 65 61 74   rc = proxyCreat
ef400 65 55 6e 69 78 46 69 6c 65 28 70 61 74 68 2c 20  eUnixFile(path, 
ef410 26 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79  &pCtx->lockProxy
ef420 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
ef430 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
ef440 7b 0a 20 20 20 20 20 20 70 43 74 78 2d 3e 63 6f  {.      pCtx->co
ef450 6e 63 68 48 65 6c 64 20 3d 20 31 3b 0a 0a 20 20  nchHeld = 1;..  
ef460 20 20 20 20 69 66 28 20 74 4c 6f 63 6b 50 61 74      if( tLockPat
ef470 68 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 74  h ){.        pCt
ef480 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68  x->lockProxyPath
ef490 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
ef4a0 75 70 28 30 2c 20 74 4c 6f 63 6b 50 61 74 68 29  up(0, tLockPath)
ef4b0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 43  ;.        if( pC
ef4c0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70  tx->lockProxy->p
ef4d0 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66 70 49 6f  Method == &afpIo
ef4e0 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20  Methods ){.     
ef4f0 20 20 20 20 20 28 28 61 66 70 4c 6f 63 6b 69 6e       ((afpLockin
ef500 67 43 6f 6e 74 65 78 74 20 2a 29 70 43 74 78 2d  gContext *)pCtx-
ef510 3e 6c 6f 63 6b 50 72 6f 78 79 2d 3e 6c 6f 63 6b  >lockProxy->lock
ef520 69 6e 67 43 6f 6e 74 65 78 74 29 2d 3e 64 62 50  ingContext)->dbP
ef530 61 74 68 20 3d 0a 20 20 20 20 20 20 20 20 20 20  ath =.          
ef540 20 20 20 20 20 20 20 20 20 20 20 70 43 74 78 2d             pCtx-
ef550 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a  >lockProxyPath;.
ef560 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
ef570 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20  }.    } else {. 
ef580 20 20 20 20 20 63 6f 6e 63 68 46 69 6c 65 2d 3e       conchFile->
ef590 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b  pMethod->xUnlock
ef5a0 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29  ((sqlite3_file*)
ef5b0 63 6f 6e 63 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f  conchFile, NO_LO
ef5c0 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f  CK);.    }.    O
ef5d0 53 54 52 41 43 45 33 28 22 54 41 4b 45 43 4f 4e  STRACE3("TAKECON
ef5e0 43 48 20 20 25 64 20 25 73 5c 6e 22 2c 20 63 6f  CH  %d %s\n", co
ef5f0 6e 63 68 46 69 6c 65 2d 3e 68 2c 20 72 63 3d 3d  nchFile->h, rc==
ef600 53 51 4c 49 54 45 5f 4f 4b 3f 22 6f 6b 22 3a 22  SQLITE_OK?"ok":"
ef610 66 61 69 6c 65 64 22 29 3b 0a 20 20 20 20 72 65  failed");.    re
ef620 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a  turn rc;.  }.}..
ef630 2f 2a 0a 2a 2a 20 49 66 20 70 46 69 6c 65 20 68  /*.** If pFile h
ef640 6f 6c 64 73 20 61 20 6c 6f 63 6b 20 6f 6e 20 61  olds a lock on a
ef650 20 63 6f 6e 63 68 20 66 69 6c 65 2c 20 74 68 65   conch file, the
ef660 6e 20 72 65 6c 65 61 73 65 20 74 68 61 74 20 6c  n release that l
ef670 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ock..*/.static i
ef680 6e 74 20 70 72 6f 78 79 52 65 6c 65 61 73 65 43  nt proxyReleaseC
ef690 6f 6e 63 68 28 75 6e 69 78 46 69 6c 65 20 2a 70  onch(unixFile *p
ef6a0 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b  File){.  int rc;
ef6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef6c0 20 20 20 20 20 2f 2a 20 53 75 62 72 6f 75 74 69       /* Subrouti
ef6d0 6e 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a  ne return code *
ef6e0 2f 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67  /.  proxyLocking
ef6f0 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 3b 20 20  Context *pCtx;  
ef700 2f 2a 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 63  /* The locking c
ef710 6f 6e 74 65 78 74 20 66 6f 72 20 74 68 65 20 70  ontext for the p
ef720 72 6f 78 79 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75  roxy lock */.  u
ef730 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69  nixFile *conchFi
ef740 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  le;        /* Na
ef750 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 63 68 20  me of the conch 
ef760 66 69 6c 65 20 2a 2f 0a 0a 20 20 70 43 74 78 20  file */..  pCtx 
ef770 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43  = (proxyLockingC
ef780 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e  ontext *)pFile->
ef790 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a  lockingContext;.
ef7a0 20 20 63 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43    conchFile = pC
ef7b0 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a 20  tx->conchFile;. 
ef7c0 20 4f 53 54 52 41 43 45 34 28 22 52 45 4c 45 41   OSTRACE4("RELEA
ef7d0 53 45 43 4f 4e 43 48 20 20 25 64 20 66 6f 72 20  SECONCH  %d for 
ef7e0 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 63 6f  %s pid=%d\n", co
ef7f0 6e 63 68 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20  nchFile->h,.    
ef800 20 20 20 20 20 20 20 28 70 43 74 78 2d 3e 6c 6f         (pCtx->lo
ef810 63 6b 50 72 6f 78 79 50 61 74 68 20 3f 20 70 43  ckProxyPath ? pC
ef820 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74  tx->lockProxyPat
ef830 68 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 0a  h : ":auto:"), .
ef840 20 20 20 20 20 20 20 20 20 20 20 67 65 74 70 69             getpi
ef850 64 28 29 29 3b 0a 20 20 70 43 74 78 2d 3e 63 6f  d());.  pCtx->co
ef860 6e 63 68 48 65 6c 64 20 3d 20 30 3b 0a 20 20 72  nchHeld = 0;.  r
ef870 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70  c = conchFile->p
ef880 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28  Method->xUnlock(
ef890 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63  (sqlite3_file*)c
ef8a0 6f 6e 63 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43  onchFile, NO_LOC
ef8b0 4b 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22  K);.  OSTRACE3("
ef8c0 52 45 4c 45 41 53 45 43 4f 4e 43 48 20 20 25 64  RELEASECONCH  %d
ef8d0 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c   %s\n", conchFil
ef8e0 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20  e->h,.          
ef8f0 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20   (rc==SQLITE_OK 
ef900 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64  ? "ok" : "failed
ef910 22 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  "));.  return rc
ef920 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e  ;.}../*.** Given
ef930 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64   the name of a d
ef940 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 63 6f  atabase file, co
ef950 6d 70 75 74 65 20 74 68 65 20 6e 61 6d 65 20 6f  mpute the name o
ef960 66 20 69 74 73 20 63 6f 6e 63 68 20 66 69 6c 65  f its conch file
ef970 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 63  ..** Store the c
ef980 6f 6e 63 68 20 66 69 6c 65 6e 61 6d 65 20 69 6e  onch filename in
ef990 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
ef9a0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61   from sqlite3_ma
ef9b0 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 4d 61 6b 65 20  lloc()..** Make 
ef9c0 2a 70 43 6f 6e 63 68 50 61 74 68 20 70 6f 69 6e  *pConchPath poin
ef9d0 74 20 74 6f 20 74 68 65 20 6e 65 77 20 6e 61 6d  t to the new nam
ef9e0 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54  e.  Return SQLIT
ef9f0 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a  E_OK on success.
efa00 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d  ** or SQLITE_NOM
efa10 45 4d 20 69 66 20 75 6e 61 62 6c 65 20 74 6f 20  EM if unable to 
efa20 6f 62 74 61 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a  obtain memory..*
efa30 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20  *.** The caller 
efa40 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
efa50 6f 72 20 65 6e 73 75 72 69 6e 67 20 74 68 61 74  or ensuring that
efa60 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 6d   the allocated m
efa70 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 63 65 20 69  emory.** space i
efa80 73 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65  s eventually fre
efa90 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 43 6f 6e 63  ed..**.** *pConc
efaa0 68 50 61 74 68 20 69 73 20 73 65 74 20 74 6f 20  hPath is set to 
efab0 4e 55 4c 4c 20 69 66 20 61 20 6d 65 6d 6f 72 79  NULL if a memory
efac0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
efad0 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61  r occurs..*/.sta
efae0 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 72 65  tic int proxyCre
efaf0 61 74 65 43 6f 6e 63 68 50 61 74 68 6e 61 6d 65  ateConchPathname
efb00 28 63 68 61 72 20 2a 64 62 50 61 74 68 2c 20 63  (char *dbPath, c
efb10 68 61 72 20 2a 2a 70 43 6f 6e 63 68 50 61 74 68  har **pConchPath
efb20 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  ){.  int i;     
efb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
efb40 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
efb50 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 20  er */.  int len 
efb60 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 64 62  = (int)strlen(db
efb70 50 61 74 68 29 3b 20 2f 2a 20 4c 65 6e 67 74 68  Path); /* Length
efb80 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c   of database fil
efb90 65 6e 61 6d 65 20 2d 20 64 62 50 61 74 68 20 2a  ename - dbPath *
efba0 2f 0a 20 20 63 68 61 72 20 2a 63 6f 6e 63 68 50  /.  char *conchP
efbb0 61 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  ath;            
efbc0 20 20 2f 2a 20 62 75 66 66 65 72 20 69 6e 20 77    /* buffer in w
efbd0 68 69 63 68 20 74 6f 20 63 6f 6e 73 74 72 75 63  hich to construc
efbe0 74 20 63 6f 6e 63 68 20 6e 61 6d 65 20 2a 2f 0a  t conch name */.
efbf0 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73  .  /* Allocate s
efc00 70 61 63 65 20 66 6f 72 20 74 68 65 20 63 6f 6e  pace for the con
efc10 63 68 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20  ch filename and 
efc20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6e  initialize the n
efc30 61 6d 65 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20  ame to.  ** the 
efc40 6e 61 6d 65 20 6f 66 20 74 68 65 20 6f 72 69 67  name of the orig
efc50 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 69  inal database fi
efc60 6c 65 2e 20 2a 2f 20 20 0a 20 20 2a 70 43 6f 6e  le. */  .  *pCon
efc70 63 68 50 61 74 68 20 3d 20 63 6f 6e 63 68 50 61  chPath = conchPa
efc80 74 68 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c  th = (char *)sql
efc90 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6c 65 6e 20  ite3_malloc(len 
efca0 2b 20 38 29 3b 0a 20 20 69 66 28 20 63 6f 6e 63  + 8);.  if( conc
efcb0 68 50 61 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20  hPath==0 ){.    
efcc0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
efcd0 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70  MEM;.  }.  memcp
efce0 79 28 63 6f 6e 63 68 50 61 74 68 2c 20 64 62 50  y(conchPath, dbP
efcf0 61 74 68 2c 20 6c 65 6e 2b 31 29 3b 0a 20 20 0a  ath, len+1);.  .
efd00 20 20 2f 2a 20 6e 6f 77 20 69 6e 73 65 72 74 20    /* now insert 
efd10 61 20 22 2e 22 20 62 65 66 6f 72 65 20 74 68 65  a "." before the
efd20 20 6c 61 73 74 20 2f 20 63 68 61 72 61 63 74 65   last / characte
efd30 72 20 2a 2f 0a 20 20 66 6f 72 28 20 69 3d 28 6c  r */.  for( i=(l
efd40 65 6e 2d 31 29 3b 20 69 3e 3d 30 3b 20 69 2d 2d  en-1); i>=0; i--
efd50 20 29 7b 0a 20 20 20 20 69 66 28 20 63 6f 6e 63   ){.    if( conc
efd60 68 50 61 74 68 5b 69 5d 3d 3d 27 2f 27 20 29 7b  hPath[i]=='/' ){
efd70 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20  .      i++;.    
efd80 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
efd90 20 7d 0a 20 20 63 6f 6e 63 68 50 61 74 68 5b 69   }.  conchPath[i
efda0 5d 3d 27 2e 27 3b 0a 20 20 77 68 69 6c 65 20 28  ]='.';.  while (
efdb0 20 69 3c 6c 65 6e 20 29 7b 0a 20 20 20 20 63 6f   i<len ){.    co
efdc0 6e 63 68 50 61 74 68 5b 69 2b 31 5d 3d 64 62 50  nchPath[i+1]=dbP
efdd0 61 74 68 5b 69 5d 3b 0a 20 20 20 20 69 2b 2b 3b  ath[i];.    i++;
efde0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 61 70 70 65 6e  .  }..  /* appen
efdf0 64 20 74 68 65 20 22 2d 63 6f 6e 63 68 22 20 73  d the "-conch" s
efe00 75 66 66 69 78 20 74 6f 20 74 68 65 20 66 69 6c  uffix to the fil
efe10 65 20 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 26 63  e */.  memcpy(&c
efe20 6f 6e 63 68 50 61 74 68 5b 69 2b 31 5d 2c 20 22  onchPath[i+1], "
efe30 2d 63 6f 6e 63 68 22 2c 20 37 29 3b 0a 20 20 61  -conch", 7);.  a
efe40 73 73 65 72 74 28 20 28 69 6e 74 29 73 74 72 6c  ssert( (int)strl
efe50 65 6e 28 63 6f 6e 63 68 50 61 74 68 29 20 3d 3d  en(conchPath) ==
efe60 20 6c 65 6e 2b 37 20 29 3b 0a 0a 20 20 72 65 74   len+7 );..  ret
efe70 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
efe80 0a 0a 0a 2f 2a 20 54 61 6b 65 73 20 61 20 66 75  .../* Takes a fu
efe90 6c 6c 79 20 63 6f 6e 66 69 67 75 72 65 64 20 70  lly configured p
efea0 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2d 73 74 79  roxy locking-sty
efeb0 6c 65 20 75 6e 69 78 20 66 69 6c 65 20 61 6e 64  le unix file and
efec0 20 73 77 69 74 63 68 65 73 0a 2a 2a 20 74 68 65   switches.** the
efed0 20 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65   local lock file
efee0 20 70 61 74 68 20 0a 2a 2f 0a 73 74 61 74 69 63   path .*/.static
efef0 20 69 6e 74 20 73 77 69 74 63 68 4c 6f 63 6b 50   int switchLockP
eff00 72 6f 78 79 50 61 74 68 28 75 6e 69 78 46 69 6c  roxyPath(unixFil
eff10 65 20 2a 70 46 69 6c 65 2c 20 63 6f 6e 73 74 20  e *pFile, const 
eff20 63 68 61 72 20 2a 70 61 74 68 29 20 7b 0a 20 20  char *path) {.  
eff30 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
eff40 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f  ext *pCtx = (pro
eff50 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
eff60 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  *)pFile->locking
eff70 43 6f 6e 74 65 78 74 3b 0a 20 20 63 68 61 72 20  Context;.  char 
eff80 2a 6f 6c 64 50 61 74 68 20 3d 20 70 43 74 78 2d  *oldPath = pCtx-
eff90 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a  >lockProxyPath;.
effa0 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
effb0 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 46 69  E_OK;..  if( pFi
effc0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f  le->locktype!=NO
effd0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74  _LOCK ){.    ret
effe0 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  urn SQLITE_BUSY;
efff0 0a 20 20 7d 20 20 0a 0a 20 20 2f 2a 20 6e 6f 74  .  }  ..  /* not
f0000 68 69 6e 67 20 74 6f 20 64 6f 20 69 66 20 74 68  hing to do if th
f0010 65 20 70 61 74 68 20 69 73 20 4e 55 4c 4c 2c 20  e path is NULL, 
f0020 3a 61 75 74 6f 3a 20 6f 72 20 6d 61 74 63 68 65  :auto: or matche
f0030 73 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 70  s the existing p
f0040 61 74 68 20 2a 2f 0a 20 20 69 66 28 20 21 70 61  ath */.  if( !pa
f0050 74 68 20 7c 7c 20 70 61 74 68 5b 30 5d 3d 3d 27  th || path[0]=='
f0060 5c 30 27 20 7c 7c 20 21 73 74 72 63 6d 70 28 70  \0' || !strcmp(p
f0070 61 74 68 2c 20 22 3a 61 75 74 6f 3a 22 29 20 7c  ath, ":auto:") |
f0080 7c 0a 20 20 20 20 28 6f 6c 64 50 61 74 68 20 26  |.    (oldPath &
f0090 26 20 21 73 74 72 6e 63 6d 70 28 6f 6c 64 50 61  & !strncmp(oldPa
f00a0 74 68 2c 20 70 61 74 68 2c 20 4d 41 58 50 41 54  th, path, MAXPAT
f00b0 48 4c 45 4e 29 29 20 29 7b 0a 20 20 20 20 72 65  HLEN)) ){.    re
f00c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
f00d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69    }else{.    uni
f00e0 78 46 69 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78 79  xFile *lockProxy
f00f0 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f   = pCtx->lockPro
f0100 78 79 3b 0a 20 20 20 20 70 43 74 78 2d 3e 6c 6f  xy;.    pCtx->lo
f0110 63 6b 50 72 6f 78 79 3d 4e 55 4c 4c 3b 0a 20 20  ckProxy=NULL;.  
f0120 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c    pCtx->conchHel
f0130 64 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 6c  d = 0;.    if( l
f0140 6f 63 6b 50 72 6f 78 79 21 3d 4e 55 4c 4c 20 29  ockProxy!=NULL )
f0150 7b 0a 20 20 20 20 20 20 72 63 3d 6c 6f 63 6b 50  {.      rc=lockP
f0160 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78  roxy->pMethod->x
f0170 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66  Close((sqlite3_f
f0180 69 6c 65 20 2a 29 6c 6f 63 6b 50 72 6f 78 79 29  ile *)lockProxy)
f0190 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
f01a0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
f01b0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6c    sqlite3_free(l
f01c0 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 7d  ockProxy);.    }
f01d0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
f01e0 65 28 6f 6c 64 50 61 74 68 29 3b 0a 20 20 20 20  e(oldPath);.    
f01f0 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50  pCtx->lockProxyP
f0200 61 74 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ath = sqlite3DbS
f0210 74 72 44 75 70 28 30 2c 20 70 61 74 68 29 3b 0a  trDup(0, path);.
f0220 20 20 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e 20    }.  .  return 
f0230 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 46 69  rc;.}../*.** pFi
f0240 6c 65 20 69 73 20 61 20 66 69 6c 65 20 74 68 61  le is a file tha
f0250 74 20 68 61 73 20 62 65 65 6e 20 6f 70 65 6e 65  t has been opene
f0260 64 20 62 79 20 61 20 70 72 69 6f 72 20 78 4f 70  d by a prior xOp
f0270 65 6e 20 63 61 6c 6c 2e 20 20 64 62 50 61 74 68  en call.  dbPath
f0280 0a 2a 2a 20 69 73 20 61 20 73 74 72 69 6e 67 20  .** is a string 
f0290 62 75 66 66 65 72 20 61 74 20 6c 65 61 73 74 20  buffer at least 
f02a0 4d 41 58 50 41 54 48 4c 45 4e 2b 31 20 63 68 61  MAXPATHLEN+1 cha
f02b0 72 61 63 74 65 72 73 20 69 6e 20 73 69 7a 65 2e  racters in size.
f02c0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
f02d0 69 6e 65 20 66 69 6e 64 20 74 68 65 20 66 69 6c  ine find the fil
f02e0 65 6e 61 6d 65 20 61 73 73 6f 63 69 61 74 65 64  ename associated
f02f0 20 77 69 74 68 20 70 46 69 6c 65 20 61 6e 64 20   with pFile and 
f0300 77 72 69 74 65 73 20 69 74 0a 2a 2a 20 69 6e 74  writes it.** int
f0310 20 64 62 50 61 74 68 2e 0a 2a 2f 0a 73 74 61 74   dbPath..*/.stat
f0320 69 63 20 69 6e 74 20 70 72 6f 78 79 47 65 74 44  ic int proxyGetD
f0330 62 50 61 74 68 46 6f 72 55 6e 69 78 46 69 6c 65  bPathForUnixFile
f0340 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65  (unixFile *pFile
f0350 2c 20 63 68 61 72 20 2a 64 62 50 61 74 68 29 7b  , char *dbPath){
f0360 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41  .#if defined(__A
f0370 50 50 4c 45 5f 5f 29 0a 20 20 69 66 28 20 70 46  PPLE__).  if( pF
f0380 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20  ile->pMethod == 
f0390 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 29 7b  &afpIoMethods ){
f03a0 0a 20 20 20 20 2f 2a 20 61 66 70 20 73 74 79 6c  .    /* afp styl
f03b0 65 20 6b 65 65 70 73 20 61 20 72 65 66 65 72 65  e keeps a refere
f03c0 6e 63 65 20 74 6f 20 74 68 65 20 64 62 20 70 61  nce to the db pa
f03d0 74 68 20 69 6e 20 74 68 65 20 66 69 6c 65 50 61  th in the filePa
f03e0 74 68 20 66 69 65 6c 64 20 0a 20 20 20 20 2a 2a  th field .    **
f03f0 20 6f 66 20 74 68 65 20 73 74 72 75 63 74 20 2a   of the struct *
f0400 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 69  /.    assert( (i
f0410 6e 74 29 73 74 72 6c 65 6e 28 28 63 68 61 72 2a  nt)strlen((char*
f0420 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43  )pFile->lockingC
f0430 6f 6e 74 65 78 74 29 3c 3d 4d 41 58 50 41 54 48  ontext)<=MAXPATH
f0440 4c 45 4e 20 29 3b 0a 20 20 20 20 73 74 72 63 70  LEN );.    strcp
f0450 79 28 64 62 50 61 74 68 2c 20 28 28 61 66 70 4c  y(dbPath, ((afpL
f0460 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29  ockingContext *)
f0470 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
f0480 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 68 29 3b  ntext)->dbPath);
f0490 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a  .  }else.#endif.
f04a0 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65    if( pFile->pMe
f04b0 74 68 6f 64 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b  thod == &dotlock
f04c0 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20  IoMethods ){.   
f04d0 20 2f 2a 20 64 6f 74 20 6c 6f 63 6b 20 73 74 79   /* dot lock sty
f04e0 6c 65 20 75 73 65 73 20 74 68 65 20 6c 6f 63 6b  le uses the lock
f04f0 69 6e 67 20 63 6f 6e 74 65 78 74 20 74 6f 20 73  ing context to s
f0500 74 6f 72 65 20 74 68 65 20 64 6f 74 20 6c 6f 63  tore the dot loc
f0510 6b 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 70 61  k.    ** file pa
f0520 74 68 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65  th */.    int le
f0530 6e 20 3d 20 73 74 72 6c 65 6e 28 28 63 68 61 72  n = strlen((char
f0540 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e   *)pFile->lockin
f0550 67 43 6f 6e 74 65 78 74 29 20 2d 20 73 74 72 6c  gContext) - strl
f0560 65 6e 28 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49  en(DOTLOCK_SUFFI
f0570 58 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 64  X);.    memcpy(d
f0580 62 50 61 74 68 2c 20 28 63 68 61 72 20 2a 29 70  bPath, (char *)p
f0590 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
f05a0 74 65 78 74 2c 20 6c 65 6e 20 2b 20 31 29 3b 0a  text, len + 1);.
f05b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
f05c0 61 6c 6c 20 6f 74 68 65 72 20 73 74 79 6c 65 73  all other styles
f05d0 20 75 73 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67   use the locking
f05e0 20 63 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72   context to stor
f05f0 65 20 74 68 65 20 64 62 20 66 69 6c 65 20 70 61  e the db file pa
f0600 74 68 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  th */.    assert
f0610 28 20 73 74 72 6c 65 6e 28 28 63 68 61 72 2a 29  ( strlen((char*)
f0620 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
f0630 6e 74 65 78 74 29 3c 3d 4d 41 58 50 41 54 48 4c  ntext)<=MAXPATHL
f0640 45 4e 20 29 3b 0a 20 20 20 20 73 74 72 63 70 79  EN );.    strcpy
f0650 28 64 62 50 61 74 68 2c 20 28 63 68 61 72 20 2a  (dbPath, (char *
f0660 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43  )pFile->lockingC
f0670 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 20 72  ontext);.  }.  r
f0680 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
f0690 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 6b 65 73 20  .}../*.** Takes 
f06a0 61 6e 20 61 6c 72 65 61 64 79 20 66 69 6c 6c 65  an already fille
f06b0 64 20 69 6e 20 75 6e 69 78 20 66 69 6c 65 20 61  d in unix file a
f06c0 6e 64 20 61 6c 74 65 72 73 20 69 74 20 73 6f 20  nd alters it so 
f06d0 61 6c 6c 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67  all file locking
f06e0 20 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 70 65 72   .** will be per
f06f0 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65 20 6c 6f  formed on the lo
f0700 63 61 6c 20 70 72 6f 78 79 20 6c 6f 63 6b 20 66  cal proxy lock f
f0710 69 6c 65 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  ile.  The follow
f0720 69 6e 67 20 66 69 65 6c 64 73 0a 2a 2a 20 61 72  ing fields.** ar
f0730 65 20 70 72 65 73 65 72 76 65 64 20 69 6e 20 74  e preserved in t
f0740 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65  he locking conte
f0750 78 74 20 73 6f 20 74 68 61 74 20 74 68 65 79 20  xt so that they 
f0760 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20  can be restored 
f0770 61 6e 64 20 0a 2a 2a 20 74 68 65 20 75 6e 69 78  and .** the unix
f0780 20 73 74 72 75 63 74 75 72 65 20 70 72 6f 70 65   structure prope
f0790 72 6c 79 20 63 6c 65 61 6e 65 64 20 75 70 20 61  rly cleaned up a
f07a0 74 20 63 6c 6f 73 65 20 74 69 6d 65 3a 0a 2a 2a  t close time:.**
f07b0 20 20 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65    ->lockingConte
f07c0 78 74 0a 2a 2a 20 20 2d 3e 70 4d 65 74 68 6f 64  xt.**  ->pMethod
f07d0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
f07e0 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69  roxyTransformUni
f07f0 78 46 69 6c 65 28 75 6e 69 78 46 69 6c 65 20 2a  xFile(unixFile *
f0800 70 46 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61  pFile, const cha
f0810 72 20 2a 70 61 74 68 29 20 7b 0a 20 20 70 72 6f  r *path) {.  pro
f0820 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  xyLockingContext
f0830 20 2a 70 43 74 78 3b 0a 20 20 63 68 61 72 20 64   *pCtx;.  char d
f0840 62 50 61 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e  bPath[MAXPATHLEN
f0850 2b 31 5d 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61  +1];       /* Na
f0860 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
f0870 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61  se file */.  cha
f0880 72 20 2a 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c  r *lockPath=NULL
f0890 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
f08a0 49 54 45 5f 4f 4b 3b 0a 20 20 0a 20 20 69 66 28  ITE_OK;.  .  if(
f08b0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
f08c0 21 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  !=NO_LOCK ){.   
f08d0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
f08e0 55 53 59 3b 0a 20 20 7d 0a 20 20 70 72 6f 78 79  USY;.  }.  proxy
f08f0 47 65 74 44 62 50 61 74 68 46 6f 72 55 6e 69 78  GetDbPathForUnix
f0900 46 69 6c 65 28 70 46 69 6c 65 2c 20 64 62 50 61  File(pFile, dbPa
f0910 74 68 29 3b 0a 20 20 69 66 28 20 21 70 61 74 68  th);.  if( !path
f0920 20 7c 7c 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30   || path[0]=='\0
f0930 27 20 7c 7c 20 21 73 74 72 63 6d 70 28 70 61 74  ' || !strcmp(pat
f0940 68 2c 20 22 3a 61 75 74 6f 3a 22 29 20 29 7b 0a  h, ":auto:") ){.
f0950 20 20 20 20 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c      lockPath=NUL
f0960 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  L;.  }else{.    
f0970 6c 6f 63 6b 50 61 74 68 3d 28 63 68 61 72 20 2a  lockPath=(char *
f0980 29 70 61 74 68 3b 0a 20 20 7d 0a 20 20 0a 20 20  )path;.  }.  .  
f0990 4f 53 54 52 41 43 45 34 28 22 54 52 41 4e 53 50  OSTRACE4("TRANSP
f09a0 52 4f 58 59 20 20 25 64 20 66 6f 72 20 25 73 20  ROXY  %d for %s 
f09b0 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  pid=%d\n", pFile
f09c0 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ->h,.           
f09d0 28 6c 6f 63 6b 50 61 74 68 20 3f 20 6c 6f 63 6b  (lockPath ? lock
f09e0 50 61 74 68 20 3a 20 22 3a 61 75 74 6f 3a 22 29  Path : ":auto:")
f09f0 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20  , getpid());..  
f0a00 70 43 74 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d  pCtx = sqlite3_m
f0a10 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70  alloc( sizeof(*p
f0a20 43 74 78 29 20 29 3b 0a 20 20 69 66 28 20 70 43  Ctx) );.  if( pC
f0a30 74 78 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  tx==0 ){.    ret
f0a40 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
f0a50 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70  ;.  }.  memset(p
f0a60 43 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a  Ctx, 0, sizeof(*
f0a70 70 43 74 78 29 29 3b 0a 0a 20 20 72 63 20 3d 20  pCtx));..  rc = 
f0a80 70 72 6f 78 79 43 72 65 61 74 65 43 6f 6e 63 68  proxyCreateConch
f0a90 50 61 74 68 6e 61 6d 65 28 64 62 50 61 74 68 2c  Pathname(dbPath,
f0aa0 20 26 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c   &pCtx->conchFil
f0ab0 65 50 61 74 68 29 3b 0a 20 20 69 66 28 20 72 63  ePath);.  if( rc
f0ac0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
f0ad0 20 20 20 72 63 20 3d 20 70 72 6f 78 79 43 72 65     rc = proxyCre
f0ae0 61 74 65 55 6e 69 78 46 69 6c 65 28 70 43 74 78  ateUnixFile(pCtx
f0af0 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 2c  ->conchFilePath,
f0b00 20 26 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c   &pCtx->conchFil
f0b10 65 29 3b 0a 20 20 7d 20 20 0a 20 20 69 66 28 20  e);.  }  .  if( 
f0b20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
f0b30 20 6c 6f 63 6b 50 61 74 68 20 29 7b 0a 20 20 20   lockPath ){.   
f0b40 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79   pCtx->lockProxy
f0b50 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 44 62  Path = sqlite3Db
f0b60 53 74 72 44 75 70 28 30 2c 20 6c 6f 63 6b 50 61  StrDup(0, lockPa
f0b70 74 68 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  th);.  }..  if( 
f0b80 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
f0b90 0a 20 20 20 20 2f 2a 20 61 6c 6c 20 6d 65 6d 6f  .    /* all memo
f0ba0 72 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c  ry is allocated,
f0bb0 20 70 72 6f 78 79 73 20 61 72 65 20 63 72 65 61   proxys are crea
f0bc0 74 65 64 20 61 6e 64 20 61 73 73 69 67 6e 65 64  ted and assigned
f0bd0 2c 20 0a 20 20 20 20 2a 2a 20 73 77 69 74 63 68  , .    ** switch
f0be0 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e   the locking con
f0bf0 74 65 78 74 20 61 6e 64 20 70 4d 65 74 68 6f 64  text and pMethod
f0c00 20 74 68 65 6e 20 72 65 74 75 72 6e 2e 0a 20 20   then return..  
f0c10 20 20 2a 2f 0a 20 20 20 20 70 43 74 78 2d 3e 64    */.    pCtx->d
f0c20 62 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 44  bPath = sqlite3D
f0c30 62 53 74 72 44 75 70 28 30 2c 20 64 62 50 61 74  bStrDup(0, dbPat
f0c40 68 29 3b 0a 20 20 20 20 70 43 74 78 2d 3e 6f 6c  h);.    pCtx->ol
f0c50 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  dLockingContext 
f0c60 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  = pFile->locking
f0c70 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 70 46 69  Context;.    pFi
f0c80 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
f0c90 78 74 20 3d 20 70 43 74 78 3b 0a 20 20 20 20 70  xt = pCtx;.    p
f0ca0 43 74 78 2d 3e 70 4f 6c 64 4d 65 74 68 6f 64 20  Ctx->pOldMethod 
f0cb0 3d 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64  = pFile->pMethod
f0cc0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 4d 65  ;.    pFile->pMe
f0cd0 74 68 6f 64 20 3d 20 26 70 72 6f 78 79 49 6f 4d  thod = &proxyIoM
f0ce0 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 7b  ethods;.  }else{
f0cf0 0a 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 63  .    if( pCtx->c
f0d00 6f 6e 63 68 46 69 6c 65 20 29 7b 20 0a 20 20 20  onchFile ){ .   
f0d10 20 20 20 72 63 20 3d 20 70 43 74 78 2d 3e 63 6f     rc = pCtx->co
f0d20 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64  nchFile->pMethod
f0d30 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65  ->xClose((sqlite
f0d40 33 5f 66 69 6c 65 20 2a 29 70 43 74 78 2d 3e 63  3_file *)pCtx->c
f0d50 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 20  onchFile);.     
f0d60 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
f0d70 20 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   rc;.      sqlit
f0d80 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 63 6f  e3_free(pCtx->co
f0d90 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a  nchFile);.    }.
f0da0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
f0db0 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65  (pCtx->conchFile
f0dc0 50 61 74 68 29 3b 20 0a 20 20 20 20 73 71 6c 69  Path); .    sqli
f0dd0 74 65 33 5f 66 72 65 65 28 70 43 74 78 29 3b 0a  te3_free(pCtx);.
f0de0 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 33 28 22    }.  OSTRACE3("
f0df0 54 52 41 4e 53 50 52 4f 58 59 20 20 25 64 20 25  TRANSPROXY  %d %
f0e00 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a  s\n", pFile->h,.
f0e10 20 20 20 20 20 20 20 20 20 20 20 28 72 63 3d 3d             (rc==
f0e20 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22  SQLITE_OK ? "ok"
f0e30 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b 0a 20   : "failed"));. 
f0e40 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a   return rc;.}...
f0e50 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
f0e60 6e 65 20 68 61 6e 64 6c 65 73 20 73 71 6c 69 74  ne handles sqlit
f0e70 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28  e3_file_control(
f0e80 29 20 63 61 6c 6c 73 20 74 68 61 74 20 61 72 65  ) calls that are
f0e90 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 74 6f 20   specific.** to 
f0ea0 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 0a 2a  proxy locking..*
f0eb0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  /.static int pro
f0ec0 78 79 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71  xyFileControl(sq
f0ed0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
f0ee0 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41  int op, void *pA
f0ef0 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28 20 6f  rg){.  switch( o
f0f00 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  p ){.    case SQ
f0f10 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f  LITE_GET_LOCKPRO
f0f20 58 59 46 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20  XYFILE: {.      
f0f30 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
f0f40 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
f0f50 0a 20 20 20 20 20 20 69 66 28 20 70 46 69 6c 65  .      if( pFile
f0f60 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 70 72  ->pMethod == &pr
f0f70 6f 78 79 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a  oxyIoMethods ){.
f0f80 20 20 20 20 20 20 20 20 70 72 6f 78 79 4c 6f 63          proxyLoc
f0f90 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74  kingContext *pCt
f0fa0 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e  x = (proxyLockin
f0fb0 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65 2d  gContext*)pFile-
f0fc0 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b  >lockingContext;
f0fd0 0a 20 20 20 20 20 20 20 20 70 72 6f 78 79 54 61  .        proxyTa
f0fe0 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b 0a  keConch(pFile);.
f0ff0 20 20 20 20 20 20 20 20 69 66 28 20 70 43 74 78          if( pCtx
f1000 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20  ->lockProxyPath 
f1010 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 63  ){.          *(c
f1020 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72  onst char **)pAr
f1030 67 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  g = pCtx->lockPr
f1040 6f 78 79 50 61 74 68 3b 0a 20 20 20 20 20 20 20  oxyPath;.       
f1050 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
f1060 20 20 2a 28 63 6f 6e 73 74 20 63 68 61 72 20 2a    *(const char *
f1070 2a 29 70 41 72 67 20 3d 20 22 3a 61 75 74 6f 3a  *)pArg = ":auto:
f1080 20 28 6e 6f 74 20 68 65 6c 64 29 22 3b 0a 20 20   (not held)";.  
f1090 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20        }.      } 
f10a0 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 2a  else {.        *
f10b0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70  (const char **)p
f10c0 41 72 67 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  Arg = NULL;.    
f10d0 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e    }.      return
f10e0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
f10f0 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
f1100 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46  E_SET_LOCKPROXYF
f1110 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69  ILE: {.      uni
f1120 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
f1130 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20  unixFile*)id;.  
f1140 20 20 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c      int rc = SQL
f1150 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 69 6e  ITE_OK;.      in
f1160 74 20 69 73 50 72 6f 78 79 53 74 79 6c 65 20 3d  t isProxyStyle =
f1170 20 28 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64   (pFile->pMethod
f1180 20 3d 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68   == &proxyIoMeth
f1190 6f 64 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ods);.      if( 
f11a0 70 41 72 67 3d 3d 4e 55 4c 4c 20 7c 7c 20 28 63  pArg==NULL || (c
f11b0 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 41 72 67  onst char *)pArg
f11c0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ==0 ){.        i
f11d0 66 28 20 69 73 50 72 6f 78 79 53 74 79 6c 65 20  f( isProxyStyle 
f11e0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
f11f0 74 75 72 6e 20 6f 66 66 20 70 72 6f 78 79 20 6c  turn off proxy l
f1200 6f 63 6b 69 6e 67 20 2d 20 6e 6f 74 20 73 75 70  ocking - not sup
f1210 70 6f 72 74 65 64 20 2a 2f 0a 20 20 20 20 20 20  ported */.      
f1220 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
f1230 45 52 52 4f 52 20 2f 2a 53 51 4c 49 54 45 5f 50  ERROR /*SQLITE_P
f1240 52 4f 54 4f 43 4f 4c 3f 20 53 51 4c 49 54 45 5f  ROTOCOL? SQLITE_
f1250 4d 49 53 55 53 45 3f 2a 2f 3b 0a 20 20 20 20 20  MISUSE?*/;.     
f1260 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
f1270 20 20 20 20 2f 2a 20 74 75 72 6e 20 6f 66 66 20      /* turn off 
f1280 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 2d 20  proxy locking - 
f1290 61 6c 72 65 61 64 79 20 6f 66 66 20 2d 20 4e 4f  already off - NO
f12a0 4f 50 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  OP */.          
f12b0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
f12c0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
f12d0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63  }else{.        c
f12e0 6f 6e 73 74 20 63 68 61 72 20 2a 70 72 6f 78 79  onst char *proxy
f12f0 50 61 74 68 20 3d 20 28 63 6f 6e 73 74 20 63 68  Path = (const ch
f1300 61 72 20 2a 29 70 41 72 67 3b 0a 20 20 20 20 20  ar *)pArg;.     
f1310 20 20 20 69 66 28 20 69 73 50 72 6f 78 79 53 74     if( isProxySt
f1320 79 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  yle ){.         
f1330 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e   proxyLockingCon
f1340 74 65 78 74 20 2a 70 43 74 78 20 3d 20 0a 20 20  text *pCtx = .  
f1350 20 20 20 20 20 20 20 20 20 20 28 70 72 6f 78 79            (proxy
f1360 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29  LockingContext*)
f1370 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
f1380 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20  ntext;.         
f1390 20 69 66 28 20 21 73 74 72 63 6d 70 28 70 41 72   if( !strcmp(pAr
f13a0 67 2c 20 22 3a 61 75 74 6f 3a 22 29 20 0a 20 20  g, ":auto:") .  
f13b0 20 20 20 20 20 20 20 20 20 7c 7c 20 28 70 43 74           || (pCt
f13c0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68  x->lockProxyPath
f13d0 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20   &&.            
f13e0 20 20 20 21 73 74 72 6e 63 6d 70 28 70 43 74 78     !strncmp(pCtx
f13f0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 2c  ->lockProxyPath,
f1400 20 70 72 6f 78 79 50 61 74 68 2c 20 4d 41 58 50   proxyPath, MAXP
f1410 41 54 48 4c 45 4e 29 29 0a 20 20 20 20 20 20 20  ATHLEN)).       
f1420 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20     ){.          
f1430 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
f1440 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
f1450 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  e{.            r
f1460 63 20 3d 20 73 77 69 74 63 68 4c 6f 63 6b 50 72  c = switchLockPr
f1470 6f 78 79 50 61 74 68 28 70 46 69 6c 65 2c 20 70  oxyPath(pFile, p
f1480 72 6f 78 79 50 61 74 68 29 3b 0a 20 20 20 20 20  roxyPath);.     
f1490 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
f14a0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
f14b0 2f 2a 20 74 75 72 6e 20 6f 6e 20 70 72 6f 78 79  /* turn on proxy
f14c0 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 2a 2f   file locking */
f14d0 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
f14e0 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e  proxyTransformUn
f14f0 69 78 46 69 6c 65 28 70 46 69 6c 65 2c 20 70 72  ixFile(pFile, pr
f1500 6f 78 79 50 61 74 68 29 3b 0a 20 20 20 20 20 20  oxyPath);.      
f1510 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
f1520 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
f1530 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20   }.    default: 
f1540 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
f1550 30 20 29 3b 20 20 2f 2a 20 54 68 65 20 63 61 6c  0 );  /* The cal
f1560 6c 20 61 73 73 75 72 65 73 20 74 68 61 74 20 6f  l assures that o
f1570 6e 6c 79 20 76 61 6c 69 64 20 6f 70 63 6f 64 65  nly valid opcode
f1580 73 20 61 72 65 20 73 65 6e 74 20 2a 2f 0a 20 20  s are sent */.  
f1590 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52    }.  }.  /*NOTR
f15a0 45 41 43 48 45 44 2a 2f 0a 20 20 72 65 74 75 72  EACHED*/.  retur
f15b0 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
f15c0 7d 0a 0a 2f 2a 0a 2a 2a 20 57 69 74 68 69 6e 20  }../*.** Within 
f15d0 74 68 69 73 20 64 69 76 69 73 69 6f 6e 20 28 74  this division (t
f15e0 68 65 20 70 72 6f 78 79 69 6e 67 20 6c 6f 63 6b  he proxying lock
f15f0 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ing implementati
f1600 6f 6e 29 20 74 68 65 20 70 72 6f 63 65 64 75 72  on) the procedur
f1610 65 73 0a 2a 2a 20 61 62 6f 76 65 20 74 68 69 73  es.** above this
f1620 20 70 6f 69 6e 74 20 61 72 65 20 61 6c 6c 20 75   point are all u
f1630 74 69 6c 69 74 69 65 73 2e 20 20 54 68 65 20 6c  tilities.  The l
f1640 6f 63 6b 2d 72 65 6c 61 74 65 64 20 6d 65 74 68  ock-related meth
f1650 6f 64 73 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72  ods of the.** pr
f1660 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 73 71 6c 69  oxy-locking sqli
f1670 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 20 6f 62  te3_io_method ob
f1680 6a 65 63 74 20 66 6f 6c 6c 6f 77 2e 0a 2a 2f 0a  ject follow..*/.
f1690 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
f16a0 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74  tine checks if t
f16b0 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56  here is a RESERV
f16c0 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20  ED lock held on 
f16d0 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  the specified.**
f16e0 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72   file by this or
f16f0 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65   any other proce
f1700 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f  ss. If such a lo
f1710 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20  ck is held, set 
f1720 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61  *pResOut.** to a
f1730 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20   non-zero value 
f1740 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f  otherwise *pResO
f1750 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72  ut is set to zer
f1760 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76  o.  The return v
f1770 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74  alue.** is set t
f1780 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65  o SQLITE_OK unle
f1790 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20  ss an I/O error 
f17a0 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f  occurs during lo
f17b0 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a  ck checking..*/.
f17c0 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
f17d0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
f17e0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
f17f0 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  id, int *pResOut
f1800 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  ) {.  unixFile *
f1810 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
f1820 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 20  e*)id;.  int rc 
f1830 3d 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68  = proxyTakeConch
f1840 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 72  (pFile);.  if( r
f1850 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
f1860 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67      proxyLocking
f1870 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20  Context *pCtx = 
f1880 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e  (proxyLockingCon
f1890 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f  text *)pFile->lo
f18a0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20  ckingContext;.  
f18b0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78    unixFile *prox
f18c0 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  y = pCtx->lockPr
f18d0 6f 78 79 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  oxy;.    return 
f18e0 70 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e  proxy->pMethod->
f18f0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
f1900 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck((sqlite3_file
f1910 2a 29 70 72 6f 78 79 2c 20 70 52 65 73 4f 75 74  *)proxy, pResOut
f1920 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
f1930 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63  rc;.}../*.** Loc
f1940 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20  k the file with 
f1950 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69  the lock specifi
f1960 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20  ed by parameter 
f1970 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a  locktype - one.*
f1980 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * of the followi
f1990 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31  ng:.**.**     (1
f19a0 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a  ) SHARED_LOCK.**
f19b0 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45       (2) RESERVE
f19c0 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33  D_LOCK.**     (3
f19d0 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a  ) PENDING_LOCK.*
f19e0 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53  *     (4) EXCLUS
f19f0 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53  IVE_LOCK.**.** S
f1a00 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65  ometimes when re
f1a10 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63  questing one loc
f1a20 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f  k state, additio
f1a30 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a  nal lock states.
f1a40 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20  ** are inserted 
f1a50 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65  in between.  The
f1a60 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66   locking might f
f1a70 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68  ail on one of th
f1a80 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73  e later.** trans
f1a90 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74  itions leaving t
f1aa0 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69  he lock state di
f1ab0 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61  fferent from wha
f1ac0 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74  t it started but
f1ad0 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20  .** still short 
f1ae0 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68  of its goal.  Th
f1af0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72  e following char
f1b00 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f  t shows the allo
f1b10 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  wed.** transitio
f1b20 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72  ns and the inser
f1b30 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65  ted intermediate
f1b40 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20   states:.**.**  
f1b50 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48    UNLOCKED -> SH
f1b60 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45  ARED.**    SHARE
f1b70 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a  D -> RESERVED.**
f1b80 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50      SHARED -> (P
f1b90 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55  ENDING) -> EXCLU
f1ba0 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52  SIVE.**    RESER
f1bb0 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29  VED -> (PENDING)
f1bc0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
f1bd0 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45      PENDING -> E
f1be0 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54  XCLUSIVE.**.** T
f1bf0 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
f1c00 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61   only increase a
f1c10 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20   lock.  Use the 
f1c20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28  sqlite3OsUnlock(
f1c30 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20  ).** routine to 
f1c40 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20  lower a locking 
f1c50 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63  level..*/.static
f1c60 20 69 6e 74 20 70 72 6f 78 79 4c 6f 63 6b 28 73   int proxyLock(s
f1c70 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
f1c80 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b   int locktype) {
f1c90 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
f1ca0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
f1cb0 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70  id;.  int rc = p
f1cc0 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46  roxyTakeConch(pF
f1cd0 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  ile);.  if( rc==
f1ce0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
f1cf0 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e   proxyLockingCon
f1d00 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72  text *pCtx = (pr
f1d10 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
f1d20 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  t *)pFile->locki
f1d30 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75  ngContext;.    u
f1d40 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d  nixFile *proxy =
f1d50 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79   pCtx->lockProxy
f1d60 3b 0a 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79  ;.    rc = proxy
f1d70 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b  ->pMethod->xLock
f1d80 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29  ((sqlite3_file*)
f1d90 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79 70 65 29  proxy, locktype)
f1da0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  ;.    pFile->loc
f1db0 6b 74 79 70 65 20 3d 20 70 72 6f 78 79 2d 3e 6c  ktype = proxy->l
f1dc0 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 72  ocktype;.  }.  r
f1dd0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
f1de0 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f  .** Lower the lo
f1df0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66  cking level on f
f1e00 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70  ile descriptor p
f1e10 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65  File to locktype
f1e20 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d  .  locktype.** m
f1e30 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f  ust be either NO
f1e40 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f  _LOCK or SHARED_
f1e50 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  LOCK..**.** If t
f1e60 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
f1e70 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73   of the file des
f1e80 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61  criptor is alrea
f1e90 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a  dy at or below.*
f1ea0 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  * the requested 
f1eb0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74  locking level, t
f1ec0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
f1ed0 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69   no-op..*/.stati
f1ee0 63 20 69 6e 74 20 70 72 6f 78 79 55 6e 6c 6f 63  c int proxyUnloc
f1ef0 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
f1f00 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
f1f10 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a  ) {.  unixFile *
f1f20 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
f1f30 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 20  e*)id;.  int rc 
f1f40 3d 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68  = proxyTakeConch
f1f50 28 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 72  (pFile);.  if( r
f1f60 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
f1f70 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67      proxyLocking
f1f80 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20  Context *pCtx = 
f1f90 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e  (proxyLockingCon
f1fa0 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f  text *)pFile->lo
f1fb0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20  ckingContext;.  
f1fc0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78    unixFile *prox
f1fd0 79 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  y = pCtx->lockPr
f1fe0 6f 78 79 3b 0a 20 20 20 20 72 63 20 3d 20 70 72  oxy;.    rc = pr
f1ff0 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55  oxy->pMethod->xU
f2000 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66  nlock((sqlite3_f
f2010 69 6c 65 2a 29 70 72 6f 78 79 2c 20 6c 6f 63 6b  ile*)proxy, lock
f2020 74 79 70 65 29 3b 0a 20 20 20 20 70 46 69 6c 65  type);.    pFile
f2030 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 70 72 6f  ->locktype = pro
f2040 78 79 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  xy->locktype;.  
f2050 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
f2060 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20  ../*.** Close a 
f2070 66 69 6c 65 20 74 68 61 74 20 75 73 65 73 20 70  file that uses p
f2080 72 6f 78 79 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73  roxy locks..*/.s
f2090 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43  tatic int proxyC
f20a0 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  lose(sqlite3_fil
f20b0 65 20 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 69  e *id) {.  if( i
f20c0 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c  d ){.    unixFil
f20d0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
f20e0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 70 72  File*)id;.    pr
f20f0 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
f2100 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79  t *pCtx = (proxy
f2110 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a  LockingContext *
f2120 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43  )pFile->lockingC
f2130 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78  ontext;.    unix
f2140 46 69 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 20  File *lockProxy 
f2150 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78  = pCtx->lockProx
f2160 79 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20  y;.    unixFile 
f2170 2a 63 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43 74  *conchFile = pCt
f2180 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20  x->conchFile;.  
f2190 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
f21a0 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 20 20 20 69  E_OK;.    .    i
f21b0 66 28 20 6c 6f 63 6b 50 72 6f 78 79 20 29 7b 0a  f( lockProxy ){.
f21c0 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b 50        rc = lockP
f21d0 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78  roxy->pMethod->x
f21e0 55 6e 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f  Unlock((sqlite3_
f21f0 66 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 2c  file*)lockProxy,
f2200 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20   NO_LOCK);.     
f2210 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
f2220 20 72 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20   rc;.      rc = 
f2230 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68  lockProxy->pMeth
f2240 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69  od->xClose((sqli
f2250 74 65 33 5f 66 69 6c 65 2a 29 6c 6f 63 6b 50 72  te3_file*)lockPr
f2260 6f 78 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20  oxy);.      if( 
f2270 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
f2280 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
f2290 65 65 28 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20  ee(lockProxy);. 
f22a0 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50       pCtx->lockP
f22b0 72 6f 78 79 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  roxy = 0;.    }.
f22c0 20 20 20 20 69 66 28 20 63 6f 6e 63 68 46 69 6c      if( conchFil
f22d0 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  e ){.      if( p
f22e0 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 29  Ctx->conchHeld )
f22f0 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70  {.        rc = p
f2300 72 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e 63 68  roxyReleaseConch
f2310 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 20  (pFile);.       
f2320 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
f2330 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   rc;.      }.   
f2340 20 20 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c     rc = conchFil
f2350 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f  e->pMethod->xClo
f2360 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  se((sqlite3_file
f2370 2a 29 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20  *)conchFile);.  
f2380 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
f2390 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71  urn rc;.      sq
f23a0 6c 69 74 65 33 5f 66 72 65 65 28 63 6f 6e 63 68  lite3_free(conch
f23b0 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  File);.    }.   
f23c0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
f23d0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74  tx->lockProxyPat
f23e0 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  h);.    sqlite3_
f23f0 66 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68  free(pCtx->conch
f2400 46 69 6c 65 50 61 74 68 29 3b 0a 20 20 20 20 73  FilePath);.    s
f2410 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78  qlite3_free(pCtx
f2420 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20 20 20 2f  ->dbPath);.    /
f2430 2a 20 72 65 73 74 6f 72 65 20 74 68 65 20 6f 72  * restore the or
f2440 69 67 69 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 63  iginal locking c
f2450 6f 6e 74 65 78 74 20 61 6e 64 20 70 4d 65 74 68  ontext and pMeth
f2460 6f 64 20 74 68 65 6e 20 63 6c 6f 73 65 20 69 74  od then close it
f2470 20 2a 2f 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c   */.    pFile->l
f2480 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20  ockingContext = 
f2490 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e 67  pCtx->oldLocking
f24a0 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 70 46 69  Context;.    pFi
f24b0 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70 43  le->pMethod = pC
f24c0 74 78 2d 3e 70 4f 6c 64 4d 65 74 68 6f 64 3b 0a  tx->pOldMethod;.
f24d0 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
f24e0 28 70 43 74 78 29 3b 0a 20 20 20 20 72 65 74 75  (pCtx);.    retu
f24f0 72 6e 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f  rn pFile->pMetho
f2500 64 2d 3e 78 43 6c 6f 73 65 28 69 64 29 3b 0a 20  d->xClose(id);. 
f2510 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
f2520 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 23 65 6e 64  TE_OK;.}....#end
f2530 69 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f  if /* defined(__
f2540 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49  APPLE__) && SQLI
f2550 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
f2560 47 5f 53 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a  G_STYLE */./*.**
f2570 20 54 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69   The proxy locki
f2580 6e 67 20 73 74 79 6c 65 20 69 73 20 69 6e 74 65  ng style is inte
f2590 6e 64 65 64 20 66 6f 72 20 75 73 65 20 77 69 74  nded for use wit
f25a0 68 20 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d  h AFP filesystem
f25b0 73 2e 0a 2a 2a 20 41 6e 64 20 73 69 6e 63 65 20  s..** And since 
f25c0 41 46 50 20 69 73 20 6f 6e 6c 79 20 73 75 70 70  AFP is only supp
f25d0 6f 72 74 65 64 20 6f 6e 20 4d 61 63 4f 53 58 2c  orted on MacOSX,
f25e0 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69   the proxy locki
f25f0 6e 67 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 72 65  ng is also.** re
f2600 73 74 72 69 63 74 65 64 20 74 6f 20 4d 61 63 4f  stricted to MacO
f2610 53 58 2e 0a 2a 2a 20 0a 2a 2a 0a 2a 2a 2a 2a 2a  SX..** .**.*****
f2620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
f2630 6e 64 20 6f 66 20 74 68 65 20 70 72 6f 78 79 20  nd of the proxy 
f2640 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74  lock implementat
f2650 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ion ************
f2660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a  **********.*****
f2670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f26a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f26b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a  *********/../*.*
f26c0 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
f26d0 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
f26e0 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a  m interface..**.
f26f0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
f2700 72 65 67 69 73 74 65 72 73 20 61 6c 6c 20 56 46  registers all VF
f2710 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  S implementation
f2720 73 20 66 6f 72 20 75 6e 69 78 2d 6c 69 6b 65 20  s for unix-like 
f2730 6f 70 65 72 61 74 69 6e 67 0a 2a 2a 20 73 79 73  operating.** sys
f2740 74 65 6d 73 2e 20 20 54 68 69 73 20 72 6f 75 74  tems.  This rout
f2750 69 6e 65 2c 20 61 6e 64 20 74 68 65 20 73 71 6c  ine, and the sql
f2760 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 20 72 6f  ite3_os_end() ro
f2770 75 74 69 6e 65 20 74 68 61 74 20 66 6f 6c 6c 6f  utine that follo
f2780 77 73 2c 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65  ws,.** should be
f2790 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e   the only routin
f27a0 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  es in this file 
f27b0 74 68 61 74 20 61 72 65 20 76 69 73 69 62 6c 65  that are visible
f27c0 20 66 72 6f 6d 20 6f 74 68 65 72 0a 2a 2a 20 66   from other.** f
f27d0 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  iles..**.** This
f27e0 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
f27f0 65 64 20 6f 6e 63 65 20 64 75 72 69 6e 67 20 53  ed once during S
f2800 51 4c 69 74 65 20 69 6e 69 74 69 61 6c 69 7a 61  QLite initializa
f2810 74 69 6f 6e 20 61 6e 64 20 62 79 20 61 0a 2a 2a  tion and by a.**
f2820 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 2e 20   single thread. 
f2830 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   The memory allo
f2840 63 61 74 69 6f 6e 20 61 6e 64 20 6d 75 74 65 78  cation and mutex
f2850 20 73 75 62 73 79 73 74 65 6d 73 20 68 61 76 65   subsystems have
f2860 20 6e 6f 74 0a 2a 2a 20 6e 65 63 65 73 73 61 72   not.** necessar
f2870 69 6c 79 20 62 65 65 6e 20 69 6e 69 74 69 61 6c  ily been initial
f2880 69 7a 65 64 20 77 68 65 6e 20 74 68 69 73 20 72  ized when this r
f2890 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
f28a0 2c 20 61 6e 64 20 73 6f 20 74 68 65 79 0a 2a 2a  , and so they.**
f28b0 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75   should not be u
f28c0 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  sed..*/.SQLITE_A
f28d0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
f28e0 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 20 0a 20  s_init(void){ . 
f28f0 20 2f 2a 20 0a 20 20 2a 2a 20 54 68 65 20 66 6f   /* .  ** The fo
f2900 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 64 65  llowing macro de
f2910 66 69 6e 65 73 20 61 6e 20 69 6e 69 74 69 61 6c  fines an initial
f2920 69 7a 65 72 20 66 6f 72 20 61 6e 20 73 71 6c 69  izer for an sqli
f2930 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e 0a  te3_vfs object..
f2940 20 20 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66    ** The name of
f2950 20 74 68 65 20 56 46 53 20 69 73 20 4e 41 4d 45   the VFS is NAME
f2960 2e 20 20 54 68 65 20 70 41 70 70 44 61 74 61 20  .  The pAppData 
f2970 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
f2980 61 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 74  a pointer.  ** t
f2990 6f 20 74 68 65 20 22 66 69 6e 64 65 72 22 20 66  o the "finder" f
f29a0 75 6e 63 74 69 6f 6e 2e 20 20 28 70 41 70 70 44  unction.  (pAppD
f29b0 61 74 61 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ata is a pointer
f29c0 20 74 6f 20 61 20 70 6f 69 6e 74 65 72 20 62 65   to a pointer be
f29d0 63 61 75 73 65 0a 20 20 2a 2a 20 73 69 6c 6c 79  cause.  ** silly
f29e0 20 43 39 30 20 72 75 6c 65 73 20 70 72 6f 68 69   C90 rules prohi
f29f0 62 69 74 20 61 20 76 6f 69 64 2a 20 66 72 6f 6d  bit a void* from
f2a00 20 62 65 69 6e 67 20 63 61 73 74 20 74 6f 20 61   being cast to a
f2a10 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65   function pointe
f2a20 72 0a 20 20 2a 2a 20 61 6e 64 20 73 6f 20 77 65  r.  ** and so we
f2a30 20 68 61 76 65 20 74 6f 20 67 6f 20 74 68 72 6f   have to go thro
f2a40 75 67 68 20 74 68 65 20 69 6e 74 65 72 6d 65 64  ugh the intermed
f2a50 69 61 74 65 20 70 6f 69 6e 74 65 72 20 74 6f 20  iate pointer to 
f2a60 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a 20  avoid problems. 
f2a70 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69   ** when compili
f2a80 6e 67 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69  ng with -pedanti
f2a90 63 2d 65 72 72 6f 72 73 20 6f 6e 20 47 43 43 2e  c-errors on GCC.
f2aa0 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ).  **.  ** The 
f2ab0 46 49 4e 44 45 52 20 70 61 72 61 6d 65 74 65 72  FINDER parameter
f2ac0 20 74 6f 20 74 68 69 73 20 6d 61 63 72 6f 20 69   to this macro i
f2ad0 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
f2ae0 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  e pointer to the
f2af0 0a 20 20 2a 2a 20 66 69 6e 64 65 72 2d 66 75 6e  .  ** finder-fun
f2b00 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 69 6e 64  ction.  The find
f2b10 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  er-function retu
f2b20 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
f2b30 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65   the.  ** sqlite
f2b40 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  _io_methods obje
f2b50 63 74 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  ct that implemen
f2b60 74 73 20 74 68 65 20 64 65 73 69 72 65 64 20 6c  ts the desired l
f2b70 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 62 65 68 61  ocking.  ** beha
f2b80 76 69 6f 72 73 2e 20 20 53 65 65 20 74 68 65 20  viors.  See the 
f2b90 64 69 76 69 73 69 6f 6e 20 61 62 6f 76 65 20 74  division above t
f2ba0 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  hat contains the
f2bb0 20 49 4f 4d 45 54 48 4f 44 53 0a 20 20 2a 2a 20   IOMETHODS.  ** 
f2bc0 6d 61 63 72 6f 20 66 6f 72 20 61 64 64 69 74 69  macro for additi
f2bd0 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f  on information o
f2be0 6e 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f  n finder-functio
f2bf0 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 6f  ns..  **.  ** Mo
f2c00 73 74 20 66 69 6e 64 65 72 73 20 73 69 6d 70 6c  st finders simpl
f2c10 79 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  y return a point
f2c20 65 72 20 74 6f 20 61 20 66 69 78 65 64 20 73 71  er to a fixed sq
f2c30 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
f2c40 0a 20 20 2a 2a 20 6f 62 6a 65 63 74 2e 20 20 42  .  ** object.  B
f2c50 75 74 20 74 68 65 20 22 61 75 74 6f 6c 6f 63 6b  ut the "autolock
f2c60 49 6f 46 69 6e 64 65 72 22 20 61 76 61 69 6c 61  IoFinder" availa
f2c70 62 6c 65 20 6f 6e 20 4d 61 63 4f 53 58 20 64 6f  ble on MacOSX do
f2c80 65 73 20 61 20 6c 69 74 74 6c 65 0a 20 20 2a 2a  es a little.  **
f2c90 20 6d 6f 72 65 20 74 68 61 6e 20 74 68 61 74 3b   more than that;
f2ca0 20 69 74 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65   it looks at the
f2cb0 20 66 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65   filesystem type
f2cc0 20 74 68 61 74 20 68 6f 73 74 73 20 74 68 65 20   that hosts the 
f2cd0 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66  .  ** database f
f2ce0 69 6c 65 20 61 6e 64 20 74 72 69 65 73 20 74 6f  ile and tries to
f2cf0 20 63 68 6f 6f 73 65 20 61 6e 20 6c 6f 63 6b 69   choose an locki
f2d00 6e 67 20 6d 65 74 68 6f 64 20 61 70 70 72 6f 70  ng method approp
f2d10 72 69 61 74 65 20 66 6f 72 0a 20 20 2a 2a 20 74  riate for.  ** t
f2d20 68 61 74 20 66 69 6c 65 73 79 73 74 65 6d 20 74  hat filesystem t
f2d30 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20 23 64 65 66  ime..  */.  #def
f2d40 69 6e 65 20 55 4e 49 58 56 46 53 28 56 46 53 4e  ine UNIXVFS(VFSN
f2d50 41 4d 45 2c 20 46 49 4e 44 45 52 29 20 7b 20 20  AME, FINDER) {  
f2d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2d70 20 20 20 20 20 20 5c 0a 20 20 20 20 31 2c 20 20        \.    1,  
f2d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2d90 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f    /* iVersion */
f2da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2db0 20 20 20 20 5c 0a 20 20 20 20 73 69 7a 65 6f 66      \.    sizeof
f2dc0 28 75 6e 69 78 46 69 6c 65 29 2c 20 20 20 20 20  (unixFile),     
f2dd0 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 20 20  /* szOsFile */  
f2de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2df0 20 20 5c 0a 20 20 20 20 4d 41 58 5f 50 41 54 48    \.    MAX_PATH
f2e00 4e 41 4d 45 2c 20 20 20 20 20 20 20 20 20 2f 2a  NAME,         /*
f2e10 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f 20 20   mxPathname */  
f2e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2e30 5c 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  \.    0,        
f2e40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70              /* p
f2e50 4e 65 78 74 20 2a 2f 20 20 20 20 20 20 20 20 20  Next */         
f2e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
f2e70 20 20 20 20 56 46 53 4e 41 4d 45 2c 20 20 20 20      VFSNAME,    
f2e80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e 61            /* zNa
f2e90 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  me */           
f2ea0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
f2eb0 20 20 28 76 6f 69 64 2a 29 26 46 49 4e 44 45 52    (void*)&FINDER
f2ec0 2c 20 20 20 20 20 20 20 2f 2a 20 70 41 70 70 44  ,       /* pAppD
f2ed0 61 74 61 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ata */          
f2ee0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
f2ef0 75 6e 69 78 4f 70 65 6e 2c 20 20 20 20 20 20 20  unixOpen,       
f2f00 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a        /* xOpen *
f2f10 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
f2f20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e          \.    un
f2f30 69 78 44 65 6c 65 74 65 2c 20 20 20 20 20 20 20  ixDelete,       
f2f40 20 20 20 20 2f 2a 20 78 44 65 6c 65 74 65 20 2a      /* xDelete *
f2f50 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
f2f60 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78        \.    unix
f2f70 41 63 63 65 73 73 2c 20 20 20 20 20 20 20 20 20  Access,         
f2f80 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 20    /* xAccess */ 
f2f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2fa0 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 46 75      \.    unixFu
f2fb0 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20 20 20  llPathname,     
f2fc0 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65  /* xFullPathname
f2fd0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
f2fe0 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 4f 70    \.    unixDlOp
f2ff0 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  en,           /*
f3000 20 78 44 6c 4f 70 65 6e 20 2a 2f 20 20 20 20 20   xDlOpen */     
f3010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3020 5c 0a 20 20 20 20 75 6e 69 78 44 6c 45 72 72 6f  \.    unixDlErro
f3030 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78  r,          /* x
f3040 44 6c 45 72 72 6f 72 20 2a 2f 20 20 20 20 20 20  DlError */      
f3050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
f3060 20 20 20 20 75 6e 69 78 44 6c 53 79 6d 2c 20 20      unixDlSym,  
f3070 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c            /* xDl
f3080 53 79 6d 20 2a 2f 20 20 20 20 20 20 20 20 20 20  Sym */          
f3090 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
f30a0 20 20 75 6e 69 78 44 6c 43 6c 6f 73 65 2c 20 20    unixDlClose,  
f30b0 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 43 6c          /* xDlCl
f30c0 6f 73 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ose */          
f30d0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
f30e0 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 73 2c 20  unixRandomness, 
f30f0 20 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d        /* xRandom
f3100 6e 65 73 73 20 2a 2f 20 20 20 20 20 20 20 20 20  ness */         
f3110 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e          \.    un
f3120 69 78 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20  ixSleep,        
f3130 20 20 20 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f      /* xSleep */
f3140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3150 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78        \.    unix
f3160 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 20  CurrentTime,    
f3170 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d    /* xCurrentTim
f3180 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  e */            
f3190 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 47 65      \.    unixGe
f31a0 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 20 20  tLastError      
f31b0 2f 2a 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72  /* xGetLastError
f31c0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
f31d0 20 20 5c 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20    \.  }..  /*.  
f31e0 2a 2a 20 41 6c 6c 20 64 65 66 61 75 6c 74 20 56  ** All default V
f31f0 46 53 65 73 20 66 6f 72 20 75 6e 69 78 20 61 72  FSes for unix ar
f3200 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74  e contained in t
f3210 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 72  he following arr
f3220 61 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f  ay..  **.  ** No
f3230 74 65 20 74 68 61 74 20 74 68 65 20 73 71 6c 69  te that the sqli
f3240 74 65 33 5f 76 66 73 2e 70 4e 65 78 74 20 66 69  te3_vfs.pNext fi
f3250 65 6c 64 20 6f 66 20 74 68 65 20 56 46 53 20 6f  eld of the VFS o
f3260 62 6a 65 63 74 20 69 73 20 6d 6f 64 69 66 69 65  bject is modifie
f3270 64 0a 20 20 2a 2a 20 62 79 20 74 68 65 20 53 51  d.  ** by the SQ
f3280 4c 69 74 65 20 63 6f 72 65 20 77 68 65 6e 20 74  Lite core when t
f3290 68 65 20 56 46 53 20 69 73 20 72 65 67 69 73 74  he VFS is regist
f32a0 65 72 65 64 2e 20 20 53 6f 20 74 68 65 20 66 6f  ered.  So the fo
f32b0 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 61 72 72  llowing.  ** arr
f32c0 61 79 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e  ay cannot be con
f32d0 73 74 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69  st..  */.  stati
f32e0 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20 61 56  c sqlite3_vfs aV
f32f0 66 73 5b 5d 20 3d 20 7b 0a 23 69 66 20 53 51 4c  fs[] = {.#if SQL
f3300 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
f3310 4e 47 5f 53 54 59 4c 45 20 26 26 20 28 4f 53 5f  NG_STYLE && (OS_
f3320 56 58 57 4f 52 4b 53 20 7c 7c 20 64 65 66 69 6e  VXWORKS || defin
f3330 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 29 0a 20  ed(__APPLE__)). 
f3340 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78     UNIXVFS("unix
f3350 22 2c 20 20 20 20 20 20 20 20 20 20 61 75 74 6f  ",          auto
f3360 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0a  lockIoFinder ),.
f3370 23 65 6c 73 65 0a 20 20 20 20 55 4e 49 58 56 46  #else.    UNIXVF
f3380 53 28 22 75 6e 69 78 22 2c 20 20 20 20 20 20 20  S("unix",       
f3390 20 20 20 70 6f 73 69 78 49 6f 46 69 6e 64 65 72     posixIoFinder
f33a0 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20 55   ),.#endif.    U
f33b0 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 6e 6f 6e  NIXVFS("unix-non
f33c0 65 22 2c 20 20 20 20 20 6e 6f 6c 6f 63 6b 49 6f  e",     nolockIo
f33d0 46 69 6e 64 65 72 20 29 2c 0a 20 20 20 20 55 4e  Finder ),.    UN
f33e0 49 58 56 46 53 28 22 75 6e 69 78 2d 64 6f 74 66  IXVFS("unix-dotf
f33f0 69 6c 65 22 2c 20 20 64 6f 74 6c 6f 63 6b 49 6f  ile",  dotlockIo
f3400 46 69 6e 64 65 72 20 29 2c 0a 20 20 20 20 55 4e  Finder ),.    UN
f3410 49 58 56 46 53 28 22 75 6e 69 78 2d 77 66 6c 22  IXVFS("unix-wfl"
f3420 2c 20 20 20 20 20 20 70 6f 73 69 78 57 66 6c 49  ,      posixWflI
f3430 6f 46 69 6e 64 65 72 20 29 2c 0a 23 69 66 20 4f  oFinder ),.#if O
f3440 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 55 4e  S_VXWORKS.    UN
f3450 49 58 56 46 53 28 22 75 6e 69 78 2d 6e 61 6d 65  IXVFS("unix-name
f3460 64 73 65 6d 22 2c 20 73 65 6d 49 6f 46 69 6e 64  dsem", semIoFind
f3470 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a 23 69 66  er ),.#endif.#if
f3480 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
f3490 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 20  OCKING_STYLE.   
f34a0 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 70   UNIXVFS("unix-p
f34b0 6f 73 69 78 22 2c 20 20 20 20 70 6f 73 69 78 49  osix",    posixI
f34c0 6f 46 69 6e 64 65 72 20 29 2c 0a 23 69 66 20 21  oFinder ),.#if !
f34d0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 55  OS_VXWORKS.    U
f34e0 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 66 6c 6f  NIXVFS("unix-flo
f34f0 63 6b 22 2c 20 20 20 20 66 6c 6f 63 6b 49 6f 46  ck",    flockIoF
f3500 69 6e 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a  inder ),.#endif.
f3510 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
f3520 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
f3530 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65  _STYLE && define
f3540 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20  d(__APPLE__).   
f3550 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 61   UNIXVFS("unix-a
f3560 66 70 22 2c 20 20 20 20 20 20 61 66 70 49 6f 46  fp",      afpIoF
f3570 69 6e 64 65 72 20 29 2c 0a 20 20 20 20 55 4e 49  inder ),.    UNI
f3580 58 56 46 53 28 22 75 6e 69 78 2d 70 72 6f 78 79  XVFS("unix-proxy
f3590 22 2c 20 20 20 20 70 72 6f 78 79 49 6f 46 69 6e  ",    proxyIoFin
f35a0 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20  der ),.#endif.  
f35b0 7d 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  };.  unsigned in
f35c0 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  t i;          /*
f35d0 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
f35e0 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20  ..  /* Register 
f35f0 61 6c 6c 20 56 46 53 65 73 20 64 65 66 69 6e 65  all VFSes define
f3600 64 20 69 6e 20 74 68 65 20 61 56 66 73 5b 5d 20  d in the aVfs[] 
f3610 61 72 72 61 79 20 2a 2f 0a 20 20 66 6f 72 28 69  array */.  for(i
f3620 3d 30 3b 20 69 3c 28 73 69 7a 65 6f 66 28 61 56  =0; i<(sizeof(aV
f3630 66 73 29 2f 73 69 7a 65 6f 66 28 73 71 6c 69 74  fs)/sizeof(sqlit
f3640 65 33 5f 76 66 73 29 29 3b 20 69 2b 2b 29 7b 0a  e3_vfs)); i++){.
f3650 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f      sqlite3_vfs_
f3660 72 65 67 69 73 74 65 72 28 26 61 56 66 73 5b 69  register(&aVfs[i
f3670 5d 2c 20 69 3d 3d 30 29 3b 0a 20 20 7d 0a 20 20  ], i==0);.  }.  
f3680 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
f3690 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74  ; .}../*.** Shut
f36a0 64 6f 77 6e 20 74 68 65 20 6f 70 65 72 61 74 69  down the operati
f36b0 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66  ng system interf
f36c0 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20  ace..**.** Some 
f36d0 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
f36e0 73 20 6d 69 67 68 74 20 6e 65 65 64 20 74 6f 20  s might need to 
f36f0 64 6f 20 73 6f 6d 65 20 63 6c 65 61 6e 75 70 20  do some cleanup 
f3700 69 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c  in this routine,
f3710 0a 2a 2a 20 74 6f 20 72 65 6c 65 61 73 65 20 64  .** to release d
f3720 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63  ynamically alloc
f3730 61 74 65 64 20 6f 62 6a 65 63 74 73 2e 20 20 42  ated objects.  B
f3740 75 74 20 6e 6f 74 20 6f 6e 20 75 6e 69 78 2e 0a  ut not on unix..
f3750 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
f3760 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 75  is a no-op for u
f3770 6e 69 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  nix..*/.SQLITE_A
f3780 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
f3790 73 5f 65 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20  s_end(void){ .  
f37a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
f37b0 3b 20 0a 7d 0a 20 0a 23 65 6e 64 69 66 20 2f 2a  ; .}. .#endif /*
f37c0 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20   SQLITE_OS_UNIX 
f37d0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
f37e0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 75 6e  *** End of os_un
f37f0 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ix.c ***********
f3800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3820 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
f3830 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f  *** Begin file o
f3840 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  s_win.c ********
f3850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3870 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d  **/./*.** 2004 M
f3880 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 22.**.** The 
f3890 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
f38a0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
f38b0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
f38c0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
f38d0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
f38e0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
f38f0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
f3900 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
f3910 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
f3920 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
f3930 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
f3940 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
f3950 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
f3960 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
f3970 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
f3980 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
f3990 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
f39a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f39b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f39c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f39d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f39e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
f39f0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
f3a00 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  ns code that is 
f3a10 73 70 65 63 69 66 69 63 20 74 6f 20 77 69 6e 64  specific to wind
f3a20 6f 77 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  ows..*/.#if SQLI
f3a30 54 45 5f 4f 53 5f 57 49 4e 20 20 20 20 20 20 20  TE_OS_WIN       
f3a40 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
f3a50 66 69 6c 65 20 69 73 20 75 73 65 64 20 66 6f 72  file is used for
f3a60 20 77 69 6e 64 6f 77 73 20 6f 6e 6c 79 20 2a 2f   windows only */
f3a70 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20  .../*.** A Note 
f3a80 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41 6c 6c  About Memory All
f3a90 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54  ocation:.**.** T
f3aa0 68 69 73 20 64 72 69 76 65 72 20 75 73 65 73 20  his driver uses 
f3ab0 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 20  malloc()/free() 
f3ac0 64 69 72 65 63 74 6c 79 20 72 61 74 68 65 72 20  directly rather 
f3ad0 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75  than going throu
f3ae0 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65  gh.** the SQLite
f3af0 2d 77 72 61 70 70 65 72 73 20 73 71 6c 69 74 65  -wrappers sqlite
f3b00 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 74  3_malloc()/sqlit
f3b10 65 33 5f 66 72 65 65 28 29 2e 20 20 54 68 6f 73  e3_free().  Thos
f3b20 65 20 77 72 61 70 70 65 72 73 0a 2a 2a 20 61 72  e wrappers.** ar
f3b30 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75  e designed for u
f3b40 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 73  se on embedded s
f3b50 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d 65 6d  ystems where mem
f3b60 6f 72 79 20 69 73 20 73 63 61 72 63 65 20 61 6e  ory is scarce an
f3b70 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  d.** malloc fail
f3b80 75 72 65 73 20 68 61 70 70 65 6e 20 66 72 65 71  ures happen freq
f3b90 75 65 6e 74 6c 79 2e 20 20 57 69 6e 33 32 20 64  uently.  Win32 d
f3ba0 6f 65 73 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c  oes not typicall
f3bb0 79 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65  y run on.** embe
f3bc0 64 64 65 64 20 73 79 73 74 65 6d 73 2c 20 61 6e  dded systems, an
f3bd0 64 20 77 68 65 6e 20 69 74 20 64 6f 65 73 20 74  d when it does t
f3be0 68 65 20 64 65 76 65 6c 6f 70 65 72 73 20 6e 6f  he developers no
f3bf0 72 6d 61 6c 6c 79 20 68 61 76 65 20 62 69 67 67  rmally have bigg
f3c00 65 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 74  er.** problems t
f3c10 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 74 68  o worry about th
f3c20 61 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20 6f  an running out o
f3c30 66 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68  f memory.  So th
f3c40 65 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20  ere is not.** a 
f3c50 63 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64 20  compelling need 
f3c60 74 6f 20 75 73 65 20 74 68 65 20 77 72 61 70 70  to use the wrapp
f3c70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74  ers..**.** But t
f3c80 68 65 72 65 20 69 73 20 61 20 67 6f 6f 64 20 72  here is a good r
f3c90 65 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 65  eason to not use
f3ca0 20 74 68 65 20 77 72 61 70 70 65 72 73 2e 20 20   the wrappers.  
f3cb0 49 66 20 77 65 20 75 73 65 20 74 68 65 0a 2a 2a  If we use the.**
f3cc0 20 77 72 61 70 70 65 72 73 20 74 68 65 6e 20 77   wrappers then w
f3cd0 65 20 77 69 6c 6c 20 67 65 74 20 73 69 6d 75 6c  e will get simul
f3ce0 61 74 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66 61  ated malloc() fa
f3cf0 69 6c 75 72 65 73 20 77 69 74 68 69 6e 20 74 68  ilures within th
f3d00 69 73 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 41  is.** driver.  A
f3d10 6e 64 20 74 68 61 74 20 63 61 75 73 65 73 20 61  nd that causes a
f3d20 6c 6c 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f 62  ll kinds of prob
f3d30 6c 65 6d 73 20 66 6f 72 20 6f 75 72 20 74 65 73  lems for our tes
f3d40 74 73 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c 64  ts.  We.** could
f3d50 20 65 6e 68 61 6e 63 65 20 53 51 4c 69 74 65 20   enhance SQLite 
f3d60 74 6f 20 64 65 61 6c 20 77 69 74 68 20 73 69 6d  to deal with sim
f3d70 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66 61  ulated malloc fa
f3d80 69 6c 75 72 65 73 20 77 69 74 68 69 6e 0a 2a 2a  ilures within.**
f3d90 20 74 68 65 20 4f 53 20 64 72 69 76 65 72 2c 20   the OS driver, 
f3da0 62 75 74 20 74 68 65 20 63 6f 64 65 20 74 6f 20  but the code to 
f3db0 64 65 61 6c 20 77 69 74 68 20 74 68 6f 73 65 20  deal with those 
f3dc0 66 61 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e 6f  failure would no
f3dd0 74 0a 2a 2a 20 62 65 20 65 78 65 72 63 69 73 65  t.** be exercise
f3de0 64 20 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69 63  d on Linux (whic
f3df0 68 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  h does not need 
f3e00 74 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 74  to malloc() in t
f3e10 68 65 20 64 72 69 76 65 72 29 0a 2a 2a 20 61 6e  he driver).** an
f3e20 64 20 73 6f 20 77 65 20 77 6f 75 6c 64 20 68 61  d so we would ha
f3e30 76 65 20 64 69 66 66 69 63 75 6c 74 79 20 77 72  ve difficulty wr
f3e40 69 74 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74  iting coverage t
f3e50 65 73 74 73 20 66 6f 72 20 74 68 61 74 0a 2a 2a  ests for that.**
f3e60 20 63 6f 64 65 2e 20 20 42 65 74 74 65 72 20 74   code.  Better t
f3e70 6f 20 6c 65 61 76 65 20 74 68 65 20 63 6f 64 65  o leave the code
f3e80 20 6f 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e 0a   out, we think..
f3e90 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20  **.** The point 
f3ea0 6f 66 20 74 68 69 73 20 64 69 73 63 75 73 73 69  of this discussi
f3eb0 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  on is as follows
f3ec0 3a 20 20 57 68 65 6e 20 63 72 65 61 74 69 6e 67  :  When creating
f3ed0 20 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 79   a new.** OS lay
f3ee0 65 72 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 64  er for an embedd
f3ef0 65 64 20 73 79 73 74 65 6d 2c 20 69 66 20 79 6f  ed system, if yo
f3f00 75 20 75 73 65 20 74 68 69 73 20 66 69 6c 65 20  u use this file 
f3f10 61 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a  as an example,.*
f3f20 2a 20 61 76 6f 69 64 20 74 68 65 20 75 73 65 20  * avoid the use 
f3f30 6f 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65  of malloc()/free
f3f40 28 29 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 69  ().  Those routi
f3f50 6e 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 77  nes work ok on w
f3f60 69 6e 64 6f 77 73 0a 2a 2a 20 64 65 73 6b 74 6f  indows.** deskto
f3f70 70 73 20 62 75 74 20 6e 6f 74 20 73 6f 20 77 65  ps but not so we
f3f80 6c 6c 20 69 6e 20 65 6d 62 65 64 64 65 64 20 73  ll in embedded s
f3f90 79 73 74 65 6d 73 2e 0a 2a 2f 0a 0a 23 69 6e 63  ystems..*/..#inc
f3fa0 6c 75 64 65 20 3c 77 69 6e 62 61 73 65 2e 68 3e  lude <winbase.h>
f3fb0 0a 0a 23 69 66 64 65 66 20 5f 5f 43 59 47 57 49  ..#ifdef __CYGWI
f3fc0 4e 5f 5f 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73  N__.# include <s
f3fd0 79 73 2f 63 79 67 77 69 6e 2e 68 3e 0a 23 65 6e  ys/cygwin.h>.#en
f3fe0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f  dif../*.** Macro
f3ff0 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
f4000 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e  ine whether or n
f4010 6f 74 20 74 6f 20 75 73 65 20 74 68 72 65 61 64  ot to use thread
f4020 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  s..*/.#if define
f4030 64 28 54 48 52 45 41 44 53 41 46 45 29 20 26 26  d(THREADSAFE) &&
f4040 20 54 48 52 45 41 44 53 41 46 45 0a 23 20 64 65   THREADSAFE.# de
f4050 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 33 32 5f  fine SQLITE_W32_
f4060 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66  THREADS 1.#endif
f4070 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20  ../*.** Include 
f4080 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d  code that is com
f4090 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e  mon to all os_*.
f40a0 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a  c files.*/./****
f40b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
f40c0 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69  de os_common.h i
f40d0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
f40e0 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a  os_win.c *******
f40f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
f4100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
f4110 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e   file os_common.
f4120 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
f4130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
f4150 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a   2004 May 22.**.
f4160 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
f4170 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
f4180 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
f4190 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
f41a0 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
f41b0 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
f41c0 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
f41d0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
f41e0 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
f41f0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
f4200 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
f4210 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
f4220 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
f4230 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
f4240 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
f4250 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
f4260 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
f4270 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
f4280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f42a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f42b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f42c0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
f42d0 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f 73   contains macros
f42e0 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20 62 69   and a little bi
f42f0 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20 69  t of code that i
f4300 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61  s common to.** a
f4310 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 66 6f  ll of the platfo
f4320 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69 6c 65  rm-specific file
f4330 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 69  s (os_*.c) and i
f4340 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f  s #included into
f4350 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 2e   those.** files.
f4360 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
f4370 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e 63 6c   should be #incl
f4380 75 64 65 64 20 62 79 20 74 68 65 20 6f 73 5f 2a  uded by the os_*
f4390 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20  .c files only.  
f43a0 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 67  It is not a.** g
f43b0 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20 68  eneral purpose h
f43c0 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  eader file..**.*
f43d0 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e  * $Id: os_common
f43e0 2e 68 2c 76 20 31 2e 33 38 20 32 30 30 39 2f 30  .h,v 1.38 2009/0
f43f0 32 2f 32 34 20 31 38 3a 34 30 3a 35 30 20 64 61  2/24 18:40:50 da
f4400 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a  nielk1977 Exp $.
f4410 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f 43  */.#ifndef _OS_C
f4420 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e 65  OMMON_H_.#define
f4430 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a   _OS_COMMON_H_..
f4440 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 74  /*.** At least t
f4450 77 6f 20 62 75 67 73 20 68 61 76 65 20 73 6c 69  wo bugs have sli
f4460 70 70 65 64 20 69 6e 20 62 65 63 61 75 73 65 20  pped in because 
f4470 77 65 20 63 68 61 6e 67 65 64 20 74 68 65 20 4d  we changed the M
f4480 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a 20 6d  EMORY_DEBUG.** m
f4490 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 45 5f 44  acro to SQLITE_D
f44a0 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 20 6f 6c  EBUG and some ol
f44b0 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 20 68 61  der makefiles ha
f44c0 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20  ve not yet made 
f44d0 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e 20 20  the.** switch.  
f44e0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  The following co
f44f0 64 65 20 73 68 6f 75 6c 64 20 63 61 74 63 68 20  de should catch 
f4500 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 61 74 20  this problem at 
f4510 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f  compile-time..*/
f4520 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 59 5f 44  .#ifdef MEMORY_D
f4530 45 42 55 47 0a 23 20 65 72 72 6f 72 20 22 54 68  EBUG.# error "Th
f4540 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 20 6d  e MEMORY_DEBUG m
f4550 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65 74 65  acro is obsolete
f4560 2e 20 20 55 73 65 20 53 51 4c 49 54 45 5f 44 45  .  Use SQLITE_DE
f4570 42 55 47 20 69 6e 73 74 65 61 64 2e 22 0a 23 65  BUG instead.".#e
f4580 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
f4590 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45  ITE_DEBUG.SQLITE
f45a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
f45b0 69 74 65 33 4f 53 54 72 61 63 65 20 3d 20 30 3b  ite3OSTrace = 0;
f45c0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
f45d0 31 28 58 29 20 20 20 20 20 20 20 20 20 69 66 28  1(X)         if(
f45e0 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20   sqlite3OSTrace 
f45f0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
f4600 69 6e 74 66 28 58 29 0a 23 64 65 66 69 6e 65 20  intf(X).#define 
f4610 4f 53 54 52 41 43 45 32 28 58 2c 59 29 20 20 20  OSTRACE2(X,Y)   
f4620 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f      if( sqlite3O
f4630 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33  STrace ) sqlite3
f4640 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 29  DebugPrintf(X,Y)
f4650 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
f4660 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 69 66 28  3(X,Y,Z)     if(
f4670 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20   sqlite3OSTrace 
f4680 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
f4690 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 66  intf(X,Y,Z).#def
f46a0 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59  ine OSTRACE4(X,Y
f46b0 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 71 6c 69  ,Z,A)   if( sqli
f46c0 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c  te3OSTrace ) sql
f46d0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
f46e0 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65  X,Y,Z,A).#define
f46f0 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c   OSTRACE5(X,Y,Z,
f4700 41 2c 42 29 20 69 66 28 20 73 71 6c 69 74 65 33  A,B) if( sqlite3
f4710 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65  OSTrace ) sqlite
f4720 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59  3DebugPrintf(X,Y
f4730 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20  ,Z,A,B).#define 
f4740 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41  OSTRACE6(X,Y,Z,A
f4750 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 66 28 73  ,B,C) \.    if(s
f4760 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73  qlite3OSTrace) s
f4770 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
f4780 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23  f(X,Y,Z,A,B,C).#
f4790 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28  define OSTRACE7(
f47a0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c  X,Y,Z,A,B,C,D) \
f47b0 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f  .    if(sqlite3O
f47c0 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 44  STrace) sqlite3D
f47d0 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a  ebugPrintf(X,Y,Z
f47e0 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65 0a  ,A,B,C,D).#else.
f47f0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31  #define OSTRACE1
f4800 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  (X).#define OSTR
f4810 41 43 45 32 28 58 2c 59 29 0a 23 64 65 66 69 6e  ACE2(X,Y).#defin
f4820 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a  e OSTRACE3(X,Y,Z
f4830 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
f4840 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66  E4(X,Y,Z,A).#def
f4850 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59  ine OSTRACE5(X,Y
f4860 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20  ,Z,A,B).#define 
f4870 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41  OSTRACE6(X,Y,Z,A
f4880 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,B,C).#define OS
f4890 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE7(X,Y,Z,A,B
f48a0 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ,C,D).#endif../*
f48b0 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 70  .** Macros for p
f48c0 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 61 63 69  erformance traci
f48d0 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75  ng.  Normally tu
f48e0 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 20  rned off.  Only 
f48f0 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 36  works.** on i486
f4900 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 69   hardware..*/.#i
f4910 66 64 65 66 20 53 51 4c 49 54 45 5f 50 45 52 46  fdef SQLITE_PERF
f4920 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a 0a 2f  ORMANCE_TRACE../
f4930 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20 63  * .** hwtime.h c
f4940 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61  ontains inline a
f4950 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 66 6f  ssembler code fo
f4960 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 0a  r implementing .
f4970 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 6d 61  ** high-performa
f4980 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 74 69  nce timing routi
f4990 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  nes..*/./*******
f49a0 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
f49b0 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 65 20  hwtime.h in the 
f49c0 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d  middle of os_com
f49d0 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  mon.h **********
f49e0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
f49f0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
f4a00 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a  le hwtime.h ****
f4a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4a30 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
f4a40 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20  08 May 27.**.** 
f4a50 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
f4a60 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
f4a70 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
f4a80 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
f4a90 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
f4aa0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
f4ab0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
f4ac0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
f4ad0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
f4ae0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
f4af0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
f4b00 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
f4b10 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
f4b20 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
f4b30 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
f4b40 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
f4b50 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
f4b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
f4bb0 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
f4bc0 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73  ntains inline as
f4bd0 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 74 72 69  m code for retri
f4be0 65 76 69 6e 67 20 22 68 69 67 68 2d 70 65 72 66  eving "high-perf
f4bf0 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e  ormance".** coun
f4c00 74 65 72 73 20 66 6f 72 20 78 38 36 20 63 6c 61  ters for x86 cla
f4c10 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24  ss CPUs..**.** $
f4c20 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20 31  Id: hwtime.h,v 1
f4c30 2e 33 20 32 30 30 38 2f 30 38 2f 30 31 20 31 34  .3 2008/08/01 14
f4c40 3a 33 33 3a 31 35 20 73 68 61 6e 65 20 45 78 70  :33:15 shane Exp
f4c50 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48   $.*/.#ifndef _H
f4c60 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65  WTIME_H_.#define
f4c70 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a   _HWTIME_H_../*.
f4c80 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
f4c90 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f   routine only wo
f4ca0 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63  rks on pentium-c
f4cb0 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20  lass (or newer) 
f4cc0 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49  processors..** I
f4cd0 74 20 75 73 65 73 20 74 68 65 20 52 44 54 53 43  t uses the RDTSC
f4ce0 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20   opcode to read 
f4cf0 74 68 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20  the cycle count 
f4d00 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65  value out of the
f4d10 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e  .** processor an
f4d20 64 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 76  d returns that v
f4d30 61 6c 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20  alue.  This can 
f4d40 62 65 20 75 73 65 64 20 66 6f 72 20 68 69 67 68  be used for high
f4d50 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e  -res.** profilin
f4d60 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e  g..*/.#if (defin
f4d70 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20  ed(__GNUC__) || 
f4d80 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52  defined(_MSC_VER
f4d90 29 29 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64  )) && \.      (d
f4da0 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20  efined(i386) || 
f4db0 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f  defined(__i386__
f4dc0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f  ) || defined(_M_
f4dd0 49 58 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65  IX86))..  #if de
f4de0 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a  fined(__GNUC__).
f4df0 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71  .  __inline__ sq
f4e00 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69  lite_uint64 sqli
f4e10 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b  te3Hwtime(void){
f4e20 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69  .     unsigned i
f4e30 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20  nt lo, hi;.     
f4e40 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69  __asm__ __volati
f4e50 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20  le__ ("rdtsc" : 
f4e60 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20  "=a" (lo), "=d" 
f4e70 28 68 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75  (hi));.     retu
f4e80 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36  rn (sqlite_uint6
f4e90 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b  4)hi << 32 | lo;
f4ea0 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65  .  }..  #elif de
f4eb0 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a  fined(_MSC_VER).
f4ec0 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61  .  __declspec(na
f4ed0 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71  ked) __inline sq
f4ee0 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64  lite_uint64 __cd
f4ef0 65 63 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d  ecl sqlite3Hwtim
f4f00 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f  e(void){.     __
f4f10 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64  asm {.        rd
f4f20 74 73 63 0a 20 20 20 20 20 20 20 20 72 65 74 20  tsc.        ret 
f4f30 20 20 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76        ; return v
f4f40 61 6c 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a  alue at EDX:EAX.
f4f50 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65       }.  }..  #e
f4f60 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66  ndif..#elif (def
f4f70 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26  ined(__GNUC__) &
f4f80 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f  & defined(__x86_
f4f90 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69  64__))..  __inli
f4fa0 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74  ne__ sqlite_uint
f4fb0 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  64 sqlite3Hwtime
f4fc0 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e  (void){.      un
f4fd0 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b  signed long val;
f4fe0 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f  .      __asm__ _
f4ff0 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64  _volatile__ ("rd
f5000 74 73 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c  tsc" : "=A" (val
f5010 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ));.      return
f5020 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69   val;.  }. .#eli
f5030 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55  f (defined(__GNU
f5040 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28  C__) && defined(
f5050 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69  __ppc__))..  __i
f5060 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75  nline__ sqlite_u
f5070 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74  int64 sqlite3Hwt
f5080 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20  ime(void){.     
f5090 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c   unsigned long l
f50a0 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20  ong retval;.    
f50b0 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20    unsigned long 
f50c0 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73  junk;.      __as
f50d0 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f  m__ __volatile__
f50e0 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20   ("\n\.         
f50f0 20 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20   1:      mftbu  
f5100 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20   %1\n\.         
f5110 20 20 20 20 20 20 20 20 20 6d 66 74 62 20 20 20           mftb   
f5120 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20   %L0\n\.        
f5130 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 75 20            mftbu 
f5140 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20    %0\n\.        
f5150 20 20 20 20 20 20 20 20 20 20 63 6d 70 77 20 20            cmpw  
f5160 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20    %0,%1\n\.     
f5170 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6e 65               bne
f5180 20 20 20 20 20 31 62 22 0a 20 20 20 20 20 20 20       1b".       
f5190 20 20 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72             : "=r
f51a0 22 20 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22  " (retval), "=r"
f51b0 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20   (junk));.      
f51c0 72 65 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20  return retval;. 
f51d0 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72   }..#else..  #er
f51e0 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65  ror Need impleme
f51f0 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74  ntation of sqlit
f5200 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79  e3Hwtime() for y
f5210 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20  our platform... 
f5220 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70   /*.  ** To comp
f5230 69 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c  ile without impl
f5240 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33  ementing sqlite3
f5250 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75  Hwtime() for you
f5260 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a  r platform,.  **
f5270 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20   you can remove 
f5280 74 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72  the above #error
f5290 20 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c   and use the fol
f52a0 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62  lowing.  ** stub
f52b0 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20   function.  You 
f52c0 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67  will lose timing
f52d0 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e   support for man
f52e0 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65  y.  ** of the de
f52f0 62 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74  bugging and test
f5300 69 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62  ing utilities, b
f5310 75 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a  ut it should at.
f5320 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69    ** least compi
f5330 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f  le and run..  */
f5340 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
f5350 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20    sqlite_uint64 
f5360 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f  sqlite3Hwtime(vo
f5370 69 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71  id){ return ((sq
f5380 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20  lite_uint64)0); 
f5390 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69  }..#endif..#endi
f53a0 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48  f /* !defined(_H
f53b0 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a  WTIME_H_) */../*
f53c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
f53d0 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a  d of hwtime.h **
f53e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f53f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
f5410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
f5420 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
f5430 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73  e left off in os
f5440 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a  _common.h ******
f5450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73  ************/..s
f5460 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e  tatic sqlite_uin
f5470 74 36 34 20 67 5f 73 74 61 72 74 3b 0a 73 74 61  t64 g_start;.sta
f5480 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  tic sqlite_uint6
f5490 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64 65  4 g_elapsed;.#de
f54a0 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54  fine TIMER_START
f54b0 20 20 20 20 20 20 20 67 5f 73 74 61 72 74 3d 73         g_start=s
f54c0 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 0a 23  qlite3Hwtime().#
f54d0 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44  define TIMER_END
f54e0 20 20 20 20 20 20 20 20 20 67 5f 65 6c 61 70 73           g_elaps
f54f0 65 64 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65  ed=sqlite3Hwtime
f5500 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 65 66 69  ()-g_start.#defi
f5510 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44  ne TIMER_ELAPSED
f5520 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 0a 23       g_elapsed.#
f5530 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 49 4d  else.#define TIM
f5540 45 52 5f 53 54 41 52 54 0a 23 64 65 66 69 6e 65  ER_START.#define
f5550 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69   TIMER_END.#defi
f5560 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44  ne TIMER_ELAPSED
f5570 20 20 20 20 20 28 28 73 71 6c 69 74 65 5f 75 69       ((sqlite_ui
f5580 6e 74 36 34 29 30 29 0a 23 65 6e 64 69 66 0a 0a  nt64)0).#endif..
f5590 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70  /*.** If we comp
f55a0 69 6c 65 20 77 69 74 68 20 74 68 65 20 53 51 4c  ile with the SQL
f55b0 49 54 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 73  ITE_TEST macro s
f55c0 65 74 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c  et, then the fol
f55d0 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20  lowing block.** 
f55e0 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 76  of code will giv
f55f0 65 20 75 73 20 74 68 65 20 61 62 69 6c 69 74 79  e us the ability
f5600 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 64   to simulate a d
f5610 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20  isk I/O error.  
f5620 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 20  This.** is used 
f5630 66 6f 72 20 74 65 73 74 69 6e 67 20 74 68 65 20  for testing the 
f5640 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f 67  I/O recovery log
f5650 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ic..*/.#ifdef SQ
f5660 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
f5670 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
f5680 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20  _io_error_hit = 
f5690 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  0;            /*
f56a0 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
f56b0 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53   I/O Errors */.S
f56c0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
f56d0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68  lite3_io_error_h
f56e0 61 72 64 68 69 74 20 3d 20 30 3b 20 20 20 20 20  ardhit = 0;     
f56f0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
f5700 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72  non-benign error
f5710 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  s */.SQLITE_API 
f5720 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
f5730 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30  rror_pending = 0
f5740 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e  ;        /* Coun
f5750 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 20  t down to first 
f5760 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c  I/O error */.SQL
f5770 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
f5780 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72  te3_io_error_per
f5790 73 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20  sist = 0;       
f57a0 20 2f 2a 20 54 72 75 65 20 69 66 20 49 2f 4f 20   /* True if I/O 
f57b0 65 72 72 6f 72 73 20 70 65 72 73 69 73 74 20 2a  errors persist *
f57c0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
f57d0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
f57e0 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20  r_benign = 0;   
f57f0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
f5800 20 65 72 72 6f 72 73 20 61 72 65 20 62 65 6e 69   errors are beni
f5810 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  gn */.SQLITE_API
f5820 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73   int sqlite3_dis
f5830 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20  kfull_pending = 
f5840 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  0;.SQLITE_API in
f5850 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75  t sqlite3_diskfu
f5860 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20  ll = 0;.#define 
f5870 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42  SimulateIOErrorB
f5880 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 74 65 33  enign(X) sqlite3
f5890 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e  _io_error_benign
f58a0 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d  =(X).#define Sim
f58b0 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 43 4f 44  ulateIOError(COD
f58c0 45 29 20 20 5c 0a 20 20 69 66 28 20 28 73 71 6c  E)  \.  if( (sql
f58d0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
f58e0 72 73 69 73 74 20 26 26 20 73 71 6c 69 74 65 33  rsist && sqlite3
f58f0 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c  _io_error_hit) \
f5900 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74  .       || sqlit
f5910 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64  e3_io_error_pend
f5920 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a  ing-- == 1 )  \.
f5930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20                { 
f5940 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43  local_ioerr(); C
f5950 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f  ODE; }.static vo
f5960 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29  id local_ioerr()
f5970 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 49 4f  {.  IOTRACE(("IO
f5980 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69  ERR\n"));.  sqli
f5990 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74  te3_io_error_hit
f59a0 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74  ++;.  if( !sqlit
f59b0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69  e3_io_error_beni
f59c0 67 6e 20 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f  gn ) sqlite3_io_
f59d0 65 72 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b  error_hardhit++;
f59e0 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c  .}.#define Simul
f59f0 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72  ateDiskfullError
f5a00 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 20  (CODE) \.   if( 
f5a10 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c  sqlite3_diskfull
f5a20 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20  _pending ){ \.  
f5a30 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64     if( sqlite3_d
f5a40 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20  iskfull_pending 
f5a50 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 20  == 1 ){ \.      
f5a60 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20   local_ioerr(); 
f5a70 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  \.       sqlite3
f5a80 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c  _diskfull = 1; \
f5a90 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .       sqlite3_
f5aa0 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 31  io_error_hit = 1
f5ab0 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 3b  ; \.       CODE;
f5ac0 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c   \.     }else{ \
f5ad0 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .       sqlite3_
f5ae0 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67  diskfull_pending
f5af0 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20  --; \.     } \. 
f5b00 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e    }.#else.#defin
f5b10 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f  e SimulateIOErro
f5b20 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 65 66 69  rBenign(X).#defi
f5b30 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  ne SimulateIOErr
f5b40 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 20 53 69  or(A).#define Si
f5b50 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72  mulateDiskfullEr
f5b60 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f  ror(A).#endif../
f5b70 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e  *.** When testin
f5b80 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 20  g, keep a count 
f5b90 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  of the number of
f5ba0 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a   open files..*/.
f5bb0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
f5bc0 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
f5bd0 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66  t sqlite3_open_f
f5be0 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23  ile_count = 0;.#
f5bf0 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74  define OpenCount
f5c00 65 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f 6f  er(X)  sqlite3_o
f5c10 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d  pen_file_count+=
f5c20 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  (X).#else.#defin
f5c30 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29  e OpenCounter(X)
f5c40 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20  .#endif..#endif 
f5c50 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 4f 53 5f  /* !defined(_OS_
f5c60 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f  COMMON_H_) */../
f5c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
f5c80 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e  nd of os_common.
f5c90 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
f5ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
f5cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
f5cd0 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
f5ce0 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f  we left off in o
f5cf0 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  s_win.c ********
f5d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
f5d10 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6d 69 63 72 6f  /*.** Some micro
f5d20 73 6f 66 74 20 63 6f 6d 70 69 6c 65 72 73 20 6c  soft compilers l
f5d30 61 63 6b 20 74 68 69 73 20 64 65 66 69 6e 69 74  ack this definit
f5d40 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ion..*/.#ifndef 
f5d50 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54  INVALID_FILE_ATT
f5d60 52 49 42 55 54 45 53 0a 23 20 64 65 66 69 6e 65  RIBUTES.# define
f5d70 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54   INVALID_FILE_AT
f5d80 54 52 49 42 55 54 45 53 20 28 28 44 57 4f 52 44  TRIBUTES ((DWORD
f5d90 29 2d 31 29 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a  )-1) .#endif../*
f5da0 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 69 66  .** Determine if
f5db0 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20   we are dealing 
f5dc0 77 69 74 68 20 57 69 6e 64 6f 77 73 43 45 20 2d  with WindowsCE -
f5dd0 20 77 68 69 63 68 20 68 61 73 20 61 20 6d 75 63   which has a muc
f5de0 68 0a 2a 2a 20 72 65 64 75 63 65 64 20 41 50 49  h.** reduced API
f5df0 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  ..*/.#if SQLITE_
f5e00 4f 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e  OS_WINCE.# defin
f5e10 65 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53  e AreFileApisANS
f5e20 49 28 29 20 31 0a 23 20 64 65 66 69 6e 65 20 47  I() 1.# define G
f5e30 65 74 44 69 73 6b 46 72 65 65 53 70 61 63 65 57  etDiskFreeSpaceW
f5e40 28 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  () 0.#endif../*.
f5e50 2a 2a 20 57 69 6e 43 45 20 6c 61 63 6b 73 20 6e  ** WinCE lacks n
f5e60 61 74 69 76 65 20 73 75 70 70 6f 72 74 20 66 6f  ative support fo
f5e70 72 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 73  r file locking s
f5e80 6f 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 6b  o we have to fak
f5e90 65 20 69 74 0a 2a 2a 20 77 69 74 68 20 73 6f 6d  e it.** with som
f5ea0 65 20 63 6f 64 65 20 6f 66 20 6f 75 72 20 6f 77  e code of our ow
f5eb0 6e 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  n..*/.#if SQLITE
f5ec0 5f 4f 53 5f 57 49 4e 43 45 0a 74 79 70 65 64 65  _OS_WINCE.typede
f5ed0 66 20 73 74 72 75 63 74 20 77 69 6e 63 65 4c 6f  f struct winceLo
f5ee0 63 6b 20 7b 0a 20 20 69 6e 74 20 6e 52 65 61 64  ck {.  int nRead
f5ef0 65 72 73 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  ers;       /* Nu
f5f00 6d 62 65 72 20 6f 66 20 72 65 61 64 65 72 20 6c  mber of reader l
f5f10 6f 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 2a 2f  ocks obtained */
f5f20 0a 20 20 42 4f 4f 4c 20 62 50 65 6e 64 69 6e 67  .  BOOL bPending
f5f30 3b 20 20 20 20 20 20 2f 2a 20 49 6e 64 69 63 61  ;      /* Indica
f5f40 74 65 73 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f  tes a pending lo
f5f50 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61  ck has been obta
f5f60 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62  ined */.  BOOL b
f5f70 52 65 73 65 72 76 65 64 3b 20 20 20 20 20 2f 2a  Reserved;     /*
f5f80 20 49 6e 64 69 63 61 74 65 73 20 61 20 72 65 73   Indicates a res
f5f90 65 72 76 65 64 20 6c 6f 63 6b 20 68 61 73 20 62  erved lock has b
f5fa0 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a  een obtained */.
f5fb0 20 20 42 4f 4f 4c 20 62 45 78 63 6c 75 73 69 76    BOOL bExclusiv
f5fc0 65 3b 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74  e;    /* Indicat
f5fd0 65 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20  es an exclusive 
f5fe0 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62  lock has been ob
f5ff0 74 61 69 6e 65 64 20 2a 2f 0a 7d 20 77 69 6e 63  tained */.} winc
f6000 65 4c 6f 63 6b 3b 0a 23 65 6e 64 69 66 0a 0a 2f  eLock;.#endif../
f6010 2a 0a 2a 2a 20 54 68 65 20 77 69 6e 46 69 6c 65  *.** The winFile
f6020 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 20   structure is a 
f6030 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69  subclass of sqli
f6040 74 65 33 5f 66 69 6c 65 2a 20 73 70 65 63 69 66  te3_file* specif
f6050 69 63 20 74 6f 20 74 68 65 20 77 69 6e 33 32 0a  ic to the win32.
f6060 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 20 6c  ** portability l
f6070 61 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ayer..*/.typedef
f6080 20 73 74 72 75 63 74 20 77 69 6e 46 69 6c 65 20   struct winFile 
f6090 77 69 6e 46 69 6c 65 3b 0a 73 74 72 75 63 74 20  winFile;.struct 
f60a0 77 69 6e 46 69 6c 65 20 7b 0a 20 20 63 6f 6e 73  winFile {.  cons
f60b0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
f60c0 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 2f 2a  hods *pMethod;/*
f60d0 20 4d 75 73 74 20 62 65 20 66 69 72 73 74 20 2a   Must be first *
f60e0 2f 0a 20 20 48 41 4e 44 4c 45 20 68 3b 20 20 20  /.  HANDLE h;   
f60f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
f6100 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73 73  andle for access
f6110 69 6e 67 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a  ing the file */.
f6120 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
f6130 6c 6f 63 6b 74 79 70 65 3b 20 2f 2a 20 54 79 70  locktype; /* Typ
f6140 65 20 6f 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e  e of lock curren
f6150 74 6c 79 20 68 65 6c 64 20 6f 6e 20 74 68 69 73  tly held on this
f6160 20 66 69 6c 65 20 2a 2f 0a 20 20 73 68 6f 72 74   file */.  short
f6170 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 3b   sharedLockByte;
f6180 20 20 20 2f 2a 20 52 61 6e 64 6f 6d 6c 79 20 63     /* Randomly c
f6190 68 6f 73 65 6e 20 62 79 74 65 20 75 73 65 64 20  hosen byte used 
f61a0 61 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b  as a shared lock
f61b0 20 2a 2f 0a 20 20 44 57 4f 52 44 20 6c 61 73 74   */.  DWORD last
f61c0 45 72 72 6e 6f 3b 20 20 20 20 20 20 20 20 2f 2a  Errno;        /*
f61d0 20 54 68 65 20 57 69 6e 64 6f 77 73 20 65 72 72   The Windows err
f61e0 6e 6f 20 66 72 6f 6d 20 74 68 65 20 6c 61 73 74  no from the last
f61f0 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 20 20   I/O error */.  
f6200 44 57 4f 52 44 20 73 65 63 74 6f 72 53 69 7a 65  DWORD sectorSize
f6210 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 63 74 6f  ;       /* Secto
f6220 72 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 65  r size of the de
f6230 76 69 63 65 20 66 69 6c 65 20 69 73 20 6f 6e 20  vice file is on 
f6240 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  */.#if SQLITE_OS
f6250 5f 57 49 4e 43 45 0a 20 20 57 43 48 41 52 20 2a  _WINCE.  WCHAR *
f6260 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 3b 20  zDeleteOnClose; 
f6270 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65   /* Name of file
f6280 20 74 6f 20 64 65 6c 65 74 65 20 77 68 65 6e 20   to delete when 
f6290 63 6c 6f 73 69 6e 67 20 2a 2f 0a 20 20 48 41 4e  closing */.  HAN
f62a0 44 4c 45 20 68 4d 75 74 65 78 3b 20 20 20 20 20  DLE hMutex;     
f62b0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 75 73       /* Mutex us
f62c0 65 64 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63  ed to control ac
f62d0 63 65 73 73 20 74 6f 20 73 68 61 72 65 64 20 6c  cess to shared l
f62e0 6f 63 6b 20 2a 2f 20 20 0a 20 20 48 41 4e 44 4c  ock */  .  HANDL
f62f0 45 20 68 53 68 61 72 65 64 3b 20 20 20 20 20 20  E hShared;      
f6300 20 20 20 2f 2a 20 53 68 61 72 65 64 20 6d 65 6d     /* Shared mem
f6310 6f 72 79 20 73 65 67 6d 65 6e 74 20 75 73 65 64  ory segment used
f6320 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a   for locking */.
f6330 20 20 77 69 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61    winceLock loca
f6340 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63  l;        /* Loc
f6350 6b 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74  ks obtained by t
f6360 68 69 73 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  his instance of 
f6370 77 69 6e 46 69 6c 65 20 2a 2f 0a 20 20 77 69 6e  winFile */.  win
f6380 63 65 4c 6f 63 6b 20 2a 73 68 61 72 65 64 3b 20  ceLock *shared; 
f6390 20 20 20 20 20 2f 2a 20 47 6c 6f 62 61 6c 20 73       /* Global s
f63a0 68 61 72 65 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72  hared lock memor
f63b0 79 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 20  y for the file  
f63c0 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a  */.#endif.};../*
f63d0 0a 2a 2a 20 46 6f 72 77 61 72 64 20 70 72 6f 74  .** Forward prot
f63e0 6f 74 79 70 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  otypes..*/.stati
f63f0 63 20 69 6e 74 20 67 65 74 53 65 63 74 6f 72 53  c int getSectorS
f6400 69 7a 65 28 0a 20 20 20 20 73 71 6c 69 74 65 33  ize(.    sqlite3
f6410 5f 76 66 73 20 2a 70 56 66 73 2c 0a 20 20 20 20  _vfs *pVfs,.    
f6420 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c  const char *zRel
f6430 61 74 69 76 65 20 20 20 20 20 2f 2a 20 55 54 46  ative     /* UTF
f6440 2d 38 20 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a  -8 file name */.
f6450 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  );../*.** The fo
f6460 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65  llowing variable
f6470 20 69 73 20 28 6e 6f 72 6d 61 6c 6c 79 29 20 73   is (normally) s
f6480 65 74 20 6f 6e 63 65 20 61 6e 64 20 6e 65 76 65  et once and neve
f6490 72 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74 68 65  r changes.** the
f64a0 72 65 61 66 74 65 72 2e 20 20 49 74 20 72 65 63  reafter.  It rec
f64b0 6f 72 64 73 20 77 68 65 74 68 65 72 20 74 68 65  ords whether the
f64c0 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
f64d0 6d 20 69 73 20 57 69 6e 39 35 0a 2a 2a 20 6f 72  m is Win95.** or
f64e0 20 57 69 6e 4e 54 2e 0a 2a 2a 0a 2a 2a 20 30 3a   WinNT..**.** 0:
f64f0 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73 79 73     Operating sys
f6500 74 65 6d 20 75 6e 6b 6e 6f 77 6e 2e 0a 2a 2a 20  tem unknown..** 
f6510 31 3a 20 20 20 4f 70 65 72 61 74 69 6e 67 20 73  1:   Operating s
f6520 79 73 74 65 6d 20 69 73 20 57 69 6e 39 35 2e 0a  ystem is Win95..
f6530 2a 2a 20 32 3a 20 20 20 4f 70 65 72 61 74 69 6e  ** 2:   Operatin
f6540 67 20 73 79 73 74 65 6d 20 69 73 20 57 69 6e 4e  g system is WinN
f6550 54 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65  T..**.** In orde
f6560 72 20 74 6f 20 66 61 63 69 6c 69 74 61 74 65 20  r to facilitate 
f6570 74 65 73 74 69 6e 67 20 6f 6e 20 61 20 57 69 6e  testing on a Win
f6580 4e 54 20 73 79 73 74 65 6d 2c 20 74 68 65 20 74  NT system, the t
f6590 65 73 74 20 66 69 78 74 75 72 65 0a 2a 2a 20 63  est fixture.** c
f65a0 61 6e 20 6d 61 6e 75 61 6c 6c 79 20 73 65 74 20  an manually set 
f65b0 74 68 69 73 20 76 61 6c 75 65 20 74 6f 20 31 20  this value to 1 
f65c0 74 6f 20 65 6d 75 6c 61 74 65 20 57 69 6e 39 38  to emulate Win98
f65d0 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 23 69   behavior..*/.#i
f65e0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
f65f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
f6600 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 20  sqlite3_os_type 
f6610 3d 20 30 3b 0a 23 65 6c 73 65 0a 73 74 61 74 69  = 0;.#else.stati
f6620 63 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73  c int sqlite3_os
f6630 5f 74 79 70 65 20 3d 20 30 3b 0a 23 65 6e 64 69  _type = 0;.#endi
f6640 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  f../*.** Return 
f6650 74 72 75 65 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20  true (non-zero) 
f6660 69 66 20 77 65 20 61 72 65 20 72 75 6e 6e 69 6e  if we are runnin
f6670 67 20 75 6e 64 65 72 20 57 69 6e 4e 54 2c 20 57  g under WinNT, W
f6680 69 6e 32 4b 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20  in2K, WinXP,.** 
f6690 6f 72 20 57 69 6e 43 45 2e 20 20 52 65 74 75 72  or WinCE.  Retur
f66a0 6e 20 66 61 6c 73 65 20 28 7a 65 72 6f 29 20 66  n false (zero) f
f66b0 6f 72 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c  or Win95, Win98,
f66c0 20 6f 72 20 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a   or WinME..**.**
f66d0 20 48 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65   Here is an inte
f66e0 72 65 73 74 69 6e 67 20 6f 62 73 65 72 76 61 74  resting observat
f66f0 69 6f 6e 3a 20 20 57 69 6e 39 35 2c 20 57 69 6e  ion:  Win95, Win
f6700 39 38 2c 20 61 6e 64 20 57 69 6e 4d 45 20 6c 61  98, and WinME la
f6710 63 6b 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69  ck.** the LockFi
f6720 6c 65 45 78 28 29 20 41 50 49 2e 20 20 42 75 74  leEx() API.  But
f6730 20 77 65 20 63 61 6e 20 73 74 69 6c 6c 20 73 74   we can still st
f6740 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 20 61 67  atically link ag
f6750 61 69 6e 73 74 20 74 68 61 74 0a 2a 2a 20 41 50  ainst that.** AP
f6760 49 20 61 73 20 6c 6f 6e 67 20 61 73 20 77 65 20  I as long as we 
f6770 64 6f 6e 27 74 20 63 61 6c 6c 20 69 74 20 77 68  don't call it wh
f6780 65 6e 20 72 75 6e 6e 69 6e 67 20 57 69 6e 39 35  en running Win95
f6790 2f 39 38 2f 4d 45 2e 20 20 41 20 63 61 6c 6c 20  /98/ME.  A call 
f67a0 74 6f 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69  to.** this routi
f67b0 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 64 65  ne is used to de
f67c0 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20 68  termine if the h
f67d0 6f 73 74 20 69 73 20 57 69 6e 39 35 2f 39 38 2f  ost is Win95/98/
f67e0 4d 45 20 6f 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32  ME or.** WinNT/2
f67f0 4b 2f 58 50 20 73 6f 20 74 68 61 74 20 77 65 20  K/XP so that we 
f6800 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 74 68 65  will know whethe
f6810 72 20 6f 72 20 6e 6f 74 20 77 65 20 63 61 6e 20  r or not we can 
f6820 73 61 66 65 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74  safely call.** t
f6830 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20  he LockFileEx() 
f6840 41 50 49 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  API..*/.#if SQLI
f6850 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 20 64 65  TE_OS_WINCE.# de
f6860 66 69 6e 65 20 69 73 4e 54 28 29 20 20 28 31 29  fine isNT()  (1)
f6870 0a 23 65 6c 73 65 0a 20 20 73 74 61 74 69 63 20  .#else.  static 
f6880 69 6e 74 20 69 73 4e 54 28 76 6f 69 64 29 7b 0a  int isNT(void){.
f6890 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f      if( sqlite3_
f68a0 6f 73 5f 74 79 70 65 3d 3d 30 20 29 7b 0a 20 20  os_type==0 ){.  
f68b0 20 20 20 20 4f 53 56 45 52 53 49 4f 4e 49 4e 46      OSVERSIONINF
f68c0 4f 20 73 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73  O sInfo;.      s
f68d0 49 6e 66 6f 2e 64 77 4f 53 56 65 72 73 69 6f 6e  Info.dwOSVersion
f68e0 49 6e 66 6f 53 69 7a 65 20 3d 20 73 69 7a 65 6f  InfoSize = sizeo
f68f0 66 28 73 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  f(sInfo);.      
f6900 47 65 74 56 65 72 73 69 6f 6e 45 78 28 26 73 49  GetVersionEx(&sI
f6910 6e 66 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  nfo);.      sqli
f6920 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 73 49  te3_os_type = sI
f6930 6e 66 6f 2e 64 77 50 6c 61 74 66 6f 72 6d 49 64  nfo.dwPlatformId
f6940 3d 3d 56 45 52 5f 50 4c 41 54 46 4f 52 4d 5f 57  ==VER_PLATFORM_W
f6950 49 4e 33 32 5f 4e 54 20 3f 20 32 20 3a 20 31 3b  IN32_NT ? 2 : 1;
f6960 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
f6970 6e 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70  n sqlite3_os_typ
f6980 65 3d 3d 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  e==2;.  }.#endif
f6990 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49   /* SQLITE_OS_WI
f69a0 4e 43 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f  NCE */../*.** Co
f69b0 6e 76 65 72 74 20 61 20 55 54 46 2d 38 20 73 74  nvert a UTF-8 st
f69c0 72 69 6e 67 20 74 6f 20 6d 69 63 72 6f 73 6f 66  ring to microsof
f69d0 74 20 75 6e 69 63 6f 64 65 20 28 55 54 46 2d 31  t unicode (UTF-1
f69e0 36 3f 29 2e 20 0a 2a 2a 0a 2a 2a 20 53 70 61 63  6?). .**.** Spac
f69f0 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65  e to hold the re
f6a00 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73  turned string is
f6a10 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d   obtained from m
f6a20 61 6c 6c 6f 63 2e 0a 2a 2f 0a 73 74 61 74 69 63  alloc..*/.static
f6a30 20 57 43 48 41 52 20 2a 75 74 66 38 54 6f 55 6e   WCHAR *utf8ToUn
f6a40 69 63 6f 64 65 28 63 6f 6e 73 74 20 63 68 61 72  icode(const char
f6a50 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20   *zFilename){.  
f6a60 69 6e 74 20 6e 43 68 61 72 3b 0a 20 20 57 43 48  int nChar;.  WCH
f6a70 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d  AR *zWideFilenam
f6a80 65 3b 0a 0a 20 20 6e 43 68 61 72 20 3d 20 4d 75  e;..  nChar = Mu
f6a90 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68 61  ltiByteToWideCha
f6aa0 72 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a 46  r(CP_UTF8, 0, zF
f6ab0 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c  ilename, -1, NUL
f6ac0 4c 2c 20 30 29 3b 0a 20 20 7a 57 69 64 65 46 69  L, 0);.  zWideFi
f6ad0 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28  lename = malloc(
f6ae0 20 6e 43 68 61 72 2a 73 69 7a 65 6f 66 28 7a 57   nChar*sizeof(zW
f6af0 69 64 65 46 69 6c 65 6e 61 6d 65 5b 30 5d 29 20  ideFilename[0]) 
f6b00 29 3b 0a 20 20 69 66 28 20 7a 57 69 64 65 46 69  );.  if( zWideFi
f6b10 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20  lename==0 ){.   
f6b20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
f6b30 20 6e 43 68 61 72 20 3d 20 4d 75 6c 74 69 42 79   nChar = MultiBy
f6b40 74 65 54 6f 57 69 64 65 43 68 61 72 28 43 50 5f  teToWideChar(CP_
f6b50 55 54 46 38 2c 20 30 2c 20 7a 46 69 6c 65 6e 61  UTF8, 0, zFilena
f6b60 6d 65 2c 20 2d 31 2c 20 7a 57 69 64 65 46 69 6c  me, -1, zWideFil
f6b70 65 6e 61 6d 65 2c 20 6e 43 68 61 72 29 3b 0a 20  ename, nChar);. 
f6b80 20 69 66 28 20 6e 43 68 61 72 3d 3d 30 20 29 7b   if( nChar==0 ){
f6b90 0a 20 20 20 20 66 72 65 65 28 7a 57 69 64 65 46  .    free(zWideF
f6ba0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 57  ilename);.    zW
f6bb0 69 64 65 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b  ideFilename = 0;
f6bc0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 57  .  }.  return zW
f6bd0 69 64 65 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a  ideFilename;.}..
f6be0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d 69  /*.** Convert mi
f6bf0 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20  crosoft unicode 
f6c00 74 6f 20 55 54 46 2d 38 2e 20 20 53 70 61 63 65  to UTF-8.  Space
f6c10 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74   to hold the ret
f6c20 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 0a  urned string is.
f6c30 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ** obtained from
f6c40 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74   malloc()..*/.st
f6c50 61 74 69 63 20 63 68 61 72 20 2a 75 6e 69 63 6f  atic char *unico
f6c60 64 65 54 6f 55 74 66 38 28 63 6f 6e 73 74 20 57  deToUtf8(const W
f6c70 43 48 41 52 20 2a 7a 57 69 64 65 46 69 6c 65 6e  CHAR *zWideFilen
f6c80 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74  ame){.  int nByt
f6c90 65 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65  e;.  char *zFile
f6ca0 6e 61 6d 65 3b 0a 0a 20 20 6e 42 79 74 65 20 3d  name;..  nByte =
f6cb0 20 57 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69   WideCharToMulti
f6cc0 42 79 74 65 28 43 50 5f 55 54 46 38 2c 20 30 2c  Byte(CP_UTF8, 0,
f6cd0 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20   zWideFilename, 
f6ce0 2d 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b  -1, 0, 0, 0, 0);
f6cf0 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 6d  .  zFilename = m
f6d00 61 6c 6c 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a  alloc( nByte );.
f6d10 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d    if( zFilename=
f6d20 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
f6d30 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20   0;.  }.  nByte 
f6d40 3d 20 57 69 64 65 43 68 61 72 54 6f 4d 75 6c 74  = WideCharToMult
f6d50 69 42 79 74 65 28 43 50 5f 55 54 46 38 2c 20 30  iByte(CP_UTF8, 0
f6d60 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c  , zWideFilename,
f6d70 20 2d 31 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20   -1, zFilename, 
f6d80 6e 42 79 74 65 2c 0a 20 20 20 20 20 20 20 20 20  nByte,.         
f6d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6da0 20 20 20 20 20 30 2c 20 30 29 3b 0a 20 20 69 66       0, 0);.  if
f6db0 28 20 6e 42 79 74 65 20 3d 3d 20 30 20 29 7b 0a  ( nByte == 0 ){.
f6dc0 20 20 20 20 66 72 65 65 28 7a 46 69 6c 65 6e 61      free(zFilena
f6dd0 6d 65 29 3b 0a 20 20 20 20 7a 46 69 6c 65 6e 61  me);.    zFilena
f6de0 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  me = 0;.  }.  re
f6df0 74 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a  turn zFilename;.
f6e00 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  }../*.** Convert
f6e10 20 61 6e 20 61 6e 73 69 20 73 74 72 69 6e 67 20   an ansi string 
f6e20 74 6f 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69  to microsoft uni
f6e30 63 6f 64 65 2c 20 62 61 73 65 64 20 6f 6e 20 74  code, based on t
f6e40 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 63 6f  he.** current co
f6e50 64 65 70 61 67 65 20 73 65 74 74 69 6e 67 73 20  depage settings 
f6e60 66 6f 72 20 66 69 6c 65 20 61 70 69 73 2e 0a 2a  for file apis..*
f6e70 2a 20 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68  * .** Space to h
f6e80 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64  old the returned
f6e90 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69   string is obtai
f6ea0 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c  ned.** from mall
f6eb0 6f 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 43  oc..*/.static WC
f6ec0 48 41 52 20 2a 6d 62 63 73 54 6f 55 6e 69 63 6f  HAR *mbcsToUnico
f6ed0 64 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  de(const char *z
f6ee0 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74  Filename){.  int
f6ef0 20 6e 42 79 74 65 3b 0a 20 20 57 43 48 41 52 20   nByte;.  WCHAR 
f6f00 2a 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3b 0a  *zMbcsFilename;.
f6f10 20 20 69 6e 74 20 63 6f 64 65 70 61 67 65 20 3d    int codepage =
f6f20 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49   AreFileApisANSI
f6f30 28 29 20 3f 20 43 50 5f 41 43 50 20 3a 20 43 50  () ? CP_ACP : CP
f6f40 5f 4f 45 4d 43 50 3b 0a 0a 20 20 6e 42 79 74 65  _OEMCP;..  nByte
f6f50 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 69   = MultiByteToWi
f6f60 64 65 43 68 61 72 28 63 6f 64 65 70 61 67 65 2c  deChar(codepage,
f6f70 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d   0, zFilename, -
f6f80 31 2c 20 4e 55 4c 4c 2c 30 29 2a 73 69 7a 65 6f  1, NULL,0)*sizeo
f6f90 66 28 57 43 48 41 52 29 3b 0a 20 20 7a 4d 62 63  f(WCHAR);.  zMbc
f6fa0 73 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c  sFilename = mall
f6fb0 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66  oc( nByte*sizeof
f6fc0 28 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 5b 30  (zMbcsFilename[0
f6fd0 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a 4d 62 63  ]) );.  if( zMbc
f6fe0 73 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a  sFilename==0 ){.
f6ff0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
f7000 7d 0a 20 20 6e 42 79 74 65 20 3d 20 4d 75 6c 74  }.  nByte = Mult
f7010 69 42 79 74 65 54 6f 57 69 64 65 43 68 61 72 28  iByteToWideChar(
f7020 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 46 69  codepage, 0, zFi
f7030 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 4d 62 63  lename, -1, zMbc
f7040 73 46 69 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65  sFilename, nByte
f7050 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3d 3d  );.  if( nByte==
f7060 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 4d  0 ){.    free(zM
f7070 62 63 73 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  bcsFilename);.  
f7080 20 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 20    zMbcsFilename 
f7090 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
f70a0 6e 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3b  n zMbcsFilename;
f70b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
f70c0 74 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69 63  t microsoft unic
f70d0 6f 64 65 20 74 6f 20 6d 75 6c 74 69 62 79 74 65  ode to multibyte
f70e0 20 63 68 61 72 61 63 74 65 72 20 73 74 72 69 6e   character strin
f70f0 67 2c 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a  g, based on the.
f7100 2a 2a 20 75 73 65 72 27 73 20 41 6e 73 69 20 63  ** user's Ansi c
f7110 6f 64 65 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53  odepage..**.** S
f7120 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65  pace to hold the
f7130 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67   returned string
f7140 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f   is obtained fro
f7150 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a  m.** malloc()..*
f7160 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 75  /.static char *u
f7170 6e 69 63 6f 64 65 54 6f 4d 62 63 73 28 63 6f 6e  nicodeToMbcs(con
f7180 73 74 20 57 43 48 41 52 20 2a 7a 57 69 64 65 46  st WCHAR *zWideF
f7190 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e 74 20  ilename){.  int 
f71a0 6e 42 79 74 65 3b 0a 20 20 63 68 61 72 20 2a 7a  nByte;.  char *z
f71b0 46 69 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 20  Filename;.  int 
f71c0 63 6f 64 65 70 61 67 65 20 3d 20 41 72 65 46 69  codepage = AreFi
f71d0 6c 65 41 70 69 73 41 4e 53 49 28 29 20 3f 20 43  leApisANSI() ? C
f71e0 50 5f 41 43 50 20 3a 20 43 50 5f 4f 45 4d 43 50  P_ACP : CP_OEMCP
f71f0 3b 0a 0a 20 20 6e 42 79 74 65 20 3d 20 57 69 64  ;..  nByte = Wid
f7200 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79 74 65  eCharToMultiByte
f7210 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 57  (codepage, 0, zW
f7220 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c  ideFilename, -1,
f7230 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20   0, 0, 0, 0);.  
f7240 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 6d 61 6c 6c  zFilename = mall
f7250 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a 20 20 69  oc( nByte );.  i
f7260 66 28 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20  f( zFilename==0 
f7270 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
f7280 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20 57  .  }.  nByte = W
f7290 69 64 65 43 68 61 72 54 6f 4d 75 6c 74 69 42 79  ideCharToMultiBy
f72a0 74 65 28 63 6f 64 65 70 61 67 65 2c 20 30 2c 20  te(codepage, 0, 
f72b0 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 2d  zWideFilename, -
f72c0 31 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 42  1, zFilename, nB
f72d0 79 74 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  yte,.           
f72e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f72f0 20 20 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20     0, 0);.  if( 
f7300 6e 42 79 74 65 20 3d 3d 20 30 20 29 7b 0a 20 20  nByte == 0 ){.  
f7310 20 20 66 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65    free(zFilename
f7320 29 3b 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65  );.    zFilename
f7330 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
f7340 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a  rn zFilename;.}.
f7350 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 6d  ./*.** Convert m
f7360 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 74  ultibyte charact
f7370 65 72 20 73 74 72 69 6e 67 20 74 6f 20 55 54 46  er string to UTF
f7380 2d 38 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f  -8.  Space to ho
f7390 6c 64 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e  ld the.** return
f73a0 65 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74  ed string is obt
f73b0 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f  ained from mallo
f73c0 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  c()..*/.SQLITE_A
f73d0 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  PI char *sqlite3
f73e0 5f 77 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75  _win32_mbcs_to_u
f73f0 74 66 38 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  tf8(const char *
f7400 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 63 68  zFilename){.  ch
f7410 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 55 74 66  ar *zFilenameUtf
f7420 38 3b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6d 70  8;.  WCHAR *zTmp
f7430 57 69 64 65 3b 0a 0a 20 20 7a 54 6d 70 57 69 64  Wide;..  zTmpWid
f7440 65 20 3d 20 6d 62 63 73 54 6f 55 6e 69 63 6f 64  e = mbcsToUnicod
f7450 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  e(zFilename);.  
f7460 69 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d 30 20  if( zTmpWide==0 
f7470 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
f7480 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d 65  .  }.  zFilename
f7490 55 74 66 38 20 3d 20 75 6e 69 63 6f 64 65 54 6f  Utf8 = unicodeTo
f74a0 55 74 66 38 28 7a 54 6d 70 57 69 64 65 29 3b 0a  Utf8(zTmpWide);.
f74b0 20 20 66 72 65 65 28 7a 54 6d 70 57 69 64 65 29    free(zTmpWide)
f74c0 3b 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65  ;.  return zFile
f74d0 6e 61 6d 65 55 74 66 38 3b 0a 7d 0a 0a 2f 2a 0a  nameUtf8;.}../*.
f74e0 2a 2a 20 43 6f 6e 76 65 72 74 20 55 54 46 2d 38  ** Convert UTF-8
f74f0 20 74 6f 20 6d 75 6c 74 69 62 79 74 65 20 63 68   to multibyte ch
f7500 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 2e 20  aracter string. 
f7510 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
f7520 68 65 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  he .** returned 
f7530 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e  string is obtain
f7540 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29  ed from malloc()
f7550 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72  ..*/.static char
f7560 20 2a 75 74 66 38 54 6f 4d 62 63 73 28 63 6f 6e   *utf8ToMbcs(con
f7570 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
f7580 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69  me){.  char *zFi
f7590 6c 65 6e 61 6d 65 4d 62 63 73 3b 0a 20 20 57 43  lenameMbcs;.  WC
f75a0 48 41 52 20 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a  HAR *zTmpWide;..
f75b0 20 20 7a 54 6d 70 57 69 64 65 20 3d 20 75 74 66    zTmpWide = utf
f75c0 38 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65  8ToUnicode(zFile
f75d0 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d  name);.  if( zTm
f75e0 70 57 69 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20  pWide==0 ){.    
f75f0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
f7600 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 73 20 3d 20  zFilenameMbcs = 
f7610 75 6e 69 63 6f 64 65 54 6f 4d 62 63 73 28 7a 54  unicodeToMbcs(zT
f7620 6d 70 57 69 64 65 29 3b 0a 20 20 66 72 65 65 28  mpWide);.  free(
f7630 7a 54 6d 70 57 69 64 65 29 3b 0a 20 20 72 65 74  zTmpWide);.  ret
f7640 75 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 4d 62 63  urn zFilenameMbc
f7650 73 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45  s;.}..#if SQLITE
f7660 5f 4f 53 5f 57 49 4e 43 45 0a 2f 2a 2a 2a 2a 2a  _OS_WINCE./*****
f7670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f76a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f76b0 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 65 63  ****.** This sec
f76c0 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 63 6f  tion contains co
f76d0 64 65 20 66 6f 72 20 57 69 6e 43 45 20 6f 6e 6c  de for WinCE onl
f76e0 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 57 69 6e 64  y..*/./*.** Wind
f76f0 6f 77 73 43 45 20 64 6f 65 73 20 6e 6f 74 20 68  owsCE does not h
f7700 61 76 65 20 61 20 6c 6f 63 61 6c 74 69 6d 65 28  ave a localtime(
f7710 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 6f 20  ) function.  So 
f7720 63 72 65 61 74 65 20 61 0a 2a 2a 20 73 75 62 73  create a.** subs
f7730 74 69 74 75 74 65 2e 0a 2a 2f 0a 73 74 72 75 63  titute..*/.struc
f7740 74 20 74 6d 20 2a 5f 5f 63 64 65 63 6c 20 6c 6f  t tm *__cdecl lo
f7750 63 61 6c 74 69 6d 65 28 63 6f 6e 73 74 20 74 69  caltime(const ti
f7760 6d 65 5f 74 20 2a 74 29 0a 7b 0a 20 20 73 74 61  me_t *t).{.  sta
f7770 74 69 63 20 73 74 72 75 63 74 20 74 6d 20 79 3b  tic struct tm y;
f7780 0a 20 20 46 49 4c 45 54 49 4d 45 20 75 54 6d 2c  .  FILETIME uTm,
f7790 20 6c 54 6d 3b 0a 20 20 53 59 53 54 45 4d 54 49   lTm;.  SYSTEMTI
f77a0 4d 45 20 70 54 6d 3b 0a 20 20 73 71 6c 69 74 65  ME pTm;.  sqlite
f77b0 33 5f 69 6e 74 36 34 20 74 36 34 3b 0a 20 20 74  3_int64 t64;.  t
f77c0 36 34 20 3d 20 2a 74 3b 0a 20 20 74 36 34 20 3d  64 = *t;.  t64 =
f77d0 20 28 74 36 34 20 2b 20 31 31 36 34 34 34 37 33   (t64 + 11644473
f77e0 36 30 30 29 2a 31 30 30 30 30 30 30 30 3b 0a 20  600)*10000000;. 
f77f0 20 75 54 6d 2e 64 77 4c 6f 77 44 61 74 65 54 69   uTm.dwLowDateTi
f7800 6d 65 20 3d 20 28 44 57 4f 52 44 29 28 74 36 34  me = (DWORD)(t64
f7810 20 26 20 30 78 46 46 46 46 46 46 46 46 29 3b 0a   & 0xFFFFFFFF);.
f7820 20 20 75 54 6d 2e 64 77 48 69 67 68 44 61 74 65    uTm.dwHighDate
f7830 54 69 6d 65 3d 20 28 44 57 4f 52 44 29 28 74 36  Time= (DWORD)(t6
f7840 34 20 3e 3e 20 33 32 29 3b 0a 20 20 46 69 6c 65  4 >> 32);.  File
f7850 54 69 6d 65 54 6f 4c 6f 63 61 6c 46 69 6c 65 54  TimeToLocalFileT
f7860 69 6d 65 28 26 75 54 6d 2c 26 6c 54 6d 29 3b 0a  ime(&uTm,&lTm);.
f7870 20 20 46 69 6c 65 54 69 6d 65 54 6f 53 79 73 74    FileTimeToSyst
f7880 65 6d 54 69 6d 65 28 26 6c 54 6d 2c 26 70 54 6d  emTime(&lTm,&pTm
f7890 29 3b 0a 20 20 79 2e 74 6d 5f 79 65 61 72 20 3d  );.  y.tm_year =
f78a0 20 70 54 6d 2e 77 59 65 61 72 20 2d 20 31 39 30   pTm.wYear - 190
f78b0 30 3b 0a 20 20 79 2e 74 6d 5f 6d 6f 6e 20 3d 20  0;.  y.tm_mon = 
f78c0 70 54 6d 2e 77 4d 6f 6e 74 68 20 2d 20 31 3b 0a  pTm.wMonth - 1;.
f78d0 20 20 79 2e 74 6d 5f 77 64 61 79 20 3d 20 70 54    y.tm_wday = pT
f78e0 6d 2e 77 44 61 79 4f 66 57 65 65 6b 3b 0a 20 20  m.wDayOfWeek;.  
f78f0 79 2e 74 6d 5f 6d 64 61 79 20 3d 20 70 54 6d 2e  y.tm_mday = pTm.
f7900 77 44 61 79 3b 0a 20 20 79 2e 74 6d 5f 68 6f 75  wDay;.  y.tm_hou
f7910 72 20 3d 20 70 54 6d 2e 77 48 6f 75 72 3b 0a 20  r = pTm.wHour;. 
f7920 20 79 2e 74 6d 5f 6d 69 6e 20 3d 20 70 54 6d 2e   y.tm_min = pTm.
f7930 77 4d 69 6e 75 74 65 3b 0a 20 20 79 2e 74 6d 5f  wMinute;.  y.tm_
f7940 73 65 63 20 3d 20 70 54 6d 2e 77 53 65 63 6f 6e  sec = pTm.wSecon
f7950 64 3b 0a 20 20 72 65 74 75 72 6e 20 26 79 3b 0a  d;.  return &y;.
f7960 7d 0a 0a 2f 2a 20 54 68 69 73 20 77 69 6c 6c 20  }../* This will 
f7970 6e 65 76 65 72 20 62 65 20 63 61 6c 6c 65 64 2c  never be called,
f7980 20 62 75 74 20 64 65 66 69 6e 65 64 20 74 6f 20   but defined to 
f7990 6d 61 6b 65 20 74 68 65 20 63 6f 64 65 20 63 6f  make the code co
f79a0 6d 70 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65  mpile */.#define
f79b0 20 47 65 74 54 65 6d 70 50 61 74 68 41 28 61 2c   GetTempPathA(a,
f79c0 62 29 0a 0a 23 64 65 66 69 6e 65 20 4c 6f 63 6b  b)..#define Lock
f79d0 46 69 6c 65 28 61 2c 62 2c 63 2c 64 2c 65 29 20  File(a,b,c,d,e) 
f79e0 20 20 20 20 20 20 77 69 6e 63 65 4c 6f 63 6b 46        winceLockF
f79f0 69 6c 65 28 26 61 2c 20 62 2c 20 63 2c 20 64 2c  ile(&a, b, c, d,
f7a00 20 65 29 0a 23 64 65 66 69 6e 65 20 55 6e 6c 6f   e).#define Unlo
f7a10 63 6b 46 69 6c 65 28 61 2c 62 2c 63 2c 64 2c 65  ckFile(a,b,c,d,e
f7a20 29 20 20 20 20 20 77 69 6e 63 65 55 6e 6c 6f 63  )     winceUnloc
f7a30 6b 46 69 6c 65 28 26 61 2c 20 62 2c 20 63 2c 20  kFile(&a, b, c, 
f7a40 64 2c 20 65 29 0a 23 64 65 66 69 6e 65 20 4c 6f  d, e).#define Lo
f7a50 63 6b 46 69 6c 65 45 78 28 61 2c 62 2c 63 2c 64  ckFileEx(a,b,c,d
f7a60 2c 65 2c 66 29 20 20 20 77 69 6e 63 65 4c 6f 63  ,e,f)   winceLoc
f7a70 6b 46 69 6c 65 45 78 28 26 61 2c 20 62 2c 20 63  kFileEx(&a, b, c
f7a80 2c 20 64 2c 20 65 2c 20 66 29 0a 0a 23 64 65 66  , d, e, f)..#def
f7a90 69 6e 65 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49  ine HANDLE_TO_WI
f7aa0 4e 46 49 4c 45 28 61 29 20 28 77 69 6e 46 69 6c  NFILE(a) (winFil
f7ab0 65 2a 29 26 28 28 63 68 61 72 2a 29 61 29 5b 2d  e*)&((char*)a)[-
f7ac0 28 69 6e 74 29 6f 66 66 73 65 74 6f 66 28 77 69  (int)offsetof(wi
f7ad0 6e 46 69 6c 65 2c 68 29 5d 0a 0a 2f 2a 0a 2a 2a  nFile,h)]../*.**
f7ae0 20 41 63 71 75 69 72 65 20 61 20 6c 6f 63 6b 20   Acquire a lock 
f7af0 6f 6e 20 74 68 65 20 68 61 6e 64 6c 65 20 68 0a  on the handle h.
f7b00 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
f7b10 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65  inceMutexAcquire
f7b20 28 48 41 4e 44 4c 45 20 68 29 7b 0a 20 20 20 44  (HANDLE h){.   D
f7b30 57 4f 52 44 20 64 77 45 72 72 3b 0a 20 20 20 64  WORD dwErr;.   d
f7b40 6f 20 7b 0a 20 20 20 20 20 64 77 45 72 72 20 3d  o {.     dwErr =
f7b50 20 57 61 69 74 46 6f 72 53 69 6e 67 6c 65 4f 62   WaitForSingleOb
f7b60 6a 65 63 74 28 68 2c 20 49 4e 46 49 4e 49 54 45  ject(h, INFINITE
f7b70 29 3b 0a 20 20 20 7d 20 77 68 69 6c 65 20 28 64  );.   } while (d
f7b80 77 45 72 72 20 21 3d 20 57 41 49 54 5f 4f 42 4a  wErr != WAIT_OBJ
f7b90 45 43 54 5f 30 20 26 26 20 64 77 45 72 72 20 21  ECT_0 && dwErr !
f7ba0 3d 20 57 41 49 54 5f 41 42 41 4e 44 4f 4e 45 44  = WAIT_ABANDONED
f7bb0 29 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61  );.}./*.** Relea
f7bc0 73 65 20 61 20 6c 6f 63 6b 20 61 63 71 75 69 72  se a lock acquir
f7bd0 65 64 20 62 79 20 77 69 6e 63 65 4d 75 74 65 78  ed by winceMutex
f7be0 41 63 71 75 69 72 65 28 29 0a 2a 2f 0a 23 64 65  Acquire().*/.#de
f7bf0 66 69 6e 65 20 77 69 6e 63 65 4d 75 74 65 78 52  fine winceMutexR
f7c00 65 6c 65 61 73 65 28 68 29 20 52 65 6c 65 61 73  elease(h) Releas
f7c10 65 4d 75 74 65 78 28 68 29 0a 0a 2f 2a 0a 2a 2a  eMutex(h)../*.**
f7c20 20 43 72 65 61 74 65 20 74 68 65 20 6d 75 74 65   Create the mute
f7c30 78 20 61 6e 64 20 73 68 61 72 65 64 20 6d 65 6d  x and shared mem
f7c40 6f 72 79 20 75 73 65 64 20 66 6f 72 20 6c 6f 63  ory used for loc
f7c50 6b 69 6e 67 20 69 6e 20 74 68 65 20 66 69 6c 65  king in the file
f7c60 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 70  .** descriptor p
f7c70 46 69 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42  File.*/.static B
f7c80 4f 4f 4c 20 77 69 6e 63 65 43 72 65 61 74 65 4c  OOL winceCreateL
f7c90 6f 63 6b 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ock(const char *
f7ca0 7a 46 69 6c 65 6e 61 6d 65 2c 20 77 69 6e 46 69  zFilename, winFi
f7cb0 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 57 43  le *pFile){.  WC
f7cc0 48 41 52 20 2a 7a 54 6f 6b 3b 0a 20 20 57 43 48  HAR *zTok;.  WCH
f7cd0 41 52 20 2a 7a 4e 61 6d 65 20 3d 20 75 74 66 38  AR *zName = utf8
f7ce0 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e  ToUnicode(zFilen
f7cf0 61 6d 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 49 6e  ame);.  BOOL bIn
f7d00 69 74 20 3d 20 54 52 55 45 3b 0a 0a 20 20 2f 2a  it = TRUE;..  /*
f7d10 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   Initialize the 
f7d20 6c 6f 63 61 6c 20 6c 6f 63 6b 64 61 74 61 20 2a  local lockdata *
f7d30 2f 0a 20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 26  /.  ZeroMemory(&
f7d40 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2c 20 73 69  pFile->local, si
f7d50 7a 65 6f 66 28 70 46 69 6c 65 2d 3e 6c 6f 63 61  zeof(pFile->loca
f7d60 6c 29 29 3b 0a 0a 20 20 2f 2a 20 52 65 70 6c 61  l));..  /* Repla
f7d70 63 65 20 74 68 65 20 62 61 63 6b 73 6c 61 73 68  ce the backslash
f7d80 65 73 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65  es from the file
f7d90 6e 61 6d 65 20 61 6e 64 20 6c 6f 77 65 72 63 61  name and lowerca
f7da0 73 65 20 69 74 0a 20 20 2a 2a 20 74 6f 20 64 65  se it.  ** to de
f7db0 72 69 76 65 20 61 20 6d 75 74 65 78 20 6e 61 6d  rive a mutex nam
f7dc0 65 2e 20 2a 2f 0a 20 20 7a 54 6f 6b 20 3d 20 43  e. */.  zTok = C
f7dd0 68 61 72 4c 6f 77 65 72 57 28 7a 4e 61 6d 65 29  harLowerW(zName)
f7de0 3b 0a 20 20 66 6f 72 20 28 3b 2a 7a 54 6f 6b 3b  ;.  for (;*zTok;
f7df0 7a 54 6f 6b 2b 2b 29 7b 0a 20 20 20 20 69 66 20  zTok++){.    if 
f7e00 28 2a 7a 54 6f 6b 20 3d 3d 20 27 5c 5c 27 29 20  (*zTok == '\\') 
f7e10 2a 7a 54 6f 6b 20 3d 20 27 5f 27 3b 0a 20 20 7d  *zTok = '_';.  }
f7e20 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 2f 6f 70  ..  /* Create/op
f7e30 65 6e 20 74 68 65 20 6e 61 6d 65 64 20 6d 75 74  en the named mut
f7e40 65 78 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 68  ex */.  pFile->h
f7e50 4d 75 74 65 78 20 3d 20 43 72 65 61 74 65 4d 75  Mutex = CreateMu
f7e60 74 65 78 57 28 4e 55 4c 4c 2c 20 46 41 4c 53 45  texW(NULL, FALSE
f7e70 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 20 28  , zName);.  if (
f7e80 21 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 7b  !pFile->hMutex){
f7e90 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74  .    pFile->last
f7ea0 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45  Errno = GetLastE
f7eb0 72 72 6f 72 28 29 3b 0a 20 20 20 20 66 72 65 65  rror();.    free
f7ec0 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74  (zName);.    ret
f7ed0 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a  urn FALSE;.  }..
f7ee0 20 20 2f 2a 20 41 63 71 75 69 72 65 20 74 68 65    /* Acquire the
f7ef0 20 6d 75 74 65 78 20 62 65 66 6f 72 65 20 63 6f   mutex before co
f7f00 6e 74 69 6e 75 69 6e 67 20 2a 2f 0a 20 20 77 69  ntinuing */.  wi
f7f10 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28  nceMutexAcquire(
f7f20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a  pFile->hMutex);.
f7f30 20 20 0a 20 20 2f 2a 20 53 69 6e 63 65 20 74 68    .  /* Since th
f7f40 65 20 6e 61 6d 65 73 20 6f 66 20 6e 61 6d 65 64  e names of named
f7f50 20 6d 75 74 65 78 65 73 2c 20 73 65 6d 61 70 68   mutexes, semaph
f7f60 6f 72 65 73 2c 20 66 69 6c 65 20 6d 61 70 70 69  ores, file mappi
f7f70 6e 67 73 20 65 74 63 20 61 72 65 20 0a 20 20 2a  ngs etc are .  *
f7f80 2a 20 63 61 73 65 2d 73 65 6e 73 69 74 69 76 65  * case-sensitive
f7f90 2c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65  , take advantage
f7fa0 20 6f 66 20 74 68 61 74 20 62 79 20 75 70 70 65   of that by uppe
f7fb0 72 63 61 73 69 6e 67 20 74 68 65 20 6d 75 74 65  rcasing the mute
f7fc0 78 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 6e 64 20  x name.  ** and 
f7fd0 75 73 69 6e 67 20 74 68 61 74 20 61 73 20 74 68  using that as th
f7fe0 65 20 73 68 61 72 65 64 20 66 69 6c 65 6d 61 70  e shared filemap
f7ff0 70 69 6e 67 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a  ping name..  */.
f8000 20 20 43 68 61 72 55 70 70 65 72 57 28 7a 4e 61    CharUpperW(zNa
f8010 6d 65 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 53  me);.  pFile->hS
f8020 68 61 72 65 64 20 3d 20 43 72 65 61 74 65 46 69  hared = CreateFi
f8030 6c 65 4d 61 70 70 69 6e 67 57 28 49 4e 56 41 4c  leMappingW(INVAL
f8040 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c 55 45 2c  ID_HANDLE_VALUE,
f8050 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20   NULL,.         
f8060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41                PA
f8080 47 45 5f 52 45 41 44 57 52 49 54 45 2c 20 30 2c  GE_READWRITE, 0,
f8090 20 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63   sizeof(winceLoc
f80a0 6b 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  k),.            
f80b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f80c0 20 20 20 20 20 20 20 20 20 20 20 7a 4e 61 6d 65             zName
f80d0 29 3b 20 20 0a 0a 20 20 2f 2a 20 53 65 74 20 61  );  ..  /* Set a
f80e0 20 66 6c 61 67 20 74 68 61 74 20 69 6e 64 69 63   flag that indic
f80f0 61 74 65 73 20 77 65 27 72 65 20 74 68 65 20 66  ates we're the f
f8100 69 72 73 74 20 74 6f 20 63 72 65 61 74 65 20 74  irst to create t
f8110 68 65 20 6d 65 6d 6f 72 79 20 73 6f 20 69 74 20  he memory so it 
f8120 0a 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 7a 65  .  ** must be ze
f8130 72 6f 2d 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a  ro-initialized *
f8140 2f 0a 20 20 69 66 20 28 47 65 74 4c 61 73 74 45  /.  if (GetLastE
f8150 72 72 6f 72 28 29 20 3d 3d 20 45 52 52 4f 52 5f  rror() == ERROR_
f8160 41 4c 52 45 41 44 59 5f 45 58 49 53 54 53 29 7b  ALREADY_EXISTS){
f8170 0a 20 20 20 20 62 49 6e 69 74 20 3d 20 46 41 4c  .    bInit = FAL
f8180 53 45 3b 0a 20 20 7d 0a 0a 20 20 66 72 65 65 28  SE;.  }..  free(
f8190 7a 4e 61 6d 65 29 3b 0a 0a 20 20 2f 2a 20 49 66  zName);..  /* If
f81a0 20 77 65 20 73 75 63 63 65 65 64 65 64 20 69 6e   we succeeded in
f81b0 20 6d 61 6b 69 6e 67 20 74 68 65 20 73 68 61 72   making the shar
f81c0 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65  ed memory handle
f81d0 2c 20 6d 61 70 20 69 74 2e 20 2a 2f 0a 20 20 69  , map it. */.  i
f81e0 66 20 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65  f (pFile->hShare
f81f0 64 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 73  d){.    pFile->s
f8200 68 61 72 65 64 20 3d 20 28 77 69 6e 63 65 4c 6f  hared = (winceLo
f8210 63 6b 2a 29 4d 61 70 56 69 65 77 4f 66 46 69 6c  ck*)MapViewOfFil
f8220 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64  e(pFile->hShared
f8230 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
f8240 46 49 4c 45 5f 4d 41 50 5f 52 45 41 44 7c 46 49  FILE_MAP_READ|FI
f8250 4c 45 5f 4d 41 50 5f 57 52 49 54 45 2c 20 30 2c  LE_MAP_WRITE, 0,
f8260 20 30 2c 20 73 69 7a 65 6f 66 28 77 69 6e 63 65   0, sizeof(wince
f8270 4c 6f 63 6b 29 29 3b 0a 20 20 20 20 2f 2a 20 49  Lock));.    /* I
f8280 66 20 6d 61 70 70 69 6e 67 20 66 61 69 6c 65 64  f mapping failed
f8290 2c 20 63 6c 6f 73 65 20 74 68 65 20 73 68 61 72  , close the shar
f82a0 65 64 20 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65  ed memory handle
f82b0 20 61 6e 64 20 65 72 61 73 65 20 69 74 20 2a 2f   and erase it */
f82c0 0a 20 20 20 20 69 66 20 28 21 70 46 69 6c 65 2d  .    if (!pFile-
f82d0 3e 73 68 61 72 65 64 29 7b 0a 20 20 20 20 20 20  >shared){.      
f82e0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
f82f0 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28   = GetLastError(
f8300 29 3b 0a 20 20 20 20 20 20 43 6c 6f 73 65 48 61  );.      CloseHa
f8310 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 61  ndle(pFile->hSha
f8320 72 65 64 29 3b 0a 20 20 20 20 20 20 70 46 69 6c  red);.      pFil
f8330 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 4e 55 4c  e->hShared = NUL
f8340 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  L;.    }.  }..  
f8350 2f 2a 20 49 66 20 73 68 61 72 65 64 20 6d 65 6d  /* If shared mem
f8360 6f 72 79 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65  ory could not be
f8370 20 63 72 65 61 74 65 64 2c 20 74 68 65 6e 20 63   created, then c
f8380 6c 6f 73 65 20 74 68 65 20 6d 75 74 65 78 20 61  lose the mutex a
f8390 6e 64 20 66 61 69 6c 20 2a 2f 0a 20 20 69 66 20  nd fail */.  if 
f83a0 28 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20  (pFile->hShared 
f83b0 3d 3d 20 4e 55 4c 4c 29 7b 0a 20 20 20 20 77 69  == NULL){.    wi
f83c0 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28  nceMutexRelease(
f83d0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a  pFile->hMutex);.
f83e0 20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28      CloseHandle(
f83f0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a  pFile->hMutex);.
f8400 20 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65      pFile->hMute
f8410 78 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72 65  x = NULL;.    re
f8420 74 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20 7d 0a  turn FALSE;.  }.
f8430 20 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69    .  /* Initiali
f8440 7a 65 20 74 68 65 20 73 68 61 72 65 64 20 6d 65  ze the shared me
f8450 6d 6f 72 79 20 69 66 20 77 65 27 72 65 20 73 75  mory if we're su
f8460 70 70 6f 73 65 64 20 74 6f 20 2a 2f 0a 20 20 69  pposed to */.  i
f8470 66 20 28 62 49 6e 69 74 29 20 7b 0a 20 20 20 20  f (bInit) {.    
f8480 5a 65 72 6f 4d 65 6d 6f 72 79 28 70 46 69 6c 65  ZeroMemory(pFile
f8490 2d 3e 73 68 61 72 65 64 2c 20 73 69 7a 65 6f 66  ->shared, sizeof
f84a0 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20 20  (winceLock));.  
f84b0 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52  }..  winceMutexR
f84c0 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d  elease(pFile->hM
f84d0 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
f84e0 54 52 55 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  TRUE;.}../*.** D
f84f0 65 73 74 72 6f 79 20 74 68 65 20 70 61 72 74 20  estroy the part 
f8500 6f 66 20 77 69 6e 46 69 6c 65 20 74 68 61 74 20  of winFile that 
f8510 64 65 61 6c 73 20 77 69 74 68 20 77 69 6e 63 65  deals with wince
f8520 20 6c 6f 63 6b 73 0a 2a 2f 0a 73 74 61 74 69 63   locks.*/.static
f8530 20 76 6f 69 64 20 77 69 6e 63 65 44 65 73 74 72   void winceDestr
f8540 6f 79 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a  oyLock(winFile *
f8550 70 46 69 6c 65 29 7b 0a 20 20 69 66 20 28 70 46  pFile){.  if (pF
f8560 69 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20  ile->hMutex){.  
f8570 20 20 2f 2a 20 41 63 71 75 69 72 65 20 74 68 65    /* Acquire the
f8580 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 77 69   mutex */.    wi
f8590 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28  nceMutexAcquire(
f85a0 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a  pFile->hMutex);.
f85b0 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  .    /* The foll
f85c0 6f 77 69 6e 67 20 62 6c 6f 63 6b 73 20 73 68 6f  owing blocks sho
f85d0 75 6c 64 20 70 72 6f 62 61 62 6c 79 20 61 73 73  uld probably ass
f85e0 65 72 74 20 69 6e 20 64 65 62 75 67 20 6d 6f 64  ert in debug mod
f85f0 65 2c 20 62 75 74 20 74 68 65 79 0a 20 20 20 20  e, but they.    
f8600 20 20 20 61 72 65 20 74 6f 20 63 6c 65 61 6e 75     are to cleanu
f8610 70 20 69 6e 20 63 61 73 65 20 61 6e 79 20 6c 6f  p in case any lo
f8620 63 6b 73 20 72 65 6d 61 69 6e 65 64 20 6f 70 65  cks remained ope
f8630 6e 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 69  n */.    if (pFi
f8640 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65  le->local.nReade
f8650 72 73 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65  rs){.      pFile
f8660 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65  ->shared->nReade
f8670 72 73 20 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20  rs --;.    }.   
f8680 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61   if (pFile->loca
f8690 6c 2e 62 52 65 73 65 72 76 65 64 29 7b 0a 20 20  l.bReserved){.  
f86a0 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65      pFile->share
f86b0 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 46  d->bReserved = F
f86c0 41 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ALSE;.    }.    
f86d0 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c  if (pFile->local
f86e0 2e 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20 20  .bPending){.    
f86f0 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
f8700 3e 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53  >bPending = FALS
f8710 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20  E;.    }.    if 
f8720 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45  (pFile->local.bE
f8730 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 20 20  xclusive){.     
f8740 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e   pFile->shared->
f8750 62 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c  bExclusive = FAL
f8760 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  SE;.    }..    /
f8770 2a 20 44 65 2d 72 65 66 65 72 65 6e 63 65 20 61  * De-reference a
f8780 6e 64 20 63 6c 6f 73 65 20 6f 75 72 20 63 6f 70  nd close our cop
f8790 79 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20  y of the shared 
f87a0 6d 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 2a 2f  memory handle */
f87b0 0a 20 20 20 20 55 6e 6d 61 70 56 69 65 77 4f 66  .    UnmapViewOf
f87c0 46 69 6c 65 28 70 46 69 6c 65 2d 3e 73 68 61 72  File(pFile->shar
f87d0 65 64 29 3b 0a 20 20 20 20 43 6c 6f 73 65 48 61  ed);.    CloseHa
f87e0 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 61  ndle(pFile->hSha
f87f0 72 65 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f  red);..    /* Do
f8800 6e 65 20 77 69 74 68 20 74 68 65 20 6d 75 74 65  ne with the mute
f8810 78 20 2a 2f 0a 20 20 20 20 77 69 6e 63 65 4d 75  x */.    winceMu
f8820 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65  texRelease(pFile
f8830 2d 3e 68 4d 75 74 65 78 29 3b 20 20 20 20 0a 20  ->hMutex);    . 
f8840 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70     CloseHandle(p
f8850 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20  File->hMutex);. 
f8860 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78     pFile->hMutex
f8870 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a 0a   = NULL;.  }.}..
f8880 2f 2a 20 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d  /* .** An implem
f8890 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
f88a0 4c 6f 63 6b 46 69 6c 65 28 29 20 41 50 49 20 6f  LockFile() API o
f88b0 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69  f windows for wi
f88c0 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f  nce.*/.static BO
f88d0 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65  OL winceLockFile
f88e0 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69  (.  HANDLE *phFi
f88f0 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69  le,.  DWORD dwFi
f8900 6c 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44  leOffsetLow,.  D
f8910 57 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65  WORD dwFileOffse
f8920 74 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e  tHigh,.  DWORD n
f8930 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c  NumberOfBytesToL
f8940 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20  ockLow,.  DWORD 
f8950 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
f8960 4c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20 77 69  LockHigh.){.  wi
f8970 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 48  nFile *pFile = H
f8980 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45  ANDLE_TO_WINFILE
f8990 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f 4f 4c  (phFile);.  BOOL
f89a0 20 62 52 65 74 75 72 6e 20 3d 20 46 41 4c 53 45   bReturn = FALSE
f89b0 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ;..  UNUSED_PARA
f89c0 4d 45 54 45 52 28 64 77 46 69 6c 65 4f 66 66 73  METER(dwFileOffs
f89d0 65 74 48 69 67 68 29 3b 0a 20 20 55 4e 55 53 45  etHigh);.  UNUSE
f89e0 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 4e 75 6d  D_PARAMETER(nNum
f89f0 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b  berOfBytesToLock
f8a00 48 69 67 68 29 3b 0a 0a 20 20 69 66 20 28 21 70  High);..  if (!p
f8a10 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 20 72 65  File->hMutex) re
f8a20 74 75 72 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e  turn TRUE;.  win
f8a30 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 70  ceMutexAcquire(p
f8a40 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a  File->hMutex);..
f8a50 20 20 2f 2a 20 57 61 6e 74 69 6e 67 20 61 6e 20    /* Wanting an 
f8a60 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20  exclusive lock? 
f8a70 2a 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f  */.  if (dwFileO
f8a80 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f  ffsetLow == (DWO
f8a90 52 44 29 53 48 41 52 45 44 5f 46 49 52 53 54 0a  RD)SHARED_FIRST.
f8aa0 20 20 20 20 20 20 20 26 26 20 6e 4e 75 6d 62 65         && nNumbe
f8ab0 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f  rOfBytesToLockLo
f8ac0 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52  w == (DWORD)SHAR
f8ad0 45 44 5f 53 49 5a 45 29 7b 0a 20 20 20 20 69 66  ED_SIZE){.    if
f8ae0 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d   (pFile->shared-
f8af0 3e 6e 52 65 61 64 65 72 73 20 3d 3d 20 30 20 26  >nReaders == 0 &
f8b00 26 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d  & pFile->shared-
f8b10 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30  >bExclusive == 0
f8b20 29 7b 0a 20 20 20 20 20 20 20 70 46 69 6c 65 2d  ){.       pFile-
f8b30 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73  >shared->bExclus
f8b40 69 76 65 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  ive = TRUE;.    
f8b50 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e     pFile->local.
f8b60 62 45 78 63 6c 75 73 69 76 65 20 3d 20 54 52 55  bExclusive = TRU
f8b70 45 3b 0a 20 20 20 20 20 20 20 62 52 65 74 75 72  E;.       bRetur
f8b80 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a  n = TRUE;.    }.
f8b90 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61    }..  /* Want a
f8ba0 20 72 65 61 64 2d 6f 6e 6c 79 20 6c 6f 63 6b 3f   read-only lock?
f8bb0 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64   */.  else if (d
f8bc0 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d  wFileOffsetLow =
f8bd0 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f  = (DWORD)SHARED_
f8be0 46 49 52 53 54 20 26 26 0a 20 20 20 20 20 20 20  FIRST &&.       
f8bf0 20 20 20 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74      nNumberOfByt
f8c00 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31  esToLockLow == 1
f8c10 29 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65  ){.    if (pFile
f8c20 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75  ->shared->bExclu
f8c30 73 69 76 65 20 3d 3d 20 30 29 7b 0a 20 20 20 20  sive == 0){.    
f8c40 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e    pFile->local.n
f8c50 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20 20 20 20  Readers ++;.    
f8c60 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63    if (pFile->loc
f8c70 61 6c 2e 6e 52 65 61 64 65 72 73 20 3d 3d 20 31  al.nReaders == 1
f8c80 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65  ){.        pFile
f8c90 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65  ->shared->nReade
f8ca0 72 73 20 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20  rs ++;.      }. 
f8cb0 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54       bReturn = T
f8cc0 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  RUE;.    }.  }..
f8cd0 20 20 2f 2a 20 57 61 6e 74 20 61 20 70 65 6e 64    /* Want a pend
f8ce0 69 6e 67 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65  ing lock? */.  e
f8cf0 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65 4f 66  lse if (dwFileOf
f8d00 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52  fsetLow == (DWOR
f8d10 44 29 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26  D)PENDING_BYTE &
f8d20 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73  & nNumberOfBytes
f8d30 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b  ToLockLow == 1){
f8d40 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f 20 70 65  .    /* If no pe
f8d50 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62  nding lock has b
f8d60 65 65 6e 20 61 63 71 75 69 72 65 64 2c 20 74 68  een acquired, th
f8d70 65 6e 20 61 63 71 75 69 72 65 20 69 74 20 2a 2f  en acquire it */
f8d80 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
f8d90 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67  shared->bPending
f8da0 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70   == 0) {.      p
f8db0 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50  File->shared->bP
f8dc0 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b 0a 20  ending = TRUE;. 
f8dd0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61       pFile->loca
f8de0 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 54 52 55  l.bPending = TRU
f8df0 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e  E;.      bReturn
f8e00 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20   = TRUE;.    }. 
f8e10 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20   }..  /* Want a 
f8e20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 3f 20 2a  reserved lock? *
f8e30 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46  /.  else if (dwF
f8e40 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20  ileOffsetLow == 
f8e50 28 44 57 4f 52 44 29 52 45 53 45 52 56 45 44 5f  (DWORD)RESERVED_
f8e60 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f  BYTE && nNumberO
f8e70 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20  fBytesToLockLow 
f8e80 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70  == 1){.    if (p
f8e90 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52  File->shared->bR
f8ea0 65 73 65 72 76 65 64 20 3d 3d 20 30 29 20 7b 0a  eserved == 0) {.
f8eb0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61        pFile->sha
f8ec0 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d  red->bReserved =
f8ed0 20 54 52 55 45 3b 0a 20 20 20 20 20 20 70 46 69   TRUE;.      pFi
f8ee0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72  le->local.bReser
f8ef0 76 65 64 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  ved = TRUE;.    
f8f00 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45    bReturn = TRUE
f8f10 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 77  ;.    }.  }..  w
f8f20 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65  inceMutexRelease
f8f30 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b  (pFile->hMutex);
f8f40 0a 20 20 72 65 74 75 72 6e 20 62 52 65 74 75 72  .  return bRetur
f8f50 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  n;.}../*.** An i
f8f60 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
f8f70 20 74 68 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 20   the UnlockFile 
f8f80 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73 20 66  API of windows f
f8f90 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 74  or wince.*/.stat
f8fa0 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 55 6e 6c  ic BOOL winceUnl
f8fb0 6f 63 6b 46 69 6c 65 28 0a 20 20 48 41 4e 44 4c  ockFile(.  HANDL
f8fc0 45 20 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f  E *phFile,.  DWO
f8fd0 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c  RD dwFileOffsetL
f8fe0 6f 77 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69  ow,.  DWORD dwFi
f8ff0 6c 65 4f 66 66 73 65 74 48 69 67 68 2c 0a 20 20  leOffsetHigh,.  
f9000 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42  DWORD nNumberOfB
f9010 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 2c  ytesToUnlockLow,
f9020 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72  .  DWORD nNumber
f9030 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 48  OfBytesToUnlockH
f9040 69 67 68 0a 29 7b 0a 20 20 77 69 6e 46 69 6c 65  igh.){.  winFile
f9050 20 2a 70 46 69 6c 65 20 3d 20 48 41 4e 44 4c 45   *pFile = HANDLE
f9060 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 70 68 46 69  _TO_WINFILE(phFi
f9070 6c 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 52 65 74  le);.  BOOL bRet
f9080 75 72 6e 20 3d 20 46 41 4c 53 45 3b 0a 0a 20 20  urn = FALSE;..  
f9090 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
f90a0 28 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69 67  (dwFileOffsetHig
f90b0 68 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  h);.  UNUSED_PAR
f90c0 41 4d 45 54 45 52 28 6e 4e 75 6d 62 65 72 4f 66  AMETER(nNumberOf
f90d0 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 48 69 67  BytesToUnlockHig
f90e0 68 29 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 6c  h);..  if (!pFil
f90f0 65 2d 3e 68 4d 75 74 65 78 29 20 72 65 74 75 72  e->hMutex) retur
f9100 6e 20 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d  n TRUE;.  winceM
f9110 75 74 65 78 41 63 71 75 69 72 65 28 70 46 69 6c  utexAcquire(pFil
f9120 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f  e->hMutex);..  /
f9130 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72 65  * Releasing a re
f9140 61 64 65 72 20 6c 6f 63 6b 20 6f 72 20 61 6e 20  ader lock or an 
f9150 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a  exclusive lock *
f9160 2f 0a 20 20 69 66 20 28 64 77 46 69 6c 65 4f 66  /.  if (dwFileOf
f9170 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52  fsetLow == (DWOR
f9180 44 29 53 48 41 52 45 44 5f 46 49 52 53 54 29 7b  D)SHARED_FIRST){
f9190 0a 20 20 20 20 2f 2a 20 44 69 64 20 77 65 20 68  .    /* Did we h
f91a0 61 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65  ave an exclusive
f91b0 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 69 66   lock? */.    if
f91c0 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62   (pFile->local.b
f91d0 45 78 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 20  Exclusive){.    
f91e0 20 20 61 73 73 65 72 74 28 6e 4e 75 6d 62 65 72    assert(nNumber
f91f0 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c  OfBytesToUnlockL
f9200 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 41  ow == (DWORD)SHA
f9210 52 45 44 5f 53 49 5a 45 29 3b 0a 20 20 20 20 20  RED_SIZE);.     
f9220 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45   pFile->local.bE
f9230 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45  xclusive = FALSE
f9240 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73  ;.      pFile->s
f9250 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76  hared->bExclusiv
f9260 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20  e = FALSE;.     
f9270 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b   bReturn = TRUE;
f9280 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44  .    }..    /* D
f9290 69 64 20 77 65 20 6a 75 73 74 20 68 61 76 65 20  id we just have 
f92a0 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 3f 20 2a  a reader lock? *
f92b0 2f 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 70  /.    else if (p
f92c0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61  File->local.nRea
f92d0 64 65 72 73 29 7b 0a 20 20 20 20 20 20 61 73 73  ders){.      ass
f92e0 65 72 74 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74  ert(nNumberOfByt
f92f0 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d  esToUnlockLow ==
f9300 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f 53   (DWORD)SHARED_S
f9310 49 5a 45 20 7c 7c 20 6e 4e 75 6d 62 65 72 4f 66  IZE || nNumberOf
f9320 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77  BytesToUnlockLow
f9330 20 3d 3d 20 31 29 3b 0a 20 20 20 20 20 20 70 46   == 1);.      pF
f9340 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64  ile->local.nRead
f9350 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20 69 66  ers --;.      if
f9360 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e   (pFile->local.n
f9370 52 65 61 64 65 72 73 20 3d 3d 20 30 29 0a 20 20  Readers == 0).  
f9380 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70 46      {.        pF
f9390 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65  ile->shared->nRe
f93a0 61 64 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20  aders --;.      
f93b0 7d 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20  }.      bReturn 
f93c0 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20  = TRUE;.    }.  
f93d0 7d 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e  }..  /* Releasin
f93e0 67 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b  g a pending lock
f93f0 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64   */.  else if (d
f9400 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d  wFileOffsetLow =
f9410 3d 20 28 44 57 4f 52 44 29 50 45 4e 44 49 4e 47  = (DWORD)PENDING
f9420 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 72  _BYTE && nNumber
f9430 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c  OfBytesToUnlockL
f9440 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66  ow == 1){.    if
f9450 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62   (pFile->local.b
f9460 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20 20  Pending){.      
f9470 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65  pFile->local.bPe
f9480 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20  nding = FALSE;. 
f9490 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72       pFile->shar
f94a0 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20 46  ed->bPending = F
f94b0 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65 74  ALSE;.      bRet
f94c0 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
f94d0 7d 0a 20 20 7d 0a 20 20 2f 2a 20 52 65 6c 65 61  }.  }.  /* Relea
f94e0 73 69 6e 67 20 61 20 72 65 73 65 72 76 65 64 20  sing a reserved 
f94f0 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73 65 20 69  lock */.  else i
f9500 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c  f (dwFileOffsetL
f9510 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 52 45 53  ow == (DWORD)RES
f9520 45 52 56 45 44 5f 42 59 54 45 20 26 26 20 6e 4e  ERVED_BYTE && nN
f9530 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e  umberOfBytesToUn
f9540 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20  lockLow == 1){. 
f9550 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f     if (pFile->lo
f9560 63 61 6c 2e 62 52 65 73 65 72 76 65 64 29 20 7b  cal.bReserved) {
f9570 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  .      pFile->lo
f9580 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20 3d 20  cal.bReserved = 
f9590 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69  FALSE;.      pFi
f95a0 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73  le->shared->bRes
f95b0 65 72 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20  erved = FALSE;. 
f95c0 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54       bReturn = T
f95d0 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  RUE;.    }.  }..
f95e0 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65    winceMutexRele
f95f0 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65  ase(pFile->hMute
f9600 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 62 52 65  x);.  return bRe
f9610 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  turn;.}../*.** A
f9620 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
f9630 20 6f 66 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65   of the LockFile
f9640 45 78 28 29 20 41 50 49 20 6f 66 20 77 69 6e 64  Ex() API of wind
f9650 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2f  ows for wince.*/
f9660 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e  .static BOOL win
f9670 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 0a 20 20  ceLockFileEx(.  
f9680 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 2c 0a  HANDLE *phFile,.
f9690 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67 73 2c    DWORD dwFlags,
f96a0 0a 20 20 44 57 4f 52 44 20 64 77 52 65 73 65 72  .  DWORD dwReser
f96b0 76 65 64 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75  ved,.  DWORD nNu
f96c0 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63  mberOfBytesToLoc
f96d0 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 6e 4e  kLow,.  DWORD nN
f96e0 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f  umberOfBytesToLo
f96f0 63 6b 48 69 67 68 2c 0a 20 20 4c 50 4f 56 45 52  ckHigh,.  LPOVER
f9700 4c 41 50 50 45 44 20 6c 70 4f 76 65 72 6c 61 70  LAPPED lpOverlap
f9710 70 65 64 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f  ped.){.  UNUSED_
f9720 50 41 52 41 4d 45 54 45 52 28 64 77 52 65 73 65  PARAMETER(dwRese
f9730 72 76 65 64 29 3b 0a 20 20 55 4e 55 53 45 44 5f  rved);.  UNUSED_
f9740 50 41 52 41 4d 45 54 45 52 28 6e 4e 75 6d 62 65  PARAMETER(nNumbe
f9750 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69  rOfBytesToLockHi
f9760 67 68 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  gh);..  /* If th
f9770 65 20 63 61 6c 6c 65 72 20 77 61 6e 74 73 20 61  e caller wants a
f9780 20 73 68 61 72 65 64 20 72 65 61 64 20 6c 6f 63   shared read loc
f9790 6b 2c 20 66 6f 72 77 61 72 64 20 74 68 69 73 20  k, forward this 
f97a0 63 61 6c 6c 0a 20 20 2a 2a 20 74 6f 20 77 69 6e  call.  ** to win
f97b0 63 65 4c 6f 63 6b 46 69 6c 65 20 2a 2f 0a 20 20  ceLockFile */.  
f97c0 69 66 20 28 6c 70 4f 76 65 72 6c 61 70 70 65 64  if (lpOverlapped
f97d0 2d 3e 4f 66 66 73 65 74 20 3d 3d 20 28 44 57 4f  ->Offset == (DWO
f97e0 52 44 29 53 48 41 52 45 44 5f 46 49 52 53 54 20  RD)SHARED_FIRST 
f97f0 26 26 0a 20 20 20 20 20 20 64 77 46 6c 61 67 73  &&.      dwFlags
f9800 20 3d 3d 20 31 20 26 26 0a 20 20 20 20 20 20 6e   == 1 &&.      n
f9810 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c  NumberOfBytesToL
f9820 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44  ockLow == (DWORD
f9830 29 53 48 41 52 45 44 5f 53 49 5a 45 29 7b 0a 20  )SHARED_SIZE){. 
f9840 20 20 20 72 65 74 75 72 6e 20 77 69 6e 63 65 4c     return winceL
f9850 6f 63 6b 46 69 6c 65 28 70 68 46 69 6c 65 2c 20  ockFile(phFile, 
f9860 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c  SHARED_FIRST, 0,
f9870 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65   1, 0);.  }.  re
f9880 74 75 72 6e 20 46 41 4c 53 45 3b 0a 7d 0a 2f 2a  turn FALSE;.}./*
f9890 0a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 73  .** End of the s
f98a0 70 65 63 69 61 6c 20 63 6f 64 65 20 66 6f 72 20  pecial code for 
f98b0 77 69 6e 63 65 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  wince.**********
f98c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f98d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f98e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f98f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9900 2a 2a 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ***/.#endif /* S
f9910 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 2a  QLITE_OS_WINCE *
f9920 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
f9930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9970 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 72  *.** The next gr
f9980 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20  oup of routines 
f9990 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 49 2f  implement the I/
f99a0 4f 20 6d 65 74 68 6f 64 73 20 73 70 65 63 69 66  O methods specif
f99b0 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 73 71  ied.** by the sq
f99c0 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
f99d0 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a 2a 2a 2a 2a   object..*******
f99e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f99f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9a20 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
f9a30 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2a  Close a file..**
f9a40 0a 2a 2a 20 49 74 20 69 73 20 72 65 70 6f 72 74  .** It is report
f9a50 65 64 20 74 68 61 74 20 61 6e 20 61 74 74 65 6d  ed that an attem
f9a60 70 74 20 74 6f 20 63 6c 6f 73 65 20 61 20 68 61  pt to close a ha
f9a70 6e 64 6c 65 20 6d 69 67 68 74 20 73 6f 6d 65 74  ndle might somet
f9a80 69 6d 65 73 0a 2a 2a 20 66 61 69 6c 2e 20 20 54  imes.** fail.  T
f9a90 68 69 73 20 69 73 20 61 20 76 65 72 79 20 75 6e  his is a very un
f9aa0 72 65 61 73 6f 6e 61 62 6c 65 20 72 65 73 75 6c  reasonable resul
f9ab0 74 2c 20 62 75 74 20 77 69 6e 64 6f 77 73 20 69  t, but windows i
f9ac0 73 20 6e 6f 74 6f 72 69 6f 75 73 0a 2a 2a 20 66  s notorious.** f
f9ad0 6f 72 20 62 65 69 6e 67 20 75 6e 72 65 61 73 6f  or being unreaso
f9ae0 6e 61 62 6c 65 20 73 6f 20 49 20 64 6f 20 6e 6f  nable so I do no
f9af0 74 20 64 6f 75 62 74 20 74 68 61 74 20 69 74 20  t doubt that it 
f9b00 6d 69 67 68 74 20 68 61 70 70 65 6e 2e 20 20 49  might happen.  I
f9b10 66 0a 2a 2a 20 74 68 65 20 63 6c 6f 73 65 20 66  f.** the close f
f9b20 61 69 6c 73 2c 20 77 65 20 70 61 75 73 65 20 66  ails, we pause f
f9b30 6f 72 20 31 30 30 20 6d 69 6c 6c 69 73 65 63 6f  or 100 milliseco
f9b40 6e 64 73 20 61 6e 64 20 74 72 79 20 61 67 61 69  nds and try agai
f9b50 6e 2e 20 20 41 73 0a 2a 2a 20 6d 61 6e 79 20 61  n.  As.** many a
f9b60 73 20 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45 4d  s MX_CLOSE_ATTEM
f9b70 50 54 20 61 74 74 65 6d 70 74 73 20 74 6f 20 63  PT attempts to c
f9b80 6c 6f 73 65 20 74 68 65 20 68 61 6e 64 6c 65 20  lose the handle 
f9b90 61 72 65 20 6d 61 64 65 20 62 65 66 6f 72 65 0a  are made before.
f9ba0 2a 2a 20 67 69 76 69 6e 67 20 75 70 20 61 6e 64  ** giving up and
f9bb0 20 72 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72   returning an er
f9bc0 72 6f 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ror..*/.#define 
f9bd0 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54  MX_CLOSE_ATTEMPT
f9be0 20 33 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69   3.static int wi
f9bf0 6e 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66  nClose(sqlite3_f
f9c00 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20  ile *id){.  int 
f9c10 72 63 2c 20 63 6e 74 20 3d 20 30 3b 0a 20 20 77  rc, cnt = 0;.  w
f9c20 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  inFile *pFile = 
f9c30 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a 20  (winFile*)id;.. 
f9c40 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29   assert( id!=0 )
f9c50 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22 43 4c  ;.  OSTRACE2("CL
f9c60 4f 53 45 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65  OSE %d\n", pFile
f9c70 2d 3e 68 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20  ->h);.  do{.    
f9c80 72 63 20 3d 20 43 6c 6f 73 65 48 61 6e 64 6c 65  rc = CloseHandle
f9c90 28 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 7d 77  (pFile->h);.  }w
f9ca0 68 69 6c 65 28 20 72 63 3d 3d 30 20 26 26 20 2b  hile( rc==0 && +
f9cb0 2b 63 6e 74 20 3c 20 4d 58 5f 43 4c 4f 53 45 5f  +cnt < MX_CLOSE_
f9cc0 41 54 54 45 4d 50 54 20 26 26 20 28 53 6c 65 65  ATTEMPT && (Slee
f9cd0 70 28 31 30 30 29 2c 20 31 29 20 29 3b 0a 23 69  p(100), 1) );.#i
f9ce0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
f9cf0 45 0a 23 64 65 66 69 6e 65 20 57 49 4e 43 45 5f  E.#define WINCE_
f9d00 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54  DELETION_ATTEMPT
f9d10 53 20 33 0a 20 20 77 69 6e 63 65 44 65 73 74 72  S 3.  winceDestr
f9d20 6f 79 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20  oyLock(pFile);. 
f9d30 20 69 66 28 20 70 46 69 6c 65 2d 3e 7a 44 65 6c   if( pFile->zDel
f9d40 65 74 65 4f 6e 43 6c 6f 73 65 20 29 7b 0a 20 20  eteOnClose ){.  
f9d50 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20    int cnt = 0;. 
f9d60 20 20 20 77 68 69 6c 65 28 0a 20 20 20 20 20 20     while(.      
f9d70 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65 57       DeleteFileW
f9d80 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f  (pFile->zDeleteO
f9d90 6e 43 6c 6f 73 65 29 3d 3d 30 0a 20 20 20 20 20  nClose)==0.     
f9da0 20 20 20 26 26 20 47 65 74 46 69 6c 65 41 74 74     && GetFileAtt
f9db0 72 69 62 75 74 65 73 57 28 70 46 69 6c 65 2d 3e  ributesW(pFile->
f9dc0 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 21  zDeleteOnClose)!
f9dd0 3d 30 78 66 66 66 66 66 66 66 66 20 0a 20 20 20  =0xffffffff .   
f9de0 20 20 20 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20       && cnt++ < 
f9df0 57 49 4e 43 45 5f 44 45 4c 45 54 49 4f 4e 5f 41  WINCE_DELETION_A
f9e00 54 54 45 4d 50 54 53 0a 20 20 20 20 29 7b 0a 20  TTEMPTS.    ){. 
f9e10 20 20 20 20 20 20 53 6c 65 65 70 28 31 30 30 29        Sleep(100)
f9e20 3b 20 20 2f 2a 20 57 61 69 74 20 61 20 6c 69 74  ;  /* Wait a lit
f9e30 74 6c 65 20 62 65 66 6f 72 65 20 74 72 79 69 6e  tle before tryin
f9e40 67 20 61 67 61 69 6e 20 2a 2f 0a 20 20 20 20 7d  g again */.    }
f9e50 0a 20 20 20 20 66 72 65 65 28 70 46 69 6c 65 2d  .    free(pFile-
f9e60 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29  >zDeleteOnClose)
f9e70 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 4f  ;.  }.#endif.  O
f9e80 70 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a  penCounter(-1);.
f9e90 20 20 72 65 74 75 72 6e 20 72 63 20 3f 20 53 51    return rc ? SQ
f9ea0 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45  LITE_OK : SQLITE
f9eb0 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  _IOERR;.}../*.**
f9ec0 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74 20   Some microsoft 
f9ed0 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20 74  compilers lack t
f9ee0 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a  his definition..
f9ef0 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 41 4c  */.#ifndef INVAL
f9f00 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e  ID_SET_FILE_POIN
f9f10 54 45 52 0a 23 20 64 65 66 69 6e 65 20 49 4e 56  TER.# define INV
f9f20 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f  ALID_SET_FILE_PO
f9f30 49 4e 54 45 52 20 28 28 44 57 4f 52 44 29 2d 31  INTER ((DWORD)-1
f9f40 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
f9f50 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61  Read data from a
f9f60 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66   file into a buf
f9f70 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c  fer.  Return SQL
f9f80 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a  ITE_OK if all.**
f9f90 20 62 79 74 65 73 20 77 65 72 65 20 72 65 61 64   bytes were read
f9fa0 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e   successfully an
f9fb0 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69  d SQLITE_IOERR i
f9fc0 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a  f anything goes.
f9fd0 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61  ** wrong..*/.sta
f9fe0 74 69 63 20 69 6e 74 20 77 69 6e 52 65 61 64 28  tic int winRead(
f9ff0 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
fa000 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  *id,          /*
fa010 20 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72   File to read fr
fa020 6f 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42  om */.  void *pB
fa030 75 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  uf,             
fa040 20 20 20 2f 2a 20 57 72 69 74 65 20 63 6f 6e 74     /* Write cont
fa050 65 6e 74 20 69 6e 74 6f 20 74 68 69 73 20 62 75  ent into this bu
fa060 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 6d  ffer */.  int am
fa070 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
fa080 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
fa090 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 64 20  f bytes to read 
fa0a0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
fa0b0 36 34 20 6f 66 66 73 65 74 20 20 20 20 20 20 20  64 offset       
fa0c0 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67  /* Begin reading
fa0d0 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20   at this offset 
fa0e0 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70  */.){.  LONG upp
fa0f0 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28  erBits = (LONG)(
fa100 28 6f 66 66 73 65 74 3e 3e 33 32 29 20 26 20 30  (offset>>32) & 0
fa110 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c 4f  x7fffffff);.  LO
fa120 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 28  NG lowerBits = (
fa130 4c 4f 4e 47 29 28 6f 66 66 73 65 74 20 26 20 30  LONG)(offset & 0
fa140 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44 57  xffffffff);.  DW
fa150 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c  ORD rc;.  winFil
fa160 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46  e *pFile = (winF
fa170 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44  ile*)id;.  DWORD
fa180 20 65 72 72 6f 72 3b 0a 20 20 44 57 4f 52 44 20   error;.  DWORD 
fa190 67 6f 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  got;..  assert( 
fa1a0 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c  id!=0 );.  Simul
fa1b0 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72  ateIOError(retur
fa1c0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52  n SQLITE_IOERR_R
fa1d0 45 41 44 29 3b 0a 20 20 4f 53 54 52 41 43 45 33  EAD);.  OSTRACE3
fa1e0 28 22 52 45 41 44 20 25 64 20 6c 6f 63 6b 3d 25  ("READ %d lock=%
fa1f0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
fa200 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29  pFile->locktype)
fa210 3b 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c 65  ;.  rc = SetFile
fa220 50 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68  Pointer(pFile->h
fa230 2c 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70  , lowerBits, &up
fa240 70 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45  perBits, FILE_BE
fa250 47 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  GIN);.  if( rc==
fa260 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45  INVALID_SET_FILE
fa270 5f 50 4f 49 4e 54 45 52 20 26 26 20 28 65 72 72  _POINTER && (err
fa280 6f 72 3d 47 65 74 4c 61 73 74 45 72 72 6f 72 28  or=GetLastError(
fa290 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a  ))!=NO_ERROR ){.
fa2a0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
fa2b0 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20  rrno = error;.  
fa2c0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
fa2d0 46 55 4c 4c 3b 0a 20 20 7d 0a 20 20 69 66 28 20  FULL;.  }.  if( 
fa2e0 21 52 65 61 64 46 69 6c 65 28 70 46 69 6c 65 2d  !ReadFile(pFile-
fa2f0 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 26  >h, pBuf, amt, &
fa300 67 6f 74 2c 20 30 29 20 29 7b 0a 20 20 20 20 70  got, 0) ){.    p
fa310 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
fa320 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29  = GetLastError()
fa330 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
fa340 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a  ITE_IOERR_READ;.
fa350 20 20 7d 0a 20 20 69 66 28 20 67 6f 74 3d 3d 28    }.  if( got==(
fa360 44 57 4f 52 44 29 61 6d 74 20 29 7b 0a 20 20 20  DWORD)amt ){.   
fa370 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
fa380 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  K;.  }else{.    
fa390 2f 2a 20 55 6e 72 65 61 64 20 70 61 72 74 73 20  /* Unread parts 
fa3a0 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 6d 75  of the buffer mu
fa3b0 73 74 20 62 65 20 7a 65 72 6f 2d 66 69 6c 6c 65  st be zero-fille
fa3c0 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28  d */.    memset(
fa3d0 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 67  &((char*)pBuf)[g
fa3e0 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f 74 29  ot], 0, amt-got)
fa3f0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
fa400 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f  ITE_IOERR_SHORT_
fa410 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  READ;.  }.}../*.
fa420 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 66 72  ** Write data fr
fa430 6f 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f  om a buffer into
fa440 20 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e   a file.  Return
fa450 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75   SQLITE_OK on su
fa460 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65  ccess.** or some
fa470 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64   other error cod
fa480 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f  e on failure..*/
fa490 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 57  .static int winW
fa4a0 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  rite(.  sqlite3_
fa4b0 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20  file *id,       
fa4c0 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 77 72 69    /* File to wri
fa4d0 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 6f 6e  te into */.  con
fa4e0 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20  st void *pBuf,  
fa4f0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 79         /* The by
fa500 74 65 73 20 74 6f 20 62 65 20 77 72 69 74 74 65  tes to be writte
fa510 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20  n */.  int amt, 
fa520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fa530 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
fa540 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a  tes to write */.
fa550 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
fa560 6f 66 66 73 65 74 20 20 20 20 20 20 2f 2a 20 4f  offset      /* O
fa570 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66  ffset into the f
fa580 69 6c 65 20 74 6f 20 62 65 67 69 6e 20 77 72 69  ile to begin wri
fa590 74 69 6e 67 20 61 74 20 2a 2f 0a 29 7b 0a 20 20  ting at */.){.  
fa5a0 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20 3d  LONG upperBits =
fa5b0 20 28 4c 4f 4e 47 29 28 28 6f 66 66 73 65 74 3e   (LONG)((offset>
fa5c0 3e 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66  >32) & 0x7ffffff
fa5d0 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72  f);.  LONG lower
fa5e0 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6f 66  Bits = (LONG)(of
fa5f0 66 73 65 74 20 26 20 30 78 66 66 66 66 66 66 66  fset & 0xfffffff
fa600 66 29 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a  f);.  DWORD rc;.
fa610 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65    winFile *pFile
fa620 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b   = (winFile*)id;
fa630 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a  .  DWORD error;.
fa640 20 20 44 57 4f 52 44 20 77 72 6f 74 65 20 3d 20    DWORD wrote = 
fa650 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64  0;..  assert( id
fa660 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74  !=0 );.  Simulat
fa670 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20  eIOError(return 
fa680 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49  SQLITE_IOERR_WRI
fa690 54 45 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 44  TE);.  SimulateD
fa6a0 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 72 65 74  iskfullError(ret
fa6b0 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29  urn SQLITE_FULL)
fa6c0 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 57 52  ;.  OSTRACE3("WR
fa6d0 49 54 45 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e  ITE %d lock=%d\n
fa6e0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69  ", pFile->h, pFi
fa6f0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a 20  le->locktype);. 
fa700 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50 6f 69   rc = SetFilePoi
fa710 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6c  nter(pFile->h, l
fa720 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70 65 72  owerBits, &upper
fa730 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47 49 4e  Bits, FILE_BEGIN
fa740 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49 4e 56  );.  if( rc==INV
fa750 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f  ALID_SET_FILE_PO
fa760 49 4e 54 45 52 20 26 26 20 28 65 72 72 6f 72 3d  INTER && (error=
fa770 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 21  GetLastError())!
fa780 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20  =NO_ERROR ){.   
fa790 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
fa7a0 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72  o = error;.    r
fa7b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c  eturn SQLITE_FUL
fa7c0 4c 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  L;.  }.  assert(
fa7d0 20 61 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69 6c   amt>0 );.  whil
fa7e0 65 28 0a 20 20 20 20 20 61 6d 74 3e 30 0a 20 20  e(.     amt>0.  
fa7f0 20 20 20 26 26 20 28 72 63 20 3d 20 57 72 69 74     && (rc = Writ
fa800 65 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  eFile(pFile->h, 
fa810 70 42 75 66 2c 20 61 6d 74 2c 20 26 77 72 6f 74  pBuf, amt, &wrot
fa820 65 2c 20 30 29 29 21 3d 30 0a 20 20 20 20 20 26  e, 0))!=0.     &
fa830 26 20 77 72 6f 74 65 3e 30 0a 20 20 29 7b 0a 20  & wrote>0.  ){. 
fa840 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f 74 65 3b     amt -= wrote;
fa850 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28 63  .    pBuf = &((c
fa860 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 65  har*)pBuf)[wrote
fa870 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 72 63  ];.  }.  if( !rc
fa880 20 7c 7c 20 61 6d 74 3e 28 69 6e 74 29 77 72 6f   || amt>(int)wro
fa890 74 65 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d  te ){.    pFile-
fa8a0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74  >lastErrno = Get
fa8b0 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20  LastError();.   
fa8c0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46   return SQLITE_F
fa8d0 55 4c 4c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ULL;.  }.  retur
fa8e0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
fa8f0 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 61  /*.** Truncate a
fa900 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f 20 61  n open file to a
fa910 20 73 70 65 63 69 66 69 65 64 20 73 69 7a 65 0a   specified size.
fa920 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
fa930 6e 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65  nTruncate(sqlite
fa940 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69  3_file *id, sqli
fa950 74 65 33 5f 69 6e 74 36 34 20 6e 42 79 74 65 29  te3_int64 nByte)
fa960 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65 72 42 69  {.  LONG upperBi
fa970 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28 6e 42 79  ts = (LONG)((nBy
fa980 74 65 3e 3e 33 32 29 20 26 20 30 78 37 66 66 66  te>>32) & 0x7fff
fa990 66 66 66 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f  ffff);.  LONG lo
fa9a0 77 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29  werBits = (LONG)
fa9b0 28 6e 42 79 74 65 20 26 20 30 78 66 66 66 66 66  (nByte & 0xfffff
fa9c0 66 66 66 29 3b 0a 20 20 44 57 4f 52 44 20 72 63  fff);.  DWORD rc
fa9d0 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69  ;.  winFile *pFi
fa9e0 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69  le = (winFile*)i
fa9f0 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72  d;.  DWORD error
faa00 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21  ;..  assert( id!
faa10 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33  =0 );.  OSTRACE3
faa20 28 22 54 52 55 4e 43 41 54 45 20 25 64 20 25 6c  ("TRUNCATE %d %l
faa30 6c 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  ld\n", pFile->h,
faa40 20 6e 42 79 74 65 29 3b 0a 20 20 53 69 6d 75 6c   nByte);.  Simul
faa50 61 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72  ateIOError(retur
faa60 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54  n SQLITE_IOERR_T
faa70 52 55 4e 43 41 54 45 29 3b 0a 20 20 72 63 20 3d  RUNCATE);.  rc =
faa80 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 28   SetFilePointer(
faa90 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42  pFile->h, lowerB
faaa0 69 74 73 2c 20 26 75 70 70 65 72 42 69 74 73 2c  its, &upperBits,
faab0 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20   FILE_BEGIN);.  
faac0 69 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44 5f  if( rc==INVALID_
faad0 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52  SET_FILE_POINTER
faae0 20 26 26 20 28 65 72 72 6f 72 3d 47 65 74 4c 61   && (error=GetLa
faaf0 73 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f 45  stError())!=NO_E
fab00 52 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 69 6c  RROR ){.    pFil
fab10 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65  e->lastErrno = e
fab20 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 6e  rror;.    return
fab30 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52   SQLITE_IOERR_TR
fab40 55 4e 43 41 54 45 3b 0a 20 20 7d 0a 20 20 2f 2a  UNCATE;.  }.  /*
fab50 20 53 65 74 45 6e 64 4f 66 46 69 6c 65 20 77 69   SetEndOfFile wi
fab60 6c 6c 20 66 61 69 6c 20 69 66 20 6e 42 79 74 65  ll fail if nByte
fab70 20 69 73 20 6e 65 67 61 74 69 76 65 20 2a 2f 0a   is negative */.
fab80 20 20 69 66 28 20 21 53 65 74 45 6e 64 4f 66 46    if( !SetEndOfF
fab90 69 6c 65 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b  ile(pFile->h) ){
faba0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74  .    pFile->last
fabb0 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45  Errno = GetLastE
fabc0 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 75  rror();.    retu
fabd0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
fabe0 54 52 55 4e 43 41 54 45 3b 0a 20 20 7d 0a 20 20  TRUNCATE;.  }.  
fabf0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
fac00 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
fac10 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f  TE_TEST./*.** Co
fac20 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  unt the number o
fac30 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20  f fullsyncs and 
fac40 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54  normal syncs.  T
fac50 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 74  his is used to t
fac60 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63  est.** that sync
fac70 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20  s and fullsyncs 
fac80 61 72 65 20 6f 63 63 75 72 69 6e 67 20 61 74 20  are occuring at 
fac90 74 68 65 20 72 69 67 68 74 20 74 69 6d 65 73 2e  the right times.
faca0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
facb0 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f  nt sqlite3_sync_
facc0 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54  count = 0;.SQLIT
facd0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
face0 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74  3_fullsync_count
facf0 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a   = 0;.#endif../*
fad00 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c  .** Make sure al
fad10 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20 70 61  l writes to a pa
fad20 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20 61 72  rticular file ar
fad30 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f 20 64  e committed to d
fad40 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  isk..*/.static i
fad50 6e 74 20 77 69 6e 53 79 6e 63 28 73 71 6c 69 74  nt winSync(sqlit
fad60 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
fad70 20 66 6c 61 67 73 29 7b 0a 23 69 66 6e 64 65 66   flags){.#ifndef
fad80 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a   SQLITE_NO_SYNC.
fad90 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65    winFile *pFile
fada0 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b   = (winFile*)id;
fadb0 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d  ..  assert( id!=
fadc0 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28  0 );.  OSTRACE3(
fadd0 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d 25 64  "SYNC %d lock=%d
fade0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70  \n", pFile->h, p
fadf0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b  File->locktype);
fae00 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f  .#else.  UNUSED_
fae10 50 41 52 41 4d 45 54 45 52 28 69 64 29 3b 0a 23  PARAMETER(id);.#
fae20 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51  endif.#ifndef SQ
fae30 4c 49 54 45 5f 54 45 53 54 0a 20 20 55 4e 55 53  LITE_TEST.  UNUS
fae40 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66 6c 61  ED_PARAMETER(fla
fae50 67 73 29 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28  gs);.#else.  if(
fae60 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f   flags & SQLITE_
fae70 53 59 4e 43 5f 46 55 4c 4c 20 29 7b 0a 20 20 20  SYNC_FULL ){.   
fae80 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e   sqlite3_fullsyn
fae90 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 7d 0a 20  c_count++;.  }. 
faea0 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f   sqlite3_sync_co
faeb0 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20  unt++;.#endif.  
faec0 2f 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65  /* If we compile
faed0 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54  d with the SQLIT
faee0 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61 67 2c 20  E_NO_SYNC flag, 
faef0 74 68 65 6e 20 73 79 6e 63 69 6e 67 20 69 73 20  then syncing is 
faf00 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a 20 20 2a  a.  ** no-op.  *
faf10 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
faf20 4e 4f 5f 53 59 4e 43 0a 20 20 20 20 72 65 74 75  NO_SYNC.    retu
faf30 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65  rn SQLITE_OK;.#e
faf40 6c 73 65 0a 20 20 69 66 28 20 46 6c 75 73 68 46  lse.  if( FlushF
faf50 69 6c 65 42 75 66 66 65 72 73 28 70 46 69 6c 65  ileBuffers(pFile
faf60 2d 3e 68 29 20 29 7b 0a 20 20 20 20 72 65 74 75  ->h) ){.    retu
faf70 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
faf80 7d 65 6c 73 65 7b 0a 20 20 20 20 70 46 69 6c 65  }else{.    pFile
faf90 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47 65  ->lastErrno = Ge
fafa0 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20  tLastError();.  
fafb0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
fafc0 49 4f 45 52 52 3b 0a 20 20 7d 0a 23 65 6e 64 69  IOERR;.  }.#endi
fafd0 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72  f.}../*.** Deter
fafe0 6d 69 6e 65 20 74 68 65 20 63 75 72 72 65 6e 74  mine the current
faff0 20 73 69 7a 65 20 6f 66 20 61 20 66 69 6c 65 20   size of a file 
fb000 69 6e 20 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74  in bytes.*/.stat
fb010 69 63 20 69 6e 74 20 77 69 6e 46 69 6c 65 53 69  ic int winFileSi
fb020 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ze(sqlite3_file 
fb030 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  *id, sqlite3_int
fb040 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 44 57  64 *pSize){.  DW
fb050 4f 52 44 20 75 70 70 65 72 42 69 74 73 3b 0a 20  ORD upperBits;. 
fb060 20 44 57 4f 52 44 20 6c 6f 77 65 72 42 69 74 73   DWORD lowerBits
fb070 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69  ;.  winFile *pFi
fb080 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69  le = (winFile*)i
fb090 64 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72  d;.  DWORD error
fb0a0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 21  ;..  assert( id!
fb0b0 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65  =0 );.  Simulate
fb0c0 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53  IOError(return S
fb0d0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41  QLITE_IOERR_FSTA
fb0e0 54 29 3b 0a 20 20 6c 6f 77 65 72 42 69 74 73 20  T);.  lowerBits 
fb0f0 3d 20 47 65 74 46 69 6c 65 53 69 7a 65 28 70 46  = GetFileSize(pF
fb100 69 6c 65 2d 3e 68 2c 20 26 75 70 70 65 72 42 69  ile->h, &upperBi
fb110 74 73 29 3b 0a 20 20 69 66 28 20 20 20 28 6c 6f  ts);.  if(   (lo
fb120 77 65 72 42 69 74 73 20 3d 3d 20 49 4e 56 41 4c  werBits == INVAL
fb130 49 44 5f 46 49 4c 45 5f 53 49 5a 45 29 0a 20 20  ID_FILE_SIZE).  
fb140 20 20 20 26 26 20 28 28 65 72 72 6f 72 20 3d 20     && ((error = 
fb150 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 20  GetLastError()) 
fb160 21 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 29 0a 20  != NO_ERROR) ). 
fb170 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61   {.    pFile->la
fb180 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b  stErrno = error;
fb190 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
fb1a0 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a  TE_IOERR_FSTAT;.
fb1b0 20 20 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20 28    }.  *pSize = (
fb1c0 28 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29  ((sqlite3_int64)
fb1d0 75 70 70 65 72 42 69 74 73 29 3c 3c 33 32 29 20  upperBits)<<32) 
fb1e0 2b 20 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20 72  + lowerBits;.  r
fb1f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
fb200 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 4f 43 4b 46 49  .}../*.** LOCKFI
fb210 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54  LE_FAIL_IMMEDIAT
fb220 45 4c 59 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ELY is undefined
fb230 20 6f 6e 20 73 6f 6d 65 20 57 69 6e 64 6f 77 73   on some Windows
fb240 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66   systems..*/.#if
fb250 6e 64 65 66 20 4c 4f 43 4b 46 49 4c 45 5f 46 41  ndef LOCKFILE_FA
fb260 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 0a 23  IL_IMMEDIATELY.#
fb270 20 64 65 66 69 6e 65 20 4c 4f 43 4b 46 49 4c 45   define LOCKFILE
fb280 5f 46 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c  _FAIL_IMMEDIATEL
fb290 59 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  Y 1.#endif../*.*
fb2a0 2a 20 41 63 71 75 69 72 65 20 61 20 72 65 61 64  * Acquire a read
fb2b0 65 72 20 6c 6f 63 6b 2e 0a 2a 2a 20 44 69 66 66  er lock..** Diff
fb2c0 65 72 65 6e 74 20 41 50 49 20 72 6f 75 74 69 6e  erent API routin
fb2d0 65 73 20 61 72 65 20 63 61 6c 6c 65 64 20 64 65  es are called de
fb2e0 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68  pending on wheth
fb2f0 65 72 20 6f 72 20 6e 6f 74 20 74 68 69 73 0a 2a  er or not this.*
fb300 2a 20 69 73 20 57 69 6e 39 35 20 6f 72 20 57 69  * is Win95 or Wi
fb310 6e 4e 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  nNT..*/.static i
fb320 6e 74 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 77  nt getReadLock(w
fb330 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a  inFile *pFile){.
fb340 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 66 28    int res;.  if(
fb350 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 4f   isNT() ){.    O
fb360 56 45 52 4c 41 50 50 45 44 20 6f 76 6c 70 3b 0a  VERLAPPED ovlp;.
fb370 20 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74 20      ovlp.Offset 
fb380 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a  = SHARED_FIRST;.
fb390 20 20 20 20 6f 76 6c 70 2e 4f 66 66 73 65 74 48      ovlp.OffsetH
fb3a0 69 67 68 20 3d 20 30 3b 0a 20 20 20 20 6f 76 6c  igh = 0;.    ovl
fb3b0 70 2e 68 45 76 65 6e 74 20 3d 20 30 3b 0a 20 20  p.hEvent = 0;.  
fb3c0 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65    res = LockFile
fb3d0 45 78 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43  Ex(pFile->h, LOC
fb3e0 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44  KFILE_FAIL_IMMED
fb3f0 49 41 54 45 4c 59 2c 0a 20 20 20 20 20 20 20 20  IATELY,.        
fb400 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
fb410 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 2c 20  SHARED_SIZE, 0, 
fb420 26 6f 76 6c 70 29 3b 0a 2f 2a 20 69 73 4e 54 28  &ovlp);./* isNT(
fb430 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45  ) is 1 if SQLITE
fb440 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f  _OS_WINCE==1, so
fb450 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65   this else is ne
fb460 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a  ver executed. .*
fb470 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
fb480 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65  WINCE==0.  }else
fb490 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 3b 0a 20 20  {.    int lk;.  
fb4a0 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d    sqlite3_random
fb4b0 6e 65 73 73 28 73 69 7a 65 6f 66 28 6c 6b 29 2c  ness(sizeof(lk),
fb4c0 20 26 6c 6b 29 3b 0a 20 20 20 20 70 46 69 6c 65   &lk);.    pFile
fb4d0 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65  ->sharedLockByte
fb4e0 20 3d 20 28 73 68 6f 72 74 29 28 28 6c 6b 20 26   = (short)((lk &
fb4f0 20 30 78 37 66 66 66 66 66 66 66 29 25 28 53 48   0x7fffffff)%(SH
fb500 41 52 45 44 5f 53 49 5a 45 20 2d 20 31 29 29 3b  ARED_SIZE - 1));
fb510 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46  .    res = LockF
fb520 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48  ile(pFile->h, SH
fb530 41 52 45 44 5f 46 49 52 53 54 2b 70 46 69 6c 65  ARED_FIRST+pFile
fb540 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65  ->sharedLockByte
fb550 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 23 65 6e 64  , 0, 1, 0);.#end
fb560 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 72 65 73  if.  }.  if( res
fb570 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 70 46 69   == 0 ){.    pFi
fb580 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
fb590 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a  GetLastError();.
fb5a0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73    }.  return res
fb5b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20  ;.}../*.** Undo 
fb5c0 61 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74  a readlock.*/.st
fb5d0 61 74 69 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52  atic int unlockR
fb5e0 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20  eadLock(winFile 
fb5f0 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72  *pFile){.  int r
fb600 65 73 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29  es;.  if( isNT()
fb610 20 29 7b 0a 20 20 20 20 72 65 73 20 3d 20 55 6e   ){.    res = Un
fb620 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e  lockFile(pFile->
fb630 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c  h, SHARED_FIRST,
fb640 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c   0, SHARED_SIZE,
fb650 20 30 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69   0);./* isNT() i
fb660 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53  s 1 if SQLITE_OS
fb670 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68  _WINCE==1, so th
fb680 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72  is else is never
fb690 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23   executed. .*/.#
fb6a0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
fb6b0 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20  CE==0.  }else{. 
fb6c0 20 20 20 72 65 73 20 3d 20 55 6e 6c 6f 63 6b 46     res = UnlockF
fb6d0 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48  ile(pFile->h, SH
fb6e0 41 52 45 44 5f 46 49 52 53 54 20 2b 20 70 46 69  ARED_FIRST + pFi
fb6f0 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79  le->sharedLockBy
fb700 74 65 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 23 65  te, 0, 1, 0);.#e
fb710 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 72  ndif.  }.  if( r
fb720 65 73 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 70  es == 0 ){.    p
fb730 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
fb740 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29  = GetLastError()
fb750 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
fb760 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63  es;.}../*.** Loc
fb770 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20  k the file with 
fb780 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69  the lock specifi
fb790 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20  ed by parameter 
fb7a0 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a  locktype - one.*
fb7b0 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * of the followi
fb7c0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31  ng:.**.**     (1
fb7d0 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a  ) SHARED_LOCK.**
fb7e0 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45       (2) RESERVE
fb7f0 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33  D_LOCK.**     (3
fb800 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a  ) PENDING_LOCK.*
fb810 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53  *     (4) EXCLUS
fb820 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53  IVE_LOCK.**.** S
fb830 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65  ometimes when re
fb840 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63  questing one loc
fb850 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f  k state, additio
fb860 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a  nal lock states.
fb870 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20  ** are inserted 
fb880 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65  in between.  The
fb890 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66   locking might f
fb8a0 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68  ail on one of th
fb8b0 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73  e later.** trans
fb8c0 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74  itions leaving t
fb8d0 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69  he lock state di
fb8e0 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61  fferent from wha
fb8f0 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74  t it started but
fb900 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20  .** still short 
fb910 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68  of its goal.  Th
fb920 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72  e following char
fb930 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f  t shows the allo
fb940 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  wed.** transitio
fb950 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72  ns and the inser
fb960 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65  ted intermediate
fb970 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20   states:.**.**  
fb980 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48    UNLOCKED -> SH
fb990 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45  ARED.**    SHARE
fb9a0 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a  D -> RESERVED.**
fb9b0 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50      SHARED -> (P
fb9c0 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55  ENDING) -> EXCLU
fb9d0 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52  SIVE.**    RESER
fb9e0 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29  VED -> (PENDING)
fb9f0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
fba00 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45      PENDING -> E
fba10 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54  XCLUSIVE.**.** T
fba20 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
fba30 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61   only increase a
fba40 20 6c 6f 63 6b 2e 20 20 54 68 65 20 77 69 6e 55   lock.  The winU
fba50 6e 6c 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a  nlock() routine.
fba60 2a 2a 20 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f  ** erases all lo
fba70 63 6b 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 20  cks at once and 
fba80 72 65 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 64  returns us immed
fba90 69 61 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e  iately to lockin
fbaa0 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74  g level 0..** It
fbab0 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
fbac0 20 74 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f   to lower the lo
fbad0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20  cking level one 
fbae0 73 74 65 70 20 61 74 20 61 20 74 69 6d 65 2e 20  step at a time. 
fbaf0 20 59 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20   You.** must go 
fbb00 73 74 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b  straight to lock
fbb10 69 6e 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a  ing level 0..*/.
fbb20 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4c 6f  static int winLo
fbb30 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
fbb40 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70  *id, int locktyp
fbb50 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  e){.  int rc = S
fbb60 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 2f 2a 20  QLITE_OK;    /* 
fbb70 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d  Return code from
fbb80 20 73 75 62 72 6f 75 74 69 6e 65 73 20 2a 2f 0a   subroutines */.
fbb90 20 20 69 6e 74 20 72 65 73 20 3d 20 31 3b 20 20    int res = 1;  
fbba0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75           /* Resu
fbbb0 6c 74 20 6f 66 20 61 20 77 69 6e 64 6f 77 73 20  lt of a windows 
fbbc0 6c 6f 63 6b 20 63 61 6c 6c 20 2a 2f 0a 20 20 69  lock call */.  i
fbbd0 6e 74 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 20  nt newLocktype; 
fbbe0 20 20 20 20 20 20 2f 2a 20 53 65 74 20 70 46 69        /* Set pFi
fbbf0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 74 6f 20  le->locktype to 
fbc00 74 68 69 73 20 76 61 6c 75 65 20 62 65 66 6f 72  this value befor
fbc10 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a 20 20 69  e exiting */.  i
fbc20 6e 74 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63  nt gotPendingLoc
fbc30 6b 20 3d 20 30 3b 2f 2a 20 54 72 75 65 20 69 66  k = 0;/* True if
fbc40 20 77 65 20 61 63 71 75 69 72 65 64 20 61 20 50   we acquired a P
fbc50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 69 73  ENDING lock this
fbc60 20 74 69 6d 65 20 2a 2f 0a 20 20 77 69 6e 46 69   time */.  winFi
fbc70 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e  le *pFile = (win
fbc80 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52  File*)id;.  DWOR
fbc90 44 20 65 72 72 6f 72 20 3d 20 4e 4f 5f 45 52 52  D error = NO_ERR
fbca0 4f 52 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69  OR;..  assert( i
fbcb0 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43  d!=0 );.  OSTRAC
fbcc0 45 35 28 22 4c 4f 43 4b 20 25 64 20 25 64 20 77  E5("LOCK %d %d w
fbcd0 61 73 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20 20  as %d(%d)\n",.  
fbce0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68          pFile->h
fbcf0 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c  , locktype, pFil
fbd00 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69  e->locktype, pFi
fbd10 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79  le->sharedLockBy
fbd20 74 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  te);..  /* If th
fbd30 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61  ere is already a
fbd40 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79   lock of this ty
fbd50 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72  pe or more restr
fbd60 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20  ictive on the.  
fbd70 2a 2a 20 4f 73 46 69 6c 65 2c 20 64 6f 20 6e 6f  ** OsFile, do no
fbd80 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65  thing. Don't use
fbd90 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65   the end_lock: e
fbda0 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a  xit path, as.  *
fbdb0 2a 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65 72  * sqlite3OsEnter
fbdc0 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 62  Mutex() hasn't b
fbdd0 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a  een called yet..
fbde0 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65    */.  if( pFile
fbdf0 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b  ->locktype>=lock
fbe00 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75  type ){.    retu
fbe10 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
fbe20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  }..  /* Make sur
fbe30 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65  e the locking se
fbe40 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63  quence is correc
fbe50 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  t.  */.  assert(
fbe60 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
fbe70 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63  !=NO_LOCK || loc
fbe80 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
fbe90 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  CK );.  assert( 
fbea0 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e  locktype!=PENDIN
fbeb0 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65  G_LOCK );.  asse
fbec0 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45  rt( locktype!=RE
fbed0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70  SERVED_LOCK || p
fbee0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
fbef0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a  SHARED_LOCK );..
fbf00 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 50 45    /* Lock the PE
fbf10 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20  NDING_LOCK byte 
fbf20 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20 61 63  if we need to ac
fbf30 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20  quire a PENDING 
fbf40 6c 6f 63 6b 20 6f 72 0a 20 20 2a 2a 20 61 20 53  lock or.  ** a S
fbf50 48 41 52 45 44 20 6c 6f 63 6b 2e 20 20 49 66 20  HARED lock.  If 
fbf60 77 65 20 61 72 65 20 61 63 71 75 69 72 69 6e 67  we are acquiring
fbf70 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20   a SHARED lock, 
fbf80 74 68 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20  the acquisition 
fbf90 6f 66 0a 20 20 2a 2a 20 74 68 65 20 50 45 4e 44  of.  ** the PEND
fbfa0 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 73  ING_LOCK byte is
fbfb0 20 74 65 6d 70 6f 72 61 72 79 2e 0a 20 20 2a 2f   temporary..  */
fbfc0 0a 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d  .  newLocktype =
fbfd0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
fbfe0 3b 0a 20 20 69 66 28 20 20 20 28 70 46 69 6c 65  ;.  if(   (pFile
fbff0 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c  ->locktype==NO_L
fc000 4f 43 4b 29 0a 20 20 20 20 20 7c 7c 20 28 20 20  OCK).     || (  
fc010 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c   (locktype==EXCL
fc020 55 53 49 56 45 5f 4c 4f 43 4b 29 0a 20 20 20 20  USIVE_LOCK).    
fc030 20 20 20 20 20 26 26 20 28 70 46 69 6c 65 2d 3e       && (pFile->
fc040 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56  locktype==RESERV
fc050 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29 7b 0a 20  ED_LOCK)).  ){. 
fc060 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 33 3b 0a     int cnt = 3;.
fc070 20 20 20 20 77 68 69 6c 65 28 20 63 6e 74 2d 2d      while( cnt--
fc080 3e 30 20 26 26 20 28 72 65 73 20 3d 20 4c 6f 63  >0 && (res = Loc
fc090 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  kFile(pFile->h, 
fc0a0 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c  PENDING_BYTE, 0,
fc0b0 20 31 2c 20 30 29 29 3d 3d 30 20 29 7b 0a 20 20   1, 0))==0 ){.  
fc0c0 20 20 20 20 2f 2a 20 54 72 79 20 33 20 74 69 6d      /* Try 3 tim
fc0d0 65 73 20 74 6f 20 67 65 74 20 74 68 65 20 70 65  es to get the pe
fc0e0 6e 64 69 6e 67 20 6c 6f 63 6b 2e 20 20 54 68 65  nding lock.  The
fc0f0 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 6d 69   pending lock mi
fc100 67 68 74 20 62 65 0a 20 20 20 20 20 20 2a 2a 20  ght be.      ** 
fc110 68 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 20  held by another 
fc120 72 65 61 64 65 72 20 70 72 6f 63 65 73 73 20 77  reader process w
fc130 68 6f 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20  ho will release 
fc140 69 74 20 6d 6f 6d 65 6e 74 61 72 69 6c 79 2e 0a  it momentarily..
fc150 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 4f        */.      O
fc160 53 54 52 41 43 45 32 28 22 63 6f 75 6c 64 20 6e  STRACE2("could n
fc170 6f 74 20 67 65 74 20 61 20 50 45 4e 44 49 4e 47  ot get a PENDING
fc180 20 6c 6f 63 6b 2e 20 63 6e 74 3d 25 64 5c 6e 22   lock. cnt=%d\n"
fc190 2c 20 63 6e 74 29 3b 0a 20 20 20 20 20 20 53 6c  , cnt);.      Sl
fc1a0 65 65 70 28 31 29 3b 0a 20 20 20 20 7d 0a 20 20  eep(1);.    }.  
fc1b0 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b    gotPendingLock
fc1c0 20 3d 20 72 65 73 3b 0a 20 20 20 20 69 66 28 20   = res;.    if( 
fc1d0 21 72 65 73 20 29 7b 0a 20 20 20 20 20 20 65 72  !res ){.      er
fc1e0 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72  ror = GetLastErr
fc1f0 6f 72 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  or();.    }.  }.
fc200 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20  .  /* Acquire a 
fc210 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20 20 2a 2f  shared lock.  */
fc220 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  .  if( locktype=
fc230 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20  =SHARED_LOCK && 
fc240 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65 72  res ){.    asser
fc250 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  t( pFile->lockty
fc260 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20  pe==NO_LOCK );. 
fc270 20 20 20 72 65 73 20 3d 20 67 65 74 52 65 61 64     res = getRead
fc280 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20  Lock(pFile);.   
fc290 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20   if( res ){.    
fc2a0 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20    newLocktype = 
fc2b0 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20  SHARED_LOCK;.   
fc2c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 72   }else{.      er
fc2d0 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72  ror = GetLastErr
fc2e0 6f 72 28 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  or();.    }.  }.
fc2f0 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20  .  /* Acquire a 
fc300 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 0a 20 20  RESERVED lock.  
fc310 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  */.  if( locktyp
fc320 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e==RESERVED_LOCK
fc330 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 61   && res ){.    a
fc340 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f  ssert( pFile->lo
fc350 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
fc360 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d  OCK );.    res =
fc370 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d   LockFile(pFile-
fc380 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54  >h, RESERVED_BYT
fc390 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20  E, 0, 1, 0);.   
fc3a0 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20   if( res ){.    
fc3b0 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20    newLocktype = 
fc3c0 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20  RESERVED_LOCK;. 
fc3d0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
fc3e0 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45  error = GetLastE
fc3f0 72 72 6f 72 28 29 3b 0a 20 20 20 20 7d 0a 20 20  rror();.    }.  
fc400 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20  }..  /* Acquire 
fc410 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 0a 20  a PENDING lock. 
fc420 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79   */.  if( lockty
fc430 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe==EXCLUSIVE_LO
fc440 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20  CK && res ){.   
fc450 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 50   newLocktype = P
fc460 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20  ENDING_LOCK;.   
fc470 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20   gotPendingLock 
fc480 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  = 0;.  }..  /* A
fc490 63 71 75 69 72 65 20 61 6e 20 45 58 43 4c 55 53  cquire an EXCLUS
fc4a0 49 56 45 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20  IVE lock.  */.  
fc4b0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58  if( locktype==EX
fc4c0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20  CLUSIVE_LOCK && 
fc4d0 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65 72  res ){.    asser
fc4e0 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  t( pFile->lockty
fc4f0 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe>=SHARED_LOCK 
fc500 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 75 6e 6c  );.    res = unl
fc510 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c  ockReadLock(pFil
fc520 65 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 32  e);.    OSTRACE2
fc530 28 22 75 6e 72 65 61 64 6c 6f 63 6b 20 3d 20 25  ("unreadlock = %
fc540 64 5c 6e 22 2c 20 72 65 73 29 3b 0a 20 20 20 20  d\n", res);.    
fc550 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70  res = LockFile(p
fc560 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f  File->h, SHARED_
fc570 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52 45 44  FIRST, 0, SHARED
fc580 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20 69  _SIZE, 0);.    i
fc590 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20  f( res ){.      
fc5a0 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 45 58  newLocktype = EX
fc5b0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20  CLUSIVE_LOCK;.  
fc5c0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65    }else{.      e
fc5d0 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72  rror = GetLastEr
fc5e0 72 6f 72 28 29 3b 0a 20 20 20 20 20 20 4f 53 54  ror();.      OST
fc5f0 52 41 43 45 32 28 22 65 72 72 6f 72 2d 63 6f 64  RACE2("error-cod
fc600 65 20 3d 20 25 64 5c 6e 22 2c 20 65 72 72 6f 72  e = %d\n", error
fc610 29 3b 0a 20 20 20 20 20 20 67 65 74 52 65 61 64  );.      getRead
fc620 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20  Lock(pFile);.   
fc630 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
fc640 77 65 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 61  we are holding a
fc650 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68   PENDING lock th
fc660 61 74 20 6f 75 67 68 74 20 74 6f 20 62 65 20 72  at ought to be r
fc670 65 6c 65 61 73 65 64 2c 20 74 68 65 6e 0a 20 20  eleased, then.  
fc680 2a 2a 20 72 65 6c 65 61 73 65 20 69 74 20 6e 6f  ** release it no
fc690 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 67 6f  w..  */.  if( go
fc6a0 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 26 26 20  tPendingLock && 
fc6b0 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
fc6c0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c  _LOCK ){.    Unl
fc6d0 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68  ockFile(pFile->h
fc6e0 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20  , PENDING_BYTE, 
fc6f0 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20  0, 1, 0);.  }.. 
fc700 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 73   /* Update the s
fc710 74 61 74 65 20 6f 66 20 74 68 65 20 6c 6f 63 6b  tate of the lock
fc720 20 68 61 73 20 68 65 6c 64 20 69 6e 20 74 68 65   has held in the
fc730 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
fc740 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 74 75 72   then.  ** retur
fc750 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  n the appropriat
fc760 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a 20  e result code.. 
fc770 20 2a 2f 0a 20 20 69 66 28 20 72 65 73 20 29 7b   */.  if( res ){
fc780 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
fc790 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  _OK;.  }else{.  
fc7a0 20 20 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b    OSTRACE4("LOCK
fc7b0 20 46 41 49 4c 45 44 20 25 64 20 74 72 79 69 6e   FAILED %d tryin
fc7c0 67 20 66 6f 72 20 25 64 20 62 75 74 20 67 6f 74  g for %d but got
fc7d0 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68   %d\n", pFile->h
fc7e0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63  ,.           loc
fc7f0 6b 74 79 70 65 2c 20 6e 65 77 4c 6f 63 6b 74 79  ktype, newLockty
fc800 70 65 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  pe);.    pFile->
fc810 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f  lastErrno = erro
fc820 72 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  r;.    rc = SQLI
fc830 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70  TE_BUSY;.  }.  p
fc840 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
fc850 20 28 75 38 29 6e 65 77 4c 6f 63 6b 74 79 70 65   (u8)newLocktype
fc860 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
fc870 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
fc880 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74  tine checks if t
fc890 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56  here is a RESERV
fc8a0 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20  ED lock held on 
fc8b0 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  the specified.**
fc8c0 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72   file by this or
fc8d0 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65   any other proce
fc8e0 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f  ss. If such a lo
fc8f0 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65 74 75  ck is held, retu
fc900 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  rn.** non-zero, 
fc910 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f 2e 0a  otherwise zero..
fc920 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
fc930 6e 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  nCheckReservedLo
fc940 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
fc950 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75  *id, int *pResOu
fc960 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  t){.  int rc;.  
fc970 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  winFile *pFile =
fc980 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 0a   (winFile*)id;..
fc990 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20    assert( id!=0 
fc9a0 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  );.  if( pFile->
fc9b0 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 56  locktype>=RESERV
fc9c0 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72  ED_LOCK ){.    r
fc9d0 63 20 3d 20 31 3b 0a 20 20 20 20 4f 53 54 52 41  c = 1;.    OSTRA
fc9e0 43 45 33 28 22 54 45 53 54 20 57 52 2d 4c 4f 43  CE3("TEST WR-LOC
fc9f0 4b 20 25 64 20 25 64 20 28 6c 6f 63 61 6c 29 5c  K %d %d (local)\
fca00 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63  n", pFile->h, rc
fca10 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
fca20 72 63 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46  rc = LockFile(pF
fca30 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44  ile->h, RESERVED
fca40 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b  _BYTE, 0, 1, 0);
fca50 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20  .    if( rc ){. 
fca60 20 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28       UnlockFile(
fca70 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56  pFile->h, RESERV
fca80 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30  ED_BYTE, 0, 1, 0
fca90 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  );.    }.    rc 
fcaa0 3d 20 21 72 63 3b 0a 20 20 20 20 4f 53 54 52 41  = !rc;.    OSTRA
fcab0 43 45 33 28 22 54 45 53 54 20 57 52 2d 4c 4f 43  CE3("TEST WR-LOC
fcac0 4b 20 25 64 20 25 64 20 28 72 65 6d 6f 74 65 29  K %d %d (remote)
fcad0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
fcae0 63 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 4f  c);.  }.  *pResO
fcaf0 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 75 72  ut = rc;.  retur
fcb00 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
fcb10 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20  /*.** Lower the 
fcb20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e  locking level on
fcb30 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
fcb40 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e   id to locktype.
fcb50 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75    locktype.** mu
fcb60 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f  st be either NO_
fcb70 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c  LOCK or SHARED_L
fcb80 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  OCK..**.** If th
fcb90 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  e locking level 
fcba0 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  of the file desc
fcbb0 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64  riptor is alread
fcbc0 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a  y at or below.**
fcbd0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c   the requested l
fcbe0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68  ocking level, th
fcbf0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
fcc00 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20  no-op..**.** It 
fcc10 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
fcc20 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  for this routine
fcc30 20 74 6f 20 66 61 69 6c 20 69 66 20 74 68 65 20   to fail if the 
fcc40 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a  second argument.
fcc50 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20  ** is NO_LOCK.  
fcc60 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  If the second ar
fcc70 67 75 6d 65 6e 74 20 69 73 20 53 48 41 52 45 44  gument is SHARED
fcc80 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 73 20  _LOCK then this 
fcc90 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68 74  routine.** might
fcca0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
fccb0 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 69 63 20  OERR;.*/.static 
fccc0 69 6e 74 20 77 69 6e 55 6e 6c 6f 63 6b 28 73 71  int winUnlock(sq
fccd0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
fcce0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20  int locktype){. 
fccf0 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 77 69 6e   int type;.  win
fcd00 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77  File *pFile = (w
fcd10 69 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e  inFile*)id;.  in
fcd20 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
fcd30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ;.  assert( pFil
fcd40 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  e!=0 );.  assert
fcd50 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52  ( locktype<=SHAR
fcd60 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 4f 53 54  ED_LOCK );.  OST
fcd70 52 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 25 64  RACE5("UNLOCK %d
fcd80 20 74 6f 20 25 64 20 77 61 73 20 25 64 28 25 64   to %d was %d(%d
fcd90 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  )\n", pFile->h, 
fcda0 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20  locktype,.      
fcdb0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
fcdc0 79 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72  ype, pFile->shar
fcdd0 65 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 20 20 74  edLockByte);.  t
fcde0 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63  ype = pFile->loc
fcdf0 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74 79 70  ktype;.  if( typ
fce00 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  e>=EXCLUSIVE_LOC
fce10 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46  K ){.    UnlockF
fce20 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48  ile(pFile->h, SH
fce30 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 53  ARED_FIRST, 0, S
fce40 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a  HARED_SIZE, 0);.
fce50 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65      if( locktype
fce60 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26  ==SHARED_LOCK &&
fce70 20 21 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46   !getReadLock(pF
fce80 69 6c 65 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a  ile) ){.      /*
fce90 20 54 68 69 73 20 73 68 6f 75 6c 64 20 6e 65 76   This should nev
fcea0 65 72 20 68 61 70 70 65 6e 2e 20 20 57 65 20 73  er happen.  We s
fceb0 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20  hould always be 
fcec0 61 62 6c 65 20 74 6f 0a 20 20 20 20 20 20 2a 2a  able to.      **
fced0 20 72 65 61 63 71 75 69 72 65 20 74 68 65 20 72   reacquire the r
fcee0 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20  ead lock */.    
fcef0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
fcf00 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20  ERR_UNLOCK;.    
fcf10 7d 0a 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65  }.  }.  if( type
fcf20 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  >=RESERVED_LOCK 
fcf30 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c  ){.    UnlockFil
fcf40 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45  e(pFile->h, RESE
fcf50 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c  RVED_BYTE, 0, 1,
fcf60 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6c   0);.  }.  if( l
fcf70 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b  ocktype==NO_LOCK
fcf80 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 45 44   && type>=SHARED
fcf90 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 75 6e 6c  _LOCK ){.    unl
fcfa0 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c  ockReadLock(pFil
fcfb0 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 74 79  e);.  }.  if( ty
fcfc0 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  pe>=PENDING_LOCK
fcfd0 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69   ){.    UnlockFi
fcfe0 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e  le(pFile->h, PEN
fcff0 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c  DING_BYTE, 0, 1,
fd000 20 30 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65   0);.  }.  pFile
fd010 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 28 75 38  ->locktype = (u8
fd020 29 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74  )locktype;.  ret
fd030 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
fd040 20 43 6f 6e 74 72 6f 6c 20 61 6e 64 20 71 75 65   Control and que
fd050 72 79 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66  ry of the open f
fd060 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73  ile handle..*/.s
fd070 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46 69 6c  tatic int winFil
fd080 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33  eControl(sqlite3
fd090 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f  _file *id, int o
fd0a0 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a  p, void *pArg){.
fd0b0 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a    switch( op ){.
fd0c0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
fd0d0 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a  FCNTL_LOCKSTATE:
fd0e0 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29   {.      *(int*)
fd0f0 70 41 72 67 20 3d 20 28 28 77 69 6e 46 69 6c 65  pArg = ((winFile
fd100 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b  *)id)->locktype;
fd110 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
fd120 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
fd130 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4c     case SQLITE_L
fd140 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20 20  AST_ERRNO: {.   
fd150 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d     *(int*)pArg =
fd160 20 28 69 6e 74 29 28 28 77 69 6e 46 69 6c 65 2a   (int)((winFile*
fd170 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b  )id)->lastErrno;
fd180 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
fd190 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
fd1a0 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
fd1b0 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a  TE_ERROR;.}../*.
fd1c0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 65  ** Return the se
fd1d0 63 74 6f 72 20 73 69 7a 65 20 69 6e 20 62 79 74  ctor size in byt
fd1e0 65 73 20 6f 66 20 74 68 65 20 75 6e 64 65 72 6c  es of the underl
fd1f0 79 69 6e 67 20 62 6c 6f 63 6b 20 64 65 76 69 63  ying block devic
fd200 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65  e for.** the spe
fd210 63 69 66 69 65 64 20 66 69 6c 65 2e 20 54 68 69  cified file. Thi
fd220 73 20 69 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61  s is almost alwa
fd230 79 73 20 35 31 32 20 62 79 74 65 73 2c 20 62 75  ys 512 bytes, bu
fd240 74 20 6d 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67  t may be.** larg
fd250 65 72 20 66 6f 72 20 73 6f 6d 65 20 64 65 76 69  er for some devi
fd260 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  ces..**.** SQLit
fd270 65 20 63 6f 64 65 20 61 73 73 75 6d 65 73 20 74  e code assumes t
fd280 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e  his function can
fd290 6e 6f 74 20 66 61 69 6c 2e 20 49 74 20 61 6c 73  not fail. It als
fd2a0 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a 2a  o assumes that.*
fd2b0 2a 20 69 66 20 74 77 6f 20 66 69 6c 65 73 20 61  * if two files a
fd2c0 72 65 20 63 72 65 61 74 65 64 20 69 6e 20 74 68  re created in th
fd2d0 65 20 73 61 6d 65 20 66 69 6c 65 2d 73 79 73 74  e same file-syst
fd2e0 65 6d 20 64 69 72 65 63 74 6f 72 79 20 28 69 2e  em directory (i.
fd2f0 65 2e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65  e..** a database
fd300 20 61 6e 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c   and its journal
fd310 20 66 69 6c 65 29 20 74 68 61 74 20 74 68 65 20   file) that the 
fd320 73 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c  sector size will
fd330 20 62 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20   be the.** same 
fd340 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61  for both..*/.sta
fd350 74 69 63 20 69 6e 74 20 77 69 6e 53 65 63 74 6f  tic int winSecto
fd360 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69  rSize(sqlite3_fi
fd370 6c 65 20 2a 69 64 29 7b 0a 20 20 61 73 73 65 72  le *id){.  asser
fd380 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 72 65  t( id!=0 );.  re
fd390 74 75 72 6e 20 28 69 6e 74 29 28 28 28 77 69 6e  turn (int)(((win
fd3a0 46 69 6c 65 2a 29 69 64 29 2d 3e 73 65 63 74 6f  File*)id)->secto
fd3b0 72 53 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rSize);.}../*.**
fd3c0 20 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72   Return a vector
fd3d0 20 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 61   of device chara
fd3e0 63 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73  cteristics..*/.s
fd3f0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65 76  tatic int winDev
fd400 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
fd410 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  cs(sqlite3_file 
fd420 2a 69 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  *id){.  UNUSED_P
fd430 41 52 41 4d 45 54 45 52 28 69 64 29 3b 0a 20 20  ARAMETER(id);.  
fd440 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
fd450 2a 2a 20 54 68 69 73 20 76 65 63 74 6f 72 20 64  ** This vector d
fd460 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d  efines all the m
fd470 65 74 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20  ethods that can 
fd480 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a  operate on an.**
fd490 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 66 6f   sqlite3_file fo
fd4a0 72 20 77 69 6e 33 32 2e 0a 2a 2f 0a 73 74 61 74  r win32..*/.stat
fd4b0 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
fd4c0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 77 69 6e 49  _io_methods winI
fd4d0 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c  oMethod = {.  1,
fd4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd4f0 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73          /* iVers
fd500 69 6f 6e 20 2a 2f 0a 20 20 77 69 6e 43 6c 6f 73  ion */.  winClos
fd510 65 2c 0a 20 20 77 69 6e 52 65 61 64 2c 0a 20 20  e,.  winRead,.  
fd520 77 69 6e 57 72 69 74 65 2c 0a 20 20 77 69 6e 54  winWrite,.  winT
fd530 72 75 6e 63 61 74 65 2c 0a 20 20 77 69 6e 53 79  runcate,.  winSy
fd540 6e 63 2c 0a 20 20 77 69 6e 46 69 6c 65 53 69 7a  nc,.  winFileSiz
fd550 65 2c 0a 20 20 77 69 6e 4c 6f 63 6b 2c 0a 20 20  e,.  winLock,.  
fd560 77 69 6e 55 6e 6c 6f 63 6b 2c 0a 20 20 77 69 6e  winUnlock,.  win
fd570 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
fd580 6b 2c 0a 20 20 77 69 6e 46 69 6c 65 43 6f 6e 74  k,.  winFileCont
fd590 72 6f 6c 2c 0a 20 20 77 69 6e 53 65 63 74 6f 72  rol,.  winSector
fd5a0 53 69 7a 65 2c 0a 20 20 77 69 6e 44 65 76 69 63  Size,.  winDevic
fd5b0 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
fd5c0 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .};../**********
fd5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd5f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd610 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 74  *.** Here ends t
fd620 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 74  he I/O methods t
fd630 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73 71 6c  hat form the sql
fd640 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
fd650 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  object..**.** Th
fd660 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20  e next block of 
fd670 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  code implements 
fd680 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64 73 2e  the VFS methods.
fd690 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
fd6a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd6b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
fd6e0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20  /*.** Convert a 
fd6f0 55 54 46 2d 38 20 66 69 6c 65 6e 61 6d 65 20 69  UTF-8 filename i
fd700 6e 74 6f 20 77 68 61 74 65 76 65 72 20 66 6f 72  nto whatever for
fd710 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  m the underlying
fd720 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 73 79  .** operating sy
fd730 73 74 65 6d 20 77 61 6e 74 73 20 66 69 6c 65 6e  stem wants filen
fd740 61 6d 65 73 20 69 6e 2e 20 20 53 70 61 63 65 20  ames in.  Space 
fd750 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73 75  to hold the resu
fd760 6c 74 0a 2a 2a 20 69 73 20 6f 62 74 61 69 6e 65  lt.** is obtaine
fd770 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e  d from malloc an
fd780 64 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 20  d must be freed 
fd790 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a  by the calling.*
fd7a0 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  * function..*/.s
fd7b0 74 61 74 69 63 20 76 6f 69 64 20 2a 63 6f 6e 76  tatic void *conv
fd7c0 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28  ertUtf8Filename(
fd7d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
fd7e0 65 6e 61 6d 65 29 7b 0a 20 20 76 6f 69 64 20 2a  ename){.  void *
fd7f0 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 30 3b 0a  zConverted = 0;.
fd800 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a    if( isNT() ){.
fd810 20 20 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d      zConverted =
fd820 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 7a   utf8ToUnicode(z
fd830 46 69 6c 65 6e 61 6d 65 29 3b 0a 2f 2a 20 69 73  Filename);./* is
fd840 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c  NT() is 1 if SQL
fd850 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c  ITE_OS_WINCE==1,
fd860 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73   so this else is
fd870 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e   never executed.
fd880 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f   .*/.#if SQLITE_
fd890 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65  OS_WINCE==0.  }e
fd8a0 6c 73 65 7b 0a 20 20 20 20 7a 43 6f 6e 76 65 72  lse{.    zConver
fd8b0 74 65 64 20 3d 20 75 74 66 38 54 6f 4d 62 63 73  ted = utf8ToMbcs
fd8c0 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e  (zFilename);.#en
fd8d0 64 69 66 0a 20 20 7d 0a 20 20 2f 2a 20 63 61 6c  dif.  }.  /* cal
fd8e0 6c 65 72 20 77 69 6c 6c 20 68 61 6e 64 6c 65 20  ler will handle 
fd8f0 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20 2a 2f  out of memory */
fd900 0a 20 20 72 65 74 75 72 6e 20 7a 43 6f 6e 76 65  .  return zConve
fd910 72 74 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  rted;.}../*.** C
fd920 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72  reate a temporar
fd930 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a  y file name in z
fd940 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20  Buf.  zBuf must 
fd950 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f  be big enough to
fd960 0a 2a 2a 20 68 6f 6c 64 20 61 74 20 70 56 66 73  .** hold at pVfs
fd970 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 63 68 61  ->mxPathname cha
fd980 72 61 63 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74  racters..*/.stat
fd990 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e 61  ic int getTempna
fd9a0 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61  me(int nBuf, cha
fd9b0 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 74  r *zBuf){.  stat
fd9c0 69 63 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d  ic char zChars[]
fd9d0 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66 67 68   =.    "abcdefgh
fd9e0 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78  ijklmnopqrstuvwx
fd9f0 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45 46 47  yz".    "ABCDEFG
fda00 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57  HIJKLMNOPQRSTUVW
fda10 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35  XYZ".    "012345
fda20 36 37 38 39 22 3b 0a 20 20 73 69 7a 65 5f 74 20  6789";.  size_t 
fda30 69 2c 20 6a 3b 0a 20 20 63 68 61 72 20 7a 54 65  i, j;.  char zTe
fda40 6d 70 50 61 74 68 5b 4d 41 58 5f 50 41 54 48 2b  mpPath[MAX_PATH+
fda50 31 5d 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  1];.  if( sqlite
fda60 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79  3_temp_directory
fda70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
fda80 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54  snprintf(MAX_PAT
fda90 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c  H-30, zTempPath,
fdaa0 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 74   "%s", sqlite3_t
fdab0 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 29 3b 0a  emp_directory);.
fdac0 20 20 7d 65 6c 73 65 20 69 66 28 20 69 73 4e 54    }else if( isNT
fdad0 28 29 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  () ){.    char *
fdae0 7a 4d 75 6c 74 69 3b 0a 20 20 20 20 57 43 48 41  zMulti;.    WCHA
fdaf0 52 20 7a 57 69 64 65 50 61 74 68 5b 4d 41 58 5f  R zWidePath[MAX_
fdb00 50 41 54 48 5d 3b 0a 20 20 20 20 47 65 74 54 65  PATH];.    GetTe
fdb10 6d 70 50 61 74 68 57 28 4d 41 58 5f 50 41 54 48  mpPathW(MAX_PATH
fdb20 2d 33 30 2c 20 7a 57 69 64 65 50 61 74 68 29 3b  -30, zWidePath);
fdb30 0a 20 20 20 20 7a 4d 75 6c 74 69 20 3d 20 75 6e  .    zMulti = un
fdb40 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 57 69 64  icodeToUtf8(zWid
fdb50 65 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20  ePath);.    if( 
fdb60 7a 4d 75 6c 74 69 20 29 7b 0a 20 20 20 20 20 20  zMulti ){.      
fdb70 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
fdb80 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54  (MAX_PATH-30, zT
fdb90 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a  empPath, "%s", z
fdba0 4d 75 6c 74 69 29 3b 0a 20 20 20 20 20 20 66 72  Multi);.      fr
fdbb0 65 65 28 7a 4d 75 6c 74 69 29 3b 0a 20 20 20 20  ee(zMulti);.    
fdbc0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74  }else{.      ret
fdbd0 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
fdbe0 3b 0a 20 20 20 20 7d 0a 2f 2a 20 69 73 4e 54 28  ;.    }./* isNT(
fdbf0 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45  ) is 1 if SQLITE
fdc00 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f  _OS_WINCE==1, so
fdc10 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65   this else is ne
fdc20 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a  ver executed. .*
fdc30 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53 43 49  * Since the ASCI
fdc40 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  I version of the
fdc50 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64  se Windows API d
fdc60 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20  o not exist for 
fdc70 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69  WINCE,.** it's i
fdc80 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20  mportant to not 
fdc90 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66  reference them f
fdca0 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e  or WINCE builds.
fdcb0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  .*/.#if SQLITE_O
fdcc0 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c  S_WINCE==0.  }el
fdcd0 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 55  se{.    char *zU
fdce0 74 66 38 3b 0a 20 20 20 20 63 68 61 72 20 7a 4d  tf8;.    char zM
fdcf0 62 63 73 50 61 74 68 5b 4d 41 58 5f 50 41 54 48  bcsPath[MAX_PATH
fdd00 5d 3b 0a 20 20 20 20 47 65 74 54 65 6d 70 50 61  ];.    GetTempPa
fdd10 74 68 41 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c  thA(MAX_PATH-30,
fdd20 20 7a 4d 62 63 73 50 61 74 68 29 3b 0a 20 20 20   zMbcsPath);.   
fdd30 20 7a 55 74 66 38 20 3d 20 73 71 6c 69 74 65 33   zUtf8 = sqlite3
fdd40 5f 77 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75  _win32_mbcs_to_u
fdd50 74 66 38 28 7a 4d 62 63 73 50 61 74 68 29 3b 0a  tf8(zMbcsPath);.
fdd60 20 20 20 20 69 66 28 20 7a 55 74 66 38 20 29 7b      if( zUtf8 ){
fdd70 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
fdd80 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48  nprintf(MAX_PATH
fdd90 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20  -30, zTempPath, 
fdda0 22 25 73 22 2c 20 7a 55 74 66 38 29 3b 0a 20 20  "%s", zUtf8);.  
fddb0 20 20 20 20 66 72 65 65 28 7a 55 74 66 38 29 3b      free(zUtf8);
fddc0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
fddd0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
fdde0 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 23 65 6e  NOMEM;.    }.#en
fddf0 64 69 66 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  dif.  }.  for(i=
fde00 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
fde10 7a 54 65 6d 70 50 61 74 68 29 3b 20 69 3e 30 20  zTempPath); i>0 
fde20 26 26 20 7a 54 65 6d 70 50 61 74 68 5b 69 2d 31  && zTempPath[i-1
fde30 5d 3d 3d 27 5c 5c 27 3b 20 69 2d 2d 29 7b 7d 0a  ]=='\\'; i--){}.
fde40 20 20 7a 54 65 6d 70 50 61 74 68 5b 69 5d 20 3d    zTempPath[i] =
fde50 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e   0;.  sqlite3_sn
fde60 70 72 69 6e 74 66 28 6e 42 75 66 2d 33 30 2c 20  printf(nBuf-30, 
fde70 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20 20  zBuf,.          
fde80 20 20 20 20 20 20 20 20 20 22 25 73 5c 5c 22 53           "%s\\"S
fde90 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f  QLITE_TEMP_FILE_
fdea0 50 52 45 46 49 58 2c 20 7a 54 65 6d 70 50 61 74  PREFIX, zTempPat
fdeb0 68 29 3b 0a 20 20 6a 20 3d 20 73 71 6c 69 74 65  h);.  j = sqlite
fdec0 33 53 74 72 6c 65 6e 33 30 28 7a 42 75 66 29 3b  3Strlen30(zBuf);
fded0 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f  .  sqlite3_rando
fdee0 6d 6e 65 73 73 28 32 30 2c 20 26 7a 42 75 66 5b  mness(20, &zBuf[
fdef0 6a 5d 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  j]);.  for(i=0; 
fdf00 69 3c 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b  i<20; i++, j++){
fdf10 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28  .    zBuf[j] = (
fdf20 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75  char)zChars[ ((u
fdf30 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75  nsigned char)zBu
fdf40 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43  f[j])%(sizeof(zC
fdf50 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a  hars)-1) ];.  }.
fdf60 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20    zBuf[j] = 0;. 
fdf70 20 4f 53 54 52 41 43 45 32 28 22 54 45 4d 50 20   OSTRACE2("TEMP 
fdf80 46 49 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c  FILENAME: %s\n",
fdf90 20 7a 42 75 66 29 3b 0a 20 20 72 65 74 75 72 6e   zBuf);.  return
fdfa0 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a   SQLITE_OK; .}..
fdfb0 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  /*.** The return
fdfc0 20 76 61 6c 75 65 20 6f 66 20 67 65 74 4c 61 73   value of getLas
fdfd0 74 45 72 72 6f 72 4d 73 67 0a 2a 2a 20 69 73 20  tErrorMsg.** is 
fdfe0 7a 65 72 6f 20 69 66 20 74 68 65 20 65 72 72 6f  zero if the erro
fdff0 72 20 6d 65 73 73 61 67 65 20 66 69 74 73 20 69  r message fits i
fe000 6e 20 74 68 65 20 62 75 66 66 65 72 2c 20 6f 72  n the buffer, or
fe010 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68   non-zero.** oth
fe020 65 72 77 69 73 65 20 28 69 66 20 74 68 65 20 6d  erwise (if the m
fe030 65 73 73 61 67 65 20 77 61 73 20 74 72 75 6e 63  essage was trunc
fe040 61 74 65 64 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  ated)..*/.static
fe050 20 69 6e 74 20 67 65 74 4c 61 73 74 45 72 72 6f   int getLastErro
fe060 72 4d 73 67 28 69 6e 74 20 6e 42 75 66 2c 20 63  rMsg(int nBuf, c
fe070 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 44 57  har *zBuf){.  DW
fe080 4f 52 44 20 65 72 72 6f 72 20 3d 20 47 65 74 4c  ORD error = GetL
fe090 61 73 74 45 72 72 6f 72 28 29 3b 0a 0a 23 69 66  astError();..#if
fe0a0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
fe0b0 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  .  sqlite3_snpri
fe0c0 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66 2c 20  ntf(nBuf, zBuf, 
fe0d0 22 4f 73 45 72 72 6f 72 20 30 78 25 78 20 28 25  "OsError 0x%x (%
fe0e0 75 29 22 2c 20 65 72 72 6f 72 2c 20 65 72 72 6f  u)", error, erro
fe0f0 72 29 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 46  r);.#else.  /* F
fe100 6f 72 6d 61 74 4d 65 73 73 61 67 65 20 72 65 74  ormatMessage ret
fe110 75 72 6e 73 20 30 20 6f 6e 20 66 61 69 6c 75 72  urns 0 on failur
fe120 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69 74  e.  Otherwise it
fe130 0a 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 74 68  .  ** returns th
fe140 65 20 6e 75 6d 62 65 72 20 6f 66 20 54 43 48 41  e number of TCHA
fe150 52 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  Rs written to th
fe160 65 20 6f 75 74 70 75 74 0a 20 20 2a 2a 20 62 75  e output.  ** bu
fe170 66 66 65 72 2c 20 65 78 63 6c 75 64 69 6e 67 20  ffer, excluding 
fe180 74 68 65 20 74 65 72 6d 69 6e 61 74 69 6e 67 20  the terminating 
fe190 6e 75 6c 6c 20 63 68 61 72 2e 0a 20 20 2a 2f 0a  null char..  */.
fe1a0 20 20 69 66 20 28 21 46 6f 72 6d 61 74 4d 65 73    if (!FormatMes
fe1b0 73 61 67 65 41 28 46 4f 52 4d 41 54 5f 4d 45 53  sageA(FORMAT_MES
fe1c0 53 41 47 45 5f 46 52 4f 4d 5f 53 59 53 54 45 4d  SAGE_FROM_SYSTEM
fe1d0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
fe1e0 20 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20          NULL,.  
fe1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe200 20 20 20 20 65 72 72 6f 72 2c 0a 20 20 20 20 20      error,.     
fe210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe220 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
fe230 20 20 20 20 20 20 20 20 20 20 7a 42 75 66 2c 0a            zBuf,.
fe240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe250 20 20 20 20 20 20 6e 42 75 66 2d 31 2c 0a 20 20        nBuf-1,.  
fe260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe270 20 20 20 20 30 29 29 0a 20 20 7b 0a 20 20 20 20      0)).  {.    
fe280 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
fe290 28 6e 42 75 66 2c 20 7a 42 75 66 2c 20 22 4f 73  (nBuf, zBuf, "Os
fe2a0 45 72 72 6f 72 20 30 78 25 78 20 28 25 75 29 22  Error 0x%x (%u)"
fe2b0 2c 20 65 72 72 6f 72 2c 20 65 72 72 6f 72 29 3b  , error, error);
fe2c0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72  .  }.#endif..  r
fe2d0 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
fe2e0 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 2e 0a 2a  * Open a file..*
fe2f0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  /.static int win
fe300 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f  Open(.  sqlite3_
fe310 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20  vfs *pVfs,      
fe320 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f    /* Not used */
fe330 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
fe340 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  Name,        /* 
fe350 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65  Name of the file
fe360 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71   (UTF-8) */.  sq
fe370 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
fe380 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
fe390 20 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65   the SQLite file
fe3a0 20 68 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a   handle here */.
fe3b0 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20    int flags,    
fe3c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
fe3d0 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20 2a  pen mode flags *
fe3e0 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61  /.  int *pOutFla
fe3f0 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  gs            /*
fe400 20 53 74 61 74 75 73 20 72 65 74 75 72 6e 20 66   Status return f
fe410 6c 61 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 41 4e  lags */.){.  HAN
fe420 44 4c 45 20 68 3b 0a 20 20 44 57 4f 52 44 20 64  DLE h;.  DWORD d
fe430 77 44 65 73 69 72 65 64 41 63 63 65 73 73 3b 0a  wDesiredAccess;.
fe440 20 20 44 57 4f 52 44 20 64 77 53 68 61 72 65 4d    DWORD dwShareM
fe450 6f 64 65 3b 0a 20 20 44 57 4f 52 44 20 64 77 43  ode;.  DWORD dwC
fe460 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69  reationDispositi
fe470 6f 6e 3b 0a 20 20 44 57 4f 52 44 20 64 77 46 6c  on;.  DWORD dwFl
fe480 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73  agsAndAttributes
fe490 20 3d 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 45   = 0;.#if SQLITE
fe4a0 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 69 6e 74 20  _OS_WINCE.  int 
fe4b0 69 73 54 65 6d 70 20 3d 20 30 3b 0a 23 65 6e 64  isTemp = 0;.#end
fe4c0 69 66 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46  if.  winFile *pF
fe4d0 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29  ile = (winFile*)
fe4e0 69 64 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e  id;.  void *zCon
fe4f0 76 65 72 74 65 64 3b 20 20 20 20 20 20 20 20 20  verted;         
fe500 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 6e          /* Filen
fe510 61 6d 65 20 69 6e 20 4f 53 20 65 6e 63 6f 64 69  ame in OS encodi
fe520 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ng */.  const ch
fe530 61 72 20 2a 7a 55 74 66 38 4e 61 6d 65 20 3d 20  ar *zUtf8Name = 
fe540 7a 4e 61 6d 65 3b 20 20 20 20 2f 2a 20 46 69 6c  zName;    /* Fil
fe550 65 6e 61 6d 65 20 69 6e 20 55 54 46 2d 38 20 65  ename in UTF-8 e
fe560 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61  ncoding */.  cha
fe570 72 20 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50  r zTmpname[MAX_P
fe580 41 54 48 2b 31 5d 3b 20 20 20 20 20 20 20 20 2f  ATH+1];        /
fe590 2a 20 42 75 66 66 65 72 20 75 73 65 64 20 74 6f  * Buffer used to
fe5a0 20 63 72 65 61 74 65 20 74 65 6d 70 20 66 69 6c   create temp fil
fe5b0 65 6e 61 6d 65 20 2a 2f 0a 0a 20 20 61 73 73 65  ename */..  asse
fe5c0 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 55  rt( id!=0 );.  U
fe5d0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
fe5e0 70 56 66 73 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  pVfs);..  /* If 
fe5f0 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
fe600 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63  ent to this func
fe610 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 65  tion is NULL, ge
fe620 6e 65 72 61 74 65 20 61 20 0a 20 20 2a 2a 20 74  nerate a .  ** t
fe630 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61  emporary file na
fe640 6d 65 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f 0a  me to use .  */.
fe650 20 20 69 66 28 20 21 7a 55 74 66 38 4e 61 6d 65    if( !zUtf8Name
fe660 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d   ){.    int rc =
fe670 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41 58   getTempname(MAX
fe680 5f 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d  _PATH+1, zTmpnam
fe690 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  e);.    if( rc!=
fe6a0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
fe6b0 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
fe6c0 20 20 7d 0a 20 20 20 20 7a 55 74 66 38 4e 61 6d    }.    zUtf8Nam
fe6d0 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20  e = zTmpname;.  
fe6e0 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20  }..  /* Convert 
fe6f0 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 6f 20  the filename to 
fe700 74 68 65 20 73 79 73 74 65 6d 20 65 6e 63 6f 64  the system encod
fe710 69 6e 67 2e 20 2a 2f 0a 20 20 7a 43 6f 6e 76 65  ing. */.  zConve
fe720 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74  rted = convertUt
fe730 66 38 46 69 6c 65 6e 61 6d 65 28 7a 55 74 66 38  f8Filename(zUtf8
fe740 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 43 6f  Name);.  if( zCo
fe750 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20  nverted==0 ){.  
fe760 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
fe770 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66  NOMEM;.  }..  if
fe780 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ( flags & SQLITE
fe790 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20  _OPEN_READWRITE 
fe7a0 29 7b 0a 20 20 20 20 64 77 44 65 73 69 72 65 64  ){.    dwDesired
fe7b0 41 63 63 65 73 73 20 3d 20 47 45 4e 45 52 49 43  Access = GENERIC
fe7c0 5f 52 45 41 44 20 7c 20 47 45 4e 45 52 49 43 5f  _READ | GENERIC_
fe7d0 57 52 49 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a  WRITE;.  }else{.
fe7e0 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63      dwDesiredAcc
fe7f0 65 73 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 45  ess = GENERIC_RE
fe800 41 44 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 51 4c  AD;.  }.  /* SQL
fe810 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49  ITE_OPEN_EXCLUSI
fe820 56 45 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61  VE is used to ma
fe830 6b 65 20 73 75 72 65 20 74 68 61 74 20 61 20 6e  ke sure that a n
fe840 65 77 20 66 69 6c 65 20 69 73 20 0a 20 20 2a 2a  ew file is .  **
fe850 20 63 72 65 61 74 65 64 2e 20 53 51 4c 69 74 65   created. SQLite
fe860 20 64 6f 65 73 6e 27 74 20 75 73 65 20 69 74 20   doesn't use it 
fe870 74 6f 20 69 6e 64 69 63 61 74 65 20 22 65 78 63  to indicate "exc
fe880 6c 75 73 69 76 65 20 61 63 63 65 73 73 22 20 0a  lusive access" .
fe890 20 20 2a 2a 20 61 73 20 69 74 20 69 73 20 75 73    ** as it is us
fe8a0 75 61 6c 6c 79 20 75 6e 64 65 72 73 74 6f 6f 64  ually understood
fe8b0 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
fe8c0 21 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  !(flags & SQLITE
fe8d0 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 29  _OPEN_EXCLUSIVE)
fe8e0 20 7c 7c 20 28 66 6c 61 67 73 20 26 20 53 51 4c   || (flags & SQL
fe8f0 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29  ITE_OPEN_CREATE)
fe900 29 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26  );.  if( flags &
fe910 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43   SQLITE_OPEN_EXC
fe920 4c 55 53 49 56 45 20 29 7b 0a 20 20 20 20 2f 2a  LUSIVE ){.    /*
fe930 20 43 72 65 61 74 65 73 20 61 20 6e 65 77 20 66   Creates a new f
fe940 69 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74 20  ile, only if it 
fe950 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  does not already
fe960 20 65 78 69 73 74 2e 20 2a 2f 0a 20 20 20 20 2f   exist. */.    /
fe970 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 65 78  * If the file ex
fe980 69 73 74 73 2c 20 69 74 20 66 61 69 6c 73 2e 20  ists, it fails. 
fe990 2a 2f 0a 20 20 20 20 64 77 43 72 65 61 74 69 6f  */.    dwCreatio
fe9a0 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20 43  nDisposition = C
fe9b0 52 45 41 54 45 5f 4e 45 57 3b 0a 20 20 7d 65 6c  REATE_NEW;.  }el
fe9c0 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 53  se if( flags & S
fe9d0 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
fe9e0 45 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 70 65 6e  E ){.    /* Open
fe9f0 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65 2c 20   existing file, 
fea00 6f 72 20 63 72 65 61 74 65 20 69 66 20 69 74 20  or create if it 
fea10 64 6f 65 73 6e 27 74 20 65 78 69 73 74 20 2a 2f  doesn't exist */
fea20 0a 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44  .    dwCreationD
fea30 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20 4f 50 45  isposition = OPE
fea40 4e 5f 41 4c 57 41 59 53 3b 0a 20 20 7d 65 6c 73  N_ALWAYS;.  }els
fea50 65 7b 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 73 20  e{.    /* Opens 
fea60 61 20 66 69 6c 65 2c 20 6f 6e 6c 79 20 69 66 20  a file, only if 
fea70 69 74 20 65 78 69 73 74 73 2e 20 2a 2f 0a 20 20  it exists. */.  
fea80 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70    dwCreationDisp
fea90 6f 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 45  osition = OPEN_E
feaa0 58 49 53 54 49 4e 47 3b 0a 20 20 7d 0a 20 20 64  XISTING;.  }.  d
feab0 77 53 68 61 72 65 4d 6f 64 65 20 3d 20 46 49 4c  wShareMode = FIL
feac0 45 5f 53 48 41 52 45 5f 52 45 41 44 20 7c 20 46  E_SHARE_READ | F
fead0 49 4c 45 5f 53 48 41 52 45 5f 57 52 49 54 45 3b  ILE_SHARE_WRITE;
feae0 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53  .  if( flags & S
feaf0 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54  QLITE_OPEN_DELET
feb00 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a 23 69 66 20  EONCLOSE ){.#if 
feb10 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a  SQLITE_OS_WINCE.
feb20 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74      dwFlagsAndAt
feb30 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f  tributes = FILE_
feb40 41 54 54 52 49 42 55 54 45 5f 48 49 44 44 45 4e  ATTRIBUTE_HIDDEN
feb50 3b 0a 20 20 20 20 69 73 54 65 6d 70 20 3d 20 31  ;.    isTemp = 1
feb60 3b 0a 23 65 6c 73 65 0a 20 20 20 20 64 77 46 6c  ;.#else.    dwFl
feb70 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73  agsAndAttributes
feb80 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54   = FILE_ATTRIBUT
feb90 45 5f 54 45 4d 50 4f 52 41 52 59 0a 20 20 20 20  E_TEMPORARY.    
feba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
febb0 20 20 20 20 20 20 20 20 20 20 20 7c 20 46 49 4c             | FIL
febc0 45 5f 41 54 54 52 49 42 55 54 45 5f 48 49 44 44  E_ATTRIBUTE_HIDD
febd0 45 4e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  EN.             
febe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
febf0 20 20 7c 20 46 49 4c 45 5f 46 4c 41 47 5f 44 45    | FILE_FLAG_DE
fec00 4c 45 54 45 5f 4f 4e 5f 43 4c 4f 53 45 3b 0a 23  LETE_ON_CLOSE;.#
fec10 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20  endif.  }else{. 
fec20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74     dwFlagsAndAtt
fec30 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f 41  ributes = FILE_A
fec40 54 54 52 49 42 55 54 45 5f 4e 4f 52 4d 41 4c 3b  TTRIBUTE_NORMAL;
fec50 0a 20 20 7d 0a 20 20 2f 2a 20 52 65 70 6f 72 74  .  }.  /* Report
fec60 73 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72  s from the inter
fec70 6e 65 74 20 61 72 65 20 74 68 61 74 20 70 65 72  net are that per
fec80 66 6f 72 6d 61 6e 63 65 20 69 73 20 61 6c 77 61  formance is alwa
fec90 79 73 0a 20 20 2a 2a 20 62 65 74 74 65 72 20 69  ys.  ** better i
feca0 66 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41 4e 44  f FILE_FLAG_RAND
fecb0 4f 4d 5f 41 43 43 45 53 53 20 69 73 20 75 73 65  OM_ACCESS is use
fecc0 64 2e 20 20 54 69 63 6b 65 74 20 23 32 36 39 39  d.  Ticket #2699
fecd0 2e 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  . */.#if SQLITE_
fece0 4f 53 5f 57 49 4e 43 45 0a 20 20 64 77 46 6c 61  OS_WINCE.  dwFla
fecf0 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20  gsAndAttributes 
fed00 7c 3d 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41 4e  |= FILE_FLAG_RAN
fed10 44 4f 4d 5f 41 43 43 45 53 53 3b 0a 23 65 6e 64  DOM_ACCESS;.#end
fed20 69 66 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20  if.  if( isNT() 
fed30 29 7b 0a 20 20 20 20 68 20 3d 20 43 72 65 61 74  ){.    h = Creat
fed40 65 46 69 6c 65 57 28 28 57 43 48 41 52 2a 29 7a  eFileW((WCHAR*)z
fed50 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 20  Converted,.     
fed60 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65 73    dwDesiredAcces
fed70 73 2c 0a 20 20 20 20 20 20 20 64 77 53 68 61 72  s,.       dwShar
fed80 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 4e 55  eMode,.       NU
fed90 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77 43 72 65  LL,.       dwCre
feda0 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e  ationDisposition
fedb0 2c 0a 20 20 20 20 20 20 20 64 77 46 6c 61 67 73  ,.       dwFlags
fedc0 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c 0a 20  AndAttributes,. 
fedd0 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20 20 29        NULL.    )
fede0 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31  ;./* isNT() is 1
fedf0 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49   if SQLITE_OS_WI
fee00 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20  NCE==1, so this 
fee10 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78  else is never ex
fee20 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63  ecuted. .** Sinc
fee30 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73  e the ASCII vers
fee40 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e  ion of these Win
fee50 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20  dows API do not 
fee60 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c  exist for WINCE,
fee70 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61  .** it's importa
fee80 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65  nt to not refere
fee90 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e  nce them for WIN
feea0 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69  CE builds..*/.#i
feeb0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
feec0 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  E==0.  }else{.  
feed0 20 20 68 20 3d 20 43 72 65 61 74 65 46 69 6c 65    h = CreateFile
feee0 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72  A((char*)zConver
feef0 74 65 64 2c 0a 20 20 20 20 20 20 20 64 77 44 65  ted,.       dwDe
fef00 73 69 72 65 64 41 63 63 65 73 73 2c 0a 20 20 20  siredAccess,.   
fef10 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65 2c      dwShareMode,
fef20 0a 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20  .       NULL,.  
fef30 20 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44       dwCreationD
fef40 69 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 20 20  isposition,.    
fef50 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74     dwFlagsAndAtt
fef60 72 69 62 75 74 65 73 2c 0a 20 20 20 20 20 20 20  ributes,.       
fef70 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a 23 65 6e 64  NULL.    );.#end
fef80 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 68 3d 3d  if.  }.  if( h==
fef90 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56  INVALID_HANDLE_V
fefa0 41 4c 55 45 20 29 7b 0a 20 20 20 20 66 72 65 65  ALUE ){.    free
fefb0 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20  (zConverted);.  
fefc0 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51    if( flags & SQ
fefd0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
fefe0 49 54 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ITE ){.      ret
feff0 75 72 6e 20 77 69 6e 4f 70 65 6e 28 70 56 66 73  urn winOpen(pVfs
ff000 2c 20 7a 4e 61 6d 65 2c 20 69 64 2c 20 0a 20 20  , zName, id, .  
ff010 20 20 20 20 20 20 20 20 20 20 20 28 28 66 6c 61             ((fla
ff020 67 73 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  gs|SQLITE_OPEN_R
ff030 45 41 44 4f 4e 4c 59 29 26 7e 53 51 4c 49 54 45  EADONLY)&~SQLITE
ff040 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29  _OPEN_READWRITE)
ff050 2c 20 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20  , pOutFlags);.  
ff060 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
ff070 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e  eturn SQLITE_CAN
ff080 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d  TOPEN;.    }.  }
ff090 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67 73  .  if( pOutFlags
ff0a0 20 29 7b 0a 20 20 20 20 69 66 28 20 66 6c 61 67   ){.    if( flag
ff0b0 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
ff0c0 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20  READWRITE ){.   
ff0d0 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20     *pOutFlags = 
ff0e0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
ff0f0 57 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 65  WRITE;.    }else
ff100 7b 0a 20 20 20 20 20 20 2a 70 4f 75 74 46 6c 61  {.      *pOutFla
ff110 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs = SQLITE_OPEN
ff120 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 7d  _READONLY;.    }
ff130 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 46  .  }.  memset(pF
ff140 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a  ile, 0, sizeof(*
ff150 70 46 69 6c 65 29 29 3b 0a 20 20 70 46 69 6c 65  pFile));.  pFile
ff160 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 77 69 6e  ->pMethod = &win
ff170 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69 6c  IoMethod;.  pFil
ff180 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 46 69 6c  e->h = h;.  pFil
ff190 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 4e  e->lastErrno = N
ff1a0 4f 5f 45 52 52 4f 52 3b 0a 20 20 70 46 69 6c 65  O_ERROR;.  pFile
ff1b0 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 67  ->sectorSize = g
ff1c0 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 56 66  etSectorSize(pVf
ff1d0 73 2c 20 7a 55 74 66 38 4e 61 6d 65 29 3b 0a 23  s, zUtf8Name);.#
ff1e0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
ff1f0 43 45 0a 20 20 69 66 28 20 28 66 6c 61 67 73 20  CE.  if( (flags 
ff200 26 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  & (SQLITE_OPEN_R
ff210 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f  EADWRITE|SQLITE_
ff220 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 29 20 3d  OPEN_MAIN_DB)) =
ff230 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  =.              
ff240 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45   (SQLITE_OPEN_RE
ff250 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f  ADWRITE|SQLITE_O
ff260 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 0a 20 20 20  PEN_MAIN_DB).   
ff270 20 20 20 20 26 26 20 21 77 69 6e 63 65 43 72 65      && !winceCre
ff280 61 74 65 4c 6f 63 6b 28 7a 4e 61 6d 65 2c 20 70  ateLock(zName, p
ff290 46 69 6c 65 29 0a 20 20 29 7b 0a 20 20 20 20 43  File).  ){.    C
ff2a0 6c 6f 73 65 48 61 6e 64 6c 65 28 68 29 3b 0a 20  loseHandle(h);. 
ff2b0 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74     free(zConvert
ff2c0 65 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ed);.    return 
ff2d0 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b  SQLITE_CANTOPEN;
ff2e0 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 54 65 6d  .  }.  if( isTem
ff2f0 70 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  p ){.    pFile->
ff300 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 3d  zDeleteOnClose =
ff310 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 7d   zConverted;.  }
ff320 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 7b 0a  else.#endif.  {.
ff330 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72      free(zConver
ff340 74 65 64 29 3b 0a 20 20 7d 0a 20 20 4f 70 65 6e  ted);.  }.  Open
ff350 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 72  Counter(+1);.  r
ff360 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
ff370 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
ff380 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e   the named file.
ff390 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
ff3a0 20 77 69 6e 64 6f 77 73 20 64 6f 65 73 20 6e 6f   windows does no
ff3b0 74 20 61 6c 6c 6f 77 20 61 20 66 69 6c 65 20 74  t allow a file t
ff3c0 6f 20 62 65 20 64 65 6c 65 74 65 64 20 69 66 20  o be deleted if 
ff3d0 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 72  some other.** pr
ff3e0 6f 63 65 73 73 20 68 61 73 20 69 74 20 6f 70 65  ocess has it ope
ff3f0 6e 2e 20 20 53 6f 6d 65 74 69 6d 65 73 20 61 20  n.  Sometimes a 
ff400 76 69 72 75 73 20 73 63 61 6e 6e 65 72 20 6f 72  virus scanner or
ff410 20 69 6e 64 65 78 69 6e 67 20 70 72 6f 67 72 61   indexing progra
ff420 6d 0a 2a 2a 20 77 69 6c 6c 20 6f 70 65 6e 20 61  m.** will open a
ff430 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 68   journal file sh
ff440 6f 72 74 6c 79 20 61 66 74 65 72 20 69 74 20 69  ortly after it i
ff450 73 20 63 72 65 61 74 65 64 20 69 6e 20 6f 72 64  s created in ord
ff460 65 72 20 74 6f 20 64 6f 0a 2a 2a 20 77 68 61 74  er to do.** what
ff470 65 76 65 72 20 69 74 20 64 6f 65 73 2e 20 20 57  ever it does.  W
ff480 68 69 6c 65 20 74 68 69 73 20 6f 74 68 65 72 20  hile this other 
ff490 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69  process is holdi
ff4a0 6e 67 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 6f  ng the.** file o
ff4b0 70 65 6e 2c 20 77 65 20 77 69 6c 6c 20 62 65 20  pen, we will be 
ff4c0 75 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65  unable to delete
ff4d0 20 69 74 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72   it.  To work ar
ff4e0 6f 75 6e 64 20 74 68 69 73 0a 2a 2a 20 70 72 6f  ound this.** pro
ff4f0 62 6c 65 6d 2c 20 77 65 20 64 65 6c 61 79 20 31  blem, we delay 1
ff500 30 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20  00 milliseconds 
ff510 61 6e 64 20 74 72 79 20 74 6f 20 64 65 6c 65 74  and try to delet
ff520 65 20 61 67 61 69 6e 2e 20 20 55 70 0a 2a 2a 20  e again.  Up.** 
ff530 74 6f 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41  to MX_DELETION_A
ff540 54 54 45 4d 50 54 73 20 64 65 6c 65 74 69 6f 6e  TTEMPTs deletion
ff550 20 61 74 74 65 6d 70 74 73 20 61 72 65 20 72 75   attempts are ru
ff560 6e 20 62 65 66 6f 72 65 20 67 69 76 69 6e 67 0a  n before giving.
ff570 2a 2a 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e  ** up and return
ff580 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f  ing an error..*/
ff590 0a 23 64 65 66 69 6e 65 20 4d 58 5f 44 45 4c 45  .#define MX_DELE
ff5a0 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20 35 0a  TION_ATTEMPTS 5.
ff5b0 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65  static int winDe
ff5c0 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  lete(.  sqlite3_
ff5d0 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20  vfs *pVfs,      
ff5e0 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20      /* Not used 
ff5f0 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f  on win32 */.  co
ff600 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
ff610 61 6d 65 2c 20 20 20 20 20 20 2f 2a 20 4e 61 6d  ame,      /* Nam
ff620 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c  e of file to del
ff630 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79 6e  ete */.  int syn
ff640 63 44 69 72 20 20 20 20 20 20 20 20 20 20 20 20  cDir            
ff650 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64       /* Not used
ff660 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 29 7b 0a   on win32 */.){.
ff670 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20    int cnt = 0;. 
ff680 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 44 57 4f   DWORD rc;.  DWO
ff690 52 44 20 65 72 72 6f 72 20 3d 20 30 3b 0a 20 20  RD error = 0;.  
ff6a0 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64  void *zConverted
ff6b0 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69   = convertUtf8Fi
ff6c0 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65  lename(zFilename
ff6d0 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
ff6e0 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 55  METER(pVfs);.  U
ff6f0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
ff700 73 79 6e 63 44 69 72 29 3b 0a 20 20 69 66 28 20  syncDir);.  if( 
ff710 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b  zConverted==0 ){
ff720 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
ff730 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
ff740 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
ff750 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
ff760 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a 20 20 69  ERR_DELETE);.  i
ff770 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20  f( isNT() ){.   
ff780 20 64 6f 7b 0a 20 20 20 20 20 20 44 65 6c 65 74   do{.      Delet
ff790 65 46 69 6c 65 57 28 7a 43 6f 6e 76 65 72 74 65  eFileW(zConverte
ff7a0 64 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20  d);.    }while( 
ff7b0 20 20 28 20 20 20 28 28 72 63 20 3d 20 47 65 74    (   ((rc = Get
ff7c0 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 28  FileAttributesW(
ff7d0 7a 43 6f 6e 76 65 72 74 65 64 29 29 20 21 3d 20  zConverted)) != 
ff7e0 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54  INVALID_FILE_ATT
ff7f0 52 49 42 55 54 45 53 29 0a 20 20 20 20 20 20 20  RIBUTES).       
ff800 20 20 20 20 20 20 20 20 7c 7c 20 28 28 65 72 72          || ((err
ff810 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f  or = GetLastErro
ff820 72 28 29 29 20 3d 3d 20 45 52 52 4f 52 5f 41 43  r()) == ERROR_AC
ff830 43 45 53 53 5f 44 45 4e 49 45 44 29 29 0a 20 20  CESS_DENIED)).  
ff840 20 20 20 20 20 20 20 20 20 26 26 20 28 2b 2b 63           && (++c
ff850 6e 74 20 3c 20 4d 58 5f 44 45 4c 45 54 49 4f 4e  nt < MX_DELETION
ff860 5f 41 54 54 45 4d 50 54 53 29 0a 20 20 20 20 20  _ATTEMPTS).     
ff870 20 20 20 20 20 20 26 26 20 28 53 6c 65 65 70 28        && (Sleep(
ff880 31 30 30 29 2c 20 31 29 20 29 3b 0a 2f 2a 20 69  100), 1) );./* i
ff890 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51  sNT() is 1 if SQ
ff8a0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31  LITE_OS_WINCE==1
ff8b0 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69  , so this else i
ff8c0 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64  s never executed
ff8d0 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20  . .** Since the 
ff8e0 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66  ASCII version of
ff8f0 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41   these Windows A
ff900 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20  PI do not exist 
ff910 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74  for WINCE,.** it
ff920 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20  's important to 
ff930 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68  not reference th
ff940 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69  em for WINCE bui
ff950 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  lds..*/.#if SQLI
ff960 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20  TE_OS_WINCE==0. 
ff970 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 7b 0a   }else{.    do{.
ff980 20 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65        DeleteFile
ff990 41 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  A(zConverted);. 
ff9a0 20 20 20 7d 77 68 69 6c 65 28 20 20 20 28 20 20     }while(   (  
ff9b0 20 28 28 72 63 20 3d 20 47 65 74 46 69 6c 65 41   ((rc = GetFileA
ff9c0 74 74 72 69 62 75 74 65 73 41 28 7a 43 6f 6e 76  ttributesA(zConv
ff9d0 65 72 74 65 64 29 29 20 21 3d 20 49 4e 56 41 4c  erted)) != INVAL
ff9e0 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54  ID_FILE_ATTRIBUT
ff9f0 45 53 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  ES).            
ffa00 20 20 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d 20     || ((error = 
ffa10 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 20  GetLastError()) 
ffa20 3d 3d 20 45 52 52 4f 52 5f 41 43 43 45 53 53 5f  == ERROR_ACCESS_
ffa30 44 45 4e 49 45 44 29 29 0a 20 20 20 20 20 20 20  DENIED)).       
ffa40 20 20 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c 20      && (++cnt < 
ffa50 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45  MX_DELETION_ATTE
ffa60 4d 50 54 53 29 0a 20 20 20 20 20 20 20 20 20 20  MPTS).          
ffa70 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c   && (Sleep(100),
ffa80 20 31 29 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20   1) );.#endif.  
ffa90 7d 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72  }.  free(zConver
ffaa0 74 65 64 29 3b 0a 20 20 4f 53 54 52 41 43 45 32  ted);.  OSTRACE2
ffab0 28 22 44 45 4c 45 54 45 20 5c 22 25 73 5c 22 5c  ("DELETE \"%s\"\
ffac0 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  n", zFilename);.
ffad0 20 20 72 65 74 75 72 6e 20 28 20 20 20 28 72 63    return (   (rc
ffae0 20 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45   == INVALID_FILE
ffaf0 5f 41 54 54 52 49 42 55 54 45 53 29 20 0a 20 20  _ATTRIBUTES) .  
ffb00 20 20 20 20 20 20 20 20 26 26 20 28 65 72 72 6f          && (erro
ffb10 72 20 3d 3d 20 45 52 52 4f 52 5f 46 49 4c 45 5f  r == ERROR_FILE_
ffb20 4e 4f 54 5f 46 4f 55 4e 44 29 29 20 3f 20 53 51  NOT_FOUND)) ? SQ
ffb30 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45  LITE_OK : SQLITE
ffb40 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0a 7d  _IOERR_DELETE;.}
ffb50 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68  ../*.** Check th
ffb60 65 20 65 78 69 73 74 61 6e 63 65 20 61 6e 64 20  e existance and 
ffb70 73 74 61 74 75 73 20 6f 66 20 61 20 66 69 6c 65  status of a file
ffb80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
ffb90 77 69 6e 41 63 63 65 73 73 28 0a 20 20 73 71 6c  winAccess(.  sql
ffba0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
ffbb0 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75          /* Not u
ffbc0 73 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a  sed on win32 */.
ffbd0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
ffbe0 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20  ilename,     /* 
ffbf0 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20  Name of file to 
ffc00 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 66  check */.  int f
ffc10 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
ffc20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66        /* Type of
ffc30 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e   test to make on
ffc40 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20   this file */.  
ffc50 69 6e 74 20 2a 70 52 65 73 4f 75 74 20 20 20 20  int *pResOut    
ffc60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
ffc70 54 3a 20 52 65 73 75 6c 74 20 2a 2f 0a 29 7b 0a  T: Result */.){.
ffc80 20 20 44 57 4f 52 44 20 61 74 74 72 3b 0a 20 20    DWORD attr;.  
ffc90 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 76 6f  int rc = 0;.  vo
ffca0 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d  id *zConverted =
ffcb0 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65   convertUtf8File
ffcc0 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b  name(zFilename);
ffcd0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
ffce0 54 45 52 28 70 56 66 73 29 3b 0a 20 20 69 66 28  TER(pVfs);.  if(
ffcf0 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29   zConverted==0 )
ffd00 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
ffd10 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
ffd20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20   if( isNT() ){. 
ffd30 20 20 20 61 74 74 72 20 3d 20 47 65 74 46 69 6c     attr = GetFil
ffd40 65 41 74 74 72 69 62 75 74 65 73 57 28 28 57 43  eAttributesW((WC
ffd50 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29  HAR*)zConverted)
ffd60 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31  ;./* isNT() is 1
ffd70 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49   if SQLITE_OS_WI
ffd80 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20  NCE==1, so this 
ffd90 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78  else is never ex
ffda0 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63  ecuted. .** Sinc
ffdb0 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72 73  e the ASCII vers
ffdc0 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e  ion of these Win
ffdd0 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20  dows API do not 
ffde0 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c  exist for WINCE,
ffdf0 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61  .** it's importa
ffe00 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65  nt to not refere
ffe10 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e  nce them for WIN
ffe20 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69  CE builds..*/.#i
ffe30 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
ffe40 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  E==0.  }else{.  
ffe50 20 20 61 74 74 72 20 3d 20 47 65 74 46 69 6c 65    attr = GetFile
ffe60 41 74 74 72 69 62 75 74 65 73 41 28 28 63 68 61  AttributesA((cha
ffe70 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  r*)zConverted);.
ffe80 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 65  #endif.  }.  fre
ffe90 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
ffea0 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 29   switch( flags )
ffeb0 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
ffec0 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20  E_ACCESS_READ:. 
ffed0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41     case SQLITE_A
ffee0 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 20  CCESS_EXISTS:.  
ffef0 20 20 20 20 72 63 20 3d 20 61 74 74 72 21 3d 49      rc = attr!=I
fff00 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52  NVALID_FILE_ATTR
fff10 49 42 55 54 45 53 3b 0a 20 20 20 20 20 20 62 72  IBUTES;.      br
fff20 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51  eak;.    case SQ
fff30 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
fff40 57 52 49 54 45 3a 0a 20 20 20 20 20 20 72 63 20  WRITE:.      rc 
fff50 3d 20 28 61 74 74 72 20 26 20 46 49 4c 45 5f 41  = (attr & FILE_A
fff60 54 54 52 49 42 55 54 45 5f 52 45 41 44 4f 4e 4c  TTRIBUTE_READONL
fff70 59 29 3d 3d 30 3b 0a 20 20 20 20 20 20 62 72 65  Y)==0;.      bre
fff80 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a  ak;.    default:
fff90 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 21 22  .      assert(!"
fffa0 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72  Invalid flags ar
fffb0 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 20  gument");.  }.  
fffc0 2a 70 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a 20  *pResOut = rc;. 
fffd0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
fffe0 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72  K;.}.../*.** Tur
ffff0 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61 74  n a relative pat
10000 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c  hname into a ful
10001 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69  l pathname.  Wri
10002 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70  te the full.** p
10003 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 4f 75  athname into zOu
10004 74 5b 5d 2e 20 20 7a 4f 75 74 5b 5d 20 77 69 6c  t[].  zOut[] wil
10005 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 70 56  l be at least pV
10006 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 2a  fs->mxPathname.*
10007 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e  * bytes in size.
10008 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
10009 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a  inFullPathname(.
1000a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
1000b 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  Vfs,            
1000c 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66  /* Pointer to vf
1000d 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f  s object */.  co
1000e 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74  nst char *zRelat
1000f 69 76 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  ive,        /* P
10010 6f 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65  ossibly relative
10011 20 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20   input path */. 
10012 20 69 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20 20   int nFull,     
10013 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10014 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 74  * Size of output
10015 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73   buffer in bytes
10016 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 75 6c   */.  char *zFul
10017 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l               
10018 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 75      /* Output bu
10019 66 66 65 72 20 2a 2f 0a 29 7b 0a 20 20 0a 23 69  ffer */.){.  .#i
1001a 66 20 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57  f defined(__CYGW
1001b 49 4e 5f 5f 29 0a 20 20 55 4e 55 53 45 44 5f 50  IN__).  UNUSED_P
1001c 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b  ARAMETER(nFull);
1001d 0a 20 20 63 79 67 77 69 6e 5f 63 6f 6e 76 5f 74  .  cygwin_conv_t
1001e 6f 5f 66 75 6c 6c 5f 77 69 6e 33 32 5f 70 61 74  o_full_win32_pat
1001f 68 28 7a 52 65 6c 61 74 69 76 65 2c 20 7a 46 75  h(zRelative, zFu
10020 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ll);.  return SQ
10021 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a  LITE_OK;.#endif.
10022 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
10023 49 4e 43 45 0a 20 20 55 4e 55 53 45 44 5f 50 41  INCE.  UNUSED_PA
10024 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b 0a  RAMETER(nFull);.
10025 20 20 2f 2a 20 57 69 6e 43 45 20 68 61 73 20 6e    /* WinCE has n
10026 6f 20 63 6f 6e 63 65 70 74 20 6f 66 20 61 20 72  o concept of a r
10027 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65  elative pathname
10028 2c 20 6f 72 20 73 6f 20 49 20 61 6d 20 74 6f 6c  , or so I am tol
10029 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  d. */.  sqlite3_
1002a 73 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d  snprintf(pVfs->m
1002b 78 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c  xPathname, zFull
1002c 2c 20 22 25 73 22 2c 20 7a 52 65 6c 61 74 69 76  , "%s", zRelativ
1002d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  e);.  return SQL
1002e 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a  ITE_OK;.#endif..
1002f 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57  #if !SQLITE_OS_W
10030 49 4e 43 45 20 26 26 20 21 64 65 66 69 6e 65 64  INCE && !defined
10031 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 69  (__CYGWIN__).  i
10032 6e 74 20 6e 42 79 74 65 3b 0a 20 20 76 6f 69 64  nt nByte;.  void
10033 20 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20   *zConverted;.  
10034 63 68 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 55 4e  char *zOut;.  UN
10035 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e  USED_PARAMETER(n
10036 46 75 6c 6c 29 3b 0a 20 20 7a 43 6f 6e 76 65 72  Full);.  zConver
10037 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66  ted = convertUtf
10038 38 46 69 6c 65 6e 61 6d 65 28 7a 52 65 6c 61 74  8Filename(zRelat
10039 69 76 65 29 3b 0a 20 20 69 66 28 20 69 73 4e 54  ive);.  if( isNT
1003a 28 29 20 29 7b 0a 20 20 20 20 57 43 48 41 52 20  () ){.    WCHAR 
1003b 2a 7a 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79 74  *zTemp;.    nByt
1003c 65 20 3d 20 47 65 74 46 75 6c 6c 50 61 74 68 4e  e = GetFullPathN
1003d 61 6d 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f  ameW((WCHAR*)zCo
1003e 6e 76 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 30  nverted, 0, 0, 0
1003f 29 20 2b 20 33 3b 0a 20 20 20 20 7a 54 65 6d 70  ) + 3;.    zTemp
10040 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65   = malloc( nByte
10041 2a 73 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d  *sizeof(zTemp[0]
10042 29 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 54 65  ) );.    if( zTe
10043 6d 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66  mp==0 ){.      f
10044 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ree(zConverted);
10045 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
10046 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
10047 7d 0a 20 20 20 20 47 65 74 46 75 6c 6c 50 61 74  }.    GetFullPat
10048 68 4e 61 6d 65 57 28 28 57 43 48 41 52 2a 29 7a  hNameW((WCHAR*)z
10049 43 6f 6e 76 65 72 74 65 64 2c 20 6e 42 79 74 65  Converted, nByte
1004a 2c 20 7a 54 65 6d 70 2c 20 30 29 3b 0a 20 20 20  , zTemp, 0);.   
1004b 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64   free(zConverted
1004c 29 3b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 75 6e  );.    zOut = un
1004d 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 65 6d  icodeToUtf8(zTem
1004e 70 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 54 65  p);.    free(zTe
1004f 6d 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69  mp);./* isNT() i
10050 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53  s 1 if SQLITE_OS
10051 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68  _WINCE==1, so th
10052 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72  is else is never
10053 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53   executed. .** S
10054 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76  ince the ASCII v
10055 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20  ersion of these 
10056 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e  Windows API do n
10057 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e  ot exist for WIN
10058 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f  CE,.** it's impo
10059 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66  rtant to not ref
1005a 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20  erence them for 
1005b 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f  WINCE builds..*/
1005c 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
1005d 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b  INCE==0.  }else{
1005e 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 65 6d 70  .    char *zTemp
1005f 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 47 65  ;.    nByte = Ge
10060 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28  tFullPathNameA((
10061 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64  char*)zConverted
10062 2c 20 30 2c 20 30 2c 20 30 29 20 2b 20 33 3b 0a  , 0, 0, 0) + 3;.
10063 20 20 20 20 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c      zTemp = mall
10064 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66  oc( nByte*sizeof
10065 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b 0a 20 20  (zTemp[0]) );.  
10066 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20 29    if( zTemp==0 )
10067 7b 0a 20 20 20 20 20 20 66 72 65 65 28 7a 43 6f  {.      free(zCo
10068 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 20 20  nverted);.      
10069 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
1006a 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 47  MEM;.    }.    G
1006b 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28  etFullPathNameA(
1006c 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65  (char*)zConverte
1006d 64 2c 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c  d, nByte, zTemp,
1006e 20 30 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 43   0);.    free(zC
1006f 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7a  onverted);.    z
10070 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 77 69  Out = sqlite3_wi
10071 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38  n32_mbcs_to_utf8
10072 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66 72 65  (zTemp);.    fre
10073 65 28 7a 54 65 6d 70 29 3b 0a 23 65 6e 64 69 66  e(zTemp);.#endif
10074 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4f 75 74 20  .  }.  if( zOut 
10075 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ){.    sqlite3_s
10076 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78  nprintf(pVfs->mx
10077 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c  Pathname, zFull,
10078 20 22 25 73 22 2c 20 7a 4f 75 74 29 3b 0a 20 20   "%s", zOut);.  
10079 20 20 66 72 65 65 28 7a 4f 75 74 29 3b 0a 20 20    free(zOut);.  
1007a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1007b 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OK;.  }else{.   
1007c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
1007d 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  OMEM;.  }.#endif
1007e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74 68  .}../*.** Get th
1007f 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66  e sector size of
10080 20 74 68 65 20 64 65 76 69 63 65 20 75 73 65 64   the device used
10081 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 66 69 6c   to store.** fil
10082 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
10083 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28 0a   getSectorSize(.
10084 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20      sqlite3_vfs 
10085 2a 70 56 66 73 2c 0a 20 20 20 20 63 6f 6e 73 74  *pVfs,.    const
10086 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 65   char *zRelative
10087 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 66 69       /* UTF-8 fi
10088 6c 65 20 6e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20  le name */.){.  
10089 44 57 4f 52 44 20 62 79 74 65 73 50 65 72 53 65  DWORD bytesPerSe
1008a 63 74 6f 72 20 3d 20 53 51 4c 49 54 45 5f 44 45  ctor = SQLITE_DE
1008b 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a  FAULT_SECTOR_SIZ
1008c 45 3b 0a 20 20 2f 2a 20 47 65 74 44 69 73 6b 46  E;.  /* GetDiskF
1008d 72 65 65 53 70 61 63 65 20 69 73 20 6e 6f 74 20  reeSpace is not 
1008e 73 75 70 70 6f 72 74 65 64 20 75 6e 64 65 72 20  supported under 
1008f 57 49 4e 43 45 20 2a 2f 0a 23 69 66 20 53 51 4c  WINCE */.#if SQL
10090 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 55  ITE_OS_WINCE.  U
10091 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
10092 70 56 66 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f  pVfs);.  UNUSED_
10093 50 41 52 41 4d 45 54 45 52 28 7a 52 65 6c 61 74  PARAMETER(zRelat
10094 69 76 65 29 3b 0a 23 65 6c 73 65 0a 20 20 63 68  ive);.#else.  ch
10095 61 72 20 7a 46 75 6c 6c 70 61 74 68 5b 4d 41 58  ar zFullpath[MAX
10096 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69 6e 74 20  _PATH+1];.  int 
10097 72 63 3b 0a 20 20 44 57 4f 52 44 20 64 77 52 65  rc;.  DWORD dwRe
10098 74 20 3d 20 30 3b 0a 20 20 44 57 4f 52 44 20 64  t = 0;.  DWORD d
10099 77 44 75 6d 6d 79 3b 0a 0a 20 20 2f 2a 0a 20 20  wDummy;..  /*.  
1009a 2a 2a 20 57 65 20 6e 65 65 64 20 74 6f 20 67 65  ** We need to ge
1009b 74 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 20  t the full path 
1009c 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65  name of the file
1009d 0a 20 20 2a 2a 20 74 6f 20 67 65 74 20 74 68 65  .  ** to get the
1009e 20 64 72 69 76 65 20 6c 65 74 74 65 72 20 74 6f   drive letter to
1009f 20 6c 6f 6f 6b 20 75 70 20 74 68 65 20 73 65 63   look up the sec
100a0 74 6f 72 0a 20 20 2a 2a 20 73 69 7a 65 2e 0a 20  tor.  ** size.. 
100a1 20 2a 2f 0a 20 20 72 63 20 3d 20 77 69 6e 46 75   */.  rc = winFu
100a2 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c  llPathname(pVfs,
100a3 20 7a 52 65 6c 61 74 69 76 65 2c 20 4d 41 58 5f   zRelative, MAX_
100a4 50 41 54 48 2c 20 7a 46 75 6c 6c 70 61 74 68 29  PATH, zFullpath)
100a5 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d 20 53 51  ;.  if( rc == SQ
100a6 4c 49 54 45 5f 4f 4b 20 29 0a 20 20 7b 0a 20 20  LITE_OK ).  {.  
100a7 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74    void *zConvert
100a8 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38  ed = convertUtf8
100a9 46 69 6c 65 6e 61 6d 65 28 7a 46 75 6c 6c 70 61  Filename(zFullpa
100aa 74 68 29 3b 0a 20 20 20 20 69 66 28 20 7a 43 6f  th);.    if( zCo
100ab 6e 76 65 72 74 65 64 20 29 7b 0a 20 20 20 20 20  nverted ){.     
100ac 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20   if( isNT() ){. 
100ad 20 20 20 20 20 20 20 2f 2a 20 74 72 69 6d 20 70         /* trim p
100ae 61 74 68 20 74 6f 20 6a 75 73 74 20 64 72 69 76  ath to just driv
100af 65 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20  e reference */. 
100b0 20 20 20 20 20 20 20 57 43 48 41 52 20 2a 70 20         WCHAR *p 
100b1 3d 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20  = zConverted;.  
100b2 20 20 20 20 20 20 66 6f 72 28 3b 2a 70 3b 70 2b        for(;*p;p+
100b3 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
100b4 28 20 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 7b 0a  ( *p == '\\' ){.
100b5 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 20 3d              *p =
100b6 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 20   '\0';.         
100b7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
100b8 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
100b9 20 20 20 20 20 20 20 20 64 77 52 65 74 20 3d 20          dwRet = 
100ba 47 65 74 44 69 73 6b 46 72 65 65 53 70 61 63 65  GetDiskFreeSpace
100bb 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65  W((WCHAR*)zConve
100bc 72 74 65 64 2c 0a 20 20 20 20 20 20 20 20 20 20  rted,.          
100bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
100be 20 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79          &dwDummy
100bf 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
100c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
100c1 20 20 20 20 26 62 79 74 65 73 50 65 72 53 65 63      &bytesPerSec
100c2 74 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  tor,.           
100c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
100c4 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c         &dwDummy,
100c5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
100c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
100c7 20 20 20 26 64 77 44 75 6d 6d 79 29 3b 0a 20 20     &dwDummy);.  
100c8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
100c9 20 20 20 2f 2a 20 74 72 69 6d 20 70 61 74 68 20     /* trim path 
100ca 74 6f 20 6a 75 73 74 20 64 72 69 76 65 20 72 65  to just drive re
100cb 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 20 20 20  ference */.     
100cc 20 20 20 43 48 41 52 20 2a 70 20 3d 20 28 43 48     CHAR *p = (CH
100cd 41 52 20 2a 29 7a 43 6f 6e 76 65 72 74 65 64 3b  AR *)zConverted;
100ce 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 2a 70  .        for(;*p
100cf 3b 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  ;p++){.         
100d0 20 69 66 28 20 2a 70 20 3d 3d 20 27 5c 5c 27 20   if( *p == '\\' 
100d1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  ){.            *
100d2 70 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20  p = '\0';.      
100d3 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
100d4 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
100d5 20 7d 0a 20 20 20 20 20 20 20 20 64 77 52 65 74   }.        dwRet
100d6 20 3d 20 47 65 74 44 69 73 6b 46 72 65 65 53 70   = GetDiskFreeSp
100d7 61 63 65 41 28 28 43 48 41 52 2a 29 7a 43 6f 6e  aceA((CHAR*)zCon
100d8 76 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 20  verted,.        
100d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
100da 20 20 20 20 20 20 20 20 20 20 26 64 77 44 75 6d            &dwDum
100db 6d 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  my,.            
100dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
100dd 20 20 20 20 20 20 26 62 79 74 65 73 50 65 72 53        &bytesPerS
100de 65 63 74 6f 72 2c 0a 20 20 20 20 20 20 20 20 20  ector,.         
100df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
100e0 20 20 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d           &dwDumm
100e1 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  y,.             
100e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
100e3 20 20 20 20 20 26 64 77 44 75 6d 6d 79 29 3b 0a       &dwDummy);.
100e4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 72        }.      fr
100e5 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  ee(zConverted);.
100e6 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 64      }.    if( !d
100e7 77 52 65 74 20 29 7b 0a 20 20 20 20 20 20 62 79  wRet ){.      by
100e8 74 65 73 50 65 72 53 65 63 74 6f 72 20 3d 20 53  tesPerSector = S
100e9 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45  QLITE_DEFAULT_SE
100ea 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 20 20 7d  CTOR_SIZE;.    }
100eb 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65  .  }.#endif.  re
100ec 74 75 72 6e 20 28 69 6e 74 29 20 62 79 74 65 73  turn (int) bytes
100ed 50 65 72 53 65 63 74 6f 72 3b 20 0a 7d 0a 0a 23  PerSector; .}..#
100ee 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
100ef 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f  IT_LOAD_EXTENSIO
100f0 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63  N./*.** Interfac
100f1 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61  es for opening a
100f2 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c   shared library,
100f3 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70   finding entry p
100f4 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20  oints.** within 
100f5 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61  the shared libra
100f6 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20  ry, and closing 
100f7 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61  the shared libra
100f8 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74  ry..*/./*.** Int
100f9 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e  erfaces for open
100fa 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62  ing a shared lib
100fb 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e  rary, finding en
100fc 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69  try points.** wi
100fd 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20  thin the shared 
100fe 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f  library, and clo
100ff 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20  sing the shared 
10100 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61 74  library..*/.stat
10101 69 63 20 76 6f 69 64 20 2a 77 69 6e 44 6c 4f 70  ic void *winDlOp
10102 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  en(sqlite3_vfs *
10103 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72  pVfs, const char
10104 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20   *zFilename){.  
10105 48 41 4e 44 4c 45 20 68 3b 0a 20 20 76 6f 69 64  HANDLE h;.  void
10106 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63   *zConverted = c
10107 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61  onvertUtf8Filena
10108 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  me(zFilename);. 
10109 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
1010a 52 28 70 56 66 73 29 3b 0a 20 20 69 66 28 20 7a  R(pVfs);.  if( z
1010b 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a  Converted==0 ){.
1010c 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
1010d 7d 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29  }.  if( isNT() )
1010e 7b 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64 4c 69  {.    h = LoadLi
1010f 62 72 61 72 79 57 28 28 57 43 48 41 52 2a 29 7a  braryW((WCHAR*)z
10110 43 6f 6e 76 65 72 74 65 64 29 3b 0a 2f 2a 20 69  Converted);./* i
10111 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51  sNT() is 1 if SQ
10112 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31  LITE_OS_WINCE==1
10113 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69  , so this else i
10114 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64  s never executed
10115 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20  . .** Since the 
10116 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66  ASCII version of
10117 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41   these Windows A
10118 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20  PI do not exist 
10119 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74  for WINCE,.** it
1011a 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20  's important to 
1011b 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68  not reference th
1011c 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69  em for WINCE bui
1011d 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  lds..*/.#if SQLI
1011e 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20  TE_OS_WINCE==0. 
1011f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20   }else{.    h = 
10120 4c 6f 61 64 4c 69 62 72 61 72 79 41 28 28 63 68  LoadLibraryA((ch
10121 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ar*)zConverted);
10122 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72  .#endif.  }.  fr
10123 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  ee(zConverted);.
10124 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29    return (void*)
10125 68 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  h;.}.static void
10126 20 77 69 6e 44 6c 45 72 72 6f 72 28 73 71 6c 69   winDlError(sqli
10127 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69  te3_vfs *pVfs, i
10128 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a  nt nBuf, char *z
10129 42 75 66 4f 75 74 29 7b 0a 20 20 55 4e 55 53 45  BufOut){.  UNUSE
1012a 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73  D_PARAMETER(pVfs
1012b 29 3b 0a 20 20 67 65 74 4c 61 73 74 45 72 72 6f  );.  getLastErro
1012c 72 4d 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 4f  rMsg(nBuf, zBufO
1012d 75 74 29 3b 0a 7d 0a 76 6f 69 64 20 28 2a 77 69  ut);.}.void (*wi
1012e 6e 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76  nDlSym(sqlite3_v
1012f 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a  fs *pVfs, void *
10130 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63  pHandle, const c
10131 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 29 28 76  har *zSymbol))(v
10132 6f 69 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  oid){.  UNUSED_P
10133 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a  ARAMETER(pVfs);.
10134 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
10135 4e 43 45 0a 20 20 2f 2a 20 54 68 65 20 47 65 74  NCE.  /* The Get
10136 50 72 6f 63 41 64 64 72 65 73 73 41 28 29 20 72  ProcAddressA() r
10137 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 61  outine is only a
10138 76 61 69 6c 61 62 6c 65 20 6f 6e 20 77 69 6e 63  vailable on winc
10139 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28  e. */.  return (
1013a 76 6f 69 64 28 2a 29 28 76 6f 69 64 29 29 47 65  void(*)(void))Ge
1013b 74 50 72 6f 63 41 64 64 72 65 73 73 41 28 28 48  tProcAddressA((H
1013c 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a  ANDLE)pHandle, z
1013d 53 79 6d 62 6f 6c 29 3b 0a 23 65 6c 73 65 0a 20  Symbol);.#else. 
1013e 20 2f 2a 20 41 6c 6c 20 6f 74 68 65 72 20 77 69   /* All other wi
1013f 6e 64 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 20  ndows platforms 
10140 65 78 70 65 63 74 20 47 65 74 50 72 6f 63 41 64  expect GetProcAd
10141 64 72 65 73 73 28 29 20 74 6f 20 74 61 6b 65 0a  dress() to take.
10142 20 20 2a 2a 20 61 6e 20 41 6e 73 69 20 73 74 72    ** an Ansi str
10143 69 6e 67 20 72 65 67 61 72 64 6c 65 73 73 20 6f  ing regardless o
10144 66 20 74 68 65 20 5f 55 4e 49 43 4f 44 45 20 73  f the _UNICODE s
10145 65 74 74 69 6e 67 20 2a 2f 0a 20 20 72 65 74 75  etting */.  retu
10146 72 6e 20 28 76 6f 69 64 28 2a 29 28 76 6f 69 64  rn (void(*)(void
10147 29 29 47 65 74 50 72 6f 63 41 64 64 72 65 73 73  ))GetProcAddress
10148 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65  ((HANDLE)pHandle
10149 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6e 64  , zSymbol);.#end
1014a 69 66 0a 7d 0a 76 6f 69 64 20 77 69 6e 44 6c 43  if.}.void winDlC
1014b 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73  lose(sqlite3_vfs
1014c 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48   *pVfs, void *pH
1014d 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45 44  andle){.  UNUSED
1014e 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29  _PARAMETER(pVfs)
1014f 3b 0a 20 20 46 72 65 65 4c 69 62 72 61 72 79 28  ;.  FreeLibrary(
10150 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 29  (HANDLE)pHandle)
10151 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20  ;.}.#else /* if 
10152 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44  SQLITE_OMIT_LOAD
10153 5f 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65  _EXTENSION is de
10154 66 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66  fined: */.  #def
10155 69 6e 65 20 77 69 6e 44 6c 4f 70 65 6e 20 20 30  ine winDlOpen  0
10156 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c  .  #define winDl
10157 45 72 72 6f 72 20 30 0a 20 20 23 64 65 66 69 6e  Error 0.  #defin
10158 65 20 77 69 6e 44 6c 53 79 6d 20 20 20 30 0a 20  e winDlSym   0. 
10159 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 43 6c   #define winDlCl
1015a 6f 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f  ose 0.#endif.../
1015b 2a 0a 2a 2a 20 57 72 69 74 65 20 75 70 20 74 6f  *.** Write up to
1015c 20 6e 42 75 66 20 62 79 74 65 73 20 6f 66 20 72   nBuf bytes of r
1015d 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 7a  andomness into z
1015e 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  Buf..*/.static i
1015f 6e 74 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73  nt winRandomness
10160 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
10161 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68  fs, int nBuf, ch
10162 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 69 6e 74  ar *zBuf){.  int
10163 20 6e 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44   n = 0;.  UNUSED
10164 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29  _PARAMETER(pVfs)
10165 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ;.#if defined(SQ
10166 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 6e 20 3d  LITE_TEST).  n =
10167 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 65 74 28   nBuf;.  memset(
10168 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b 0a  zBuf, 0, nBuf);.
10169 23 65 6c 73 65 0a 20 20 69 66 28 20 73 69 7a 65  #else.  if( size
1016a 6f 66 28 53 59 53 54 45 4d 54 49 4d 45 29 3c 3d  of(SYSTEMTIME)<=
1016b 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 53 59  nBuf-n ){.    SY
1016c 53 54 45 4d 54 49 4d 45 20 78 3b 0a 20 20 20 20  STEMTIME x;.    
1016d 47 65 74 53 79 73 74 65 6d 54 69 6d 65 28 26 78  GetSystemTime(&x
1016e 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a  );.    memcpy(&z
1016f 42 75 66 5b 6e 5d 2c 20 26 78 2c 20 73 69 7a 65  Buf[n], &x, size
10170 6f 66 28 78 29 29 3b 0a 20 20 20 20 6e 20 2b 3d  of(x));.    n +=
10171 20 73 69 7a 65 6f 66 28 78 29 3b 0a 20 20 7d 0a   sizeof(x);.  }.
10172 20 20 69 66 28 20 73 69 7a 65 6f 66 28 44 57 4f    if( sizeof(DWO
10173 52 44 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20  RD)<=nBuf-n ){. 
10174 20 20 20 44 57 4f 52 44 20 70 69 64 20 3d 20 47     DWORD pid = G
10175 65 74 43 75 72 72 65 6e 74 50 72 6f 63 65 73 73  etCurrentProcess
10176 49 64 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  Id();.    memcpy
10177 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 69 64 2c  (&zBuf[n], &pid,
10178 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20   sizeof(pid));. 
10179 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 70     n += sizeof(p
1017a 69 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  id);.  }.  if( s
1017b 69 7a 65 6f 66 28 44 57 4f 52 44 29 3c 3d 6e 42  izeof(DWORD)<=nB
1017c 75 66 2d 6e 20 29 7b 0a 20 20 20 20 44 57 4f 52  uf-n ){.    DWOR
1017d 44 20 63 6e 74 20 3d 20 47 65 74 54 69 63 6b 43  D cnt = GetTickC
1017e 6f 75 6e 74 28 29 3b 0a 20 20 20 20 6d 65 6d 63  ount();.    memc
1017f 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 63 6e  py(&zBuf[n], &cn
10180 74 2c 20 73 69 7a 65 6f 66 28 63 6e 74 29 29 3b  t, sizeof(cnt));
10181 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66  .    n += sizeof
10182 28 63 6e 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28  (cnt);.  }.  if(
10183 20 73 69 7a 65 6f 66 28 4c 41 52 47 45 5f 49 4e   sizeof(LARGE_IN
10184 54 45 47 45 52 29 3c 3d 6e 42 75 66 2d 6e 20 29  TEGER)<=nBuf-n )
10185 7b 0a 20 20 20 20 4c 41 52 47 45 5f 49 4e 54 45  {.    LARGE_INTE
10186 47 45 52 20 69 3b 0a 20 20 20 20 51 75 65 72 79  GER i;.    Query
10187 50 65 72 66 6f 72 6d 61 6e 63 65 43 6f 75 6e 74  PerformanceCount
10188 65 72 28 26 69 29 3b 0a 20 20 20 20 6d 65 6d 63  er(&i);.    memc
10189 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 69 2c  py(&zBuf[n], &i,
1018a 20 73 69 7a 65 6f 66 28 69 29 29 3b 0a 20 20 20   sizeof(i));.   
1018b 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 69 29 3b   n += sizeof(i);
1018c 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65  .  }.#endif.  re
1018d 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  turn n;.}.../*.*
1018e 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c 69  * Sleep for a li
1018f 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 74  ttle while.  Ret
10190 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f  urn the amount o
10191 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2f  f time slept..*/
10192 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53  .static int winS
10193 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73  leep(sqlite3_vfs
10194 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63 72   *pVfs, int micr
10195 6f 73 65 63 29 7b 0a 20 20 53 6c 65 65 70 28 28  osec){.  Sleep((
10196 6d 69 63 72 6f 73 65 63 2b 39 39 39 29 2f 31 30  microsec+999)/10
10197 30 30 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  00);.  UNUSED_PA
10198 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20  RAMETER(pVfs);. 
10199 20 72 65 74 75 72 6e 20 28 28 6d 69 63 72 6f 73   return ((micros
1019a 65 63 2b 39 39 39 29 2f 31 30 30 30 29 2a 31 30  ec+999)/1000)*10
1019b 30 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  00;.}../*.** The
1019c 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61   following varia
1019d 62 6c 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61  ble, if set to a
1019e 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c   non-zero value,
1019f 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65 73   becomes the res
101a0 75 6c 74 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  ult.** returned 
101a1 66 72 6f 6d 20 73 71 6c 69 74 65 33 4f 73 43 75  from sqlite3OsCu
101a2 72 72 65 6e 74 54 69 6d 65 28 29 2e 20 20 54 68  rrentTime().  Th
101a3 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 74  is is used for t
101a4 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65  esting..*/.#ifde
101a5 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
101a6 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
101a7 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d  ite3_current_tim
101a8 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f  e = 0;.#endif../
101a9 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75  *.** Find the cu
101aa 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55  rrent time (in U
101ab 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e  niversal Coordin
101ac 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69  ated Time).  Wri
101ad 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e  te the.** curren
101ae 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20  t time and date 
101af 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20  as a Julian Day 
101b0 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e  number into *prN
101b1 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e  ow and.** return
101b2 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66   0.  Return 1 if
101b3 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61   the time and da
101b4 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75  te cannot be fou
101b5 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 77 69 6e 43 75  nd..*/.int winCu
101b6 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65  rrentTime(sqlite
101b7 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f 75  3_vfs *pVfs, dou
101b8 62 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 20 20 46  ble *prNow){.  F
101b9 49 4c 45 54 49 4d 45 20 66 74 3b 0a 20 20 2f 2a  ILETIME ft;.  /*
101ba 20 46 49 4c 45 54 49 4d 45 20 73 74 72 75 63 74   FILETIME struct
101bb 75 72 65 20 69 73 20 61 20 36 34 2d 62 69 74 20  ure is a 64-bit 
101bc 76 61 6c 75 65 20 72 65 70 72 65 73 65 6e 74 69  value representi
101bd 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ng the number of
101be 20 0a 20 20 20 20 20 31 30 30 2d 6e 61 6e 6f 73   .     100-nanos
101bf 65 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 20  econd intervals 
101c0 73 69 6e 63 65 20 4a 61 6e 75 61 72 79 20 31 2c  since January 1,
101c1 20 31 36 30 31 20 28 3d 20 4a 44 20 32 33 30 35   1601 (= JD 2305
101c2 38 31 33 2e 35 29 2e 20 0a 20 20 2a 2f 0a 20 20  813.5). .  */.  
101c3 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74 69  sqlite3_int64 ti
101c4 6d 65 57 3b 20 20 20 2f 2a 20 57 68 6f 6c 65 20  meW;   /* Whole 
101c5 64 61 79 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65  days */.  sqlite
101c6 33 5f 69 6e 74 36 34 20 74 69 6d 65 46 3b 20 20  3_int64 timeF;  
101c7 20 2f 2a 20 46 72 61 63 74 69 6f 6e 61 6c 20 44   /* Fractional D
101c8 61 79 73 20 2a 2f 0a 0a 20 20 2f 2a 20 4e 75 6d  ays */..  /* Num
101c9 62 65 72 20 6f 66 20 31 30 30 2d 6e 61 6e 6f 73  ber of 100-nanos
101ca 65 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 20  econd intervals 
101cb 69 6e 20 61 20 73 69 6e 67 6c 65 20 64 61 79 20  in a single day 
101cc 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  */.  static cons
101cd 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  t sqlite3_int64 
101ce 6e 74 75 50 65 72 44 61 79 20 3d 20 0a 20 20 20  ntuPerDay = .   
101cf 20 20 20 31 30 30 30 30 30 30 30 2a 28 73 71 6c     10000000*(sql
101d0 69 74 65 33 5f 69 6e 74 36 34 29 38 36 34 30 30  ite3_int64)86400
101d1 3b 0a 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f  ;..  /* Number o
101d2 66 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64  f 100-nanosecond
101d3 20 69 6e 74 65 72 76 61 6c 73 20 69 6e 20 68 61   intervals in ha
101d4 6c 66 20 6f 66 20 61 20 64 61 79 20 2a 2f 0a 20  lf of a day */. 
101d5 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71   static const sq
101d6 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 74 75 50  lite3_int64 ntuP
101d7 65 72 48 61 6c 66 44 61 79 20 3d 20 0a 20 20 20  erHalfDay = .   
101d8 20 20 20 31 30 30 30 30 30 30 30 2a 28 73 71 6c     10000000*(sql
101d9 69 74 65 33 5f 69 6e 74 36 34 29 34 33 32 30 30  ite3_int64)43200
101da 3b 0a 0a 20 20 2f 2a 20 32 5e 33 32 20 2d 20 74  ;..  /* 2^32 - t
101db 6f 20 61 76 6f 69 64 20 75 73 65 20 6f 66 20 4c  o avoid use of L
101dc 4c 20 61 6e 64 20 77 61 72 6e 69 6e 67 73 20 69  L and warnings i
101dd 6e 20 67 63 63 20 2a 2f 0a 20 20 73 74 61 74 69  n gcc */.  stati
101de 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
101df 69 6e 74 36 34 20 6d 61 78 33 32 42 69 74 56 61  int64 max32BitVa
101e0 6c 75 65 20 3d 20 0a 20 20 20 20 20 20 28 73 71  lue = .      (sq
101e1 6c 69 74 65 33 5f 69 6e 74 36 34 29 32 30 30 30  lite3_int64)2000
101e2 30 30 30 30 30 30 20 2b 20 28 73 71 6c 69 74 65  000000 + (sqlite
101e3 33 5f 69 6e 74 36 34 29 32 30 30 30 30 30 30 30  3_int64)20000000
101e4 30 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e  00 + (sqlite3_in
101e5 74 36 34 29 32 39 34 39 36 37 32 39 36 3b 0a 0a  t64)294967296;..
101e6 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
101e7 4e 43 45 0a 20 20 53 59 53 54 45 4d 54 49 4d 45  NCE.  SYSTEMTIME
101e8 20 74 69 6d 65 3b 0a 20 20 47 65 74 53 79 73 74   time;.  GetSyst
101e9 65 6d 54 69 6d 65 28 26 74 69 6d 65 29 3b 0a 20  emTime(&time);. 
101ea 20 2f 2a 20 69 66 20 53 79 73 74 65 6d 54 69 6d   /* if SystemTim
101eb 65 54 6f 46 69 6c 65 54 69 6d 65 28 29 20 66 61  eToFileTime() fa
101ec 69 6c 73 2c 20 69 74 20 72 65 74 75 72 6e 73 20  ils, it returns 
101ed 7a 65 72 6f 2e 20 2a 2f 0a 20 20 69 66 20 28 21  zero. */.  if (!
101ee 53 79 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65  SystemTimeToFile
101ef 54 69 6d 65 28 26 74 69 6d 65 2c 26 66 74 29 29  Time(&time,&ft))
101f0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  {.    return 1;.
101f1 20 20 7d 0a 23 65 6c 73 65 0a 20 20 47 65 74 53    }.#else.  GetS
101f2 79 73 74 65 6d 54 69 6d 65 41 73 46 69 6c 65 54  ystemTimeAsFileT
101f3 69 6d 65 28 20 26 66 74 20 29 3b 0a 23 65 6e 64  ime( &ft );.#end
101f4 69 66 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  if.  UNUSED_PARA
101f5 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 74  METER(pVfs);.  t
101f6 69 6d 65 57 20 3d 20 28 28 28 73 71 6c 69 74 65  imeW = (((sqlite
101f7 33 5f 69 6e 74 36 34 29 66 74 2e 64 77 48 69 67  3_int64)ft.dwHig
101f8 68 44 61 74 65 54 69 6d 65 29 2a 6d 61 78 33 32  hDateTime)*max32
101f9 42 69 74 56 61 6c 75 65 29 20 2b 20 28 73 71 6c  BitValue) + (sql
101fa 69 74 65 33 5f 69 6e 74 36 34 29 66 74 2e 64 77  ite3_int64)ft.dw
101fb 4c 6f 77 44 61 74 65 54 69 6d 65 3b 0a 20 20 74  LowDateTime;.  t
101fc 69 6d 65 46 20 3d 20 74 69 6d 65 57 20 25 20 6e  imeF = timeW % n
101fd 74 75 50 65 72 44 61 79 3b 20 20 20 20 20 20 20  tuPerDay;       
101fe 20 20 20 2f 2a 20 66 72 61 63 74 69 6f 6e 61 6c     /* fractional
101ff 20 64 61 79 73 20 28 31 30 30 2d 6e 61 6e 6f 73   days (100-nanos
10200 65 63 6f 6e 64 73 29 20 2a 2f 0a 20 20 74 69 6d  econds) */.  tim
10201 65 57 20 3d 20 74 69 6d 65 57 20 2f 20 6e 74 75  eW = timeW / ntu
10202 50 65 72 44 61 79 3b 20 20 20 20 20 20 20 20 20  PerDay;         
10203 20 2f 2a 20 77 68 6f 6c 65 20 64 61 79 73 20 2a   /* whole days *
10204 2f 0a 20 20 74 69 6d 65 57 20 3d 20 74 69 6d 65  /.  timeW = time
10205 57 20 2b 20 32 33 30 35 38 31 33 3b 20 20 20 20  W + 2305813;    
10206 20 20 20 20 20 20 20 20 2f 2a 20 61 64 64 20 77          /* add w
10207 68 6f 6c 65 20 64 61 79 73 20 28 66 72 6f 6d 20  hole days (from 
10208 32 33 30 35 38 31 33 2e 35 29 20 2a 2f 0a 20 20  2305813.5) */.  
10209 74 69 6d 65 46 20 3d 20 74 69 6d 65 46 20 2b 20  timeF = timeF + 
1020a 6e 74 75 50 65 72 48 61 6c 66 44 61 79 3b 20 20  ntuPerHalfDay;  
1020b 20 20 20 20 2f 2a 20 61 64 64 20 68 61 6c 66 20      /* add half 
1020c 61 20 64 61 79 20 28 66 72 6f 6d 20 32 33 30 35  a day (from 2305
1020d 38 31 33 2e 35 29 20 2a 2f 0a 20 20 74 69 6d 65  813.5) */.  time
1020e 57 20 3d 20 74 69 6d 65 57 20 2b 20 28 74 69 6d  W = timeW + (tim
1020f 65 46 2f 6e 74 75 50 65 72 44 61 79 29 3b 20 20  eF/ntuPerDay);  
10210 2f 2a 20 61 64 64 20 77 68 6f 6c 65 20 64 61 79  /* add whole day
10211 20 69 66 20 68 61 6c 66 20 64 61 79 20 6d 61 64   if half day mad
10212 65 20 6f 6e 65 20 2a 2f 0a 20 20 74 69 6d 65 46  e one */.  timeF
10213 20 3d 20 74 69 6d 65 46 20 25 20 6e 74 75 50 65   = timeF % ntuPe
10214 72 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f  rDay;          /
10215 2a 20 63 6f 6d 70 75 74 65 20 6e 65 77 20 66 72  * compute new fr
10216 61 63 74 69 6f 6e 61 6c 20 64 61 79 73 20 2a 2f  actional days */
10217 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 28 64 6f 75  .  *prNow = (dou
10218 62 6c 65 29 74 69 6d 65 57 20 2b 20 28 28 64 6f  ble)timeW + ((do
10219 75 62 6c 65 29 74 69 6d 65 46 20 2f 20 28 64 6f  uble)timeF / (do
1021a 75 62 6c 65 29 6e 74 75 50 65 72 44 61 79 29 3b  uble)ntuPerDay);
1021b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
1021c 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65  EST.  if( sqlite
1021d 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29  3_current_time )
1021e 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 28  {.    *prNow = (
1021f 28 64 6f 75 62 6c 65 29 73 71 6c 69 74 65 33 5f  (double)sqlite3_
10220 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 2b 20 28  current_time + (
10221 64 6f 75 62 6c 65 29 34 33 32 30 30 29 20 2f 20  double)43200) / 
10222 28 64 6f 75 62 6c 65 29 38 36 34 30 30 20 2b 20  (double)86400 + 
10223 28 64 6f 75 62 6c 65 29 32 34 34 30 35 38 37 3b  (double)2440587;
10224 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65  .  }.#endif.  re
10225 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
10226 20 54 68 65 20 69 64 65 61 20 69 73 20 74 68 61   The idea is tha
10227 74 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  t this function 
10228 77 6f 72 6b 73 20 6c 69 6b 65 20 61 20 63 6f 6d  works like a com
10229 62 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 47  bination of.** G
1022a 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 61 6e  etLastError() an
1022b 64 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 28  d FormatMessage(
1022c 29 20 6f 6e 20 77 69 6e 64 6f 77 73 20 28 6f 72  ) on windows (or
1022d 20 65 72 72 6e 6f 20 61 6e 64 0a 2a 2a 20 73 74   errno and.** st
1022e 72 65 72 72 6f 72 5f 72 28 29 20 6f 6e 20 75 6e  rerror_r() on un
1022f 69 78 29 2e 20 41 66 74 65 72 20 61 6e 20 65 72  ix). After an er
10230 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20  ror is returned 
10231 62 79 20 61 6e 20 4f 53 0a 2a 2a 20 66 75 6e 63  by an OS.** func
10232 74 69 6f 6e 2c 20 53 51 4c 69 74 65 20 63 61 6c  tion, SQLite cal
10233 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ls this function
10234 20 77 69 74 68 20 7a 42 75 66 20 70 6f 69 6e 74   with zBuf point
10235 69 6e 67 20 74 6f 0a 2a 2a 20 61 20 62 75 66 66  ing to.** a buff
10236 65 72 20 6f 66 20 6e 42 75 66 20 62 79 74 65 73  er of nBuf bytes
10237 2e 20 54 68 65 20 4f 53 20 6c 61 79 65 72 20 73  . The OS layer s
10238 68 6f 75 6c 64 20 70 6f 70 75 6c 61 74 65 20 74  hould populate t
10239 68 65 0a 2a 2a 20 62 75 66 66 65 72 20 77 69 74  he.** buffer wit
1023a 68 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74  h a nul-terminat
1023b 65 64 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  ed UTF-8 encoded
1023c 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a   error message.*
1023d 2a 20 64 65 73 63 72 69 62 69 6e 67 20 74 68 65  * describing the
1023e 20 6c 61 73 74 20 49 4f 20 65 72 72 6f 72 20 74   last IO error t
1023f 6f 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 20  o have occurred 
10240 77 69 74 68 69 6e 20 74 68 65 20 63 61 6c 6c 69  within the calli
10241 6e 67 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a 2a  ng.** thread..**
10242 0a 2a 2a 20 49 66 20 74 68 65 20 65 72 72 6f 72  .** If the error
10243 20 6d 65 73 73 61 67 65 20 69 73 20 74 6f 6f 20   message is too 
10244 6c 61 72 67 65 20 66 6f 72 20 74 68 65 20 73 75  large for the su
10245 70 70 6c 69 65 64 20 62 75 66 66 65 72 2c 0a 2a  pplied buffer,.*
10246 2a 20 69 74 20 73 68 6f 75 6c 64 20 62 65 20 74  * it should be t
10247 72 75 6e 63 61 74 65 64 2e 20 54 68 65 20 72 65  runcated. The re
10248 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 78 47  turn value of xG
10249 65 74 4c 61 73 74 45 72 72 6f 72 0a 2a 2a 20 69  etLastError.** i
1024a 73 20 7a 65 72 6f 20 69 66 20 74 68 65 20 65 72  s zero if the er
1024b 72 6f 72 20 6d 65 73 73 61 67 65 20 66 69 74 73  ror message fits
1024c 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 2c 20   in the buffer, 
1024d 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f  or non-zero.** o
1024e 74 68 65 72 77 69 73 65 20 28 69 66 20 74 68 65  therwise (if the
1024f 20 6d 65 73 73 61 67 65 20 77 61 73 20 74 72 75   message was tru
10250 6e 63 61 74 65 64 29 2e 20 49 66 20 6e 6f 6e 2d  ncated). If non-
10251 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64  zero is returned
10252 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20  ,.** then it is 
10253 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 74 6f  not necessary to
10254 20 69 6e 63 6c 75 64 65 20 74 68 65 20 6e 75 6c   include the nul
10255 2d 74 65 72 6d 69 6e 61 74 6f 72 20 63 68 61 72  -terminator char
10256 61 63 74 65 72 0a 2a 2a 20 69 6e 20 74 68 65 20  acter.** in the 
10257 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e 0a 2a  output buffer..*
10258 2a 0a 2a 2a 20 4e 6f 74 20 73 75 70 70 6c 79 69  *.** Not supplyi
10259 6e 67 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  ng an error mess
1025a 61 67 65 20 77 69 6c 6c 20 68 61 76 65 20 6e 6f  age will have no
1025b 20 61 64 76 65 72 73 65 20 65 66 66 65 63 74 0a   adverse effect.
1025c 2a 2a 20 6f 6e 20 53 51 4c 69 74 65 2e 20 49 74  ** on SQLite. It
1025d 20 69 73 20 66 69 6e 65 20 74 6f 20 68 61 76 65   is fine to have
1025e 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69   an implementati
1025f 6f 6e 20 74 68 61 74 20 6e 65 76 65 72 0a 2a 2a  on that never.**
10260 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f   returns an erro
10261 72 20 6d 65 73 73 61 67 65 3a 0a 2a 2a 0a 2a 2a  r message:.**.**
10262 20 20 20 69 6e 74 20 78 47 65 74 4c 61 73 74 45     int xGetLastE
10263 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73  rror(sqlite3_vfs
10264 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66   *pVfs, int nBuf
10265 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 2a  , char *zBuf){.*
10266 2a 20 20 20 20 20 61 73 73 65 72 74 28 7a 42 75  *     assert(zBu
10267 66 5b 30 5d 3d 3d 27 5c 30 27 29 3b 0a 2a 2a 20  f[0]=='\0');.** 
10268 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 2a 2a      return 0;.**
10269 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 76     }.**.** Howev
1026a 65 72 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6d  er if an error m
1026b 65 73 73 61 67 65 20 69 73 20 73 75 70 70 6c 69  essage is suppli
1026c 65 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 69  ed, it will be i
1026d 6e 63 6f 72 70 6f 72 61 74 65 64 0a 2a 2a 20 62  ncorporated.** b
1026e 79 20 73 71 6c 69 74 65 20 69 6e 74 6f 20 74 68  y sqlite into th
1026f 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
10270 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 65  available to the
10271 20 75 73 65 72 20 75 73 69 6e 67 0a 2a 2a 20 73   user using.** s
10272 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 2c  qlite3_errmsg(),
10273 20 70 6f 73 73 69 62 6c 79 20 6d 61 6b 69 6e 67   possibly making
10274 20 49 4f 20 65 72 72 6f 72 73 20 65 61 73 69 65   IO errors easie
10275 72 20 74 6f 20 64 65 62 75 67 2e 0a 2a 2f 0a 73  r to debug..*/.s
10276 74 61 74 69 63 20 69 6e 74 20 77 69 6e 47 65 74  tatic int winGet
10277 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 65  LastError(sqlite
10278 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74  3_vfs *pVfs, int
10279 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75   nBuf, char *zBu
1027a 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  f){.  UNUSED_PAR
1027b 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20  AMETER(pVfs);.  
1027c 72 65 74 75 72 6e 20 67 65 74 4c 61 73 74 45 72  return getLastEr
1027d 72 6f 72 4d 73 67 28 6e 42 75 66 2c 20 7a 42 75  rorMsg(nBuf, zBu
1027e 66 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69  f);.}../*.** Ini
1027f 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e  tialize and dein
10280 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65  itialize the ope
10281 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e  rating system in
10282 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49  terface..*/.SQLI
10283 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
10284 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29  e3_os_init(void)
10285 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74  {.  static sqlit
10286 65 33 5f 76 66 73 20 77 69 6e 56 66 73 20 3d 20  e3_vfs winVfs = 
10287 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 20  {.    1,        
10288 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72           /* iVer
10289 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65  sion */.    size
1028a 6f 66 28 77 69 6e 46 69 6c 65 29 2c 20 20 20 2f  of(winFile),   /
1028b 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20  * szOsFile */.  
1028c 20 20 4d 41 58 5f 50 41 54 48 2c 20 20 20 20 20    MAX_PATH,     
1028d 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61       /* mxPathna
1028e 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20  me */.    0,    
1028f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10290 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 77 69  pNext */.    "wi
10291 6e 33 32 22 2c 20 20 20 20 20 20 20 20 20 20 20  n32",           
10292 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20  /* zName */.    
10293 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
10294 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a     /* pAppData *
10295 2f 0a 20 0a 20 20 20 20 77 69 6e 4f 70 65 6e 2c  /. .    winOpen,
10296 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f             /* xO
10297 70 65 6e 20 2a 2f 0a 20 20 20 20 77 69 6e 44 65  pen */.    winDe
10298 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  lete,         /*
10299 20 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20   xDelete */.    
1029a 77 69 6e 41 63 63 65 73 73 2c 20 20 20 20 20 20  winAccess,      
1029b 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f     /* xAccess */
1029c 0a 20 20 20 20 77 69 6e 46 75 6c 6c 50 61 74 68  .    winFullPath
1029d 6e 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c  name,   /* xFull
1029e 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20  Pathname */.    
1029f 77 69 6e 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20  winDlOpen,      
102a0 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f     /* xDlOpen */
102a1 0a 20 20 20 20 77 69 6e 44 6c 45 72 72 6f 72 2c  .    winDlError,
102a2 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72          /* xDlEr
102a3 72 6f 72 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c  ror */.    winDl
102a4 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Sym,          /*
102a5 20 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 77   xDlSym */.    w
102a6 69 6e 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20  inDlClose,      
102a7 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f    /* xDlClose */
102a8 0a 20 20 20 20 77 69 6e 52 61 6e 64 6f 6d 6e 65  .    winRandomne
102a9 73 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64  ss,     /* xRand
102aa 6f 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 77 69  omness */.    wi
102ab 6e 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20  nSleep,         
102ac 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20   /* xSleep */.  
102ad 20 20 77 69 6e 43 75 72 72 65 6e 74 54 69 6d 65    winCurrentTime
102ae 2c 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74  ,    /* xCurrent
102af 54 69 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e 47  Time */.    winG
102b0 65 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f  etLastError    /
102b1 2a 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20  * xGetLastError 
102b2 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 73 71 6c 69 74  */.  };..  sqlit
102b3 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28  e3_vfs_register(
102b4 26 77 69 6e 56 66 73 2c 20 31 29 3b 0a 20 20 72  &winVfs, 1);.  r
102b5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
102b6 20 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   .}.SQLITE_API i
102b7 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e  nt sqlite3_os_en
102b8 64 28 76 6f 69 64 29 7b 20 0a 20 20 72 65 74 75  d(void){ .  retu
102b9 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
102ba 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
102bb 45 5f 4f 53 5f 57 49 4e 20 2a 2f 0a 0a 2f 2a 2a  E_OS_WIN */../**
102bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
102bd 20 6f 66 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a   of os_win.c ***
102be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
102c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
102c2 69 6e 20 66 69 6c 65 20 62 69 74 76 65 63 2e 63  in file bitvec.c
102c3 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
102c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
102c6 2a 2a 20 32 30 30 38 20 46 65 62 72 75 61 72 79  ** 2008 February
102c7 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   16.**.** The au
102c8 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
102c9 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
102ca 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
102cb 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
102cc 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
102cd 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
102ce 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
102cf 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
102d0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
102d1 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
102d2 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
102d3 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
102d4 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
102d5 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
102d6 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
102d7 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
102d8 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
102d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
102dd 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
102de 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 6f 62  implements an ob
102df 6a 65 63 74 20 74 68 61 74 20 72 65 70 72 65 73  ject that repres
102e0 65 6e 74 73 20 61 20 66 69 78 65 64 2d 6c 65 6e  ents a fixed-len
102e1 67 74 68 0a 2a 2a 20 62 69 74 6d 61 70 2e 20 20  gth.** bitmap.  
102e2 42 69 74 73 20 61 72 65 20 6e 75 6d 62 65 72 65  Bits are numbere
102e3 64 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20  d starting with 
102e4 31 2e 0a 2a 2a 0a 2a 2a 20 41 20 62 69 74 6d 61  1..**.** A bitma
102e5 70 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 63  p is used to rec
102e6 6f 72 64 20 77 68 69 63 68 20 70 61 67 65 73 20  ord which pages 
102e7 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 66 69  of a database fi
102e8 6c 65 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20  le have been.** 
102e9 6a 6f 75 72 6e 61 6c 6c 65 64 20 64 75 72 69 6e  journalled durin
102ea 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  g a transaction,
102eb 20 6f 72 20 77 68 69 63 68 20 70 61 67 65 73 20   or which pages 
102ec 68 61 76 65 20 74 68 65 20 22 64 6f 6e 74 2d 77  have the "dont-w
102ed 72 69 74 65 22 0a 2a 2a 20 70 72 6f 70 65 72 74  rite".** propert
102ee 79 2e 20 20 55 73 75 61 6c 6c 79 20 6f 6e 6c 79  y.  Usually only
102ef 20 61 20 66 65 77 20 70 61 67 65 73 20 61 72 65   a few pages are
102f0 20 6d 65 65 74 20 65 69 74 68 65 72 20 63 6f 6e   meet either con
102f1 64 69 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 20 74 68  dition..** So th
102f2 65 20 62 69 74 6d 61 70 20 69 73 20 75 73 75 61  e bitmap is usua
102f3 6c 6c 79 20 73 70 61 72 73 65 20 61 6e 64 20 68  lly sparse and h
102f4 61 73 20 6c 6f 77 20 63 61 72 64 69 6e 61 6c 69  as low cardinali
102f5 74 79 2e 0a 2a 2a 20 42 75 74 20 73 6f 6d 65 74  ty..** But somet
102f6 69 6d 65 73 20 28 66 6f 72 20 65 78 61 6d 70 6c  imes (for exampl
102f7 65 20 77 68 65 6e 20 64 75 72 69 6e 67 20 61 20  e when during a 
102f8 44 52 4f 50 20 6f 66 20 61 20 6c 61 72 67 65 20  DROP of a large 
102f9 74 61 62 6c 65 29 20 6d 6f 73 74 0a 2a 2a 20 6f  table) most.** o
102fa 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 61 67  r all of the pag
102fb 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 65  es in a database
102fc 20 63 61 6e 20 67 65 74 20 6a 6f 75 72 6e 61 6c   can get journal
102fd 6c 65 64 2e 20 20 49 6e 20 74 68 6f 73 65 20 63  led.  In those c
102fe 61 73 65 73 2c 20 0a 2a 2a 20 74 68 65 20 62 69  ases, .** the bi
102ff 74 6d 61 70 20 62 65 63 6f 6d 65 73 20 64 65 6e  tmap becomes den
10300 73 65 20 77 69 74 68 20 68 69 67 68 20 63 61 72  se with high car
10301 64 69 6e 61 6c 69 74 79 2e 20 20 54 68 65 20 61  dinality.  The a
10302 6c 67 6f 72 69 74 68 6d 20 6e 65 65 64 73 20 0a  lgorithm needs .
10303 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 62 6f 74  ** to handle bot
10304 68 20 63 61 73 65 73 20 77 65 6c 6c 2e 0a 2a 2a  h cases well..**
10305 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20  .** The size of 
10306 74 68 65 20 62 69 74 6d 61 70 20 69 73 20 66 69  the bitmap is fi
10307 78 65 64 20 77 68 65 6e 20 74 68 65 20 6f 62 6a  xed when the obj
10308 65 63 74 20 69 73 20 63 72 65 61 74 65 64 2e 0a  ect is created..
10309 2a 2a 0a 2a 2a 20 41 6c 6c 20 62 69 74 73 20 61  **.** All bits a
1030a 72 65 20 63 6c 65 61 72 20 77 68 65 6e 20 74 68  re clear when th
1030b 65 20 62 69 74 6d 61 70 20 69 73 20 63 72 65 61  e bitmap is crea
1030c 74 65 64 2e 20 20 49 6e 64 69 76 69 64 75 61 6c  ted.  Individual
1030d 20 62 69 74 73 0a 2a 2a 20 6d 61 79 20 62 65 20   bits.** may be 
1030e 73 65 74 20 6f 72 20 63 6c 65 61 72 65 64 20 6f  set or cleared o
1030f 6e 65 20 61 74 20 61 20 74 69 6d 65 2e 0a 2a 2a  ne at a time..**
10310 0a 2a 2a 20 54 65 73 74 20 6f 70 65 72 61 74 69  .** Test operati
10311 6f 6e 73 20 61 72 65 20 61 62 6f 75 74 20 31 30  ons are about 10
10312 30 20 74 69 6d 65 73 20 6d 6f 72 65 20 63 6f 6d  0 times more com
10313 6d 6f 6e 20 74 68 61 74 20 73 65 74 20 6f 70 65  mon that set ope
10314 72 61 74 69 6f 6e 73 2e 0a 2a 2a 20 43 6c 65 61  rations..** Clea
10315 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65  r operations are
10316 20 65 78 63 65 65 64 69 6e 67 6c 79 20 72 61 72   exceedingly rar
10317 65 2e 20 20 54 68 65 72 65 20 61 72 65 20 75 73  e.  There are us
10318 75 61 6c 6c 79 20 62 65 74 77 65 65 6e 0a 2a 2a  ually between.**
10319 20 35 20 61 6e 64 20 35 30 30 20 73 65 74 20 6f   5 and 500 set o
1031a 70 65 72 61 74 69 6f 6e 73 20 70 65 72 20 42 69  perations per Bi
1031b 74 76 65 63 20 6f 62 6a 65 63 74 2c 20 74 68 6f  tvec object, tho
1031c 75 67 68 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ugh the number o
1031d 66 20 73 65 74 73 20 63 61 6e 0a 2a 2a 20 73 6f  f sets can.** so
1031e 6d 65 74 69 6d 65 73 20 67 72 6f 77 20 69 6e 74  metimes grow int
1031f 6f 20 74 65 6e 73 20 6f 66 20 74 68 6f 75 73 61  o tens of thousa
10320 6e 64 73 20 6f 72 20 6c 61 72 67 65 72 2e 20 20  nds or larger.  
10321 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 0a  The size of the.
10322 2a 2a 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74  ** Bitvec object
10323 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
10324 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64  f pages in the d
10325 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 20  atabase file at 
10326 74 68 65 0a 2a 2a 20 73 74 61 72 74 20 6f 66 20  the.** start of 
10327 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61  a transaction, a
10328 6e 64 20 69 73 20 74 68 75 73 20 75 73 75 61 6c  nd is thus usual
10329 6c 79 20 6c 65 73 73 20 74 68 61 6e 20 61 20 66  ly less than a f
1032a 65 77 20 74 68 6f 75 73 61 6e 64 2c 0a 2a 2a 20  ew thousand,.** 
1032b 62 75 74 20 63 61 6e 20 62 65 20 61 73 20 6c 61  but can be as la
1032c 72 67 65 20 61 73 20 32 20 62 69 6c 6c 69 6f 6e  rge as 2 billion
1032d 20 66 6f 72 20 61 20 72 65 61 6c 6c 79 20 62 69   for a really bi
1032e 67 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a  g database..**.*
1032f 2a 20 40 28 23 29 20 24 49 64 3a 20 62 69 74 76  * @(#) $Id: bitv
10330 65 63 2e 63 2c 76 20 31 2e 31 37 20 32 30 30 39  ec.c,v 1.17 2009
10331 2f 30 37 2f 32 35 20 31 37 3a 33 33 3a 32 36 20  /07/25 17:33:26 
10332 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  drh Exp $.*/../*
10333 20 53 69 7a 65 20 6f 66 20 74 68 65 20 42 69 74   Size of the Bit
10334 76 65 63 20 73 74 72 75 63 74 75 72 65 20 69 6e  vec structure in
10335 20 62 79 74 65 73 2e 20 2a 2f 0a 23 64 65 66 69   bytes. */.#defi
10336 6e 65 20 42 49 54 56 45 43 5f 53 5a 20 20 20 20  ne BITVEC_SZ    
10337 20 20 20 20 28 73 69 7a 65 6f 66 28 76 6f 69 64      (sizeof(void
10338 2a 29 2a 31 32 38 29 20 20 2f 2a 20 35 31 32 20  *)*128)  /* 512 
10339 6f 6e 20 33 32 62 69 74 2e 20 20 31 30 32 34 20  on 32bit.  1024 
1033a 6f 6e 20 36 34 62 69 74 20 2a 2f 0a 0a 2f 2a 20  on 64bit */../* 
1033b 52 6f 75 6e 64 20 74 68 65 20 75 6e 69 6f 6e 20  Round the union 
1033c 73 69 7a 65 20 64 6f 77 6e 20 74 6f 20 74 68 65  size down to the
1033d 20 6e 65 61 72 65 73 74 20 70 6f 69 6e 74 65 72   nearest pointer
1033e 20 62 6f 75 6e 64 61 72 79 2c 20 73 69 6e 63 65   boundary, since
1033f 20 74 68 61 74 27 73 20 68 6f 77 20 0a 2a 2a 20   that's how .** 
10340 69 74 20 77 69 6c 6c 20 62 65 20 61 6c 69 67 6e  it will be align
10341 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 42 69  ed within the Bi
10342 74 76 65 63 20 73 74 72 75 63 74 2e 20 2a 2f 0a  tvec struct. */.
10343 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 55  #define BITVEC_U
10344 53 49 5a 45 20 20 20 20 20 28 28 28 42 49 54 56  SIZE     (((BITV
10345 45 43 5f 53 5a 2d 28 33 2a 73 69 7a 65 6f 66 28  EC_SZ-(3*sizeof(
10346 75 33 32 29 29 29 2f 73 69 7a 65 6f 66 28 42 69  u32)))/sizeof(Bi
10347 74 76 65 63 2a 29 29 2a 73 69 7a 65 6f 66 28 42  tvec*))*sizeof(B
10348 69 74 76 65 63 2a 29 29 0a 0a 2f 2a 20 54 79 70  itvec*))../* Typ
10349 65 20 6f 66 20 74 68 65 20 61 72 72 61 79 20 22  e of the array "
1034a 65 6c 65 6d 65 6e 74 22 20 66 6f 72 20 74 68 65  element" for the
1034b 20 62 69 74 6d 61 70 20 72 65 70 72 65 73 65 6e   bitmap represen
1034c 74 61 74 69 6f 6e 2e 20 0a 2a 2a 20 53 68 6f 75  tation. .** Shou
1034d 6c 64 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66  ld be a power of
1034e 20 32 2c 20 61 6e 64 20 69 64 65 61 6c 6c 79 2c   2, and ideally,
1034f 20 65 76 65 6e 6c 79 20 64 69 76 69 64 65 20 69   evenly divide i
10350 6e 74 6f 20 42 49 54 56 45 43 5f 55 53 49 5a 45  nto BITVEC_USIZE
10351 2e 20 0a 2a 2a 20 53 65 74 74 69 6e 67 20 74 68  . .** Setting th
10352 69 73 20 74 6f 20 74 68 65 20 22 6e 61 74 75 72  is to the "natur
10353 61 6c 20 77 6f 72 64 22 20 73 69 7a 65 20 6f 66  al word" size of
10354 20 79 6f 75 72 20 43 50 55 20 6d 61 79 20 69 6d   your CPU may im
10355 70 72 6f 76 65 0a 2a 2a 20 70 65 72 66 6f 72 6d  prove.** perform
10356 61 6e 63 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65  ance. */.#define
10357 20 42 49 54 56 45 43 5f 54 45 4c 45 4d 20 20 20   BITVEC_TELEM   
10358 20 20 75 38 0a 2f 2a 20 53 69 7a 65 2c 20 69 6e    u8./* Size, in
10359 20 62 69 74 73 2c 20 6f 66 20 74 68 65 20 62 69   bits, of the bi
1035a 74 6d 61 70 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f  tmap element. */
1035b 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f  .#define BITVEC_
1035c 53 5a 45 4c 45 4d 20 20 20 20 38 0a 2f 2a 20 4e  SZELEM    8./* N
1035d 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74  umber of element
1035e 73 20 69 6e 20 61 20 62 69 74 6d 61 70 20 61 72  s in a bitmap ar
1035f 72 61 79 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ray. */.#define 
10360 42 49 54 56 45 43 5f 4e 45 4c 45 4d 20 20 20 20  BITVEC_NELEM    
10361 20 28 42 49 54 56 45 43 5f 55 53 49 5a 45 2f 73   (BITVEC_USIZE/s
10362 69 7a 65 6f 66 28 42 49 54 56 45 43 5f 54 45 4c  izeof(BITVEC_TEL
10363 45 4d 29 29 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f  EM))./* Number o
10364 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 62 69  f bits in the bi
10365 74 6d 61 70 20 61 72 72 61 79 2e 20 2a 2f 0a 23  tmap array. */.#
10366 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e 42  define BITVEC_NB
10367 49 54 20 20 20 20 20 20 28 42 49 54 56 45 43 5f  IT      (BITVEC_
10368 4e 45 4c 45 4d 2a 42 49 54 56 45 43 5f 53 5a 45  NELEM*BITVEC_SZE
10369 4c 45 4d 29 0a 0a 2f 2a 20 4e 75 6d 62 65 72 20  LEM)../* Number 
1036a 6f 66 20 75 33 32 20 76 61 6c 75 65 73 20 69 6e  of u32 values in
1036b 20 68 61 73 68 20 74 61 62 6c 65 2e 20 2a 2f 0a   hash table. */.
1036c 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e  #define BITVEC_N
1036d 49 4e 54 20 20 20 20 20 20 28 42 49 54 56 45 43  INT      (BITVEC
1036e 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28 75 33  _USIZE/sizeof(u3
1036f 32 29 29 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e  2))./* Maximum n
10370 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
10371 20 69 6e 20 68 61 73 68 20 74 61 62 6c 65 20 62   in hash table b
10372 65 66 6f 72 65 20 0a 2a 2a 20 73 75 62 2d 64 69  efore .** sub-di
10373 76 69 64 69 6e 67 20 61 6e 64 20 72 65 2d 68 61  viding and re-ha
10374 73 68 69 6e 67 2e 20 2a 2f 0a 23 64 65 66 69 6e  shing. */.#defin
10375 65 20 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20  e BITVEC_MXHASH 
10376 20 20 20 28 42 49 54 56 45 43 5f 4e 49 4e 54 2f     (BITVEC_NINT/
10377 32 29 0a 2f 2a 20 48 61 73 68 69 6e 67 20 66 75  2)./* Hashing fu
10378 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 61  nction for the a
10379 48 61 73 68 20 72 65 70 72 65 73 65 6e 74 61 74  Hash representat
1037a 69 6f 6e 2e 0a 2a 2a 20 45 6d 70 69 72 69 63 61  ion..** Empirica
1037b 6c 20 74 65 73 74 69 6e 67 20 73 68 6f 77 65 64  l testing showed
1037c 20 74 68 61 74 20 74 68 65 20 2a 33 37 20 6d 75   that the *37 mu
1037d 6c 74 69 70 6c 69 65 72 20 0a 2a 2a 20 28 61 6e  ltiplier .** (an
1037e 20 61 72 62 69 74 72 61 72 79 20 70 72 69 6d 65   arbitrary prime
1037f 29 69 6e 20 74 68 65 20 68 61 73 68 20 66 75 6e  )in the hash fun
10380 63 74 69 6f 6e 20 70 72 6f 76 69 64 65 64 20 0a  ction provided .
10381 2a 2a 20 6e 6f 20 66 65 77 65 72 20 63 6f 6c 6c  ** no fewer coll
10382 69 73 69 6f 6e 73 20 74 68 61 6e 20 74 68 65 20  isions than the 
10383 6e 6f 2d 6f 70 20 2a 31 2e 20 2a 2f 0a 23 64 65  no-op *1. */.#de
10384 66 69 6e 65 20 42 49 54 56 45 43 5f 48 41 53 48  fine BITVEC_HASH
10385 28 58 29 20 20 20 28 28 28 58 29 2a 31 29 25 42  (X)   (((X)*1)%B
10386 49 54 56 45 43 5f 4e 49 4e 54 29 0a 0a 23 64 65  ITVEC_NINT)..#de
10387 66 69 6e 65 20 42 49 54 56 45 43 5f 4e 50 54 52  fine BITVEC_NPTR
10388 20 20 20 20 20 20 28 42 49 54 56 45 43 5f 55 53        (BITVEC_US
10389 49 5a 45 2f 73 69 7a 65 6f 66 28 42 69 74 76 65  IZE/sizeof(Bitve
1038a 63 20 2a 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20  c *)).../*.** A 
1038b 62 69 74 6d 61 70 20 69 73 20 61 6e 20 69 6e 73  bitmap is an ins
1038c 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
1038d 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
1038e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 62 69 74  ..**.** This bit
1038f 6d 61 70 20 72 65 63 6f 72 64 73 20 74 68 65 20  map records the 
10390 65 78 69 73 74 61 6e 63 65 20 6f 66 20 7a 65 72  existance of zer
10391 6f 20 6f 72 20 6d 6f 72 65 20 62 69 74 73 0a 2a  o or more bits.*
10392 2a 20 77 69 74 68 20 76 61 6c 75 65 73 20 62 65  * with values be
10393 74 77 65 65 6e 20 31 20 61 6e 64 20 69 53 69 7a  tween 1 and iSiz
10394 65 2c 20 69 6e 63 6c 75 73 69 76 65 2e 0a 2a 2a  e, inclusive..**
10395 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68  .** There are th
10396 72 65 65 20 70 6f 73 73 69 62 6c 65 20 72 65 70  ree possible rep
10397 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20  resentations of 
10398 74 68 65 20 62 69 74 6d 61 70 2e 0a 2a 2a 20 49  the bitmap..** I
10399 66 20 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f  f iSize<=BITVEC_
1039a 4e 42 49 54 2c 20 74 68 65 6e 20 42 69 74 76 65  NBIT, then Bitve
1039b 63 2e 75 2e 61 42 69 74 6d 61 70 5b 5d 20 69 73  c.u.aBitmap[] is
1039c 20 61 20 73 74 72 61 69 67 68 74 0a 2a 2a 20 62   a straight.** b
1039d 69 74 6d 61 70 2e 20 20 54 68 65 20 6c 65 61 73  itmap.  The leas
1039e 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 62 69  t significant bi
1039f 74 20 69 73 20 62 69 74 20 31 2e 0a 2a 2a 0a 2a  t is bit 1..**.*
103a0 2a 20 49 66 20 69 53 69 7a 65 3e 42 49 54 56 45  * If iSize>BITVE
103a1 43 5f 4e 42 49 54 20 61 6e 64 20 69 44 69 76 69  C_NBIT and iDivi
103a2 73 6f 72 3d 3d 30 20 74 68 65 6e 20 42 69 74 76  sor==0 then Bitv
103a3 65 63 2e 75 2e 61 48 61 73 68 5b 5d 20 69 73 0a  ec.u.aHash[] is.
103a4 2a 2a 20 61 20 68 61 73 68 20 74 61 62 6c 65 20  ** a hash table 
103a5 74 68 61 74 20 77 69 6c 6c 20 68 6f 6c 64 20 75  that will hold u
103a6 70 20 74 6f 20 42 49 54 56 45 43 5f 4d 58 48 41  p to BITVEC_MXHA
103a7 53 48 20 64 69 73 74 69 6e 63 74 20 76 61 6c 75  SH distinct valu
103a8 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77  es..**.** Otherw
103a9 69 73 65 2c 20 74 68 65 20 76 61 6c 75 65 20 69  ise, the value i
103aa 20 69 73 20 72 65 64 69 72 65 63 74 65 64 20 69   is redirected i
103ab 6e 74 6f 20 6f 6e 65 20 6f 66 20 42 49 54 56 45  nto one of BITVE
103ac 43 5f 4e 50 54 52 0a 2a 2a 20 73 75 62 2d 62 69  C_NPTR.** sub-bi
103ad 74 6d 61 70 73 20 70 6f 69 6e 74 65 64 20 74 6f  tmaps pointed to
103ae 20 62 79 20 42 69 74 76 65 63 2e 75 2e 61 70 53   by Bitvec.u.apS
103af 75 62 5b 5d 2e 20 20 45 61 63 68 20 73 75 62 62  ub[].  Each subb
103b0 69 74 6d 61 70 0a 2a 2a 20 68 61 6e 64 6c 65 73  itmap.** handles
103b1 20 75 70 20 74 6f 20 69 44 69 76 69 73 6f 72 20   up to iDivisor 
103b2 73 65 70 61 72 61 74 65 20 76 61 6c 75 65 73 20  separate values 
103b3 6f 66 20 69 2e 20 20 61 70 53 75 62 5b 30 5d 20  of i.  apSub[0] 
103b4 68 6f 6c 64 73 0a 2a 2a 20 76 61 6c 75 65 73 20  holds.** values 
103b5 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 69 44  between 1 and iD
103b6 69 76 69 73 6f 72 2e 20 20 61 70 53 75 62 5b 31  ivisor.  apSub[1
103b7 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65 73 20 62  ] holds values b
103b8 65 74 77 65 65 6e 0a 2a 2a 20 69 44 69 76 69 73  etween.** iDivis
103b9 6f 72 2b 31 20 61 6e 64 20 32 2a 69 44 69 76 69  or+1 and 2*iDivi
103ba 73 6f 72 2e 20 20 61 70 53 75 62 5b 4e 5d 20 68  sor.  apSub[N] h
103bb 6f 6c 64 73 20 76 61 6c 75 65 73 20 62 65 74 77  olds values betw
103bc 65 65 6e 0a 2a 2a 20 4e 2a 69 44 69 76 69 73 6f  een.** N*iDiviso
103bd 72 2b 31 20 61 6e 64 20 28 4e 2b 31 29 2a 69 44  r+1 and (N+1)*iD
103be 69 76 69 73 6f 72 2e 20 20 45 61 63 68 20 73 75  ivisor.  Each su
103bf 62 62 69 74 6d 61 70 20 69 73 20 6e 6f 72 6d 61  bbitmap is norma
103c0 6c 69 7a 65 64 0a 2a 2a 20 74 6f 20 68 6f 6c 64  lized.** to hold
103c1 20 64 65 61 6c 20 77 69 74 68 20 76 61 6c 75 65   deal with value
103c2 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20  s between 1 and 
103c3 69 44 69 76 69 73 6f 72 2e 0a 2a 2f 0a 73 74 72  iDivisor..*/.str
103c4 75 63 74 20 42 69 74 76 65 63 20 7b 0a 20 20 75  uct Bitvec {.  u
103c5 33 32 20 69 53 69 7a 65 3b 20 20 20 20 20 20 2f  32 iSize;      /
103c6 2a 20 4d 61 78 69 6d 75 6d 20 62 69 74 20 69 6e  * Maximum bit in
103c7 64 65 78 2e 20 20 4d 61 78 20 69 53 69 7a 65 20  dex.  Max iSize 
103c8 69 73 20 34 2c 32 39 34 2c 39 36 37 2c 32 39 36  is 4,294,967,296
103c9 2e 20 2a 2f 0a 20 20 75 33 32 20 6e 53 65 74 3b  . */.  u32 nSet;
103ca 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
103cb 20 6f 66 20 62 69 74 73 20 74 68 61 74 20 61 72   of bits that ar
103cc 65 20 73 65 74 20 2d 20 6f 6e 6c 79 20 76 61 6c  e set - only val
103cd 69 64 20 66 6f 72 20 61 48 61 73 68 0a 20 20 20  id for aHash.   
103ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
103cf 2a 20 65 6c 65 6d 65 6e 74 2e 20 20 4d 61 78 20  * element.  Max 
103d0 69 73 20 42 49 54 56 45 43 5f 4e 49 4e 54 2e 20  is BITVEC_NINT. 
103d1 20 46 6f 72 20 42 49 54 56 45 43 5f 53 5a 20 6f   For BITVEC_SZ o
103d2 66 20 35 31 32 2c 0a 20 20 20 20 20 20 20 20 20  f 512,.         
103d3 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69 73           ** this
103d4 20 77 6f 75 6c 64 20 62 65 20 31 32 35 2e 20 2a   would be 125. *
103d5 2f 0a 20 20 75 33 32 20 69 44 69 76 69 73 6f 72  /.  u32 iDivisor
103d6 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66  ;   /* Number of
103d7 20 62 69 74 73 20 68 61 6e 64 6c 65 64 20 62 79   bits handled by
103d8 20 65 61 63 68 20 61 70 53 75 62 5b 5d 20 65 6e   each apSub[] en
103d9 74 72 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  try. */.        
103da 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 6f            /* Sho
103db 75 6c 64 20 3e 3d 30 20 66 6f 72 20 61 70 53 75  uld >=0 for apSu
103dc 62 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20  b element. */.  
103dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
103de 2f 2a 20 4d 61 78 20 69 44 69 76 69 73 6f 72 20  /* Max iDivisor 
103df 69 73 20 6d 61 78 28 75 33 32 29 20 2f 20 42 49  is max(u32) / BI
103e0 54 56 45 43 5f 4e 50 54 52 20 2b 20 31 2e 20 20  TVEC_NPTR + 1.  
103e1 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
103e2 20 20 20 20 20 2f 2a 20 46 6f 72 20 61 20 42 49       /* For a BI
103e3 54 56 45 43 5f 53 5a 20 6f 66 20 35 31 32 2c 20  TVEC_SZ of 512, 
103e4 74 68 69 73 20 77 6f 75 6c 64 20 62 65 20 33 34  this would be 34
103e5 2c 33 35 39 2c 37 33 39 2e 20 2a 2f 0a 20 20 75  ,359,739. */.  u
103e6 6e 69 6f 6e 20 7b 0a 20 20 20 20 42 49 54 56 45  nion {.    BITVE
103e7 43 5f 54 45 4c 45 4d 20 61 42 69 74 6d 61 70 5b  C_TELEM aBitmap[
103e8 42 49 54 56 45 43 5f 4e 45 4c 45 4d 5d 3b 20 20  BITVEC_NELEM];  
103e9 20 20 2f 2a 20 42 69 74 6d 61 70 20 72 65 70 72    /* Bitmap repr
103ea 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20  esentation */.  
103eb 20 20 75 33 32 20 61 48 61 73 68 5b 42 49 54 56    u32 aHash[BITV
103ec 45 43 5f 4e 49 4e 54 5d 3b 20 20 20 20 20 20 2f  EC_NINT];      /
103ed 2a 20 48 61 73 68 20 74 61 62 6c 65 20 72 65 70  * Hash table rep
103ee 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20  resentation */. 
103ef 20 20 20 42 69 74 76 65 63 20 2a 61 70 53 75 62     Bitvec *apSub
103f0 5b 42 49 54 56 45 43 5f 4e 50 54 52 5d 3b 20 20  [BITVEC_NPTR];  
103f1 2f 2a 20 52 65 63 75 72 73 69 76 65 20 72 65 70  /* Recursive rep
103f2 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20  resentation */. 
103f3 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20   } u;.};../*.** 
103f4 43 72 65 61 74 65 20 61 20 6e 65 77 20 62 69 74  Create a new bit
103f5 6d 61 70 20 6f 62 6a 65 63 74 20 61 62 6c 65 20  map object able 
103f6 74 6f 20 68 61 6e 64 6c 65 20 62 69 74 73 20 62  to handle bits b
103f7 65 74 77 65 65 6e 20 30 20 61 6e 64 20 69 53 69  etween 0 and iSi
103f8 7a 65 2c 0a 2a 2a 20 69 6e 63 6c 75 73 69 76 65  ze,.** inclusive
103f9 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  .  Return a poin
103fa 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 6f  ter to the new o
103fb 62 6a 65 63 74 2e 20 20 52 65 74 75 72 6e 20 4e  bject.  Return N
103fc 55 4c 4c 20 69 66 20 0a 2a 2a 20 6d 61 6c 6c 6f  ULL if .** mallo
103fd 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49  c fails..*/.SQLI
103fe 54 45 5f 50 52 49 56 41 54 45 20 42 69 74 76 65  TE_PRIVATE Bitve
103ff 63 20 2a 73 71 6c 69 74 65 33 42 69 74 76 65 63  c *sqlite3Bitvec
10400 43 72 65 61 74 65 28 75 33 32 20 69 53 69 7a 65  Create(u32 iSize
10401 29 7b 0a 20 20 42 69 74 76 65 63 20 2a 70 3b 0a  ){.  Bitvec *p;.
10402 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
10403 28 2a 70 29 3d 3d 42 49 54 56 45 43 5f 53 5a 20  (*p)==BITVEC_SZ 
10404 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  );.  p = sqlite3
10405 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65  MallocZero( size
10406 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69 66 28 20  of(*p) );.  if( 
10407 70 20 29 7b 0a 20 20 20 20 70 2d 3e 69 53 69 7a  p ){.    p->iSiz
10408 65 20 3d 20 69 53 69 7a 65 3b 0a 20 20 7d 0a 20  e = iSize;.  }. 
10409 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
1040a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65  .** Check to see
1040b 20 69 66 20 74 68 65 20 69 2d 74 68 20 62 69 74   if the i-th bit
1040c 20 69 73 20 73 65 74 2e 20 20 52 65 74 75 72 6e   is set.  Return
1040d 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65 2e 0a   true or false..
1040e 2a 2a 20 49 66 20 70 20 69 73 20 4e 55 4c 4c 20  ** If p is NULL 
1040f 28 69 66 20 74 68 65 20 62 69 74 6d 61 70 20 68  (if the bitmap h
10410 61 73 20 6e 6f 74 20 62 65 65 6e 20 63 72 65 61  as not been crea
10411 74 65 64 29 20 6f 72 20 69 66 0a 2a 2a 20 69 20  ted) or if.** i 
10412 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c  is out of range,
10413 20 74 68 65 6e 20 72 65 74 75 72 6e 20 66 61 6c   then return fal
10414 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
10415 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
10416 33 42 69 74 76 65 63 54 65 73 74 28 42 69 74 76  3BitvecTest(Bitv
10417 65 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20  ec *p, u32 i){. 
10418 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
10419 72 6e 20 30 3b 0a 20 20 69 66 28 20 69 3e 70 2d  rn 0;.  if( i>p-
1041a 3e 69 53 69 7a 65 20 7c 7c 20 69 3d 3d 30 20 29  >iSize || i==0 )
1041b 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 2d 2d   return 0;.  i--
1041c 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 69 44  ;.  while( p->iD
1041d 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 33  ivisor ){.    u3
1041e 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69  2 bin = i/p->iDi
1041f 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 69  visor;.    i = i
10420 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20  %p->iDivisor;.  
10421 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75 62    p = p->u.apSub
10422 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 66 20 28 21  [bin];.    if (!
10423 70 29 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72  p) {.      retur
10424 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 0;.    }.  }. 
10425 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42   if( p->iSize<=B
10426 49 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20  ITVEC_NBIT ){.  
10427 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 75 2e 61    return (p->u.a
10428 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f  Bitmap[i/BITVEC_
10429 53 5a 45 4c 45 4d 5d 20 26 20 28 31 3c 3c 28 69  SZELEM] & (1<<(i
1042a 26 28 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 2d  &(BITVEC_SZELEM-
1042b 31 29 29 29 29 21 3d 30 3b 0a 20 20 7d 20 65 6c  1))))!=0;.  } el
1042c 73 65 7b 0a 20 20 20 20 75 33 32 20 68 20 3d 20  se{.    u32 h = 
1042d 42 49 54 56 45 43 5f 48 41 53 48 28 69 2b 2b 29  BITVEC_HASH(i++)
1042e 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e  ;.    while( p->
1042f 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20  u.aHash[h] ){.  
10430 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61 48 61      if( p->u.aHa
10431 73 68 5b 68 5d 3d 3d 69 20 29 20 72 65 74 75 72  sh[h]==i ) retur
10432 6e 20 31 3b 0a 20 20 20 20 20 20 68 20 3d 20 28  n 1;.      h = (
10433 68 2b 31 29 20 25 20 42 49 54 56 45 43 5f 4e 49  h+1) % BITVEC_NI
10434 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  NT;.    }.    re
10435 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f  turn 0;.  }.}../
10436 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 69 2d 74  *.** Set the i-t
10437 68 20 62 69 74 2e 20 20 52 65 74 75 72 6e 20 30  h bit.  Return 0
10438 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20   on success and 
10439 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66  an error code if
1043a 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 67 6f 65  .** anything goe
1043b 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54  s wrong..**.** T
1043c 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68  his routine migh
1043d 74 20 63 61 75 73 65 20 73 75 62 2d 62 69 74 6d  t cause sub-bitm
1043e 61 70 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61  aps to be alloca
1043f 74 65 64 2e 20 20 46 61 69 6c 69 6e 67 0a 2a 2a  ted.  Failing.**
10440 20 74 6f 20 67 65 74 20 74 68 65 20 6d 65 6d 6f   to get the memo
10441 72 79 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c  ry needed to hol
10442 64 20 74 68 65 20 73 75 62 2d 62 69 74 6d 61 70  d the sub-bitmap
10443 20 69 73 20 74 68 65 20 6f 6e 6c 79 0a 2a 2a 20   is the only.** 
10444 74 68 61 74 20 63 61 6e 20 67 6f 20 77 72 6f 6e  that can go wron
10445 67 20 77 69 74 68 20 61 6e 20 69 6e 73 65 72 74  g with an insert
10446 2c 20 61 73 73 75 6d 69 6e 67 20 70 20 61 6e 64  , assuming p and
10447 20 69 20 61 72 65 20 76 61 6c 69 64 2e 0a 2a 2a   i are valid..**
10448 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  .** The calling 
10449 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 65 6e  function must en
1044a 73 75 72 65 20 74 68 61 74 20 70 20 69 73 20 61  sure that p is a
1044b 20 76 61 6c 69 64 20 42 69 74 76 65 63 20 6f 62   valid Bitvec ob
1044c 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 74 68 61 74  ject.** and that
1044d 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72 20 22   the value for "
1044e 69 22 20 69 73 20 77 69 74 68 69 6e 20 72 61 6e  i" is within ran
1044f 67 65 20 6f 66 20 74 68 65 20 42 69 74 76 65 63  ge of the Bitvec
10450 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 4f 74 68 65   object..** Othe
10451 72 77 69 73 65 20 74 68 65 20 62 65 68 61 76 69  rwise the behavi
10452 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  or is undefined.
10453 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
10454 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69  TE int sqlite3Bi
10455 74 76 65 63 53 65 74 28 42 69 74 76 65 63 20 2a  tvecSet(Bitvec *
10456 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 75 33 32  p, u32 i){.  u32
10457 20 68 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29   h;.  if( p==0 )
10458 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
10459 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 30  K;.  assert( i>0
1045a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3c   );.  assert( i<
1045b 3d 70 2d 3e 69 53 69 7a 65 20 29 3b 0a 20 20 69  =p->iSize );.  i
1045c 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 28 70 2d 3e  --;.  while((p->
1045d 69 53 69 7a 65 20 3e 20 42 49 54 56 45 43 5f 4e  iSize > BITVEC_N
1045e 42 49 54 29 20 26 26 20 70 2d 3e 69 44 69 76 69  BIT) && p->iDivi
1045f 73 6f 72 29 20 7b 0a 20 20 20 20 75 33 32 20 62  sor) {.    u32 b
10460 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73  in = i/p->iDivis
10461 6f 72 3b 0a 20 20 20 20 69 20 3d 20 69 25 70 2d  or;.    i = i%p-
10462 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69  >iDivisor;.    i
10463 66 28 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69  f( p->u.apSub[bi
10464 6e 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  n]==0 ){.      p
10465 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 20 3d  ->u.apSub[bin] =
10466 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72   sqlite3BitvecCr
10467 65 61 74 65 28 20 70 2d 3e 69 44 69 76 69 73 6f  eate( p->iDiviso
10468 72 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  r );.      if( p
10469 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3d 3d  ->u.apSub[bin]==
1046a 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  0 ) return SQLIT
1046b 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  E_NOMEM;.    }. 
1046c 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75     p = p->u.apSu
1046d 62 5b 62 69 6e 5d 3b 0a 20 20 7d 0a 20 20 69 66  b[bin];.  }.  if
1046e 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56  ( p->iSize<=BITV
1046f 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 20 70  EC_NBIT ){.    p
10470 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49  ->u.aBitmap[i/BI
10471 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 7c 3d 20  TVEC_SZELEM] |= 
10472 31 20 3c 3c 20 28 69 26 28 42 49 54 56 45 43 5f  1 << (i&(BITVEC_
10473 53 5a 45 4c 45 4d 2d 31 29 29 3b 0a 20 20 20 20  SZELEM-1));.    
10474 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
10475 3b 0a 20 20 7d 0a 20 20 68 20 3d 20 42 49 54 56  ;.  }.  h = BITV
10476 45 43 5f 48 41 53 48 28 69 2b 2b 29 3b 0a 20 20  EC_HASH(i++);.  
10477 2f 2a 20 69 66 20 74 68 65 72 65 20 77 61 73 6e  /* if there wasn
10478 27 74 20 61 20 68 61 73 68 20 63 6f 6c 6c 69 73  't a hash collis
10479 69 6f 6e 2c 20 61 6e 64 20 74 68 69 73 20 64 6f  ion, and this do
1047a 65 73 6e 27 74 20 2a 2f 0a 20 20 2f 2a 20 63 6f  esn't */.  /* co
1047b 6d 70 6c 65 74 65 6c 79 20 66 69 6c 6c 20 74 68  mpletely fill th
1047c 65 20 68 61 73 68 2c 20 74 68 65 6e 20 6a 75 73  e hash, then jus
1047d 74 20 61 64 64 20 69 74 20 77 69 74 68 6f 75 74  t add it without
1047e 20 2a 2f 0a 20 20 2f 2a 20 77 6f 72 72 69 6e 67   */.  /* worring
1047f 20 61 62 6f 75 74 20 73 75 62 2d 64 69 76 69 64   about sub-divid
10480 69 6e 67 20 61 6e 64 20 72 65 2d 68 61 73 68 69  ing and re-hashi
10481 6e 67 2e 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d  ng. */.  if( !p-
10482 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20  >u.aHash[h] ){. 
10483 20 20 20 69 66 20 28 70 2d 3e 6e 53 65 74 3c 28     if (p->nSet<(
10484 42 49 54 56 45 43 5f 4e 49 4e 54 2d 31 29 29 20  BITVEC_NINT-1)) 
10485 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 69 74  {.      goto bit
10486 76 65 63 5f 73 65 74 5f 65 6e 64 3b 0a 20 20 20  vec_set_end;.   
10487 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
10488 67 6f 74 6f 20 62 69 74 76 65 63 5f 73 65 74 5f  goto bitvec_set_
10489 72 65 68 61 73 68 3b 0a 20 20 20 20 7d 0a 20 20  rehash;.    }.  
1048a 7d 0a 20 20 2f 2a 20 74 68 65 72 65 20 77 61 73  }.  /* there was
1048b 20 61 20 63 6f 6c 6c 69 73 69 6f 6e 2c 20 63 68   a collision, ch
1048c 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 69 74  eck to see if it
1048d 27 73 20 61 6c 72 65 61 64 79 20 2a 2f 0a 20 20  's already */.  
1048e 2f 2a 20 69 6e 20 68 61 73 68 2c 20 69 66 20 6e  /* in hash, if n
1048f 6f 74 2c 20 74 72 79 20 74 6f 20 66 69 6e 64 20  ot, try to find 
10490 61 20 73 70 6f 74 20 66 6f 72 20 69 74 20 2a 2f  a spot for it */
10491 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69 66 28 20  .  do {.    if( 
10492 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 3d 3d 69  p->u.aHash[h]==i
10493 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
10494 5f 4f 4b 3b 0a 20 20 20 20 68 2b 2b 3b 0a 20 20  _OK;.    h++;.  
10495 20 20 69 66 28 20 68 3e 3d 42 49 54 56 45 43 5f    if( h>=BITVEC_
10496 4e 49 4e 54 20 29 20 68 20 3d 20 30 3b 0a 20 20  NINT ) h = 0;.  
10497 7d 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48  } while( p->u.aH
10498 61 73 68 5b 68 5d 20 29 3b 0a 20 20 2f 2a 20 77  ash[h] );.  /* w
10499 65 20 64 69 64 6e 27 74 20 66 69 6e 64 20 69 74  e didn't find it
1049a 20 69 6e 20 74 68 65 20 68 61 73 68 2e 20 20 68   in the hash.  h
1049b 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66   points to the f
1049c 69 72 73 74 20 2a 2f 0a 20 20 2f 2a 20 61 76 61  irst */.  /* ava
1049d 69 6c 61 62 6c 65 20 66 72 65 65 20 73 70 6f 74  ilable free spot
1049e 2e 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69  . check to see i
1049f 66 20 74 68 69 73 20 69 73 20 67 6f 69 6e 67 20  f this is going 
104a0 74 6f 20 2a 2f 0a 20 20 2f 2a 20 6d 61 6b 65 20  to */.  /* make 
104a1 6f 75 72 20 68 61 73 68 20 74 6f 6f 20 22 66 75  our hash too "fu
104a2 6c 6c 22 2e 20 20 2a 2f 0a 62 69 74 76 65 63 5f  ll".  */.bitvec_
104a3 73 65 74 5f 72 65 68 61 73 68 3a 0a 20 20 69 66  set_rehash:.  if
104a4 28 20 70 2d 3e 6e 53 65 74 3e 3d 42 49 54 56 45  ( p->nSet>=BITVE
104a5 43 5f 4d 58 48 41 53 48 20 29 7b 0a 20 20 20 20  C_MXHASH ){.    
104a6 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6a 3b 0a  unsigned int j;.
104a7 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20      int rc;.    
104a8 75 33 32 20 2a 61 69 56 61 6c 75 65 73 20 3d 20  u32 *aiValues = 
104a9 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f  sqlite3StackAllo
104aa 63 52 61 77 28 30 2c 20 73 69 7a 65 6f 66 28 70  cRaw(0, sizeof(p
104ab 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 20 20  ->u.aHash));.   
104ac 20 69 66 28 20 61 69 56 61 6c 75 65 73 3d 3d 30   if( aiValues==0
104ad 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
104ae 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
104af 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
104b0 6d 65 6d 63 70 79 28 61 69 56 61 6c 75 65 73 2c  memcpy(aiValues,
104b1 20 70 2d 3e 75 2e 61 48 61 73 68 2c 20 73 69 7a   p->u.aHash, siz
104b2 65 6f 66 28 70 2d 3e 75 2e 61 48 61 73 68 29 29  eof(p->u.aHash))
104b3 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70  ;.      memset(p
104b4 2d 3e 75 2e 61 70 53 75 62 2c 20 30 2c 20 73 69  ->u.apSub, 0, si
104b5 7a 65 6f 66 28 70 2d 3e 75 2e 61 70 53 75 62 29  zeof(p->u.apSub)
104b6 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 44 69 76  );.      p->iDiv
104b7 69 73 6f 72 20 3d 20 28 70 2d 3e 69 53 69 7a 65  isor = (p->iSize
104b8 20 2b 20 42 49 54 56 45 43 5f 4e 50 54 52 20 2d   + BITVEC_NPTR -
104b9 20 31 29 2f 42 49 54 56 45 43 5f 4e 50 54 52 3b   1)/BITVEC_NPTR;
104ba 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
104bb 74 65 33 42 69 74 76 65 63 53 65 74 28 70 2c 20  te3BitvecSet(p, 
104bc 69 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d  i);.      for(j=
104bd 30 3b 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54  0; j<BITVEC_NINT
104be 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
104bf 69 66 28 20 61 69 56 61 6c 75 65 73 5b 6a 5d 20  if( aiValues[j] 
104c0 29 20 72 63 20 7c 3d 20 73 71 6c 69 74 65 33 42  ) rc |= sqlite3B
104c1 69 74 76 65 63 53 65 74 28 70 2c 20 61 69 56 61  itvecSet(p, aiVa
104c2 6c 75 65 73 5b 6a 5d 29 3b 0a 20 20 20 20 20 20  lues[j]);.      
104c3 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  }.      sqlite3S
104c4 74 61 63 6b 46 72 65 65 28 30 2c 20 61 69 56 61  tackFree(0, aiVa
104c5 6c 75 65 73 29 3b 0a 20 20 20 20 20 20 72 65 74  lues);.      ret
104c6 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
104c7 7d 0a 62 69 74 76 65 63 5f 73 65 74 5f 65 6e 64  }.bitvec_set_end
104c8 3a 0a 20 20 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20  :.  p->nSet++;. 
104c9 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 3d   p->u.aHash[h] =
104ca 20 69 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c   i;.  return SQL
104cb 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
104cc 20 43 6c 65 61 72 20 74 68 65 20 69 2d 74 68 20   Clear the i-th 
104cd 62 69 74 2e 0a 2a 2a 0a 2a 2a 20 70 42 75 66 20  bit..**.** pBuf 
104ce 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e 74 65  must be a pointe
104cf 72 20 74 6f 20 61 74 20 6c 65 61 73 74 20 42 49  r to at least BI
104d0 54 56 45 43 5f 53 5a 20 62 79 74 65 73 20 6f 66  TVEC_SZ bytes of
104d1 20 74 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61   temporary stora
104d2 67 65 0a 2a 2a 20 74 68 61 74 20 42 69 74 76 65  ge.** that Bitve
104d3 63 43 6c 65 61 72 20 63 61 6e 20 75 73 65 20 74  cClear can use t
104d4 6f 20 72 65 62 75 69 6c 74 20 69 74 73 20 68 61  o rebuilt its ha
104d5 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  sh table..*/.SQL
104d6 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
104d7 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c   sqlite3BitvecCl
104d8 65 61 72 28 42 69 74 76 65 63 20 2a 70 2c 20 75  ear(Bitvec *p, u
104d9 33 32 20 69 2c 20 76 6f 69 64 20 2a 70 42 75 66  32 i, void *pBuf
104da 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  ){.  if( p==0 ) 
104db 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74  return;.  assert
104dc 28 20 69 3e 30 20 29 3b 0a 20 20 69 2d 2d 3b 0a  ( i>0 );.  i--;.
104dd 20 20 77 68 69 6c 65 28 20 70 2d 3e 69 44 69 76    while( p->iDiv
104de 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 33 32 20  isor ){.    u32 
104df 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 76 69  bin = i/p->iDivi
104e0 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 69 25 70  sor;.    i = i%p
104e1 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20  ->iDivisor;.    
104e2 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62  p = p->u.apSub[b
104e3 69 6e 5d 3b 0a 20 20 20 20 69 66 20 28 21 70 29  in];.    if (!p)
104e4 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b   {.      return;
104e5 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
104e6 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45   p->iSize<=BITVE
104e7 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 20 70 2d  C_NBIT ){.    p-
104e8 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54  >u.aBitmap[i/BIT
104e9 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 26 3d 20 7e  VEC_SZELEM] &= ~
104ea 28 31 20 3c 3c 20 28 69 26 28 42 49 54 56 45 43  (1 << (i&(BITVEC
104eb 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 3b 0a 20 20  _SZELEM-1)));.  
104ec 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 73 69 67  }else{.    unsig
104ed 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20 20 20 75  ned int j;.    u
104ee 33 32 20 2a 61 69 56 61 6c 75 65 73 20 3d 20 70  32 *aiValues = p
104ef 42 75 66 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  Buf;.    memcpy(
104f0 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e 75 2e 61  aiValues, p->u.a
104f1 48 61 73 68 2c 20 73 69 7a 65 6f 66 28 70 2d 3e  Hash, sizeof(p->
104f2 75 2e 61 48 61 73 68 29 29 3b 0a 20 20 20 20 6d  u.aHash));.    m
104f3 65 6d 73 65 74 28 70 2d 3e 75 2e 61 48 61 73 68  emset(p->u.aHash
104f4 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75  , 0, sizeof(p->u
104f5 2e 61 48 61 73 68 29 29 3b 0a 20 20 20 20 70 2d  .aHash));.    p-
104f6 3e 6e 53 65 74 20 3d 20 30 3b 0a 20 20 20 20 66  >nSet = 0;.    f
104f7 6f 72 28 6a 3d 30 3b 20 6a 3c 42 49 54 56 45 43  or(j=0; j<BITVEC
104f8 5f 4e 49 4e 54 3b 20 6a 2b 2b 29 7b 0a 20 20 20  _NINT; j++){.   
104f9 20 20 20 69 66 28 20 61 69 56 61 6c 75 65 73 5b     if( aiValues[
104fa 6a 5d 20 26 26 20 61 69 56 61 6c 75 65 73 5b 6a  j] && aiValues[j
104fb 5d 21 3d 28 69 2b 31 29 20 29 7b 0a 20 20 20 20  ]!=(i+1) ){.    
104fc 20 20 20 20 75 33 32 20 68 20 3d 20 42 49 54 56      u32 h = BITV
104fd 45 43 5f 48 41 53 48 28 61 69 56 61 6c 75 65 73  EC_HASH(aiValues
104fe 5b 6a 5d 2d 31 29 3b 0a 20 20 20 20 20 20 20 20  [j]-1);.        
104ff 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 20 20 20  p->nSet++;.     
10500 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61     while( p->u.a
10501 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 20  Hash[h] ){.     
10502 20 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 20 20       h++;.      
10503 20 20 20 20 69 66 28 20 68 3e 3d 42 49 54 56 45      if( h>=BITVE
10504 43 5f 4e 49 4e 54 20 29 20 68 20 3d 20 30 3b 0a  C_NINT ) h = 0;.
10505 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
10506 20 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20    p->u.aHash[h] 
10507 3d 20 61 69 56 61 6c 75 65 73 5b 6a 5d 3b 0a 20  = aiValues[j];. 
10508 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
10509 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f  .}../*.** Destro
1050a 79 20 61 20 62 69 74 6d 61 70 20 6f 62 6a 65 63  y a bitmap objec
1050b 74 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20  t.  Reclaim all 
1050c 6d 65 6d 6f 72 79 20 75 73 65 64 2e 0a 2a 2f 0a  memory used..*/.
1050d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1050e 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65  oid sqlite3Bitve
1050f 63 44 65 73 74 72 6f 79 28 42 69 74 76 65 63 20  cDestroy(Bitvec 
10510 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20  *p){.  if( p==0 
10511 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
10512 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a 20  p->iDivisor ){. 
10513 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20     unsigned int 
10514 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  i;.    for(i=0; 
10515 69 3c 42 49 54 56 45 43 5f 4e 50 54 52 3b 20 69  i<BITVEC_NPTR; i
10516 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
10517 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28  e3BitvecDestroy(
10518 70 2d 3e 75 2e 61 70 53 75 62 5b 69 5d 29 3b 0a  p->u.apSub[i]);.
10519 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
1051a 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a  te3_free(p);.}..
1051b 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
1051c 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 53   value of the iS
1051d 69 7a 65 20 70 61 72 61 6d 65 74 65 72 20 73 70  ize parameter sp
1051e 65 63 69 66 69 65 64 20 77 68 65 6e 20 42 69 74  ecified when Bit
1051f 76 65 63 20 2a 70 0a 2a 2a 20 77 61 73 20 63 72  vec *p.** was cr
10520 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  eated..*/.SQLITE
10521 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c  _PRIVATE u32 sql
10522 69 74 65 33 42 69 74 76 65 63 53 69 7a 65 28 42  ite3BitvecSize(B
10523 69 74 76 65 63 20 2a 70 29 7b 0a 20 20 72 65 74  itvec *p){.  ret
10524 75 72 6e 20 70 2d 3e 69 53 69 7a 65 3b 0a 7d 0a  urn p->iSize;.}.
10525 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
10526 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53  OMIT_BUILTIN_TES
10527 54 0a 2f 2a 0a 2a 2a 20 4c 65 74 20 56 5b 5d 20  T./*.** Let V[] 
10528 62 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20 75  be an array of u
10529 6e 73 69 67 6e 65 64 20 63 68 61 72 61 63 74 65  nsigned characte
1052a 72 73 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f  rs sufficient to
1052b 20 68 6f 6c 64 0a 2a 2a 20 75 70 20 74 6f 20 4e   hold.** up to N
1052c 20 62 69 74 73 2e 20 20 4c 65 74 20 49 20 62 65   bits.  Let I be
1052d 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 74 77   an integer betw
1052e 65 65 6e 20 30 20 61 6e 64 20 4e 2e 20 20 30 3c  een 0 and N.  0<
1052f 3d 49 3c 4e 2e 0a 2a 2a 20 54 68 65 6e 20 74 68  =I<N..** Then th
10530 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72  e following macr
10531 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  os can be used t
10532 6f 20 73 65 74 2c 20 63 6c 65 61 72 2c 20 6f 72  o set, clear, or
10533 20 74 65 73 74 0a 2a 2a 20 69 6e 64 69 76 69 64   test.** individ
10534 75 61 6c 20 62 69 74 73 20 77 69 74 68 69 6e 20  ual bits within 
10535 56 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 45  V..*/.#define SE
10536 54 42 49 54 28 56 2c 49 29 20 20 20 20 20 20 56  TBIT(V,I)      V
10537 5b 49 3e 3e 33 5d 20 7c 3d 20 28 31 3c 3c 28 49  [I>>3] |= (1<<(I
10538 26 37 29 29 0a 23 64 65 66 69 6e 65 20 43 4c 45  &7)).#define CLE
10539 41 52 42 49 54 28 56 2c 49 29 20 20 20 20 56 5b  ARBIT(V,I)    V[
1053a 49 3e 3e 33 5d 20 26 3d 20 7e 28 31 3c 3c 28 49  I>>3] &= ~(1<<(I
1053b 26 37 29 29 0a 23 64 65 66 69 6e 65 20 54 45 53  &7)).#define TES
1053c 54 42 49 54 28 56 2c 49 29 20 20 20 20 20 28 56  TBIT(V,I)     (V
1053d 5b 49 3e 3e 33 5d 26 28 31 3c 3c 28 49 26 37 29  [I>>3]&(1<<(I&7)
1053e 29 29 21 3d 30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ))!=0../*.** Thi
1053f 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 20 61  s routine runs a
10540 6e 20 65 78 74 65 6e 73 69 76 65 20 74 65 73 74  n extensive test
10541 20 6f 66 20 74 68 65 20 42 69 74 76 65 63 20 63   of the Bitvec c
10542 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ode..**.** The i
10543 6e 70 75 74 20 69 73 20 61 6e 20 61 72 72 61 79  nput is an array
10544 20 6f 66 20 69 6e 74 65 67 65 72 73 20 74 68 61   of integers tha
10545 74 20 61 63 74 73 20 61 73 20 61 20 70 72 6f 67  t acts as a prog
10546 72 61 6d 0a 2a 2a 20 74 6f 20 74 65 73 74 20 74  ram.** to test t
10547 68 65 20 42 69 74 76 65 63 2e 20 20 54 68 65 20  he Bitvec.  The 
10548 69 6e 74 65 67 65 72 73 20 61 72 65 20 6f 70 63  integers are opc
10549 6f 64 65 73 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a  odes followed.**
1054a 20 62 79 20 30 2c 20 31 2c 20 6f 72 20 33 20 6f   by 0, 1, or 3 o
1054b 70 65 72 61 6e 64 73 2c 20 64 65 70 65 6e 64 69  perands, dependi
1054c 6e 67 20 6f 6e 20 74 68 65 20 6f 70 63 6f 64 65  ng on the opcode
1054d 2e 20 20 41 6e 6f 74 68 65 72 0a 2a 2a 20 6f 70  .  Another.** op
1054e 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 69 6d 6d  code follows imm
1054f 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 74  ediately after t
10550 68 65 20 6c 61 73 74 20 6f 70 65 72 61 6e 64 2e  he last operand.
10551 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  .**.** There are
10552 20 36 20 6f 70 63 6f 64 65 73 20 6e 75 6d 62 65   6 opcodes numbe
10553 72 65 64 20 66 72 6f 6d 20 30 20 74 68 72 6f 75  red from 0 throu
10554 67 68 20 35 2e 20 20 30 20 69 73 20 74 68 65 0a  gh 5.  0 is the.
10555 2a 2a 20 22 68 61 6c 74 22 20 6f 70 63 6f 64 65  ** "halt" opcode
10556 20 61 6e 64 20 63 61 75 73 65 73 20 74 68 65 20   and causes the 
10557 74 65 73 74 20 74 6f 20 65 6e 64 2e 0a 2a 2a 0a  test to end..**.
10558 2a 2a 20 20 20 20 30 20 20 20 20 20 20 20 20 20  **    0         
10559 20 48 61 6c 74 20 61 6e 64 20 72 65 74 75 72 6e   Halt and return
1055a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
1055b 72 72 6f 72 73 0a 2a 2a 20 20 20 20 31 20 4e 20  rrors.**    1 N 
1055c 53 20 58 20 20 20 20 53 65 74 20 4e 20 62 69 74  S X    Set N bit
1055d 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68  s beginning with
1055e 20 53 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74   S and increment
1055f 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20 20 20 32  ing by X.**    2
10560 20 4e 20 53 20 58 20 20 20 20 43 6c 65 61 72 20   N S X    Clear 
10561 4e 20 62 69 74 73 20 62 65 67 69 6e 6e 69 6e 67  N bits beginning
10562 20 77 69 74 68 20 53 20 61 6e 64 20 69 6e 63 72   with S and incr
10563 65 6d 65 6e 74 69 6e 67 20 62 79 20 58 0a 2a 2a  ementing by X.**
10564 20 20 20 20 33 20 4e 20 20 20 20 20 20 20 20 53      3 N        S
10565 65 74 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 63 68  et N randomly ch
10566 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20 20 20 20  osen bits.**    
10567 34 20 4e 20 20 20 20 20 20 20 20 43 6c 65 61 72  4 N        Clear
10568 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 63 68 6f 73   N randomly chos
10569 65 6e 20 62 69 74 73 0a 2a 2a 20 20 20 20 35 20  en bits.**    5 
1056a 4e 20 53 20 58 20 20 20 20 53 65 74 20 4e 20 62  N S X    Set N b
1056b 69 74 73 20 66 72 6f 6d 20 53 20 69 6e 63 72 65  its from S incre
1056c 6d 65 6e 74 20 58 20 69 6e 20 61 72 72 61 79 20  ment X in array 
1056d 6f 6e 6c 79 2c 20 6e 6f 74 20 69 6e 20 62 69 74  only, not in bit
1056e 76 65 63 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70  vec.**.** The op
1056f 63 6f 64 65 73 20 31 20 74 68 72 6f 75 67 68 20  codes 1 through 
10570 34 20 70 65 72 66 6f 72 6d 20 73 65 74 20 61 6e  4 perform set an
10571 64 20 63 6c 65 61 72 20 6f 70 65 72 61 74 69 6f  d clear operatio
10572 6e 73 20 61 72 65 20 70 65 72 66 6f 72 6d 65 64  ns are performed
10573 0a 2a 2a 20 6f 6e 20 62 6f 74 68 20 61 20 42 69  .** on both a Bi
10574 74 76 65 63 20 6f 62 6a 65 63 74 20 61 6e 64 20  tvec object and 
10575 6f 6e 20 61 20 6c 69 6e 65 61 72 20 61 72 72 61  on a linear arra
10576 79 20 6f 66 20 62 69 74 73 20 6f 62 74 61 69 6e  y of bits obtain
10577 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a  ed from malloc..
10578 2a 2a 20 4f 70 63 6f 64 65 20 35 20 77 6f 72 6b  ** Opcode 5 work
10579 73 20 6f 6e 20 74 68 65 20 6c 69 6e 65 61 72 20  s on the linear 
1057a 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20  array only, not 
1057b 6f 6e 20 74 68 65 20 42 69 74 76 65 63 2e 0a 2a  on the Bitvec..*
1057c 2a 20 4f 70 63 6f 64 65 20 35 20 69 73 20 75 73  * Opcode 5 is us
1057d 65 64 20 74 6f 20 64 65 6c 69 62 65 72 61 74 65  ed to deliberate
1057e 6c 79 20 69 6e 64 75 63 65 20 61 20 66 61 75 6c  ly induce a faul
1057f 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a  t in order to.**
10580 20 63 6f 6e 66 69 72 6d 20 74 68 61 74 20 65 72   confirm that er
10581 72 6f 72 20 64 65 74 65 63 74 69 6f 6e 20 77 6f  ror detection wo
10582 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68  rks..**.** At th
10583 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20  e conclusion of 
10584 74 68 65 20 74 65 73 74 20 74 68 65 20 6c 69 6e  the test the lin
10585 65 61 72 20 61 72 72 61 79 20 69 73 20 63 6f 6d  ear array is com
10586 70 61 72 65 64 0a 2a 2a 20 61 67 61 69 6e 73 74  pared.** against
10587 20 74 68 65 20 42 69 74 76 65 63 20 6f 62 6a 65   the Bitvec obje
10588 63 74 2e 20 20 49 66 20 74 68 65 72 65 20 61 72  ct.  If there ar
10589 65 20 61 6e 79 20 64 69 66 66 65 72 65 6e 63 65  e any difference
1058a 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69  s,.** an error i
1058b 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20  s returned.  If 
1058c 74 68 65 79 20 61 72 65 20 74 68 65 20 73 61 6d  they are the sam
1058d 65 2c 20 7a 65 72 6f 20 69 73 20 72 65 74 75 72  e, zero is retur
1058e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  ned..**.** If a 
1058f 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
10590 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
10591 72 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f 0a 53 51  return -1..*/.SQ
10592 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
10593 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 42 75   sqlite3BitvecBu
10594 69 6c 74 69 6e 54 65 73 74 28 69 6e 74 20 73 7a  iltinTest(int sz
10595 2c 20 69 6e 74 20 2a 61 4f 70 29 7b 0a 20 20 42  , int *aOp){.  B
10596 69 74 76 65 63 20 2a 70 42 69 74 76 65 63 20 3d  itvec *pBitvec =
10597 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   0;.  unsigned c
10598 68 61 72 20 2a 70 56 20 3d 20 30 3b 0a 20 20 69  har *pV = 0;.  i
10599 6e 74 20 72 63 20 3d 20 2d 31 3b 0a 20 20 69 6e  nt rc = -1;.  in
1059a 74 20 69 2c 20 6e 78 2c 20 70 63 2c 20 6f 70 3b  t i, nx, pc, op;
1059b 0a 20 20 76 6f 69 64 20 2a 70 54 6d 70 53 70 61  .  void *pTmpSpa
1059c 63 65 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61  ce;..  /* Alloca
1059d 74 65 20 74 68 65 20 42 69 74 76 65 63 20 74 6f  te the Bitvec to
1059e 20 62 65 20 74 65 73 74 65 64 20 61 6e 64 20 61   be tested and a
1059f 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 6f 66   linear array of
105a0 0a 20 20 2a 2a 20 62 69 74 73 20 74 6f 20 61 63  .  ** bits to ac
105a1 74 20 61 73 20 74 68 65 20 72 65 66 65 72 65 6e  t as the referen
105a2 63 65 20 2a 2f 0a 20 20 70 42 69 74 76 65 63 20  ce */.  pBitvec 
105a3 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43  = sqlite3BitvecC
105a4 72 65 61 74 65 28 20 73 7a 20 29 3b 0a 20 20 70  reate( sz );.  p
105a5 56 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  V = sqlite3_mall
105a6 6f 63 28 20 28 73 7a 2b 37 29 2f 38 20 2b 20 31  oc( (sz+7)/8 + 1
105a7 20 29 3b 0a 20 20 70 54 6d 70 53 70 61 63 65 20   );.  pTmpSpace 
105a8 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
105a9 28 42 49 54 56 45 43 5f 53 5a 29 3b 0a 20 20 69  (BITVEC_SZ);.  i
105aa 66 28 20 70 42 69 74 76 65 63 3d 3d 30 20 7c 7c  f( pBitvec==0 ||
105ab 20 70 56 3d 3d 30 20 7c 7c 20 70 54 6d 70 53 70   pV==0 || pTmpSp
105ac 61 63 65 3d 3d 30 20 20 29 20 67 6f 74 6f 20 62  ace==0  ) goto b
105ad 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20 6d 65 6d  itvec_end;.  mem
105ae 73 65 74 28 70 56 2c 20 30 2c 20 28 73 7a 2b 37  set(pV, 0, (sz+7
105af 29 2f 38 20 2b 20 31 29 3b 0a 0a 20 20 2f 2a 20  )/8 + 1);..  /* 
105b0 4e 55 4c 4c 20 70 42 69 74 76 65 63 20 74 65 73  NULL pBitvec tes
105b1 74 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42  ts */.  sqlite3B
105b2 69 74 76 65 63 53 65 74 28 30 2c 20 31 29 3b 0a  itvecSet(0, 1);.
105b3 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43    sqlite3BitvecC
105b4 6c 65 61 72 28 30 2c 20 31 2c 20 70 54 6d 70 53  lear(0, 1, pTmpS
105b5 70 61 63 65 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e  pace);..  /* Run
105b6 20 74 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a   the program */.
105b7 20 20 70 63 20 3d 20 30 3b 0a 20 20 77 68 69 6c    pc = 0;.  whil
105b8 65 28 20 28 6f 70 20 3d 20 61 4f 70 5b 70 63 5d  e( (op = aOp[pc]
105b9 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 77 69 74  )!=0 ){.    swit
105ba 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20  ch( op ){.      
105bb 63 61 73 65 20 31 3a 0a 20 20 20 20 20 20 63 61  case 1:.      ca
105bc 73 65 20 32 3a 0a 20 20 20 20 20 20 63 61 73 65  se 2:.      case
105bd 20 35 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78   5: {.        nx
105be 20 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 69 20   = 4;.        i 
105bf 3d 20 61 4f 70 5b 70 63 2b 32 5d 20 2d 20 31 3b  = aOp[pc+2] - 1;
105c0 0a 20 20 20 20 20 20 20 20 61 4f 70 5b 70 63 2b  .        aOp[pc+
105c1 32 5d 20 2b 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b  2] += aOp[pc+3];
105c2 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
105c3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
105c4 73 65 20 33 3a 0a 20 20 20 20 20 20 63 61 73 65  se 3:.      case
105c5 20 34 3a 20 0a 20 20 20 20 20 20 64 65 66 61 75   4: .      defau
105c6 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78  lt: {.        nx
105c7 20 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 73 71   = 2;.        sq
105c8 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73  lite3_randomness
105c9 28 73 69 7a 65 6f 66 28 69 29 2c 20 26 69 29 3b  (sizeof(i), &i);
105ca 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
105cb 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
105cc 20 20 69 66 28 20 28 2d 2d 61 4f 70 5b 70 63 2b    if( (--aOp[pc+
105cd 31 5d 29 20 3e 20 30 20 29 20 6e 78 20 3d 20 30  1]) > 0 ) nx = 0
105ce 3b 0a 20 20 20 20 70 63 20 2b 3d 20 6e 78 3b 0a  ;.    pc += nx;.
105cf 20 20 20 20 69 20 3d 20 28 69 20 26 20 30 78 37      i = (i & 0x7
105d0 66 66 66 66 66 66 66 29 25 73 7a 3b 0a 20 20 20  fffffff)%sz;.   
105d1 20 69 66 28 20 28 6f 70 20 26 20 31 29 21 3d 30   if( (op & 1)!=0
105d2 20 29 7b 0a 20 20 20 20 20 20 53 45 54 42 49 54   ){.      SETBIT
105d3 28 70 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20  (pV, (i+1));.   
105d4 20 20 20 69 66 28 20 6f 70 21 3d 35 20 29 7b 0a     if( op!=5 ){.
105d5 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
105d6 74 65 33 42 69 74 76 65 63 53 65 74 28 70 42 69  te3BitvecSet(pBi
105d7 74 76 65 63 2c 20 69 2b 31 29 20 29 20 67 6f 74  tvec, i+1) ) got
105d8 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20  o bitvec_end;.  
105d9 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
105da 0a 20 20 20 20 20 20 43 4c 45 41 52 42 49 54 28  .      CLEARBIT(
105db 70 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20  pV, (i+1));.    
105dc 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43    sqlite3BitvecC
105dd 6c 65 61 72 28 70 42 69 74 76 65 63 2c 20 69 2b  lear(pBitvec, i+
105de 31 2c 20 70 54 6d 70 53 70 61 63 65 29 3b 0a 20  1, pTmpSpace);. 
105df 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54     }.  }..  /* T
105e0 65 73 74 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  est to make sure
105e1 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72 72 61   the linear arra
105e2 79 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65  y exactly matche
105e3 73 20 74 68 65 0a 20 20 2a 2a 20 42 69 74 76 65  s the.  ** Bitve
105e4 63 20 6f 62 6a 65 63 74 2e 20 20 53 74 61 72 74  c object.  Start
105e5 20 77 69 74 68 20 74 68 65 20 61 73 73 75 6d 70   with the assump
105e6 74 69 6f 6e 20 74 68 61 74 20 74 68 65 79 20 64  tion that they d
105e7 6f 0a 20 20 2a 2a 20 6d 61 74 63 68 20 28 72 63  o.  ** match (rc
105e8 3d 3d 30 29 2e 20 20 43 68 61 6e 67 65 20 72 63  ==0).  Change rc
105e9 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20   to non-zero if 
105ea 61 20 64 69 73 63 72 65 70 61 6e 63 79 0a 20 20  a discrepancy.  
105eb 2a 2a 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20 2a  ** is found..  *
105ec 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  /.  rc = sqlite3
105ed 42 69 74 76 65 63 54 65 73 74 28 30 2c 30 29 20  BitvecTest(0,0) 
105ee 2b 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54  + sqlite3BitvecT
105ef 65 73 74 28 70 42 69 74 76 65 63 2c 20 73 7a 2b  est(pBitvec, sz+
105f0 31 29 0a 20 20 20 20 20 20 20 20 20 20 2b 20 73  1).          + s
105f1 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74  qlite3BitvecTest
105f2 28 70 42 69 74 76 65 63 2c 20 30 29 0a 20 20 20  (pBitvec, 0).   
105f3 20 20 20 20 20 20 20 2b 20 28 73 71 6c 69 74 65         + (sqlite
105f4 33 42 69 74 76 65 63 53 69 7a 65 28 70 42 69 74  3BitvecSize(pBit
105f5 76 65 63 29 20 2d 20 73 7a 29 3b 0a 20 20 66 6f  vec) - sz);.  fo
105f6 72 28 69 3d 31 3b 20 69 3c 3d 73 7a 3b 20 69 2b  r(i=1; i<=sz; i+
105f7 2b 29 7b 0a 20 20 20 20 69 66 28 20 20 28 54 45  +){.    if(  (TE
105f8 53 54 42 49 54 28 70 56 2c 69 29 29 21 3d 73 71  STBIT(pV,i))!=sq
105f9 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28  lite3BitvecTest(
105fa 70 42 69 74 76 65 63 2c 69 29 20 29 7b 0a 20 20  pBitvec,i) ){.  
105fb 20 20 20 20 72 63 20 3d 20 69 3b 0a 20 20 20 20      rc = i;.    
105fc 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
105fd 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6c   }..  /* Free al
105fe 6c 6f 63 61 74 65 64 20 73 74 72 75 63 74 75 72  located structur
105ff 65 20 2a 2f 0a 62 69 74 76 65 63 5f 65 6e 64 3a  e */.bitvec_end:
10600 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
10601 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 73 71  pTmpSpace);.  sq
10602 6c 69 74 65 33 5f 66 72 65 65 28 70 56 29 3b 0a  lite3_free(pV);.
10603 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44    sqlite3BitvecD
10604 65 73 74 72 6f 79 28 70 42 69 74 76 65 63 29 3b  estroy(pBitvec);
10605 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
10606 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
10607 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45  _OMIT_BUILTIN_TE
10608 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ST */../********
10609 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 69  ****** End of bi
1060a 74 76 65 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  tvec.c *********
1060b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1060c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1060d 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
1060e 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
1060f 65 20 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a  e pcache.c *****
10610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10611 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10612 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
10613 38 20 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a 2a  8 August 05.**.*
10614 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
10615 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
10616 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
10617 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
10618 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
10619 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
1061a 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
1061b 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
1061c 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
1061d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
1061e 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
1061f 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
10620 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
10621 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
10622 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
10623 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
10624 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
10625 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
10626 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10627 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10628 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10629 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
1062a 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e  is file implemen
1062b 74 73 20 74 68 61 74 20 70 61 67 65 20 63 61 63  ts that page cac
1062c 68 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24  he..**.** @(#) $
1062d 49 64 3a 20 70 63 61 63 68 65 2e 63 2c 76 20 31  Id: pcache.c,v 1
1062e 2e 34 37 20 32 30 30 39 2f 30 37 2f 32 35 20 31  .47 2009/07/25 1
1062f 31 3a 34 36 3a 34 39 20 64 61 6e 69 65 6c 6b 31  1:46:49 danielk1
10630 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  977 Exp $.*/../*
10631 0a 2a 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20 70  .** A complete p
10632 61 67 65 20 63 61 63 68 65 20 69 73 20 61 6e 20  age cache is an 
10633 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
10634 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
10635 74 72 75 63 74 20 50 43 61 63 68 65 20 7b 0a 20  truct PCache {. 
10636 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 2c 20   PgHdr *pDirty, 
10637 2a 70 44 69 72 74 79 54 61 69 6c 3b 20 20 20 20  *pDirtyTail;    
10638 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
10639 64 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 4c  dirty pages in L
1063a 52 55 20 6f 72 64 65 72 20 2a 2f 0a 20 20 50 67  RU order */.  Pg
1063b 48 64 72 20 2a 70 53 79 6e 63 65 64 3b 20 20 20  Hdr *pSynced;   
1063c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1063d 20 20 2f 2a 20 4c 61 73 74 20 73 79 6e 63 65 64    /* Last synced
1063e 20 70 61 67 65 20 69 6e 20 64 69 72 74 79 20 70   page in dirty p
1063f 61 67 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e  age list */.  in
10640 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20  t nRef;         
10641 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10642 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
10643 65 66 65 72 65 6e 63 65 64 20 70 61 67 65 73 20  eferenced pages 
10644 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 78 3b 20 20  */.  int nMax;  
10645 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10646 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66           /* Conf
10647 69 67 75 72 65 64 20 63 61 63 68 65 20 73 69 7a  igured cache siz
10648 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67  e */.  int szPag
10649 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1064a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
1064b 7a 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65  ze of every page
1064c 20 69 6e 20 74 68 69 73 20 63 61 63 68 65 20 2a   in this cache *
1064d 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72 61 3b  /.  int szExtra;
1064e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1064f 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
10650 6f 66 20 65 78 74 72 61 20 73 70 61 63 65 20 66  of extra space f
10651 6f 72 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a  or each page */.
10652 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65    int bPurgeable
10653 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
10654 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
10655 20 70 61 67 65 73 20 61 72 65 20 6f 6e 20 62 61   pages are on ba
10656 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20  cking store */. 
10657 20 69 6e 74 20 28 2a 78 53 74 72 65 73 73 29 28   int (*xStress)(
10658 76 6f 69 64 2a 2c 50 67 48 64 72 2a 29 3b 20 20  void*,PgHdr*);  
10659 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 6f 20       /* Call to 
1065a 74 72 79 20 6d 61 6b 65 20 61 20 70 61 67 65 20  try make a page 
1065b 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20  clean */.  void 
1065c 2a 70 53 74 72 65 73 73 3b 20 20 20 20 20 20 20  *pStress;       
1065d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1065e 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53  * Argument to xS
1065f 74 72 65 73 73 20 2a 2f 0a 20 20 73 71 6c 69 74  tress */.  sqlit
10660 65 33 5f 70 63 61 63 68 65 20 2a 70 43 61 63 68  e3_pcache *pCach
10661 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e;             /
10662 2a 20 50 6c 75 67 67 61 62 6c 65 20 63 61 63 68  * Pluggable cach
10663 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 50 67  e module */.  Pg
10664 48 64 72 20 2a 70 50 61 67 65 31 3b 20 20 20 20  Hdr *pPage1;    
10665 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10666 20 20 2f 2a 20 52 65 66 65 72 65 6e 63 65 20 74    /* Reference t
10667 6f 20 70 61 67 65 20 31 20 2a 2f 0a 7d 3b 0a 0a  o page 1 */.};..
10668 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68  /*.** Some of th
10669 65 20 61 73 73 65 72 74 28 29 20 6d 61 63 72 6f  e assert() macro
1066a 73 20 69 6e 20 74 68 69 73 20 63 6f 64 65 20 61  s in this code a
1066b 72 65 20 74 6f 6f 20 65 78 70 65 6e 73 69 76 65  re too expensive
1066c 20 74 6f 20 72 75 6e 0a 2a 2a 20 65 76 65 6e 20   to run.** even 
1066d 64 75 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 64 65  during normal de
1066e 62 75 67 67 69 6e 67 2e 20 20 55 73 65 20 74 68  bugging.  Use th
1066f 65 6d 20 6f 6e 6c 79 20 72 61 72 65 6c 79 20 6f  em only rarely o
10670 6e 20 6c 6f 6e 67 2d 72 75 6e 6e 69 6e 67 0a 2a  n long-running.*
10671 2a 20 74 65 73 74 73 2e 20 20 45 6e 61 62 6c 65  * tests.  Enable
10672 20 74 68 65 20 65 78 70 65 6e 73 69 76 65 20 61   the expensive a
10673 73 73 65 72 74 73 20 75 73 69 6e 67 20 74 68 65  sserts using the
10674 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 5f 45 4e 41  .** -DSQLITE_ENA
10675 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53  BLE_EXPENSIVE_AS
10676 53 45 52 54 3d 31 20 63 6f 6d 70 69 6c 65 2d 74  SERT=1 compile-t
10677 69 6d 65 20 6f 70 74 69 6f 6e 2e 0a 2a 2f 0a 23  ime option..*/.#
10678 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
10679 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53  BLE_EXPENSIVE_AS
1067a 53 45 52 54 0a 23 20 64 65 66 69 6e 65 20 65 78  SERT.# define ex
1067b 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28 58  pensive_assert(X
1067c 29 20 20 61 73 73 65 72 74 28 58 29 0a 23 65 6c  )  assert(X).#el
1067d 73 65 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65  se.# define expe
1067e 6e 73 69 76 65 5f 61 73 73 65 72 74 28 58 29 0a  nsive_assert(X).
1067f 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
10680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10681 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4c 69 6e 6b  *********** Link
10682 65 64 20 4c 69 73 74 20 4d 61 6e 61 67 65 6d 65  ed List Manageme
10683 6e 74 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt *************
10684 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 20 21 64  *******/..#if !d
10685 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26  efined(NDEBUG) &
10686 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  & defined(SQLITE
10687 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56  _ENABLE_EXPENSIV
10688 45 5f 41 53 53 45 52 54 29 0a 2f 2a 0a 2a 2a 20  E_ASSERT)./*.** 
10689 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70  Check that the p
1068a 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 76  Cache->pSynced v
1068b 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 63  ariable is set c
1068c 6f 72 72 65 63 74 6c 79 2e 20 49 66 20 69 74 0a  orrectly. If it.
1068d 2a 2a 20 69 73 20 6e 6f 74 2c 20 65 69 74 68 65  ** is not, eithe
1068e 72 20 66 61 69 6c 20 61 6e 20 61 73 73 65 72 74  r fail an assert
1068f 20 6f 72 20 72 65 74 75 72 6e 20 7a 65 72 6f 2e   or return zero.
10690 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75   Otherwise, retu
10691 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 20  rn.** non-zero. 
10692 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65  This is only use
10693 64 20 69 6e 20 64 65 62 75 67 67 69 6e 67 20 62  d in debugging b
10694 75 69 6c 64 73 2c 20 61 73 20 66 6f 6c 6c 6f 77  uilds, as follow
10695 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 65 78 70 65 6e  s:.**.**   expen
10696 73 69 76 65 5f 61 73 73 65 72 74 28 20 70 63 61  sive_assert( pca
10697 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70  cheCheckSynced(p
10698 43 61 63 68 65 29 20 29 3b 0a 2a 2f 0a 73 74 61  Cache) );.*/.sta
10699 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 43 68  tic int pcacheCh
1069a 65 63 6b 53 79 6e 63 65 64 28 50 43 61 63 68 65  eckSynced(PCache
1069b 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48   *pCache){.  PgH
1069c 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70  dr *p;.  for(p=p
1069d 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69  Cache->pDirtyTai
1069e 6c 3b 20 70 21 3d 70 43 61 63 68 65 2d 3e 70 53  l; p!=pCache->pS
1069f 79 6e 63 65 64 3b 20 70 3d 70 2d 3e 70 44 69 72  ynced; p=p->pDir
106a0 74 79 50 72 65 76 29 7b 0a 20 20 20 20 61 73 73  tyPrev){.    ass
106a1 65 72 74 28 20 70 2d 3e 6e 52 65 66 20 7c 7c 20  ert( p->nRef || 
106a2 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  (p->flags&PGHDR_
106a3 4e 45 45 44 5f 53 59 4e 43 29 20 29 3b 0a 20 20  NEED_SYNC) );.  
106a4 7d 0a 20 20 72 65 74 75 72 6e 20 28 70 3d 3d 30  }.  return (p==0
106a5 20 7c 7c 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28   || p->nRef || (
106a6 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  p->flags&PGHDR_N
106a7 45 45 44 5f 53 59 4e 43 29 3d 3d 30 29 3b 0a 7d  EED_SYNC)==0);.}
106a8 0a 23 65 6e 64 69 66 20 2f 2a 20 21 4e 44 45 42  .#endif /* !NDEB
106a9 55 47 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41  UG && SQLITE_ENA
106aa 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53  BLE_EXPENSIVE_AS
106ab 53 45 52 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52  SERT */../*.** R
106ac 65 6d 6f 76 65 20 70 61 67 65 20 70 50 61 67 65  emove page pPage
106ad 20 66 72 6f 6d 20 74 68 65 20 6c 69 73 74 20 6f   from the list o
106ae 66 20 64 69 72 74 79 20 70 61 67 65 73 2e 0a 2a  f dirty pages..*
106af 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63  /.static void pc
106b0 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69  acheRemoveFromDi
106b1 72 74 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70  rtyList(PgHdr *p
106b2 50 61 67 65 29 7b 0a 20 20 50 43 61 63 68 65 20  Page){.  PCache 
106b3 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63  *p = pPage->pCac
106b4 68 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  he;..  assert( p
106b5 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74  Page->pDirtyNext
106b6 20 7c 7c 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44   || pPage==p->pD
106b7 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 20 61 73  irtyTail );.  as
106b8 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69  sert( pPage->pDi
106b9 72 74 79 50 72 65 76 20 7c 7c 20 70 50 61 67 65  rtyPrev || pPage
106ba 3d 3d 70 2d 3e 70 44 69 72 74 79 20 29 3b 0a 0a  ==p->pDirty );..
106bb 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20    /* Update the 
106bc 50 43 61 63 68 65 31 2e 70 53 79 6e 63 65 64 20  PCache1.pSynced 
106bd 76 61 72 69 61 62 6c 65 20 69 66 20 6e 65 63 65  variable if nece
106be 73 73 61 72 79 2e 20 2a 2f 0a 20 20 69 66 28 20  ssary. */.  if( 
106bf 70 2d 3e 70 53 79 6e 63 65 64 3d 3d 70 50 61 67  p->pSynced==pPag
106c0 65 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a  e ){.    PgHdr *
106c1 70 53 79 6e 63 65 64 20 3d 20 70 50 61 67 65 2d  pSynced = pPage-
106c2 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20 20 20  >pDirtyPrev;.   
106c3 20 77 68 69 6c 65 28 20 70 53 79 6e 63 65 64 20   while( pSynced 
106c4 26 26 20 28 70 53 79 6e 63 65 64 2d 3e 66 6c 61  && (pSynced->fla
106c5 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
106c6 4e 43 29 20 29 7b 0a 20 20 20 20 20 20 70 53 79  NC) ){.      pSy
106c7 6e 63 65 64 20 3d 20 70 53 79 6e 63 65 64 2d 3e  nced = pSynced->
106c8 70 44 69 72 74 79 50 72 65 76 3b 0a 20 20 20 20  pDirtyPrev;.    
106c9 7d 0a 20 20 20 20 70 2d 3e 70 53 79 6e 63 65 64  }.    p->pSynced
106ca 20 3d 20 70 53 79 6e 63 65 64 3b 0a 20 20 7d 0a   = pSynced;.  }.
106cb 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 44  .  if( pPage->pD
106cc 69 72 74 79 4e 65 78 74 20 29 7b 0a 20 20 20 20  irtyNext ){.    
106cd 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78  pPage->pDirtyNex
106ce 74 2d 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20  t->pDirtyPrev = 
106cf 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65  pPage->pDirtyPre
106d0 76 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  v;.  }else{.    
106d1 61 73 73 65 72 74 28 20 70 50 61 67 65 3d 3d 70  assert( pPage==p
106d2 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 29 3b 0a  ->pDirtyTail );.
106d3 20 20 20 20 70 2d 3e 70 44 69 72 74 79 54 61 69      p->pDirtyTai
106d4 6c 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74  l = pPage->pDirt
106d5 79 50 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28  yPrev;.  }.  if(
106d6 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72   pPage->pDirtyPr
106d7 65 76 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d  ev ){.    pPage-
106d8 3e 70 44 69 72 74 79 50 72 65 76 2d 3e 70 44 69  >pDirtyPrev->pDi
106d9 72 74 79 4e 65 78 74 20 3d 20 70 50 61 67 65 2d  rtyNext = pPage-
106da 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d  >pDirtyNext;.  }
106db 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
106dc 28 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72  ( pPage==p->pDir
106dd 74 79 20 29 3b 0a 20 20 20 20 70 2d 3e 70 44 69  ty );.    p->pDi
106de 72 74 79 20 3d 20 70 50 61 67 65 2d 3e 70 44 69  rty = pPage->pDi
106df 72 74 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 70  rtyNext;.  }.  p
106e0 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74  Page->pDirtyNext
106e1 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 2d 3e 70   = 0;.  pPage->p
106e2 44 69 72 74 79 50 72 65 76 20 3d 20 30 3b 0a 0a  DirtyPrev = 0;..
106e3 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65    expensive_asse
106e4 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b 53  rt( pcacheCheckS
106e5 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f  ynced(p) );.}../
106e6 2a 0a 2a 2a 20 41 64 64 20 70 61 67 65 20 70 50  *.** Add page pP
106e7 61 67 65 20 74 6f 20 74 68 65 20 68 65 61 64 20  age to the head 
106e8 6f 66 20 74 68 65 20 64 69 72 74 79 20 6c 69 73  of the dirty lis
106e9 74 20 28 50 43 61 63 68 65 31 2e 70 44 69 72 74  t (PCache1.pDirt
106ea 79 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20 70  y is set to.** p
106eb 50 61 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  Page)..*/.static
106ec 20 76 6f 69 64 20 70 63 61 63 68 65 41 64 64 54   void pcacheAddT
106ed 6f 44 69 72 74 79 4c 69 73 74 28 50 67 48 64 72  oDirtyList(PgHdr
106ee 20 2a 70 50 61 67 65 29 7b 0a 20 20 50 43 61 63   *pPage){.  PCac
106ef 68 65 20 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70  he *p = pPage->p
106f0 43 61 63 68 65 3b 0a 0a 20 20 61 73 73 65 72 74  Cache;..  assert
106f1 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e  ( pPage->pDirtyN
106f2 65 78 74 3d 3d 30 20 26 26 20 70 50 61 67 65 2d  ext==0 && pPage-
106f3 3e 70 44 69 72 74 79 50 72 65 76 3d 3d 30 20 26  >pDirtyPrev==0 &
106f4 26 20 70 2d 3e 70 44 69 72 74 79 21 3d 70 50 61  & p->pDirty!=pPa
106f5 67 65 20 29 3b 0a 0a 20 20 70 50 61 67 65 2d 3e  ge );..  pPage->
106f6 70 44 69 72 74 79 4e 65 78 74 20 3d 20 70 2d 3e  pDirtyNext = p->
106f7 70 44 69 72 74 79 3b 0a 20 20 69 66 28 20 70 50  pDirty;.  if( pP
106f8 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20  age->pDirtyNext 
106f9 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
106fa 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74  Page->pDirtyNext
106fb 2d 3e 70 44 69 72 74 79 50 72 65 76 3d 3d 30 20  ->pDirtyPrev==0 
106fc 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44  );.    pPage->pD
106fd 69 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79  irtyNext->pDirty
106fe 50 72 65 76 20 3d 20 70 50 61 67 65 3b 0a 20 20  Prev = pPage;.  
106ff 7d 0a 20 20 70 2d 3e 70 44 69 72 74 79 20 3d 20  }.  p->pDirty = 
10700 70 50 61 67 65 3b 0a 20 20 69 66 28 20 21 70 2d  pPage;.  if( !p-
10701 3e 70 44 69 72 74 79 54 61 69 6c 20 29 7b 0a 20  >pDirtyTail ){. 
10702 20 20 20 70 2d 3e 70 44 69 72 74 79 54 61 69 6c     p->pDirtyTail
10703 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20   = pPage;.  }.  
10704 69 66 28 20 21 70 2d 3e 70 53 79 6e 63 65 64 20  if( !p->pSynced 
10705 26 26 20 30 3d 3d 28 70 50 61 67 65 2d 3e 66 6c  && 0==(pPage->fl
10706 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53  ags&PGHDR_NEED_S
10707 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 2d 3e 70  YNC) ){.    p->p
10708 53 79 6e 63 65 64 20 3d 20 70 50 61 67 65 3b 0a  Synced = pPage;.
10709 20 20 7d 0a 20 20 65 78 70 65 6e 73 69 76 65 5f    }.  expensive_
1070a 61 73 73 65 72 74 28 20 70 63 61 63 68 65 43 68  assert( pcacheCh
1070b 65 63 6b 53 79 6e 63 65 64 28 70 29 20 29 3b 0a  eckSynced(p) );.
1070c 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 72  }../*.** Wrapper
1070d 20 61 72 6f 75 6e 64 20 74 68 65 20 70 6c 75 67   around the plug
1070e 67 61 62 6c 65 20 63 61 63 68 65 73 20 78 55 6e  gable caches xUn
1070f 70 69 6e 20 6d 65 74 68 6f 64 2e 20 49 66 20 74  pin method. If t
10710 68 65 20 63 61 63 68 65 20 69 73 0a 2a 2a 20 62  he cache is.** b
10711 65 69 6e 67 20 75 73 65 64 20 66 6f 72 20 61 6e  eing used for an
10712 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
10713 61 73 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69  ase, this functi
10714 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  on is a no-op..*
10715 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63  /.static void pc
10716 61 63 68 65 55 6e 70 69 6e 28 50 67 48 64 72 20  acheUnpin(PgHdr 
10717 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70  *p){.  PCache *p
10718 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68  Cache = p->pCach
10719 65 3b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d  e;.  if( pCache-
1071a 3e 62 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20  >bPurgeable ){. 
1071b 20 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d     if( p->pgno==
1071c 31 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68  1 ){.      pCach
1071d 65 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20  e->pPage1 = 0;. 
1071e 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
1071f 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61  GlobalConfig.pca
10720 63 68 65 2e 78 55 6e 70 69 6e 28 70 43 61 63 68  che.xUnpin(pCach
10721 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20 30 29  e->pCache, p, 0)
10722 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  ;.  }.}../******
10723 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10724 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10725 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65  ************* Ge
10726 6e 65 72 61 6c 20 49 6e 74 65 72 66 61 63 65 73  neral Interfaces
10727 20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6e   ******.**.** In
10728 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 68 75  itialize and shu
10729 74 64 6f 77 6e 20 74 68 65 20 70 61 67 65 20 63  tdown the page c
1072a 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 2e 20  ache subsystem. 
1072b 4e 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65  Neither of these
1072c 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61   .** functions a
1072d 72 65 20 74 68 72 65 61 64 73 61 66 65 2e 0a 2a  re threadsafe..*
1072e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1072f 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63   int sqlite3Pcac
10730 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69  heInitialize(voi
10731 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  d){.  if( sqlite
10732 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
10733 61 63 68 65 2e 78 49 6e 69 74 3d 3d 30 20 29 7b  ache.xInit==0 ){
10734 0a 20 20 20 20 73 71 6c 69 74 65 33 50 43 61 63  .    sqlite3PCac
10735 68 65 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0a  heSetDefault();.
10736 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c    }.  return sql
10737 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
10738 2e 70 63 61 63 68 65 2e 78 49 6e 69 74 28 73 71  .pcache.xInit(sq
10739 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
1073a 67 2e 70 63 61 63 68 65 2e 70 41 72 67 29 3b 0a  g.pcache.pArg);.
1073b 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
1073c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
1073d 63 68 65 53 68 75 74 64 6f 77 6e 28 76 6f 69 64  cheShutdown(void
1073e 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ){.  if( sqlite3
1073f 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61  GlobalConfig.pca
10740 63 68 65 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b  che.xShutdown ){
10741 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
10742 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e  alConfig.pcache.
10743 78 53 68 75 74 64 6f 77 6e 28 73 71 6c 69 74 65  xShutdown(sqlite
10744 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
10745 61 63 68 65 2e 70 41 72 67 29 3b 0a 20 20 7d 0a  ache.pArg);.  }.
10746 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
10747 74 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65  the size in byte
10748 73 20 6f 66 20 61 20 50 43 61 63 68 65 20 6f 62  s of a PCache ob
10749 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ject..*/.SQLITE_
1074a 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1074b 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f  te3PcacheSize(vo
1074c 69 64 29 7b 20 72 65 74 75 72 6e 20 73 69 7a 65  id){ return size
1074d 6f 66 28 50 43 61 63 68 65 29 3b 20 7d 0a 0a 2f  of(PCache); }../
1074e 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
1074f 77 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e  w PCache object.
10750 20 53 74 6f 72 61 67 65 20 73 70 61 63 65 20 74   Storage space t
10751 6f 20 68 6f 6c 64 20 74 68 65 20 6f 62 6a 65 63  o hold the objec
10752 74 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79  t.** has already
10753 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20   been allocated 
10754 61 6e 64 20 69 73 20 70 61 73 73 65 64 20 69 6e  and is passed in
10755 20 61 73 20 74 68 65 20 70 20 70 6f 69 6e 74 65   as the p pointe
10756 72 2e 20 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65  r. .** The calle
10757 72 20 64 69 73 63 6f 76 65 72 73 20 68 6f 77 20  r discovers how 
10758 6d 75 63 68 20 73 70 61 63 65 20 6e 65 65 64 73  much space needs
10759 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64   to be allocated
1075a 20 62 79 20 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20   by .** calling 
1075b 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a  sqlite3PcacheSiz
1075c 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  e()..*/.SQLITE_P
1075d 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1075e 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28 0a 20  te3PcacheOpen(. 
1075f 20 69 6e 74 20 73 7a 50 61 67 65 2c 20 20 20 20   int szPage,    
10760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10761 20 53 69 7a 65 20 6f 66 20 65 76 65 72 79 20 70   Size of every p
10762 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45  age */.  int szE
10763 78 74 72 61 2c 20 20 20 20 20 20 20 20 20 20 20  xtra,           
10764 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20 73        /* Extra s
10765 70 61 63 65 20 61 73 73 6f 63 69 61 74 65 64 20  pace associated 
10766 77 69 74 68 20 65 61 63 68 20 70 61 67 65 20 2a  with each page *
10767 2f 0a 20 20 69 6e 74 20 62 50 75 72 67 65 61 62  /.  int bPurgeab
10768 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  le,             
10769 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61 67 65   /* True if page
1076a 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67  s are on backing
1076b 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 20   store */.  int 
1076c 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69 64 2a  (*xStress)(void*
1076d 2c 50 67 48 64 72 2a 29 2c 2f 2a 20 43 61 6c 6c  ,PgHdr*),/* Call
1076e 20 74 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20   to try to make 
1076f 70 61 67 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20  pages clean */. 
10770 20 76 6f 69 64 20 2a 70 53 74 72 65 73 73 2c 20   void *pStress, 
10771 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10772 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74   Argument to xSt
10773 72 65 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65  ress */.  PCache
10774 20 2a 70 20 20 20 20 20 20 20 20 20 20 20 20 20   *p             
10775 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c         /* Preall
10776 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 6f 72  ocated space for
10777 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f 0a 29   the PCache */.)
10778 7b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c  {.  memset(p, 0,
10779 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 29 29   sizeof(PCache))
1077a 3b 0a 20 20 70 2d 3e 73 7a 50 61 67 65 20 3d 20  ;.  p->szPage = 
1077b 73 7a 50 61 67 65 3b 0a 20 20 70 2d 3e 73 7a 45  szPage;.  p->szE
1077c 78 74 72 61 20 3d 20 73 7a 45 78 74 72 61 3b 0a  xtra = szExtra;.
1077d 20 20 70 2d 3e 62 50 75 72 67 65 61 62 6c 65 20    p->bPurgeable 
1077e 3d 20 62 50 75 72 67 65 61 62 6c 65 3b 0a 20 20  = bPurgeable;.  
1077f 70 2d 3e 78 53 74 72 65 73 73 20 3d 20 78 53 74  p->xStress = xSt
10780 72 65 73 73 3b 0a 20 20 70 2d 3e 70 53 74 72 65  ress;.  p->pStre
10781 73 73 20 3d 20 70 53 74 72 65 73 73 3b 0a 20 20  ss = pStress;.  
10782 70 2d 3e 6e 4d 61 78 20 3d 20 31 30 30 3b 0a 7d  p->nMax = 100;.}
10783 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
10784 68 65 20 70 61 67 65 20 73 69 7a 65 20 66 6f 72  he page size for
10785 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20   PCache object. 
10786 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20  The caller must 
10787 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 72  ensure that ther
10788 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 6f 75 74 73  e.** are no outs
10789 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72 65 66  tanding page ref
1078a 65 72 65 6e 63 65 73 20 77 68 65 6e 20 74 68 69  erences when thi
1078b 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
1078c 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lled..*/.SQLITE_
1078d 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1078e 69 74 65 33 50 63 61 63 68 65 53 65 74 50 61 67  ite3PcacheSetPag
1078f 65 53 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43  eSize(PCache *pC
10790 61 63 68 65 2c 20 69 6e 74 20 73 7a 50 61 67 65  ache, int szPage
10791 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 43 61  ){.  assert( pCa
10792 63 68 65 2d 3e 6e 52 65 66 3d 3d 30 20 26 26 20  che->nRef==0 && 
10793 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3d 3d  pCache->pDirty==
10794 30 20 29 3b 0a 20 20 69 66 28 20 70 43 61 63 68  0 );.  if( pCach
10795 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20  e->pCache ){.   
10796 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
10797 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 44 65 73  nfig.pcache.xDes
10798 74 72 6f 79 28 70 43 61 63 68 65 2d 3e 70 43 61  troy(pCache->pCa
10799 63 68 65 29 3b 0a 20 20 20 20 70 43 61 63 68 65  che);.    pCache
1079a 2d 3e 70 43 61 63 68 65 20 3d 20 30 3b 0a 20 20  ->pCache = 0;.  
1079b 7d 0a 20 20 70 43 61 63 68 65 2d 3e 73 7a 50 61  }.  pCache->szPa
1079c 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a 7d 0a 0a  ge = szPage;.}..
1079d 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74  /*.** Try to obt
1079e 61 69 6e 20 61 20 70 61 67 65 20 66 72 6f 6d 20  ain a page from 
1079f 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51  the cache..*/.SQ
107a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
107a1 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65   sqlite3PcacheFe
107a2 74 63 68 28 0a 20 20 50 43 61 63 68 65 20 2a 70  tch(.  PCache *p
107a3 43 61 63 68 65 2c 20 20 20 20 20 20 20 2f 2a 20  Cache,       /* 
107a4 4f 62 74 61 69 6e 20 74 68 65 20 70 61 67 65 20  Obtain the page 
107a5 66 72 6f 6d 20 74 68 69 73 20 63 61 63 68 65 20  from this cache 
107a6 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20  */.  Pgno pgno, 
107a7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
107a8 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 6f 62 74  ge number to obt
107a9 61 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65  ain */.  int cre
107aa 61 74 65 46 6c 61 67 2c 20 20 20 20 20 20 20 2f  ateFlag,       /
107ab 2a 20 49 66 20 74 72 75 65 2c 20 63 72 65 61 74  * If true, creat
107ac 65 20 70 61 67 65 20 69 66 20 69 74 20 64 6f 65  e page if it doe
107ad 73 20 6e 6f 74 20 65 78 69 73 74 20 61 6c 72 65  s not exist alre
107ae 61 64 79 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a  ady */.  PgHdr *
107af 2a 70 70 50 61 67 65 20 20 20 20 20 20 20 20 2f  *ppPage        /
107b0 2a 20 57 72 69 74 65 20 74 68 65 20 70 61 67 65  * Write the page
107b1 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 50 67   here */.){.  Pg
107b2 48 64 72 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a  Hdr *pPage = 0;.
107b3 20 20 69 6e 74 20 65 43 72 65 61 74 65 3b 0a 0a    int eCreate;..
107b4 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65    assert( pCache
107b5 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
107b6 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 7c   createFlag==1 |
107b7 7c 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 30 20  | createFlag==0 
107b8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 67 6e  );.  assert( pgn
107b9 6f 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  o>0 );..  /* If 
107ba 74 68 65 20 70 6c 75 67 67 61 62 6c 65 20 63 61  the pluggable ca
107bb 63 68 65 20 28 73 71 6c 69 74 65 33 5f 70 63 61  che (sqlite3_pca
107bc 63 68 65 2a 29 20 68 61 73 20 6e 6f 74 20 62 65  che*) has not be
107bd 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 0a 20 20  en allocated,.  
107be 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e  ** allocate it n
107bf 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  ow..  */.  if( !
107c0 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 26  pCache->pCache &
107c1 26 20 63 72 65 61 74 65 46 6c 61 67 20 29 7b 0a  & createFlag ){.
107c2 20 20 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63      sqlite3_pcac
107c3 68 65 20 2a 70 3b 0a 20 20 20 20 69 6e 74 20 6e  he *p;.    int n
107c4 42 79 74 65 3b 0a 20 20 20 20 6e 42 79 74 65 20  Byte;.    nByte 
107c5 3d 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65  = pCache->szPage
107c6 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 45 78 74   + pCache->szExt
107c7 72 61 20 2b 20 73 69 7a 65 6f 66 28 50 67 48 64  ra + sizeof(PgHd
107c8 72 29 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69  r);.    p = sqli
107c9 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
107ca 70 63 61 63 68 65 2e 78 43 72 65 61 74 65 28 6e  pcache.xCreate(n
107cb 42 79 74 65 2c 20 70 43 61 63 68 65 2d 3e 62 50  Byte, pCache->bP
107cc 75 72 67 65 61 62 6c 65 29 3b 0a 20 20 20 20 69  urgeable);.    i
107cd 66 28 20 21 70 20 29 7b 0a 20 20 20 20 20 20 72  f( !p ){.      r
107ce 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
107cf 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  EM;.    }.    sq
107d0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
107d1 67 2e 70 63 61 63 68 65 2e 78 43 61 63 68 65 73  g.pcache.xCaches
107d2 69 7a 65 28 70 2c 20 70 43 61 63 68 65 2d 3e 6e  ize(p, pCache->n
107d3 4d 61 78 29 3b 0a 20 20 20 20 70 43 61 63 68 65  Max);.    pCache
107d4 2d 3e 70 43 61 63 68 65 20 3d 20 70 3b 0a 20 20  ->pCache = p;.  
107d5 7d 0a 0a 20 20 65 43 72 65 61 74 65 20 3d 20 63  }..  eCreate = c
107d6 72 65 61 74 65 46 6c 61 67 20 2a 20 28 31 20 2b  reateFlag * (1 +
107d7 20 28 21 70 43 61 63 68 65 2d 3e 62 50 75 72 67   (!pCache->bPurg
107d8 65 61 62 6c 65 20 7c 7c 20 21 70 43 61 63 68 65  eable || !pCache
107d9 2d 3e 70 44 69 72 74 79 29 29 3b 0a 20 20 69 66  ->pDirty));.  if
107da 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65  ( pCache->pCache
107db 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20   ){.    pPage = 
107dc 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
107dd 66 69 67 2e 70 63 61 63 68 65 2e 78 46 65 74 63  fig.pcache.xFetc
107de 68 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65  h(pCache->pCache
107df 2c 20 70 67 6e 6f 2c 20 65 43 72 65 61 74 65 29  , pgno, eCreate)
107e0 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50  ;.  }..  if( !pP
107e1 61 67 65 20 26 26 20 65 43 72 65 61 74 65 3d 3d  age && eCreate==
107e2 31 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a  1 ){.    PgHdr *
107e3 70 50 67 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e  pPg;..    /* Fin
107e4 64 20 61 20 64 69 72 74 79 20 70 61 67 65 20 74  d a dirty page t
107e5 6f 20 77 72 69 74 65 2d 6f 75 74 20 61 6e 64 20  o write-out and 
107e6 72 65 63 79 63 6c 65 2e 20 46 69 72 73 74 20 74  recycle. First t
107e7 72 79 20 74 6f 20 66 69 6e 64 20 61 20 0a 20 20  ry to find a .  
107e8 20 20 2a 2a 20 70 61 67 65 20 74 68 61 74 20 64    ** page that d
107e9 6f 65 73 20 6e 6f 74 20 72 65 71 75 69 72 65 20  oes not require 
107ea 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 28  a journal-sync (
107eb 6f 6e 65 20 77 69 74 68 20 50 47 48 44 52 5f 4e  one with PGHDR_N
107ec 45 45 44 5f 53 59 4e 43 0a 20 20 20 20 2a 2a 20  EED_SYNC.    ** 
107ed 63 6c 65 61 72 65 64 29 2c 20 62 75 74 20 69 66  cleared), but if
107ee 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70 6f 73   that is not pos
107ef 73 69 62 6c 65 20 73 65 74 74 6c 65 20 66 6f 72  sible settle for
107f0 20 61 6e 79 20 6f 74 68 65 72 20 0a 20 20 20 20   any other .    
107f1 2a 2a 20 75 6e 72 65 66 65 72 65 6e 63 65 64 20  ** unreferenced 
107f2 64 69 72 74 79 20 70 61 67 65 2e 0a 20 20 20 20  dirty page..    
107f3 2a 2f 0a 20 20 20 20 65 78 70 65 6e 73 69 76 65  */.    expensive
107f4 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65 43  _assert( pcacheC
107f5 68 65 63 6b 53 79 6e 63 65 64 28 70 43 61 63 68  heckSynced(pCach
107f6 65 29 20 29 3b 0a 20 20 20 20 66 6f 72 28 70 50  e) );.    for(pP
107f7 67 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65  g=pCache->pSynce
107f8 64 3b 20 0a 20 20 20 20 20 20 20 20 70 50 67 20  d; .        pPg 
107f9 26 26 20 28 70 50 67 2d 3e 6e 52 65 66 20 7c 7c  && (pPg->nRef ||
107fa 20 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48   (pPg->flags&PGH
107fb 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 29 3b 20  DR_NEED_SYNC)); 
107fc 0a 20 20 20 20 20 20 20 20 70 50 67 3d 70 50 67  .        pPg=pPg
107fd 2d 3e 70 44 69 72 74 79 50 72 65 76 0a 20 20 20  ->pDirtyPrev.   
107fe 20 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50 67   );.    if( !pPg
107ff 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 70 50   ){.      for(pP
10800 67 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79  g=pCache->pDirty
10801 54 61 69 6c 3b 20 70 50 67 20 26 26 20 70 50 67  Tail; pPg && pPg
10802 2d 3e 6e 52 65 66 3b 20 70 50 67 3d 70 50 67 2d  ->nRef; pPg=pPg-
10803 3e 70 44 69 72 74 79 50 72 65 76 29 3b 0a 20 20  >pDirtyPrev);.  
10804 20 20 7d 0a 20 20 20 20 69 66 28 20 70 50 67 20    }.    if( pPg 
10805 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b  ){.      int rc;
10806 0a 20 20 20 20 20 20 72 63 20 3d 20 70 43 61 63  .      rc = pCac
10807 68 65 2d 3e 78 53 74 72 65 73 73 28 70 43 61 63  he->xStress(pCac
10808 68 65 2d 3e 70 53 74 72 65 73 73 2c 20 70 50 67  he->pStress, pPg
10809 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
1080a 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63  =SQLITE_OK && rc
1080b 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b  !=SQLITE_BUSY ){
1080c 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
1080d 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
1080e 7d 0a 0a 20 20 20 20 70 50 61 67 65 20 3d 20 73  }..    pPage = s
1080f 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
10810 69 67 2e 70 63 61 63 68 65 2e 78 46 65 74 63 68  ig.pcache.xFetch
10811 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c  (pCache->pCache,
10812 20 70 67 6e 6f 2c 20 32 29 3b 0a 20 20 7d 0a 0a   pgno, 2);.  }..
10813 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20    if( pPage ){. 
10814 20 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 70     if( !pPage->p
10815 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 6d 65  Data ){.      me
10816 6d 73 65 74 28 70 50 61 67 65 2c 20 30 2c 20 73  mset(pPage, 0, s
10817 69 7a 65 6f 66 28 50 67 48 64 72 29 20 2b 20 70  izeof(PgHdr) + p
10818 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 29 3b  Cache->szExtra);
10819 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 45  .      pPage->pE
1081a 78 74 72 61 20 3d 20 28 76 6f 69 64 2a 29 26 70  xtra = (void*)&p
1081b 50 61 67 65 5b 31 5d 3b 0a 20 20 20 20 20 20 70  Page[1];.      p
1081c 50 61 67 65 2d 3e 70 44 61 74 61 20 3d 20 28 76  Page->pData = (v
1081d 6f 69 64 20 2a 29 26 28 28 63 68 61 72 20 2a 29  oid *)&((char *)
1081e 70 50 61 67 65 29 5b 73 69 7a 65 6f 66 28 50 67  pPage)[sizeof(Pg
1081f 48 64 72 29 20 2b 20 70 43 61 63 68 65 2d 3e 73  Hdr) + pCache->s
10820 7a 45 78 74 72 61 5d 3b 0a 20 20 20 20 20 20 70  zExtra];.      p
10821 50 61 67 65 2d 3e 70 43 61 63 68 65 20 3d 20 70  Page->pCache = p
10822 43 61 63 68 65 3b 0a 20 20 20 20 20 20 70 50 61  Cache;.      pPa
10823 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b  ge->pgno = pgno;
10824 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
10825 74 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65  t( pPage->pCache
10826 3d 3d 70 43 61 63 68 65 20 29 3b 0a 20 20 20 20  ==pCache );.    
10827 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70  assert( pPage->p
10828 67 6e 6f 3d 3d 70 67 6e 6f 20 29 3b 0a 20 20 20  gno==pgno );.   
10829 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
1082a 70 45 78 74 72 61 3d 3d 28 76 6f 69 64 20 2a 29  pExtra==(void *)
1082b 26 70 50 61 67 65 5b 31 5d 20 29 3b 0a 0a 20 20  &pPage[1] );..  
1082c 20 20 69 66 28 20 30 3d 3d 70 50 61 67 65 2d 3e    if( 0==pPage->
1082d 6e 52 65 66 20 29 7b 0a 20 20 20 20 20 20 70 43  nRef ){.      pC
1082e 61 63 68 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20  ache->nRef++;.  
1082f 20 20 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e    }.    pPage->n
10830 52 65 66 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70  Ref++;.    if( p
10831 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  gno==1 ){.      
10832 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20 3d  pCache->pPage1 =
10833 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20   pPage;.    }.  
10834 7d 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50  }.  *ppPage = pP
10835 61 67 65 3b 0a 20 20 72 65 74 75 72 6e 20 28 70  age;.  return (p
10836 50 61 67 65 3d 3d 30 20 26 26 20 65 43 72 65 61  Page==0 && eCrea
10837 74 65 29 20 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d  te) ? SQLITE_NOM
10838 45 4d 20 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  EM : SQLITE_OK;.
10839 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65  }../*.** Decreme
1083a 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  nt the reference
1083b 20 63 6f 75 6e 74 20 6f 6e 20 61 20 70 61 67 65   count on a page
1083c 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 69 73  . If the page is
1083d 20 63 6c 65 61 6e 20 61 6e 64 20 74 68 65 0a 2a   clean and the.*
1083e 2a 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  * reference coun
1083f 74 20 64 72 6f 70 73 20 74 6f 20 30 2c 20 74 68  t drops to 0, th
10840 65 6e 20 69 74 20 69 73 20 6d 61 64 65 20 65 6c  en it is made el
10841 69 62 6c 65 20 66 6f 72 20 72 65 63 79 63 6c 69  ible for recycli
10842 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ng..*/.SQLITE_PR
10843 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
10844 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 28  e3PcacheRelease(
10845 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 61 73 73  PgHdr *p){.  ass
10846 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29  ert( p->nRef>0 )
10847 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20  ;.  p->nRef--;. 
10848 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20   if( p->nRef==0 
10849 29 7b 0a 20 20 20 20 50 43 61 63 68 65 20 2a 70  ){.    PCache *p
1084a 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68  Cache = p->pCach
1084b 65 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e  e;.    pCache->n
1084c 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 28  Ref--;.    if( (
1084d 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44  p->flags&PGHDR_D
1084e 49 52 54 59 29 3d 3d 30 20 29 7b 0a 20 20 20 20  IRTY)==0 ){.    
1084f 20 20 70 63 61 63 68 65 55 6e 70 69 6e 28 70 29    pcacheUnpin(p)
10850 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
10851 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70     /* Move the p
10852 61 67 65 20 74 6f 20 74 68 65 20 68 65 61 64 20  age to the head 
10853 6f 66 20 74 68 65 20 64 69 72 74 79 20 6c 69 73  of the dirty lis
10854 74 2e 20 2a 2f 0a 20 20 20 20 20 20 70 63 61 63  t. */.      pcac
10855 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74  heRemoveFromDirt
10856 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 20 20  yList(p);.      
10857 70 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 79  pcacheAddToDirty
10858 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 7d 0a 20  List(p);.    }. 
10859 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72   }.}../*.** Incr
1085a 65 61 73 65 20 74 68 65 20 72 65 66 65 72 65 6e  ease the referen
1085b 63 65 20 63 6f 75 6e 74 20 6f 66 20 61 20 73 75  ce count of a su
1085c 70 70 6c 69 65 64 20 70 61 67 65 20 62 79 20 31  pplied page by 1
1085d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1085e 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1085f 50 63 61 63 68 65 52 65 66 28 50 67 48 64 72 20  PcacheRef(PgHdr 
10860 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 70 2d  *p){.  assert(p-
10861 3e 6e 52 65 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e  >nRef>0);.  p->n
10862 52 65 66 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Ref++;.}../*.** 
10863 44 72 6f 70 20 61 20 70 61 67 65 20 66 72 6f 6d  Drop a page from
10864 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 65 72   the cache. Ther
10865 65 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c  e must be exactl
10866 79 20 6f 6e 65 20 72 65 66 65 72 65 6e 63 65 20  y one reference 
10867 74 6f 20 74 68 65 0a 2a 2a 20 70 61 67 65 2e 20  to the.** page. 
10868 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65  This function de
10869 6c 65 74 65 73 20 74 68 61 74 20 72 65 66 65 72  letes that refer
1086a 65 6e 63 65 2c 20 73 6f 20 61 66 74 65 72 20 69  ence, so after i
1086b 74 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a  t returns the.**
1086c 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f   page pointed to
1086d 20 62 79 20 70 20 69 73 20 69 6e 76 61 6c 69 64   by p is invalid
1086e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1086f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
10870 50 63 61 63 68 65 44 72 6f 70 28 50 67 48 64 72  PcacheDrop(PgHdr
10871 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 20 2a   *p){.  PCache *
10872 70 43 61 63 68 65 3b 0a 20 20 61 73 73 65 72 74  pCache;.  assert
10873 28 20 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a  ( p->nRef==1 );.
10874 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 50    if( p->flags&P
10875 47 48 44 52 5f 44 49 52 54 59 20 29 7b 0a 20 20  GHDR_DIRTY ){.  
10876 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72    pcacheRemoveFr
10877 6f 6d 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a  omDirtyList(p);.
10878 20 20 7d 0a 20 20 70 43 61 63 68 65 20 3d 20 70    }.  pCache = p
10879 2d 3e 70 43 61 63 68 65 3b 0a 20 20 70 43 61 63  ->pCache;.  pCac
1087a 68 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66  he->nRef--;.  if
1087b 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a  ( p->pgno==1 ){.
1087c 20 20 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67      pCache->pPag
1087d 65 31 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71  e1 = 0;.  }.  sq
1087e 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
1087f 67 2e 70 63 61 63 68 65 2e 78 55 6e 70 69 6e 28  g.pcache.xUnpin(
10880 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20  pCache->pCache, 
10881 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  p, 1);.}../*.** 
10882 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 61  Make sure the pa
10883 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20  ge is marked as 
10884 64 69 72 74 79 2e 20 49 66 20 69 74 20 69 73 6e  dirty. If it isn
10885 27 74 20 64 69 72 74 79 20 61 6c 72 65 61 64 79  't dirty already
10886 2c 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e  ,.** make it so.
10887 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
10888 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
10889 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 50  cacheMakeDirty(P
1088a 67 48 64 72 20 2a 70 29 7b 0a 20 20 70 2d 3e 66  gHdr *p){.  p->f
1088b 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 44  lags &= ~PGHDR_D
1088c 4f 4e 54 5f 57 52 49 54 45 3b 0a 20 20 61 73 73  ONT_WRITE;.  ass
1088d 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29  ert( p->nRef>0 )
1088e 3b 0a 20 20 69 66 28 20 30 3d 3d 28 70 2d 3e 66  ;.  if( 0==(p->f
1088f 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 49 52  lags & PGHDR_DIR
10890 54 59 29 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c  TY) ){.    p->fl
10891 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 44 49 52  ags |= PGHDR_DIR
10892 54 59 3b 0a 20 20 20 20 70 63 61 63 68 65 41 64  TY;.    pcacheAd
10893 64 54 6f 44 69 72 74 79 4c 69 73 74 28 20 70 29  dToDirtyList( p)
10894 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  ;.  }.}../*.** M
10895 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 61 67  ake sure the pag
10896 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 63  e is marked as c
10897 6c 65 61 6e 2e 20 49 66 20 69 74 20 69 73 6e 27  lean. If it isn'
10898 74 20 63 6c 65 61 6e 20 61 6c 72 65 61 64 79 2c  t clean already,
10899 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a  .** make it so..
1089a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1089b 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
1089c 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 50 67  acheMakeClean(Pg
1089d 48 64 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 28  Hdr *p){.  if( (
1089e 70 2d 3e 66 6c 61 67 73 20 26 20 50 47 48 44 52  p->flags & PGHDR
1089f 5f 44 49 52 54 59 29 20 29 7b 0a 20 20 20 20 70  _DIRTY) ){.    p
108a0 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44  cacheRemoveFromD
108a1 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20  irtyList(p);.   
108a2 20 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 50   p->flags &= ~(P
108a3 47 48 44 52 5f 44 49 52 54 59 7c 50 47 48 44 52  GHDR_DIRTY|PGHDR
108a4 5f 4e 45 45 44 5f 53 59 4e 43 29 3b 0a 20 20 20  _NEED_SYNC);.   
108a5 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20   if( p->nRef==0 
108a6 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 55  ){.      pcacheU
108a7 6e 70 69 6e 28 70 29 3b 0a 20 20 20 20 7d 0a 20  npin(p);.    }. 
108a8 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65   }.}../*.** Make
108a9 20 65 76 65 72 79 20 70 61 67 65 20 69 6e 20 74   every page in t
108aa 68 65 20 63 61 63 68 65 20 63 6c 65 61 6e 2e 0a  he cache clean..
108ab 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
108ac 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
108ad 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61  acheCleanAll(PCa
108ae 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20  che *pCache){.  
108af 50 67 48 64 72 20 2a 70 3b 0a 20 20 77 68 69 6c  PgHdr *p;.  whil
108b0 65 28 20 28 70 20 3d 20 70 43 61 63 68 65 2d 3e  e( (p = pCache->
108b1 70 44 69 72 74 79 29 21 3d 30 20 29 7b 0a 20 20  pDirty)!=0 ){.  
108b2 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d    sqlite3PcacheM
108b3 61 6b 65 43 6c 65 61 6e 28 70 29 3b 0a 20 20 7d  akeClean(p);.  }
108b4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  .}../*.** Clear 
108b5 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53  the PGHDR_NEED_S
108b6 59 4e 43 20 66 6c 61 67 20 66 72 6f 6d 20 61 6c  YNC flag from al
108b7 6c 20 64 69 72 74 79 20 70 61 67 65 73 2e 0a 2a  l dirty pages..*
108b8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
108b9 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
108ba 63 68 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67  cheClearSyncFlag
108bb 73 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65  s(PCache *pCache
108bc 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20  ){.  PgHdr *p;. 
108bd 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70   for(p=pCache->p
108be 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70  Dirty; p; p=p->p
108bf 44 69 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20  DirtyNext){.    
108c0 70 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48  p->flags &= ~PGH
108c1 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20  DR_NEED_SYNC;.  
108c2 7d 0a 20 20 70 43 61 63 68 65 2d 3e 70 53 79 6e  }.  pCache->pSyn
108c3 63 65 64 20 3d 20 70 43 61 63 68 65 2d 3e 70 44  ced = pCache->pD
108c4 69 72 74 79 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a  irtyTail;.}../*.
108c5 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 70 61  ** Change the pa
108c6 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  ge number of pag
108c7 65 20 70 20 74 6f 20 6e 65 77 50 67 6e 6f 2e 20  e p to newPgno. 
108c8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
108c9 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
108ca 63 61 63 68 65 4d 6f 76 65 28 50 67 48 64 72 20  cacheMove(PgHdr 
108cb 2a 70 2c 20 50 67 6e 6f 20 6e 65 77 50 67 6e 6f  *p, Pgno newPgno
108cc 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61  ){.  PCache *pCa
108cd 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b  che = p->pCache;
108ce 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52  .  assert( p->nR
108cf 65 66 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74  ef>0 );.  assert
108d0 28 20 6e 65 77 50 67 6e 6f 3e 30 20 29 3b 0a 20  ( newPgno>0 );. 
108d1 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
108d2 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 52 65 6b  nfig.pcache.xRek
108d3 65 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68  ey(pCache->pCach
108d4 65 2c 20 70 2c 20 70 2d 3e 70 67 6e 6f 2c 20 6e  e, p, p->pgno, n
108d5 65 77 50 67 6e 6f 29 3b 0a 20 20 70 2d 3e 70 67  ewPgno);.  p->pg
108d6 6e 6f 20 3d 20 6e 65 77 50 67 6e 6f 3b 0a 20 20  no = newPgno;.  
108d7 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 26 50 47  if( (p->flags&PG
108d8 48 44 52 5f 44 49 52 54 59 29 20 26 26 20 28 70  HDR_DIRTY) && (p
108d9 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45  ->flags&PGHDR_NE
108da 45 44 5f 53 59 4e 43 29 20 29 7b 0a 20 20 20 20  ED_SYNC) ){.    
108db 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d  pcacheRemoveFrom
108dc 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20  DirtyList(p);.  
108dd 20 20 70 63 61 63 68 65 41 64 64 54 6f 44 69 72    pcacheAddToDir
108de 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 7d 0a 7d  tyList(p);.  }.}
108df 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 65 76 65  ../*.** Drop eve
108e0 72 79 20 63 61 63 68 65 20 65 6e 74 72 79 20 77  ry cache entry w
108e1 68 6f 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72  hose page number
108e2 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
108e3 20 22 70 67 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20   "pgno". The.** 
108e4 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75  caller must ensu
108e5 72 65 20 74 68 61 74 20 74 68 65 72 65 20 61 72  re that there ar
108e6 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67  e no outstanding
108e7 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61   references to a
108e8 6e 79 20 70 61 67 65 73 0a 2a 2a 20 6f 74 68 65  ny pages.** othe
108e9 72 20 74 68 61 6e 20 70 61 67 65 20 31 20 77 69  r than page 1 wi
108ea 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72  th a page number
108eb 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 70 67   greater than pg
108ec 6e 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  no..**.** If the
108ed 72 65 20 69 73 20 61 20 72 65 66 65 72 65 6e 63  re is a referenc
108ee 65 20 74 6f 20 70 61 67 65 20 31 20 61 6e 64 20  e to page 1 and 
108ef 74 68 65 20 70 67 6e 6f 20 70 61 72 61 6d 65 74  the pgno paramet
108f0 65 72 20 70 61 73 73 65 64 20 74 6f 20 74 68 69  er passed to thi
108f1 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73  s.** function is
108f2 20 30 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74   0, then the dat
108f3 61 20 61 72 65 61 20 61 73 73 6f 63 69 61 74 65  a area associate
108f4 64 20 77 69 74 68 20 70 61 67 65 20 31 20 69 73  d with page 1 is
108f5 20 7a 65 72 6f 65 64 2c 20 62 75 74 0a 2a 2a 20   zeroed, but.** 
108f6 74 68 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20  the page object 
108f7 69 73 20 6e 6f 74 20 64 72 6f 70 70 65 64 2e 0a  is not dropped..
108f8 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
108f9 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
108fa 61 63 68 65 54 72 75 6e 63 61 74 65 28 50 43 61  acheTruncate(PCa
108fb 63 68 65 20 2a 70 43 61 63 68 65 2c 20 50 67 6e  che *pCache, Pgn
108fc 6f 20 70 67 6e 6f 29 7b 0a 20 20 69 66 28 20 70  o pgno){.  if( p
108fd 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b  Cache->pCache ){
108fe 0a 20 20 20 20 50 67 48 64 72 20 2a 70 3b 0a 20  .    PgHdr *p;. 
108ff 20 20 20 50 67 48 64 72 20 2a 70 4e 65 78 74 3b     PgHdr *pNext;
10900 0a 20 20 20 20 66 6f 72 28 70 3d 70 43 61 63 68  .    for(p=pCach
10901 65 2d 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 3d  e->pDirty; p; p=
10902 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 70 4e  pNext){.      pN
10903 65 78 74 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e  ext = p->pDirtyN
10904 65 78 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ext;.      if( p
10905 2d 3e 70 67 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20  ->pgno>pgno ){. 
10906 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
10907 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49  ->flags&PGHDR_DI
10908 52 54 59 20 29 3b 0a 20 20 20 20 20 20 20 20 73  RTY );.        s
10909 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65  qlite3PcacheMake
1090a 43 6c 65 61 6e 28 70 29 3b 0a 20 20 20 20 20 20  Clean(p);.      
1090b 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
1090c 70 67 6e 6f 3d 3d 30 20 26 26 20 70 43 61 63 68  pgno==0 && pCach
1090d 65 2d 3e 70 50 61 67 65 31 20 29 7b 0a 20 20 20  e->pPage1 ){.   
1090e 20 20 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65     memset(pCache
1090f 2d 3e 70 50 61 67 65 31 2d 3e 70 44 61 74 61 2c  ->pPage1->pData,
10910 20 30 2c 20 70 43 61 63 68 65 2d 3e 73 7a 50 61   0, pCache->szPa
10911 67 65 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20  ge);.      pgno 
10912 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  = 1;.    }.    s
10913 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
10914 69 67 2e 70 63 61 63 68 65 2e 78 54 72 75 6e 63  ig.pcache.xTrunc
10915 61 74 65 28 70 43 61 63 68 65 2d 3e 70 43 61 63  ate(pCache->pCac
10916 68 65 2c 20 70 67 6e 6f 2b 31 29 3b 0a 20 20 7d  he, pgno+1);.  }
10917 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
10918 61 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49  a cache..*/.SQLI
10919 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1091a 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f  sqlite3PcacheClo
1091b 73 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68  se(PCache *pCach
1091c 65 29 7b 0a 20 20 69 66 28 20 70 43 61 63 68 65  e){.  if( pCache
1091d 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20  ->pCache ){.    
1091e 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
1091f 66 69 67 2e 70 63 61 63 68 65 2e 78 44 65 73 74  fig.pcache.xDest
10920 72 6f 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63  roy(pCache->pCac
10921 68 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a  he);.  }.}../* .
10922 2a 2a 20 44 69 73 63 61 72 64 20 74 68 65 20 63  ** Discard the c
10923 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63  ontents of the c
10924 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ache..*/.SQLITE_
10925 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
10926 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 28  ite3PcacheClear(
10927 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b  PCache *pCache){
10928 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  .  sqlite3Pcache
10929 54 72 75 6e 63 61 74 65 28 70 43 61 63 68 65 2c  Truncate(pCache,
1092a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65   0);.}../*.** Me
1092b 72 67 65 20 74 77 6f 20 6c 69 73 74 73 20 6f 66  rge two lists of
1092c 20 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64   pages connected
1092d 20 62 79 20 70 44 69 72 74 79 20 61 6e 64 20 69   by pDirty and i
1092e 6e 20 70 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a  n pgno order..**
1092f 20 44 6f 20 6e 6f 74 20 62 6f 74 68 20 66 69 78   Do not both fix
10930 69 6e 67 20 74 68 65 20 70 44 69 72 74 79 50 72  ing the pDirtyPr
10931 65 76 20 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a  ev pointers..*/.
10932 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63  static PgHdr *pc
10933 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69  acheMergeDirtyLi
10934 73 74 28 50 67 48 64 72 20 2a 70 41 2c 20 50 67  st(PgHdr *pA, Pg
10935 48 64 72 20 2a 70 42 29 7b 0a 20 20 50 67 48 64  Hdr *pB){.  PgHd
10936 72 20 72 65 73 75 6c 74 2c 20 2a 70 54 61 69 6c  r result, *pTail
10937 3b 0a 20 20 70 54 61 69 6c 20 3d 20 26 72 65 73  ;.  pTail = &res
10938 75 6c 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 41  ult;.  while( pA
10939 20 26 26 20 70 42 20 29 7b 0a 20 20 20 20 69 66   && pB ){.    if
1093a 28 20 70 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70  ( pA->pgno<pB->p
1093b 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 70 54 61  gno ){.      pTa
1093c 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41 3b  il->pDirty = pA;
1093d 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70  .      pTail = p
1093e 41 3b 0a 20 20 20 20 20 20 70 41 20 3d 20 70 41  A;.      pA = pA
1093f 2d 3e 70 44 69 72 74 79 3b 0a 20 20 20 20 7d 65  ->pDirty;.    }e
10940 6c 73 65 7b 0a 20 20 20 20 20 20 70 54 61 69 6c  lse{.      pTail
10941 2d 3e 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20  ->pDirty = pB;. 
10942 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 42 3b       pTail = pB;
10943 0a 20 20 20 20 20 20 70 42 20 3d 20 70 42 2d 3e  .      pB = pB->
10944 70 44 69 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20  pDirty;.    }.  
10945 7d 0a 20 20 69 66 28 20 70 41 20 29 7b 0a 20 20  }.  if( pA ){.  
10946 20 20 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 20    pTail->pDirty 
10947 3d 20 70 41 3b 0a 20 20 7d 65 6c 73 65 20 69 66  = pA;.  }else if
10948 28 20 70 42 20 29 7b 0a 20 20 20 20 70 54 61 69  ( pB ){.    pTai
10949 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70 42 3b 0a  l->pDirty = pB;.
1094a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 61    }else{.    pTa
1094b 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a  il->pDirty = 0;.
1094c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73    }.  return res
1094d 75 6c 74 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f  ult.pDirty;.}../
1094e 2a 0a 2a 2a 20 53 6f 72 74 20 74 68 65 20 6c 69  *.** Sort the li
1094f 73 74 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61  st of pages in a
10950 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62  ccending order b
10951 79 20 70 67 6e 6f 2e 20 20 50 61 67 65 73 20 61  y pgno.  Pages a
10952 72 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20  re.** connected 
10953 62 79 20 70 44 69 72 74 79 20 70 6f 69 6e 74 65  by pDirty pointe
10954 72 73 2e 20 20 54 68 65 20 70 44 69 72 74 79 50  rs.  The pDirtyP
10955 72 65 76 20 70 6f 69 6e 74 65 72 73 20 61 72 65  rev pointers are
10956 0a 2a 2a 20 63 6f 72 72 75 70 74 65 64 20 62 79  .** corrupted by
10957 20 74 68 69 73 20 73 6f 72 74 2e 0a 2a 2a 0a 2a   this sort..**.*
10958 2a 20 53 69 6e 63 65 20 74 68 65 72 65 20 63 61  * Since there ca
10959 6e 6e 6f 74 20 62 65 20 6d 6f 72 65 20 74 68 61  nnot be more tha
1095a 6e 20 32 5e 33 31 20 64 69 73 74 69 6e 63 74 20  n 2^31 distinct 
1095b 70 61 67 65 73 20 69 6e 20 61 20 64 61 74 61 62  pages in a datab
1095c 61 73 65 2c 0a 2a 2a 20 74 68 65 72 65 20 63 61  ase,.** there ca
1095d 6e 6e 6f 74 20 62 65 20 6d 6f 72 65 20 74 68 61  nnot be more tha
1095e 6e 20 33 31 20 62 75 63 6b 65 74 73 20 72 65 71  n 31 buckets req
1095f 75 69 72 65 64 20 62 79 20 74 68 65 20 6d 65 72  uired by the mer
10960 67 65 20 73 6f 72 74 65 72 2e 0a 2a 2a 20 4f 6e  ge sorter..** On
10961 65 20 65 78 74 72 61 20 62 75 63 6b 65 74 20 69  e extra bucket i
10962 73 20 61 64 64 65 64 20 74 6f 20 63 61 74 63 68  s added to catch
10963 20 6f 76 65 72 66 6c 6f 77 20 69 6e 20 63 61 73   overflow in cas
10964 65 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 65  e something.** e
10965 76 65 72 20 63 68 61 6e 67 65 73 20 74 6f 20 6d  ver changes to m
10966 61 6b 65 20 74 68 65 20 70 72 65 76 69 6f 75 73  ake the previous
10967 20 73 65 6e 74 65 6e 63 65 20 69 6e 63 6f 72 72   sentence incorr
10968 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ect..*/.#define 
10969 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 20 20 33  N_SORT_BUCKET  3
1096a 32 0a 73 74 61 74 69 63 20 50 67 48 64 72 20 2a  2.static PgHdr *
1096b 70 63 61 63 68 65 53 6f 72 74 44 69 72 74 79 4c  pcacheSortDirtyL
1096c 69 73 74 28 50 67 48 64 72 20 2a 70 49 6e 29 7b  ist(PgHdr *pIn){
1096d 0a 20 20 50 67 48 64 72 20 2a 61 5b 4e 5f 53 4f  .  PgHdr *a[N_SO
1096e 52 54 5f 42 55 43 4b 45 54 5d 2c 20 2a 70 3b 0a  RT_BUCKET], *p;.
1096f 20 20 69 6e 74 20 69 3b 0a 20 20 6d 65 6d 73 65    int i;.  memse
10970 74 28 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61  t(a, 0, sizeof(a
10971 29 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 49 6e  ));.  while( pIn
10972 20 29 7b 0a 20 20 20 20 70 20 3d 20 70 49 6e 3b   ){.    p = pIn;
10973 0a 20 20 20 20 70 49 6e 20 3d 20 70 2d 3e 70 44  .    pIn = p->pD
10974 69 72 74 79 3b 0a 20 20 20 20 70 2d 3e 70 44 69  irty;.    p->pDi
10975 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72  rty = 0;.    for
10976 28 69 3d 30 3b 20 41 4c 57 41 59 53 28 69 3c 4e  (i=0; ALWAYS(i<N
10977 5f 53 4f 52 54 5f 42 55 43 4b 45 54 2d 31 29 3b  _SORT_BUCKET-1);
10978 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
10979 20 61 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20   a[i]==0 ){.    
1097a 20 20 20 20 61 5b 69 5d 20 3d 20 70 3b 0a 20 20      a[i] = p;.  
1097b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1097c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1097d 20 20 70 20 3d 20 70 63 61 63 68 65 4d 65 72 67    p = pcacheMerg
1097e 65 44 69 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c  eDirtyList(a[i],
1097f 20 70 29 3b 0a 20 20 20 20 20 20 20 20 61 5b 69   p);.        a[i
10980 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  ] = 0;.      }. 
10981 20 20 20 7d 0a 20 20 20 20 69 66 28 20 4e 45 56     }.    if( NEV
10982 45 52 28 69 3d 3d 4e 5f 53 4f 52 54 5f 42 55 43  ER(i==N_SORT_BUC
10983 4b 45 54 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  KET-1) ){.      
10984 2f 2a 20 54 6f 20 67 65 74 20 68 65 72 65 2c 20  /* To get here, 
10985 74 68 65 72 65 20 6e 65 65 64 20 74 6f 20 62 65  there need to be
10986 20 32 5e 28 4e 5f 53 4f 52 54 5f 42 55 43 4b 45   2^(N_SORT_BUCKE
10987 54 29 20 65 6c 65 6d 65 6e 74 73 20 69 6e 0a 20  T) elements in. 
10988 20 20 20 20 20 2a 2a 20 74 68 65 20 69 6e 70 75       ** the inpu
10989 74 20 6c 69 73 74 2e 20 20 42 75 74 20 74 68 61  t list.  But tha
1098a 74 20 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 2e  t is impossible.
1098b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1098c 61 5b 69 5d 20 3d 20 70 63 61 63 68 65 4d 65 72  a[i] = pcacheMer
1098d 67 65 44 69 72 74 79 4c 69 73 74 28 61 5b 69 5d  geDirtyList(a[i]
1098e 2c 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  , p);.    }.  }.
1098f 20 20 70 20 3d 20 61 5b 30 5d 3b 0a 20 20 66 6f    p = a[0];.  fo
10990 72 28 69 3d 31 3b 20 69 3c 4e 5f 53 4f 52 54 5f  r(i=1; i<N_SORT_
10991 42 55 43 4b 45 54 3b 20 69 2b 2b 29 7b 0a 20 20  BUCKET; i++){.  
10992 20 20 70 20 3d 20 70 63 61 63 68 65 4d 65 72 67    p = pcacheMerg
10993 65 44 69 72 74 79 4c 69 73 74 28 70 2c 20 61 5b  eDirtyList(p, a[
10994 69 5d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  i]);.  }.  retur
10995 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  n p;.}../*.** Re
10996 74 75 72 6e 20 61 20 6c 69 73 74 20 6f 66 20 61  turn a list of a
10997 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 69  ll dirty pages i
10998 6e 20 74 68 65 20 63 61 63 68 65 2c 20 73 6f 72  n the cache, sor
10999 74 65 64 20 62 79 20 70 61 67 65 20 6e 75 6d 62  ted by page numb
1099a 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
1099b 49 56 41 54 45 20 50 67 48 64 72 20 2a 73 71 6c  IVATE PgHdr *sql
1099c 69 74 65 33 50 63 61 63 68 65 44 69 72 74 79 4c  ite3PcacheDirtyL
1099d 69 73 74 28 50 43 61 63 68 65 20 2a 70 43 61 63  ist(PCache *pCac
1099e 68 65 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b  he){.  PgHdr *p;
1099f 0a 20 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d  .  for(p=pCache-
109a0 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 2d  >pDirty; p; p=p-
109a1 3e 70 44 69 72 74 79 4e 65 78 74 29 7b 0a 20 20  >pDirtyNext){.  
109a2 20 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 2d    p->pDirty = p-
109a3 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d  >pDirtyNext;.  }
109a4 0a 20 20 72 65 74 75 72 6e 20 70 63 61 63 68 65  .  return pcache
109a5 53 6f 72 74 44 69 72 74 79 4c 69 73 74 28 70 43  SortDirtyList(pC
109a6 61 63 68 65 2d 3e 70 44 69 72 74 79 29 3b 0a 7d  ache->pDirty);.}
109a7 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20  ../* .** Return 
109a8 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
109a9 20 6f 66 20 72 65 66 65 72 65 6e 63 65 64 20 70   of referenced p
109aa 61 67 65 73 20 68 65 6c 64 20 62 79 20 74 68 65  ages held by the
109ab 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54   cache..*/.SQLIT
109ac 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
109ad 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f  lite3PcacheRefCo
109ae 75 6e 74 28 50 43 61 63 68 65 20 2a 70 43 61 63  unt(PCache *pCac
109af 68 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 43  he){.  return pC
109b0 61 63 68 65 2d 3e 6e 52 65 66 3b 0a 7d 0a 0a 2f  ache->nRef;.}../
109b1 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
109b2 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65  number of refere
109b3 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65  nces to the page
109b4 20 73 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20   supplied as an 
109b5 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  argument..*/.SQL
109b6 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
109b7 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67  sqlite3PcachePag
109b8 65 52 65 66 63 6f 75 6e 74 28 50 67 48 64 72 20  eRefcount(PgHdr 
109b9 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d  *p){.  return p-
109ba 3e 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  >nRef;.}../* .**
109bb 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61   Return the tota
109bc 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  l number of page
109bd 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a  s in the cache..
109be 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
109bf 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61  E int sqlite3Pca
109c0 63 68 65 50 61 67 65 63 6f 75 6e 74 28 50 43 61  chePagecount(PCa
109c1 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20  che *pCache){.  
109c2 69 6e 74 20 6e 50 61 67 65 20 3d 20 30 3b 0a 20  int nPage = 0;. 
109c3 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61   if( pCache->pCa
109c4 63 68 65 20 29 7b 0a 20 20 20 20 6e 50 61 67 65  che ){.    nPage
109c5 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
109c6 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 50  Config.pcache.xP
109c7 61 67 65 63 6f 75 6e 74 28 70 43 61 63 68 65 2d  agecount(pCache-
109c8 3e 70 43 61 63 68 65 29 3b 0a 20 20 7d 0a 20 20  >pCache);.  }.  
109c9 72 65 74 75 72 6e 20 6e 50 61 67 65 3b 0a 7d 0a  return nPage;.}.
109ca 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
109cb 45 53 54 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74 68  EST./*.** Get th
109cc 65 20 73 75 67 67 65 73 74 65 64 20 63 61 63 68  e suggested cach
109cd 65 2d 73 69 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f  e-size value..*/
109ce 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
109cf 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68  int sqlite3Pcach
109d0 65 47 65 74 43 61 63 68 65 73 69 7a 65 28 50 43  eGetCachesize(PC
109d1 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20  ache *pCache){. 
109d2 20 72 65 74 75 72 6e 20 70 43 61 63 68 65 2d 3e   return pCache->
109d3 6e 4d 61 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  nMax;.}.#endif..
109d4 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 75  /*.** Set the su
109d5 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73 69  ggested cache-si
109d6 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c  ze value..*/.SQL
109d7 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
109d8 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53 65   sqlite3PcacheSe
109d9 74 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 68  tCachesize(PCach
109da 65 20 2a 70 43 61 63 68 65 2c 20 69 6e 74 20 6d  e *pCache, int m
109db 78 50 61 67 65 29 7b 0a 20 20 70 43 61 63 68 65  xPage){.  pCache
109dc 2d 3e 6e 4d 61 78 20 3d 20 6d 78 50 61 67 65 3b  ->nMax = mxPage;
109dd 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70  .  if( pCache->p
109de 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c  Cache ){.    sql
109df 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
109e0 2e 70 63 61 63 68 65 2e 78 43 61 63 68 65 73 69  .pcache.xCachesi
109e1 7a 65 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68  ze(pCache->pCach
109e2 65 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20 7d 0a  e, mxPage);.  }.
109e3 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  }..#if defined(S
109e4 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45  QLITE_CHECK_PAGE
109e5 53 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  S) || defined(SQ
109e6 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a  LITE_DEBUG)./*.*
109e7 2a 20 46 6f 72 20 61 6c 6c 20 64 69 72 74 79 20  * For all dirty 
109e8 70 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20  pages currently 
109e9 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 69 6e  in the cache, in
109ea 76 6f 6b 65 20 74 68 65 20 73 70 65 63 69 66 69  voke the specifi
109eb 65 64 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 2e 20  ed.** callback. 
109ec 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65  This is only use
109ed 64 20 69 66 20 74 68 65 20 53 51 4c 49 54 45 5f  d if the SQLITE_
109ee 43 48 45 43 4b 5f 50 41 47 45 53 20 6d 61 63 72  CHECK_PAGES macr
109ef 6f 20 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 2e  o is.** defined.
109f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
109f1 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
109f2 63 61 63 68 65 49 74 65 72 61 74 65 44 69 72 74  cacheIterateDirt
109f3 79 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65  y(PCache *pCache
109f4 2c 20 76 6f 69 64 20 28 2a 78 49 74 65 72 29 28  , void (*xIter)(
109f5 50 67 48 64 72 20 2a 29 29 7b 0a 20 20 50 67 48  PgHdr *)){.  PgH
109f6 64 72 20 2a 70 44 69 72 74 79 3b 0a 20 20 66 6f  dr *pDirty;.  fo
109f7 72 28 70 44 69 72 74 79 3d 70 43 61 63 68 65 2d  r(pDirty=pCache-
109f8 3e 70 44 69 72 74 79 3b 20 70 44 69 72 74 79 3b  >pDirty; pDirty;
109f9 20 70 44 69 72 74 79 3d 70 44 69 72 74 79 2d 3e   pDirty=pDirty->
109fa 70 44 69 72 74 79 4e 65 78 74 29 7b 0a 20 20 20  pDirtyNext){.   
109fb 20 78 49 74 65 72 28 70 44 69 72 74 79 29 3b 0a   xIter(pDirty);.
109fc 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a    }.}.#endif../*
109fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
109fe 64 20 6f 66 20 70 63 61 63 68 65 2e 63 20 2a 2a  d of pcache.c **
109ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
10a02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
10a03 67 69 6e 20 66 69 6c 65 20 70 63 61 63 68 65 31  gin file pcache1
10a04 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
10a05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
10a07 0a 2a 2a 20 32 30 30 38 20 4e 6f 76 65 6d 62 65  .** 2008 Novembe
10a08 72 20 30 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 05.**.** The a
10a09 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
10a0a 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
10a0b 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
10a0c 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
10a0d 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
10a0e 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
10a0f 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
10a10 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
10a11 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
10a12 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
10a13 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
10a14 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
10a15 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
10a16 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
10a17 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
10a18 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
10a19 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
10a1a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a1b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a1e 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
10a1f 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  ile implements t
10a20 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 20  he default page 
10a21 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  cache implementa
10a22 74 69 6f 6e 20 28 74 68 65 0a 2a 2a 20 73 71 6c  tion (the.** sql
10a23 69 74 65 33 5f 70 63 61 63 68 65 20 69 6e 74 65  ite3_pcache inte
10a24 72 66 61 63 65 29 2e 20 49 74 20 61 6c 73 6f 20  rface). It also 
10a25 63 6f 6e 74 61 69 6e 73 20 70 61 72 74 20 6f 66  contains part of
10a26 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
10a27 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51  ion.** of the SQ
10a28 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45  LITE_CONFIG_PAGE
10a29 43 41 43 48 45 20 61 6e 64 20 73 71 6c 69 74 65  CACHE and sqlite
10a2a 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
10a2b 28 29 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a 20  () features..** 
10a2c 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 70  If the default p
10a2d 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d  age cache implem
10a2e 65 6e 74 61 74 69 6f 6e 20 69 73 20 6f 76 65 72  entation is over
10a2f 72 69 64 65 6e 2c 20 74 68 65 6e 20 6e 65 69 74  riden, then neit
10a30 68 65 72 20 6f 66 0a 2a 2a 20 74 68 65 73 65 20  her of.** these 
10a31 74 77 6f 20 66 65 61 74 75 72 65 73 20 61 72 65  two features are
10a32 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a   available..**.*
10a33 2a 20 40 28 23 29 20 24 49 64 3a 20 70 63 61 63  * @(#) $Id: pcac
10a34 68 65 31 2e 63 2c 76 20 31 2e 31 39 20 32 30 30  he1.c,v 1.19 200
10a35 39 2f 30 37 2f 31 37 20 31 31 3a 34 34 3a 30 37  9/07/17 11:44:07
10a36 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a   drh Exp $.*/...
10a37 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50  typedef struct P
10a38 43 61 63 68 65 31 20 50 43 61 63 68 65 31 3b 0a  Cache1 PCache1;.
10a39 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50  typedef struct P
10a3a 67 48 64 72 31 20 50 67 48 64 72 31 3b 0a 74 79  gHdr1 PgHdr1;.ty
10a3b 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 46  pedef struct PgF
10a3c 72 65 65 73 6c 6f 74 20 50 67 46 72 65 65 73 6c  reeslot PgFreesl
10a3d 6f 74 3b 0a 0a 2f 2a 20 50 6f 69 6e 74 65 72 73  ot;../* Pointers
10a3e 20 74 6f 20 73 74 72 75 63 74 75 72 65 73 20 6f   to structures o
10a3f 66 20 74 68 69 73 20 74 79 70 65 20 61 72 65 20  f this type are 
10a40 63 61 73 74 20 61 6e 64 20 72 65 74 75 72 6e 65  cast and returne
10a41 64 20 61 73 20 0a 2a 2a 20 6f 70 61 71 75 65 20  d as .** opaque 
10a42 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 20  sqlite3_pcache* 
10a43 68 61 6e 64 6c 65 73 0a 2a 2f 0a 73 74 72 75 63  handles.*/.struc
10a44 74 20 50 43 61 63 68 65 31 20 7b 0a 20 20 2f 2a  t PCache1 {.  /*
10a45 20 43 61 63 68 65 20 63 6f 6e 66 69 67 75 72 61   Cache configura
10a46 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 2e  tion parameters.
10a47 20 50 61 67 65 20 73 69 7a 65 20 28 73 7a 50 61   Page size (szPa
10a48 67 65 29 20 61 6e 64 20 74 68 65 20 70 75 72 67  ge) and the purg
10a49 65 61 62 6c 65 0a 20 20 2a 2a 20 66 6c 61 67 20  eable.  ** flag 
10a4a 28 62 50 75 72 67 65 61 62 6c 65 29 20 61 72 65  (bPurgeable) are
10a4b 20 73 65 74 20 77 68 65 6e 20 74 68 65 20 63 61   set when the ca
10a4c 63 68 65 20 69 73 20 63 72 65 61 74 65 64 2e 20  che is created. 
10a4d 6e 4d 61 78 20 6d 61 79 20 62 65 20 0a 20 20 2a  nMax may be .  *
10a4e 2a 20 6d 6f 64 69 66 69 65 64 20 61 74 20 61 6e  * modified at an
10a4f 79 20 74 69 6d 65 20 62 79 20 61 20 63 61 6c 6c  y time by a call
10a50 20 74 6f 20 74 68 65 20 70 63 61 63 68 65 31 43   to the pcache1C
10a51 61 63 68 65 53 69 7a 65 28 29 20 6d 65 74 68 6f  acheSize() metho
10a52 64 2e 0a 20 20 2a 2a 20 54 68 65 20 67 6c 6f 62  d..  ** The glob
10a53 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65  al mutex must be
10a54 20 68 65 6c 64 20 77 68 65 6e 20 61 63 63 65 73   held when acces
10a55 73 69 6e 67 20 6e 4d 61 78 2e 0a 20 20 2a 2f 0a  sing nMax..  */.
10a56 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20    int szPage;   
10a57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10a58 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
10a59 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 73   allocated pages
10a5a 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69   in bytes */.  i
10a5b 6e 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20  nt bPurgeable;  
10a5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10a5d 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 61     /* True if ca
10a5e 63 68 65 20 69 73 20 70 75 72 67 65 61 62 6c 65  che is purgeable
10a5f 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69   */.  unsigned i
10a60 6e 74 20 6e 4d 69 6e 3b 20 20 20 20 20 20 20 20  nt nMin;        
10a61 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e            /* Min
10a62 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70  imum number of p
10a63 61 67 65 73 20 72 65 73 65 72 76 65 64 20 2a 2f  ages reserved */
10a64 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
10a65 6e 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20 20  nMax;           
10a66 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67         /* Config
10a67 75 72 65 64 20 22 63 61 63 68 65 5f 73 69 7a 65  ured "cache_size
10a68 22 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a  " value */..  /*
10a69 20 48 61 73 68 20 74 61 62 6c 65 20 6f 66 20 61   Hash table of a
10a6a 6c 6c 20 70 61 67 65 73 2e 20 54 68 65 20 66 6f  ll pages. The fo
10a6b 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65  llowing variable
10a6c 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 61 63  s may only be ac
10a6d 63 65 73 73 65 64 0a 20 20 2a 2a 20 77 68 65 6e  cessed.  ** when
10a6e 20 74 68 65 20 61 63 63 65 73 73 6f 72 20 69 73   the accessor is
10a6f 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 67 6c 6f   holding the glo
10a70 62 61 6c 20 6d 75 74 65 78 20 28 73 65 65 20 70  bal mutex (see p
10a71 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78  cache1EnterMutex
10a72 28 29 20 0a 20 20 2a 2a 20 61 6e 64 20 70 63 61  () .  ** and pca
10a73 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29  che1LeaveMutex()
10a74 29 2e 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  )..  */.  unsign
10a75 65 64 20 69 6e 74 20 6e 52 65 63 79 63 6c 61 62  ed int nRecyclab
10a76 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  le;           /*
10a77 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
10a78 20 69 6e 20 74 68 65 20 4c 52 55 20 6c 69 73 74   in the LRU list
10a79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69   */.  unsigned i
10a7a 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20  nt nPage;       
10a7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74            /* Tot
10a7c 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  al number of pag
10a7d 65 73 20 69 6e 20 61 70 48 61 73 68 20 2a 2f 0a  es in apHash */.
10a7e 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e    unsigned int n
10a7f 48 61 73 68 3b 20 20 20 20 20 20 20 20 20 20 20  Hash;           
10a80 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
10a81 6f 66 20 73 6c 6f 74 73 20 69 6e 20 61 70 48 61  of slots in apHa
10a82 73 68 5b 5d 20 2a 2f 0a 20 20 50 67 48 64 72 31  sh[] */.  PgHdr1
10a83 20 2a 2a 61 70 48 61 73 68 3b 20 20 20 20 20 20   **apHash;      
10a84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10a85 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20   Hash table for 
10a86 66 61 73 74 20 6c 6f 6f 6b 75 70 20 62 79 20 6b  fast lookup by k
10a87 65 79 20 2a 2f 0a 0a 20 20 75 6e 73 69 67 6e 65  ey */..  unsigne
10a88 64 20 69 6e 74 20 69 4d 61 78 4b 65 79 3b 20 20  d int iMaxKey;  
10a89 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10a8a 4c 61 72 67 65 73 74 20 6b 65 79 20 73 65 65 6e  Largest key seen
10a8b 20 73 69 6e 63 65 20 78 54 72 75 6e 63 61 74 65   since xTruncate
10a8c 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  () */.};../*.** 
10a8d 45 61 63 68 20 63 61 63 68 65 20 65 6e 74 72 79  Each cache entry
10a8e 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20   is represented 
10a8f 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  by an instance o
10a90 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
10a91 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 41  .** structure. A
10a92 20 62 75 66 66 65 72 20 6f 66 20 50 67 48 64 72   buffer of PgHdr
10a93 31 2e 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65  1.pCache->szPage
10a94 20 62 79 74 65 73 20 69 73 20 61 6c 6c 6f 63 61   bytes is alloca
10a95 74 65 64 20 0a 2a 2a 20 64 69 72 65 63 74 6c 79  ted .** directly
10a96 20 62 65 66 6f 72 65 20 74 68 69 73 20 73 74 72   before this str
10a97 75 63 74 75 72 65 20 69 6e 20 6d 65 6d 6f 72 79  ucture in memory
10a98 20 28 73 65 65 20 74 68 65 20 50 47 48 44 52 31   (see the PGHDR1
10a99 5f 54 4f 5f 50 41 47 45 28 29 20 0a 2a 2a 20 6d  _TO_PAGE() .** m
10a9a 61 63 72 6f 20 62 65 6c 6f 77 29 2e 0a 2a 2f 0a  acro below)..*/.
10a9b 73 74 72 75 63 74 20 50 67 48 64 72 31 20 7b 0a  struct PgHdr1 {.
10a9c 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69    unsigned int i
10a9d 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  Key;            
10a9e 20 2f 2a 20 4b 65 79 20 76 61 6c 75 65 20 28 70   /* Key value (p
10a9f 61 67 65 20 6e 75 6d 62 65 72 29 20 2a 2f 0a 20  age number) */. 
10aa0 20 50 67 48 64 72 31 20 2a 70 4e 65 78 74 3b 20   PgHdr1 *pNext; 
10aa1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10aa2 2f 2a 20 4e 65 78 74 20 69 6e 20 68 61 73 68 20  /* Next in hash 
10aa3 74 61 62 6c 65 20 63 68 61 69 6e 20 2a 2f 0a 20  table chain */. 
10aa4 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65   PCache1 *pCache
10aa5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
10aa6 2f 2a 20 43 61 63 68 65 20 74 68 61 74 20 63 75  /* Cache that cu
10aa7 72 72 65 6e 74 6c 79 20 6f 77 6e 73 20 74 68 69  rrently owns thi
10aa8 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 48 64  s page */.  PgHd
10aa9 72 31 20 2a 70 4c 72 75 4e 65 78 74 3b 20 20 20  r1 *pLruNext;   
10aaa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
10aab 78 74 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f  xt in LRU list o
10aac 66 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73  f unpinned pages
10aad 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c   */.  PgHdr1 *pL
10aae 72 75 50 72 65 76 3b 20 20 20 20 20 20 20 20 20  ruPrev;         
10aaf 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73       /* Previous
10ab0 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20   in LRU list of 
10ab1 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a  unpinned pages *
10ab2 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  /.};../*.** Free
10ab3 20 73 6c 6f 74 73 20 69 6e 20 74 68 65 20 61 6c   slots in the al
10ab4 6c 6f 63 61 74 6f 72 20 75 73 65 64 20 74 6f 20  locator used to 
10ab5 64 69 76 69 64 65 20 75 70 20 74 68 65 20 62 75  divide up the bu
10ab6 66 66 65 72 20 70 72 6f 76 69 64 65 64 20 75 73  ffer provided us
10ab7 69 6e 67 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54  ing.** the SQLIT
10ab8 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43  E_CONFIG_PAGECAC
10ab9 48 45 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f  HE mechanism..*/
10aba 0a 73 74 72 75 63 74 20 50 67 46 72 65 65 73 6c  .struct PgFreesl
10abb 6f 74 20 7b 0a 20 20 50 67 46 72 65 65 73 6c 6f  ot {.  PgFreeslo
10abc 74 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65  t *pNext;  /* Ne
10abd 78 74 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a  xt free slot */.
10abe 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c  };../*.** Global
10abf 20 64 61 74 61 20 75 73 65 64 20 62 79 20 74 68   data used by th
10ac0 69 73 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61  is cache..*/.sta
10ac1 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73  tic SQLITE_WSD s
10ac2 74 72 75 63 74 20 50 43 61 63 68 65 47 6c 6f 62  truct PCacheGlob
10ac3 61 6c 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  al {.  sqlite3_m
10ac4 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20  utex *mutex;    
10ac5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74             /* st
10ac6 61 74 69 63 20 6d 75 74 65 78 20 4d 55 54 45 58  atic mutex MUTEX
10ac7 5f 53 54 41 54 49 43 5f 4c 52 55 20 2a 2f 0a 0a  _STATIC_LRU */..
10ac8 20 20 69 6e 74 20 6e 4d 61 78 50 61 67 65 3b 20    int nMaxPage; 
10ac9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10aca 20 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20        /* Sum of 
10acb 6e 4d 61 78 50 61 67 65 20 66 6f 72 20 70 75 72  nMaxPage for pur
10acc 67 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f  geable caches */
10acd 0a 20 20 69 6e 74 20 6e 4d 69 6e 50 61 67 65 3b  .  int nMinPage;
10ace 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10acf 20 20 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66         /* Sum of
10ad0 20 6e 4d 69 6e 50 61 67 65 20 66 6f 72 20 70 75   nMinPage for pu
10ad1 72 67 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a  rgeable caches *
10ad2 2f 0a 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74  /.  int nCurrent
10ad3 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
10ad4 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
10ad5 72 20 6f 66 20 70 75 72 67 65 61 62 6c 65 20 70  r of purgeable p
10ad6 61 67 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a  ages allocated *
10ad7 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75  /.  PgHdr1 *pLru
10ad8 48 65 61 64 2c 20 2a 70 4c 72 75 54 61 69 6c 3b  Head, *pLruTail;
10ad9 20 20 20 20 20 20 20 20 2f 2a 20 4c 52 55 20 6c          /* LRU l
10ada 69 73 74 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20  ist of unpinned 
10adb 70 61 67 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 56  pages */..  /* V
10adc 61 72 69 61 62 6c 65 73 20 72 65 6c 61 74 65 64  ariables related
10add 20 74 6f 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49   to SQLITE_CONFI
10ade 47 5f 50 41 47 45 43 41 43 48 45 20 73 65 74 74  G_PAGECACHE sett
10adf 69 6e 67 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73  ings. */.  int s
10ae0 7a 53 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 20  zSlot;          
10ae1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10ae2 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 66  * Size of each f
10ae3 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 76 6f  ree slot */.  vo
10ae4 69 64 20 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e  id *pStart, *pEn
10ae5 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
10ae6 20 20 2f 2a 20 42 6f 75 6e 64 73 20 6f 66 20 70    /* Bounds of p
10ae7 61 67 65 63 61 63 68 65 20 6d 61 6c 6c 6f 63 20  agecache malloc 
10ae8 72 61 6e 67 65 20 2a 2f 0a 20 20 50 67 46 72 65  range */.  PgFre
10ae9 65 73 6c 6f 74 20 2a 70 46 72 65 65 3b 20 20 20  eslot *pFree;   
10aea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10aeb 2a 20 46 72 65 65 20 70 61 67 65 20 62 6c 6f 63  * Free page bloc
10aec 6b 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e  ks */.  int isIn
10aed 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  it;             
10aee 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
10aef 72 75 65 20 69 66 20 69 6e 69 74 69 61 6c 69 7a  rue if initializ
10af0 65 64 20 2a 2f 0a 7d 20 70 63 61 63 68 65 31 5f  ed */.} pcache1_
10af1 67 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 6f  g;../*.** All co
10af2 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
10af3 73 68 6f 75 6c 64 20 61 63 63 65 73 73 20 74 68  should access th
10af4 65 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75  e global structu
10af5 72 65 20 61 62 6f 76 65 20 76 69 61 20 74 68 65  re above via the
10af6 0a 2a 2a 20 61 6c 69 61 73 20 22 70 63 61 63 68  .** alias "pcach
10af7 65 31 22 2e 20 54 68 69 73 20 65 6e 73 75 72 65  e1". This ensure
10af8 73 20 74 68 61 74 20 74 68 65 20 57 53 44 20 65  s that the WSD e
10af9 6d 75 6c 61 74 69 6f 6e 20 69 73 20 75 73 65 64  mulation is used
10afa 20 77 68 65 6e 0a 2a 2a 20 63 6f 6d 70 69 6c 69   when.** compili
10afb 6e 67 20 66 6f 72 20 73 79 73 74 65 6d 73 20 74  ng for systems t
10afc 68 61 74 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f  hat do not suppo
10afd 72 74 20 72 65 61 6c 20 57 53 44 2e 0a 2a 2f 0a  rt real WSD..*/.
10afe 23 64 65 66 69 6e 65 20 70 63 61 63 68 65 31 20  #define pcache1 
10aff 28 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 50  (GLOBAL(struct P
10b00 43 61 63 68 65 47 6c 6f 62 61 6c 2c 20 70 63 61  CacheGlobal, pca
10b01 63 68 65 31 5f 67 29 29 0a 0a 2f 2a 0a 2a 2a 20  che1_g))../*.** 
10b02 57 68 65 6e 20 61 20 50 67 48 64 72 31 20 73 74  When a PgHdr1 st
10b03 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63  ructure is alloc
10b04 61 74 65 64 2c 20 74 68 65 20 61 73 73 6f 63 69  ated, the associ
10b05 61 74 65 64 20 50 43 61 63 68 65 31 2e 73 7a 50  ated PCache1.szP
10b06 61 67 65 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20  age.** bytes of 
10b07 64 61 74 61 20 61 72 65 20 6c 6f 63 61 74 65 64  data are located
10b08 20 64 69 72 65 63 74 6c 79 20 62 65 66 6f 72 65   directly before
10b09 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79 20 28 69   it in memory (i
10b0a 2e 65 2e 20 74 68 65 20 74 6f 74 61 6c 0a 2a 2a  .e. the total.**
10b0b 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c   size of the all
10b0c 6f 63 61 74 69 6f 6e 20 69 73 20 73 69 7a 65 6f  ocation is sizeo
10b0d 66 28 50 67 48 64 72 31 29 2b 50 43 61 63 68 65  f(PgHdr1)+PCache
10b0e 31 2e 73 7a 50 61 67 65 20 62 79 74 65 29 2e 20  1.szPage byte). 
10b0f 54 68 65 0a 2a 2a 20 50 47 48 44 52 31 5f 54 4f  The.** PGHDR1_TO
10b10 5f 50 41 47 45 28 29 20 6d 61 63 72 6f 20 74 61  _PAGE() macro ta
10b11 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  kes a pointer to
10b12 20 61 20 50 67 48 64 72 31 20 73 74 72 75 63 74   a PgHdr1 struct
10b13 75 72 65 20 61 73 0a 2a 2a 20 61 6e 20 61 72 67  ure as.** an arg
10b14 75 6d 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e  ument and return
10b15 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
10b16 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 62 6c  he associated bl
10b17 6f 63 6b 20 6f 66 20 73 7a 50 61 67 65 0a 2a 2a  ock of szPage.**
10b18 20 62 79 74 65 73 2e 20 54 68 65 20 50 41 47 45   bytes. The PAGE
10b19 5f 54 4f 5f 50 47 48 44 52 31 28 29 20 6d 61 63  _TO_PGHDR1() mac
10b1a 72 6f 20 64 6f 65 73 20 74 68 65 20 6f 70 70 6f  ro does the oppo
10b1b 73 69 74 65 3a 20 69 74 73 20 61 72 67 75 6d 65  site: its argume
10b1c 6e 74 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e 74  nt is.** a point
10b1d 65 72 20 74 6f 20 61 20 62 6c 6f 63 6b 20 6f 66  er to a block of
10b1e 20 73 7a 50 61 67 65 20 62 79 74 65 73 20 6f 66   szPage bytes of
10b1f 20 64 61 74 61 20 61 6e 64 20 74 68 65 20 72 65   data and the re
10b20 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a  turn value is.**
10b21 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
10b22 65 20 61 73 73 6f 63 69 61 74 65 64 20 50 67 48  e associated PgH
10b23 64 72 31 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  dr1 structure..*
10b24 2a 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20 50  *.**   assert( P
10b25 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 50 41  GHDR1_TO_PAGE(PA
10b26 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 43 61  GE_TO_PGHDR1(pCa
10b27 63 68 65 2c 20 58 29 29 3d 3d 58 20 29 3b 0a 2a  che, X))==X );.*
10b28 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 31  /.#define PGHDR1
10b29 5f 54 4f 5f 50 41 47 45 28 70 29 20 20 20 20 28  _TO_PAGE(p)    (
10b2a 76 6f 69 64 2a 29 28 28 28 63 68 61 72 2a 29 70  void*)(((char*)p
10b2b 29 20 2d 20 70 2d 3e 70 43 61 63 68 65 2d 3e 73  ) - p->pCache->s
10b2c 7a 50 61 67 65 29 0a 23 64 65 66 69 6e 65 20 50  zPage).#define P
10b2d 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 63 2c  AGE_TO_PGHDR1(c,
10b2e 20 70 29 20 28 50 67 48 64 72 31 2a 29 28 28 28   p) (PgHdr1*)(((
10b2f 63 68 61 72 2a 29 70 29 20 2b 20 63 2d 3e 73 7a  char*)p) + c->sz
10b30 50 61 67 65 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63  Page)../*.** Mac
10b31 72 6f 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 64  ros to enter and
10b32 20 6c 65 61 76 65 20 74 68 65 20 67 6c 6f 62 61   leave the globa
10b33 6c 20 4c 52 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a  l LRU mutex..*/.
10b34 23 64 65 66 69 6e 65 20 70 63 61 63 68 65 31 45  #define pcache1E
10b35 6e 74 65 72 4d 75 74 65 78 28 29 20 73 71 6c 69  nterMutex() sqli
10b36 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
10b37 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 0a 23  pcache1.mutex).#
10b38 64 65 66 69 6e 65 20 70 63 61 63 68 65 31 4c 65  define pcache1Le
10b39 61 76 65 4d 75 74 65 78 28 29 20 73 71 6c 69 74  aveMutex() sqlit
10b3a 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
10b3b 63 61 63 68 65 31 2e 6d 75 74 65 78 29 0a 0a 2f  cache1.mutex)../
10b3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b3e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b3f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
10b41 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 50 61 67 65 20 41  /******** Page A
10b42 6c 6c 6f 63 61 74 69 6f 6e 2f 53 51 4c 49 54 45  llocation/SQLITE
10b43 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 52  _CONFIG_PCACHE R
10b44 65 6c 61 74 65 64 20 46 75 6e 63 74 69 6f 6e 73  elated Functions
10b45 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f   **************/
10b46 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
10b47 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
10b48 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a  during initializ
10b49 61 74 69 6f 6e 20 69 66 20 61 20 73 74 61 74 69  ation if a stati
10b4a 63 20 62 75 66 66 65 72 20 69 73 20 0a 2a 2a 20  c buffer is .** 
10b4b 73 75 70 70 6c 69 65 64 20 74 6f 20 75 73 65 20  supplied to use 
10b4c 66 6f 72 20 74 68 65 20 70 61 67 65 2d 63 61 63  for the page-cac
10b4d 68 65 20 62 79 20 70 61 73 73 69 6e 67 20 74 68  he by passing th
10b4e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
10b4f 50 41 47 45 43 41 43 48 45 0a 2a 2a 20 76 65 72  PAGECACHE.** ver
10b50 62 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e  b to sqlite3_con
10b51 66 69 67 28 29 2e 20 50 61 72 61 6d 65 74 65 72  fig(). Parameter
10b52 20 70 42 75 66 20 70 6f 69 6e 74 73 20 74 6f 20   pBuf points to 
10b53 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61  an allocation la
10b54 72 67 65 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f  rge.** enough to
10b55 20 63 6f 6e 74 61 69 6e 20 27 6e 27 20 62 75 66   contain 'n' buf
10b56 66 65 72 73 20 6f 66 20 27 73 7a 27 20 62 79 74  fers of 'sz' byt
10b57 65 73 20 65 61 63 68 2e 0a 2a 2f 0a 53 51 4c 49  es each..*/.SQLI
10b58 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
10b59 73 71 6c 69 74 65 33 50 43 61 63 68 65 42 75 66  sqlite3PCacheBuf
10b5a 66 65 72 53 65 74 75 70 28 76 6f 69 64 20 2a 70  ferSetup(void *p
10b5b 42 75 66 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74  Buf, int sz, int
10b5c 20 6e 29 7b 0a 20 20 69 66 28 20 70 63 61 63 68   n){.  if( pcach
10b5d 65 31 2e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20  e1.isInit ){.   
10b5e 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 3b 0a   PgFreeslot *p;.
10b5f 20 20 20 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f      sz = ROUNDDO
10b60 57 4e 38 28 73 7a 29 3b 0a 20 20 20 20 70 63 61  WN8(sz);.    pca
10b61 63 68 65 31 2e 73 7a 53 6c 6f 74 20 3d 20 73 7a  che1.szSlot = sz
10b62 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 70 53  ;.    pcache1.pS
10b63 74 61 72 74 20 3d 20 70 42 75 66 3b 0a 20 20 20  tart = pBuf;.   
10b64 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d   pcache1.pFree =
10b65 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e   0;.    while( n
10b66 2d 2d 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20  -- ){.      p = 
10b67 28 50 67 46 72 65 65 73 6c 6f 74 2a 29 70 42 75  (PgFreeslot*)pBu
10b68 66 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78  f;.      p->pNex
10b69 74 20 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65  t = pcache1.pFre
10b6a 65 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31  e;.      pcache1
10b6b 2e 70 46 72 65 65 20 3d 20 70 3b 0a 20 20 20 20  .pFree = p;.    
10b6c 20 20 70 42 75 66 20 3d 20 28 76 6f 69 64 2a 29    pBuf = (void*)
10b6d 26 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 73  &((char*)pBuf)[s
10b6e 7a 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 63  z];.    }.    pc
10b6f 61 63 68 65 31 2e 70 45 6e 64 20 3d 20 70 42 75  ache1.pEnd = pBu
10b70 66 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  f;.  }.}../*.** 
10b71 4d 61 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e 20  Malloc function 
10b72 75 73 65 64 20 77 69 74 68 69 6e 20 74 68 69 73  used within this
10b73 20 66 69 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74   file to allocat
10b74 65 20 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65  e space from the
10b75 20 62 75 66 66 65 72 0a 2a 2a 20 63 6f 6e 66 69   buffer.** confi
10b76 67 75 72 65 64 20 75 73 69 6e 67 20 73 71 6c 69  gured using sqli
10b77 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54  te3_config(SQLIT
10b78 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43  E_CONFIG_PAGECAC
10b79 48 45 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e  HE) option. If n
10b7a 6f 20 0a 2a 2a 20 73 75 63 68 20 62 75 66 66 65  o .** such buffe
10b7b 72 20 65 78 69 73 74 73 20 6f 72 20 74 68 65 72  r exists or ther
10b7c 65 20 69 73 20 6e 6f 20 73 70 61 63 65 20 6c 65  e is no space le
10b7d 66 74 20 69 6e 20 69 74 2c 20 74 68 69 73 20 66  ft in it, this f
10b7e 75 6e 63 74 69 6f 6e 20 66 61 6c 6c 73 20 0a 2a  unction falls .*
10b7f 2a 20 62 61 63 6b 20 74 6f 20 73 71 6c 69 74 65  * back to sqlite
10b80 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74  3Malloc()..*/.st
10b81 61 74 69 63 20 76 6f 69 64 20 2a 70 63 61 63 68  atic void *pcach
10b82 65 31 41 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74  e1Alloc(int nByt
10b83 65 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20  e){.  void *p;. 
10b84 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
10b85 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63  _mutex_held(pcac
10b86 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20  he1.mutex) );.  
10b87 69 66 28 20 6e 42 79 74 65 3c 3d 70 63 61 63 68  if( nByte<=pcach
10b88 65 31 2e 73 7a 53 6c 6f 74 20 26 26 20 70 63 61  e1.szSlot && pca
10b89 63 68 65 31 2e 70 46 72 65 65 20 29 7b 0a 20 20  che1.pFree ){.  
10b8a 20 20 61 73 73 65 72 74 28 20 70 63 61 63 68 65    assert( pcache
10b8b 31 2e 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20  1.isInit );.    
10b8c 70 20 3d 20 28 50 67 48 64 72 31 20 2a 29 70 63  p = (PgHdr1 *)pc
10b8d 61 63 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 20  ache1.pFree;.   
10b8e 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d   pcache1.pFree =
10b8f 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 2d 3e   pcache1.pFree->
10b90 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74  pNext;.    sqlit
10b91 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49  e3StatusSet(SQLI
10b92 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41  TE_STATUS_PAGECA
10b93 43 48 45 5f 53 49 5a 45 2c 20 6e 42 79 74 65 29  CHE_SIZE, nByte)
10b94 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61  ;.    sqlite3Sta
10b95 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
10b96 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55  ATUS_PAGECACHE_U
10b97 53 45 44 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65  SED, 1);.  }else
10b98 7b 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61  {..    /* Alloca
10b99 74 65 20 61 20 6e 65 77 20 62 75 66 66 65 72 20  te a new buffer 
10b9a 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4d 61 6c  using sqlite3Mal
10b9b 6c 6f 63 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e  loc. Before doin
10b9c 67 20 73 6f 2c 20 65 78 69 74 20 74 68 65 0a 20  g so, exit the. 
10b9d 20 20 20 2a 2a 20 67 6c 6f 62 61 6c 20 70 63 61     ** global pca
10b9e 63 68 65 20 6d 75 74 65 78 20 61 6e 64 20 75 6e  che mutex and un
10b9f 6c 6f 63 6b 20 74 68 65 20 70 61 67 65 72 2d 63  lock the pager-c
10ba0 61 63 68 65 20 6f 62 6a 65 63 74 20 70 43 61 63  ache object pCac
10ba1 68 65 2e 20 54 68 69 73 20 69 73 20 0a 20 20 20  he. This is .   
10ba2 20 2a 2a 20 73 6f 20 74 68 61 74 20 69 66 20 74   ** so that if t
10ba3 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c  he attempt to al
10ba4 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 62 75 66  locate a new buf
10ba5 66 65 72 20 63 61 75 73 65 73 20 74 68 65 20 74  fer causes the t
10ba6 68 65 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 66 69  he .    ** confi
10ba7 67 75 72 65 64 20 73 6f 66 74 2d 68 65 61 70 2d  gured soft-heap-
10ba8 6c 69 6d 69 74 20 74 6f 20 62 65 20 62 72 65 61  limit to be brea
10ba9 63 68 65 64 2c 20 69 74 20 77 69 6c 6c 20 62 65  ched, it will be
10baa 20 70 6f 73 73 69 62 6c 65 20 74 6f 0a 20 20 20   possible to.   
10bab 20 2a 2a 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f   ** reclaim memo
10bac 72 79 20 66 72 6f 6d 20 74 68 69 73 20 70 61 67  ry from this pag
10bad 65 72 2d 63 61 63 68 65 2e 0a 20 20 20 20 2a 2f  er-cache..    */
10bae 0a 20 20 20 20 70 63 61 63 68 65 31 4c 65 61 76  .    pcache1Leav
10baf 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 70 20  eMutex();.    p 
10bb0 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  = sqlite3Malloc(
10bb1 6e 42 79 74 65 29 3b 0a 20 20 20 20 70 63 61 63  nByte);.    pcac
10bb2 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  he1EnterMutex();
10bb3 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20  .    if( p ){.  
10bb4 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 73 71 6c      int sz = sql
10bb5 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70  ite3MallocSize(p
10bb6 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
10bb7 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45  StatusAdd(SQLITE
10bb8 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48  _STATUS_PAGECACH
10bb9 45 5f 4f 56 45 52 46 4c 4f 57 2c 20 73 7a 29 3b  E_OVERFLOW, sz);
10bba 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
10bbb 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
10bbc 46 72 65 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65  Free an allocate
10bbd 64 20 62 75 66 66 65 72 20 6f 62 74 61 69 6e 65  d buffer obtaine
10bbe 64 20 66 72 6f 6d 20 70 63 61 63 68 65 31 41 6c  d from pcache1Al
10bbf 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  loc()..*/.static
10bc0 20 76 6f 69 64 20 70 63 61 63 68 65 31 46 72 65   void pcache1Fre
10bc1 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73  e(void *p){.  as
10bc2 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
10bc3 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31  tex_held(pcache1
10bc4 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28  .mutex) );.  if(
10bc5 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   p==0 ) return;.
10bc6 20 20 69 66 28 20 70 3e 3d 70 63 61 63 68 65 31    if( p>=pcache1
10bc7 2e 70 53 74 61 72 74 20 26 26 20 70 3c 70 63 61  .pStart && p<pca
10bc8 63 68 65 31 2e 70 45 6e 64 20 29 7b 0a 20 20 20  che1.pEnd ){.   
10bc9 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 53 6c   PgFreeslot *pSl
10bca 6f 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  ot;.    sqlite3S
10bcb 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f  tatusAdd(SQLITE_
10bcc 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45  STATUS_PAGECACHE
10bcd 5f 55 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20 20  _USED, -1);.    
10bce 70 53 6c 6f 74 20 3d 20 28 50 67 46 72 65 65 73  pSlot = (PgFrees
10bcf 6c 6f 74 2a 29 70 3b 0a 20 20 20 20 70 53 6c 6f  lot*)p;.    pSlo
10bd0 74 2d 3e 70 4e 65 78 74 20 3d 20 70 63 61 63 68  t->pNext = pcach
10bd1 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63  e1.pFree;.    pc
10bd2 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70 53  ache1.pFree = pS
10bd3 6c 6f 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  lot;.  }else{.  
10bd4 20 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71    int iSize = sq
10bd5 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28  lite3MallocSize(
10bd6 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  p);.    sqlite3S
10bd7 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f  tatusAdd(SQLITE_
10bd8 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45  STATUS_PAGECACHE
10bd9 5f 4f 56 45 52 46 4c 4f 57 2c 20 2d 69 53 69 7a  _OVERFLOW, -iSiz
10bda 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  e);.    sqlite3_
10bdb 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  free(p);.  }.}..
10bdc 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  /*.** Allocate a
10bdd 20 6e 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74   new page object
10bde 20 69 6e 69 74 69 61 6c 6c 79 20 61 73 73 6f 63   initially assoc
10bdf 69 61 74 65 64 20 77 69 74 68 20 63 61 63 68 65  iated with cache
10be0 20 70 43 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74   pCache..*/.stat
10be1 69 63 20 50 67 48 64 72 31 20 2a 70 63 61 63 68  ic PgHdr1 *pcach
10be2 65 31 41 6c 6c 6f 63 50 61 67 65 28 50 43 61 63  e1AllocPage(PCac
10be3 68 65 31 20 2a 70 43 61 63 68 65 29 7b 0a 20 20  he1 *pCache){.  
10be4 69 6e 74 20 6e 42 79 74 65 20 3d 20 73 69 7a 65  int nByte = size
10be5 6f 66 28 50 67 48 64 72 31 29 20 2b 20 70 43 61  of(PgHdr1) + pCa
10be6 63 68 65 2d 3e 73 7a 50 61 67 65 3b 0a 20 20 76  che->szPage;.  v
10be7 6f 69 64 20 2a 70 50 67 20 3d 20 70 63 61 63 68  oid *pPg = pcach
10be8 65 31 41 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a  e1Alloc(nByte);.
10be9 20 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 69    PgHdr1 *p;.  i
10bea 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20 70 20  f( pPg ){.    p 
10beb 3d 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31  = PAGE_TO_PGHDR1
10bec 28 70 43 61 63 68 65 2c 20 70 50 67 29 3b 0a 20  (pCache, pPg);. 
10bed 20 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62     if( pCache->b
10bee 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20  Purgeable ){.   
10bef 20 20 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72     pcache1.nCurr
10bf0 65 6e 74 50 61 67 65 2b 2b 3b 0a 20 20 20 20 7d  entPage++;.    }
10bf1 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20  .  }else{.    p 
10bf2 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
10bf3 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72  n p;.}../*.** Fr
10bf4 65 65 20 61 20 70 61 67 65 20 6f 62 6a 65 63 74  ee a page object
10bf5 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 70 63   allocated by pc
10bf6 61 63 68 65 31 41 6c 6c 6f 63 50 61 67 65 28 29  ache1AllocPage()
10bf7 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e  ..**.** The poin
10bf8 74 65 72 20 69 73 20 61 6c 6c 6f 77 65 64 20 74  ter is allowed t
10bf9 6f 20 62 65 20 4e 55 4c 4c 2c 20 77 68 69 63 68  o be NULL, which
10bfa 20 69 73 20 70 72 75 64 65 6e 74 2e 20 20 42 75   is prudent.  Bu
10bfb 74 20 69 74 20 74 75 72 6e 73 20 6f 75 74 0a 2a  t it turns out.*
10bfc 2a 20 74 68 61 74 20 74 68 65 20 63 75 72 72 65  * that the curre
10bfd 6e 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  nt implementatio
10bfe 6e 20 68 61 70 70 65 6e 73 20 74 6f 20 6e 65 76  n happens to nev
10bff 65 72 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75  er call this rou
10c00 74 69 6e 65 0a 2a 2a 20 77 69 74 68 20 61 20 4e  tine.** with a N
10c01 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20  ULL pointer, so 
10c02 77 65 20 6d 61 72 6b 20 74 68 65 20 4e 55 4c 4c  we mark the NULL
10c03 20 74 65 73 74 20 77 69 74 68 20 41 4c 57 41 59   test with ALWAY
10c04 53 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  S()..*/.static v
10c05 6f 69 64 20 70 63 61 63 68 65 31 46 72 65 65 50  oid pcache1FreeP
10c06 61 67 65 28 50 67 48 64 72 31 20 2a 70 29 7b 0a  age(PgHdr1 *p){.
10c07 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 29 20    if( ALWAYS(p) 
10c08 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 43  ){.    if( p->pC
10c09 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65  ache->bPurgeable
10c0a 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65   ){.      pcache
10c0b 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 2d 2d  1.nCurrentPage--
10c0c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63  ;.    }.    pcac
10c0d 68 65 31 46 72 65 65 28 50 47 48 44 52 31 5f 54  he1Free(PGHDR1_T
10c0e 4f 5f 50 41 47 45 28 70 29 29 3b 0a 20 20 7d 0a  O_PAGE(p));.  }.
10c0f 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20  }../*.** Malloc 
10c10 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 62 79  function used by
10c11 20 53 51 4c 69 74 65 20 74 6f 20 6f 62 74 61 69   SQLite to obtai
10c12 6e 20 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65  n space from the
10c13 20 62 75 66 66 65 72 20 63 6f 6e 66 69 67 75 72   buffer configur
10c14 65 64 0a 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69  ed.** using sqli
10c15 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54  te3_config(SQLIT
10c16 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43  E_CONFIG_PAGECAC
10c17 48 45 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e  HE) option. If n
10c18 6f 20 73 75 63 68 20 62 75 66 66 65 72 0a 2a 2a  o such buffer.**
10c19 20 65 78 69 73 74 73 2c 20 74 68 69 73 20 66 75   exists, this fu
10c1a 6e 63 74 69 6f 6e 20 66 61 6c 6c 73 20 62 61 63  nction falls bac
10c1b 6b 20 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c  k to sqlite3Mall
10c1c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  oc()..*/.SQLITE_
10c1d 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
10c1e 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28  lite3PageMalloc(
10c1f 69 6e 74 20 73 7a 29 7b 0a 20 20 76 6f 69 64 20  int sz){.  void 
10c20 2a 70 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74  *p;.  pcache1Ent
10c21 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 20 3d  erMutex();.  p =
10c22 20 70 63 61 63 68 65 31 41 6c 6c 6f 63 28 73 7a   pcache1Alloc(sz
10c23 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76  );.  pcache1Leav
10c24 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75  eMutex();.  retu
10c25 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  rn p;.}../*.** F
10c26 72 65 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64  ree an allocated
10c27 20 62 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64   buffer obtained
10c28 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 50 61 67   from sqlite3Pag
10c29 65 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51  eMalloc()..*/.SQ
10c2a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
10c2b 64 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65  d sqlite3PageFre
10c2c 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 70 63  e(void *p){.  pc
10c2d 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28  ache1EnterMutex(
10c2e 29 3b 0a 20 20 70 63 61 63 68 65 31 46 72 65 65  );.  pcache1Free
10c2f 28 70 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65  (p);.  pcache1Le
10c30 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f  aveMutex();.}../
10c31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c32 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
10c36 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61  /******** Genera
10c37 6c 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  l Implementation
10c38 20 46 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a   Functions *****
10c39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
10c3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
10c3c 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f  ction is used to
10c3d 20 72 65 73 69 7a 65 20 74 68 65 20 68 61 73 68   resize the hash
10c3e 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 74   table used by t
10c3f 68 65 20 63 61 63 68 65 20 70 61 73 73 65 64 0a  he cache passed.
10c40 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ** as the first 
10c41 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
10c42 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78  The global mutex
10c43 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68   must be held wh
10c44 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
10c45 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73   is called..*/.s
10c46 74 61 74 69 63 20 69 6e 74 20 70 63 61 63 68 65  tatic int pcache
10c47 31 52 65 73 69 7a 65 48 61 73 68 28 50 43 61 63  1ResizeHash(PCac
10c48 68 65 31 20 2a 70 29 7b 0a 20 20 50 67 48 64 72  he1 *p){.  PgHdr
10c49 31 20 2a 2a 61 70 4e 65 77 3b 0a 20 20 75 6e 73  1 **apNew;.  uns
10c4a 69 67 6e 65 64 20 69 6e 74 20 6e 4e 65 77 3b 0a  igned int nNew;.
10c4b 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69    unsigned int i
10c4c 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
10c4d 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
10c4e 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29  pcache1.mutex) )
10c4f 3b 0a 0a 20 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e  ;..  nNew = p->n
10c50 48 61 73 68 2a 32 3b 0a 20 20 69 66 28 20 6e 4e  Hash*2;.  if( nN
10c51 65 77 3c 32 35 36 20 29 7b 0a 20 20 20 20 6e 4e  ew<256 ){.    nN
10c52 65 77 20 3d 20 32 35 36 3b 0a 20 20 7d 0a 0a 20  ew = 256;.  }.. 
10c53 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74   pcache1LeaveMut
10c54 65 78 28 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e  ex();.  if( p->n
10c55 48 61 73 68 20 29 7b 20 73 71 6c 69 74 65 33 42  Hash ){ sqlite3B
10c56 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
10c57 28 29 3b 20 7d 0a 20 20 61 70 4e 65 77 20 3d 20  (); }.  apNew = 
10c58 28 50 67 48 64 72 31 20 2a 2a 29 73 71 6c 69 74  (PgHdr1 **)sqlit
10c59 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66  e3_malloc(sizeof
10c5a 28 50 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29  (PgHdr1 *)*nNew)
10c5b 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 48 61 73 68  ;.  if( p->nHash
10c5c 20 29 7b 20 73 71 6c 69 74 65 33 45 6e 64 42 65   ){ sqlite3EndBe
10c5d 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a  nignMalloc(); }.
10c5e 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75    pcache1EnterMu
10c5f 74 65 78 28 29 3b 0a 20 20 69 66 28 20 61 70 4e  tex();.  if( apN
10c60 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74  ew ){.    memset
10c61 28 61 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f  (apNew, 0, sizeo
10c62 66 28 50 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77  f(PgHdr1 *)*nNew
10c63 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  );.    for(i=0; 
10c64 69 3c 70 2d 3e 6e 48 61 73 68 3b 20 69 2b 2b 29  i<p->nHash; i++)
10c65 7b 0a 20 20 20 20 20 20 50 67 48 64 72 31 20 2a  {.      PgHdr1 *
10c66 70 50 61 67 65 3b 0a 20 20 20 20 20 20 50 67 48  pPage;.      PgH
10c67 64 72 31 20 2a 70 4e 65 78 74 20 3d 20 70 2d 3e  dr1 *pNext = p->
10c68 61 70 48 61 73 68 5b 69 5d 3b 0a 20 20 20 20 20  apHash[i];.     
10c69 20 77 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d   while( (pPage =
10c6a 20 70 4e 65 78 74 29 21 3d 30 20 29 7b 0a 20 20   pNext)!=0 ){.  
10c6b 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69        unsigned i
10c6c 6e 74 20 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b  nt h = pPage->iK
10c6d 65 79 20 25 20 6e 4e 65 77 3b 0a 20 20 20 20 20  ey % nNew;.     
10c6e 20 20 20 70 4e 65 78 74 20 3d 20 70 50 61 67 65     pNext = pPage
10c6f 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20  ->pNext;.       
10c70 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20   pPage->pNext = 
10c71 61 70 4e 65 77 5b 68 5d 3b 0a 20 20 20 20 20 20  apNew[h];.      
10c72 20 20 61 70 4e 65 77 5b 68 5d 20 3d 20 70 50 61    apNew[h] = pPa
10c73 67 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ge;.      }.    
10c74 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  }.    sqlite3_fr
10c75 65 65 28 70 2d 3e 61 70 48 61 73 68 29 3b 0a 20  ee(p->apHash);. 
10c76 20 20 20 70 2d 3e 61 70 48 61 73 68 20 3d 20 61     p->apHash = a
10c77 70 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 48 61  pNew;.    p->nHa
10c78 73 68 20 3d 20 6e 4e 65 77 3b 0a 20 20 7d 0a 0a  sh = nNew;.  }..
10c79 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 61 70 48    return (p->apH
10c7a 61 73 68 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20  ash ? SQLITE_OK 
10c7b 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b  : SQLITE_NOMEM);
10c7c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
10c7d 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
10c7e 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20 72 65  internally to re
10c7f 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 70 50  move the page pP
10c80 61 67 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a  age from the .**
10c81 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74   global LRU list
10c82 2c 20 69 66 20 69 73 20 70 61 72 74 20 6f 66 20  , if is part of 
10c83 69 74 2e 20 49 66 20 70 50 61 67 65 20 69 73 20  it. If pPage is 
10c84 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 20  not part of the 
10c85 67 6c 6f 62 61 6c 0a 2a 2a 20 4c 52 55 20 6c 69  global.** LRU li
10c86 73 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75  st, then this fu
10c87 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f  nction is a no-o
10c88 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f  p..**.** The glo
10c89 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62  bal mutex must b
10c8a 65 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73  e held when this
10c8b 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
10c8c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  led..*/.static v
10c8d 6f 69 64 20 70 63 61 63 68 65 31 50 69 6e 50 61  oid pcache1PinPa
10c8e 67 65 28 50 67 48 64 72 31 20 2a 70 50 61 67 65  ge(PgHdr1 *pPage
10c8f 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
10c90 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
10c91 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29  pcache1.mutex) )
10c92 3b 0a 20 20 69 66 28 20 70 50 61 67 65 20 26 26  ;.  if( pPage &&
10c93 20 28 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78   (pPage->pLruNex
10c94 74 20 7c 7c 20 70 50 61 67 65 3d 3d 70 63 61 63  t || pPage==pcac
10c95 68 65 31 2e 70 4c 72 75 54 61 69 6c 29 20 29 7b  he1.pLruTail) ){
10c96 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e  .    if( pPage->
10c97 70 4c 72 75 50 72 65 76 20 29 7b 0a 20 20 20 20  pLruPrev ){.    
10c98 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65    pPage->pLruPre
10c99 76 2d 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 50  v->pLruNext = pP
10c9a 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20  age->pLruNext;. 
10c9b 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61     }.    if( pPa
10c9c 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 29 7b 0a  ge->pLruNext ){.
10c9d 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72        pPage->pLr
10c9e 75 4e 65 78 74 2d 3e 70 4c 72 75 50 72 65 76 20  uNext->pLruPrev 
10c9f 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65  = pPage->pLruPre
10ca0 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  v;.    }.    if(
10ca1 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61   pcache1.pLruHea
10ca2 64 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20  d==pPage ){.    
10ca3 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65    pcache1.pLruHe
10ca4 61 64 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75  ad = pPage->pLru
10ca5 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Next;.    }.    
10ca6 69 66 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75  if( pcache1.pLru
10ca7 54 61 69 6c 3d 3d 70 50 61 67 65 20 29 7b 0a 20  Tail==pPage ){. 
10ca8 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72       pcache1.pLr
10ca9 75 54 61 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70  uTail = pPage->p
10caa 4c 72 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20  LruPrev;.    }. 
10cab 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65     pPage->pLruNe
10cac 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67  xt = 0;.    pPag
10cad 65 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 30 3b  e->pLruPrev = 0;
10cae 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 43 61 63  .    pPage->pCac
10caf 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2d  he->nRecyclable-
10cb0 2d 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  -;.  }.}.../*.**
10cb1 20 52 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65   Remove the page
10cb2 20 73 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20   supplied as an 
10cb3 61 72 67 75 6d 65 6e 74 20 66 72 6f 6d 20 74 68  argument from th
10cb4 65 20 68 61 73 68 20 74 61 62 6c 65 20 0a 2a 2a  e hash table .**
10cb5 20 28 50 43 61 63 68 65 31 2e 61 70 48 61 73 68   (PCache1.apHash
10cb6 20 73 74 72 75 63 74 75 72 65 29 20 74 68 61 74   structure) that
10cb7 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79   it is currently
10cb8 20 73 74 6f 72 65 64 20 69 6e 2e 0a 2a 2a 0a 2a   stored in..**.*
10cb9 2a 20 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74  * The global mut
10cba 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20  ex must be held 
10cbb 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  when this functi
10cbc 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f  on is called..*/
10cbd 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61  .static void pca
10cbe 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61  che1RemoveFromHa
10cbf 73 68 28 50 67 48 64 72 31 20 2a 70 50 61 67 65  sh(PgHdr1 *pPage
10cc0 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  ){.  unsigned in
10cc1 74 20 68 3b 0a 20 20 50 43 61 63 68 65 31 20 2a  t h;.  PCache1 *
10cc2 70 43 61 63 68 65 20 3d 20 70 50 61 67 65 2d 3e  pCache = pPage->
10cc3 70 43 61 63 68 65 3b 0a 20 20 50 67 48 64 72 31  pCache;.  PgHdr1
10cc4 20 2a 2a 70 70 3b 0a 0a 20 20 68 20 3d 20 70 50   **pp;..  h = pP
10cc5 61 67 65 2d 3e 69 4b 65 79 20 25 20 70 43 61 63  age->iKey % pCac
10cc6 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20 66 6f 72  he->nHash;.  for
10cc7 28 70 70 3d 26 70 43 61 63 68 65 2d 3e 61 70 48  (pp=&pCache->apH
10cc8 61 73 68 5b 68 5d 3b 20 28 2a 70 70 29 21 3d 70  ash[h]; (*pp)!=p
10cc9 50 61 67 65 3b 20 70 70 3d 26 28 2a 70 70 29 2d  Page; pp=&(*pp)-
10cca 3e 70 4e 65 78 74 29 3b 0a 20 20 2a 70 70 20 3d  >pNext);.  *pp =
10ccb 20 28 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 0a   (*pp)->pNext;..
10ccc 20 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2d    pCache->nPage-
10ccd 2d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  -;.}../*.** If t
10cce 68 65 72 65 20 61 72 65 20 63 75 72 72 65 6e 74  here are current
10ccf 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20 70 63 61  ly more than pca
10cd0 63 68 65 2e 6e 4d 61 78 50 61 67 65 20 70 61 67  che.nMaxPage pag
10cd1 65 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 72  es allocated, tr
10cd2 79 0a 2a 2a 20 74 6f 20 72 65 63 79 63 6c 65 20  y.** to recycle 
10cd3 70 61 67 65 73 20 74 6f 20 72 65 64 75 63 65 20  pages to reduce 
10cd4 74 68 65 20 6e 75 6d 62 65 72 20 61 6c 6c 6f 63  the number alloc
10cd5 61 74 65 64 20 74 6f 20 70 63 61 63 68 65 2e 6e  ated to pcache.n
10cd6 4d 61 78 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74  MaxPage..*/.stat
10cd7 69 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 45  ic void pcache1E
10cd8 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 28 76 6f  nforceMaxPage(vo
10cd9 69 64 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  id){.  assert( s
10cda 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
10cdb 64 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29  d(pcache1.mutex)
10cdc 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 63 61   );.  while( pca
10cdd 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67  che1.nCurrentPag
10cde 65 3e 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61  e>pcache1.nMaxPa
10cdf 67 65 20 26 26 20 70 63 61 63 68 65 31 2e 70 4c  ge && pcache1.pL
10ce0 72 75 54 61 69 6c 20 29 7b 0a 20 20 20 20 50 67  ruTail ){.    Pg
10ce1 48 64 72 31 20 2a 70 20 3d 20 70 63 61 63 68 65  Hdr1 *p = pcache
10ce2 31 2e 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20  1.pLruTail;.    
10ce3 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 70  pcache1PinPage(p
10ce4 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 52 65  );.    pcache1Re
10ce5 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 29 3b  moveFromHash(p);
10ce6 0a 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65  .    pcache1Free
10ce7 50 61 67 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  Page(p);.  }.}..
10ce8 2f 2a 0a 2a 2a 20 44 69 73 63 61 72 64 20 61 6c  /*.** Discard al
10ce9 6c 20 70 61 67 65 73 20 66 72 6f 6d 20 63 61 63  l pages from cac
10cea 68 65 20 70 43 61 63 68 65 20 77 69 74 68 20 61  he pCache with a
10ceb 20 70 61 67 65 20 6e 75 6d 62 65 72 20 28 6b 65   page number (ke
10cec 79 20 76 61 6c 75 65 29 20 0a 2a 2a 20 67 72 65  y value) .** gre
10ced 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  ater than or equ
10cee 61 6c 20 74 6f 20 69 4c 69 6d 69 74 2e 20 41 6e  al to iLimit. An
10cef 79 20 70 69 6e 6e 65 64 20 70 61 67 65 73 20 74  y pinned pages t
10cf0 68 61 74 20 6d 65 65 74 20 74 68 69 73 20 0a 2a  hat meet this .*
10cf1 2a 20 63 72 69 74 65 72 69 61 20 61 72 65 20 75  * criteria are u
10cf2 6e 70 69 6e 6e 65 64 20 62 65 66 6f 72 65 20 74  npinned before t
10cf3 68 65 79 20 61 72 65 20 64 69 73 63 61 72 64 65  hey are discarde
10cf4 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f  d..**.** The glo
10cf5 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62  bal mutex must b
10cf6 65 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73  e held when this
10cf7 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
10cf8 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  led..*/.static v
10cf9 6f 69 64 20 70 63 61 63 68 65 31 54 72 75 6e 63  oid pcache1Trunc
10cfa 61 74 65 55 6e 73 61 66 65 28 0a 20 20 50 43 61  ateUnsafe(.  PCa
10cfb 63 68 65 31 20 2a 70 43 61 63 68 65 2c 20 0a 20  che1 *pCache, . 
10cfc 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c   unsigned int iL
10cfd 69 6d 69 74 20 0a 29 7b 0a 20 20 54 45 53 54 4f  imit .){.  TESTO
10cfe 4e 4c 59 28 20 75 6e 73 69 67 6e 65 64 20 69 6e  NLY( unsigned in
10cff 74 20 6e 50 61 67 65 20 3d 20 30 3b 20 29 20 20  t nPage = 0; )  
10d00 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 61      /* Used to a
10d01 73 73 65 72 74 20 70 43 61 63 68 65 2d 3e 6e 50  ssert pCache->nP
10d02 61 67 65 20 69 73 20 63 6f 72 72 65 63 74 20 2a  age is correct *
10d03 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  /.  unsigned int
10d04 20 68 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   h;.  assert( sq
10d05 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
10d06 28 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20  (pcache1.mutex) 
10d07 29 3b 0a 20 20 66 6f 72 28 68 3d 30 3b 20 68 3c  );.  for(h=0; h<
10d08 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 20 68  pCache->nHash; h
10d09 2b 2b 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20  ++){.    PgHdr1 
10d0a 2a 2a 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e  **pp = &pCache->
10d0b 61 70 48 61 73 68 5b 68 5d 3b 20 0a 20 20 20 20  apHash[h]; .    
10d0c 50 67 48 64 72 31 20 2a 70 50 61 67 65 3b 0a 20  PgHdr1 *pPage;. 
10d0d 20 20 20 77 68 69 6c 65 28 20 28 70 50 61 67 65     while( (pPage
10d0e 20 3d 20 2a 70 70 29 21 3d 30 20 29 7b 0a 20 20   = *pp)!=0 ){.  
10d0f 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69      if( pPage->i
10d10 4b 65 79 3e 3d 69 4c 69 6d 69 74 20 29 7b 0a 20  Key>=iLimit ){. 
10d11 20 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e         pCache->n
10d12 50 61 67 65 2d 2d 3b 0a 20 20 20 20 20 20 20 20  Page--;.        
10d13 2a 70 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65  *pp = pPage->pNe
10d14 78 74 3b 0a 20 20 20 20 20 20 20 20 70 63 61 63  xt;.        pcac
10d15 68 65 31 50 69 6e 50 61 67 65 28 70 50 61 67 65  he1PinPage(pPage
10d16 29 3b 0a 20 20 20 20 20 20 20 20 70 63 61 63 68  );.        pcach
10d17 65 31 46 72 65 65 50 61 67 65 28 70 50 61 67 65  e1FreePage(pPage
10d18 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
10d19 20 20 20 20 20 20 20 20 70 70 20 3d 20 26 70 50          pp = &pP
10d1a 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  age->pNext;.    
10d1b 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 6e 50      TESTONLY( nP
10d1c 61 67 65 2b 2b 3b 20 29 0a 20 20 20 20 20 20 7d  age++; ).      }
10d1d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73  .    }.  }.  ass
10d1e 65 72 74 28 20 70 43 61 63 68 65 2d 3e 6e 50 61  ert( pCache->nPa
10d1f 67 65 3d 3d 6e 50 61 67 65 20 29 3b 0a 7d 0a 0a  ge==nPage );.}..
10d20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
10d21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
10d25 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74  ./******** sqlit
10d26 65 33 5f 70 63 61 63 68 65 20 4d 65 74 68 6f 64  e3_pcache Method
10d27 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s **************
10d28 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  /../*.** Impleme
10d2b 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ntation of the s
10d2c 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 49  qlite3_pcache.xI
10d2d 6e 69 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  nit method..*/.s
10d2e 74 61 74 69 63 20 69 6e 74 20 70 63 61 63 68 65  tatic int pcache
10d2f 31 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55  1Init(void *NotU
10d30 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  sed){.  UNUSED_P
10d31 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
10d32 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 63 61  );.  assert( pca
10d33 63 68 65 31 2e 69 73 49 6e 69 74 3d 3d 30 20 29  che1.isInit==0 )
10d34 3b 0a 20 20 6d 65 6d 73 65 74 28 26 70 63 61 63  ;.  memset(&pcac
10d35 68 65 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70  he1, 0, sizeof(p
10d36 63 61 63 68 65 31 29 29 3b 0a 20 20 69 66 28 20  cache1));.  if( 
10d37 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
10d38 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29  fig.bCoreMutex )
10d39 7b 0a 20 20 20 20 70 63 61 63 68 65 31 2e 6d 75  {.    pcache1.mu
10d3a 74 65 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75  tex = sqlite3_mu
10d3b 74 65 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45  tex_alloc(SQLITE
10d3c 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52  _MUTEX_STATIC_LR
10d3d 55 29 3b 0a 20 20 7d 0a 20 20 70 63 61 63 68 65  U);.  }.  pcache
10d3e 31 2e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20  1.isInit = 1;.  
10d3f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
10d40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
10d41 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
10d42 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e   sqlite3_pcache.
10d43 78 53 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64  xShutdown method
10d44 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74  ..** Note that t
10d45 68 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 20  he static mutex 
10d46 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 78 49 6e  allocated in xIn
10d47 69 74 20 64 6f 65 73 20 0a 2a 2a 20 6e 6f 74 20  it does .** not 
10d48 6e 65 65 64 20 74 6f 20 62 65 20 66 72 65 65 64  need to be freed
10d49 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
10d4a 20 70 63 61 63 68 65 31 53 68 75 74 64 6f 77 6e   pcache1Shutdown
10d4b 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b  (void *NotUsed){
10d4c 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
10d4d 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
10d4e 61 73 73 65 72 74 28 20 70 63 61 63 68 65 31 2e  assert( pcache1.
10d4f 69 73 49 6e 69 74 21 3d 30 20 29 3b 0a 20 20 6d  isInit!=0 );.  m
10d50 65 6d 73 65 74 28 26 70 63 61 63 68 65 31 2c 20  emset(&pcache1, 
10d51 30 2c 20 73 69 7a 65 6f 66 28 70 63 61 63 68 65  0, sizeof(pcache
10d52 31 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  1));.}../*.** Im
10d53 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
10d54 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63  the sqlite3_pcac
10d55 68 65 2e 78 43 72 65 61 74 65 20 6d 65 74 68 6f  he.xCreate metho
10d56 64 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  d..**.** Allocat
10d57 65 20 61 20 6e 65 77 20 63 61 63 68 65 2e 0a 2a  e a new cache..*
10d58 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33  /.static sqlite3
10d59 5f 70 63 61 63 68 65 20 2a 70 63 61 63 68 65 31  _pcache *pcache1
10d5a 43 72 65 61 74 65 28 69 6e 74 20 73 7a 50 61 67  Create(int szPag
10d5b 65 2c 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c  e, int bPurgeabl
10d5c 65 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70  e){.  PCache1 *p
10d5d 43 61 63 68 65 3b 0a 0a 20 20 70 43 61 63 68 65  Cache;..  pCache
10d5e 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 73 71   = (PCache1 *)sq
10d5f 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
10d60 65 6f 66 28 50 43 61 63 68 65 31 29 29 3b 0a 20  eof(PCache1));. 
10d61 20 69 66 28 20 70 43 61 63 68 65 20 29 7b 0a 20   if( pCache ){. 
10d62 20 20 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65     memset(pCache
10d63 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 43 61 63  , 0, sizeof(PCac
10d64 68 65 31 29 29 3b 0a 20 20 20 20 70 43 61 63 68  he1));.    pCach
10d65 65 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61  e->szPage = szPa
10d66 67 65 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e  ge;.    pCache->
10d67 62 50 75 72 67 65 61 62 6c 65 20 3d 20 28 62 50  bPurgeable = (bP
10d68 75 72 67 65 61 62 6c 65 20 3f 20 31 20 3a 20 30  urgeable ? 1 : 0
10d69 29 3b 0a 20 20 20 20 69 66 28 20 62 50 75 72 67  );.    if( bPurg
10d6a 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70  eable ){.      p
10d6b 43 61 63 68 65 2d 3e 6e 4d 69 6e 20 3d 20 31 30  Cache->nMin = 10
10d6c 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 45  ;.      pcache1E
10d6d 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20  nterMutex();.   
10d6e 20 20 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50     pcache1.nMinP
10d6f 61 67 65 20 2b 3d 20 70 43 61 63 68 65 2d 3e 6e  age += pCache->n
10d70 4d 69 6e 3b 0a 20 20 20 20 20 20 70 63 61 63 68  Min;.      pcach
10d71 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  e1LeaveMutex();.
10d72 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
10d73 72 6e 20 28 73 71 6c 69 74 65 33 5f 70 63 61 63  rn (sqlite3_pcac
10d74 68 65 20 2a 29 70 43 61 63 68 65 3b 0a 7d 0a 0a  he *)pCache;.}..
10d75 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
10d76 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69  tion of the sqli
10d77 74 65 33 5f 70 63 61 63 68 65 2e 78 43 61 63 68  te3_pcache.xCach
10d78 65 73 69 7a 65 20 6d 65 74 68 6f 64 2e 20 0a 2a  esize method. .*
10d79 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 65 20 74  *.** Configure t
10d7a 68 65 20 63 61 63 68 65 5f 73 69 7a 65 20 6c 69  he cache_size li
10d7b 6d 69 74 20 66 6f 72 20 61 20 63 61 63 68 65 2e  mit for a cache.
10d7c 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
10d7d 70 63 61 63 68 65 31 43 61 63 68 65 73 69 7a 65  pcache1Cachesize
10d7e 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20  (sqlite3_pcache 
10d7f 2a 70 2c 20 69 6e 74 20 6e 4d 61 78 29 7b 0a 20  *p, int nMax){. 
10d80 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65   PCache1 *pCache
10d81 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b   = (PCache1 *)p;
10d82 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62  .  if( pCache->b
10d83 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20  Purgeable ){.   
10d84 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74   pcache1EnterMut
10d85 65 78 28 29 3b 0a 20 20 20 20 70 63 61 63 68 65  ex();.    pcache
10d86 31 2e 6e 4d 61 78 50 61 67 65 20 2b 3d 20 28 6e  1.nMaxPage += (n
10d87 4d 61 78 20 2d 20 70 43 61 63 68 65 2d 3e 6e 4d  Max - pCache->nM
10d88 61 78 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d  ax);.    pCache-
10d89 3e 6e 4d 61 78 20 3d 20 6e 4d 61 78 3b 0a 20 20  >nMax = nMax;.  
10d8a 20 20 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65    pcache1Enforce
10d8b 4d 61 78 50 61 67 65 28 29 3b 0a 20 20 20 20 70  MaxPage();.    p
10d8c 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78  cache1LeaveMutex
10d8d 28 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ();.  }.}../*.**
10d8e 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
10d8f 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70  of the sqlite3_p
10d90 63 61 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74  cache.xPagecount
10d91 20 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61   method. .*/.sta
10d92 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 50  tic int pcache1P
10d93 61 67 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33  agecount(sqlite3
10d94 5f 70 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 69  _pcache *p){.  i
10d95 6e 74 20 6e 3b 0a 20 20 70 63 61 63 68 65 31 45  nt n;.  pcache1E
10d96 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 6e  nterMutex();.  n
10d97 20 3d 20 28 28 50 43 61 63 68 65 31 20 2a 29 70   = ((PCache1 *)p
10d98 29 2d 3e 6e 50 61 67 65 3b 0a 20 20 70 63 61 63  )->nPage;.  pcac
10d99 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  he1LeaveMutex();
10d9a 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a  .  return n;.}..
10d9b 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
10d9c 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69  tion of the sqli
10d9d 74 65 33 5f 70 63 61 63 68 65 2e 78 46 65 74 63  te3_pcache.xFetc
10d9e 68 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a  h method. .**.**
10d9f 20 46 65 74 63 68 20 61 20 70 61 67 65 20 62 79   Fetch a page by
10da0 20 6b 65 79 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a   key value..**.*
10da1 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  * Whether or not
10da2 20 61 20 6e 65 77 20 70 61 67 65 20 6d 61 79 20   a new page may 
10da3 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  be allocated by 
10da4 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65  this function de
10da5 70 65 6e 64 73 20 6f 6e 0a 2a 2a 20 74 68 65 20  pends on.** the 
10da6 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 72 65  value of the cre
10da7 61 74 65 46 6c 61 67 20 61 72 67 75 6d 65 6e 74  ateFlag argument
10da8 2e 20 20 30 20 6d 65 61 6e 73 20 64 6f 20 6e 6f  .  0 means do no
10da9 74 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  t allocate a new
10daa 0a 2a 2a 20 70 61 67 65 2e 20 20 31 20 6d 65 61  .** page.  1 mea
10dab 6e 73 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65  ns allocate a ne
10dac 77 20 70 61 67 65 20 69 66 20 73 70 61 63 65 20  w page if space 
10dad 69 73 20 65 61 73 69 6c 79 20 61 76 61 69 6c 61  is easily availa
10dae 62 6c 65 2e 20 20 32 20 0a 2a 2a 20 6d 65 61 6e  ble.  2 .** mean
10daf 73 20 74 6f 20 74 72 79 20 72 65 61 6c 6c 79 20  s to try really 
10db0 68 61 72 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65  hard to allocate
10db1 20 61 20 6e 65 77 20 70 61 67 65 2e 0a 2a 2a 0a   a new page..**.
10db2 2a 2a 20 46 6f 72 20 61 20 6e 6f 6e 2d 70 75 72  ** For a non-pur
10db3 67 65 61 62 6c 65 20 63 61 63 68 65 20 28 61 20  geable cache (a 
10db4 63 61 63 68 65 20 75 73 65 64 20 61 73 20 74 68  cache used as th
10db5 65 20 73 74 6f 72 61 67 65 20 66 6f 72 20 61 6e  e storage for an
10db6 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61   in-memory.** da
10db7 74 61 62 61 73 65 29 20 74 68 65 72 65 20 69 73  tabase) there is
10db8 20 72 65 61 6c 6c 79 20 6e 6f 20 64 69 66 66 65   really no diffe
10db9 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 63 72  rence between cr
10dba 65 61 74 65 46 6c 61 67 20 31 20 61 6e 64 20 32  eateFlag 1 and 2
10dbb 2e 20 20 53 6f 0a 2a 2a 20 74 68 65 20 63 61 6c  .  So.** the cal
10dbc 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 28 70  ling function (p
10dbd 63 61 63 68 65 2e 63 29 20 77 69 6c 6c 20 6e 65  cache.c) will ne
10dbe 76 65 72 20 68 61 76 65 20 61 20 63 72 65 61 74  ver have a creat
10dbf 65 46 6c 61 67 20 6f 66 20 31 20 6f 6e 0a 2a 2a  eFlag of 1 on.**
10dc0 20 61 20 6e 6f 6e 2d 70 75 72 67 61 62 6c 65 20   a non-purgable 
10dc1 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  cache..**.** The
10dc2 72 65 20 61 72 65 20 74 68 72 65 65 20 64 69 66  re are three dif
10dc3 66 65 72 65 6e 74 20 61 70 70 72 6f 61 63 68 65  ferent approache
10dc4 73 20 74 6f 20 6f 62 74 61 69 6e 69 6e 67 20 73  s to obtaining s
10dc5 70 61 63 65 20 66 6f 72 20 61 20 70 61 67 65 2c  pace for a page,
10dc6 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  .** depending on
10dc7 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 70 61   the value of pa
10dc8 72 61 6d 65 74 65 72 20 63 72 65 61 74 65 46 6c  rameter createFl
10dc9 61 67 20 28 77 68 69 63 68 20 6d 61 79 20 62 65  ag (which may be
10dca 20 30 2c 20 31 20 6f 72 20 32 29 2e 0a 2a 2a 0a   0, 1 or 2)..**.
10dcb 2a 2a 20 20 20 31 2e 20 52 65 67 61 72 64 6c 65  **   1. Regardle
10dcc 73 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20  ss of the value 
10dcd 6f 66 20 63 72 65 61 74 65 46 6c 61 67 2c 20 74  of createFlag, t
10dce 68 65 20 63 61 63 68 65 20 69 73 20 73 65 61 72  he cache is sear
10dcf 63 68 65 64 20 66 6f 72 20 61 20 0a 2a 2a 20 20  ched for a .**  
10dd0 20 20 20 20 63 6f 70 79 20 6f 66 20 74 68 65 20      copy of the 
10dd1 72 65 71 75 65 73 74 65 64 20 70 61 67 65 2e 20  requested page. 
10dd2 49 66 20 6f 6e 65 20 69 73 20 66 6f 75 6e 64 2c  If one is found,
10dd3 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e   it is returned.
10dd4 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 49 66 20 63  .**.**   2. If c
10dd5 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 61 6e 64  reateFlag==0 and
10dd6 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
10dd7 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
10dd8 63 61 63 68 65 2c 20 4e 55 4c 4c 20 69 73 0a 2a  cache, NULL is.*
10dd9 2a 20 20 20 20 20 20 72 65 74 75 72 6e 65 64 2e  *      returned.
10dda 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 49 66 20 63  .**.**   3. If c
10ddb 72 65 61 74 65 46 6c 61 67 20 69 73 20 31 2c 20  reateFlag is 1, 
10ddc 61 6e 64 20 74 68 65 20 70 61 67 65 20 69 73 20  and the page is 
10ddd 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20 74  not already in t
10dde 68 65 20 63 61 63 68 65 2c 0a 2a 2a 20 20 20 20  he cache,.**    
10ddf 20 20 61 6e 64 20 69 66 20 65 69 74 68 65 72 20    and if either 
10de0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
10de1 20 61 72 65 20 74 72 75 65 2c 20 72 65 74 75 72   are true, retur
10de2 6e 20 4e 55 4c 4c 3a 0a 2a 2a 0a 2a 2a 20 20 20  n NULL:.**.**   
10de3 20 20 20 20 28 61 29 20 74 68 65 20 6e 75 6d 62      (a) the numb
10de4 65 72 20 6f 66 20 70 61 67 65 73 20 70 69 6e 6e  er of pages pinn
10de5 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65 20  ed by the cache 
10de6 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a  is greater than.
10de7 2a 2a 20 20 20 20 20 20 20 20 20 20 20 50 43 61  **           PCa
10de8 63 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a  che1.nMax, or.**
10de9 20 20 20 20 20 20 20 28 62 29 20 74 68 65 20 6e         (b) the n
10dea 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 70  umber of pages p
10deb 69 6e 6e 65 64 20 62 79 20 74 68 65 20 63 61 63  inned by the cac
10dec 68 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68  he is greater th
10ded 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  an.**           
10dee 74 68 65 20 73 75 6d 20 6f 66 20 6e 4d 61 78 20  the sum of nMax 
10def 66 6f 72 20 61 6c 6c 20 70 75 72 67 65 61 62 6c  for all purgeabl
10df0 65 20 63 61 63 68 65 73 2c 20 6c 65 73 73 20 74  e caches, less t
10df1 68 65 20 73 75 6d 20 6f 66 20 0a 2a 2a 20 20 20  he sum of .**   
10df2 20 20 20 20 20 20 20 20 6e 4d 69 6e 20 66 6f 72          nMin for
10df3 20 61 6c 6c 20 6f 74 68 65 72 20 70 75 72 67 65   all other purge
10df4 61 62 6c 65 20 63 61 63 68 65 73 2e 20 0a 2a 2a  able caches. .**
10df5 0a 2a 2a 20 20 20 34 2e 20 49 66 20 6e 6f 6e 65  .**   4. If none
10df6 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 74 68   of the first th
10df7 72 65 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61  ree conditions a
10df8 70 70 6c 79 20 61 6e 64 20 74 68 65 20 63 61 63  pply and the cac
10df9 68 65 20 69 73 20 6d 61 72 6b 65 64 0a 2a 2a 20  he is marked.** 
10dfa 20 20 20 20 20 61 73 20 70 75 72 67 65 61 62 6c       as purgeabl
10dfb 65 2c 20 61 6e 64 20 69 66 20 6f 6e 65 20 6f 66  e, and if one of
10dfc 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   the following i
10dfd 73 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  s true:.**.**   
10dfe 20 20 20 20 28 61 29 20 54 68 65 20 6e 75 6d 62      (a) The numb
10dff 65 72 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f  er of pages allo
10e00 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 63 61  cated for the ca
10e01 63 68 65 20 69 73 20 61 6c 72 65 61 64 79 20 0a  che is already .
10e02 2a 2a 20 20 20 20 20 20 20 20 20 20 20 50 43 61  **           PCa
10e03 63 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a  che1.nMax, or.**
10e04 0a 2a 2a 20 20 20 20 20 20 20 28 62 29 20 54 68  .**       (b) Th
10e05 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
10e06 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  s allocated for 
10e07 61 6c 6c 20 70 75 72 67 65 61 62 6c 65 20 63 61  all purgeable ca
10e08 63 68 65 73 20 69 73 0a 2a 2a 20 20 20 20 20 20  ches is.**      
10e09 20 20 20 20 20 61 6c 72 65 61 64 79 20 65 71 75       already equ
10e0a 61 6c 20 74 6f 20 6f 72 20 67 72 65 61 74 65 72  al to or greater
10e0b 20 74 68 61 6e 20 74 68 65 20 73 75 6d 20 6f 66   than the sum of
10e0c 20 6e 4d 61 78 20 66 6f 72 20 61 6c 6c 0a 2a 2a   nMax for all.**
10e0d 20 20 20 20 20 20 20 20 20 20 20 70 75 72 67 65             purge
10e0e 61 62 6c 65 20 63 61 63 68 65 73 2c 0a 2a 2a 0a  able caches,.**.
10e0f 2a 2a 20 20 20 20 20 20 74 68 65 6e 20 61 74 74  **      then att
10e10 65 6d 70 74 20 74 6f 20 72 65 63 79 63 6c 65 20  empt to recycle 
10e11 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  a page from the 
10e12 4c 52 55 20 6c 69 73 74 2e 20 49 66 20 69 74 20  LRU list. If it 
10e13 69 73 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 20  is the right.** 
10e14 20 20 20 20 20 73 69 7a 65 2c 20 72 65 74 75 72       size, retur
10e15 6e 20 74 68 65 20 72 65 63 79 63 6c 65 64 20 62  n the recycled b
10e16 75 66 66 65 72 2e 20 4f 74 68 65 72 77 69 73 65  uffer. Otherwise
10e17 2c 20 66 72 65 65 20 74 68 65 20 62 75 66 66 65  , free the buffe
10e18 72 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 70 72  r and.**      pr
10e19 6f 63 65 65 64 20 74 6f 20 73 74 65 70 20 35 2e  oceed to step 5.
10e1a 20 0a 2a 2a 0a 2a 2a 20 20 20 35 2e 20 4f 74 68   .**.**   5. Oth
10e1b 65 72 77 69 73 65 2c 20 61 6c 6c 6f 63 61 74 65  erwise, allocate
10e1c 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 6e 65   and return a ne
10e1d 77 20 70 61 67 65 20 62 75 66 66 65 72 2e 0a 2a  w page buffer..*
10e1e 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 70  /.static void *p
10e1f 63 61 63 68 65 31 46 65 74 63 68 28 73 71 6c 69  cache1Fetch(sqli
10e20 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 75  te3_pcache *p, u
10e21 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 79  nsigned int iKey
10e22 2c 20 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67  , int createFlag
10e23 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  ){.  unsigned in
10e24 74 20 6e 50 69 6e 6e 65 64 3b 0a 20 20 50 43 61  t nPinned;.  PCa
10e25 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28  che1 *pCache = (
10e26 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 50  PCache1 *)p;.  P
10e27 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 30  gHdr1 *pPage = 0
10e28 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 61  ;..  assert( pCa
10e29 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20  che->bPurgeable 
10e2a 7c 7c 20 63 72 65 61 74 65 46 6c 61 67 21 3d 31  || createFlag!=1
10e2b 20 29 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74   );.  pcache1Ent
10e2c 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28  erMutex();.  if(
10e2d 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 29   createFlag==1 )
10e2e 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e   sqlite3BeginBen
10e2f 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20  ignMalloc();..  
10e30 2f 2a 20 53 65 61 72 63 68 20 74 68 65 20 68 61  /* Search the ha
10e31 73 68 20 74 61 62 6c 65 20 66 6f 72 20 61 6e 20  sh table for an 
10e32 65 78 69 73 74 69 6e 67 20 65 6e 74 72 79 2e 20  existing entry. 
10e33 2a 2f 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d  */.  if( pCache-
10e34 3e 6e 48 61 73 68 3e 30 20 29 7b 0a 20 20 20 20  >nHash>0 ){.    
10e35 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d  unsigned int h =
10e36 20 69 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e   iKey % pCache->
10e37 6e 48 61 73 68 3b 0a 20 20 20 20 66 6f 72 28 70  nHash;.    for(p
10e38 50 61 67 65 3d 70 43 61 63 68 65 2d 3e 61 70 48  Page=pCache->apH
10e39 61 73 68 5b 68 5d 3b 20 70 50 61 67 65 26 26 70  ash[h]; pPage&&p
10e3a 50 61 67 65 2d 3e 69 4b 65 79 21 3d 69 4b 65 79  Page->iKey!=iKey
10e3b 3b 20 70 50 61 67 65 3d 70 50 61 67 65 2d 3e 70  ; pPage=pPage->p
10e3c 4e 65 78 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  Next);.  }..  if
10e3d 28 20 70 50 61 67 65 20 7c 7c 20 63 72 65 61 74  ( pPage || creat
10e3e 65 46 6c 61 67 3d 3d 30 20 29 7b 0a 20 20 20 20  eFlag==0 ){.    
10e3f 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 70  pcache1PinPage(p
10e40 50 61 67 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20  Page);.    goto 
10e41 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  fetch_out;.  }..
10e42 20 20 2f 2a 20 53 74 65 70 20 33 20 6f 66 20 68    /* Step 3 of h
10e43 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 2e 20 2a  eader comment. *
10e44 2f 0a 20 20 6e 50 69 6e 6e 65 64 20 3d 20 70 43  /.  nPinned = pC
10e45 61 63 68 65 2d 3e 6e 50 61 67 65 20 2d 20 70 43  ache->nPage - pC
10e46 61 63 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c  ache->nRecyclabl
10e47 65 3b 0a 20 20 69 66 28 20 63 72 65 61 74 65 46  e;.  if( createF
10e48 6c 61 67 3d 3d 31 20 26 26 20 28 0a 20 20 20 20  lag==1 && (.    
10e49 20 20 20 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 63      nPinned>=(pc
10e4a 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 2b 70  ache1.nMaxPage+p
10e4b 43 61 63 68 65 2d 3e 6e 4d 69 6e 2d 70 63 61 63  Cache->nMin-pcac
10e4c 68 65 31 2e 6e 4d 69 6e 50 61 67 65 29 0a 20 20  he1.nMinPage).  
10e4d 20 20 20 7c 7c 20 6e 50 69 6e 6e 65 64 3e 3d 28     || nPinned>=(
10e4e 70 43 61 63 68 65 2d 3e 6e 4d 61 78 20 2a 20 39  pCache->nMax * 9
10e4f 20 2f 20 31 30 29 0a 20 20 29 29 7b 0a 20 20 20   / 10).  )){.   
10e50 20 67 6f 74 6f 20 66 65 74 63 68 5f 6f 75 74 3b   goto fetch_out;
10e51 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 43 61 63  .  }..  if( pCac
10e52 68 65 2d 3e 6e 50 61 67 65 3e 3d 70 43 61 63 68  he->nPage>=pCach
10e53 65 2d 3e 6e 48 61 73 68 20 26 26 20 70 63 61 63  e->nHash && pcac
10e54 68 65 31 52 65 73 69 7a 65 48 61 73 68 28 70 43  he1ResizeHash(pC
10e55 61 63 68 65 29 20 29 7b 0a 20 20 20 20 67 6f 74  ache) ){.    got
10e56 6f 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d  o fetch_out;.  }
10e57 0a 0a 20 20 2f 2a 20 53 74 65 70 20 34 2e 20 54  ..  /* Step 4. T
10e58 72 79 20 74 6f 20 72 65 63 79 63 6c 65 20 61 20  ry to recycle a 
10e59 70 61 67 65 20 62 75 66 66 65 72 20 69 66 20 61  page buffer if a
10e5a 70 70 72 6f 70 72 69 61 74 65 2e 20 2a 2f 0a 20  ppropriate. */. 
10e5b 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75   if( pCache->bPu
10e5c 72 67 65 61 62 6c 65 20 26 26 20 70 63 61 63 68  rgeable && pcach
10e5d 65 31 2e 70 4c 72 75 54 61 69 6c 20 26 26 20 28  e1.pLruTail && (
10e5e 0a 20 20 20 20 20 28 70 43 61 63 68 65 2d 3e 6e  .     (pCache->n
10e5f 50 61 67 65 2b 31 3e 3d 70 43 61 63 68 65 2d 3e  Page+1>=pCache->
10e60 6e 4d 61 78 29 20 7c 7c 20 70 63 61 63 68 65 31  nMax) || pcache1
10e61 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e 3d 70  .nCurrentPage>=p
10e62 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 0a  cache1.nMaxPage.
10e63 20 20 29 29 7b 0a 20 20 20 20 70 50 61 67 65 20    )){.    pPage 
10e64 3d 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61  = pcache1.pLruTa
10e65 69 6c 3b 0a 20 20 20 20 70 63 61 63 68 65 31 52  il;.    pcache1R
10e66 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 50  emoveFromHash(pP
10e67 61 67 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65  age);.    pcache
10e68 31 50 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b  1PinPage(pPage);
10e69 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e  .    if( pPage->
10e6a 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 21 3d  pCache->szPage!=
10e6b 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 29  pCache->szPage )
10e6c 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 46  {.      pcache1F
10e6d 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  reePage(pPage);.
10e6e 20 20 20 20 20 20 70 50 61 67 65 20 3d 20 30 3b        pPage = 0;
10e6f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
10e70 20 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65    pcache1.nCurre
10e71 6e 74 50 61 67 65 20 2d 3d 20 28 70 50 61 67 65  ntPage -= (pPage
10e72 2d 3e 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65  ->pCache->bPurge
10e73 61 62 6c 65 20 2d 20 70 43 61 63 68 65 2d 3e 62  able - pCache->b
10e74 50 75 72 67 65 61 62 6c 65 29 3b 0a 20 20 20 20  Purgeable);.    
10e75 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70  }.  }..  /* Step
10e76 20 35 2e 20 49 66 20 61 20 75 73 61 62 6c 65 20   5. If a usable 
10e77 70 61 67 65 20 62 75 66 66 65 72 20 68 61 73 20  page buffer has 
10e78 73 74 69 6c 6c 20 6e 6f 74 20 62 65 65 6e 20 66  still not been f
10e79 6f 75 6e 64 2c 20 0a 20 20 2a 2a 20 61 74 74 65  ound, .  ** atte
10e7a 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  mpt to allocate 
10e7b 61 20 6e 65 77 20 6f 6e 65 2e 20 0a 20 20 2a 2f  a new one. .  */
10e7c 0a 20 20 69 66 28 20 21 70 50 61 67 65 20 29 7b  .  if( !pPage ){
10e7d 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 63 61  .    pPage = pca
10e7e 63 68 65 31 41 6c 6c 6f 63 50 61 67 65 28 70 43  che1AllocPage(pC
10e7f 61 63 68 65 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  ache);.  }..  if
10e80 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20 75  ( pPage ){.    u
10e81 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20  nsigned int h = 
10e82 69 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e 6e  iKey % pCache->n
10e83 48 61 73 68 3b 0a 20 20 20 20 70 43 61 63 68 65  Hash;.    pCache
10e84 2d 3e 6e 50 61 67 65 2b 2b 3b 0a 20 20 20 20 70  ->nPage++;.    p
10e85 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 69 4b 65  Page->iKey = iKe
10e86 79 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e  y;.    pPage->pN
10e87 65 78 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70  ext = pCache->ap
10e88 48 61 73 68 5b 68 5d 3b 0a 20 20 20 20 70 50 61  Hash[h];.    pPa
10e89 67 65 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61  ge->pCache = pCa
10e8a 63 68 65 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  che;.    pPage->
10e8b 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20  pLruPrev = 0;.  
10e8c 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78    pPage->pLruNex
10e8d 74 20 3d 20 30 3b 0a 20 20 20 20 2a 28 76 6f 69  t = 0;.    *(voi
10e8e 64 20 2a 2a 29 28 50 47 48 44 52 31 5f 54 4f 5f  d **)(PGHDR1_TO_
10e8f 50 41 47 45 28 70 50 61 67 65 29 29 20 3d 20 30  PAGE(pPage)) = 0
10e90 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 61 70  ;.    pCache->ap
10e91 48 61 73 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b  Hash[h] = pPage;
10e92 0a 20 20 7d 0a 0a 66 65 74 63 68 5f 6f 75 74 3a  .  }..fetch_out:
10e93 0a 20 20 69 66 28 20 70 50 61 67 65 20 26 26 20  .  if( pPage && 
10e94 69 4b 65 79 3e 70 43 61 63 68 65 2d 3e 69 4d 61  iKey>pCache->iMa
10e95 78 4b 65 79 20 29 7b 0a 20 20 20 20 70 43 61 63  xKey ){.    pCac
10e96 68 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4b  he->iMaxKey = iK
10e97 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 72  ey;.  }.  if( cr
10e98 65 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71  eateFlag==1 ) sq
10e99 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61  lite3EndBenignMa
10e9a 6c 6c 6f 63 28 29 3b 0a 20 20 70 63 61 63 68 65  lloc();.  pcache
10e9b 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  1LeaveMutex();. 
10e9c 20 72 65 74 75 72 6e 20 28 70 50 61 67 65 20 3f   return (pPage ?
10e9d 20 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28   PGHDR1_TO_PAGE(
10e9e 70 50 61 67 65 29 20 3a 20 30 29 3b 0a 7d 0a 0a  pPage) : 0);.}..
10e9f 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
10ea0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c  ation of the sql
10ea1 69 74 65 33 5f 70 63 61 63 68 65 2e 78 55 6e 70  ite3_pcache.xUnp
10ea2 69 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a  in method..**.**
10ea3 20 4d 61 72 6b 20 61 20 70 61 67 65 20 61 73 20   Mark a page as 
10ea4 75 6e 70 69 6e 6e 65 64 20 28 65 6c 69 67 69 62  unpinned (eligib
10ea5 6c 65 20 66 6f 72 20 61 73 79 6e 63 68 72 6f 6e  le for asynchron
10ea6 6f 75 73 20 72 65 63 79 63 6c 69 6e 67 29 2e 0a  ous recycling)..
10ea7 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
10ea8 63 61 63 68 65 31 55 6e 70 69 6e 28 73 71 6c 69  cache1Unpin(sqli
10ea9 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 76  te3_pcache *p, v
10eaa 6f 69 64 20 2a 70 50 67 2c 20 69 6e 74 20 72 65  oid *pPg, int re
10eab 75 73 65 55 6e 6c 69 6b 65 6c 79 29 7b 0a 20 20  useUnlikely){.  
10eac 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20  PCache1 *pCache 
10ead 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a  = (PCache1 *)p;.
10eae 20 20 50 67 48 64 72 31 20 2a 70 50 61 67 65 20    PgHdr1 *pPage 
10eaf 3d 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31  = PAGE_TO_PGHDR1
10eb0 28 70 43 61 63 68 65 2c 20 70 50 67 29 3b 0a 20  (pCache, pPg);. 
10eb1 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
10eb2 2d 3e 70 43 61 63 68 65 3d 3d 70 43 61 63 68 65  ->pCache==pCache
10eb3 20 29 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74   );.  pcache1Ent
10eb4 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a  erMutex();..  /*
10eb5 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20   It is an error 
10eb6 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e  to call this fun
10eb7 63 74 69 6f 6e 20 69 66 20 74 68 65 20 70 61 67  ction if the pag
10eb8 65 20 69 73 20 61 6c 72 65 61 64 79 20 0a 20 20  e is already .  
10eb9 2a 2a 20 70 61 72 74 20 6f 66 20 74 68 65 20 67  ** part of the g
10eba 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2e 0a  lobal LRU list..
10ebb 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
10ebc 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3d 3d  Page->pLruPrev==
10ebd 30 20 26 26 20 70 50 61 67 65 2d 3e 70 4c 72 75  0 && pPage->pLru
10ebe 4e 65 78 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73  Next==0 );.  ass
10ebf 65 72 74 28 20 70 63 61 63 68 65 31 2e 70 4c 72  ert( pcache1.pLr
10ec0 75 48 65 61 64 21 3d 70 50 61 67 65 20 26 26 20  uHead!=pPage && 
10ec1 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c  pcache1.pLruTail
10ec2 21 3d 70 50 61 67 65 20 29 3b 0a 0a 20 20 69 66  !=pPage );..  if
10ec3 28 20 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 20  ( reuseUnlikely 
10ec4 7c 7c 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72  || pcache1.nCurr
10ec5 65 6e 74 50 61 67 65 3e 70 63 61 63 68 65 31 2e  entPage>pcache1.
10ec6 6e 4d 61 78 50 61 67 65 20 29 7b 0a 20 20 20 20  nMaxPage ){.    
10ec7 70 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f  pcache1RemoveFro
10ec8 6d 48 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20  mHash(pPage);.  
10ec9 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67    pcache1FreePag
10eca 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73  e(pPage);.  }els
10ecb 65 7b 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68  e{.    /* Add th
10ecc 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 67 6c  e page to the gl
10ecd 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2e 20 4e  obal LRU list. N
10ece 6f 72 6d 61 6c 6c 79 2c 20 74 68 65 20 70 61 67  ormally, the pag
10ecf 65 20 69 73 20 61 64 64 65 64 20 74 6f 0a 20 20  e is added to.  
10ed0 20 20 2a 2a 20 74 68 65 20 68 65 61 64 20 6f 66    ** the head of
10ed1 20 74 68 65 20 6c 69 73 74 20 28 6c 61 73 74 20   the list (last 
10ed2 70 61 67 65 20 74 6f 20 62 65 20 72 65 63 79 63  page to be recyc
10ed3 6c 65 64 29 2e 20 48 6f 77 65 76 65 72 2c 20 69  led). However, i
10ed4 66 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 65  f the .    ** re
10ed5 75 73 65 55 6e 6c 69 6b 65 6c 79 20 66 6c 61 67  useUnlikely flag
10ed6 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20   passed to this 
10ed7 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 72 75 65  function is true
10ed8 2c 20 74 68 65 20 70 61 67 65 20 69 73 20 61 64  , the page is ad
10ed9 64 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68  ded.    ** to th
10eda 65 20 74 61 69 6c 20 6f 66 20 74 68 65 20 6c 69  e tail of the li
10edb 73 74 20 28 66 69 72 73 74 20 70 61 67 65 20 74  st (first page t
10edc 6f 20 62 65 20 72 65 63 79 63 6c 65 64 29 2e 0a  o be recycled)..
10edd 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
10ede 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20  cache1.pLruHead 
10edf 29 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31  ){.      pcache1
10ee0 2e 70 4c 72 75 48 65 61 64 2d 3e 70 4c 72 75 50  .pLruHead->pLruP
10ee1 72 65 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 20  rev = pPage;.   
10ee2 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65     pPage->pLruNe
10ee3 78 74 20 3d 20 70 63 61 63 68 65 31 2e 70 4c 72  xt = pcache1.pLr
10ee4 75 48 65 61 64 3b 0a 20 20 20 20 20 20 70 63 61  uHead;.      pca
10ee5 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20 3d 20  che1.pLruHead = 
10ee6 70 50 61 67 65 3b 0a 20 20 20 20 7d 65 6c 73 65  pPage;.    }else
10ee7 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e  {.      pcache1.
10ee8 70 4c 72 75 54 61 69 6c 20 3d 20 70 50 61 67 65  pLruTail = pPage
10ee9 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e  ;.      pcache1.
10eea 70 4c 72 75 48 65 61 64 20 3d 20 70 50 61 67 65  pLruHead = pPage
10eeb 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 61 63  ;.    }.    pCac
10eec 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2b  he->nRecyclable+
10eed 2b 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65  +;.  }..  pcache
10eee 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d  1LeaveMutex();.}
10eef 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
10ef0 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71  tation of the sq
10ef1 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 52 65  lite3_pcache.xRe
10ef2 6b 65 79 20 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a  key method. .*/.
10ef3 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63  static void pcac
10ef4 68 65 31 52 65 6b 65 79 28 0a 20 20 73 71 6c 69  he1Rekey(.  sqli
10ef5 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 0a 20  te3_pcache *p,. 
10ef6 20 76 6f 69 64 20 2a 70 50 67 2c 0a 20 20 75 6e   void *pPg,.  un
10ef7 73 69 67 6e 65 64 20 69 6e 74 20 69 4f 6c 64 2c  signed int iOld,
10ef8 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
10ef9 69 4e 65 77 0a 29 7b 0a 20 20 50 43 61 63 68 65  iNew.){.  PCache
10efa 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61  1 *pCache = (PCa
10efb 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64  che1 *)p;.  PgHd
10efc 72 31 20 2a 70 50 61 67 65 20 3d 20 50 41 47 45  r1 *pPage = PAGE
10efd 5f 54 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68  _TO_PGHDR1(pCach
10efe 65 2c 20 70 50 67 29 3b 0a 20 20 50 67 48 64 72  e, pPg);.  PgHdr
10eff 31 20 2a 2a 70 70 3b 0a 20 20 75 6e 73 69 67 6e  1 **pp;.  unsign
10f00 65 64 20 69 6e 74 20 68 3b 20 0a 20 20 61 73 73  ed int h; .  ass
10f01 65 72 74 28 20 70 50 61 67 65 2d 3e 69 4b 65 79  ert( pPage->iKey
10f02 3d 3d 69 4f 6c 64 20 29 3b 0a 20 20 61 73 73 65  ==iOld );.  asse
10f03 72 74 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68  rt( pPage->pCach
10f04 65 3d 3d 70 43 61 63 68 65 20 29 3b 0a 0a 20 20  e==pCache );..  
10f05 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65  pcache1EnterMute
10f06 78 28 29 3b 0a 0a 20 20 68 20 3d 20 69 4f 6c 64  x();..  h = iOld
10f07 25 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a  %pCache->nHash;.
10f08 20 20 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e    pp = &pCache->
10f09 61 70 48 61 73 68 5b 68 5d 3b 0a 20 20 77 68 69  apHash[h];.  whi
10f0a 6c 65 28 20 28 2a 70 70 29 21 3d 70 50 61 67 65  le( (*pp)!=pPage
10f0b 20 29 7b 0a 20 20 20 20 70 70 20 3d 20 26 28 2a   ){.    pp = &(*
10f0c 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a  pp)->pNext;.  }.
10f0d 20 20 2a 70 70 20 3d 20 70 50 61 67 65 2d 3e 70    *pp = pPage->p
10f0e 4e 65 78 74 3b 0a 0a 20 20 68 20 3d 20 69 4e 65  Next;..  h = iNe
10f0f 77 25 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b  w%pCache->nHash;
10f10 0a 20 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 3d  .  pPage->iKey =
10f11 20 69 4e 65 77 3b 0a 20 20 70 50 61 67 65 2d 3e   iNew;.  pPage->
10f12 70 4e 65 78 74 20 3d 20 70 43 61 63 68 65 2d 3e  pNext = pCache->
10f13 61 70 48 61 73 68 5b 68 5d 3b 0a 20 20 70 43 61  apHash[h];.  pCa
10f14 63 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 20 3d  che->apHash[h] =
10f15 20 70 50 61 67 65 3b 0a 0a 20 20 2f 2a 20 54 68   pPage;..  /* Th
10f16 65 20 78 52 65 6b 65 79 28 29 20 69 6e 74 65 72  e xRekey() inter
10f17 66 61 63 65 20 69 73 20 6f 6e 6c 79 20 75 73 65  face is only use
10f18 64 20 74 6f 20 6d 6f 76 65 20 70 61 67 65 73 20  d to move pages 
10f19 65 61 72 6c 69 65 72 20 69 6e 20 74 68 65 0a 20  earlier in the. 
10f1a 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c   ** database fil
10f1b 65 20 28 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d  e (in order to m
10f1c 6f 76 65 20 61 6c 6c 20 66 72 65 65 20 70 61 67  ove all free pag
10f1d 65 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  es to the end of
10f1e 20 74 68 65 0a 20 20 2a 2a 20 66 69 6c 65 20 77   the.  ** file w
10f1f 68 65 72 65 20 74 68 65 79 20 63 61 6e 20 62 65  here they can be
10f20 20 74 72 75 6e 63 61 74 65 64 20 6f 66 66 2e 29   truncated off.)
10f21 20 20 48 65 6e 63 65 2c 20 69 74 20 69 73 20 6e    Hence, it is n
10f22 6f 74 20 70 6f 73 73 69 62 6c 65 0a 20 20 2a 2a  ot possible.  **
10f23 20 66 6f 72 20 74 68 65 20 6e 65 77 20 70 61 67   for the new pag
10f24 65 20 6e 75 6d 62 65 72 20 74 6f 20 62 65 20 67  e number to be g
10f25 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20  reater than the 
10f26 6c 61 72 67 65 73 74 20 70 72 65 76 69 6f 75 73  largest previous
10f27 6c 79 0a 20 20 2a 2a 20 66 65 74 63 68 65 64 20  ly.  ** fetched 
10f28 70 61 67 65 2e 20 20 42 75 74 20 77 65 20 72 65  page.  But we re
10f29 74 61 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  tain the followi
10f2a 6e 67 20 74 65 73 74 20 69 6e 20 63 61 73 65 20  ng test in case 
10f2b 78 52 65 6b 65 79 28 29 0a 20 20 2a 2a 20 62 65  xRekey().  ** be
10f2c 67 69 6e 73 20 74 6f 20 62 65 20 75 73 65 64 20  gins to be used 
10f2d 69 6e 20 64 69 66 66 65 72 65 6e 74 20 77 61 79  in different way
10f2e 73 20 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e  s in the future.
10f2f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45  .  */.  if( NEVE
10f30 52 28 69 4e 65 77 3e 70 43 61 63 68 65 2d 3e 69  R(iNew>pCache->i
10f31 4d 61 78 4b 65 79 29 20 29 7b 0a 20 20 20 20 70  MaxKey) ){.    p
10f32 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 3d  Cache->iMaxKey =
10f33 20 69 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 70 63   iNew;.  }..  pc
10f34 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28  ache1LeaveMutex(
10f35 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  );.}../*.** Impl
10f36 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
10f37 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  e sqlite3_pcache
10f38 2e 78 54 72 75 6e 63 61 74 65 20 6d 65 74 68 6f  .xTruncate metho
10f39 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 69 73 63 61 72  d. .**.** Discar
10f3a 64 20 61 6c 6c 20 75 6e 70 69 6e 6e 65 64 20 70  d all unpinned p
10f3b 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68  ages in the cach
10f3c 65 20 77 69 74 68 20 61 20 70 61 67 65 20 6e 75  e with a page nu
10f3d 6d 62 65 72 20 65 71 75 61 6c 20 74 6f 0a 2a 2a  mber equal to.**
10f3e 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e   or greater than
10f3f 20 70 61 72 61 6d 65 74 65 72 20 69 4c 69 6d 69   parameter iLimi
10f40 74 2e 20 41 6e 79 20 70 69 6e 6e 65 64 20 70 61  t. Any pinned pa
10f41 67 65 73 20 77 69 74 68 20 61 20 70 61 67 65 20  ges with a page 
10f42 6e 75 6d 62 65 72 0a 2a 2a 20 65 71 75 61 6c 20  number.** equal 
10f43 74 6f 20 6f 72 20 67 72 65 61 74 65 72 20 74 68  to or greater th
10f44 61 6e 20 69 4c 69 6d 69 74 20 61 72 65 20 69 6d  an iLimit are im
10f45 70 6c 69 63 69 74 6c 79 20 75 6e 70 69 6e 6e 65  plicitly unpinne
10f46 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
10f47 64 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74  d pcache1Truncat
10f48 65 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  e(sqlite3_pcache
10f49 20 2a 70 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e   *p, unsigned in
10f4a 74 20 69 4c 69 6d 69 74 29 7b 0a 20 20 50 43 61  t iLimit){.  PCa
10f4b 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28  che1 *pCache = (
10f4c 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 70  PCache1 *)p;.  p
10f4d 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78  cache1EnterMutex
10f4e 28 29 3b 0a 20 20 69 66 28 20 69 4c 69 6d 69 74  ();.  if( iLimit
10f4f 3c 3d 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65  <=pCache->iMaxKe
10f50 79 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31  y ){.    pcache1
10f51 54 72 75 6e 63 61 74 65 55 6e 73 61 66 65 28 70  TruncateUnsafe(p
10f52 43 61 63 68 65 2c 20 69 4c 69 6d 69 74 29 3b 0a  Cache, iLimit);.
10f53 20 20 20 20 70 43 61 63 68 65 2d 3e 69 4d 61 78      pCache->iMax
10f54 4b 65 79 20 3d 20 69 4c 69 6d 69 74 2d 31 3b 0a  Key = iLimit-1;.
10f55 20 20 7d 0a 20 20 70 63 61 63 68 65 31 4c 65 61    }.  pcache1Lea
10f56 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a  veMutex();.}../*
10f57 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
10f58 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  on of the sqlite
10f59 33 5f 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f  3_pcache.xDestro
10f5a 79 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a  y method. .**.**
10f5b 20 44 65 73 74 72 6f 79 20 61 20 63 61 63 68 65   Destroy a cache
10f5c 20 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67   allocated using
10f5d 20 70 63 61 63 68 65 31 43 72 65 61 74 65 28 29   pcache1Create()
10f5e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
10f5f 20 70 63 61 63 68 65 31 44 65 73 74 72 6f 79 28   pcache1Destroy(
10f60 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a  sqlite3_pcache *
10f61 70 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70  p){.  PCache1 *p
10f62 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31  Cache = (PCache1
10f63 20 2a 29 70 3b 0a 20 20 70 63 61 63 68 65 31 45   *)p;.  pcache1E
10f64 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70  nterMutex();.  p
10f65 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 55 6e  cache1TruncateUn
10f66 73 61 66 65 28 70 43 61 63 68 65 2c 20 30 29 3b  safe(pCache, 0);
10f67 0a 20 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 50  .  pcache1.nMaxP
10f68 61 67 65 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e  age -= pCache->n
10f69 4d 61 78 3b 0a 20 20 70 63 61 63 68 65 31 2e 6e  Max;.  pcache1.n
10f6a 4d 69 6e 50 61 67 65 20 2d 3d 20 70 43 61 63 68  MinPage -= pCach
10f6b 65 2d 3e 6e 4d 69 6e 3b 0a 20 20 70 63 61 63 68  e->nMin;.  pcach
10f6c 65 31 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65  e1EnforceMaxPage
10f6d 28 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61  ();.  pcache1Lea
10f6e 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 73 71 6c  veMutex();.  sql
10f6f 69 74 65 33 5f 66 72 65 65 28 70 43 61 63 68 65  ite3_free(pCache
10f70 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 73 71 6c  ->apHash);.  sql
10f71 69 74 65 33 5f 66 72 65 65 28 70 43 61 63 68 65  ite3_free(pCache
10f72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
10f73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
10f74 6c 65 64 20 64 75 72 69 6e 67 20 69 6e 69 74 69  led during initi
10f75 61 6c 69 7a 61 74 69 6f 6e 20 28 73 71 6c 69 74  alization (sqlit
10f76 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 29  e3_initialize())
10f77 20 74 6f 0a 2a 2a 20 69 6e 73 74 61 6c 6c 20 74   to.** install t
10f78 68 65 20 64 65 66 61 75 6c 74 20 70 6c 75 67 67  he default plugg
10f79 61 62 6c 65 20 63 61 63 68 65 20 6d 6f 64 75 6c  able cache modul
10f7a 65 2c 20 61 73 73 75 6d 69 6e 67 20 74 68 65 20  e, assuming the 
10f7b 75 73 65 72 20 68 61 73 20 6e 6f 74 0a 2a 2a 20  user has not.** 
10f7c 61 6c 72 65 61 64 79 20 70 72 6f 76 69 64 65 64  already provided
10f7d 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 2e   an alternative.
10f7e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
10f7f 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
10f80 43 61 63 68 65 53 65 74 44 65 66 61 75 6c 74 28  CacheSetDefault(
10f81 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  void){.  static 
10f82 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d  sqlite3_pcache_m
10f83 65 74 68 6f 64 73 20 64 65 66 61 75 6c 74 4d 65  ethods defaultMe
10f84 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 30 2c  thods = {.    0,
10f85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10f86 20 20 20 20 20 20 20 2f 2a 20 70 41 72 67 20 2a         /* pArg *
10f87 2f 0a 20 20 20 20 70 63 61 63 68 65 31 49 6e 69  /.    pcache1Ini
10f88 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  t,             /
10f89 2a 20 78 49 6e 69 74 20 2a 2f 0a 20 20 20 20 70  * xInit */.    p
10f8a 63 61 63 68 65 31 53 68 75 74 64 6f 77 6e 2c 20  cache1Shutdown, 
10f8b 20 20 20 20 20 20 20 20 2f 2a 20 78 53 68 75 74          /* xShut
10f8c 64 6f 77 6e 20 2a 2f 0a 20 20 20 20 70 63 61 63  down */.    pcac
10f8d 68 65 31 43 72 65 61 74 65 2c 20 20 20 20 20 20  he1Create,      
10f8e 20 20 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20       /* xCreate 
10f8f 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 43 61  */.    pcache1Ca
10f90 63 68 65 73 69 7a 65 2c 20 20 20 20 20 20 20 20  chesize,        
10f91 2f 2a 20 78 43 61 63 68 65 73 69 7a 65 20 2a 2f  /* xCachesize */
10f92 0a 20 20 20 20 70 63 61 63 68 65 31 50 61 67 65  .    pcache1Page
10f93 63 6f 75 6e 74 2c 20 20 20 20 20 20 20 20 2f 2a  count,        /*
10f94 20 78 50 61 67 65 63 6f 75 6e 74 20 2a 2f 0a 20   xPagecount */. 
10f95 20 20 20 70 63 61 63 68 65 31 46 65 74 63 68 2c     pcache1Fetch,
10f96 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
10f97 46 65 74 63 68 20 2a 2f 0a 20 20 20 20 70 63 61  Fetch */.    pca
10f98 63 68 65 31 55 6e 70 69 6e 2c 20 20 20 20 20 20  che1Unpin,      
10f99 20 20 20 20 20 20 2f 2a 20 78 55 6e 70 69 6e 20        /* xUnpin 
10f9a 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 52 65  */.    pcache1Re
10f9b 6b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 20  key,            
10f9c 2f 2a 20 78 52 65 6b 65 79 20 2a 2f 0a 20 20 20  /* xRekey */.   
10f9d 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65   pcache1Truncate
10f9e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 54 72  ,         /* xTr
10f9f 75 6e 63 61 74 65 20 2a 2f 0a 20 20 20 20 70 63  uncate */.    pc
10fa0 61 63 68 65 31 44 65 73 74 72 6f 79 20 20 20 20  ache1Destroy    
10fa1 20 20 20 20 20 20 20 2f 2a 20 78 44 65 73 74 72         /* xDestr
10fa2 6f 79 20 2a 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c  oy */.  };.  sql
10fa3 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49  ite3_config(SQLI
10fa4 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45  TE_CONFIG_PCACHE
10fa5 2c 20 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64  , &defaultMethod
10fa6 73 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51  s);.}..#ifdef SQ
10fa7 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f  LITE_ENABLE_MEMO
10fa8 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a  RY_MANAGEMENT./*
10fa9 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
10faa 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 66  n is called to f
10fab 72 65 65 20 73 75 70 65 72 66 6c 75 6f 75 73 20  ree superfluous 
10fac 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f  dynamically allo
10fad 63 61 74 65 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20  cated memory.** 
10fae 68 65 6c 64 20 62 79 20 74 68 65 20 70 61 67 65  held by the page
10faf 72 20 73 79 73 74 65 6d 2e 20 4d 65 6d 6f 72 79  r system. Memory
10fb0 20 69 6e 20 75 73 65 20 62 79 20 61 6e 79 20 53   in use by any S
10fb1 51 4c 69 74 65 20 70 61 67 65 72 20 61 6c 6c 6f  QLite pager allo
10fb2 63 61 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20  cated.** by the 
10fb3 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 6d  current thread m
10fb4 61 79 20 62 65 20 73 71 6c 69 74 65 33 5f 66 72  ay be sqlite3_fr
10fb5 65 65 28 29 65 64 2e 0a 2a 2a 0a 2a 2a 20 6e 52  ee()ed..**.** nR
10fb6 65 71 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  eq is the number
10fb7 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d   of bytes of mem
10fb8 6f 72 79 20 72 65 71 75 69 72 65 64 2e 20 4f 6e  ory required. On
10fb9 63 65 20 74 68 69 73 20 6d 75 63 68 20 68 61 73  ce this much has
10fba 0a 2a 2a 20 62 65 65 6e 20 72 65 6c 65 61 73 65  .** been release
10fbb 64 2c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  d, the function 
10fbc 72 65 74 75 72 6e 73 2e 20 54 68 65 20 72 65 74  returns. The ret
10fbd 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65  urn value is the
10fbe 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 0a 2a   total number .*
10fbf 2a 20 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65  * of bytes of me
10fc0 6d 6f 72 79 20 72 65 6c 65 61 73 65 64 2e 0a 2a  mory released..*
10fc1 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
10fc2 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63   int sqlite3Pcac
10fc3 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28  heReleaseMemory(
10fc4 69 6e 74 20 6e 52 65 71 29 7b 0a 20 20 69 6e 74  int nReq){.  int
10fc5 20 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 69 66   nFree = 0;.  if
10fc6 28 20 70 63 61 63 68 65 31 2e 70 53 74 61 72 74  ( pcache1.pStart
10fc7 3d 3d 30 20 29 7b 0a 20 20 20 20 50 67 48 64 72  ==0 ){.    PgHdr
10fc8 31 20 2a 70 3b 0a 20 20 20 20 70 63 61 63 68 65  1 *p;.    pcache
10fc9 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  1EnterMutex();. 
10fca 20 20 20 77 68 69 6c 65 28 20 28 6e 52 65 71 3c     while( (nReq<
10fcb 30 20 7c 7c 20 6e 46 72 65 65 3c 6e 52 65 71 29  0 || nFree<nReq)
10fcc 20 26 26 20 28 70 3d 70 63 61 63 68 65 31 2e 70   && (p=pcache1.p
10fcd 4c 72 75 54 61 69 6c 29 20 29 7b 0a 20 20 20 20  LruTail) ){.    
10fce 20 20 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69 74    nFree += sqlit
10fcf 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 50 47 48  e3MallocSize(PGH
10fd0 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 29 29 3b  DR1_TO_PAGE(p));
10fd1 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 50 69  .      pcache1Pi
10fd2 6e 50 61 67 65 28 70 29 3b 0a 20 20 20 20 20 20  nPage(p);.      
10fd3 70 63 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f  pcache1RemoveFro
10fd4 6d 48 61 73 68 28 70 29 3b 0a 20 20 20 20 20 20  mHash(p);.      
10fd5 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28  pcache1FreePage(
10fd6 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 63  p);.    }.    pc
10fd7 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28  ache1LeaveMutex(
10fd8 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
10fd9 6e 46 72 65 65 3b 0a 7d 0a 23 65 6e 64 69 66 20  nFree;.}.#endif 
10fda 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  /* SQLITE_ENABLE
10fdb 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45  _MEMORY_MANAGEME
10fdc 4e 54 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51  NT */..#ifdef SQ
10fdd 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20  LITE_TEST./*.** 
10fde 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
10fdf 20 75 73 65 64 20 62 79 20 74 65 73 74 20 70 72   used by test pr
10fe0 6f 63 65 64 75 72 65 73 20 74 6f 20 69 6e 73 70  ocedures to insp
10fe1 65 63 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  ect the internal
10fe2 20 73 74 61 74 65 0a 2a 2a 20 6f 66 20 74 68 65   state.** of the
10fe3 20 67 6c 6f 62 61 6c 20 63 61 63 68 65 2e 0a 2a   global cache..*
10fe4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
10fe5 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
10fe6 63 68 65 53 74 61 74 73 28 0a 20 20 69 6e 74 20  cheStats(.  int 
10fe7 2a 70 6e 43 75 72 72 65 6e 74 2c 20 20 20 20 20  *pnCurrent,     
10fe8 20 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 6c 20 6e   /* OUT: Total n
10fe9 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 63  umber of pages c
10fea 61 63 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a  ached */.  int *
10feb 70 6e 4d 61 78 2c 20 20 20 20 20 20 20 20 20 20  pnMax,          
10fec 2f 2a 20 4f 55 54 3a 20 47 6c 6f 62 61 6c 20 6d  /* OUT: Global m
10fed 61 78 69 6d 75 6d 20 63 61 63 68 65 20 73 69 7a  aximum cache siz
10fee 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d 69  e */.  int *pnMi
10fef 6e 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  n,          /* O
10ff0 55 54 3a 20 53 75 6d 20 6f 66 20 50 43 61 63 68  UT: Sum of PCach
10ff1 65 31 2e 6e 4d 69 6e 20 66 6f 72 20 70 75 72 67  e1.nMin for purg
10ff2 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a  eable caches */.
10ff3 20 20 69 6e 74 20 2a 70 6e 52 65 63 79 63 6c 61    int *pnRecycla
10ff4 62 6c 65 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54  ble    /* OUT: T
10ff5 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70  otal number of p
10ff6 61 67 65 73 20 61 76 61 69 6c 61 62 6c 65 20 66  ages available f
10ff7 6f 72 20 72 65 63 79 63 6c 69 6e 67 20 2a 2f 0a  or recycling */.
10ff8 29 7b 0a 20 20 50 67 48 64 72 31 20 2a 70 3b 0a  ){.  PgHdr1 *p;.
10ff9 20 20 69 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c    int nRecyclabl
10ffa 65 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 3d 70  e = 0;.  for(p=p
10ffb 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3b  cache1.pLruHead;
10ffc 20 70 3b 20 70 3d 70 2d 3e 70 4c 72 75 4e 65 78   p; p=p->pLruNex
10ffd 74 29 7b 0a 20 20 20 20 6e 52 65 63 79 63 6c 61  t){.    nRecycla
10ffe 62 6c 65 2b 2b 3b 0a 20 20 7d 0a 20 20 2a 70 6e  ble++;.  }.  *pn
10fff 43 75 72 72 65 6e 74 20 3d 20 70 63 61 63 68 65  Current = pcache
11000 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3b 0a  1.nCurrentPage;.
11001 20 20 2a 70 6e 4d 61 78 20 3d 20 70 63 61 63 68    *pnMax = pcach
11002 65 31 2e 6e 4d 61 78 50 61 67 65 3b 0a 20 20 2a  e1.nMaxPage;.  *
11003 70 6e 4d 69 6e 20 3d 20 70 63 61 63 68 65 31 2e  pnMin = pcache1.
11004 6e 4d 69 6e 50 61 67 65 3b 0a 20 20 2a 70 6e 52  nMinPage;.  *pnR
11005 65 63 79 63 6c 61 62 6c 65 20 3d 20 6e 52 65 63  ecyclable = nRec
11006 79 63 6c 61 62 6c 65 3b 0a 7d 0a 23 65 6e 64 69  yclable;.}.#endi
11007 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
11008 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61 63 68 65  ** End of pcache
11009 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  1.c ************
1100a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1100b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1100c 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
1100d 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 6f  ** Begin file ro
1100e 77 73 65 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  wset.c *********
1100f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11011 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 44 65  */./*.** 2008 De
11012 63 65 6d 62 65 72 20 33 0a 2a 2a 0a 2a 2a 20 54  cember 3.**.** T
11013 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
11014 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
11015 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
11016 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
11017 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
11018 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
11019 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
1101a 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
1101b 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
1101c 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
1101d 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
1101e 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
1101f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
11020 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
11021 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
11022 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
11023 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
11024 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11025 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11026 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11027 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11028 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
11029 69 73 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d  is module implem
1102a 65 6e 74 73 20 61 6e 20 6f 62 6a 65 63 74 20 77  ents an object w
1102b 65 20 63 61 6c 6c 20 61 20 22 52 6f 77 53 65 74  e call a "RowSet
1102c 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 52 6f 77  "..**.** The Row
1102d 53 65 74 20 6f 62 6a 65 63 74 20 69 73 20 61 20  Set object is a 
1102e 63 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 72 6f  collection of ro
1102f 77 69 64 73 2e 20 20 52 6f 77 69 64 73 0a 2a 2a  wids.  Rowids.**
11030 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e   are inserted in
11031 74 6f 20 74 68 65 20 52 6f 77 53 65 74 20 69 6e  to the RowSet in
11032 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 6f 72   an arbitrary or
11033 64 65 72 2e 20 20 49 6e 73 65 72 74 73 0a 2a 2a  der.  Inserts.**
11034 20 63 61 6e 20 62 65 20 69 6e 74 65 72 6d 69 78   can be intermix
11035 65 64 20 77 69 74 68 20 74 65 73 74 73 20 74 6f  ed with tests to
11036 20 73 65 65 20 69 66 20 61 20 67 69 76 65 6e 20   see if a given 
11037 72 6f 77 69 64 20 68 61 73 20 62 65 65 6e 0a 2a  rowid has been.*
11038 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 73  * previously ins
11039 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 52  erted into the R
1103a 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 66 74  owSet..**.** Aft
1103b 65 72 20 61 6c 6c 20 69 6e 73 65 72 74 73 20 61  er all inserts a
1103c 72 65 20 66 69 6e 69 73 68 65 64 2c 20 69 74 20  re finished, it 
1103d 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 65  is possible to e
1103e 78 74 72 61 63 74 20 74 68 65 0a 2a 2a 20 65 6c  xtract the.** el
1103f 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 52 6f  ements of the Ro
11040 77 53 65 74 20 69 6e 20 73 6f 72 74 65 64 20 6f  wSet in sorted o
11041 72 64 65 72 2e 20 20 4f 6e 63 65 20 74 68 69 73  rder.  Once this
11042 20 65 78 74 72 61 63 74 69 6f 6e 0a 2a 2a 20 70   extraction.** p
11043 72 6f 63 65 73 73 20 68 61 73 20 73 74 61 72 74  rocess has start
11044 65 64 2c 20 6e 6f 20 6e 65 77 20 65 6c 65 6d 65  ed, no new eleme
11045 6e 74 73 20 6d 61 79 20 62 65 20 69 6e 73 65 72  nts may be inser
11046 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 48 65 6e 63 65  ted..**.** Hence
11047 2c 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 20  , the primitive 
11048 6f 70 65 72 61 74 69 6f 6e 73 20 66 6f 72 20 61  operations for a
11049 20 52 6f 77 53 65 74 20 61 72 65 3a 0a 2a 2a 0a   RowSet are:.**.
1104a 2a 2a 20 20 20 20 43 52 45 41 54 45 0a 2a 2a 20  **    CREATE.** 
1104b 20 20 20 49 4e 53 45 52 54 0a 2a 2a 20 20 20 20     INSERT.**    
1104c 54 45 53 54 0a 2a 2a 20 20 20 20 53 4d 41 4c 4c  TEST.**    SMALL
1104d 45 53 54 0a 2a 2a 20 20 20 20 44 45 53 54 52 4f  EST.**    DESTRO
1104e 59 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 52 45 41  Y.**.** The CREA
1104f 54 45 20 61 6e 64 20 44 45 53 54 52 4f 59 20 70  TE and DESTROY p
11050 72 69 6d 69 74 69 76 65 73 20 61 72 65 20 74 68  rimitives are th
11051 65 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 61 6e  e constructor an
11052 64 20 64 65 73 74 72 75 63 74 6f 72 2c 0a 2a 2a  d destructor,.**
11053 20 6f 62 76 69 6f 75 73 6c 79 2e 20 20 54 68 65   obviously.  The
11054 20 49 4e 53 45 52 54 20 70 72 69 6d 69 74 69 76   INSERT primitiv
11055 65 20 61 64 64 73 20 61 20 6e 65 77 20 65 6c 65  e adds a new ele
11056 6d 65 6e 74 20 74 6f 20 74 68 65 20 52 6f 77 53  ment to the RowS
11057 65 74 2e 0a 2a 2a 20 54 45 53 54 20 63 68 65 63  et..** TEST chec
11058 6b 73 20 74 6f 20 73 65 65 20 69 66 20 61 6e 20  ks to see if an 
11059 65 6c 65 6d 65 6e 74 20 69 73 20 61 6c 72 65 61  element is alrea
1105a 64 79 20 69 6e 20 74 68 65 20 52 6f 77 53 65 74  dy in the RowSet
1105b 2e 20 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 65  .  SMALLEST.** e
1105c 78 74 72 61 63 74 73 20 74 68 65 20 6c 65 61 73  xtracts the leas
1105d 74 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65  t value from the
1105e 20 52 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54   RowSet..**.** T
1105f 68 65 20 49 4e 53 45 52 54 20 70 72 69 6d 69 74  he INSERT primit
11060 69 76 65 20 6d 69 67 68 74 20 61 6c 6c 6f 63 61  ive might alloca
11061 74 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 6d 65  te additional me
11062 6d 6f 72 79 2e 20 20 4d 65 6d 6f 72 79 20 69 73  mory.  Memory is
11063 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e  .** allocated in
11064 20 63 68 75 6e 6b 73 20 73 6f 20 6d 6f 73 74 20   chunks so most 
11065 49 4e 53 45 52 54 73 20 64 6f 20 6e 6f 20 61 6c  INSERTs do no al
11066 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 72 65  location.  There
11067 20 69 73 20 61 6e 20 0a 2a 2a 20 75 70 70 65 72   is an .** upper
11068 20 62 6f 75 6e 64 20 6f 6e 20 74 68 65 20 73 69   bound on the si
11069 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20  ze of allocated 
1106a 6d 65 6d 6f 72 79 2e 20 20 4e 6f 20 6d 65 6d 6f  memory.  No memo
1106b 72 79 20 69 73 20 66 72 65 65 64 0a 2a 2a 20 75  ry is freed.** u
1106c 6e 74 69 6c 20 44 45 53 54 52 4f 59 2e 0a 2a 2a  ntil DESTROY..**
1106d 0a 2a 2a 20 54 68 65 20 54 45 53 54 20 70 72 69  .** The TEST pri
1106e 6d 69 74 69 76 65 20 69 6e 63 6c 75 64 65 73 20  mitive includes 
1106f 61 20 22 62 61 74 63 68 22 20 6e 75 6d 62 65 72  a "batch" number
11070 2e 20 20 54 68 65 20 54 45 53 54 20 70 72 69 6d  .  The TEST prim
11071 69 74 69 76 65 0a 2a 2a 20 77 69 6c 6c 20 6f 6e  itive.** will on
11072 6c 79 20 73 65 65 20 65 6c 65 6d 65 6e 74 73 20  ly see elements 
11073 74 68 61 74 20 77 65 72 65 20 69 6e 73 65 72 74  that were insert
11074 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61  ed before the la
11075 73 74 20 63 68 61 6e 67 65 0a 2a 2a 20 69 6e 20  st change.** in 
11076 74 68 65 20 62 61 74 63 68 20 6e 75 6d 62 65 72  the batch number
11077 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
11078 73 2c 20 69 66 20 61 6e 20 49 4e 53 45 52 54 20  s, if an INSERT 
11079 6f 63 63 75 72 73 20 62 65 74 77 65 65 6e 0a 2a  occurs between.*
1107a 2a 20 74 77 6f 20 54 45 53 54 73 20 77 68 65 72  * two TESTs wher
1107b 65 20 74 68 65 20 54 45 53 54 73 20 68 61 76 65  e the TESTs have
1107c 20 74 68 65 20 73 61 6d 65 20 62 61 74 63 68 20   the same batch 
1107d 6e 75 62 6d 65 72 2c 20 74 68 65 6e 20 74 68 65  nubmer, then the
1107e 0a 2a 2a 20 76 61 6c 75 65 20 61 64 64 65 64 20  .** value added 
1107f 62 79 20 74 68 65 20 49 4e 53 45 52 54 20 77 69  by the INSERT wi
11080 6c 6c 20 6e 6f 74 20 62 65 20 76 69 73 69 62 6c  ll not be visibl
11081 65 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20  e to the second 
11082 54 45 53 54 2e 0a 2a 2a 20 54 68 65 20 69 6e 69  TEST..** The ini
11083 74 69 61 6c 20 62 61 74 63 68 20 6e 75 6d 62 65  tial batch numbe
11084 72 20 69 73 20 7a 65 72 6f 2c 20 73 6f 20 69 66  r is zero, so if
11085 20 74 68 65 20 76 65 72 79 20 66 69 72 73 74 20   the very first 
11086 54 45 53 54 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a  TEST contains.**
11087 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 62 61 74 63   a non-zero batc
11088 68 20 6e 75 6d 62 65 72 2c 20 69 74 20 77 69 6c  h number, it wil
11089 6c 20 73 65 65 20 61 6c 6c 20 70 72 69 6f 72 20  l see all prior 
1108a 49 4e 53 45 52 54 73 2e 0a 2a 2a 0a 2a 2a 20 4e  INSERTs..**.** N
1108b 6f 20 49 4e 53 45 52 54 73 20 6d 61 79 20 6f 63  o INSERTs may oc
1108c 63 75 72 73 20 61 66 74 65 72 20 61 20 53 4d 41  curs after a SMA
1108d 4c 4c 45 53 54 2e 20 20 41 6e 20 61 73 73 65 72  LLEST.  An asser
1108e 74 69 6f 6e 20 77 69 6c 6c 20 66 61 69 6c 20 69  tion will fail i
1108f 66 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 74 74  f.** that is att
11090 65 6d 70 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  empted..**.** Th
11091 65 20 63 6f 73 74 20 6f 66 20 61 6e 20 49 4e 53  e cost of an INS
11092 45 52 54 20 69 73 20 72 6f 75 67 68 6c 79 20 63  ERT is roughly c
11093 6f 6e 73 74 61 6e 74 2e 20 20 28 53 6f 6d 65 74  onstant.  (Somet
11094 69 6d 65 20 6e 65 77 20 6d 65 6d 6f 72 79 0a 2a  ime new memory.*
11095 2a 20 68 61 73 20 74 6f 20 62 65 20 61 6c 6c 6f  * has to be allo
11096 63 61 74 65 64 20 6f 6e 20 61 6e 20 49 4e 53 45  cated on an INSE
11097 52 54 2e 29 20 20 54 68 65 20 63 6f 73 74 20 6f  RT.)  The cost o
11098 66 20 61 20 54 45 53 54 20 77 69 74 68 20 61 20  f a TEST with a 
11099 6e 65 77 0a 2a 2a 20 62 61 74 63 68 20 6e 75 6d  new.** batch num
1109a 62 65 72 20 69 73 20 4f 28 4e 6c 6f 67 4e 29 20  ber is O(NlogN) 
1109b 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e  where N is the n
1109c 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74  umber of element
1109d 73 20 69 6e 20 74 68 65 20 52 6f 77 53 65 74 2e  s in the RowSet.
1109e 0a 2a 2a 20 54 68 65 20 63 6f 73 74 20 6f 66 20  .** The cost of 
1109f 61 20 54 45 53 54 20 75 73 69 6e 67 20 74 68 65  a TEST using the
110a0 20 73 61 6d 65 20 62 61 74 63 68 20 6e 75 6d 62   same batch numb
110a1 65 72 20 69 73 20 4f 28 6c 6f 67 4e 29 2e 20 20  er is O(logN).  
110a2 54 68 65 20 63 6f 73 74 0a 2a 2a 20 6f 66 20 74  The cost.** of t
110a3 68 65 20 66 69 72 73 74 20 53 4d 41 4c 4c 45 53  he first SMALLES
110a4 54 20 69 73 20 4f 28 4e 6c 6f 67 4e 29 2e 20 20  T is O(NlogN).  
110a5 53 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65  Second and subse
110a6 71 75 65 6e 74 20 53 4d 41 4c 4c 45 53 54 0a 2a  quent SMALLEST.*
110a7 2a 20 70 72 69 6d 69 74 69 76 65 73 20 61 72 65  * primitives are
110a8 20 63 6f 6e 73 74 61 6e 74 20 74 69 6d 65 2e 20   constant time. 
110a9 20 54 68 65 20 63 6f 73 74 20 6f 66 20 44 45 53   The cost of DES
110aa 54 52 4f 59 20 69 73 20 4f 28 4e 29 2e 0a 2a 2a  TROY is O(N)..**
110ab 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 61 6e 20  .** There is an 
110ac 61 64 64 65 64 20 63 6f 73 74 20 6f 66 20 4f 28  added cost of O(
110ad 4e 29 20 77 68 65 6e 20 73 77 69 74 63 68 69 6e  N) when switchin
110ae 67 20 62 65 74 77 65 65 6e 20 54 45 53 54 20 61  g between TEST a
110af 6e 64 0a 2a 2a 20 53 4d 41 4c 4c 45 53 54 20 70  nd.** SMALLEST p
110b0 72 69 6d 69 74 69 76 65 73 2e 0a 2a 2a 0a 2a 2a  rimitives..**.**
110b1 20 24 49 64 3a 20 72 6f 77 73 65 74 2e 63 2c 76   $Id: rowset.c,v
110b2 20 31 2e 37 20 32 30 30 39 2f 30 35 2f 32 32 20   1.7 2009/05/22 
110b3 30 31 3a 30 30 3a 31 33 20 64 72 68 20 45 78 70  01:00:13 drh Exp
110b4 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 61   $.*/.../*.** Ta
110b5 72 67 65 74 20 73 69 7a 65 20 66 6f 72 20 61 6c  rget size for al
110b6 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e 6b 73 2e  location chunks.
110b7 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f 57 53  .*/.#define ROWS
110b8 45 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49  ET_ALLOCATION_SI
110b9 5a 45 20 31 30 32 34 0a 0a 2f 2a 0a 2a 2a 20 54  ZE 1024../*.** T
110ba 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
110bb 73 65 74 20 65 6e 74 72 69 65 73 20 70 65 72 20  set entries per 
110bc 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e 6b  allocation chunk
110bd 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f 57  ..*/.#define ROW
110be 53 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48  SET_ENTRY_PER_CH
110bf 55 4e 4b 20 20 5c 0a 20 20 20 20 20 20 20 20 20  UNK  \.         
110c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 28                ((
110c1 52 4f 57 53 45 54 5f 41 4c 4c 4f 43 41 54 49 4f  ROWSET_ALLOCATIO
110c2 4e 5f 53 49 5a 45 2d 38 29 2f 73 69 7a 65 6f 66  N_SIZE-8)/sizeof
110c3 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e  (struct RowSetEn
110c4 74 72 79 29 29 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  try))../*.** Eac
110c5 68 20 65 6e 74 72 79 20 69 6e 20 61 20 52 6f 77  h entry in a Row
110c6 53 65 74 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  Set is an instan
110c7 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
110c8 69 6e 67 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73  ing object..*/.s
110c9 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
110ca 79 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 0a  y {            .
110cb 20 20 69 36 34 20 76 3b 20 20 20 20 20 20 20 20    i64 v;        
110cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
110cd 2f 2a 20 52 4f 57 49 44 20 76 61 6c 75 65 20 66  /* ROWID value f
110ce 6f 72 20 74 68 69 73 20 65 6e 74 72 79 20 2a 2f  or this entry */
110cf 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
110d0 45 6e 74 72 79 20 2a 70 52 69 67 68 74 3b 20 20  Entry *pRight;  
110d1 20 2f 2a 20 52 69 67 68 74 20 73 75 62 74 72 65   /* Right subtre
110d2 65 20 28 6c 61 72 67 65 72 20 65 6e 74 72 69 65  e (larger entrie
110d3 73 29 20 6f 72 20 6c 69 73 74 20 2a 2f 0a 20 20  s) or list */.  
110d4 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
110d5 72 79 20 2a 70 4c 65 66 74 3b 20 20 20 20 2f 2a  ry *pLeft;    /*
110d6 20 4c 65 66 74 20 73 75 62 74 72 65 65 20 28 73   Left subtree (s
110d7 6d 61 6c 6c 65 72 20 65 6e 74 72 69 65 73 29 20  maller entries) 
110d8 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 77  */.};../*.** Row
110d9 53 65 74 45 6e 74 72 79 20 6f 62 6a 65 63 74 73  SetEntry objects
110da 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 69   are allocated i
110db 6e 20 6c 61 72 67 65 20 63 68 75 6e 6b 73 20 28  n large chunks (
110dc 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68 65  instances of the
110dd 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  .** following st
110de 72 75 63 74 75 72 65 29 20 74 6f 20 72 65 64 75  ructure) to redu
110df 63 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ce memory alloca
110e0 74 69 6f 6e 20 6f 76 65 72 68 65 61 64 2e 20 20  tion overhead.  
110e1 54 68 65 0a 2a 2a 20 63 68 75 6e 6b 73 20 61 72  The.** chunks ar
110e2 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e 6b  e kept on a link
110e3 65 64 20 6c 69 73 74 20 73 6f 20 74 68 61 74 20  ed list so that 
110e4 74 68 65 79 20 63 61 6e 20 62 65 20 64 65 61 6c  they can be deal
110e5 6c 6f 63 61 74 65 64 0a 2a 2a 20 77 68 65 6e 20  located.** when 
110e6 74 68 65 20 52 6f 77 53 65 74 20 69 73 20 64 65  the RowSet is de
110e7 73 74 72 6f 79 65 64 2e 0a 2a 2f 0a 73 74 72 75  stroyed..*/.stru
110e8 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 7b  ct RowSetChunk {
110e9 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
110ea 43 68 75 6e 6b 20 2a 70 4e 65 78 74 43 68 75 6e  Chunk *pNextChun
110eb 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78  k;        /* Nex
110ec 74 20 63 68 75 6e 6b 20 6f 6e 20 6c 69 73 74 20  t chunk on list 
110ed 6f 66 20 74 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20  of them all */. 
110ee 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
110ef 74 72 79 20 61 45 6e 74 72 79 5b 52 4f 57 53 45  try aEntry[ROWSE
110f0 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e  T_ENTRY_PER_CHUN
110f1 4b 5d 3b 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64  K]; /* Allocated
110f2 20 65 6e 74 72 69 65 73 20 2a 2f 0a 7d 3b 0a 0a   entries */.};..
110f3 2f 2a 0a 2a 2a 20 41 20 52 6f 77 53 65 74 20 69  /*.** A RowSet i
110f4 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  n an instance of
110f5 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
110f6 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
110f7 41 20 74 79 70 65 64 65 66 20 6f 66 20 74 68 69  A typedef of thi
110f8 73 20 73 74 72 75 63 74 75 72 65 20 69 66 20 66  s structure if f
110f9 6f 75 6e 64 20 69 6e 20 73 71 6c 69 74 65 49 6e  ound in sqliteIn
110fa 74 2e 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52  t.h..*/.struct R
110fb 6f 77 53 65 74 20 7b 0a 20 20 73 74 72 75 63 74  owSet {.  struct
110fc 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 43   RowSetChunk *pC
110fd 68 75 6e 6b 3b 20 20 20 20 2f 2a 20 4c 69 73 74  hunk;    /* List
110fe 20 6f 66 20 61 6c 6c 20 63 68 75 6e 6b 20 61 6c   of all chunk al
110ff 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 73  locations */.  s
11100 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
11101 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11102 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
11103 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 74  nnection */.  st
11104 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
11105 20 2a 70 45 6e 74 72 79 3b 20 20 20 20 2f 2a 20   *pEntry;    /* 
11106 4c 69 73 74 20 6f 66 20 65 6e 74 72 69 65 73 20  List of entries 
11107 75 73 69 6e 67 20 70 52 69 67 68 74 20 2a 2f 0a  using pRight */.
11108 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
11109 6e 74 72 79 20 2a 70 4c 61 73 74 3b 20 20 20 20  ntry *pLast;    
1110a 20 2f 2a 20 4c 61 73 74 20 65 6e 74 72 79 20 6f   /* Last entry o
1110b 6e 20 74 68 65 20 70 45 6e 74 72 79 20 6c 69 73  n the pEntry lis
1110c 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f  t */.  struct Ro
1110d 77 53 65 74 45 6e 74 72 79 20 2a 70 46 72 65 73  wSetEntry *pFres
1110e 68 3b 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20  h;    /* Source 
1110f 6f 66 20 6e 65 77 20 65 6e 74 72 79 20 6f 62 6a  of new entry obj
11110 65 63 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74  ects */.  struct
11111 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 54   RowSetEntry *pT
11112 72 65 65 3b 20 20 20 20 20 2f 2a 20 42 69 6e 61  ree;     /* Bina
11113 72 79 20 74 72 65 65 20 6f 66 20 65 6e 74 72 69  ry tree of entri
11114 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72 65  es */.  u16 nFre
11115 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sh;             
11116 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
11117 20 6f 66 20 6f 62 6a 65 63 74 73 20 6f 6e 20 70   of objects on p
11118 46 72 65 73 68 20 2a 2f 0a 20 20 75 38 20 69 73  Fresh */.  u8 is
11119 53 6f 72 74 65 64 3b 20 20 20 20 20 20 20 20 20  Sorted;         
1111a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1111b 65 20 69 66 20 70 45 6e 74 72 79 20 69 73 20 73  e if pEntry is s
1111c 6f 72 74 65 64 20 2a 2f 0a 20 20 75 38 20 69 42  orted */.  u8 iB
1111d 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20  atch;           
1111e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
1111f 72 65 6e 74 20 69 6e 73 65 72 74 20 62 61 74 63  rent insert batc
11120 68 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  h */.};../*.** T
11121 75 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 20  urn bulk memory 
11122 69 6e 74 6f 20 61 20 52 6f 77 53 65 74 20 6f 62  into a RowSet ob
11123 6a 65 63 74 2e 20 20 4e 20 62 79 74 65 73 20 6f  ject.  N bytes o
11124 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 20  f memory.** are 
11125 61 76 61 69 6c 61 62 6c 65 20 61 74 20 70 53 70  available at pSp
11126 61 63 65 2e 20 20 54 68 65 20 64 62 20 70 6f 69  ace.  The db poi
11127 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73 20  nter is used as 
11128 61 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 65 78 74  a memory context
11129 0a 2a 2a 20 66 6f 72 20 61 6e 79 20 73 75 62 73  .** for any subs
1112a 65 71 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f  equent allocatio
1112b 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20  ns that need to 
1112c 6f 63 63 75 72 2e 0a 2a 2a 20 52 65 74 75 72 6e  occur..** Return
1112d 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
1112e 65 20 6e 65 77 20 52 6f 77 53 65 74 20 6f 62 6a  e new RowSet obj
1112f 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 75  ect..**.** It mu
11130 73 74 20 62 65 20 74 68 65 20 63 61 73 65 20 74  st be the case t
11131 68 61 74 20 4e 20 69 73 20 73 75 66 66 69 63 69  hat N is suffici
11132 65 6e 74 20 74 6f 20 6d 61 6b 65 20 61 20 52 6f  ent to make a Ro
11133 77 73 65 74 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a  wset.  If not.**
11134 20 61 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61   an assertion fa
11135 75 6c 74 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 0a  ult occurs..** .
11136 2a 2a 20 49 66 20 4e 20 69 73 20 6c 61 72 67 65  ** If N is large
11137 72 20 74 68 61 6e 20 74 68 65 20 6d 69 6e 69 6d  r than the minim
11138 75 6d 2c 20 75 73 65 20 74 68 65 20 73 75 72 70  um, use the surp
11139 6c 75 73 20 61 73 20 61 6e 20 69 6e 69 74 69 61  lus as an initia
1113a 6c 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  l.** allocation 
1113b 6f 66 20 65 6e 74 72 69 65 73 20 61 76 61 69 6c  of entries avail
1113c 61 62 6c 65 20 74 6f 20 62 65 20 66 69 6c 6c 65  able to be fille
1113d 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
1113e 56 41 54 45 20 52 6f 77 53 65 74 20 2a 73 71 6c  VATE RowSet *sql
1113f 69 74 65 33 52 6f 77 53 65 74 49 6e 69 74 28 73  ite3RowSetInit(s
11140 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64  qlite3 *db, void
11141 20 2a 70 53 70 61 63 65 2c 20 75 6e 73 69 67 6e   *pSpace, unsign
11142 65 64 20 69 6e 74 20 4e 29 7b 0a 20 20 52 6f 77  ed int N){.  Row
11143 53 65 74 20 2a 70 3b 0a 20 20 61 73 73 65 72 74  Set *p;.  assert
11144 28 20 4e 20 3e 3d 20 52 4f 55 4e 44 38 28 73 69  ( N >= ROUND8(si
11145 7a 65 6f 66 28 2a 70 29 29 20 29 3b 0a 20 20 70  zeof(*p)) );.  p
11146 20 3d 20 70 53 70 61 63 65 3b 0a 20 20 70 2d 3e   = pSpace;.  p->
11147 70 43 68 75 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d  pChunk = 0;.  p-
11148 3e 64 62 20 3d 20 64 62 3b 0a 20 20 70 2d 3e 70  >db = db;.  p->p
11149 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 70 2d 3e  Entry = 0;.  p->
1114a 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e  pLast = 0;.  p->
1114b 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 70 2d 3e  pTree = 0;.  p->
1114c 70 46 72 65 73 68 20 3d 20 28 73 74 72 75 63 74  pFresh = (struct
1114d 20 52 6f 77 53 65 74 45 6e 74 72 79 2a 29 28 52   RowSetEntry*)(R
1114e 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 29  OUND8(sizeof(*p)
1114f 29 20 2b 20 28 63 68 61 72 2a 29 70 29 3b 0a 20  ) + (char*)p);. 
11150 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20 28 75 31   p->nFresh = (u1
11151 36 29 28 28 4e 20 2d 20 52 4f 55 4e 44 38 28 73  6)((N - ROUND8(s
11152 69 7a 65 6f 66 28 2a 70 29 29 29 2f 73 69 7a 65  izeof(*p)))/size
11153 6f 66 28 73 74 72 75 63 74 20 52 6f 77 53 65 74  of(struct RowSet
11154 45 6e 74 72 79 29 29 3b 0a 20 20 70 2d 3e 69 73  Entry));.  p->is
11155 53 6f 72 74 65 64 20 3d 20 31 3b 0a 20 20 70 2d  Sorted = 1;.  p-
11156 3e 69 42 61 74 63 68 20 3d 20 30 3b 0a 20 20 72  >iBatch = 0;.  r
11157 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
11158 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c 6c  * Deallocate all
11159 20 63 68 75 6e 6b 73 20 66 72 6f 6d 20 61 20 52   chunks from a R
1115a 6f 77 53 65 74 2e 20 20 54 68 69 73 20 66 72 65  owSet.  This fre
1115b 65 73 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 74 68  es all memory th
1115c 61 74 0a 2a 2a 20 74 68 65 20 52 6f 77 53 65 74  at.** the RowSet
1115d 20 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 6f   has allocated o
1115e 76 65 72 20 69 74 73 20 6c 69 66 65 74 69 6d 65  ver its lifetime
1115f 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
11160 69 73 0a 2a 2a 20 74 68 65 20 64 65 73 74 72 75  is.** the destru
11161 63 74 6f 72 20 66 6f 72 20 74 68 65 20 52 6f 77  ctor for the Row
11162 53 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  Set..*/.SQLITE_P
11163 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
11164 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 52  te3RowSetClear(R
11165 6f 77 53 65 74 20 2a 70 29 7b 0a 20 20 73 74 72  owSet *p){.  str
11166 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20  uct RowSetChunk 
11167 2a 70 43 68 75 6e 6b 2c 20 2a 70 4e 65 78 74 43  *pChunk, *pNextC
11168 68 75 6e 6b 3b 0a 20 20 66 6f 72 28 70 43 68 75  hunk;.  for(pChu
11169 6e 6b 3d 70 2d 3e 70 43 68 75 6e 6b 3b 20 70 43  nk=p->pChunk; pC
1116a 68 75 6e 6b 3b 20 70 43 68 75 6e 6b 20 3d 20 70  hunk; pChunk = p
1116b 4e 65 78 74 43 68 75 6e 6b 29 7b 0a 20 20 20 20  NextChunk){.    
1116c 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20 70 43 68  pNextChunk = pCh
1116d 75 6e 6b 2d 3e 70 4e 65 78 74 43 68 75 6e 6b 3b  unk->pNextChunk;
1116e 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
1116f 65 65 28 70 2d 3e 64 62 2c 20 70 43 68 75 6e 6b  ee(p->db, pChunk
11170 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 43 68 75  );.  }.  p->pChu
11171 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 46 72  nk = 0;.  p->nFr
11172 65 73 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 45  esh = 0;.  p->pE
11173 6e 74 72 79 20 3d 20 30 3b 0a 20 20 70 2d 3e 70  ntry = 0;.  p->p
11174 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 70  Last = 0;.  p->p
11175 54 72 65 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 69  Tree = 0;.  p->i
11176 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a  sSorted = 1;.}..
11177 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e  /*.** Insert a n
11178 65 77 20 76 61 6c 75 65 20 69 6e 74 6f 20 61 20  ew value into a 
11179 52 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  RowSet..**.** Th
1117a 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66  e mallocFailed f
1117b 6c 61 67 20 6f 66 20 74 68 65 20 64 61 74 61 62  lag of the datab
1117c 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  ase connection i
1117d 73 20 73 65 74 20 69 66 20 61 0a 2a 2a 20 6d 65  s set if a.** me
1117e 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
1117f 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  fails..*/.SQLITE
11180 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
11181 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72  lite3RowSetInser
11182 74 28 52 6f 77 53 65 74 20 2a 70 2c 20 69 36 34  t(RowSet *p, i64
11183 20 72 6f 77 69 64 29 7b 0a 20 20 73 74 72 75 63   rowid){.  struc
11184 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70  t RowSetEntry *p
11185 45 6e 74 72 79 3b 20 20 2f 2a 20 54 68 65 20 6e  Entry;  /* The n
11186 65 77 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73 74  ew entry */.  st
11187 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
11188 20 2a 70 4c 61 73 74 3b 20 20 20 2f 2a 20 54 68   *pLast;   /* Th
11189 65 20 6c 61 73 74 20 70 72 69 6f 72 20 65 6e 74  e last prior ent
1118a 72 79 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  ry */.  assert( 
1118b 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d  p!=0 );.  if( p-
1118c 3e 6e 46 72 65 73 68 3d 3d 30 20 29 7b 0a 20 20  >nFresh==0 ){.  
1118d 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 43    struct RowSetC
1118e 68 75 6e 6b 20 2a 70 4e 65 77 3b 0a 20 20 20 20  hunk *pNew;.    
1118f 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  pNew = sqlite3Db
11190 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c  MallocRaw(p->db,
11191 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b   sizeof(*pNew));
11192 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30  .    if( pNew==0
11193 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
11194 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77  ;.    }.    pNew
11195 2d 3e 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20 70  ->pNextChunk = p
11196 2d 3e 70 43 68 75 6e 6b 3b 0a 20 20 20 20 70 2d  ->pChunk;.    p-
11197 3e 70 43 68 75 6e 6b 20 3d 20 70 4e 65 77 3b 0a  >pChunk = pNew;.
11198 20 20 20 20 70 2d 3e 70 46 72 65 73 68 20 3d 20      p->pFresh = 
11199 70 4e 65 77 2d 3e 61 45 6e 74 72 79 3b 0a 20 20  pNew->aEntry;.  
1119a 20 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20 52 4f    p->nFresh = RO
1119b 57 53 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43  WSET_ENTRY_PER_C
1119c 48 55 4e 4b 3b 0a 20 20 7d 0a 20 20 70 45 6e 74  HUNK;.  }.  pEnt
1119d 72 79 20 3d 20 70 2d 3e 70 46 72 65 73 68 2b 2b  ry = p->pFresh++
1119e 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 68 2d 2d 3b  ;.  p->nFresh--;
1119f 0a 20 20 70 45 6e 74 72 79 2d 3e 76 20 3d 20 72  .  pEntry->v = r
111a0 6f 77 69 64 3b 0a 20 20 70 45 6e 74 72 79 2d 3e  owid;.  pEntry->
111a1 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 70 4c  pRight = 0;.  pL
111a2 61 73 74 20 3d 20 70 2d 3e 70 4c 61 73 74 3b 0a  ast = p->pLast;.
111a3 20 20 69 66 28 20 70 4c 61 73 74 20 29 7b 0a 20    if( pLast ){. 
111a4 20 20 20 69 66 28 20 70 2d 3e 69 73 53 6f 72 74     if( p->isSort
111a5 65 64 20 26 26 20 72 6f 77 69 64 3c 3d 70 4c 61  ed && rowid<=pLa
111a6 73 74 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20 70  st->v ){.      p
111a7 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 30 3b 0a  ->isSorted = 0;.
111a8 20 20 20 20 7d 0a 20 20 20 20 70 4c 61 73 74 2d      }.    pLast-
111a9 3e 70 52 69 67 68 74 20 3d 20 70 45 6e 74 72 79  >pRight = pEntry
111aa 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
111ab 73 73 65 72 74 28 20 70 2d 3e 70 45 6e 74 72 79  ssert( p->pEntry
111ac 3d 3d 30 20 29 3b 20 2f 2a 20 46 69 72 65 73 20  ==0 ); /* Fires 
111ad 69 66 20 49 4e 53 45 52 54 20 61 66 74 65 72 20  if INSERT after 
111ae 53 4d 41 4c 4c 45 53 54 20 2a 2f 0a 20 20 20 20  SMALLEST */.    
111af 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74  p->pEntry = pEnt
111b0 72 79 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4c 61  ry;.  }.  p->pLa
111b1 73 74 20 3d 20 70 45 6e 74 72 79 3b 0a 7d 0a 0a  st = pEntry;.}..
111b2 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20 74 77 6f 20  /*.** Merge two 
111b3 6c 69 73 74 73 20 6f 66 20 52 6f 77 53 65 74 45  lists of RowSetE
111b4 6e 74 72 79 20 6f 62 6a 65 63 74 73 2e 20 20 52  ntry objects.  R
111b5 65 6d 6f 76 65 20 64 75 70 6c 69 63 61 74 65 73  emove duplicates
111b6 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75  ..**.** The inpu
111b7 74 20 6c 69 73 74 73 20 61 72 65 20 63 6f 6e 6e  t lists are conn
111b8 65 63 74 65 64 20 76 69 61 20 70 52 69 67 68 74  ected via pRight
111b9 20 70 6f 69 6e 74 65 72 73 20 61 6e 64 20 61 72   pointers and ar
111ba 65 20 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 6f  e .** assumed to
111bb 20 65 61 63 68 20 61 6c 72 65 61 64 79 20 62 65   each already be
111bc 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72   in sorted order
111bd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75  ..*/.static stru
111be 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
111bf 72 6f 77 53 65 74 4d 65 72 67 65 28 0a 20 20 73  rowSetMerge(.  s
111c0 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
111c1 79 20 2a 70 41 2c 20 20 20 20 2f 2a 20 46 69 72  y *pA,    /* Fir
111c2 73 74 20 73 6f 72 74 65 64 20 6c 69 73 74 20 74  st sorted list t
111c3 6f 20 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a 20  o be merged */. 
111c4 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
111c5 74 72 79 20 2a 70 42 20 20 20 20 20 2f 2a 20 53  try *pB     /* S
111c6 65 63 6f 6e 64 20 73 6f 72 74 65 64 20 6c 69 73  econd sorted lis
111c7 74 20 74 6f 20 62 65 20 6d 65 72 67 65 64 20 2a  t to be merged *
111c8 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f  /.){.  struct Ro
111c9 77 53 65 74 45 6e 74 72 79 20 68 65 61 64 3b 0a  wSetEntry head;.
111ca 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
111cb 6e 74 72 79 20 2a 70 54 61 69 6c 3b 0a 0a 20 20  ntry *pTail;..  
111cc 70 54 61 69 6c 20 3d 20 26 68 65 61 64 3b 0a 20  pTail = &head;. 
111cd 20 77 68 69 6c 65 28 20 70 41 20 26 26 20 70 42   while( pA && pB
111ce 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
111cf 70 41 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c  pA->pRight==0 ||
111d0 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70 52 69 67   pA->v<=pA->pRig
111d1 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 61 73 73  ht->v );.    ass
111d2 65 72 74 28 20 70 42 2d 3e 70 52 69 67 68 74 3d  ert( pB->pRight=
111d3 3d 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d  =0 || pB->v<=pB-
111d4 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20  >pRight->v );.  
111d5 20 20 69 66 28 20 70 41 2d 3e 76 3c 70 42 2d 3e    if( pA->v<pB->
111d6 76 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c  v ){.      pTail
111d7 2d 3e 70 52 69 67 68 74 20 3d 20 70 41 3b 0a 20  ->pRight = pA;. 
111d8 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 52       pA = pA->pR
111d9 69 67 68 74 3b 0a 20 20 20 20 20 20 70 54 61 69  ight;.      pTai
111da 6c 20 3d 20 70 54 61 69 6c 2d 3e 70 52 69 67 68  l = pTail->pRigh
111db 74 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  t;.    }else if(
111dc 20 70 42 2d 3e 76 3c 70 41 2d 3e 76 20 29 7b 0a   pB->v<pA->v ){.
111dd 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 52 69        pTail->pRi
111de 67 68 74 20 3d 20 70 42 3b 0a 20 20 20 20 20 20  ght = pB;.      
111df 70 42 20 3d 20 70 42 2d 3e 70 52 69 67 68 74 3b  pB = pB->pRight;
111e0 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70  .      pTail = p
111e1 54 61 69 6c 2d 3e 70 52 69 67 68 74 3b 0a 20 20  Tail->pRight;.  
111e2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
111e3 41 20 3d 20 70 41 2d 3e 70 52 69 67 68 74 3b 0a  A = pA->pRight;.
111e4 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
111e5 70 41 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  pA ){.    assert
111e6 28 20 70 41 2d 3e 70 52 69 67 68 74 3d 3d 30 20  ( pA->pRight==0 
111e7 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70 52  || pA->v<=pA->pR
111e8 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 70  ight->v );.    p
111e9 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70  Tail->pRight = p
111ea 41 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  A;.  }else{.    
111eb 61 73 73 65 72 74 28 20 70 42 3d 3d 30 20 7c 7c  assert( pB==0 ||
111ec 20 70 42 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c   pB->pRight==0 |
111ed 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 52 69  | pB->v<=pB->pRi
111ee 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 70 54  ght->v );.    pT
111ef 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 42  ail->pRight = pB
111f0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68  ;.  }.  return h
111f1 65 61 64 2e 70 52 69 67 68 74 3b 0a 7d 0a 0a 2f  ead.pRight;.}../
111f2 2a 0a 2a 2a 20 53 6f 72 74 20 61 6c 6c 20 65 6c  *.** Sort all el
111f3 65 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 70 45  ements on the pE
111f4 6e 74 72 79 20 6c 69 73 74 20 6f 66 20 74 68 65  ntry list of the
111f5 20 52 6f 77 53 65 74 20 69 6e 74 6f 20 61 73 63   RowSet into asc
111f6 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f  ending order..*/
111f7 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f   .static void ro
111f8 77 53 65 74 53 6f 72 74 28 52 6f 77 53 65 74 20  wSetSort(RowSet 
111f9 2a 70 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  *p){.  unsigned 
111fa 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
111fb 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45 6e  RowSetEntry *pEn
111fc 74 72 79 3b 0a 20 20 73 74 72 75 63 74 20 52 6f  try;.  struct Ro
111fd 77 53 65 74 45 6e 74 72 79 20 2a 61 42 75 63 6b  wSetEntry *aBuck
111fe 65 74 5b 34 30 5d 3b 0a 0a 20 20 61 73 73 65 72  et[40];..  asser
111ff 74 28 20 70 2d 3e 69 73 53 6f 72 74 65 64 3d 3d  t( p->isSorted==
11200 30 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 61 42  0 );.  memset(aB
11201 75 63 6b 65 74 2c 20 30 2c 20 73 69 7a 65 6f 66  ucket, 0, sizeof
11202 28 61 42 75 63 6b 65 74 29 29 3b 0a 20 20 77 68  (aBucket));.  wh
11203 69 6c 65 28 20 70 2d 3e 70 45 6e 74 72 79 20 29  ile( p->pEntry )
11204 7b 0a 20 20 20 20 70 45 6e 74 72 79 20 3d 20 70  {.    pEntry = p
11205 2d 3e 70 45 6e 74 72 79 3b 0a 20 20 20 20 70 2d  ->pEntry;.    p-
11206 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 79  >pEntry = pEntry
11207 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 70 45  ->pRight;.    pE
11208 6e 74 72 79 2d 3e 70 52 69 67 68 74 20 3d 20 30  ntry->pRight = 0
11209 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 61  ;.    for(i=0; a
1120a 42 75 63 6b 65 74 5b 69 5d 3b 20 69 2b 2b 29 7b  Bucket[i]; i++){
1120b 0a 20 20 20 20 20 20 70 45 6e 74 72 79 20 3d 20  .      pEntry = 
1120c 72 6f 77 53 65 74 4d 65 72 67 65 28 61 42 75 63  rowSetMerge(aBuc
1120d 6b 65 74 5b 69 5d 2c 20 70 45 6e 74 72 79 29 3b  ket[i], pEntry);
1120e 0a 20 20 20 20 20 20 61 42 75 63 6b 65 74 5b 69  .      aBucket[i
1120f 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  ] = 0;.    }.   
11210 20 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20 70 45   aBucket[i] = pE
11211 6e 74 72 79 3b 0a 20 20 7d 0a 20 20 70 45 6e 74  ntry;.  }.  pEnt
11212 72 79 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d  ry = 0;.  for(i=
11213 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 42 75 63  0; i<sizeof(aBuc
11214 6b 65 74 29 2f 73 69 7a 65 6f 66 28 61 42 75 63  ket)/sizeof(aBuc
11215 6b 65 74 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20  ket[0]); i++){. 
11216 20 20 20 70 45 6e 74 72 79 20 3d 20 72 6f 77 53     pEntry = rowS
11217 65 74 4d 65 72 67 65 28 70 45 6e 74 72 79 2c 20  etMerge(pEntry, 
11218 61 42 75 63 6b 65 74 5b 69 5d 29 3b 0a 20 20 7d  aBucket[i]);.  }
11219 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70  .  p->pEntry = p
1121a 45 6e 74 72 79 3b 0a 20 20 70 2d 3e 70 4c 61 73  Entry;.  p->pLas
1121b 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53 6f  t = 0;.  p->isSo
1121c 72 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a  rted = 1;.}.../*
1121d 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 2c 20 70  .** The input, p
1121e 49 6e 2c 20 69 73 20 61 20 62 69 6e 61 72 79 20  In, is a binary 
1121f 74 72 65 65 20 28 6f 72 20 73 75 62 74 72 65 65  tree (or subtree
11220 29 20 6f 66 20 52 6f 77 53 65 74 45 6e 74 72 79  ) of RowSetEntry
11221 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 43 6f 6e   objects..** Con
11222 76 65 72 74 20 74 68 69 73 20 74 72 65 65 20 69  vert this tree i
11223 6e 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73  nto a linked lis
11224 74 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 74  t connected by t
11225 68 65 20 70 52 69 67 68 74 20 70 6f 69 6e 74 65  he pRight pointe
11226 72 73 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72 6e  rs.** and return
11227 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65   pointers to the
11228 20 66 69 72 73 74 20 61 6e 64 20 6c 61 73 74 20   first and last 
11229 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20  elements of the 
1122a 6e 65 77 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61  new list..*/.sta
1122b 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65 74 54  tic void rowSetT
1122c 72 65 65 54 6f 4c 69 73 74 28 0a 20 20 73 74 72  reeToList(.  str
1122d 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
1122e 2a 70 49 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a  *pIn,         /*
1122f 20 52 6f 6f 74 20 6f 66 20 74 68 65 20 69 6e 70   Root of the inp
11230 75 74 20 74 72 65 65 20 2a 2f 0a 20 20 73 74 72  ut tree */.  str
11231 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
11232 2a 2a 70 70 46 69 72 73 74 2c 20 20 20 20 2f 2a  **ppFirst,    /*
11233 20 57 72 69 74 65 20 68 65 61 64 20 6f 66 20 74   Write head of t
11234 68 65 20 6f 75 74 70 75 74 20 6c 69 73 74 20 68  he output list h
11235 65 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ere */.  struct 
11236 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 70  RowSetEntry **pp
11237 4c 61 73 74 20 20 20 20 20 20 2f 2a 20 57 72 69  Last      /* Wri
11238 74 65 20 74 61 69 6c 20 6f 66 20 74 68 65 20 6f  te tail of the o
11239 75 74 70 75 74 20 6c 69 73 74 20 68 65 72 65 20  utput list here 
1123a 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20  */.){.  assert( 
1123b 70 49 6e 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  pIn!=0 );.  if( 
1123c 70 49 6e 2d 3e 70 4c 65 66 74 20 29 7b 0a 20 20  pIn->pLeft ){.  
1123d 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
1123e 6e 74 72 79 20 2a 70 3b 0a 20 20 20 20 72 6f 77  ntry *p;.    row
1123f 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 70 49  SetTreeToList(pI
11240 6e 2d 3e 70 4c 65 66 74 2c 20 70 70 46 69 72 73  n->pLeft, ppFirs
11241 74 2c 20 26 70 29 3b 0a 20 20 20 20 70 2d 3e 70  t, &p);.    p->p
11242 52 69 67 68 74 20 3d 20 70 49 6e 3b 0a 20 20 7d  Right = pIn;.  }
11243 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 46 69 72  else{.    *ppFir
11244 73 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a 20 20  st = pIn;.  }.  
11245 69 66 28 20 70 49 6e 2d 3e 70 52 69 67 68 74 20  if( pIn->pRight 
11246 29 7b 0a 20 20 20 20 72 6f 77 53 65 74 54 72 65  ){.    rowSetTre
11247 65 54 6f 4c 69 73 74 28 70 49 6e 2d 3e 70 52 69  eToList(pIn->pRi
11248 67 68 74 2c 20 26 70 49 6e 2d 3e 70 52 69 67 68  ght, &pIn->pRigh
11249 74 2c 20 70 70 4c 61 73 74 29 3b 0a 20 20 7d 65  t, ppLast);.  }e
1124a 6c 73 65 7b 0a 20 20 20 20 2a 70 70 4c 61 73 74  lse{.    *ppLast
1124b 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a 20 20 61 73   = pIn;.  }.  as
1124c 73 65 72 74 28 20 28 2a 70 70 4c 61 73 74 29 2d  sert( (*ppLast)-
1124d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 7d 0a  >pRight==0 );.}.
1124e 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
1124f 61 20 73 6f 72 74 65 64 20 6c 69 73 74 20 6f 66  a sorted list of
11250 20 65 6c 65 6d 65 6e 74 73 20 28 63 6f 6e 6e 65   elements (conne
11251 63 74 65 64 20 62 79 20 70 52 69 67 68 74 29 20  cted by pRight) 
11252 69 6e 74 6f 20 61 20 62 69 6e 61 72 79 0a 2a 2a  into a binary.**
11253 20 74 72 65 65 20 77 69 74 68 20 64 65 70 74 68   tree with depth
11254 20 6f 66 20 69 44 65 70 74 68 2e 20 20 41 20 64   of iDepth.  A d
11255 65 70 74 68 20 6f 66 20 31 20 6d 65 61 6e 73 20  epth of 1 means 
11256 74 68 65 20 74 72 65 65 20 63 6f 6e 74 61 69 6e  the tree contain
11257 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6e 6f  s a single.** no
11258 64 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68  de taken from th
11259 65 20 68 65 61 64 20 6f 66 20 2a 70 70 4c 69 73  e head of *ppLis
1125a 74 2e 20 20 41 20 64 65 70 74 68 20 6f 66 20 32  t.  A depth of 2
1125b 20 6d 65 61 6e 73 20 61 20 74 72 65 65 20 77 69   means a tree wi
1125c 74 68 0a 2a 2a 20 74 68 72 65 65 20 6e 6f 64 65  th.** three node
1125d 73 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 68  s.  And so forth
1125e 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 61 73 20 6d  ..**.** Use as m
1125f 61 6e 79 20 65 6e 74 72 69 65 73 20 66 72 6f 6d  any entries from
11260 20 74 68 65 20 69 6e 70 75 74 20 6c 69 73 74 20   the input list 
11261 61 73 20 72 65 71 75 69 72 65 64 20 61 6e 64 20  as required and 
11262 75 70 64 61 74 65 20 74 68 65 0a 2a 2a 20 2a 70  update the.** *p
11263 70 4c 69 73 74 20 74 6f 20 70 6f 69 6e 74 20 74  pList to point t
11264 6f 20 74 68 65 20 75 6e 75 73 65 64 20 65 6c 65  o the unused ele
11265 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6c 69 73  ments of the lis
11266 74 2e 20 20 49 66 20 74 68 65 20 69 6e 70 75 74  t.  If the input
11267 0a 2a 2a 20 6c 69 73 74 20 63 6f 6e 74 61 69 6e  .** list contain
11268 73 20 74 6f 6f 20 66 65 77 20 65 6c 65 6d 65 6e  s too few elemen
11269 74 73 2c 20 74 68 65 6e 20 63 6f 6e 73 74 72 75  ts, then constru
1126a 63 74 20 61 6e 20 69 6e 63 6f 6d 70 6c 65 74 65  ct an incomplete
1126b 20 74 72 65 65 0a 2a 2a 20 61 6e 64 20 6c 65 61   tree.** and lea
1126c 76 65 20 2a 70 70 4c 69 73 74 20 73 65 74 20 74  ve *ppList set t
1126d 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65  o NULL..**.** Re
1126e 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
1126f 6f 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68  o the root of th
11270 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 69  e constructed bi
11271 6e 61 72 79 20 74 72 65 65 2e 0a 2a 2f 0a 73 74  nary tree..*/.st
11272 61 74 69 63 20 73 74 72 75 63 74 20 52 6f 77 53  atic struct RowS
11273 65 74 45 6e 74 72 79 20 2a 72 6f 77 53 65 74 4e  etEntry *rowSetN
11274 44 65 65 70 54 72 65 65 28 0a 20 20 73 74 72 75  DeepTree(.  stru
11275 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
11276 2a 70 70 4c 69 73 74 2c 0a 20 20 69 6e 74 20 69  *ppList,.  int i
11277 44 65 70 74 68 0a 29 7b 0a 20 20 73 74 72 75 63  Depth.){.  struc
11278 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70  t RowSetEntry *p
11279 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f  ;         /* Roo
1127a 74 20 6f 66 20 74 68 65 20 6e 65 77 20 74 72 65  t of the new tre
1127b 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f  e */.  struct Ro
1127c 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 65 66 74  wSetEntry *pLeft
1127d 3b 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 73 75  ;     /* Left su
1127e 62 74 72 65 65 20 2a 2f 0a 20 20 69 66 28 20 2a  btree */.  if( *
1127f 70 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20  ppList==0 ){.   
11280 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
11281 20 69 66 28 20 69 44 65 70 74 68 3d 3d 31 20 29   if( iDepth==1 )
11282 7b 0a 20 20 20 20 70 20 3d 20 2a 70 70 4c 69 73  {.    p = *ppLis
11283 74 3b 0a 20 20 20 20 2a 70 70 4c 69 73 74 20 3d  t;.    *ppList =
11284 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20   p->pRight;.    
11285 70 2d 3e 70 4c 65 66 74 20 3d 20 70 2d 3e 70 52  p->pLeft = p->pR
11286 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 72 65  ight = 0;.    re
11287 74 75 72 6e 20 70 3b 0a 20 20 7d 0a 20 20 70 4c  turn p;.  }.  pL
11288 65 66 74 20 3d 20 72 6f 77 53 65 74 4e 44 65 65  eft = rowSetNDee
11289 70 54 72 65 65 28 70 70 4c 69 73 74 2c 20 69 44  pTree(ppList, iD
1128a 65 70 74 68 2d 31 29 3b 0a 20 20 70 20 3d 20 2a  epth-1);.  p = *
1128b 70 70 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 3d  ppList;.  if( p=
1128c 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
1128d 20 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 70 2d   pLeft;.  }.  p-
1128e 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0a  >pLeft = pLeft;.
1128f 20 20 2a 70 70 4c 69 73 74 20 3d 20 70 2d 3e 70    *ppList = p->p
11290 52 69 67 68 74 3b 0a 20 20 70 2d 3e 70 52 69 67  Right;.  p->pRig
11291 68 74 20 3d 20 72 6f 77 53 65 74 4e 44 65 65 70  ht = rowSetNDeep
11292 54 72 65 65 28 70 70 4c 69 73 74 2c 20 69 44 65  Tree(ppList, iDe
11293 70 74 68 2d 31 29 3b 0a 20 20 72 65 74 75 72 6e  pth-1);.  return
11294 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e   p;.}../*.** Con
11295 76 65 72 74 20 61 20 73 6f 72 74 65 64 20 6c 69  vert a sorted li
11296 73 74 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69  st of elements i
11297 6e 74 6f 20 61 20 62 69 6e 61 72 79 20 74 72 65  nto a binary tre
11298 65 2e 20 4d 61 6b 65 20 74 68 65 20 74 72 65 65  e. Make the tree
11299 0a 2a 2a 20 61 73 20 64 65 65 70 20 61 73 20 69  .** as deep as i
1129a 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e  t needs to be in
1129b 20 6f 72 64 65 72 20 74 6f 20 63 6f 6e 74 61 69   order to contai
1129c 6e 20 74 68 65 20 65 6e 74 69 72 65 20 6c 69 73  n the entire lis
1129d 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72  t..*/.static str
1129e 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
1129f 2a 72 6f 77 53 65 74 4c 69 73 74 54 6f 54 72 65  *rowSetListToTre
112a0 65 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 45  e(struct RowSetE
112a1 6e 74 72 79 20 2a 70 4c 69 73 74 29 7b 0a 20 20  ntry *pList){.  
112a2 69 6e 74 20 69 44 65 70 74 68 3b 20 20 20 20 20  int iDepth;     
112a3 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68 20 6f        /* Depth o
112a4 66 20 74 68 65 20 74 72 65 65 20 73 6f 20 66 61  f the tree so fa
112a5 72 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f  r */.  struct Ro
112a6 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 20 20 20  wSetEntry *p;   
112a7 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 74      /* Current t
112a8 72 65 65 20 72 6f 6f 74 20 2a 2f 0a 20 20 73 74  ree root */.  st
112a9 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
112aa 20 2a 70 4c 65 66 74 3b 20 20 20 2f 2a 20 4c 65   *pLeft;   /* Le
112ab 66 74 20 73 75 62 74 72 65 65 20 2a 2f 0a 0a 20  ft subtree */.. 
112ac 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d   assert( pList!=
112ad 30 20 29 3b 0a 20 20 70 20 3d 20 70 4c 69 73 74  0 );.  p = pList
112ae 3b 0a 20 20 70 4c 69 73 74 20 3d 20 70 2d 3e 70  ;.  pList = p->p
112af 52 69 67 68 74 3b 0a 20 20 70 2d 3e 70 4c 65 66  Right;.  p->pLef
112b0 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 20 3d 20  t = p->pRight = 
112b1 30 3b 0a 20 20 66 6f 72 28 69 44 65 70 74 68 3d  0;.  for(iDepth=
112b2 31 3b 20 70 4c 69 73 74 3b 20 69 44 65 70 74 68  1; pList; iDepth
112b3 2b 2b 29 7b 0a 20 20 20 20 70 4c 65 66 74 20 3d  ++){.    pLeft =
112b4 20 70 3b 0a 20 20 20 20 70 20 3d 20 70 4c 69 73   p;.    p = pLis
112b5 74 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 70  t;.    pList = p
112b6 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 70 2d  ->pRight;.    p-
112b7 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0a  >pLeft = pLeft;.
112b8 20 20 20 20 70 2d 3e 70 52 69 67 68 74 20 3d 20      p->pRight = 
112b9 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 65 28  rowSetNDeepTree(
112ba 26 70 4c 69 73 74 2c 20 69 44 65 70 74 68 29 3b  &pList, iDepth);
112bb 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
112bc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
112bd 74 20 74 68 65 20 6c 69 73 74 20 69 6e 20 70 2d  t the list in p-
112be 3e 70 45 6e 74 72 79 20 69 6e 74 6f 20 61 20 73  >pEntry into a s
112bf 6f 72 74 65 64 20 6c 69 73 74 20 69 66 20 69 74  orted list if it
112c0 20 69 73 20 6e 6f 74 0a 2a 2a 20 73 6f 72 74 65   is not.** sorte
112c1 64 20 61 6c 72 65 61 64 79 2e 20 20 49 66 20 74  d already.  If t
112c2 68 65 72 65 20 69 73 20 61 20 62 69 6e 61 72 79  here is a binary
112c3 20 74 72 65 65 20 6f 6e 20 70 2d 3e 70 54 72 65   tree on p->pTre
112c4 65 2c 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76 65  e, then.** conve
112c5 72 74 20 69 74 20 69 6e 74 6f 20 61 20 6c 69 73  rt it into a lis
112c6 74 20 74 6f 6f 20 61 6e 64 20 6d 65 72 67 65 20  t too and merge 
112c7 69 74 20 69 6e 74 6f 20 74 68 65 20 70 2d 3e 70  it into the p->p
112c8 45 6e 74 72 79 20 6c 69 73 74 2e 0a 2a 2f 0a 73  Entry list..*/.s
112c9 74 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65  tatic void rowSe
112ca 74 54 6f 4c 69 73 74 28 52 6f 77 53 65 74 20 2a  tToList(RowSet *
112cb 70 29 7b 0a 20 20 69 66 28 20 21 70 2d 3e 69 73  p){.  if( !p->is
112cc 53 6f 72 74 65 64 20 29 7b 0a 20 20 20 20 72 6f  Sorted ){.    ro
112cd 77 53 65 74 53 6f 72 74 28 70 29 3b 0a 20 20 7d  wSetSort(p);.  }
112ce 0a 20 20 69 66 28 20 70 2d 3e 70 54 72 65 65 20  .  if( p->pTree 
112cf 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 52 6f  ){.    struct Ro
112d0 77 53 65 74 45 6e 74 72 79 20 2a 70 48 65 61 64  wSetEntry *pHead
112d1 2c 20 2a 70 54 61 69 6c 3b 0a 20 20 20 20 72 6f  , *pTail;.    ro
112d2 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 70  wSetTreeToList(p
112d3 2d 3e 70 54 72 65 65 2c 20 26 70 48 65 61 64 2c  ->pTree, &pHead,
112d4 20 26 70 54 61 69 6c 29 3b 0a 20 20 20 20 70 2d   &pTail);.    p-
112d5 3e 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 20 20  >pTree = 0;.    
112d6 70 2d 3e 70 45 6e 74 72 79 20 3d 20 72 6f 77 53  p->pEntry = rowS
112d7 65 74 4d 65 72 67 65 28 70 2d 3e 70 45 6e 74 72  etMerge(p->pEntr
112d8 79 2c 20 70 48 65 61 64 29 3b 0a 20 20 7d 0a 7d  y, pHead);.  }.}
112d9 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20  ../*.** Extract 
112da 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6c 65  the smallest ele
112db 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 52 6f  ment from the Ro
112dc 77 53 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 74  wSet..** Write t
112dd 68 65 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20  he element into 
112de 2a 70 52 6f 77 69 64 2e 20 20 52 65 74 75 72 6e  *pRowid.  Return
112df 20 31 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20   1 on success.  
112e0 52 65 74 75 72 6e 0a 2a 2a 20 30 20 69 66 20 74  Return.** 0 if t
112e1 68 65 20 52 6f 77 53 65 74 20 69 73 20 61 6c 72  he RowSet is alr
112e2 65 61 64 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a  eady empty..**.*
112e3 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75  * After this rou
112e4 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20 63 61  tine has been ca
112e5 6c 6c 65 64 2c 20 74 68 65 20 73 71 6c 69 74 65  lled, the sqlite
112e6 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 29 0a  3RowSetInsert().
112e7 2a 2a 20 72 6f 75 74 69 6e 65 20 6d 61 79 20 6e  ** routine may n
112e8 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61  ot be called aga
112e9 69 6e 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f  in.  .*/.SQLITE_
112ea 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
112eb 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28 52 6f  te3RowSetNext(Ro
112ec 77 53 65 74 20 2a 70 2c 20 69 36 34 20 2a 70 52  wSet *p, i64 *pR
112ed 6f 77 69 64 29 7b 0a 20 20 72 6f 77 53 65 74 54  owid){.  rowSetT
112ee 6f 4c 69 73 74 28 70 29 3b 0a 20 20 69 66 28 20  oList(p);.  if( 
112ef 70 2d 3e 70 45 6e 74 72 79 20 29 7b 0a 20 20 20  p->pEntry ){.   
112f0 20 2a 70 52 6f 77 69 64 20 3d 20 70 2d 3e 70 45   *pRowid = p->pE
112f1 6e 74 72 79 2d 3e 76 3b 0a 20 20 20 20 70 2d 3e  ntry->v;.    p->
112f2 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 45 6e 74  pEntry = p->pEnt
112f3 72 79 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20  ry->pRight;.    
112f4 69 66 28 20 70 2d 3e 70 45 6e 74 72 79 3d 3d 30  if( p->pEntry==0
112f5 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
112f6 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 29 3b  3RowSetClear(p);
112f7 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
112f8 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  n 1;.  }else{.  
112f9 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
112fa 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  }../*.** Check t
112fb 6f 20 73 65 65 20 69 66 20 65 6c 65 6d 65 6e 74  o see if element
112fc 20 69 52 6f 77 69 64 20 77 61 73 20 69 6e 73 65   iRowid was inse
112fd 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74 68  rted into the th
112fe 65 20 72 6f 77 73 65 74 20 61 73 0a 2a 2a 20 70  e rowset as.** p
112ff 61 72 74 20 6f 66 20 61 6e 79 20 69 6e 73 65 72  art of any inser
11300 74 20 62 61 74 63 68 20 70 72 69 6f 72 20 74 6f  t batch prior to
11301 20 69 42 61 74 63 68 2e 20 20 52 65 74 75 72 6e   iBatch.  Return
11302 20 31 20 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49   1 or 0..*/.SQLI
11303 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
11304 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73 74  qlite3RowSetTest
11305 28 52 6f 77 53 65 74 20 2a 70 52 6f 77 53 65 74  (RowSet *pRowSet
11306 2c 20 75 38 20 69 42 61 74 63 68 2c 20 73 71 6c  , u8 iBatch, sql
11307 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69  ite3_int64 iRowi
11308 64 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77  d){.  struct Row
11309 53 65 74 45 6e 74 72 79 20 2a 70 3b 0a 20 20 69  SetEntry *p;.  i
1130a 66 28 20 69 42 61 74 63 68 21 3d 70 52 6f 77 53  f( iBatch!=pRowS
1130b 65 74 2d 3e 69 42 61 74 63 68 20 29 7b 0a 20 20  et->iBatch ){.  
1130c 20 20 69 66 28 20 70 52 6f 77 53 65 74 2d 3e 70    if( pRowSet->p
1130d 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 20 20 72  Entry ){.      r
1130e 6f 77 53 65 74 54 6f 4c 69 73 74 28 70 52 6f 77  owSetToList(pRow
1130f 53 65 74 29 3b 0a 20 20 20 20 20 20 70 52 6f 77  Set);.      pRow
11310 53 65 74 2d 3e 70 54 72 65 65 20 3d 20 72 6f 77  Set->pTree = row
11311 53 65 74 4c 69 73 74 54 6f 54 72 65 65 28 70 52  SetListToTree(pR
11312 6f 77 53 65 74 2d 3e 70 45 6e 74 72 79 29 3b 0a  owSet->pEntry);.
11313 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d 3e 70        pRowSet->p
11314 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 20 20 20  Entry = 0;.     
11315 20 70 52 6f 77 53 65 74 2d 3e 70 4c 61 73 74 20   pRowSet->pLast 
11316 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  = 0;.    }.    p
11317 52 6f 77 53 65 74 2d 3e 69 42 61 74 63 68 20 3d  RowSet->iBatch =
11318 20 69 42 61 74 63 68 3b 0a 20 20 7d 0a 20 20 70   iBatch;.  }.  p
11319 20 3d 20 70 52 6f 77 53 65 74 2d 3e 70 54 72 65   = pRowSet->pTre
1131a 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b  e;.  while( p ){
1131b 0a 20 20 20 20 69 66 28 20 70 2d 3e 76 3c 69 52  .    if( p->v<iR
1131c 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 70 20  owid ){.      p 
1131d 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20  = p->pRight;.   
1131e 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 76 3e   }else if( p->v>
1131f 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20  iRowid ){.      
11320 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20  p = p->pLeft;.  
11321 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
11322 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
11323 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
11324 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
11325 2a 20 45 6e 64 20 6f 66 20 72 6f 77 73 65 74 2e  * End of rowset.
11326 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
11327 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11328 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11329 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
1132a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 61 67  * Begin file pag
1132b 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
1132c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1132d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1132e 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
1132f 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
11330 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
11331 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
11332 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
11333 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
11334 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
11335 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
11336 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
11337 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
11338 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
11339 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
1133a 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
1133b 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
1133c 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
1133d 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
1133e 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
1133f 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
11340 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
11341 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11342 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11343 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11344 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11345 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
11346 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e   is the implemen
11347 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 61  tation of the pa
11348 67 65 20 63 61 63 68 65 20 73 75 62 73 79 73 74  ge cache subsyst
11349 65 6d 20 6f 72 20 22 70 61 67 65 72 22 2e 0a 2a  em or "pager"..*
1134a 2a 20 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20  * .** The pager 
1134b 69 73 20 75 73 65 64 20 74 6f 20 61 63 63 65 73  is used to acces
1134c 73 20 61 20 64 61 74 61 62 61 73 65 20 64 69 73  s a database dis
1134d 6b 20 66 69 6c 65 2e 20 20 49 74 20 69 6d 70 6c  k file.  It impl
1134e 65 6d 65 6e 74 73 0a 2a 2a 20 61 74 6f 6d 69 63  ements.** atomic
1134f 20 63 6f 6d 6d 69 74 20 61 6e 64 20 72 6f 6c 6c   commit and roll
11350 62 61 63 6b 20 74 68 72 6f 75 67 68 20 74 68 65  back through the
11351 20 75 73 65 20 6f 66 20 61 20 6a 6f 75 72 6e 61   use of a journa
11352 6c 20 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20 69  l file that.** i
11353 73 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20  s separate from 
11354 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
11355 65 2e 20 20 54 68 65 20 70 61 67 65 72 20 61 6c  e.  The pager al
11356 73 6f 20 69 6d 70 6c 65 6d 65 6e 74 73 20 66 69  so implements fi
11357 6c 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 74 6f  le.** locking to
11358 20 70 72 65 76 65 6e 74 20 74 77 6f 20 70 72 6f   prevent two pro
11359 63 65 73 73 65 73 20 66 72 6f 6d 20 77 72 69 74  cesses from writ
1135a 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74  ing the same dat
1135b 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 73 69  abase.** file si
1135c 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2c 20 6f 72  multaneously, or
1135d 20 6f 6e 65 20 70 72 6f 63 65 73 73 20 66 72 6f   one process fro
1135e 6d 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61  m reading the da
1135f 74 61 62 61 73 65 20 77 68 69 6c 65 0a 2a 2a 20  tabase while.** 
11360 61 6e 6f 74 68 65 72 20 69 73 20 77 72 69 74 69  another is writi
11361 6e 67 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24  ng..**.** @(#) $
11362 49 64 3a 20 70 61 67 65 72 2e 63 2c 76 20 31 2e  Id: pager.c,v 1.
11363 36 32 39 20 32 30 30 39 2f 30 38 2f 31 30 20 31  629 2009/08/10 1
11364 37 3a 34 38 3a 35 37 20 64 72 68 20 45 78 70 20  7:48:57 drh Exp 
11365 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  $.*/.#ifndef SQL
11366 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a  ITE_OMIT_DISKIO.
11367 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f  ./*.** Macros fo
11368 72 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e  r troubleshootin
11369 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72  g.  Normally tur
1136a 6e 65 64 20 6f 66 66 0a 2a 2f 0a 23 69 66 20 30  ned off.*/.#if 0
1136b 0a 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65  .int sqlite3Page
1136c 72 54 72 61 63 65 3d 31 3b 20 20 2f 2a 20 54 72  rTrace=1;  /* Tr
1136d 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 74 72 61  ue to enable tra
1136e 63 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20  cing */.#define 
1136f 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
11370 74 66 20 70 72 69 6e 74 66 0a 23 64 65 66 69 6e  tf printf.#defin
11371 65 20 50 41 47 45 52 54 52 41 43 45 28 58 29 20  e PAGERTRACE(X) 
11372 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 50      if( sqlite3P
11373 61 67 65 72 54 72 61 63 65 20 29 7b 20 73 71 6c  agerTrace ){ sql
11374 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 20  ite3DebugPrintf 
11375 58 3b 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69  X; }.#else.#defi
11376 6e 65 20 50 41 47 45 52 54 52 41 43 45 28 58 29  ne PAGERTRACE(X)
11377 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
11378 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f  he following two
11379 20 6d 61 63 72 6f 73 20 61 72 65 20 75 73 65 64   macros are used
1137a 20 77 69 74 68 69 6e 20 74 68 65 20 50 41 47 45   within the PAGE
1137b 52 54 52 41 43 45 28 29 20 6d 61 63 72 6f 73 20  RTRACE() macros 
1137c 61 62 6f 76 65 0a 2a 2a 20 74 6f 20 70 72 69 6e  above.** to prin
1137d 74 20 6f 75 74 20 66 69 6c 65 2d 64 65 73 63 72  t out file-descr
1137e 69 70 74 6f 72 73 2e 20 0a 2a 2a 0a 2a 2a 20 50  iptors. .**.** P
1137f 41 47 45 52 49 44 28 29 20 74 61 6b 65 73 20 61  AGERID() takes a
11380 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 50 61   pointer to a Pa
11381 67 65 72 20 73 74 72 75 63 74 20 61 73 20 69 74  ger struct as it
11382 73 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 65 0a  s argument. The.
11383 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 66 69  ** associated fi
11384 6c 65 2d 64 65 73 63 72 69 70 74 6f 72 20 69 73  le-descriptor is
11385 20 72 65 74 75 72 6e 65 64 2e 20 46 49 4c 45 48   returned. FILEH
11386 41 4e 44 4c 45 49 44 28 29 20 74 61 6b 65 73 20  ANDLEID() takes 
11387 61 6e 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 0a  an sqlite3_file.
11388 2a 2a 20 73 74 72 75 63 74 20 61 73 20 69 74 73  ** struct as its
11389 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 64   argument..*/.#d
1138a 65 66 69 6e 65 20 50 41 47 45 52 49 44 28 70 29  efine PAGERID(p)
1138b 20 28 28 69 6e 74 29 28 70 2d 3e 66 64 29 29 0a   ((int)(p->fd)).
1138c 23 64 65 66 69 6e 65 20 46 49 4c 45 48 41 4e 44  #define FILEHAND
1138d 4c 45 49 44 28 66 64 29 20 28 28 69 6e 74 29 66  LEID(fd) ((int)f
1138e 64 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61  d)../*.** The pa
1138f 67 65 20 63 61 63 68 65 20 61 73 20 61 20 77 68  ge cache as a wh
11390 6f 6c 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e  ole is always in
11391 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
11392 6f 77 69 6e 67 0a 2a 2a 20 73 74 61 74 65 73 3a  owing.** states:
11393 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 55  .**.**   PAGER_U
11394 4e 4c 4f 43 4b 20 20 20 20 20 20 20 20 54 68 65  NLOCK        The
11395 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 6e   page cache is n
11396 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 72 65 61  ot currently rea
11397 64 69 6e 67 20 6f 72 20 0a 2a 2a 20 20 20 20 20  ding or .**     
11398 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11399 20 20 77 72 69 74 69 6e 67 20 74 68 65 20 64 61    writing the da
1139a 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68  tabase file.  Th
1139b 65 72 65 20 69 73 20 6e 6f 0a 2a 2a 20 20 20 20  ere is no.**    
1139c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1139d 20 20 20 64 61 74 61 20 68 65 6c 64 20 69 6e 20     data held in 
1139e 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73 20 69 73  memory.  This is
1139f 20 74 68 65 20 69 6e 69 74 69 61 6c 0a 2a 2a 20   the initial.** 
113a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113a1 20 20 20 20 20 20 73 74 61 74 65 2e 0a 2a 2a 0a        state..**.
113a2 2a 2a 20 20 20 50 41 47 45 52 5f 53 48 41 52 45  **   PAGER_SHARE
113a3 44 20 20 20 20 20 20 20 20 54 68 65 20 70 61 67  D        The pag
113a4 65 20 63 61 63 68 65 20 69 73 20 72 65 61 64 69  e cache is readi
113a5 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  ng the database.
113a6 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
113a7 20 20 20 20 20 20 20 20 20 20 57 72 69 74 69 6e            Writin
113a8 67 20 69 73 20 6e 6f 74 20 70 65 72 6d 69 74 74  g is not permitt
113a9 65 64 2e 20 20 54 68 65 72 65 20 63 61 6e 20 62  ed.  There can b
113aa 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
113ab 20 20 20 20 20 20 20 20 20 20 20 6d 75 6c 74 69             multi
113ac 70 6c 65 20 72 65 61 64 65 72 73 20 61 63 63 65  ple readers acce
113ad 73 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64  ssing the same d
113ae 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 20  atabase.**      
113af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113b0 20 66 69 6c 65 20 61 74 20 74 68 65 20 73 61 6d   file at the sam
113b1 65 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  e time..**.**   
113b2 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 20  PAGER_RESERVED  
113b3 20 20 20 20 54 68 69 73 20 70 72 6f 63 65 73 73      This process
113b4 20 68 61 73 20 72 65 73 65 72 76 65 64 20 74 68   has reserved th
113b5 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 77  e database for w
113b6 72 69 74 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20  riting.**       
113b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113b8 62 75 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20  but has not yet 
113b9 6d 61 64 65 20 61 6e 79 20 63 68 61 6e 67 65 73  made any changes
113ba 2e 20 20 4f 6e 6c 79 20 6f 6e 65 20 70 72 6f 63  .  Only one proc
113bb 65 73 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ess.**          
113bc 20 20 20 20 20 20 20 20 20 20 20 20 20 61 74 20               at 
113bd 61 20 74 69 6d 65 20 63 61 6e 20 72 65 73 65 72  a time can reser
113be 76 65 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  ve the database.
113bf 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a    The original.*
113c0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
113c1 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65          database
113c2 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 62 65   file has not be
113c3 65 6e 20 6d 6f 64 69 66 69 65 64 20 73 6f 20 6f  en modified so o
113c4 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  ther.**         
113c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 72                pr
113c6 6f 63 65 73 73 65 73 20 6d 61 79 20 73 74 69 6c  ocesses may stil
113c7 6c 20 62 65 20 72 65 61 64 69 6e 67 20 74 68 65  l be reading the
113c8 20 6f 6e 2d 64 69 73 6b 0a 2a 2a 20 20 20 20 20   on-disk.**     
113c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113ca 20 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e    database file.
113cb 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 45  .**.**   PAGER_E
113cc 58 43 4c 55 53 49 56 45 20 20 20 20 20 54 68 65  XCLUSIVE     The
113cd 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 77   page cache is w
113ce 72 69 74 69 6e 67 20 74 68 65 20 64 61 74 61 62  riting the datab
113cf 61 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20  ase..**         
113d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 41 63                Ac
113d1 63 65 73 73 20 69 73 20 65 78 63 6c 75 73 69 76  cess is exclusiv
113d2 65 2e 20 20 4e 6f 20 6f 74 68 65 72 20 70 72 6f  e.  No other pro
113d3 63 65 73 73 65 73 20 6f 72 0a 2a 2a 20 20 20 20  cesses or.**    
113d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113d5 20 20 20 74 68 72 65 61 64 73 20 63 61 6e 20 62     threads can b
113d6 65 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69  e reading or wri
113d7 74 69 6e 67 20 77 68 69 6c 65 20 6f 6e 65 0a 2a  ting while one.*
113d8 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
113d9 20 20 20 20 20 20 20 20 70 72 6f 63 65 73 73 20          process 
113da 69 73 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a  is writing..**.*
113db 2a 20 20 20 50 41 47 45 52 5f 53 59 4e 43 45 44  *   PAGER_SYNCED
113dc 20 20 20 20 20 20 20 20 54 68 65 20 70 61 67 65          The page
113dd 72 20 6d 6f 76 65 73 20 74 6f 20 74 68 69 73 20  r moves to this 
113de 73 74 61 74 65 20 66 72 6f 6d 20 50 41 47 45 52  state from PAGER
113df 5f 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20  _EXCLUSIVE.**   
113e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113e1 20 20 20 20 61 66 74 65 72 20 61 6c 6c 20 64 69      after all di
113e2 72 74 79 20 70 61 67 65 73 20 68 61 76 65 20 62  rty pages have b
113e3 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74  een written to t
113e4 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  he.**           
113e5 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61              data
113e6 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 74 68  base file and th
113e7 65 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20  e file has been 
113e8 73 79 6e 63 65 64 20 74 6f 0a 2a 2a 20 20 20 20  synced to.**    
113e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113ea 20 20 20 64 69 73 6b 2e 20 41 6c 6c 20 74 68 61     disk. All tha
113eb 74 20 72 65 6d 61 69 6e 73 20 74 6f 20 64 6f 20  t remains to do 
113ec 69 73 20 74 6f 20 72 65 6d 6f 76 65 20 6f 72 0a  is to remove or.
113ed 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
113ee 20 20 20 20 20 20 20 20 20 74 72 75 6e 63 61 74           truncat
113ef 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  e the journal fi
113f0 6c 65 20 61 6e 64 20 74 68 65 20 74 72 61 6e 73  le and the trans
113f1 61 63 74 69 6f 6e 20 0a 2a 2a 20 20 20 20 20 20  action .**      
113f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113f3 20 77 69 6c 6c 20 62 65 20 63 6f 6d 6d 69 74 74   will be committ
113f4 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61  ed..**.** The pa
113f5 67 65 20 63 61 63 68 65 20 63 6f 6d 65 73 20 75  ge cache comes u
113f6 70 20 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43  p in PAGER_UNLOC
113f7 4b 2e 20 20 54 68 65 20 66 69 72 73 74 20 74 69  K.  The first ti
113f8 6d 65 20 61 0a 2a 2a 20 73 71 6c 69 74 65 33 50  me a.** sqlite3P
113f9 61 67 65 72 47 65 74 28 29 20 6f 63 63 75 72 73  agerGet() occurs
113fa 2c 20 74 68 65 20 73 74 61 74 65 20 74 72 61 6e  , the state tran
113fb 73 69 74 69 6f 6e 73 20 74 6f 20 50 41 47 45 52  sitions to PAGER
113fc 5f 53 48 41 52 45 44 2e 0a 2a 2a 20 41 66 74 65  _SHARED..** Afte
113fd 72 20 61 6c 6c 20 70 61 67 65 73 20 68 61 76 65  r all pages have
113fe 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 20 75   been released u
113ff 73 69 6e 67 20 73 71 6c 69 74 65 5f 70 61 67 65  sing sqlite_page
11400 5f 75 6e 72 65 66 28 29 2c 0a 2a 2a 20 74 68 65  _unref(),.** the
11401 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f   state transitio
11402 6e 73 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52  ns back to PAGER
11403 5f 55 4e 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69  _UNLOCK.  The fi
11404 72 73 74 20 74 69 6d 65 0a 2a 2a 20 74 68 61 74  rst time.** that
11405 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
11406 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 2c 20  te() is called, 
11407 74 68 65 20 73 74 61 74 65 20 74 72 61 6e 73 69  the state transi
11408 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 50 41 47 45  tions to.** PAGE
11409 52 5f 52 45 53 45 52 56 45 44 2e 20 20 28 4e 6f  R_RESERVED.  (No
1140a 74 65 20 74 68 61 74 20 73 71 6c 69 74 65 33 50  te that sqlite3P
1140b 61 67 65 72 57 72 69 74 65 28 29 20 63 61 6e 20  agerWrite() can 
1140c 6f 6e 6c 79 20 62 65 0a 2a 2a 20 63 61 6c 6c 65  only be.** calle
1140d 64 20 6f 6e 20 61 6e 20 6f 75 74 73 74 61 6e 64  d on an outstand
1140e 69 6e 67 20 70 61 67 65 20 77 68 69 63 68 20 6d  ing page which m
1140f 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 70 61  eans that the pa
11410 67 65 72 20 6d 75 73 74 0a 2a 2a 20 62 65 20 69  ger must.** be i
11411 6e 20 50 41 47 45 52 5f 53 48 41 52 45 44 20 62  n PAGER_SHARED b
11412 65 66 6f 72 65 20 69 74 20 74 72 61 6e 73 69 74  efore it transit
11413 69 6f 6e 73 20 74 6f 20 50 41 47 45 52 5f 52 45  ions to PAGER_RE
11414 53 45 52 56 45 44 2e 29 0a 2a 2a 20 50 41 47 45  SERVED.).** PAGE
11415 52 5f 52 45 53 45 52 56 45 44 20 6d 65 61 6e 73  R_RESERVED means
11416 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 61   that there is a
11417 6e 20 6f 70 65 6e 20 72 6f 6c 6c 62 61 63 6b 20  n open rollback 
11418 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20  journal..** The 
11419 74 72 61 6e 73 69 74 69 6f 6e 20 74 6f 20 50 41  transition to PA
1141a 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 6f 63  GER_EXCLUSIVE oc
1141b 63 75 72 73 20 62 65 66 6f 72 65 20 61 6e 79 20  curs before any 
1141c 63 68 61 6e 67 65 73 0a 2a 2a 20 61 72 65 20 6d  changes.** are m
1141d 61 64 65 20 74 6f 20 74 68 65 20 64 61 74 61 62  ade to the datab
1141e 61 73 65 20 66 69 6c 65 2c 20 74 68 6f 75 67 68  ase file, though
1141f 20 77 72 69 74 65 73 20 74 6f 20 74 68 65 20 72   writes to the r
11420 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6a 6f 75 72 6e  ollback.** journ
11421 61 6c 20 6f 63 63 75 72 73 20 77 69 74 68 20 6a  al occurs with j
11422 75 73 74 20 50 41 47 45 52 5f 52 45 53 45 52 56  ust PAGER_RESERV
11423 45 44 2e 20 20 41 66 74 65 72 20 61 6e 20 73 71  ED.  After an sq
11424 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61  lite3PagerRollba
11425 63 6b 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74  ck().** or sqlit
11426 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61  e3PagerCommitPha
11427 73 65 54 77 6f 28 29 2c 20 74 68 65 20 73 74 61  seTwo(), the sta
11428 74 65 20 63 61 6e 20 67 6f 20 62 61 63 6b 20 74  te can go back t
11429 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44 2c 0a  o PAGER_SHARED,.
1142a 2a 2a 20 6f 72 20 69 74 20 63 61 6e 20 73 74 61  ** or it can sta
1142b 79 20 61 74 20 50 41 47 45 52 5f 45 58 43 4c 55  y at PAGER_EXCLU
1142c 53 49 56 45 20 69 66 20 77 65 20 61 72 65 20 69  SIVE if we are i
1142d 6e 20 65 78 63 6c 75 73 69 76 65 20 61 63 63 65  n exclusive acce
1142e 73 73 20 6d 6f 64 65 2e 0a 2a 2f 0a 23 64 65 66  ss mode..*/.#def
1142f 69 6e 65 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b  ine PAGER_UNLOCK
11430 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20        0.#define 
11431 50 41 47 45 52 5f 53 48 41 52 45 44 20 20 20 20  PAGER_SHARED    
11432 20 20 31 20 20 20 2f 2a 20 73 61 6d 65 20 61 73    1   /* same as
11433 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a   SHARED_LOCK */.
11434 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 52 45  #define PAGER_RE
11435 53 45 52 56 45 44 20 20 20 20 32 20 20 20 2f 2a  SERVED    2   /*
11436 20 73 61 6d 65 20 61 73 20 52 45 53 45 52 56 45   same as RESERVE
11437 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e  D_LOCK */.#defin
11438 65 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  e PAGER_EXCLUSIV
11439 45 20 20 20 34 20 20 20 2f 2a 20 73 61 6d 65 20  E   4   /* same 
1143a 61 73 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  as EXCLUSIVE_LOC
1143b 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47  K */.#define PAG
1143c 45 52 5f 53 59 4e 43 45 44 20 20 20 20 20 20 35  ER_SYNCED      5
1143d 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f 20  ../*.** A macro 
1143e 75 73 65 64 20 66 6f 72 20 69 6e 76 6f 6b 69 6e  used for invokin
1143f 67 20 74 68 65 20 63 6f 64 65 63 20 69 66 20 74  g the codec if t
11440 68 65 72 65 20 69 73 20 6f 6e 65 0a 2a 2f 0a 23  here is one.*/.#
11441 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53  ifdef SQLITE_HAS
11442 5f 43 4f 44 45 43 0a 23 20 64 65 66 69 6e 65 20  _CODEC.# define 
11443 43 4f 44 45 43 31 28 50 2c 44 2c 4e 2c 58 2c 45  CODEC1(P,D,N,X,E
11444 29 20 5c 0a 20 20 20 20 69 66 28 20 50 2d 3e 78  ) \.    if( P->x
11445 43 6f 64 65 63 20 26 26 20 50 2d 3e 78 43 6f 64  Codec && P->xCod
11446 65 63 28 50 2d 3e 70 43 6f 64 65 63 2c 44 2c 4e  ec(P->pCodec,D,N
11447 2c 58 29 3d 3d 30 20 29 7b 20 45 3b 20 7d 0a 23  ,X)==0 ){ E; }.#
11448 20 64 65 66 69 6e 65 20 43 4f 44 45 43 32 28 50   define CODEC2(P
11449 2c 44 2c 4e 2c 58 2c 45 2c 4f 29 20 5c 0a 20 20  ,D,N,X,E,O) \.  
1144a 20 20 69 66 28 20 50 2d 3e 78 43 6f 64 65 63 3d    if( P->xCodec=
1144b 3d 30 20 29 7b 20 4f 3d 28 63 68 61 72 2a 29 44  =0 ){ O=(char*)D
1144c 3b 20 7d 65 6c 73 65 20 5c 0a 20 20 20 20 69 66  ; }else \.    if
1144d 28 20 28 4f 3d 28 63 68 61 72 2a 29 28 50 2d 3e  ( (O=(char*)(P->
1144e 78 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63  xCodec(P->pCodec
1144f 2c 44 2c 4e 2c 58 29 29 29 3d 3d 30 20 29 7b 20  ,D,N,X)))==0 ){ 
11450 45 3b 20 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66  E; }.#else.# def
11451 69 6e 65 20 43 4f 44 45 43 31 28 50 2c 44 2c 4e  ine CODEC1(P,D,N
11452 2c 58 2c 45 29 20 20 20 2f 2a 20 4e 4f 2d 4f 50  ,X,E)   /* NO-OP
11453 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 43 4f 44   */.# define COD
11454 45 43 32 28 50 2c 44 2c 4e 2c 58 2c 45 2c 4f 29  EC2(P,D,N,X,E,O)
11455 20 4f 3d 28 63 68 61 72 2a 29 44 0a 23 65 6e 64   O=(char*)D.#end
11456 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61  if../*.** The ma
11457 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 65  ximum allowed se
11458 63 74 6f 72 20 73 69 7a 65 2e 20 36 34 4b 69 42  ctor size. 64KiB
11459 2e 20 49 66 20 74 68 65 20 78 53 65 63 74 6f 72  . If the xSector
1145a 73 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 0a 2a  size() method .*
1145b 2a 20 72 65 74 75 72 6e 73 20 61 20 76 61 6c 75  * returns a valu
1145c 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  e larger than th
1145d 69 73 2c 20 74 68 65 6e 20 4d 41 58 5f 53 45 43  is, then MAX_SEC
1145e 54 4f 52 5f 53 49 5a 45 20 69 73 20 75 73 65 64  TOR_SIZE is used
1145f 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 20 54 68 69   instead..** Thi
11460 73 20 63 6f 75 6c 64 20 63 6f 6e 63 65 69 76 61  s could conceiva
11461 62 6c 79 20 63 61 75 73 65 20 63 6f 72 72 75 70  bly cause corrup
11462 74 69 6f 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 61  tion following a
11463 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f   power failure o
11464 6e 0a 2a 2a 20 73 75 63 68 20 61 20 73 79 73 74  n.** such a syst
11465 65 6d 2e 20 54 68 69 73 20 69 73 20 63 75 72 72  em. This is curr
11466 65 6e 74 6c 79 20 61 6e 20 75 6e 64 6f 63 75 6d  ently an undocum
11467 65 6e 74 65 64 20 6c 69 6d 69 74 2e 0a 2a 2f 0a  ented limit..*/.
11468 23 64 65 66 69 6e 65 20 4d 41 58 5f 53 45 43 54  #define MAX_SECT
11469 4f 52 5f 53 49 5a 45 20 30 78 31 30 30 30 30 0a  OR_SIZE 0x10000.
1146a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
1146b 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
1146c 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73  ing structure is
1146d 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65   allocated for e
1146e 61 63 68 20 61 63 74 69 76 65 0a 2a 2a 20 73 61  ach active.** sa
1146f 76 65 70 6f 69 6e 74 20 61 6e 64 20 73 74 61 74  vepoint and stat
11470 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
11471 6e 20 69 6e 20 74 68 65 20 73 79 73 74 65 6d 2e  n in the system.
11472 20 41 6c 6c 20 73 75 63 68 20 73 74 72 75 63 74   All such struct
11473 75 72 65 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72  ures.** are stor
11474 65 64 20 69 6e 20 74 68 65 20 50 61 67 65 72 2e  ed in the Pager.
11475 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20 61 72 72  aSavepoint[] arr
11476 61 79 2c 20 77 68 69 63 68 20 69 73 20 61 6c 6c  ay, which is all
11477 6f 63 61 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65  ocated and.** re
11478 73 69 7a 65 64 20 75 73 69 6e 67 20 73 71 6c 69  sized using sqli
11479 74 65 33 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a  te3Realloc()..**
1147a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 61 76 65 70  .** When a savep
1147b 6f 69 6e 74 20 69 73 20 63 72 65 61 74 65 64 2c  oint is created,
1147c 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f   the PagerSavepo
1147d 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 66  int.iHdrOffset f
1147e 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65 74 20 74  ield is.** set t
1147f 6f 20 30 2e 20 49 66 20 61 20 6a 6f 75 72 6e 61  o 0. If a journa
11480 6c 2d 68 65 61 64 65 72 20 69 73 20 77 72 69 74  l-header is writ
11481 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6d 61 69  ten into the mai
11482 6e 20 6a 6f 75 72 6e 61 6c 20 77 68 69 6c 65 0a  n journal while.
11483 2a 2a 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74  ** the savepoint
11484 20 69 73 20 61 63 74 69 76 65 2c 20 74 68 65 6e   is active, then
11485 20 69 48 64 72 4f 66 66 73 65 74 20 69 73 20 73   iHdrOffset is s
11486 65 74 20 74 6f 20 74 68 65 20 62 79 74 65 20 6f  et to the byte o
11487 66 66 73 65 74 20 0a 2a 2a 20 69 6d 6d 65 64 69  ffset .** immedi
11488 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20  ately following 
11489 74 68 65 20 6c 61 73 74 20 6a 6f 75 72 6e 61 6c  the last journal
1148a 20 72 65 63 6f 72 64 20 77 72 69 74 74 65 6e 20   record written 
1148b 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 0a 2a 2a  into the main.**
1148c 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 20   journal before 
1148d 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64  the journal-head
1148e 65 72 2e 20 54 68 69 73 20 69 73 20 72 65 71 75  er. This is requ
1148f 69 72 65 64 20 64 75 72 69 6e 67 20 73 61 76 65  ired during save
11490 70 6f 69 6e 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63  point.** rollbac
11491 6b 20 28 73 65 65 20 70 61 67 65 72 50 6c 61 79  k (see pagerPlay
11492 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 28 29 29  backSavepoint())
11493 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
11494 75 63 74 20 50 61 67 65 72 53 61 76 65 70 6f 69  uct PagerSavepoi
11495 6e 74 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  nt PagerSavepoin
11496 74 3b 0a 73 74 72 75 63 74 20 50 61 67 65 72 53  t;.struct PagerS
11497 61 76 65 70 6f 69 6e 74 20 7b 0a 20 20 69 36 34  avepoint {.  i64
11498 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20   iOffset;       
11499 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61            /* Sta
1149a 72 74 69 6e 67 20 6f 66 66 73 65 74 20 69 6e 20  rting offset in 
1149b 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  main journal */.
1149c 20 20 69 36 34 20 69 48 64 72 4f 66 66 73 65 74    i64 iHdrOffset
1149d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1149e 2a 20 53 65 65 20 61 62 6f 76 65 20 2a 2f 0a 20  * See above */. 
1149f 20 42 69 74 76 65 63 20 2a 70 49 6e 53 61 76 65   Bitvec *pInSave
114a0 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a  point;        /*
114a1 20 53 65 74 20 6f 66 20 70 61 67 65 73 20 69 6e   Set of pages in
114a2 20 74 68 69 73 20 73 61 76 65 70 6f 69 6e 74 20   this savepoint 
114a3 2a 2f 0a 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b  */.  Pgno nOrig;
114a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
114a5 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6e 75    /* Original nu
114a6 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
114a7 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20   file */.  Pgno 
114a8 69 53 75 62 52 65 63 3b 20 20 20 20 20 20 20 20  iSubRec;        
114a9 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
114aa 20 6f 66 20 66 69 72 73 74 20 72 65 63 6f 72 64   of first record
114ab 20 69 6e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20   in sub-journal 
114ac 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6f  */.};../*.** A o
114ad 70 65 6e 20 70 61 67 65 20 63 61 63 68 65 20 69  pen page cache i
114ae 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  s an instance of
114af 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
114b0 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
114b1 65 72 72 43 6f 64 65 0a 2a 2a 0a 2a 2a 20 20 20  errCode.**.**   
114b2 50 61 67 65 72 2e 65 72 72 43 6f 64 65 20 6d 61  Pager.errCode ma
114b3 79 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49  y be set to SQLI
114b4 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45  TE_IOERR, SQLITE
114b5 5f 43 4f 52 52 55 50 54 2c 20 6f 72 0a 2a 2a 20  _CORRUPT, or.** 
114b6 20 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c    or SQLITE_FULL
114b7 2e 20 4f 6e 63 65 20 6f 6e 65 20 6f 66 20 74 68  . Once one of th
114b8 65 20 66 69 72 73 74 20 74 68 72 65 65 20 65 72  e first three er
114b9 72 6f 72 73 20 6f 63 63 75 72 73 2c 20 69 74 20  rors occurs, it 
114ba 70 65 72 73 69 73 74 73 0a 2a 2a 20 20 20 61 6e  persists.**   an
114bb 64 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 73  d is returned as
114bc 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65   the result of e
114bd 76 65 72 79 20 6d 61 6a 6f 72 20 70 61 67 65 72  very major pager
114be 20 41 50 49 20 63 61 6c 6c 2e 20 20 54 68 65 0a   API call.  The.
114bf 2a 2a 20 20 20 53 51 4c 49 54 45 5f 46 55 4c 4c  **   SQLITE_FULL
114c0 20 72 65 74 75 72 6e 20 63 6f 64 65 20 69 73 20   return code is 
114c1 73 6c 69 67 68 74 6c 79 20 64 69 66 66 65 72 65  slightly differe
114c2 6e 74 2e 20 49 74 20 70 65 72 73 69 73 74 73 20  nt. It persists 
114c3 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 0a 2a  only until the.*
114c4 2a 20 20 20 6e 65 78 74 20 73 75 63 63 65 73 73  *   next success
114c5 66 75 6c 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20  ful rollback is 
114c6 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65  performed on the
114c7 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 41 6c   pager cache. Al
114c8 73 6f 2c 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f  so,.**   SQLITE_
114c9 46 55 4c 4c 20 64 6f 65 73 20 6e 6f 74 20 61 66  FULL does not af
114ca 66 65 63 74 20 74 68 65 20 73 71 6c 69 74 65 33  fect the sqlite3
114cb 50 61 67 65 72 47 65 74 28 29 20 61 6e 64 20 73  PagerGet() and s
114cc 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75  qlite3PagerLooku
114cd 70 28 29 0a 2a 2a 20 20 20 41 50 49 73 2c 20 74  p().**   APIs, t
114ce 68 65 79 20 6d 61 79 20 73 74 69 6c 6c 20 62 65  hey may still be
114cf 20 75 73 65 64 20 73 75 63 63 65 73 73 66 75 6c   used successful
114d0 6c 79 2e 0a 2a 2a 0a 2a 2a 20 64 62 53 69 7a 65  ly..**.** dbSize
114d1 56 61 6c 69 64 2c 20 64 62 53 69 7a 65 2c 20 64  Valid, dbSize, d
114d2 62 4f 72 69 67 53 69 7a 65 2c 20 64 62 46 69 6c  bOrigSize, dbFil
114d3 65 53 69 7a 65 0a 2a 2a 0a 2a 2a 20 20 20 4d 61  eSize.**.**   Ma
114d4 6e 61 67 69 6e 67 20 74 68 65 20 73 69 7a 65 20  naging the size 
114d5 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
114d6 66 69 6c 65 20 69 6e 20 70 61 67 65 73 20 69 73  file in pages is
114d7 20 61 20 6c 69 74 74 6c 65 20 63 6f 6d 70 6c 69   a little compli
114d8 63 61 74 65 64 2e 0a 2a 2a 20 20 20 54 68 65 20  cated..**   The 
114d9 76 61 72 69 61 62 6c 65 20 50 61 67 65 72 2e 64  variable Pager.d
114da 62 53 69 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74  bSize contains t
114db 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
114dc 65 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61  es that the data
114dd 62 61 73 65 0a 2a 2a 20 20 20 69 6d 61 67 65 20  base.**   image 
114de 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69  currently contai
114df 6e 73 2e 20 41 73 20 74 68 65 20 64 61 74 61 62  ns. As the datab
114e0 61 73 65 20 69 6d 61 67 65 20 67 72 6f 77 73 20  ase image grows 
114e1 6f 72 20 73 68 72 69 6e 6b 73 20 74 68 69 73 0a  or shrinks this.
114e2 2a 2a 20 20 20 76 61 72 69 61 62 6c 65 20 69 73  **   variable is
114e3 20 75 70 64 61 74 65 64 2e 20 54 68 65 20 76 61   updated. The va
114e4 72 69 61 62 6c 65 20 50 61 67 65 72 2e 64 62 46  riable Pager.dbF
114e5 69 6c 65 53 69 7a 65 20 63 6f 6e 74 61 69 6e 73  ileSize contains
114e6 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 20   the number.**  
114e7 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65   of pages in the
114e8 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
114e9 54 68 69 73 20 6d 61 79 20 62 65 20 64 69 66 66  This may be diff
114ea 65 72 65 6e 74 20 66 72 6f 6d 20 50 61 67 65 72  erent from Pager
114eb 2e 64 62 53 69 7a 65 0a 2a 2a 20 20 20 69 66 20  .dbSize.**   if 
114ec 73 6f 6d 65 20 70 61 67 65 73 20 68 61 76 65 20  some pages have 
114ed 62 65 65 6e 20 61 70 70 65 6e 64 65 64 20 74 6f  been appended to
114ee 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d   the database im
114ef 61 67 65 20 62 75 74 20 6e 6f 74 20 79 65 74 20  age but not yet 
114f0 77 72 69 74 74 65 6e 0a 2a 2a 20 20 20 6f 75 74  written.**   out
114f1 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20   from the cache 
114f2 74 6f 20 74 68 65 20 61 63 74 75 61 6c 20 66 69  to the actual fi
114f3 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 4f 72 20 69  le on disk. Or i
114f4 66 20 74 68 65 20 69 6d 61 67 65 20 68 61 73 20  f the image has 
114f5 62 65 65 6e 0a 2a 2a 20 20 20 74 72 75 6e 63 61  been.**   trunca
114f6 74 65 64 20 62 79 20 61 6e 20 69 6e 63 72 65 6d  ted by an increm
114f7 65 6e 74 61 6c 2d 76 61 63 75 75 6d 20 6f 70 65  ental-vacuum ope
114f8 72 61 74 69 6f 6e 2e 20 54 68 65 20 50 61 67 65  ration. The Page
114f9 72 2e 64 62 4f 72 69 67 53 69 7a 65 20 76 61 72  r.dbOrigSize var
114fa 69 61 62 6c 65 0a 2a 2a 20 20 20 63 6f 6e 74 61  iable.**   conta
114fb 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ins the number o
114fc 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64  f pages in the d
114fd 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 77 68  atabase image wh
114fe 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a  en the current.*
114ff 2a 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  *   transaction 
11500 77 61 73 20 6f 70 65 6e 65 64 2e 20 54 68 65 20  was opened. The 
11501 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6c 6c 20  contents of all 
11502 74 68 72 65 65 20 6f 66 20 74 68 65 73 65 20 76  three of these v
11503 61 72 69 61 62 6c 65 73 20 69 73 0a 2a 2a 20 20  ariables is.**  
11504 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65 65 64   only guaranteed
11505 20 74 6f 20 62 65 20 63 6f 72 72 65 63 74 20 69   to be correct i
11506 66 20 74 68 65 20 62 6f 6f 6c 65 61 6e 20 50 61  f the boolean Pa
11507 67 65 72 2e 64 62 53 69 7a 65 56 61 6c 69 64 20  ger.dbSizeValid 
11508 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20  is true..**.**  
11509 20 54 4f 44 4f 3a 20 55 6e 64 65 72 20 77 68 61   TODO: Under wha
1150a 74 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 73 20  t conditions is 
1150b 64 62 53 69 7a 65 56 61 6c 69 64 20 73 65 74 3f  dbSizeValid set?
1150c 20 43 6c 65 61 72 65 64 3f 0a 2a 2a 0a 2a 2a 20   Cleared?.**.** 
1150d 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 0a  changeCountDone.
1150e 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 62 6f 6f  **.**   This boo
1150f 6c 65 61 6e 20 76 61 72 69 61 62 6c 65 20 69 73  lean variable is
11510 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 75   used to make su
11511 72 65 20 74 68 61 74 20 74 68 65 20 63 68 61 6e  re that the chan
11512 67 65 2d 63 6f 75 6e 74 65 72 20 0a 2a 2a 20 20  ge-counter .**  
11513 20 28 74 68 65 20 34 2d 62 79 74 65 20 68 65 61   (the 4-byte hea
11514 64 65 72 20 66 69 65 6c 64 20 61 74 20 62 79 74  der field at byt
11515 65 20 6f 66 66 73 65 74 20 32 34 20 6f 66 20 74  e offset 24 of t
11516 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
11517 29 20 69 73 20 0a 2a 2a 20 20 20 6e 6f 74 20 75  ) is .**   not u
11518 70 64 61 74 65 64 20 6d 6f 72 65 20 6f 66 74 65  pdated more ofte
11519 6e 20 74 68 61 6e 20 6e 65 63 65 73 73 61 72 79  n than necessary
1151a 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 49 74 20 69 73  . .**.**   It is
1151b 20 73 65 74 20 74 6f 20 74 72 75 65 20 77 68 65   set to true whe
1151c 6e 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75  n the change-cou
1151d 6e 74 65 72 20 66 69 65 6c 64 20 69 73 20 75 70  nter field is up
1151e 64 61 74 65 64 2c 20 77 68 69 63 68 20 0a 2a 2a  dated, which .**
1151f 20 20 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70     can only happ
11520 65 6e 20 69 66 20 61 6e 20 65 78 63 6c 75 73 69  en if an exclusi
11521 76 65 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20  ve lock is held 
11522 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
11523 66 69 6c 65 2e 0a 2a 2a 20 20 20 49 74 20 69 73  file..**   It is
11524 20 63 6c 65 61 72 65 64 20 28 73 65 74 20 74 6f   cleared (set to
11525 20 66 61 6c 73 65 29 20 77 68 65 6e 65 76 65 72   false) whenever
11526 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   an exclusive lo
11527 63 6b 20 69 73 20 0a 2a 2a 20 20 20 72 65 6c 69  ck is .**   reli
11528 6e 71 75 69 73 68 65 64 20 6f 6e 20 74 68 65 20  nquished on the 
11529 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 45  database file. E
1152a 61 63 68 20 74 69 6d 65 20 61 20 74 72 61 6e 73  ach time a trans
1152b 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74  action is commit
1152c 74 65 64 2c 0a 2a 2a 20 20 20 54 68 65 20 63 68  ted,.**   The ch
1152d 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c  angeCountDone fl
1152e 61 67 20 69 73 20 69 6e 73 70 65 63 74 65 64 2e  ag is inspected.
1152f 20 49 66 20 69 74 20 69 73 20 74 72 75 65 2c 20   If it is true, 
11530 74 68 65 20 77 6f 72 6b 20 6f 66 0a 2a 2a 20 20  the work of.**  
11531 20 75 70 64 61 74 69 6e 67 20 74 68 65 20 63 68   updating the ch
11532 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 69 73 20  ange-counter is 
11533 6f 6d 69 74 74 65 64 20 66 6f 72 20 74 68 65 20  omitted for the 
11534 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
11535 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69  ion..**.**   Thi
11536 73 20 6d 65 63 68 61 6e 69 73 6d 20 6d 65 61 6e  s mechanism mean
11537 73 20 74 68 61 74 20 77 68 65 6e 20 72 75 6e 6e  s that when runn
11538 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65  ing in exclusive
11539 20 6d 6f 64 65 2c 20 61 20 63 6f 6e 6e 65 63 74   mode, a connect
1153a 69 6f 6e 20 0a 2a 2a 20 20 20 6e 65 65 64 20 6f  ion .**   need o
1153b 6e 6c 79 20 75 70 64 61 74 65 20 74 68 65 20 63  nly update the c
1153c 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 6f 6e  hange-counter on
1153d 63 65 2c 20 66 6f 72 20 74 68 65 20 66 69 72 73  ce, for the firs
1153e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a  t transaction.**
1153f 20 20 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a     committed..**
11540 0a 2a 2a 20 64 62 4d 6f 64 69 66 69 65 64 0a 2a  .** dbModified.*
11541 2a 0a 2a 2a 20 20 20 54 68 65 20 64 62 4d 6f 64  *.**   The dbMod
11542 69 66 69 65 64 20 66 6c 61 67 20 69 73 20 73 65  ified flag is se
11543 74 20 77 68 65 6e 65 76 65 72 20 61 20 64 61 74  t whenever a dat
11544 61 62 61 73 65 20 70 61 67 65 20 69 73 20 64 69  abase page is di
11545 72 74 69 65 64 2e 0a 2a 2a 20 20 20 49 74 20 69  rtied..**   It i
11546 73 20 63 6c 65 61 72 65 64 20 61 74 20 74 68 65  s cleared at the
11547 20 65 6e 64 20 6f 66 20 65 61 63 68 20 74 72 61   end of each tra
11548 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nsaction..**.** 
11549 20 20 49 74 20 69 73 20 75 73 65 64 20 77 68 65    It is used whe
1154a 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 6f 72 20  n committing or 
1154b 6f 74 68 65 72 77 69 73 65 20 65 6e 64 69 6e 67  otherwise ending
1154c 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20   a transaction. 
1154d 49 66 0a 2a 2a 20 20 20 74 68 65 20 64 62 4d 6f  If.**   the dbMo
1154e 64 69 66 69 65 64 20 66 6c 61 67 20 69 73 20 63  dified flag is c
1154f 6c 65 61 72 20 74 68 65 6e 20 6c 65 73 73 20 77  lear then less w
11550 6f 72 6b 20 68 61 73 20 74 6f 20 62 65 20 64 6f  ork has to be do
11551 6e 65 2e 0a 2a 2a 0a 2a 2a 20 6a 6f 75 72 6e 61  ne..**.** journa
11552 6c 53 74 61 72 74 65 64 0a 2a 2a 0a 2a 2a 20 20  lStarted.**.**  
11553 20 54 68 69 73 20 66 6c 61 67 20 69 73 20 73 65   This flag is se
11554 74 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 74  t whenever the t
11555 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  he main journal 
11556 69 73 20 73 79 6e 63 65 64 2e 20 0a 2a 2a 0a 2a  is synced. .**.*
11557 2a 20 20 20 54 68 65 20 70 6f 69 6e 74 20 6f 66  *   The point of
11558 20 74 68 69 73 20 66 6c 61 67 20 69 73 20 74 68   this flag is th
11559 61 74 20 69 74 20 6d 75 73 74 20 62 65 20 73 65  at it must be se
1155a 74 20 61 66 74 65 72 20 74 68 65 20 0a 2a 2a 20  t after the .** 
1155b 20 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20    first journal 
1155c 68 65 61 64 65 72 20 69 6e 20 61 20 6a 6f 75 72  header in a jour
1155d 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 62 65 65  nal file has bee
1155e 6e 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b  n synced to disk
1155f 2e 0a 2a 2a 20 20 20 41 66 74 65 72 20 74 68 69  ..**   After thi
11560 73 20 68 61 73 20 68 61 70 70 65 6e 65 64 2c 20  s has happened, 
11561 6e 65 77 20 70 61 67 65 73 20 61 70 70 65 6e 64  new pages append
11562 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ed to the databa
11563 73 65 20 0a 2a 2a 20 20 20 64 6f 20 6e 6f 74 20  se .**   do not 
11564 6e 65 65 64 20 74 68 65 20 50 47 48 44 52 5f 4e  need the PGHDR_N
11565 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 73 65  EED_SYNC flag se
11566 74 2c 20 61 73 20 74 68 65 79 20 64 6f 20 6e 6f  t, as they do no
11567 74 20 6e 65 65 64 0a 2a 2a 20 20 20 74 6f 20 77  t need.**   to w
11568 61 69 74 20 66 6f 72 20 61 20 6a 6f 75 72 6e 61  ait for a journa
11569 6c 20 73 79 6e 63 20 62 65 66 6f 72 65 20 74 68  l sync before th
1156a 65 79 20 63 61 6e 20 62 65 20 77 72 69 74 74 65  ey can be writte
1156b 6e 20 6f 75 74 20 74 6f 0a 2a 2a 20 20 20 74 68  n out to.**   th
1156c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
1156d 28 73 65 65 20 66 75 6e 63 74 69 6f 6e 20 70 61  (see function pa
1156e 67 65 72 5f 77 72 69 74 65 28 29 29 2e 0a 2a 2a  ger_write())..**
1156f 20 20 20 0a 2a 2a 20 73 65 74 4d 61 73 74 65 72     .** setMaster
11570 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20 76 61  .**.**   This va
11571 72 69 61 62 6c 65 20 69 73 20 75 73 65 64 20 74  riable is used t
11572 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68  o ensure that th
11573 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
11574 20 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20   file name.**   
11575 28 69 66 20 61 6e 79 29 20 69 73 20 6f 6e 6c 79  (if any) is only
11576 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
11577 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f  e journal file o
11578 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 57 68 65  nce..**.**   Whe
11579 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74  n committing a t
1157a 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 20  ransaction, the 
1157b 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
1157c 69 6c 65 20 6e 61 6d 65 20 28 69 66 20 61 6e 79  ile name (if any
1157d 29 0a 2a 2a 20 20 20 6d 61 79 20 62 65 20 77 72  ).**   may be wr
1157e 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a  itten into the j
1157f 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 69 6c  ournal file whil
11580 65 20 74 68 65 20 70 61 67 65 72 20 69 73 20 73  e the pager is s
11581 74 69 6c 6c 20 69 6e 0a 2a 2a 20 20 20 50 41 47  till in.**   PAG
11582 45 52 5f 52 45 53 45 52 56 45 44 20 73 74 61 74  ER_RESERVED stat
11583 65 20 28 73 65 65 20 43 6f 6d 6d 69 74 50 68 61  e (see CommitPha
11584 73 65 4f 6e 65 28 29 20 66 6f 72 20 74 68 65 20  seOne() for the 
11585 61 63 74 69 6f 6e 29 2e 20 49 74 0a 2a 2a 20 20  action). It.**  
11586 20 74 68 65 6e 20 61 74 74 65 6d 70 74 73 20 74   then attempts t
11587 6f 20 75 70 67 72 61 64 65 20 74 6f 20 61 6e 20  o upgrade to an 
11588 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20  exclusive lock. 
11589 49 66 20 74 68 69 73 20 61 74 74 65 6d 70 74 0a  If this attempt.
1158a 2a 2a 20 20 20 66 61 69 6c 73 2c 20 74 68 65 6e  **   fails, then
1158b 20 53 51 4c 49 54 45 5f 42 55 53 59 20 6d 61 79   SQLITE_BUSY may
1158c 20 62 65 20 72 65 74 75 72 6e 65 64 20 74 6f 20   be returned to 
1158d 74 68 65 20 75 73 65 72 20 61 6e 64 20 74 68 65  the user and the
1158e 20 75 73 65 72 0a 2a 2a 20 20 20 6d 61 79 20 61   user.**   may a
1158f 74 74 65 6d 70 74 20 74 6f 20 63 6f 6d 6d 69 74  ttempt to commit
11590 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
11591 20 61 67 61 69 6e 20 6c 61 74 65 72 20 28 63 61   again later (ca
11592 6c 6c 69 6e 67 0a 2a 2a 20 20 20 43 6f 6d 6d 69  lling.**   Commi
11593 74 50 68 61 73 65 4f 6e 65 28 29 20 61 67 61 69  tPhaseOne() agai
11594 6e 29 2e 20 54 68 69 73 20 66 6c 61 67 20 69 73  n). This flag is
11595 20 75 73 65 64 20 74 6f 20 65 6e 73 75 72 65 20   used to ensure 
11596 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 20 20 6d  that the .**   m
11597 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61  aster journal na
11598 6d 65 20 69 73 20 6f 6e 6c 79 20 77 72 69 74 74  me is only writt
11599 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61  en to the journa
1159a 6c 20 66 69 6c 65 20 74 68 65 20 66 69 72 73 74  l file the first
1159b 0a 2a 2a 20 20 20 74 69 6d 65 20 43 6f 6d 6d 69  .**   time Commi
1159c 74 50 68 61 73 65 4f 6e 65 28 29 20 69 73 20 63  tPhaseOne() is c
1159d 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 64 6f 4e  alled..**.** doN
1159e 6f 74 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54  otSync.**.**   T
1159f 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 73 20  his variable is 
115a0 73 65 74 20 61 6e 64 20 63 6c 65 61 72 65 64 20  set and cleared 
115a1 62 79 20 73 71 6c 69 74 65 33 50 61 67 65 72 57  by sqlite3PagerW
115a2 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 6e 65  rite()..**.** ne
115a3 65 64 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54  edSync.**.**   T
115a4 4f 44 4f 3a 20 49 74 20 6d 69 67 68 74 20 62 65  ODO: It might be
115a5 20 65 61 73 69 65 72 20 74 6f 20 73 65 74 20 74   easier to set t
115a6 68 69 73 20 76 61 72 69 61 62 6c 65 20 69 6e 20  his variable in 
115a7 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28  writeJournalHdr(
115a8 29 0a 2a 2a 20 20 20 61 6e 64 20 77 72 69 74 65  ).**   and write
115a9 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20  MasterJournal() 
115aa 6f 6e 6c 79 2e 20 43 68 61 6e 67 65 20 69 74 73  only. Change its
115ab 20 6d 65 61 6e 69 6e 67 20 74 6f 20 22 75 6e 73   meaning to "uns
115ac 79 6e 63 65 64 20 64 61 74 61 0a 2a 2a 20 20 20  ynced data.**   
115ad 68 61 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e  has been written
115ae 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 22   to the journal"
115af 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 49 6e 4d 65  ..**.** subjInMe
115b0 6d 6f 72 79 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69  mory.**.**   Thi
115b1 73 20 69 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76  s is a boolean v
115b2 61 72 69 61 62 6c 65 2e 20 49 66 20 74 72 75 65  ariable. If true
115b3 2c 20 74 68 65 6e 20 61 6e 79 20 72 65 71 75 69  , then any requi
115b4 72 65 64 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a  red sub-journal.
115b5 2a 2a 20 20 20 69 73 20 6f 70 65 6e 65 64 20 61  **   is opened a
115b6 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a  s an in-memory j
115b7 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20  ournal file. If 
115b8 66 61 6c 73 65 2c 20 74 68 65 6e 20 69 6e 2d 6d  false, then in-m
115b9 65 6d 6f 72 79 0a 2a 2a 20 20 20 73 75 62 2d 6a  emory.**   sub-j
115ba 6f 75 72 6e 61 6c 73 20 61 72 65 20 6f 6e 6c 79  ournals are only
115bb 20 75 73 65 64 20 66 6f 72 20 69 6e 2d 6d 65 6d   used for in-mem
115bc 6f 72 79 20 70 61 67 65 72 20 66 69 6c 65 73 2e  ory pager files.
115bd 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61 67 65 72  .*/.struct Pager
115be 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73   {.  sqlite3_vfs
115bf 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20   *pVfs;         
115c0 20 2f 2a 20 4f 53 20 66 75 6e 63 74 69 6f 6e 73   /* OS functions
115c1 20 74 6f 20 75 73 65 20 66 6f 72 20 49 4f 20 2a   to use for IO *
115c2 2f 0a 20 20 75 38 20 65 78 63 6c 75 73 69 76 65  /.  u8 exclusive
115c3 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  Mode;           
115c4 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 54 72 75 65  /* Boolean. True
115c5 20 69 66 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65   if locking_mode
115c6 3d 3d 45 58 43 4c 55 53 49 56 45 20 2a 2f 0a 20  ==EXCLUSIVE */. 
115c7 20 75 38 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b   u8 journalMode;
115c8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
115c9 4f 6e 20 6f 66 20 74 68 65 20 50 41 47 45 52 5f  On of the PAGER_
115ca 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 2a 20 76 61  JOURNALMODE_* va
115cb 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 75 73 65  lues */.  u8 use
115cc 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20  Journal;        
115cd 20 20 20 20 20 20 2f 2a 20 55 73 65 20 61 20 72        /* Use a r
115ce 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
115cf 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a  on this file */.
115d0 20 20 75 38 20 6e 6f 52 65 61 64 6c 6f 63 6b 3b    u8 noReadlock;
115d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
115d2 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 74   Do not bother t
115d3 6f 20 6f 62 74 61 69 6e 20 72 65 61 64 6c 6f 63  o obtain readloc
115d4 6b 73 20 2a 2f 0a 20 20 75 38 20 6e 6f 53 79 6e  ks */.  u8 noSyn
115d5 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
115d6 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 79      /* Do not sy
115d7 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69  nc the journal i
115d8 66 20 74 72 75 65 20 2a 2f 0a 20 20 75 38 20 66  f true */.  u8 f
115d9 75 6c 6c 53 79 6e 63 3b 20 20 20 20 20 20 20 20  ullSync;        
115da 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 65 78          /* Do ex
115db 74 72 61 20 73 79 6e 63 73 20 6f 66 20 74 68 65  tra syncs of the
115dc 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 6f 62   journal for rob
115dd 75 73 74 6e 65 73 73 20 2a 2f 0a 20 20 75 38 20  ustness */.  u8 
115de 73 79 6e 63 5f 66 6c 61 67 73 3b 20 20 20 20 20  sync_flags;     
115df 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
115e0 6f 66 20 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f  of SYNC_NORMAL o
115e1 72 20 53 59 4e 43 5f 46 55 4c 4c 20 2a 2f 0a 20  r SYNC_FULL */. 
115e2 20 75 38 20 74 65 6d 70 46 69 6c 65 3b 20 20 20   u8 tempFile;   
115e3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
115e4 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 61 20 74  zFilename is a t
115e5 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 2a 2f  emporary file */
115e6 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20  .  u8 readOnly; 
115e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
115e8 2a 20 54 72 75 65 20 66 6f 72 20 61 20 72 65 61  * True for a rea
115e9 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20  d-only database 
115ea 2a 2f 0a 20 20 75 38 20 6d 65 6d 44 62 3b 20 20  */.  u8 memDb;  
115eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
115ec 20 2f 2a 20 54 72 75 65 20 74 6f 20 69 6e 68 69   /* True to inhi
115ed 62 69 74 20 61 6c 6c 20 66 69 6c 65 20 49 2f 4f  bit all file I/O
115ee 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f   */..  /* The fo
115ef 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 63 6f  llowing block co
115f0 6e 74 61 69 6e 73 20 74 68 6f 73 65 20 63 6c 61  ntains those cla
115f1 73 73 20 6d 65 6d 62 65 72 73 20 74 68 61 74 20  ss members that 
115f2 61 72 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a  are dynamically.
115f3 20 20 2a 2a 20 6d 6f 64 69 66 69 65 64 20 64 75    ** modified du
115f4 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 6f 70 65 72  ring normal oper
115f5 61 74 69 6f 6e 73 2e 20 54 68 65 20 6f 74 68 65  ations. The othe
115f6 72 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74  r variables in t
115f7 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 20 20  his structure.  
115f8 2a 2a 20 61 72 65 20 65 69 74 68 65 72 20 63 6f  ** are either co
115f9 6e 73 74 61 6e 74 20 74 68 72 6f 75 67 68 6f 75  nstant throughou
115fa 74 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f  t the lifetime o
115fb 66 20 74 68 65 20 70 61 67 65 72 2c 20 6f 72 20  f the pager, or 
115fc 65 6c 73 65 0a 20 20 2a 2a 20 75 73 65 64 20 74  else.  ** used t
115fd 6f 20 73 74 6f 72 65 20 63 6f 6e 66 69 67 75 72  o store configur
115fe 61 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 73  ation parameters
115ff 20 74 68 61 74 20 61 66 66 65 63 74 20 74 68 65   that affect the
11600 20 77 61 79 20 74 68 65 20 70 61 67 65 72 20 0a   way the pager .
11601 20 20 2a 2a 20 6f 70 65 72 61 74 65 73 2e 0a 20    ** operates.. 
11602 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 27 73 74   **.  ** The 'st
11603 61 74 65 27 20 76 61 72 69 61 62 6c 65 20 69 73  ate' variable is
11604 20 64 65 73 63 72 69 62 65 64 20 69 6e 20 6d 6f   described in mo
11605 72 65 20 64 65 74 61 69 6c 20 61 6c 6f 6e 67 20  re detail along 
11606 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 64 65  with the.  ** de
11607 73 63 72 69 70 74 69 6f 6e 73 20 6f 66 20 74 68  scriptions of th
11608 65 20 76 61 6c 75 65 73 20 69 74 20 6d 61 79 20  e values it may 
11609 74 61 6b 65 20 2d 20 50 41 47 45 52 5f 55 4e 4c  take - PAGER_UNL
1160a 4f 43 4b 20 65 74 63 2e 20 4d 61 6e 79 20 6f 66  OCK etc. Many of
1160b 20 74 68 65 0a 20 20 2a 2a 20 6f 74 68 65 72 20   the.  ** other 
1160c 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68 69  variables in thi
1160d 73 20 62 6c 6f 63 6b 20 61 72 65 20 64 65 73 63  s block are desc
1160e 72 69 62 65 64 20 69 6e 20 74 68 65 20 63 6f 6d  ribed in the com
1160f 6d 65 6e 74 20 64 69 72 65 63 74 6c 79 20 0a 20  ment directly . 
11610 20 2a 2a 20 61 62 6f 76 65 20 74 68 69 73 20 63   ** above this c
11611 6c 61 73 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e  lass definition.
11612 0a 20 20 2a 2f 0a 20 20 75 38 20 73 74 61 74 65  .  */.  u8 state
11613 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11614 20 20 20 20 2f 2a 20 50 41 47 45 52 5f 55 4e 4c      /* PAGER_UNL
11615 4f 43 4b 2c 20 5f 53 48 41 52 45 44 2c 20 5f 52  OCK, _SHARED, _R
11616 45 53 45 52 56 45 44 2c 20 65 74 63 2e 20 2a 2f  ESERVED, etc. */
11617 0a 20 20 75 38 20 64 62 4d 6f 64 69 66 69 65 64  .  u8 dbModified
11618 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
11619 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20  * True if there 
1161a 61 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20  are any changes 
1161b 74 6f 20 74 68 65 20 44 62 20 2a 2f 0a 20 20 75  to the Db */.  u
1161c 38 20 6e 65 65 64 53 79 6e 63 3b 20 20 20 20 20  8 needSync;     
1161d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
1161e 75 65 20 69 66 20 61 6e 20 66 73 79 6e 63 28 29  ue if an fsync()
1161f 20 69 73 20 6e 65 65 64 65 64 20 6f 6e 20 74 68   is needed on th
11620 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75  e journal */.  u
11621 38 20 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64  8 journalStarted
11622 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ;          /* Tr
11623 75 65 20 69 66 20 68 65 61 64 65 72 20 6f 66 20  ue if header of 
11624 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65  journal is synce
11625 64 20 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65  d */.  u8 change
11626 43 6f 75 6e 74 44 6f 6e 65 3b 20 20 20 20 20 20  CountDone;      
11627 20 20 20 2f 2a 20 53 65 74 20 61 66 74 65 72 20     /* Set after 
11628 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 74 68 65  incrementing the
11629 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20   change-counter 
1162a 2a 2f 0a 20 20 75 38 20 73 65 74 4d 61 73 74 65  */.  u8 setMaste
1162b 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
1162c 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 6d 2d   /* True if a m-
1162d 6a 20 6e 61 6d 65 20 68 61 73 20 62 65 65 6e 20  j name has been 
1162e 77 72 69 74 74 65 6e 20 74 6f 20 6a 72 6e 6c 20  written to jrnl 
1162f 2a 2f 0a 20 20 75 38 20 64 6f 4e 6f 74 53 79 6e  */.  u8 doNotSyn
11630 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
11631 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 57 68 69   /* Boolean. Whi
11632 6c 65 20 74 72 75 65 2c 20 64 6f 20 6e 6f 74 20  le true, do not 
11633 73 70 69 6c 6c 20 74 68 65 20 63 61 63 68 65 20  spill the cache 
11634 2a 2f 0a 20 20 75 38 20 64 62 53 69 7a 65 56 61  */.  u8 dbSizeVa
11635 6c 69 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  lid;            
11636 20 2f 2a 20 53 65 74 20 77 68 65 6e 20 64 62 53   /* Set when dbS
11637 69 7a 65 20 69 73 20 63 6f 72 72 65 63 74 20 2a  ize is correct *
11638 2f 0a 20 20 75 38 20 73 75 62 6a 49 6e 4d 65 6d  /.  u8 subjInMem
11639 6f 72 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  ory;            
1163a 2f 2a 20 54 72 75 65 20 74 6f 20 75 73 65 20 69  /* True to use i
1163b 6e 2d 6d 65 6d 6f 72 79 20 73 75 62 2d 6a 6f 75  n-memory sub-jou
1163c 72 6e 61 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20  rnals */.  Pgno 
1163d 64 62 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20  dbSize;         
1163e 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1163f 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65   of pages in the
11640 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 50   database */.  P
11641 67 6e 6f 20 64 62 4f 72 69 67 53 69 7a 65 3b 20  gno dbOrigSize; 
11642 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 64 62             /* db
11643 53 69 7a 65 20 62 65 66 6f 72 65 20 74 68 65 20  Size before the 
11644 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
11645 69 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62  ion */.  Pgno db
11646 46 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20  FileSize;       
11647 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
11648 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64  f pages in the d
11649 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
1164a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 20    int errCode;  
1164b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1164c 20 4f 6e 65 20 6f 66 20 73 65 76 65 72 61 6c 20   One of several 
1164d 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 20  kinds of errors 
1164e 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 63 3b 20 20  */.  int nRec;  
1164f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11650 20 2f 2a 20 50 61 67 65 73 20 6a 6f 75 72 6e 61   /* Pages journa
11651 6c 6c 65 64 20 73 69 6e 63 65 20 6c 61 73 74 20  lled since last 
11652 6a 2d 68 65 61 64 65 72 20 77 72 69 74 74 65 6e  j-header written
11653 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 49   */.  u32 cksumI
11654 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  nit;            
11655 20 20 2f 2a 20 51 75 61 73 69 2d 72 61 6e 64 6f    /* Quasi-rando
11656 6d 20 76 61 6c 75 65 20 61 64 64 65 64 20 74 6f  m value added to
11657 20 65 76 65 72 79 20 63 68 65 63 6b 73 75 6d 20   every checksum 
11658 2a 2f 0a 20 20 75 33 32 20 6e 53 75 62 52 65 63  */.  u32 nSubRec
11659 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1165a 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65   /* Number of re
1165b 63 6f 72 64 73 20 77 72 69 74 74 65 6e 20 74 6f  cords written to
1165c 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a   sub-journal */.
1165d 20 20 42 69 74 76 65 63 20 2a 70 49 6e 4a 6f 75    Bitvec *pInJou
1165e 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a  rnal;         /*
1165f 20 4f 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63   One bit for eac
11660 68 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61  h page in the da
11661 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20  tabase file */. 
11662 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66   sqlite3_file *f
11663 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  d;           /* 
11664 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  File descriptor 
11665 66 6f 72 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  for database */.
11666 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
11667 6a 66 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  jfd;          /*
11668 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   File descriptor
11669 20 66 6f 72 20 6d 61 69 6e 20 6a 6f 75 72 6e 61   for main journa
1166a 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66  l */.  sqlite3_f
1166b 69 6c 65 20 2a 73 6a 66 64 3b 20 20 20 20 20 20  ile *sjfd;      
1166c 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72     /* File descr
1166d 69 70 74 6f 72 20 66 6f 72 20 73 75 62 2d 6a 6f  iptor for sub-jo
1166e 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 6a  urnal */.  i64 j
1166f 6f 75 72 6e 61 6c 4f 66 66 3b 20 20 20 20 20 20  ournalOff;      
11670 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
11671 74 20 77 72 69 74 65 20 6f 66 66 73 65 74 20 69  t write offset i
11672 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  n the journal fi
11673 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72  le */.  i64 jour
11674 6e 61 6c 48 64 72 3b 20 20 20 20 20 20 20 20 20  nalHdr;         
11675 20 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 73      /* Byte offs
11676 65 74 20 74 6f 20 70 72 65 76 69 6f 75 73 20 6a  et to previous j
11677 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f  ournal header */
11678 0a 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  .  PagerSavepoin
11679 74 20 2a 61 53 61 76 65 70 6f 69 6e 74 3b 20 2f  t *aSavepoint; /
1167a 2a 20 41 72 72 61 79 20 6f 66 20 61 63 74 69 76  * Array of activ
1167b 65 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a  e savepoints */.
1167c 20 20 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74    int nSavepoint
1167d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1167e 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   Number of eleme
1167f 6e 74 73 20 69 6e 20 61 53 61 76 65 70 6f 69 6e  nts in aSavepoin
11680 74 5b 5d 20 2a 2f 0a 20 20 63 68 61 72 20 64 62  t[] */.  char db
11681 46 69 6c 65 56 65 72 73 5b 31 36 5d 3b 20 20 20  FileVers[16];   
11682 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 65 73 20       /* Changes 
11683 77 68 65 6e 65 76 65 72 20 64 61 74 61 62 61 73  whenever databas
11684 65 20 66 69 6c 65 20 63 68 61 6e 67 65 73 20 2a  e file changes *
11685 2f 0a 20 20 75 33 32 20 73 65 63 74 6f 72 53 69  /.  u32 sectorSi
11686 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
11687 2f 2a 20 41 73 73 75 6d 65 64 20 73 65 63 74 6f  /* Assumed secto
11688 72 20 73 69 7a 65 20 64 75 72 69 6e 67 20 72 6f  r size during ro
11689 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20 75 31 36  llback */..  u16
1168a 20 6e 45 78 74 72 61 3b 20 20 20 20 20 20 20 20   nExtra;        
1168b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20           /* Add 
1168c 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20  this many bytes 
1168d 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72  to each in-memor
1168e 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 31 36 20  y page */.  i16 
1168f 6e 52 65 73 65 72 76 65 3b 20 20 20 20 20 20 20  nReserve;       
11690 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
11691 72 20 6f 66 20 75 6e 75 73 65 64 20 62 79 74 65  r of unused byte
11692 73 20 61 74 20 65 6e 64 20 6f 66 20 65 61 63 68  s at end of each
11693 20 70 61 67 65 20 2a 2f 0a 20 20 75 33 32 20 76   page */.  u32 v
11694 66 73 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20  fsFlags;        
11695 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
11696 66 6f 72 20 73 71 6c 69 74 65 33 5f 76 66 73 2e  for sqlite3_vfs.
11697 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74  xOpen() */.  int
11698 20 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20   pageSize;      
11699 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1169a 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61  er of bytes in a
1169b 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20   page */.  Pgno 
1169c 6d 78 50 67 6e 6f 3b 20 20 20 20 20 20 20 20 20  mxPgno;         
1169d 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
1169e 6d 20 61 6c 6c 6f 77 65 64 20 73 69 7a 65 20 6f  m allowed size o
1169f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a  f the database *
116a0 2f 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  /.  char *zFilen
116a1 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
116a2 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64  /* Name of the d
116a3 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
116a4 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c    char *zJournal
116a5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
116a6 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75   Name of the jou
116a7 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69  rnal file */.  i
116a8 6e 74 20 28 2a 78 42 75 73 79 48 61 6e 64 6c 65  nt (*xBusyHandle
116a9 72 29 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 46 75  r)(void*); /* Fu
116aa 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 77  nction to call w
116ab 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20 20 76 6f  hen busy */.  vo
116ac 69 64 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72  id *pBusyHandler
116ad 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6e  Arg;      /* Con
116ae 74 65 78 74 20 61 72 67 75 6d 65 6e 74 20 66 6f  text argument fo
116af 72 20 78 42 75 73 79 48 61 6e 64 6c 65 72 20 2a  r xBusyHandler *
116b0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
116b1 54 45 53 54 0a 20 20 69 6e 74 20 6e 48 69 74 2c  TEST.  int nHit,
116b2 20 6e 4d 69 73 73 3b 20 20 20 20 20 20 20 20 20   nMiss;         
116b3 20 20 20 2f 2a 20 43 61 63 68 65 20 68 69 74 73     /* Cache hits
116b4 20 61 6e 64 20 6d 69 73 73 69 6e 67 20 2a 2f 0a   and missing */.
116b5 20 20 69 6e 74 20 6e 52 65 61 64 2c 20 6e 57 72    int nRead, nWr
116b6 69 74 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ite;          /*
116b7 20 44 61 74 61 62 61 73 65 20 70 61 67 65 73 20   Database pages 
116b8 72 65 61 64 2f 77 72 69 74 74 65 6e 20 2a 2f 0a  read/written */.
116b9 23 65 6e 64 69 66 0a 20 20 76 6f 69 64 20 28 2a  #endif.  void (*
116ba 78 52 65 69 6e 69 74 65 72 29 28 44 62 50 61 67  xReiniter)(DbPag
116bb 65 2a 29 3b 20 2f 2a 20 43 61 6c 6c 20 74 68 69  e*); /* Call thi
116bc 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 72  s routine when r
116bd 65 6c 6f 61 64 69 6e 67 20 70 61 67 65 73 20 2a  eloading pages *
116be 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
116bf 48 41 53 5f 43 4f 44 45 43 0a 20 20 76 6f 69 64  HAS_CODEC.  void
116c0 20 2a 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 64   *(*xCodec)(void
116c1 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74  *,void*,Pgno,int
116c2 29 3b 20 2f 2a 20 52 6f 75 74 69 6e 65 20 66 6f  ); /* Routine fo
116c3 72 20 65 6e 2f 64 65 63 6f 64 69 6e 67 20 64 61  r en/decoding da
116c4 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  ta */.  void (*x
116c5 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 29 28 76  CodecSizeChng)(v
116c6 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 29 3b 20 2f  oid*,int,int); /
116c7 2a 20 4e 6f 74 69 66 79 20 6f 66 20 70 61 67 65  * Notify of page
116c8 20 73 69 7a 65 20 63 68 61 6e 67 65 73 20 2a 2f   size changes */
116c9 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f 64 65 63  .  void (*xCodec
116ca 46 72 65 65 29 28 76 6f 69 64 2a 29 3b 20 20 20  Free)(void*);   
116cb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73            /* Des
116cc 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20  tructor for the 
116cd 63 6f 64 65 63 20 2a 2f 0a 20 20 76 6f 69 64 20  codec */.  void 
116ce 2a 70 43 6f 64 65 63 3b 20 20 20 20 20 20 20 20  *pCodec;        
116cf 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
116d0 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 6f 64  argument to xCod
116d1 65 63 2e 2e 2e 20 6d 65 74 68 6f 64 73 20 2a 2f  ec... methods */
116d2 0a 23 65 6e 64 69 66 0a 20 20 63 68 61 72 20 2a  .#endif.  char *
116d3 70 54 6d 70 53 70 61 63 65 3b 20 20 20 20 20 20  pTmpSpace;      
116d4 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 2e 70        /* Pager.p
116d5 61 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66  ageSize bytes of
116d6 20 73 70 61 63 65 20 66 6f 72 20 74 6d 70 20 75   space for tmp u
116d7 73 65 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72  se */.  i64 jour
116d8 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20  nalSizeLimit;   
116d9 20 20 20 20 2f 2a 20 53 69 7a 65 20 6c 69 6d 69      /* Size limi
116da 74 20 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74  t for persistent
116db 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a   journal files *
116dc 2f 0a 20 20 50 43 61 63 68 65 20 2a 70 50 43 61  /.  PCache *pPCa
116dd 63 68 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  che;            
116de 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61  /* Pointer to pa
116df 67 65 20 63 61 63 68 65 20 6f 62 6a 65 63 74 20  ge cache object 
116e0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63  */.  sqlite3_bac
116e1 6b 75 70 20 2a 70 42 61 63 6b 75 70 3b 20 20 20  kup *pBackup;   
116e2 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6c   /* Pointer to l
116e3 69 73 74 20 6f 66 20 6f 6e 67 6f 69 6e 67 20 62  ist of ongoing b
116e4 61 63 6b 75 70 20 70 72 6f 63 65 73 73 65 73 20  ackup processes 
116e5 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  */.};../*.** The
116e6 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61   following globa
116e7 6c 20 76 61 72 69 61 62 6c 65 73 20 68 6f 6c 64  l variables hold
116e8 20 63 6f 75 6e 74 65 72 73 20 75 73 65 64 20 66   counters used f
116e9 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 70 75  or.** testing pu
116ea 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 54 68  rposes only.  Th
116eb 65 73 65 20 76 61 72 69 61 62 6c 65 73 20 64 6f  ese variables do
116ec 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 0a 2a 2a   not exist in.**
116ed 20 61 20 6e 6f 6e 2d 74 65 73 74 69 6e 67 20 62   a non-testing b
116ee 75 69 6c 64 2e 20 20 54 68 65 73 65 20 76 61 72  uild.  These var
116ef 69 61 62 6c 65 73 20 61 72 65 20 6e 6f 74 20 74  iables are not t
116f0 68 72 65 61 64 2d 73 61 66 65 2e 0a 2a 2f 0a 23  hread-safe..*/.#
116f1 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
116f2 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
116f3 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 72   sqlite3_pager_r
116f4 65 61 64 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b  eaddb_count = 0;
116f5 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
116f6 20 66 75 6c 6c 20 70 61 67 65 73 20 72 65 61 64   full pages read
116f7 20 66 72 6f 6d 20 44 42 20 2a 2f 0a 53 51 4c 49   from DB */.SQLI
116f8 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
116f9 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62  e3_pager_writedb
116fa 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20 2f 2a  _count = 0;   /*
116fb 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 20   Number of full 
116fc 70 61 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f  pages written to
116fd 20 44 42 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50   DB */.SQLITE_AP
116fe 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61  I int sqlite3_pa
116ff 67 65 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74  ger_writej_count
11700 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62   = 0;    /* Numb
11701 65 72 20 6f 66 20 70 61 67 65 73 20 77 72 69 74  er of pages writ
11702 74 65 6e 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a  ten to journal *
11703 2f 0a 23 20 64 65 66 69 6e 65 20 50 41 47 45 52  /.# define PAGER
11704 5f 49 4e 43 52 28 76 29 20 20 76 2b 2b 0a 23 65  _INCR(v)  v++.#e
11705 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 50 41 47  lse.# define PAG
11706 45 52 5f 49 4e 43 52 28 76 29 0a 23 65 6e 64 69  ER_INCR(v).#endi
11707 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 75 72 6e  f..../*.** Journ
11708 61 6c 20 66 69 6c 65 73 20 62 65 67 69 6e 20 77  al files begin w
11709 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ith the followin
1170a 67 20 6d 61 67 69 63 20 73 74 72 69 6e 67 2e 20  g magic string. 
1170b 20 54 68 65 20 64 61 74 61 0a 2a 2a 20 77 61 73   The data.** was
1170c 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 2f   obtained from /
1170d 64 65 76 2f 72 61 6e 64 6f 6d 2e 20 20 49 74 20  dev/random.  It 
1170e 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 61 73 20  is used only as 
1170f 61 20 73 61 6e 69 74 79 20 63 68 65 63 6b 2e 0a  a sanity check..
11710 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 76 65 72 73  **.** Since vers
11711 69 6f 6e 20 32 2e 38 2e 30 2c 20 74 68 65 20 6a  ion 2.8.0, the j
11712 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 63 6f  ournal format co
11713 6e 74 61 69 6e 73 20 61 64 64 69 74 69 6f 6e 61  ntains additiona
11714 6c 20 73 61 6e 69 74 79 0a 2a 2a 20 63 68 65 63  l sanity.** chec
11715 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  king information
11716 2e 20 20 49 66 20 74 68 65 20 70 6f 77 65 72 20  .  If the power 
11717 66 61 69 6c 73 20 77 68 69 6c 65 20 74 68 65 20  fails while the 
11718 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 69 6e 67  journal is being
11719 0a 2a 2a 20 77 72 69 74 74 65 6e 2c 20 73 65 6d  .** written, sem
1171a 69 2d 72 61 6e 64 6f 6d 20 67 61 72 62 61 67 65  i-random garbage
1171b 20 64 61 74 61 20 6d 69 67 68 74 20 61 70 70 65   data might appe
1171c 61 72 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  ar in the journa
1171d 6c 0a 2a 2a 20 66 69 6c 65 20 61 66 74 65 72 20  l.** file after 
1171e 70 6f 77 65 72 20 69 73 20 72 65 73 74 6f 72 65  power is restore
1171f 64 2e 20 20 49 66 20 61 6e 20 61 74 74 65 6d 70  d.  If an attemp
11720 74 20 69 73 20 74 68 65 6e 20 6d 61 64 65 0a 2a  t is then made.*
11721 2a 20 74 6f 20 72 6f 6c 6c 20 74 68 65 20 6a 6f  * to roll the jo
11722 75 72 6e 61 6c 20 62 61 63 6b 2c 20 74 68 65 20  urnal back, the 
11723 64 61 74 61 62 61 73 65 20 63 6f 75 6c 64 20 62  database could b
11724 65 20 63 6f 72 72 75 70 74 65 64 2e 20 20 54 68  e corrupted.  Th
11725 65 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20  e additional.** 
11726 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20  sanity checking 
11727 64 61 74 61 20 69 73 20 61 6e 20 61 74 74 65 6d  data is an attem
11728 70 74 20 74 6f 20 64 69 73 63 6f 76 65 72 20 74  pt to discover t
11729 68 65 20 67 61 72 62 61 67 65 20 69 6e 20 74 68  he garbage in th
1172a 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 61 6e 64  e.** journal and
1172b 20 69 67 6e 6f 72 65 20 69 74 2e 0a 2a 2a 0a 2a   ignore it..**.*
1172c 2a 20 54 68 65 20 73 61 6e 69 74 79 20 63 68 65  * The sanity che
1172d 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f  cking informatio
1172e 6e 20 66 6f 72 20 74 68 65 20 6e 65 77 20 6a 6f  n for the new jo
1172f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e  urnal format con
11730 73 69 73 74 73 0a 2a 2a 20 6f 66 20 61 20 33 32  sists.** of a 32
11731 2d 62 69 74 20 63 68 65 63 6b 73 75 6d 20 6f 6e  -bit checksum on
11732 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 64 61   each page of da
11733 74 61 2e 20 20 54 68 65 20 63 68 65 63 6b 73 75  ta.  The checksu
11734 6d 20 63 6f 76 65 72 73 20 62 6f 74 68 0a 2a 2a  m covers both.**
11735 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
11736 20 61 6e 64 20 74 68 65 20 70 50 61 67 65 72 2d   and the pPager-
11737 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 20  >pageSize bytes 
11738 6f 66 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  of data for the 
11739 70 61 67 65 2e 0a 2a 2a 20 54 68 69 73 20 63 6b  page..** This ck
1173a 73 75 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a  sum is initializ
1173b 65 64 20 74 6f 20 61 20 33 32 2d 62 69 74 20 72  ed to a 32-bit r
1173c 61 6e 64 6f 6d 20 76 61 6c 75 65 20 74 68 61 74  andom value that
1173d 20 61 70 70 65 61 72 73 20 69 6e 20 74 68 65 0a   appears in the.
1173e 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ** journal file 
1173f 72 69 67 68 74 20 61 66 74 65 72 20 74 68 65 20  right after the 
11740 68 65 61 64 65 72 2e 20 20 54 68 65 20 72 61 6e  header.  The ran
11741 64 6f 6d 20 69 6e 69 74 69 61 6c 69 7a 65 72 20  dom initializer 
11742 69 73 20 69 6d 70 6f 72 74 61 6e 74 2c 0a 2a 2a  is important,.**
11743 20 62 65 63 61 75 73 65 20 67 61 72 62 61 67 65   because garbage
11744 20 64 61 74 61 20 74 68 61 74 20 61 70 70 65 61   data that appea
11745 72 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  rs at the end of
11746 20 61 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 69   a journal is li
11747 6b 65 6c 79 0a 2a 2a 20 64 61 74 61 20 74 68 61  kely.** data tha
11748 74 20 77 61 73 20 6f 6e 63 65 20 69 6e 20 6f 74  t was once in ot
11749 68 65 72 20 66 69 6c 65 73 20 74 68 61 74 20 68  her files that h
1174a 61 76 65 20 6e 6f 77 20 62 65 65 6e 20 64 65 6c  ave now been del
1174b 65 74 65 64 2e 20 20 49 66 20 74 68 65 0a 2a 2a  eted.  If the.**
1174c 20 67 61 72 62 61 67 65 20 64 61 74 61 20 63 61   garbage data ca
1174d 6d 65 20 66 72 6f 6d 20 61 6e 20 6f 62 73 6f 6c  me from an obsol
1174e 65 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ete journal file
1174f 2c 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73 20  , the checksums 
11750 6d 69 67 68 74 0a 2a 2a 20 62 65 20 63 6f 72 72  might.** be corr
11751 65 63 74 2e 20 20 42 75 74 20 62 79 20 69 6e 69  ect.  But by ini
11752 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 63 68  tializing the ch
11753 65 63 6b 73 75 6d 20 74 6f 20 72 61 6e 64 6f 6d  ecksum to random
11754 20 76 61 6c 75 65 20 77 68 69 63 68 0a 2a 2a 20   value which.** 
11755 69 73 20 64 69 66 66 65 72 65 6e 74 20 66 6f 72  is different for
11756 20 65 76 65 72 79 20 6a 6f 75 72 6e 61 6c 2c 20   every journal, 
11757 77 65 20 6d 69 6e 69 6d 69 7a 65 20 74 68 61 74  we minimize that
11758 20 72 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63   risk..*/.static
11759 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
1175a 63 68 61 72 20 61 4a 6f 75 72 6e 61 6c 4d 61 67  char aJournalMag
1175b 69 63 5b 5d 20 3d 20 7b 0a 20 20 30 78 64 39 2c  ic[] = {.  0xd9,
1175c 20 30 78 64 35 2c 20 30 78 30 35 2c 20 30 78 66   0xd5, 0x05, 0xf
1175d 39 2c 20 30 78 32 30 2c 20 30 78 61 31 2c 20 30  9, 0x20, 0xa1, 0
1175e 78 36 33 2c 20 30 78 64 37 2c 0a 7d 3b 0a 0a 2f  x63, 0xd7,.};../
1175f 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66  *.** The size of
11760 20 74 68 65 20 6f 66 20 65 61 63 68 20 70 61 67   the of each pag
11761 65 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65 20  e record in the 
11762 6a 6f 75 72 6e 61 6c 20 69 73 20 67 69 76 65 6e  journal is given
11763 20 62 79 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f   by.** the follo
11764 77 69 6e 67 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23  wing macro..*/.#
11765 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 50  define JOURNAL_P
11766 47 5f 53 5a 28 70 50 61 67 65 72 29 20 20 28 28  G_SZ(pPager)  ((
11767 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
11768 29 20 2b 20 38 29 0a 0a 2f 2a 0a 2a 2a 20 54 68  ) + 8)../*.** Th
11769 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  e journal header
1176a 20 73 69 7a 65 20 66 6f 72 20 74 68 69 73 20 70   size for this p
1176b 61 67 65 72 2e 20 54 68 69 73 20 69 73 20 75 73  ager. This is us
1176c 75 61 6c 6c 79 20 74 68 65 20 73 61 6d 65 20 0a  ually the same .
1176d 2a 2a 20 73 69 7a 65 20 61 73 20 61 20 73 69 6e  ** size as a sin
1176e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 6f 72 2e  gle disk sector.
1176f 20 53 65 65 20 61 6c 73 6f 20 73 65 74 53 65 63   See also setSec
11770 74 6f 72 53 69 7a 65 28 29 2e 0a 2a 2f 0a 23 64  torSize()..*/.#d
11771 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 48 44  efine JOURNAL_HD
11772 52 5f 53 5a 28 70 50 61 67 65 72 29 20 28 70 50  R_SZ(pPager) (pP
11773 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65  ager->sectorSize
11774 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63  )../*.** The mac
11775 72 6f 20 4d 45 4d 44 42 20 69 73 20 74 72 75 65  ro MEMDB is true
11776 20 69 66 20 77 65 20 61 72 65 20 64 65 61 6c 69   if we are deali
11777 6e 67 20 77 69 74 68 20 61 6e 20 69 6e 2d 6d 65  ng with an in-me
11778 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e 0a 2a  mory database..*
11779 2a 20 57 65 20 64 6f 20 74 68 69 73 20 61 73 20  * We do this as 
1177a 61 20 6d 61 63 72 6f 20 73 6f 20 74 68 61 74 20  a macro so that 
1177b 69 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d  if the SQLITE_OM
1177c 49 54 5f 4d 45 4d 4f 52 59 44 42 20 6d 61 63 72  IT_MEMORYDB macr
1177d 6f 20 69 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65  o is set,.** the
1177e 20 76 61 6c 75 65 20 6f 66 20 4d 45 4d 44 42 20   value of MEMDB 
1177f 77 69 6c 6c 20 62 65 20 61 20 63 6f 6e 73 74 61  will be a consta
11780 6e 74 20 61 6e 64 20 74 68 65 20 63 6f 6d 70 69  nt and the compi
11781 6c 65 72 20 77 69 6c 6c 20 6f 70 74 69 6d 69 7a  ler will optimiz
11782 65 0a 2a 2a 20 6f 75 74 20 63 6f 64 65 20 74 68  e.** out code th
11783 61 74 20 77 6f 75 6c 64 20 6e 65 76 65 72 20 65  at would never e
11784 78 65 63 75 74 65 2e 0a 2a 2f 0a 23 69 66 64 65  xecute..*/.#ifde
11785 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45  f SQLITE_OMIT_ME
11786 4d 4f 52 59 44 42 0a 23 20 64 65 66 69 6e 65 20  MORYDB.# define 
11787 4d 45 4d 44 42 20 30 0a 23 65 6c 73 65 0a 23 20  MEMDB 0.#else.# 
11788 64 65 66 69 6e 65 20 4d 45 4d 44 42 20 70 50 61  define MEMDB pPa
11789 67 65 72 2d 3e 6d 65 6d 44 62 0a 23 65 6e 64 69  ger->memDb.#endi
1178a 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78  f../*.** The max
1178b 69 6d 75 6d 20 6c 65 67 61 6c 20 70 61 67 65 20  imum legal page 
1178c 6e 75 6d 62 65 72 20 69 73 20 28 32 5e 33 31 20  number is (2^31 
1178d 2d 20 31 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  - 1)..*/.#define
1178e 20 50 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20   PAGER_MAX_PGNO 
1178f 32 31 34 37 34 38 33 36 34 37 0a 0a 23 69 66 6e  2147483647..#ifn
11790 64 65 66 20 4e 44 45 42 55 47 20 0a 2f 2a 0a 2a  def NDEBUG ./*.*
11791 2a 20 55 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20  * Usage:.**.**  
11792 20 61 73 73 65 72 74 28 20 61 73 73 65 72 74 5f   assert( assert_
11793 70 61 67 65 72 5f 73 74 61 74 65 28 70 50 61 67  pager_state(pPag
11794 65 72 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63  er) );.*/.static
11795 20 69 6e 74 20 61 73 73 65 72 74 5f 70 61 67 65   int assert_page
11796 72 5f 73 74 61 74 65 28 50 61 67 65 72 20 2a 70  r_state(Pager *p
11797 50 61 67 65 72 29 7b 0a 0a 20 20 2f 2a 20 41 20  Pager){..  /* A 
11798 74 65 6d 70 2d 66 69 6c 65 20 69 73 20 61 6c 77  temp-file is alw
11799 61 79 73 20 69 6e 20 50 41 47 45 52 5f 45 58 43  ays in PAGER_EXC
1179a 4c 55 53 49 56 45 20 6f 72 20 50 41 47 45 52 5f  LUSIVE or PAGER_
1179b 53 59 4e 43 45 44 20 73 74 61 74 65 2e 20 2a 2f  SYNCED state. */
1179c 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
1179d 72 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c  r->tempFile==0 |
1179e 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  | pPager->state>
1179f 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45  =PAGER_EXCLUSIVE
117a0 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 63 68   );..  /* The ch
117a1 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c  angeCountDone fl
117a2 61 67 20 69 73 20 61 6c 77 61 79 73 20 73 65 74  ag is always set
117a3 20 66 6f 72 20 74 65 6d 70 2d 66 69 6c 65 73 20   for temp-files 
117a4 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  */.  assert( pPa
117a5 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d 30  ger->tempFile==0
117a6 20 7c 7c 20 70 50 61 67 65 72 2d 3e 63 68 61 6e   || pPager->chan
117a7 67 65 43 6f 75 6e 74 44 6f 6e 65 20 29 3b 0a 0a  geCountDone );..
117a8 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65    return 1;.}.#e
117a9 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ndif../*.** Retu
117aa 72 6e 20 74 72 75 65 20 69 66 20 69 74 20 69 73  rn true if it is
117ab 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77 72   necessary to wr
117ac 69 74 65 20 70 61 67 65 20 2a 70 50 67 20 69 6e  ite page *pPg in
117ad 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e  to the sub-journ
117ae 61 6c 2e 0a 2a 2a 20 41 20 70 61 67 65 20 6e 65  al..** A page ne
117af 65 64 73 20 74 6f 20 62 65 20 77 72 69 74 74 65  eds to be writte
117b0 6e 20 69 6e 74 6f 20 74 68 65 20 73 75 62 2d 6a  n into the sub-j
117b1 6f 75 72 6e 61 6c 20 69 66 20 74 68 65 72 65 20  ournal if there 
117b2 65 78 69 73 74 73 20 6f 6e 65 0a 2a 2a 20 6f 72  exists one.** or
117b3 20 6d 6f 72 65 20 6f 70 65 6e 20 73 61 76 65 70   more open savep
117b4 6f 69 6e 74 73 20 66 6f 72 20 77 68 69 63 68 3a  oints for which:
117b5 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 70  .**.**   * The p
117b6 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 6c 65  age-number is le
117b7 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  ss than or equal
117b8 20 74 6f 20 50 61 67 65 72 53 61 76 65 70 6f 69   to PagerSavepoi
117b9 6e 74 2e 6e 4f 72 69 67 2c 20 61 6e 64 0a 2a 2a  nt.nOrig, and.**
117ba 20 20 20 2a 20 54 68 65 20 62 69 74 20 63 6f 72     * The bit cor
117bb 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68  responding to th
117bc 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73  e page-number is
117bd 20 6e 6f 74 20 73 65 74 20 69 6e 0a 2a 2a 20 20   not set in.**  
117be 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e     PagerSavepoin
117bf 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2e 0a  t.pInSavepoint..
117c0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 75  */.static int su
117c1 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 50  bjRequiresPage(P
117c2 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 67  gHdr *pPg){.  Pg
117c3 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 67 2d 3e 70  no pgno = pPg->p
117c4 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20 2a 70 50  gno;.  Pager *pP
117c5 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67  ager = pPg->pPag
117c6 65 72 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66  er;.  int i;.  f
117c7 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 72  or(i=0; i<pPager
117c8 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 2b  ->nSavepoint; i+
117c9 2b 29 7b 0a 20 20 20 20 50 61 67 65 72 53 61 76  +){.    PagerSav
117ca 65 70 6f 69 6e 74 20 2a 70 20 3d 20 26 70 50 61  epoint *p = &pPa
117cb 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b  ger->aSavepoint[
117cc 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e  i];.    if( p->n
117cd 4f 72 69 67 3e 3d 70 67 6e 6f 20 26 26 20 30 3d  Orig>=pgno && 0=
117ce 3d 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65  =sqlite3BitvecTe
117cf 73 74 28 70 2d 3e 70 49 6e 53 61 76 65 70 6f 69  st(p->pInSavepoi
117d0 6e 74 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 20  nt, pgno) ){.   
117d1 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
117d2 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
117d3 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  0;.}../*.** Retu
117d4 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 70  rn true if the p
117d5 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 69  age is already i
117d6 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  n the journal fi
117d7 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
117d8 74 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28  t pageInJournal(
117d9 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 72  PgHdr *pPg){.  r
117da 65 74 75 72 6e 20 73 71 6c 69 74 65 33 42 69 74  eturn sqlite3Bit
117db 76 65 63 54 65 73 74 28 70 50 67 2d 3e 70 50 61  vecTest(pPg->pPa
117dc 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c  ger->pInJournal,
117dd 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a   pPg->pgno);.}..
117de 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 33 32 2d  /*.** Read a 32-
117df 62 69 74 20 69 6e 74 65 67 65 72 20 66 72 6f 6d  bit integer from
117e0 20 74 68 65 20 67 69 76 65 6e 20 66 69 6c 65 20   the given file 
117e1 64 65 73 63 72 69 70 74 6f 72 2e 20 20 53 74 6f  descriptor.  Sto
117e2 72 65 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a  re the integer.*
117e3 2a 20 74 68 61 74 20 69 73 20 72 65 61 64 20 69  * that is read i
117e4 6e 20 2a 70 52 65 73 2e 20 20 52 65 74 75 72 6e  n *pRes.  Return
117e5 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76   SQLITE_OK if ev
117e6 65 72 79 74 68 69 6e 67 20 77 6f 72 6b 65 64 2c  erything worked,
117e7 20 6f 72 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20   or an.** error 
117e8 63 6f 64 65 20 69 73 20 73 6f 6d 65 74 68 69 6e  code is somethin
117e9 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a  g goes wrong..**
117ea 0a 2a 2a 20 41 6c 6c 20 76 61 6c 75 65 73 20 61  .** All values a
117eb 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73  re stored on dis
117ec 6b 20 61 73 20 62 69 67 2d 65 6e 64 69 61 6e 2e  k as big-endian.
117ed 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
117ee 65 61 64 33 32 62 69 74 73 28 73 71 6c 69 74 65  ead32bits(sqlite
117ef 33 5f 66 69 6c 65 20 2a 66 64 2c 20 69 36 34 20  3_file *fd, i64 
117f0 6f 66 66 73 65 74 2c 20 75 33 32 20 2a 70 52 65  offset, u32 *pRe
117f1 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  s){.  unsigned c
117f2 68 61 72 20 61 63 5b 34 5d 3b 0a 20 20 69 6e 74  har ac[4];.  int
117f3 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52   rc = sqlite3OsR
117f4 65 61 64 28 66 64 2c 20 61 63 2c 20 73 69 7a 65  ead(fd, ac, size
117f5 6f 66 28 61 63 29 2c 20 6f 66 66 73 65 74 29 3b  of(ac), offset);
117f6 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
117f7 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 52 65  E_OK ){.    *pRe
117f8 73 20 3d 20 73 71 6c 69 74 65 33 47 65 74 34 62  s = sqlite3Get4b
117f9 79 74 65 28 61 63 29 3b 0a 20 20 7d 0a 20 20 72  yte(ac);.  }.  r
117fa 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
117fb 2a 2a 20 57 72 69 74 65 20 61 20 33 32 2d 62 69  ** Write a 32-bi
117fc 74 20 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 61  t integer into a
117fd 20 73 74 72 69 6e 67 20 62 75 66 66 65 72 20 69   string buffer i
117fe 6e 20 62 69 67 2d 65 6e 64 69 61 6e 20 62 79 74  n big-endian byt
117ff 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 23 64 65 66  e order..*/.#def
11800 69 6e 65 20 70 75 74 33 32 62 69 74 73 28 41 2c  ine put32bits(A,
11801 42 29 20 20 73 71 6c 69 74 65 33 50 75 74 34 62  B)  sqlite3Put4b
11802 79 74 65 28 28 75 38 2a 29 41 2c 42 29 0a 0a 2f  yte((u8*)A,B)../
11803 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 33 32 2d  *.** Write a 32-
11804 62 69 74 20 69 6e 74 65 67 65 72 20 69 6e 74 6f  bit integer into
11805 20 74 68 65 20 67 69 76 65 6e 20 66 69 6c 65 20   the given file 
11806 64 65 73 63 72 69 70 74 6f 72 2e 20 20 52 65 74  descriptor.  Ret
11807 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a  urn SQLITE_OK.**
11808 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61   on success or a
11809 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  n error code is 
1180a 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77  something goes w
1180b 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rong..*/.static 
1180c 69 6e 74 20 77 72 69 74 65 33 32 62 69 74 73 28  int write32bits(
1180d 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64  sqlite3_file *fd
1180e 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20 75 33  , i64 offset, u3
1180f 32 20 76 61 6c 29 7b 0a 20 20 63 68 61 72 20 61  2 val){.  char a
11810 63 5b 34 5d 3b 0a 20 20 70 75 74 33 32 62 69 74  c[4];.  put32bit
11811 73 28 61 63 2c 20 76 61 6c 29 3b 0a 20 20 72 65  s(ac, val);.  re
11812 74 75 72 6e 20 73 71 6c 69 74 65 33 4f 73 57 72  turn sqlite3OsWr
11813 69 74 65 28 66 64 2c 20 61 63 2c 20 34 2c 20 6f  ite(fd, ac, 4, o
11814 66 66 73 65 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ffset);.}../*.**
11815 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f   The argument to
11816 20 74 68 69 73 20 6d 61 63 72 6f 20 69 73 20 61   this macro is a
11817 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
11818 20 28 74 79 70 65 20 73 71 6c 69 74 65 33 5f 66   (type sqlite3_f
11819 69 6c 65 2a 29 2e 0a 2a 2a 20 52 65 74 75 72 6e  ile*)..** Return
1181a 20 30 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20   0 if it is not 
1181b 6f 70 65 6e 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72  open, or non-zer
1181c 6f 20 28 62 75 74 20 6e 6f 74 20 31 29 20 69 66  o (but not 1) if
1181d 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   it is..**.** Th
1181e 69 73 20 69 73 20 73 6f 20 74 68 61 74 20 65 78  is is so that ex
1181f 70 72 65 73 73 69 6f 6e 73 20 63 61 6e 20 62 65  pressions can be
11820 20 77 72 69 74 74 65 6e 20 61 73 3a 0a 2a 2a 0a   written as:.**.
11821 2a 2a 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28  **   if( isOpen(
11822 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 20  pPager->jfd) ){ 
11823 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 69 6e 73 74 65 61  ....**.** instea
11824 64 20 6f 66 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28  d of.**.**   if(
11825 20 70 50 61 67 65 72 2d 3e 6a 66 64 2d 3e 70 4d   pPager->jfd->pM
11826 65 74 68 6f 64 73 20 29 7b 20 2e 2e 2e 0a 2a 2f  ethods ){ ....*/
11827 0a 23 64 65 66 69 6e 65 20 69 73 4f 70 65 6e 28  .#define isOpen(
11828 70 46 64 29 20 28 28 70 46 64 29 2d 3e 70 4d 65  pFd) ((pFd)->pMe
11829 74 68 6f 64 73 29 0a 0a 2f 2a 0a 2a 2a 20 49 66  thods)../*.** If
1182a 20 66 69 6c 65 20 70 46 64 20 69 73 20 6f 70 65   file pFd is ope
1182b 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4f  n, call sqlite3O
1182c 73 55 6e 6c 6f 63 6b 28 29 20 6f 6e 20 69 74 2e  sUnlock() on it.
1182d 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
1182e 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  sUnlock(sqlite3_
1182f 66 69 6c 65 20 2a 70 46 64 2c 20 69 6e 74 20 65  file *pFd, int e
11830 4c 6f 63 6b 29 7b 0a 20 20 69 66 28 20 21 69 73  Lock){.  if( !is
11831 4f 70 65 6e 28 70 46 64 29 20 29 7b 0a 20 20 20  Open(pFd) ){.   
11832 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
11833 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  K;.  }.  return 
11834 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28  sqlite3OsUnlock(
11835 70 46 64 2c 20 65 4c 6f 63 6b 29 3b 0a 7d 0a 0a  pFd, eLock);.}..
11836 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
11837 69 6f 6e 20 64 65 74 65 72 6d 69 6e 65 73 20 77  ion determines w
11838 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
11839 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f  e atomic-write o
1183a 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63  ptimization.** c
1183b 61 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20  an be used with 
1183c 74 68 69 73 20 70 61 67 65 72 2e 20 54 68 65 20  this pager. The 
1183d 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63 61 6e  optimization can
1183e 20 62 65 20 75 73 65 64 20 69 66 3a 0a 2a 2a 0a   be used if:.**.
1183f 2a 2a 20 20 28 61 29 20 74 68 65 20 76 61 6c 75  **  (a) the valu
11840 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f 73  e returned by Os
11841 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
11842 73 74 69 63 73 28 29 20 69 6e 64 69 63 61 74 65  stics() indicate
11843 73 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 61  s that.**      a
11844 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 6d   database page m
11845 61 79 20 62 65 20 77 72 69 74 74 65 6e 20 61 74  ay be written at
11846 6f 6d 69 63 61 6c 6c 79 2c 20 61 6e 64 0a 2a 2a  omically, and.**
11847 20 20 28 62 29 20 74 68 65 20 76 61 6c 75 65 20    (b) the value 
11848 72 65 74 75 72 6e 65 64 20 62 79 20 4f 73 53 65  returned by OsSe
11849 63 74 6f 72 53 69 7a 65 28 29 20 69 73 20 6c 65  ctorSize() is le
1184a 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  ss than or equal
1184b 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20  .**      to the 
1184c 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a  page size..**.**
1184d 20 54 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f   The optimizatio
1184e 6e 20 69 73 20 61 6c 73 6f 20 61 6c 77 61 79 73  n is also always
1184f 20 65 6e 61 62 6c 65 64 20 66 6f 72 20 74 65 6d   enabled for tem
11850 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 20 49 74  porary files. It
11851 20 69 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20   is.** an error 
11852 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e  to call this fun
11853 63 74 69 6f 6e 20 69 66 20 70 50 61 67 65 72 20  ction if pPager 
11854 69 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 6e 20  is opened on an 
11855 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74  in-memory.** dat
11856 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  abase..**.** If 
11857 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  the optimization
11858 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 2c   cannot be used,
11859 20 30 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20   0 is returned. 
1185a 49 66 20 69 74 20 63 61 6e 20 62 65 20 75 73 65  If it can be use
1185b 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 76  d,.** then the v
1185c 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73  alue returned is
1185d 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
1185e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68   journal file wh
1185f 65 6e 20 69 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e  en it.** contain
11860 73 20 72 6f 6c 6c 62 61 63 6b 20 64 61 74 61 20  s rollback data 
11861 66 6f 72 20 65 78 61 63 74 6c 79 20 6f 6e 65 20  for exactly one 
11862 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  page..*/.#ifdef 
11863 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54  SQLITE_ENABLE_AT
11864 4f 4d 49 43 5f 57 52 49 54 45 0a 73 74 61 74 69  OMIC_WRITE.stati
11865 63 20 69 6e 74 20 6a 72 6e 6c 42 75 66 66 65 72  c int jrnlBuffer
11866 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67  Size(Pager *pPag
11867 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21  er){.  assert( !
11868 4d 45 4d 44 42 20 29 3b 0a 20 20 69 66 28 20 21  MEMDB );.  if( !
11869 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
1186a 20 29 7b 0a 20 20 20 20 69 6e 74 20 64 63 3b 20   ){.    int dc; 
1186b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1186c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 76            /* Dev
1186d 69 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74  ice characterist
1186e 69 63 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  ics */.    int n
1186f 53 65 63 74 6f 72 3b 20 20 20 20 20 20 20 20 20  Sector;         
11870 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11871 53 65 63 74 6f 72 20 73 69 7a 65 20 2a 2f 0a 20  Sector size */. 
11872 20 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20     int szPage;  
11873 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11874 20 20 20 20 20 2f 2a 20 50 61 67 65 20 73 69 7a       /* Page siz
11875 65 20 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74  e */..    assert
11876 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
11877 3e 66 64 29 20 29 3b 0a 20 20 20 20 64 63 20 3d  >fd) );.    dc =
11878 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65   sqlite3OsDevice
11879 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28  Characteristics(
1187a 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20  pPager->fd);.   
1187b 20 6e 53 65 63 74 6f 72 20 3d 20 70 50 61 67 65   nSector = pPage
1187c 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3b 0a 20  r->sectorSize;. 
1187d 20 20 20 73 7a 50 61 67 65 20 3d 20 70 50 61 67     szPage = pPag
1187e 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 0a 20  er->pageSize;.. 
1187f 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45     assert(SQLITE
11880 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32  _IOCAP_ATOMIC512
11881 3d 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20  ==(512>>8));.   
11882 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49   assert(SQLITE_I
11883 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d  OCAP_ATOMIC64K==
11884 28 36 35 35 33 36 3e 3e 38 29 29 3b 0a 20 20 20  (65536>>8));.   
11885 20 69 66 28 20 30 3d 3d 28 64 63 26 28 53 51 4c   if( 0==(dc&(SQL
11886 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
11887 7c 28 73 7a 50 61 67 65 3e 3e 38 29 29 20 7c 7c  |(szPage>>8)) ||
11888 20 6e 53 65 63 74 6f 72 3e 73 7a 50 61 67 65 29   nSector>szPage)
11889 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1188a 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   0;.    }.  }.. 
1188b 20 72 65 74 75 72 6e 20 4a 4f 55 52 4e 41 4c 5f   return JOURNAL_
1188c 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 2b  HDR_SZ(pPager) +
1188d 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70   JOURNAL_PG_SZ(p
1188e 50 61 67 65 72 29 3b 0a 7d 0a 23 65 6e 64 69 66  Pager);.}.#endif
1188f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54  ../*.** If SQLIT
11890 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 20 69 73  E_CHECK_PAGES is
11891 20 64 65 66 69 6e 65 64 20 74 68 65 6e 20 77 65   defined then we
11892 20 64 6f 20 73 6f 6d 65 20 73 61 6e 69 74 79 20   do some sanity 
11893 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 6f 6e 20 74  checking.** on t
11894 68 65 20 63 61 63 68 65 20 75 73 69 6e 67 20 61  he cache using a
11895 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 2e 20   hash function. 
11896 20 54 68 69 73 20 69 73 20 75 73 65 64 20 66 6f   This is used fo
11897 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64  r testing.** and
11898 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e   debugging only.
11899 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
1189a 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 2f 2a  E_CHECK_PAGES./*
1189b 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 33 32 2d  .** Return a 32-
1189c 62 69 74 20 68 61 73 68 20 6f 66 20 74 68 65 20  bit hash of the 
1189d 70 61 67 65 20 64 61 74 61 20 66 6f 72 20 70 50  page data for pP
1189e 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75  age..*/.static u
1189f 33 32 20 70 61 67 65 72 5f 64 61 74 61 68 61 73  32 pager_datahas
118a0 68 28 69 6e 74 20 6e 42 79 74 65 2c 20 75 6e 73  h(int nByte, uns
118a1 69 67 6e 65 64 20 63 68 61 72 20 2a 70 44 61 74  igned char *pDat
118a2 61 29 7b 0a 20 20 75 33 32 20 68 61 73 68 20 3d  a){.  u32 hash =
118a3 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66   0;.  int i;.  f
118a4 6f 72 28 69 3d 30 3b 20 69 3c 6e 42 79 74 65 3b  or(i=0; i<nByte;
118a5 20 69 2b 2b 29 7b 0a 20 20 20 20 68 61 73 68 20   i++){.    hash 
118a6 3d 20 28 68 61 73 68 2a 31 30 33 39 29 20 2b 20  = (hash*1039) + 
118a7 70 44 61 74 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20  pData[i];.  }.  
118a8 72 65 74 75 72 6e 20 68 61 73 68 3b 0a 7d 0a 73  return hash;.}.s
118a9 74 61 74 69 63 20 75 33 32 20 70 61 67 65 72 5f  tatic u32 pager_
118aa 70 61 67 65 68 61 73 68 28 50 67 48 64 72 20 2a  pagehash(PgHdr *
118ab 70 50 61 67 65 29 7b 0a 20 20 72 65 74 75 72 6e  pPage){.  return
118ac 20 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28   pager_datahash(
118ad 70 50 61 67 65 2d 3e 70 50 61 67 65 72 2d 3e 70  pPage->pPager->p
118ae 61 67 65 53 69 7a 65 2c 20 28 75 6e 73 69 67 6e  ageSize, (unsign
118af 65 64 20 63 68 61 72 20 2a 29 70 50 61 67 65 2d  ed char *)pPage-
118b0 3e 70 44 61 74 61 29 3b 0a 7d 0a 73 74 61 74 69  >pData);.}.stati
118b1 63 20 76 6f 69 64 20 70 61 67 65 72 5f 73 65 74  c void pager_set
118b2 5f 70 61 67 65 68 61 73 68 28 50 67 48 64 72 20  _pagehash(PgHdr 
118b3 2a 70 50 61 67 65 29 7b 0a 20 20 70 50 61 67 65  *pPage){.  pPage
118b4 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67  ->pageHash = pag
118b5 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 61 67  er_pagehash(pPag
118b6 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  e);.}../*.** The
118b7 20 43 48 45 43 4b 5f 50 41 47 45 20 6d 61 63 72   CHECK_PAGE macr
118b8 6f 20 74 61 6b 65 73 20 61 20 50 67 48 64 72 2a  o takes a PgHdr*
118b9 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e   as an argument.
118ba 20 49 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b   If SQLITE_CHECK
118bb 5f 50 41 47 45 53 0a 2a 2a 20 69 73 20 64 65 66  _PAGES.** is def
118bc 69 6e 65 64 2c 20 61 6e 64 20 4e 44 45 42 55 47  ined, and NDEBUG
118bd 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2c   is not defined,
118be 20 61 6e 20 61 73 73 65 72 74 28 29 20 73 74 61   an assert() sta
118bf 74 65 6d 65 6e 74 20 63 68 65 63 6b 73 0a 2a 2a  tement checks.**
118c0 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 69   that the page i
118c1 73 20 65 69 74 68 65 72 20 64 69 72 74 79 20 6f  s either dirty o
118c2 72 20 73 74 69 6c 6c 20 6d 61 74 63 68 65 73 20  r still matches 
118c3 74 68 65 20 63 61 6c 63 75 6c 61 74 65 64 20 70  the calculated p
118c4 61 67 65 2d 68 61 73 68 2e 0a 2a 2f 0a 23 64 65  age-hash..*/.#de
118c5 66 69 6e 65 20 43 48 45 43 4b 5f 50 41 47 45 28  fine CHECK_PAGE(
118c6 78 29 20 63 68 65 63 6b 50 61 67 65 28 78 29 0a  x) checkPage(x).
118c7 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 63  static void chec
118c8 6b 50 61 67 65 28 50 67 48 64 72 20 2a 70 50 67  kPage(PgHdr *pPg
118c9 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  ){.  Pager *pPag
118ca 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72  er = pPg->pPager
118cb 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 67  ;.  assert( !pPg
118cc 2d 3e 70 61 67 65 48 61 73 68 20 7c 7c 20 70 50  ->pageHash || pP
118cd 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 0a 20 20  ager->errCode.  
118ce 20 20 20 20 7c 7c 20 28 70 50 67 2d 3e 66 6c 61      || (pPg->fla
118cf 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 29 20  gs&PGHDR_DIRTY) 
118d0 7c 7c 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68  || pPg->pageHash
118d1 3d 3d 70 61 67 65 72 5f 70 61 67 65 68 61 73 68  ==pager_pagehash
118d2 28 70 50 67 29 20 29 3b 0a 7d 0a 0a 23 65 6c 73  (pPg) );.}..#els
118d3 65 0a 23 64 65 66 69 6e 65 20 70 61 67 65 72 5f  e.#define pager_
118d4 64 61 74 61 68 61 73 68 28 58 2c 59 29 20 20 30  datahash(X,Y)  0
118d5 0a 23 64 65 66 69 6e 65 20 70 61 67 65 72 5f 70  .#define pager_p
118d6 61 67 65 68 61 73 68 28 58 29 20 20 30 0a 23 64  agehash(X)  0.#d
118d7 65 66 69 6e 65 20 43 48 45 43 4b 5f 50 41 47 45  efine CHECK_PAGE
118d8 28 78 29 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53  (x).#endif  /* S
118d9 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45  QLITE_CHECK_PAGE
118da 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e  S */../*.** When
118db 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20   this is called 
118dc 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
118dd 20 66 6f 72 20 70 61 67 65 72 20 70 50 61 67 65   for pager pPage
118de 72 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 2e 0a  r must be open..
118df 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
118e0 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 61   attempts to rea
118e1 64 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  d a master journ
118e2 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 66 72 6f  al file name fro
118e3 6d 20 74 68 65 20 0a 2a 2a 20 65 6e 64 20 6f 66  m the .** end of
118e4 20 74 68 65 20 66 69 6c 65 20 61 6e 64 2c 20 69   the file and, i
118e5 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 63 6f  f successful, co
118e6 70 69 65 73 20 69 74 20 69 6e 74 6f 20 6d 65 6d  pies it into mem
118e7 6f 72 79 20 73 75 70 70 6c 69 65 64 20 0a 2a 2a  ory supplied .**
118e8 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20   by the caller. 
118e9 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f  See comments abo
118ea 76 65 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f  ve writeMasterJo
118eb 75 72 6e 61 6c 28 29 20 66 6f 72 20 74 68 65 20  urnal() for the 
118ec 66 6f 72 6d 61 74 0a 2a 2a 20 75 73 65 64 20 74  format.** used t
118ed 6f 20 73 74 6f 72 65 20 61 20 6d 61 73 74 65 72  o store a master
118ee 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61   journal file na
118ef 6d 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  me at the end of
118f0 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e   a journal file.
118f1 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73 74 65 72 20 6d  .**.** zMaster m
118f2 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 62  ust point to a b
118f3 75 66 66 65 72 20 6f 66 20 61 74 20 6c 65 61 73  uffer of at leas
118f4 74 20 6e 4d 61 73 74 65 72 20 62 79 74 65 73 20  t nMaster bytes 
118f5 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a 20  allocated by.** 
118f6 74 68 65 20 63 61 6c 6c 65 72 2e 20 54 68 69 73  the caller. This
118f7 20 73 68 6f 75 6c 64 20 62 65 20 73 71 6c 69 74   should be sqlit
118f8 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d  e3_vfs.mxPathnam
118f9 65 2b 31 20 28 74 6f 20 65 6e 73 75 72 65 20 74  e+1 (to ensure t
118fa 68 65 72 65 20 69 73 0a 2a 2a 20 65 6e 6f 75 67  here is.** enoug
118fb 68 20 73 70 61 63 65 20 74 6f 20 77 72 69 74 65  h space to write
118fc 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
118fd 6e 61 6c 20 6e 61 6d 65 29 2e 20 49 66 20 74 68  nal name). If th
118fe 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
118ff 0a 2a 2a 20 6e 61 6d 65 20 69 6e 20 74 68 65 20  .** name in the 
11900 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 6f 6e 67 65  journal is longe
11901 72 20 74 68 61 6e 20 6e 4d 61 73 74 65 72 20 62  r than nMaster b
11902 79 74 65 73 20 28 69 6e 63 6c 75 64 69 6e 67 20  ytes (including 
11903 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61  a.** nul-termina
11904 74 6f 72 29 2c 20 74 68 65 6e 20 74 68 69 73 20  tor), then this 
11905 69 73 20 68 61 6e 64 6c 65 64 20 61 73 20 69 66  is handled as if
11906 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e   no master journ
11907 61 6c 20 6e 61 6d 65 0a 2a 2a 20 77 65 72 65 20  al name.** were 
11908 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 6a  present in the j
11909 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66  ournal..**.** If
1190a 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
1190b 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 70  l file name is p
1190c 72 65 73 65 6e 74 20 61 74 20 74 68 65 20 65 6e  resent at the en
1190d 64 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  d of the journal
1190e 0a 2a 2a 20 66 69 6c 65 2c 20 74 68 65 6e 20 69  .** file, then i
1190f 74 20 69 73 20 63 6f 70 69 65 64 20 69 6e 74 6f  t is copied into
11910 20 74 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e   the buffer poin
11911 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 73 74 65  ted to by zMaste
11912 72 2e 20 41 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d  r. A.** nul-term
11913 69 6e 61 74 6f 72 20 62 79 74 65 20 69 73 20 61  inator byte is a
11914 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 62  ppended to the b
11915 75 66 66 65 72 20 66 6f 6c 6c 6f 77 69 6e 67 20  uffer following 
11916 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f  the master.** jo
11917 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 2e  urnal file name.
11918 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73 20  .**.** If it is 
11919 64 65 74 65 72 6d 69 6e 65 64 20 74 68 61 74 20  determined that 
1191a 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  no master journa
1191b 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 70  l file name is p
1191c 72 65 73 65 6e 74 20 0a 2a 2a 20 7a 4d 61 73 74  resent .** zMast
1191d 65 72 5b 30 5d 20 69 73 20 73 65 74 20 74 6f 20  er[0] is set to 
1191e 30 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20  0 and SQLITE_OK 
1191f 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
11920 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
11921 72 73 20 77 68 69 6c 65 20 72 65 61 64 69 6e 67  rs while reading
11922 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61   from the journa
11923 6c 20 66 69 6c 65 2c 20 61 6e 20 53 51 4c 69 74  l file, an SQLit
11924 65 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20  e.** error code 
11925 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
11926 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 4d  static int readM
11927 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 73 71 6c  asterJournal(sql
11928 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 72 6e 6c  ite3_file *pJrnl
11929 2c 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c  , char *zMaster,
1192a 20 75 33 32 20 6e 4d 61 73 74 65 72 29 7b 0a 20   u32 nMaster){. 
1192b 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
1192c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
1192d 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
1192e 75 33 32 20 6c 65 6e 3b 20 20 20 20 20 20 20 20  u32 len;        
1192f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
11930 6e 67 74 68 20 69 6e 20 62 79 74 65 73 20 6f 66  ngth in bytes of
11931 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
11932 6e 61 6d 65 20 2a 2f 0a 20 20 69 36 34 20 73 7a  name */.  i64 sz
11933 4a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  J;              
11934 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69       /* Total si
11935 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 6a  ze in bytes of j
11936 6f 75 72 6e 61 6c 20 66 69 6c 65 20 70 4a 72 6e  ournal file pJrn
11937 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d  l */.  u32 cksum
11938 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11939 20 20 2f 2a 20 4d 4a 20 63 68 65 63 6b 73 75 6d    /* MJ checksum
1193a 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d   value read from
1193b 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33   journal */.  u3
1193c 32 20 75 3b 20 20 20 20 20 20 20 20 20 20 20 20  2 u;            
1193d 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69           /* Unsi
1193e 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65  gned loop counte
1193f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  r */.  unsigned 
11940 63 68 61 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20  char aMagic[8]; 
11941 20 20 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f    /* A buffer to
11942 20 68 6f 6c 64 20 74 68 65 20 6d 61 67 69 63 20   hold the magic 
11943 68 65 61 64 65 72 20 2a 2f 0a 20 20 7a 4d 61 73  header */.  zMas
11944 74 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 0a  ter[0] = '\0';..
11945 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
11946 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73  =(rc = sqlite3Os
11947 46 69 6c 65 53 69 7a 65 28 70 4a 72 6e 6c 2c 20  FileSize(pJrnl, 
11948 26 73 7a 4a 29 29 0a 20 20 20 7c 7c 20 73 7a 4a  &szJ)).   || szJ
11949 3c 31 36 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45  <16.   || SQLITE
1194a 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33  _OK!=(rc = read3
1194b 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a  2bits(pJrnl, szJ
1194c 2d 31 36 2c 20 26 6c 65 6e 29 29 0a 20 20 20 7c  -16, &len)).   |
1194d 7c 20 6c 65 6e 3e 3d 6e 4d 61 73 74 65 72 20 0a  | len>=nMaster .
1194e 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21     || SQLITE_OK!
1194f 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62 69 74  =(rc = read32bit
11950 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 32 2c  s(pJrnl, szJ-12,
11951 20 26 63 6b 73 75 6d 29 29 0a 20 20 20 7c 7c 20   &cksum)).   || 
11952 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
11953 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70   sqlite3OsRead(p
11954 4a 72 6e 6c 2c 20 61 4d 61 67 69 63 2c 20 38 2c  Jrnl, aMagic, 8,
11955 20 73 7a 4a 2d 38 29 29 0a 20 20 20 7c 7c 20 6d   szJ-8)).   || m
11956 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a  emcmp(aMagic, aJ
11957 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 38 29 0a  ournalMagic, 8).
11958 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21     || SQLITE_OK!
11959 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73  =(rc = sqlite3Os
1195a 52 65 61 64 28 70 4a 72 6e 6c 2c 20 7a 4d 61 73  Read(pJrnl, zMas
1195b 74 65 72 2c 20 6c 65 6e 2c 20 73 7a 4a 2d 31 36  ter, len, szJ-16
1195c 2d 6c 65 6e 29 29 0a 20 20 29 7b 0a 20 20 20 20  -len)).  ){.    
1195d 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
1195e 20 20 2f 2a 20 53 65 65 20 69 66 20 74 68 65 20    /* See if the 
1195f 63 68 65 63 6b 73 75 6d 20 6d 61 74 63 68 65 73  checksum matches
11960 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
11961 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 66 6f  nal name */.  fo
11962 72 28 75 3d 30 3b 20 75 3c 6c 65 6e 3b 20 75 2b  r(u=0; u<len; u+
11963 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2d 3d  +){.    cksum -=
11964 20 7a 4d 61 73 74 65 72 5b 75 5d 3b 0a 20 20 7d   zMaster[u];.  }
11965 0a 20 20 69 66 28 20 63 6b 73 75 6d 20 29 7b 0a  .  if( cksum ){.
11966 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 68      /* If the ch
11967 65 63 6b 73 75 6d 20 64 6f 65 73 6e 27 74 20 61  ecksum doesn't a
11968 64 64 20 75 70 2c 20 74 68 65 6e 20 6f 6e 65 20  dd up, then one 
11969 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 64  or more of the d
1196a 69 73 6b 20 73 65 63 74 6f 72 73 0a 20 20 20 20  isk sectors.    
1196b 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ** containing th
1196c 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
1196d 20 66 69 6c 65 6e 61 6d 65 20 69 73 20 63 6f 72   filename is cor
1196e 72 75 70 74 65 64 2e 20 54 68 69 73 20 6d 65 61  rupted. This mea
1196f 6e 73 0a 20 20 20 20 2a 2a 20 64 65 66 69 6e 69  ns.    ** defini
11970 74 65 6c 79 20 72 6f 6c 6c 20 62 61 63 6b 2c 20  tely roll back, 
11971 73 6f 20 6a 75 73 74 20 72 65 74 75 72 6e 20 53  so just return S
11972 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 72 65 70  QLITE_OK and rep
11973 6f 72 74 20 61 20 28 6e 75 6c 29 0a 20 20 20 20  ort a (nul).    
11974 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61  ** master-journa
11975 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 20 20 20 20  l filename..    
11976 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d 20 30 3b 0a  */.    len = 0;.
11977 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 5b 6c 65    }.  zMaster[le
11978 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 0a 20  n] = '\0';.   . 
11979 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1197a 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  K;.}../*.** Retu
1197b 72 6e 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66  rn the offset of
1197c 20 74 68 65 20 73 65 63 74 6f 72 20 62 6f 75 6e   the sector boun
1197d 64 61 72 79 20 61 74 20 6f 72 20 69 6d 6d 65 64  dary at or immed
1197e 69 61 74 65 6c 79 20 0a 2a 2a 20 66 6f 6c 6c 6f  iately .** follo
1197f 77 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 69  wing the value i
11980 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  n pPager->journa
11981 6c 4f 66 66 2c 20 61 73 73 75 6d 69 6e 67 20 61  lOff, assuming a
11982 20 73 65 63 74 6f 72 20 0a 2a 2a 20 73 69 7a 65   sector .** size
11983 20 6f 66 20 70 50 61 67 65 72 2d 3e 73 65 63 74   of pPager->sect
11984 6f 72 53 69 7a 65 20 62 79 74 65 73 2e 0a 2a 2a  orSize bytes..**
11985 0a 2a 2a 20 69 2e 65 20 66 6f 72 20 61 20 73 65  .** i.e for a se
11986 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 35 31 32  ctor size of 512
11987 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 61 67 65 72 2e  :.**.**   Pager.
11988 6a 6f 75 72 6e 61 6c 4f 66 66 20 20 20 20 20 20  journalOff      
11989 20 20 20 20 52 65 74 75 72 6e 20 76 61 6c 75 65      Return value
1198a 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .**   ----------
1198b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1198c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
1198d 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20     0            
1198e 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 2a               0.*
1198f 2a 20 20 20 35 31 32 20 20 20 20 20 20 20 20 20  *   512         
11990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 31                51
11991 32 0a 2a 2a 20 20 20 31 30 30 20 20 20 20 20 20  2.**   100      
11992 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11993 20 35 31 32 0a 2a 2a 20 20 20 32 30 30 30 20 20   512.**   2000  
11994 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11995 20 20 20 20 32 30 34 38 0a 2a 2a 20 0a 2a 2f 0a      2048.** .*/.
11996 73 74 61 74 69 63 20 69 36 34 20 6a 6f 75 72 6e  static i64 journ
11997 61 6c 48 64 72 4f 66 66 73 65 74 28 50 61 67 65  alHdrOffset(Page
11998 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 36  r *pPager){.  i6
11999 34 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20  4 offset = 0;.  
1199a 69 36 34 20 63 20 3d 20 70 50 61 67 65 72 2d 3e  i64 c = pPager->
1199b 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 69 66  journalOff;.  if
1199c 28 20 63 20 29 7b 0a 20 20 20 20 6f 66 66 73 65  ( c ){.    offse
1199d 74 20 3d 20 28 28 63 2d 31 29 2f 4a 4f 55 52 4e  t = ((c-1)/JOURN
1199e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
1199f 29 20 2b 20 31 29 20 2a 20 4a 4f 55 52 4e 41 4c  ) + 1) * JOURNAL
119a0 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b  _HDR_SZ(pPager);
119a1 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6f  .  }.  assert( o
119a2 66 66 73 65 74 25 4a 4f 55 52 4e 41 4c 5f 48 44  ffset%JOURNAL_HD
119a3 52 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d 30 20  R_SZ(pPager)==0 
119a4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66  );.  assert( off
119a5 73 65 74 3e 3d 63 20 29 3b 0a 20 20 61 73 73 65  set>=c );.  asse
119a6 72 74 28 20 28 6f 66 66 73 65 74 2d 63 29 3c 4a  rt( (offset-c)<J
119a7 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50  OURNAL_HDR_SZ(pP
119a8 61 67 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72  ager) );.  retur
119a9 6e 20 6f 66 66 73 65 74 3b 0a 7d 0a 0a 2f 2a 0a  n offset;.}../*.
119aa 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ** The journal f
119ab 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e  ile must be open
119ac 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
119ad 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a  ion is called..*
119ae 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
119af 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66  on is a no-op if
119b0 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
119b1 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 77  e has not been w
119b2 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 77 69 74  ritten to.** wit
119b3 68 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20  hin the current 
119b4 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 69 2e 65  transaction (i.e
119b5 2e 20 69 66 20 50 61 67 65 72 2e 6a 6f 75 72 6e  . if Pager.journ
119b6 61 6c 4f 66 66 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a  alOff==0)..**.**
119b7 20 49 66 20 64 6f 54 72 75 6e 63 61 74 65 20 69   If doTruncate i
119b8 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 20 74 68  s non-zero or th
119b9 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 53  e Pager.journalS
119ba 69 7a 65 4c 69 6d 69 74 20 76 61 72 69 61 62 6c  izeLimit variabl
119bb 65 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30  e is.** set to 0
119bc 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65 20  , then truncate 
119bd 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
119be 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69   to zero bytes i
119bf 6e 20 73 69 7a 65 2e 20 4f 74 68 65 72 77 69 73  n size. Otherwis
119c0 65 2c 0a 2a 2a 20 7a 65 72 6f 20 74 68 65 20 32  e,.** zero the 2
119c1 38 2d 62 79 74 65 20 68 65 61 64 65 72 20 61 74  8-byte header at
119c2 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
119c3 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  e journal file. 
119c4 49 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c 20  In either case, 
119c5 0a 2a 2a 20 69 66 20 74 68 65 20 70 61 67 65 72  .** if the pager
119c6 20 69 73 20 6e 6f 74 20 69 6e 20 6e 6f 2d 73 79   is not in no-sy
119c7 6e 63 20 6d 6f 64 65 2c 20 73 79 6e 63 20 74 68  nc mode, sync th
119c8 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
119c9 6d 6d 65 64 69 61 74 65 6c 79 20 0a 2a 2a 20 61  mmediately .** a
119ca 66 74 65 72 20 77 72 69 74 69 6e 67 20 6f 72 20  fter writing or 
119cb 74 72 75 6e 63 61 74 69 6e 67 20 69 74 2e 0a 2a  truncating it..*
119cc 2a 0a 2a 2a 20 49 66 20 50 61 67 65 72 2e 6a 6f  *.** If Pager.jo
119cd 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 69  urnalSizeLimit i
119ce 73 20 73 65 74 20 74 6f 20 61 20 70 6f 73 69 74  s set to a posit
119cf 69 76 65 2c 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61  ive, non-zero va
119d0 6c 75 65 2c 20 61 6e 64 0a 2a 2a 20 66 6f 6c 6c  lue, and.** foll
119d1 6f 77 69 6e 67 20 74 68 65 20 74 72 75 6e 63 61  owing the trunca
119d2 74 69 6f 6e 20 6f 72 20 7a 65 72 6f 69 6e 67 20  tion or zeroing 
119d3 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 20  described above 
119d4 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
119d5 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  .** journal file
119d6 20 69 6e 20 62 79 74 65 73 20 69 73 20 6c 61 72   in bytes is lar
119d7 67 65 72 20 74 68 61 6e 20 74 68 69 73 20 76 61  ger than this va
119d8 6c 75 65 2c 20 74 68 65 6e 20 74 72 75 6e 63 61  lue, then trunca
119d9 74 65 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61  te the.** journa
119da 6c 20 66 69 6c 65 20 74 6f 20 50 61 67 65 72 2e  l file to Pager.
119db 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74  journalSizeLimit
119dc 20 62 79 74 65 73 2e 20 54 68 65 20 6a 6f 75 72   bytes. The jour
119dd 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 0a 2a 2a  nal file does.**
119de 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20   not need to be 
119df 73 79 6e 63 65 64 20 66 6f 6c 6c 6f 77 69 6e 67  synced following
119e0 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e   this operation.
119e1 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20  .**.** If an IO 
119e2 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 62  error occurs, ab
119e3 61 6e 64 6f 6e 20 70 72 6f 63 65 73 73 69 6e 67  andon processing
119e4 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20   and return the 
119e5 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a  IO error code..*
119e6 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74  * Otherwise, ret
119e7 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a  urn SQLITE_OK..*
119e8 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 7a 65 72  /.static int zer
119e9 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 50 61 67 65  oJournalHdr(Page
119ea 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 64  r *pPager, int d
119eb 6f 54 72 75 6e 63 61 74 65 29 7b 0a 20 20 69 6e  oTruncate){.  in
119ec 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
119ed 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
119ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
119ef 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
119f0 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70  /.  assert( isOp
119f1 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
119f2 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  );.  if( pPager-
119f3 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 7b 0a 20  >journalOff ){. 
119f4 20 20 20 63 6f 6e 73 74 20 69 36 34 20 69 4c 69     const i64 iLi
119f5 6d 69 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f  mit = pPager->jo
119f6 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 20  urnalSizeLimit; 
119f7 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68     /* Local cach
119f8 65 20 6f 66 20 6a 73 6c 20 2a 2f 0a 0a 20 20 20  e of jsl */..   
119f9 20 49 4f 54 52 41 43 45 28 28 22 4a 5a 45 52 4f   IOTRACE(("JZERO
119fa 48 44 52 20 25 70 5c 6e 22 2c 20 70 50 61 67 65  HDR %p\n", pPage
119fb 72 29 29 0a 20 20 20 20 69 66 28 20 64 6f 54 72  r)).    if( doTr
119fc 75 6e 63 61 74 65 20 7c 7c 20 69 4c 69 6d 69 74  uncate || iLimit
119fd 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ==0 ){.      rc 
119fe 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63  = sqlite3OsTrunc
119ff 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ate(pPager->jfd,
11a00 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a   0);.    }else{.
11a01 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e        static con
11a02 73 74 20 63 68 61 72 20 7a 65 72 6f 48 64 72 5b  st char zeroHdr[
11a03 32 38 5d 20 3d 20 7b 30 7d 3b 0a 20 20 20 20 20  28] = {0};.     
11a04 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57   rc = sqlite3OsW
11a05 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  rite(pPager->jfd
11a06 2c 20 7a 65 72 6f 48 64 72 2c 20 73 69 7a 65 6f  , zeroHdr, sizeo
11a07 66 28 7a 65 72 6f 48 64 72 29 2c 20 30 29 3b 0a  f(zeroHdr), 0);.
11a08 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
11a09 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21  ==SQLITE_OK && !
11a0a 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29  pPager->noSync )
11a0b 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
11a0c 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65  ite3OsSync(pPage
11a0d 72 2d 3e 6a 66 64 2c 20 53 51 4c 49 54 45 5f 53  r->jfd, SQLITE_S
11a0e 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 7c 70 50 61  YNC_DATAONLY|pPa
11a0f 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29  ger->sync_flags)
11a10 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
11a11 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68  At this point th
11a12 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  e transaction is
11a13 20 63 6f 6d 6d 69 74 74 65 64 20 62 75 74 20 74   committed but t
11a14 68 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 0a 20  he write lock . 
11a15 20 20 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 68     ** is still h
11a16 65 6c 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e  eld on the file.
11a17 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 73   If there is a s
11a18 69 7a 65 20 6c 69 6d 69 74 20 63 6f 6e 66 69 67  ize limit config
11a19 75 72 65 64 20 66 6f 72 20 0a 20 20 20 20 2a 2a  ured for .    **
11a1a 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20   the persistent 
11a1b 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 20  journal and the 
11a1c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 75 72  journal file cur
11a1d 72 65 6e 74 6c 79 20 63 6f 6e 73 75 6d 65 73 20  rently consumes 
11a1e 6d 6f 72 65 0a 20 20 20 20 2a 2a 20 73 70 61 63  more.    ** spac
11a1f 65 20 74 68 61 6e 20 74 68 61 74 20 6c 69 6d 69  e than that limi
11a20 74 20 61 6c 6c 6f 77 73 20 66 6f 72 2c 20 74 72  t allows for, tr
11a21 75 6e 63 61 74 65 20 69 74 20 6e 6f 77 2e 20 54  uncate it now. T
11a22 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a  here is no need.
11a23 20 20 20 20 2a 2a 20 74 6f 20 73 79 6e 63 20 74      ** to sync t
11a24 68 65 20 66 69 6c 65 20 66 6f 6c 6c 6f 77 69 6e  he file followin
11a25 67 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e  g this operation
11a26 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
11a27 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
11a28 26 20 69 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 20  & iLimit>0 ){.  
11a29 20 20 20 20 69 36 34 20 73 7a 3b 0a 20 20 20 20      i64 sz;.    
11a2a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
11a2b 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d  FileSize(pPager-
11a2c 3e 6a 66 64 2c 20 26 73 7a 29 3b 0a 20 20 20 20  >jfd, &sz);.    
11a2d 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
11a2e 5f 4f 4b 20 26 26 20 73 7a 3e 69 4c 69 6d 69 74  _OK && sz>iLimit
11a2f 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
11a30 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61   sqlite3OsTrunca
11a31 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  te(pPager->jfd, 
11a32 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 7d  iLimit);.      }
11a33 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
11a34 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
11a35 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   The journal fil
11a36 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 77  e must be open w
11a37 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
11a38 20 69 73 20 63 61 6c 6c 65 64 2e 20 41 20 6a 6f   is called. A jo
11a39 75 72 6e 61 6c 0a 2a 2a 20 68 65 61 64 65 72 20  urnal.** header 
11a3a 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20  (JOURNAL_HDR_SZ 
11a3b 62 79 74 65 73 29 20 69 73 20 77 72 69 74 74 65  bytes) is writte
11a3c 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e  n into the journ
11a3d 61 6c 20 66 69 6c 65 20 61 74 20 74 68 65 0a 2a  al file at the.*
11a3e 2a 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69  * current locati
11a3f 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  on..**.** The fo
11a40 72 6d 61 74 20 66 6f 72 20 74 68 65 20 6a 6f 75  rmat for the jou
11a41 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20 61  rnal header is a
11a42 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 2d 20  s follows:.** - 
11a43 38 20 62 79 74 65 73 3a 20 4d 61 67 69 63 20 69  8 bytes: Magic i
11a44 64 65 6e 74 69 66 79 69 6e 67 20 6a 6f 75 72 6e  dentifying journ
11a45 61 6c 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20 2d 20  al format..** - 
11a46 34 20 62 79 74 65 73 3a 20 4e 75 6d 62 65 72 20  4 bytes: Number 
11a47 6f 66 20 72 65 63 6f 72 64 73 20 69 6e 20 6a 6f  of records in jo
11a48 75 72 6e 61 6c 2c 20 6f 72 20 2d 31 20 6e 6f 2d  urnal, or -1 no-
11a49 73 79 6e 63 20 6d 6f 64 65 20 69 73 20 6f 6e 2e  sync mode is on.
11a4a 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 52  .** - 4 bytes: R
11a4b 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 75 73 65  andom number use
11a4c 64 20 66 6f 72 20 70 61 67 65 20 68 61 73 68 2e  d for page hash.
11a4d 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 49  .** - 4 bytes: I
11a4e 6e 69 74 69 61 6c 20 64 61 74 61 62 61 73 65 20  nitial database 
11a4f 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a 2a 20 2d  page count..** -
11a50 20 34 20 62 79 74 65 73 3a 20 53 65 63 74 6f 72   4 bytes: Sector
11a51 20 73 69 7a 65 20 75 73 65 64 20 62 79 20 74 68   size used by th
11a52 65 20 70 72 6f 63 65 73 73 20 74 68 61 74 20 77  e process that w
11a53 72 6f 74 65 20 74 68 69 73 20 6a 6f 75 72 6e 61  rote this journa
11a54 6c 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a  l..** - 4 bytes:
11a55 20 44 61 74 61 62 61 73 65 20 70 61 67 65 20 73   Database page s
11a56 69 7a 65 2e 0a 2a 2a 20 0a 2a 2a 20 46 6f 6c 6c  ize..** .** Foll
11a57 6f 77 65 64 20 62 79 20 28 4a 4f 55 52 4e 41 4c  owed by (JOURNAL
11a58 5f 48 44 52 5f 53 5a 20 2d 20 32 38 29 20 62 79  _HDR_SZ - 28) by
11a59 74 65 73 20 6f 66 20 75 6e 75 73 65 64 20 73 70  tes of unused sp
11a5a 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ace..*/.static i
11a5b 6e 74 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48  nt writeJournalH
11a5c 64 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  dr(Pager *pPager
11a5d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
11a5e 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20  LITE_OK;        
11a5f 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
11a60 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 63 68 61  rn code */.  cha
11a61 72 20 2a 7a 48 65 61 64 65 72 20 3d 20 70 50 61  r *zHeader = pPa
11a62 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 20  ger->pTmpSpace; 
11a63 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 73 70   /* Temporary sp
11a64 61 63 65 20 75 73 65 64 20 74 6f 20 62 75 69 6c  ace used to buil
11a65 64 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33  d header */.  u3
11a66 32 20 6e 48 65 61 64 65 72 20 3d 20 70 50 61 67  2 nHeader = pPag
11a67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 20 20 20  er->pageSize;   
11a68 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75 66    /* Size of buf
11a69 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  fer pointed to b
11a6a 79 20 7a 48 65 61 64 65 72 20 2a 2f 0a 20 20 75  y zHeader */.  u
11a6b 33 32 20 6e 57 72 69 74 65 3b 20 20 20 20 20 20  32 nWrite;      
11a6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a6d 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 68     /* Bytes of h
11a6e 65 61 64 65 72 20 73 65 63 74 6f 72 20 77 72 69  eader sector wri
11a6f 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 69  tten */.  int ii
11a70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11a71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11a72 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
11a73 0a 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70  ..  assert( isOp
11a74 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
11a75 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f 75 72 6e  );      /* Journ
11a76 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20  al file must be 
11a77 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 69 66 28 20  open. */..  if( 
11a78 6e 48 65 61 64 65 72 3e 4a 4f 55 52 4e 41 4c 5f  nHeader>JOURNAL_
11a79 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29  HDR_SZ(pPager) )
11a7a 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 3d 20  {.    nHeader = 
11a7b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
11a7c 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f  Pager);.  }..  /
11a7d 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61  * If there are a
11a7e 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73  ctive savepoints
11a7f 20 61 6e 64 20 61 6e 79 20 6f 66 20 74 68 65 6d   and any of them
11a80 20 77 65 72 65 20 63 72 65 61 74 65 64 20 0a 20   were created . 
11a81 20 2a 2a 20 73 69 6e 63 65 20 74 68 65 20 6d 6f   ** since the mo
11a82 73 74 20 72 65 63 65 6e 74 20 6a 6f 75 72 6e 61  st recent journa
11a83 6c 20 68 65 61 64 65 72 20 77 61 73 20 77 72 69  l header was wri
11a84 74 74 65 6e 2c 20 75 70 64 61 74 65 20 74 68 65  tten, update the
11a85 20 0a 20 20 2a 2a 20 50 61 67 65 72 53 61 76 65   .  ** PagerSave
11a86 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74  point.iHdrOffset
11a87 20 66 69 65 6c 64 73 20 6e 6f 77 2e 0a 20 20 2a   fields now..  *
11a88 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  /.  for(ii=0; ii
11a89 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f  <pPager->nSavepo
11a8a 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  int; ii++){.    
11a8b 69 66 28 20 70 50 61 67 65 72 2d 3e 61 53 61 76  if( pPager->aSav
11a8c 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 48 64 72 4f  epoint[ii].iHdrO
11a8d 66 66 73 65 74 3d 3d 30 20 29 7b 0a 20 20 20 20  ffset==0 ){.    
11a8e 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70    pPager->aSavep
11a8f 6f 69 6e 74 5b 69 69 5d 2e 69 48 64 72 4f 66 66  oint[ii].iHdrOff
11a90 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f  set = pPager->jo
11a91 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20 20 7d 0a  urnalOff;.    }.
11a92 20 20 7d 0a 0a 20 20 70 50 61 67 65 72 2d 3e 6a    }..  pPager->j
11a93 6f 75 72 6e 61 6c 48 64 72 20 3d 20 70 50 61 67  ournalHdr = pPag
11a94 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d  er->journalOff =
11a95 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65   journalHdrOffse
11a96 74 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 2f 2a  t(pPager);..  /*
11a97 20 0a 20 20 2a 2a 20 57 72 69 74 65 20 74 68 65   .  ** Write the
11a98 20 6e 52 65 63 20 46 69 65 6c 64 20 2d 20 74 68   nRec Field - th
11a99 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
11a9a 20 72 65 63 6f 72 64 73 20 74 68 61 74 20 66 6f   records that fo
11a9b 6c 6c 6f 77 20 74 68 69 73 0a 20 20 2a 2a 20 6a  llow this.  ** j
11a9c 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 4e  ournal header. N
11a9d 6f 72 6d 61 6c 6c 79 2c 20 7a 65 72 6f 20 69 73  ormally, zero is
11a9e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 69 73   written to this
11a9f 20 76 61 6c 75 65 20 61 74 20 74 68 69 73 20 74   value at this t
11aa0 69 6d 65 2e 0a 20 20 2a 2a 20 41 66 74 65 72 20  ime..  ** After 
11aa1 74 68 65 20 72 65 63 6f 72 64 73 20 61 72 65 20  the records are 
11aa2 61 64 64 65 64 20 74 6f 20 74 68 65 20 6a 6f 75  added to the jou
11aa3 72 6e 61 6c 20 28 61 6e 64 20 74 68 65 20 6a 6f  rnal (and the jo
11aa4 75 72 6e 61 6c 20 73 79 6e 63 65 64 2c 20 0a 20  urnal synced, . 
11aa5 20 2a 2a 20 69 66 20 69 6e 20 66 75 6c 6c 2d 73   ** if in full-s
11aa6 79 6e 63 20 6d 6f 64 65 29 2c 20 74 68 65 20 7a  ync mode), the z
11aa7 65 72 6f 20 69 73 20 6f 76 65 72 77 72 69 74 74  ero is overwritt
11aa8 65 6e 20 77 69 74 68 20 74 68 65 20 74 72 75 65  en with the true
11aa9 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20   number.  ** of 
11aaa 72 65 63 6f 72 64 73 20 28 73 65 65 20 73 79 6e  records (see syn
11aab 63 4a 6f 75 72 6e 61 6c 28 29 29 2e 0a 20 20 2a  cJournal())..  *
11aac 2a 0a 20 20 2a 2a 20 41 20 66 61 73 74 65 72 20  *.  ** A faster 
11aad 61 6c 74 65 72 6e 61 74 69 76 65 20 69 73 20 74  alternative is t
11aae 6f 20 77 72 69 74 65 20 30 78 46 46 46 46 46 46  o write 0xFFFFFF
11aaf 46 46 20 74 6f 20 74 68 65 20 6e 52 65 63 20 66  FF to the nRec f
11ab0 69 65 6c 64 2e 20 57 68 65 6e 0a 20 20 2a 2a 20  ield. When.  ** 
11ab1 72 65 61 64 69 6e 67 20 74 68 65 20 6a 6f 75 72  reading the jour
11ab2 6e 61 6c 20 74 68 69 73 20 76 61 6c 75 65 20 74  nal this value t
11ab3 65 6c 6c 73 20 53 51 4c 69 74 65 20 74 6f 20 61  ells SQLite to a
11ab4 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 0a 20  ssume that the. 
11ab5 20 2a 2a 20 72 65 73 74 20 6f 66 20 74 68 65 20   ** rest of the 
11ab6 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e  journal file con
11ab7 74 61 69 6e 73 20 76 61 6c 69 64 20 70 61 67 65  tains valid page
11ab8 20 72 65 63 6f 72 64 73 2e 20 54 68 69 73 20 61   records. This a
11ab9 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2a 20 69  ssumption.  ** i
11aba 73 20 64 61 6e 67 65 72 6f 75 73 2c 20 61 73 20  s dangerous, as 
11abb 69 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63  if a failure occ
11abc 75 72 72 65 64 20 77 68 69 6c 73 74 20 77 72 69  urred whilst wri
11abd 74 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72  ting to the jour
11abe 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74  nal.  ** file it
11abf 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 73 6f 6d   may contain som
11ac0 65 20 67 61 72 62 61 67 65 20 64 61 74 61 2e 20  e garbage data. 
11ac1 54 68 65 72 65 20 61 72 65 20 74 77 6f 20 73 63  There are two sc
11ac2 65 6e 61 72 69 6f 73 0a 20 20 2a 2a 20 77 68 65  enarios.  ** whe
11ac3 72 65 20 74 68 69 73 20 72 69 73 6b 20 63 61 6e  re this risk can
11ac4 20 62 65 20 69 67 6e 6f 72 65 64 3a 0a 20 20 2a   be ignored:.  *
11ac5 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20  *.  **   * When 
11ac6 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20  the pager is in 
11ac7 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 43 6f  no-sync mode. Co
11ac8 72 72 75 70 74 69 6f 6e 20 63 61 6e 20 66 6f 6c  rruption can fol
11ac9 6c 6f 77 20 61 0a 20 20 2a 2a 20 20 20 20 20 70  low a.  **     p
11aca 6f 77 65 72 20 66 61 69 6c 75 72 65 20 69 6e 20  ower failure in 
11acb 74 68 69 73 20 63 61 73 65 20 61 6e 79 77 61 79  this case anyway
11acc 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20  ..  **.  **   * 
11acd 57 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f  When the SQLITE_
11ace 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e  IOCAP_SAFE_APPEN
11acf 44 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 54  D flag is set. T
11ad0 68 69 73 20 67 75 61 72 61 6e 74 65 65 73 0a 20  his guarantees. 
11ad1 20 2a 2a 20 20 20 20 20 74 68 61 74 20 67 61 72   **     that gar
11ad2 62 61 67 65 20 64 61 74 61 20 69 73 20 6e 65 76  bage data is nev
11ad3 65 72 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74  er appended to t
11ad4 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  he journal file.
11ad5 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
11ad6 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66  isOpen(pPager->f
11ad7 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6e 6f  d) || pPager->no
11ad8 53 79 6e 63 20 29 3b 0a 20 20 69 66 28 20 28 70  Sync );.  if( (p
11ad9 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c  Pager->noSync) |
11ada 7c 20 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  | (pPager->journ
11adb 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f  alMode==PAGER_JO
11adc 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59  URNALMODE_MEMORY
11add 29 0a 20 20 20 7c 7c 20 28 73 71 6c 69 74 65 33  ).   || (sqlite3
11ade 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65  OsDeviceCharacte
11adf 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e  ristics(pPager->
11ae0 66 64 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50  fd)&SQLITE_IOCAP
11ae1 5f 53 41 46 45 5f 41 50 50 45 4e 44 29 20 0a 20  _SAFE_APPEND) . 
11ae2 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a   ){.    memcpy(z
11ae3 48 65 61 64 65 72 2c 20 61 4a 6f 75 72 6e 61 6c  Header, aJournal
11ae4 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4a  Magic, sizeof(aJ
11ae5 6f 75 72 6e 61 6c 4d 61 67 69 63 29 29 3b 0a 20  ournalMagic));. 
11ae6 20 20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48     put32bits(&zH
11ae7 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f  eader[sizeof(aJo
11ae8 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 30 78  urnalMagic)], 0x
11ae9 66 66 66 66 66 66 66 66 29 3b 0a 20 20 7d 65 6c  ffffffff);.  }el
11aea 73 65 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 7a  se{.    memset(z
11aeb 48 65 61 64 65 72 2c 20 30 2c 20 73 69 7a 65 6f  Header, 0, sizeo
11aec 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29  f(aJournalMagic)
11aed 2b 34 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  +4);.  }..  /* T
11aee 68 65 20 72 61 6e 64 6f 6d 20 63 68 65 63 6b 2d  he random check-
11aef 68 61 73 68 20 69 6e 69 74 69 61 6c 69 73 65 72  hash initialiser
11af0 20 2a 2f 20 0a 20 20 73 71 6c 69 74 65 33 5f 72   */ .  sqlite3_r
11af1 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66  andomness(sizeof
11af2 28 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e  (pPager->cksumIn
11af3 69 74 29 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b  it), &pPager->ck
11af4 73 75 6d 49 6e 69 74 29 3b 0a 20 20 70 75 74 33  sumInit);.  put3
11af5 32 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73  2bits(&zHeader[s
11af6 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61  izeof(aJournalMa
11af7 67 69 63 29 2b 34 5d 2c 20 70 50 61 67 65 72 2d  gic)+4], pPager-
11af8 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 2f  >cksumInit);.  /
11af9 2a 20 54 68 65 20 69 6e 69 74 69 61 6c 20 64 61  * The initial da
11afa 74 61 62 61 73 65 20 73 69 7a 65 20 2a 2f 0a 20  tabase size */. 
11afb 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61   put32bits(&zHea
11afc 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72  der[sizeof(aJour
11afd 6e 61 6c 4d 61 67 69 63 29 2b 38 5d 2c 20 70 50  nalMagic)+8], pP
11afe 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65  ager->dbOrigSize
11aff 29 3b 0a 20 20 2f 2a 20 54 68 65 20 61 73 73 75  );.  /* The assu
11b00 6d 65 64 20 73 65 63 74 6f 72 20 73 69 7a 65 20  med sector size 
11b01 66 6f 72 20 74 68 69 73 20 70 72 6f 63 65 73 73  for this process
11b02 20 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28   */.  put32bits(
11b03 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28  &zHeader[sizeof(
11b04 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31  aJournalMagic)+1
11b05 32 5d 2c 20 70 50 61 67 65 72 2d 3e 73 65 63 74  2], pPager->sect
11b06 6f 72 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 54  orSize);..  /* T
11b07 68 65 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a  he page size */.
11b08 20 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65    put32bits(&zHe
11b09 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75  ader[sizeof(aJou
11b0a 72 6e 61 6c 4d 61 67 69 63 29 2b 31 36 5d 2c 20  rnalMagic)+16], 
11b0b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
11b0c 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c  );..  /* Initial
11b0d 69 7a 69 6e 67 20 74 68 65 20 74 61 69 6c 20 6f  izing the tail o
11b0e 66 20 74 68 65 20 62 75 66 66 65 72 20 69 73 20  f the buffer is 
11b0f 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 2e 20 20  not necessary.  
11b10 45 76 65 72 79 74 68 69 6e 67 0a 20 20 2a 2a 20  Everything.  ** 
11b11 77 6f 72 6b 73 20 66 69 6e 64 20 69 66 20 74 68  works find if th
11b12 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 65 6d 73  e following mems
11b13 65 74 28 29 20 69 73 20 6f 6d 69 74 74 65 64 2e  et() is omitted.
11b14 20 20 42 75 74 20 69 6e 69 74 69 61 6c 69 7a 69    But initializi
11b15 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6d 65 6d 6f  ng.  ** the memo
11b16 72 79 20 70 72 65 76 65 6e 74 73 20 76 61 6c 67  ry prevents valg
11b17 72 69 6e 64 20 66 72 6f 6d 20 63 6f 6d 70 6c 61  rind from compla
11b18 69 6e 69 6e 67 2c 20 73 6f 20 77 65 20 61 72 65  ining, so we are
11b19 20 77 69 6c 6c 69 6e 67 20 74 6f 0a 20 20 2a 2a   willing to.  **
11b1a 20 74 61 6b 65 20 74 68 65 20 70 65 72 66 6f 72   take the perfor
11b1b 6d 61 6e 63 65 20 68 69 74 2e 0a 20 20 2a 2f 0a  mance hit..  */.
11b1c 20 20 6d 65 6d 73 65 74 28 26 7a 48 65 61 64 65    memset(&zHeade
11b1d 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61  r[sizeof(aJourna
11b1e 6c 4d 61 67 69 63 29 2b 32 30 5d 2c 20 30 2c 0a  lMagic)+20], 0,.
11b1f 20 20 20 20 20 20 20 20 20 6e 48 65 61 64 65 72           nHeader
11b20 2d 28 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61  -(sizeof(aJourna
11b21 6c 4d 61 67 69 63 29 2b 32 30 29 29 3b 0a 0a 20  lMagic)+20));.. 
11b22 20 2f 2a 20 49 6e 20 74 68 65 6f 72 79 2c 20 69   /* In theory, i
11b23 74 20 69 73 20 6f 6e 6c 79 20 6e 65 63 65 73 73  t is only necess
11b24 61 72 79 20 74 6f 20 77 72 69 74 65 20 74 68 65  ary to write the
11b25 20 32 38 20 62 79 74 65 73 20 74 68 61 74 20 74   28 bytes that t
11b26 68 65 20 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c  he .  ** journal
11b27 20 68 65 61 64 65 72 20 63 6f 6e 73 75 6d 65 73   header consumes
11b28 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   to the journal 
11b29 66 69 6c 65 20 68 65 72 65 2e 20 54 68 65 6e 20  file here. Then 
11b2a 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 0a 20  increment the . 
11b2b 20 2a 2a 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61   ** Pager.journa
11b2c 6c 4f 66 66 20 76 61 72 69 61 62 6c 65 20 62 79  lOff variable by
11b2d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20   JOURNAL_HDR_SZ 
11b2e 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 78 74  so that the next
11b2f 20 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20 69 73   .  ** record is
11b30 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
11b31 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 63 74 6f 72  following sector
11b32 20 28 6c 65 61 76 69 6e 67 20 61 20 67 61 70 20   (leaving a gap 
11b33 69 6e 20 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a  in the file.  **
11b34 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 69 6d   that will be im
11b35 70 6c 69 63 69 74 6c 79 20 66 69 6c 6c 65 64 20  plicitly filled 
11b36 69 6e 20 62 79 20 74 68 65 20 4f 53 29 2e 0a 20  in by the OS).. 
11b37 20 2a 2a 0a 20 20 2a 2a 20 48 6f 77 65 76 65 72   **.  ** However
11b38 20 69 74 20 68 61 73 20 62 65 65 6e 20 64 69 73   it has been dis
11b39 63 6f 76 65 72 65 64 20 74 68 61 74 20 6f 6e 20  covered that on 
11b3a 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 74 68 69  some systems thi
11b3b 73 20 70 61 74 74 65 72 6e 20 63 61 6e 20 0a 20  s pattern can . 
11b3c 20 2a 2a 20 62 65 20 73 69 67 6e 69 66 69 63 61   ** be significa
11b3d 6e 74 6c 79 20 73 6c 6f 77 65 72 20 74 68 61 6e  ntly slower than
11b3e 20 63 6f 6e 74 69 67 75 6f 75 73 6c 79 20 77 72   contiguously wr
11b3f 69 74 69 6e 67 20 64 61 74 61 20 74 6f 20 74 68  iting data to th
11b40 65 20 66 69 6c 65 2c 0a 20 20 2a 2a 20 65 76 65  e file,.  ** eve
11b41 6e 20 69 66 20 74 68 61 74 20 6d 65 61 6e 73 20  n if that means 
11b42 65 78 70 6c 69 63 69 74 6c 79 20 77 72 69 74 69  explicitly writi
11b43 6e 67 20 64 61 74 61 20 74 6f 20 74 68 65 20 62  ng data to the b
11b44 6c 6f 63 6b 20 6f 66 20 0a 20 20 2a 2a 20 28 4a  lock of .  ** (J
11b45 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20  OURNAL_HDR_SZ - 
11b46 32 38 29 20 62 79 74 65 73 20 74 68 61 74 20 77  28) bytes that w
11b47 69 6c 6c 20 6e 6f 74 20 62 65 20 75 73 65 64 2e  ill not be used.
11b48 20 53 6f 20 74 68 61 74 20 69 73 20 77 68 61 74   So that is what
11b49 0a 20 20 2a 2a 20 69 73 20 64 6f 6e 65 2e 20 0a  .  ** is done. .
11b4a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 6c 6f    **.  ** The lo
11b4b 6f 70 20 69 73 20 72 65 71 75 69 72 65 64 20 68  op is required h
11b4c 65 72 65 20 69 6e 20 63 61 73 65 20 74 68 65 20  ere in case the 
11b4d 73 65 63 74 6f 72 2d 73 69 7a 65 20 69 73 20 6c  sector-size is l
11b4e 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 0a  arger than the .
11b4f 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61    ** database pa
11b50 67 65 20 73 69 7a 65 2e 20 53 69 6e 63 65 20 74  ge size. Since t
11b51 68 65 20 7a 48 65 61 64 65 72 20 62 75 66 66 65  he zHeader buffe
11b52 72 20 69 73 20 6f 6e 6c 79 20 50 61 67 65 72 2e  r is only Pager.
11b53 70 61 67 65 53 69 7a 65 0a 20 20 2a 2a 20 62 79  pageSize.  ** by
11b54 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 6d 6f 72  tes in size, mor
11b55 65 20 74 68 61 6e 20 6f 6e 65 20 63 61 6c 6c 20  e than one call 
11b56 74 6f 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74  to sqlite3OsWrit
11b57 65 28 29 20 6d 61 79 20 62 65 20 72 65 71 75 69  e() may be requi
11b58 72 65 64 0a 20 20 2a 2a 20 74 6f 20 70 6f 70 75  red.  ** to popu
11b59 6c 61 74 65 20 74 68 65 20 65 6e 74 69 72 65 20  late the entire 
11b5a 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73  journal header s
11b5b 65 63 74 6f 72 2e 0a 20 20 2a 2f 20 0a 20 20 66  ector..  */ .  f
11b5c 6f 72 28 6e 57 72 69 74 65 3d 30 3b 20 72 63 3d  or(nWrite=0; rc=
11b5d 3d 53 51 4c 49 54 45 5f 4f 4b 26 26 6e 57 72 69  =SQLITE_OK&&nWri
11b5e 74 65 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  te<JOURNAL_HDR_S
11b5f 5a 28 70 50 61 67 65 72 29 3b 20 6e 57 72 69 74  Z(pPager); nWrit
11b60 65 2b 3d 6e 48 65 61 64 65 72 29 7b 0a 20 20 20  e+=nHeader){.   
11b61 20 49 4f 54 52 41 43 45 28 28 22 4a 48 44 52 20   IOTRACE(("JHDR 
11b62 25 70 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70  %p %lld %d\n", p
11b63 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a  Pager, pPager->j
11b64 6f 75 72 6e 61 6c 48 64 72 2c 20 6e 48 65 61 64  ournalHdr, nHead
11b65 65 72 29 29 0a 20 20 20 20 72 63 20 3d 20 73 71  er)).    rc = sq
11b66 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61  lite3OsWrite(pPa
11b67 67 65 72 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65  ger->jfd, zHeade
11b68 72 2c 20 6e 48 65 61 64 65 72 2c 20 70 50 61 67  r, nHeader, pPag
11b69 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b  er->journalOff);
11b6a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75  .    pPager->jou
11b6b 72 6e 61 6c 4f 66 66 20 2b 3d 20 6e 48 65 61 64  rnalOff += nHead
11b6c 65 72 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  er;.  }..  retur
11b6d 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
11b6e 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11b6f 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65  must be open whe
11b70 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64  n this is called
11b71 2e 20 41 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  . A journal head
11b72 65 72 20 66 69 6c 65 0a 2a 2a 20 28 4a 4f 55 52  er file.** (JOUR
11b73 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 73  NAL_HDR_SZ bytes
11b74 29 20 69 73 20 72 65 61 64 20 66 72 6f 6d 20 74  ) is read from t
11b75 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74  he current locat
11b76 69 6f 6e 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  ion in the journ
11b77 61 6c 0a 2a 2a 20 66 69 6c 65 2e 20 54 68 65 20  al.** file. The 
11b78 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e  current location
11b79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   in the journal 
11b7a 66 69 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79  file is given by
11b7b 0a 2a 2a 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  .** pPager->jour
11b7c 6e 61 6c 4f 66 66 2e 20 53 65 65 20 63 6f 6d 6d  nalOff. See comm
11b7d 65 6e 74 73 20 61 62 6f 76 65 20 66 75 6e 63 74  ents above funct
11b7e 69 6f 6e 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c  ion writeJournal
11b7f 48 64 72 28 29 20 66 6f 72 0a 2a 2a 20 61 20 64  Hdr() for.** a d
11b80 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68  escription of th
11b81 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  e journal header
11b82 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49   format..**.** I
11b83 66 20 74 68 65 20 68 65 61 64 65 72 20 69 73 20  f the header is 
11b84 72 65 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c  read successfull
11b85 79 2c 20 2a 70 4e 52 65 63 20 69 73 20 73 65 74  y, *pNRec is set
11b86 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
11b87 66 0a 2a 2a 20 70 61 67 65 20 72 65 63 6f 72 64  f.** page record
11b88 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73  s following this
11b89 20 68 65 61 64 65 72 20 61 6e 64 20 2a 70 44 62   header and *pDb
11b8a 53 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 74  Size is set to t
11b8b 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a  he size of the.*
11b8c 2a 20 64 61 74 61 62 61 73 65 20 62 65 66 6f 72  * database befor
11b8d 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  e the transactio
11b8e 6e 20 62 65 67 61 6e 2c 20 69 6e 20 70 61 67 65  n began, in page
11b8f 73 2e 20 41 6c 73 6f 2c 20 70 50 61 67 65 72 2d  s. Also, pPager-
11b90 3e 63 6b 73 75 6d 49 6e 69 74 0a 2a 2a 20 69 73  >cksumInit.** is
11b91 20 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75   set to the valu
11b92 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  e read from the 
11b93 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20  journal header. 
11b94 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
11b95 75 72 6e 65 64 0a 2a 2a 20 69 6e 20 74 68 69 73  urned.** in this
11b96 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   case..**.** If 
11b97 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  the journal head
11b98 65 72 20 66 69 6c 65 20 61 70 70 65 61 72 73 20  er file appears 
11b99 74 6f 20 62 65 20 63 6f 72 72 75 70 74 65 64 2c  to be corrupted,
11b9a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 0a   SQLITE_DONE is.
11b9b 2a 2a 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  ** returned and 
11b9c 2a 70 4e 52 65 63 20 61 6e 64 20 2a 50 44 62 53  *pNRec and *PDbS
11b9d 69 7a 65 20 61 72 65 20 75 6e 64 65 66 69 6e 65  ize are undefine
11b9e 64 2e 20 20 49 66 20 4a 4f 55 52 4e 41 4c 5f 48  d.  If JOURNAL_H
11b9f 44 52 5f 53 5a 20 62 79 74 65 73 0a 2a 2a 20 63  DR_SZ bytes.** c
11ba0 61 6e 6e 6f 74 20 62 65 20 72 65 61 64 20 66 72  annot be read fr
11ba1 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  om the journal f
11ba2 69 6c 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  ile an error cod
11ba3 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  e is returned..*
11ba4 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61  /.static int rea
11ba5 64 4a 6f 75 72 6e 61 6c 48 64 72 28 0a 20 20 50  dJournalHdr(.  P
11ba6 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20  ager *pPager,   
11ba7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
11ba8 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20  ager object */. 
11ba9 20 69 6e 74 20 69 73 48 6f 74 2c 0a 20 20 69 36   int isHot,.  i6
11baa 34 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 2c 20 20  4 journalSize,  
11bab 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
11bac 7a 65 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 6a  ze of the open j
11bad 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 62  ournal file in b
11bae 79 74 65 73 20 2a 2f 0a 20 20 75 33 32 20 2a 70  ytes */.  u32 *p
11baf 4e 52 65 63 2c 20 20 20 20 20 20 20 20 20 20 20  NRec,           
11bb0 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 56         /* OUT: V
11bb1 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 74  alue read from t
11bb2 68 65 20 6e 52 65 63 20 66 69 65 6c 64 20 2a 2f  he nRec field */
11bb3 0a 20 20 75 33 32 20 2a 70 44 62 53 69 7a 65 20  .  u32 *pDbSize 
11bb4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11bb5 2f 2a 20 4f 55 54 3a 20 56 61 6c 75 65 20 6f 66  /* OUT: Value of
11bb6 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61   original databa
11bb7 73 65 20 73 69 7a 65 20 66 69 65 6c 64 20 2a 2f  se size field */
11bb8 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  .){.  int rc;   
11bb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11bba 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
11bbb 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  e */.  unsigned 
11bbc 63 68 61 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20  char aMagic[8]; 
11bbd 20 20 20 20 2f 2a 20 41 20 62 75 66 66 65 72 20      /* A buffer 
11bbe 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 67 69  to hold the magi
11bbf 63 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36  c header */.  i6
11bc0 34 20 69 48 64 72 4f 66 66 3b 20 20 20 20 20 20  4 iHdrOff;      
11bc1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66             /* Of
11bc2 66 73 65 74 20 6f 66 20 6a 6f 75 72 6e 61 6c 20  fset of journal 
11bc3 68 65 61 64 65 72 20 62 65 69 6e 67 20 72 65 61  header being rea
11bc4 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  d */..  assert( 
11bc5 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
11bc6 66 64 29 20 29 3b 20 20 20 20 20 20 2f 2a 20 4a  fd) );      /* J
11bc7 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74  ournal file must
11bc8 20 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20   be open. */..  
11bc9 2f 2a 20 41 64 76 61 6e 63 65 20 50 61 67 65 72  /* Advance Pager
11bca 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 74 6f 20 74  .journalOff to t
11bcb 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
11bcc 6e 65 78 74 20 73 65 63 74 6f 72 2e 20 49 66 20  next sector. If 
11bcd 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c  the.  ** journal
11bce 20 66 69 6c 65 20 69 73 20 74 6f 6f 20 73 6d 61   file is too sma
11bcf 6c 6c 20 66 6f 72 20 74 68 65 72 65 20 74 6f 20  ll for there to 
11bd0 62 65 20 61 20 68 65 61 64 65 72 20 73 74 6f 72  be a header stor
11bd1 65 64 20 61 74 20 74 68 69 73 0a 20 20 2a 2a 20  ed at this.  ** 
11bd2 70 6f 69 6e 74 2c 20 72 65 74 75 72 6e 20 53 51  point, return SQ
11bd3 4c 49 54 45 5f 44 4f 4e 45 2e 0a 20 20 2a 2f 0a  LITE_DONE..  */.
11bd4 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
11bd5 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64  lOff = journalHd
11bd6 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29 3b  rOffset(pPager);
11bd7 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a  .  if( pPager->j
11bd8 6f 75 72 6e 61 6c 4f 66 66 2b 4a 4f 55 52 4e 41  ournalOff+JOURNA
11bd9 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29  L_HDR_SZ(pPager)
11bda 20 3e 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 20 29   > journalSize )
11bdb 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
11bdc 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20  ITE_DONE;.  }.  
11bdd 69 48 64 72 4f 66 66 20 3d 20 70 50 61 67 65 72  iHdrOff = pPager
11bde 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20  ->journalOff;.. 
11bdf 20 2f 2a 20 52 65 61 64 20 69 6e 20 74 68 65 20   /* Read in the 
11be0 66 69 72 73 74 20 38 20 62 79 74 65 73 20 6f 66  first 8 bytes of
11be1 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61   the journal hea
11be2 64 65 72 2e 20 49 66 20 74 68 65 79 20 64 6f 20  der. If they do 
11be3 6e 6f 74 20 6d 61 74 63 68 0a 20 20 2a 2a 20 74  not match.  ** t
11be4 68 65 20 20 6d 61 67 69 63 20 73 74 72 69 6e 67  he  magic string
11be5 20 66 6f 75 6e 64 20 61 74 20 74 68 65 20 73 74   found at the st
11be6 61 72 74 20 6f 66 20 65 61 63 68 20 6a 6f 75 72  art of each jour
11be7 6e 61 6c 20 68 65 61 64 65 72 2c 20 72 65 74 75  nal header, retu
11be8 72 6e 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44  rn.  ** SQLITE_D
11be9 4f 4e 45 2e 20 49 66 20 61 6e 20 49 4f 20 65 72  ONE. If an IO er
11bea 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75  ror occurs, retu
11beb 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  rn an error code
11bec 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 20 20 2a  . Otherwise,.  *
11bed 2a 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a  * proceed..  */.
11bee 20 20 69 66 28 20 69 73 48 6f 74 20 7c 7c 20 69    if( isHot || i
11bef 48 64 72 4f 66 66 21 3d 70 50 61 67 65 72 2d 3e  HdrOff!=pPager->
11bf0 6a 6f 75 72 6e 61 6c 48 64 72 20 29 7b 0a 20 20  journalHdr ){.  
11bf1 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
11bf2 52 65 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64  Read(pPager->jfd
11bf3 2c 20 61 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66  , aMagic, sizeof
11bf4 28 61 4d 61 67 69 63 29 2c 20 69 48 64 72 4f 66  (aMagic), iHdrOf
11bf5 66 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  f);.    if( rc )
11bf6 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
11bf7 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  c;.    }.    if(
11bf8 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20   memcmp(aMagic, 
11bf9 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73  aJournalMagic, s
11bfa 69 7a 65 6f 66 28 61 4d 61 67 69 63 29 29 21 3d  izeof(aMagic))!=
11bfb 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
11bfc 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  n SQLITE_DONE;. 
11bfd 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52     }.  }..  /* R
11bfe 65 61 64 20 74 68 65 20 66 69 72 73 74 20 74 68  ead the first th
11bff 72 65 65 20 33 32 2d 62 69 74 20 66 69 65 6c 64  ree 32-bit field
11c00 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  s of the journal
11c01 20 68 65 61 64 65 72 3a 20 54 68 65 20 6e 52 65   header: The nRe
11c02 63 0a 20 20 2a 2a 20 66 69 65 6c 64 2c 20 74 68  c.  ** field, th
11c03 65 20 63 68 65 63 6b 73 75 6d 2d 69 6e 69 74 69  e checksum-initi
11c04 61 6c 69 7a 65 72 20 61 6e 64 20 74 68 65 20 64  alizer and the d
11c05 61 74 61 62 61 73 65 20 73 69 7a 65 20 61 74 20  atabase size at 
11c06 74 68 65 20 73 74 61 72 74 0a 20 20 2a 2a 20 6f  the start.  ** o
11c07 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  f the transactio
11c08 6e 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72  n. Return an err
11c09 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68  or code if anyth
11c0a 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a  ing goes wrong..
11c0b 20 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54    */.  if( SQLIT
11c0c 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64  E_OK!=(rc = read
11c0d 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a  32bits(pPager->j
11c0e 66 64 2c 20 69 48 64 72 4f 66 66 2b 38 2c 20 70  fd, iHdrOff+8, p
11c0f 4e 52 65 63 29 29 0a 20 20 20 7c 7c 20 53 51 4c  NRec)).   || SQL
11c10 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65  ITE_OK!=(rc = re
11c11 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d  ad32bits(pPager-
11c12 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 31 32  >jfd, iHdrOff+12
11c13 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d  , &pPager->cksum
11c14 49 6e 69 74 29 29 0a 20 20 20 7c 7c 20 53 51 4c  Init)).   || SQL
11c15 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65  ITE_OK!=(rc = re
11c16 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d  ad32bits(pPager-
11c17 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 31 36  >jfd, iHdrOff+16
11c18 2c 20 70 44 62 53 69 7a 65 29 29 0a 20 20 29 7b  , pDbSize)).  ){
11c19 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
11c1a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65    }..  if( pPage
11c1b 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30  r->journalOff==0
11c1c 20 29 7b 0a 20 20 20 20 75 33 32 20 69 50 61 67   ){.    u32 iPag
11c1d 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  eSize;          
11c1e 20 20 20 20 20 2f 2a 20 50 61 67 65 2d 73 69 7a       /* Page-siz
11c1f 65 20 66 69 65 6c 64 20 6f 66 20 6a 6f 75 72 6e  e field of journ
11c20 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20  al header */.   
11c21 20 75 33 32 20 69 53 65 63 74 6f 72 53 69 7a 65   u32 iSectorSize
11c22 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
11c23 20 53 65 63 74 6f 72 2d 73 69 7a 65 20 66 69 65   Sector-size fie
11c24 6c 64 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65  ld of journal he
11c25 61 64 65 72 20 2a 2f 0a 20 20 20 20 75 31 36 20  ader */.    u16 
11c26 69 50 61 67 65 53 69 7a 65 31 36 3b 20 20 20 20  iPageSize16;    
11c27 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79           /* Copy
11c28 20 6f 66 20 69 50 61 67 65 53 69 7a 65 20 69 6e   of iPageSize in
11c29 20 31 36 2d 62 69 74 20 76 61 72 69 61 62 6c 65   16-bit variable
11c2a 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64   */..    /* Read
11c2b 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61   the page-size a
11c2c 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 6a  nd sector-size j
11c2d 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 69  ournal header fi
11c2e 65 6c 64 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28  elds. */.    if(
11c2f 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20   SQLITE_OK!=(rc 
11c30 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61  = read32bits(pPa
11c31 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66  ger->jfd, iHdrOf
11c32 66 2b 32 30 2c 20 26 69 53 65 63 74 6f 72 53 69  f+20, &iSectorSi
11c33 7a 65 29 29 0a 20 20 20 20 20 7c 7c 20 53 51 4c  ze)).     || SQL
11c34 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65  ITE_OK!=(rc = re
11c35 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d  ad32bits(pPager-
11c36 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 32 34  >jfd, iHdrOff+24
11c37 2c 20 26 69 50 61 67 65 53 69 7a 65 29 29 0a 20  , &iPageSize)). 
11c38 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75     ){.      retu
11c39 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20  rn rc;.    }..  
11c3a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20    /* Check that 
11c3b 74 68 65 20 76 61 6c 75 65 73 20 72 65 61 64 20  the values read 
11c3c 66 72 6f 6d 20 74 68 65 20 70 61 67 65 2d 73 69  from the page-si
11c3d 7a 65 20 61 6e 64 20 73 65 63 74 6f 72 2d 73 69  ze and sector-si
11c3e 7a 65 20 66 69 65 6c 64 73 0a 20 20 20 20 2a 2a  ze fields.    **
11c3f 20 61 72 65 20 77 69 74 68 69 6e 20 72 61 6e 67   are within rang
11c40 65 2e 20 54 6f 20 62 65 20 27 69 6e 20 72 61 6e  e. To be 'in ran
11c41 67 65 27 2c 20 62 6f 74 68 20 76 61 6c 75 65 73  ge', both values
11c42 20 6e 65 65 64 20 74 6f 20 62 65 20 61 20 70 6f   need to be a po
11c43 77 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20 74 77  wer.    ** of tw
11c44 6f 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  o greater than o
11c45 72 20 65 71 75 61 6c 20 74 6f 20 35 31 32 2c 20  r equal to 512, 
11c46 61 6e 64 20 6e 6f 74 20 67 72 65 61 74 65 72 20  and not greater 
11c47 74 68 61 6e 20 74 68 65 69 72 20 0a 20 20 20 20  than their .    
11c48 2a 2a 20 72 65 73 70 65 63 74 69 76 65 20 63 6f  ** respective co
11c49 6d 70 69 6c 65 20 74 69 6d 65 20 6d 61 78 69 6d  mpile time maxim
11c4a 75 6d 20 6c 69 6d 69 74 73 2e 0a 20 20 20 20 2a  um limits..    *
11c4b 2f 0a 20 20 20 20 69 66 28 20 69 50 61 67 65 53  /.    if( iPageS
11c4c 69 7a 65 3c 35 31 32 20 20 20 20 20 20 20 20 20  ize<512         
11c4d 20 20 20 20 20 20 20 20 20 7c 7c 20 69 53 65 63           || iSec
11c4e 74 6f 72 53 69 7a 65 3c 35 31 32 0a 20 20 20 20  torSize<512.    
11c4f 20 7c 7c 20 69 50 61 67 65 53 69 7a 65 3e 53 51   || iPageSize>SQ
11c50 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49  LITE_MAX_PAGE_SI
11c51 5a 45 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a  ZE || iSectorSiz
11c52 65 3e 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a  e>MAX_SECTOR_SIZ
11c53 45 0a 20 20 20 20 20 7c 7c 20 28 28 69 50 61 67  E.     || ((iPag
11c54 65 53 69 7a 65 2d 31 29 26 69 50 61 67 65 53 69  eSize-1)&iPageSi
11c55 7a 65 29 21 3d 30 20 20 20 7c 7c 20 28 28 69 53  ze)!=0   || ((iS
11c56 65 63 74 6f 72 53 69 7a 65 2d 31 29 26 69 53 65  ectorSize-1)&iSe
11c57 63 74 6f 72 53 69 7a 65 29 21 3d 30 20 0a 20 20  ctorSize)!=0 .  
11c58 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66    ){.      /* If
11c59 20 74 68 65 20 65 69 74 68 65 72 20 74 68 65 20   the either the 
11c5a 70 61 67 65 2d 73 69 7a 65 20 6f 72 20 73 65 63  page-size or sec
11c5b 74 6f 72 2d 73 69 7a 65 20 69 6e 20 74 68 65 20  tor-size in the 
11c5c 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 69  journal-header i
11c5d 73 20 0a 20 20 20 20 20 20 2a 2a 20 69 6e 76 61  s .      ** inva
11c5e 6c 69 64 2c 20 74 68 65 6e 20 74 68 65 20 70 72  lid, then the pr
11c5f 6f 63 65 73 73 20 74 68 61 74 20 77 72 6f 74 65  ocess that wrote
11c60 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61   the journal-hea
11c61 64 65 72 20 6d 75 73 74 20 68 61 76 65 20 0a 20  der must have . 
11c62 20 20 20 20 20 2a 2a 20 63 72 61 73 68 65 64 20       ** crashed 
11c63 62 65 66 6f 72 65 20 74 68 65 20 68 65 61 64 65  before the heade
11c64 72 20 77 61 73 20 73 79 6e 63 65 64 2e 20 49 6e  r was synced. In
11c65 20 74 68 69 73 20 63 61 73 65 20 73 74 6f 70 20   this case stop 
11c66 72 65 61 64 69 6e 67 20 0a 20 20 20 20 20 20 2a  reading .      *
11c67 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  * the journal fi
11c68 6c 65 20 68 65 72 65 2e 0a 20 20 20 20 20 20 2a  le here..      *
11c69 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  /.      return S
11c6a 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20  QLITE_DONE;.    
11c6b 7d 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65  }..    /* Update
11c6c 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 74   the page-size t
11c6d 6f 20 6d 61 74 63 68 20 74 68 65 20 76 61 6c 75  o match the valu
11c6e 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  e read from the 
11c6f 6a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20 2a 2a  journal. .    **
11c70 20 55 73 65 20 61 20 74 65 73 74 63 61 73 65 28   Use a testcase(
11c71 29 20 6d 61 63 72 6f 20 74 6f 20 6d 61 6b 65 20  ) macro to make 
11c72 73 75 72 65 20 74 68 61 74 20 6d 61 6c 6c 6f 63  sure that malloc
11c73 20 66 61 69 6c 75 72 65 20 77 69 74 68 69 6e 20   failure within 
11c74 0a 20 20 20 20 2a 2a 20 50 61 67 65 72 53 65 74  .    ** PagerSet
11c75 50 61 67 65 73 69 7a 65 28 29 20 69 73 20 74 65  Pagesize() is te
11c76 73 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  sted..    */.   
11c77 20 69 50 61 67 65 53 69 7a 65 31 36 20 3d 20 28   iPageSize16 = (
11c78 75 31 36 29 69 50 61 67 65 53 69 7a 65 3b 0a 20  u16)iPageSize;. 
11c79 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
11c7a 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28  agerSetPagesize(
11c7b 70 50 61 67 65 72 2c 20 26 69 50 61 67 65 53 69  pPager, &iPageSi
11c7c 7a 65 31 36 2c 20 2d 31 29 3b 0a 20 20 20 20 74  ze16, -1);.    t
11c7d 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c  estcase( rc!=SQL
11c7e 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 61 73  ITE_OK );.    as
11c7f 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
11c80 5f 4f 4b 20 7c 7c 20 69 50 61 67 65 53 69 7a 65  _OK || iPageSize
11c81 31 36 3d 3d 28 75 31 36 29 69 50 61 67 65 53 69  16==(u16)iPageSi
11c82 7a 65 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 55 70  ze );..    /* Up
11c83 64 61 74 65 20 74 68 65 20 61 73 73 75 6d 65 64  date the assumed
11c84 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 74 6f 20   sector-size to 
11c85 6d 61 74 63 68 20 74 68 65 20 76 61 6c 75 65 20  match the value 
11c86 75 73 65 64 20 62 79 20 0a 20 20 20 20 2a 2a 20  used by .    ** 
11c87 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 61 74  the process that
11c88 20 63 72 65 61 74 65 64 20 74 68 69 73 20 6a 6f   created this jo
11c89 75 72 6e 61 6c 2e 20 49 66 20 74 68 69 73 20 6a  urnal. If this j
11c8a 6f 75 72 6e 61 6c 20 77 61 73 0a 20 20 20 20 2a  ournal was.    *
11c8b 2a 20 63 72 65 61 74 65 64 20 62 79 20 61 20 70  * created by a p
11c8c 72 6f 63 65 73 73 20 6f 74 68 65 72 20 74 68 61  rocess other tha
11c8d 6e 20 74 68 69 73 20 6f 6e 65 2c 20 74 68 65 6e  n this one, then
11c8e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 20 20   this routine.  
11c8f 20 20 2a 2a 20 69 73 20 62 65 69 6e 67 20 63 61    ** is being ca
11c90 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  lled from within
11c91 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28   pager_playback(
11c92 29 2e 20 54 68 65 20 6c 6f 63 61 6c 20 76 61 6c  ). The local val
11c93 75 65 0a 20 20 20 20 2a 2a 20 6f 66 20 50 61 67  ue.    ** of Pag
11c94 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 69 73  er.sectorSize is
11c95 20 72 65 73 74 6f 72 65 64 20 61 74 20 74 68 65   restored at the
11c96 20 65 6e 64 20 6f 66 20 74 68 61 74 20 72 6f 75   end of that rou
11c97 74 69 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tine..    */.   
11c98 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53   pPager->sectorS
11c99 69 7a 65 20 3d 20 69 53 65 63 74 6f 72 53 69 7a  ize = iSectorSiz
11c9a 65 3b 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 72  e;.  }..  pPager
11c9b 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20  ->journalOff += 
11c9c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
11c9d 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e  Pager);.  return
11c9e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57   rc;.}.../*.** W
11c9f 72 69 74 65 20 74 68 65 20 73 75 70 70 6c 69 65  rite the supplie
11ca0 64 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  d master journal
11ca1 20 6e 61 6d 65 20 69 6e 74 6f 20 74 68 65 20 6a   name into the j
11ca2 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20  ournal file for 
11ca3 70 61 67 65 72 0a 2a 2a 20 70 50 61 67 65 72 20  pager.** pPager 
11ca4 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c  at the current l
11ca5 6f 63 61 74 69 6f 6e 2e 20 54 68 65 20 6d 61 73  ocation. The mas
11ca6 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  ter journal name
11ca7 20 6d 75 73 74 20 62 65 20 74 68 65 20 6c 61 73   must be the las
11ca8 74 0a 2a 2a 20 74 68 69 6e 67 20 77 72 69 74 74  t.** thing writt
11ca9 65 6e 20 74 6f 20 61 20 6a 6f 75 72 6e 61 6c 20  en to a journal 
11caa 66 69 6c 65 2e 20 49 66 20 74 68 65 20 70 61 67  file. If the pag
11cab 65 72 20 69 73 20 69 6e 20 66 75 6c 6c 2d 73 79  er is in full-sy
11cac 6e 63 20 6d 6f 64 65 2c 20 74 68 65 0a 2a 2a 20  nc mode, the.** 
11cad 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73  journal file des
11cae 63 72 69 70 74 6f 72 20 69 73 20 61 64 76 61 6e  criptor is advan
11caf 63 65 64 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ced to the next 
11cb0 73 65 63 74 6f 72 20 62 6f 75 6e 64 61 72 79 20  sector boundary 
11cb1 62 65 66 6f 72 65 0a 2a 2a 20 61 6e 79 74 68 69  before.** anythi
11cb2 6e 67 20 69 73 20 77 72 69 74 74 65 6e 2e 20 54  ng is written. T
11cb3 68 65 20 66 6f 72 6d 61 74 20 69 73 3a 0a 2a 2a  he format is:.**
11cb4 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74 65 73 3a  .**   + 4 bytes:
11cb5 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 2e 0a   PAGER_MJ_PGNO..
11cb6 2a 2a 20 20 20 2b 20 4e 20 62 79 74 65 73 3a 20  **   + N bytes: 
11cb7 4d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  Master journal f
11cb8 69 6c 65 6e 61 6d 65 20 69 6e 20 75 74 66 2d 38  ilename in utf-8
11cb9 2e 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74 65 73  ..**   + 4 bytes
11cba 3a 20 4e 20 28 6c 65 6e 67 74 68 20 6f 66 20 6d  : N (length of m
11cbb 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61  aster journal na
11cbc 6d 65 20 69 6e 20 62 79 74 65 73 2c 20 6e 6f 20  me in bytes, no 
11cbd 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 29 2e  nul-terminator).
11cbe 0a 2a 2a 20 20 20 2b 20 34 20 62 79 74 65 73 3a  .**   + 4 bytes:
11cbf 20 4d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   Master journal 
11cc0 6e 61 6d 65 20 63 68 65 63 6b 73 75 6d 2e 0a 2a  name checksum..*
11cc1 2a 20 20 20 2b 20 38 20 62 79 74 65 73 3a 20 61  *   + 8 bytes: a
11cc2 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a  JournalMagic[]..
11cc3 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 73 74 65 72  **.** The master
11cc4 20 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 63 68   journal page ch
11cc5 65 63 6b 73 75 6d 20 69 73 20 74 68 65 20 73 75  ecksum is the su
11cc6 6d 20 6f 66 20 74 68 65 20 62 79 74 65 73 20 69  m of the bytes i
11cc7 6e 20 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20  n the master.** 
11cc8 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 2c 20 77 68  journal name, wh
11cc9 65 72 65 20 65 61 63 68 20 62 79 74 65 20 69 73  ere each byte is
11cca 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20   interpreted as 
11ccb 61 20 73 69 67 6e 65 64 20 38 2d 62 69 74 20 69  a signed 8-bit i
11ccc 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66  nteger..**.** If
11ccd 20 7a 4d 61 73 74 65 72 20 69 73 20 61 20 4e 55   zMaster is a NU
11cce 4c 4c 20 70 6f 69 6e 74 65 72 20 28 6f 63 63 75  LL pointer (occu
11ccf 72 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20  rs for a single 
11cd0 64 61 74 61 62 61 73 65 20 74 72 61 6e 73 61 63  database transac
11cd1 74 69 6f 6e 29 2c 20 0a 2a 2a 20 74 68 69 73 20  tion), .** this 
11cd2 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  call is a no-op.
11cd3 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
11cd4 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61  riteMasterJourna
11cd5 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  l(Pager *pPager,
11cd6 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61   const char *zMa
11cd7 73 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b  ster){.  int rc;
11cd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11cd9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
11cda 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e  urn code */.  in
11cdb 74 20 6e 4d 61 73 74 65 72 3b 20 20 20 20 20 20  t nMaster;      
11cdc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11cdd 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69  * Length of stri
11cde 6e 67 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 20 20  ng zMaster */.  
11cdf 69 36 34 20 69 48 64 72 4f 66 66 3b 20 20 20 20  i64 iHdrOff;    
11ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11ce1 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 65   /* Offset of he
11ce2 61 64 65 72 20 69 6e 20 6a 6f 75 72 6e 61 6c 20  ader in journal 
11ce3 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6a 72  file */.  i64 jr
11ce4 6e 6c 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20  nlSize;         
11ce5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
11ce6 7a 65 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 66 69  ze of journal fi
11ce7 6c 65 20 6f 6e 20 64 69 73 6b 20 2a 2f 0a 20 20  le on disk */.  
11ce8 75 33 32 20 63 6b 73 75 6d 20 3d 20 30 3b 20 20  u32 cksum = 0;  
11ce9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11cea 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20 6f 66 20   /* Checksum of 
11ceb 73 74 72 69 6e 67 20 7a 4d 61 73 74 65 72 20 2a  string zMaster *
11cec 2f 0a 0a 20 20 69 66 28 20 21 7a 4d 61 73 74 65  /..  if( !zMaste
11ced 72 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 65 74  r || pPager->set
11cee 4d 61 73 74 65 72 0a 20 20 20 7c 7c 20 70 50 61  Master.   || pPa
11cef 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
11cf0 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
11cf1 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 7c  ODE_MEMORY .   |
11cf2 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  | pPager->journa
11cf3 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  lMode==PAGER_JOU
11cf4 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20  RNALMODE_OFF .  
11cf5 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
11cf6 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70  LITE_OK;.  }.  p
11cf7 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72  Pager->setMaster
11cf8 20 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20   = 1;.  assert( 
11cf9 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
11cfa 66 64 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 61 6c  fd) );..  /* Cal
11cfb 63 75 6c 61 74 65 20 74 68 65 20 6c 65 6e 67 74  culate the lengt
11cfc 68 20 69 6e 20 62 79 74 65 73 20 61 6e 64 20 74  h in bytes and t
11cfd 68 65 20 63 68 65 63 6b 73 75 6d 20 6f 66 20 7a  he checksum of z
11cfe 4d 61 73 74 65 72 20 2a 2f 0a 20 20 66 6f 72 28  Master */.  for(
11cff 6e 4d 61 73 74 65 72 3d 30 3b 20 7a 4d 61 73 74  nMaster=0; zMast
11d00 65 72 5b 6e 4d 61 73 74 65 72 5d 3b 20 6e 4d 61  er[nMaster]; nMa
11d01 73 74 65 72 2b 2b 29 7b 0a 20 20 20 20 63 6b 73  ster++){.    cks
11d02 75 6d 20 2b 3d 20 7a 4d 61 73 74 65 72 5b 6e 4d  um += zMaster[nM
11d03 61 73 74 65 72 5d 3b 0a 20 20 7d 0a 0a 20 20 2f  aster];.  }..  /
11d04 2a 20 49 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e  * If in full-syn
11d05 63 20 6d 6f 64 65 2c 20 61 64 76 61 6e 63 65 20  c mode, advance 
11d06 74 6f 20 74 68 65 20 6e 65 78 74 20 64 69 73 6b  to the next disk
11d07 20 73 65 63 74 6f 72 20 62 65 66 6f 72 65 20 77   sector before w
11d08 72 69 74 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20  riting.  ** the 
11d09 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e  master journal n
11d0a 61 6d 65 2e 20 54 68 69 73 20 69 73 20 69 6e 20  ame. This is in 
11d0b 63 61 73 65 20 74 68 65 20 70 72 65 76 69 6f 75  case the previou
11d0c 73 20 70 61 67 65 20 77 72 69 74 74 65 6e 20 74  s page written t
11d0d 6f 0a 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e  o.  ** the journ
11d0e 61 6c 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  al has already b
11d0f 65 65 6e 20 73 79 6e 63 65 64 2e 0a 20 20 2a 2f  een synced..  */
11d10 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 66  .  if( pPager->f
11d11 75 6c 6c 53 79 6e 63 20 29 7b 0a 20 20 20 20 70  ullSync ){.    p
11d12 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
11d13 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66  f = journalHdrOf
11d14 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20  fset(pPager);.  
11d15 7d 0a 20 20 69 48 64 72 4f 66 66 20 3d 20 70 50  }.  iHdrOff = pP
11d16 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
11d17 3b 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68  ;..  /* Write th
11d18 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
11d19 20 64 61 74 61 20 74 6f 20 74 68 65 20 65 6e 64   data to the end
11d1a 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
11d1b 66 69 6c 65 2e 20 49 66 0a 20 20 2a 2a 20 61 6e  file. If.  ** an
11d1c 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72   error occurs, r
11d1d 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20  eturn the error 
11d1e 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c  code to the call
11d1f 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  er..  */.  if( (
11d20 30 20 21 3d 20 28 72 63 20 3d 20 77 72 69 74 65  0 != (rc = write
11d21 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a  32bits(pPager->j
11d22 66 64 2c 20 69 48 64 72 4f 66 66 2c 20 50 41 47  fd, iHdrOff, PAG
11d23 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65  ER_MJ_PGNO(pPage
11d24 72 29 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21  r)))).   || (0 !
11d25 3d 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f  = (rc = sqlite3O
11d26 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a  sWrite(pPager->j
11d27 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 6e 4d 61  fd, zMaster, nMa
11d28 73 74 65 72 2c 20 69 48 64 72 4f 66 66 2b 34 29  ster, iHdrOff+4)
11d29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28  )).   || (0 != (
11d2a 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73  rc = write32bits
11d2b 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48  (pPager->jfd, iH
11d2c 64 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2c  drOff+4+nMaster,
11d2d 20 6e 4d 61 73 74 65 72 29 29 29 0a 20 20 20 7c   nMaster))).   |
11d2e 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 77 72  | (0 != (rc = wr
11d2f 69 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72  ite32bits(pPager
11d30 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 34  ->jfd, iHdrOff+4
11d31 2b 6e 4d 61 73 74 65 72 2b 34 2c 20 63 6b 73 75  +nMaster+4, cksu
11d32 6d 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d  m))).   || (0 !=
11d33 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73   (rc = sqlite3Os
11d34 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66  Write(pPager->jf
11d35 64 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  d, aJournalMagic
11d36 2c 20 38 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e  , 8, iHdrOff+4+n
11d37 4d 61 73 74 65 72 2b 38 29 29 29 0a 20 20 29 7b  Master+8))).  ){
11d38 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
11d39 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f    }.  pPager->jo
11d3a 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 28 6e 4d 61  urnalOff += (nMa
11d3b 73 74 65 72 2b 32 30 29 3b 0a 20 20 70 50 61 67  ster+20);.  pPag
11d3c 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 21  er->needSync = !
11d3d 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a  pPager->noSync;.
11d3e 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67  .  /* If the pag
11d3f 65 72 20 69 73 20 69 6e 20 70 65 72 69 73 74 65  er is in periste
11d40 6e 74 2d 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 2c  nt-journal mode,
11d41 20 74 68 65 6e 20 74 68 65 20 70 68 79 73 69 63   then the physic
11d42 61 6c 20 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c  al .  ** journal
11d43 2d 66 69 6c 65 20 6d 61 79 20 65 78 74 65 6e 64  -file may extend
11d44 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66   past the end of
11d45 20 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72   the master-jour
11d46 6e 61 6c 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 6e  nal name.  ** an
11d47 64 20 38 20 62 79 74 65 73 20 6f 66 20 6d 61 67  d 8 bytes of mag
11d48 69 63 20 64 61 74 61 20 6a 75 73 74 20 77 72 69  ic data just wri
11d49 74 74 65 6e 20 74 6f 20 74 68 65 20 66 69 6c 65  tten to the file
11d4a 2e 20 54 68 69 73 20 69 73 20 0a 20 20 2a 2a 20  . This is .  ** 
11d4b 64 61 6e 67 65 72 6f 75 73 20 62 65 63 61 75 73  dangerous becaus
11d4c 65 20 74 68 65 20 63 6f 64 65 20 74 6f 20 72 6f  e the code to ro
11d4d 6c 6c 62 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75  llback a hot-jou
11d4e 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20 77  rnal file.  ** w
11d4f 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20  ill not be able 
11d50 74 6f 20 66 69 6e 64 20 74 68 65 20 6d 61 73 74  to find the mast
11d51 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20  er-journal name 
11d52 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 0a 20 20  to determine .  
11d53 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ** whether or no
11d54 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73  t the journal is
11d55 20 68 6f 74 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a   hot. .  **.  **
11d56 20 45 61 73 69 65 73 74 20 74 68 69 6e 67 20 74   Easiest thing t
11d57 6f 20 64 6f 20 69 6e 20 74 68 69 73 20 73 63 65  o do in this sce
11d58 6e 61 72 69 6f 20 69 73 20 74 6f 20 74 72 75 6e  nario is to trun
11d59 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  cate the journal
11d5a 20 0a 20 20 2a 2a 20 66 69 6c 65 20 74 6f 20 74   .  ** file to t
11d5b 68 65 20 72 65 71 75 69 72 65 64 20 73 69 7a 65  he required size
11d5c 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28 20 53 51  ..  */ .  if( SQ
11d5d 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73  LITE_OK==(rc = s
11d5e 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65  qlite3OsFileSize
11d5f 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 6a  (pPager->jfd, &j
11d60 72 6e 6c 53 69 7a 65 29 29 0a 20 20 20 26 26 20  rnlSize)).   && 
11d61 6a 72 6e 6c 53 69 7a 65 3e 70 50 61 67 65 72 2d  jrnlSize>pPager-
11d62 3e 6a 6f 75 72 6e 61 6c 4f 66 66 0a 20 20 29 7b  >journalOff.  ){
11d63 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
11d64 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67  3OsTruncate(pPag
11d65 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d  er->jfd, pPager-
11d66 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 20  >journalOff);.  
11d67 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
11d68 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61 20 70  ../*.** Find a p
11d69 61 67 65 20 69 6e 20 74 68 65 20 68 61 73 68 20  age in the hash 
11d6a 74 61 62 6c 65 20 67 69 76 65 6e 20 69 74 73 20  table given its 
11d6b 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 52 65 74  page number. Ret
11d6c 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72  urn.** a pointer
11d6d 20 74 6f 20 74 68 65 20 70 61 67 65 20 6f 72 20   to the page or 
11d6e 4e 55 4c 4c 20 69 66 20 74 68 65 20 72 65 71 75  NULL if the requ
11d6f 65 73 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f  ested page is no
11d70 74 20 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 6e  t .** already in
11d71 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74   memory..*/.stat
11d72 69 63 20 50 67 48 64 72 20 2a 70 61 67 65 72 5f  ic PgHdr *pager_
11d73 6c 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50  lookup(Pager *pP
11d74 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29  ager, Pgno pgno)
11d75 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 20 20 20  {.  PgHdr *p;   
11d76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11d77 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
11d78 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49  value */..  /* I
11d79 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
11d7a 65 20 66 6f 72 20 61 20 63 61 6c 6c 20 74 6f 20  e for a call to 
11d7b 50 63 61 63 68 65 46 65 74 63 68 28 29 20 77 69  PcacheFetch() wi
11d7c 74 68 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 30  th createFlag==0
11d7d 20 74 6f 0a 20 20 2a 2a 20 66 61 69 6c 2c 20 73   to.  ** fail, s
11d7e 69 6e 63 65 20 6e 6f 20 61 74 74 65 6d 70 74 20  ince no attempt 
11d7f 74 6f 20 61 6c 6c 6f 63 61 74 65 20 64 79 6e 61  to allocate dyna
11d80 6d 69 63 20 6d 65 6d 6f 72 79 20 77 69 6c 6c 20  mic memory will 
11d81 62 65 20 6d 61 64 65 2e 0a 20 20 2a 2f 0a 20 20  be made..  */.  
11d82 28 76 6f 69 64 29 73 71 6c 69 74 65 33 50 63 61  (void)sqlite3Pca
11d83 63 68 65 46 65 74 63 68 28 70 50 61 67 65 72 2d  cheFetch(pPager-
11d84 3e 70 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20  >pPCache, pgno, 
11d85 30 2c 20 26 70 29 3b 0a 20 20 72 65 74 75 72 6e  0, &p);.  return
11d86 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c   p;.}../*.** Unl
11d87 65 73 73 20 74 68 65 20 70 61 67 65 72 20 69 73  ess the pager is
11d88 20 69 6e 20 65 72 72 6f 72 2d 73 74 61 74 65 2c   in error-state,
11d89 20 64 69 73 63 61 72 64 20 61 6c 6c 20 69 6e 2d   discard all in-
11d8a 6d 65 6d 6f 72 79 20 70 61 67 65 73 2e 20 49 66  memory pages. If
11d8b 0a 2a 2a 20 74 68 65 20 70 61 67 65 72 20 69 73  .** the pager is
11d8c 20 69 6e 20 65 72 72 6f 72 2d 73 74 61 74 65 2c   in error-state,
11d8d 20 74 68 65 6e 20 74 68 69 73 20 63 61 6c 6c 20   then this call 
11d8e 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
11d8f 2a 20 54 4f 44 4f 3a 20 57 68 79 20 63 61 6e 20  * TODO: Why can 
11d90 77 65 20 6e 6f 74 20 72 65 73 65 74 20 74 68 65  we not reset the
11d91 20 70 61 67 65 72 20 77 68 69 6c 65 20 69 6e 20   pager while in 
11d92 65 72 72 6f 72 20 73 74 61 74 65 3f 0a 2a 2f 0a  error state?.*/.
11d93 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65  static void page
11d94 72 5f 72 65 73 65 74 28 50 61 67 65 72 20 2a 70  r_reset(Pager *p
11d95 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 53 51  Pager){.  if( SQ
11d96 4c 49 54 45 5f 4f 4b 3d 3d 70 50 61 67 65 72 2d  LITE_OK==pPager-
11d97 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20  >errCode ){.    
11d98 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73  sqlite3BackupRes
11d99 74 61 72 74 28 70 50 61 67 65 72 2d 3e 70 42 61  tart(pPager->pBa
11d9a 63 6b 75 70 29 3b 0a 20 20 20 20 73 71 6c 69 74  ckup);.    sqlit
11d9b 65 33 50 63 61 63 68 65 43 6c 65 61 72 28 70 50  e3PcacheClear(pP
11d9c 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a  ager->pPCache);.
11d9d 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69      pPager->dbSi
11d9e 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d  zeValid = 0;.  }
11d9f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61  .}../*.** Free a
11da0 6c 6c 20 73 74 72 75 63 74 75 72 65 73 20 69 6e  ll structures in
11da1 20 74 68 65 20 50 61 67 65 72 2e 61 53 61 76 65   the Pager.aSave
11da2 70 6f 69 6e 74 5b 5d 20 61 72 72 61 79 20 61 6e  point[] array an
11da3 64 20 73 65 74 20 62 6f 74 68 0a 2a 2a 20 50 61  d set both.** Pa
11da4 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 61  ger.aSavepoint a
11da5 6e 64 20 50 61 67 65 72 2e 6e 53 61 76 65 70 6f  nd Pager.nSavepo
11da6 69 6e 74 20 74 6f 20 7a 65 72 6f 2e 20 43 6c 6f  int to zero. Clo
11da7 73 65 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e  se the sub-journ
11da8 61 6c 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 6f  al.** if it is o
11da9 70 65 6e 20 61 6e 64 20 74 68 65 20 70 61 67 65  pen and the page
11daa 72 20 69 73 20 6e 6f 74 20 69 6e 20 65 78 63 6c  r is not in excl
11dab 75 73 69 76 65 20 6d 6f 64 65 2e 0a 2a 2f 0a 73  usive mode..*/.s
11dac 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61  tatic void relea
11dad 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28  seAllSavepoints(
11dae 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
11daf 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20    int ii;       
11db0 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61          /* Itera
11db1 74 6f 72 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20  tor for looping 
11db2 74 68 72 6f 75 67 68 20 50 61 67 65 72 2e 61 53  through Pager.aS
11db3 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 66 6f  avepoint */.  fo
11db4 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61 67 65  r(ii=0; ii<pPage
11db5 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69  r->nSavepoint; i
11db6 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
11db7 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70  3BitvecDestroy(p
11db8 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e  Pager->aSavepoin
11db9 74 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69  t[ii].pInSavepoi
11dba 6e 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  nt);.  }.  if( !
11dbb 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
11dbc 65 4d 6f 64 65 20 7c 7c 20 73 71 6c 69 74 65 33  eMode || sqlite3
11dbd 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61  IsMemJournal(pPa
11dbe 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20  ger->sjfd) ){.  
11dbf 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65    sqlite3OsClose
11dc0 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 3b 0a  (pPager->sjfd);.
11dc1 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72    }.  sqlite3_fr
11dc2 65 65 28 70 50 61 67 65 72 2d 3e 61 53 61 76 65  ee(pPager->aSave
11dc3 70 6f 69 6e 74 29 3b 0a 20 20 70 50 61 67 65 72  point);.  pPager
11dc4 2d 3e 61 53 61 76 65 70 6f 69 6e 74 20 3d 20 30  ->aSavepoint = 0
11dc5 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53 61 76  ;.  pPager->nSav
11dc6 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 70 50  epoint = 0;.  pP
11dc7 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 20 3d 20  ager->nSubRec = 
11dc8 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  0;.}../*.** Set 
11dc9 74 68 65 20 62 69 74 20 6e 75 6d 62 65 72 20 70  the bit number p
11dca 67 6e 6f 20 69 6e 20 74 68 65 20 50 61 67 65 72  gno in the Pager
11dcb 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76  Savepoint.pInSav
11dcc 65 70 6f 69 6e 74 20 0a 2a 2a 20 62 69 74 76 65  epoint .** bitve
11dcd 63 73 20 6f 66 20 61 6c 6c 20 6f 70 65 6e 20 73  cs of all open s
11dce 61 76 65 70 6f 69 6e 74 73 2e 20 52 65 74 75 72  avepoints. Retur
11dcf 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73  n SQLITE_OK if s
11dd0 75 63 63 65 73 73 66 75 6c 0a 2a 2a 20 6f 72 20  uccessful.** or 
11dd1 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20  SQLITE_NOMEM if 
11dd2 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  a malloc failure
11dd3 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74   occurs..*/.stat
11dd4 69 63 20 69 6e 74 20 61 64 64 54 6f 53 61 76 65  ic int addToSave
11dd5 70 6f 69 6e 74 42 69 74 76 65 63 73 28 50 61 67  pointBitvecs(Pag
11dd6 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f  er *pPager, Pgno
11dd7 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 69 69   pgno){.  int ii
11dd8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11dd9 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
11dda 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20  ter */.  int rc 
11ddb 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
11ddc 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64     /* Result cod
11ddd 65 20 2a 2f 0a 0a 20 20 66 6f 72 28 69 69 3d 30  e */..  for(ii=0
11dde 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61  ; ii<pPager->nSa
11ddf 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a  vepoint; ii++){.
11de0 20 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69      PagerSavepoi
11de1 6e 74 20 2a 70 20 3d 20 26 70 50 61 67 65 72 2d  nt *p = &pPager-
11de2 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 3b  >aSavepoint[ii];
11de3 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70  .    if( pgno<=p
11de4 2d 3e 6e 4f 72 69 67 20 29 7b 0a 20 20 20 20 20  ->nOrig ){.     
11de5 20 72 63 20 7c 3d 20 73 71 6c 69 74 65 33 42 69   rc |= sqlite3Bi
11de6 74 76 65 63 53 65 74 28 70 2d 3e 70 49 6e 53 61  tvecSet(p->pInSa
11de7 76 65 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 3b 0a  vepoint, pgno);.
11de8 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
11de9 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
11dea 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
11deb 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
11dec 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  || rc==SQLITE_NO
11ded 4d 45 4d 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  MEM );.    }.  }
11dee 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
11def 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20 74 68  ./*.** Unlock th
11df0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
11df1 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
11df2 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65  s a no-op if the
11df3 20 70 61 67 65 72 0a 2a 2a 20 69 73 20 69 6e 20   pager.** is in 
11df4 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e 0a  exclusive mode..
11df5 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67  **.** If the pag
11df6 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  er is currently 
11df7 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20  in error state, 
11df8 64 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74  discard the cont
11df9 65 6e 74 73 20 6f 66 20 0a 2a 2a 20 74 68 65 20  ents of .** the 
11dfa 63 61 63 68 65 20 61 6e 64 20 72 65 73 65 74 20  cache and reset 
11dfb 74 68 65 20 50 61 67 65 72 20 73 74 72 75 63 74  the Pager struct
11dfc 75 72 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61  ure internal sta
11dfd 74 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 0a  te. If there is.
11dfe 2a 2a 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e  ** an open journ
11dff 61 6c 2d 66 69 6c 65 2c 20 74 68 65 6e 20 74 68  al-file, then th
11e00 65 20 6e 65 78 74 20 74 69 6d 65 20 61 20 73 68  e next time a sh
11e01 61 72 65 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74  ared-lock is obt
11e02 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20  ained.** on the 
11e03 70 61 67 65 72 20 66 69 6c 65 20 28 62 79 20 74  pager file (by t
11e04 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72  his or any other
11e05 20 70 72 6f 63 65 73 73 29 2c 20 69 74 20 77 69   process), it wi
11e06 6c 6c 20 62 65 0a 2a 2a 20 74 72 65 61 74 65 64  ll be.** treated
11e07 20 61 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61   as a hot-journa
11e08 6c 20 61 6e 64 20 72 6f 6c 6c 65 64 20 62 61 63  l and rolled bac
11e09 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  k..*/.static voi
11e0a 64 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 50  d pager_unlock(P
11e0b 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
11e0c 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78   if( !pPager->ex
11e0d 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a 20  clusiveMode ){. 
11e0e 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20     int rc;      
11e0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11e10 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
11e11 2f 0a 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 73  /..    /* Always
11e12 20 63 6c 6f 73 65 20 74 68 65 20 6a 6f 75 72 6e   close the journ
11e13 61 6c 20 66 69 6c 65 20 77 68 65 6e 20 64 72 6f  al file when dro
11e14 70 70 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  pping the databa
11e15 73 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2a 20  se lock..    ** 
11e16 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 6f 74 68  Otherwise, anoth
11e17 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69  er connection wi
11e18 74 68 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d  th journal_mode=
11e19 64 65 6c 65 74 65 20 6d 69 67 68 74 0a 20 20 20  delete might.   
11e1a 20 2a 2a 20 64 65 6c 65 74 65 20 74 68 65 20 66   ** delete the f
11e1b 69 6c 65 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64  ile out from und
11e1c 65 72 20 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  er us..    */.  
11e1d 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65    sqlite3OsClose
11e1e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20  (pPager->jfd);. 
11e1f 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63     sqlite3Bitvec
11e20 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e  Destroy(pPager->
11e21 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20  pInJournal);.   
11e22 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72   pPager->pInJour
11e23 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 6c  nal = 0;.    rel
11e24 65 61 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e 74  easeAllSavepoint
11e25 73 28 70 50 61 67 65 72 29 3b 0a 0a 20 20 20 20  s(pPager);..    
11e26 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 69  /* If the file i
11e27 73 20 75 6e 6c 6f 63 6b 65 64 2c 20 73 6f 6d 65  s unlocked, some
11e28 62 6f 64 79 20 65 6c 73 65 20 6d 69 67 68 74 20  body else might 
11e29 63 68 61 6e 67 65 20 69 74 2e 20 54 68 65 0a 20  change it. The. 
11e2a 20 20 20 2a 2a 20 76 61 6c 75 65 73 20 73 74 6f     ** values sto
11e2b 72 65 64 20 69 6e 20 50 61 67 65 72 2e 64 62 53  red in Pager.dbS
11e2c 69 7a 65 20 65 74 63 2e 20 6d 69 67 68 74 20 62  ize etc. might b
11e2d 65 63 6f 6d 65 20 69 6e 76 61 6c 69 64 20 69 66  ecome invalid if
11e2e 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 68 61 70  .    ** this hap
11e2f 70 65 6e 73 2e 20 54 4f 44 4f 3a 20 52 65 61 6c  pens. TODO: Real
11e30 6c 79 2c 20 74 68 69 73 20 64 6f 65 73 6e 27 74  ly, this doesn't
11e31 20 6e 65 65 64 20 74 6f 20 62 65 20 63 6c 65 61   need to be clea
11e32 72 65 64 0a 20 20 20 20 2a 2a 20 75 6e 74 69 6c  red.    ** until
11e33 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e   the change-coun
11e34 74 65 72 20 63 68 65 63 6b 20 66 61 69 6c 73 20  ter check fails 
11e35 69 6e 20 50 61 67 65 72 53 68 61 72 65 64 4c 6f  in PagerSharedLo
11e36 63 6b 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ck()..    */.   
11e37 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56   pPager->dbSizeV
11e38 61 6c 69 64 20 3d 20 30 3b 0a 0a 20 20 20 20 72  alid = 0;..    r
11e39 63 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61  c = osUnlock(pPa
11e3a 67 65 72 2d 3e 66 64 2c 20 4e 4f 5f 4c 4f 43 4b  ger->fd, NO_LOCK
11e3b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b  );.    if( rc ){
11e3c 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65  .      pPager->e
11e3d 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 20  rrCode = rc;.   
11e3e 20 7d 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28   }.    IOTRACE((
11e3f 22 55 4e 4c 4f 43 4b 20 25 70 5c 6e 22 2c 20 70  "UNLOCK %p\n", p
11e40 50 61 67 65 72 29 29 0a 0a 20 20 20 20 2f 2a 20  Pager))..    /* 
11e41 49 66 20 50 61 67 65 72 2e 65 72 72 43 6f 64 65  If Pager.errCode
11e42 20 69 73 20 73 65 74 2c 20 74 68 65 20 63 6f 6e   is set, the con
11e43 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67  tents of the pag
11e44 65 72 20 63 61 63 68 65 20 63 61 6e 6e 6f 74 20  er cache cannot 
11e45 62 65 0a 20 20 20 20 2a 2a 20 74 72 75 73 74 65  be.    ** truste
11e46 64 2e 20 4e 6f 77 20 74 68 61 74 20 74 68 65 20  d. Now that the 
11e47 70 61 67 65 72 20 66 69 6c 65 20 69 73 20 75 6e  pager file is un
11e48 6c 6f 63 6b 65 64 2c 20 74 68 65 20 63 6f 6e 74  locked, the cont
11e49 65 6e 74 73 20 6f 66 20 74 68 65 0a 20 20 20 20  ents of the.    
11e4a 2a 2a 20 63 61 63 68 65 20 63 61 6e 20 62 65 20  ** cache can be 
11e4b 64 69 73 63 61 72 64 65 64 20 61 6e 64 20 74 68  discarded and th
11e4c 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 61 66  e error code saf
11e4d 65 6c 79 20 63 6c 65 61 72 65 64 2e 0a 20 20 20  ely cleared..   
11e4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67   */.    if( pPag
11e4f 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20  er->errCode ){. 
11e50 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
11e51 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
11e52 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64    pPager->errCod
11e53 65 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  e = SQLITE_OK;. 
11e54 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 61 67       }.      pag
11e55 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29  er_reset(pPager)
11e56 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 61  ;.    }..    pPa
11e57 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74  ger->changeCount
11e58 44 6f 6e 65 20 3d 20 30 3b 0a 20 20 20 20 70 50  Done = 0;.    pP
11e59 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41  ager->state = PA
11e5a 47 45 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 7d 0a  GER_UNLOCK;.  }.
11e5b 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
11e5c 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65  nction should be
11e5d 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20   called when an 
11e5e 49 4f 45 52 52 2c 20 43 4f 52 52 55 50 54 20 6f  IOERR, CORRUPT o
11e5f 72 20 46 55 4c 4c 20 65 72 72 6f 72 0a 2a 2a 20  r FULL error.** 
11e60 6d 61 79 20 68 61 76 65 20 6f 63 63 75 72 72 65  may have occurre
11e61 64 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67  d. The first arg
11e62 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74  ument is a point
11e63 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 72 20  er to the pager 
11e64 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74  .** structure, t
11e65 68 65 20 73 65 63 6f 6e 64 20 74 68 65 20 65 72  he second the er
11e66 72 6f 72 2d 63 6f 64 65 20 61 62 6f 75 74 20 74  ror-code about t
11e67 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79  o be returned by
11e68 20 61 20 70 61 67 65 72 20 0a 2a 2a 20 41 50 49   a pager .** API
11e69 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 76   function. The v
11e6a 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73  alue returned is
11e6b 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73   a copy of the s
11e6c 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 0a  econd argument .
11e6d 2a 2a 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  ** to this funct
11e6e 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ion. .**.** If t
11e6f 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
11e70 6e 74 20 69 73 20 53 51 4c 49 54 45 5f 49 4f 45  nt is SQLITE_IOE
11e71 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  RR, SQLITE_CORRU
11e72 50 54 2c 20 6f 72 20 53 51 4c 49 54 45 5f 46 55  PT, or SQLITE_FU
11e73 4c 4c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20  LL.** the error 
11e74 62 65 63 6f 6d 65 73 20 70 65 72 73 69 73 74 65  becomes persiste
11e75 6e 74 2e 20 55 6e 74 69 6c 20 74 68 65 20 70 65  nt. Until the pe
11e76 72 73 69 73 74 65 6e 20 65 72 72 6f 72 20 69 73  rsisten error is
11e77 20 63 6c 65 61 72 65 64 2c 0a 2a 2a 20 73 75 62   cleared,.** sub
11e78 73 65 71 75 65 6e 74 20 41 50 49 20 63 61 6c 6c  sequent API call
11e79 73 20 6f 6e 20 74 68 69 73 20 50 61 67 65 72 20  s on this Pager 
11e7a 77 69 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79  will immediately
11e7b 20 72 65 74 75 72 6e 20 74 68 65 20 73 61 6d 65   return the same
11e7c 20 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 2e   .** error code.
11e7d 0a 2a 2a 0a 2a 2a 20 41 20 70 65 72 73 69 73 74  .**.** A persist
11e7e 65 6e 74 20 65 72 72 6f 72 20 69 6e 64 69 63 61  ent error indica
11e7f 74 65 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e  tes that the con
11e80 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67  tents of the pag
11e81 65 72 2d 63 61 63 68 65 20 0a 2a 2a 20 63 61 6e  er-cache .** can
11e82 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64 2e 20  not be trusted. 
11e83 54 68 69 73 20 73 74 61 74 65 20 63 61 6e 20 62  This state can b
11e84 65 20 63 6c 65 61 72 65 64 20 62 79 20 63 6f 6d  e cleared by com
11e85 70 6c 65 74 65 6c 79 20 64 69 73 63 61 72 64 69  pletely discardi
11e86 6e 67 20 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65  ng .** the conte
11e87 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 72  nts of the pager
11e88 2d 63 61 63 68 65 2e 20 49 66 20 61 20 74 72 61  -cache. If a tra
11e89 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 61 63 74  nsaction was act
11e8a 69 76 65 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20  ive when.** the 
11e8b 70 65 72 73 69 73 74 65 6e 74 20 65 72 72 6f 72  persistent error
11e8c 20 6f 63 63 75 72 72 65 64 2c 20 74 68 65 6e 20   occurred, then 
11e8d 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
11e8e 72 6e 61 6c 20 6d 61 79 20 6e 65 65 64 0a 2a 2a  rnal may need.**
11e8f 20 74 6f 20 62 65 20 72 65 70 6c 61 79 65 64 20   to be replayed 
11e90 74 6f 20 72 65 73 74 6f 72 65 20 74 68 65 20 63  to restore the c
11e91 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64  ontents of the d
11e92 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 61 73  atabase file (as
11e93 20 69 66 0a 2a 2a 20 69 74 20 77 65 72 65 20 61   if.** it were a
11e94 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 29 2e 0a 2a   hot-journal)..*
11e95 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
11e96 65 72 5f 65 72 72 6f 72 28 50 61 67 65 72 20 2a  er_error(Pager *
11e97 70 50 61 67 65 72 2c 20 69 6e 74 20 72 63 29 7b  pPager, int rc){
11e98 0a 20 20 69 6e 74 20 72 63 32 20 3d 20 72 63 20  .  int rc2 = rc 
11e99 26 20 30 78 66 66 3b 0a 20 20 61 73 73 65 72 74  & 0xff;.  assert
11e9a 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
11e9b 7c 7c 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 61  || !MEMDB );.  a
11e9c 73 73 65 72 74 28 0a 20 20 20 20 20 20 20 70 50  ssert(.       pP
11e9d 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53  ager->errCode==S
11e9e 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20  QLITE_FULL ||.  
11e9f 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72       pPager->err
11ea0 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  Code==SQLITE_OK 
11ea1 7c 7c 0a 20 20 20 20 20 20 20 28 70 50 61 67 65  ||.       (pPage
11ea2 72 2d 3e 65 72 72 43 6f 64 65 20 26 20 30 78 66  r->errCode & 0xf
11ea3 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  f)==SQLITE_IOERR
11ea4 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63 32 3d  .  );.  if( rc2=
11ea5 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 20  =SQLITE_FULL || 
11ea6 72 63 32 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52  rc2==SQLITE_IOER
11ea7 52 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d  R ){.    pPager-
11ea8 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20  >errCode = rc;. 
11ea9 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
11eaa 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65  }../*.** Execute
11eab 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 61   a rollback if a
11eac 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
11ead 61 63 74 69 76 65 20 61 6e 64 20 75 6e 6c 6f 63  active and unloc
11eae 6b 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61  k the .** databa
11eaf 73 65 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20  se file. .**.** 
11eb0 49 66 20 74 68 65 20 70 61 67 65 72 20 68 61 73  If the pager has
11eb1 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64   already entered
11eb2 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
11eb3 2c 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74  , do not attempt
11eb4 20 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63   .** the rollbac
11eb5 6b 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20  k at this time. 
11eb6 49 6e 73 74 65 61 64 2c 20 70 61 67 65 72 5f 75  Instead, pager_u
11eb7 6e 6c 6f 63 6b 28 29 20 69 73 20 63 61 6c 6c 65  nlock() is calle
11eb8 64 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 20 74  d. The.** call t
11eb9 6f 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29  o pager_unlock()
11eba 20 77 69 6c 6c 20 64 69 73 63 61 72 64 20 61 6c   will discard al
11ebb 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65  l in-memory page
11ebc 73 2c 20 75 6e 6c 6f 63 6b 0a 2a 2a 20 74 68 65  s, unlock.** the
11ebd 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
11ebe 6e 64 20 63 6c 65 61 72 20 74 68 65 20 65 72 72  nd clear the err
11ebf 6f 72 20 73 74 61 74 65 2e 20 49 66 20 74 68 69  or state. If thi
11ec0 73 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20  s means that.** 
11ec1 74 68 65 72 65 20 69 73 20 61 20 68 6f 74 2d 6a  there is a hot-j
11ec2 6f 75 72 6e 61 6c 20 6c 65 66 74 20 69 6e 20 74  ournal left in t
11ec3 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20  he file-system, 
11ec4 74 68 65 20 6e 65 78 74 20 63 6f 6e 6e 65 63 74  the next connect
11ec5 69 6f 6e 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e  ion.** to obtain
11ec6 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f   a shared lock o
11ec7 6e 20 74 68 65 20 70 61 67 65 72 20 28 77 68 69  n the pager (whi
11ec8 63 68 20 6d 61 79 20 62 65 20 74 68 69 73 20 6f  ch may be this o
11ec9 6e 65 29 20 77 69 6c 6c 0a 2a 2a 20 72 6f 6c 6c  ne) will.** roll
11eca 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20   it back..**.** 
11ecb 49 66 20 74 68 65 20 70 61 67 65 72 20 68 61 73  If the pager has
11ecc 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 6e 74   not already ent
11ecd 65 72 65 64 20 74 68 65 20 65 72 72 6f 72 20 73  ered the error s
11ece 74 61 74 65 2c 20 62 75 74 20 61 6e 20 49 4f 20  tate, but an IO 
11ecf 6f 72 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72  or.** malloc err
11ed0 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
11ed1 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65   a rollback, the
11ed2 6e 20 74 68 69 73 20 77 69 6c 6c 20 69 74 73 65  n this will itse
11ed3 6c 66 20 63 61 75 73 65 20 0a 2a 2a 20 74 68 65  lf cause .** the
11ed4 20 70 61 67 65 72 20 74 6f 20 65 6e 74 65 72 20   pager to enter 
11ed5 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e  the error state.
11ed6 20 57 68 69 63 68 20 77 69 6c 6c 20 62 65 20 63   Which will be c
11ed7 6c 65 61 72 65 64 20 62 79 20 74 68 65 0a 2a 2a  leared by the.**
11ed8 20 63 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f 75   call to pager_u
11ed9 6e 6c 6f 63 6b 28 29 2c 20 61 73 20 64 65 73 63  nlock(), as desc
11eda 72 69 62 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a  ribed above..*/.
11edb 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65  static void page
11edc 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61  rUnlockAndRollba
11edd 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ck(Pager *pPager
11ede 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  ){.  if( pPager-
11edf 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45  >errCode==SQLITE
11ee0 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 73  _OK && pPager->s
11ee1 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45  tate>=PAGER_RESE
11ee2 52 56 45 44 20 29 7b 0a 20 20 20 20 73 71 6c 69  RVED ){.    sqli
11ee3 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61  te3BeginBenignMa
11ee4 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 73 71 6c 69  lloc();.    sqli
11ee5 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b  te3PagerRollback
11ee6 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 73 71  (pPager);.    sq
11ee7 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61  lite3EndBenignMa
11ee8 6c 6c 6f 63 28 29 3b 0a 20 20 7d 0a 20 20 70 61  lloc();.  }.  pa
11ee9 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65  ger_unlock(pPage
11eea 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  r);.}../*.** Thi
11eeb 73 20 72 6f 75 74 69 6e 65 20 65 6e 64 73 20 61  s routine ends a
11eec 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 20   transaction. A 
11eed 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 75  transaction is u
11eee 73 75 61 6c 6c 79 20 65 6e 64 65 64 20 62 79 20  sually ended by 
11eef 0a 2a 2a 20 65 69 74 68 65 72 20 61 20 43 4f 4d  .** either a COM
11ef0 4d 49 54 20 6f 72 20 61 20 52 4f 4c 4c 42 41 43  MIT or a ROLLBAC
11ef1 4b 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 69  K operation. Thi
11ef2 73 20 72 6f 75 74 69 6e 65 20 6d 61 79 20 62 65  s routine may be
11ef3 20 63 61 6c 6c 65 64 20 0a 2a 2a 20 61 66 74 65   called .** afte
11ef4 72 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 20  r rollback of a 
11ef5 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20  hot-journal, or 
11ef6 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  if an error occu
11ef7 72 73 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e 67  rs while opening
11ef8 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  .** the journal 
11ef9 66 69 6c 65 20 6f 72 20 77 72 69 74 69 6e 67 20  file or writing 
11efa 74 68 65 20 76 65 72 79 20 66 69 72 73 74 20 6a  the very first j
11efb 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 6f 66  ournal-header of
11efc 20 61 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74   a.** database t
11efd 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 0a  ransaction..** .
11efe 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20  ** If the pager 
11eff 69 73 20 69 6e 20 50 41 47 45 52 5f 53 48 41 52  is in PAGER_SHAR
11f00 45 44 20 6f 72 20 50 41 47 45 52 5f 55 4e 4c 4f  ED or PAGER_UNLO
11f01 43 4b 20 73 74 61 74 65 20 77 68 65 6e 20 74 68  CK state when th
11f02 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73  is.** routine is
11f03 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61   called, it is a
11f04 20 6e 6f 2d 6f 70 20 28 72 65 74 75 72 6e 73 20   no-op (returns 
11f05 53 51 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a  SQLITE_OK)..**.*
11f06 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 79  * Otherwise, any
11f07 20 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e   active savepoin
11f08 74 73 20 61 72 65 20 72 65 6c 65 61 73 65 64 2e  ts are released.
11f09 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f  .**.** If the jo
11f0a 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70  urnal file is op
11f0b 65 6e 2c 20 74 68 65 6e 20 69 74 20 69 73 20 22  en, then it is "
11f0c 66 69 6e 61 6c 69 7a 65 64 22 2e 20 4f 6e 63 65  finalized". Once
11f0d 20 61 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 66   a journal .** f
11f0e 69 6c 65 20 68 61 73 20 62 65 65 6e 20 66 69 6e  ile has been fin
11f0f 61 6c 69 7a 65 64 20 69 74 20 69 73 20 6e 6f 74  alized it is not
11f10 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65   possible to use
11f11 20 69 74 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b   it to roll back
11f12 20 61 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69   a .** transacti
11f13 6f 6e 2e 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20  on. Nor will it 
11f14 62 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f  be considered to
11f15 20 62 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61   be a hot-journa
11f16 6c 20 62 79 20 74 68 69 73 0a 2a 2a 20 6f 72 20  l by this.** or 
11f17 61 6e 79 20 6f 74 68 65 72 20 64 61 74 61 62 61  any other databa
11f18 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 45  se connection. E
11f19 78 61 63 74 6c 79 20 68 6f 77 20 61 20 6a 6f 75  xactly how a jou
11f1a 72 6e 61 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65  rnal is finalize
11f1b 64 0a 2a 2a 20 64 65 70 65 6e 64 73 20 6f 6e 20  d.** depends on 
11f1c 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
11f1d 68 65 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e  he pager is runn
11f1e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65  ing in exclusive
11f1f 20 6d 6f 64 65 20 61 6e 64 0a 2a 2a 20 74 68 65   mode and.** the
11f20 20 63 75 72 72 65 6e 74 20 6a 6f 75 72 6e 61 6c   current journal
11f21 2d 6d 6f 64 65 20 28 50 61 67 65 72 2e 6a 6f 75  -mode (Pager.jou
11f22 72 6e 61 6c 4d 6f 64 65 20 76 61 6c 75 65 29 2c  rnalMode value),
11f23 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
11f24 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65  **   journalMode
11f25 3d 3d 4d 45 4d 4f 52 59 0a 2a 2a 20 20 20 20 20  ==MEMORY.**     
11f26 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73  Journal file des
11f27 63 72 69 70 74 6f 72 20 69 73 20 73 69 6d 70 6c  criptor is simpl
11f28 79 20 63 6c 6f 73 65 64 2e 20 54 68 69 73 20 64  y closed. This d
11f29 65 73 74 72 6f 79 73 20 61 6e 20 0a 2a 2a 20 20  estroys an .**  
11f2a 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75     in-memory jou
11f2b 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f  rnal..**.**   jo
11f2c 75 72 6e 61 6c 4d 6f 64 65 3d 3d 54 52 55 4e 43  urnalMode==TRUNC
11f2d 41 54 45 0a 2a 2a 20 20 20 20 20 4a 6f 75 72 6e  ATE.**     Journ
11f2e 61 6c 20 66 69 6c 65 20 69 73 20 74 72 75 6e 63  al file is trunc
11f2f 61 74 65 64 20 74 6f 20 7a 65 72 6f 20 62 79 74  ated to zero byt
11f30 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a  es in size..**.*
11f31 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d  *   journalMode=
11f32 3d 50 45 52 53 49 53 54 0a 2a 2a 20 20 20 20 20  =PERSIST.**     
11f33 54 68 65 20 66 69 72 73 74 20 32 38 20 62 79 74  The first 28 byt
11f34 65 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  es of the journa
11f35 6c 20 66 69 6c 65 20 61 72 65 20 7a 65 72 6f 65  l file are zeroe
11f36 64 2e 20 54 68 69 73 20 69 6e 76 61 6c 69 64 61  d. This invalida
11f37 74 65 73 0a 2a 2a 20 20 20 20 20 74 68 65 20 66  tes.**     the f
11f38 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61  irst journal hea
11f39 64 65 72 20 69 6e 20 74 68 65 20 66 69 6c 65 2c  der in the file,
11f3a 20 61 6e 64 20 68 65 6e 63 65 20 74 68 65 20 65   and hence the e
11f3b 6e 74 69 72 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a  ntire journal.**
11f3c 20 20 20 20 20 66 69 6c 65 2e 20 41 6e 20 69 6e       file. An in
11f3d 76 61 6c 69 64 20 6a 6f 75 72 6e 61 6c 20 66 69  valid journal fi
11f3e 6c 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 6f 6c  le cannot be rol
11f3f 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  led back..**.** 
11f40 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 44    journalMode==D
11f41 45 4c 45 54 45 0a 2a 2a 20 20 20 20 20 54 68 65  ELETE.**     The
11f42 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
11f43 20 63 6c 6f 73 65 64 20 61 6e 64 20 64 65 6c 65   closed and dele
11f44 74 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65  ted using sqlite
11f45 33 4f 73 44 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a  3OsDelete()..**.
11f46 2a 2a 20 20 20 20 20 49 66 20 74 68 65 20 70 61  **     If the pa
11f47 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69  ger is running i
11f48 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65  n exclusive mode
11f49 2c 20 74 68 69 73 20 6d 65 74 68 6f 64 20 6f 66  , this method of
11f4a 20 66 69 6e 61 6c 69 7a 69 6e 67 0a 2a 2a 20 20   finalizing.**  
11f4b 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66     the journal f
11f4c 69 6c 65 20 69 73 20 6e 65 76 65 72 20 75 73 65  ile is never use
11f4d 64 2e 20 49 6e 73 74 65 61 64 2c 20 69 66 20 74  d. Instead, if t
11f4e 68 65 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 69  he journalMode i
11f4f 73 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20  s.**     DELETE 
11f50 61 6e 64 20 74 68 65 20 70 61 67 65 72 20 69 73  and the pager is
11f51 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f   in exclusive mo
11f52 64 65 2c 20 74 68 65 20 6d 65 74 68 6f 64 20 64  de, the method d
11f53 65 73 63 72 69 62 65 64 20 75 6e 64 65 72 0a 2a  escribed under.*
11f54 2a 20 20 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64  *     journalMod
11f55 65 3d 3d 50 45 52 53 49 53 54 20 69 73 20 75 73  e==PERSIST is us
11f56 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a  ed instead..**.*
11f57 2a 20 41 66 74 65 72 20 74 68 65 20 6a 6f 75 72  * After the jour
11f58 6e 61 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65 64  nal is finalized
11f59 2c 20 69 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20  , if running in 
11f5a 6e 6f 6e 2d 65 78 63 6c 75 73 69 76 65 20 6d 6f  non-exclusive mo
11f5b 64 65 2c 20 74 68 65 0a 2a 2a 20 70 61 67 65 72  de, the.** pager
11f5c 20 6d 6f 76 65 73 20 74 6f 20 50 41 47 45 52 5f   moves to PAGER_
11f5d 53 48 41 52 45 44 20 73 74 61 74 65 20 28 61 6e  SHARED state (an
11f5e 64 20 64 6f 77 6e 67 72 61 64 65 73 20 74 68 65  d downgrades the
11f5f 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 2a 2a 20   lock on the.** 
11f60 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 63  database file ac
11f61 63 6f 72 64 69 6e 67 6c 79 29 2e 0a 2a 2a 0a 2a  cordingly)..**.*
11f62 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  * If the pager i
11f63 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63  s running in exc
11f64 6c 75 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20  lusive mode and 
11f65 69 73 20 69 6e 20 50 41 47 45 52 5f 53 59 4e 43  is in PAGER_SYNC
11f66 45 44 20 73 74 61 74 65 2c 0a 2a 2a 20 69 74 20  ED state,.** it 
11f67 6d 6f 76 65 73 20 74 6f 20 50 41 47 45 52 5f 45  moves to PAGER_E
11f68 58 43 4c 55 53 49 56 45 2e 20 4e 6f 20 6c 6f 63  XCLUSIVE. No loc
11f69 6b 73 20 61 72 65 20 64 6f 77 6e 67 72 61 64 65  ks are downgrade
11f6a 64 20 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69  d when running i
11f6b 6e 0a 2a 2a 20 65 78 63 6c 75 73 69 76 65 20 6d  n.** exclusive m
11f6c 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54  ode..**.** SQLIT
11f6d 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
11f6e 20 69 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63   if no error occ
11f6f 75 72 73 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  urs. If an error
11f70 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 0a 2a   occurs during.*
11f71 2a 20 61 6e 79 20 6f 66 20 74 68 65 20 49 4f 20  * any of the IO 
11f72 6f 70 65 72 61 74 69 6f 6e 73 20 74 6f 20 66 69  operations to fi
11f73 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e  nalize the journ
11f74 61 6c 20 66 69 6c 65 20 6f 72 20 75 6e 6c 6f 63  al file or unloc
11f75 6b 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  k the.** databas
11f76 65 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72  e then the IO er
11f77 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
11f78 72 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65 72  rned to the user
11f79 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 6f 70 65  . If the .** ope
11f7a 72 61 74 69 6f 6e 20 74 6f 20 66 69 6e 61 6c 69  ration to finali
11f7b 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ze the journal f
11f7c 69 6c 65 20 66 61 69 6c 73 2c 20 74 68 65 6e 20  ile fails, then 
11f7d 74 68 65 20 63 6f 64 65 20 73 74 69 6c 6c 0a 2a  the code still.*
11f7e 2a 20 74 72 69 65 73 20 74 6f 20 75 6e 6c 6f 63  * tries to unloc
11f7f 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  k the database f
11f80 69 6c 65 20 69 66 20 6e 6f 74 20 69 6e 20 65 78  ile if not in ex
11f81 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e 20 49 66  clusive mode. If
11f82 20 74 68 65 0a 2a 2a 20 75 6e 6c 6f 63 6b 20 6f   the.** unlock o
11f83 70 65 72 61 74 69 6f 6e 20 66 61 69 6c 73 20 61  peration fails a
11f84 73 20 77 65 6c 6c 2c 20 74 68 65 6e 20 74 68 65  s well, then the
11f85 20 66 69 72 73 74 20 65 72 72 6f 72 20 63 6f 64   first error cod
11f86 65 20 72 65 6c 61 74 65 64 0a 2a 2a 20 74 6f 20  e related.** to 
11f87 74 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20  the first error 
11f88 65 6e 63 6f 75 6e 74 65 72 65 64 20 28 74 68 65  encountered (the
11f89 20 6a 6f 75 72 6e 61 6c 20 66 69 6e 61 6c 69 7a   journal finaliz
11f8a 61 74 69 6f 6e 20 6f 6e 65 29 20 69 73 0a 2a 2a  ation one) is.**
11f8b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74   returned..*/.st
11f8c 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 65  atic int pager_e
11f8d 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 50  nd_transaction(P
11f8e 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
11f8f 74 20 68 61 73 4d 61 73 74 65 72 29 7b 0a 20 20  t hasMaster){.  
11f90 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
11f91 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 45 72 72 6f  OK;      /* Erro
11f92 72 20 63 6f 64 65 20 66 72 6f 6d 20 6a 6f 75 72  r code from jour
11f93 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e  nal finalization
11f94 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20   operation */.  
11f95 69 6e 74 20 72 63 32 20 3d 20 53 51 4c 49 54 45  int rc2 = SQLITE
11f96 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20 45 72 72 6f  _OK;     /* Erro
11f97 72 20 63 6f 64 65 20 66 72 6f 6d 20 64 62 20 66  r code from db f
11f98 69 6c 65 20 75 6e 6c 6f 63 6b 20 6f 70 65 72 61  ile unlock opera
11f99 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70  tion */..  if( p
11f9a 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 50 41 47  Pager->state<PAG
11f9b 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20  ER_RESERVED ){. 
11f9c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
11f9d 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61  _OK;.  }.  relea
11f9e 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28  seAllSavepoints(
11f9f 70 50 61 67 65 72 29 3b 0a 0a 20 20 61 73 73 65  pPager);..  asse
11fa0 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65  rt( isOpen(pPage
11fa1 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65  r->jfd) || pPage
11fa2 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30  r->pInJournal==0
11fa3 20 29 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e   );.  if( isOpen
11fa4 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b  (pPager->jfd) ){
11fa5 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 61 6c 69 7a  ..    /* Finaliz
11fa6 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  e the journal fi
11fa7 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 73  le. */.    if( s
11fa8 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e  qlite3IsMemJourn
11fa9 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  al(pPager->jfd) 
11faa 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
11fab 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
11fac 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
11fad 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29  NALMODE_MEMORY )
11fae 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  ;.      sqlite3O
11faf 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a  sClose(pPager->j
11fb0 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  fd);.    }else i
11fb1 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  f( pPager->journ
11fb2 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f  alMode==PAGER_JO
11fb3 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41  URNALMODE_TRUNCA
11fb4 54 45 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  TE ){.      if( 
11fb5 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
11fb6 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ff==0 ){.       
11fb7 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
11fb8 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
11fb9 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
11fba 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61  e3OsTruncate(pPa
11fbb 67 65 72 2d 3e 6a 66 64 2c 20 30 29 3b 0a 20 20  ger->jfd, 0);.  
11fbc 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67      }.      pPag
11fbd 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d  er->journalOff =
11fbe 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72   0;.      pPager
11fbf 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64  ->journalStarted
11fc0 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20   = 0;.    }else 
11fc1 69 66 28 20 70 50 61 67 65 72 2d 3e 65 78 63 6c  if( pPager->excl
11fc2 75 73 69 76 65 4d 6f 64 65 20 0a 20 20 20 20 20  usiveMode .     
11fc3 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  || pPager->journ
11fc4 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f  alMode==PAGER_JO
11fc5 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53  URNALMODE_PERSIS
11fc6 54 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72  T.    ){.      r
11fc7 63 20 3d 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48  c = zeroJournalH
11fc8 64 72 28 70 50 61 67 65 72 2c 20 68 61 73 4d 61  dr(pPager, hasMa
11fc9 73 74 65 72 29 3b 0a 20 20 20 20 20 20 70 61 67  ster);.      pag
11fca 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c  er_error(pPager,
11fcb 20 72 63 29 3b 0a 20 20 20 20 20 20 70 50 61 67   rc);.      pPag
11fcc 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d  er->journalOff =
11fcd 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72   0;.      pPager
11fce 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64  ->journalStarted
11fcf 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
11fd0 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62  .      /* This b
11fd1 72 61 6e 63 68 20 6d 61 79 20 62 65 20 65 78 65  ranch may be exe
11fd2 63 75 74 65 64 20 77 69 74 68 20 50 61 67 65 72  cuted with Pager
11fd3 2e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45  .journalMode==ME
11fd4 4d 4f 52 59 20 69 66 0a 20 20 20 20 20 20 2a 2a  MORY if.      **
11fd5 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 77   a hot-journal w
11fd6 61 73 20 6a 75 73 74 20 72 6f 6c 6c 65 64 20 62  as just rolled b
11fd7 61 63 6b 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ack. In this cas
11fd8 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20  e the journal.  
11fd9 20 20 20 20 2a 2a 20 66 69 6c 65 20 73 68 6f 75      ** file shou
11fda 6c 64 20 62 65 20 63 6c 6f 73 65 64 20 61 6e 64  ld be closed and
11fdb 20 64 65 6c 65 74 65 64 2e 20 49 66 20 74 68 69   deleted. If thi
11fdc 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 72 69  s connection wri
11fdd 74 65 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  tes to.      ** 
11fde 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
11fdf 65 2c 20 69 74 20 77 69 6c 6c 20 64 6f 20 73 6f  e, it will do so
11fe0 20 75 73 69 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d   using an in-mem
11fe1 6f 72 79 20 6a 6f 75 72 6e 61 6c 2e 20 20 2a 2f  ory journal.  */
11fe2 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
11fe3 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
11fe4 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
11fe5 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 20 0a 20 20  LMODE_DELETE .  
11fe6 20 20 20 20 20 20 20 20 20 7c 7c 20 70 50 61 67           || pPag
11fe7 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d  er->journalMode=
11fe8 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
11fe9 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20  DE_MEMORY .     
11fea 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
11feb 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d  3OsClose(pPager-
11fec 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28  >jfd);.      if(
11fed 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69   !pPager->tempFi
11fee 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  le ){.        rc
11fef 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65   = sqlite3OsDele
11ff0 74 65 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c  te(pPager->pVfs,
11ff1 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61   pPager->zJourna
11ff2 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  l, 0);.      }. 
11ff3 20 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c     }..#ifdef SQL
11ff4 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a  ITE_CHECK_PAGES.
11ff5 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68      sqlite3Pcach
11ff6 65 49 74 65 72 61 74 65 44 69 72 74 79 28 70 50  eIterateDirty(pP
11ff7 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70  ager->pPCache, p
11ff8 61 67 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73  ager_set_pagehas
11ff9 68 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20  h);.#endif..    
11ffa 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65  sqlite3PcacheCle
11ffb 61 6e 41 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50  anAll(pPager->pP
11ffc 43 61 63 68 65 29 3b 0a 20 20 20 20 73 71 6c 69  Cache);.    sqli
11ffd 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79  te3BitvecDestroy
11ffe 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72  (pPager->pInJour
11fff 6e 61 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72  nal);.    pPager
12000 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30  ->pInJournal = 0
12001 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52  ;.    pPager->nR
12002 65 63 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 69  ec = 0;.  }..  i
12003 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c  f( !pPager->excl
12004 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20  usiveMode ){.   
12005 20 72 63 32 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28   rc2 = osUnlock(
12006 70 50 61 67 65 72 2d 3e 66 64 2c 20 53 48 41 52  pPager->fd, SHAR
12007 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 70 50  ED_LOCK);.    pP
12008 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41  ager->state = PA
12009 47 45 52 5f 53 48 41 52 45 44 3b 0a 20 20 20 20  GER_SHARED;.    
1200a 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f  pPager->changeCo
1200b 75 6e 74 44 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d  untDone = 0;.  }
1200c 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 2d  else if( pPager-
1200d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 59  >state==PAGER_SY
1200e 4e 43 45 44 20 29 7b 0a 20 20 20 20 70 50 61 67  NCED ){.    pPag
1200f 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45  er->state = PAGE
12010 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 7d  R_EXCLUSIVE;.  }
12011 0a 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61  .  pPager->setMa
12012 73 74 65 72 20 3d 20 30 3b 0a 20 20 70 50 61 67  ster = 0;.  pPag
12013 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30  er->needSync = 0
12014 3b 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f  ;.  pPager->dbMo
12015 64 69 66 69 65 64 20 3d 20 30 3b 0a 0a 20 20 2f  dified = 0;..  /
12016 2a 20 54 4f 44 4f 3a 20 49 73 20 74 68 69 73 20  * TODO: Is this 
12017 6f 70 74 69 6d 61 6c 3f 20 57 68 79 20 69 73 20  optimal? Why is 
12018 74 68 65 20 64 62 20 73 69 7a 65 20 69 6e 76 61  the db size inva
12019 6c 69 64 61 74 65 64 20 68 65 72 65 20 0a 20 20  lidated here .  
1201a 2a 2a 20 77 68 65 6e 20 74 68 65 20 64 61 74 61  ** when the data
1201b 62 61 73 65 20 66 69 6c 65 20 69 73 20 6e 6f 74  base file is not
1201c 20 75 6e 6c 6f 63 6b 65 64 3f 20 2a 2f 0a 20 20   unlocked? */.  
1201d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69  pPager->dbOrigSi
1201e 7a 65 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  ze = 0;.  sqlite
1201f 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 28  3PcacheTruncate(
12020 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c  pPager->pPCache,
12021 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29   pPager->dbSize)
12022 3b 0a 20 20 69 66 28 20 21 4d 45 4d 44 42 20 29  ;.  if( !MEMDB )
12023 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62  {.    pPager->db
12024 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20  SizeValid = 0;. 
12025 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28 72 63   }..  return (rc
12026 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a  ==SQLITE_OK?rc2:
12027 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61  rc);.}../*.** Pa
12028 72 61 6d 65 74 65 72 20 61 44 61 74 61 20 6d 75  rameter aData mu
12029 73 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75  st point to a bu
1202a 66 66 65 72 20 6f 66 20 70 50 61 67 65 72 2d 3e  ffer of pPager->
1202b 70 61 67 65 53 69 7a 65 20 62 79 74 65 73 0a 2a  pageSize bytes.*
1202c 2a 20 6f 66 20 64 61 74 61 2e 20 43 6f 6d 70 75  * of data. Compu
1202d 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20  te and return a 
1202e 63 68 65 63 6b 73 75 6d 20 62 61 73 65 64 20 6f  checksum based o
1202f 6e 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  nt the contents 
12030 6f 66 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 20  of the .** page 
12031 6f 66 20 64 61 74 61 20 61 6e 64 20 74 68 65 20  of data and the 
12032 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66  current value of
12033 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e   pPager->cksumIn
12034 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  it..**.** This i
12035 73 20 6e 6f 74 20 61 20 72 65 61 6c 20 63 68 65  s not a real che
12036 63 6b 73 75 6d 2e 20 49 74 20 69 73 20 72 65 61  cksum. It is rea
12037 6c 6c 79 20 6a 75 73 74 20 74 68 65 20 73 75 6d  lly just the sum
12038 20 6f 66 20 74 68 65 20 0a 2a 2a 20 72 61 6e 64   of the .** rand
12039 6f 6d 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65  om initial value
1203a 20 28 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49   (pPager->cksumI
1203b 6e 69 74 29 20 61 6e 64 20 65 76 65 72 79 20 32  nit) and every 2
1203c 30 30 74 68 20 62 79 74 65 0a 2a 2a 20 6f 66 20  00th byte.** of 
1203d 74 68 65 20 70 61 67 65 20 64 61 74 61 2c 20 73  the page data, s
1203e 74 61 72 74 69 6e 67 20 77 69 74 68 20 62 79 74  tarting with byt
1203f 65 20 6f 66 66 73 65 74 20 28 70 50 61 67 65 72  e offset (pPager
12040 2d 3e 70 61 67 65 53 69 7a 65 25 32 30 30 29 2e  ->pageSize%200).
12041 0a 2a 2a 20 45 61 63 68 20 62 79 74 65 20 69 73  .** Each byte is
12042 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20   interpreted as 
12043 61 6e 20 38 2d 62 69 74 20 75 6e 73 69 67 6e 65  an 8-bit unsigne
12044 64 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a  d integer..**.**
12045 20 43 68 61 6e 67 69 6e 67 20 74 68 65 20 66 6f   Changing the fo
12046 72 6d 75 6c 61 20 75 73 65 64 20 74 6f 20 63 6f  rmula used to co
12047 6d 70 75 74 65 20 74 68 69 73 20 63 68 65 63 6b  mpute this check
12048 73 75 6d 20 72 65 73 75 6c 74 73 20 69 6e 20 61  sum results in a
12049 6e 0a 2a 2a 20 69 6e 63 6f 6d 70 61 74 69 62 6c  n.** incompatibl
1204a 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66  e journal file f
1204b 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ormat..**.** If 
1204c 6a 6f 75 72 6e 61 6c 20 63 6f 72 72 75 70 74 69  journal corrupti
1204d 6f 6e 20 6f 63 63 75 72 73 20 64 75 65 20 74 6f  on occurs due to
1204e 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65   a power failure
1204f 2c 20 74 68 65 20 6d 6f 73 74 20 6c 69 6b 65 6c  , the most likel
12050 79 20 0a 2a 2a 20 73 63 65 6e 61 72 69 6f 20 69  y .** scenario i
12051 73 20 74 68 61 74 20 6f 6e 65 20 65 6e 64 20 6f  s that one end o
12052 72 20 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74  r the other of t
12053 68 65 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 62  he record will b
12054 65 20 63 68 61 6e 67 65 64 2e 20 0a 2a 2a 20 49  e changed. .** I
12055 74 20 69 73 20 6d 75 63 68 20 6c 65 73 73 20 6c  t is much less l
12056 69 6b 65 6c 79 20 74 68 61 74 20 74 68 65 20 74  ikely that the t
12057 77 6f 20 65 6e 64 73 20 6f 66 20 74 68 65 20 6a  wo ends of the j
12058 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 20 77 69  ournal record wi
12059 6c 6c 20 62 65 0a 2a 2a 20 63 6f 72 72 65 63 74  ll be.** correct
1205a 20 61 6e 64 20 74 68 65 20 6d 69 64 64 6c 65 20   and the middle 
1205b 62 65 20 63 6f 72 72 75 70 74 2e 20 20 54 68 75  be corrupt.  Thu
1205c 73 2c 20 74 68 69 73 20 22 63 68 65 63 6b 73 75  s, this "checksu
1205d 6d 22 20 73 63 68 65 6d 65 2c 0a 2a 2a 20 74 68  m" scheme,.** th
1205e 6f 75 67 68 20 66 61 73 74 20 61 6e 64 20 73 69  ough fast and si
1205f 6d 70 6c 65 2c 20 63 61 74 63 68 65 73 20 74 68  mple, catches th
12060 65 20 6d 6f 73 74 6c 79 20 6c 69 6b 65 6c 79 20  e mostly likely 
12061 6b 69 6e 64 20 6f 66 20 63 6f 72 72 75 70 74 69  kind of corrupti
12062 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33  on..*/.static u3
12063 32 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 50 61  2 pager_cksum(Pa
12064 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e  ger *pPager, con
12065 73 74 20 75 38 20 2a 61 44 61 74 61 29 7b 0a 20  st u8 *aData){. 
12066 20 75 33 32 20 63 6b 73 75 6d 20 3d 20 70 50 61   u32 cksum = pPa
12067 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 3b 20  ger->cksumInit; 
12068 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b          /* Check
12069 73 75 6d 20 76 61 6c 75 65 20 74 6f 20 72 65 74  sum value to ret
1206a 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d  urn */.  int i =
1206b 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
1206c 65 2d 32 30 30 3b 20 20 20 20 20 20 20 20 20 20  e-200;          
1206d 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
1206e 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20  */.  while( i>0 
1206f 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20  ){.    cksum += 
12070 61 44 61 74 61 5b 69 5d 3b 0a 20 20 20 20 69 20  aData[i];.    i 
12071 2d 3d 20 32 30 30 3b 0a 20 20 7d 0a 20 20 72 65  -= 200;.  }.  re
12072 74 75 72 6e 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f  turn cksum;.}../
12073 2a 0a 2a 2a 20 52 65 61 64 20 61 20 73 69 6e 67  *.** Read a sing
12074 6c 65 20 70 61 67 65 20 66 72 6f 6d 20 65 69 74  le page from eit
12075 68 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  her the journal 
12076 66 69 6c 65 20 28 69 66 20 69 73 4d 61 69 6e 4a  file (if isMainJ
12077 72 6e 6c 3d 3d 31 29 20 6f 72 0a 2a 2a 20 66 72  rnl==1) or.** fr
12078 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e  om the sub-journ
12079 61 6c 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e  al (if isMainJrn
1207a 6c 3d 3d 30 29 20 61 6e 64 20 70 6c 61 79 62 61  l==0) and playba
1207b 63 6b 20 74 68 61 74 20 70 61 67 65 2e 0a 2a 2a  ck that page..**
1207c 20 54 68 65 20 70 61 67 65 20 62 65 67 69 6e 73   The page begins
1207d 20 61 74 20 6f 66 66 73 65 74 20 2a 70 4f 66 66   at offset *pOff
1207e 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c  set into the fil
1207f 65 2e 20 54 68 65 20 2a 70 4f 66 66 73 65 74 0a  e. The *pOffset.
12080 2a 2a 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72  ** value is incr
12081 65 61 73 65 64 20 74 6f 20 74 68 65 20 73 74 61  eased to the sta
12082 72 74 20 6f 66 20 74 68 65 20 6e 65 78 74 20 70  rt of the next p
12083 61 67 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  age in the journ
12084 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73  al..**.** The is
12085 4d 61 69 6e 4a 72 6e 6c 20 66 6c 61 67 20 69 73  MainJrnl flag is
12086 20 74 72 75 65 20 69 66 20 74 68 69 73 20 69 73   true if this is
12087 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61   the main rollba
12088 63 6b 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 0a 2a  ck journal and.*
12089 2a 20 66 61 6c 73 65 20 66 6f 72 20 74 68 65 20  * false for the 
1208a 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61  statement journa
1208b 6c 2e 20 20 54 68 65 20 6d 61 69 6e 20 72 6f 6c  l.  The main rol
1208c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 75 73  lback journal us
1208d 65 73 0a 2a 2a 20 63 68 65 63 6b 73 75 6d 73 20  es.** checksums 
1208e 2d 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  - the statement 
1208f 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74  journal does not
12090 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70  ..**.** If the p
12091 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  age number of th
12092 65 20 70 61 67 65 20 72 65 63 6f 72 64 20 72 65  e page record re
12093 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62  ad from the (sub
12094 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a  -)journal file.*
12095 2a 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  * is greater tha
12096 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61  n the current va
12097 6c 75 65 20 6f 66 20 50 61 67 65 72 2e 64 62 53  lue of Pager.dbS
12098 69 7a 65 2c 20 74 68 65 6e 20 70 6c 61 79 62 61  ize, then playba
12099 63 6b 20 69 73 0a 2a 2a 20 73 6b 69 70 70 65 64  ck is.** skipped
1209a 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69   and SQLITE_OK i
1209b 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
1209c 2a 20 49 66 20 70 44 6f 6e 65 20 69 73 20 6e 6f  * If pDone is no
1209d 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20  t NULL, then it 
1209e 69 73 20 61 20 72 65 63 6f 72 64 20 6f 66 20 70  is a record of p
1209f 61 67 65 73 20 74 68 61 74 20 68 61 76 65 20 61  ages that have a
120a0 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65 6e 20 70  lready.** been p
120a1 6c 61 79 65 64 20 62 61 63 6b 2e 20 20 49 66 20  layed back.  If 
120a2 74 68 65 20 70 61 67 65 20 61 74 20 2a 70 4f 66  the page at *pOf
120a3 66 73 65 74 20 68 61 73 20 61 6c 72 65 61 64 79  fset has already
120a4 20 62 65 65 6e 20 70 6c 61 79 65 64 20 62 61 63   been played bac
120a5 6b 0a 2a 2a 20 28 69 66 20 74 68 65 20 63 6f 72  k.** (if the cor
120a6 72 65 73 70 6f 6e 64 69 6e 67 20 70 44 6f 6e 65  responding pDone
120a7 20 62 69 74 20 69 73 20 73 65 74 29 20 74 68 65   bit is set) the
120a8 6e 20 73 6b 69 70 20 74 68 65 20 70 6c 61 79 62  n skip the playb
120a9 61 63 6b 2e 0a 2a 2a 20 4d 61 6b 65 20 73 75 72  ack..** Make sur
120aa 65 20 74 68 65 20 70 44 6f 6e 65 20 62 69 74 20  e the pDone bit 
120ab 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
120ac 20 74 68 65 20 2a 70 4f 66 66 73 65 74 20 70 61   the *pOffset pa
120ad 67 65 20 69 73 20 73 65 74 0a 2a 2a 20 70 72 69  ge is set.** pri
120ae 6f 72 20 74 6f 20 72 65 74 75 72 6e 69 6e 67 2e  or to returning.
120af 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61  .**.** If the pa
120b0 67 65 20 72 65 63 6f 72 64 20 69 73 20 73 75 63  ge record is suc
120b1 63 65 73 73 66 75 6c 6c 79 20 72 65 61 64 20 66  cessfully read f
120b2 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f  rom the (sub-)jo
120b3 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 61 6e  urnal file.** an
120b4 64 20 70 6c 61 79 65 64 20 62 61 63 6b 2c 20 74  d played back, t
120b5 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  hen SQLITE_OK is
120b6 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e   returned. If an
120b7 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73   IO error occurs
120b8 0a 2a 2a 20 77 68 69 6c 65 20 72 65 61 64 69 6e  .** while readin
120b9 67 20 74 68 65 20 72 65 63 6f 72 64 20 66 72 6f  g the record fro
120ba 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72  m the (sub-)jour
120bb 6e 61 6c 20 66 69 6c 65 20 6f 72 20 77 68 69 6c  nal file or whil
120bc 65 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 6f 20  e writing.** to 
120bd 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
120be 65 2c 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65  e, then the IO e
120bf 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
120c0 75 72 6e 65 64 2e 20 49 66 20 64 61 74 61 0a 2a  urned. If data.*
120c1 2a 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c  * is successfull
120c2 79 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  y read from the 
120c3 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69  (sub-)journal fi
120c4 6c 65 20 62 75 74 20 61 70 70 65 61 72 73 20 74  le but appears t
120c5 6f 20 62 65 0a 2a 2a 20 63 6f 72 72 75 70 74 65  o be.** corrupte
120c6 64 2c 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69  d, SQLITE_DONE i
120c7 73 20 72 65 74 75 72 6e 65 64 2e 20 44 61 74 61  s returned. Data
120c8 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 63   is considered c
120c9 6f 72 72 75 70 74 65 64 20 69 6e 0a 2a 2a 20 74  orrupted in.** t
120ca 77 6f 20 63 69 72 63 75 6d 73 74 61 6e 63 65 73  wo circumstances
120cb 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 2a 20 49 66 20  :.** .**   * If 
120cc 74 68 65 20 72 65 63 6f 72 64 20 70 61 67 65 2d  the record page-
120cd 6e 75 6d 62 65 72 20 69 73 20 69 6c 6c 65 67 61  number is illega
120ce 6c 20 28 30 20 6f 72 20 50 41 47 45 52 5f 4d 4a  l (0 or PAGER_MJ
120cf 5f 50 47 4e 4f 29 2c 20 6f 72 0a 2a 2a 20 20 20  _PGNO), or.**   
120d0 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20  * If the record 
120d1 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20  is being rolled 
120d2 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61  back from the ma
120d3 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a  in journal file.
120d4 2a 2a 20 20 20 20 20 61 6e 64 20 74 68 65 20 63  **     and the c
120d5 68 65 63 6b 73 75 6d 20 66 69 65 6c 64 20 64 6f  hecksum field do
120d6 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65  es not match the
120d7 20 72 65 63 6f 72 64 20 63 6f 6e 74 65 6e 74 2e   record content.
120d8 0a 2a 2a 0a 2a 2a 20 4e 65 69 74 68 65 72 20 6f  .**.** Neither o
120d9 66 20 74 68 65 73 65 20 74 77 6f 20 73 63 65 6e  f these two scen
120da 61 72 69 6f 73 20 61 72 65 20 70 6f 73 73 69 62  arios are possib
120db 6c 65 20 64 75 72 69 6e 67 20 61 20 73 61 76 65  le during a save
120dc 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 2e 0a  point rollback..
120dd 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73  **.** If this is
120de 20 61 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c   a savepoint rol
120df 6c 62 61 63 6b 2c 20 74 68 65 6e 20 6d 65 6d 6f  lback, then memo
120e0 72 79 20 6d 61 79 20 68 61 76 65 20 74 6f 20 62  ry may have to b
120e1 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a  e dynamically.**
120e2 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68   allocated by th
120e3 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20  is function. If 
120e4 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65  this is the case
120e5 20 61 6e 64 20 61 6e 20 61 6c 6c 6f 63 61 74 69   and an allocati
120e6 6f 6e 20 66 61 69 6c 73 2c 0a 2a 2a 20 53 51 4c  on fails,.** SQL
120e7 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74  ITE_NOMEM is ret
120e8 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  urned..*/.static
120e9 20 69 6e 74 20 70 61 67 65 72 5f 70 6c 61 79 62   int pager_playb
120ea 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 0a 20 20  ack_one_page(.  
120eb 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20  Pager *pPager,  
120ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
120ed 20 54 68 65 20 70 61 67 65 72 20 62 65 69 6e 67   The pager being
120ee 20 70 6c 61 79 65 64 20 62 61 63 6b 20 2a 2f 0a   played back */.
120ef 20 20 69 6e 74 20 69 73 4d 61 69 6e 4a 72 6e 6c    int isMainJrnl
120f0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
120f1 2f 2a 20 31 20 2d 3e 20 6d 61 69 6e 20 6a 6f 75  /* 1 -> main jou
120f2 72 6e 61 6c 2e 20 30 20 2d 3e 20 73 75 62 2d 6a  rnal. 0 -> sub-j
120f3 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 69 6e 74  ournal. */.  int
120f4 20 69 73 55 6e 73 79 6e 63 2c 20 20 20 20 20 20   isUnsync,      
120f5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
120f6 75 65 20 69 66 20 72 65 61 64 69 6e 67 20 66 72  ue if reading fr
120f7 6f 6d 20 75 6e 73 79 6e 63 65 64 20 6d 61 69 6e  om unsynced main
120f8 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36   journal */.  i6
120f9 34 20 2a 70 4f 66 66 73 65 74 2c 20 20 20 20 20  4 *pOffset,     
120fa 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
120fb 66 66 73 65 74 20 6f 66 20 72 65 63 6f 72 64 20  ffset of record 
120fc 74 6f 20 70 6c 61 79 62 61 63 6b 20 2a 2f 0a 20  to playback */. 
120fd 20 69 6e 74 20 69 73 53 61 76 65 70 6e 74 2c 20   int isSavepnt, 
120fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
120ff 2a 20 54 72 75 65 20 66 6f 72 20 61 20 73 61 76  * True for a sav
12100 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 20  epoint rollback 
12101 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 44 6f  */.  Bitvec *pDo
12102 6e 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ne              
12103 20 20 20 2f 2a 20 42 69 74 76 65 63 20 6f 66 20     /* Bitvec of 
12104 70 61 67 65 73 20 61 6c 72 65 61 64 79 20 70 6c  pages already pl
12105 61 79 65 64 20 62 61 63 6b 20 2a 2f 0a 29 7b 0a  ayed back */.){.
12106 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67 48 64    int rc;.  PgHd
12107 72 20 2a 70 50 67 3b 20 20 20 20 20 20 20 20 20  r *pPg;         
12108 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20            /* An 
12109 65 78 69 73 74 69 6e 67 20 70 61 67 65 20 69 6e  existing page in
1210a 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 20 20   the cache */.  
1210b 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20  Pgno pgno;      
1210c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1210d 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   The page number
1210e 20 6f 66 20 61 20 70 61 67 65 20 69 6e 20 6a 6f   of a page in jo
1210f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 63  urnal */.  u32 c
12110 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20  ksum;           
12111 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63           /* Chec
12112 6b 73 75 6d 20 75 73 65 64 20 66 6f 72 20 73 61  ksum used for sa
12113 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f  nity checking */
12114 0a 20 20 75 38 20 2a 61 44 61 74 61 3b 20 20 20  .  u8 *aData;   
12115 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12116 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 73 74   /* Temporary st
12117 6f 72 61 67 65 20 66 6f 72 20 74 68 65 20 70 61  orage for the pa
12118 67 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ge */.  sqlite3_
12119 66 69 6c 65 20 2a 6a 66 64 3b 20 20 20 20 20 20  file *jfd;      
1211a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c        /* The fil
1211b 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72  e descriptor for
1211c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
1211d 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  e */..  assert( 
1211e 28 69 73 4d 61 69 6e 4a 72 6e 6c 26 7e 31 29 3d  (isMainJrnl&~1)=
1211f 3d 30 20 29 3b 20 20 20 20 20 20 2f 2a 20 69 73  =0 );      /* is
12120 4d 61 69 6e 4a 72 6e 6c 20 69 73 20 30 20 6f 72  MainJrnl is 0 or
12121 20 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20   1 */.  assert( 
12122 28 69 73 53 61 76 65 70 6e 74 26 7e 31 29 3d 3d  (isSavepnt&~1)==
12123 30 20 29 3b 20 20 20 20 20 20 20 2f 2a 20 69 73  0 );       /* is
12124 53 61 76 65 70 6e 74 20 69 73 20 30 20 6f 72 20  Savepnt is 0 or 
12125 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69  1 */.  assert( i
12126 73 4d 61 69 6e 4a 72 6e 6c 20 7c 7c 20 70 44 6f  sMainJrnl || pDo
12127 6e 65 20 29 3b 20 20 20 20 20 2f 2a 20 70 44 6f  ne );     /* pDo
12128 6e 65 20 61 6c 77 61 79 73 20 75 73 65 64 20 6f  ne always used o
12129 6e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a  n sub-journals *
1212a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 53 61  /.  assert( isSa
1212b 76 65 70 6e 74 20 7c 7c 20 70 44 6f 6e 65 3d 3d  vepnt || pDone==
1212c 30 20 29 3b 20 20 20 2f 2a 20 70 44 6f 6e 65 20  0 );   /* pDone 
1212d 6e 65 76 65 72 20 75 73 65 64 20 6f 6e 20 6e 6f  never used on no
1212e 6e 2d 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 0a  n-savepoint */..
1212f 20 20 61 44 61 74 61 20 3d 20 28 75 38 2a 29 70    aData = (u8*)p
12130 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65  Pager->pTmpSpace
12131 3b 0a 20 20 61 73 73 65 72 74 28 20 61 44 61 74  ;.  assert( aDat
12132 61 20 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  a );         /* 
12133 54 65 6d 70 20 73 74 6f 72 61 67 65 20 6d 75 73  Temp storage mus
12134 74 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62  t have already b
12135 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f  een allocated */
12136 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20  ..  /* Read the 
12137 70 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 20  page number and 
12138 70 61 67 65 20 64 61 74 61 20 66 72 6f 6d 20 74  page data from t
12139 68 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 73 75  he journal or su
1213a 62 2d 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66  b-journal.  ** f
1213b 69 6c 65 2e 20 52 65 74 75 72 6e 20 61 6e 20 65  ile. Return an e
1213c 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 74 68 65  rror code to the
1213d 20 63 61 6c 6c 65 72 20 69 66 20 61 6e 20 49 4f   caller if an IO
1213e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 20   error occurs.. 
1213f 20 2a 2f 0a 20 20 6a 66 64 20 3d 20 69 73 4d 61   */.  jfd = isMa
12140 69 6e 4a 72 6e 6c 20 3f 20 70 50 61 67 65 72 2d  inJrnl ? pPager-
12141 3e 6a 66 64 20 3a 20 70 50 61 67 65 72 2d 3e 73  >jfd : pPager->s
12142 6a 66 64 3b 0a 20 20 72 63 20 3d 20 72 65 61 64  jfd;.  rc = read
12143 33 32 62 69 74 73 28 6a 66 64 2c 20 2a 70 4f 66  32bits(jfd, *pOf
12144 66 73 65 74 2c 20 26 70 67 6e 6f 29 3b 0a 20 20  fset, &pgno);.  
12145 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
12146 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
12147 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52   rc = sqlite3OsR
12148 65 61 64 28 6a 66 64 2c 20 61 44 61 74 61 2c 20  ead(jfd, aData, 
12149 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
1214a 2c 20 28 2a 70 4f 66 66 73 65 74 29 2b 34 29 3b  , (*pOffset)+4);
1214b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
1214c 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
1214d 3b 0a 20 20 2a 70 4f 66 66 73 65 74 20 2b 3d 20  ;.  *pOffset += 
1214e 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
1214f 20 2b 20 34 20 2b 20 69 73 4d 61 69 6e 4a 72 6e   + 4 + isMainJrn
12150 6c 2a 34 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69 74  l*4;..  /* Sanit
12151 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74 68  y checking on th
12152 65 20 70 61 67 65 2e 20 20 54 68 69 73 20 69 73  e page.  This is
12153 20 6d 6f 72 65 20 69 6d 70 6f 72 74 61 6e 74 20   more important 
12154 74 68 61 74 20 49 20 6f 72 69 67 69 6e 61 6c 6c  that I originall
12155 79 0a 20 20 2a 2a 20 74 68 6f 75 67 68 74 2e 20  y.  ** thought. 
12156 20 49 66 20 61 20 70 6f 77 65 72 20 66 61 69 6c   If a power fail
12157 75 72 65 20 6f 63 63 75 72 73 20 77 68 69 6c 65  ure occurs while
12158 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   the journal is 
12159 62 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 0a 20  being written,. 
1215a 20 2a 2a 20 69 74 20 63 6f 75 6c 64 20 63 61 75   ** it could cau
1215b 73 65 20 69 6e 76 61 6c 69 64 20 64 61 74 61 20  se invalid data 
1215c 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e  to be written in
1215d 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20  to the journal. 
1215e 20 57 65 20 6e 65 65 64 20 74 6f 0a 20 20 2a 2a   We need to.  **
1215f 20 64 65 74 65 63 74 20 74 68 69 73 20 69 6e 76   detect this inv
12160 61 6c 69 64 20 64 61 74 61 20 28 77 69 74 68 20  alid data (with 
12161 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69 74 79  high probability
12162 29 20 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e  ) and ignore it.
12163 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 67 6e 6f  .  */.  if( pgno
12164 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47  ==0 || pgno==PAG
12165 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65  ER_MJ_PGNO(pPage
12166 72 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  r) ){.    assert
12167 28 20 21 69 73 53 61 76 65 70 6e 74 20 29 3b 0a  ( !isSavepnt );.
12168 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
12169 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 66  E_DONE;.  }.  if
1216a 28 20 70 67 6e 6f 3e 28 50 67 6e 6f 29 70 50 61  ( pgno>(Pgno)pPa
1216b 67 65 72 2d 3e 64 62 53 69 7a 65 20 7c 7c 20 73  ger->dbSize || s
1216c 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74  qlite3BitvecTest
1216d 28 70 44 6f 6e 65 2c 20 70 67 6e 6f 29 20 29 7b  (pDone, pgno) ){
1216e 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1216f 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28  TE_OK;.  }.  if(
12170 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 29 7b 0a 20   isMainJrnl ){. 
12171 20 20 20 72 63 20 3d 20 72 65 61 64 33 32 62 69     rc = read32bi
12172 74 73 28 6a 66 64 2c 20 28 2a 70 4f 66 66 73 65  ts(jfd, (*pOffse
12173 74 29 2d 34 2c 20 26 63 6b 73 75 6d 29 3b 0a 20  t)-4, &cksum);. 
12174 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
12175 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 20 21  rn rc;.    if( !
12176 69 73 53 61 76 65 70 6e 74 20 26 26 20 70 61 67  isSavepnt && pag
12177 65 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 2c  er_cksum(pPager,
12178 20 61 44 61 74 61 29 21 3d 63 6b 73 75 6d 20 29   aData)!=cksum )
12179 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
1217a 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20  QLITE_DONE;.    
1217b 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 44 6f  }.  }..  if( pDo
1217c 6e 65 20 26 26 20 28 72 63 20 3d 20 73 71 6c 69  ne && (rc = sqli
1217d 74 65 33 42 69 74 76 65 63 53 65 74 28 70 44 6f  te3BitvecSet(pDo
1217e 6e 65 2c 20 70 67 6e 6f 29 29 21 3d 53 51 4c 49  ne, pgno))!=SQLI
1217f 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
12180 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 61  urn rc;.  }..  a
12181 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
12182 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45  tate==PAGER_RESE
12183 52 56 45 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e  RVED || pPager->
12184 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43  state>=PAGER_EXC
12185 4c 55 53 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20  LUSIVE );..  /* 
12186 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20  If the pager is 
12187 69 6e 20 52 45 53 45 52 56 45 44 20 73 74 61 74  in RESERVED stat
12188 65 2c 20 74 68 65 6e 20 74 68 65 72 65 20 6d 75  e, then there mu
12189 73 74 20 62 65 20 61 20 63 6f 70 79 20 6f 66 20  st be a copy of 
1218a 74 68 69 73 0a 20 20 2a 2a 20 70 61 67 65 20 69  this.  ** page i
1218b 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68  n the pager cach
1218c 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  e. In this case 
1218d 6a 75 73 74 20 75 70 64 61 74 65 20 74 68 65 20  just update the 
1218e 70 61 67 65 72 20 63 61 63 68 65 2c 0a 20 20 2a  pager cache,.  *
1218f 2a 20 6e 6f 74 20 74 68 65 20 64 61 74 61 62 61  * not the databa
12190 73 65 20 66 69 6c 65 2e 20 54 68 65 20 70 61 67  se file. The pag
12191 65 20 69 73 20 6c 65 66 74 20 6d 61 72 6b 65 64  e is left marked
12192 20 64 69 72 74 79 20 69 6e 20 74 68 69 73 20 63   dirty in this c
12193 61 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41  ase..  **.  ** A
12194 6e 20 65 78 63 65 70 74 69 6f 6e 20 74 6f 20 74  n exception to t
12195 68 65 20 61 62 6f 76 65 20 72 75 6c 65 3a 20 49  he above rule: I
12196 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  f the database i
12197 73 20 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64  s in no-sync mod
12198 65 0a 20 20 2a 2a 20 61 6e 64 20 61 20 70 61 67  e.  ** and a pag
12199 65 20 69 73 20 6d 6f 76 65 64 20 64 75 72 69 6e  e is moved durin
1219a 67 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c  g an incremental
1219b 20 76 61 63 75 75 6d 20 74 68 65 6e 20 74 68 65   vacuum then the
1219c 20 70 61 67 65 20 6d 61 79 0a 20 20 2a 2a 20 6e   page may.  ** n
1219d 6f 74 20 62 65 20 69 6e 20 74 68 65 20 70 61 67  ot be in the pag
1219e 65 72 20 63 61 63 68 65 2e 20 4c 61 74 65 72 3a  er cache. Later:
1219f 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f   if a malloc() o
121a0 72 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72  r IO error occur
121a1 73 0a 20 20 2a 2a 20 64 75 72 69 6e 67 20 61 20  s.  ** during a 
121a2 4d 6f 76 65 70 61 67 65 28 29 20 63 61 6c 6c 2c  Movepage() call,
121a3 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d   then the page m
121a4 61 79 20 6e 6f 74 20 62 65 20 69 6e 20 74 68 65  ay not be in the
121a5 20 63 61 63 68 65 0a 20 20 2a 2a 20 65 69 74 68   cache.  ** eith
121a6 65 72 2e 20 53 6f 20 74 68 65 20 63 6f 6e 64 69  er. So the condi
121a7 74 69 6f 6e 20 64 65 73 63 72 69 62 65 64 20 69  tion described i
121a8 6e 20 74 68 65 20 61 62 6f 76 65 20 70 61 72 61  n the above para
121a9 67 72 61 70 68 20 69 73 20 6e 6f 74 0a 20 20 2a  graph is not.  *
121aa 2a 20 61 73 73 65 72 74 28 29 61 62 6c 65 2e 0a  * assert()able..
121ab 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 69 6e 20    **.  ** If in 
121ac 45 58 43 4c 55 53 49 56 45 20 73 74 61 74 65 2c  EXCLUSIVE state,
121ad 20 74 68 65 6e 20 77 65 20 75 70 64 61 74 65 20   then we update 
121ae 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20  the pager cache 
121af 69 66 20 69 74 20 65 78 69 73 74 73 0a 20 20 2a  if it exists.  *
121b0 2a 20 61 6e 64 20 74 68 65 20 6d 61 69 6e 20 66  * and the main f
121b1 69 6c 65 2e 20 54 68 65 20 70 61 67 65 20 69 73  ile. The page is
121b2 20 74 68 65 6e 20 6d 61 72 6b 65 64 20 6e 6f 74   then marked not
121b3 20 64 69 72 74 79 2e 0a 20 20 2a 2a 0a 20 20 2a   dirty..  **.  *
121b4 2a 20 54 69 63 6b 65 74 20 23 31 31 37 31 3a 20  * Ticket #1171: 
121b5 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a   The statement j
121b6 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e  ournal might con
121b7 74 61 69 6e 20 70 61 67 65 20 63 6f 6e 74 65 6e  tain page conten
121b8 74 20 74 68 61 74 20 69 73 0a 20 20 2a 2a 20 64  t that is.  ** d
121b9 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68  ifferent from th
121ba 65 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 61  e page content a
121bb 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  t the start of t
121bc 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a  he transaction..
121bd 20 20 2a 2a 20 54 68 69 73 20 6f 63 63 75 72 73    ** This occurs
121be 20 77 68 65 6e 20 61 20 70 61 67 65 20 69 73 20   when a page is 
121bf 63 68 61 6e 67 65 64 20 70 72 69 6f 72 20 74 6f  changed prior to
121c0 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 61 20   the start of a 
121c1 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 74  statement.  ** t
121c2 68 65 6e 20 63 68 61 6e 67 65 64 20 61 67 61 69  hen changed agai
121c3 6e 20 77 69 74 68 69 6e 20 74 68 65 20 73 74 61  n within the sta
121c4 74 65 6d 65 6e 74 2e 20 20 57 68 65 6e 20 72 6f  tement.  When ro
121c5 6c 6c 69 6e 67 20 62 61 63 6b 20 73 75 63 68 20  lling back such 
121c6 61 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74  a.  ** statement
121c7 20 77 65 20 6d 75 73 74 20 6e 6f 74 20 77 72 69   we must not wri
121c8 74 65 20 74 6f 20 74 68 65 20 6f 72 69 67 69 6e  te to the origin
121c9 61 6c 20 64 61 74 61 62 61 73 65 20 75 6e 6c 65  al database unle
121ca 73 73 20 77 65 20 6b 6e 6f 77 0a 20 20 2a 2a 20  ss we know.  ** 
121cb 66 6f 72 20 63 65 72 74 61 69 6e 20 74 68 61 74  for certain that
121cc 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 20 63   original page c
121cd 6f 6e 74 65 6e 74 73 20 61 72 65 20 73 79 6e 63  ontents are sync
121ce 65 64 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e  ed into the main
121cf 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a   rollback.  ** j
121d0 6f 75 72 6e 61 6c 2e 20 20 4f 74 68 65 72 77 69  ournal.  Otherwi
121d1 73 65 2c 20 61 20 70 6f 77 65 72 20 6c 6f 73 73  se, a power loss
121d2 20 6d 69 67 68 74 20 6c 65 61 76 65 20 6d 6f 64   might leave mod
121d3 69 66 69 65 64 20 64 61 74 61 20 69 6e 20 74 68  ified data in th
121d4 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20  e.  ** database 
121d5 66 69 6c 65 20 77 69 74 68 6f 75 74 20 61 6e 20  file without an 
121d6 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72 6f 6c  entry in the rol
121d7 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 68  lback journal th
121d8 61 74 20 63 61 6e 0a 20 20 2a 2a 20 72 65 73 74  at can.  ** rest
121d9 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ore the database
121da 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c   to its original
121db 20 66 6f 72 6d 2e 20 20 54 77 6f 20 63 6f 6e 64   form.  Two cond
121dc 69 74 69 6f 6e 73 20 6d 75 73 74 20 62 65 0a 20  itions must be. 
121dd 20 2a 2a 20 6d 65 74 20 62 65 66 6f 72 65 20 77   ** met before w
121de 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61  riting to the da
121df 74 61 62 61 73 65 20 66 69 6c 65 73 2e 20 28 31  tabase files. (1
121e0 29 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d  ) the database m
121e1 75 73 74 20 62 65 0a 20 20 2a 2a 20 6c 6f 63 6b  ust be.  ** lock
121e2 65 64 2e 20 20 28 32 29 20 77 65 20 6b 6e 6f 77  ed.  (2) we know
121e3 20 74 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e   that the origin
121e4 61 6c 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20  al page content 
121e5 69 73 20 66 75 6c 6c 79 20 73 79 6e 63 65 64 0a  is fully synced.
121e6 20 20 2a 2a 20 69 6e 20 74 68 65 20 6d 61 69 6e    ** in the main
121e7 20 6a 6f 75 72 6e 61 6c 20 65 69 74 68 65 72 20   journal either 
121e8 62 65 63 61 75 73 65 20 74 68 65 20 70 61 67 65  because the page
121e9 20 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65   is not in cache
121ea 20 6f 72 20 65 6c 73 65 0a 20 20 2a 2a 20 74 68   or else.  ** th
121eb 65 20 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64  e page is marked
121ec 20 61 73 20 6e 65 65 64 53 79 6e 63 3d 3d 30 2e   as needSync==0.
121ed 0a 20 20 2a 2a 0a 20 20 2a 2a 20 32 30 30 38 2d  .  **.  ** 2008-
121ee 30 34 2d 31 34 3a 20 20 57 68 65 6e 20 61 74 74  04-14:  When att
121ef 65 6d 70 74 69 6e 67 20 74 6f 20 76 61 63 75 75  empting to vacuu
121f0 6d 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61  m a corrupt data
121f1 62 61 73 65 20 66 69 6c 65 2c 20 69 74 0a 20 20  base file, it.  
121f2 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74  ** is possible t
121f3 6f 20 66 61 69 6c 20 61 20 73 74 61 74 65 6d 65  o fail a stateme
121f4 6e 74 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65  nt on a database
121f5 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 79   that does not y
121f6 65 74 20 65 78 69 73 74 2e 0a 20 20 2a 2a 20 44  et exist..  ** D
121f7 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f  o not attempt to
121f8 20 77 72 69 74 65 20 69 66 20 64 61 74 61 62 61   write if databa
121f9 73 65 20 66 69 6c 65 20 68 61 73 20 6e 65 76 65  se file has neve
121fa 72 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 20  r been opened.. 
121fb 20 2a 2f 0a 20 20 70 50 67 20 3d 20 70 61 67 65   */.  pPg = page
121fc 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c  r_lookup(pPager,
121fd 20 70 67 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74   pgno);.  assert
121fe 28 20 70 50 67 20 7c 7c 20 21 4d 45 4d 44 42 20  ( pPg || !MEMDB 
121ff 29 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 28  );.  PAGERTRACE(
12200 28 22 50 4c 41 59 42 41 43 4b 20 25 64 20 70 61  ("PLAYBACK %d pa
12201 67 65 20 25 64 20 68 61 73 68 28 25 30 38 78 29  ge %d hash(%08x)
12202 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20   %s\n",.        
12203 20 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70         PAGERID(p
12204 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61  Pager), pgno, pa
12205 67 65 72 5f 64 61 74 61 68 61 73 68 28 70 50 61  ger_datahash(pPa
12206 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 61  ger->pageSize, a
12207 44 61 74 61 29 2c 0a 20 20 20 20 20 20 20 20 20  Data),.         
12208 20 20 20 20 20 20 28 69 73 4d 61 69 6e 4a 72 6e        (isMainJrn
12209 6c 3f 22 6d 61 69 6e 2d 6a 6f 75 72 6e 61 6c 22  l?"main-journal"
1220a 3a 22 73 75 62 2d 6a 6f 75 72 6e 61 6c 22 29 0a  :"sub-journal").
1220b 20 20 29 29 3b 0a 20 20 69 66 28 20 28 70 50 61    ));.  if( (pPa
1220c 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
1220d 52 5f 45 58 43 4c 55 53 49 56 45 29 0a 20 20 20  R_EXCLUSIVE).   
1220e 26 26 20 28 70 50 67 3d 3d 30 20 7c 7c 20 30 3d  && (pPg==0 || 0=
1220f 3d 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48  =(pPg->flags&PGH
12210 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 29 0a 20  DR_NEED_SYNC)). 
12211 20 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67    && isOpen(pPag
12212 65 72 2d 3e 66 64 29 0a 20 20 20 26 26 20 21 69  er->fd).   && !i
12213 73 55 6e 73 79 6e 63 0a 20 20 29 7b 0a 20 20 20  sUnsync.  ){.   
12214 20 69 36 34 20 6f 66 73 74 20 3d 20 28 70 67 6e   i64 ofst = (pgn
12215 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65 72  o-1)*(i64)pPager
12216 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20  ->pageSize;.    
12217 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
12218 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ite(pPager->fd, 
12219 61 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70  aData, pPager->p
1221a 61 67 65 53 69 7a 65 2c 20 6f 66 73 74 29 3b 0a  ageSize, ofst);.
1221b 20 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 61      if( pgno>pPa
1221c 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20  ger->dbFileSize 
1221d 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  ){.      pPager-
1221e 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d 20 70 67  >dbFileSize = pg
1221f 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  no;.    }.    if
12220 28 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75  ( pPager->pBacku
12221 70 20 29 7b 0a 20 20 20 20 20 20 43 4f 44 45 43  p ){.      CODEC
12222 31 28 70 50 61 67 65 72 2c 20 61 44 61 74 61 2c  1(pPager, aData,
12223 20 70 67 6e 6f 2c 20 33 2c 20 72 63 3d 53 51 4c   pgno, 3, rc=SQL
12224 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20  ITE_NOMEM);.    
12225 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55    sqlite3BackupU
12226 70 64 61 74 65 28 70 50 61 67 65 72 2d 3e 70 42  pdate(pPager->pB
12227 61 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 61 44 61  ackup, pgno, aDa
12228 74 61 29 3b 0a 20 20 20 20 20 20 43 4f 44 45 43  ta);.      CODEC
12229 31 28 70 50 61 67 65 72 2c 20 61 44 61 74 61 2c  1(pPager, aData,
1222a 20 70 67 6e 6f 2c 20 30 2c 20 72 63 3d 53 51 4c   pgno, 0, rc=SQL
1222b 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20  ITE_NOMEM);.    
1222c 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 69  }.  }else if( !i
1222d 73 4d 61 69 6e 4a 72 6e 6c 20 26 26 20 70 50 67  sMainJrnl && pPg
1222e 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  ==0 ){.    /* If
1222f 20 74 68 69 73 20 69 73 20 61 20 72 6f 6c 6c 62   this is a rollb
12230 61 63 6b 20 6f 66 20 61 20 73 61 76 65 70 6f 69  ack of a savepoi
12231 6e 74 20 61 6e 64 20 64 61 74 61 20 77 61 73 20  nt and data was 
12232 6e 6f 74 20 77 72 69 74 74 65 6e 20 74 6f 0a 20  not written to. 
12233 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61     ** the databa
12234 73 65 20 61 6e 64 20 74 68 65 20 70 61 67 65 20  se and the page 
12235 69 73 20 6e 6f 74 20 69 6e 2d 6d 65 6d 6f 72 79  is not in-memory
12236 2c 20 74 68 65 72 65 20 69 73 20 61 20 70 6f 74  , there is a pot
12237 65 6e 74 69 61 6c 0a 20 20 20 20 2a 2a 20 70 72  ential.    ** pr
12238 6f 62 6c 65 6d 2e 20 57 68 65 6e 20 74 68 65 20  oblem. When the 
12239 70 61 67 65 20 69 73 20 6e 65 78 74 20 66 65 74  page is next fet
1223a 63 68 65 64 20 62 79 20 74 68 65 20 62 2d 74 72  ched by the b-tr
1223b 65 65 20 6c 61 79 65 72 2c 20 69 74 20 0a 20 20  ee layer, it .  
1223c 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 72 65 61    ** will be rea
1223d 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62  d from the datab
1223e 61 73 65 20 66 69 6c 65 2c 20 77 68 69 63 68 20  ase file, which 
1223f 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62  may or may not b
12240 65 20 0a 20 20 20 20 2a 2a 20 63 75 72 72 65 6e  e .    ** curren
12241 74 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  t. .    **.    *
12242 2a 20 54 68 65 72 65 20 61 72 65 20 61 20 63 6f  * There are a co
12243 75 70 6c 65 20 6f 66 20 64 69 66 66 65 72 65 6e  uple of differen
12244 74 20 77 61 79 73 20 74 68 69 73 20 63 61 6e 20  t ways this can 
12245 68 61 70 70 65 6e 2e 20 41 6c 6c 20 61 72 65 20  happen. All are 
12246 71 75 69 74 65 0a 20 20 20 20 2a 2a 20 6f 62 73  quite.    ** obs
12247 63 75 72 65 2e 20 57 68 65 6e 20 72 75 6e 6e 69  cure. When runni
12248 6e 67 20 69 6e 20 73 79 6e 63 68 72 6f 6e 6f 75  ng in synchronou
12249 73 20 6d 6f 64 65 2c 20 74 68 69 73 20 63 61 6e  s mode, this can
1224a 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 0a 20 20   only happen .  
1224b 20 20 2a 2a 20 69 66 20 74 68 65 20 70 61 67 65    ** if the page
1224c 20 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65 2d   is on the free-
1224d 6c 69 73 74 20 61 74 20 74 68 65 20 73 74 61 72  list at the star
1224e 74 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63  t of the transac
1224f 74 69 6f 6e 2c 20 74 68 65 6e 0a 20 20 20 20 2a  tion, then.    *
12250 2a 20 70 6f 70 75 6c 61 74 65 64 2c 20 74 68 65  * populated, the
12251 6e 20 6d 6f 76 65 64 20 75 73 69 6e 67 20 73 71  n moved using sq
12252 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61  lite3PagerMovepa
12253 67 65 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  ge()..    **.   
12254 20 2a 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f 6e   ** The solution
12255 20 69 73 20 74 6f 20 61 64 64 20 61 6e 20 69 6e   is to add an in
12256 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 74 6f 20  -memory page to 
12257 74 68 65 20 63 61 63 68 65 20 63 6f 6e 74 61 69  the cache contai
12258 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 74 68 65 20  ning.    ** the 
12259 64 61 74 61 20 6a 75 73 74 20 72 65 61 64 20 66  data just read f
1225a 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72  rom the sub-jour
1225b 6e 61 6c 2e 20 4d 61 72 6b 20 74 68 65 20 70 61  nal. Mark the pa
1225c 67 65 20 61 73 20 64 69 72 74 79 20 0a 20 20 20  ge as dirty .   
1225d 20 2a 2a 20 61 6e 64 20 69 66 20 74 68 65 20 70   ** and if the p
1225e 61 67 65 72 20 72 65 71 75 69 72 65 73 20 61 20  ager requires a 
1225f 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 2c 20 74 68  journal-sync, th
12260 65 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65  en mark the page
12261 20 61 73 20 0a 20 20 20 20 2a 2a 20 72 65 71 75   as .    ** requ
12262 69 72 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 2d  iring a journal-
12263 73 79 6e 63 20 62 65 66 6f 72 65 20 69 74 20 69  sync before it i
12264 73 20 77 72 69 74 74 65 6e 2e 0a 20 20 20 20 2a  s written..    *
12265 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73  /.    assert( is
12266 53 61 76 65 70 6e 74 20 29 3b 0a 20 20 20 20 69  Savepnt );.    i
12267 66 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33  f( (rc = sqlite3
12268 50 61 67 65 72 41 63 71 75 69 72 65 28 70 50 61  PagerAcquire(pPa
12269 67 65 72 2c 20 70 67 6e 6f 2c 20 26 70 50 67 2c  ger, pgno, &pPg,
1226a 20 31 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20   1))!=SQLITE_OK 
1226b 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1226c 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  rc;.    }.    pP
1226d 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48  g->flags &= ~PGH
1226e 44 52 5f 4e 45 45 44 5f 52 45 41 44 3b 0a 20 20  DR_NEED_READ;.  
1226f 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d    sqlite3PcacheM
12270 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a 20  akeDirty(pPg);. 
12271 20 7d 0a 20 20 69 66 28 20 70 50 67 20 29 7b 0a   }.  if( pPg ){.
12272 20 20 20 20 2f 2a 20 4e 6f 20 70 61 67 65 20 73      /* No page s
12273 68 6f 75 6c 64 20 65 76 65 72 20 62 65 20 65 78  hould ever be ex
12274 70 6c 69 63 69 74 6c 79 20 72 6f 6c 6c 65 64 20  plicitly rolled 
12275 62 61 63 6b 20 74 68 61 74 20 69 73 20 69 6e 20  back that is in 
12276 75 73 65 2c 20 65 78 63 65 70 74 0a 20 20 20 20  use, except.    
12277 2a 2a 20 66 6f 72 20 70 61 67 65 20 31 20 77 68  ** for page 1 wh
12278 69 63 68 20 69 73 20 68 65 6c 64 20 69 6e 20 75  ich is held in u
12279 73 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b  se in order to k
1227a 65 65 70 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20  eep the lock on 
1227b 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62  the.    ** datab
1227c 61 73 65 20 61 63 74 69 76 65 2e 20 48 6f 77 65  ase active. Howe
1227d 76 65 72 20 73 75 63 68 20 61 20 70 61 67 65 20  ver such a page 
1227e 6d 61 79 20 62 65 20 72 6f 6c 6c 65 64 20 62 61  may be rolled ba
1227f 63 6b 20 61 73 20 61 20 72 65 73 75 6c 74 0a 20  ck as a result. 
12280 20 20 20 2a 2a 20 6f 66 20 61 6e 20 69 6e 74 65     ** of an inte
12281 72 6e 61 6c 20 65 72 72 6f 72 20 72 65 73 75 6c  rnal error resul
12282 74 69 6e 67 20 69 6e 20 61 6e 20 61 75 74 6f 6d  ting in an autom
12283 61 74 69 63 20 63 61 6c 6c 20 74 6f 0a 20 20 20  atic call to.   
12284 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72   ** sqlite3Pager
12285 52 6f 6c 6c 62 61 63 6b 28 29 2e 0a 20 20 20 20  Rollback()..    
12286 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 44 61  */.    void *pDa
12287 74 61 3b 0a 20 20 20 20 70 44 61 74 61 20 3d 20  ta;.    pData = 
12288 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20  pPg->pData;.    
12289 6d 65 6d 63 70 79 28 70 44 61 74 61 2c 20 61 44  memcpy(pData, aD
1228a 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  ata, pPager->pag
1228b 65 53 69 7a 65 29 3b 0a 20 20 20 20 70 50 61 67  eSize);.    pPag
1228c 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 28 70 50  er->xReiniter(pP
1228d 67 29 3b 0a 20 20 20 20 69 66 28 20 69 73 4d 61  g);.    if( isMa
1228e 69 6e 4a 72 6e 6c 20 26 26 20 28 21 69 73 53 61  inJrnl && (!isSa
1228f 76 65 70 6e 74 20 7c 7c 20 2a 70 4f 66 66 73 65  vepnt || *pOffse
12290 74 3c 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  t<=pPager->journ
12291 61 6c 48 64 72 29 20 29 7b 0a 20 20 20 20 20 20  alHdr) ){.      
12292 2f 2a 20 49 66 20 74 68 65 20 63 6f 6e 74 65 6e  /* If the conten
12293 74 73 20 6f 66 20 74 68 69 73 20 70 61 67 65 20  ts of this page 
12294 77 65 72 65 20 6a 75 73 74 20 72 65 73 74 6f 72  were just restor
12295 65 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e  ed from the main
12296 20 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e   .      ** journ
12297 61 6c 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74  al file, then it
12298 73 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62  s content must b
12299 65 20 61 73 20 74 68 65 79 20 77 65 72 65 20 77  e as they were w
1229a 68 65 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a  hen the .      *
1229b 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61  * transaction wa
1229c 73 20 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20  s first opened. 
1229d 49 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20  In this case we 
1229e 63 61 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67  can mark the pag
1229f 65 0a 20 20 20 20 20 20 2a 2a 20 61 73 20 63 6c  e.      ** as cl
122a0 65 61 6e 2c 20 73 69 6e 63 65 20 74 68 65 72 65  ean, since there
122a1 20 77 69 6c 6c 20 62 65 20 6e 6f 20 6e 65 65 64   will be no need
122a2 20 74 6f 20 77 72 69 74 65 20 69 74 20 6f 75 74   to write it out
122a3 20 74 6f 20 74 68 65 2e 0a 20 20 20 20 20 20 2a   to the..      *
122a4 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65  *.      ** There
122a5 20 69 73 20 6f 6e 65 20 65 78 63 65 70 74 69 6f   is one exceptio
122a6 6e 20 74 6f 20 74 68 69 73 20 72 75 6c 65 2e 20  n to this rule. 
122a7 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 62  If the page is b
122a8 65 69 6e 67 20 72 6f 6c 6c 65 64 0a 20 20 20 20  eing rolled.    
122a9 20 20 2a 2a 20 62 61 63 6b 20 61 73 20 70 61 72    ** back as par
122aa 74 20 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74  t of a savepoint
122ab 20 28 6f 72 20 73 74 61 74 65 6d 65 6e 74 29 20   (or statement) 
122ac 72 6f 6c 6c 62 61 63 6b 20 66 72 6f 6d 20 61 6e  rollback from an
122ad 20 0a 20 20 20 20 20 20 2a 2a 20 75 6e 73 79 6e   .      ** unsyn
122ae 63 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74  ced portion of t
122af 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  he main journal 
122b0 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73  file, then it is
122b1 20 6e 6f 74 20 73 61 66 65 0a 20 20 20 20 20 20   not safe.      
122b2 2a 2a 20 74 6f 20 6d 61 72 6b 20 74 68 65 20 70  ** to mark the p
122b3 61 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 54 68  age as clean. Th
122b4 69 73 20 69 73 20 62 65 63 61 75 73 65 20 6d 61  is is because ma
122b5 72 6b 69 6e 67 20 74 68 65 20 70 61 67 65 20 61  rking the page a
122b6 73 0a 20 20 20 20 20 20 2a 2a 20 63 6c 65 61 6e  s.      ** clean
122b7 20 77 69 6c 6c 20 63 6c 65 61 72 20 74 68 65 20   will clear the 
122b8 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20  PGHDR_NEED_SYNC 
122b9 66 6c 61 67 2e 20 53 69 6e 63 65 20 74 68 65 20  flag. Since the 
122ba 70 61 67 65 20 69 73 0a 20 20 20 20 20 20 2a 2a  page is.      **
122bb 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
122bc 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 72 65  journal file (re
122bd 63 6f 72 64 65 64 20 69 6e 20 50 61 67 65 72 2e  corded in Pager.
122be 70 49 6e 4a 6f 75 72 6e 61 6c 29 20 61 6e 64 0a  pInJournal) and.
122bf 20 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48        ** the PGH
122c0 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61  DR_NEED_SYNC fla
122c1 67 20 69 73 20 63 6c 65 61 72 65 64 2c 20 69 66  g is cleared, if
122c2 20 74 68 65 20 70 61 67 65 20 69 73 20 77 72 69   the page is wri
122c3 74 74 65 6e 20 74 6f 0a 20 20 20 20 20 20 2a 2a  tten to.      **
122c4 20 61 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68   again within th
122c5 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  is transaction, 
122c6 69 74 20 77 69 6c 6c 20 62 65 20 6d 61 72 6b 65  it will be marke
122c7 64 20 61 73 20 64 69 72 74 79 20 62 75 74 0a 20  d as dirty but. 
122c8 20 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44       ** the PGHD
122c9 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67  R_NEED_SYNC flag
122ca 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 73 65 74   will not be set
122cb 2e 20 49 74 20 63 6f 75 6c 64 20 74 68 65 6e 20  . It could then 
122cc 70 6f 74 65 6e 74 69 61 6c 6c 79 0a 20 20 20 20  potentially.    
122cd 20 20 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20    ** be written 
122ce 6f 75 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74  out into the dat
122cf 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72  abase file befor
122d0 65 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69  e its journal fi
122d1 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73 65 67 6d  le.      ** segm
122d2 65 6e 74 20 69 73 20 73 79 6e 63 65 64 2e 20 49  ent is synced. I
122d3 66 20 61 20 63 72 61 73 68 20 6f 63 63 75 72 73  f a crash occurs
122d4 20 64 75 72 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f   during or follo
122d5 77 69 6e 67 20 74 68 69 73 2c 0a 20 20 20 20 20  wing this,.     
122d6 20 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 72   ** database cor
122d7 72 75 70 74 69 6f 6e 20 6d 61 79 20 65 6e 73 75  ruption may ensu
122d8 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
122d9 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d    sqlite3PcacheM
122da 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20  akeClean(pPg);. 
122db 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49     }.#ifdef SQLI
122dc 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20  TE_CHECK_PAGES. 
122dd 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68     pPg->pageHash
122de 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73   = pager_pagehas
122df 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20  h(pPg);.#endif. 
122e0 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61     /* If this wa
122e1 73 20 70 61 67 65 20 31 2c 20 74 68 65 6e 20 72  s page 1, then r
122e2 65 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65  estore the value
122e3 20 6f 66 20 50 61 67 65 72 2e 64 62 46 69 6c 65   of Pager.dbFile
122e4 56 65 72 73 2e 0a 20 20 20 20 2a 2a 20 44 6f 20  Vers..    ** Do 
122e5 74 68 69 73 20 62 65 66 6f 72 65 20 61 6e 79 20  this before any 
122e6 64 65 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20 20 20  decoding. */.   
122e7 20 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a   if( pgno==1 ){.
122e8 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50        memcpy(&pP
122e9 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73  ager->dbFileVers
122ea 2c 20 26 28 28 75 38 2a 29 70 44 61 74 61 29 5b  , &((u8*)pData)[
122eb 32 34 5d 2c 73 69 7a 65 6f 66 28 70 50 61 67 65  24],sizeof(pPage
122ec 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b  r->dbFileVers));
122ed 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44  .    }..    /* D
122ee 65 63 6f 64 65 20 74 68 65 20 70 61 67 65 20 6a  ecode the page j
122ef 75 73 74 20 72 65 61 64 20 66 72 6f 6d 20 64 69  ust read from di
122f0 73 6b 20 2a 2f 0a 20 20 20 20 43 4f 44 45 43 31  sk */.    CODEC1
122f1 28 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20  (pPager, pData, 
122f2 70 50 67 2d 3e 70 67 6e 6f 2c 20 33 2c 20 72 63  pPg->pgno, 3, rc
122f3 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a  =SQLITE_NOMEM);.
122f4 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68      sqlite3Pcach
122f5 65 52 65 6c 65 61 73 65 28 70 50 67 29 3b 0a 20  eRelease(pPg);. 
122f6 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
122f7 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74  }../*.** Paramet
122f8 65 72 20 7a 4d 61 73 74 65 72 20 69 73 20 74 68  er zMaster is th
122f9 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74  e name of a mast
122fa 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  er journal file.
122fb 20 41 20 73 69 6e 67 6c 65 20 6a 6f 75 72 6e 61   A single journa
122fc 6c 0a 2a 2a 20 66 69 6c 65 20 74 68 61 74 20 72  l.** file that r
122fd 65 66 65 72 72 65 64 20 74 6f 20 74 68 65 20 6d  eferred to the m
122fe 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
122ff 6c 65 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e  le has just been
12300 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a   rolled back..**
12301 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
12302 65 63 6b 73 20 69 66 20 69 74 20 69 73 20 70 6f  ecks if it is po
12303 73 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 65  ssible to delete
12304 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
12305 6e 61 6c 20 66 69 6c 65 2c 0a 2a 2a 20 61 6e 64  nal file,.** and
12306 20 64 6f 65 73 20 73 6f 20 69 66 20 69 74 20 69   does so if it i
12307 73 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e  s..**.** Argumen
12308 74 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 70 6f  t zMaster may po
12309 69 6e 74 20 74 6f 20 50 61 67 65 72 2e 70 54 6d  int to Pager.pTm
1230a 70 53 70 61 63 65 2e 20 53 6f 20 74 68 61 74 20  pSpace. So that 
1230b 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 0a 2a  buffer is not .*
1230c 2a 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20  * available for 
1230d 75 73 65 20 77 69 74 68 69 6e 20 74 68 69 73 20  use within this 
1230e 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
1230f 57 68 65 6e 20 61 20 6d 61 73 74 65 72 20 6a 6f  When a master jo
12310 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63 72  urnal file is cr
12311 65 61 74 65 64 2c 20 69 74 20 69 73 20 70 6f 70  eated, it is pop
12312 75 6c 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ulated with the 
12313 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66 20 61 6c 6c  names .** of all
12314 20 6f 66 20 69 74 73 20 63 68 69 6c 64 20 6a 6f   of its child jo
12315 75 72 6e 61 6c 73 2c 20 6f 6e 65 20 61 66 74 65  urnals, one afte
12316 72 20 61 6e 6f 74 68 65 72 2c 20 66 6f 72 6d 61  r another, forma
12317 74 74 65 64 20 61 73 20 75 74 66 2d 38 20 0a 2a  tted as utf-8 .*
12318 2a 20 65 6e 63 6f 64 65 64 20 74 65 78 74 2e 20  * encoded text. 
12319 54 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20  The end of each 
1231a 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 66 69  child journal fi
1231b 6c 65 20 69 73 20 6d 61 72 6b 65 64 20 77 69 74  le is marked wit
1231c 68 20 61 20 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d  h a .** nul-term
1231d 69 6e 61 74 6f 72 20 62 79 74 65 20 28 30 78 30  inator byte (0x0
1231e 30 29 2e 20 69 2e 65 2e 20 74 68 65 20 65 6e 74  0). i.e. the ent
1231f 69 72 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  ire contents of 
12320 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
12321 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 20 61 20 74  .** file for a t
12322 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 76 6f 6c  ransaction invol
12323 76 69 6e 67 20 74 77 6f 20 64 61 74 61 62 61 73  ving two databas
12324 65 73 20 6d 69 67 68 74 20 62 65 3a 0a 2a 2a 0a  es might be:.**.
12325 2a 2a 20 20 20 22 2f 68 6f 6d 65 2f 62 69 6c 6c  **   "/home/bill
12326 2f 61 2e 64 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30  /a.db-journal\x0
12327 30 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 62 2e 64 62  0/home/bill/b.db
12328 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 22 0a 2a 2a  -journal\x00".**
12329 0a 2a 2a 20 41 20 6d 61 73 74 65 72 20 6a 6f 75  .** A master jou
1232a 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 20 6f 6e  rnal file may on
1232b 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20 6f 6e  ly be deleted on
1232c 63 65 20 61 6c 6c 20 6f 66 20 69 74 73 20 63 68  ce all of its ch
1232d 69 6c 64 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 73  ild .** journals
1232e 20 68 61 76 65 20 62 65 65 6e 20 72 6f 6c 6c 65   have been rolle
1232f 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68  d back..**.** Th
12330 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 61 64  is function read
12331 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  s the contents o
12332 66 20 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75  f the master-jou
12333 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 0a  rnal file into .
12334 2a 2a 20 6d 65 6d 6f 72 79 20 61 6e 64 20 6c 6f  ** memory and lo
12335 6f 70 73 20 74 68 72 6f 75 67 68 20 65 61 63 68  ops through each
12336 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 6a 6f   of the child jo
12337 75 72 6e 61 6c 20 6e 61 6d 65 73 2e 20 46 6f 72  urnal names. For
12338 0a 2a 2a 20 65 61 63 68 20 63 68 69 6c 64 20 6a  .** each child j
12339 6f 75 72 6e 61 6c 2c 20 69 74 20 63 68 65 63 6b  ournal, it check
1233a 73 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  s if:.**.**   * 
1233b 69 66 20 74 68 65 20 63 68 69 6c 64 20 6a 6f 75  if the child jou
1233c 72 6e 61 6c 20 65 78 69 73 74 73 2c 20 61 6e 64  rnal exists, and
1233d 20 69 66 20 73 6f 0a 2a 2a 20 20 20 2a 20 69 66   if so.**   * if
1233e 20 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e   the child journ
1233f 61 6c 20 63 6f 6e 74 61 69 6e 73 20 61 20 72 65  al contains a re
12340 66 65 72 65 6e 63 65 20 74 6f 20 6d 61 73 74 65  ference to maste
12341 72 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 20 20  r journal .**   
12342 20 20 66 69 6c 65 20 7a 4d 61 73 74 65 72 0a 2a    file zMaster.*
12343 2a 0a 2a 2a 20 49 66 20 61 20 63 68 69 6c 64 20  *.** If a child 
12344 6a 6f 75 72 6e 61 6c 20 63 61 6e 20 62 65 20 66  journal can be f
12345 6f 75 6e 64 20 74 68 61 74 20 6d 61 74 63 68 65  ound that matche
12346 73 20 62 6f 74 68 20 6f 66 20 74 68 65 20 63 72  s both of the cr
12347 69 74 65 72 69 61 0a 2a 2a 20 61 62 6f 76 65 2c  iteria.** above,
12348 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
12349 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74 20 64  eturns without d
1234a 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20 4f  oing anything. O
1234b 74 68 65 72 77 69 73 65 2c 20 69 66 0a 2a 2a 20  therwise, if.** 
1234c 6e 6f 20 73 75 63 68 20 63 68 69 6c 64 20 6a 6f  no such child jo
1234d 75 72 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f 75  urnal can be fou
1234e 6e 64 2c 20 66 69 6c 65 20 7a 4d 61 73 74 65 72  nd, file zMaster
1234f 20 69 73 20 64 65 6c 65 74 65 64 20 66 72 6f 6d   is deleted from
12350 0a 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73 79 73  .** the file-sys
12351 74 65 6d 20 75 73 69 6e 67 20 73 71 6c 69 74 65  tem using sqlite
12352 33 4f 73 44 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a  3OsDelete()..**.
12353 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f  ** If an IO erro
12354 72 20 77 69 74 68 69 6e 20 74 68 69 73 20 66 75  r within this fu
12355 6e 63 74 69 6f 6e 2c 20 61 6e 20 65 72 72 6f 72  nction, an error
12356 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
12357 64 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74  d. This.** funct
12358 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 6d 65  ion allocates me
12359 6d 6f 72 79 20 62 79 20 63 61 6c 6c 69 6e 67 20  mory by calling 
1235a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e  sqlite3Malloc().
1235b 20 49 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f   If an allocatio
1235c 6e 0a 2a 2a 20 66 61 69 6c 73 2c 20 53 51 4c 49  n.** fails, SQLI
1235d 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75  TE_NOMEM is retu
1235e 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  rned. Otherwise,
1235f 20 69 66 20 6e 6f 20 49 4f 20 6f 72 20 6d 61 6c   if no IO or mal
12360 6c 6f 63 20 65 72 72 6f 72 73 20 0a 2a 2a 20 6f  loc errors .** o
12361 63 63 75 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  ccur, SQLITE_OK 
12362 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
12363 2a 2a 20 54 4f 44 4f 3a 20 54 68 69 73 20 66 75  ** TODO: This fu
12364 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73  nction allocates
12365 20 61 20 73 69 6e 67 6c 65 20 62 6c 6f 63 6b 20   a single block 
12366 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 6c 6f 61  of memory to loa
12367 64 0a 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20  d.** the entire 
12368 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
12369 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
1236a 69 6c 65 2e 20 54 68 69 73 20 63 6f 75 6c 64 20  ile. This could 
1236b 62 65 0a 2a 2a 20 61 20 63 6f 75 70 6c 65 20 6f  be.** a couple o
1236c 66 20 6b 69 6c 6f 62 79 74 65 73 20 6f 72 20 73  f kilobytes or s
1236d 6f 20 2d 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20  o - potentially 
1236e 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20  larger than the 
1236f 70 61 67 65 20 0a 2a 2a 20 73 69 7a 65 2e 0a 2a  page .** size..*
12370 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
12371 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 50 61 67  er_delmaster(Pag
12372 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73  er *pPager, cons
12373 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29  t char *zMaster)
12374 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  {.  sqlite3_vfs 
12375 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e  *pVfs = pPager->
12376 70 56 66 73 3b 0a 20 20 69 6e 74 20 72 63 3b 20  pVfs;.  int rc; 
12377 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12378 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
12379 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69   */.  sqlite3_fi
1237a 6c 65 20 2a 70 4d 61 73 74 65 72 3b 20 20 20 20  le *pMaster;    
1237b 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 6d 61 73 74  /* Malloc'd mast
1237c 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er-journal file 
1237d 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20  descriptor */.  
1237e 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
1237f 6f 75 72 6e 61 6c 3b 20 20 20 2f 2a 20 4d 61 6c  ournal;   /* Mal
12380 6c 6f 63 27 64 20 63 68 69 6c 64 2d 6a 6f 75 72  loc'd child-jour
12381 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70  nal file descrip
12382 74 6f 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  tor */.  char *z
12383 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 3d 20  MasterJournal = 
12384 30 3b 20 2f 2a 20 43 6f 6e 74 65 6e 74 73 20 6f  0; /* Contents o
12385 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  f master journal
12386 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6e   file */.  i64 n
12387 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 20 20  MasterJournal;  
12388 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
12389 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
1238a 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c  ile */..  /* All
1238b 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20  ocate space for 
1238c 62 6f 74 68 20 74 68 65 20 70 4a 6f 75 72 6e 61  both the pJourna
1238d 6c 20 61 6e 64 20 70 4d 61 73 74 65 72 20 66 69  l and pMaster fi
1238e 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 2e 0a  le descriptors..
1238f 20 20 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66    ** If successf
12390 75 6c 2c 20 6f 70 65 6e 20 74 68 65 20 6d 61 73  ul, open the mas
12391 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
12392 20 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a 20 20   for reading..  
12393 2a 2f 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 28  */.  pMaster = (
12394 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 73  sqlite3_file *)s
12395 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
12396 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20  (pVfs->szOsFile 
12397 2a 20 32 29 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c  * 2);.  pJournal
12398 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65   = (sqlite3_file
12399 20 2a 29 28 28 28 75 38 20 2a 29 70 4d 61 73 74   *)(((u8 *)pMast
1239a 65 72 29 20 2b 20 70 56 66 73 2d 3e 73 7a 4f 73  er) + pVfs->szOs
1239b 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 21 70 4d  File);.  if( !pM
1239c 61 73 74 65 72 20 29 7b 0a 20 20 20 20 72 63 20  aster ){.    rc 
1239d 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
1239e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e    }else{.    con
1239f 73 74 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28  st int flags = (
123a0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
123a1 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e  ONLY|SQLITE_OPEN
123a2 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 29  _MASTER_JOURNAL)
123a3 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
123a4 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a  e3OsOpen(pVfs, z
123a5 4d 61 73 74 65 72 2c 20 70 4d 61 73 74 65 72 2c  Master, pMaster,
123a6 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a   flags, 0);.  }.
123a7 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
123a8 5f 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61  _OK ) goto delma
123a9 73 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 72 63 20  ster_out;..  rc 
123aa 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53  = sqlite3OsFileS
123ab 69 7a 65 28 70 4d 61 73 74 65 72 2c 20 26 6e 4d  ize(pMaster, &nM
123ac 61 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20  asterJournal);. 
123ad 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
123ae 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73  OK ) goto delmas
123af 74 65 72 5f 6f 75 74 3b 0a 0a 20 20 69 66 28 20  ter_out;..  if( 
123b0 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3e 30  nMasterJournal>0
123b1 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4a   ){.    char *zJ
123b2 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 63 68 61 72  ournal;.    char
123b3 20 2a 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 30   *zMasterPtr = 0
123b4 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61 73 74 65  ;.    int nMaste
123b5 72 50 74 72 20 3d 20 70 56 66 73 2d 3e 6d 78 50  rPtr = pVfs->mxP
123b6 61 74 68 6e 61 6d 65 2b 31 3b 0a 0a 20 20 20 20  athname+1;..    
123b7 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 6e 74 69  /* Load the enti
123b8 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  re master journa
123b9 6c 20 66 69 6c 65 20 69 6e 74 6f 20 73 70 61 63  l file into spac
123ba 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a  e obtained from.
123bb 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6d      ** sqlite3_m
123bc 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 6f 69 6e  alloc() and poin
123bd 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 73 74 65  ted to by zMaste
123be 72 4a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20 2a  rJournal. .    *
123bf 2f 0a 20 20 20 20 7a 4d 61 73 74 65 72 4a 6f 75  /.    zMasterJou
123c0 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61  rnal = sqlite3Ma
123c1 6c 6c 6f 63 28 28 69 6e 74 29 6e 4d 61 73 74 65  lloc((int)nMaste
123c2 72 4a 6f 75 72 6e 61 6c 20 2b 20 6e 4d 61 73 74  rJournal + nMast
123c3 65 72 50 74 72 20 2b 20 31 29 3b 0a 20 20 20 20  erPtr + 1);.    
123c4 69 66 28 20 21 7a 4d 61 73 74 65 72 4a 6f 75 72  if( !zMasterJour
123c5 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20  nal ){.      rc 
123c6 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
123c7 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61        goto delma
123c8 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  ster_out;.    }.
123c9 20 20 20 20 7a 4d 61 73 74 65 72 50 74 72 20 3d      zMasterPtr =
123ca 20 26 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c   &zMasterJournal
123cb 5b 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2b  [nMasterJournal+
123cc 31 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  1];.    rc = sql
123cd 69 74 65 33 4f 73 52 65 61 64 28 70 4d 61 73 74  ite3OsRead(pMast
123ce 65 72 2c 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e  er, zMasterJourn
123cf 61 6c 2c 20 28 69 6e 74 29 6e 4d 61 73 74 65 72  al, (int)nMaster
123d0 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20  Journal, 0);.   
123d1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
123d2 4f 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73  OK ) goto delmas
123d3 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 7a 4d 61  ter_out;.    zMa
123d4 73 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73  sterJournal[nMas
123d5 74 65 72 4a 6f 75 72 6e 61 6c 5d 20 3d 20 30 3b  terJournal] = 0;
123d6 0a 0a 20 20 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d  ..    zJournal =
123d7 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b   zMasterJournal;
123d8 0a 20 20 20 20 77 68 69 6c 65 28 20 28 7a 4a 6f  .    while( (zJo
123d9 75 72 6e 61 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75  urnal-zMasterJou
123da 72 6e 61 6c 29 3c 6e 4d 61 73 74 65 72 4a 6f 75  rnal)<nMasterJou
123db 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 69 6e  rnal ){.      in
123dc 74 20 65 78 69 73 74 73 3b 0a 20 20 20 20 20 20  t exists;.      
123dd 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63  rc = sqlite3OsAc
123de 63 65 73 73 28 70 56 66 73 2c 20 7a 4a 6f 75 72  cess(pVfs, zJour
123df 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 45  nal, SQLITE_ACCE
123e0 53 53 5f 45 58 49 53 54 53 2c 20 26 65 78 69 73  SS_EXISTS, &exis
123e1 74 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ts);.      if( r
123e2 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
123e3 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c          goto del
123e4 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20  master_out;.    
123e5 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 65 78    }.      if( ex
123e6 69 73 74 73 20 29 7b 0a 20 20 20 20 20 20 20 20  ists ){.        
123e7 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 6a 6f  /* One of the jo
123e8 75 72 6e 61 6c 73 20 70 6f 69 6e 74 65 64 20 74  urnals pointed t
123e9 6f 20 62 79 20 74 68 65 20 6d 61 73 74 65 72 20  o by the master 
123ea 6a 6f 75 72 6e 61 6c 20 65 78 69 73 74 73 2e 0a  journal exists..
123eb 20 20 20 20 20 20 20 20 2a 2a 20 4f 70 65 6e 20          ** Open 
123ec 69 74 20 61 6e 64 20 63 68 65 63 6b 20 69 66 20  it and check if 
123ed 69 74 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65  it points at the
123ee 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e   master journal.
123ef 20 49 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 73   If.        ** s
123f0 6f 2c 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75  o, return withou
123f1 74 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6d  t deleting the m
123f2 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
123f3 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  le..        */. 
123f4 20 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 20         int c;.  
123f5 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20        int flags 
123f6 3d 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  = (SQLITE_OPEN_R
123f7 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f  EADONLY|SQLITE_O
123f8 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c  PEN_MAIN_JOURNAL
123f9 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
123fa 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56  sqlite3OsOpen(pV
123fb 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20 70 4a  fs, zJournal, pJ
123fc 6f 75 72 6e 61 6c 2c 20 66 6c 61 67 73 2c 20 30  ournal, flags, 0
123fd 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
123fe 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
123ff 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64            goto d
12400 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20  elmaster_out;.  
12401 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
12402 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72   rc = readMaster
12403 4a 6f 75 72 6e 61 6c 28 70 4a 6f 75 72 6e 61 6c  Journal(pJournal
12404 2c 20 7a 4d 61 73 74 65 72 50 74 72 2c 20 6e 4d  , zMasterPtr, nM
12405 61 73 74 65 72 50 74 72 29 3b 0a 20 20 20 20 20  asterPtr);.     
12406 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
12407 65 28 70 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20  e(pJournal);.   
12408 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
12409 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1240a 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74      goto delmast
1240b 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20  er_out;.        
1240c 7d 0a 0a 20 20 20 20 20 20 20 20 63 20 3d 20 7a  }..        c = z
1240d 4d 61 73 74 65 72 50 74 72 5b 30 5d 21 3d 30 20  MasterPtr[0]!=0 
1240e 26 26 20 73 74 72 63 6d 70 28 7a 4d 61 73 74 65  && strcmp(zMaste
1240f 72 50 74 72 2c 20 7a 4d 61 73 74 65 72 29 3d 3d  rPtr, zMaster)==
12410 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  0;.        if( c
12411 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
12412 20 57 65 20 68 61 76 65 20 61 20 6d 61 74 63 68   We have a match
12413 2e 20 44 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20  . Do not delete 
12414 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
12415 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20  al file. */.    
12416 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61        goto delma
12417 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20  ster_out;.      
12418 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
12419 20 20 7a 4a 6f 75 72 6e 61 6c 20 2b 3d 20 28 73    zJournal += (s
1241a 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
1241b 4a 6f 75 72 6e 61 6c 29 2b 31 29 3b 0a 20 20 20  Journal)+1);.   
1241c 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 72 63 20 3d   }.  }.  .  rc =
1241d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65   sqlite3OsDelete
1241e 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20  (pVfs, zMaster, 
1241f 30 29 3b 0a 0a 64 65 6c 6d 61 73 74 65 72 5f 6f  0);..delmaster_o
12420 75 74 3a 0a 20 20 69 66 28 20 7a 4d 61 73 74 65  ut:.  if( zMaste
12421 72 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20  rJournal ){.    
12422 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 4d 61  sqlite3_free(zMa
12423 73 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20  sterJournal);.  
12424 7d 20 20 0a 20 20 69 66 28 20 70 4d 61 73 74 65  }  .  if( pMaste
12425 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  r ){.    sqlite3
12426 4f 73 43 6c 6f 73 65 28 70 4d 61 73 74 65 72 29  OsClose(pMaster)
12427 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 69  ;.    assert( !i
12428 73 4f 70 65 6e 28 70 4a 6f 75 72 6e 61 6c 29 20  sOpen(pJournal) 
12429 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
1242a 5f 66 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a  _free(pMaster);.
1242b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1242c 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
1242d 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
1242e 63 68 61 6e 67 65 20 74 68 65 20 61 63 74 75 61  change the actua
1242f 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61  l size of the da
12430 74 61 62 61 73 65 20 0a 2a 2a 20 66 69 6c 65 20  tabase .** file 
12431 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74  in the file-syst
12432 65 6d 2e 20 54 68 69 73 20 6f 6e 6c 79 20 68 61  em. This only ha
12433 70 70 65 6e 73 20 77 68 65 6e 20 63 6f 6d 6d 69  ppens when commi
12434 74 74 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74  tting a transact
12435 69 6f 6e 2c 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 69  ion,.** or rolli
12436 6e 67 20 62 61 63 6b 20 61 20 74 72 61 6e 73 61  ng back a transa
12437 63 74 69 6f 6e 20 28 69 6e 63 6c 75 64 69 6e 67  ction (including
12438 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20   rolling back a 
12439 68 6f 74 2d 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2a  hot-journal)..**
1243a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 61 69 6e 20  .** If the main 
1243b 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73  database file is
1243c 20 6e 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 61 6e   not open, or an
1243d 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
1243e 69 73 20 6e 6f 74 0a 2a 2a 20 68 65 6c 64 2c 20  is not.** held, 
1243f 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
12440 20 61 20 6e 6f 2d 6f 70 2e 20 4f 74 68 65 72 77   a no-op. Otherw
12441 69 73 65 2c 20 74 68 65 20 73 69 7a 65 20 6f 66  ise, the size of
12442 20 74 68 65 20 66 69 6c 65 20 69 73 0a 2a 2a 20   the file is.** 
12443 63 68 61 6e 67 65 64 20 74 6f 20 6e 50 61 67 65  changed to nPage
12444 20 70 61 67 65 73 20 28 6e 50 61 67 65 2a 70 50   pages (nPage*pP
12445 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62  ager->pageSize b
12446 79 74 65 73 29 2e 20 49 66 20 74 68 65 20 66 69  ytes). If the fi
12447 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73  le.** on disk is
12448 20 63 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65   currently large
12449 72 20 74 68 61 6e 20 6e 50 61 67 65 20 70 61 67  r than nPage pag
1244a 65 73 2c 20 74 68 65 6e 20 75 73 65 20 74 68 65  es, then use the
1244b 20 56 46 53 0a 2a 2a 20 78 54 72 75 6e 63 61 74   VFS.** xTruncat
1244c 65 28 29 20 6d 65 74 68 6f 64 20 74 6f 20 74 72  e() method to tr
1244d 75 6e 63 61 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a  uncate it..**.**
1244e 20 4f 72 2c 20 69 74 20 6d 69 67 68 74 20 6d 69   Or, it might mi
1244f 67 68 74 20 62 65 20 74 68 65 20 63 61 73 65 20  ght be the case 
12450 74 68 61 74 20 74 68 65 20 66 69 6c 65 20 6f 6e  that the file on
12451 20 64 69 73 6b 20 69 73 20 73 6d 61 6c 6c 65 72   disk is smaller
12452 20 74 68 61 6e 20 0a 2a 2a 20 6e 50 61 67 65 20   than .** nPage 
12453 70 61 67 65 73 2e 20 53 6f 6d 65 20 6f 70 65 72  pages. Some oper
12454 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6d 70  ating system imp
12455 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 63 61 6e  lementations can
12456 20 67 65 74 20 63 6f 6e 66 75 73 65 64 20 69 66   get confused if
12457 20 0a 2a 2a 20 79 6f 75 20 74 72 79 20 74 6f 20   .** you try to 
12458 74 72 75 6e 63 61 74 65 20 61 20 66 69 6c 65 20  truncate a file 
12459 74 6f 20 73 6f 6d 65 20 73 69 7a 65 20 74 68 61  to some size tha
1245a 74 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  t is larger than
1245b 20 69 74 20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c   it .** currentl
1245c 79 20 69 73 2c 20 73 6f 20 64 65 74 65 63 74 20  y is, so detect 
1245d 74 68 69 73 20 63 61 73 65 20 61 6e 64 20 77 72  this case and wr
1245e 69 74 65 20 61 20 73 69 6e 67 6c 65 20 7a 65 72  ite a single zer
1245f 6f 20 62 79 74 65 20 74 6f 20 0a 2a 2a 20 74 68  o byte to .** th
12460 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6e 65 77  e end of the new
12461 20 66 69 6c 65 20 69 6e 73 74 65 61 64 2e 0a 2a   file instead..*
12462 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66  *.** If successf
12463 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ul, return SQLIT
12464 45 5f 4f 4b 2e 20 49 66 20 61 6e 20 49 4f 20 65  E_OK. If an IO e
12465 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c  rror occurs whil
12466 65 20 6d 6f 64 69 66 79 69 6e 67 0a 2a 2a 20 74  e modifying.** t
12467 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12468 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72  , return the err
12469 6f 72 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63  or code to the c
1246a 61 6c 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  aller..*/.static
1246b 20 69 6e 74 20 70 61 67 65 72 5f 74 72 75 6e 63   int pager_trunc
1246c 61 74 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ate(Pager *pPage
1246d 72 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a  r, Pgno nPage){.
1246e 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1246f 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 50 61 67  E_OK;.  if( pPag
12470 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52  er->state>=PAGER
12471 5f 45 58 43 4c 55 53 49 56 45 20 26 26 20 69 73  _EXCLUSIVE && is
12472 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29  Open(pPager->fd)
12473 20 29 7b 0a 20 20 20 20 69 36 34 20 63 75 72 72   ){.    i64 curr
12474 65 6e 74 53 69 7a 65 2c 20 6e 65 77 53 69 7a 65  entSize, newSize
12475 3b 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 49  ;.    /* TODO: I
12476 73 20 69 74 20 73 61 66 65 20 74 6f 20 75 73 65  s it safe to use
12477 20 50 61 67 65 72 2e 64 62 46 69 6c 65 53 69 7a   Pager.dbFileSiz
12478 65 20 68 65 72 65 3f 20 2a 2f 0a 20 20 20 20 72  e here? */.    r
12479 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c  c = sqlite3OsFil
1247a 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64  eSize(pPager->fd
1247b 2c 20 26 63 75 72 72 65 6e 74 53 69 7a 65 29 3b  , &currentSize);
1247c 0a 20 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 70  .    newSize = p
1247d 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2a  Pager->pageSize*
1247e 28 69 36 34 29 6e 50 61 67 65 3b 0a 20 20 20 20  (i64)nPage;.    
1247f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
12480 4b 20 26 26 20 63 75 72 72 65 6e 74 53 69 7a 65  K && currentSize
12481 21 3d 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20 20  !=newSize ){.   
12482 20 20 20 69 66 28 20 63 75 72 72 65 6e 74 53 69     if( currentSi
12483 7a 65 3e 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20  ze>newSize ){.  
12484 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
12485 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61  e3OsTruncate(pPa
12486 67 65 72 2d 3e 66 64 2c 20 6e 65 77 53 69 7a 65  ger->fd, newSize
12487 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
12488 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
12489 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67  ite3OsWrite(pPag
1248a 65 72 2d 3e 66 64 2c 20 22 22 2c 20 31 2c 20 6e  er->fd, "", 1, n
1248b 65 77 53 69 7a 65 2d 31 29 3b 0a 20 20 20 20 20  ewSize-1);.     
1248c 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d   }.      if( rc=
1248d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1248e 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62        pPager->db
1248f 46 69 6c 65 53 69 7a 65 20 3d 20 6e 50 61 67 65  FileSize = nPage
12490 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
12491 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
12492 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
12493 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50  e value of the P
12494 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20  ager.sectorSize 
12495 76 61 72 69 61 62 6c 65 20 66 6f 72 20 74 68 65  variable for the
12496 20 67 69 76 65 6e 0a 2a 2a 20 70 61 67 65 72 20   given.** pager 
12497 62 61 73 65 64 20 6f 6e 20 74 68 65 20 76 61 6c  based on the val
12498 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  ue returned by t
12499 68 65 20 78 53 65 63 74 6f 72 53 69 7a 65 20 6d  he xSectorSize m
1249a 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20  ethod.** of the 
1249b 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 66 69  open database fi
1249c 6c 65 2e 20 54 68 65 20 73 65 63 74 6f 72 20 73  le. The sector s
1249d 69 7a 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64  ize will be used
1249e 20 75 73 65 64 20 0a 2a 2a 20 74 6f 20 64 65 74   used .** to det
1249f 65 72 6d 69 6e 65 20 74 68 65 20 73 69 7a 65 20  ermine the size 
124a0 61 6e 64 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66  and alignment of
124a1 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
124a2 61 6e 64 20 0a 2a 2a 20 6d 61 73 74 65 72 20 6a  and .** master j
124a3 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 73 20  ournal pointers 
124a4 77 69 74 68 69 6e 20 63 72 65 61 74 65 64 20 6a  within created j
124a5 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a  ournal files..**
124a6 0a 2a 2a 20 46 6f 72 20 74 65 6d 70 6f 72 61 72  .** For temporar
124a7 79 20 66 69 6c 65 73 20 74 68 65 20 65 66 66 65  y files the effe
124a8 63 74 69 76 65 20 73 65 63 74 6f 72 20 73 69 7a  ctive sector siz
124a9 65 20 69 73 20 61 6c 77 61 79 73 20 35 31 32 20  e is always 512 
124aa 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68  bytes..**.** Oth
124ab 65 72 77 69 73 65 2c 20 66 6f 72 20 6e 6f 6e 2d  erwise, for non-
124ac 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2c  temporary files,
124ad 20 74 68 65 20 65 66 66 65 63 74 69 76 65 20 73   the effective s
124ae 65 63 74 6f 72 20 73 69 7a 65 20 69 73 0a 2a 2a  ector size is.**
124af 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
124b0 6e 65 64 20 62 79 20 74 68 65 20 78 53 65 63 74  ned by the xSect
124b1 6f 72 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 20  orSize() method 
124b2 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 35 31  rounded up to 51
124b3 32 20 69 66 0a 2a 2a 20 69 74 20 69 73 20 6c 65  2 if.** it is le
124b4 73 73 20 74 68 61 6e 20 35 31 32 2c 20 6f 72 20  ss than 512, or 
124b5 72 6f 75 6e 64 65 64 20 64 6f 77 6e 20 74 6f 20  rounded down to 
124b6 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20  MAX_SECTOR_SIZE 
124b7 69 66 20 69 74 0a 2a 2a 20 69 73 20 67 72 65 61  if it.** is grea
124b8 74 65 72 20 74 68 61 6e 20 4d 41 58 5f 53 45 43  ter than MAX_SEC
124b9 54 4f 52 5f 53 49 5a 45 2e 0a 2a 2f 0a 73 74 61  TOR_SIZE..*/.sta
124ba 74 69 63 20 76 6f 69 64 20 73 65 74 53 65 63 74  tic void setSect
124bb 6f 72 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50  orSize(Pager *pP
124bc 61 67 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28  ager){.  assert(
124bd 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
124be 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74  fd) || pPager->t
124bf 65 6d 70 46 69 6c 65 20 29 3b 0a 0a 20 20 69 66  empFile );..  if
124c0 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  ( !pPager->tempF
124c1 69 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65  ile ){.    /* Se
124c2 63 74 6f 72 20 73 69 7a 65 20 64 6f 65 73 6e 27  ctor size doesn'
124c3 74 20 6d 61 74 74 65 72 20 66 6f 72 20 74 65 6d  t matter for tem
124c4 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 20 41 6c  porary files. Al
124c5 73 6f 2c 20 74 68 65 20 66 69 6c 65 0a 20 20 20  so, the file.   
124c6 20 2a 2a 20 6d 61 79 20 6e 6f 74 20 68 61 76 65   ** may not have
124c7 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 79 65 74   been opened yet
124c8 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20  , in which case 
124c9 74 68 65 20 4f 73 53 65 63 74 6f 72 53 69 7a 65  the OsSectorSize
124ca 28 29 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 77  ().    ** call w
124cb 69 6c 6c 20 73 65 67 66 61 75 6c 74 2e 0a 20 20  ill segfault..  
124cc 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d    */.    pPager-
124cd 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 73 71  >sectorSize = sq
124ce 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a  lite3OsSectorSiz
124cf 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20  e(pPager->fd);. 
124d0 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d   }.  if( pPager-
124d1 3e 73 65 63 74 6f 72 53 69 7a 65 3c 35 31 32 20  >sectorSize<512 
124d2 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73  ){.    pPager->s
124d3 65 63 74 6f 72 53 69 7a 65 20 3d 20 35 31 32 3b  ectorSize = 512;
124d4 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65  .  }.  if( pPage
124d5 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3e 4d 41  r->sectorSize>MA
124d6 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 29 7b  X_SECTOR_SIZE ){
124d7 0a 20 20 20 20 61 73 73 65 72 74 28 20 4d 41 58  .    assert( MAX
124d8 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3e 3d 35 31  _SECTOR_SIZE>=51
124d9 32 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  2 );.    pPager-
124da 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 4d 41  >sectorSize = MA
124db 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20  X_SECTOR_SIZE;. 
124dc 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79   }.}../*.** Play
124dd 62 61 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c  back the journal
124de 20 61 6e 64 20 74 68 75 73 20 72 65 73 74 6f 72   and thus restor
124df 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  e the database f
124e0 69 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20 73 74  ile to.** the st
124e1 61 74 65 20 69 74 20 77 61 73 20 69 6e 20 62 65  ate it was in be
124e2 66 6f 72 65 20 77 65 20 73 74 61 72 74 65 64 20  fore we started 
124e3 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65 73 2e 20  making changes. 
124e4 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72   .**.** The jour
124e5 6e 61 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20  nal file format 
124e6 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 0a  is as follows: .
124e7 2a 2a 0a 2a 2a 20 20 28 31 29 20 20 38 20 62 79  **.**  (1)  8 by
124e8 74 65 20 70 72 65 66 69 78 2e 20 20 41 20 63 6f  te prefix.  A co
124e9 70 79 20 6f 66 20 61 4a 6f 75 72 6e 61 6c 4d 61  py of aJournalMa
124ea 67 69 63 5b 5d 2e 0a 2a 2a 20 20 28 32 29 20 20  gic[]..**  (2)  
124eb 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61  4 byte big-endia
124ec 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20  n integer which 
124ed 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
124ee 20 76 61 6c 69 64 20 70 61 67 65 20 72 65 63 6f   valid page reco
124ef 72 64 73 0a 2a 2a 20 20 20 20 20 20 20 69 6e 20  rds.**       in 
124f0 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 66  the journal.  If
124f1 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 20 30   this value is 0
124f2 78 66 66 66 66 66 66 66 66 2c 20 74 68 65 6e 20  xffffffff, then 
124f3 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20 20  compute the.**  
124f4 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 70       number of p
124f5 61 67 65 20 72 65 63 6f 72 64 73 20 66 72 6f 6d  age records from
124f6 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 73 69 7a   the journal siz
124f7 65 2e 0a 2a 2a 20 20 28 33 29 20 20 34 20 62 79  e..**  (3)  4 by
124f8 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e  te big-endian in
124f9 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74  teger which is t
124fa 68 65 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65  he initial value
124fb 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 20 20 20   for the .**    
124fc 20 20 20 73 61 6e 69 74 79 20 63 68 65 63 6b 73     sanity checks
124fd 75 6d 2e 0a 2a 2a 20 20 28 34 29 20 20 34 20 62  um..**  (4)  4 b
124fe 79 74 65 20 69 6e 74 65 67 65 72 20 77 68 69 63  yte integer whic
124ff 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  h is the number 
12500 6f 66 20 70 61 67 65 73 20 74 6f 20 74 72 75 6e  of pages to trun
12501 63 61 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20  cate the.**     
12502 20 20 64 61 74 61 62 61 73 65 20 74 6f 20 64 75    database to du
12503 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2e  ring a rollback.
12504 0a 2a 2a 20 20 28 35 29 20 20 34 20 62 79 74 65  .**  (5)  4 byte
12505 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65   big-endian inte
12506 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65  ger which is the
12507 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 20 20 54   sector size.  T
12508 68 65 20 68 65 61 64 65 72 0a 2a 2a 20 20 20 20  he header.**    
12509 20 20 20 69 73 20 74 68 69 73 20 6d 61 6e 79 20     is this many 
1250a 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a  bytes in size..*
1250b 2a 20 20 28 36 29 20 20 34 20 62 79 74 65 20 62  *  (6)  4 byte b
1250c 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65  ig-endian intege
1250d 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 70  r which is the p
1250e 61 67 65 20 63 61 73 65 2e 0a 2a 2a 20 20 28 37  age case..**  (7
1250f 29 20 20 34 20 62 79 74 65 20 69 6e 74 65 67 65  )  4 byte intege
12510 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6e  r which is the n
12511 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
12512 6e 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  n the master jou
12513 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 6e 61  rnal.**       na
12514 6d 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6d  me.  The value m
12515 61 79 20 62 65 20 7a 65 72 6f 20 28 69 6e 64 69  ay be zero (indi
12516 63 61 74 65 20 74 68 61 74 20 74 68 65 72 65 20  cate that there 
12517 69 73 20 6e 6f 20 6d 61 73 74 65 72 0a 2a 2a 20  is no master.** 
12518 20 20 20 20 20 20 6a 6f 75 72 6e 61 6c 2e 29 0a        journal.).
12519 2a 2a 20 20 28 38 29 20 20 4e 20 62 79 74 65 73  **  (8)  N bytes
1251a 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a   of the master j
1251b 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 20 20 54 68  ournal name.  Th
1251c 65 20 6e 61 6d 65 20 77 69 6c 6c 20 62 65 20 6e  e name will be n
1251d 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a  ul-terminated.**
1251e 20 20 20 20 20 20 20 61 6e 64 20 6d 69 67 68 74         and might
1251f 20 62 65 20 73 68 6f 72 74 65 72 20 74 68 61 6e   be shorter than
12520 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 20   the value read 
12521 66 72 6f 6d 20 28 35 29 2e 20 20 49 66 20 74 68  from (5).  If th
12522 65 20 66 69 72 73 74 20 62 79 74 65 0a 2a 2a 20  e first byte.** 
12523 20 20 20 20 20 20 6f 66 20 74 68 65 20 6e 61 6d        of the nam
12524 65 20 69 73 20 5c 30 30 30 20 74 68 65 6e 20 74  e is \000 then t
12525 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 73 74 65  here is no maste
12526 72 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20  r journal.  The 
12527 6d 61 73 74 65 72 0a 2a 2a 20 20 20 20 20 20 20  master.**       
12528 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 73 20  journal name is 
12529 73 74 6f 72 65 64 20 69 6e 20 55 54 46 2d 38 2e  stored in UTF-8.
1252a 0a 2a 2a 20 20 28 39 29 20 20 5a 65 72 6f 20 6f  .**  (9)  Zero o
1252b 72 20 6d 6f 72 65 20 70 61 67 65 73 20 69 6e 73  r more pages ins
1252c 74 61 6e 63 65 73 2c 20 65 61 63 68 20 61 73 20  tances, each as 
1252d 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 20 20 20 20  follows:.**     
1252e 20 20 20 2b 20 20 34 20 62 79 74 65 20 70 61 67     +  4 byte pag
1252f 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 20 20 20  e number..**    
12530 20 20 20 20 2b 20 20 70 50 61 67 65 72 2d 3e 70      +  pPager->p
12531 61 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66  ageSize bytes of
12532 20 64 61 74 61 2e 0a 2a 2a 20 20 20 20 20 20 20   data..**       
12533 20 2b 20 20 34 20 62 79 74 65 20 63 68 65 63 6b   +  4 byte check
12534 73 75 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77  sum.**.** When w
12535 65 20 73 70 65 61 6b 20 6f 66 20 74 68 65 20 6a  e speak of the j
12536 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2c 20 77  ournal header, w
12537 65 20 6d 65 61 6e 20 74 68 65 20 66 69 72 73 74  e mean the first
12538 20 38 20 69 74 65 6d 73 20 61 62 6f 76 65 2e 0a   8 items above..
12539 2a 2a 20 45 61 63 68 20 65 6e 74 72 79 20 69 6e  ** Each entry in
1253a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   the journal is 
1253b 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
1253c 68 65 20 39 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a  he 9th item..**.
1253d 2a 2a 20 43 61 6c 6c 20 74 68 65 20 76 61 6c 75  ** Call the valu
1253e 65 20 66 72 6f 6d 20 74 68 65 20 73 65 63 6f 6e  e from the secon
1253f 64 20 62 75 6c 6c 65 74 20 22 6e 52 65 63 22 2e  d bullet "nRec".
12540 20 20 6e 52 65 63 20 69 73 20 74 68 65 20 6e 75    nRec is the nu
12541 6d 62 65 72 20 6f 66 0a 2a 2a 20 76 61 6c 69 64  mber of.** valid
12542 20 70 61 67 65 20 65 6e 74 72 69 65 73 20 69 6e   page entries in
12543 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49   the journal.  I
12544 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c 20 79 6f  n most cases, yo
12545 75 20 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68  u can compute th
12546 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6e 52  e.** value of nR
12547 65 63 20 66 72 6f 6d 20 74 68 65 20 73 69 7a 65  ec from the size
12548 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
12549 66 69 6c 65 2e 20 20 42 75 74 20 69 66 20 61 20  file.  But if a 
1254a 70 6f 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65  power.** failure
1254b 20 6f 63 63 75 72 72 65 64 20 77 68 69 6c 65 20   occurred while 
1254c 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20  the journal was 
1254d 62 65 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 69  being written, i
1254e 74 20 63 6f 75 6c 64 20 62 65 20 74 68 65 0a 2a  t could be the.*
1254f 2a 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20  * case that the 
12550 73 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72  size of the jour
12551 6e 61 6c 20 66 69 6c 65 20 68 61 64 20 61 6c 72  nal file had alr
12552 65 61 64 79 20 62 65 65 6e 20 69 6e 63 72 65 61  eady been increa
12553 73 65 64 20 62 75 74 0a 2a 2a 20 74 68 65 20 65  sed but.** the e
12554 78 74 72 61 20 65 6e 74 72 69 65 73 20 68 61 64  xtra entries had
12555 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 69 74   not yet made it
12556 20 73 61 66 65 6c 79 20 74 6f 20 64 69 73 6b 2e   safely to disk.
12557 20 20 49 6e 20 73 75 63 68 20 61 20 63 61 73 65    In such a case
12558 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f  ,.** the value o
12559 66 20 6e 52 65 63 20 63 6f 6d 70 75 74 65 64 20  f nRec computed 
1255a 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 20 73 69  from the file si
1255b 7a 65 20 77 6f 75 6c 64 20 62 65 20 74 6f 6f 20  ze would be too 
1255c 6c 61 72 67 65 2e 20 20 46 6f 72 0a 2a 2a 20 74  large.  For.** t
1255d 68 61 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 61  hat reason, we a
1255e 6c 77 61 79 73 20 75 73 65 20 74 68 65 20 6e 52  lways use the nR
1255f 65 63 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20  ec value in the 
12560 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66  header..**.** If
12561 20 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20   the nRec value 
12562 69 73 20 30 78 66 66 66 66 66 66 66 66 20 69 74  is 0xffffffff it
12563 20 6d 65 61 6e 73 20 74 68 61 74 20 6e 52 65 63   means that nRec
12564 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75   should be compu
12565 74 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20  ted.** from the 
12566 66 69 6c 65 20 73 69 7a 65 2e 20 20 54 68 69 73  file size.  This
12567 20 76 61 6c 75 65 20 69 73 20 75 73 65 64 20 77   value is used w
12568 68 65 6e 20 74 68 65 20 75 73 65 72 20 73 65 6c  hen the user sel
12569 65 63 74 73 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73  ects the.** no-s
1256a 79 6e 63 20 6f 70 74 69 6f 6e 20 66 6f 72 20 74  ync option for t
1256b 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 20 70  he journal.  A p
1256c 6f 77 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75  ower failure cou
1256d 6c 64 20 6c 65 61 64 20 74 6f 20 63 6f 72 72 75  ld lead to corru
1256e 70 74 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 69 73  ption.** in this
1256f 20 63 61 73 65 2e 20 20 42 75 74 20 66 6f 72 20   case.  But for 
12570 74 68 69 6e 67 73 20 6c 69 6b 65 20 74 65 6d 70  things like temp
12571 6f 72 61 72 79 20 74 61 62 6c 65 20 28 77 68 69  orary table (whi
12572 63 68 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 65  ch will be.** de
12573 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20 70  leted when the p
12574 6f 77 65 72 20 69 73 20 72 65 73 74 6f 72 65 64  ower is restored
12575 29 20 77 65 20 64 6f 6e 27 74 20 63 61 72 65 2e  ) we don't care.
12576 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20    .**.** If the 
12577 66 69 6c 65 20 6f 70 65 6e 65 64 20 61 73 20 74  file opened as t
12578 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
12579 69 73 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f  is not a well-fo
1257a 72 6d 65 64 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20  rmed.** journal 
1257b 66 69 6c 65 20 74 68 65 6e 20 61 6c 6c 20 70 61  file then all pa
1257c 67 65 73 20 75 70 20 74 6f 20 74 68 65 20 66 69  ges up to the fi
1257d 72 73 74 20 63 6f 72 72 75 70 74 65 64 20 70 61  rst corrupted pa
1257e 67 65 20 61 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a  ge are rolled.**
1257f 20 62 61 63 6b 20 28 6f 72 20 6e 6f 20 70 61 67   back (or no pag
12580 65 73 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61  es if the journa
12581 6c 20 68 65 61 64 65 72 20 69 73 20 63 6f 72 72  l header is corr
12582 75 70 74 65 64 29 2e 20 54 68 65 20 6a 6f 75 72  upted). The jour
12583 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20 74  nal file.** is t
12584 68 65 6e 20 64 65 6c 65 74 65 64 20 61 6e 64 20  hen deleted and 
12585 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e  SQLITE_OK return
12586 65 64 2c 20 6a 75 73 74 20 61 73 20 69 66 20 6e  ed, just as if n
12587 6f 20 63 6f 72 72 75 70 74 69 6f 6e 20 68 61 64  o corruption had
12588 0a 2a 2a 20 62 65 65 6e 20 65 6e 63 6f 75 6e 74  .** been encount
12589 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  ered..**.** If a
1258a 6e 20 49 2f 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28  n I/O or malloc(
1258b 29 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  ) error occurs, 
1258c 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65  the journal-file
1258d 20 69 73 20 6e 6f 74 20 64 65 6c 65 74 65 64 0a   is not deleted.
1258e 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20  ** and an error 
1258f 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
12590 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 48 6f  ..**.** The isHo
12591 74 20 70 61 72 61 6d 65 74 65 72 20 69 6e 64 69  t parameter indi
12592 63 61 74 65 73 20 74 68 61 74 20 77 65 20 61 72  cates that we ar
12593 65 20 74 72 79 69 6e 67 20 74 6f 20 72 6f 6c 6c  e trying to roll
12594 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 0a 2a  back a journal.*
12595 2a 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 20  * that might be 
12596 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 2e 20 20  a hot journal.  
12597 4f 72 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20  Or, it could be 
12598 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c  that the journal
12599 20 69 73 20 0a 2a 2a 20 70 72 65 73 65 72 76 65   is .** preserve
1259a 64 20 62 65 63 61 75 73 65 20 6f 66 20 4a 4f 55  d because of JOU
1259b 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54  RNALMODE_PERSIST
1259c 20 6f 72 20 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f   or JOURNALMODE_
1259d 54 52 55 4e 43 41 54 45 2e 0a 2a 2a 20 49 66 20  TRUNCATE..** If 
1259e 74 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65 61 6c  the journal real
1259f 6c 79 20 69 73 20 68 6f 74 2c 20 72 65 73 65 74  ly is hot, reset
125a0 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65   the pager cache
125a1 20 70 72 69 6f 72 20 72 6f 6c 6c 69 6e 67 0a 2a   prior rolling.*
125a2 2a 20 62 61 63 6b 20 61 6e 79 20 63 6f 6e 74 65  * back any conte
125a3 6e 74 2e 20 20 49 66 20 74 68 65 20 6a 6f 75 72  nt.  If the jour
125a4 6e 61 6c 20 69 73 20 6d 65 72 65 6c 79 20 70 65  nal is merely pe
125a5 72 73 69 73 74 65 6e 74 2c 20 6e 6f 20 72 65 73  rsistent, no res
125a6 65 74 20 69 73 0a 2a 2a 20 6e 65 65 64 65 64 2e  et is.** needed.
125a7 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
125a8 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 50 61  ager_playback(Pa
125a9 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
125aa 20 69 73 48 6f 74 29 7b 0a 20 20 73 71 6c 69 74   isHot){.  sqlit
125ab 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70  e3_vfs *pVfs = p
125ac 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69  Pager->pVfs;.  i
125ad 36 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20  64 szJ;         
125ae 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
125af 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  of the journal f
125b0 69 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  ile in bytes */.
125b1 20 20 75 33 32 20 6e 52 65 63 3b 20 20 20 20 20    u32 nRec;     
125b2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
125b3 6d 62 65 72 20 6f 66 20 52 65 63 6f 72 64 73 20  mber of Records 
125b4 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a  in the journal *
125b5 2f 0a 20 20 75 33 32 20 75 3b 20 20 20 20 20 20  /.  u32 u;      
125b6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
125b7 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f  Unsigned loop co
125b8 75 6e 74 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20  unter */.  Pgno 
125b9 6d 78 50 67 20 3d 20 30 3b 20 20 20 20 20 20 20  mxPg = 0;       
125ba 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
125bb 68 65 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65  he original file
125bc 20 69 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20 69   in pages */.  i
125bd 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
125be 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c          /* Resul
125bf 74 20 63 6f 64 65 20 6f 66 20 61 20 73 75 62 72  t code of a subr
125c0 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  outine */.  int 
125c1 72 65 73 20 3d 20 31 3b 20 20 20 20 20 20 20 20  res = 1;        
125c2 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65       /* Value re
125c3 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65  turned by sqlite
125c4 33 4f 73 41 63 63 65 73 73 28 29 20 2a 2f 0a 20  3OsAccess() */. 
125c5 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d   char *zMaster =
125c6 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d   0;       /* Nam
125c7 65 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72  e of master jour
125c8 6e 61 6c 20 66 69 6c 65 20 69 66 20 61 6e 79 20  nal file if any 
125c9 2a 2f 0a 20 20 69 6e 74 20 6e 65 65 64 50 61 67  */.  int needPag
125ca 65 72 52 65 73 65 74 3b 20 20 20 20 20 20 2f 2a  erReset;      /*
125cb 20 54 72 75 65 20 74 6f 20 72 65 73 65 74 20 70   True to reset p
125cc 61 67 65 20 70 72 69 6f 72 20 74 6f 20 66 69 72  age prior to fir
125cd 73 74 20 70 61 67 65 20 72 6f 6c 6c 62 61 63 6b  st page rollback
125ce 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65   */..  /* Figure
125cf 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 65   out how many re
125d0 63 6f 72 64 73 20 61 72 65 20 69 6e 20 74 68 65  cords are in the
125d1 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 62 6f 72 74   journal.  Abort
125d2 20 65 61 72 6c 79 20 69 66 0a 20 20 2a 2a 20 74   early if.  ** t
125d3 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 65 6d  he journal is em
125d4 70 74 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  pty..  */.  asse
125d5 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65  rt( isOpen(pPage
125d6 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 72 63 20  r->jfd) );.  rc 
125d7 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53  = sqlite3OsFileS
125d8 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ize(pPager->jfd,
125d9 20 26 73 7a 4a 29 3b 0a 20 20 69 66 28 20 72 63   &szJ);.  if( rc
125da 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73  !=SQLITE_OK || s
125db 7a 4a 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74  zJ==0 ){.    got
125dc 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a  o end_playback;.
125dd 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74    }..  /* Read t
125de 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
125df 6c 20 6e 61 6d 65 20 66 72 6f 6d 20 74 68 65 20  l name from the 
125e0 6a 6f 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 69  journal, if it i
125e1 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 2a 2a 20  s present..  ** 
125e2 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72  If a master jour
125e3 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73  nal file name is
125e4 20 73 70 65 63 69 66 69 65 64 2c 20 62 75 74 20   specified, but 
125e5 74 68 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 0a  the file is not.
125e6 20 20 2a 2a 20 70 72 65 73 65 6e 74 20 6f 6e 20    ** present on 
125e7 64 69 73 6b 2c 20 74 68 65 6e 20 74 68 65 20 6a  disk, then the j
125e8 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 68 6f  ournal is not ho
125e9 74 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e  t and does not n
125ea 65 65 64 20 74 6f 20 62 65 0a 20 20 2a 2a 20 70  eed to be.  ** p
125eb 6c 61 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a 2a  layed back..  **
125ec 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20 54 65 63 68  .  ** TODO: Tech
125ed 6e 69 63 61 6c 6c 79 20 74 68 65 20 66 6f 6c 6c  nically the foll
125ee 6f 77 69 6e 67 20 69 73 20 61 6e 20 65 72 72 6f  owing is an erro
125ef 72 20 62 65 63 61 75 73 65 20 69 74 20 61 73 73  r because it ass
125f0 75 6d 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 62  umes that.  ** b
125f1 75 66 66 65 72 20 50 61 67 65 72 2e 70 54 6d 70  uffer Pager.pTmp
125f2 53 70 61 63 65 20 69 73 20 28 6d 78 50 61 74 68  Space is (mxPath
125f3 6e 61 6d 65 2b 31 29 20 62 79 74 65 73 20 6f 72  name+1) bytes or
125f4 20 6c 61 72 67 65 72 2e 20 69 2e 65 2e 20 74 68   larger. i.e. th
125f5 61 74 0a 20 20 2a 2a 20 28 70 50 61 67 65 72 2d  at.  ** (pPager-
125f6 3e 70 61 67 65 53 69 7a 65 20 3e 3d 20 70 50 61  >pageSize >= pPa
125f7 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74  ger->pVfs->mxPat
125f8 68 6e 61 6d 65 2b 31 29 2e 20 55 73 69 6e 67 20  hname+1). Using 
125f9 6f 73 5f 75 6e 69 78 2e 63 2c 0a 20 20 2a 2a 20  os_unix.c,.  ** 
125fa 20 6d 78 50 61 74 68 6e 61 6d 65 20 69 73 20 35   mxPathname is 5
125fb 31 32 2c 20 77 68 69 63 68 20 69 73 20 74 68 65  12, which is the
125fc 20 73 61 6d 65 20 61 73 20 74 68 65 20 6d 69 6e   same as the min
125fd 69 6d 75 6d 20 61 6c 6c 6f 77 61 62 6c 65 20 76  imum allowable v
125fe 61 6c 75 65 0a 20 20 2a 2a 20 66 6f 72 20 70 61  alue.  ** for pa
125ff 67 65 53 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 7a  geSize..  */.  z
12600 4d 61 73 74 65 72 20 3d 20 70 50 61 67 65 72 2d  Master = pPager-
12601 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 72 63  >pTmpSpace;.  rc
12602 20 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75   = readMasterJou
12603 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64  rnal(pPager->jfd
12604 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65  , zMaster, pPage
12605 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e  r->pVfs->mxPathn
12606 61 6d 65 2b 31 29 3b 0a 20 20 69 66 28 20 72 63  ame+1);.  if( rc
12607 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 7a  ==SQLITE_OK && z
12608 4d 61 73 74 65 72 5b 30 5d 20 29 7b 0a 20 20 20  Master[0] ){.   
12609 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41   rc = sqlite3OsA
1260a 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73  ccess(pVfs, zMas
1260b 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45  ter, SQLITE_ACCE
1260c 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29  SS_EXISTS, &res)
1260d 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 20  ;.  }.  zMaster 
1260e 3d 20 30 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  = 0;.  if( rc!=S
1260f 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 72 65 73  QLITE_OK || !res
12610 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64   ){.    goto end
12611 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a 20  _playback;.  }. 
12612 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
12613 4f 66 66 20 3d 20 30 3b 0a 20 20 6e 65 65 64 50  Off = 0;.  needP
12614 61 67 65 72 52 65 73 65 74 20 3d 20 69 73 48 6f  agerReset = isHo
12615 74 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f  t;..  /* This lo
12616 6f 70 20 74 65 72 6d 69 6e 61 74 65 73 20 65 69  op terminates ei
12617 74 68 65 72 20 77 68 65 6e 20 61 20 72 65 61 64  ther when a read
12618 4a 6f 75 72 6e 61 6c 48 64 72 28 29 20 6f 72 20  JournalHdr() or 
12619 0a 20 20 2a 2a 20 70 61 67 65 72 5f 70 6c 61 79  .  ** pager_play
1261a 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20  back_one_page() 
1261b 63 61 6c 6c 20 72 65 74 75 72 6e 73 20 53 51 4c  call returns SQL
1261c 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 49  ITE_DONE or an I
1261d 4f 20 65 72 72 6f 72 20 0a 20 20 2a 2a 20 6f 63  O error .  ** oc
1261e 63 75 72 73 2e 20 0a 20 20 2a 2f 0a 20 20 77 68  curs. .  */.  wh
1261f 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 69 6e  ile( 1 ){.    in
12620 74 20 69 73 55 6e 73 79 6e 63 20 3d 20 30 3b 0a  t isUnsync = 0;.
12621 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65  .    /* Read the
12622 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65   next journal he
12623 61 64 65 72 20 66 72 6f 6d 20 74 68 65 20 6a 6f  ader from the jo
12624 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 49 66 20  urnal file.  If 
12625 74 68 65 72 65 20 61 72 65 0a 20 20 20 20 2a 2a  there are.    **
12626 20 6e 6f 74 20 65 6e 6f 75 67 68 20 62 79 74 65   not enough byte
12627 73 20 6c 65 66 74 20 69 6e 20 74 68 65 20 6a 6f  s left in the jo
12628 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 61  urnal file for a
12629 20 63 6f 6d 70 6c 65 74 65 20 68 65 61 64 65 72   complete header
1262a 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 69 74 20 69  , or.    ** it i
1262b 73 20 63 6f 72 72 75 70 74 65 64 2c 20 74 68 65  s corrupted, the
1262c 6e 20 61 20 70 72 6f 63 65 73 73 20 6d 75 73 74  n a process must
1262d 20 6f 66 20 66 61 69 6c 65 64 20 77 68 69 6c 65   of failed while
1262e 20 77 72 69 74 69 6e 67 20 69 74 2e 0a 20 20 20   writing it..   
1262f 20 2a 2a 20 54 68 69 73 20 69 6e 64 69 63 61 74   ** This indicat
12630 65 73 20 6e 6f 74 68 69 6e 67 20 6d 6f 72 65 20  es nothing more 
12631 6e 65 65 64 73 20 74 6f 20 62 65 20 72 6f 6c 6c  needs to be roll
12632 65 64 20 62 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a  ed back..    */.
12633 20 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75      rc = readJou
12634 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20  rnalHdr(pPager, 
12635 69 73 48 6f 74 2c 20 73 7a 4a 2c 20 26 6e 52 65  isHot, szJ, &nRe
12636 63 2c 20 26 6d 78 50 67 29 3b 0a 20 20 20 20 69  c, &mxPg);.    i
12637 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
12638 20 29 7b 20 0a 20 20 20 20 20 20 69 66 28 20 72   ){ .      if( r
12639 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c==SQLITE_DONE )
1263a 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  {.        rc = S
1263b 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
1263c 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64  }.      goto end
1263d 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 7d  _playback;.    }
1263e 0a 0a 20 20 20 20 2f 2a 20 49 66 20 6e 52 65 63  ..    /* If nRec
1263f 20 69 73 20 30 78 66 66 66 66 66 66 66 66 2c 20   is 0xffffffff, 
12640 74 68 65 6e 20 74 68 69 73 20 6a 6f 75 72 6e 61  then this journa
12641 6c 20 77 61 73 20 63 72 65 61 74 65 64 20 62 79  l was created by
12642 20 61 20 70 72 6f 63 65 73 73 0a 20 20 20 20 2a   a process.    *
12643 2a 20 77 6f 72 6b 69 6e 67 20 69 6e 20 6e 6f 2d  * working in no-
12644 73 79 6e 63 20 6d 6f 64 65 2e 20 54 68 69 73 20  sync mode. This 
12645 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 72  means that the r
12646 65 73 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  est of the journ
12647 61 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 63  al.    ** file c
12648 6f 6e 73 69 73 74 73 20 6f 66 20 70 61 67 65 73  onsists of pages
12649 2c 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6d  , there are no m
1264a 6f 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  ore journal head
1264b 65 72 73 2e 20 43 6f 6d 70 75 74 65 0a 20 20 20  ers. Compute.   
1264c 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66   ** the value of
1264d 20 6e 52 65 63 20 62 61 73 65 64 20 6f 6e 20 74   nRec based on t
1264e 68 69 73 20 61 73 73 75 6d 70 74 69 6f 6e 2e 0a  his assumption..
1264f 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e      */.    if( n
12650 52 65 63 3d 3d 30 78 66 66 66 66 66 66 66 66 20  Rec==0xffffffff 
12651 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
12652 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
12653 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52  Off==JOURNAL_HDR
12654 5f 53 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20  _SZ(pPager) );. 
12655 20 20 20 20 20 6e 52 65 63 20 3d 20 28 69 6e 74       nRec = (int
12656 29 28 28 73 7a 4a 20 2d 20 4a 4f 55 52 4e 41 4c  )((szJ - JOURNAL
12657 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 29  _HDR_SZ(pPager))
12658 2f 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70  /JOURNAL_PG_SZ(p
12659 50 61 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a 0a  Pager));.    }..
1265a 20 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69      /* If nRec i
1265b 73 20 30 20 61 6e 64 20 74 68 69 73 20 72 6f 6c  s 0 and this rol
1265c 6c 62 61 63 6b 20 69 73 20 6f 66 20 61 20 74 72  lback is of a tr
1265d 61 6e 73 61 63 74 69 6f 6e 20 63 72 65 61 74 65  ansaction create
1265e 64 20 62 79 20 74 68 69 73 0a 20 20 20 20 2a 2a  d by this.    **
1265f 20 70 72 6f 63 65 73 73 20 61 6e 64 20 69 66 20   process and if 
12660 74 68 69 73 20 69 73 20 74 68 65 20 66 69 6e 61  this is the fina
12661 6c 20 68 65 61 64 65 72 20 69 6e 20 74 68 65 20  l header in the 
12662 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 6e 20 69 74  journal, then it
12663 20 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68   means.    ** th
12664 61 74 20 74 68 69 73 20 70 61 72 74 20 6f 66 20  at this part of 
12665 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20  the journal was 
12666 62 65 69 6e 67 20 66 69 6c 6c 65 64 20 62 75 74  being filled but
12667 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65   has not yet bee
12668 6e 0a 20 20 20 20 2a 2a 20 73 79 6e 63 65 64 20  n.    ** synced 
12669 74 6f 20 64 69 73 6b 2e 20 20 43 6f 6d 70 75 74  to disk.  Comput
1266a 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
1266b 70 61 67 65 73 20 62 61 73 65 64 20 6f 6e 20 74  pages based on t
1266c 68 65 20 72 65 6d 61 69 6e 69 6e 67 0a 20 20 20  he remaining.   
1266d 20 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20   ** size of the 
1266e 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  file..    **.   
1266f 20 2a 2a 20 54 68 65 20 74 68 69 72 64 20 74 65   ** The third te
12670 72 6d 20 6f 66 20 74 68 65 20 74 65 73 74 20 77  rm of the test w
12671 61 73 20 61 64 64 65 64 20 74 6f 20 66 69 78 20  as added to fix 
12672 74 69 63 6b 65 74 20 23 32 35 36 35 2e 0a 20 20  ticket #2565..  
12673 20 20 2a 2a 20 57 68 65 6e 20 72 6f 6c 6c 69 6e    ** When rollin
12674 67 20 62 61 63 6b 20 61 20 68 6f 74 20 6a 6f 75  g back a hot jou
12675 72 6e 61 6c 2c 20 6e 52 65 63 3d 3d 30 20 61 6c  rnal, nRec==0 al
12676 77 61 79 73 20 6d 65 61 6e 73 20 74 68 61 74 20  ways means that 
12677 74 68 65 20 6e 65 78 74 0a 20 20 20 20 2a 2a 20  the next.    ** 
12678 63 68 75 6e 6b 20 6f 66 20 74 68 65 20 6a 6f 75  chunk of the jou
12679 72 6e 61 6c 20 63 6f 6e 74 61 69 6e 73 20 7a 65  rnal contains ze
1267a 72 6f 20 70 61 67 65 73 20 74 6f 20 62 65 20 72  ro pages to be r
1267b 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 42 75 74  olled back.  But
1267c 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 64 6f 69  .    ** when doi
1267d 6e 67 20 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e  ng a ROLLBACK an
1267e 64 20 74 68 65 20 6e 52 65 63 3d 3d 30 20 63 68  d the nRec==0 ch
1267f 75 6e 6b 20 69 73 20 74 68 65 20 6c 61 73 74 20  unk is the last 
12680 63 68 75 6e 6b 20 69 6e 0a 20 20 20 20 2a 2a 20  chunk in.    ** 
12681 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20  the journal, it 
12682 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 6a  means that the j
12683 6f 75 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e  ournal might con
12684 74 61 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a  tain additional.
12685 20 20 20 20 2a 2a 20 70 61 67 65 73 20 74 68 61      ** pages tha
12686 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72 6f 6c  t need to be rol
12687 6c 65 64 20 62 61 63 6b 20 61 6e 64 20 74 68 61  led back and tha
12688 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
12689 70 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 73 68  pages .    ** sh
1268a 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64  ould be computed
1268b 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 6a 6f   based on the jo
1268c 75 72 6e 61 6c 20 66 69 6c 65 20 73 69 7a 65 2e  urnal file size.
1268d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1268e 6e 52 65 63 3d 3d 30 20 26 26 20 21 69 73 48 6f  nRec==0 && !isHo
1268f 74 20 26 26 0a 20 20 20 20 20 20 20 20 70 50 61  t &&.        pPa
12690 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b  ger->journalHdr+
12691 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
12692 50 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e  Pager)==pPager->
12693 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20  journalOff ){.  
12694 20 20 20 20 6e 52 65 63 20 3d 20 28 69 6e 74 29      nRec = (int)
12695 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e  ((szJ - pPager->
12696 6a 6f 75 72 6e 61 6c 4f 66 66 29 20 2f 20 4a 4f  journalOff) / JO
12697 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67  URNAL_PG_SZ(pPag
12698 65 72 29 29 3b 0a 20 20 20 20 20 20 69 73 55 6e  er));.      isUn
12699 73 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  sync = 1;.    }.
1269a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20  .    /* If this 
1269b 69 73 20 74 68 65 20 66 69 72 73 74 20 68 65 61  is the first hea
1269c 64 65 72 20 72 65 61 64 20 66 72 6f 6d 20 74 68  der read from th
1269d 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 72 75 6e 63  e journal, trunc
1269e 61 74 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 64  ate the.    ** d
1269f 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 61 63  atabase file bac
126a0 6b 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61  k to its origina
126a1 6c 20 73 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20  l size..    */. 
126a2 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a     if( pPager->j
126a3 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e  ournalOff==JOURN
126a4 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
126a5 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  ) ){.      rc = 
126a6 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 70  pager_truncate(p
126a7 50 61 67 65 72 2c 20 6d 78 50 67 29 3b 0a 20 20  Pager, mxPg);.  
126a8 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
126a9 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
126aa 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61   goto end_playba
126ab 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ck;.      }.    
126ac 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
126ad 20 3d 20 6d 78 50 67 3b 0a 20 20 20 20 7d 0a 0a   = mxPg;.    }..
126ae 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 72 69 67      /* Copy orig
126af 69 6e 61 6c 20 70 61 67 65 73 20 6f 75 74 20 6f  inal pages out o
126b0 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e  f the journal an
126b1 64 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20  d back into the 
126b2 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65  .    ** database
126b3 20 66 69 6c 65 20 61 6e 64 2f 6f 72 20 70 61 67   file and/or pag
126b4 65 20 63 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a  e cache..    */.
126b5 20 20 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6e      for(u=0; u<n
126b6 52 65 63 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 20  Rec; u++){.     
126b7 20 69 66 28 20 6e 65 65 64 50 61 67 65 72 52 65   if( needPagerRe
126b8 73 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70  set ){.        p
126b9 61 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65  ager_reset(pPage
126ba 72 29 3b 0a 20 20 20 20 20 20 20 20 6e 65 65 64  r);.        need
126bb 50 61 67 65 72 52 65 73 65 74 20 3d 20 30 3b 0a  PagerReset = 0;.
126bc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
126bd 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63   = pager_playbac
126be 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65  k_one_page(pPage
126bf 72 2c 31 2c 69 73 55 6e 73 79 6e 63 2c 26 70 50  r,1,isUnsync,&pP
126c0 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
126c1 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20 69 66 28  ,0,0);.      if(
126c2 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
126c3 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  {.        if( rc
126c4 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b  ==SQLITE_DONE ){
126c5 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
126c6 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
126c7 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75       pPager->jou
126c8 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20  rnalOff = szJ;. 
126c9 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
126ca 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
126cb 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77           /* If w
126cc 65 20 61 72 65 20 75 6e 61 62 6c 65 20 74 6f 20  e are unable to 
126cd 72 6f 6c 6c 62 61 63 6b 2c 20 71 75 69 74 20 61  rollback, quit a
126ce 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72  nd return the er
126cf 72 6f 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ror.          **
126d0 20 63 6f 64 65 2e 20 20 54 68 69 73 20 77 69 6c   code.  This wil
126d1 6c 20 63 61 75 73 65 20 74 68 65 20 70 61 67 65  l cause the page
126d2 72 20 74 6f 20 65 6e 74 65 72 20 74 68 65 20 65  r to enter the e
126d3 72 72 6f 72 20 73 74 61 74 65 0a 20 20 20 20 20  rror state.     
126d4 20 20 20 20 20 2a 2a 20 73 6f 20 74 68 61 74 20       ** so that 
126d5 6e 6f 20 66 75 72 74 68 65 72 20 68 61 72 6d 20  no further harm 
126d6 77 69 6c 6c 20 62 65 20 64 6f 6e 65 2e 20 20 50  will be done.  P
126d7 65 72 68 61 70 73 20 74 68 65 20 6e 65 78 74 0a  erhaps the next.
126d8 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f            ** pro
126d9 63 65 73 73 20 74 6f 20 63 6f 6d 65 20 61 6c 6f  cess to come alo
126da 6e 67 20 77 69 6c 6c 20 62 65 20 61 62 6c 65 20  ng will be able 
126db 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20  to rollback the 
126dc 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20  database..      
126dd 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20      */.         
126de 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61   goto end_playba
126df 63 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ck;.        }.  
126e0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
126e1 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45 44 2a 2f    /*NOTREACHED*/
126e2 0a 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a  .  assert( 0 );.
126e3 0a 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3a 0a 20  .end_playback:. 
126e4 20 2f 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20 61 20   /* Following a 
126e5 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 20 64 61  rollback, the da
126e6 74 61 62 61 73 65 20 66 69 6c 65 20 73 68 6f 75  tabase file shou
126e7 6c 64 20 62 65 20 62 61 63 6b 20 69 6e 20 69 74  ld be back in it
126e8 73 20 6f 72 69 67 69 6e 61 6c 0a 20 20 2a 2a 20  s original.  ** 
126e9 73 74 61 74 65 20 70 72 69 6f 72 20 74 6f 20 74  state prior to t
126ea 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
126eb 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 73 6f 20  transaction, so 
126ec 69 6e 76 6f 6b 65 20 74 68 65 0a 20 20 2a 2a 20  invoke the.  ** 
126ed 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f  SQLITE_FCNTL_DB_
126ee 55 4e 43 48 41 4e 47 45 44 20 66 69 6c 65 2d 63  UNCHANGED file-c
126ef 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 20 74 6f  ontrol method to
126f0 20 64 69 73 61 62 6c 65 20 74 68 65 0a 20 20 2a   disable the.  *
126f1 2a 20 61 73 73 65 72 74 69 6f 6e 20 74 68 61 74  * assertion that
126f2 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
126f3 20 63 6f 75 6e 74 65 72 20 77 61 73 20 6d 6f 64   counter was mod
126f4 69 66 69 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73  ified..  */.  as
126f5 73 65 72 74 28 0a 20 20 20 20 70 50 61 67 65 72  sert(.    pPager
126f6 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d  ->fd->pMethods==
126f7 30 20 7c 7c 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ||.    sqlite3
126f8 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 70 50  OsFileControl(pP
126f9 61 67 65 72 2d 3e 66 64 2c 53 51 4c 49 54 45 5f  ager->fd,SQLITE_
126fa 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47  FCNTL_DB_UNCHANG
126fb 45 44 2c 30 29 3e 3d 53 51 4c 49 54 45 5f 4f 4b  ED,0)>=SQLITE_OK
126fc 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  .  );..  /* If t
126fd 68 69 73 20 70 6c 61 79 62 61 63 6b 20 69 73 20  his playback is 
126fe 68 61 70 70 65 6e 69 6e 67 20 61 75 74 6f 6d 61  happening automa
126ff 74 69 63 61 6c 6c 79 20 61 73 20 61 20 72 65 73  tically as a res
12700 75 6c 74 20 6f 66 20 61 6e 20 49 4f 20 6f 72 20  ult of an IO or 
12701 0a 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72  .  ** malloc err
12702 6f 72 20 74 68 61 74 20 6f 63 63 75 72 72 65 64  or that occurred
12703 20 61 66 74 65 72 20 74 68 65 20 63 68 61 6e 67   after the chang
12704 65 2d 63 6f 75 6e 74 65 72 20 77 61 73 20 75 70  e-counter was up
12705 64 61 74 65 64 20 62 75 74 20 0a 20 20 2a 2a 20  dated but .  ** 
12706 62 65 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73  before the trans
12707 61 63 74 69 6f 6e 20 77 61 73 20 63 6f 6d 6d 69  action was commi
12708 74 74 65 64 2c 20 74 68 65 6e 20 74 68 65 20 63  tted, then the c
12709 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 0a 20  hange-counter . 
1270a 20 2a 2a 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e   ** modification
1270b 20 6d 61 79 20 6a 75 73 74 20 68 61 76 65 20 62   may just have b
1270c 65 65 6e 20 72 65 76 65 72 74 65 64 2e 20 49 66  een reverted. If
1270d 20 74 68 69 73 20 68 61 70 70 65 6e 73 20 69 6e   this happens in
1270e 20 65 78 63 6c 75 73 69 76 65 20 0a 20 20 2a 2a   exclusive .  **
1270f 20 6d 6f 64 65 2c 20 74 68 65 6e 20 73 75 62 73   mode, then subs
12710 65 71 75 65 6e 74 20 74 72 61 6e 73 61 63 74 69  equent transacti
12711 6f 6e 73 20 70 65 72 66 6f 72 6d 65 64 20 62 79  ons performed by
12712 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20   the connection 
12713 77 69 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20 75 70  will not.  ** up
12714 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d  date the change-
12715 63 6f 75 6e 74 65 72 20 61 74 20 61 6c 6c 2e 20  counter at all. 
12716 54 68 69 73 20 6d 61 79 20 6c 65 61 64 20 74 6f  This may lead to
12717 20 63 61 63 68 65 20 69 6e 63 6f 6e 73 69 73 74   cache inconsist
12718 65 6e 63 79 0a 20 20 2a 2a 20 70 72 6f 62 6c 65  ency.  ** proble
12719 6d 73 20 66 6f 72 20 6f 74 68 65 72 20 70 72 6f  ms for other pro
1271a 63 65 73 73 65 73 20 61 74 20 73 6f 6d 65 20 70  cesses at some p
1271b 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75  oint in the futu
1271c 72 65 2e 20 53 6f 2c 20 6a 75 73 74 0a 20 20 2a  re. So, just.  *
1271d 2a 20 69 6e 20 63 61 73 65 20 74 68 69 73 20 68  * in case this h
1271e 61 73 20 68 61 70 70 65 6e 65 64 2c 20 63 6c 65  as happened, cle
1271f 61 72 20 74 68 65 20 63 68 61 6e 67 65 43 6f 75  ar the changeCou
12720 6e 74 44 6f 6e 65 20 66 6c 61 67 20 6e 6f 77 2e  ntDone flag now.
12721 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e  .  */.  pPager->
12722 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20  changeCountDone 
12723 3d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69  = pPager->tempFi
12724 6c 65 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53  le;..  if( rc==S
12725 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
12726 7a 4d 61 73 74 65 72 20 3d 20 70 50 61 67 65 72  zMaster = pPager
12727 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 20  ->pTmpSpace;.   
12728 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72   rc = readMaster
12729 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e  Journal(pPager->
1272a 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50  jfd, zMaster, pP
1272b 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61  ager->pVfs->mxPa
1272c 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 74  thname+1);.    t
1272d 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c  estcase( rc!=SQL
1272e 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20  ITE_OK );.  }.  
1272f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
12730 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61  K ){.    rc = pa
12731 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74  ger_end_transact
12732 69 6f 6e 28 70 50 61 67 65 72 2c 20 7a 4d 61 73  ion(pPager, zMas
12733 74 65 72 5b 30 5d 21 3d 27 5c 30 27 29 3b 0a 20  ter[0]!='\0');. 
12734 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 21     testcase( rc!
12735 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
12736 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
12737 54 45 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 65 72  TE_OK && zMaster
12738 5b 30 5d 20 26 26 20 72 65 73 20 29 7b 0a 20 20  [0] && res ){.  
12739 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 77 61    /* If there wa
1273a 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  s a master journ
1273b 61 6c 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74  al and this rout
1273c 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  ine will return 
1273d 73 75 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a 20  success,.    ** 
1273e 73 65 65 20 69 66 20 69 74 20 69 73 20 70 6f 73  see if it is pos
1273f 73 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20  sible to delete 
12740 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
12741 61 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  al..    */.    r
12742 63 20 3d 20 70 61 67 65 72 5f 64 65 6c 6d 61 73  c = pager_delmas
12743 74 65 72 28 70 50 61 67 65 72 2c 20 7a 4d 61 73  ter(pPager, zMas
12744 74 65 72 29 3b 0a 20 20 20 20 74 65 73 74 63 61  ter);.    testca
12745 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  se( rc!=SQLITE_O
12746 4b 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  K );.  }..  /* T
12747 68 65 20 50 61 67 65 72 2e 73 65 63 74 6f 72 53  he Pager.sectorS
12748 69 7a 65 20 76 61 72 69 61 62 6c 65 20 6d 61 79  ize variable may
12749 20 68 61 76 65 20 62 65 65 6e 20 75 70 64 61 74   have been updat
1274a 65 64 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e 67  ed while rolling
1274b 0a 20 20 2a 2a 20 62 61 63 6b 20 61 20 6a 6f 75  .  ** back a jou
1274c 72 6e 61 6c 20 63 72 65 61 74 65 64 20 62 79 20  rnal created by 
1274d 61 20 70 72 6f 63 65 73 73 20 77 69 74 68 20 61  a process with a
1274e 20 64 69 66 66 65 72 65 6e 74 20 73 65 63 74 6f   different secto
1274f 72 20 73 69 7a 65 0a 20 20 2a 2a 20 76 61 6c 75  r size.  ** valu
12750 65 2e 20 52 65 73 65 74 20 69 74 20 74 6f 20 74  e. Reset it to t
12751 68 65 20 63 6f 72 72 65 63 74 20 76 61 6c 75 65  he correct value
12752 20 66 6f 72 20 74 68 69 73 20 70 72 6f 63 65 73   for this proces
12753 73 2e 0a 20 20 2a 2f 0a 20 20 73 65 74 53 65 63  s..  */.  setSec
12754 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b  torSize(pPager);
12755 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
12756 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b 20  ./*.** Playback 
12757 73 61 76 65 70 6f 69 6e 74 20 70 53 61 76 65 70  savepoint pSavep
12758 6f 69 6e 74 2e 20 4f 72 2c 20 69 66 20 70 53 61  oint. Or, if pSa
12759 76 65 70 6f 69 6e 74 3d 3d 4e 55 4c 4c 2c 20 74  vepoint==NULL, t
1275a 68 65 6e 20 70 6c 61 79 62 61 63 6b 0a 2a 2a 20  hen playback.** 
1275b 74 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65  the entire maste
1275c 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  r journal file. 
1275d 54 68 65 20 63 61 73 65 20 70 53 61 76 65 70 6f  The case pSavepo
1275e 69 6e 74 3d 3d 4e 55 4c 4c 20 6f 63 63 75 72 73  int==NULL occurs
1275f 20 77 68 65 6e 20 0a 2a 2a 20 61 20 52 4f 4c 4c   when .** a ROLL
12760 42 41 43 4b 20 54 4f 20 63 6f 6d 6d 61 6e 64 20  BACK TO command 
12761 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 61 20  is invoked on a 
12762 53 41 56 45 50 4f 49 4e 54 20 74 68 61 74 20 69  SAVEPOINT that i
12763 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  s a transaction 
12764 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74 2e 0a 2a  .** savepoint..*
12765 2a 0a 2a 2a 20 57 68 65 6e 20 70 53 61 76 65 70  *.** When pSavep
12766 6f 69 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  oint is not NULL
12767 20 28 6d 65 61 6e 69 6e 67 20 61 20 6e 6f 6e 2d   (meaning a non-
12768 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65  transaction save
12769 70 6f 69 6e 74 20 69 73 20 0a 2a 2a 20 62 65 69  point is .** bei
1276a 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b 29 2c  ng rolled back),
1276b 20 74 68 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61   then the rollba
1276c 63 6b 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 75  ck consists of u
1276d 70 20 74 6f 20 74 68 72 65 65 20 73 74 61 67 65  p to three stage
1276e 73 2c 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20  s,.** performed 
1276f 69 6e 20 74 68 65 20 6f 72 64 65 72 20 73 70 65  in the order spe
12770 63 69 66 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20  cified:.**.**   
12771 2a 20 50 61 67 65 73 20 61 72 65 20 70 6c 61 79  * Pages are play
12772 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65  ed back from the
12773 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 74   main journal st
12774 61 72 74 69 6e 67 20 61 74 20 62 79 74 65 0a 2a  arting at byte.*
12775 2a 20 20 20 20 20 6f 66 66 73 65 74 20 50 61 67  *     offset Pag
12776 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 4f 66 66  erSavepoint.iOff
12777 73 65 74 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69  set and continui
12778 6e 67 20 74 6f 20 0a 2a 2a 20 20 20 20 20 50 61  ng to .**     Pa
12779 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64  gerSavepoint.iHd
1277a 72 4f 66 66 73 65 74 2c 20 6f 72 20 74 6f 20 74  rOffset, or to t
1277b 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61  he end of the ma
1277c 69 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20  in journal.**   
1277d 20 20 66 69 6c 65 20 69 66 20 50 61 67 65 72 53    file if PagerS
1277e 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66  avepoint.iHdrOff
1277f 73 65 74 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a  set is zero..**.
12780 2a 2a 20 20 20 2a 20 49 66 20 50 61 67 65 72 53  **   * If PagerS
12781 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66  avepoint.iHdrOff
12782 73 65 74 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c  set is not zero,
12783 20 74 68 65 6e 20 70 61 67 65 73 20 61 72 65 20   then pages are 
12784 70 6c 61 79 65 64 0a 2a 2a 20 20 20 20 20 62 61  played.**     ba
12785 63 6b 20 73 74 61 72 74 69 6e 67 20 66 72 6f 6d  ck starting from
12786 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61   the journal hea
12787 64 65 72 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  der immediately 
12788 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 20 20  following .**   
12789 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74    PagerSavepoint
1278a 2e 69 48 64 72 4f 66 66 73 65 74 20 74 6f 20 74  .iHdrOffset to t
1278b 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61  he end of the ma
1278c 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  in journal file.
1278d 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50 61 67 65 73  .**.**   * Pages
1278e 20 61 72 65 20 74 68 65 6e 20 70 6c 61 79 65 64   are then played
1278f 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 73   back from the s
12790 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c  ub-journal file,
12791 20 73 74 61 72 74 69 6e 67 0a 2a 2a 20 20 20 20   starting.**    
12792 20 77 69 74 68 20 74 68 65 20 50 61 67 65 72 53   with the PagerS
12793 61 76 65 70 6f 69 6e 74 2e 69 53 75 62 52 65 63  avepoint.iSubRec
12794 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20   and continuing 
12795 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 0a 2a 2a  to the end of.**
12796 20 20 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 6c       the journal
12797 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 72   file..**.** Thr
12798 6f 75 67 68 6f 75 74 20 74 68 65 20 72 6f 6c 6c  oughout the roll
12799 62 61 63 6b 20 70 72 6f 63 65 73 73 2c 20 65 61  back process, ea
1279a 63 68 20 74 69 6d 65 20 61 20 70 61 67 65 20 69  ch time a page i
1279b 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 74  s rolled back, t
1279c 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64  he.** correspond
1279d 69 6e 67 20 62 69 74 20 69 73 20 73 65 74 20 69  ing bit is set i
1279e 6e 20 61 20 62 69 74 76 65 63 20 73 74 72 75 63  n a bitvec struc
1279f 74 75 72 65 20 28 76 61 72 69 61 62 6c 65 20 70  ture (variable p
127a0 44 6f 6e 65 20 69 6e 20 74 68 65 0a 2a 2a 20 69  Done in the.** i
127a1 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 62 65  mplementation be
127a2 6c 6f 77 29 2e 20 54 68 69 73 20 69 73 20 75 73  low). This is us
127a3 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61  ed to ensure tha
127a4 74 20 61 20 70 61 67 65 20 69 73 20 6f 6e 6c 79  t a page is only
127a5 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20  .** rolled back 
127a6 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 69  the first time i
127a7 74 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  t is encountered
127a8 20 69 6e 20 65 69 74 68 65 72 20 6a 6f 75 72 6e   in either journ
127a9 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 53 61  al..**.** If pSa
127aa 76 65 70 6f 69 6e 74 20 69 73 20 4e 55 4c 4c 2c  vepoint is NULL,
127ab 20 74 68 65 6e 20 70 61 67 65 73 20 61 72 65 20   then pages are 
127ac 6f 6e 6c 79 20 70 6c 61 79 65 64 20 62 61 63 6b  only played back
127ad 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 0a 2a   from the main.*
127ae 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  * journal file. 
127af 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64  There is no need
127b0 20 66 6f 72 20 61 20 62 69 74 76 65 63 20 69 6e   for a bitvec in
127b1 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a   this case..**.*
127b2 2a 20 49 6e 20 65 69 74 68 65 72 20 63 61 73 65  * In either case
127b3 2c 20 62 65 66 6f 72 65 20 70 6c 61 79 62 61 63  , before playbac
127b4 6b 20 63 6f 6d 6d 65 6e 63 65 73 20 74 68 65 20  k commences the 
127b5 50 61 67 65 72 2e 64 62 53 69 7a 65 20 76 61 72  Pager.dbSize var
127b6 69 61 62 6c 65 0a 2a 2a 20 69 73 20 72 65 73 65  iable.** is rese
127b7 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 74  t to the value t
127b8 68 61 74 20 69 74 20 68 65 6c 64 20 61 74 20 74  hat it held at t
127b9 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
127ba 73 61 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 28 6f  savepoint .** (o
127bb 72 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20  r transaction). 
127bc 4e 6f 20 70 61 67 65 20 77 69 74 68 20 61 20 70  No page with a p
127bd 61 67 65 2d 6e 75 6d 62 65 72 20 67 72 65 61 74  age-number great
127be 65 72 20 74 68 61 6e 20 74 68 69 73 20 76 61 6c  er than this val
127bf 75 65 0a 2a 2a 20 69 73 20 70 6c 61 79 65 64 20  ue.** is played 
127c0 62 61 63 6b 2e 20 49 66 20 6f 6e 65 20 69 73 20  back. If one is 
127c1 65 6e 63 6f 75 6e 74 65 72 65 64 20 69 74 20 69  encountered it i
127c2 73 20 73 69 6d 70 6c 79 20 73 6b 69 70 70 65 64  s simply skipped
127c3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
127c4 70 61 67 65 72 50 6c 61 79 62 61 63 6b 53 61 76  pagerPlaybackSav
127c5 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50  epoint(Pager *pP
127c6 61 67 65 72 2c 20 50 61 67 65 72 53 61 76 65 70  ager, PagerSavep
127c7 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f 69 6e 74  oint *pSavepoint
127c8 29 7b 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20  ){.  i64 szJ;   
127c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
127ca 20 45 66 66 65 63 74 69 76 65 20 73 69 7a 65 20   Effective size 
127cb 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72  of the main jour
127cc 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 69 48 64  nal */.  i64 iHd
127cd 72 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20  rOff;           
127ce 20 20 2f 2a 20 45 6e 64 20 6f 66 20 66 69 72 73    /* End of firs
127cf 74 20 73 65 67 6d 65 6e 74 20 6f 66 20 6d 61 69  t segment of mai
127d0 6e 2d 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64  n-journal record
127d1 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20  s */.  int rc = 
127d2 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
127d3 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
127d4 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 44 6f 6e  /.  Bitvec *pDon
127d5 65 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20  e = 0;       /* 
127d6 42 69 74 76 65 63 20 74 6f 20 65 6e 73 75 72 65  Bitvec to ensure
127d7 20 70 61 67 65 73 20 70 6c 61 79 65 64 20 62 61   pages played ba
127d8 63 6b 20 6f 6e 6c 79 20 6f 6e 63 65 20 2a 2f 0a  ck only once */.
127d9 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
127da 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f  r->state>=PAGER_
127db 53 48 41 52 45 44 20 29 3b 0a 0a 20 20 2f 2a 20  SHARED );..  /* 
127dc 41 6c 6c 6f 63 61 74 65 20 61 20 62 69 74 76 65  Allocate a bitve
127dd 63 20 74 6f 20 75 73 65 20 74 6f 20 73 74 6f 72  c to use to stor
127de 65 20 74 68 65 20 73 65 74 20 6f 66 20 70 61 67  e the set of pag
127df 65 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 2a  es rolled back *
127e0 2f 0a 20 20 69 66 28 20 70 53 61 76 65 70 6f 69  /.  if( pSavepoi
127e1 6e 74 20 29 7b 0a 20 20 20 20 70 44 6f 6e 65 20  nt ){.    pDone 
127e2 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43  = sqlite3BitvecC
127e3 72 65 61 74 65 28 70 53 61 76 65 70 6f 69 6e 74  reate(pSavepoint
127e4 2d 3e 6e 4f 72 69 67 29 3b 0a 20 20 20 20 69 66  ->nOrig);.    if
127e5 28 20 21 70 44 6f 6e 65 20 29 7b 0a 20 20 20 20  ( !pDone ){.    
127e6 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
127e7 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d  NOMEM;.    }.  }
127e8 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 64  ..  /* Set the d
127e9 61 74 61 62 61 73 65 20 73 69 7a 65 20 62 61 63  atabase size bac
127ea 6b 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 69  k to the value i
127eb 74 20 77 61 73 20 62 65 66 6f 72 65 20 74 68 65  t was before the
127ec 20 73 61 76 65 70 6f 69 6e 74 20 0a 20 20 2a 2a   savepoint .  **
127ed 20 62 65 69 6e 67 20 72 65 76 65 72 74 65 64 20   being reverted 
127ee 77 61 73 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f  was opened..  */
127ef 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  .  pPager->dbSiz
127f0 65 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 20 3f  e = pSavepoint ?
127f1 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72   pSavepoint->nOr
127f2 69 67 20 3a 20 70 50 61 67 65 72 2d 3e 64 62 4f  ig : pPager->dbO
127f3 72 69 67 53 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55  rigSize;..  /* U
127f4 73 65 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  se pPager->journ
127f5 61 6c 4f 66 66 20 61 73 20 74 68 65 20 65 66 66  alOff as the eff
127f6 65 63 74 69 76 65 20 73 69 7a 65 20 6f 66 20 74  ective size of t
127f7 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b  he main rollback
127f8 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20  .  ** journal.  
127f9 54 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65 20  The actual file 
127fa 6d 69 67 68 74 20 62 65 20 6c 61 72 67 65 72 20  might be larger 
127fb 74 68 61 6e 20 74 68 69 73 20 69 6e 0a 20 20 2a  than this in.  *
127fc 2a 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  * PAGER_JOURNALM
127fd 4f 44 45 5f 54 52 55 4e 43 41 54 45 20 6f 72 20  ODE_TRUNCATE or 
127fe 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
127ff 45 5f 50 45 52 53 49 53 54 2e 20 20 42 75 74 20  E_PERSIST.  But 
12800 61 6e 79 74 68 69 6e 67 0a 20 20 2a 2a 20 70 61  anything.  ** pa
12801 73 74 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  st pPager->journ
12802 61 6c 4f 66 66 20 69 73 20 6f 66 66 2d 6c 69 6d  alOff is off-lim
12803 69 74 73 20 74 6f 20 75 73 2e 0a 20 20 2a 2f 0a  its to us..  */.
12804 20 20 73 7a 4a 20 3d 20 70 50 61 67 65 72 2d 3e    szJ = pPager->
12805 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f  journalOff;..  /
12806 2a 20 42 65 67 69 6e 20 62 79 20 72 6f 6c 6c 69  * Begin by rolli
12807 6e 67 20 62 61 63 6b 20 72 65 63 6f 72 64 73 20  ng back records 
12808 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f  from the main jo
12809 75 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 61  urnal starting a
1280a 74 0a 20 20 2a 2a 20 50 61 67 65 72 53 61 76 65  t.  ** PagerSave
1280b 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 61 6e  point.iOffset an
1280c 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20  d continuing to 
1280d 74 68 65 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c  the next journal
1280e 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a 20 54 68   header..  ** Th
1280f 65 72 65 20 6d 69 67 68 74 20 62 65 20 72 65 63  ere might be rec
12810 6f 72 64 73 20 69 6e 20 74 68 65 20 6d 61 69 6e  ords in the main
12811 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74 20 68 61   journal that ha
12812 76 65 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72  ve a page number
12813 0a 20 20 2a 2a 20 67 72 65 61 74 65 72 20 74 68  .  ** greater th
12814 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 64  an the current d
12815 61 74 61 62 61 73 65 20 73 69 7a 65 20 28 70 50  atabase size (pP
12816 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 20 62 75  ager->dbSize) bu
12817 74 20 74 68 6f 73 65 0a 20 20 2a 2a 20 77 69 6c  t those.  ** wil
12818 6c 20 62 65 20 73 6b 69 70 70 65 64 20 61 75 74  l be skipped aut
12819 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 50 61 67  omatically.  Pag
1281a 65 73 20 61 72 65 20 61 64 64 65 64 20 74 6f 20  es are added to 
1281b 70 44 6f 6e 65 20 61 73 20 74 68 65 79 0a 20 20  pDone as they.  
1281c 2a 2a 20 61 72 65 20 70 6c 61 79 65 64 20 62 61  ** are played ba
1281d 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ck..  */.  if( p
1281e 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20  Savepoint ){.   
1281f 20 69 48 64 72 4f 66 66 20 3d 20 70 53 61 76 65   iHdrOff = pSave
12820 70 6f 69 6e 74 2d 3e 69 48 64 72 4f 66 66 73 65  point->iHdrOffse
12821 74 20 3f 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e  t ? pSavepoint->
12822 69 48 64 72 4f 66 66 73 65 74 20 3a 20 73 7a 4a  iHdrOffset : szJ
12823 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f  ;.    pPager->jo
12824 75 72 6e 61 6c 4f 66 66 20 3d 20 70 53 61 76 65  urnalOff = pSave
12825 70 6f 69 6e 74 2d 3e 69 4f 66 66 73 65 74 3b 0a  point->iOffset;.
12826 20 20 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53      while( rc==S
12827 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67  QLITE_OK && pPag
12828 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 69  er->journalOff<i
12829 48 64 72 4f 66 66 20 29 7b 0a 20 20 20 20 20 20  HdrOff ){.      
1282a 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62  rc = pager_playb
1282b 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61  ack_one_page(pPa
1282c 67 65 72 2c 20 31 2c 20 30 2c 20 26 70 50 61 67  ger, 1, 0, &pPag
1282d 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20  er->journalOff, 
1282e 31 2c 20 70 44 6f 6e 65 29 3b 0a 20 20 20 20 7d  1, pDone);.    }
1282f 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21  .    assert( rc!
12830 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a  =SQLITE_DONE );.
12831 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61    }else{.    pPa
12832 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
12833 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  = 0;.  }..  /* C
12834 6f 6e 74 69 6e 75 65 20 72 6f 6c 6c 69 6e 67 20  ontinue rolling 
12835 62 61 63 6b 20 72 65 63 6f 72 64 73 20 6f 75 74  back records out
12836 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75   of the main jou
12837 72 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 61 74  rnal starting at
12838 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20  .  ** the first 
12839 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73  journal header s
1283a 65 65 6e 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69  een and continui
1283b 6e 67 20 75 6e 74 69 6c 20 74 68 65 20 65 66 66  ng until the eff
1283c 65 63 74 69 76 65 20 65 6e 64 0a 20 20 2a 2a 20  ective end.  ** 
1283d 6f 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72  of the main jour
1283e 6e 61 6c 20 66 69 6c 65 2e 20 20 43 6f 6e 74 69  nal file.  Conti
1283f 6e 75 65 20 74 6f 20 73 6b 69 70 20 6f 75 74 2d  nue to skip out-
12840 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 73 20 61  of-range pages a
12841 6e 64 0a 20 20 2a 2a 20 63 6f 6e 74 69 6e 75 65  nd.  ** continue
12842 20 61 64 64 69 6e 67 20 70 61 67 65 73 20 72 6f   adding pages ro
12843 6c 6c 65 64 20 62 61 63 6b 20 74 6f 20 70 44 6f  lled back to pDo
12844 6e 65 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65  ne..  */.  while
12845 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
12846 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  && pPager->journ
12847 61 6c 4f 66 66 3c 73 7a 4a 20 29 7b 0a 20 20 20  alOff<szJ ){.   
12848 20 75 33 32 20 69 69 3b 20 20 20 20 20 20 20 20   u32 ii;        
12849 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
1284a 74 65 72 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e  ter */.    u32 n
1284b 4a 52 65 63 20 3d 20 30 3b 20 20 20 20 20 2f 2a  JRec = 0;     /*
1284c 20 4e 75 6d 62 65 72 20 6f 66 20 4a 6f 75 72 6e   Number of Journ
1284d 61 6c 20 52 65 63 6f 72 64 73 20 2a 2f 0a 20 20  al Records */.  
1284e 20 20 75 33 32 20 64 75 6d 6d 79 3b 0a 20 20 20    u32 dummy;.   
1284f 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61   rc = readJourna
12850 6c 48 64 72 28 70 50 61 67 65 72 2c 20 30 2c 20  lHdr(pPager, 0, 
12851 73 7a 4a 2c 20 26 6e 4a 52 65 63 2c 20 26 64 75  szJ, &nJRec, &du
12852 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74  mmy);.    assert
12853 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc!=SQLITE_DON
12854 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 0a 20 20 20  E );..    /*.   
12855 20 2a 2a 20 54 68 65 20 22 70 50 61 67 65 72 2d   ** The "pPager-
12856 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52  >journalHdr+JOUR
12857 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
12858 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72  r)==pPager->jour
12859 6e 61 6c 4f 66 66 22 0a 20 20 20 20 2a 2a 20 74  nalOff".    ** t
1285a 65 73 74 20 69 73 20 72 65 6c 61 74 65 64 20 74  est is related t
1285b 6f 20 74 69 63 6b 65 74 20 23 32 35 36 35 2e 20  o ticket #2565. 
1285c 20 53 65 65 20 74 68 65 20 64 69 73 63 75 73 73   See the discuss
1285d 69 6f 6e 20 69 6e 20 74 68 65 0a 20 20 20 20 2a  ion in the.    *
1285e 2a 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  * pager_playback
1285f 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20  () function for 
12860 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
12861 6d 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20  mation..    */. 
12862 20 20 20 69 66 28 20 6e 4a 52 65 63 3d 3d 30 20     if( nJRec==0 
12863 0a 20 20 20 20 20 26 26 20 70 50 61 67 65 72 2d  .     && pPager-
12864 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52  >journalHdr+JOUR
12865 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
12866 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72  r)==pPager->jour
12867 6e 61 6c 4f 66 66 0a 20 20 20 20 29 7b 0a 20 20  nalOff.    ){.  
12868 20 20 20 20 6e 4a 52 65 63 20 3d 20 28 75 33 32      nJRec = (u32
12869 29 28 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 2d  )((szJ - pPager-
1286a 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 2f 4a 4f 55  >journalOff)/JOU
1286b 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65  RNAL_PG_SZ(pPage
1286c 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  r));.    }.    f
1286d 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c  or(ii=0; rc==SQL
1286e 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e 4a 52  ITE_OK && ii<nJR
1286f 65 63 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f  ec && pPager->jo
12870 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 3b 20 69 69  urnalOff<szJ; ii
12871 2b 2b 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  ++){.      rc = 
12872 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f  pager_playback_o
12873 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c 20  ne_page(pPager, 
12874 31 2c 20 30 2c 20 26 70 50 61 67 65 72 2d 3e 6a  1, 0, &pPager->j
12875 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 44  ournalOff, 1, pD
12876 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  one);.    }.    
12877 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
12878 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 20  TE_DONE );.  }. 
12879 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
1287a 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72  ITE_OK || pPager
1287b 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 73 7a  ->journalOff==sz
1287c 4a 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 61 6c  J );..  /* Final
1287d 6c 79 2c 20 20 72 6f 6c 6c 62 61 63 6b 20 70 61  ly,  rollback pa
1287e 67 65 73 20 66 72 6f 6d 20 74 68 65 20 73 75 62  ges from the sub
1287f 2d 6a 6f 75 72 6e 61 6c 2e 20 20 50 61 67 65 20  -journal.  Page 
12880 74 68 61 74 20 77 65 72 65 0a 20 20 2a 2a 20 70  that were.  ** p
12881 72 65 76 69 6f 75 73 6c 79 20 72 6f 6c 6c 65 64  reviously rolled
12882 20 62 61 63 6b 20 6f 75 74 20 6f 66 20 74 68 65   back out of the
12883 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 28 61   main journal (a
12884 6e 64 20 61 72 65 20 68 65 6e 63 65 20 69 6e 20  nd are hence in 
12885 70 44 6f 6e 65 29 0a 20 20 2a 2a 20 77 69 6c 6c  pDone).  ** will
12886 20 62 65 20 73 6b 69 70 70 65 64 2e 20 20 4f 75   be skipped.  Ou
12887 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 73  t-of-range pages
12888 20 61 72 65 20 61 6c 73 6f 20 73 6b 69 70 70 65   are also skippe
12889 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53  d..  */.  if( pS
1288a 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20  avepoint ){.    
1288b 75 33 32 20 69 69 3b 20 20 20 20 20 20 20 20 20  u32 ii;         
1288c 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
1288d 65 72 20 2a 2f 0a 20 20 20 20 69 36 34 20 6f 66  er */.    i64 of
1288e 66 73 65 74 20 3d 20 70 53 61 76 65 70 6f 69 6e  fset = pSavepoin
1288f 74 2d 3e 69 53 75 62 52 65 63 2a 28 34 2b 70 50  t->iSubRec*(4+pP
12890 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b  ager->pageSize);
12891 0a 20 20 20 20 66 6f 72 28 69 69 3d 70 53 61 76  .    for(ii=pSav
12892 65 70 6f 69 6e 74 2d 3e 69 53 75 62 52 65 63 3b  epoint->iSubRec;
12893 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
12894 26 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 75  & ii<pPager->nSu
12895 62 52 65 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  bRec; ii++){.   
12896 20 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65     assert( offse
12897 74 3d 3d 69 69 2a 28 34 2b 70 50 61 67 65 72 2d  t==ii*(4+pPager-
12898 3e 70 61 67 65 53 69 7a 65 29 20 29 3b 0a 20 20  >pageSize) );.  
12899 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70      rc = pager_p
1289a 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65  layback_one_page
1289b 28 70 50 61 67 65 72 2c 20 30 2c 20 30 2c 20 26  (pPager, 0, 0, &
1289c 6f 66 66 73 65 74 2c 20 31 2c 20 70 44 6f 6e 65  offset, 1, pDone
1289d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  );.    }.    ass
1289e 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
1289f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 0a 20 20 73  DONE );.  }..  s
128a0 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74  qlite3BitvecDest
128a1 72 6f 79 28 70 44 6f 6e 65 29 3b 0a 20 20 69 66  roy(pDone);.  if
128a2 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
128a3 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a  ){.    pPager->j
128a4 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b  ournalOff = szJ;
128a5 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
128a6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  ;.}../*.** Chang
128a7 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  e the maximum nu
128a8 6d 62 65 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72  mber of in-memor
128a9 79 20 70 61 67 65 73 20 74 68 61 74 20 61 72 65  y pages that are
128aa 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2f 0a 53 51 4c   allowed..*/.SQL
128ab 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
128ac 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
128ad 43 61 63 68 65 73 69 7a 65 28 50 61 67 65 72 20  Cachesize(Pager 
128ae 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6d 78 50  *pPager, int mxP
128af 61 67 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 50  age){.  sqlite3P
128b0 63 61 63 68 65 53 65 74 43 61 63 68 65 73 69 7a  cacheSetCachesiz
128b1 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  e(pPager->pPCach
128b2 65 2c 20 6d 78 50 61 67 65 29 3b 0a 7d 0a 0a 2f  e, mxPage);.}../
128b3 2a 0a 2a 2a 20 41 64 6a 75 73 74 20 74 68 65 20  *.** Adjust the 
128b4 72 6f 62 75 73 74 6e 65 73 73 20 6f 66 20 74 68  robustness of th
128b5 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 64 61  e database to da
128b6 6d 61 67 65 20 64 75 65 20 74 6f 20 4f 53 20 63  mage due to OS c
128b7 72 61 73 68 65 73 0a 2a 2a 20 6f 72 20 70 6f 77  rashes.** or pow
128b8 65 72 20 66 61 69 6c 75 72 65 73 20 62 79 20 63  er failures by c
128b9 68 61 6e 67 69 6e 67 20 74 68 65 20 6e 75 6d 62  hanging the numb
128ba 65 72 20 6f 66 20 73 79 6e 63 73 28 29 73 20 77  er of syncs()s w
128bb 68 65 6e 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74  hen writing.** t
128bc 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  he rollback jour
128bd 6e 61 6c 2e 20 20 54 68 65 72 65 20 61 72 65 20  nal.  There are 
128be 74 68 72 65 65 20 6c 65 76 65 6c 73 3a 0a 2a 2a  three levels:.**
128bf 0a 2a 2a 20 20 20 20 4f 46 46 20 20 20 20 20 20  .**    OFF      
128c0 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 29   sqlite3OsSync()
128c1 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64   is never called
128c2 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 64  .  This is the d
128c3 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20  efault.**       
128c4 20 20 20 20 20 20 20 66 6f 72 20 74 65 6d 70 6f         for tempo
128c5 72 61 72 79 20 61 6e 64 20 74 72 61 6e 73 69 65  rary and transie
128c6 6e 74 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  nt files..**.** 
128c7 20 20 20 4e 4f 52 4d 41 4c 20 20 20 20 54 68 65     NORMAL    The
128c8 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63   journal is sync
128c9 65 64 20 6f 6e 63 65 20 62 65 66 6f 72 65 20 77  ed once before w
128ca 72 69 74 65 73 20 62 65 67 69 6e 20 6f 6e 20 74  rites begin on t
128cb 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  he.**           
128cc 20 20 20 64 61 74 61 62 61 73 65 2e 20 20 54 68     database.  Th
128cd 69 73 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 61  is is normally a
128ce 64 65 71 75 61 74 65 20 70 72 6f 74 65 63 74 69  dequate protecti
128cf 6f 6e 2c 20 62 75 74 0a 2a 2a 20 20 20 20 20 20  on, but.**      
128d0 20 20 20 20 20 20 20 20 69 74 20 69 73 20 74 68          it is th
128d1 65 6f 72 65 74 69 63 61 6c 6c 79 20 70 6f 73 73  eoretically poss
128d2 69 62 6c 65 2c 20 74 68 6f 75 67 68 20 76 65 72  ible, though ver
128d3 79 20 75 6e 6c 69 6b 65 6c 79 2c 0a 2a 2a 20 20  y unlikely,.**  
128d4 20 20 20 20 20 20 20 20 20 20 20 20 74 68 61 74              that
128d5 20 61 6e 20 69 6e 6f 70 65 72 74 75 6e 65 20 70   an inopertune p
128d6 6f 77 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75  ower failure cou
128d7 6c 64 20 6c 65 61 76 65 20 74 68 65 20 6a 6f 75  ld leave the jou
128d8 72 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20  rnal.**         
128d9 20 20 20 20 20 69 6e 20 61 20 73 74 61 74 65 20       in a state 
128da 77 68 69 63 68 20 77 6f 75 6c 64 20 63 61 75 73  which would caus
128db 65 20 64 61 6d 61 67 65 20 74 6f 20 74 68 65 20  e damage to the 
128dc 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20  database.**     
128dd 20 20 20 20 20 20 20 20 20 77 68 65 6e 20 69 74           when it
128de 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e   is rolled back.
128df 0a 2a 2a 0a 2a 2a 20 20 20 20 46 55 4c 4c 20 20  .**.**    FULL  
128e0 20 20 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20      The journal 
128e1 69 73 20 73 79 6e 63 65 64 20 74 77 69 63 65 20  is synced twice 
128e2 62 65 66 6f 72 65 20 77 72 69 74 65 73 20 62 65  before writes be
128e3 67 69 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 20 20  gin on the.**   
128e4 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61 62             datab
128e5 61 73 65 20 28 77 69 74 68 20 73 6f 6d 65 20 61  ase (with some a
128e6 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
128e7 61 74 69 6f 6e 20 2d 20 74 68 65 20 6e 52 65 63  ation - the nRec
128e8 20 66 69 65 6c 64 0a 2a 2a 20 20 20 20 20 20 20   field.**       
128e9 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 6a 6f         of the jo
128ea 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2d 20 62  urnal header - b
128eb 65 69 6e 67 20 77 72 69 74 74 65 6e 20 69 6e 20  eing written in 
128ec 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 0a  between the two.
128ed 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
128ee 73 79 6e 63 73 29 2e 20 20 49 66 20 77 65 20 61  syncs).  If we a
128ef 73 73 75 6d 65 20 74 68 61 74 20 77 72 69 74 69  ssume that writi
128f0 6e 67 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20  ng a.**         
128f1 20 20 20 20 20 73 69 6e 67 6c 65 20 64 69 73 6b       single disk
128f2 20 73 65 63 74 6f 72 20 69 73 20 61 74 6f 6d 69   sector is atomi
128f3 63 2c 20 74 68 65 6e 20 74 68 69 73 20 6d 6f 64  c, then this mod
128f4 65 20 70 72 6f 76 69 64 65 73 0a 2a 2a 20 20 20  e provides.**   
128f5 20 20 20 20 20 20 20 20 20 20 20 61 73 73 75 72             assur
128f6 61 6e 63 65 20 74 68 61 74 20 74 68 65 20 6a 6f  ance that the jo
128f7 75 72 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 62  urnal will not b
128f8 65 20 63 6f 72 72 75 70 74 65 64 20 74 6f 20 74  e corrupted to t
128f9 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  he.**           
128fa 20 20 20 70 6f 69 6e 74 20 6f 66 20 63 61 75 73     point of caus
128fb 69 6e 67 20 64 61 6d 61 67 65 20 74 6f 20 74 68  ing damage to th
128fc 65 20 64 61 74 61 62 61 73 65 20 64 75 72 69 6e  e database durin
128fd 67 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a  g rollback..**.*
128fe 2a 20 4e 75 6d 65 72 69 63 20 76 61 6c 75 65 73  * Numeric values
128ff 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
12900 20 74 68 65 73 65 20 73 74 61 74 65 73 20 61 72   these states ar
12901 65 20 4f 46 46 3d 3d 31 2c 20 4e 4f 52 4d 41 4c  e OFF==1, NORMAL
12902 3d 32 2c 0a 2a 2a 20 61 6e 64 20 46 55 4c 4c 3d  =2,.** and FULL=
12903 33 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  3..*/.#ifndef SQ
12904 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
12905 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f 50  PRAGMAS.SQLITE_P
12906 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
12907 74 65 33 50 61 67 65 72 53 65 74 53 61 66 65 74  te3PagerSetSafet
12908 79 4c 65 76 65 6c 28 50 61 67 65 72 20 2a 70 50  yLevel(Pager *pP
12909 61 67 65 72 2c 20 69 6e 74 20 6c 65 76 65 6c 2c  ager, int level,
1290a 20 69 6e 74 20 62 46 75 6c 6c 46 73 79 6e 63 29   int bFullFsync)
1290b 7b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79  {.  pPager->noSy
1290c 6e 63 20 3d 20 20 28 6c 65 76 65 6c 3d 3d 31 20  nc =  (level==1 
1290d 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  || pPager->tempF
1290e 69 6c 65 29 20 3f 31 3a 30 3b 0a 20 20 70 50 61  ile) ?1:0;.  pPa
1290f 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20  ger->fullSync = 
12910 28 6c 65 76 65 6c 3d 3d 33 20 26 26 20 21 70 50  (level==3 && !pP
12911 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 20  ager->tempFile) 
12912 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e  ?1:0;.  pPager->
12913 73 79 6e 63 5f 66 6c 61 67 73 20 3d 20 28 62 46  sync_flags = (bF
12914 75 6c 6c 46 73 79 6e 63 3f 53 51 4c 49 54 45 5f  ullFsync?SQLITE_
12915 53 59 4e 43 5f 46 55 4c 4c 3a 53 51 4c 49 54 45  SYNC_FULL:SQLITE
12916 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 3b 0a 20  _SYNC_NORMAL);. 
12917 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53   if( pPager->noS
12918 79 6e 63 20 29 20 70 50 61 67 65 72 2d 3e 6e 65  ync ) pPager->ne
12919 65 64 53 79 6e 63 20 3d 20 30 3b 0a 7d 0a 23 65  edSync = 0;.}.#e
1291a 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
1291b 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c  following global
1291c 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63   variable is inc
1291d 72 65 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65  remented wheneve
1291e 72 20 74 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a  r the library.**
1291f 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6f 70 65   attempts to ope
12920 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69  n a temporary fi
12921 6c 65 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d  le.  This inform
12922 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f  ation is used fo
12923 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64  r.** testing and
12924 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 20   analysis only. 
12925 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49   .*/.#ifdef SQLI
12926 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
12927 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
12928 70 65 6e 74 65 6d 70 5f 63 6f 75 6e 74 20 3d 20  pentemp_count = 
12929 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0;.#endif../*.**
1292a 20 4f 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 72   Open a temporar
1292b 79 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 72  y file..**.** Wr
1292c 69 74 65 20 74 68 65 20 66 69 6c 65 20 64 65 73  ite the file des
1292d 63 72 69 70 74 6f 72 20 69 6e 74 6f 20 2a 70 46  criptor into *pF
1292e 69 6c 65 2e 20 52 65 74 75 72 6e 20 53 51 4c 49  ile. Return SQLI
1292f 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73  TE_OK on success
12930 20 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68   .** or some oth
12931 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66  er error code if
12932 20 77 65 20 66 61 69 6c 2e 20 54 68 65 20 4f 53   we fail. The OS
12933 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61   will automatica
12934 6c 6c 79 20 0a 2a 2a 20 64 65 6c 65 74 65 20 74  lly .** delete t
12935 68 65 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  he temporary fil
12936 65 20 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f  e when it is clo
12937 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  sed..**.** The f
12938 6c 61 67 73 20 70 61 73 73 65 64 20 74 6f 20 74  lags passed to t
12939 68 65 20 56 46 53 20 6c 61 79 65 72 20 78 4f 70  he VFS layer xOp
1293a 65 6e 28 29 20 63 61 6c 6c 20 61 72 65 20 74 68  en() call are th
1293b 6f 73 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  ose specified.**
1293c 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 76 66   by parameter vf
1293d 73 46 6c 61 67 73 20 4f 52 65 64 20 77 69 74 68  sFlags ORed with
1293e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
1293f 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  **.**     SQLITE
12940 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 0a  _OPEN_READWRITE.
12941 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50  **     SQLITE_OP
12942 45 4e 5f 43 52 45 41 54 45 0a 2a 2a 20 20 20 20  EN_CREATE.**    
12943 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43   SQLITE_OPEN_EXC
12944 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20 53 51  LUSIVE.**     SQ
12945 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45  LITE_OPEN_DELETE
12946 4f 4e 43 4c 4f 53 45 0a 2a 2f 0a 73 74 61 74 69  ONCLOSE.*/.stati
12947 63 20 69 6e 74 20 70 61 67 65 72 4f 70 65 6e 74  c int pagerOpent
12948 65 6d 70 28 0a 20 20 50 61 67 65 72 20 2a 70 50  emp(.  Pager *pP
12949 61 67 65 72 2c 20 20 20 20 20 20 20 20 2f 2a 20  ager,        /* 
1294a 54 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74  The pager object
1294b 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69   */.  sqlite3_fi
1294c 6c 65 20 2a 70 46 69 6c 65 2c 20 20 2f 2a 20 57  le *pFile,  /* W
1294d 72 69 74 65 20 74 68 65 20 66 69 6c 65 20 64 65  rite the file de
1294e 73 63 72 69 70 74 6f 72 20 68 65 72 65 20 2a 2f  scriptor here */
1294f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20  .  int vfsFlags 
12950 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67           /* Flag
12951 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68  s passed through
12952 20 74 6f 20 74 68 65 20 56 46 53 20 2a 2f 0a 29   to the VFS */.)
12953 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  {.  int rc;     
12954 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
12955 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 23 69 66  urn code */..#if
12956 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
12957 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 65    sqlite3_opente
12958 6d 70 5f 63 6f 75 6e 74 2b 2b 3b 20 20 2f 2a 20  mp_count++;  /* 
12959 55 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  Used for testing
1295a 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e   and analysis on
1295b 6c 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20  ly */.#endif..  
1295c 76 66 73 46 6c 61 67 73 20 7c 3d 20 20 53 51 4c  vfsFlags |=  SQL
1295d 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
1295e 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e  TE | SQLITE_OPEN
1295f 5f 43 52 45 41 54 45 20 7c 0a 20 20 20 20 20 20  _CREATE |.      
12960 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
12961 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c 20 53 51  N_EXCLUSIVE | SQ
12962 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45  LITE_OPEN_DELETE
12963 4f 4e 43 4c 4f 53 45 3b 0a 20 20 72 63 20 3d 20  ONCLOSE;.  rc = 
12964 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 50  sqlite3OsOpen(pP
12965 61 67 65 72 2d 3e 70 56 66 73 2c 20 30 2c 20 70  ager->pVfs, 0, p
12966 46 69 6c 65 2c 20 76 66 73 46 6c 61 67 73 2c 20  File, vfsFlags, 
12967 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  0);.  assert( rc
12968 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69  !=SQLITE_OK || i
12969 73 4f 70 65 6e 28 70 46 69 6c 65 29 20 29 3b 0a  sOpen(pFile) );.
1296a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1296b 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 75  /*.** Set the bu
1296c 73 79 20 68 61 6e 64 6c 65 72 20 66 75 6e 63 74  sy handler funct
1296d 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ion..**.** The p
1296e 61 67 65 72 20 69 6e 76 6f 6b 65 73 20 74 68 65  ager invokes the
1296f 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 69 66   busy-handler if
12970 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29   sqlite3OsLock()
12971 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20 53 51 4c   returns .** SQL
12972 49 54 45 5f 42 55 53 59 20 77 68 65 6e 20 74 72  ITE_BUSY when tr
12973 79 69 6e 67 20 74 6f 20 75 70 67 72 61 64 65 20  ying to upgrade 
12974 66 72 6f 6d 20 6e 6f 2d 6c 6f 63 6b 20 74 6f 20  from no-lock to 
12975 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 0a 2a  a SHARED lock,.*
12976 2a 20 6f 72 20 77 68 65 6e 20 74 72 79 69 6e 67  * or when trying
12977 20 74 6f 20 75 70 67 72 61 64 65 20 66 72 6f 6d   to upgrade from
12978 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
12979 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45   to an EXCLUSIVE
1297a 20 0a 2a 2a 20 6c 6f 63 6b 2e 20 49 74 20 64 6f   .** lock. It do
1297b 65 73 20 2a 6e 6f 74 2a 20 69 6e 76 6f 6b 65 20  es *not* invoke 
1297c 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72  the busy handler
1297d 20 77 68 65 6e 20 75 70 67 72 61 64 69 6e 67 20   when upgrading 
1297e 66 72 6f 6d 0a 2a 2a 20 53 48 41 52 45 44 20 74  from.** SHARED t
1297f 6f 20 52 45 53 45 52 56 45 44 2c 20 6f 72 20 77  o RESERVED, or w
12980 68 65 6e 20 75 70 67 72 61 64 69 6e 67 20 66 72  hen upgrading fr
12981 6f 6d 20 53 48 41 52 45 44 20 74 6f 20 45 58 43  om SHARED to EXC
12982 4c 55 53 49 56 45 0a 2a 2a 20 28 77 68 69 63 68  LUSIVE.** (which
12983 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 68   occurs during h
12984 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62  ot-journal rollb
12985 61 63 6b 29 2e 20 53 75 6d 6d 61 72 79 3a 0a 2a  ack). Summary:.*
12986 2a 0a 2a 2a 20 20 20 54 72 61 6e 73 69 74 69 6f  *.**   Transitio
12987 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
12988 20 20 20 20 20 20 20 20 20 7c 20 49 6e 76 6f 6b           | Invok
12989 65 73 20 78 42 75 73 79 48 61 6e 64 6c 65 72 0a  es xBusyHandler.
1298a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **   -----------
1298b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1298c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1298d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
1298e 20 20 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20 20     NO_LOCK      
1298f 20 2d 3e 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20   -> SHARED_LOCK 
12990 20 20 20 20 20 7c 20 59 65 73 0a 2a 2a 20 20 20       | Yes.**   
12991 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e  SHARED_LOCK   ->
12992 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 20   RESERVED_LOCK  
12993 20 20 7c 20 4e 6f 0a 2a 2a 20 20 20 53 48 41 52    | No.**   SHAR
12994 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 45 58 43  ED_LOCK   -> EXC
12995 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 20 7c 20  LUSIVE_LOCK   | 
12996 4e 6f 0a 2a 2a 20 20 20 52 45 53 45 52 56 45 44  No.**   RESERVED
12997 5f 4c 4f 43 4b 20 2d 3e 20 45 58 43 4c 55 53 49  _LOCK -> EXCLUSI
12998 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 59 65 73 0a  VE_LOCK   | Yes.
12999 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 73  **.** If the bus
1299a 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61  y-handler callba
1299b 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a  ck returns non-z
1299c 65 72 6f 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73  ero, the lock is
1299d 20 0a 2a 2a 20 72 65 74 72 69 65 64 2e 20 49 66   .** retried. If
1299e 20 69 74 20 72 65 74 75 72 6e 73 20 7a 65 72 6f   it returns zero
1299f 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c 49 54  , then the SQLIT
129a0 45 5f 42 55 53 59 20 65 72 72 6f 72 20 69 73 0a  E_BUSY error is.
129a1 2a 2a 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74  ** returned to t
129a2 68 65 20 63 61 6c 6c 65 72 20 6f 66 20 74 68 65  he caller of the
129a3 20 70 61 67 65 72 20 41 50 49 20 66 75 6e 63 74   pager API funct
129a4 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
129a5 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
129a6 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68  te3PagerSetBusyh
129a7 61 6e 64 6c 65 72 28 0a 20 20 50 61 67 65 72 20  andler(.  Pager 
129a8 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 20  *pPager,        
129a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
129aa 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a  * Pager object *
129ab 2f 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 79 48  /.  int (*xBusyH
129ac 61 6e 64 6c 65 72 29 28 76 6f 69 64 20 2a 29 2c  andler)(void *),
129ad 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
129ae 74 65 72 20 74 6f 20 62 75 73 79 2d 68 61 6e 64  ter to busy-hand
129af 6c 65 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ler function */.
129b0 20 20 76 6f 69 64 20 2a 70 42 75 73 79 48 61 6e    void *pBusyHan
129b1 64 6c 65 72 41 72 67 20 20 20 20 20 20 20 20 20  dlerArg         
129b2 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
129b3 6e 74 20 74 6f 20 70 61 73 73 20 74 6f 20 78 42  nt to pass to xB
129b4 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a 29 7b  usyHandler */.){
129b5 20 20 0a 20 20 70 50 61 67 65 72 2d 3e 78 42 75    .  pPager->xBu
129b6 73 79 48 61 6e 64 6c 65 72 20 3d 20 78 42 75 73  syHandler = xBus
129b7 79 48 61 6e 64 6c 65 72 3b 0a 20 20 70 50 61 67  yHandler;.  pPag
129b8 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72  er->pBusyHandler
129b9 41 72 67 20 3d 20 70 42 75 73 79 48 61 6e 64 6c  Arg = pBusyHandl
129ba 65 72 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  erArg;.}../*.** 
129bb 52 65 70 6f 72 74 20 74 68 65 20 63 75 72 72 65  Report the curre
129bc 6e 74 20 70 61 67 65 20 73 69 7a 65 20 61 6e 64  nt page size and
129bd 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72   number of reser
129be 76 65 64 20 62 79 74 65 73 20 62 61 63 6b 0a 2a  ved bytes back.*
129bf 2a 20 74 6f 20 74 68 65 20 63 6f 64 65 63 2e 0a  * to the codec..
129c0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
129c1 5f 48 41 53 5f 43 4f 44 45 43 0a 73 74 61 74 69  _HAS_CODEC.stati
129c2 63 20 76 6f 69 64 20 70 61 67 65 72 52 65 70 6f  c void pagerRepo
129c3 72 74 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50  rtSize(Pager *pP
129c4 61 67 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61  ager){.  if( pPa
129c5 67 65 72 2d 3e 78 43 6f 64 65 63 53 69 7a 65 43  ger->xCodecSizeC
129c6 68 6e 67 20 29 7b 0a 20 20 20 20 70 50 61 67 65  hng ){.    pPage
129c7 72 2d 3e 78 43 6f 64 65 63 53 69 7a 65 43 68 6e  r->xCodecSizeChn
129c8 67 28 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63  g(pPager->pCodec
129c9 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
129ca 7a 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ze,.            
129cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
129cc 69 6e 74 29 70 50 61 67 65 72 2d 3e 6e 52 65 73  int)pPager->nRes
129cd 65 72 76 65 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c  erve);.  }.}.#el
129ce 73 65 0a 23 20 64 65 66 69 6e 65 20 70 61 67 65  se.# define page
129cf 72 52 65 70 6f 72 74 53 69 7a 65 28 58 29 20 20  rReportSize(X)  
129d0 20 20 20 2f 2a 20 4e 6f 2d 6f 70 20 69 66 20 77     /* No-op if w
129d1 65 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74  e do not support
129d2 20 61 20 63 6f 64 65 63 20 2a 2f 0a 23 65 6e 64   a codec */.#end
129d3 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  if../*.** Change
129d4 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 75   the page size u
129d5 73 65 64 20 62 79 20 74 68 65 20 50 61 67 65 72  sed by the Pager
129d6 20 6f 62 6a 65 63 74 2e 20 54 68 65 20 6e 65 77   object. The new
129d7 20 70 61 67 65 20 73 69 7a 65 20 0a 2a 2a 20 69   page size .** i
129d8 73 20 70 61 73 73 65 64 20 69 6e 20 2a 70 50 61  s passed in *pPa
129d9 67 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  geSize..**.** If
129da 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
129db 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
129dc 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
129dd 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 69  ion is called, i
129de 74 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  t.** is a no-op.
129df 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   The value retur
129e0 6e 65 64 20 69 73 20 74 68 65 20 65 72 72 6f 72  ned is the error
129e1 20 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64   state error cod
129e2 65 20 28 69 2e 65 2e 20 0a 2a 2a 20 6f 6e 65 20  e (i.e. .** one 
129e3 6f 66 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c  of SQLITE_IOERR,
129e4 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20   SQLITE_CORRUPT 
129e5 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29 2e  or SQLITE_FULL).
129e6 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  .**.** Otherwise
129e7 2c 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 20  , if all of the 
129e8 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72  following are tr
129e9 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68  ue:.**.**   * th
129ea 65 20 6e 65 77 20 70 61 67 65 20 73 69 7a 65 20  e new page size 
129eb 28 76 61 6c 75 65 20 6f 66 20 2a 70 50 61 67 65  (value of *pPage
129ec 53 69 7a 65 29 20 69 73 20 76 61 6c 69 64 20 28  Size) is valid (
129ed 61 20 70 6f 77 65 72 20 0a 2a 2a 20 20 20 20 20  a power .**     
129ee 6f 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20 35  of two between 5
129ef 31 32 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 41  12 and SQLITE_MA
129f0 58 5f 50 41 47 45 5f 53 49 5a 45 2c 20 69 6e 63  X_PAGE_SIZE, inc
129f1 6c 75 73 69 76 65 29 2c 20 61 6e 64 0a 2a 2a 0a  lusive), and.**.
129f2 2a 2a 20 20 20 2a 20 74 68 65 72 65 20 61 72 65  **   * there are
129f3 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   no outstanding 
129f4 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 73 2c  page references,
129f5 20 61 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74   and.**.**   * t
129f6 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 65  he database is e
129f7 69 74 68 65 72 20 6e 6f 74 20 61 6e 20 69 6e 2d  ither not an in-
129f8 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20  memory database 
129f9 6f 72 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20  or it is.**     
129fa 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  an in-memory dat
129fb 61 62 61 73 65 20 74 68 61 74 20 63 75 72 72 65  abase that curre
129fc 6e 74 6c 79 20 63 6f 6e 73 69 73 74 73 20 6f 66  ntly consists of
129fd 20 7a 65 72 6f 20 70 61 67 65 73 2e 0a 2a 2a 0a   zero pages..**.
129fe 2a 2a 20 74 68 65 6e 20 74 68 65 20 70 61 67 65  ** then the page
129ff 72 20 6f 62 6a 65 63 74 20 70 61 67 65 20 73 69  r object page si
12a00 7a 65 20 69 73 20 73 65 74 20 74 6f 20 2a 70 50  ze is set to *pP
12a01 61 67 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49  ageSize..**.** I
12a02 66 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20  f the page size 
12a03 69 73 20 63 68 61 6e 67 65 64 2c 20 74 68 65 6e  is changed, then
12a04 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 75   this function u
12a05 73 65 73 20 73 71 6c 69 74 65 33 50 61 67 65 72  ses sqlite3Pager
12a06 4d 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 74 6f 20  Malloc() .** to 
12a07 6f 62 74 61 69 6e 20 61 20 6e 65 77 20 50 61 67  obtain a new Pag
12a08 65 72 2e 70 54 6d 70 53 70 61 63 65 20 62 75 66  er.pTmpSpace buf
12a09 66 65 72 2e 20 49 66 20 74 68 69 73 20 61 6c 6c  fer. If this all
12a0a 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 20  ocation attempt 
12a0b 0a 2a 2a 20 66 61 69 6c 73 2c 20 53 51 4c 49 54  .** fails, SQLIT
12a0c 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72  E_NOMEM is retur
12a0d 6e 65 64 20 61 6e 64 20 74 68 65 20 70 61 67 65  ned and the page
12a0e 20 73 69 7a 65 20 72 65 6d 61 69 6e 73 20 75 6e   size remains un
12a0f 63 68 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 6e 20  changed. .** In 
12a10 61 6c 6c 20 6f 74 68 65 72 20 63 61 73 65 73 2c  all other cases,
12a11 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
12a12 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  turned..**.** If
12a13 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 69   the page size i
12a14 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 2c 20 65  s not changed, e
12a15 69 74 68 65 72 20 62 65 63 61 75 73 65 20 6f 6e  ither because on
12a16 65 20 6f 66 20 74 68 65 20 65 6e 75 6d 65 72 61  e of the enumera
12a17 74 65 64 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e  ted.** condition
12a18 73 20 61 62 6f 76 65 20 69 73 20 6e 6f 74 20 74  s above is not t
12a19 72 75 65 2c 20 74 68 65 20 70 61 67 65 72 20 77  rue, the pager w
12a1a 61 73 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74  as in error stat
12a1b 65 20 77 68 65 6e 20 74 68 69 73 0a 2a 2a 20 66  e when this.** f
12a1c 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c  unction was call
12a1d 65 64 2c 20 6f 72 20 62 65 63 61 75 73 65 20 74  ed, or because t
12a1e 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
12a1f 74 69 6f 6e 20 61 74 74 65 6d 70 74 20 66 61 69  tion attempt fai
12a20 6c 65 64 2c 20 0a 2a 2a 20 74 68 65 6e 20 2a 70  led, .** then *p
12a21 50 61 67 65 53 69 7a 65 20 69 73 20 73 65 74 20  PageSize is set 
12a22 74 6f 20 74 68 65 20 6f 6c 64 2c 20 72 65 74 61  to the old, reta
12a23 69 6e 65 64 20 70 61 67 65 20 73 69 7a 65 20 62  ined page size b
12a24 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e  efore returning.
12a25 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
12a26 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
12a27 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 50  gerSetPagesize(P
12a28 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 75 31  ager *pPager, u1
12a29 36 20 2a 70 50 61 67 65 53 69 7a 65 2c 20 69 6e  6 *pPageSize, in
12a2a 74 20 6e 52 65 73 65 72 76 65 29 7b 0a 20 20 69  t nReserve){.  i
12a2b 6e 74 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e  nt rc = pPager->
12a2c 65 72 72 43 6f 64 65 3b 0a 0a 20 20 69 66 28 20  errCode;..  if( 
12a2d 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
12a2e 0a 20 20 20 20 75 31 36 20 70 61 67 65 53 69 7a  .    u16 pageSiz
12a2f 65 20 3d 20 2a 70 50 61 67 65 53 69 7a 65 3b 0a  e = *pPageSize;.
12a30 20 20 20 20 61 73 73 65 72 74 28 20 70 61 67 65      assert( page
12a31 53 69 7a 65 3d 3d 30 20 7c 7c 20 28 70 61 67 65  Size==0 || (page
12a32 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 61 67  Size>=512 && pag
12a33 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41  eSize<=SQLITE_MA
12a34 58 5f 50 41 47 45 5f 53 49 5a 45 29 20 29 3b 0a  X_PAGE_SIZE) );.
12a35 20 20 20 20 69 66 28 20 28 70 50 61 67 65 72 2d      if( (pPager-
12a36 3e 6d 65 6d 44 62 3d 3d 30 20 7c 7c 20 70 50 61  >memDb==0 || pPa
12a37 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 30 29 0a  ger->dbSize==0).
12a38 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 50       && sqlite3P
12a39 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50  cacheRefCount(pP
12a3a 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d  ager->pPCache)==
12a3b 30 20 0a 20 20 20 20 20 26 26 20 70 61 67 65 53  0 .     && pageS
12a3c 69 7a 65 20 26 26 20 70 61 67 65 53 69 7a 65 21  ize && pageSize!
12a3d 3d 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a  =pPager->pageSiz
12a3e 65 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  e .    ){.      
12a3f 63 68 61 72 20 2a 70 4e 65 77 20 3d 20 28 63 68  char *pNew = (ch
12a40 61 72 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65  ar *)sqlite3Page
12a41 4d 61 6c 6c 6f 63 28 70 61 67 65 53 69 7a 65 29  Malloc(pageSize)
12a42 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65  ;.      if( !pNe
12a43 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  w ){.        rc 
12a44 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
12a45 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
12a46 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 74       pager_reset
12a47 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20  (pPager);.      
12a48 20 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69    pPager->pageSi
12a49 7a 65 20 3d 20 70 61 67 65 53 69 7a 65 3b 0a 20  ze = pageSize;. 
12a4a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
12a4b 67 65 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70  geFree(pPager->p
12a4c 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 20 20  TmpSpace);.     
12a4d 20 20 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53     pPager->pTmpS
12a4e 70 61 63 65 20 3d 20 70 4e 65 77 3b 0a 20 20 20  pace = pNew;.   
12a4f 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63       sqlite3Pcac
12a50 68 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 50  heSetPageSize(pP
12a51 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70  ager->pPCache, p
12a52 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  ageSize);.      
12a53 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 50 61  }.    }.    *pPa
12a54 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70 50  geSize = (u16)pP
12a55 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a  ager->pageSize;.
12a56 20 20 20 20 69 66 28 20 6e 52 65 73 65 72 76 65      if( nReserve
12a57 3c 30 20 29 20 6e 52 65 73 65 72 76 65 20 3d 20  <0 ) nReserve = 
12a58 70 50 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65  pPager->nReserve
12a59 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 52  ;.    assert( nR
12a5a 65 73 65 72 76 65 3e 3d 30 20 26 26 20 6e 52 65  eserve>=0 && nRe
12a5b 73 65 72 76 65 3c 31 30 30 30 20 29 3b 0a 20 20  serve<1000 );.  
12a5c 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 73 65 72    pPager->nReser
12a5d 76 65 20 3d 20 28 69 31 36 29 6e 52 65 73 65 72  ve = (i16)nReser
12a5e 76 65 3b 0a 20 20 20 20 70 61 67 65 72 52 65 70  ve;.    pagerRep
12a5f 6f 72 74 53 69 7a 65 28 70 50 61 67 65 72 29 3b  ortSize(pPager);
12a60 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
12a61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
12a62 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
12a63 68 65 20 22 74 65 6d 70 6f 72 61 72 79 20 70 61  he "temporary pa
12a64 67 65 22 20 62 75 66 66 65 72 20 68 65 6c 64 20  ge" buffer held 
12a65 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62 79  internally.** by
12a66 20 74 68 65 20 70 61 67 65 72 2e 20 20 54 68 69   the pager.  Thi
12a67 73 20 69 73 20 61 20 62 75 66 66 65 72 20 74 68  s is a buffer th
12a68 61 74 20 69 73 20 62 69 67 20 65 6e 6f 75 67 68  at is big enough
12a69 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20   to hold the.** 
12a6a 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 20 6f  entire content o
12a6b 66 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67  f a database pag
12a6c 65 2e 20 20 54 68 69 73 20 62 75 66 66 65 72 20  e.  This buffer 
12a6d 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c  is used internal
12a6e 6c 79 0a 2a 2a 20 64 75 72 69 6e 67 20 72 6f 6c  ly.** during rol
12a6f 6c 62 61 63 6b 20 61 6e 64 20 77 69 6c 6c 20 62  lback and will b
12a70 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 68  e overwritten wh
12a71 65 6e 65 76 65 72 20 61 20 72 6f 6c 6c 62 61 63  enever a rollbac
12a72 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 42 75  k.** occurs.  Bu
12a73 74 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 20  t other modules 
12a74 61 72 65 20 66 72 65 65 20 74 6f 20 75 73 65 20  are free to use 
12a75 69 74 20 74 6f 6f 2c 20 61 73 20 6c 6f 6e 67 20  it too, as long 
12a76 61 73 0a 2a 2a 20 6e 6f 20 72 6f 6c 6c 62 61 63  as.** no rollbac
12a77 6b 73 20 61 72 65 20 68 61 70 70 65 6e 69 6e 67  ks are happening
12a78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
12a79 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
12a7a 33 50 61 67 65 72 54 65 6d 70 53 70 61 63 65 28  3PagerTempSpace(
12a7b 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
12a7c 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
12a7d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 7d 0a 0a 2f  >pTmpSpace;.}../
12a7e 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  *.** Attempt to 
12a7f 73 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  set the maximum 
12a80 64 61 74 61 62 61 73 65 20 70 61 67 65 20 63 6f  database page co
12a81 75 6e 74 20 69 66 20 6d 78 50 61 67 65 20 69 73  unt if mxPage is
12a82 20 70 6f 73 69 74 69 76 65 2e 20 0a 2a 2a 20 4d   positive. .** M
12a83 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 69  ake no changes i
12a84 66 20 6d 78 50 61 67 65 20 69 73 20 7a 65 72 6f  f mxPage is zero
12a85 20 6f 72 20 6e 65 67 61 74 69 76 65 2e 20 20 41   or negative.  A
12a86 6e 64 20 6e 65 76 65 72 20 72 65 64 75 63 65 20  nd never reduce 
12a87 74 68 65 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20 70  the.** maximum p
12a88 61 67 65 20 63 6f 75 6e 74 20 62 65 6c 6f 77 20  age count below 
12a89 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65  the current size
12a8a 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
12a8b 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 61 72 64 6c 65  ..**.** Regardle
12a8c 73 73 20 6f 66 20 6d 78 50 61 67 65 2c 20 72 65  ss of mxPage, re
12a8d 74 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74  turn the current
12a8e 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 6f   maximum page co
12a8f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  unt..*/.SQLITE_P
12a90 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
12a91 65 33 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f  e3PagerMaxPageCo
12a92 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65  unt(Pager *pPage
12a93 72 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a  r, int mxPage){.
12a94 20 20 69 66 28 20 6d 78 50 61 67 65 3e 30 20 29    if( mxPage>0 )
12a95 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6d 78  {.    pPager->mx
12a96 50 67 6e 6f 20 3d 20 6d 78 50 61 67 65 3b 0a 20  Pgno = mxPage;. 
12a97 20 7d 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65   }.  sqlite3Page
12a98 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65  rPagecount(pPage
12a99 72 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20  r, 0);.  return 
12a9a 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 3b 0a  pPager->mxPgno;.
12a9b 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  }../*.** The fol
12a9c 6c 6f 77 69 6e 67 20 73 65 74 20 6f 66 20 72 6f  lowing set of ro
12a9d 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20  utines are used 
12a9e 74 6f 20 64 69 73 61 62 6c 65 20 74 68 65 20 73  to disable the s
12a9f 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 49 2f 4f 20  imulated.** I/O 
12aa0 65 72 72 6f 72 20 6d 65 63 68 61 6e 69 73 6d 2e  error mechanism.
12aa1 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73    These routines
12aa2 20 61 72 65 20 75 73 65 64 20 74 6f 20 61 76 6f   are used to avo
12aa3 69 64 20 73 69 6d 75 6c 61 74 65 64 0a 2a 2a 20  id simulated.** 
12aa4 65 72 72 6f 72 73 20 69 6e 20 70 6c 61 63 65 73  errors in places
12aa5 20 77 68 65 72 65 20 77 65 20 64 6f 20 6e 6f 74   where we do not
12aa6 20 63 61 72 65 20 61 62 6f 75 74 20 65 72 72 6f   care about erro
12aa7 72 73 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73  rs..**.** Unless
12aa8 20 2d 44 53 51 4c 49 54 45 5f 54 45 53 54 3d 31   -DSQLITE_TEST=1
12aa9 20 69 73 20 75 73 65 64 2c 20 74 68 65 73 65 20   is used, these 
12aaa 72 6f 75 74 69 6e 65 73 20 61 72 65 20 61 6c 6c  routines are all
12aab 20 6e 6f 2d 6f 70 73 0a 2a 2a 20 61 6e 64 20 67   no-ops.** and g
12aac 65 6e 65 72 61 74 65 20 6e 6f 20 63 6f 64 65 2e  enerate no code.
12aad 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
12aae 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
12aaf 49 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c  I extern int sql
12ab0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
12ab1 6e 64 69 6e 67 3b 0a 53 51 4c 49 54 45 5f 41 50  nding;.SQLITE_AP
12ab2 49 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c  I extern int sql
12ab3 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69  ite3_io_error_hi
12ab4 74 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61  t;.static int sa
12ab5 76 65 64 5f 63 6e 74 3b 0a 76 6f 69 64 20 64 69  ved_cnt;.void di
12ab6 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  sable_simulated_
12ab7 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 7b  io_errors(void){
12ab8 0a 20 20 73 61 76 65 64 5f 63 6e 74 20 3d 20 73  .  saved_cnt = s
12ab9 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
12aba 70 65 6e 64 69 6e 67 3b 0a 20 20 73 71 6c 69 74  pending;.  sqlit
12abb 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64  e3_io_error_pend
12abc 69 6e 67 20 3d 20 2d 31 3b 0a 7d 0a 76 6f 69 64  ing = -1;.}.void
12abd 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65   enable_simulate
12abe 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64  d_io_errors(void
12abf 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f  ){.  sqlite3_io_
12ac0 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20  error_pending = 
12ac1 73 61 76 65 64 5f 63 6e 74 3b 0a 7d 0a 23 65 6c  saved_cnt;.}.#el
12ac2 73 65 0a 23 20 64 65 66 69 6e 65 20 64 69 73 61  se.# define disa
12ac3 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f  ble_simulated_io
12ac4 5f 65 72 72 6f 72 73 28 29 0a 23 20 64 65 66 69  _errors().# defi
12ac5 6e 65 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61  ne enable_simula
12ac6 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a  ted_io_errors().
12ac7 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65  #endif../*.** Re
12ac8 61 64 20 74 68 65 20 66 69 72 73 74 20 4e 20 62  ad the first N b
12ac9 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 62 65  ytes from the be
12aca 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 66  ginning of the f
12acb 69 6c 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 0a  ile into memory.
12acc 2a 2a 20 74 68 61 74 20 70 44 65 73 74 20 70 6f  ** that pDest po
12acd 69 6e 74 73 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a 20  ints to. .**.** 
12ace 49 66 20 74 68 65 20 70 61 67 65 72 20 77 61 73  If the pager was
12acf 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 74 72 61   opened on a tra
12ad0 6e 73 69 65 6e 74 20 66 69 6c 65 20 28 7a 46 69  nsient file (zFi
12ad1 6c 65 6e 61 6d 65 3d 3d 22 22 29 2c 20 6f 72 0a  lename==""), or.
12ad2 2a 2a 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 66  ** opened on a f
12ad3 69 6c 65 20 6c 65 73 73 20 74 68 61 6e 20 4e 20  ile less than N 
12ad4 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 74  bytes in size, t
12ad5 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72  he output buffer
12ad6 20 69 73 0a 2a 2a 20 7a 65 72 6f 65 64 20 61 6e   is.** zeroed an
12ad7 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75  d SQLITE_OK retu
12ad8 72 6e 65 64 2e 20 54 68 65 20 72 61 74 69 6f 6e  rned. The ration
12ad9 61 6c 65 20 66 6f 72 20 74 68 69 73 20 69 73 20  ale for this is 
12ada 74 68 61 74 20 74 68 69 73 20 0a 2a 2a 20 66 75  that this .** fu
12adb 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  nction is used t
12adc 6f 20 72 65 61 64 20 64 61 74 61 62 61 73 65 20  o read database 
12add 68 65 61 64 65 72 73 2c 20 61 6e 64 20 61 20 6e  headers, and a n
12ade 65 77 20 74 72 61 6e 73 69 65 6e 74 20 6f 72 0a  ew transient or.
12adf 2a 2a 20 7a 65 72 6f 20 73 69 7a 65 64 20 64 61  ** zero sized da
12ae0 74 61 62 61 73 65 20 68 61 73 20 61 20 68 65 61  tabase has a hea
12ae1 64 65 72 20 74 68 61 6e 20 63 6f 6e 73 69 73 74  der than consist
12ae2 73 20 65 6e 74 69 72 65 6c 79 20 6f 66 20 7a 65  s entirely of ze
12ae3 72 6f 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  roes..**.** If a
12ae4 6e 79 20 49 4f 20 65 72 72 6f 72 20 61 70 61 72  ny IO error apar
12ae5 74 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 49 4f  t from SQLITE_IO
12ae6 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 69  ERR_SHORT_READ i
12ae7 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 0a 2a  s encountered,.*
12ae8 2a 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  * the error code
12ae9 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20   is returned to 
12aea 74 68 65 20 63 61 6c 6c 65 72 20 61 6e 64 20 74  the caller and t
12aeb 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
12aec 68 65 0a 2a 2a 20 6f 75 74 70 75 74 20 62 75 66  he.** output buf
12aed 66 65 72 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a  fer undefined..*
12aee 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
12aef 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
12af0 72 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28  rReadFileheader(
12af1 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
12af2 6e 74 20 4e 2c 20 75 6e 73 69 67 6e 65 64 20 63  nt N, unsigned c
12af3 68 61 72 20 2a 70 44 65 73 74 29 7b 0a 20 20 69  har *pDest){.  i
12af4 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
12af5 4b 3b 0a 20 20 6d 65 6d 73 65 74 28 70 44 65 73  K;.  memset(pDes
12af6 74 2c 20 30 2c 20 4e 29 3b 0a 20 20 61 73 73 65  t, 0, N);.  asse
12af7 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65  rt( isOpen(pPage
12af8 72 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 72  r->fd) || pPager
12af9 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20  ->tempFile );.  
12afa 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65  if( isOpen(pPage
12afb 72 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20 49 4f  r->fd) ){.    IO
12afc 54 52 41 43 45 28 28 22 44 42 48 44 52 20 25 70  TRACE(("DBHDR %p
12afd 20 30 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72   0 %d\n", pPager
12afe 2c 20 4e 29 29 0a 20 20 20 20 72 63 20 3d 20 73  , N)).    rc = s
12aff 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61  qlite3OsRead(pPa
12b00 67 65 72 2d 3e 66 64 2c 20 70 44 65 73 74 2c 20  ger->fd, pDest, 
12b01 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72  N, 0);.    if( r
12b02 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  c==SQLITE_IOERR_
12b03 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20  SHORT_READ ){.  
12b04 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
12b05 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  OK;.    }.  }.  
12b06 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
12b07 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74  .** Return the t
12b08 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70  otal number of p
12b09 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61  ages in the data
12b0a 62 61 73 65 20 66 69 6c 65 20 61 73 73 6f 63 69  base file associ
12b0b 61 74 65 64 20 0a 2a 2a 20 77 69 74 68 20 70 50  ated .** with pP
12b0c 61 67 65 72 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20  ager. Normally, 
12b0d 74 68 69 73 20 69 73 20 63 61 6c 63 75 6c 61 74  this is calculat
12b0e 65 64 20 61 73 20 28 3c 64 62 20 66 69 6c 65 20  ed as (<db file 
12b0f 73 69 7a 65 3e 2f 3c 70 61 67 65 2d 73 69 7a 65  size>/<page-size
12b10 3e 29 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20  >)..** However, 
12b11 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 62  if the file is b
12b12 65 74 77 65 65 6e 20 31 20 61 6e 64 20 3c 70 61  etween 1 and <pa
12b13 67 65 2d 73 69 7a 65 3e 20 62 79 74 65 73 20 69  ge-size> bytes i
12b14 6e 20 73 69 7a 65 2c 20 74 68 65 6e 20 0a 2a 2a  n size, then .**
12b15 20 74 68 69 73 20 69 73 20 63 6f 6e 73 69 64 65   this is conside
12b16 72 65 64 20 61 20 31 20 70 61 67 65 20 66 69 6c  red a 1 page fil
12b17 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
12b18 70 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f  pager is in erro
12b19 72 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69  r state when thi
12b1a 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
12b1b 6c 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 0a 2a  lled, then the.*
12b1c 2a 20 65 72 72 6f 72 20 73 74 61 74 65 20 65 72  * error state er
12b1d 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
12b1e 72 6e 65 64 20 61 6e 64 20 2a 70 6e 50 61 67 65  rned and *pnPage
12b1f 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e   left unchanged.
12b20 20 4f 72 2c 0a 2a 2a 20 69 66 20 74 68 65 20 66   Or,.** if the f
12b21 69 6c 65 20 73 79 73 74 65 6d 20 68 61 73 20 74  ile system has t
12b22 6f 20 62 65 20 71 75 65 72 69 65 64 20 66 6f 72  o be queried for
12b23 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
12b24 20 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 74 68 65   file and.** the
12b25 20 71 75 65 72 79 20 61 74 74 65 6d 70 74 20 72   query attempt r
12b26 65 74 75 72 6e 73 20 61 6e 20 49 4f 20 65 72 72  eturns an IO err
12b27 6f 72 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72  or, the IO error
12b28 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
12b29 64 0a 2a 2a 20 61 6e 64 20 2a 70 6e 50 61 67 65  d.** and *pnPage
12b2a 20 69 73 20 6c 65 66 74 20 75 6e 63 68 61 6e 67   is left unchang
12b2b 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77  ed..**.** Otherw
12b2c 69 73 65 2c 20 69 66 20 65 76 65 72 79 74 68 69  ise, if everythi
12b2d 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  ng is successful
12b2e 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4b  , then SQLITE_OK
12b2f 20 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20   is returned.** 
12b30 61 6e 64 20 2a 70 6e 50 61 67 65 20 69 73 20 73  and *pnPage is s
12b31 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72  et to the number
12b32 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65   of pages in the
12b33 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 53 51   database..*/.SQ
12b34 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
12b35 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
12b36 65 63 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50  ecount(Pager *pP
12b37 61 67 65 72 2c 20 69 6e 74 20 2a 70 6e 50 61 67  ager, int *pnPag
12b38 65 29 7b 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65  e){.  Pgno nPage
12b39 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
12b3a 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75  /* Value to retu
12b3b 72 6e 20 76 69 61 20 2a 70 6e 50 61 67 65 20 2a  rn via *pnPage *
12b3c 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70  /..  /* If the p
12b3d 61 67 65 72 20 69 73 20 61 6c 72 65 61 64 79 20  ager is already 
12b3e 69 6e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61  in the error sta
12b3f 74 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65  te, return the e
12b40 72 72 6f 72 20 63 6f 64 65 2e 20 2a 2f 0a 20 20  rror code. */.  
12b41 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43  if( pPager->errC
12b42 6f 64 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ode ){.    retur
12b43 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  n pPager->errCod
12b44 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 74  e;.  }..  /* Det
12b45 65 72 6d 69 6e 65 20 74 68 65 20 6e 75 6d 62 65  ermine the numbe
12b46 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
12b47 65 20 66 69 6c 65 2e 20 53 74 6f 72 65 20 74 68  e file. Store th
12b48 69 73 20 69 6e 20 6e 50 61 67 65 2e 20 2a 2f 0a  is in nPage. */.
12b49 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62    if( pPager->db
12b4a 53 69 7a 65 56 61 6c 69 64 20 29 7b 0a 20 20 20  SizeValid ){.   
12b4b 20 6e 50 61 67 65 20 3d 20 70 50 61 67 65 72 2d   nPage = pPager-
12b4c 3e 64 62 53 69 7a 65 3b 0a 20 20 7d 65 6c 73 65  >dbSize;.  }else
12b4d 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20  {.    int rc;   
12b4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
12b4f 20 45 72 72 6f 72 20 72 65 74 75 72 6e 65 64 20   Error returned 
12b50 62 79 20 4f 73 46 69 6c 65 53 69 7a 65 28 29 20  by OsFileSize() 
12b51 2a 2f 0a 20 20 20 20 69 36 34 20 6e 20 3d 20 30  */.    i64 n = 0
12b52 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
12b53 2a 20 46 69 6c 65 20 73 69 7a 65 20 69 6e 20 62  * File size in b
12b54 79 74 65 73 20 72 65 74 75 72 6e 65 64 20 62 79  ytes returned by
12b55 20 4f 73 46 69 6c 65 53 69 7a 65 28 29 20 2a 2f   OsFileSize() */
12b56 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73  ..    assert( is
12b57 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29  Open(pPager->fd)
12b58 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70   || pPager->temp
12b59 46 69 6c 65 20 29 3b 0a 20 20 20 20 69 66 28 20  File );.    if( 
12b5a 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66  isOpen(pPager->f
12b5b 64 29 20 26 26 20 28 30 20 21 3d 20 28 72 63 20  d) && (0 != (rc 
12b5c 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53  = sqlite3OsFileS
12b5d 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ize(pPager->fd, 
12b5e 26 6e 29 29 29 20 29 7b 0a 20 20 20 20 20 20 70  &n))) ){.      p
12b5f 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65  ager_error(pPage
12b60 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 72 65  r, rc);.      re
12b61 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
12b62 20 20 20 69 66 28 20 6e 3e 30 20 26 26 20 6e 3c     if( n>0 && n<
12b63 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
12b64 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20   ){.      nPage 
12b65 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 1;.    }else{.
12b66 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 28 50        nPage = (P
12b67 67 6e 6f 29 28 6e 20 2f 20 70 50 61 67 65 72 2d  gno)(n / pPager-
12b68 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20  >pageSize);.    
12b69 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72  }.    if( pPager
12b6a 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f 55  ->state!=PAGER_U
12b6b 4e 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 70  NLOCK ){.      p
12b6c 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20  Pager->dbSize = 
12b6d 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 70 50 61  nPage;.      pPa
12b6e 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20  ger->dbFileSize 
12b6f 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 70  = nPage;.      p
12b70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c  Pager->dbSizeVal
12b71 69 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  id = 1;.    }.  
12b72 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63  }..  /* If the c
12b73 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66  urrent number of
12b74 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 66 69   pages in the fi
12b75 6c 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68  le is greater th
12b76 61 6e 20 74 68 65 20 0a 20 20 2a 2a 20 63 6f 6e  an the .  ** con
12b77 66 69 67 75 72 65 64 20 6d 61 78 69 6d 75 6d 20  figured maximum 
12b78 70 61 67 65 72 20 6e 75 6d 62 65 72 2c 20 69 6e  pager number, in
12b79 63 72 65 61 73 65 20 74 68 65 20 61 6c 6c 6f 77  crease the allow
12b7a 65 64 20 6c 69 6d 69 74 20 73 6f 0a 20 20 2a 2a  ed limit so.  **
12b7b 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 20 63   that the file c
12b7c 61 6e 20 62 65 20 72 65 61 64 2e 0a 20 20 2a 2f  an be read..  */
12b7d 0a 20 20 69 66 28 20 6e 50 61 67 65 3e 70 50 61  .  if( nPage>pPa
12b7e 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20  ger->mxPgno ){. 
12b7f 20 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e     pPager->mxPgn
12b80 6f 20 3d 20 28 50 67 6e 6f 29 6e 50 61 67 65 3b  o = (Pgno)nPage;
12b81 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74  .  }..  /* Set t
12b82 68 65 20 6f 75 74 70 75 74 20 76 61 72 69 61 62  he output variab
12b83 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51  le and return SQ
12b84 4c 49 54 45 5f 4f 4b 20 2a 2f 0a 20 20 69 66 28  LITE_OK */.  if(
12b85 20 70 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 2a   pnPage ){.    *
12b86 70 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 3b 0a  pnPage = nPage;.
12b87 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
12b88 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  ITE_OK;.}.../*.*
12b89 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69 6e 20  * Try to obtain 
12b8a 61 20 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 6c  a lock of type l
12b8b 6f 63 6b 74 79 70 65 20 6f 6e 20 74 68 65 20 64  ocktype on the d
12b8c 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66  atabase file. If
12b8d 0a 2a 2a 20 61 20 73 69 6d 69 6c 61 72 20 6f 72  .** a similar or
12b8e 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 69 73   greater lock is
12b8f 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 74   already held, t
12b90 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
12b91 61 20 6e 6f 2d 6f 70 0a 2a 2a 20 28 72 65 74 75  a no-op.** (retu
12b92 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 4f 4b 20  rning SQLITE_OK 
12b93 69 6d 6d 65 64 69 61 74 65 6c 79 29 2e 0a 2a 2a  immediately)..**
12b94 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61  .** Otherwise, a
12b95 74 74 65 6d 70 74 20 74 6f 20 6f 62 74 61 69 6e  ttempt to obtain
12b96 20 74 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20   the lock using 
12b97 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e  sqlite3OsLock().
12b98 20 49 6e 76 6f 6b 65 20 0a 2a 2a 20 74 68 65 20   Invoke .** the 
12b99 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 66  busy callback if
12b9a 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 63 75 72   the lock is cur
12b9b 72 65 6e 74 6c 79 20 6e 6f 74 20 61 76 61 69 6c  rently not avail
12b9c 61 62 6c 65 2e 20 52 65 70 65 61 74 20 0a 2a 2a  able. Repeat .**
12b9d 20 75 6e 74 69 6c 20 74 68 65 20 62 75 73 79 20   until the busy 
12b9e 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73  callback returns
12b9f 20 66 61 6c 73 65 20 6f 72 20 75 6e 74 69 6c 20   false or until 
12ba0 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 0a  the attempt to .
12ba1 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 6f  ** obtain the lo
12ba2 63 6b 20 73 75 63 63 65 65 64 73 2e 0a 2a 2a 0a  ck succeeds..**.
12ba3 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
12ba4 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61  _OK on success a
12ba5 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  nd an error code
12ba6 20 69 66 20 77 65 20 63 61 6e 6e 6f 74 20 6f 62   if we cannot ob
12ba7 74 61 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b  tain.** the lock
12ba8 2e 20 49 66 20 74 68 65 20 6c 6f 63 6b 20 69 73  . If the lock is
12ba9 20 6f 62 74 61 69 6e 65 64 20 73 75 63 63 65 73   obtained succes
12baa 73 66 75 6c 6c 79 2c 20 73 65 74 20 74 68 65 20  sfully, set the 
12bab 50 61 67 65 72 2e 73 74 61 74 65 20 0a 2a 2a 20  Pager.state .** 
12bac 76 61 72 69 61 62 6c 65 20 74 6f 20 6c 6f 63 6b  variable to lock
12bad 74 79 70 65 20 62 65 66 6f 72 65 20 72 65 74 75  type before retu
12bae 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  rning..*/.static
12baf 20 69 6e 74 20 70 61 67 65 72 5f 77 61 69 74 5f   int pager_wait_
12bb0 6f 6e 5f 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70  on_lock(Pager *p
12bb1 50 61 67 65 72 2c 20 69 6e 74 20 6c 6f 63 6b 74  Pager, int lockt
12bb2 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20  ype){.  int rc; 
12bb3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12bb4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
12bb5 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a  Return code */..
12bb6 20 20 2f 2a 20 54 68 65 20 4f 53 20 6c 6f 63 6b    /* The OS lock
12bb7 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20   values must be 
12bb8 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20  the same as the 
12bb9 50 61 67 65 72 20 6c 6f 63 6b 20 76 61 6c 75 65  Pager lock value
12bba 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 50  s */.  assert( P
12bbb 41 47 45 52 5f 53 48 41 52 45 44 3d 3d 53 48 41  AGER_SHARED==SHA
12bbc 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73  RED_LOCK );.  as
12bbd 73 65 72 74 28 20 50 41 47 45 52 5f 52 45 53 45  sert( PAGER_RESE
12bbe 52 56 45 44 3d 3d 52 45 53 45 52 56 45 44 5f 4c  RVED==RESERVED_L
12bbf 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  OCK );.  assert(
12bc0 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45   PAGER_EXCLUSIVE
12bc1 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ==EXCLUSIVE_LOCK
12bc2 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   );..  /* If the
12bc3 20 66 69 6c 65 20 69 73 20 63 75 72 72 65 6e 74   file is current
12bc4 6c 79 20 75 6e 6c 6f 63 6b 65 64 20 74 68 65 6e  ly unlocked then
12bc5 20 74 68 65 20 73 69 7a 65 20 6d 75 73 74 20 62   the size must b
12bc6 65 20 75 6e 6b 6e 6f 77 6e 20 2a 2f 0a 20 20 61  e unknown */.  a
12bc7 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
12bc8 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52  tate>=PAGER_SHAR
12bc9 45 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62  ED || pPager->db
12bca 53 69 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b 0a  SizeValid==0 );.
12bcb 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74  .  /* Check that
12bcc 20 74 68 69 73 20 69 73 20 65 69 74 68 65 72 20   this is either 
12bcd 61 20 6e 6f 2d 6f 70 20 28 62 65 63 61 75 73 65  a no-op (because
12bce 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c   the requested l
12bcf 6f 63 6b 20 69 73 20 0a 20 20 2a 2a 20 61 6c 72  ock is .  ** alr
12bd0 65 61 64 79 20 68 65 6c 64 2c 20 6f 72 20 6f 6e  eady held, or on
12bd1 65 20 6f 66 20 74 68 65 20 74 72 61 6e 73 69 73  e of the transis
12bd2 74 69 6f 6e 73 20 74 68 61 74 20 74 68 65 20 62  tions that the b
12bd3 75 73 79 2d 68 61 6e 64 6c 65 72 0a 20 20 2a 2a  usy-handler.  **
12bd4 20 6d 61 79 20 62 65 20 69 6e 76 6f 6b 65 64 20   may be invoked 
12bd5 64 75 72 69 6e 67 2c 20 61 63 63 6f 72 64 69 6e  during, accordin
12bd6 67 20 74 6f 20 74 68 65 20 63 6f 6d 6d 65 6e 74  g to the comment
12bd7 20 61 62 6f 76 65 0a 20 20 2a 2a 20 73 71 6c 69   above.  ** sqli
12bd8 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79 68  te3PagerSetBusyh
12bd9 61 6e 64 6c 65 72 28 29 2e 0a 20 20 2a 2f 0a 20  andler()..  */. 
12bda 20 61 73 73 65 72 74 28 20 28 70 50 61 67 65 72   assert( (pPager
12bdb 2d 3e 73 74 61 74 65 3e 3d 6c 6f 63 6b 74 79 70  ->state>=locktyp
12bdc 65 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 50  e).       || (pP
12bdd 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47  ager->state==PAG
12bde 45 52 5f 55 4e 4c 4f 43 4b 20 26 26 20 6c 6f 63  ER_UNLOCK && loc
12bdf 6b 74 79 70 65 3d 3d 50 41 47 45 52 5f 53 48 41  ktype==PAGER_SHA
12be0 52 45 44 29 0a 20 20 20 20 20 20 20 7c 7c 20 28  RED).       || (
12be1 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50  pPager->state==P
12be2 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 26 26  AGER_RESERVED &&
12be3 20 6c 6f 63 6b 74 79 70 65 3d 3d 50 41 47 45 52   locktype==PAGER
12be4 5f 45 58 43 4c 55 53 49 56 45 29 0a 20 20 29 3b  _EXCLUSIVE).  );
12be5 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ..  if( pPager->
12be6 73 74 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 20  state>=locktype 
12be7 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
12be8 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a  TE_OK;.  }else{.
12be9 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 72      do {.      r
12bea 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63  c = sqlite3OsLoc
12beb 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 6c 6f  k(pPager->fd, lo
12bec 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 7d 77 68  cktype);.    }wh
12bed 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
12bee 42 55 53 59 20 26 26 20 70 50 61 67 65 72 2d 3e  BUSY && pPager->
12bef 78 42 75 73 79 48 61 6e 64 6c 65 72 28 70 50 61  xBusyHandler(pPa
12bf0 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65  ger->pBusyHandle
12bf1 72 41 72 67 29 20 29 3b 0a 20 20 20 20 69 66 28  rArg) );.    if(
12bf2 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
12bf3 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  {.      pPager->
12bf4 73 74 61 74 65 20 3d 20 28 75 38 29 6c 6f 63 6b  state = (u8)lock
12bf5 74 79 70 65 3b 0a 20 20 20 20 20 20 49 4f 54 52  type;.      IOTR
12bf6 41 43 45 28 28 22 4c 4f 43 4b 20 25 70 20 25 64  ACE(("LOCK %p %d
12bf7 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 6c 6f 63  \n", pPager, loc
12bf8 6b 74 79 70 65 29 29 0a 20 20 20 20 7d 0a 20 20  ktype)).    }.  
12bf9 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
12bfa 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e  ../*.** Function
12bfb 20 61 73 73 65 72 74 54 72 75 6e 63 61 74 65 43   assertTruncateC
12bfc 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 67 65 72  onstraint(pPager
12bfd 29 20 63 68 65 63 6b 73 20 74 68 61 74 20 6f 6e  ) checks that on
12bfe 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 6f 6c  e of the .** fol
12bff 6c 6f 77 69 6e 67 20 69 73 20 74 72 75 65 20 66  lowing is true f
12c00 6f 72 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67  or all dirty pag
12c01 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  es currently in 
12c02 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 3a 0a  the page-cache:.
12c03 2a 2a 0a 2a 2a 20 20 20 61 29 20 54 68 65 20 70  **.**   a) The p
12c04 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 6c 65  age number is le
12c05 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  ss than or equal
12c06 20 74 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20   to the size of 
12c07 74 68 65 20 0a 2a 2a 20 20 20 20 20 20 63 75 72  the .**      cur
12c08 72 65 6e 74 20 64 61 74 61 62 61 73 65 20 69 6d  rent database im
12c09 61 67 65 2c 20 69 6e 20 70 61 67 65 73 2c 20 4f  age, in pages, O
12c0a 52 0a 2a 2a 0a 2a 2a 20 20 20 62 29 20 69 66 20  R.**.**   b) if 
12c0b 74 68 65 20 70 61 67 65 20 63 6f 6e 74 65 6e 74  the page content
12c0c 20 77 65 72 65 20 77 72 69 74 74 65 6e 20 61 74   were written at
12c0d 20 74 68 69 73 20 74 69 6d 65 2c 20 69 74 20 77   this time, it w
12c0e 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 20 20 20 20  ould not.**     
12c0f 20 62 65 20 6e 65 63 65 73 73 61 72 79 20 74 6f   be necessary to
12c10 20 77 72 69 74 65 20 74 68 65 20 63 75 72 72 65   write the curre
12c11 6e 74 20 63 6f 6e 74 65 6e 74 20 6f 75 74 20 74  nt content out t
12c12 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61  o the sub-journa
12c13 6c 0a 2a 2a 20 20 20 20 20 20 28 61 73 20 64 65  l.**      (as de
12c14 74 65 72 6d 69 6e 65 64 20 62 79 20 66 75 6e 63  termined by func
12c15 74 69 6f 6e 20 73 75 62 6a 52 65 71 75 69 72 65  tion subjRequire
12c16 73 50 61 67 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20  sPage())..**.** 
12c17 49 66 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e  If the condition
12c18 20 61 73 73 65 72 74 65 64 20 62 79 20 74 68 69   asserted by thi
12c19 73 20 66 75 6e 63 74 69 6f 6e 20 77 65 72 65 20  s function were 
12c1a 6e 6f 74 20 74 72 75 65 2c 20 61 6e 64 20 74 68  not true, and th
12c1b 65 0a 2a 2a 20 64 69 72 74 79 20 70 61 67 65 20  e.** dirty page 
12c1c 77 65 72 65 20 74 6f 20 62 65 20 64 69 73 63 61  were to be disca
12c1d 72 64 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61  rded from the ca
12c1e 63 68 65 20 76 69 61 20 74 68 65 20 70 61 67 65  che via the page
12c1f 72 53 74 72 65 73 73 28 29 0a 2a 2a 20 72 6f 75  rStress().** rou
12c20 74 69 6e 65 2c 20 70 61 67 65 72 53 74 72 65 73  tine, pagerStres
12c21 73 28 29 20 77 6f 75 6c 64 20 6e 6f 74 20 77 72  s() would not wr
12c22 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ite the current 
12c23 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 74 6f 0a  page content to.
12c24 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
12c25 66 69 6c 65 2e 20 49 66 20 61 20 73 61 76 65 70  file. If a savep
12c26 6f 69 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  oint transaction
12c27 20 77 65 72 65 20 72 6f 6c 6c 65 64 20 62 61 63   were rolled bac
12c28 6b 20 61 66 74 65 72 0a 2a 2a 20 74 68 69 73 20  k after.** this 
12c29 68 61 70 70 65 6e 65 64 2c 20 74 68 65 20 63 6f  happened, the co
12c2a 72 72 65 63 74 20 62 65 68 61 76 69 6f 75 72 20  rrect behaviour 
12c2b 77 6f 75 6c 64 20 62 65 20 74 6f 20 72 65 73 74  would be to rest
12c2c 6f 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 0a  ore the current.
12c2d 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  ** content of th
12c2e 65 20 70 61 67 65 2e 20 48 6f 77 65 76 65 72 2c  e page. However,
12c2f 20 73 69 6e 63 65 20 74 68 69 73 20 63 6f 6e 74   since this cont
12c30 65 6e 74 20 69 73 20 6e 6f 74 20 70 72 65 73 65  ent is not prese
12c31 6e 74 20 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20  nt in either.** 
12c32 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
12c33 65 20 6f 72 20 74 68 65 20 70 6f 72 74 69 6f 6e  e or the portion
12c34 20 6f 66 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b   of the rollback
12c35 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 0a 2a 2a   journal and .**
12c36 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c   sub-journal rol
12c37 6c 65 64 20 62 61 63 6b 20 74 68 65 20 63 6f 6e  led back the con
12c38 74 65 6e 74 20 63 6f 75 6c 64 20 6e 6f 74 20 62  tent could not b
12c39 65 20 72 65 73 74 6f 72 65 64 20 61 6e 64 20 74  e restored and t
12c3a 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69  he.** database i
12c3b 6d 61 67 65 20 77 6f 75 6c 64 20 62 65 63 6f 6d  mage would becom
12c3c 65 20 63 6f 72 72 75 70 74 2e 20 49 74 20 69 73  e corrupt. It is
12c3d 20 74 68 65 72 65 66 6f 72 65 20 66 6f 72 74 75   therefore fortu
12c3e 6e 61 74 65 20 74 68 61 74 20 0a 2a 2a 20 74 68  nate that .** th
12c3f 69 73 20 63 69 72 63 75 6d 73 74 61 6e 63 65 20  is circumstance 
12c40 63 61 6e 6e 6f 74 20 61 72 69 73 65 2e 0a 2a 2f  cannot arise..*/
12c41 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
12c42 49 54 45 5f 44 45 42 55 47 29 0a 73 74 61 74 69  ITE_DEBUG).stati
12c43 63 20 76 6f 69 64 20 61 73 73 65 72 74 54 72 75  c void assertTru
12c44 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43  ncateConstraintC
12c45 62 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20  b(PgHdr *pPg){. 
12c46 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 66 6c   assert( pPg->fl
12c47 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 20  ags&PGHDR_DIRTY 
12c48 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 73 75  );.  assert( !su
12c49 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70  bjRequiresPage(p
12c4a 50 67 29 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f  Pg) || pPg->pgno
12c4b 3c 3d 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 64  <=pPg->pPager->d
12c4c 62 53 69 7a 65 20 29 3b 0a 7d 0a 73 74 61 74 69  bSize );.}.stati
12c4d 63 20 76 6f 69 64 20 61 73 73 65 72 74 54 72 75  c void assertTru
12c4e 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 28  ncateConstraint(
12c4f 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
12c50 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49    sqlite3PcacheI
12c51 74 65 72 61 74 65 44 69 72 74 79 28 70 50 61 67  terateDirty(pPag
12c52 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 61 73 73  er->pPCache, ass
12c53 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74  ertTruncateConst
12c54 72 61 69 6e 74 43 62 29 3b 0a 7d 0a 23 65 6c 73  raintCb);.}.#els
12c55 65 0a 23 20 64 65 66 69 6e 65 20 61 73 73 65 72  e.# define asser
12c56 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61  tTruncateConstra
12c57 69 6e 74 28 70 50 61 67 65 72 29 0a 23 65 6e 64  int(pPager).#end
12c58 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61  if../*.** Trunca
12c59 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  te the in-memory
12c5a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
12c5b 6d 61 67 65 20 74 6f 20 6e 50 61 67 65 20 70 61  mage to nPage pa
12c5c 67 65 73 2e 20 54 68 69 73 20 0a 2a 2a 20 66 75  ges. This .** fu
12c5d 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  nction does not 
12c5e 61 63 74 75 61 6c 6c 79 20 6d 6f 64 69 66 79 20  actually modify 
12c5f 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
12c60 65 20 6f 6e 20 64 69 73 6b 2e 20 49 74 20 0a 2a  e on disk. It .*
12c61 2a 20 6a 75 73 74 20 73 65 74 73 20 74 68 65 20  * just sets the 
12c62 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 20 6f  internal state o
12c63 66 20 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65  f the pager obje
12c64 63 74 20 73 6f 20 74 68 61 74 20 74 68 65 20 0a  ct so that the .
12c65 2a 2a 20 74 72 75 6e 63 61 74 69 6f 6e 20 77 69  ** truncation wi
12c66 6c 6c 20 62 65 20 64 6f 6e 65 20 77 68 65 6e 20  ll be done when 
12c67 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e  the current tran
12c68 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69  saction is commi
12c69 74 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tted..*/.SQLITE_
12c6a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
12c6b 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
12c6c 65 49 6d 61 67 65 28 50 61 67 65 72 20 2a 70 50  eImage(Pager *pP
12c6d 61 67 65 72 2c 20 50 67 6e 6f 20 6e 50 61 67 65  ager, Pgno nPage
12c6e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ){.  assert( pPa
12c6f 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64  ger->dbSizeValid
12c70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
12c71 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e 3d 6e 50  ager->dbSize>=nP
12c72 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  age );.  assert(
12c73 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d   pPager->state>=
12c74 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29  PAGER_RESERVED )
12c75 3b 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69  ;.  pPager->dbSi
12c76 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 61 73  ze = nPage;.  as
12c77 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73  sertTruncateCons
12c78 74 72 61 69 6e 74 28 70 50 61 67 65 72 29 3b 0a  traint(pPager);.
12c79 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77  }../*.** Shutdow
12c7a 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  n the page cache
12c7b 2e 20 20 46 72 65 65 20 61 6c 6c 20 6d 65 6d 6f  .  Free all memo
12c7c 72 79 20 61 6e 64 20 63 6c 6f 73 65 20 61 6c 6c  ry and close all
12c7d 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66   files..**.** If
12c7e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77   a transaction w
12c7f 61 73 20 69 6e 20 70 72 6f 67 72 65 73 73 20 77  as in progress w
12c80 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
12c81 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 61 74   is called, that
12c82 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  .** transaction 
12c83 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20  is rolled back. 
12c84 20 41 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67   All outstanding
12c85 20 70 61 67 65 73 20 61 72 65 20 69 6e 76 61 6c   pages are inval
12c86 69 64 61 74 65 64 0a 2a 2a 20 61 6e 64 20 74 68  idated.** and th
12c87 65 69 72 20 6d 65 6d 6f 72 79 20 69 73 20 66 72  eir memory is fr
12c88 65 65 64 2e 20 20 41 6e 79 20 61 74 74 65 6d 70  eed.  Any attemp
12c89 74 20 74 6f 20 75 73 65 20 61 20 70 61 67 65 20  t to use a page 
12c8a 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69  associated.** wi
12c8b 74 68 20 74 68 69 73 20 70 61 67 65 20 63 61 63  th this page cac
12c8c 68 65 20 61 66 74 65 72 20 74 68 69 73 20 66 75  he after this fu
12c8d 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 77  nction returns w
12c8e 69 6c 6c 20 6c 69 6b 65 6c 79 0a 2a 2a 20 72 65  ill likely.** re
12c8f 73 75 6c 74 20 69 6e 20 61 20 63 6f 72 65 64 75  sult in a coredu
12c90 6d 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  mp..**.** This f
12c91 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 73  unction always s
12c92 75 63 63 65 65 64 73 2e 20 49 66 20 61 20 74 72  ucceeds. If a tr
12c93 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
12c94 69 76 65 20 61 6e 20 61 74 74 65 6d 70 74 0a 2a  ive an attempt.*
12c95 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 72 6f 6c  * is made to rol
12c96 6c 20 69 74 20 62 61 63 6b 2e 20 49 66 20 61 6e  l it back. If an
12c97 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75   error occurs du
12c98 72 69 6e 67 20 74 68 65 20 72 6f 6c 6c 62 61 63  ring the rollbac
12c99 6b 20 0a 2a 2a 20 61 20 68 6f 74 20 6a 6f 75 72  k .** a hot jour
12c9a 6e 61 6c 20 6d 61 79 20 62 65 20 6c 65 66 74 20  nal may be left 
12c9b 69 6e 20 74 68 65 20 66 69 6c 65 73 79 73 74 65  in the filesyste
12c9c 6d 20 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 69  m but no error i
12c9d 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 74 6f  s returned.** to
12c9e 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a   the caller..*/.
12c9f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
12ca0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43  nt sqlite3PagerC
12ca1 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 50 61 67  lose(Pager *pPag
12ca2 65 72 29 7b 0a 20 20 64 69 73 61 62 6c 65 5f 73  er){.  disable_s
12ca3 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f  imulated_io_erro
12ca4 72 73 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  rs();.  sqlite3B
12ca5 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
12ca6 28 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 72  ();.  pPager->er
12ca7 72 43 6f 64 65 20 3d 20 30 3b 0a 20 20 70 50 61  rCode = 0;.  pPa
12ca8 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f  ger->exclusiveMo
12ca9 64 65 20 3d 20 30 3b 0a 20 20 70 61 67 65 72 5f  de = 0;.  pager_
12caa 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20  reset(pPager);. 
12cab 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20   if( MEMDB ){.  
12cac 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70    pager_unlock(p
12cad 50 61 67 65 72 29 3b 0a 20 20 7d 65 6c 73 65 7b  Pager);.  }else{
12cae 0a 20 20 20 20 2f 2a 20 53 65 74 20 50 61 67 65  .    /* Set Page
12caf 72 2e 6a 6f 75 72 6e 61 6c 48 64 72 20 74 6f 20  r.journalHdr to 
12cb0 2d 31 20 66 6f 72 20 74 68 65 20 62 65 6e 65 66  -1 for the benef
12cb1 69 74 20 6f 66 20 74 68 65 20 70 61 67 65 72 5f  it of the pager_
12cb2 70 6c 61 79 62 61 63 6b 28 29 20 0a 20 20 20 20  playback() .    
12cb3 2a 2a 20 63 61 6c 6c 20 77 68 69 63 68 20 6d 61  ** call which ma
12cb4 79 20 62 65 20 6d 61 64 65 20 66 72 6f 6d 20 77  y be made from w
12cb5 69 74 68 69 6e 20 70 61 67 65 72 55 6e 6c 6f 63  ithin pagerUnloc
12cb6 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 29 2e 20  kAndRollback(). 
12cb7 49 66 20 69 74 0a 20 20 20 20 2a 2a 20 69 73 20  If it.    ** is 
12cb8 6e 6f 74 20 2d 31 2c 20 74 68 65 6e 20 74 68 65  not -1, then the
12cb9 20 75 6e 73 79 6e 63 65 64 20 70 6f 72 74 69 6f   unsynced portio
12cba 6e 20 6f 66 20 61 6e 20 6f 70 65 6e 20 6a 6f 75  n of an open jou
12cbb 72 6e 61 6c 20 66 69 6c 65 20 6d 61 79 0a 20 20  rnal file may.  
12cbc 20 20 2a 2a 20 62 65 20 70 6c 61 79 65 64 20 62    ** be played b
12cbd 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74  ack into the dat
12cbe 61 62 61 73 65 2e 20 49 66 20 61 20 70 6f 77 65  abase. If a powe
12cbf 72 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73  r failure occurs
12cc0 20 77 68 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68   while.    ** th
12cc1 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e 67 2c  is is happening,
12cc2 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 61   the database ma
12cc3 79 20 62 65 63 6f 6d 65 20 63 6f 72 72 75 70 74  y become corrupt
12cc4 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61  ..    */.    pPa
12cc5 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20  ger->journalHdr 
12cc6 3d 20 2d 31 3b 0a 20 20 20 20 70 61 67 65 72 55  = -1;.    pagerU
12cc7 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b  nlockAndRollback
12cc8 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20  (pPager);.  }.  
12cc9 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
12cca 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 65 6e 61 62  Malloc();.  enab
12ccb 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f  le_simulated_io_
12ccc 65 72 72 6f 72 73 28 29 3b 0a 20 20 50 41 47 45  errors();.  PAGE
12ccd 52 54 52 41 43 45 28 28 22 43 4c 4f 53 45 20 25  RTRACE(("CLOSE %
12cce 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50  d\n", PAGERID(pP
12ccf 61 67 65 72 29 29 29 3b 0a 20 20 49 4f 54 52 41  ager)));.  IOTRA
12cd0 43 45 28 28 22 43 4c 4f 53 45 20 25 70 5c 6e 22  CE(("CLOSE %p\n"
12cd1 2c 20 70 50 61 67 65 72 29 29 0a 20 20 73 71 6c  , pPager)).  sql
12cd2 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67  ite3OsClose(pPag
12cd3 65 72 2d 3e 66 64 29 3b 0a 20 20 73 71 6c 69 74  er->fd);.  sqlit
12cd4 65 33 50 61 67 65 46 72 65 65 28 70 50 61 67 65  e3PageFree(pPage
12cd5 72 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20  r->pTmpSpace);. 
12cd6 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c   sqlite3PcacheCl
12cd7 6f 73 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61  ose(pPager->pPCa
12cd8 63 68 65 29 3b 0a 0a 23 69 66 64 65 66 20 53 51  che);..#ifdef SQ
12cd9 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20  LITE_HAS_CODEC. 
12cda 20 69 66 28 20 70 50 61 67 65 72 2d 3e 78 43 6f   if( pPager->xCo
12cdb 64 65 63 46 72 65 65 20 29 20 70 50 61 67 65 72  decFree ) pPager
12cdc 2d 3e 78 43 6f 64 65 63 46 72 65 65 28 70 50 61  ->xCodecFree(pPa
12cdd 67 65 72 2d 3e 70 43 6f 64 65 63 29 3b 0a 23 65  ger->pCodec);.#e
12cde 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20  ndif..  assert( 
12cdf 21 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f  !pPager->aSavepo
12ce0 69 6e 74 20 26 26 20 21 70 50 61 67 65 72 2d 3e  int && !pPager->
12ce1 70 49 6e 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20  pInJournal );.  
12ce2 61 73 73 65 72 74 28 20 21 69 73 4f 70 65 6e 28  assert( !isOpen(
12ce3 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20  pPager->jfd) && 
12ce4 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e  !isOpen(pPager->
12ce5 73 6a 66 64 29 20 29 3b 0a 0a 20 20 73 71 6c 69  sjfd) );..  sqli
12ce6 74 65 33 5f 66 72 65 65 28 70 50 61 67 65 72 29  te3_free(pPager)
12ce7 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
12ce8 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 21 64 65  E_OK;.}..#if !de
12ce9 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c  fined(NDEBUG) ||
12cea 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
12ceb 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 52 65 74 75  TEST)./*.** Retu
12cec 72 6e 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  rn the page numb
12ced 65 72 20 66 6f 72 20 70 61 67 65 20 70 50 67 2e  er for page pPg.
12cee 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
12cef 54 45 20 50 67 6e 6f 20 73 71 6c 69 74 65 33 50  TE Pgno sqlite3P
12cf0 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 28 44  agerPagenumber(D
12cf1 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 72  bPage *pPg){.  r
12cf2 65 74 75 72 6e 20 70 50 67 2d 3e 70 67 6e 6f 3b  eturn pPg->pgno;
12cf3 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
12cf4 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72   Increment the r
12cf5 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66  eference count f
12cf6 6f 72 20 70 61 67 65 20 70 50 67 2e 0a 2a 2f 0a  or page pPg..*/.
12cf7 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
12cf8 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
12cf9 52 65 66 28 44 62 50 61 67 65 20 2a 70 50 67 29  Ref(DbPage *pPg)
12cfa 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68  {.  sqlite3Pcach
12cfb 65 52 65 66 28 70 50 67 29 3b 0a 7d 0a 0a 2f 2a  eRef(pPg);.}../*
12cfc 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75  .** Sync the jou
12cfd 72 6e 61 6c 2e 20 49 6e 20 6f 74 68 65 72 20 77  rnal. In other w
12cfe 6f 72 64 73 2c 20 6d 61 6b 65 20 73 75 72 65 20  ords, make sure 
12cff 61 6c 6c 20 74 68 65 20 70 61 67 65 73 20 74 68  all the pages th
12d00 61 74 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20  at have.** been 
12d01 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a  written to the j
12d02 6f 75 72 6e 61 6c 20 68 61 76 65 20 61 63 74 75  ournal have actu
12d03 61 6c 6c 79 20 72 65 61 63 68 65 64 20 74 68 65  ally reached the
12d04 20 73 75 72 66 61 63 65 20 6f 66 20 74 68 65 0a   surface of the.
12d05 2a 2a 20 64 69 73 6b 20 61 6e 64 20 63 61 6e 20  ** disk and can 
12d06 62 65 20 72 65 73 74 6f 72 65 64 20 69 6e 20 74  be restored in t
12d07 68 65 20 65 76 65 6e 74 20 6f 66 20 61 20 68 6f  he event of a ho
12d08 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61  t-journal rollba
12d09 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ck..**.** If the
12d0a 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20   Pager.needSync 
12d0b 66 6c 61 67 20 69 73 20 6e 6f 74 20 73 65 74 2c  flag is not set,
12d0c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   then this funct
12d0d 69 6f 6e 20 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f  ion is a.** no-o
12d0e 70 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68  p. Otherwise, th
12d0f 65 20 61 63 74 69 6f 6e 73 20 72 65 71 75 69 72  e actions requir
12d10 65 64 20 64 65 70 65 6e 64 20 6f 6e 20 74 68 65  ed depend on the
12d11 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 0a 2a 2a   journal-mode.**
12d12 20 61 6e 64 20 74 68 65 20 64 65 76 69 63 65 20   and the device 
12d13 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20  characteristics 
12d14 6f 66 20 74 68 65 20 74 68 65 20 66 69 6c 65 2d  of the the file-
12d15 73 79 73 74 65 6d 2c 20 61 73 20 66 6f 6c 6c 6f  system, as follo
12d16 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66  ws:.**.**   * If
12d17 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
12d18 65 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  e is an in-memor
12d19 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20  y journal file, 
12d1a 6e 6f 20 61 63 74 69 6f 6e 20 6e 65 65 64 0a 2a  no action need.*
12d1b 2a 20 20 20 20 20 62 65 20 74 61 6b 65 6e 2e 0a  *     be taken..
12d1c 2a 2a 0a 2a 2a 20 20 20 2a 20 4f 74 68 65 72 77  **.**   * Otherw
12d1d 69 73 65 2c 20 69 66 20 74 68 65 20 64 65 76 69  ise, if the devi
12d1e 63 65 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70  ce does not supp
12d1f 6f 72 74 20 74 68 65 20 53 41 46 45 5f 41 50 50  ort the SAFE_APP
12d20 45 4e 44 20 70 72 6f 70 65 72 74 79 2c 0a 2a 2a  END property,.**
12d21 20 20 20 20 20 74 68 65 6e 20 74 68 65 20 6e 52       then the nR
12d22 65 63 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20  ec field of the 
12d23 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 77 72  most recently wr
12d24 69 74 74 65 6e 20 6a 6f 75 72 6e 61 6c 20 68 65  itten journal he
12d25 61 64 65 72 0a 2a 2a 20 20 20 20 20 69 73 20 75  ader.**     is u
12d26 70 64 61 74 65 64 20 74 6f 20 63 6f 6e 74 61 69  pdated to contai
12d27 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
12d28 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 73 20  journal records 
12d29 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 20 20 20  that have.**    
12d2a 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 66 6f   been written fo
12d2b 6c 6c 6f 77 69 6e 67 20 69 74 2e 20 49 66 20 74  llowing it. If t
12d2c 68 65 20 70 61 67 65 72 20 69 73 20 6f 70 65 72  he pager is oper
12d2d 61 74 69 6e 67 20 69 6e 20 66 75 6c 6c 2d 73 79  ating in full-sy
12d2e 6e 63 0a 2a 2a 20 20 20 20 20 6d 6f 64 65 2c 20  nc.**     mode, 
12d2f 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  then the journal
12d30 20 66 69 6c 65 20 69 73 20 73 79 6e 63 65 64 20   file is synced 
12d31 62 65 66 6f 72 65 20 74 68 69 73 20 66 69 65 6c  before this fiel
12d32 64 20 69 73 20 75 70 64 61 74 65 64 2e 0a 2a 2a  d is updated..**
12d33 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 64  .**   * If the d
12d34 65 76 69 63 65 20 64 6f 65 73 20 6e 6f 74 20 73  evice does not s
12d35 75 70 70 6f 72 74 20 74 68 65 20 53 45 51 55 45  upport the SEQUE
12d36 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 79 2c 20  NTIAL property, 
12d37 74 68 65 6e 20 0a 2a 2a 20 20 20 20 20 6a 6f 75  then .**     jou
12d38 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 73 79 6e  rnal file is syn
12d39 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69  ced..**.** Or, i
12d3a 6e 20 70 73 65 75 64 6f 2d 63 6f 64 65 3a 0a 2a  n pseudo-code:.*
12d3b 2a 0a 2a 2a 20 20 20 69 66 28 20 4e 4f 54 20 3c  *.**   if( NOT <
12d3c 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61  in-memory journa
12d3d 6c 3e 20 29 7b 0a 2a 2a 20 20 20 20 20 69 66 28  l> ){.**     if(
12d3e 20 4e 4f 54 20 53 41 46 45 5f 41 50 50 45 4e 44   NOT SAFE_APPEND
12d3f 20 29 7b 0a 2a 2a 20 20 20 20 20 20 20 69 66 28   ){.**       if(
12d40 20 3c 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65   <full-sync mode
12d41 3e 20 29 20 78 53 79 6e 63 28 3c 6a 6f 75 72 6e  > ) xSync(<journ
12d42 61 6c 20 66 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20  al file>);.**   
12d43 20 20 20 20 3c 75 70 64 61 74 65 20 6e 52 65 63      <update nRec
12d44 20 66 69 65 6c 64 3e 0a 2a 2a 20 20 20 20 20 7d   field>.**     }
12d45 20 0a 2a 2a 20 20 20 20 20 69 66 28 20 4e 4f 54   .**     if( NOT
12d46 20 53 45 51 55 45 4e 54 49 41 4c 20 29 20 78 53   SEQUENTIAL ) xS
12d47 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c  ync(<journal fil
12d48 65 3e 29 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a  e>);.**   }.**.*
12d49 2a 20 54 68 65 20 50 61 67 65 72 2e 6e 65 65 64  * The Pager.need
12d4a 53 79 6e 63 20 66 6c 61 67 20 69 73 20 6e 65 76  Sync flag is nev
12d4b 65 72 20 62 65 20 73 65 74 20 66 6f 72 20 74 65  er be set for te
12d4c 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2c 20 6f  mporary files, o
12d4d 72 20 61 6e 79 0a 2a 2a 20 66 69 6c 65 20 6f 70  r any.** file op
12d4e 65 72 61 74 69 6e 67 20 69 6e 20 6e 6f 2d 73 79  erating in no-sy
12d4f 6e 63 20 6d 6f 64 65 20 28 50 61 67 65 72 2e 6e  nc mode (Pager.n
12d50 6f 53 79 6e 63 20 73 65 74 20 74 6f 20 6e 6f 6e  oSync set to non
12d51 2d 7a 65 72 6f 29 2e 0a 2a 2a 0a 2a 2a 20 49 66  -zero)..**.** If
12d52 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 69   successful, thi
12d53 73 20 72 6f 75 74 69 6e 65 20 63 6c 65 61 72 73  s routine clears
12d54 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f   the PGHDR_NEED_
12d55 53 59 4e 43 20 66 6c 61 67 20 6f 66 20 65 76 65  SYNC flag of eve
12d56 72 79 20 0a 2a 2a 20 70 61 67 65 20 63 75 72 72  ry .** page curr
12d57 65 6e 74 6c 79 20 68 65 6c 64 20 69 6e 20 6d 65  ently held in me
12d58 6d 6f 72 79 20 62 65 66 6f 72 65 20 72 65 74 75  mory before retu
12d59 72 6e 69 6e 67 20 53 51 4c 49 54 45 5f 4f 4b 2e  rning SQLITE_OK.
12d5a 20 49 66 20 61 6e 20 49 4f 0a 2a 2a 20 65 72 72   If an IO.** err
12d5b 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
12d5c 64 2c 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65  d, then the IO e
12d5d 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
12d5e 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c  urned to the cal
12d5f 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ler..*/.static i
12d60 6e 74 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 50  nt syncJournal(P
12d61 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
12d62 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 65 65   if( pPager->nee
12d63 64 53 79 6e 63 20 29 7b 0a 20 20 20 20 61 73 73  dSync ){.    ass
12d64 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 74 65  ert( !pPager->te
12d65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 69 66  mpFile );.    if
12d66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
12d67 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55  lMode!=PAGER_JOU
12d68 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20  RNALMODE_MEMORY 
12d69 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b  ){.      int rc;
12d6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12d6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
12d6c 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
12d6d 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20        const int 
12d6e 69 44 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44  iDc = sqlite3OsD
12d6f 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
12d70 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29  tics(pPager->fd)
12d71 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
12d72 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
12d73 66 64 29 20 29 3b 0a 0a 20 20 20 20 20 20 69 66  fd) );..      if
12d74 28 20 30 3d 3d 28 69 44 63 26 53 51 4c 49 54 45  ( 0==(iDc&SQLITE
12d75 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45  _IOCAP_SAFE_APPE
12d76 4e 44 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ND) ){.        /
12d77 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 64 65 61  * This block dea
12d78 6c 73 20 77 69 74 68 20 61 6e 20 6f 62 73 63 75  ls with an obscu
12d79 72 65 20 70 72 6f 62 6c 65 6d 2e 20 49 66 20 74  re problem. If t
12d7a 68 65 20 6c 61 73 74 20 63 6f 6e 6e 65 63 74 69  he last connecti
12d7b 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  on.        ** th
12d7c 61 74 20 77 72 6f 74 65 20 74 6f 20 74 68 69 73  at wrote to this
12d7d 20 64 61 74 61 62 61 73 65 20 77 61 73 20 6f 70   database was op
12d7e 65 72 61 74 69 6e 67 20 69 6e 20 70 65 72 73 69  erating in persi
12d7f 73 74 65 6e 74 2d 6a 6f 75 72 6e 61 6c 0a 20 20  stent-journal.  
12d80 20 20 20 20 20 20 2a 2a 20 6d 6f 64 65 2c 20 74        ** mode, t
12d81 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  hen the journal 
12d82 66 69 6c 65 20 6d 61 79 20 61 74 20 74 68 69 73  file may at this
12d83 20 70 6f 69 6e 74 20 61 63 74 75 61 6c 6c 79 20   point actually 
12d84 62 65 20 6c 61 72 67 65 72 0a 20 20 20 20 20 20  be larger.      
12d85 20 20 2a 2a 20 74 68 61 6e 20 50 61 67 65 72 2e    ** than Pager.
12d86 6a 6f 75 72 6e 61 6c 4f 66 66 20 62 79 74 65 73  journalOff bytes
12d87 2e 20 49 66 20 74 68 65 20 6e 65 78 74 20 74 68  . If the next th
12d88 69 6e 67 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  ing in the journ
12d89 61 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 69  al.        ** fi
12d8a 6c 65 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65  le happens to be
12d8b 20 61 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65   a journal-heade
12d8c 72 20 28 77 72 69 74 74 65 6e 20 61 73 20 70 61  r (written as pa
12d8d 72 74 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20  rt of the.      
12d8e 20 20 2a 2a 20 70 72 65 76 69 6f 75 73 20 63 6f    ** previous co
12d8f 6e 6e 65 63 74 69 6f 6e 73 20 74 72 61 6e 73 61  nnections transa
12d90 63 74 69 6f 6e 29 2c 20 61 6e 64 20 61 20 63 72  ction), and a cr
12d91 61 73 68 20 6f 72 20 70 6f 77 65 72 2d 66 61 69  ash or power-fai
12d92 6c 75 72 65 20 0a 20 20 20 20 20 20 20 20 2a 2a  lure .        **
12d93 20 6f 63 63 75 72 73 20 61 66 74 65 72 20 6e 52   occurs after nR
12d94 65 63 20 69 73 20 75 70 64 61 74 65 64 20 62 75  ec is updated bu
12d95 74 20 62 65 66 6f 72 65 20 74 68 69 73 20 63 6f  t before this co
12d96 6e 6e 65 63 74 69 6f 6e 20 77 72 69 74 65 73 20  nnection writes 
12d97 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 74  .        ** anyt
12d98 68 69 6e 67 20 65 6c 73 65 20 74 6f 20 74 68 65  hing else to the
12d99 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f   journal file (o
12d9a 72 20 63 6f 6d 6d 69 74 73 2f 72 6f 6c 6c 73 20  r commits/rolls 
12d9b 62 61 63 6b 20 69 74 73 20 0a 20 20 20 20 20 20  back its .      
12d9c 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
12d9d 29 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 6d  ), then SQLite m
12d9e 61 79 20 62 65 63 6f 6d 65 20 63 6f 6e 66 75 73  ay become confus
12d9f 65 64 20 77 68 65 6e 20 64 6f 69 6e 67 20 74 68  ed when doing th
12da0 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 68 6f  e .        ** ho
12da1 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61  t-journal rollba
12da2 63 6b 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 63  ck following rec
12da3 6f 76 65 72 79 2e 20 49 74 20 6d 61 79 20 72 6f  overy. It may ro
12da4 6c 6c 20 62 61 63 6b 20 61 6c 6c 0a 20 20 20 20  ll back all.    
12da5 20 20 20 20 2a 2a 20 6f 66 20 74 68 69 73 20 63      ** of this c
12da6 6f 6e 6e 65 63 74 69 6f 6e 73 20 64 61 74 61 2c  onnections data,
12da7 20 74 68 65 6e 20 70 72 6f 63 65 65 64 20 74 6f   then proceed to
12da8 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 74 68   rolling back th
12da9 65 20 6f 6c 64 2c 0a 20 20 20 20 20 20 20 20 2a  e old,.        *
12daa 2a 20 6f 75 74 2d 6f 66 2d 64 61 74 65 20 64 61  * out-of-date da
12dab 74 61 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20  ta that follows 
12dac 69 74 2e 20 44 61 74 61 62 61 73 65 20 63 6f 72  it. Database cor
12dad 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20  ruption..       
12dae 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54   **.        ** T
12daf 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68  o work around th
12db0 69 73 2c 20 69 66 20 74 68 65 20 6a 6f 75 72 6e  is, if the journ
12db1 61 6c 20 66 69 6c 65 20 64 6f 65 73 20 61 70 70  al file does app
12db2 65 61 72 20 74 6f 20 63 6f 6e 74 61 69 6e 0a 20  ear to contain. 
12db3 20 20 20 20 20 20 20 2a 2a 20 61 20 76 61 6c 69         ** a vali
12db4 64 20 68 65 61 64 65 72 20 66 6f 6c 6c 6f 77 69  d header followi
12db5 6e 67 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c  ng Pager.journal
12db6 4f 66 66 2c 20 74 68 65 6e 20 77 72 69 74 65 20  Off, then write 
12db7 61 20 30 78 30 30 0a 20 20 20 20 20 20 20 20 2a  a 0x00.        *
12db8 2a 20 62 79 74 65 20 74 6f 20 74 68 65 20 73 74  * byte to the st
12db9 61 72 74 20 6f 66 20 69 74 20 74 6f 20 70 72 65  art of it to pre
12dba 76 65 6e 74 20 69 74 20 66 72 6f 6d 20 62 65 69  vent it from bei
12dbb 6e 67 20 72 65 63 6f 67 6e 69 7a 65 64 2e 0a 20  ng recognized.. 
12dbc 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
12dbd 20 20 2a 2a 20 56 61 72 69 61 62 6c 65 20 69 4e    ** Variable iN
12dbe 65 78 74 48 64 72 4f 66 66 73 65 74 20 69 73 20  extHdrOffset is 
12dbf 73 65 74 20 74 6f 20 74 68 65 20 6f 66 66 73 65  set to the offse
12dc0 74 20 61 74 20 77 68 69 63 68 20 74 68 69 73 0a  t at which this.
12dc1 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 62 6c          ** probl
12dc2 65 6d 61 74 69 63 20 68 65 61 64 65 72 20 77 69  ematic header wi
12dc3 6c 6c 20 6f 63 63 75 72 2c 20 69 66 20 69 74 20  ll occur, if it 
12dc4 65 78 69 73 74 73 2e 20 61 4d 61 67 69 63 20 69  exists. aMagic i
12dc5 73 20 75 73 65 64 20 0a 20 20 20 20 20 20 20 20  s used .        
12dc6 2a 2a 20 61 73 20 61 20 74 65 6d 70 6f 72 61 72  ** as a temporar
12dc7 79 20 62 75 66 66 65 72 20 74 6f 20 69 6e 73 70  y buffer to insp
12dc8 65 63 74 20 74 68 65 20 66 69 72 73 74 20 63 6f  ect the first co
12dc9 75 70 6c 65 20 6f 66 20 62 79 74 65 73 20 6f 66  uple of bytes of
12dca 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20  .        ** the 
12dcb 70 6f 74 65 6e 74 69 61 6c 20 6a 6f 75 72 6e 61  potential journa
12dcc 6c 20 68 65 61 64 65 72 2e 0a 20 20 20 20 20 20  l header..      
12dcd 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 36 34    */.        i64
12dce 20 69 4e 65 78 74 48 64 72 4f 66 66 73 65 74 3b   iNextHdrOffset;
12dcf 0a 20 20 20 20 20 20 20 20 75 38 20 61 4d 61 67  .        u8 aMag
12dd0 69 63 5b 38 5d 3b 0a 09 75 38 20 7a 48 65 61 64  ic[8];..u8 zHead
12dd1 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e  er[sizeof(aJourn
12dd2 61 6c 4d 61 67 69 63 29 2b 34 5d 3b 0a 0a 09 6d  alMagic)+4];...m
12dd3 65 6d 63 70 79 28 7a 48 65 61 64 65 72 2c 20 61  emcpy(zHeader, a
12dd4 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69  JournalMagic, si
12dd5 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
12dd6 69 63 29 29 3b 0a 09 70 75 74 33 32 62 69 74 73  ic));..put32bits
12dd7 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66  (&zHeader[sizeof
12dd8 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d  (aJournalMagic)]
12dd9 2c 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 29 3b  , pPager->nRec);
12dda 0a 0a 20 20 20 20 20 20 20 20 69 4e 65 78 74 48  ..        iNextH
12ddb 64 72 4f 66 66 73 65 74 20 3d 20 6a 6f 75 72 6e  drOffset = journ
12ddc 61 6c 48 64 72 4f 66 66 73 65 74 28 70 50 61 67  alHdrOffset(pPag
12ddd 65 72 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  er);.        rc 
12dde 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28  = sqlite3OsRead(
12ddf 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 61 4d 61  pPager->jfd, aMa
12de0 67 69 63 2c 20 38 2c 20 69 4e 65 78 74 48 64 72  gic, 8, iNextHdr
12de1 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 20  Offset);.       
12de2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
12de3 4f 4b 20 26 26 20 30 3d 3d 6d 65 6d 63 6d 70 28  OK && 0==memcmp(
12de4 61 4d 61 67 69 63 2c 20 61 4a 6f 75 72 6e 61 6c  aMagic, aJournal
12de5 4d 61 67 69 63 2c 20 38 29 20 29 7b 0a 20 20 20  Magic, 8) ){.   
12de6 20 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f         static co
12de7 6e 73 74 20 75 38 20 7a 65 72 6f 62 79 74 65 20  nst u8 zerobyte 
12de8 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 72  = 0;.          r
12de9 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
12dea 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  te(pPager->jfd, 
12deb 26 7a 65 72 6f 62 79 74 65 2c 20 31 2c 20 69 4e  &zerobyte, 1, iN
12dec 65 78 74 48 64 72 4f 66 66 73 65 74 29 3b 0a 20  extHdrOffset);. 
12ded 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
12dee 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
12def 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45  OK && rc!=SQLITE
12df0 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41  _IOERR_SHORT_REA
12df1 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  D ){.          r
12df2 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
12df3 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20    }..        /* 
12df4 57 72 69 74 65 20 74 68 65 20 6e 52 65 63 20 76  Write the nRec v
12df5 61 6c 75 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f  alue into the jo
12df6 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 61 64 65  urnal file heade
12df7 72 2e 20 49 66 20 69 6e 0a 20 20 20 20 20 20 20  r. If in.       
12df8 20 2a 2a 20 66 75 6c 6c 2d 73 79 6e 63 68 72 6f   ** full-synchro
12df9 6e 6f 75 73 20 6d 6f 64 65 2c 20 73 79 6e 63 20  nous mode, sync 
12dfa 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 72 73  the journal firs
12dfb 74 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73 20  t. This ensures 
12dfc 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20  that.        ** 
12dfd 61 6c 6c 20 64 61 74 61 20 68 61 73 20 72 65 61  all data has rea
12dfe 6c 6c 79 20 68 69 74 20 74 68 65 20 64 69 73 6b  lly hit the disk
12dff 20 62 65 66 6f 72 65 20 6e 52 65 63 20 69 73 20   before nRec is 
12e00 75 70 64 61 74 65 64 20 74 6f 20 6d 61 72 6b 0a  updated to mark.
12e01 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20 61 73          ** it as
12e02 20 61 20 63 61 6e 64 69 64 61 74 65 20 66 6f 72   a candidate for
12e03 20 72 6f 6c 6c 62 61 63 6b 2e 0a 20 20 20 20 20   rollback..     
12e04 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
12e05 20 54 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71   This is not req
12e06 75 69 72 65 64 20 69 66 20 74 68 65 20 70 65 72  uired if the per
12e07 73 69 73 74 65 6e 74 20 6d 65 64 69 61 20 73 75  sistent media su
12e08 70 70 6f 72 74 73 20 74 68 65 0a 20 20 20 20 20  pports the.     
12e09 20 20 20 2a 2a 20 53 41 46 45 5f 41 50 50 45 4e     ** SAFE_APPEN
12e0a 44 20 70 72 6f 70 65 72 74 79 2e 20 42 65 63 61  D property. Beca
12e0b 75 73 65 20 69 6e 20 74 68 69 73 20 63 61 73 65  use in this case
12e0c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69   it is not possi
12e0d 62 6c 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  ble .        ** 
12e0e 66 6f 72 20 67 61 72 62 61 67 65 20 64 61 74 61  for garbage data
12e0f 20 74 6f 20 62 65 20 61 70 70 65 6e 64 65 64 20   to be appended 
12e10 74 6f 20 74 68 65 20 66 69 6c 65 2c 20 74 68 65  to the file, the
12e11 20 6e 52 65 63 20 66 69 65 6c 64 0a 20 20 20 20   nRec field.    
12e12 20 20 20 20 2a 2a 20 69 73 20 70 6f 70 75 6c 61      ** is popula
12e13 74 65 64 20 77 69 74 68 20 30 78 46 46 46 46 46  ted with 0xFFFFF
12e14 46 46 46 20 77 68 65 6e 20 74 68 65 20 6a 6f 75  FFF when the jou
12e15 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20 77  rnal header is w
12e16 72 69 74 74 65 6e 0a 20 20 20 20 20 20 20 20 2a  ritten.        *
12e17 2a 20 61 6e 64 20 6e 65 76 65 72 20 6e 65 65 64  * and never need
12e18 73 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e  s to be updated.
12e19 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
12e1a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
12e1b 66 75 6c 6c 53 79 6e 63 20 26 26 20 30 3d 3d 28  fullSync && 0==(
12e1c 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50  iDc&SQLITE_IOCAP
12e1d 5f 53 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a  _SEQUENTIAL) ){.
12e1e 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52 54            PAGERT
12e1f 52 41 43 45 28 28 22 53 59 4e 43 20 6a 6f 75 72  RACE(("SYNC jour
12e20 6e 61 6c 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41  nal of %d\n", PA
12e21 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b  GERID(pPager)));
12e22 0a 20 20 20 20 20 20 20 20 20 20 49 4f 54 52 41  .          IOTRA
12e23 43 45 28 28 22 4a 53 59 4e 43 20 25 70 5c 6e 22  CE(("JSYNC %p\n"
12e24 2c 20 70 50 61 67 65 72 29 29 0a 20 20 20 20 20  , pPager)).     
12e25 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
12e26 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e  3OsSync(pPager->
12e27 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e  jfd, pPager->syn
12e28 63 5f 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 20  c_flags);.      
12e29 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
12e2a 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
12e2b 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  c;.        }.   
12e2c 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a       IOTRACE(("J
12e2d 48 44 52 20 25 70 20 25 6c 6c 64 5c 6e 22 2c 20  HDR %p %lld\n", 
12e2e 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e  pPager, pPager->
12e2f 6a 6f 75 72 6e 61 6c 48 64 72 29 29 3b 0a 20 20  journalHdr));.  
12e30 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
12e31 65 33 4f 73 57 72 69 74 65 28 0a 20 20 20 20 20  e3OsWrite(.     
12e32 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a         pPager->j
12e33 66 64 2c 20 7a 48 65 61 64 65 72 2c 20 73 69 7a  fd, zHeader, siz
12e34 65 6f 66 28 7a 48 65 61 64 65 72 29 2c 20 70 50  eof(zHeader), pP
12e35 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72  ager->journalHdr
12e36 0a 09 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ..);.        if(
12e37 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
12e38 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
12e39 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 30 3d    }.      if( 0=
12e3a 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43  =(iDc&SQLITE_IOC
12e3b 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 29 20 29  AP_SEQUENTIAL) )
12e3c 7b 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 54  {.        PAGERT
12e3d 52 41 43 45 28 28 22 53 59 4e 43 20 6a 6f 75 72  RACE(("SYNC jour
12e3e 6e 61 6c 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41  nal of %d\n", PA
12e3f 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b  GERID(pPager)));
12e40 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45  .        IOTRACE
12e41 28 28 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20  (("JSYNC %p\n", 
12e42 70 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 20  pPager)).       
12e43 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53   rc = sqlite3OsS
12e44 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ync(pPager->jfd,
12e45 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c   pPager->sync_fl
12e46 61 67 73 7c 20 0a 20 20 20 20 20 20 20 20 20 20  ags| .          
12e47 28 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c  (pPager->sync_fl
12e48 61 67 73 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43  ags==SQLITE_SYNC
12e49 5f 46 55 4c 4c 3f 53 51 4c 49 54 45 5f 53 59 4e  _FULL?SQLITE_SYN
12e4a 43 5f 44 41 54 41 4f 4e 4c 59 3a 30 29 0a 20 20  C_DATAONLY:0).  
12e4b 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20        );.       
12e4c 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
12e4d 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
12e4e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
12e4f 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61     /* The journa
12e50 6c 20 66 69 6c 65 20 77 61 73 20 6a 75 73 74 20  l file was just 
12e51 73 75 63 63 65 73 73 66 75 6c 6c 79 20 73 79 6e  successfully syn
12e52 63 65 64 2e 20 53 65 74 20 50 61 67 65 72 2e 6e  ced. Set Pager.n
12e53 65 65 64 53 79 6e 63 20 0a 20 20 20 20 2a 2a 20  eedSync .    ** 
12e54 74 6f 20 7a 65 72 6f 20 61 6e 64 20 63 6c 65 61  to zero and clea
12e55 72 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44  r the PGHDR_NEED
12e56 5f 53 59 4e 43 20 66 6c 61 67 20 6f 6e 20 61 6c  _SYNC flag on al
12e57 6c 20 70 61 67 65 73 73 2e 0a 20 20 20 20 2a 2f  l pagess..    */
12e58 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65  .    pPager->nee
12e59 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70  dSync = 0;.    p
12e5a 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74  Pager->journalSt
12e5b 61 72 74 65 64 20 3d 20 31 3b 0a 20 20 20 20 73  arted = 1;.    s
12e5c 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61  qlite3PcacheClea
12e5d 72 53 79 6e 63 46 6c 61 67 73 28 70 50 61 67 65  rSyncFlags(pPage
12e5e 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 7d  r->pPCache);.  }
12e5f 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
12e60 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK;.}../*.** T
12e61 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  he argument is t
12e62 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69  he first in a li
12e63 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 64 69 72  nked list of dir
12e64 74 79 20 70 61 67 65 73 20 63 6f 6e 6e 65 63 74  ty pages connect
12e65 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 50 67 48  ed.** by the PgH
12e66 64 72 2e 70 44 69 72 74 79 20 70 6f 69 6e 74 65  dr.pDirty pointe
12e67 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  r. This function
12e68 20 77 72 69 74 65 73 20 65 61 63 68 20 6f 6e 65   writes each one
12e69 20 6f 66 20 74 68 65 0a 2a 2a 20 69 6e 2d 6d 65   of the.** in-me
12e6a 6d 6f 72 79 20 70 61 67 65 73 20 69 6e 20 74 68  mory pages in th
12e6b 65 20 6c 69 73 74 20 74 6f 20 74 68 65 20 64 61  e list to the da
12e6c 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65  tabase file. The
12e6d 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 0a 2a 2a   argument may.**
12e6e 20 62 65 20 4e 55 4c 4c 2c 20 72 65 70 72 65 73   be NULL, repres
12e6f 65 6e 74 69 6e 67 20 61 6e 20 65 6d 70 74 79 20  enting an empty 
12e70 6c 69 73 74 2e 20 49 6e 20 74 68 69 73 20 63 61  list. In this ca
12e71 73 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  se this function
12e72 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 0a   is.** a no-op..
12e73 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20  **.** The pager 
12e74 6d 75 73 74 20 68 6f 6c 64 20 61 74 20 6c 65 61  must hold at lea
12e75 73 74 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  st a RESERVED lo
12e76 63 6b 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e  ck when this fun
12e77 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 61 6c 6c  ction.** is call
12e78 65 64 2e 20 42 65 66 6f 72 65 20 77 72 69 74 69  ed. Before writi
12e79 6e 67 20 61 6e 79 74 68 69 6e 67 20 74 6f 20 74  ng anything to t
12e7a 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12e7b 2c 20 74 68 69 73 20 6c 6f 63 6b 0a 2a 2a 20 69  , this lock.** i
12e7c 73 20 75 70 67 72 61 64 65 64 20 74 6f 20 61 6e  s upgraded to an
12e7d 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e   EXCLUSIVE lock.
12e7e 20 49 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e   If the lock can
12e7f 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2c  not be obtained,
12e80 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20  .** SQLITE_BUSY 
12e81 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
12e82 6e 6f 20 64 61 74 61 20 69 73 20 77 72 69 74 74  no data is writt
12e83 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  en to the databa
12e84 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20  se file..** .** 
12e85 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20  If the pager is 
12e86 61 20 74 65 6d 70 2d 66 69 6c 65 20 70 61 67 65  a temp-file page
12e87 72 20 61 6e 64 20 74 68 65 20 61 63 74 75 61 6c  r and the actual
12e88 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 66 69 6c   file-system fil
12e89 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 79 65 74 20  e.** is not yet 
12e8a 6f 70 65 6e 2c 20 69 74 20 69 73 20 63 72 65 61  open, it is crea
12e8b 74 65 64 20 61 6e 64 20 6f 70 65 6e 65 64 20 62  ted and opened b
12e8c 65 66 6f 72 65 20 61 6e 79 20 64 61 74 61 20 69  efore any data i
12e8d 73 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20 6f 75  s .** written ou
12e8e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68  t..**.** Once th
12e8f 65 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20  e lock has been 
12e90 75 70 67 72 61 64 65 64 20 61 6e 64 2c 20 69 66  upgraded and, if
12e91 20 6e 65 63 65 73 73 61 72 79 2c 20 74 68 65 20   necessary, the 
12e92 66 69 6c 65 20 6f 70 65 6e 65 64 2c 0a 2a 2a 20  file opened,.** 
12e93 74 68 65 20 70 61 67 65 73 20 61 72 65 20 77 72  the pages are wr
12e94 69 74 74 65 6e 20 6f 75 74 20 74 6f 20 74 68 65  itten out to the
12e95 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
12e96 6e 20 6c 69 73 74 20 6f 72 64 65 72 2e 20 57 72  n list order. Wr
12e97 69 74 69 6e 67 0a 2a 2a 20 61 20 70 61 67 65 20  iting.** a page 
12e98 69 73 20 73 6b 69 70 70 65 64 20 69 66 20 69 74  is skipped if it
12e99 20 6d 65 65 74 73 20 65 69 74 68 65 72 20 6f 66   meets either of
12e9a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   the following c
12e9b 72 69 74 65 72 69 61 3a 0a 2a 2a 0a 2a 2a 20 20  riteria:.**.**  
12e9c 20 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62   * The page numb
12e9d 65 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68  er is greater th
12e9e 61 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c  an Pager.dbSize,
12e9f 20 6f 72 0a 2a 2a 20 20 20 2a 20 54 68 65 20 50   or.**   * The P
12ea0 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20  GHDR_DONT_WRITE 
12ea1 66 6c 61 67 20 69 73 20 73 65 74 20 6f 6e 20 74  flag is set on t
12ea2 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49  he page..**.** I
12ea3 66 20 77 72 69 74 69 6e 67 20 6f 75 74 20 61 20  f writing out a 
12ea4 70 61 67 65 20 63 61 75 73 65 73 20 74 68 65 20  page causes the 
12ea5 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f  database file to
12ea6 20 67 72 6f 77 2c 20 50 61 67 65 72 2e 64 62 46   grow, Pager.dbF
12ea7 69 6c 65 53 69 7a 65 0a 2a 2a 20 69 73 20 75 70  ileSize.** is up
12ea8 64 61 74 65 64 20 61 63 63 6f 72 64 69 6e 67 6c  dated accordingl
12ea9 79 2e 20 49 66 20 70 61 67 65 20 31 20 69 73 20  y. If page 1 is 
12eaa 77 72 69 74 74 65 6e 20 6f 75 74 2c 20 74 68 65  written out, the
12eab 6e 20 74 68 65 20 76 61 6c 75 65 20 63 61 63 68  n the value cach
12eac 65 64 0a 2a 2a 20 69 6e 20 50 61 67 65 72 2e 64  ed.** in Pager.d
12ead 62 46 69 6c 65 56 65 72 73 5b 5d 20 69 73 20 75  bFileVers[] is u
12eae 70 64 61 74 65 64 20 74 6f 20 6d 61 74 63 68 20  pdated to match 
12eaf 74 68 65 20 6e 65 77 20 76 61 6c 75 65 20 73 74  the new value st
12eb0 6f 72 65 64 20 69 6e 0a 2a 2a 20 74 68 65 20 64  ored in.** the d
12eb1 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a  atabase file..**
12eb2 0a 2a 2a 20 49 66 20 65 76 65 72 79 74 68 69 6e  .** If everythin
12eb3 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c  g is successful,
12eb4 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
12eb5 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f  turned. If an IO
12eb6 20 65 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72   error .** occur
12eb7 73 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63  s, an IO error c
12eb8 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
12eb9 20 4f 72 2c 20 69 66 20 74 68 65 20 45 58 43 4c   Or, if the EXCL
12eba 55 53 49 56 45 20 6c 6f 63 6b 20 63 61 6e 6e 6f  USIVE lock canno
12ebb 74 0a 2a 2a 20 62 65 20 6f 62 74 61 69 6e 65 64  t.** be obtained
12ebc 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73  , SQLITE_BUSY is
12ebd 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74   returned..*/.st
12ebe 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77  atic int pager_w
12ebf 72 69 74 65 5f 70 61 67 65 6c 69 73 74 28 50 67  rite_pagelist(Pg
12ec0 48 64 72 20 2a 70 4c 69 73 74 29 7b 0a 20 20 50  Hdr *pList){.  P
12ec1 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20  ager *pPager;   
12ec2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12ec3 20 20 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a      /* Pager obj
12ec4 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  ect */.  int rc;
12ec5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12ec6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
12ec7 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
12ec8 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 4c 69  .  if( NEVER(pLi
12ec9 73 74 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20  st==0) ) return 
12eca 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 50 61  SQLITE_OK;.  pPa
12ecb 67 65 72 20 3d 20 70 4c 69 73 74 2d 3e 70 50 61  ger = pList->pPa
12ecc 67 65 72 3b 0a 0a 20 20 2f 2a 20 41 74 20 74 68  ger;..  /* At th
12ecd 69 73 20 70 6f 69 6e 74 20 74 68 65 72 65 20 6d  is point there m
12ece 61 79 20 62 65 20 65 69 74 68 65 72 20 61 20 52  ay be either a R
12ecf 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55  ESERVED or EXCLU
12ed0 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  SIVE lock on the
12ed1 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66  .  ** database f
12ed2 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 73  ile. If there is
12ed3 20 61 6c 72 65 61 64 79 20 61 6e 20 45 58 43 4c   already an EXCL
12ed4 55 53 49 56 45 20 6c 6f 63 6b 2c 20 74 68 65 20  USIVE lock, the 
12ed5 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 63  following.  ** c
12ed6 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  all is a no-op..
12ed7 20 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 76 69 6e 67    **.  ** Moving
12ed8 20 74 68 65 20 6c 6f 63 6b 20 66 72 6f 6d 20 52   the lock from R
12ed9 45 53 45 52 56 45 44 20 74 6f 20 45 58 43 4c 55  ESERVED to EXCLU
12eda 53 49 56 45 20 61 63 74 75 61 6c 6c 79 20 69 6e  SIVE actually in
12edb 76 6f 6c 76 65 73 20 67 6f 69 6e 67 0a 20 20 2a  volves going.  *
12edc 2a 20 74 68 72 6f 75 67 68 20 61 6e 20 69 6e 74  * through an int
12edd 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 20  ermediate state 
12ede 50 45 4e 44 49 4e 47 2e 20 20 20 41 20 50 45 4e  PENDING.   A PEN
12edf 44 49 4e 47 20 6c 6f 63 6b 20 70 72 65 76 65 6e  DING lock preven
12ee0 74 73 20 6e 65 77 0a 20 20 2a 2a 20 72 65 61 64  ts new.  ** read
12ee1 65 72 73 20 66 72 6f 6d 20 61 74 74 61 63 68 69  ers from attachi
12ee2 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ng to the databa
12ee3 73 65 20 62 75 74 20 69 73 20 75 6e 73 75 66 66  se but is unsuff
12ee4 69 63 69 65 6e 74 20 66 6f 72 20 75 73 20 74 6f  icient for us to
12ee5 0a 20 20 2a 2a 20 77 72 69 74 65 2e 20 20 54 68  .  ** write.  Th
12ee6 65 20 69 64 65 61 20 6f 66 20 61 20 50 45 4e 44  e idea of a PEND
12ee7 49 4e 47 20 6c 6f 63 6b 20 69 73 20 74 6f 20 70  ING lock is to p
12ee8 72 65 76 65 6e 74 20 6e 65 77 20 72 65 61 64 65  revent new reade
12ee9 72 73 20 66 72 6f 6d 0a 20 20 2a 2a 20 63 6f 6d  rs from.  ** com
12eea 69 6e 67 20 69 6e 20 77 68 69 6c 65 20 77 65 20  ing in while we 
12eeb 77 61 69 74 20 66 6f 72 20 65 78 69 73 74 69 6e  wait for existin
12eec 67 20 72 65 61 64 65 72 73 20 74 6f 20 63 6c 65  g readers to cle
12eed 61 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68  ar..  **.  ** Wh
12eee 69 6c 65 20 74 68 65 20 70 61 67 65 72 20 69 73  ile the pager is
12eef 20 69 6e 20 74 68 65 20 52 45 53 45 52 56 45 44   in the RESERVED
12ef0 20 73 74 61 74 65 2c 20 74 68 65 20 6f 72 69 67   state, the orig
12ef1 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 69  inal database fi
12ef2 6c 65 0a 20 20 2a 2a 20 69 73 20 75 6e 63 68 61  le.  ** is uncha
12ef3 6e 67 65 64 20 61 6e 64 20 77 65 20 63 61 6e 20  nged and we can 
12ef4 72 6f 6c 6c 62 61 63 6b 20 77 69 74 68 6f 75 74  rollback without
12ef5 20 68 61 76 69 6e 67 20 74 6f 20 70 6c 61 79 62   having to playb
12ef6 61 63 6b 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 75  ack the.  ** jou
12ef7 72 6e 61 6c 20 69 6e 74 6f 20 74 68 65 20 6f 72  rnal into the or
12ef8 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20  iginal database 
12ef9 66 69 6c 65 2e 20 20 4f 6e 63 65 20 77 65 20 74  file.  Once we t
12efa 72 61 6e 73 69 74 69 6f 6e 20 74 6f 0a 20 20 2a  ransition to.  *
12efb 2a 20 45 58 43 4c 55 53 49 56 45 2c 20 69 74 20  * EXCLUSIVE, it 
12efc 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 61  means the databa
12efd 73 65 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e  se file has been
12efe 20 63 68 61 6e 67 65 64 20 61 6e 64 20 61 6e 79   changed and any
12eff 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 77   rollback.  ** w
12f00 69 6c 6c 20 72 65 71 75 69 72 65 20 61 20 6a 6f  ill require a jo
12f01 75 72 6e 61 6c 20 70 6c 61 79 62 61 63 6b 2e 0a  urnal playback..
12f02 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
12f03 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41  Pager->state>=PA
12f04 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a  GER_RESERVED );.
12f05 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69    rc = pager_wai
12f06 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72  t_on_lock(pPager
12f07 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  , EXCLUSIVE_LOCK
12f08 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
12f09 66 69 6c 65 20 69 73 20 61 20 74 65 6d 70 2d 66  file is a temp-f
12f0a 69 6c 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20  ile has not yet 
12f0b 62 65 65 6e 20 6f 70 65 6e 65 64 2c 20 6f 70 65  been opened, ope
12f0c 6e 20 69 74 20 6e 6f 77 2e 20 49 74 0a 20 20 2a  n it now. It.  *
12f0d 2a 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  * is not possibl
12f0e 65 20 66 6f 72 20 72 63 20 74 6f 20 62 65 20 6f  e for rc to be o
12f0f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45  ther than SQLITE
12f10 5f 4f 4b 20 69 66 20 74 68 69 73 20 62 72 61 6e  _OK if this bran
12f11 63 68 0a 20 20 2a 2a 20 69 73 20 74 61 6b 65 6e  ch.  ** is taken
12f12 2c 20 61 73 20 70 61 67 65 72 5f 77 61 69 74 5f  , as pager_wait_
12f13 6f 6e 5f 6c 6f 63 6b 28 29 20 69 73 20 61 20 6e  on_lock() is a n
12f14 6f 2d 6f 70 20 66 6f 72 20 74 65 6d 70 2d 66 69  o-op for temp-fi
12f15 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  les..  */.  if( 
12f16 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e  !isOpen(pPager->
12f17 66 64 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72  fd) ){.    asser
12f18 74 28 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  t( pPager->tempF
12f19 69 6c 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  ile && rc==SQLIT
12f1a 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 72 63 20 3d  E_OK );.    rc =
12f1b 20 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 70   pagerOpentemp(p
12f1c 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 66  Pager, pPager->f
12f1d 64 2c 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c  d, pPager->vfsFl
12f1e 61 67 73 29 3b 0a 20 20 7d 0a 0a 20 20 77 68 69  ags);.  }..  whi
12f1f 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  le( rc==SQLITE_O
12f20 4b 20 26 26 20 70 4c 69 73 74 20 29 7b 0a 20 20  K && pList ){.  
12f21 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 4c    Pgno pgno = pL
12f22 69 73 74 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 20 20  ist->pgno;..    
12f23 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  /* If there are 
12f24 64 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 74  dirty pages in t
12f25 68 65 20 70 61 67 65 20 63 61 63 68 65 20 77 69  he page cache wi
12f26 74 68 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20  th page numbers 
12f27 67 72 65 61 74 65 72 0a 20 20 20 20 2a 2a 20 74  greater.    ** t
12f28 68 61 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65  han Pager.dbSize
12f29 2c 20 74 68 69 73 20 6d 65 61 6e 73 20 73 71 6c  , this means sql
12f2a 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
12f2b 65 49 6d 61 67 65 28 29 20 77 61 73 20 63 61 6c  eImage() was cal
12f2c 6c 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20 6d 61  led to.    ** ma
12f2d 6b 65 20 74 68 65 20 66 69 6c 65 20 73 6d 61 6c  ke the file smal
12f2e 6c 65 72 20 28 70 72 65 73 75 6d 61 62 6c 79 20  ler (presumably 
12f2f 62 79 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 63  by auto-vacuum c
12f30 6f 64 65 29 2e 20 44 6f 20 6e 6f 74 20 77 72 69  ode). Do not wri
12f31 74 65 0a 20 20 20 20 2a 2a 20 61 6e 79 20 73 75  te.    ** any su
12f32 63 68 20 70 61 67 65 73 20 74 6f 20 74 68 65 20  ch pages to the 
12f33 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  file..    **.   
12f34 20 2a 2a 20 41 6c 73 6f 2c 20 64 6f 20 6e 6f 74   ** Also, do not
12f35 20 77 72 69 74 65 20 6f 75 74 20 61 6e 79 20 70   write out any p
12f36 61 67 65 20 74 68 61 74 20 68 61 73 20 74 68 65  age that has the
12f37 20 50 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54   PGHDR_DONT_WRIT
12f38 45 20 66 6c 61 67 0a 20 20 20 20 2a 2a 20 73 65  E flag.    ** se
12f39 74 20 28 73 65 74 20 62 79 20 73 71 6c 69 74 65  t (set by sqlite
12f3a 33 50 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28  3PagerDontWrite(
12f3b 29 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ))..    */.    i
12f3c 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d  f( pgno<=pPager-
12f3d 3e 64 62 53 69 7a 65 20 26 26 20 30 3d 3d 28 70  >dbSize && 0==(p
12f3e 4c 69 73 74 2d 3e 66 6c 61 67 73 26 50 47 48 44  List->flags&PGHD
12f3f 52 5f 44 4f 4e 54 5f 57 52 49 54 45 29 20 29 7b  R_DONT_WRITE) ){
12f40 0a 20 20 20 20 20 20 69 36 34 20 6f 66 66 73 65  .      i64 offse
12f41 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36  t = (pgno-1)*(i6
12f42 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  4)pPager->pageSi
12f43 7a 65 3b 20 20 20 2f 2a 20 4f 66 66 73 65 74 20  ze;   /* Offset 
12f44 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 20 20  to write */.    
12f45 20 20 63 68 61 72 20 2a 70 44 61 74 61 3b 20 20    char *pData;  
12f46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f48 20 2f 2a 20 44 61 74 61 20 74 6f 20 77 72 69 74   /* Data to writ
12f49 65 20 2a 2f 20 20 20 20 0a 0a 20 20 20 20 20 20  e */    ..      
12f4a 2f 2a 20 45 6e 63 6f 64 65 20 74 68 65 20 64 61  /* Encode the da
12f4b 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20  tabase */.      
12f4c 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 70  CODEC2(pPager, p
12f4d 4c 69 73 74 2d 3e 70 44 61 74 61 2c 20 70 67 6e  List->pData, pgn
12f4e 6f 2c 20 36 2c 20 72 65 74 75 72 6e 20 53 51 4c  o, 6, return SQL
12f4f 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61  ITE_NOMEM, pData
12f50 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 57 72 69  );..      /* Wri
12f51 74 65 20 6f 75 74 20 74 68 65 20 70 61 67 65 20  te out the page 
12f52 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20 20 20 72  data. */.      r
12f53 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
12f54 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70  te(pPager->fd, p
12f55 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61  Data, pPager->pa
12f56 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 74 29 3b  geSize, offset);
12f57 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 70 61  ..      /* If pa
12f58 67 65 20 31 20 77 61 73 20 6a 75 73 74 20 77 72  ge 1 was just wr
12f59 69 74 74 65 6e 2c 20 75 70 64 61 74 65 20 50 61  itten, update Pa
12f5a 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 20 74  ger.dbFileVers t
12f5b 6f 20 6d 61 74 63 68 0a 20 20 20 20 20 20 2a 2a  o match.      **
12f5c 20 74 68 65 20 76 61 6c 75 65 20 6e 6f 77 20 73   the value now s
12f5d 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64 61 74  tored in the dat
12f5e 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 77  abase file. If w
12f5f 72 69 74 69 6e 67 20 74 68 69 73 20 0a 20 20 20  riting this .   
12f60 20 20 20 2a 2a 20 70 61 67 65 20 63 61 75 73 65     ** page cause
12f61 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  d the database f
12f62 69 6c 65 20 74 6f 20 67 72 6f 77 2c 20 75 70 64  ile to grow, upd
12f63 61 74 65 20 64 62 46 69 6c 65 53 69 7a 65 2e 20  ate dbFileSize. 
12f64 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
12f65 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20  if( pgno==1 ){. 
12f66 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70         memcpy(&p
12f67 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72  Pager->dbFileVer
12f68 73 2c 20 26 70 44 61 74 61 5b 32 34 5d 2c 20 73  s, &pData[24], s
12f69 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62  izeof(pPager->db
12f6a 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20  FileVers));.    
12f6b 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 67    }.      if( pg
12f6c 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 46 69 6c  no>pPager->dbFil
12f6d 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20  eSize ){.       
12f6e 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53   pPager->dbFileS
12f6f 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20  ize = pgno;.    
12f70 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 55 70    }..      /* Up
12f71 64 61 74 65 20 61 6e 79 20 62 61 63 6b 75 70 20  date any backup 
12f72 6f 62 6a 65 63 74 73 20 63 6f 70 79 69 6e 67 20  objects copying 
12f73 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
12f74 74 68 69 73 20 70 61 67 65 72 2e 20 2a 2f 0a 20  this pager. */. 
12f75 20 20 20 20 20 73 71 6c 69 74 65 33 42 61 63 6b       sqlite3Back
12f76 75 70 55 70 64 61 74 65 28 70 50 61 67 65 72 2d  upUpdate(pPager-
12f77 3e 70 42 61 63 6b 75 70 2c 20 70 67 6e 6f 2c 20  >pBackup, pgno, 
12f78 28 75 38 2a 29 70 4c 69 73 74 2d 3e 70 44 61 74  (u8*)pList->pDat
12f79 61 29 3b 0a 0a 20 20 20 20 20 20 50 41 47 45 52  a);..      PAGER
12f7a 54 52 41 43 45 28 28 22 53 54 4f 52 45 20 25 64  TRACE(("STORE %d
12f7b 20 70 61 67 65 20 25 64 20 68 61 73 68 28 25 30   page %d hash(%0
12f7c 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20  8x)\n",.        
12f7d 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45 52             PAGER
12f7e 49 44 28 70 50 61 67 65 72 29 2c 20 70 67 6e 6f  ID(pPager), pgno
12f7f 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68  , pager_pagehash
12f80 28 70 4c 69 73 74 29 29 29 3b 0a 20 20 20 20 20  (pList)));.     
12f81 20 49 4f 54 52 41 43 45 28 28 22 50 47 4f 55 54   IOTRACE(("PGOUT
12f82 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65   %p %d\n", pPage
12f83 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20  r, pgno));.     
12f84 20 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69   PAGER_INCR(sqli
12f85 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64  te3_pager_writed
12f86 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20  b_count);.      
12f87 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65  PAGER_INCR(pPage
12f88 72 2d 3e 6e 57 72 69 74 65 29 3b 0a 20 20 20 20  r->nWrite);.    
12f89 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 50 41 47  }else{.      PAG
12f8a 45 52 54 52 41 43 45 28 28 22 4e 4f 53 54 4f 52  ERTRACE(("NOSTOR
12f8b 45 20 25 64 20 70 61 67 65 20 25 64 5c 6e 22 2c  E %d page %d\n",
12f8c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
12f8d 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a  , pgno));.    }.
12f8e 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48  #ifdef SQLITE_CH
12f8f 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 4c  ECK_PAGES.    pL
12f90 69 73 74 2d 3e 70 61 67 65 48 61 73 68 20 3d 20  ist->pageHash = 
12f91 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70  pager_pagehash(p
12f92 4c 69 73 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20  List);.#endif.  
12f93 20 20 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 2d    pList = pList-
12f94 3e 70 44 69 72 74 79 3b 0a 20 20 7d 0a 0a 20 20  >pDirty;.  }..  
12f95 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
12f96 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 72 65 63  .** Append a rec
12f97 6f 72 64 20 6f 66 20 74 68 65 20 63 75 72 72 65  ord of the curre
12f98 6e 74 20 73 74 61 74 65 20 6f 66 20 70 61 67 65  nt state of page
12f99 20 70 50 67 20 74 6f 20 74 68 65 20 73 75 62 2d   pPg to the sub-
12f9a 6a 6f 75 72 6e 61 6c 2e 20 0a 2a 2a 20 49 74 20  journal. .** It 
12f9b 69 73 20 74 68 65 20 63 61 6c 6c 65 72 73 20 72  is the callers r
12f9c 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 74 6f  esponsibility to
12f9d 20 75 73 65 20 73 75 62 6a 52 65 71 75 69 72 65   use subjRequire
12f9e 73 50 61 67 65 28 29 20 74 6f 20 63 68 65 63 6b  sPage() to check
12f9f 20 0a 2a 2a 20 74 68 61 74 20 69 74 20 69 73 20   .** that it is 
12fa0 72 65 61 6c 6c 79 20 72 65 71 75 69 72 65 64 20  really required 
12fa1 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74  before calling t
12fa2 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  his function..**
12fa3 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
12fa4 6c 2c 20 73 65 74 20 74 68 65 20 62 69 74 20 63  l, set the bit c
12fa5 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
12fa6 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 20 74 68 65  pPg->pgno in the
12fa7 20 62 69 74 76 65 63 73 0a 2a 2a 20 66 6f 72 20   bitvecs.** for 
12fa8 61 6c 6c 20 6f 70 65 6e 20 73 61 76 65 70 6f 69  all open savepoi
12fa9 6e 74 73 20 62 65 66 6f 72 65 20 72 65 74 75 72  nts before retur
12faa 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ning..**.** This
12fab 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
12fac 73 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65  s SQLITE_OK if e
12fad 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63  verything is suc
12fae 63 65 73 73 66 75 6c 2c 20 61 6e 20 49 4f 0a 2a  cessful, an IO.*
12faf 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20  * error code if 
12fb0 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 77  the attempt to w
12fb1 72 69 74 65 20 74 6f 20 74 68 65 20 73 75 62 2d  rite to the sub-
12fb2 6a 6f 75 72 6e 61 6c 20 66 61 69 6c 73 2c 20 6f  journal fails, o
12fb3 72 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d  r .** SQLITE_NOM
12fb4 45 4d 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66  EM if a malloc f
12fb5 61 69 6c 73 20 77 68 69 6c 65 20 73 65 74 74 69  ails while setti
12fb6 6e 67 20 61 20 62 69 74 20 69 6e 20 61 20 73 61  ng a bit in a sa
12fb7 76 65 70 6f 69 6e 74 0a 2a 2a 20 62 69 74 76 65  vepoint.** bitve
12fb8 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  c..*/.static int
12fb9 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28   subjournalPage(
12fba 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 69  PgHdr *pPg){.  i
12fbb 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
12fbc 4b 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  K;.  Pager *pPag
12fbd 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72  er = pPg->pPager
12fbe 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70  ;.  if( isOpen(p
12fbf 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a  Pager->sjfd) ){.
12fc0 20 20 20 20 76 6f 69 64 20 2a 70 44 61 74 61 20      void *pData 
12fc1 3d 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20  = pPg->pData;.  
12fc2 20 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 70    i64 offset = p
12fc3 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 2a 28  Pager->nSubRec*(
12fc4 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  4+pPager->pageSi
12fc5 7a 65 29 3b 0a 20 20 20 20 63 68 61 72 20 2a 70  ze);.    char *p
12fc6 44 61 74 61 32 3b 0a 0a 20 20 20 20 43 4f 44 45  Data2;..    CODE
12fc7 43 32 28 70 50 61 67 65 72 2c 20 70 44 61 74 61  C2(pPager, pData
12fc8 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 37 2c 20  , pPg->pgno, 7, 
12fc9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
12fca 4d 45 4d 2c 20 70 44 61 74 61 32 29 3b 0a 20 20  MEM, pData2);.  
12fcb 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 53    PAGERTRACE(("S
12fcc 54 4d 54 2d 4a 4f 55 52 4e 41 4c 20 25 64 20 70  TMT-JOURNAL %d p
12fcd 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52  age %d\n", PAGER
12fce 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d  ID(pPager), pPg-
12fcf 3e 70 67 6e 6f 29 29 3b 0a 20 20 0a 20 20 20 20  >pgno));.  .    
12fd0 61 73 73 65 72 74 28 20 70 61 67 65 49 6e 4a 6f  assert( pageInJo
12fd1 75 72 6e 61 6c 28 70 50 67 29 20 7c 7c 20 70 50  urnal(pPg) || pP
12fd2 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e  g->pgno>pPager->
12fd3 64 62 4f 72 69 67 53 69 7a 65 20 29 3b 0a 20 20  dbOrigSize );.  
12fd4 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69    rc = write32bi
12fd5 74 73 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c  ts(pPager->sjfd,
12fd6 20 6f 66 66 73 65 74 2c 20 70 50 67 2d 3e 70 67   offset, pPg->pg
12fd7 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  no);.    if( rc=
12fd8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
12fd9 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
12fda 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e  OsWrite(pPager->
12fdb 73 6a 66 64 2c 20 70 44 61 74 61 32 2c 20 70 50  sjfd, pData2, pP
12fdc 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20  ager->pageSize, 
12fdd 6f 66 66 73 65 74 2b 34 29 3b 0a 20 20 20 20 7d  offset+4);.    }
12fde 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
12fdf 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
12fe0 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 2b  pPager->nSubRec+
12fe1 2b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  +;.    assert( p
12fe2 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e  Pager->nSavepoin
12fe3 74 3e 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  t>0 );.    rc = 
12fe4 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69  addToSavepointBi
12fe5 74 76 65 63 73 28 70 50 61 67 65 72 2c 20 70 50  tvecs(pPager, pP
12fe6 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20  g->pgno);.  }.  
12fe7 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
12fe8 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
12fe9 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  on is called by 
12fea 74 68 65 20 70 63 61 63 68 65 20 6c 61 79 65 72  the pcache layer
12feb 20 77 68 65 6e 20 69 74 20 68 61 73 20 72 65 61   when it has rea
12fec 63 68 65 64 20 73 6f 6d 65 0a 2a 2a 20 73 6f 66  ched some.** sof
12fed 74 20 6d 65 6d 6f 72 79 20 6c 69 6d 69 74 2e 20  t memory limit. 
12fee 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
12fef 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  nt is a pointer 
12ff0 74 6f 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63  to a Pager objec
12ff1 74 0a 2a 2a 20 28 63 61 73 74 20 61 73 20 61 20  t.** (cast as a 
12ff2 76 6f 69 64 2a 29 2e 20 54 68 65 20 70 61 67 65  void*). The page
12ff3 72 20 69 73 20 61 6c 77 61 79 73 20 27 70 75 72  r is always 'pur
12ff4 67 65 61 62 6c 65 27 20 28 6e 6f 74 20 61 6e 20  geable' (not an 
12ff5 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74  in-memory.** dat
12ff6 61 62 61 73 65 29 2e 20 54 68 65 20 73 65 63 6f  abase). The seco
12ff7 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  nd argument is a
12ff8 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20   reference to a 
12ff9 70 61 67 65 20 74 68 61 74 20 69 73 20 0a 2a 2a  page that is .**
12ffa 20 63 75 72 72 65 6e 74 6c 79 20 64 69 72 74 79   currently dirty
12ffb 20 62 75 74 20 68 61 73 20 6e 6f 20 6f 75 74 73   but has no outs
12ffc 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63  tanding referenc
12ffd 65 73 2e 20 54 68 65 20 70 61 67 65 0a 2a 2a 20  es. The page.** 
12ffe 69 73 20 61 6c 77 61 79 73 20 61 73 73 6f 63 69  is always associ
12fff 61 74 65 64 20 77 69 74 68 20 74 68 65 20 50 61  ated with the Pa
13000 67 65 72 20 6f 62 6a 65 63 74 20 70 61 73 73 65  ger object passe
13001 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20 0a  d as the first .
13002 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a  ** argument..**.
13003 2a 2a 20 54 68 65 20 6a 6f 62 20 6f 66 20 74 68  ** The job of th
13004 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74  is function is t
13005 6f 20 6d 61 6b 65 20 70 50 67 20 63 6c 65 61 6e  o make pPg clean
13006 20 62 79 20 77 72 69 74 69 6e 67 20 69 74 73 20   by writing its 
13007 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 75 74 20  contents.** out 
13008 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
13009 66 69 6c 65 2c 20 69 66 20 70 6f 73 73 69 62 6c  file, if possibl
1300a 65 2e 20 54 68 69 73 20 6d 61 79 20 69 6e 76 6f  e. This may invo
1300b 6c 76 65 20 73 79 6e 63 69 6e 67 20 74 68 65 0a  lve syncing the.
1300c 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  ** journal file.
1300d 20 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65   .**.** If succe
1300e 73 73 66 75 6c 2c 20 73 71 6c 69 74 65 33 50 63  ssful, sqlite3Pc
1300f 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20  acheMakeClean() 
13010 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65  is called on the
13011 20 70 61 67 65 20 61 6e 64 0a 2a 2a 20 53 51 4c   page and.** SQL
13012 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e  ITE_OK returned.
13013 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20   If an IO error 
13014 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 72 79  occurs while try
13015 69 6e 67 20 74 6f 20 6d 61 6b 65 20 74 68 65 0a  ing to make the.
13016 2a 2a 20 70 61 67 65 20 63 6c 65 61 6e 2c 20 74  ** page clean, t
13017 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65  he IO error code
13018 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66   is returned. If
13019 20 74 68 65 20 70 61 67 65 20 63 61 6e 6e 6f 74   the page cannot
1301a 20 62 65 0a 2a 2a 20 6d 61 64 65 20 63 6c 65 61   be.** made clea
1301b 6e 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72  n for some other
1301c 20 72 65 61 73 6f 6e 2c 20 62 75 74 20 6e 6f 20   reason, but no 
1301d 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68  error occurs, th
1301e 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20  en SQLITE_OK.** 
1301f 69 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  is returned by s
13020 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65  qlite3PcacheMake
13021 43 6c 65 61 6e 28 29 20 69 73 20 6e 6f 74 20 63  Clean() is not c
13022 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  alled..*/.static
13023 20 69 6e 74 20 70 61 67 65 72 53 74 72 65 73 73   int pagerStress
13024 28 76 6f 69 64 20 2a 70 2c 20 50 67 48 64 72 20  (void *p, PgHdr 
13025 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a  *pPg){.  Pager *
13026 70 50 61 67 65 72 20 3d 20 28 50 61 67 65 72 20  pPager = (Pager 
13027 2a 29 70 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  *)p;.  int rc = 
13028 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73  SQLITE_OK;..  as
13029 73 65 72 74 28 20 70 50 67 2d 3e 70 50 61 67 65  sert( pPg->pPage
1302a 72 3d 3d 70 50 61 67 65 72 20 29 3b 0a 20 20 61  r==pPager );.  a
1302b 73 73 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67  ssert( pPg->flag
1302c 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b  s&PGHDR_DIRTY );
1302d 0a 0a 20 20 2f 2a 20 54 68 65 20 64 6f 4e 6f 74  ..  /* The doNot
1302e 53 79 6e 63 20 66 6c 61 67 20 69 73 20 73 65 74  Sync flag is set
1302f 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 50   by the sqlite3P
13030 61 67 65 72 57 72 69 74 65 28 29 20 66 75 6e 63  agerWrite() func
13031 74 69 6f 6e 20 77 68 69 6c 65 20 69 74 0a 20 20  tion while it.  
13032 2a 2a 20 69 73 20 6a 6f 75 72 6e 61 6c 6c 69 6e  ** is journallin
13033 67 20 61 20 73 65 74 20 6f 66 20 74 77 6f 20 6f  g a set of two o
13034 72 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20  r more database 
13035 70 61 67 65 73 20 74 68 61 74 20 61 72 65 20 73  pages that are s
13036 74 6f 72 65 64 0a 20 20 2a 2a 20 6f 6e 20 74 68  tored.  ** on th
13037 65 20 73 61 6d 65 20 64 69 73 6b 20 73 65 63 74  e same disk sect
13038 6f 72 2e 20 53 79 6e 63 69 6e 67 20 74 68 65 20  or. Syncing the 
13039 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 61  journal is not a
1303a 6c 6c 6f 77 65 64 20 77 68 69 6c 65 0a 20 20 2a  llowed while.  *
1303b 2a 20 74 68 69 73 20 69 73 20 68 61 70 70 65 6e  * this is happen
1303c 69 6e 67 20 61 73 20 69 74 20 69 73 20 69 6d 70  ing as it is imp
1303d 6f 72 74 61 6e 74 20 74 68 61 74 20 61 6c 6c 20  ortant that all 
1303e 6d 65 6d 62 65 72 73 20 6f 66 20 73 75 63 68 20  members of such 
1303f 61 0a 20 20 2a 2a 20 73 65 74 20 6f 66 20 70 61  a.  ** set of pa
13040 67 65 73 20 61 72 65 20 73 79 6e 63 65 64 20 74  ges are synced t
13041 6f 20 64 69 73 6b 20 74 6f 67 65 74 68 65 72 2e  o disk together.
13042 20 53 6f 2c 20 69 66 20 74 68 65 20 70 61 67 65   So, if the page
13043 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20   this function. 
13044 20 2a 2a 20 69 73 20 74 72 79 69 6e 67 20 74 6f   ** is trying to
13045 20 6d 61 6b 65 20 63 6c 65 61 6e 20 77 69 6c 6c   make clean will
13046 20 72 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e   require a journ
13047 61 6c 20 73 79 6e 63 20 61 6e 64 20 74 68 65 20  al sync and the 
13048 64 6f 4e 6f 74 53 79 6e 63 0a 20 20 2a 2a 20 66  doNotSync.  ** f
13049 6c 61 67 20 69 73 20 73 65 74 2c 20 72 65 74 75  lag is set, retu
1304a 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67  rn without doing
1304b 20 61 6e 79 74 68 69 6e 67 2e 20 54 68 65 20 70   anything. The p
1304c 63 61 63 68 65 20 6c 61 79 65 72 20 77 69 6c 6c  cache layer will
1304d 0a 20 20 2a 2a 20 6a 75 73 74 20 68 61 76 65 20  .  ** just have 
1304e 74 6f 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20  to go ahead and 
1304f 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  allocate a new p
13050 61 67 65 20 62 75 66 66 65 72 20 69 6e 73 74 65  age buffer inste
13051 61 64 20 6f 66 0a 20 20 2a 2a 20 72 65 75 73 69  ad of.  ** reusi
13052 6e 67 20 70 50 67 2e 0a 20 20 2a 2a 0a 20 20 2a  ng pPg..  **.  *
13053 2a 20 53 69 6d 69 6c 61 72 6c 79 2c 20 69 66 20  * Similarly, if 
13054 74 68 65 20 70 61 67 65 72 20 68 61 73 20 61 6c  the pager has al
13055 72 65 61 64 79 20 65 6e 74 65 72 65 64 20 74 68  ready entered th
13056 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 64  e error state, d
13057 6f 20 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 20 74  o not.  ** try t
13058 6f 20 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74  o write the cont
13059 65 6e 74 73 20 6f 66 20 70 50 67 20 74 6f 20 64  ents of pPg to d
1305a 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  isk..  */.  if( 
1305b 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65 72  NEVER(pPager->er
1305c 72 43 6f 64 65 29 0a 20 20 20 7c 7c 20 28 70 50  rCode).   || (pP
1305d 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20  ager->doNotSync 
1305e 26 26 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47  && pPg->flags&PG
1305f 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 0a 20  HDR_NEED_SYNC). 
13060 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
13061 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
13062 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75   /* Sync the jou
13063 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 72 65 71  rnal file if req
13064 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20  uired. */.  if( 
13065 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  pPg->flags&PGHDR
13066 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20  _NEED_SYNC ){.  
13067 20 20 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 6e    rc = syncJourn
13068 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  al(pPager);.    
13069 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1306a 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 66 75 6c  K && pPager->ful
1306b 6c 53 79 6e 63 20 26 26 20 0a 20 20 20 20 20 20  lSync && .      
1306c 21 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  !(pPager->journa
1306d 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  lMode==PAGER_JOU
1306e 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29  RNALMODE_MEMORY)
1306f 20 26 26 0a 20 20 20 20 20 20 21 28 73 71 6c 69   &&.      !(sqli
13070 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61  te3OsDeviceChara
13071 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67 65  cteristics(pPage
13072 72 2d 3e 66 64 29 26 53 51 4c 49 54 45 5f 49 4f  r->fd)&SQLITE_IO
13073 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29  CAP_SAFE_APPEND)
13074 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 70 50  .    ){.      pP
13075 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a  ager->nRec = 0;.
13076 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65        rc = write
13077 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65  JournalHdr(pPage
13078 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  r);.    }.  }.. 
13079 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 20   /* If the page 
1307a 6e 75 6d 62 65 72 20 6f 66 20 74 68 69 73 20 70  number of this p
1307b 61 67 65 20 69 73 20 6c 61 72 67 65 72 20 74 68  age is larger th
1307c 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  an the current s
1307d 69 7a 65 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20  ize of.  ** the 
1307e 64 61 74 61 62 61 73 65 20 69 6d 61 67 65 2c 20  database image, 
1307f 69 74 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62  it may need to b
13080 65 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  e written to the
13081 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20   sub-journal..  
13082 2a 2a 20 54 68 69 73 20 69 73 20 62 65 63 61 75  ** This is becau
13083 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 70  se the call to p
13084 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c  ager_write_pagel
13085 69 73 74 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c  ist() below will
13086 20 6e 6f 74 0a 20 20 2a 2a 20 61 63 74 75 61 6c   not.  ** actual
13087 6c 79 20 77 72 69 74 65 20 64 61 74 61 20 74 6f  ly write data to
13088 20 74 68 65 20 66 69 6c 65 20 69 6e 20 74 68 69   the file in thi
13089 73 20 63 61 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a  s case..  **.  *
1308a 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 65 20 66  * Consider the f
1308b 6f 6c 6c 6f 77 69 6e 67 20 73 65 71 75 65 6e 63  ollowing sequenc
1308c 65 20 6f 66 20 65 76 65 6e 74 73 3a 0a 20 20 2a  e of events:.  *
1308d 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49 4e 3b 0a  *.  **   BEGIN;.
1308e 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75 72 6e 61    **     <journa
1308f 6c 20 70 61 67 65 20 58 3e 0a 20 20 2a 2a 20 20  l page X>.  **  
13090 20 20 20 3c 6d 6f 64 69 66 79 20 70 61 67 65 20     <modify page 
13091 58 3e 0a 20 20 2a 2a 20 20 20 20 20 53 41 56 45  X>.  **     SAVE
13092 50 4f 49 4e 54 20 73 70 3b 0a 20 20 2a 2a 20 20  POINT sp;.  **  
13093 20 20 20 20 20 3c 73 68 72 69 6e 6b 20 64 61 74       <shrink dat
13094 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 59 20  abase file to Y 
13095 70 61 67 65 73 3e 0a 20 20 2a 2a 20 20 20 20 20  pages>.  **     
13096 20 20 70 61 67 65 72 53 74 72 65 73 73 28 70 61    pagerStress(pa
13097 67 65 20 58 29 0a 20 20 2a 2a 20 20 20 20 20 52  ge X).  **     R
13098 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70 3b 0a 20  OLLBACK TO sp;. 
13099 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 28 58 3e 59   **.  ** If (X>Y
1309a 29 2c 20 74 68 65 6e 20 77 68 65 6e 20 70 61 67  ), then when pag
1309b 65 72 53 74 72 65 73 73 20 69 73 20 63 61 6c 6c  erStress is call
1309c 65 64 20 70 61 67 65 20 58 20 77 69 6c 6c 20 6e  ed page X will n
1309d 6f 74 20 62 65 20 77 72 69 74 74 65 6e 0a 20 20  ot be written.  
1309e 2a 2a 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61  ** out to the da
1309f 74 61 62 61 73 65 20 66 69 6c 65 2c 20 62 75 74  tabase file, but
130a0 20 77 69 6c 6c 20 62 65 20 64 72 6f 70 70 65 64   will be dropped
130a1 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 2e   from the cache.
130a2 20 54 68 65 6e 2c 0a 20 20 2a 2a 20 66 6f 6c 6c   Then,.  ** foll
130a3 6f 77 69 6e 67 20 74 68 65 20 22 52 4f 4c 4c 42  owing the "ROLLB
130a4 41 43 4b 20 54 4f 20 73 70 22 20 73 74 61 74 65  ACK TO sp" state
130a5 6d 65 6e 74 2c 20 72 65 61 64 69 6e 67 20 70 61  ment, reading pa
130a6 67 65 20 58 20 77 69 6c 6c 20 72 65 61 64 0a 20  ge X will read. 
130a7 20 2a 2a 20 64 61 74 61 20 66 72 6f 6d 20 74 68   ** data from th
130a8 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
130a9 20 54 68 69 73 20 77 69 6c 6c 20 62 65 20 74 68   This will be th
130aa 65 20 63 6f 70 79 20 6f 66 20 70 61 67 65 20 58  e copy of page X
130ab 20 61 73 20 69 74 0a 20 20 2a 2a 20 77 61 73 20   as it.  ** was 
130ac 77 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63  when the transac
130ad 74 69 6f 6e 20 73 74 61 72 74 65 64 2c 20 6e 6f  tion started, no
130ae 74 20 61 73 20 69 74 20 77 61 73 20 77 68 65 6e  t as it was when
130af 20 22 53 41 56 45 50 4f 49 4e 54 20 73 70 22 0a   "SAVEPOINT sp".
130b0 20 20 2a 2a 20 77 61 73 20 65 78 65 63 75 74 65    ** was execute
130b1 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  d..  **.  ** The
130b2 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 74 6f 20   solution is to 
130b3 77 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e  write the curren
130b4 74 20 64 61 74 61 20 66 6f 72 20 70 61 67 65 20  t data for page 
130b5 58 20 69 6e 74 6f 20 74 68 65 20 0a 20 20 2a 2a  X into the .  **
130b6 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c   sub-journal fil
130b7 65 20 6e 6f 77 20 28 69 66 20 69 74 20 69 73 20  e now (if it is 
130b8 6e 6f 74 20 61 6c 72 65 61 64 79 20 74 68 65 72  not already ther
130b9 65 29 2c 20 73 6f 20 74 68 61 74 20 69 74 20 77  e), so that it w
130ba 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 73 74  ill.  ** be rest
130bb 6f 72 65 64 20 74 6f 20 69 74 73 20 63 75 72 72  ored to its curr
130bc 65 6e 74 20 76 61 6c 75 65 20 77 68 65 6e 20 74  ent value when t
130bd 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20 54 4f 20  he "ROLLBACK TO 
130be 73 70 22 20 69 73 20 0a 20 20 2a 2a 20 65 78 65  sp" is .  ** exe
130bf 63 75 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  cuted..  */.  if
130c0 28 20 4e 45 56 45 52 28 0a 20 20 20 20 20 20 72  ( NEVER(.      r
130c1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
130c2 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72  pPg->pgno>pPager
130c3 2d 3e 64 62 53 69 7a 65 20 26 26 20 73 75 62 6a  ->dbSize && subj
130c4 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67  RequiresPage(pPg
130c5 29 0a 20 20 29 20 29 7b 0a 20 20 20 20 72 63 20  ).  ) ){.    rc 
130c6 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65  = subjournalPage
130c7 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  (pPg);.  }..  /*
130c8 20 57 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65   Write the conte
130c9 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 20  nts of the page 
130ca 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61 62  out to the datab
130cb 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 69  ase file. */.  i
130cc 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
130cd 20 29 7b 0a 20 20 20 20 70 50 67 2d 3e 70 44 69   ){.    pPg->pDi
130ce 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20  rty = 0;.    rc 
130cf 3d 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61  = pager_write_pa
130d0 67 65 6c 69 73 74 28 70 50 67 29 3b 0a 20 20 7d  gelist(pPg);.  }
130d1 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 74 68 65 20  ..  /* Mark the 
130d2 70 61 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 2a  page as clean. *
130d3 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
130d4 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 50 41 47  TE_OK ){.    PAG
130d5 45 52 54 52 41 43 45 28 28 22 53 54 52 45 53 53  ERTRACE(("STRESS
130d6 20 25 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20   %d page %d\n", 
130d7 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c  PAGERID(pPager),
130d8 20 70 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20   pPg->pgno));.  
130d9 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d    sqlite3PcacheM
130da 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20  akeClean(pPg);. 
130db 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 61 67   }..  return pag
130dc 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c  er_error(pPager,
130dd 20 72 63 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20   rc);.}.../*.** 
130de 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e 69  Allocate and ini
130df 74 69 61 6c 69 7a 65 20 61 20 6e 65 77 20 50 61  tialize a new Pa
130e0 67 65 72 20 6f 62 6a 65 63 74 20 61 6e 64 20 70  ger object and p
130e1 75 74 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ut a pointer to 
130e2 69 74 0a 2a 2a 20 69 6e 20 2a 70 70 50 61 67 65  it.** in *ppPage
130e3 72 2e 20 54 68 65 20 70 61 67 65 72 20 73 68 6f  r. The pager sho
130e4 75 6c 64 20 65 76 65 6e 74 75 61 6c 6c 79 20 62  uld eventually b
130e5 65 20 66 72 65 65 64 20 62 79 20 70 61 73 73 69  e freed by passi
130e6 6e 67 20 69 74 0a 2a 2a 20 74 6f 20 73 71 6c 69  ng it.** to sqli
130e7 74 65 33 50 61 67 65 72 43 6c 6f 73 65 28 29 2e  te3PagerClose().
130e8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 46 69 6c 65  .**.** The zFile
130e9 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73  name argument is
130ea 20 74 68 65 20 70 61 74 68 20 74 6f 20 74 68 65   the path to the
130eb 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74   database file t
130ec 6f 20 6f 70 65 6e 2e 0a 2a 2a 20 49 66 20 7a 46  o open..** If zF
130ed 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 20  ilename is NULL 
130ee 74 68 65 6e 20 61 20 72 61 6e 64 6f 6d 6c 79 2d  then a randomly-
130ef 6e 61 6d 65 64 20 74 65 6d 70 6f 72 61 72 79 20  named temporary 
130f0 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 0a  file is created.
130f1 2a 2a 20 61 6e 64 20 75 73 65 64 20 61 73 20 74  ** and used as t
130f2 68 65 20 66 69 6c 65 20 74 6f 20 62 65 20 63 61  he file to be ca
130f3 63 68 65 64 2e 20 54 65 6d 70 6f 72 61 72 79 20  ched. Temporary 
130f4 66 69 6c 65 73 20 61 72 65 20 62 65 20 64 65 6c  files are be del
130f5 65 74 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69  eted.** automati
130f6 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65 79 20  cally when they 
130f7 61 72 65 20 63 6c 6f 73 65 64 2e 20 49 66 20 7a  are closed. If z
130f8 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65  Filename is ":me
130f9 6d 6f 72 79 3a 22 20 74 68 65 6e 20 0a 2a 2a 20  mory:" then .** 
130fa 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  all information 
130fb 69 73 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65  is held in cache
130fc 2e 20 49 74 20 69 73 20 6e 65 76 65 72 20 77 72  . It is never wr
130fd 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e 20 0a  itten to disk. .
130fe 2a 2a 20 54 68 69 73 20 63 61 6e 20 62 65 20 75  ** This can be u
130ff 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  sed to implement
13100 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
13101 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  tabase..**.** Th
13102 65 20 6e 45 78 74 72 61 20 70 61 72 61 6d 65 74  e nExtra paramet
13103 65 72 20 73 70 65 63 69 66 69 65 73 20 74 68 65  er specifies the
13104 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
13105 20 6f 66 20 73 70 61 63 65 20 61 6c 6c 6f 63 61   of space alloca
13106 74 65 64 0a 2a 2a 20 61 6c 6f 6e 67 20 77 69 74  ted.** along wit
13107 68 20 65 61 63 68 20 70 61 67 65 20 72 65 66 65  h each page refe
13108 72 65 6e 63 65 2e 20 54 68 69 73 20 73 70 61 63  rence. This spac
13109 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 74  e is available t
1310a 6f 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 76 69  o the user.** vi
1310b 61 20 74 68 65 20 73 71 6c 69 74 65 33 50 61 67  a the sqlite3Pag
1310c 65 72 47 65 74 45 78 74 72 61 28 29 20 41 50 49  erGetExtra() API
1310d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67  ..**.** The flag
1310e 73 20 61 72 67 75 6d 65 6e 74 20 69 73 20 75 73  s argument is us
1310f 65 64 20 74 6f 20 73 70 65 63 69 66 79 20 70 72  ed to specify pr
13110 6f 70 65 72 74 69 65 73 20 74 68 61 74 20 61 66  operties that af
13111 66 65 63 74 20 74 68 65 0a 2a 2a 20 6f 70 65 72  fect the.** oper
13112 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 61 67  ation of the pag
13113 65 72 2e 20 49 74 20 73 68 6f 75 6c 64 20 62 65  er. It should be
13114 20 70 61 73 73 65 64 20 73 6f 6d 65 20 62 69 74   passed some bit
13115 77 69 73 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e  wise combination
13116 0a 2a 2a 20 6f 66 20 74 68 65 20 50 41 47 45 52  .** of the PAGER
13117 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 61 6e  _OMIT_JOURNAL an
13118 64 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c  d PAGER_NO_READL
13119 4f 43 4b 20 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a  OCK flags..**.**
1311a 20 54 68 65 20 76 66 73 46 6c 61 67 73 20 70 61   The vfsFlags pa
1311b 72 61 6d 65 74 65 72 20 69 73 20 61 20 62 69 74  rameter is a bit
1311c 6d 61 73 6b 20 74 6f 20 70 61 73 73 20 74 6f 20  mask to pass to 
1311d 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65  the flags parame
1311e 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65 20 78 4f  ter.** of the xO
1311f 70 65 6e 28 29 20 6d 65 74 68 6f 64 20 6f 66 20  pen() method of 
13120 74 68 65 20 73 75 70 70 6c 69 65 64 20 56 46 53  the supplied VFS
13121 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20 66 69   when opening fi
13122 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74  les. .**.** If t
13123 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20  he pager object 
13124 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  is allocated and
13125 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66   the specified f
13126 69 6c 65 20 6f 70 65 6e 65 64 20 0a 2a 2a 20 73  ile opened .** s
13127 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 53 51 4c  uccessfully, SQL
13128 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
13129 65 64 20 61 6e 64 20 2a 70 70 50 61 67 65 72 20  ed and *ppPager 
1312a 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 0a  set to point to.
1312b 2a 2a 20 74 68 65 20 6e 65 77 20 70 61 67 65 72  ** the new pager
1312c 20 6f 62 6a 65 63 74 2e 20 49 66 20 61 6e 20 65   object. If an e
1312d 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 2a 70 70  rror occurs, *pp
1312e 50 61 67 65 72 20 69 73 20 73 65 74 20 74 6f 20  Pager is set to 
1312f 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 20 65 72 72 6f  NULL.** and erro
13130 72 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 2e  r code returned.
13131 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d   This function m
13132 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ay return SQLITE
13133 5f 4e 4f 4d 45 4d 0a 2a 2a 20 28 73 71 6c 69 74  _NOMEM.** (sqlit
13134 65 33 4d 61 6c 6c 6f 63 28 29 20 69 73 20 75 73  e3Malloc() is us
13135 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d  ed to allocate m
13136 65 6d 6f 72 79 29 2c 20 53 51 4c 49 54 45 5f 43  emory), SQLITE_C
13137 41 4e 54 4f 50 45 4e 20 6f 72 20 0a 2a 2a 20 76  ANTOPEN or .** v
13138 61 72 69 6f 75 73 20 53 51 4c 49 54 45 5f 49 4f  arious SQLITE_IO
13139 5f 58 58 58 20 65 72 72 6f 72 73 2e 0a 2a 2f 0a  _XXX errors..*/.
1313a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1313b 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f  nt sqlite3PagerO
1313c 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  pen(.  sqlite3_v
1313d 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20  fs *pVfs,       
1313e 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 66  /* The virtual f
1313f 69 6c 65 20 73 79 73 74 65 6d 20 74 6f 20 75 73  ile system to us
13140 65 20 2a 2f 0a 20 20 50 61 67 65 72 20 2a 2a 70  e */.  Pager **p
13141 70 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 20  pPager,         
13142 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 74  /* OUT: Return t
13143 68 65 20 50 61 67 65 72 20 73 74 72 75 63 74 75  he Pager structu
13144 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e  re here */.  con
13145 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
13146 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  me,   /* Name of
13147 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
13148 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20  le to open */.  
13149 69 6e 74 20 6e 45 78 74 72 61 2c 20 20 20 20 20  int nExtra,     
1314a 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72           /* Extr
1314b 61 20 62 79 74 65 73 20 61 70 70 65 6e 64 20 74  a bytes append t
1314c 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79  o each in-memory
1314d 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 66   page */.  int f
1314e 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
1314f 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 63 6f 6e      /* flags con
13150 74 72 6f 6c 6c 69 6e 67 20 74 68 69 73 20 66 69  trolling this fi
13151 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46  le */.  int vfsF
13152 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
13153 20 2f 2a 20 66 6c 61 67 73 20 70 61 73 73 65 64   /* flags passed
13154 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71 6c 69   through to sqli
13155 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20  te3_vfs.xOpen() 
13156 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 69  */.  void (*xRei
13157 6e 69 74 29 28 44 62 50 61 67 65 2a 29 20 2f 2a  nit)(DbPage*) /*
13158 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 72 65 69   Function to rei
13159 6e 69 74 69 61 6c 69 7a 65 20 70 61 67 65 73 20  nitialize pages 
1315a 2a 2f 0a 29 7b 0a 20 20 75 38 20 2a 70 50 74 72  */.){.  u8 *pPtr
1315b 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65  ;.  Pager *pPage
1315c 72 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20  r = 0;       /* 
1315d 50 61 67 65 72 20 6f 62 6a 65 63 74 20 74 6f 20  Pager object to 
1315e 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 72 65 74  allocate and ret
1315f 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20  urn */.  int rc 
13160 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
13161 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
13162 20 2a 2f 0a 20 20 69 6e 74 20 74 65 6d 70 46 69   */.  int tempFi
13163 6c 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  le = 0;        /
13164 2a 20 54 72 75 65 20 66 6f 72 20 74 65 6d 70 20  * True for temp 
13165 66 69 6c 65 73 20 28 69 6e 63 6c 2e 20 69 6e 2d  files (incl. in-
13166 6d 65 6d 6f 72 79 20 66 69 6c 65 73 29 20 2a 2f  memory files) */
13167 0a 20 20 69 6e 74 20 6d 65 6d 44 62 20 3d 20 30  .  int memDb = 0
13168 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
13169 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61  rue if this is a
1316a 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65  n in-memory file
1316b 20 2a 2f 0a 20 20 69 6e 74 20 72 65 61 64 4f 6e   */.  int readOn
1316c 6c 79 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  ly = 0;        /
1316d 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69  * True if this i
1316e 73 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 66 69  s a read-only fi
1316f 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6a 6f 75 72  le */.  int jour
13170 6e 61 6c 46 69 6c 65 53 69 7a 65 3b 20 20 20 20  nalFileSize;    
13171 20 2f 2a 20 42 79 74 65 73 20 74 6f 20 61 6c 6c   /* Bytes to all
13172 6f 63 61 74 65 20 66 6f 72 20 65 61 63 68 20 6a  ocate for each j
13173 6f 75 72 6e 61 6c 20 66 64 20 2a 2f 0a 20 20 63  ournal fd */.  c
13174 68 61 72 20 2a 7a 50 61 74 68 6e 61 6d 65 20 3d  har *zPathname =
13175 20 30 3b 20 20 20 20 20 2f 2a 20 46 75 6c 6c 20   0;     /* Full 
13176 70 61 74 68 20 74 6f 20 64 61 74 61 62 61 73 65  path to database
13177 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   file */.  int n
13178 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20 20 20  Pathname = 0;   
13179 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1317a 20 62 79 74 65 73 20 69 6e 20 7a 50 61 74 68 6e   bytes in zPathn
1317b 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73 65  ame */.  int use
1317c 4a 6f 75 72 6e 61 6c 20 3d 20 28 66 6c 61 67 73  Journal = (flags
1317d 20 26 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f   & PAGER_OMIT_JO
1317e 55 52 4e 41 4c 29 3d 3d 30 3b 20 2f 2a 20 46 61  URNAL)==0; /* Fa
1317f 6c 73 65 20 74 6f 20 6f 6d 69 74 20 6a 6f 75 72  lse to omit jour
13180 6e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 52  nal */.  int noR
13181 65 61 64 6c 6f 63 6b 20 3d 20 28 66 6c 61 67 73  eadlock = (flags
13182 20 26 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44   & PAGER_NO_READ
13183 4c 4f 43 4b 29 21 3d 30 3b 20 20 2f 2a 20 54 72  LOCK)!=0;  /* Tr
13184 75 65 20 74 6f 20 6f 6d 69 74 20 72 65 61 64 2d  ue to omit read-
13185 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 70 63  lock */.  int pc
13186 61 63 68 65 53 69 7a 65 20 3d 20 73 71 6c 69 74  acheSize = sqlit
13187 65 33 50 63 61 63 68 65 53 69 7a 65 28 29 3b 20  e3PcacheSize(); 
13188 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 74        /* Bytes t
13189 6f 20 61 6c 6c 6f 63 61 74 65 20 66 6f 72 20 50  o allocate for P
1318a 43 61 63 68 65 20 2a 2f 0a 20 20 75 31 36 20 73  Cache */.  u16 s
1318b 7a 50 61 67 65 44 66 6c 74 20 3d 20 53 51 4c 49  zPageDflt = SQLI
1318c 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  TE_DEFAULT_PAGE_
1318d 53 49 5a 45 3b 20 20 2f 2a 20 44 65 66 61 75 6c  SIZE;  /* Defaul
1318e 74 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a 0a  t page size */..
1318f 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
13190 68 6f 77 20 6d 75 63 68 20 73 70 61 63 65 20 69  how much space i
13191 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 65  s required for e
13192 61 63 68 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ach journal file
13193 2d 68 61 6e 64 6c 65 0a 20 20 2a 2a 20 28 74 68  -handle.  ** (th
13194 65 72 65 20 61 72 65 20 74 77 6f 20 6f 66 20 74  ere are two of t
13195 68 65 6d 2c 20 74 68 65 20 6d 61 69 6e 20 6a 6f  hem, the main jo
13196 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 20 73 75  urnal and the su
13197 62 2d 6a 6f 75 72 6e 61 6c 29 2e 20 54 68 69 73  b-journal). This
13198 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 6d 61 78  .  ** is the max
13199 69 6d 75 6d 20 73 70 61 63 65 20 72 65 71 75 69  imum space requi
1319a 72 65 64 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65  red for an in-me
1319b 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  mory journal fil
1319c 65 20 68 61 6e 64 6c 65 20 0a 20 20 2a 2a 20 61  e handle .  ** a
1319d 6e 64 20 61 20 72 65 67 75 6c 61 72 20 6a 6f 75  nd a regular jou
1319e 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e 64 6c 65  rnal file-handle
1319f 2e 20 4e 6f 74 65 20 74 68 61 74 20 61 20 22 72  . Note that a "r
131a0 65 67 75 6c 61 72 20 6a 6f 75 72 6e 61 6c 2d 68  egular journal-h
131a1 61 6e 64 6c 65 22 0a 20 20 2a 2a 20 6d 61 79 20  andle".  ** may 
131a2 62 65 20 61 20 77 72 61 70 70 65 72 20 63 61 70  be a wrapper cap
131a3 61 62 6c 65 20 6f 66 20 63 61 63 68 69 6e 67 20  able of caching 
131a4 74 68 65 20 66 69 72 73 74 20 70 6f 72 74 69 6f  the first portio
131a5 6e 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  n of the journal
131a6 0a 20 20 2a 2a 20 66 69 6c 65 20 69 6e 20 6d 65  .  ** file in me
131a7 6d 6f 72 79 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  mory to implemen
131a8 74 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69  t the atomic-wri
131a9 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  te optimization 
131aa 28 73 65 65 20 0a 20 20 2a 2a 20 73 6f 75 72 63  (see .  ** sourc
131ab 65 20 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63  e file journal.c
131ac 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  )..  */.  if( sq
131ad 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65  lite3JournalSize
131ae 28 70 56 66 73 29 3e 73 71 6c 69 74 65 33 4d 65  (pVfs)>sqlite3Me
131af 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29 20 29  mJournalSize() )
131b0 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c  {.    journalFil
131b1 65 53 69 7a 65 20 3d 20 52 4f 55 4e 44 38 28 73  eSize = ROUND8(s
131b2 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a  qlite3JournalSiz
131b3 65 28 70 56 66 73 29 29 3b 0a 20 20 7d 65 6c 73  e(pVfs));.  }els
131b4 65 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69  e{.    journalFi
131b5 6c 65 53 69 7a 65 20 3d 20 52 4f 55 4e 44 38 28  leSize = ROUND8(
131b6 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61  sqlite3MemJourna
131b7 6c 53 69 7a 65 28 29 29 3b 0a 20 20 7d 0a 0a 20  lSize());.  }.. 
131b8 20 2f 2a 20 53 65 74 20 74 68 65 20 6f 75 74 70   /* Set the outp
131b9 75 74 20 76 61 72 69 61 62 6c 65 20 74 6f 20 4e  ut variable to N
131ba 55 4c 4c 20 69 6e 20 63 61 73 65 20 61 6e 20 65  ULL in case an e
131bb 72 72 6f 72 20 6f 63 63 75 72 73 2e 20 2a 2f 0a  rror occurs. */.
131bc 20 20 2a 70 70 50 61 67 65 72 20 3d 20 30 3b 0a    *ppPager = 0;.
131bd 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 61 6e  .  /* Compute an
131be 64 20 73 74 6f 72 65 20 74 68 65 20 66 75 6c 6c  d store the full
131bf 20 70 61 74 68 6e 61 6d 65 20 69 6e 20 61 6e 20   pathname in an 
131c0 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72  allocated buffer
131c1 20 70 6f 69 6e 74 65 64 0a 20 20 2a 2a 20 74 6f   pointed.  ** to
131c2 20 62 79 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6c   by zPathname, l
131c3 65 6e 67 74 68 20 6e 50 61 74 68 6e 61 6d 65 2e  ength nPathname.
131c4 20 4f 72 2c 20 69 66 20 74 68 69 73 20 69 73 20   Or, if this is 
131c5 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  a temporary file
131c6 2c 0a 20 20 2a 2a 20 6c 65 61 76 65 20 62 6f 74  ,.  ** leave bot
131c7 68 20 6e 50 61 74 68 6e 61 6d 65 20 61 6e 64 20  h nPathname and 
131c8 7a 50 61 74 68 6e 61 6d 65 20 73 65 74 20 74 6f  zPathname set to
131c9 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a   0..  */.  if( z
131ca 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c  Filename && zFil
131cb 65 6e 61 6d 65 5b 30 5d 20 29 7b 0a 20 20 20 20  ename[0] ){.    
131cc 6e 50 61 74 68 6e 61 6d 65 20 3d 20 70 56 66 73  nPathname = pVfs
131cd 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 3b 0a  ->mxPathname+1;.
131ce 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 20 3d 20      zPathname = 
131cf 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 50  sqlite3Malloc(nP
131d0 61 74 68 6e 61 6d 65 2a 32 29 3b 0a 20 20 20 20  athname*2);.    
131d1 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 3d 3d 30  if( zPathname==0
131d2 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
131d3 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
131d4 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
131d5 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44  ITE_OMIT_MEMORYD
131d6 42 0a 20 20 20 20 69 66 28 20 73 74 72 63 6d 70  B.    if( strcmp
131d7 28 7a 46 69 6c 65 6e 61 6d 65 2c 22 3a 6d 65 6d  (zFilename,":mem
131d8 6f 72 79 3a 22 29 3d 3d 30 20 29 7b 0a 20 20 20  ory:")==0 ){.   
131d9 20 20 20 6d 65 6d 44 62 20 3d 20 31 3b 0a 20 20     memDb = 1;.  
131da 20 20 20 20 7a 50 61 74 68 6e 61 6d 65 5b 30 5d      zPathname[0]
131db 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 0a   = 0;.    }else.
131dc 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20 20  #endif.    {.   
131dd 20 20 20 7a 50 61 74 68 6e 61 6d 65 5b 30 5d 20     zPathname[0] 
131de 3d 20 30 3b 20 2f 2a 20 4d 61 6b 65 20 73 75 72  = 0; /* Make sur
131df 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 65 76  e initialized ev
131e0 65 6e 20 69 66 20 46 75 6c 6c 50 61 74 68 6e 61  en if FullPathna
131e1 6d 65 28 29 20 66 61 69 6c 73 20 2a 2f 0a 20 20  me() fails */.  
131e2 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
131e3 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70  OsFullPathname(p
131e4 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20  Vfs, zFilename, 
131e5 6e 50 61 74 68 6e 61 6d 65 2c 20 7a 50 61 74 68  nPathname, zPath
131e6 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  name);.    }..  
131e7 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 73 71    nPathname = sq
131e8 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 50  lite3Strlen30(zP
131e9 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66  athname);.    if
131ea 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
131eb 26 26 20 6e 50 61 74 68 6e 61 6d 65 2b 38 3e 70  && nPathname+8>p
131ec 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20  Vfs->mxPathname 
131ed 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73  ){.      /* This
131ee 20 62 72 61 6e 63 68 20 69 73 20 74 61 6b 65 6e   branch is taken
131ef 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61   when the journa
131f0 6c 20 70 61 74 68 20 72 65 71 75 69 72 65 64 20  l path required 
131f1 62 79 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  by.      ** the 
131f2 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 20 6f  database being o
131f3 70 65 6e 65 64 20 77 69 6c 6c 20 62 65 20 6d 6f  pened will be mo
131f4 72 65 20 74 68 61 6e 20 70 56 66 73 2d 3e 6d 78  re than pVfs->mx
131f5 50 61 74 68 6e 61 6d 65 0a 20 20 20 20 20 20 2a  Pathname.      *
131f6 2a 20 62 79 74 65 73 20 69 6e 20 6c 65 6e 67 74  * bytes in lengt
131f7 68 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68  h. This means th
131f8 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 6e 6f  e database canno
131f9 74 20 62 65 20 6f 70 65 6e 65 64 2c 0a 20 20 20  t be opened,.   
131fa 20 20 20 2a 2a 20 61 73 20 69 74 20 77 69 6c 6c     ** as it will
131fb 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c 65   not be possible
131fc 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 6a 6f 75   to open the jou
131fd 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 65 76 65  rnal file or eve
131fe 6e 0a 20 20 20 20 20 20 2a 2a 20 63 68 65 63 6b  n.      ** check
131ff 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e   for a hot-journ
13200 61 6c 20 62 65 66 6f 72 65 20 72 65 61 64 69 6e  al before readin
13201 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  g..      */.    
13202 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 41    rc = SQLITE_CA
13203 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20  NTOPEN;.    }.  
13204 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
13205 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
13206 69 74 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e  ite3_free(zPathn
13207 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ame);.      retu
13208 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d  rn rc;.    }.  }
13209 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
1320a 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 50  memory for the P
1320b 61 67 65 72 20 73 74 72 75 63 74 75 72 65 2c 20  ager structure, 
1320c 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2c 20 74  PCache object, t
1320d 68 65 0a 20 20 2a 2a 20 74 68 72 65 65 20 66 69  he.  ** three fi
1320e 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 2c 20  le descriptors, 
1320f 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
13210 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 6a  e name and the j
13211 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 6c  ournal .  ** fil
13212 65 20 6e 61 6d 65 2e 20 54 68 65 20 6c 61 79 6f  e name. The layo
13213 75 74 20 69 6e 20 6d 65 6d 6f 72 79 20 69 73 20  ut in memory is 
13214 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a  as follows:.  **
13215 0a 20 20 2a 2a 20 20 20 20 20 50 61 67 65 72 20  .  **     Pager 
13216 6f 62 6a 65 63 74 20 20 20 20 20 20 20 20 20 20  object          
13217 20 20 20 20 20 20 20 20 20 20 28 73 69 7a 65 6f            (sizeo
13218 66 28 50 61 67 65 72 29 20 62 79 74 65 73 29 0a  f(Pager) bytes).
13219 20 20 2a 2a 20 20 20 20 20 50 43 61 63 68 65 20    **     PCache 
1321a 6f 62 6a 65 63 74 20 20 20 20 20 20 20 20 20 20  object          
1321b 20 20 20 20 20 20 20 20 20 28 73 71 6c 69 74 65           (sqlite
1321c 33 50 63 61 63 68 65 53 69 7a 65 28 29 20 62 79  3PcacheSize() by
1321d 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 44 61  tes).  **     Da
1321e 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 6e 64  tabase file hand
1321f 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 28 70  le            (p
13220 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 62 79  Vfs->szOsFile by
13221 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 53 75  tes).  **     Su
13222 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68  b-journal file h
13223 61 6e 64 6c 65 20 20 20 20 20 20 20 20 20 28 6a  andle         (j
13224 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 62  ournalFileSize b
13225 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 4d  ytes).  **     M
13226 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ain journal file
13227 20 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20 28   handle        (
13228 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20  journalFileSize 
13229 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20  bytes).  **     
1322a 44 61 74 61 62 61 73 65 20 66 69 6c 65 20 6e 61  Database file na
1322b 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  me              
1322c 28 6e 50 61 74 68 6e 61 6d 65 2b 31 20 62 79 74  (nPathname+1 byt
1322d 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 4a 6f 75  es).  **     Jou
1322e 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 20  rnal file name  
1322f 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6e 50               (nP
13230 61 74 68 6e 61 6d 65 2b 38 2b 31 20 62 79 74 65  athname+8+1 byte
13231 73 29 0a 20 20 2a 2f 0a 20 20 70 50 74 72 20 3d  s).  */.  pPtr =
13232 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 4d 61   (u8 *)sqlite3Ma
13233 6c 6c 6f 63 5a 65 72 6f 28 0a 20 20 20 20 52 4f  llocZero(.    RO
13234 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 50 61  UND8(sizeof(*pPa
13235 67 65 72 29 29 20 2b 20 20 20 20 20 20 2f 2a 20  ger)) +      /* 
13236 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20  Pager structure 
13237 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38 28 70 63  */.    ROUND8(pc
13238 61 63 68 65 53 69 7a 65 29 20 2b 20 20 20 20 20  acheSize) +     
13239 20 20 20 20 20 20 2f 2a 20 50 43 61 63 68 65 20        /* PCache 
1323a 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 52 4f  object */.    RO
1323b 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a 4f 73 46  UND8(pVfs->szOsF
1323c 69 6c 65 29 20 2b 20 20 20 20 20 20 20 2f 2a 20  ile) +       /* 
1323d 54 68 65 20 6d 61 69 6e 20 64 62 20 66 69 6c 65  The main db file
1323e 20 2a 2f 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46   */.    journalF
1323f 69 6c 65 53 69 7a 65 20 2a 20 32 20 2b 20 20 20  ileSize * 2 +   
13240 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 77         /* The tw
13241 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20  o journal files 
13242 2a 2f 20 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d  */ .    nPathnam
13243 65 20 2b 20 31 20 2b 20 20 20 20 20 20 20 20 20  e + 1 +         
13244 20 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c 65 6e         /* zFilen
13245 61 6d 65 20 2a 2f 0a 20 20 20 20 6e 50 61 74 68  ame */.    nPath
13246 6e 61 6d 65 20 2b 20 38 20 2b 20 31 20 20 20 20  name + 8 + 1    
13247 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4a 6f            /* zJo
13248 75 72 6e 61 6c 20 2a 2f 0a 20 20 29 3b 0a 20 20  urnal */.  );.  
13249 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59  assert( EIGHT_BY
1324a 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 53 51 4c  TE_ALIGNMENT(SQL
1324b 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 6a  ITE_INT_TO_PTR(j
1324c 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 29  ournalFileSize))
1324d 20 29 3b 0a 20 20 69 66 28 20 21 70 50 74 72 20   );.  if( !pPtr 
1324e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
1324f 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a  ree(zPathname);.
13250 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
13251 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70  E_NOMEM;.  }.  p
13252 50 61 67 65 72 20 3d 20 20 20 20 20 20 20 20 20  Pager =         
13253 20 20 20 20 20 28 50 61 67 65 72 2a 29 28 70 50       (Pager*)(pP
13254 74 72 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70  tr);.  pPager->p
13255 50 43 61 63 68 65 20 3d 20 20 20 20 28 50 43 61  PCache =    (PCa
13256 63 68 65 2a 29 28 70 50 74 72 20 2b 3d 20 52 4f  che*)(pPtr += RO
13257 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 50 61  UND8(sizeof(*pPa
13258 67 65 72 29 29 29 3b 0a 20 20 70 50 61 67 65 72  ger)));.  pPager
13259 2d 3e 66 64 20 3d 20 20 20 28 73 71 6c 69 74 65  ->fd =   (sqlite
1325a 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d  3_file*)(pPtr +=
1325b 20 52 4f 55 4e 44 38 28 70 63 61 63 68 65 53 69   ROUND8(pcacheSi
1325c 7a 65 29 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  ze));.  pPager->
1325d 73 6a 66 64 20 3d 20 28 73 71 6c 69 74 65 33 5f  sjfd = (sqlite3_
1325e 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 52  file*)(pPtr += R
1325f 4f 55 4e 44 38 28 70 56 66 73 2d 3e 73 7a 4f 73  OUND8(pVfs->szOs
13260 46 69 6c 65 29 29 3b 0a 20 20 70 50 61 67 65 72  File));.  pPager
13261 2d 3e 6a 66 64 20 3d 20 20 28 73 71 6c 69 74 65  ->jfd =  (sqlite
13262 33 5f 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d  3_file*)(pPtr +=
13263 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65   journalFileSize
13264 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 7a 46 69  );.  pPager->zFi
13265 6c 65 6e 61 6d 65 20 3d 20 20 20 20 28 63 68 61  lename =    (cha
13266 72 2a 29 28 70 50 74 72 20 2b 3d 20 6a 6f 75 72  r*)(pPtr += jour
13267 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b 0a 20 20  nalFileSize);.  
13268 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59  assert( EIGHT_BY
13269 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 50 61  TE_ALIGNMENT(pPa
1326a 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 20  ger->jfd) );..  
1326b 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 50  /* Fill in the P
1326c 61 67 65 72 2e 7a 46 69 6c 65 6e 61 6d 65 20 61  ager.zFilename a
1326d 6e 64 20 50 61 67 65 72 2e 7a 4a 6f 75 72 6e 61  nd Pager.zJourna
1326e 6c 20 62 75 66 66 65 72 73 2c 20 69 66 20 72 65  l buffers, if re
1326f 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28  quired. */.  if(
13270 20 7a 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20   zPathname ){.  
13271 20 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e    pPager->zJourn
13272 61 6c 20 3d 20 20 20 28 63 68 61 72 2a 29 28 70  al =   (char*)(p
13273 50 74 72 20 2b 3d 20 6e 50 61 74 68 6e 61 6d 65  Ptr += nPathname
13274 20 2b 20 31 29 3b 0a 20 20 20 20 6d 65 6d 63 70   + 1);.    memcp
13275 79 28 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e  y(pPager->zFilen
13276 61 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 2c 20  ame, zPathname, 
13277 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20  nPathname);.    
13278 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d 3e 7a  memcpy(pPager->z
13279 4a 6f 75 72 6e 61 6c 2c 20 7a 50 61 74 68 6e 61  Journal, zPathna
1327a 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b 0a  me, nPathname);.
1327b 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67      memcpy(&pPag
1327c 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b 6e 50 61  er->zJournal[nPa
1327d 74 68 6e 61 6d 65 5d 2c 20 22 2d 6a 6f 75 72 6e  thname], "-journ
1327e 61 6c 22 2c 20 38 29 3b 0a 20 20 20 20 69 66 28  al", 8);.    if(
1327f 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61   pPager->zFilena
13280 6d 65 5b 30 5d 3d 3d 30 20 29 20 70 50 61 67 65  me[0]==0 ) pPage
13281 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b 30 5d 20 3d  r->zJournal[0] =
13282 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   0;.    sqlite3_
13283 66 72 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b  free(zPathname);
13284 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 70  .  }.  pPager->p
13285 56 66 73 20 3d 20 70 56 66 73 3b 0a 20 20 70 50  Vfs = pVfs;.  pP
13286 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 73 20 3d  ager->vfsFlags =
13287 20 76 66 73 46 6c 61 67 73 3b 0a 0a 20 20 2f 2a   vfsFlags;..  /*
13288 20 4f 70 65 6e 20 74 68 65 20 70 61 67 65 72 20   Open the pager 
13289 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  file..  */.  if(
1328a 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46   zFilename && zF
1328b 69 6c 65 6e 61 6d 65 5b 30 5d 20 26 26 20 21 6d  ilename[0] && !m
1328c 65 6d 44 62 20 29 7b 0a 20 20 20 20 69 6e 74 20  emDb ){.    int 
1328d 66 6f 75 74 20 3d 20 30 3b 20 20 20 20 20 20 20  fout = 0;       
1328e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1328f 56 46 53 20 66 6c 61 67 73 20 72 65 74 75 72 6e  VFS flags return
13290 65 64 20 62 79 20 78 4f 70 65 6e 28 29 20 2a 2f  ed by xOpen() */
13291 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
13292 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50  3OsOpen(pVfs, pP
13293 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c  ager->zFilename,
13294 20 70 50 61 67 65 72 2d 3e 66 64 2c 20 76 66 73   pPager->fd, vfs
13295 46 6c 61 67 73 2c 20 26 66 6f 75 74 29 3b 0a 20  Flags, &fout);. 
13296 20 20 20 72 65 61 64 4f 6e 6c 79 20 3d 20 28 66     readOnly = (f
13297 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  out&SQLITE_OPEN_
13298 52 45 41 44 4f 4e 4c 59 29 3b 0a 0a 20 20 20 20  READONLY);..    
13299 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 77  /* If the file w
1329a 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  as successfully 
1329b 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 2f  opened for read/
1329c 77 72 69 74 65 20 61 63 63 65 73 73 2c 0a 20 20  write access,.  
1329d 20 20 2a 2a 20 63 68 6f 6f 73 65 20 61 20 64 65    ** choose a de
1329e 66 61 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20  fault page size 
1329f 69 6e 20 63 61 73 65 20 77 65 20 68 61 76 65 20  in case we have 
132a0 74 6f 20 63 72 65 61 74 65 20 74 68 65 0a 20 20  to create the.  
132a1 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69    ** database fi
132a2 6c 65 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20  le. The default 
132a3 70 61 67 65 20 73 69 7a 65 20 69 73 20 74 68 65  page size is the
132a4 20 6d 61 78 69 6d 75 6d 20 6f 66 3a 0a 20 20 20   maximum of:.   
132a5 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20   **.    **    + 
132a6 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50  SQLITE_DEFAULT_P
132a7 41 47 45 5f 53 49 5a 45 2c 0a 20 20 20 20 2a 2a  AGE_SIZE,.    **
132a8 20 20 20 20 2b 20 54 68 65 20 76 61 6c 75 65 20      + The value 
132a9 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69  returned by sqli
132aa 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28  te3OsSectorSize(
132ab 29 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 68  ).    **    + Th
132ac 65 20 6c 61 72 67 65 73 74 20 70 61 67 65 20 73  e largest page s
132ad 69 7a 65 20 74 68 61 74 20 63 61 6e 20 62 65 20  ize that can be 
132ae 77 72 69 74 74 65 6e 20 61 74 6f 6d 69 63 61 6c  written atomical
132af 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ly..    */.    i
132b0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
132b1 20 26 26 20 21 72 65 61 64 4f 6e 6c 79 20 29 7b   && !readOnly ){
132b2 0a 20 20 20 20 20 20 73 65 74 53 65 63 74 6f 72  .      setSector
132b3 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20  Size(pPager);.  
132b4 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54      assert(SQLIT
132b5 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53  E_DEFAULT_PAGE_S
132b6 49 5a 45 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f  IZE<=SQLITE_MAX_
132b7 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a  DEFAULT_PAGE_SIZ
132b8 45 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 7a  E);.      if( sz
132b9 50 61 67 65 44 66 6c 74 3c 70 50 61 67 65 72 2d  PageDflt<pPager-
132ba 3e 73 65 63 74 6f 72 53 69 7a 65 20 29 7b 0a 20  >sectorSize ){. 
132bb 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65         if( pPage
132bc 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3e 53 51  r->sectorSize>SQ
132bd 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54  LITE_MAX_DEFAULT
132be 5f 50 41 47 45 5f 53 49 5a 45 20 29 7b 0a 20 20  _PAGE_SIZE ){.  
132bf 20 20 20 20 20 20 20 20 73 7a 50 61 67 65 44 66          szPageDf
132c0 6c 74 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f  lt = SQLITE_MAX_
132c1 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a  DEFAULT_PAGE_SIZ
132c2 45 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  E;.        }else
132c3 7b 0a 20 20 20 20 20 20 20 20 20 20 73 7a 50 61  {.          szPa
132c4 67 65 44 66 6c 74 20 3d 20 28 75 31 36 29 70 50  geDflt = (u16)pP
132c5 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65  ager->sectorSize
132c6 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
132c7 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54    }.#ifdef SQLIT
132c8 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f  E_ENABLE_ATOMIC_
132c9 57 52 49 54 45 0a 20 20 20 20 20 20 7b 0a 20 20  WRITE.      {.  
132ca 20 20 20 20 20 20 69 6e 74 20 69 44 63 20 3d 20        int iDc = 
132cb 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43  sqlite3OsDeviceC
132cc 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70  haracteristics(p
132cd 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20  Pager->fd);.    
132ce 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20      int ii;.    
132cf 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54      assert(SQLIT
132d0 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31  E_IOCAP_ATOMIC51
132d1 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 20 20  2==(512>>8));.  
132d2 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51 4c        assert(SQL
132d3 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
132d4 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29  64K==(65536>>8))
132d5 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
132d6 28 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41  (SQLITE_MAX_DEFA
132d7 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c 3d 36  ULT_PAGE_SIZE<=6
132d8 35 35 33 36 29 3b 0a 20 20 20 20 20 20 20 20 66  5536);.        f
132d9 6f 72 28 69 69 3d 73 7a 50 61 67 65 44 66 6c 74  or(ii=szPageDflt
132da 3b 20 69 69 3c 3d 53 51 4c 49 54 45 5f 4d 41 58  ; ii<=SQLITE_MAX
132db 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49  _DEFAULT_PAGE_SI
132dc 5a 45 3b 20 69 69 3d 69 69 2a 32 29 7b 0a 20 20  ZE; ii=ii*2){.  
132dd 20 20 20 20 20 20 20 20 69 66 28 20 69 44 63 26          if( iDc&
132de 28 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54  (SQLITE_IOCAP_AT
132df 4f 4d 49 43 7c 28 69 69 3e 3e 38 29 29 20 29 7b  OMIC|(ii>>8)) ){
132e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 7a 50  .            szP
132e1 61 67 65 44 66 6c 74 20 3d 20 69 69 3b 0a 20 20  ageDflt = ii;.  
132e2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
132e3 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64    }.      }.#end
132e4 69 66 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  if.    }.  }else
132e5 7b 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74 65  {.    /* If a te
132e6 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 69 73 20  mporary file is 
132e7 72 65 71 75 65 73 74 65 64 2c 20 69 74 20 69 73  requested, it is
132e8 20 6e 6f 74 20 6f 70 65 6e 65 64 20 69 6d 6d 65   not opened imme
132e9 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20  diately..    ** 
132ea 49 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20  In this case we 
132eb 61 63 63 65 70 74 20 74 68 65 20 64 65 66 61 75  accept the defau
132ec 6c 74 20 70 61 67 65 20 73 69 7a 65 20 61 6e 64  lt page size and
132ed 20 64 65 6c 61 79 20 61 63 74 75 61 6c 6c 79 0a   delay actually.
132ee 20 20 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 74      ** opening t
132ef 68 65 20 66 69 6c 65 20 75 6e 74 69 6c 20 74 68  he file until th
132f0 65 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20  e first call to 
132f1 4f 73 57 72 69 74 65 28 29 2e 0a 20 20 20 20 2a  OsWrite()..    *
132f2 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 62 72  *.    ** This br
132f3 61 6e 63 68 20 69 73 20 61 6c 73 6f 20 72 75 6e  anch is also run
132f4 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72   for an in-memor
132f5 79 20 64 61 74 61 62 61 73 65 2e 20 41 6e 20 69  y database. An i
132f6 6e 2d 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20  n-memory.    ** 
132f7 64 61 74 61 62 61 73 65 20 69 73 20 74 68 65 20  database is the 
132f8 73 61 6d 65 20 61 73 20 61 20 74 65 6d 70 2d 66  same as a temp-f
132f9 69 6c 65 20 74 68 61 74 20 69 73 20 6e 65 76 65  ile that is neve
132fa 72 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f  r written out to
132fb 0a 20 20 20 20 2a 2a 20 64 69 73 6b 20 61 6e 64  .    ** disk and
132fc 20 75 73 65 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f   uses an in-memo
132fd 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  ry rollback jour
132fe 6e 61 6c 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20  nal..    */ .   
132ff 20 74 65 6d 70 46 69 6c 65 20 3d 20 31 3b 0a 20   tempFile = 1;. 
13300 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65     pPager->state
13301 20 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49   = PAGER_EXCLUSI
13302 56 45 3b 0a 20 20 20 20 72 65 61 64 4f 6e 6c 79  VE;.    readOnly
13303 20 3d 20 28 76 66 73 46 6c 61 67 73 26 53 51 4c   = (vfsFlags&SQL
13304 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
13305 59 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68  Y);.  }..  /* Th
13306 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c  e following call
13307 20 74 6f 20 50 61 67 65 72 53 65 74 50 61 67 65   to PagerSetPage
13308 73 69 7a 65 28 29 20 73 65 72 76 65 73 20 74 6f  size() serves to
13309 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f   set the value o
1330a 66 20 0a 20 20 2a 2a 20 50 61 67 65 72 2e 70 61  f .  ** Pager.pa
1330b 67 65 53 69 7a 65 20 61 6e 64 20 74 6f 20 61 6c  geSize and to al
1330c 6c 6f 63 61 74 65 20 74 68 65 20 50 61 67 65 72  locate the Pager
1330d 2e 70 54 6d 70 53 70 61 63 65 20 62 75 66 66 65  .pTmpSpace buffe
1330e 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  r..  */.  if( rc
1330f 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
13310 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
13311 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 29 3b 0a 20  r->memDb==0 );. 
13312 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
13313 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28  agerSetPagesize(
13314 70 50 61 67 65 72 2c 20 26 73 7a 50 61 67 65 44  pPager, &szPageD
13315 66 6c 74 2c 20 2d 31 29 3b 0a 20 20 20 20 74 65  flt, -1);.    te
13316 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49  stcase( rc!=SQLI
13317 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 0a 20 20  TE_OK );.  }..  
13318 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  /* If an error o
13319 63 63 75 72 72 65 64 20 69 6e 20 65 69 74 68 65  ccurred in eithe
1331a 72 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 73 20  r of the blocks 
1331b 61 62 6f 76 65 2c 20 66 72 65 65 20 74 68 65 20  above, free the 
1331c 0a 20 20 2a 2a 20 50 61 67 65 72 20 73 74 72 75  .  ** Pager stru
1331d 63 74 75 72 65 20 61 6e 64 20 63 6c 6f 73 65 20  cture and close 
1331e 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20  the file..  */. 
1331f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
13320 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  OK ){.    assert
13321 28 20 21 70 50 61 67 65 72 2d 3e 70 54 6d 70 53  ( !pPager->pTmpS
13322 70 61 63 65 20 29 3b 0a 20 20 20 20 73 71 6c 69  pace );.    sqli
13323 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65  te3OsClose(pPage
13324 72 2d 3e 66 64 29 3b 0a 20 20 20 20 73 71 6c 69  r->fd);.    sqli
13325 74 65 33 5f 66 72 65 65 28 70 50 61 67 65 72 29  te3_free(pPager)
13326 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  ;.    return rc;
13327 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69  .  }..  /* Initi
13328 61 6c 69 7a 65 20 74 68 65 20 50 43 61 63 68 65  alize the PCache
13329 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 61 73   object. */.  as
1332a 73 65 72 74 28 20 6e 45 78 74 72 61 3c 31 30 30  sert( nExtra<100
1332b 30 20 29 3b 0a 20 20 6e 45 78 74 72 61 20 3d 20  0 );.  nExtra = 
1332c 52 4f 55 4e 44 38 28 6e 45 78 74 72 61 29 3b 0a  ROUND8(nExtra);.
1332d 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f    sqlite3PcacheO
1332e 70 65 6e 28 73 7a 50 61 67 65 44 66 6c 74 2c 20  pen(szPageDflt, 
1332f 6e 45 78 74 72 61 2c 20 21 6d 65 6d 44 62 2c 0a  nExtra, !memDb,.
13330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13331 20 20 20 20 21 6d 65 6d 44 62 3f 70 61 67 65 72      !memDb?pager
13332 53 74 72 65 73 73 3a 30 2c 20 28 76 6f 69 64 20  Stress:0, (void 
13333 2a 29 70 50 61 67 65 72 2c 20 70 50 61 67 65 72  *)pPager, pPager
13334 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a 20 20 50  ->pPCache);..  P
13335 41 47 45 52 54 52 41 43 45 28 28 22 4f 50 45 4e  AGERTRACE(("OPEN
13336 20 25 64 20 25 73 5c 6e 22 2c 20 46 49 4c 45 48   %d %s\n", FILEH
13337 41 4e 44 4c 45 49 44 28 70 50 61 67 65 72 2d 3e  ANDLEID(pPager->
13338 66 64 29 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69  fd), pPager->zFi
13339 6c 65 6e 61 6d 65 29 29 3b 0a 20 20 49 4f 54 52  lename));.  IOTR
1333a 41 43 45 28 28 22 4f 50 45 4e 20 25 70 20 25 73  ACE(("OPEN %p %s
1333b 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 61  \n", pPager, pPa
1333c 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 29  ger->zFilename))
1333d 0a 0a 20 20 70 50 61 67 65 72 2d 3e 75 73 65 4a  ..  pPager->useJ
1333e 6f 75 72 6e 61 6c 20 3d 20 28 75 38 29 75 73 65  ournal = (u8)use
1333f 4a 6f 75 72 6e 61 6c 3b 0a 20 20 70 50 61 67 65  Journal;.  pPage
13340 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63 6b 20 3d 20  r->noReadlock = 
13341 28 6e 6f 52 65 61 64 6c 6f 63 6b 20 26 26 20 72  (noReadlock && r
13342 65 61 64 4f 6e 6c 79 29 20 3f 31 3a 30 3b 0a 20  eadOnly) ?1:0;. 
13343 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d 74   /* pPager->stmt
13344 4f 70 65 6e 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f  Open = 0; */.  /
13345 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e  * pPager->stmtIn
13346 55 73 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a  Use = 0; */.  /*
13347 20 70 50 61 67 65 72 2d 3e 6e 52 65 66 20 3d 20   pPager->nRef = 
13348 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e  0; */.  pPager->
13349 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 28 75  dbSizeValid = (u
1334a 38 29 6d 65 6d 44 62 3b 0a 20 20 2f 2a 20 70 50  8)memDb;.  /* pP
1334b 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65 20 3d  ager->stmtSize =
1334c 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67   0; */.  /* pPag
1334d 65 72 2d 3e 73 74 6d 74 4a 53 69 7a 65 20 3d 20  er->stmtJSize = 
1334e 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65  0; */.  /* pPage
1334f 72 2d 3e 6e 50 61 67 65 20 3d 20 30 3b 20 2a 2f  r->nPage = 0; */
13350 0a 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e  .  pPager->mxPgn
13351 6f 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50  o = SQLITE_MAX_P
13352 41 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 2f 2a 20  AGE_COUNT;.  /* 
13353 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20  pPager->state = 
13354 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b 20 2a 2f  PAGER_UNLOCK; */
13355 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
13356 72 2d 3e 73 74 61 74 65 20 3d 3d 20 28 74 65 6d  r->state == (tem
13357 70 46 69 6c 65 20 3f 20 50 41 47 45 52 5f 45 58  pFile ? PAGER_EX
13358 43 4c 55 53 49 56 45 20 3a 20 50 41 47 45 52 5f  CLUSIVE : PAGER_
13359 55 4e 4c 4f 43 4b 29 20 29 3b 0a 20 20 2f 2a 20  UNLOCK) );.  /* 
1335a 70 50 61 67 65 72 2d 3e 65 72 72 4d 61 73 6b 20  pPager->errMask 
1335b 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72  = 0; */.  pPager
1335c 2d 3e 74 65 6d 70 46 69 6c 65 20 3d 20 28 75 38  ->tempFile = (u8
1335d 29 74 65 6d 70 46 69 6c 65 3b 0a 20 20 61 73 73  )tempFile;.  ass
1335e 65 72 74 28 20 74 65 6d 70 46 69 6c 65 3d 3d 50  ert( tempFile==P
1335f 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
13360 5f 4e 4f 52 4d 41 4c 20 0a 20 20 20 20 20 20 20  _NORMAL .       
13361 20 20 20 7c 7c 20 74 65 6d 70 46 69 6c 65 3d 3d     || tempFile==
13362 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
13363 45 5f 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 20  E_EXCLUSIVE );. 
13364 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 4c   assert( PAGER_L
13365 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55  OCKINGMODE_EXCLU
13366 53 49 56 45 3d 3d 31 20 29 3b 0a 20 20 70 50 61  SIVE==1 );.  pPa
13367 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f  ger->exclusiveMo
13368 64 65 20 3d 20 28 75 38 29 74 65 6d 70 46 69 6c  de = (u8)tempFil
13369 65 3b 20 0a 20 20 70 50 61 67 65 72 2d 3e 63 68  e; .  pPager->ch
1336a 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20  angeCountDone = 
1336b 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
1336c 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44  ;.  pPager->memD
1336d 62 20 3d 20 28 75 38 29 6d 65 6d 44 62 3b 0a 20  b = (u8)memDb;. 
1336e 20 70 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c   pPager->readOnl
1336f 79 20 3d 20 28 75 38 29 72 65 61 64 4f 6e 6c 79  y = (u8)readOnly
13370 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e  ;.  /* pPager->n
13371 65 65 64 53 79 6e 63 20 3d 20 30 3b 20 2a 2f 0a  eedSync = 0; */.
13372 20 20 61 73 73 65 72 74 28 20 75 73 65 4a 6f 75    assert( useJou
13373 72 6e 61 6c 20 7c 7c 20 70 50 61 67 65 72 2d 3e  rnal || pPager->
13374 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 70 50  tempFile );.  pP
13375 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 70  ager->noSync = p
13376 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b  Pager->tempFile;
13377 0a 20 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53  .  pPager->fullS
13378 79 6e 63 20 3d 20 70 50 61 67 65 72 2d 3e 6e 6f  ync = pPager->no
13379 53 79 6e 63 20 3f 30 3a 31 3b 0a 20 20 70 50 61  Sync ?0:1;.  pPa
1337a 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 20  ger->sync_flags 
1337b 3d 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f  = SQLITE_SYNC_NO
1337c 52 4d 41 4c 3b 0a 20 20 2f 2a 20 70 50 61 67 65  RMAL;.  /* pPage
1337d 72 2d 3e 70 46 69 72 73 74 20 3d 20 30 3b 20 2a  r->pFirst = 0; *
1337e 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70  /.  /* pPager->p
1337f 46 69 72 73 74 53 79 6e 63 65 64 20 3d 20 30 3b  FirstSynced = 0;
13380 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d   */.  /* pPager-
13381 3e 70 4c 61 73 74 20 3d 20 30 3b 20 2a 2f 0a 20  >pLast = 0; */. 
13382 20 70 50 61 67 65 72 2d 3e 6e 45 78 74 72 61 20   pPager->nExtra 
13383 3d 20 28 75 31 36 29 6e 45 78 74 72 61 3b 0a 20  = (u16)nExtra;. 
13384 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
13385 53 69 7a 65 4c 69 6d 69 74 20 3d 20 53 51 4c 49  SizeLimit = SQLI
13386 54 45 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e  TE_DEFAULT_JOURN
13387 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54 3b 0a 20  AL_SIZE_LIMIT;. 
13388 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28   assert( isOpen(
13389 70 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 74  pPager->fd) || t
1338a 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 73 65 74  empFile );.  set
1338b 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65  SectorSize(pPage
1338c 72 29 3b 0a 20 20 69 66 28 20 21 75 73 65 4a 6f  r);.  if( !useJo
1338d 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 70 50 61  urnal ){.    pPa
1338e 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
1338f 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c   = PAGER_JOURNAL
13390 4d 4f 44 45 5f 4f 46 46 3b 0a 20 20 7d 65 6c 73  MODE_OFF;.  }els
13391 65 20 69 66 28 20 6d 65 6d 44 62 20 29 7b 0a 20  e if( memDb ){. 
13392 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
13393 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a  alMode = PAGER_J
13394 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52  OURNALMODE_MEMOR
13395 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20 70 50 61 67  Y;.  }.  /* pPag
13396 65 72 2d 3e 78 42 75 73 79 48 61 6e 64 6c 65 72  er->xBusyHandler
13397 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50   = 0; */.  /* pP
13398 61 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c  ager->pBusyHandl
13399 65 72 41 72 67 20 3d 20 30 3b 20 2a 2f 0a 20 20  erArg = 0; */.  
1339a 70 50 61 67 65 72 2d 3e 78 52 65 69 6e 69 74 65  pPager->xReinite
1339b 72 20 3d 20 78 52 65 69 6e 69 74 3b 0a 20 20 2f  r = xReinit;.  /
1339c 2a 20 6d 65 6d 73 65 74 28 70 50 61 67 65 72 2d  * memset(pPager-
1339d 3e 61 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f  >aHash, 0, sizeo
1339e 66 28 70 50 61 67 65 72 2d 3e 61 48 61 73 68 29  f(pPager->aHash)
1339f 29 3b 20 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72  ); */.  *ppPager
133a0 20 3d 20 70 50 61 67 65 72 3b 0a 20 20 72 65 74   = pPager;.  ret
133a1 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
133a2 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  ..../*.** This f
133a3 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
133a4 64 20 61 66 74 65 72 20 74 72 61 6e 73 69 74 69  d after transiti
133a5 6f 6e 69 6e 67 20 66 72 6f 6d 20 50 41 47 45 52  oning from PAGER
133a6 5f 55 4e 4c 4f 43 4b 20 74 6f 0a 2a 2a 20 50 41  _UNLOCK to.** PA
133a7 47 45 52 5f 53 48 41 52 45 44 20 73 74 61 74 65  GER_SHARED state
133a8 2e 20 49 74 20 74 65 73 74 73 20 69 66 20 74 68  . It tests if th
133a9 65 72 65 20 69 73 20 61 20 68 6f 74 20 6a 6f 75  ere is a hot jou
133aa 72 6e 61 6c 20 70 72 65 73 65 6e 74 20 69 6e 0a  rnal present in.
133ab 2a 2a 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74  ** the file-syst
133ac 65 6d 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e  em for the given
133ad 20 70 61 67 65 72 2e 20 41 20 68 6f 74 20 6a 6f   pager. A hot jo
133ae 75 72 6e 61 6c 20 69 73 20 6f 6e 65 20 74 68 61  urnal is one tha
133af 74 20 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62  t .** needs to b
133b0 65 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 41  e played back. A
133b1 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73  ccording to this
133b2 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 68 6f 74   function, a hot
133b3 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65  -journal.** file
133b4 20 65 78 69 73 74 73 20 69 66 20 74 68 65 20 66   exists if the f
133b5 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74 65 72 69  ollowing criteri
133b6 61 20 61 72 65 20 6d 65 74 3a 0a 2a 2a 0a 2a 2a  a are met:.**.**
133b7 20 20 20 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c     * The journal
133b8 20 66 69 6c 65 20 65 78 69 73 74 73 20 69 6e 20   file exists in 
133b9 74 68 65 20 66 69 6c 65 20 73 79 73 74 65 6d 2c  the file system,
133ba 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 4e 6f 20 70   and.**   * No p
133bb 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 61 20 52  rocess holds a R
133bc 45 53 45 52 56 45 44 20 6f 72 20 67 72 65 61 74  ESERVED or great
133bd 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  er lock on the d
133be 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 61 6e  atabase file, an
133bf 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20 64 61 74  d.**   * The dat
133c0 61 62 61 73 65 20 66 69 6c 65 20 69 74 73 65 6c  abase file itsel
133c1 66 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  f is greater tha
133c2 6e 20 30 20 62 79 74 65 73 20 69 6e 20 73 69 7a  n 0 bytes in siz
133c3 65 2c 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68  e, and.**   * Th
133c4 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20  e first byte of 
133c5 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
133c6 20 65 78 69 73 74 73 20 61 6e 64 20 69 73 20 6e   exists and is n
133c7 6f 74 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 49  ot 0x00..**.** I
133c8 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69  f the current si
133c9 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ze of the databa
133ca 73 65 20 66 69 6c 65 20 69 73 20 30 20 62 75 74  se file is 0 but
133cb 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a   a journal file.
133cc 2a 2a 20 65 78 69 73 74 73 2c 20 74 68 61 74 20  ** exists, that 
133cd 69 73 20 70 72 6f 62 61 62 6c 79 20 61 6e 20 6f  is probably an o
133ce 6c 64 20 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20  ld journal left 
133cf 6f 76 65 72 20 66 72 6f 6d 20 61 20 70 72 69 6f  over from a prio
133d0 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77 69  r.** database wi
133d1 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  th the same name
133d2 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
133d3 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
133d4 69 73 0a 2a 2a 20 6a 75 73 74 20 64 65 6c 65 74  is.** just delet
133d5 65 64 20 75 73 69 6e 67 20 4f 73 44 65 6c 65 74  ed using OsDelet
133d6 65 2c 20 2a 70 45 78 69 73 74 73 20 69 73 20 73  e, *pExists is s
133d7 65 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49  et to 0 and SQLI
133d8 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72 65 74 75  TE_OK.** is retu
133d9 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rned..**.** This
133da 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f   routine does no
133db 74 20 63 68 65 63 6b 20 69 66 20 74 68 65 72 65  t check if there
133dc 20 69 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75   is a master jou
133dd 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 0a 2a 2a  rnal filename.**
133de 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   at the end of t
133df 68 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72  he file. If ther
133e0 65 20 69 73 2c 20 61 6e 64 20 74 68 61 74 20 6d  e is, and that m
133e1 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
133e2 6c 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 65  le.** does not e
133e3 78 69 73 74 2c 20 74 68 65 6e 20 74 68 65 20 6a  xist, then the j
133e4 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e  ournal file is n
133e5 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 2e 20 49  ot really hot. I
133e6 6e 20 74 68 69 73 0a 2a 2a 20 63 61 73 65 20 74  n this.** case t
133e7 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
133e8 20 72 65 74 75 72 6e 20 61 20 66 61 6c 73 65 2d   return a false-
133e9 70 6f 73 69 74 69 76 65 2e 20 54 68 65 20 70 61  positive. The pa
133ea 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 0a 2a  ger_playback().*
133eb 2a 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64  * routine will d
133ec 69 73 63 6f 76 65 72 20 74 68 61 74 20 74 68 65  iscover that the
133ed 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
133ee 20 6e 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 20   not really hot 
133ef 61 6e 64 20 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74  and .** will not
133f0 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 0a   roll it back. .
133f1 2a 2a 0a 2a 2a 20 49 66 20 61 20 68 6f 74 2d 6a  **.** If a hot-j
133f2 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 66  ournal file is f
133f3 6f 75 6e 64 20 74 6f 20 65 78 69 73 74 2c 20 2a  ound to exist, *
133f4 70 45 78 69 73 74 73 20 69 73 20 73 65 74 20 74  pExists is set t
133f5 6f 20 31 20 61 6e 64 20 0a 2a 2a 20 53 51 4c 49  o 1 and .** SQLI
133f6 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20  TE_OK returned. 
133f7 49 66 20 6e 6f 20 68 6f 74 2d 6a 6f 75 72 6e 61  If no hot-journa
133f8 6c 20 66 69 6c 65 20 69 73 20 70 72 65 73 65 6e  l file is presen
133f9 74 2c 20 2a 70 45 78 69 73 74 73 20 69 73 0a 2a  t, *pExists is.*
133fa 2a 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53  * set to 0 and S
133fb 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65  QLITE_OK returne
133fc 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f  d. If an IO erro
133fd 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74  r occurs while t
133fe 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 64 65 74 65  rying.** to dete
133ff 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72  rmine whether or
13400 20 6e 6f 74 20 61 20 68 6f 74 2d 6a 6f 75 72 6e   not a hot-journ
13401 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73 2c 20  al file exists, 
13402 74 68 65 20 49 4f 20 65 72 72 6f 72 0a 2a 2a 20  the IO error.** 
13403 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
13404 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f   and the value o
13405 66 20 2a 70 45 78 69 73 74 73 20 69 73 20 75 6e  f *pExists is un
13406 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  defined..*/.stat
13407 69 63 20 69 6e 74 20 68 61 73 48 6f 74 4a 6f 75  ic int hasHotJou
13408 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67  rnal(Pager *pPag
13409 65 72 2c 20 69 6e 74 20 2a 70 45 78 69 73 74 73  er, int *pExists
1340a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  ){.  sqlite3_vfs
1340b 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 20 3d 20   * const pVfs = 
1340c 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20  pPager->pVfs;.  
1340d 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
1340e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1340f 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
13410 20 20 69 6e 74 20 65 78 69 73 74 73 3b 20 20 20    int exists;   
13411 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13412 2f 2a 20 54 72 75 65 20 69 66 20 61 20 6a 6f 75  /* True if a jou
13413 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 70 72 65  rnal file is pre
13414 73 65 6e 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72  sent */..  asser
13415 74 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a  t( pPager!=0 );.
13416 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
13417 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a  ->useJournal );.
13418 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e    assert( isOpen
13419 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a  (pPager->fd) );.
1341a 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 65    assert( !isOpe
1341b 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29  n(pPager->jfd) )
1341c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
1341d 65 72 2d 3e 73 74 61 74 65 20 3c 3d 20 50 41 47  er->state <= PAG
1341e 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 0a 20 20  ER_SHARED );..  
1341f 2a 70 45 78 69 73 74 73 20 3d 20 30 3b 0a 20 20  *pExists = 0;.  
13420 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63  rc = sqlite3OsAc
13421 63 65 73 73 28 70 56 66 73 2c 20 70 50 61 67 65  cess(pVfs, pPage
13422 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c  r->zJournal, SQL
13423 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54  ITE_ACCESS_EXIST
13424 53 2c 20 26 65 78 69 73 74 73 29 3b 0a 20 20 69  S, &exists);.  i
13425 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
13426 20 26 26 20 65 78 69 73 74 73 20 29 7b 0a 20 20   && exists ){.  
13427 20 20 69 6e 74 20 6c 6f 63 6b 65 64 3b 20 20 20    int locked;   
13428 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
13429 20 54 72 75 65 20 69 66 20 73 6f 6d 65 20 70 72   True if some pr
1342a 6f 63 65 73 73 20 68 6f 6c 64 73 20 61 20 52 45  ocess holds a RE
1342b 53 45 52 56 45 44 20 6c 6f 63 6b 20 2a 2f 0a 0a  SERVED lock */..
1342c 20 20 20 20 2f 2a 20 52 61 63 65 20 63 6f 6e 64      /* Race cond
1342d 69 74 69 6f 6e 20 68 65 72 65 3a 20 20 41 6e 6f  ition here:  Ano
1342e 74 68 65 72 20 70 72 6f 63 65 73 73 20 6d 69 67  ther process mig
1342f 68 74 20 68 61 76 65 20 62 65 65 6e 20 68 6f 6c  ht have been hol
13430 64 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20  ding the.    ** 
13431 74 68 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63  the RESERVED loc
13432 6b 20 61 6e 64 20 68 61 76 65 20 61 20 6a 6f 75  k and have a jou
13433 72 6e 61 6c 20 6f 70 65 6e 20 61 74 20 74 68 65  rnal open at the
13434 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73   sqlite3OsAccess
13435 28 29 20 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20  () .    ** call 
13436 61 62 6f 76 65 2c 20 62 75 74 20 74 68 65 6e 20  above, but then 
13437 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e  delete the journ
13438 61 6c 20 61 6e 64 20 64 72 6f 70 20 74 68 65 20  al and drop the 
13439 6c 6f 63 6b 20 62 65 66 6f 72 65 0a 20 20 20 20  lock before.    
1343a 2a 2a 20 77 65 20 67 65 74 20 74 6f 20 74 68 65  ** we get to the
1343b 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 71 6c 69 74   following sqlit
1343c 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65  e3OsCheckReserve
1343d 64 4c 6f 63 6b 28 29 20 63 61 6c 6c 2e 20 20 49  dLock() call.  I
1343e 66 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 69 73  f that.    ** is
1343f 20 74 68 65 20 63 61 73 65 2c 20 74 68 69 73 20   the case, this 
13440 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 74 68  routine might th
13441 69 6e 6b 20 74 68 65 72 65 20 69 73 20 61 20 68  ink there is a h
13442 6f 74 20 6a 6f 75 72 6e 61 6c 20 77 68 65 6e 0a  ot journal when.
13443 20 20 20 20 2a 2a 20 69 6e 20 66 61 63 74 20 74      ** in fact t
13444 68 65 72 65 20 69 73 20 6e 6f 6e 65 2e 20 20 54  here is none.  T
13445 68 69 73 20 72 65 73 75 6c 74 73 20 69 6e 20 61  his results in a
13446 20 66 61 6c 73 65 2d 70 6f 73 69 74 69 76 65 20   false-positive 
13447 77 68 69 63 68 20 77 69 6c 6c 0a 20 20 20 20 2a  which will.    *
13448 2a 20 62 65 20 64 65 61 6c 74 20 77 69 74 68 20  * be dealt with 
13449 62 79 20 74 68 65 20 70 6c 61 79 62 61 63 6b 20  by the playback 
1344a 72 6f 75 74 69 6e 65 2e 20 20 54 69 63 6b 65 74  routine.  Ticket
1344b 20 23 33 38 38 33 2e 0a 20 20 20 20 2a 2f 0a 20   #3883..    */. 
1344c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
1344d 73 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  sCheckReservedLo
1344e 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26  ck(pPager->fd, &
1344f 6c 6f 63 6b 65 64 29 3b 0a 20 20 20 20 69 66 28  locked);.    if(
13450 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
13451 26 20 21 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20  & !locked ){.   
13452 20 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20     int nPage;.. 
13453 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68       /* Check th
13454 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61  e size of the da
13455 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20  tabase file. If 
13456 69 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 30  it consists of 0
13457 20 70 61 67 65 73 2c 0a 20 20 20 20 20 20 2a 2a   pages,.      **
13458 20 74 68 65 6e 20 64 65 6c 65 74 65 20 74 68 65   then delete the
13459 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 53   journal file. S
1345a 65 65 20 74 68 65 20 68 65 61 64 65 72 20 63 6f  ee the header co
1345b 6d 6d 65 6e 74 20 61 62 6f 76 65 20 66 6f 72 20  mment above for 
1345c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65  .      ** the re
1345d 61 73 6f 6e 69 6e 67 20 68 65 72 65 2e 20 20 44  asoning here.  D
1345e 65 6c 65 74 65 20 74 68 65 20 6f 62 73 6f 6c 65  elete the obsole
1345f 74 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  te journal file 
13460 75 6e 64 65 72 0a 20 20 20 20 20 20 2a 2a 20 61  under.      ** a
13461 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 74   RESERVED lock t
13462 6f 20 61 76 6f 69 64 20 72 61 63 65 20 63 6f 6e  o avoid race con
13463 64 69 74 69 6f 6e 73 20 61 6e 64 20 74 6f 20 61  ditions and to a
13464 76 6f 69 64 20 76 69 6f 6c 61 74 69 6e 67 0a 20  void violating. 
13465 20 20 20 20 20 2a 2a 20 5b 48 33 33 30 32 30 5d       ** [H33020]
13466 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
13467 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
13468 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67  erPagecount(pPag
13469 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 20  er, &nPage);.   
1346a 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1346b 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1346c 69 66 28 20 6e 50 61 67 65 3d 3d 30 20 29 7b 0a  if( nPage==0 ){.
1346d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1346e 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c  3BeginBenignMall
1346f 6f 63 28 29 3b 0a 20 20 20 20 20 20 20 20 20 20  oc();.          
13470 69 66 28 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63  if( sqlite3OsLoc
13471 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 52 45  k(pPager->fd, RE
13472 53 45 52 56 45 44 5f 4c 4f 43 4b 29 3d 3d 53 51  SERVED_LOCK)==SQ
13473 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
13474 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73         sqlite3Os
13475 44 65 6c 65 74 65 28 70 56 66 73 2c 20 70 50 61  Delete(pVfs, pPa
13476 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30  ger->zJournal, 0
13477 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
13478 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 70  qlite3OsUnlock(p
13479 50 61 67 65 72 2d 3e 66 64 2c 20 53 48 41 52 45  Pager->fd, SHARE
1347a 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20  D_LOCK);.       
1347b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73     }.          s
1347c 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
1347d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 20  alloc();.       
1347e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1347f 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c    /* The journal
13480 20 66 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64   file exists and
13481 20 6e 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63   no other connec
13482 74 69 6f 6e 20 68 61 73 20 61 20 72 65 73 65 72  tion has a reser
13483 76 65 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ved.          **
13484 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b   or greater lock
13485 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
13486 20 66 69 6c 65 2e 20 4e 6f 77 20 63 68 65 63 6b   file. Now check
13487 20 74 68 61 74 20 74 68 65 72 65 20 69 73 0a 20   that there is. 
13488 20 20 20 20 20 20 20 20 20 2a 2a 20 61 74 20 6c           ** at l
13489 65 61 73 74 20 6f 6e 65 20 6e 6f 6e 2d 7a 65 72  east one non-zer
1348a 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 20 73  o bytes at the s
1348b 74 61 72 74 20 6f 66 20 74 68 65 20 6a 6f 75 72  tart of the jour
1348c 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20 20  nal file..      
1348d 20 20 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20      ** If there 
1348e 69 73 2c 20 74 68 65 6e 20 77 65 20 63 6f 6e 73  is, then we cons
1348f 69 64 65 72 20 74 68 69 73 20 6a 6f 75 72 6e 61  ider this journa
13490 6c 20 74 6f 20 62 65 20 68 6f 74 2e 20 49 66 20  l to be hot. If 
13491 6e 6f 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20  not, .          
13492 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 69 67 6e  ** it can be ign
13493 6f 72 65 64 2e 0a 20 20 20 20 20 20 20 20 20 20  ored..          
13494 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  */.          int
13495 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e   f = SQLITE_OPEN
13496 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 45  _READONLY|SQLITE
13497 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e  _OPEN_MAIN_JOURN
13498 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63  AL;.          rc
13499 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e   = sqlite3OsOpen
1349a 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a  (pVfs, pPager->z
1349b 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d  Journal, pPager-
1349c 3e 6a 66 64 2c 20 66 2c 20 26 66 29 3b 0a 20 20  >jfd, f, &f);.  
1349d 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
1349e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1349f 20 20 20 20 20 20 20 20 20 75 38 20 66 69 72 73           u8 firs
134a0 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  t = 0;.         
134a1 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
134a2 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 6a 66  sRead(pPager->jf
134a3 64 2c 20 28 76 6f 69 64 20 2a 29 26 66 69 72 73  d, (void *)&firs
134a4 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20  t, 1, 0);.      
134a5 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
134a6 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54  LITE_IOERR_SHORT
134a7 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 20  _READ ){.       
134a8 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
134a9 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20  TE_OK;.         
134aa 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
134ab 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
134ac 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20  pPager->jfd);.  
134ad 20 20 20 20 20 20 20 20 20 20 2a 70 45 78 69 73            *pExis
134ae 74 73 20 3d 20 28 66 69 72 73 74 21 3d 30 29 3b  ts = (first!=0);
134af 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
134b0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
134b1 43 41 4e 54 4f 50 45 4e 20 29 7b 0a 20 20 20 20  CANTOPEN ){.    
134b2 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65          /* If we
134b3 20 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 74 68 65   cannot open the
134b4 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
134b5 6c 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 20  l file in order 
134b6 74 6f 20 73 65 65 20 69 66 0a 20 20 20 20 20 20  to see if.      
134b7 20 20 20 20 20 20 2a 2a 20 69 74 73 20 68 61 73        ** its has
134b8 20 61 20 7a 65 72 6f 20 68 65 61 64 65 72 2c 20   a zero header, 
134b9 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 64 75  that might be du
134ba 65 20 74 6f 20 61 6e 20 49 2f 4f 20 65 72 72 6f  e to an I/O erro
134bb 72 2c 20 6f 72 0a 20 20 20 20 20 20 20 20 20 20  r, or.          
134bc 20 20 2a 2a 20 69 74 20 6d 69 67 68 74 20 62 65    ** it might be
134bd 20 64 75 65 20 74 6f 20 74 68 65 20 72 61 63 65   due to the race
134be 20 63 6f 6e 64 69 74 69 6f 6e 20 64 65 73 63 72   condition descr
134bf 69 62 65 64 20 61 62 6f 76 65 20 61 6e 64 20 69  ibed above and i
134c0 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  n.            **
134c1 20 74 69 63 6b 65 74 20 23 33 38 38 33 2e 20 20   ticket #3883.  
134c2 45 69 74 68 65 72 20 77 61 79 2c 20 61 73 73 75  Either way, assu
134c3 6d 65 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72  me that the jour
134c4 6e 61 6c 20 69 73 20 68 6f 74 2e 0a 20 20 20 20  nal is hot..    
134c5 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20          ** This 
134c6 6d 69 67 68 74 20 62 65 20 61 20 66 61 6c 73 65  might be a false
134c7 20 70 6f 73 69 74 69 76 65 2e 20 20 42 75 74 20   positive.  But 
134c8 69 66 20 69 74 20 69 73 2c 20 74 68 65 6e 20 74  if it is, then t
134c9 68 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  he.            *
134ca 2a 20 61 75 74 6f 6d 61 74 69 63 20 6a 6f 75 72  * automatic jour
134cb 6e 61 6c 20 70 6c 61 79 62 61 63 6b 20 61 6e 64  nal playback and
134cc 20 72 65 63 6f 76 65 72 79 20 6d 65 63 68 61 6e   recovery mechan
134cd 69 73 6d 20 77 69 6c 6c 20 64 65 61 6c 0a 20 20  ism will deal.  
134ce 20 20 20 20 20 20 20 20 20 20 2a 2a 20 77 69 74            ** wit
134cf 68 20 69 74 20 75 6e 64 65 72 20 61 6e 20 45 58  h it under an EX
134d0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 77 68 65  CLUSIVE lock whe
134d1 72 65 20 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65  re we do not nee
134d2 64 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20  d to.           
134d3 20 2a 2a 20 77 6f 72 72 79 20 73 6f 20 6d 75 63   ** worry so muc
134d4 68 20 77 69 74 68 20 72 61 63 65 20 63 6f 6e 64  h with race cond
134d5 69 74 69 6f 6e 73 2e 0a 20 20 20 20 20 20 20 20  itions..        
134d6 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20      */.         
134d7 20 20 20 2a 70 45 78 69 73 74 73 20 3d 20 31 3b     *pExists = 1;
134d8 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
134d9 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
134da 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
134db 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
134dc 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
134dd 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  c;.}../*.** Read
134de 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 66 6f 72   the content for
134df 20 70 61 67 65 20 70 50 67 20 6f 75 74 20 6f 66   page pPg out of
134e0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
134e1 6c 65 20 61 6e 64 20 69 6e 74 6f 20 0a 2a 2a 20  le and into .** 
134e2 70 50 67 2d 3e 70 44 61 74 61 2e 20 41 20 73 68  pPg->pData. A sh
134e3 61 72 65 64 20 6c 6f 63 6b 20 6f 72 20 67 72 65  ared lock or gre
134e4 61 74 65 72 20 6d 75 73 74 20 62 65 20 68 65 6c  ater must be hel
134e5 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  d on the databas
134e6 65 0a 2a 2a 20 66 69 6c 65 20 62 65 66 6f 72 65  e.** file before
134e7 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
134e8 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20  s called..**.** 
134e9 49 66 20 70 61 67 65 20 31 20 69 73 20 72 65 61  If page 1 is rea
134ea 64 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  d, then the valu
134eb 65 20 6f 66 20 50 61 67 65 72 2e 64 62 46 69 6c  e of Pager.dbFil
134ec 65 56 65 72 73 5b 5d 20 69 73 20 73 65 74 20 74  eVers[] is set t
134ed 6f 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 72  o.** the value r
134ee 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74  ead from the dat
134ef 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  abase file..**.*
134f0 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72  * If an IO error
134f1 20 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 74 68   occurs, then th
134f2 65 20 49 4f 20 65 72 72 6f 72 20 69 73 20 72 65  e IO error is re
134f3 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61  turned to the ca
134f4 6c 6c 65 72 2e 0a 2a 2a 20 4f 74 68 65 72 77 69  ller..** Otherwi
134f5 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  se, SQLITE_OK is
134f6 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74   returned..*/.st
134f7 61 74 69 63 20 69 6e 74 20 72 65 61 64 44 62 50  atic int readDbP
134f8 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b  age(PgHdr *pPg){
134f9 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
134fa 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 20   = pPg->pPager; 
134fb 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20  /* Pager object 
134fc 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
134fd 70 61 67 65 20 70 50 67 20 2a 2f 0a 20 20 50 67  page pPg */.  Pg
134fe 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 67 2d 3e 70  no pgno = pPg->p
134ff 67 6e 6f 3b 20 20 20 20 20 20 20 2f 2a 20 50 61  gno;       /* Pa
13500 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 72 65 61  ge number to rea
13501 64 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  d */.  int rc;  
13502 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13503 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
13504 64 65 20 2a 2f 0a 20 20 69 36 34 20 69 4f 66 66  de */.  i64 iOff
13505 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  set;            
13506 20 20 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 66       /* Byte off
13507 73 65 74 20 6f 66 20 66 69 6c 65 20 74 6f 20 72  set of file to r
13508 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 0a 20 20 61  ead from */..  a
13509 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
1350a 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52  tate>=PAGER_SHAR
1350b 45 44 20 26 26 20 21 4d 45 4d 44 42 20 29 3b 0a  ED && !MEMDB );.
1350c 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e    assert( isOpen
1350d 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a  (pPager->fd) );.
1350e 0a 20 20 69 66 28 20 4e 45 56 45 52 28 21 69 73  .  if( NEVER(!is
1350f 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29  Open(pPager->fd)
13510 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ) ){.    assert(
13511 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c   pPager->tempFil
13512 65 20 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  e );.    memset(
13513 70 50 67 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70  pPg->pData, 0, p
13514 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29  Pager->pageSize)
13515 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
13516 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 4f  ITE_OK;.  }.  iO
13517 66 66 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 29  ffset = (pgno-1)
13518 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61  *(i64)pPager->pa
13519 67 65 53 69 7a 65 3b 0a 20 20 72 63 20 3d 20 73  geSize;.  rc = s
1351a 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61  qlite3OsRead(pPa
1351b 67 65 72 2d 3e 66 64 2c 20 70 50 67 2d 3e 70 44  ger->fd, pPg->pD
1351c 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  ata, pPager->pag
1351d 65 53 69 7a 65 2c 20 69 4f 66 66 73 65 74 29 3b  eSize, iOffset);
1351e 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1351f 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45  E_IOERR_SHORT_RE
13520 41 44 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  AD ){.    rc = S
13521 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
13522 69 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20  if( pgno==1 ){. 
13523 20 20 20 75 38 20 2a 64 62 46 69 6c 65 56 65 72     u8 *dbFileVer
13524 73 20 3d 20 26 28 28 75 38 2a 29 70 50 67 2d 3e  s = &((u8*)pPg->
13525 70 44 61 74 61 29 5b 32 34 5d 3b 0a 20 20 20 20  pData)[24];.    
13526 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e  memcpy(&pPager->
13527 64 62 46 69 6c 65 56 65 72 73 2c 20 64 62 46 69  dbFileVers, dbFi
13528 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 70  leVers, sizeof(p
13529 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72  Pager->dbFileVer
1352a 73 29 29 3b 0a 20 20 7d 0a 20 20 43 4f 44 45 43  s));.  }.  CODEC
1352b 31 28 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70  1(pPager, pPg->p
1352c 44 61 74 61 2c 20 70 67 6e 6f 2c 20 33 2c 20 72  Data, pgno, 3, r
1352d 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
1352e 29 3b 0a 0a 20 20 50 41 47 45 52 5f 49 4e 43 52  );..  PAGER_INCR
1352f 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 72  (sqlite3_pager_r
13530 65 61 64 64 62 5f 63 6f 75 6e 74 29 3b 0a 20 20  eaddb_count);.  
13531 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65  PAGER_INCR(pPage
13532 72 2d 3e 6e 52 65 61 64 29 3b 0a 20 20 49 4f 54  r->nRead);.  IOT
13533 52 41 43 45 28 28 22 50 47 49 4e 20 25 70 20 25  RACE(("PGIN %p %
13534 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67  d\n", pPager, pg
13535 6e 6f 29 29 3b 0a 20 20 50 41 47 45 52 54 52 41  no));.  PAGERTRA
13536 43 45 28 28 22 46 45 54 43 48 20 25 64 20 70 61  CE(("FETCH %d pa
13537 67 65 20 25 64 20 68 61 73 68 28 25 30 38 78 29  ge %d hash(%08x)
13538 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
13539 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67      PAGERID(pPag
1353a 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72  er), pgno, pager
1353b 5f 70 61 67 65 68 61 73 68 28 70 50 67 29 29 29  _pagehash(pPg)))
1353c 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  ;..  return rc;.
1353d 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
1353e 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
1353f 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 73 68 61   to obtain a sha
13540 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  red lock on the 
13541 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
13542 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20  * It is illegal 
13543 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50  to call sqlite3P
13544 61 67 65 72 41 63 71 75 69 72 65 28 29 20 75 6e  agerAcquire() un
13545 74 69 6c 20 61 66 74 65 72 20 74 68 69 73 20 66  til after this f
13546 75 6e 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 62  unction.** has b
13547 65 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79  een successfully
13548 20 63 61 6c 6c 65 64 2e 20 49 66 20 61 20 73 68   called. If a sh
13549 61 72 65 64 2d 6c 6f 63 6b 20 69 73 20 61 6c 72  ared-lock is alr
1354a 65 61 64 79 20 68 65 6c 64 20 77 68 65 6e 0a 2a  eady held when.*
1354b 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * this function 
1354c 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 73  is called, it is
1354d 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20   a no-op..**.** 
1354e 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70  The following op
1354f 65 72 61 74 69 6f 6e 73 20 61 72 65 20 61 6c 73  erations are als
13550 6f 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74  o performed by t
13551 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  his function..**
13552 0a 2a 2a 20 20 20 31 29 20 49 66 20 74 68 65 20  .**   1) If the 
13553 70 61 67 65 72 20 69 73 20 63 75 72 72 65 6e 74  pager is current
13554 6c 79 20 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f  ly in PAGER_UNLO
13555 43 4b 20 73 74 61 74 65 20 28 6e 6f 20 6c 6f 63  CK state (no loc
13556 6b 20 68 65 6c 64 0a 2a 2a 20 20 20 20 20 20 6f  k held.**      o
13557 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
13558 69 6c 65 29 2c 20 74 68 65 6e 20 61 6e 20 61 74  ile), then an at
13559 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f  tempt is made to
1355a 20 6f 62 74 61 69 6e 20 61 0a 2a 2a 20 20 20 20   obtain a.**    
1355b 20 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 6f 6e    SHARED lock on
1355c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1355d 6c 65 2e 20 49 6d 6d 65 64 69 61 74 65 6c 79 20  le. Immediately 
1355e 61 66 74 65 72 20 6f 62 74 61 69 6e 69 6e 67 0a  after obtaining.
1355f 2a 2a 20 20 20 20 20 20 74 68 65 20 53 48 41 52  **      the SHAR
13560 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 66 69 6c  ED lock, the fil
13561 65 2d 73 79 73 74 65 6d 20 69 73 20 63 68 65 63  e-system is chec
13562 6b 65 64 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f  ked for a hot-jo
13563 75 72 6e 61 6c 2c 0a 2a 2a 20 20 20 20 20 20 77  urnal,.**      w
13564 68 69 63 68 20 69 73 20 70 6c 61 79 65 64 20 62  hich is played b
13565 61 63 6b 20 69 66 20 70 72 65 73 65 6e 74 2e 20  ack if present. 
13566 46 6f 6c 6c 6f 77 69 6e 67 20 61 6e 79 20 68 6f  Following any ho
13567 74 2d 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 20 20  t-journal .**   
13568 20 20 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65     rollback, the
13569 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
1356a 20 63 61 63 68 65 20 61 72 65 20 76 61 6c 69 64   cache are valid
1356b 61 74 65 64 20 62 79 20 63 68 65 63 6b 69 6e 67  ated by checking
1356c 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 27 63 68  .**      the 'ch
1356d 61 6e 67 65 2d 63 6f 75 6e 74 65 72 27 20 66 69  ange-counter' fi
1356e 65 6c 64 20 6f 66 20 74 68 65 20 64 61 74 61 62  eld of the datab
1356f 61 73 65 20 66 69 6c 65 20 68 65 61 64 65 72 20  ase file header 
13570 61 6e 64 0a 2a 2a 20 20 20 20 20 20 64 69 73 63  and.**      disc
13571 61 72 64 65 64 20 69 66 20 74 68 65 79 20 61 72  arded if they ar
13572 65 20 66 6f 75 6e 64 20 74 6f 20 62 65 20 69 6e  e found to be in
13573 76 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32  valid..**.**   2
13574 29 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  ) If the pager i
13575 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63  s running in exc
13576 6c 75 73 69 76 65 2d 6d 6f 64 65 2c 20 61 6e 64  lusive-mode, and
13577 20 74 68 65 72 65 20 61 72 65 20 63 75 72 72 65   there are curre
13578 6e 74 6c 79 0a 2a 2a 20 20 20 20 20 20 6e 6f 20  ntly.**      no 
13579 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65  outstanding refe
1357a 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 20 70 61  rences to any pa
1357b 67 65 73 2c 20 61 6e 64 20 69 73 20 69 6e 20 74  ges, and is in t
1357c 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 0a  he error state,.
1357d 2a 2a 20 20 20 20 20 20 74 68 65 6e 20 61 6e 20  **      then an 
1357e 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20  attempt is made 
1357f 74 6f 20 63 6c 65 61 72 20 74 68 65 20 65 72 72  to clear the err
13580 6f 72 20 73 74 61 74 65 20 62 79 20 64 69 73 63  or state by disc
13581 61 72 64 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74  arding.**      t
13582 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
13583 68 65 20 70 61 67 65 20 63 61 63 68 65 20 61 6e  he page cache an
13584 64 20 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61  d rolling back a
13585 6e 79 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 0a  ny open journal.
13586 2a 2a 20 20 20 20 20 20 66 69 6c 65 2e 0a 2a 2a  **      file..**
13587 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70 65 72 61  .** If the opera
13588 74 69 6f 6e 20 64 65 73 63 72 69 62 65 64 20 62  tion described b
13589 79 20 28 32 29 20 61 62 6f 76 65 20 69 73 20 6e  y (2) above is n
1358a 6f 74 20 61 74 74 65 6d 70 74 65 64 2c 20 61 6e  ot attempted, an
1358b 64 20 69 66 20 74 68 65 0a 2a 2a 20 70 61 67 65  d if the.** page
1358c 72 20 69 73 20 69 6e 20 61 6e 20 65 72 72 6f 72  r is in an error
1358d 20 73 74 61 74 65 20 6f 74 68 65 72 20 74 68 61   state other tha
1358e 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 77 68  n SQLITE_FULL wh
1358f 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65  en this is calle
13590 64 2c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20  d,.** the error 
13591 73 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65  state error code
13592 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 74   is returned. It
13593 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74 6f   is permitted to
13594 20 72 65 61 64 20 74 68 65 0a 2a 2a 20 64 61 74   read the.** dat
13595 61 62 61 73 65 20 77 68 65 6e 20 69 6e 20 53 51  abase when in SQ
13596 4c 49 54 45 5f 46 55 4c 4c 20 65 72 72 6f 72 20  LITE_FULL error 
13597 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68  state..**.** Oth
13598 65 72 77 69 73 65 2c 20 69 66 20 65 76 65 72 79  erwise, if every
13599 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 73  thing is success
1359a 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69  ful, SQLITE_OK i
1359b 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61  s returned. If a
1359c 6e 0a 2a 2a 20 49 4f 20 65 72 72 6f 72 20 6f 63  n.** IO error oc
1359d 63 75 72 73 20 77 68 69 6c 65 20 6c 6f 63 6b 69  curs while locki
1359e 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  ng the database,
1359f 20 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 61 20   checking for a 
135a0 68 6f 74 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66  hot-journal.** f
135a1 69 6c 65 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62  ile or rolling b
135a2 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69  ack a journal fi
135a3 6c 65 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72  le, the IO error
135a4 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
135a5 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
135a6 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
135a7 50 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28  PagerSharedLock(
135a8 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
135a9 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
135aa 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20  E_OK;           
135ab 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
135ac 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45  ode */.  int isE
135ad 72 72 6f 72 52 65 73 65 74 20 3d 20 30 3b 20 20  rrorReset = 0;  
135ae 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
135af 72 75 65 20 69 66 20 72 65 63 6f 76 65 72 69 6e  rue if recoverin
135b0 67 20 66 72 6f 6d 20 65 72 72 6f 72 20 73 74 61  g from error sta
135b1 74 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73  te */..  /* This
135b2 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79   routine is only
135b3 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 62 2d 74   called from b-t
135b4 72 65 65 20 61 6e 64 20 6f 6e 6c 79 20 77 68 65  ree and only whe
135b5 6e 20 74 68 65 72 65 20 61 72 65 20 6e 6f 0a 20  n there are no. 
135b6 20 2a 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20   ** outstanding 
135b7 70 61 67 65 73 20 2a 2f 0a 20 20 61 73 73 65 72  pages */.  asser
135b8 74 28 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  t( sqlite3Pcache
135b9 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d  RefCount(pPager-
135ba 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 29 3b 0a  >pPCache)==0 );.
135bb 20 20 69 66 28 20 4e 45 56 45 52 28 4d 45 4d 44    if( NEVER(MEMD
135bc 42 20 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72  B && pPager->err
135bd 43 6f 64 65 29 20 29 7b 20 72 65 74 75 72 6e 20  Code) ){ return 
135be 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b  pPager->errCode;
135bf 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73   }..  /* If this
135c0 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e 20   database is in 
135c1 61 6e 20 65 72 72 6f 72 2d 73 74 61 74 65 2c 20  an error-state, 
135c2 6e 6f 77 20 69 73 20 61 20 63 68 61 6e 63 65 20  now is a chance 
135c3 74 6f 20 63 6c 65 61 72 0a 20 20 2a 2a 20 74 68  to clear.  ** th
135c4 65 20 65 72 72 6f 72 2e 20 44 69 73 63 61 72 64  e error. Discard
135c5 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
135c6 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65   the pager-cache
135c7 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 0a 20 20   and rollback.  
135c8 2a 2a 20 61 6e 79 20 68 6f 74 20 6a 6f 75 72 6e  ** any hot journ
135c9 61 6c 20 69 6e 20 74 68 65 20 66 69 6c 65 2d 73  al in the file-s
135ca 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66  ystem..  */.  if
135cb 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  ( pPager->errCod
135cc 65 20 29 7b 0a 20 20 20 20 69 66 28 20 69 73 4f  e ){.    if( isO
135cd 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
135ce 20 7c 7c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75   || pPager->zJou
135cf 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 69 73  rnal ){.      is
135d0 45 72 72 6f 72 52 65 73 65 74 20 3d 20 31 3b 0a  ErrorReset = 1;.
135d1 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72      }.    pPager
135d2 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 4c 49  ->errCode = SQLI
135d3 54 45 5f 4f 4b 3b 0a 20 20 20 20 70 61 67 65 72  TE_OK;.    pager
135d4 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a  _reset(pPager);.
135d5 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65    }..  if( pPage
135d6 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f  r->state==PAGER_
135d7 55 4e 4c 4f 43 4b 20 7c 7c 20 69 73 45 72 72 6f  UNLOCK || isErro
135d8 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 73 71  rReset ){.    sq
135d9 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 6f 6e 73  lite3_vfs * cons
135da 74 20 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d  t pVfs = pPager-
135db 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 69  >pVfs;.    int i
135dc 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b  sHotJournal = 0;
135dd 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45  .    assert( !ME
135de 4d 44 42 20 29 3b 0a 20 20 20 20 61 73 73 65 72  MDB );.    asser
135df 74 28 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  t( sqlite3Pcache
135e0 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d  RefCount(pPager-
135e1 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 29 3b 0a  >pPCache)==0 );.
135e2 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
135e3 6e 6f 52 65 61 64 6c 6f 63 6b 20 29 7b 0a 20 20  noReadlock ){.  
135e4 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
135e5 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a  er->readOnly );.
135e6 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74        pPager->st
135e7 61 74 65 20 3d 20 50 41 47 45 52 5f 53 48 41 52  ate = PAGER_SHAR
135e8 45 44 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ED;.    }else{. 
135e9 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
135ea 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61  wait_on_lock(pPa
135eb 67 65 72 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b  ger, SHARED_LOCK
135ec 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
135ed 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
135ee 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
135ef 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47  ager->state==PAG
135f0 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20  ER_UNLOCK );.   
135f1 20 20 20 20 20 72 65 74 75 72 6e 20 70 61 67 65       return page
135f2 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20  r_error(pPager, 
135f3 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rc);.      }.   
135f4 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
135f5 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 53 48  Pager->state>=SH
135f6 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20  ARED_LOCK );..  
135f7 20 20 2f 2a 20 49 66 20 61 20 6a 6f 75 72 6e 61    /* If a journa
135f8 6c 20 66 69 6c 65 20 65 78 69 73 74 73 2c 20 61  l file exists, a
135f9 6e 64 20 74 68 65 72 65 20 69 73 20 6e 6f 20 52  nd there is no R
135fa 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20  ESERVED lock on 
135fb 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62  the.    ** datab
135fc 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 69  ase file, then i
135fd 74 20 65 69 74 68 65 72 20 6e 65 65 64 73 20 74  t either needs t
135fe 6f 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b  o be played back
135ff 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a 20 20 20   or deleted..   
13600 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 73 45   */.    if( !isE
13601 72 72 6f 72 52 65 73 65 74 20 29 7b 0a 20 20 20  rrorReset ){.   
13602 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
13603 72 2d 3e 73 74 61 74 65 20 3c 3d 20 50 41 47 45  r->state <= PAGE
13604 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 20 20 20  R_SHARED );.    
13605 20 20 72 63 20 3d 20 68 61 73 48 6f 74 4a 6f 75    rc = hasHotJou
13606 72 6e 61 6c 28 70 50 61 67 65 72 2c 20 26 69 73  rnal(pPager, &is
13607 48 6f 74 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20  HotJournal);.   
13608 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
13609 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1360a 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20  goto failed;.   
1360b 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
1360c 66 28 20 69 73 45 72 72 6f 72 52 65 73 65 74 20  f( isErrorReset 
1360d 7c 7c 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 20  || isHotJournal 
1360e 29 7b 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 20  ){.      /* Get 
1360f 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  an EXCLUSIVE loc
13610 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
13611 65 20 66 69 6c 65 2e 20 41 74 20 74 68 69 73 20  e file. At this 
13612 70 6f 69 6e 74 20 69 74 20 69 73 0a 20 20 20 20  point it is.    
13613 20 20 2a 2a 20 69 6d 70 6f 72 74 61 6e 74 20 74    ** important t
13614 68 61 74 20 61 20 52 45 53 45 52 56 45 44 20 6c  hat a RESERVED l
13615 6f 63 6b 20 69 73 20 6e 6f 74 20 6f 62 74 61 69  ock is not obtai
13616 6e 65 64 20 6f 6e 20 74 68 65 20 77 61 79 20 74  ned on the way t
13617 6f 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 45  o the.      ** E
13618 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49  XCLUSIVE lock. I
13619 66 20 69 74 20 77 65 72 65 2c 20 61 6e 6f 74 68  f it were, anoth
1361a 65 72 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74  er process might
1361b 20 6f 70 65 6e 20 74 68 65 0a 20 20 20 20 20 20   open the.      
1361c 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
1361d 2c 20 64 65 74 65 63 74 20 74 68 65 20 52 45 53  , detect the RES
1361e 45 52 56 45 44 20 6c 6f 63 6b 2c 20 61 6e 64 20  ERVED lock, and 
1361f 63 6f 6e 63 6c 75 64 65 20 74 68 61 74 20 74 68  conclude that th
13620 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62  e.      ** datab
13621 61 73 65 20 69 73 20 73 61 66 65 20 74 6f 20 72  ase is safe to r
13622 65 61 64 20 77 68 69 6c 65 20 74 68 69 73 20 70  ead while this p
13623 72 6f 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20  rocess is still 
13624 72 6f 6c 6c 69 6e 67 20 74 68 65 20 0a 20 20 20  rolling the .   
13625 20 20 20 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61     ** hot-journa
13626 6c 20 62 61 63 6b 2e 0a 20 20 20 20 20 20 2a 2a  l back..      **
13627 20 0a 20 20 20 20 20 20 2a 2a 20 42 65 63 61 75   .      ** Becau
13628 73 65 20 74 68 65 20 69 6e 74 65 72 6d 65 64 69  se the intermedi
13629 61 74 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63  ate RESERVED loc
1362a 6b 20 69 73 20 6e 6f 74 20 72 65 71 75 65 73 74  k is not request
1362b 65 64 2c 20 61 6e 79 0a 20 20 20 20 20 20 2a 2a  ed, any.      **
1362c 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 61   other process a
1362d 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 61 63 63  ttempting to acc
1362e 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65  ess the database
1362f 20 66 69 6c 65 20 77 69 6c 6c 20 67 65 74 20 74   file will get t
13630 6f 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 69 73  o .      ** this
13631 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 6f   point in the co
13632 64 65 20 61 6e 64 20 66 61 69 6c 20 74 6f 20 6f  de and fail to o
13633 62 74 61 69 6e 20 69 74 73 20 6f 77 6e 20 45 58  btain its own EX
13634 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 0a 20 20  CLUSIVE lock .  
13635 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 64 61      ** on the da
13636 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20  tabase file..   
13637 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
13638 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 45 58  pPager->state<EX
13639 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a  CLUSIVE_LOCK ){.
1363a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1363b 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65  ite3OsLock(pPage
1363c 72 2d 3e 66 64 2c 20 45 58 43 4c 55 53 49 56 45  r->fd, EXCLUSIVE
1363d 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20  _LOCK);.        
1363e 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1363f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  K ){.          r
13640 63 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28  c = pager_error(
13641 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20  pPager, rc);.   
13642 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c         goto fail
13643 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ed;.        }.  
13644 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74        pPager->st
13645 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c  ate = PAGER_EXCL
13646 55 53 49 56 45 3b 0a 20 20 20 20 20 20 7d 0a 20  USIVE;.      }. 
13647 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74  .      /* Open t
13648 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72  he journal for r
13649 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73  ead/write access
1364a 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73  . This is becaus
1364b 65 20 69 6e 20 0a 20 20 20 20 20 20 2a 2a 20 65  e in .      ** e
1364c 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20  xclusive-access 
1364d 6d 6f 64 65 20 74 68 65 20 66 69 6c 65 20 64 65  mode the file de
1364e 73 63 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65  scriptor will be
1364f 20 6b 65 70 74 20 6f 70 65 6e 20 61 6e 64 0a 20   kept open and. 
13650 20 20 20 20 20 2a 2a 20 70 6f 73 73 69 62 6c 79       ** possibly
13651 20 75 73 65 64 20 66 6f 72 20 61 20 74 72 61 6e   used for a tran
13652 73 61 63 74 69 6f 6e 20 6c 61 74 65 72 20 6f 6e  saction later on
13653 2e 20 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d  . On some system
13654 73 2c 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  s, the.      ** 
13655 4f 73 54 72 75 6e 63 61 74 65 28 29 20 63 61 6c  OsTruncate() cal
13656 6c 20 75 73 65 64 20 69 6e 20 65 78 63 6c 75 73  l used in exclus
13657 69 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20  ive-access mode 
13658 61 6c 73 6f 20 72 65 71 75 69 72 65 73 0a 20 20  also requires.  
13659 20 20 20 20 2a 2a 20 61 20 72 65 61 64 2f 77 72      ** a read/wr
1365a 69 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e  ite file handle.
1365b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1365c 69 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67  if( !isOpen(pPag
1365d 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20  er->jfd) ){.    
1365e 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
1365f 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
13660 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c 70  3OsAccess(pVfs,p
13661 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c  Pager->zJournal,
13662 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58  SQLITE_ACCESS_EX
13663 49 53 54 53 2c 26 72 65 73 29 3b 0a 20 20 20 20  ISTS,&res);.    
13664 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
13665 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
13666 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20     if( res ){.  
13667 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 6f            int fo
13668 75 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ut = 0;.        
13669 20 20 20 20 69 6e 74 20 66 20 3d 20 53 51 4c 49      int f = SQLI
1366a 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
1366b 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  E|SQLITE_OPEN_MA
1366c 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20  IN_JOURNAL;.    
1366d 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1366e 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c  !pPager->tempFil
1366f 65 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  e );.           
13670 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f   rc = sqlite3OsO
13671 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72  pen(pVfs, pPager
13672 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67  ->zJournal, pPag
13673 65 72 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 6f 75  er->jfd, f, &fou
13674 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  t);.            
13675 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
13676 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28  TE_OK || isOpen(
13677 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a  pPager->jfd) );.
13678 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
13679 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1367a 20 66 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50 45   fout&SQLITE_OPE
1367b 4e 5f 52 45 41 44 4f 4e 4c 59 20 29 7b 0a 20 20  N_READONLY ){.  
1367c 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
1367d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e   SQLITE_CANTOPEN
1367e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
1367f 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70  sqlite3OsClose(p
13680 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20  Pager->jfd);.   
13681 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
13682 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
13683 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
13684 65 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20 6e  e journal does n
13685 6f 74 20 65 78 69 73 74 2c 20 69 74 20 75 73 75  ot exist, it usu
13686 61 6c 6c 79 20 6d 65 61 6e 73 20 74 68 61 74 20  ally means that 
13687 73 6f 6d 65 20 0a 20 20 20 20 20 20 20 20 20 20  some .          
13688 20 20 2a 2a 20 6f 74 68 65 72 20 63 6f 6e 6e 65    ** other conne
13689 63 74 69 6f 6e 20 6d 61 6e 61 67 65 64 20 74 6f  ction managed to
1368a 20 67 65 74 20 69 6e 20 61 6e 64 20 72 6f 6c 6c   get in and roll
1368b 20 69 74 20 62 61 63 6b 20 62 65 66 6f 72 65 20   it back before 
1368c 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
1368d 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  this connection 
1368e 6f 62 74 61 69 6e 65 64 20 74 68 65 20 65 78 63  obtained the exc
1368f 6c 75 73 69 76 65 20 6c 6f 63 6b 20 61 62 6f 76  lusive lock abov
13690 65 2e 20 4f 72 2c 20 69 74 20 0a 20 20 20 20 20  e. Or, it .     
13691 20 20 20 20 20 20 20 2a 2a 20 6d 61 79 20 6d 65         ** may me
13692 61 6e 20 74 68 61 74 20 74 68 65 20 70 61 67 65  an that the page
13693 72 20 77 61 73 20 69 6e 20 74 68 65 20 65 72 72  r was in the err
13694 6f 72 2d 73 74 61 74 65 20 77 68 65 6e 20 74 68  or-state when th
13695 69 73 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  is.            *
13696 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63  * function was c
13697 61 6c 6c 65 64 20 61 6e 64 20 74 68 65 20 6a 6f  alled and the jo
13698 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 20  urnal file does 
13699 6e 6f 74 20 65 78 69 73 74 2e 20 20 2a 2f 0a 20  not exist.  */. 
1369a 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
1369b 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61  pager_end_transa
1369c 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 30 29  ction(pPager, 0)
1369d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1369e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1369f 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
136a0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
136a1 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a     goto failed;.
136a2 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
136a3 2a 20 54 4f 44 4f 3a 20 57 68 79 20 61 72 65 20  * TODO: Why are 
136a4 74 68 65 73 65 20 63 6c 65 61 72 65 64 20 68 65  these cleared he
136a5 72 65 3f 20 49 73 20 69 74 20 6e 65 63 65 73 73  re? Is it necess
136a6 61 72 79 3f 20 2a 2f 0a 20 20 20 20 20 20 70 50  ary? */.      pP
136a7 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61  ager->journalSta
136a8 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20  rted = 0;.      
136a9 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
136aa 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50  ff = 0;.      pP
136ab 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20  ager->setMaster 
136ac 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65  = 0;.      pPage
136ad 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20  r->journalHdr = 
136ae 30 3b 0a 20 0a 20 20 20 20 20 20 2f 2a 20 50 6c  0;. .      /* Pl
136af 61 79 62 61 63 6b 20 61 6e 64 20 64 65 6c 65 74  ayback and delet
136b0 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20  e the journal.  
136b1 44 72 6f 70 20 74 68 65 20 64 61 74 61 62 61 73  Drop the databas
136b2 65 20 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a  e write.      **
136b3 20 6c 6f 63 6b 20 61 6e 64 20 72 65 61 63 71 75   lock and reacqu
136b4 69 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63  ire the read loc
136b5 6b 2e 20 50 75 72 67 65 20 74 68 65 20 63 61 63  k. Purge the cac
136b6 68 65 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20  he before.      
136b7 2a 2a 20 70 6c 61 79 69 6e 67 20 62 61 63 6b 20  ** playing back 
136b8 74 68 65 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  the hot-journal 
136b9 73 6f 20 74 68 61 74 20 77 65 20 64 6f 6e 27 74  so that we don't
136ba 20 65 6e 64 20 75 70 20 77 69 74 68 0a 20 20 20   end up with.   
136bb 20 20 20 2a 2a 20 61 6e 20 69 6e 63 6f 6e 73 69     ** an inconsi
136bc 73 74 65 6e 74 20 63 61 63 68 65 2e 0a 20 20 20  stent cache..   
136bd 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
136be 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
136bf 66 64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72  fd) ){.        r
136c0 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61  c = pager_playba
136c1 63 6b 28 70 50 61 67 65 72 2c 20 31 29 3b 0a 20  ck(pPager, 1);. 
136c2 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
136c3 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
136c4 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
136c5 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72  _error(pPager, r
136c6 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  c);.          go
136c7 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20  to failed;.     
136c8 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
136c9 20 20 20 61 73 73 65 72 74 28 20 28 70 50 61 67     assert( (pPag
136ca 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52  er->state==PAGER
136cb 5f 53 48 41 52 45 44 29 0a 20 20 20 20 20 20 20  _SHARED).       
136cc 20 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e      || (pPager->
136cd 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 26 26  exclusiveMode &&
136ce 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 50   pPager->state>P
136cf 41 47 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20  AGER_SHARED).   
136d0 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20     );.    }..   
136d1 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 42 61   if( pPager->pBa
136d2 63 6b 75 70 20 7c 7c 20 73 71 6c 69 74 65 33 50  ckup || sqlite3P
136d3 63 61 63 68 65 50 61 67 65 63 6f 75 6e 74 28 70  cachePagecount(p
136d4 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3e  Pager->pPCache)>
136d5 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  0 ){.      /* Th
136d6 65 20 73 68 61 72 65 64 2d 6c 6f 63 6b 20 68 61  e shared-lock ha
136d7 73 20 6a 75 73 74 20 62 65 65 6e 20 61 63 71 75  s just been acqu
136d8 69 72 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61  ired on the data
136d9 62 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20 20  base file.      
136da 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 61 72 65  ** and there are
136db 20 61 6c 72 65 61 64 79 20 70 61 67 65 73 20 69   already pages i
136dc 6e 20 74 68 65 20 63 61 63 68 65 20 28 66 72 6f  n the cache (fro
136dd 6d 20 61 20 70 72 65 76 69 6f 75 73 0a 20 20 20  m a previous.   
136de 20 20 20 2a 2a 20 72 65 61 64 20 6f 72 20 77 72     ** read or wr
136df 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 29  ite transaction)
136e0 2e 20 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  .  Check to see 
136e1 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  if the database.
136e2 20 20 20 20 20 20 2a 2a 20 68 61 73 20 62 65 65        ** has bee
136e3 6e 20 6d 6f 64 69 66 69 65 64 2e 20 20 49 66 20  n modified.  If 
136e4 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73  the database has
136e5 20 63 68 61 6e 67 65 64 2c 20 66 6c 75 73 68 20   changed, flush 
136e6 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 63 61 63  the.      ** cac
136e7 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  he..      **.   
136e8 20 20 20 2a 2a 20 44 61 74 61 62 61 73 65 20 63     ** Database c
136e9 68 61 6e 67 65 73 20 69 73 20 64 65 74 65 63 74  hanges is detect
136ea 65 64 20 62 79 20 6c 6f 6f 6b 69 6e 67 20 61 74  ed by looking at
136eb 20 31 35 20 62 79 74 65 73 20 62 65 67 69 6e 6e   15 bytes beginn
136ec 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 61 74 20  ing.      ** at 
136ed 6f 66 66 73 65 74 20 32 34 20 69 6e 74 6f 20 74  offset 24 into t
136ee 68 65 20 66 69 6c 65 2e 20 20 54 68 65 20 66 69  he file.  The fi
136ef 72 73 74 20 34 20 6f 66 20 74 68 65 73 65 20 31  rst 4 of these 1
136f0 36 20 62 79 74 65 73 20 61 72 65 0a 20 20 20 20  6 bytes are.    
136f1 20 20 2a 2a 20 61 20 33 32 2d 62 69 74 20 63 6f    ** a 32-bit co
136f2 75 6e 74 65 72 20 74 68 61 74 20 69 73 20 69 6e  unter that is in
136f3 63 72 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65  cremented with e
136f4 61 63 68 20 63 68 61 6e 67 65 2e 20 20 54 68 65  ach change.  The
136f5 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20  .      ** other 
136f6 62 79 74 65 73 20 63 68 61 6e 67 65 20 72 61 6e  bytes change ran
136f7 64 6f 6d 6c 79 20 77 69 74 68 20 65 61 63 68 20  domly with each 
136f8 66 69 6c 65 20 63 68 61 6e 67 65 20 77 68 65 6e  file change when
136f9 0a 20 20 20 20 20 20 2a 2a 20 61 20 63 6f 64 65  .      ** a code
136fa 63 20 69 73 20 69 6e 20 75 73 65 2e 0a 20 20 20  c is in use..   
136fb 20 20 20 2a 2a 20 0a 20 20 20 20 20 20 2a 2a 20     ** .      ** 
136fc 54 68 65 72 65 20 69 73 20 61 20 76 61 6e 69 73  There is a vanis
136fd 68 69 6e 67 6c 79 20 73 6d 61 6c 6c 20 63 68 61  hingly small cha
136fe 6e 63 65 20 74 68 61 74 20 61 20 63 68 61 6e 67  nce that a chang
136ff 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 0a 20  e will not be . 
13700 20 20 20 20 20 2a 2a 20 64 65 74 65 63 74 65 64       ** detected
13701 2e 20 20 54 68 65 20 63 68 61 6e 63 65 20 6f 66  .  The chance of
13702 20 61 6e 20 75 6e 64 65 74 65 63 74 65 64 20 63   an undetected c
13703 68 61 6e 67 65 20 69 73 20 73 6f 20 73 6d 61 6c  hange is so smal
13704 6c 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a 20  l that.      ** 
13705 69 74 20 63 61 6e 20 62 65 20 6e 65 67 6c 65 63  it can be neglec
13706 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ted..      */.  
13707 20 20 20 20 63 68 61 72 20 64 62 46 69 6c 65 56      char dbFileV
13708 65 72 73 5b 73 69 7a 65 6f 66 28 70 50 61 67 65  ers[sizeof(pPage
13709 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29 5d 3b  r->dbFileVers)];
1370a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61  .      sqlite3Pa
1370b 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61  gerPagecount(pPa
1370c 67 65 72 2c 20 30 29 3b 0a 0a 20 20 20 20 20 20  ger, 0);..      
1370d 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43  if( pPager->errC
1370e 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ode ){.        r
1370f 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43  c = pPager->errC
13710 6f 64 65 3b 0a 20 20 20 20 20 20 20 20 67 6f 74  ode;.        got
13711 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20  o failed;.      
13712 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  }..      assert(
13713 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56   pPager->dbSizeV
13714 61 6c 69 64 20 29 3b 0a 20 20 20 20 20 20 69 66  alid );.      if
13715 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  ( pPager->dbSize
13716 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 49 4f  >0 ){.        IO
13717 54 52 41 43 45 28 28 22 43 4b 56 45 52 53 20 25  TRACE(("CKVERS %
13718 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c  p %d\n", pPager,
13719 20 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65   sizeof(dbFileVe
1371a 72 73 29 29 29 3b 0a 20 20 20 20 20 20 20 20 72  rs)));.        r
1371b 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
1371c 64 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 64  d(pPager->fd, &d
1371d 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f  bFileVers, sizeo
1371e 66 28 64 62 46 69 6c 65 56 65 72 73 29 2c 20 32  f(dbFileVers), 2
1371f 34 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  4);.        if( 
13720 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
13721 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
13722 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20  failed;.        
13723 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
13724 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 64 62         memset(db
13725 46 69 6c 65 56 65 72 73 2c 20 30 2c 20 73 69 7a  FileVers, 0, siz
13726 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 29  eof(dbFileVers))
13727 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
13728 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 50 61 67   if( memcmp(pPag
13729 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20  er->dbFileVers, 
1372a 64 62 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65  dbFileVers, size
1372b 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29 29 21  of(dbFileVers))!
1372c 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 61  =0 ){.        pa
1372d 67 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72  ger_reset(pPager
1372e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
1372f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
13730 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f  ger->exclusiveMo
13731 64 65 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74  de || pPager->st
13732 61 74 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45  ate==PAGER_SHARE
13733 44 20 29 3b 0a 20 20 7d 0a 0a 20 66 61 69 6c 65  D );.  }.. faile
13734 64 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  d:.  if( rc!=SQL
13735 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a  ITE_OK ){.    /*
13736 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20   pager_unlock() 
13737 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 65  is a no-op for e
13738 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 20 61 6e  xclusive mode an
13739 64 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  d in-memory data
1373a 62 61 73 65 73 2e 20 2a 2f 0a 20 20 20 20 70 61  bases. */.    pa
1373b 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65  ger_unlock(pPage
1373c 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  r);.  }.  return
1373d 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66   rc;.}../*.** If
1373e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63   the reference c
1373f 6f 75 6e 74 20 68 61 73 20 72 65 61 63 68 65 64  ount has reached
13740 20 7a 65 72 6f 2c 20 72 6f 6c 6c 62 61 63 6b 20   zero, rollback 
13741 61 6e 79 20 61 63 74 69 76 65 0a 2a 2a 20 74 72  any active.** tr
13742 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 75 6e  ansaction and un
13743 6c 6f 63 6b 20 74 68 65 20 70 61 67 65 72 2e 0a  lock the pager..
13744 2a 2a 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 6e  **.** Except, in
13745 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 45 58   locking_mode=EX
13746 43 4c 55 53 49 56 45 20 77 68 65 6e 20 74 68 65  CLUSIVE when the
13747 72 65 20 69 73 20 6e 6f 74 68 69 6e 67 20 74 6f  re is nothing to
13748 20 69 6e 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62   in.** the rollb
13749 61 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65  ack journal, the
1374a 20 75 6e 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 70   unlock is not p
1374b 65 72 66 6f 72 6d 65 64 20 61 6e 64 20 74 68 65  erformed and the
1374c 72 65 20 69 73 0a 2a 2a 20 6e 6f 74 68 69 6e 67  re is.** nothing
1374d 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20 73 6f   to rollback, so
1374e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
1374f 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 20 0a 73 74   a no-op..*/ .st
13750 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 55  atic void pagerU
13751 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28 50 61  nlockIfUnused(Pa
13752 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
13753 69 66 28 20 28 73 71 6c 69 74 65 33 50 63 61 63  if( (sqlite3Pcac
13754 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65  heRefCount(pPage
13755 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 29 0a  r->pPCache)==0).
13756 20 20 20 26 26 20 28 21 70 50 61 67 65 72 2d 3e     && (!pPager->
13757 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c  exclusiveMode ||
13758 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
13759 4f 66 66 3e 30 29 20 0a 20 20 29 7b 0a 20 20 20  Off>0) .  ){.   
1375a 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52   pagerUnlockAndR
1375b 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b  ollback(pPager);
1375c 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63  .  }.}../*.** Ac
1375d 71 75 69 72 65 20 61 20 72 65 66 65 72 65 6e 63  quire a referenc
1375e 65 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72  e to page number
1375f 20 70 67 6e 6f 20 69 6e 20 70 61 67 65 72 20 70   pgno in pager p
13760 50 61 67 65 72 20 28 61 20 70 61 67 65 0a 2a 2a  Pager (a page.**
13761 20 72 65 66 65 72 65 6e 63 65 20 68 61 73 20 74   reference has t
13762 79 70 65 20 44 62 50 61 67 65 2a 29 2e 20 49 66  ype DbPage*). If
13763 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 72   the requested r
13764 65 66 65 72 65 6e 63 65 20 69 73 20 0a 2a 2a 20  eference is .** 
13765 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 62 74  successfully obt
13766 61 69 6e 65 64 2c 20 69 74 20 69 73 20 63 6f 70  ained, it is cop
13767 69 65 64 20 74 6f 20 2a 70 70 50 61 67 65 20 61  ied to *ppPage a
13768 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74  nd SQLITE_OK ret
13769 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  urned..**.** If 
1376a 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61  the requested pa
1376b 67 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e  ge is already in
1376c 20 74 68 65 20 63 61 63 68 65 2c 20 69 74 20 69   the cache, it i
1376d 73 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 20  s returned. .** 
1376e 4f 74 68 65 72 77 69 73 65 2c 20 61 20 6e 65 77  Otherwise, a new
1376f 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20   page object is 
13770 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f  allocated and po
13771 70 75 6c 61 74 65 64 20 77 69 74 68 20 64 61 74  pulated with dat
13772 61 0a 2a 2a 20 72 65 61 64 20 66 72 6f 6d 20 74  a.** read from t
13773 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
13774 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 65 73 2c  . In some cases,
13775 20 74 68 65 20 70 63 61 63 68 65 20 6d 6f 64 75   the pcache modu
13776 6c 65 20 6d 61 79 0a 2a 2a 20 63 68 6f 6f 73 65  le may.** choose
13777 20 6e 6f 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65   not to allocate
13778 20 61 20 6e 65 77 20 70 61 67 65 20 6f 62 6a 65   a new page obje
13779 63 74 20 61 6e 64 20 6d 61 79 20 72 65 75 73 65  ct and may reuse
1377a 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20   an existing.** 
1377b 6f 62 6a 65 63 74 20 77 69 74 68 20 6e 6f 20 6f  object with no o
1377c 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72  utstanding refer
1377d 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ences..**.** The
1377e 20 65 78 74 72 61 20 64 61 74 61 20 61 70 70 65   extra data appe
1377f 6e 64 65 64 20 74 6f 20 61 20 70 61 67 65 20 69  nded to a page i
13780 73 20 61 6c 77 61 79 73 20 69 6e 69 74 69 61 6c  s always initial
13781 69 7a 65 64 20 74 6f 20 7a 65 72 6f 73 20 74 68  ized to zeros th
13782 65 20 0a 2a 2a 20 66 69 72 73 74 20 74 69 6d 65  e .** first time
13783 20 61 20 70 61 67 65 20 69 73 20 6c 6f 61 64 65   a page is loade
13784 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2e 20 49  d into memory. I
13785 66 20 74 68 65 20 70 61 67 65 20 72 65 71 75 65  f the page reque
13786 73 74 65 64 20 69 73 20 0a 2a 2a 20 61 6c 72 65  sted is .** alre
13787 61 64 79 20 69 6e 20 74 68 65 20 63 61 63 68 65  ady in the cache
13788 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
13789 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74  ion is called, t
1378a 68 65 6e 20 74 68 65 20 65 78 74 72 61 0a 2a 2a  hen the extra.**
1378b 20 64 61 74 61 20 69 73 20 6c 65 66 74 20 61 73   data is left as
1378c 20 69 74 20 77 61 73 20 77 68 65 6e 20 74 68 65   it was when the
1378d 20 70 61 67 65 20 6f 62 6a 65 63 74 20 77 61 73   page object was
1378e 20 6c 61 73 74 20 75 73 65 64 2e 0a 2a 2a 0a 2a   last used..**.*
1378f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  * If the databas
13790 65 20 69 6d 61 67 65 20 69 73 20 73 6d 61 6c 6c  e image is small
13791 65 72 20 74 68 61 6e 20 74 68 65 20 72 65 71 75  er than the requ
13792 65 73 74 65 64 20 70 61 67 65 20 6f 72 20 69 66  ested page or if
13793 20 61 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20   a .** non-zero 
13794 76 61 6c 75 65 20 69 73 20 70 61 73 73 65 64 20  value is passed 
13795 61 73 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e 74  as the noContent
13796 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 20 74   parameter and t
13797 68 65 20 0a 2a 2a 20 72 65 71 75 65 73 74 65 64  he .** requested
13798 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72   page is not alr
13799 65 61 64 79 20 73 74 6f 72 65 64 20 69 6e 20 74  eady stored in t
1379a 68 65 20 63 61 63 68 65 2c 20 74 68 65 6e 20 6e  he cache, then n
1379b 6f 20 0a 2a 2a 20 61 63 74 75 61 6c 20 64 69 73  o .** actual dis
1379c 6b 20 72 65 61 64 20 6f 63 63 75 72 73 2e 20 49  k read occurs. I
1379d 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20  n this case the 
1379e 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f 66 20  memory image of 
1379f 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 69 73 20  the .** page is 
137a0 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61  initialized to a
137a1 6c 6c 20 7a 65 72 6f 73 2e 20 0a 2a 2a 0a 2a 2a  ll zeros. .**.**
137a2 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73   If noContent is
137a3 20 74 72 75 65 2c 20 69 74 20 6d 65 61 6e 73 20   true, it means 
137a4 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 20 63  that we do not c
137a5 61 72 65 20 61 62 6f 75 74 20 74 68 65 20 63 6f  are about the co
137a6 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65  ntents.** of the
137a7 20 70 61 67 65 2e 20 54 68 69 73 20 6f 63 63 75   page. This occu
137a8 72 73 20 69 6e 20 74 77 6f 20 73 65 70 65 72 61  rs in two sepera
137a9 74 65 20 73 63 65 6e 61 72 69 6f 73 3a 0a 2a 2a  te scenarios:.**
137aa 0a 2a 2a 20 20 20 61 29 20 57 68 65 6e 20 72 65  .**   a) When re
137ab 61 64 69 6e 67 20 61 20 66 72 65 65 2d 6c 69 73  ading a free-lis
137ac 74 20 6c 65 61 66 20 70 61 67 65 20 66 72 6f 6d  t leaf page from
137ad 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 61   the database, a
137ae 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 62 29 20 57 68  nd.**.**   b) Wh
137af 65 6e 20 61 20 73 61 76 65 70 6f 69 6e 74 20 69  en a savepoint i
137b0 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62  s being rolled b
137b1 61 63 6b 20 61 6e 64 20 77 65 20 6e 65 65 64 20  ack and we need 
137b2 74 6f 20 6c 6f 61 64 0a 2a 2a 20 20 20 20 20 20  to load.**      
137b3 61 20 6e 65 77 20 70 61 67 65 20 69 6e 74 6f 20  a new page into 
137b4 74 68 65 20 63 61 63 68 65 20 74 6f 20 70 6f 70  the cache to pop
137b5 75 6c 61 74 65 20 77 69 74 68 20 74 68 65 20 64  ulate with the d
137b6 61 74 61 20 72 65 61 64 0a 2a 2a 20 20 20 20 20  ata read.**     
137b7 20 66 72 6f 6d 20 74 68 65 20 73 61 76 65 70 6f   from the savepo
137b8 69 6e 74 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a  int journal..**.
137b9 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 20  ** If noContent 
137ba 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68  is true, then th
137bb 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 64 20  e data returned 
137bc 69 73 20 7a 65 72 6f 65 64 20 69 6e 73 74 65 61  is zeroed instea
137bd 64 20 6f 66 0a 2a 2a 20 62 65 69 6e 67 20 72 65  d of.** being re
137be 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ad from the data
137bf 62 61 73 65 2e 20 41 64 64 69 74 69 6f 6e 61 6c  base. Additional
137c0 6c 79 2c 20 74 68 65 20 62 69 74 73 20 63 6f 72  ly, the bits cor
137c1 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f  responding.** to
137c2 20 70 67 6e 6f 20 69 6e 20 50 61 67 65 72 2e 70   pgno in Pager.p
137c3 49 6e 4a 6f 75 72 6e 61 6c 20 28 62 69 74 76 65  InJournal (bitve
137c4 63 20 6f 66 20 70 61 67 65 73 20 61 6c 72 65 61  c of pages alrea
137c5 64 79 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  dy written to th
137c6 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  e.** journal fil
137c7 65 29 20 61 6e 64 20 74 68 65 20 50 61 67 65 72  e) and the Pager
137c8 53 61 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76  Savepoint.pInSav
137c9 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73 20 6f  epoint bitvecs o
137ca 66 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 73 61  f any open.** sa
137cb 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73 65 74  vepoints are set
137cc 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 69 66 20  . This means if 
137cd 74 68 65 20 70 61 67 65 20 69 73 20 6d 61 64 65  the page is made
137ce 20 77 72 69 74 61 62 6c 65 20 61 74 20 61 6e 79   writable at any
137cf 0a 2a 2a 20 70 6f 69 6e 74 20 69 6e 20 74 68 65  .** point in the
137d0 20 66 75 74 75 72 65 2c 20 75 73 69 6e 67 20 61   future, using a
137d1 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
137d2 50 61 67 65 72 57 72 69 74 65 28 29 2c 20 69 74  PagerWrite(), it
137d3 73 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 77 69  s contents.** wi
137d4 6c 6c 20 6e 6f 74 20 62 65 20 6a 6f 75 72 6e 61  ll not be journa
137d5 6c 65 64 2e 20 54 68 69 73 20 73 61 76 65 73 20  led. This saves 
137d6 49 4f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63  IO..**.** The ac
137d7 71 75 69 73 69 74 69 6f 6e 20 6d 69 67 68 74 20  quisition might 
137d8 66 61 69 6c 20 66 6f 72 20 73 65 76 65 72 61 6c  fail for several
137d9 20 72 65 61 73 6f 6e 73 2e 20 20 49 6e 20 61 6c   reasons.  In al
137da 6c 20 63 61 73 65 73 2c 0a 2a 2a 20 61 6e 20 61  l cases,.** an a
137db 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72  ppropriate error
137dc 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
137dd 64 20 61 6e 64 20 2a 70 70 50 61 67 65 20 69 73  d and *ppPage is
137de 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a   set to NULL..**
137df 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 6c  .** See also sql
137e0 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28  ite3PagerLookup(
137e1 29 2e 20 20 42 6f 74 68 20 74 68 69 73 20 72 6f  ).  Both this ro
137e2 75 74 69 6e 65 20 61 6e 64 20 4c 6f 6f 6b 75 70  utine and Lookup
137e3 28 29 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f  () attempt.** to
137e4 20 66 69 6e 64 20 61 20 70 61 67 65 20 69 6e 20   find a page in 
137e5 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61  the in-memory ca
137e6 63 68 65 20 66 69 72 73 74 2e 20 20 49 66 20 74  che first.  If t
137e7 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61  he page is not a
137e8 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20 6d 65 6d  lready.** in mem
137e9 6f 72 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  ory, this routin
137ea 65 20 67 6f 65 73 20 74 6f 20 64 69 73 6b 20 74  e goes to disk t
137eb 6f 20 72 65 61 64 20 69 74 20 69 6e 20 77 68 65  o read it in whe
137ec 72 65 61 73 20 4c 6f 6f 6b 75 70 28 29 0a 2a 2a  reas Lookup().**
137ed 20 6a 75 73 74 20 72 65 74 75 72 6e 73 20 30 2e   just returns 0.
137ee 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61    This routine a
137ef 63 71 75 69 72 65 73 20 61 20 72 65 61 64 2d 6c  cquires a read-l
137f0 6f 63 6b 20 74 68 65 20 66 69 72 73 74 20 74 69  ock the first ti
137f1 6d 65 20 69 74 0a 2a 2a 20 68 61 73 20 74 6f 20  me it.** has to 
137f2 67 6f 20 74 6f 20 64 69 73 6b 2c 20 61 6e 64 20  go to disk, and 
137f3 63 6f 75 6c 64 20 61 6c 73 6f 20 70 6c 61 79 62  could also playb
137f4 61 63 6b 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e  ack an old journ
137f5 61 6c 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e  al if necessary.
137f6 0a 2a 2a 20 53 69 6e 63 65 20 4c 6f 6f 6b 75 70  .** Since Lookup
137f7 28 29 20 6e 65 76 65 72 20 67 6f 65 73 20 74 6f  () never goes to
137f8 20 64 69 73 6b 2c 20 69 74 20 6e 65 76 65 72 20   disk, it never 
137f9 68 61 73 20 74 6f 20 64 65 61 6c 20 77 69 74 68  has to deal with
137fa 20 6c 6f 63 6b 73 0a 2a 2a 20 6f 72 20 6a 6f 75   locks.** or jou
137fb 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53  rnal files..*/.S
137fc 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
137fd 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63  t sqlite3PagerAc
137fe 71 75 69 72 65 28 0a 20 20 50 61 67 65 72 20 2a  quire(.  Pager *
137ff 70 50 61 67 65 72 2c 20 20 20 20 20 20 2f 2a 20  pPager,      /* 
13800 54 68 65 20 70 61 67 65 72 20 6f 70 65 6e 20 6f  The pager open o
13801 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
13802 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67  ile */.  Pgno pg
13803 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  no,          /* 
13804 50 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 66  Page number to f
13805 65 74 63 68 20 2a 2f 0a 20 20 44 62 50 61 67 65  etch */.  DbPage
13806 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 2f 2a   **ppPage,    /*
13807 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65 72   Write a pointer
13808 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 65 72   to the page her
13809 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e  e */.  int noCon
1380a 74 65 6e 74 20 20 20 20 20 20 20 2f 2a 20 44 6f  tent       /* Do
1380b 20 6e 6f 74 20 62 6f 74 68 65 72 20 72 65 61 64   not bother read
1380c 69 6e 67 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d  ing content from
1380d 20 64 69 73 6b 20 69 66 20 74 72 75 65 20 2a 2f   disk if true */
1380e 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
1380f 50 67 48 64 72 20 2a 70 50 67 3b 0a 0a 20 20 61  PgHdr *pPg;..  a
13810 73 73 65 72 74 28 20 61 73 73 65 72 74 5f 70 61  ssert( assert_pa
13811 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 65 72  ger_state(pPager
13812 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
13813 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 50 41 47  Pager->state>PAG
13814 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 0a 20 20  ER_UNLOCK );..  
13815 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29 7b 0a 20  if( pgno==0 ){. 
13816 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
13817 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
13818 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
13819 70 61 67 65 72 20 69 73 20 69 6e 20 74 68 65 20  pager is in the 
1381a 65 72 72 6f 72 20 73 74 61 74 65 2c 20 72 65 74  error state, ret
1381b 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6d 6d  urn an error imm
1381c 65 64 69 61 74 65 6c 79 2e 20 0a 20 20 2a 2a 20  ediately. .  ** 
1381d 4f 74 68 65 72 77 69 73 65 2c 20 72 65 71 75 65  Otherwise, reque
1381e 73 74 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d  st the page from
1381f 20 74 68 65 20 50 43 61 63 68 65 20 6c 61 79 65   the PCache laye
13820 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67  r. */.  if( pPag
13821 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c  er->errCode!=SQL
13822 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72  ITE_OK && pPager
13823 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 54  ->errCode!=SQLIT
13824 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 72 63  E_FULL ){.    rc
13825 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f   = pPager->errCo
13826 64 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  de;.  }else{.   
13827 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 63 61   rc = sqlite3Pca
13828 63 68 65 46 65 74 63 68 28 70 50 61 67 65 72 2d  cheFetch(pPager-
13829 3e 70 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20  >pPCache, pgno, 
1382a 31 2c 20 70 70 50 61 67 65 29 3b 0a 20 20 7d 0a  1, ppPage);.  }.
1382b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
1382c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 45  E_OK ){.    /* E
1382d 69 74 68 65 72 20 74 68 65 20 63 61 6c 6c 20 74  ither the call t
1382e 6f 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 46  o sqlite3PcacheF
1382f 65 74 63 68 28 29 20 72 65 74 75 72 6e 65 64 20  etch() returned 
13830 61 6e 20 65 72 72 6f 72 20 6f 72 20 74 68 65 0a  an error or the.
13831 20 20 20 20 2a 2a 20 70 61 67 65 72 20 77 61 73      ** pager was
13832 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
13833 65 72 72 6f 72 2d 73 74 61 74 65 20 77 68 65 6e  error-state when
13834 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77   this function w
13835 61 73 20 63 61 6c 6c 65 64 2e 0a 20 20 20 20 2a  as called..    *
13836 2a 20 53 65 74 20 70 50 67 20 74 6f 20 30 20 61  * Set pPg to 0 a
13837 6e 64 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 65  nd jump to the e
13838 78 63 65 70 74 69 6f 6e 20 68 61 6e 64 6c 65 72  xception handler
13839 2e 20 20 2a 2f 0a 20 20 20 20 70 50 67 20 3d 20  .  */.    pPg = 
1383a 30 3b 0a 20 20 20 20 67 6f 74 6f 20 70 61 67 65  0;.    goto page
1383b 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20  r_acquire_err;. 
1383c 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 2a 70   }.  assert( (*p
1383d 70 50 61 67 65 29 2d 3e 70 67 6e 6f 3d 3d 70 67  pPage)->pgno==pg
1383e 6e 6f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  no );.  assert( 
1383f 28 2a 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65  (*ppPage)->pPage
13840 72 3d 3d 70 50 61 67 65 72 20 7c 7c 20 28 2a 70  r==pPager || (*p
13841 70 50 61 67 65 29 2d 3e 70 50 61 67 65 72 3d 3d  pPage)->pPager==
13842 30 20 29 3b 0a 0a 20 20 69 66 28 20 28 2a 70 70  0 );..  if( (*pp
13843 50 61 67 65 29 2d 3e 70 50 61 67 65 72 20 29 7b  Page)->pPager ){
13844 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20  .    /* In this 
13845 63 61 73 65 20 74 68 65 20 70 63 61 63 68 65 20  case the pcache 
13846 61 6c 72 65 61 64 79 20 63 6f 6e 74 61 69 6e 73  already contains
13847 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   an initialized 
13848 63 6f 70 79 20 6f 66 0a 20 20 20 20 2a 2a 20 74  copy of.    ** t
13849 68 65 20 70 61 67 65 2e 20 52 65 74 75 72 6e 20  he page. Return 
1384a 77 69 74 68 6f 75 74 20 66 75 72 74 68 65 72 20  without further 
1384b 61 64 6f 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73  ado.  */.    ass
1384c 65 72 74 28 20 70 67 6e 6f 3c 3d 50 41 47 45 52  ert( pgno<=PAGER
1384d 5f 4d 41 58 5f 50 47 4e 4f 20 26 26 20 70 67 6e  _MAX_PGNO && pgn
1384e 6f 21 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  o!=PAGER_MJ_PGNO
1384f 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20  (pPager) );.    
13850 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65  PAGER_INCR(pPage
13851 72 2d 3e 6e 48 69 74 29 3b 0a 20 20 20 20 72 65  r->nHit);.    re
13852 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
13853 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
13854 20 54 68 65 20 70 61 67 65 72 20 63 61 63 68 65   The pager cache
13855 20 68 61 73 20 63 72 65 61 74 65 64 20 61 20 6e   has created a n
13856 65 77 20 70 61 67 65 2e 20 49 74 73 20 63 6f 6e  ew page. Its con
13857 74 65 6e 74 20 6e 65 65 64 73 20 74 6f 20 0a 20  tent needs to . 
13858 20 20 20 2a 2a 20 62 65 20 69 6e 69 74 69 61 6c     ** be initial
13859 69 7a 65 64 2e 20 20 2a 2f 0a 20 20 20 20 69 6e  ized.  */.    in
1385a 74 20 6e 4d 61 78 3b 0a 0a 20 20 20 20 50 41 47  t nMax;..    PAG
1385b 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e  ER_INCR(pPager->
1385c 6e 4d 69 73 73 29 3b 0a 20 20 20 20 70 50 67 20  nMiss);.    pPg 
1385d 3d 20 2a 70 70 50 61 67 65 3b 0a 20 20 20 20 70  = *ppPage;.    p
1385e 50 67 2d 3e 70 50 61 67 65 72 20 3d 20 70 50 61  Pg->pPager = pPa
1385f 67 65 72 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65  ger;..    /* The
13860 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 6e 75   maximum page nu
13861 6d 62 65 72 20 69 73 20 32 5e 33 31 2e 20 52 65  mber is 2^31. Re
13862 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
13863 55 50 54 20 69 66 20 61 20 70 61 67 65 0a 20 20  UPT if a page.  
13864 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67 72 65 61    ** number grea
13865 74 65 72 20 74 68 61 6e 20 74 68 69 73 2c 20 6f  ter than this, o
13866 72 20 74 68 65 20 75 6e 75 73 65 64 20 6c 6f 63  r the unused loc
13867 6b 69 6e 67 2d 70 61 67 65 2c 20 69 73 20 72 65  king-page, is re
13868 71 75 65 73 74 65 64 2e 20 2a 2f 0a 20 20 20 20  quested. */.    
13869 69 66 28 20 70 67 6e 6f 3e 50 41 47 45 52 5f 4d  if( pgno>PAGER_M
1386a 41 58 5f 50 47 4e 4f 20 7c 7c 20 70 67 6e 6f 3d  AX_PGNO || pgno=
1386b 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70  =PAGER_MJ_PGNO(p
1386c 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20  Pager) ){.      
1386d 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52  rc = SQLITE_CORR
1386e 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20  UPT_BKPT;.      
1386f 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69  goto pager_acqui
13870 72 65 5f 65 72 72 3b 0a 20 20 20 20 7d 0a 0a 20  re_err;.    }.. 
13871 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
13872 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50  agerPagecount(pP
13873 61 67 65 72 2c 20 26 6e 4d 61 78 29 3b 0a 20 20  ager, &nMax);.  
13874 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
13875 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  _OK ){.      got
13876 6f 20 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f  o pager_acquire_
13877 65 72 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  err;.    }..    
13878 69 66 28 20 6e 4d 61 78 3c 28 69 6e 74 29 70 67  if( nMax<(int)pg
13879 6e 6f 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20 6e  no || MEMDB || n
1387a 6f 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20  oContent ){.    
1387b 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65    if( pgno>pPage
1387c 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a 09 72 63  r->mxPgno ){..rc
1387d 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a   = SQLITE_FULL;.
1387e 09 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 75  .goto pager_acqu
1387f 69 72 65 5f 65 72 72 3b 0a 20 20 20 20 20 20 7d  ire_err;.      }
13880 0a 20 20 20 20 20 20 69 66 28 20 6e 6f 43 6f 6e  .      if( noCon
13881 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20  tent ){.        
13882 2f 2a 20 46 61 69 6c 75 72 65 20 74 6f 20 73 65  /* Failure to se
13883 74 20 74 68 65 20 62 69 74 73 20 69 6e 20 74 68  t the bits in th
13884 65 20 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 2d  e InJournal bit-
13885 76 65 63 74 6f 72 73 20 69 73 20 62 65 6e 69 67  vectors is benig
13886 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 74  n..        ** It
13887 20 6d 65 72 65 6c 79 20 6d 65 61 6e 73 20 74 68   merely means th
13888 61 74 20 77 65 20 6d 69 67 68 74 20 64 6f 20 73  at we might do s
13889 6f 6d 65 20 65 78 74 72 61 20 77 6f 72 6b 20 74  ome extra work t
1388a 6f 20 6a 6f 75 72 6e 61 6c 20 61 20 0a 20 20 20  o journal a .   
1388b 20 20 20 20 20 2a 2a 20 70 61 67 65 20 74 68 61       ** page tha
1388c 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  t does not need 
1388d 74 6f 20 62 65 20 6a 6f 75 72 6e 61 6c 65 64 2e  to be journaled.
1388e 20 20 4e 65 76 65 72 74 68 65 6c 65 73 73 2c 20    Nevertheless, 
1388f 62 65 20 73 75 72 65 20 0a 20 20 20 20 20 20 20  be sure .       
13890 20 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65 20   ** to test the 
13891 63 61 73 65 20 77 68 65 72 65 20 61 20 6d 61 6c  case where a mal
13892 6c 6f 63 20 65 72 72 6f 72 20 6f 63 63 75 72 73  loc error occurs
13893 20 77 68 69 6c 65 20 74 72 79 69 6e 67 20 74 6f   while trying to
13894 20 73 65 74 20 0a 20 20 20 20 20 20 20 20 2a 2a   set .        **
13895 20 61 20 62 69 74 20 69 6e 20 61 20 62 69 74 20   a bit in a bit 
13896 76 65 63 74 6f 72 2e 0a 20 20 20 20 20 20 20 20  vector..        
13897 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  */.        sqlit
13898 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c  e3BeginBenignMal
13899 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 20 20 69  loc();.        i
1389a 66 28 20 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d  f( pgno<=pPager-
1389b 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20  >dbOrigSize ){. 
1389c 20 20 20 20 20 20 20 20 20 54 45 53 54 4f 4e 4c           TESTONL
1389d 59 28 20 72 63 20 3d 20 29 20 73 71 6c 69 74 65  Y( rc = ) sqlite
1389e 33 42 69 74 76 65 63 53 65 74 28 70 50 61 67 65  3BitvecSet(pPage
1389f 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70  r->pInJournal, p
138a0 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20  gno);.          
138a1 74 65 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51  testcase( rc==SQ
138a2 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20  LITE_NOMEM );.  
138a3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
138a4 54 45 53 54 4f 4e 4c 59 28 20 72 63 20 3d 20 29  TESTONLY( rc = )
138a5 20 61 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42   addToSavepointB
138a6 69 74 76 65 63 73 28 70 50 61 67 65 72 2c 20 70  itvecs(pPager, p
138a7 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 74 65  gno);.        te
138a8 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49  stcase( rc==SQLI
138a9 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20  TE_NOMEM );.    
138aa 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65      sqlite3EndBe
138ab 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20  nignMalloc();.  
138ac 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
138ad 20 20 20 6d 65 6d 73 65 74 28 70 50 67 2d 3e 70     memset(pPg->p
138ae 44 61 74 61 2c 20 30 2c 20 70 50 61 67 65 72 2d  Data, 0, pPager-
138af 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20  >pageSize);.    
138b0 20 20 7d 0a 20 20 20 20 20 20 49 4f 54 52 41 43    }.      IOTRAC
138b1 45 28 28 22 5a 45 52 4f 20 25 70 20 25 64 5c 6e  E(("ZERO %p %d\n
138b2 22 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f 29  ", pPager, pgno)
138b3 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
138b4 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67 2d      assert( pPg-
138b5 3e 70 50 61 67 65 72 3d 3d 70 50 61 67 65 72 20  >pPager==pPager 
138b6 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65  );.      rc = re
138b7 61 64 44 62 50 61 67 65 28 70 50 67 29 3b 0a 20  adDbPage(pPg);. 
138b8 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
138b9 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
138ba 20 20 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71    goto pager_acq
138bb 75 69 72 65 5f 65 72 72 3b 0a 20 20 20 20 20 20  uire_err;.      
138bc 7d 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53  }.    }.#ifdef S
138bd 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45  QLITE_CHECK_PAGE
138be 53 0a 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48  S.    pPg->pageH
138bf 61 73 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65  ash = pager_page
138c0 68 61 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69  hash(pPg);.#endi
138c1 66 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  f.  }..  return 
138c2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65  SQLITE_OK;..page
138c3 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3a 0a 20  r_acquire_err:. 
138c4 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
138c5 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20  ITE_OK );.  if( 
138c6 70 50 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  pPg ){.    sqlit
138c7 65 33 50 63 61 63 68 65 44 72 6f 70 28 70 50 67  e3PcacheDrop(pPg
138c8 29 3b 0a 20 20 7d 0a 20 20 70 61 67 65 72 55 6e  );.  }.  pagerUn
138c9 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28 70 50 61  lockIfUnused(pPa
138ca 67 65 72 29 3b 0a 0a 20 20 2a 70 70 50 61 67 65  ger);..  *ppPage
138cb 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72   = 0;.  return r
138cc 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75  c;.}../*.** Acqu
138cd 69 72 65 20 61 20 70 61 67 65 20 69 66 20 69 74  ire a page if it
138ce 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74   is already in t
138cf 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63  he in-memory cac
138d0 68 65 2e 20 20 44 6f 0a 2a 2a 20 6e 6f 74 20 72  he.  Do.** not r
138d1 65 61 64 20 74 68 65 20 70 61 67 65 20 66 72 6f  ead the page fro
138d2 6d 20 64 69 73 6b 2e 20 20 52 65 74 75 72 6e 20  m disk.  Return 
138d3 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
138d4 20 70 61 67 65 2c 0a 2a 2a 20 6f 72 20 30 20 69   page,.** or 0 i
138d5 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f  f the page is no
138d6 74 20 69 6e 20 63 61 63 68 65 2e 20 41 6c 73 6f  t in cache. Also
138d7 2c 20 72 65 74 75 72 6e 20 30 20 69 66 20 74 68  , return 0 if th
138d8 65 20 0a 2a 2a 20 70 61 67 65 72 20 69 73 20 69  e .** pager is i
138d9 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 73  n PAGER_UNLOCK s
138da 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 66  tate when this f
138db 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
138dc 64 2c 0a 2a 2a 20 6f 72 20 69 66 20 74 68 65 20  d,.** or if the 
138dd 70 61 67 65 72 20 69 73 20 69 6e 20 61 6e 20 65  pager is in an e
138de 72 72 6f 72 20 73 74 61 74 65 20 6f 74 68 65 72  rror state other
138df 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 46 55 4c   than SQLITE_FUL
138e0 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  L..**.** See als
138e1 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  o sqlite3PagerGe
138e2 74 28 29 2e 20 20 54 68 65 20 64 69 66 66 65 72  t().  The differ
138e3 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 69  ence between thi
138e4 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 6e 64  s routine.** and
138e5 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
138e6 28 29 20 69 73 20 74 68 61 74 20 5f 67 65 74 28  () is that _get(
138e7 29 20 77 69 6c 6c 20 67 6f 20 74 6f 20 74 68 65  ) will go to the
138e8 20 64 69 73 6b 20 61 6e 64 20 72 65 61 64 0a 2a   disk and read.*
138e9 2a 20 69 6e 20 74 68 65 20 70 61 67 65 20 69 66  * in the page if
138ea 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
138eb 20 61 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68   already in cach
138ec 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
138ed 0a 2a 2a 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c  .** returns NULL
138ee 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20   if the page is 
138ef 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20  not in cache or 
138f0 69 66 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72  if a disk I/O er
138f1 72 6f 72 20 0a 2a 2a 20 68 61 73 20 65 76 65 72  ror .** has ever
138f2 20 68 61 70 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51   happened..*/.SQ
138f3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 44 62 50  LITE_PRIVATE DbP
138f4 61 67 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65  age *sqlite3Page
138f5 72 4c 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70  rLookup(Pager *p
138f6 50 61 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f  Pager, Pgno pgno
138f7 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20  ){.  PgHdr *pPg 
138f8 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70  = 0;.  assert( p
138f9 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 61 73  Pager!=0 );.  as
138fa 73 65 72 74 28 20 70 67 6e 6f 21 3d 30 20 29 3b  sert( pgno!=0 );
138fb 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
138fc 72 2d 3e 70 50 43 61 63 68 65 21 3d 30 20 29 3b  r->pPCache!=0 );
138fd 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
138fe 72 2d 3e 73 74 61 74 65 20 3e 20 50 41 47 45 52  r->state > PAGER
138ff 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 73 71 6c  _UNLOCK );.  sql
13900 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68 28  ite3PcacheFetch(
13901 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c  pPager->pPCache,
13902 20 70 67 6e 6f 2c 20 30 2c 20 26 70 50 67 29 3b   pgno, 0, &pPg);
13903 0a 20 20 72 65 74 75 72 6e 20 70 50 67 3b 0a 7d  .  return pPg;.}
13904 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20  ../*.** Release 
13905 61 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65  a page reference
13906 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e  ..**.** If the n
13907 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e  umber of referen
13908 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20  ces to the page 
13909 64 72 6f 70 20 74 6f 20 7a 65 72 6f 2c 20 74 68  drop to zero, th
1390a 65 6e 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 69  en the.** page i
1390b 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 4c  s added to the L
1390c 52 55 20 6c 69 73 74 2e 20 20 57 68 65 6e 20 61  RU list.  When a
1390d 6c 6c 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  ll references to
1390e 20 61 6c 6c 20 70 61 67 65 73 0a 2a 2a 20 61 72   all pages.** ar
1390f 65 20 72 65 6c 65 61 73 65 64 2c 20 61 20 72 6f  e released, a ro
13910 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 20 61 6e  llback occurs an
13911 64 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68  d the lock on th
13912 65 20 64 61 74 61 62 61 73 65 20 69 73 0a 2a 2a  e database is.**
13913 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c   removed..*/.SQL
13914 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
13915 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
13916 65 66 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b  ef(DbPage *pPg){
13917 0a 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20  .  if( pPg ){.  
13918 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20    Pager *pPager 
13919 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20  = pPg->pPager;. 
1391a 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65     sqlite3Pcache
1391b 52 65 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 20  Release(pPg);.  
1391c 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55    pagerUnlockIfU
1391d 6e 75 73 65 64 28 70 50 61 67 65 72 29 3b 0a 20  nused(pPager);. 
1391e 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74   }.}../*.** If t
1391f 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  he main journal 
13920 66 69 6c 65 20 68 61 73 20 61 6c 72 65 61 64 79  file has already
13921 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c 20 65 6e   been opened, en
13922 73 75 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a  sure that the.**
13923 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c   sub-journal fil
13924 65 20 69 73 20 6f 70 65 6e 20 74 6f 6f 2e 20 49  e is open too. I
13925 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e  f the main journ
13926 61 6c 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c 0a  al is not open,.
13927 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** this function
13928 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a   is a no-op..**.
13929 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
1392a 72 65 74 75 72 6e 65 64 20 69 66 20 65 76 65 72  returned if ever
1392b 79 74 68 69 6e 67 20 67 6f 65 73 20 61 63 63 6f  ything goes acco
1392c 72 64 69 6e 67 20 74 6f 20 70 6c 61 6e 2e 20 0a  rding to plan. .
1392d 2a 2a 20 41 6e 20 53 51 4c 49 54 45 5f 49 4f 45  ** An SQLITE_IOE
1392e 52 52 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64  RR_XXX error cod
1392f 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  e is returned if
13930 20 61 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 73   a call to .** s
13931 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 29 20 66  qlite3OsOpen() f
13932 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ails..*/.static 
13933 69 6e 74 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e  int openSubJourn
13934 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  al(Pager *pPager
13935 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
13936 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69  LITE_OK;.  if( i
13937 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
13938 64 29 20 26 26 20 21 69 73 4f 70 65 6e 28 70 50  d) && !isOpen(pP
13939 61 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20  ager->sjfd) ){. 
1393a 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a     if( pPager->j
1393b 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45  ournalMode==PAGE
1393c 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45  R_JOURNALMODE_ME
1393d 4d 4f 52 59 20 7c 7c 20 70 50 61 67 65 72 2d 3e  MORY || pPager->
1393e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 29 7b 0a  subjInMemory ){.
1393f 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d        sqlite3Mem
13940 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67  JournalOpen(pPag
13941 65 72 2d 3e 73 6a 66 64 29 3b 0a 20 20 20 20 7d  er->sjfd);.    }
13942 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
13943 20 70 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 70   pagerOpentemp(p
13944 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73  Pager, pPager->s
13945 6a 66 64 2c 20 53 51 4c 49 54 45 5f 4f 50 45 4e  jfd, SQLITE_OPEN
13946 5f 53 55 42 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20  _SUBJOURNAL);.  
13947 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
13948 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
13949 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
1394a 61 6c 6c 65 64 20 61 74 20 74 68 65 20 73 74 61  alled at the sta
1394b 72 74 20 6f 66 20 65 76 65 72 79 20 77 72 69 74  rt of every writ
1394c 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  e transaction..*
1394d 2a 20 54 68 65 72 65 20 6d 75 73 74 20 61 6c 72  * There must alr
1394e 65 61 64 79 20 62 65 20 61 20 52 45 53 45 52 56  eady be a RESERV
1394f 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20  ED or EXCLUSIVE 
13950 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
13951 62 61 73 65 20 0a 2a 2a 20 66 69 6c 65 20 77 68  base .** file wh
13952 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
13953 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a  is called..**.**
13954 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61   Open the journa
13955 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65 72  l file for pager
13956 20 70 50 61 67 65 72 20 61 6e 64 20 77 72 69 74   pPager and writ
13957 65 20 61 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  e a journal head
13958 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74 61  er.** to the sta
13959 72 74 20 6f 66 20 69 74 2e 20 49 66 20 74 68 65  rt of it. If the
1395a 72 65 20 61 72 65 20 61 63 74 69 76 65 20 73 61  re are active sa
1395b 76 65 70 6f 69 6e 74 73 2c 20 6f 70 65 6e 20 74  vepoints, open t
1395c 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a  he sub-journal.*
1395d 2a 20 61 73 20 77 65 6c 6c 2e 20 54 68 69 73 20  * as well. This 
1395e 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79  function is only
1395f 20 75 73 65 64 20 77 68 65 6e 20 74 68 65 20 6a   used when the j
13960 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 62  ournal file is b
13961 65 69 6e 67 20 0a 2a 2a 20 6f 70 65 6e 65 64 20  eing .** opened 
13962 74 6f 20 77 72 69 74 65 20 61 20 72 6f 6c 6c 62  to write a rollb
13963 61 63 6b 20 6c 6f 67 20 66 6f 72 20 61 20 74 72  ack log for a tr
13964 61 6e 73 61 63 74 69 6f 6e 2e 20 49 74 20 69 73  ansaction. It is
13965 20 6e 6f 74 20 75 73 65 64 20 0a 2a 2a 20 77 68   not used .** wh
13966 65 6e 20 6f 70 65 6e 69 6e 67 20 61 20 68 6f 74  en opening a hot
13967 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f   journal file to
13968 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 0a 2a   roll it back..*
13969 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72  *.** If the jour
1396a 6e 61 6c 20 66 69 6c 65 20 69 73 20 61 6c 72 65  nal file is alre
1396b 61 64 79 20 6f 70 65 6e 20 28 61 73 20 69 74 20  ady open (as it 
1396c 6d 61 79 20 62 65 20 69 6e 20 65 78 63 6c 75 73  may be in exclus
1396d 69 76 65 20 6d 6f 64 65 29 2c 0a 2a 2a 20 74 68  ive mode),.** th
1396e 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
1396f 20 6a 75 73 74 20 77 72 69 74 65 73 20 61 20 6a   just writes a j
13970 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 74 6f  ournal header to
13971 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
13972 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20 6f 70 65  e.** already ope
13973 6e 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 57  n file. .**.** W
13974 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
13975 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
13976 73 20 6f 70 65 6e 65 64 20 62 79 20 74 68 69 73  s opened by this
13977 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 0a 2a   function, the.*
13978 2a 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e  * Pager.pInJourn
13979 61 6c 20 62 69 74 76 65 63 20 73 74 72 75 63 74  al bitvec struct
1397a 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ure is allocated
1397b 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
1397c 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72  QLITE_OK if ever
1397d 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 65 73  ything is succes
1397e 73 66 75 6c 2e 20 4f 74 68 65 72 77 69 73 65 2c  sful. Otherwise,
1397f 20 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49   return .** SQLI
13980 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 74 68 65 20  TE_NOMEM if the 
13981 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63  attempt to alloc
13982 61 74 65 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75  ate Pager.pInJou
13983 72 6e 61 6c 20 66 61 69 6c 73 2c 20 6f 72 20 0a  rnal fails, or .
13984 2a 2a 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63  ** an IO error c
13985 6f 64 65 20 69 66 20 6f 70 65 6e 69 6e 67 20 6f  ode if opening o
13986 72 20 77 72 69 74 69 6e 67 20 74 68 65 20 6a 6f  r writing the jo
13987 75 72 6e 61 6c 20 66 69 6c 65 20 66 61 69 6c 73  urnal file fails
13988 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
13989 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e  pager_open_journ
1398a 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  al(Pager *pPager
1398b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1398c 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20  LITE_OK;        
1398d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1398e 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
1398f 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  /.  sqlite3_vfs 
13990 2a 20 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 70  * const pVfs = p
13991 50 61 67 65 72 2d 3e 70 56 66 73 3b 20 20 20 2f  Pager->pVfs;   /
13992 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66  * Local cache of
13993 20 76 66 73 20 70 6f 69 6e 74 65 72 20 2a 2f 0a   vfs pointer */.
13994 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
13995 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f  r->state>=PAGER_
13996 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 61 73  RESERVED );.  as
13997 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 75 73  sert( pPager->us
13998 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73  eJournal );.  as
13999 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f  sert( pPager->jo
1399a 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52  urnalMode!=PAGER
1399b 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46  _JOURNALMODE_OFF
1399c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
1399d 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
1399e 3d 3d 30 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 49  ==0 );.  .  /* I
1399f 66 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  f already in the
139a0 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 74 68   error state, th
139a1 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  is function is a
139a2 20 6e 6f 2d 6f 70 2e 20 20 42 75 74 20 6f 6e 0a   no-op.  But on.
139a3 20 20 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 68    ** the other h
139a4 61 6e 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  and, this routin
139a5 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65  e is never calle
139a6 64 20 69 66 20 77 65 20 61 72 65 20 61 6c 72 65  d if we are alre
139a7 61 64 79 20 69 6e 0a 20 20 2a 2a 20 61 6e 20 65  ady in.  ** an e
139a8 72 72 6f 72 20 73 74 61 74 65 2e 20 2a 2f 0a 20  rror state. */. 
139a9 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65   if( NEVER(pPage
139aa 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 72 65  r->errCode) ) re
139ab 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72  turn pPager->err
139ac 43 6f 64 65 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f  Code;..  /* TODO
139ad 3a 20 49 73 20 69 74 20 72 65 61 6c 6c 79 20 70  : Is it really p
139ae 6f 73 73 69 62 6c 65 20 74 6f 20 67 65 74 20 68  ossible to get h
139af 65 72 65 20 77 69 74 68 20 64 62 53 69 7a 65 56  ere with dbSizeV
139b0 61 6c 69 64 3d 3d 30 3f 20 49 66 20 6e 6f 74 2c  alid==0? If not,
139b1 0a 20 20 2a 2a 20 74 68 65 20 63 61 6c 6c 20 74  .  ** the call t
139b2 6f 20 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  o PagerPagecount
139b3 28 29 20 63 61 6e 20 62 65 20 72 65 6d 6f 76 65  () can be remove
139b4 64 2e 0a 20 20 2a 2f 0a 20 20 74 65 73 74 63 61  d..  */.  testca
139b5 73 65 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69  se( pPager->dbSi
139b6 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 20  zeValid==0 );.  
139b7 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
139b8 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30 29  count(pPager, 0)
139b9 3b 0a 0a 20 20 70 50 61 67 65 72 2d 3e 70 49 6e  ;..  pPager->pIn
139ba 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65  Journal = sqlite
139bb 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70 50  3BitvecCreate(pP
139bc 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20  ager->dbSize);. 
139bd 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70 49 6e   if( pPager->pIn
139be 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 7b 0a 20 20  Journal==0 ){.  
139bf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
139c0 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  NOMEM;.  }..  /*
139c1 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61   Open the journa
139c2 6c 20 66 69 6c 65 20 69 66 20 69 74 20 69 73 20  l file if it is 
139c3 6e 6f 74 20 61 6c 72 65 61 64 79 20 6f 70 65 6e  not already open
139c4 2e 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 4f 70  . */.  if( !isOp
139c5 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
139c6 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  ){.    if( pPage
139c7 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d  r->journalMode==
139c8 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
139c9 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20  E_MEMORY ){.    
139ca 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72    sqlite3MemJour
139cb 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e  nalOpen(pPager->
139cc 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  jfd);.    }else{
139cd 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74  .      const int
139ce 20 66 6c 61 67 73 20 3d 20 20 20 20 20 20 20 20   flags =        
139cf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 46             /* VF
139d0 53 20 66 6c 61 67 73 20 74 6f 20 6f 70 65 6e 20  S flags to open 
139d1 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a  journal file */.
139d2 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
139d3 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51  PEN_READWRITE|SQ
139d4 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
139d5 7c 0a 20 20 20 20 20 20 20 20 28 70 50 61 67 65  |.        (pPage
139d6 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3f 20 0a 20  r->tempFile ? . 
139d7 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
139d8 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
139d9 4f 53 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  OSE|SQLITE_OPEN_
139da 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 29 3a 0a 20  TEMP_JOURNAL):. 
139db 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
139dc 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e  _OPEN_MAIN_JOURN
139dd 41 4c 29 0a 20 20 20 20 20 20 20 20 29 3b 0a 23  AL).        );.#
139de 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
139df 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45  BLE_ATOMIC_WRITE
139e0 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
139e1 74 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a  te3JournalOpen(.
139e2 20 20 20 20 20 20 20 20 20 20 70 56 66 73 2c 20            pVfs, 
139e3 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
139e4 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66  , pPager->jfd, f
139e5 6c 61 67 73 2c 20 6a 72 6e 6c 42 75 66 66 65 72  lags, jrnlBuffer
139e6 53 69 7a 65 28 70 50 61 67 65 72 29 0a 20 20 20  Size(pPager).   
139e7 20 20 20 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20     );.#else.    
139e8 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
139e9 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65  Open(pVfs, pPage
139ea 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61  r->zJournal, pPa
139eb 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 2c  ger->jfd, flags,
139ec 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20   0);.#endif.    
139ed 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63  }.    assert( rc
139ee 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69  !=SQLITE_OK || i
139ef 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
139f0 64 29 20 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a  d) );.  }...  /*
139f1 20 57 72 69 74 65 20 74 68 65 20 66 69 72 73 74   Write the first
139f2 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
139f3 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
139f4 69 6c 65 20 61 6e 64 20 6f 70 65 6e 20 0a 20 20  ile and open .  
139f5 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e  ** the sub-journ
139f6 61 6c 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e  al if necessary.
139f7 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d  .  */.  if( rc==
139f8 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
139f9 20 2f 2a 20 54 4f 44 4f 3a 20 43 68 65 63 6b 20   /* TODO: Check 
139fa 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 73 65 20  if all of these 
139fb 61 72 65 20 72 65 61 6c 6c 79 20 72 65 71 75 69  are really requi
139fc 72 65 64 2e 20 2a 2f 0a 20 20 20 20 70 50 61 67  red. */.    pPag
139fd 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 3d  er->dbOrigSize =
139fe 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b   pPager->dbSize;
139ff 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75  .    pPager->jou
13a00 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b  rnalStarted = 0;
13a01 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65  .    pPager->nee
13a02 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70  dSync = 0;.    p
13a03 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b  Pager->nRec = 0;
13a04 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75  .    pPager->jou
13a05 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20  rnalOff = 0;.   
13a06 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74   pPager->setMast
13a07 65 72 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67  er = 0;.    pPag
13a08 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d  er->journalHdr =
13a09 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 77 72 69   0;.    rc = wri
13a0a 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61  teJournalHdr(pPa
13a0b 67 65 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ger);.  }.  if( 
13a0c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
13a0d 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f   pPager->nSavepo
13a0e 69 6e 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  int ){.    rc = 
13a0f 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 70  openSubJournal(p
13a10 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 69  Pager);.  }..  i
13a11 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
13a12 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   ){.    sqlite3B
13a13 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61  itvecDestroy(pPa
13a14 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29  ger->pInJournal)
13a15 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49  ;.    pPager->pI
13a16 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20  nJournal = 0;.  
13a17 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
13a18 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20  ../*.** Begin a 
13a19 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
13a1a 6e 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69  n on the specifi
13a1b 65 64 20 70 61 67 65 72 20 6f 62 6a 65 63 74 2e  ed pager object.
13a1c 20 49 66 20 61 20 0a 2a 2a 20 77 72 69 74 65 2d   If a .** write-
13a1d 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20  transaction has 
13a1e 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6f 70 65  already been ope
13a1f 6e 65 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69  ned, this functi
13a20 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  on is a no-op..*
13a21 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 46 6c  *.** If the exFl
13a22 61 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20 66  ag argument is f
13a23 61 6c 73 65 2c 20 74 68 65 6e 20 61 63 71 75 69  alse, then acqui
13a24 72 65 20 61 74 20 6c 65 61 73 74 20 61 20 52 45  re at least a RE
13a25 53 45 52 56 45 44 0a 2a 2a 20 6c 6f 63 6b 20 6f  SERVED.** lock o
13a26 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
13a27 69 6c 65 2e 20 49 66 20 65 78 46 6c 61 67 20 69  ile. If exFlag i
13a28 73 20 74 72 75 65 2c 20 74 68 65 6e 20 61 63 71  s true, then acq
13a29 75 69 72 65 20 61 74 20 6c 65 61 73 74 0a 2a 2a  uire at least.**
13a2a 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   an EXCLUSIVE lo
13a2b 63 6b 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f  ck. If such a lo
13a2c 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65  ck is already he
13a2d 6c 64 2c 20 6e 6f 20 6c 6f 63 6b 69 6e 67 20 0a  ld, no locking .
13a2e 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 6e 65 65  ** functions nee
13a2f 64 20 62 65 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a  d be called..**.
13a30 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f  ** If this is no
13a31 74 20 61 20 74 65 6d 70 6f 72 61 72 79 20 6f 72  t a temporary or
13a32 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20   in-memory file 
13a33 61 6e 64 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c  and, the journal
13a34 20 66 69 6c 65 20 69 73 20 0a 2a 2a 20 6f 70 65   file is .** ope
13a35 6e 65 64 20 69 66 20 69 74 20 68 61 73 20 6e 6f  ned if it has no
13a36 74 20 62 65 65 6e 20 61 6c 72 65 61 64 79 2e 20  t been already. 
13a37 46 6f 72 20 61 20 74 65 6d 70 6f 72 61 72 79 20  For a temporary 
13a38 66 69 6c 65 2c 20 74 68 65 20 6f 70 65 6e 69 6e  file, the openin
13a39 67 20 0a 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 75  g .** of the jou
13a3a 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 64 65 66  rnal file is def
13a3b 65 72 72 65 64 20 75 6e 74 69 6c 20 74 68 65 72  erred until ther
13a3c 65 20 69 73 20 61 6e 20 61 63 74 75 61 6c 20 6e  e is an actual n
13a3d 65 65 64 20 74 6f 20 0a 2a 2a 20 77 72 69 74 65  eed to .** write
13a3e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e   to the journal.
13a3f 20 54 4f 44 4f 3a 20 57 68 79 20 68 61 6e 64 6c   TODO: Why handl
13a40 65 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  e temporary file
13a41 73 20 64 69 66 66 65 72 65 6e 74 6c 79 3f 0a 2a  s differently?.*
13a42 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72  *.** If the jour
13a43 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e  nal file is open
13a44 65 64 20 28 6f 72 20 69 66 20 69 74 20 69 73 20  ed (or if it is 
13a45 61 6c 72 65 61 64 79 20 6f 70 65 6e 29 2c 20 74  already open), t
13a46 68 65 6e 20 61 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  hen a.** journal
13a47 2d 68 65 61 64 65 72 20 69 73 20 77 72 69 74 74  -header is writt
13a48 65 6e 20 74 6f 20 74 68 65 20 73 74 61 72 74 20  en to the start 
13a49 6f 66 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  of it..**.** If 
13a4a 74 68 65 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79  the subjInMemory
13a4b 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e   argument is non
13a4c 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 6e 79 20  -zero, then any 
13a4d 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 6f 70 65 6e  sub-journal open
13a4e 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 69  ed.** within thi
13a4f 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69  s transaction wi
13a50 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 61 73 20  ll be opened as 
13a51 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c  an in-memory fil
13a52 65 2e 20 54 68 69 73 0a 2a 2a 20 68 61 73 20 6e  e. This.** has n
13a53 6f 20 65 66 66 65 63 74 20 69 66 20 74 68 65 20  o effect if the 
13a54 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 61  sub-journal is a
13a55 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 20 28 61  lready opened (a
13a56 73 20 69 74 20 6d 61 79 20 62 65 20 77 68 65 6e  s it may be when
13a57 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65  .** running in e
13a58 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 29 20 6f  xclusive mode) o
13a59 72 20 69 66 20 74 68 65 20 74 72 61 6e 73 61 63  r if the transac
13a5a 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 72 65  tion does not re
13a5b 71 75 69 72 65 20 61 0a 2a 2a 20 73 75 62 2d 6a  quire a.** sub-j
13a5c 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68 65 20 73  ournal. If the s
13a5d 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61 72 67 75  ubjInMemory argu
13a5e 6d 65 6e 74 20 69 73 20 7a 65 72 6f 2c 20 74 68  ment is zero, th
13a5f 65 6e 20 61 6e 79 20 72 65 71 75 69 72 65 64 0a  en any required.
13a60 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69  ** sub-journal i
13a61 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e  s implemented in
13a62 2d 6d 65 6d 6f 72 79 20 69 66 20 70 50 61 67 65  -memory if pPage
13a63 72 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  r is an in-memor
13a64 79 20 64 61 74 61 62 61 73 65 2c 20 0a 2a 2a 20  y database, .** 
13a65 6f 72 20 75 73 69 6e 67 20 61 20 74 65 6d 70 6f  or using a tempo
13a66 72 61 72 79 20 66 69 6c 65 20 6f 74 68 65 72 77  rary file otherw
13a67 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ise..*/.SQLITE_P
13a68 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
13a69 65 33 50 61 67 65 72 42 65 67 69 6e 28 50 61 67  e3PagerBegin(Pag
13a6a 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
13a6b 65 78 46 6c 61 67 2c 20 69 6e 74 20 73 75 62 6a  exFlag, int subj
13a6c 49 6e 4d 65 6d 6f 72 79 29 7b 0a 20 20 69 6e 74  InMemory){.  int
13a6d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
13a6e 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
13a6f 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f  r->state!=PAGER_
13a70 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 70 50 61 67  UNLOCK );.  pPag
13a71 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79  er->subjInMemory
13a72 20 3d 20 28 75 38 29 73 75 62 6a 49 6e 4d 65 6d   = (u8)subjInMem
13a73 6f 72 79 3b 0a 20 20 69 66 28 20 70 50 61 67 65  ory;.  if( pPage
13a74 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f  r->state==PAGER_
13a75 53 48 41 52 45 44 20 29 7b 0a 20 20 20 20 61 73  SHARED ){.    as
13a76 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49  sert( pPager->pI
13a77 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20  nJournal==0 );. 
13a78 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44     assert( !MEMD
13a79 42 20 26 26 20 21 70 50 61 67 65 72 2d 3e 74 65  B && !pPager->te
13a7a 6d 70 46 69 6c 65 20 29 3b 0a 0a 20 20 20 20 2f  mpFile );..    /
13a7b 2a 20 4f 62 74 61 69 6e 20 61 20 52 45 53 45 52  * Obtain a RESER
13a7c 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  VED lock on the 
13a7d 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49  database file. I
13a7e 66 20 74 68 65 20 65 78 46 6c 61 67 20 70 61 72  f the exFlag par
13a7f 61 6d 65 74 65 72 0a 20 20 20 20 2a 2a 20 69 73  ameter.    ** is
13a80 20 74 72 75 65 2c 20 74 68 65 6e 20 69 6d 6d 65   true, then imme
13a81 64 69 61 74 65 6c 79 20 75 70 67 72 61 64 65 20  diately upgrade 
13a82 74 68 69 73 20 74 6f 20 61 6e 20 45 58 43 4c 55  this to an EXCLU
13a83 53 49 56 45 20 6c 6f 63 6b 2e 20 54 68 65 0a 20  SIVE lock. The. 
13a84 20 20 20 2a 2a 20 62 75 73 79 2d 68 61 6e 64 6c     ** busy-handl
13a85 65 72 20 63 61 6c 6c 62 61 63 6b 20 63 61 6e 20  er callback can 
13a86 62 65 20 75 73 65 64 20 77 68 65 6e 20 75 70 67  be used when upg
13a87 72 61 64 69 6e 67 20 74 6f 20 74 68 65 20 45 58  rading to the EX
13a88 43 4c 55 53 49 56 45 0a 20 20 20 20 2a 2a 20 6c  CLUSIVE.    ** l
13a89 6f 63 6b 2c 20 62 75 74 20 6e 6f 74 20 77 68 65  ock, but not whe
13a8a 6e 20 6f 62 74 61 69 6e 69 6e 67 20 74 68 65 20  n obtaining the 
13a8b 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e 0a 20  RESERVED lock.. 
13a8c 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73     */.    rc = s
13a8d 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61  qlite3OsLock(pPa
13a8e 67 65 72 2d 3e 66 64 2c 20 52 45 53 45 52 56 45  ger->fd, RESERVE
13a8f 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28  D_LOCK);.    if(
13a90 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
13a91 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  {.      pPager->
13a92 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 52 45  state = PAGER_RE
13a93 53 45 52 56 45 44 3b 0a 20 20 20 20 20 20 69 66  SERVED;.      if
13a94 28 20 65 78 46 6c 61 67 20 29 7b 0a 20 20 20 20  ( exFlag ){.    
13a95 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77      rc = pager_w
13a96 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67  ait_on_lock(pPag
13a97 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f  er, EXCLUSIVE_LO
13a98 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  CK);.      }.   
13a99 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   }..    /* If th
13a9a 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 73  e required locks
13a9b 20 77 65 72 65 20 73 75 63 63 65 73 73 66 75 6c   were successful
13a9c 6c 79 20 6f 62 74 61 69 6e 65 64 2c 20 6f 70 65  ly obtained, ope
13a9d 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20  n the journal.  
13a9e 20 20 2a 2a 20 66 69 6c 65 20 61 6e 64 20 77 72    ** file and wr
13a9f 69 74 65 20 74 68 65 20 66 69 72 73 74 20 6a 6f  ite the first jo
13aa0 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 74 6f 20  urnal-header to 
13aa1 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  it..    */.    i
13aa2 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
13aa3 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72   && pPager->jour
13aa4 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a  nalMode!=PAGER_J
13aa5 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29  OURNALMODE_OFF )
13aa6 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67  {.      rc = pag
13aa7 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28  er_open_journal(
13aa8 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20  pPager);.    }. 
13aa9 20 7d 65 6c 73 65 20 69 66 28 20 69 73 4f 70 65   }else if( isOpe
13aaa 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26  n(pPager->jfd) &
13aab 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  & pPager->journa
13aac 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  lOff==0 ){.    /
13aad 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77  * This happens w
13aae 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 77 61  hen the pager wa
13aaf 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 61  s in exclusive-a
13ab0 63 63 65 73 73 20 6d 6f 64 65 20 74 68 65 20 6c  ccess mode the l
13ab1 61 73 74 0a 20 20 20 20 2a 2a 20 74 69 6d 65 20  ast.    ** time 
13ab2 61 20 28 72 65 61 64 20 6f 72 20 77 72 69 74 65  a (read or write
13ab3 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61  ) transaction wa
13ab4 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63  s successfully c
13ab5 6f 6e 63 6c 75 64 65 64 0a 20 20 20 20 2a 2a 20  oncluded.    ** 
13ab6 62 79 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69  by this connecti
13ab7 6f 6e 2e 20 49 6e 73 74 65 61 64 20 6f 66 20 64  on. Instead of d
13ab8 65 6c 65 74 69 6e 67 20 74 68 65 20 6a 6f 75 72  eleting the jour
13ab9 6e 61 6c 20 66 69 6c 65 20 69 74 20 77 61 73 20  nal file it was 
13aba 0a 20 20 20 20 2a 2a 20 6b 65 70 74 20 6f 70 65  .    ** kept ope
13abb 6e 20 61 6e 64 20 65 69 74 68 65 72 20 77 61 73  n and either was
13abc 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 30 20   truncated to 0 
13abd 62 79 74 65 73 20 6f 72 20 69 74 73 20 68 65 61  bytes or its hea
13abe 64 65 72 20 77 61 73 0a 20 20 20 20 2a 2a 20 6f  der was.    ** o
13abf 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20  verwritten with 
13ac0 7a 65 72 6f 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  zeros..    */.  
13ac1 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
13ac2 2d 3e 6e 52 65 63 3d 3d 30 20 29 3b 0a 20 20 20  ->nRec==0 );.   
13ac3 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
13ac4 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d 30 20 29  >dbOrigSize==0 )
13ac5 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  ;.    assert( pP
13ac6 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
13ac7 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  ==0 );.    rc = 
13ac8 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e  pager_open_journ
13ac9 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a  al(pPager);.  }.
13aca 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22  .  PAGERTRACE(("
13acb 54 52 41 4e 53 41 43 54 49 4f 4e 20 25 64 5c 6e  TRANSACTION %d\n
13acc 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65  ", PAGERID(pPage
13acd 72 29 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r)));.  assert( 
13ace 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e  !isOpen(pPager->
13acf 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e  jfd) || pPager->
13ad0 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 20 7c 7c 20  journalOff>0 || 
13ad1 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc!=SQLITE_OK );
13ad2 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
13ad3 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65  E_OK ){.    asse
13ad4 72 74 28 20 21 70 50 61 67 65 72 2d 3e 64 62 4d  rt( !pPager->dbM
13ad5 6f 64 69 66 69 65 64 20 29 3b 0a 20 20 20 20 2f  odified );.    /
13ad6 2a 20 49 67 6e 6f 72 65 20 61 6e 79 20 49 4f 20  * Ignore any IO 
13ad7 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72  error that occur
13ad8 73 20 77 69 74 68 69 6e 20 70 61 67 65 72 5f 65  s within pager_e
13ad9 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29  nd_transaction()
13ada 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 70 75 72  . The.    ** pur
13adb 70 6f 73 65 20 6f 66 20 74 68 69 73 20 63 61 6c  pose of this cal
13adc 6c 20 69 73 20 74 6f 20 72 65 73 65 74 20 74 68  l is to reset th
13add 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65  e internal state
13ade 20 6f 66 20 74 68 65 20 70 61 67 65 72 0a 20 20   of the pager.  
13adf 20 20 2a 2a 20 73 75 62 2d 73 79 73 74 65 6d 2e    ** sub-system.
13ae0 20 49 74 20 64 6f 65 73 6e 27 74 20 6d 61 74 74   It doesn't matt
13ae1 65 72 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61  er if the journa
13ae2 6c 2d 66 69 6c 65 20 69 73 20 6e 6f 74 20 70 72  l-file is not pr
13ae3 6f 70 65 72 6c 79 0a 20 20 20 20 2a 2a 20 66 69  operly.    ** fi
13ae4 6e 61 6c 69 7a 65 64 20 61 74 20 74 68 69 73 20  nalized at this 
13ae5 70 6f 69 6e 74 20 28 73 69 6e 63 65 20 69 74 20  point (since it 
13ae6 69 73 20 6e 6f 74 20 61 20 76 61 6c 69 64 20 6a  is not a valid j
13ae7 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 79 77  ournal file anyw
13ae8 61 79 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ay)..    */.    
13ae9 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61  pager_end_transa
13aea 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 30 29  ction(pPager, 0)
13aeb 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
13aec 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b  c;.}../*.** Mark
13aed 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 20 70   a single data p
13aee 61 67 65 20 61 73 20 77 72 69 74 65 61 62 6c 65  age as writeable
13aef 2e 20 54 68 65 20 70 61 67 65 20 69 73 20 77 72  . The page is wr
13af0 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 0a  itten into the .
13af1 2a 2a 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  ** main journal 
13af2 6f 72 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 61  or sub-journal a
13af3 73 20 72 65 71 75 69 72 65 64 2e 20 49 66 20 74  s required. If t
13af4 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74  he page is writt
13af5 65 6e 20 69 6e 74 6f 0a 2a 2a 20 6f 6e 65 20 6f  en into.** one o
13af6 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 73 2c 20  f the journals, 
13af7 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
13af8 67 20 62 69 74 20 69 73 20 73 65 74 20 69 6e 20  g bit is set in 
13af9 74 68 65 20 0a 2a 2a 20 50 61 67 65 72 2e 70 49  the .** Pager.pI
13afa 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 76 65 63 20  nJournal bitvec 
13afb 61 6e 64 20 74 68 65 20 50 61 67 65 72 53 61 76  and the PagerSav
13afc 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f  epoint.pInSavepo
13afd 69 6e 74 20 62 69 74 76 65 63 73 0a 2a 2a 20 6f  int bitvecs.** o
13afe 66 20 61 6e 79 20 6f 70 65 6e 20 73 61 76 65 70  f any open savep
13aff 6f 69 6e 74 73 20 61 73 20 61 70 70 72 6f 70 72  oints as appropr
13b00 69 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  iate..*/.static 
13b01 69 6e 74 20 70 61 67 65 72 5f 77 72 69 74 65 28  int pager_write(
13b02 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 76  PgHdr *pPg){.  v
13b03 6f 69 64 20 2a 70 44 61 74 61 20 3d 20 70 50 67  oid *pData = pPg
13b04 2d 3e 70 44 61 74 61 3b 0a 20 20 50 61 67 65 72  ->pData;.  Pager
13b05 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e   *pPager = pPg->
13b06 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63  pPager;.  int rc
13b07 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
13b08 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65   /* This routine
13b09 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 20 75   is not called u
13b0a 6e 6c 65 73 73 20 61 20 74 72 61 6e 73 61 63 74  nless a transact
13b0b 69 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20  ion has already 
13b0c 62 65 65 6e 0a 20 20 2a 2a 20 73 74 61 72 74 65  been.  ** starte
13b0d 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  d..  */.  assert
13b0e 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
13b0f 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20  =PAGER_RESERVED 
13b10 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65  );..  /* If an e
13b11 72 72 6f 72 20 68 61 73 20 62 65 65 6e 20 70 72  rror has been pr
13b12 65 76 69 6f 75 73 6c 79 20 64 65 74 65 63 74 65  eviously detecte
13b13 64 2c 20 77 65 20 73 68 6f 75 6c 64 20 6e 6f 74  d, we should not
13b14 20 62 65 0a 20 20 2a 2a 20 63 61 6c 6c 69 6e 67   be.  ** calling
13b15 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20   this routine.  
13b16 52 65 70 65 61 74 20 74 68 65 20 65 72 72 6f 72  Repeat the error
13b17 20 66 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 2e   for robustness.
13b18 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45  .  */.  if( NEVE
13b19 52 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  R(pPager->errCod
13b1a 65 29 20 29 20 20 72 65 74 75 72 6e 20 70 50 61  e) )  return pPa
13b1b 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20  ger->errCode;.. 
13b1c 20 2f 2a 20 48 69 67 68 65 72 2d 6c 65 76 65 6c   /* Higher-level
13b1d 20 72 6f 75 74 69 6e 65 73 20 6e 65 76 65 72 20   routines never 
13b1e 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69  call this functi
13b1f 6f 6e 20 69 66 20 64 61 74 61 62 61 73 65 20 69  on if database i
13b20 73 20 6e 6f 74 0a 20 20 2a 2a 20 77 72 69 74 61  s not.  ** writa
13b21 62 6c 65 2e 20 20 42 75 74 20 63 68 65 63 6b 20  ble.  But check 
13b22 61 6e 79 77 61 79 2c 20 6a 75 73 74 20 66 6f 72  anyway, just for
13b23 20 72 6f 62 75 73 74 6e 65 73 73 2e 20 2a 2f 0a   robustness. */.
13b24 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67    if( NEVER(pPag
13b25 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29 20  er->readOnly) ) 
13b26 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45  return SQLITE_PE
13b27 52 4d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 21  RM;..  assert( !
13b28 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65  pPager->setMaste
13b29 72 20 29 3b 0a 0a 20 20 43 48 45 43 4b 5f 50 41  r );..  CHECK_PA
13b2a 47 45 28 70 50 67 29 3b 0a 0a 20 20 2f 2a 20 4d  GE(pPg);..  /* M
13b2b 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73 20  ark the page as 
13b2c 64 69 72 74 79 2e 20 20 49 66 20 74 68 65 20 70  dirty.  If the p
13b2d 61 67 65 20 68 61 73 20 61 6c 72 65 61 64 79 20  age has already 
13b2e 62 65 65 6e 20 77 72 69 74 74 65 6e 0a 20 20 2a  been written.  *
13b2f 2a 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  * to the journal
13b30 20 74 68 65 6e 20 77 65 20 63 61 6e 20 72 65 74   then we can ret
13b31 75 72 6e 20 72 69 67 68 74 20 61 77 61 79 2e 0a  urn right away..
13b32 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 63    */.  sqlite3Pc
13b33 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50  acheMakeDirty(pP
13b34 67 29 3b 0a 20 20 69 66 28 20 70 61 67 65 49 6e  g);.  if( pageIn
13b35 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 26 26 20  Journal(pPg) && 
13b36 21 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67  !subjRequiresPag
13b37 65 28 70 50 67 29 20 29 7b 0a 20 20 20 20 70 50  e(pPg) ){.    pP
13b38 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64  ager->dbModified
13b39 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a   = 1;.  }else{..
13b3a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 67 65 74      /* If we get
13b3b 20 74 68 69 73 20 66 61 72 2c 20 69 74 20 6d 65   this far, it me
13b3c 61 6e 73 20 74 68 61 74 20 74 68 65 20 70 61 67  ans that the pag
13b3d 65 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20  e needs to be.  
13b3e 20 20 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20    ** written to 
13b3f 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
13b40 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65 20 63  journal or the c
13b41 6b 65 63 6b 70 6f 69 6e 74 20 6a 6f 75 72 6e 61  keckpoint journa
13b42 6c 0a 20 20 20 20 2a 2a 20 6f 72 20 62 6f 74 68  l.    ** or both
13b43 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
13b44 48 69 67 68 65 72 20 6c 65 76 65 6c 20 72 6f 75  Higher level rou
13b45 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 68 61 76  tines should hav
13b46 65 20 61 6c 72 65 61 64 79 20 73 74 61 72 74 65  e already starte
13b47 64 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  d a transaction,
13b48 0a 20 20 20 20 2a 2a 20 77 68 69 63 68 20 6d 65  .    ** which me
13b49 61 6e 73 20 74 68 65 79 20 68 61 76 65 20 61 63  ans they have ac
13b4a 71 75 69 72 65 64 20 74 68 65 20 6e 65 63 65 73  quired the neces
13b4b 73 61 72 79 20 6c 6f 63 6b 73 20 61 6e 64 20 6f  sary locks and o
13b4c 70 65 6e 65 64 0a 20 20 20 20 2a 2a 20 61 20 72  pened.    ** a r
13b4d 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e  ollback journal.
13b4e 20 20 44 6f 75 62 6c 65 2d 63 68 65 63 6b 20 74    Double-check t
13b4f 6f 20 6d 61 6b 65 73 20 73 75 72 65 20 74 68 69  o makes sure thi
13b50 73 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20  s is the case.. 
13b51 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73     */.    rc = s
13b52 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e  qlite3PagerBegin
13b53 28 70 50 61 67 65 72 2c 20 30 2c 20 70 50 61 67  (pPager, 0, pPag
13b54 65 72 2d 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79  er->subjInMemory
13b55 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52  );.    if( NEVER
13b56 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20  (rc!=SQLITE_OK) 
13b57 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
13b58 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  rc;.    }.    if
13b59 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72  ( !isOpen(pPager
13b5a 2d 3e 6a 66 64 29 20 26 26 20 70 50 61 67 65 72  ->jfd) && pPager
13b5b 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50  ->journalMode!=P
13b5c 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
13b5d 5f 4f 46 46 20 29 7b 0a 20 20 20 20 20 20 61 73  _OFF ){.      as
13b5e 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 75 73  sert( pPager->us
13b5f 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 20 20  eJournal );.    
13b60 20 20 72 63 20 3d 20 70 61 67 65 72 5f 6f 70 65    rc = pager_ope
13b61 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72  n_journal(pPager
13b62 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
13b63 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
13b64 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
13b65 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69    pPager->dbModi
13b66 66 69 65 64 20 3d 20 31 3b 0a 20 20 0a 20 20 20  fied = 1;.  .   
13b67 20 2f 2a 20 54 68 65 20 74 72 61 6e 73 61 63 74   /* The transact
13b68 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20  ion journal now 
13b69 65 78 69 73 74 73 20 61 6e 64 20 77 65 20 68 61  exists and we ha
13b6a 76 65 20 61 20 52 45 53 45 52 56 45 44 20 6f 72  ve a RESERVED or
13b6b 20 61 6e 0a 20 20 20 20 2a 2a 20 45 58 43 4c 55   an.    ** EXCLU
13b6c 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  SIVE lock on the
13b6d 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
13b6e 69 6c 65 2e 20 20 57 72 69 74 65 20 74 68 65 20  ile.  Write the 
13b6f 63 75 72 72 65 6e 74 20 70 61 67 65 20 74 6f 0a  current page to.
13b70 20 20 20 20 2a 2a 20 74 68 65 20 74 72 61 6e 73      ** the trans
13b71 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69  action journal i
13b72 66 20 69 74 20 69 73 20 6e 6f 74 20 74 68 65 72  f it is not ther
13b73 65 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a  e already..    *
13b74 2f 0a 20 20 20 20 69 66 28 20 21 70 61 67 65 49  /.    if( !pageI
13b75 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 26 26  nJournal(pPg) &&
13b76 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
13b77 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 69 66  jfd) ){.      if
13b78 28 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61  ( pPg->pgno<=pPa
13b79 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20  ger->dbOrigSize 
13b7a 29 7b 0a 20 20 20 20 20 20 20 20 75 33 32 20 63  ){.        u32 c
13b7b 6b 73 75 6d 3b 0a 20 20 20 20 20 20 20 20 63 68  ksum;.        ch
13b7c 61 72 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20  ar *pData2;..   
13b7d 20 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c       /* We shoul
13b7e 64 20 6e 65 76 65 72 20 77 72 69 74 65 20 74 6f  d never write to
13b7f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
13b80 65 20 74 68 65 20 70 61 67 65 20 74 68 61 74 0a  e the page that.
13b81 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61          ** conta
13b82 69 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65  ins the database
13b83 20 6c 6f 63 6b 73 2e 20 20 54 68 65 20 66 6f 6c   locks.  The fol
13b84 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 20 76 65  lowing assert ve
13b85 72 69 66 69 65 73 0a 20 20 20 20 20 20 20 20 2a  rifies.        *
13b86 2a 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74  * that we do not
13b87 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73  . */.        ass
13b88 65 72 74 28 20 70 50 67 2d 3e 70 67 6e 6f 21 3d  ert( pPg->pgno!=
13b89 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50  PAGER_MJ_PGNO(pP
13b8a 61 67 65 72 29 20 29 3b 0a 20 20 20 20 20 20 20  ager) );.       
13b8b 20 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20   CODEC2(pPager, 
13b8c 70 44 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f  pData, pPg->pgno
13b8d 2c 20 37 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  , 7, return SQLI
13b8e 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 32  TE_NOMEM, pData2
13b8f 29 3b 0a 20 20 20 20 20 20 20 20 63 6b 73 75 6d  );.        cksum
13b90 20 3d 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 70   = pager_cksum(p
13b91 50 61 67 65 72 2c 20 28 75 38 2a 29 70 44 61 74  Pager, (u8*)pDat
13b92 61 32 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  a2);.        rc 
13b93 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70 50  = write32bits(pP
13b94 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65  ager->jfd, pPage
13b95 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70  r->journalOff, p
13b96 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20  Pg->pgno);.     
13b97 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
13b98 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
13b99 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
13b9a 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66  Write(pPager->jf
13b9b 64 2c 20 70 44 61 74 61 32 2c 20 70 50 61 67 65  d, pData2, pPage
13b9c 72 2d 3e 70 61 67 65 53 69 7a 65 2c 0a 20 20 20  r->pageSize,.   
13b9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13b9e 20 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65             pPage
13b9f 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 20  r->journalOff + 
13ba0 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50  4);.          pP
13ba1 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
13ba2 20 2b 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65   += pPager->page
13ba3 53 69 7a 65 2b 34 3b 0a 20 20 20 20 20 20 20 20  Size+4;.        
13ba4 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  }.        if( rc
13ba5 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
13ba6 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 77 72           rc = wr
13ba7 69 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72  ite32bits(pPager
13ba8 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a  ->jfd, pPager->j
13ba9 6f 75 72 6e 61 6c 4f 66 66 2c 20 63 6b 73 75 6d  ournalOff, cksum
13baa 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61  );.          pPa
13bab 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
13bac 2b 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a  += 4;.        }.
13bad 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28          IOTRACE(
13bae 28 22 4a 4f 55 54 20 25 70 20 25 64 20 25 6c 6c  ("JOUT %p %d %ll
13baf 64 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c  d %d\n", pPager,
13bb0 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20   pPg->pgno, .   
13bb1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50                pP
13bb2 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
13bb3 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
13bb4 7a 65 29 29 3b 0a 20 20 20 20 20 20 20 20 50 41  ze));.        PA
13bb5 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 33  GER_INCR(sqlite3
13bb6 5f 70 61 67 65 72 5f 77 72 69 74 65 6a 5f 63 6f  _pager_writej_co
13bb7 75 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 50 41  unt);.        PA
13bb8 47 45 52 54 52 41 43 45 28 28 22 4a 4f 55 52 4e  GERTRACE(("JOURN
13bb9 41 4c 20 25 64 20 70 61 67 65 20 25 64 20 6e 65  AL %d page %d ne
13bba 65 64 53 79 6e 63 3d 25 64 20 68 61 73 68 28 25  edSync=%d hash(%
13bbb 30 38 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20  08x)\n",.       
13bbc 20 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50        PAGERID(pP
13bbd 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f  ager), pPg->pgno
13bbe 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
13bbf 28 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48  ((pPg->flags&PGH
13bc0 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31 3a  DR_NEED_SYNC)?1:
13bc1 30 29 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61  0), pager_pageha
13bc2 73 68 28 70 50 67 29 29 29 3b 0a 0a 20 20 20 20  sh(pPg)));..    
13bc3 20 20 20 20 2f 2a 20 45 76 65 6e 20 69 66 20 61      /* Even if a
13bc4 6e 20 49 4f 20 6f 72 20 64 69 73 6b 66 75 6c 6c  n IO or diskfull
13bc5 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20   error occurred 
13bc6 77 68 69 6c 65 20 6a 6f 75 72 6e 61 6c 6c 69 6e  while journallin
13bc7 67 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  g the.        **
13bc8 20 70 61 67 65 20 69 6e 20 74 68 65 20 62 6c 6f   page in the blo
13bc9 63 6b 20 61 62 6f 76 65 2c 20 73 65 74 20 74 68  ck above, set th
13bca 65 20 6e 65 65 64 2d 73 79 6e 63 20 66 6c 61 67  e need-sync flag
13bcb 20 66 6f 72 20 74 68 65 20 70 61 67 65 2e 0a 20   for the page.. 
13bcc 20 20 20 20 20 20 20 2a 2a 20 4f 74 68 65 72 77         ** Otherw
13bcd 69 73 65 2c 20 77 68 65 6e 20 74 68 65 20 74 72  ise, when the tr
13bce 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c  ansaction is rol
13bcf 6c 65 64 20 62 61 63 6b 2c 20 74 68 65 20 6c 6f  led back, the lo
13bd0 67 69 63 20 69 6e 0a 20 20 20 20 20 20 20 20 2a  gic in.        *
13bd1 2a 20 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70  * playback_one_p
13bd2 61 67 65 28 29 20 77 69 6c 6c 20 74 68 69 6e 6b  age() will think
13bd3 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 6e   that the page n
13bd4 65 65 64 73 20 74 6f 20 62 65 20 72 65 73 74 6f  eeds to be resto
13bd5 72 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  red.        ** i
13bd6 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
13bd7 69 6c 65 2e 20 41 6e 64 20 69 66 20 61 6e 20 49  ile. And if an I
13bd8 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77  O error occurs w
13bd9 68 69 6c 65 20 64 6f 69 6e 67 20 73 6f 2c 0a 20  hile doing so,. 
13bda 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 63         ** then c
13bdb 6f 72 72 75 70 74 69 6f 6e 20 6d 61 79 20 66 6f  orruption may fo
13bdc 6c 6c 6f 77 2e 0a 20 20 20 20 20 20 20 20 2a 2f  llow..        */
13bdd 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 50  .        if( !pP
13bde 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a  ager->noSync ){.
13bdf 20 20 20 20 20 20 20 20 20 20 70 50 67 2d 3e 66            pPg->f
13be0 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45  lags |= PGHDR_NE
13be1 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20 20  ED_SYNC;.       
13be2 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53     pPager->needS
13be3 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ync = 1;.       
13be4 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 41   }..        /* A
13be5 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75  n error has occu
13be6 72 72 65 64 20 77 72 69 74 69 6e 67 20 74 6f 20  rred writing to 
13be7 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
13be8 2e 20 54 68 65 20 0a 20 20 20 20 20 20 20 20 2a  . The .        *
13be9 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69  * transaction wi
13bea 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63  ll be rolled bac
13beb 6b 20 62 79 20 74 68 65 20 6c 61 79 65 72 20 61  k by the layer a
13bec 62 6f 76 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f  bove..        */
13bed 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
13bee 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
13bef 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
13bf0 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  c;.        }..  
13bf1 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52        pPager->nR
13bf2 65 63 2b 2b 3b 0a 20 20 20 20 20 20 20 20 61 73  ec++;.        as
13bf3 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49  sert( pPager->pI
13bf4 6e 4a 6f 75 72 6e 61 6c 21 3d 30 20 29 3b 0a 20  nJournal!=0 );. 
13bf5 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
13bf6 74 65 33 42 69 74 76 65 63 53 65 74 28 70 50 61  te3BitvecSet(pPa
13bf7 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c  ger->pInJournal,
13bf8 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20   pPg->pgno);.   
13bf9 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
13bfa 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
13bfb 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
13bfc 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
13bfd 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e   || rc==SQLITE_N
13bfe 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20  OMEM );.        
13bff 72 63 20 7c 3d 20 61 64 64 54 6f 53 61 76 65 70  rc |= addToSavep
13c00 6f 69 6e 74 42 69 74 76 65 63 73 28 70 50 61 67  ointBitvecs(pPag
13c01 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a  er, pPg->pgno);.
13c02 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
13c03 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13c04 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72         assert( r
13c05 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
13c06 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  );.          ret
13c07 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20  urn rc;.        
13c08 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
13c09 20 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67         if( !pPag
13c0a 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74  er->journalStart
13c0b 65 64 20 26 26 20 21 70 50 61 67 65 72 2d 3e 6e  ed && !pPager->n
13c0c 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 20  oSync ){.       
13c0d 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d     pPg->flags |=
13c0e 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43   PGHDR_NEED_SYNC
13c0f 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67  ;.          pPag
13c10 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 31  er->needSync = 1
13c11 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
13c12 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28      PAGERTRACE((
13c13 22 41 50 50 45 4e 44 20 25 64 20 70 61 67 65 20  "APPEND %d page 
13c14 25 64 20 6e 65 65 64 53 79 6e 63 3d 25 64 5c 6e  %d needSync=%d\n
13c15 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
13c16 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65     PAGERID(pPage
13c17 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 0a 20  r), pPg->pgno,. 
13c18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 28                ((
13c19 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  pPg->flags&PGHDR
13c1a 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 29  _NEED_SYNC)?1:0)
13c1b 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ));.      }.    
13c1c 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74  }.  .    /* If t
13c1d 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75  he statement jou
13c1e 72 6e 61 6c 20 69 73 20 6f 70 65 6e 20 61 6e 64  rnal is open and
13c1f 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
13c20 20 69 6e 20 69 74 2c 0a 20 20 20 20 2a 2a 20 74   in it,.    ** t
13c21 68 65 6e 20 77 72 69 74 65 20 74 68 65 20 63 75  hen write the cu
13c22 72 72 65 6e 74 20 70 61 67 65 20 74 6f 20 74 68  rrent page to th
13c23 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72  e statement jour
13c24 6e 61 6c 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a  nal.  Note that.
13c25 20 20 20 20 2a 2a 20 74 68 65 20 73 74 61 74 65      ** the state
13c26 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 6f 72  ment journal for
13c27 6d 61 74 20 64 69 66 66 65 72 73 20 66 72 6f 6d  mat differs from
13c28 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 6a 6f   the standard jo
13c29 75 72 6e 61 6c 20 66 6f 72 6d 61 74 0a 20 20 20  urnal format.   
13c2a 20 2a 2a 20 69 6e 20 74 68 61 74 20 69 74 20 6f   ** in that it o
13c2b 6d 69 74 73 20 74 68 65 20 63 68 65 63 6b 73 75  mits the checksu
13c2c 6d 73 20 61 6e 64 20 74 68 65 20 68 65 61 64 65  ms and the heade
13c2d 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  r..    */.    if
13c2e 28 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61  ( subjRequiresPa
13c2f 67 65 28 70 50 67 29 20 29 7b 0a 20 20 20 20 20  ge(pPg) ){.     
13c30 20 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c   rc = subjournal
13c31 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 20 7d  Page(pPg);.    }
13c32 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74  .  }..  /* Updat
13c33 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  e the database s
13c34 69 7a 65 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a  ize and return..
13c35 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
13c36 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41  Pager->state>=PA
13c37 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 20  GER_SHARED );.  
13c38 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69  if( pPager->dbSi
13c39 7a 65 3c 70 50 67 2d 3e 70 67 6e 6f 20 29 7b 0a  ze<pPg->pgno ){.
13c3a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69      pPager->dbSi
13c3b 7a 65 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a  ze = pPg->pgno;.
13c3c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
13c3d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61  .}../*.** Mark a
13c3e 20 64 61 74 61 20 70 61 67 65 20 61 73 20 77 72   data page as wr
13c3f 69 74 65 61 62 6c 65 2e 20 54 68 69 73 20 72 6f  iteable. This ro
13c40 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61  utine must be ca
13c41 6c 6c 65 64 20 62 65 66 6f 72 65 20 0a 2a 2a 20  lled before .** 
13c42 6d 61 6b 69 6e 67 20 63 68 61 6e 67 65 73 20 74  making changes t
13c43 6f 20 61 20 70 61 67 65 2e 20 54 68 65 20 63 61  o a page. The ca
13c44 6c 6c 65 72 20 6d 75 73 74 20 63 68 65 63 6b 20  ller must check 
13c45 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
13c46 20 0a 2a 2a 20 6f 66 20 74 68 69 73 20 66 75 6e   .** of this fun
13c47 63 74 69 6f 6e 20 61 6e 64 20 62 65 20 63 61 72  ction and be car
13c48 65 66 75 6c 20 6e 6f 74 20 74 6f 20 63 68 61 6e  eful not to chan
13c49 67 65 20 61 6e 79 20 70 61 67 65 20 64 61 74 61  ge any page data
13c4a 20 75 6e 6c 65 73 73 20 0a 2a 2a 20 74 68 69 73   unless .** this
13c4b 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
13c4c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a   SQLITE_OK..**.*
13c4d 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65  * The difference
13c4e 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 66 75   between this fu
13c4f 6e 63 74 69 6f 6e 20 61 6e 64 20 70 61 67 65 72  nction and pager
13c50 5f 77 72 69 74 65 28 29 20 69 73 20 74 68 61 74  _write() is that
13c51 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f   this.** functio
13c52 6e 20 61 6c 73 6f 20 64 65 61 6c 73 20 77 69 74  n also deals wit
13c53 68 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 61  h the special ca
13c54 73 65 20 77 68 65 72 65 20 32 20 6f 72 20 6d 6f  se where 2 or mo
13c55 72 65 20 70 61 67 65 73 0a 2a 2a 20 66 69 74 20  re pages.** fit 
13c56 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 69 73 6b  on a single disk
13c57 20 73 65 63 74 6f 72 2e 20 49 6e 20 74 68 69 73   sector. In this
13c58 20 63 61 73 65 20 61 6c 6c 20 63 6f 2d 72 65 73   case all co-res
13c59 69 64 65 6e 74 20 70 61 67 65 73 0a 2a 2a 20 6d  ident pages.** m
13c5a 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 77 72  ust have been wr
13c5b 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75  itten to the jou
13c5c 72 6e 61 6c 20 66 69 6c 65 20 62 65 66 6f 72 65  rnal file before
13c5d 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a   returning..**.*
13c5e 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
13c5f 63 75 72 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d  curs, SQLITE_NOM
13c60 45 4d 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f  EM or an IO erro
13c61 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
13c62 65 64 0a 2a 2a 20 61 73 20 61 70 70 72 6f 70 72  ed.** as appropr
13c63 69 61 74 65 2e 20 4f 74 68 65 72 77 69 73 65 2c  iate. Otherwise,
13c64 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53   SQLITE_OK..*/.S
13c65 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
13c66 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  t sqlite3PagerWr
13c67 69 74 65 28 44 62 50 61 67 65 20 2a 70 44 62 50  ite(DbPage *pDbP
13c68 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  age){.  int rc =
13c69 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 50   SQLITE_OK;..  P
13c6a 67 48 64 72 20 2a 70 50 67 20 3d 20 70 44 62 50  gHdr *pPg = pDbP
13c6b 61 67 65 3b 0a 20 20 50 61 67 65 72 20 2a 70 50  age;.  Pager *pP
13c6c 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67  ager = pPg->pPag
13c6d 65 72 3b 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65  er;.  Pgno nPage
13c6e 50 65 72 53 65 63 74 6f 72 20 3d 20 28 70 50 61  PerSector = (pPa
13c6f 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 2f  ger->sectorSize/
13c70 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
13c71 29 3b 0a 0a 20 20 69 66 28 20 6e 50 61 67 65 50  );..  if( nPageP
13c72 65 72 53 65 63 74 6f 72 3e 31 20 29 7b 0a 20 20  erSector>1 ){.  
13c73 20 20 50 67 6e 6f 20 6e 50 61 67 65 43 6f 75 6e    Pgno nPageCoun
13c74 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  t;          /* T
13c75 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70  otal number of p
13c76 61 67 65 73 20 69 6e 20 64 61 74 61 62 61 73 65  ages in database
13c77 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 50 67 6e   file */.    Pgn
13c78 6f 20 70 67 31 3b 20 20 20 20 20 20 20 20 20 20  o pg1;          
13c79 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
13c7a 70 61 67 65 20 6f 66 20 74 68 65 20 73 65 63 74  page of the sect
13c7b 6f 72 20 70 50 67 20 69 73 20 6c 6f 63 61 74 65  or pPg is locate
13c7c 64 20 6f 6e 2e 20 2a 2f 0a 20 20 20 20 69 6e 74  d on. */.    int
13c7d 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20   nPage;         
13c7e 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
13c7f 20 6f 66 20 70 61 67 65 73 20 73 74 61 72 74 69   of pages starti
13c80 6e 67 20 61 74 20 70 67 31 20 74 6f 20 6a 6f 75  ng at pg1 to jou
13c81 72 6e 61 6c 20 2a 2f 0a 20 20 20 20 69 6e 74 20  rnal */.    int 
13c82 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ii;             
13c83 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
13c84 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74  unter */.    int
13c85 20 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 20 20   needSync = 0;  
13c86 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
13c87 66 20 61 6e 79 20 70 61 67 65 20 68 61 73 20 50  f any page has P
13c88 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 2a  GHDR_NEED_SYNC *
13c89 2f 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68  /..    /* Set th
13c8a 65 20 64 6f 4e 6f 74 53 79 6e 63 20 66 6c 61 67  e doNotSync flag
13c8b 20 74 6f 20 31 2e 20 54 68 69 73 20 69 73 20 62   to 1. This is b
13c8c 65 63 61 75 73 65 20 77 65 20 63 61 6e 6e 6f 74  ecause we cannot
13c8d 20 61 6c 6c 6f 77 20 61 20 6a 6f 75 72 6e 61 6c   allow a journal
13c8e 0a 20 20 20 20 2a 2a 20 68 65 61 64 65 72 20 74  .    ** header t
13c8f 6f 20 62 65 20 77 72 69 74 74 65 6e 20 62 65 74  o be written bet
13c90 77 65 65 6e 20 74 68 65 20 70 61 67 65 73 20 6a  ween the pages j
13c91 6f 75 72 6e 61 6c 65 64 20 62 79 20 74 68 69 73  ournaled by this
13c92 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 2a   function..    *
13c93 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d  /.    assert( !M
13c94 45 4d 44 42 20 29 3b 0a 20 20 20 20 61 73 73 65  EMDB );.    asse
13c95 72 74 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f  rt( pPager->doNo
13c96 74 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20  tSync==0 );.    
13c97 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e  pPager->doNotSyn
13c98 63 20 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a 20 54  c = 1;..    /* T
13c99 68 69 73 20 74 72 69 63 6b 20 61 73 73 75 6d 65  his trick assume
13c9a 73 20 74 68 61 74 20 62 6f 74 68 20 74 68 65 20  s that both the 
13c9b 70 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 65  page-size and se
13c9c 63 74 6f 72 2d 73 69 7a 65 20 61 72 65 0a 20 20  ctor-size are.  
13c9d 20 20 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 20    ** an integer 
13c9e 70 6f 77 65 72 20 6f 66 20 32 2e 20 49 74 20 73  power of 2. It s
13c9f 65 74 73 20 76 61 72 69 61 62 6c 65 20 70 67 31  ets variable pg1
13ca0 20 74 6f 20 74 68 65 20 69 64 65 6e 74 69 66 69   to the identifi
13ca1 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65  er.    ** of the
13ca2 20 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74   first page of t
13ca3 68 65 20 73 65 63 74 6f 72 20 70 50 67 20 69 73  he sector pPg is
13ca4 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 0a 20 20 20   located on..   
13ca5 20 2a 2f 0a 20 20 20 20 70 67 31 20 3d 20 28 28   */.    pg1 = ((
13ca6 70 50 67 2d 3e 70 67 6e 6f 2d 31 29 20 26 20 7e  pPg->pgno-1) & ~
13ca7 28 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 2d  (nPagePerSector-
13ca8 31 29 29 20 2b 20 31 3b 0a 0a 20 20 20 20 73 71  1)) + 1;..    sq
13ca9 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f  lite3PagerPageco
13caa 75 6e 74 28 70 50 61 67 65 72 2c 20 28 69 6e 74  unt(pPager, (int
13cab 20 2a 29 26 6e 50 61 67 65 43 6f 75 6e 74 29 3b   *)&nPageCount);
13cac 0a 20 20 20 20 69 66 28 20 70 50 67 2d 3e 70 67  .    if( pPg->pg
13cad 6e 6f 3e 6e 50 61 67 65 43 6f 75 6e 74 20 29 7b  no>nPageCount ){
13cae 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 28  .      nPage = (
13caf 70 50 67 2d 3e 70 67 6e 6f 20 2d 20 70 67 31 29  pPg->pgno - pg1)
13cb0 2b 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  +1;.    }else if
13cb1 28 20 28 70 67 31 2b 6e 50 61 67 65 50 65 72 53  ( (pg1+nPagePerS
13cb2 65 63 74 6f 72 2d 31 29 3e 6e 50 61 67 65 43 6f  ector-1)>nPageCo
13cb3 75 6e 74 20 29 7b 0a 20 20 20 20 20 20 6e 50 61  unt ){.      nPa
13cb4 67 65 20 3d 20 6e 50 61 67 65 43 6f 75 6e 74 2b  ge = nPageCount+
13cb5 31 2d 70 67 31 3b 0a 20 20 20 20 7d 65 6c 73 65  1-pg1;.    }else
13cb6 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20  {.      nPage = 
13cb7 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 3b 0a  nPagePerSector;.
13cb8 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
13cb9 28 6e 50 61 67 65 3e 30 29 3b 0a 20 20 20 20 61  (nPage>0);.    a
13cba 73 73 65 72 74 28 70 67 31 3c 3d 70 50 67 2d 3e  ssert(pg1<=pPg->
13cbb 70 67 6e 6f 29 3b 0a 20 20 20 20 61 73 73 65 72  pgno);.    asser
13cbc 74 28 28 70 67 31 2b 6e 50 61 67 65 29 3e 70 50  t((pg1+nPage)>pP
13cbd 67 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20 20 66  g->pgno);..    f
13cbe 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67  or(ii=0; ii<nPag
13cbf 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f  e && rc==SQLITE_
13cc0 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20  OK; ii++){.     
13cc1 20 50 67 6e 6f 20 70 67 20 3d 20 70 67 31 2b 69   Pgno pg = pg1+i
13cc2 69 3b 0a 20 20 20 20 20 20 50 67 48 64 72 20 2a  i;.      PgHdr *
13cc3 70 50 61 67 65 3b 0a 20 20 20 20 20 20 69 66 28  pPage;.      if(
13cc4 20 70 67 3d 3d 70 50 67 2d 3e 70 67 6e 6f 20 7c   pg==pPg->pgno |
13cc5 7c 20 21 73 71 6c 69 74 65 33 42 69 74 76 65 63  | !sqlite3Bitvec
13cc6 54 65 73 74 28 70 50 61 67 65 72 2d 3e 70 49 6e  Test(pPager->pIn
13cc7 4a 6f 75 72 6e 61 6c 2c 20 70 67 29 20 29 7b 0a  Journal, pg) ){.
13cc8 20 20 20 20 20 20 20 20 69 66 28 20 70 67 21 3d          if( pg!=
13cc9 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50  PAGER_MJ_PGNO(pP
13cca 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 20  ager) ){.       
13ccb 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
13ccc 61 67 65 72 47 65 74 28 70 50 61 67 65 72 2c 20  agerGet(pPager, 
13ccd 70 67 2c 20 26 70 50 61 67 65 29 3b 0a 20 20 20  pg, &pPage);.   
13cce 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
13ccf 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
13cd0 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67          rc = pag
13cd1 65 72 5f 77 72 69 74 65 28 70 50 61 67 65 29 3b  er_write(pPage);
13cd2 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
13cd3 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47   pPage->flags&PG
13cd4 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b  HDR_NEED_SYNC ){
13cd5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e  .              n
13cd6 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20  eedSync = 1;.   
13cd7 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72             asser
13cd8 74 28 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79  t(pPager->needSy
13cd9 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nc);.           
13cda 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73   }.            s
13cdb 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
13cdc 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  (pPage);.       
13cdd 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
13cde 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28       }else if( (
13cdf 70 50 61 67 65 20 3d 20 70 61 67 65 72 5f 6c 6f  pPage = pager_lo
13ce0 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 29  okup(pPager, pg)
13ce1 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
13ce2 69 66 28 20 70 50 61 67 65 2d 3e 66 6c 61 67 73  if( pPage->flags
13ce3 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
13ce4 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65   ){.          ne
13ce5 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20  edSync = 1;.    
13ce6 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71      }.        sq
13ce7 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
13ce8 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a  pPage);.      }.
13ce9 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
13cea 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f   the PGHDR_NEED_
13ceb 53 59 4e 43 20 66 6c 61 67 20 69 73 20 73 65 74  SYNC flag is set
13cec 20 66 6f 72 20 61 6e 79 20 6f 66 20 74 68 65 20   for any of the 
13ced 6e 50 61 67 65 20 70 61 67 65 73 20 0a 20 20 20  nPage pages .   
13cee 20 2a 2a 20 73 74 61 72 74 69 6e 67 20 61 74 20   ** starting at 
13cef 70 67 31 2c 20 74 68 65 6e 20 69 74 20 6e 65 65  pg1, then it nee
13cf0 64 73 20 74 6f 20 62 65 20 73 65 74 20 66 6f 72  ds to be set for
13cf1 20 61 6c 6c 20 6f 66 20 74 68 65 6d 2e 20 42 65   all of them. Be
13cf2 63 61 75 73 65 0a 20 20 20 20 2a 2a 20 77 72 69  cause.    ** wri
13cf3 74 69 6e 67 20 74 6f 20 61 6e 79 20 6f 66 20 74  ting to any of t
13cf4 68 65 73 65 20 6e 50 61 67 65 20 70 61 67 65 73  hese nPage pages
13cf5 20 6d 61 79 20 64 61 6d 61 67 65 20 74 68 65 20   may damage the 
13cf6 6f 74 68 65 72 73 2c 20 74 68 65 0a 20 20 20 20  others, the.    
13cf7 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ** journal file 
13cf8 6d 75 73 74 20 63 6f 6e 74 61 69 6e 20 73 79 6e  must contain syn
13cf9 63 28 29 65 64 20 63 6f 70 69 65 73 20 6f 66 20  c()ed copies of 
13cfa 61 6c 6c 20 6f 66 20 74 68 65 6d 0a 20 20 20 20  all of them.    
13cfb 2a 2a 20 62 65 66 6f 72 65 20 61 6e 79 20 6f 66  ** before any of
13cfc 20 74 68 65 6d 20 63 61 6e 20 62 65 20 77 72 69   them can be wri
13cfd 74 74 65 6e 20 6f 75 74 20 74 6f 20 74 68 65 20  tten out to the 
13cfe 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20  database file.. 
13cff 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63     */.    if( rc
13d00 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e  ==SQLITE_OK && n
13d01 65 65 64 53 79 6e 63 20 29 7b 0a 20 20 20 20 20  eedSync ){.     
13d02 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20   assert( !MEMDB 
13d03 26 26 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  && pPager->noSyn
13d04 63 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 66 6f  c==0 );.      fo
13d05 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67 65  r(ii=0; ii<nPage
13d06 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  ; ii++){.       
13d07 20 50 67 48 64 72 20 2a 70 50 61 67 65 20 3d 20   PgHdr *pPage = 
13d08 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61  pager_lookup(pPa
13d09 67 65 72 2c 20 70 67 31 2b 69 69 29 3b 0a 20 20  ger, pg1+ii);.  
13d0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 20        if( pPage 
13d0b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61  ){.          pPa
13d0c 67 65 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48  ge->flags |= PGH
13d0d 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20  DR_NEED_SYNC;.  
13d0e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
13d0f 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 29  agerUnref(pPage)
13d10 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
13d11 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74    }.      assert
13d12 28 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e  (pPager->needSyn
13d13 63 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61  c);.    }..    a
13d14 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64  ssert( pPager->d
13d15 6f 4e 6f 74 53 79 6e 63 3d 3d 31 20 29 3b 0a 20  oNotSync==1 );. 
13d16 20 20 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74     pPager->doNot
13d17 53 79 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  Sync = 0;.  }els
13d18 65 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65  e{.    rc = page
13d19 72 5f 77 72 69 74 65 28 70 44 62 50 61 67 65 29  r_write(pDbPage)
13d1a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
13d1b 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
13d1c 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 70  rn TRUE if the p
13d1d 61 67 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65  age given in the
13d1e 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20 70 72   argument was pr
13d1f 65 76 69 6f 75 73 6c 79 20 70 61 73 73 65 64 0a  eviously passed.
13d20 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67  ** to sqlite3Pag
13d21 65 72 57 72 69 74 65 28 29 2e 20 20 49 6e 20 6f  erWrite().  In o
13d22 74 68 65 72 20 77 6f 72 64 73 2c 20 72 65 74 75  ther words, retu
13d23 72 6e 20 54 52 55 45 20 69 66 20 69 74 20 69 73  rn TRUE if it is
13d24 20 6f 6b 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 65   ok.** to change
13d25 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
13d26 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66  the page..*/.#if
13d27 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49  ndef NDEBUG.SQLI
13d28 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
13d29 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
13d2a 74 65 61 62 6c 65 28 44 62 50 61 67 65 20 2a 70  teable(DbPage *p
13d2b 50 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50  Pg){.  return pP
13d2c 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44  g->flags&PGHDR_D
13d2d 49 52 54 59 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  IRTY;.}.#endif..
13d2e 2f 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20  /*.** A call to 
13d2f 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 65 6c  this routine tel
13d30 6c 73 20 74 68 65 20 70 61 67 65 72 20 74 68 61  ls the pager tha
13d31 74 20 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65  t it is not nece
13d32 73 73 61 72 79 20 74 6f 0a 2a 2a 20 77 72 69 74  ssary to.** writ
13d33 65 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f  e the informatio
13d34 6e 20 6f 6e 20 70 61 67 65 20 70 50 67 20 62 61  n on page pPg ba
13d35 63 6b 20 74 6f 20 74 68 65 20 64 69 73 6b 2c 20  ck to the disk, 
13d36 65 76 65 6e 20 74 68 6f 75 67 68 0a 2a 2a 20 74  even though.** t
13d37 68 61 74 20 70 61 67 65 20 6d 69 67 68 74 20 62  hat page might b
13d38 65 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74  e marked as dirt
13d39 79 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e 73  y.  This happens
13d3a 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77  , for example, w
13d3b 68 65 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 20  hen.** the page 
13d3c 68 61 73 20 62 65 65 6e 20 61 64 64 65 64 20 61  has been added a
13d3d 73 20 61 20 6c 65 61 66 20 6f 66 20 74 68 65 20  s a leaf of the 
13d3e 66 72 65 65 6c 69 73 74 20 61 6e 64 20 73 6f 20  freelist and so 
13d3f 69 74 73 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6e  its.** content n
13d40 6f 20 6c 6f 6e 67 65 72 20 6d 61 74 74 65 72 73  o longer matters
13d41 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 76 65 72  ..**.** The over
13d42 6c 79 69 6e 67 20 73 6f 66 74 77 61 72 65 20 6c  lying software l
13d43 61 79 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  ayer calls this 
13d44 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 61 6c 6c  routine when all
13d45 20 6f 66 20 74 68 65 20 64 61 74 61 0a 2a 2a 20   of the data.** 
13d46 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 70 61 67  on the given pag
13d47 65 20 69 73 20 75 6e 75 73 65 64 2e 20 54 68 65  e is unused. The
13d48 20 70 61 67 65 72 20 6d 61 72 6b 73 20 74 68 65   pager marks the
13d49 20 70 61 67 65 20 61 73 20 63 6c 65 61 6e 20 73   page as clean s
13d4a 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 64 6f 65  o.** that it doe
13d4b 73 20 6e 6f 74 20 67 65 74 20 77 72 69 74 74 65  s not get writte
13d4c 6e 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a  n to disk..**.**
13d4d 20 54 65 73 74 73 20 73 68 6f 77 20 74 68 61 74   Tests show that
13d4e 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69   this optimizati
13d4f 6f 6e 20 63 61 6e 20 71 75 61 64 72 75 70 6c 65  on can quadruple
13d50 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 6c 61   the speed of la
13d51 72 67 65 20 0a 2a 2a 20 44 45 4c 45 54 45 20 6f  rge .** DELETE o
13d52 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51  perations..*/.SQ
13d53 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
13d54 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f  d sqlite3PagerDo
13d55 6e 74 57 72 69 74 65 28 50 67 48 64 72 20 2a 70  ntWrite(PgHdr *p
13d56 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50  Pg){.  Pager *pP
13d57 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67  ager = pPg->pPag
13d58 65 72 3b 0a 20 20 69 66 28 20 28 70 50 67 2d 3e  er;.  if( (pPg->
13d59 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54  flags&PGHDR_DIRT
13d5a 59 29 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 53  Y) && pPager->nS
13d5b 61 76 65 70 6f 69 6e 74 3d 3d 30 20 29 7b 0a 20  avepoint==0 ){. 
13d5c 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22     PAGERTRACE(("
13d5d 44 4f 4e 54 5f 57 52 49 54 45 20 70 61 67 65 20  DONT_WRITE page 
13d5e 25 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 67  %d of %d\n", pPg
13d5f 2d 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49 44 28  ->pgno, PAGERID(
13d60 70 50 61 67 65 72 29 29 29 3b 0a 20 20 20 20 49  pPager)));.    I
13d61 4f 54 52 41 43 45 28 28 22 43 4c 45 41 4e 20 25  OTRACE(("CLEAN %
13d62 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c  p %d\n", pPager,
13d63 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a 20 20 20   pPg->pgno)).   
13d64 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50   pPg->flags |= P
13d65 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 3b  GHDR_DONT_WRITE;
13d66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43  .#ifdef SQLITE_C
13d67 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70  HECK_PAGES.    p
13d68 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70  Pg->pageHash = p
13d69 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50  ager_pagehash(pP
13d6a 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d  g);.#endif.  }.}
13d6b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
13d6c 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74  tine is called t
13d6d 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20  o increment the 
13d6e 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74  value of the dat
13d6f 61 62 61 73 65 20 66 69 6c 65 20 0a 2a 2a 20 63  abase file .** c
13d70 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2c 20 73  hange-counter, s
13d71 74 6f 72 65 64 20 61 73 20 61 20 34 2d 62 79 74  tored as a 4-byt
13d72 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74  e big-endian int
13d73 65 67 65 72 20 73 74 61 72 74 69 6e 67 20 61 74  eger starting at
13d74 20 0a 2a 2a 20 62 79 74 65 20 6f 66 66 73 65 74   .** byte offset
13d75 20 32 34 20 6f 66 20 74 68 65 20 70 61 67 65 72   24 of the pager
13d76 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   file..**.** If 
13d77 74 68 65 20 69 73 44 69 72 65 63 74 4d 6f 64 65  the isDirectMode
13d78 20 66 6c 61 67 20 69 73 20 7a 65 72 6f 2c 20 74   flag is zero, t
13d79 68 65 6e 20 74 68 69 73 20 69 73 20 64 6f 6e 65  hen this is done
13d7a 20 62 79 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20   by calling .** 
13d7b 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
13d7c 65 28 29 20 6f 6e 20 70 61 67 65 20 31 2c 20 74  e() on page 1, t
13d7d 68 65 6e 20 6d 6f 64 69 66 79 69 6e 67 20 74 68  hen modifying th
13d7e 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
13d7f 65 0a 2a 2a 20 70 61 67 65 20 64 61 74 61 2e 20  e.** page data. 
13d80 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
13d81 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 75 70   file will be up
13d82 64 61 74 65 64 20 77 68 65 6e 20 74 68 65 20 63  dated when the c
13d83 75 72 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61  urrent.** transa
13d84 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74  ction is committ
13d85 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73  ed..**.** The is
13d86 44 69 72 65 63 74 4d 6f 64 65 20 66 6c 61 67 20  DirectMode flag 
13d87 6d 61 79 20 6f 6e 6c 79 20 62 65 20 6e 6f 6e 2d  may only be non-
13d88 7a 65 72 6f 20 69 66 20 74 68 65 20 6c 69 62 72  zero if the libr
13d89 61 72 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64  ary was compiled
13d8a 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 53 51 4c  .** with the SQL
13d8b 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49  ITE_ENABLE_ATOMI
13d8c 43 5f 57 52 49 54 45 20 6d 61 63 72 6f 20 64 65  C_WRITE macro de
13d8d 66 69 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63  fined. In this c
13d8e 61 73 65 2c 0a 2a 2a 20 69 66 20 69 73 44 69 72  ase,.** if isDir
13d8f 65 63 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  ect is non-zero,
13d90 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 62 61   then the databa
13d91 73 65 20 66 69 6c 65 20 69 73 20 75 70 64 61 74  se file is updat
13d92 65 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62  ed directly.** b
13d93 79 20 77 72 69 74 69 6e 67 20 61 6e 20 75 70 64  y writing an upd
13d94 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20  ated version of 
13d95 70 61 67 65 20 31 20 75 73 69 6e 67 20 61 20 63  page 1 using a c
13d96 61 6c 6c 20 74 6f 20 74 68 65 20 0a 2a 2a 20 73  all to the .** s
13d97 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 29 20  qlite3OsWrite() 
13d98 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  function..*/.sta
13d99 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 69 6e  tic int pager_in
13d9a 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72  cr_changecounter
13d9b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
13d9c 69 6e 74 20 69 73 44 69 72 65 63 74 4d 6f 64 65  int isDirectMode
13d9d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
13d9e 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 44  LITE_OK;..  /* D
13d9f 65 63 6c 61 72 65 20 61 6e 64 20 69 6e 69 74 69  eclare and initi
13da0 61 6c 69 7a 65 20 63 6f 6e 73 74 61 6e 74 20 69  alize constant i
13da1 6e 74 65 67 65 72 20 27 69 73 44 69 72 65 63 74  nteger 'isDirect
13da2 27 2e 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 61  '. If the.  ** a
13da3 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69  tomic-write opti
13da4 6d 69 7a 61 74 69 6f 6e 20 69 73 20 65 6e 61 62  mization is enab
13da5 6c 65 64 20 69 6e 20 74 68 69 73 20 62 75 69 6c  led in this buil
13da6 64 2c 20 74 68 65 6e 20 69 73 44 69 72 65 63 74  d, then isDirect
13da7 0a 20 20 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c  .  ** is initial
13da8 69 7a 65 64 20 74 6f 20 74 68 65 20 76 61 6c 75  ized to the valu
13da9 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
13daa 69 73 44 69 72 65 63 74 4d 6f 64 65 20 70 61 72  isDirectMode par
13dab 61 6d 65 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74  ameter.  ** to t
13dac 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 4f 74  his function. Ot
13dad 68 65 72 77 69 73 65 2c 20 69 74 20 69 73 20 61  herwise, it is a
13dae 6c 77 61 79 73 20 73 65 74 20 74 6f 20 7a 65 72  lways set to zer
13daf 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  o..  **.  ** The
13db0 20 69 64 65 61 20 69 73 20 74 68 61 74 20 69 66   idea is that if
13db1 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74   the atomic-writ
13db2 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69  e optimization i
13db3 73 20 6e 6f 74 0a 20 20 2a 2a 20 65 6e 61 62 6c  s not.  ** enabl
13db4 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69  ed at compile ti
13db5 6d 65 2c 20 74 68 65 20 63 6f 6d 70 69 6c 65 72  me, the compiler
13db6 20 63 61 6e 20 6f 6d 69 74 20 74 68 65 20 74 65   can omit the te
13db7 73 74 73 20 6f 66 0a 20 20 2a 2a 20 27 69 73 44  sts of.  ** 'isD
13db8 69 72 65 63 74 27 20 62 65 6c 6f 77 2c 20 61 73  irect' below, as
13db9 20 77 65 6c 6c 20 61 73 20 74 68 65 20 62 6c 6f   well as the blo
13dba 63 6b 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 74  ck enclosed in t
13dbb 68 65 0a 20 20 2a 2a 20 22 69 66 28 20 69 73 44  he.  ** "if( isD
13dbc 69 72 65 63 74 20 29 22 20 63 6f 6e 64 69 74 69  irect )" conditi
13dbd 6f 6e 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66  on..  */.#ifndef
13dbe 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41   SQLITE_ENABLE_A
13dbf 54 4f 4d 49 43 5f 57 52 49 54 45 0a 23 20 64 65  TOMIC_WRITE.# de
13dc0 66 69 6e 65 20 44 49 52 45 43 54 5f 4d 4f 44 45  fine DIRECT_MODE
13dc1 20 30 0a 20 20 61 73 73 65 72 74 28 20 69 73 44   0.  assert( isD
13dc2 69 72 65 63 74 4d 6f 64 65 3d 3d 30 20 29 3b 0a  irectMode==0 );.
13dc3 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
13dc4 45 52 28 69 73 44 69 72 65 63 74 4d 6f 64 65 29  ER(isDirectMode)
13dc5 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ;.#else.# define
13dc6 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 69 73 44   DIRECT_MODE isD
13dc7 69 72 65 63 74 4d 6f 64 65 0a 23 65 6e 64 69 66  irectMode.#endif
13dc8 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
13dc9 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52  er->state>=PAGER
13dca 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 69  _RESERVED );.  i
13dcb 66 28 20 21 70 50 61 67 65 72 2d 3e 63 68 61 6e  f( !pPager->chan
13dcc 67 65 43 6f 75 6e 74 44 6f 6e 65 20 26 26 20 41  geCountDone && A
13dcd 4c 57 41 59 53 28 70 50 61 67 65 72 2d 3e 64 62  LWAYS(pPager->db
13dce 53 69 7a 65 3e 30 29 20 29 7b 0a 20 20 20 20 50  Size>0) ){.    P
13dcf 67 48 64 72 20 2a 70 50 67 48 64 72 3b 20 20 20  gHdr *pPgHdr;   
13dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13dd1 52 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67  Reference to pag
13dd2 65 20 31 20 2a 2f 0a 20 20 20 20 75 33 32 20 63  e 1 */.    u32 c
13dd3 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 3b 20 20  hange_counter;  
13dd4 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74           /* Init
13dd5 69 61 6c 20 76 61 6c 75 65 20 6f 66 20 63 68 61  ial value of cha
13dd6 6e 67 65 2d 63 6f 75 6e 74 65 72 20 66 69 65 6c  nge-counter fiel
13dd7 64 20 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74  d */..    assert
13dd8 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  ( !pPager->tempF
13dd9 69 6c 65 20 26 26 20 69 73 4f 70 65 6e 28 70 50  ile && isOpen(pP
13dda 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 0a 20 20  ager->fd) );..  
13ddb 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 31    /* Open page 1
13ddc 20 6f 66 20 74 68 65 20 66 69 6c 65 20 66 6f 72   of the file for
13ddd 20 77 72 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20   writing. */.   
13dde 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
13ddf 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 31 2c  erGet(pPager, 1,
13de0 20 26 70 50 67 48 64 72 29 3b 0a 20 20 20 20 61   &pPgHdr);.    a
13de1 73 73 65 72 74 28 20 70 50 67 48 64 72 3d 3d 30  ssert( pPgHdr==0
13de2 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   || rc==SQLITE_O
13de3 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  K );..    /* If 
13de4 70 61 67 65 20 6f 6e 65 20 77 61 73 20 66 65 74  page one was fet
13de5 63 68 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c  ched successfull
13de6 79 2c 20 61 6e 64 20 74 68 69 73 20 66 75 6e 63  y, and this func
13de7 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 20 20 20 20  tion is not.    
13de8 2a 2a 20 6f 70 65 72 61 74 69 6e 67 20 69 6e 20  ** operating in 
13de9 64 69 72 65 63 74 2d 6d 6f 64 65 2c 20 6d 61 6b  direct-mode, mak
13dea 65 20 70 61 67 65 20 31 20 77 72 69 74 61 62 6c  e page 1 writabl
13deb 65 2e 20 20 57 68 65 6e 20 6e 6f 74 20 69 6e 20  e.  When not in 
13dec 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 74 20 6d  .    ** direct m
13ded 6f 64 65 2c 20 70 61 67 65 20 31 20 69 73 20 61  ode, page 1 is a
13dee 6c 77 61 79 73 20 68 65 6c 64 20 69 6e 20 63 61  lways held in ca
13def 63 68 65 20 61 6e 64 20 68 65 6e 63 65 20 74 68  che and hence th
13df0 65 20 50 61 67 65 72 47 65 74 28 29 0a 20 20 20  e PagerGet().   
13df1 20 2a 2a 20 61 62 6f 76 65 20 69 73 20 61 6c 77   ** above is alw
13df2 61 79 73 20 73 75 63 63 65 73 73 66 75 6c 20 2d  ays successful -
13df3 20 68 65 6e 63 65 20 74 68 65 20 41 4c 57 41 59   hence the ALWAY
13df4 53 20 6f 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f  S on rc==SQLITE_
13df5 4f 4b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  OK..    */.    i
13df6 66 28 20 21 44 49 52 45 43 54 5f 4d 4f 44 45 20  f( !DIRECT_MODE 
13df7 26 26 20 41 4c 57 41 59 53 28 72 63 3d 3d 53 51  && ALWAYS(rc==SQ
13df8 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 20 20 20  LITE_OK) ){.    
13df9 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
13dfa 67 65 72 57 72 69 74 65 28 70 50 67 48 64 72 29  gerWrite(pPgHdr)
13dfb 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
13dfc 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
13dfd 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65  {.      /* Incre
13dfe 6d 65 6e 74 20 74 68 65 20 76 61 6c 75 65 20 6a  ment the value j
13dff 75 73 74 20 72 65 61 64 20 61 6e 64 20 77 72 69  ust read and wri
13e00 74 65 20 69 74 20 62 61 63 6b 20 74 6f 20 62 79  te it back to by
13e01 74 65 20 32 34 2e 20 2a 2f 0a 20 20 20 20 20 20  te 24. */.      
13e02 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 20 3d  change_counter =
13e03 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65   sqlite3Get4byte
13e04 28 28 75 38 2a 29 70 50 61 67 65 72 2d 3e 64 62  ((u8*)pPager->db
13e05 46 69 6c 65 56 65 72 73 29 3b 0a 20 20 20 20 20  FileVers);.     
13e06 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 2b   change_counter+
13e07 2b 3b 0a 20 20 20 20 20 20 70 75 74 33 32 62 69  +;.      put32bi
13e08 74 73 28 28 28 63 68 61 72 2a 29 70 50 67 48 64  ts(((char*)pPgHd
13e09 72 2d 3e 70 44 61 74 61 29 2b 32 34 2c 20 63 68  r->pData)+24, ch
13e0a 61 6e 67 65 5f 63 6f 75 6e 74 65 72 29 3b 0a 0a  ange_counter);..
13e0b 20 20 20 20 20 20 2f 2a 20 49 66 20 72 75 6e 6e        /* If runn
13e0c 69 6e 67 20 69 6e 20 64 69 72 65 63 74 20 6d 6f  ing in direct mo
13e0d 64 65 2c 20 77 72 69 74 65 20 74 68 65 20 63 6f  de, write the co
13e0e 6e 74 65 6e 74 73 20 6f 66 20 70 61 67 65 20 31  ntents of page 1
13e0f 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 20 2a 2f   to the file. */
13e10 0a 20 20 20 20 20 20 69 66 28 20 44 49 52 45 43  .      if( DIREC
13e11 54 5f 4d 4f 44 45 20 29 7b 0a 20 20 20 20 20 20  T_MODE ){.      
13e12 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42    const void *zB
13e13 75 66 20 3d 20 70 50 67 48 64 72 2d 3e 70 44 61  uf = pPgHdr->pDa
13e14 74 61 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  ta;.        asse
13e15 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 46 69  rt( pPager->dbFi
13e16 6c 65 53 69 7a 65 3e 30 20 29 3b 0a 20 20 20 20  leSize>0 );.    
13e17 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
13e18 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e  OsWrite(pPager->
13e19 66 64 2c 20 7a 42 75 66 2c 20 70 50 61 67 65 72  fd, zBuf, pPager
13e1a 2d 3e 70 61 67 65 53 69 7a 65 2c 20 30 29 3b 0a  ->pageSize, 0);.
13e1b 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
13e1c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13e1d 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 63         pPager->c
13e1e 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d  hangeCountDone =
13e1f 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
13e20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
13e21 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67     pPager->chang
13e22 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a  eCountDone = 1;.
13e23 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
13e24 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68     /* Release th
13e25 65 20 70 61 67 65 20 72 65 66 65 72 65 6e 63 65  e page reference
13e26 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  . */.    sqlite3
13e27 50 61 67 65 72 55 6e 72 65 66 28 70 50 67 48 64  PagerUnref(pPgHd
13e28 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  r);.  }.  return
13e29 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79   rc;.}../*.** Sy
13e2a 6e 63 20 74 68 65 20 70 61 67 65 72 20 66 69 6c  nc the pager fil
13e2b 65 20 74 6f 20 64 69 73 6b 2e 20 54 68 69 73 20  e to disk. This 
13e2c 69 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 69  is a no-op for i
13e2d 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 73 0a 2a  n-memory files.*
13e2e 2a 20 6f 72 20 70 61 67 65 73 20 77 69 74 68 20  * or pages with 
13e2f 74 68 65 20 50 61 67 65 72 2e 6e 6f 53 79 6e 63  the Pager.noSync
13e30 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2a 0a 2a 2a   flag set..**.**
13e31 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
13e32 6f 72 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 70  or called on a p
13e33 61 67 65 72 20 66 6f 72 20 77 68 69 63 68 20 69  ager for which i
13e34 74 20 69 73 20 61 20 6e 6f 2d 6f 70 2c 20 74 68  t is a no-op, th
13e35 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72  is.** function r
13e36 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b  eturns SQLITE_OK
13e37 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20  . Otherwise, an 
13e38 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  IO error code is
13e39 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51   returned..*/.SQ
13e3a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
13e3b 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e   sqlite3PagerSyn
13e3c 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  c(Pager *pPager)
13e3d 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  {.  int rc;     
13e3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13e3f 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
13e40 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 61 73 73  rn code */.  ass
13e41 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20  ert( !MEMDB );. 
13e42 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53   if( pPager->noS
13e43 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ync ){.    rc = 
13e44 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c  SQLITE_OK;.  }el
13e45 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  se{.    rc = sql
13e46 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65  ite3OsSync(pPage
13e47 72 2d 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e 73  r->fd, pPager->s
13e48 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 7d 0a  ync_flags);.  }.
13e49 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
13e4a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 64  /*.** Sync the d
13e4b 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72  atabase file for
13e4c 20 74 68 65 20 70 61 67 65 72 20 70 50 61 67 65   the pager pPage
13e4d 72 2e 20 7a 4d 61 73 74 65 72 20 70 6f 69 6e 74  r. zMaster point
13e4e 73 20 74 6f 20 74 68 65 20 6e 61 6d 65 0a 2a 2a  s to the name.**
13e4f 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75   of a master jou
13e50 72 6e 61 6c 20 66 69 6c 65 20 74 68 61 74 20 73  rnal file that s
13e51 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e  hould be written
13e52 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 69 76 69   into the indivi
13e53 64 75 61 6c 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20  dual.** journal 
13e54 66 69 6c 65 2e 20 7a 4d 61 73 74 65 72 20 6d 61  file. zMaster ma
13e55 79 20 62 65 20 4e 55 4c 4c 2c 20 77 68 69 63 68  y be NULL, which
13e56 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20   is interpreted 
13e57 61 73 20 6e 6f 20 6d 61 73 74 65 72 0a 2a 2a 20  as no master.** 
13e58 6a 6f 75 72 6e 61 6c 20 28 61 20 73 69 6e 67 6c  journal (a singl
13e59 65 20 64 61 74 61 62 61 73 65 20 74 72 61 6e 73  e database trans
13e5a 61 63 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 54  action)..**.** T
13e5b 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 73 75  his routine ensu
13e5c 72 65 73 20 74 68 61 74 3a 0a 2a 2a 0a 2a 2a 20  res that:.**.** 
13e5d 20 20 2a 20 54 68 65 20 64 61 74 61 62 61 73 65    * The database
13e5e 20 66 69 6c 65 20 63 68 61 6e 67 65 2d 63 6f 75   file change-cou
13e5f 6e 74 65 72 20 69 73 20 75 70 64 61 74 65 64 2c  nter is updated,
13e60 0a 2a 2a 20 20 20 2a 20 74 68 65 20 6a 6f 75 72  .**   * the jour
13e61 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 28 75  nal is synced (u
13e62 6e 6c 65 73 73 20 74 68 65 20 61 74 6f 6d 69 63  nless the atomic
13e63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74  -write optimizat
13e64 69 6f 6e 20 69 73 20 75 73 65 64 29 2c 0a 2a 2a  ion is used),.**
13e65 20 20 20 2a 20 61 6c 6c 20 64 69 72 74 79 20 70     * all dirty p
13e66 61 67 65 73 20 61 72 65 20 77 72 69 74 74 65 6e  ages are written
13e67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
13e68 20 66 69 6c 65 2c 20 0a 2a 2a 20 20 20 2a 20 74   file, .**   * t
13e69 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
13e6a 20 69 73 20 74 72 75 6e 63 61 74 65 64 20 28 69   is truncated (i
13e6b 66 20 72 65 71 75 69 72 65 64 29 2c 20 61 6e 64  f required), and
13e6c 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 61 74 61  .**   * the data
13e6d 62 61 73 65 20 66 69 6c 65 20 73 79 6e 63 65 64  base file synced
13e6e 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6e 6c  . .**.** The onl
13e6f 79 20 74 68 69 6e 67 20 74 68 61 74 20 72 65 6d  y thing that rem
13e70 61 69 6e 73 20 74 6f 20 63 6f 6d 6d 69 74 20 74  ains to commit t
13e71 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  he transaction i
13e72 73 20 74 6f 20 66 69 6e 61 6c 69 7a 65 20 0a 2a  s to finalize .*
13e73 2a 20 28 64 65 6c 65 74 65 2c 20 74 72 75 6e 63  * (delete, trunc
13e74 61 74 65 20 6f 72 20 7a 65 72 6f 20 74 68 65 20  ate or zero the 
13e75 66 69 72 73 74 20 70 61 72 74 20 6f 66 29 20 74  first part of) t
13e76 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
13e77 28 6f 72 20 0a 2a 2a 20 64 65 6c 65 74 65 20 74  (or .** delete t
13e78 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
13e79 6c 20 66 69 6c 65 20 69 66 20 73 70 65 63 69 66  l file if specif
13e7a 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  ied)..**.** Note
13e7b 20 74 68 61 74 20 69 66 20 7a 4d 61 73 74 65 72   that if zMaster
13e7c 3d 3d 4e 55 4c 4c 2c 20 74 68 69 73 20 64 6f 65  ==NULL, this doe
13e7d 73 20 6e 6f 74 20 6f 76 65 72 77 72 69 74 65 20  s not overwrite 
13e7e 61 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65  a previous value
13e7f 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f 20 61 6e  .** passed to an
13e80 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d   sqlite3PagerCom
13e81 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 63 61  mitPhaseOne() ca
13e82 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ll..**.** If the
13e83 20 66 69 6e 61 6c 20 70 61 72 61 6d 65 74 65 72   final parameter
13e84 20 2d 20 6e 6f 53 79 6e 63 20 2d 20 69 73 20 74   - noSync - is t
13e85 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 64 61  rue, then the da
13e86 74 61 62 61 73 65 20 66 69 6c 65 20 69 74 73 65  tabase file itse
13e87 6c 66 0a 2a 2a 20 69 73 20 6e 6f 74 20 73 79 6e  lf.** is not syn
13e88 63 65 64 2e 20 54 68 65 20 63 61 6c 6c 65 72 20  ced. The caller 
13e89 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65  must call sqlite
13e8a 33 50 61 67 65 72 53 79 6e 63 28 29 20 64 69 72  3PagerSync() dir
13e8b 65 63 74 6c 79 20 74 6f 0a 2a 2a 20 73 79 6e 63  ectly to.** sync
13e8c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
13e8d 6c 65 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e  le before callin
13e8e 67 20 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  g CommitPhaseTwo
13e8f 28 29 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65  () to delete the
13e90 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  .** journal file
13e91 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a   in this case..*
13e92 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
13e93 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
13e94 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28  rCommitPhaseOne(
13e95 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
13e96 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
13e97 20 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65     /* Pager obje
13e98 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ct */.  const ch
13e99 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20 20 20 20  ar *zMaster,    
13e9a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f          /* If no
13e9b 74 20 4e 55 4c 4c 2c 20 74 68 65 20 6d 61 73 74  t NULL, the mast
13e9c 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20  er journal name 
13e9d 2a 2f 0a 20 20 69 6e 74 20 6e 6f 53 79 6e 63 20  */.  int noSync 
13e9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13e9f 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20       /* True to 
13ea0 6f 6d 69 74 20 74 68 65 20 78 53 79 6e 63 20 6f  omit the xSync o
13ea1 6e 20 74 68 65 20 64 62 20 66 69 6c 65 20 2a 2f  n the db file */
13ea2 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
13ea3 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20  QLITE_OK;       
13ea4 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
13ea5 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68  code */..  /* Th
13ea6 65 20 64 62 4f 72 69 67 53 69 7a 65 20 69 73 20  e dbOrigSize is 
13ea7 6e 65 76 65 72 20 73 65 74 20 69 66 20 6a 6f 75  never set if jou
13ea8 72 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 46 20 2a 2f  rnal_mode=OFF */
13ea9 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
13eaa 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d  r->journalMode!=
13eab 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
13eac 45 5f 4f 46 46 20 7c 7c 20 70 50 61 67 65 72 2d  E_OFF || pPager-
13ead 3e 64 62 4f 72 69 67 53 69 7a 65 3d 3d 30 20 29  >dbOrigSize==0 )
13eae 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 20 70 72 69  ;..  /* If a pri
13eaf 6f 72 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65  or error occurre
13eb0 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  d, this routine 
13eb1 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61  should not be ca
13eb2 6c 6c 65 64 2e 20 20 52 4f 4c 4c 42 41 43 4b 0a  lled.  ROLLBACK.
13eb3 20 20 2a 2a 20 69 73 20 74 68 65 20 61 70 70 72    ** is the appr
13eb4 6f 70 72 69 61 74 65 20 72 65 73 70 6f 6e 73 65  opriate response
13eb5 20 74 6f 20 61 6e 20 65 72 72 6f 72 2c 20 6e 6f   to an error, no
13eb6 74 20 43 4f 4d 4d 49 54 2e 20 20 47 75 61 72 64  t COMMIT.  Guard
13eb7 20 61 67 61 69 6e 73 74 0a 20 20 2a 2a 20 63 6f   against.  ** co
13eb8 64 69 6e 67 20 65 72 72 6f 72 73 20 62 79 20 72  ding errors by r
13eb9 65 70 65 61 74 69 6e 67 20 74 68 65 20 70 72 69  epeating the pri
13eba 6f 72 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 69  or error. */.  i
13ebb 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d  f( NEVER(pPager-
13ebc 3e 65 72 72 43 6f 64 65 29 20 29 20 72 65 74 75  >errCode) ) retu
13ebd 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  rn pPager->errCo
13ebe 64 65 3b 0a 0a 20 20 50 41 47 45 52 54 52 41 43  de;..  PAGERTRAC
13ebf 45 28 28 22 44 41 54 41 42 41 53 45 20 53 59 4e  E(("DATABASE SYN
13ec0 43 3a 20 46 69 6c 65 3d 25 73 20 7a 4d 61 73 74  C: File=%s zMast
13ec1 65 72 3d 25 73 20 6e 53 69 7a 65 3d 25 64 5c 6e  er=%s nSize=%d\n
13ec2 22 2c 20 0a 20 20 20 20 20 20 70 50 61 67 65 72  ", .      pPager
13ec3 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 7a 4d 61  ->zFilename, zMa
13ec4 73 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 64 62  ster, pPager->db
13ec5 53 69 7a 65 29 29 3b 0a 0a 20 20 69 66 28 20 4d  Size));..  if( M
13ec6 45 4d 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e  EMDB && pPager->
13ec7 64 62 4d 6f 64 69 66 69 65 64 20 29 7b 0a 20 20  dbModified ){.  
13ec8 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
13ec9 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 62 2c  an in-memory db,
13eca 20 6f 72 20 6e 6f 20 70 61 67 65 73 20 68 61 76   or no pages hav
13ecb 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74  e been written t
13ecc 6f 2c 20 6f 72 20 74 68 69 73 0a 20 20 20 20 2a  o, or this.    *
13ecd 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 61  * function has a
13ece 6c 72 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c  lready been call
13ecf 65 64 2c 20 69 74 20 69 73 20 6d 6f 73 74 6c 79  ed, it is mostly
13ed0 20 61 20 6e 6f 2d 6f 70 2e 20 20 48 6f 77 65 76   a no-op.  Howev
13ed1 65 72 2c 20 61 6e 79 0a 20 20 20 20 2a 2a 20 62  er, any.    ** b
13ed2 61 63 6b 75 70 20 69 6e 20 70 72 6f 67 72 65 73  ackup in progres
13ed3 73 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  s needs to be re
13ed4 73 74 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f 0a  started..    */.
13ed5 20 20 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75      sqlite3Backu
13ed6 70 52 65 73 74 61 72 74 28 70 50 61 67 65 72 2d  pRestart(pPager-
13ed7 3e 70 42 61 63 6b 75 70 29 3b 0a 20 20 7d 65 6c  >pBackup);.  }el
13ed8 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  se if( pPager->s
13ed9 74 61 74 65 21 3d 50 41 47 45 52 5f 53 59 4e 43  tate!=PAGER_SYNC
13eda 45 44 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62  ED && pPager->db
13edb 4d 6f 64 69 66 69 65 64 20 29 7b 0a 0a 20 20 20  Modified ){..   
13edc 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
13edd 67 20 62 6c 6f 63 6b 20 75 70 64 61 74 65 73 20  g block updates 
13ede 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
13edf 65 72 2e 20 45 78 61 63 74 6c 79 20 68 6f 77 20  er. Exactly how 
13ee0 69 74 0a 20 20 20 20 2a 2a 20 64 6f 65 73 20 74  it.    ** does t
13ee1 68 69 73 20 64 65 70 65 6e 64 73 20 6f 6e 20 77  his depends on w
13ee2 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
13ee3 65 20 61 74 6f 6d 69 63 2d 75 70 64 61 74 65 20  e atomic-update 
13ee4 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 20 20 20  optimization.   
13ee5 20 2a 2a 20 77 61 73 20 65 6e 61 62 6c 65 64 20   ** was enabled 
13ee6 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c  at compile time,
13ee7 20 61 6e 64 20 69 66 20 74 68 69 73 20 74 72 61   and if this tra
13ee8 6e 73 61 63 74 69 6f 6e 20 6d 65 65 74 73 20 74  nsaction meets t
13ee9 68 65 20 0a 20 20 20 20 2a 2a 20 72 75 6e 74 69  he .    ** runti
13eea 6d 65 20 63 72 69 74 65 72 69 61 20 74 6f 20 75  me criteria to u
13eeb 73 65 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  se the operation
13eec 3a 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  : .    **.    **
13eed 20 20 20 20 2a 20 54 68 65 20 66 69 6c 65 2d 73      * The file-s
13eee 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 74  ystem supports t
13eef 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20  he atomic-write 
13ef0 70 72 6f 70 65 72 74 79 20 66 6f 72 0a 20 20 20  property for.   
13ef1 20 2a 2a 20 20 20 20 20 20 62 6c 6f 63 6b 73 20   **      blocks 
13ef2 6f 66 20 73 69 7a 65 20 70 61 67 65 2d 73 69 7a  of size page-siz
13ef3 65 2c 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 20  e, and .    **  
13ef4 20 20 2a 20 54 68 69 73 20 63 6f 6d 6d 69 74 20    * This commit 
13ef5 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61  is not part of a
13ef6 20 6d 75 6c 74 69 2d 66 69 6c 65 20 74 72 61 6e   multi-file tran
13ef7 73 61 63 74 69 6f 6e 2c 20 61 6e 64 0a 20 20 20  saction, and.   
13ef8 20 2a 2a 20 20 20 20 2a 20 45 78 61 63 74 6c 79   **    * Exactly
13ef9 20 6f 6e 65 20 70 61 67 65 20 68 61 73 20 62 65   one page has be
13efa 65 6e 20 6d 6f 64 69 66 69 65 64 20 61 6e 64 20  en modified and 
13efb 73 74 6f 72 65 20 69 6e 20 74 68 65 20 6a 6f 75  store in the jou
13efc 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 2a  rnal file..    *
13efd 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20  *.    ** If the 
13efe 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 77 61 73  optimization was
13eff 20 6e 6f 74 20 65 6e 61 62 6c 65 64 20 61 74 20   not enabled at 
13f00 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 74 68  compile time, th
13f01 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 61  en the.    ** pa
13f02 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63  ger_incr_changec
13f03 6f 75 6e 74 65 72 28 29 20 66 75 6e 63 74 69 6f  ounter() functio
13f04 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 75  n is called to u
13f05 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65  pdate the change
13f06 0a 20 20 20 20 2a 2a 20 63 6f 75 6e 74 65 72 20  .    ** counter 
13f07 69 6e 20 27 69 6e 64 69 72 65 63 74 2d 6d 6f 64  in 'indirect-mod
13f08 65 27 2e 20 49 66 20 74 68 65 20 6f 70 74 69 6d  e'. If the optim
13f09 69 7a 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 69  ization is compi
13f0a 6c 65 64 20 69 6e 20 62 75 74 0a 20 20 20 20 2a  led in but.    *
13f0b 2a 20 69 73 20 6e 6f 74 20 61 70 70 6c 69 63 61  * is not applica
13f0c 62 6c 65 20 74 6f 20 74 68 69 73 20 74 72 61 6e  ble to this tran
13f0d 73 61 63 74 69 6f 6e 2c 20 63 61 6c 6c 20 73 71  saction, call sq
13f0e 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61  lite3JournalCrea
13f0f 74 65 28 29 0a 20 20 20 20 2a 2a 20 74 6f 20 6d  te().    ** to m
13f10 61 6b 65 20 73 75 72 65 20 74 68 65 20 6a 6f 75  ake sure the jou
13f11 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20 61 63  rnal file has ac
13f12 74 75 61 6c 6c 79 20 62 65 65 6e 20 63 72 65 61  tually been crea
13f13 74 65 64 2c 20 74 68 65 6e 20 63 61 6c 6c 0a 20  ted, then call. 
13f14 20 20 20 2a 2a 20 70 61 67 65 72 5f 69 6e 63 72     ** pager_incr
13f15 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 29  _changecounter()
13f16 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 63   to update the c
13f17 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 69 6e  hange-counter in
13f18 20 69 6e 64 69 72 65 63 74 0a 20 20 20 20 2a 2a   indirect.    **
13f19 20 6d 6f 64 65 2e 20 0a 20 20 20 20 2a 2a 0a 20   mode. .    **. 
13f1a 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c     ** Otherwise,
13f1b 20 69 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61   if the optimiza
13f1c 74 69 6f 6e 20 69 73 20 62 6f 74 68 20 65 6e 61  tion is both ena
13f1d 62 6c 65 64 20 61 6e 64 20 61 70 70 6c 69 63 61  bled and applica
13f1e 62 6c 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e  ble,.    ** then
13f1f 20 63 61 6c 6c 20 70 61 67 65 72 5f 69 6e 63 72   call pager_incr
13f20 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 29  _changecounter()
13f21 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 63   to update the c
13f22 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 0a 20 20  hange-counter.  
13f23 20 20 2a 2a 20 69 6e 20 27 64 69 72 65 63 74 27    ** in 'direct'
13f24 20 6d 6f 64 65 2e 20 49 6e 20 74 68 69 73 20 63   mode. In this c
13f25 61 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ase the journal 
13f26 66 69 6c 65 20 77 69 6c 6c 20 6e 65 76 65 72 20  file will never 
13f27 62 65 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65  be.    ** create
13f28 64 20 66 6f 72 20 74 68 69 73 20 74 72 61 6e 73  d for this trans
13f29 61 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 23  action..    */.#
13f2a 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
13f2b 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45  BLE_ATOMIC_WRITE
13f2c 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 3b  .    PgHdr *pPg;
13f2d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f  .    assert( isO
13f2e 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
13f2f 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72   || pPager->jour
13f30 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  nalMode==PAGER_J
13f31 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29  OURNALMODE_OFF )
13f32 3b 0a 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74  ;.    if( !zMast
13f33 65 72 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61  er && isOpen(pPa
13f34 67 65 72 2d 3e 6a 66 64 29 20 0a 20 20 20 20 20  ger->jfd) .     
13f35 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  && pPager->journ
13f36 61 6c 4f 66 66 3d 3d 6a 72 6e 6c 42 75 66 66 65  alOff==jrnlBuffe
13f37 72 53 69 7a 65 28 70 50 61 67 65 72 29 20 0a 20  rSize(pPager) . 
13f38 20 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e 64      && pPager->d
13f39 62 53 69 7a 65 3e 3d 70 50 61 67 65 72 2d 3e 64  bSize>=pPager->d
13f3a 62 46 69 6c 65 53 69 7a 65 0a 20 20 20 20 20 26  bFileSize.     &
13f3b 26 20 28 30 3d 3d 28 70 50 67 20 3d 20 73 71 6c  & (0==(pPg = 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 20 7c 7c 20 30 3d 3d 70 50 67 2d  che)) || 0==pPg-
13f3f 3e 70 44 69 72 74 79 29 0a 20 20 20 20 29 7b 0a  >pDirty).    ){.
13f40 20 20 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20        /* Update 
13f41 74 68 65 20 64 62 20 66 69 6c 65 20 63 68 61 6e  the db file chan
13f42 67 65 20 63 6f 75 6e 74 65 72 20 76 69 61 20 74  ge counter via t
13f43 68 65 20 64 69 72 65 63 74 2d 77 72 69 74 65 20  he direct-write 
13f44 6d 65 74 68 6f 64 2e 20 54 68 65 20 0a 20 20 20  method. The .   
13f45 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20     ** following 
13f46 63 61 6c 6c 20 77 69 6c 6c 20 6d 6f 64 69 66 79  call will modify
13f47 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72   the in-memory r
13f48 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
13f49 20 70 61 67 65 20 31 20 0a 20 20 20 20 20 20 2a   page 1 .      *
13f4a 2a 20 74 6f 20 69 6e 63 6c 75 64 65 20 74 68 65  * to include the
13f4b 20 75 70 64 61 74 65 64 20 63 68 61 6e 67 65 20   updated change 
13f4c 63 6f 75 6e 74 65 72 20 61 6e 64 20 74 68 65 6e  counter and then
13f4d 20 77 72 69 74 65 20 70 61 67 65 20 31 20 0a 20   write page 1 . 
13f4e 20 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79       ** directly
13f4f 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
13f50 20 66 69 6c 65 2e 20 42 65 63 61 75 73 65 20 6f   file. Because o
13f51 66 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69  f the atomic-wri
13f52 74 65 20 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f  te .      ** pro
13f53 70 65 72 74 79 20 6f 66 20 74 68 65 20 68 6f 73  perty of the hos
13f54 74 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20 74  t file-system, t
13f55 68 69 73 20 69 73 20 73 61 66 65 2e 0a 20 20 20  his is safe..   
13f56 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d     */.      rc =
13f57 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e   pager_incr_chan
13f58 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67 65 72  gecounter(pPager
13f59 2c 20 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , 1);.    }else{
13f5a 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
13f5b 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65  te3JournalCreate
13f5c 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20  (pPager->jfd);. 
13f5d 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
13f5e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
13f5f 20 20 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63    rc = pager_inc
13f60 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28  r_changecounter(
13f61 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20  pPager, 0);.    
13f62 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a    }.    }.#else.
13f63 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 69      rc = pager_i
13f64 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65  ncr_changecounte
13f65 72 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 23 65  r(pPager, 0);.#e
13f66 6e 64 69 66 0a 20 20 20 20 69 66 28 20 72 63 21  ndif.    if( rc!
13f67 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
13f68 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f  o commit_phase_o
13f69 6e 65 5f 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a  ne_exit;..    /*
13f6a 20 49 66 20 74 68 69 73 20 74 72 61 6e 73 61 63   If this transac
13f6b 74 69 6f 6e 20 68 61 73 20 6d 61 64 65 20 74 68  tion has made th
13f6c 65 20 64 61 74 61 62 61 73 65 20 73 6d 61 6c 6c  e database small
13f6d 65 72 2c 20 74 68 65 6e 20 61 6c 6c 20 70 61 67  er, then all pag
13f6e 65 73 0a 20 20 20 20 2a 2a 20 62 65 69 6e 67 20  es.    ** being 
13f6f 64 69 73 63 61 72 64 65 64 20 62 79 20 74 68 65  discarded by the
13f70 20 74 72 75 6e 63 61 74 69 6f 6e 20 6d 75 73 74   truncation must
13f71 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20 74   be written to t
13f72 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a  he journal.    *
13f73 2a 20 66 69 6c 65 2e 20 54 68 69 73 20 63 61 6e  * file. This can
13f74 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 6e 20   only happen in 
13f75 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65  auto-vacuum mode
13f76 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
13f77 42 65 66 6f 72 65 20 72 65 61 64 69 6e 67 20 74  Before reading t
13f78 68 65 20 70 61 67 65 73 20 77 69 74 68 20 70 61  he pages with pa
13f79 67 65 20 6e 75 6d 62 65 72 73 20 6c 61 72 67 65  ge numbers large
13f7a 72 20 74 68 61 6e 20 74 68 65 20 0a 20 20 20 20  r than the .    
13f7b 2a 2a 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65  ** current value
13f7c 20 6f 66 20 50 61 67 65 72 2e 64 62 53 69 7a 65   of Pager.dbSize
13f7d 2c 20 73 65 74 20 64 62 53 69 7a 65 20 62 61 63  , set dbSize bac
13f7e 6b 20 74 6f 20 74 68 65 20 76 61 6c 75 65 0a 20  k to the value. 
13f7f 20 20 20 2a 2a 20 74 68 61 74 20 69 74 20 74 6f     ** that it to
13f80 6f 6b 20 61 74 20 74 68 65 20 73 74 61 72 74 20  ok at the start 
13f81 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  of the transacti
13f82 6f 6e 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74  on. Otherwise, t
13f83 68 65 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 73 20  he.    ** calls 
13f84 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 47  to sqlite3PagerG
13f85 65 74 28 29 20 72 65 74 75 72 6e 20 7a 65 72 6f  et() return zero
13f86 65 64 20 70 61 67 65 73 20 69 6e 73 74 65 61 64  ed pages instead
13f87 20 6f 66 20 0a 20 20 20 20 2a 2a 20 72 65 61 64   of .    ** read
13f88 69 6e 67 20 64 61 74 61 20 66 72 6f 6d 20 74 68  ing data from th
13f89 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
13f8a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57  .    **.    ** W
13f8b 68 65 6e 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65  hen journal_mode
13f8c 3d 3d 4f 46 46 20 74 68 65 20 64 62 4f 72 69 67  ==OFF the dbOrig
13f8d 53 69 7a 65 20 69 73 20 61 6c 77 61 79 73 20 7a  Size is always z
13f8e 65 72 6f 2c 20 73 6f 20 74 68 69 73 0a 20 20 20  ero, so this.   
13f8f 20 2a 2a 20 62 6c 6f 63 6b 20 6e 65 76 65 72 20   ** block never 
13f90 72 75 6e 73 20 69 66 20 6a 6f 75 72 6e 61 6c 5f  runs if journal_
13f91 6d 6f 64 65 3d 4f 46 46 2e 0a 20 20 20 20 2a 2f  mode=OFF..    */
13f92 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
13f93 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
13f94 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
13f95 64 62 53 69 7a 65 3c 70 50 61 67 65 72 2d 3e 64  dbSize<pPager->d
13f96 62 4f 72 69 67 53 69 7a 65 20 0a 20 20 20 20 20  bOrigSize .     
13f97 26 26 20 41 4c 57 41 59 53 28 70 50 61 67 65 72  && ALWAYS(pPager
13f98 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50  ->journalMode!=P
13f99 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
13f9a 5f 4f 46 46 29 0a 20 20 20 20 29 7b 0a 20 20 20  _OFF).    ){.   
13f9b 20 20 20 50 67 6e 6f 20 69 3b 20 20 20 20 20 20     Pgno i;      
13f9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13f9d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13f9e 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c  Iterator variabl
13f9f 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74  e */.      const
13fa0 20 50 67 6e 6f 20 69 53 6b 69 70 20 3d 20 50 41   Pgno iSkip = PA
13fa1 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67  GER_MJ_PGNO(pPag
13fa2 65 72 29 3b 20 2f 2a 20 50 65 6e 64 69 6e 67 20  er); /* Pending 
13fa3 6c 6f 63 6b 20 70 61 67 65 20 2a 2f 0a 20 20 20  lock page */.   
13fa4 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20 64 62     const Pgno db
13fa5 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64  Size = pPager->d
13fa6 62 53 69 7a 65 3b 20 20 20 20 20 20 20 2f 2a 20  bSize;       /* 
13fa7 44 61 74 61 62 61 73 65 20 69 6d 61 67 65 20 73  Database image s
13fa8 69 7a 65 20 2a 2f 20 0a 20 20 20 20 20 20 70 50  ize */ .      pP
13fa9 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70  ager->dbSize = p
13faa 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a  Pager->dbOrigSiz
13fab 65 3b 0a 20 20 20 20 20 20 66 6f 72 28 20 69 3d  e;.      for( i=
13fac 64 62 53 69 7a 65 2b 31 3b 20 69 3c 3d 70 50 61  dbSize+1; i<=pPa
13fad 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3b  ger->dbOrigSize;
13fae 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20 20   i++ ){.        
13faf 69 66 28 20 21 73 71 6c 69 74 65 33 42 69 74 76  if( !sqlite3Bitv
13fb0 65 63 54 65 73 74 28 70 50 61 67 65 72 2d 3e 70  ecTest(pPager->p
13fb1 49 6e 4a 6f 75 72 6e 61 6c 2c 20 69 29 20 26 26  InJournal, i) &&
13fb2 20 69 21 3d 69 53 6b 69 70 20 29 7b 0a 20 20 20   i!=iSkip ){.   
13fb3 20 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 50         PgHdr *pP
13fb4 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
13fb5 20 2f 2a 20 50 61 67 65 20 74 6f 20 6a 6f 75 72   /* Page to jour
13fb6 6e 61 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  nal */.         
13fb7 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
13fb8 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 69 2c  erGet(pPager, i,
13fb9 20 26 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20   &pPage);.      
13fba 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
13fbb 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d  TE_OK ) goto com
13fbc 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78  mit_phase_one_ex
13fbd 69 74 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63  it;.          rc
13fbe 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
13fbf 72 69 74 65 28 70 50 61 67 65 29 3b 0a 20 20 20  rite(pPage);.   
13fc0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
13fc1 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b  gerUnref(pPage);
13fc2 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
13fc3 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
13fc4 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65  oto commit_phase
13fc5 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20 20 20  _one_exit;.     
13fc6 20 20 20 7d 0a 20 20 20 20 20 20 7d 20 0a 20 20     }.      } .  
13fc7 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69      pPager->dbSi
13fc8 7a 65 20 3d 20 64 62 53 69 7a 65 3b 0a 20 20 20  ze = dbSize;.   
13fc9 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f   }.#endif..    /
13fca 2a 20 57 72 69 74 65 20 74 68 65 20 6d 61 73 74  * Write the mast
13fcb 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20  er journal name 
13fcc 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  into the journal
13fcd 20 66 69 6c 65 2e 20 49 66 20 61 20 6d 61 73 74   file. If a mast
13fce 65 72 20 0a 20 20 20 20 2a 2a 20 6a 6f 75 72 6e  er .    ** journ
13fcf 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 68 61 73  al file name has
13fd0 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 77 72   already been wr
13fd1 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75  itten to the jou
13fd2 72 6e 61 6c 20 66 69 6c 65 2c 20 0a 20 20 20 20  rnal file, .    
13fd3 2a 2a 20 6f 72 20 69 66 20 7a 4d 61 73 74 65 72  ** or if zMaster
13fd4 20 69 73 20 4e 55 4c 4c 20 28 6e 6f 20 6d 61 73   is NULL (no mas
13fd5 74 65 72 20 6a 6f 75 72 6e 61 6c 29 2c 20 74 68  ter journal), th
13fd6 65 6e 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20  en this call is 
13fd7 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20 2a 2f 0a  a no-op..    */.
13fd8 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 4d 61      rc = writeMa
13fd9 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67  sterJournal(pPag
13fda 65 72 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20  er, zMaster);.  
13fdb 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
13fdc 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69  _OK ) goto commi
13fdd 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74  t_phase_one_exit
13fde 3b 0a 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20 74  ;..    /* Sync t
13fdf 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  he journal file.
13fe0 20 49 66 20 74 68 65 20 61 74 6f 6d 69 63 2d 75   If the atomic-u
13fe1 70 64 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69  pdate optimizati
13fe2 6f 6e 20 69 73 20 62 65 69 6e 67 0a 20 20 20 20  on is being.    
13fe3 2a 2a 20 75 73 65 64 2c 20 74 68 69 73 20 63 61  ** used, this ca
13fe4 6c 6c 20 77 69 6c 6c 20 6e 6f 74 20 63 72 65 61  ll will not crea
13fe5 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  te the journal f
13fe6 69 6c 65 20 6f 72 20 70 65 72 66 6f 72 6d 20 61  ile or perform a
13fe7 6e 79 0a 20 20 20 20 2a 2a 20 72 65 61 6c 20 49  ny.    ** real I
13fe8 4f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63  O..    */.    rc
13fe9 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70   = syncJournal(p
13fea 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20  Pager);.    if( 
13feb 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
13fec 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73  goto commit_phas
13fed 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20 20 20  e_one_exit;..   
13fee 20 2f 2a 20 57 72 69 74 65 20 61 6c 6c 20 64 69   /* Write all di
13fef 72 74 79 20 70 61 67 65 73 20 74 6f 20 74 68 65  rty pages to the
13ff0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
13ff1 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65  */.    rc = page
13ff2 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74  r_write_pagelist
13ff3 28 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 69  (sqlite3PcacheDi
13ff4 72 74 79 4c 69 73 74 28 70 50 61 67 65 72 2d 3e  rtyList(pPager->
13ff5 70 50 43 61 63 68 65 29 29 3b 0a 20 20 20 20 69  pPCache));.    i
13ff6 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
13ff7 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
13ff8 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45  ( rc!=SQLITE_IOE
13ff9 52 52 5f 42 4c 4f 43 4b 45 44 20 29 3b 0a 20 20  RR_BLOCKED );.  
13ffa 20 20 20 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f      goto commit_
13ffb 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a  phase_one_exit;.
13ffc 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
13ffd 33 50 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28  3PcacheCleanAll(
13ffe 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
13fff 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  ;..    /* If the
14000 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69 73   file on disk is
14001 20 6e 6f 74 20 74 68 65 20 73 61 6d 65 20 73 69   not the same si
14002 7a 65 20 61 73 20 74 68 65 20 64 61 74 61 62 61  ze as the databa
14003 73 65 20 69 6d 61 67 65 2c 0a 20 20 20 20 2a 2a  se image,.    **
14004 20 74 68 65 6e 20 75 73 65 20 70 61 67 65 72 5f   then use pager_
14005 74 72 75 6e 63 61 74 65 20 74 6f 20 67 72 6f 77  truncate to grow
14006 20 6f 72 20 73 68 72 69 6e 6b 20 74 68 65 20 66   or shrink the f
14007 69 6c 65 20 68 65 72 65 2e 0a 20 20 20 20 2a 2f  ile here..    */
14008 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
14009 3e 64 62 53 69 7a 65 21 3d 70 50 61 67 65 72 2d  >dbSize!=pPager-
1400a 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20  >dbFileSize ){. 
1400b 20 20 20 20 20 50 67 6e 6f 20 6e 4e 65 77 20 3d       Pgno nNew =
1400c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20   pPager->dbSize 
1400d 2d 20 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  - (pPager->dbSiz
1400e 65 3d 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f  e==PAGER_MJ_PGNO
1400f 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20  (pPager));.     
14010 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
14011 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58  >state>=PAGER_EX
14012 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 20 20 20  CLUSIVE );.     
14013 20 72 63 20 3d 20 70 61 67 65 72 5f 74 72 75 6e   rc = pager_trun
14014 63 61 74 65 28 70 50 61 67 65 72 2c 20 6e 4e 65  cate(pPager, nNe
14015 77 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  w);.      if( rc
14016 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
14017 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f  to commit_phase_
14018 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20 20 7d 0a  one_exit;.    }.
14019 0a 20 20 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c  .    /* Finally,
1401a 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 62 61   sync the databa
1401b 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20  se file. */.    
1401c 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53  if( !pPager->noS
1401d 79 6e 63 20 26 26 20 21 6e 6f 53 79 6e 63 20 29  ync && !noSync )
1401e 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
1401f 69 74 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65  ite3OsSync(pPage
14020 72 2d 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e 73  r->fd, pPager->s
14021 79 6e 63 5f 66 6c 61 67 73 29 3b 0a 20 20 20 20  ync_flags);.    
14022 7d 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22  }.    IOTRACE(("
14023 44 42 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50  DBSYNC %p\n", pP
14024 61 67 65 72 29 29 0a 0a 20 20 20 20 70 50 61 67  ager))..    pPag
14025 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45  er->state = PAGE
14026 52 5f 53 59 4e 43 45 44 3b 0a 20 20 7d 0a 0a 63  R_SYNCED;.  }..c
14027 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f  ommit_phase_one_
14028 65 78 69 74 3a 0a 20 20 72 65 74 75 72 6e 20 72  exit:.  return r
14029 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 65  c;.}.../*.** Whe
1402a 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
1402b 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 64  is called, the d
1402c 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 73  atabase file has
1402d 20 62 65 65 6e 20 63 6f 6d 70 6c 65 74 65 6c 79   been completely
1402e 0a 2a 2a 20 75 70 64 61 74 65 64 20 74 6f 20 72  .** updated to r
1402f 65 66 6c 65 63 74 20 74 68 65 20 63 68 61 6e 67  eflect the chang
14030 65 73 20 6d 61 64 65 20 62 79 20 74 68 65 20 63  es made by the c
14031 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
14032 6f 6e 20 61 6e 64 0a 2a 2a 20 73 79 6e 63 65 64  on and.** synced
14033 20 74 6f 20 64 69 73 6b 2e 20 54 68 65 20 6a 6f   to disk. The jo
14034 75 72 6e 61 6c 20 66 69 6c 65 20 73 74 69 6c 6c  urnal file still
14035 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20 66   exists in the f
14036 69 6c 65 2d 73 79 73 74 65 6d 20 0a 2a 2a 20 74  ile-system .** t
14037 68 6f 75 67 68 2c 20 61 6e 64 20 69 66 20 61 20  hough, and if a 
14038 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 61  failure occurs a
14039 74 20 74 68 69 73 20 70 6f 69 6e 74 20 69 74 20  t this point it 
1403a 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 0a  will eventually.
1403b 2a 2a 20 62 65 20 75 73 65 64 20 61 73 20 61 20  ** be used as a 
1403c 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e 64 20  hot-journal and 
1403d 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e  the current tran
1403e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c 65 64 20 62  saction rolled b
1403f 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ack..**.** This 
14040 66 75 6e 63 74 69 6f 6e 20 66 69 6e 61 6c 69 7a  function finaliz
14041 65 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  es the journal f
14042 69 6c 65 2c 20 65 69 74 68 65 72 20 62 79 20 64  ile, either by d
14043 65 6c 65 74 69 6e 67 2c 20 0a 2a 2a 20 74 72 75  eleting, .** tru
14044 6e 63 61 74 69 6e 67 20 6f 72 20 70 61 72 74 69  ncating or parti
14045 61 6c 6c 79 20 7a 65 72 6f 69 6e 67 20 69 74 2c  ally zeroing it,
14046 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 6e   so that it cann
14047 6f 74 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 66  ot be used .** f
14048 6f 72 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72  or hot-journal r
14049 6f 6c 6c 62 61 63 6b 2e 20 4f 6e 63 65 20 74 68  ollback. Once th
1404a 69 73 20 69 73 20 64 6f 6e 65 20 74 68 65 20 74  is is done the t
1404b 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a  ransaction is.**
1404c 20 69 72 72 65 76 6f 63 61 62 6c 79 20 63 6f 6d   irrevocably com
1404d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  mitted..**.** If
1404e 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
1404f 2c 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f  , an IO error co
14050 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  de is returned a
14051 6e 64 20 74 68 65 20 70 61 67 65 72 0a 2a 2a 20  nd the pager.** 
14052 6d 6f 76 65 73 20 69 6e 74 6f 20 74 68 65 20 65  moves into the e
14053 72 72 6f 72 20 73 74 61 74 65 2e 20 4f 74 68 65  rror state. Othe
14054 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b  rwise, SQLITE_OK
14055 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
14056 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14057 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
14058 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50  CommitPhaseTwo(P
14059 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
1405a 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1405b 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
1405c 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
1405d 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68  code */..  /* Th
1405e 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c  is routine shoul
1405f 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20  d not be called 
14060 69 66 20 61 20 70 72 69 6f 72 20 65 72 72 6f 72  if a prior error
14061 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 0a 20   has occurred.. 
14062 20 2a 2a 20 42 75 74 20 69 66 20 28 64 75 65 20   ** But if (due 
14063 74 6f 20 61 20 63 6f 64 69 6e 67 20 65 72 72 6f  to a coding erro
14064 72 20 65 6c 73 65 77 68 65 72 65 20 69 6e 20 74  r elsewhere in t
14065 68 65 20 73 79 73 74 65 6d 29 20 69 74 20 64 6f  he system) it do
14066 65 73 20 67 65 74 0a 20 20 2a 2a 20 63 61 6c 6c  es get.  ** call
14067 65 64 2c 20 6a 75 73 74 20 72 65 74 75 72 6e 20  ed, just return 
14068 74 68 65 20 73 61 6d 65 20 65 72 72 6f 72 20 63  the same error c
14069 6f 64 65 20 77 69 74 68 6f 75 74 20 64 6f 69 6e  ode without doin
1406a 67 20 61 6e 79 74 68 69 6e 67 2e 20 2a 2f 0a 20  g anything. */. 
1406b 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65   if( NEVER(pPage
1406c 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 72 65  r->errCode) ) re
1406d 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72  turn pPager->err
1406e 43 6f 64 65 3b 0a 0a 20 20 2f 2a 20 54 68 69 73  Code;..  /* This
1406f 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64   function should
14070 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 69   not be called i
14071 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 6e  f the pager is n
14072 6f 74 20 69 6e 20 61 74 20 6c 65 61 73 74 0a 20  ot in at least. 
14073 20 2a 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56   ** PAGER_RESERV
14074 45 44 20 73 74 61 74 65 2e 20 41 6e 64 20 69 6e  ED state. And in
14075 64 65 65 64 20 53 51 4c 69 74 65 20 6e 65 76 65  deed SQLite neve
14076 72 20 64 6f 65 73 20 74 68 69 73 2e 20 42 75 74  r does this. But
14077 20 69 74 20 69 73 0a 20 20 2a 2a 20 6e 69 63 65   it is.  ** nice
14078 20 74 6f 20 68 61 76 65 20 74 68 69 73 20 64 65   to have this de
14079 66 65 6e 73 69 76 65 20 74 65 73 74 20 68 65 72  fensive test her
1407a 65 20 61 6e 79 77 61 79 2e 0a 20 20 2a 2f 0a 20  e anyway..  */. 
1407b 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65   if( NEVER(pPage
1407c 72 2d 3e 73 74 61 74 65 3c 50 41 47 45 52 5f 52  r->state<PAGER_R
1407d 45 53 45 52 56 45 44 29 20 29 20 72 65 74 75 72  ESERVED) ) retur
1407e 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
1407f 0a 20 20 2f 2a 20 41 6e 20 6f 70 74 69 6d 69 7a  .  /* An optimiz
14080 61 74 69 6f 6e 2e 20 49 66 20 74 68 65 20 64 61  ation. If the da
14081 74 61 62 61 73 65 20 77 61 73 20 6e 6f 74 20 61  tabase was not a
14082 63 74 75 61 6c 6c 79 20 6d 6f 64 69 66 69 65 64  ctually modified
14083 20 64 75 72 69 6e 67 0a 20 20 2a 2a 20 74 68 69   during.  ** thi
14084 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74  s transaction, t
14085 68 65 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e  he pager is runn
14086 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65  ing in exclusive
14087 2d 6d 6f 64 65 20 61 6e 64 20 69 73 0a 20 20 2a  -mode and is.  *
14088 2a 20 75 73 69 6e 67 20 70 65 72 73 69 73 74 65  * using persiste
14089 6e 74 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65  nt journals, the
1408a 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
1408b 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a  is a no-op..  **
1408c 0a 20 20 2a 2a 20 54 68 65 20 73 74 61 72 74 20  .  ** The start 
1408d 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  of the journal f
1408e 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f  ile currently co
1408f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 20  ntains a single 
14090 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 65  journal .  ** he
14091 61 64 65 72 20 77 69 74 68 20 74 68 65 20 6e 52  ader with the nR
14092 65 63 20 66 69 65 6c 64 20 73 65 74 20 74 6f 20  ec field set to 
14093 30 2e 20 49 66 20 73 75 63 68 20 61 20 6a 6f 75  0. If such a jou
14094 72 6e 61 6c 20 69 73 20 75 73 65 64 20 61 73 0a  rnal is used as.
14095 20 20 2a 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 6e    ** a hot-journ
14096 61 6c 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a 6f  al during hot-jo
14097 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20  urnal rollback, 
14098 30 20 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 62  0 changes will b
14099 65 20 6d 61 64 65 0a 20 20 2a 2a 20 74 6f 20 74  e made.  ** to t
1409a 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1409b 2e 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f  . So there is no
1409c 20 6e 65 65 64 20 74 6f 20 7a 65 72 6f 20 74 68   need to zero th
1409d 65 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20  e journal .  ** 
1409e 68 65 61 64 65 72 2e 20 53 69 6e 63 65 20 74 68  header. Since th
1409f 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 65 78  e pager is in ex
140a0 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 68  clusive mode, th
140a1 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20  ere is no need. 
140a2 20 2a 2a 20 74 6f 20 64 72 6f 70 20 61 6e 79 20   ** to drop any 
140a3 6c 6f 63 6b 73 20 65 69 74 68 65 72 2e 0a 20 20  locks either..  
140a4 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  */.  if( pPager-
140a5 3e 64 62 4d 6f 64 69 66 69 65 64 3d 3d 30 20 26  >dbModified==0 &
140a6 26 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73  & pPager->exclus
140a7 69 76 65 4d 6f 64 65 20 0a 20 20 20 26 26 20 70  iveMode .   && p
140a8 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
140a9 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
140aa 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20  LMODE_PERSIST.  
140ab 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
140ac 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
140ad 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  f==JOURNAL_HDR_S
140ae 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20  Z(pPager) );.   
140af 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
140b0 4b 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54  K;.  }..  PAGERT
140b1 52 41 43 45 28 28 22 43 4f 4d 4d 49 54 20 25 64  RACE(("COMMIT %d
140b2 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61  \n", PAGERID(pPa
140b3 67 65 72 29 29 29 3b 0a 20 20 61 73 73 65 72 74  ger)));.  assert
140b4 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d  ( pPager->state=
140b5 3d 50 41 47 45 52 5f 53 59 4e 43 45 44 20 7c 7c  =PAGER_SYNCED ||
140b6 20 4d 45 4d 44 42 20 7c 7c 20 21 70 50 61 67 65   MEMDB || !pPage
140b7 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 3b  r->dbModified );
140b8 0a 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e  .  rc = pager_en
140b9 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50  d_transaction(pP
140ba 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65  ager, pPager->se
140bb 74 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 74 75  tMaster);.  retu
140bc 72 6e 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70  rn pager_error(p
140bd 50 61 67 65 72 2c 20 72 63 29 3b 0a 7d 0a 0a 2f  Pager, rc);.}../
140be 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6c  *.** Rollback al
140bf 6c 20 63 68 61 6e 67 65 73 2e 20 54 68 65 20 64  l changes. The d
140c0 61 74 61 62 61 73 65 20 66 61 6c 6c 73 20 62 61  atabase falls ba
140c1 63 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52  ck to PAGER_SHAR
140c2 45 44 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54  ED mode..**.** T
140c3 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 70 65 72  his function per
140c4 66 6f 72 6d 73 20 74 77 6f 20 74 61 73 6b 73 3a  forms two tasks:
140c5 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 74 20 72  .**.**   1) It r
140c6 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65 20 6a 6f  olls back the jo
140c7 75 72 6e 61 6c 20 66 69 6c 65 2c 20 72 65 73 74  urnal file, rest
140c8 6f 72 69 6e 67 20 61 6c 6c 20 64 61 74 61 62 61  oring all databa
140c9 73 65 20 66 69 6c 65 20 61 6e 64 20 0a 2a 2a 20  se file and .** 
140ca 20 20 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20 63       in-memory c
140cb 61 63 68 65 20 70 61 67 65 73 20 74 6f 20 74 68  ache pages to th
140cc 65 20 73 74 61 74 65 20 74 68 65 79 20 77 65 72  e state they wer
140cd 65 20 69 6e 20 77 68 65 6e 20 74 68 65 20 74 72  e in when the tr
140ce 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20  ansaction.**    
140cf 20 20 77 61 73 20 6f 70 65 6e 65 64 2c 20 61 6e    was opened, an
140d0 64 0a 2a 2a 20 20 20 32 29 20 49 74 20 66 69 6e  d.**   2) It fin
140d1 61 6c 69 7a 65 73 20 74 68 65 20 6a 6f 75 72 6e  alizes the journ
140d2 61 6c 20 66 69 6c 65 2c 20 73 6f 20 74 68 61 74  al file, so that
140d3 20 69 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20   it is not used 
140d4 66 6f 72 20 68 6f 74 0a 2a 2a 20 20 20 20 20 20  for hot.**      
140d5 72 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6e 79 20  rollback at any 
140d6 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74  point in the fut
140d7 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 65  ure..**.** subje
140d8 63 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77  ct to the follow
140d9 69 6e 67 20 71 75 61 6c 69 66 69 63 61 74 69 6f  ing qualificatio
140da 6e 73 3a 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 74  ns:.**.** * If t
140db 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
140dc 69 73 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e 20  is not yet open 
140dd 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  when this functi
140de 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a  on is called,.**
140df 20 20 20 74 68 65 6e 20 6f 6e 6c 79 20 28 32 29     then only (2)
140e0 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 49   is performed. I
140e1 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72  n this case ther
140e2 65 20 69 73 20 6e 6f 20 6a 6f 75 72 6e 61 6c 20  e is no journal 
140e3 66 69 6c 65 0a 2a 2a 20 20 20 74 6f 20 72 6f 6c  file.**   to rol
140e4 6c 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 2a 20  l back..**.** * 
140e5 49 66 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73  If in an error s
140e6 74 61 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20  tate other than 
140e7 53 51 4c 49 54 45 5f 46 55 4c 4c 2c 20 74 68 65  SQLITE_FULL, the
140e8 6e 20 74 61 73 6b 20 28 31 29 20 69 73 20 0a 2a  n task (1) is .*
140e9 2a 20 20 20 70 65 72 66 6f 72 6d 65 64 2e 20 49  *   performed. I
140ea 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 61  f successful, ta
140eb 73 6b 20 28 32 29 2e 20 52 65 67 61 72 64 6c 65  sk (2). Regardle
140ec 73 73 20 6f 66 20 74 68 65 20 6f 75 74 63 6f 6d  ss of the outcom
140ed 65 0a 2a 2a 20 20 20 6f 66 20 65 69 74 68 65 72  e.**   of either
140ee 2c 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74  , the error stat
140ef 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  e error code is 
140f0 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
140f1 63 61 6c 6c 65 72 0a 2a 2a 20 20 20 28 69 2e 65  caller.**   (i.e
140f2 2e 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f  . either SQLITE_
140f3 49 4f 45 52 52 20 6f 72 20 53 51 4c 49 54 45 5f  IOERR or SQLITE_
140f4 43 4f 52 52 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20  CORRUPT)..**.** 
140f5 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  * If the pager i
140f6 73 20 69 6e 20 50 41 47 45 52 5f 52 45 53 45 52  s in PAGER_RESER
140f7 56 45 44 20 73 74 61 74 65 2c 20 74 68 65 6e 20  VED state, then 
140f8 61 74 74 65 6d 70 74 20 28 31 29 2e 20 57 68 65  attempt (1). Whe
140f9 74 68 65 72 0a 2a 2a 20 20 20 6f 72 20 6e 6f 74  ther.**   or not
140fa 20 28 31 29 20 69 73 20 73 75 63 63 75 73 73 66   (1) is succussf
140fb 75 6c 2c 20 61 6c 73 6f 20 61 74 74 65 6d 70 74  ul, also attempt
140fc 20 28 32 29 2e 20 49 66 20 73 75 63 63 65 73 73   (2). If success
140fd 66 75 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 20  ful, return.**  
140fe 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65   SQLITE_OK. Othe
140ff 72 77 69 73 65 2c 20 65 6e 74 65 72 20 74 68 65  rwise, enter the
14100 20 65 72 72 6f 72 20 73 74 61 74 65 20 61 6e 64   error state and
14101 20 72 65 74 75 72 6e 20 74 68 65 20 66 69 72 73   return the firs
14102 74 20 0a 2a 2a 20 20 20 65 72 72 6f 72 20 63 6f  t .**   error co
14103 64 65 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 20  de encountered. 
14104 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20 74 68 69 73  .**.**   In this
14105 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20 6e   case there is n
14106 6f 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 68  o chance that th
14107 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20 77  e database was w
14108 72 69 74 74 65 6e 20 74 6f 2e 20 0a 2a 2a 20 20  ritten to. .**  
14109 20 53 6f 20 69 73 20 73 61 66 65 20 74 6f 20 66   So is safe to f
1410a 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72  inalize the jour
1410b 6e 61 6c 20 66 69 6c 65 20 65 76 65 6e 20 69 66  nal file even if
1410c 20 74 68 65 20 70 6c 61 79 62 61 63 6b 20 0a 2a   the playback .*
1410d 2a 20 20 20 28 6f 70 65 72 61 74 69 6f 6e 20 31  *   (operation 1
1410e 29 20 66 61 69 6c 65 64 2e 20 48 6f 77 65 76 65  ) failed. Howeve
1410f 72 20 74 68 65 20 70 61 67 65 72 20 6d 75 73 74  r the pager must
14110 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72   enter the error
14111 20 73 74 61 74 65 0a 2a 2a 20 20 20 61 73 20 74   state.**   as t
14112 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
14113 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63  he in-memory cac
14114 68 65 20 61 72 65 20 6e 6f 77 20 73 75 73 70 65  he are now suspe
14115 63 74 2e 0a 2a 2a 0a 2a 2a 20 2a 20 46 69 6e 61  ct..**.** * Fina
14116 6c 6c 79 2c 20 69 66 20 69 6e 20 50 41 47 45 52  lly, if in PAGER
14117 5f 45 58 43 4c 55 53 49 56 45 20 73 74 61 74 65  _EXCLUSIVE state
14118 2c 20 74 68 65 6e 20 61 74 74 65 6d 70 74 20 28  , then attempt (
14119 31 29 2e 20 4f 6e 6c 79 0a 2a 2a 20 20 20 61 74  1). Only.**   at
1411a 74 65 6d 70 74 20 28 32 29 20 69 66 20 28 31 29  tempt (2) if (1)
1411b 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2e 20   is successful. 
1411c 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
1411d 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c 0a   if successful,.
1411e 2a 2a 20 20 20 6f 74 68 65 72 77 69 73 65 20 65  **   otherwise e
1411f 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 73  nter the error s
14120 74 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20  tate and return 
14121 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 66  the error code f
14122 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 20 20 66 61  rom the .**   fa
14123 69 6c 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 2e  iling operation.
14124 0a 2a 2a 0a 2a 2a 20 20 20 49 6e 20 74 68 69 73  .**.**   In this
14125 20 63 61 73 65 20 74 68 65 20 64 61 74 61 62 61   case the databa
14126 73 65 20 66 69 6c 65 20 6d 61 79 20 68 61 76 65  se file may have
14127 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f   been written to
14128 2e 20 53 6f 20 69 66 20 74 68 65 0a 2a 2a 20 20  . So if the.**  
14129 20 70 6c 61 79 62 61 63 6b 20 6f 70 65 72 61 74   playback operat
1412a 69 6f 6e 20 64 69 64 20 6e 6f 74 20 73 75 63 63  ion did not succ
1412b 65 65 64 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74  eed it would not
1412c 20 62 65 20 73 61 66 65 20 74 6f 20 66 69 6e 61   be safe to fina
1412d 6c 69 7a 65 0a 2a 2a 20 20 20 74 68 65 20 6a 6f  lize.**   the jo
1412e 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 74 20 6e  urnal file. It n
1412f 65 65 64 73 20 74 6f 20 62 65 20 6c 65 66 74 20  eeds to be left 
14130 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74  in the file-syst
14131 65 6d 20 73 6f 20 74 68 61 74 0a 2a 2a 20 20 20  em so that.**   
14132 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65  some other proce
14133 73 73 20 63 61 6e 20 75 73 65 20 69 74 20 74 6f  ss can use it to
14134 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 74   restore the dat
14135 61 62 61 73 65 20 73 74 61 74 65 20 28 62 79 0a  abase state (by.
14136 2a 2a 20 20 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c  **   hot-journal
14137 20 72 6f 6c 6c 62 61 63 6b 29 2e 0a 2a 2f 0a 53   rollback)..*/.S
14138 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
14139 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f  t sqlite3PagerRo
1413a 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 2a 70 50  llback(Pager *pP
1413b 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20  ager){.  int rc 
1413c 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
1413d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1413e 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
1413f 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 52    PAGERTRACE(("R
14140 4f 4c 4c 42 41 43 4b 20 25 64 5c 6e 22 2c 20 50  OLLBACK %d\n", P
14141 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29  AGERID(pPager)))
14142 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d  ;.  if( !pPager-
14143 3e 64 62 4d 6f 64 69 66 69 65 64 20 7c 7c 20 21  >dbModified || !
14144 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
14145 66 64 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  fd) ){.    rc = 
14146 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61  pager_end_transa
14147 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 70 50  ction(pPager, pP
14148 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 29  ager->setMaster)
14149 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50  ;.  }else if( pP
1414a 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 26  ager->errCode &&
1414b 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
1414c 21 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 7b  !=SQLITE_FULL ){
1414d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
1414e 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58  >state>=PAGER_EX
1414f 43 4c 55 53 49 56 45 20 29 7b 0a 20 20 20 20 20  CLUSIVE ){.     
14150 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28   pager_playback(
14151 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20  pPager, 0);.    
14152 7d 0a 20 20 20 20 72 63 20 3d 20 70 50 61 67 65  }.    rc = pPage
14153 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 65  r->errCode;.  }e
14154 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70 50 61  lse{.    if( pPa
14155 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45  ger->state==PAGE
14156 52 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20  R_RESERVED ){.  
14157 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 20      int rc2;.   
14158 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c     rc = pager_pl
14159 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20 30  ayback(pPager, 0
1415a 29 3b 0a 20 20 20 20 20 20 72 63 32 20 3d 20 70  );.      rc2 = p
1415b 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63  ager_end_transac
1415c 74 69 6f 6e 28 70 50 61 67 65 72 2c 20 70 50 61  tion(pPager, pPa
1415d 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b  ger->setMaster);
1415e 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
1415f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
14160 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20      rc = rc2;.  
14161 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
14162 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
14163 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65  r_playback(pPage
14164 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  r, 0);.    }..  
14165 20 20 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a    if( !MEMDB ){.
14166 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62        pPager->db
14167 53 69 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20  SizeValid = 0;. 
14168 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
14169 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
1416a 64 75 72 69 6e 67 20 61 20 52 4f 4c 4c 42 41 43  during a ROLLBAC
1416b 4b 2c 20 77 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e  K, we can no lon
1416c 67 65 72 20 74 72 75 73 74 20 74 68 65 20 70 61  ger trust the pa
1416d 67 65 72 0a 20 20 20 20 2a 2a 20 63 61 63 68 65  ger.    ** cache
1416e 2e 20 53 6f 20 63 61 6c 6c 20 70 61 67 65 72 5f  . So call pager_
1416f 65 72 72 6f 72 28 29 20 6f 6e 20 74 68 65 20 77  error() on the w
14170 61 79 20 6f 75 74 20 74 6f 20 6d 61 6b 65 20 61  ay out to make a
14171 6e 79 20 65 72 72 6f 72 20 0a 20 20 20 20 2a 2a  ny error .    **
14172 20 70 65 72 73 69 73 74 65 6e 74 2e 0a 20 20 20   persistent..   
14173 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 67   */.    rc = pag
14174 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c  er_error(pPager,
14175 20 72 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75   rc);.  }.  retu
14176 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
14177 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74  Return TRUE if t
14178 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
14179 20 69 73 20 6f 70 65 6e 65 64 20 72 65 61 64 2d   is opened read-
1417a 6f 6e 6c 79 2e 20 20 52 65 74 75 72 6e 20 46 41  only.  Return FA
1417b 4c 53 45 0a 2a 2a 20 69 66 20 74 68 65 20 64 61  LSE.** if the da
1417c 74 61 62 61 73 65 20 69 73 20 28 69 6e 20 74 68  tabase is (in th
1417d 65 6f 72 79 29 20 77 72 69 74 61 62 6c 65 2e 0a  eory) writable..
1417e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1417f 45 20 75 38 20 73 71 6c 69 74 65 33 50 61 67 65  E u8 sqlite3Page
14180 72 49 73 72 65 61 64 6f 6e 6c 79 28 50 61 67 65  rIsreadonly(Page
14181 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65  r *pPager){.  re
14182 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 72 65 61  turn pPager->rea
14183 64 4f 6e 6c 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  dOnly;.}../*.** 
14184 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
14185 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20  r of references 
14186 74 6f 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f  to the pager..*/
14187 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14188 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
14189 52 65 66 63 6f 75 6e 74 28 50 61 67 65 72 20 2a  Refcount(Pager *
1418a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72  pPager){.  retur
1418b 6e 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52  n sqlite3PcacheR
1418c 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e  efCount(pPager->
1418d 70 50 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a  pPCache);.}../*.
1418e 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
1418f 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63  mber of referenc
14190 65 73 20 74 6f 20 74 68 65 20 73 70 65 63 69 66  es to the specif
14191 69 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c  ied page..*/.SQL
14192 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
14193 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
14194 52 65 66 63 6f 75 6e 74 28 44 62 50 61 67 65 20  Refcount(DbPage 
14195 2a 70 50 61 67 65 29 7b 0a 20 20 72 65 74 75 72  *pPage){.  retur
14196 6e 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50  n sqlite3PcacheP
14197 61 67 65 52 65 66 63 6f 75 6e 74 28 70 50 61 67  ageRefcount(pPag
14198 65 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51  e);.}..#ifdef SQ
14199 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20  LITE_TEST./*.** 
1419a 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
1419b 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  used for testing
1419c 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e   and analysis on
1419d 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ly..*/.SQLITE_PR
1419e 49 56 41 54 45 20 69 6e 74 20 2a 73 71 6c 69 74  IVATE int *sqlit
1419f 65 33 50 61 67 65 72 53 74 61 74 73 28 50 61 67  e3PagerStats(Pag
141a0 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73  er *pPager){.  s
141a1 74 61 74 69 63 20 69 6e 74 20 61 5b 31 31 5d 3b  tatic int a[11];
141a2 0a 20 20 61 5b 30 5d 20 3d 20 73 71 6c 69 74 65  .  a[0] = sqlite
141a3 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28  3PcacheRefCount(
141a4 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
141a5 3b 0a 20 20 61 5b 31 5d 20 3d 20 73 71 6c 69 74  ;.  a[1] = sqlit
141a6 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 6e  e3PcachePagecoun
141a7 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  t(pPager->pPCach
141a8 65 29 3b 0a 20 20 61 5b 32 5d 20 3d 20 73 71 6c  e);.  a[2] = sql
141a9 69 74 65 33 50 63 61 63 68 65 47 65 74 43 61 63  ite3PcacheGetCac
141aa 68 65 73 69 7a 65 28 70 50 61 67 65 72 2d 3e 70  hesize(pPager->p
141ab 50 43 61 63 68 65 29 3b 0a 20 20 61 5b 33 5d 20  PCache);.  a[3] 
141ac 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  = pPager->dbSize
141ad 56 61 6c 69 64 20 3f 20 28 69 6e 74 29 20 70 50  Valid ? (int) pP
141ae 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3a 20 2d  ager->dbSize : -
141af 31 3b 0a 20 20 61 5b 34 5d 20 3d 20 70 50 61 67  1;.  a[4] = pPag
141b0 65 72 2d 3e 73 74 61 74 65 3b 0a 20 20 61 5b 35  er->state;.  a[5
141b1 5d 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43  ] = pPager->errC
141b2 6f 64 65 3b 0a 20 20 61 5b 36 5d 20 3d 20 70 50  ode;.  a[6] = pP
141b3 61 67 65 72 2d 3e 6e 48 69 74 3b 0a 20 20 61 5b  ager->nHit;.  a[
141b4 37 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e 4d 69  7] = pPager->nMi
141b5 73 73 3b 0a 20 20 61 5b 38 5d 20 3d 20 30 3b 20  ss;.  a[8] = 0; 
141b6 20 2f 2a 20 55 73 65 64 20 74 6f 20 62 65 20 70   /* Used to be p
141b7 50 61 67 65 72 2d 3e 6e 4f 76 66 6c 20 2a 2f 0a  Pager->nOvfl */.
141b8 20 20 61 5b 39 5d 20 3d 20 70 50 61 67 65 72 2d    a[9] = pPager-
141b9 3e 6e 52 65 61 64 3b 0a 20 20 61 5b 31 30 5d 20  >nRead;.  a[10] 
141ba 3d 20 70 50 61 67 65 72 2d 3e 6e 57 72 69 74 65  = pPager->nWrite
141bb 3b 0a 20 20 72 65 74 75 72 6e 20 61 3b 0a 7d 0a  ;.  return a;.}.
141bc 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65  #endif../*.** Re
141bd 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 69  turn true if thi
141be 73 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  s is an in-memor
141bf 79 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49  y pager..*/.SQLI
141c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
141c1 71 6c 69 74 65 33 50 61 67 65 72 49 73 4d 65 6d  qlite3PagerIsMem
141c2 64 62 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  db(Pager *pPager
141c3 29 7b 0a 20 20 72 65 74 75 72 6e 20 4d 45 4d 44  ){.  return MEMD
141c4 42 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  B;.}../*.** Chec
141c5 6b 20 74 68 61 74 20 74 68 65 72 65 20 61 72 65  k that there are
141c6 20 61 74 20 6c 65 61 73 74 20 6e 53 61 76 65 70   at least nSavep
141c7 6f 69 6e 74 20 73 61 76 65 70 6f 69 6e 74 73 20  oint savepoints 
141c8 6f 70 65 6e 2e 20 49 66 20 74 68 65 72 65 20 61  open. If there a
141c9 72 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20  re.** currently 
141ca 6c 65 73 73 20 74 68 61 6e 20 6e 53 61 76 65 70  less than nSavep
141cb 6f 69 6e 74 73 20 6f 70 65 6e 2c 20 74 68 65 6e  oints open, then
141cc 20 6f 70 65 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72   open one or mor
141cd 65 20 73 61 76 65 70 6f 69 6e 74 73 0a 2a 2a 20  e savepoints.** 
141ce 74 6f 20 6d 61 6b 65 20 75 70 20 74 68 65 20 64  to make up the d
141cf 69 66 66 65 72 65 6e 63 65 2e 20 49 66 20 74 68  ifference. If th
141d0 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65  e number of save
141d1 70 6f 69 6e 74 73 20 69 73 20 61 6c 72 65 61 64  points is alread
141d2 79 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6e 53  y.** equal to nS
141d3 61 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 74  avepoint, then t
141d4 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
141d5 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49  a no-op..**.** I
141d6 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  f a memory alloc
141d7 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 53 51 4c  ation fails, SQL
141d8 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74  ITE_NOMEM is ret
141d9 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72  urned. If an err
141da 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 20 77 68  or .** occurs wh
141db 69 6c 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 20  ile opening the 
141dc 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  sub-journal file
141dd 2c 20 74 68 65 6e 20 61 6e 20 49 4f 20 65 72 72  , then an IO err
141de 6f 72 20 63 6f 64 65 20 69 73 0a 2a 2a 20 72 65  or code is.** re
141df 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  turned. Otherwis
141e0 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f  e, SQLITE_OK..*/
141e1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
141e2 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
141e3 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 50 61  OpenSavepoint(Pa
141e4 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
141e5 20 6e 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20   nSavepoint){.  
141e6 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
141e7 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  OK;             
141e8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
141e9 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e  urn code */.  in
141ea 74 20 6e 43 75 72 72 65 6e 74 20 3d 20 70 50 61  t nCurrent = pPa
141eb 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b  ger->nSavepoint;
141ec 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
141ed 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 76  nt number of sav
141ee 65 70 6f 69 6e 74 73 20 2a 2f 0a 0a 20 20 69 66  epoints */..  if
141ef 28 20 6e 53 61 76 65 70 6f 69 6e 74 3e 6e 43 75  ( nSavepoint>nCu
141f0 72 72 65 6e 74 20 26 26 20 70 50 61 67 65 72 2d  rrent && pPager-
141f1 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20  >useJournal ){. 
141f2 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20     int ii;      
141f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
141f4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74             /* It
141f5 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20  erator variable 
141f6 2a 2f 0a 20 20 20 20 50 61 67 65 72 53 61 76 65  */.    PagerSave
141f7 70 6f 69 6e 74 20 2a 61 4e 65 77 3b 20 20 20 20  point *aNew;    
141f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
141f9 2a 20 4e 65 77 20 50 61 67 65 72 2e 61 53 61 76  * New Pager.aSav
141fa 65 70 6f 69 6e 74 20 61 72 72 61 79 20 2a 2f 0a  epoint array */.
141fb 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 74  .    /* Either t
141fc 68 65 72 65 20 69 73 20 6e 6f 20 61 63 74 69 76  here is no activ
141fd 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65  e journal or the
141fe 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20   sub-journal is 
141ff 6f 70 65 6e 20 6f 72 20 0a 20 20 20 20 2a 2a 20  open or .    ** 
14200 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61  the journal is a
14201 6c 77 61 79 73 20 73 74 6f 72 65 64 20 69 6e 20  lways stored in 
14202 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 20 20 61 73  memory */.    as
14203 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 53  sert( pPager->nS
14204 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 69  avepoint==0 || i
14205 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a  sOpen(pPager->sj
14206 66 64 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20  fd) ||.         
14207 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
14208 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f  alMode==PAGER_JO
14209 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59  URNALMODE_MEMORY
1420a 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 72 6f 77   );..    /* Grow
1420b 20 74 68 65 20 50 61 67 65 72 2e 61 53 61 76 65   the Pager.aSave
1420c 70 6f 69 6e 74 20 61 72 72 61 79 20 75 73 69 6e  point array usin
1420d 67 20 72 65 61 6c 6c 6f 63 28 29 2e 20 52 65 74  g realloc(). Ret
1420e 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
1420f 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 65 20 61  .    ** if the a
14210 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2e  llocation fails.
14211 20 4f 74 68 65 72 77 69 73 65 2c 20 7a 65 72 6f   Otherwise, zero
14212 20 74 68 65 20 6e 65 77 20 70 6f 72 74 69 6f 6e   the new portion
14213 20 69 6e 20 63 61 73 65 20 61 20 0a 20 20 20 20   in case a .    
14214 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72  ** malloc failur
14215 65 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 70  e occurs while p
14216 6f 70 75 6c 61 74 69 6e 67 20 69 74 20 69 6e 20  opulating it in 
14217 74 68 65 20 66 6f 72 28 2e 2e 2e 29 20 6c 6f 6f  the for(...) loo
14218 70 20 62 65 6c 6f 77 2e 0a 20 20 20 20 2a 2f 0a  p below..    */.
14219 20 20 20 20 61 4e 65 77 20 3d 20 28 50 61 67 65      aNew = (Page
1421a 72 53 61 76 65 70 6f 69 6e 74 20 2a 29 73 71 6c  rSavepoint *)sql
1421b 69 74 65 33 52 65 61 6c 6c 6f 63 28 0a 20 20 20  ite3Realloc(.   
1421c 20 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61       pPager->aSa
1421d 76 65 70 6f 69 6e 74 2c 20 73 69 7a 65 6f 66 28  vepoint, sizeof(
1421e 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 29 2a  PagerSavepoint)*
1421f 6e 53 61 76 65 70 6f 69 6e 74 0a 20 20 20 20 29  nSavepoint.    )
14220 3b 0a 20 20 20 20 69 66 28 20 21 61 4e 65 77 20  ;.    if( !aNew 
14221 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
14222 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
14223 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 26    }.    memset(&
14224 61 4e 65 77 5b 6e 43 75 72 72 65 6e 74 5d 2c 20  aNew[nCurrent], 
14225 30 2c 20 28 6e 53 61 76 65 70 6f 69 6e 74 2d 6e  0, (nSavepoint-n
14226 43 75 72 72 65 6e 74 29 20 2a 20 73 69 7a 65 6f  Current) * sizeo
14227 66 28 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74  f(PagerSavepoint
14228 29 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  ));.    pPager->
14229 61 53 61 76 65 70 6f 69 6e 74 20 3d 20 61 4e 65  aSavepoint = aNe
1422a 77 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e  w;.    pPager->n
1422b 53 61 76 65 70 6f 69 6e 74 20 3d 20 6e 53 61 76  Savepoint = nSav
1422c 65 70 6f 69 6e 74 3b 0a 0a 20 20 20 20 2f 2a 20  epoint;..    /* 
1422d 50 6f 70 75 6c 61 74 65 20 74 68 65 20 50 61 67  Populate the Pag
1422e 65 72 53 61 76 65 70 6f 69 6e 74 20 73 74 72 75  erSavepoint stru
1422f 63 74 75 72 65 73 20 6a 75 73 74 20 61 6c 6c 6f  ctures just allo
14230 63 61 74 65 64 2e 20 2a 2f 0a 20 20 20 20 66 6f  cated. */.    fo
14231 72 28 69 69 3d 6e 43 75 72 72 65 6e 74 3b 20 69  r(ii=nCurrent; i
14232 69 3c 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69  i<nSavepoint; ii
14233 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  ++){.      asser
14234 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  t( pPager->dbSiz
14235 65 56 61 6c 69 64 20 29 3b 0a 20 20 20 20 20 20  eValid );.      
14236 61 4e 65 77 5b 69 69 5d 2e 6e 4f 72 69 67 20 3d  aNew[ii].nOrig =
14237 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b   pPager->dbSize;
14238 0a 20 20 20 20 20 20 69 66 28 20 69 73 4f 70 65  .      if( isOpe
14239 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26  n(pPager->jfd) &
1423a 26 20 41 4c 57 41 59 53 28 70 50 61 67 65 72 2d  & ALWAYS(pPager-
1423b 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 29 20 29  >journalOff>0) )
1423c 7b 0a 20 20 20 20 20 20 20 20 61 4e 65 77 5b 69  {.        aNew[i
1423d 69 5d 2e 69 4f 66 66 73 65 74 20 3d 20 70 50 61  i].iOffset = pPa
1423e 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b  ger->journalOff;
1423f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
14240 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69        aNew[ii].i
14241 4f 66 66 73 65 74 20 3d 20 4a 4f 55 52 4e 41 4c  Offset = JOURNAL
14242 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b  _HDR_SZ(pPager);
14243 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
14244 4e 65 77 5b 69 69 5d 2e 69 53 75 62 52 65 63 20  New[ii].iSubRec 
14245 3d 20 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65  = pPager->nSubRe
14246 63 3b 0a 20 20 20 20 20 20 61 4e 65 77 5b 69 69  c;.      aNew[ii
14247 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 3d  ].pInSavepoint =
14248 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72   sqlite3BitvecCr
14249 65 61 74 65 28 70 50 61 67 65 72 2d 3e 64 62 53  eate(pPager->dbS
1424a 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ize);.      if( 
1424b 21 61 4e 65 77 5b 69 69 5d 2e 70 49 6e 53 61 76  !aNew[ii].pInSav
1424c 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 20 20  epoint ){.      
1424d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1424e 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20  NOMEM;.      }. 
1424f 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f 70 65     }..    /* Ope
14250 6e 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61  n the sub-journa
14251 6c 2c 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20  l, if it is not 
14252 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 2e 20  already opened. 
14253 2a 2f 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e  */.    rc = open
14254 53 75 62 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65  SubJournal(pPage
14255 72 29 3b 0a 20 20 20 20 61 73 73 65 72 74 54 72  r);.    assertTr
14256 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74  uncateConstraint
14257 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20  (pPager);.  }.. 
14258 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
14259 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
1425a 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  on is called to 
1425b 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 72 65 6c 65  rollback or rele
1425c 61 73 65 20 28 63 6f 6d 6d 69 74 29 20 61 20 73  ase (commit) a s
1425d 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 20 54 68 65  avepoint..** The
1425e 20 73 61 76 65 70 6f 69 6e 74 20 74 6f 20 72 65   savepoint to re
1425f 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63  lease or rollbac
14260 6b 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 74 68  k need not be th
14261 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
14262 0a 2a 2a 20 63 72 65 61 74 65 64 20 73 61 76 65  .** created save
14263 70 6f 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 50 61 72  point..**.** Par
14264 61 6d 65 74 65 72 20 6f 70 20 69 73 20 61 6c 77  ameter op is alw
14265 61 79 73 20 65 69 74 68 65 72 20 53 41 56 45 50  ays either SAVEP
14266 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 6f 72  OINT_ROLLBACK or
14267 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41   SAVEPOINT_RELEA
14268 53 45 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20  SE..** If it is 
14269 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53  SAVEPOINT_RELEAS
1426a 45 2c 20 74 68 65 6e 20 72 65 6c 65 61 73 65 20  E, then release 
1426b 61 6e 64 20 64 65 73 74 72 6f 79 20 74 68 65 20  and destroy the 
1426c 73 61 76 65 70 6f 69 6e 74 20 77 69 74 68 0a 2a  savepoint with.*
1426d 2a 20 69 6e 64 65 78 20 69 53 61 76 65 70 6f 69  * index iSavepoi
1426e 6e 74 2e 20 49 66 20 69 74 20 69 73 20 53 41 56  nt. If it is SAV
1426f 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c  EPOINT_ROLLBACK,
14270 20 74 68 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 61   then rollback a
14271 6c 6c 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74 68  ll changes.** th
14272 61 74 20 68 61 76 65 20 6f 63 63 75 72 72 65 64  at have occurred
14273 20 73 69 6e 63 65 20 74 68 65 20 73 70 65 63 69   since the speci
14274 66 69 65 64 20 73 61 76 65 70 6f 69 6e 74 20 77  fied savepoint w
14275 61 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a  as created..**.*
14276 2a 20 54 68 65 20 73 61 76 65 70 6f 69 6e 74 20  * The savepoint 
14277 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 72  to rollback or r
14278 65 6c 65 61 73 65 20 69 73 20 69 64 65 6e 74 69  elease is identi
14279 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65  fied by paramete
1427a 72 20 0a 2a 2a 20 69 53 61 76 65 70 6f 69 6e 74  r .** iSavepoint
1427b 2e 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20 6d  . A value of 0 m
1427c 65 61 6e 73 20 74 6f 20 6f 70 65 72 61 74 65 20  eans to operate 
1427d 6f 6e 20 74 68 65 20 6f 75 74 65 72 6d 6f 73 74  on the outermost
1427e 20 73 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 28 74   savepoint.** (t
1427f 68 65 20 66 69 72 73 74 20 63 72 65 61 74 65 64  he first created
14280 29 2e 20 41 20 76 61 6c 75 65 20 6f 66 20 28 50  ). A value of (P
14281 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e 74 2d  ager.nSavepoint-
14282 31 29 20 6d 65 61 6e 73 20 6f 70 65 72 61 74 65  1) means operate
14283 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 6f 73 74 20  .** on the most 
14284 72 65 63 65 6e 74 6c 79 20 63 72 65 61 74 65 64  recently created
14285 20 73 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 69   savepoint. If i
14286 53 61 76 65 70 6f 69 6e 74 20 69 73 20 67 72 65  Savepoint is gre
14287 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 28 50 61  ater than.** (Pa
14288 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e 74 2d 31  ger.nSavepoint-1
14289 29 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e  ), then this fun
1428a 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
1428b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6e 65 67  ..**.** If a neg
1428c 61 74 69 76 65 20 76 61 6c 75 65 20 69 73 20 70  ative value is p
1428d 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75  assed to this fu
1428e 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65  nction, then the
1428f 20 63 75 72 72 65 6e 74 0a 2a 2a 20 74 72 61 6e   current.** tran
14290 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65  saction is rolle
14291 64 20 62 61 63 6b 2e 20 54 68 69 73 20 69 73 20  d back. This is 
14292 64 69 66 66 65 72 65 6e 74 20 74 6f 20 63 61 6c  different to cal
14293 6c 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 33  ling .** sqlite3
14294 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 20  PagerRollback() 
14295 62 65 63 61 75 73 65 20 74 68 69 73 20 66 75 6e  because this fun
14296 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 74  ction does not t
14297 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 74 68 65 20  erminate.** the 
14298 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 72 20 75  transaction or u
14299 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61  nlock the databa
1429a 73 65 2c 20 69 74 20 6a 75 73 74 20 72 65 73 74  se, it just rest
1429b 6f 72 65 73 20 74 68 65 20 0a 2a 2a 20 63 6f 6e  ores the .** con
1429c 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64 61 74  tents of the dat
1429d 61 62 61 73 65 20 74 6f 20 69 74 73 20 6f 72 69  abase to its ori
1429e 67 69 6e 61 6c 20 73 74 61 74 65 2e 20 0a 2a 2a  ginal state. .**
1429f 0a 2a 2a 20 49 6e 20 61 6e 79 20 63 61 73 65 2c  .** In any case,
142a0 20 61 6c 6c 20 73 61 76 65 70 6f 69 6e 74 73 20   all savepoints 
142a1 77 69 74 68 20 61 6e 20 69 6e 64 65 78 20 67 72  with an index gr
142a2 65 61 74 65 72 20 74 68 61 6e 20 69 53 61 76 65  eater than iSave
142a3 70 6f 69 6e 74 20 0a 2a 2a 20 61 72 65 20 64 65  point .** are de
142a4 73 74 72 6f 79 65 64 2e 20 49 66 20 74 68 69 73  stroyed. If this
142a5 20 69 73 20 61 20 72 65 6c 65 61 73 65 20 6f 70   is a release op
142a6 65 72 61 74 69 6f 6e 20 28 6f 70 3d 3d 53 41 56  eration (op==SAV
142a7 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 2c  EPOINT_RELEASE),
142a8 0a 2a 2a 20 74 68 65 6e 20 73 61 76 65 70 6f 69  .** then savepoi
142a9 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73  nt iSavepoint is
142aa 20 61 6c 73 6f 20 64 65 73 74 72 6f 79 65 64 2e   also destroyed.
142ab 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
142ac 74 69 6f 6e 20 6d 61 79 20 72 65 74 75 72 6e 20  tion may return 
142ad 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20  SQLITE_NOMEM if 
142ae 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
142af 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a 2a 20 6f 72  ion fails,.** or
142b0 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64   an IO error cod
142b1 65 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f 72  e if an IO error
142b2 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 6f   occurs while ro
142b3 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 0a 2a 2a  lling back a .**
142b4 20 73 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 6e   savepoint. If n
142b5 6f 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20  o errors occur, 
142b6 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
142b7 75 72 6e 65 64 2e 0a 2a 2f 20 0a 53 51 4c 49 54  urned..*/ .SQLIT
142b8 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
142b9 6c 69 74 65 33 50 61 67 65 72 53 61 76 65 70 6f  lite3PagerSavepo
142ba 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65  int(Pager *pPage
142bb 72 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 69  r, int op, int i
142bc 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 6e  Savepoint){.  in
142bd 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
142be 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3d  ;..  assert( op=
142bf 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41  =SAVEPOINT_RELEA
142c0 53 45 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f  SE || op==SAVEPO
142c1 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a  INT_ROLLBACK );.
142c2 20 20 61 73 73 65 72 74 28 20 69 53 61 76 65 70    assert( iSavep
142c3 6f 69 6e 74 3e 3d 30 20 7c 7c 20 6f 70 3d 3d 53  oint>=0 || op==S
142c4 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
142c5 4b 20 29 3b 0a 0a 20 20 69 66 28 20 69 53 61 76  K );..  if( iSav
142c6 65 70 6f 69 6e 74 3c 70 50 61 67 65 72 2d 3e 6e  epoint<pPager->n
142c7 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20  Savepoint ){.   
142c8 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20   int ii;        
142c9 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20      /* Iterator 
142ca 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20  variable */.    
142cb 69 6e 74 20 6e 4e 65 77 3b 20 20 20 20 20 20 20  int nNew;       
142cc 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
142cd 72 65 6d 61 69 6e 69 6e 67 20 73 61 76 65 70 6f  remaining savepo
142ce 69 6e 74 73 20 61 66 74 65 72 20 74 68 69 73 20  ints after this 
142cf 6f 70 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46  op. */..    /* F
142d0 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61  igure out how ma
142d1 6e 79 20 73 61 76 65 70 6f 69 6e 74 73 20 77 69  ny savepoints wi
142d2 6c 6c 20 73 74 69 6c 6c 20 62 65 20 61 63 74 69  ll still be acti
142d3 76 65 20 61 66 74 65 72 20 74 68 69 73 0a 20 20  ve after this.  
142d4 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e 20    ** operation. 
142d5 53 74 6f 72 65 20 74 68 69 73 20 76 61 6c 75 65  Store this value
142d6 20 69 6e 20 6e 4e 65 77 2e 20 54 68 65 6e 20 66   in nNew. Then f
142d7 72 65 65 20 72 65 73 6f 75 72 63 65 73 20 61 73  ree resources as
142d8 73 6f 63 69 61 74 65 64 20 0a 20 20 20 20 2a 2a  sociated .    **
142d9 20 77 69 74 68 20 61 6e 79 20 73 61 76 65 70 6f   with any savepo
142da 69 6e 74 73 20 74 68 61 74 20 61 72 65 20 64 65  ints that are de
142db 73 74 72 6f 79 65 64 20 62 79 20 74 68 69 73 20  stroyed by this 
142dc 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20 20 20 2a  operation..    *
142dd 2f 0a 20 20 20 20 6e 4e 65 77 20 3d 20 69 53 61  /.    nNew = iSa
142de 76 65 70 6f 69 6e 74 20 2b 20 28 6f 70 3d 3d 53  vepoint + (op==S
142df 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
142e0 4b 29 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 6e  K);.    for(ii=n
142e1 4e 65 77 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e  New; ii<pPager->
142e2 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b  nSavepoint; ii++
142e3 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
142e4 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50  BitvecDestroy(pP
142e5 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74  ager->aSavepoint
142e6 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e  [ii].pInSavepoin
142e7 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  t);.    }.    pP
142e8 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74  ager->nSavepoint
142e9 20 3d 20 6e 4e 65 77 3b 0a 0a 20 20 20 20 2f 2a   = nNew;..    /*
142ea 20 49 66 20 74 68 69 73 20 69 73 20 61 20 72 6f   If this is a ro
142eb 6c 6c 62 61 63 6b 20 6f 70 65 72 61 74 69 6f 6e  llback operation
142ec 2c 20 70 6c 61 79 62 61 63 6b 20 74 68 65 20 73  , playback the s
142ed 70 65 63 69 66 69 65 64 20 73 61 76 65 70 6f 69  pecified savepoi
142ee 6e 74 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68  nt..    ** If th
142ef 69 73 20 69 73 20 61 20 74 65 6d 70 2d 66 69 6c  is is a temp-fil
142f0 65 2c 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c  e, it is possibl
142f1 65 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e  e that the journ
142f2 61 6c 20 66 69 6c 65 20 68 61 73 0a 20 20 20 20  al file has.    
142f3 2a 2a 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20  ** not yet been 
142f4 6f 70 65 6e 65 64 2e 20 49 6e 20 74 68 69 73 20  opened. In this 
142f5 63 61 73 65 20 74 68 65 72 65 20 68 61 76 65 20  case there have 
142f6 62 65 65 6e 20 6e 6f 20 63 68 61 6e 67 65 73 20  been no changes 
142f7 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61  to.    ** the da
142f8 74 61 62 61 73 65 20 66 69 6c 65 2c 20 73 6f 20  tabase file, so 
142f9 74 68 65 20 70 6c 61 79 62 61 63 6b 20 6f 70 65  the playback ope
142fa 72 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 73 6b  ration can be sk
142fb 69 70 70 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  ipped..    */.  
142fc 20 20 69 66 28 20 6f 70 3d 3d 53 41 56 45 50 4f    if( op==SAVEPO
142fd 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20  INT_ROLLBACK && 
142fe 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
142ff 66 64 29 20 29 7b 0a 20 20 20 20 20 20 50 61 67  fd) ){.      Pag
14300 65 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61  erSavepoint *pSa
14301 76 65 70 6f 69 6e 74 20 3d 20 28 6e 4e 65 77 3d  vepoint = (nNew=
14302 3d 30 29 3f 30 3a 26 70 50 61 67 65 72 2d 3e 61  =0)?0:&pPager->a
14303 53 61 76 65 70 6f 69 6e 74 5b 6e 4e 65 77 2d 31  Savepoint[nNew-1
14304 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61  ];.      rc = pa
14305 67 65 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70  gerPlaybackSavep
14306 6f 69 6e 74 28 70 50 61 67 65 72 2c 20 70 53 61  oint(pPager, pSa
14307 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20  vepoint);.      
14308 61 73 73 65 72 74 28 72 63 21 3d 53 51 4c 49 54  assert(rc!=SQLIT
14309 45 5f 44 4f 4e 45 29 3b 0a 20 20 20 20 7d 0a 20  E_DONE);.    }. 
1430a 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73   .    /* If this
1430b 20 69 73 20 61 20 72 65 6c 65 61 73 65 20 6f 66   is a release of
1430c 20 74 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73   the outermost s
1430d 61 76 65 70 6f 69 6e 74 2c 20 74 72 75 6e 63 61  avepoint, trunca
1430e 74 65 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 73  te .    ** the s
1430f 75 62 2d 6a 6f 75 72 6e 61 6c 20 74 6f 20 7a 65  ub-journal to ze
14310 72 6f 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  ro bytes in size
14311 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 4e 65  . */.    if( nNe
14312 77 3d 3d 30 20 26 26 20 6f 70 3d 3d 53 41 56 45  w==0 && op==SAVE
14313 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 26 26  POINT_RELEASE &&
14314 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
14315 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20 61  sjfd) ){.      a
14316 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
14317 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 72 63  E_OK );.      rc
14318 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e   = sqlite3OsTrun
14319 63 61 74 65 28 70 50 61 67 65 72 2d 3e 73 6a 66  cate(pPager->sjf
1431a 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 50 61  d, 0);.      pPa
1431b 67 65 72 2d 3e 6e 53 75 62 52 65 63 20 3d 20 30  ger->nSubRec = 0
1431c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
1431d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1431e 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c  * Return the ful
1431f 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68  l pathname of th
14320 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
14321 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
14322 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  TE const char *s
14323 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e  qlite3PagerFilen
14324 61 6d 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ame(Pager *pPage
14325 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61  r){.  return pPa
14326 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 3b 0a  ger->zFilename;.
14327 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
14328 74 68 65 20 56 46 53 20 73 74 72 75 63 74 75 72  the VFS structur
14329 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 72 2e  e for the pager.
1432a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1432b 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  TE const sqlite3
1432c 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67  _vfs *sqlite3Pag
1432d 65 72 56 66 73 28 50 61 67 65 72 20 2a 70 50 61  erVfs(Pager *pPa
1432e 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  ger){.  return p
1432f 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 7d 0a 0a  Pager->pVfs;.}..
14330 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
14331 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72   file handle for
14332 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
14333 6c 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a  le associated.**
14334 20 77 69 74 68 20 74 68 65 20 70 61 67 65 72 2e   with the pager.
14335 20 20 54 68 69 73 20 6d 69 67 68 74 20 72 65 74    This might ret
14336 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 20  urn NULL if the 
14337 66 69 6c 65 20 68 61 73 0a 2a 2a 20 6e 6f 74 20  file has.** not 
14338 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e  yet been opened.
14339 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1433a 54 45 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  TE sqlite3_file 
1433b 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c  *sqlite3PagerFil
1433c 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  e(Pager *pPager)
1433d 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  {.  return pPage
1433e 72 2d 3e 66 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  r->fd;.}../*.** 
1433f 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20  Return the full 
14340 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20  pathname of the 
14341 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f  journal file..*/
14342 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14343 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
14344 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 6e  te3PagerJournaln
14345 61 6d 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ame(Pager *pPage
14346 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61  r){.  return pPa
14347 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a 7d  ger->zJournal;.}
14348 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
14349 72 75 65 20 69 66 20 66 73 79 6e 63 28 29 20 63  rue if fsync() c
1434a 61 6c 6c 73 20 61 72 65 20 64 69 73 61 62 6c 65  alls are disable
1434b 64 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72  d for this pager
1434c 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 0a  .  Return FALSE.
1434d 2a 2a 20 69 66 20 66 73 79 6e 63 28 29 73 20 61  ** if fsync()s a
1434e 72 65 20 65 78 65 63 75 74 65 64 20 6e 6f 72 6d  re executed norm
1434f 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ally..*/.SQLITE_
14350 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
14351 74 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 50  te3PagerNosync(P
14352 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
14353 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e   return pPager->
14354 6e 6f 53 79 6e 63 3b 0a 7d 0a 0a 23 69 66 64 65  noSync;.}..#ifde
14355 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44  f SQLITE_HAS_COD
14356 45 43 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 20  EC./*.** Set or 
14357 72 65 74 72 69 65 76 65 20 74 68 65 20 63 6f 64  retrieve the cod
14358 65 63 20 66 6f 72 20 74 68 69 73 20 70 61 67 65  ec for this page
14359 72 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  r.*/.static void
1435a 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
1435b 43 6f 64 65 63 28 0a 20 20 50 61 67 65 72 20 2a  Codec(.  Pager *
1435c 70 50 61 67 65 72 2c 0a 20 20 76 6f 69 64 20 2a  pPager,.  void *
1435d 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c  (*xCodec)(void*,
1435e 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c  void*,Pgno,int),
1435f 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f 64 65 63  .  void (*xCodec
14360 53 69 7a 65 43 68 6e 67 29 28 76 6f 69 64 2a 2c  SizeChng)(void*,
14361 69 6e 74 2c 69 6e 74 29 2c 0a 20 20 76 6f 69 64  int,int),.  void
14362 20 28 2a 78 43 6f 64 65 63 46 72 65 65 29 28 76   (*xCodecFree)(v
14363 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 20 2a 70  oid*),.  void *p
14364 43 6f 64 65 63 0a 29 7b 0a 20 20 69 66 28 20 70  Codec.){.  if( p
14365 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65  Pager->xCodecFre
14366 65 20 29 20 70 50 61 67 65 72 2d 3e 78 43 6f 64  e ) pPager->xCod
14367 65 63 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70  ecFree(pPager->p
14368 43 6f 64 65 63 29 3b 0a 20 20 70 50 61 67 65 72  Codec);.  pPager
14369 2d 3e 78 43 6f 64 65 63 20 3d 20 78 43 6f 64 65  ->xCodec = xCode
1436a 63 3b 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 6f  c;.  pPager->xCo
1436b 64 65 63 53 69 7a 65 43 68 6e 67 20 3d 20 78 43  decSizeChng = xC
1436c 6f 64 65 63 53 69 7a 65 43 68 6e 67 3b 0a 20 20  odecSizeChng;.  
1436d 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72  pPager->xCodecFr
1436e 65 65 20 3d 20 78 43 6f 64 65 63 46 72 65 65 3b  ee = xCodecFree;
1436f 0a 20 20 70 50 61 67 65 72 2d 3e 70 43 6f 64 65  .  pPager->pCode
14370 63 20 3d 20 70 43 6f 64 65 63 3b 0a 20 20 70 61  c = pCodec;.  pa
14371 67 65 72 52 65 70 6f 72 74 53 69 7a 65 28 70 50  gerReportSize(pP
14372 61 67 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20  ager);.}.static 
14373 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67  void *sqlite3Pag
14374 65 72 47 65 74 43 6f 64 65 63 28 50 61 67 65 72  erGetCodec(Pager
14375 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74   *pPager){.  ret
14376 75 72 6e 20 70 50 61 67 65 72 2d 3e 70 43 6f 64  urn pPager->pCod
14377 65 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ec;.}.#endif..#i
14378 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
14379 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a  T_AUTOVACUUM./*.
1437a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65  ** Move the page
1437b 20 70 50 67 20 74 6f 20 6c 6f 63 61 74 69 6f 6e   pPg to location
1437c 20 70 67 6e 6f 20 69 6e 20 74 68 65 20 66 69 6c   pgno in the fil
1437d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d  e..**.** There m
1437e 75 73 74 20 62 65 20 6e 6f 20 72 65 66 65 72 65  ust be no refere
1437f 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61 67 65  nces to the page
14380 20 70 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61   previously loca
14381 74 65 64 20 61 74 0a 2a 2a 20 70 67 6e 6f 20 28  ted at.** pgno (
14382 77 68 69 63 68 20 77 65 20 63 61 6c 6c 20 70 50  which we call pP
14383 67 4f 6c 64 29 20 74 68 6f 75 67 68 20 74 68 61  gOld) though tha
14384 74 20 70 61 67 65 20 69 73 20 61 6c 6c 6f 77 65  t page is allowe
14385 64 20 74 6f 20 62 65 0a 2a 2a 20 69 6e 20 63 61  d to be.** in ca
14386 63 68 65 2e 20 20 49 66 20 74 68 65 20 70 61 67  che.  If the pag
14387 65 20 70 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63  e previously loc
14388 61 74 65 64 20 61 74 20 70 67 6e 6f 20 69 73 20  ated at pgno is 
14389 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69  not already.** i
1438a 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a  n the rollback j
1438b 6f 75 72 6e 61 6c 2c 20 69 74 20 69 73 20 6e 6f  ournal, it is no
1438c 74 20 70 75 74 20 74 68 65 72 65 20 62 79 20 62  t put there by b
1438d 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a  y this routine..
1438e 2a 2a 0a 2a 2a 20 52 65 66 65 72 65 6e 63 65 73  **.** References
1438f 20 74 6f 20 74 68 65 20 70 61 67 65 20 70 50 67   to the page pPg
14390 20 72 65 6d 61 69 6e 20 76 61 6c 69 64 2e 20 55   remain valid. U
14391 70 64 61 74 69 6e 67 20 61 6e 79 0a 2a 2a 20 6d  pdating any.** m
14392 65 74 61 2d 64 61 74 61 20 61 73 73 6f 63 69 61  eta-data associa
14393 74 65 64 20 77 69 74 68 20 70 50 67 20 28 69 2e  ted with pPg (i.
14394 65 2e 20 64 61 74 61 20 73 74 6f 72 65 64 20 69  e. data stored i
14395 6e 20 74 68 65 20 6e 45 78 74 72 61 20 62 79 74  n the nExtra byt
14396 65 73 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  es.** allocated 
14397 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 70  along with the p
14398 61 67 65 29 20 69 73 20 74 68 65 20 72 65 73 70  age) is the resp
14399 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68  onsibility of th
1439a 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20  e caller..**.** 
1439b 41 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75  A transaction mu
1439c 73 74 20 62 65 20 61 63 74 69 76 65 20 77 68 65  st be active whe
1439d 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
1439e 73 20 63 61 6c 6c 65 64 2e 20 49 74 20 75 73 65  s called. It use
1439f 64 20 74 6f 20 62 65 0a 2a 2a 20 72 65 71 75 69  d to be.** requi
143a0 72 65 64 20 74 68 61 74 20 61 20 73 74 61 74 65  red that a state
143a1 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
143a2 20 77 61 73 20 6e 6f 74 20 61 63 74 69 76 65 2c   was not active,
143a3 20 62 75 74 20 74 68 69 73 20 72 65 73 74 72 69   but this restri
143a4 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 65 65  ction.** has bee
143a5 6e 20 72 65 6d 6f 76 65 64 20 28 43 52 45 41 54  n removed (CREAT
143a6 45 20 49 4e 44 45 58 20 6e 65 65 64 73 20 74 6f  E INDEX needs to
143a7 20 6d 6f 76 65 20 61 20 70 61 67 65 20 77 68 65   move a page whe
143a8 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  n a statement.**
143a9 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
143aa 61 63 74 69 76 65 29 2e 0a 2a 2a 0a 2a 2a 20 49  active)..**.** I
143ab 66 20 74 68 65 20 66 6f 75 72 74 68 20 61 72 67  f the fourth arg
143ac 75 6d 65 6e 74 2c 20 69 73 43 6f 6d 6d 69 74 2c  ument, isCommit,
143ad 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68   is non-zero, th
143ae 65 6e 20 74 68 69 73 20 70 61 67 65 20 69 73 20  en this page is 
143af 62 65 69 6e 67 0a 2a 2a 20 6d 6f 76 65 64 20 61  being.** moved a
143b0 73 20 70 61 72 74 20 6f 66 20 61 20 64 61 74 61  s part of a data
143b1 62 61 73 65 20 72 65 6f 72 67 61 6e 69 7a 61 74  base reorganizat
143b2 69 6f 6e 20 6a 75 73 74 20 62 65 66 6f 72 65 20  ion just before 
143b3 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
143b4 0a 2a 2a 20 69 73 20 62 65 69 6e 67 20 63 6f 6d  .** is being com
143b5 6d 69 74 74 65 64 2e 20 49 6e 20 74 68 69 73 20  mitted. In this 
143b6 63 61 73 65 2c 20 69 74 20 69 73 20 67 75 61 72  case, it is guar
143b7 61 6e 74 65 65 64 20 74 68 61 74 20 74 68 65 20  anteed that the 
143b8 64 61 74 61 62 61 73 65 20 70 61 67 65 20 0a 2a  database page .*
143b9 2a 20 70 50 67 20 72 65 66 65 72 73 20 74 6f 20  * pPg refers to 
143ba 77 69 6c 6c 20 6e 6f 74 20 62 65 20 77 72 69 74  will not be writ
143bb 74 65 6e 20 74 6f 20 61 67 61 69 6e 20 77 69 74  ten to again wit
143bc 68 69 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63  hin this transac
143bd 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tion..**.** This
143be 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65   function may re
143bf 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
143c0 4d 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72  M or an IO error
143c1 20 63 6f 64 65 20 69 66 20 61 6e 20 65 72 72 6f   code if an erro
143c2 72 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 4f 74 68  r.** occurs. Oth
143c3 65 72 77 69 73 65 2c 20 69 74 20 72 65 74 75 72  erwise, it retur
143c4 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f  ns SQLITE_OK..*/
143c5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
143c6 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
143c7 4d 6f 76 65 70 61 67 65 28 50 61 67 65 72 20 2a  Movepage(Pager *
143c8 70 50 61 67 65 72 2c 20 44 62 50 61 67 65 20 2a  pPager, DbPage *
143c9 70 50 67 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20  pPg, Pgno pgno, 
143ca 69 6e 74 20 69 73 43 6f 6d 6d 69 74 29 7b 0a 20  int isCommit){. 
143cb 20 50 67 48 64 72 20 2a 70 50 67 4f 6c 64 3b 20   PgHdr *pPgOld; 
143cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
143cd 20 54 68 65 20 70 61 67 65 20 62 65 69 6e 67 20   The page being 
143ce 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 2a 2f 0a  overwritten. */.
143cf 20 20 50 67 6e 6f 20 6e 65 65 64 53 79 6e 63 50    Pgno needSyncP
143d0 67 6e 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  gno = 0;       /
143d1 2a 20 4f 6c 64 20 76 61 6c 75 65 20 6f 66 20 70  * Old value of p
143d2 50 67 2d 3e 70 67 6e 6f 2c 20 69 66 20 73 79 6e  Pg->pgno, if syn
143d3 63 20 69 73 20 72 65 71 75 69 72 65 64 20 2a 2f  c is required */
143d4 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
143d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
143d6 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
143d7 2f 0a 20 20 50 67 6e 6f 20 6f 72 69 67 50 67 6e  /.  Pgno origPgn
143d8 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o;              
143d9 20 2f 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c   /* The original
143da 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a   page number */.
143db 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e  .  assert( pPg->
143dc 6e 52 65 66 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20  nRef>0 );..  /* 
143dd 49 66 20 74 68 65 20 70 61 67 65 20 62 65 69 6e  If the page bein
143de 67 20 6d 6f 76 65 64 20 69 73 20 64 69 72 74 79  g moved is dirty
143df 20 61 6e 64 20 68 61 73 20 6e 6f 74 20 62 65 65   and has not bee
143e0 6e 20 73 61 76 65 64 20 62 79 20 74 68 65 20 6c  n saved by the l
143e1 61 74 65 73 74 0a 20 20 2a 2a 20 73 61 76 65 70  atest.  ** savep
143e2 6f 69 6e 74 2c 20 74 68 65 6e 20 73 61 76 65 20  oint, then save 
143e3 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74  the current cont
143e4 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65  ents of the page
143e5 20 69 6e 74 6f 20 74 68 65 20 0a 20 20 2a 2a 20   into the .  ** 
143e6 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 6e 6f 77 2e  sub-journal now.
143e7 20 54 68 69 73 20 69 73 20 72 65 71 75 69 72 65   This is require
143e8 64 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 20  d to handle the 
143e9 66 6f 6c 6c 6f 77 69 6e 67 20 73 63 65 6e 61 72  following scenar
143ea 69 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  io:.  **.  **   
143eb 42 45 47 49 4e 3b 0a 20 20 2a 2a 20 20 20 20 20  BEGIN;.  **     
143ec 3c 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 58 2c  <journal page X,
143ed 20 74 68 65 6e 20 6d 6f 64 69 66 79 20 69 74 20   then modify it 
143ee 69 6e 20 6d 65 6d 6f 72 79 3e 0a 20 20 2a 2a 20  in memory>.  ** 
143ef 20 20 20 20 53 41 56 45 50 4f 49 4e 54 20 6f 6e      SAVEPOINT on
143f0 65 3b 0a 20 20 2a 2a 20 20 20 20 20 20 20 3c 4d  e;.  **       <M
143f1 6f 76 65 20 70 61 67 65 20 58 20 74 6f 20 6c 6f  ove page X to lo
143f2 63 61 74 69 6f 6e 20 59 3e 0a 20 20 2a 2a 20 20  cation Y>.  **  
143f3 20 20 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f     ROLLBACK TO o
143f4 6e 65 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  ne;.  **.  ** If
143f5 20 70 61 67 65 20 58 20 77 65 72 65 20 6e 6f 74   page X were not
143f6 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
143f7 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 68 65 72 65  sub-journal here
143f8 2c 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74 0a 20  , it would not. 
143f9 20 2a 2a 20 62 65 20 70 6f 73 73 69 62 6c 65 20   ** be possible 
143fa 74 6f 20 72 65 73 74 6f 72 65 20 69 74 73 20 63  to restore its c
143fb 6f 6e 74 65 6e 74 73 20 77 68 65 6e 20 74 68 65  ontents when the
143fc 20 22 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e   "ROLLBACK TO on
143fd 65 22 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e  e".  ** statemen
143fe 74 20 77 65 72 65 20 69 73 20 70 72 6f 63 65 73  t were is proces
143ff 73 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 73  sed..  **.  ** s
14400 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 29 20  ubjournalPage() 
14401 6d 61 79 20 6e 65 65 64 20 74 6f 20 61 6c 6c 6f  may need to allo
14402 63 61 74 65 20 73 70 61 63 65 20 74 6f 20 73 74  cate space to st
14403 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f 20 69 6e  ore pPg->pgno in
14404 74 6f 0a 20 20 2a 2a 20 6f 6e 65 20 6f 72 20 6d  to.  ** one or m
14405 6f 72 65 20 73 61 76 65 70 6f 69 6e 74 20 62 69  ore savepoint bi
14406 74 76 65 63 73 2e 20 54 68 69 73 20 69 73 20 74  tvecs. This is t
14407 68 65 20 72 65 61 73 6f 6e 20 74 68 69 73 20 66  he reason this f
14408 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 6d 61 79  unction.  ** may
14409 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
1440a 4f 4d 45 4d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  OMEM..  */.  if(
1440b 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44   pPg->flags&PGHD
1440c 52 5f 44 49 52 54 59 20 0a 20 20 20 26 26 20 73  R_DIRTY .   && s
1440d 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28  ubjRequiresPage(
1440e 70 50 67 29 0a 20 20 20 26 26 20 53 51 4c 49 54  pPg).   && SQLIT
1440f 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 75 62 6a  E_OK!=(rc = subj
14410 6f 75 72 6e 61 6c 50 61 67 65 28 70 50 67 29 29  ournalPage(pPg))
14411 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  .  ){.    return
14412 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45   rc;.  }..  PAGE
14413 52 54 52 41 43 45 28 28 22 4d 4f 56 45 20 25 64  RTRACE(("MOVE %d
14414 20 70 61 67 65 20 25 64 20 28 6e 65 65 64 53 79   page %d (needSy
14415 6e 63 3d 25 64 29 20 6d 6f 76 65 73 20 74 6f 20  nc=%d) moves to 
14416 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 50 41  %d\n", .      PA
14417 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70  GERID(pPager), p
14418 50 67 2d 3e 70 67 6e 6f 2c 20 28 70 50 67 2d 3e  Pg->pgno, (pPg->
14419 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44  flags&PGHDR_NEED
1441a 5f 53 59 4e 43 29 3f 31 3a 30 2c 20 70 67 6e 6f  _SYNC)?1:0, pgno
1441b 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22  ));.  IOTRACE(("
1441c 4d 4f 56 45 20 25 70 20 25 64 20 25 64 5c 6e 22  MOVE %p %d %d\n"
1441d 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70  , pPager, pPg->p
1441e 67 6e 6f 2c 20 70 67 6e 6f 29 29 0a 0a 20 20 2f  gno, pgno))..  /
1441f 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  * If the journal
14420 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e   needs to be syn
14421 63 28 29 65 64 20 62 65 66 6f 72 65 20 70 61 67  c()ed before pag
14422 65 20 70 50 67 2d 3e 70 67 6e 6f 20 63 61 6e 0a  e pPg->pgno can.
14423 20 20 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20    ** be written 
14424 74 6f 2c 20 73 74 6f 72 65 20 70 50 67 2d 3e 70  to, store pPg->p
14425 67 6e 6f 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72  gno in local var
14426 69 61 62 6c 65 20 6e 65 65 64 53 79 6e 63 50 67  iable needSyncPg
14427 6e 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  no..  **.  ** If
14428 20 74 68 65 20 69 73 43 6f 6d 6d 69 74 20 66 6c   the isCommit fl
14429 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 72 65  ag is set, there
1442a 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 72   is no need to r
1442b 65 6d 65 6d 62 65 72 20 74 68 61 74 0a 20 20 2a  emember that.  *
1442c 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65  * the journal ne
1442d 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 28 29  eds to be sync()
1442e 65 64 20 62 65 66 6f 72 65 20 64 61 74 61 62 61  ed before databa
1442f 73 65 20 70 61 67 65 20 70 50 67 2d 3e 70 67 6e  se page pPg->pgn
14430 6f 20 0a 20 20 2a 2a 20 63 61 6e 20 62 65 20 77  o .  ** can be w
14431 72 69 74 74 65 6e 20 74 6f 2e 20 54 68 65 20 63  ritten to. The c
14432 61 6c 6c 65 72 20 68 61 73 20 61 6c 72 65 61 64  aller has alread
14433 79 20 70 72 6f 6d 69 73 65 64 20 6e 6f 74 20 74  y promised not t
14434 6f 20 77 72 69 74 65 20 74 6f 20 69 74 2e 0a 20  o write to it.. 
14435 20 2a 2f 0a 20 20 69 66 28 20 28 70 50 67 2d 3e   */.  if( (pPg->
14436 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44  flags&PGHDR_NEED
14437 5f 53 59 4e 43 29 20 26 26 20 21 69 73 43 6f 6d  _SYNC) && !isCom
14438 6d 69 74 20 29 7b 0a 20 20 20 20 6e 65 65 64 53  mit ){.    needS
14439 79 6e 63 50 67 6e 6f 20 3d 20 70 50 67 2d 3e 70  yncPgno = pPg->p
1443a 67 6e 6f 3b 0a 20 20 20 20 61 73 73 65 72 74 28  gno;.    assert(
1443b 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70   pageInJournal(p
1443c 50 67 29 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f  Pg) || pPg->pgno
1443d 3e 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53  >pPager->dbOrigS
1443e 69 7a 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ize );.    asser
1443f 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47  t( pPg->flags&PG
14440 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 20 20 20  HDR_DIRTY );.   
14441 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
14442 3e 6e 65 65 64 53 79 6e 63 20 29 3b 0a 20 20 7d  >needSync );.  }
14443 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 61  ..  /* If the ca
14444 63 68 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70  che contains a p
14445 61 67 65 20 77 69 74 68 20 70 61 67 65 2d 6e 75  age with page-nu
14446 6d 62 65 72 20 70 67 6e 6f 2c 20 72 65 6d 6f 76  mber pgno, remov
14447 65 20 69 74 0a 20 20 2a 2a 20 66 72 6f 6d 20 69  e it.  ** from i
14448 74 73 20 68 61 73 68 20 63 68 61 69 6e 2e 20 41  ts hash chain. A
14449 6c 73 6f 2c 20 69 66 20 74 68 65 20 50 67 48 64  lso, if the PgHd
1444a 72 2e 6e 65 65 64 53 79 6e 63 20 77 61 73 20 73  r.needSync was s
1444b 65 74 20 66 6f 72 20 0a 20 20 2a 2a 20 70 61 67  et for .  ** pag
1444c 65 20 70 67 6e 6f 20 62 65 66 6f 72 65 20 74 68  e pgno before th
1444d 65 20 27 6d 6f 76 65 27 20 6f 70 65 72 61 74 69  e 'move' operati
1444e 6f 6e 2c 20 69 74 20 6e 65 65 64 73 20 74 6f 20  on, it needs to 
1444f 62 65 20 72 65 74 61 69 6e 65 64 20 0a 20 20 2a  be retained .  *
14450 2a 20 66 6f 72 20 74 68 65 20 70 61 67 65 20 6d  * for the page m
14451 6f 76 65 64 20 74 68 65 72 65 2e 0a 20 20 2a 2f  oved there..  */
14452 0a 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 26 3d  .  pPg->flags &=
14453 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e   ~PGHDR_NEED_SYN
14454 43 3b 0a 20 20 70 50 67 4f 6c 64 20 3d 20 70 61  C;.  pPgOld = pa
14455 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65  ger_lookup(pPage
14456 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 61 73 73 65  r, pgno);.  asse
14457 72 74 28 20 21 70 50 67 4f 6c 64 20 7c 7c 20 70  rt( !pPgOld || p
14458 50 67 4f 6c 64 2d 3e 6e 52 65 66 3d 3d 31 20 29  PgOld->nRef==1 )
14459 3b 0a 20 20 69 66 28 20 70 50 67 4f 6c 64 20 29  ;.  if( pPgOld )
1445a 7b 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73  {.    pPg->flags
1445b 20 7c 3d 20 28 70 50 67 4f 6c 64 2d 3e 66 6c 61   |= (pPgOld->fla
1445c 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
1445d 4e 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  NC);.    sqlite3
1445e 50 63 61 63 68 65 44 72 6f 70 28 70 50 67 4f 6c  PcacheDrop(pPgOl
1445f 64 29 3b 0a 20 20 7d 0a 0a 20 20 6f 72 69 67 50  d);.  }..  origP
14460 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b  gno = pPg->pgno;
14461 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  .  sqlite3Pcache
14462 4d 6f 76 65 28 70 50 67 2c 20 70 67 6e 6f 29 3b  Move(pPg, pgno);
14463 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  .  sqlite3Pcache
14464 4d 61 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a  MakeDirty(pPg);.
14465 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69    pPager->dbModi
14466 66 69 65 64 20 3d 20 31 3b 0a 0a 20 20 69 66 28  fied = 1;..  if(
14467 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 29 7b   needSyncPgno ){
14468 0a 20 20 20 20 2f 2a 20 49 66 20 6e 65 65 64 53  .    /* If needS
14469 79 6e 63 50 67 6e 6f 20 69 73 20 6e 6f 6e 2d 7a  yncPgno is non-z
1446a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f  ero, then the jo
1446b 75 72 6e 61 6c 20 66 69 6c 65 20 6e 65 65 64 73  urnal file needs
1446c 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a 20 73   to be .    ** s
1446d 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65 20 61  ync()ed before a
1446e 6e 79 20 64 61 74 61 20 69 73 20 77 72 69 74 74  ny data is writt
1446f 65 6e 20 74 6f 20 64 61 74 61 62 61 73 65 20 66  en to database f
14470 69 6c 65 20 70 61 67 65 20 6e 65 65 64 53 79 6e  ile page needSyn
14471 63 50 67 6e 6f 2e 0a 20 20 20 20 2a 2a 20 43 75  cPgno..    ** Cu
14472 72 72 65 6e 74 6c 79 2c 20 6e 6f 20 73 75 63 68  rrently, no such
14473 20 70 61 67 65 20 65 78 69 73 74 73 20 69 6e 20   page exists in 
14474 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 61  the page-cache a
14475 6e 64 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 22  nd the .    ** "
14476 69 73 20 6a 6f 75 72 6e 61 6c 65 64 22 20 62 69  is journaled" bi
14477 74 76 65 63 20 66 6c 61 67 20 68 61 73 20 62 65  tvec flag has be
14478 65 6e 20 73 65 74 2e 20 54 68 69 73 20 6e 65 65  en set. This nee
14479 64 73 20 74 6f 20 62 65 20 72 65 6d 65 64 69 65  ds to be remedie
1447a 64 20 62 79 0a 20 20 20 20 2a 2a 20 6c 6f 61 64  d by.    ** load
1447b 69 6e 67 20 74 68 65 20 70 61 67 65 20 69 6e 74  ing the page int
1447c 6f 20 74 68 65 20 70 61 67 65 72 2d 63 61 63 68  o the pager-cach
1447d 65 20 61 6e 64 20 73 65 74 74 69 6e 67 20 74 68  e and setting th
1447e 65 20 50 67 48 64 72 2e 6e 65 65 64 53 79 6e 63  e PgHdr.needSync
1447f 20 0a 20 20 20 20 2a 2a 20 66 6c 61 67 2e 0a 20   .    ** flag.. 
14480 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20     **.    ** If 
14481 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f 20 6c  the attempt to l
14482 6f 61 64 20 74 68 65 20 70 61 67 65 20 69 6e 74  oad the page int
14483 6f 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65  o the page-cache
14484 20 66 61 69 6c 73 2c 20 28 64 75 65 0a 20 20 20   fails, (due.   
14485 20 2a 2a 20 74 6f 20 61 20 6d 61 6c 6c 6f 63 28   ** to a malloc(
14486 29 20 6f 72 20 49 4f 20 66 61 69 6c 75 72 65 29  ) or IO failure)
14487 2c 20 63 6c 65 61 72 20 74 68 65 20 62 69 74 20  , clear the bit 
14488 69 6e 20 74 68 65 20 70 49 6e 4a 6f 75 72 6e 61  in the pInJourna
14489 6c 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72 61 79  l[].    ** array
1448a 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  . Otherwise, if 
1448b 74 68 65 20 70 61 67 65 20 69 73 20 6c 6f 61 64  the page is load
1448c 65 64 20 61 6e 64 20 77 72 69 74 74 65 6e 20 61  ed and written a
1448d 67 61 69 6e 20 69 6e 0a 20 20 20 20 2a 2a 20 74  gain in.    ** t
1448e 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  his transaction,
1448f 20 69 74 20 6d 61 79 20 62 65 20 77 72 69 74 74   it may be writt
14490 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  en to the databa
14491 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 0a 20  se file before. 
14492 20 20 20 2a 2a 20 69 74 20 69 73 20 73 79 6e 63     ** it is sync
14493 65 64 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72  ed into the jour
14494 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 77  nal file. This w
14495 61 79 2c 20 69 74 20 6d 61 79 20 65 6e 64 20 75  ay, it may end u
14496 70 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20  p in.    ** the 
14497 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 77 69  journal file twi
14498 63 65 2c 20 62 75 74 20 74 68 61 74 20 69 73 20  ce, but that is 
14499 6e 6f 74 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20  not a problem.. 
1449a 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
1449b 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
1449c 28 29 20 63 61 6c 6c 20 6d 61 79 20 63 61 75 73  () call may caus
1449d 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 6f  e the journal to
1449e 20 73 79 6e 63 2e 20 53 6f 20 6d 61 6b 65 0a 20   sync. So make. 
1449f 20 20 20 2a 2a 20 73 75 72 65 20 74 68 65 20 50     ** sure the P
144a0 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c  ager.needSync fl
144a1 61 67 20 69 73 20 73 65 74 20 74 6f 6f 2e 0a 20  ag is set too.. 
144a2 20 20 20 2a 2f 0a 20 20 20 20 50 67 48 64 72 20     */.    PgHdr 
144a3 2a 70 50 67 48 64 72 3b 0a 20 20 20 20 61 73 73  *pPgHdr;.    ass
144a4 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 65  ert( pPager->nee
144a5 64 53 79 6e 63 20 29 3b 0a 20 20 20 20 72 63 20  dSync );.    rc 
144a6 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
144a7 74 28 70 50 61 67 65 72 2c 20 6e 65 65 64 53 79  t(pPager, needSy
144a8 6e 63 50 67 6e 6f 2c 20 26 70 50 67 48 64 72 29  ncPgno, &pPgHdr)
144a9 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
144aa 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
144ab 20 69 66 28 20 6e 65 65 64 53 79 6e 63 50 67 6e   if( needSyncPgn
144ac 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69  o<=pPager->dbOri
144ad 67 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20  gSize ){.       
144ae 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
144af 3e 70 54 6d 70 53 70 61 63 65 21 3d 30 20 29 3b  >pTmpSpace!=0 );
144b0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
144b1 42 69 74 76 65 63 43 6c 65 61 72 28 70 50 61 67  BitvecClear(pPag
144b2 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20  er->pInJournal, 
144b3 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2c 20 70 50  needSyncPgno, pP
144b4 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29  ager->pTmpSpace)
144b5 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
144b6 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
144b7 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65  .    pPager->nee
144b8 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 61  dSync = 1;.    a
144b9 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e  ssert( pPager->n
144ba 6f 53 79 6e 63 3d 3d 30 20 26 26 20 21 4d 45 4d  oSync==0 && !MEM
144bb 44 42 20 29 3b 0a 20 20 20 20 70 50 67 48 64 72  DB );.    pPgHdr
144bc 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52  ->flags |= PGHDR
144bd 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20  _NEED_SYNC;.    
144be 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b  sqlite3PcacheMak
144bf 65 44 69 72 74 79 28 70 50 67 48 64 72 29 3b 0a  eDirty(pPgHdr);.
144c0 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
144c1 55 6e 72 65 66 28 70 50 67 48 64 72 29 3b 0a 20  Unref(pPgHdr);. 
144c2 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 6f   }..  /*.  ** Fo
144c3 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64  r an in-memory d
144c4 61 74 61 62 61 73 65 2c 20 6d 61 6b 65 20 73 75  atabase, make su
144c5 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  re the original 
144c6 70 61 67 65 20 63 6f 6e 74 69 6e 75 65 73 0a 20  page continues. 
144c7 20 2a 2a 20 74 6f 20 65 78 69 73 74 2c 20 69 6e   ** to exist, in
144c8 20 63 61 73 65 20 74 68 65 20 74 72 61 6e 73 61   case the transa
144c9 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 72  ction needs to r
144ca 6f 6c 6c 20 62 61 63 6b 2e 20 20 57 65 20 61 6c  oll back.  We al
144cb 6c 6f 63 61 74 65 0a 20 20 2a 2a 20 74 68 65 20  locate.  ** the 
144cc 70 61 67 65 20 6e 6f 77 2c 20 69 6e 73 74 65 61  page now, instea
144cd 64 20 6f 66 20 61 74 20 72 6f 6c 6c 62 61 63 6b  d of at rollback
144ce 2c 20 62 65 63 61 75 73 65 20 77 65 20 63 61 6e  , because we can
144cf 20 62 65 74 74 65 72 20 64 65 61 6c 0a 20 20 2a   better deal.  *
144d0 2a 20 77 69 74 68 20 61 6e 20 6f 75 74 2d 6f 66  * with an out-of
144d1 2d 6d 65 6d 6f 72 79 20 65 72 72 6f 72 20 6e 6f  -memory error no
144d2 77 2e 20 20 54 69 63 6b 65 74 20 23 33 37 36 31  w.  Ticket #3761
144d3 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4d 45 4d  ..  */.  if( MEM
144d4 44 42 20 29 7b 0a 20 20 20 20 44 62 50 61 67 65  DB ){.    DbPage
144d5 20 2a 70 4e 65 77 3b 0a 20 20 20 20 72 63 20 3d   *pNew;.    rc =
144d6 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71   sqlite3PagerAcq
144d7 75 69 72 65 28 70 50 61 67 65 72 2c 20 6f 72 69  uire(pPager, ori
144d8 67 50 67 6e 6f 2c 20 26 70 4e 65 77 2c 20 31 29  gPgno, &pNew, 1)
144d9 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
144da 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
144db 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f   sqlite3PcacheMo
144dc 76 65 28 70 50 67 2c 20 6f 72 69 67 50 67 6e 6f  ve(pPg, origPgno
144dd 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
144de 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  rc;.    }.    sq
144df 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
144e0 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  pNew);.  }..  re
144e1 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
144e2 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
144e3 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
144e4 20 74 6f 20 74 68 65 20 64 61 74 61 20 66 6f 72   to the data for
144e5 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 70   the specified p
144e6 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  age..*/.SQLITE_P
144e7 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
144e8 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61  ite3PagerGetData
144e9 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20  (DbPage *pPg){. 
144ea 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52   assert( pPg->nR
144eb 65 66 3e 30 20 7c 7c 20 70 50 67 2d 3e 70 50 61  ef>0 || pPg->pPa
144ec 67 65 72 2d 3e 6d 65 6d 44 62 20 29 3b 0a 20 20  ger->memDb );.  
144ed 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 44 61 74  return pPg->pDat
144ee 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  a;.}../*.** Retu
144ef 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
144f0 74 68 65 20 50 61 67 65 72 2e 6e 45 78 74 72 61  the Pager.nExtra
144f1 20 62 79 74 65 73 20 6f 66 20 22 65 78 74 72 61   bytes of "extra
144f2 22 20 73 70 61 63 65 20 0a 2a 2a 20 61 6c 6c 6f  " space .** allo
144f3 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68  cated along with
144f4 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 70   the specified p
144f5 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  age..*/.SQLITE_P
144f6 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
144f7 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72  ite3PagerGetExtr
144f8 61 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a  a(DbPage *pPg){.
144f9 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 45    return pPg->pE
144fa 78 74 72 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  xtra;.}../*.** G
144fb 65 74 2f 73 65 74 20 74 68 65 20 6c 6f 63 6b 69  et/set the locki
144fc 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 74 68 69 73  ng-mode for this
144fd 20 70 61 67 65 72 2e 20 50 61 72 61 6d 65 74 65   pager. Paramete
144fe 72 20 65 4d 6f 64 65 20 6d 75 73 74 20 62 65 20  r eMode must be 
144ff 6f 6e 65 0a 2a 2a 20 6f 66 20 50 41 47 45 52 5f  one.** of PAGER_
14500 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52  LOCKINGMODE_QUER
14501 59 2c 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  Y, PAGER_LOCKING
14502 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 6f 72 20 0a  MODE_NORMAL or .
14503 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  ** PAGER_LOCKING
14504 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 2e 20  MODE_EXCLUSIVE. 
14505 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  If the parameter
14506 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20   is not _QUERY, 
14507 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b  then.** the lock
14508 69 6e 67 2d 6d 6f 64 65 20 69 73 20 73 65 74 20  ing-mode is set 
14509 74 6f 20 74 68 65 20 76 61 6c 75 65 20 73 70 65  to the value spe
1450a 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  cified..**.** Th
1450b 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65  e returned value
1450c 20 69 73 20 65 69 74 68 65 72 20 50 41 47 45 52   is either PAGER
1450d 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52  _LOCKINGMODE_NOR
1450e 4d 41 4c 20 6f 72 0a 2a 2a 20 50 41 47 45 52 5f  MAL or.** PAGER_
1450f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c  LOCKINGMODE_EXCL
14510 55 53 49 56 45 2c 20 69 6e 64 69 63 61 74 69 6e  USIVE, indicatin
14511 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 28 70  g the current (p
14512 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 64 29  ossibly updated)
14513 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65  .** locking-mode
14514 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
14515 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
14516 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28  agerLockingMode(
14517 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
14518 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73  nt eMode){.  ass
14519 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45  ert( eMode==PAGE
1451a 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55  R_LOCKINGMODE_QU
1451b 45 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20  ERY.            
1451c 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  || eMode==PAGER_
1451d 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d  LOCKINGMODE_NORM
1451e 41 4c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  AL.            |
1451f 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c  | eMode==PAGER_L
14520 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55  OCKINGMODE_EXCLU
14521 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65 72 74  SIVE );.  assert
14522 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  ( PAGER_LOCKINGM
14523 4f 44 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a 20  ODE_QUERY<0 );. 
14524 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 4c   assert( PAGER_L
14525 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41  OCKINGMODE_NORMA
14526 4c 3e 3d 30 20 26 26 20 50 41 47 45 52 5f 4c 4f  L>=0 && PAGER_LO
14527 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53  CKINGMODE_EXCLUS
14528 49 56 45 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20  IVE>=0 );.  if( 
14529 65 4d 6f 64 65 3e 3d 30 20 26 26 20 21 70 50 61  eMode>=0 && !pPa
1452a 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b  ger->tempFile ){
1452b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 78 63  .    pPager->exc
1452c 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 28 75 38  lusiveMode = (u8
1452d 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20 20 72 65  )eMode;.  }.  re
1452e 74 75 72 6e 20 28 69 6e 74 29 70 50 61 67 65 72  turn (int)pPager
1452f 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 3b  ->exclusiveMode;
14530 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65  .}../*.** Get/se
14531 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f  t the journal-mo
14532 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65  de for this page
14533 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f  r. Parameter eMo
14534 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f  de must be one o
14535 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 50 41 47 45  f:.**.**    PAGE
14536 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55  R_JOURNALMODE_QU
14537 45 52 59 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f  ERY.**    PAGER_
14538 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45  JOURNALMODE_DELE
14539 54 45 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a  TE.**    PAGER_J
1453a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43  OURNALMODE_TRUNC
1453b 41 54 45 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f  ATE.**    PAGER_
1453c 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53  JOURNALMODE_PERS
1453d 49 53 54 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f  IST.**    PAGER_
1453e 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a  JOURNALMODE_OFF.
1453f 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52  **    PAGER_JOUR
14540 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 0a 2a  NALMODE_MEMORY.*
14541 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 72 61  *.** If the para
14542 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 5f 51 55  meter is not _QU
14543 45 52 59 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f  ERY, then the jo
14544 75 72 6e 61 6c 5f 6d 6f 64 65 20 69 73 20 73 65  urnal_mode is se
14545 74 20 74 6f 20 74 68 65 0a 2a 2a 20 76 61 6c 75  t to the.** valu
14546 65 20 73 70 65 63 69 66 69 65 64 20 69 66 20 74  e specified if t
14547 68 65 20 63 68 61 6e 67 65 20 69 73 20 61 6c 6c  he change is all
14548 6f 77 65 64 2e 20 20 54 68 65 20 63 68 61 6e 67  owed.  The chang
14549 65 20 69 73 20 64 69 73 61 6c 6c 6f 77 65 64 0a  e is disallowed.
1454a 2a 2a 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f  ** for the follo
1454b 77 69 6e 67 20 72 65 61 73 6f 6e 73 3a 0a 2a 2a  wing reasons:.**
1454c 0a 2a 2a 20 20 20 2a 20 20 41 6e 20 69 6e 2d 6d  .**   *  An in-m
1454d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 63  emory database c
1454e 61 6e 20 6f 6e 6c 79 20 68 61 76 65 20 69 74 73  an only have its
1454f 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 73 65   journal_mode se
14550 74 20 74 6f 20 5f 4f 46 46 0a 2a 2a 20 20 20 20  t to _OFF.**    
14551 20 20 6f 72 20 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a    or _MEMORY..**
14552 0a 2a 2a 20 20 20 2a 20 20 54 68 65 20 6a 6f 75  .**   *  The jou
14553 72 6e 61 6c 20 6d 6f 64 65 20 6d 61 79 20 6e 6f  rnal mode may no
14554 74 20 62 65 20 63 68 61 6e 67 65 64 20 77 68 69  t be changed whi
14555 6c 65 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  le a transaction
14556 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a   is active..**.*
14557 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 69  * The returned i
14558 6e 64 69 63 61 74 65 20 74 68 65 20 63 75 72 72  ndicate the curr
14559 65 6e 74 20 28 70 6f 73 73 69 62 6c 79 20 75 70  ent (possibly up
1455a 64 61 74 65 64 29 20 6a 6f 75 72 6e 61 6c 2d 6d  dated) journal-m
1455b 6f 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ode..*/.SQLITE_P
1455c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1455d 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f  e3PagerJournalMo
1455e 64 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  de(Pager *pPager
1455f 2c 20 69 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20  , int eMode){.  
14560 61 73 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50  assert( eMode==P
14561 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
14562 5f 51 55 45 52 59 0a 20 20 20 20 20 20 20 20 20  _QUERY.         
14563 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47     || eMode==PAG
14564 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44  ER_JOURNALMODE_D
14565 45 4c 45 54 45 0a 20 20 20 20 20 20 20 20 20 20  ELETE.          
14566 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45    || eMode==PAGE
14567 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52  R_JOURNALMODE_TR
14568 55 4e 43 41 54 45 0a 20 20 20 20 20 20 20 20 20  UNCATE.         
14569 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47     || eMode==PAG
1456a 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50  ER_JOURNALMODE_P
1456b 45 52 53 49 53 54 0a 20 20 20 20 20 20 20 20 20  ERSIST.         
1456c 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47     || eMode==PAG
1456d 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f  ER_JOURNALMODE_O
1456e 46 46 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  FF .            
1456f 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  || eMode==PAGER_
14570 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f  JOURNALMODE_MEMO
14571 52 59 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  RY );.  assert( 
14572 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
14573 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a 20 20 69  E_QUERY<0 );.  i
14574 66 28 20 65 4d 6f 64 65 3e 3d 30 0a 20 20 20 26  f( eMode>=0.   &
14575 26 20 28 21 4d 45 4d 44 42 20 7c 7c 20 65 4d 6f  & (!MEMDB || eMo
14576 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
14577 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20  LMODE_MEMORY .  
14578 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65              || e
14579 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
1457a 4e 41 4c 4d 4f 44 45 5f 4f 46 46 29 0a 20 20 20  NALMODE_OFF).   
1457b 26 26 20 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f  && !pPager->dbMo
1457c 64 69 66 69 65 64 0a 20 20 20 26 26 20 28 21 69  dified.   && (!i
1457d 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
1457e 64 29 20 7c 7c 20 30 3d 3d 70 50 61 67 65 72 2d  d) || 0==pPager-
1457f 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 0a 20 20 29  >journalOff).  )
14580 7b 0a 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e  {.    if( isOpen
14581 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b  (pPager->jfd) ){
14582 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  .      sqlite3Os
14583 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66  Close(pPager->jf
14584 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  d);.    }.    pP
14585 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
14586 65 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20  e = (u8)eMode;. 
14587 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74   }.  return (int
14588 29 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c  )pPager->journal
14589 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  Mode;.}../*.** G
1458a 65 74 2f 73 65 74 20 74 68 65 20 73 69 7a 65 2d  et/set the size-
1458b 6c 69 6d 69 74 20 75 73 65 64 20 66 6f 72 20 70  limit used for p
1458c 65 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61  ersistent journa
1458d 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53  l files..**.** S
1458e 65 74 74 69 6e 67 20 74 68 65 20 73 69 7a 65 20  etting the size 
1458f 6c 69 6d 69 74 20 74 6f 20 2d 31 20 6d 65 61 6e  limit to -1 mean
14590 73 20 6e 6f 20 6c 69 6d 69 74 20 69 73 20 65 6e  s no limit is en
14591 66 6f 72 63 65 64 2e 0a 2a 2a 20 41 6e 20 61 74  forced..** An at
14592 74 65 6d 70 74 20 74 6f 20 73 65 74 20 61 20 6c  tempt to set a l
14593 69 6d 69 74 20 73 6d 61 6c 6c 65 72 20 74 68 61  imit smaller tha
14594 6e 20 2d 31 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  n -1 is a no-op.
14595 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
14596 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 50 61  TE i64 sqlite3Pa
14597 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69  gerJournalSizeLi
14598 6d 69 74 28 50 61 67 65 72 20 2a 70 50 61 67 65  mit(Pager *pPage
14599 72 2c 20 69 36 34 20 69 4c 69 6d 69 74 29 7b 0a  r, i64 iLimit){.
1459a 20 20 69 66 28 20 69 4c 69 6d 69 74 3e 3d 2d 31    if( iLimit>=-1
1459b 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   ){.    pPager->
1459c 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74  journalSizeLimit
1459d 20 3d 20 69 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20   = iLimit;.  }. 
1459e 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e   return pPager->
1459f 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74  journalSizeLimit
145a0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
145a1 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
145a2 68 65 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b  he pPager->pBack
145a3 75 70 20 76 61 72 69 61 62 6c 65 2e 20 54 68 65  up variable. The
145a4 20 62 61 63 6b 75 70 20 6d 6f 64 75 6c 65 0a 2a   backup module.*
145a5 2a 20 69 6e 20 62 61 63 6b 75 70 2e 63 20 6d 61  * in backup.c ma
145a6 69 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e 74  intains the cont
145a7 65 6e 74 20 6f 66 20 74 68 69 73 20 76 61 72 69  ent of this vari
145a8 61 62 6c 65 2e 20 54 68 69 73 20 6d 6f 64 75 6c  able. This modul
145a9 65 0a 2a 2a 20 75 73 65 73 20 69 74 20 6f 70 61  e.** uses it opa
145aa 71 75 65 6c 79 20 61 73 20 61 6e 20 61 72 67 75  quely as an argu
145ab 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 42  ment to sqlite3B
145ac 61 63 6b 75 70 52 65 73 74 61 72 74 28 29 20 61  ackupRestart() a
145ad 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 42 61 63  nd.** sqlite3Bac
145ae 6b 75 70 55 70 64 61 74 65 28 29 20 6f 6e 6c 79  kupUpdate() only
145af 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
145b0 41 54 45 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  ATE sqlite3_back
145b1 75 70 20 2a 2a 73 71 6c 69 74 65 33 50 61 67 65  up **sqlite3Page
145b2 72 42 61 63 6b 75 70 50 74 72 28 50 61 67 65 72  rBackupPtr(Pager
145b3 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74   *pPager){.  ret
145b4 75 72 6e 20 26 70 50 61 67 65 72 2d 3e 70 42 61  urn &pPager->pBa
145b5 63 6b 75 70 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  ckup;.}..#endif 
145b6 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  /* SQLITE_OMIT_D
145b7 49 53 4b 49 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  ISKIO */../*****
145b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
145b9 20 70 61 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a   pager.c *******
145ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
145bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
145bc 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
145bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
145be 66 69 6c 65 20 62 74 6d 75 74 65 78 2e 63 20 2a  file btmutex.c *
145bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
145c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
145c1 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
145c2 32 30 30 37 20 41 75 67 75 73 74 20 32 37 0a 2a  2007 August 27.*
145c3 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
145c4 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
145c5 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
145c6 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
145c7 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
145c8 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
145c9 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
145ca 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
145cb 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
145cc 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
145cd 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
145ce 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
145cf 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
145d0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
145d1 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
145d2 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
145d3 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
145d4 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
145d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
145d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
145d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
145d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
145d9 0a 2a 2a 20 24 49 64 3a 20 62 74 6d 75 74 65 78  .** $Id: btmutex
145da 2e 63 2c 76 20 31 2e 31 37 20 32 30 30 39 2f 30  .c,v 1.17 2009/0
145db 37 2f 32 30 20 31 32 3a 33 33 3a 33 33 20 64 72  7/20 12:33:33 dr
145dc 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68  h Exp $.**.** Th
145dd 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
145de 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d   code used to im
145df 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20  plement mutexes 
145e0 6f 6e 20 42 74 72 65 65 20 6f 62 6a 65 63 74 73  on Btree objects
145e1 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 72  ..** This code r
145e2 65 61 6c 6c 79 20 62 65 6c 6f 6e 67 73 20 69 6e  eally belongs in
145e3 20 62 74 72 65 65 2e 63 2e 20 20 42 75 74 20 62   btree.c.  But b
145e4 74 72 65 65 2e 63 20 69 73 20 67 65 74 74 69 6e  tree.c is gettin
145e5 67 20 74 6f 6f 0a 2a 2a 20 62 69 67 20 61 6e 64  g too.** big and
145e6 20 77 65 20 77 61 6e 74 20 74 6f 20 62 72 65 61   we want to brea
145e7 6b 20 69 74 20 64 6f 77 6e 20 73 6f 6d 65 2e 20  k it down some. 
145e8 20 54 68 69 73 20 70 61 63 6b 61 67 65 64 20 73   This packaged s
145e9 65 65 6d 65 64 20 6c 69 6b 65 0a 2a 2a 20 61 20  eemed like.** a 
145ea 67 6f 6f 64 20 62 72 65 61 6b 6f 75 74 2e 0a 2a  good breakout..*
145eb 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
145ec 2a 20 49 6e 63 6c 75 64 65 20 62 74 72 65 65 49  * Include btreeI
145ed 6e 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  nt.h in the midd
145ee 6c 65 20 6f 66 20 62 74 6d 75 74 65 78 2e 63 20  le of btmutex.c 
145ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
145f0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
145f1 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 74 72  * Begin file btr
145f2 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  eeInt.h ********
145f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
145f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
145f5 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72  /./*.** 2004 Apr
145f6 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  il 6.**.** The a
145f7 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
145f8 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
145f9 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
145fa 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
145fb 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
145fc 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
145fd 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
145fe 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
145ff 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
14600 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
14601 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
14602 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
14603 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
14604 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
14605 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
14606 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
14607 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
14608 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14609 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1460a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1460b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1460c 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 74 72 65  ***.** $Id: btre
1460d 65 49 6e 74 2e 68 2c 76 20 31 2e 35 32 20 32 30  eInt.h,v 1.52 20
1460e 30 39 2f 30 37 2f 31 35 20 31 37 3a 32 35 3a 34  09/07/15 17:25:4
1460f 36 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a  6 drh Exp $.**.*
14610 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c  * This file impl
14611 65 6d 65 6e 74 73 20 61 20 65 78 74 65 72 6e 61  ements a externa
14612 6c 20 28 64 69 73 6b 2d 62 61 73 65 64 29 20 64  l (disk-based) d
14613 61 74 61 62 61 73 65 20 75 73 69 6e 67 20 42 54  atabase using BT
14614 72 65 65 73 2e 0a 2a 2a 20 46 6f 72 20 61 20 64  rees..** For a d
14615 65 74 61 69 6c 65 64 20 64 69 73 63 75 73 73 69  etailed discussi
14616 6f 6e 20 6f 66 20 42 54 72 65 65 73 2c 20 72 65  on of BTrees, re
14617 66 65 72 20 74 6f 0a 2a 2a 0a 2a 2a 20 20 20 20  fer to.**.**    
14618 20 44 6f 6e 61 6c 64 20 45 2e 20 4b 6e 75 74 68   Donald E. Knuth
14619 2c 20 54 48 45 20 41 52 54 20 4f 46 20 43 4f 4d  , THE ART OF COM
1461a 50 55 54 45 52 20 50 52 4f 47 52 41 4d 4d 49 4e  PUTER PROGRAMMIN
1461b 47 2c 20 56 6f 6c 75 6d 65 20 33 3a 0a 2a 2a 20  G, Volume 3:.** 
1461c 20 20 20 20 22 53 6f 72 74 69 6e 67 20 41 6e 64      "Sorting And
1461d 20 53 65 61 72 63 68 69 6e 67 22 2c 20 70 61 67   Searching", pag
1461e 65 73 20 34 37 33 2d 34 38 30 2e 20 41 64 64 69  es 473-480. Addi
1461f 73 6f 6e 2d 57 65 73 6c 65 79 0a 2a 2a 20 20 20  son-Wesley.**   
14620 20 20 50 75 62 6c 69 73 68 69 6e 67 20 43 6f 6d    Publishing Com
14621 70 61 6e 79 2c 20 52 65 61 64 69 6e 67 2c 20 4d  pany, Reading, M
14622 61 73 73 61 63 68 75 73 65 74 74 73 2e 0a 2a 2a  assachusetts..**
14623 0a 2a 2a 20 54 68 65 20 62 61 73 69 63 20 69 64  .** The basic id
14624 65 61 20 69 73 20 74 68 61 74 20 65 61 63 68 20  ea is that each 
14625 70 61 67 65 20 6f 66 20 74 68 65 20 66 69 6c 65  page of the file
14626 20 63 6f 6e 74 61 69 6e 73 20 4e 20 64 61 74 61   contains N data
14627 62 61 73 65 0a 2a 2a 20 65 6e 74 72 69 65 73 20  base.** entries 
14628 61 6e 64 20 4e 2b 31 20 70 6f 69 6e 74 65 72 73  and N+1 pointers
14629 20 74 6f 20 73 75 62 70 61 67 65 73 2e 0a 2a 2a   to subpages..**
1462a 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .**   ----------
1462b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1462c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1462d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1462e 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 7c 20 20 50  ------.**   |  P
1462f 74 72 28 30 29 20 7c 20 4b 65 79 28 30 29 20 7c  tr(0) | Key(0) |
14630 20 50 74 72 28 31 29 20 7c 20 4b 65 79 28 31 29   Ptr(1) | Key(1)
14631 20 7c 20 2e 2e 2e 20 7c 20 4b 65 79 28 4e 2d 31   | ... | Key(N-1
14632 29 20 7c 20 50 74 72 28 4e 29 20 7c 0a 2a 2a 20  ) | Ptr(N) |.** 
14633 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
14634 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14635 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14636 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14637 2d 2d 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20  --.**.** All of 
14638 74 68 65 20 6b 65 79 73 20 6f 6e 20 74 68 65 20  the keys on the 
14639 70 61 67 65 20 74 68 61 74 20 50 74 72 28 30 29  page that Ptr(0)
1463a 20 70 6f 69 6e 74 73 20 74 6f 20 68 61 76 65 20   points to have 
1463b 76 61 6c 75 65 73 20 6c 65 73 73 0a 2a 2a 20 74  values less.** t
1463c 68 61 6e 20 4b 65 79 28 30 29 2e 20 20 41 6c 6c  han Key(0).  All
1463d 20 6f 66 20 74 68 65 20 6b 65 79 73 20 6f 6e 20   of the keys on 
1463e 70 61 67 65 20 50 74 72 28 31 29 20 61 6e 64 20  page Ptr(1) and 
1463f 69 74 73 20 73 75 62 70 61 67 65 73 20 68 61 76  its subpages hav
14640 65 0a 2a 2a 20 76 61 6c 75 65 73 20 67 72 65 61  e.** values grea
14641 74 65 72 20 74 68 61 6e 20 4b 65 79 28 30 29 20  ter than Key(0) 
14642 61 6e 64 20 6c 65 73 73 20 74 68 61 6e 20 4b 65  and less than Ke
14643 79 28 31 29 2e 20 20 41 6c 6c 20 6f 66 20 74 68  y(1).  All of th
14644 65 20 6b 65 79 73 0a 2a 2a 20 6f 6e 20 50 74 72  e keys.** on Ptr
14645 28 4e 29 20 61 6e 64 20 69 74 73 20 73 75 62 70  (N) and its subp
14646 61 67 65 73 20 68 61 76 65 20 76 61 6c 75 65 73  ages have values
14647 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 4b 65   greater than Ke
14648 79 28 4e 2d 31 29 2e 20 20 41 6e 64 0a 2a 2a 20  y(N-1).  And.** 
14649 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20  so forth..**.** 
1464a 46 69 6e 64 69 6e 67 20 61 20 70 61 72 74 69 63  Finding a partic
1464b 75 6c 61 72 20 6b 65 79 20 72 65 71 75 69 72 65  ular key require
1464c 73 20 72 65 61 64 69 6e 67 20 4f 28 6c 6f 67 28  s reading O(log(
1464d 4d 29 29 20 70 61 67 65 73 20 66 72 6f 6d 20 74  M)) pages from t
1464e 68 65 20 0a 2a 2a 20 64 69 73 6b 20 77 68 65 72  he .** disk wher
1464f 65 20 4d 20 69 73 20 74 68 65 20 6e 75 6d 62 65  e M is the numbe
14650 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
14651 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20  the tree..**.** 
14652 49 6e 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e  In this implemen
14653 74 61 74 69 6f 6e 2c 20 61 20 73 69 6e 67 6c 65  tation, a single
14654 20 66 69 6c 65 20 63 61 6e 20 68 6f 6c 64 20 6f   file can hold o
14655 6e 65 20 6f 72 20 6d 6f 72 65 20 73 65 70 61 72  ne or more separ
14656 61 74 65 20 0a 2a 2a 20 42 54 72 65 65 73 2e 20  ate .** BTrees. 
14657 20 45 61 63 68 20 42 54 72 65 65 20 69 73 20 69   Each BTree is i
14658 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65  dentified by the
14659 20 69 6e 64 65 78 20 6f 66 20 69 74 73 20 72 6f   index of its ro
1465a 6f 74 20 70 61 67 65 2e 20 20 54 68 65 0a 2a 2a  ot page.  The.**
1465b 20 6b 65 79 20 61 6e 64 20 64 61 74 61 20 66 6f   key and data fo
1465c 72 20 61 6e 79 20 65 6e 74 72 79 20 61 72 65 20  r any entry are 
1465d 63 6f 6d 62 69 6e 65 64 20 74 6f 20 66 6f 72 6d  combined to form
1465e 20 74 68 65 20 22 70 61 79 6c 6f 61 64 22 2e 20   the "payload". 
1465f 20 41 0a 2a 2a 20 66 69 78 65 64 20 61 6d 6f 75   A.** fixed amou
14660 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 63 61  nt of payload ca
14661 6e 20 62 65 20 63 61 72 72 69 65 64 20 64 69 72  n be carried dir
14662 65 63 74 6c 79 20 6f 6e 20 74 68 65 20 64 61 74  ectly on the dat
14663 61 62 61 73 65 0a 2a 2a 20 70 61 67 65 2e 20 20  abase.** page.  
14664 49 66 20 74 68 65 20 70 61 79 6c 6f 61 64 20 69  If the payload i
14665 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  s larger than th
14666 65 20 70 72 65 73 65 74 20 61 6d 6f 75 6e 74 20  e preset amount 
14667 74 68 65 6e 20 73 75 72 70 6c 75 73 0a 2a 2a 20  then surplus.** 
14668 62 79 74 65 73 20 61 72 65 20 73 74 6f 72 65 64  bytes are stored
14669 20 6f 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   on overflow pag
1466a 65 73 2e 20 20 54 68 65 20 70 61 79 6c 6f 61 64  es.  The payload
1466b 20 66 6f 72 20 61 6e 20 65 6e 74 72 79 0a 2a 2a   for an entry.**
1466c 20 61 6e 64 20 74 68 65 20 70 72 65 63 65 64 69   and the precedi
1466d 6e 67 20 70 6f 69 6e 74 65 72 20 61 72 65 20 63  ng pointer are c
1466e 6f 6d 62 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20  ombined to form 
1466f 61 20 22 43 65 6c 6c 22 2e 20 20 45 61 63 68 20  a "Cell".  Each 
14670 0a 2a 2a 20 70 61 67 65 20 68 61 73 20 61 20 73  .** page has a s
14671 6d 61 6c 6c 20 68 65 61 64 65 72 20 77 68 69 63  mall header whic
14672 68 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 50  h contains the P
14673 74 72 28 4e 29 20 70 6f 69 6e 74 65 72 20 61 6e  tr(N) pointer an
14674 64 20 6f 74 68 65 72 0a 2a 2a 20 69 6e 66 6f 72  d other.** infor
14675 6d 61 74 69 6f 6e 20 73 75 63 68 20 61 73 20 74  mation such as t
14676 68 65 20 73 69 7a 65 20 6f 66 20 6b 65 79 20 61  he size of key a
14677 6e 64 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 46  nd data..**.** F
14678 4f 52 4d 41 54 20 44 45 54 41 49 4c 53 0a 2a 2a  ORMAT DETAILS.**
14679 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 69 73 20  .** The file is 
1467a 64 69 76 69 64 65 64 20 69 6e 74 6f 20 70 61 67  divided into pag
1467b 65 73 2e 20 20 54 68 65 20 66 69 72 73 74 20 70  es.  The first p
1467c 61 67 65 20 69 73 20 63 61 6c 6c 65 64 20 70 61  age is called pa
1467d 67 65 20 31 2c 0a 2a 2a 20 74 68 65 20 73 65 63  ge 1,.** the sec
1467e 6f 6e 64 20 69 73 20 70 61 67 65 20 32 2c 20 61  ond is page 2, a
1467f 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 41 20  nd so forth.  A 
14680 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 7a  page number of z
14681 65 72 6f 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a  ero indicates.**
14682 20 22 6e 6f 20 73 75 63 68 20 70 61 67 65 22 2e   "no such page".
14683 20 20 54 68 65 20 70 61 67 65 20 73 69 7a 65 20    The page size 
14684 63 61 6e 20 62 65 20 61 6e 79 74 68 69 6e 67 20  can be anything 
14685 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64 20  between 512 and 
14686 36 35 35 33 36 2e 0a 2a 2a 20 45 61 63 68 20 70  65536..** Each p
14687 61 67 65 20 63 61 6e 20 62 65 20 65 69 74 68 65  age can be eithe
14688 72 20 61 20 62 74 72 65 65 20 70 61 67 65 2c 20  r a btree page, 
14689 61 20 66 72 65 65 6c 69 73 74 20 70 61 67 65 20  a freelist page 
1468a 6f 72 20 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a  or an overflow.*
1468b 2a 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  * page..**.** Th
1468c 65 20 66 69 72 73 74 20 70 61 67 65 20 69 73 20  e first page is 
1468d 61 6c 77 61 79 73 20 61 20 62 74 72 65 65 20 70  always a btree p
1468e 61 67 65 2e 20 20 54 68 65 20 66 69 72 73 74 20  age.  The first 
1468f 31 30 30 20 62 79 74 65 73 20 6f 66 20 74 68 65  100 bytes of the
14690 20 66 69 72 73 74 0a 2a 2a 20 70 61 67 65 20 63   first.** page c
14691 6f 6e 74 61 69 6e 20 61 20 73 70 65 63 69 61 6c  ontain a special
14692 20 68 65 61 64 65 72 20 28 74 68 65 20 22 66 69   header (the "fi
14693 6c 65 20 68 65 61 64 65 72 22 29 20 74 68 61 74  le header") that
14694 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 66   describes the f
14695 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 66 6f 72 6d  ile..** The form
14696 61 74 20 6f 66 20 74 68 65 20 66 69 6c 65 20 68  at of the file h
14697 65 61 64 65 72 20 69 73 20 61 73 20 66 6f 6c 6c  eader is as foll
14698 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46  ows:.**.**   OFF
14699 53 45 54 20 20 20 53 49 5a 45 20 20 20 20 44 45  SET   SIZE    DE
1469a 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20  SCRIPTION.**    
1469b 20 20 30 20 20 20 20 20 20 31 36 20 20 20 20 20    0      16     
1469c 48 65 61 64 65 72 20 73 74 72 69 6e 67 3a 20 22  Header string: "
1469d 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 5c  SQLite format 3\
1469e 30 30 30 22 0a 2a 2a 20 20 20 20 20 31 36 20 20  000".**     16  
1469f 20 20 20 20 20 32 20 20 20 20 20 50 61 67 65 20       2     Page 
146a0 73 69 7a 65 20 69 6e 20 62 79 74 65 73 2e 20 20  size in bytes.  
146a1 0a 2a 2a 20 20 20 20 20 31 38 20 20 20 20 20 20  .**     18      
146a2 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f 72 6d   1     File form
146a3 61 74 20 77 72 69 74 65 20 76 65 72 73 69 6f 6e  at write version
146a4 0a 2a 2a 20 20 20 20 20 31 39 20 20 20 20 20 20  .**     19      
146a5 20 31 20 20 20 20 20 46 69 6c 65 20 66 6f 72 6d   1     File form
146a6 61 74 20 72 65 61 64 20 76 65 72 73 69 6f 6e 0a  at read version.
146a7 2a 2a 20 20 20 20 20 32 30 20 20 20 20 20 20 20  **     20       
146a8 31 20 20 20 20 20 42 79 74 65 73 20 6f 66 20 75  1     Bytes of u
146a9 6e 75 73 65 64 20 73 70 61 63 65 20 61 74 20 74  nused space at t
146aa 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20 70  he end of each p
146ab 61 67 65 0a 2a 2a 20 20 20 20 20 32 31 20 20 20  age.**     21   
146ac 20 20 20 20 31 20 20 20 20 20 4d 61 78 20 65 6d      1     Max em
146ad 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66  bedded payload f
146ae 72 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32  raction.**     2
146af 32 20 20 20 20 20 20 20 31 20 20 20 20 20 4d 69  2       1     Mi
146b0 6e 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f  n embedded paylo
146b1 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20  ad fraction.**  
146b2 20 20 20 32 33 20 20 20 20 20 20 20 31 20 20 20     23       1   
146b3 20 20 4d 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f    Min leaf paylo
146b4 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 20  ad fraction.**  
146b5 20 20 20 32 34 20 20 20 20 20 20 20 34 20 20 20     24       4   
146b6 20 20 46 69 6c 65 20 63 68 61 6e 67 65 20 63 6f    File change co
146b7 75 6e 74 65 72 0a 2a 2a 20 20 20 20 20 32 38 20  unter.**     28 
146b8 20 20 20 20 20 20 34 20 20 20 20 20 52 65 73 65        4     Rese
146b9 72 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20  rved for future 
146ba 75 73 65 0a 2a 2a 20 20 20 20 20 33 32 20 20 20  use.**     32   
146bb 20 20 20 20 34 20 20 20 20 20 46 69 72 73 74 20      4     First 
146bc 66 72 65 65 6c 69 73 74 20 70 61 67 65 0a 2a 2a  freelist page.**
146bd 20 20 20 20 20 33 36 20 20 20 20 20 20 20 34 20       36       4 
146be 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 66 72      Number of fr
146bf 65 65 6c 69 73 74 20 70 61 67 65 73 20 69 6e 20  eelist pages in 
146c0 74 68 65 20 66 69 6c 65 0a 2a 2a 20 20 20 20 20  the file.**     
146c1 34 30 20 20 20 20 20 20 36 30 20 20 20 20 20 31  40      60     1
146c2 35 20 34 2d 62 79 74 65 20 6d 65 74 61 20 76 61  5 4-byte meta va
146c3 6c 75 65 73 20 70 61 73 73 65 64 20 74 6f 20 68  lues passed to h
146c4 69 67 68 65 72 20 6c 61 79 65 72 73 0a 2a 2a 0a  igher layers.**.
146c5 2a 2a 20 20 20 20 20 34 30 20 20 20 20 20 20 20  **     40       
146c6 34 20 20 20 20 20 53 63 68 65 6d 61 20 63 6f 6f  4     Schema coo
146c7 6b 69 65 0a 2a 2a 20 20 20 20 20 34 34 20 20 20  kie.**     44   
146c8 20 20 20 20 34 20 20 20 20 20 46 69 6c 65 20 66      4     File f
146c9 6f 72 6d 61 74 20 6f 66 20 73 63 68 65 6d 61 20  ormat of schema 
146ca 6c 61 79 65 72 0a 2a 2a 20 20 20 20 20 34 38 20  layer.**     48 
146cb 20 20 20 20 20 20 34 20 20 20 20 20 53 69 7a 65        4     Size
146cc 20 6f 66 20 70 61 67 65 20 63 61 63 68 65 0a 2a   of page cache.*
146cd 2a 20 20 20 20 20 35 32 20 20 20 20 20 20 20 34  *     52       4
146ce 20 20 20 20 20 4c 61 72 67 65 73 74 20 72 6f 6f       Largest roo
146cf 74 2d 70 61 67 65 20 28 61 75 74 6f 2f 69 6e 63  t-page (auto/inc
146d0 72 5f 76 61 63 75 75 6d 29 0a 2a 2a 20 20 20 20  r_vacuum).**    
146d1 20 35 36 20 20 20 20 20 20 20 34 20 20 20 20 20   56       4     
146d2 31 3d 55 54 46 2d 38 20 32 3d 55 54 46 31 36 6c  1=UTF-8 2=UTF16l
146d3 65 20 33 3d 55 54 46 31 36 62 65 0a 2a 2a 20 20  e 3=UTF16be.**  
146d4 20 20 20 36 30 20 20 20 20 20 20 20 34 20 20 20     60       4   
146d5 20 20 55 73 65 72 20 76 65 72 73 69 6f 6e 0a 2a    User version.*
146d6 2a 20 20 20 20 20 36 34 20 20 20 20 20 20 20 34  *     64       4
146d7 20 20 20 20 20 49 6e 63 72 65 6d 65 6e 74 61 6c       Incremental
146d8 20 76 61 63 75 75 6d 20 6d 6f 64 65 0a 2a 2a 20   vacuum mode.** 
146d9 20 20 20 20 36 38 20 20 20 20 20 20 20 34 20 20      68       4  
146da 20 20 20 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20     unused.**    
146db 20 37 32 20 20 20 20 20 20 20 34 20 20 20 20 20   72       4     
146dc 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20 37 36  unused.**     76
146dd 20 20 20 20 20 20 20 34 20 20 20 20 20 75 6e 75         4     unu
146de 73 65 64 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66  sed.**.** All of
146df 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c   the integer val
146e0 75 65 73 20 61 72 65 20 62 69 67 2d 65 6e 64 69  ues are big-endi
146e1 61 6e 20 28 6d 6f 73 74 20 73 69 67 6e 69 66 69  an (most signifi
146e2 63 61 6e 74 20 62 79 74 65 20 66 69 72 73 74 29  cant byte first)
146e3 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65  ..**.** The file
146e4 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20   change counter 
146e5 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77  is incremented w
146e6 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
146e7 20 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 54   is changed.** T
146e8 68 69 73 20 63 6f 75 6e 74 65 72 20 61 6c 6c 6f  his counter allo
146e9 77 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  ws other process
146ea 65 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 6e 20  es to know when 
146eb 74 68 65 20 66 69 6c 65 20 68 61 73 20 63 68 61  the file has cha
146ec 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 75 73  nged.** and thus
146ed 20 77 68 65 6e 20 74 68 65 79 20 6e 65 65 64 20   when they need 
146ee 74 6f 20 66 6c 75 73 68 20 74 68 65 69 72 20 63  to flush their c
146ef 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ache..**.** The 
146f0 6d 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79  max embedded pay
146f1 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73  load fraction is
146f2 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74   the amount of t
146f3 68 65 20 74 6f 74 61 6c 20 75 73 61 62 6c 65 0a  he total usable.
146f4 2a 2a 20 73 70 61 63 65 20 69 6e 20 61 20 70 61  ** space in a pa
146f5 67 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 63  ge that can be c
146f6 6f 6e 73 75 6d 65 64 20 62 79 20 61 20 73 69 6e  onsumed by a sin
146f7 67 6c 65 20 63 65 6c 6c 20 66 6f 72 20 73 74 61  gle cell for sta
146f8 6e 64 61 72 64 0a 2a 2a 20 42 2d 74 72 65 65 20  ndard.** B-tree 
146f9 28 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 29 20 74  (non-LEAFDATA) t
146fa 61 62 6c 65 73 2e 20 20 41 20 76 61 6c 75 65 20  ables.  A value 
146fb 6f 66 20 32 35 35 20 6d 65 61 6e 73 20 31 30 30  of 255 means 100
146fc 25 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a  %.  The default.
146fd 2a 2a 20 69 73 20 74 6f 20 6c 69 6d 69 74 20 74  ** is to limit t
146fe 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20  he maximum cell 
146ff 73 69 7a 65 20 73 6f 20 74 68 61 74 20 61 74 20  size so that at 
14700 6c 65 61 73 74 20 34 20 63 65 6c 6c 73 20 77 69  least 4 cells wi
14701 6c 6c 20 66 69 74 0a 2a 2a 20 6f 6e 20 6f 6e 65  ll fit.** on one
14702 20 70 61 67 65 2e 20 20 54 68 75 73 20 74 68 65   page.  Thus the
14703 20 64 65 66 61 75 6c 74 20 6d 61 78 20 65 6d 62   default max emb
14704 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72  edded payload fr
14705 61 63 74 69 6f 6e 20 69 73 20 36 34 2e 0a 2a 2a  action is 64..**
14706 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 79 6c 6f  .** If the paylo
14707 61 64 20 66 6f 72 20 61 20 63 65 6c 6c 20 69 73  ad for a cell is
14708 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65   larger than the
14709 20 6d 61 78 20 70 61 79 6c 6f 61 64 2c 20 74 68   max payload, th
1470a 65 6e 20 65 78 74 72 61 0a 2a 2a 20 70 61 79 6c  en extra.** payl
1470b 6f 61 64 20 69 73 20 73 70 69 6c 6c 65 64 20 74  oad is spilled t
1470c 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  o overflow pages
1470d 2e 20 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66  .  Once an overf
1470e 6c 6f 77 20 70 61 67 65 20 69 73 20 61 6c 6c 6f  low page is allo
1470f 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20 6d 61 6e  cated,.** as man
14710 79 20 62 79 74 65 73 20 61 73 20 70 6f 73 73 69  y bytes as possi
14711 62 6c 65 20 61 72 65 20 6d 6f 76 65 64 20 69 6e  ble are moved in
14712 74 6f 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  to the overflow 
14713 70 61 67 65 73 20 77 69 74 68 6f 75 74 20 6c 65  pages without le
14714 74 74 69 6e 67 0a 2a 2a 20 74 68 65 20 63 65 6c  tting.** the cel
14715 6c 20 73 69 7a 65 20 64 72 6f 70 20 62 65 6c 6f  l size drop belo
14716 77 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64  w the min embedd
14717 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74  ed payload fract
14718 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d  ion..**.** The m
14719 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20  in leaf payload 
1471a 66 72 61 63 74 69 6f 6e 20 69 73 20 6c 69 6b 65  fraction is like
1471b 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 65   the min embedde
1471c 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69  d payload fracti
1471d 6f 6e 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61  on.** except tha
1471e 74 20 69 74 20 61 70 70 6c 69 65 73 20 74 6f 20  t it applies to 
1471f 6c 65 61 66 20 6e 6f 64 65 73 20 69 6e 20 61 20  leaf nodes in a 
14720 4c 45 41 46 44 41 54 41 20 74 72 65 65 2e 20 20  LEAFDATA tree.  
14721 54 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 70  The maximum.** p
14722 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20  ayload fraction 
14723 66 6f 72 20 61 20 4c 45 41 46 44 41 54 41 20 74  for a LEAFDATA t
14724 72 65 65 20 69 73 20 61 6c 77 61 79 73 20 31 30  ree is always 10
14725 30 25 20 28 6f 72 20 32 35 35 29 20 61 6e 64 20  0% (or 255) and 
14726 69 74 0a 2a 2a 20 6e 6f 74 20 73 70 65 63 69 66  it.** not specif
14727 69 65 64 20 69 6e 20 74 68 65 20 68 65 61 64 65  ied in the heade
14728 72 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 62 74  r..**.** Each bt
14729 72 65 65 20 70 61 67 65 73 20 69 73 20 64 69 76  ree pages is div
1472a 69 64 65 64 20 69 6e 74 6f 20 74 68 72 65 65 20  ided into three 
1472b 73 65 63 74 69 6f 6e 73 3a 20 20 54 68 65 20 68  sections:  The h
1472c 65 61 64 65 72 2c 20 74 68 65 0a 2a 2a 20 63 65  eader, the.** ce
1472d 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79  ll pointer array
1472e 2c 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63  , and the cell c
1472f 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 50 61  ontent area.  Pa
14730 67 65 20 31 20 61 6c 73 6f 20 68 61 73 20 61 20  ge 1 also has a 
14731 31 30 30 2d 62 79 74 65 0a 2a 2a 20 66 69 6c 65  100-byte.** file
14732 20 68 65 61 64 65 72 20 74 68 61 74 20 6f 63 63   header that occ
14733 75 72 73 20 62 65 66 6f 72 65 20 74 68 65 20 70  urs before the p
14734 61 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a  age header..**.*
14735 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d  *      |--------
14736 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20  --------|.**    
14737 20 20 7c 20 66 69 6c 65 20 68 65 61 64 65 72 20    | file header 
14738 20 20 20 7c 20 20 20 31 30 30 20 62 79 74 65 73     |   100 bytes
14739 2e 20 20 50 61 67 65 20 31 20 6f 6e 6c 79 2e 0a  .  Page 1 only..
1473a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d  **      |-------
1473b 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20  ---------|.**   
1473c 20 20 20 7c 20 70 61 67 65 20 68 65 61 64 65 72     | page header
1473d 20 20 20 20 7c 20 20 20 38 20 62 79 74 65 73 20      |   8 bytes 
1473e 66 6f 72 20 6c 65 61 76 65 73 2e 20 20 31 32 20  for leaves.  12 
1473f 62 79 74 65 73 20 66 6f 72 20 69 6e 74 65 72 69  bytes for interi
14740 6f 72 20 6e 6f 64 65 73 0a 2a 2a 20 20 20 20 20  or nodes.**     
14741 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   |--------------
14742 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65  --|.**      | ce
14743 6c 6c 20 70 6f 69 6e 74 65 72 20 20 20 7c 20 20  ll pointer   |  
14744 20 7c 20 20 32 20 62 79 74 65 73 20 70 65 72 20   |  2 bytes per 
14745 63 65 6c 6c 2e 20 20 53 6f 72 74 65 64 20 6f 72  cell.  Sorted or
14746 64 65 72 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61  der..**      | a
14747 72 72 61 79 20 20 20 20 20 20 20 20 20 20 7c 20  rray          | 
14748 20 20 7c 20 20 47 72 6f 77 73 20 64 6f 77 6e 77    |  Grows downw
14749 61 72 64 0a 2a 2a 20 20 20 20 20 20 7c 20 20 20  ard.**      |   
1474a 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 20               |  
1474b 20 76 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d   v.**      |----
1474c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a  ------------|.**
1474d 20 20 20 20 20 20 7c 20 75 6e 61 6c 6c 6f 63 61        | unalloca
1474e 74 65 64 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20  ted    |.**     
1474f 20 7c 20 73 70 61 63 65 20 20 20 20 20 20 20 20   | space        
14750 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d    |.**      |---
14751 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 20 20  -------------|  
14752 20 5e 20 20 47 72 6f 77 73 20 75 70 77 61 72 64   ^  Grows upward
14753 73 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c  s.**      | cell
14754 20 63 6f 6e 74 65 6e 74 20 20 20 7c 20 20 20 7c   content   |   |
14755 20 20 41 72 62 69 74 72 61 72 79 20 6f 72 64 65    Arbitrary orde
14756 72 20 69 6e 74 65 72 73 70 65 72 73 65 64 20 77  r interspersed w
14757 69 74 68 20 66 72 65 65 62 6c 6f 63 6b 73 2e 0a  ith freeblocks..
14758 2a 2a 20 20 20 20 20 20 7c 20 61 72 65 61 20 20  **      | area  
14759 20 20 20 20 20 20 20 20 20 7c 20 20 20 7c 20 20           |   |  
1475a 61 6e 64 20 66 72 65 65 20 73 70 61 63 65 20 66  and free space f
1475b 72 61 67 6d 65 6e 74 73 2e 0a 2a 2a 20 20 20 20  ragments..**    
1475c 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    |-------------
1475d 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ---|.**.** The p
1475e 61 67 65 20 68 65 61 64 65 72 73 20 6c 6f 6f 6b  age headers look
1475f 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a  s like this:.**.
14760 2a 2a 20 20 20 4f 46 46 53 45 54 20 20 20 53 49  **   OFFSET   SI
14761 5a 45 20 20 20 20 20 44 45 53 43 52 49 50 54 49  ZE     DESCRIPTI
14762 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20  ON.**      0    
14763 20 20 20 31 20 20 20 20 20 20 46 6c 61 67 73 2e     1      Flags.
14764 20 31 3a 20 69 6e 74 6b 65 79 2c 20 32 3a 20 7a   1: intkey, 2: z
14765 65 72 6f 64 61 74 61 2c 20 34 3a 20 6c 65 61 66  erodata, 4: leaf
14766 64 61 74 61 2c 20 38 3a 20 6c 65 61 66 0a 2a 2a  data, 8: leaf.**
14767 20 20 20 20 20 20 31 20 20 20 20 20 20 20 32 20        1       2 
14768 20 20 20 20 20 62 79 74 65 20 6f 66 66 73 65 74       byte offset
14769 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 66 72   to the first fr
1476a 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 20  eeblock.**      
1476b 33 20 20 20 20 20 20 20 32 20 20 20 20 20 20 6e  3       2      n
1476c 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f  umber of cells o
1476d 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20 20  n this page.**  
1476e 20 20 20 20 35 20 20 20 20 20 20 20 32 20 20 20      5       2   
1476f 20 20 20 66 69 72 73 74 20 62 79 74 65 20 6f 66     first byte of
14770 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
14771 74 20 61 72 65 61 0a 2a 2a 20 20 20 20 20 20 37  t area.**      7
14772 20 20 20 20 20 20 20 31 20 20 20 20 20 20 6e 75         1      nu
14773 6d 62 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74  mber of fragment
14774 65 64 20 66 72 65 65 20 62 79 74 65 73 0a 2a 2a  ed free bytes.**
14775 20 20 20 20 20 20 38 20 20 20 20 20 20 20 34 20        8       4 
14776 20 20 20 20 20 52 69 67 68 74 20 63 68 69 6c 64       Right child
14777 20 28 74 68 65 20 50 74 72 28 4e 29 20 76 61 6c   (the Ptr(N) val
14778 75 65 29 2e 20 20 4f 6d 69 74 74 65 64 20 6f 6e  ue).  Omitted on
14779 20 6c 65 61 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 54   leaves..**.** T
1477a 68 65 20 66 6c 61 67 73 20 64 65 66 69 6e 65 20  he flags define 
1477b 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68  the format of th
1477c 69 73 20 62 74 72 65 65 20 70 61 67 65 2e 20 20  is btree page.  
1477d 54 68 65 20 6c 65 61 66 20 66 6c 61 67 20 6d 65  The leaf flag me
1477e 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 69 73  ans that.** this
1477f 20 70 61 67 65 20 68 61 73 20 6e 6f 20 63 68 69   page has no chi
14780 6c 64 72 65 6e 2e 20 20 54 68 65 20 7a 65 72 6f  ldren.  The zero
14781 64 61 74 61 20 66 6c 61 67 20 6d 65 61 6e 73 20  data flag means 
14782 74 68 61 74 20 74 68 69 73 20 70 61 67 65 20 63  that this page c
14783 61 72 72 69 65 73 0a 2a 2a 20 6f 6e 6c 79 20 6b  arries.** only k
14784 65 79 73 20 61 6e 64 20 6e 6f 20 64 61 74 61 2e  eys and no data.
14785 20 20 54 68 65 20 69 6e 74 6b 65 79 20 66 6c 61    The intkey fla
14786 67 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  g means that the
14787 20 6b 65 79 20 69 73 20 61 20 69 6e 74 65 67 65   key is a intege
14788 72 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 73 74  r.** which is st
14789 6f 72 65 64 20 69 6e 20 74 68 65 20 6b 65 79 20  ored in the key 
1478a 73 69 7a 65 20 65 6e 74 72 79 20 6f 66 20 74 68  size entry of th
1478b 65 20 63 65 6c 6c 20 68 65 61 64 65 72 20 72 61  e cell header ra
1478c 74 68 65 72 20 74 68 61 6e 20 69 6e 0a 2a 2a 20  ther than in.** 
1478d 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72 65 61  the payload area
1478e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 65 6c 6c  ..**.** The cell
1478f 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 62   pointer array b
14790 65 67 69 6e 73 20 6f 6e 20 74 68 65 20 66 69 72  egins on the fir
14791 73 74 20 62 79 74 65 20 61 66 74 65 72 20 74 68  st byte after th
14792 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 2a  e page header..*
14793 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74  * The cell point
14794 65 72 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e  er array contain
14795 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 32  s zero or more 2
14796 2d 62 79 74 65 20 6e 75 6d 62 65 72 73 20 77 68  -byte numbers wh
14797 69 63 68 20 61 72 65 0a 2a 2a 20 6f 66 66 73 65  ich are.** offse
14798 74 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69  ts from the begi
14799 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67  nning of the pag
1479a 65 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f  e to the cell co
1479b 6e 74 65 6e 74 20 69 6e 20 74 68 65 20 63 65 6c  ntent in the cel
1479c 6c 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 72 65  l.** content are
1479d 61 2e 20 20 54 68 65 20 63 65 6c 6c 20 70 6f 69  a.  The cell poi
1479e 6e 74 65 72 73 20 6f 63 63 75 72 20 69 6e 20 73  nters occur in s
1479f 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20 54 68  orted order.  Th
147a0 65 20 73 79 73 74 65 6d 20 73 74 72 69 76 65 73  e system strives
147a1 0a 2a 2a 20 74 6f 20 6b 65 65 70 20 66 72 65 65  .** to keep free
147a2 20 73 70 61 63 65 20 61 66 74 65 72 20 74 68 65   space after the
147a3 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74   last cell point
147a4 65 72 20 73 6f 20 74 68 61 74 20 6e 65 77 20 63  er so that new c
147a5 65 6c 6c 73 20 63 61 6e 0a 2a 2a 20 62 65 20 65  ells can.** be e
147a6 61 73 69 6c 79 20 61 64 64 65 64 20 77 69 74 68  asily added with
147a7 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 64 65  out having to de
147a8 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67  fragment the pag
147a9 65 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f  e..**.** Cell co
147aa 6e 74 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20  ntent is stored 
147ab 61 74 20 74 68 65 20 76 65 72 79 20 65 6e 64 20  at the very end 
147ac 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20  of the page and 
147ad 67 72 6f 77 73 20 74 6f 77 61 72 64 20 74 68 65  grows toward the
147ae 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  .** beginning of
147af 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a   the page..**.**
147b0 20 55 6e 75 73 65 64 20 73 70 61 63 65 20 77 69   Unused space wi
147b1 74 68 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f  thin the cell co
147b2 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20 63 6f  ntent area is co
147b3 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 61 20 6c  llected into a l
147b4 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 0a 2a 2a  inked list of.**
147b5 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20 45 61   freeblocks.  Ea
147b6 63 68 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20  ch freeblock is 
147b7 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73  at least 4 bytes
147b8 20 69 6e 20 73 69 7a 65 2e 20 20 54 68 65 20 62   in size.  The b
147b9 79 74 65 20 6f 66 66 73 65 74 0a 2a 2a 20 74 6f  yte offset.** to
147ba 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65 62   the first freeb
147bb 6c 6f 63 6b 20 69 73 20 67 69 76 65 6e 20 69 6e  lock is given in
147bc 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 46 72   the header.  Fr
147bd 65 65 62 6c 6f 63 6b 73 20 6f 63 63 75 72 20 69  eeblocks occur i
147be 6e 0a 2a 2a 20 69 6e 63 72 65 61 73 69 6e 67 20  n.** increasing 
147bf 6f 72 64 65 72 2e 20 20 42 65 63 61 75 73 65 20  order.  Because 
147c0 61 20 66 72 65 65 62 6c 6f 63 6b 20 6d 75 73 74  a freeblock must
147c1 20 62 65 20 61 74 20 6c 65 61 73 74 20 34 20 62   be at least 4 b
147c2 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 0a 2a 2a  ytes in size,.**
147c3 20 61 6e 79 20 67 72 6f 75 70 20 6f 66 20 33 20   any group of 3 
147c4 6f 72 20 66 65 77 65 72 20 75 6e 75 73 65 64 20  or fewer unused 
147c5 62 79 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c  bytes in the cel
147c6 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 63  l content area c
147c7 61 6e 6e 6f 74 0a 2a 2a 20 65 78 69 73 74 20 6f  annot.** exist o
147c8 6e 20 74 68 65 20 66 72 65 65 62 6c 6f 63 6b 20  n the freeblock 
147c9 63 68 61 69 6e 2e 20 20 41 20 67 72 6f 75 70 20  chain.  A group 
147ca 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20 66 72  of 3 or fewer fr
147cb 65 65 20 62 79 74 65 73 20 69 73 20 63 61 6c 6c  ee bytes is call
147cc 65 64 0a 2a 2a 20 61 20 66 72 61 67 6d 65 6e 74  ed.** a fragment
147cd 2e 20 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d  .  The total num
147ce 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
147cf 61 6c 6c 20 66 72 61 67 6d 65 6e 74 73 20 69 73  all fragments is
147d0 20 72 65 63 6f 72 64 65 64 2e 0a 2a 2a 20 69 6e   recorded..** in
147d1 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72   the page header
147d2 20 61 74 20 6f 66 66 73 65 74 20 37 2e 0a 2a 2a   at offset 7..**
147d3 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44  .**    SIZE    D
147d4 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20  ESCRIPTION.**   
147d5 20 20 20 32 20 20 20 20 20 42 79 74 65 20 6f 66     2     Byte of
147d6 66 73 65 74 20 6f 66 20 74 68 65 20 6e 65 78 74  fset of the next
147d7 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20   freeblock.**   
147d8 20 20 20 32 20 20 20 20 20 42 79 74 65 73 20 69     2     Bytes i
147d9 6e 20 74 68 69 73 20 66 72 65 65 62 6c 6f 63 6b  n this freeblock
147da 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 73 20 61 72 65  .**.** Cells are
147db 20 6f 66 20 76 61 72 69 61 62 6c 65 20 6c 65 6e   of variable len
147dc 67 74 68 2e 20 20 43 65 6c 6c 73 20 61 72 65 20  gth.  Cells are 
147dd 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 65  stored in the ce
147de 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20  ll content area 
147df 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66  at.** the end of
147e0 20 74 68 65 20 70 61 67 65 2e 20 20 50 6f 69 6e   the page.  Poin
147e1 74 65 72 73 20 74 6f 20 74 68 65 20 63 65 6c 6c  ters to the cell
147e2 73 20 61 72 65 20 69 6e 20 74 68 65 20 63 65 6c  s are in the cel
147e3 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 0a  l pointer array.
147e4 2a 2a 20 74 68 61 74 20 69 6d 6d 65 64 69 61 74  ** that immediat
147e5 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20  ely follows the 
147e6 70 61 67 65 20 68 65 61 64 65 72 2e 20 20 43 65  page header.  Ce
147e7 6c 6c 73 20 69 73 20 6e 6f 74 20 6e 65 63 65 73  lls is not neces
147e8 73 61 72 69 6c 79 0a 2a 2a 20 63 6f 6e 74 69 67  sarily.** contig
147e9 75 6f 75 73 20 6f 72 20 69 6e 20 6f 72 64 65 72  uous or in order
147ea 2c 20 62 75 74 20 63 65 6c 6c 20 70 6f 69 6e 74  , but cell point
147eb 65 72 73 20 61 72 65 20 63 6f 6e 74 69 67 75 6f  ers are contiguo
147ec 75 73 20 61 6e 64 20 69 6e 20 6f 72 64 65 72 2e  us and in order.
147ed 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74  .**.** Cell cont
147ee 65 6e 74 20 6d 61 6b 65 73 20 75 73 65 20 6f 66  ent makes use of
147ef 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68   variable length
147f0 20 69 6e 74 65 67 65 72 73 2e 20 20 41 20 76 61   integers.  A va
147f1 72 69 61 62 6c 65 0a 2a 2a 20 6c 65 6e 67 74 68  riable.** length
147f2 20 69 6e 74 65 67 65 72 20 69 73 20 31 20 74 6f   integer is 1 to
147f3 20 39 20 62 79 74 65 73 20 77 68 65 72 65 20 74   9 bytes where t
147f4 68 65 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20  he lower 7 bits 
147f5 6f 66 20 65 61 63 68 20 0a 2a 2a 20 62 79 74 65  of each .** byte
147f6 20 61 72 65 20 75 73 65 64 2e 20 20 54 68 65 20   are used.  The 
147f7 69 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73  integer consists
147f8 20 6f 66 20 61 6c 6c 20 62 79 74 65 73 20 74 68   of all bytes th
147f9 61 74 20 68 61 76 65 20 62 69 74 20 38 20 73 65  at have bit 8 se
147fa 74 20 61 6e 64 0a 2a 2a 20 74 68 65 20 66 69 72  t and.** the fir
147fb 73 74 20 62 79 74 65 20 77 69 74 68 20 62 69 74  st byte with bit
147fc 20 38 20 63 6c 65 61 72 2e 20 20 54 68 65 20 6d   8 clear.  The m
147fd 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20  ost significant 
147fe 62 79 74 65 20 6f 66 20 74 68 65 20 69 6e 74 65  byte of the inte
147ff 67 65 72 0a 2a 2a 20 61 70 70 65 61 72 73 20 66  ger.** appears f
14800 69 72 73 74 2e 20 20 41 20 76 61 72 69 61 62 6c  irst.  A variabl
14801 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
14802 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 72 65   may not be more
14803 20 74 68 61 6e 20 39 20 62 79 74 65 73 20 6c 6f   than 9 bytes lo
14804 6e 67 2e 0a 2a 2a 20 41 73 20 61 20 73 70 65 63  ng..** As a spec
14805 69 61 6c 20 63 61 73 65 2c 20 61 6c 6c 20 38 20  ial case, all 8 
14806 62 79 74 65 73 20 6f 66 20 74 68 65 20 39 74 68  bytes of the 9th
14807 20 62 79 74 65 20 61 72 65 20 75 73 65 64 20 61   byte are used a
14808 73 20 64 61 74 61 2e 20 20 54 68 69 73 0a 2a 2a  s data.  This.**
14809 20 61 6c 6c 6f 77 73 20 61 20 36 34 2d 62 69 74   allows a 64-bit
1480a 20 69 6e 74 65 67 65 72 20 74 6f 20 62 65 20 65   integer to be e
1480b 6e 63 6f 64 65 64 20 69 6e 20 39 20 62 79 74 65  ncoded in 9 byte
1480c 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 78 30 30  s..**.**    0x00
1480d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1480e 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30        becomes  0
1480f 78 30 30 30 30 30 30 30 30 0a 2a 2a 20 20 20 20  x00000000.**    
14810 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20 20  0x7f            
14811 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65            become
14812 73 20 20 30 78 30 30 30 30 30 30 37 66 0a 2a 2a  s  0x0000007f.**
14813 20 20 20 20 30 78 38 31 20 30 78 30 30 20 20 20      0x81 0x00   
14814 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65                be
14815 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30 38  comes  0x0000008
14816 30 0a 2a 2a 20 20 20 20 30 78 38 32 20 30 78 30  0.**    0x82 0x0
14817 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
14818 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30    becomes  0x000
14819 30 30 31 30 30 0a 2a 2a 20 20 20 20 30 78 38 30  00100.**    0x80
1481a 20 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20   0x7f           
1481b 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30        becomes  0
1481c 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 20 20  x0000007f.**    
1481d 30 78 38 61 20 30 78 39 31 20 30 78 64 31 20 30  0x8a 0x91 0xd1 0
1481e 78 61 63 20 30 78 37 38 20 20 62 65 63 6f 6d 65  xac 0x78  become
1481f 73 20 20 30 78 31 32 33 34 35 36 37 38 0a 2a 2a  s  0x12345678.**
14820 20 20 20 20 30 78 38 31 20 30 78 38 31 20 30 78      0x81 0x81 0x
14821 38 31 20 30 78 38 31 20 30 78 30 31 20 20 62 65  81 0x81 0x01  be
14822 63 6f 6d 65 73 20 20 30 78 31 30 32 30 34 30 38  comes  0x1020408
14823 31 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65  1.**.** Variable
14824 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73   length integers
14825 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 72 6f   are used for ro
14826 77 69 64 73 20 61 6e 64 20 74 6f 20 68 6f 6c 64  wids and to hold
14827 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
14828 2a 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20 61  * bytes of key a
14829 6e 64 20 64 61 74 61 20 69 6e 20 61 20 62 74 72  nd data in a btr
1482a 65 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54  ee cell..**.** T
1482b 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20  he content of a 
1482c 63 65 6c 6c 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20  cell looks like 
1482d 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53  this:.**.**    S
1482e 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49  IZE    DESCRIPTI
1482f 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20  ON.**      4    
14830 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   Page number of 
14831 74 68 65 20 6c 65 66 74 20 63 68 69 6c 64 2e 20  the left child. 
14832 4f 6d 69 74 74 65 64 20 69 66 20 6c 65 61 66 20  Omitted if leaf 
14833 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20  flag is set..** 
14834 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65      var    Numbe
14835 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61  r of bytes of da
14836 74 61 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 74  ta. Omitted if t
14837 68 65 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67  he zerodata flag
14838 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20   is set..**     
14839 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66  var    Number of
1483a 20 62 79 74 65 73 20 6f 66 20 6b 65 79 2e 20 4f   bytes of key. O
1483b 72 20 74 68 65 20 6b 65 79 20 69 74 73 65 6c 66  r the key itself
1483c 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 20   if intkey flag 
1483d 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 20  is set..**      
1483e 2a 20 20 20 20 20 50 61 79 6c 6f 61 64 0a 2a 2a  *     Payload.**
1483f 20 20 20 20 20 20 34 20 20 20 20 20 46 69 72 73        4     Firs
14840 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6f 76  t page of the ov
14841 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 20 4f  erflow chain.  O
14842 6d 69 74 74 65 64 20 69 66 20 6e 6f 20 6f 76 65  mitted if no ove
14843 72 66 6c 6f 77 0a 2a 2a 0a 2a 2a 20 4f 76 65 72  rflow.**.** Over
14844 66 6c 6f 77 20 70 61 67 65 73 20 66 6f 72 6d 20  flow pages form 
14845 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 20 20  a linked list.  
14846 45 61 63 68 20 70 61 67 65 20 65 78 63 65 70 74  Each page except
14847 20 74 68 65 20 6c 61 73 74 20 69 73 20 63 6f 6d   the last is com
14848 70 6c 65 74 65 6c 79 0a 2a 2a 20 66 69 6c 6c 65  pletely.** fille
14849 64 20 77 69 74 68 20 64 61 74 61 20 28 70 61 67  d with data (pag
1484a 65 73 69 7a 65 20 2d 20 34 20 62 79 74 65 73 29  esize - 4 bytes)
1484b 2e 20 20 54 68 65 20 6c 61 73 74 20 70 61 67 65  .  The last page
1484c 20 63 61 6e 20 68 61 76 65 20 61 73 20 6c 69 74   can have as lit
1484d 74 6c 65 0a 2a 2a 20 61 73 20 31 20 62 79 74 65  tle.** as 1 byte
1484e 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20   of data..**.** 
1484f 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52     SIZE    DESCR
14850 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34  IPTION.**      4
14851 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72       Page number
14852 20 6f 66 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f   of next overflo
14853 77 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a  w page.**      *
14854 20 20 20 20 20 44 61 74 61 0a 2a 2a 0a 2a 2a 20       Data.**.** 
14855 46 72 65 65 6c 69 73 74 20 70 61 67 65 73 20 63  Freelist pages c
14856 6f 6d 65 20 69 6e 20 74 77 6f 20 73 75 62 74 79  ome in two subty
14857 70 65 73 3a 20 74 72 75 6e 6b 20 70 61 67 65 73  pes: trunk pages
14858 20 61 6e 64 20 6c 65 61 66 20 70 61 67 65 73 2e   and leaf pages.
14859 20 20 54 68 65 0a 2a 2a 20 66 69 6c 65 20 68 65    The.** file he
1485a 61 64 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74  ader points to t
1485b 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69  he first in a li
1485c 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 72 75  nked list of tru
1485d 6e 6b 20 70 61 67 65 2e 20 20 45 61 63 68 20 74  nk page.  Each t
1485e 72 75 6e 6b 0a 2a 2a 20 70 61 67 65 20 70 6f 69  runk.** page poi
1485f 6e 74 73 20 74 6f 20 6d 75 6c 74 69 70 6c 65 20  nts to multiple 
14860 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54 68 65  leaf pages.  The
14861 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 6c 65   content of a le
14862 61 66 20 70 61 67 65 20 69 73 0a 2a 2a 20 75 6e  af page is.** un
14863 73 70 65 63 69 66 69 65 64 2e 20 20 41 20 74 72  specified.  A tr
14864 75 6e 6b 20 70 61 67 65 20 6c 6f 6f 6b 73 20 6c  unk page looks l
14865 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
14866 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52     SIZE    DESCR
14867 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34  IPTION.**      4
14868 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72       Page number
14869 20 6f 66 20 6e 65 78 74 20 74 72 75 6e 6b 20 70   of next trunk p
1486a 61 67 65 0a 2a 2a 20 20 20 20 20 20 34 20 20 20  age.**      4   
1486b 20 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66    Number of leaf
1486c 20 70 6f 69 6e 74 65 72 73 20 6f 6e 20 74 68 69   pointers on thi
1486d 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a  s page.**      *
1486e 20 20 20 20 20 7a 65 72 6f 20 6f 72 20 6d 6f 72       zero or mor
1486f 65 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20  e pages numbers 
14870 6f 66 20 6c 65 61 76 65 73 0a 2a 2f 0a 0a 0a 2f  of leaves.*/.../
14871 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
14872 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d 61 78  value is the max
14873 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 61  imum cell size a
14874 73 73 75 6d 69 6e 67 20 61 20 6d 61 78 69 6d 75  ssuming a maximu
14875 6d 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 67  m page.** size g
14876 69 76 65 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 64  ive above..*/.#d
14877 65 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 5f 53 49  efine MX_CELL_SI
14878 5a 45 28 70 42 74 29 20 20 28 70 42 74 2d 3e 70  ZE(pBt)  (pBt->p
14879 61 67 65 53 69 7a 65 2d 38 29 0a 0a 2f 2a 20 54  ageSize-8)../* T
1487a 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
1487b 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 61 20  r of cells on a 
1487c 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 74  single page of t
1487d 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68  he database.  Th
1487e 69 73 0a 2a 2a 20 61 73 73 75 6d 65 73 20 61 20  is.** assumes a 
1487f 6d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a  minimum cell siz
14880 65 20 6f 66 20 36 20 62 79 74 65 73 20 20 28 34  e of 6 bytes  (4
14881 20 62 79 74 65 73 20 66 6f 72 20 74 68 65 20 63   bytes for the c
14882 65 6c 6c 20 69 74 73 65 6c 66 0a 2a 2a 20 70 6c  ell itself.** pl
14883 75 73 20 32 20 62 79 74 65 73 20 66 6f 72 20 74  us 2 bytes for t
14884 68 65 20 69 6e 64 65 78 20 74 6f 20 74 68 65 20  he index to the 
14885 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 67 65  cell in the page
14886 20 68 65 61 64 65 72 29 2e 20 20 53 75 63 68 0a   header).  Such.
14887 2a 2a 20 73 6d 61 6c 6c 20 63 65 6c 6c 73 20 77  ** small cells w
14888 69 6c 6c 20 62 65 20 72 61 72 65 2c 20 62 75 74  ill be rare, but
14889 20 74 68 65 79 20 61 72 65 20 70 6f 73 73 69 62   they are possib
1488a 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  le..*/.#define M
1488b 58 5f 43 45 4c 4c 28 70 42 74 29 20 28 28 70 42  X_CELL(pBt) ((pB
1488c 74 2d 3e 70 61 67 65 53 69 7a 65 2d 38 29 2f 36  t->pageSize-8)/6
1488d 29 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65  )../* Forward de
1488e 63 6c 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 74 79  clarations */.ty
1488f 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d  pedef struct Mem
14890 50 61 67 65 20 4d 65 6d 50 61 67 65 3b 0a 74 79  Page MemPage;.ty
14891 70 65 64 65 66 20 73 74 72 75 63 74 20 42 74 4c  pedef struct BtL
14892 6f 63 6b 20 42 74 4c 6f 63 6b 3b 0a 0a 2f 2a 0a  ock BtLock;../*.
14893 2a 2a 20 54 68 69 73 20 69 73 20 61 20 6d 61 67  ** This is a mag
14894 69 63 20 73 74 72 69 6e 67 20 74 68 61 74 20 61  ic string that a
14895 70 70 65 61 72 73 20 61 74 20 74 68 65 20 62 65  ppears at the be
14896 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79  ginning of every
14897 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62  .** SQLite datab
14898 61 73 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ase in order to 
14899 69 64 65 6e 74 69 66 79 20 74 68 65 20 66 69 6c  identify the fil
1489a 65 20 61 73 20 61 20 72 65 61 6c 20 64 61 74 61  e as a real data
1489b 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20  base..**.** You 
1489c 63 61 6e 20 63 68 61 6e 67 65 20 74 68 69 73 20  can change this 
1489d 76 61 6c 75 65 20 61 74 20 63 6f 6d 70 69 6c 65  value at compile
1489e 2d 74 69 6d 65 20 62 79 20 73 70 65 63 69 66 79  -time by specify
1489f 69 6e 67 20 61 0a 2a 2a 20 2d 44 53 51 4c 49 54  ing a.** -DSQLIT
148a0 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 3d 22 2e  E_FILE_HEADER=".
148a1 2e 2e 22 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69  .." on the compi
148a2 6c 65 72 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65  ler command-line
148a3 2e 20 20 54 68 65 0a 2a 2a 20 68 65 61 64 65 72  .  The.** header
148a4 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c 79   must be exactly
148a5 20 31 36 20 62 79 74 65 73 20 69 6e 63 6c 75 64   16 bytes includ
148a6 69 6e 67 20 74 68 65 20 7a 65 72 6f 2d 74 65 72  ing the zero-ter
148a7 6d 69 6e 61 74 6f 72 20 73 6f 0a 2a 2a 20 74 68  minator so.** th
148a8 65 20 73 74 72 69 6e 67 20 69 74 73 65 6c 66 20  e string itself 
148a9 73 68 6f 75 6c 64 20 62 65 20 31 35 20 63 68 61  should be 15 cha
148aa 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 49  racters long.  I
148ab 66 20 79 6f 75 20 63 68 61 6e 67 65 0a 2a 2a 20  f you change.** 
148ac 74 68 65 20 68 65 61 64 65 72 2c 20 74 68 65 6e  the header, then
148ad 20 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69 62   your custom lib
148ae 72 61 72 79 20 77 69 6c 6c 20 6e 6f 74 20 62 65  rary will not be
148af 20 61 62 6c 65 20 74 6f 20 72 65 61 64 20 0a 2a   able to read .*
148b0 2a 20 64 61 74 61 62 61 73 65 73 20 67 65 6e 65  * databases gene
148b1 72 61 74 65 64 20 62 79 20 74 68 65 20 73 74 61  rated by the sta
148b2 6e 64 61 72 64 20 74 6f 6f 6c 73 20 61 6e 64 20  ndard tools and 
148b3 74 68 65 20 73 74 61 6e 64 61 72 64 20 74 6f 6f  the standard too
148b4 6c 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62  ls.** will not b
148b5 65 20 61 62 6c 65 20 74 6f 20 72 65 61 64 20 64  e able to read d
148b6 61 74 61 62 61 73 65 73 20 63 72 65 61 74 65 64  atabases created
148b7 20 62 79 20 79 6f 75 72 20 63 75 73 74 6f 6d 20   by your custom 
148b8 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e  library..*/.#ifn
148b9 64 65 66 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f  def SQLITE_FILE_
148ba 48 45 41 44 45 52 20 2f 2a 20 31 32 33 34 35 36  HEADER /* 123456
148bb 37 38 39 20 31 32 33 34 35 36 20 2a 2f 0a 23 20  789 123456 */.# 
148bc 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46   define SQLITE_F
148bd 49 4c 45 5f 48 45 41 44 45 52 20 22 53 51 4c 69  ILE_HEADER "SQLi
148be 74 65 20 66 6f 72 6d 61 74 20 33 22 0a 23 65 6e  te format 3".#en
148bf 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20  dif../*.** Page 
148c0 74 79 70 65 20 66 6c 61 67 73 2e 20 20 41 6e 20  type flags.  An 
148c1 4f 52 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e  ORed combination
148c2 20 6f 66 20 74 68 65 73 65 20 66 6c 61 67 73 20   of these flags 
148c3 61 70 70 65 61 72 20 61 73 20 74 68 65 0a 2a 2a  appear as the.**
148c4 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 6f   first byte of o
148c5 6e 2d 64 69 73 6b 20 69 6d 61 67 65 20 6f 66 20  n-disk image of 
148c6 65 76 65 72 79 20 42 54 72 65 65 20 70 61 67 65  every BTree page
148c7 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 46  ..*/.#define PTF
148c8 5f 49 4e 54 4b 45 59 20 20 20 20 30 78 30 31 0a  _INTKEY    0x01.
148c9 23 64 65 66 69 6e 65 20 50 54 46 5f 5a 45 52 4f  #define PTF_ZERO
148ca 44 41 54 41 20 20 30 78 30 32 0a 23 64 65 66 69  DATA  0x02.#defi
148cb 6e 65 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20  ne PTF_LEAFDATA 
148cc 20 30 78 30 34 0a 23 64 65 66 69 6e 65 20 50 54   0x04.#define PT
148cd 46 5f 4c 45 41 46 20 20 20 20 20 20 30 78 30 38  F_LEAF      0x08
148ce 0a 0a 2f 2a 0a 2a 2a 20 41 73 20 65 61 63 68 20  ../*.** As each 
148cf 70 61 67 65 20 6f 66 20 74 68 65 20 66 69 6c 65  page of the file
148d0 20 69 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20   is loaded into 
148d1 6d 65 6d 6f 72 79 2c 20 61 6e 20 69 6e 73 74 61  memory, an insta
148d2 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
148d3 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72  wing.** structur
148d4 65 20 69 73 20 61 70 70 65 6e 64 65 64 20 61 6e  e is appended an
148d5 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f  d initialized to
148d6 20 7a 65 72 6f 2e 20 20 54 68 69 73 20 73 74 72   zero.  This str
148d7 75 63 74 75 72 65 20 73 74 6f 72 65 73 0a 2a 2a  ucture stores.**
148d8 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
148d9 75 74 20 74 68 65 20 70 61 67 65 20 74 68 61 74  ut the page that
148da 20 69 73 20 64 65 63 6f 64 65 64 20 66 72 6f 6d   is decoded from
148db 20 74 68 65 20 72 61 77 20 66 69 6c 65 20 70 61   the raw file pa
148dc 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 50  ge..**.** The pP
148dd 61 72 65 6e 74 20 66 69 65 6c 64 20 70 6f 69 6e  arent field poin
148de 74 73 20 62 61 63 6b 20 74 6f 20 74 68 65 20 70  ts back to the p
148df 61 72 65 6e 74 20 70 61 67 65 2e 20 20 54 68 69  arent page.  Thi
148e0 73 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f 0a 2a  s allows us to.*
148e1 2a 20 77 61 6c 6b 20 75 70 20 74 68 65 20 42 54  * walk up the BT
148e2 72 65 65 20 66 72 6f 6d 20 61 6e 79 20 6c 65 61  ree from any lea
148e3 66 20 74 6f 20 74 68 65 20 72 6f 6f 74 2e 20 20  f to the root.  
148e4 43 61 72 65 20 6d 75 73 74 20 62 65 20 74 61 6b  Care must be tak
148e5 65 6e 20 74 6f 0a 2a 2a 20 75 6e 72 65 66 28 29  en to.** unref()
148e6 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
148e7 20 70 6f 69 6e 74 65 72 20 77 68 65 6e 20 74 68   pointer when th
148e8 69 73 20 70 61 67 65 20 69 73 20 6e 6f 20 6c 6f  is page is no lo
148e9 6e 67 65 72 20 72 65 66 65 72 65 6e 63 65 64 2e  nger referenced.
148ea 0a 2a 2a 20 54 68 65 20 70 61 67 65 44 65 73 74  .** The pageDest
148eb 72 75 63 74 6f 72 28 29 20 72 6f 75 74 69 6e 65  ructor() routine
148ec 20 68 61 6e 64 6c 65 73 20 74 68 61 74 20 63 68   handles that ch
148ed 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73  ore..**.** Acces
148ee 73 20 74 6f 20 61 6c 6c 20 66 69 65 6c 64 73 20  s to all fields 
148ef 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
148f0 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20  e is controlled 
148f1 62 79 20 74 68 65 20 6d 75 74 65 78 0a 2a 2a 20  by the mutex.** 
148f2 73 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50 61 67  stored in MemPag
148f3 65 2e 70 42 74 2d 3e 6d 75 74 65 78 2e 0a 2a 2f  e.pBt->mutex..*/
148f4 0a 73 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20  .struct MemPage 
148f5 7b 0a 20 20 75 38 20 69 73 49 6e 69 74 3b 20 20  {.  u8 isInit;  
148f6 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
148f7 20 69 66 20 70 72 65 76 69 6f 75 73 6c 79 20 69   if previously i
148f8 6e 69 74 69 61 6c 69 7a 65 64 2e 20 4d 55 53 54  nitialized. MUST
148f9 20 42 45 20 46 49 52 53 54 21 20 2a 2f 0a 20 20   BE FIRST! */.  
148fa 75 38 20 6e 4f 76 65 72 66 6c 6f 77 3b 20 20 20  u8 nOverflow;   
148fb 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
148fc 66 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20  f overflow cell 
148fd 62 6f 64 69 65 73 20 69 6e 20 61 43 65 6c 6c 5b  bodies in aCell[
148fe 5d 20 2a 2f 0a 20 20 75 38 20 69 6e 74 4b 65 79  ] */.  u8 intKey
148ff 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
14900 72 75 65 20 69 66 20 69 6e 74 6b 65 79 20 66 6c  rue if intkey fl
14901 61 67 20 69 73 20 73 65 74 20 2a 2f 0a 20 20 75  ag is set */.  u
14902 38 20 6c 65 61 66 3b 20 20 20 20 20 20 20 20 20  8 leaf;         
14903 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c      /* True if l
14904 65 61 66 20 66 6c 61 67 20 69 73 20 73 65 74 20  eaf flag is set 
14905 2a 2f 0a 20 20 75 38 20 68 61 73 44 61 74 61 3b  */.  u8 hasData;
14906 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
14907 65 20 69 66 20 74 68 69 73 20 70 61 67 65 20 73  e if this page s
14908 74 6f 72 65 73 20 64 61 74 61 20 2a 2f 0a 20 20  tores data */.  
14909 75 38 20 68 64 72 4f 66 66 73 65 74 3b 20 20 20  u8 hdrOffset;   
1490a 20 20 20 20 20 2f 2a 20 31 30 30 20 66 6f 72 20       /* 100 for 
1490b 70 61 67 65 20 31 2e 20 20 30 20 6f 74 68 65 72  page 1.  0 other
1490c 77 69 73 65 20 2a 2f 0a 20 20 75 38 20 63 68 69  wise */.  u8 chi
1490d 6c 64 50 74 72 53 69 7a 65 3b 20 20 20 20 20 2f  ldPtrSize;     /
1490e 2a 20 30 20 69 66 20 6c 65 61 66 3d 3d 31 2e 20  * 0 if leaf==1. 
1490f 20 34 20 69 66 20 6c 65 61 66 3d 3d 30 20 2a 2f   4 if leaf==0 */
14910 0a 20 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c 3b  .  u16 maxLocal;
14911 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20          /* Copy 
14912 6f 66 20 42 74 53 68 61 72 65 64 2e 6d 61 78 4c  of BtShared.maxL
14913 6f 63 61 6c 20 6f 72 20 42 74 53 68 61 72 65 64  ocal or BtShared
14914 2e 6d 61 78 4c 65 61 66 20 2a 2f 0a 20 20 75 31  .maxLeaf */.  u1
14915 36 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20  6 minLocal;     
14916 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74     /* Copy of Bt
14917 53 68 61 72 65 64 2e 6d 69 6e 4c 6f 63 61 6c 20  Shared.minLocal 
14918 6f 72 20 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c  or BtShared.minL
14919 65 61 66 20 2a 2f 0a 20 20 75 31 36 20 63 65 6c  eaf */.  u16 cel
1491a 6c 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a  lOffset;      /*
1491b 20 49 6e 64 65 78 20 69 6e 20 61 44 61 74 61 20   Index in aData 
1491c 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f  of first cell po
1491d 69 6e 74 65 72 20 2a 2f 0a 20 20 75 31 36 20 6e  inter */.  u16 n
1491e 46 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20  Free;           
1491f 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65  /* Number of fre
14920 65 20 62 79 74 65 73 20 6f 6e 20 74 68 65 20 70  e bytes on the p
14921 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 6e 43 65  age */.  u16 nCe
14922 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ll;           /*
14923 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73   Number of cells
14924 20 6f 6e 20 74 68 69 73 20 70 61 67 65 2c 20 6c   on this page, l
14925 6f 63 61 6c 20 61 6e 64 20 6f 76 66 6c 20 2a 2f  ocal and ovfl */
14926 0a 20 20 75 31 36 20 6d 61 73 6b 50 61 67 65 3b  .  u16 maskPage;
14927 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
14928 66 6f 72 20 70 61 67 65 20 6f 66 66 73 65 74 20  for page offset 
14929 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 4f 76 66  */.  struct _Ovf
1492a 6c 43 65 6c 6c 20 7b 20 20 20 2f 2a 20 43 65 6c  lCell {   /* Cel
1492b 6c 73 20 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74  ls that will not
1492c 20 66 69 74 20 6f 6e 20 61 44 61 74 61 5b 5d 20   fit on aData[] 
1492d 2a 2f 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c  */.    u8 *pCell
1492e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f  ;          /* Po
1492f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 62 6f  inters to the bo
14930 64 79 20 6f 66 20 74 68 65 20 6f 76 65 72 66 6c  dy of the overfl
14931 6f 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 75  ow cell */.    u
14932 31 36 20 69 64 78 3b 20 20 20 20 20 20 20 20 20  16 idx;         
14933 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 69     /* Insert thi
14934 73 20 63 65 6c 6c 20 62 65 66 6f 72 65 20 69 64  s cell before id
14935 78 2d 74 68 20 6e 6f 6e 2d 6f 76 65 72 66 6c 6f  x-th non-overflo
14936 77 20 63 65 6c 6c 20 2a 2f 0a 20 20 7d 20 61 4f  w cell */.  } aO
14937 76 66 6c 5b 35 5d 3b 0a 20 20 42 74 53 68 61 72  vfl[5];.  BtShar
14938 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 2f  ed *pBt;       /
14939 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 42 74 53  * Pointer to BtS
1493a 68 61 72 65 64 20 74 68 61 74 20 74 68 69 73 20  hared that this 
1493b 70 61 67 65 20 69 73 20 70 61 72 74 20 6f 66 20  page is part of 
1493c 2a 2f 0a 20 20 75 38 20 2a 61 44 61 74 61 3b 20  */.  u8 *aData; 
1493d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
1493e 6e 74 65 72 20 74 6f 20 64 69 73 6b 20 69 6d 61  nter to disk ima
1493f 67 65 20 6f 66 20 74 68 65 20 70 61 67 65 20 64  ge of the page d
14940 61 74 61 20 2a 2f 0a 20 20 44 62 50 61 67 65 20  ata */.  DbPage 
14941 2a 70 44 62 50 61 67 65 3b 20 20 20 20 20 2f 2a  *pDbPage;     /*
14942 20 50 61 67 65 72 20 70 61 67 65 20 68 61 6e 64   Pager page hand
14943 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e  le */.  Pgno pgn
14944 6f 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  o;           /* 
14945 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20  Page number for 
14946 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a  this page */.};.
14947 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65  ./*.** The in-me
14948 6d 6f 72 79 20 69 6d 61 67 65 20 6f 66 20 61 20  mory image of a 
14949 64 69 73 6b 20 70 61 67 65 20 68 61 73 20 74 68  disk page has th
1494a 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f  e auxiliary info
1494b 72 6d 61 74 69 6f 6e 20 61 70 70 65 6e 64 65 64  rmation appended
1494c 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6e 64 2e 20  .** to the end. 
1494d 20 45 58 54 52 41 5f 53 49 5a 45 20 69 73 20 74   EXTRA_SIZE is t
1494e 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
1494f 65 73 20 6f 66 20 73 70 61 63 65 20 6e 65 65 64  es of space need
14950 65 64 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 74 68  ed to hold.** th
14951 61 74 20 65 78 74 72 61 20 69 6e 66 6f 72 6d 61  at extra informa
14952 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  tion..*/.#define
14953 20 45 58 54 52 41 5f 53 49 5a 45 20 73 69 7a 65   EXTRA_SIZE size
14954 6f 66 28 4d 65 6d 50 61 67 65 29 0a 0a 2f 2a 0a  of(MemPage)../*.
14955 2a 2a 20 41 20 6c 69 6e 6b 65 64 20 6c 69 73 74  ** A linked list
14956 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
14957 67 20 73 74 72 75 63 74 75 72 65 73 20 69 73 20  g structures is 
14958 73 74 6f 72 65 64 20 61 74 20 42 74 53 68 61 72  stored at BtShar
14959 65 64 2e 70 4c 6f 63 6b 2e 0a 2a 2a 20 4c 6f 63  ed.pLock..** Loc
1495a 6b 73 20 61 72 65 20 61 64 64 65 64 20 28 6f 72  ks are added (or
1495b 20 75 70 67 72 61 64 65 64 20 66 72 6f 6d 20 52   upgraded from R
1495c 45 41 44 5f 4c 4f 43 4b 20 74 6f 20 57 52 49 54  EAD_LOCK to WRIT
1495d 45 5f 4c 4f 43 4b 29 20 77 68 65 6e 20 61 20 63  E_LOCK) when a c
1495e 75 72 73 6f 72 20 0a 2a 2a 20 69 73 20 6f 70 65  ursor .** is ope
1495f 6e 65 64 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  ned on the table
14960 20 77 69 74 68 20 72 6f 6f 74 20 70 61 67 65 20   with root page 
14961 42 74 53 68 61 72 65 64 2e 69 54 61 62 6c 65 2e  BtShared.iTable.
14962 20 4c 6f 63 6b 73 20 61 72 65 20 72 65 6d 6f 76   Locks are remov
14963 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20  ed.** from this 
14964 6c 69 73 74 20 77 68 65 6e 20 61 20 74 72 61 6e  list when a tran
14965 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69  saction is commi
14966 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62  tted or rolled b
14967 61 63 6b 2c 20 6f 72 20 77 68 65 6e 0a 2a 2a 20  ack, or when.** 
14968 61 20 62 74 72 65 65 20 68 61 6e 64 6c 65 20 69  a btree handle i
14969 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 73 74 72  s closed..*/.str
1496a 75 63 74 20 42 74 4c 6f 63 6b 20 7b 0a 20 20 42  uct BtLock {.  B
1496b 74 72 65 65 20 2a 70 42 74 72 65 65 3b 20 20 20  tree *pBtree;   
1496c 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 68 61       /* Btree ha
1496d 6e 64 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 69  ndle holding thi
1496e 73 20 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f  s lock */.  Pgno
1496f 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20   iTable;        
14970 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f    /* Root page o
14971 66 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20  f table */.  u8 
14972 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20  eLock;          
14973 20 20 20 2f 2a 20 52 45 41 44 5f 4c 4f 43 4b 20     /* READ_LOCK 
14974 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 20 2a 2f  or WRITE_LOCK */
14975 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4e 65 78 74  .  BtLock *pNext
14976 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74  ;        /* Next
14977 20 69 6e 20 42 74 53 68 61 72 65 64 2e 70 4c 6f   in BtShared.pLo
14978 63 6b 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f  ck list */.};../
14979 2a 20 43 61 6e 64 69 64 61 74 65 20 76 61 6c 75  * Candidate valu
1497a 65 73 20 66 6f 72 20 42 74 4c 6f 63 6b 2e 65 4c  es for BtLock.eL
1497b 6f 63 6b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 52  ock */.#define R
1497c 45 41 44 5f 4c 4f 43 4b 20 20 20 20 20 31 0a 23  EAD_LOCK     1.#
1497d 64 65 66 69 6e 65 20 57 52 49 54 45 5f 4c 4f 43  define WRITE_LOC
1497e 4b 20 20 20 20 32 0a 0a 2f 2a 20 41 20 42 74 72  K    2../* A Btr
1497f 65 65 20 68 61 6e 64 6c 65 0a 2a 2a 0a 2a 2a 20  ee handle.**.** 
14980 41 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  A database conne
14981 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 61  ction contains a
14982 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69   pointer to an i
14983 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68  nstance of.** th
14984 69 73 20 6f 62 6a 65 63 74 20 66 6f 72 20 65 76  is object for ev
14985 65 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c  ery database fil
14986 65 20 74 68 61 74 20 69 74 20 68 61 73 20 6f 70  e that it has op
14987 65 6e 2e 20 20 54 68 69 73 20 73 74 72 75 63 74  en.  This struct
14988 75 72 65 0a 2a 2a 20 69 73 20 6f 70 61 71 75 65  ure.** is opaque
14989 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
1498a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68   connection.  Th
1498b 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
1498c 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a 2a 20  ction cannot.** 
1498d 73 65 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  see the internal
1498e 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  s of this struct
1498f 75 72 65 20 61 6e 64 20 6f 6e 6c 79 20 64 65 61  ure and only dea
14990 6c 73 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73  ls with pointers
14991 20 74 6f 0a 2a 2a 20 74 68 69 73 20 73 74 72 75   to.** this stru
14992 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  cture..**.** For
14993 20 73 6f 6d 65 20 64 61 74 61 62 61 73 65 20 66   some database f
14994 69 6c 65 73 2c 20 74 68 65 20 73 61 6d 65 20 75  iles, the same u
14995 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 62 61  nderlying databa
14996 73 65 20 63 61 63 68 65 20 6d 69 67 68 74 20 62  se cache might b
14997 65 20 0a 2a 2a 20 73 68 61 72 65 64 20 62 65 74  e .** shared bet
14998 77 65 65 6e 20 6d 75 6c 74 69 70 6c 65 20 63 6f  ween multiple co
14999 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 49 6e 20 74  nnections.  In t
1499a 68 61 74 20 63 61 73 65 2c 20 65 61 63 68 20 63  hat case, each c
1499b 6f 6e 74 65 63 74 69 6f 6e 0a 2a 2a 20 68 61 73  ontection.** has
1499c 20 69 74 20 6f 77 6e 20 70 6f 69 6e 74 65 72 20   it own pointer 
1499d 74 6f 20 74 68 69 73 20 6f 62 6a 65 63 74 2e 20  to this object. 
1499e 20 42 75 74 20 65 61 63 68 20 69 6e 73 74 61 6e   But each instan
1499f 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63  ce of this objec
149a0 74 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 74  t.** points to t
149a1 68 65 20 73 61 6d 65 20 42 74 53 68 61 72 65 64  he same BtShared
149a2 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 64 61   object.  The da
149a3 74 61 62 61 73 65 20 63 61 63 68 65 20 61 6e 64  tabase cache and
149a4 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 61   the.** schema a
149a5 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
149a6 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
149a7 20 61 72 65 20 61 6c 6c 20 63 6f 6e 74 61 69 6e   are all contain
149a8 65 64 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65  ed within.** the
149a9 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74   BtShared object
149aa 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 66 69 65 6c  ..**.** All fiel
149ab 64 73 20 69 6e 20 74 68 69 73 20 73 74 72 75 63  ds in this struc
149ac 74 75 72 65 20 61 72 65 20 61 63 63 65 73 73 65  ture are accesse
149ad 64 20 75 6e 64 65 72 20 73 71 6c 69 74 65 33 2e  d under sqlite3.
149ae 6d 75 74 65 78 2e 0a 2a 2a 20 54 68 65 20 70 42  mutex..** The pB
149af 74 20 70 6f 69 6e 74 65 72 20 69 74 73 65 6c 66  t pointer itself
149b0 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 68 61 6e   may not be chan
149b1 67 65 64 20 77 68 69 6c 65 20 74 68 65 72 65 20  ged while there 
149b2 65 78 69 73 74 73 20 63 75 72 73 6f 72 73 20 0a  exists cursors .
149b3 2a 2a 20 69 6e 20 74 68 65 20 72 65 66 65 72 65  ** in the refere
149b4 6e 63 65 64 20 42 74 53 68 61 72 65 64 20 74 68  nced BtShared th
149b5 61 74 20 70 6f 69 6e 74 20 62 61 63 6b 20 74 6f  at point back to
149b6 20 74 68 69 73 20 42 74 72 65 65 20 73 69 6e 63   this Btree sinc
149b7 65 20 74 68 6f 73 65 0a 2a 2a 20 63 75 72 73 6f  e those.** curso
149b8 72 73 20 68 61 76 65 20 74 6f 20 64 6f 20 67 6f  rs have to do go
149b9 20 74 68 72 6f 75 67 68 20 74 68 69 73 20 42 74   through this Bt
149ba 72 65 65 20 74 6f 20 66 69 6e 64 20 74 68 65 69  ree to find thei
149bb 72 20 42 74 53 68 61 72 65 64 20 61 6e 64 0a 2a  r BtShared and.*
149bc 2a 20 74 68 65 79 20 6f 66 74 65 6e 20 64 6f 20  * they often do 
149bd 73 6f 20 77 69 74 68 6f 75 74 20 68 6f 6c 64 69  so without holdi
149be 6e 67 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78  ng sqlite3.mutex
149bf 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 72 65  ..*/.struct Btre
149c0 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  e {.  sqlite3 *d
149c1 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  b;       /* The 
149c2 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
149c3 69 6f 6e 20 68 6f 6c 64 69 6e 67 20 74 68 69 73  ion holding this
149c4 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 53 68   btree */.  BtSh
149c5 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 2f  ared *pBt;     /
149c6 2a 20 53 68 61 72 61 62 6c 65 20 63 6f 6e 74 65  * Sharable conte
149c7 6e 74 20 6f 66 20 74 68 69 73 20 62 74 72 65 65  nt of this btree
149c8 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e 73   */.  u8 inTrans
149c9 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 52 41 4e  ;        /* TRAN
149ca 53 5f 4e 4f 4e 45 2c 20 54 52 41 4e 53 5f 52 45  S_NONE, TRANS_RE
149cb 41 44 20 6f 72 20 54 52 41 4e 53 5f 57 52 49 54  AD or TRANS_WRIT
149cc 45 20 2a 2f 0a 20 20 75 38 20 73 68 61 72 61 62  E */.  u8 sharab
149cd 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75  le;       /* Tru
149ce 65 20 69 66 20 77 65 20 63 61 6e 20 73 68 61 72  e if we can shar
149cf 65 20 70 42 74 20 77 69 74 68 20 61 6e 6f 74 68  e pBt with anoth
149d0 65 72 20 64 62 20 2a 2f 0a 20 20 75 38 20 6c 6f  er db */.  u8 lo
149d1 63 6b 65 64 3b 20 20 20 20 20 20 20 20 20 2f 2a  cked;         /*
149d2 20 54 72 75 65 20 69 66 20 64 62 20 63 75 72 72   True if db curr
149d3 65 6e 74 6c 79 20 68 61 73 20 70 42 74 20 6c 6f  ently has pBt lo
149d4 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 77 61  cked */.  int wa
149d5 6e 74 54 6f 4c 6f 63 6b 3b 20 20 20 20 2f 2a 20  ntToLock;    /* 
149d6 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64  Number of nested
149d7 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65   calls to sqlite
149d8 33 42 74 72 65 65 45 6e 74 65 72 28 29 20 2a 2f  3BtreeEnter() */
149d9 0a 20 20 69 6e 74 20 6e 42 61 63 6b 75 70 3b 20  .  int nBackup; 
149da 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
149db 6f 66 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74  of backup operat
149dc 69 6f 6e 73 20 72 65 61 64 69 6e 67 20 74 68 69  ions reading thi
149dd 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 72  s btree */.  Btr
149de 65 65 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20  ee *pNext;      
149df 2f 2a 20 4c 69 73 74 20 6f 66 20 6f 74 68 65 72  /* List of other
149e0 20 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73   sharable Btrees
149e1 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 64   from the same d
149e2 62 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 50  b */.  Btree *pP
149e3 72 65 76 3b 20 20 20 20 20 20 2f 2a 20 42 61 63  rev;      /* Bac
149e4 6b 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68 65  k pointer of the
149e5 20 73 61 6d 65 20 6c 69 73 74 20 2a 2f 0a 23 69   same list */.#i
149e6 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
149e7 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20  T_SHARED_CACHE. 
149e8 20 42 74 4c 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20   BtLock lock;   
149e9 20 20 20 20 2f 2a 20 4f 62 6a 65 63 74 20 75 73      /* Object us
149ea 65 64 20 74 6f 20 6c 6f 63 6b 20 70 61 67 65 20  ed to lock page 
149eb 31 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a  1 */.#endif.};..
149ec 2f 2a 0a 2a 2a 20 42 74 72 65 65 2e 69 6e 54 72  /*.** Btree.inTr
149ed 61 6e 73 20 6d 61 79 20 74 61 6b 65 20 6f 6e 65  ans may take one
149ee 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
149ef 67 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20  g values..**.** 
149f0 49 66 20 74 68 65 20 73 68 61 72 65 64 2d 64 61  If the shared-da
149f1 74 61 20 65 78 74 65 6e 73 69 6f 6e 20 69 73 20  ta extension is 
149f2 65 6e 61 62 6c 65 64 2c 20 74 68 65 72 65 20 6d  enabled, there m
149f3 61 79 20 62 65 20 6d 75 6c 74 69 70 6c 65 20 75  ay be multiple u
149f4 73 65 72 73 0a 2a 2a 20 6f 66 20 74 68 65 20 42  sers.** of the B
149f5 74 72 65 65 20 73 74 72 75 63 74 75 72 65 2e 20  tree structure. 
149f6 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 66 20 74  At most one of t
149f7 68 65 73 65 20 6d 61 79 20 6f 70 65 6e 20 61 20  hese may open a 
149f8 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
149f9 6e 2c 0a 2a 2a 20 62 75 74 20 61 6e 79 20 6e 75  n,.** but any nu
149fa 6d 62 65 72 20 6d 61 79 20 68 61 76 65 20 61 63  mber may have ac
149fb 74 69 76 65 20 72 65 61 64 20 74 72 61 6e 73 61  tive read transa
149fc 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69  ctions..*/.#defi
149fd 6e 65 20 54 52 41 4e 53 5f 4e 4f 4e 45 20 20 30  ne TRANS_NONE  0
149fe 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 52  .#define TRANS_R
149ff 45 41 44 20 20 31 0a 23 64 65 66 69 6e 65 20 54  EAD  1.#define T
14a00 52 41 4e 53 5f 57 52 49 54 45 20 32 0a 0a 2f 2a  RANS_WRITE 2../*
14a01 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
14a02 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72  of this object r
14a03 65 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67  epresents a sing
14a04 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  le database file
14a05 2e 0a 2a 2a 20 0a 2a 2a 20 41 20 73 69 6e 67 6c  ..** .** A singl
14a06 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
14a07 63 61 6e 20 62 65 20 69 6e 20 75 73 65 20 61 73  can be in use as
14a08 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 62   the same time b
14a09 79 20 74 77 6f 0a 2a 2a 20 6f 72 20 6d 6f 72 65  y two.** or more
14a0a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
14a0b 74 69 6f 6e 73 2e 20 20 57 68 65 6e 20 74 77 6f  tions.  When two
14a0c 20 6f 72 20 6d 6f 72 65 20 63 6f 6e 6e 65 63 74   or more connect
14a0d 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 73 68 61 72  ions are.** shar
14a0e 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74  ing the same dat
14a0f 61 62 61 73 65 20 66 69 6c 65 2c 20 65 61 63 68  abase file, each
14a10 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20   connection has 
14a11 69 74 20 6f 77 6e 0a 2a 2a 20 70 72 69 76 61 74  it own.** privat
14a12 65 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 66  e Btree object f
14a13 6f 72 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20  or the file and 
14a14 65 61 63 68 20 6f 66 20 74 68 6f 73 65 20 42 74  each of those Bt
14a15 72 65 65 73 20 70 6f 69 6e 74 73 0a 2a 2a 20 74  rees points.** t
14a16 6f 20 74 68 69 73 20 6f 6e 65 20 42 74 53 68 61  o this one BtSha
14a17 72 65 64 20 6f 62 6a 65 63 74 2e 20 20 42 74 53  red object.  BtS
14a18 68 61 72 65 64 2e 6e 52 65 66 20 69 73 20 74 68  hared.nRef is th
14a19 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63  e number of.** c
14a1a 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 75 72 72 65  onnections curre
14a1b 6e 74 6c 79 20 73 68 61 72 69 6e 67 20 74 68 69  ntly sharing thi
14a1c 73 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  s database file.
14a1d 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 20 69 6e  .**.** Fields in
14a1e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
14a1f 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64  are accessed und
14a20 65 72 20 74 68 65 20 42 74 53 68 61 72 65 64 2e  er the BtShared.
14a21 6d 75 74 65 78 0a 2a 2a 20 6d 75 74 65 78 2c 20  mutex.** mutex, 
14a22 65 78 63 65 70 74 20 66 6f 72 20 6e 52 65 66 20  except for nRef 
14a23 61 6e 64 20 70 4e 65 78 74 20 77 68 69 63 68 20  and pNext which 
14a24 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64  are accessed und
14a25 65 72 20 74 68 65 0a 2a 2a 20 67 6c 6f 62 61 6c  er the.** global
14a26 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
14a27 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65  ATIC_MASTER mute
14a28 78 2e 20 20 54 68 65 20 70 50 61 67 65 72 20 66  x.  The pPager f
14a29 69 65 6c 64 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20  ield.** may not 
14a2a 62 65 20 6d 6f 64 69 66 69 65 64 20 6f 6e 63 65  be modified once
14a2b 20 69 74 20 69 73 20 69 6e 69 74 69 61 6c 6c 79   it is initially
14a2c 20 73 65 74 20 61 73 20 6c 6f 6e 67 20 61 73 20   set as long as 
14a2d 6e 52 65 66 3e 30 2e 0a 2a 2a 20 54 68 65 20 70  nRef>0..** The p
14a2e 53 63 68 65 6d 61 20 66 69 65 6c 64 20 6d 61 79  Schema field may
14a2f 20 62 65 20 73 65 74 20 6f 6e 63 65 20 75 6e 64   be set once und
14a30 65 72 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65  er BtShared.mute
14a31 78 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65 61 66  x and.** thereaf
14a32 74 65 72 20 69 73 20 75 6e 63 68 61 6e 67 65 64  ter is unchanged
14a33 20 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65 66   as long as nRef
14a34 3e 30 2e 0a 2a 2a 0a 2a 2a 20 69 73 50 65 6e 64  >0..**.** isPend
14a35 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 49 66 20  ing:.**.**   If 
14a36 61 20 42 74 53 68 61 72 65 64 20 63 6c 69 65 6e  a BtShared clien
14a37 74 20 66 61 69 6c 73 20 74 6f 20 6f 62 74 61 69  t fails to obtai
14a38 6e 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f  n a write-lock o
14a39 6e 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  n a database.** 
14a3a 20 20 74 61 62 6c 65 20 28 62 65 63 61 75 73 65    table (because
14a3b 20 74 68 65 72 65 20 65 78 69 73 74 73 20 6f 6e   there exists on
14a3c 65 20 6f 72 20 6d 6f 72 65 20 72 65 61 64 2d 6c  e or more read-l
14a3d 6f 63 6b 73 20 6f 6e 20 74 68 65 20 74 61 62 6c  ocks on the tabl
14a3e 65 29 2c 0a 2a 2a 20 20 20 74 68 65 20 73 68 61  e),.**   the sha
14a3f 72 65 64 2d 63 61 63 68 65 20 65 6e 74 65 72 73  red-cache enters
14a40 20 27 70 65 6e 64 69 6e 67 2d 6c 6f 63 6b 27 20   'pending-lock' 
14a41 73 74 61 74 65 20 61 6e 64 20 69 73 50 65 6e 64  state and isPend
14a42 69 6e 67 20 69 73 0a 2a 2a 20 20 20 73 65 74 20  ing is.**   set 
14a43 74 6f 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20  to true..**.**  
14a44 20 54 68 65 20 73 68 61 72 65 64 2d 63 61 63 68   The shared-cach
14a45 65 20 6c 65 61 76 65 73 20 74 68 65 20 27 70 65  e leaves the 'pe
14a46 6e 64 69 6e 67 20 6c 6f 63 6b 27 20 73 74 61 74  nding lock' stat
14a47 65 20 77 68 65 6e 20 65 69 74 68 65 72 20 6f 66  e when either of
14a48 0a 2a 2a 20 20 20 74 68 65 20 66 6f 6c 6c 6f 77  .**   the follow
14a49 69 6e 67 20 6f 63 63 75 72 3a 0a 2a 2a 0a 2a 2a  ing occur:.**.**
14a4a 20 20 20 20 20 31 29 20 54 68 65 20 63 75 72 72       1) The curr
14a4b 65 6e 74 20 77 72 69 74 65 72 20 28 42 74 53 68  ent writer (BtSh
14a4c 61 72 65 64 2e 70 57 72 69 74 65 72 29 20 63 6f  ared.pWriter) co
14a4d 6e 63 6c 75 64 65 73 20 69 74 73 20 74 72 61 6e  ncludes its tran
14a4e 73 61 63 74 69 6f 6e 2c 20 4f 52 0a 2a 2a 20 20  saction, OR.**  
14a4f 20 20 20 32 29 20 54 68 65 20 6e 75 6d 62 65 72     2) The number
14a50 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20 62   of locks held b
14a51 79 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69  y other connecti
14a52 6f 6e 73 20 64 72 6f 70 73 20 74 6f 20 7a 65 72  ons drops to zer
14a53 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 77 68 69 6c 65  o..**.**   while
14a54 20 69 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67   in the 'pending
14a55 2d 6c 6f 63 6b 27 20 73 74 61 74 65 2c 20 6e 6f  -lock' state, no
14a56 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 61 79 20   connection may 
14a57 73 74 61 72 74 20 61 20 6e 65 77 0a 2a 2a 20 20  start a new.**  
14a58 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a   transaction..**
14a59 0a 2a 2a 20 20 20 54 68 69 73 20 66 65 61 74 75  .**   This featu
14a5a 72 65 20 69 73 20 69 6e 63 6c 75 64 65 64 20 74  re is included t
14a5b 6f 20 68 65 6c 70 20 70 72 65 76 65 6e 74 20 77  o help prevent w
14a5c 72 69 74 65 72 2d 73 74 61 72 76 61 74 69 6f 6e  riter-starvation
14a5d 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 53 68  ..*/.struct BtSh
14a5e 61 72 65 64 20 7b 0a 20 20 50 61 67 65 72 20 2a  ared {.  Pager *
14a5f 70 50 61 67 65 72 3b 20 20 20 20 20 20 20 20 2f  pPager;        /
14a60 2a 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65  * The page cache
14a61 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
14a62 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  b;          /* D
14a63 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
14a64 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 73 69  on currently usi
14a65 6e 67 20 74 68 69 73 20 42 74 72 65 65 20 2a 2f  ng this Btree */
14a66 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75  .  BtCursor *pCu
14a67 72 73 6f 72 3b 20 20 20 20 2f 2a 20 41 20 6c 69  rsor;    /* A li
14a68 73 74 20 6f 66 20 61 6c 6c 20 6f 70 65 6e 20 63  st of all open c
14a69 75 72 73 6f 72 73 20 2a 2f 0a 20 20 4d 65 6d 50  ursors */.  MemP
14a6a 61 67 65 20 2a 70 50 61 67 65 31 3b 20 20 20 20  age *pPage1;    
14a6b 20 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65 20    /* First page 
14a6c 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
14a6d 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c 79  */.  u8 readOnly
14a6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ;          /* Tr
14a6f 75 65 20 69 66 20 74 68 65 20 75 6e 64 65 72 6c  ue if the underl
14a70 79 69 6e 67 20 66 69 6c 65 20 69 73 20 72 65 61  ying file is rea
14a71 64 6f 6e 6c 79 20 2a 2f 0a 20 20 75 38 20 70 61  donly */.  u8 pa
14a72 67 65 53 69 7a 65 46 69 78 65 64 3b 20 20 20 20  geSizeFixed;    
14a73 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
14a74 70 61 67 65 20 73 69 7a 65 20 63 61 6e 20 6e 6f  page size can no
14a75 20 6c 6f 6e 67 65 72 20 62 65 20 63 68 61 6e 67   longer be chang
14a76 65 64 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ed */.#ifndef SQ
14a77 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
14a78 43 55 55 4d 0a 20 20 75 38 20 61 75 74 6f 56 61  CUUM.  u8 autoVa
14a79 63 75 75 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20  cuum;        /* 
14a7a 54 72 75 65 20 69 66 20 61 75 74 6f 2d 76 61 63  True if auto-vac
14a7b 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 2a  uum is enabled *
14a7c 2f 0a 20 20 75 38 20 69 6e 63 72 56 61 63 75 75  /.  u8 incrVacuu
14a7d 6d 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  m;        /* Tru
14a7e 65 20 69 66 20 69 6e 63 72 2d 76 61 63 75 75 6d  e if incr-vacuum
14a7f 20 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 23   is enabled */.#
14a80 65 6e 64 69 66 0a 20 20 75 31 36 20 70 61 67 65  endif.  u16 page
14a81 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 2f 2a  Size;         /*
14a82 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
14a83 20 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 65   bytes on a page
14a84 20 2a 2f 0a 20 20 75 31 36 20 75 73 61 62 6c 65   */.  u16 usable
14a85 53 69 7a 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e  Size;       /* N
14a86 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 20  umber of usable 
14a87 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61  bytes on each pa
14a88 67 65 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78 4c  ge */.  u16 maxL
14a89 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a  ocal;         /*
14a8a 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c 20 70   Maximum local p
14a8b 61 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d 4c 45  ayload in non-LE
14a8c 41 46 44 41 54 41 20 74 61 62 6c 65 73 20 2a 2f  AFDATA tables */
14a8d 0a 20 20 75 31 36 20 6d 69 6e 4c 6f 63 61 6c 3b  .  u16 minLocal;
14a8e 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69           /* Mini
14a8f 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61  mum local payloa
14a90 64 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41 54  d in non-LEAFDAT
14a91 41 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 75 31  A tables */.  u1
14a92 36 20 6d 61 78 4c 65 61 66 3b 20 20 20 20 20 20  6 maxLeaf;      
14a93 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c      /* Maximum l
14a94 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20  ocal payload in 
14a95 61 20 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65  a LEAFDATA table
14a96 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 65 61   */.  u16 minLea
14a97 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  f;          /* M
14a98 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79  inimum local pay
14a99 6c 6f 61 64 20 69 6e 20 61 20 4c 45 41 46 44 41  load in a LEAFDA
14a9a 54 41 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 38  TA table */.  u8
14a9b 20 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 20   inTransaction; 
14a9c 20 20 20 20 2f 2a 20 54 72 61 6e 73 61 63 74 69      /* Transacti
14a9d 6f 6e 20 73 74 61 74 65 20 2a 2f 0a 20 20 69 6e  on state */.  in
14a9e 74 20 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3b 20  t nTransaction; 
14a9f 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
14aa0 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f   open transactio
14aa1 6e 73 20 28 72 65 61 64 20 2b 20 77 72 69 74 65  ns (read + write
14aa2 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 63  ) */.  void *pSc
14aa3 68 65 6d 61 3b 20 20 20 20 20 20 20 20 2f 2a 20  hema;        /* 
14aa4 50 6f 69 6e 74 65 72 20 74 6f 20 73 70 61 63 65  Pointer to space
14aa5 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71   allocated by sq
14aa6 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61  lite3BtreeSchema
14aa7 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  () */.  void (*x
14aa8 46 72 65 65 53 63 68 65 6d 61 29 28 76 6f 69 64  FreeSchema)(void
14aa9 2a 29 3b 20 20 2f 2a 20 44 65 73 74 72 75 63 74  *);  /* Destruct
14aaa 6f 72 20 66 6f 72 20 42 74 53 68 61 72 65 64 2e  or for BtShared.
14aab 70 53 63 68 65 6d 61 20 2a 2f 0a 20 20 73 71 6c  pSchema */.  sql
14aac 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
14aad 78 3b 20 2f 2a 20 4e 6f 6e 2d 72 65 63 75 72 73  x; /* Non-recurs
14aae 69 76 65 20 6d 75 74 65 78 20 72 65 71 75 69 72  ive mutex requir
14aaf 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 69  ed to access thi
14ab0 73 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 42 69  s struct */.  Bi
14ab1 74 76 65 63 20 2a 70 48 61 73 43 6f 6e 74 65 6e  tvec *pHasConten
14ab2 74 3b 20 20 2f 2a 20 53 65 74 20 6f 66 20 70 61  t;  /* Set of pa
14ab3 67 65 73 20 6d 6f 76 65 64 20 74 6f 20 66 72 65  ges moved to fre
14ab4 65 2d 6c 69 73 74 20 74 68 69 73 20 74 72 61 6e  e-list this tran
14ab5 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64  saction */.#ifnd
14ab6 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
14ab7 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69 6e  HARED_CACHE.  in
14ab8 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20  t nRef;         
14ab9 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
14aba 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74   references to t
14abb 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f  his structure */
14abc 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 4e 65  .  BtShared *pNe
14abd 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74  xt;      /* Next
14abe 20 6f 6e 20 61 20 6c 69 73 74 20 6f 66 20 73 68   on a list of sh
14abf 61 72 61 62 6c 65 20 42 74 53 68 61 72 65 64 20  arable BtShared 
14ac0 73 74 72 75 63 74 73 20 2a 2f 0a 20 20 42 74 4c  structs */.  BtL
14ac1 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 20  ock *pLock;     
14ac2 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6c 6f     /* List of lo
14ac3 63 6b 73 20 68 65 6c 64 20 6f 6e 20 74 68 69 73  cks held on this
14ac4 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 73 74   shared-btree st
14ac5 72 75 63 74 20 2a 2f 0a 20 20 42 74 72 65 65 20  ruct */.  Btree 
14ac6 2a 70 57 72 69 74 65 72 3b 20 20 20 20 20 20 20  *pWriter;       
14ac7 2f 2a 20 42 74 72 65 65 20 77 69 74 68 20 63 75  /* Btree with cu
14ac8 72 72 65 6e 74 6c 79 20 6f 70 65 6e 20 77 72 69  rrently open wri
14ac9 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a  te transaction *
14aca 2f 0a 20 20 75 38 20 69 73 45 78 63 6c 75 73 69  /.  u8 isExclusi
14acb 76 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ve;       /* Tru
14acc 65 20 69 66 20 70 57 72 69 74 65 72 20 68 61 73  e if pWriter has
14acd 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   an EXCLUSIVE lo
14ace 63 6b 20 6f 6e 20 74 68 65 20 64 62 20 2a 2f 0a  ck on the db */.
14acf 20 20 75 38 20 69 73 50 65 6e 64 69 6e 67 3b 20    u8 isPending; 
14ad0 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 61          /* If wa
14ad1 69 74 69 6e 67 20 66 6f 72 20 72 65 61 64 2d 6c  iting for read-l
14ad2 6f 63 6b 73 20 74 6f 20 63 6c 65 61 72 20 2a 2f  ocks to clear */
14ad3 0a 23 65 6e 64 69 66 0a 20 20 75 38 20 2a 70 54  .#endif.  u8 *pT
14ad4 6d 70 53 70 61 63 65 3b 20 20 20 20 20 20 20 20  mpSpace;        
14ad5 2f 2a 20 42 74 53 68 61 72 65 64 2e 70 61 67 65  /* BtShared.page
14ad6 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73 70  Size bytes of sp
14ad7 61 63 65 20 66 6f 72 20 74 6d 70 20 75 73 65 20  ace for tmp use 
14ad8 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  */.};../*.** An 
14ad9 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
14ada 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
14adb 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 68  ure is used to h
14adc 6f 6c 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a  old information.
14add 2a 2a 20 61 62 6f 75 74 20 61 20 63 65 6c 6c 2e  ** about a cell.
14ade 20 20 54 68 65 20 70 61 72 73 65 43 65 6c 6c 50    The parseCellP
14adf 74 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 69  tr() function fi
14ae0 6c 6c 73 20 69 6e 20 74 68 69 73 20 73 74 72 75  lls in this stru
14ae1 63 74 75 72 65 0a 2a 2a 20 62 61 73 65 64 20 6f  cture.** based o
14ae2 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 65 78  n information ex
14ae3 74 72 61 63 74 20 66 72 6f 6d 20 74 68 65 20 72  tract from the r
14ae4 61 77 20 64 69 73 6b 20 70 61 67 65 2e 0a 2a 2f  aw disk page..*/
14ae5 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
14ae6 43 65 6c 6c 49 6e 66 6f 20 43 65 6c 6c 49 6e 66  CellInfo CellInf
14ae7 6f 3b 0a 73 74 72 75 63 74 20 43 65 6c 6c 49 6e  o;.struct CellIn
14ae8 66 6f 20 7b 0a 20 20 75 38 20 2a 70 43 65 6c 6c  fo {.  u8 *pCell
14ae9 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72  ;     /* Pointer
14aea 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66   to the start of
14aeb 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f   cell content */
14aec 0a 20 20 69 36 34 20 6e 4b 65 79 3b 20 20 20 20  .  i64 nKey;    
14aed 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 66 6f 72    /* The key for
14aee 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c 20   INTKEY tables, 
14aef 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  or number of byt
14af0 65 73 20 69 6e 20 6b 65 79 20 2a 2f 0a 20 20 75  es in key */.  u
14af1 33 32 20 6e 44 61 74 61 3b 20 20 20 20 20 2f 2a  32 nData;     /*
14af2 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
14af3 20 6f 66 20 64 61 74 61 20 2a 2f 0a 20 20 75 33   of data */.  u3
14af4 32 20 6e 50 61 79 6c 6f 61 64 3b 20 20 2f 2a 20  2 nPayload;  /* 
14af5 54 6f 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20  Total amount of 
14af6 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 75 31 36  payload */.  u16
14af7 20 6e 48 65 61 64 65 72 3b 20 20 20 2f 2a 20 53   nHeader;   /* S
14af8 69 7a 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  ize of the cell 
14af9 63 6f 6e 74 65 6e 74 20 68 65 61 64 65 72 20 69  content header i
14afa 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 31 36  n bytes */.  u16
14afb 20 6e 4c 6f 63 61 6c 3b 20 20 20 20 2f 2a 20 41   nLocal;    /* A
14afc 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64  mount of payload
14afd 20 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f   held locally */
14afe 0a 20 20 75 31 36 20 69 4f 76 65 72 66 6c 6f 77  .  u16 iOverflow
14aff 3b 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 6f  ; /* Offset to o
14b00 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d  verflow page num
14b01 62 65 72 2e 20 20 5a 65 72 6f 20 69 66 20 6e 6f  ber.  Zero if no
14b02 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 75   overflow */.  u
14b03 31 36 20 6e 53 69 7a 65 3b 20 20 20 20 20 2f 2a  16 nSize;     /*
14b04 20 53 69 7a 65 20 6f 66 20 74 68 65 20 63 65 6c   Size of the cel
14b05 6c 20 63 6f 6e 74 65 6e 74 20 6f 6e 20 74 68 65  l content on the
14b06 20 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61 67   main b-tree pag
14b07 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d  e */.};../*.** M
14b08 61 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20  aximum depth of 
14b09 61 6e 20 53 51 4c 69 74 65 20 42 2d 54 72 65 65  an SQLite B-Tree
14b0a 20 73 74 72 75 63 74 75 72 65 2e 20 41 6e 79 20   structure. Any 
14b0b 42 2d 54 72 65 65 20 64 65 65 70 65 72 20 74 68  B-Tree deeper th
14b0c 61 6e 0a 2a 2a 20 74 68 69 73 20 77 69 6c 6c 20  an.** this will 
14b0d 62 65 20 64 65 63 6c 61 72 65 64 20 63 6f 72 72  be declared corr
14b0e 75 70 74 2e 20 54 68 69 73 20 76 61 6c 75 65 20  upt. This value 
14b0f 69 73 20 63 61 6c 63 75 6c 61 74 65 64 20 62 61  is calculated ba
14b10 73 65 64 20 6f 6e 20 61 0a 2a 2a 20 6d 61 78 69  sed on a.** maxi
14b11 6d 75 6d 20 64 61 74 61 62 61 73 65 20 73 69 7a  mum database siz
14b12 65 20 6f 66 20 32 5e 33 31 20 70 61 67 65 73 20  e of 2^31 pages 
14b13 61 20 6d 69 6e 69 6d 75 6d 20 66 61 6e 6f 75 74  a minimum fanout
14b14 20 6f 66 20 32 20 66 6f 72 20 61 0a 2a 2a 20 72   of 2 for a.** r
14b15 6f 6f 74 2d 6e 6f 64 65 20 61 6e 64 20 33 20 66  oot-node and 3 f
14b16 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 69 6e 74  or all other int
14b17 65 72 6e 61 6c 20 6e 6f 64 65 73 2e 0a 2a 2a 0a  ernal nodes..**.
14b18 2a 2a 20 49 66 20 61 20 74 72 65 65 20 74 68 61  ** If a tree tha
14b19 74 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20  t appears to be 
14b1a 74 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 69 73  taller than this
14b1b 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c   is encountered,
14b1c 20 69 74 20 69 73 0a 2a 2a 20 61 73 73 75 6d 65   it is.** assume
14b1d 64 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62  d that the datab
14b1e 61 73 65 20 69 73 20 63 6f 72 72 75 70 74 2e 0a  ase is corrupt..
14b1f 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 43 55 52  */.#define BTCUR
14b20 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 20 32 30  SOR_MAX_DEPTH 20
14b21 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72 73 6f 72  ../*.** A cursor
14b22 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
14b23 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 65 6e   a particular en
14b24 74 72 79 20 77 69 74 68 69 6e 20 61 20 70 61 72  try within a par
14b25 74 69 63 75 6c 61 72 0a 2a 2a 20 62 2d 74 72 65  ticular.** b-tre
14b26 65 20 77 69 74 68 69 6e 20 61 20 64 61 74 61 62  e within a datab
14b27 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ase file..**.** 
14b28 54 68 65 20 65 6e 74 72 79 20 69 73 20 69 64 65  The entry is ide
14b29 6e 74 69 66 69 65 64 20 62 79 20 69 74 73 20 4d  ntified by its M
14b2a 65 6d 50 61 67 65 20 61 6e 64 20 74 68 65 20 69  emPage and the i
14b2b 6e 64 65 78 20 69 6e 0a 2a 2a 20 4d 65 6d 50 61  ndex in.** MemPa
14b2c 67 65 2e 61 43 65 6c 6c 5b 5d 20 6f 66 20 74 68  ge.aCell[] of th
14b2d 65 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 57  e entry..**.** W
14b2e 68 65 6e 20 61 20 73 69 6e 67 6c 65 20 64 61 74  hen a single dat
14b2f 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e 20 73  abase file can s
14b30 68 61 72 65 64 20 62 79 20 74 77 6f 20 6d 6f 72  hared by two mor
14b31 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
14b32 63 74 69 6f 6e 73 2c 0a 2a 2a 20 62 75 74 20 63  ctions,.** but c
14b33 75 72 73 6f 72 73 20 63 61 6e 6e 6f 74 20 62 65  ursors cannot be
14b34 20 73 68 61 72 65 64 2e 20 20 45 61 63 68 20 63   shared.  Each c
14b35 75 72 73 6f 72 20 69 73 20 61 73 73 6f 63 69 61  ursor is associa
14b36 74 65 64 20 77 69 74 68 20 61 0a 2a 2a 20 70 61  ted with a.** pa
14b37 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73  rticular databas
14b38 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 64 65  e connection ide
14b39 6e 74 69 66 69 65 64 20 42 74 43 75 72 73 6f 72  ntified BtCursor
14b3a 2e 70 42 74 72 65 65 2e 64 62 2e 0a 2a 2a 0a 2a  .pBtree.db..**.*
14b3b 2a 20 46 69 65 6c 64 73 20 69 6e 20 74 68 69 73  * Fields in this
14b3c 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 61   structure are a
14b3d 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 68  ccessed under th
14b3e 65 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65 78  e BtShared.mutex
14b3f 0a 2a 2a 20 66 6f 75 6e 64 20 61 74 20 73 65 6c  .** found at sel
14b40 66 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 2e 20 0a  f->pBt->mutex. .
14b41 2a 2f 0a 73 74 72 75 63 74 20 42 74 43 75 72 73  */.struct BtCurs
14b42 6f 72 20 7b 0a 20 20 42 74 72 65 65 20 2a 70 42  or {.  Btree *pB
14b43 74 72 65 65 3b 20 20 20 20 20 20 20 20 20 20 20  tree;           
14b44 20 2f 2a 20 54 68 65 20 42 74 72 65 65 20 74 6f   /* The Btree to
14b45 20 77 68 69 63 68 20 74 68 69 73 20 63 75 72 73   which this curs
14b46 6f 72 20 62 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20  or belongs */.  
14b47 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20  BtShared *pBt;  
14b48 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
14b49 20 42 74 53 68 61 72 65 64 20 74 68 69 73 20 63   BtShared this c
14b4a 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20  ursor points to 
14b4b 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  */.  BtCursor *p
14b4c 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b 20 20 2f  Next, *pPrev;  /
14b4d 2a 20 46 6f 72 6d 73 20 61 20 6c 69 6e 6b 65 64  * Forms a linked
14b4e 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 75 72   list of all cur
14b4f 73 6f 72 73 20 2a 2f 0a 20 20 73 74 72 75 63 74  sors */.  struct
14b50 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
14b51 66 6f 3b 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20  fo; /* Argument 
14b52 70 61 73 73 65 64 20 74 6f 20 63 6f 6d 70 61 72  passed to compar
14b53 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  ison function */
14b54 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74  .  Pgno pgnoRoot
14b55 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
14b56 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66  The root page of
14b57 20 74 68 69 73 20 74 72 65 65 20 2a 2f 0a 20 20   this tree */.  
14b58 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 63 61  sqlite3_int64 ca
14b59 63 68 65 64 52 6f 77 69 64 3b 20 2f 2a 20 4e 65  chedRowid; /* Ne
14b5a 78 74 20 72 6f 77 69 64 20 63 61 63 68 65 2e 20  xt rowid cache. 
14b5b 20 30 20 6d 65 61 6e 73 20 6e 6f 74 20 76 61 6c   0 means not val
14b5c 69 64 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f  id */.  CellInfo
14b5d 20 69 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20   info;          
14b5e 20 20 2f 2a 20 41 20 70 61 72 73 65 20 6f 66 20    /* A parse of 
14b5f 74 68 65 20 63 65 6c 6c 20 77 65 20 61 72 65 20  the cell we are 
14b60 70 6f 69 6e 74 69 6e 67 20 61 74 20 2a 2f 0a 20  pointing at */. 
14b61 20 75 38 20 77 72 46 6c 61 67 3b 20 20 20 20 20   u8 wrFlag;     
14b62 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
14b63 75 65 20 69 66 20 77 72 69 74 61 62 6c 65 20 2a  ue if writable *
14b64 2f 0a 20 20 75 38 20 61 74 4c 61 73 74 3b 20 20  /.  u8 atLast;  
14b65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
14b66 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67   Cursor pointing
14b67 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74   to the last ent
14b68 72 79 20 2a 2f 0a 20 20 75 38 20 76 61 6c 69 64  ry */.  u8 valid
14b69 4e 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20  NKey;           
14b6a 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 66    /* True if inf
14b6b 6f 2e 6e 4b 65 79 20 69 73 20 76 61 6c 69 64 20  o.nKey is valid 
14b6c 2a 2f 0a 20 20 75 38 20 65 53 74 61 74 65 3b 20  */.  u8 eState; 
14b6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
14b6e 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 43 55 52  * One of the CUR
14b6f 53 4f 52 5f 58 58 58 20 63 6f 6e 73 74 61 6e 74  SOR_XXX constant
14b70 73 20 28 73 65 65 20 62 65 6c 6f 77 29 20 2a 2f  s (see below) */
14b71 0a 20 20 76 6f 69 64 20 2a 70 4b 65 79 3b 20 20  .  void *pKey;  
14b72 20 20 20 20 2f 2a 20 53 61 76 65 64 20 6b 65 79      /* Saved key
14b73 20 74 68 61 74 20 77 61 73 20 63 75 72 73 6f 72   that was cursor
14b74 27 73 20 6c 61 73 74 20 6b 6e 6f 77 6e 20 70 6f  's last known po
14b75 73 69 74 69 6f 6e 20 2a 2f 0a 20 20 69 36 34 20  sition */.  i64 
14b76 6e 4b 65 79 3b 20 20 20 20 20 20 20 20 2f 2a 20  nKey;        /* 
14b77 53 69 7a 65 20 6f 66 20 70 4b 65 79 2c 20 6f 72  Size of pKey, or
14b78 20 6c 61 73 74 20 69 6e 74 65 67 65 72 20 6b 65   last integer ke
14b79 79 20 2a 2f 0a 20 20 69 6e 74 20 73 6b 69 70 4e  y */.  int skipN
14b7a 65 78 74 3b 20 20 20 20 2f 2a 20 50 72 65 76 28  ext;    /* Prev(
14b7b 29 20 69 73 20 6e 6f 6f 70 20 69 66 20 6e 65 67  ) is noop if neg
14b7c 61 74 69 76 65 2e 20 4e 65 78 74 28 29 20 69 73  ative. Next() is
14b7d 20 6e 6f 6f 70 20 69 66 20 70 6f 73 69 74 69 76   noop if positiv
14b7e 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  e */.#ifndef SQL
14b7f 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
14b80 42 0a 20 20 75 38 20 69 73 49 6e 63 72 62 6c 6f  B.  u8 isIncrblo
14b81 62 48 61 6e 64 6c 65 3b 20 20 20 20 20 20 2f 2a  bHandle;      /*
14b82 20 54 72 75 65 20 69 66 20 74 68 69 73 20 63 75   True if this cu
14b83 72 73 6f 72 20 69 73 20 61 6e 20 69 6e 63 72 2e  rsor is an incr.
14b84 20 69 6f 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20   io handle */.  
14b85 50 67 6e 6f 20 2a 61 4f 76 65 72 66 6c 6f 77 3b  Pgno *aOverflow;
14b86 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63            /* Cac
14b87 68 65 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70  he of overflow p
14b88 61 67 65 20 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f  age locations */
14b89 0a 23 65 6e 64 69 66 0a 20 20 69 31 36 20 69 50  .#endif.  i16 iP
14b8a 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
14b8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14b8c 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 72  /* Index of curr
14b8d 65 6e 74 20 70 61 67 65 20 69 6e 20 61 70 50 61  ent page in apPa
14b8e 67 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  ge */.  MemPage 
14b8f 2a 61 70 50 61 67 65 5b 42 54 43 55 52 53 4f 52  *apPage[BTCURSOR
14b90 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20 20 2f 2a  _MAX_DEPTH];  /*
14b91 20 50 61 67 65 73 20 66 72 6f 6d 20 72 6f 6f 74   Pages from root
14b92 20 74 6f 20 63 75 72 72 65 6e 74 20 70 61 67 65   to current page
14b93 20 2a 2f 0a 20 20 75 31 36 20 61 69 49 64 78 5b   */.  u16 aiIdx[
14b94 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50  BTCURSOR_MAX_DEP
14b95 54 48 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 43  TH];        /* C
14b96 75 72 72 65 6e 74 20 69 6e 64 65 78 20 69 6e 20  urrent index in 
14b97 61 70 50 61 67 65 5b 69 5d 20 2a 2f 0a 7d 3b 0a  apPage[i] */.};.
14b98 0a 2f 2a 0a 2a 2a 20 50 6f 74 65 6e 74 69 61 6c  ./*.** Potential
14b99 20 76 61 6c 75 65 73 20 66 6f 72 20 42 74 43 75   values for BtCu
14b9a 72 73 6f 72 2e 65 53 74 61 74 65 2e 0a 2a 2a 0a  rsor.eState..**.
14b9b 2a 2a 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a  ** CURSOR_VALID:
14b9c 0a 2a 2a 20 20 20 43 75 72 73 6f 72 20 70 6f 69  .**   Cursor poi
14b9d 6e 74 73 20 74 6f 20 61 20 76 61 6c 69 64 20 65  nts to a valid e
14b9e 6e 74 72 79 2e 20 67 65 74 50 61 79 6c 6f 61 64  ntry. getPayload
14b9f 28 29 20 65 74 63 2e 20 6d 61 79 20 62 65 20 63  () etc. may be c
14ba0 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52  alled..**.** CUR
14ba1 53 4f 52 5f 49 4e 56 41 4c 49 44 3a 0a 2a 2a 20  SOR_INVALID:.** 
14ba2 20 20 43 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f    Cursor does no
14ba3 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c  t point to a val
14ba4 69 64 20 65 6e 74 72 79 2e 20 54 68 69 73 20 63  id entry. This c
14ba5 61 6e 20 68 61 70 70 65 6e 20 28 66 6f 72 20 65  an happen (for e
14ba6 78 61 6d 70 6c 65 29 20 0a 2a 2a 20 20 20 62 65  xample) .**   be
14ba7 63 61 75 73 65 20 74 68 65 20 74 61 62 6c 65 20  cause the table 
14ba8 69 73 20 65 6d 70 74 79 20 6f 72 20 62 65 63 61  is empty or beca
14ba9 75 73 65 20 42 74 72 65 65 43 75 72 73 6f 72 46  use BtreeCursorF
14baa 69 72 73 74 28 29 20 68 61 73 20 6e 6f 74 20 62  irst() has not b
14bab 65 65 6e 0a 2a 2a 20 20 20 63 61 6c 6c 65 64 2e  een.**   called.
14bac 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 52 45  .**.** CURSOR_RE
14bad 51 55 49 52 45 53 45 45 4b 3a 0a 2a 2a 20 20 20  QUIRESEEK:.**   
14bae 54 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 74  The table that t
14baf 68 69 73 20 63 75 72 73 6f 72 20 77 61 73 20 6f  his cursor was o
14bb0 70 65 6e 65 64 20 6f 6e 20 73 74 69 6c 6c 20 65  pened on still e
14bb1 78 69 73 74 73 2c 20 62 75 74 20 68 61 73 20 62  xists, but has b
14bb2 65 65 6e 20 0a 2a 2a 20 20 20 6d 6f 64 69 66 69  een .**   modifi
14bb3 65 64 20 73 69 6e 63 65 20 74 68 65 20 63 75 72  ed since the cur
14bb4 73 6f 72 20 77 61 73 20 6c 61 73 74 20 75 73 65  sor was last use
14bb5 64 2e 20 54 68 65 20 63 75 72 73 6f 72 20 70 6f  d. The cursor po
14bb6 73 69 74 69 6f 6e 20 69 73 20 73 61 76 65 64 0a  sition is saved.
14bb7 2a 2a 20 20 20 69 6e 20 76 61 72 69 61 62 6c 65  **   in variable
14bb8 73 20 42 74 43 75 72 73 6f 72 2e 70 4b 65 79 20  s BtCursor.pKey 
14bb9 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 6e 4b 65  and BtCursor.nKe
14bba 79 2e 20 57 68 65 6e 20 61 20 63 75 72 73 6f 72  y. When a cursor
14bbb 20 69 73 20 69 6e 20 0a 2a 2a 20 20 20 74 68 69   is in .**   thi
14bbc 73 20 73 74 61 74 65 2c 20 72 65 73 74 6f 72 65  s state, restore
14bbd 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29  CursorPosition()
14bbe 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 74   can be called t
14bbf 6f 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20  o attempt to.** 
14bc0 20 20 73 65 65 6b 20 74 68 65 20 63 75 72 73 6f    seek the curso
14bc1 72 20 74 6f 20 74 68 65 20 73 61 76 65 64 20 70  r to the saved p
14bc2 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43  osition..**.** C
14bc3 55 52 53 4f 52 5f 46 41 55 4c 54 3a 0a 2a 2a 20  URSOR_FAULT:.** 
14bc4 20 20 41 20 75 6e 72 65 63 6f 76 65 72 61 62 6c    A unrecoverabl
14bc5 65 20 65 72 72 6f 72 20 28 61 6e 20 49 2f 4f 20  e error (an I/O 
14bc6 65 72 72 6f 72 20 6f 72 20 61 20 6d 61 6c 6c 6f  error or a mallo
14bc7 63 20 66 61 69 6c 75 72 65 29 20 68 61 73 20 6f  c failure) has o
14bc8 63 63 75 72 72 65 64 0a 2a 2a 20 20 20 6f 6e 20  ccurred.**   on 
14bc9 61 20 64 69 66 66 65 72 65 6e 74 20 63 6f 6e 6e  a different conn
14bca 65 63 74 69 6f 6e 20 74 68 61 74 20 73 68 61 72  ection that shar
14bcb 65 73 20 74 68 65 20 42 74 53 68 61 72 65 64 20  es the BtShared 
14bcc 63 61 63 68 65 20 77 69 74 68 20 74 68 69 73 0a  cache with this.
14bcd 2a 2a 20 20 20 63 75 72 73 6f 72 2e 20 20 54 68  **   cursor.  Th
14bce 65 20 65 72 72 6f 72 20 68 61 73 20 6c 65 66 74  e error has left
14bcf 20 74 68 65 20 63 61 63 68 65 20 69 6e 20 61 6e   the cache in an
14bd0 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74   inconsistent st
14bd1 61 74 65 2e 0a 2a 2a 20 20 20 44 6f 20 6e 6f 74  ate..**   Do not
14bd2 68 69 6e 67 20 65 6c 73 65 20 77 69 74 68 20 74  hing else with t
14bd3 68 69 73 20 63 75 72 73 6f 72 2e 20 20 41 6e 79  his cursor.  Any
14bd4 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20   attempt to use 
14bd5 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 20 20  the cursor.**   
14bd6 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68  should return th
14bd7 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 74 6f  e error code sto
14bd8 72 65 64 20 69 6e 20 42 74 43 75 72 73 6f 72 2e  red in BtCursor.
14bd9 73 6b 69 70 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  skip.*/.#define 
14bda 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 20  CURSOR_INVALID  
14bdb 20 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69           0.#defi
14bdc 6e 65 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  ne CURSOR_VALID 
14bdd 20 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64              1.#d
14bde 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 52 45 51  efine CURSOR_REQ
14bdf 55 49 52 45 53 45 45 4b 20 20 20 20 20 20 20 32  UIRESEEK       2
14be0 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f  .#define CURSOR_
14be1 46 41 55 4c 54 20 20 20 20 20 20 20 20 20 20 20  FAULT           
14be2 20 20 33 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20    3../* .** The 
14be3 64 61 74 61 62 61 73 65 20 70 61 67 65 20 74 68  database page th
14be4 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 6f  e PENDING_BYTE o
14be5 63 63 75 70 69 65 73 2e 20 54 68 69 73 20 70 61  ccupies. This pa
14be6 67 65 20 69 73 20 6e 65 76 65 72 20 75 73 65 64  ge is never used
14be7 2e 0a 2a 2f 0a 23 20 64 65 66 69 6e 65 20 50 45  ..*/.# define PE
14be8 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
14be9 70 42 74 29 20 50 41 47 45 52 5f 4d 4a 5f 50 47  pBt) PAGER_MJ_PG
14bea 4e 4f 28 70 42 74 29 0a 0a 2f 2a 0a 2a 2a 20 54  NO(pBt)../*.** T
14beb 68 65 73 65 20 6d 61 63 72 6f 73 20 64 65 66 69  hese macros defi
14bec 6e 65 20 74 68 65 20 6c 6f 63 61 74 69 6f 6e 20  ne the location 
14bed 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  of the pointer-m
14bee 61 70 20 65 6e 74 72 79 20 66 6f 72 20 61 20 0a  ap entry for a .
14bef 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 67 65  ** database page
14bf0 2e 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75  . The first argu
14bf1 6d 65 6e 74 20 74 6f 20 65 61 63 68 20 69 73 20  ment to each is 
14bf2 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 73  the number of us
14bf3 61 62 6c 65 0a 2a 2a 20 62 79 74 65 73 20 6f 6e  able.** bytes on
14bf4 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74 68   each page of th
14bf5 65 20 64 61 74 61 62 61 73 65 20 28 6f 66 74 65  e database (ofte
14bf6 6e 20 31 30 32 34 29 2e 20 54 68 65 20 73 65 63  n 1024). The sec
14bf7 6f 6e 64 20 69 73 20 74 68 65 0a 2a 2a 20 70 61  ond is the.** pa
14bf8 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 6c 6f 6f  ge number to loo
14bf9 6b 20 75 70 20 69 6e 20 74 68 65 20 70 6f 69 6e  k up in the poin
14bfa 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 50  ter map..**.** P
14bfb 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 72 65 74  TRMAP_PAGENO ret
14bfc 75 72 6e 73 20 74 68 65 20 64 61 74 61 62 61 73  urns the databas
14bfd 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  e page number of
14bfe 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70   the pointer-map
14bff 0a 2a 2a 20 70 61 67 65 20 74 68 61 74 20 73 74  .** page that st
14c00 6f 72 65 73 20 74 68 65 20 72 65 71 75 69 72 65  ores the require
14c01 64 20 70 6f 69 6e 74 65 72 2e 20 50 54 52 4d 41  d pointer. PTRMA
14c02 50 5f 50 54 52 4f 46 46 53 45 54 20 72 65 74 75  P_PTROFFSET retu
14c03 72 6e 73 0a 2a 2a 20 74 68 65 20 6f 66 66 73 65  rns.** the offse
14c04 74 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74  t of the request
14c05 65 64 20 6d 61 70 20 65 6e 74 72 79 2e 0a 2a 2a  ed map entry..**
14c06 0a 2a 2a 20 49 66 20 74 68 65 20 70 67 6e 6f 20  .** If the pgno 
14c07 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20  argument passed 
14c08 74 6f 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f  to PTRMAP_PAGENO
14c09 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61   is a pointer-ma
14c0a 70 20 70 61 67 65 2c 0a 2a 2a 20 74 68 65 6e 20  p page,.** then 
14c0b 70 67 6e 6f 20 69 73 20 72 65 74 75 72 6e 65 64  pgno is returned
14c0c 2e 20 53 6f 20 28 70 67 6e 6f 3d 3d 50 54 52 4d  . So (pgno==PTRM
14c0d 41 50 5f 50 41 47 45 4e 4f 28 70 67 73 7a 2c 20  AP_PAGENO(pgsz, 
14c0e 70 67 6e 6f 29 29 20 63 61 6e 20 62 65 0a 2a 2a  pgno)) can be.**
14c0f 20 75 73 65 64 20 74 6f 20 74 65 73 74 20 69 66   used to test if
14c10 20 70 67 6e 6f 20 69 73 20 61 20 70 6f 69 6e 74   pgno is a point
14c11 65 72 2d 6d 61 70 20 70 61 67 65 2e 20 50 54 52  er-map page. PTR
14c12 4d 41 50 5f 49 53 50 41 47 45 20 69 6d 70 6c 65  MAP_ISPAGE imple
14c13 6d 65 6e 74 73 0a 2a 2a 20 74 68 69 73 20 74 65  ments.** this te
14c14 73 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50  st..*/.#define P
14c15 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74  TRMAP_PAGENO(pBt
14c16 2c 20 70 67 6e 6f 29 20 70 74 72 6d 61 70 50 61  , pgno) ptrmapPa
14c17 67 65 6e 6f 28 70 42 74 2c 20 70 67 6e 6f 29 0a  geno(pBt, pgno).
14c18 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 50  #define PTRMAP_P
14c19 54 52 4f 46 46 53 45 54 28 70 67 70 74 72 6d 61  TROFFSET(pgptrma
14c1a 70 2c 20 70 67 6e 6f 29 20 28 35 2a 28 70 67 6e  p, pgno) (5*(pgn
14c1b 6f 2d 70 67 70 74 72 6d 61 70 2d 31 29 29 0a 23  o-pgptrmap-1)).#
14c1c 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 49 53  define PTRMAP_IS
14c1d 50 41 47 45 28 70 42 74 2c 20 70 67 6e 6f 29 20  PAGE(pBt, pgno) 
14c1e 28 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 28  (PTRMAP_PAGENO((
14c1f 70 42 74 29 2c 28 70 67 6e 6f 29 29 3d 3d 28 70  pBt),(pgno))==(p
14c20 67 6e 6f 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  gno))../*.** The
14c21 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 73 20   pointer map is 
14c22 61 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65 20 74  a lookup table t
14c23 68 61 74 20 69 64 65 6e 74 69 66 69 65 73 20 74  hat identifies t
14c24 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 66  he parent page f
14c25 6f 72 0a 2a 2a 20 65 61 63 68 20 63 68 69 6c 64  or.** each child
14c26 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74   page in the dat
14c27 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65  abase file.  The
14c28 20 70 61 72 65 6e 74 20 70 61 67 65 20 69 73 20   parent page is 
14c29 74 68 65 20 70 61 67 65 20 74 68 61 74 0a 2a 2a  the page that.**
14c2a 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e   contains a poin
14c2b 74 65 72 20 74 6f 20 74 68 65 20 63 68 69 6c 64  ter to the child
14c2c 2e 20 20 45 76 65 72 79 20 70 61 67 65 20 69 6e  .  Every page in
14c2d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
14c2e 6e 74 61 69 6e 73 0a 2a 2a 20 30 20 6f 72 20 31  ntains.** 0 or 1
14c2f 20 70 61 72 65 6e 74 20 70 61 67 65 73 2e 20 20   parent pages.  
14c30 28 49 6e 20 74 68 69 73 20 63 6f 6e 74 65 78 74  (In this context
14c31 20 27 64 61 74 61 62 61 73 65 20 70 61 67 65 27   'database page'
14c32 20 72 65 66 65 72 73 0a 2a 2a 20 74 6f 20 61 6e   refers.** to an
14c33 79 20 70 61 67 65 20 74 68 61 74 20 69 73 20 6e  y page that is n
14c34 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 70  ot part of the p
14c35 6f 69 6e 74 65 72 20 6d 61 70 20 69 74 73 65 6c  ointer map itsel
14c36 66 2e 29 20 20 45 61 63 68 20 70 6f 69 6e 74 65  f.)  Each pointe
14c37 72 20 6d 61 70 0a 2a 2a 20 65 6e 74 72 79 20 63  r map.** entry c
14c38 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73 69 6e  onsists of a sin
14c39 67 6c 65 20 62 79 74 65 20 27 74 79 70 65 27 20  gle byte 'type' 
14c3a 61 6e 64 20 61 20 34 20 62 79 74 65 20 70 61 72  and a 4 byte par
14c3b 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 2e  ent page number.
14c3c 0a 2a 2a 20 54 68 65 20 50 54 52 4d 41 50 5f 58  .** The PTRMAP_X
14c3d 58 58 20 69 64 65 6e 74 69 66 69 65 72 73 20 62  XX identifiers b
14c3e 65 6c 6f 77 20 61 72 65 20 74 68 65 20 76 61 6c  elow are the val
14c3f 69 64 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20  id types..**.** 
14c40 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74  The purpose of t
14c41 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69  he pointer map i
14c42 73 20 74 6f 20 66 61 63 69 6c 69 74 79 20 6d 6f  s to facility mo
14c43 76 69 6e 67 20 70 61 67 65 73 20 66 72 6f 6d 20  ving pages from 
14c44 6f 6e 65 0a 2a 2a 20 70 6f 73 69 74 69 6f 6e 20  one.** position 
14c45 69 6e 20 74 68 65 20 66 69 6c 65 20 74 6f 20 61  in the file to a
14c46 6e 6f 74 68 65 72 20 61 73 20 70 61 72 74 20 6f  nother as part o
14c47 66 20 61 75 74 6f 76 61 63 75 75 6d 2e 20 20 57  f autovacuum.  W
14c48 68 65 6e 20 61 20 70 61 67 65 0a 2a 2a 20 69 73  hen a page.** is
14c49 20 6d 6f 76 65 64 2c 20 74 68 65 20 70 6f 69 6e   moved, the poin
14c4a 74 65 72 20 69 6e 20 69 74 73 20 70 61 72 65 6e  ter in its paren
14c4b 74 20 6d 75 73 74 20 62 65 20 75 70 64 61 74 65  t must be update
14c4c 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  d to point to th
14c4d 65 0a 2a 2a 20 6e 65 77 20 6c 6f 63 61 74 69 6f  e.** new locatio
14c4e 6e 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72 20  n.  The pointer 
14c4f 6d 61 70 20 69 73 20 75 73 65 64 20 74 6f 20 6c  map is used to l
14c50 6f 63 61 74 65 20 74 68 65 20 70 61 72 65 6e 74  ocate the parent
14c51 20 70 61 67 65 20 71 75 69 63 6b 6c 79 2e 0a 2a   page quickly..*
14c52 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 52 4f 4f 54  *.** PTRMAP_ROOT
14c53 50 41 47 45 3a 20 54 68 65 20 64 61 74 61 62 61  PAGE: The databa
14c54 73 65 20 70 61 67 65 20 69 73 20 61 20 72 6f 6f  se page is a roo
14c55 74 2d 70 61 67 65 2e 20 54 68 65 20 70 61 67 65  t-page. The page
14c56 2d 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 0a 2a  -number is not.*
14c57 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
14c58 20 20 20 75 73 65 64 20 69 6e 20 74 68 69 73 20     used in this 
14c59 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d  case..**.** PTRM
14c5a 41 50 5f 46 52 45 45 50 41 47 45 3a 20 54 68 65  AP_FREEPAGE: The
14c5b 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69   database page i
14c5c 73 20 61 6e 20 75 6e 75 73 65 64 20 28 66 72 65  s an unused (fre
14c5d 65 29 20 70 61 67 65 2e 20 54 68 65 20 70 61 67  e) page. The pag
14c5e 65 2d 6e 75 6d 62 65 72 20 0a 2a 2a 20 20 20 20  e-number .**    
14c5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73                is
14c60 20 6e 6f 74 20 75 73 65 64 20 69 6e 20 74 68 69   not used in thi
14c61 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54  s case..**.** PT
14c62 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 3a 20  RMAP_OVERFLOW1: 
14c63 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67  The database pag
14c64 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20 70  e is the first p
14c65 61 67 65 20 69 6e 20 61 20 6c 69 73 74 20 6f 66  age in a list of
14c66 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20   .**            
14c67 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20         overflow 
14c68 70 61 67 65 73 2e 20 54 68 65 20 70 61 67 65 20  pages. The page 
14c69 6e 75 6d 62 65 72 20 69 64 65 6e 74 69 66 69 65  number identifie
14c6a 73 20 74 68 65 20 70 61 67 65 20 74 68 61 74 0a  s the page that.
14c6b 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
14c6c 20 20 20 20 20 63 6f 6e 74 61 69 6e 73 20 74 68       contains th
14c6d 65 20 63 65 6c 6c 20 77 69 74 68 20 61 20 70 6f  e cell with a po
14c6e 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 6f 76  inter to this ov
14c6f 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a 2a 0a  erflow page..**.
14c70 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  ** PTRMAP_OVERFL
14c71 4f 57 32 3a 20 54 68 65 20 64 61 74 61 62 61 73  OW2: The databas
14c72 65 20 70 61 67 65 20 69 73 20 74 68 65 20 73 65  e page is the se
14c73 63 6f 6e 64 20 6f 72 20 6c 61 74 65 72 20 70 61  cond or later pa
14c74 67 65 20 69 6e 20 61 20 6c 69 73 74 20 6f 66 0a  ge in a list of.
14c75 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
14c76 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61       overflow pa
14c77 67 65 73 2e 20 54 68 65 20 70 61 67 65 2d 6e 75  ges. The page-nu
14c78 6d 62 65 72 20 69 64 65 6e 74 69 66 69 65 73 20  mber identifies 
14c79 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20  the previous.** 
14c7a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14c7b 20 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76    page in the ov
14c7c 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74  erflow page list
14c7d 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 42  ..**.** PTRMAP_B
14c7e 54 52 45 45 3a 20 54 68 65 20 64 61 74 61 62 61  TREE: The databa
14c7f 73 65 20 70 61 67 65 20 69 73 20 61 20 6e 6f 6e  se page is a non
14c80 2d 72 6f 6f 74 20 62 74 72 65 65 20 70 61 67 65  -root btree page
14c81 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65  . The page numbe
14c82 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  r.**            
14c83 20 20 20 69 64 65 6e 74 69 66 69 65 73 20 74 68     identifies th
14c84 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 69 6e  e parent page in
14c85 20 74 68 65 20 62 74 72 65 65 2e 0a 2a 2f 0a 23   the btree..*/.#
14c86 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 52 4f  define PTRMAP_RO
14c87 4f 54 50 41 47 45 20 31 0a 23 64 65 66 69 6e 65  OTPAGE 1.#define
14c88 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45   PTRMAP_FREEPAGE
14c89 20 32 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41   2.#define PTRMA
14c8a 50 5f 4f 56 45 52 46 4c 4f 57 31 20 33 0a 23 64  P_OVERFLOW1 3.#d
14c8b 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f 56 45  efine PTRMAP_OVE
14c8c 52 46 4c 4f 57 32 20 34 0a 23 64 65 66 69 6e 65  RFLOW2 4.#define
14c8d 20 50 54 52 4d 41 50 5f 42 54 52 45 45 20 35 0a   PTRMAP_BTREE 5.
14c8e 0a 2f 2a 20 41 20 62 75 6e 63 68 20 6f 66 20 61  ./* A bunch of a
14c8f 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e  ssert() statemen
14c90 74 73 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20  ts to check the 
14c91 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74  transaction stat
14c92 65 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 6f  e variables.** o
14c93 66 20 68 61 6e 64 6c 65 20 70 20 28 74 79 70 65  f handle p (type
14c94 20 42 74 72 65 65 2a 29 20 61 72 65 20 69 6e 74   Btree*) are int
14c95 65 72 6e 61 6c 6c 79 20 63 6f 6e 73 69 73 74 65  ernally consiste
14c96 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 62  nt..*/.#define b
14c97 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29  treeIntegrity(p)
14c98 20 5c 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e   \.  assert( p->
14c99 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
14c9a 6f 6e 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 7c  on!=TRANS_NONE |
14c9b 7c 20 70 2d 3e 70 42 74 2d 3e 6e 54 72 61 6e 73  | p->pBt->nTrans
14c9c 61 63 74 69 6f 6e 3d 3d 30 20 29 3b 20 5c 0a 20  action==0 ); \. 
14c9d 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d   assert( p->pBt-
14c9e 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 3d  >inTransaction>=
14c9f 70 2d 3e 69 6e 54 72 61 6e 73 20 29 3b 20 0a 0a  p->inTrans ); ..
14ca0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 49 53 41 55 54  ./*.** The ISAUT
14ca1 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f 20 69 73  OVACUUM macro is
14ca2 20 75 73 65 64 20 77 69 74 68 69 6e 20 62 61 6c   used within bal
14ca3 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 74  ance_nonroot() t
14ca4 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 69  o determine.** i
14ca5 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  f the database s
14ca6 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63  upports auto-vac
14ca7 75 75 6d 20 6f 72 20 6e 6f 74 2e 20 42 65 63 61  uum or not. Beca
14ca8 75 73 65 20 69 74 20 69 73 20 75 73 65 64 0a 2a  use it is used.*
14ca9 2a 20 77 69 74 68 69 6e 20 61 6e 20 65 78 70 72  * within an expr
14caa 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 61  ession that is a
14cab 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 6e  n argument to an
14cac 6f 74 68 65 72 20 6d 61 63 72 6f 20 0a 2a 2a 20  other macro .** 
14cad 28 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 52 61 77  (sqliteMallocRaw
14cae 29 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73  ), it is not pos
14caf 73 69 62 6c 65 20 74 6f 20 75 73 65 20 63 6f 6e  sible to use con
14cb0 64 69 74 69 6f 6e 61 6c 20 63 6f 6d 70 69 6c 61  ditional compila
14cb1 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 2c 20 74 68 69  tion..** So, thi
14cb2 73 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e  s macro is defin
14cb3 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2f 0a 23  ed instead..*/.#
14cb4 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
14cb5 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 23 64  IT_AUTOVACUUM.#d
14cb6 65 66 69 6e 65 20 49 53 41 55 54 4f 56 41 43 55  efine ISAUTOVACU
14cb7 55 4d 20 28 70 42 74 2d 3e 61 75 74 6f 56 61 63  UM (pBt->autoVac
14cb8 75 75 6d 29 0a 23 65 6c 73 65 0a 23 64 65 66 69  uum).#else.#defi
14cb9 6e 65 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20  ne ISAUTOVACUUM 
14cba 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  0.#endif.../*.**
14cbb 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
14cbc 69 73 20 70 61 73 73 65 64 20 61 72 6f 75 6e 64  is passed around
14cbd 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 74 68 65   through all the
14cbe 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67   sanity checking
14cbf 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 6e 20   routines.** in 
14cc0 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74 72  order to keep tr
14cc1 61 63 6b 20 6f 66 20 73 6f 6d 65 20 67 6c 6f 62  ack of some glob
14cc2 61 6c 20 73 74 61 74 65 20 69 6e 66 6f 72 6d 61  al state informa
14cc3 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  tion..*/.typedef
14cc4 20 73 74 72 75 63 74 20 49 6e 74 65 67 72 69 74   struct Integrit
14cc5 79 43 6b 20 49 6e 74 65 67 72 69 74 79 43 6b 3b  yCk IntegrityCk;
14cc6 0a 73 74 72 75 63 74 20 49 6e 74 65 67 72 69 74  .struct Integrit
14cc7 79 43 6b 20 7b 0a 20 20 42 74 53 68 61 72 65 64  yCk {.  BtShared
14cc8 20 2a 70 42 74 3b 20 20 20 20 2f 2a 20 54 68 65   *pBt;    /* The
14cc9 20 74 72 65 65 20 62 65 69 6e 67 20 63 68 65 63   tree being chec
14cca 6b 65 64 20 6f 75 74 20 2a 2f 0a 20 20 50 61 67  ked out */.  Pag
14ccb 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 2f  er *pPager;    /
14ccc 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 64  * The associated
14ccd 20 70 61 67 65 72 2e 20 20 41 6c 73 6f 20 61 63   pager.  Also ac
14cce 63 65 73 73 69 62 6c 65 20 62 79 20 70 42 74 2d  cessible by pBt-
14ccf 3e 70 50 61 67 65 72 20 2a 2f 0a 20 20 50 67 6e  >pPager */.  Pgn
14cd0 6f 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 2f  o nPage;       /
14cd1 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  * Number of page
14cd2 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
14cd3 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 6e 52 65  e */.  int *anRe
14cd4 66 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  f;       /* Numb
14cd5 65 72 20 6f 66 20 74 69 6d 65 73 20 65 61 63 68  er of times each
14cd6 20 70 61 67 65 20 69 73 20 72 65 66 65 72 65 6e   page is referen
14cd7 63 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 45  ced */.  int mxE
14cd8 72 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 74  rr;        /* St
14cd9 6f 70 20 61 63 63 75 6d 75 6c 61 74 69 6e 67 20  op accumulating 
14cda 65 72 72 6f 72 73 20 77 68 65 6e 20 74 68 69 73  errors when this
14cdb 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20 2a 2f   reaches zero */
14cdc 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20  .  int nErr;    
14cdd 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
14cde 66 20 6d 65 73 73 61 67 65 73 20 77 72 69 74 74  f messages writt
14cdf 65 6e 20 74 6f 20 7a 45 72 72 4d 73 67 20 73 6f  en to zErrMsg so
14ce0 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6d 61   far */.  int ma
14ce1 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 2f 2a 20 41  llocFailed; /* A
14ce2 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
14ce3 6f 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63  on error has occ
14ce4 75 72 72 65 64 20 2a 2f 0a 20 20 53 74 72 41 63  urred */.  StrAc
14ce5 63 75 6d 20 65 72 72 4d 73 67 3b 20 20 2f 2a 20  cum errMsg;  /* 
14ce6 41 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 65  Accumulate the e
14ce7 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78  rror message tex
14ce8 74 20 68 65 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  t here */.};../*
14ce9 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77 72 69 74  .** Read or writ
14cea 65 20 61 20 74 77 6f 2d 20 61 6e 64 20 66 6f 75  e a two- and fou
14ceb 72 2d 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61  r-byte big-endia
14cec 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73  n integer values
14ced 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74  ..*/.#define get
14cee 32 62 79 74 65 28 78 29 20 20 20 28 28 78 29 5b  2byte(x)   ((x)[
14cef 30 5d 3c 3c 38 20 7c 20 28 78 29 5b 31 5d 29 0a  0]<<8 | (x)[1]).
14cf0 23 64 65 66 69 6e 65 20 70 75 74 32 62 79 74 65  #define put2byte
14cf1 28 70 2c 76 29 20 28 28 70 29 5b 30 5d 20 3d 20  (p,v) ((p)[0] = 
14cf2 28 75 38 29 28 28 76 29 3e 3e 38 29 2c 20 28 70  (u8)((v)>>8), (p
14cf3 29 5b 31 5d 20 3d 20 28 75 38 29 28 76 29 29 0a  )[1] = (u8)(v)).
14cf4 23 64 65 66 69 6e 65 20 67 65 74 34 62 79 74 65  #define get4byte
14cf5 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65   sqlite3Get4byte
14cf6 0a 23 64 65 66 69 6e 65 20 70 75 74 34 62 79 74  .#define put4byt
14cf7 65 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74  e sqlite3Put4byt
14cf8 65 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e../************
14cf9 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 49  ** End of btreeI
14cfa 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
14cfb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14cfc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14cfd 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
14cfe 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
14cff 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
14d00 69 6e 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a  in btmutex.c ***
14d01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14d02 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
14d03 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
14d04 43 48 45 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  CHE.#if SQLITE_T
14d05 48 52 45 41 44 53 41 46 45 0a 0a 2f 2a 0a 2a 2a  HREADSAFE../*.**
14d06 20 4f 62 74 61 69 6e 20 74 68 65 20 42 74 53 68   Obtain the BtSh
14d07 61 72 65 64 20 6d 75 74 65 78 20 61 73 73 6f 63  ared mutex assoc
14d08 69 61 74 65 64 20 77 69 74 68 20 42 2d 54 72 65  iated with B-Tre
14d09 65 20 68 61 6e 64 6c 65 20 70 2e 20 41 6c 73 6f  e handle p. Also
14d0a 2c 0a 2a 2a 20 73 65 74 20 42 74 53 68 61 72 65  ,.** set BtShare
14d0b 64 2e 64 62 20 74 6f 20 74 68 65 20 64 61 74 61  d.db to the data
14d0c 62 61 73 65 20 68 61 6e 64 6c 65 20 61 73 73 6f  base handle asso
14d0d 63 69 61 74 65 64 20 77 69 74 68 20 70 20 61 6e  ciated with p an
14d0e 64 20 74 68 65 0a 2a 2a 20 70 2d 3e 6c 6f 63 6b  d the.** p->lock
14d0f 65 64 20 62 6f 6f 6c 65 61 6e 20 74 6f 20 74 72  ed boolean to tr
14d10 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ue..*/.static vo
14d11 69 64 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65  id lockBtreeMute
14d12 78 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61  x(Btree *p){.  a
14d13 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64  ssert( p->locked
14d14 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
14d15 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e   sqlite3_mutex_n
14d16 6f 74 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d  otheld(p->pBt->m
14d17 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
14d18 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
14d19 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74  _held(p->db->mut
14d1a 65 78 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65  ex) );..  sqlite
14d1b 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d  3_mutex_enter(p-
14d1c 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  >pBt->mutex);.  
14d1d 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e  p->pBt->db = p->
14d1e 64 62 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64 20  db;.  p->locked 
14d1f 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  = 1;.}../*.** Re
14d20 6c 65 61 73 65 20 74 68 65 20 42 74 53 68 61 72  lease the BtShar
14d21 65 64 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61  ed mutex associa
14d22 74 65 64 20 77 69 74 68 20 42 2d 54 72 65 65 20  ted with B-Tree 
14d23 68 61 6e 64 6c 65 20 70 20 61 6e 64 0a 2a 2a 20  handle p and.** 
14d24 63 6c 65 61 72 20 74 68 65 20 70 2d 3e 6c 6f 63  clear the p->loc
14d25 6b 65 64 20 62 6f 6f 6c 65 61 6e 2e 0a 2a 2f 0a  ked boolean..*/.
14d26 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 6c 6f  static void unlo
14d27 63 6b 42 74 72 65 65 4d 75 74 65 78 28 42 74 72  ckBtreeMutex(Btr
14d28 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  ee *p){.  assert
14d29 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 31 20 29  ( p->locked==1 )
14d2a 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
14d2b 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
14d2c 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
14d2d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
14d2e 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
14d2f 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
14d30 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 3d 3d   assert( p->db==
14d31 70 2d 3e 70 42 74 2d 3e 64 62 20 29 3b 0a 0a 20  p->pBt->db );.. 
14d32 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
14d33 65 61 76 65 28 70 2d 3e 70 42 74 2d 3e 6d 75 74  eave(p->pBt->mut
14d34 65 78 29 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 65 64  ex);.  p->locked
14d35 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45   = 0;.}../*.** E
14d36 6e 74 65 72 20 61 20 6d 75 74 65 78 20 6f 6e 20  nter a mutex on 
14d37 74 68 65 20 67 69 76 65 6e 20 42 54 72 65 65 20  the given BTree 
14d38 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  object..**.** If
14d39 20 74 68 65 20 6f 62 6a 65 63 74 20 69 73 20 6e   the object is n
14d3a 6f 74 20 73 68 61 72 61 62 6c 65 2c 20 74 68 65  ot sharable, the
14d3b 6e 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 65 76  n no mutex is ev
14d3c 65 72 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 61  er required.** a
14d3d 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  nd this routine 
14d3e 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 65  is a no-op.  The
14d3f 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d 75 74 65   underlying mute
14d40 78 20 69 73 20 6e 6f 6e 2d 72 65 63 75 72 73 69  x is non-recursi
14d41 76 65 2e 0a 2a 2a 20 42 75 74 20 77 65 20 6b 65  ve..** But we ke
14d42 65 70 20 61 20 72 65 66 65 72 65 6e 63 65 20 63  ep a reference c
14d43 6f 75 6e 74 20 69 6e 20 42 74 72 65 65 2e 77 61  ount in Btree.wa
14d44 6e 74 54 6f 4c 6f 63 6b 20 73 6f 20 74 68 65 20  ntToLock so the 
14d45 62 65 68 61 76 69 6f 72 0a 2a 2a 20 6f 66 20 74  behavior.** of t
14d46 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73  his interface is
14d47 20 72 65 63 75 72 73 69 76 65 2e 0a 2a 2a 0a 2a   recursive..**.*
14d48 2a 20 54 6f 20 61 76 6f 69 64 20 64 65 61 64 6c  * To avoid deadl
14d49 6f 63 6b 73 2c 20 6d 75 6c 74 69 70 6c 65 20 42  ocks, multiple B
14d4a 74 72 65 65 73 20 61 72 65 20 6c 6f 63 6b 65 64  trees are locked
14d4b 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 64   in the same ord
14d4c 65 72 0a 2a 2a 20 62 79 20 61 6c 6c 20 64 61 74  er.** by all dat
14d4d 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
14d4e 73 2e 20 20 54 68 65 20 70 2d 3e 70 4e 65 78 74  s.  The p->pNext
14d4f 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 6f 74   is a list of ot
14d50 68 65 72 0a 2a 2a 20 42 74 72 65 65 73 20 62 65  her.** Btrees be
14d51 6c 6f 6e 67 69 6e 67 20 74 6f 20 74 68 65 20 73  longing to the s
14d52 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ame database con
14d53 6e 65 63 74 69 6f 6e 20 61 73 20 74 68 65 20 70  nection as the p
14d54 20 42 74 72 65 65 0a 2a 2a 20 77 68 69 63 68 20   Btree.** which 
14d55 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65  need to be locke
14d56 64 20 61 66 74 65 72 20 70 2e 20 20 49 66 20 77  d after p.  If w
14d57 65 20 63 61 6e 6e 6f 74 20 67 65 74 20 61 20 6c  e cannot get a l
14d58 6f 63 6b 20 6f 6e 0a 2a 2a 20 70 2c 20 74 68 65  ock on.** p, the
14d59 6e 20 66 69 72 73 74 20 75 6e 6c 6f 63 6b 20 61  n first unlock a
14d5a 6c 6c 20 6f 66 20 74 68 65 20 6f 74 68 65 72 73  ll of the others
14d5b 20 6f 6e 20 70 2d 3e 70 4e 65 78 74 2c 20 74 68   on p->pNext, th
14d5c 65 6e 20 77 61 69 74 0a 2a 2a 20 66 6f 72 20 74  en wait.** for t
14d5d 68 65 20 6c 6f 63 6b 20 74 6f 20 62 65 63 6f 6d  he lock to becom
14d5e 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 70  e available on p
14d5f 2c 20 74 68 65 6e 20 72 65 6c 6f 63 6b 20 61 6c  , then relock al
14d60 6c 20 6f 66 20 74 68 65 0a 2a 2a 20 73 75 62 73  l of the.** subs
14d61 65 71 75 65 6e 74 20 42 74 72 65 65 73 20 74 68  equent Btrees th
14d62 61 74 20 64 65 73 69 72 65 20 61 20 6c 6f 63 6b  at desire a lock
14d63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
14d64 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
14d65 42 74 72 65 65 45 6e 74 65 72 28 42 74 72 65 65  BtreeEnter(Btree
14d66 20 2a 70 29 7b 0a 20 20 42 74 72 65 65 20 2a 70   *p){.  Btree *p
14d67 4c 61 74 65 72 3b 0a 0a 20 20 2f 2a 20 53 6f 6d  Later;..  /* Som
14d68 65 20 62 61 73 69 63 20 73 61 6e 69 74 79 20 63  e basic sanity c
14d69 68 65 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 42  hecking on the B
14d6a 74 72 65 65 2e 20 20 54 68 65 20 6c 69 73 74 20  tree.  The list 
14d6b 6f 66 20 42 74 72 65 65 73 0a 20 20 2a 2a 20 63  of Btrees.  ** c
14d6c 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 4e 65 78  onnected by pNex
14d6d 74 20 61 6e 64 20 70 50 72 65 76 20 73 68 6f 75  t and pPrev shou
14d6e 6c 64 20 62 65 20 69 6e 20 73 6f 72 74 65 64 20  ld be in sorted 
14d6f 6f 72 64 65 72 20 62 79 0a 20 20 2a 2a 20 42 74  order by.  ** Bt
14d70 72 65 65 2e 70 42 74 20 76 61 6c 75 65 2e 20 41  ree.pBt value. A
14d71 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74  ll elements of t
14d72 68 65 20 6c 69 73 74 20 73 68 6f 75 6c 64 20 62  he list should b
14d73 65 6c 6f 6e 67 20 74 6f 0a 20 20 2a 2a 20 74 68  elong to.  ** th
14d74 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f  e same connectio
14d75 6e 2e 20 4f 6e 6c 79 20 73 68 61 72 65 64 20 42  n. Only shared B
14d76 74 72 65 65 73 20 61 72 65 20 6f 6e 20 74 68 65  trees are on the
14d77 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 61 73 73 65   list. */.  asse
14d78 72 74 28 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20  rt( p->pNext==0 
14d79 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 42 74  || p->pNext->pBt
14d7a 3e 70 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73  >p->pBt );.  ass
14d7b 65 72 74 28 20 70 2d 3e 70 50 72 65 76 3d 3d 30  ert( p->pPrev==0
14d7c 20 7c 7c 20 70 2d 3e 70 50 72 65 76 2d 3e 70 42   || p->pPrev->pB
14d7d 74 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73  t<p->pBt );.  as
14d7e 73 65 72 74 28 20 70 2d 3e 70 4e 65 78 74 3d 3d  sert( p->pNext==
14d7f 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d 3e 64  0 || p->pNext->d
14d80 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 61 73  b==p->db );.  as
14d81 73 65 72 74 28 20 70 2d 3e 70 50 72 65 76 3d 3d  sert( p->pPrev==
14d82 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76 2d 3e 64  0 || p->pPrev->d
14d83 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 61 73  b==p->db );.  as
14d84 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c  sert( p->sharabl
14d85 65 20 7c 7c 20 28 70 2d 3e 70 4e 65 78 74 3d 3d  e || (p->pNext==
14d86 30 20 26 26 20 70 2d 3e 70 50 72 65 76 3d 3d 30  0 && p->pPrev==0
14d87 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  ) );..  /* Check
14d88 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e   for locking con
14d89 73 69 73 74 65 6e 63 79 20 2a 2f 0a 20 20 61 73  sistency */.  as
14d8a 73 65 72 74 28 20 21 70 2d 3e 6c 6f 63 6b 65 64  sert( !p->locked
14d8b 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63   || p->wantToLoc
14d8c 6b 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  k>0 );.  assert(
14d8d 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20   p->sharable || 
14d8e 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30  p->wantToLock==0
14d8f 20 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 73 68 6f   );..  /* We sho
14d90 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f 6c 64  uld already hold
14d91 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64   a lock on the d
14d92 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
14d93 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  on */.  assert( 
14d94 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
14d95 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ld(p->db->mutex)
14d96 20 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65 73 73   );..  /* Unless
14d97 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
14d98 20 73 68 61 72 61 62 6c 65 20 61 6e 64 20 75 6e   sharable and un
14d99 6c 6f 63 6b 65 64 2c 20 74 68 65 6e 20 42 74 53  locked, then BtS
14d9a 68 61 72 65 64 2e 64 62 0a 20 20 2a 2a 20 73 68  hared.db.  ** sh
14d9b 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 62 65 20  ould already be 
14d9c 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 2a  set correctly. *
14d9d 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e  /.  assert( (p->
14d9e 6c 6f 63 6b 65 64 3d 3d 30 20 26 26 20 70 2d 3e  locked==0 && p->
14d9f 73 68 61 72 61 62 6c 65 29 20 7c 7c 20 70 2d 3e  sharable) || p->
14da0 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29  pBt->db==p->db )
14da1 3b 0a 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 61  ;..  if( !p->sha
14da2 72 61 62 6c 65 20 29 20 72 65 74 75 72 6e 3b 0a  rable ) return;.
14da3 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b    p->wantToLock+
14da4 2b 3b 0a 20 20 69 66 28 20 70 2d 3e 6c 6f 63 6b  +;.  if( p->lock
14da5 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20  ed ) return;..  
14da6 2f 2a 20 49 6e 20 6d 6f 73 74 20 63 61 73 65 73  /* In most cases
14da7 2c 20 77 65 20 73 68 6f 75 6c 64 20 62 65 20 61  , we should be a
14da8 62 6c 65 20 74 6f 20 61 63 71 75 69 72 65 20 74  ble to acquire t
14da9 68 65 20 6c 6f 63 6b 20 77 65 0a 20 20 2a 2a 20  he lock we.  ** 
14daa 77 61 6e 74 20 77 69 74 68 6f 75 74 20 68 61 76  want without hav
14dab 69 6e 67 20 74 6f 20 67 6f 20 74 68 72 6f 75 67  ing to go throug
14dac 68 74 20 74 68 65 20 61 73 63 65 6e 64 69 6e 67  ht the ascending
14dad 20 6c 6f 63 6b 0a 20 20 2a 2a 20 70 72 6f 63 65   lock.  ** proce
14dae 64 75 72 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77  dure that follow
14daf 73 2e 20 20 4a 75 73 74 20 62 65 20 73 75 72 65  s.  Just be sure
14db0 20 6e 6f 74 20 74 6f 20 62 6c 6f 63 6b 2e 0a 20   not to block.. 
14db1 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
14db2 33 5f 6d 75 74 65 78 5f 74 72 79 28 70 2d 3e 70  3_mutex_try(p->p
14db3 42 74 2d 3e 6d 75 74 65 78 29 3d 3d 53 51 4c 49  Bt->mutex)==SQLI
14db4 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 2d 3e  TE_OK ){.    p->
14db5 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b  pBt->db = p->db;
14db6 0a 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d  .    p->locked =
14db7 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   1;.    return;.
14db8 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 20 61 76 6f    }..  /* To avo
14db9 69 64 20 64 65 61 64 6c 6f 63 6b 2c 20 66 69 72  id deadlock, fir
14dba 73 74 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 6c  st release all l
14dbb 6f 63 6b 73 20 77 69 74 68 20 61 20 6c 61 72 67  ocks with a larg
14dbc 65 72 0a 20 20 2a 2a 20 42 74 53 68 61 72 65 64  er.  ** BtShared
14dbd 20 61 64 64 72 65 73 73 2e 20 20 54 68 65 6e 20   address.  Then 
14dbe 61 63 71 75 69 72 65 20 6f 75 72 20 6c 6f 63 6b  acquire our lock
14dbf 2e 20 20 54 68 65 6e 20 72 65 61 63 71 75 69 72  .  Then reacquir
14dc0 65 0a 20 20 2a 2a 20 74 68 65 20 6f 74 68 65 72  e.  ** the other
14dc1 20 42 74 53 68 61 72 65 64 20 6c 6f 63 6b 73 20   BtShared locks 
14dc2 74 68 61 74 20 77 65 20 75 73 65 64 20 74 6f 20  that we used to 
14dc3 68 6f 6c 64 20 69 6e 20 61 73 63 65 6e 64 69 6e  hold in ascendin
14dc4 67 0a 20 20 2a 2a 20 6f 72 64 65 72 2e 0a 20 20  g.  ** order..  
14dc5 2a 2f 0a 20 20 66 6f 72 28 70 4c 61 74 65 72 3d  */.  for(pLater=
14dc6 70 2d 3e 70 4e 65 78 74 3b 20 70 4c 61 74 65 72  p->pNext; pLater
14dc7 3b 20 70 4c 61 74 65 72 3d 70 4c 61 74 65 72 2d  ; pLater=pLater-
14dc8 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61 73 73  >pNext){.    ass
14dc9 65 72 74 28 20 70 4c 61 74 65 72 2d 3e 73 68 61  ert( pLater->sha
14dca 72 61 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 73  rable );.    ass
14dcb 65 72 74 28 20 70 4c 61 74 65 72 2d 3e 70 4e 65  ert( pLater->pNe
14dcc 78 74 3d 3d 30 20 7c 7c 20 70 4c 61 74 65 72 2d  xt==0 || pLater-
14dcd 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70 4c 61 74  >pNext->pBt>pLat
14dce 65 72 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61  er->pBt );.    a
14dcf 73 73 65 72 74 28 20 21 70 4c 61 74 65 72 2d 3e  ssert( !pLater->
14dd0 6c 6f 63 6b 65 64 20 7c 7c 20 70 4c 61 74 65 72  locked || pLater
14dd1 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29  ->wantToLock>0 )
14dd2 3b 0a 20 20 20 20 69 66 28 20 70 4c 61 74 65 72  ;.    if( pLater
14dd3 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20  ->locked ){.    
14dd4 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74    unlockBtreeMut
14dd5 65 78 28 70 4c 61 74 65 72 29 3b 0a 20 20 20 20  ex(pLater);.    
14dd6 7d 0a 20 20 7d 0a 20 20 6c 6f 63 6b 42 74 72 65  }.  }.  lockBtre
14dd7 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 66 6f 72  eMutex(p);.  for
14dd8 28 70 4c 61 74 65 72 3d 70 2d 3e 70 4e 65 78 74  (pLater=p->pNext
14dd9 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72  ; pLater; pLater
14dda 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b  =pLater->pNext){
14ddb 0a 20 20 20 20 69 66 28 20 70 4c 61 74 65 72 2d  .    if( pLater-
14ddc 3e 77 61 6e 74 54 6f 4c 6f 63 6b 20 29 7b 0a 20  >wantToLock ){. 
14ddd 20 20 20 20 20 6c 6f 63 6b 42 74 72 65 65 4d 75       lockBtreeMu
14dde 74 65 78 28 70 4c 61 74 65 72 29 3b 0a 20 20 20  tex(pLater);.   
14ddf 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
14de0 45 78 69 74 20 74 68 65 20 72 65 63 75 72 73 69  Exit the recursi
14de1 76 65 20 6d 75 74 65 78 20 6f 6e 20 61 20 42 74  ve mutex on a Bt
14de2 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ree..*/.SQLITE_P
14de3 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
14de4 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 42 74  te3BtreeLeave(Bt
14de5 72 65 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70  ree *p){.  if( p
14de6 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20  ->sharable ){.  
14de7 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e    assert( p->wan
14de8 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 20  tToLock>0 );.   
14de9 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d   p->wantToLock--
14dea 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 77 61 6e  ;.    if( p->wan
14deb 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20  tToLock==0 ){.  
14dec 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 4d      unlockBtreeM
14ded 75 74 65 78 28 70 29 3b 0a 20 20 20 20 7d 0a 20  utex(p);.    }. 
14dee 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44   }.}..#ifndef ND
14def 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  EBUG./*.** Retur
14df0 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 42 74  n true if the Bt
14df1 53 68 61 72 65 64 20 6d 75 74 65 78 20 69 73 20  Shared mutex is 
14df2 68 65 6c 64 20 6f 6e 20 74 68 65 20 62 74 72 65  held on the btre
14df3 65 2c 20 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20  e, or if the.** 
14df4 42 2d 54 72 65 65 20 69 73 20 6e 6f 74 20 6d 61  B-Tree is not ma
14df5 72 6b 65 64 20 61 73 20 73 68 61 72 61 62 6c 65  rked as sharable
14df6 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
14df7 74 69 6e 65 20 69 73 20 75 73 65 64 20 6f 6e 6c  tine is used onl
14df8 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 73  y from within as
14df9 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
14dfa 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
14dfb 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
14dfc 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28  BtreeHoldsMutex(
14dfd 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73  Btree *p){.  ass
14dfe 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65  ert( p->sharable
14dff 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64  ==0 || p->locked
14e00 3d 3d 30 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f  ==0 || p->wantTo
14e01 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 73 73 65  Lock>0 );.  asse
14e02 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 3d  rt( p->sharable=
14e03 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64 3d  =0 || p->locked=
14e04 3d 30 20 7c 7c 20 70 2d 3e 64 62 3d 3d 70 2d 3e  =0 || p->db==p->
14e05 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73  pBt->db );.  ass
14e06 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65  ert( p->sharable
14e07 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65 64  ==0 || p->locked
14e08 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
14e09 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74  utex_held(p->pBt
14e0a 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
14e0b 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c  sert( p->sharabl
14e0c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65  e==0 || p->locke
14e0d 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  d==0 || sqlite3_
14e0e 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62  mutex_held(p->db
14e0f 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 72  ->mutex) );..  r
14e10 65 74 75 72 6e 20 28 70 2d 3e 73 68 61 72 61 62  eturn (p->sharab
14e11 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b  le==0 || p->lock
14e12 65 64 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a  ed);.}.#endif...
14e13 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
14e14 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a  MIT_INCRBLOB./*.
14e15 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20 6c 65 61  ** Enter and lea
14e16 76 65 20 61 20 6d 75 74 65 78 20 6f 6e 20 61 20  ve a mutex on a 
14e17 42 74 72 65 65 20 67 69 76 65 6e 20 61 20 63 75  Btree given a cu
14e18 72 73 6f 72 20 6f 77 6e 65 64 20 62 79 20 74 68  rsor owned by th
14e19 61 74 0a 2a 2a 20 42 74 72 65 65 2e 20 20 54 68  at.** Btree.  Th
14e1a 65 73 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 73  ese entry points
14e1b 20 61 72 65 20 75 73 65 64 20 62 79 20 69 6e 63   are used by inc
14e1c 72 65 6d 65 6e 74 61 6c 20 49 2f 4f 20 61 6e 64  remental I/O and
14e1d 20 63 61 6e 20 62 65 0a 2a 2a 20 6f 6d 69 74 74   can be.** omitt
14e1e 65 64 20 69 66 20 74 68 61 74 20 6d 6f 64 75 6c  ed if that modul
14e1f 65 20 69 73 20 6e 6f 74 20 75 73 65 64 2e 0a 2a  e is not used..*
14e20 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
14e21 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
14e22 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 42 74  eeEnterCursor(Bt
14e23 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
14e24 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
14e25 65 72 28 70 43 75 72 2d 3e 70 42 74 72 65 65 29  er(pCur->pBtree)
14e26 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
14e27 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
14e28 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28  treeLeaveCursor(
14e29 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
14e2a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
14e2b 65 61 76 65 28 70 43 75 72 2d 3e 70 42 74 72 65  eave(pCur->pBtre
14e2c 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  e);.}.#endif /* 
14e2d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
14e2e 42 4c 4f 42 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  BLOB */.../*.** 
14e2f 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78 20  Enter the mutex 
14e30 6f 6e 20 65 76 65 72 79 20 42 74 72 65 65 20 61  on every Btree a
14e31 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
14e32 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e   database.** con
14e33 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 69  nection.  This i
14e34 73 20 6e 65 65 64 65 64 20 28 66 6f 72 20 65 78  s needed (for ex
14e35 61 6d 70 6c 65 29 20 70 72 69 6f 72 20 74 6f 20  ample) prior to 
14e36 70 61 72 73 69 6e 67 0a 2a 2a 20 61 20 73 74 61  parsing.** a sta
14e37 74 65 6d 65 6e 74 20 73 69 6e 63 65 20 77 65 20  tement since we 
14e38 77 69 6c 6c 20 62 65 20 63 6f 6d 70 61 72 69 6e  will be comparin
14e39 67 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75  g table and colu
14e3a 6d 6e 20 6e 61 6d 65 73 0a 2a 2a 20 61 67 61 69  mn names.** agai
14e3b 6e 73 74 20 61 6c 6c 20 73 63 68 65 6d 61 73 20  nst all schemas 
14e3c 61 6e 64 20 77 65 20 64 6f 20 6e 6f 74 20 77 61  and we do not wa
14e3d 6e 74 20 74 68 6f 73 65 20 73 63 68 65 6d 61 73  nt those schemas
14e3e 20 62 65 69 6e 67 0a 2a 2a 20 72 65 73 65 74 20   being.** reset 
14e3f 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 75  out from under u
14e40 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69  s..**.** There i
14e41 73 20 61 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  s a correspondin
14e42 67 20 6c 65 61 76 65 2d 61 6c 6c 20 70 72 6f 63  g leave-all proc
14e43 65 64 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 6e  edures..**.** En
14e44 74 65 72 20 74 68 65 20 6d 75 74 65 78 65 73 20  ter the mutexes 
14e45 69 6e 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64  in accending ord
14e46 65 72 20 62 79 20 42 74 53 68 61 72 65 64 20 70  er by BtShared p
14e47 6f 69 6e 74 65 72 20 61 64 64 72 65 73 73 0a 2a  ointer address.*
14e48 2a 20 74 6f 20 61 76 6f 69 64 20 74 68 65 20 70  * to avoid the p
14e49 6f 73 73 69 62 69 6c 69 74 79 20 6f 66 20 64 65  ossibility of de
14e4a 61 64 6c 6f 63 6b 20 77 68 65 6e 20 74 77 6f 20  adlock when two 
14e4b 74 68 72 65 61 64 73 20 77 69 74 68 0a 2a 2a 20  threads with.** 
14e4c 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 74 72 65  two or more btre
14e4d 65 73 20 69 6e 20 63 6f 6d 6d 6f 6e 20 62 6f 74  es in common bot
14e4e 68 20 74 72 79 20 74 6f 20 6c 6f 63 6b 20 61 6c  h try to lock al
14e4f 6c 20 74 68 65 69 72 20 62 74 72 65 65 73 0a 2a  l their btrees.*
14e50 2a 20 61 74 20 74 68 65 20 73 61 6d 65 20 69 6e  * at the same in
14e51 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  stant..*/.SQLITE
14e52 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
14e53 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41  lite3BtreeEnterA
14e54 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ll(sqlite3 *db){
14e55 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 74 72 65  .  int i;.  Btre
14e56 65 20 2a 70 2c 20 2a 70 4c 61 74 65 72 3b 0a 20  e *p, *pLater;. 
14e57 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
14e58 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
14e59 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28  mutex) );.  for(
14e5a 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  i=0; i<db->nDb; 
14e5b 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 64 62  i++){.    p = db
14e5c 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20  ->aDb[i].pBt;.  
14e5d 20 20 61 73 73 65 72 74 28 20 21 70 20 7c 7c 20    assert( !p || 
14e5e 28 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 26 26  (p->locked==0 &&
14e5f 20 70 2d 3e 73 68 61 72 61 62 6c 65 29 20 7c 7c   p->sharable) ||
14e60 20 70 2d 3e 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e   p->pBt->db==p->
14e61 64 62 20 29 3b 0a 20 20 20 20 69 66 28 20 70 20  db );.    if( p 
14e62 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29  && p->sharable )
14e63 7b 0a 20 20 20 20 20 20 70 2d 3e 77 61 6e 74 54  {.      p->wantT
14e64 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 69  oLock++;.      i
14e65 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b  f( !p->locked ){
14e66 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
14e67 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d   p->wantToLock==
14e68 31 20 29 3b 0a 20 20 20 20 20 20 20 20 77 68 69  1 );.        whi
14e69 6c 65 28 20 70 2d 3e 70 50 72 65 76 20 29 20 70  le( p->pPrev ) p
14e6a 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20 20 20   = p->pPrev;.   
14e6b 20 20 20 20 20 2f 2a 20 52 65 61 73 6f 6e 20 66       /* Reason f
14e6c 6f 72 20 41 4c 57 41 59 53 3a 20 20 54 68 65 72  or ALWAYS:  Ther
14e6d 65 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61  e must be at lea
14e6e 73 74 20 6f 6e 20 75 6e 6c 6f 63 6b 65 64 20 42  st on unlocked B
14e6f 74 72 65 65 20 69 6e 0a 20 20 20 20 20 20 20 20  tree in.        
14e70 2a 2a 20 74 68 65 20 63 68 61 69 6e 2e 20 20 4f  ** the chain.  O
14e71 74 68 65 72 77 69 73 65 20 74 68 65 20 21 70 2d  therwise the !p-
14e72 3e 6c 6f 63 6b 65 64 20 74 65 73 74 20 61 62 6f  >locked test abo
14e73 76 65 20 77 6f 75 6c 64 20 68 61 76 65 20 66 61  ve would have fa
14e74 69 6c 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20  iled */.        
14e75 77 68 69 6c 65 28 20 70 2d 3e 6c 6f 63 6b 65 64  while( p->locked
14e76 20 26 26 20 41 4c 57 41 59 53 28 70 2d 3e 70 4e   && ALWAYS(p->pN
14e77 65 78 74 29 20 29 20 70 20 3d 20 70 2d 3e 70 4e  ext) ) p = p->pN
14e78 65 78 74 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  ext;.        for
14e79 28 70 4c 61 74 65 72 20 3d 20 70 2d 3e 70 4e 65  (pLater = p->pNe
14e7a 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74  xt; pLater; pLat
14e7b 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74  er=pLater->pNext
14e7c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
14e7d 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20   pLater->locked 
14e7e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 75  ){.            u
14e7f 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28  nlockBtreeMutex(
14e80 70 4c 61 74 65 72 29 3b 0a 20 20 20 20 20 20 20  pLater);.       
14e81 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
14e82 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 20         while( p 
14e83 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 6f 63  ){.          loc
14e84 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a  kBtreeMutex(p);.
14e85 20 20 20 20 20 20 20 20 20 20 70 20 3d 20 70 2d            p = p-
14e86 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20  >pNext;.        
14e87 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
14e88 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49    }.}.SQLITE_PRI
14e89 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
14e8a 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 73  3BtreeLeaveAll(s
14e8b 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
14e8c 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20 2a 70  nt i;.  Btree *p
14e8d 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
14e8e 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
14e8f 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66  b->mutex) );.  f
14e90 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
14e91 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d  b; i++){.    p =
14e92 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
14e93 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 70 2d  .    if( p && p-
14e94 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20  >sharable ){.   
14e95 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61     assert( p->wa
14e96 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20  ntToLock>0 );.  
14e97 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63      p->wantToLoc
14e98 6b 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 70  k--;.      if( p
14e99 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20  ->wantToLock==0 
14e9a 29 7b 0a 20 20 20 20 20 20 20 20 75 6e 6c 6f 63  ){.        unloc
14e9b 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a  kBtreeMutex(p);.
14e9c 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
14e9d 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  }.}..#ifndef NDE
14e9e 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  BUG./*.** Return
14e9f 20 74 72 75 65 20 69 66 20 74 68 65 20 63 75 72   true if the cur
14ea0 72 65 6e 74 20 74 68 72 65 61 64 20 68 6f 6c 64  rent thread hold
14ea1 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  s the database c
14ea2 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6d 75 74  onnection.** mut
14ea3 65 78 20 61 6e 64 20 61 6c 6c 20 72 65 71 75 69  ex and all requi
14ea4 72 65 64 20 42 74 53 68 61 72 65 64 20 6d 75 74  red BtShared mut
14ea5 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  exes..**.** This
14ea6 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
14ea7 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29   inside assert()
14ea8 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79   statements only
14ea9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
14eaa 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
14eab 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65  treeHoldsAllMute
14eac 78 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29  xes(sqlite3 *db)
14ead 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  {.  int i;.  if(
14eae 20 21 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f   !sqlite3_mutex_
14eaf 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
14eb0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
14eb1 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
14eb2 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
14eb3 0a 20 20 20 20 42 74 72 65 65 20 2a 70 3b 0a 20  .    Btree *p;. 
14eb4 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69     p = db->aDb[i
14eb5 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70  ].pBt;.    if( p
14eb6 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20   && p->sharable 
14eb7 26 26 0a 20 20 20 20 20 20 20 20 20 28 70 2d 3e  &&.         (p->
14eb8 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 7c 7c  wantToLock==0 ||
14eb9 20 21 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f   !sqlite3_mutex_
14eba 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74  held(p->pBt->mut
14ebb 65 78 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65  ex)) ){.      re
14ebc 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
14ebd 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  }.  return 1;.}.
14ebe 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47  #endif /* NDEBUG
14ebf 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61   */../*.** Add a
14ec0 20 6e 65 77 20 42 74 72 65 65 20 70 6f 69 6e 74   new Btree point
14ec1 65 72 20 74 6f 20 61 20 42 74 72 65 65 4d 75 74  er to a BtreeMut
14ec2 65 78 41 72 72 61 79 2e 20 0a 2a 2a 20 69 66 20  exArray. .** if 
14ec3 74 68 65 20 70 6f 69 6e 74 65 72 20 63 61 6e 20  the pointer can 
14ec4 70 6f 73 73 69 62 6c 79 20 62 65 20 73 68 61 72  possibly be shar
14ec5 65 64 20 77 69 74 68 0a 2a 2a 20 61 6e 6f 74 68  ed with.** anoth
14ec6 65 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  er database conn
14ec7 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  ection..**.** Th
14ec8 65 20 70 6f 69 6e 74 65 72 73 20 61 72 65 20 6b  e pointers are k
14ec9 65 70 74 20 69 6e 20 73 6f 72 74 65 64 20 6f 72  ept in sorted or
14eca 64 65 72 20 62 79 20 70 42 74 72 65 65 2d 3e 70  der by pBtree->p
14ecb 42 74 2e 20 20 54 68 61 74 0a 2a 2a 20 77 61 79  Bt.  That.** way
14ecc 20 77 68 65 6e 20 77 65 20 67 6f 20 74 6f 20 65   when we go to e
14ecd 6e 74 65 72 20 61 6c 6c 20 74 68 65 20 6d 75 74  nter all the mut
14ece 65 78 65 73 2c 20 77 65 20 63 61 6e 20 65 6e 74  exes, we can ent
14ecf 65 72 20 74 68 65 6d 0a 2a 2a 20 69 6e 20 6f 72  er them.** in or
14ed0 64 65 72 20 77 69 74 68 6f 75 74 20 65 76 65 72  der without ever
14ed1 79 20 68 61 76 69 6e 67 20 74 6f 20 62 61 63 6b  y having to back
14ed2 75 70 20 61 6e 64 20 72 65 74 72 79 20 61 6e 64  up and retry and
14ed3 20 77 69 74 68 6f 75 74 0a 2a 2a 20 77 6f 72 72   without.** worr
14ed4 79 69 6e 67 20 61 62 6f 75 74 20 64 65 61 64 6c  ying about deadl
14ed5 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e  ock..**.** The n
14ed6 75 6d 62 65 72 20 6f 66 20 73 68 61 72 65 64 20  umber of shared 
14ed7 62 74 72 65 65 73 20 77 69 6c 6c 20 61 6c 77 61  btrees will alwa
14ed8 79 73 20 62 65 20 73 6d 61 6c 6c 20 28 75 73 75  ys be small (usu
14ed9 61 6c 6c 79 20 30 20 6f 72 20 31 29 0a 2a 2a 20  ally 0 or 1).** 
14eda 73 6f 20 61 6e 20 69 6e 73 65 72 74 69 6f 6e 20  so an insertion 
14edb 73 6f 72 74 20 69 73 20 61 6e 20 61 64 65 71 75  sort is an adequ
14edc 61 74 65 20 61 6c 67 6f 72 69 74 68 6d 20 68 65  ate algorithm he
14edd 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  re..*/.SQLITE_PR
14ede 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
14edf 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  e3BtreeMutexArra
14ee0 79 49 6e 73 65 72 74 28 42 74 72 65 65 4d 75 74  yInsert(BtreeMut
14ee1 65 78 41 72 72 61 79 20 2a 70 41 72 72 61 79 2c  exArray *pArray,
14ee2 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 29 7b   Btree *pBtree){
14ee3 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 42  .  int i, j;.  B
14ee4 74 53 68 61 72 65 64 20 2a 70 42 74 3b 0a 20 20  tShared *pBt;.  
14ee5 69 66 28 20 70 42 74 72 65 65 3d 3d 30 20 7c 7c  if( pBtree==0 ||
14ee6 20 70 42 74 72 65 65 2d 3e 73 68 61 72 61 62 6c   pBtree->sharabl
14ee7 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23  e==0 ) return;.#
14ee8 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
14ee9 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
14eea 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b  <pArray->nMutex;
14eeb 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73   i++){.      ass
14eec 65 72 74 28 20 70 41 72 72 61 79 2d 3e 61 42 74  ert( pArray->aBt
14eed 72 65 65 5b 69 5d 21 3d 70 42 74 72 65 65 20 29  ree[i]!=pBtree )
14eee 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
14eef 69 66 0a 20 20 61 73 73 65 72 74 28 20 70 41 72  if.  assert( pAr
14ef0 72 61 79 2d 3e 6e 4d 75 74 65 78 3e 3d 30 20 29  ray->nMutex>=0 )
14ef1 3b 0a 20 20 61 73 73 65 72 74 28 20 70 41 72 72  ;.  assert( pArr
14ef2 61 79 2d 3e 6e 4d 75 74 65 78 3c 41 72 72 61 79  ay->nMutex<Array
14ef3 53 69 7a 65 28 70 41 72 72 61 79 2d 3e 61 42 74  Size(pArray->aBt
14ef4 72 65 65 29 2d 31 20 29 3b 0a 20 20 70 42 74 20  ree)-1 );.  pBt 
14ef5 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a 20  = pBtree->pBt;. 
14ef6 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 72   for(i=0; i<pArr
14ef7 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29  ay->nMutex; i++)
14ef8 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 41  {.    assert( pA
14ef9 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 21  rray->aBtree[i]!
14efa 3d 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20 69  =pBtree );.    i
14efb 66 28 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65  f( pArray->aBtre
14efc 65 5b 69 5d 2d 3e 70 42 74 3e 70 42 74 20 29 7b  e[i]->pBt>pBt ){
14efd 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 70 41 72  .      for(j=pAr
14efe 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 6a 3e 69  ray->nMutex; j>i
14eff 3b 20 6a 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20  ; j--){.        
14f00 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 6a  pArray->aBtree[j
14f01 5d 20 3d 20 70 41 72 72 61 79 2d 3e 61 42 74 72  ] = pArray->aBtr
14f02 65 65 5b 6a 2d 31 5d 3b 0a 20 20 20 20 20 20 7d  ee[j-1];.      }
14f03 0a 20 20 20 20 20 20 70 41 72 72 61 79 2d 3e 61  .      pArray->a
14f04 42 74 72 65 65 5b 69 5d 20 3d 20 70 42 74 72 65  Btree[i] = pBtre
14f05 65 3b 0a 20 20 20 20 20 20 70 41 72 72 61 79 2d  e;.      pArray-
14f06 3e 6e 4d 75 74 65 78 2b 2b 3b 0a 20 20 20 20 20  >nMutex++;.     
14f07 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
14f08 20 7d 0a 20 20 70 41 72 72 61 79 2d 3e 61 42 74   }.  pArray->aBt
14f09 72 65 65 5b 70 41 72 72 61 79 2d 3e 6e 4d 75 74  ree[pArray->nMut
14f0a 65 78 2b 2b 5d 20 3d 20 70 42 74 72 65 65 3b 0a  ex++] = pBtree;.
14f0b 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 74  }../*.** Enter t
14f0c 68 65 20 6d 75 74 65 78 20 6f 66 20 65 76 65 72  he mutex of ever
14f0d 79 20 62 74 72 65 65 20 69 6e 20 74 68 65 20 61  y btree in the a
14f0e 72 72 61 79 2e 20 20 54 68 69 73 20 72 6f 75 74  rray.  This rout
14f0f 69 6e 65 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64  ine is.** called
14f10 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   at the beginnin
14f11 67 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62 65  g of sqlite3Vdbe
14f12 45 78 65 63 28 29 2e 20 20 54 68 65 20 6d 75 74  Exec().  The mut
14f13 65 78 65 73 20 61 72 65 0a 2a 2a 20 65 78 69 74  exes are.** exit
14f14 65 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  ed at the end of
14f15 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69   the same functi
14f16 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
14f17 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
14f18 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  e3BtreeMutexArra
14f19 79 45 6e 74 65 72 28 42 74 72 65 65 4d 75 74 65  yEnter(BtreeMute
14f1a 78 41 72 72 61 79 20 2a 70 41 72 72 61 79 29 7b  xArray *pArray){
14f1b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28  .  int i;.  for(
14f1c 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e  i=0; i<pArray->n
14f1d 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20  Mutex; i++){.   
14f1e 20 42 74 72 65 65 20 2a 70 20 3d 20 70 41 72 72   Btree *p = pArr
14f1f 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 3b 0a 20  ay->aBtree[i];. 
14f20 20 20 20 2f 2a 20 53 6f 6d 65 20 62 61 73 69 63     /* Some basic
14f21 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67   sanity checking
14f22 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
14f23 69 3d 3d 30 20 7c 7c 20 70 41 72 72 61 79 2d 3e  i==0 || pArray->
14f24 61 42 74 72 65 65 5b 69 2d 31 5d 2d 3e 70 42 74  aBtree[i-1]->pBt
14f25 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61  <p->pBt );.    a
14f26 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f 63 6b 65  ssert( !p->locke
14f27 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f  d || p->wantToLo
14f28 63 6b 3e 30 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  ck>0 );..    /* 
14f29 57 65 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64  We should alread
14f2a 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e  y hold a lock on
14f2b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
14f2c 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  nnection */.    
14f2d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
14f2e 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62  mutex_held(p->db
14f2f 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 20  ->mutex) );..   
14f30 20 2f 2a 20 54 68 65 20 42 74 72 65 65 20 69 73   /* The Btree is
14f31 20 73 68 61 72 61 62 6c 65 20 62 65 63 61 75 73   sharable becaus
14f32 65 20 6f 6e 6c 79 20 73 68 61 72 61 62 6c 65 20  e only sharable 
14f33 42 74 72 65 65 73 20 61 72 65 20 65 6e 74 65 72  Btrees are enter
14f34 65 64 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74  ed.    ** into t
14f35 68 65 20 61 72 72 61 79 20 69 6e 20 74 68 65 20  he array in the 
14f36 66 69 72 73 74 20 70 6c 61 63 65 2e 20 2a 2f 0a  first place. */.
14f37 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73      assert( p->s
14f38 68 61 72 61 62 6c 65 20 29 3b 0a 0a 20 20 20 20  harable );..    
14f39 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b  p->wantToLock++;
14f3a 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 6c 6f 63  .    if( !p->loc
14f3b 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63  ked ){.      loc
14f3c 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a  kBtreeMutex(p);.
14f3d 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
14f3e 2a 2a 20 4c 65 61 76 65 20 74 68 65 20 6d 75 74  ** Leave the mut
14f3f 65 78 20 6f 66 20 65 76 65 72 79 20 62 74 72 65  ex of every btre
14f40 65 20 69 6e 20 74 68 65 20 67 72 6f 75 70 2e 0a  e in the group..
14f41 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14f42 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
14f43 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61  reeMutexArrayLea
14f44 76 65 28 42 74 72 65 65 4d 75 74 65 78 41 72 72  ve(BtreeMutexArr
14f45 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69  ay *pArray){.  i
14f46 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
14f47 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65   i<pArray->nMute
14f48 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72  x; i++){.    Btr
14f49 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e  ee *p = pArray->
14f4a 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f  aBtree[i];.    /
14f4b 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e  * Some basic san
14f4c 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a  ity checking */.
14f4d 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30      assert( i==0
14f4e 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72   || pArray->aBtr
14f4f 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e  ee[i-1]->pBt<p->
14f50 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72  pBt );.    asser
14f51 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 3b 0a  t( p->locked );.
14f52 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77      assert( p->w
14f53 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 0a  antToLock>0 );..
14f54 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64      /* We should
14f55 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20   already hold a 
14f56 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
14f57 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
14f58 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  */.    assert( s
14f59 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14f5a 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  d(p->db->mutex) 
14f5b 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54  );..    p->wantT
14f5c 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 28  oLock--;.    if(
14f5d 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d   p->wantToLock==
14f5e 30 20 29 7b 0a 20 20 20 20 20 20 75 6e 6c 6f 63  0 ){.      unloc
14f5f 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a  kBtreeMutex(p);.
14f60 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c      }.  }.}..#el
14f61 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  se.SQLITE_PRIVAT
14f62 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
14f63 72 65 65 45 6e 74 65 72 28 42 74 72 65 65 20 2a  reeEnter(Btree *
14f64 70 29 7b 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62  p){.  p->pBt->db
14f65 20 3d 20 70 2d 3e 64 62 3b 0a 7d 0a 53 51 4c 49   = p->db;.}.SQLI
14f66 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
14f67 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
14f68 72 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62  rAll(sqlite3 *db
14f69 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f  ){.  int i;.  fo
14f6a 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62  r(i=0; i<db->nDb
14f6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65  ; i++){.    Btre
14f6c 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69  e *p = db->aDb[i
14f6d 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70  ].pBt;.    if( p
14f6e 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 42 74   ){.      p->pBt
14f6f 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20  ->db = p->db;.  
14f70 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66    }.  }.}.#endif
14f71 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 54 48   /* if SQLITE_TH
14f72 52 45 41 44 53 41 46 45 20 2a 2f 0a 23 65 6e 64  READSAFE */.#end
14f73 69 66 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c  if /* ifndef SQL
14f74 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
14f75 43 41 43 48 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  CACHE */../*****
14f76 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
14f77 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a   btmutex.c *****
14f78 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14f79 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14f7a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
14f7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
14f7c 66 69 6c 65 20 62 74 72 65 65 2e 63 20 2a 2a 2a  file btree.c ***
14f7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14f7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14f7f 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
14f80 32 30 30 34 20 41 70 72 69 6c 20 36 0a 2a 2a 0a  2004 April 6.**.
14f81 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
14f82 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
14f83 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
14f84 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
14f85 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
14f86 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
14f87 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
14f88 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
14f89 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
14f8a 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
14f8b 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
14f8c 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
14f8d 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
14f8e 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
14f8f 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
14f90 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
14f91 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
14f92 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
14f93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14f94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14f95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14f96 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24  ***********.** $
14f97 49 64 3a 20 62 74 72 65 65 2e 63 2c 76 20 31 2e  Id: btree.c,v 1.
14f98 37 30 35 20 32 30 30 39 2f 30 38 2f 31 30 20 30  705 2009/08/10 0
14f99 33 3a 35 37 3a 35 38 20 73 68 61 6e 65 20 45 78  3:57:58 shane Ex
14f9a 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  p $.**.** This f
14f9b 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  ile implements a
14f9c 20 65 78 74 65 72 6e 61 6c 20 28 64 69 73 6b 2d   external (disk-
14f9d 62 61 73 65 64 29 20 64 61 74 61 62 61 73 65 20  based) database 
14f9e 75 73 69 6e 67 20 42 54 72 65 65 73 2e 0a 2a 2a  using BTrees..**
14f9f 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72 20   See the header 
14fa0 63 6f 6d 6d 65 6e 74 20 6f 6e 20 22 62 74 72 65  comment on "btre
14fa1 65 49 6e 74 2e 68 22 20 66 6f 72 20 61 64 64 69  eInt.h" for addi
14fa2 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
14fa3 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c 75 64 69 6e 67  on..** Including
14fa4 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f   a description o
14fa5 66 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 61 6e  f file format an
14fa6 64 20 61 6e 20 6f 76 65 72 76 69 65 77 20 6f 66  d an overview of
14fa7 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 0a   operation..*/..
14fa8 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 64 65 72  /*.** The header
14fa9 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 70 70   string that app
14faa 65 61 72 73 20 61 74 20 74 68 65 20 62 65 67 69  ears at the begi
14fab 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a  nning of every.*
14fac 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73  * SQLite databas
14fad 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  e..*/.static con
14fae 73 74 20 63 68 61 72 20 7a 4d 61 67 69 63 48 65  st char zMagicHe
14faf 61 64 65 72 5b 5d 20 3d 20 53 51 4c 49 54 45 5f  ader[] = SQLITE_
14fb0 46 49 4c 45 5f 48 45 41 44 45 52 3b 0a 0a 2f 2a  FILE_HEADER;../*
14fb1 0a 2a 2a 20 53 65 74 20 74 68 69 73 20 67 6c 6f  .** Set this glo
14fb2 62 61 6c 20 76 61 72 69 61 62 6c 65 20 74 6f 20  bal variable to 
14fb3 31 20 74 6f 20 65 6e 61 62 6c 65 20 74 72 61 63  1 to enable trac
14fb4 69 6e 67 20 75 73 69 6e 67 20 74 68 65 20 54 52  ing using the TR
14fb5 41 43 45 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a 2f  ACE.** macro..*/
14fb6 0a 23 69 66 20 30 0a 69 6e 74 20 73 71 6c 69 74  .#if 0.int sqlit
14fb7 65 33 42 74 72 65 65 54 72 61 63 65 3d 31 3b 20  e3BtreeTrace=1; 
14fb8 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e 61 62   /* True to enab
14fb9 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f 0a 23 20  le tracing */.# 
14fba 64 65 66 69 6e 65 20 54 52 41 43 45 28 58 29 20  define TRACE(X) 
14fbb 20 69 66 28 73 71 6c 69 74 65 33 42 74 72 65 65   if(sqlite3Btree
14fbc 54 72 61 63 65 29 7b 70 72 69 6e 74 66 20 58 3b  Trace){printf X;
14fbd 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b 7d  fflush(stdout);}
14fbe 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
14fbf 54 52 41 43 45 28 58 29 0a 23 65 6e 64 69 66 0a  TRACE(X).#endif.
14fc0 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
14fc1 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
14fc2 43 48 45 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74  CHE./*.** A list
14fc3 20 6f 66 20 42 74 53 68 61 72 65 64 20 6f 62 6a   of BtShared obj
14fc4 65 63 74 73 20 74 68 61 74 20 61 72 65 20 65 6c  ects that are el
14fc5 69 67 69 62 6c 65 20 66 6f 72 20 70 61 72 74 69  igible for parti
14fc6 63 69 70 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 73  cipation.** in s
14fc7 68 61 72 65 64 20 63 61 63 68 65 2e 20 20 54 68  hared cache.  Th
14fc8 69 73 20 76 61 72 69 61 62 6c 65 20 68 61 73 20  is variable has 
14fc9 66 69 6c 65 20 73 63 6f 70 65 20 64 75 72 69 6e  file scope durin
14fca 67 20 6e 6f 72 6d 61 6c 20 62 75 69 6c 64 73 2c  g normal builds,
14fcb 0a 2a 2a 20 62 75 74 20 74 68 65 20 74 65 73 74  .** but the test
14fcc 20 68 61 72 6e 65 73 73 20 6e 65 65 64 73 20 74   harness needs t
14fcd 6f 20 61 63 63 65 73 73 20 69 74 20 73 6f 20 77  o access it so w
14fce 65 20 6d 61 6b 65 20 69 74 20 67 6c 6f 62 61 6c  e make it global
14fcf 20 66 6f 72 20 0a 2a 2a 20 74 65 73 74 20 62 75   for .** test bu
14fd0 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65  ilds..**.** Acce
14fd1 73 73 20 74 6f 20 74 68 69 73 20 76 61 72 69 61  ss to this varia
14fd2 62 6c 65 20 69 73 20 70 72 6f 74 65 63 74 65 64  ble is protected
14fd3 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58   by SQLITE_MUTEX
14fd4 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 2e 0a  _STATIC_MASTER..
14fd5 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
14fd6 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49  _TEST.SQLITE_PRI
14fd7 56 41 54 45 20 42 74 53 68 61 72 65 64 20 2a 53  VATE BtShared *S
14fd8 51 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74 65  QLITE_WSD sqlite
14fd9 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74  3SharedCacheList
14fda 20 3d 20 30 3b 0a 23 65 6c 73 65 0a 73 74 61 74   = 0;.#else.stat
14fdb 69 63 20 42 74 53 68 61 72 65 64 20 2a 53 51 4c  ic BtShared *SQL
14fdc 49 54 45 5f 57 53 44 20 73 71 6c 69 74 65 33 53  ITE_WSD sqlite3S
14fdd 68 61 72 65 64 43 61 63 68 65 4c 69 73 74 20 3d  haredCacheList =
14fde 20 30 3b 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69   0;.#endif.#endi
14fdf 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
14fe0 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f  _SHARED_CACHE */
14fe1 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
14fe2 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
14fe3 48 45 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20  HE./*.** Enable 
14fe4 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65 20 73  or disable the s
14fe5 68 61 72 65 64 20 70 61 67 65 72 20 61 6e 64 20  hared pager and 
14fe6 73 63 68 65 6d 61 20 66 65 61 74 75 72 65 73 2e  schema features.
14fe7 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
14fe8 69 6e 65 20 68 61 73 20 6e 6f 20 65 66 66 65 63  ine has no effec
14fe9 74 20 6f 6e 20 65 78 69 73 74 69 6e 67 20 64 61  t on existing da
14fea 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
14feb 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72 65  ns..** The share
14fec 64 20 63 61 63 68 65 20 73 65 74 74 69 6e 67 20  d cache setting 
14fed 65 66 66 65 63 74 73 20 6f 6e 6c 79 20 66 75 74  effects only fut
14fee 75 72 65 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20  ure calls to.** 
14fef 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2c 20  sqlite3_open(), 
14ff0 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29  sqlite3_open16()
14ff1 2c 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65  , or sqlite3_ope
14ff2 6e 5f 76 32 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  n_v2()..*/.SQLIT
14ff3 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
14ff4 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f  3_enable_shared_
14ff5 63 61 63 68 65 28 69 6e 74 20 65 6e 61 62 6c 65  cache(int enable
14ff6 29 7b 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  ){.  sqlite3Glob
14ff7 61 6c 43 6f 6e 66 69 67 2e 73 68 61 72 65 64 43  alConfig.sharedC
14ff8 61 63 68 65 45 6e 61 62 6c 65 64 20 3d 20 65 6e  acheEnabled = en
14ff9 61 62 6c 65 3b 0a 20 20 72 65 74 75 72 6e 20 53  able;.  return S
14ffa 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64  QLITE_OK;.}.#end
14ffb 69 66 0a 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c  if....#ifdef SQL
14ffc 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
14ffd 43 41 43 48 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20  CACHE.  /*.  ** 
14ffe 54 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 71 75  The functions qu
14fff 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61  erySharedCacheTa
15000 62 6c 65 4c 6f 63 6b 28 29 2c 20 73 65 74 53 68  bleLock(), setSh
15001 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f  aredCacheTableLo
15002 63 6b 28 29 2c 0a 20 20 2a 2a 20 61 6e 64 20 63  ck(),.  ** and c
15003 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63  learAllSharedCac
15004 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 0a 20  heTableLocks(). 
15005 20 2a 2a 20 6d 61 6e 69 70 75 6c 61 74 65 20 65   ** manipulate e
15006 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 42 74  ntries in the Bt
15007 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 6e  Shared.pLock lin
15008 6b 65 64 20 6c 69 73 74 20 75 73 65 64 20 74 6f  ked list used to
15009 20 73 74 6f 72 65 0a 20 20 2a 2a 20 73 68 61 72   store.  ** shar
1500a 65 64 2d 63 61 63 68 65 20 74 61 62 6c 65 20 6c  ed-cache table l
1500b 65 76 65 6c 20 6c 6f 63 6b 73 2e 20 49 66 20 74  evel locks. If t
1500c 68 65 20 6c 69 62 72 61 72 79 20 69 73 20 63 6f  he library is co
1500d 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a  mpiled with the.
1500e 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68    ** shared-cach
1500f 65 20 66 65 61 74 75 72 65 20 64 69 73 61 62 6c  e feature disabl
15010 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69  ed, then there i
15011 73 20 6f 6e 6c 79 20 65 76 65 72 20 6f 6e 65 20  s only ever one 
15012 75 73 65 72 0a 20 20 2a 2a 20 6f 66 20 65 61 63  user.  ** of eac
15013 68 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63  h BtShared struc
15014 74 75 72 65 20 61 6e 64 20 73 6f 20 74 68 69 73  ture and so this
15015 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6e 6f 74 20   locking is not 
15016 6e 65 63 65 73 73 61 72 79 2e 20 0a 20 20 2a 2a  necessary. .  **
15017 20 53 6f 20 64 65 66 69 6e 65 20 74 68 65 20 6c   So define the l
15018 6f 63 6b 20 72 65 6c 61 74 65 64 20 66 75 6e 63  ock related func
15019 74 69 6f 6e 73 20 61 73 20 6e 6f 2d 6f 70 73 2e  tions as no-ops.
1501a 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20  .  */.  #define 
1501b 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65  querySharedCache
1501c 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 29  TableLock(a,b,c)
1501d 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65   SQLITE_OK.  #de
1501e 66 69 6e 65 20 73 65 74 53 68 61 72 65 64 43 61  fine setSharedCa
1501f 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62  cheTableLock(a,b
15020 2c 63 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20  ,c) SQLITE_OK.  
15021 23 64 65 66 69 6e 65 20 63 6c 65 61 72 41 6c 6c  #define clearAll
15022 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
15023 4c 6f 63 6b 73 28 61 29 0a 20 20 23 64 65 66 69  Locks(a).  #defi
15024 6e 65 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c 53  ne downgradeAllS
15025 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
15026 6f 63 6b 73 28 61 29 0a 20 20 23 64 65 66 69 6e  ocks(a).  #defin
15027 65 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65  e hasSharedCache
15028 54 61 62 6c 65 4c 6f 63 6b 28 61 2c 62 2c 63 2c  TableLock(a,b,c,
15029 64 29 20 31 0a 20 20 23 64 65 66 69 6e 65 20 68  d) 1.  #define h
1502a 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28  asReadConflicts(
1502b 61 2c 20 62 29 20 30 0a 23 65 6e 64 69 66 0a 0a  a, b) 0.#endif..
1502c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1502d 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
1502e 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
1502f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73  DEBUG./*.** This
15030 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c   function is onl
15031 79 20 75 73 65 64 20 61 73 20 70 61 72 74 20 6f  y used as part o
15032 66 20 61 6e 20 61 73 73 65 72 74 28 29 20 73 74  f an assert() st
15033 61 74 65 6d 65 6e 74 2e 20 49 74 20 63 68 65 63  atement. It chec
15034 6b 73 0a 2a 2a 20 74 68 61 74 20 63 6f 6e 6e 65  ks.** that conne
15035 63 74 69 6f 6e 20 70 20 68 6f 6c 64 73 20 74 68  ction p holds th
15036 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 73  e required locks
15037 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74   to read or writ
15038 65 20 74 6f 20 74 68 65 20 0a 2a 2a 20 62 2d 74  e to the .** b-t
15039 72 65 65 20 77 69 74 68 20 72 6f 6f 74 20 70 61  ree with root pa
1503a 67 65 20 69 52 6f 6f 74 2e 20 49 66 20 73 6f 2c  ge iRoot. If so,
1503b 20 74 72 75 65 20 69 73 20 72 65 74 75 72 6e 65   true is returne
1503c 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61  d. Otherwise, fa
1503d 6c 73 65 2e 20 0a 2a 2a 20 46 6f 72 20 65 78 61  lse. .** For exa
1503e 6d 70 6c 65 2c 20 77 68 65 6e 20 77 72 69 74 69  mple, when writi
1503f 6e 67 20 74 6f 20 61 20 74 61 62 6c 65 20 62 2d  ng to a table b-
15040 74 72 65 65 20 77 69 74 68 20 72 6f 6f 74 2d 70  tree with root-p
15041 61 67 65 20 69 52 6f 6f 74 20 76 69 61 20 0a 2a  age iRoot via .*
15042 2a 20 42 74 72 65 65 20 63 6f 6e 6e 65 63 74 69  * Btree connecti
15043 6f 6e 20 70 42 74 72 65 65 3a 0a 2a 2a 0a 2a 2a  on pBtree:.**.**
15044 20 20 20 20 61 73 73 65 72 74 28 20 68 61 73 53      assert( hasS
15045 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
15046 6f 63 6b 28 70 42 74 72 65 65 2c 20 69 52 6f 6f  ock(pBtree, iRoo
15047 74 2c 20 30 2c 20 57 52 49 54 45 5f 4c 4f 43 4b  t, 0, WRITE_LOCK
15048 29 20 29 3b 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  ) );.**.** When 
15049 77 72 69 74 69 6e 67 20 74 6f 20 61 6e 20 69 6e  writing to an in
1504a 64 65 78 20 62 2d 74 72 65 65 20 74 68 61 74 20  dex b-tree that 
1504b 72 65 73 69 64 65 73 20 69 6e 20 61 20 73 68 61  resides in a sha
1504c 72 61 62 6c 65 20 64 61 74 61 62 61 73 65 2c 20  rable database, 
1504d 74 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20 73  the .** caller s
1504e 68 6f 75 6c 64 20 68 61 76 65 20 66 69 72 73 74  hould have first
1504f 20 6f 62 74 61 69 6e 65 64 20 61 20 6c 6f 63 6b   obtained a lock
15050 20 73 70 65 63 69 66 79 69 6e 67 20 74 68 65 20   specifying the 
15051 72 6f 6f 74 20 70 61 67 65 20 6f 66 0a 2a 2a 20  root page of.** 
15052 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
15053 67 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2e 20  g table b-tree. 
15054 54 68 69 73 20 6d 61 6b 65 73 20 74 68 69 6e 67  This makes thing
15055 73 20 61 20 62 69 74 20 6d 6f 72 65 20 63 6f 6d  s a bit more com
15056 70 6c 69 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20  plicated,.** as 
15057 74 68 69 73 20 6d 6f 64 75 6c 65 20 74 72 65 61  this module trea
15058 74 73 20 65 61 63 68 20 62 2d 74 72 65 65 20 61  ts each b-tree a
15059 73 20 61 20 73 65 70 61 72 61 74 65 20 73 74 72  s a separate str
1505a 75 63 74 75 72 65 2e 20 54 6f 20 64 65 74 65 72  ucture. To deter
1505b 6d 69 6e 65 0a 2a 2a 20 74 68 65 20 74 61 62 6c  mine.** the tabl
1505c 65 20 62 2d 74 72 65 65 20 63 6f 72 72 65 73 70  e b-tree corresp
1505d 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 69 6e  onding to the in
1505e 64 65 78 20 62 2d 74 72 65 65 20 62 65 69 6e 67  dex b-tree being
1505f 20 77 72 69 74 74 65 6e 2c 20 74 68 69 73 0a 2a   written, this.*
15060 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 74  * function has t
15061 6f 20 73 65 61 72 63 68 20 74 68 72 6f 75 67 68  o search through
15062 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
15063 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74  hema..**.** Inst
15064 65 61 64 20 6f 66 20 61 20 6c 6f 63 6b 20 6f 6e  ead of a lock on
15065 20 74 68 65 20 62 2d 74 72 65 65 20 72 6f 6f 74   the b-tree root
15066 65 64 20 61 74 20 70 61 67 65 20 69 52 6f 6f 74  ed at page iRoot
15067 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 6d 61 79  , the caller may
15068 0a 2a 2a 20 68 6f 6c 64 20 61 20 77 72 69 74 65  .** hold a write
15069 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 63 68  -lock on the sch
1506a 65 6d 61 20 74 61 62 6c 65 20 28 72 6f 6f 74 20  ema table (root 
1506b 70 61 67 65 20 31 29 2e 20 54 68 69 73 20 69 73  page 1). This is
1506c 20 61 6c 73 6f 0a 2a 2a 20 61 63 63 65 70 74 61   also.** accepta
1506d 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ble..*/.static i
1506e 6e 74 20 68 61 73 53 68 61 72 65 64 43 61 63 68  nt hasSharedCach
1506f 65 54 61 62 6c 65 4c 6f 63 6b 28 0a 20 20 42 74  eTableLock(.  Bt
15070 72 65 65 20 2a 70 42 74 72 65 65 2c 20 20 20 20  ree *pBtree,    
15071 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 74       /* Handle t
15072 68 61 74 20 6d 75 73 74 20 68 6f 6c 64 20 6c 6f  hat must hold lo
15073 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 52 6f  ck */.  Pgno iRo
15074 6f 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ot,            /
15075 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 62  * Root page of b
15076 2d 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69  -tree */.  int i
15077 73 49 6e 64 65 78 2c 20 20 20 20 20 20 20 20 20  sIndex,         
15078 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 52 6f    /* True if iRo
15079 6f 74 20 69 73 20 74 68 65 20 72 6f 6f 74 20 6f  ot is the root o
1507a 66 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65  f an index b-tre
1507b 65 20 2a 2f 0a 20 20 69 6e 74 20 65 4c 6f 63 6b  e */.  int eLock
1507c 54 79 70 65 20 20 20 20 20 20 20 20 20 20 2f 2a  Type          /*
1507d 20 52 65 71 75 69 72 65 64 20 6c 6f 63 6b 20 74   Required lock t
1507e 79 70 65 20 28 52 45 41 44 5f 4c 4f 43 4b 20 6f  ype (READ_LOCK o
1507f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 2a 2f  r WRITE_LOCK) */
15080 0a 29 7b 0a 20 20 53 63 68 65 6d 61 20 2a 70 53  .){.  Schema *pS
15081 63 68 65 6d 61 20 3d 20 28 53 63 68 65 6d 61 20  chema = (Schema 
15082 2a 29 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70  *)pBtree->pBt->p
15083 53 63 68 65 6d 61 3b 0a 20 20 50 67 6e 6f 20 69  Schema;.  Pgno i
15084 54 61 62 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63  Tab = 0;.  BtLoc
15085 6b 20 2a 70 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20  k *pLock;..  /* 
15086 49 66 20 74 68 69 73 20 62 2d 74 72 65 65 20 64  If this b-tree d
15087 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 73  atabase is not s
15088 68 61 72 65 61 62 6c 65 2c 20 6f 72 20 69 66 20  hareable, or if 
15089 74 68 65 20 63 6c 69 65 6e 74 20 69 73 20 72 65  the client is re
1508a 61 64 69 6e 67 0a 20 20 2a 2a 20 61 6e 64 20 68  ading.  ** and h
1508b 61 73 20 74 68 65 20 72 65 61 64 2d 75 6e 63 6f  as the read-unco
1508c 6d 6d 69 74 74 65 64 20 66 6c 61 67 20 73 65 74  mmitted flag set
1508d 2c 20 74 68 65 6e 20 6e 6f 20 6c 6f 63 6b 20 69  , then no lock i
1508e 73 20 72 65 71 75 69 72 65 64 2e 20 0a 20 20 2a  s required. .  *
1508f 2a 20 49 6e 20 74 68 65 73 65 20 63 61 73 65 73  * In these cases
15090 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 6d 6d   return true imm
15091 65 64 69 61 74 65 6c 79 2e 20 20 49 66 20 74 68  ediately.  If th
15092 65 20 63 6c 69 65 6e 74 20 69 73 20 72 65 61 64  e client is read
15093 69 6e 67 20 0a 20 20 2a 2a 20 6f 72 20 77 72 69  ing .  ** or wri
15094 74 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 62 2d  ting an index b-
15095 74 72 65 65 2c 20 62 75 74 20 74 68 65 20 73 63  tree, but the sc
15096 68 65 6d 61 20 69 73 20 6e 6f 74 20 6c 6f 61 64  hema is not load
15097 65 64 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 0a  ed, then return.
15098 20 20 2a 2a 20 74 72 75 65 20 61 6c 73 6f 2e 20    ** true also. 
15099 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
1509a 20 6c 6f 63 6b 20 69 73 20 72 65 71 75 69 72 65   lock is require
1509b 64 2c 20 62 75 74 20 69 74 20 69 73 20 74 6f 6f  d, but it is too
1509c 20 64 69 66 66 69 63 75 6c 74 0a 20 20 2a 2a 20   difficult.  ** 
1509d 74 6f 20 63 68 65 63 6b 20 69 66 20 74 68 65 20  to check if the 
1509e 63 6c 69 65 6e 74 20 61 63 74 75 61 6c 6c 79 20  client actually 
1509f 68 6f 6c 64 73 20 69 74 2e 20 54 68 69 73 20 64  holds it. This d
150a0 6f 65 73 6e 27 74 20 68 61 70 70 65 6e 20 76 65  oesn't happen ve
150a1 72 79 0a 20 20 2a 2a 20 6f 66 74 65 6e 2e 20 20  ry.  ** often.  
150a2 2a 2f 0a 20 20 69 66 28 20 28 70 42 74 72 65 65  */.  if( (pBtree
150a3 2d 3e 73 68 61 72 61 62 6c 65 3d 3d 30 29 0a 20  ->sharable==0). 
150a4 20 20 7c 7c 20 28 65 4c 6f 63 6b 54 79 70 65 3d    || (eLockType=
150a5 3d 52 45 41 44 5f 4c 4f 43 4b 20 26 26 20 28 70  =READ_LOCK && (p
150a6 42 74 72 65 65 2d 3e 64 62 2d 3e 66 6c 61 67 73  Btree->db->flags
150a7 20 26 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e   & SQLITE_ReadUn
150a8 63 6f 6d 6d 69 74 74 65 64 29 29 0a 20 20 20 7c  committed)).   |
150a9 7c 20 28 69 73 49 6e 64 65 78 20 26 26 20 28 21  | (isIndex && (!
150aa 70 53 63 68 65 6d 61 20 7c 7c 20 28 70 53 63 68  pSchema || (pSch
150ab 65 6d 61 2d 3e 66 6c 61 67 73 26 44 42 5f 53 63  ema->flags&DB_Sc
150ac 68 65 6d 61 4c 6f 61 64 65 64 29 3d 3d 30 20 29  hemaLoaded)==0 )
150ad 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ).  ){.    retur
150ae 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46  n 1;.  }..  /* F
150af 69 67 75 72 65 20 6f 75 74 20 74 68 65 20 72 6f  igure out the ro
150b0 6f 74 2d 70 61 67 65 20 74 68 61 74 20 74 68 65  ot-page that the
150b1 20 6c 6f 63 6b 20 73 68 6f 75 6c 64 20 62 65 20   lock should be 
150b2 68 65 6c 64 20 6f 6e 2e 20 46 6f 72 20 74 61 62  held on. For tab
150b3 6c 65 0a 20 20 2a 2a 20 62 2d 74 72 65 65 73 2c  le.  ** b-trees,
150b4 20 74 68 69 73 20 69 73 20 6a 75 73 74 20 74 68   this is just th
150b5 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74  e root page of t
150b6 68 65 20 62 2d 74 72 65 65 20 62 65 69 6e 67 20  he b-tree being 
150b7 72 65 61 64 20 6f 72 0a 20 20 2a 2a 20 77 72 69  read or.  ** wri
150b8 74 74 65 6e 2e 20 46 6f 72 20 69 6e 64 65 78 20  tten. For index 
150b9 62 2d 74 72 65 65 73 2c 20 69 74 20 69 73 20 74  b-trees, it is t
150ba 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20  he root page of 
150bb 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 0a 20  the associated. 
150bc 20 2a 2a 20 74 61 62 6c 65 2e 20 20 2a 2f 0a 20   ** table.  */. 
150bd 20 69 66 28 20 69 73 49 6e 64 65 78 20 29 7b 0a   if( isIndex ){.
150be 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b      HashElem *p;
150bf 0a 20 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74  .    for(p=sqlit
150c0 65 48 61 73 68 46 69 72 73 74 28 26 70 53 63 68  eHashFirst(&pSch
150c1 65 6d 61 2d 3e 69 64 78 48 61 73 68 29 3b 20 70  ema->idxHash); p
150c2 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65  ; p=sqliteHashNe
150c3 78 74 28 70 29 29 7b 0a 20 20 20 20 20 20 49 6e  xt(p)){.      In
150c4 64 65 78 20 2a 70 49 64 78 20 3d 20 28 49 6e 64  dex *pIdx = (Ind
150c5 65 78 20 2a 29 73 71 6c 69 74 65 48 61 73 68 44  ex *)sqliteHashD
150c6 61 74 61 28 70 29 3b 0a 20 20 20 20 20 20 69 66  ata(p);.      if
150c7 28 20 70 49 64 78 2d 3e 74 6e 75 6d 3d 3d 28 69  ( pIdx->tnum==(i
150c8 6e 74 29 69 52 6f 6f 74 20 29 7b 0a 20 20 20 20  nt)iRoot ){.    
150c9 20 20 20 20 69 54 61 62 20 3d 20 70 49 64 78 2d      iTab = pIdx-
150ca 3e 70 54 61 62 6c 65 2d 3e 74 6e 75 6d 3b 0a 20  >pTable->tnum;. 
150cb 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
150cc 65 6c 73 65 7b 0a 20 20 20 20 69 54 61 62 20 3d  else{.    iTab =
150cd 20 69 52 6f 6f 74 3b 0a 20 20 7d 0a 0a 20 20 2f   iRoot;.  }..  /
150ce 2a 20 53 65 61 72 63 68 20 66 6f 72 20 74 68 65  * Search for the
150cf 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 2e 20   required lock. 
150d0 45 69 74 68 65 72 20 61 20 77 72 69 74 65 2d 6c  Either a write-l
150d1 6f 63 6b 20 6f 6e 20 72 6f 6f 74 2d 70 61 67 65  ock on root-page
150d2 20 69 54 61 62 2c 20 61 20 0a 20 20 2a 2a 20 77   iTab, a .  ** w
150d3 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65  rite-lock on the
150d4 20 73 63 68 65 6d 61 20 74 61 62 6c 65 2c 20 6f   schema table, o
150d5 72 20 28 69 66 20 74 68 65 20 63 6c 69 65 6e 74  r (if the client
150d6 20 69 73 20 72 65 61 64 69 6e 67 29 20 61 0a 20   is reading) a. 
150d7 20 2a 2a 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e   ** read-lock on
150d8 20 69 54 61 62 20 77 69 6c 6c 20 73 75 66 66 69   iTab will suffi
150d9 63 65 2e 20 52 65 74 75 72 6e 20 31 20 69 66 20  ce. Return 1 if 
150da 61 6e 79 20 6f 66 20 74 68 65 73 65 20 61 72 65  any of these are
150db 20 66 6f 75 6e 64 2e 20 20 2a 2f 0a 20 20 66 6f   found.  */.  fo
150dc 72 28 70 4c 6f 63 6b 3d 70 42 74 72 65 65 2d 3e  r(pLock=pBtree->
150dd 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63  pBt->pLock; pLoc
150de 6b 3b 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e  k; pLock=pLock->
150df 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20  pNext){.    if( 
150e0 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70  pLock->pBtree==p
150e1 42 74 72 65 65 20 0a 20 20 20 20 20 26 26 20 28  Btree .     && (
150e2 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 3d 3d 69  pLock->iTable==i
150e3 54 61 62 20 7c 7c 20 28 70 4c 6f 63 6b 2d 3e 65  Tab || (pLock->e
150e4 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b  Lock==WRITE_LOCK
150e5 20 26 26 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c   && pLock->iTabl
150e6 65 3d 3d 31 29 29 0a 20 20 20 20 20 26 26 20 70  e==1)).     && p
150e7 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 3e 3d 65 4c 6f  Lock->eLock>=eLo
150e8 63 6b 54 79 70 65 20 0a 20 20 20 20 29 7b 0a 20  ckType .    ){. 
150e9 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
150ea 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46     }.  }..  /* F
150eb 61 69 6c 65 64 20 74 6f 20 66 69 6e 64 20 74 68  ailed to find th
150ec 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 2e  e required lock.
150ed 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a   */.  return 0;.
150ee 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
150ef 6e 63 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 75  nction is also u
150f0 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61  sed as part of a
150f1 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e  ssert() statemen
150f2 74 73 20 6f 6e 6c 79 2e 20 49 74 20 0a 2a 2a 20  ts only. It .** 
150f3 72 65 74 75 72 6e 73 20 74 72 75 65 20 69 66 20  returns true if 
150f4 74 68 65 72 65 20 65 78 69 73 74 20 6f 6e 65 20  there exist one 
150f5 6f 72 20 6d 6f 72 65 20 63 75 72 73 6f 72 73 20  or more cursors 
150f6 6f 70 65 6e 20 6f 6e 20 74 68 65 20 74 61 62 6c  open on the tabl
150f7 65 20 0a 2a 2a 20 77 69 74 68 20 72 6f 6f 74 20  e .** with root 
150f8 70 61 67 65 20 69 52 6f 6f 74 20 74 68 61 74 20  page iRoot that 
150f9 64 6f 20 6e 6f 74 20 62 65 6c 6f 6e 67 20 74 6f  do not belong to
150fa 20 65 69 74 68 65 72 20 63 6f 6e 6e 65 63 74 69   either connecti
150fb 6f 6e 20 70 42 74 72 65 65 20 0a 2a 2a 20 6f 72  on pBtree .** or
150fc 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 6f 6e 6e   some other conn
150fd 65 63 74 69 6f 6e 20 74 68 61 74 20 68 61 73 20  ection that has 
150fe 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69  the read-uncommi
150ff 74 74 65 64 20 66 6c 61 67 20 73 65 74 2e 0a 2a  tted flag set..*
15100 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65  *.** For example
15101 2c 20 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67  , before writing
15102 20 74 6f 20 70 61 67 65 20 69 52 6f 6f 74 3a 0a   to page iRoot:.
15103 2a 2a 0a 2a 2a 20 20 20 20 61 73 73 65 72 74 28  **.**    assert(
15104 20 21 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63   !hasReadConflic
15105 74 73 28 70 42 74 72 65 65 2c 20 69 52 6f 6f 74  ts(pBtree, iRoot
15106 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ) );.*/.static i
15107 6e 74 20 68 61 73 52 65 61 64 43 6f 6e 66 6c 69  nt hasReadConfli
15108 63 74 73 28 42 74 72 65 65 20 2a 70 42 74 72 65  cts(Btree *pBtre
15109 65 2c 20 50 67 6e 6f 20 69 52 6f 6f 74 29 7b 0a  e, Pgno iRoot){.
1510a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20    BtCursor *p;. 
1510b 20 66 6f 72 28 70 3d 70 42 74 72 65 65 2d 3e 70   for(p=pBtree->p
1510c 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20  Bt->pCursor; p; 
1510d 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  p=p->pNext){.   
1510e 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74   if( p->pgnoRoot
1510f 3d 3d 69 52 6f 6f 74 20 0a 20 20 20 20 20 26 26  ==iRoot .     &&
15110 20 70 2d 3e 70 42 74 72 65 65 21 3d 70 42 74 72   p->pBtree!=pBtr
15111 65 65 0a 20 20 20 20 20 26 26 20 30 3d 3d 28 70  ee.     && 0==(p
15112 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 66 6c  ->pBtree->db->fl
15113 61 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65 61  ags & SQLITE_Rea
15114 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 0a 20 20  dUncommitted).  
15115 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72    ){.      retur
15116 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 1;.    }.  }. 
15117 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e   return 0;.}.#en
15118 64 69 66 20 20 20 20 2f 2a 20 23 69 66 64 65 66  dif    /* #ifdef
15119 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f   SQLITE_DEBUG */
1511a 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74 6f  ../*.** Query to
1511b 20 73 65 65 20 69 66 20 62 74 72 65 65 20 68 61   see if btree ha
1511c 6e 64 6c 65 20 70 20 6d 61 79 20 6f 62 74 61 69  ndle p may obtai
1511d 6e 20 61 20 6c 6f 63 6b 20 6f 66 20 74 79 70 65  n a lock of type
1511e 20 65 4c 6f 63 6b 20 0a 2a 2a 20 28 52 45 41 44   eLock .** (READ
1511f 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c  _LOCK or WRITE_L
15120 4f 43 4b 29 20 6f 6e 20 74 68 65 20 74 61 62 6c  OCK) on the tabl
15121 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65  e with root-page
15122 20 69 54 61 62 2e 20 52 65 74 75 72 6e 0a 2a 2a   iTab. Return.**
15123 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 74 68   SQLITE_OK if th
15124 65 20 6c 6f 63 6b 20 6d 61 79 20 62 65 20 6f 62  e lock may be ob
15125 74 61 69 6e 65 64 20 28 62 79 20 63 61 6c 6c 69  tained (by calli
15126 6e 67 0a 2a 2a 20 73 65 74 53 68 61 72 65 64 43  ng.** setSharedC
15127 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 29  acheTableLock())
15128 2c 20 6f 72 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  , or SQLITE_LOCK
15129 45 44 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74  ED if not..*/.st
1512a 61 74 69 63 20 69 6e 74 20 71 75 65 72 79 53 68  atic int querySh
1512b 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f  aredCacheTableLo
1512c 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e  ck(Btree *p, Pgn
1512d 6f 20 69 54 61 62 2c 20 75 38 20 65 4c 6f 63 6b  o iTab, u8 eLock
1512e 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
1512f 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42  Bt = p->pBt;.  B
15130 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a 20  tLock *pIter;.. 
15131 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15132 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28  BtreeHoldsMutex(
15133 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  p) );.  assert( 
15134 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b  eLock==READ_LOCK
15135 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45   || eLock==WRITE
15136 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72  _LOCK );.  asser
15137 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a 20  t( p->db!=0 );. 
15138 20 61 73 73 65 72 74 28 20 21 28 70 2d 3e 64 62   assert( !(p->db
15139 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52  ->flags&SQLITE_R
1513a 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 7c  eadUncommitted)|
1513b 7c 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f  |eLock==WRITE_LO
1513c 43 4b 7c 7c 69 54 61 62 3d 3d 31 20 29 3b 0a 20  CK||iTab==1 );. 
1513d 20 0a 20 20 2f 2a 20 49 66 20 72 65 71 75 65 73   .  /* If reques
1513e 74 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63  ting a write-loc
1513f 6b 2c 20 74 68 65 6e 20 74 68 65 20 42 74 72 65  k, then the Btre
15140 65 20 6d 75 73 74 20 68 61 76 65 20 61 6e 20 6f  e must have an o
15141 70 65 6e 20 77 72 69 74 65 0a 20 20 2a 2a 20 74  pen write.  ** t
15142 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68  ransaction on th
15143 69 73 20 66 69 6c 65 2e 20 41 6e 64 2c 20 6f 62  is file. And, ob
15144 76 69 6f 75 73 6c 79 2c 20 66 6f 72 20 74 68 69  viously, for thi
15145 73 20 74 6f 20 62 65 20 73 6f 20 74 68 65 72 65  s to be so there
15146 20 0a 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 61   .  ** must be a
15147 6e 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61  n open write tra
15148 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20  nsaction on the 
15149 66 69 6c 65 20 69 74 73 65 6c 66 2e 0a 20 20 2a  file itself..  *
1514a 2f 0a 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63  /.  assert( eLoc
1514b 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20  k==READ_LOCK || 
1514c 28 70 3d 3d 70 42 74 2d 3e 70 57 72 69 74 65 72  (p==pBt->pWriter
1514d 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d   && p->inTrans==
1514e 54 52 41 4e 53 5f 57 52 49 54 45 29 20 29 3b 0a  TRANS_WRITE) );.
1514f 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d    assert( eLock=
15150 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 70 42  =READ_LOCK || pB
15151 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
15152 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b  ==TRANS_WRITE );
15153 0a 20 20 0a 20 20 2f 2a 20 54 68 69 73 20 69 73  .  .  /* This is
15154 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20   a no-op if the 
15155 73 68 61 72 65 64 2d 63 61 63 68 65 20 69 73 20  shared-cache is 
15156 6e 6f 74 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20  not enabled */. 
15157 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c   if( !p->sharabl
15158 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
15159 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
1515a 20 20 2f 2a 20 49 66 20 73 6f 6d 65 20 6f 74 68    /* If some oth
1515b 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  er connection is
1515c 20 68 6f 6c 64 69 6e 67 20 61 6e 20 65 78 63 6c   holding an excl
1515d 75 73 69 76 65 20 6c 6f 63 6b 2c 20 74 68 65 0a  usive lock, the.
1515e 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 20 6c    ** requested l
1515f 6f 63 6b 20 6d 61 79 20 6e 6f 74 20 62 65 20 6f  ock may not be o
15160 62 74 61 69 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20  btained..  */.  
15161 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74 65 72  if( pBt->pWriter
15162 21 3d 70 20 26 26 20 70 42 74 2d 3e 69 73 45 78  !=p && pBt->isEx
15163 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 20 20 73  clusive ){.    s
15164 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e  qlite3Connection
15165 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70  Blocked(p->db, p
15166 42 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64 62 29  Bt->pWriter->db)
15167 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
15168 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45  ITE_LOCKED_SHARE
15169 44 43 41 43 48 45 3b 0a 20 20 7d 0a 0a 20 20 66  DCACHE;.  }..  f
1516a 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c  or(pIter=pBt->pL
1516b 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65  ock; pIter; pIte
1516c 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b  r=pIter->pNext){
1516d 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 64  .    /* The cond
1516e 69 74 69 6f 6e 20 28 70 49 74 65 72 2d 3e 65 4c  ition (pIter->eL
1516f 6f 63 6b 21 3d 65 4c 6f 63 6b 29 20 69 6e 20 74  ock!=eLock) in t
15170 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 66 28  he following if(
15171 2e 2e 2e 29 20 0a 20 20 20 20 2a 2a 20 73 74 61  ...) .    ** sta
15172 74 65 6d 65 6e 74 20 69 73 20 61 20 73 69 6d 70  tement is a simp
15173 6c 69 66 69 63 61 74 69 6f 6e 20 6f 66 3a 0a 20  lification of:. 
15174 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 28     **.    **   (
15175 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43  eLock==WRITE_LOC
15176 4b 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63  K || pIter->eLoc
15177 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 29 0a 20  k==WRITE_LOCK). 
15178 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 73 69 6e     **.    ** sin
15179 63 65 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20  ce we know that 
1517a 69 66 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f  if eLock==WRITE_
1517b 4c 4f 43 4b 2c 20 74 68 65 6e 20 6e 6f 20 6f 74  LOCK, then no ot
1517c 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20  her connection. 
1517d 20 20 20 2a 2a 20 6d 61 79 20 68 6f 6c 64 20 61     ** may hold a
1517e 20 57 52 49 54 45 5f 4c 4f 43 4b 20 6f 6e 20 61   WRITE_LOCK on a
1517f 6e 79 20 74 61 62 6c 65 20 69 6e 20 74 68 69 73  ny table in this
15180 20 66 69 6c 65 20 28 73 69 6e 63 65 20 74 68 65   file (since the
15181 72 65 20 63 61 6e 0a 20 20 20 20 2a 2a 20 6f 6e  re can.    ** on
15182 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65 20 77  ly be a single w
15183 72 69 74 65 72 29 2e 0a 20 20 20 20 2a 2f 0a 20  riter)..    */. 
15184 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 72     assert( pIter
15185 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f  ->eLock==READ_LO
15186 43 4b 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f  CK || pIter->eLo
15187 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29  ck==WRITE_LOCK )
15188 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 4c  ;.    assert( eL
15189 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c  ock==READ_LOCK |
1518a 7c 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 3d  | pIter->pBtree=
1518b 3d 70 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f  =p || pIter->eLo
1518c 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a  ck==READ_LOCK);.
1518d 20 20 20 20 69 66 28 20 70 49 74 65 72 2d 3e 70      if( pIter->p
1518e 42 74 72 65 65 21 3d 70 20 26 26 20 70 49 74 65  Btree!=p && pIte
1518f 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20  r->iTable==iTab 
15190 26 26 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21  && pIter->eLock!
15191 3d 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20  =eLock ){.      
15192 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f  sqlite3Connectio
15193 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20  nBlocked(p->db, 
15194 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64  pIter->pBtree->d
15195 62 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 4c  b);.      if( eL
15196 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20  ock==WRITE_LOCK 
15197 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
15198 74 28 20 70 3d 3d 70 42 74 2d 3e 70 57 72 69 74  t( p==pBt->pWrit
15199 65 72 20 29 3b 0a 20 20 20 20 20 20 20 20 70 42  er );.        pB
1519a 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 31  t->isPending = 1
1519b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1519c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f  return SQLITE_LO
1519d 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 45  CKED_SHAREDCACHE
1519e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
1519f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
151a0 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c  }.#endif /* !SQL
151a1 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
151a2 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65  CACHE */..#ifnde
151a3 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
151a4 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a  ARED_CACHE./*.**
151a5 20 41 64 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74   Add a lock on t
151a6 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f  he table with ro
151a7 6f 74 2d 70 61 67 65 20 69 54 61 62 6c 65 20 74  ot-page iTable t
151a8 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72  o the shared-btr
151a9 65 65 20 75 73 65 64 0a 2a 2a 20 62 79 20 42 74  ee used.** by Bt
151aa 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 50 61  ree handle p. Pa
151ab 72 61 6d 65 74 65 72 20 65 4c 6f 63 6b 20 6d 75  rameter eLock mu
151ac 73 74 20 62 65 20 65 69 74 68 65 72 20 52 45 41  st be either REA
151ad 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20 57 52  D_LOCK or .** WR
151ae 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20  ITE_LOCK..**.** 
151af 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73  This function as
151b0 73 75 6d 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77  sumes the follow
151b1 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 61 29  ing:.**.**   (a)
151b2 20 54 68 65 20 73 70 65 63 69 66 69 65 64 20 62   The specified b
151b3 2d 74 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  -tree connection
151b4 20 68 61 6e 64 6c 65 20 69 73 20 63 6f 6e 6e 65   handle is conne
151b5 63 74 65 64 20 74 6f 20 61 20 73 68 61 72 61 62  cted to a sharab
151b6 6c 65 0a 2a 2a 20 20 20 20 20 20 20 62 2d 74 72  le.**       b-tr
151b7 65 65 20 64 61 74 61 62 61 73 65 20 28 6f 6e 65  ee database (one
151b8 20 77 69 74 68 20 74 68 65 20 42 74 53 68 61 72   with the BtShar
151b9 65 64 2e 73 68 61 72 61 62 6c 65 29 20 66 6c 61  ed.sharable) fla
151ba 67 20 73 65 74 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a  g set, and.**.**
151bb 20 20 20 28 62 29 20 4e 6f 20 6f 74 68 65 72 20     (b) No other 
151bc 62 2d 74 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f  b-tree connectio
151bd 6e 20 68 61 6e 64 6c 65 20 68 6f 6c 64 73 20 61  n handle holds a
151be 20 6c 6f 63 6b 20 74 68 61 74 20 63 6f 6e 66 6c   lock that confl
151bf 69 63 74 73 0a 2a 2a 20 20 20 20 20 20 20 77 69  icts.**       wi
151c0 74 68 20 74 68 65 20 72 65 71 75 65 73 74 65 64  th the requested
151c1 20 6c 6f 63 6b 20 28 69 2e 65 2e 20 71 75 65 72   lock (i.e. quer
151c2 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  ySharedCacheTabl
151c3 65 4c 6f 63 6b 28 29 20 68 61 73 0a 2a 2a 20 20  eLock() has.**  
151c4 20 20 20 20 20 61 6c 72 65 61 64 79 20 62 65 65       already bee
151c5 6e 20 63 61 6c 6c 65 64 20 61 6e 64 20 72 65 74  n called and ret
151c6 75 72 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4b 29  urned SQLITE_OK)
151c7 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f  ..**.** SQLITE_O
151c8 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  K is returned if
151c9 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 64 64   the lock is add
151ca 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e  ed successfully.
151cb 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 0a 2a   SQLITE_NOMEM .*
151cc 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  * is returned if
151cd 20 61 20 6d 61 6c 6c 6f 63 20 61 74 74 65 6d 70   a malloc attemp
151ce 74 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74  t fails..*/.stat
151cf 69 63 20 69 6e 74 20 73 65 74 53 68 61 72 65 64  ic int setShared
151d0 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 42  CacheTableLock(B
151d1 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54  tree *p, Pgno iT
151d2 61 62 6c 65 2c 20 75 38 20 65 4c 6f 63 6b 29 7b  able, u8 eLock){
151d3 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
151d4 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42 74 4c   = p->pBt;.  BtL
151d5 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 30 3b 0a  ock *pLock = 0;.
151d6 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b    BtLock *pIter;
151d7 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
151d8 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74  te3BtreeHoldsMut
151d9 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72  ex(p) );.  asser
151da 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c  t( eLock==READ_L
151db 4f 43 4b 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52  OCK || eLock==WR
151dc 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73  ITE_LOCK );.  as
151dd 73 65 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 29  sert( p->db!=0 )
151de 3b 0a 0a 20 20 2f 2a 20 41 20 63 6f 6e 6e 65 63  ;..  /* A connec
151df 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 72 65  tion with the re
151e0 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20 66  ad-uncommitted f
151e1 6c 61 67 20 73 65 74 20 77 69 6c 6c 20 6e 65 76  lag set will nev
151e2 65 72 20 74 72 79 20 74 6f 0a 20 20 2a 2a 20 6f  er try to.  ** o
151e3 62 74 61 69 6e 20 61 20 72 65 61 64 2d 6c 6f 63  btain a read-loc
151e4 6b 20 75 73 69 6e 67 20 74 68 69 73 20 66 75 6e  k using this fun
151e5 63 74 69 6f 6e 2e 20 54 68 65 20 6f 6e 6c 79 20  ction. The only 
151e6 72 65 61 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e  read-lock obtain
151e7 65 64 0a 20 20 2a 2a 20 62 79 20 61 20 63 6f 6e  ed.  ** by a con
151e8 6e 65 63 74 69 6f 6e 20 69 6e 20 72 65 61 64 2d  nection in read-
151e9 75 6e 63 6f 6d 6d 69 74 74 65 64 20 6d 6f 64 65  uncommitted mode
151ea 20 69 73 20 6f 6e 20 74 68 65 20 73 71 6c 69 74   is on the sqlit
151eb 65 5f 6d 61 73 74 65 72 20 0a 20 20 2a 2a 20 74  e_master .  ** t
151ec 61 62 6c 65 2c 20 61 6e 64 20 74 68 61 74 20 6c  able, and that l
151ed 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20  ock is obtained 
151ee 69 6e 20 42 74 72 65 65 42 65 67 69 6e 54 72 61  in BtreeBeginTra
151ef 6e 73 28 29 2e 20 20 2a 2f 0a 20 20 61 73 73 65  ns().  */.  asse
151f0 72 74 28 20 30 3d 3d 28 70 2d 3e 64 62 2d 3e 66  rt( 0==(p->db->f
151f1 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 61 64  lags&SQLITE_Read
151f2 55 6e 63 6f 6d 6d 69 74 74 65 64 29 20 7c 7c 20  Uncommitted) || 
151f3 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43  eLock==WRITE_LOC
151f4 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20  K );..  /* This 
151f5 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  function should 
151f6 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 6f  only be called o
151f7 6e 20 61 20 73 68 61 72 61 62 6c 65 20 62 2d 74  n a sharable b-t
151f8 72 65 65 20 61 66 74 65 72 20 69 74 20 0a 20 20  ree after it .  
151f9 2a 2a 20 68 61 73 20 62 65 65 6e 20 64 65 74 65  ** has been dete
151fa 72 6d 69 6e 65 64 20 74 68 61 74 20 6e 6f 20 6f  rmined that no o
151fb 74 68 65 72 20 62 2d 74 72 65 65 20 68 6f 6c 64  ther b-tree hold
151fc 73 20 61 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20  s a conflicting 
151fd 6c 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 73 73 65  lock.  */.  asse
151fe 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20  rt( p->sharable 
151ff 29 3b 0a 20 20 61 73 73 65 72 74 28 20 53 51 4c  );.  assert( SQL
15200 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53 68 61  ITE_OK==querySha
15201 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
15202 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20 65 4c 6f  k(p, iTable, eLo
15203 63 6b 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 72  ck) );..  /* Fir
15204 73 74 20 73 65 61 72 63 68 20 74 68 65 20 6c 69  st search the li
15205 73 74 20 66 6f 72 20 61 6e 20 65 78 69 73 74 69  st for an existi
15206 6e 67 20 6c 6f 63 6b 20 6f 6e 20 74 68 69 73 20  ng lock on this 
15207 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 66 6f 72 28  table. */.  for(
15208 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b  pIter=pBt->pLock
15209 3b 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70  ; pIter; pIter=p
1520a 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  Iter->pNext){.  
1520b 20 20 69 66 28 20 70 49 74 65 72 2d 3e 69 54 61    if( pIter->iTa
1520c 62 6c 65 3d 3d 69 54 61 62 6c 65 20 26 26 20 70  ble==iTable && p
1520d 49 74 65 72 2d 3e 70 42 74 72 65 65 3d 3d 70 20  Iter->pBtree==p 
1520e 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 20 3d  ){.      pLock =
1520f 20 70 49 74 65 72 3b 0a 20 20 20 20 20 20 62 72   pIter;.      br
15210 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  eak;.    }.  }..
15211 20 20 2f 2a 20 49 66 20 74 68 65 20 61 62 6f 76    /* If the abov
15212 65 20 73 65 61 72 63 68 20 64 69 64 20 6e 6f 74  e search did not
15213 20 66 69 6e 64 20 61 20 42 74 4c 6f 63 6b 20 73   find a BtLock s
15214 74 72 75 63 74 20 61 73 73 6f 63 69 61 74 69 6e  truct associatin
15215 67 20 42 74 72 65 65 20 70 0a 20 20 2a 2a 20 77  g Btree p.  ** w
15216 69 74 68 20 74 61 62 6c 65 20 69 54 61 62 6c 65  ith table iTable
15217 2c 20 61 6c 6c 6f 63 61 74 65 20 6f 6e 65 20 61  , allocate one a
15218 6e 64 20 6c 69 6e 6b 20 69 74 20 69 6e 74 6f 20  nd link it into 
15219 74 68 65 20 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20  the list..  */. 
1521a 20 69 66 28 20 21 70 4c 6f 63 6b 20 29 7b 0a 20   if( !pLock ){. 
1521b 20 20 20 70 4c 6f 63 6b 20 3d 20 28 42 74 4c 6f     pLock = (BtLo
1521c 63 6b 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c  ck *)sqlite3Mall
1521d 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 42 74  ocZero(sizeof(Bt
1521e 4c 6f 63 6b 29 29 3b 0a 20 20 20 20 69 66 28 20  Lock));.    if( 
1521f 21 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20  !pLock ){.      
15220 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
15221 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  MEM;.    }.    p
15222 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 20 3d 20 69  Lock->iTable = i
15223 54 61 62 6c 65 3b 0a 20 20 20 20 70 4c 6f 63 6b  Table;.    pLock
15224 2d 3e 70 42 74 72 65 65 20 3d 20 70 3b 0a 20 20  ->pBtree = p;.  
15225 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d    pLock->pNext =
15226 20 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 20   pBt->pLock;.   
15227 20 70 42 74 2d 3e 70 4c 6f 63 6b 20 3d 20 70 4c   pBt->pLock = pL
15228 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  ock;.  }..  /* S
15229 65 74 20 74 68 65 20 42 74 4c 6f 63 6b 2e 65 4c  et the BtLock.eL
1522a 6f 63 6b 20 76 61 72 69 61 62 6c 65 20 74 6f 20  ock variable to 
1522b 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 74  the maximum of t
1522c 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 6b 0a  he current lock.
1522d 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 72 65 71    ** and the req
1522e 75 65 73 74 65 64 20 6c 6f 63 6b 2e 20 54 68 69  uested lock. Thi
1522f 73 20 6d 65 61 6e 73 20 69 66 20 61 20 77 72 69  s means if a wri
15230 74 65 2d 6c 6f 63 6b 20 77 61 73 20 61 6c 72 65  te-lock was alre
15231 61 64 79 20 68 65 6c 64 0a 20 20 2a 2a 20 61 6e  ady held.  ** an
15232 64 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 72 65  d a read-lock re
15233 71 75 65 73 74 65 64 2c 20 77 65 20 64 6f 6e 27  quested, we don'
15234 74 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 64 6f  t incorrectly do
15235 77 6e 67 72 61 64 65 20 74 68 65 20 6c 6f 63 6b  wngrade the lock
15236 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
15237 20 57 52 49 54 45 5f 4c 4f 43 4b 3e 52 45 41 44   WRITE_LOCK>READ
15238 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20 65  _LOCK );.  if( e
15239 4c 6f 63 6b 3e 70 4c 6f 63 6b 2d 3e 65 4c 6f 63  Lock>pLock->eLoc
1523a 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e  k ){.    pLock->
1523b 65 4c 6f 63 6b 20 3d 20 65 4c 6f 63 6b 3b 0a 20  eLock = eLock;. 
1523c 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c   }..  return SQL
1523d 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66  ITE_OK;.}.#endif
1523e 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54   /* !SQLITE_OMIT
1523f 5f 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f  _SHARED_CACHE */
15240 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
15241 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
15242 48 45 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65  HE./*.** Release
15243 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 20 6c   all the table l
15244 6f 63 6b 73 20 28 6c 6f 63 6b 73 20 6f 62 74 61  ocks (locks obta
15245 69 6e 65 64 20 76 69 61 20 63 61 6c 6c 73 20 74  ined via calls t
15246 6f 0a 2a 2a 20 74 68 65 20 73 65 74 53 68 61 72  o.** the setShar
15247 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
15248 28 29 20 70 72 6f 63 65 64 75 72 65 29 20 68 65  () procedure) he
15249 6c 64 20 62 79 20 42 74 72 65 65 20 68 61 6e 64  ld by Btree hand
1524a 6c 65 20 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  le p..**.** This
1524b 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65   function assume
1524c 73 20 74 68 61 74 20 68 61 6e 64 6c 65 20 70 20  s that handle p 
1524d 68 61 73 20 61 6e 20 6f 70 65 6e 20 72 65 61 64  has an open read
1524e 20 6f 72 20 77 72 69 74 65 20 0a 2a 2a 20 74 72   or write .** tr
1524f 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20 69 74  ansaction. If it
15250 20 64 6f 65 73 20 6e 6f 74 2c 20 74 68 65 6e 20   does not, then 
15251 74 68 65 20 42 74 53 68 61 72 65 64 2e 69 73 50  the BtShared.isP
15252 65 6e 64 69 6e 67 20 76 61 72 69 61 62 6c 65 0a  ending variable.
15253 2a 2a 20 6d 61 79 20 62 65 20 69 6e 63 6f 72 72  ** may be incorr
15254 65 63 74 6c 79 20 63 6c 65 61 72 65 64 2e 0a 2a  ectly cleared..*
15255 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c  /.static void cl
15256 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63 68  earAllSharedCach
15257 65 54 61 62 6c 65 4c 6f 63 6b 73 28 42 74 72 65  eTableLocks(Btre
15258 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65  e *p){.  BtShare
15259 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
1525a 0a 20 20 42 74 4c 6f 63 6b 20 2a 2a 70 70 49 74  .  BtLock **ppIt
1525b 65 72 20 3d 20 26 70 42 74 2d 3e 70 4c 6f 63 6b  er = &pBt->pLock
1525c 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
1525d 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
1525e 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65  tex(p) );.  asse
1525f 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20  rt( p->sharable 
15260 7c 7c 20 30 3d 3d 2a 70 70 49 74 65 72 20 29 3b  || 0==*ppIter );
15261 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e  .  assert( p->in
15262 54 72 61 6e 73 3e 30 20 29 3b 0a 0a 20 20 77 68  Trans>0 );..  wh
15263 69 6c 65 28 20 2a 70 70 49 74 65 72 20 29 7b 0a  ile( *ppIter ){.
15264 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63      BtLock *pLoc
15265 6b 20 3d 20 2a 70 70 49 74 65 72 3b 0a 20 20 20  k = *ppIter;.   
15266 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 73   assert( pBt->is
15267 45 78 63 6c 75 73 69 76 65 3d 3d 30 20 7c 7c 20  Exclusive==0 || 
15268 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 4c  pBt->pWriter==pL
15269 6f 63 6b 2d 3e 70 42 74 72 65 65 20 29 3b 0a 20  ock->pBtree );. 
1526a 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b     assert( pLock
1526b 2d 3e 70 42 74 72 65 65 2d 3e 69 6e 54 72 61 6e  ->pBtree->inTran
1526c 73 3e 3d 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20  s>=pLock->eLock 
1526d 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b  );.    if( pLock
1526e 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20  ->pBtree==p ){. 
1526f 20 20 20 20 20 2a 70 70 49 74 65 72 20 3d 20 70       *ppIter = p
15270 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  Lock->pNext;.   
15271 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b     assert( pLock
15272 2d 3e 69 54 61 62 6c 65 21 3d 31 20 7c 7c 20 70  ->iTable!=1 || p
15273 4c 6f 63 6b 3d 3d 26 70 2d 3e 6c 6f 63 6b 20 29  Lock==&p->lock )
15274 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63  ;.      if( pLoc
15275 6b 2d 3e 69 54 61 62 6c 65 21 3d 31 20 29 7b 0a  k->iTable!=1 ){.
15276 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
15277 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20  free(pLock);.   
15278 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
15279 20 20 20 20 20 20 70 70 49 74 65 72 20 3d 20 26        ppIter = &
1527a 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20  pLock->pNext;.  
1527b 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72    }.  }..  asser
1527c 74 28 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e  t( pBt->isPendin
1527d 67 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 57 72  g==0 || pBt->pWr
1527e 69 74 65 72 20 29 3b 0a 20 20 69 66 28 20 70 42  iter );.  if( pB
1527f 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 20 29 7b  t->pWriter==p ){
15280 0a 20 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65  .    pBt->pWrite
15281 72 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e  r = 0;.    pBt->
15282 69 73 45 78 63 6c 75 73 69 76 65 20 3d 20 30 3b  isExclusive = 0;
15283 0a 20 20 20 20 70 42 74 2d 3e 69 73 50 65 6e 64  .    pBt->isPend
15284 69 6e 67 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  ing = 0;.  }else
15285 20 69 66 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73   if( pBt->nTrans
15286 61 63 74 69 6f 6e 3d 3d 32 20 29 7b 0a 20 20 20  action==2 ){.   
15287 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f   /* This functio
15288 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  n is called when
15289 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 20 69 73   connection p is
1528a 20 63 6f 6e 63 6c 75 64 69 6e 67 20 69 74 73 20   concluding its 
1528b 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74  .    ** transact
1528c 69 6f 6e 2e 20 49 66 20 74 68 65 72 65 20 63 75  ion. If there cu
1528d 72 72 65 6e 74 6c 79 20 65 78 69 73 74 73 20 61  rrently exists a
1528e 20 77 72 69 74 65 72 2c 20 61 6e 64 20 70 20 69   writer, and p i
1528f 73 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 74 68 61  s not.    ** tha
15290 74 20 77 72 69 74 65 72 2c 20 74 68 65 6e 20 74  t writer, then t
15291 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63  he number of loc
15292 6b 73 20 68 65 6c 64 20 62 79 20 63 6f 6e 6e 65  ks held by conne
15293 63 74 69 6f 6e 73 20 6f 74 68 65 72 0a 20 20 20  ctions other.   
15294 20 2a 2a 20 74 68 61 6e 20 74 68 65 20 77 72 69   ** than the wri
15295 74 65 72 20 6d 75 73 74 20 62 65 20 61 62 6f 75  ter must be abou
15296 74 20 74 6f 20 64 72 6f 70 20 74 6f 20 7a 65 72  t to drop to zer
15297 6f 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 0a  o. In this case.
15298 20 20 20 20 2a 2a 20 73 65 74 20 74 68 65 20 69      ** set the i
15299 73 50 65 6e 64 69 6e 67 20 66 6c 61 67 20 74 6f  sPending flag to
1529a 20 30 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a   0..    **.    *
1529b 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  * If there is no
1529c 74 20 63 75 72 72 65 6e 74 6c 79 20 61 20 77 72  t currently a wr
1529d 69 74 65 72 2c 20 74 68 65 6e 20 42 74 53 68 61  iter, then BtSha
1529e 72 65 64 2e 69 73 50 65 6e 64 69 6e 67 20 6d 75  red.isPending mu
1529f 73 74 0a 20 20 20 20 2a 2a 20 62 65 20 7a 65 72  st.    ** be zer
152a0 6f 20 61 6c 72 65 61 64 79 2e 20 53 6f 20 74 68  o already. So th
152a1 69 73 20 6e 65 78 74 20 6c 69 6e 65 20 69 73 20  is next line is 
152a2 68 61 72 6d 6c 65 73 73 20 69 6e 20 74 68 61 74  harmless in that
152a3 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20   case..    */.  
152a4 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67    pBt->isPending
152a5 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   = 0;.  }.}../*.
152a6 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
152a7 20 63 68 61 6e 67 65 73 20 61 6c 6c 20 77 72 69   changes all wri
152a8 74 65 2d 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79  te-locks held by
152a9 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 20 74 6f   connection p to
152aa 20 72 65 61 64 2d 6c 6f 63 6b 73 2e 0a 2a 2f 0a   read-locks..*/.
152ab 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 77 6e  static void down
152ac 67 72 61 64 65 41 6c 6c 53 68 61 72 65 64 43 61  gradeAllSharedCa
152ad 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 42 74  cheTableLocks(Bt
152ae 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61  ree *p){.  BtSha
152af 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
152b0 74 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 57  t;.  if( pBt->pW
152b1 72 69 74 65 72 3d 3d 70 20 29 7b 0a 20 20 20 20  riter==p ){.    
152b2 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 0a 20  BtLock *pLock;. 
152b3 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65 72 20     pBt->pWriter 
152b4 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 73  = 0;.    pBt->is
152b5 45 78 63 6c 75 73 69 76 65 20 3d 20 30 3b 0a 20  Exclusive = 0;. 
152b6 20 20 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e     pBt->isPendin
152b7 67 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70  g = 0;.    for(p
152b8 4c 6f 63 6b 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b  Lock=pBt->pLock;
152b9 20 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3d 70 4c   pLock; pLock=pL
152ba 6f 63 6b 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  ock->pNext){.   
152bb 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b     assert( pLock
152bc 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f  ->eLock==READ_LO
152bd 43 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 70 42 74  CK || pLock->pBt
152be 72 65 65 3d 3d 70 20 29 3b 0a 20 20 20 20 20 20  ree==p );.      
152bf 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 52  pLock->eLock = R
152c0 45 41 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a  EAD_LOCK;.    }.
152c1 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a    }.}..#endif /*
152c2 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
152c3 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 73 74  RED_CACHE */..st
152c4 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73  atic void releas
152c5 65 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70  ePage(MemPage *p
152c6 50 61 67 65 29 3b 20 20 2f 2a 20 46 6f 72 77 61  Page);  /* Forwa
152c7 72 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a  rd reference */.
152c8 0a 2f 2a 0a 2a 2a 20 56 65 72 69 66 79 20 74 68  ./*.** Verify th
152c9 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 68 6f  at the cursor ho
152ca 6c 64 73 20 61 20 6d 75 74 65 78 20 6f 6e 20 74  lds a mutex on t
152cb 68 65 20 42 74 53 68 61 72 65 64 0a 2a 2f 0a 23  he BtShared.*/.#
152cc 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74  ifndef NDEBUG.st
152cd 61 74 69 63 20 69 6e 74 20 63 75 72 73 6f 72 48  atic int cursorH
152ce 6f 6c 64 73 4d 75 74 65 78 28 42 74 43 75 72 73  oldsMutex(BtCurs
152cf 6f 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  or *p){.  return
152d0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
152d1 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65  eld(p->pBt->mute
152d2 78 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23  x);.}.#endif...#
152d3 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
152d4 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a  IT_INCRBLOB./*.*
152d5 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74 68 65  * Invalidate the
152d6 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c   overflow page-l
152d7 69 73 74 20 63 61 63 68 65 20 66 6f 72 20 63 75  ist cache for cu
152d8 72 73 6f 72 20 70 43 75 72 2c 20 69 66 20 61 6e  rsor pCur, if an
152d9 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  y..*/.static voi
152da 64 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72  d invalidateOver
152db 66 6c 6f 77 43 61 63 68 65 28 42 74 43 75 72 73  flowCache(BtCurs
152dc 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73  or *pCur){.  ass
152dd 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
152de 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
152df 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
152e0 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 29 3b 0a  ur->aOverflow);.
152e1 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f    pCur->aOverflo
152e2 77 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  w = 0;.}../*.** 
152e3 49 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 6f  Invalidate the o
152e4 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73  verflow page-lis
152e5 74 20 63 61 63 68 65 20 66 6f 72 20 61 6c 6c 20  t cache for all 
152e6 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a 2a  cursors opened.*
152e7 2a 20 6f 6e 20 74 68 65 20 73 68 61 72 65 64 20  * on the shared 
152e8 62 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20  btree structure 
152e9 70 42 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  pBt..*/.static v
152ea 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 41 6c  oid invalidateAl
152eb 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28 42  lOverflowCache(B
152ec 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20  tShared *pBt){. 
152ed 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20   BtCursor *p;.  
152ee 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
152ef 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
152f0 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28  mutex) );.  for(
152f1 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20  p=pBt->pCursor; 
152f2 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a  p; p=p->pNext){.
152f3 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 4f 76      invalidateOv
152f4 65 72 66 6c 6f 77 43 61 63 68 65 28 70 29 3b 0a  erflowCache(p);.
152f5 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
152f6 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
152f7 6c 6c 65 64 20 62 65 66 6f 72 65 20 6d 6f 64 69  lled before modi
152f8 66 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e  fying the conten
152f9 74 73 20 6f 66 20 61 20 74 61 62 6c 65 0a 2a 2a  ts of a table.**
152fa 20 62 2d 74 72 65 65 20 74 6f 20 69 6e 76 61 6c   b-tree to inval
152fb 69 64 61 74 65 20 61 6e 79 20 69 6e 63 72 62 6c  idate any incrbl
152fc 6f 62 20 63 75 72 73 6f 72 73 20 74 68 61 74 20  ob cursors that 
152fd 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65 0a  are open on the.
152fe 2a 2a 20 72 6f 77 20 6f 72 20 6f 6e 65 20 6f 66  ** row or one of
152ff 20 74 68 65 20 72 6f 77 73 20 62 65 69 6e 67 20   the rows being 
15300 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20  modified..**.** 
15301 49 66 20 61 72 67 75 6d 65 6e 74 20 69 73 43 6c  If argument isCl
15302 65 61 72 54 61 62 6c 65 20 69 73 20 74 72 75 65  earTable is true
15303 2c 20 74 68 65 6e 20 74 68 65 20 65 6e 74 69 72  , then the entir
15304 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
15305 65 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 61 62  e.** table is ab
15306 6f 75 74 20 74 6f 20 62 65 20 64 65 6c 65 74 65  out to be delete
15307 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  d. In this case 
15308 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 69  invalidate all i
15309 6e 63 72 62 6c 6f 62 0a 2a 2a 20 63 75 72 73 6f  ncrblob.** curso
1530a 72 73 20 6f 70 65 6e 20 6f 6e 20 61 6e 79 20 72  rs open on any r
1530b 6f 77 20 77 69 74 68 69 6e 20 74 68 65 20 74 61  ow within the ta
1530c 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61  ble with root-pa
1530d 67 65 20 70 67 6e 6f 52 6f 6f 74 2e 0a 2a 2a 0a  ge pgnoRoot..**.
1530e 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66  ** Otherwise, if
1530f 20 61 72 67 75 6d 65 6e 74 20 69 73 43 6c 65 61   argument isClea
15310 72 54 61 62 6c 65 20 69 73 20 66 61 6c 73 65 2c  rTable is false,
15311 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20 77 69   then the row wi
15312 74 68 0a 2a 2a 20 72 6f 77 69 64 20 69 52 6f 77  th.** rowid iRow
15313 20 69 73 20 62 65 69 6e 67 20 72 65 70 6c 61 63   is being replac
15314 65 64 20 6f 72 20 64 65 6c 65 74 65 64 2e 20 49  ed or deleted. I
15315 6e 20 74 68 69 73 20 63 61 73 65 20 69 6e 76 61  n this case inva
15316 6c 69 64 61 74 65 0a 2a 2a 20 6f 6e 6c 79 20 74  lidate.** only t
15317 68 6f 73 65 20 69 6e 63 72 62 6c 6f 62 20 63 75  hose incrblob cu
15318 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68  rsors open on th
15319 69 73 20 73 70 65 63 69 66 69 63 20 72 6f 77 2e  is specific row.
1531a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1531b 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62 6c  invalidateIncrbl
1531c 6f 62 43 75 72 73 6f 72 73 28 0a 20 20 42 74 72  obCursors(.  Btr
1531d 65 65 20 2a 70 42 74 72 65 65 2c 20 20 20 20 20  ee *pBtree,     
1531e 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
1531f 62 61 73 65 20 66 69 6c 65 20 74 6f 20 63 68 65  base file to che
15320 63 6b 20 2a 2f 0a 20 20 69 36 34 20 69 52 6f 77  ck */.  i64 iRow
15321 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
15322 2f 2a 20 54 68 65 20 72 6f 77 69 64 20 74 68 61  /* The rowid tha
15323 74 20 6d 69 67 68 74 20 62 65 20 63 68 61 6e 67  t might be chang
15324 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43  ing */.  int isC
15325 6c 65 61 72 54 61 62 6c 65 20 20 20 20 20 20 20  learTable       
15326 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6c 6c 20   /* True if all 
15327 72 6f 77 73 20 61 72 65 20 62 65 69 6e 67 20 64  rows are being d
15328 65 6c 65 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 42  eleted */.){.  B
15329 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 42 74  tCursor *p;.  Bt
1532a 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 42  Shared *pBt = pB
1532b 74 72 65 65 2d 3e 70 42 74 3b 0a 20 20 61 73 73  tree->pBt;.  ass
1532c 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1532d 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 42 74 72  eHoldsMutex(pBtr
1532e 65 65 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70  ee) );.  for(p=p
1532f 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20  Bt->pCursor; p; 
15330 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  p=p->pNext){.   
15331 20 69 66 28 20 70 2d 3e 69 73 49 6e 63 72 62 6c   if( p->isIncrbl
15332 6f 62 48 61 6e 64 6c 65 20 26 26 20 28 69 73 43  obHandle && (isC
15333 6c 65 61 72 54 61 62 6c 65 20 7c 7c 20 70 2d 3e  learTable || p->
15334 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 52 6f 77 29  info.nKey==iRow)
15335 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 65 53 74   ){.      p->eSt
15336 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56  ate = CURSOR_INV
15337 41 4c 49 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ALID;.    }.  }.
15338 7d 0a 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69  }..#else.  #defi
15339 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65  ne invalidateOve
1533a 72 66 6c 6f 77 43 61 63 68 65 28 78 29 0a 20 20  rflowCache(x).  
1533b 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 61  #define invalida
1533c 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63  teAllOverflowCac
1533d 68 65 28 78 29 0a 20 20 23 64 65 66 69 6e 65 20  he(x).  #define 
1533e 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62 6c  invalidateIncrbl
1533f 6f 62 43 75 72 73 6f 72 73 28 78 2c 79 2c 7a 29  obCursors(x,y,z)
15340 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53  .#endif../*.** S
15341 65 74 20 62 69 74 20 70 67 6e 6f 20 6f 66 20 74  et bit pgno of t
15342 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 73  he BtShared.pHas
15343 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e 20  Content bitvec. 
15344 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 0a  This is called .
15345 2a 2a 20 77 68 65 6e 20 61 20 70 61 67 65 20 74  ** when a page t
15346 68 61 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63  hat previously c
15347 6f 6e 74 61 69 6e 65 64 20 64 61 74 61 20 62 65  ontained data be
15348 63 6f 6d 65 73 20 61 20 66 72 65 65 2d 6c 69 73  comes a free-lis
15349 74 20 6c 65 61 66 20 0a 2a 2a 20 70 61 67 65 2e  t leaf .** page.
1534a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 42 74 53 68 61  .**.** The BtSha
1534b 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20  red.pHasContent 
1534c 62 69 74 76 65 63 20 65 78 69 73 74 73 20 74 6f  bitvec exists to
1534d 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 61 6e 20   work around an 
1534e 6f 62 73 63 75 72 65 0a 2a 2a 20 62 75 67 20 63  obscure.** bug c
1534f 61 75 73 65 64 20 62 79 20 74 68 65 20 69 6e 74  aused by the int
15350 65 72 61 63 74 69 6f 6e 20 6f 66 20 74 77 6f 20  eraction of two 
15351 75 73 65 66 75 6c 20 49 4f 20 6f 70 74 69 6d 69  useful IO optimi
15352 7a 61 74 69 6f 6e 73 20 73 75 72 72 6f 75 6e 64  zations surround
15353 69 6e 67 0a 2a 2a 20 66 72 65 65 2d 6c 69 73 74  ing.** free-list
15354 20 6c 65 61 66 20 70 61 67 65 73 3a 0a 2a 2a 0a   leaf pages:.**.
15355 2a 2a 20 20 20 31 29 20 57 68 65 6e 20 61 6c 6c  **   1) When all
15356 20 64 61 74 61 20 69 73 20 64 65 6c 65 74 65 64   data is deleted
15357 20 66 72 6f 6d 20 61 20 70 61 67 65 20 61 6e 64   from a page and
15358 20 74 68 65 20 70 61 67 65 20 62 65 63 6f 6d 65   the page become
15359 73 0a 2a 2a 20 20 20 20 20 20 61 20 66 72 65 65  s.**      a free
1535a 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c  -list leaf page,
1535b 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
1535c 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
1535d 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20  database.**     
1535e 20 28 61 73 20 66 72 65 65 2d 6c 69 73 74 20 6c   (as free-list l
1535f 65 61 66 20 70 61 67 65 73 20 63 6f 6e 74 61 69  eaf pages contai
15360 6e 20 6e 6f 20 6d 65 61 6e 69 6e 67 66 75 6c 20  n no meaningful 
15361 64 61 74 61 29 2e 20 53 6f 6d 65 74 69 6d 65 73  data). Sometimes
15362 0a 2a 2a 20 20 20 20 20 20 73 75 63 68 20 61 20  .**      such a 
15363 70 61 67 65 20 69 73 20 6e 6f 74 20 65 76 65 6e  page is not even
15364 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 28 61 73 20   journalled (as 
15365 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6d  it will not be m
15366 6f 64 69 66 69 65 64 2c 0a 2a 2a 20 20 20 20 20  odified,.**     
15367 20 77 68 79 20 62 6f 74 68 65 72 20 6a 6f 75 72   why bother jour
15368 6e 61 6c 6c 69 6e 67 20 69 74 3f 29 2e 0a 2a 2a  nalling it?)..**
15369 0a 2a 2a 20 20 20 32 29 20 57 68 65 6e 20 61 20  .**   2) When a 
1536a 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70  free-list leaf p
1536b 61 67 65 20 69 73 20 72 65 75 73 65 64 2c 20 69  age is reused, i
1536c 74 73 20 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f  ts content is no
1536d 74 20 72 65 61 64 0a 2a 2a 20 20 20 20 20 20 66  t read.**      f
1536e 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
1536f 20 6f 72 20 77 72 69 74 74 65 6e 20 74 6f 20 74   or written to t
15370 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
15371 28 77 68 79 20 73 68 6f 75 6c 64 20 69 74 0a 2a  (why should it.*
15372 2a 20 20 20 20 20 20 62 65 2c 20 69 66 20 69 74  *      be, if it
15373 20 69 73 20 6e 6f 74 20 61 74 20 61 6c 6c 20 6d   is not at all m
15374 65 61 6e 69 6e 67 66 75 6c 3f 29 2e 0a 2a 2a 0a  eaningful?)..**.
15375 2a 2a 20 42 79 20 74 68 65 6d 73 65 6c 76 65 73  ** By themselves
15376 2c 20 74 68 65 73 65 20 6f 70 74 69 6d 69 7a 61  , these optimiza
15377 74 69 6f 6e 73 20 77 6f 72 6b 20 66 69 6e 65 20  tions work fine 
15378 61 6e 64 20 70 72 6f 76 69 64 65 20 61 20 68 61  and provide a ha
15379 6e 64 79 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e  ndy.** performan
1537a 63 65 20 62 6f 6f 73 74 20 74 6f 20 62 75 6c 6b  ce boost to bulk
1537b 20 64 65 6c 65 74 65 20 6f 72 20 69 6e 73 65 72   delete or inser
1537c 74 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 48 6f  t operations. Ho
1537d 77 65 76 65 72 2c 20 69 66 0a 2a 2a 20 61 20 70  wever, if.** a p
1537e 61 67 65 20 69 73 20 6d 6f 76 65 64 20 74 6f 20  age is moved to 
1537f 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 61 6e  the free-list an
15380 64 20 74 68 65 6e 20 72 65 75 73 65 64 20 77 69  d then reused wi
15381 74 68 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a  thin the same.**
15382 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20   transaction, a 
15383 70 72 6f 62 6c 65 6d 20 63 6f 6d 65 73 20 75 70  problem comes up
15384 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 69 73  . If the page is
15385 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20   not journalled 
15386 77 68 65 6e 0a 2a 2a 20 69 74 20 69 73 20 6d 6f  when.** it is mo
15387 76 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 2d  ved to the free-
15388 6c 69 73 74 20 61 6e 64 20 69 74 20 69 73 20 61  list and it is a
15389 6c 73 6f 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c  lso not journall
1538a 65 64 20 77 68 65 6e 20 69 74 0a 2a 2a 20 69 73  ed when it.** is
1538b 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20   extracted from 
1538c 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 61 6e  the free-list an
1538d 64 20 72 65 75 73 65 64 2c 20 74 68 65 6e 20 74  d reused, then t
1538e 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61  he original data
1538f 0a 2a 2a 20 6d 61 79 20 62 65 20 6c 6f 73 74 2e  .** may be lost.
15390 20 49 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66   In the event of
15391 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 69 74 20   a rollback, it 
15392 6d 61 79 20 6e 6f 74 20 62 65 20 70 6f 73 73 69  may not be possi
15393 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 73 74 6f 72  ble.** to restor
15394 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  e the database t
15395 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 63  o its original c
15396 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2e 0a 2a 2a  onfiguration..**
15397 0a 2a 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f 6e  .** The solution
15398 20 69 73 20 74 68 65 20 42 74 53 68 61 72 65 64   is the BtShared
15399 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74  .pHasContent bit
1539a 76 65 63 2e 20 57 68 65 6e 65 76 65 72 20 61 20  vec. Whenever a 
1539b 70 61 67 65 20 69 73 20 0a 2a 2a 20 6d 6f 76 65  page is .** move
1539c 64 20 74 6f 20 62 65 63 6f 6d 65 20 61 20 66 72  d to become a fr
1539d 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67  ee-list leaf pag
1539e 65 2c 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  e, the correspon
1539f 64 69 6e 67 20 62 69 74 20 69 73 0a 2a 2a 20 73  ding bit is.** s
153a0 65 74 20 69 6e 20 74 68 65 20 62 69 74 76 65 63  et in the bitvec
153a1 2e 20 57 68 65 6e 65 76 65 72 20 61 20 6c 65 61  . Whenever a lea
153a2 66 20 70 61 67 65 20 69 73 20 65 78 74 72 61 63  f page is extrac
153a3 74 65 64 20 66 72 6f 6d 20 74 68 65 20 66 72 65  ted from the fre
153a4 65 2d 6c 69 73 74 2c 0a 2a 2a 20 6f 70 74 69 6d  e-list,.** optim
153a5 69 7a 61 74 69 6f 6e 20 32 20 61 62 6f 76 65 20  ization 2 above 
153a6 69 73 20 6f 6d 6d 69 74 74 65 64 20 69 66 20 74  is ommitted if t
153a7 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
153a8 20 62 69 74 20 69 73 20 61 6c 72 65 61 64 79 0a   bit is already.
153a9 2a 2a 20 73 65 74 20 69 6e 20 42 74 53 68 61 72  ** set in BtShar
153aa 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 2e 20  ed.pHasContent. 
153ab 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  The contents of 
153ac 74 68 65 20 62 69 74 76 65 63 20 61 72 65 20 63  the bitvec are c
153ad 6c 65 61 72 65 64 0a 2a 2a 20 61 74 20 74 68 65  leared.** at the
153ae 20 65 6e 64 20 6f 66 20 65 76 65 72 79 20 74 72   end of every tr
153af 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  ansaction..*/.st
153b0 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 53 65  atic int btreeSe
153b1 74 48 61 73 43 6f 6e 74 65 6e 74 28 42 74 53 68  tHasContent(BtSh
153b2 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20  ared *pBt, Pgno 
153b3 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 72 63 20  pgno){.  int rc 
153b4 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
153b5 66 28 20 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e  f( !pBt->pHasCon
153b6 74 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20  tent ){.    int 
153b7 6e 50 61 67 65 20 3d 20 31 30 30 3b 0a 20 20 20  nPage = 100;.   
153b8 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
153b9 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67  ecount(pBt->pPag
153ba 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 20  er, &nPage);.   
153bb 20 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 50 61   /* If sqlite3Pa
153bc 67 65 72 50 61 67 65 63 6f 75 6e 74 28 29 20 66  gerPagecount() f
153bd 61 69 6c 73 20 74 68 65 72 65 20 69 73 20 6e 6f  ails there is no
153be 20 68 61 72 6d 20 62 65 63 61 75 73 65 20 74 68   harm because th
153bf 65 0a 20 20 20 20 2a 2a 20 6e 50 61 67 65 20 76  e.    ** nPage v
153c0 61 72 69 61 62 6c 65 20 69 73 20 75 6e 63 68 61  ariable is uncha
153c1 6e 67 65 64 20 66 72 6f 6d 20 69 74 73 20 64 65  nged from its de
153c2 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 31  fault value of 1
153c3 30 30 20 2a 2f 0a 20 20 20 20 70 42 74 2d 3e 70  00 */.    pBt->p
153c4 48 61 73 43 6f 6e 74 65 6e 74 20 3d 20 73 71 6c  HasContent = sql
153c5 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65  ite3BitvecCreate
153c6 28 28 75 33 32 29 6e 50 61 67 65 29 3b 0a 20 20  ((u32)nPage);.  
153c7 20 20 69 66 28 20 21 70 42 74 2d 3e 70 48 61 73    if( !pBt->pHas
153c8 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20  Content ){.     
153c9 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
153ca 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  EM;.    }.  }.  
153cb 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
153cc 4b 20 26 26 20 70 67 6e 6f 3c 3d 73 71 6c 69 74  K && pgno<=sqlit
153cd 65 33 42 69 74 76 65 63 53 69 7a 65 28 70 42 74  e3BitvecSize(pBt
153ce 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 29 20 29  ->pHasContent) )
153cf 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
153d0 65 33 42 69 74 76 65 63 53 65 74 28 70 42 74 2d  e3BitvecSet(pBt-
153d1 3e 70 48 61 73 43 6f 6e 74 65 6e 74 2c 20 70 67  >pHasContent, pg
153d2 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  no);.  }.  retur
153d3 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51  n rc;.}../*.** Q
153d4 75 65 72 79 20 74 68 65 20 42 74 53 68 61 72 65  uery the BtShare
153d5 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 76 65  d.pHasContent ve
153d6 63 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ctor..**.** This
153d7 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
153d8 6c 65 64 20 77 68 65 6e 20 61 20 66 72 65 65 2d  led when a free-
153d9 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 69  list leaf page i
153da 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74  s removed from t
153db 68 65 0a 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20  he.** free-list 
153dc 66 6f 72 20 72 65 75 73 65 2e 20 49 74 20 72 65  for reuse. It re
153dd 74 75 72 6e 73 20 66 61 6c 73 65 20 69 66 20 69  turns false if i
153de 74 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 74  t is safe to ret
153df 72 69 65 76 65 20 74 68 65 0a 2a 2a 20 70 61 67  rieve the.** pag
153e0 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72  e from the pager
153e1 20 6c 61 79 65 72 20 77 69 74 68 20 74 68 65 20   layer with the 
153e2 27 6e 6f 2d 63 6f 6e 74 65 6e 74 27 20 66 6c 61  'no-content' fla
153e3 67 20 73 65 74 2e 20 54 72 75 65 20 6f 74 68 65  g set. True othe
153e4 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  rwise..*/.static
153e5 20 69 6e 74 20 62 74 72 65 65 47 65 74 48 61 73   int btreeGetHas
153e6 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64  Content(BtShared
153e7 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f   *pBt, Pgno pgno
153e8 29 7b 0a 20 20 42 69 74 76 65 63 20 2a 70 20 3d  ){.  Bitvec *p =
153e9 20 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e   pBt->pHasConten
153ea 74 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20 26  t;.  return (p &
153eb 26 20 28 70 67 6e 6f 3e 73 71 6c 69 74 65 33 42  & (pgno>sqlite3B
153ec 69 74 76 65 63 53 69 7a 65 28 70 29 20 7c 7c 20  itvecSize(p) || 
153ed 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73  sqlite3BitvecTes
153ee 74 28 70 2c 20 70 67 6e 6f 29 29 29 3b 0a 7d 0a  t(p, pgno)));.}.
153ef 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 28 64 65  ./*.** Clear (de
153f0 73 74 72 6f 79 29 20 74 68 65 20 42 74 53 68 61  stroy) the BtSha
153f1 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20  red.pHasContent 
153f2 62 69 74 76 65 63 2e 20 54 68 69 73 20 73 68 6f  bitvec. This sho
153f3 75 6c 64 20 62 65 0a 2a 2a 20 69 6e 76 6f 6b 65  uld be.** invoke
153f4 64 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73  d at the conclus
153f5 69 6f 6e 20 6f 66 20 65 61 63 68 20 77 72 69 74  ion of each writ
153f6 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  e-transaction..*
153f7 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 74  /.static void bt
153f8 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 65  reeClearHasConte
153f9 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  nt(BtShared *pBt
153fa 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76  ){.  sqlite3Bitv
153fb 65 63 44 65 73 74 72 6f 79 28 70 42 74 2d 3e 70  ecDestroy(pBt->p
153fc 48 61 73 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 70  HasContent);.  p
153fd 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20  Bt->pHasContent 
153fe 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61  = 0;.}../*.** Sa
153ff 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  ve the current c
15400 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69  ursor position i
15401 6e 20 74 68 65 20 76 61 72 69 61 62 6c 65 73 20  n the variables 
15402 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a  BtCursor.nKey .*
15403 2a 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 70  * and BtCursor.p
15404 4b 65 79 2e 20 54 68 65 20 63 75 72 73 6f 72 27  Key. The cursor'
15405 73 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74  s state is set t
15406 6f 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  o CURSOR_REQUIRE
15407 53 45 45 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  SEEK..**.** The 
15408 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75  caller must ensu
15409 72 65 20 74 68 61 74 20 74 68 65 20 63 75 72 73  re that the curs
1540a 6f 72 20 69 73 20 76 61 6c 69 64 20 28 68 61 73  or is valid (has
1540b 20 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f   eState==CURSOR_
1540c 56 41 4c 49 44 29 0a 2a 2a 20 70 72 69 6f 72 20  VALID).** prior 
1540d 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20  to calling this 
1540e 72 6f 75 74 69 6e 65 2e 20 20 0a 2a 2f 0a 73 74  routine.  .*/.st
1540f 61 74 69 63 20 69 6e 74 20 73 61 76 65 43 75 72  atic int saveCur
15410 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 43 75  sorPosition(BtCu
15411 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69  rsor *pCur){.  i
15412 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
15413 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d  ( CURSOR_VALID==
15414 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 3b 0a  pCur->eState );.
15415 20 20 61 73 73 65 72 74 28 20 30 3d 3d 70 43 75    assert( 0==pCu
15416 72 2d 3e 70 4b 65 79 20 29 3b 0a 20 20 61 73 73  r->pKey );.  ass
15417 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
15418 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 0a  Mutex(pCur) );..
15419 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1541a 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 2c  reeKeySize(pCur,
1541b 20 26 70 43 75 72 2d 3e 6e 4b 65 79 29 3b 0a 20   &pCur->nKey);. 
1541c 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
1541d 49 54 45 5f 4f 4b 20 29 3b 20 20 2f 2a 20 4b 65  ITE_OK );  /* Ke
1541e 79 53 69 7a 65 28 29 20 63 61 6e 6e 6f 74 20 66  ySize() cannot f
1541f 61 69 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20  ail */..  /* If 
15420 74 68 69 73 20 69 73 20 61 6e 20 69 6e 74 4b 65  this is an intKe
15421 79 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 74 68  y table, then th
15422 65 20 61 62 6f 76 65 20 63 61 6c 6c 20 74 6f 20  e above call to 
15423 42 74 72 65 65 4b 65 79 53 69 7a 65 28 29 0a 20  BtreeKeySize(). 
15424 20 2a 2a 20 73 74 6f 72 65 73 20 74 68 65 20 69   ** stores the i
15425 6e 74 65 67 65 72 20 6b 65 79 20 69 6e 20 70 43  nteger key in pC
15426 75 72 2d 3e 6e 4b 65 79 2e 20 49 6e 20 74 68 69  ur->nKey. In thi
15427 73 20 63 61 73 65 20 74 68 69 73 20 76 61 6c 75  s case this valu
15428 65 20 69 73 0a 20 20 2a 2a 20 61 6c 6c 20 74 68  e is.  ** all th
15429 61 74 20 69 73 20 72 65 71 75 69 72 65 64 2e 20  at is required. 
1542a 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 70 43  Otherwise, if pC
1542b 75 72 20 69 73 20 6e 6f 74 20 6f 70 65 6e 20 6f  ur is not open o
1542c 6e 20 61 6e 20 69 6e 74 4b 65 79 0a 20 20 2a 2a  n an intKey.  **
1542d 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 6d 61 6c   table, then mal
1542e 6c 6f 63 20 73 70 61 63 65 20 66 6f 72 20 61 6e  loc space for an
1542f 64 20 73 74 6f 72 65 20 74 68 65 20 70 43 75 72  d store the pCur
15430 2d 3e 6e 4b 65 79 20 62 79 74 65 73 20 6f 66 20  ->nKey bytes of 
15431 6b 65 79 20 0a 20 20 2a 2a 20 64 61 74 61 2e 0a  key .  ** data..
15432 20 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 70 43    */.  if( 0==pC
15433 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69  ur->apPage[0]->i
15434 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 76 6f 69  ntKey ){.    voi
15435 64 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65  d *pKey = sqlite
15436 33 4d 61 6c 6c 6f 63 28 20 28 69 6e 74 29 70 43  3Malloc( (int)pC
15437 75 72 2d 3e 6e 4b 65 79 20 29 3b 0a 20 20 20 20  ur->nKey );.    
15438 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20  if( pKey ){.    
15439 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1543a 72 65 65 4b 65 79 28 70 43 75 72 2c 20 30 2c 20  reeKey(pCur, 0, 
1543b 28 69 6e 74 29 70 43 75 72 2d 3e 6e 4b 65 79 2c  (int)pCur->nKey,
1543c 20 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 69 66   pKey);.      if
1543d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1543e 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d  ){.        pCur-
1543f 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20  >pKey = pKey;.  
15440 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
15441 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
15442 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20  pKey);.      }. 
15443 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
15444 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
15445 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61  M;.    }.  }.  a
15446 73 73 65 72 74 28 20 21 70 43 75 72 2d 3e 61 70  ssert( !pCur->ap
15447 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20  Page[0]->intKey 
15448 7c 7c 20 21 70 43 75 72 2d 3e 70 4b 65 79 20 29  || !pCur->pKey )
15449 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
1544a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e  ITE_OK ){.    in
1544b 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t i;.    for(i=0
1544c 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65  ; i<=pCur->iPage
1544d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65  ; i++){.      re
1544e 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e  leasePage(pCur->
1544f 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20  apPage[i]);.    
15450 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69    pCur->apPage[i
15451 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  ] = 0;.    }.   
15452 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d   pCur->iPage = -
15453 31 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74  1;.    pCur->eSt
15454 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 52 45 51  ate = CURSOR_REQ
15455 55 49 52 45 53 45 45 4b 3b 0a 20 20 7d 0a 0a 20  UIRESEEK;.  }.. 
15456 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66   invalidateOverf
15457 6c 6f 77 43 61 63 68 65 28 70 43 75 72 29 3b 0a  lowCache(pCur);.
15458 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
15459 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 70  /*.** Save the p
1545a 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6c 6c 20  ositions of all 
1545b 63 75 72 73 6f 72 73 20 65 78 63 65 70 74 20 70  cursors except p
1545c 45 78 63 65 70 74 20 6f 70 65 6e 20 6f 6e 20 74  Except open on t
1545d 68 65 20 74 61 62 6c 65 20 0a 2a 2a 20 77 69 74  he table .** wit
1545e 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 52 6f 6f  h root-page iRoo
1545f 74 2e 20 55 73 75 61 6c 6c 79 2c 20 74 68 69 73  t. Usually, this
15460 20 69 73 20 63 61 6c 6c 65 64 20 6a 75 73 74 20   is called just 
15461 62 65 66 6f 72 65 20 63 75 72 73 6f 72 0a 2a 2a  before cursor.**
15462 20 70 45 78 63 65 70 74 20 69 73 20 75 73 65 64   pExcept is used
15463 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 74   to modify the t
15464 61 62 6c 65 20 28 42 74 72 65 65 44 65 6c 65 74  able (BtreeDelet
15465 65 28 29 20 6f 72 20 42 74 72 65 65 49 6e 73 65  e() or BtreeInse
15466 72 74 28 29 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  rt())..*/.static
15467 20 69 6e 74 20 73 61 76 65 41 6c 6c 43 75 72 73   int saveAllCurs
15468 6f 72 73 28 42 74 53 68 61 72 65 64 20 2a 70 42  ors(BtShared *pB
15469 74 2c 20 50 67 6e 6f 20 69 52 6f 6f 74 2c 20 42  t, Pgno iRoot, B
1546a 74 43 75 72 73 6f 72 20 2a 70 45 78 63 65 70 74  tCursor *pExcept
1546b 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  ){.  BtCursor *p
1546c 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
1546d 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1546e 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
1546f 61 73 73 65 72 74 28 20 70 45 78 63 65 70 74 3d  assert( pExcept=
15470 3d 30 20 7c 7c 20 70 45 78 63 65 70 74 2d 3e 70  =0 || pExcept->p
15471 42 74 3d 3d 70 42 74 20 29 3b 0a 20 20 66 6f 72  Bt==pBt );.  for
15472 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b  (p=pBt->pCursor;
15473 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b   p; p=p->pNext){
15474 0a 20 20 20 20 69 66 28 20 70 21 3d 70 45 78 63  .    if( p!=pExc
15475 65 70 74 20 26 26 20 28 30 3d 3d 69 52 6f 6f 74  ept && (0==iRoot
15476 20 7c 7c 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d   || p->pgnoRoot=
15477 3d 69 52 6f 6f 74 29 20 26 26 20 0a 20 20 20 20  =iRoot) && .    
15478 20 20 20 20 70 2d 3e 65 53 74 61 74 65 3d 3d 43      p->eState==C
15479 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20  URSOR_VALID ){. 
1547a 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 61       int rc = sa
1547b 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  veCursorPosition
1547c 28 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53  (p);.      if( S
1547d 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a  QLITE_OK!=rc ){.
1547e 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
1547f 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  c;.      }.    }
15480 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
15481 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
15482 2a 20 43 6c 65 61 72 20 74 68 65 20 63 75 72 72  * Clear the curr
15483 65 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69 74  ent cursor posit
15484 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
15485 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
15486 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72  te3BtreeClearCur
15487 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43  sor(BtCursor *pC
15488 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63  ur){.  assert( c
15489 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
1548a 70 43 75 72 29 20 29 3b 0a 20 20 73 71 6c 69 74  pCur) );.  sqlit
1548b 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 70 4b  e3_free(pCur->pK
1548c 65 79 29 3b 0a 20 20 70 43 75 72 2d 3e 70 4b 65  ey);.  pCur->pKe
1548d 79 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 65  y = 0;.  pCur->e
1548e 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49  State = CURSOR_I
1548f 4e 56 41 4c 49 44 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  NVALID;.}../*.**
15490 20 49 6e 20 74 68 69 73 20 76 65 72 73 69 6f 6e   In this version
15491 20 6f 66 20 42 74 72 65 65 4d 6f 76 65 74 6f 2c   of BtreeMoveto,
15492 20 70 4b 65 79 20 69 73 20 61 20 70 61 63 6b 65   pKey is a packe
15493 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 0a 2a  d index record.*
15494 2a 20 73 75 63 68 20 61 73 20 69 73 20 67 65 6e  * such as is gen
15495 65 72 61 74 65 64 20 62 79 20 74 68 65 20 4f 50  erated by the OP
15496 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f  _MakeRecord opco
15497 64 65 2e 20 20 55 6e 70 61 63 6b 20 74 68 65 0a  de.  Unpack the.
15498 2a 2a 20 72 65 63 6f 72 64 20 61 6e 64 20 74 68  ** record and th
15499 65 6e 20 63 61 6c 6c 20 42 74 72 65 65 4d 6f 76  en call BtreeMov
1549a 65 74 6f 55 6e 70 61 63 6b 65 64 28 29 20 74 6f  etoUnpacked() to
1549b 20 64 6f 20 74 68 65 20 77 6f 72 6b 2e 0a 2a 2f   do the work..*/
1549c 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65  .static int btre
1549d 65 4d 6f 76 65 74 6f 28 0a 20 20 42 74 43 75 72  eMoveto(.  BtCur
1549e 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 2f  sor *pCur,     /
1549f 2a 20 43 75 72 73 6f 72 20 6f 70 65 6e 20 6f 6e  * Cursor open on
154a0 20 74 68 65 20 62 74 72 65 65 20 74 6f 20 62 65   the btree to be
154a1 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 63   searched */.  c
154a2 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c  onst void *pKey,
154a3 20 20 20 2f 2a 20 50 61 63 6b 65 64 20 6b 65 79     /* Packed key
154a4 20 69 66 20 74 68 65 20 62 74 72 65 65 20 69 73   if the btree is
154a5 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69   an index */.  i
154a6 36 34 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20  64 nKey,        
154a7 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 6b 65     /* Integer ke
154a8 79 20 66 6f 72 20 74 61 62 6c 65 73 2e 20 20 53  y for tables.  S
154a9 69 7a 65 20 6f 66 20 70 4b 65 79 20 66 6f 72 20  ize of pKey for 
154aa 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74  indices */.  int
154ab 20 62 69 61 73 2c 20 20 20 20 20 20 20 20 20 20   bias,          
154ac 20 2f 2a 20 42 69 61 73 20 73 65 61 72 63 68 20   /* Bias search 
154ad 74 6f 20 74 68 65 20 68 69 67 68 20 65 6e 64 20  to the high end 
154ae 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 20 20  */.  int *pRes  
154af 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
154b0 65 20 73 65 61 72 63 68 20 72 65 73 75 6c 74 73  e search results
154b1 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   here */.){.  in
154b2 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
154b3 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74           /* Stat
154b4 75 73 20 63 6f 64 65 20 2a 2f 0a 20 20 55 6e 70  us code */.  Unp
154b5 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 64  ackedRecord *pId
154b6 78 4b 65 79 3b 20 20 20 2f 2a 20 55 6e 70 61 63  xKey;   /* Unpac
154b7 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f  ked index key */
154b8 0a 20 20 63 68 61 72 20 61 53 70 61 63 65 5b 31  .  char aSpace[1
154b9 35 30 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  50];          /*
154ba 20 54 65 6d 70 20 73 70 61 63 65 20 66 6f 72 20   Temp space for 
154bb 70 49 64 78 4b 65 79 20 2d 20 74 6f 20 61 76 6f  pIdxKey - to avo
154bc 69 64 20 61 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 0a  id a malloc */..
154bd 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20    if( pKey ){.  
154be 20 20 61 73 73 65 72 74 28 20 6e 4b 65 79 3d 3d    assert( nKey==
154bf 28 69 36 34 29 28 69 6e 74 29 6e 4b 65 79 20 29  (i64)(int)nKey )
154c0 3b 0a 20 20 20 20 70 49 64 78 4b 65 79 20 3d 20  ;.    pIdxKey = 
154c1 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72  sqlite3VdbeRecor
154c2 64 55 6e 70 61 63 6b 28 70 43 75 72 2d 3e 70 4b  dUnpack(pCur->pK
154c3 65 79 49 6e 66 6f 2c 20 28 69 6e 74 29 6e 4b 65  eyInfo, (int)nKe
154c4 79 2c 20 70 4b 65 79 2c 0a 20 20 20 20 20 20 20  y, pKey,.       
154c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
154c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
154c7 53 70 61 63 65 2c 20 73 69 7a 65 6f 66 28 61 53  Space, sizeof(aS
154c8 70 61 63 65 29 29 3b 0a 20 20 20 20 69 66 28 20  pace));.    if( 
154c9 70 49 64 78 4b 65 79 3d 3d 30 20 29 20 72 65 74  pIdxKey==0 ) ret
154ca 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
154cb 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
154cc 49 64 78 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a  IdxKey = 0;.  }.
154cd 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
154ce 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65  reeMovetoUnpacke
154cf 64 28 70 43 75 72 2c 20 70 49 64 78 4b 65 79 2c  d(pCur, pIdxKey,
154d0 20 6e 4b 65 79 2c 20 62 69 61 73 2c 20 70 52 65   nKey, bias, pRe
154d1 73 29 3b 0a 20 20 69 66 28 20 70 4b 65 79 20 29  s);.  if( pKey )
154d2 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
154d3 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52  eDeleteUnpackedR
154d4 65 63 6f 72 64 28 70 49 64 78 4b 65 79 29 3b 0a  ecord(pIdxKey);.
154d5 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
154d6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72  .}../*.** Restor
154d7 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20  e the cursor to 
154d8 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 20  the position it 
154d9 77 61 73 20 69 6e 20 28 6f 72 20 61 73 20 63 6c  was in (or as cl
154da 6f 73 65 20 74 6f 20 61 73 20 70 6f 73 73 69 62  ose to as possib
154db 6c 65 29 0a 2a 2a 20 77 68 65 6e 20 73 61 76 65  le).** when save
154dc 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29  CursorPosition()
154dd 20 77 61 73 20 63 61 6c 6c 65 64 2e 20 4e 6f 74   was called. Not
154de 65 20 74 68 61 74 20 74 68 69 73 20 63 61 6c 6c  e that this call
154df 20 64 65 6c 65 74 65 73 20 74 68 65 20 0a 2a 2a   deletes the .**
154e0 20 73 61 76 65 64 20 70 6f 73 69 74 69 6f 6e 20   saved position 
154e1 69 6e 66 6f 20 73 74 6f 72 65 64 20 62 79 20 73  info stored by s
154e2 61 76 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  aveCursorPositio
154e3 6e 28 29 2c 20 73 6f 20 74 68 65 72 65 20 63 61  n(), so there ca
154e4 6e 20 62 65 0a 2a 2a 20 61 74 20 6d 6f 73 74 20  n be.** at most 
154e5 6f 6e 65 20 65 66 66 65 63 74 69 76 65 20 72 65  one effective re
154e6 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74  storeCursorPosit
154e7 69 6f 6e 28 29 20 63 61 6c 6c 20 61 66 74 65 72  ion() call after
154e8 20 65 61 63 68 20 0a 2a 2a 20 73 61 76 65 43 75   each .** saveCu
154e9 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0a  rsorPosition()..
154ea 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74  */.static int bt
154eb 72 65 65 52 65 73 74 6f 72 65 43 75 72 73 6f 72  reeRestoreCursor
154ec 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f  Position(BtCurso
154ed 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20  r *pCur){.  int 
154ee 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75  rc;.  assert( cu
154ef 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
154f0 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
154f1 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d  ( pCur->eState>=
154f2 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
154f3 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72  EK );.  if( pCur
154f4 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
154f5 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65  _FAULT ){.    re
154f6 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e  turn pCur->skipN
154f7 65 78 74 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d  ext;.  }.  pCur-
154f8 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52  >eState = CURSOR
154f9 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 72 63 20 3d  _INVALID;.  rc =
154fa 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75   btreeMoveto(pCu
154fb 72 2c 20 70 43 75 72 2d 3e 70 4b 65 79 2c 20 70  r, pCur->pKey, p
154fc 43 75 72 2d 3e 6e 4b 65 79 2c 20 30 2c 20 26 70  Cur->nKey, 0, &p
154fd 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 29 3b 0a  Cur->skipNext);.
154fe 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
154ff 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
15500 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e 70 4b  e3_free(pCur->pK
15501 65 79 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 70  ey);.    pCur->p
15502 4b 65 79 20 3d 20 30 3b 0a 20 20 20 20 61 73 73  Key = 0;.    ass
15503 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74  ert( pCur->eStat
15504 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e==CURSOR_VALID 
15505 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  || pCur->eState=
15506 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20  =CURSOR_INVALID 
15507 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
15508 72 63 3b 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 72  rc;.}..#define r
15509 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69  estoreCursorPosi
1550a 74 69 6f 6e 28 70 29 20 5c 0a 20 20 28 70 2d 3e  tion(p) \.  (p->
1550b 65 53 74 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52  eState>=CURSOR_R
1550c 45 51 55 49 52 45 53 45 45 4b 20 3f 20 5c 0a 20  EQUIRESEEK ? \. 
1550d 20 20 20 20 20 20 20 20 62 74 72 65 65 52 65 73          btreeRes
1550e 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69  toreCursorPositi
1550f 6f 6e 28 70 29 20 3a 20 5c 0a 20 20 20 20 20 20  on(p) : \.      
15510 20 20 20 53 51 4c 49 54 45 5f 4f 4b 29 0a 0a 2f     SQLITE_OK)../
15511 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 77  *.** Determine w
15512 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20  hether or not a 
15513 63 75 72 73 6f 72 20 68 61 73 20 6d 6f 76 65 64  cursor has moved
15514 20 66 72 6f 6d 20 74 68 65 20 70 6f 73 69 74 69   from the positi
15515 6f 6e 20 69 74 0a 2a 2a 20 77 61 73 20 6c 61 73  on it.** was las
15516 74 20 70 6c 61 63 65 64 20 61 74 2e 20 20 43 75  t placed at.  Cu
15517 72 73 6f 72 73 20 63 61 6e 20 6d 6f 76 65 20 77  rsors can move w
15518 68 65 6e 20 74 68 65 20 72 6f 77 20 74 68 65 79  hen the row they
15519 20 61 72 65 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a   are pointing.**
1551a 20 61 74 20 69 73 20 64 65 6c 65 74 65 64 20 6f   at is deleted o
1551b 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74 68  ut from under th
1551c 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  em..**.** This r
1551d 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61  outine returns a
1551e 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20  n error code if 
1551f 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77  something goes w
15520 72 6f 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 69 6e  rong.  The.** in
15521 74 65 67 65 72 20 2a 70 48 61 73 4d 6f 76 65 64  teger *pHasMoved
15522 20 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 69   is set to one i
15523 66 20 74 68 65 20 63 75 72 73 6f 72 20 68 61 73  f the cursor has
15524 20 6d 6f 76 65 64 20 61 6e 64 20 30 20 69 66 20   moved and 0 if 
15525 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  not..*/.SQLITE_P
15526 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
15527 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 61 73  e3BtreeCursorHas
15528 4d 6f 76 65 64 28 42 74 43 75 72 73 6f 72 20 2a  Moved(BtCursor *
15529 70 43 75 72 2c 20 69 6e 74 20 2a 70 48 61 73 4d  pCur, int *pHasM
1552a 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b  oved){.  int rc;
1552b 0a 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65  ..  rc = restore
1552c 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70  CursorPosition(p
1552d 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29  Cur);.  if( rc )
1552e 7b 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 64  {.    *pHasMoved
1552f 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 1;.    return
15530 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70   rc;.  }.  if( p
15531 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52  Cur->eState!=CUR
15532 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75  SOR_VALID || pCu
15533 72 2d 3e 73 6b 69 70 4e 65 78 74 21 3d 30 20 29  r->skipNext!=0 )
15534 7b 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 64  {.    *pHasMoved
15535 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 1;.  }else{. 
15536 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20     *pHasMoved = 
15537 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
15538 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69  SQLITE_OK;.}..#i
15539 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1553a 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a  T_AUTOVACUUM./*.
1553b 2a 2a 20 47 69 76 65 6e 20 61 20 70 61 67 65 20  ** Given a page 
1553c 6e 75 6d 62 65 72 20 6f 66 20 61 20 72 65 67 75  number of a regu
1553d 6c 61 72 20 64 61 74 61 62 61 73 65 20 70 61 67  lar database pag
1553e 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 70 61  e, return the pa
1553f 67 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72  ge.** number for
15540 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70   the pointer-map
15541 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61   page that conta
15542 69 6e 73 20 74 68 65 20 65 6e 74 72 79 20 66 6f  ins the entry fo
15543 72 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 70  r the.** input p
15544 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73  age number..*/.s
15545 74 61 74 69 63 20 50 67 6e 6f 20 70 74 72 6d 61  tatic Pgno ptrma
15546 70 50 61 67 65 6e 6f 28 42 74 53 68 61 72 65 64  pPageno(BtShared
15547 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f   *pBt, Pgno pgno
15548 29 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65 73 50  ){.  int nPagesP
15549 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 50 67 6e  erMapPage;.  Pgn
1554a 6f 20 69 50 74 72 4d 61 70 2c 20 72 65 74 3b 0a  o iPtrMap, ret;.
1554b 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1554c 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
1554d 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6e 50  ->mutex) );.  nP
1554e 61 67 65 73 50 65 72 4d 61 70 50 61 67 65 20 3d  agesPerMapPage =
1554f 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a   (pBt->usableSiz
15550 65 2f 35 29 2b 31 3b 0a 20 20 69 50 74 72 4d 61  e/5)+1;.  iPtrMa
15551 70 20 3d 20 28 70 67 6e 6f 2d 32 29 2f 6e 50 61  p = (pgno-2)/nPa
15552 67 65 73 50 65 72 4d 61 70 50 61 67 65 3b 0a 20  gesPerMapPage;. 
15553 20 72 65 74 20 3d 20 28 69 50 74 72 4d 61 70 2a   ret = (iPtrMap*
15554 6e 50 61 67 65 73 50 65 72 4d 61 70 50 61 67 65  nPagesPerMapPage
15555 29 20 2b 20 32 3b 20 0a 20 20 69 66 28 20 72 65  ) + 2; .  if( re
15556 74 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  t==PENDING_BYTE_
15557 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20  PAGE(pBt) ){.   
15558 20 72 65 74 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65   ret++;.  }.  re
15559 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a  turn ret;.}../*.
1555a 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 6e 74 72  ** Write an entr
1555b 79 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74  y into the point
1555c 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68  er map..**.** Th
1555d 69 73 20 72 6f 75 74 69 6e 65 20 75 70 64 61 74  is routine updat
1555e 65 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d  es the pointer m
1555f 61 70 20 65 6e 74 72 79 20 66 6f 72 20 70 61 67  ap entry for pag
15560 65 20 6e 75 6d 62 65 72 20 27 6b 65 79 27 0a 2a  e number 'key'.*
15561 2a 20 73 6f 20 74 68 61 74 20 69 74 20 6d 61 70  * so that it map
15562 73 20 74 6f 20 74 79 70 65 20 27 65 54 79 70 65  s to type 'eType
15563 27 20 61 6e 64 20 70 61 72 65 6e 74 20 70 61 67  ' and parent pag
15564 65 20 6e 75 6d 62 65 72 20 27 70 67 6e 6f 27 2e  e number 'pgno'.
15565 0a 2a 2a 0a 2a 2a 20 49 66 20 2a 70 52 43 20 69  .**.** If *pRC i
15566 73 20 69 6e 69 74 69 61 6c 6c 79 20 6e 6f 6e 2d  s initially non-
15567 7a 65 72 6f 20 28 6e 6f 6e 2d 53 51 4c 49 54 45  zero (non-SQLITE
15568 5f 4f 4b 29 20 74 68 65 6e 20 74 68 69 73 20 72  _OK) then this r
15569 6f 75 74 69 6e 65 20 69 73 0a 2a 2a 20 61 20 6e  outine is.** a n
1556a 6f 2d 6f 70 2e 20 20 49 66 20 61 6e 20 65 72 72  o-op.  If an err
1556b 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 20 61  or occurs, the a
1556c 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72  ppropriate error
1556d 20 63 6f 64 65 20 69 73 20 77 72 69 74 74 65 6e   code is written
1556e 0a 2a 2a 20 69 6e 74 6f 20 2a 70 52 43 2e 0a 2a  .** into *pRC..*
1556f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 74  /.static void pt
15570 72 6d 61 70 50 75 74 28 42 74 53 68 61 72 65 64  rmapPut(BtShared
15571 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c   *pBt, Pgno key,
15572 20 75 38 20 65 54 79 70 65 2c 20 50 67 6e 6f 20   u8 eType, Pgno 
15573 70 61 72 65 6e 74 2c 20 69 6e 74 20 2a 70 52 43  parent, int *pRC
15574 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62  ){.  DbPage *pDb
15575 50 61 67 65 3b 20 20 2f 2a 20 54 68 65 20 70 6f  Page;  /* The po
15576 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a  inter map page *
15577 2f 0a 20 20 75 38 20 2a 70 50 74 72 6d 61 70 3b  /.  u8 *pPtrmap;
15578 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69        /* The poi
15579 6e 74 65 72 20 6d 61 70 20 64 61 74 61 20 2a 2f  nter map data */
1557a 0a 20 20 50 67 6e 6f 20 69 50 74 72 6d 61 70 3b  .  Pgno iPtrmap;
1557b 20 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e       /* The poin
1557c 74 65 72 20 6d 61 70 20 70 61 67 65 20 6e 75 6d  ter map page num
1557d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66  ber */.  int off
1557e 73 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 4f 66  set;       /* Of
1557f 66 73 65 74 20 69 6e 20 70 6f 69 6e 74 65 72 20  fset in pointer 
15580 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e  map page */.  in
15581 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
15582 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66  /* Return code f
15583 72 6f 6d 20 73 75 62 66 75 6e 63 74 69 6f 6e 73  rom subfunctions
15584 20 2a 2f 0a 0a 20 20 69 66 28 20 2a 70 52 43 20   */..  if( *pRC 
15585 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73  ) return;..  ass
15586 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
15587 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74  ex_held(pBt->mut
15588 65 78 29 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20  ex) );.  /* The 
15589 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 70  master-journal p
1558a 61 67 65 20 6e 75 6d 62 65 72 20 6d 75 73 74 20  age number must 
1558b 6e 65 76 65 72 20 62 65 20 75 73 65 64 20 61 73  never be used as
1558c 20 61 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70   a pointer map p
1558d 61 67 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  age */.  assert(
1558e 20 30 3d 3d 50 54 52 4d 41 50 5f 49 53 50 41 47   0==PTRMAP_ISPAG
1558f 45 28 70 42 74 2c 20 50 45 4e 44 49 4e 47 5f 42  E(pBt, PENDING_B
15590 59 54 45 5f 50 41 47 45 28 70 42 74 29 29 20 29  YTE_PAGE(pBt)) )
15591 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  ;..  assert( pBt
15592 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a  ->autoVacuum );.
15593 20 20 69 66 28 20 6b 65 79 3d 3d 30 20 29 7b 0a    if( key==0 ){.
15594 20 20 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 54      *pRC = SQLIT
15595 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
15596 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
15597 20 20 69 50 74 72 6d 61 70 20 3d 20 50 54 52 4d    iPtrmap = PTRM
15598 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b  AP_PAGENO(pBt, k
15599 65 79 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  ey);.  rc = sqli
1559a 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74 2d  te3PagerGet(pBt-
1559b 3e 70 50 61 67 65 72 2c 20 69 50 74 72 6d 61 70  >pPager, iPtrmap
1559c 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 69  , &pDbPage);.  i
1559d 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1559e 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20 72   ){.    *pRC = r
1559f 63 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  c;.    return;. 
155a0 20 7d 0a 20 20 6f 66 66 73 65 74 20 3d 20 50 54   }.  offset = PT
155a1 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28 69  RMAP_PTROFFSET(i
155a2 50 74 72 6d 61 70 2c 20 6b 65 79 29 3b 0a 20 20  Ptrmap, key);.  
155a3 69 66 28 20 6f 66 66 73 65 74 3c 30 20 29 7b 0a  if( offset<0 ){.
155a4 20 20 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 54      *pRC = SQLIT
155a5 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
155a6 20 20 20 20 67 6f 74 6f 20 70 74 72 6d 61 70 5f      goto ptrmap_
155a7 65 78 69 74 3b 0a 20 20 7d 0a 20 20 70 50 74 72  exit;.  }.  pPtr
155a8 6d 61 70 20 3d 20 28 75 38 20 2a 29 73 71 6c 69  map = (u8 *)sqli
155a9 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28  te3PagerGetData(
155aa 70 44 62 50 61 67 65 29 3b 0a 0a 20 20 69 66 28  pDbPage);..  if(
155ab 20 65 54 79 70 65 21 3d 70 50 74 72 6d 61 70 5b   eType!=pPtrmap[
155ac 6f 66 66 73 65 74 5d 20 7c 7c 20 67 65 74 34 62  offset] || get4b
155ad 79 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66  yte(&pPtrmap[off
155ae 73 65 74 2b 31 5d 29 21 3d 70 61 72 65 6e 74 20  set+1])!=parent 
155af 29 7b 0a 20 20 20 20 54 52 41 43 45 28 28 22 50  ){.    TRACE(("P
155b0 54 52 4d 41 50 5f 55 50 44 41 54 45 3a 20 25 64  TRMAP_UPDATE: %d
155b1 2d 3e 28 25 64 2c 25 64 29 5c 6e 22 2c 20 6b 65  ->(%d,%d)\n", ke
155b2 79 2c 20 65 54 79 70 65 2c 20 70 61 72 65 6e 74  y, eType, parent
155b3 29 29 3b 0a 20 20 20 20 2a 70 52 43 3d 20 72 63  ));.    *pRC= rc
155b4 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
155b5 72 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a 20  rite(pDbPage);. 
155b6 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
155b7 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 50  E_OK ){.      pP
155b8 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d 20 3d 20  trmap[offset] = 
155b9 65 54 79 70 65 3b 0a 20 20 20 20 20 20 70 75 74  eType;.      put
155ba 34 62 79 74 65 28 26 70 50 74 72 6d 61 70 5b 6f  4byte(&pPtrmap[o
155bb 66 66 73 65 74 2b 31 5d 2c 20 70 61 72 65 6e 74  ffset+1], parent
155bc 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 70 74  );.    }.  }..pt
155bd 72 6d 61 70 5f 65 78 69 74 3a 0a 20 20 73 71 6c  rmap_exit:.  sql
155be 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
155bf 44 62 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  DbPage);.}../*.*
155c0 2a 20 52 65 61 64 20 61 6e 20 65 6e 74 72 79 20  * Read an entry 
155c1 66 72 6f 6d 20 74 68 65 20 70 6f 69 6e 74 65 72  from the pointer
155c2 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73   map..**.** This
155c3 20 72 6f 75 74 69 6e 65 20 72 65 74 72 69 65 76   routine retriev
155c4 65 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d  es the pointer m
155c5 61 70 20 65 6e 74 72 79 20 66 6f 72 20 70 61 67  ap entry for pag
155c6 65 20 27 6b 65 79 27 2c 20 77 72 69 74 69 6e 67  e 'key', writing
155c7 0a 2a 2a 20 74 68 65 20 74 79 70 65 20 61 6e 64  .** the type and
155c8 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d   parent page num
155c9 62 65 72 20 74 6f 20 2a 70 45 54 79 70 65 20 61  ber to *pEType a
155ca 6e 64 20 2a 70 50 67 6e 6f 20 72 65 73 70 65 63  nd *pPgno respec
155cb 74 69 76 65 6c 79 2e 0a 2a 2a 20 41 6e 20 65 72  tively..** An er
155cc 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
155cd 72 6e 65 64 20 69 66 20 73 6f 6d 65 74 68 69 6e  rned if somethin
155ce 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 6f 74  g goes wrong, ot
155cf 68 65 72 77 69 73 65 20 53 51 4c 49 54 45 5f 4f  herwise SQLITE_O
155d0 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  K..*/.static int
155d1 20 70 74 72 6d 61 70 47 65 74 28 42 74 53 68 61   ptrmapGet(BtSha
155d2 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 6b  red *pBt, Pgno k
155d3 65 79 2c 20 75 38 20 2a 70 45 54 79 70 65 2c 20  ey, u8 *pEType, 
155d4 50 67 6e 6f 20 2a 70 50 67 6e 6f 29 7b 0a 20 20  Pgno *pPgno){.  
155d5 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b  DbPage *pDbPage;
155d6 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65     /* The pointe
155d7 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20  r map page */.  
155d8 69 6e 74 20 69 50 74 72 6d 61 70 3b 20 20 20 20  int iPtrmap;    
155d9 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61     /* Pointer ma
155da 70 20 70 61 67 65 20 69 6e 64 65 78 20 2a 2f 0a  p page index */.
155db 20 20 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 20    u8 *pPtrmap;  
155dc 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
155dd 6d 61 70 20 70 61 67 65 20 64 61 74 61 20 2a 2f  map page data */
155de 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20  .  int offset;  
155df 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
155e0 6f 66 20 65 6e 74 72 79 20 69 6e 20 70 6f 69 6e  of entry in poin
155e1 74 65 72 20 6d 61 70 20 2a 2f 0a 20 20 69 6e 74  ter map */.  int
155e2 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   rc;..  assert( 
155e3 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
155e4 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  ld(pBt->mutex) )
155e5 3b 0a 0a 20 20 69 50 74 72 6d 61 70 20 3d 20 50  ;..  iPtrmap = P
155e6 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74  TRMAP_PAGENO(pBt
155e7 2c 20 6b 65 79 29 3b 0a 20 20 72 63 20 3d 20 73  , key);.  rc = s
155e8 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70  qlite3PagerGet(p
155e9 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 50 74 72  Bt->pPager, iPtr
155ea 6d 61 70 2c 20 26 70 44 62 50 61 67 65 29 3b 0a  map, &pDbPage);.
155eb 20 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20    if( rc!=0 ){. 
155ec 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
155ed 7d 0a 20 20 70 50 74 72 6d 61 70 20 3d 20 28 75  }.  pPtrmap = (u
155ee 38 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72  8 *)sqlite3Pager
155ef 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29  GetData(pDbPage)
155f0 3b 0a 0a 20 20 6f 66 66 73 65 74 20 3d 20 50 54  ;..  offset = PT
155f1 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28 69  RMAP_PTROFFSET(i
155f2 50 74 72 6d 61 70 2c 20 6b 65 79 29 3b 0a 20 20  Ptrmap, key);.  
155f3 61 73 73 65 72 74 28 20 70 45 54 79 70 65 21 3d  assert( pEType!=
155f4 30 20 29 3b 0a 20 20 2a 70 45 54 79 70 65 20 3d  0 );.  *pEType =
155f5 20 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d   pPtrmap[offset]
155f6 3b 0a 20 20 69 66 28 20 70 50 67 6e 6f 20 29 20  ;.  if( pPgno ) 
155f7 2a 70 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74  *pPgno = get4byt
155f8 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65  e(&pPtrmap[offse
155f9 74 2b 31 5d 29 3b 0a 0a 20 20 73 71 6c 69 74 65  t+1]);..  sqlite
155fa 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50  3PagerUnref(pDbP
155fb 61 67 65 29 3b 0a 20 20 69 66 28 20 2a 70 45 54  age);.  if( *pET
155fc 79 70 65 3c 31 20 7c 7c 20 2a 70 45 54 79 70 65  ype<1 || *pEType
155fd 3e 35 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  >5 ) return SQLI
155fe 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
155ff 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
15600 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a  _OK;.}..#else /*
15601 20 69 66 20 64 65 66 69 6e 65 64 20 53 51 4c 49   if defined SQLI
15602 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
15603 55 4d 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20  UM */.  #define 
15604 70 74 72 6d 61 70 50 75 74 28 77 2c 78 2c 79 2c  ptrmapPut(w,x,y,
15605 7a 2c 72 63 29 0a 20 20 23 64 65 66 69 6e 65 20  z,rc).  #define 
15606 70 74 72 6d 61 70 47 65 74 28 77 2c 78 2c 79 2c  ptrmapGet(w,x,y,
15607 7a 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 23  z) SQLITE_OK.  #
15608 64 65 66 69 6e 65 20 70 74 72 6d 61 70 50 75 74  define ptrmapPut
15609 4f 76 66 6c 50 74 72 28 78 2c 20 79 2c 20 72 63  OvflPtr(x, y, rc
1560a 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
1560b 47 69 76 65 6e 20 61 20 62 74 72 65 65 20 70 61  Given a btree pa
1560c 67 65 20 61 6e 64 20 61 20 63 65 6c 6c 20 69 6e  ge and a cell in
1560d 64 65 78 20 28 30 20 6d 65 61 6e 73 20 74 68 65  dex (0 means the
1560e 20 66 69 72 73 74 20 63 65 6c 6c 20 6f 6e 0a 2a   first cell on.*
1560f 2a 20 74 68 65 20 70 61 67 65 2c 20 31 20 6d 65  * the page, 1 me
15610 61 6e 73 20 74 68 65 20 73 65 63 6f 6e 64 20 63  ans the second c
15611 65 6c 6c 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74  ell, and so fort
15612 68 29 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e  h) return a poin
15613 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63 65  ter.** to the ce
15614 6c 6c 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a  ll content..**.*
15615 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
15616 6f 72 6b 73 20 6f 6e 6c 79 20 66 6f 72 20 70 61  orks only for pa
15617 67 65 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20  ges that do not 
15618 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77  contain overflow
15619 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69   cells..*/.#defi
1561a 6e 65 20 66 69 6e 64 43 65 6c 6c 28 50 2c 49 29  ne findCell(P,I)
1561b 20 5c 0a 20 20 28 28 50 29 2d 3e 61 44 61 74 61   \.  ((P)->aData
1561c 20 2b 20 28 28 50 29 2d 3e 6d 61 73 6b 50 61 67   + ((P)->maskPag
1561d 65 20 26 20 67 65 74 32 62 79 74 65 28 26 28 50  e & get2byte(&(P
1561e 29 2d 3e 61 44 61 74 61 5b 28 50 29 2d 3e 63 65  )->aData[(P)->ce
1561f 6c 6c 4f 66 66 73 65 74 2b 32 2a 28 49 29 5d 29  llOffset+2*(I)])
15620 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 61  ))../*.** This a
15621 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 78 20 76 65   more complex ve
15622 72 73 69 6f 6e 20 6f 66 20 66 69 6e 64 43 65 6c  rsion of findCel
15623 6c 28 29 20 74 68 61 74 20 77 6f 72 6b 73 20 66  l() that works f
15624 6f 72 0a 2a 2a 20 70 61 67 65 73 20 74 68 61 74  or.** pages that
15625 20 64 6f 20 63 6f 6e 74 61 69 6e 20 6f 76 65 72   do contain over
15626 66 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 73  flow cells..*/.s
15627 74 61 74 69 63 20 75 38 20 2a 66 69 6e 64 4f 76  tatic u8 *findOv
15628 65 72 66 6c 6f 77 43 65 6c 6c 28 4d 65 6d 50 61  erflowCell(MemPa
15629 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69  ge *pPage, int i
1562a 43 65 6c 6c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  Cell){.  int i;.
1562b 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1562c 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
1562d 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
1562e 29 3b 0a 20 20 66 6f 72 28 69 3d 70 50 61 67 65  );.  for(i=pPage
1562f 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2d 31 3b 20 69  ->nOverflow-1; i
15630 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69  >=0; i--){.    i
15631 6e 74 20 6b 3b 0a 20 20 20 20 73 74 72 75 63 74  nt k;.    struct
15632 20 5f 4f 76 66 6c 43 65 6c 6c 20 2a 70 4f 76 66   _OvflCell *pOvf
15633 6c 3b 0a 20 20 20 20 70 4f 76 66 6c 20 3d 20 26  l;.    pOvfl = &
15634 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 69 5d 3b  pPage->aOvfl[i];
15635 0a 20 20 20 20 6b 20 3d 20 70 4f 76 66 6c 2d 3e  .    k = pOvfl->
15636 69 64 78 3b 0a 20 20 20 20 69 66 28 20 6b 3c 3d  idx;.    if( k<=
15637 69 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69  iCell ){.      i
15638 66 28 20 6b 3d 3d 69 43 65 6c 6c 20 29 7b 0a 20  f( k==iCell ){. 
15639 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 4f         return pO
1563a 76 66 6c 2d 3e 70 43 65 6c 6c 3b 0a 20 20 20 20  vfl->pCell;.    
1563b 20 20 7d 0a 20 20 20 20 20 20 69 43 65 6c 6c 2d    }.      iCell-
1563c 2d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  -;.    }.  }.  r
1563d 65 74 75 72 6e 20 66 69 6e 64 43 65 6c 6c 28 70  eturn findCell(p
1563e 50 61 67 65 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a  Page, iCell);.}.
1563f 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 61 20 63  ./*.** Parse a c
15640 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 62 6c 6f 63  ell content bloc
15641 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e 20 74 68  k and fill in th
15642 65 20 43 65 6c 6c 49 6e 66 6f 20 73 74 72 75 63  e CellInfo struc
15643 74 75 72 65 2e 20 20 54 68 65 72 65 0a 2a 2a 20  ture.  There.** 
15644 61 72 65 20 74 77 6f 20 76 65 72 73 69 6f 6e 73  are two versions
15645 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   of this functio
15646 6e 2e 20 20 62 74 72 65 65 50 61 72 73 65 43 65  n.  btreeParseCe
15647 6c 6c 28 29 20 74 61 6b 65 73 20 61 20 0a 2a 2a  ll() takes a .**
15648 20 63 65 6c 6c 20 69 6e 64 65 78 20 61 73 20 74   cell index as t
15649 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
1564a 6e 74 20 61 6e 64 20 62 74 72 65 65 50 61 72 73  nt and btreePars
1564b 65 43 65 6c 6c 50 74 72 28 29 20 0a 2a 2a 20 74  eCellPtr() .** t
1564c 61 6b 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74  akes a pointer t
1564d 6f 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74 68  o the body of th
1564e 65 20 63 65 6c 6c 20 61 73 20 69 74 73 20 73 65  e cell as its se
1564f 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a  cond argument..*
15650 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 69 73  *.** Within this
15651 20 66 69 6c 65 2c 20 74 68 65 20 70 61 72 73 65   file, the parse
15652 43 65 6c 6c 28 29 20 6d 61 63 72 6f 20 63 61 6e  Cell() macro can
15653 20 62 65 20 63 61 6c 6c 65 64 20 69 6e 73 74 65   be called inste
15654 61 64 20 6f 66 0a 2a 2a 20 62 74 72 65 65 50 61  ad of.** btreePa
15655 72 73 65 43 65 6c 6c 50 74 72 28 29 2e 20 55 73  rseCellPtr(). Us
15656 69 6e 67 20 73 6f 6d 65 20 63 6f 6d 70 69 6c 65  ing some compile
15657 72 73 2c 20 74 68 69 73 20 77 69 6c 6c 20 62 65  rs, this will be
15658 20 66 61 73 74 65 72 2e 0a 2a 2f 0a 73 74 61 74   faster..*/.stat
15659 69 63 20 76 6f 69 64 20 62 74 72 65 65 50 61 72  ic void btreePar
1565a 73 65 43 65 6c 6c 50 74 72 28 0a 20 20 4d 65 6d  seCellPtr(.  Mem
1565b 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20  Page *pPage,    
1565c 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e       /* Page con
1565d 74 61 69 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c  taining the cell
1565e 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 2c   */.  u8 *pCell,
1565f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
15660 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   Pointer to the 
15661 63 65 6c 6c 20 74 65 78 74 2e 20 2a 2f 0a 20 20  cell text. */.  
15662 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66 6f 20  CellInfo *pInfo 
15663 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20          /* Fill 
15664 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72  in this structur
15665 65 20 2a 2f 0a 29 7b 0a 20 20 75 31 36 20 6e 3b  e */.){.  u16 n;
15666 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15667 20 20 2f 2a 20 4e 75 6d 62 65 72 20 62 79 74 65    /* Number byte
15668 73 20 69 6e 20 63 65 6c 6c 20 63 6f 6e 74 65 6e  s in cell conten
15669 74 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33  t header */.  u3
1566a 32 20 6e 50 61 79 6c 6f 61 64 3b 20 20 20 20 20  2 nPayload;     
1566b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1566c 6f 66 20 62 79 74 65 73 20 6f 66 20 63 65 6c 6c  of bytes of cell
1566d 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 0a 20 20 61   payload */..  a
1566e 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1566f 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d  utex_held(pPage-
15670 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  >pBt->mutex) );.
15671 0a 20 20 70 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20  .  pInfo->pCell 
15672 3d 20 70 43 65 6c 6c 3b 0a 20 20 61 73 73 65 72  = pCell;.  asser
15673 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d  t( pPage->leaf==
15674 30 20 7c 7c 20 70 50 61 67 65 2d 3e 6c 65 61 66  0 || pPage->leaf
15675 3d 3d 31 20 29 3b 0a 20 20 6e 20 3d 20 70 50 61  ==1 );.  n = pPa
15676 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65  ge->childPtrSize
15677 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 34  ;.  assert( n==4
15678 2d 34 2a 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  -4*pPage->leaf )
15679 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69  ;.  if( pPage->i
1567a 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 69 66 28  ntKey ){.    if(
1567b 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20   pPage->hasData 
1567c 29 7b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 67 65  ){.      n += ge
1567d 74 56 61 72 69 6e 74 33 32 28 26 70 43 65 6c 6c  tVarint32(&pCell
1567e 5b 6e 5d 2c 20 6e 50 61 79 6c 6f 61 64 29 3b 0a  [n], nPayload);.
1567f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
15680 20 6e 50 61 79 6c 6f 61 64 20 3d 20 30 3b 0a 20   nPayload = 0;. 
15681 20 20 20 7d 0a 20 20 20 20 6e 20 2b 3d 20 67 65     }.    n += ge
15682 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e  tVarint(&pCell[n
15683 5d 2c 20 28 75 36 34 2a 29 26 70 49 6e 66 6f 2d  ], (u64*)&pInfo-
15684 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 70 49 6e 66  >nKey);.    pInf
15685 6f 2d 3e 6e 44 61 74 61 20 3d 20 6e 50 61 79 6c  o->nData = nPayl
15686 6f 61 64 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  oad;.  }else{.  
15687 20 20 70 49 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d    pInfo->nData =
15688 20 30 3b 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74   0;.    n += get
15689 56 61 72 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b  Varint32(&pCell[
1568a 6e 5d 2c 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20  n], nPayload);. 
1568b 20 20 20 70 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d     pInfo->nKey =
1568c 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 7d 0a 20   nPayload;.  }. 
1568d 20 70 49 6e 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64   pInfo->nPayload
1568e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 70   = nPayload;.  p
1568f 49 6e 66 6f 2d 3e 6e 48 65 61 64 65 72 20 3d 20  Info->nHeader = 
15690 6e 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e  n;.  testcase( n
15691 50 61 79 6c 6f 61 64 3d 3d 70 50 61 67 65 2d 3e  Payload==pPage->
15692 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 74 65  maxLocal );.  te
15693 73 74 63 61 73 65 28 20 6e 50 61 79 6c 6f 61 64  stcase( nPayload
15694 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61  ==pPage->maxLoca
15695 6c 2b 31 20 29 3b 0a 20 20 69 66 28 20 6c 69 6b  l+1 );.  if( lik
15696 65 6c 79 28 6e 50 61 79 6c 6f 61 64 3c 3d 70 50  ely(nPayload<=pP
15697 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 29 20 29  age->maxLocal) )
15698 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73  {.    /* This is
15699 20 74 68 65 20 28 65 61 73 79 29 20 63 6f 6d 6d   the (easy) comm
1569a 6f 6e 20 63 61 73 65 20 77 68 65 72 65 20 74 68  on case where th
1569b 65 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61 64  e entire payload
1569c 20 66 69 74 73 0a 20 20 20 20 2a 2a 20 6f 6e 20   fits.    ** on 
1569d 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 2e 20  the local page. 
1569e 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 69 73 20   No overflow is 
1569f 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f  required..    */
156a0 0a 20 20 20 20 69 6e 74 20 6e 53 69 7a 65 3b 20  .    int nSize; 
156a1 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61           /* Tota
156a2 6c 20 73 69 7a 65 20 6f 66 20 63 65 6c 6c 20 63  l size of cell c
156a3 6f 6e 74 65 6e 74 20 69 6e 20 62 79 74 65 73 20  ontent in bytes 
156a4 2a 2f 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 6e  */.    nSize = n
156a5 50 61 79 6c 6f 61 64 20 2b 20 6e 3b 0a 20 20 20  Payload + n;.   
156a6 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d   pInfo->nLocal =
156a7 20 28 75 31 36 29 6e 50 61 79 6c 6f 61 64 3b 0a   (u16)nPayload;.
156a8 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 72      pInfo->iOver
156a9 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 69 66  flow = 0;.    if
156aa 28 20 28 6e 53 69 7a 65 20 26 20 7e 33 29 3d 3d  ( (nSize & ~3)==
156ab 30 20 29 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65  0 ){.      nSize
156ac 20 3d 20 34 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 4;        /* 
156ad 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a  Minimum cell siz
156ae 65 20 69 73 20 34 20 2a 2f 0a 20 20 20 20 7d 0a  e is 4 */.    }.
156af 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65      pInfo->nSize
156b0 20 3d 20 28 75 31 36 29 6e 53 69 7a 65 3b 0a 20   = (u16)nSize;. 
156b1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49   }else{.    /* I
156b2 66 20 74 68 65 20 70 61 79 6c 6f 61 64 20 77 69  f the payload wi
156b3 6c 6c 20 6e 6f 74 20 66 69 74 20 63 6f 6d 70 6c  ll not fit compl
156b4 65 74 65 6c 79 20 6f 6e 20 74 68 65 20 6c 6f 63  etely on the loc
156b5 61 6c 20 70 61 67 65 2c 20 77 65 20 68 61 76 65  al page, we have
156b6 0a 20 20 20 20 2a 2a 20 74 6f 20 64 65 63 69 64  .    ** to decid
156b7 65 20 68 6f 77 20 6d 75 63 68 20 74 6f 20 73 74  e how much to st
156b8 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 61 6e 64 20  ore locally and 
156b9 68 6f 77 20 6d 75 63 68 20 74 6f 20 73 70 69 6c  how much to spil
156ba 6c 20 6f 6e 74 6f 0a 20 20 20 20 2a 2a 20 6f 76  l onto.    ** ov
156bb 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 54  erflow pages.  T
156bc 68 65 20 73 74 72 61 74 65 67 79 20 69 73 20 74  he strategy is t
156bd 6f 20 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 61  o minimize the a
156be 6d 6f 75 6e 74 20 6f 66 20 75 6e 75 73 65 64 0a  mount of unused.
156bf 20 20 20 20 2a 2a 20 73 70 61 63 65 20 6f 6e 20      ** space on 
156c0 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 77  overflow pages w
156c1 68 69 6c 65 20 6b 65 65 70 69 6e 67 20 74 68 65  hile keeping the
156c2 20 61 6d 6f 75 6e 74 20 6f 66 20 6c 6f 63 61 6c   amount of local
156c3 20 73 74 6f 72 61 67 65 0a 20 20 20 20 2a 2a 20   storage.    ** 
156c4 69 6e 20 62 65 74 77 65 65 6e 20 6d 69 6e 4c 6f  in between minLo
156c5 63 61 6c 20 61 6e 64 20 6d 61 78 4c 6f 63 61 6c  cal and maxLocal
156c6 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
156c7 57 61 72 6e 69 6e 67 3a 20 20 63 68 61 6e 67 69  Warning:  changi
156c8 6e 67 20 74 68 65 20 77 61 79 20 6f 76 65 72 66  ng the way overf
156c9 6c 6f 77 20 70 61 79 6c 6f 61 64 20 69 73 20 64  low payload is d
156ca 69 73 74 72 69 62 75 74 65 64 20 69 6e 20 61 6e  istributed in an
156cb 79 0a 20 20 20 20 2a 2a 20 77 61 79 20 77 69 6c  y.    ** way wil
156cc 6c 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 69  l result in an i
156cd 6e 63 6f 6d 70 61 74 69 62 6c 65 20 66 69 6c 65  ncompatible file
156ce 20 66 6f 72 6d 61 74 2e 0a 20 20 20 20 2a 2f 0a   format..    */.
156cf 20 20 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c      int minLocal
156d0 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d  ;  /* Minimum am
156d1 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20  ount of payload 
156d2 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a  held locally */.
156d3 20 20 20 20 69 6e 74 20 6d 61 78 4c 6f 63 61 6c      int maxLocal
156d4 3b 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6d  ;  /* Maximum am
156d5 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20  ount of payload 
156d6 68 65 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a  held locally */.
156d7 20 20 20 20 69 6e 74 20 73 75 72 70 6c 75 73 3b      int surplus;
156d8 20 20 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70     /* Overflow p
156d9 61 79 6c 6f 61 64 20 61 76 61 69 6c 61 62 6c 65  ayload available
156da 20 66 6f 72 20 6c 6f 63 61 6c 20 73 74 6f 72 61   for local stora
156db 67 65 20 2a 2f 0a 0a 20 20 20 20 6d 69 6e 4c 6f  ge */..    minLo
156dc 63 61 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 69 6e  cal = pPage->min
156dd 4c 6f 63 61 6c 3b 0a 20 20 20 20 6d 61 78 4c 6f  Local;.    maxLo
156de 63 61 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 61 78  cal = pPage->max
156df 4c 6f 63 61 6c 3b 0a 20 20 20 20 73 75 72 70 6c  Local;.    surpl
156e0 75 73 20 3d 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20  us = minLocal + 
156e1 28 6e 50 61 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c  (nPayload - minL
156e2 6f 63 61 6c 29 25 28 70 50 61 67 65 2d 3e 70 42  ocal)%(pPage->pB
156e3 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20  t->usableSize - 
156e4 34 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65  4);.    testcase
156e5 28 20 73 75 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f  ( surplus==maxLo
156e6 63 61 6c 20 29 3b 0a 20 20 20 20 74 65 73 74 63  cal );.    testc
156e7 61 73 65 28 20 73 75 72 70 6c 75 73 3d 3d 6d 61  ase( surplus==ma
156e8 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 20 20  xLocal+1 );.    
156e9 69 66 28 20 73 75 72 70 6c 75 73 20 3c 3d 20 6d  if( surplus <= m
156ea 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20  axLocal ){.     
156eb 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d   pInfo->nLocal =
156ec 20 28 75 31 36 29 73 75 72 70 6c 75 73 3b 0a 20   (u16)surplus;. 
156ed 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
156ee 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20  pInfo->nLocal = 
156ef 28 75 31 36 29 6d 69 6e 4c 6f 63 61 6c 3b 0a 20  (u16)minLocal;. 
156f0 20 20 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e     }.    pInfo->
156f1 69 4f 76 65 72 66 6c 6f 77 20 3d 20 28 75 31 36  iOverflow = (u16
156f2 29 28 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20  )(pInfo->nLocal 
156f3 2b 20 6e 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d  + n);.    pInfo-
156f4 3e 6e 53 69 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e  >nSize = pInfo->
156f5 69 4f 76 65 72 66 6c 6f 77 20 2b 20 34 3b 0a 20  iOverflow + 4;. 
156f6 20 7d 0a 7d 0a 23 64 65 66 69 6e 65 20 70 61 72   }.}.#define par
156f7 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43  seCell(pPage, iC
156f8 65 6c 6c 2c 20 70 49 6e 66 6f 29 20 5c 0a 20 20  ell, pInfo) \.  
156f9 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  btreeParseCellPt
156fa 72 28 28 70 50 61 67 65 29 2c 20 66 69 6e 64 43  r((pPage), findC
156fb 65 6c 6c 28 28 70 50 61 67 65 29 2c 20 28 69 43  ell((pPage), (iC
156fc 65 6c 6c 29 29 2c 20 28 70 49 6e 66 6f 29 29 0a  ell)), (pInfo)).
156fd 73 74 61 74 69 63 20 76 6f 69 64 20 62 74 72 65  static void btre
156fe 65 50 61 72 73 65 43 65 6c 6c 28 0a 20 20 4d 65  eParseCell(.  Me
156ff 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20  mPage *pPage,   
15700 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f        /* Page co
15701 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 65 6c  ntaining the cel
15702 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c  l */.  int iCell
15703 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
15704 2a 20 54 68 65 20 63 65 6c 6c 20 69 6e 64 65 78  * The cell index
15705 2e 20 20 46 69 72 73 74 20 63 65 6c 6c 20 69 73  .  First cell is
15706 20 30 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f   0 */.  CellInfo
15707 20 2a 70 49 6e 66 6f 20 20 20 20 20 20 20 20 20   *pInfo         
15708 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 69 73 20  /* Fill in this 
15709 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a  structure */.){.
1570a 20 20 70 61 72 73 65 43 65 6c 6c 28 70 50 61 67    parseCell(pPag
1570b 65 2c 20 69 43 65 6c 6c 2c 20 70 49 6e 66 6f 29  e, iCell, pInfo)
1570c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75  ;.}../*.** Compu
1570d 74 65 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  te the total num
1570e 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 68 61  ber of bytes tha
1570f 74 20 61 20 43 65 6c 6c 20 6e 65 65 64 73 20 69  t a Cell needs i
15710 6e 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61  n the cell.** da
15711 74 61 20 61 72 65 61 20 6f 66 20 74 68 65 20 62  ta area of the b
15712 74 72 65 65 2d 70 61 67 65 2e 20 20 54 68 65 20  tree-page.  The 
15713 72 65 74 75 72 6e 20 6e 75 6d 62 65 72 20 69 6e  return number in
15714 63 6c 75 64 65 73 20 74 68 65 20 63 65 6c 6c 0a  cludes the cell.
15715 2a 2a 20 64 61 74 61 20 68 65 61 64 65 72 20 61  ** data header a
15716 6e 64 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 79  nd the local pay
15717 6c 6f 61 64 2c 20 62 75 74 20 6e 6f 74 20 61 6e  load, but not an
15718 79 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  y overflow page 
15719 6f 72 0a 2a 2a 20 74 68 65 20 73 70 61 63 65 20  or.** the space 
1571a 75 73 65 64 20 62 79 20 74 68 65 20 63 65 6c 6c  used by the cell
1571b 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61   pointer..*/.sta
1571c 74 69 63 20 75 31 36 20 63 65 6c 6c 53 69 7a 65  tic u16 cellSize
1571d 50 74 72 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  Ptr(MemPage *pPa
1571e 67 65 2c 20 75 38 20 2a 70 43 65 6c 6c 29 7b 0a  ge, u8 *pCell){.
1571f 20 20 75 38 20 2a 70 49 74 65 72 20 3d 20 26 70    u8 *pIter = &p
15720 43 65 6c 6c 5b 70 50 61 67 65 2d 3e 63 68 69 6c  Cell[pPage->chil
15721 64 50 74 72 53 69 7a 65 5d 3b 0a 20 20 75 33 32  dPtrSize];.  u32
15722 20 6e 53 69 7a 65 3b 0a 0a 23 69 66 64 65 66 20   nSize;..#ifdef 
15723 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 2f  SQLITE_DEBUG.  /
15724 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75  * The value retu
15725 72 6e 65 64 20 62 79 20 74 68 69 73 20 66 75 6e  rned by this fun
15726 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 61 6c 77  ction should alw
15727 61 79 73 20 62 65 20 74 68 65 20 73 61 6d 65 20  ays be the same 
15728 61 73 0a 20 20 2a 2a 20 74 68 65 20 28 43 65 6c  as.  ** the (Cel
15729 6c 49 6e 66 6f 2e 6e 53 69 7a 65 29 20 76 61 6c  lInfo.nSize) val
1572a 75 65 20 66 6f 75 6e 64 20 62 79 20 64 6f 69 6e  ue found by doin
1572b 67 20 61 20 66 75 6c 6c 20 70 61 72 73 65 20 6f  g a full parse o
1572c 66 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c 6c 2e  f the.  ** cell.
1572d 20 49 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47   If SQLITE_DEBUG
1572e 20 69 73 20 64 65 66 69 6e 65 64 2c 20 61 6e 20   is defined, an 
1572f 61 73 73 65 72 74 28 29 20 61 74 20 74 68 65 20  assert() at the 
15730 62 6f 74 74 6f 6d 20 6f 66 0a 20 20 2a 2a 20 74  bottom of.  ** t
15731 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 76 65 72  his function ver
15732 69 66 69 65 73 20 74 68 61 74 20 74 68 69 73 20  ifies that this 
15733 69 6e 76 61 72 69 61 6e 74 20 69 73 20 6e 6f 74  invariant is not
15734 20 76 69 6f 6c 61 74 65 64 2e 20 2a 2f 0a 20 20   violated. */.  
15735 43 65 6c 6c 49 6e 66 6f 20 64 65 62 75 67 69 6e  CellInfo debugin
15736 66 6f 3b 0a 20 20 62 74 72 65 65 50 61 72 73 65  fo;.  btreeParse
15737 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70  CellPtr(pPage, p
15738 43 65 6c 6c 2c 20 26 64 65 62 75 67 69 6e 66 6f  Cell, &debuginfo
15739 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28  );.#endif..  if(
1573a 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29   pPage->intKey )
1573b 7b 0a 20 20 20 20 75 38 20 2a 70 45 6e 64 3b 0a  {.    u8 *pEnd;.
1573c 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 68      if( pPage->h
1573d 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20  asData ){.      
1573e 70 49 74 65 72 20 2b 3d 20 67 65 74 56 61 72 69  pIter += getVari
1573f 6e 74 33 32 28 70 49 74 65 72 2c 20 6e 53 69 7a  nt32(pIter, nSiz
15740 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  e);.    }else{. 
15741 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 30 3b 0a       nSize = 0;.
15742 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 70 49      }..    /* pI
15743 74 65 72 20 6e 6f 77 20 70 6f 69 6e 74 73 20 61  ter now points a
15744 74 20 74 68 65 20 36 34 2d 62 69 74 20 69 6e 74  t the 64-bit int
15745 65 67 65 72 20 6b 65 79 20 76 61 6c 75 65 2c 20  eger key value, 
15746 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74  a variable lengt
15747 68 20 0a 20 20 20 20 2a 2a 20 69 6e 74 65 67 65  h .    ** intege
15748 72 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  r. The following
15749 20 62 6c 6f 63 6b 20 6d 6f 76 65 73 20 70 49 74   block moves pIt
1574a 65 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74  er to point at t
1574b 68 65 20 66 69 72 73 74 20 62 79 74 65 0a 20 20  he first byte.  
1574c 20 20 2a 2a 20 70 61 73 74 20 74 68 65 20 65 6e    ** past the en
1574d 64 20 6f 66 20 74 68 65 20 6b 65 79 20 76 61 6c  d of the key val
1574e 75 65 2e 20 2a 2f 0a 20 20 20 20 70 45 6e 64 20  ue. */.    pEnd 
1574f 3d 20 26 70 49 74 65 72 5b 39 5d 3b 0a 20 20 20  = &pIter[9];.   
15750 20 77 68 69 6c 65 28 20 28 2a 70 49 74 65 72 2b   while( (*pIter+
15751 2b 29 26 30 78 38 30 20 26 26 20 70 49 74 65 72  +)&0x80 && pIter
15752 3c 70 45 6e 64 20 29 3b 0a 20 20 7d 65 6c 73 65  <pEnd );.  }else
15753 7b 0a 20 20 20 20 70 49 74 65 72 20 2b 3d 20 67  {.    pIter += g
15754 65 74 56 61 72 69 6e 74 33 32 28 70 49 74 65 72  etVarint32(pIter
15755 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20  , nSize);.  }.. 
15756 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a 65   testcase( nSize
15757 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61  ==pPage->maxLoca
15758 6c 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  l );.  testcase(
15759 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d   nSize==pPage->m
1575a 61 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 69  axLocal+1 );.  i
1575b 66 28 20 6e 53 69 7a 65 3e 70 50 61 67 65 2d 3e  f( nSize>pPage->
1575c 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20  maxLocal ){.    
1575d 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70  int minLocal = p
1575e 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a  Page->minLocal;.
1575f 20 20 20 20 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c      nSize = minL
15760 6f 63 61 6c 20 2b 20 28 6e 53 69 7a 65 20 2d 20  ocal + (nSize - 
15761 6d 69 6e 4c 6f 63 61 6c 29 20 25 20 28 70 50 61  minLocal) % (pPa
15762 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53  ge->pBt->usableS
15763 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 74 65  ize - 4);.    te
15764 73 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70  stcase( nSize==p
15765 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29  Page->maxLocal )
15766 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
15767 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61  nSize==pPage->ma
15768 78 4c 6f 63 61 6c 2b 31 20 29 3b 0a 20 20 20 20  xLocal+1 );.    
15769 69 66 28 20 6e 53 69 7a 65 3e 70 50 61 67 65 2d  if( nSize>pPage-
1576a 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20 20 20  >maxLocal ){.   
1576b 20 20 20 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f     nSize = minLo
1576c 63 61 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e  cal;.    }.    n
1576d 53 69 7a 65 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20  Size += 4;.  }. 
1576e 20 6e 53 69 7a 65 20 2b 3d 20 28 75 33 32 29 28   nSize += (u32)(
1576f 70 49 74 65 72 20 2d 20 70 43 65 6c 6c 29 3b 0a  pIter - pCell);.
15770 0a 20 20 2f 2a 20 54 68 65 20 6d 69 6e 69 6d 75  .  /* The minimu
15771 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 63 65  m size of any ce
15772 6c 6c 20 69 73 20 34 20 62 79 74 65 73 2e 20 2a  ll is 4 bytes. *
15773 2f 0a 20 20 69 66 28 20 6e 53 69 7a 65 3c 34 20  /.  if( nSize<4 
15774 29 7b 0a 20 20 20 20 6e 53 69 7a 65 20 3d 20 34  ){.    nSize = 4
15775 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
15776 20 6e 53 69 7a 65 3d 3d 64 65 62 75 67 69 6e 66   nSize==debuginf
15777 6f 2e 6e 53 69 7a 65 20 29 3b 0a 20 20 72 65 74  o.nSize );.  ret
15778 75 72 6e 20 28 75 31 36 29 6e 53 69 7a 65 3b 0a  urn (u16)nSize;.
15779 7d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  }.#ifndef NDEBUG
1577a 0a 73 74 61 74 69 63 20 75 31 36 20 63 65 6c 6c  .static u16 cell
1577b 53 69 7a 65 28 4d 65 6d 50 61 67 65 20 2a 70 50  Size(MemPage *pP
1577c 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b  age, int iCell){
1577d 0a 20 20 72 65 74 75 72 6e 20 63 65 6c 6c 53 69  .  return cellSi
1577e 7a 65 50 74 72 28 70 50 61 67 65 2c 20 66 69 6e  zePtr(pPage, fin
1577f 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65  dCell(pPage, iCe
15780 6c 6c 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  ll));.}.#endif..
15781 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
15782 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f  MIT_AUTOVACUUM./
15783 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 65 6c 6c  *.** If the cell
15784 20 70 43 65 6c 6c 2c 20 70 61 72 74 20 6f 66 20   pCell, part of 
15785 70 61 67 65 20 70 50 61 67 65 20 63 6f 6e 74 61  page pPage conta
15786 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a  ins a pointer.**
15787 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20   to an overflow 
15788 70 61 67 65 2c 20 69 6e 73 65 72 74 20 61 6e 20  page, insert an 
15789 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 70  entry into the p
1578a 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 66 6f  ointer-map.** fo
1578b 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70  r the overflow p
1578c 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  age..*/.static v
1578d 6f 69 64 20 70 74 72 6d 61 70 50 75 74 4f 76 66  oid ptrmapPutOvf
1578e 6c 50 74 72 28 4d 65 6d 50 61 67 65 20 2a 70 50  lPtr(MemPage *pP
1578f 61 67 65 2c 20 75 38 20 2a 70 43 65 6c 6c 2c 20  age, u8 *pCell, 
15790 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 43 65 6c  int *pRC){.  Cel
15791 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 69 66  lInfo info;.  if
15792 28 20 2a 70 52 43 20 29 20 72 65 74 75 72 6e 3b  ( *pRC ) return;
15793 0a 20 20 61 73 73 65 72 74 28 20 70 43 65 6c 6c  .  assert( pCell
15794 21 3d 30 20 29 3b 0a 20 20 62 74 72 65 65 50 61  !=0 );.  btreePa
15795 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65  rseCellPtr(pPage
15796 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b  , pCell, &info);
15797 0a 20 20 61 73 73 65 72 74 28 20 28 69 6e 66 6f  .  assert( (info
15798 2e 6e 44 61 74 61 2b 28 70 50 61 67 65 2d 3e 69  .nData+(pPage->i
15799 6e 74 4b 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65  ntKey?0:info.nKe
1579a 79 29 29 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f  y))==info.nPaylo
1579b 61 64 20 29 3b 0a 20 20 69 66 28 20 69 6e 66 6f  ad );.  if( info
1579c 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20  .iOverflow ){.  
1579d 20 20 50 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65    Pgno ovfl = ge
1579e 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e  t4byte(&pCell[in
1579f 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a  fo.iOverflow]);.
157a0 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 50      ptrmapPut(pP
157a1 61 67 65 2d 3e 70 42 74 2c 20 6f 76 66 6c 2c 20  age->pBt, ovfl, 
157a2 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31  PTRMAP_OVERFLOW1
157a3 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70  , pPage->pgno, p
157a4 52 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  RC);.  }.}.#endi
157a5 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 72 61 67  f.../*.** Defrag
157a6 6d 65 6e 74 20 74 68 65 20 70 61 67 65 20 67 69  ment the page gi
157a7 76 65 6e 2e 20 20 41 6c 6c 20 43 65 6c 6c 73 20  ven.  All Cells 
157a8 61 72 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65  are moved to the
157a9 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65 20 70  .** end of the p
157aa 61 67 65 20 61 6e 64 20 61 6c 6c 20 66 72 65 65  age and all free
157ab 20 73 70 61 63 65 20 69 73 20 63 6f 6c 6c 65 63   space is collec
157ac 74 65 64 20 69 6e 74 6f 20 6f 6e 65 0a 2a 2a 20  ted into one.** 
157ad 62 69 67 20 46 72 65 65 42 6c 6b 20 74 68 61 74  big FreeBlk that
157ae 20 6f 63 63 75 72 73 20 69 6e 20 62 65 74 77 65   occurs in betwe
157af 65 6e 20 74 68 65 20 68 65 61 64 65 72 20 61 6e  en the header an
157b0 64 20 63 65 6c 6c 0a 2a 2a 20 70 6f 69 6e 74 65  d cell.** pointe
157b1 72 20 61 72 72 61 79 20 61 6e 64 20 74 68 65 20  r array and the 
157b2 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65  cell content are
157b3 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  a..*/.static int
157b4 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67 65 28   defragmentPage(
157b5 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b  MemPage *pPage){
157b6 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
157b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
157b8 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
157b9 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20  .  int pc;      
157ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
157bb 20 41 64 64 72 65 73 73 20 6f 66 20 61 20 69 2d   Address of a i-
157bc 74 68 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74  th cell */.  int
157bd 20 68 64 72 3b 20 20 20 20 20 20 20 20 20 20 20   hdr;           
157be 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
157bf 74 20 74 6f 20 74 68 65 20 70 61 67 65 20 68 65  t to the page he
157c0 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 69  ader */.  int si
157c1 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
157c2 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
157c3 61 20 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20  a cell */.  int 
157c4 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 20  usableSize;     
157c5 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
157c6 20 6f 66 20 75 73 61 62 6c 65 20 62 79 74 65 73   of usable bytes
157c7 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 20   on a page */.  
157c8 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20  int cellOffset; 
157c9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66             /* Of
157ca 66 73 65 74 20 74 6f 20 74 68 65 20 63 65 6c 6c  fset to the cell
157cb 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 2a   pointer array *
157cc 2f 0a 20 20 69 6e 74 20 63 62 72 6b 3b 20 20 20  /.  int cbrk;   
157cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
157ce 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20  * Offset to the 
157cf 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65  cell content are
157d0 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  a */.  int nCell
157d1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
157d2 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
157d3 65 6c 6c 73 20 6f 6e 20 74 68 65 20 70 61 67 65  ells on the page
157d4 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   */.  unsigned c
157d5 68 61 72 20 2a 64 61 74 61 3b 20 20 20 20 20 20  har *data;      
157d6 20 2f 2a 20 54 68 65 20 70 61 67 65 20 64 61 74   /* The page dat
157d7 61 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  a */.  unsigned 
157d8 63 68 61 72 20 2a 74 65 6d 70 3b 20 20 20 20 20  char *temp;     
157d9 20 20 2f 2a 20 54 65 6d 70 20 61 72 65 61 20 66    /* Temp area f
157da 6f 72 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  or cell content 
157db 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 46 69  */.  int iCellFi
157dc 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  rst;            
157dd 2f 2a 20 46 69 72 73 74 20 61 6c 6c 6f 77 61 62  /* First allowab
157de 6c 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 2a 2f  le cell index */
157df 0a 20 20 69 6e 74 20 69 43 65 6c 6c 4c 61 73 74  .  int iCellLast
157e0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
157e1 20 4c 61 73 74 20 70 6f 73 73 69 62 6c 65 20 63   Last possible c
157e2 65 6c 6c 20 69 6e 64 65 78 20 2a 2f 0a 0a 0a 20  ell index */... 
157e3 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
157e4 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
157e5 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
157e6 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
157e7 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20  age->pBt!=0 );. 
157e8 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
157e9 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
157ea 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41  <= SQLITE_MAX_PA
157eb 47 45 5f 53 49 5a 45 20 29 3b 0a 20 20 61 73 73  GE_SIZE );.  ass
157ec 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65  ert( pPage->nOve
157ed 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73  rflow==0 );.  as
157ee 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
157ef 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
157f0 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
157f1 20 74 65 6d 70 20 3d 20 73 71 6c 69 74 65 33 50   temp = sqlite3P
157f2 61 67 65 72 54 65 6d 70 53 70 61 63 65 28 70 50  agerTempSpace(pP
157f3 61 67 65 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72  age->pBt->pPager
157f4 29 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67  );.  data = pPag
157f5 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20  e->aData;.  hdr 
157f6 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  = pPage->hdrOffs
157f7 65 74 3b 0a 20 20 63 65 6c 6c 4f 66 66 73 65 74  et;.  cellOffset
157f8 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66   = pPage->cellOf
157f9 66 73 65 74 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20  fset;.  nCell = 
157fa 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20  pPage->nCell;.  
157fb 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3d 3d 67  assert( nCell==g
157fc 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  et2byte(&data[hd
157fd 72 2b 33 5d 29 20 29 3b 0a 20 20 75 73 61 62 6c  r+3]) );.  usabl
157fe 65 53 69 7a 65 20 3d 20 70 50 61 67 65 2d 3e 70  eSize = pPage->p
157ff 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a  Bt->usableSize;.
15800 20 20 63 62 72 6b 20 3d 20 67 65 74 32 62 79 74    cbrk = get2byt
15801 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b  e(&data[hdr+5]);
15802 0a 20 20 6d 65 6d 63 70 79 28 26 74 65 6d 70 5b  .  memcpy(&temp[
15803 63 62 72 6b 5d 2c 20 26 64 61 74 61 5b 63 62 72  cbrk], &data[cbr
15804 6b 5d 2c 20 75 73 61 62 6c 65 53 69 7a 65 20 2d  k], usableSize -
15805 20 63 62 72 6b 29 3b 0a 20 20 63 62 72 6b 20 3d   cbrk);.  cbrk =
15806 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 69   usableSize;.  i
15807 43 65 6c 6c 46 69 72 73 74 20 3d 20 63 65 6c 6c  CellFirst = cell
15808 4f 66 66 73 65 74 20 2b 20 32 2a 6e 43 65 6c 6c  Offset + 2*nCell
15809 3b 0a 20 20 69 43 65 6c 6c 4c 61 73 74 20 3d 20  ;.  iCellLast = 
1580a 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a  usableSize - 4;.
1580b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65    for(i=0; i<nCe
1580c 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38  ll; i++){.    u8
1580d 20 2a 70 41 64 64 72 3b 20 20 20 20 20 2f 2a 20   *pAddr;     /* 
1580e 54 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 70 6f  The i-th cell po
1580f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 70 41 64  inter */.    pAd
15810 64 72 20 3d 20 26 64 61 74 61 5b 63 65 6c 6c 4f  dr = &data[cellO
15811 66 66 73 65 74 20 2b 20 69 2a 32 5d 3b 0a 20 20  ffset + i*2];.  
15812 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28    pc = get2byte(
15813 70 41 64 64 72 29 3b 0a 20 20 20 20 74 65 73 74  pAddr);.    test
15814 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c 46  case( pc==iCellF
15815 69 72 73 74 20 29 3b 0a 20 20 20 20 74 65 73 74  irst );.    test
15816 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c 4c  case( pc==iCellL
15817 61 73 74 20 29 3b 0a 23 69 66 20 21 64 65 66 69  ast );.#if !defi
15818 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
15819 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c 5f  E_OVERSIZE_CELL_
1581a 43 48 45 43 4b 29 0a 20 20 20 20 2f 2a 20 54 68  CHECK).    /* Th
1581b 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 68  ese conditions h
1581c 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e  ave already been
1581d 20 76 65 72 69 66 69 65 64 20 69 6e 20 62 74 72   verified in btr
1581e 65 65 49 6e 69 74 50 61 67 65 28 29 0a 20 20 20  eeInitPage().   
1581f 20 2a 2a 20 69 66 20 53 51 4c 49 54 45 5f 45 4e   ** if SQLITE_EN
15820 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45  ABLE_OVERSIZE_CE
15821 4c 4c 5f 43 48 45 43 4b 20 69 73 20 64 65 66 69  LL_CHECK is defi
15822 6e 65 64 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ned .    */.    
15823 69 66 28 20 70 63 3c 69 43 65 6c 6c 46 69 72 73  if( pc<iCellFirs
15824 74 20 7c 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 73  t || pc>iCellLas
15825 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  t ){.      retur
15826 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
15827 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 23 65 6e  _BKPT;.    }.#en
15828 64 69 66 0a 20 20 20 20 61 73 73 65 72 74 28 20  dif.    assert( 
15829 70 63 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20 26  pc>=iCellFirst &
1582a 26 20 70 63 3c 3d 69 43 65 6c 6c 4c 61 73 74 20  & pc<=iCellLast 
1582b 29 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 63 65  );.    size = ce
1582c 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c  llSizePtr(pPage,
1582d 20 26 74 65 6d 70 5b 70 63 5d 29 3b 0a 20 20 20   &temp[pc]);.   
1582e 20 63 62 72 6b 20 2d 3d 20 73 69 7a 65 3b 0a 23   cbrk -= size;.#
1582f 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
15830 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a  E_ENABLE_OVERSIZ
15831 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 29 0a 20 20  E_CELL_CHECK).  
15832 20 20 69 66 28 20 63 62 72 6b 3c 69 43 65 6c 6c    if( cbrk<iCell
15833 46 69 72 73 74 20 29 7b 0a 20 20 20 20 20 20 72  First ){.      r
15834 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
15835 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d  RUPT_BKPT;.    }
15836 0a 23 65 6c 73 65 0a 20 20 20 20 69 66 28 20 63  .#else.    if( c
15837 62 72 6b 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c  brk<iCellFirst |
15838 7c 20 70 63 2b 73 69 7a 65 3e 75 73 61 62 6c 65  | pc+size>usable
15839 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72 65  Size ){.      re
1583a 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
1583b 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a  UPT_BKPT;.    }.
1583c 23 65 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72  #endif.    asser
1583d 74 28 20 63 62 72 6b 2b 73 69 7a 65 3c 3d 75 73  t( cbrk+size<=us
1583e 61 62 6c 65 53 69 7a 65 20 26 26 20 63 62 72 6b  ableSize && cbrk
1583f 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a  >=iCellFirst );.
15840 20 20 20 20 74 65 73 74 63 61 73 65 28 20 63 62      testcase( cb
15841 72 6b 2b 73 69 7a 65 3d 3d 75 73 61 62 6c 65 53  rk+size==usableS
15842 69 7a 65 20 29 3b 0a 20 20 20 20 74 65 73 74 63  ize );.    testc
15843 61 73 65 28 20 70 63 2b 73 69 7a 65 3d 3d 75 73  ase( pc+size==us
15844 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20  ableSize );.    
15845 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 63 62 72  memcpy(&data[cbr
15846 6b 5d 2c 20 26 74 65 6d 70 5b 70 63 5d 2c 20 73  k], &temp[pc], s
15847 69 7a 65 29 3b 0a 20 20 20 20 70 75 74 32 62 79  ize);.    put2by
15848 74 65 28 70 41 64 64 72 2c 20 63 62 72 6b 29 3b  te(pAddr, cbrk);
15849 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 63  .  }.  assert( c
1584a 62 72 6b 3e 3d 69 43 65 6c 6c 46 69 72 73 74 20  brk>=iCellFirst 
1584b 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64  );.  put2byte(&d
1584c 61 74 61 5b 68 64 72 2b 35 5d 2c 20 63 62 72 6b  ata[hdr+5], cbrk
1584d 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b 31 5d  );.  data[hdr+1]
1584e 20 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68 64 72   = 0;.  data[hdr
1584f 2b 32 5d 20 3d 20 30 3b 0a 20 20 64 61 74 61 5b  +2] = 0;.  data[
15850 68 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 6d 65  hdr+7] = 0;.  me
15851 6d 73 65 74 28 26 64 61 74 61 5b 69 43 65 6c 6c  mset(&data[iCell
15852 46 69 72 73 74 5d 2c 20 30 2c 20 63 62 72 6b 2d  First], 0, cbrk-
15853 69 43 65 6c 6c 46 69 72 73 74 29 3b 0a 20 20 61  iCellFirst);.  a
15854 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
15855 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
15856 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
15857 3b 0a 20 20 69 66 28 20 63 62 72 6b 2d 69 43 65  ;.  if( cbrk-iCe
15858 6c 6c 46 69 72 73 74 21 3d 70 50 61 67 65 2d 3e  llFirst!=pPage->
15859 6e 46 72 65 65 20 29 7b 0a 20 20 20 20 72 65 74  nFree ){.    ret
1585a 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
1585b 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72  PT_BKPT;.  }.  r
1585c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1585d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
1585e 74 65 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f  te nByte bytes o
1585f 66 20 73 70 61 63 65 20 66 72 6f 6d 20 77 69 74  f space from wit
15860 68 69 6e 20 74 68 65 20 42 2d 54 72 65 65 20 70  hin the B-Tree p
15861 61 67 65 20 70 61 73 73 65 64 0a 2a 2a 20 61 73  age passed.** as
15862 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
15863 65 6e 74 2e 20 57 72 69 74 65 20 69 6e 74 6f 20  ent. Write into 
15864 2a 70 49 64 78 20 74 68 65 20 69 6e 64 65 78 20  *pIdx the index 
15865 69 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74  into pPage->aDat
15866 61 5b 5d 0a 2a 2a 20 6f 66 20 74 68 65 20 66 69  a[].** of the fi
15867 72 73 74 20 62 79 74 65 20 6f 66 20 61 6c 6c 6f  rst byte of allo
15868 63 61 74 65 64 20 73 70 61 63 65 2e 20 52 65 74  cated space. Ret
15869 75 72 6e 20 65 69 74 68 65 72 20 53 51 4c 49 54  urn either SQLIT
1586a 45 5f 4f 4b 20 6f 72 0a 2a 2a 20 61 6e 20 65 72  E_OK or.** an er
1586b 72 6f 72 20 63 6f 64 65 20 28 75 73 75 61 6c 6c  ror code (usuall
1586c 79 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  y SQLITE_CORRUPT
1586d 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c  )..**.** The cal
1586e 6c 65 72 20 67 75 61 72 61 6e 74 65 65 73 20 74  ler guarantees t
1586f 68 61 74 20 74 68 65 72 65 20 69 73 20 73 75 66  hat there is suf
15870 66 69 63 69 65 6e 74 20 73 70 61 63 65 20 74 6f  ficient space to
15871 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 61 6c 6c   make the.** all
15872 6f 63 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 72  ocation.  This r
15873 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 6e 65 65  outine might nee
15874 64 20 74 6f 20 64 65 66 72 61 67 6d 65 6e 74 20  d to defragment 
15875 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 72 69 6e  in order to brin
15876 67 0a 2a 2a 20 61 6c 6c 20 74 68 65 20 73 70 61  g.** all the spa
15877 63 65 20 74 6f 67 65 74 68 65 72 2c 20 68 6f 77  ce together, how
15878 65 76 65 72 2e 20 20 54 68 69 73 20 72 6f 75 74  ever.  This rout
15879 69 6e 65 20 77 69 6c 6c 20 61 76 6f 69 64 20 75  ine will avoid u
1587a 73 69 6e 67 0a 2a 2a 20 74 68 65 20 66 69 72 73  sing.** the firs
1587b 74 20 74 77 6f 20 62 79 74 65 73 20 70 61 73 74  t two bytes past
1587c 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65   the cell pointe
1587d 72 20 61 72 65 61 20 73 69 6e 63 65 20 70 72 65  r area since pre
1587e 73 75 6d 61 62 6c 79 20 74 68 69 73 0a 2a 2a 20  sumably this.** 
1587f 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 62 65  allocation is be
15880 69 6e 67 20 6d 61 64 65 20 69 6e 20 6f 72 64 65  ing made in orde
15881 72 20 74 6f 20 69 6e 73 65 72 74 20 61 20 6e 65  r to insert a ne
15882 77 20 63 65 6c 6c 2c 20 73 6f 20 77 65 20 77 69  w cell, so we wi
15883 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 65 6e 64 20 75  ll.** also end u
15884 70 20 6e 65 65 64 69 6e 67 20 61 20 6e 65 77 20  p needing a new 
15885 63 65 6c 6c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f  cell pointer..*/
15886 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f  .static int allo
15887 63 61 74 65 53 70 61 63 65 28 4d 65 6d 50 61 67  cateSpace(MemPag
15888 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 6e 42  e *pPage, int nB
15889 79 74 65 2c 20 69 6e 74 20 2a 70 49 64 78 29 7b  yte, int *pIdx){
1588a 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 72  .  const int hdr
1588b 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66   = pPage->hdrOff
1588c 73 65 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 61 6c  set;    /* Local
1588d 20 63 61 63 68 65 20 6f 66 20 70 50 61 67 65 2d   cache of pPage-
1588e 3e 68 64 72 4f 66 66 73 65 74 20 2a 2f 0a 20 20  >hdrOffset */.  
1588f 75 38 20 2a 20 63 6f 6e 73 74 20 64 61 74 61 20  u8 * const data 
15890 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 20  = pPage->aData; 
15891 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63 61       /* Local ca
15892 63 68 65 20 6f 66 20 70 50 61 67 65 2d 3e 61 44  che of pPage->aD
15893 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 72  ata */.  int nFr
15894 61 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ag;             
15895 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
15896 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 61 67 6d   Number of fragm
15897 65 6e 74 65 64 20 62 79 74 65 73 20 6f 6e 20 70  ented bytes on p
15898 50 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 74 6f  Page */.  int to
15899 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
1589a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1589b 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20  * First byte of 
1589c 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65  cell content are
1589d 61 20 2a 2f 0a 20 20 69 6e 74 20 67 61 70 3b 20  a */.  int gap; 
1589e 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
1589f 62 79 74 65 20 6f 66 20 67 61 70 20 62 65 74 77  byte of gap betw
158a0 65 65 6e 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  een cell pointer
158a1 73 20 61 6e 64 20 63 65 6c 6c 20 63 6f 6e 74 65  s and cell conte
158a2 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20  nt */.  int rc; 
158a3 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67          /* Integ
158a4 65 72 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a  er return code *
158a5 2f 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 73  /.  .  assert( s
158a6 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
158a7 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44  teable(pPage->pD
158a8 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65  bPage) );.  asse
158a9 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 20 29  rt( pPage->pBt )
158aa 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
158ab 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
158ac 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
158ad 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  ) );.  assert( n
158ae 42 79 74 65 3e 3d 30 20 29 3b 20 20 2f 2a 20 4d  Byte>=0 );  /* M
158af 69 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65  inimum cell size
158b0 20 69 73 20 34 20 2a 2f 0a 20 20 61 73 73 65 72   is 4 */.  asser
158b1 74 28 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3e  t( pPage->nFree>
158b2 3d 6e 42 79 74 65 20 29 3b 0a 20 20 61 73 73 65  =nByte );.  asse
158b3 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  rt( pPage->nOver
158b4 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73  flow==0 );.  ass
158b5 65 72 74 28 20 6e 42 79 74 65 3c 70 50 61 67 65  ert( nByte<pPage
158b6 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  ->pBt->usableSiz
158b7 65 2d 38 20 29 3b 0a 0a 20 20 6e 46 72 61 67 20  e-8 );..  nFrag 
158b8 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 3b 0a 20  = data[hdr+7];. 
158b9 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
158ba 63 65 6c 6c 4f 66 66 73 65 74 20 3d 3d 20 68 64  cellOffset == hd
158bb 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 65  r + 12 - 4*pPage
158bc 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 67 61 70 20  ->leaf );.  gap 
158bd 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66  = pPage->cellOff
158be 73 65 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e  set + 2*pPage->n
158bf 43 65 6c 6c 3b 0a 20 20 74 6f 70 20 3d 20 67 65  Cell;.  top = ge
158c0 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72  t2byte(&data[hdr
158c1 2b 35 5d 29 3b 0a 20 20 69 66 28 20 67 61 70 3e  +5]);.  if( gap>
158c2 74 6f 70 20 29 20 72 65 74 75 72 6e 20 53 51 4c  top ) return SQL
158c3 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
158c4 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 67 61  ;.  testcase( ga
158c5 70 2b 32 3d 3d 74 6f 70 20 29 3b 0a 20 20 74 65  p+2==top );.  te
158c6 73 74 63 61 73 65 28 20 67 61 70 2b 31 3d 3d 74  stcase( gap+1==t
158c7 6f 70 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65  op );.  testcase
158c8 28 20 67 61 70 3d 3d 74 6f 70 20 29 3b 0a 0a 20  ( gap==top );.. 
158c9 20 69 66 28 20 6e 46 72 61 67 3e 3d 36 30 20 29   if( nFrag>=60 )
158ca 7b 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20  {.    /* Always 
158cb 64 65 66 72 61 67 6d 65 6e 74 20 68 69 67 68 6c  defragment highl
158cc 79 20 66 72 61 67 6d 65 6e 74 65 64 20 70 61 67  y fragmented pag
158cd 65 73 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 64  es */.    rc = d
158ce 65 66 72 61 67 6d 65 6e 74 50 61 67 65 28 70 50  efragmentPage(pP
158cf 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63  age);.    if( rc
158d0 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
158d1 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65    top = get2byte
158d2 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a  (&data[hdr+5]);.
158d3 20 20 7d 65 6c 73 65 20 69 66 28 20 67 61 70 2b    }else if( gap+
158d4 32 3c 3d 74 6f 70 20 29 7b 0a 20 20 20 20 2f 2a  2<=top ){.    /*
158d5 20 53 65 61 72 63 68 20 74 68 65 20 66 72 65 65   Search the free
158d6 6c 69 73 74 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72  list looking for
158d7 20 61 20 66 72 65 65 20 73 6c 6f 74 20 62 69 67   a free slot big
158d8 20 65 6e 6f 75 67 68 20 74 6f 20 73 61 74 69 73   enough to satis
158d9 66 79 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 72  fy .    ** the r
158da 65 71 75 65 73 74 2e 20 54 68 65 20 61 6c 6c 6f  equest. The allo
158db 63 61 74 69 6f 6e 20 69 73 20 6d 61 64 65 20 66  cation is made f
158dc 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 66 72  rom the first fr
158dd 65 65 20 73 6c 6f 74 20 69 6e 20 0a 20 20 20 20  ee slot in .    
158de 2a 2a 20 74 68 65 20 6c 69 73 74 20 74 68 61 74  ** the list that
158df 20 69 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68   is large enough
158e0 20 74 6f 20 61 63 63 6f 6d 61 64 61 74 65 20 69   to accomadate i
158e1 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e  t..    */.    in
158e2 74 20 70 63 2c 20 61 64 64 72 3b 0a 20 20 20 20  t pc, addr;.    
158e3 66 6f 72 28 61 64 64 72 3d 68 64 72 2b 31 3b 20  for(addr=hdr+1; 
158e4 28 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26  (pc = get2byte(&
158e5 64 61 74 61 5b 61 64 64 72 5d 29 29 3e 30 3b 20  data[addr]))>0; 
158e6 61 64 64 72 3d 70 63 29 7b 0a 20 20 20 20 20 20  addr=pc){.      
158e7 69 6e 74 20 73 69 7a 65 20 3d 20 67 65 74 32 62  int size = get2b
158e8 79 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 29  yte(&data[pc+2])
158e9 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66  ;     /* Size of
158ea 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 20 20   free slot */.  
158eb 20 20 20 20 69 66 28 20 73 69 7a 65 3e 3d 6e 42      if( size>=nB
158ec 79 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69  yte ){.        i
158ed 6e 74 20 78 20 3d 20 73 69 7a 65 20 2d 20 6e 42  nt x = size - nB
158ee 79 74 65 3b 0a 20 20 20 20 20 20 20 20 74 65 73  yte;.        tes
158ef 74 63 61 73 65 28 20 78 3d 3d 34 20 29 3b 0a 20  tcase( x==4 );. 
158f0 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
158f1 20 78 3d 3d 33 20 29 3b 0a 20 20 20 20 20 20 20   x==3 );.       
158f2 20 69 66 28 20 78 3c 34 20 29 7b 0a 20 20 20 20   if( x<4 ){.    
158f3 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20        /* Remove 
158f4 74 68 65 20 73 6c 6f 74 20 66 72 6f 6d 20 74 68  the slot from th
158f5 65 20 66 72 65 65 2d 6c 69 73 74 2e 20 55 70 64  e free-list. Upd
158f6 61 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ate the number o
158f7 66 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66  f.          ** f
158f8 72 61 67 6d 65 6e 74 65 64 20 62 79 74 65 73 20  ragmented bytes 
158f9 77 69 74 68 69 6e 20 74 68 65 20 70 61 67 65 2e  within the page.
158fa 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 6d 65   */.          me
158fb 6d 63 70 79 28 26 64 61 74 61 5b 61 64 64 72 5d  mcpy(&data[addr]
158fc 2c 20 26 64 61 74 61 5b 70 63 5d 2c 20 32 29 3b  , &data[pc], 2);
158fd 0a 20 20 20 20 20 20 20 20 20 20 64 61 74 61 5b  .          data[
158fe 68 64 72 2b 37 5d 20 3d 20 28 75 38 29 28 6e 46  hdr+7] = (u8)(nF
158ff 72 61 67 20 2b 20 78 29 3b 0a 20 20 20 20 20 20  rag + x);.      
15900 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
15901 20 20 20 2f 2a 20 54 68 65 20 73 6c 6f 74 20 72     /* The slot r
15902 65 6d 61 69 6e 73 20 6f 6e 20 74 68 65 20 66 72  emains on the fr
15903 65 65 2d 6c 69 73 74 2e 20 52 65 64 75 63 65 20  ee-list. Reduce 
15904 69 74 73 20 73 69 7a 65 20 74 6f 20 61 63 63 6f  its size to acco
15905 75 6e 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  unt.          **
15906 20 66 6f 72 20 74 68 65 20 70 6f 72 74 69 6f 6e   for the portion
15907 20 75 73 65 64 20 62 79 20 74 68 65 20 6e 65 77   used by the new
15908 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 2a 2f 0a   allocation. */.
15909 20 20 20 20 20 20 20 20 20 20 70 75 74 32 62 79            put2by
1590a 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 2c 20  te(&data[pc+2], 
1590b 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  x);.        }.  
1590c 20 20 20 20 20 20 2a 70 49 64 78 20 3d 20 70 63        *pIdx = pc
1590d 20 2b 20 78 3b 0a 20 20 20 20 20 20 20 20 72 65   + x;.        re
1590e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1590f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
15910 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f  }..  /* Check to
15911 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 72 65   make sure there
15912 20 69 73 20 65 6e 6f 75 67 68 20 73 70 61 63 65   is enough space
15913 20 69 6e 20 74 68 65 20 67 61 70 20 74 6f 20 73   in the gap to s
15914 61 74 69 73 66 79 0a 20 20 2a 2a 20 74 68 65 20  atisfy.  ** the 
15915 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 49 66 20  allocation.  If 
15916 6e 6f 74 2c 20 64 65 66 72 61 67 6d 65 6e 74 2e  not, defragment.
15917 0a 20 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65  .  */.  testcase
15918 28 20 67 61 70 2b 32 2b 6e 42 79 74 65 3d 3d 74  ( gap+2+nByte==t
15919 6f 70 20 29 3b 0a 20 20 69 66 28 20 67 61 70 2b  op );.  if( gap+
1591a 32 2b 6e 42 79 74 65 3e 74 6f 70 20 29 7b 0a 20  2+nByte>top ){. 
1591b 20 20 20 72 63 20 3d 20 64 65 66 72 61 67 6d 65     rc = defragme
1591c 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  ntPage(pPage);. 
1591d 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
1591e 72 6e 20 72 63 3b 0a 20 20 20 20 74 6f 70 20 3d  rn rc;.    top =
1591f 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
15920 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 61 73 73  hdr+5]);.    ass
15921 65 72 74 28 20 67 61 70 2b 6e 42 79 74 65 3c 3d  ert( gap+nByte<=
15922 74 6f 70 20 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f  top );.  }...  /
15923 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  * Allocate memor
15924 79 20 66 72 6f 6d 20 74 68 65 20 67 61 70 20 69  y from the gap i
15925 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 63 65  n between the ce
15926 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79  ll pointer array
15927 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 63 65  .  ** and the ce
15928 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e  ll content area.
15929 20 20 54 68 65 20 62 74 72 65 65 49 6e 69 74 50    The btreeInitP
1592a 61 67 65 28 29 20 63 61 6c 6c 20 68 61 73 20 61  age() call has a
1592b 6c 72 65 61 64 79 0a 20 20 2a 2a 20 76 61 6c 69  lready.  ** vali
1592c 64 61 74 65 64 20 74 68 65 20 66 72 65 65 6c 69  dated the freeli
1592d 73 74 2e 20 20 47 69 76 65 6e 20 74 68 61 74 20  st.  Given that 
1592e 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 73 20  the freelist is 
1592f 76 61 6c 69 64 2c 20 74 68 65 72 65 0a 20 20 2a  valid, there.  *
15930 2a 20 69 73 20 6e 6f 20 77 61 79 20 74 68 61 74  * is no way that
15931 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
15932 63 61 6e 20 65 78 74 65 6e 64 20 6f 66 66 20 74  can extend off t
15933 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61  he end of the pa
15934 67 65 2e 0a 20 20 2a 2a 20 54 68 65 20 61 73 73  ge..  ** The ass
15935 65 72 74 28 29 20 62 65 6c 6f 77 20 76 65 72 69  ert() below veri
15936 66 69 65 73 20 74 68 65 20 70 72 65 76 69 6f 75  fies the previou
15937 73 20 73 65 6e 74 65 6e 63 65 2e 0a 20 20 2a 2f  s sentence..  */
15938 0a 20 20 74 6f 70 20 2d 3d 20 6e 42 79 74 65 3b  .  top -= nByte;
15939 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74  .  put2byte(&dat
1593a 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b 0a  a[hdr+5], top);.
1593b 20 20 61 73 73 65 72 74 28 20 74 6f 70 2b 6e 42    assert( top+nB
1593c 79 74 65 20 3c 3d 20 70 50 61 67 65 2d 3e 70 42  yte <= pPage->pB
1593d 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b  t->usableSize );
1593e 0a 20 20 2a 70 49 64 78 20 3d 20 74 6f 70 3b 0a  .  *pIdx = top;.
1593f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15940 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  OK;.}../*.** Ret
15941 75 72 6e 20 61 20 73 65 63 74 69 6f 6e 20 6f 66  urn a section of
15942 20 74 68 65 20 70 50 61 67 65 2d 3e 61 44 61 74   the pPage->aDat
15943 61 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73  a to the freelis
15944 74 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  t..** The first 
15945 62 79 74 65 20 6f 66 20 74 68 65 20 6e 65 77 20  byte of the new 
15946 66 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 70 50  free block is pP
15947 61 67 65 2d 3e 61 44 69 73 6b 5b 73 74 61 72 74  age->aDisk[start
15948 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 69 7a  ].** and the siz
15949 65 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 20 69  e of the block i
1594a 73 20 22 73 69 7a 65 22 20 62 79 74 65 73 2e 0a  s "size" bytes..
1594b 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68  **.** Most of th
1594c 65 20 65 66 66 6f 72 74 20 68 65 72 65 20 69 73  e effort here is
1594d 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 63 6f 61   involved in coa
1594e 6c 65 73 69 6e 67 20 61 64 6a 61 63 65 6e 74 0a  lesing adjacent.
1594f 2a 2a 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 69  ** free blocks i
15950 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 62 69 67  nto a single big
15951 20 66 72 65 65 20 62 6c 6f 63 6b 2e 0a 2a 2f 0a   free block..*/.
15952 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 65 53  static int freeS
15953 70 61 63 65 28 4d 65 6d 50 61 67 65 20 2a 70 50  pace(MemPage *pP
15954 61 67 65 2c 20 69 6e 74 20 73 74 61 72 74 2c 20  age, int start, 
15955 69 6e 74 20 73 69 7a 65 29 7b 0a 20 20 69 6e 74  int size){.  int
15956 20 61 64 64 72 2c 20 70 62 65 67 69 6e 2c 20 68   addr, pbegin, h
15957 64 72 3b 0a 20 20 69 6e 74 20 69 4c 61 73 74 3b  dr;.  int iLast;
15958 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15959 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 65          /* Large
1595a 73 74 20 70 6f 73 73 69 62 6c 65 20 66 72 65 65  st possible free
1595b 62 6c 6f 63 6b 20 6f 66 66 73 65 74 20 2a 2f 0a  block offset */.
1595c 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
1595d 2a 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61  *data = pPage->a
1595e 44 61 74 61 3b 0a 0a 20 20 61 73 73 65 72 74 28  Data;..  assert(
1595f 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29   pPage->pBt!=0 )
15960 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
15961 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
15962 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ble(pPage->pDbPa
15963 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ge) );.  assert(
15964 20 73 74 61 72 74 3e 3d 70 50 61 67 65 2d 3e 68   start>=pPage->h
15965 64 72 4f 66 66 73 65 74 2b 36 2b 70 50 61 67 65  drOffset+6+pPage
15966 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 20 29  ->childPtrSize )
15967 3b 0a 20 20 61 73 73 65 72 74 28 20 28 73 74 61  ;.  assert( (sta
15968 72 74 20 2b 20 73 69 7a 65 29 3c 3d 70 50 61 67  rt + size)<=pPag
15969 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  e->pBt->usableSi
1596a 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ze );.  assert( 
1596b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1596c 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
1596d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
1596e 74 28 20 73 69 7a 65 3e 3d 30 20 29 3b 20 20 20  t( size>=0 );   
1596f 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20  /* Minimum cell 
15970 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 0a 23 69  size is 4 */..#i
15971 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55  fdef SQLITE_SECU
15972 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f 2a 20 4f  RE_DELETE.  /* O
15973 76 65 72 77 72 69 74 65 20 64 65 6c 65 74 65 64  verwrite deleted
15974 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 74   information wit
15975 68 20 7a 65 72 6f 73 20 77 68 65 6e 20 74 68 65  h zeros when the
15976 20 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20 0a   SECURE_DELETE .
15977 20 20 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 65    ** option is e
15978 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c  nabled at compil
15979 65 2d 74 69 6d 65 20 2a 2f 0a 20 20 6d 65 6d 73  e-time */.  mems
1597a 65 74 28 26 64 61 74 61 5b 73 74 61 72 74 5d 2c  et(&data[start],
1597b 20 30 2c 20 73 69 7a 65 29 3b 0a 23 65 6e 64 69   0, size);.#endi
1597c 66 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20  f..  /* Add the 
1597d 73 70 61 63 65 20 62 61 63 6b 20 69 6e 74 6f 20  space back into 
1597e 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  the linked list 
1597f 6f 66 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20  of freeblocks.  
15980 4e 6f 74 65 20 74 68 61 74 0a 20 20 2a 2a 20 65  Note that.  ** e
15981 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 20 66  ven though the f
15982 72 65 65 62 6c 6f 63 6b 20 6c 69 73 74 20 77 61  reeblock list wa
15983 73 20 63 68 65 63 6b 65 64 20 62 79 20 62 74 72  s checked by btr
15984 65 65 49 6e 69 74 50 61 67 65 28 29 2c 0a 20 20  eeInitPage(),.  
15985 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 61 67 65  ** btreeInitPage
15986 28 29 20 64 69 64 20 6e 6f 74 20 64 65 74 65 63  () did not detec
15987 74 20 6f 76 65 72 6c 61 70 70 69 6e 67 20 63 65  t overlapping ce
15988 6c 6c 73 20 6f 72 0a 20 20 2a 2a 20 66 72 65 65  lls or.  ** free
15989 62 6c 6f 63 6b 73 20 74 68 61 74 20 6f 76 65 72  blocks that over
1598a 6c 61 70 70 65 64 20 63 65 6c 6c 73 2e 20 20 20  lapped cells.   
1598b 4e 6f 72 20 64 6f 65 73 20 69 74 20 64 65 74 65  Nor does it dete
1598c 63 74 20 77 68 65 6e 20 74 68 65 0a 20 20 2a 2a  ct when the.  **
1598d 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72   cell content ar
1598e 65 61 20 65 78 63 65 65 64 73 20 74 68 65 20 76  ea exceeds the v
1598f 61 6c 75 65 20 69 6e 20 74 68 65 20 70 61 67 65  alue in the page
15990 20 68 65 61 64 65 72 2e 20 20 49 66 20 74 68 65   header.  If the
15991 73 65 0a 20 20 2a 2a 20 73 69 74 75 61 74 69 6f  se.  ** situatio
15992 6e 73 20 61 72 69 73 65 2c 20 74 68 65 6e 20 73  ns arise, then s
15993 75 62 73 65 71 75 65 6e 74 20 69 6e 73 65 72 74  ubsequent insert
15994 20 6f 70 65 72 61 74 69 6f 6e 73 20 6d 69 67 68   operations migh
15995 74 20 63 6f 72 72 75 70 74 0a 20 20 2a 2a 20 74  t corrupt.  ** t
15996 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 53 6f  he freelist.  So
15997 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f 20 63   we do need to c
15998 68 65 63 6b 20 66 6f 72 20 63 6f 72 72 75 70 74  heck for corrupt
15999 69 6f 6e 20 77 68 69 6c 65 20 73 63 61 6e 6e 69  ion while scanni
1599a 6e 67 0a 20 20 2a 2a 20 74 68 65 20 66 72 65 65  ng.  ** the free
1599b 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 68 64 72  list..  */.  hdr
1599c 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66   = pPage->hdrOff
1599d 73 65 74 3b 0a 20 20 61 64 64 72 20 3d 20 68 64  set;.  addr = hd
1599e 72 20 2b 20 31 3b 0a 20 20 69 4c 61 73 74 20 3d  r + 1;.  iLast =
1599f 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61   pPage->pBt->usa
159a0 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 61  bleSize - 4;.  a
159a1 73 73 65 72 74 28 20 73 74 61 72 74 3c 3d 69 4c  ssert( start<=iL
159a2 61 73 74 20 29 3b 0a 20 20 77 68 69 6c 65 28 20  ast );.  while( 
159a3 28 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62 79  (pbegin = get2by
159a4 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29 29  te(&data[addr]))
159a5 3c 73 74 61 72 74 20 26 26 20 70 62 65 67 69 6e  <start && pbegin
159a6 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 62  >0 ){.    if( pb
159a7 65 67 69 6e 3c 61 64 64 72 2b 34 20 29 7b 0a 20  egin<addr+4 ){. 
159a8 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
159a9 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
159aa 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 64 72 20  .    }.    addr 
159ab 3d 20 70 62 65 67 69 6e 3b 0a 20 20 7d 0a 20 20  = pbegin;.  }.  
159ac 69 66 28 20 70 62 65 67 69 6e 3e 69 4c 61 73 74  if( pbegin>iLast
159ad 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
159ae 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
159af 50 54 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  PT;.  }.  assert
159b0 28 20 70 62 65 67 69 6e 3e 61 64 64 72 20 7c 7c  ( pbegin>addr ||
159b1 20 70 62 65 67 69 6e 3d 3d 30 20 29 3b 0a 20 20   pbegin==0 );.  
159b2 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 61  put2byte(&data[a
159b3 64 64 72 5d 2c 20 73 74 61 72 74 29 3b 0a 20 20  ddr], start);.  
159b4 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 73  put2byte(&data[s
159b5 74 61 72 74 5d 2c 20 70 62 65 67 69 6e 29 3b 0a  tart], pbegin);.
159b6 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61    put2byte(&data
159b7 5b 73 74 61 72 74 2b 32 5d 2c 20 73 69 7a 65 29  [start+2], size)
159b8 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65  ;.  pPage->nFree
159b9 20 3d 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20   = pPage->nFree 
159ba 2b 20 28 75 31 36 29 73 69 7a 65 3b 0a 0a 20 20  + (u16)size;..  
159bb 2f 2a 20 43 6f 61 6c 65 73 63 65 20 61 64 6a 61  /* Coalesce adja
159bc 63 65 6e 74 20 66 72 65 65 20 62 6c 6f 63 6b 73  cent free blocks
159bd 20 2a 2f 0a 20 20 61 64 64 72 20 3d 20 68 64 72   */.  addr = hdr
159be 20 2b 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 28   + 1;.  while( (
159bf 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62 79 74  pbegin = get2byt
159c0 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29 29 3e  e(&data[addr]))>
159c1 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 70 6e 65  0 ){.    int pne
159c2 78 74 2c 20 70 73 69 7a 65 2c 20 78 3b 0a 20 20  xt, psize, x;.  
159c3 20 20 61 73 73 65 72 74 28 20 70 62 65 67 69 6e    assert( pbegin
159c4 3e 61 64 64 72 20 29 3b 0a 20 20 20 20 61 73 73  >addr );.    ass
159c5 65 72 74 28 20 70 62 65 67 69 6e 3c 3d 70 50 61  ert( pbegin<=pPa
159c6 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53  ge->pBt->usableS
159c7 69 7a 65 2d 34 20 29 3b 0a 20 20 20 20 70 6e 65  ize-4 );.    pne
159c8 78 74 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  xt = get2byte(&d
159c9 61 74 61 5b 70 62 65 67 69 6e 5d 29 3b 0a 20 20  ata[pbegin]);.  
159ca 20 20 70 73 69 7a 65 20 3d 20 67 65 74 32 62 79    psize = get2by
159cb 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 2b  te(&data[pbegin+
159cc 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 62 65  2]);.    if( pbe
159cd 67 69 6e 20 2b 20 70 73 69 7a 65 20 2b 20 33 20  gin + psize + 3 
159ce 3e 3d 20 70 6e 65 78 74 20 26 26 20 70 6e 65 78  >= pnext && pnex
159cf 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  t>0 ){.      int
159d0 20 66 72 61 67 20 3d 20 70 6e 65 78 74 20 2d 20   frag = pnext - 
159d1 28 70 62 65 67 69 6e 2b 70 73 69 7a 65 29 3b 0a  (pbegin+psize);.
159d2 20 20 20 20 20 20 69 66 28 20 28 66 72 61 67 3c        if( (frag<
159d3 30 29 20 7c 7c 20 28 66 72 61 67 3e 28 69 6e 74  0) || (frag>(int
159d4 29 64 61 74 61 5b 68 64 72 2b 37 5d 29 20 29 7b  )data[hdr+7]) ){
159d5 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
159d6 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
159d7 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  KPT;.      }.   
159d8 20 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 2d     data[hdr+7] -
159d9 3d 20 28 75 38 29 66 72 61 67 3b 0a 20 20 20 20  = (u8)frag;.    
159da 20 20 78 20 3d 20 67 65 74 32 62 79 74 65 28 26    x = get2byte(&
159db 64 61 74 61 5b 70 6e 65 78 74 5d 29 3b 0a 20 20  data[pnext]);.  
159dc 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61      put2byte(&da
159dd 74 61 5b 70 62 65 67 69 6e 5d 2c 20 78 29 3b 0a  ta[pbegin], x);.
159de 20 20 20 20 20 20 78 20 3d 20 70 6e 65 78 74 20        x = pnext 
159df 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  + get2byte(&data
159e0 5b 70 6e 65 78 74 2b 32 5d 29 20 2d 20 70 62 65  [pnext+2]) - pbe
159e1 67 69 6e 3b 0a 20 20 20 20 20 20 70 75 74 32 62  gin;.      put2b
159e2 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e  yte(&data[pbegin
159e3 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 7d 65 6c  +2], x);.    }el
159e4 73 65 7b 0a 20 20 20 20 20 20 61 64 64 72 20 3d  se{.      addr =
159e5 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 7d 0a 20   pbegin;.    }. 
159e6 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
159e7 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65  cell content are
159e8 61 20 62 65 67 69 6e 73 20 77 69 74 68 20 61 20  a begins with a 
159e9 66 72 65 65 62 6c 6f 63 6b 2c 20 72 65 6d 6f 76  freeblock, remov
159ea 65 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 64  e it. */.  if( d
159eb 61 74 61 5b 68 64 72 2b 31 5d 3d 3d 64 61 74 61  ata[hdr+1]==data
159ec 5b 68 64 72 2b 35 5d 20 26 26 20 64 61 74 61 5b  [hdr+5] && data[
159ed 68 64 72 2b 32 5d 3d 3d 64 61 74 61 5b 68 64 72  hdr+2]==data[hdr
159ee 2b 36 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20 74  +6] ){.    int t
159ef 6f 70 3b 0a 20 20 20 20 70 62 65 67 69 6e 20 3d  op;.    pbegin =
159f0 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
159f1 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6d 65 6d  hdr+1]);.    mem
159f2 63 70 79 28 26 64 61 74 61 5b 68 64 72 2b 31 5d  cpy(&data[hdr+1]
159f3 2c 20 26 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c  , &data[pbegin],
159f4 20 32 29 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67   2);.    top = g
159f5 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  et2byte(&data[hd
159f6 72 2b 35 5d 29 20 2b 20 67 65 74 32 62 79 74 65  r+5]) + get2byte
159f7 28 26 64 61 74 61 5b 70 62 65 67 69 6e 2b 32 5d  (&data[pbegin+2]
159f8 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65 28  );.    put2byte(
159f9 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 74 6f  &data[hdr+5], to
159fa 70 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  p);.  }.  assert
159fb 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
159fc 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d  writeable(pPage-
159fd 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 72  >pDbPage) );.  r
159fe 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
159ff 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6f 64 65  .}../*.** Decode
15a00 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 20   the flags byte 
15a01 28 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20  (the first byte 
15a02 6f 66 20 74 68 65 20 68 65 61 64 65 72 29 20 66  of the header) f
15a03 6f 72 20 61 20 70 61 67 65 0a 2a 2a 20 61 6e 64  or a page.** and
15a04 20 69 6e 69 74 69 61 6c 69 7a 65 20 66 69 65 6c   initialize fiel
15a05 64 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 61 67  ds of the MemPag
15a06 65 20 73 74 72 75 63 74 75 72 65 20 61 63 63 6f  e structure acco
15a07 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4f  rdingly..**.** O
15a08 6e 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  nly the followin
15a09 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 61  g combinations a
15a0a 72 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20 41  re supported.  A
15a0b 6e 79 74 68 69 6e 67 20 64 69 66 66 65 72 65 6e  nything differen
15a0c 74 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 61  t.** indicates a
15a0d 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73   corrupt databas
15a0e 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  e files:.**.**  
15a0f 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52 4f 44         PTF_ZEROD
15a10 41 54 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 50  ATA.**         P
15a11 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c 20 50 54  TF_ZERODATA | PT
15a12 46 5f 4c 45 41 46 0a 2a 2a 20 20 20 20 20 20 20  F_LEAF.**       
15a13 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 7c    PTF_LEAFDATA |
15a14 20 50 54 46 5f 49 4e 54 4b 45 59 0a 2a 2a 20 20   PTF_INTKEY.**  
15a15 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41 46 44         PTF_LEAFD
15a16 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59  ATA | PTF_INTKEY
15a17 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2f 0a 73   | PTF_LEAF.*/.s
15a18 74 61 74 69 63 20 69 6e 74 20 64 65 63 6f 64 65  tatic int decode
15a19 46 6c 61 67 73 28 4d 65 6d 50 61 67 65 20 2a 70  Flags(MemPage *p
15a1a 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 42 79  Page, int flagBy
15a1b 74 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  te){.  BtShared 
15a1c 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 41 20 63  *pBt;     /* A c
15a1d 6f 70 79 20 6f 66 20 70 50 61 67 65 2d 3e 70 42  opy of pPage->pB
15a1e 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  t */..  assert( 
15a1f 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
15a20 3d 3d 28 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d  ==(pPage->pgno==
15a21 31 20 3f 20 31 30 30 20 3a 20 30 29 20 29 3b 0a  1 ? 100 : 0) );.
15a22 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
15a23 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
15a24 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
15a25 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6c 65 61 66  );.  pPage->leaf
15a26 20 3d 20 28 75 38 29 28 66 6c 61 67 42 79 74 65   = (u8)(flagByte
15a27 3e 3e 33 29 3b 20 20 61 73 73 65 72 74 28 20 50  >>3);  assert( P
15a28 54 46 5f 4c 45 41 46 20 3d 3d 20 31 3c 3c 33 20  TF_LEAF == 1<<3 
15a29 29 3b 0a 20 20 66 6c 61 67 42 79 74 65 20 26 3d  );.  flagByte &=
15a2a 20 7e 50 54 46 5f 4c 45 41 46 3b 0a 20 20 70 50   ~PTF_LEAF;.  pP
15a2b 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a  age->childPtrSiz
15a2c 65 20 3d 20 34 2d 34 2a 70 50 61 67 65 2d 3e 6c  e = 4-4*pPage->l
15a2d 65 61 66 3b 0a 20 20 70 42 74 20 3d 20 70 50 61  eaf;.  pBt = pPa
15a2e 67 65 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20 66  ge->pBt;.  if( f
15a2f 6c 61 67 42 79 74 65 3d 3d 28 50 54 46 5f 4c 45  lagByte==(PTF_LE
15a30 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54  AFDATA | PTF_INT
15a31 4b 45 59 29 20 29 7b 0a 20 20 20 20 70 50 61 67  KEY) ){.    pPag
15a32 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 31 3b 0a 20  e->intKey = 1;. 
15a33 20 20 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74     pPage->hasDat
15a34 61 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 66 3b  a = pPage->leaf;
15a35 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c  .    pPage->maxL
15a36 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c  ocal = pBt->maxL
15a37 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  eaf;.    pPage->
15a38 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e  minLocal = pBt->
15a39 6d 69 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c 73 65  minLeaf;.  }else
15a3a 20 69 66 28 20 66 6c 61 67 42 79 74 65 3d 3d 50   if( flagByte==P
15a3b 54 46 5f 5a 45 52 4f 44 41 54 41 20 29 7b 0a 20  TF_ZERODATA ){. 
15a3c 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79     pPage->intKey
15a3d 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d   = 0;.    pPage-
15a3e 3e 68 61 73 44 61 74 61 20 3d 20 30 3b 0a 20 20  >hasData = 0;.  
15a3f 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61    pPage->maxLoca
15a40 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61  l = pBt->maxLoca
15a41 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 69  l;.    pPage->mi
15a42 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 69  nLocal = pBt->mi
15a43 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 65 6c 73 65 7b  nLocal;.  }else{
15a44 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
15a45 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
15a46 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
15a47 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
15a48 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
15a49 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72   auxiliary infor
15a4a 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 64 69 73  mation for a dis
15a4b 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52  k block..**.** R
15a4c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
15a4d 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66 20  on success.  If 
15a4e 77 65 20 73 65 65 20 74 68 61 74 20 74 68 65 20  we see that the 
15a4f 70 61 67 65 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74  page does.** not
15a50 20 63 6f 6e 74 61 69 6e 20 61 20 77 65 6c 6c 2d   contain a well-
15a51 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65 20  formed database 
15a52 70 61 67 65 2c 20 74 68 65 6e 20 72 65 74 75 72  page, then retur
15a53 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 52  n .** SQLITE_COR
15a54 52 55 50 54 2e 20 20 4e 6f 74 65 20 74 68 61 74  RUPT.  Note that
15a55 20 61 20 72 65 74 75 72 6e 20 6f 66 20 53 51 4c   a return of SQL
15a56 49 54 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f 74 0a  ITE_OK does not.
15a57 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74 68 61  ** guarantee tha
15a58 74 20 74 68 65 20 70 61 67 65 20 69 73 20 77 65  t the page is we
15a59 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20 49 74 20 6f  ll-formed.  It o
15a5a 6e 6c 79 20 73 68 6f 77 73 20 74 68 61 74 0a 2a  nly shows that.*
15a5b 2a 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20 64  * we failed to d
15a5c 65 74 65 63 74 20 61 6e 79 20 63 6f 72 72 75 70  etect any corrup
15a5d 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
15a5e 69 6e 74 20 62 74 72 65 65 49 6e 69 74 50 61 67  int btreeInitPag
15a5f 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  e(MemPage *pPage
15a60 29 7b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  ){..  assert( pP
15a61 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20  age->pBt!=0 );. 
15a62 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15a63 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67  _mutex_held(pPag
15a64 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  e->pBt->mutex) )
15a65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
15a66 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69 74 65 33  e->pgno==sqlite3
15a67 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 28  PagerPagenumber(
15a68 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
15a69 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
15a6a 67 65 20 3d 3d 20 73 71 6c 69 74 65 33 50 61 67  ge == sqlite3Pag
15a6b 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67 65  erGetExtra(pPage
15a6c 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
15a6d 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 61  assert( pPage->a
15a6e 44 61 74 61 20 3d 3d 20 73 71 6c 69 74 65 33 50  Data == sqlite3P
15a6f 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61 67  agerGetData(pPag
15a70 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a  e->pDbPage) );..
15a71 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 73    if( !pPage->is
15a72 49 6e 69 74 20 29 7b 0a 20 20 20 20 75 31 36 20  Init ){.    u16 
15a73 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  pc;            /
15a74 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 20 66  * Address of a f
15a75 72 65 65 62 6c 6f 63 6b 20 77 69 74 68 69 6e 20  reeblock within 
15a76 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 20 2a  pPage->aData[] *
15a77 2f 0a 20 20 20 20 75 38 20 68 64 72 3b 20 20 20  /.    u8 hdr;   
15a78 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
15a79 65 74 20 74 6f 20 62 65 67 69 6e 6e 69 6e 67 20  et to beginning 
15a7a 6f 66 20 70 61 67 65 20 68 65 61 64 65 72 20 2a  of page header *
15a7b 2f 0a 20 20 20 20 75 38 20 2a 64 61 74 61 3b 20  /.    u8 *data; 
15a7c 20 20 20 20 20 20 20 20 20 2f 2a 20 45 71 75 61           /* Equa
15a7d 6c 20 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74  l to pPage->aDat
15a7e 61 20 2a 2f 0a 20 20 20 20 42 74 53 68 61 72 65  a */.    BtShare
15a7f 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 2f  d *pBt;        /
15a80 2a 20 54 68 65 20 6d 61 69 6e 20 62 74 72 65 65  * The main btree
15a81 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20   structure */.  
15a82 20 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a 65    u16 usableSize
15a83 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f  ;    /* Amount o
15a84 66 20 75 73 61 62 6c 65 20 73 70 61 63 65 20 6f  f usable space o
15a85 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20  n each page */. 
15a86 20 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 73 65     u16 cellOffse
15a87 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20  t;    /* Offset 
15a88 66 72 6f 6d 20 73 74 61 72 74 20 6f 66 20 70 61  from start of pa
15a89 67 65 20 74 6f 20 66 69 72 73 74 20 63 65 6c 6c  ge to first cell
15a8a 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20   pointer */.    
15a8b 75 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20 20  u16 nFree;      
15a8c 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
15a8d 75 6e 75 73 65 64 20 62 79 74 65 73 20 6f 6e 20  unused bytes on 
15a8e 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20 20  the page */.    
15a8f 75 31 36 20 74 6f 70 3b 20 20 20 20 20 20 20 20  u16 top;        
15a90 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65     /* First byte
15a91 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e   of the cell con
15a92 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 20  tent area */.   
15a93 20 69 6e 74 20 69 43 65 6c 6c 46 69 72 73 74 3b   int iCellFirst;
15a94 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 6c 6c      /* First all
15a95 6f 77 61 62 6c 65 20 63 65 6c 6c 20 6f 72 20 66  owable cell or f
15a96 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 20  reeblock offset 
15a97 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c  */.    int iCell
15a98 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61 73  Last;     /* Las
15a99 74 20 70 6f 73 73 69 62 6c 65 20 63 65 6c 6c 20  t possible cell 
15a9a 6f 72 20 66 72 65 65 62 6c 6f 63 6b 20 6f 66 66  or freeblock off
15a9b 73 65 74 20 2a 2f 0a 0a 20 20 20 20 70 42 74 20  set */..    pBt 
15a9c 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 0a 20  = pPage->pBt;.. 
15a9d 20 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e     hdr = pPage->
15a9e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 20 64  hdrOffset;.    d
15a9f 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61  ata = pPage->aDa
15aa0 74 61 3b 0a 20 20 20 20 69 66 28 20 64 65 63 6f  ta;.    if( deco
15aa1 64 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20 64  deFlags(pPage, d
15aa2 61 74 61 5b 68 64 72 5d 29 20 29 20 72 65 74 75  ata[hdr]) ) retu
15aa3 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
15aa4 54 5f 42 4b 50 54 3b 0a 20 20 20 20 61 73 73 65  T_BKPT;.    asse
15aa5 72 74 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  rt( pBt->pageSiz
15aa6 65 3e 3d 35 31 32 20 26 26 20 70 42 74 2d 3e 70  e>=512 && pBt->p
15aa7 61 67 65 53 69 7a 65 3c 3d 33 32 37 36 38 20 29  ageSize<=32768 )
15aa8 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 73  ;.    pPage->mas
15aa9 6b 50 61 67 65 20 3d 20 70 42 74 2d 3e 70 61 67  kPage = pBt->pag
15aaa 65 53 69 7a 65 20 2d 20 31 3b 0a 20 20 20 20 70  eSize - 1;.    p
15aab 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20  Page->nOverflow 
15aac 3d 20 30 3b 0a 20 20 20 20 75 73 61 62 6c 65 53  = 0;.    usableS
15aad 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c  ize = pBt->usabl
15aae 65 53 69 7a 65 3b 0a 20 20 20 20 70 50 61 67 65  eSize;.    pPage
15aaf 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 63  ->cellOffset = c
15ab0 65 6c 6c 4f 66 66 73 65 74 20 3d 20 68 64 72 20  ellOffset = hdr 
15ab1 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 65 2d 3e  + 12 - 4*pPage->
15ab2 6c 65 61 66 3b 0a 20 20 20 20 74 6f 70 20 3d 20  leaf;.    top = 
15ab3 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
15ab4 64 72 2b 35 5d 29 3b 0a 20 20 20 20 70 50 61 67  dr+5]);.    pPag
15ab5 65 2d 3e 6e 43 65 6c 6c 20 3d 20 67 65 74 32 62  e->nCell = get2b
15ab6 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d  yte(&data[hdr+3]
15ab7 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  );.    if( pPage
15ab8 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f 43 45 4c 4c 28  ->nCell>MX_CELL(
15ab9 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a  pBt) ){.      /*
15aba 20 54 6f 20 6d 61 6e 79 20 63 65 6c 6c 73 20 66   To many cells f
15abb 6f 72 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65  or a single page
15abc 2e 20 20 54 68 65 20 70 61 67 65 20 6d 75 73 74  .  The page must
15abd 20 62 65 20 63 6f 72 72 75 70 74 20 2a 2f 0a 20   be corrupt */. 
15abe 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
15abf 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
15ac0 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 73 74 63  .    }.    testc
15ac1 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c  ase( pPage->nCel
15ac2 6c 3d 3d 4d 58 5f 43 45 4c 4c 28 70 42 74 29 20  l==MX_CELL(pBt) 
15ac3 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20 6d 61 6c  );..    /* A mal
15ac4 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65 20  formed database 
15ac5 70 61 67 65 20 6d 69 67 68 74 20 63 61 75 73 65  page might cause
15ac6 20 75 73 20 74 6f 20 72 65 61 64 20 70 61 73 74   us to read past
15ac7 20 74 68 65 20 65 6e 64 0a 20 20 20 20 2a 2a 20   the end.    ** 
15ac8 6f 66 20 70 61 67 65 20 77 68 65 6e 20 70 61 72  of page when par
15ac9 73 69 6e 67 20 61 20 63 65 6c 6c 2e 20 20 0a 20  sing a cell.  . 
15aca 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
15acb 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b   following block
15acc 20 6f 66 20 63 6f 64 65 20 63 68 65 63 6b 73 20   of code checks 
15acd 65 61 72 6c 79 20 74 6f 20 73 65 65 20 69 66 20  early to see if 
15ace 61 20 63 65 6c 6c 20 65 78 74 65 6e 64 73 0a 20  a cell extends. 
15acf 20 20 20 2a 2a 20 70 61 73 74 20 74 68 65 20 65     ** past the e
15ad0 6e 64 20 6f 66 20 61 20 70 61 67 65 20 62 6f 75  nd of a page bou
15ad1 6e 64 61 72 79 20 61 6e 64 20 63 61 75 73 65 73  ndary and causes
15ad2 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20   SQLITE_CORRUPT 
15ad3 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a 20 72 65  to be .    ** re
15ad4 74 75 72 6e 65 64 20 69 66 20 69 74 20 64 6f 65  turned if it doe
15ad5 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 43  s..    */.    iC
15ad6 65 6c 6c 46 69 72 73 74 20 3d 20 63 65 6c 6c 4f  ellFirst = cellO
15ad7 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67 65 2d  ffset + 2*pPage-
15ad8 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 69 43 65 6c  >nCell;.    iCel
15ad9 6c 4c 61 73 74 20 3d 20 75 73 61 62 6c 65 53 69  lLast = usableSi
15ada 7a 65 20 2d 20 34 3b 0a 23 69 66 20 64 65 66 69  ze - 4;.#if defi
15adb 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
15adc 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c 5f  E_OVERSIZE_CELL_
15add 43 48 45 43 4b 29 0a 20 20 20 20 7b 0a 20 20 20  CHECK).    {.   
15ade 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20     int i;       
15adf 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
15ae0 74 6f 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e  to the cell poin
15ae1 74 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 20 20  ter array */.   
15ae2 20 20 20 69 6e 74 20 73 7a 3b 20 20 20 20 20 20     int sz;      
15ae3 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
15ae4 61 20 63 65 6c 6c 20 2a 2f 0a 0a 20 20 20 20 20  a cell */..     
15ae5 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
15ae6 66 20 29 20 69 43 65 6c 6c 4c 61 73 74 2d 2d 3b  f ) iCellLast--;
15ae7 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
15ae8 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20  i<pPage->nCell; 
15ae9 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 63  i++){.        pc
15aea 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
15aeb 61 5b 63 65 6c 6c 4f 66 66 73 65 74 2b 69 2a 32  a[cellOffset+i*2
15aec 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74  ]);.        test
15aed 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c 46  case( pc==iCellF
15aee 69 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20  irst );.        
15aef 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69 43  testcase( pc==iC
15af0 65 6c 6c 4c 61 73 74 20 29 3b 0a 20 20 20 20 20  ellLast );.     
15af1 20 20 20 69 66 28 20 70 63 3c 69 43 65 6c 6c 46     if( pc<iCellF
15af2 69 72 73 74 20 7c 7c 20 70 63 3e 69 43 65 6c 6c  irst || pc>iCell
15af3 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Last ){.        
15af4 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15af5 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
15af6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
15af7 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72  sz = cellSizePtr
15af8 28 70 50 61 67 65 2c 20 26 64 61 74 61 5b 70 63  (pPage, &data[pc
15af9 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74  ]);.        test
15afa 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d 75 73 61  case( pc+sz==usa
15afb 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 20  bleSize );.     
15afc 20 20 20 69 66 28 20 70 63 2b 73 7a 3e 75 73 61     if( pc+sz>usa
15afd 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  bleSize ){.     
15afe 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
15aff 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
15b00 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
15b01 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70 50   }.      if( !pP
15b02 61 67 65 2d 3e 6c 65 61 66 20 29 20 69 43 65 6c  age->leaf ) iCel
15b03 6c 4c 61 73 74 2b 2b 3b 0a 20 20 20 20 7d 20 20  lLast++;.    }  
15b04 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
15b05 43 6f 6d 70 75 74 65 20 74 68 65 20 74 6f 74 61  Compute the tota
15b06 6c 20 66 72 65 65 20 73 70 61 63 65 20 6f 6e 20  l free space on 
15b07 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20 20  the page */.    
15b08 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  pc = get2byte(&d
15b09 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20  ata[hdr+1]);.   
15b0a 20 6e 46 72 65 65 20 3d 20 64 61 74 61 5b 68 64   nFree = data[hd
15b0b 72 2b 37 5d 20 2b 20 74 6f 70 3b 0a 20 20 20 20  r+7] + top;.    
15b0c 77 68 69 6c 65 28 20 70 63 3e 30 20 29 7b 0a 20  while( pc>0 ){. 
15b0d 20 20 20 20 20 75 31 36 20 6e 65 78 74 2c 20 73       u16 next, s
15b0e 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ize;.      if( p
15b0f 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20  c<iCellFirst || 
15b10 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b 0a  pc>iCellLast ){.
15b11 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74          /* Start
15b12 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 20 69   of free block i
15b13 73 20 6f 66 66 20 74 68 65 20 70 61 67 65 20 2a  s off the page *
15b14 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  /.        return
15b15 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
15b16 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a 20  BKPT; .      }. 
15b17 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65 74 32       next = get2
15b18 62 79 74 65 28 26 64 61 74 61 5b 70 63 5d 29 3b  byte(&data[pc]);
15b19 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67 65  .      size = ge
15b1a 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 2b  t2byte(&data[pc+
15b1b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28  2]);.      if( (
15b1c 6e 65 78 74 3e 30 20 26 26 20 6e 65 78 74 3c 3d  next>0 && next<=
15b1d 70 63 2b 73 69 7a 65 2b 33 29 20 7c 7c 20 70 63  pc+size+3) || pc
15b1e 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53 69 7a 65  +size>usableSize
15b1f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46   ){.        /* F
15b20 72 65 65 20 62 6c 6f 63 6b 73 20 6d 75 73 74 20  ree blocks must 
15b21 62 65 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20  be in ascending 
15b22 6f 72 64 65 72 2e 20 41 6e 64 20 74 68 65 20 6c  order. And the l
15b23 61 73 74 20 62 79 74 65 20 6f 66 0a 09 2a 2a 20  ast byte of..** 
15b24 74 68 65 20 66 72 65 65 2d 62 6c 6f 63 6b 20 6d  the free-block m
15b25 75 73 74 20 6c 69 65 20 6f 6e 20 74 68 65 20 64  ust lie on the d
15b26 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 20 2a  atabase page.  *
15b27 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  /.        return
15b28 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
15b29 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a 20  BKPT; .      }. 
15b2a 20 20 20 20 20 6e 46 72 65 65 20 3d 20 6e 46 72       nFree = nFr
15b2b 65 65 20 2b 20 73 69 7a 65 3b 0a 20 20 20 20 20  ee + size;.     
15b2c 20 70 63 20 3d 20 6e 65 78 74 3b 0a 20 20 20 20   pc = next;.    
15b2d 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 74 68 69  }..    /* At thi
15b2e 73 20 70 6f 69 6e 74 2c 20 6e 46 72 65 65 20 63  s point, nFree c
15b2f 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 75 6d 20  ontains the sum 
15b30 6f 66 20 74 68 65 20 6f 66 66 73 65 74 20 74 6f  of the offset to
15b31 20 74 68 65 20 73 74 61 72 74 0a 20 20 20 20 2a   the start.    *
15b32 2a 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63 6f  * of the cell-co
15b33 6e 74 65 6e 74 20 61 72 65 61 20 70 6c 75 73 20  ntent area plus 
15b34 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 72  the number of fr
15b35 65 65 20 62 79 74 65 73 20 77 69 74 68 69 6e 0a  ee bytes within.
15b36 20 20 20 20 2a 2a 20 74 68 65 20 63 65 6c 6c 2d      ** the cell-
15b37 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 49 66  content area. If
15b38 20 74 68 69 73 20 69 73 20 67 72 65 61 74 65 72   this is greater
15b39 20 74 68 61 6e 20 74 68 65 20 75 73 61 62 6c 65   than the usable
15b3a 2d 73 69 7a 65 0a 20 20 20 20 2a 2a 20 6f 66 20  -size.    ** of 
15b3b 74 68 65 20 70 61 67 65 2c 20 74 68 65 6e 20 74  the page, then t
15b3c 68 65 20 70 61 67 65 20 6d 75 73 74 20 62 65 20  he page must be 
15b3d 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 73 20  corrupted. This 
15b3e 63 68 65 63 6b 20 61 6c 73 6f 0a 20 20 20 20 2a  check also.    *
15b3f 2a 20 73 65 72 76 65 73 20 74 6f 20 76 65 72 69  * serves to veri
15b40 66 79 20 74 68 61 74 20 74 68 65 20 6f 66 66 73  fy that the offs
15b41 65 74 20 74 6f 20 74 68 65 20 73 74 61 72 74 20  et to the start 
15b42 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63 6f 6e 74  of the cell-cont
15b43 65 6e 74 0a 20 20 20 20 2a 2a 20 61 72 65 61 2c  ent.    ** area,
15b44 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68   according to th
15b45 65 20 70 61 67 65 20 68 65 61 64 65 72 2c 20 6c  e page header, l
15b46 69 65 73 20 77 69 74 68 69 6e 20 74 68 65 20 70  ies within the p
15b47 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  age..    */.    
15b48 69 66 28 20 6e 46 72 65 65 3e 75 73 61 62 6c 65  if( nFree>usable
15b49 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72 65  Size ){.      re
15b4a 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
15b4b 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20 7d  UPT_BKPT; .    }
15b4c 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 72 65  .    pPage->nFre
15b4d 65 20 3d 20 28 75 31 36 29 28 6e 46 72 65 65 20  e = (u16)(nFree 
15b4e 2d 20 69 43 65 6c 6c 46 69 72 73 74 29 3b 0a 20  - iCellFirst);. 
15b4f 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74     pPage->isInit
15b50 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 1;.  }.  retu
15b51 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
15b52 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 61 20  ./*.** Set up a 
15b53 72 61 77 20 70 61 67 65 20 73 6f 20 74 68 61 74  raw page so that
15b54 20 69 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61   it looks like a
15b55 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 68   database page h
15b56 6f 6c 64 69 6e 67 0a 2a 2a 20 6e 6f 20 65 6e 74  olding.** no ent
15b57 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ries..*/.static 
15b58 76 6f 69 64 20 7a 65 72 6f 50 61 67 65 28 4d 65  void zeroPage(Me
15b59 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e  mPage *pPage, in
15b5a 74 20 66 6c 61 67 73 29 7b 0a 20 20 75 6e 73 69  t flags){.  unsi
15b5b 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 20  gned char *data 
15b5c 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a  = pPage->aData;.
15b5d 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
15b5e 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20  = pPage->pBt;.  
15b5f 75 38 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e  u8 hdr = pPage->
15b60 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 75 31 36  hdrOffset;.  u16
15b61 20 66 69 72 73 74 3b 0a 0a 20 20 61 73 73 65 72   first;..  asser
15b62 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 50  t( sqlite3PagerP
15b63 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67 65 2d  agenumber(pPage-
15b64 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65  >pDbPage)==pPage
15b65 2d 3e 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73 65  ->pgno );.  asse
15b66 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
15b67 47 65 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e  GetExtra(pPage->
15b68 70 44 62 50 61 67 65 29 20 3d 3d 20 28 76 6f 69  pDbPage) == (voi
15b69 64 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 61 73  d*)pPage );.  as
15b6a 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
15b6b 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65 2d  erGetData(pPage-
15b6c 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 64 61 74  >pDbPage) == dat
15b6d 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  a );.  assert( s
15b6e 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
15b6f 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44  teable(pPage->pD
15b70 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65  bPage) );.  asse
15b71 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
15b72 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
15b73 78 29 20 29 3b 0a 20 20 2f 2a 6d 65 6d 73 65 74  x) );.  /*memset
15b74 28 26 64 61 74 61 5b 68 64 72 5d 2c 20 30 2c 20  (&data[hdr], 0, 
15b75 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
15b76 2d 20 68 64 72 29 3b 2a 2f 0a 20 20 64 61 74 61  - hdr);*/.  data
15b77 5b 68 64 72 5d 20 3d 20 28 63 68 61 72 29 66 6c  [hdr] = (char)fl
15b78 61 67 73 3b 0a 20 20 66 69 72 73 74 20 3d 20 68  ags;.  first = h
15b79 64 72 20 2b 20 38 20 2b 20 34 2a 28 28 66 6c 61  dr + 8 + 4*((fla
15b7a 67 73 26 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20  gs&PTF_LEAF)==0 
15b7b 3f 31 3a 30 29 3b 0a 20 20 6d 65 6d 73 65 74 28  ?1:0);.  memset(
15b7c 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 30 2c  &data[hdr+1], 0,
15b7d 20 34 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b   4);.  data[hdr+
15b7e 37 5d 20 3d 20 30 3b 0a 20 20 70 75 74 32 62 79  7] = 0;.  put2by
15b7f 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c  te(&data[hdr+5],
15b80 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
15b81 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65  );.  pPage->nFre
15b82 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  e = pBt->usableS
15b83 69 7a 65 20 2d 20 66 69 72 73 74 3b 0a 20 20 64  ize - first;.  d
15b84 65 63 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65  ecodeFlags(pPage
15b85 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 50 61 67  , flags);.  pPag
15b86 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 3d 20 68  e->hdrOffset = h
15b87 64 72 3b 0a 20 20 70 50 61 67 65 2d 3e 63 65 6c  dr;.  pPage->cel
15b88 6c 4f 66 66 73 65 74 20 3d 20 66 69 72 73 74 3b  lOffset = first;
15b89 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  .  pPage->nOverf
15b8a 6c 6f 77 20 3d 20 30 3b 0a 20 20 61 73 73 65 72  low = 0;.  asser
15b8b 74 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  t( pBt->pageSize
15b8c 3e 3d 35 31 32 20 26 26 20 70 42 74 2d 3e 70 61  >=512 && pBt->pa
15b8d 67 65 53 69 7a 65 3c 3d 33 32 37 36 38 20 29 3b  geSize<=32768 );
15b8e 0a 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61  .  pPage->maskPa
15b8f 67 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69  ge = pBt->pageSi
15b90 7a 65 20 2d 20 31 3b 0a 20 20 70 50 61 67 65 2d  ze - 1;.  pPage-
15b91 3e 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20 70 50  >nCell = 0;.  pP
15b92 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b  age->isInit = 1;
15b93 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  .}.../*.** Conve
15b94 72 74 20 61 20 44 62 50 61 67 65 20 6f 62 74 61  rt a DbPage obta
15b95 69 6e 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61  ined from the pa
15b96 67 65 72 20 69 6e 74 6f 20 61 20 4d 65 6d 50 61  ger into a MemPa
15b97 67 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 74 68  ge used by.** th
15b98 65 20 62 74 72 65 65 20 6c 61 79 65 72 2e 0a 2a  e btree layer..*
15b99 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 61 67 65  /.static MemPage
15b9a 20 2a 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44   *btreePageFromD
15b9b 62 50 61 67 65 28 44 62 50 61 67 65 20 2a 70 44  bPage(DbPage *pD
15b9c 62 50 61 67 65 2c 20 50 67 6e 6f 20 70 67 6e 6f  bPage, Pgno pgno
15b9d 2c 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  , BtShared *pBt)
15b9e 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  {.  MemPage *pPa
15b9f 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 73  ge = (MemPage*)s
15ba0 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78  qlite3PagerGetEx
15ba1 74 72 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20  tra(pDbPage);.  
15ba2 70 50 61 67 65 2d 3e 61 44 61 74 61 20 3d 20 73  pPage->aData = s
15ba3 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
15ba4 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70  ta(pDbPage);.  p
15ba5 50 61 67 65 2d 3e 70 44 62 50 61 67 65 20 3d 20  Page->pDbPage = 
15ba6 70 44 62 50 61 67 65 3b 0a 20 20 70 50 61 67 65  pDbPage;.  pPage
15ba7 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 70  ->pBt = pBt;.  p
15ba8 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e  Page->pgno = pgn
15ba9 6f 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64 72 4f  o;.  pPage->hdrO
15baa 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 70  ffset = pPage->p
15bab 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30  gno==1 ? 100 : 0
15bac 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  ;.  return pPage
15bad 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20  ; .}../*.** Get 
15bae 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  a page from the 
15baf 70 61 67 65 72 2e 20 20 49 6e 69 74 69 61 6c 69  pager.  Initiali
15bb0 7a 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e 70  ze the MemPage.p
15bb1 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67  Bt and.** MemPag
15bb2 65 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e 74 73  e.aData elements
15bb3 20 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a   if needed..**.*
15bb4 2a 20 49 66 20 74 68 65 20 6e 6f 43 6f 6e 74 65  * If the noConte
15bb5 6e 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  nt flag is set, 
15bb6 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65  it means that we
15bb7 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f   do not care abo
15bb8 75 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e  ut.** the conten
15bb9 74 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 74  t of the page at
15bba 20 74 68 69 73 20 74 69 6d 65 2e 20 20 53 6f 20   this time.  So 
15bbb 64 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 74 68 65  do not go to the
15bbc 20 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 65 74 63   disk.** to fetc
15bbd 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 20 20  h the content.  
15bbe 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20 74 68 65  Just fill in the
15bbf 20 63 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a 65   content with ze
15bc0 72 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20  ros for now..** 
15bc1 49 66 20 69 6e 20 74 68 65 20 66 75 74 75 72 65  If in the future
15bc2 20 77 65 20 63 61 6c 6c 20 73 71 6c 69 74 65 33   we call sqlite3
15bc3 50 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20  PagerWrite() on 
15bc4 74 68 69 73 20 70 61 67 65 2c 20 74 68 61 74 0a  this page, that.
15bc5 2a 2a 20 6d 65 61 6e 73 20 77 65 20 68 61 76 65  ** means we have
15bc6 20 73 74 61 72 74 65 64 20 74 6f 20 62 65 20 63   started to be c
15bc7 6f 6e 63 65 72 6e 65 64 20 61 62 6f 75 74 20 63  oncerned about c
15bc8 6f 6e 74 65 6e 74 20 61 6e 64 20 74 68 65 20 64  ontent and the d
15bc9 69 73 6b 0a 2a 2a 20 72 65 61 64 20 73 68 6f 75  isk.** read shou
15bca 6c 64 20 6f 63 63 75 72 20 61 74 20 74 68 61 74  ld occur at that
15bcb 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69   point..*/.stati
15bcc 63 20 69 6e 74 20 62 74 72 65 65 47 65 74 50 61  c int btreeGetPa
15bcd 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a  ge(.  BtShared *
15bce 70 42 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  pBt,       /* Th
15bcf 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 50 67 6e  e btree */.  Pgn
15bd0 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20  o pgno,         
15bd1 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
15bd2 68 65 20 70 61 67 65 20 74 6f 20 66 65 74 63 68  he page to fetch
15bd3 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a   */.  MemPage **
15bd4 70 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 52 65  ppPage,    /* Re
15bd5 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 69 6e  turn the page in
15bd6 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 20   this parameter 
15bd7 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65  */.  int noConte
15bd8 6e 74 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20  nt        /* Do 
15bd9 6e 6f 74 20 6c 6f 61 64 20 70 61 67 65 20 63 6f  not load page co
15bda 6e 74 65 6e 74 20 69 66 20 74 72 75 65 20 2a 2f  ntent if true */
15bdb 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
15bdc 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b  DbPage *pDbPage;
15bdd 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
15bde 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
15bdf 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
15be0 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
15be1 72 41 63 71 75 69 72 65 28 70 42 74 2d 3e 70 50  rAcquire(pBt->pP
15be2 61 67 65 72 2c 20 70 67 6e 6f 2c 20 28 44 62 50  ager, pgno, (DbP
15be3 61 67 65 2a 2a 29 26 70 44 62 50 61 67 65 2c 20  age**)&pDbPage, 
15be4 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 66  noContent);.  if
15be5 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
15be6 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 62 74  ;.  *ppPage = bt
15be7 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67  reePageFromDbPag
15be8 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c  e(pDbPage, pgno,
15be9 20 70 42 74 29 3b 0a 20 20 72 65 74 75 72 6e 20   pBt);.  return 
15bea 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
15beb 0a 2a 2a 20 52 65 74 72 69 65 76 65 20 61 20 70  .** Retrieve a p
15bec 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67  age from the pag
15bed 65 72 20 63 61 63 68 65 2e 20 49 66 20 74 68 65  er cache. If the
15bee 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20   requested page 
15bef 69 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64  is not.** alread
15bf0 79 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63  y in the pager c
15bf1 61 63 68 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c  ache return NULL
15bf2 2e 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  . Initialize the
15bf3 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61 6e 64   MemPage.pBt and
15bf4 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44 61 74  .** MemPage.aDat
15bf5 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20 6e 65  a elements if ne
15bf6 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eded..*/.static 
15bf7 4d 65 6d 50 61 67 65 20 2a 62 74 72 65 65 50 61  MemPage *btreePa
15bf8 67 65 4c 6f 6f 6b 75 70 28 42 74 53 68 61 72 65  geLookup(BtShare
15bf9 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e  d *pBt, Pgno pgn
15bfa 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44  o){.  DbPage *pD
15bfb 62 50 61 67 65 3b 0a 20 20 61 73 73 65 72 74 28  bPage;.  assert(
15bfc 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15bfd 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
15bfe 29 3b 0a 20 20 70 44 62 50 61 67 65 20 3d 20 73  );.  pDbPage = s
15bff 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75  qlite3PagerLooku
15c00 70 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70  p(pBt->pPager, p
15c01 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 44 62 50  gno);.  if( pDbP
15c02 61 67 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  age ){.    retur
15c03 6e 20 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44  n btreePageFromD
15c04 62 50 61 67 65 28 70 44 62 50 61 67 65 2c 20 70  bPage(pDbPage, p
15c05 67 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 7d 0a 20  gno, pBt);.  }. 
15c06 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
15c07 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73  .** Return the s
15c08 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ize of the datab
15c09 61 73 65 20 66 69 6c 65 20 69 6e 20 70 61 67 65  ase file in page
15c0a 73 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61  s. If there is a
15c0b 6e 79 20 6b 69 6e 64 20 6f 66 0a 2a 2a 20 65 72  ny kind of.** er
15c0c 72 6f 72 2c 20 72 65 74 75 72 6e 20 28 28 75 6e  ror, return ((un
15c0d 73 69 67 6e 65 64 20 69 6e 74 29 2d 31 29 2e 0a  signed int)-1)..
15c0e 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 70  */.static Pgno p
15c0f 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 42 74  agerPagecount(Bt
15c10 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20  Shared *pBt){.  
15c11 69 6e 74 20 6e 50 61 67 65 20 3d 20 2d 31 3b 0a  int nPage = -1;.
15c12 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65    int rc;.  asse
15c13 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20  rt( pBt->pPage1 
15c14 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
15c15 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
15c16 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50  pBt->pPager, &nP
15c17 61 67 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20  age);.  assert( 
15c18 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
15c19 20 6e 50 61 67 65 3d 3d 2d 31 20 29 3b 0a 20 20   nPage==-1 );.  
15c1a 72 65 74 75 72 6e 20 28 50 67 6e 6f 29 6e 50 61  return (Pgno)nPa
15c1b 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74  ge;.}../*.** Get
15c1c 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65   a page from the
15c1d 20 70 61 67 65 72 20 61 6e 64 20 69 6e 69 74 69   pager and initi
15c1e 61 6c 69 7a 65 20 69 74 2e 20 20 54 68 69 73 20  alize it.  This 
15c1f 72 6f 75 74 69 6e 65 20 69 73 20 6a 75 73 74 20  routine is just 
15c20 61 0a 2a 2a 20 63 6f 6e 76 65 6e 69 65 6e 63 65  a.** convenience
15c21 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20   wrapper around 
15c22 73 65 70 61 72 61 74 65 20 63 61 6c 6c 73 20 74  separate calls t
15c23 6f 20 62 74 72 65 65 47 65 74 50 61 67 65 28 29  o btreeGetPage()
15c24 20 61 6e 64 20 0a 2a 2a 20 62 74 72 65 65 49 6e   and .** btreeIn
15c25 69 74 50 61 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20  itPage()..**.** 
15c26 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
15c27 72 73 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c  rs, then the val
15c28 75 65 20 2a 70 70 50 61 67 65 20 69 73 20 73 65  ue *ppPage is se
15c29 74 20 74 6f 20 69 73 20 75 6e 64 65 66 69 6e 65  t to is undefine
15c2a 64 2e 20 49 74 0a 2a 2a 20 6d 61 79 20 72 65 6d  d. It.** may rem
15c2b 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 2c 20 6f  ain unchanged, o
15c2c 72 20 69 74 20 6d 61 79 20 62 65 20 73 65 74 20  r it may be set 
15c2d 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64 20 76 61  to an invalid va
15c2e 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  lue..*/.static i
15c2f 6e 74 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67  nt getAndInitPag
15c30 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  e(.  BtShared *p
15c31 42 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  Bt,          /* 
15c32 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  The database fil
15c33 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  e */.  Pgno pgno
15c34 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ,           /* N
15c35 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67  umber of the pag
15c36 65 20 74 6f 20 67 65 74 20 2a 2f 0a 20 20 4d 65  e to get */.  Me
15c37 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 20 20  mPage **ppPage  
15c38 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
15c39 70 61 67 65 20 70 6f 69 6e 74 65 72 20 68 65 72  page pointer her
15c3a 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
15c3b 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 50 67  ;.  TESTONLY( Pg
15c3c 6e 6f 20 69 4c 61 73 74 50 67 20 3d 20 70 61 67  no iLastPg = pag
15c3d 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29  erPagecount(pBt)
15c3e 3b 20 29 0a 20 20 61 73 73 65 72 74 28 20 73 71  ; ).  assert( sq
15c3f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
15c40 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
15c41 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74  .  rc = btreeGet
15c42 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20  Page(pBt, pgno, 
15c43 70 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 69 66  ppPage, 0);.  if
15c44 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
15c45 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65  ){.    rc = btre
15c46 65 49 6e 69 74 50 61 67 65 28 2a 70 70 50 61 67  eInitPage(*ppPag
15c47 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  e);.    if( rc!=
15c48 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
15c49 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a     releasePage(*
15c4a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20  ppPage);.    }. 
15c4b 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
15c4c 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 6e  requested page n
15c4d 75 6d 62 65 72 20 77 61 73 20 65 69 74 68 65 72  umber was either
15c4e 20 30 20 6f 72 20 67 72 65 61 74 65 72 20 74 68   0 or greater th
15c4f 61 6e 20 74 68 65 20 70 61 67 65 0a 20 20 2a 2a  an the page.  **
15c50 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6c   number of the l
15c51 61 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20  ast page in the 
15c52 64 61 74 61 62 61 73 65 2c 20 74 68 69 73 20 66  database, this f
15c53 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72  unction should r
15c54 65 74 75 72 6e 0a 20 20 2a 2a 20 53 51 4c 49 54  eturn.  ** SQLIT
15c55 45 5f 43 4f 52 52 55 50 54 20 6f 72 20 73 6f 6d  E_CORRUPT or som
15c56 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 28 69  e other error (i
15c57 2e 65 2e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29  .e. SQLITE_FULL)
15c58 2e 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 69  . Check that thi
15c59 73 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 63 61  s.  ** is the ca
15c5a 73 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  se.  */.  assert
15c5b 28 20 28 70 67 6e 6f 3e 30 20 26 26 20 70 67 6e  ( (pgno>0 && pgn
15c5c 6f 3c 3d 69 4c 61 73 74 50 67 29 20 7c 7c 20 72  o<=iLastPg) || r
15c5d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c!=SQLITE_OK );.
15c5e 20 20 74 65 73 74 63 61 73 65 28 20 70 67 6e 6f    testcase( pgno
15c5f 3d 3d 30 20 29 3b 0a 20 20 74 65 73 74 63 61 73  ==0 );.  testcas
15c60 65 28 20 70 67 6e 6f 3d 3d 69 4c 61 73 74 50 67  e( pgno==iLastPg
15c61 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63   );..  return rc
15c62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61  ;.}../*.** Relea
15c63 73 65 20 61 20 4d 65 6d 50 61 67 65 2e 20 20 54  se a MemPage.  T
15c64 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 63 61  his should be ca
15c65 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61  lled once for ea
15c66 63 68 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c  ch prior.** call
15c67 20 74 6f 20 62 74 72 65 65 47 65 74 50 61 67 65   to btreeGetPage
15c68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
15c69 20 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65 6d   releasePage(Mem
15c6a 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20  Page *pPage){.  
15c6b 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20  if( pPage ){.   
15c6c 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
15c6d 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20  nOverflow==0 || 
15c6e 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
15c6f 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 2d 3e  Refcount(pPage->
15c70 70 44 62 50 61 67 65 29 3e 31 20 29 3b 0a 20 20  pDbPage)>1 );.  
15c71 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
15c72 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73  >aData );.    as
15c73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74  sert( pPage->pBt
15c74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
15c75 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45  sqlite3PagerGetE
15c76 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50  xtra(pPage->pDbP
15c77 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70  age) == (void*)p
15c78 50 61 67 65 20 29 3b 0a 20 20 20 20 61 73 73 65  Page );.    asse
15c79 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
15c7a 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70  GetData(pPage->p
15c7b 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e  DbPage)==pPage->
15c7c 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73  aData );.    ass
15c7d 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
15c7e 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
15c7f 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
15c80 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
15c81 72 65 66 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ref(pPage->pDbPa
15c82 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ge);.  }.}../*.*
15c83 2a 20 44 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62  * During a rollb
15c84 61 63 6b 2c 20 77 68 65 6e 20 74 68 65 20 70 61  ack, when the pa
15c85 67 65 72 20 72 65 6c 6f 61 64 73 20 69 6e 66 6f  ger reloads info
15c86 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65  rmation into the
15c87 20 63 61 63 68 65 0a 2a 2a 20 73 6f 20 74 68 61   cache.** so tha
15c88 74 20 74 68 65 20 63 61 63 68 65 20 69 73 20 72  t the cache is r
15c89 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 6f  estored to its o
15c8a 72 69 67 69 6e 61 6c 20 73 74 61 74 65 20 61 74  riginal state at
15c8b 20 74 68 65 20 73 74 61 72 74 20 6f 66 0a 2a 2a   the start of.**
15c8c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
15c8d 2c 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20  , for each page 
15c8e 72 65 73 74 6f 72 65 64 20 74 68 69 73 20 72 6f  restored this ro
15c8f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e  utine is called.
15c90 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
15c91 69 6e 65 20 6e 65 65 64 73 20 74 6f 20 72 65 73  ine needs to res
15c92 65 74 20 74 68 65 20 65 78 74 72 61 20 64 61 74  et the extra dat
15c93 61 20 73 65 63 74 69 6f 6e 20 61 74 20 74 68 65  a section at the
15c94 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 70   end of the.** p
15c95 61 67 65 20 74 6f 20 61 67 72 65 65 20 77 69 74  age to agree wit
15c96 68 20 74 68 65 20 72 65 73 74 6f 72 65 64 20 64  h the restored d
15c97 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ata..*/.static v
15c98 6f 69 64 20 70 61 67 65 52 65 69 6e 69 74 28 44  oid pageReinit(D
15c99 62 50 61 67 65 20 2a 70 44 61 74 61 29 7b 0a 20  bPage *pData){. 
15c9a 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
15c9b 0a 20 20 70 50 61 67 65 20 3d 20 28 4d 65 6d 50  .  pPage = (MemP
15c9c 61 67 65 20 2a 29 73 71 6c 69 74 65 33 50 61 67  age *)sqlite3Pag
15c9d 65 72 47 65 74 45 78 74 72 61 28 70 44 61 74 61  erGetExtra(pData
15c9e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
15c9f 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66  ite3PagerPageRef
15ca0 63 6f 75 6e 74 28 70 44 61 74 61 29 3e 30 20 29  count(pData)>0 )
15ca1 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69  ;.  if( pPage->i
15ca2 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 61 73 73  sInit ){.    ass
15ca3 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
15ca4 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
15ca5 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
15ca6 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20    pPage->isInit 
15ca7 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  = 0;.    if( sql
15ca8 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66  ite3PagerPageRef
15ca9 63 6f 75 6e 74 28 70 44 61 74 61 29 3e 31 20 29  count(pData)>1 )
15caa 7b 0a 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65  {.      /* pPage
15cab 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 61 20   might not be a 
15cac 62 74 72 65 65 20 70 61 67 65 3b 20 20 69 74 20  btree page;  it 
15cad 6d 69 67 68 74 20 62 65 20 61 6e 20 6f 76 65 72  might be an over
15cae 66 6c 6f 77 20 70 61 67 65 0a 20 20 20 20 20 20  flow page.      
15caf 2a 2a 20 6f 72 20 70 74 72 6d 61 70 20 70 61 67  ** or ptrmap pag
15cb0 65 20 6f 72 20 61 20 66 72 65 65 20 70 61 67 65  e or a free page
15cb1 2e 20 20 49 6e 20 74 68 6f 73 65 20 63 61 73 65  .  In those case
15cb2 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  s, the following
15cb3 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 20 74  .      ** call t
15cb4 6f 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28  o btreeInitPage(
15cb5 29 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 72 65  ) will likely re
15cb6 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
15cb7 55 50 54 2e 0a 20 20 20 20 20 20 2a 2a 20 42 75  UPT..      ** Bu
15cb8 74 20 6e 6f 20 68 61 72 6d 20 69 73 20 64 6f 6e  t no harm is don
15cb9 65 20 62 79 20 74 68 69 73 2e 20 20 41 6e 64 20  e by this.  And 
15cba 69 74 20 69 73 20 76 65 72 79 20 69 6d 70 6f 72  it is very impor
15cbb 74 61 6e 74 20 74 68 61 74 0a 20 20 20 20 20 20  tant that.      
15cbc 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 61 67 65  ** btreeInitPage
15cbd 28 29 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20  () be called on 
15cbe 65 76 65 72 79 20 62 74 72 65 65 20 70 61 67 65  every btree page
15cbf 20 73 6f 20 77 65 20 6d 61 6b 65 0a 20 20 20 20   so we make.    
15cc0 20 20 2a 2a 20 74 68 65 20 63 61 6c 6c 20 66 6f    ** the call fo
15cc1 72 20 65 76 65 72 79 20 70 61 67 65 20 74 68 61  r every page tha
15cc2 74 20 63 6f 6d 65 73 20 69 6e 20 66 6f 72 20 72  t comes in for r
15cc3 65 2d 69 6e 69 74 69 6e 67 2e 20 2a 2f 0a 20 20  e-initing. */.  
15cc4 20 20 20 20 62 74 72 65 65 49 6e 69 74 50 61 67      btreeInitPag
15cc5 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a  e(pPage);.    }.
15cc6 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76    }.}../*.** Inv
15cc7 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 6e  oke the busy han
15cc8 64 6c 65 72 20 66 6f 72 20 61 20 62 74 72 65 65  dler for a btree
15cc9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
15cca 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48  btreeInvokeBusyH
15ccb 61 6e 64 6c 65 72 28 76 6f 69 64 20 2a 70 41 72  andler(void *pAr
15ccc 67 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  g){.  BtShared *
15ccd 70 42 74 20 3d 20 28 42 74 53 68 61 72 65 64 2a  pBt = (BtShared*
15cce 29 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74 28  )pArg;.  assert(
15ccf 20 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20 61 73   pBt->db );.  as
15cd0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
15cd1 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 64 62  tex_held(pBt->db
15cd2 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65  ->mutex) );.  re
15cd3 74 75 72 6e 20 73 71 6c 69 74 65 33 49 6e 76 6f  turn sqlite3Invo
15cd4 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 26 70  keBusyHandler(&p
15cd5 42 74 2d 3e 64 62 2d 3e 62 75 73 79 48 61 6e 64  Bt->db->busyHand
15cd6 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f  ler);.}../*.** O
15cd7 70 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 66  pen a database f
15cd8 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a 46 69 6c  ile..** .** zFil
15cd9 65 6e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d  ename is the nam
15cda 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
15cdb 65 20 66 69 6c 65 2e 20 20 49 66 20 7a 46 69 6c  e file.  If zFil
15cdc 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 0a 2a 2a  ename is NULL.**
15cdd 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20   a new database 
15cde 77 69 74 68 20 61 20 72 61 6e 64 6f 6d 20 6e 61  with a random na
15cdf 6d 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 20  me is created.  
15ce0 54 68 69 73 20 72 61 6e 64 6f 6d 6c 79 20 6e 61  This randomly na
15ce1 6d 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  med.** database 
15ce2 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 64 65 6c  file will be del
15ce3 65 74 65 64 20 77 68 65 6e 20 73 71 6c 69 74 65  eted when sqlite
15ce4 33 42 74 72 65 65 43 6c 6f 73 65 28 29 20 69 73  3BtreeClose() is
15ce5 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 49 66 20 7a   called..** If z
15ce6 46 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65  Filename is ":me
15ce7 6d 6f 72 79 3a 22 20 74 68 65 6e 20 61 6e 20 69  mory:" then an i
15ce8 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
15ce9 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20  e is created.** 
15cea 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61 74 69  that is automati
15ceb 63 61 6c 6c 79 20 64 65 73 74 72 6f 79 65 64 20  cally destroyed 
15cec 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65  when it is close
15ced 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
15cee 64 61 74 61 62 61 73 65 20 69 73 20 61 6c 72 65  database is alre
15cef 61 64 79 20 6f 70 65 6e 65 64 20 69 6e 20 74 68  ady opened in th
15cf0 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
15cf1 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 6e  connection.** an
15cf2 64 20 77 65 20 61 72 65 20 69 6e 20 73 68 61 72  d we are in shar
15cf3 65 64 20 63 61 63 68 65 20 6d 6f 64 65 2c 20 74  ed cache mode, t
15cf4 68 65 6e 20 74 68 65 20 6f 70 65 6e 20 77 69 6c  hen the open wil
15cf5 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 0a 2a  l fail with an.*
15cf6 2a 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  * SQLITE_CONSTRA
15cf7 49 4e 54 20 65 72 72 6f 72 2e 20 20 57 65 20 63  INT error.  We c
15cf8 61 6e 6e 6f 74 20 61 6c 6c 6f 77 20 74 77 6f 20  annot allow two 
15cf9 6f 72 20 6d 6f 72 65 20 42 74 53 68 61 72 65 64  or more BtShared
15cfa 0a 2a 2a 20 6f 62 6a 65 63 74 73 20 69 6e 20 74  .** objects in t
15cfb 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
15cfc 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 69 6e 63   connection sinc
15cfd 65 20 64 6f 69 6e 67 20 73 6f 20 77 69 6c 6c 20  e doing so will 
15cfe 6c 65 61 64 0a 2a 2a 20 74 6f 20 70 72 6f 62 6c  lead.** to probl
15cff 65 6d 73 20 77 69 74 68 20 6c 6f 63 6b 69 6e 67  ems with locking
15d00 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
15d01 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
15d02 74 72 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73  treeOpen(.  cons
15d03 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
15d04 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74  e,  /* Name of t
15d05 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69  he file containi
15d06 6e 67 20 74 68 65 20 42 54 72 65 65 20 64 61 74  ng the BTree dat
15d07 61 62 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74  abase */.  sqlit
15d08 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
15d09 20 20 20 2f 2a 20 41 73 73 6f 63 69 61 74 65 64     /* Associated
15d0a 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
15d0b 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70   */.  Btree **pp
15d0c 42 74 72 65 65 2c 20 20 20 20 20 20 20 20 2f 2a  Btree,        /*
15d0d 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77 20   Pointer to new 
15d0e 42 74 72 65 65 20 6f 62 6a 65 63 74 20 77 72 69  Btree object wri
15d0f 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69  tten here */.  i
15d10 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nt flags,       
15d11 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e         /* Option
15d12 73 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c  s */.  int vfsFl
15d13 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f  ags            /
15d14 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74  * Flags passed t
15d15 68 72 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65  hrough to sqlite
15d16 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f  3_vfs.xOpen() */
15d17 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  .){.  sqlite3_vf
15d18 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20  s *pVfs;        
15d19 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20       /* The VFS 
15d1a 74 6f 20 75 73 65 20 66 6f 72 20 74 68 69 73 20  to use for this 
15d1b 62 74 72 65 65 20 2a 2f 0a 20 20 42 74 53 68 61  btree */.  BtSha
15d1c 72 65 64 20 2a 70 42 74 20 3d 20 30 3b 20 20 20  red *pBt = 0;   
15d1d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61            /* Sha
15d1e 72 65 64 20 70 61 72 74 20 6f 66 20 62 74 72 65  red part of btre
15d1f 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  e structure */. 
15d20 20 42 74 72 65 65 20 2a 70 3b 20 20 20 20 20 20   Btree *p;      
15d21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15d22 2f 2a 20 48 61 6e 64 6c 65 20 74 6f 20 72 65 74  /* Handle to ret
15d23 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  urn */.  sqlite3
15d24 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 4f 70 65  _mutex *mutexOpe
15d25 6e 20 3d 20 30 3b 20 20 2f 2a 20 50 72 65 76 65  n = 0;  /* Preve
15d26 6e 74 73 20 61 20 72 61 63 65 20 63 6f 6e 64 69  nts a race condi
15d27 74 69 6f 6e 2e 20 54 69 63 6b 65 74 20 23 33 35  tion. Ticket #35
15d28 33 37 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d  37 */.  int rc =
15d29 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
15d2a 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
15d2b 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 69 73 20   code from this 
15d2c 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38  function */.  u8
15d2d 20 6e 52 65 73 65 72 76 65 3b 20 20 20 20 20 20   nReserve;      
15d2e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
15d2f 42 79 74 65 20 6f 66 20 75 6e 75 73 65 64 20 73  Byte of unused s
15d30 70 61 63 65 20 6f 6e 20 65 61 63 68 20 70 61 67  pace on each pag
15d31 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  e */.  unsigned 
15d32 63 68 61 72 20 7a 44 62 48 65 61 64 65 72 5b 31  char zDbHeader[1
15d33 30 30 5d 3b 20 20 2f 2a 20 44 61 74 61 62 61 73  00];  /* Databas
15d34 65 20 68 65 61 64 65 72 20 63 6f 6e 74 65 6e 74  e header content
15d35 20 2a 2f 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68   */..  /* Set th
15d36 65 20 76 61 72 69 61 62 6c 65 20 69 73 4d 65 6d  e variable isMem
15d37 64 62 20 74 6f 20 74 72 75 65 20 66 6f 72 20 61  db to true for a
15d38 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  n in-memory data
15d39 62 61 73 65 2c 20 6f 72 20 0a 20 20 2a 2a 20 66  base, or .  ** f
15d3a 61 6c 73 65 20 66 6f 72 20 61 20 66 69 6c 65 2d  alse for a file-
15d3b 62 61 73 65 64 20 64 61 74 61 62 61 73 65 2e 20  based database. 
15d3c 54 68 69 73 20 73 79 6d 62 6f 6c 20 69 73 20 6f  This symbol is o
15d3d 6e 6c 79 20 72 65 71 75 69 72 65 64 20 69 66 0a  nly required if.
15d3e 20 20 2a 2a 20 65 69 74 68 65 72 20 6f 66 20 74    ** either of t
15d3f 68 65 20 73 68 61 72 65 64 2d 64 61 74 61 20 6f  he shared-data o
15d40 72 20 61 75 74 6f 76 61 63 75 75 6d 20 66 65 61  r autovacuum fea
15d41 74 75 72 65 73 20 61 72 65 20 63 6f 6d 70 69 6c  tures are compil
15d42 65 64 20 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68  ed .  ** into th
15d43 65 20 6c 69 62 72 61 72 79 2e 0a 20 20 2a 2f 0a  e library..  */.
15d44 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
15d45 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
15d46 43 41 43 48 45 29 20 7c 7c 20 21 64 65 66 69 6e  CACHE) || !defin
15d47 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ed(SQLITE_OMIT_A
15d48 55 54 4f 56 41 43 55 55 4d 29 0a 20 20 23 69 66  UTOVACUUM).  #if
15d49 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
15d4a 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 63 6f 6e  MEMORYDB.    con
15d4b 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 20 3d  st int isMemdb =
15d4c 20 30 3b 0a 20 20 23 65 6c 73 65 0a 20 20 20 20   0;.  #else.    
15d4d 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64  const int isMemd
15d4e 62 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26  b = zFilename &&
15d4f 20 21 73 74 72 63 6d 70 28 7a 46 69 6c 65 6e 61   !strcmp(zFilena
15d50 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a 22 29 3b  me, ":memory:");
15d51 0a 20 20 23 65 6e 64 69 66 0a 23 65 6e 64 69 66  .  #endif.#endif
15d52 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d  ..  assert( db!=
15d53 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  0 );.  assert( s
15d54 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
15d55 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
15d56 0a 20 20 70 56 66 73 20 3d 20 64 62 2d 3e 70 56  .  pVfs = db->pV
15d57 66 73 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65  fs;.  p = sqlite
15d58 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65  3MallocZero(size
15d59 6f 66 28 42 74 72 65 65 29 29 3b 0a 20 20 69 66  of(Btree));.  if
15d5a 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75  ( !p ){.    retu
15d5b 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
15d5c 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72 61 6e  .  }.  p->inTran
15d5d 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a  s = TRANS_NONE;.
15d5e 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 23 69    p->db = db;.#i
15d5f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
15d60 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20  T_SHARED_CACHE. 
15d61 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 20   p->lock.pBtree 
15d62 3d 20 70 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 69  = p;.  p->lock.i
15d63 54 61 62 6c 65 20 3d 20 31 3b 0a 23 65 6e 64 69  Table = 1;.#endi
15d64 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  f..#if !defined(
15d65 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
15d66 45 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65  ED_CACHE) && !de
15d67 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
15d68 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20  T_DISKIO).  /*. 
15d69 20 2a 2a 20 49 66 20 74 68 69 73 20 42 74 72 65   ** If this Btre
15d6a 65 20 69 73 20 61 20 63 61 6e 64 69 64 61 74 65  e is a candidate
15d6b 20 66 6f 72 20 73 68 61 72 65 64 20 63 61 63 68   for shared cach
15d6c 65 2c 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61  e, try to find a
15d6d 6e 0a 20 20 2a 2a 20 65 78 69 73 74 69 6e 67 20  n.  ** existing 
15d6e 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20  BtShared object 
15d6f 74 68 61 74 20 77 65 20 63 61 6e 20 73 68 61 72  that we can shar
15d70 65 20 77 69 74 68 0a 20 20 2a 2f 0a 20 20 69 66  e with.  */.  if
15d71 28 20 69 73 4d 65 6d 64 62 3d 3d 30 20 26 26 20  ( isMemdb==0 && 
15d72 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69  zFilename && zFi
15d73 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b 0a 20 20 20  lename[0] ){.   
15d74 20 69 66 28 20 76 66 73 46 6c 61 67 73 20 26 20   if( vfsFlags & 
15d75 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52  SQLITE_OPEN_SHAR
15d76 45 44 43 41 43 48 45 20 29 7b 0a 20 20 20 20 20  EDCACHE ){.     
15d77 20 69 6e 74 20 6e 46 75 6c 6c 50 61 74 68 6e 61   int nFullPathna
15d78 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74  me = pVfs->mxPat
15d79 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 20 20 63  hname+1;.      c
15d7a 68 61 72 20 2a 7a 46 75 6c 6c 50 61 74 68 6e 61  har *zFullPathna
15d7b 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  me = sqlite3Mall
15d7c 6f 63 28 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65  oc(nFullPathname
15d7d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
15d7e 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 68 61  _mutex *mutexSha
15d7f 72 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e 73 68  red;.      p->sh
15d80 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20  arable = 1;.    
15d81 20 20 69 66 28 20 21 7a 46 75 6c 6c 50 61 74 68    if( !zFullPath
15d82 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20  name ){.        
15d83 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
15d84 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
15d85 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
15d86 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
15d87 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d  te3OsFullPathnam
15d88 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d  e(pVfs, zFilenam
15d89 65 2c 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65  e, nFullPathname
15d8a 2c 20 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29  , zFullPathname)
15d8b 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 4f 70 65  ;.      mutexOpe
15d8c 6e 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78  n = sqlite3Mutex
15d8d 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
15d8e 45 58 5f 53 54 41 54 49 43 5f 4f 50 45 4e 29 3b  EX_STATIC_OPEN);
15d8f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
15d90 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78  utex_enter(mutex
15d91 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 6d 75 74  Open);.      mut
15d92 65 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69 74  exShared = sqlit
15d93 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
15d94 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
15d95 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20 20  _MASTER);.      
15d96 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
15d97 74 65 72 28 6d 75 74 65 78 53 68 61 72 65 64 29  ter(mutexShared)
15d98 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 42 74 3d  ;.      for(pBt=
15d99 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a  GLOBAL(BtShared*
15d9a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61  ,sqlite3SharedCa
15d9b 63 68 65 4c 69 73 74 29 3b 20 70 42 74 3b 20 70  cheList); pBt; p
15d9c 42 74 3d 70 42 74 2d 3e 70 4e 65 78 74 29 7b 0a  Bt=pBt->pNext){.
15d9d 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
15d9e 70 42 74 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  pBt->nRef>0 );. 
15d9f 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73 74         if( 0==st
15da0 72 63 6d 70 28 7a 46 75 6c 6c 50 61 74 68 6e 61  rcmp(zFullPathna
15da1 6d 65 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72  me, sqlite3Pager
15da2 46 69 6c 65 6e 61 6d 65 28 70 42 74 2d 3e 70 50  Filename(pBt->pP
15da3 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 20  ager)).         
15da4 20 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74          && sqlit
15da5 65 33 50 61 67 65 72 56 66 73 28 70 42 74 2d 3e  e3PagerVfs(pBt->
15da6 70 50 61 67 65 72 29 3d 3d 70 56 66 73 20 29 7b  pPager)==pVfs ){
15da7 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69  .          int i
15da8 44 62 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f  Db;.          fo
15da9 72 28 69 44 62 3d 64 62 2d 3e 6e 44 62 2d 31 3b  r(iDb=db->nDb-1;
15daa 20 69 44 62 3e 3d 30 3b 20 69 44 62 2d 2d 29 7b   iDb>=0; iDb--){
15dab 0a 20 20 20 20 20 20 20 20 20 20 20 20 42 74 72  .            Btr
15dac 65 65 20 2a 70 45 78 69 73 74 69 6e 67 20 3d 20  ee *pExisting = 
15dad 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74  db->aDb[iDb].pBt
15dae 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
15daf 28 20 70 45 78 69 73 74 69 6e 67 20 26 26 20 70  ( pExisting && p
15db0 45 78 69 73 74 69 6e 67 2d 3e 70 42 74 3d 3d 70  Existing->pBt==p
15db1 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  Bt ){.          
15db2 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
15db3 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61  x_leave(mutexSha
15db4 72 65 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20  red);.          
15db5 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
15db6 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 4f 70 65  x_leave(mutexOpe
15db7 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  n);.            
15db8 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
15db9 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20  FullPathname);. 
15dba 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
15dbb 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20  ite3_free(p);.  
15dbc 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75              retu
15dbd 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  rn SQLITE_CONSTR
15dbe 41 49 4e 54 3b 0a 20 20 20 20 20 20 20 20 20 20  AINT;.          
15dbf 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
15dc0 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 42 74            p->pBt
15dc1 20 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 20 20   = pBt;.        
15dc2 20 20 70 42 74 2d 3e 6e 52 65 66 2b 2b 3b 0a 20    pBt->nRef++;. 
15dc3 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
15dc4 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
15dc5 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  }.      sqlite3_
15dc6 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
15dc7 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20  xShared);.      
15dc8 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 75  sqlite3_free(zFu
15dc9 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20  llPathname);.   
15dca 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45   }.#ifdef SQLITE
15dcb 5f 44 45 42 55 47 0a 20 20 20 20 65 6c 73 65 7b  _DEBUG.    else{
15dcc 0a 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 65 62  .      /* In deb
15dcd 75 67 20 6d 6f 64 65 2c 20 77 65 20 6d 61 72 6b  ug mode, we mark
15dce 20 61 6c 6c 20 70 65 72 73 69 73 74 65 6e 74 20   all persistent 
15dcf 64 61 74 61 62 61 73 65 73 20 61 73 20 73 68 61  databases as sha
15dd0 72 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 65  rable.      ** e
15dd1 76 65 6e 20 77 68 65 6e 20 74 68 65 79 20 61 72  ven when they ar
15dd2 65 20 6e 6f 74 2e 20 20 54 68 69 73 20 65 78 65  e not.  This exe
15dd3 72 63 69 73 65 73 20 74 68 65 20 6c 6f 63 6b 69  rcises the locki
15dd4 6e 67 20 63 6f 64 65 20 61 6e 64 0a 20 20 20 20  ng code and.    
15dd5 20 20 2a 2a 20 67 69 76 65 73 20 6d 6f 72 65 20    ** gives more 
15dd6 6f 70 70 6f 72 74 75 6e 69 74 79 20 66 6f 72 20  opportunity for 
15dd7 61 73 73 65 72 74 73 28 73 71 6c 69 74 65 33 5f  asserts(sqlite3_
15dd8 6d 75 74 65 78 5f 68 65 6c 64 28 29 29 0a 20 20  mutex_held()).  
15dd9 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74      ** statement
15dda 73 20 74 6f 20 66 69 6e 64 20 6c 6f 63 6b 69 6e  s to find lockin
15ddb 67 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 20 20  g problems..    
15ddc 20 20 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 73 68    */.      p->sh
15ddd 61 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20  arable = 1;.    
15dde 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23 65 6e  }.#endif.  }.#en
15ddf 64 69 66 0a 20 20 69 66 28 20 70 42 74 3d 3d 30  dif.  if( pBt==0
15de0 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a   ){.    /*.    *
15de1 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
15de2 61 73 73 65 72 74 73 20 6d 61 6b 65 20 73 75 72  asserts make sur
15de3 65 20 74 68 61 74 20 73 74 72 75 63 74 75 72 65  e that structure
15de4 73 20 75 73 65 64 20 62 79 20 74 68 65 20 62 74  s used by the bt
15de5 72 65 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 74  ree are.    ** t
15de6 68 65 20 72 69 67 68 74 20 73 69 7a 65 2e 20 20  he right size.  
15de7 54 68 69 73 20 69 73 20 74 6f 20 67 75 61 72 64  This is to guard
15de8 20 61 67 61 69 6e 73 74 20 73 69 7a 65 20 63 68   against size ch
15de9 61 6e 67 65 73 20 74 68 61 74 20 72 65 73 75 6c  anges that resul
15dea 74 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 63 6f  t.    ** when co
15deb 6d 70 69 6c 69 6e 67 20 6f 6e 20 61 20 64 69 66  mpiling on a dif
15dec 66 65 72 65 6e 74 20 61 72 63 68 69 74 65 63 74  ferent architect
15ded 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ure..    */.    
15dee 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 69  assert( sizeof(i
15def 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65 6f 66  64)==8 || sizeof
15df0 28 69 36 34 29 3d 3d 34 20 29 3b 0a 20 20 20 20  (i64)==4 );.    
15df1 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75  assert( sizeof(u
15df2 36 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65 6f 66  64)==8 || sizeof
15df3 28 75 36 34 29 3d 3d 34 20 29 3b 0a 20 20 20 20  (u64)==4 );.    
15df4 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75  assert( sizeof(u
15df5 33 32 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73  32)==4 );.    as
15df6 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 31 36  sert( sizeof(u16
15df7 29 3d 3d 32 20 29 3b 0a 20 20 20 20 61 73 73 65  )==2 );.    asse
15df8 72 74 28 20 73 69 7a 65 6f 66 28 50 67 6e 6f 29  rt( sizeof(Pgno)
15df9 3d 3d 34 20 29 3b 0a 20 20 0a 20 20 20 20 70 42  ==4 );.  .    pB
15dfa 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  t = sqlite3Mallo
15dfb 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70  cZero( sizeof(*p
15dfc 42 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70  Bt) );.    if( p
15dfd 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  Bt==0 ){.      r
15dfe 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
15dff 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72  ;.      goto btr
15e00 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20  ee_open_out;.   
15e01 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69   }.    rc = sqli
15e02 74 65 33 50 61 67 65 72 4f 70 65 6e 28 70 56 66  te3PagerOpen(pVf
15e03 73 2c 20 26 70 42 74 2d 3e 70 50 61 67 65 72 2c  s, &pBt->pPager,
15e04 20 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20   zFilename,.    
15e05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15e06 20 20 20 20 20 20 45 58 54 52 41 5f 53 49 5a 45        EXTRA_SIZE
15e07 2c 20 66 6c 61 67 73 2c 20 76 66 73 46 6c 61 67  , flags, vfsFlag
15e08 73 2c 20 70 61 67 65 52 65 69 6e 69 74 29 3b 0a  s, pageReinit);.
15e09 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
15e0a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
15e0b 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
15e0c 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 70  ReadFileheader(p
15e0d 42 74 2d 3e 70 50 61 67 65 72 2c 73 69 7a 65 6f  Bt->pPager,sizeo
15e0e 66 28 7a 44 62 48 65 61 64 65 72 29 2c 7a 44 62  f(zDbHeader),zDb
15e0f 48 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a 20  Header);.    }. 
15e10 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
15e11 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f  E_OK ){.      go
15e12 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75  to btree_open_ou
15e13 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74  t;.    }.    pBt
15e14 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 73  ->db = db;.    s
15e15 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 42 75  qlite3PagerSetBu
15e16 73 79 68 61 6e 64 6c 65 72 28 70 42 74 2d 3e 70  syhandler(pBt->p
15e17 50 61 67 65 72 2c 20 62 74 72 65 65 49 6e 76 6f  Pager, btreeInvo
15e18 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 2c 20 70  keBusyHandler, p
15e19 42 74 29 3b 0a 20 20 20 20 70 2d 3e 70 42 74 20  Bt);.    p->pBt 
15e1a 3d 20 70 42 74 3b 0a 20 20 0a 20 20 20 20 70 42  = pBt;.  .    pB
15e1b 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a  t->pCursor = 0;.
15e1c 20 20 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20      pBt->pPage1 
15e1d 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 72 65  = 0;.    pBt->re
15e1e 61 64 4f 6e 6c 79 20 3d 20 73 71 6c 69 74 65 33  adOnly = sqlite3
15e1f 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28  PagerIsreadonly(
15e20 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20  pBt->pPager);.  
15e21 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20    pBt->pageSize 
15e22 3d 20 67 65 74 32 62 79 74 65 28 26 7a 44 62 48  = get2byte(&zDbH
15e23 65 61 64 65 72 5b 31 36 5d 29 3b 0a 20 20 20 20  eader[16]);.    
15e24 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  if( pBt->pageSiz
15e25 65 3c 35 31 32 20 7c 7c 20 70 42 74 2d 3e 70 61  e<512 || pBt->pa
15e26 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41  geSize>SQLITE_MA
15e27 58 5f 50 41 47 45 5f 53 49 5a 45 0a 20 20 20 20  X_PAGE_SIZE.    
15e28 20 20 20 20 20 7c 7c 20 28 28 70 42 74 2d 3e 70       || ((pBt->p
15e29 61 67 65 53 69 7a 65 2d 31 29 26 70 42 74 2d 3e  ageSize-1)&pBt->
15e2a 70 61 67 65 53 69 7a 65 29 21 3d 30 20 29 7b 0a  pageSize)!=0 ){.
15e2b 20 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53        pBt->pageS
15e2c 69 7a 65 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66  ize = 0;.#ifndef
15e2d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
15e2e 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f 2a  OVACUUM.      /*
15e2f 20 49 66 20 74 68 65 20 6d 61 67 69 63 20 6e 61   If the magic na
15e30 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 77 69  me ":memory:" wi
15e31 6c 6c 20 63 72 65 61 74 65 20 61 6e 20 69 6e 2d  ll create an in-
15e32 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c  memory database,
15e33 20 74 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 6c   then.      ** l
15e34 65 61 76 65 20 74 68 65 20 61 75 74 6f 56 61 63  eave the autoVac
15e35 75 75 6d 20 6d 6f 64 65 20 61 74 20 30 20 28 64  uum mode at 0 (d
15e36 6f 20 6e 6f 74 20 61 75 74 6f 2d 76 61 63 75 75  o not auto-vacuu
15e37 6d 29 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 20  m), even if.    
15e38 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 45 46 41    ** SQLITE_DEFA
15e39 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 69  ULT_AUTOVACUUM i
15e3a 73 20 74 72 75 65 2e 20 4f 6e 20 74 68 65 20 6f  s true. On the o
15e3b 74 68 65 72 20 68 61 6e 64 2c 20 69 66 0a 20 20  ther hand, if.  
15e3c 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d      ** SQLITE_OM
15e3d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 68 61 73 20  IT_MEMORYDB has 
15e3e 62 65 65 6e 20 64 65 66 69 6e 65 64 2c 20 74 68  been defined, th
15e3f 65 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 69 73  en ":memory:" is
15e40 20 6a 75 73 74 20 61 0a 20 20 20 20 20 20 2a 2a   just a.      **
15e41 20 72 65 67 75 6c 61 72 20 66 69 6c 65 2d 6e 61   regular file-na
15e42 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  me. In this case
15e43 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d   the auto-vacuum
15e44 20 61 70 70 6c 69 65 73 20 61 73 20 70 65 72 20   applies as per 
15e45 6e 6f 72 6d 61 6c 2e 0a 20 20 20 20 20 20 2a 2f  normal..      */
15e46 0a 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c 65  .      if( zFile
15e47 6e 61 6d 65 20 26 26 20 21 69 73 4d 65 6d 64 62  name && !isMemdb
15e48 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d   ){.        pBt-
15e49 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 53  >autoVacuum = (S
15e4a 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55  QLITE_DEFAULT_AU
15e4b 54 4f 56 41 43 55 55 4d 20 3f 20 31 20 3a 20 30  TOVACUUM ? 1 : 0
15e4c 29 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e  );.        pBt->
15e4d 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 53 51  incrVacuum = (SQ
15e4e 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54  LITE_DEFAULT_AUT
15e4f 4f 56 41 43 55 55 4d 3d 3d 32 20 3f 20 31 20 3a  OVACUUM==2 ? 1 :
15e50 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e   0);.      }.#en
15e51 64 69 66 0a 20 20 20 20 20 20 6e 52 65 73 65 72  dif.      nReser
15e52 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  ve = 0;.    }els
15e53 65 7b 0a 20 20 20 20 20 20 6e 52 65 73 65 72 76  e{.      nReserv
15e54 65 20 3d 20 7a 44 62 48 65 61 64 65 72 5b 32 30  e = zDbHeader[20
15e55 5d 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61  ];.      pBt->pa
15e56 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b  geSizeFixed = 1;
15e57 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
15e58 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
15e59 20 20 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56        pBt->autoV
15e5a 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74  acuum = (get4byt
15e5b 65 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20  e(&zDbHeader[36 
15e5c 2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20  + 4*4])?1:0);.  
15e5d 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63      pBt->incrVac
15e5e 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28  uum = (get4byte(
15e5f 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20  &zDbHeader[36 + 
15e60 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 64  7*4])?1:0);.#end
15e61 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  if.    }.    rc 
15e62 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65  = sqlite3PagerSe
15e63 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70  tPagesize(pBt->p
15e64 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67  Pager, &pBt->pag
15e65 65 53 69 7a 65 2c 20 6e 52 65 73 65 72 76 65 29  eSize, nReserve)
15e66 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67  ;.    if( rc ) g
15e67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f  oto btree_open_o
15e68 75 74 3b 0a 20 20 20 20 70 42 74 2d 3e 75 73 61  ut;.    pBt->usa
15e69 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70  bleSize = pBt->p
15e6a 61 67 65 53 69 7a 65 20 2d 20 6e 52 65 73 65 72  ageSize - nReser
15e6b 76 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ve;.    assert( 
15e6c 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 26  (pBt->pageSize &
15e6d 20 37 29 3d 3d 30 20 29 3b 20 20 2f 2a 20 38 2d   7)==0 );  /* 8-
15e6e 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f  byte alignment o
15e6f 66 20 70 61 67 65 53 69 7a 65 20 2a 2f 0a 20 20  f pageSize */.  
15e70 20 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53   .#if !defined(S
15e71 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
15e72 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66  D_CACHE) && !def
15e73 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
15e74 5f 44 49 53 4b 49 4f 29 0a 20 20 20 20 2f 2a 20  _DISKIO).    /* 
15e75 41 64 64 20 74 68 65 20 6e 65 77 20 42 74 53 68  Add the new BtSh
15e76 61 72 65 64 20 6f 62 6a 65 63 74 20 74 6f 20 74  ared object to t
15e77 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73  he linked list s
15e78 68 61 72 61 62 6c 65 20 42 74 53 68 61 72 65 64  harable BtShared
15e79 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  s..    */.    if
15e7a 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b  ( p->sharable ){
15e7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
15e7c 75 74 65 78 20 2a 6d 75 74 65 78 53 68 61 72 65  utex *mutexShare
15e7d 64 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 52  d;.      pBt->nR
15e7e 65 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 6d 75  ef = 1;.      mu
15e7f 74 65 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69  texShared = sqli
15e80 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
15e81 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
15e82 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20  C_MASTER);.     
15e83 20 69 66 28 20 53 51 4c 49 54 45 5f 54 48 52 45   if( SQLITE_THRE
15e84 41 44 53 41 46 45 20 26 26 20 73 71 6c 69 74 65  ADSAFE && sqlite
15e85 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43  3GlobalConfig.bC
15e86 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20  oreMutex ){.    
15e87 20 20 20 20 70 42 74 2d 3e 6d 75 74 65 78 20 3d      pBt->mutex =
15e88 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
15e89 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
15e8a 46 41 53 54 29 3b 0a 20 20 20 20 20 20 20 20 69  FAST);.        i
15e8b 66 28 20 70 42 74 2d 3e 6d 75 74 65 78 3d 3d 30  f( pBt->mutex==0
15e8c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
15e8d 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
15e8e 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6d  .          db->m
15e8f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b  allocFailed = 0;
15e90 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
15e91 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a  btree_open_out;.
15e92 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
15e93 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  }.      sqlite3_
15e94 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65  mutex_enter(mute
15e95 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20  xShared);.      
15e96 70 42 74 2d 3e 70 4e 65 78 74 20 3d 20 47 4c 4f  pBt->pNext = GLO
15e97 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71  BAL(BtShared*,sq
15e98 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65  lite3SharedCache
15e99 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 47 4c 4f  List);.      GLO
15e9a 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71  BAL(BtShared*,sq
15e9b 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65  lite3SharedCache
15e9c 4c 69 73 74 29 20 3d 20 70 42 74 3b 0a 20 20 20  List) = pBt;.   
15e9d 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
15e9e 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 72  _leave(mutexShar
15e9f 65 64 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  ed);.    }.#endi
15ea0 66 0a 20 20 7d 0a 0a 23 69 66 20 21 64 65 66 69  f.  }..#if !defi
15ea1 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
15ea2 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26  SHARED_CACHE) &&
15ea3 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
15ea4 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20  _OMIT_DISKIO).  
15ea5 2f 2a 20 49 66 20 74 68 65 20 6e 65 77 20 42 74  /* If the new Bt
15ea6 72 65 65 20 75 73 65 73 20 61 20 73 68 61 72 61  ree uses a shara
15ea7 62 6c 65 20 70 42 74 53 68 61 72 65 64 2c 20 74  ble pBtShared, t
15ea8 68 65 6e 20 6c 69 6e 6b 20 74 68 65 20 6e 65 77  hen link the new
15ea9 0a 20 20 2a 2a 20 42 74 72 65 65 20 69 6e 74 6f  .  ** Btree into
15eaa 20 74 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c   the list of all
15eab 20 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73   sharable Btrees
15eac 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 63 6f   for the same co
15ead 6e 6e 65 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54  nnection..  ** T
15eae 68 65 20 6c 69 73 74 20 69 73 20 6b 65 70 74 20  he list is kept 
15eaf 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64  in ascending ord
15eb0 65 72 20 62 79 20 70 42 74 20 61 64 64 72 65 73  er by pBt addres
15eb1 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  s..  */.  if( p-
15eb2 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20  >sharable ){.   
15eb3 20 69 6e 74 20 69 3b 0a 20 20 20 20 42 74 72 65   int i;.    Btre
15eb4 65 20 2a 70 53 69 62 3b 0a 20 20 20 20 66 6f 72  e *pSib;.    for
15eb5 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
15eb6 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
15eb7 20 28 70 53 69 62 20 3d 20 64 62 2d 3e 61 44 62   (pSib = db->aDb
15eb8 5b 69 5d 2e 70 42 74 29 21 3d 30 20 26 26 20 70  [i].pBt)!=0 && p
15eb9 53 69 62 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b  Sib->sharable ){
15eba 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
15ebb 70 53 69 62 2d 3e 70 50 72 65 76 20 29 7b 20 70  pSib->pPrev ){ p
15ebc 53 69 62 20 3d 20 70 53 69 62 2d 3e 70 50 72 65  Sib = pSib->pPre
15ebd 76 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28  v; }.        if(
15ebe 20 70 2d 3e 70 42 74 3c 70 53 69 62 2d 3e 70 42   p->pBt<pSib->pB
15ebf 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  t ){.          p
15ec0 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69 62 3b 0a  ->pNext = pSib;.
15ec1 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50 72            p->pPr
15ec2 65 76 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ev = 0;.        
15ec3 20 20 70 53 69 62 2d 3e 70 50 72 65 76 20 3d 20    pSib->pPrev = 
15ec4 70 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  p;.        }else
15ec5 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c  {.          whil
15ec6 65 28 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 26  e( pSib->pNext &
15ec7 26 20 70 53 69 62 2d 3e 70 4e 65 78 74 2d 3e 70  & pSib->pNext->p
15ec8 42 74 3c 70 2d 3e 70 42 74 20 29 7b 0a 20 20 20  Bt<p->pBt ){.   
15ec9 20 20 20 20 20 20 20 20 20 70 53 69 62 20 3d 20           pSib = 
15eca 70 53 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  pSib->pNext;.   
15ecb 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
15ecc 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 53     p->pNext = pS
15ecd 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  ib->pNext;.     
15ece 20 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d 20       p->pPrev = 
15ecf 70 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 20  pSib;.          
15ed0 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a  if( p->pNext ){.
15ed1 20 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70              p->p
15ed2 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 3b  Next->pPrev = p;
15ed3 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
15ed4 20 20 20 20 20 20 20 70 53 69 62 2d 3e 70 4e 65         pSib->pNe
15ed5 78 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20  xt = p;.        
15ed6 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
15ed7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
15ed8 20 7d 0a 23 65 6e 64 69 66 0a 20 20 2a 70 70 42   }.#endif.  *ppB
15ed9 74 72 65 65 20 3d 20 70 3b 0a 0a 62 74 72 65 65  tree = p;..btree
15eda 5f 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 69 66 28  _open_out:.  if(
15edb 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
15edc 7b 0a 20 20 20 20 69 66 28 20 70 42 74 20 26 26  {.    if( pBt &&
15edd 20 70 42 74 2d 3e 70 50 61 67 65 72 20 29 7b 0a   pBt->pPager ){.
15ede 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
15edf 65 72 43 6c 6f 73 65 28 70 42 74 2d 3e 70 50 61  erClose(pBt->pPa
15ee0 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ger);.    }.    
15ee1 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74  sqlite3_free(pBt
15ee2 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
15ee3 72 65 65 28 70 29 3b 0a 20 20 20 20 2a 70 70 42  ree(p);.    *ppB
15ee4 74 72 65 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  tree = 0;.  }.  
15ee5 69 66 28 20 6d 75 74 65 78 4f 70 65 6e 20 29 7b  if( mutexOpen ){
15ee6 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
15ee7 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
15ee8 6d 75 74 65 78 4f 70 65 6e 29 20 29 3b 0a 20 20  mutexOpen) );.  
15ee9 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
15eea 6c 65 61 76 65 28 6d 75 74 65 78 4f 70 65 6e 29  leave(mutexOpen)
15eeb 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
15eec 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72  c;.}../*.** Decr
15eed 65 6d 65 6e 74 20 74 68 65 20 42 74 53 68 61 72  ement the BtShar
15eee 65 64 2e 6e 52 65 66 20 63 6f 75 6e 74 65 72 2e  ed.nRef counter.
15eef 20 20 57 68 65 6e 20 69 74 20 72 65 61 63 68 65    When it reache
15ef0 73 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 6d 6f 76  s zero,.** remov
15ef1 65 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73  e the BtShared s
15ef2 74 72 75 63 74 75 72 65 20 66 72 6f 6d 20 74 68  tructure from th
15ef3 65 20 73 68 61 72 69 6e 67 20 6c 69 73 74 2e 20  e sharing list. 
15ef4 20 52 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20   Return.** true 
15ef5 69 66 20 74 68 65 20 42 74 53 68 61 72 65 64 2e  if the BtShared.
15ef6 6e 52 65 66 20 63 6f 75 6e 74 65 72 20 72 65 61  nRef counter rea
15ef7 63 68 65 73 20 7a 65 72 6f 20 61 6e 64 20 72 65  ches zero and re
15ef8 74 75 72 6e 0a 2a 2a 20 66 61 6c 73 65 20 69 66  turn.** false if
15ef9 20 69 74 20 69 73 20 73 74 69 6c 6c 20 70 6f 73   it is still pos
15efa 69 74 69 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  itive..*/.static
15efb 20 69 6e 74 20 72 65 6d 6f 76 65 46 72 6f 6d 53   int removeFromS
15efc 68 61 72 69 6e 67 4c 69 73 74 28 42 74 53 68 61  haringList(BtSha
15efd 72 65 64 20 2a 70 42 74 29 7b 0a 23 69 66 6e 64  red *pBt){.#ifnd
15efe 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
15eff 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 73 71  HARED_CACHE.  sq
15f00 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61  lite3_mutex *pMa
15f01 73 74 65 72 3b 0a 20 20 42 74 53 68 61 72 65 64  ster;.  BtShared
15f02 20 2a 70 4c 69 73 74 3b 0a 20 20 69 6e 74 20 72   *pList;.  int r
15f03 65 6d 6f 76 65 64 20 3d 20 30 3b 0a 0a 20 20 61  emoved = 0;..  a
15f04 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
15f05 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70 42 74  utex_notheld(pBt
15f06 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d  ->mutex) );.  pM
15f07 61 73 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d  aster = sqlite3M
15f08 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
15f09 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
15f0a 53 54 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33  STER);.  sqlite3
15f0b 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 4d 61  _mutex_enter(pMa
15f0c 73 74 65 72 29 3b 0a 20 20 70 42 74 2d 3e 6e 52  ster);.  pBt->nR
15f0d 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70 42 74 2d  ef--;.  if( pBt-
15f0e 3e 6e 52 65 66 3c 3d 30 20 29 7b 0a 20 20 20 20  >nRef<=0 ){.    
15f0f 69 66 28 20 47 4c 4f 42 41 4c 28 42 74 53 68 61  if( GLOBAL(BtSha
15f10 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72  red*,sqlite3Shar
15f11 65 64 43 61 63 68 65 4c 69 73 74 29 3d 3d 70 42  edCacheList)==pB
15f12 74 20 29 7b 0a 20 20 20 20 20 20 47 4c 4f 42 41  t ){.      GLOBA
15f13 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69  L(BtShared*,sqli
15f14 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69  te3SharedCacheLi
15f15 73 74 29 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74  st) = pBt->pNext
15f16 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
15f17 20 20 20 70 4c 69 73 74 20 3d 20 47 4c 4f 42 41     pList = GLOBA
15f18 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69  L(BtShared*,sqli
15f19 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69  te3SharedCacheLi
15f1a 73 74 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65  st);.      while
15f1b 28 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 20  ( ALWAYS(pList) 
15f1c 26 26 20 70 4c 69 73 74 2d 3e 70 4e 65 78 74 21  && pList->pNext!
15f1d 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20  =pBt ){.        
15f1e 70 4c 69 73 74 3d 70 4c 69 73 74 2d 3e 70 4e 65  pList=pList->pNe
15f1f 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  xt;.      }.    
15f20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 4c 69    if( ALWAYS(pLi
15f21 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70  st) ){.        p
15f22 4c 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 42  List->pNext = pB
15f23 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  t->pNext;.      
15f24 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
15f25 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
15f26 45 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  E ){.      sqlit
15f27 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 70 42  e3_mutex_free(pB
15f28 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d  t->mutex);.    }
15f29 0a 20 20 20 20 72 65 6d 6f 76 65 64 20 3d 20 31  .    removed = 1
15f2a 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
15f2b 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 73  mutex_leave(pMas
15f2c 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ter);.  return r
15f2d 65 6d 6f 76 65 64 3b 0a 23 65 6c 73 65 0a 20 20  emoved;.#else.  
15f2e 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66  return 1;.#endif
15f2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73  .}../*.** Make s
15f30 75 72 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 61  ure pBt->pTmpSpa
15f31 63 65 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20  ce points to an 
15f32 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 0a 2a  allocation of .*
15f33 2a 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70  * MX_CELL_SIZE(p
15f34 42 74 29 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74  Bt) bytes..*/.st
15f35 61 74 69 63 20 76 6f 69 64 20 61 6c 6c 6f 63 61  atic void alloca
15f36 74 65 54 65 6d 70 53 70 61 63 65 28 42 74 53 68  teTempSpace(BtSh
15f37 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 66  ared *pBt){.  if
15f38 28 20 21 70 42 74 2d 3e 70 54 6d 70 53 70 61 63  ( !pBt->pTmpSpac
15f39 65 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 70 54  e ){.    pBt->pT
15f3a 6d 70 53 70 61 63 65 20 3d 20 73 71 6c 69 74 65  mpSpace = sqlite
15f3b 33 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74  3PageMalloc( pBt
15f3c 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20  ->pageSize );.  
15f3d 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  }.}../*.** Free 
15f3e 74 68 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 61  the pBt->pTmpSpa
15f3f 63 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f  ce allocation.*/
15f40 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65  .static void fre
15f41 65 54 65 6d 70 53 70 61 63 65 28 42 74 53 68 61  eTempSpace(BtSha
15f42 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 73 71 6c  red *pBt){.  sql
15f43 69 74 65 33 50 61 67 65 46 72 65 65 28 20 70 42  ite3PageFree( pB
15f44 74 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20  t->pTmpSpace);. 
15f45 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20   pBt->pTmpSpace 
15f46 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  = 0;.}../*.** Cl
15f47 6f 73 65 20 61 6e 20 6f 70 65 6e 20 64 61 74 61  ose an open data
15f48 62 61 73 65 20 61 6e 64 20 69 6e 76 61 6c 69 64  base and invalid
15f49 61 74 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e  ate all cursors.
15f4a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
15f4b 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
15f4c 72 65 65 43 6c 6f 73 65 28 42 74 72 65 65 20 2a  reeClose(Btree *
15f4d 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  p){.  BtShared *
15f4e 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
15f4f 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a  BtCursor *pCur;.
15f50 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20  .  /* Close all 
15f51 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 20 76  cursors opened v
15f52 69 61 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 20  ia this handle. 
15f53 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71   */.  assert( sq
15f54 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
15f55 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  (p->db->mutex) )
15f56 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
15f57 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 43 75 72  Enter(p);.  pCur
15f58 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b   = pBt->pCursor;
15f59 0a 20 20 77 68 69 6c 65 28 20 70 43 75 72 20 29  .  while( pCur )
15f5a 7b 0a 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a  {.    BtCursor *
15f5b 70 54 6d 70 20 3d 20 70 43 75 72 3b 0a 20 20 20  pTmp = pCur;.   
15f5c 20 70 43 75 72 20 3d 20 70 43 75 72 2d 3e 70 4e   pCur = pCur->pN
15f5d 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 54 6d  ext;.    if( pTm
15f5e 70 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a  p->pBtree==p ){.
15f5f 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
15f60 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 54  eeCloseCursor(pT
15f61 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  mp);.    }.  }..
15f62 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6e    /* Rollback an
15f63 79 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 63  y active transac
15f64 74 69 6f 6e 20 61 6e 64 20 66 72 65 65 20 74 68  tion and free th
15f65 65 20 68 61 6e 64 6c 65 20 73 74 72 75 63 74 75  e handle structu
15f66 72 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 61 6c  re..  ** The cal
15f67 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65  l to sqlite3Btre
15f68 65 52 6f 6c 6c 62 61 63 6b 28 29 20 64 72 6f 70  eRollback() drop
15f69 73 20 61 6e 79 20 74 61 62 6c 65 2d 6c 6f 63 6b  s any table-lock
15f6a 73 20 68 65 6c 64 20 62 79 0a 20 20 2a 2a 20 74  s held by.  ** t
15f6b 68 69 73 20 68 61 6e 64 6c 65 2e 0a 20 20 2a 2f  his handle..  */
15f6c 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52  .  sqlite3BtreeR
15f6d 6f 6c 6c 62 61 63 6b 28 70 29 3b 0a 20 20 73 71  ollback(p);.  sq
15f6e 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
15f6f 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  p);..  /* If the
15f70 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6f 74 68  re are still oth
15f71 65 72 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72  er outstanding r
15f72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65  eferences to the
15f73 20 73 68 61 72 65 64 2d 62 74 72 65 65 0a 20 20   shared-btree.  
15f74 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 72 65  ** structure, re
15f75 74 75 72 6e 20 6e 6f 77 2e 20 54 68 65 20 72 65  turn now. The re
15f76 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20  mainder of this 
15f77 70 72 6f 63 65 64 75 72 65 20 63 6c 65 61 6e 73  procedure cleans
15f78 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 73 68   .  ** up the sh
15f79 61 72 65 64 2d 62 74 72 65 65 2e 0a 20 20 2a 2f  ared-btree..  */
15f7a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61  .  assert( p->wa
15f7b 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 26 26 20 70  ntToLock==0 && p
15f7c 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a 20  ->locked==0 );. 
15f7d 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c   if( !p->sharabl
15f7e 65 20 7c 7c 20 72 65 6d 6f 76 65 46 72 6f 6d 53  e || removeFromS
15f7f 68 61 72 69 6e 67 4c 69 73 74 28 70 42 74 29 20  haringList(pBt) 
15f80 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 42  ){.    /* The pB
15f81 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6f  t is no longer o
15f82 6e 20 74 68 65 20 73 68 61 72 69 6e 67 20 6c 69  n the sharing li
15f83 73 74 2c 20 73 6f 20 77 65 20 63 61 6e 20 61 63  st, so we can ac
15f84 63 65 73 73 0a 20 20 20 20 2a 2a 20 69 74 20 77  cess.    ** it w
15f85 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f  ithout having to
15f86 20 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 2e   hold the mutex.
15f87 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43  .    **.    ** C
15f88 6c 65 61 6e 20 6f 75 74 20 61 6e 64 20 64 65 6c  lean out and del
15f89 65 74 65 20 74 68 65 20 42 74 53 68 61 72 65 64  ete the BtShared
15f8a 20 6f 62 6a 65 63 74 2e 0a 20 20 20 20 2a 2f 0a   object..    */.
15f8b 20 20 20 20 61 73 73 65 72 74 28 20 21 70 42 74      assert( !pBt
15f8c 2d 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20 20 20  ->pCursor );.   
15f8d 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f   sqlite3PagerClo
15f8e 73 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b  se(pBt->pPager);
15f8f 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 78 46  .    if( pBt->xF
15f90 72 65 65 53 63 68 65 6d 61 20 26 26 20 70 42 74  reeSchema && pBt
15f91 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20  ->pSchema ){.   
15f92 20 20 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68     pBt->xFreeSch
15f93 65 6d 61 28 70 42 74 2d 3e 70 53 63 68 65 6d 61  ema(pBt->pSchema
15f94 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
15f95 69 74 65 33 5f 66 72 65 65 28 70 42 74 2d 3e 70  ite3_free(pBt->p
15f96 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 66 72 65  Schema);.    fre
15f97 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b  eTempSpace(pBt);
15f98 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
15f99 65 28 70 42 74 29 3b 0a 20 20 7d 0a 0a 23 69 66  e(pBt);.  }..#if
15f9a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
15f9b 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20  _SHARED_CACHE.  
15f9c 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54  assert( p->wantT
15f9d 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20 61 73  oLock==0 );.  as
15f9e 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d  sert( p->locked=
15f9f 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70  =0 );.  if( p->p
15fa0 50 72 65 76 20 29 20 70 2d 3e 70 50 72 65 76 2d  Prev ) p->pPrev-
15fa1 3e 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78  >pNext = p->pNex
15fa2 74 3b 0a 20 20 69 66 28 20 70 2d 3e 70 4e 65 78  t;.  if( p->pNex
15fa3 74 20 29 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50  t ) p->pNext->pP
15fa4 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a  rev = p->pPrev;.
15fa5 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65  #endif..  sqlite
15fa6 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 72 65 74  3_free(p);.  ret
15fa7 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
15fa8 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
15fa9 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 20  he limit on the 
15faa 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
15fab 61 6c 6c 6f 77 65 64 20 69 6e 20 74 68 65 20 63  allowed in the c
15fac 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ache..**.** The 
15fad 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
15fae 66 20 63 61 63 68 65 20 70 61 67 65 73 20 69 73  f cache pages is
15faf 20 73 65 74 20 74 6f 20 74 68 65 20 61 62 73 6f   set to the abso
15fb0 6c 75 74 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66  lute.** value of
15fb1 20 6d 78 50 61 67 65 2e 20 20 49 66 20 6d 78 50   mxPage.  If mxP
15fb2 61 67 65 20 69 73 20 6e 65 67 61 74 69 76 65 2c  age is negative,
15fb3 20 74 68 65 20 70 61 67 65 72 20 77 69 6c 6c 0a   the pager will.
15fb4 2a 2a 20 6f 70 65 72 61 74 65 20 61 73 79 6e 63  ** operate async
15fb5 68 72 6f 6e 6f 75 73 6c 79 20 2d 20 69 74 20 77  hronously - it w
15fb6 69 6c 6c 20 6e 6f 74 20 73 74 6f 70 20 74 6f 20  ill not stop to 
15fb7 64 6f 20 66 73 79 6e 63 28 29 73 0a 2a 2a 20 74  do fsync()s.** t
15fb8 6f 20 69 6e 73 75 72 65 20 64 61 74 61 20 69 73  o insure data is
15fb9 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
15fba 64 69 73 6b 20 73 75 72 66 61 63 65 20 62 65 66  disk surface bef
15fbb 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e  ore.** continuin
15fbc 67 2e 20 20 54 72 61 6e 73 61 63 74 69 6f 6e 73  g.  Transactions
15fbd 20 73 74 69 6c 6c 20 77 6f 72 6b 20 69 66 20 73   still work if s
15fbe 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 66  ynchronous is of
15fbf 66 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61  f,.** and the da
15fc0 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65  tabase cannot be
15fc1 20 63 6f 72 72 75 70 74 65 64 20 69 66 20 74 68   corrupted if th
15fc2 69 73 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 63 72  is program.** cr
15fc3 61 73 68 65 73 2e 20 20 42 75 74 20 69 66 20 74  ashes.  But if t
15fc4 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  he operating sys
15fc5 74 65 6d 20 63 72 61 73 68 65 73 20 6f 72 20 74  tem crashes or t
15fc6 68 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20 61 62  here is.** an ab
15fc7 72 75 70 74 20 70 6f 77 65 72 20 66 61 69 6c 75  rupt power failu
15fc8 72 65 20 77 68 65 6e 20 73 79 6e 63 68 72 6f 6e  re when synchron
15fc9 6f 75 73 20 69 73 20 6f 66 66 2c 20 74 68 65 20  ous is off, the 
15fca 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 75 6c  database.** coul
15fcb 64 20 62 65 20 6c 65 66 74 20 69 6e 20 61 6e 20  d be left in an 
15fcc 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 61 6e 64  inconsistent and
15fcd 20 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 73   unrecoverable s
15fce 74 61 74 65 2e 0a 2a 2a 20 53 79 6e 63 68 72 6f  tate..** Synchro
15fcf 6e 6f 75 73 20 69 73 20 6f 6e 20 62 79 20 64 65  nous is on by de
15fd0 66 61 75 6c 74 20 73 6f 20 64 61 74 61 62 61 73  fault so databas
15fd1 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20  e corruption is 
15fd2 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20  not.** normally 
15fd3 61 20 77 6f 72 72 79 2e 0a 2a 2f 0a 53 51 4c 49  a worry..*/.SQLI
15fd4 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
15fd5 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61  qlite3BtreeSetCa
15fd6 63 68 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70  cheSize(Btree *p
15fd7 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20  , int mxPage){. 
15fd8 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
15fd9 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72   p->pBt;.  asser
15fda 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
15fdb 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74  _held(p->db->mut
15fdc 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ex) );.  sqlite3
15fdd 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
15fde 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
15fdf 43 61 63 68 65 73 69 7a 65 28 70 42 74 2d 3e 70  Cachesize(pBt->p
15fe0 50 61 67 65 72 2c 20 6d 78 50 61 67 65 29 3b 0a  Pager, mxPage);.
15fe1 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
15fe2 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
15fe3 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
15fe4 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
15fe5 77 61 79 20 64 61 74 61 20 69 73 20 73 79 6e 63  way data is sync
15fe6 65 64 20 74 6f 20 64 69 73 6b 20 69 6e 20 6f 72  ed to disk in or
15fe7 64 65 72 20 74 6f 20 69 6e 63 72 65 61 73 65 20  der to increase 
15fe8 6f 72 20 64 65 63 72 65 61 73 65 0a 2a 2a 20 68  or decrease.** h
15fe9 6f 77 20 77 65 6c 6c 20 74 68 65 20 64 61 74 61  ow well the data
15fea 62 61 73 65 20 72 65 73 69 73 74 73 20 64 61 6d  base resists dam
15feb 61 67 65 20 64 75 65 20 74 6f 20 4f 53 20 63 72  age due to OS cr
15fec 61 73 68 65 73 20 61 6e 64 20 70 6f 77 65 72 0a  ashes and power.
15fed 2a 2a 20 66 61 69 6c 75 72 65 73 2e 20 20 4c 65  ** failures.  Le
15fee 76 65 6c 20 31 20 69 73 20 74 68 65 20 73 61 6d  vel 1 is the sam
15fef 65 20 61 73 20 61 73 79 6e 63 68 72 6f 6e 6f 75  e as asynchronou
15ff0 73 20 28 6e 6f 20 73 79 6e 63 73 28 29 20 6f 63  s (no syncs() oc
15ff1 63 75 72 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65  cur and.** there
15ff2 20 69 73 20 61 20 68 69 67 68 20 70 72 6f 62 61   is a high proba
15ff3 62 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65  bility of damage
15ff4 29 20 20 4c 65 76 65 6c 20 32 20 69 73 20 74 68  )  Level 2 is th
15ff5 65 20 64 65 66 61 75 6c 74 2e 20 20 54 68 65 72  e default.  Ther
15ff6 65 0a 2a 2a 20 69 73 20 61 20 76 65 72 79 20 6c  e.** is a very l
15ff7 6f 77 20 62 75 74 20 6e 6f 6e 2d 7a 65 72 6f 20  ow but non-zero 
15ff8 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64  probability of d
15ff9 61 6d 61 67 65 2e 20 20 4c 65 76 65 6c 20 33 20  amage.  Level 3 
15ffa 72 65 64 75 63 65 73 20 74 68 65 0a 2a 2a 20 70  reduces the.** p
15ffb 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61  robability of da
15ffc 6d 61 67 65 20 74 6f 20 6e 65 61 72 20 7a 65 72  mage to near zer
15ffd 6f 20 62 75 74 20 77 69 74 68 20 61 20 77 72 69  o but with a wri
15ffe 74 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 72  te performance r
15fff 65 64 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66  eduction..*/.#if
16000 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
16001 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 53  _PAGER_PRAGMAS.S
16002 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
16003 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  t sqlite3BtreeSe
16004 74 53 61 66 65 74 79 4c 65 76 65 6c 28 42 74 72  tSafetyLevel(Btr
16005 65 65 20 2a 70 2c 20 69 6e 74 20 6c 65 76 65 6c  ee *p, int level
16006 2c 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 29 7b  , int fullSync){
16007 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
16008 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73   = p->pBt;.  ass
16009 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1600a 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d  ex_held(p->db->m
1600b 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
1600c 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
1600d 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  .  sqlite3PagerS
1600e 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 70 42  etSafetyLevel(pB
1600f 74 2d 3e 70 50 61 67 65 72 2c 20 6c 65 76 65 6c  t->pPager, level
16010 2c 20 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 73  , fullSync);.  s
16011 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
16012 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  (p);.  return SQ
16013 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69  LITE_OK;.}.#endi
16014 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  f../*.** Return 
16015 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76 65  TRUE if the give
16016 6e 20 62 74 72 65 65 20 69 73 20 73 65 74 20 74  n btree is set t
16017 6f 20 73 61 66 65 74 79 20 6c 65 76 65 6c 20 31  o safety level 1
16018 2e 20 20 49 6e 20 6f 74 68 65 72 0a 2a 2a 20 77  .  In other.** w
16019 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 54 52 55  ords, return TRU
1601a 45 20 69 66 20 6e 6f 20 73 79 6e 63 28 29 20 6f  E if no sync() o
1601b 63 63 75 72 73 20 6f 6e 20 74 68 65 20 64 69 73  ccurs on the dis
1601c 6b 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49  k files..*/.SQLI
1601d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1601e 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44  qlite3BtreeSyncD
1601f 69 73 61 62 6c 65 64 28 42 74 72 65 65 20 2a 70  isabled(Btree *p
16020 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
16021 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69  Bt = p->pBt;.  i
16022 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28  nt rc;.  assert(
16023 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
16024 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  eld(p->db->mutex
16025 29 20 29 3b 20 20 0a 20 20 73 71 6c 69 74 65 33  ) );  .  sqlite3
16026 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
16027 20 61 73 73 65 72 74 28 20 70 42 74 20 26 26 20   assert( pBt && 
16028 70 42 74 2d 3e 70 50 61 67 65 72 20 29 3b 0a 20  pBt->pPager );. 
16029 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1602a 65 72 4e 6f 73 79 6e 63 28 70 42 74 2d 3e 70 50  erNosync(pBt->pP
1602b 61 67 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  ager);.  sqlite3
1602c 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
1602d 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23   return rc;.}..#
1602e 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
1602f 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52  TE_OMIT_PAGER_PR
16030 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66 69 6e  AGMAS) || !defin
16031 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
16032 41 43 55 55 4d 29 0a 2f 2a 0a 2a 2a 20 43 68 61  ACUUM)./*.** Cha
16033 6e 67 65 20 74 68 65 20 64 65 66 61 75 6c 74 20  nge the default 
16034 70 61 67 65 73 20 73 69 7a 65 20 61 6e 64 20 74  pages size and t
16035 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73  he number of res
16036 65 72 76 65 64 20 62 79 74 65 73 20 70 65 72 20  erved bytes per 
16037 70 61 67 65 2e 0a 2a 2a 20 4f 72 2c 20 69 66 20  page..** Or, if 
16038 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 68 61  the page size ha
16039 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 66  s already been f
1603a 69 78 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c  ixed, return SQL
1603b 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 0a 2a 2a  ITE_READONLY .**
1603c 20 77 69 74 68 6f 75 74 20 63 68 61 6e 67 69 6e   without changin
1603d 67 20 61 6e 79 74 68 69 6e 67 2e 0a 2a 2a 0a 2a  g anything..**.*
1603e 2a 20 54 68 65 20 70 61 67 65 20 73 69 7a 65 20  * The page size 
1603f 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20  must be a power 
16040 6f 66 20 32 20 62 65 74 77 65 65 6e 20 35 31 32  of 2 between 512
16041 20 61 6e 64 20 36 35 35 33 36 2e 20 20 49 66 20   and 65536.  If 
16042 74 68 65 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65  the page.** size
16043 20 73 75 70 70 6c 69 65 64 20 64 6f 65 73 20 6e   supplied does n
16044 6f 74 20 6d 65 65 74 20 74 68 69 73 20 63 6f 6e  ot meet this con
16045 73 74 72 61 69 6e 74 20 74 68 65 6e 20 74 68 65  straint then the
16046 20 70 61 67 65 20 73 69 7a 65 20 69 73 20 6e 6f   page size is no
16047 74 0a 2a 2a 20 63 68 61 6e 67 65 64 2e 0a 2a 2a  t.** changed..**
16048 0a 2a 2a 20 50 61 67 65 20 73 69 7a 65 73 20 61  .** Page sizes a
16049 72 65 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 74  re constrained t
1604a 6f 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20  o be a power of 
1604b 74 77 6f 20 73 6f 20 74 68 61 74 20 74 68 65 20  two so that the 
1604c 72 65 67 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65  region.** of the
1604d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75   database file u
1604e 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20  sed for locking 
1604f 28 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 50 45  (beginning at PE
16050 4e 44 49 4e 47 5f 42 59 54 45 2c 0a 2a 2a 20 74  NDING_BYTE,.** t
16051 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61  he first byte pa
16052 73 74 20 74 68 65 20 31 47 42 20 62 6f 75 6e 64  st the 1GB bound
16053 61 72 79 2c 20 30 78 34 30 30 30 30 30 30 30 29  ary, 0x40000000)
16054 20 6e 65 65 64 73 20 74 6f 20 6f 63 63 75 72 0a   needs to occur.
16055 2a 2a 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  ** at the beginn
16056 69 6e 67 20 6f 66 20 61 20 70 61 67 65 2e 0a 2a  ing of a page..*
16057 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65  *.** If paramete
16058 72 20 6e 52 65 73 65 72 76 65 20 69 73 20 6c 65  r nReserve is le
16059 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68  ss than zero, th
1605a 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  en the number of
1605b 20 72 65 73 65 72 76 65 64 0a 2a 2a 20 62 79 74   reserved.** byt
1605c 65 73 20 70 65 72 20 70 61 67 65 20 69 73 20 6c  es per page is l
1605d 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a  eft unchanged..*
1605e 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 46 69 78  *.** If the iFix
1605f 21 3d 30 20 74 68 65 6e 20 74 68 65 20 70 61 67  !=0 then the pag
16060 65 53 69 7a 65 46 69 78 65 64 20 66 6c 61 67 20  eSizeFixed flag 
16061 69 73 20 73 65 74 20 73 6f 20 74 68 61 74 20 74  is set so that t
16062 68 65 20 70 61 67 65 20 73 69 7a 65 0a 2a 2a 20  he page size.** 
16063 61 6e 64 20 61 75 74 6f 76 61 63 75 75 6d 20 6d  and autovacuum m
16064 6f 64 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65  ode can no longe
16065 72 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f  r be changed..*/
16066 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16067 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
16068 53 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65  SetPageSize(Btre
16069 65 20 2a 70 2c 20 69 6e 74 20 70 61 67 65 53 69  e *p, int pageSi
1606a 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65 72 76 65  ze, int nReserve
1606b 2c 20 69 6e 74 20 69 46 69 78 29 7b 0a 20 20 69  , int iFix){.  i
1606c 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1606d 4b 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  K;.  BtShared *p
1606e 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61  Bt = p->pBt;.  a
1606f 73 73 65 72 74 28 20 6e 52 65 73 65 72 76 65 3e  ssert( nReserve>
16070 3d 2d 31 20 26 26 20 6e 52 65 73 65 72 76 65 3c  =-1 && nReserve<
16071 3d 32 35 35 20 29 3b 0a 20 20 73 71 6c 69 74 65  =255 );.  sqlite
16072 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
16073 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53    if( pBt->pageS
16074 69 7a 65 46 69 78 65 64 20 29 7b 0a 20 20 20 20  izeFixed ){.    
16075 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
16076 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  e(p);.    return
16077 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59   SQLITE_READONLY
16078 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 65 73  ;.  }.  if( nRes
16079 65 72 76 65 3c 30 20 29 7b 0a 20 20 20 20 6e 52  erve<0 ){.    nR
1607a 65 73 65 72 76 65 20 3d 20 70 42 74 2d 3e 70 61  eserve = pBt->pa
1607b 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73  geSize - pBt->us
1607c 61 62 6c 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 20  ableSize;.  }.  
1607d 61 73 73 65 72 74 28 20 6e 52 65 73 65 72 76 65  assert( nReserve
1607e 3e 3d 30 20 26 26 20 6e 52 65 73 65 72 76 65 3c  >=0 && nReserve<
1607f 3d 32 35 35 20 29 3b 0a 20 20 69 66 28 20 70 61  =255 );.  if( pa
16080 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70  geSize>=512 && p
16081 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f  ageSize<=SQLITE_
16082 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 26 26  MAX_PAGE_SIZE &&
16083 0a 20 20 20 20 20 20 20 20 28 28 70 61 67 65 53  .        ((pageS
16084 69 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65 29  ize-1)&pageSize)
16085 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ==0 ){.    asser
16086 74 28 20 28 70 61 67 65 53 69 7a 65 20 26 20 37  t( (pageSize & 7
16087 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  )==0 );.    asse
16088 72 74 28 20 21 70 42 74 2d 3e 70 50 61 67 65 31  rt( !pBt->pPage1
16089 20 26 26 20 21 70 42 74 2d 3e 70 43 75 72 73 6f   && !pBt->pCurso
1608a 72 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 61  r );.    pBt->pa
1608b 67 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70 61  geSize = (u16)pa
1608c 67 65 53 69 7a 65 3b 0a 20 20 20 20 66 72 65 65  geSize;.    free
1608d 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a  TempSpace(pBt);.
1608e 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74    }.  rc = sqlit
1608f 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69  e3PagerSetPagesi
16090 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  ze(pBt->pPager, 
16091 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2c 20  &pBt->pageSize, 
16092 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 70 42 74  nReserve);.  pBt
16093 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70  ->usableSize = p
16094 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 28  Bt->pageSize - (
16095 75 31 36 29 6e 52 65 73 65 72 76 65 3b 0a 20 20  u16)nReserve;.  
16096 69 66 28 20 69 46 69 78 20 29 20 70 42 74 2d 3e  if( iFix ) pBt->
16097 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20  pageSizeFixed = 
16098 31 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  1;.  sqlite3Btre
16099 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
1609a 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1609b 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72   Return the curr
1609c 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 70 61  ently defined pa
1609d 67 65 20 73 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54  ge size.*/.SQLIT
1609e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1609f 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
160a0 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 29 7b  eSize(Btree *p){
160a1 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74  .  return p->pBt
160a2 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 7d 0a 0a 2f  ->pageSize;.}../
160a3 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
160a4 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
160a5 6f 66 20 73 70 61 63 65 20 61 74 20 74 68 65 20  of space at the 
160a6 65 6e 64 20 6f 66 20 65 76 65 72 79 20 70 61 67  end of every pag
160a7 65 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 69 6e  e that.** are in
160a8 74 65 6e 74 75 61 6c 6c 79 20 6c 65 66 74 20 75  tentually left u
160a9 6e 75 73 65 64 2e 20 20 54 68 69 73 20 69 73 20  nused.  This is 
160aa 74 68 65 20 22 72 65 73 65 72 76 65 64 22 20 73  the "reserved" s
160ab 70 61 63 65 20 74 68 61 74 20 69 73 0a 2a 2a 20  pace that is.** 
160ac 73 6f 6d 65 74 69 6d 65 73 20 75 73 65 64 20 62  sometimes used b
160ad 79 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f  y extensions..*/
160ae 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
160af 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
160b0 47 65 74 52 65 73 65 72 76 65 28 42 74 72 65 65  GetReserve(Btree
160b1 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20   *p){.  int n;. 
160b2 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
160b3 65 72 28 70 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e  er(p);.  n = p->
160b4 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20  pBt->pageSize - 
160b5 70 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  p->pBt->usableSi
160b6 7a 65 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  ze;.  sqlite3Btr
160b7 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
160b8 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn n;.}../*.**
160b9 20 53 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d   Set the maximum
160ba 20 70 61 67 65 20 63 6f 75 6e 74 20 66 6f 72 20   page count for 
160bb 61 20 64 61 74 61 62 61 73 65 20 69 66 20 6d 78  a database if mx
160bc 50 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 65  Page is positive
160bd 2e 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 73 20  ..** No changes 
160be 61 72 65 20 6d 61 64 65 20 69 66 20 6d 78 50 61  are made if mxPa
160bf 67 65 20 69 73 20 30 20 6f 72 20 6e 65 67 61 74  ge is 0 or negat
160c0 69 76 65 2e 0a 2a 2a 20 52 65 67 61 72 64 6c 65  ive..** Regardle
160c1 73 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20  ss of the value 
160c2 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72  of mxPage, retur
160c3 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61  n the maximum pa
160c4 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c  ge count..*/.SQL
160c5 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
160c6 73 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50  sqlite3BtreeMaxP
160c7 61 67 65 43 6f 75 6e 74 28 42 74 72 65 65 20 2a  ageCount(Btree *
160c8 70 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a  p, int mxPage){.
160c9 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74    int n;.  sqlit
160ca 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
160cb 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 50 61  .  n = sqlite3Pa
160cc 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28  gerMaxPageCount(
160cd 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  p->pBt->pPager, 
160ce 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74  mxPage);.  sqlit
160cf 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
160d0 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 23  .  return n;.}.#
160d1 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
160d2 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41  d(SQLITE_OMIT_PA
160d3 47 45 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c 20  GER_PRAGMAS) || 
160d4 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
160d5 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20 2a 2f 0a  OMIT_VACUUM) */.
160d6 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
160d7 65 20 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 20  e 'auto-vacuum' 
160d8 70 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65 20  property of the 
160d9 64 61 74 61 62 61 73 65 2e 20 49 66 20 74 68 65  database. If the
160da 20 27 61 75 74 6f 56 61 63 75 75 6d 27 0a 2a 2a   'autoVacuum'.**
160db 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f   parameter is no
160dc 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 75 74  n-zero, then aut
160dd 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 69 73  o-vacuum mode is
160de 20 65 6e 61 62 6c 65 64 2e 20 49 66 20 7a 65 72   enabled. If zer
160df 6f 2c 20 69 74 0a 2a 2a 20 69 73 20 64 69 73 61  o, it.** is disa
160e0 62 6c 65 64 2e 20 54 68 65 20 64 65 66 61 75 6c  bled. The defaul
160e1 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  t value for the 
160e2 61 75 74 6f 2d 76 61 63 75 75 6d 20 70 72 6f 70  auto-vacuum prop
160e3 65 72 74 79 20 69 73 20 0a 2a 2a 20 64 65 74 65  erty is .** dete
160e4 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 53 51  rmined by the SQ
160e5 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54  LITE_DEFAULT_AUT
160e6 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a  OVACUUM macro..*
160e7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
160e8 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
160e9 65 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42  eSetAutoVacuum(B
160ea 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 61 75 74  tree *p, int aut
160eb 6f 56 61 63 75 75 6d 29 7b 0a 23 69 66 64 65 66  oVacuum){.#ifdef
160ec 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
160ed 4f 56 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e  OVACUUM.  return
160ee 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59   SQLITE_READONLY
160ef 3b 0a 23 65 6c 73 65 0a 20 20 42 74 53 68 61 72  ;.#else.  BtShar
160f0 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
160f1 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
160f2 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 61 76 20  ITE_OK;.  u8 av 
160f3 3d 20 28 75 38 29 61 75 74 6f 56 61 63 75 75 6d  = (u8)autoVacuum
160f4 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  ;..  sqlite3Btre
160f5 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28  eEnter(p);.  if(
160f6 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69   pBt->pageSizeFi
160f7 78 65 64 20 26 26 20 28 61 76 20 3f 31 3a 30 29  xed && (av ?1:0)
160f8 21 3d 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  !=pBt->autoVacuu
160f9 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  m ){.    rc = SQ
160fa 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20  LITE_READONLY;. 
160fb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 2d   }else{.    pBt-
160fc 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 61 76  >autoVacuum = av
160fd 20 3f 31 3a 30 3b 0a 20 20 20 20 70 42 74 2d 3e   ?1:0;.    pBt->
160fe 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 61 76 3d  incrVacuum = av=
160ff 3d 32 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 20 73  =2 ?1:0;.  }.  s
16100 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
16101 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
16102 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  ;.#endif.}../*.*
16103 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c  * Return the val
16104 75 65 20 6f 66 20 74 68 65 20 27 61 75 74 6f 2d  ue of the 'auto-
16105 76 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74 79  vacuum' property
16106 2e 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75 6d  . If auto-vacuum
16107 20 69 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64 20   is .** enabled 
16108 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f  1 is returned. O
16109 74 68 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a 53  therwise 0..*/.S
1610a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1610b 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  t sqlite3BtreeGe
1610c 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65  tAutoVacuum(Btre
1610d 65 20 2a 70 29 7b 0a 23 69 66 64 65 66 20 53 51  e *p){.#ifdef SQ
1610e 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
1610f 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 42 54  CUUM.  return BT
16110 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e  REE_AUTOVACUUM_N
16111 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74  ONE;.#else.  int
16112 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74   rc;.  sqlite3Bt
16113 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72  reeEnter(p);.  r
16114 63 20 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e 70  c = (.    (!p->p
16115 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3f  Bt->autoVacuum)?
16116 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d  BTREE_AUTOVACUUM
16117 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d 3e  _NONE:.    (!p->
16118 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29  pBt->incrVacuum)
16119 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55  ?BTREE_AUTOVACUU
1611a 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52 45  M_FULL:.    BTRE
1611b 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43  E_AUTOVACUUM_INC
1611c 52 0a 20 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  R.  );.  sqlite3
1611d 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
1611e 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64   return rc;.#end
1611f 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74  if.}.../*.** Get
16120 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
16121 70 50 61 67 65 31 20 6f 66 20 74 68 65 20 64 61  pPage1 of the da
16122 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68  tabase file.  Th
16123 69 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20  is will.** also 
16124 61 63 71 75 69 72 65 20 61 20 72 65 61 64 6c 6f  acquire a readlo
16125 63 6b 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 2e  ck on that file.
16126 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  .**.** SQLITE_OK
16127 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20   is returned on 
16128 73 75 63 63 65 73 73 2e 20 20 49 66 20 74 68 65  success.  If the
16129 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a 2a   file is not a.*
1612a 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61  * well-formed da
1612b 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65  tabase file, the
1612c 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
1612d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
1612e 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20   SQLITE_BUSY is 
1612f 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20  returned if the 
16130 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b  database is lock
16131 65 64 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  ed.  SQLITE_NOME
16132 4d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  M.** is returned
16133 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f   if we run out o
16134 66 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74  f memory. .*/.st
16135 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 72  atic int lockBtr
16136 65 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  ee(BtShared *pBt
16137 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d  ){.  int rc;.  M
16138 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b 0a  emPage *pPage1;.
16139 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20    int nPage;..  
1613a 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1613b 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
1613c 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
1613d 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d  rt( pBt->pPage1=
1613e 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  =0 );.  rc = sql
1613f 69 74 65 33 50 61 67 65 72 53 68 61 72 65 64 4c  ite3PagerSharedL
16140 6f 63 6b 28 70 42 74 2d 3e 70 50 61 67 65 72 29  ock(pBt->pPager)
16141 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
16142 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
16143 63 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47  c;.  rc = btreeG
16144 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c 20 26  etPage(pBt, 1, &
16145 70 50 61 67 65 31 2c 20 30 29 3b 0a 20 20 69 66  pPage1, 0);.  if
16146 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
16147 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
16148 2f 2a 20 44 6f 20 73 6f 6d 65 20 63 68 65 63 6b  /* Do some check
16149 69 6e 67 20 74 6f 20 68 65 6c 70 20 69 6e 73 75  ing to help insu
1614a 72 65 20 74 68 65 20 66 69 6c 65 20 77 65 20 6f  re the file we o
1614b 70 65 6e 65 64 20 72 65 61 6c 6c 79 20 69 73 0a  pened really is.
1614c 20 20 2a 2a 20 61 20 76 61 6c 69 64 20 64 61 74    ** a valid dat
1614d 61 62 61 73 65 20 66 69 6c 65 2e 20 0a 20 20 2a  abase file. .  *
1614e 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  /.  rc = sqlite3
1614f 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  PagerPagecount(p
16150 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 61  Bt->pPager, &nPa
16151 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  ge);.  if( rc!=S
16152 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
16153 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f  goto page1_init_
16154 66 61 69 6c 65 64 3b 0a 20 20 7d 65 6c 73 65 20  failed;.  }else 
16155 69 66 28 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20  if( nPage>0 ){. 
16156 20 20 20 69 6e 74 20 70 61 67 65 53 69 7a 65 3b     int pageSize;
16157 0a 20 20 20 20 69 6e 74 20 75 73 61 62 6c 65 53  .    int usableS
16158 69 7a 65 3b 0a 20 20 20 20 75 38 20 2a 70 61 67  ize;.    u8 *pag
16159 65 31 20 3d 20 70 50 61 67 65 31 2d 3e 61 44 61  e1 = pPage1->aDa
1615a 74 61 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ta;.    rc = SQL
1615b 49 54 45 5f 4e 4f 54 41 44 42 3b 0a 20 20 20 20  ITE_NOTADB;.    
1615c 69 66 28 20 6d 65 6d 63 6d 70 28 70 61 67 65 31  if( memcmp(page1
1615d 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 2c 20  , zMagicHeader, 
1615e 31 36 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  16)!=0 ){.      
1615f 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f  goto page1_init_
16160 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20  failed;.    }.  
16161 20 20 69 66 28 20 70 61 67 65 31 5b 31 38 5d 3e    if( page1[18]>
16162 31 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e  1 ){.      pBt->
16163 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20  readOnly = 1;.  
16164 20 20 7d 0a 20 20 20 20 69 66 28 20 70 61 67 65    }.    if( page
16165 31 5b 31 39 5d 3e 31 20 29 7b 0a 20 20 20 20 20  1[19]>1 ){.     
16166 20 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74   goto page1_init
16167 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 0a  _failed;.    }..
16168 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d      /* The maxim
16169 75 6d 20 65 6d 62 65 64 64 65 64 20 66 72 61 63  um embedded frac
1616a 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 65 78 61  tion must be exa
1616b 63 74 6c 79 20 32 35 25 2e 20 20 41 6e 64 20 74  ctly 25%.  And t
1616c 68 65 20 6d 69 6e 69 6d 75 6d 0a 20 20 20 20 2a  he minimum.    *
1616d 2a 20 65 6d 62 65 64 64 65 64 20 66 72 61 63 74  * embedded fract
1616e 69 6f 6e 20 6d 75 73 74 20 62 65 20 31 32 2e 35  ion must be 12.5
1616f 25 20 66 6f 72 20 62 6f 74 68 20 6c 65 61 66 2d  % for both leaf-
16170 64 61 74 61 20 61 6e 64 20 6e 6f 6e 2d 6c 65 61  data and non-lea
16171 66 2d 64 61 74 61 2e 0a 20 20 20 20 2a 2a 20 54  f-data..    ** T
16172 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 65 73 69  he original desi
16173 67 6e 20 61 6c 6c 6f 77 65 64 20 74 68 65 73 65  gn allowed these
16174 20 61 6d 6f 75 6e 74 73 20 74 6f 20 76 61 72 79   amounts to vary
16175 2c 20 62 75 74 20 61 73 20 6f 66 0a 20 20 20 20  , but as of.    
16176 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e 36 2e 30  ** version 3.6.0
16177 2c 20 77 65 20 72 65 71 75 69 72 65 20 74 68 65  , we require the
16178 6d 20 74 6f 20 62 65 20 66 69 78 65 64 2e 0a 20  m to be fixed.. 
16179 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6d 65     */.    if( me
1617a 6d 63 6d 70 28 26 70 61 67 65 31 5b 32 31 5d 2c  mcmp(&page1[21],
1617b 20 22 5c 31 30 30 5c 30 34 30 5c 30 34 30 22 2c   "\100\040\040",
1617c 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 67  3)!=0 ){.      g
1617d 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66  oto page1_init_f
1617e 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20  ailed;.    }.   
1617f 20 70 61 67 65 53 69 7a 65 20 3d 20 67 65 74 32   pageSize = get2
16180 62 79 74 65 28 26 70 61 67 65 31 5b 31 36 5d 29  byte(&page1[16])
16181 3b 0a 20 20 20 20 69 66 28 20 28 28 70 61 67 65  ;.    if( ((page
16182 53 69 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65  Size-1)&pageSize
16183 29 21 3d 30 20 7c 7c 20 70 61 67 65 53 69 7a 65  )!=0 || pageSize
16184 3c 35 31 32 20 7c 7c 0a 20 20 20 20 20 20 20 20  <512 ||.        
16185 28 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45  (SQLITE_MAX_PAGE
16186 5f 53 49 5a 45 3c 33 32 37 36 38 20 26 26 20 70  _SIZE<32768 && p
16187 61 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d  ageSize>SQLITE_M
16188 41 58 5f 50 41 47 45 5f 53 49 5a 45 29 0a 20 20  AX_PAGE_SIZE).  
16189 20 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20    ){.      goto 
1618a 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65  page1_init_faile
1618b 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  d;.    }.    ass
1618c 65 72 74 28 20 28 70 61 67 65 53 69 7a 65 20 26  ert( (pageSize &
1618d 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 75 73   7)==0 );.    us
1618e 61 62 6c 65 53 69 7a 65 20 3d 20 70 61 67 65 53  ableSize = pageS
1618f 69 7a 65 20 2d 20 70 61 67 65 31 5b 32 30 5d 3b  ize - page1[20];
16190 0a 20 20 20 20 69 66 28 20 70 61 67 65 53 69 7a  .    if( pageSiz
16191 65 21 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  e!=pBt->pageSize
16192 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 66 74   ){.      /* Aft
16193 65 72 20 72 65 61 64 69 6e 67 20 74 68 65 20 66  er reading the f
16194 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65  irst page of the
16195 20 64 61 74 61 62 61 73 65 20 61 73 73 75 6d 69   database assumi
16196 6e 67 20 61 20 70 61 67 65 20 73 69 7a 65 0a 20  ng a page size. 
16197 20 20 20 20 20 2a 2a 20 6f 66 20 42 74 53 68 61       ** of BtSha
16198 72 65 64 2e 70 61 67 65 53 69 7a 65 2c 20 77 65  red.pageSize, we
16199 20 68 61 76 65 20 64 69 73 63 6f 76 65 72 65 64   have discovered
1619a 20 74 68 61 74 20 74 68 65 20 70 61 67 65 2d 73   that the page-s
1619b 69 7a 65 20 69 73 0a 20 20 20 20 20 20 2a 2a 20  ize is.      ** 
1619c 61 63 74 75 61 6c 6c 79 20 70 61 67 65 53 69 7a  actually pageSiz
1619d 65 2e 20 55 6e 6c 6f 63 6b 20 74 68 65 20 64 61  e. Unlock the da
1619e 74 61 62 61 73 65 2c 20 6c 65 61 76 65 20 70 42  tabase, leave pB
1619f 74 2d 3e 70 50 61 67 65 31 20 61 74 0a 20 20 20  t->pPage1 at.   
161a0 20 20 20 2a 2a 20 7a 65 72 6f 20 61 6e 64 20 72     ** zero and r
161a1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
161a2 20 54 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c   The caller will
161a3 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74   call this funct
161a4 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 61 67 61  ion.      ** aga
161a5 69 6e 20 77 69 74 68 20 74 68 65 20 63 6f 72 72  in with the corr
161a6 65 63 74 20 70 61 67 65 2d 73 69 7a 65 2e 0a 20  ect page-size.. 
161a7 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65       */.      re
161a8 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 31  leasePage(pPage1
161a9 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 75 73  );.      pBt->us
161aa 61 62 6c 65 53 69 7a 65 20 3d 20 28 75 31 36 29  ableSize = (u16)
161ab 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20  usableSize;.    
161ac 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20    pBt->pageSize 
161ad 3d 20 28 75 31 36 29 70 61 67 65 53 69 7a 65 3b  = (u16)pageSize;
161ae 0a 20 20 20 20 20 20 66 72 65 65 54 65 6d 70 53  .      freeTempS
161af 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 20  pace(pBt);.     
161b0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
161b1 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 42  erSetPagesize(pB
161b2 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d  t->pPager, &pBt-
161b3 3e 70 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20  >pageSize,.     
161b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
161b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61                pa
161b6 67 65 53 69 7a 65 2d 75 73 61 62 6c 65 53 69 7a  geSize-usableSiz
161b7 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  e);.      return
161b8 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69   rc;.    }.    i
161b9 66 28 20 75 73 61 62 6c 65 53 69 7a 65 3c 34 38  f( usableSize<48
161ba 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  0 ){.      goto 
161bb 70 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65  page1_init_faile
161bc 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74  d;.    }.    pBt
161bd 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 31  ->pageSize = (u1
161be 36 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20  6)pageSize;.    
161bf 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
161c0 3d 20 28 75 31 36 29 75 73 61 62 6c 65 53 69 7a  = (u16)usableSiz
161c1 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  e;.#ifndef SQLIT
161c2 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
161c3 4d 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56  M.    pBt->autoV
161c4 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74  acuum = (get4byt
161c5 65 28 26 70 61 67 65 31 5b 33 36 20 2b 20 34 2a  e(&page1[36 + 4*
161c6 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 70 42  4])?1:0);.    pB
161c7 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20  t->incrVacuum = 
161c8 28 67 65 74 34 62 79 74 65 28 26 70 61 67 65 31  (get4byte(&page1
161c9 5b 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29  [36 + 7*4])?1:0)
161ca 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20  ;.#endif.  }..  
161cb 2f 2a 20 6d 61 78 4c 6f 63 61 6c 20 69 73 20 74  /* maxLocal is t
161cc 68 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e  he maximum amoun
161cd 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 74 6f 20  t of payload to 
161ce 73 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 66 6f  store locally fo
161cf 72 0a 20 20 2a 2a 20 61 20 63 65 6c 6c 2e 20 20  r.  ** a cell.  
161d0 4d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20  Make sure it is 
161d1 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 20 73 6f 20  small enough so 
161d2 74 68 61 74 20 61 74 20 6c 65 61 73 74 20 6d 69  that at least mi
161d3 6e 46 61 6e 6f 75 74 0a 20 20 2a 2a 20 63 65 6c  nFanout.  ** cel
161d4 6c 73 20 63 61 6e 20 77 69 6c 6c 20 66 69 74 20  ls can will fit 
161d5 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 57 65  on one page.  We
161d6 20 61 73 73 75 6d 65 20 61 20 31 30 2d 62 79 74   assume a 10-byt
161d7 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 20  e page header.. 
161d8 20 2a 2a 20 42 65 73 69 64 65 73 20 74 68 65 20   ** Besides the 
161d9 70 61 79 6c 6f 61 64 2c 20 74 68 65 20 63 65 6c  payload, the cel
161da 6c 20 6d 75 73 74 20 73 74 6f 72 65 3a 0a 20 20  l must store:.  
161db 2a 2a 20 20 20 20 20 32 2d 62 79 74 65 20 70 6f  **     2-byte po
161dc 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c  inter to the cel
161dd 6c 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74  l.  **     4-byt
161de 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 0a  e child pointer.
161df 20 20 2a 2a 20 20 20 20 20 39 2d 62 79 74 65 20    **     9-byte 
161e0 6e 4b 65 79 20 76 61 6c 75 65 0a 20 20 2a 2a 20  nKey value.  ** 
161e1 20 20 20 20 34 2d 62 79 74 65 20 6e 44 61 74 61      4-byte nData
161e2 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20   value.  **     
161e3 34 2d 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 20  4-byte overflow 
161e4 70 61 67 65 20 70 6f 69 6e 74 65 72 0a 20 20 2a  page pointer.  *
161e5 2a 20 53 6f 20 61 20 63 65 6c 6c 20 63 6f 6e 73  * So a cell cons
161e6 69 73 74 73 20 6f 66 20 61 20 32 2d 62 79 74 65  ists of a 2-byte
161e7 20 70 6f 69 6e 65 72 2c 20 61 20 68 65 61 64 65   poiner, a heade
161e8 72 20 77 68 69 63 68 20 69 73 20 61 73 20 6d 75  r which is as mu
161e9 63 68 20 61 73 0a 20 20 2a 2a 20 31 37 20 62 79  ch as.  ** 17 by
161ea 74 65 73 20 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e  tes long, 0 to N
161eb 20 62 79 74 65 73 20 6f 66 20 70 61 79 6c 6f 61   bytes of payloa
161ec 64 2c 20 61 6e 64 20 61 6e 20 6f 70 74 69 6f 6e  d, and an option
161ed 61 6c 20 34 20 62 79 74 65 20 6f 76 65 72 66 6c  al 4 byte overfl
161ee 6f 77 0a 20 20 2a 2a 20 70 61 67 65 20 70 6f 69  ow.  ** page poi
161ef 6e 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 42 74  nter..  */.  pBt
161f0 2d 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 28 70 42  ->maxLocal = (pB
161f1 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32  t->usableSize-12
161f2 29 2a 36 34 2f 32 35 35 20 2d 20 32 33 3b 0a 20  )*64/255 - 23;. 
161f3 20 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d   pBt->minLocal =
161f4 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a   (pBt->usableSiz
161f5 65 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d 20 32  e-12)*32/255 - 2
161f6 33 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 4c 65 61  3;.  pBt->maxLea
161f7 66 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  f = pBt->usableS
161f8 69 7a 65 20 2d 20 33 35 3b 0a 20 20 70 42 74 2d  ize - 35;.  pBt-
161f9 3e 6d 69 6e 4c 65 61 66 20 3d 20 28 70 42 74 2d  >minLeaf = (pBt-
161fa 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a  >usableSize-12)*
161fb 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 61  32/255 - 23;.  a
161fc 73 73 65 72 74 28 20 70 42 74 2d 3e 6d 61 78 4c  ssert( pBt->maxL
161fd 65 61 66 20 2b 20 32 33 20 3c 3d 20 4d 58 5f 43  eaf + 23 <= MX_C
161fe 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b  ELL_SIZE(pBt) );
161ff 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d  .  pBt->pPage1 =
16200 20 70 50 61 67 65 31 3b 0a 20 20 72 65 74 75 72   pPage1;.  retur
16201 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61  n SQLITE_OK;..pa
16202 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3a  ge1_init_failed:
16203 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70  .  releasePage(p
16204 50 61 67 65 31 29 3b 0a 20 20 70 42 74 2d 3e 70  Page1);.  pBt->p
16205 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 72 65 74  Page1 = 0;.  ret
16206 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
16207 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   If there are no
16208 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 75 72   outstanding cur
16209 73 6f 72 73 20 61 6e 64 20 77 65 20 61 72 65 20  sors and we are 
1620a 6e 6f 74 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  not in the middl
1620b 65 0a 2a 2a 20 6f 66 20 61 20 74 72 61 6e 73 61  e.** of a transa
1620c 63 74 69 6f 6e 20 62 75 74 20 74 68 65 72 65 20  ction but there 
1620d 69 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f  is a read lock o
1620e 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  n the database, 
1620f 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75  then.** this rou
16210 74 69 6e 65 20 75 6e 72 65 66 73 20 74 68 65 20  tine unrefs the 
16211 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68  first page of th
16212 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
16213 77 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 74 68  which .** has th
16214 65 20 65 66 66 65 63 74 20 6f 66 20 72 65 6c 65  e effect of rele
16215 61 73 69 6e 67 20 74 68 65 20 72 65 61 64 20 6c  asing the read l
16216 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ock..**.** If th
16217 65 72 65 20 69 73 20 61 20 74 72 61 6e 73 61 63  ere is a transac
16218 74 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73  tion in progress
16219 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
1621a 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74  s a no-op..*/.st
1621b 61 74 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b  atic void unlock
1621c 42 74 72 65 65 49 66 55 6e 75 73 65 64 28 42 74  BtreeIfUnused(Bt
1621d 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20  Shared *pBt){.  
1621e 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1621f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
16220 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
16221 72 74 28 20 70 42 74 2d 3e 70 43 75 72 73 6f 72  rt( pBt->pCursor
16222 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 69 6e 54 72  ==0 || pBt->inTr
16223 61 6e 73 61 63 74 69 6f 6e 3e 54 52 41 4e 53 5f  ansaction>TRANS_
16224 4e 4f 4e 45 20 29 3b 0a 20 20 69 66 28 20 70 42  NONE );.  if( pB
16225 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
16226 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 20  ==TRANS_NONE && 
16227 70 42 74 2d 3e 70 50 61 67 65 31 21 3d 30 20 29  pBt->pPage1!=0 )
16228 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42  {.    assert( pB
16229 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61  t->pPage1->aData
1622a 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1622b 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63  sqlite3PagerRefc
1622c 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72  ount(pBt->pPager
1622d 29 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65  )==1 );.    asse
1622e 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 2d  rt( pBt->pPage1-
1622f 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 20 72 65  >aData );.    re
16230 6c 65 61 73 65 50 61 67 65 28 70 42 74 2d 3e 70  leasePage(pBt->p
16231 50 61 67 65 31 29 3b 0a 20 20 20 20 70 42 74 2d  Page1);.    pBt-
16232 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 7d  >pPage1 = 0;.  }
16233 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 42 74  .}../*.** If pBt
16234 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 6d   points to an em
16235 70 74 79 20 66 69 6c 65 20 74 68 65 6e 20 63 6f  pty file then co
16236 6e 76 65 72 74 20 74 68 61 74 20 65 6d 70 74 79  nvert that empty
16237 20 66 69 6c 65 0a 2a 2a 20 69 6e 74 6f 20 61 20   file.** into a 
16238 6e 65 77 20 65 6d 70 74 79 20 64 61 74 61 62 61  new empty databa
16239 73 65 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69  se by initializi
1623a 6e 67 20 74 68 65 20 66 69 72 73 74 20 70 61 67  ng the first pag
1623b 65 20 6f 66 0a 2a 2a 20 74 68 65 20 64 61 74 61  e of.** the data
1623c 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  base..*/.static 
1623d 69 6e 74 20 6e 65 77 44 61 74 61 62 61 73 65 28  int newDatabase(
1623e 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a  BtShared *pBt){.
1623f 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 31 3b 0a    MemPage *pP1;.
16240 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
16241 2a 64 61 74 61 3b 0a 20 20 69 6e 74 20 72 63 3b  *data;.  int rc;
16242 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20  .  int nPage;.. 
16243 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
16244 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
16245 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a 20  >mutex) );.  /* 
16246 54 68 65 20 64 61 74 61 62 61 73 65 20 73 69 7a  The database siz
16247 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  e has already be
16248 65 6e 20 6d 65 61 73 75 72 65 64 20 61 6e 64 20  en measured and 
16249 63 61 63 68 65 64 2c 20 73 6f 20 66 61 69 6c 75  cached, so failu
1624a 72 65 0a 20 20 2a 2a 20 69 73 20 69 6d 70 6f 73  re.  ** is impos
1624b 73 69 62 6c 65 20 68 65 72 65 2e 20 20 49 66 20  sible here.  If 
1624c 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a  the original siz
1624d 65 20 6d 65 61 73 75 72 65 6d 65 6e 74 20 66 61  e measurement fa
1624e 69 6c 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20  iled, then.  ** 
1624f 70 72 6f 63 65 73 73 69 6e 67 20 61 62 6f 72 74  processing abort
16250 73 20 62 65 66 6f 72 65 20 65 6e 74 65 72 69 6e  s before enterin
16251 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20  g this routine. 
16252 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
16253 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
16254 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50  pBt->pPager, &nP
16255 61 67 65 29 3b 0a 20 20 69 66 28 20 4e 45 56 45  age);.  if( NEVE
16256 52 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29  R(rc!=SQLITE_OK)
16257 20 7c 7c 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20   || nPage>0 ){. 
16258 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
16259 7d 0a 20 20 70 50 31 20 3d 20 70 42 74 2d 3e 70  }.  pP1 = pBt->p
1625a 50 61 67 65 31 3b 0a 20 20 61 73 73 65 72 74 28  Page1;.  assert(
1625b 20 70 50 31 21 3d 30 20 29 3b 0a 20 20 64 61 74   pP1!=0 );.  dat
1625c 61 20 3d 20 70 50 31 2d 3e 61 44 61 74 61 3b 0a  a = pP1->aData;.
1625d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
1625e 67 65 72 57 72 69 74 65 28 70 50 31 2d 3e 70 44  gerWrite(pP1->pD
1625f 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63  bPage);.  if( rc
16260 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
16261 6d 65 6d 63 70 79 28 64 61 74 61 2c 20 7a 4d 61  memcpy(data, zMa
16262 67 69 63 48 65 61 64 65 72 2c 20 73 69 7a 65 6f  gicHeader, sizeo
16263 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29 29  f(zMagicHeader))
16264 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65  ;.  assert( size
16265 6f 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29  of(zMagicHeader)
16266 3d 3d 31 36 20 29 3b 0a 20 20 70 75 74 32 62 79  ==16 );.  put2by
16267 74 65 28 26 64 61 74 61 5b 31 36 5d 2c 20 70 42  te(&data[16], pB
16268 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20  t->pageSize);.  
16269 64 61 74 61 5b 31 38 5d 20 3d 20 31 3b 0a 20 20  data[18] = 1;.  
1626a 64 61 74 61 5b 31 39 5d 20 3d 20 31 3b 0a 20 20  data[19] = 1;.  
1626b 61 73 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61  assert( pBt->usa
1626c 62 6c 65 53 69 7a 65 3c 3d 70 42 74 2d 3e 70 61  bleSize<=pBt->pa
1626d 67 65 53 69 7a 65 20 26 26 20 70 42 74 2d 3e 75  geSize && pBt->u
1626e 73 61 62 6c 65 53 69 7a 65 2b 32 35 35 3e 3d 70  sableSize+255>=p
1626f 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20  Bt->pageSize);. 
16270 20 64 61 74 61 5b 32 30 5d 20 3d 20 28 75 38 29   data[20] = (u8)
16271 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d  (pBt->pageSize -
16272 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
16273 29 3b 0a 20 20 64 61 74 61 5b 32 31 5d 20 3d 20  );.  data[21] = 
16274 36 34 3b 0a 20 20 64 61 74 61 5b 32 32 5d 20 3d  64;.  data[22] =
16275 20 33 32 3b 0a 20 20 64 61 74 61 5b 32 33 5d 20   32;.  data[23] 
16276 3d 20 33 32 3b 0a 20 20 6d 65 6d 73 65 74 28 26  = 32;.  memset(&
16277 64 61 74 61 5b 32 34 5d 2c 20 30 2c 20 31 30 30  data[24], 0, 100
16278 2d 32 34 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65  -24);.  zeroPage
16279 28 70 50 31 2c 20 50 54 46 5f 49 4e 54 4b 45 59  (pP1, PTF_INTKEY
1627a 7c 50 54 46 5f 4c 45 41 46 7c 50 54 46 5f 4c 45  |PTF_LEAF|PTF_LE
1627b 41 46 44 41 54 41 20 29 3b 0a 20 20 70 42 74 2d  AFDATA );.  pBt-
1627c 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d  >pageSizeFixed =
1627d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   1;.#ifndef SQLI
1627e 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
1627f 55 4d 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  UM.  assert( pBt
16280 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d 31 20  ->autoVacuum==1 
16281 7c 7c 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  || pBt->autoVacu
16282 75 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  um==0 );.  asser
16283 74 28 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75  t( pBt->incrVacu
16284 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 69 6e  um==1 || pBt->in
16285 63 72 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20  crVacuum==0 );. 
16286 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61 5b   put4byte(&data[
16287 33 36 20 2b 20 34 2a 34 5d 2c 20 70 42 74 2d 3e  36 + 4*4], pBt->
16288 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20 70  autoVacuum);.  p
16289 75 74 34 62 79 74 65 28 26 64 61 74 61 5b 33 36  ut4byte(&data[36
1628a 20 2b 20 37 2a 34 5d 2c 20 70 42 74 2d 3e 69 6e   + 7*4], pBt->in
1628b 63 72 56 61 63 75 75 6d 29 3b 0a 23 65 6e 64 69  crVacuum);.#endi
1628c 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  f.  return SQLIT
1628d 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  E_OK;.}../*.** A
1628e 74 74 65 6d 70 74 20 74 6f 20 73 74 61 72 74 20  ttempt to start 
1628f 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f  a new transactio
16290 6e 2e 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73  n. A write-trans
16291 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 74 61  action.** is sta
16292 72 74 65 64 20 69 66 20 74 68 65 20 73 65 63 6f  rted if the seco
16293 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e  nd argument is n
16294 6f 6e 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73  onzero, otherwis
16295 65 20 61 20 72 65 61 64 2d 0a 2a 2a 20 74 72 61  e a read-.** tra
16296 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66 20 74 68  nsaction.  If th
16297 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
16298 74 20 69 73 20 32 20 6f 72 20 6d 6f 72 65 20 61  t is 2 or more a
16299 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20  nd exclusive.** 
1629a 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73  transaction is s
1629b 74 61 72 74 65 64 2c 20 6d 65 61 6e 69 6e 67 20  tarted, meaning 
1629c 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 70 72  that no other pr
1629d 6f 63 65 73 73 20 69 73 20 61 6c 6c 6f 77 65 64  ocess is allowed
1629e 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74 68  .** to access th
1629f 65 20 64 61 74 61 62 61 73 65 2e 20 20 41 20 70  e database.  A p
162a0 72 65 65 78 69 73 74 69 6e 67 20 74 72 61 6e 73  reexisting trans
162a1 61 63 74 69 6f 6e 20 6d 61 79 20 6e 6f 74 20 62  action may not b
162a2 65 0a 2a 2a 20 75 70 67 72 61 64 65 64 20 74 6f  e.** upgraded to
162a3 20 65 78 63 6c 75 73 69 76 65 20 62 79 20 63 61   exclusive by ca
162a4 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69  lling this routi
162a5 6e 65 20 61 20 73 65 63 6f 6e 64 20 74 69 6d 65  ne a second time
162a6 20 2d 20 74 68 65 0a 2a 2a 20 65 78 63 6c 75 73   - the.** exclus
162a7 69 76 69 74 79 20 66 6c 61 67 20 6f 6e 6c 79 20  ivity flag only 
162a8 77 6f 72 6b 73 20 66 6f 72 20 61 20 6e 65 77 20  works for a new 
162a9 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a  transaction..**.
162aa 2a 2a 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73  ** A write-trans
162ab 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73  action must be s
162ac 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 74  tarted before at
162ad 74 65 6d 70 74 69 6e 67 20 61 6e 79 20 0a 2a 2a  tempting any .**
162ae 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20   changes to the 
162af 64 61 74 61 62 61 73 65 2e 20 20 4e 6f 6e 65 20  database.  None 
162b0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
162b1 20 72 6f 75 74 69 6e 65 73 20 0a 2a 2a 20 77 69   routines .** wi
162b2 6c 6c 20 77 6f 72 6b 20 75 6e 6c 65 73 73 20 61  ll work unless a
162b3 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
162b4 73 74 61 72 74 65 64 20 66 69 72 73 74 3a 0a 2a  started first:.*
162b5 2a 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65  *.**      sqlite
162b6 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c  3BtreeCreateTabl
162b7 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69  e().**      sqli
162b8 74 65 33 42 74 72 65 65 43 72 65 61 74 65 49 6e  te3BtreeCreateIn
162b9 64 65 78 28 29 0a 2a 2a 20 20 20 20 20 20 73 71  dex().**      sq
162ba 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54  lite3BtreeClearT
162bb 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73  able().**      s
162bc 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54  qlite3BtreeDropT
162bd 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73  able().**      s
162be 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72  qlite3BtreeInser
162bf 74 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69  t().**      sqli
162c0 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 29  te3BtreeDelete()
162c1 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33  .**      sqlite3
162c2 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28  BtreeUpdateMeta(
162c3 29 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6e  ).**.** If an in
162c4 69 74 69 61 6c 20 61 74 74 65 6d 70 74 20 74 6f  itial attempt to
162c5 20 61 63 71 75 69 72 65 20 74 68 65 20 6c 6f 63   acquire the loc
162c6 6b 20 66 61 69 6c 73 20 62 65 63 61 75 73 65 20  k fails because 
162c7 6f 66 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69  of lock contenti
162c8 6f 6e 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61  on.** and the da
162c9 74 61 62 61 73 65 20 77 61 73 20 70 72 65 76 69  tabase was previ
162ca 6f 75 73 6c 79 20 75 6e 6c 6f 63 6b 65 64 2c 20  ously unlocked, 
162cb 74 68 65 6e 20 69 6e 76 6f 6b 65 20 74 68 65 20  then invoke the 
162cc 62 75 73 79 20 68 61 6e 64 6c 65 72 0a 2a 2a 20  busy handler.** 
162cd 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 2e  if there is one.
162ce 20 20 42 75 74 20 69 66 20 74 68 65 72 65 20 77    But if there w
162cf 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 61 20  as previously a 
162d0 72 65 61 64 2d 6c 6f 63 6b 2c 20 64 6f 20 6e 6f  read-lock, do no
162d1 74 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20  t.** invoke the 
162d2 62 75 73 79 20 68 61 6e 64 6c 65 72 20 2d 20 6a  busy handler - j
162d3 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ust return SQLIT
162d4 45 5f 42 55 53 59 2e 20 20 53 51 4c 49 54 45 5f  E_BUSY.  SQLITE_
162d5 42 55 53 59 20 69 73 20 0a 2a 2a 20 72 65 74 75  BUSY is .** retu
162d6 72 6e 65 64 20 77 68 65 6e 20 74 68 65 72 65 20  rned when there 
162d7 69 73 20 61 6c 72 65 61 64 79 20 61 20 72 65 61  is already a rea
162d8 64 2d 6c 6f 63 6b 20 69 6e 20 6f 72 64 65 72 20  d-lock in order 
162d9 74 6f 20 61 76 6f 69 64 20 61 20 64 65 61 64 6c  to avoid a deadl
162da 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f  ock..**.** Suppo
162db 73 65 20 74 68 65 72 65 20 61 72 65 20 74 77 6f  se there are two
162dc 20 70 72 6f 63 65 73 73 65 73 20 41 20 61 6e 64   processes A and
162dd 20 42 2e 20 20 41 20 68 61 73 20 61 20 72 65 61   B.  A has a rea
162de 64 20 6c 6f 63 6b 20 61 6e 64 20 42 20 68 61 73  d lock and B has
162df 0a 2a 2a 20 61 20 72 65 73 65 72 76 65 64 20 6c  .** a reserved l
162e0 6f 63 6b 2e 20 20 42 20 74 72 69 65 73 20 74 6f  ock.  B tries to
162e1 20 70 72 6f 6d 6f 74 65 20 74 6f 20 65 78 63 6c   promote to excl
162e2 75 73 69 76 65 20 62 75 74 20 69 73 20 62 6c 6f  usive but is blo
162e3 63 6b 65 64 20 62 65 63 61 75 73 65 0a 2a 2a 20  cked because.** 
162e4 6f 66 20 41 27 73 20 72 65 61 64 20 6c 6f 63 6b  of A's read lock
162e5 2e 20 20 41 20 74 72 69 65 73 20 74 6f 20 70 72  .  A tries to pr
162e6 6f 6d 6f 74 65 20 74 6f 20 72 65 73 65 72 76 65  omote to reserve
162e7 64 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64  d but is blocked
162e8 20 62 79 20 42 2e 0a 2a 2a 20 4f 6e 65 20 6f 72   by B..** One or
162e9 20 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74 68   the other of th
162ea 65 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20  e two processes 
162eb 6d 75 73 74 20 67 69 76 65 20 77 61 79 20 6f 72  must give way or
162ec 20 74 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a   there can be.**
162ed 20 6e 6f 20 70 72 6f 67 72 65 73 73 2e 20 20 42   no progress.  B
162ee 79 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c 49  y returning SQLI
162ef 54 45 5f 42 55 53 59 20 61 6e 64 20 6e 6f 74 20  TE_BUSY and not 
162f0 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73  invoking the bus
162f1 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 77 68  y callback.** wh
162f2 65 6e 20 41 20 61 6c 72 65 61 64 79 20 68 61 73  en A already has
162f3 20 61 20 72 65 61 64 20 6c 6f 63 6b 2c 20 77 65   a read lock, we
162f4 20 65 6e 63 6f 75 72 61 67 65 20 41 20 74 6f 20   encourage A to 
162f5 67 69 76 65 20 75 70 20 61 6e 64 20 6c 65 74 20  give up and let 
162f6 42 0a 2a 2a 20 70 72 6f 63 65 65 64 2e 0a 2a 2f  B.** proceed..*/
162f7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
162f8 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
162f9 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72 65 65  BeginTrans(Btree
162fa 20 2a 70 2c 20 69 6e 74 20 77 72 66 6c 61 67 29   *p, int wrflag)
162fb 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 42 6c  {.  sqlite3 *pBl
162fc 6f 63 6b 20 3d 20 30 3b 0a 20 20 42 74 53 68 61  ock = 0;.  BtSha
162fd 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
162fe 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  t;.  int rc = SQ
162ff 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69  LITE_OK;..  sqli
16300 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
16301 3b 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69  ;.  btreeIntegri
16302 74 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  ty(p);..  /* If 
16303 74 68 65 20 62 74 72 65 65 20 69 73 20 61 6c 72  the btree is alr
16304 65 61 64 79 20 69 6e 20 61 20 77 72 69 74 65 2d  eady in a write-
16305 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20  transaction, or 
16306 69 74 0a 20 20 2a 2a 20 69 73 20 61 6c 72 65 61  it.  ** is alrea
16307 64 79 20 69 6e 20 61 20 72 65 61 64 2d 74 72 61  dy in a read-tra
16308 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 61 20 72  nsaction and a r
16309 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a  ead-transaction.
1630a 20 20 2a 2a 20 69 73 20 72 65 71 75 65 73 74 65    ** is requeste
1630b 64 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d  d, this is a no-
1630c 6f 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  op..  */.  if( p
1630d 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53  ->inTrans==TRANS
1630e 5f 57 52 49 54 45 20 7c 7c 20 28 70 2d 3e 69 6e  _WRITE || (p->in
1630f 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 52 45 41  Trans==TRANS_REA
16310 44 20 26 26 20 21 77 72 66 6c 61 67 29 20 29 7b  D && !wrflag) ){
16311 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f  .    goto trans_
16312 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  begun;.  }..  /*
16313 20 57 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   Write transacti
16314 6f 6e 73 20 61 72 65 20 6e 6f 74 20 70 6f 73 73  ons are not poss
16315 69 62 6c 65 20 6f 6e 20 61 20 72 65 61 64 2d 6f  ible on a read-o
16316 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  nly database */.
16317 20 20 69 66 28 20 70 42 74 2d 3e 72 65 61 64 4f    if( pBt->readO
16318 6e 6c 79 20 26 26 20 77 72 66 6c 61 67 20 29 7b  nly && wrflag ){
16319 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1631a 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 67  _READONLY;.    g
1631b 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b  oto trans_begun;
1631c 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  .  }..#ifndef SQ
1631d 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
1631e 5f 43 41 43 48 45 0a 20 20 2f 2a 20 49 66 20 61  _CACHE.  /* If a
1631f 6e 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20  nother database 
16320 68 61 6e 64 6c 65 20 68 61 73 20 61 6c 72 65 61  handle has alrea
16321 64 79 20 6f 70 65 6e 65 64 20 61 20 77 72 69 74  dy opened a writ
16322 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 20  e transaction . 
16323 20 2a 2a 20 6f 6e 20 74 68 69 73 20 73 68 61 72   ** on this shar
16324 65 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 75  ed-btree structu
16325 72 65 20 61 6e 64 20 61 20 73 65 63 6f 6e 64 20  re and a second 
16326 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
16327 6e 20 69 73 0a 20 20 2a 2a 20 72 65 71 75 65 73  n is.  ** reques
16328 74 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  ted, return SQLI
16329 54 45 5f 4c 4f 43 4b 45 44 2e 0a 20 20 2a 2f 0a  TE_LOCKED..  */.
1632a 20 20 69 66 28 20 28 77 72 66 6c 61 67 20 26 26    if( (wrflag &&
1632b 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74   pBt->inTransact
1632c 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ion==TRANS_WRITE
1632d 29 20 7c 7c 20 70 42 74 2d 3e 69 73 50 65 6e 64  ) || pBt->isPend
1632e 69 6e 67 20 29 7b 0a 20 20 20 20 70 42 6c 6f 63  ing ){.    pBloc
1632f 6b 20 3d 20 70 42 74 2d 3e 70 57 72 69 74 65 72  k = pBt->pWriter
16330 2d 3e 64 62 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ->db;.  }else if
16331 28 20 77 72 66 6c 61 67 3e 31 20 29 7b 0a 20 20  ( wrflag>1 ){.  
16332 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b    BtLock *pIter;
16333 0a 20 20 20 20 66 6f 72 28 70 49 74 65 72 3d 70  .    for(pIter=p
16334 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72  Bt->pLock; pIter
16335 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70  ; pIter=pIter->p
16336 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28  Next){.      if(
16337 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 21 3d   pIter->pBtree!=
16338 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 6c  p ){.        pBl
16339 6f 63 6b 20 3d 20 70 49 74 65 72 2d 3e 70 42 74  ock = pIter->pBt
1633a 72 65 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 20  ree->db;.       
1633b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
1633c 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
1633d 70 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 73 71  pBlock ){.    sq
1633e 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42  lite3ConnectionB
1633f 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42  locked(p->db, pB
16340 6c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d 20  lock);.    rc = 
16341 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48  SQLITE_LOCKED_SH
16342 41 52 45 44 43 41 43 48 45 3b 0a 20 20 20 20 67  AREDCACHE;.    g
16343 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b  oto trans_begun;
16344 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
16345 2a 20 41 6e 79 20 72 65 61 64 2d 6f 6e 6c 79 20  * Any read-only 
16346 6f 72 20 72 65 61 64 2d 77 72 69 74 65 20 74 72  or read-write tr
16347 61 6e 73 61 63 74 69 6f 6e 20 69 6d 70 6c 69 65  ansaction implie
16348 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e  s a read-lock on
16349 20 0a 20 20 2a 2a 20 70 61 67 65 20 31 2e 20 53   .  ** page 1. S
1634a 6f 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20  o if some other 
1634b 73 68 61 72 65 64 2d 63 61 63 68 65 20 63 6c 69  shared-cache cli
1634c 65 6e 74 20 61 6c 72 65 61 64 79 20 68 61 73 20  ent already has 
1634d 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 0a 20 20  a write-lock .  
1634e 2a 2a 20 6f 6e 20 70 61 67 65 20 31 2c 20 74 68  ** on page 1, th
1634f 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61  e transaction ca
16350 6e 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64 2e 20  nnot be opened. 
16351 2a 2f 0a 20 20 72 63 20 3d 20 71 75 65 72 79 53  */.  rc = queryS
16352 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
16353 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f  ock(p, MASTER_RO
16354 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a  OT, READ_LOCK);.
16355 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
16356 3d 72 63 20 29 20 67 6f 74 6f 20 74 72 61 6e 73  =rc ) goto trans
16357 5f 62 65 67 75 6e 3b 0a 0a 20 20 64 6f 20 7b 0a  _begun;..  do {.
16358 20 20 20 20 2f 2a 20 43 61 6c 6c 20 6c 6f 63 6b      /* Call lock
16359 42 74 72 65 65 28 29 20 75 6e 74 69 6c 20 65 69  Btree() until ei
1635a 74 68 65 72 20 70 42 74 2d 3e 70 50 61 67 65 31  ther pBt->pPage1
1635b 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 6f 72   is populated or
1635c 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 42 74 72 65  .    ** lockBtre
1635d 65 28 29 20 72 65 74 75 72 6e 73 20 73 6f 6d 65  e() returns some
1635e 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e  thing other than
1635f 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 6c 6f 63 6b   SQLITE_OK. lock
16360 42 74 72 65 65 28 29 0a 20 20 20 20 2a 2a 20 6d  Btree().    ** m
16361 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ay return SQLITE
16362 5f 4f 4b 20 62 75 74 20 6c 65 61 76 65 20 70 42  _OK but leave pB
16363 74 2d 3e 70 50 61 67 65 31 20 73 65 74 20 74 6f  t->pPage1 set to
16364 20 30 20 69 66 20 61 66 74 65 72 0a 20 20 20 20   0 if after.    
16365 2a 2a 20 72 65 61 64 69 6e 67 20 70 61 67 65 20  ** reading page 
16366 31 20 69 74 20 64 69 73 63 6f 76 65 72 73 20 74  1 it discovers t
16367 68 61 74 20 74 68 65 20 70 61 67 65 2d 73 69 7a  hat the page-siz
16368 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
16369 65 20 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 69  e .    ** file i
1636a 73 20 6e 6f 74 20 70 42 74 2d 3e 70 61 67 65 53  s not pBt->pageS
1636b 69 7a 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ize. In this cas
1636c 65 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 77 69  e lockBtree() wi
1636d 6c 6c 20 75 70 64 61 74 65 0a 20 20 20 20 2a 2a  ll update.    **
1636e 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 74   pBt->pageSize t
1636f 6f 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20  o the page-size 
16370 6f 66 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64  of the file on d
16371 69 73 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  isk..    */.    
16372 77 68 69 6c 65 28 20 70 42 74 2d 3e 70 50 61 67  while( pBt->pPag
16373 65 31 3d 3d 30 20 26 26 20 53 51 4c 49 54 45 5f  e1==0 && SQLITE_
16374 4f 4b 3d 3d 28 72 63 20 3d 20 6c 6f 63 6b 42 74  OK==(rc = lockBt
16375 72 65 65 28 70 42 74 29 29 20 29 3b 0a 0a 20 20  ree(pBt)) );..  
16376 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
16377 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 7b  _OK && wrflag ){
16378 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e  .      if( pBt->
16379 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20  readOnly ){.    
1637a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1637b 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 20  READONLY;.      
1637c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72  }else{.        r
1637d 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
1637e 42 65 67 69 6e 28 70 42 74 2d 3e 70 50 61 67 65  Begin(pBt->pPage
1637f 72 2c 77 72 66 6c 61 67 3e 31 2c 73 71 6c 69 74  r,wrflag>1,sqlit
16380 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 70  e3TempInMemory(p
16381 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 20 20  ->db));.        
16382 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
16383 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  K ){.          r
16384 63 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 28  c = newDatabase(
16385 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  pBt);.        }.
16386 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
16387 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
16388 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
16389 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75  unlockBtreeIfUnu
1638a 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a  sed(pBt);.    }.
1638b 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51    }while( rc==SQ
1638c 4c 49 54 45 5f 42 55 53 59 20 26 26 20 70 42 74  LITE_BUSY && pBt
1638d 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d  ->inTransaction=
1638e 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20  =TRANS_NONE &&. 
1638f 20 20 20 20 20 20 20 20 20 62 74 72 65 65 49 6e           btreeIn
16390 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28  vokeBusyHandler(
16391 70 42 74 29 20 29 3b 0a 0a 20 20 69 66 28 20 72  pBt) );..  if( r
16392 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
16393 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61      if( p->inTra
16394 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29  ns==TRANS_NONE )
16395 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72  {.      pBt->nTr
16396 61 6e 73 61 63 74 69 6f 6e 2b 2b 3b 0a 23 69 66  ansaction++;.#if
16397 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
16398 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20  _SHARED_CACHE.  
16399 20 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61      if( p->shara
1639a 62 6c 65 20 29 7b 0a 09 61 73 73 65 72 74 28 20  ble ){..assert( 
1639b 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 3d 3d  p->lock.pBtree==
1639c 70 20 26 26 20 70 2d 3e 6c 6f 63 6b 2e 69 54 61  p && p->lock.iTa
1639d 62 6c 65 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20  ble==1 );.      
1639e 20 20 70 2d 3e 6c 6f 63 6b 2e 65 4c 6f 63 6b 20    p->lock.eLock 
1639f 3d 20 52 45 41 44 5f 4c 4f 43 4b 3b 0a 20 20 20  = READ_LOCK;.   
163a0 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 2e 70 4e 65       p->lock.pNe
163a1 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f 63 6b 3b  xt = pBt->pLock;
163a2 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 70 4c  .        pBt->pL
163a3 6f 63 6b 20 3d 20 26 70 2d 3e 6c 6f 63 6b 3b 0a  ock = &p->lock;.
163a4 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
163a5 20 20 20 7d 0a 20 20 20 20 70 2d 3e 69 6e 54 72     }.    p->inTr
163a6 61 6e 73 20 3d 20 28 77 72 66 6c 61 67 3f 54 52  ans = (wrflag?TR
163a7 41 4e 53 5f 57 52 49 54 45 3a 54 52 41 4e 53 5f  ANS_WRITE:TRANS_
163a8 52 45 41 44 29 3b 0a 20 20 20 20 69 66 28 20 70  READ);.    if( p
163a9 2d 3e 69 6e 54 72 61 6e 73 3e 70 42 74 2d 3e 69  ->inTrans>pBt->i
163aa 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a  nTransaction ){.
163ab 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61        pBt->inTra
163ac 6e 73 61 63 74 69 6f 6e 20 3d 20 70 2d 3e 69 6e  nsaction = p->in
163ad 54 72 61 6e 73 3b 0a 20 20 20 20 7d 0a 23 69 66  Trans;.    }.#if
163ae 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
163af 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20  _SHARED_CACHE.  
163b0 20 20 69 66 28 20 77 72 66 6c 61 67 20 29 7b 0a    if( wrflag ){.
163b1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
163b2 42 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a 20  Bt->pWriter );. 
163b3 20 20 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65       pBt->pWrite
163b4 72 20 3d 20 70 3b 0a 20 20 20 20 20 20 70 42 74  r = p;.      pBt
163b5 2d 3e 69 73 45 78 63 6c 75 73 69 76 65 20 3d 20  ->isExclusive = 
163b6 28 75 38 29 28 77 72 66 6c 61 67 3e 31 29 3b 0a  (u8)(wrflag>1);.
163b7 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
163b8 0a 0a 0a 74 72 61 6e 73 5f 62 65 67 75 6e 3a 0a  ...trans_begun:.
163b9 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
163ba 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 7b  _OK && wrflag ){
163bb 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6c  .    /* This cal
163bc 6c 20 6d 61 6b 65 73 20 73 75 72 65 20 74 68 61  l makes sure tha
163bd 74 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20  t the pager has 
163be 74 68 65 20 63 6f 72 72 65 63 74 20 6e 75 6d 62  the correct numb
163bf 65 72 20 6f 66 0a 20 20 20 20 2a 2a 20 6f 70 65  er of.    ** ope
163c0 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 49 66  n savepoints. If
163c1 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   the second para
163c2 6d 65 74 65 72 20 69 73 20 67 72 65 61 74 65 72  meter is greater
163c3 20 74 68 61 6e 20 30 20 61 6e 64 0a 20 20 20 20   than 0 and.    
163c4 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e  ** the sub-journ
163c5 61 6c 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64  al is not alread
163c6 79 20 6f 70 65 6e 2c 20 74 68 65 6e 20 69 74 20  y open, then it 
163c7 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 68  will be opened h
163c8 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ere..    */.    
163c9 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
163ca 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 70  rOpenSavepoint(p
163cb 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 2d 3e 64  Bt->pPager, p->d
163cc 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a  b->nSavepoint);.
163cd 20 20 7d 0a 0a 20 20 62 74 72 65 65 49 6e 74 65    }..  btreeInte
163ce 67 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69  grity(p);.  sqli
163cf 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
163d0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
163d1 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
163d2 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
163d3 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
163d4 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72  pointer-map entr
163d5 69 65 73 20 66 6f 72 20 61 6c 6c 20 63 68 69 6c  ies for all chil
163d6 64 72 65 6e 20 6f 66 20 70 61 67 65 20 70 50 61  dren of page pPa
163d7 67 65 2e 20 41 6c 73 6f 2c 20 69 66 0a 2a 2a 20  ge. Also, if.** 
163d8 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 63  pPage contains c
163d9 65 6c 6c 73 20 74 68 61 74 20 70 6f 69 6e 74 20  ells that point 
163da 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  to overflow page
163db 73 2c 20 73 65 74 20 74 68 65 20 70 6f 69 6e 74  s, set the point
163dc 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65  er.** map entrie
163dd 73 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c  s for the overfl
163de 6f 77 20 70 61 67 65 73 20 61 73 20 77 65 6c 6c  ow pages as well
163df 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
163e0 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28  setChildPtrmaps(
163e1 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b  MemPage *pPage){
163e2 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
163e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
163e4 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72        /* Counter
163e5 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 69   variable */.  i
163e6 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20  nt nCell;       
163e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
163e8 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
163e9 65 6c 6c 73 20 69 6e 20 70 61 67 65 20 70 50 61  ells in page pPa
163ea 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20  ge */.  int rc; 
163eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
163ec 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
163ed 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42  turn code */.  B
163ee 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
163ef 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20  Page->pBt;.  u8 
163f0 69 73 49 6e 69 74 4f 72 69 67 20 3d 20 70 50 61  isInitOrig = pPa
163f1 67 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 20 50 67  ge->isInit;.  Pg
163f2 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 61 67 65 2d  no pgno = pPage-
163f3 3e 70 67 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 74  >pgno;..  assert
163f4 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
163f5 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
163f6 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20  >mutex) );.  rc 
163f7 3d 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28  = btreeInitPage(
163f8 70 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63  pPage);.  if( rc
163f9 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
163fa 20 20 20 67 6f 74 6f 20 73 65 74 5f 63 68 69 6c     goto set_chil
163fb 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b 0a 20  d_ptrmaps_out;. 
163fc 20 7d 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61   }.  nCell = pPa
163fd 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 66 6f  ge->nCell;..  fo
163fe 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20  r(i=0; i<nCell; 
163ff 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 43  i++){.    u8 *pC
16400 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70  ell = findCell(p
16401 50 61 67 65 2c 20 69 29 3b 0a 0a 20 20 20 20 70  Page, i);..    p
16402 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28  trmapPutOvflPtr(
16403 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 72  pPage, pCell, &r
16404 63 29 3b 0a 0a 20 20 20 20 69 66 28 20 21 70 50  c);..    if( !pP
16405 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
16406 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e     Pgno childPgn
16407 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 43 65  o = get4byte(pCe
16408 6c 6c 29 3b 0a 20 20 20 20 20 20 70 74 72 6d 61  ll);.      ptrma
16409 70 50 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50  pPut(pBt, childP
1640a 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45  gno, PTRMAP_BTRE
1640b 45 2c 20 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20  E, pgno, &rc);. 
1640c 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
1640d 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
1640e 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67      Pgno childPg
1640f 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  no = get4byte(&p
16410 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67  Page->aData[pPag
16411 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  e->hdrOffset+8])
16412 3b 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28  ;.    ptrmapPut(
16413 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20  pBt, childPgno, 
16414 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67  PTRMAP_BTREE, pg
16415 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 7d 0a 0a 73  no, &rc);.  }..s
16416 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73  et_child_ptrmaps
16417 5f 6f 75 74 3a 0a 20 20 70 50 61 67 65 2d 3e 69  _out:.  pPage->i
16418 73 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72  sInit = isInitOr
16419 69 67 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ig;.  return rc;
1641a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 68  .}../*.** Somewh
1641b 65 72 65 20 6f 6e 20 70 50 61 67 65 20 69 73 20  ere on pPage is 
1641c 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 67  a pointer to pag
1641d 65 20 69 46 72 6f 6d 2e 20 20 4d 6f 64 69 66 79  e iFrom.  Modify
1641e 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 73 6f   this pointer so
1641f 0a 2a 2a 20 74 68 61 74 20 69 74 20 70 6f 69 6e  .** that it poin
16420 74 73 20 74 6f 20 69 54 6f 2e 20 50 61 72 61 6d  ts to iTo. Param
16421 65 74 65 72 20 65 54 79 70 65 20 64 65 73 63 72  eter eType descr
16422 69 62 65 73 20 74 68 65 20 74 79 70 65 20 6f 66  ibes the type of
16423 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 62   pointer to.** b
16424 65 20 6d 6f 64 69 66 69 65 64 2c 20 61 73 20 20  e modified, as  
16425 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 50  follows:.**.** P
16426 54 52 4d 41 50 5f 42 54 52 45 45 3a 20 20 20 20  TRMAP_BTREE:    
16427 20 70 50 61 67 65 20 69 73 20 61 20 62 74 72 65   pPage is a btre
16428 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e  e-page. The poin
16429 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20  ter points at a 
1642a 63 68 69 6c 64 20 0a 2a 2a 20 20 20 20 20 20 20  child .**       
1642b 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65              page
1642c 20 6f 66 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a   of pPage..**.**
1642d 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
1642e 31 3a 20 70 50 61 67 65 20 69 73 20 61 20 62 74  1: pPage is a bt
1642f 72 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f  ree-page. The po
16430 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20  inter points at 
16431 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 20  an overflow.**  
16432 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16433 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f   page pointed to
16434 20 62 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 63   by one of the c
16435 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 2e 0a 2a  ells on pPage..*
16436 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52  *.** PTRMAP_OVER
16437 46 4c 4f 57 32 3a 20 70 50 61 67 65 20 69 73 20  FLOW2: pPage is 
16438 61 6e 20 6f 76 65 72 66 6c 6f 77 2d 70 61 67 65  an overflow-page
16439 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f  . The pointer po
1643a 69 6e 74 73 20 61 74 20 74 68 65 20 6e 65 78 74  ints at the next
1643b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1643c 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70        overflow p
1643d 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74 2e  age in the list.
1643e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
1643f 6f 64 69 66 79 50 61 67 65 50 6f 69 6e 74 65 72  odifyPagePointer
16440 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
16441 20 50 67 6e 6f 20 69 46 72 6f 6d 2c 20 50 67 6e   Pgno iFrom, Pgn
16442 6f 20 69 54 6f 2c 20 75 38 20 65 54 79 70 65 29  o iTo, u8 eType)
16443 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
16444 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
16445 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
16446 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
16447 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
16448 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44  teable(pPage->pD
16449 62 50 61 67 65 29 20 29 3b 0a 20 20 69 66 28 20  bPage) );.  if( 
1644a 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56  eType==PTRMAP_OV
1644b 45 52 46 4c 4f 57 32 20 29 7b 0a 20 20 20 20 2f  ERFLOW2 ){.    /
1644c 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 73  * The pointer is
1644d 20 61 6c 77 61 79 73 20 74 68 65 20 66 69 72 73   always the firs
1644e 74 20 34 20 62 79 74 65 73 20 6f 66 20 74 68 65  t 4 bytes of the
1644f 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 63 61   page in this ca
16450 73 65 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  se.  */.    if( 
16451 67 65 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e  get4byte(pPage->
16452 61 44 61 74 61 29 21 3d 69 46 72 6f 6d 20 29 7b  aData)!=iFrom ){
16453 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
16454 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
16455 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 75 74  T;.    }.    put
16456 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61  4byte(pPage->aDa
16457 74 61 2c 20 69 54 6f 29 3b 0a 20 20 7d 65 6c 73  ta, iTo);.  }els
16458 65 7b 0a 20 20 20 20 75 38 20 69 73 49 6e 69 74  e{.    u8 isInit
16459 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 73  Orig = pPage->is
1645a 49 6e 69 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b  Init;.    int i;
1645b 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a  .    int nCell;.
1645c 0a 20 20 20 20 62 74 72 65 65 49 6e 69 74 50 61  .    btreeInitPa
1645d 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 6e  ge(pPage);.    n
1645e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43  Cell = pPage->nC
1645f 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d  ell;..    for(i=
16460 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29  0; i<nCell; i++)
16461 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 65 6c  {.      u8 *pCel
16462 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61  l = findCell(pPa
16463 67 65 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 66  ge, i);.      if
16464 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f  ( eType==PTRMAP_
16465 4f 56 45 52 46 4c 4f 57 31 20 29 7b 0a 20 20 20  OVERFLOW1 ){.   
16466 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e       CellInfo in
16467 66 6f 3b 0a 20 20 20 20 20 20 20 20 62 74 72 65  fo;.        btre
16468 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50  eParseCellPtr(pP
16469 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66  age, pCell, &inf
1646a 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  o);.        if( 
1646b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20 29  info.iOverflow )
1646c 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
1646d 69 46 72 6f 6d 3d 3d 67 65 74 34 62 79 74 65 28  iFrom==get4byte(
1646e 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65  &pCell[info.iOve
1646f 72 66 6c 6f 77 5d 29 20 29 7b 0a 20 20 20 20 20  rflow]) ){.     
16470 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28         put4byte(
16471 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65  &pCell[info.iOve
16472 72 66 6c 6f 77 5d 2c 20 69 54 6f 29 3b 0a 20 20  rflow], iTo);.  
16473 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
16474 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
16475 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
16476 73 65 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  se{.        if( 
16477 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3d  get4byte(pCell)=
16478 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20  =iFrom ){.      
16479 20 20 20 20 70 75 74 34 62 79 74 65 28 70 43 65      put4byte(pCe
1647a 6c 6c 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20  ll, iTo);.      
1647b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1647c 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1647d 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 69 3d   }.  .    if( i=
1647e 3d 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20  =nCell ){.      
1647f 69 66 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41  if( eType!=PTRMA
16480 50 5f 42 54 52 45 45 20 7c 7c 20 0a 20 20 20 20  P_BTREE || .    
16481 20 20 20 20 20 20 67 65 74 34 62 79 74 65 28 26        get4byte(&
16482 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61  pPage->aData[pPa
16483 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  ge->hdrOffset+8]
16484 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20  )!=iFrom ){.    
16485 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
16486 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
16487 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 75        }.      pu
16488 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61  t4byte(&pPage->a
16489 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f  Data[pPage->hdrO
1648a 66 66 73 65 74 2b 38 5d 2c 20 69 54 6f 29 3b 0a  ffset+8], iTo);.
1648b 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65      }..    pPage
1648c 2d 3e 69 73 49 6e 69 74 20 3d 20 69 73 49 6e 69  ->isInit = isIni
1648d 74 4f 72 69 67 3b 0a 20 20 7d 0a 20 20 72 65 74  tOrig;.  }.  ret
1648e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1648f 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68  .../*.** Move th
16490 65 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20  e open database 
16491 70 61 67 65 20 70 44 62 50 61 67 65 20 74 6f 20  page pDbPage to 
16492 6c 6f 63 61 74 69 6f 6e 20 69 46 72 65 65 50 61  location iFreePa
16493 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 64 61  ge in the .** da
16494 74 61 62 61 73 65 2e 20 54 68 65 20 70 44 62 50  tabase. The pDbP
16495 61 67 65 20 72 65 66 65 72 65 6e 63 65 20 72 65  age reference re
16496 6d 61 69 6e 73 20 76 61 6c 69 64 2e 0a 2a 2a 0a  mains valid..**.
16497 2a 2a 20 54 68 65 20 69 73 43 6f 6d 6d 69 74 20  ** The isCommit 
16498 66 6c 61 67 20 69 6e 64 69 63 61 74 65 73 20 74  flag indicates t
16499 68 61 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20  hat there is no 
1649a 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 65 72  need to remember
1649b 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6a 6f 75   that.** the jou
1649c 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65  rnal needs to be
1649d 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65   sync()ed before
1649e 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 70   database page p
1649f 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 0a 2a 2a  DbPage->pgno .**
164a0 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20   can be written 
164a1 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 68  to. The caller h
164a2 61 73 20 61 6c 72 65 61 64 79 20 70 72 6f 6d 69  as already promi
164a3 73 65 64 20 6e 6f 74 20 74 6f 20 77 72 69 74 65  sed not to write
164a4 20 74 6f 20 74 68 61 74 0a 2a 2a 20 70 61 67 65   to that.** page
164a5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
164a6 72 65 6c 6f 63 61 74 65 50 61 67 65 28 0a 20 20  relocatePage(.  
164a7 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 20  BtShared *pBt,  
164a8 20 20 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65           /* Btre
164a9 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a  e */.  MemPage *
164aa 70 44 62 50 61 67 65 2c 20 20 20 20 20 20 20 20  pDbPage,        
164ab 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 74 6f 20  /* Open page to 
164ac 6d 6f 76 65 20 2a 2f 0a 20 20 75 38 20 65 54 79  move */.  u8 eTy
164ad 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pe,             
164ae 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61     /* Pointer ma
164af 70 20 27 74 79 70 65 27 20 65 6e 74 72 79 20 66  p 'type' entry f
164b0 6f 72 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20  or pDbPage */.  
164b1 50 67 6e 6f 20 69 50 74 72 50 61 67 65 2c 20 20  Pgno iPtrPage,  
164b2 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
164b3 74 65 72 20 6d 61 70 20 27 70 61 67 65 2d 6e 6f  ter map 'page-no
164b4 27 20 65 6e 74 72 79 20 66 6f 72 20 70 44 62 50  ' entry for pDbP
164b5 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 46  age */.  Pgno iF
164b6 72 65 65 50 61 67 65 2c 20 20 20 20 20 20 20 20  reePage,        
164b7 20 20 2f 2a 20 54 68 65 20 6c 6f 63 61 74 69 6f    /* The locatio
164b8 6e 20 74 6f 20 6d 6f 76 65 20 70 44 62 50 61 67  n to move pDbPag
164b9 65 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 73  e to */.  int is
164ba 43 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 20 20  Commit          
164bb 20 20 20 2f 2a 20 69 73 43 6f 6d 6d 69 74 20 66     /* isCommit f
164bc 6c 61 67 20 70 61 73 73 65 64 20 74 6f 20 73 71  lag passed to sq
164bd 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61  lite3PagerMovepa
164be 67 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61  ge */.){.  MemPa
164bf 67 65 20 2a 70 50 74 72 50 61 67 65 3b 20 20 20  ge *pPtrPage;   
164c0 2f 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 74  /* The page that
164c1 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e   contains a poin
164c2 74 65 72 20 74 6f 20 70 44 62 50 61 67 65 20 2a  ter to pDbPage *
164c3 2f 0a 20 20 50 67 6e 6f 20 69 44 62 50 61 67 65  /.  Pgno iDbPage
164c4 20 3d 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f   = pDbPage->pgno
164c5 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65  ;.  Pager *pPage
164c6 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 72 3b  r = pBt->pPager;
164c7 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73  .  int rc;..  as
164c8 73 65 72 74 28 20 65 54 79 70 65 3d 3d 50 54 52  sert( eType==PTR
164c9 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 7c 7c  MAP_OVERFLOW2 ||
164ca 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f   eType==PTRMAP_O
164cb 56 45 52 46 4c 4f 57 31 20 7c 7c 20 0a 20 20 20  VERFLOW1 || .   
164cc 20 20 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50     eType==PTRMAP
164cd 5f 42 54 52 45 45 20 7c 7c 20 65 54 79 70 65 3d  _BTREE || eType=
164ce 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45  =PTRMAP_ROOTPAGE
164cf 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
164d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
164d1 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
164d2 20 20 61 73 73 65 72 74 28 20 70 44 62 50 61 67    assert( pDbPag
164d3 65 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b 0a 0a  e->pBt==pBt );..
164d4 20 20 2f 2a 20 4d 6f 76 65 20 70 61 67 65 20 69    /* Move page i
164d5 44 62 50 61 67 65 20 66 72 6f 6d 20 69 74 73 20  DbPage from its 
164d6 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e  current location
164d7 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 20   to page number 
164d8 69 46 72 65 65 50 61 67 65 20 2a 2f 0a 20 20 54  iFreePage */.  T
164d9 52 41 43 45 28 28 22 41 55 54 4f 56 41 43 55 55  RACE(("AUTOVACUU
164da 4d 3a 20 4d 6f 76 69 6e 67 20 25 64 20 74 6f 20  M: Moving %d to 
164db 66 72 65 65 20 70 61 67 65 20 25 64 20 28 70 74  free page %d (pt
164dc 72 20 70 61 67 65 20 25 64 20 74 79 70 65 20 25  r page %d type %
164dd 64 29 5c 6e 22 2c 20 0a 20 20 20 20 20 20 69 44  d)\n", .      iD
164de 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65  bPage, iFreePage
164df 2c 20 69 50 74 72 50 61 67 65 2c 20 65 54 79 70  , iPtrPage, eTyp
164e0 65 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  e));.  rc = sqli
164e1 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65  te3PagerMovepage
164e2 28 70 50 61 67 65 72 2c 20 70 44 62 50 61 67 65  (pPager, pDbPage
164e3 2d 3e 70 44 62 50 61 67 65 2c 20 69 46 72 65 65  ->pDbPage, iFree
164e4 50 61 67 65 2c 20 69 73 43 6f 6d 6d 69 74 29 3b  Page, isCommit);
164e5 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
164e6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
164e7 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 44 62  rn rc;.  }.  pDb
164e8 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 69 46 72  Page->pgno = iFr
164e9 65 65 50 61 67 65 3b 0a 0a 20 20 2f 2a 20 49 66  eePage;..  /* If
164ea 20 70 44 62 50 61 67 65 20 77 61 73 20 61 20 62   pDbPage was a b
164eb 74 72 65 65 2d 70 61 67 65 2c 20 74 68 65 6e 20  tree-page, then 
164ec 69 74 20 6d 61 79 20 68 61 76 65 20 63 68 69 6c  it may have chil
164ed 64 20 70 61 67 65 73 20 61 6e 64 2f 6f 72 20 63  d pages and/or c
164ee 65 6c 6c 73 0a 20 20 2a 2a 20 74 68 61 74 20 70  ells.  ** that p
164ef 6f 69 6e 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77  oint to overflow
164f0 20 70 61 67 65 73 2e 20 54 68 65 20 70 6f 69 6e   pages. The poin
164f1 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65 73 20  ter map entries 
164f2 66 6f 72 20 61 6c 6c 20 74 68 65 73 65 0a 20 20  for all these.  
164f3 2a 2a 20 70 61 67 65 73 20 6e 65 65 64 20 74 6f  ** pages need to
164f4 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a   be changed..  *
164f5 2a 0a 20 20 2a 2a 20 49 66 20 70 44 62 50 61 67  *.  ** If pDbPag
164f6 65 20 69 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77  e is an overflow
164f7 20 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20   page, then the 
164f8 66 69 72 73 74 20 34 20 62 79 74 65 73 20 6d 61  first 4 bytes ma
164f9 79 20 73 74 6f 72 65 20 61 0a 20 20 2a 2a 20 70  y store a.  ** p
164fa 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 75 62 73  ointer to a subs
164fb 65 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20  equent overflow 
164fc 70 61 67 65 2e 20 49 66 20 74 68 69 73 20 69 73  page. If this is
164fd 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e 0a   the case, then.
164fe 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72    ** the pointer
164ff 20 6d 61 70 20 6e 65 65 64 73 20 74 6f 20 62 65   map needs to be
16500 20 75 70 64 61 74 65 64 20 66 6f 72 20 74 68 65   updated for the
16501 20 73 75 62 73 65 71 75 65 6e 74 20 6f 76 65 72   subsequent over
16502 66 6c 6f 77 20 70 61 67 65 2e 0a 20 20 2a 2f 0a  flow page..  */.
16503 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52    if( eType==PTR
16504 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65 54 79  MAP_BTREE || eTy
16505 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50  pe==PTRMAP_ROOTP
16506 41 47 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  AGE ){.    rc = 
16507 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28  setChildPtrmaps(
16508 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66  pDbPage);.    if
16509 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1650a 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1650b 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  rc;.    }.  }els
1650c 65 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74  e{.    Pgno next
1650d 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28  Ovfl = get4byte(
1650e 70 44 62 50 61 67 65 2d 3e 61 44 61 74 61 29 3b  pDbPage->aData);
1650f 0a 20 20 20 20 69 66 28 20 6e 65 78 74 4f 76 66  .    if( nextOvf
16510 6c 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 74  l!=0 ){.      pt
16511 72 6d 61 70 50 75 74 28 70 42 74 2c 20 6e 65 78  rmapPut(pBt, nex
16512 74 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56  tOvfl, PTRMAP_OV
16513 45 52 46 4c 4f 57 32 2c 20 69 46 72 65 65 50 61  ERFLOW2, iFreePa
16514 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20  ge, &rc);.      
16515 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
16516 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  K ){.        ret
16517 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
16518 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
16519 46 69 78 20 74 68 65 20 64 61 74 61 62 61 73 65  Fix the database
1651a 20 70 6f 69 6e 74 65 72 20 6f 6e 20 70 61 67 65   pointer on page
1651b 20 69 50 74 72 50 61 67 65 20 74 68 61 74 20 70   iPtrPage that p
1651c 6f 69 6e 74 65 64 20 61 74 20 69 44 62 50 61 67  ointed at iDbPag
1651d 65 20 73 6f 0a 20 20 2a 2a 20 74 68 61 74 20 69  e so.  ** that i
1651e 74 20 70 6f 69 6e 74 73 20 61 74 20 69 46 72 65  t points at iFre
1651f 65 50 61 67 65 2e 20 41 6c 73 6f 20 66 69 78 20  ePage. Also fix 
16520 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  the pointer map 
16521 65 6e 74 72 79 20 66 6f 72 0a 20 20 2a 2a 20 69  entry for.  ** i
16522 50 74 72 50 61 67 65 2e 0a 20 20 2a 2f 0a 20 20  PtrPage..  */.  
16523 69 66 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41  if( eType!=PTRMA
16524 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20  P_ROOTPAGE ){.  
16525 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50    rc = btreeGetP
16526 61 67 65 28 70 42 74 2c 20 69 50 74 72 50 61 67  age(pBt, iPtrPag
16527 65 2c 20 26 70 50 74 72 50 61 67 65 2c 20 30 29  e, &pPtrPage, 0)
16528 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
16529 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1652a 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
1652b 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  }.    rc = sqlit
1652c 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 74  e3PagerWrite(pPt
1652d 72 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b  rPage->pDbPage);
1652e 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1652f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
16530 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 72  releasePage(pPtr
16531 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65 74  Page);.      ret
16532 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
16533 20 20 72 63 20 3d 20 6d 6f 64 69 66 79 50 61 67    rc = modifyPag
16534 65 50 6f 69 6e 74 65 72 28 70 50 74 72 50 61 67  ePointer(pPtrPag
16535 65 2c 20 69 44 62 50 61 67 65 2c 20 69 46 72 65  e, iDbPage, iFre
16536 65 50 61 67 65 2c 20 65 54 79 70 65 29 3b 0a 20  ePage, eType);. 
16537 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
16538 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66  PtrPage);.    if
16539 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1653a 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50  ){.      ptrmapP
1653b 75 74 28 70 42 74 2c 20 69 46 72 65 65 50 61 67  ut(pBt, iFreePag
1653c 65 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 61  e, eType, iPtrPa
1653d 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a  ge, &rc);.    }.
1653e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1653f 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64  .}../* Forward d
16540 65 63 6c 61 72 61 74 69 6f 6e 20 72 65 71 75 69  eclaration requi
16541 72 65 64 20 62 79 20 69 6e 63 72 56 61 63 75 75  red by incrVacuu
16542 6d 53 74 65 70 28 29 2e 20 2a 2f 0a 73 74 61 74  mStep(). */.stat
16543 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42  ic int allocateB
16544 74 72 65 65 50 61 67 65 28 42 74 53 68 61 72 65  treePage(BtShare
16545 64 20 2a 2c 20 4d 65 6d 50 61 67 65 20 2a 2a 2c  d *, MemPage **,
16546 20 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75   Pgno *, Pgno, u
16547 38 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f  8);../*.** Perfo
16548 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70  rm a single step
16549 20 6f 66 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74   of an increment
1654a 61 6c 2d 76 61 63 75 75 6d 2e 20 49 66 20 73 75  al-vacuum. If su
1654b 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 72 65 74  ccessful,.** ret
1654c 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49  urn SQLITE_OK. I
1654d 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 6f  f there is no wo
1654e 72 6b 20 74 6f 20 64 6f 20 28 61 6e 64 20 74 68  rk to do (and th
1654f 65 72 65 66 6f 72 65 20 6e 6f 0a 2a 2a 20 70 6f  erefore no.** po
16550 69 6e 74 20 69 6e 20 63 61 6c 6c 69 6e 67 20 74  int in calling t
16551 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 67 61  his function aga
16552 69 6e 29 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  in), return SQLI
16553 54 45 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d  TE_DONE..**.** M
16554 6f 72 65 20 73 70 65 63 69 66 69 63 6c 79 2c 20  ore specificly, 
16555 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74  this function at
16556 74 65 6d 70 74 73 20 74 6f 20 72 65 2d 6f 72 67  tempts to re-org
16557 61 6e 69 7a 65 20 74 68 65 20 0a 2a 2a 20 64 61  anize the .** da
16558 74 61 62 61 73 65 20 73 6f 20 74 68 61 74 20 74  tabase so that t
16559 68 65 20 6c 61 73 74 20 70 61 67 65 20 6f 66 20  he last page of 
1655a 74 68 65 20 66 69 6c 65 20 63 75 72 72 65 6e 74  the file current
1655b 6c 79 20 69 6e 20 75 73 65 0a 2a 2a 20 69 73 20  ly in use.** is 
1655c 6e 6f 20 6c 6f 6e 67 65 72 20 69 6e 20 75 73 65  no longer in use
1655d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e  ..**.** If the n
1655e 46 69 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73  Fin parameter is
1655f 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 69 73 20   non-zero, this 
16560 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73  function assumes
16561 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63 61 6c  .** that the cal
16562 6c 65 72 20 77 69 6c 6c 20 6b 65 65 70 20 63 61  ler will keep ca
16563 6c 6c 69 6e 67 20 69 6e 63 72 56 61 63 75 75 6d  lling incrVacuum
16564 53 74 65 70 28 29 20 75 6e 74 69 6c 0a 2a 2a 20  Step() until.** 
16565 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  it returns SQLIT
16566 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 65 72 72  E_DONE or an err
16567 6f 72 2c 20 61 6e 64 20 74 68 61 74 20 6e 46 69  or, and that nFi
16568 6e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62  n is the.** numb
16569 65 72 20 6f 66 20 70 61 67 65 73 20 74 68 65 20  er of pages the 
1656a 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 69  database file wi
1656b 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 66 74 65 72  ll contain after
1656c 20 74 68 69 73 20 0a 2a 2a 20 70 72 6f 63 65 73   this .** proces
1656d 73 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 20 20  s is complete.  
1656e 49 66 20 6e 46 69 6e 20 69 73 20 7a 65 72 6f 2c  If nFin is zero,
1656f 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 20 74   it is assumed t
16570 68 61 74 0a 2a 2a 20 69 6e 63 72 56 61 63 75 75  hat.** incrVacuu
16571 6d 53 74 65 70 28 29 20 77 69 6c 6c 20 62 65 20  mStep() will be 
16572 63 61 6c 6c 65 64 20 61 20 66 69 6e 69 74 65 20  called a finite 
16573 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 73 0a  amount of times.
16574 2a 2a 20 77 68 69 63 68 20 6d 61 79 20 6f 72 20  ** which may or 
16575 6d 61 79 20 6e 6f 74 20 65 6d 70 74 79 20 74 68  may not empty th
16576 65 20 66 72 65 65 6c 69 73 74 2e 20 20 41 20 66  e freelist.  A f
16577 75 6c 6c 20 61 75 74 6f 76 61 63 75 75 6d 0a 2a  ull autovacuum.*
16578 2a 20 68 61 73 20 6e 46 69 6e 3e 30 2e 20 20 41  * has nFin>0.  A
16579 20 22 50 52 41 47 4d 41 20 69 6e 63 72 65 6d 65   "PRAGMA increme
1657a 6e 74 61 6c 5f 76 61 63 75 75 6d 22 20 68 61 73  ntal_vacuum" has
1657b 20 6e 46 69 6e 3d 3d 30 2e 0a 2a 2f 0a 73 74 61   nFin==0..*/.sta
1657c 74 69 63 20 69 6e 74 20 69 6e 63 72 56 61 63 75  tic int incrVacu
1657d 75 6d 53 74 65 70 28 42 74 53 68 61 72 65 64 20  umStep(BtShared 
1657e 2a 70 42 74 2c 20 50 67 6e 6f 20 6e 46 69 6e 2c  *pBt, Pgno nFin,
1657f 20 50 67 6e 6f 20 69 4c 61 73 74 50 67 29 7b 0a   Pgno iLastPg){.
16580 20 20 50 67 6e 6f 20 6e 46 72 65 65 4c 69 73 74    Pgno nFreeList
16581 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
16582 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73  umber of pages s
16583 74 69 6c 6c 20 6f 6e 20 74 68 65 20 66 72 65 65  till on the free
16584 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20 61 73 73 65  -list */..  asse
16585 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
16586 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
16587 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
16588 69 4c 61 73 74 50 67 3e 6e 46 69 6e 20 29 3b 0a  iLastPg>nFin );.
16589 0a 20 20 69 66 28 20 21 50 54 52 4d 41 50 5f 49  .  if( !PTRMAP_I
1658a 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74  SPAGE(pBt, iLast
1658b 50 67 29 20 26 26 20 69 4c 61 73 74 50 67 21 3d  Pg) && iLastPg!=
1658c 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
1658d 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 69 6e  E(pBt) ){.    in
1658e 74 20 72 63 3b 0a 20 20 20 20 75 38 20 65 54 79  t rc;.    u8 eTy
1658f 70 65 3b 0a 20 20 20 20 50 67 6e 6f 20 69 50 74  pe;.    Pgno iPt
16590 72 50 61 67 65 3b 0a 0a 20 20 20 20 6e 46 72 65  rPage;..    nFre
16591 65 4c 69 73 74 20 3d 20 67 65 74 34 62 79 74 65  eList = get4byte
16592 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61  (&pBt->pPage1->a
16593 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20 69  Data[36]);.    i
16594 66 28 20 6e 46 72 65 65 4c 69 73 74 3d 3d 30 20  f( nFreeList==0 
16595 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
16596 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20  SQLITE_DONE;.   
16597 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 70 74 72   }..    rc = ptr
16598 6d 61 70 47 65 74 28 70 42 74 2c 20 69 4c 61 73  mapGet(pBt, iLas
16599 74 50 67 2c 20 26 65 54 79 70 65 2c 20 26 69 50  tPg, &eType, &iP
1659a 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28  trPage);.    if(
1659b 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1659c 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
1659d 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  c;.    }.    if(
1659e 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52   eType==PTRMAP_R
1659f 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 20  OOTPAGE ){.     
165a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
165a1 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
165a2 20 7d 0a 0a 20 20 20 20 69 66 28 20 65 54 79 70   }..    if( eTyp
165a3 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41  e==PTRMAP_FREEPA
165a4 47 45 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  GE ){.      if( 
165a5 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20  nFin==0 ){.     
165a6 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65     /* Remove the
165a7 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 66   page from the f
165a8 69 6c 65 73 20 66 72 65 65 2d 6c 69 73 74 2e 20  iles free-list. 
165a9 54 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75  This is not requ
165aa 69 72 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20  ired.        ** 
165ab 69 66 20 6e 46 69 6e 20 69 73 20 6e 6f 6e 2d 7a  if nFin is non-z
165ac 65 72 6f 2e 20 49 6e 20 74 68 61 74 20 63 61 73  ero. In that cas
165ad 65 2c 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  e, the free-list
165ae 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20   will be.       
165af 20 2a 2a 20 74 72 75 6e 63 61 74 65 64 20 74 6f   ** truncated to
165b0 20 7a 65 72 6f 20 61 66 74 65 72 20 74 68 69 73   zero after this
165b1 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
165b2 73 2c 20 73 6f 20 69 74 20 64 6f 65 73 6e 27 74  s, so it doesn't
165b3 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 74   .        ** mat
165b4 74 65 72 20 69 66 20 69 74 20 73 74 69 6c 6c 20  ter if it still 
165b5 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 67 61  contains some ga
165b6 72 62 61 67 65 20 65 6e 74 72 69 65 73 2e 0a 20  rbage entries.. 
165b7 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
165b8 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67 3b 0a    Pgno iFreePg;.
165b9 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20          MemPage 
165ba 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 20 20 20  *pFreePg;.      
165bb 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42    rc = allocateB
165bc 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70  treePage(pBt, &p
165bd 46 72 65 65 50 67 2c 20 26 69 46 72 65 65 50 67  FreePg, &iFreePg
165be 2c 20 69 4c 61 73 74 50 67 2c 20 31 29 3b 0a 20  , iLastPg, 1);. 
165bf 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
165c0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
165c1 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
165c2 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
165c3 20 20 20 61 73 73 65 72 74 28 20 69 46 72 65 65     assert( iFree
165c4 50 67 3d 3d 69 4c 61 73 74 50 67 20 29 3b 0a 20  Pg==iLastPg );. 
165c5 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61         releasePa
165c6 67 65 28 70 46 72 65 65 50 67 29 3b 0a 20 20 20  ge(pFreePg);.   
165c7 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20     }.    } else 
165c8 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72  {.      Pgno iFr
165c9 65 65 50 67 3b 20 20 20 20 20 20 20 20 20 20 20  eePg;           
165ca 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 72    /* Index of fr
165cb 65 65 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20  ee page to move 
165cc 70 4c 61 73 74 50 67 20 74 6f 20 2a 2f 0a 20 20  pLastPg to */.  
165cd 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c 61      MemPage *pLa
165ce 73 74 50 67 3b 0a 0a 20 20 20 20 20 20 72 63 20  stPg;..      rc 
165cf 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70  = btreeGetPage(p
165d0 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 70 4c  Bt, iLastPg, &pL
165d1 61 73 74 50 67 2c 20 30 29 3b 0a 20 20 20 20 20  astPg, 0);.     
165d2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
165d3 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  OK ){.        re
165d4 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
165d5 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 46  ..      /* If nF
165d6 69 6e 20 69 73 20 7a 65 72 6f 2c 20 74 68 69 73  in is zero, this
165d7 20 6c 6f 6f 70 20 72 75 6e 73 20 65 78 61 63 74   loop runs exact
165d8 6c 79 20 6f 6e 63 65 20 61 6e 64 20 70 61 67 65  ly once and page
165d9 20 70 4c 61 73 74 50 67 0a 20 20 20 20 20 20 2a   pLastPg.      *
165da 2a 20 69 73 20 73 77 61 70 70 65 64 20 77 69 74  * is swapped wit
165db 68 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65  h the first free
165dc 20 70 61 67 65 20 70 75 6c 6c 65 64 20 6f 66 66   page pulled off
165dd 20 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e 0a   the free list..
165de 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
165df 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68  * On the other h
165e0 61 6e 64 2c 20 69 66 20 6e 46 69 6e 20 69 73 20  and, if nFin is 
165e1 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72  greater than zer
165e2 6f 2c 20 74 68 65 6e 20 6b 65 65 70 0a 20 20 20  o, then keep.   
165e3 20 20 20 2a 2a 20 6c 6f 6f 70 69 6e 67 20 75 6e     ** looping un
165e4 74 69 6c 20 61 20 66 72 65 65 2d 70 61 67 65 20  til a free-page 
165e5 6c 6f 63 61 74 65 64 20 77 69 74 68 69 6e 20 74  located within t
165e6 68 65 20 66 69 72 73 74 20 6e 46 69 6e 20 70 61  he first nFin pa
165e7 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20  ges.      ** of 
165e8 74 68 65 20 66 69 6c 65 20 69 73 20 66 6f 75 6e  the file is foun
165e9 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  d..      */.    
165ea 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 4d    do {.        M
165eb 65 6d 50 61 67 65 20 2a 70 46 72 65 65 50 67 3b  emPage *pFreePg;
165ec 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 61 6c  .        rc = al
165ed 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28  locateBtreePage(
165ee 70 42 74 2c 20 26 70 46 72 65 65 50 67 2c 20 26  pBt, &pFreePg, &
165ef 69 46 72 65 65 50 67 2c 20 30 2c 20 30 29 3b 0a  iFreePg, 0, 0);.
165f0 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
165f1 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
165f2 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61         releasePa
165f3 67 65 28 70 4c 61 73 74 50 67 29 3b 0a 20 20 20  ge(pLastPg);.   
165f4 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
165f5 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
165f6 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
165f7 70 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20 20  pFreePg);.      
165f8 7d 77 68 69 6c 65 28 20 6e 46 69 6e 21 3d 30 20  }while( nFin!=0 
165f9 26 26 20 69 46 72 65 65 50 67 3e 6e 46 69 6e 20  && iFreePg>nFin 
165fa 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
165fb 20 69 46 72 65 65 50 67 3c 69 4c 61 73 74 50 67   iFreePg<iLastPg
165fc 20 29 3b 0a 20 20 20 20 20 20 0a 20 20 20 20 20   );.      .     
165fd 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
165fe 65 72 57 72 69 74 65 28 70 4c 61 73 74 50 67 2d  erWrite(pLastPg-
165ff 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
16600 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
16601 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
16602 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28   = relocatePage(
16603 70 42 74 2c 20 70 4c 61 73 74 50 67 2c 20 65 54  pBt, pLastPg, eT
16604 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20 69  ype, iPtrPage, i
16605 46 72 65 65 50 67 2c 20 6e 46 69 6e 21 3d 30 29  FreePg, nFin!=0)
16606 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
16607 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c 61 73  releasePage(pLas
16608 74 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20  tPg);.      if( 
16609 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1660a 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
1660b 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
1660c 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e 46 69  }.  }..  if( nFi
1660d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 4c 61 73  n==0 ){.    iLas
1660e 74 50 67 2d 2d 3b 0a 20 20 20 20 77 68 69 6c 65  tPg--;.    while
1660f 28 20 69 4c 61 73 74 50 67 3d 3d 50 45 4e 44 49  ( iLastPg==PENDI
16610 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
16611 29 7c 7c 50 54 52 4d 41 50 5f 49 53 50 41 47 45  )||PTRMAP_ISPAGE
16612 28 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20 29  (pBt, iLastPg) )
16613 7b 0a 20 20 20 20 20 20 69 66 28 20 50 54 52 4d  {.      if( PTRM
16614 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69  AP_ISPAGE(pBt, i
16615 4c 61 73 74 50 67 29 20 29 7b 0a 20 20 20 20 20  LastPg) ){.     
16616 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 3b     MemPage *pPg;
16617 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 20  .        int rc 
16618 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70  = btreeGetPage(p
16619 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 70 50  Bt, iLastPg, &pP
1661a 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69  g, 0);.        i
1661b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1661c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
1661d 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20  turn rc;.       
1661e 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20   }.        rc = 
1661f 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
16620 65 28 70 50 67 2d 3e 70 44 62 50 61 67 65 29 3b  e(pPg->pDbPage);
16621 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
16622 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 20 20  Page(pPg);.     
16623 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
16624 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
16625 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
16626 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
16627 20 20 20 20 20 69 4c 61 73 74 50 67 2d 2d 3b 0a       iLastPg--;.
16628 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
16629 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d  3PagerTruncateIm
1662a 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  age(pBt->pPager,
1662b 20 69 4c 61 73 74 50 67 29 3b 0a 20 20 7d 0a 20   iLastPg);.  }. 
1662c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1662d 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 77 72  K;.}../*.** A wr
1662e 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite-transaction 
1662f 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 64 20 62  must be opened b
16630 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74 68  efore calling th
16631 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20  is function..** 
16632 49 74 20 70 65 72 66 6f 72 6d 73 20 61 20 73 69  It performs a si
16633 6e 67 6c 65 20 75 6e 69 74 20 6f 66 20 77 6f 72  ngle unit of wor
16634 6b 20 74 6f 77 61 72 64 73 20 61 6e 20 69 6e 63  k towards an inc
16635 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 2e  remental vacuum.
16636 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e  .**.** If the in
16637 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d  cremental vacuum
16638 20 69 73 20 66 69 6e 69 73 68 65 64 20 61 66 74   is finished aft
16639 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  er this function
1663a 20 68 61 73 20 72 75 6e 2c 0a 2a 2a 20 53 51 4c   has run,.** SQL
1663b 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65 74 75  ITE_DONE is retu
1663c 72 6e 65 64 2e 20 49 66 20 69 74 20 69 73 20 6e  rned. If it is n
1663d 6f 74 20 66 69 6e 69 73 68 65 64 2c 20 62 75 74  ot finished, but
1663e 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 72   no error occurr
1663f 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  ed,.** SQLITE_OK
16640 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74   is returned. Ot
16641 68 65 72 77 69 73 65 20 61 6e 20 53 51 4c 69 74  herwise an SQLit
16642 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 0a 2a  e error code. .*
16643 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16644 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
16645 65 49 6e 63 72 56 61 63 75 75 6d 28 42 74 72 65  eIncrVacuum(Btre
16646 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b  e *p){.  int rc;
16647 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
16648 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71   = p->pBt;..  sq
16649 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
1664a 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42  p);.  assert( pB
1664b 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
1664c 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 26 26  ==TRANS_WRITE &&
1664d 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41   p->inTrans==TRA
1664e 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 66  NS_WRITE );.  if
1664f 28 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  ( !pBt->autoVacu
16650 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  um ){.    rc = S
16651 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65  QLITE_DONE;.  }e
16652 6c 73 65 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64  lse{.    invalid
16653 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61  ateAllOverflowCa
16654 63 68 65 28 70 42 74 29 3b 0a 20 20 20 20 72 63  che(pBt);.    rc
16655 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65   = incrVacuumSte
16656 70 28 70 42 74 2c 20 30 2c 20 70 61 67 65 72 50  p(pBt, 0, pagerP
16657 61 67 65 63 6f 75 6e 74 28 70 42 74 29 29 3b 0a  agecount(pBt));.
16658 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72    }.  sqlite3Btr
16659 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
1665a 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1665b 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
1665c 73 20 63 61 6c 6c 65 64 20 70 72 69 6f 72 20 74  s called prior t
1665d 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  o sqlite3PagerCo
1665e 6d 6d 69 74 20 77 68 65 6e 20 61 20 74 72 61 6e  mmit when a tran
1665f 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f  saction.** is co
16660 6d 6d 69 74 65 64 20 66 6f 72 20 61 6e 20 61 75  mmited for an au
16661 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61  to-vacuum databa
16662 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c  se..**.** If SQL
16663 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
16664 65 64 2c 20 74 68 65 6e 20 2a 70 6e 54 72 75 6e  ed, then *pnTrun
16665 63 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  c is set to the 
16666 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 0a  number of pages.
16667 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
16668 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 74  file should be t
16669 72 75 6e 63 61 74 65 64 20 74 6f 20 64 75 72 69  runcated to duri
1666a 6e 67 20 74 68 65 20 63 6f 6d 6d 69 74 20 70 72  ng the commit pr
1666b 6f 63 65 73 73 2e 20 0a 2a 2a 20 69 2e 65 2e 20  ocess. .** i.e. 
1666c 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73  the database has
1666d 20 62 65 65 6e 20 72 65 6f 72 67 61 6e 69 7a 65   been reorganize
1666e 64 20 73 6f 20 74 68 61 74 20 6f 6e 6c 79 20 74  d so that only t
1666f 68 65 20 66 69 72 73 74 20 2a 70 6e 54 72 75 6e  he first *pnTrun
16670 63 0a 2a 2a 20 70 61 67 65 73 20 61 72 65 20 69  c.** pages are i
16671 6e 20 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  n use..*/.static
16672 20 69 6e 74 20 61 75 74 6f 56 61 63 75 75 6d 43   int autoVacuumC
16673 6f 6d 6d 69 74 28 42 74 53 68 61 72 65 64 20 2a  ommit(BtShared *
16674 70 42 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  pBt){.  int rc =
16675 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 61   SQLITE_OK;.  Pa
16676 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 42  ger *pPager = pB
16677 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 56 56 41  t->pPager;.  VVA
16678 5f 4f 4e 4c 59 28 20 69 6e 74 20 6e 52 65 66 20  _ONLY( int nRef 
16679 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65  = sqlite3PagerRe
1667a 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 20 29  fcount(pPager) )
1667b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
1667c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1667d 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
1667e 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76   invalidateAllOv
1667f 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29  erflowCache(pBt)
16680 3b 0a 20 20 61 73 73 65 72 74 28 70 42 74 2d 3e  ;.  assert(pBt->
16681 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20 69  autoVacuum);.  i
16682 66 28 20 21 70 42 74 2d 3e 69 6e 63 72 56 61 63  f( !pBt->incrVac
16683 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20  uum ){.    Pgno 
16684 6e 46 69 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a  nFin;         /*
16685 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
16686 20 69 6e 20 64 61 74 61 62 61 73 65 20 61 66 74   in database aft
16687 65 72 20 61 75 74 6f 76 61 63 75 75 6d 69 6e 67  er autovacuuming
16688 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 46 72   */.    Pgno nFr
16689 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ee;        /* Nu
1668a 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e  mber of pages on
1668b 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 6e   the freelist in
1668c 69 74 69 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 50  itially */.    P
1668d 67 6e 6f 20 6e 50 74 72 6d 61 70 3b 20 20 20 20  gno nPtrmap;    
1668e 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 50    /* Number of P
1668f 74 72 4d 61 70 20 70 61 67 65 73 20 74 6f 20 62  trMap pages to b
16690 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20 50  e freed */.    P
16691 67 6e 6f 20 69 46 72 65 65 3b 20 20 20 20 20 20  gno iFree;      
16692 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 70 61    /* The next pa
16693 67 65 20 74 6f 20 62 65 20 66 72 65 65 64 20 2a  ge to be freed *
16694 2f 0a 20 20 20 20 69 6e 74 20 6e 45 6e 74 72 79  /.    int nEntry
16695 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ;        /* Numb
16696 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 6f 6e  er of entries on
16697 20 6f 6e 65 20 70 74 72 6d 61 70 20 70 61 67 65   one ptrmap page
16698 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 4f 72   */.    Pgno nOr
16699 69 67 3b 20 20 20 20 20 20 20 20 2f 2a 20 44 61  ig;        /* Da
1669a 74 61 62 61 73 65 20 73 69 7a 65 20 62 65 66 6f  tabase size befo
1669b 72 65 20 66 72 65 65 69 6e 67 20 2a 2f 0a 0a 20  re freeing */.. 
1669c 20 20 20 6e 4f 72 69 67 20 3d 20 70 61 67 65 72     nOrig = pager
1669d 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a  Pagecount(pBt);.
1669e 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49      if( PTRMAP_I
1669f 53 50 41 47 45 28 70 42 74 2c 20 6e 4f 72 69 67  SPAGE(pBt, nOrig
166a0 29 20 7c 7c 20 6e 4f 72 69 67 3d 3d 50 45 4e 44  ) || nOrig==PEND
166a1 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
166a2 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  t) ){.      /* I
166a3 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
166a4 65 20 74 6f 20 63 72 65 61 74 65 20 61 20 64 61  e to create a da
166a5 74 61 62 61 73 65 20 66 6f 72 20 77 68 69 63 68  tabase for which
166a6 20 74 68 65 20 66 69 6e 61 6c 20 70 61 67 65 0a   the final page.
166a7 20 20 20 20 20 20 2a 2a 20 69 73 20 65 69 74 68        ** is eith
166a8 65 72 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70  er a pointer-map
166a9 20 70 61 67 65 20 6f 72 20 74 68 65 20 70 65 6e   page or the pen
166aa 64 69 6e 67 2d 62 79 74 65 20 70 61 67 65 2e 20  ding-byte page. 
166ab 49 66 20 6f 6e 65 0a 20 20 20 20 20 20 2a 2a 20  If one.      ** 
166ac 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20  is encountered, 
166ad 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 63  this indicates c
166ae 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20 20  orruption..     
166af 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e   */.      return
166b0 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
166b1 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  BKPT;.    }..   
166b2 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74   nFree = get4byt
166b3 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e  e(&pBt->pPage1->
166b4 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20  aData[36]);.    
166b5 6e 45 6e 74 72 79 20 3d 20 70 42 74 2d 3e 75 73  nEntry = pBt->us
166b6 61 62 6c 65 53 69 7a 65 2f 35 3b 0a 20 20 20 20  ableSize/5;.    
166b7 6e 50 74 72 6d 61 70 20 3d 20 28 6e 46 72 65 65  nPtrmap = (nFree
166b8 2d 6e 4f 72 69 67 2b 50 54 52 4d 41 50 5f 50 41  -nOrig+PTRMAP_PA
166b9 47 45 4e 4f 28 70 42 74 2c 20 6e 4f 72 69 67 29  GENO(pBt, nOrig)
166ba 2b 6e 45 6e 74 72 79 29 2f 6e 45 6e 74 72 79 3b  +nEntry)/nEntry;
166bb 0a 20 20 20 20 6e 46 69 6e 20 3d 20 6e 4f 72 69  .    nFin = nOri
166bc 67 20 2d 20 6e 46 72 65 65 20 2d 20 6e 50 74 72  g - nFree - nPtr
166bd 6d 61 70 3b 0a 20 20 20 20 69 66 28 20 6e 4f 72  map;.    if( nOr
166be 69 67 3e 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  ig>PENDING_BYTE_
166bf 50 41 47 45 28 70 42 74 29 20 26 26 20 6e 46 69  PAGE(pBt) && nFi
166c0 6e 3c 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  n<PENDING_BYTE_P
166c1 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20  AGE(pBt) ){.    
166c2 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a    nFin--;.    }.
166c3 20 20 20 20 77 68 69 6c 65 28 20 50 54 52 4d 41      while( PTRMA
166c4 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 6e 46  P_ISPAGE(pBt, nF
166c5 69 6e 29 20 7c 7c 20 6e 46 69 6e 3d 3d 50 45 4e  in) || nFin==PEN
166c6 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
166c7 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 69  Bt) ){.      nFi
166c8 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  n--;.    }.    i
166c9 66 28 20 6e 46 69 6e 3e 6e 4f 72 69 67 20 29 20  f( nFin>nOrig ) 
166ca 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
166cb 52 52 55 50 54 5f 42 4b 50 54 3b 0a 0a 20 20 20  RRUPT_BKPT;..   
166cc 20 66 6f 72 28 69 46 72 65 65 3d 6e 4f 72 69 67   for(iFree=nOrig
166cd 3b 20 69 46 72 65 65 3e 6e 46 69 6e 20 26 26 20  ; iFree>nFin && 
166ce 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69  rc==SQLITE_OK; i
166cf 46 72 65 65 2d 2d 29 7b 0a 20 20 20 20 20 20 72  Free--){.      r
166d0 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74  c = incrVacuumSt
166d1 65 70 28 70 42 74 2c 20 6e 46 69 6e 2c 20 69 46  ep(pBt, nFin, iF
166d2 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ree);.    }.    
166d3 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f  if( (rc==SQLITE_
166d4 44 4f 4e 45 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  DONE || rc==SQLI
166d5 54 45 5f 4f 4b 29 20 26 26 20 6e 46 72 65 65 3e  TE_OK) && nFree>
166d6 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
166d7 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
166d8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
166d9 65 72 57 72 69 74 65 28 70 42 74 2d 3e 70 50 61  erWrite(pBt->pPa
166da 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  ge1->pDbPage);. 
166db 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70       put4byte(&p
166dc 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74  Bt->pPage1->aDat
166dd 61 5b 33 32 5d 2c 20 30 29 3b 0a 20 20 20 20 20  a[32], 0);.     
166de 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e   put4byte(&pBt->
166df 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36  pPage1->aData[36
166e0 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  ], 0);.      sql
166e1 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
166e2 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 50 61 67  eImage(pBt->pPag
166e3 65 72 2c 20 6e 46 69 6e 29 3b 0a 20 20 20 20 7d  er, nFin);.    }
166e4 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
166e5 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
166e6 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c  sqlite3PagerRoll
166e7 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20  back(pPager);.  
166e8 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72    }.  }..  asser
166e9 74 28 20 6e 52 65 66 3d 3d 73 71 6c 69 74 65 33  t( nRef==sqlite3
166ea 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 50  PagerRefcount(pP
166eb 61 67 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72  ager) );.  retur
166ec 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f  n rc;.}..#else /
166ed 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  * ifndef SQLITE_
166ee 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 20  OMIT_AUTOVACUUM 
166ef 2a 2f 0a 23 20 64 65 66 69 6e 65 20 73 65 74 43  */.# define setC
166f0 68 69 6c 64 50 74 72 6d 61 70 73 28 78 29 20 53  hildPtrmaps(x) S
166f1 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a  QLITE_OK.#endif.
166f2 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
166f3 69 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72  ine does the fir
166f4 73 74 20 70 68 61 73 65 20 6f 66 20 61 20 74 77  st phase of a tw
166f5 6f 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20  o-phase commit. 
166f6 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   This routine.**
166f7 20 63 61 75 73 65 73 20 61 20 72 6f 6c 6c 62 61   causes a rollba
166f8 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 65  ck journal to be
166f9 20 63 72 65 61 74 65 64 20 28 69 66 20 69 74 20   created (if it 
166fa 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  does not already
166fb 20 65 78 69 73 74 29 0a 2a 2a 20 61 6e 64 20 70   exist).** and p
166fc 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 65 6e  opulated with en
166fd 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ough information
166fe 20 73 6f 20 74 68 61 74 20 69 66 20 61 20 70 6f   so that if a po
166ff 77 65 72 20 6c 6f 73 73 20 6f 63 63 75 72 73 0a  wer loss occurs.
16700 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
16701 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20  can be restored 
16702 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20  to its original 
16703 73 74 61 74 65 20 62 79 20 70 6c 61 79 69 6e 67  state by playing
16704 20 62 61 63 6b 0a 2a 2a 20 74 68 65 20 6a 6f 75   back.** the jou
16705 72 6e 61 6c 2e 20 20 54 68 65 6e 20 74 68 65 20  rnal.  Then the 
16706 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
16707 6a 6f 75 72 6e 61 6c 20 61 72 65 20 66 6c 75 73  journal are flus
16708 68 65 64 20 6f 75 74 20 74 6f 0a 2a 2a 20 74 68  hed out to.** th
16709 65 20 64 69 73 6b 2e 20 20 41 66 74 65 72 20 74  e disk.  After t
1670a 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 61  he journal is sa
1670b 66 65 6c 79 20 6f 6e 20 6f 78 69 64 65 2c 20 74  fely on oxide, t
1670c 68 65 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68  he changes to th
1670d 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 72  e.** database ar
1670e 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  e written into t
1670f 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
16710 20 61 6e 64 20 66 6c 75 73 68 65 64 20 74 6f 20   and flushed to 
16711 6f 78 69 64 65 2e 0a 2a 2a 20 41 74 20 74 68 65  oxide..** At the
16712 20 65 6e 64 20 6f 66 20 74 68 69 73 20 63 61 6c   end of this cal
16713 6c 2c 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  l, the rollback 
16714 6a 6f 75 72 6e 61 6c 20 73 74 69 6c 6c 20 65 78  journal still ex
16715 69 73 74 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 64  ists on the.** d
16716 69 73 6b 20 61 6e 64 20 77 65 20 61 72 65 20 73  isk and we are s
16717 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 6c 6c  till holding all
16718 20 6c 6f 63 6b 73 2c 20 73 6f 20 74 68 65 20 74   locks, so the t
16719 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6e  ransaction has n
1671a 6f 74 0a 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 2e  ot.** committed.
1671b 20 20 53 65 65 20 73 71 6c 69 74 65 33 42 74 72    See sqlite3Btr
1671c 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  eeCommitPhaseTwo
1671d 28 29 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e  () for the secon
1671e 64 20 70 68 61 73 65 20 6f 66 20 74 68 65 0a 2a  d phase of the.*
1671f 2a 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73  * commit process
16720 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c  ..**.** This cal
16721 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20  l is a no-op if 
16722 6e 6f 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63  no write-transac
16723 74 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 6c  tion is currentl
16724 79 20 61 63 74 69 76 65 20 6f 6e 20 70 42 74 2e  y active on pBt.
16725 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  .**.** Otherwise
16726 2c 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 62  , sync the datab
16727 61 73 65 20 66 69 6c 65 20 66 6f 72 20 74 68 65  ase file for the
16728 20 62 74 72 65 65 20 70 42 74 2e 20 7a 4d 61 73   btree pBt. zMas
16729 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a  ter points to.**
1672a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d   the name of a m
1672b 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
1672c 6c 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62  le that should b
1672d 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  e written into t
1672e 68 65 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c  he.** individual
1672f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6f   journal file, o
16730 72 20 69 73 20 4e 55 4c 4c 2c 20 69 6e 64 69 63  r is NULL, indic
16731 61 74 69 6e 67 20 6e 6f 20 6d 61 73 74 65 72 20  ating no master 
16732 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a  journal file .**
16733 20 28 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73   (single databas
16734 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 0a  e transaction)..
16735 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20  **.** When this 
16736 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 6d  is called, the m
16737 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 73 68  aster journal sh
16738 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 61 76  ould already hav
16739 65 20 62 65 65 6e 0a 2a 2a 20 63 72 65 61 74 65  e been.** create
1673a 64 2c 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74  d, populated wit
1673b 68 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 70  h this journal p
1673c 6f 69 6e 74 65 72 20 61 6e 64 20 73 79 6e 63 65  ointer and synce
1673d 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a  d to disk..**.**
1673e 20 4f 6e 63 65 20 74 68 69 73 20 69 73 20 72 6f   Once this is ro
1673f 75 74 69 6e 65 20 68 61 73 20 72 65 74 75 72 6e  utine has return
16740 65 64 2c 20 74 68 65 20 6f 6e 6c 79 20 74 68 69  ed, the only thi
16741 6e 67 20 72 65 71 75 69 72 65 64 20 74 6f 20 63  ng required to c
16742 6f 6d 6d 69 74 0a 2a 2a 20 74 68 65 20 77 72 69  ommit.** the wri
16743 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 66  te-transaction f
16744 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73 65  or this database
16745 20 66 69 6c 65 20 69 73 20 74 6f 20 64 65 6c 65   file is to dele
16746 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a  te the journal..
16747 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
16748 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
16749 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  eeCommitPhaseOne
1674a 28 42 74 72 65 65 20 2a 70 2c 20 63 6f 6e 73 74  (Btree *p, const
1674b 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b   char *zMaster){
1674c 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1674d 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e  TE_OK;.  if( p->
1674e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57  inTrans==TRANS_W
1674f 52 49 54 45 20 29 7b 0a 20 20 20 20 42 74 53 68  RITE ){.    BtSh
16750 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
16751 42 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  Bt;.    sqlite3B
16752 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 23 69  treeEnter(p);.#i
16753 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
16754 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
16755 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61   if( pBt->autoVa
16756 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 63  cuum ){.      rc
16757 20 3d 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d   = autoVacuumCom
16758 6d 69 74 28 70 42 74 29 3b 0a 20 20 20 20 20 20  mit(pBt);.      
16759 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1675a 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  K ){.        sql
1675b 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1675c 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
1675d 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n rc;.      }.  
1675e 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 72    }.#endif.    r
1675f 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
16760 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70  CommitPhaseOne(p
16761 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a 4d 61 73  Bt->pPager, zMas
16762 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  ter, 0);.    sql
16763 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
16764 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
16765 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
16766 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
16767 6c 6c 65 64 20 66 72 6f 6d 20 62 6f 74 68 20 42  lled from both B
16768 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54  treeCommitPhaseT
16769 77 6f 28 29 20 61 6e 64 20 42 74 72 65 65 52 6f  wo() and BtreeRo
1676a 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 61 74 20 74  llback().** at t
1676b 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66  he conclusion of
1676c 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a   a transaction..
1676d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62  */.static void b
1676e 74 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74 69  treeEndTransacti
1676f 6f 6e 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  on(Btree *p){.  
16770 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
16771 70 2d 3e 70 42 74 3b 0a 20 20 42 74 43 75 72 73  p->pBt;.  BtCurs
16772 6f 72 20 2a 70 43 73 72 3b 0a 20 20 61 73 73 65  or *pCsr;.  asse
16773 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
16774 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b  HoldsMutex(p) );
16775 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f  ..  /* Search fo
16776 72 20 61 20 63 75 72 73 6f 72 20 68 65 6c 64 20  r a cursor held 
16777 6f 70 65 6e 20 62 79 20 74 68 69 73 20 62 2d 74  open by this b-t
16778 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  ree connection. 
16779 49 66 20 6f 6e 65 20 65 78 69 73 74 73 2c 0a 20  If one exists,. 
1677a 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 74 72 61   ** then the tra
1677b 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65  nsaction will be
1677c 20 64 6f 77 6e 67 72 61 64 65 64 20 74 6f 20 61   downgraded to a
1677d 20 72 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73   read-only trans
1677e 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 6e 73 74  action.  ** inst
1677f 65 61 64 20 6f 66 20 61 63 74 75 61 6c 6c 79 20  ead of actually 
16780 63 6f 6e 63 6c 75 64 65 64 2e 20 41 20 73 75 62  concluded. A sub
16781 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 20  sequent call to 
16782 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29  CommitPhaseTwo()
16783 20 0a 20 20 2a 2a 20 6f 72 20 52 6f 6c 6c 62 61   .  ** or Rollba
16784 63 6b 28 29 20 77 69 6c 6c 20 66 69 6e 69 73 68  ck() will finish
16785 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
16786 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20   and unlock the 
16787 64 61 74 61 62 61 73 65 2e 20 20 2a 2f 0a 20 20  database.  */.  
16788 66 6f 72 28 70 43 73 72 3d 70 42 74 2d 3e 70 43  for(pCsr=pBt->pC
16789 75 72 73 6f 72 3b 20 70 43 73 72 20 26 26 20 70  ursor; pCsr && p
1678a 43 73 72 2d 3e 70 42 74 72 65 65 21 3d 70 3b 20  Csr->pBtree!=p; 
1678b 70 43 73 72 3d 70 43 73 72 2d 3e 70 4e 65 78 74  pCsr=pCsr->pNext
1678c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 73  );.  assert( pCs
1678d 72 3d 3d 30 20 7c 7c 20 70 2d 3e 69 6e 54 72 61  r==0 || p->inTra
1678e 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b  ns>TRANS_NONE );
1678f 0a 0a 20 20 62 74 72 65 65 43 6c 65 61 72 48 61  ..  btreeClearHa
16790 73 43 6f 6e 74 65 6e 74 28 70 42 74 29 3b 0a 20  sContent(pBt);. 
16791 20 69 66 28 20 70 43 73 72 20 29 7b 0a 20 20 20   if( pCsr ){.   
16792 20 64 6f 77 6e 67 72 61 64 65 41 6c 6c 53 68 61   downgradeAllSha
16793 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
16794 6b 73 28 70 29 3b 0a 20 20 20 20 70 2d 3e 69 6e  ks(p);.    p->in
16795 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 52 45  Trans = TRANS_RE
16796 41 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  AD;.  }else{.   
16797 20 2f 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c   /* If the handl
16798 65 20 68 61 64 20 61 6e 79 20 6b 69 6e 64 20 6f  e had any kind o
16799 66 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70  f transaction op
1679a 65 6e 2c 20 64 65 63 72 65 6d 65 6e 74 20 74 68  en, decrement th
1679b 65 20 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61  e .    ** transa
1679c 63 74 69 6f 6e 20 63 6f 75 6e 74 20 6f 66 20 74  ction count of t
1679d 68 65 20 73 68 61 72 65 64 20 62 74 72 65 65 2e  he shared btree.
1679e 20 49 66 20 74 68 65 20 74 72 61 6e 73 61 63 74   If the transact
1679f 69 6f 6e 20 63 6f 75 6e 74 20 0a 20 20 20 20 2a  ion count .    *
167a0 2a 20 72 65 61 63 68 65 73 20 30 2c 20 73 65 74  * reaches 0, set
167a1 20 74 68 65 20 73 68 61 72 65 64 20 73 74 61 74   the shared stat
167a2 65 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 2e  e to TRANS_NONE.
167a3 20 54 68 65 20 75 6e 6c 6f 63 6b 42 74 72 65 65   The unlockBtree
167a4 49 66 55 6e 75 73 65 64 28 29 0a 20 20 20 20 2a  IfUnused().    *
167a5 2a 20 63 61 6c 6c 20 62 65 6c 6f 77 20 77 69 6c  * call below wil
167a6 6c 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67  l unlock the pag
167a7 65 72 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  er.  */.    if( 
167a8 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e  p->inTrans!=TRAN
167a9 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20  S_NONE ){.      
167aa 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61  clearAllSharedCa
167ab 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 29  cheTableLocks(p)
167ac 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72  ;.      pBt->nTr
167ad 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20  ansaction--;.   
167ae 20 20 20 69 66 28 20 30 3d 3d 70 42 74 2d 3e 6e     if( 0==pBt->n
167af 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20  Transaction ){. 
167b0 20 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72         pBt->inTr
167b1 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e  ansaction = TRAN
167b2 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a  S_NONE;.      }.
167b3 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 65      }..    /* Se
167b4 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72  t the current tr
167b5 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20  ansaction state 
167b6 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 20 61 6e  to TRANS_NONE an
167b7 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a 20 20  d unlock the .  
167b8 20 20 2a 2a 20 70 61 67 65 72 20 69 66 20 74 68    ** pager if th
167b9 69 73 20 63 61 6c 6c 20 63 6c 6f 73 65 64 20 74  is call closed t
167ba 68 65 20 6f 6e 6c 79 20 72 65 61 64 20 6f 72 20  he only read or 
167bb 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
167bc 6e 2e 20 20 2a 2f 0a 20 20 20 20 70 2d 3e 69 6e  n.  */.    p->in
167bd 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f  Trans = TRANS_NO
167be 4e 45 3b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74  NE;.    unlockBt
167bf 72 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29  reeIfUnused(pBt)
167c0 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 49 6e  ;.  }..  btreeIn
167c1 74 65 67 72 69 74 79 28 70 29 3b 0a 7d 0a 0a 2f  tegrity(p);.}../
167c2 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 74 68 65 20  *.** Commit the 
167c3 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 75 72 72  transaction curr
167c4 65 6e 74 6c 79 20 69 6e 20 70 72 6f 67 72 65 73  ently in progres
167c5 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  s..**.** This ro
167c6 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73  utine implements
167c7 20 74 68 65 20 73 65 63 6f 6e 64 20 70 68 61 73   the second phas
167c8 65 20 6f 66 20 61 20 32 2d 70 68 61 73 65 20 63  e of a 2-phase c
167c9 6f 6d 6d 69 74 2e 20 20 54 68 65 0a 2a 2a 20 73  ommit.  The.** s
167ca 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
167cb 74 50 68 61 73 65 4f 6e 65 28 29 20 72 6f 75 74  tPhaseOne() rout
167cc 69 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72  ine does the fir
167cd 73 74 20 70 68 61 73 65 20 61 6e 64 20 73 68 6f  st phase and sho
167ce 75 6c 64 0a 2a 2a 20 62 65 20 69 6e 76 6f 6b 65  uld.** be invoke
167cf 64 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69  d prior to calli
167d0 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  ng this routine.
167d1 20 20 54 68 65 20 73 71 6c 69 74 65 33 42 74 72    The sqlite3Btr
167d2 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  eeCommitPhaseOne
167d3 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 64 69  ().** routine di
167d4 64 20 61 6c 6c 20 74 68 65 20 77 6f 72 6b 20 6f  d all the work o
167d5 66 20 77 72 69 74 69 6e 67 20 69 6e 66 6f 72 6d  f writing inform
167d6 61 74 69 6f 6e 20 6f 75 74 20 74 6f 20 64 69 73  ation out to dis
167d7 6b 20 61 6e 64 20 66 6c 75 73 68 69 6e 67 20 74  k and flushing t
167d8 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 73  he.** contents s
167d9 6f 20 74 68 61 74 20 74 68 65 79 20 61 72 65 20  o that they are 
167da 77 72 69 74 74 65 6e 20 6f 6e 74 6f 20 74 68 65  written onto the
167db 20 64 69 73 6b 20 70 6c 61 74 74 65 72 2e 20 20   disk platter.  
167dc 41 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74  All this.** rout
167dd 69 6e 65 20 68 61 73 20 74 6f 20 64 6f 20 69 73  ine has to do is
167de 20 64 65 6c 65 74 65 20 6f 72 20 74 72 75 6e 63   delete or trunc
167df 61 74 65 20 6f 72 20 7a 65 72 6f 20 74 68 65 20  ate or zero the 
167e0 68 65 61 64 65 72 20 69 6e 20 74 68 65 0a 2a 2a  header in the.**
167e1 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
167e2 75 72 6e 61 6c 20 28 77 68 69 63 68 20 63 61 75  urnal (which cau
167e3 73 65 73 20 74 68 65 20 74 72 61 6e 73 61 63 74  ses the transact
167e4 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74 29 20 61  ion to commit) a
167e5 6e 64 0a 2a 2a 20 64 72 6f 70 20 6c 6f 63 6b 73  nd.** drop locks
167e6 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 69 6c  ..**.** This wil
167e7 6c 20 72 65 6c 65 61 73 65 20 74 68 65 20 77 72  l release the wr
167e8 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ite lock on the 
167e9 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
167ea 49 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20  If there.** are 
167eb 6e 6f 20 61 63 74 69 76 65 20 63 75 72 73 6f 72  no active cursor
167ec 73 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c 65 61  s, it also relea
167ed 73 65 73 20 74 68 65 20 72 65 61 64 20 6c 6f 63  ses the read loc
167ee 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  k..*/.SQLITE_PRI
167ef 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
167f0 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
167f1 54 77 6f 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  Two(Btree *p){. 
167f2 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
167f3 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69   p->pBt;..  sqli
167f4 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
167f5 3b 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69  ;.  btreeIntegri
167f6 74 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  ty(p);..  /* If 
167f7 74 68 65 20 68 61 6e 64 6c 65 20 68 61 73 20 61  the handle has a
167f8 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
167f9 6f 6e 20 6f 70 65 6e 2c 20 63 6f 6d 6d 69 74 20  on open, commit 
167fa 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65  the shared-btree
167fb 73 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74  s .  ** transact
167fc 69 6f 6e 20 61 6e 64 20 73 65 74 20 74 68 65 20  ion and set the 
167fd 73 68 61 72 65 64 20 73 74 61 74 65 20 74 6f 20  shared state to 
167fe 54 52 41 4e 53 5f 52 45 41 44 2e 0a 20 20 2a 2f  TRANS_READ..  */
167ff 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e  .  if( p->inTran
16800 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  s==TRANS_WRITE )
16801 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20  {.    int rc;.  
16802 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69    assert( pBt->i
16803 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52  nTransaction==TR
16804 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 20  ANS_WRITE );.   
16805 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e 54   assert( pBt->nT
16806 72 61 6e 73 61 63 74 69 6f 6e 3e 30 20 29 3b 0a  ransaction>0 );.
16807 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
16808 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65  PagerCommitPhase
16809 54 77 6f 28 70 42 74 2d 3e 70 50 61 67 65 72 29  Two(pBt->pPager)
1680a 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
1680b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1680c 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
1680d 76 65 28 70 29 3b 0a 20 20 20 20 20 20 72 65 74  ve(p);.      ret
1680e 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
1680f 20 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63    pBt->inTransac
16810 74 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45 41  tion = TRANS_REA
16811 44 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 45  D;.  }..  btreeE
16812 6e 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 29  ndTransaction(p)
16813 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
16814 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
16815 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
16816 0a 2f 2a 0a 2a 2a 20 44 6f 20 62 6f 74 68 20 70  ./*.** Do both p
16817 68 61 73 65 73 20 6f 66 20 61 20 63 6f 6d 6d 69  hases of a commi
16818 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
16819 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1681a 42 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 65  BtreeCommit(Btre
1681b 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b  e *p){.  int rc;
1681c 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
1681d 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20  nter(p);.  rc = 
1681e 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
1681f 69 74 50 68 61 73 65 4f 6e 65 28 70 2c 20 30 29  itPhaseOne(p, 0)
16820 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
16821 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20  TE_OK ){.    rc 
16822 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  = sqlite3BtreeCo
16823 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 29 3b  mmitPhaseTwo(p);
16824 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
16825 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
16826 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66  eturn rc;.}..#if
16827 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a  ndef NDEBUG./*.*
16828 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
16829 62 65 72 20 6f 66 20 77 72 69 74 65 2d 63 75 72  ber of write-cur
1682a 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69  sors open on thi
1682b 73 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20 69  s handle. This i
1682c 73 20 66 6f 72 20 75 73 65 0a 2a 2a 20 69 6e 20  s for use.** in 
1682d 61 73 73 65 72 74 28 29 20 65 78 70 72 65 73 73  assert() express
1682e 69 6f 6e 73 2c 20 73 6f 20 69 74 20 69 73 20 6f  ions, so it is o
1682f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 20  nly compiled if 
16830 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 0a 2a 2a  NDEBUG is not.**
16831 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20   defined..**.** 
16832 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73  For the purposes
16833 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
16834 2c 20 61 20 77 72 69 74 65 2d 63 75 72 73 6f 72  , a write-cursor
16835 20 69 73 20 61 6e 79 20 63 75 72 73 6f 72 20 74   is any cursor t
16836 68 61 74 0a 2a 2a 20 69 73 20 63 61 70 61 62 6c  hat.** is capabl
16837 65 20 6f 66 20 77 72 69 74 69 6e 67 20 74 6f 20  e of writing to 
16838 74 68 65 20 64 61 74 61 62 73 65 2e 20 20 54 68  the databse.  Th
16839 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 75 72  at means the cur
1683a 73 6f 72 20 77 61 73 0a 2a 2a 20 6f 72 69 67 69  sor was.** origi
1683b 6e 61 6c 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72  nally opened for
1683c 20 77 72 69 74 69 6e 67 20 61 6e 64 20 74 68 65   writing and the
1683d 20 63 75 72 73 6f 72 20 68 61 73 20 6e 6f 74 20   cursor has not 
1683e 62 65 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 62  be disabled.** b
1683f 79 20 68 61 76 69 6e 67 20 69 74 73 20 73 74 61  y having its sta
16840 74 65 20 63 68 61 6e 67 65 64 20 74 6f 20 43 55  te changed to CU
16841 52 53 4f 52 5f 46 41 55 4c 54 2e 0a 2a 2f 0a 73  RSOR_FAULT..*/.s
16842 74 61 74 69 63 20 69 6e 74 20 63 6f 75 6e 74 57  tatic int countW
16843 72 69 74 65 43 75 72 73 6f 72 73 28 42 74 53 68  riteCursors(BtSh
16844 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 74  ared *pBt){.  Bt
16845 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20  Cursor *pCur;.  
16846 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 66 6f 72  int r = 0;.  for
16847 28 70 43 75 72 3d 70 42 74 2d 3e 70 43 75 72 73  (pCur=pBt->pCurs
16848 6f 72 3b 20 70 43 75 72 3b 20 70 43 75 72 3d 70  or; pCur; pCur=p
16849 43 75 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Cur->pNext){.   
1684a 20 69 66 28 20 70 43 75 72 2d 3e 77 72 46 6c 61   if( pCur->wrFla
1684b 67 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74  g && pCur->eStat
1684c 65 21 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20  e!=CURSOR_FAULT 
1684d 29 20 72 2b 2b 3b 20 0a 20 20 7d 0a 20 20 72 65  ) r++; .  }.  re
1684e 74 75 72 6e 20 72 3b 0a 7d 0a 23 65 6e 64 69 66  turn r;.}.#endif
1684f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
16850 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 73 74  tine sets the st
16851 61 74 65 20 74 6f 20 43 55 52 53 4f 52 5f 46 41  ate to CURSOR_FA
16852 55 4c 54 20 61 6e 64 20 74 68 65 20 65 72 72 6f  ULT and the erro
16853 72 0a 2a 2a 20 63 6f 64 65 20 74 6f 20 65 72 72  r.** code to err
16854 43 6f 64 65 20 66 6f 72 20 65 76 65 72 79 20 63  Code for every c
16855 75 72 73 6f 72 20 6f 6e 20 42 74 53 68 61 72 65  ursor on BtShare
16856 64 20 74 68 61 74 20 70 42 74 72 65 65 0a 2a 2a  d that pBtree.**
16857 20 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a   references..**.
16858 2a 2a 20 45 76 65 72 79 20 63 75 72 73 6f 72 20  ** Every cursor 
16859 69 73 20 74 72 69 70 70 65 64 2c 20 69 6e 63 6c  is tripped, incl
1685a 75 64 69 6e 67 20 63 75 72 73 6f 72 73 20 74 68  uding cursors th
1685b 61 74 20 62 65 6c 6f 6e 67 0a 2a 2a 20 74 6f 20  at belong.** to 
1685c 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63  other database c
1685d 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 68 61 74 20  onnections that 
1685e 68 61 70 70 65 6e 20 74 6f 20 62 65 20 73 68 61  happen to be sha
1685f 72 69 6e 67 0a 2a 2a 20 74 68 65 20 63 61 63 68  ring.** the cach
16860 65 20 77 69 74 68 20 70 42 74 72 65 65 2e 0a 2a  e with pBtree..*
16861 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
16862 65 20 67 65 74 73 20 63 61 6c 6c 65 64 20 77 68  e gets called wh
16863 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 63  en a rollback oc
16864 63 75 72 73 2e 0a 2a 2a 20 41 6c 6c 20 63 75 72  curs..** All cur
16865 73 6f 72 73 20 75 73 69 6e 67 20 74 68 65 20 73  sors using the s
16866 61 6d 65 20 63 61 63 68 65 20 6d 75 73 74 20 62  ame cache must b
16867 65 20 74 72 69 70 70 65 64 0a 2a 2a 20 74 6f 20  e tripped.** to 
16868 70 72 65 76 65 6e 74 20 74 68 65 6d 20 66 72 6f  prevent them fro
16869 6d 20 74 72 79 69 6e 67 20 74 6f 20 75 73 65 20  m trying to use 
1686a 74 68 65 20 62 74 72 65 65 20 61 66 74 65 72 0a  the btree after.
1686b 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e  ** the rollback.
1686c 20 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d    The rollback m
1686d 61 79 20 68 61 76 65 20 64 65 6c 65 74 65 64 20  ay have deleted 
1686e 74 61 62 6c 65 73 0a 2a 2a 20 6f 72 20 6d 6f 76  tables.** or mov
1686f 65 64 20 72 6f 6f 74 20 70 61 67 65 73 2c 20 73  ed root pages, s
16870 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75 66 66  o it is not suff
16871 69 63 69 65 6e 74 20 74 6f 0a 2a 2a 20 73 61 76  icient to.** sav
16872 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74  e the state of t
16873 68 65 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20  he cursor.  The 
16874 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 0a 2a  cursor must be.*
16875 2a 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a  * invalidated..*
16876 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16877 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
16878 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73  eeTripAllCursors
16879 28 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20  (Btree *pBtree, 
1687a 69 6e 74 20 65 72 72 43 6f 64 65 29 7b 0a 20 20  int errCode){.  
1687b 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 73  BtCursor *p;.  s
1687c 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1687d 28 70 42 74 72 65 65 29 3b 0a 20 20 66 6f 72 28  (pBtree);.  for(
1687e 70 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70  p=pBtree->pBt->p
1687f 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e  Cursor; p; p=p->
16880 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74 20  pNext){.    int 
16881 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  i;.    sqlite3Bt
16882 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70  reeClearCursor(p
16883 29 3b 0a 20 20 20 20 70 2d 3e 65 53 74 61 74 65  );.    p->eState
16884 20 3d 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 3b   = CURSOR_FAULT;
16885 0a 20 20 20 20 70 2d 3e 73 6b 69 70 4e 65 78 74  .    p->skipNext
16886 20 3d 20 65 72 72 43 6f 64 65 3b 0a 20 20 20 20   = errCode;.    
16887 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 2d 3e 69  for(i=0; i<=p->i
16888 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Page; i++){.    
16889 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 2d    releasePage(p-
1688a 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20  >apPage[i]);.   
1688b 20 20 20 70 2d 3e 61 70 50 61 67 65 5b 69 5d 20     p->apPage[i] 
1688c 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 0;.    }.  }. 
1688d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
1688e 76 65 28 70 42 74 72 65 65 29 3b 0a 7d 0a 0a 2f  ve(pBtree);.}../
1688f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 74 68  *.** Rollback th
16890 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e  e transaction in
16891 20 70 72 6f 67 72 65 73 73 2e 20 20 41 6c 6c 20   progress.  All 
16892 63 75 72 73 6f 72 73 20 77 69 6c 6c 20 62 65 0a  cursors will be.
16893 2a 2a 20 69 6e 76 61 6c 69 64 65 64 20 62 79 20  ** invalided by 
16894 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 20  this operation. 
16895 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20   Any attempt to 
16896 75 73 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a 20  use a cursor.** 
16897 74 68 61 74 20 77 61 73 20 6f 70 65 6e 20 61 74  that was open at
16898 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
16899 66 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e  f this operation
1689a 20 77 69 6c 6c 20 72 65 73 75 6c 74 0a 2a 2a 20   will result.** 
1689b 69 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a  in an error..**.
1689c 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 65 6c  ** This will rel
1689d 65 61 73 65 20 74 68 65 20 77 72 69 74 65 20 6c  ease the write l
1689e 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
1689f 61 73 65 20 66 69 6c 65 2e 20 20 49 66 20 74 68  ase file.  If th
168a0 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63  ere.** are no ac
168a1 74 69 76 65 20 63 75 72 73 6f 72 73 2c 20 69 74  tive cursors, it
168a2 20 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 74   also releases t
168a3 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f  he read lock..*/
168a4 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
168a5 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
168a6 52 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 20 2a  Rollback(Btree *
168a7 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  p){.  int rc;.  
168a8 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
168a9 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67  p->pBt;.  MemPag
168aa 65 20 2a 70 50 61 67 65 31 3b 0a 0a 20 20 73 71  e *pPage1;..  sq
168ab 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
168ac 70 29 3b 0a 20 20 72 63 20 3d 20 73 61 76 65 41  p);.  rc = saveA
168ad 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 30  llCursors(pBt, 0
168ae 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  , 0);.#ifndef SQ
168af 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
168b0 5f 43 41 43 48 45 0a 20 20 69 66 28 20 72 63 21  _CACHE.  if( rc!
168b1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
168b2 20 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 68    /* This is a h
168b3 6f 72 72 69 62 6c 65 20 73 69 74 75 61 74 69 6f  orrible situatio
168b4 6e 2e 20 41 6e 20 49 4f 20 6f 72 20 6d 61 6c 6c  n. An IO or mall
168b5 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63 75 72  oc() error occur
168b6 72 65 64 20 77 68 69 6c 73 74 0a 20 20 20 20 2a  red whilst.    *
168b7 2a 20 74 72 79 69 6e 67 20 74 6f 20 73 61 76 65  * trying to save
168b8 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e   cursor position
168b9 73 2e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  s. If this is an
168ba 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c 6c 62   automatic rollb
168bb 61 63 6b 20 28 61 73 0a 20 20 20 20 2a 2a 20 74  ack (as.    ** t
168bc 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 63  he result of a c
168bd 6f 6e 73 74 72 61 69 6e 74 2c 20 6d 61 6c 6c 6f  onstraint, mallo
168be 63 28 29 20 66 61 69 6c 75 72 65 20 6f 72 20 49  c() failure or I
168bf 4f 20 65 72 72 6f 72 29 20 74 68 65 6e 20 0a 20  O error) then . 
168c0 20 20 20 2a 2a 20 74 68 65 20 63 61 63 68 65 20     ** the cache 
168c1 6d 61 79 20 62 65 20 69 6e 74 65 72 6e 61 6c 6c  may be internall
168c2 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 28  y inconsistent (
168c3 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 76 61 6c 69  not contain vali
168c4 64 20 74 72 65 65 73 29 20 73 6f 0a 20 20 20 20  d trees) so.    
168c5 2a 2a 20 77 65 20 63 61 6e 6e 6f 74 20 73 69 6d  ** we cannot sim
168c6 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65 20 65  ply return the e
168c7 72 72 6f 72 20 74 6f 20 74 68 65 20 63 61 6c 6c  rror to the call
168c8 65 72 2e 20 49 6e 73 74 65 61 64 2c 20 61 62 6f  er. Instead, abo
168c9 72 74 20 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 71  rt .    ** all q
168ca 75 65 72 69 65 73 20 74 68 61 74 20 6d 61 79 20  ueries that may 
168cb 62 65 20 75 73 69 6e 67 20 61 6e 79 20 6f 66 20  be using any of 
168cc 74 68 65 20 63 75 72 73 6f 72 73 20 74 68 61 74  the cursors that
168cd 20 66 61 69 6c 65 64 20 74 6f 20 73 61 76 65 2e   failed to save.
168ce 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
168cf 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43  te3BtreeTripAllC
168d0 75 72 73 6f 72 73 28 70 2c 20 72 63 29 3b 0a 20  ursors(p, rc);. 
168d1 20 7d 0a 23 65 6e 64 69 66 0a 20 20 62 74 72 65   }.#endif.  btre
168d2 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 0a  eIntegrity(p);..
168d3 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73    if( p->inTrans
168d4 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b  ==TRANS_WRITE ){
168d5 0a 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 0a 20  .    int rc2;.. 
168d6 20 20 20 61 73 73 65 72 74 28 20 54 52 41 4e 53     assert( TRANS
168d7 5f 57 52 49 54 45 3d 3d 70 42 74 2d 3e 69 6e 54  _WRITE==pBt->inT
168d8 72 61 6e 73 61 63 74 69 6f 6e 20 29 3b 0a 20 20  ransaction );.  
168d9 20 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 50    rc2 = sqlite3P
168da 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 42 74  agerRollback(pBt
168db 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69  ->pPager);.    i
168dc 66 28 20 72 63 32 21 3d 53 51 4c 49 54 45 5f 4f  f( rc2!=SQLITE_O
168dd 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
168de 72 63 32 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  rc2;.    }..    
168df 2f 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20  /* The rollback 
168e0 6d 61 79 20 68 61 76 65 20 64 65 73 74 72 6f 79  may have destroy
168e1 65 64 20 74 68 65 20 70 50 61 67 65 31 2d 3e 61  ed the pPage1->a
168e2 44 61 74 61 20 76 61 6c 75 65 2e 20 20 53 6f 0a  Data value.  So.
168e3 20 20 20 20 2a 2a 20 63 61 6c 6c 20 62 74 72 65      ** call btre
168e4 65 47 65 74 50 61 67 65 28 29 20 6f 6e 20 70 61  eGetPage() on pa
168e5 67 65 20 31 20 61 67 61 69 6e 20 74 6f 20 6d 61  ge 1 again to ma
168e6 6b 65 0a 20 20 20 20 2a 2a 20 73 75 72 65 20 70  ke.    ** sure p
168e7 50 61 67 65 31 2d 3e 61 44 61 74 61 20 69 73 20  Page1->aData is 
168e8 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 2a  set correctly. *
168e9 2f 0a 20 20 20 20 69 66 28 20 62 74 72 65 65 47  /.    if( btreeG
168ea 65 74 50 61 67 65 28 70 42 74 2c 20 31 2c 20 26  etPage(pBt, 1, &
168eb 70 50 61 67 65 31 2c 20 30 29 3d 3d 53 51 4c 49  pPage1, 0)==SQLI
168ec 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
168ed 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65  eleasePage(pPage
168ee 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  1);.    }.    as
168ef 73 65 72 74 28 20 63 6f 75 6e 74 57 72 69 74 65  sert( countWrite
168f0 43 75 72 73 6f 72 73 28 70 42 74 29 3d 3d 30 20  Cursors(pBt)==0 
168f1 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72  );.    pBt->inTr
168f2 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e  ansaction = TRAN
168f3 53 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 62  S_READ;.  }..  b
168f4 74 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74 69  treeEndTransacti
168f5 6f 6e 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  on(p);.  sqlite3
168f6 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
168f7 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
168f8 2a 0a 2a 2a 20 53 74 61 72 74 20 61 20 73 74 61  *.** Start a sta
168f9 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61  tement subtransa
168fa 63 74 69 6f 6e 2e 20 54 68 65 20 73 75 62 74 72  ction. The subtr
168fb 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 20 63 61  ansaction can ca
168fc 6e 20 62 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62  n be rolled.** b
168fd 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c  ack independentl
168fe 79 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 74 72  y of the main tr
168ff 61 6e 73 61 63 74 69 6f 6e 2e 20 59 6f 75 20 6d  ansaction. You m
16900 75 73 74 20 73 74 61 72 74 20 61 20 74 72 61 6e  ust start a tran
16901 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 62 65 66 6f  saction .** befo
16902 72 65 20 73 74 61 72 74 69 6e 67 20 61 20 73 75  re starting a su
16903 62 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68  btransaction. Th
16904 65 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e  e subtransaction
16905 20 69 73 20 65 6e 64 65 64 20 61 75 74 6f 6d 61   is ended automa
16906 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 69 66 20 74  tically .** if t
16907 68 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74  he main transact
16908 69 6f 6e 20 63 6f 6d 6d 69 74 73 20 6f 72 20 72  ion commits or r
16909 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a  olls back..**.**
1690a 20 53 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72   Statement subtr
1690b 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 75  ansactions are u
1690c 73 65 64 20 61 72 6f 75 6e 64 20 69 6e 64 69 76  sed around indiv
1690d 69 64 75 61 6c 20 53 51 4c 20 73 74 61 74 65 6d  idual SQL statem
1690e 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65  ents.** that are
1690f 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69   contained withi
16910 6e 20 61 20 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d  n a BEGIN...COMM
16911 49 54 20 62 6c 6f 63 6b 2e 20 20 49 66 20 61 20  IT block.  If a 
16912 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72  constraint.** er
16913 72 6f 72 20 6f 63 63 75 72 73 20 77 69 74 68 69  ror occurs withi
16914 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c  n the statement,
16915 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 74   the effect of t
16916 68 61 74 20 6f 6e 65 20 73 74 61 74 65 6d 65 6e  hat one statemen
16917 74 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c  t.** can be roll
16918 65 64 20 62 61 63 6b 20 77 69 74 68 6f 75 74 20  ed back without 
16919 68 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61  having to rollba
1691a 63 6b 20 74 68 65 20 65 6e 74 69 72 65 20 74 72  ck the entire tr
1691b 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  ansaction..**.**
1691c 20 41 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62   A statement sub
1691d 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20  -transaction is 
1691e 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61  implemented as a
1691f 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65  n anonymous save
16920 70 6f 69 6e 74 2e 20 54 68 65 0a 2a 2a 20 76 61  point. The.** va
16921 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68  lue passed as th
16922 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
16923 65 72 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20  er is the total 
16924 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f  number of savepo
16925 69 6e 74 73 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69  ints,.** includi
16926 6e 67 20 74 68 65 20 6e 65 77 20 61 6e 6f 6e 79  ng the new anony
16927 6d 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 2c 20  mous savepoint, 
16928 6f 70 65 6e 20 6f 6e 20 74 68 65 20 42 2d 54 72  open on the B-Tr
16929 65 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 65 72  ee. i.e. if ther
1692a 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69  e.** are no acti
1692b 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 6e  ve savepoints an
1692c 64 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 65  d no other state
1692d 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e  ment-transaction
1692e 73 20 6f 70 65 6e 2c 0a 2a 2a 20 69 53 74 61 74  s open,.** iStat
1692f 65 6d 65 6e 74 20 69 73 20 31 2e 20 54 68 69 73  ement is 1. This
16930 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70   anonymous savep
16931 6f 69 6e 74 20 63 61 6e 20 62 65 20 72 65 6c 65  oint can be rele
16932 61 73 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62  ased or rolled b
16933 61 63 6b 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65  ack.** using the
16934 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76   sqlite3BtreeSav
16935 65 70 6f 69 6e 74 28 29 20 66 75 6e 63 74 69 6f  epoint() functio
16936 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
16937 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
16938 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 42  BtreeBeginStmt(B
16939 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 53 74  tree *p, int iSt
1693a 61 74 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20  atement){.  int 
1693b 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  rc;.  BtShared *
1693c 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
1693d 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1693e 72 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r(p);.  assert( 
1693f 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
16940 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73  S_WRITE );.  ass
16941 65 72 74 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e  ert( pBt->readOn
16942 6c 79 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ly==0 );.  asser
16943 74 28 20 69 53 74 61 74 65 6d 65 6e 74 3e 30 20  t( iStatement>0 
16944 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 74  );.  assert( iSt
16945 61 74 65 6d 65 6e 74 3e 70 2d 3e 64 62 2d 3e 6e  atement>p->db->n
16946 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 20 69  Savepoint );.  i
16947 66 28 20 4e 45 56 45 52 28 70 2d 3e 69 6e 54 72  f( NEVER(p->inTr
16948 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans!=TRANS_WRITE
16949 20 7c 7c 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c   || pBt->readOnl
1694a 79 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  y) ){.    rc = S
1694b 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 3b 0a  QLITE_INTERNAL;.
1694c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
1694d 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e  ert( pBt->inTran
1694e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57  saction==TRANS_W
1694f 52 49 54 45 20 29 3b 0a 20 20 20 20 2f 2a 20 41  RITE );.    /* A
16950 74 20 74 68 65 20 70 61 67 65 72 20 6c 65 76 65  t the pager leve
16951 6c 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74  l, a statement t
16952 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 20  ransaction is a 
16953 73 61 76 65 70 6f 69 6e 74 20 77 69 74 68 0a 20  savepoint with. 
16954 20 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78 20 67     ** an index g
16955 72 65 61 74 65 72 20 74 68 61 6e 20 61 6c 6c 20  reater than all 
16956 73 61 76 65 70 6f 69 6e 74 73 20 63 72 65 61 74  savepoints creat
16957 65 64 20 65 78 70 6c 69 63 69 74 6c 79 20 75 73  ed explicitly us
16958 69 6e 67 0a 20 20 20 20 2a 2a 20 53 51 4c 20 73  ing.    ** SQL s
16959 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 73  tatements. It is
1695a 20 69 6c 6c 65 67 61 6c 20 74 6f 20 6f 70 65 6e   illegal to open
1695b 2c 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c  , release or rol
1695c 6c 62 61 63 6b 20 61 6e 79 0a 20 20 20 20 2a 2a  lback any.    **
1695d 20 73 75 63 68 20 73 61 76 65 70 6f 69 6e 74 73   such savepoints
1695e 20 77 68 69 6c 65 20 74 68 65 20 73 74 61 74 65   while the state
1695f 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
16960 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 61 63   savepoint is ac
16961 74 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tive..    */.   
16962 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
16963 65 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28  erOpenSavepoint(
16964 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 53 74  pBt->pPager, iSt
16965 61 74 65 6d 65 6e 74 29 3b 0a 20 20 7d 0a 20 20  atement);.  }.  
16966 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
16967 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  e(p);.  return r
16968 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  c;.}../*.** The 
16969 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
1696a 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
1696b 2c 20 6f 70 2c 20 69 73 20 61 6c 77 61 79 73 20  , op, is always 
1696c 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
1696d 43 4b 0a 2a 2a 20 6f 72 20 53 41 56 45 50 4f 49  CK.** or SAVEPOI
1696e 4e 54 5f 52 45 4c 45 41 53 45 2e 20 54 68 69 73  NT_RELEASE. This
1696f 20 66 75 6e 63 74 69 6f 6e 20 65 69 74 68 65 72   function either
16970 20 72 65 6c 65 61 73 65 73 20 6f 72 20 72 6f 6c   releases or rol
16971 6c 73 20 62 61 63 6b 20 74 68 65 0a 2a 2a 20 73  ls back the.** s
16972 61 76 65 70 6f 69 6e 74 20 69 64 65 6e 74 69 66  avepoint identif
16973 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72  ied by parameter
16974 20 69 53 61 76 65 70 6f 69 6e 74 2c 20 64 65 70   iSavepoint, dep
16975 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 76 61  ending on the va
16976 6c 75 65 20 0a 2a 2a 20 6f 66 20 6f 70 2e 0a 2a  lue .** of op..*
16977 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69  *.** Normally, i
16978 53 61 76 65 70 6f 69 6e 74 20 69 73 20 67 72 65  Savepoint is gre
16979 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  ater than or equ
1697a 61 6c 20 74 6f 20 7a 65 72 6f 2e 20 48 6f 77 65  al to zero. Howe
1697b 76 65 72 2c 20 69 66 20 6f 70 20 69 73 0a 2a 2a  ver, if op is.**
1697c 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42   SAVEPOINT_ROLLB
1697d 41 43 4b 2c 20 74 68 65 6e 20 69 53 61 76 65 70  ACK, then iSavep
1697e 6f 69 6e 74 20 6d 61 79 20 61 6c 73 6f 20 62 65  oint may also be
1697f 20 2d 31 2e 20 49 6e 20 74 68 69 73 20 63 61 73   -1. In this cas
16980 65 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e  e the .** conten
16981 74 73 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65  ts of the entire
16982 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 72 65   transaction are
16983 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68   rolled back. Th
16984 69 73 20 69 73 20 64 69 66 66 65 72 65 6e 74 0a  is is different.
16985 2a 2a 20 66 72 6f 6d 20 61 20 6e 6f 72 6d 61 6c  ** from a normal
16986 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c   transaction rol
16987 6c 62 61 63 6b 2c 20 61 73 20 6e 6f 20 6c 6f 63  lback, as no loc
16988 6b 73 20 61 72 65 20 72 65 6c 65 61 73 65 64 20  ks are released 
16989 61 6e 64 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73  and the.** trans
1698a 61 63 74 69 6f 6e 20 72 65 6d 61 69 6e 73 20 6f  action remains o
1698b 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  pen..*/.SQLITE_P
1698c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1698d 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74  e3BtreeSavepoint
1698e 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6f  (Btree *p, int o
1698f 70 2c 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e  p, int iSavepoin
16990 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  t){.  int rc = S
16991 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
16992 70 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d  p && p->inTrans=
16993 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a  =TRANS_WRITE ){.
16994 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42      BtShared *pB
16995 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 20 20  t = p->pBt;.    
16996 61 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 56 45  assert( op==SAVE
16997 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 7c 7c  POINT_RELEASE ||
16998 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52   op==SAVEPOINT_R
16999 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 20 20 61  OLLBACK );.    a
1699a 73 73 65 72 74 28 20 69 53 61 76 65 70 6f 69 6e  ssert( iSavepoin
1699b 74 3e 3d 30 20 7c 7c 20 28 69 53 61 76 65 70 6f  t>=0 || (iSavepo
1699c 69 6e 74 3d 3d 2d 31 20 26 26 20 6f 70 3d 3d 53  int==-1 && op==S
1699d 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
1699e 4b 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  K) );.    sqlite
1699f 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
169a0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
169a1 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28 70  PagerSavepoint(p
169a2 42 74 2d 3e 70 50 61 67 65 72 2c 20 6f 70 2c 20  Bt->pPager, op, 
169a3 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20  iSavepoint);.   
169a4 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
169a5 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
169a6 20 6e 65 77 44 61 74 61 62 61 73 65 28 70 42 74   newDatabase(pBt
169a7 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
169a8 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
169a9 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
169aa 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  rc;.}../*.** Cre
169ab 61 74 65 20 61 20 6e 65 77 20 63 75 72 73 6f 72  ate a new cursor
169ac 20 66 6f 72 20 74 68 65 20 42 54 72 65 65 20 77   for the BTree w
169ad 68 6f 73 65 20 72 6f 6f 74 20 69 73 20 6f 6e 20  hose root is on 
169ae 74 68 65 20 70 61 67 65 0a 2a 2a 20 69 54 61 62  the page.** iTab
169af 6c 65 2e 20 49 66 20 61 20 72 65 61 64 2d 6f 6e  le. If a read-on
169b0 6c 79 20 63 75 72 73 6f 72 20 69 73 20 72 65 71  ly cursor is req
169b1 75 65 73 74 65 64 2c 20 69 74 20 69 73 20 61 73  uested, it is as
169b2 73 75 6d 65 64 20 74 68 61 74 0a 2a 2a 20 74 68  sumed that.** th
169b3 65 20 63 61 6c 6c 65 72 20 61 6c 72 65 61 64 79  e caller already
169b4 20 68 61 73 20 61 74 20 6c 65 61 73 74 20 61 20   has at least a 
169b5 72 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61  read-only transa
169b6 63 74 69 6f 6e 20 6f 70 65 6e 0a 2a 2a 20 6f 6e  ction open.** on
169b7 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6c   the database al
169b8 72 65 61 64 79 2e 20 49 66 20 61 20 77 72 69 74  ready. If a writ
169b9 65 2d 63 75 72 73 6f 72 20 69 73 20 72 65 71 75  e-cursor is requ
169ba 65 73 74 65 64 2c 20 74 68 65 6e 0a 2a 2a 20 74  ested, then.** t
169bb 68 65 20 63 61 6c 6c 65 72 20 69 73 20 61 73 73  he caller is ass
169bc 75 6d 65 64 20 74 6f 20 68 61 76 65 20 61 6e 20  umed to have an 
169bd 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73  open write trans
169be 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  action..**.** If
169bf 20 77 72 46 6c 61 67 3d 3d 30 2c 20 74 68 65 6e   wrFlag==0, then
169c0 20 74 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20   the cursor can 
169c1 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 66 6f 72  only be used for
169c2 20 72 65 61 64 69 6e 67 2e 0a 2a 2a 20 49 66 20   reading..** If 
169c3 77 72 46 6c 61 67 3d 3d 31 2c 20 74 68 65 6e 20  wrFlag==1, then 
169c4 74 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 62  the cursor can b
169c5 65 20 75 73 65 64 20 66 6f 72 20 72 65 61 64 69  e used for readi
169c6 6e 67 20 6f 72 20 66 6f 72 0a 2a 2a 20 77 72 69  ng or for.** wri
169c7 74 69 6e 67 20 69 66 20 6f 74 68 65 72 20 63 6f  ting if other co
169c8 6e 64 69 74 69 6f 6e 73 20 66 6f 72 20 77 72 69  nditions for wri
169c9 74 69 6e 67 20 61 72 65 20 61 6c 73 6f 20 6d 65  ting are also me
169ca 74 2e 20 20 54 68 65 73 65 0a 2a 2a 20 61 72 65  t.  These.** are
169cb 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20   the conditions 
169cc 74 68 61 74 20 6d 75 73 74 20 62 65 20 6d 65 74  that must be met
169cd 20 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 77 72   in order for wr
169ce 69 74 69 6e 67 20 74 6f 0a 2a 2a 20 62 65 20 61  iting to.** be a
169cf 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 31 3a  llowed:.**.** 1:
169d0 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73    The cursor mus
169d1 74 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e  t have been open
169d2 65 64 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d  ed with wrFlag==
169d3 31 0a 2a 2a 0a 2a 2a 20 32 3a 20 20 4f 74 68 65  1.**.** 2:  Othe
169d4 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  r database conne
169d5 63 74 69 6f 6e 73 20 74 68 61 74 20 73 68 61 72  ctions that shar
169d6 65 20 74 68 65 20 73 61 6d 65 20 70 61 67 65 72  e the same pager
169d7 20 63 61 63 68 65 0a 2a 2a 20 20 20 20 20 62 75   cache.**     bu
169d8 74 20 77 68 69 63 68 20 61 72 65 20 6e 6f 74 20  t which are not 
169d9 69 6e 20 74 68 65 20 52 45 41 44 5f 55 4e 43 4f  in the READ_UNCO
169da 4d 4d 49 54 54 45 44 20 73 74 61 74 65 20 6d 61  MMITTED state ma
169db 79 20 6e 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20  y not have.**   
169dc 20 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 77    cursors open w
169dd 69 74 68 20 77 72 46 6c 61 67 3d 3d 30 20 6f 6e  ith wrFlag==0 on
169de 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2e   the same table.
169df 20 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 20    Otherwise.**  
169e0 20 20 20 74 68 65 20 63 68 61 6e 67 65 73 20 6d     the changes m
169e1 61 64 65 20 62 79 20 74 68 69 73 20 77 72 69 74  ade by this writ
169e2 65 20 63 75 72 73 6f 72 20 77 6f 75 6c 64 20 62  e cursor would b
169e3 65 20 76 69 73 69 62 6c 65 20 74 6f 0a 2a 2a 20  e visible to.** 
169e4 20 20 20 20 74 68 65 20 72 65 61 64 20 63 75 72      the read cur
169e5 73 6f 72 73 20 69 6e 20 74 68 65 20 6f 74 68 65  sors in the othe
169e6 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  r database conne
169e7 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 33 3a 20  ction..**.** 3: 
169e8 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6d 75   The database mu
169e9 73 74 20 62 65 20 77 72 69 74 61 62 6c 65 20 28  st be writable (
169ea 6e 6f 74 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79  not on read-only
169eb 20 6d 65 64 69 61 29 0a 2a 2a 0a 2a 2a 20 34 3a   media).**.** 4:
169ec 20 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20    There must be 
169ed 61 6e 20 61 63 74 69 76 65 20 74 72 61 6e 73 61  an active transa
169ee 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20  ction..**.** No 
169ef 63 68 65 63 6b 69 6e 67 20 69 73 20 64 6f 6e 65  checking is done
169f0 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
169f1 61 74 20 70 61 67 65 20 69 54 61 62 6c 65 20 72  at page iTable r
169f2 65 61 6c 6c 79 20 69 73 20 74 68 65 0a 2a 2a 20  eally is the.** 
169f3 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 20 62  root page of a b
169f4 2d 74 72 65 65 2e 20 20 49 66 20 69 74 20 69 73  -tree.  If it is
169f5 20 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 20 63   not, then the c
169f6 75 72 73 6f 72 20 61 63 71 75 69 72 65 64 0a 2a  ursor acquired.*
169f7 2a 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20  * will not work 
169f8 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a  correctly..**.**
169f9 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74   It is assumed t
169fa 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 42  hat the sqlite3B
169fb 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 28 29  treeCursorSize()
169fc 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79   bytes of memory
169fd 20 0a 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20   .** pointed to 
169fe 62 79 20 70 43 75 72 20 68 61 76 65 20 62 65 65  by pCur have bee
169ff 6e 20 7a 65 72 6f 65 64 20 62 79 20 74 68 65 20  n zeroed by the 
16a00 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  caller..*/.stati
16a01 63 20 69 6e 74 20 62 74 72 65 65 43 75 72 73 6f  c int btreeCurso
16a02 72 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20  r(.  Btree *p,  
16a03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a04 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
16a05 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 69 6e  he btree */.  in
16a06 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20  t iTable,       
16a07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a08 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67       /* Root pag
16a09 65 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 6f 70  e of table to op
16a0a 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c  en */.  int wrFl
16a0b 61 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ag,             
16a0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
16a0d 2a 20 31 20 74 6f 20 77 72 69 74 65 2e 20 30 20  * 1 to write. 0 
16a0e 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73  read-only */.  s
16a0f 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70  truct KeyInfo *p
16a10 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 20 20  KeyInfo,        
16a11 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61        /* First a
16a12 72 67 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f 6e  rg to comparison
16a13 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 42   function */.  B
16a14 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 20 20  tCursor *pCur   
16a15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a16 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66        /* Space f
16a17 6f 72 20 6e 65 77 20 63 75 72 73 6f 72 20 2a 2f  or new cursor */
16a18 0a 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  .){.  BtShared *
16a19 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 20 20 20  pBt = p->pBt;   
16a1a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
16a1b 53 68 61 72 65 64 20 62 2d 74 72 65 65 20 68 61  Shared b-tree ha
16a1c 6e 64 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72  ndle */..  asser
16a1d 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
16a1e 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a  oldsMutex(p) );.
16a1f 20 20 61 73 73 65 72 74 28 20 77 72 46 6c 61 67    assert( wrFlag
16a20 3d 3d 30 20 7c 7c 20 77 72 46 6c 61 67 3d 3d 31  ==0 || wrFlag==1
16a21 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f   );..  /* The fo
16a22 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74 20 73  llowing assert s
16a23 74 61 74 65 6d 65 6e 74 73 20 76 65 72 69 66 79  tatements verify
16a24 20 74 68 61 74 20 69 66 20 74 68 69 73 20 69 73   that if this is
16a25 20 61 20 73 68 61 72 61 62 6c 65 20 0a 20 20 2a   a sharable .  *
16a26 2a 20 62 2d 74 72 65 65 20 64 61 74 61 62 61 73  * b-tree databas
16a27 65 2c 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f  e, the connectio
16a28 6e 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 68 65  n is holding the
16a29 20 72 65 71 75 69 72 65 64 20 74 61 62 6c 65 20   required table 
16a2a 6c 6f 63 6b 73 2c 20 0a 20 20 2a 2a 20 61 6e 64  locks, .  ** and
16a2b 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 63   that no other c
16a2c 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20 61 6e  onnection has an
16a2d 79 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 74 68  y open cursor th
16a2e 61 74 20 63 6f 6e 66 6c 69 63 74 73 20 77 69 74  at conflicts wit
16a2f 68 20 0a 20 20 2a 2a 20 74 68 69 73 20 6c 6f 63  h .  ** this loc
16a30 6b 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  k.  */.  assert(
16a31 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65 54   hasSharedCacheT
16a32 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62  ableLock(p, iTab
16a33 6c 65 2c 20 70 4b 65 79 49 6e 66 6f 21 3d 30 2c  le, pKeyInfo!=0,
16a34 20 77 72 46 6c 61 67 2b 31 29 20 29 3b 0a 20 20   wrFlag+1) );.  
16a35 61 73 73 65 72 74 28 20 77 72 46 6c 61 67 3d 3d  assert( wrFlag==
16a36 30 20 7c 7c 20 21 68 61 73 52 65 61 64 43 6f 6e  0 || !hasReadCon
16a37 66 6c 69 63 74 73 28 70 2c 20 69 54 61 62 6c 65  flicts(p, iTable
16a38 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72  ) );..  /* Asser
16a39 74 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65  t that the calle
16a3a 72 20 68 61 73 20 6f 70 65 6e 65 64 20 74 68 65  r has opened the
16a3b 20 72 65 71 75 69 72 65 64 20 74 72 61 6e 73 61   required transa
16a3c 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 61 73 73 65  ction. */.  asse
16a3d 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 54  rt( p->inTrans>T
16a3e 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 61  RANS_NONE );.  a
16a3f 73 73 65 72 74 28 20 77 72 46 6c 61 67 3d 3d 30  ssert( wrFlag==0
16a40 20 7c 7c 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d   || p->inTrans==
16a41 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20  TRANS_WRITE );. 
16a42 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50   assert( pBt->pP
16a43 61 67 65 31 20 26 26 20 70 42 74 2d 3e 70 50 61  age1 && pBt->pPa
16a44 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a 0a 20  ge1->aData );.. 
16a45 20 69 66 28 20 4e 45 56 45 52 28 77 72 46 6c 61   if( NEVER(wrFla
16a46 67 20 26 26 20 70 42 74 2d 3e 72 65 61 64 4f 6e  g && pBt->readOn
16a47 6c 79 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ly) ){.    retur
16a48 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c  n SQLITE_READONL
16a49 59 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 54 61  Y;.  }.  if( iTa
16a4a 62 6c 65 3d 3d 31 20 26 26 20 70 61 67 65 72 50  ble==1 && pagerP
16a4b 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3d 3d 30  agecount(pBt)==0
16a4c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
16a4d 51 4c 49 54 45 5f 45 4d 50 54 59 3b 0a 20 20 7d  QLITE_EMPTY;.  }
16a4e 0a 0a 20 20 2f 2a 20 4e 6f 77 20 74 68 61 74 20  ..  /* Now that 
16a4f 6e 6f 20 6f 74 68 65 72 20 65 72 72 6f 72 73 20  no other errors 
16a50 63 61 6e 20 6f 63 63 75 72 2c 20 66 69 6e 69 73  can occur, finis
16a51 68 20 66 69 6c 6c 69 6e 67 20 69 6e 20 74 68 65  h filling in the
16a52 20 42 74 43 75 72 73 6f 72 0a 20 20 2a 2a 20 76   BtCursor.  ** v
16a53 61 72 69 61 62 6c 65 73 20 61 6e 64 20 6c 69 6e  ariables and lin
16a54 6b 20 74 68 65 20 63 75 72 73 6f 72 20 69 6e 74  k the cursor int
16a55 6f 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6c  o the BtShared l
16a56 69 73 74 2e 20 20 2a 2f 0a 20 20 70 43 75 72 2d  ist.  */.  pCur-
16a57 3e 70 67 6e 6f 52 6f 6f 74 20 3d 20 28 50 67 6e  >pgnoRoot = (Pgn
16a58 6f 29 69 54 61 62 6c 65 3b 0a 20 20 70 43 75 72  o)iTable;.  pCur
16a59 2d 3e 69 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20  ->iPage = -1;.  
16a5a 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d  pCur->pKeyInfo =
16a5b 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 70 43 75   pKeyInfo;.  pCu
16a5c 72 2d 3e 70 42 74 72 65 65 20 3d 20 70 3b 0a 20  r->pBtree = p;. 
16a5d 20 70 43 75 72 2d 3e 70 42 74 20 3d 20 70 42 74   pCur->pBt = pBt
16a5e 3b 0a 20 20 70 43 75 72 2d 3e 77 72 46 6c 61 67  ;.  pCur->wrFlag
16a5f 20 3d 20 28 75 38 29 77 72 46 6c 61 67 3b 0a 20   = (u8)wrFlag;. 
16a60 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 3d 20 70   pCur->pNext = p
16a61 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69  Bt->pCursor;.  i
16a62 66 28 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 29  f( pCur->pNext )
16a63 7b 0a 20 20 20 20 70 43 75 72 2d 3e 70 4e 65 78  {.    pCur->pNex
16a64 74 2d 3e 70 50 72 65 76 20 3d 20 70 43 75 72 3b  t->pPrev = pCur;
16a65 0a 20 20 7d 0a 20 20 70 42 74 2d 3e 70 43 75 72  .  }.  pBt->pCur
16a66 73 6f 72 20 3d 20 70 43 75 72 3b 0a 20 20 70 43  sor = pCur;.  pC
16a67 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52  ur->eState = CUR
16a68 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 70  SOR_INVALID;.  p
16a69 43 75 72 2d 3e 63 61 63 68 65 64 52 6f 77 69 64  Cur->cachedRowid
16a6a 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 53   = 0;.  return S
16a6b 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49  QLITE_OK;.}.SQLI
16a6c 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
16a6d 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f  qlite3BtreeCurso
16a6e 72 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20  r(.  Btree *p,  
16a6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a71 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f   /* The btree */
16a72 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20  .  int iTable,  
16a73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
16a75 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74  * Root page of t
16a76 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a  able to open */.
16a77 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20    int wrFlag,   
16a78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16a7a 20 31 20 74 6f 20 77 72 69 74 65 2e 20 30 20 72   1 to write. 0 r
16a7b 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74  ead-only */.  st
16a7c 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  ruct KeyInfo *pK
16a7d 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 20 20 20  eyInfo,         
16a7e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
16a7f 73 74 20 61 72 67 20 74 6f 20 78 43 6f 6d 70 61  st arg to xCompa
16a80 72 65 28 29 20 2a 2f 0a 20 20 42 74 43 75 72 73  re() */.  BtCurs
16a81 6f 72 20 2a 70 43 75 72 20 20 20 20 20 20 20 20  or *pCur        
16a82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a83 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 6e        /* Write n
16a84 65 77 20 63 75 72 73 6f 72 20 68 65 72 65 20 2a  ew cursor here *
16a85 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
16a86 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
16a87 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 62 74  er(p);.  rc = bt
16a88 72 65 65 43 75 72 73 6f 72 28 70 2c 20 69 54 61  reeCursor(p, iTa
16a89 62 6c 65 2c 20 77 72 46 6c 61 67 2c 20 70 4b 65  ble, wrFlag, pKe
16a8a 79 49 6e 66 6f 2c 20 70 43 75 72 29 3b 0a 20 20  yInfo, pCur);.  
16a8b 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
16a8c 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  e(p);.  return r
16a8d 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
16a8e 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61  rn the size of a
16a8f 20 42 74 43 75 72 73 6f 72 20 6f 62 6a 65 63 74   BtCursor object
16a90 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a   in bytes..**.**
16a91 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 73   This interfaces
16a92 20 69 73 20 6e 65 65 64 65 64 20 73 6f 20 74 68   is needed so th
16a93 61 74 20 75 73 65 72 73 20 6f 66 20 63 75 72 73  at users of curs
16a94 6f 72 73 20 63 61 6e 20 70 72 65 61 6c 6c 6f 63  ors can prealloc
16a95 61 74 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e  ate.** sufficien
16a96 74 20 73 74 6f 72 61 67 65 20 74 6f 20 68 6f 6c  t storage to hol
16a97 64 20 61 20 63 75 72 73 6f 72 2e 20 20 54 68 65  d a cursor.  The
16a98 20 42 74 43 75 72 73 6f 72 20 6f 62 6a 65 63 74   BtCursor object
16a99 20 69 73 20 6f 70 61 71 75 65 0a 2a 2a 20 74 6f   is opaque.** to
16a9a 20 75 73 65 72 73 20 73 6f 20 74 68 65 79 20 63   users so they c
16a9b 61 6e 6e 6f 74 20 64 6f 20 74 68 65 20 73 69 7a  annot do the siz
16a9c 65 6f 66 28 29 20 74 68 65 6d 73 65 6c 76 65 73  eof() themselves
16a9d 20 2d 20 74 68 65 79 20 6d 75 73 74 20 63 61 6c   - they must cal
16a9e 6c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  l.** this routin
16a9f 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
16aa0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
16aa1 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 28  BtreeCursorSize(
16aa2 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20  void){.  return 
16aa3 73 69 7a 65 6f 66 28 42 74 43 75 72 73 6f 72 29  sizeof(BtCursor)
16aa4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  ;.}../*.** Set t
16aa5 68 65 20 63 61 63 68 65 64 20 72 6f 77 69 64 20  he cached rowid 
16aa6 76 61 6c 75 65 20 6f 66 20 65 76 65 72 79 20 63  value of every c
16aa7 75 72 73 6f 72 20 69 6e 20 74 68 65 20 73 61 6d  ursor in the sam
16aa8 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a  e database file.
16aa9 2a 2a 20 61 73 20 70 43 75 72 20 61 6e 64 20 68  ** as pCur and h
16aaa 61 76 69 6e 67 20 74 68 65 20 73 61 6d 65 20 72  aving the same r
16aab 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  oot page number 
16aac 61 73 20 70 43 75 72 2e 20 20 54 68 65 20 76 61  as pCur.  The va
16aad 6c 75 65 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f  lue is.** set to
16aae 20 69 52 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 4f   iRowid..**.** O
16aaf 6e 6c 79 20 70 6f 73 69 74 69 76 65 20 72 6f 77  nly positive row
16ab0 69 64 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f  id values are co
16ab1 6e 73 69 64 65 72 65 64 20 76 61 6c 69 64 20 66  nsidered valid f
16ab2 6f 72 20 74 68 69 73 20 63 61 63 68 65 2e 0a 2a  or this cache..*
16ab3 2a 20 54 68 65 20 63 61 63 68 65 20 69 73 20 69  * The cache is i
16ab4 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a 65  nitialized to ze
16ab5 72 6f 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 61  ro, indicating a
16ab6 6e 20 69 6e 76 61 6c 69 64 20 63 61 63 68 65 2e  n invalid cache.
16ab7 0a 2a 2a 20 41 20 62 74 72 65 65 20 77 69 6c 6c  .** A btree will
16ab8 20 77 6f 72 6b 20 66 69 6e 65 20 77 69 74 68 20   work fine with 
16ab9 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65  zero or negative
16aba 20 72 6f 77 69 64 73 2e 20 20 57 65 20 6a 75 73   rowids.  We jus
16abb 74 20 63 61 6e 6e 6f 74 0a 2a 2a 20 63 61 63 68  t cannot.** cach
16abc 65 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 69  e zero or negati
16abd 76 65 20 72 6f 77 69 64 73 2c 20 77 68 69 63 68  ve rowids, which
16abe 20 6d 65 61 6e 73 20 74 61 62 6c 65 73 20 74 68   means tables th
16abf 61 74 20 75 73 65 20 7a 65 72 6f 20 6f 72 0a 2a  at use zero or.*
16ac0 2a 20 6e 65 67 61 74 69 76 65 20 72 6f 77 69 64  * negative rowid
16ac1 73 20 6d 69 67 68 74 20 72 75 6e 20 61 20 6c 69  s might run a li
16ac2 74 74 6c 65 20 73 6c 6f 77 65 72 2e 20 20 42 75  ttle slower.  Bu
16ac3 74 20 69 6e 20 70 72 61 63 74 69 63 65 2c 20 7a  t in practice, z
16ac4 65 72 6f 0a 2a 2a 20 6f 72 20 6e 65 67 61 74 69  ero.** or negati
16ac5 76 65 20 72 6f 77 69 64 73 20 61 72 65 20 76 65  ve rowids are ve
16ac6 72 79 20 75 6e 63 6f 6d 6d 6f 6e 20 73 6f 20 74  ry uncommon so t
16ac7 68 69 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  his should not b
16ac8 65 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 2a 2f 0a  e a problem..*/.
16ac9 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
16aca 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
16acb 53 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 42  SetCachedRowid(B
16acc 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 73  tCursor *pCur, s
16acd 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f  qlite3_int64 iRo
16ace 77 69 64 29 7b 0a 20 20 42 74 43 75 72 73 6f 72  wid){.  BtCursor
16acf 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 75   *p;.  for(p=pCu
16ad0 72 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b  r->pBt->pCursor;
16ad1 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b   p; p=p->pNext){
16ad2 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f  .    if( p->pgno
16ad3 52 6f 6f 74 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f  Root==pCur->pgno
16ad4 52 6f 6f 74 20 29 20 70 2d 3e 63 61 63 68 65 64  Root ) p->cached
16ad5 52 6f 77 69 64 20 3d 20 69 52 6f 77 69 64 3b 0a  Rowid = iRowid;.
16ad6 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43    }.  assert( pC
16ad7 75 72 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 3d  ur->cachedRowid=
16ad8 3d 69 52 6f 77 69 64 20 29 3b 0a 7d 0a 0a 2f 2a  =iRowid );.}../*
16ad9 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63  .** Return the c
16ada 61 63 68 65 64 20 72 6f 77 69 64 20 66 6f 72 20  ached rowid for 
16adb 74 68 65 20 67 69 76 65 6e 20 63 75 72 73 6f 72  the given cursor
16adc 2e 20 20 41 20 6e 65 67 61 74 69 76 65 20 6f 72  .  A negative or
16add 20 7a 65 72 6f 0a 2a 2a 20 72 65 74 75 72 6e 20   zero.** return 
16ade 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20  value indicates 
16adf 74 68 61 74 20 74 68 65 20 72 6f 77 69 64 20 63  that the rowid c
16ae0 61 63 68 65 20 69 73 20 69 6e 76 61 6c 69 64 20  ache is invalid 
16ae1 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a  and should be.**
16ae2 20 69 67 6e 6f 72 65 64 2e 20 20 49 66 20 74 68   ignored.  If th
16ae3 65 20 72 6f 77 69 64 20 63 61 63 68 65 20 68 61  e rowid cache ha
16ae4 73 20 6e 65 76 65 72 20 62 65 66 6f 72 65 20 62  s never before b
16ae5 65 65 6e 20 73 65 74 2c 20 74 68 65 6e 20 61 0a  een set, then a.
16ae6 2a 2a 20 7a 65 72 6f 20 69 73 20 72 65 74 75 72  ** zero is retur
16ae7 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ned..*/.SQLITE_P
16ae8 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 69  RIVATE sqlite3_i
16ae9 6e 74 36 34 20 73 71 6c 69 74 65 33 42 74 72 65  nt64 sqlite3Btre
16aea 65 47 65 74 43 61 63 68 65 64 52 6f 77 69 64 28  eGetCachedRowid(
16aeb 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
16aec 0a 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d 3e  .  return pCur->
16aed 63 61 63 68 65 64 52 6f 77 69 64 3b 0a 7d 0a 0a  cachedRowid;.}..
16aee 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 75  /*.** Close a cu
16aef 72 73 6f 72 2e 20 20 54 68 65 20 72 65 61 64 20  rsor.  The read 
16af0 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
16af1 62 61 73 65 20 66 69 6c 65 20 69 73 20 72 65 6c  base file is rel
16af2 65 61 73 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68  eased.** when th
16af3 65 20 6c 61 73 74 20 63 75 72 73 6f 72 20 69 73  e last cursor is
16af4 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49   closed..*/.SQLI
16af5 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
16af6 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65  qlite3BtreeClose
16af7 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20  Cursor(BtCursor 
16af8 2a 70 43 75 72 29 7b 0a 20 20 42 74 72 65 65 20  *pCur){.  Btree 
16af9 2a 70 42 74 72 65 65 20 3d 20 70 43 75 72 2d 3e  *pBtree = pCur->
16afa 70 42 74 72 65 65 3b 0a 20 20 69 66 28 20 70 42  pBtree;.  if( pB
16afb 74 72 65 65 20 29 7b 0a 20 20 20 20 69 6e 74 20  tree ){.    int 
16afc 69 3b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20  i;.    BtShared 
16afd 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74  *pBt = pCur->pBt
16afe 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
16aff 65 65 45 6e 74 65 72 28 70 42 74 72 65 65 29 3b  eeEnter(pBtree);
16b00 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
16b01 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 43 75  eClearCursor(pCu
16b02 72 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75 72  r);.    if( pCur
16b03 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 20  ->pPrev ){.     
16b04 20 70 43 75 72 2d 3e 70 50 72 65 76 2d 3e 70 4e   pCur->pPrev->pN
16b05 65 78 74 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78  ext = pCur->pNex
16b06 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  t;.    }else{.  
16b07 20 20 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72      pBt->pCursor
16b08 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a   = pCur->pNext;.
16b09 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 43      }.    if( pC
16b0a 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20  ur->pNext ){.   
16b0b 20 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d 3e     pCur->pNext->
16b0c 70 50 72 65 76 20 3d 20 70 43 75 72 2d 3e 70 50  pPrev = pCur->pP
16b0d 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  rev;.    }.    f
16b0e 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72 2d  or(i=0; i<=pCur-
16b0f 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20  >iPage; i++){.  
16b10 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
16b11 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29  pCur->apPage[i])
16b12 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 6c 6f  ;.    }.    unlo
16b13 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28  ckBtreeIfUnused(
16b14 70 42 74 29 3b 0a 20 20 20 20 69 6e 76 61 6c 69  pBt);.    invali
16b15 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68  dateOverflowCach
16b16 65 28 70 43 75 72 29 3b 0a 20 20 20 20 2f 2a 20  e(pCur);.    /* 
16b17 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75  sqlite3_free(pCu
16b18 72 29 3b 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  r); */.    sqlit
16b19 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 42 74  e3BtreeLeave(pBt
16b1a 72 65 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ree);.  }.  retu
16b1b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
16b1c 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
16b1d 20 74 68 65 20 42 74 43 75 72 73 6f 72 2a 20 67   the BtCursor* g
16b1e 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75  iven in the argu
16b1f 6d 65 6e 74 20 68 61 73 20 61 20 76 61 6c 69 64  ment has a valid
16b20 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66  .** BtCursor.inf
16b21 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 49 66  o structure.  If
16b22 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61   it is not alrea
16b23 64 79 20 76 61 6c 69 64 2c 20 63 61 6c 6c 0a 2a  dy valid, call.*
16b24 2a 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c  * btreeParseCell
16b25 28 29 20 74 6f 20 66 69 6c 6c 20 69 74 20 69 6e  () to fill it in
16b26 2e 0a 2a 2a 0a 2a 2a 20 42 74 43 75 72 73 6f 72  ..**.** BtCursor
16b27 2e 69 6e 66 6f 20 69 73 20 61 20 63 61 63 68 65  .info is a cache
16b28 20 6f 66 20 74 68 65 20 69 6e 66 6f 72 6d 61 74   of the informat
16b29 69 6f 6e 20 69 6e 20 74 68 65 20 63 75 72 72 65  ion in the curre
16b2a 6e 74 20 63 65 6c 6c 2e 0a 2a 2a 20 55 73 69 6e  nt cell..** Usin
16b2b 67 20 74 68 69 73 20 63 61 63 68 65 20 72 65 64  g this cache red
16b2c 75 63 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20  uces the number 
16b2d 6f 66 20 63 61 6c 6c 73 20 74 6f 20 62 74 72 65  of calls to btre
16b2e 65 50 61 72 73 65 43 65 6c 6c 28 29 2e 0a 2a 2a  eParseCell()..**
16b2f 0a 2a 2a 20 32 30 30 37 2d 30 36 2d 32 35 3a 20  .** 2007-06-25: 
16b30 20 54 68 65 72 65 20 69 73 20 61 20 62 75 67 20   There is a bug 
16b31 69 6e 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73  in some versions
16b32 20 6f 66 20 4d 53 56 43 20 74 68 61 74 20 63 61   of MSVC that ca
16b33 75 73 65 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69  use the.** compi
16b34 6c 65 72 20 74 6f 20 63 72 61 73 68 20 77 68 65  ler to crash whe
16b35 6e 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 29 20  n getCellInfo() 
16b36 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61  is implemented a
16b37 73 20 61 20 6d 61 63 72 6f 2e 0a 2a 2a 20 42 75  s a macro..** Bu
16b38 74 20 74 68 65 72 65 20 69 73 20 61 20 6d 65 61  t there is a mea
16b39 73 75 72 65 61 62 6c 65 20 73 70 65 65 64 20 61  sureable speed a
16b3a 64 76 61 6e 74 61 67 65 20 74 6f 20 75 73 69 6e  dvantage to usin
16b3b 67 20 74 68 65 20 6d 61 63 72 6f 20 6f 6e 20 67  g the macro on g
16b3c 63 63 0a 2a 2a 20 28 77 68 65 6e 20 6c 65 73 73  cc.** (when less
16b3d 20 63 6f 6d 70 69 6c 65 72 20 6f 70 74 69 6d 69   compiler optimi
16b3e 7a 61 74 69 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73  zations like -Os
16b3f 20 6f 72 20 2d 4f 30 20 61 72 65 20 75 73 65 64   or -O0 are used
16b40 20 61 6e 64 20 74 68 65 0a 2a 2a 20 63 6f 6d 70   and the.** comp
16b41 69 6c 65 72 20 69 73 20 6e 6f 74 20 64 6f 69 6e  iler is not doin
16b42 67 20 61 67 72 65 73 73 69 76 65 20 69 6e 6c 69  g agressive inli
16b43 6e 69 6e 67 2e 29 20 20 53 6f 20 77 65 20 75 73  ning.)  So we us
16b44 65 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f  e a real functio
16b45 6e 0a 2a 2a 20 66 6f 72 20 4d 53 56 43 20 61 6e  n.** for MSVC an
16b46 64 20 61 20 6d 61 63 72 6f 20 66 6f 72 20 65 76  d a macro for ev
16b47 65 72 79 74 68 69 6e 67 20 65 6c 73 65 2e 20 20  erything else.  
16b48 54 69 63 6b 65 74 20 23 32 34 35 37 2e 0a 2a 2f  Ticket #2457..*/
16b49 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
16b4a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20 61 73    static void as
16b4b 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43  sertCellInfo(BtC
16b4c 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
16b4d 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b    CellInfo info;
16b4e 0a 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d  .    int iPage =
16b4f 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20   pCur->iPage;.  
16b50 20 20 6d 65 6d 73 65 74 28 26 69 6e 66 6f 2c 20    memset(&info, 
16b51 30 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29  0, sizeof(info))
16b52 3b 0a 20 20 20 20 62 74 72 65 65 50 61 72 73 65  ;.    btreeParse
16b53 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67  Cell(pCur->apPag
16b54 65 5b 69 50 61 67 65 5d 2c 20 70 43 75 72 2d 3e  e[iPage], pCur->
16b55 61 69 49 64 78 5b 69 50 61 67 65 5d 2c 20 26 69  aiIdx[iPage], &i
16b56 6e 66 6f 29 3b 0a 20 20 20 20 61 73 73 65 72 74  nfo);.    assert
16b57 28 20 6d 65 6d 63 6d 70 28 26 69 6e 66 6f 2c 20  ( memcmp(&info, 
16b58 26 70 43 75 72 2d 3e 69 6e 66 6f 2c 20 73 69 7a  &pCur->info, siz
16b59 65 6f 66 28 69 6e 66 6f 29 29 3d 3d 30 20 29 3b  eof(info))==0 );
16b5a 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65  .  }.#else.  #de
16b5b 66 69 6e 65 20 61 73 73 65 72 74 43 65 6c 6c 49  fine assertCellI
16b5c 6e 66 6f 28 78 29 0a 23 65 6e 64 69 66 0a 23 69  nfo(x).#endif.#i
16b5d 66 64 65 66 20 5f 4d 53 43 5f 56 45 52 0a 20 20  fdef _MSC_VER.  
16b5e 2f 2a 20 55 73 65 20 61 20 72 65 61 6c 20 66 75  /* Use a real fu
16b5f 6e 63 74 69 6f 6e 20 69 6e 20 4d 53 56 43 20 74  nction in MSVC t
16b60 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 62 75  o work around bu
16b61 67 73 20 69 6e 20 74 68 61 74 20 63 6f 6d 70 69  gs in that compi
16b62 6c 65 72 2e 20 2a 2f 0a 20 20 73 74 61 74 69 63  ler. */.  static
16b63 20 76 6f 69 64 20 67 65 74 43 65 6c 6c 49 6e 66   void getCellInf
16b64 6f 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  o(BtCursor *pCur
16b65 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d  ){.    if( pCur-
16b66 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29  >info.nSize==0 )
16b67 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 50 61 67  {.      int iPag
16b68 65 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b  e = pCur->iPage;
16b69 0a 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73  .      btreePars
16b6a 65 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61  eCell(pCur->apPa
16b6b 67 65 5b 69 50 61 67 65 5d 2c 70 43 75 72 2d 3e  ge[iPage],pCur->
16b6c 61 69 49 64 78 5b 69 50 61 67 65 5d 2c 26 70 43  aiIdx[iPage],&pC
16b6d 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20 20 20 20 20  ur->info);.     
16b6e 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79   pCur->validNKey
16b6f 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 1;.    }else{
16b70 0a 20 20 20 20 20 20 61 73 73 65 72 74 43 65 6c  .      assertCel
16b71 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 20  lInfo(pCur);.   
16b72 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 20 2f 2a 20   }.  }.#else /* 
16b73 69 66 20 6e 6f 74 20 5f 4d 53 43 5f 56 45 52 20  if not _MSC_VER 
16b74 2a 2f 0a 20 20 2f 2a 20 55 73 65 20 61 20 6d 61  */.  /* Use a ma
16b75 63 72 6f 20 69 6e 20 61 6c 6c 20 6f 74 68 65 72  cro in all other
16b76 20 63 6f 6d 70 69 6c 65 72 73 20 73 6f 20 74 68   compilers so th
16b77 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  at the function 
16b78 69 73 20 69 6e 6c 69 6e 65 64 20 2a 2f 0a 23 64  is inlined */.#d
16b79 65 66 69 6e 65 20 67 65 74 43 65 6c 6c 49 6e 66  efine getCellInf
16b7a 6f 28 70 43 75 72 29 20 20 20 20 20 20 20 20 20  o(pCur)         
16b7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b7d 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
16b7e 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e   if( pCur->info.
16b7f 6e 53 69 7a 65 3d 3d 30 20 29 7b 20 20 20 20 20  nSize==0 ){     
16b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
16b83 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20      int iPage = 
16b84 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 20 20 20  pCur->iPage;    
16b85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
16b88 0a 20 20 20 20 62 74 72 65 65 50 61 72 73 65 43  .    btreeParseC
16b89 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65  ell(pCur->apPage
16b8a 5b 69 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69  [iPage],pCur->ai
16b8b 49 64 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 72  Idx[iPage],&pCur
16b8c 2d 3e 69 6e 66 6f 29 3b 20 5c 0a 20 20 20 20 70  ->info); \.    p
16b8d 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d  Cur->validNKey =
16b8e 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   1;             
16b8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b91 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65            \.  }e
16b92 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20  lse{            
16b93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b96 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
16b97 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28   assertCellInfo(
16b98 70 43 75 72 29 3b 20 20 20 20 20 20 20 20 20 20  pCur);          
16b99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b9b 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
16b9c 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 4d 53 43  }.#endif /* _MSC
16b9d 5f 56 45 52 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  _VER */..#ifndef
16b9e 20 4e 44 45 42 55 47 20 20 2f 2a 20 54 68 65 20   NDEBUG  /* The 
16b9f 6e 65 78 74 20 72 6f 75 74 69 6e 65 20 75 73 65  next routine use
16ba0 64 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 61 73  d only within as
16ba1 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
16ba2 73 20 2a 2f 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  s */./*.** Retur
16ba3 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 67 69  n true if the gi
16ba4 76 65 6e 20 42 74 43 75 72 73 6f 72 20 69 73 20  ven BtCursor is 
16ba5 76 61 6c 69 64 2e 20 20 41 20 76 61 6c 69 64 20  valid.  A valid 
16ba6 63 75 72 73 6f 72 20 69 73 20 6f 6e 65 0a 2a 2a  cursor is one.**
16ba7 20 74 68 61 74 20 69 73 20 63 75 72 72 65 6e 74   that is current
16ba8 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61  ly pointing to a
16ba9 20 72 6f 77 20 69 6e 20 61 20 28 6e 6f 6e 2d 65   row in a (non-e
16baa 6d 70 74 79 29 20 74 61 62 6c 65 2e 0a 2a 2a 20  mpty) table..** 
16bab 54 68 69 73 20 69 73 20 61 20 76 65 72 69 66 69  This is a verifi
16bac 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69  cation routine i
16bad 73 20 75 73 65 64 20 6f 6e 6c 79 20 77 69 74 68  s used only with
16bae 69 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74  in assert() stat
16baf 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ements..*/.SQLIT
16bb0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
16bb1 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
16bb2 49 73 56 61 6c 69 64 28 42 74 43 75 72 73 6f 72  IsValid(BtCursor
16bb3 20 2a 70 43 75 72 29 7b 0a 20 20 72 65 74 75 72   *pCur){.  retur
16bb4 6e 20 70 43 75 72 20 26 26 20 70 43 75 72 2d 3e  n pCur && pCur->
16bb5 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56  eState==CURSOR_V
16bb6 41 4c 49 44 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  ALID;.}.#endif /
16bb7 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a  * NDEBUG */../*.
16bb8 2a 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f  ** Set *pSize to
16bb9 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
16bba 20 62 75 66 66 65 72 20 6e 65 65 64 65 64 20 74   buffer needed t
16bbb 6f 20 68 6f 6c 64 20 74 68 65 20 76 61 6c 75 65  o hold the value
16bbc 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 20 66   of.** the key f
16bbd 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 65  or the current e
16bbe 6e 74 72 79 2e 20 20 49 66 20 74 68 65 20 63 75  ntry.  If the cu
16bbf 72 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e  rsor is not poin
16bc0 74 69 6e 67 0a 2a 2a 20 74 6f 20 61 20 76 61 6c  ting.** to a val
16bc1 69 64 20 65 6e 74 72 79 2c 20 2a 70 53 69 7a 65  id entry, *pSize
16bc2 20 69 73 20 73 65 74 20 74 6f 20 30 2e 20 0a 2a   is set to 0. .*
16bc3 2a 0a 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c 65  *.** For a table
16bc4 20 77 69 74 68 20 74 68 65 20 49 4e 54 4b 45 59   with the INTKEY
16bc5 20 66 6c 61 67 20 73 65 74 2c 20 74 68 69 73 20   flag set, this 
16bc6 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
16bc7 74 68 65 20 6b 65 79 0a 2a 2a 20 69 74 73 65 6c  the key.** itsel
16bc8 66 2c 20 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65  f, not the numbe
16bc9 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
16bca 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  e key..**.** The
16bcb 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 70 6f 73   caller must pos
16bcc 69 74 69 6f 6e 20 74 68 65 20 63 75 72 73 6f 72  ition the cursor
16bcd 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69   prior to invoki
16bce 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  ng this routine.
16bcf 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75  .** .** This rou
16bd0 74 69 6e 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c  tine cannot fail
16bd1 2e 20 20 49 74 20 61 6c 77 61 79 73 20 72 65 74  .  It always ret
16bd2 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 20  urns SQLITE_OK. 
16bd3 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
16bd4 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
16bd5 74 72 65 65 4b 65 79 53 69 7a 65 28 42 74 43 75  treeKeySize(BtCu
16bd6 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 34 20  rsor *pCur, i64 
16bd7 2a 70 53 69 7a 65 29 7b 0a 20 20 61 73 73 65 72  *pSize){.  asser
16bd8 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
16bd9 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61  tex(pCur) );.  a
16bda 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74  ssert( pCur->eSt
16bdb 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41  ate==CURSOR_INVA
16bdc 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74  LID || pCur->eSt
16bdd 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
16bde 44 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d  D );.  if( pCur-
16bdf 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f  >eState!=CURSOR_
16be0 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 2a 70 53  VALID ){.    *pS
16be1 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  ize = 0;.  }else
16be2 7b 0a 20 20 20 20 67 65 74 43 65 6c 6c 49 6e 66  {.    getCellInf
16be3 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 2a 70 53  o(pCur);.    *pS
16be4 69 7a 65 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f  ize = pCur->info
16be5 2e 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 72 65 74  .nKey;.  }.  ret
16be6 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
16be7 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 53 69  ../*.** Set *pSi
16be8 7a 65 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72  ze to the number
16be9 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74   of bytes of dat
16bea 61 20 69 6e 20 74 68 65 20 65 6e 74 72 79 20 74  a in the entry t
16beb 68 65 0a 2a 2a 20 63 75 72 73 6f 72 20 63 75 72  he.** cursor cur
16bec 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f  rently points to
16bed 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c  ..**.** The call
16bee 65 72 20 6d 75 73 74 20 67 75 61 72 61 6e 74 65  er must guarante
16bef 65 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f  e that the curso
16bf0 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f  r is pointing to
16bf1 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 76   a non-NULL.** v
16bf2 61 6c 69 64 20 65 6e 74 72 79 2e 20 20 49 6e 20  alid entry.  In 
16bf3 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65  other words, the
16bf4 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75   calling procedu
16bf5 72 65 20 6d 75 73 74 20 67 75 61 72 61 6e 74 65  re must guarante
16bf6 65 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63 75  e.** that the cu
16bf7 72 73 6f 72 20 68 61 73 20 43 75 72 73 6f 72 2e  rsor has Cursor.
16bf8 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56  eState==CURSOR_V
16bf9 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a 20 46 61 69 6c  ALID..**.** Fail
16bfa 75 72 65 20 69 73 20 6e 6f 74 20 70 6f 73 73 69  ure is not possi
16bfb 62 6c 65 2e 20 20 54 68 69 73 20 66 75 6e 63 74  ble.  This funct
16bfc 69 6f 6e 20 61 6c 77 61 79 73 20 72 65 74 75 72  ion always retur
16bfd 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a  ns SQLITE_OK..**
16bfe 20 49 74 20 6d 69 67 68 74 20 6a 75 73 74 20 61   It might just a
16bff 73 20 77 65 6c 6c 20 62 65 20 61 20 70 72 6f 63  s well be a proc
16c00 65 64 75 72 65 20 28 72 65 74 75 72 6e 69 6e 67  edure (returning
16c01 20 76 6f 69 64 29 20 62 75 74 20 77 65 20 63 6f   void) but we co
16c02 6e 74 69 6e 75 65 0a 2a 2a 20 74 6f 20 72 65 74  ntinue.** to ret
16c03 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 72  urn an integer r
16c04 65 73 75 6c 74 20 63 6f 64 65 20 66 6f 72 20 68  esult code for h
16c05 69 73 74 6f 72 69 63 61 6c 20 72 65 61 73 6f 6e  istorical reason
16c06 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
16c07 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
16c08 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 42 74  BtreeDataSize(Bt
16c09 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33  Cursor *pCur, u3
16c0a 32 20 2a 70 53 69 7a 65 29 7b 0a 20 20 61 73 73  2 *pSize){.  ass
16c0b 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
16c0c 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
16c0d 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65   assert( pCur->e
16c0e 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
16c0f 4c 49 44 20 29 3b 0a 20 20 67 65 74 43 65 6c 6c  LID );.  getCell
16c10 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 2a 70  Info(pCur);.  *p
16c11 53 69 7a 65 20 3d 20 70 43 75 72 2d 3e 69 6e 66  Size = pCur->inf
16c12 6f 2e 6e 44 61 74 61 3b 0a 20 20 72 65 74 75 72  o.nData;.  retur
16c13 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
16c14 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20  /*.** Given the 
16c15 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61  page number of a
16c16 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  n overflow page 
16c17 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
16c18 28 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 6f 76  (parameter.** ov
16c19 66 6c 29 2c 20 74 68 69 73 20 66 75 6e 63 74 69  fl), this functi
16c1a 6f 6e 20 66 69 6e 64 73 20 74 68 65 20 70 61 67  on finds the pag
16c1b 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
16c1c 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65  next page in the
16c1d 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74   .** linked list
16c1e 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   of overflow pag
16c1f 65 73 2e 20 49 66 20 70 6f 73 73 69 62 6c 65 2c  es. If possible,
16c20 20 69 74 20 75 73 65 73 20 74 68 65 20 61 75 74   it uses the aut
16c21 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e  o-vacuum.** poin
16c22 74 65 72 2d 6d 61 70 20 64 61 74 61 20 69 6e 73  ter-map data ins
16c23 74 65 61 64 20 6f 66 20 72 65 61 64 69 6e 67 20  tead of reading 
16c24 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70  the content of p
16c25 61 67 65 20 6f 76 66 6c 20 74 6f 20 64 6f 20 73  age ovfl to do s
16c26 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  o. .**.** If an 
16c27 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e 20  error occurs an 
16c28 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64  SQLite error cod
16c29 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f  e is returned. O
16c2a 74 68 65 72 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20  therwise:.**.** 
16c2b 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  The page number 
16c2c 6f 66 20 74 68 65 20 6e 65 78 74 20 6f 76 65 72  of the next over
16c2d 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65  flow page in the
16c2e 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 20   linked list is 
16c2f 0a 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 2a  .** written to *
16c30 70 50 67 6e 6f 4e 65 78 74 2e 20 49 66 20 70 61  pPgnoNext. If pa
16c31 67 65 20 6f 76 66 6c 20 69 73 20 74 68 65 20 6c  ge ovfl is the l
16c32 61 73 74 20 70 61 67 65 20 69 6e 20 69 74 73 20  ast page in its 
16c33 6c 69 6e 6b 65 64 20 0a 2a 2a 20 6c 69 73 74 2c  linked .** list,
16c34 20 2a 70 50 67 6e 6f 4e 65 78 74 20 69 73 20 73   *pPgnoNext is s
16c35 65 74 20 74 6f 20 7a 65 72 6f 2e 20 0a 2a 2a 0a  et to zero. .**.
16c36 2a 2a 20 49 66 20 70 70 50 61 67 65 20 69 73 20  ** If ppPage is 
16c37 6e 6f 74 20 4e 55 4c 4c 2c 20 61 6e 64 20 61 20  not NULL, and a 
16c38 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65  reference to the
16c39 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20   MemPage object 
16c3a 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a  corresponding.**
16c3b 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 20   to page number 
16c3c 70 4f 76 66 6c 20 77 61 73 20 6f 62 74 61 69 6e  pOvfl was obtain
16c3d 65 64 2c 20 74 68 65 6e 20 2a 70 70 50 61 67 65  ed, then *ppPage
16c3e 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74   is set to point
16c3f 20 74 6f 20 74 68 61 74 0a 2a 2a 20 72 65 66 65   to that.** refe
16c40 72 65 6e 63 65 2e 20 49 74 20 69 73 20 74 68 65  rence. It is the
16c41 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20   responsibility 
16c42 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f  of the caller to
16c43 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 50 61 67   call releasePag
16c44 65 28 29 0a 2a 2a 20 6f 6e 20 2a 70 70 50 61 67  e().** on *ppPag
16c45 65 20 74 6f 20 66 72 65 65 20 74 68 65 20 72 65  e to free the re
16c46 66 65 72 65 6e 63 65 2e 20 49 6e 20 6e 6f 20 72  ference. In no r
16c47 65 66 65 72 65 6e 63 65 20 77 61 73 20 6f 62 74  eference was obt
16c48 61 69 6e 65 64 20 28 62 65 63 61 75 73 65 0a 2a  ained (because.*
16c49 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  * the pointer-ma
16c4a 70 20 77 61 73 20 75 73 65 64 20 74 6f 20 6f 62  p was used to ob
16c4b 74 61 69 6e 20 74 68 65 20 76 61 6c 75 65 20 66  tain the value f
16c4c 6f 72 20 2a 70 50 67 6e 6f 4e 65 78 74 29 2c 20  or *pPgnoNext), 
16c4d 74 68 65 6e 0a 2a 2a 20 2a 70 70 50 61 67 65 20  then.** *ppPage 
16c4e 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 0a  is set to zero..
16c4f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
16c50 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 0a 20  tOverflowPage(. 
16c51 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20   BtShared *pBt, 
16c52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16c53 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69   The database fi
16c54 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6f 76 66  le */.  Pgno ovf
16c55 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
16c56 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
16c57 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75  overflow page nu
16c58 6d 62 65 72 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  mber */.  MemPag
16c59 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20 20  e **ppPage,     
16c5a 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d         /* OUT: M
16c5b 65 6d 50 61 67 65 20 68 61 6e 64 6c 65 20 28 6d  emPage handle (m
16c5c 61 79 20 62 65 20 4e 55 4c 4c 29 20 2a 2f 0a 20  ay be NULL) */. 
16c5d 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 4e 65 78 74   Pgno *pPgnoNext
16c5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16c5f 20 4f 55 54 3a 20 4e 65 78 74 20 6f 76 65 72 66   OUT: Next overf
16c60 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20  low page number 
16c61 2a 2f 0a 29 7b 0a 20 20 50 67 6e 6f 20 6e 65 78  */.){.  Pgno nex
16c62 74 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65  t = 0;.  MemPage
16c63 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 69   *pPage = 0;.  i
16c64 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
16c65 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  K;..  assert( sq
16c66 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
16c67 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
16c68 20 20 61 73 73 65 72 74 28 70 50 67 6e 6f 4e 65    assert(pPgnoNe
16c69 78 74 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  xt);..#ifndef SQ
16c6a 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
16c6b 43 55 55 4d 0a 20 20 2f 2a 20 54 72 79 20 74 6f  CUUM.  /* Try to
16c6c 20 66 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70   find the next p
16c6d 61 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72 66  age in the overf
16c6e 6c 6f 77 20 6c 69 73 74 20 75 73 69 6e 67 20 74  low list using t
16c6f 68 65 0a 20 20 2a 2a 20 61 75 74 6f 76 61 63 75  he.  ** autovacu
16c70 75 6d 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70  um pointer-map p
16c71 61 67 65 73 2e 20 47 75 65 73 73 20 74 68 61 74  ages. Guess that
16c72 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69   the next page i
16c73 6e 20 0a 20 20 2a 2a 20 74 68 65 20 6f 76 65 72  n .  ** the over
16c74 66 6c 6f 77 20 6c 69 73 74 20 69 73 20 70 61 67  flow list is pag
16c75 65 20 6e 75 6d 62 65 72 20 28 6f 76 66 6c 2b 31  e number (ovfl+1
16c76 29 2e 20 49 66 20 74 68 61 74 20 67 75 65 73 73  ). If that guess
16c77 20 74 75 72 6e 73 20 0a 20 20 2a 2a 20 6f 75 74   turns .  ** out
16c78 20 74 6f 20 62 65 20 77 72 6f 6e 67 2c 20 66 61   to be wrong, fa
16c79 6c 6c 20 62 61 63 6b 20 74 6f 20 6c 6f 61 64 69  ll back to loadi
16c7a 6e 67 20 74 68 65 20 64 61 74 61 20 6f 66 20 70  ng the data of p
16c7b 61 67 65 20 0a 20 20 2a 2a 20 6e 75 6d 62 65 72  age .  ** number
16c7c 20 6f 76 66 6c 20 74 6f 20 64 65 74 65 72 6d 69   ovfl to determi
16c7d 6e 65 20 74 68 65 20 6e 65 78 74 20 70 61 67 65  ne the next page
16c7e 20 6e 75 6d 62 65 72 2e 0a 20 20 2a 2f 0a 20 20   number..  */.  
16c7f 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
16c80 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20  uum ){.    Pgno 
16c81 70 67 6e 6f 3b 0a 20 20 20 20 50 67 6e 6f 20 69  pgno;.    Pgno i
16c82 47 75 65 73 73 20 3d 20 6f 76 66 6c 2b 31 3b 0a  Guess = ovfl+1;.
16c83 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a 0a 20      u8 eType;.. 
16c84 20 20 20 77 68 69 6c 65 28 20 50 54 52 4d 41 50     while( PTRMAP
16c85 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69 47 75  _ISPAGE(pBt, iGu
16c86 65 73 73 29 20 7c 7c 20 69 47 75 65 73 73 3d 3d  ess) || iGuess==
16c87 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
16c88 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20  E(pBt) ){.      
16c89 69 47 75 65 73 73 2b 2b 3b 0a 20 20 20 20 7d 0a  iGuess++;.    }.
16c8a 0a 20 20 20 20 69 66 28 20 69 47 75 65 73 73 3c  .    if( iGuess<
16c8b 3d 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  =pagerPagecount(
16c8c 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 72 63  pBt) ){.      rc
16c8d 20 3d 20 70 74 72 6d 61 70 47 65 74 28 70 42 74   = ptrmapGet(pBt
16c8e 2c 20 69 47 75 65 73 73 2c 20 26 65 54 79 70 65  , iGuess, &eType
16c8f 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20  , &pgno);.      
16c90 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
16c91 4b 20 26 26 20 65 54 79 70 65 3d 3d 50 54 52 4d  K && eType==PTRM
16c92 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 26 26 20  AP_OVERFLOW2 && 
16c93 70 67 6e 6f 3d 3d 6f 76 66 6c 20 29 7b 0a 20 20  pgno==ovfl ){.  
16c94 20 20 20 20 20 20 6e 65 78 74 20 3d 20 69 47 75        next = iGu
16c95 65 73 73 3b 0a 20 20 20 20 20 20 20 20 72 63 20  ess;.        rc 
16c96 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  = SQLITE_DONE;. 
16c97 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
16c98 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72  .#endif..  asser
16c99 74 28 20 6e 65 78 74 3d 3d 30 20 7c 7c 20 72 63  t( next==0 || rc
16c9a 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b  ==SQLITE_DONE );
16c9b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
16c9c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d  E_OK ){.    rc =
16c9d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42   btreeGetPage(pB
16c9e 74 2c 20 6f 76 66 6c 2c 20 26 70 50 61 67 65 2c  t, ovfl, &pPage,
16c9f 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28   0);.    assert(
16ca0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc==SQLITE_OK |
16ca1 7c 20 70 50 61 67 65 3d 3d 30 20 29 3b 0a 20 20  | pPage==0 );.  
16ca2 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
16ca3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 6e 65 78  _OK ){.      nex
16ca4 74 20 3d 20 67 65 74 34 62 79 74 65 28 70 50 61  t = get4byte(pPa
16ca5 67 65 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20 20  ge->aData);.    
16ca6 7d 0a 20 20 7d 0a 0a 20 20 2a 70 50 67 6e 6f 4e  }.  }..  *pPgnoN
16ca7 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 20 69 66  ext = next;.  if
16ca8 28 20 70 70 50 61 67 65 20 29 7b 0a 20 20 20 20  ( ppPage ){.    
16ca9 2a 70 70 50 61 67 65 20 3d 20 70 50 61 67 65 3b  *ppPage = pPage;
16caa 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
16cab 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
16cac 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  ;.  }.  return (
16cad 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
16cae 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 72 63  ? SQLITE_OK : rc
16caf 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70 79  );.}../*.** Copy
16cb0 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66   data from a buf
16cb1 66 65 72 20 74 6f 20 61 20 70 61 67 65 2c 20 6f  fer to a page, o
16cb2 72 20 66 72 6f 6d 20 61 20 70 61 67 65 20 74 6f  r from a page to
16cb3 20 61 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a   a buffer..**.**
16cb4 20 70 50 61 79 6c 6f 61 64 20 69 73 20 61 20 70   pPayload is a p
16cb5 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 61 20 73  ointer to data s
16cb6 74 6f 72 65 64 20 6f 6e 20 64 61 74 61 62 61 73  tored on databas
16cb7 65 20 70 61 67 65 20 70 44 62 50 61 67 65 2e 0a  e page pDbPage..
16cb8 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 65  ** If argument e
16cb9 4f 70 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65  Op is false, the
16cba 6e 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66  n nByte bytes of
16cbb 20 64 61 74 61 20 61 72 65 20 63 6f 70 69 65 64   data are copied
16cbc 0a 2a 2a 20 66 72 6f 6d 20 70 50 61 79 6c 6f 61  .** from pPayloa
16cbd 64 20 74 6f 20 74 68 65 20 62 75 66 66 65 72 20  d to the buffer 
16cbe 70 6f 69 6e 74 65 64 20 61 74 20 62 79 20 70 42  pointed at by pB
16cbf 75 66 2e 20 49 66 20 65 4f 70 20 69 73 20 74 72  uf. If eOp is tr
16cc0 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 73 71 6c 69  ue,.** then sqli
16cc1 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20  te3PagerWrite() 
16cc2 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 70 44 62  is called on pDb
16cc3 50 61 67 65 20 61 6e 64 20 6e 42 79 74 65 20 62  Page and nByte b
16cc4 79 74 65 73 0a 2a 2a 20 6f 66 20 64 61 74 61 20  ytes.** of data 
16cc5 61 72 65 20 63 6f 70 69 65 64 20 66 72 6f 6d 20  are copied from 
16cc6 74 68 65 20 62 75 66 66 65 72 20 70 42 75 66 20  the buffer pBuf 
16cc7 74 6f 20 70 50 61 79 6c 6f 61 64 2e 0a 2a 2a 0a  to pPayload..**.
16cc8 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
16cc9 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63  returned on succ
16cca 65 73 73 2c 20 6f 74 68 65 72 77 69 73 65 20 61  ess, otherwise a
16ccb 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f  n error code..*/
16ccc 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 70 79  .static int copy
16ccd 50 61 79 6c 6f 61 64 28 0a 20 20 76 6f 69 64 20  Payload(.  void 
16cce 2a 70 50 61 79 6c 6f 61 64 2c 20 20 20 20 20 20  *pPayload,      
16ccf 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
16cd0 74 6f 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a  to page data */.
16cd1 20 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20    void *pBuf,   
16cd2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
16cd3 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 66 65 72  ointer to buffer
16cd4 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c   */.  int nByte,
16cd5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16cd6 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
16cd7 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20  es to copy */.  
16cd8 69 6e 74 20 65 4f 70 2c 20 20 20 20 20 20 20 20  int eOp,        
16cd9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 20 2d            /* 0 -
16cda 3e 20 63 6f 70 79 20 66 72 6f 6d 20 70 61 67 65  > copy from page
16cdb 2c 20 31 20 2d 3e 20 63 6f 70 79 20 74 6f 20 70  , 1 -> copy to p
16cdc 61 67 65 20 2a 2f 0a 20 20 44 62 50 61 67 65 20  age */.  DbPage 
16cdd 2a 70 44 62 50 61 67 65 20 20 20 20 20 20 20 20  *pDbPage        
16cde 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61     /* Page conta
16cdf 69 6e 69 6e 67 20 70 50 61 79 6c 6f 61 64 20 2a  ining pPayload *
16ce0 2f 0a 29 7b 0a 20 20 69 66 28 20 65 4f 70 20 29  /.){.  if( eOp )
16ce1 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 64 61  {.    /* Copy da
16ce2 74 61 20 66 72 6f 6d 20 62 75 66 66 65 72 20 74  ta from buffer t
16ce3 6f 20 70 61 67 65 20 28 61 20 77 72 69 74 65 20  o page (a write 
16ce4 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f 0a 20 20  operation) */.  
16ce5 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74    int rc = sqlit
16ce6 65 33 50 61 67 65 72 57 72 69 74 65 28 70 44 62  e3PagerWrite(pDb
16ce7 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Page);.    if( r
16ce8 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
16ce9 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
16cea 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70  .    }.    memcp
16ceb 79 28 70 50 61 79 6c 6f 61 64 2c 20 70 42 75 66  y(pPayload, pBuf
16cec 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d 65 6c 73  , nByte);.  }els
16ced 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 64  e{.    /* Copy d
16cee 61 74 61 20 66 72 6f 6d 20 70 61 67 65 20 74 6f  ata from page to
16cef 20 62 75 66 66 65 72 20 28 61 20 72 65 61 64 20   buffer (a read 
16cf0 6f 70 65 72 61 74 69 6f 6e 29 20 2a 2f 0a 20 20  operation) */.  
16cf1 20 20 6d 65 6d 63 70 79 28 70 42 75 66 2c 20 70    memcpy(pBuf, p
16cf2 50 61 79 6c 6f 61 64 2c 20 6e 42 79 74 65 29 3b  Payload, nByte);
16cf3 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
16cf4 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
16cf5 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
16cf6 69 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20  is used to read 
16cf7 6f 72 20 6f 76 65 72 77 72 69 74 65 20 70 61 79  or overwrite pay
16cf8 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  load information
16cf9 0a 2a 2a 20 66 6f 72 20 74 68 65 20 65 6e 74 72  .** for the entr
16cfa 79 20 74 68 61 74 20 74 68 65 20 70 43 75 72 20  y that the pCur 
16cfb 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69  cursor is pointi
16cfc 6e 67 20 74 6f 2e 20 49 66 20 74 68 65 20 65 4f  ng to. If the eO
16cfd 70 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69  p.** parameter i
16cfe 73 20 30 2c 20 74 68 69 73 20 69 73 20 61 20 72  s 0, this is a r
16cff 65 61 64 20 6f 70 65 72 61 74 69 6f 6e 20 28 64  ead operation (d
16d00 61 74 61 20 63 6f 70 69 65 64 20 69 6e 74 6f 0a  ata copied into.
16d01 2a 2a 20 62 75 66 66 65 72 20 70 42 75 66 29 2e  ** buffer pBuf).
16d02 20 49 66 20 69 74 20 69 73 20 6e 6f 6e 2d 7a 65   If it is non-ze
16d03 72 6f 2c 20 61 20 77 72 69 74 65 20 28 64 61 74  ro, a write (dat
16d04 61 20 63 6f 70 69 65 64 20 66 72 6f 6d 0a 2a 2a  a copied from.**
16d05 20 62 75 66 66 65 72 20 70 42 75 66 29 2e 0a 2a   buffer pBuf)..*
16d06 2a 0a 2a 2a 20 41 20 74 6f 74 61 6c 20 6f 66 20  *.** A total of 
16d07 22 61 6d 74 22 20 62 79 74 65 73 20 61 72 65 20  "amt" bytes are 
16d08 72 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 20  read or written 
16d09 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 22 6f 66  beginning at "of
16d0a 66 73 65 74 22 2e 0a 2a 2a 20 44 61 74 61 20 69  fset"..** Data i
16d0b 73 20 72 65 61 64 20 74 6f 20 6f 72 20 66 72 6f  s read to or fro
16d0c 6d 20 74 68 65 20 62 75 66 66 65 72 20 70 42 75  m the buffer pBu
16d0d 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e  f..**.** The con
16d0e 74 65 6e 74 20 62 65 69 6e 67 20 72 65 61 64 20  tent being read 
16d0f 6f 72 20 77 72 69 74 74 65 6e 20 6d 69 67 68 74  or written might
16d10 20 61 70 70 65 61 72 20 6f 6e 20 74 68 65 20 6d   appear on the m
16d11 61 69 6e 20 70 61 67 65 0a 2a 2a 20 6f 72 20 62  ain page.** or b
16d12 65 20 73 63 61 74 74 65 72 65 64 20 6f 75 74 20  e scattered out 
16d13 6f 6e 20 6d 75 6c 74 69 70 6c 65 20 6f 76 65 72  on multiple over
16d14 66 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a  flow pages..**.*
16d15 2a 20 49 66 20 74 68 65 20 42 74 43 75 72 73 6f  * If the BtCurso
16d16 72 2e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64  r.isIncrblobHand
16d17 6c 65 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  le flag is set, 
16d18 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 0a  and the current.
16d19 2a 2a 20 63 75 72 73 6f 72 20 65 6e 74 72 79 20  ** cursor entry 
16d1a 75 73 65 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  uses one or more
16d1b 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c   overflow pages,
16d1c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a   this function.*
16d1d 2a 20 61 6c 6c 6f 63 61 74 65 73 20 73 70 61 63  * allocates spac
16d1e 65 20 66 6f 72 20 61 6e 64 20 6c 61 7a 69 6c 79  e for and lazily
16d1f 20 70 6f 70 6c 75 61 74 65 73 20 74 68 65 20 6f   popluates the o
16d20 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73  verflow page-lis
16d21 74 20 0a 2a 2a 20 63 61 63 68 65 20 61 72 72 61  t .** cache arra
16d22 79 20 28 42 74 43 75 72 73 6f 72 2e 61 4f 76 65  y (BtCursor.aOve
16d23 72 66 6c 6f 77 29 2e 20 53 75 62 73 65 71 75 65  rflow). Subseque
16d24 6e 74 20 63 61 6c 6c 73 20 75 73 65 20 74 68 69  nt calls use thi
16d25 73 0a 2a 2a 20 63 61 63 68 65 20 74 6f 20 6d 61  s.** cache to ma
16d26 6b 65 20 73 65 65 6b 69 6e 67 20 74 6f 20 74 68  ke seeking to th
16d27 65 20 73 75 70 70 6c 69 65 64 20 6f 66 66 73 65  e supplied offse
16d28 74 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74  t more efficient
16d29 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 61 6e 20  ..**.** Once an 
16d2a 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69  overflow page-li
16d2b 73 74 20 63 61 63 68 65 20 68 61 73 20 62 65 65  st cache has bee
16d2c 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20  n allocated, it 
16d2d 6d 61 79 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69  may be.** invali
16d2e 64 61 74 65 64 20 69 66 20 73 6f 6d 65 20 6f 74  dated if some ot
16d2f 68 65 72 20 63 75 72 73 6f 72 20 77 72 69 74 65  her cursor write
16d30 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 74 61  s to the same ta
16d31 62 6c 65 2c 20 6f 72 20 69 66 0a 2a 2a 20 74 68  ble, or if.** th
16d32 65 20 63 75 72 73 6f 72 20 69 73 20 6d 6f 76 65  e cursor is move
16d33 64 20 74 6f 20 61 20 64 69 66 66 65 72 65 6e 74  d to a different
16d34 20 72 6f 77 2e 20 41 64 64 69 74 69 6f 6e 61 6c   row. Additional
16d35 6c 79 2c 20 69 6e 20 61 75 74 6f 2d 76 61 63 75  ly, in auto-vacu
16d36 75 6d 0a 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 20  um.** mode, the 
16d37 66 6f 6c 6c 6f 77 69 6e 67 20 65 76 65 6e 74 73  following events
16d38 20 6d 61 79 20 69 6e 76 61 6c 69 64 61 74 65 20   may invalidate 
16d39 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  an overflow page
16d3a 2d 6c 69 73 74 20 63 61 63 68 65 2e 0a 2a 2a 0a  -list cache..**.
16d3b 2a 2a 20 20 20 2a 20 41 6e 20 69 6e 63 72 65 6d  **   * An increm
16d3c 65 6e 74 61 6c 20 76 61 63 75 75 6d 2c 0a 2a 2a  ental vacuum,.**
16d3d 20 20 20 2a 20 41 20 63 6f 6d 6d 69 74 20 69 6e     * A commit in
16d3e 20 61 75 74 6f 5f 76 61 63 75 75 6d 3d 22 66 75   auto_vacuum="fu
16d3f 6c 6c 22 20 6d 6f 64 65 2c 0a 2a 2a 20 20 20 2a  ll" mode,.**   *
16d40 20 43 72 65 61 74 69 6e 67 20 61 20 74 61 62 6c   Creating a tabl
16d41 65 20 28 6d 61 79 20 72 65 71 75 69 72 65 20 6d  e (may require m
16d42 6f 76 69 6e 67 20 61 6e 20 6f 76 65 72 66 6c 6f  oving an overflo
16d43 77 20 70 61 67 65 29 2e 0a 2a 2f 0a 73 74 61 74  w page)..*/.stat
16d44 69 63 20 69 6e 74 20 61 63 63 65 73 73 50 61 79  ic int accessPay
16d45 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f 72  load(.  BtCursor
16d46 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a 20   *pCur,      /* 
16d47 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  Cursor pointing 
16d48 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61 64  to entry to read
16d49 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 6f   from */.  u32 o
16d4a 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20  ffset,          
16d4b 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67  /* Begin reading
16d4c 20 74 68 69 73 20 66 61 72 20 69 6e 74 6f 20 70   this far into p
16d4d 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 75 33 32 20  ayload */.  u32 
16d4e 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  amt,            
16d4f 20 2f 2a 20 52 65 61 64 20 74 68 69 73 20 6d 61   /* Read this ma
16d50 6e 79 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 6e  ny bytes */.  un
16d51 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 42 75  signed char *pBu
16d52 66 2c 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20  f, /* Write the 
16d53 62 79 74 65 73 20 69 6e 74 6f 20 74 68 69 73 20  bytes into this 
16d54 62 75 66 66 65 72 20 2a 2f 20 0a 20 20 69 6e 74  buffer */ .  int
16d55 20 65 4f 70 20 20 20 20 20 20 20 20 20 20 20 20   eOp            
16d56 20 20 2f 2a 20 7a 65 72 6f 20 74 6f 20 72 65 61    /* zero to rea
16d57 64 2e 20 6e 6f 6e 2d 7a 65 72 6f 20 74 6f 20 77  d. non-zero to w
16d58 72 69 74 65 2e 20 2a 2f 0a 29 7b 0a 20 20 75 6e  rite. */.){.  un
16d59 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 50 61  signed char *aPa
16d5a 79 6c 6f 61 64 3b 0a 20 20 69 6e 74 20 72 63 20  yload;.  int rc 
16d5b 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75  = SQLITE_OK;.  u
16d5c 33 32 20 6e 4b 65 79 3b 0a 20 20 69 6e 74 20 69  32 nKey;.  int i
16d5d 49 64 78 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61  Idx = 0;.  MemPa
16d5e 67 65 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72  ge *pPage = pCur
16d5f 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
16d60 50 61 67 65 5d 3b 20 2f 2a 20 42 74 72 65 65 20  Page]; /* Btree 
16d61 70 61 67 65 20 6f 66 20 63 75 72 72 65 6e 74 20  page of current 
16d62 65 6e 74 72 79 20 2a 2f 0a 20 20 42 74 53 68 61  entry */.  BtSha
16d63 72 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72 2d  red *pBt = pCur-
16d64 3e 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20  >pBt;           
16d65 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20         /* Btree 
16d66 74 68 69 73 20 63 75 72 73 6f 72 20 62 65 6c 6f  this cursor belo
16d67 6e 67 73 20 74 6f 20 2a 2f 0a 0a 20 20 61 73 73  ngs to */..  ass
16d68 65 72 74 28 20 70 50 61 67 65 20 29 3b 0a 20 20  ert( pPage );.  
16d69 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53  assert( pCur->eS
16d6a 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c  tate==CURSOR_VAL
16d6b 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ID );.  assert( 
16d6c 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
16d6d 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e  ->iPage]<pPage->
16d6e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72  nCell );.  asser
16d6f 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
16d70 74 65 78 28 70 43 75 72 29 20 29 3b 0a 0a 20 20  tex(pCur) );..  
16d71 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72  getCellInfo(pCur
16d72 29 3b 0a 20 20 61 50 61 79 6c 6f 61 64 20 3d 20  );.  aPayload = 
16d73 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 43 65 6c 6c  pCur->info.pCell
16d74 20 2b 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 48   + pCur->info.nH
16d75 65 61 64 65 72 3b 0a 20 20 6e 4b 65 79 20 3d 20  eader;.  nKey = 
16d76 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3f  (pPage->intKey ?
16d77 20 30 20 3a 20 28 69 6e 74 29 70 43 75 72 2d 3e   0 : (int)pCur->
16d78 69 6e 66 6f 2e 6e 4b 65 79 29 3b 0a 0a 20 20 69  info.nKey);..  i
16d79 66 28 20 4e 45 56 45 52 28 6f 66 66 73 65 74 2b  f( NEVER(offset+
16d7a 61 6d 74 20 3e 20 6e 4b 65 79 2b 70 43 75 72 2d  amt > nKey+pCur-
16d7b 3e 69 6e 66 6f 2e 6e 44 61 74 61 29 20 0a 20 20  >info.nData) .  
16d7c 20 7c 7c 20 26 61 50 61 79 6c 6f 61 64 5b 70 43   || &aPayload[pC
16d7d 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d  ur->info.nLocal]
16d7e 20 3e 20 26 70 50 61 67 65 2d 3e 61 44 61 74 61   > &pPage->aData
16d7f 5b 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  [pBt->usableSize
16d80 5d 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20 54 72  ].  ){.    /* Tr
16d81 79 69 6e 67 20 74 6f 20 72 65 61 64 20 6f 72 20  ying to read or 
16d82 77 72 69 74 65 20 70 61 73 74 20 74 68 65 20 65  write past the e
16d83 6e 64 20 6f 66 20 74 68 65 20 64 61 74 61 20 69  nd of the data i
16d84 73 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20  s an error */.  
16d85 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
16d86 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
16d87 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66  }..  /* Check if
16d88 20 64 61 74 61 20 6d 75 73 74 20 62 65 20 72 65   data must be re
16d89 61 64 2f 77 72 69 74 74 65 6e 20 74 6f 2f 66 72  ad/written to/fr
16d8a 6f 6d 20 74 68 65 20 62 74 72 65 65 20 70 61 67  om the btree pag
16d8b 65 20 69 74 73 65 6c 66 2e 20 2a 2f 0a 20 20 69  e itself. */.  i
16d8c 66 28 20 6f 66 66 73 65 74 3c 70 43 75 72 2d 3e  f( offset<pCur->
16d8d 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20  info.nLocal ){. 
16d8e 20 20 20 69 6e 74 20 61 20 3d 20 61 6d 74 3b 0a     int a = amt;.
16d8f 20 20 20 20 69 66 28 20 61 2b 6f 66 66 73 65 74      if( a+offset
16d90 3e 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63  >pCur->info.nLoc
16d91 61 6c 20 29 7b 0a 20 20 20 20 20 20 61 20 3d 20  al ){.      a = 
16d92 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61  pCur->info.nLoca
16d93 6c 20 2d 20 6f 66 66 73 65 74 3b 0a 20 20 20 20  l - offset;.    
16d94 7d 0a 20 20 20 20 72 63 20 3d 20 63 6f 70 79 50  }.    rc = copyP
16d95 61 79 6c 6f 61 64 28 26 61 50 61 79 6c 6f 61 64  ayload(&aPayload
16d96 5b 6f 66 66 73 65 74 5d 2c 20 70 42 75 66 2c 20  [offset], pBuf, 
16d97 61 2c 20 65 4f 70 2c 20 70 50 61 67 65 2d 3e 70  a, eOp, pPage->p
16d98 44 62 50 61 67 65 29 3b 0a 20 20 20 20 6f 66 66  DbPage);.    off
16d99 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 70 42 75  set = 0;.    pBu
16d9a 66 20 2b 3d 20 61 3b 0a 20 20 20 20 61 6d 74 20  f += a;.    amt 
16d9b 2d 3d 20 61 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  -= a;.  }else{. 
16d9c 20 20 20 6f 66 66 73 65 74 20 2d 3d 20 70 43 75     offset -= pCu
16d9d 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a  r->info.nLocal;.
16d9e 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53    }..  if( rc==S
16d9f 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e  QLITE_OK && amt>
16da0 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 75  0 ){.    const u
16da1 33 32 20 6f 76 66 6c 53 69 7a 65 20 3d 20 70 42  32 ovflSize = pB
16da2 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20  t->usableSize - 
16da3 34 3b 20 20 2f 2a 20 42 79 74 65 73 20 63 6f 6e  4;  /* Bytes con
16da4 74 65 6e 74 20 70 65 72 20 6f 76 66 6c 20 70 61  tent per ovfl pa
16da5 67 65 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e  ge */.    Pgno n
16da6 65 78 74 50 61 67 65 3b 0a 0a 20 20 20 20 6e 65  extPage;..    ne
16da7 78 74 50 61 67 65 20 3d 20 67 65 74 34 62 79 74  xtPage = get4byt
16da8 65 28 26 61 50 61 79 6c 6f 61 64 5b 70 43 75 72  e(&aPayload[pCur
16da9 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 5d 29 3b  ->info.nLocal]);
16daa 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
16dab 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20  _OMIT_INCRBLOB. 
16dac 20 20 20 2f 2a 20 49 66 20 74 68 65 20 69 73 49     /* If the isI
16dad 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 66 6c  ncrblobHandle fl
16dae 61 67 20 69 73 20 73 65 74 20 61 6e 64 20 74 68  ag is set and th
16daf 65 20 42 74 43 75 72 73 6f 72 2e 61 4f 76 65 72  e BtCursor.aOver
16db0 66 6c 6f 77 5b 5d 0a 20 20 20 20 2a 2a 20 68 61  flow[].    ** ha
16db1 73 20 6e 6f 74 20 62 65 65 6e 20 61 6c 6c 6f 63  s not been alloc
16db2 61 74 65 64 2c 20 61 6c 6c 6f 63 61 74 65 20 69  ated, allocate i
16db3 74 20 6e 6f 77 2e 20 54 68 65 20 61 72 72 61 79  t now. The array
16db4 20 69 73 20 73 69 7a 65 64 20 61 74 0a 20 20 20   is sized at.   
16db5 20 2a 2a 20 6f 6e 65 20 65 6e 74 72 79 20 66 6f   ** one entry fo
16db6 72 20 65 61 63 68 20 6f 76 65 72 66 6c 6f 77 20  r each overflow 
16db7 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76 65 72  page in the over
16db8 66 6c 6f 77 20 63 68 61 69 6e 2e 20 54 68 65 0a  flow chain. The.
16db9 20 20 20 20 2a 2a 20 70 61 67 65 20 6e 75 6d 62      ** page numb
16dba 65 72 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  er of the first 
16dbb 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73  overflow page is
16dbc 20 73 74 6f 72 65 64 20 69 6e 20 61 4f 76 65 72   stored in aOver
16dbd 66 6c 6f 77 5b 30 5d 2c 0a 20 20 20 20 2a 2a 20  flow[0],.    ** 
16dbe 65 74 63 2e 20 41 20 76 61 6c 75 65 20 6f 66 20  etc. A value of 
16dbf 30 20 69 6e 20 74 68 65 20 61 4f 76 65 72 66 6c  0 in the aOverfl
16dc0 6f 77 5b 5d 20 61 72 72 61 79 20 6d 65 61 6e 73  ow[] array means
16dc1 20 22 6e 6f 74 20 79 65 74 20 6b 6e 6f 77 6e 22   "not yet known"
16dc2 0a 20 20 20 20 2a 2a 20 28 74 68 65 20 63 61 63  .    ** (the cac
16dc3 68 65 20 69 73 20 6c 61 7a 69 6c 79 20 70 6f 70  he is lazily pop
16dc4 75 6c 61 74 65 64 29 2e 0a 20 20 20 20 2a 2f 0a  ulated)..    */.
16dc5 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 73      if( pCur->is
16dc6 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 26  IncrblobHandle &
16dc7 26 20 21 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c  & !pCur->aOverfl
16dc8 6f 77 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  ow ){.      int 
16dc9 6e 4f 76 66 6c 20 3d 20 28 70 43 75 72 2d 3e 69  nOvfl = (pCur->i
16dca 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 2d 70 43 75  nfo.nPayload-pCu
16dcb 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 2b 6f  r->info.nLocal+o
16dcc 76 66 6c 53 69 7a 65 2d 31 29 2f 6f 76 66 6c 53  vflSize-1)/ovflS
16dcd 69 7a 65 3b 0a 20 20 20 20 20 20 70 43 75 72 2d  ize;.      pCur-
16dce 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20 28 50 67  >aOverflow = (Pg
16dcf 6e 6f 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c  no *)sqlite3Mall
16dd0 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66 28 50 67  ocZero(sizeof(Pg
16dd1 6e 6f 29 2a 6e 4f 76 66 6c 29 3b 0a 20 20 20 20  no)*nOvfl);.    
16dd2 20 20 2f 2a 20 6e 4f 76 66 6c 20 69 73 20 61 6c    /* nOvfl is al
16dd3 77 61 79 73 20 70 6f 73 69 74 69 76 65 2e 20 20  ways positive.  
16dd4 49 66 20 69 74 20 77 65 72 65 20 7a 65 72 6f 2c  If it were zero,
16dd5 20 66 65 74 63 68 50 61 79 6c 6f 61 64 20 77 6f   fetchPayload wo
16dd6 75 6c 64 20 68 61 76 65 0a 20 20 20 20 20 20 2a  uld have.      *
16dd7 2a 20 62 65 65 6e 20 75 73 65 64 20 69 6e 73 74  * been used inst
16dd8 65 61 64 20 6f 66 20 74 68 69 73 20 72 6f 75 74  ead of this rout
16dd9 69 6e 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66  ine. */.      if
16dda 28 20 41 4c 57 41 59 53 28 6e 4f 76 66 6c 29 20  ( ALWAYS(nOvfl) 
16ddb 26 26 20 21 70 43 75 72 2d 3e 61 4f 76 65 72 66  && !pCur->aOverf
16ddc 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72  low ){.        r
16ddd 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
16dde 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
16ddf 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6f  .    /* If the o
16de0 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73  verflow page-lis
16de1 74 20 63 61 63 68 65 20 68 61 73 20 62 65 65 6e  t cache has been
16de2 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74   allocated and t
16de3 68 65 0a 20 20 20 20 2a 2a 20 65 6e 74 72 79 20  he.    ** entry 
16de4 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 72 65  for the first re
16de5 71 75 69 72 65 64 20 6f 76 65 72 66 6c 6f 77 20  quired overflow 
16de6 70 61 67 65 20 69 73 20 76 61 6c 69 64 2c 20 73  page is valid, s
16de7 6b 69 70 0a 20 20 20 20 2a 2a 20 64 69 72 65 63  kip.    ** direc
16de8 74 6c 79 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a  tly to it..    *
16de9 2f 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e  /.    if( pCur->
16dea 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 70 43 75  aOverflow && pCu
16deb 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 6f 66 66  r->aOverflow[off
16dec 73 65 74 2f 6f 76 66 6c 53 69 7a 65 5d 20 29 7b  set/ovflSize] ){
16ded 0a 20 20 20 20 20 20 69 49 64 78 20 3d 20 28 6f  .      iIdx = (o
16dee 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65 29 3b  ffset/ovflSize);
16def 0a 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 20  .      nextPage 
16df0 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f  = pCur->aOverflo
16df1 77 5b 69 49 64 78 5d 3b 0a 20 20 20 20 20 20 6f  w[iIdx];.      o
16df2 66 66 73 65 74 20 3d 20 28 6f 66 66 73 65 74 25  ffset = (offset%
16df3 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 20 7d  ovflSize);.    }
16df4 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 66 6f 72  .#endif..    for
16df5 28 20 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  ( ; rc==SQLITE_O
16df6 4b 20 26 26 20 61 6d 74 3e 30 20 26 26 20 6e 65  K && amt>0 && ne
16df7 78 74 50 61 67 65 3b 20 69 49 64 78 2b 2b 29 7b  xtPage; iIdx++){
16df8 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
16df9 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20  _OMIT_INCRBLOB. 
16dfa 20 20 20 20 20 2f 2a 20 49 66 20 72 65 71 75 69       /* If requi
16dfb 72 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 74 68  red, populate th
16dfc 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d  e overflow page-
16dfd 6c 69 73 74 20 63 61 63 68 65 2e 20 2a 2f 0a 20  list cache. */. 
16dfe 20 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61       if( pCur->a
16dff 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20  Overflow ){.    
16e00 20 20 20 20 61 73 73 65 72 74 28 21 70 43 75 72      assert(!pCur
16e01 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78  ->aOverflow[iIdx
16e02 5d 20 7c 7c 20 70 43 75 72 2d 3e 61 4f 76 65 72  ] || pCur->aOver
16e03 66 6c 6f 77 5b 69 49 64 78 5d 3d 3d 6e 65 78 74  flow[iIdx]==next
16e04 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 70  Page);.        p
16e05 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69  Cur->aOverflow[i
16e06 49 64 78 5d 20 3d 20 6e 65 78 74 50 61 67 65 3b  Idx] = nextPage;
16e07 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
16e08 0a 20 20 20 20 20 20 69 66 28 20 6f 66 66 73 65  .      if( offse
16e09 74 3e 3d 6f 76 66 6c 53 69 7a 65 20 29 7b 0a 20  t>=ovflSize ){. 
16e0a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 6e         /* The on
16e0b 6c 79 20 72 65 61 73 6f 6e 20 74 6f 20 72 65 61  ly reason to rea
16e0c 64 20 74 68 69 73 20 70 61 67 65 20 69 73 20 74  d this page is t
16e0d 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 70 61 67  o obtain the pag
16e0e 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d  e.        ** num
16e0f 62 65 72 20 66 6f 72 20 74 68 65 20 6e 65 78 74  ber for the next
16e10 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76 65   page in the ove
16e11 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 54 68 65  rflow chain. The
16e12 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a   page.        **
16e13 20 64 61 74 61 20 69 73 20 6e 6f 74 20 72 65 71   data is not req
16e14 75 69 72 65 64 2e 20 53 6f 20 66 69 72 73 74 20  uired. So first 
16e15 74 72 79 20 74 6f 20 6c 6f 6f 6b 75 70 20 74 68  try to lookup th
16e16 65 20 6f 76 65 72 66 6c 6f 77 0a 20 20 20 20 20  e overflow.     
16e17 20 20 20 2a 2a 20 70 61 67 65 2d 6c 69 73 74 20     ** page-list 
16e18 63 61 63 68 65 2c 20 69 66 20 61 6e 79 2c 20 74  cache, if any, t
16e19 68 65 6e 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f  hen fall back to
16e1a 20 74 68 65 20 67 65 74 4f 76 65 72 66 6c 6f 77   the getOverflow
16e1b 50 61 67 65 28 29 0a 20 20 20 20 20 20 20 20 2a  Page().        *
16e1c 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20  * function..    
16e1d 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53      */.#ifndef S
16e1e 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
16e1f 4c 4f 42 0a 20 20 20 20 20 20 20 20 69 66 28 20  LOB.        if( 
16e20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20  pCur->aOverflow 
16e21 26 26 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c  && pCur->aOverfl
16e22 6f 77 5b 69 49 64 78 2b 31 5d 20 29 7b 0a 20 20  ow[iIdx+1] ){.  
16e23 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 67 65          nextPage
16e24 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c   = pCur->aOverfl
16e25 6f 77 5b 69 49 64 78 2b 31 5d 3b 0a 20 20 20 20  ow[iIdx+1];.    
16e26 20 20 20 20 7d 20 65 6c 73 65 20 0a 23 65 6e 64      } else .#end
16e27 69 66 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  if.          rc 
16e28 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67  = getOverflowPag
16e29 65 28 70 42 74 2c 20 6e 65 78 74 50 61 67 65 2c  e(pBt, nextPage,
16e2a 20 30 2c 20 26 6e 65 78 74 50 61 67 65 29 3b 0a   0, &nextPage);.
16e2b 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 2d          offset -
16e2c 3d 20 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20  = ovflSize;.    
16e2d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
16e2e 20 2f 2a 20 4e 65 65 64 20 74 6f 20 72 65 61 64   /* Need to read
16e2f 20 74 68 69 73 20 70 61 67 65 20 70 72 6f 70 65   this page prope
16e30 72 6c 79 2e 20 49 74 20 63 6f 6e 74 61 69 6e 73  rly. It contains
16e31 20 73 6f 6d 65 20 6f 66 20 74 68 65 0a 20 20 20   some of the.   
16e32 20 20 20 20 20 2a 2a 20 72 61 6e 67 65 20 6f 66       ** range of
16e33 20 64 61 74 61 20 74 68 61 74 20 69 73 20 62 65   data that is be
16e34 69 6e 67 20 72 65 61 64 20 28 65 4f 70 3d 3d 30  ing read (eOp==0
16e35 29 20 6f 72 20 77 72 69 74 74 65 6e 20 28 65 4f  ) or written (eO
16e36 70 21 3d 30 29 2e 0a 20 20 20 20 20 20 20 20 2a  p!=0)..        *
16e37 2f 0a 20 20 20 20 20 20 20 20 44 62 50 61 67 65  /.        DbPage
16e38 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 20 20 20   *pDbPage;.     
16e39 20 20 20 69 6e 74 20 61 20 3d 20 61 6d 74 3b 0a     int a = amt;.
16e3a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
16e3b 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74  ite3PagerGet(pBt
16e3c 2d 3e 70 50 61 67 65 72 2c 20 6e 65 78 74 50 61  ->pPager, nextPa
16e3d 67 65 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20  ge, &pDbPage);. 
16e3e 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
16e3f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
16e40 20 20 20 20 20 20 61 50 61 79 6c 6f 61 64 20 3d        aPayload =
16e41 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
16e42 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20  Data(pDbPage);. 
16e43 20 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 67           nextPag
16e44 65 20 3d 20 67 65 74 34 62 79 74 65 28 61 50 61  e = get4byte(aPa
16e45 79 6c 6f 61 64 29 3b 0a 20 20 20 20 20 20 20 20  yload);.        
16e46 20 20 69 66 28 20 61 20 2b 20 6f 66 66 73 65 74    if( a + offset
16e47 20 3e 20 6f 76 66 6c 53 69 7a 65 20 29 7b 0a 20   > ovflSize ){. 
16e48 20 20 20 20 20 20 20 20 20 20 20 61 20 3d 20 6f             a = o
16e49 76 66 6c 53 69 7a 65 20 2d 20 6f 66 66 73 65 74  vflSize - offset
16e4a 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
16e4b 20 20 20 20 20 20 20 20 72 63 20 3d 20 63 6f 70          rc = cop
16e4c 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79 6c 6f  yPayload(&aPaylo
16e4d 61 64 5b 6f 66 66 73 65 74 2b 34 5d 2c 20 70 42  ad[offset+4], pB
16e4e 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 44 62 50  uf, a, eOp, pDbP
16e4f 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  age);.          
16e50 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
16e51 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  f(pDbPage);.    
16e52 20 20 20 20 20 20 6f 66 66 73 65 74 20 3d 20 30        offset = 0
16e53 3b 0a 20 20 20 20 20 20 20 20 20 20 61 6d 74 20  ;.          amt 
16e54 2d 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 20 20  -= a;.          
16e55 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 20 20  pBuf += a;.     
16e56 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
16e57 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63   }.  }..  if( rc
16e58 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61  ==SQLITE_OK && a
16e59 6d 74 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 75  mt>0 ){.    retu
16e5a 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
16e5b 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65  T_BKPT;.  }.  re
16e5c 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
16e5d 2a 20 52 65 61 64 20 70 61 72 74 20 6f 66 20 74  * Read part of t
16e5e 68 65 20 6b 65 79 20 61 73 73 6f 63 69 61 74 65  he key associate
16e5f 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 70 43  d with cursor pC
16e60 75 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20  ur.  Exactly.** 
16e61 22 61 6d 74 22 20 62 79 74 65 73 20 77 69 6c 6c  "amt" bytes will
16e62 20 62 65 20 74 72 61 6e 73 66 65 72 65 64 20 69   be transfered i
16e63 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54 68 65  nto pBuf[].  The
16e64 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67   transfer.** beg
16e65 69 6e 73 20 61 74 20 22 6f 66 66 73 65 74 22 2e  ins at "offset".
16e66 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65  .**.** The calle
16e67 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68  r must ensure th
16e68 61 74 20 70 43 75 72 20 69 73 20 70 6f 69 6e 74  at pCur is point
16e69 69 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20 72  ing to a valid r
16e6a 6f 77 0a 2a 2a 20 69 6e 20 74 68 65 20 74 61 62  ow.** in the tab
16e6b 6c 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  le..**.** Return
16e6c 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75   SQLITE_OK on su
16e6d 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f  ccess or an erro
16e6e 72 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68 69  r code if anythi
16e6f 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67  ng goes.** wrong
16e70 2e 20 20 41 6e 20 65 72 72 6f 72 20 69 73 20 72  .  An error is r
16e71 65 74 75 72 6e 65 64 20 69 66 20 22 6f 66 66 73  eturned if "offs
16e72 65 74 2b 61 6d 74 22 20 69 73 20 6c 61 72 67 65  et+amt" is large
16e73 72 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76  r than.** the av
16e74 61 69 6c 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e  ailable payload.
16e75 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
16e76 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
16e77 72 65 65 4b 65 79 28 42 74 43 75 72 73 6f 72 20  reeKey(BtCursor 
16e78 2a 70 43 75 72 2c 20 75 33 32 20 6f 66 66 73 65  *pCur, u32 offse
16e79 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64  t, u32 amt, void
16e7a 20 2a 70 42 75 66 29 7b 0a 20 20 61 73 73 65 72   *pBuf){.  asser
16e7b 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
16e7c 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61  tex(pCur) );.  a
16e7d 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74  ssert( pCur->eSt
16e7e 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
16e7f 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  D );.  assert( p
16e80 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26  Cur->iPage>=0 &&
16e81 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
16e82 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20  ur->iPage] );.  
16e83 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69  assert( pCur->ai
16e84 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
16e85 3c 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43  <pCur->apPage[pC
16e86 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c  ur->iPage]->nCel
16e87 6c 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 61 63  l );.  return ac
16e88 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72  cessPayload(pCur
16e89 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 28  , offset, amt, (
16e8a 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 70  unsigned char*)p
16e8b 42 75 66 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Buf, 0);.}../*.*
16e8c 2a 20 52 65 61 64 20 70 61 72 74 20 6f 66 20 74  * Read part of t
16e8d 68 65 20 64 61 74 61 20 61 73 73 6f 63 69 61 74  he data associat
16e8e 65 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 70  ed with cursor p
16e8f 43 75 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a 2a  Cur.  Exactly.**
16e90 20 22 61 6d 74 22 20 62 79 74 65 73 20 77 69 6c   "amt" bytes wil
16e91 6c 20 62 65 20 74 72 61 6e 73 66 65 72 65 64 20  l be transfered 
16e92 69 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54 68  into pBuf[].  Th
16e93 65 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62 65  e transfer.** be
16e94 67 69 6e 73 20 61 74 20 22 6f 66 66 73 65 74 22  gins at "offset"
16e95 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
16e96 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
16e97 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20  ess or an error 
16e98 63 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67  code if anything
16e99 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20   goes.** wrong. 
16e9a 20 41 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74   An error is ret
16e9b 75 72 6e 65 64 20 69 66 20 22 6f 66 66 73 65 74  urned if "offset
16e9c 2b 61 6d 74 22 20 69 73 20 6c 61 72 67 65 72 20  +amt" is larger 
16e9d 74 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69  than.** the avai
16e9e 6c 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a  lable payload..*
16e9f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16ea0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
16ea1 65 44 61 74 61 28 42 74 43 75 72 73 6f 72 20 2a  eData(BtCursor *
16ea2 70 43 75 72 2c 20 75 33 32 20 6f 66 66 73 65 74  pCur, u32 offset
16ea3 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20  , u32 amt, void 
16ea4 2a 70 42 75 66 29 7b 0a 20 20 69 6e 74 20 72 63  *pBuf){.  int rc
16ea5 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
16ea6 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a  E_OMIT_INCRBLOB.
16ea7 20 20 69 66 20 28 20 70 43 75 72 2d 3e 65 53 74    if ( pCur->eSt
16ea8 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41  ate==CURSOR_INVA
16ea9 4c 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72  LID ){.    retur
16eaa 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a  n SQLITE_ABORT;.
16eab 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73    }.#endif..  as
16eac 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
16ead 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
16eae 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75    rc = restoreCu
16eaf 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75  rsorPosition(pCu
16eb0 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  r);.  if( rc==SQ
16eb1 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61  LITE_OK ){.    a
16eb2 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74  ssert( pCur->eSt
16eb3 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
16eb4 44 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  D );.    assert(
16eb5 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30 20   pCur->iPage>=0 
16eb6 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  && pCur->apPage[
16eb7 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a  pCur->iPage] );.
16eb8 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
16eb9 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
16eba 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50 61 67  age]<pCur->apPag
16ebb 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e  e[pCur->iPage]->
16ebc 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20  nCell );.    rc 
16ebd 3d 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28  = accessPayload(
16ebe 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d  pCur, offset, am
16ebf 74 2c 20 70 42 75 66 2c 20 30 29 3b 0a 20 20 7d  t, pBuf, 0);.  }
16ec0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
16ec1 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
16ec2 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 79 6c 6f  pointer to paylo
16ec3 61 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ad information f
16ec4 72 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68  rom the entry th
16ec5 61 74 20 74 68 65 20 0a 2a 2a 20 70 43 75 72 20  at the .** pCur 
16ec6 63 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69  cursor is pointi
16ec7 6e 67 20 74 6f 2e 20 20 54 68 65 20 70 6f 69 6e  ng to.  The poin
16ec8 74 65 72 20 69 73 20 74 6f 20 74 68 65 20 62 65  ter is to the be
16ec9 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68  ginning of.** th
16eca 65 20 6b 65 79 20 69 66 20 73 6b 69 70 4b 65 79  e key if skipKey
16ecb 3d 3d 30 20 61 6e 64 20 69 74 20 70 6f 69 6e 74  ==0 and it point
16ecc 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69  s to the beginni
16ecd 6e 67 20 6f 66 20 64 61 74 61 20 69 66 0a 2a 2a  ng of data if.**
16ece 20 73 6b 69 70 4b 65 79 3d 3d 31 2e 20 20 54 68   skipKey==1.  Th
16ecf 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
16ed0 73 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 6b  s of available k
16ed1 65 79 2f 64 61 74 61 20 69 73 20 77 72 69 74 74  ey/data is writt
16ed2 65 6e 0a 2a 2a 20 69 6e 74 6f 20 2a 70 41 6d 74  en.** into *pAmt
16ed3 2e 20 20 49 66 20 2a 70 41 6d 74 3d 3d 30 2c 20  .  If *pAmt==0, 
16ed4 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72  then the value r
16ed5 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 6e 6f 74  eturned will not
16ed6 20 62 65 0a 2a 2a 20 61 20 76 61 6c 69 64 20 70   be.** a valid p
16ed7 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ointer..**.** Th
16ed8 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 6e  is routine is an
16ed9 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20   optimization.  
16eda 49 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 66 6f 72  It is common for
16edb 20 74 68 65 20 65 6e 74 69 72 65 20 6b 65 79 0a   the entire key.
16edc 2a 2a 20 61 6e 64 20 64 61 74 61 20 74 6f 20 66  ** and data to f
16edd 69 74 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20  it on the local 
16ede 70 61 67 65 20 61 6e 64 20 66 6f 72 20 74 68 65  page and for the
16edf 72 65 20 74 6f 20 62 65 20 6e 6f 20 6f 76 65 72  re to be no over
16ee0 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 73 2e 20 20  flow.** pages.  
16ee1 57 68 65 6e 20 74 68 61 74 20 69 73 20 73 6f 2c  When that is so,
16ee2 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   this routine ca
16ee3 6e 20 62 65 20 75 73 65 64 20 74 6f 20 61 63 63  n be used to acc
16ee4 65 73 73 20 74 68 65 0a 2a 2a 20 6b 65 79 20 61  ess the.** key a
16ee5 6e 64 20 64 61 74 61 20 77 69 74 68 6f 75 74 20  nd data without 
16ee6 6d 61 6b 69 6e 67 20 61 20 63 6f 70 79 2e 20 20  making a copy.  
16ee7 49 66 20 74 68 65 20 6b 65 79 20 61 6e 64 2f 6f  If the key and/o
16ee8 72 20 64 61 74 61 20 73 70 69 6c 6c 73 0a 2a 2a  r data spills.**
16ee9 20 6f 6e 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70   onto overflow p
16eea 61 67 65 73 2c 20 74 68 65 6e 20 61 63 63 65 73  ages, then acces
16eeb 73 50 61 79 6c 6f 61 64 28 29 20 6d 75 73 74 20  sPayload() must 
16eec 62 65 20 75 73 65 64 20 74 6f 20 72 65 61 73 73  be used to reass
16eed 65 6d 62 6c 65 0a 2a 2a 20 74 68 65 20 6b 65 79  emble.** the key
16eee 2f 64 61 74 61 20 61 6e 64 20 63 6f 70 79 20 69  /data and copy i
16eef 74 20 69 6e 74 6f 20 61 20 70 72 65 61 6c 6c 6f  t into a preallo
16ef0 63 61 74 65 64 20 62 75 66 66 65 72 2e 0a 2a 2a  cated buffer..**
16ef1 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20  .** The pointer 
16ef2 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73  returned by this
16ef3 20 72 6f 75 74 69 6e 65 20 6c 6f 6f 6b 73 20 64   routine looks d
16ef4 69 72 65 63 74 6c 79 20 69 6e 74 6f 20 74 68 65  irectly into the
16ef5 20 63 61 63 68 65 64 0a 2a 2a 20 70 61 67 65 20   cached.** page 
16ef6 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  of the database.
16ef7 20 20 54 68 65 20 64 61 74 61 20 6d 69 67 68 74    The data might
16ef8 20 63 68 61 6e 67 65 20 6f 72 20 6d 6f 76 65 20   change or move 
16ef9 74 68 65 20 6e 65 78 74 20 74 69 6d 65 0a 2a 2a  the next time.**
16efa 20 61 6e 79 20 62 74 72 65 65 20 72 6f 75 74 69   any btree routi
16efb 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f  ne is called..*/
16efc 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e  .static const un
16efd 73 69 67 6e 65 64 20 63 68 61 72 20 2a 66 65 74  signed char *fet
16efe 63 68 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 43  chPayload(.  BtC
16eff 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20  ursor *pCur,    
16f00 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e    /* Cursor poin
16f01 74 69 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 6f  ting to entry to
16f02 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20   read from */.  
16f03 69 6e 74 20 2a 70 41 6d 74 2c 20 20 20 20 20 20  int *pAmt,      
16f04 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68       /* Write th
16f05 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 76 61 69  e number of avai
16f06 6c 61 62 6c 65 20 62 79 74 65 73 20 68 65 72 65  lable bytes here
16f07 20 2a 2f 0a 20 20 69 6e 74 20 73 6b 69 70 4b 65   */.  int skipKe
16f08 79 20 20 20 20 20 20 20 20 20 20 2f 2a 20 72 65  y          /* re
16f09 61 64 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20  ad beginning at 
16f0a 64 61 74 61 20 69 66 20 74 68 69 73 20 69 73 20  data if this is 
16f0b 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 73  true */.){.  uns
16f0c 69 67 6e 65 64 20 63 68 61 72 20 2a 61 50 61 79  igned char *aPay
16f0d 6c 6f 61 64 3b 0a 20 20 4d 65 6d 50 61 67 65 20  load;.  MemPage 
16f0e 2a 70 50 61 67 65 3b 0a 20 20 75 33 32 20 6e 4b  *pPage;.  u32 nK
16f0f 65 79 3b 0a 20 20 75 33 32 20 6e 4c 6f 63 61 6c  ey;.  u32 nLocal
16f10 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  ;..  assert( pCu
16f11 72 21 3d 30 20 26 26 20 70 43 75 72 2d 3e 69 50  r!=0 && pCur->iP
16f12 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e  age>=0 && pCur->
16f13 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
16f14 67 65 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ge]);.  assert( 
16f15 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
16f16 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20  RSOR_VALID );.  
16f17 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
16f18 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
16f19 3b 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72  ;.  pPage = pCur
16f1a 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
16f1b 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28  Page];.  assert(
16f1c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
16f1d 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61 67 65 2d  r->iPage]<pPage-
16f1e 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 69 66 28 20  >nCell );.  if( 
16f1f 4e 45 56 45 52 28 70 43 75 72 2d 3e 69 6e 66 6f  NEVER(pCur->info
16f20 2e 6e 53 69 7a 65 3d 3d 30 29 20 29 7b 0a 20 20  .nSize==0) ){.  
16f21 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c    btreeParseCell
16f22 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43  (pCur->apPage[pC
16f23 75 72 2d 3e 69 50 61 67 65 5d 2c 20 70 43 75 72  ur->iPage], pCur
16f24 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
16f25 61 67 65 5d 2c 0a 20 20 20 20 20 20 20 20 20 20  age],.          
16f26 20 20 20 20 20 20 20 20 20 26 70 43 75 72 2d 3e           &pCur->
16f27 69 6e 66 6f 29 3b 0a 20 20 7d 0a 20 20 61 50 61  info);.  }.  aPa
16f28 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e  yload = pCur->in
16f29 66 6f 2e 70 43 65 6c 6c 3b 0a 20 20 61 50 61 79  fo.pCell;.  aPay
16f2a 6c 6f 61 64 20 2b 3d 20 70 43 75 72 2d 3e 69 6e  load += pCur->in
16f2b 66 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 69 66  fo.nHeader;.  if
16f2c 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20  ( pPage->intKey 
16f2d 29 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 30 3b  ){.    nKey = 0;
16f2e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 4b  .  }else{.    nK
16f2f 65 79 20 3d 20 28 69 6e 74 29 70 43 75 72 2d 3e  ey = (int)pCur->
16f30 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 7d 0a 20  info.nKey;.  }. 
16f31 20 69 66 28 20 73 6b 69 70 4b 65 79 20 29 7b 0a   if( skipKey ){.
16f32 20 20 20 20 61 50 61 79 6c 6f 61 64 20 2b 3d 20      aPayload += 
16f33 6e 4b 65 79 3b 0a 20 20 20 20 6e 4c 6f 63 61 6c  nKey;.    nLocal
16f34 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c   = pCur->info.nL
16f35 6f 63 61 6c 20 2d 20 6e 4b 65 79 3b 0a 20 20 7d  ocal - nKey;.  }
16f36 65 6c 73 65 7b 0a 20 20 20 20 6e 4c 6f 63 61 6c  else{.    nLocal
16f37 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c   = pCur->info.nL
16f38 6f 63 61 6c 3b 0a 20 20 20 20 61 73 73 65 72 74  ocal;.    assert
16f39 28 20 6e 4c 6f 63 61 6c 3c 3d 6e 4b 65 79 20 29  ( nLocal<=nKey )
16f3a 3b 0a 20 20 7d 0a 20 20 2a 70 41 6d 74 20 3d 20  ;.  }.  *pAmt = 
16f3b 6e 4c 6f 63 61 6c 3b 0a 20 20 72 65 74 75 72 6e  nLocal;.  return
16f3c 20 61 50 61 79 6c 6f 61 64 3b 0a 7d 0a 0a 0a 2f   aPayload;.}.../
16f3d 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 65 6e 74  *.** For the ent
16f3e 72 79 20 74 68 61 74 20 63 75 72 73 6f 72 20 70  ry that cursor p
16f3f 43 75 72 20 69 73 20 70 6f 69 6e 74 20 74 6f 2c  Cur is point to,
16f40 20 72 65 74 75 72 6e 20 61 73 0a 2a 2a 20 6d 61   return as.** ma
16f41 6e 79 20 62 79 74 65 73 20 6f 66 20 74 68 65 20  ny bytes of the 
16f42 6b 65 79 20 6f 72 20 64 61 74 61 20 61 73 20 61  key or data as a
16f43 72 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20  re available on 
16f44 74 68 65 20 6c 6f 63 61 6c 0a 2a 2a 20 62 2d 74  the local.** b-t
16f45 72 65 65 20 70 61 67 65 2e 20 20 57 72 69 74 65  ree page.  Write
16f46 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61   the number of a
16f47 76 61 69 6c 61 62 6c 65 20 62 79 74 65 73 20 69  vailable bytes i
16f48 6e 74 6f 20 2a 70 41 6d 74 2e 0a 2a 2a 0a 2a 2a  nto *pAmt..**.**
16f49 20 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74   The pointer ret
16f4a 75 72 6e 65 64 20 69 73 20 65 70 68 65 6d 65 72  urned is ephemer
16f4b 61 6c 2e 20 20 54 68 65 20 6b 65 79 2f 64 61 74  al.  The key/dat
16f4c 61 20 6d 61 79 20 6d 6f 76 65 0a 2a 2a 20 6f 72  a may move.** or
16f4d 20 62 65 20 64 65 73 74 72 6f 79 65 64 20 6f 6e   be destroyed on
16f4e 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74   the next call t
16f4f 6f 20 61 6e 79 20 42 74 72 65 65 20 72 6f 75 74  o any Btree rout
16f50 69 6e 65 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e  ine,.** includin
16f51 67 20 63 61 6c 6c 73 20 66 72 6f 6d 20 6f 74 68  g calls from oth
16f52 65 72 20 74 68 72 65 61 64 73 20 61 67 61 69 6e  er threads again
16f53 73 74 20 74 68 65 20 73 61 6d 65 20 63 61 63 68  st the same cach
16f54 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6d  e..** Hence, a m
16f55 75 74 65 78 20 6f 6e 20 74 68 65 20 42 74 53 68  utex on the BtSh
16f56 61 72 65 64 20 73 68 6f 75 6c 64 20 62 65 20 68  ared should be h
16f57 65 6c 64 20 70 72 69 6f 72 20 74 6f 20 63 61 6c  eld prior to cal
16f58 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72 6f 75  ling.** this rou
16f59 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  tine..**.** Thes
16f5a 65 20 72 6f 75 74 69 6e 65 73 20 69 73 20 75 73  e routines is us
16f5b 65 64 20 74 6f 20 67 65 74 20 71 75 69 63 6b 20  ed to get quick 
16f5c 61 63 63 65 73 73 20 74 6f 20 6b 65 79 20 61 6e  access to key an
16f5d 64 20 64 61 74 61 0a 2a 2a 20 69 6e 20 74 68 65  d data.** in the
16f5e 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65   common case whe
16f5f 72 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 20 70  re no overflow p
16f60 61 67 65 73 20 61 72 65 20 75 73 65 64 2e 0a 2a  ages are used..*
16f61 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16f62 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
16f63 69 74 65 33 42 74 72 65 65 4b 65 79 46 65 74 63  ite3BtreeKeyFetc
16f64 68 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  h(BtCursor *pCur
16f65 2c 20 69 6e 74 20 2a 70 41 6d 74 29 7b 0a 20 20  , int *pAmt){.  
16f66 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 20 3d 20  const void *p = 
16f67 30 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  0;.  assert( sql
16f68 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
16f69 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62  pCur->pBtree->db
16f6a 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
16f6b 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
16f6c 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
16f6d 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 43 75    if( ALWAYS(pCu
16f6e 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
16f6f 52 5f 56 41 4c 49 44 29 20 29 7b 0a 20 20 20 20  R_VALID) ){.    
16f70 70 20 3d 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a  p = (const void*
16f71 29 66 65 74 63 68 50 61 79 6c 6f 61 64 28 70 43  )fetchPayload(pC
16f72 75 72 2c 20 70 41 6d 74 2c 20 30 29 3b 0a 20 20  ur, pAmt, 0);.  
16f73 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
16f74 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
16f75 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
16f76 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68  e3BtreeDataFetch
16f77 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c  (BtCursor *pCur,
16f78 20 69 6e 74 20 2a 70 41 6d 74 29 7b 0a 20 20 63   int *pAmt){.  c
16f79 6f 6e 73 74 20 76 6f 69 64 20 2a 70 20 3d 20 30  onst void *p = 0
16f7a 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
16f7b 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
16f7c 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d  Cur->pBtree->db-
16f7d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
16f7e 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
16f7f 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
16f80 20 69 66 28 20 41 4c 57 41 59 53 28 70 43 75 72   if( ALWAYS(pCur
16f81 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
16f82 5f 56 41 4c 49 44 29 20 29 7b 0a 20 20 20 20 70  _VALID) ){.    p
16f83 20 3d 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 29   = (const void*)
16f84 66 65 74 63 68 50 61 79 6c 6f 61 64 28 70 43 75  fetchPayload(pCu
16f85 72 2c 20 70 41 6d 74 2c 20 31 29 3b 0a 20 20 7d  r, pAmt, 1);.  }
16f86 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
16f87 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20  ./*.** Move the 
16f88 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f 20 61  cursor down to a
16f89 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 2e   new child page.
16f8a 20 20 54 68 65 20 6e 65 77 50 67 6e 6f 20 61 72    The newPgno ar
16f8b 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a  gument is the.**
16f8c 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   page number of 
16f8d 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 74  the child page t
16f8e 6f 20 6d 6f 76 65 20 74 6f 2e 0a 2a 2a 0a 2a 2a  o move to..**.**
16f8f 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   This function r
16f90 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 43 4f  eturns SQLITE_CO
16f91 52 52 55 50 54 20 69 66 20 74 68 65 20 70 61 67  RRUPT if the pag
16f92 65 2d 68 65 61 64 65 72 20 66 6c 61 67 73 20 66  e-header flags f
16f93 69 65 6c 64 20 6f 66 0a 2a 2a 20 74 68 65 20 6e  ield of.** the n
16f94 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 64 6f  ew child page do
16f95 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65  es not match the
16f96 20 66 6c 61 67 73 20 66 69 65 6c 64 20 6f 66 20   flags field of 
16f97 74 68 65 20 70 61 72 65 6e 74 20 28 69 2e 65 2e  the parent (i.e.
16f98 0a 2a 2a 20 69 66 20 61 6e 20 69 6e 74 6b 65 79  .** if an intkey
16f99 20 70 61 67 65 20 61 70 70 65 61 72 73 20 74 6f   page appears to
16f9a 20 62 65 20 74 68 65 20 70 61 72 65 6e 74 20 6f   be the parent o
16f9b 66 20 61 20 6e 6f 6e 2d 69 6e 74 6b 65 79 20 70  f a non-intkey p
16f9c 61 67 65 2c 20 6f 72 0a 2a 2a 20 76 69 63 65 2d  age, or.** vice-
16f9d 76 65 72 73 61 29 2e 0a 2a 2f 0a 73 74 61 74 69  versa)..*/.stati
16f9e 63 20 69 6e 74 20 6d 6f 76 65 54 6f 43 68 69 6c  c int moveToChil
16f9f 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  d(BtCursor *pCur
16fa0 2c 20 75 33 32 20 6e 65 77 50 67 6e 6f 29 7b 0a  , u32 newPgno){.
16fa1 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20    int rc;.  int 
16fa2 69 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b  i = pCur->iPage;
16fa3 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77  .  MemPage *pNew
16fa4 50 61 67 65 3b 0a 20 20 42 74 53 68 61 72 65 64  Page;.  BtShared
16fa5 20 2a 70 42 74 20 3d 20 70 43 75 72 2d 3e 70 42   *pBt = pCur->pB
16fa6 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75  t;..  assert( cu
16fa7 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
16fa8 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
16fa9 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
16faa 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a  CURSOR_VALID );.
16fab 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
16fac 69 50 61 67 65 3c 42 54 43 55 52 53 4f 52 5f 4d  iPage<BTCURSOR_M
16fad 41 58 5f 44 45 50 54 48 20 29 3b 0a 20 20 69 66  AX_DEPTH );.  if
16fae 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 28  ( pCur->iPage>=(
16faf 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50  BTCURSOR_MAX_DEP
16fb0 54 48 2d 31 29 20 29 7b 0a 20 20 20 20 72 65 74  TH-1) ){.    ret
16fb1 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
16fb2 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72  PT_BKPT;.  }.  r
16fb3 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61  c = getAndInitPa
16fb4 67 65 28 70 42 74 2c 20 6e 65 77 50 67 6e 6f 2c  ge(pBt, newPgno,
16fb5 20 26 70 4e 65 77 50 61 67 65 29 3b 0a 20 20 69   &pNewPage);.  i
16fb6 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
16fb7 63 3b 0a 20 20 70 43 75 72 2d 3e 61 70 50 61 67  c;.  pCur->apPag
16fb8 65 5b 69 2b 31 5d 20 3d 20 70 4e 65 77 50 61 67  e[i+1] = pNewPag
16fb9 65 3b 0a 20 20 70 43 75 72 2d 3e 61 69 49 64 78  e;.  pCur->aiIdx
16fba 5b 69 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 43 75  [i+1] = 0;.  pCu
16fbb 72 2d 3e 69 50 61 67 65 2b 2b 3b 0a 0a 20 20 70  r->iPage++;..  p
16fbc 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20  Cur->info.nSize 
16fbd 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c  = 0;.  pCur->val
16fbe 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66  idNKey = 0;.  if
16fbf 28 20 70 4e 65 77 50 61 67 65 2d 3e 6e 43 65 6c  ( pNewPage->nCel
16fc0 6c 3c 31 20 7c 7c 20 70 4e 65 77 50 61 67 65 2d  l<1 || pNewPage-
16fc1 3e 69 6e 74 4b 65 79 21 3d 70 43 75 72 2d 3e 61  >intKey!=pCur->a
16fc2 70 50 61 67 65 5b 69 5d 2d 3e 69 6e 74 4b 65 79  pPage[i]->intKey
16fc3 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
16fc4 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
16fc5 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  PT;.  }.  return
16fc6 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23   SQLITE_OK;.}..#
16fc7 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a  ifndef NDEBUG./*
16fc8 0a 2a 2a 20 50 61 67 65 20 70 50 61 72 65 6e 74  .** Page pParent
16fc9 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20   is an internal 
16fca 28 6e 6f 6e 2d 6c 65 61 66 29 20 74 72 65 65 20  (non-leaf) tree 
16fcb 70 61 67 65 2e 20 54 68 69 73 20 66 75 6e 63 74  page. This funct
16fcc 69 6f 6e 20 0a 2a 2a 20 61 73 73 65 72 74 73 20  ion .** asserts 
16fcd 74 68 61 74 20 70 61 67 65 20 6e 75 6d 62 65 72  that page number
16fce 20 69 43 68 69 6c 64 20 69 73 20 74 68 65 20 6c   iChild is the l
16fcf 65 66 74 2d 63 68 69 6c 64 20 69 66 20 74 68 65  eft-child if the
16fd0 20 69 49 64 78 27 74 68 0a 2a 2a 20 63 65 6c 6c   iIdx'th.** cell
16fd1 20 69 6e 20 70 61 67 65 20 70 50 61 72 65 6e 74   in page pParent
16fd2 2e 20 4f 72 2c 20 69 66 20 69 49 64 78 20 69 73  . Or, if iIdx is
16fd3 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 74 6f   equal to the to
16fd4 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  tal number of.**
16fd5 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e   cells in pParen
16fd6 74 2c 20 74 68 61 74 20 70 61 67 65 20 6e 75 6d  t, that page num
16fd7 62 65 72 20 69 43 68 69 6c 64 20 69 73 20 74 68  ber iChild is th
16fd8 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66  e right-child of
16fd9 0a 2a 2a 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f  .** the page..*/
16fda 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 73  .static void ass
16fdb 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 4d  ertParentIndex(M
16fdc 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 2c  emPage *pParent,
16fdd 20 69 6e 74 20 69 49 64 78 2c 20 50 67 6e 6f 20   int iIdx, Pgno 
16fde 69 43 68 69 6c 64 29 7b 0a 20 20 61 73 73 65 72  iChild){.  asser
16fdf 74 28 20 69 49 64 78 3c 3d 70 50 61 72 65 6e 74  t( iIdx<=pParent
16fe0 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 69 66 28  ->nCell );.  if(
16fe1 20 69 49 64 78 3d 3d 70 50 61 72 65 6e 74 2d 3e   iIdx==pParent->
16fe2 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 61 73 73  nCell ){.    ass
16fe3 65 72 74 28 20 67 65 74 34 62 79 74 65 28 26 70  ert( get4byte(&p
16fe4 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50  Parent->aData[pP
16fe5 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74  arent->hdrOffset
16fe6 2b 38 5d 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a  +8])==iChild );.
16fe7 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
16fe8 65 72 74 28 20 67 65 74 34 62 79 74 65 28 66 69  ert( get4byte(fi
16fe9 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20  ndCell(pParent, 
16fea 69 49 64 78 29 29 3d 3d 69 43 68 69 6c 64 20 29  iIdx))==iChild )
16feb 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20  ;.  }.}.#else.# 
16fec 20 64 65 66 69 6e 65 20 61 73 73 65 72 74 50 61   define assertPa
16fed 72 65 6e 74 49 6e 64 65 78 28 78 2c 79 2c 7a 29  rentIndex(x,y,z)
16fee 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20   .#endif../*.** 
16fef 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20  Move the cursor 
16ff0 75 70 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74  up to the parent
16ff1 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75   page..**.** pCu
16ff2 72 2d 3e 69 64 78 20 69 73 20 73 65 74 20 74 6f  r->idx is set to
16ff3 20 74 68 65 20 63 65 6c 6c 20 69 6e 64 65 78 20   the cell index 
16ff4 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
16ff5 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20  e pointer.** to 
16ff6 74 68 65 20 70 61 67 65 20 77 65 20 61 72 65 20  the page we are 
16ff7 63 6f 6d 69 6e 67 20 66 72 6f 6d 2e 20 20 49 66  coming from.  If
16ff8 20 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 20 66   we are coming f
16ff9 72 6f 6d 20 74 68 65 0a 2a 2a 20 72 69 67 68 74  rom the.** right
16ffa 2d 6d 6f 73 74 20 63 68 69 6c 64 20 70 61 67 65  -most child page
16ffb 20 74 68 65 6e 20 70 43 75 72 2d 3e 69 64 78 20   then pCur->idx 
16ffc 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 6d 6f  is set to one mo
16ffd 72 65 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 6c  re than.** the l
16ffe 61 72 67 65 73 74 20 63 65 6c 6c 20 69 6e 64 65  argest cell inde
16fff 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  x..*/.static voi
17000 64 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28 42  d moveToParent(B
17001 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
17002 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
17003 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
17004 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
17005 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
17006 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73  OR_VALID );.  as
17007 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67  sert( pCur->iPag
17008 65 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  e>0 );.  assert(
17009 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
1700a 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20  ur->iPage] );.  
1700b 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64 65  assertParentInde
1700c 78 28 0a 20 20 20 20 70 43 75 72 2d 3e 61 70 50  x(.    pCur->apP
1700d 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d  age[pCur->iPage-
1700e 31 5d 2c 20 0a 20 20 20 20 70 43 75 72 2d 3e 61  1], .    pCur->a
1700f 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
17010 2d 31 5d 2c 20 0a 20 20 20 20 70 43 75 72 2d 3e  -1], .    pCur->
17011 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
17012 67 65 5d 2d 3e 70 67 6e 6f 0a 20 20 29 3b 0a 20  ge]->pgno.  );. 
17013 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43 75   releasePage(pCu
17014 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
17015 69 50 61 67 65 5d 29 3b 0a 20 20 70 43 75 72 2d  iPage]);.  pCur-
17016 3e 69 50 61 67 65 2d 2d 3b 0a 20 20 70 43 75 72  >iPage--;.  pCur
17017 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30  ->info.nSize = 0
17018 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e  ;.  pCur->validN
17019 4b 65 79 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  Key = 0;.}../*.*
1701a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f  * Move the curso
1701b 72 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  r to point to th
1701c 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 69  e root page of i
1701d 74 73 20 62 2d 74 72 65 65 20 73 74 72 75 63 74  ts b-tree struct
1701e 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ure..**.** If th
1701f 65 20 74 61 62 6c 65 20 68 61 73 20 61 20 76 69  e table has a vi
17020 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 2c  rtual root page,
17021 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72   then the cursor
17022 20 69 73 20 6d 6f 76 65 64 20 74 6f 20 70 6f 69   is moved to poi
17023 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 76 69 72  nt.** to the vir
17024 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 69  tual root page i
17025 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 61 63  nstead of the ac
17026 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 2e 20  tual root page. 
17027 41 20 74 61 62 6c 65 20 68 61 73 20 61 0a 2a 2a  A table has a.**
17028 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61   virtual root pa
17029 67 65 20 77 68 65 6e 20 74 68 65 20 61 63 74 75  ge when the actu
1702a 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 63 6f 6e  al root page con
1702b 74 61 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 20 61  tains no cells a
1702c 6e 64 20 61 20 0a 2a 2a 20 73 69 6e 67 6c 65 20  nd a .** single 
1702d 63 68 69 6c 64 20 70 61 67 65 2e 20 54 68 69 73  child page. This
1702e 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e   can only happen
1702f 20 77 69 74 68 20 74 68 65 20 74 61 62 6c 65 20   with the table 
17030 72 6f 6f 74 65 64 20 61 74 20 70 61 67 65 20 31  rooted at page 1
17031 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62  ..**.** If the b
17032 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20  -tree structure 
17033 69 73 20 65 6d 70 74 79 2c 20 74 68 65 20 63 75  is empty, the cu
17034 72 73 6f 72 20 73 74 61 74 65 20 69 73 20 73 65  rsor state is se
17035 74 20 74 6f 20 0a 2a 2a 20 43 55 52 53 4f 52 5f  t to .** CURSOR_
17036 49 4e 56 41 4c 49 44 2e 20 4f 74 68 65 72 77 69  INVALID. Otherwi
17037 73 65 2c 20 74 68 65 20 63 75 72 73 6f 72 20 69  se, the cursor i
17038 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74  s set to point t
17039 6f 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 63  o the first.** c
1703a 65 6c 6c 20 6c 6f 63 61 74 65 64 20 6f 6e 20 74  ell located on t
1703b 68 65 20 72 6f 6f 74 20 28 6f 72 20 76 69 72 74  he root (or virt
1703c 75 61 6c 20 72 6f 6f 74 29 20 70 61 67 65 20 61  ual root) page a
1703d 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20 73 74  nd the cursor st
1703e 61 74 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f  ate.** is set to
1703f 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 2e 0a 2a   CURSOR_VALID..*
17040 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 66 75 6e  *.** If this fun
17041 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 73 75  ction returns su
17042 63 63 65 73 73 66 75 6c 6c 79 2c 20 69 74 20 6d  ccessfully, it m
17043 61 79 20 62 65 20 61 73 73 75 6d 65 64 20 74 68  ay be assumed th
17044 61 74 20 74 68 65 0a 2a 2a 20 70 61 67 65 2d 68  at the.** page-h
17045 65 61 64 65 72 20 66 6c 61 67 73 20 69 6e 64 69  eader flags indi
17046 63 61 74 65 20 74 68 61 74 20 74 68 65 20 5b 76  cate that the [v
17047 69 72 74 75 61 6c 5d 20 72 6f 6f 74 2d 70 61 67  irtual] root-pag
17048 65 20 69 73 20 74 68 65 20 65 78 70 65 63 74 65  e is the expecte
17049 64 20 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 62 2d  d .** kind of b-
1704a 74 72 65 65 20 70 61 67 65 20 28 69 2e 65 2e 20  tree page (i.e. 
1704b 69 66 20 77 68 65 6e 20 6f 70 65 6e 69 6e 67 20  if when opening 
1704c 74 68 65 20 63 75 72 73 6f 72 20 74 68 65 20 63  the cursor the c
1704d 61 6c 6c 65 72 20 64 69 64 20 6e 6f 74 0a 2a 2a  aller did not.**
1704e 20 73 70 65 63 69 66 79 20 61 20 4b 65 79 49 6e   specify a KeyIn
1704f 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 65  fo structure the
17050 20 66 6c 61 67 73 20 62 79 74 65 20 69 73 20 73   flags byte is s
17051 65 74 20 74 6f 20 30 78 30 35 20 6f 72 20 30 78  et to 0x05 or 0x
17052 30 44 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e  0D,.** indicatin
17053 67 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65  g a table b-tree
17054 2c 20 6f 72 20 69 66 20 74 68 65 20 63 61 6c 6c  , or if the call
17055 65 72 20 64 69 64 20 73 70 65 63 69 66 79 20 61  er did specify a
17056 20 4b 65 79 49 6e 66 6f 20 0a 2a 2a 20 73 74 72   KeyInfo .** str
17057 75 63 74 75 72 65 20 74 68 65 20 66 6c 61 67 73  ucture the flags
17058 20 62 79 74 65 20 69 73 20 73 65 74 20 74 6f 20   byte is set to 
17059 30 78 30 32 20 6f 72 20 30 78 30 41 2c 20 69 6e  0x02 or 0x0A, in
1705a 64 69 63 61 74 69 6e 67 20 61 6e 20 69 6e 64 65  dicating an inde
1705b 78 0a 2a 2a 20 62 2d 74 72 65 65 29 2e 0a 2a 2f  x.** b-tree)..*/
1705c 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65  .static int move
1705d 54 6f 52 6f 6f 74 28 42 74 43 75 72 73 6f 72 20  ToRoot(BtCursor 
1705e 2a 70 43 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67  *pCur){.  MemPag
1705f 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 69 6e 74 20  e *pRoot;.  int 
17060 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
17061 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75    Btree *p = pCu
17062 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53  r->pBtree;.  BtS
17063 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
17064 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  pBt;..  assert( 
17065 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
17066 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
17067 72 74 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c  rt( CURSOR_INVAL
17068 49 44 20 3c 20 43 55 52 53 4f 52 5f 52 45 51 55  ID < CURSOR_REQU
17069 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 61 73 73  IRESEEK );.  ass
1706a 65 72 74 28 20 43 55 52 53 4f 52 5f 56 41 4c 49  ert( CURSOR_VALI
1706b 44 20 20 20 3c 20 43 55 52 53 4f 52 5f 52 45 51  D   < CURSOR_REQ
1706c 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 61 73  UIRESEEK );.  as
1706d 73 65 72 74 28 20 43 55 52 53 4f 52 5f 46 41 55  sert( CURSOR_FAU
1706e 4c 54 20 20 20 3e 20 43 55 52 53 4f 52 5f 52 45  LT   > CURSOR_RE
1706f 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20 69  QUIRESEEK );.  i
17070 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3e  f( pCur->eState>
17071 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53  =CURSOR_REQUIRES
17072 45 45 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70  EEK ){.    if( p
17073 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
17074 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20  SOR_FAULT ){.   
17075 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
17076 3e 73 6b 69 70 4e 65 78 74 21 3d 53 51 4c 49 54  >skipNext!=SQLIT
17077 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 72 65  E_OK );.      re
17078 74 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e  turn pCur->skipN
17079 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ext;.    }.    s
1707a 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72  qlite3BtreeClear
1707b 43 75 72 73 6f 72 28 70 43 75 72 29 3b 0a 20 20  Cursor(pCur);.  
1707c 7d 0a 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69  }..  if( pCur->i
1707d 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20 69  Page>=0 ){.    i
1707e 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
1707f 31 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67  1; i<=pCur->iPag
17080 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72  e; i++){.      r
17081 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d  eleasePage(pCur-
17082 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20  >apPage[i]);.   
17083 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 50 61   }.    pCur->iPa
17084 67 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  ge = 0;.  }else{
17085 0a 20 20 20 20 72 63 20 3d 20 67 65 74 41 6e 64  .    rc = getAnd
17086 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 70 43  InitPage(pBt, pC
17087 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 26 70  ur->pgnoRoot, &p
17088 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 29 3b  Cur->apPage[0]);
17089 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1708a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1708b 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43  pCur->eState = C
1708c 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20  URSOR_INVALID;. 
1708d 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1708e 20 20 20 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e      }.    pCur->
1708f 69 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 20 20  iPage = 0;..    
17090 2f 2a 20 49 66 20 70 43 75 72 2d 3e 70 4b 65 79  /* If pCur->pKey
17091 49 6e 66 6f 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  Info is not NULL
17092 2c 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65  , then the calle
17093 72 20 74 68 61 74 20 6f 70 65 6e 65 64 20 74 68  r that opened th
17094 69 73 20 63 75 72 73 6f 72 0a 20 20 20 20 2a 2a  is cursor.    **
17095 20 65 78 70 65 63 74 65 64 20 74 6f 20 6f 70 65   expected to ope
17096 6e 20 69 74 20 6f 6e 20 61 6e 20 69 6e 64 65 78  n it on an index
17097 20 62 2d 74 72 65 65 2e 20 4f 74 68 65 72 77 69   b-tree. Otherwi
17098 73 65 2c 20 69 66 20 70 4b 65 79 49 6e 66 6f 20  se, if pKeyInfo 
17099 69 73 0a 20 20 20 20 2a 2a 20 4e 55 4c 4c 2c 20  is.    ** NULL, 
1709a 74 68 65 20 63 61 6c 6c 65 72 20 65 78 70 65 63  the caller expec
1709b 74 73 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65  ts a table b-tre
1709c 65 2e 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f  e. If this is no
1709d 74 20 74 68 65 20 63 61 73 65 2c 0a 20 20 20 20  t the case,.    
1709e 2a 2a 20 72 65 74 75 72 6e 20 61 6e 20 53 51 4c  ** return an SQL
1709f 49 54 45 5f 43 4f 52 52 55 50 54 20 65 72 72 6f  ITE_CORRUPT erro
170a0 72 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  r.  */.    asser
170a1 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  t( pCur->apPage[
170a2 30 5d 2d 3e 69 6e 74 4b 65 79 3d 3d 31 20 7c 7c  0]->intKey==1 ||
170a3 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d   pCur->apPage[0]
170a4 2d 3e 69 6e 74 4b 65 79 3d 3d 30 20 29 3b 0a 20  ->intKey==0 );. 
170a5 20 20 20 69 66 28 20 28 70 43 75 72 2d 3e 70 4b     if( (pCur->pK
170a6 65 79 49 6e 66 6f 3d 3d 30 29 21 3d 70 43 75 72  eyInfo==0)!=pCur
170a7 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74  ->apPage[0]->int
170a8 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 65 74  Key ){.      ret
170a9 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
170aa 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20  PT_BKPT;.    }. 
170ab 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20   }..  /* Assert 
170ac 74 68 61 74 20 74 68 65 20 72 6f 6f 74 20 70 61  that the root pa
170ad 67 65 20 69 73 20 6f 66 20 74 68 65 20 63 6f 72  ge is of the cor
170ae 72 65 63 74 20 74 79 70 65 2e 20 54 68 69 73 20  rect type. This 
170af 6d 75 73 74 20 62 65 20 74 68 65 0a 20 20 2a 2a  must be the.  **
170b0 20 63 61 73 65 20 61 73 20 74 68 65 20 63 61 6c   case as the cal
170b1 6c 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  l to this functi
170b2 6f 6e 20 74 68 61 74 20 6c 6f 61 64 65 64 20 74  on that loaded t
170b3 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 28 65 69  he root-page (ei
170b4 74 68 65 72 0a 20 20 2a 2a 20 74 68 69 73 20 63  ther.  ** this c
170b5 61 6c 6c 20 6f 72 20 61 20 70 72 65 76 69 6f 75  all or a previou
170b6 73 20 69 6e 76 6f 63 61 74 69 6f 6e 29 20 77 6f  s invocation) wo
170b7 75 6c 64 20 68 61 76 65 20 64 65 74 65 63 74 65  uld have detecte
170b8 64 20 63 6f 72 72 75 70 74 69 6f 6e 20 0a 20 20  d corruption .  
170b9 2a 2a 20 69 66 20 74 68 65 20 61 73 73 75 6d 70  ** if the assump
170ba 74 69 6f 6e 20 77 65 72 65 20 6e 6f 74 20 74 72  tion were not tr
170bb 75 65 2c 20 61 6e 64 20 69 74 20 69 73 20 6e 6f  ue, and it is no
170bc 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74  t possible for t
170bd 68 65 20 66 6c 61 67 73 20 0a 20 20 2a 2a 20 62  he flags .  ** b
170be 79 74 65 20 74 6f 20 68 61 76 65 20 62 65 65 6e  yte to have been
170bf 20 6d 6f 64 69 66 69 65 64 20 77 68 69 6c 65 20   modified while 
170c0 74 68 69 73 20 63 75 72 73 6f 72 20 69 73 20 68  this cursor is h
170c1 6f 6c 64 69 6e 67 20 61 20 72 65 66 65 72 65 6e  olding a referen
170c2 63 65 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 70  ce.  ** to the p
170c3 61 67 65 2e 20 20 2a 2f 0a 20 20 70 52 6f 6f 74  age.  */.  pRoot
170c4 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
170c5 30 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52  0];.  assert( pR
170c6 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 70 43 75 72 2d  oot->pgno==pCur-
170c7 3e 70 67 6e 6f 52 6f 6f 74 20 29 3b 0a 20 20 61  >pgnoRoot );.  a
170c8 73 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e 69 73  ssert( pRoot->is
170c9 49 6e 69 74 20 26 26 20 28 70 43 75 72 2d 3e 70  Init && (pCur->p
170ca 4b 65 79 49 6e 66 6f 3d 3d 30 29 3d 3d 70 52 6f  KeyInfo==0)==pRo
170cb 6f 74 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 0a 20  ot->intKey );.. 
170cc 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 30 5d 20   pCur->aiIdx[0] 
170cd 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66  = 0;.  pCur->inf
170ce 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70  o.nSize = 0;.  p
170cf 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 30 3b  Cur->atLast = 0;
170d0 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b  .  pCur->validNK
170d1 65 79 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70  ey = 0;..  if( p
170d2 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26  Root->nCell==0 &
170d3 26 20 21 70 52 6f 6f 74 2d 3e 6c 65 61 66 20 29  & !pRoot->leaf )
170d4 7b 0a 20 20 20 20 50 67 6e 6f 20 73 75 62 70 61  {.    Pgno subpa
170d5 67 65 3b 0a 20 20 20 20 69 66 28 20 70 52 6f 6f  ge;.    if( pRoo
170d6 74 2d 3e 70 67 6e 6f 21 3d 31 20 29 20 72 65 74  t->pgno!=1 ) ret
170d7 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
170d8 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 73 75 62  PT_BKPT;.    sub
170d9 70 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28  page = get4byte(
170da 26 70 52 6f 6f 74 2d 3e 61 44 61 74 61 5b 70 52  &pRoot->aData[pR
170db 6f 6f 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38  oot->hdrOffset+8
170dc 5d 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53  ]);.    pCur->eS
170dd 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 56 41  tate = CURSOR_VA
170de 4c 49 44 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f  LID;.    rc = mo
170df 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20  veToChild(pCur, 
170e0 73 75 62 70 61 67 65 29 3b 0a 20 20 7d 65 6c 73  subpage);.  }els
170e1 65 7b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74  e{.    pCur->eSt
170e2 61 74 65 20 3d 20 28 28 70 52 6f 6f 74 2d 3e 6e  ate = ((pRoot->n
170e3 43 65 6c 6c 3e 30 29 3f 43 55 52 53 4f 52 5f 56  Cell>0)?CURSOR_V
170e4 41 4c 49 44 3a 43 55 52 53 4f 52 5f 49 4e 56 41  ALID:CURSOR_INVA
170e5 4c 49 44 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  LID);.  }.  retu
170e6 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
170e7 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20  Move the cursor 
170e8 64 6f 77 6e 20 74 6f 20 74 68 65 20 6c 65 66 74  down to the left
170e9 2d 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72 79  -most leaf entry
170ea 20 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20   beneath the.** 
170eb 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 69  entry to which i
170ec 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70  t is currently p
170ed 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54  ointing..**.** T
170ee 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 61  he left-most lea
170ef 66 20 69 73 20 74 68 65 20 6f 6e 65 20 77 69 74  f is the one wit
170f0 68 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 6b  h the smallest k
170f1 65 79 20 2d 20 74 68 65 20 66 69 72 73 74 0a 2a  ey - the first.*
170f2 2a 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f  * in ascending o
170f3 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rder..*/.static 
170f4 69 6e 74 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f  int moveToLeftmo
170f5 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  st(BtCursor *pCu
170f6 72 29 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b  r){.  Pgno pgno;
170f7 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
170f8 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65  TE_OK;.  MemPage
170f9 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65   *pPage;..  asse
170fa 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
170fb 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
170fc 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53  assert( pCur->eS
170fd 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c  tate==CURSOR_VAL
170fe 49 44 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 72  ID );.  while( r
170ff 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
17100 21 28 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e  !(pPage = pCur->
17101 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
17102 67 65 5d 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  ge])->leaf ){.  
17103 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
17104 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
17105 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  e]<pPage->nCell 
17106 29 3b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65  );.    pgno = ge
17107 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28  t4byte(findCell(
17108 70 50 61 67 65 2c 20 70 43 75 72 2d 3e 61 69 49  pPage, pCur->aiI
17109 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29  dx[pCur->iPage])
1710a 29 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65  );.    rc = move
1710b 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 70 67  ToChild(pCur, pg
1710c 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  no);.  }.  retur
1710d 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  n rc;.}../*.** M
1710e 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 64  ove the cursor d
1710f 6f 77 6e 20 74 6f 20 74 68 65 20 72 69 67 68 74  own to the right
17110 2d 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72 79  -most leaf entry
17111 20 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20   beneath the.** 
17112 70 61 67 65 20 74 6f 20 77 68 69 63 68 20 69 74  page to which it
17113 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f   is currently po
17114 69 6e 74 69 6e 67 2e 20 20 4e 6f 74 69 63 65 20  inting.  Notice 
17115 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 0a 2a  the difference.*
17116 2a 20 62 65 74 77 65 65 6e 20 6d 6f 76 65 54 6f  * between moveTo
17117 4c 65 66 74 6d 6f 73 74 28 29 20 61 6e 64 20 6d  Leftmost() and m
17118 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 29  oveToRightmost()
17119 2e 20 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73  .  moveToLeftmos
1711a 74 28 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68 65  t().** finds the
1711b 20 6c 65 66 74 2d 6d 6f 73 74 20 65 6e 74 72 79   left-most entry
1711c 20 62 65 6e 65 61 74 68 20 74 68 65 20 2a 65 6e   beneath the *en
1711d 74 72 79 2a 20 77 68 65 72 65 61 73 20 6d 6f 76  try* whereas mov
1711e 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 29 0a 2a  eToRightmost().*
1711f 2a 20 66 69 6e 64 73 20 74 68 65 20 72 69 67 68  * finds the righ
17120 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 6e  t-most entry ben
17121 65 61 74 68 20 74 68 65 20 2a 70 61 67 65 2a 2e  eath the *page*.
17122 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 69 67 68 74  .**.** The right
17123 2d 6d 6f 73 74 20 65 6e 74 72 79 20 69 73 20 74  -most entry is t
17124 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65 20  he one with the 
17125 6c 61 72 67 65 73 74 20 6b 65 79 20 2d 20 74 68  largest key - th
17126 65 20 6c 61 73 74 0a 2a 2a 20 6b 65 79 20 69 6e  e last.** key in
17127 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72   ascending order
17128 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
17129 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28  moveToRightmost(
1712a 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
1712b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20  .  Pgno pgno;.  
1712c 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1712d 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  OK;.  MemPage *p
1712e 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73  Page = 0;..  ass
1712f 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
17130 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
17131 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65   assert( pCur->e
17132 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
17133 4c 49 44 20 29 3b 0a 20 20 77 68 69 6c 65 28 20  LID );.  while( 
17134 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
17135 20 21 28 70 50 61 67 65 20 3d 20 70 43 75 72 2d   !(pPage = pCur-
17136 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
17137 61 67 65 5d 29 2d 3e 6c 65 61 66 20 29 7b 0a 20  age])->leaf ){. 
17138 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79     pgno = get4by
17139 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61  te(&pPage->aData
1713a 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65  [pPage->hdrOffse
1713b 74 2b 38 5d 29 3b 0a 20 20 20 20 70 43 75 72 2d  t+8]);.    pCur-
1713c 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
1713d 67 65 5d 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65  ge] = pPage->nCe
1713e 6c 6c 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76  ll;.    rc = mov
1713f 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 70  eToChild(pCur, p
17140 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  gno);.  }.  if( 
17141 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
17142 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78  .    pCur->aiIdx
17143 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20  [pCur->iPage] = 
17144 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a  pPage->nCell-1;.
17145 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e      pCur->info.n
17146 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 70 43  Size = 0;.    pC
17147 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20  ur->validNKey = 
17148 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
17149 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74  rc;.}../* Move t
1714a 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65  he cursor to the
1714b 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20   first entry in 
1714c 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75  the table.  Retu
1714d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20  rn SQLITE_OK.** 
1714e 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53 65 74  on success.  Set
1714f 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66 20 74   *pRes to 0 if t
17150 68 65 20 63 75 72 73 6f 72 20 61 63 74 75 61 6c  he cursor actual
17151 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 6f 6d  ly points to som
17152 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 74  ething.** or set
17153 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66 20 74   *pRes to 1 if t
17154 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74  he table is empt
17155 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
17156 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
17157 42 74 72 65 65 46 69 72 73 74 28 42 74 43 75 72  BtreeFirst(BtCur
17158 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a  sor *pCur, int *
17159 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b  pRes){.  int rc;
1715a 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  ..  assert( curs
1715b 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
1715c 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r) );.  assert( 
1715d 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1715e 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d  ld(pCur->pBtree-
1715f 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
17160 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74   rc = moveToRoot
17161 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63  (pCur);.  if( rc
17162 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
17163 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74     if( pCur->eSt
17164 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41  ate==CURSOR_INVA
17165 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 61 73 73  LID ){.      ass
17166 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67  ert( pCur->apPag
17167 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e  e[pCur->iPage]->
17168 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  nCell==0 );.    
17169 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20    *pRes = 1;.   
1716a 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
1716b 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  K;.    }else{.  
1716c 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
1716d 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
1716e 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3e 30 20 29  Page]->nCell>0 )
1716f 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20  ;.      *pRes = 
17170 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f  0;.      rc = mo
17171 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75  veToLeftmost(pCu
17172 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  r);.    }.  }.  
17173 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
17174 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72   Move the cursor
17175 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74   to the last ent
17176 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e  ry in the table.
17177 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    Return SQLITE_
17178 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73  OK.** on success
17179 2e 20 20 53 65 74 20 2a 70 52 65 73 20 74 6f 20  .  Set *pRes to 
1717a 30 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20  0 if the cursor 
1717b 61 63 74 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20  actually points 
1717c 74 6f 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20  to something.** 
1717d 6f 72 20 73 65 74 20 2a 70 52 65 73 20 74 6f 20  or set *pRes to 
1717e 31 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69  1 if the table i
1717f 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49  s empty..*/.SQLI
17180 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
17181 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 28  qlite3BtreeLast(
17182 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
17183 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e  int *pRes){.  in
17184 74 20 72 63 3b 0a 20 0a 20 20 61 73 73 65 72 74  t rc;. .  assert
17185 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
17186 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73  ex(pCur) );.  as
17187 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
17188 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70  tex_held(pCur->p
17189 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78  Btree->db->mutex
1718a 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  ) );..  /* If th
1718b 65 20 63 75 72 73 6f 72 20 61 6c 72 65 61 64 79  e cursor already
1718c 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6c   points to the l
1718d 61 73 74 20 65 6e 74 72 79 2c 20 74 68 69 73 20  ast entry, this 
1718e 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 2a 2f 0a 20  is a no-op. */. 
1718f 20 69 66 28 20 43 55 52 53 4f 52 5f 56 41 4c 49   if( CURSOR_VALI
17190 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20  D==pCur->eState 
17191 26 26 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20  && pCur->atLast 
17192 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ){.#ifdef SQLITE
17193 5f 44 45 42 55 47 0a 20 20 20 20 2f 2a 20 54 68  _DEBUG.    /* Th
17194 69 73 20 62 6c 6f 63 6b 20 73 65 72 76 65 73 20  is block serves 
17195 74 6f 20 61 73 73 65 72 74 28 29 20 74 68 61 74  to assert() that
17196 20 74 68 65 20 63 75 72 73 6f 72 20 72 65 61 6c   the cursor real
17197 6c 79 20 64 6f 65 73 20 70 6f 69 6e 74 20 0a 20  ly does point . 
17198 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 6c 61 73     ** to the las
17199 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 62  t entry in the b
1719a 2d 74 72 65 65 2e 20 2a 2f 0a 20 20 20 20 69 6e  -tree. */.    in
1719b 74 20 69 69 3b 0a 20 20 20 20 66 6f 72 28 69 69  t ii;.    for(ii
1719c 3d 30 3b 20 69 69 3c 70 43 75 72 2d 3e 69 50 61  =0; ii<pCur->iPa
1719d 67 65 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20  ge; ii++){.     
1719e 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61   assert( pCur->a
1719f 69 49 64 78 5b 69 69 5d 3d 3d 70 43 75 72 2d 3e  iIdx[ii]==pCur->
171a0 61 70 50 61 67 65 5b 69 69 5d 2d 3e 6e 43 65 6c  apPage[ii]->nCel
171a1 6c 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  l );.    }.    a
171a2 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49  ssert( pCur->aiI
171a3 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3d  dx[pCur->iPage]=
171a4 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43  =pCur->apPage[pC
171a5 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c  ur->iPage]->nCel
171a6 6c 2d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72  l-1 );.    asser
171a7 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  t( pCur->apPage[
171a8 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6c 65  pCur->iPage]->le
171a9 61 66 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  af );.#endif.   
171aa 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
171ab 4b 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d  K;.  }..  rc = m
171ac 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b  oveToRoot(pCur);
171ad 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
171ae 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20  E_OK ){.    if( 
171af 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d  CURSOR_INVALID==
171b0 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a  pCur->eState ){.
171b1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43        assert( pC
171b2 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
171b3 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d  >iPage]->nCell==
171b4 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73  0 );.      *pRes
171b5 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 1;.    }else{
171b6 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
171b7 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
171b8 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20  SOR_VALID );.   
171b9 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20     *pRes = 0;.  
171ba 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52      rc = moveToR
171bb 69 67 68 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a  ightmost(pCur);.
171bc 20 20 20 20 20 20 70 43 75 72 2d 3e 61 74 4c 61        pCur->atLa
171bd 73 74 20 3d 20 72 63 3d 3d 53 51 4c 49 54 45 5f  st = rc==SQLITE_
171be 4f 4b 20 3f 31 3a 30 3b 0a 20 20 20 20 7d 0a 20  OK ?1:0;.    }. 
171bf 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
171c0 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63  }../* Move the c
171c1 75 72 73 6f 72 20 73 6f 20 74 68 61 74 20 69 74  ursor so that it
171c2 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 6e   points to an en
171c3 74 72 79 20 6e 65 61 72 20 74 68 65 20 6b 65 79  try near the key
171c4 20 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62   .** specified b
171c5 79 20 70 49 64 78 4b 65 79 20 6f 72 20 69 6e 74  y pIdxKey or int
171c6 4b 65 79 2e 20 20 20 52 65 74 75 72 6e 20 61 20  Key.   Return a 
171c7 73 75 63 63 65 73 73 20 63 6f 64 65 2e 0a 2a 2a  success code..**
171c8 0a 2a 2a 20 46 6f 72 20 49 4e 54 4b 45 59 20 74  .** For INTKEY t
171c9 61 62 6c 65 73 2c 20 74 68 65 20 69 6e 74 4b 65  ables, the intKe
171ca 79 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75  y parameter is u
171cb 73 65 64 2e 20 20 70 49 64 78 4b 65 79 20 0a 2a  sed.  pIdxKey .*
171cc 2a 20 6d 75 73 74 20 62 65 20 4e 55 4c 4c 2e 20  * must be NULL. 
171cd 20 46 6f 72 20 69 6e 64 65 78 20 74 61 62 6c 65   For index table
171ce 73 2c 20 70 49 64 78 4b 65 79 20 69 73 20 75 73  s, pIdxKey is us
171cf 65 64 20 61 6e 64 20 69 6e 74 4b 65 79 0a 2a 2a  ed and intKey.**
171d0 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a   is ignored..**.
171d1 2a 2a 20 49 66 20 61 6e 20 65 78 61 63 74 20 6d  ** If an exact m
171d2 61 74 63 68 20 69 73 20 6e 6f 74 20 66 6f 75 6e  atch is not foun
171d3 64 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73  d, then the curs
171d4 6f 72 20 69 73 20 61 6c 77 61 79 73 0a 2a 2a 20  or is always.** 
171d5 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74  left pointing at
171d6 20 61 20 6c 65 61 66 20 70 61 67 65 20 77 68 69   a leaf page whi
171d7 63 68 20 77 6f 75 6c 64 20 68 6f 6c 64 20 74 68  ch would hold th
171d8 65 20 65 6e 74 72 79 20 69 66 20 69 74 0a 2a 2a  e entry if it.**
171d9 20 77 65 72 65 20 70 72 65 73 65 6e 74 2e 20 20   were present.  
171da 54 68 65 20 63 75 72 73 6f 72 20 6d 69 67 68 74  The cursor might
171db 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 65 6e 74   point to an ent
171dc 72 79 20 74 68 61 74 20 63 6f 6d 65 73 0a 2a 2a  ry that comes.**
171dd 20 62 65 66 6f 72 65 20 6f 72 20 61 66 74 65 72   before or after
171de 20 74 68 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20   the key..**.** 
171df 41 6e 20 69 6e 74 65 67 65 72 20 69 73 20 77 72  An integer is wr
171e0 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 52 65 73  itten into *pRes
171e1 20 77 68 69 63 68 20 69 73 20 74 68 65 20 72 65   which is the re
171e2 73 75 6c 74 20 6f 66 0a 2a 2a 20 63 6f 6d 70 61  sult of.** compa
171e3 72 69 6e 67 20 74 68 65 20 6b 65 79 20 77 69 74  ring the key wit
171e4 68 20 74 68 65 20 65 6e 74 72 79 20 74 6f 20 77  h the entry to w
171e5 68 69 63 68 20 74 68 65 20 63 75 72 73 6f 72 20  hich the cursor 
171e6 69 73 20 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 2e  is .** pointing.
171e7 20 20 54 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66    The meaning of
171e8 20 74 68 65 20 69 6e 74 65 67 65 72 20 77 72 69   the integer wri
171e9 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20 2a 70 52  tten into.** *pR
171ea 65 73 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  es is as follows
171eb 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65  :.**.**     *pRe
171ec 73 3c 30 20 20 20 20 20 20 54 68 65 20 63 75 72  s<0      The cur
171ed 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e  sor is left poin
171ee 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79  ting at an entry
171ef 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20   that.**        
171f0 20 20 20 20 20 20 20 20 20 20 69 73 20 73 6d 61            is sma
171f1 6c 6c 65 72 20 74 68 61 6e 20 69 6e 74 4b 65 79  ller than intKey
171f2 2f 70 49 64 78 4b 65 79 20 6f 72 20 69 66 20 74  /pIdxKey or if t
171f3 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74  he table is empt
171f4 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  y.**            
171f5 20 20 20 20 20 20 61 6e 64 20 74 68 65 20 63 75        and the cu
171f6 72 73 6f 72 20 69 73 20 74 68 65 72 65 66 6f 72  rsor is therefor
171f7 65 20 6c 65 66 74 20 70 6f 69 6e 74 20 74 6f 20  e left point to 
171f8 6e 6f 74 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20  nothing..**.**  
171f9 20 20 20 2a 70 52 65 73 3d 3d 30 20 20 20 20 20     *pRes==0     
171fa 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65  The cursor is le
171fb 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61  ft pointing at a
171fc 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20  n entry that.** 
171fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
171fe 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65 73   exactly matches
171ff 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79 2e   intKey/pIdxKey.
17200 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73  .**.**     *pRes
17201 3e 30 20 20 20 20 20 20 54 68 65 20 63 75 72 73  >0      The curs
17202 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74  or is left point
17203 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20  ing at an entry 
17204 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  that.**         
17205 20 20 20 20 20 20 20 20 20 69 73 20 6c 61 72 67           is larg
17206 65 72 20 74 68 61 6e 20 69 6e 74 4b 65 79 2f 70  er than intKey/p
17207 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a 2f 0a 53 51  IdxKey..**.*/.SQ
17208 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
17209 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
1720a 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20 20 42  etoUnpacked(.  B
1720b 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20  tCursor *pCur,  
1720c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63          /* The c
1720d 75 72 73 6f 72 20 74 6f 20 62 65 20 6d 6f 76 65  ursor to be move
1720e 64 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52  d */.  UnpackedR
1720f 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 2c 20  ecord *pIdxKey, 
17210 2f 2a 20 55 6e 70 61 63 6b 65 64 20 69 6e 64 65  /* Unpacked inde
17211 78 20 6b 65 79 20 2a 2f 0a 20 20 69 36 34 20 69  x key */.  i64 i
17212 6e 74 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20  ntKey,          
17213 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
17214 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 62 69   key */.  int bi
17215 61 73 52 69 67 68 74 2c 20 20 20 20 20 20 20 20  asRight,        
17216 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 62     /* If true, b
17217 69 61 73 20 74 68 65 20 73 65 61 72 63 68 20 74  ias the search t
17218 6f 20 74 68 65 20 68 69 67 68 20 65 6e 64 20 2a  o the high end *
17219 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 20 20 20  /.  int *pRes   
1721a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1721b 57 72 69 74 65 20 73 65 61 72 63 68 20 72 65 73  Write search res
1721c 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  ults here */.){.
1721d 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73    int rc;..  ass
1721e 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
1721f 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
17220 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
17221 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72  _mutex_held(pCur
17222 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75  ->pBtree->db->mu
17223 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
17224 28 20 70 52 65 73 20 29 3b 0a 20 20 61 73 73 65  ( pRes );.  asse
17225 72 74 28 20 28 70 49 64 78 4b 65 79 3d 3d 30 29  rt( (pIdxKey==0)
17226 3d 3d 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66  ==(pCur->pKeyInf
17227 6f 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49  o==0) );..  /* I
17228 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  f the cursor is 
17229 61 6c 72 65 61 64 79 20 70 6f 73 69 74 69 6f 6e  already position
1722a 65 64 20 61 74 20 74 68 65 20 70 6f 69 6e 74 20  ed at the point 
1722b 77 65 20 61 72 65 20 74 72 79 69 6e 67 0a 20 20  we are trying.  
1722c 2a 2a 20 74 6f 20 6d 6f 76 65 20 74 6f 2c 20 74  ** to move to, t
1722d 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72 6e 20  hen just return 
1722e 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e  without doing an
1722f 79 20 77 6f 72 6b 20 2a 2f 0a 20 20 69 66 28 20  y work */.  if( 
17230 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
17231 52 53 4f 52 5f 56 41 4c 49 44 20 26 26 20 70 43  RSOR_VALID && pC
17232 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 0a 20  ur->validNKey . 
17233 20 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67    && pCur->apPag
17234 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 0a 20 20  e[0]->intKey .  
17235 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d  ){.    if( pCur-
17236 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 6e 74 4b  >info.nKey==intK
17237 65 79 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 65  ey ){.      *pRe
17238 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74  s = 0;.      ret
17239 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
1723a 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 43 75     }.    if( pCu
1723b 72 2d 3e 61 74 4c 61 73 74 20 26 26 20 70 43 75  r->atLast && pCu
1723c 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3c 69 6e 74  r->info.nKey<int
1723d 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 2a 70 52  Key ){.      *pR
1723e 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72  es = -1;.      r
1723f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
17240 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 63  .    }.  }..  rc
17241 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43   = moveToRoot(pC
17242 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b  ur);.  if( rc ){
17243 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
17244 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43    }.  assert( pC
17245 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
17246 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73 73  >iPage] );.  ass
17247 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67  ert( pCur->apPag
17248 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e  e[pCur->iPage]->
17249 69 73 49 6e 69 74 20 29 3b 0a 20 20 61 73 73 65  isInit );.  asse
1724a 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65  rt( pCur->apPage
1724b 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e  [pCur->iPage]->n
1724c 43 65 6c 6c 3e 30 20 7c 7c 20 70 43 75 72 2d 3e  Cell>0 || pCur->
1724d 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49  eState==CURSOR_I
1724e 4e 56 41 4c 49 44 20 29 3b 0a 20 20 69 66 28 20  NVALID );.  if( 
1724f 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
17250 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a  RSOR_INVALID ){.
17251 20 20 20 20 2a 70 52 65 73 20 3d 20 2d 31 3b 0a      *pRes = -1;.
17252 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
17253 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
17254 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20  Page]->nCell==0 
17255 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
17256 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61  LITE_OK;.  }.  a
17257 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50  ssert( pCur->apP
17258 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c  age[0]->intKey |
17259 7c 20 70 49 64 78 4b 65 79 20 29 3b 0a 20 20 66  | pIdxKey );.  f
1725a 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 69 6e 74 20  or(;;){.    int 
1725b 6c 77 72 2c 20 75 70 72 3b 0a 20 20 20 20 50 67  lwr, upr;.    Pg
1725c 6e 6f 20 63 68 6c 64 50 67 3b 0a 20 20 20 20 4d  no chldPg;.    M
1725d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20  emPage *pPage = 
1725e 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
1725f 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69  r->iPage];.    i
17260 6e 74 20 63 3b 0a 0a 20 20 20 20 2f 2a 20 70 50  nt c;..    /* pP
17261 61 67 65 2d 3e 6e 43 65 6c 6c 20 6d 75 73 74 20  age->nCell must 
17262 62 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  be greater than 
17263 7a 65 72 6f 2e 20 49 66 20 74 68 69 73 20 69 73  zero. If this is
17264 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 0a 20   the root-page. 
17265 20 20 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72     ** the cursor
17266 20 77 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e   would have been
17267 20 49 4e 56 41 4c 49 44 20 61 62 6f 76 65 20 61   INVALID above a
17268 6e 64 20 74 68 69 73 20 66 6f 72 28 3b 3b 29 20  nd this for(;;) 
17269 6c 6f 6f 70 0a 20 20 20 20 2a 2a 20 6e 6f 74 20  loop.    ** not 
1726a 72 75 6e 2e 20 49 66 20 74 68 69 73 20 69 73 20  run. If this is 
1726b 6e 6f 74 20 74 68 65 20 72 6f 6f 74 2d 70 61 67  not the root-pag
1726c 65 2c 20 74 68 65 6e 20 74 68 65 20 6d 6f 76 65  e, then the move
1726d 54 6f 43 68 69 6c 64 28 29 20 72 6f 75 74 69 6e  ToChild() routin
1726e 65 0a 20 20 20 20 2a 2a 20 77 6f 75 6c 64 20 68  e.    ** would h
1726f 61 76 65 20 61 6c 72 65 61 64 79 20 64 65 74 65  ave already dete
17270 63 74 65 64 20 64 62 20 63 6f 72 72 75 70 74 69  cted db corrupti
17271 6f 6e 2e 20 53 69 6d 69 6c 61 72 6c 79 2c 20 70  on. Similarly, p
17272 50 61 67 65 20 6d 75 73 74 0a 20 20 20 20 2a 2a  Page must.    **
17273 20 62 65 20 74 68 65 20 72 69 67 68 74 20 6b 69   be the right ki
17274 6e 64 20 28 69 6e 64 65 78 20 6f 72 20 74 61 62  nd (index or tab
17275 6c 65 29 20 6f 66 20 62 2d 74 72 65 65 20 70 61  le) of b-tree pa
17276 67 65 2e 20 4f 74 68 65 72 77 69 73 65 0a 20 20  ge. Otherwise.  
17277 20 20 2a 2a 20 61 20 6d 6f 76 65 54 6f 43 68 69    ** a moveToChi
17278 6c 64 28 29 20 6f 72 20 6d 6f 76 65 54 6f 52 6f  ld() or moveToRo
17279 6f 74 28 29 20 63 61 6c 6c 20 77 6f 75 6c 64 20  ot() call would 
1727a 68 61 76 65 20 64 65 74 65 63 74 65 64 20 63 6f  have detected co
1727b 72 72 75 70 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20  rruption.  */.  
1727c 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
1727d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20  >nCell>0 );.    
1727e 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69  assert( pPage->i
1727f 6e 74 4b 65 79 3d 3d 28 70 49 64 78 4b 65 79 3d  ntKey==(pIdxKey=
17280 3d 30 29 20 29 3b 0a 20 20 20 20 6c 77 72 20 3d  =0) );.    lwr =
17281 20 30 3b 0a 20 20 20 20 75 70 72 20 3d 20 70 50   0;.    upr = pP
17282 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20  age->nCell-1;.  
17283 20 20 69 66 28 20 62 69 61 73 52 69 67 68 74 20    if( biasRight 
17284 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61  ){.      pCur->a
17285 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
17286 5d 20 3d 20 28 75 31 36 29 75 70 72 3b 0a 20 20  ] = (u16)upr;.  
17287 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
17288 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
17289 3e 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 28  >iPage] = (u16)(
1728a 28 75 70 72 2b 6c 77 72 29 2f 32 29 3b 0a 20 20  (upr+lwr)/2);.  
1728b 20 20 7d 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b    }.    for(;;){
1728c 0a 20 20 20 20 20 20 69 6e 74 20 69 64 78 20 3d  .      int idx =
1728d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
1728e 72 2d 3e 69 50 61 67 65 5d 3b 20 2f 2a 20 49 6e  r->iPage]; /* In
1728f 64 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20 63  dex of current c
17290 65 6c 6c 20 69 6e 20 70 50 61 67 65 20 2a 2f 0a  ell in pPage */.
17291 20 20 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b        u8 *pCell;
17292 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17293 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
17294 6e 74 65 72 20 74 6f 20 63 75 72 72 65 6e 74 20  nter to current 
17295 63 65 6c 6c 20 69 6e 20 70 50 61 67 65 20 2a 2f  cell in pPage */
17296 0a 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e  ..      pCur->in
17297 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
17298 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64      pCell = find
17299 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29  Cell(pPage, idx)
1729a 20 2b 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50   + pPage->childP
1729b 74 72 53 69 7a 65 3b 0a 20 20 20 20 20 20 69 66  trSize;.      if
1729c 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20  ( pPage->intKey 
1729d 29 7b 0a 20 20 20 20 20 20 20 20 69 36 34 20 6e  ){.        i64 n
1729e 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 20  CellKey;.       
1729f 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44   if( pPage->hasD
172a0 61 74 61 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ata ){.         
172a1 20 75 33 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20   u32 dummy;.    
172a2 20 20 20 20 20 20 70 43 65 6c 6c 20 2b 3d 20 67        pCell += g
172a3 65 74 56 61 72 69 6e 74 33 32 28 70 43 65 6c 6c  etVarint32(pCell
172a4 2c 20 64 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20  , dummy);.      
172a5 20 20 7d 0a 20 20 20 20 20 20 20 20 67 65 74 56    }.        getV
172a6 61 72 69 6e 74 28 70 43 65 6c 6c 2c 20 28 75 36  arint(pCell, (u6
172a7 34 2a 29 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20  4*)&nCellKey);. 
172a8 20 20 20 20 20 20 20 69 66 28 20 6e 43 65 6c 6c         if( nCell
172a9 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20  Key==intKey ){. 
172aa 20 20 20 20 20 20 20 20 20 63 20 3d 20 30 3b 0a           c = 0;.
172ab 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
172ac 28 20 6e 43 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65  ( nCellKey<intKe
172ad 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63  y ){.          c
172ae 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d   = -1;.        }
172af 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
172b0 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 4b 65 79  assert( nCellKey
172b1 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 20 20 20 20  >intKey );.     
172b2 20 20 20 20 20 63 20 3d 20 2b 31 3b 0a 20 20 20       c = +1;.   
172b3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70       }.        p
172b4 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d  Cur->validNKey =
172b5 20 31 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72   1;.        pCur
172b6 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43  ->info.nKey = nC
172b7 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 7d 65  ellKey;.      }e
172b8 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
172b9 54 68 65 20 6d 61 78 69 6d 75 6d 20 73 75 70 70  The maximum supp
172ba 6f 72 74 65 64 20 70 61 67 65 2d 73 69 7a 65 20  orted page-size 
172bb 69 73 20 33 32 37 36 38 20 62 79 74 65 73 2e 20  is 32768 bytes. 
172bc 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 0a  This means that.
172bd 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6d          ** the m
172be 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
172bf 20 72 65 63 6f 72 64 20 62 79 74 65 73 20 73 74   record bytes st
172c0 6f 72 65 64 20 6f 6e 20 61 6e 20 69 6e 64 65 78  ored on an index
172c1 20 42 2d 54 72 65 65 0a 20 20 20 20 20 20 20 20   B-Tree.        
172c2 2a 2a 20 70 61 67 65 20 69 73 20 61 74 20 6d 6f  ** page is at mo
172c3 73 74 20 38 31 39 38 20 62 79 74 65 73 2c 20 77  st 8198 bytes, w
172c4 68 69 63 68 20 6d 61 79 20 62 65 20 73 74 6f 72  hich may be stor
172c5 65 64 20 61 73 20 61 20 32 2d 62 79 74 65 0a 20  ed as a 2-byte. 
172c6 20 20 20 20 20 20 20 2a 2a 20 76 61 72 69 6e 74         ** varint
172c7 2e 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69  . This informati
172c8 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 61 74  on is used to at
172c9 74 65 6d 70 74 20 74 6f 20 61 76 6f 69 64 20 70  tempt to avoid p
172ca 61 72 73 69 6e 67 20 0a 20 20 20 20 20 20 20 20  arsing .        
172cb 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20 63 65  ** the entire ce
172cc 6c 6c 20 62 79 20 63 68 65 63 6b 69 6e 67 20 66  ll by checking f
172cd 6f 72 20 74 68 65 20 63 61 73 65 73 20 77 68 65  or the cases whe
172ce 72 65 20 74 68 65 20 72 65 63 6f 72 64 20 69 73  re the record is
172cf 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f   .        ** sto
172d0 72 65 64 20 65 6e 74 69 72 65 6c 79 20 77 69 74  red entirely wit
172d1 68 69 6e 20 74 68 65 20 62 2d 74 72 65 65 20 70  hin the b-tree p
172d2 61 67 65 20 62 79 20 69 6e 73 70 65 63 74 69 6e  age by inspectin
172d3 67 20 74 68 65 20 66 69 72 73 74 20 0a 20 20 20  g the first .   
172d4 20 20 20 20 20 2a 2a 20 32 20 62 79 74 65 73 20       ** 2 bytes 
172d5 6f 66 20 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20  of the cell..   
172d6 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
172d7 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 70 43 65 6c  int nCell = pCel
172d8 6c 5b 30 5d 3b 0a 20 20 20 20 20 20 20 20 69 66  l[0];.        if
172d9 28 20 21 28 6e 43 65 6c 6c 20 26 20 30 78 38 30  ( !(nCell & 0x80
172da 29 20 26 26 20 6e 43 65 6c 6c 3c 3d 70 50 61 67  ) && nCell<=pPag
172db 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 7b 0a 20  e->maxLocal ){. 
172dc 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
172dd 20 62 72 61 6e 63 68 20 72 75 6e 73 20 69 66 20   branch runs if 
172de 74 68 65 20 72 65 63 6f 72 64 2d 73 69 7a 65 20  the record-size 
172df 66 69 65 6c 64 20 6f 66 20 74 68 65 20 63 65 6c  field of the cel
172e0 6c 20 69 73 20 61 0a 20 20 20 20 20 20 20 20 20  l is a.         
172e1 20 2a 2a 20 73 69 6e 67 6c 65 20 62 79 74 65 20   ** single byte 
172e2 76 61 72 69 6e 74 20 61 6e 64 20 74 68 65 20 72  varint and the r
172e3 65 63 6f 72 64 20 66 69 74 73 20 65 6e 74 69 72  ecord fits entir
172e4 65 6c 79 20 6f 6e 20 74 68 65 20 6d 61 69 6e 0a  ely on the main.
172e5 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 2d 74            ** b-t
172e6 72 65 65 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20  ree page.  */.  
172e7 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69          c = sqli
172e8 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d  te3VdbeRecordCom
172e9 70 61 72 65 28 6e 43 65 6c 6c 2c 20 28 76 6f 69  pare(nCell, (voi
172ea 64 2a 29 26 70 43 65 6c 6c 5b 31 5d 2c 20 70 49  d*)&pCell[1], pI
172eb 64 78 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20  dxKey);.        
172ec 7d 65 6c 73 65 20 69 66 28 20 21 28 70 43 65 6c  }else if( !(pCel
172ed 6c 5b 31 5d 20 26 20 30 78 38 30 29 20 0a 20 20  l[1] & 0x80) .  
172ee 20 20 20 20 20 20 20 20 26 26 20 28 6e 43 65 6c          && (nCel
172ef 6c 20 3d 20 28 28 6e 43 65 6c 6c 26 30 78 37 66  l = ((nCell&0x7f
172f0 29 3c 3c 37 29 20 2b 20 70 43 65 6c 6c 5b 31 5d  )<<7) + pCell[1]
172f1 29 3c 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63  )<=pPage->maxLoc
172f2 61 6c 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20  al.        ){.  
172f3 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
172f4 65 63 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c 64  ecord-size field
172f5 20 69 73 20 61 20 32 20 62 79 74 65 20 76 61 72   is a 2 byte var
172f6 69 6e 74 20 61 6e 64 20 74 68 65 20 72 65 63 6f  int and the reco
172f7 72 64 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  rd .          **
172f8 20 66 69 74 73 20 65 6e 74 69 72 65 6c 79 20 6f   fits entirely o
172f9 6e 20 74 68 65 20 6d 61 69 6e 20 62 2d 74 72 65  n the main b-tre
172fa 65 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20 20 20  e page.  */.    
172fb 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65        c = sqlite
172fc 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61  3VdbeRecordCompa
172fd 72 65 28 6e 43 65 6c 6c 2c 20 28 76 6f 69 64 2a  re(nCell, (void*
172fe 29 26 70 43 65 6c 6c 5b 32 5d 2c 20 70 49 64 78  )&pCell[2], pIdx
172ff 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  Key);.        }e
17300 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  lse{.          /
17301 2a 20 54 68 65 20 72 65 63 6f 72 64 20 66 6c 6f  * The record flo
17302 77 73 20 6f 76 65 72 20 6f 6e 74 6f 20 6f 6e 65  ws over onto one
17303 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f   or more overflo
17304 77 20 70 61 67 65 73 2e 20 49 6e 0a 20 20 20 20  w pages. In.    
17305 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 63 61        ** this ca
17306 73 65 20 74 68 65 20 77 68 6f 6c 65 20 63 65 6c  se the whole cel
17307 6c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 70 61  l needs to be pa
17308 72 73 65 64 2c 20 61 20 62 75 66 66 65 72 20 61  rsed, a buffer a
17309 6c 6c 6f 63 61 74 65 64 0a 20 20 20 20 20 20 20  llocated.       
1730a 20 20 20 2a 2a 20 61 6e 64 20 61 63 63 65 73 73     ** and access
1730b 50 61 79 6c 6f 61 64 28 29 20 75 73 65 64 20 74  Payload() used t
1730c 6f 20 72 65 74 72 69 65 76 65 20 74 68 65 20 72  o retrieve the r
1730d 65 63 6f 72 64 20 69 6e 74 6f 20 74 68 65 0a 20  ecord into the. 
1730e 20 20 20 20 20 20 20 20 20 2a 2a 20 62 75 66 66           ** buff
1730f 65 72 20 62 65 66 6f 72 65 20 56 64 62 65 52 65  er before VdbeRe
17310 63 6f 72 64 43 6f 6d 70 61 72 65 28 29 20 63 61  cordCompare() ca
17311 6e 20 62 65 20 63 61 6c 6c 65 64 2e 20 2a 2f 0a  n be called. */.
17312 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 20 2a            void *
17313 70 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20  pCellKey;.      
17314 20 20 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 70      u8 * const p
17315 43 65 6c 6c 42 6f 64 79 20 3d 20 70 43 65 6c 6c  CellBody = pCell
17316 20 2d 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50   - pPage->childP
17317 74 72 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20  trSize;.        
17318 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c    btreeParseCell
17319 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c  Ptr(pPage, pCell
1731a 42 6f 64 79 2c 20 26 70 43 75 72 2d 3e 69 6e 66  Body, &pCur->inf
1731b 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 43  o);.          nC
1731c 65 6c 6c 20 3d 20 28 69 6e 74 29 70 43 75 72 2d  ell = (int)pCur-
1731d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20  >info.nKey;.    
1731e 20 20 20 20 20 20 70 43 65 6c 6c 4b 65 79 20 3d        pCellKey =
1731f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20   sqlite3Malloc( 
17320 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 20 20 20  nCell );.       
17321 20 20 20 69 66 28 20 70 43 65 6c 6c 4b 65 79 3d     if( pCellKey=
17322 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
17323 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
17324 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 20  MEM;.           
17325 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e   goto moveto_fin
17326 69 73 68 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ish;.          }
17327 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
17328 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43  accessPayload(pC
17329 75 72 2c 20 30 2c 20 6e 43 65 6c 6c 2c 20 28 75  ur, 0, nCell, (u
1732a 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 70 43  nsigned char*)pC
1732b 65 6c 6c 4b 65 79 2c 20 30 29 3b 0a 20 20 20 20  ellKey, 0);.    
1732c 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
1732d 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1732e 74 65 33 5f 66 72 65 65 28 70 43 65 6c 6c 4b 65  te3_free(pCellKe
1732f 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  y);.            
17330 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69  goto moveto_fini
17331 73 68 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  sh;.          }.
17332 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 73 71            c = sq
17333 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43  lite3VdbeRecordC
17334 6f 6d 70 61 72 65 28 6e 43 65 6c 6c 2c 20 70 43  ompare(nCell, pC
17335 65 6c 6c 4b 65 79 2c 20 70 49 64 78 4b 65 79 29  ellKey, pIdxKey)
17336 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
17337 74 65 33 5f 66 72 65 65 28 70 43 65 6c 6c 4b 65  te3_free(pCellKe
17338 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  y);.        }.  
17339 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
1733a 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  c==0 ){.        
1733b 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65  if( pPage->intKe
1733c 79 20 26 26 20 21 70 50 61 67 65 2d 3e 6c 65 61  y && !pPage->lea
1733d 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c  f ){.          l
1733e 77 72 20 3d 20 69 64 78 3b 0a 20 20 20 20 20 20  wr = idx;.      
1733f 20 20 20 20 75 70 72 20 3d 20 6c 77 72 20 2d 20      upr = lwr - 
17340 31 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  1;.          bre
17341 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ak;.        }els
17342 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 70 52  e{.          *pR
17343 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  es = 0;.        
17344 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
17345 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
17346 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a   moveto_finish;.
17347 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
17348 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3c 30 20  }.      if( c<0 
17349 29 7b 0a 20 20 20 20 20 20 20 20 6c 77 72 20 3d  ){.        lwr =
1734a 20 69 64 78 2b 31 3b 0a 20 20 20 20 20 20 7d 65   idx+1;.      }e
1734b 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 75 70 72  lse{.        upr
1734c 20 3d 20 69 64 78 2d 31 3b 0a 20 20 20 20 20 20   = idx-1;.      
1734d 7d 0a 20 20 20 20 20 20 69 66 28 20 6c 77 72 3e  }.      if( lwr>
1734e 75 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 62  upr ){.        b
1734f 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
17350 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b      pCur->aiIdx[
17351 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28  pCur->iPage] = (
17352 75 31 36 29 28 28 6c 77 72 2b 75 70 72 29 2f 32  u16)((lwr+upr)/2
17353 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  );.    }.    ass
17354 65 72 74 28 20 6c 77 72 3d 3d 75 70 72 2b 31 20  ert( lwr==upr+1 
17355 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
17356 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a  Page->isInit );.
17357 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c      if( pPage->l
17358 65 61 66 20 29 7b 0a 20 20 20 20 20 20 63 68 6c  eaf ){.      chl
17359 64 50 67 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  dPg = 0;.    }el
1735a 73 65 20 69 66 28 20 6c 77 72 3e 3d 70 50 61 67  se if( lwr>=pPag
1735b 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20  e->nCell ){.    
1735c 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 34 62    chldPg = get4b
1735d 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74  yte(&pPage->aDat
1735e 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  a[pPage->hdrOffs
1735f 65 74 2b 38 5d 29 3b 0a 20 20 20 20 7d 65 6c 73  et+8]);.    }els
17360 65 7b 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20  e{.      chldPg 
17361 3d 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43  = get4byte(findC
17362 65 6c 6c 28 70 50 61 67 65 2c 20 6c 77 72 29 29  ell(pPage, lwr))
17363 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
17364 63 68 6c 64 50 67 3d 3d 30 20 29 7b 0a 20 20 20  chldPg==0 ){.   
17365 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
17366 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
17367 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50 61 67 65  ge]<pCur->apPage
17368 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e  [pCur->iPage]->n
17369 43 65 6c 6c 20 29 3b 0a 20 20 20 20 20 20 2a 70  Cell );.      *p
1736a 52 65 73 20 3d 20 63 3b 0a 20 20 20 20 20 20 72  Res = c;.      r
1736b 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1736c 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f       goto moveto
1736d 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 7d 0a 20  _finish;.    }. 
1736e 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70     pCur->aiIdx[p
1736f 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75  Cur->iPage] = (u
17370 31 36 29 6c 77 72 3b 0a 20 20 20 20 70 43 75 72  16)lwr;.    pCur
17371 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30  ->info.nSize = 0
17372 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69  ;.    pCur->vali
17373 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 20 20 72  dNKey = 0;.    r
17374 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28  c = moveToChild(
17375 70 43 75 72 2c 20 63 68 6c 64 50 67 29 3b 0a 20  pCur, chldPg);. 
17376 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f     if( rc ) goto
17377 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a   moveto_finish;.
17378 20 20 7d 0a 6d 6f 76 65 74 6f 5f 66 69 6e 69 73    }.moveto_finis
17379 68 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  h:.  return rc;.
1737a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  }.../*.** Return
1737b 20 54 52 55 45 20 69 66 20 74 68 65 20 63 75 72   TRUE if the cur
1737c 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e 74  sor is not point
1737d 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20  ing at an entry 
1737e 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a  of the table..**
1737f 0a 2a 2a 20 54 52 55 45 20 77 69 6c 6c 20 62 65  .** TRUE will be
17380 20 72 65 74 75 72 6e 65 64 20 61 66 74 65 72 20   returned after 
17381 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
17382 33 42 74 72 65 65 4e 65 78 74 28 29 20 6d 6f 76  3BtreeNext() mov
17383 65 73 0a 2a 2a 20 70 61 73 74 20 74 68 65 20 6c  es.** past the l
17384 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65  ast entry in the
17385 20 74 61 62 6c 65 20 6f 72 20 73 71 6c 69 74 65   table or sqlite
17386 33 42 74 72 65 65 50 72 65 76 28 29 20 6d 6f 76  3BtreePrev() mov
17387 65 73 20 70 61 73 74 0a 2a 2a 20 74 68 65 20 66  es past.** the f
17388 69 72 73 74 20 65 6e 74 72 79 2e 20 20 54 52 55  irst entry.  TRU
17389 45 20 69 73 20 61 6c 73 6f 20 72 65 74 75 72 6e  E is also return
1738a 65 64 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  ed if the table 
1738b 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c  is empty..*/.SQL
1738c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1738d 73 71 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28  sqlite3BtreeEof(
1738e 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
1738f 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74  .  /* TODO: What
17390 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 69   if the cursor i
17391 73 20 69 6e 20 43 55 52 53 4f 52 5f 52 45 51 55  s in CURSOR_REQU
17392 49 52 45 53 45 45 4b 20 62 75 74 20 61 6c 6c 20  IRESEEK but all 
17393 74 61 62 6c 65 20 65 6e 74 72 69 65 73 0a 20 20  table entries.  
17394 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 64 65 6c  ** have been del
17395 65 74 65 64 3f 20 54 68 69 73 20 41 50 49 20 77  eted? This API w
17396 69 6c 6c 20 6e 65 65 64 20 74 6f 20 63 68 61 6e  ill need to chan
17397 67 65 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20  ge to return an 
17398 65 72 72 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20  error code.  ** 
17399 61 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 62  as well as the b
1739a 6f 6f 6c 65 61 6e 20 72 65 73 75 6c 74 20 76 61  oolean result va
1739b 6c 75 65 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75  lue..  */.  retu
1739c 72 6e 20 28 43 55 52 53 4f 52 5f 56 41 4c 49 44  rn (CURSOR_VALID
1739d 21 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 29 3b  !=pCur->eState);
1739e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63  .}../*.** Advanc
1739f 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20  e the cursor to 
173a0 74 68 65 20 6e 65 78 74 20 65 6e 74 72 79 20 69  the next entry i
173a1 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  n the database. 
173a2 20 49 66 0a 2a 2a 20 73 75 63 63 65 73 73 66 75   If.** successfu
173a3 6c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73  l then set *pRes
173a4 3d 30 2e 20 20 49 66 20 74 68 65 20 63 75 72 73  =0.  If the curs
173a5 6f 72 0a 2a 2a 20 77 61 73 20 61 6c 72 65 61 64  or.** was alread
173a6 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68  y pointing to th
173a7 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20  e last entry in 
173a8 74 68 65 20 64 61 74 61 62 61 73 65 20 62 65 66  the database bef
173a9 6f 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74  ore.** this rout
173aa 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 2c 20  ine was called, 
173ab 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d 31  then set *pRes=1
173ac 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
173ad 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
173ae 74 72 65 65 4e 65 78 74 28 42 74 43 75 72 73 6f  treeNext(BtCurso
173af 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52  r *pCur, int *pR
173b0 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  es){.  int rc;. 
173b1 20 69 6e 74 20 69 64 78 3b 0a 20 20 4d 65 6d 50   int idx;.  MemP
173b2 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61  age *pPage;..  a
173b3 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
173b4 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
173b5 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43  .  rc = restoreC
173b6 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43  ursorPosition(pC
173b7 75 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  ur);.  if( rc!=S
173b8 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
173b9 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
173ba 20 61 73 73 65 72 74 28 20 70 52 65 73 21 3d 30   assert( pRes!=0
173bb 20 29 3b 0a 20 20 69 66 28 20 43 55 52 53 4f 52   );.  if( CURSOR
173bc 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e  _INVALID==pCur->
173bd 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 2a 70  eState ){.    *p
173be 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 72 65 74  Res = 1;.    ret
173bf 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
173c0 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 73   }.  if( pCur->s
173c1 6b 69 70 4e 65 78 74 3e 30 20 29 7b 0a 20 20 20  kipNext>0 ){.   
173c2 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20   pCur->skipNext 
173c3 3d 20 30 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d  = 0;.    *pRes =
173c4 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   0;.    return S
173c5 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
173c6 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d  pCur->skipNext =
173c7 20 30 3b 0a 0a 20 20 70 50 61 67 65 20 3d 20 70   0;..  pPage = p
173c8 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
173c9 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 69 64 78 20  ->iPage];.  idx 
173ca 3d 20 2b 2b 70 43 75 72 2d 3e 61 69 49 64 78 5b  = ++pCur->aiIdx[
173cb 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20  pCur->iPage];.  
173cc 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69  assert( pPage->i
173cd 73 49 6e 69 74 20 29 3b 0a 20 20 61 73 73 65 72  sInit );.  asser
173ce 74 28 20 69 64 78 3c 3d 70 50 61 67 65 2d 3e 6e  t( idx<=pPage->n
173cf 43 65 6c 6c 20 29 3b 0a 0a 20 20 70 43 75 72 2d  Cell );..  pCur-
173d0 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b  >info.nSize = 0;
173d1 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b  .  pCur->validNK
173d2 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 69 64  ey = 0;.  if( id
173d3 78 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  x>=pPage->nCell 
173d4 29 7b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67  ){.    if( !pPag
173d5 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20  e->leaf ){.     
173d6 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c   rc = moveToChil
173d7 64 28 70 43 75 72 2c 20 67 65 74 34 62 79 74 65  d(pCur, get4byte
173d8 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70  (&pPage->aData[p
173d9 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b  Page->hdrOffset+
173da 38 5d 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20  8]));.      if( 
173db 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
173dc 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54        rc = moveT
173dd 6f 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b  oLeftmost(pCur);
173de 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30  .      *pRes = 0
173df 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
173e0 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 6f 7b  c;.    }.    do{
173e1 0a 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d  .      if( pCur-
173e2 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20  >iPage==0 ){.   
173e3 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a       *pRes = 1;.
173e4 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53          pCur->eS
173e5 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e  tate = CURSOR_IN
173e6 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 20 20 72  VALID;.        r
173e7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
173e8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d  .      }.      m
173e9 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 43 75 72  oveToParent(pCur
173ea 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 20 3d  );.      pPage =
173eb 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
173ec 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20  ur->iPage];.    
173ed 7d 77 68 69 6c 65 28 20 70 43 75 72 2d 3e 61 69  }while( pCur->ai
173ee 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
173ef 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29  >=pPage->nCell )
173f0 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b  ;.    *pRes = 0;
173f1 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e  .    if( pPage->
173f2 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  intKey ){.      
173f3 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
173f4 65 4e 65 78 74 28 70 43 75 72 2c 20 70 52 65 73  eNext(pCur, pRes
173f5 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
173f6 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
173f7 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  OK;.    }.    re
173f8 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 2a  turn rc;.  }.  *
173f9 70 52 65 73 20 3d 20 30 3b 0a 20 20 69 66 28 20  pRes = 0;.  if( 
173fa 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
173fb 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
173fc 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  _OK;.  }.  rc = 
173fd 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70  moveToLeftmost(p
173fe 43 75 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  Cur);.  return r
173ff 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65  c;.}.../*.** Ste
17400 70 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20  p the cursor to 
17401 74 68 65 20 62 61 63 6b 20 74 6f 20 74 68 65 20  the back to the 
17402 70 72 65 76 69 6f 75 73 20 65 6e 74 72 79 20 69  previous entry i
17403 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  n the database. 
17404 20 49 66 0a 2a 2a 20 73 75 63 63 65 73 73 66 75   If.** successfu
17405 6c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73  l then set *pRes
17406 3d 30 2e 20 20 49 66 20 74 68 65 20 63 75 72 73  =0.  If the curs
17407 6f 72 0a 2a 2a 20 77 61 73 20 61 6c 72 65 61 64  or.** was alread
17408 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68  y pointing to th
17409 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 69 6e  e first entry in
1740a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 65   the database be
1740b 66 6f 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75  fore.** this rou
1740c 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 2c  tine was called,
1740d 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d   then set *pRes=
1740e 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  1..*/.SQLITE_PRI
1740f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
17410 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 42 74  BtreePrevious(Bt
17411 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e  Cursor *pCur, in
17412 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20  t *pRes){.  int 
17413 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  rc;.  MemPage *p
17414 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28  Page;..  assert(
17415 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
17416 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20  x(pCur) );.  rc 
17417 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50  = restoreCursorP
17418 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20  osition(pCur);. 
17419 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1741a 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
1741b 20 72 63 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d   rc;.  }.  pCur-
1741c 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20 69  >atLast = 0;.  i
1741d 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49  f( CURSOR_INVALI
1741e 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20  D==pCur->eState 
1741f 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 31  ){.    *pRes = 1
17420 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
17421 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66  ITE_OK;.  }.  if
17422 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74  ( pCur->skipNext
17423 3c 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e  <0 ){.    pCur->
17424 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20  skipNext = 0;.  
17425 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20    *pRes = 0;.   
17426 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
17427 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 73  K;.  }.  pCur->s
17428 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 0a 20 20  kipNext = 0;..  
17429 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70  pPage = pCur->ap
1742a 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
1742b 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ];.  assert( pPa
1742c 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20  ge->isInit );.  
1742d 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
1742e 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 64 78 20   ){.    int idx 
1742f 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  = pCur->aiIdx[pC
17430 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20  ur->iPage];.    
17431 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64  rc = moveToChild
17432 28 70 43 75 72 2c 20 67 65 74 34 62 79 74 65 28  (pCur, get4byte(
17433 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20  findCell(pPage, 
17434 69 64 78 29 29 29 3b 0a 20 20 20 20 69 66 28 20  idx)));.    if( 
17435 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  rc ){.      retu
17436 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
17437 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68   rc = moveToRigh
17438 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 7d  tmost(pCur);.  }
17439 65 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c 65 28  else{.    while(
1743a 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
1743b 72 2d 3e 69 50 61 67 65 5d 3d 3d 30 20 29 7b 0a  r->iPage]==0 ){.
1743c 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e        if( pCur->
1743d 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20  iPage==0 ){.    
1743e 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65      pCur->eState
1743f 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49   = CURSOR_INVALI
17440 44 3b 0a 20 20 20 20 20 20 20 20 2a 70 52 65 73  D;.        *pRes
17441 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 65   = 1;.        re
17442 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
17443 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 6f        }.      mo
17444 76 65 54 6f 50 61 72 65 6e 74 28 70 43 75 72 29  veToParent(pCur)
17445 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 72  ;.    }.    pCur
17446 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30  ->info.nSize = 0
17447 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69  ;.    pCur->vali
17448 64 4e 4b 65 79 20 3d 20 30 3b 0a 0a 20 20 20 20  dNKey = 0;..    
17449 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
1744a 2d 3e 69 50 61 67 65 5d 2d 2d 3b 0a 20 20 20 20  ->iPage]--;.    
1744b 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70  pPage = pCur->ap
1744c 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
1744d 5d 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  ];.    if( pPage
1744e 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 70 50 61  ->intKey && !pPa
1744f 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20  ge->leaf ){.    
17450 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
17451 72 65 65 50 72 65 76 69 6f 75 73 28 70 43 75 72  reePrevious(pCur
17452 2c 20 70 52 65 73 29 3b 0a 20 20 20 20 7d 65 6c  , pRes);.    }el
17453 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  se{.      rc = S
17454 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
17455 20 20 7d 0a 20 20 2a 70 52 65 73 20 3d 20 30 3b    }.  *pRes = 0;
17456 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
17457 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
17458 61 20 6e 65 77 20 70 61 67 65 20 66 72 6f 6d 20  a new page from 
17459 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1745a 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 77  e..**.** The new
1745b 20 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20   page is marked 
1745c 61 73 20 64 69 72 74 79 2e 20 20 28 49 6e 20 6f  as dirty.  (In o
1745d 74 68 65 72 20 77 6f 72 64 73 2c 20 73 71 6c 69  ther words, sqli
1745e 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 0a  te3PagerWrite().
1745f 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  ** has already b
17460 65 65 6e 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68  een called on th
17461 65 20 6e 65 77 20 70 61 67 65 2e 29 20 20 54 68  e new page.)  Th
17462 65 20 6e 65 77 20 70 61 67 65 20 68 61 73 20 61  e new page has a
17463 6c 73 6f 0a 2a 2a 20 62 65 65 6e 20 72 65 66 65  lso.** been refe
17464 72 65 6e 63 65 64 20 61 6e 64 20 74 68 65 20 63  renced and the c
17465 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20 69  alling routine i
17466 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f  s responsible fo
17467 72 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c  r calling.** sql
17468 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 29  ite3PagerUnref()
17469 20 6f 6e 20 74 68 65 20 6e 65 77 20 70 61 67 65   on the new page
1746a 20 77 68 65 6e 20 69 74 20 69 73 20 64 6f 6e 65   when it is done
1746b 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f  ..**.** SQLITE_O
1746c 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e  K is returned on
1746d 20 73 75 63 63 65 73 73 2e 20 20 41 6e 79 20 6f   success.  Any o
1746e 74 68 65 72 20 72 65 74 75 72 6e 20 76 61 6c 75  ther return valu
1746f 65 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 61  e indicates.** a
17470 6e 20 65 72 72 6f 72 2e 20 20 2a 70 70 50 61 67  n error.  *ppPag
17471 65 20 61 6e 64 20 2a 70 50 67 6e 6f 20 61 72 65  e and *pPgno are
17472 20 75 6e 64 65 66 69 6e 65 64 20 69 6e 20 74 68   undefined in th
17473 65 20 65 76 65 6e 74 20 6f 66 20 61 6e 20 65 72  e event of an er
17474 72 6f 72 2e 0a 2a 2a 20 44 6f 20 6e 6f 74 20 69  ror..** Do not i
17475 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 50 61 67  nvoke sqlite3Pag
17476 65 72 55 6e 72 65 66 28 29 20 6f 6e 20 2a 70 70  erUnref() on *pp
17477 50 61 67 65 20 69 66 20 61 6e 20 65 72 72 6f 72  Page if an error
17478 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
17479 0a 2a 2a 20 49 66 20 74 68 65 20 22 6e 65 61 72  .** If the "near
1747a 62 79 22 20 70 61 72 61 6d 65 74 65 72 20 69 73  by" parameter is
1747b 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 61 20 28   not 0, then a (
1747c 66 65 65 62 6c 65 29 20 65 66 66 6f 72 74 20 69  feeble) effort i
1747d 73 20 6d 61 64 65 20 74 6f 20 0a 2a 2a 20 6c 6f  s made to .** lo
1747e 63 61 74 65 20 61 20 70 61 67 65 20 63 6c 6f 73  cate a page clos
1747f 65 20 74 6f 20 74 68 65 20 70 61 67 65 20 6e 75  e to the page nu
17480 6d 62 65 72 20 22 6e 65 61 72 62 79 22 2e 20 20  mber "nearby".  
17481 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64  This can be used
17482 20 69 6e 20 61 6e 0a 2a 2a 20 61 74 74 65 6d 70   in an.** attemp
17483 74 20 74 6f 20 6b 65 65 70 20 72 65 6c 61 74 65  t to keep relate
17484 64 20 70 61 67 65 73 20 63 6c 6f 73 65 20 74 6f  d pages close to
17485 20 65 61 63 68 20 6f 74 68 65 72 20 69 6e 20 74   each other in t
17486 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
17487 2c 0a 2a 2a 20 77 68 69 63 68 20 69 6e 20 74 75  ,.** which in tu
17488 72 6e 20 63 61 6e 20 6d 61 6b 65 20 64 61 74 61  rn can make data
17489 62 61 73 65 20 61 63 63 65 73 73 20 66 61 73 74  base access fast
1748a 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  er..**.** If the
1748b 20 22 65 78 61 63 74 22 20 70 61 72 61 6d 65 74   "exact" paramet
1748c 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 61 6e 64  er is not 0, and
1748d 20 74 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72   the page-number
1748e 20 6e 65 61 72 62 79 20 65 78 69 73 74 73 20 0a   nearby exists .
1748f 2a 2a 20 61 6e 79 77 68 65 72 65 20 6f 6e 20 74  ** anywhere on t
17490 68 65 20 66 72 65 65 2d 6c 69 73 74 2c 20 74 68  he free-list, th
17491 65 6e 20 69 74 20 69 73 20 67 75 61 72 65 6e 74  en it is guarent
17492 65 65 64 20 74 6f 20 62 65 20 72 65 74 75 72 6e  eed to be return
17493 65 64 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 6f  ed. This.** is o
17494 6e 6c 79 20 75 73 65 64 20 62 79 20 61 75 74 6f  nly used by auto
17495 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65  -vacuum database
17496 73 20 77 68 65 6e 20 61 6c 6c 6f 63 61 74 69 6e  s when allocatin
17497 67 20 61 20 6e 65 77 20 74 61 62 6c 65 2e 0a 2a  g a new table..*
17498 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c  /.static int all
17499 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 0a  ocateBtreePage(.
1749a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c    BtShared *pBt,
1749b 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70   .  MemPage **pp
1749c 50 61 67 65 2c 20 0a 20 20 50 67 6e 6f 20 2a 70  Page, .  Pgno *p
1749d 50 67 6e 6f 2c 20 0a 20 20 50 67 6e 6f 20 6e 65  Pgno, .  Pgno ne
1749e 61 72 62 79 2c 0a 20 20 75 38 20 65 78 61 63 74  arby,.  u8 exact
1749f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  .){.  MemPage *p
174a0 50 61 67 65 31 3b 0a 20 20 69 6e 74 20 72 63 3b  Page1;.  int rc;
174a1 0a 20 20 75 33 32 20 6e 3b 20 20 20 20 20 2f 2a  .  u32 n;     /*
174a2 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
174a3 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74   on the freelist
174a4 20 2a 2f 0a 20 20 75 33 32 20 6b 3b 20 20 20 20   */.  u32 k;    
174a5 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65   /* Number of le
174a6 61 76 65 73 20 6f 6e 20 74 68 65 20 74 72 75 6e  aves on the trun
174a7 6b 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73  k of the freelis
174a8 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a  t */.  MemPage *
174a9 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 4d 65  pTrunk = 0;.  Me
174aa 6d 50 61 67 65 20 2a 70 50 72 65 76 54 72 75 6e  mPage *pPrevTrun
174ab 6b 20 3d 20 30 3b 0a 20 20 50 67 6e 6f 20 6d 78  k = 0;.  Pgno mx
174ac 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 54 6f 74  Page;     /* Tot
174ad 61 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64  al size of the d
174ae 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
174af 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
174b0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
174b1 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70  t->mutex) );.  p
174b2 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61  Page1 = pBt->pPa
174b3 67 65 31 3b 0a 20 20 6d 78 50 61 67 65 20 3d 20  ge1;.  mxPage = 
174b4 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  pagerPagecount(p
174b5 42 74 29 3b 0a 20 20 6e 20 3d 20 67 65 74 34 62  Bt);.  n = get4b
174b6 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61  yte(&pPage1->aDa
174b7 74 61 5b 33 36 5d 29 3b 0a 20 20 74 65 73 74 63  ta[36]);.  testc
174b8 61 73 65 28 20 6e 3d 3d 6d 78 50 61 67 65 2d 31  ase( n==mxPage-1
174b9 20 29 3b 0a 20 20 69 66 28 20 6e 3e 3d 6d 78 50   );.  if( n>=mxP
174ba 61 67 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  age ){.    retur
174bb 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
174bc 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 69 66 28  _BKPT;.  }.  if(
174bd 20 6e 3e 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54   n>0 ){.    /* T
174be 68 65 72 65 20 61 72 65 20 70 61 67 65 73 20 6f  here are pages o
174bf 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20  n the freelist. 
174c0 20 52 65 75 73 65 20 6f 6e 65 20 6f 66 20 74 68   Reuse one of th
174c1 6f 73 65 20 70 61 67 65 73 2e 20 2a 2f 0a 20 20  ose pages. */.  
174c2 20 20 50 67 6e 6f 20 69 54 72 75 6e 6b 3b 0a 20    Pgno iTrunk;. 
174c3 20 20 20 75 38 20 73 65 61 72 63 68 4c 69 73 74     u8 searchList
174c4 20 3d 20 30 3b 20 2f 2a 20 49 66 20 74 68 65 20   = 0; /* If the 
174c5 66 72 65 65 2d 6c 69 73 74 20 6d 75 73 74 20 62  free-list must b
174c6 65 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 27  e searched for '
174c7 6e 65 61 72 62 79 27 20 2a 2f 0a 20 20 20 20 0a  nearby' */.    .
174c8 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 27 65      /* If the 'e
174c9 78 61 63 74 27 20 70 61 72 61 6d 65 74 65 72 20  xact' parameter 
174ca 77 61 73 20 74 72 75 65 20 61 6e 64 20 61 20 71  was true and a q
174cb 75 65 72 79 20 6f 66 20 74 68 65 20 70 6f 69 6e  uery of the poin
174cc 74 65 72 2d 6d 61 70 0a 20 20 20 20 2a 2a 20 73  ter-map.    ** s
174cd 68 6f 77 73 20 74 68 61 74 20 74 68 65 20 70 61  hows that the pa
174ce 67 65 20 27 6e 65 61 72 62 79 27 20 69 73 20 73  ge 'nearby' is s
174cf 6f 6d 65 77 68 65 72 65 20 6f 6e 20 74 68 65 20  omewhere on the 
174d0 66 72 65 65 2d 6c 69 73 74 2c 20 74 68 65 6e 0a  free-list, then.
174d1 20 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 69 72      ** the entir
174d2 65 2d 6c 69 73 74 20 77 69 6c 6c 20 62 65 20 73  e-list will be s
174d3 65 61 72 63 68 65 64 20 66 6f 72 20 74 68 61 74  earched for that
174d4 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 23 69   page..    */.#i
174d5 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
174d6 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
174d7 20 69 66 28 20 65 78 61 63 74 20 26 26 20 6e 65   if( exact && ne
174d8 61 72 62 79 3c 3d 6d 78 50 61 67 65 20 29 7b 0a  arby<=mxPage ){.
174d9 20 20 20 20 20 20 75 38 20 65 54 79 70 65 3b 0a        u8 eType;.
174da 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 65        assert( ne
174db 61 72 62 79 3e 30 20 29 3b 0a 20 20 20 20 20 20  arby>0 );.      
174dc 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74  assert( pBt->aut
174dd 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 20 20 20  oVacuum );.     
174de 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28   rc = ptrmapGet(
174df 70 42 74 2c 20 6e 65 61 72 62 79 2c 20 26 65 54  pBt, nearby, &eT
174e0 79 70 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 69  ype, 0);.      i
174e1 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
174e2 63 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54 79  c;.      if( eTy
174e3 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50  pe==PTRMAP_FREEP
174e4 41 47 45 20 29 7b 0a 20 20 20 20 20 20 20 20 73  AGE ){.        s
174e5 65 61 72 63 68 4c 69 73 74 20 3d 20 31 3b 0a 20  earchList = 1;. 
174e6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 50       }.      *pP
174e7 67 6e 6f 20 3d 20 6e 65 61 72 62 79 3b 0a 20 20  gno = nearby;.  
174e8 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
174e9 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65  /* Decrement the
174ea 20 66 72 65 65 2d 6c 69 73 74 20 63 6f 75 6e 74   free-list count
174eb 20 62 79 20 31 2e 20 53 65 74 20 69 54 72 75 6e   by 1. Set iTrun
174ec 6b 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f  k to the index o
174ed 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 69 72  f the.    ** fir
174ee 73 74 20 66 72 65 65 2d 6c 69 73 74 20 74 72 75  st free-list tru
174ef 6e 6b 20 70 61 67 65 2e 20 69 50 72 65 76 54 72  nk page. iPrevTr
174f0 75 6e 6b 20 69 73 20 69 6e 69 74 69 61 6c 6c 79  unk is initially
174f1 20 31 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72   1..    */.    r
174f2 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
174f3 57 72 69 74 65 28 70 50 61 67 65 31 2d 3e 70 44  Write(pPage1->pD
174f4 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20  bPage);.    if( 
174f5 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
174f6 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50      put4byte(&pP
174f7 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c  age1->aData[36],
174f8 20 6e 2d 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 54   n-1);..    /* T
174f9 68 65 20 63 6f 64 65 20 77 69 74 68 69 6e 20 74  he code within t
174fa 68 69 73 20 6c 6f 6f 70 20 69 73 20 72 75 6e 20  his loop is run 
174fb 6f 6e 6c 79 20 6f 6e 63 65 20 69 66 20 74 68 65  only once if the
174fc 20 27 73 65 61 72 63 68 4c 69 73 74 27 20 76 61   'searchList' va
174fd 72 69 61 62 6c 65 0a 20 20 20 20 2a 2a 20 69 73  riable.    ** is
174fe 20 6e 6f 74 20 74 72 75 65 2e 20 4f 74 68 65 72   not true. Other
174ff 77 69 73 65 2c 20 69 74 20 72 75 6e 73 20 6f 6e  wise, it runs on
17500 63 65 20 66 6f 72 20 65 61 63 68 20 74 72 75 6e  ce for each trun
17501 6b 2d 70 61 67 65 20 6f 6e 20 74 68 65 0a 20 20  k-page on the.  
17502 20 20 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 75    ** free-list u
17503 6e 74 69 6c 20 74 68 65 20 70 61 67 65 20 27 6e  ntil the page 'n
17504 65 61 72 62 79 27 20 69 73 20 6c 6f 63 61 74 65  earby' is locate
17505 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 6f  d..    */.    do
17506 20 7b 0a 20 20 20 20 20 20 70 50 72 65 76 54 72   {.      pPrevTr
17507 75 6e 6b 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20  unk = pTrunk;.  
17508 20 20 20 20 69 66 28 20 70 50 72 65 76 54 72 75      if( pPrevTru
17509 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 54  nk ){.        iT
1750a 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28  runk = get4byte(
1750b 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61  &pPrevTrunk->aDa
1750c 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20 7d 65  ta[0]);.      }e
1750d 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 54 72  lse{.        iTr
1750e 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26  unk = get4byte(&
1750f 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32  pPage1->aData[32
17510 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ]);.      }.    
17511 20 20 74 65 73 74 63 61 73 65 28 20 69 54 72 75    testcase( iTru
17512 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20  nk==mxPage );.  
17513 20 20 20 20 69 66 28 20 69 54 72 75 6e 6b 3e 6d      if( iTrunk>m
17514 78 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20  xPage ){.       
17515 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52   rc = SQLITE_COR
17516 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20  RUPT_BKPT;.     
17517 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
17518 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67  rc = btreeGetPag
17519 65 28 70 42 74 2c 20 69 54 72 75 6e 6b 2c 20 26  e(pBt, iTrunk, &
1751a 70 54 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20  pTrunk, 0);.    
1751b 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63    }.      if( rc
1751c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 72 75   ){.        pTru
1751d 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  nk = 0;.        
1751e 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74  goto end_allocat
1751f 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a  e_page;.      }.
17520 0a 20 20 20 20 20 20 6b 20 3d 20 67 65 74 34 62  .      k = get4b
17521 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61  yte(&pTrunk->aDa
17522 74 61 5b 34 5d 29 3b 0a 20 20 20 20 20 20 69 66  ta[4]);.      if
17523 28 20 6b 3d 3d 30 20 26 26 20 21 73 65 61 72 63  ( k==0 && !searc
17524 68 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20  hList ){.       
17525 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b 20 68 61   /* The trunk ha
17526 73 20 6e 6f 20 6c 65 61 76 65 73 20 61 6e 64 20  s no leaves and 
17527 74 68 65 20 6c 69 73 74 20 69 73 20 6e 6f 74 20  the list is not 
17528 62 65 69 6e 67 20 73 65 61 72 63 68 65 64 2e 20  being searched. 
17529 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 6f 20 65  .        ** So e
1752a 78 74 72 61 63 74 20 74 68 65 20 74 72 75 6e 6b  xtract the trunk
1752b 20 70 61 67 65 20 69 74 73 65 6c 66 20 61 6e 64   page itself and
1752c 20 75 73 65 20 69 74 20 61 73 20 74 68 65 20 6e   use it as the n
1752d 65 77 6c 79 20 0a 20 20 20 20 20 20 20 20 2a 2a  ewly .        **
1752e 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20   allocated page 
1752f 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  */.        asser
17530 74 28 20 70 50 72 65 76 54 72 75 6e 6b 3d 3d 30  t( pPrevTrunk==0
17531 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   );.        rc =
17532 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
17533 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61  te(pTrunk->pDbPa
17534 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ge);.        if(
17535 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20   rc ){.         
17536 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61   goto end_alloca
17537 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20  te_page;.       
17538 20 7d 0a 20 20 20 20 20 20 20 20 2a 70 50 67 6e   }.        *pPgn
17539 6f 20 3d 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20  o = iTrunk;.    
1753a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67      memcpy(&pPag
1753b 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 26  e1->aData[32], &
1753c 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d  pTrunk->aData[0]
1753d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 2a 70  , 4);.        *p
1753e 70 50 61 67 65 20 3d 20 70 54 72 75 6e 6b 3b 0a  pPage = pTrunk;.
1753f 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d          pTrunk =
17540 20 30 3b 0a 20 20 20 20 20 20 20 20 54 52 41 43   0;.        TRAC
17541 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64  E(("ALLOCATE: %d
17542 20 74 72 75 6e 6b 20 2d 20 25 64 20 66 72 65 65   trunk - %d free
17543 20 70 61 67 65 73 20 6c 65 66 74 5c 6e 22 2c 20   pages left\n", 
17544 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20  *pPgno, n-1));. 
17545 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6b       }else if( k
17546 3e 28 75 33 32 29 28 70 42 74 2d 3e 75 73 61 62  >(u32)(pBt->usab
17547 6c 65 53 69 7a 65 2f 34 20 2d 20 32 29 20 29 7b  leSize/4 - 2) ){
17548 0a 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75  .        /* Valu
17549 65 20 6f 66 20 6b 20 69 73 20 6f 75 74 20 6f 66  e of k is out of
1754a 20 72 61 6e 67 65 2e 20 20 44 61 74 61 62 61 73   range.  Databas
1754b 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 2a 2f 0a  e corruption */.
1754c 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
1754d 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1754e 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65  ;.        goto e
1754f 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65  nd_allocate_page
17550 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
17551 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
17552 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
17553 20 73 65 61 72 63 68 4c 69 73 74 20 26 26 20 6e   searchList && n
17554 65 61 72 62 79 3d 3d 69 54 72 75 6e 6b 20 29 7b  earby==iTrunk ){
17555 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
17556 6c 69 73 74 20 69 73 20 62 65 69 6e 67 20 73 65  list is being se
17557 61 72 63 68 65 64 20 61 6e 64 20 74 68 69 73 20  arched and this 
17558 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20 74 68  trunk page is th
17559 65 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a  e page.        *
1755a 2a 20 74 6f 20 61 6c 6c 6f 63 61 74 65 2c 20 72  * to allocate, r
1755b 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65  egardless of whe
1755c 74 68 65 72 20 69 74 20 68 61 73 20 6c 65 61 76  ther it has leav
1755d 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  es..        */. 
1755e 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2a         assert( *
1755f 70 50 67 6e 6f 3d 3d 69 54 72 75 6e 6b 20 29 3b  pPgno==iTrunk );
17560 0a 20 20 20 20 20 20 20 20 2a 70 70 50 61 67 65  .        *ppPage
17561 20 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20   = pTrunk;.     
17562 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20     searchList = 
17563 30 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  0;.        rc = 
17564 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
17565 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67  e(pTrunk->pDbPag
17566 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  e);.        if( 
17567 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  rc ){.          
17568 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74  goto end_allocat
17569 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20  e_page;.        
1756a 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6b 3d  }.        if( k=
1756b 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
1756c 69 66 28 20 21 70 50 72 65 76 54 72 75 6e 6b 20  if( !pPrevTrunk 
1756d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d  ){.            m
1756e 65 6d 63 70 79 28 26 70 50 61 67 65 31 2d 3e 61  emcpy(&pPage1->a
1756f 44 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e  Data[32], &pTrun
17570 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b  k->aData[0], 4);
17571 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
17572 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d 65  {.            me
17573 6d 63 70 79 28 26 70 50 72 65 76 54 72 75 6e 6b  mcpy(&pPrevTrunk
17574 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26 70 54 72  ->aData[0], &pTr
17575 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34  unk->aData[0], 4
17576 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
17577 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
17578 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
17579 72 75 6e 6b 20 70 61 67 65 20 69 73 20 72 65 71  runk page is req
1757a 75 69 72 65 64 20 62 79 20 74 68 65 20 63 61 6c  uired by the cal
1757b 6c 65 72 20 62 75 74 20 69 74 20 63 6f 6e 74 61  ler but it conta
1757c 69 6e 73 20 0a 20 20 20 20 20 20 20 20 20 20 2a  ins .          *
1757d 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 66 72  * pointers to fr
1757e 65 65 2d 6c 69 73 74 20 6c 65 61 76 65 73 2e 20  ee-list leaves. 
1757f 54 68 65 20 66 69 72 73 74 20 6c 65 61 66 20 62  The first leaf b
17580 65 63 6f 6d 65 73 20 61 20 74 72 75 6e 6b 0a 20  ecomes a trunk. 
17581 20 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65           ** page
17582 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20   in this case.. 
17583 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
17584 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70        MemPage *p
17585 4e 65 77 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20  NewTrunk;.      
17586 20 20 20 20 50 67 6e 6f 20 69 4e 65 77 54 72 75      Pgno iNewTru
17587 6e 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  nk = get4byte(&p
17588 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d 29  Trunk->aData[8])
17589 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
1758a 69 4e 65 77 54 72 75 6e 6b 3e 6d 78 50 61 67 65  iNewTrunk>mxPage
1758b 20 29 7b 20 0a 20 20 20 20 20 20 20 20 20 20 20   ){ .           
1758c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52   rc = SQLITE_COR
1758d 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20  RUPT_BKPT;.     
1758e 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
1758f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20  allocate_page;. 
17590 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
17591 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
17592 4e 65 77 54 72 75 6e 6b 3d 3d 6d 78 50 61 67 65  NewTrunk==mxPage
17593 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63   );.          rc
17594 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28   = btreeGetPage(
17595 70 42 74 2c 20 69 4e 65 77 54 72 75 6e 6b 2c 20  pBt, iNewTrunk, 
17596 26 70 4e 65 77 54 72 75 6e 6b 2c 20 30 29 3b 0a  &pNewTrunk, 0);.
17597 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
17598 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
17599 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20             goto 
1759a 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67  end_allocate_pag
1759b 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  e;.          }. 
1759c 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
1759d 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
1759e 70 4e 65 77 54 72 75 6e 6b 2d 3e 70 44 62 50 61  pNewTrunk->pDbPa
1759f 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ge);.          i
175a0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
175a1 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
175a2 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77  releasePage(pNew
175a3 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20  Trunk);.        
175a4 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c      goto end_all
175a5 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20  ocate_page;.    
175a6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
175a7 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 54 72    memcpy(&pNewTr
175a8 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 26  unk->aData[0], &
175a9 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d  pTrunk->aData[0]
175aa 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 4);.          
175ab 70 75 74 34 62 79 74 65 28 26 70 4e 65 77 54 72  put4byte(&pNewTr
175ac 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 6b  unk->aData[4], k
175ad 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6d  -1);.          m
175ae 65 6d 63 70 79 28 26 70 4e 65 77 54 72 75 6e 6b  emcpy(&pNewTrunk
175af 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 26 70 54 72  ->aData[8], &pTr
175b0 75 6e 6b 2d 3e 61 44 61 74 61 5b 31 32 5d 2c 20  unk->aData[12], 
175b1 28 6b 2d 31 29 2a 34 29 3b 0a 20 20 20 20 20 20  (k-1)*4);.      
175b2 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
175b3 70 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20  pNewTrunk);.    
175b4 20 20 20 20 20 20 69 66 28 20 21 70 50 72 65 76        if( !pPrev
175b5 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20  Trunk ){.       
175b6 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c       assert( sql
175b7 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
175b8 61 62 6c 65 28 70 50 61 67 65 31 2d 3e 70 44 62  able(pPage1->pDb
175b9 50 61 67 65 29 20 29 3b 0a 20 20 20 20 20 20 20  Page) );.       
175ba 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70       put4byte(&p
175bb 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d  Page1->aData[32]
175bc 2c 20 69 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20  , iNewTrunk);.  
175bd 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
175be 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
175bf 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
175c0 65 28 70 50 72 65 76 54 72 75 6e 6b 2d 3e 70 44  e(pPrevTrunk->pD
175c1 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  bPage);.        
175c2 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
175c3 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f              goto
175c4 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61   end_allocate_pa
175c5 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ge;.            
175c6 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 75  }.            pu
175c7 74 34 62 79 74 65 28 26 70 50 72 65 76 54 72 75  t4byte(&pPrevTru
175c8 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 69 4e  nk->aData[0], iN
175c9 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20  ewTrunk);.      
175ca 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
175cb 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d          pTrunk =
175cc 20 30 3b 0a 20 20 20 20 20 20 20 20 54 52 41 43   0;.        TRAC
175cd 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64  E(("ALLOCATE: %d
175ce 20 74 72 75 6e 6b 20 2d 20 25 64 20 66 72 65 65   trunk - %d free
175cf 20 70 61 67 65 73 20 6c 65 66 74 5c 6e 22 2c 20   pages left\n", 
175d0 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 23  *pPgno, n-1));.#
175d1 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 65 6c 73  endif.      }els
175d2 65 20 69 66 28 20 6b 3e 30 20 29 7b 0a 20 20 20  e if( k>0 ){.   
175d3 20 20 20 20 20 2f 2a 20 45 78 74 72 61 63 74 20       /* Extract 
175d4 61 20 6c 65 61 66 20 66 72 6f 6d 20 74 68 65 20  a leaf from the 
175d5 74 72 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 20  trunk */.       
175d6 20 75 33 32 20 63 6c 6f 73 65 73 74 3b 0a 20 20   u32 closest;.  
175d7 20 20 20 20 20 20 50 67 6e 6f 20 69 50 61 67 65        Pgno iPage
175d8 3b 0a 20 20 20 20 20 20 20 20 75 6e 73 69 67 6e  ;.        unsign
175d9 65 64 20 63 68 61 72 20 2a 61 44 61 74 61 20 3d  ed char *aData =
175da 20 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 3b 0a   pTrunk->aData;.
175db 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
175dc 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
175dd 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b  Trunk->pDbPage);
175de 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20  .        if( rc 
175df 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  ){.          got
175e0 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70  o end_allocate_p
175e1 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  age;.        }. 
175e2 20 20 20 20 20 20 20 69 66 28 20 6e 65 61 72 62         if( nearb
175e3 79 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  y>0 ){.         
175e4 20 75 33 32 20 69 3b 0a 20 20 20 20 20 20 20 20   u32 i;.        
175e5 20 20 69 6e 74 20 64 69 73 74 3b 0a 20 20 20 20    int dist;.    
175e6 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20        closest = 
175e7 30 3b 0a 20 20 20 20 20 20 20 20 20 20 64 69 73  0;.          dis
175e8 74 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44  t = get4byte(&aD
175e9 61 74 61 5b 38 5d 29 20 2d 20 6e 65 61 72 62 79  ata[8]) - nearby
175ea 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
175eb 64 69 73 74 3c 30 20 29 20 64 69 73 74 20 3d 20  dist<0 ) dist = 
175ec 2d 64 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20  -dist;.         
175ed 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6b 3b 20 69   for(i=1; i<k; i
175ee 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
175ef 20 69 6e 74 20 64 32 20 3d 20 67 65 74 34 62 79   int d2 = get4by
175f0 74 65 28 26 61 44 61 74 61 5b 38 2b 69 2a 34 5d  te(&aData[8+i*4]
175f1 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20  ) - nearby;.    
175f2 20 20 20 20 20 20 20 20 69 66 28 20 64 32 3c 30          if( d2<0
175f3 20 29 20 64 32 20 3d 20 2d 64 32 3b 0a 20 20 20   ) d2 = -d2;.   
175f4 20 20 20 20 20 20 20 20 20 69 66 28 20 64 32 3c           if( d2<
175f5 64 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20  dist ){.        
175f6 20 20 20 20 20 20 63 6c 6f 73 65 73 74 20 3d 20        closest = 
175f7 69 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  i;.             
175f8 20 64 69 73 74 20 3d 20 64 32 3b 0a 20 20 20 20   dist = d2;.    
175f9 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
175fa 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65      }.        }e
175fb 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63  lse{.          c
175fc 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 20 20 20  losest = 0;.    
175fd 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 69      }..        i
175fe 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28  Page = get4byte(
175ff 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74  &aData[8+closest
17600 2a 34 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65  *4]);.        te
17601 73 74 63 61 73 65 28 20 69 50 61 67 65 3d 3d 6d  stcase( iPage==m
17602 78 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 20  xPage );.       
17603 20 69 66 28 20 69 50 61 67 65 3e 6d 78 50 61 67   if( iPage>mxPag
17604 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  e ){.          r
17605 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  c = SQLITE_CORRU
17606 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 20  PT_BKPT;.       
17607 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f     goto end_allo
17608 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20  cate_page;.     
17609 20 20 20 7d 0a 20 20 20 20 20 20 20 20 74 65 73     }.        tes
1760a 74 63 61 73 65 28 20 69 50 61 67 65 3d 3d 6d 78  tcase( iPage==mx
1760b 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 20 20  Page );.        
1760c 69 66 28 20 21 73 65 61 72 63 68 4c 69 73 74 20  if( !searchList 
1760d 7c 7c 20 69 50 61 67 65 3d 3d 6e 65 61 72 62 79  || iPage==nearby
1760e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   ){.          in
1760f 74 20 6e 6f 43 6f 6e 74 65 6e 74 3b 0a 20 20 20  t noContent;.   
17610 20 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20         *pPgno = 
17611 69 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20  iPage;.         
17612 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54   TRACE(("ALLOCAT
17613 45 3a 20 25 64 20 77 61 73 20 6c 65 61 66 20 25  E: %d was leaf %
17614 64 20 6f 66 20 25 64 20 6f 6e 20 74 72 75 6e 6b  d of %d on trunk
17615 20 25 64 22 0a 20 20 20 20 20 20 20 20 20 20 20   %d".           
17616 20 20 20 20 20 20 22 3a 20 25 64 20 6d 6f 72 65        ": %d more
17617 20 66 72 65 65 20 70 61 67 65 73 5c 6e 22 2c 0a   free pages\n",.
17618 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17619 20 2a 70 50 67 6e 6f 2c 20 63 6c 6f 73 65 73 74   *pPgno, closest
1761a 2b 31 2c 20 6b 2c 20 70 54 72 75 6e 6b 2d 3e 70  +1, k, pTrunk->p
1761b 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20  gno, n-1));.    
1761c 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 73        if( closes
1761d 74 3c 6b 2d 31 20 29 7b 0a 20 20 20 20 20 20 20  t<k-1 ){.       
1761e 20 20 20 20 20 6d 65 6d 63 70 79 28 26 61 44 61       memcpy(&aDa
1761f 74 61 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 2c  ta[8+closest*4],
17620 20 26 61 44 61 74 61 5b 34 2b 6b 2a 34 5d 2c 20   &aData[4+k*4], 
17621 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  4);.          }.
17622 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79            put4by
17623 74 65 28 26 61 44 61 74 61 5b 34 5d 2c 20 6b 2d  te(&aData[4], k-
17624 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73  1);.          as
17625 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
17626 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 54  erIswriteable(pT
17627 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 20 29  runk->pDbPage) )
17628 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 6f 43 6f  ;.          noCo
17629 6e 74 65 6e 74 20 3d 20 21 62 74 72 65 65 47 65  ntent = !btreeGe
1762a 74 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 2c  tHasContent(pBt,
1762b 20 2a 70 50 67 6e 6f 29 3b 0a 20 20 20 20 20 20   *pPgno);.      
1762c 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65      rc = btreeGe
1762d 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67 6e  tPage(pBt, *pPgn
1762e 6f 2c 20 70 70 50 61 67 65 2c 20 6e 6f 43 6f 6e  o, ppPage, noCon
1762f 74 65 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20  tent);.         
17630 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
17631 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
17632 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
17633 67 65 72 57 72 69 74 65 28 28 2a 70 70 50 61 67  gerWrite((*ppPag
17634 65 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  e)->pDbPage);.  
17635 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
17636 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
17637 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c               rel
17638 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 67 65  easePage(*ppPage
17639 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  );.            }
1763a 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1763b 20 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 73         searchLis
1763c 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  t = 0;.        }
1763d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
1763e 65 6c 65 61 73 65 50 61 67 65 28 70 50 72 65 76  eleasePage(pPrev
1763f 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 70 50  Trunk);.      pP
17640 72 65 76 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20  revTrunk = 0;.  
17641 20 20 7d 77 68 69 6c 65 28 20 73 65 61 72 63 68    }while( search
17642 4c 69 73 74 20 29 3b 0a 20 20 7d 65 6c 73 65 7b  List );.  }else{
17643 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61 72  .    /* There ar
17644 65 20 6e 6f 20 70 61 67 65 73 20 6f 6e 20 74 68  e no pages on th
17645 65 20 66 72 65 65 6c 69 73 74 2c 20 73 6f 20 63  e freelist, so c
17646 72 65 61 74 65 20 61 20 6e 65 77 20 70 61 67 65  reate a new page
17647 20 61 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 65   at the.    ** e
17648 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a  nd of the file *
17649 2f 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 65 20  /.    int nPage 
1764a 3d 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74  = pagerPagecount
1764b 28 70 42 74 29 3b 0a 20 20 20 20 2a 70 50 67 6e  (pBt);.    *pPgn
1764c 6f 20 3d 20 6e 50 61 67 65 20 2b 20 31 3b 0a 0a  o = nPage + 1;..
1764d 20 20 20 20 69 66 28 20 2a 70 50 67 6e 6f 3d 3d      if( *pPgno==
1764e 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
1764f 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20  E(pBt) ){.      
17650 28 2a 70 50 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20  (*pPgno)++;.    
17651 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
17652 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
17653 4d 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61  M.    if( pBt->a
17654 75 74 6f 56 61 63 75 75 6d 20 26 26 20 50 54 52  utoVacuum && PTR
17655 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20  MAP_ISPAGE(pBt, 
17656 2a 70 50 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20  *pPgno) ){.     
17657 20 2f 2a 20 49 66 20 2a 70 50 67 6e 6f 20 72 65   /* If *pPgno re
17658 66 65 72 73 20 74 6f 20 61 20 70 6f 69 6e 74 65  fers to a pointe
17659 72 2d 6d 61 70 20 70 61 67 65 2c 20 61 6c 6c 6f  r-map page, allo
1765a 63 61 74 65 20 74 77 6f 20 6e 65 77 20 70 61 67  cate two new pag
1765b 65 73 0a 20 20 20 20 20 20 2a 2a 20 61 74 20 74  es.      ** at t
1765c 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69  he end of the fi
1765d 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 6f 6e  le instead of on
1765e 65 2e 20 54 68 65 20 66 69 72 73 74 20 61 6c 6c  e. The first all
1765f 6f 63 61 74 65 64 20 70 61 67 65 0a 20 20 20 20  ocated page.    
17660 20 20 2a 2a 20 62 65 63 6f 6d 65 73 20 61 20 6e    ** becomes a n
17661 65 77 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70  ew pointer-map p
17662 61 67 65 2c 20 74 68 65 20 73 65 63 6f 6e 64 20  age, the second 
17663 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 63  is used by the c
17664 61 6c 6c 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a  aller..      */.
17665 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70        MemPage *p
17666 50 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 54 52  Pg = 0;.      TR
17667 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20  ACE(("ALLOCATE: 
17668 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66 20 66  %d from end of f
17669 69 6c 65 20 28 70 6f 69 6e 74 65 72 2d 6d 61 70  ile (pointer-map
1766a 20 70 61 67 65 29 5c 6e 22 2c 20 2a 70 50 67 6e   page)\n", *pPgn
1766b 6f 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  o));.      asser
1766c 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49  t( *pPgno!=PENDI
1766d 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
1766e 29 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  ) );.      rc = 
1766f 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74  btreeGetPage(pBt
17670 2c 20 2a 70 50 67 6e 6f 2c 20 26 70 50 67 2c 20  , *pPgno, &pPg, 
17671 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  0);.      if( rc
17672 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
17673 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
17674 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
17675 67 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  g->pDbPage);.   
17676 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
17677 28 70 50 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20  (pPg);.      }. 
17678 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65       if( rc ) re
17679 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 28  turn rc;.      (
1767a 2a 70 50 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 20  *pPgno)++;.     
1767b 20 69 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e   if( *pPgno==PEN
1767c 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
1767d 42 74 29 20 29 7b 20 28 2a 70 50 67 6e 6f 29 2b  Bt) ){ (*pPgno)+
1767e 2b 3b 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69  +; }.    }.#endi
1767f 66 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 2a  f..    assert( *
17680 70 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42  pPgno!=PENDING_B
17681 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b  YTE_PAGE(pBt) );
17682 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47  .    rc = btreeG
17683 65 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67  etPage(pBt, *pPg
17684 6e 6f 2c 20 70 70 50 61 67 65 2c 20 30 29 3b 0a  no, ppPage, 0);.
17685 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
17686 75 72 6e 20 72 63 3b 0a 20 20 20 20 72 63 20 3d  urn rc;.    rc =
17687 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
17688 74 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44  te((*ppPage)->pD
17689 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20  bPage);.    if( 
1768a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1768b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
1768c 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20  ge(*ppPage);.   
1768d 20 7d 0a 20 20 20 20 54 52 41 43 45 28 28 22 41   }.    TRACE(("A
1768e 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d  LLOCATE: %d from
1768f 20 65 6e 64 20 6f 66 20 66 69 6c 65 5c 6e 22 2c   end of file\n",
17690 20 2a 70 50 67 6e 6f 29 29 3b 0a 20 20 7d 0a 0a   *pPgno));.  }..
17691 20 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f    assert( *pPgno
17692 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  !=PENDING_BYTE_P
17693 41 47 45 28 70 42 74 29 20 29 3b 0a 0a 65 6e 64  AGE(pBt) );..end
17694 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3a 0a  _allocate_page:.
17695 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54    releasePage(pT
17696 72 75 6e 6b 29 3b 0a 20 20 72 65 6c 65 61 73 65  runk);.  release
17697 50 61 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29  Page(pPrevTrunk)
17698 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
17699 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
1769a 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
1769b 65 52 65 66 63 6f 75 6e 74 28 28 2a 70 70 50 61  eRefcount((*ppPa
1769c 67 65 29 2d 3e 70 44 62 50 61 67 65 29 3e 31 20  ge)->pDbPage)>1 
1769d 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  ){.      release
1769e 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20  Page(*ppPage);. 
1769f 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
176a0 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
176a1 0a 20 20 20 20 7d 0a 20 20 20 20 28 2a 70 70 50  .    }.    (*ppP
176a2 61 67 65 29 2d 3e 69 73 49 6e 69 74 20 3d 20 30  age)->isInit = 0
176a3 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a  ;.  }else{.    *
176a4 70 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a  ppPage = 0;.  }.
176a5 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
176a6 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
176a7 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 61  ion is used to a
176a8 64 64 20 70 61 67 65 20 69 50 61 67 65 20 74 6f  dd page iPage to
176a9 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
176aa 6c 65 20 66 72 65 65 2d 6c 69 73 74 2e 20 0a 2a  le free-list. .*
176ab 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20  * It is assumed 
176ac 74 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73  that the page is
176ad 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 20 70   not already a p
176ae 61 72 74 20 6f 66 20 74 68 65 20 66 72 65 65 2d  art of the free-
176af 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  list..**.** The 
176b0 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73 20  value passed as 
176b1 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
176b2 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63  ent to this func
176b3 74 69 6f 6e 20 69 73 20 6f 70 74 69 6f 6e 61 6c  tion is optional
176b4 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c  ..** If the call
176b5 65 72 20 68 61 70 70 65 6e 73 20 74 6f 20 68 61  er happens to ha
176b6 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ve a pointer to 
176b7 74 68 65 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65  the MemPage obje
176b8 63 74 20 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e  ct .** correspon
176b9 64 69 6e 67 20 74 6f 20 70 61 67 65 20 69 50 61  ding to page iPa
176ba 67 65 20 68 61 6e 64 79 2c 20 69 74 20 6d 61 79  ge handy, it may
176bb 20 70 61 73 73 20 69 74 20 61 73 20 74 68 65 20   pass it as the 
176bc 73 65 63 6f 6e 64 20 76 61 6c 75 65 2e 20 0a 2a  second value. .*
176bd 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20  * Otherwise, it 
176be 6d 61 79 20 70 61 73 73 20 4e 55 4c 4c 2e 0a 2a  may pass NULL..*
176bf 2a 0a 2a 2a 20 49 66 20 61 20 70 6f 69 6e 74 65  *.** If a pointe
176c0 72 20 74 6f 20 61 20 4d 65 6d 50 61 67 65 20 6f  r to a MemPage o
176c1 62 6a 65 63 74 20 69 73 20 70 61 73 73 65 64 20  bject is passed 
176c2 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  as the second ar
176c3 67 75 6d 65 6e 74 2c 0a 2a 2a 20 69 74 73 20 72  gument,.** its r
176c4 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 69  eference count i
176c5 73 20 6e 6f 74 20 61 6c 74 65 72 65 64 20 62 79  s not altered by
176c6 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a   this function..
176c7 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 72  */.static int fr
176c8 65 65 50 61 67 65 32 28 42 74 53 68 61 72 65 64  eePage2(BtShared
176c9 20 2a 70 42 74 2c 20 4d 65 6d 50 61 67 65 20 2a   *pBt, MemPage *
176ca 70 4d 65 6d 50 61 67 65 2c 20 50 67 6e 6f 20 69  pMemPage, Pgno i
176cb 50 61 67 65 29 7b 0a 20 20 4d 65 6d 50 61 67 65  Page){.  MemPage
176cc 20 2a 70 54 72 75 6e 6b 20 3d 20 30 3b 20 20 20   *pTrunk = 0;   
176cd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
176ce 46 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20  Free-list trunk 
176cf 70 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69  page */.  Pgno i
176d0 54 72 75 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20  Trunk = 0;      
176d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
176d2 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   Page number of 
176d3 66 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20  free-list trunk 
176d4 70 61 67 65 20 2a 2f 20 0a 20 20 4d 65 6d 50 61  page */ .  MemPa
176d5 67 65 20 2a 70 50 61 67 65 31 20 3d 20 70 42 74  ge *pPage1 = pBt
176d6 2d 3e 70 50 61 67 65 31 3b 20 20 20 20 20 20 2f  ->pPage1;      /
176d7 2a 20 4c 6f 63 61 6c 20 72 65 66 65 72 65 6e 63  * Local referenc
176d8 65 20 74 6f 20 70 61 67 65 20 31 20 2a 2f 0a 20  e to page 1 */. 
176d9 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
176da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
176db 20 20 20 20 20 2f 2a 20 50 61 67 65 20 62 65 69       /* Page bei
176dc 6e 67 20 66 72 65 65 64 2e 20 4d 61 79 20 62 65  ng freed. May be
176dd 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 6e 74 20   NULL. */.  int 
176de 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
176df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
176e0 2f 2a 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a  /* Return Code *
176e1 2f 0a 20 20 69 6e 74 20 6e 46 72 65 65 3b 20 20  /.  int nFree;  
176e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
176e3 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69          /* Initi
176e4 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  al number of pag
176e5 65 73 20 6f 6e 20 66 72 65 65 2d 6c 69 73 74 20  es on free-list 
176e6 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  */..  assert( sq
176e7 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
176e8 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
176e9 20 20 61 73 73 65 72 74 28 20 69 50 61 67 65 3e    assert( iPage>
176ea 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  1 );.  assert( !
176eb 70 4d 65 6d 50 61 67 65 20 7c 7c 20 70 4d 65 6d  pMemPage || pMem
176ec 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 69 50 61 67  Page->pgno==iPag
176ed 65 20 29 3b 0a 0a 20 20 69 66 28 20 70 4d 65 6d  e );..  if( pMem
176ee 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61 67  Page ){.    pPag
176ef 65 20 3d 20 70 4d 65 6d 50 61 67 65 3b 0a 20 20  e = pMemPage;.  
176f0 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65    sqlite3PagerRe
176f1 66 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  f(pPage->pDbPage
176f2 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
176f3 70 50 61 67 65 20 3d 20 62 74 72 65 65 50 61 67  pPage = btreePag
176f4 65 4c 6f 6f 6b 75 70 28 70 42 74 2c 20 69 50 61  eLookup(pBt, iPa
176f5 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  ge);.  }..  /* I
176f6 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 66 72 65  ncrement the fre
176f7 65 20 70 61 67 65 20 63 6f 75 6e 74 20 6f 6e 20  e page count on 
176f8 70 50 61 67 65 31 20 2a 2f 0a 20 20 72 63 20 3d  pPage1 */.  rc =
176f9 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
176fa 74 65 28 70 50 61 67 65 31 2d 3e 70 44 62 50 61  te(pPage1->pDbPa
176fb 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  ge);.  if( rc ) 
176fc 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75  goto freepage_ou
176fd 74 3b 0a 20 20 6e 46 72 65 65 20 3d 20 67 65 74  t;.  nFree = get
176fe 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
176ff 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 70 75 74  Data[36]);.  put
17700 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
17701 44 61 74 61 5b 33 36 5d 2c 20 6e 46 72 65 65 2b  Data[36], nFree+
17702 31 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  1);..#ifdef SQLI
17703 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45  TE_SECURE_DELETE
17704 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 53 51 4c  .  /* If the SQL
17705 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54  ITE_SECURE_DELET
17706 45 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f  E compile-time o
17707 70 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64  ption is enabled
17708 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 61 6c 77 61  , then.  ** alwa
17709 79 73 20 66 75 6c 6c 79 20 6f 76 65 72 77 72 69  ys fully overwri
1770a 74 65 20 64 65 6c 65 74 65 64 20 69 6e 66 6f 72  te deleted infor
1770b 6d 61 74 69 6f 6e 20 77 69 74 68 20 7a 65 72 6f  mation with zero
1770c 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 21  s..  */.  if( (!
1770d 70 50 61 67 65 20 26 26 20 28 72 63 20 3d 20 62  pPage && (rc = b
1770e 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
1770f 20 69 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20   iPage, &pPage, 
17710 30 29 29 29 0a 20 20 20 7c 7c 20 20 20 20 20 20  0))).   ||      
17711 20 20 20 20 20 20 28 72 63 20 3d 20 73 71 6c 69        (rc = sqli
17712 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
17713 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29 0a 20  age->pDbPage)). 
17714 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 72 65   ){.    goto fre
17715 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20  epage_out;.  }. 
17716 20 6d 65 6d 73 65 74 28 70 50 61 67 65 2d 3e 61   memset(pPage->a
17717 44 61 74 61 2c 20 30 2c 20 70 50 61 67 65 2d 3e  Data, 0, pPage->
17718 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a  pBt->pageSize);.
17719 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20  #endif..  /* If 
1771a 74 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70  the database sup
1771b 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75  ports auto-vacuu
1771c 6d 2c 20 77 72 69 74 65 20 61 6e 20 65 6e 74 72  m, write an entr
1771d 79 20 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72  y in the pointer
1771e 2d 6d 61 70 0a 20 20 2a 2a 20 74 6f 20 69 6e 64  -map.  ** to ind
1771f 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20 70  icate that the p
17720 61 67 65 20 69 73 20 66 72 65 65 2e 0a 20 20 2a  age is free..  *
17721 2f 0a 20 20 69 66 28 20 49 53 41 55 54 4f 56 41  /.  if( ISAUTOVA
17722 43 55 55 4d 20 29 7b 0a 20 20 20 20 70 74 72 6d  CUUM ){.    ptrm
17723 61 70 50 75 74 28 70 42 74 2c 20 69 50 61 67 65  apPut(pBt, iPage
17724 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47  , PTRMAP_FREEPAG
17725 45 2c 20 30 2c 20 26 72 63 29 3b 0a 20 20 20 20  E, 0, &rc);.    
17726 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 66 72  if( rc ) goto fr
17727 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a  eepage_out;.  }.
17728 0a 20 20 2f 2a 20 4e 6f 77 20 6d 61 6e 69 70 75  .  /* Now manipu
17729 6c 61 74 65 20 74 68 65 20 61 63 74 75 61 6c 20  late the actual 
1772a 64 61 74 61 62 61 73 65 20 66 72 65 65 2d 6c 69  database free-li
1772b 73 74 20 73 74 72 75 63 74 75 72 65 2e 20 54 68  st structure. Th
1772c 65 72 65 20 61 72 65 20 74 77 6f 0a 20 20 2a 2a  ere are two.  **
1772d 20 70 6f 73 73 69 62 69 6c 69 74 69 65 73 2e 20   possibilities. 
1772e 49 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  If the free-list
1772f 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 65 6d   is currently em
17730 70 74 79 2c 20 6f 72 20 69 66 20 74 68 65 20 66  pty, or if the f
17731 69 72 73 74 0a 20 20 2a 2a 20 74 72 75 6e 6b 20  irst.  ** trunk 
17732 70 61 67 65 20 69 6e 20 74 68 65 20 66 72 65 65  page in the free
17733 2d 6c 69 73 74 20 69 73 20 66 75 6c 6c 2c 20 74  -list is full, t
17734 68 65 6e 20 74 68 69 73 20 70 61 67 65 20 77 69  hen this page wi
17735 6c 6c 20 62 65 63 6f 6d 65 20 61 0a 20 20 2a 2a  ll become a.  **
17736 20 6e 65 77 20 66 72 65 65 2d 6c 69 73 74 20 74   new free-list t
17737 72 75 6e 6b 20 70 61 67 65 2e 20 4f 74 68 65 72  runk page. Other
17738 77 69 73 65 2c 20 69 74 20 77 69 6c 6c 20 62 65  wise, it will be
17739 63 6f 6d 65 20 61 20 6c 65 61 66 20 6f 66 20 74  come a leaf of t
1773a 68 65 0a 20 20 2a 2a 20 66 69 72 73 74 20 74 72  he.  ** first tr
1773b 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20  unk page in the 
1773c 63 75 72 72 65 6e 74 20 66 72 65 65 2d 6c 69 73  current free-lis
1773d 74 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 74 65  t. This block te
1773e 73 74 73 20 69 66 20 69 74 0a 20 20 2a 2a 20 69  sts if it.  ** i
1773f 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 61 64  s possible to ad
17740 64 20 74 68 65 20 70 61 67 65 20 61 73 20 61 20  d the page as a 
17741 6e 65 77 20 66 72 65 65 2d 6c 69 73 74 20 6c 65  new free-list le
17742 61 66 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e  af..  */.  if( n
17743 46 72 65 65 21 3d 30 20 29 7b 0a 20 20 20 20 75  Free!=0 ){.    u
17744 33 32 20 6e 4c 65 61 66 3b 20 20 20 20 20 20 20  32 nLeaf;       
17745 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74           /* Init
17746 69 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65  ial number of le
17747 61 66 20 63 65 6c 6c 73 20 6f 6e 20 74 72 75 6e  af cells on trun
17748 6b 20 70 61 67 65 20 2a 2f 0a 0a 20 20 20 20 69  k page */..    i
17749 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74 65  Trunk = get4byte
1774a 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  (&pPage1->aData[
1774b 33 32 5d 29 3b 0a 20 20 20 20 72 63 20 3d 20 62  32]);.    rc = b
1774c 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
1774d 20 69 54 72 75 6e 6b 2c 20 26 70 54 72 75 6e 6b   iTrunk, &pTrunk
1774e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , 0);.    if( rc
1774f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
17750 20 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61       goto freepa
17751 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20  ge_out;.    }.. 
17752 20 20 20 6e 4c 65 61 66 20 3d 20 67 65 74 34 62     nLeaf = get4b
17753 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61  yte(&pTrunk->aDa
17754 74 61 5b 34 5d 29 3b 0a 20 20 20 20 61 73 73 65  ta[4]);.    asse
17755 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  rt( pBt->usableS
17756 69 7a 65 3e 33 32 20 29 3b 0a 20 20 20 20 69 66  ize>32 );.    if
17757 28 20 6e 4c 65 61 66 20 3e 20 28 75 33 32 29 70  ( nLeaf > (u32)p
17758 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34  Bt->usableSize/4
17759 20 2d 20 32 20 29 7b 0a 20 20 20 20 20 20 72 63   - 2 ){.      rc
1775a 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
1775b 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f  T_BKPT;.      go
1775c 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b  to freepage_out;
1775d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e  .    }.    if( n
1775e 4c 65 61 66 20 3c 20 28 75 33 32 29 70 42 74 2d  Leaf < (u32)pBt-
1775f 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20  >usableSize/4 - 
17760 38 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e  8 ){.      /* In
17761 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65   this case there
17762 20 69 73 20 72 6f 6f 6d 20 6f 6e 20 74 68 65 20   is room on the 
17763 74 72 75 6e 6b 20 70 61 67 65 20 74 6f 20 69 6e  trunk page to in
17764 73 65 72 74 20 74 68 65 20 70 61 67 65 0a 20 20  sert the page.  
17765 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 66 72 65      ** being fre
17766 65 64 20 61 73 20 61 20 6e 65 77 20 6c 65 61 66  ed as a new leaf
17767 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
17768 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68   ** Note that th
17769 65 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73 20  e trunk page is 
1776a 6e 6f 74 20 72 65 61 6c 6c 79 20 66 75 6c 6c 20  not really full 
1776b 75 6e 74 69 6c 20 69 74 20 63 6f 6e 74 61 69 6e  until it contain
1776c 73 0a 20 20 20 20 20 20 2a 2a 20 75 73 61 62 6c  s.      ** usabl
1776d 65 53 69 7a 65 2f 34 20 2d 20 32 20 65 6e 74 72  eSize/4 - 2 entr
1776e 69 65 73 2c 20 6e 6f 74 20 75 73 61 62 6c 65 53  ies, not usableS
1776f 69 7a 65 2f 34 20 2d 20 38 20 65 6e 74 72 69 65  ize/4 - 8 entrie
17770 73 20 61 73 20 77 65 20 68 61 76 65 0a 20 20 20  s as we have.   
17771 20 20 20 2a 2a 20 63 6f 64 65 64 2e 20 20 42 75     ** coded.  Bu
17772 74 20 64 75 65 20 74 6f 20 61 20 63 6f 64 69 6e  t due to a codin
17773 67 20 65 72 72 6f 72 20 69 6e 20 76 65 72 73 69  g error in versi
17774 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 70 72  ons of SQLite pr
17775 69 6f 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  ior to.      ** 
17776 33 2e 36 2e 30 2c 20 64 61 74 61 62 61 73 65 73  3.6.0, databases
17777 20 77 69 74 68 20 66 72 65 65 6c 69 73 74 20 74   with freelist t
17778 72 75 6e 6b 20 70 61 67 65 73 20 68 6f 6c 64 69  runk pages holdi
17779 6e 67 20 6d 6f 72 65 20 74 68 61 6e 0a 20 20 20  ng more than.   
1777a 20 20 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65     ** usableSize
1777b 2f 34 20 2d 20 38 20 65 6e 74 72 69 65 73 20 77  /4 - 8 entries w
1777c 69 6c 6c 20 62 65 20 72 65 70 6f 72 74 65 64 20  ill be reported 
1777d 61 73 20 63 6f 72 72 75 70 74 2e 20 20 49 6e 20  as corrupt.  In 
1777e 6f 72 64 65 72 0a 20 20 20 20 20 20 2a 2a 20 74  order.      ** t
1777f 6f 20 6d 61 69 6e 74 61 69 6e 20 62 61 63 6b 77  o maintain backw
17780 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69  ards compatibili
17781 74 79 20 77 69 74 68 20 6f 6c 64 65 72 20 76 65  ty with older ve
17782 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65  rsions of SQLite
17783 2c 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 77 69  ,.      ** we wi
17784 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 72  ll continue to r
17785 65 73 74 72 69 63 74 20 74 68 65 20 6e 75 6d 62  estrict the numb
17786 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 74 6f  er of entries to
17787 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20   usableSize/4 - 
17788 38 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 6e  8.      ** for n
17789 6f 77 2e 20 20 41 74 20 73 6f 6d 65 20 70 6f 69  ow.  At some poi
1778a 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 65  nt in the future
1778b 20 28 6f 6e 63 65 20 65 76 65 72 79 6f 6e 65 20   (once everyone 
1778c 68 61 73 20 75 70 67 72 61 64 65 64 0a 20 20 20  has upgraded.   
1778d 20 20 20 2a 2a 20 74 6f 20 33 2e 36 2e 30 20 6f     ** to 3.6.0 o
1778e 72 20 6c 61 74 65 72 29 20 77 65 20 73 68 6f 75  r later) we shou
1778f 6c 64 20 63 6f 6e 73 69 64 65 72 20 66 69 78 69  ld consider fixi
17790 6e 67 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e  ng the condition
17791 61 6c 20 61 62 6f 76 65 0a 20 20 20 20 20 20 2a  al above.      *
17792 2a 20 74 6f 20 72 65 61 64 20 22 75 73 61 62 6c  * to read "usabl
17793 65 53 69 7a 65 2f 34 2d 32 22 20 69 6e 73 74 65  eSize/4-2" inste
17794 61 64 20 6f 66 20 22 75 73 61 62 6c 65 53 69 7a  ad of "usableSiz
17795 65 2f 34 2d 38 22 2e 0a 20 20 20 20 20 20 2a 2f  e/4-8"..      */
17796 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
17797 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54  te3PagerWrite(pT
17798 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a  runk->pDbPage);.
17799 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
1779a 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1779b 20 20 20 70 75 74 34 62 79 74 65 28 26 70 54 72     put4byte(&pTr
1779c 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 6e  unk->aData[4], n
1779d 4c 65 61 66 2b 31 29 3b 0a 20 20 20 20 20 20 20  Leaf+1);.       
1779e 20 70 75 74 34 62 79 74 65 28 26 70 54 72 75 6e   put4byte(&pTrun
1779f 6b 2d 3e 61 44 61 74 61 5b 38 2b 6e 4c 65 61 66  k->aData[8+nLeaf
177a0 2a 34 5d 2c 20 69 50 61 67 65 29 3b 0a 23 69 66  *4], iPage);.#if
177a1 6e 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43 55  ndef SQLITE_SECU
177a2 52 45 5f 44 45 4c 45 54 45 0a 20 20 20 20 20 20  RE_DELETE.      
177a3 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20    if( pPage ){. 
177a4 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
177a5 50 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 70  PagerDontWrite(p
177a6 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a  Page->pDbPage);.
177a7 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66          }.#endif
177a8 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 74  .        rc = bt
177a9 72 65 65 53 65 74 48 61 73 43 6f 6e 74 65 6e 74  reeSetHasContent
177aa 28 70 42 74 2c 20 69 50 61 67 65 29 3b 0a 20 20  (pBt, iPage);.  
177ab 20 20 20 20 7d 0a 20 20 20 20 20 20 54 52 41 43      }.      TRAC
177ac 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a 20 25  E(("FREE-PAGE: %
177ad 64 20 6c 65 61 66 20 6f 6e 20 74 72 75 6e 6b 20  d leaf on trunk 
177ae 70 61 67 65 20 25 64 5c 6e 22 2c 70 50 61 67 65  page %d\n",pPage
177af 2d 3e 70 67 6e 6f 2c 70 54 72 75 6e 6b 2d 3e 70  ->pgno,pTrunk->p
177b0 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 67 6f 74  gno));.      got
177b1 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a  o freepage_out;.
177b2 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
177b3 49 66 20 63 6f 6e 74 72 6f 6c 20 66 6c 6f 77 73  If control flows
177b4 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c 20   to this point, 
177b5 74 68 65 6e 20 69 74 20 77 61 73 20 6e 6f 74 20  then it was not 
177b6 70 6f 73 73 69 62 6c 65 20 74 6f 20 61 64 64 20  possible to add 
177b7 74 68 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 67  the.  ** the pag
177b8 65 20 62 65 69 6e 67 20 66 72 65 65 64 20 61 73  e being freed as
177b9 20 61 20 6c 65 61 66 20 70 61 67 65 20 6f 66 20   a leaf page of 
177ba 74 68 65 20 66 69 72 73 74 20 74 72 75 6e 6b 20  the first trunk 
177bb 69 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  in the free-list
177bc 2e 0a 20 20 2a 2a 20 50 6f 73 73 69 62 6c 79 20  ..  ** Possibly 
177bd 62 65 63 61 75 73 65 20 74 68 65 20 66 72 65 65  because the free
177be 2d 6c 69 73 74 20 69 73 20 65 6d 70 74 79 2c 20  -list is empty, 
177bf 6f 72 20 70 6f 73 73 69 62 6c 79 20 62 65 63 61  or possibly beca
177c0 75 73 65 20 74 68 65 20 0a 20 20 2a 2a 20 66 69  use the .  ** fi
177c1 72 73 74 20 74 72 75 6e 6b 20 69 6e 20 74 68 65  rst trunk in the
177c2 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 66 75   free-list is fu
177c3 6c 6c 2e 20 45 69 74 68 65 72 20 77 61 79 2c 20  ll. Either way, 
177c4 74 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 66  the page being f
177c5 72 65 65 64 0a 20 20 2a 2a 20 77 69 6c 6c 20 62  reed.  ** will b
177c6 65 63 6f 6d 65 20 74 68 65 20 6e 65 77 20 66 69  ecome the new fi
177c7 72 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 69  rst trunk page i
177c8 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e  n the free-list.
177c9 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67  .  */.  if( pPag
177ca 65 3d 3d 30 20 26 26 20 53 51 4c 49 54 45 5f 4f  e==0 && SQLITE_O
177cb 4b 21 3d 28 72 63 20 3d 20 62 74 72 65 65 47 65  K!=(rc = btreeGe
177cc 74 50 61 67 65 28 70 42 74 2c 20 69 50 61 67 65  tPage(pBt, iPage
177cd 2c 20 26 70 50 61 67 65 2c 20 30 29 29 20 29 7b  , &pPage, 0)) ){
177ce 0a 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61  .    goto freepa
177cf 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 72 63  ge_out;.  }.  rc
177d0 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
177d1 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50  rite(pPage->pDbP
177d2 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  age);.  if( rc!=
177d3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
177d4 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f   goto freepage_o
177d5 75 74 3b 0a 20 20 7d 0a 20 20 70 75 74 34 62 79  ut;.  }.  put4by
177d6 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c  te(pPage->aData,
177d7 20 69 54 72 75 6e 6b 29 3b 0a 20 20 70 75 74 34   iTrunk);.  put4
177d8 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61  byte(&pPage->aDa
177d9 74 61 5b 34 5d 2c 20 30 29 3b 0a 20 20 70 75 74  ta[4], 0);.  put
177da 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61  4byte(&pPage1->a
177db 44 61 74 61 5b 33 32 5d 2c 20 69 50 61 67 65 29  Data[32], iPage)
177dc 3b 0a 20 20 54 52 41 43 45 28 28 22 46 52 45 45  ;.  TRACE(("FREE
177dd 2d 50 41 47 45 3a 20 25 64 20 6e 65 77 20 74 72  -PAGE: %d new tr
177de 75 6e 6b 20 70 61 67 65 20 72 65 70 6c 61 63 69  unk page replaci
177df 6e 67 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 2d  ng %d\n", pPage-
177e0 3e 70 67 6e 6f 2c 20 69 54 72 75 6e 6b 29 29 3b  >pgno, iTrunk));
177e1 0a 0a 66 72 65 65 70 61 67 65 5f 6f 75 74 3a 0a  ..freepage_out:.
177e2 20 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20    if( pPage ){. 
177e3 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74     pPage->isInit
177e4 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 6c 65   = 0;.  }.  rele
177e5 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
177e6 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54    releasePage(pT
177e7 72 75 6e 6b 29 3b 0a 20 20 72 65 74 75 72 6e 20  runk);.  return 
177e8 72 63 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  rc;.}.static voi
177e9 64 20 66 72 65 65 50 61 67 65 28 4d 65 6d 50 61  d freePage(MemPa
177ea 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 2a  ge *pPage, int *
177eb 70 52 43 29 7b 0a 20 20 69 66 28 20 28 2a 70 52  pRC){.  if( (*pR
177ec 43 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  C)==SQLITE_OK ){
177ed 0a 20 20 20 20 2a 70 52 43 20 3d 20 66 72 65 65  .    *pRC = free
177ee 50 61 67 65 32 28 70 50 61 67 65 2d 3e 70 42 74  Page2(pPage->pBt
177ef 2c 20 70 50 61 67 65 2c 20 70 50 61 67 65 2d 3e  , pPage, pPage->
177f0 70 67 6e 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  pgno);.  }.}../*
177f1 0a 2a 2a 20 46 72 65 65 20 61 6e 79 20 6f 76 65  .** Free any ove
177f2 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73 73 6f  rflow pages asso
177f3 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
177f4 67 69 76 65 6e 20 43 65 6c 6c 2e 0a 2a 2f 0a 73  given Cell..*/.s
177f5 74 61 74 69 63 20 69 6e 74 20 63 6c 65 61 72 43  tatic int clearC
177f6 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  ell(MemPage *pPa
177f7 67 65 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ge, unsigned cha
177f8 72 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 42 74 53  r *pCell){.  BtS
177f9 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61  hared *pBt = pPa
177fa 67 65 2d 3e 70 42 74 3b 0a 20 20 43 65 6c 6c 49  ge->pBt;.  CellI
177fb 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 50 67 6e 6f  nfo info;.  Pgno
177fc 20 6f 76 66 6c 50 67 6e 6f 3b 0a 20 20 69 6e 74   ovflPgno;.  int
177fd 20 72 63 3b 0a 20 20 69 6e 74 20 6e 4f 76 66 6c   rc;.  int nOvfl
177fe 3b 0a 20 20 75 31 36 20 6f 76 66 6c 50 61 67 65  ;.  u16 ovflPage
177ff 53 69 7a 65 3b 0a 0a 20 20 61 73 73 65 72 74 28  Size;..  assert(
17800 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
17801 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
17802 6d 75 74 65 78 29 20 29 3b 0a 20 20 62 74 72 65  mutex) );.  btre
17803 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50  eParseCellPtr(pP
17804 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66  age, pCell, &inf
17805 6f 29 3b 0a 20 20 69 66 28 20 69 6e 66 6f 2e 69  o);.  if( info.i
17806 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 7b 0a 20  Overflow==0 ){. 
17807 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
17808 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f 20 6f 76 65 72  _OK;  /* No over
17809 66 6c 6f 77 20 70 61 67 65 73 2e 20 52 65 74 75  flow pages. Retu
1780a 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67  rn without doing
1780b 20 61 6e 79 74 68 69 6e 67 20 2a 2f 0a 20 20 7d   anything */.  }
1780c 0a 20 20 6f 76 66 6c 50 67 6e 6f 20 3d 20 67 65  .  ovflPgno = ge
1780d 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e  t4byte(&pCell[in
1780e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a  fo.iOverflow]);.
1780f 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 75    assert( pBt->u
17810 73 61 62 6c 65 53 69 7a 65 20 3e 20 34 20 29 3b  sableSize > 4 );
17811 0a 20 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 20  .  ovflPageSize 
17812 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  = pBt->usableSiz
17813 65 20 2d 20 34 3b 0a 20 20 6e 4f 76 66 6c 20 3d  e - 4;.  nOvfl =
17814 20 28 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20   (info.nPayload 
17815 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b 20  - info.nLocal + 
17816 6f 76 66 6c 50 61 67 65 53 69 7a 65 20 2d 20 31  ovflPageSize - 1
17817 29 2f 6f 76 66 6c 50 61 67 65 53 69 7a 65 3b 0a  )/ovflPageSize;.
17818 20 20 61 73 73 65 72 74 28 20 6f 76 66 6c 50 67    assert( ovflPg
17819 6e 6f 3d 3d 30 20 7c 7c 20 6e 4f 76 66 6c 3e 30  no==0 || nOvfl>0
1781a 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4f 76   );.  while( nOv
1781b 66 6c 2d 2d 20 29 7b 0a 20 20 20 20 50 67 6e 6f  fl-- ){.    Pgno
1781c 20 69 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20   iNext = 0;.    
1781d 4d 65 6d 50 61 67 65 20 2a 70 4f 76 66 6c 20 3d  MemPage *pOvfl =
1781e 20 30 3b 0a 20 20 20 20 69 66 28 20 6f 76 66 6c   0;.    if( ovfl
1781f 50 67 6e 6f 3c 32 20 7c 7c 20 6f 76 66 6c 50 67  Pgno<2 || ovflPg
17820 6e 6f 3e 70 61 67 65 72 50 61 67 65 63 6f 75 6e  no>pagerPagecoun
17821 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20  t(pBt) ){.      
17822 2f 2a 20 30 20 69 73 20 6e 6f 74 20 61 20 6c 65  /* 0 is not a le
17823 67 61 6c 20 70 61 67 65 20 6e 75 6d 62 65 72 20  gal page number 
17824 61 6e 64 20 70 61 67 65 20 31 20 63 61 6e 6e 6f  and page 1 canno
17825 74 20 62 65 20 61 6e 20 0a 20 20 20 20 20 20 2a  t be an .      *
17826 2a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e  * overflow page.
17827 20 54 68 65 72 65 66 6f 72 65 20 69 66 20 6f 76   Therefore if ov
17828 66 6c 50 67 6e 6f 3c 32 20 6f 72 20 70 61 73 74  flPgno<2 or past
17829 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
1782a 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 20 74  .      ** file t
1782b 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 73 74  he database must
1782c 20 62 65 20 63 6f 72 72 75 70 74 2e 20 2a 2f 0a   be corrupt. */.
1782d 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
1782e 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1782f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
17830 6e 4f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 72  nOvfl ){.      r
17831 63 20 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77 50  c = getOverflowP
17832 61 67 65 28 70 42 74 2c 20 6f 76 66 6c 50 67 6e  age(pBt, ovflPgn
17833 6f 2c 20 26 70 4f 76 66 6c 2c 20 26 69 4e 65 78  o, &pOvfl, &iNex
17834 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  t);.      if( rc
17835 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
17836 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 66 72 65    }.    rc = fre
17837 65 50 61 67 65 32 28 70 42 74 2c 20 70 4f 76 66  ePage2(pBt, pOvf
17838 6c 2c 20 6f 76 66 6c 50 67 6e 6f 29 3b 0a 20 20  l, ovflPgno);.  
17839 20 20 69 66 28 20 70 4f 76 66 6c 20 29 7b 0a 20    if( pOvfl ){. 
1783a 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
1783b 72 55 6e 72 65 66 28 70 4f 76 66 6c 2d 3e 70 44  rUnref(pOvfl->pD
1783c 62 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20  bPage);.    }.  
1783d 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
1783e 6e 20 72 63 3b 0a 20 20 20 20 6f 76 66 6c 50 67  n rc;.    ovflPg
1783f 6e 6f 20 3d 20 69 4e 65 78 74 3b 0a 20 20 7d 0a  no = iNext;.  }.
17840 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
17841 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65  OK;.}../*.** Cre
17842 61 74 65 20 74 68 65 20 62 79 74 65 20 73 65 71  ate the byte seq
17843 75 65 6e 63 65 20 75 73 65 64 20 74 6f 20 72 65  uence used to re
17844 70 72 65 73 65 6e 74 20 61 20 63 65 6c 6c 20 6f  present a cell o
17845 6e 20 70 61 67 65 20 70 50 61 67 65 0a 2a 2a 20  n page pPage.** 
17846 61 6e 64 20 77 72 69 74 65 20 74 68 61 74 20 62  and write that b
17847 79 74 65 20 73 65 71 75 65 6e 63 65 20 69 6e 74  yte sequence int
17848 6f 20 70 43 65 6c 6c 5b 5d 2e 20 20 4f 76 65 72  o pCell[].  Over
17849 66 6c 6f 77 20 70 61 67 65 73 20 61 72 65 0a 2a  flow pages are.*
1784a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  * allocated and 
1784b 66 69 6c 6c 65 64 20 69 6e 20 61 73 20 6e 65 63  filled in as nec
1784c 65 73 73 61 72 79 2e 20 20 54 68 65 20 63 61 6c  essary.  The cal
1784d 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 0a 2a  ling procedure.*
1784e 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65  * is responsible
1784f 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72 65   for making sure
17850 20 73 75 66 66 69 63 69 65 6e 74 20 73 70 61 63   sufficient spac
17851 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63  e has been alloc
17852 61 74 65 64 0a 2a 2a 20 66 6f 72 20 70 43 65 6c  ated.** for pCel
17853 6c 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  l[]..**.** Note 
17854 74 68 61 74 20 70 43 65 6c 6c 20 64 6f 65 73 20  that pCell does 
17855 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 6e 65  not necessary ne
17856 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  ed to point to t
17857 68 65 20 70 50 61 67 65 2d 3e 61 44 61 74 61 0a  he pPage->aData.
17858 2a 2a 20 61 72 65 61 2e 20 20 70 43 65 6c 6c 20  ** area.  pCell 
17859 6d 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 73  might point to s
1785a 6f 6d 65 20 74 65 6d 70 6f 72 61 72 79 20 73 74  ome temporary st
1785b 6f 72 61 67 65 2e 20 20 54 68 65 20 63 65 6c 6c  orage.  The cell
1785c 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 63 6f 6e 73   will.** be cons
1785d 74 72 75 63 74 65 64 20 69 6e 20 74 68 69 73 20  tructed in this 
1785e 74 65 6d 70 6f 72 61 72 79 20 61 72 65 61 20 74  temporary area t
1785f 68 65 6e 20 63 6f 70 69 65 64 20 69 6e 74 6f 20  hen copied into 
17860 70 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20  pPage->aData.** 
17861 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  later..*/.static
17862 20 69 6e 74 20 66 69 6c 6c 49 6e 43 65 6c 6c 28   int fillInCell(
17863 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
17864 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
17865 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74 68    /* The page th
17866 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
17867 63 65 6c 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  cell */.  unsign
17868 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 2c 20  ed char *pCell, 
17869 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70           /* Comp
1786a 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65  lete text of the
1786b 20 63 65 6c 6c 20 2a 2f 0a 20 20 63 6f 6e 73 74   cell */.  const
1786c 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34   void *pKey, i64
1786d 20 6e 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65   nKey,    /* The
1786e 20 6b 65 79 20 2a 2f 0a 20 20 63 6f 6e 73 74 20   key */.  const 
1786f 76 6f 69 64 20 2a 70 44 61 74 61 2c 69 6e 74 20  void *pData,int 
17870 6e 44 61 74 61 2c 20 20 20 2f 2a 20 54 68 65 20  nData,   /* The 
17871 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a  data */.  int nZ
17872 65 72 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20  ero,            
17873 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72           /* Extr
17874 61 20 7a 65 72 6f 20 62 79 74 65 73 20 74 6f 20  a zero bytes to 
17875 61 70 70 65 6e 64 20 74 6f 20 70 44 61 74 61 20  append to pData 
17876 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 53 69 7a 65  */.  int *pnSize
17877 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17878 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 65 6c      /* Write cel
17879 6c 20 73 69 7a 65 20 68 65 72 65 20 2a 2f 0a 29  l size here */.)
1787a 7b 0a 20 20 69 6e 74 20 6e 50 61 79 6c 6f 61 64  {.  int nPayload
1787b 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 70 53  ;.  const u8 *pS
1787c 72 63 3b 0a 20 20 69 6e 74 20 6e 53 72 63 2c 20  rc;.  int nSrc, 
1787d 6e 2c 20 72 63 3b 0a 20 20 69 6e 74 20 73 70 61  n, rc;.  int spa
1787e 63 65 4c 65 66 74 3b 0a 20 20 4d 65 6d 50 61 67  ceLeft;.  MemPag
1787f 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a 20 20  e *pOvfl = 0;.  
17880 4d 65 6d 50 61 67 65 20 2a 70 54 6f 52 65 6c 65  MemPage *pToRele
17881 61 73 65 20 3d 20 30 3b 0a 20 20 75 6e 73 69 67  ase = 0;.  unsig
17882 6e 65 64 20 63 68 61 72 20 2a 70 50 72 69 6f 72  ned char *pPrior
17883 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ;.  unsigned cha
17884 72 20 2a 70 50 61 79 6c 6f 61 64 3b 0a 20 20 42  r *pPayload;.  B
17885 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
17886 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 50 67 6e  Page->pBt;.  Pgn
17887 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 30 3b 0a  o pgnoOvfl = 0;.
17888 20 20 69 6e 74 20 6e 48 65 61 64 65 72 3b 0a 20    int nHeader;. 
17889 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a   CellInfo info;.
1788a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1788b 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
1788c 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
1788d 20 29 3b 0a 0a 20 20 2f 2a 20 70 50 61 67 65 20   );..  /* pPage 
1788e 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69  is not necessari
1788f 6c 79 20 77 72 69 74 65 61 62 6c 65 20 73 69 6e  ly writeable sin
17890 63 65 20 70 43 65 6c 6c 20 6d 69 67 68 74 20 62  ce pCell might b
17891 65 20 61 75 78 69 6c 69 61 72 79 0a 20 20 2a 2a  e auxiliary.  **
17892 20 62 75 66 66 65 72 20 73 70 61 63 65 20 74 68   buffer space th
17893 61 74 20 69 73 20 73 65 70 61 72 61 74 65 20 66  at is separate f
17894 72 6f 6d 20 74 68 65 20 70 50 61 67 65 20 62 75  rom the pPage bu
17895 66 66 65 72 20 61 72 65 61 20 2a 2f 0a 20 20 61  ffer area */.  a
17896 73 73 65 72 74 28 20 70 43 65 6c 6c 3c 70 50 61  ssert( pCell<pPa
17897 67 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70 43 65  ge->aData || pCe
17898 6c 6c 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61 74  ll>=&pPage->aDat
17899 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d  a[pBt->pageSize]
1789a 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
1789b 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
1789c 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70  iteable(pPage->p
1789d 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 2f 2a  DbPage) );..  /*
1789e 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 68 65 61   Fill in the hea
1789f 64 65 72 2e 20 2a 2f 0a 20 20 6e 48 65 61 64 65  der. */.  nHeade
178a0 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 21 70 50  r = 0;.  if( !pP
178a1 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
178a2 20 6e 48 65 61 64 65 72 20 2b 3d 20 34 3b 0a 20   nHeader += 4;. 
178a3 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e   }.  if( pPage->
178a4 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20 6e  hasData ){.    n
178a5 48 65 61 64 65 72 20 2b 3d 20 70 75 74 56 61 72  Header += putVar
178a6 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 48 65 61 64  int(&pCell[nHead
178a7 65 72 5d 2c 20 6e 44 61 74 61 2b 6e 5a 65 72 6f  er], nData+nZero
178a8 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
178a9 6e 44 61 74 61 20 3d 20 6e 5a 65 72 6f 20 3d 20  nData = nZero = 
178aa 30 3b 0a 20 20 7d 0a 20 20 6e 48 65 61 64 65 72  0;.  }.  nHeader
178ab 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26 70   += putVarint(&p
178ac 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 2a  Cell[nHeader], *
178ad 28 75 36 34 2a 29 26 6e 4b 65 79 29 3b 0a 20 20  (u64*)&nKey);.  
178ae 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  btreeParseCellPt
178af 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20  r(pPage, pCell, 
178b0 26 69 6e 66 6f 29 3b 0a 20 20 61 73 73 65 72 74  &info);.  assert
178b1 28 20 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3d 3d  ( info.nHeader==
178b2 6e 48 65 61 64 65 72 20 29 3b 0a 20 20 61 73 73  nHeader );.  ass
178b3 65 72 74 28 20 69 6e 66 6f 2e 6e 4b 65 79 3d 3d  ert( info.nKey==
178b4 6e 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74  nKey );.  assert
178b5 28 20 69 6e 66 6f 2e 6e 44 61 74 61 3d 3d 28 75  ( info.nData==(u
178b6 33 32 29 28 6e 44 61 74 61 2b 6e 5a 65 72 6f 29  32)(nData+nZero)
178b7 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 46 69 6c 6c   );.  .  /* Fill
178b8 20 69 6e 20 74 68 65 20 70 61 79 6c 6f 61 64 20   in the payload 
178b9 2a 2f 0a 20 20 6e 50 61 79 6c 6f 61 64 20 3d 20  */.  nPayload = 
178ba 6e 44 61 74 61 20 2b 20 6e 5a 65 72 6f 3b 0a 20  nData + nZero;. 
178bb 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b   if( pPage->intK
178bc 65 79 20 29 7b 0a 20 20 20 20 70 53 72 63 20 3d  ey ){.    pSrc =
178bd 20 70 44 61 74 61 3b 0a 20 20 20 20 6e 53 72 63   pData;.    nSrc
178be 20 3d 20 6e 44 61 74 61 3b 0a 20 20 20 20 6e 44   = nData;.    nD
178bf 61 74 61 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  ata = 0;.  }else
178c0 7b 20 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52  { .    if( NEVER
178c1 28 6e 4b 65 79 3e 30 78 37 66 66 66 66 66 66 66  (nKey>0x7fffffff
178c2 20 7c 7c 20 70 4b 65 79 3d 3d 30 29 20 29 7b 0a   || pKey==0) ){.
178c3 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
178c4 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
178c5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 50 61 79  ;.    }.    nPay
178c6 6c 6f 61 64 20 2b 3d 20 28 69 6e 74 29 6e 4b 65  load += (int)nKe
178c7 79 3b 0a 20 20 20 20 70 53 72 63 20 3d 20 70 4b  y;.    pSrc = pK
178c8 65 79 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 28  ey;.    nSrc = (
178c9 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20  int)nKey;.  }.  
178ca 2a 70 6e 53 69 7a 65 20 3d 20 69 6e 66 6f 2e 6e  *pnSize = info.n
178cb 53 69 7a 65 3b 0a 20 20 73 70 61 63 65 4c 65 66  Size;.  spaceLef
178cc 74 20 3d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b  t = info.nLocal;
178cd 0a 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70  .  pPayload = &p
178ce 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 3b 0a 20  Cell[nHeader];. 
178cf 20 70 50 72 69 6f 72 20 3d 20 26 70 43 65 6c 6c   pPrior = &pCell
178d0 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d  [info.iOverflow]
178d1 3b 0a 0a 20 20 77 68 69 6c 65 28 20 6e 50 61 79  ;..  while( nPay
178d2 6c 6f 61 64 3e 30 20 29 7b 0a 20 20 20 20 69 66  load>0 ){.    if
178d3 28 20 73 70 61 63 65 4c 65 66 74 3d 3d 30 20 29  ( spaceLeft==0 )
178d4 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
178d5 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
178d6 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f  .      Pgno pgno
178d7 50 74 72 6d 61 70 20 3d 20 70 67 6e 6f 4f 76 66  Ptrmap = pgnoOvf
178d8 6c 3b 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70  l; /* Overflow p
178d9 61 67 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  age pointer-map 
178da 65 6e 74 72 79 20 70 61 67 65 20 2a 2f 0a 20 20  entry page */.  
178db 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74      if( pBt->aut
178dc 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20  oVacuum ){.     
178dd 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 20 20 20     do{.         
178de 20 70 67 6e 6f 4f 76 66 6c 2b 2b 3b 0a 20 20 20   pgnoOvfl++;.   
178df 20 20 20 20 20 7d 20 77 68 69 6c 65 28 20 0a 20       } while( . 
178e0 20 20 20 20 20 20 20 20 20 50 54 52 4d 41 50 5f           PTRMAP_
178e1 49 53 50 41 47 45 28 70 42 74 2c 20 70 67 6e 6f  ISPAGE(pBt, pgno
178e2 4f 76 66 6c 29 20 7c 7c 20 70 67 6e 6f 4f 76 66  Ovfl) || pgnoOvf
178e3 6c 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  l==PENDING_BYTE_
178e4 50 41 47 45 28 70 42 74 29 20 0a 20 20 20 20 20  PAGE(pBt) .     
178e5 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65     );.      }.#e
178e6 6e 64 69 66 0a 20 20 20 20 20 20 72 63 20 3d 20  ndif.      rc = 
178e7 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67  allocateBtreePag
178e8 65 28 70 42 74 2c 20 26 70 4f 76 66 6c 2c 20 26  e(pBt, &pOvfl, &
178e9 70 67 6e 6f 4f 76 66 6c 2c 20 70 67 6e 6f 4f 76  pgnoOvfl, pgnoOv
178ea 66 6c 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20  fl, 0);.#ifndef 
178eb 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
178ec 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f 2a 20  VACUUM.      /* 
178ed 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
178ee 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61  supports auto-va
178ef 63 75 75 6d 2c 20 61 6e 64 20 74 68 65 20 73 65  cuum, and the se
178f0 63 6f 6e 64 20 6f 72 20 73 75 62 73 65 71 75 65  cond or subseque
178f1 6e 74 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72  nt.      ** over
178f2 66 6c 6f 77 20 70 61 67 65 20 69 73 20 62 65 69  flow page is bei
178f3 6e 67 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61 64  ng allocated, ad
178f4 64 20 61 6e 20 65 6e 74 72 79 20 74 6f 20 74 68  d an entry to th
178f5 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20  e pointer-map.  
178f6 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 61 74 20      ** for that 
178f7 70 61 67 65 20 6e 6f 77 2e 20 0a 20 20 20 20 20  page now. .     
178f8 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 20   **.      ** If 
178f9 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73  this is the firs
178fa 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c  t overflow page,
178fb 20 74 68 65 6e 20 77 72 69 74 65 20 61 20 70 61   then write a pa
178fc 72 74 69 61 6c 20 65 6e 74 72 79 20 0a 20 20 20  rtial entry .   
178fd 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 70 6f 69     ** to the poi
178fe 6e 74 65 72 2d 6d 61 70 2e 20 49 66 20 77 65 20  nter-map. If we 
178ff 77 72 69 74 65 20 6e 6f 74 68 69 6e 67 20 74 6f  write nothing to
17900 20 74 68 69 73 20 70 6f 69 6e 74 65 72 2d 6d 61   this pointer-ma
17901 70 20 73 6c 6f 74 2c 0a 20 20 20 20 20 20 2a 2a  p slot,.      **
17902 20 74 68 65 6e 20 74 68 65 20 6f 70 74 69 6d 69   then the optimi
17903 73 74 69 63 20 6f 76 65 72 66 6c 6f 77 20 63 68  stic overflow ch
17904 61 69 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 69  ain processing i
17905 6e 20 63 6c 65 61 72 43 65 6c 6c 28 29 0a 20 20  n clearCell().  
17906 20 20 20 20 2a 2a 20 6d 61 79 20 6d 69 73 69 6e      ** may misin
17907 74 65 72 70 72 65 74 20 74 68 65 20 75 6e 69 6e  terpret the unin
17908 69 74 69 61 6c 69 73 65 64 20 76 61 6c 75 65 73  itialised values
17909 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 0a   and delete the.
1790a 20 20 20 20 20 20 2a 2a 20 77 72 6f 6e 67 20 70        ** wrong p
1790b 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 64 61  ages from the da
1790c 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 2a 2f  tabase..      */
1790d 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e  .      if( pBt->
1790e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 72 63  autoVacuum && rc
1790f 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
17910 20 20 20 20 20 20 20 75 38 20 65 54 79 70 65 20         u8 eType 
17911 3d 20 28 70 67 6e 6f 50 74 72 6d 61 70 3f 50 54  = (pgnoPtrmap?PT
17912 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 3a 50  RMAP_OVERFLOW2:P
17913 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 29  TRMAP_OVERFLOW1)
17914 3b 0a 20 20 20 20 20 20 20 20 70 74 72 6d 61 70  ;.        ptrmap
17915 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66  Put(pBt, pgnoOvf
17916 6c 2c 20 65 54 79 70 65 2c 20 70 67 6e 6f 50 74  l, eType, pgnoPt
17917 72 6d 61 70 2c 20 26 72 63 29 3b 0a 20 20 20 20  rmap, &rc);.    
17918 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
17919 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
1791a 61 67 65 28 70 4f 76 66 6c 29 3b 0a 20 20 20 20  age(pOvfl);.    
1791b 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65      }.      }.#e
1791c 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 72  ndif.      if( r
1791d 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c  c ){.        rel
1791e 65 61 73 65 50 61 67 65 28 70 54 6f 52 65 6c 65  easePage(pToRele
1791f 61 73 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65  ase);.        re
17920 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
17921 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 70 54  ..      /* If pT
17922 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74 20  oRelease is not 
17923 7a 65 72 6f 20 74 68 61 6e 20 70 50 72 69 6f 72  zero than pPrior
17924 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 74 68 65   points into the
17925 20 64 61 74 61 20 61 72 65 61 0a 20 20 20 20 20   data area.     
17926 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61 73   ** of pToReleas
17927 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70 54  e.  Make sure pT
17928 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 69 6c  oRelease is stil
17929 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f 0a  l writeable. */.
1792a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
1792b 6f 52 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73  oRelease==0 || s
1792c 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
1792d 74 65 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73  teable(pToReleas
1792e 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a  e->pDbPage) );..
1792f 20 20 20 20 20 20 2f 2a 20 49 66 20 70 50 72 69        /* If pPri
17930 6f 72 20 69 73 20 70 61 72 74 20 6f 66 20 74 68  or is part of th
17931 65 20 64 61 74 61 20 61 72 65 61 20 6f 66 20 70  e data area of p
17932 50 61 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20  Page, then make 
17933 73 75 72 65 20 70 50 61 67 65 0a 20 20 20 20 20  sure pPage.     
17934 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 77 72 69   ** is still wri
17935 74 65 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20  teable */.      
17936 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 3c 70  assert( pPrior<p
17937 50 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c 20 70  Page->aData || p
17938 50 72 69 6f 72 3e 3d 26 70 50 61 67 65 2d 3e 61  Prior>=&pPage->a
17939 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69  Data[pBt->pageSi
1793a 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20  ze].            
1793b 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  || sqlite3PagerI
1793c 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
1793d 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20  ->pDbPage) );.. 
1793e 20 20 20 20 20 70 75 74 34 62 79 74 65 28 70 50       put4byte(pP
1793f 72 69 6f 72 2c 20 70 67 6e 6f 4f 76 66 6c 29 3b  rior, pgnoOvfl);
17940 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
17941 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a  ge(pToRelease);.
17942 20 20 20 20 20 20 70 54 6f 52 65 6c 65 61 73 65        pToRelease
17943 20 3d 20 70 4f 76 66 6c 3b 0a 20 20 20 20 20 20   = pOvfl;.      
17944 70 50 72 69 6f 72 20 3d 20 70 4f 76 66 6c 2d 3e  pPrior = pOvfl->
17945 61 44 61 74 61 3b 0a 20 20 20 20 20 20 70 75 74  aData;.      put
17946 34 62 79 74 65 28 70 50 72 69 6f 72 2c 20 30 29  4byte(pPrior, 0)
17947 3b 0a 20 20 20 20 20 20 70 50 61 79 6c 6f 61 64  ;.      pPayload
17948 20 3d 20 26 70 4f 76 66 6c 2d 3e 61 44 61 74 61   = &pOvfl->aData
17949 5b 34 5d 3b 0a 20 20 20 20 20 20 73 70 61 63 65  [4];.      space
1794a 4c 65 66 74 20 3d 20 70 42 74 2d 3e 75 73 61 62  Left = pBt->usab
1794b 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 20 20  leSize - 4;.    
1794c 7d 0a 20 20 20 20 6e 20 3d 20 6e 50 61 79 6c 6f  }.    n = nPaylo
1794d 61 64 3b 0a 20 20 20 20 69 66 28 20 6e 3e 73 70  ad;.    if( n>sp
1794e 61 63 65 4c 65 66 74 20 29 20 6e 20 3d 20 73 70  aceLeft ) n = sp
1794f 61 63 65 4c 65 66 74 3b 0a 0a 20 20 20 20 2f 2a  aceLeft;..    /*
17950 20 49 66 20 70 54 6f 52 65 6c 65 61 73 65 20 69   If pToRelease i
17951 73 20 6e 6f 74 20 7a 65 72 6f 20 74 68 61 6e 20  s not zero than 
17952 70 50 61 79 6c 6f 61 64 20 70 6f 69 6e 74 73 20  pPayload points 
17953 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 61 72  into the data ar
17954 65 61 0a 20 20 20 20 2a 2a 20 6f 66 20 70 54 6f  ea.    ** of pTo
17955 52 65 6c 65 61 73 65 2e 20 20 4d 61 6b 65 20 73  Release.  Make s
17956 75 72 65 20 70 54 6f 52 65 6c 65 61 73 65 20 69  ure pToRelease i
17957 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c  s still writeabl
17958 65 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  e. */.    assert
17959 28 20 70 54 6f 52 65 6c 65 61 73 65 3d 3d 30 20  ( pToRelease==0 
1795a 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  || sqlite3PagerI
1795b 73 77 72 69 74 65 61 62 6c 65 28 70 54 6f 52 65  swriteable(pToRe
1795c 6c 65 61 73 65 2d 3e 70 44 62 50 61 67 65 29 20  lease->pDbPage) 
1795d 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 50  );..    /* If pP
1795e 61 79 6c 6f 61 64 20 69 73 20 70 61 72 74 20 6f  ayload is part o
1795f 66 20 74 68 65 20 64 61 74 61 20 61 72 65 61 20  f the data area 
17960 6f 66 20 70 50 61 67 65 2c 20 74 68 65 6e 20 6d  of pPage, then m
17961 61 6b 65 20 73 75 72 65 20 70 50 61 67 65 0a 20  ake sure pPage. 
17962 20 20 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 77     ** is still w
17963 72 69 74 65 61 62 6c 65 20 2a 2f 0a 20 20 20 20  riteable */.    
17964 61 73 73 65 72 74 28 20 70 50 61 79 6c 6f 61 64  assert( pPayload
17965 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c  <pPage->aData ||
17966 20 70 50 61 79 6c 6f 61 64 3e 3d 26 70 50 61 67   pPayload>=&pPag
17967 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 70 61  e->aData[pBt->pa
17968 67 65 53 69 7a 65 5d 0a 20 20 20 20 20 20 20 20  geSize].        
17969 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 50 61      || sqlite3Pa
1796a 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
1796b 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
1796c 3b 0a 0a 20 20 20 20 69 66 28 20 6e 53 72 63 3e  ;..    if( nSrc>
1796d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e  0 ){.      if( n
1796e 3e 6e 53 72 63 20 29 20 6e 20 3d 20 6e 53 72 63  >nSrc ) n = nSrc
1796f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
17970 70 53 72 63 20 29 3b 0a 20 20 20 20 20 20 6d 65  pSrc );.      me
17971 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c 20 70  mcpy(pPayload, p
17972 53 72 63 2c 20 6e 29 3b 0a 20 20 20 20 7d 65 6c  Src, n);.    }el
17973 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74  se{.      memset
17974 28 70 50 61 79 6c 6f 61 64 2c 20 30 2c 20 6e 29  (pPayload, 0, n)
17975 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 50 61 79  ;.    }.    nPay
17976 6c 6f 61 64 20 2d 3d 20 6e 3b 0a 20 20 20 20 70  load -= n;.    p
17977 50 61 79 6c 6f 61 64 20 2b 3d 20 6e 3b 0a 20 20  Payload += n;.  
17978 20 20 70 53 72 63 20 2b 3d 20 6e 3b 0a 20 20 20    pSrc += n;.   
17979 20 6e 53 72 63 20 2d 3d 20 6e 3b 0a 20 20 20 20   nSrc -= n;.    
1797a 73 70 61 63 65 4c 65 66 74 20 2d 3d 20 6e 3b 0a  spaceLeft -= n;.
1797b 20 20 20 20 69 66 28 20 6e 53 72 63 3d 3d 30 20      if( nSrc==0 
1797c 29 7b 0a 20 20 20 20 20 20 6e 53 72 63 20 3d 20  ){.      nSrc = 
1797d 6e 44 61 74 61 3b 0a 20 20 20 20 20 20 70 53 72  nData;.      pSr
1797e 63 20 3d 20 70 44 61 74 61 3b 0a 20 20 20 20 7d  c = pData;.    }
1797f 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 61  .  }.  releasePa
17980 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a  ge(pToRelease);.
17981 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
17982 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d  OK;.}../*.** Rem
17983 6f 76 65 20 74 68 65 20 69 2d 74 68 20 63 65 6c  ove the i-th cel
17984 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 20 20 54  l from pPage.  T
17985 68 69 73 20 72 6f 75 74 69 6e 65 20 65 66 66 65  his routine effe
17986 63 74 73 20 70 50 61 67 65 20 6f 6e 6c 79 2e 0a  cts pPage only..
17987 2a 2a 20 54 68 65 20 63 65 6c 6c 20 63 6f 6e 74  ** The cell cont
17988 65 6e 74 20 69 73 20 6e 6f 74 20 66 72 65 65 64  ent is not freed
17989 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e   or deallocated.
1798a 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20    It is assumed 
1798b 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 65 6c 6c  that.** the cell
1798c 20 63 6f 6e 74 65 6e 74 20 68 61 73 20 62 65 65   content has bee
1798d 6e 20 63 6f 70 69 65 64 20 73 6f 6d 65 70 6c 61  n copied somepla
1798e 63 65 20 65 6c 73 65 2e 20 20 54 68 69 73 20 72  ce else.  This r
1798f 6f 75 74 69 6e 65 20 6a 75 73 74 0a 2a 2a 20 72  outine just.** r
17990 65 6d 6f 76 65 73 20 74 68 65 20 72 65 66 65 72  emoves the refer
17991 65 6e 63 65 20 74 6f 20 74 68 65 20 63 65 6c 6c  ence to the cell
17992 20 66 72 6f 6d 20 70 50 61 67 65 2e 0a 2a 2a 0a   from pPage..**.
17993 2a 2a 20 22 73 7a 22 20 6d 75 73 74 20 62 65 20  ** "sz" must be 
17994 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
17995 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 2e  tes in the cell.
17996 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
17997 64 72 6f 70 43 65 6c 6c 28 4d 65 6d 50 61 67 65  dropCell(MemPage
17998 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 64 78   *pPage, int idx
17999 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 2a 70  , int sz, int *p
1799a 52 43 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20  RC){.  int i;   
1799b 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
1799c 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ounter */.  int 
1799d 70 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  pc;         /* O
1799e 66 66 73 65 74 20 74 6f 20 63 65 6c 6c 20 63 6f  ffset to cell co
1799f 6e 74 65 6e 74 20 6f 66 20 63 65 6c 6c 20 62 65  ntent of cell be
179a0 69 6e 67 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20  ing deleted */. 
179a1 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20 20 20   u8 *data;      
179a2 20 2f 2a 20 70 50 61 67 65 2d 3e 61 44 61 74 61   /* pPage->aData
179a3 20 2a 2f 0a 20 20 75 38 20 2a 70 74 72 3b 20 20   */.  u8 *ptr;  
179a4 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
179a5 20 6d 6f 76 65 20 62 79 74 65 73 20 61 72 6f 75   move bytes arou
179a6 6e 64 20 77 69 74 68 69 6e 20 64 61 74 61 5b 5d  nd within data[]
179a7 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
179a8 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74        /* The ret
179a9 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e  urn code */.  in
179aa 74 20 68 64 72 3b 20 20 20 20 20 20 20 20 2f 2a  t hdr;        /*
179ab 20 42 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   Beginning of th
179ac 65 20 68 65 61 64 65 72 2e 20 20 30 20 6d 6f 73  e header.  0 mos
179ad 74 20 70 61 67 65 73 2e 20 20 31 30 30 20 70 61  t pages.  100 pa
179ae 67 65 20 31 20 2a 2f 0a 0a 20 20 69 66 28 20 2a  ge 1 */..  if( *
179af 70 52 43 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20  pRC ) return;.. 
179b0 20 61 73 73 65 72 74 28 20 69 64 78 3e 3d 30 20   assert( idx>=0 
179b1 26 26 20 69 64 78 3c 70 50 61 67 65 2d 3e 6e 43  && idx<pPage->nC
179b2 65 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ell );.  assert(
179b3 20 73 7a 3d 3d 63 65 6c 6c 53 69 7a 65 28 70 50   sz==cellSize(pP
179b4 61 67 65 2c 20 69 64 78 29 20 29 3b 0a 20 20 61  age, idx) );.  a
179b5 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
179b6 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
179b7 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
179b8 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
179b9 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
179ba 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
179bb 29 20 29 3b 0a 20 20 64 61 74 61 20 3d 20 70 50  ) );.  data = pP
179bc 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 70 74  age->aData;.  pt
179bd 72 20 3d 20 26 64 61 74 61 5b 70 50 61 67 65 2d  r = &data[pPage-
179be 3e 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a  >cellOffset + 2*
179bf 69 64 78 5d 3b 0a 20 20 70 63 20 3d 20 67 65 74  idx];.  pc = get
179c0 32 62 79 74 65 28 70 74 72 29 3b 0a 20 20 68 64  2byte(ptr);.  hd
179c1 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66  r = pPage->hdrOf
179c2 66 73 65 74 3b 0a 20 20 74 65 73 74 63 61 73 65  fset;.  testcase
179c3 28 20 70 63 3d 3d 67 65 74 32 62 79 74 65 28 26  ( pc==get2byte(&
179c4 64 61 74 61 5b 68 64 72 2b 35 5d 29 20 29 3b 0a  data[hdr+5]) );.
179c5 20 20 74 65 73 74 63 61 73 65 28 20 70 63 2b 73    testcase( pc+s
179c6 7a 3d 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75  z==pPage->pBt->u
179c7 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 69  sableSize );.  i
179c8 66 28 20 70 63 20 3c 20 67 65 74 32 62 79 74 65  f( pc < get2byte
179c9 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 20 7c  (&data[hdr+5]) |
179ca 7c 20 70 63 2b 73 7a 20 3e 20 70 50 61 67 65 2d  | pc+sz > pPage-
179cb 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  >pBt->usableSize
179cc 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20 53   ){.    *pRC = S
179cd 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
179ce 50 54 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  PT;.    return;.
179cf 20 20 7d 0a 20 20 72 63 20 3d 20 66 72 65 65 53    }.  rc = freeS
179d0 70 61 63 65 28 70 50 61 67 65 2c 20 70 63 2c 20  pace(pPage, pc, 
179d1 73 7a 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b  sz);.  if( rc ){
179d2 0a 20 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a  .    *pRC = rc;.
179d3 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
179d4 20 20 66 6f 72 28 69 3d 69 64 78 2b 31 3b 20 69    for(i=idx+1; i
179d5 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69  <pPage->nCell; i
179d6 2b 2b 2c 20 70 74 72 2b 3d 32 29 7b 0a 20 20 20  ++, ptr+=2){.   
179d7 20 70 74 72 5b 30 5d 20 3d 20 70 74 72 5b 32 5d   ptr[0] = ptr[2]
179d8 3b 0a 20 20 20 20 70 74 72 5b 31 5d 20 3d 20 70  ;.    ptr[1] = p
179d9 74 72 5b 33 5d 3b 0a 20 20 7d 0a 20 20 70 50 61  tr[3];.  }.  pPa
179da 67 65 2d 3e 6e 43 65 6c 6c 2d 2d 3b 0a 20 20 70  ge->nCell--;.  p
179db 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  ut2byte(&data[hd
179dc 72 2b 33 5d 2c 20 70 50 61 67 65 2d 3e 6e 43 65  r+3], pPage->nCe
179dd 6c 6c 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46  ll);.  pPage->nF
179de 72 65 65 20 2b 3d 20 32 3b 0a 7d 0a 0a 2f 2a 0a  ree += 2;.}../*.
179df 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e 65 77 20  ** Insert a new 
179e0 63 65 6c 6c 20 6f 6e 20 70 50 61 67 65 20 61 74  cell on pPage at
179e1 20 63 65 6c 6c 20 69 6e 64 65 78 20 22 69 22 2e   cell index "i".
179e2 20 20 70 43 65 6c 6c 20 70 6f 69 6e 74 73 20 74    pCell points t
179e3 6f 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74  o the.** content
179e4 20 6f 66 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2a   of the cell..**
179e5 0a 2a 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20  .** If the cell 
179e6 63 6f 6e 74 65 6e 74 20 77 69 6c 6c 20 66 69 74  content will fit
179e7 20 6f 6e 20 74 68 65 20 70 61 67 65 2c 20 74 68   on the page, th
179e8 65 6e 20 70 75 74 20 69 74 20 74 68 65 72 65 2e  en put it there.
179e9 20 20 49 66 20 69 74 0a 2a 2a 20 77 69 6c 6c 20    If it.** will 
179ea 6e 6f 74 20 66 69 74 2c 20 74 68 65 6e 20 6d 61  not fit, then ma
179eb 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  ke a copy of the
179ec 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e   cell content in
179ed 74 6f 20 70 54 65 6d 70 20 69 66 0a 2a 2a 20 70  to pTemp if.** p
179ee 54 65 6d 70 20 69 73 20 6e 6f 74 20 6e 75 6c 6c  Temp is not null
179ef 2e 20 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66  .  Regardless of
179f0 20 70 54 65 6d 70 2c 20 61 6c 6c 6f 63 61 74 65   pTemp, allocate
179f1 20 61 20 6e 65 77 20 65 6e 74 72 79 0a 2a 2a 20   a new entry.** 
179f2 69 6e 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b  in pPage->aOvfl[
179f3 5d 20 61 6e 64 20 6d 61 6b 65 20 69 74 20 70 6f  ] and make it po
179f4 69 6e 74 20 74 6f 20 74 68 65 20 63 65 6c 6c 20  int to the cell 
179f5 63 6f 6e 74 65 6e 74 20 28 65 69 74 68 65 72 0a  content (either.
179f6 2a 2a 20 69 6e 20 70 54 65 6d 70 20 6f 72 20 74  ** in pTemp or t
179f7 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 43 65 6c  he original pCel
179f8 6c 29 20 61 6e 64 20 61 6c 73 6f 20 72 65 63 6f  l) and also reco
179f9 72 64 20 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a  rd its index. .*
179fa 2a 20 41 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e  * Allocating a n
179fb 65 77 20 65 6e 74 72 79 20 69 6e 20 70 50 61 67  ew entry in pPag
179fc 65 2d 3e 61 43 65 6c 6c 5b 5d 20 69 6d 70 6c 69  e->aCell[] impli
179fd 65 73 20 74 68 61 74 20 0a 2a 2a 20 70 50 61 67  es that .** pPag
179fe 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 69 73 20  e->nOverflow is 
179ff 69 6e 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a 0a  incremented..**.
17a00 2a 2a 20 49 66 20 6e 53 6b 69 70 20 69 73 20 6e  ** If nSkip is n
17a01 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 64 6f  on-zero, then do
17a02 20 6e 6f 74 20 63 6f 70 79 20 74 68 65 20 66 69   not copy the fi
17a03 72 73 74 20 6e 53 6b 69 70 20 62 79 74 65 73 20  rst nSkip bytes 
17a04 6f 66 20 74 68 65 0a 2a 2a 20 63 65 6c 6c 2e 20  of the.** cell. 
17a05 54 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20  The caller will 
17a06 6f 76 65 72 77 72 69 74 65 20 74 68 65 6d 20 61  overwrite them a
17a07 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74 69  fter this functi
17a08 6f 6e 20 72 65 74 75 72 6e 73 2e 20 49 66 0a 2a  on returns. If.*
17a09 2a 20 6e 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a  * nSkip is non-z
17a0a 65 72 6f 2c 20 74 68 65 6e 20 70 43 65 6c 6c 20  ero, then pCell 
17a0b 6d 61 79 20 6e 6f 74 20 70 6f 69 6e 74 20 74 6f  may not point to
17a0c 20 61 6e 20 69 6e 76 61 6c 69 64 20 6d 65 6d 6f   an invalid memo
17a0d 72 79 20 6c 6f 63 61 74 69 6f 6e 20 0a 2a 2a 20  ry location .** 
17a0e 28 62 75 74 20 70 43 65 6c 6c 2b 6e 53 6b 69 70  (but pCell+nSkip
17a0f 20 69 73 20 61 6c 77 61 79 73 20 76 61 6c 69 64   is always valid
17a10 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
17a11 64 20 69 6e 73 65 72 74 43 65 6c 6c 28 0a 20 20  d insertCell(.  
17a12 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
17a13 20 20 2f 2a 20 50 61 67 65 20 69 6e 74 6f 20 77    /* Page into w
17a14 68 69 63 68 20 77 65 20 61 72 65 20 63 6f 70 79  hich we are copy
17a15 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20  ing */.  int i, 
17a16 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
17a17 77 20 63 65 6c 6c 20 62 65 63 6f 6d 65 73 20 74  w cell becomes t
17a18 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 6f 66 20  he i-th cell of 
17a19 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 38  the page */.  u8
17a1a 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20   *pCell,        
17a1b 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68  /* Content of th
17a1c 65 20 6e 65 77 20 63 65 6c 6c 20 2a 2f 0a 20 20  e new cell */.  
17a1d 69 6e 74 20 73 7a 2c 20 20 20 20 20 20 20 20 20  int sz,         
17a1e 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 63 6f    /* Bytes of co
17a1f 6e 74 65 6e 74 20 69 6e 20 70 43 65 6c 6c 20 2a  ntent in pCell *
17a20 2f 0a 20 20 75 38 20 2a 70 54 65 6d 70 2c 20 20  /.  u8 *pTemp,  
17a21 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 73 74        /* Temp st
17a22 6f 72 61 67 65 20 73 70 61 63 65 20 66 6f 72 20  orage space for 
17a23 70 43 65 6c 6c 2c 20 69 66 20 6e 65 65 64 65 64  pCell, if needed
17a24 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 43 68 69 6c   */.  Pgno iChil
17a25 64 2c 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f  d,      /* If no
17a26 6e 2d 7a 65 72 6f 2c 20 72 65 70 6c 61 63 65 20  n-zero, replace 
17a27 66 69 72 73 74 20 34 20 62 79 74 65 73 20 77 69  first 4 bytes wi
17a28 74 68 20 74 68 69 73 20 76 61 6c 75 65 20 2a 2f  th this value */
17a29 0a 20 20 69 6e 74 20 2a 70 52 43 20 20 20 20 20  .  int *pRC     
17a2a 20 20 20 20 20 2f 2a 20 52 65 61 64 20 61 6e 64       /* Read and
17a2b 20 77 72 69 74 65 20 72 65 74 75 72 6e 20 63 6f   write return co
17a2c 64 65 20 66 72 6f 6d 20 68 65 72 65 20 2a 2f 0a  de from here */.
17a2d 29 7b 0a 20 20 69 6e 74 20 69 64 78 3b 20 20 20  ){.  int idx;   
17a2e 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20         /* Where 
17a2f 74 6f 20 77 72 69 74 65 20 6e 65 77 20 63 65 6c  to write new cel
17a30 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 64 61 74  l content in dat
17a31 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20  a[] */.  int j; 
17a32 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
17a33 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
17a34 69 6e 74 20 65 6e 64 3b 20 20 20 20 20 20 20 20  int end;        
17a35 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20    /* First byte 
17a36 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 63 65  past the last ce
17a37 6c 6c 20 70 6f 69 6e 74 65 72 20 69 6e 20 64 61  ll pointer in da
17a38 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 6e  ta[] */.  int in
17a39 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  s;          /* I
17a3a 6e 64 65 78 20 69 6e 20 64 61 74 61 5b 5d 20 77  ndex in data[] w
17a3b 68 65 72 65 20 6e 65 77 20 63 65 6c 6c 20 70 6f  here new cell po
17a3c 69 6e 74 65 72 20 69 73 20 69 6e 73 65 72 74 65  inter is inserte
17a3d 64 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f  d */.  int cellO
17a3e 66 66 73 65 74 3b 20 20 20 2f 2a 20 41 64 64 72  ffset;   /* Addr
17a3f 65 73 73 20 6f 66 20 66 69 72 73 74 20 63 65 6c  ess of first cel
17a40 6c 20 70 6f 69 6e 74 65 72 20 69 6e 20 64 61 74  l pointer in dat
17a41 61 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74  a[] */.  u8 *dat
17a42 61 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  a;         /* Th
17a43 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  e content of the
17a44 20 77 68 6f 6c 65 20 70 61 67 65 20 2a 2f 0a 20   whole page */. 
17a45 20 75 38 20 2a 70 74 72 3b 20 20 20 20 20 20 20   u8 *ptr;       
17a46 20 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 6d     /* Used for m
17a47 6f 76 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f  oving informatio
17a48 6e 20 61 72 6f 75 6e 64 20 69 6e 20 64 61 74 61  n around in data
17a49 5b 5d 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 53 6b  [] */..  int nSk
17a4a 69 70 20 3d 20 28 69 43 68 69 6c 64 20 3f 20 34  ip = (iChild ? 4
17a4b 20 3a 20 30 29 3b 0a 0a 20 20 69 66 28 20 2a 70   : 0);..  if( *p
17a4c 52 43 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20  RC ) return;..  
17a4d 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
17a4e 69 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2b  i<=pPage->nCell+
17a4f 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
17a50 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
17a51 61 67 65 2d 3e 6e 43 65 6c 6c 3c 3d 4d 58 5f 43  age->nCell<=MX_C
17a52 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 20  ELL(pPage->pBt) 
17a53 26 26 20 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65  && MX_CELL(pPage
17a54 2d 3e 70 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a  ->pBt)<=5460 );.
17a55 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
17a56 3e 6e 4f 76 65 72 66 6c 6f 77 3c 3d 41 72 72 61  >nOverflow<=Arra
17a57 79 53 69 7a 65 28 70 50 61 67 65 2d 3e 61 4f 76  ySize(pPage->aOv
17a58 66 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  fl) );.  assert(
17a59 20 73 7a 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72   sz==cellSizePtr
17a5a 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 20 29  (pPage, pCell) )
17a5b 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
17a5c 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
17a5d 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
17a5e 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65  ) );.  if( pPage
17a5f 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 7c 7c 20 73  ->nOverflow || s
17a60 7a 2b 32 3e 70 50 61 67 65 2d 3e 6e 46 72 65 65  z+2>pPage->nFree
17a61 20 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65 6d   ){.    if( pTem
17a62 70 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  p ){.      memcp
17a63 79 28 70 54 65 6d 70 2b 6e 53 6b 69 70 2c 20 70  y(pTemp+nSkip, p
17a64 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e  Cell+nSkip, sz-n
17a65 53 6b 69 70 29 3b 0a 20 20 20 20 20 20 70 43 65  Skip);.      pCe
17a66 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20  ll = pTemp;.    
17a67 7d 0a 20 20 20 20 69 66 28 20 69 43 68 69 6c 64  }.    if( iChild
17a68 20 29 7b 0a 20 20 20 20 20 20 70 75 74 34 62 79   ){.      put4by
17a69 74 65 28 70 43 65 6c 6c 2c 20 69 43 68 69 6c 64  te(pCell, iChild
17a6a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6a 20 3d  );.    }.    j =
17a6b 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f   pPage->nOverflo
17a6c 77 2b 2b 3b 0a 20 20 20 20 61 73 73 65 72 74 28  w++;.    assert(
17a6d 20 6a 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66 28   j<(int)(sizeof(
17a6e 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29 2f 73 69  pPage->aOvfl)/si
17a6f 7a 65 6f 66 28 70 50 61 67 65 2d 3e 61 4f 76 66  zeof(pPage->aOvf
17a70 6c 5b 30 5d 29 29 20 29 3b 0a 20 20 20 20 70 50  l[0])) );.    pP
17a71 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 70 43  age->aOvfl[j].pC
17a72 65 6c 6c 20 3d 20 70 43 65 6c 6c 3b 0a 20 20 20  ell = pCell;.   
17a73 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d   pPage->aOvfl[j]
17a74 2e 69 64 78 20 3d 20 28 75 31 36 29 69 3b 0a 20  .idx = (u16)i;. 
17a75 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20   }else{.    int 
17a76 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
17a77 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44  rWrite(pPage->pD
17a78 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20  bPage);.    if( 
17a79 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
17a7a 0a 20 20 20 20 20 20 2a 70 52 43 20 3d 20 72 63  .      *pRC = rc
17a7b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
17a7c 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
17a7d 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
17a7e 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d  writeable(pPage-
17a7f 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20  >pDbPage) );.   
17a80 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61   data = pPage->a
17a81 44 61 74 61 3b 0a 20 20 20 20 63 65 6c 6c 4f 66  Data;.    cellOf
17a82 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 63 65  fset = pPage->ce
17a83 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 20 20 65 6e  llOffset;.    en
17a84 64 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b  d = cellOffset +
17a85 20 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b   2*pPage->nCell;
17a86 0a 20 20 20 20 69 6e 73 20 3d 20 63 65 6c 6c 4f  .    ins = cellO
17a87 66 66 73 65 74 20 2b 20 32 2a 69 3b 0a 20 20 20  ffset + 2*i;.   
17a88 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 53 70   rc = allocateSp
17a89 61 63 65 28 70 50 61 67 65 2c 20 73 7a 2c 20 26  ace(pPage, sz, &
17a8a 69 64 78 29 3b 0a 20 20 20 20 69 66 28 20 72 63  idx);.    if( rc
17a8b 20 29 7b 20 2a 70 52 43 20 3d 20 72 63 3b 20 72   ){ *pRC = rc; r
17a8c 65 74 75 72 6e 3b 20 7d 0a 20 20 20 20 2f 2a 20  eturn; }.    /* 
17a8d 54 68 65 20 61 6c 6c 6f 63 61 74 65 53 70 61 63  The allocateSpac
17a8e 65 28 29 20 72 6f 75 74 69 6e 65 20 67 75 61 72  e() routine guar
17a8f 61 6e 74 65 65 73 20 74 68 65 20 66 6f 6c 6c 6f  antees the follo
17a90 77 69 6e 67 20 74 77 6f 20 70 72 6f 70 65 72 74  wing two propert
17a91 69 65 73 0a 20 20 20 20 2a 2a 20 69 66 20 69 74  ies.    ** if it
17a92 20 72 65 74 75 72 6e 73 20 73 75 63 63 65 73 73   returns success
17a93 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
17a94 69 64 78 20 3e 3d 20 65 6e 64 2b 32 20 29 3b 0a  idx >= end+2 );.
17a95 20 20 20 20 61 73 73 65 72 74 28 20 69 64 78 2b      assert( idx+
17a96 73 7a 20 3c 3d 20 70 50 61 67 65 2d 3e 70 42 74  sz <= pPage->pBt
17a97 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a  ->usableSize );.
17a98 20 20 20 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c      pPage->nCell
17a99 2b 2b 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e  ++;.    pPage->n
17a9a 46 72 65 65 20 2d 3d 20 28 75 31 36 29 28 32 20  Free -= (u16)(2 
17a9b 2b 20 73 7a 29 3b 0a 20 20 20 20 6d 65 6d 63 70  + sz);.    memcp
17a9c 79 28 26 64 61 74 61 5b 69 64 78 2b 6e 53 6b 69  y(&data[idx+nSki
17a9d 70 5d 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c  p], pCell+nSkip,
17a9e 20 73 7a 2d 6e 53 6b 69 70 29 3b 0a 20 20 20 20   sz-nSkip);.    
17a9f 69 66 28 20 69 43 68 69 6c 64 20 29 7b 0a 20 20  if( iChild ){.  
17aa0 20 20 20 20 70 75 74 34 62 79 74 65 28 26 64 61      put4byte(&da
17aa1 74 61 5b 69 64 78 5d 2c 20 69 43 68 69 6c 64 29  ta[idx], iChild)
17aa2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  ;.    }.    for(
17aa3 6a 3d 65 6e 64 2c 20 70 74 72 3d 26 64 61 74 61  j=end, ptr=&data
17aa4 5b 6a 5d 3b 20 6a 3e 69 6e 73 3b 20 6a 2d 3d 32  [j]; j>ins; j-=2
17aa5 2c 20 70 74 72 2d 3d 32 29 7b 0a 20 20 20 20 20  , ptr-=2){.     
17aa6 20 70 74 72 5b 30 5d 20 3d 20 70 74 72 5b 2d 32   ptr[0] = ptr[-2
17aa7 5d 3b 0a 20 20 20 20 20 20 70 74 72 5b 31 5d 20  ];.      ptr[1] 
17aa8 3d 20 70 74 72 5b 2d 31 5d 3b 0a 20 20 20 20 7d  = ptr[-1];.    }
17aa9 0a 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64  .    put2byte(&d
17aaa 61 74 61 5b 69 6e 73 5d 2c 20 69 64 78 29 3b 0a  ata[ins], idx);.
17aab 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61      put2byte(&da
17aac 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66  ta[pPage->hdrOff
17aad 73 65 74 2b 33 5d 2c 20 70 50 61 67 65 2d 3e 6e  set+3], pPage->n
17aae 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53  Cell);.#ifndef S
17aaf 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
17ab0 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 50  ACUUM.    if( pP
17ab1 61 67 65 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61  age->pBt->autoVa
17ab2 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 2f 2a  cuum ){.      /*
17ab3 20 54 68 65 20 63 65 6c 6c 20 6d 61 79 20 63 6f   The cell may co
17ab4 6e 74 61 69 6e 20 61 20 70 6f 69 6e 74 65 72 20  ntain a pointer 
17ab5 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70  to an overflow p
17ab6 61 67 65 2e 20 49 66 20 73 6f 2c 20 77 72 69 74  age. If so, writ
17ab7 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 65  e.      ** the e
17ab8 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6f 76 65  ntry for the ove
17ab9 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 74 6f 20  rflow page into 
17aba 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e  the pointer map.
17abb 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
17abc 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72  ptrmapPutOvflPtr
17abd 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 70  (pPage, pCell, p
17abe 52 43 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  RC);.    }.#endi
17abf 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  f.  }.}../*.** A
17ac0 64 64 20 61 20 6c 69 73 74 20 6f 66 20 63 65 6c  dd a list of cel
17ac1 6c 73 20 74 6f 20 61 20 70 61 67 65 2e 20 20 54  ls to a page.  T
17ac2 68 65 20 70 61 67 65 20 73 68 6f 75 6c 64 20 62  he page should b
17ac3 65 20 69 6e 69 74 69 61 6c 6c 79 20 65 6d 70 74  e initially empt
17ac4 79 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 73 20  y..** The cells 
17ac5 61 72 65 20 67 75 61 72 61 6e 74 65 65 64 20 74  are guaranteed t
17ac6 6f 20 66 69 74 20 6f 6e 20 74 68 65 20 70 61 67  o fit on the pag
17ac7 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
17ac8 64 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 0a  d assemblePage(.
17ac9 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
17aca 2c 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20  ,   /* The page 
17acb 74 6f 20 62 65 20 61 73 73 65 6d 62 6c 69 65 64  to be assemblied
17acc 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c   */.  int nCell,
17acd 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e          /* The n
17ace 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 74  umber of cells t
17acf 6f 20 61 64 64 20 74 6f 20 74 68 69 73 20 70 61  o add to this pa
17ad0 67 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43  ge */.  u8 **apC
17ad1 65 6c 6c 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69  ell,      /* Poi
17ad2 6e 74 65 72 73 20 74 6f 20 63 65 6c 6c 20 62 6f  nters to cell bo
17ad3 64 69 65 73 20 2a 2f 0a 20 20 75 31 36 20 2a 61  dies */.  u16 *a
17ad4 53 69 7a 65 20 20 20 20 20 20 20 20 2f 2a 20 53  Size        /* S
17ad5 69 7a 65 73 20 6f 66 20 74 68 65 20 63 65 6c 6c  izes of the cell
17ad6 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  s */.){.  int i;
17ad7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
17ad8 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
17ad9 20 75 38 20 2a 70 43 65 6c 6c 70 74 72 3b 20 20   u8 *pCellptr;  
17ada 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
17adb 20 6e 65 78 74 20 63 65 6c 6c 20 70 6f 69 6e 74   next cell point
17adc 65 72 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c  er */.  int cell
17add 62 6f 64 79 3b 20 20 20 20 20 2f 2a 20 41 64 64  body;     /* Add
17ade 72 65 73 73 20 6f 66 20 6e 65 78 74 20 63 65 6c  ress of next cel
17adf 6c 20 62 6f 64 79 20 2a 2f 0a 20 20 75 38 20 2a  l body */.  u8 *
17ae0 20 63 6f 6e 73 74 20 64 61 74 61 20 3d 20 70 50   const data = pP
17ae1 61 67 65 2d 3e 61 44 61 74 61 3b 20 20 20 20 20  age->aData;     
17ae2 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
17ae3 65 72 20 74 6f 20 64 61 74 61 20 66 6f 72 20 70  er to data for p
17ae4 50 61 67 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  Page */.  const 
17ae5 69 6e 74 20 68 64 72 20 3d 20 70 50 61 67 65 2d  int hdr = pPage-
17ae6 3e 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20 20  >hdrOffset;     
17ae7 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
17ae8 6f 66 20 68 65 61 64 65 72 20 6f 6e 20 70 50 61  of header on pPa
17ae9 67 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 69 6e  ge */.  const in
17aea 74 20 6e 55 73 61 62 6c 65 20 3d 20 70 50 61 67  t nUsable = pPag
17aeb 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  e->pBt->usableSi
17aec 7a 65 3b 20 2f 2a 20 55 73 61 62 6c 65 20 73 69  ze; /* Usable si
17aed 7a 65 20 6f 66 20 70 61 67 65 20 2a 2f 0a 0a 20  ze of page */.. 
17aee 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
17aef 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a  nOverflow==0 );.
17af0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
17af1 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
17af2 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
17af3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 43 65  );.  assert( nCe
17af4 6c 6c 3e 3d 30 20 26 26 20 6e 43 65 6c 6c 3c 3d  ll>=0 && nCell<=
17af5 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70  MX_CELL(pPage->p
17af6 42 74 29 20 26 26 20 4d 58 5f 43 45 4c 4c 28 70  Bt) && MX_CELL(p
17af7 50 61 67 65 2d 3e 70 42 74 29 3c 3d 35 34 36 30  Page->pBt)<=5460
17af8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
17af9 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
17afa 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62  eable(pPage->pDb
17afb 50 61 67 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 43  Page) );..  /* C
17afc 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 61  heck that the pa
17afd 67 65 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e  ge has just been
17afe 20 7a 65 72 6f 65 64 20 62 79 20 7a 65 72 6f 50   zeroed by zeroP
17aff 61 67 65 28 29 20 2a 2f 0a 20 20 61 73 73 65 72  age() */.  asser
17b00 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d  t( pPage->nCell=
17b01 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
17b02 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
17b03 64 72 2b 35 5d 29 3d 3d 6e 55 73 61 62 6c 65 20  dr+5])==nUsable 
17b04 29 3b 0a 0a 20 20 70 43 65 6c 6c 70 74 72 20 3d  );..  pCellptr =
17b05 20 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 63 65   &data[pPage->ce
17b06 6c 6c 4f 66 66 73 65 74 20 2b 20 6e 43 65 6c 6c  llOffset + nCell
17b07 2a 32 5d 3b 0a 20 20 63 65 6c 6c 62 6f 64 79 20  *2];.  cellbody 
17b08 3d 20 6e 55 73 61 62 6c 65 3b 0a 20 20 66 6f 72  = nUsable;.  for
17b09 28 69 3d 6e 43 65 6c 6c 2d 31 3b 20 69 3e 3d 30  (i=nCell-1; i>=0
17b0a 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 70 43 65 6c  ; i--){.    pCel
17b0b 6c 70 74 72 20 2d 3d 20 32 3b 0a 20 20 20 20 63  lptr -= 2;.    c
17b0c 65 6c 6c 62 6f 64 79 20 2d 3d 20 61 53 69 7a 65  ellbody -= aSize
17b0d 5b 69 5d 3b 0a 20 20 20 20 70 75 74 32 62 79 74  [i];.    put2byt
17b0e 65 28 70 43 65 6c 6c 70 74 72 2c 20 63 65 6c 6c  e(pCellptr, cell
17b0f 62 6f 64 79 29 3b 0a 20 20 20 20 6d 65 6d 63 70  body);.    memcp
17b10 79 28 26 64 61 74 61 5b 63 65 6c 6c 62 6f 64 79  y(&data[cellbody
17b11 5d 2c 20 61 70 43 65 6c 6c 5b 69 5d 2c 20 61 53  ], apCell[i], aS
17b12 69 7a 65 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 70  ize[i]);.  }.  p
17b13 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  ut2byte(&data[hd
17b14 72 2b 33 5d 2c 20 6e 43 65 6c 6c 29 3b 0a 20 20  r+3], nCell);.  
17b15 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  put2byte(&data[h
17b16 64 72 2b 35 5d 2c 20 63 65 6c 6c 62 6f 64 79 29  dr+5], cellbody)
17b17 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65  ;.  pPage->nFree
17b18 20 2d 3d 20 28 6e 43 65 6c 6c 2a 32 20 2b 20 6e   -= (nCell*2 + n
17b19 55 73 61 62 6c 65 20 2d 20 63 65 6c 6c 62 6f 64  Usable - cellbod
17b1a 79 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65  y);.  pPage->nCe
17b1b 6c 6c 20 3d 20 28 75 31 36 29 6e 43 65 6c 6c 3b  ll = (u16)nCell;
17b1c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  .}../*.** The fo
17b1d 6c 6c 6f 77 69 6e 67 20 70 61 72 61 6d 65 74 65  llowing paramete
17b1e 72 73 20 64 65 74 65 72 6d 69 6e 65 20 68 6f 77  rs determine how
17b1f 20 6d 61 6e 79 20 61 64 6a 61 63 65 6e 74 20 70   many adjacent p
17b20 61 67 65 73 20 67 65 74 20 69 6e 76 6f 6c 76 65  ages get involve
17b21 64 0a 2a 2a 20 69 6e 20 61 20 62 61 6c 61 6e 63  d.** in a balanc
17b22 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20  ing operation.  
17b23 4e 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  NN is the number
17b24 20 6f 66 20 6e 65 69 67 68 62 6f 72 73 20 6f 6e   of neighbors on
17b25 20 65 69 74 68 65 72 20 73 69 64 65 0a 2a 2a 20   either side.** 
17b26 6f 66 20 74 68 65 20 70 61 67 65 20 74 68 61 74  of the page that
17b27 20 70 61 72 74 69 63 69 70 61 74 65 20 69 6e 20   participate in 
17b28 74 68 65 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70  the balancing op
17b29 65 72 61 74 69 6f 6e 2e 20 20 4e 42 20 69 73 20  eration.  NB is 
17b2a 74 68 65 0a 2a 2a 20 74 6f 74 61 6c 20 6e 75 6d  the.** total num
17b2b 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 68 61  ber of pages tha
17b2c 74 20 70 61 72 74 69 63 69 70 61 74 65 2c 20 69  t participate, i
17b2d 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 74 61 72  ncluding the tar
17b2e 67 65 74 20 70 61 67 65 20 61 6e 64 0a 2a 2a 20  get page and.** 
17b2f 4e 4e 20 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20  NN neighbors on 
17b30 65 69 74 68 65 72 20 73 69 64 65 2e 0a 2a 2a 0a  either side..**.
17b31 2a 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 76  ** The minimum v
17b32 61 6c 75 65 20 6f 66 20 4e 4e 20 69 73 20 31 20  alue of NN is 1 
17b33 28 6f 66 20 63 6f 75 72 73 65 29 2e 20 20 49 6e  (of course).  In
17b34 63 72 65 61 73 69 6e 67 20 4e 4e 20 61 62 6f 76  creasing NN abov
17b35 65 20 31 0a 2a 2a 20 28 74 6f 20 32 20 6f 72 20  e 1.** (to 2 or 
17b36 33 29 20 67 69 76 65 73 20 61 20 6d 6f 64 65 73  3) gives a modes
17b37 74 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 69 6e  t improvement in
17b38 20 53 45 4c 45 43 54 20 61 6e 64 20 44 45 4c 45   SELECT and DELE
17b39 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 65 0a 2a  TE performance.*
17b3a 2a 20 69 6e 20 65 78 63 68 61 6e 67 65 20 66 6f  * in exchange fo
17b3b 72 20 61 20 6c 61 72 67 65 72 20 64 65 67 72 61  r a larger degra
17b3c 64 61 74 69 6f 6e 20 69 6e 20 49 4e 53 45 52 54  dation in INSERT
17b3d 20 61 6e 64 20 55 50 44 41 54 45 20 70 65 72 66   and UPDATE perf
17b3e 6f 72 6d 61 6e 63 65 2e 0a 2a 2a 20 54 68 65 20  ormance..** The 
17b3f 76 61 6c 75 65 20 6f 66 20 4e 4e 20 61 70 70 65  value of NN appe
17b40 61 72 73 20 74 6f 20 67 69 76 65 20 74 68 65 20  ars to give the 
17b41 62 65 73 74 20 72 65 73 75 6c 74 73 20 6f 76 65  best results ove
17b42 72 61 6c 6c 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  rall..*/.#define
17b43 20 4e 4e 20 31 20 20 20 20 20 20 20 20 20 20 20   NN 1           
17b44 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e    /* Number of n
17b45 65 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68  eighbors on eith
17b46 65 72 20 73 69 64 65 20 6f 66 20 70 50 61 67 65  er side of pPage
17b47 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 42 20 28   */.#define NB (
17b48 4e 4e 2a 32 2b 31 29 20 20 20 20 20 20 2f 2a 20  NN*2+1)      /* 
17b49 54 6f 74 61 6c 20 70 61 67 65 73 20 69 6e 76 6f  Total pages invo
17b4a 6c 76 65 64 20 69 6e 20 74 68 65 20 62 61 6c 61  lved in the bala
17b4b 6e 63 65 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66  nce */...#ifndef
17b4c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49   SQLITE_OMIT_QUI
17b4d 43 4b 42 41 4c 41 4e 43 45 0a 2f 2a 0a 2a 2a 20  CKBALANCE./*.** 
17b4e 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  This version of 
17b4f 62 61 6c 61 6e 63 65 28 29 20 68 61 6e 64 6c 65  balance() handle
17b50 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 73 70 65  s the common spe
17b51 63 69 61 6c 20 63 61 73 65 20 77 68 65 72 65 0a  cial case where.
17b52 2a 2a 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69  ** a new entry i
17b53 73 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64  s being inserted
17b54 20 6f 6e 20 74 68 65 20 65 78 74 72 65 6d 65 20   on the extreme 
17b55 72 69 67 68 74 2d 65 6e 64 20 6f 66 20 74 68 65  right-end of the
17b56 0a 2a 2a 20 74 72 65 65 2c 20 69 6e 20 6f 74 68  .** tree, in oth
17b57 65 72 20 77 6f 72 64 73 2c 20 77 68 65 6e 20 74  er words, when t
17b58 68 65 20 6e 65 77 20 65 6e 74 72 79 20 77 69 6c  he new entry wil
17b59 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6c 61 72  l become the lar
17b5a 67 65 73 74 0a 2a 2a 20 65 6e 74 72 79 20 69 6e  gest.** entry in
17b5b 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a   the tree..**.**
17b5c 20 49 6e 73 74 65 61 64 20 6f 66 20 74 72 79 69   Instead of tryi
17b5d 6e 67 20 74 6f 20 62 61 6c 61 6e 63 65 20 74 68  ng to balance th
17b5e 65 20 33 20 72 69 67 68 74 2d 6d 6f 73 74 20 6c  e 3 right-most l
17b5f 65 61 66 20 70 61 67 65 73 2c 20 6a 75 73 74 20  eaf pages, just 
17b60 61 64 64 0a 2a 2a 20 61 20 6e 65 77 20 70 61 67  add.** a new pag
17b61 65 20 74 6f 20 74 68 65 20 72 69 67 68 74 2d 68  e to the right-h
17b62 61 6e 64 20 73 69 64 65 20 61 6e 64 20 70 75 74  and side and put
17b63 20 74 68 65 20 6f 6e 65 20 6e 65 77 20 65 6e 74   the one new ent
17b64 72 79 20 69 6e 0a 2a 2a 20 74 68 61 74 20 70 61  ry in.** that pa
17b65 67 65 2e 20 20 54 68 69 73 20 6c 65 61 76 65 73  ge.  This leaves
17b66 20 74 68 65 20 72 69 67 68 74 20 73 69 64 65 20   the right side 
17b67 6f 66 20 74 68 65 20 74 72 65 65 20 73 6f 6d 65  of the tree some
17b68 77 68 61 74 0a 2a 2a 20 75 6e 62 61 6c 61 6e 63  what.** unbalanc
17b69 65 64 2e 20 20 42 75 74 20 6f 64 64 73 20 61 72  ed.  But odds ar
17b6a 65 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 62  e that we will b
17b6b 65 20 69 6e 73 65 72 74 69 6e 67 20 6e 65 77 20  e inserting new 
17b6c 65 6e 74 72 69 65 73 0a 2a 2a 20 61 74 20 74 68  entries.** at th
17b6d 65 20 65 6e 64 20 73 6f 6f 6e 20 61 66 74 65 72  e end soon after
17b6e 77 61 72 64 73 20 73 6f 20 74 68 65 20 6e 65 61  wards so the nea
17b6f 72 6c 79 20 65 6d 70 74 79 20 70 61 67 65 20 77  rly empty page w
17b70 69 6c 6c 20 71 75 69 63 6b 6c 79 0a 2a 2a 20 66  ill quickly.** f
17b71 69 6c 6c 20 75 70 2e 20 20 4f 6e 20 61 76 65 72  ill up.  On aver
17b72 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 67 65  age..**.** pPage
17b73 20 69 73 20 74 68 65 20 6c 65 61 66 20 70 61 67   is the leaf pag
17b74 65 20 77 68 69 63 68 20 69 73 20 74 68 65 20 72  e which is the r
17b75 69 67 68 74 2d 6d 6f 73 74 20 70 61 67 65 20 69  ight-most page i
17b76 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 20 70  n the tree..** p
17b77 50 61 72 65 6e 74 20 69 73 20 69 74 73 20 70 61  Parent is its pa
17b78 72 65 6e 74 2e 20 20 70 50 61 67 65 20 6d 75 73  rent.  pPage mus
17b79 74 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65 20  t have a single 
17b7a 6f 76 65 72 66 6c 6f 77 20 65 6e 74 72 79 0a 2a  overflow entry.*
17b7b 2a 20 77 68 69 63 68 20 69 73 20 61 6c 73 6f 20  * which is also 
17b7c 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 65  the right-most e
17b7d 6e 74 72 79 20 6f 6e 20 74 68 65 20 70 61 67 65  ntry on the page
17b7e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 53 70 61  ..**.** The pSpa
17b7f 63 65 20 62 75 66 66 65 72 20 69 73 20 75 73 65  ce buffer is use
17b80 64 20 74 6f 20 73 74 6f 72 65 20 61 20 74 65 6d  d to store a tem
17b81 70 6f 72 61 72 79 20 63 6f 70 79 20 6f 66 20 74  porary copy of t
17b82 68 65 20 64 69 76 69 64 65 72 0a 2a 2a 20 63 65  he divider.** ce
17b83 6c 6c 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20  ll that will be 
17b84 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 70 50  inserted into pP
17b85 61 72 65 6e 74 2e 20 53 75 63 68 20 61 20 63 65  arent. Such a ce
17b86 6c 6c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61  ll consists of a
17b87 20 34 0a 2a 2a 20 62 79 74 65 20 70 61 67 65 20   4.** byte page 
17b88 6e 75 6d 62 65 72 20 66 6f 6c 6c 6f 77 65 64 20  number followed 
17b89 62 79 20 61 20 76 61 72 69 61 62 6c 65 20 6c 65  by a variable le
17b8a 6e 67 74 68 20 69 6e 74 65 67 65 72 2e 20 49 6e  ngth integer. In
17b8b 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c   other.** words,
17b8c 20 61 74 20 6d 6f 73 74 20 31 33 20 62 79 74 65   at most 13 byte
17b8d 73 2e 20 48 65 6e 63 65 20 74 68 65 20 70 53 70  s. Hence the pSp
17b8e 61 63 65 20 62 75 66 66 65 72 20 6d 75 73 74 20  ace buffer must 
17b8f 62 65 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20 31  be at.** least 1
17b90 33 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e  3 bytes in size.
17b91 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
17b92 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 4d 65 6d  alance_quick(Mem
17b93 50 61 67 65 20 2a 70 50 61 72 65 6e 74 2c 20 4d  Page *pParent, M
17b94 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75  emPage *pPage, u
17b95 38 20 2a 70 53 70 61 63 65 29 7b 0a 20 20 42 74  8 *pSpace){.  Bt
17b96 53 68 61 72 65 64 20 2a 63 6f 6e 73 74 20 70 42  Shared *const pB
17b97 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 20  t = pPage->pBt; 
17b98 20 20 20 2f 2a 20 42 2d 54 72 65 65 20 44 61 74     /* B-Tree Dat
17b99 61 62 61 73 65 20 2a 2f 0a 20 20 4d 65 6d 50 61  abase */.  MemPa
17b9a 67 65 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20  ge *pNew;       
17b9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17b9c 2f 2a 20 4e 65 77 6c 79 20 61 6c 6c 6f 63 61 74  /* Newly allocat
17b9d 65 64 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74  ed page */.  int
17b9e 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
17b9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17ba0 20 20 2f 2a 20 52 65 74 75 72 6e 20 43 6f 64 65    /* Return Code
17ba1 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 4e   */.  Pgno pgnoN
17ba2 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ew;             
17ba3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
17ba4 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 4e 65  ge number of pNe
17ba5 77 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  w */..  assert( 
17ba6 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
17ba7 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
17ba8 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
17ba9 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
17baa 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 65  swriteable(pPare
17bab 6e 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  nt->pDbPage) );.
17bac 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
17bad 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20 29 3b  >nOverflow==1 );
17bae 0a 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6e  ..  if( pPage->n
17baf 43 65 6c 6c 3c 3d 30 20 29 20 72 65 74 75 72 6e  Cell<=0 ) return
17bb0 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
17bb1 42 4b 50 54 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  BKPT;..  /* Allo
17bb2 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e  cate a new page.
17bb3 20 54 68 69 73 20 70 61 67 65 20 77 69 6c 6c 20   This page will 
17bb4 62 65 63 6f 6d 65 20 74 68 65 20 72 69 67 68 74  become the right
17bb5 2d 73 69 62 6c 69 6e 67 20 6f 66 20 0a 20 20 2a  -sibling of .  *
17bb6 2a 20 70 50 61 67 65 2e 20 4d 61 6b 65 20 74 68  * pPage. Make th
17bb7 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 77 72  e parent page wr
17bb8 69 74 61 62 6c 65 2c 20 73 6f 20 74 68 61 74 20  itable, so that 
17bb9 74 68 65 20 6e 65 77 20 64 69 76 69 64 65 72 20  the new divider 
17bba 63 65 6c 6c 0a 20 20 2a 2a 20 6d 61 79 20 62 65  cell.  ** may be
17bbb 20 69 6e 73 65 72 74 65 64 2e 20 49 66 20 62 6f   inserted. If bo
17bbc 74 68 20 74 68 65 73 65 20 6f 70 65 72 61 74 69  th these operati
17bbd 6f 6e 73 20 61 72 65 20 73 75 63 63 65 73 73 66  ons are successf
17bbe 75 6c 2c 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a  ul, proceed..  *
17bbf 2f 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74  /.  rc = allocat
17bc0 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20  eBtreePage(pBt, 
17bc1 26 70 4e 65 77 2c 20 26 70 67 6e 6f 4e 65 77 2c  &pNew, &pgnoNew,
17bc2 20 30 2c 20 30 29 3b 0a 0a 20 20 69 66 28 20 72   0, 0);..  if( r
17bc3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
17bc4 0a 20 20 20 20 75 38 20 2a 70 4f 75 74 20 3d 20  .    u8 *pOut = 
17bc5 26 70 53 70 61 63 65 5b 34 5d 3b 0a 20 20 20 20  &pSpace[4];.    
17bc6 75 38 20 2a 70 43 65 6c 6c 20 3d 20 70 50 61 67  u8 *pCell = pPag
17bc7 65 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 70 43 65 6c  e->aOvfl[0].pCel
17bc8 6c 3b 0a 20 20 20 20 75 31 36 20 73 7a 43 65 6c  l;.    u16 szCel
17bc9 6c 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  l = cellSizePtr(
17bca 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20  pPage, pCell);. 
17bcb 20 20 20 75 38 20 2a 70 53 74 6f 70 3b 0a 0a 20     u8 *pStop;.. 
17bcc 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
17bcd 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
17bce 6c 65 28 70 4e 65 77 2d 3e 70 44 62 50 61 67 65  le(pNew->pDbPage
17bcf 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ) );.    assert(
17bd0 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d   pPage->aData[0]
17bd1 3d 3d 28 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54  ==(PTF_INTKEY|PT
17bd2 46 5f 4c 45 41 46 44 41 54 41 7c 50 54 46 5f 4c  F_LEAFDATA|PTF_L
17bd3 45 41 46 29 20 29 3b 0a 20 20 20 20 7a 65 72 6f  EAF) );.    zero
17bd4 50 61 67 65 28 70 4e 65 77 2c 20 50 54 46 5f 49  Page(pNew, PTF_I
17bd5 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 44 41  NTKEY|PTF_LEAFDA
17bd6 54 41 7c 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20  TA|PTF_LEAF);.  
17bd7 20 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70    assemblePage(p
17bd8 4e 65 77 2c 20 31 2c 20 26 70 43 65 6c 6c 2c 20  New, 1, &pCell, 
17bd9 26 73 7a 43 65 6c 6c 29 3b 0a 0a 20 20 20 20 2f  &szCell);..    /
17bda 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  * If this is an 
17bdb 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61  auto-vacuum data
17bdc 62 61 73 65 2c 20 75 70 64 61 74 65 20 74 68 65  base, update the
17bdd 20 70 6f 69 6e 74 65 72 20 6d 61 70 0a 20 20 20   pointer map.   
17bde 20 2a 2a 20 77 69 74 68 20 65 6e 74 72 69 65 73   ** with entries
17bdf 20 66 6f 72 20 74 68 65 20 6e 65 77 20 70 61 67   for the new pag
17be0 65 2c 20 61 6e 64 20 61 6e 79 20 70 6f 69 6e 74  e, and any point
17be1 65 72 20 66 72 6f 6d 20 74 68 65 20 0a 20 20 20  er from the .   
17be2 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 74 68 65 20   ** cell on the 
17be3 70 61 67 65 20 74 6f 20 61 6e 20 6f 76 65 72 66  page to an overf
17be4 6c 6f 77 20 70 61 67 65 2e 20 49 66 20 65 69 74  low page. If eit
17be5 68 65 72 20 6f 66 20 74 68 65 73 65 0a 20 20 20  her of these.   
17be6 20 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 73 20 66   ** operations f
17be7 61 69 6c 73 2c 20 74 68 65 20 72 65 74 75 72 6e  ails, the return
17be8 20 63 6f 64 65 20 69 73 20 73 65 74 2c 20 62 75   code is set, bu
17be9 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 20  t the contents. 
17bea 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 72     ** of the par
17beb 65 6e 74 20 70 61 67 65 20 61 72 65 20 73 74 69  ent page are sti
17bec 6c 6c 20 6d 61 6e 69 70 75 6c 61 74 65 64 20 62  ll manipulated b
17bed 79 20 74 68 68 20 63 6f 64 65 20 62 65 6c 6f 77  y thh code below
17bee 2e 0a 20 20 20 20 2a 2a 20 54 68 61 74 20 69 73  ..    ** That is
17bef 20 4f 6b 2c 20 61 74 20 74 68 69 73 20 70 6f 69   Ok, at this poi
17bf0 6e 74 20 74 68 65 20 70 61 72 65 6e 74 20 70 61  nt the parent pa
17bf1 67 65 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  ge is guaranteed
17bf2 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20 6d 61   to.    ** be ma
17bf3 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 52  rked as dirty. R
17bf4 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f  eturning an erro
17bf5 72 20 63 6f 64 65 20 77 69 6c 6c 20 63 61 75 73  r code will caus
17bf6 65 20 61 0a 20 20 20 20 2a 2a 20 72 6f 6c 6c 62  e a.    ** rollb
17bf7 61 63 6b 2c 20 75 6e 64 6f 69 6e 67 20 61 6e 79  ack, undoing any
17bf8 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 74 6f   changes made to
17bf9 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
17bfa 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
17bfb 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b   ISAUTOVACUUM ){
17bfc 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74  .      ptrmapPut
17bfd 28 70 42 74 2c 20 70 67 6e 6f 4e 65 77 2c 20 50  (pBt, pgnoNew, P
17bfe 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61  TRMAP_BTREE, pPa
17bff 72 65 6e 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29  rent->pgno, &rc)
17c00 3b 0a 20 20 20 20 20 20 69 66 28 20 73 7a 43 65  ;.      if( szCe
17c01 6c 6c 3e 70 4e 65 77 2d 3e 6d 69 6e 4c 6f 63 61  ll>pNew->minLoca
17c02 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 74 72  l ){.        ptr
17c03 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 4e  mapPutOvflPtr(pN
17c04 65 77 2c 20 70 43 65 6c 6c 2c 20 26 72 63 29 3b  ew, pCell, &rc);
17c05 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
17c06 20 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20   .    /* Create 
17c07 61 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 74  a divider cell t
17c08 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 70 50  o insert into pP
17c09 61 72 65 6e 74 2e 20 54 68 65 20 64 69 76 69 64  arent. The divid
17c0a 65 72 20 63 65 6c 6c 0a 20 20 20 20 2a 2a 20 63  er cell.    ** c
17c0b 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 34 2d 62  onsists of a 4-b
17c0c 79 74 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  yte page number 
17c0d 28 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72  (the page number
17c0e 20 6f 66 20 70 50 61 67 65 29 20 61 6e 64 0a 20   of pPage) and. 
17c0f 20 20 20 2a 2a 20 61 20 76 61 72 69 61 62 6c 65     ** a variable
17c10 20 6c 65 6e 67 74 68 20 6b 65 79 20 76 61 6c 75   length key valu
17c11 65 20 28 77 68 69 63 68 20 6d 75 73 74 20 62 65  e (which must be
17c12 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20   the same value 
17c13 61 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 6c 61  as the.    ** la
17c14 72 67 65 73 74 20 6b 65 79 20 6f 6e 20 70 50 61  rgest key on pPa
17c15 67 65 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ge)..    **.    
17c16 2a 2a 20 54 6f 20 66 69 6e 64 20 74 68 65 20 6c  ** To find the l
17c17 61 72 67 65 73 74 20 6b 65 79 20 76 61 6c 75 65  argest key value
17c18 20 6f 6e 20 70 50 61 67 65 2c 20 66 69 72 73 74   on pPage, first
17c19 20 66 69 6e 64 20 74 68 65 20 72 69 67 68 74 2d   find the right-
17c1a 6d 6f 73 74 20 0a 20 20 20 20 2a 2a 20 63 65 6c  most .    ** cel
17c1b 6c 20 6f 6e 20 70 50 61 67 65 2e 20 54 68 65 20  l on pPage. The 
17c1c 66 69 72 73 74 20 74 77 6f 20 66 69 65 6c 64 73  first two fields
17c1d 20 6f 66 20 74 68 69 73 20 63 65 6c 6c 20 61 72   of this cell ar
17c1e 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 65  e the .    ** re
17c1f 63 6f 72 64 2d 6c 65 6e 67 74 68 20 28 61 20 76  cord-length (a v
17c20 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69  ariable length i
17c21 6e 74 65 67 65 72 20 61 74 20 6d 6f 73 74 20 33  nteger at most 3
17c22 32 2d 62 69 74 73 20 69 6e 20 73 69 7a 65 29 0a  2-bits in size).
17c23 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 6b      ** and the k
17c24 65 79 20 76 61 6c 75 65 20 28 61 20 76 61 72 69  ey value (a vari
17c25 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65  able length inte
17c26 67 65 72 2c 20 6d 61 79 20 68 61 76 65 20 61 6e  ger, may have an
17c27 79 20 76 61 6c 75 65 29 2e 0a 20 20 20 20 2a 2a  y value)..    **
17c28 20 54 68 65 20 66 69 72 73 74 20 6f 66 20 74 68   The first of th
17c29 65 20 77 68 69 6c 65 28 2e 2e 2e 29 20 6c 6f 6f  e while(...) loo
17c2a 70 73 20 62 65 6c 6f 77 20 73 6b 69 70 73 20 6f  ps below skips o
17c2b 76 65 72 20 74 68 65 20 72 65 63 6f 72 64 2d 6c  ver the record-l
17c2c 65 6e 67 74 68 0a 20 20 20 20 2a 2a 20 66 69 65  ength.    ** fie
17c2d 6c 64 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 77  ld. The second w
17c2e 68 69 6c 65 28 2e 2e 2e 29 20 6c 6f 6f 70 20 63  hile(...) loop c
17c2f 6f 70 69 65 73 20 74 68 65 20 6b 65 79 20 76 61  opies the key va
17c30 6c 75 65 20 66 72 6f 6d 20 74 68 65 0a 20 20 20  lue from the.   
17c31 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67   ** cell on pPag
17c32 65 20 69 6e 74 6f 20 74 68 65 20 70 53 70 61 63  e into the pSpac
17c33 65 20 62 75 66 66 65 72 2e 0a 20 20 20 20 2a 2f  e buffer..    */
17c34 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e  .    pCell = fin
17c35 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 50 61  dCell(pPage, pPa
17c36 67 65 2d 3e 6e 43 65 6c 6c 2d 31 29 3b 0a 20 20  ge->nCell-1);.  
17c37 20 20 70 53 74 6f 70 20 3d 20 26 70 43 65 6c 6c    pStop = &pCell
17c38 5b 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  [9];.    while( 
17c39 28 2a 28 70 43 65 6c 6c 2b 2b 29 26 30 78 38 30  (*(pCell++)&0x80
17c3a 29 20 26 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70  ) && pCell<pStop
17c3b 20 29 3b 0a 20 20 20 20 70 53 74 6f 70 20 3d 20   );.    pStop = 
17c3c 26 70 43 65 6c 6c 5b 39 5d 3b 0a 20 20 20 20 77  &pCell[9];.    w
17c3d 68 69 6c 65 28 20 28 28 2a 28 70 4f 75 74 2b 2b  hile( ((*(pOut++
17c3e 29 20 3d 20 2a 28 70 43 65 6c 6c 2b 2b 29 29 26  ) = *(pCell++))&
17c3f 30 78 38 30 29 20 26 26 20 70 43 65 6c 6c 3c 70  0x80) && pCell<p
17c40 53 74 6f 70 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  Stop );..    /* 
17c41 49 6e 73 65 72 74 20 74 68 65 20 6e 65 77 20 64  Insert the new d
17c42 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 74 6f  ivider cell into
17c43 20 70 50 61 72 65 6e 74 2e 20 2a 2f 0a 20 20 20   pParent. */.   
17c44 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 72   insertCell(pPar
17c45 65 6e 74 2c 20 70 50 61 72 65 6e 74 2d 3e 6e 43  ent, pParent->nC
17c46 65 6c 6c 2c 20 70 53 70 61 63 65 2c 20 28 69 6e  ell, pSpace, (in
17c47 74 29 28 70 4f 75 74 2d 70 53 70 61 63 65 29 2c  t)(pOut-pSpace),
17c48 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
17c49 30 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20  0, pPage->pgno, 
17c4a 26 72 63 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65  &rc);..    /* Se
17c4b 74 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c  t the right-chil
17c4c 64 20 70 6f 69 6e 74 65 72 20 6f 66 20 70 50 61  d pointer of pPa
17c4d 72 65 6e 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f  rent to point to
17c4e 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e 20 2a   the new page. *
17c4f 2f 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 26  /.    put4byte(&
17c50 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70  pParent->aData[p
17c51 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65  Parent->hdrOffse
17c52 74 2b 38 5d 2c 20 70 67 6e 6f 4e 65 77 29 3b 0a  t+8], pgnoNew);.
17c53 20 20 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73    .    /* Releas
17c54 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  e the reference 
17c55 74 6f 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e  to the new page.
17c56 20 2a 2f 0a 20 20 20 20 72 65 6c 65 61 73 65 50   */.    releaseP
17c57 61 67 65 28 70 4e 65 77 29 3b 0a 20 20 7d 0a 0a  age(pNew);.  }..
17c58 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23    return rc;.}.#
17c59 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
17c5a 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e 43  OMIT_QUICKBALANC
17c5b 45 20 2a 2f 0a 0a 23 69 66 20 30 0a 2f 2a 0a 2a  E */..#if 0./*.*
17c5c 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
17c5d 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 72 69 62  does not contrib
17c5e 75 74 65 20 61 6e 79 74 68 69 6e 67 20 74 6f 20  ute anything to 
17c5f 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66  the operation of
17c60 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 69 74 20 69   SQLite..** it i
17c61 73 20 73 6f 6d 65 74 69 6d 65 73 20 61 63 74 69  s sometimes acti
17c62 76 61 74 65 64 20 74 65 6d 70 6f 72 61 72 69 6c  vated temporaril
17c63 79 20 77 68 69 6c 65 20 64 65 62 75 67 67 69 6e  y while debuggin
17c64 67 20 63 6f 64 65 20 72 65 73 70 6f 6e 73 69 62  g code responsib
17c65 6c 65 20 0a 2a 2a 20 66 6f 72 20 73 65 74 74 69  le .** for setti
17c66 6e 67 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65  ng pointer-map e
17c67 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  ntries..*/.stati
17c68 63 20 69 6e 74 20 70 74 72 6d 61 70 43 68 65 63  c int ptrmapChec
17c69 6b 50 61 67 65 73 28 4d 65 6d 50 61 67 65 20 2a  kPages(MemPage *
17c6a 2a 61 70 50 61 67 65 2c 20 69 6e 74 20 6e 50 61  *apPage, int nPa
17c6b 67 65 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  ge){.  int i, j;
17c6c 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 50  .  for(i=0; i<nP
17c6d 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 50  age; i++){.    P
17c6e 67 6e 6f 20 6e 3b 0a 20 20 20 20 75 38 20 65 3b  gno n;.    u8 e;
17c6f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  .    MemPage *pP
17c70 61 67 65 20 3d 20 61 70 50 61 67 65 5b 69 5d 3b  age = apPage[i];
17c71 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a 70  .    BtShared *p
17c72 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b  Bt = pPage->pBt;
17c73 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
17c74 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 0a 20  ge->isInit );.. 
17c75 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 50     for(j=0; j<pP
17c76 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 6a 2b 2b 29  age->nCell; j++)
17c77 7b 0a 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f  {.      CellInfo
17c78 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 75 38 20   info;.      u8 
17c79 2a 7a 3b 0a 20 20 20 20 20 0a 20 20 20 20 20 20  *z;.     .      
17c7a 7a 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61  z = findCell(pPa
17c7b 67 65 2c 20 6a 29 3b 0a 20 20 20 20 20 20 62 74  ge, j);.      bt
17c7c 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
17c7d 70 50 61 67 65 2c 20 7a 2c 20 26 69 6e 66 6f 29  pPage, z, &info)
17c7e 3b 0a 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f  ;.      if( info
17c7f 2e 69 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20  .iOverflow ){.  
17c80 20 20 20 20 20 20 50 67 6e 6f 20 6f 76 66 6c 20        Pgno ovfl 
17c81 3d 20 67 65 74 34 62 79 74 65 28 26 7a 5b 69 6e  = get4byte(&z[in
17c82 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a  fo.iOverflow]);.
17c83 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 47 65          ptrmapGe
17c84 74 28 70 42 74 2c 20 6f 76 66 6c 2c 20 26 65 2c  t(pBt, ovfl, &e,
17c85 20 26 6e 29 3b 0a 20 20 20 20 20 20 20 20 61 73   &n);.        as
17c86 73 65 72 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e  sert( n==pPage->
17c87 70 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41  pgno && e==PTRMA
17c88 50 5f 4f 56 45 52 46 4c 4f 57 31 20 29 3b 0a 20  P_OVERFLOW1 );. 
17c89 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
17c8a 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
17c8b 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 63 68  .        Pgno ch
17c8c 69 6c 64 20 3d 20 67 65 74 34 62 79 74 65 28 7a  ild = get4byte(z
17c8d 29 3b 0a 20 20 20 20 20 20 20 20 70 74 72 6d 61  );.        ptrma
17c8e 70 47 65 74 28 70 42 74 2c 20 63 68 69 6c 64 2c  pGet(pBt, child,
17c8f 20 26 65 2c 20 26 6e 29 3b 0a 20 20 20 20 20 20   &e, &n);.      
17c90 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 70 50 61    assert( n==pPa
17c91 67 65 2d 3e 70 67 6e 6f 20 26 26 20 65 3d 3d 50  ge->pgno && e==P
17c92 54 52 4d 41 50 5f 42 54 52 45 45 20 29 3b 0a 20  TRMAP_BTREE );. 
17c93 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
17c94 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
17c95 66 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20  f ){.      Pgno 
17c96 63 68 69 6c 64 20 3d 20 67 65 74 34 62 79 74 65  child = get4byte
17c97 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70  (&pPage->aData[p
17c98 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b  Page->hdrOffset+
17c99 38 5d 29 3b 0a 20 20 20 20 20 20 70 74 72 6d 61  8]);.      ptrma
17c9a 70 47 65 74 28 70 42 74 2c 20 63 68 69 6c 64 2c  pGet(pBt, child,
17c9b 20 26 65 2c 20 26 6e 29 3b 0a 20 20 20 20 20 20   &e, &n);.      
17c9c 61 73 73 65 72 74 28 20 6e 3d 3d 70 50 61 67 65  assert( n==pPage
17c9d 2d 3e 70 67 6e 6f 20 26 26 20 65 3d 3d 50 54 52  ->pgno && e==PTR
17c9e 4d 41 50 5f 42 54 52 45 45 20 29 3b 0a 20 20 20  MAP_BTREE );.   
17c9f 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
17ca0 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  1;.}.#endif../*.
17ca1 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
17ca2 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 70 79   is used to copy
17ca3 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
17ca4 20 74 68 65 20 62 2d 74 72 65 65 20 6e 6f 64 65   the b-tree node
17ca5 20 73 74 6f 72 65 64 20 0a 2a 2a 20 6f 6e 20 70   stored .** on p
17ca6 61 67 65 20 70 46 72 6f 6d 20 74 6f 20 70 61 67  age pFrom to pag
17ca7 65 20 70 54 6f 2e 20 49 66 20 70 61 67 65 20 70  e pTo. If page p
17ca8 46 72 6f 6d 20 77 61 73 20 6e 6f 74 20 61 20 6c  From was not a l
17ca9 65 61 66 20 70 61 67 65 2c 20 74 68 65 6e 0a 2a  eaf page, then.*
17caa 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  * the pointer-ma
17cab 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20 65 61  p entries for ea
17cac 63 68 20 63 68 69 6c 64 20 70 61 67 65 20 61 72  ch child page ar
17cad 65 20 75 70 64 61 74 65 64 20 73 6f 20 74 68 61  e updated so tha
17cae 74 20 74 68 65 0a 2a 2a 20 70 61 72 65 6e 74 20  t the.** parent 
17caf 70 61 67 65 20 73 74 6f 72 65 64 20 69 6e 20 74  page stored in t
17cb0 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69  he pointer map i
17cb1 73 20 70 61 67 65 20 70 54 6f 2e 20 49 66 20 70  s page pTo. If p
17cb2 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 65 64 0a 2a  From contained.*
17cb3 2a 20 61 6e 79 20 63 65 6c 6c 73 20 77 69 74 68  * any cells with
17cb4 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70   overflow page p
17cb5 6f 69 6e 74 65 72 73 2c 20 74 68 65 6e 20 74 68  ointers, then th
17cb6 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
17cb7 70 6f 69 6e 74 65 72 0a 2a 2a 20 6d 61 70 20 65  pointer.** map e
17cb8 6e 74 72 69 65 73 20 61 72 65 20 61 6c 73 6f 20  ntries are also 
17cb9 75 70 64 61 74 65 64 20 73 6f 20 74 68 61 74 20  updated so that 
17cba 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20  the parent page 
17cbb 69 73 20 70 61 67 65 20 70 54 6f 2e 0a 2a 2a 0a  is page pTo..**.
17cbc 2a 2a 20 49 66 20 70 46 72 6f 6d 20 69 73 20 63  ** If pFrom is c
17cbd 75 72 72 65 6e 74 6c 79 20 63 61 72 72 79 69 6e  urrently carryin
17cbe 67 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 63  g any overflow c
17cbf 65 6c 6c 73 20 28 65 6e 74 72 69 65 73 20 69 6e  ells (entries in
17cc0 20 74 68 65 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e   the.** MemPage.
17cc1 61 4f 76 66 6c 5b 5d 20 61 72 72 61 79 29 2c 20  aOvfl[] array), 
17cc2 74 68 65 79 20 61 72 65 20 6e 6f 74 20 63 6f 70  they are not cop
17cc3 69 65 64 20 74 6f 20 70 54 6f 2e 20 0a 2a 2a 0a  ied to pTo. .**.
17cc4 2a 2a 20 42 65 66 6f 72 65 20 72 65 74 75 72 6e  ** Before return
17cc5 69 6e 67 2c 20 70 61 67 65 20 70 54 6f 20 69 73  ing, page pTo is
17cc6 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 64 20 75   reinitialized u
17cc7 73 69 6e 67 20 62 74 72 65 65 49 6e 69 74 50 61  sing btreeInitPa
17cc8 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ge()..**.** The 
17cc9 70 65 72 66 6f 72 6d 61 6e 63 65 20 6f 66 20 74  performance of t
17cca 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
17ccb 6e 6f 74 20 63 72 69 74 69 63 61 6c 2e 20 49 74  not critical. It
17ccc 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79   is only used by
17ccd 20 0a 2a 2a 20 74 68 65 20 62 61 6c 61 6e 63 65   .** the balance
17cce 5f 73 68 61 6c 6c 6f 77 65 72 28 29 20 61 6e 64  _shallower() and
17ccf 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28   balance_deeper(
17cd0 29 20 70 72 6f 63 65 64 75 72 65 73 2c 20 6e 65  ) procedures, ne
17cd1 69 74 68 65 72 20 6f 66 0a 2a 2a 20 77 68 69 63  ither of.** whic
17cd2 68 20 61 72 65 20 63 61 6c 6c 65 64 20 6f 66 74  h are called oft
17cd3 65 6e 20 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20  en under normal 
17cd4 63 69 72 63 75 6d 73 74 61 6e 63 65 73 2e 0a 2a  circumstances..*
17cd5 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  /.static void co
17cd6 70 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 4d 65  pyNodeContent(Me
17cd7 6d 50 61 67 65 20 2a 70 46 72 6f 6d 2c 20 4d 65  mPage *pFrom, Me
17cd8 6d 50 61 67 65 20 2a 70 54 6f 2c 20 69 6e 74 20  mPage *pTo, int 
17cd9 2a 70 52 43 29 7b 0a 20 20 69 66 28 20 28 2a 70  *pRC){.  if( (*p
17cda 52 43 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  RC)==SQLITE_OK )
17cdb 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a  {.    BtShared *
17cdc 20 63 6f 6e 73 74 20 70 42 74 20 3d 20 70 46 72   const pBt = pFr
17cdd 6f 6d 2d 3e 70 42 74 3b 0a 20 20 20 20 75 38 20  om->pBt;.    u8 
17cde 2a 20 63 6f 6e 73 74 20 61 46 72 6f 6d 20 3d 20  * const aFrom = 
17cdf 70 46 72 6f 6d 2d 3e 61 44 61 74 61 3b 0a 20 20  pFrom->aData;.  
17ce0 20 20 75 38 20 2a 20 63 6f 6e 73 74 20 61 54 6f    u8 * const aTo
17ce1 20 3d 20 70 54 6f 2d 3e 61 44 61 74 61 3b 0a 20   = pTo->aData;. 
17ce2 20 20 20 69 6e 74 20 63 6f 6e 73 74 20 69 46 72     int const iFr
17ce3 6f 6d 48 64 72 20 3d 20 70 46 72 6f 6d 2d 3e 68  omHdr = pFrom->h
17ce4 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 20 69 6e  drOffset;.    in
17ce5 74 20 63 6f 6e 73 74 20 69 54 6f 48 64 72 20 3d  t const iToHdr =
17ce6 20 28 28 70 54 6f 2d 3e 70 67 6e 6f 3d 3d 31 29   ((pTo->pgno==1)
17ce7 20 3f 20 31 30 30 20 3a 20 30 29 3b 0a 20 20 20   ? 100 : 0);.   
17ce8 20 54 45 53 54 4f 4e 4c 59 28 69 6e 74 20 72 63   TESTONLY(int rc
17ce9 3b 29 0a 20 20 20 20 69 6e 74 20 69 44 61 74 61  ;).    int iData
17cea 3b 0a 20 20 0a 20 20 0a 20 20 20 20 61 73 73 65  ;.  .  .    asse
17ceb 72 74 28 20 70 46 72 6f 6d 2d 3e 69 73 49 6e 69  rt( pFrom->isIni
17cec 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  t );.    assert(
17ced 20 70 46 72 6f 6d 2d 3e 6e 46 72 65 65 3e 3d 69   pFrom->nFree>=i
17cee 54 6f 48 64 72 20 29 3b 0a 20 20 20 20 61 73 73  ToHdr );.    ass
17cef 65 72 74 28 20 67 65 74 32 62 79 74 65 28 26 61  ert( get2byte(&a
17cf0 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 5d  From[iFromHdr+5]
17cf1 29 3c 3d 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  )<=pBt->usableSi
17cf2 7a 65 20 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20  ze );.  .    /* 
17cf3 43 6f 70 79 20 74 68 65 20 62 2d 74 72 65 65 20  Copy the b-tree 
17cf4 6e 6f 64 65 20 63 6f 6e 74 65 6e 74 20 66 72 6f  node content fro
17cf5 6d 20 70 61 67 65 20 70 46 72 6f 6d 20 74 6f 20  m page pFrom to 
17cf6 70 61 67 65 20 70 54 6f 2e 20 2a 2f 0a 20 20 20  page pTo. */.   
17cf7 20 69 44 61 74 61 20 3d 20 67 65 74 32 62 79 74   iData = get2byt
17cf8 65 28 26 61 46 72 6f 6d 5b 69 46 72 6f 6d 48 64  e(&aFrom[iFromHd
17cf9 72 2b 35 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70  r+5]);.    memcp
17cfa 79 28 26 61 54 6f 5b 69 44 61 74 61 5d 2c 20 26  y(&aTo[iData], &
17cfb 61 46 72 6f 6d 5b 69 44 61 74 61 5d 2c 20 70 42  aFrom[iData], pB
17cfc 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 69 44  t->usableSize-iD
17cfd 61 74 61 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  ata);.    memcpy
17cfe 28 26 61 54 6f 5b 69 54 6f 48 64 72 5d 2c 20 26  (&aTo[iToHdr], &
17cff 61 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 5d 2c  aFrom[iFromHdr],
17d00 20 70 46 72 6f 6d 2d 3e 63 65 6c 6c 4f 66 66 73   pFrom->cellOffs
17d01 65 74 20 2b 20 32 2a 70 46 72 6f 6d 2d 3e 6e 43  et + 2*pFrom->nC
17d02 65 6c 6c 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20  ell);.  .    /* 
17d03 52 65 69 6e 69 74 69 61 6c 69 7a 65 20 70 61 67  Reinitialize pag
17d04 65 20 70 54 6f 20 73 6f 20 74 68 61 74 20 74 68  e pTo so that th
17d05 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
17d06 65 20 4d 65 6d 50 61 67 65 20 73 74 72 75 63 74  e MemPage struct
17d07 75 72 65 0a 20 20 20 20 2a 2a 20 6d 61 74 63 68  ure.    ** match
17d08 20 74 68 65 20 6e 65 77 20 64 61 74 61 2e 20 54   the new data. T
17d09 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  he initializatio
17d0a 6e 20 6f 66 20 70 54 6f 20 22 63 61 6e 6e 6f 74  n of pTo "cannot
17d0b 22 20 66 61 69 6c 2c 20 61 73 20 74 68 65 0a 20  " fail, as the. 
17d0c 20 20 20 2a 2a 20 64 61 74 61 20 63 6f 70 69 65     ** data copie
17d0d 64 20 66 72 6f 6d 20 70 46 72 6f 6d 20 69 73 20  d from pFrom is 
17d0e 6b 6e 6f 77 6e 20 74 6f 20 62 65 20 76 61 6c 69  known to be vali
17d0f 64 2e 20 20 2a 2f 0a 20 20 20 20 70 54 6f 2d 3e  d.  */.    pTo->
17d10 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20  isInit = 0;.    
17d11 54 45 53 54 4f 4e 4c 59 28 72 63 20 3d 20 29 20  TESTONLY(rc = ) 
17d12 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 54  btreeInitPage(pT
17d13 6f 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  o);.    assert( 
17d14 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
17d15 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  .  .    /* If th
17d16 69 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61  is is an auto-va
17d17 63 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20 75  cuum database, u
17d18 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65  pdate the pointe
17d19 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 0a 20 20  r-map entries.  
17d1a 20 20 2a 2a 20 66 6f 72 20 61 6e 79 20 62 2d 74    ** for any b-t
17d1b 72 65 65 20 6f 72 20 6f 76 65 72 66 6c 6f 77 20  ree or overflow 
17d1c 70 61 67 65 73 20 74 68 61 74 20 70 54 6f 20 6e  pages that pTo n
17d1d 6f 77 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  ow contains the 
17d1e 70 6f 69 6e 74 65 72 73 20 74 6f 2e 0a 20 20 20  pointers to..   
17d1f 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 53 41 55   */.    if( ISAU
17d20 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20  TOVACUUM ){.    
17d21 20 20 2a 70 52 43 20 3d 20 73 65 74 43 68 69 6c    *pRC = setChil
17d22 64 50 74 72 6d 61 70 73 28 70 54 6f 29 3b 0a 20  dPtrmaps(pTo);. 
17d23 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
17d24 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
17d25 65 64 69 73 74 72 69 62 75 74 65 73 20 63 65 6c  edistributes cel
17d26 6c 73 20 6f 6e 20 74 68 65 20 69 50 61 72 65 6e  ls on the iParen
17d27 74 49 64 78 27 74 68 20 63 68 69 6c 64 20 6f 66  tIdx'th child of
17d28 20 70 50 61 72 65 6e 74 0a 2a 2a 20 28 68 65 72   pParent.** (her
17d29 65 61 66 74 65 72 20 22 74 68 65 20 70 61 67 65  eafter "the page
17d2a 22 29 20 61 6e 64 20 75 70 20 74 6f 20 32 20 73  ") and up to 2 s
17d2b 69 62 6c 69 6e 67 73 20 73 6f 20 74 68 61 74 20  iblings so that 
17d2c 61 6c 6c 20 70 61 67 65 73 20 68 61 76 65 20 61  all pages have a
17d2d 62 6f 75 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65  bout the.** same
17d2e 20 61 6d 6f 75 6e 74 20 6f 66 20 66 72 65 65 20   amount of free 
17d2f 73 70 61 63 65 2e 20 55 73 75 61 6c 6c 79 20 61  space. Usually a
17d30 20 73 69 6e 67 6c 65 20 73 69 62 6c 69 6e 67 20   single sibling 
17d31 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65 20 6f  on either side o
17d32 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 61 72  f the.** page ar
17d33 65 20 75 73 65 64 20 69 6e 20 74 68 65 20 62 61  e used in the ba
17d34 6c 61 6e 63 69 6e 67 2c 20 74 68 6f 75 67 68 20  lancing, though 
17d35 62 6f 74 68 20 73 69 62 6c 69 6e 67 73 20 6d 69  both siblings mi
17d36 67 68 74 20 63 6f 6d 65 20 66 72 6f 6d 20 6f 6e  ght come from on
17d37 65 0a 2a 2a 20 73 69 64 65 20 69 66 20 74 68 65  e.** side if the
17d38 20 70 61 67 65 20 69 73 20 74 68 65 20 66 69 72   page is the fir
17d39 73 74 20 6f 72 20 6c 61 73 74 20 63 68 69 6c 64  st or last child
17d3a 20 6f 66 20 69 74 73 20 70 61 72 65 6e 74 2e 20   of its parent. 
17d3b 49 66 20 74 68 65 20 70 61 67 65 20 0a 2a 2a 20  If the page .** 
17d3c 68 61 73 20 66 65 77 65 72 20 74 68 61 6e 20 32  has fewer than 2
17d3d 20 73 69 62 6c 69 6e 67 73 20 28 73 6f 6d 65 74   siblings (somet
17d3e 68 69 6e 67 20 77 68 69 63 68 20 63 61 6e 20 6f  hing which can o
17d3f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 20 74 68  nly happen if th
17d40 65 20 70 61 67 65 0a 2a 2a 20 69 73 20 61 20 72  e page.** is a r
17d41 6f 6f 74 20 70 61 67 65 20 6f 72 20 61 20 63 68  oot page or a ch
17d42 69 6c 64 20 6f 66 20 61 20 72 6f 6f 74 20 70 61  ild of a root pa
17d43 67 65 29 20 74 68 65 6e 20 61 6c 6c 20 61 76 61  ge) then all ava
17d44 69 6c 61 62 6c 65 20 73 69 62 6c 69 6e 67 73 0a  ilable siblings.
17d45 2a 2a 20 70 61 72 74 69 63 69 70 61 74 65 20 69  ** participate i
17d46 6e 20 74 68 65 20 62 61 6c 61 6e 63 69 6e 67 2e  n the balancing.
17d47 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65  .**.** The numbe
17d48 72 20 6f 66 20 73 69 62 6c 69 6e 67 73 20 6f 66  r of siblings of
17d49 20 74 68 65 20 70 61 67 65 20 6d 69 67 68 74 20   the page might 
17d4a 62 65 20 69 6e 63 72 65 61 73 65 64 20 6f 72 20  be increased or 
17d4b 64 65 63 72 65 61 73 65 64 20 62 79 20 0a 2a 2a  decreased by .**
17d4c 20 6f 6e 65 20 6f 72 20 74 77 6f 20 69 6e 20 61   one or two in a
17d4d 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 65 70  n effort to keep
17d4e 20 70 61 67 65 73 20 6e 65 61 72 6c 79 20 66 75   pages nearly fu
17d4f 6c 6c 20 62 75 74 20 6e 6f 74 20 6f 76 65 72 20  ll but not over 
17d50 66 75 6c 6c 2e 20 0a 2a 2a 0a 2a 2a 20 4e 6f 74  full. .**.** Not
17d51 65 20 74 68 61 74 20 77 68 65 6e 20 74 68 69 73  e that when this
17d52 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
17d53 65 64 2c 20 73 6f 6d 65 20 6f 66 20 74 68 65 20  ed, some of the 
17d54 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 70 61 67  cells on the pag
17d55 65 0a 2a 2a 20 6d 69 67 68 74 20 6e 6f 74 20 61  e.** might not a
17d56 63 74 75 61 6c 6c 79 20 62 65 20 73 74 6f 72 65  ctually be store
17d57 64 20 69 6e 20 4d 65 6d 50 61 67 65 2e 61 44 61  d in MemPage.aDa
17d58 74 61 5b 5d 2e 20 54 68 69 73 20 63 61 6e 20 68  ta[]. This can h
17d59 61 70 70 65 6e 0a 2a 2a 20 69 66 20 74 68 65 20  appen.** if the 
17d5a 70 61 67 65 20 69 73 20 6f 76 65 72 66 75 6c 6c  page is overfull
17d5b 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65  . This routine e
17d5c 6e 73 75 72 65 73 20 74 68 61 74 20 61 6c 6c 20  nsures that all 
17d5d 63 65 6c 6c 73 20 61 6c 6c 6f 63 61 74 65 64 0a  cells allocated.
17d5e 2a 2a 20 74 6f 20 74 68 65 20 70 61 67 65 20 61  ** to the page a
17d5f 6e 64 20 69 74 73 20 73 69 62 6c 69 6e 67 73 20  nd its siblings 
17d60 66 69 74 20 69 6e 74 6f 20 4d 65 6d 50 61 67 65  fit into MemPage
17d61 2e 61 44 61 74 61 5b 5d 20 62 65 66 6f 72 65 20  .aData[] before 
17d62 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a  returning..**.**
17d63 20 49 6e 20 74 68 65 20 63 6f 75 72 73 65 20 6f   In the course o
17d64 66 20 62 61 6c 61 6e 63 69 6e 67 20 74 68 65 20  f balancing the 
17d65 70 61 67 65 20 61 6e 64 20 69 74 73 20 73 69 62  page and its sib
17d66 6c 69 6e 67 73 2c 20 63 65 6c 6c 73 20 6d 61 79  lings, cells may
17d67 20 62 65 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20   be.** inserted 
17d68 69 6e 74 6f 20 6f 72 20 72 65 6d 6f 76 65 64 20  into or removed 
17d69 66 72 6f 6d 20 74 68 65 20 70 61 72 65 6e 74 20  from the parent 
17d6a 70 61 67 65 20 28 70 50 61 72 65 6e 74 29 2e 20  page (pParent). 
17d6b 44 6f 69 6e 67 20 73 6f 0a 2a 2a 20 6d 61 79 20  Doing so.** may 
17d6c 63 61 75 73 65 20 74 68 65 20 70 61 72 65 6e 74  cause the parent
17d6d 20 70 61 67 65 20 74 6f 20 62 65 63 6f 6d 65 20   page to become 
17d6e 6f 76 65 72 66 75 6c 6c 20 6f 72 20 75 6e 64 65  overfull or unde
17d6f 72 66 75 6c 6c 2e 20 49 66 20 74 68 69 73 0a 2a  rfull. If this.*
17d70 2a 20 68 61 70 70 65 6e 73 2c 20 69 74 20 69 73  * happens, it is
17d71 20 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c   the responsibil
17d72 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65  ity of the calle
17d73 72 20 74 6f 20 69 6e 76 6f 6b 65 20 74 68 65 20  r to invoke the 
17d74 63 6f 72 72 65 63 74 0a 2a 2a 20 62 61 6c 61 6e  correct.** balan
17d75 63 69 6e 67 20 72 6f 75 74 69 6e 65 20 74 6f 20  cing routine to 
17d76 66 69 78 20 74 68 69 73 20 70 72 6f 62 6c 65 6d  fix this problem
17d77 20 28 73 65 65 20 74 68 65 20 62 61 6c 61 6e 63   (see the balanc
17d78 65 28 29 20 72 6f 75 74 69 6e 65 29 2e 20 0a 2a  e() routine). .*
17d79 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 72 6f 75  *.** If this rou
17d7a 74 69 6e 65 20 66 61 69 6c 73 20 66 6f 72 20 61  tine fails for a
17d7b 6e 79 20 72 65 61 73 6f 6e 2c 20 69 74 20 6d 69  ny reason, it mi
17d7c 67 68 74 20 6c 65 61 76 65 20 74 68 65 20 64 61  ght leave the da
17d7d 74 61 62 61 73 65 0a 2a 2a 20 69 6e 20 61 20 63  tabase.** in a c
17d7e 6f 72 72 75 70 74 65 64 20 73 74 61 74 65 2e 20  orrupted state. 
17d7f 53 6f 20 69 66 20 74 68 69 73 20 72 6f 75 74 69  So if this routi
17d80 6e 65 20 66 61 69 6c 73 2c 20 74 68 65 20 64 61  ne fails, the da
17d81 74 61 62 61 73 65 20 73 68 6f 75 6c 64 0a 2a 2a  tabase should.**
17d82 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e   be rolled back.
17d83 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64  .**.** The third
17d84 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69   argument to thi
17d85 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 4f 76 66  s function, aOvf
17d86 6c 53 70 61 63 65 2c 20 69 73 20 61 20 70 6f 69  lSpace, is a poi
17d87 6e 74 65 72 20 74 6f 20 61 0a 2a 2a 20 62 75 66  nter to a.** buf
17d88 66 65 72 20 62 69 67 20 65 6e 6f 75 67 68 20 74  fer big enough t
17d89 6f 20 68 6f 6c 64 20 6f 6e 65 20 70 61 67 65 2e  o hold one page.
17d8a 20 49 66 20 77 68 69 6c 65 20 69 6e 73 65 72 74   If while insert
17d8b 69 6e 67 20 63 65 6c 6c 73 20 69 6e 74 6f 20 74  ing cells into t
17d8c 68 65 20 70 61 72 65 6e 74 0a 2a 2a 20 70 61 67  he parent.** pag
17d8d 65 20 28 70 50 61 72 65 6e 74 29 20 74 68 65 20  e (pParent) the 
17d8e 70 61 72 65 6e 74 20 70 61 67 65 20 62 65 63 6f  parent page beco
17d8f 6d 65 73 20 6f 76 65 72 66 75 6c 6c 2c 20 74 68  mes overfull, th
17d90 69 73 20 62 75 66 66 65 72 20 69 73 0a 2a 2a 20  is buffer is.** 
17d91 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68  used to store th
17d92 65 20 70 61 72 65 6e 74 27 73 20 6f 76 65 72 66  e parent's overf
17d93 6c 6f 77 20 63 65 6c 6c 73 2e 20 42 65 63 61 75  low cells. Becau
17d94 73 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  se this function
17d95 20 69 6e 73 65 72 74 73 0a 2a 2a 20 61 20 6d 61   inserts.** a ma
17d96 78 69 6d 75 6d 20 6f 66 20 66 6f 75 72 20 64 69  ximum of four di
17d97 76 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 74 6f  vider cells into
17d98 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
17d99 2c 20 61 6e 64 20 74 68 65 20 6d 61 78 69 6d 75  , and the maximu
17d9a 6d 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 61 20 63  m.** size of a c
17d9b 65 6c 6c 20 73 74 6f 72 65 64 20 77 69 74 68 69  ell stored withi
17d9c 6e 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f  n an internal no
17d9d 64 65 20 69 73 20 61 6c 77 61 79 73 20 6c 65 73  de is always les
17d9e 73 20 74 68 61 6e 20 31 2f 34 0a 2a 2a 20 6f 66  s than 1/4.** of
17d9f 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 2c 20   the page-size, 
17da0 74 68 65 20 61 4f 76 66 6c 53 70 61 63 65 5b 5d  the aOvflSpace[]
17da1 20 62 75 66 66 65 72 20 69 73 20 67 75 61 72 61   buffer is guara
17da2 6e 74 65 65 64 20 74 6f 20 62 65 20 6c 61 72 67  nteed to be larg
17da3 65 0a 2a 2a 20 65 6e 6f 75 67 68 20 66 6f 72 20  e.** enough for 
17da4 61 6c 6c 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c  all overflow cel
17da5 6c 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 4f 76  ls..**.** If aOv
17da6 66 6c 53 70 61 63 65 20 69 73 20 73 65 74 20 74  flSpace is set t
17da7 6f 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72  o a null pointer
17da8 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
17da9 72 65 74 75 72 6e 73 20 0a 2a 2a 20 53 51 4c 49  returns .** SQLI
17daa 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 73 74 61  TE_NOMEM..*/.sta
17dab 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f  tic int balance_
17dac 6e 6f 6e 72 6f 6f 74 28 0a 20 20 4d 65 6d 50 61  nonroot(.  MemPa
17dad 67 65 20 2a 70 50 61 72 65 6e 74 2c 20 20 20 20  ge *pParent,    
17dae 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
17daf 72 65 6e 74 20 70 61 67 65 20 6f 66 20 73 69 62  rent page of sib
17db0 6c 69 6e 67 73 20 62 65 69 6e 67 20 62 61 6c 61  lings being bala
17db1 6e 63 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 50  nced */.  int iP
17db2 61 72 65 6e 74 49 64 78 2c 20 20 20 20 20 20 20  arentIdx,       
17db3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
17db4 65 78 20 6f 66 20 22 74 68 65 20 70 61 67 65 22  ex of "the page"
17db5 20 69 6e 20 70 50 61 72 65 6e 74 20 2a 2f 0a 20   in pParent */. 
17db6 20 75 38 20 2a 61 4f 76 66 6c 53 70 61 63 65 2c   u8 *aOvflSpace,
17db7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17db8 20 2f 2a 20 70 61 67 65 2d 73 69 7a 65 20 62 79   /* page-size by
17db9 74 65 73 20 6f 66 20 73 70 61 63 65 20 66 6f 72  tes of space for
17dba 20 70 61 72 65 6e 74 20 6f 76 66 6c 20 2a 2f 0a   parent ovfl */.
17dbb 20 20 69 6e 74 20 69 73 52 6f 6f 74 20 20 20 20    int isRoot    
17dbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17dbd 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 50 61    /* True if pPa
17dbe 72 65 6e 74 20 69 73 20 61 20 72 6f 6f 74 2d 70  rent is a root-p
17dbf 61 67 65 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68  age */.){.  BtSh
17dc0 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20  ared *pBt;      
17dc1 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
17dc2 77 68 6f 6c 65 20 64 61 74 61 62 61 73 65 20 2a  whole database *
17dc3 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20  /.  int nCell = 
17dc4 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
17dc5 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65   /* Number of ce
17dc6 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20  lls in apCell[] 
17dc7 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 78 43 65 6c  */.  int nMaxCel
17dc8 6c 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ls = 0;         
17dc9 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73    /* Allocated s
17dca 69 7a 65 20 6f 66 20 61 70 43 65 6c 6c 2c 20 73  ize of apCell, s
17dcb 7a 43 65 6c 6c 2c 20 61 46 72 6f 6d 2e 20 2a 2f  zCell, aFrom. */
17dcc 0a 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20 30 3b  .  int nNew = 0;
17dcd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17dce 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
17dcf 65 73 20 69 6e 20 61 70 4e 65 77 5b 5d 20 2a 2f  es in apNew[] */
17dd0 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b 20 20 20 20  .  int nOld;    
17dd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17dd2 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
17dd3 65 73 20 69 6e 20 61 70 4f 6c 64 5b 5d 20 2a 2f  es in apOld[] */
17dd4 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6b 3b 20  .  int i, j, k; 
17dd5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17dd6 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73  /* Loop counters
17dd7 20 2a 2f 0a 20 20 69 6e 74 20 6e 78 44 69 76 3b   */.  int nxDiv;
17dd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17dd9 20 20 20 2f 2a 20 4e 65 78 74 20 64 69 76 69 64     /* Next divid
17dda 65 72 20 73 6c 6f 74 20 69 6e 20 70 50 61 72 65  er slot in pPare
17ddb 6e 74 2d 3e 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20  nt->aCell[] */. 
17ddc 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
17ddd 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  _OK;          /*
17dde 20 54 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65   The return code
17ddf 20 2a 2f 0a 20 20 75 31 36 20 6c 65 61 66 43 6f   */.  u16 leafCo
17de0 72 72 65 63 74 69 6f 6e 3b 20 20 20 20 20 20 20  rrection;       
17de1 20 20 20 2f 2a 20 34 20 69 66 20 70 50 61 67 65     /* 4 if pPage
17de2 20 69 73 20 61 20 6c 65 61 66 2e 20 20 30 20 69   is a leaf.  0 i
17de3 66 20 6e 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 6c  f not */.  int l
17de4 65 61 66 44 61 74 61 3b 20 20 20 20 20 20 20 20  eafData;        
17de5 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
17de6 69 66 20 70 50 61 67 65 20 69 73 20 61 20 6c 65  if pPage is a le
17de7 61 66 20 6f 66 20 61 20 4c 45 41 46 44 41 54 41  af of a LEAFDATA
17de8 20 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 75   tree */.  int u
17de9 73 61 62 6c 65 53 70 61 63 65 3b 20 20 20 20 20  sableSpace;     
17dea 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73          /* Bytes
17deb 20 69 6e 20 70 50 61 67 65 20 62 65 79 6f 6e 64   in pPage beyond
17dec 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20   the header */. 
17ded 20 69 6e 74 20 70 61 67 65 46 6c 61 67 73 3b 20   int pageFlags; 
17dee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17def 20 56 61 6c 75 65 20 6f 66 20 70 50 61 67 65 2d   Value of pPage-
17df0 3e 61 44 61 74 61 5b 30 5d 20 2a 2f 0a 20 20 69  >aData[0] */.  i
17df1 6e 74 20 73 75 62 74 6f 74 61 6c 3b 20 20 20 20  nt subtotal;    
17df2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
17df3 75 62 74 6f 74 61 6c 20 6f 66 20 62 79 74 65 73  ubtotal of bytes
17df4 20 69 6e 20 63 65 6c 6c 73 20 6f 6e 20 6f 6e 65   in cells on one
17df5 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69   page */.  int i
17df6 53 70 61 63 65 31 20 3d 20 30 3b 20 20 20 20 20  Space1 = 0;     
17df7 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
17df8 20 75 6e 75 73 65 64 20 62 79 74 65 20 6f 66 20   unused byte of 
17df9 61 53 70 61 63 65 31 5b 5d 20 2a 2f 0a 20 20 69  aSpace1[] */.  i
17dfa 6e 74 20 69 4f 76 66 6c 53 70 61 63 65 20 3d 20  nt iOvflSpace = 
17dfb 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  0;          /* F
17dfc 69 72 73 74 20 75 6e 75 73 65 64 20 62 79 74 65  irst unused byte
17dfd 20 6f 66 20 61 4f 76 66 6c 53 70 61 63 65 5b 5d   of aOvflSpace[]
17dfe 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72 61   */.  int szScra
17dff 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  tch;            
17e00 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 63     /* Size of sc
17e01 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 72 65 71  ratch memory req
17e02 75 65 73 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 50  uested */.  MemP
17e03 61 67 65 20 2a 61 70 4f 6c 64 5b 4e 42 5d 3b 20  age *apOld[NB]; 
17e04 20 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67           /* pPag
17e05 65 20 61 6e 64 20 75 70 20 74 6f 20 74 77 6f 20  e and up to two 
17e06 73 69 62 6c 69 6e 67 73 20 2a 2f 0a 20 20 4d 65  siblings */.  Me
17e07 6d 50 61 67 65 20 2a 61 70 43 6f 70 79 5b 4e 42  mPage *apCopy[NB
17e08 5d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72  ];         /* Pr
17e09 69 76 61 74 65 20 63 6f 70 69 65 73 20 6f 66 20  ivate copies of 
17e0a 61 70 4f 6c 64 5b 5d 20 70 61 67 65 73 20 2a 2f  apOld[] pages */
17e0b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 4e 65  .  MemPage *apNe
17e0c 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20  w[NB+2];        
17e0d 2f 2a 20 70 50 61 67 65 20 61 6e 64 20 75 70 20  /* pPage and up 
17e0e 74 6f 20 4e 42 20 73 69 62 6c 69 6e 67 73 20 61  to NB siblings a
17e0f 66 74 65 72 20 62 61 6c 61 6e 63 69 6e 67 20 2a  fter balancing *
17e10 2f 0a 20 20 75 38 20 2a 70 52 69 67 68 74 3b 20  /.  u8 *pRight; 
17e11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17e12 20 2f 2a 20 4c 6f 63 61 74 69 6f 6e 20 69 6e 20   /* Location in 
17e13 70 61 72 65 6e 74 20 6f 66 20 72 69 67 68 74 2d  parent of right-
17e14 73 69 62 6c 69 6e 67 20 70 6f 69 6e 74 65 72 20  sibling pointer 
17e15 2a 2f 0a 20 20 75 38 20 2a 61 70 44 69 76 5b 4e  */.  u8 *apDiv[N
17e16 42 2d 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20  B-1];           
17e17 20 20 2f 2a 20 44 69 76 69 64 65 72 20 63 65 6c    /* Divider cel
17e18 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20 2a 2f  ls in pParent */
17e19 0a 20 20 69 6e 74 20 63 6e 74 4e 65 77 5b 4e 42  .  int cntNew[NB
17e1a 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20  +2];            
17e1b 2f 2a 20 49 6e 64 65 78 20 69 6e 20 61 43 65 6c  /* Index in aCel
17e1c 6c 5b 5d 20 6f 66 20 63 65 6c 6c 20 61 66 74 65  l[] of cell afte
17e1d 72 20 69 2d 74 68 20 70 61 67 65 20 2a 2f 0a 20  r i-th page */. 
17e1e 20 69 6e 74 20 73 7a 4e 65 77 5b 4e 42 2b 32 5d   int szNew[NB+2]
17e1f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
17e20 20 43 6f 6d 62 69 6e 65 64 20 73 69 7a 65 20 6f   Combined size o
17e21 66 20 63 65 6c 6c 73 20 70 6c 61 63 65 20 6f 6e  f cells place on
17e22 20 69 2d 74 68 20 70 61 67 65 20 2a 2f 0a 20 20   i-th page */.  
17e23 75 38 20 2a 2a 61 70 43 65 6c 6c 20 3d 20 30 3b  u8 **apCell = 0;
17e24 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17e25 41 6c 6c 20 63 65 6c 6c 73 20 62 65 67 69 6e 20  All cells begin 
17e26 62 61 6c 61 6e 63 65 64 20 2a 2f 0a 20 20 75 31  balanced */.  u1
17e27 36 20 2a 73 7a 43 65 6c 6c 3b 20 20 20 20 20 20  6 *szCell;      
17e28 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
17e29 63 61 6c 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20  cal size of all 
17e2a 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b  cells in apCell[
17e2b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53 70 61 63  ] */.  u8 *aSpac
17e2c 65 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  e1;             
17e2d 20 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72      /* Space for
17e2e 20 63 6f 70 69 65 73 20 6f 66 20 64 69 76 69 64   copies of divid
17e2f 65 72 73 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 50  ers cells */.  P
17e30 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20  gno pgno;       
17e31 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
17e32 65 6d 70 20 76 61 72 20 74 6f 20 73 74 6f 72 65  emp var to store
17e33 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69   a page number i
17e34 6e 20 2a 2f 0a 0a 20 20 70 42 74 20 3d 20 70 50  n */..  pBt = pP
17e35 61 72 65 6e 74 2d 3e 70 42 74 3b 0a 20 20 61 73  arent->pBt;.  as
17e36 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
17e37 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
17e38 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
17e39 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
17e3a 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e  writeable(pParen
17e3b 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a  t->pDbPage) );..
17e3c 23 69 66 20 30 0a 20 20 54 52 41 43 45 28 28 22  #if 0.  TRACE(("
17e3d 42 41 4c 41 4e 43 45 3a 20 62 65 67 69 6e 20 70  BALANCE: begin p
17e3e 61 67 65 20 25 64 20 63 68 69 6c 64 20 6f 66 20  age %d child of 
17e3f 25 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67  %d\n", pPage->pg
17e40 6e 6f 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e  no, pParent->pgn
17e41 6f 29 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  o));.#endif..  /
17e42 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20  * At this point 
17e43 70 50 61 72 65 6e 74 20 6d 61 79 20 68 61 76 65  pParent may have
17e44 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 76 65   at most one ove
17e45 72 66 6c 6f 77 20 63 65 6c 6c 2e 20 41 6e 64 20  rflow cell. And 
17e46 69 66 0a 20 20 2a 2a 20 74 68 69 73 20 6f 76 65  if.  ** this ove
17e47 72 66 6c 6f 77 20 63 65 6c 6c 20 69 73 20 70 72  rflow cell is pr
17e48 65 73 65 6e 74 2c 20 69 74 20 6d 75 73 74 20 62  esent, it must b
17e49 65 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68 20  e the cell with 
17e4a 0a 20 20 2a 2a 20 69 6e 64 65 78 20 69 50 61 72  .  ** index iPar
17e4b 65 6e 74 49 64 78 2e 20 54 68 69 73 20 73 63 65  entIdx. This sce
17e4c 6e 61 72 69 6f 20 63 6f 6d 65 73 20 61 62 6f 75  nario comes abou
17e4d 74 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63  t when this func
17e4e 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 63 61 6c  tion.  ** is cal
17e4f 6c 65 64 20 28 69 6e 64 69 72 65 63 74 6c 79 29  led (indirectly)
17e50 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 42 74 72   from sqlite3Btr
17e51 65 65 44 65 6c 65 74 65 28 29 2e 0a 20 20 2a 2f  eeDelete()..  */
17e52 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65  .  assert( pPare
17e53 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30  nt->nOverflow==0
17e54 20 7c 7c 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76   || pParent->nOv
17e55 65 72 66 6c 6f 77 3d 3d 31 20 29 3b 0a 20 20 61  erflow==1 );.  a
17e56 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e  ssert( pParent->
17e57 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 7c 7c 20  nOverflow==0 || 
17e58 70 50 61 72 65 6e 74 2d 3e 61 4f 76 66 6c 5b 30  pParent->aOvfl[0
17e59 5d 2e 69 64 78 3d 3d 69 50 61 72 65 6e 74 49 64  ].idx==iParentId
17e5a 78 20 29 3b 0a 0a 20 20 69 66 28 20 21 61 4f 76  x );..  if( !aOv
17e5b 66 6c 53 70 61 63 65 20 29 7b 0a 20 20 20 20 72  flSpace ){.    r
17e5c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
17e5d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69  EM;.  }..  /* Fi
17e5e 6e 64 20 74 68 65 20 73 69 62 6c 69 6e 67 20 70  nd the sibling p
17e5f 61 67 65 73 20 74 6f 20 62 61 6c 61 6e 63 65 2e  ages to balance.
17e60 20 41 6c 73 6f 20 6c 6f 63 61 74 65 20 74 68 65   Also locate the
17e61 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e   cells in pParen
17e62 74 20 0a 20 20 2a 2a 20 74 68 61 74 20 64 69 76  t .  ** that div
17e63 69 64 65 20 74 68 65 20 73 69 62 6c 69 6e 67 73  ide the siblings
17e64 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 69 73 20  . An attempt is 
17e65 6d 61 64 65 20 74 6f 20 66 69 6e 64 20 4e 4e 20  made to find NN 
17e66 73 69 62 6c 69 6e 67 73 20 6f 6e 20 0a 20 20 2a  siblings on .  *
17e67 2a 20 65 69 74 68 65 72 20 73 69 64 65 20 6f 66  * either side of
17e68 20 70 50 61 67 65 2e 20 4d 6f 72 65 20 73 69 62   pPage. More sib
17e69 6c 69 6e 67 73 20 61 72 65 20 74 61 6b 65 6e 20  lings are taken 
17e6a 66 72 6f 6d 20 6f 6e 65 20 73 69 64 65 2c 20 68  from one side, h
17e6b 6f 77 65 76 65 72 2c 20 0a 20 20 2a 2a 20 69 66  owever, .  ** if
17e6c 20 74 68 65 72 65 20 61 72 65 20 66 65 77 65 72   there are fewer
17e6d 20 74 68 61 6e 20 4e 4e 20 73 69 62 6c 69 6e 67   than NN sibling
17e6e 73 20 6f 6e 20 74 68 65 20 6f 74 68 65 72 20 73  s on the other s
17e6f 69 64 65 2e 20 49 66 20 70 50 61 72 65 6e 74 0a  ide. If pParent.
17e70 20 20 2a 2a 20 68 61 73 20 4e 42 20 6f 72 20 66    ** has NB or f
17e71 65 77 65 72 20 63 68 69 6c 64 72 65 6e 20 74 68  ewer children th
17e72 65 6e 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20  en all children 
17e73 6f 66 20 70 50 61 72 65 6e 74 20 61 72 65 20 74  of pParent are t
17e74 61 6b 65 6e 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a  aken.  .  **.  *
17e75 2a 20 54 68 69 73 20 6c 6f 6f 70 20 61 6c 73 6f  * This loop also
17e76 20 64 72 6f 70 73 20 74 68 65 20 64 69 76 69 64   drops the divid
17e77 65 72 20 63 65 6c 6c 73 20 66 72 6f 6d 20 74 68  er cells from th
17e78 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 54  e parent page. T
17e79 68 69 73 0a 20 20 2a 2a 20 77 61 79 2c 20 74 68  his.  ** way, th
17e7a 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 74  e remainder of t
17e7b 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73  he function does
17e7c 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 64 65 61   not have to dea
17e7d 6c 20 77 69 74 68 20 61 6e 79 0a 20 20 2a 2a 20  l with any.  ** 
17e7e 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 69  overflow cells i
17e7f 6e 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  n the parent pag
17e80 65 2c 20 73 69 6e 63 65 20 69 66 20 61 6e 79 20  e, since if any 
17e81 65 78 69 73 74 65 64 20 74 68 65 79 20 77 69 6c  existed they wil
17e82 6c 0a 20 20 2a 2a 20 68 61 76 65 20 61 6c 72 65  l.  ** have alre
17e83 61 64 79 20 62 65 65 6e 20 72 65 6d 6f 76 65 64  ady been removed
17e84 2e 0a 20 20 2a 2f 0a 20 20 69 20 3d 20 70 50 61  ..  */.  i = pPa
17e85 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20  rent->nOverflow 
17e86 2b 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c  + pParent->nCell
17e87 3b 0a 20 20 69 66 28 20 69 3c 32 20 29 7b 0a 20  ;.  if( i<2 ){. 
17e88 20 20 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20 20     nxDiv = 0;.  
17e89 20 20 6e 4f 6c 64 20 3d 20 69 2b 31 3b 0a 20 20    nOld = i+1;.  
17e8a 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 4f 6c 64 20  }else{.    nOld 
17e8b 3d 20 33 3b 0a 20 20 20 20 69 66 28 20 69 50 61  = 3;.    if( iPa
17e8c 72 65 6e 74 49 64 78 3d 3d 30 20 29 7b 20 20 20  rentIdx==0 ){   
17e8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0a 20                . 
17e8e 20 20 20 20 20 6e 78 44 69 76 20 3d 20 30 3b 0a       nxDiv = 0;.
17e8f 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 50      }else if( iP
17e90 61 72 65 6e 74 49 64 78 3d 3d 69 20 29 7b 0a 20  arentIdx==i ){. 
17e91 20 20 20 20 20 6e 78 44 69 76 20 3d 20 69 2d 32       nxDiv = i-2
17e92 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
17e93 20 20 20 6e 78 44 69 76 20 3d 20 69 50 61 72 65     nxDiv = iPare
17e94 6e 74 49 64 78 2d 31 3b 0a 20 20 20 20 7d 0a 20  ntIdx-1;.    }. 
17e95 20 20 20 69 20 3d 20 32 3b 0a 20 20 7d 0a 20 20     i = 2;.  }.  
17e96 69 66 28 20 28 69 2b 6e 78 44 69 76 2d 70 50 61  if( (i+nxDiv-pPa
17e97 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 29  rent->nOverflow)
17e98 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c  ==pParent->nCell
17e99 20 29 7b 0a 20 20 20 20 70 52 69 67 68 74 20 3d   ){.    pRight =
17e9a 20 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61   &pParent->aData
17e9b 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66  [pParent->hdrOff
17e9c 73 65 74 2b 38 5d 3b 0a 20 20 7d 65 6c 73 65 7b  set+8];.  }else{
17e9d 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20 66 69  .    pRight = fi
17e9e 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20  ndCell(pParent, 
17e9f 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d  i+nxDiv-pParent-
17ea0 3e 6e 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 7d  >nOverflow);.  }
17ea1 0a 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79  .  pgno = get4by
17ea2 74 65 28 70 52 69 67 68 74 29 3b 0a 20 20 77 68  te(pRight);.  wh
17ea3 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 72 63  ile( 1 ){.    rc
17ea4 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67   = getAndInitPag
17ea5 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20 26 61 70  e(pBt, pgno, &ap
17ea6 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28  Old[i]);.    if(
17ea7 20 72 63 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d   rc ){.      mem
17ea8 73 65 74 28 61 70 4f 6c 64 2c 20 30 2c 20 28 69  set(apOld, 0, (i
17ea9 2b 31 29 2a 73 69 7a 65 6f 66 28 4d 65 6d 50 61  +1)*sizeof(MemPa
17eaa 67 65 2a 29 29 3b 0a 20 20 20 20 20 20 67 6f 74  ge*));.      got
17eab 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75  o balance_cleanu
17eac 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 4d 61  p;.    }.    nMa
17ead 78 43 65 6c 6c 73 20 2b 3d 20 31 2b 61 70 4f 6c  xCells += 1+apOl
17eae 64 5b 69 5d 2d 3e 6e 43 65 6c 6c 2b 61 70 4f 6c  d[i]->nCell+apOl
17eaf 64 5b 69 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b  d[i]->nOverflow;
17eb0 0a 20 20 20 20 69 66 28 20 28 69 2d 2d 29 3d 3d  .    if( (i--)==
17eb1 30 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20  0 ) break;..    
17eb2 69 66 28 20 69 2b 6e 78 44 69 76 3d 3d 70 50 61  if( i+nxDiv==pPa
17eb3 72 65 6e 74 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69  rent->aOvfl[0].i
17eb4 64 78 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e  dx && pParent->n
17eb5 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20  Overflow ){.    
17eb6 20 20 61 70 44 69 76 5b 69 5d 20 3d 20 70 50 61    apDiv[i] = pPa
17eb7 72 65 6e 74 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 70  rent->aOvfl[0].p
17eb8 43 65 6c 6c 3b 0a 20 20 20 20 20 20 70 67 6e 6f  Cell;.      pgno
17eb9 20 3d 20 67 65 74 34 62 79 74 65 28 61 70 44 69   = get4byte(apDi
17eba 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 73 7a 4e  v[i]);.      szN
17ebb 65 77 5b 69 5d 20 3d 20 63 65 6c 6c 53 69 7a 65  ew[i] = cellSize
17ebc 50 74 72 28 70 50 61 72 65 6e 74 2c 20 61 70 44  Ptr(pParent, apD
17ebd 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 70 50  iv[i]);.      pP
17ebe 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77  arent->nOverflow
17ebf 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
17ec0 0a 20 20 20 20 20 20 61 70 44 69 76 5b 69 5d 20  .      apDiv[i] 
17ec1 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 65  = findCell(pPare
17ec2 6e 74 2c 20 69 2b 6e 78 44 69 76 2d 70 50 61 72  nt, i+nxDiv-pPar
17ec3 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 29 3b  ent->nOverflow);
17ec4 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 67 65  .      pgno = ge
17ec5 74 34 62 79 74 65 28 61 70 44 69 76 5b 69 5d 29  t4byte(apDiv[i])
17ec6 3b 0a 20 20 20 20 20 20 73 7a 4e 65 77 5b 69 5d  ;.      szNew[i]
17ec7 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70   = cellSizePtr(p
17ec8 50 61 72 65 6e 74 2c 20 61 70 44 69 76 5b 69 5d  Parent, apDiv[i]
17ec9 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 44 72 6f  );..      /* Dro
17eca 70 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20  p the cell from 
17ecb 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e  the parent page.
17ecc 20 61 70 44 69 76 5b 69 5d 20 73 74 69 6c 6c 20   apDiv[i] still 
17ecd 70 6f 69 6e 74 73 20 74 6f 0a 20 20 20 20 20 20  points to.      
17ece 2a 2a 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68  ** the cell with
17ecf 69 6e 20 74 68 65 20 70 61 72 65 6e 74 2c 20 65  in the parent, e
17ed0 76 65 6e 20 74 68 6f 75 67 68 20 69 74 20 68 61  ven though it ha
17ed1 73 20 62 65 65 6e 20 64 72 6f 70 70 65 64 2e 0a  s been dropped..
17ed2 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73        ** This is
17ed3 20 73 61 66 65 20 62 65 63 61 75 73 65 20 64 72   safe because dr
17ed4 6f 70 70 69 6e 67 20 61 20 63 65 6c 6c 20 6f 6e  opping a cell on
17ed5 6c 79 20 6f 76 65 72 77 72 69 74 65 73 20 74 68  ly overwrites th
17ed6 65 20 66 69 72 73 74 0a 20 20 20 20 20 20 2a 2a  e first.      **
17ed7 20 66 6f 75 72 20 62 79 74 65 73 20 6f 66 20 69   four bytes of i
17ed8 74 2c 20 61 6e 64 20 74 68 69 73 20 66 75 6e 63  t, and this func
17ed9 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65  tion does not ne
17eda 65 64 20 74 68 65 20 66 69 72 73 74 0a 20 20 20  ed the first.   
17edb 20 20 20 2a 2a 20 66 6f 75 72 20 62 79 74 65 73     ** four bytes
17edc 20 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20   of the divider 
17edd 63 65 6c 6c 2e 20 53 6f 20 74 68 65 20 70 6f 69  cell. So the poi
17ede 6e 74 65 72 20 69 73 20 73 61 66 65 20 74 6f 20  nter is safe to 
17edf 75 73 65 0a 20 20 20 20 20 20 2a 2a 20 6c 61 74  use.      ** lat
17ee0 65 72 20 6f 6e 2e 20 20 0a 20 20 20 20 20 20 2a  er on.  .      *
17ee1 2a 0a 20 20 20 20 20 20 2a 2a 20 55 6e 6c 65 73  *.      ** Unles
17ee2 73 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70  s SQLite is comp
17ee3 69 6c 65 64 20 69 6e 20 73 65 63 75 72 65 2d 64  iled in secure-d
17ee4 65 6c 65 74 65 20 6d 6f 64 65 2e 20 49 6e 20 74  elete mode. In t
17ee5 68 69 73 20 63 61 73 65 2c 0a 20 20 20 20 20 20  his case,.      
17ee6 2a 2a 20 74 68 65 20 64 72 6f 70 43 65 6c 6c 28  ** the dropCell(
17ee7 29 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f  ) routine will o
17ee8 76 65 72 77 72 69 74 65 20 74 68 65 20 65 6e 74  verwrite the ent
17ee9 69 72 65 20 63 65 6c 6c 20 77 69 74 68 20 7a 65  ire cell with ze
17eea 72 6f 65 73 2e 0a 20 20 20 20 20 20 2a 2a 20 49  roes..      ** I
17eeb 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74 65 6d  n this case, tem
17eec 70 6f 72 61 72 69 6c 79 20 63 6f 70 79 20 74 68  porarily copy th
17eed 65 20 63 65 6c 6c 20 69 6e 74 6f 20 74 68 65 20  e cell into the 
17eee 61 4f 76 66 6c 53 70 61 63 65 5b 5d 0a 20 20 20  aOvflSpace[].   
17eef 20 20 20 2a 2a 20 62 75 66 66 65 72 2e 20 49 74     ** buffer. It
17ef0 20 77 69 6c 6c 20 62 65 20 63 6f 70 69 65 64 20   will be copied 
17ef1 6f 75 74 20 61 67 61 69 6e 20 61 73 20 73 6f 6f  out again as soo
17ef2 6e 20 61 73 20 74 68 65 20 61 53 70 61 63 65 5b  n as the aSpace[
17ef3 5d 20 62 75 66 66 65 72 0a 20 20 20 20 20 20 2a  ] buffer.      *
17ef4 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2e 20  * is allocated. 
17ef5 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
17ef6 45 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a  E_SECURE_DELETE.
17ef7 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 61 4f        memcpy(&aO
17ef8 76 66 6c 53 70 61 63 65 5b 61 70 44 69 76 5b 69  vflSpace[apDiv[i
17ef9 5d 2d 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61  ]-pParent->aData
17efa 5d 2c 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a 4e  ], apDiv[i], szN
17efb 65 77 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 70  ew[i]);.      ap
17efc 44 69 76 5b 69 5d 20 3d 20 26 61 4f 76 66 6c 53  Div[i] = &aOvflS
17efd 70 61 63 65 5b 61 70 44 69 76 5b 69 5d 2d 70 50  pace[apDiv[i]-pP
17efe 61 72 65 6e 74 2d 3e 61 44 61 74 61 5d 3b 0a 23  arent->aData];.#
17eff 65 6e 64 69 66 0a 20 20 20 20 20 20 64 72 6f 70  endif.      drop
17f00 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b  Cell(pParent, i+
17f01 6e 78 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e  nxDiv-pParent->n
17f02 4f 76 65 72 66 6c 6f 77 2c 20 73 7a 4e 65 77 5b  Overflow, szNew[
17f03 69 5d 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a  i], &rc);.    }.
17f04 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 6e    }..  /* Make n
17f05 4d 61 78 43 65 6c 6c 73 20 61 20 6d 75 6c 74 69  MaxCells a multi
17f06 70 6c 65 20 6f 66 20 34 20 69 6e 20 6f 72 64 65  ple of 4 in orde
17f07 72 20 74 6f 20 70 72 65 73 65 72 76 65 20 38 2d  r to preserve 8-
17f08 62 79 74 65 0a 20 20 2a 2a 20 61 6c 69 67 6e 6d  byte.  ** alignm
17f09 65 6e 74 20 2a 2f 0a 20 20 6e 4d 61 78 43 65 6c  ent */.  nMaxCel
17f0a 6c 73 20 3d 20 28 6e 4d 61 78 43 65 6c 6c 73 20  ls = (nMaxCells 
17f0b 2b 20 33 29 26 7e 33 3b 0a 0a 20 20 2f 2a 0a 20  + 3)&~3;..  /*. 
17f0c 20 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61   ** Allocate spa
17f0d 63 65 20 66 6f 72 20 6d 65 6d 6f 72 79 20 73 74  ce for memory st
17f0e 72 75 63 74 75 72 65 73 0a 20 20 2a 2f 0a 20 20  ructures.  */.  
17f0f 6b 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  k = pBt->pageSiz
17f10 65 20 2b 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f  e + ROUND8(sizeo
17f11 66 28 4d 65 6d 50 61 67 65 29 29 3b 0a 20 20 73  f(MemPage));.  s
17f12 7a 53 63 72 61 74 63 68 20 3d 0a 20 20 20 20 20  zScratch =.     
17f13 20 20 6e 4d 61 78 43 65 6c 6c 73 2a 73 69 7a 65    nMaxCells*size
17f14 6f 66 28 75 38 2a 29 20 20 20 20 20 20 20 20 20  of(u8*)         
17f15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17f16 20 61 70 43 65 6c 6c 20 2a 2f 0a 20 20 20 20 20   apCell */.     
17f17 2b 20 6e 4d 61 78 43 65 6c 6c 73 2a 73 69 7a 65  + nMaxCells*size
17f18 6f 66 28 75 31 36 29 20 20 20 20 20 20 20 20 20  of(u16)         
17f19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17f1a 20 73 7a 43 65 6c 6c 20 2a 2f 0a 20 20 20 20 20   szCell */.     
17f1b 2b 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20  + pBt->pageSize 
17f1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17f1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17f1e 20 61 53 70 61 63 65 31 20 2a 2f 0a 20 20 20 20   aSpace1 */.    
17f1f 20 2b 20 6b 2a 6e 4f 6c 64 3b 20 20 20 20 20 20   + k*nOld;      
17f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17f21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
17f22 2a 20 50 61 67 65 20 63 6f 70 69 65 73 20 28 61  * Page copies (a
17f23 70 43 6f 70 79 29 20 2a 2f 0a 20 20 61 70 43 65  pCopy) */.  apCe
17f24 6c 6c 20 3d 20 73 71 6c 69 74 65 33 53 63 72 61  ll = sqlite3Scra
17f25 74 63 68 4d 61 6c 6c 6f 63 28 20 73 7a 53 63 72  tchMalloc( szScr
17f26 61 74 63 68 20 29 3b 20 0a 20 20 69 66 28 20 61  atch ); .  if( a
17f27 70 43 65 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20  pCell==0 ){.    
17f28 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
17f29 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 62 61 6c 61  M;.    goto bala
17f2a 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  nce_cleanup;.  }
17f2b 0a 20 20 73 7a 43 65 6c 6c 20 3d 20 28 75 31 36  .  szCell = (u16
17f2c 2a 29 26 61 70 43 65 6c 6c 5b 6e 4d 61 78 43 65  *)&apCell[nMaxCe
17f2d 6c 6c 73 5d 3b 0a 20 20 61 53 70 61 63 65 31 20  lls];.  aSpace1 
17f2e 3d 20 28 75 38 2a 29 26 73 7a 43 65 6c 6c 5b 6e  = (u8*)&szCell[n
17f2f 4d 61 78 43 65 6c 6c 73 5d 3b 0a 20 20 61 73 73  MaxCells];.  ass
17f30 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f  ert( EIGHT_BYTE_
17f31 41 4c 49 47 4e 4d 45 4e 54 28 61 53 70 61 63 65  ALIGNMENT(aSpace
17f32 31 29 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  1) );..  /*.  **
17f33 20 4c 6f 61 64 20 70 6f 69 6e 74 65 72 73 20 74   Load pointers t
17f34 6f 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 73  o all cells on s
17f35 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 6e 64  ibling pages and
17f36 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c   the divider cel
17f37 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65  ls.  ** into the
17f38 20 6c 6f 63 61 6c 20 61 70 43 65 6c 6c 5b 5d 20   local apCell[] 
17f39 61 72 72 61 79 2e 20 20 4d 61 6b 65 20 63 6f 70  array.  Make cop
17f3a 69 65 73 20 6f 66 20 74 68 65 20 64 69 76 69 64  ies of the divid
17f3b 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e  er cells.  ** in
17f3c 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65  to space obtaine
17f3d 64 20 66 72 6f 6d 20 61 53 70 61 63 65 31 5b 5d  d from aSpace1[]
17f3e 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 65 20   and remove the 
17f3f 74 68 65 20 64 69 76 69 64 65 72 20 43 65 6c 6c  the divider Cell
17f40 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 70 50 61 72  s.  ** from pPar
17f41 65 6e 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  ent..  **.  ** I
17f42 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 61  f the siblings a
17f43 72 65 20 6f 6e 20 6c 65 61 66 20 70 61 67 65 73  re on leaf pages
17f44 2c 20 74 68 65 6e 20 74 68 65 20 63 68 69 6c 64  , then the child
17f45 20 70 6f 69 6e 74 65 72 73 20 6f 66 20 74 68 65   pointers of the
17f46 0a 20 20 2a 2a 20 64 69 76 69 64 65 72 20 63 65  .  ** divider ce
17f47 6c 6c 73 20 61 72 65 20 73 74 72 69 70 70 65 64  lls are stripped
17f48 20 66 72 6f 6d 20 74 68 65 20 63 65 6c 6c 73 20   from the cells 
17f49 62 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20  before they are 
17f4a 63 6f 70 69 65 64 0a 20 20 2a 2a 20 69 6e 74 6f  copied.  ** into
17f4b 20 61 53 70 61 63 65 31 5b 5d 2e 20 20 49 6e 20   aSpace1[].  In 
17f4c 74 68 69 73 20 77 61 79 2c 20 61 6c 6c 20 63 65  this way, all ce
17f4d 6c 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20  lls in apCell[] 
17f4e 61 72 65 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a  are without.  **
17f4f 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e   child pointers.
17f50 20 20 49 66 20 73 69 62 6c 69 6e 67 73 20 61 72    If siblings ar
17f51 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74 68  e not leaves, th
17f52 65 6e 20 61 6c 6c 20 63 65 6c 6c 20 69 6e 0a 20  en all cell in. 
17f53 20 2a 2a 20 61 70 43 65 6c 6c 5b 5d 20 69 6e 63   ** apCell[] inc
17f54 6c 75 64 65 20 63 68 69 6c 64 20 70 6f 69 6e 74  lude child point
17f55 65 72 73 2e 20 20 45 69 74 68 65 72 20 77 61 79  ers.  Either way
17f56 2c 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61  , all cells in a
17f57 70 43 65 6c 6c 5b 5d 0a 20 20 2a 2a 20 61 72 65  pCell[].  ** are
17f58 20 61 6c 69 6b 65 2e 0a 20 20 2a 2a 0a 20 20 2a   alike..  **.  *
17f59 2a 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e  * leafCorrection
17f5a 3a 20 20 34 20 69 66 20 70 50 61 67 65 20 69 73  :  4 if pPage is
17f5b 20 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20 70   a leaf.  0 if p
17f5c 50 61 67 65 20 69 73 20 6e 6f 74 20 61 20 6c 65  Page is not a le
17f5d 61 66 2e 0a 20 20 2a 2a 20 20 20 20 20 20 20 6c  af..  **       l
17f5e 65 61 66 44 61 74 61 3a 20 20 31 20 69 66 20 70  eafData:  1 if p
17f5f 50 61 67 65 20 68 6f 6c 64 73 20 6b 65 79 2b 64  Page holds key+d
17f60 61 74 61 20 61 6e 64 20 70 50 61 72 65 6e 74 20  ata and pParent 
17f61 68 6f 6c 64 73 20 6f 6e 6c 79 20 6b 65 79 73 2e  holds only keys.
17f62 0a 20 20 2a 2f 0a 20 20 6c 65 61 66 43 6f 72 72  .  */.  leafCorr
17f63 65 63 74 69 6f 6e 20 3d 20 61 70 4f 6c 64 5b 30  ection = apOld[0
17f64 5d 2d 3e 6c 65 61 66 2a 34 3b 0a 20 20 6c 65 61  ]->leaf*4;.  lea
17f65 66 44 61 74 61 20 3d 20 61 70 4f 6c 64 5b 30 5d  fData = apOld[0]
17f66 2d 3e 68 61 73 44 61 74 61 3b 0a 20 20 66 6f 72  ->hasData;.  for
17f67 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b  (i=0; i<nOld; i+
17f68 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6c 69 6d 69  +){.    int limi
17f69 74 3b 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20 42  t;.    .    /* B
17f6a 65 66 6f 72 65 20 64 6f 69 6e 67 20 61 6e 79 74  efore doing anyt
17f6b 68 69 6e 67 20 65 6c 73 65 2c 20 74 61 6b 65 20  hing else, take 
17f6c 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 69 27  a copy of the i'
17f6d 74 68 20 6f 72 69 67 69 6e 61 6c 20 73 69 62 6c  th original sibl
17f6e 69 6e 67 0a 20 20 20 20 2a 2a 20 54 68 65 20 72  ing.    ** The r
17f6f 65 73 74 20 6f 66 20 74 68 69 73 20 66 75 6e 63  est of this func
17f70 74 69 6f 6e 20 77 69 6c 6c 20 75 73 65 20 64 61  tion will use da
17f71 74 61 20 66 72 6f 6d 20 74 68 65 20 63 6f 70 69  ta from the copi
17f72 65 73 20 72 61 74 68 65 72 0a 20 20 20 20 2a 2a  es rather.    **
17f73 20 74 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e   that the origin
17f74 61 6c 20 70 61 67 65 73 20 73 69 6e 63 65 20 74  al pages since t
17f75 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65  he original page
17f76 73 20 77 69 6c 6c 20 62 65 20 69 6e 20 74 68 65  s will be in the
17f77 0a 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 20  .    ** process 
17f78 6f 66 20 62 65 69 6e 67 20 6f 76 65 72 77 72 69  of being overwri
17f79 74 74 65 6e 2e 20 20 2a 2f 0a 20 20 20 20 4d 65  tten.  */.    Me
17f7a 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d 20 61 70  mPage *pOld = ap
17f7b 43 6f 70 79 5b 69 5d 20 3d 20 28 4d 65 6d 50 61  Copy[i] = (MemPa
17f7c 67 65 2a 29 26 61 53 70 61 63 65 31 5b 70 42 74  ge*)&aSpace1[pBt
17f7d 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 6b 2a 69  ->pageSize + k*i
17f7e 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4f  ];.    memcpy(pO
17f7f 6c 64 2c 20 61 70 4f 6c 64 5b 69 5d 2c 20 73 69  ld, apOld[i], si
17f80 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 3b 0a  zeof(MemPage));.
17f81 20 20 20 20 70 4f 6c 64 2d 3e 61 44 61 74 61 20      pOld->aData 
17f82 3d 20 28 76 6f 69 64 2a 29 26 70 4f 6c 64 5b 31  = (void*)&pOld[1
17f83 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4f  ];.    memcpy(pO
17f84 6c 64 2d 3e 61 44 61 74 61 2c 20 61 70 4f 6c 64  ld->aData, apOld
17f85 5b 69 5d 2d 3e 61 44 61 74 61 2c 20 70 42 74 2d  [i]->aData, pBt-
17f86 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20 20  >pageSize);..   
17f87 20 6c 69 6d 69 74 20 3d 20 70 4f 6c 64 2d 3e 6e   limit = pOld->n
17f88 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e 4f 76 65 72  Cell+pOld->nOver
17f89 66 6c 6f 77 3b 0a 20 20 20 20 66 6f 72 28 6a 3d  flow;.    for(j=
17f8a 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29  0; j<limit; j++)
17f8b 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
17f8c 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20  nCell<nMaxCells 
17f8d 29 3b 0a 20 20 20 20 20 20 61 70 43 65 6c 6c 5b  );.      apCell[
17f8e 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e 64 4f 76 65  nCell] = findOve
17f8f 72 66 6c 6f 77 43 65 6c 6c 28 70 4f 6c 64 2c 20  rflowCell(pOld, 
17f90 6a 29 3b 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c  j);.      szCell
17f91 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65 6c 6c 53 69  [nCell] = cellSi
17f92 7a 65 50 74 72 28 70 4f 6c 64 2c 20 61 70 43 65  zePtr(pOld, apCe
17f93 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a 20 20 20 20  ll[nCell]);.    
17f94 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 7d    nCell++;.    }
17f95 0a 20 20 20 20 69 66 28 20 69 3c 6e 4f 6c 64 2d  .    if( i<nOld-
17f96 31 20 26 26 20 21 6c 65 61 66 44 61 74 61 29 7b  1 && !leafData){
17f97 0a 20 20 20 20 20 20 75 31 36 20 73 7a 20 3d 20  .      u16 sz = 
17f98 28 75 31 36 29 73 7a 4e 65 77 5b 69 5d 3b 0a 20  (u16)szNew[i];. 
17f99 20 20 20 20 20 75 38 20 2a 70 54 65 6d 70 3b 0a       u8 *pTemp;.
17f9a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 43        assert( nC
17f9b 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b  ell<nMaxCells );
17f9c 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b 6e 43  .      szCell[nC
17f9d 65 6c 6c 5d 20 3d 20 73 7a 3b 0a 20 20 20 20 20  ell] = sz;.     
17f9e 20 70 54 65 6d 70 20 3d 20 26 61 53 70 61 63 65   pTemp = &aSpace
17f9f 31 5b 69 53 70 61 63 65 31 5d 3b 0a 20 20 20 20  1[iSpace1];.    
17fa0 20 20 69 53 70 61 63 65 31 20 2b 3d 20 73 7a 3b    iSpace1 += sz;
17fa1 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73  .      assert( s
17fa2 7a 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  z<=pBt->pageSize
17fa3 2f 34 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  /4 );.      asse
17fa4 72 74 28 20 69 53 70 61 63 65 31 3c 3d 70 42 74  rt( iSpace1<=pBt
17fa5 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20  ->pageSize );.  
17fa6 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d 70      memcpy(pTemp
17fa7 2c 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a 29 3b  , apDiv[i], sz);
17fa8 0a 20 20 20 20 20 20 61 70 43 65 6c 6c 5b 6e 43  .      apCell[nC
17fa9 65 6c 6c 5d 20 3d 20 70 54 65 6d 70 2b 6c 65 61  ell] = pTemp+lea
17faa 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 20 20  fCorrection;.   
17fab 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43     assert( leafC
17fac 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 7c 7c 20  orrection==0 || 
17fad 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d  leafCorrection==
17fae 34 20 29 3b 0a 20 20 20 20 20 20 73 7a 43 65 6c  4 );.      szCel
17faf 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a 43 65 6c  l[nCell] = szCel
17fb0 6c 5b 6e 43 65 6c 6c 5d 20 2d 20 6c 65 61 66 43  l[nCell] - leafC
17fb1 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20  orrection;.     
17fb2 20 69 66 28 20 21 70 4f 6c 64 2d 3e 6c 65 61 66   if( !pOld->leaf
17fb3 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
17fb4 72 74 28 20 6c 65 61 66 43 6f 72 72 65 63 74 69  rt( leafCorrecti
17fb5 6f 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  on==0 );.       
17fb6 20 61 73 73 65 72 74 28 20 70 4f 6c 64 2d 3e 68   assert( pOld->h
17fb7 64 72 4f 66 66 73 65 74 3d 3d 30 20 29 3b 0a 20  drOffset==0 );. 
17fb8 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69         /* The ri
17fb9 67 68 74 20 70 6f 69 6e 74 65 72 20 6f 66 20 74  ght pointer of t
17fba 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 70 4f  he child page pO
17fbb 6c 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 6c  ld becomes the l
17fbc 65 66 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 70  eft.        ** p
17fbd 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 64 69  ointer of the di
17fbe 76 69 64 65 72 20 63 65 6c 6c 20 2a 2f 0a 20 20  vider cell */.  
17fbf 20 20 20 20 20 20 6d 65 6d 63 70 79 28 61 70 43        memcpy(apC
17fc0 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c 20 26 70 4f 6c  ell[nCell], &pOl
17fc1 64 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 34 29 3b  d->aData[8], 4);
17fc2 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
17fc3 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65        assert( le
17fc4 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20  afCorrection==4 
17fc5 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  );.        if( s
17fc6 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 3c 34 20 29  zCell[nCell]<4 )
17fc7 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  {.          /* D
17fc8 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 6e 79 20  o not allow any 
17fc9 63 65 6c 6c 73 20 73 6d 61 6c 6c 65 72 20 74 68  cells smaller th
17fca 61 6e 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20  an 4 bytes. */. 
17fcb 20 20 20 20 20 20 20 20 20 73 7a 43 65 6c 6c 5b           szCell[
17fcc 6e 43 65 6c 6c 5d 20 3d 20 34 3b 0a 20 20 20 20  nCell] = 4;.    
17fcd 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
17fce 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20      nCell++;.   
17fcf 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a   }.  }..  /*.  *
17fd0 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 65  * Figure out the
17fd1 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
17fd2 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20   needed to hold 
17fd3 61 6c 6c 20 6e 43 65 6c 6c 20 63 65 6c 6c 73 2e  all nCell cells.
17fd4 0a 20 20 2a 2a 20 53 74 6f 72 65 20 74 68 69 73  .  ** Store this
17fd5 20 6e 75 6d 62 65 72 20 69 6e 20 22 6b 22 2e 20   number in "k". 
17fd6 20 41 6c 73 6f 20 63 6f 6d 70 75 74 65 20 73 7a   Also compute sz
17fd7 4e 65 77 5b 5d 20 77 68 69 63 68 20 69 73 20 74  New[] which is t
17fd8 68 65 20 74 6f 74 61 6c 0a 20 20 2a 2a 20 73 69  he total.  ** si
17fd9 7a 65 20 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20  ze of all cells 
17fda 6f 6e 20 74 68 65 20 69 2d 74 68 20 70 61 67 65  on the i-th page
17fdb 20 61 6e 64 20 63 6e 74 4e 65 77 5b 5d 20 77 68   and cntNew[] wh
17fdc 69 63 68 20 69 73 20 74 68 65 20 69 6e 64 65 78  ich is the index
17fdd 0a 20 20 2a 2a 20 69 6e 20 61 70 43 65 6c 6c 5b  .  ** in apCell[
17fde 5d 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74 68  ] of the cell th
17fdf 61 74 20 64 69 76 69 64 65 73 20 70 61 67 65 20  at divides page 
17fe0 69 20 66 72 6f 6d 20 70 61 67 65 20 69 2b 31 2e  i from page i+1.
17fe1 20 20 0a 20 20 2a 2a 20 63 6e 74 4e 65 77 5b 6b    .  ** cntNew[k
17fe2 5d 20 73 68 6f 75 6c 64 20 65 71 75 61 6c 20 6e  ] should equal n
17fe3 43 65 6c 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  Cell..  **.  ** 
17fe4 56 61 6c 75 65 73 20 63 6f 6d 70 75 74 65 64 20  Values computed 
17fe5 62 79 20 74 68 69 73 20 62 6c 6f 63 6b 3a 0a 20  by this block:. 
17fe6 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20   **.  **        
17fe7 20 20 20 6b 3a 20 54 68 65 20 74 6f 74 61 6c 20     k: The total 
17fe8 6e 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69 6e  number of siblin
17fe9 67 20 70 61 67 65 73 0a 20 20 2a 2a 20 20 20 20  g pages.  **    
17fea 73 7a 4e 65 77 5b 69 5d 3a 20 53 70 61 63 65 64  szNew[i]: Spaced
17feb 20 75 73 65 64 20 6f 6e 20 74 68 65 20 69 2d 74   used on the i-t
17fec 68 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e 0a  h sibling page..
17fed 20 20 2a 2a 20 20 20 63 6e 74 4e 65 77 5b 69 5d    **   cntNew[i]
17fee 3a 20 49 6e 64 65 78 20 69 6e 20 61 70 43 65 6c  : Index in apCel
17fef 6c 5b 5d 20 61 6e 64 20 73 7a 43 65 6c 6c 5b 5d  l[] and szCell[]
17ff0 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 63   for the first c
17ff1 65 6c 6c 20 74 6f 0a 20 20 2a 2a 20 20 20 20 20  ell to.  **     
17ff2 20 20 20 20 20 20 20 20 20 74 68 65 20 72 69 67           the rig
17ff3 68 74 20 6f 66 20 74 68 65 20 69 2d 74 68 20 73  ht of the i-th s
17ff4 69 62 6c 69 6e 67 20 70 61 67 65 2e 0a 20 20 2a  ibling page..  *
17ff5 2a 20 75 73 61 62 6c 65 53 70 61 63 65 3a 20 4e  * usableSpace: N
17ff6 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
17ff7 66 20 73 70 61 63 65 20 61 76 61 69 6c 61 62 6c  f space availabl
17ff8 65 20 6f 6e 20 65 61 63 68 20 73 69 62 6c 69 6e  e on each siblin
17ff9 67 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2f 0a 20 20  g..  ** .  */.  
17ffa 75 73 61 62 6c 65 53 70 61 63 65 20 3d 20 70 42  usableSpace = pB
17ffb 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20  t->usableSize - 
17ffc 31 32 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 74  12 + leafCorrect
17ffd 69 6f 6e 3b 0a 20 20 66 6f 72 28 73 75 62 74 6f  ion;.  for(subto
17ffe 74 61 6c 3d 6b 3d 69 3d 30 3b 20 69 3c 6e 43 65  tal=k=i=0; i<nCe
17fff 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 73  ll; i++){.    as
18000 73 65 72 74 28 20 69 3c 6e 4d 61 78 43 65 6c 6c  sert( i<nMaxCell
18001 73 20 29 3b 0a 20 20 20 20 73 75 62 74 6f 74 61  s );.    subtota
18002 6c 20 2b 3d 20 73 7a 43 65 6c 6c 5b 69 5d 20 2b  l += szCell[i] +
18003 20 32 3b 0a 20 20 20 20 69 66 28 20 73 75 62 74   2;.    if( subt
18004 6f 74 61 6c 20 3e 20 75 73 61 62 6c 65 53 70 61  otal > usableSpa
18005 63 65 20 29 7b 0a 20 20 20 20 20 20 73 7a 4e 65  ce ){.      szNe
18006 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 6c 20  w[k] = subtotal 
18007 2d 20 73 7a 43 65 6c 6c 5b 69 5d 3b 0a 20 20 20  - szCell[i];.   
18008 20 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 69     cntNew[k] = i
18009 3b 0a 20 20 20 20 20 20 69 66 28 20 6c 65 61 66  ;.      if( leaf
1800a 44 61 74 61 20 29 7b 20 69 2d 2d 3b 20 7d 0a 20  Data ){ i--; }. 
1800b 20 20 20 20 20 73 75 62 74 6f 74 61 6c 20 3d 20       subtotal = 
1800c 30 3b 0a 20 20 20 20 20 20 6b 2b 2b 3b 0a 20 20  0;.      k++;.  
1800d 20 20 20 20 69 66 28 20 6b 3e 4e 42 2b 31 20 29      if( k>NB+1 )
1800e 7b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f  { rc = SQLITE_CO
1800f 52 52 55 50 54 3b 20 67 6f 74 6f 20 62 61 6c 61  RRUPT; goto bala
18010 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 20 7d 0a 20  nce_cleanup; }. 
18011 20 20 20 7d 0a 20 20 7d 0a 20 20 73 7a 4e 65 77     }.  }.  szNew
18012 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61 6c 3b 0a  [k] = subtotal;.
18013 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d 20 6e 43    cntNew[k] = nC
18014 65 6c 6c 3b 0a 20 20 6b 2b 2b 3b 0a 0a 20 20 2f  ell;.  k++;..  /
18015 2a 0a 20 20 2a 2a 20 54 68 65 20 70 61 63 6b 69  *.  ** The packi
18016 6e 67 20 63 6f 6d 70 75 74 65 64 20 62 79 20 74  ng computed by t
18017 68 65 20 70 72 65 76 69 6f 75 73 20 62 6c 6f 63  he previous bloc
18018 6b 20 69 73 20 62 69 61 73 65 64 20 74 6f 77 61  k is biased towa
18019 72 64 20 74 68 65 20 73 69 62 6c 69 6e 67 73 0a  rd the siblings.
1801a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 65 66 74    ** on the left
1801b 20 73 69 64 65 2e 20 20 54 68 65 20 6c 65 66 74   side.  The left
1801c 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 61 6c   siblings are al
1801d 77 61 79 73 20 6e 65 61 72 6c 79 20 66 75 6c 6c  ways nearly full
1801e 2c 20 77 68 69 6c 65 20 74 68 65 0a 20 20 2a 2a  , while the.  **
1801f 20 72 69 67 68 74 2d 6d 6f 73 74 20 73 69 62 6c   right-most sibl
18020 69 6e 67 20 6d 69 67 68 74 20 62 65 20 6e 65 61  ing might be nea
18021 72 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 69 73  rly empty.  This
18022 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 61   block of code a
18023 74 74 65 6d 70 74 73 0a 20 20 2a 2a 20 74 6f 20  ttempts.  ** to 
18024 61 64 6a 75 73 74 20 74 68 65 20 70 61 63 6b 69  adjust the packi
18025 6e 67 20 6f 66 20 73 69 62 6c 69 6e 67 73 20 74  ng of siblings t
18026 6f 20 67 65 74 20 61 20 62 65 74 74 65 72 20 62  o get a better b
18027 61 6c 61 6e 63 65 2e 0a 20 20 2a 2a 0a 20 20 2a  alance..  **.  *
18028 2a 20 54 68 69 73 20 61 64 6a 75 73 74 6d 65 6e  * This adjustmen
18029 74 20 69 73 20 6d 6f 72 65 20 74 68 61 6e 20 61  t is more than a
1802a 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20  n optimization. 
1802b 20 54 68 65 20 70 61 63 6b 69 6e 67 20 61 62 6f   The packing abo
1802c 76 65 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62 65  ve might.  ** be
1802d 20 73 6f 20 6f 75 74 20 6f 66 20 62 61 6c 61 6e   so out of balan
1802e 63 65 20 61 73 20 74 6f 20 62 65 20 69 6c 6c 65  ce as to be ille
1802f 67 61 6c 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c  gal.  For exampl
18030 65 2c 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  e, the right-mos
18031 74 0a 20 20 2a 2a 20 73 69 62 6c 69 6e 67 20 6d  t.  ** sibling m
18032 69 67 68 74 20 62 65 20 63 6f 6d 70 6c 65 74 65  ight be complete
18033 6c 79 20 65 6d 70 74 79 2e 20 20 54 68 69 73 20  ly empty.  This 
18034 61 64 6a 75 73 74 6d 65 6e 74 20 69 73 20 6e 6f  adjustment is no
18035 74 20 6f 70 74 69 6f 6e 61 6c 2e 0a 20 20 2a 2f  t optional..  */
18036 0a 20 20 66 6f 72 28 69 3d 6b 2d 31 3b 20 69 3e  .  for(i=k-1; i>
18037 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74  0; i--){.    int
18038 20 73 7a 52 69 67 68 74 20 3d 20 73 7a 4e 65 77   szRight = szNew
18039 5b 69 5d 3b 20 20 2f 2a 20 53 69 7a 65 20 6f 66  [i];  /* Size of
1803a 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 68 65 20   sibling on the 
1803b 72 69 67 68 74 20 2a 2f 0a 20 20 20 20 69 6e 74  right */.    int
1803c 20 73 7a 4c 65 66 74 20 3d 20 73 7a 4e 65 77 5b   szLeft = szNew[
1803d 69 2d 31 5d 3b 20 2f 2a 20 53 69 7a 65 20 6f 66  i-1]; /* Size of
1803e 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74 68 65 20   sibling on the 
1803f 6c 65 66 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20  left */.    int 
18040 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
18041 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 69 67 68  /* Index of righ
18042 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 69 6e 20 6c  t-most cell in l
18043 65 66 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0a 20  eft sibling */. 
18044 20 20 20 69 6e 74 20 64 3b 20 20 20 20 20 20 20     int d;       
18045 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
18046 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 74 6f  of first cell to
18047 20 74 68 65 20 6c 65 66 74 20 6f 66 20 72 69 67   the left of rig
18048 68 74 20 73 69 62 6c 69 6e 67 20 2a 2f 0a 0a 20  ht sibling */.. 
18049 20 20 20 72 20 3d 20 63 6e 74 4e 65 77 5b 69 2d     r = cntNew[i-
1804a 31 5d 20 2d 20 31 3b 0a 20 20 20 20 64 20 3d 20  1] - 1;.    d = 
1804b 72 20 2b 20 31 20 2d 20 6c 65 61 66 44 61 74 61  r + 1 - leafData
1804c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 3c  ;.    assert( d<
1804d 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20  nMaxCells );.   
1804e 20 61 73 73 65 72 74 28 20 72 3c 6e 4d 61 78 43   assert( r<nMaxC
1804f 65 6c 6c 73 20 29 3b 0a 20 20 20 20 77 68 69 6c  ells );.    whil
18050 65 28 20 73 7a 52 69 67 68 74 3d 3d 30 20 7c 7c  e( szRight==0 ||
18051 20 73 7a 52 69 67 68 74 2b 73 7a 43 65 6c 6c 5b   szRight+szCell[
18052 64 5d 2b 32 3c 3d 73 7a 4c 65 66 74 2d 28 73 7a  d]+2<=szLeft-(sz
18053 43 65 6c 6c 5b 72 5d 2b 32 29 20 29 7b 0a 20 20  Cell[r]+2) ){.  
18054 20 20 20 20 73 7a 52 69 67 68 74 20 2b 3d 20 73      szRight += s
18055 7a 43 65 6c 6c 5b 64 5d 20 2b 20 32 3b 0a 20 20  zCell[d] + 2;.  
18056 20 20 20 20 73 7a 4c 65 66 74 20 2d 3d 20 73 7a      szLeft -= sz
18057 43 65 6c 6c 5b 72 5d 20 2b 20 32 3b 0a 20 20 20  Cell[r] + 2;.   
18058 20 20 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 2d 2d     cntNew[i-1]--
18059 3b 0a 20 20 20 20 20 20 72 20 3d 20 63 6e 74 4e  ;.      r = cntN
1805a 65 77 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20  ew[i-1] - 1;.   
1805b 20 20 20 64 20 3d 20 72 20 2b 20 31 20 2d 20 6c     d = r + 1 - l
1805c 65 61 66 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20  eafData;.    }. 
1805d 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 73 7a     szNew[i] = sz
1805e 52 69 67 68 74 3b 0a 20 20 20 20 73 7a 4e 65 77  Right;.    szNew
1805f 5b 69 2d 31 5d 20 3d 20 73 7a 4c 65 66 74 3b 0a  [i-1] = szLeft;.
18060 20 20 7d 0a 0a 20 20 2f 2a 20 45 69 74 68 65 72    }..  /* Either
18061 20 77 65 20 66 6f 75 6e 64 20 6f 6e 65 20 6f 72   we found one or
18062 20 6d 6f 72 65 20 63 65 6c 6c 73 20 28 63 6e 74   more cells (cnt
18063 6e 65 77 5b 30 5d 29 3e 30 29 20 6f 72 20 70 50  new[0])>0) or pP
18064 61 67 65 20 69 73 0a 20 20 2a 2a 20 61 20 76 69  age is.  ** a vi
18065 72 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65 2e  rtual root page.
18066 20 20 41 20 76 69 72 74 75 61 6c 20 72 6f 6f 74    A virtual root
18067 20 70 61 67 65 20 69 73 20 77 68 65 6e 20 74 68   page is when th
18068 65 20 72 65 61 6c 20 72 6f 6f 74 0a 20 20 2a 2a  e real root.  **
18069 20 70 61 67 65 20 69 73 20 70 61 67 65 20 31 20   page is page 1 
1806a 61 6e 64 20 77 65 20 61 72 65 20 74 68 65 20 6f  and we are the o
1806b 6e 6c 79 20 63 68 69 6c 64 20 6f 66 20 74 68 61  nly child of tha
1806c 74 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 61  t page..  */.  a
1806d 73 73 65 72 74 28 20 63 6e 74 4e 65 77 5b 30 5d  ssert( cntNew[0]
1806e 3e 30 20 7c 7c 20 28 70 50 61 72 65 6e 74 2d 3e  >0 || (pParent->
1806f 70 67 6e 6f 3d 3d 31 20 26 26 20 70 50 61 72 65  pgno==1 && pPare
18070 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 29 20 29 3b  nt->nCell==0) );
18071 0a 0a 20 20 54 52 41 43 45 28 28 22 42 41 4c 41  ..  TRACE(("BALA
18072 4e 43 45 3a 20 6f 6c 64 3a 20 25 64 20 25 64 20  NCE: old: %d %d 
18073 25 64 20 20 22 2c 0a 20 20 20 20 61 70 4f 6c 64  %d  ",.    apOld
18074 5b 30 5d 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20  [0]->pgno, .    
18075 6e 4f 6c 64 3e 3d 32 20 3f 20 61 70 4f 6c 64 5b  nOld>=2 ? apOld[
18076 31 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 0a 20 20  1]->pgno : 0,.  
18077 20 20 6e 4f 6c 64 3e 3d 33 20 3f 20 61 70 4f 6c    nOld>=3 ? apOl
18078 64 5b 32 5d 2d 3e 70 67 6e 6f 20 3a 20 30 0a 20  d[2]->pgno : 0. 
18079 20 29 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20   ));..  /*.  ** 
1807a 41 6c 6c 6f 63 61 74 65 20 6b 20 6e 65 77 20 70  Allocate k new p
1807b 61 67 65 73 2e 20 20 52 65 75 73 65 20 6f 6c 64  ages.  Reuse old
1807c 20 70 61 67 65 73 20 77 68 65 72 65 20 70 6f 73   pages where pos
1807d 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  sible..  */.  if
1807e 28 20 61 70 4f 6c 64 5b 30 5d 2d 3e 70 67 6e 6f  ( apOld[0]->pgno
1807f 3c 3d 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  <=1 ){.    rc = 
18080 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a  SQLITE_CORRUPT;.
18081 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65      goto balance
18082 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20  _cleanup;.  }.  
18083 70 61 67 65 46 6c 61 67 73 20 3d 20 61 70 4f 6c  pageFlags = apOl
18084 64 5b 30 5d 2d 3e 61 44 61 74 61 5b 30 5d 3b 0a  d[0]->aData[0];.
18085 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6b 3b 20    for(i=0; i<k; 
18086 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67  i++){.    MemPag
18087 65 20 2a 70 4e 65 77 3b 0a 20 20 20 20 69 66 28  e *pNew;.    if(
18088 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 20   i<nOld ){.     
18089 20 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 69 5d   pNew = apNew[i]
1808a 20 3d 20 61 70 4f 6c 64 5b 69 5d 3b 0a 20 20 20   = apOld[i];.   
1808b 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b     apOld[i] = 0;
1808c 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1808d 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 4e  te3PagerWrite(pN
1808e 65 77 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  ew->pDbPage);.  
1808f 20 20 20 20 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20      nNew++;.    
18090 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
18091 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b  balance_cleanup;
18092 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
18093 20 20 61 73 73 65 72 74 28 20 69 3e 30 20 29 3b    assert( i>0 );
18094 0a 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f  .      rc = allo
18095 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42  cateBtreePage(pB
18096 74 2c 20 26 70 4e 65 77 2c 20 26 70 67 6e 6f 2c  t, &pNew, &pgno,
18097 20 70 67 6e 6f 2c 20 30 29 3b 0a 20 20 20 20 20   pgno, 0);.     
18098 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 62   if( rc ) goto b
18099 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a  alance_cleanup;.
1809a 20 20 20 20 20 20 61 70 4e 65 77 5b 69 5d 20 3d        apNew[i] =
1809b 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 6e 4e 65   pNew;.      nNe
1809c 77 2b 2b 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 53  w++;..      /* S
1809d 65 74 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  et the pointer-m
1809e 61 70 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65  ap entry for the
1809f 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61 67   new sibling pag
180a0 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  e. */.      if( 
180a1 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a  ISAUTOVACUUM ){.
180a2 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 50 75          ptrmapPu
180a3 74 28 70 42 74 2c 20 70 4e 65 77 2d 3e 70 67 6e  t(pBt, pNew->pgn
180a4 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c  o, PTRMAP_BTREE,
180a5 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 2c 20   pParent->pgno, 
180a6 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 69 66  &rc);.        if
180a7 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
180a8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  ){.          got
180a9 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75  o balance_cleanu
180aa 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  p;.        }.   
180ab 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
180ac 20 20 2f 2a 20 46 72 65 65 20 61 6e 79 20 6f 6c    /* Free any ol
180ad 64 20 70 61 67 65 73 20 74 68 61 74 20 77 65 72  d pages that wer
180ae 65 20 6e 6f 74 20 72 65 75 73 65 64 20 61 73 20  e not reused as 
180af 6e 65 77 20 70 61 67 65 73 2e 0a 20 20 2a 2f 0a  new pages..  */.
180b0 20 20 77 68 69 6c 65 28 20 69 3c 6e 4f 6c 64 20    while( i<nOld 
180b1 29 7b 0a 20 20 20 20 66 72 65 65 50 61 67 65 28  ){.    freePage(
180b2 61 70 4f 6c 64 5b 69 5d 2c 20 26 72 63 29 3b 0a  apOld[i], &rc);.
180b3 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74      if( rc ) got
180b4 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75  o balance_cleanu
180b5 70 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61  p;.    releasePa
180b6 67 65 28 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20  ge(apOld[i]);.  
180b7 20 20 61 70 4f 6c 64 5b 69 5d 20 3d 20 30 3b 0a    apOld[i] = 0;.
180b8 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 0a 20 20      i++;.  }..  
180b9 2f 2a 0a 20 20 2a 2a 20 50 75 74 20 74 68 65 20  /*.  ** Put the 
180ba 6e 65 77 20 70 61 67 65 73 20 69 6e 20 61 63 63  new pages in acc
180bb 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 20 20 54  ending order.  T
180bc 68 69 73 20 68 65 6c 70 73 20 74 6f 0a 20 20 2a  his helps to.  *
180bd 2a 20 6b 65 65 70 20 65 6e 74 72 69 65 73 20 69  * keep entries i
180be 6e 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 20  n the disk file 
180bf 69 6e 20 6f 72 64 65 72 20 73 6f 20 74 68 61 74  in order so that
180c0 20 61 20 73 63 61 6e 0a 20 20 2a 2a 20 6f 66 20   a scan.  ** of 
180c1 74 68 65 20 74 61 62 6c 65 20 69 73 20 61 20 6c  the table is a l
180c2 69 6e 65 61 72 20 73 63 61 6e 20 74 68 72 6f 75  inear scan throu
180c3 67 68 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68  gh the file.  Th
180c4 61 74 0a 20 20 2a 2a 20 69 6e 20 74 75 72 6e 20  at.  ** in turn 
180c5 68 65 6c 70 73 20 74 68 65 20 6f 70 65 72 61 74  helps the operat
180c6 69 6e 67 20 73 79 73 74 65 6d 20 74 6f 20 64 65  ing system to de
180c7 6c 69 76 65 72 20 70 61 67 65 73 0a 20 20 2a 2a  liver pages.  **
180c8 20 66 72 6f 6d 20 74 68 65 20 64 69 73 6b 20 6d   from the disk m
180c9 6f 72 65 20 72 61 70 69 64 6c 79 2e 0a 20 20 2a  ore rapidly..  *
180ca 2a 0a 20 20 2a 2a 20 41 6e 20 4f 28 6e 5e 32 29  *.  ** An O(n^2)
180cb 20 69 6e 73 65 72 74 69 6f 6e 20 73 6f 72 74 20   insertion sort 
180cc 61 6c 67 6f 72 69 74 68 6d 20 69 73 20 75 73 65  algorithm is use
180cd 64 2c 20 62 75 74 20 73 69 6e 63 65 0a 20 20 2a  d, but since.  *
180ce 2a 20 6e 20 69 73 20 6e 65 76 65 72 20 6d 6f 72  * n is never mor
180cf 65 20 74 68 61 6e 20 4e 42 20 28 61 20 73 6d 61  e than NB (a sma
180d0 6c 6c 20 63 6f 6e 73 74 61 6e 74 29 2c 20 74 68  ll constant), th
180d1 61 74 20 73 68 6f 75 6c 64 0a 20 20 2a 2a 20 6e  at should.  ** n
180d2 6f 74 20 62 65 20 61 20 70 72 6f 62 6c 65 6d 2e  ot be a problem.
180d3 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e 20  .  **.  ** When 
180d4 4e 42 3d 3d 33 2c 20 74 68 69 73 20 6f 6e 65 20  NB==3, this one 
180d5 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6d 61 6b  optimization mak
180d6 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  es the database.
180d7 20 20 2a 2a 20 61 62 6f 75 74 20 32 35 25 20 66    ** about 25% f
180d8 61 73 74 65 72 20 66 6f 72 20 6c 61 72 67 65 20  aster for large 
180d9 69 6e 73 65 72 74 69 6f 6e 73 20 61 6e 64 20 64  insertions and d
180da 65 6c 65 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20  eletions..  */. 
180db 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6b 2d 31 3b   for(i=0; i<k-1;
180dc 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6d   i++){.    int m
180dd 69 6e 56 20 3d 20 61 70 4e 65 77 5b 69 5d 2d 3e  inV = apNew[i]->
180de 70 67 6e 6f 3b 0a 20 20 20 20 69 6e 74 20 6d 69  pgno;.    int mi
180df 6e 49 20 3d 20 69 3b 0a 20 20 20 20 66 6f 72 28  nI = i;.    for(
180e0 6a 3d 69 2b 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b 29  j=i+1; j<k; j++)
180e1 7b 0a 20 20 20 20 20 20 69 66 28 20 61 70 4e 65  {.      if( apNe
180e2 77 5b 6a 5d 2d 3e 70 67 6e 6f 3c 28 75 6e 73 69  w[j]->pgno<(unsi
180e3 67 6e 65 64 29 6d 69 6e 56 20 29 7b 0a 20 20 20  gned)minV ){.   
180e4 20 20 20 20 20 6d 69 6e 49 20 3d 20 6a 3b 0a 20       minI = j;. 
180e5 20 20 20 20 20 20 20 6d 69 6e 56 20 3d 20 61 70         minV = ap
180e6 4e 65 77 5b 6a 5d 2d 3e 70 67 6e 6f 3b 0a 20 20  New[j]->pgno;.  
180e7 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
180e8 69 66 28 20 6d 69 6e 49 3e 69 20 29 7b 0a 20 20  if( minI>i ){.  
180e9 20 20 20 20 69 6e 74 20 74 3b 0a 20 20 20 20 20      int t;.     
180ea 20 4d 65 6d 50 61 67 65 20 2a 70 54 3b 0a 20 20   MemPage *pT;.  
180eb 20 20 20 20 74 20 3d 20 61 70 4e 65 77 5b 69 5d      t = apNew[i]
180ec 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 20 20 70 54  ->pgno;.      pT
180ed 20 3d 20 61 70 4e 65 77 5b 69 5d 3b 0a 20 20 20   = apNew[i];.   
180ee 20 20 20 61 70 4e 65 77 5b 69 5d 20 3d 20 61 70     apNew[i] = ap
180ef 4e 65 77 5b 6d 69 6e 49 5d 3b 0a 20 20 20 20 20  New[minI];.     
180f0 20 61 70 4e 65 77 5b 6d 69 6e 49 5d 20 3d 20 70   apNew[minI] = p
180f1 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 54  T;.    }.  }.  T
180f2 52 41 43 45 28 28 22 6e 65 77 3a 20 25 64 28 25  RACE(("new: %d(%
180f3 64 29 20 25 64 28 25 64 29 20 25 64 28 25 64 29  d) %d(%d) %d(%d)
180f4 20 25 64 28 25 64 29 20 25 64 28 25 64 29 5c 6e   %d(%d) %d(%d)\n
180f5 22 2c 0a 20 20 20 20 61 70 4e 65 77 5b 30 5d 2d  ",.    apNew[0]-
180f6 3e 70 67 6e 6f 2c 20 73 7a 4e 65 77 5b 30 5d 2c  >pgno, szNew[0],
180f7 0a 20 20 20 20 6e 4e 65 77 3e 3d 32 20 3f 20 61  .    nNew>=2 ? a
180f8 70 4e 65 77 5b 31 5d 2d 3e 70 67 6e 6f 20 3a 20  pNew[1]->pgno : 
180f9 30 2c 20 6e 4e 65 77 3e 3d 32 20 3f 20 73 7a 4e  0, nNew>=2 ? szN
180fa 65 77 5b 31 5d 20 3a 20 30 2c 0a 20 20 20 20 6e  ew[1] : 0,.    n
180fb 4e 65 77 3e 3d 33 20 3f 20 61 70 4e 65 77 5b 32  New>=3 ? apNew[2
180fc 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65  ]->pgno : 0, nNe
180fd 77 3e 3d 33 20 3f 20 73 7a 4e 65 77 5b 32 5d 20  w>=3 ? szNew[2] 
180fe 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 34  : 0,.    nNew>=4
180ff 20 3f 20 61 70 4e 65 77 5b 33 5d 2d 3e 70 67 6e   ? apNew[3]->pgn
18100 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 34 20 3f  o : 0, nNew>=4 ?
18101 20 73 7a 4e 65 77 5b 33 5d 20 3a 20 30 2c 0a 20   szNew[3] : 0,. 
18102 20 20 20 6e 4e 65 77 3e 3d 35 20 3f 20 61 70 4e     nNew>=5 ? apN
18103 65 77 5b 34 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c  ew[4]->pgno : 0,
18104 20 6e 4e 65 77 3e 3d 35 20 3f 20 73 7a 4e 65 77   nNew>=5 ? szNew
18105 5b 34 5d 20 3a 20 30 29 29 3b 0a 0a 20 20 61 73  [4] : 0));..  as
18106 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
18107 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50  erIswriteable(pP
18108 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 20  arent->pDbPage) 
18109 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 70 52  );.  put4byte(pR
1810a 69 67 68 74 2c 20 61 70 4e 65 77 5b 6e 4e 65 77  ight, apNew[nNew
1810b 2d 31 5d 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 2f  -1]->pgno);..  /
1810c 2a 0a 20 20 2a 2a 20 45 76 65 6e 6c 79 20 64 69  *.  ** Evenly di
1810d 73 74 72 69 62 75 74 65 20 74 68 65 20 64 61 74  stribute the dat
1810e 61 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 63  a in apCell[] ac
1810f 72 6f 73 73 20 74 68 65 20 6e 65 77 20 70 61 67  ross the new pag
18110 65 73 2e 0a 20 20 2a 2a 20 49 6e 73 65 72 74 20  es..  ** Insert 
18111 64 69 76 69 64 65 72 20 63 65 6c 6c 73 20 69 6e  divider cells in
18112 74 6f 20 70 50 61 72 65 6e 74 20 61 73 20 6e 65  to pParent as ne
18113 63 65 73 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20  cessary..  */.  
18114 6a 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30  j = 0;.  for(i=0
18115 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a  ; i<nNew; i++){.
18116 20 20 20 20 2f 2a 20 41 73 73 65 6d 62 6c 65 20      /* Assemble 
18117 74 68 65 20 6e 65 77 20 73 69 62 6c 69 6e 67 20  the new sibling 
18118 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d  page. */.    Mem
18119 50 61 67 65 20 2a 70 4e 65 77 20 3d 20 61 70 4e  Page *pNew = apN
1811a 65 77 5b 69 5d 3b 0a 20 20 20 20 61 73 73 65 72  ew[i];.    asser
1811b 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29  t( j<nMaxCells )
1811c 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70  ;.    zeroPage(p
1811d 4e 65 77 2c 20 70 61 67 65 46 6c 61 67 73 29 3b  New, pageFlags);
1811e 0a 20 20 20 20 61 73 73 65 6d 62 6c 65 50 61 67  .    assemblePag
1811f 65 28 70 4e 65 77 2c 20 63 6e 74 4e 65 77 5b 69  e(pNew, cntNew[i
18120 5d 2d 6a 2c 20 26 61 70 43 65 6c 6c 5b 6a 5d 2c  ]-j, &apCell[j],
18121 20 26 73 7a 43 65 6c 6c 5b 6a 5d 29 3b 0a 20 20   &szCell[j]);.  
18122 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e    assert( pNew->
18123 6e 43 65 6c 6c 3e 30 20 7c 7c 20 28 6e 4e 65 77  nCell>0 || (nNew
18124 3d 3d 31 20 26 26 20 63 6e 74 4e 65 77 5b 30 5d  ==1 && cntNew[0]
18125 3d 3d 30 29 20 29 3b 0a 20 20 20 20 61 73 73 65  ==0) );.    asse
18126 72 74 28 20 70 4e 65 77 2d 3e 6e 4f 76 65 72 66  rt( pNew->nOverf
18127 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 6a  low==0 );..    j
18128 20 3d 20 63 6e 74 4e 65 77 5b 69 5d 3b 0a 0a 20   = cntNew[i];.. 
18129 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 69 62     /* If the sib
1812a 6c 69 6e 67 20 70 61 67 65 20 61 73 73 65 6d 62  ling page assemb
1812b 6c 65 64 20 61 62 6f 76 65 20 77 61 73 20 6e 6f  led above was no
1812c 74 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  t the right-most
1812d 20 73 69 62 6c 69 6e 67 2c 0a 20 20 20 20 2a 2a   sibling,.    **
1812e 20 69 6e 73 65 72 74 20 61 20 64 69 76 69 64 65   insert a divide
1812f 72 20 63 65 6c 6c 20 69 6e 74 6f 20 74 68 65 20  r cell into the 
18130 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 20  parent page..   
18131 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
18132 69 3c 6e 4e 65 77 2d 31 20 7c 7c 20 6a 3d 3d 6e  i<nNew-1 || j==n
18133 43 65 6c 6c 20 29 3b 0a 20 20 20 20 69 66 28 20  Cell );.    if( 
18134 6a 3c 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20  j<nCell ){.     
18135 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20   u8 *pCell;.    
18136 20 20 75 38 20 2a 70 54 65 6d 70 3b 0a 20 20 20    u8 *pTemp;.   
18137 20 20 20 69 6e 74 20 73 7a 3b 0a 0a 20 20 20 20     int sz;..    
18138 20 20 61 73 73 65 72 74 28 20 6a 3c 6e 4d 61 78    assert( j<nMax
18139 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 70  Cells );.      p
1813a 43 65 6c 6c 20 3d 20 61 70 43 65 6c 6c 5b 6a 5d  Cell = apCell[j]
1813b 3b 0a 20 20 20 20 20 20 73 7a 20 3d 20 73 7a 43  ;.      sz = szC
1813c 65 6c 6c 5b 6a 5d 20 2b 20 6c 65 61 66 43 6f 72  ell[j] + leafCor
1813d 72 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20 70  rection;.      p
1813e 54 65 6d 70 20 3d 20 26 61 4f 76 66 6c 53 70 61  Temp = &aOvflSpa
1813f 63 65 5b 69 4f 76 66 6c 53 70 61 63 65 5d 3b 0a  ce[iOvflSpace];.
18140 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 77 2d        if( !pNew-
18141 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 20  >leaf ){.       
18142 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 2d 3e 61   memcpy(&pNew->a
18143 44 61 74 61 5b 38 5d 2c 20 70 43 65 6c 6c 2c 20  Data[8], pCell, 
18144 34 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  4);.      }else 
18145 69 66 28 20 6c 65 61 66 44 61 74 61 20 29 7b 0a  if( leafData ){.
18146 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
18147 65 20 74 72 65 65 20 69 73 20 61 20 6c 65 61 66  e tree is a leaf
18148 2d 64 61 74 61 20 74 72 65 65 2c 20 61 6e 64 20  -data tree, and 
18149 74 68 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65  the siblings are
1814a 20 6c 65 61 76 65 73 2c 20 0a 20 20 20 20 20 20   leaves, .      
1814b 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 72 65 20    ** then there 
1814c 69 73 20 6e 6f 20 64 69 76 69 64 65 72 20 63 65  is no divider ce
1814d 6c 6c 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 2e 20  ll in apCell[]. 
1814e 49 6e 73 74 65 61 64 2c 20 74 68 65 20 64 69 76  Instead, the div
1814f 69 64 65 72 20 0a 20 20 20 20 20 20 20 20 2a 2a  ider .        **
18150 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73 20 6f   cell consists o
18151 66 20 74 68 65 20 69 6e 74 65 67 65 72 20 6b 65  f the integer ke
18152 79 20 66 6f 72 20 74 68 65 20 72 69 67 68 74 2d  y for the right-
18153 6d 6f 73 74 20 63 65 6c 6c 20 6f 66 20 0a 20 20  most cell of .  
18154 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73 69 62        ** the sib
18155 6c 69 6e 67 2d 70 61 67 65 20 61 73 73 65 6d 62  ling-page assemb
18156 6c 65 64 20 61 62 6f 76 65 20 6f 6e 6c 79 2e 0a  led above only..
18157 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
18158 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f     CellInfo info
18159 3b 0a 20 20 20 20 20 20 20 20 6a 2d 2d 3b 0a 20  ;.        j--;. 
1815a 20 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73         btreePars
1815b 65 43 65 6c 6c 50 74 72 28 70 4e 65 77 2c 20 61  eCellPtr(pNew, a
1815c 70 43 65 6c 6c 5b 6a 5d 2c 20 26 69 6e 66 6f 29  pCell[j], &info)
1815d 3b 0a 20 20 20 20 20 20 20 20 70 43 65 6c 6c 20  ;.        pCell 
1815e 3d 20 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 20  = pTemp;.       
1815f 20 73 7a 20 3d 20 34 20 2b 20 70 75 74 56 61 72   sz = 4 + putVar
18160 69 6e 74 28 26 70 43 65 6c 6c 5b 34 5d 2c 20 69  int(&pCell[4], i
18161 6e 66 6f 2e 6e 4b 65 79 29 3b 0a 20 20 20 20 20  nfo.nKey);.     
18162 20 20 20 70 54 65 6d 70 20 3d 20 30 3b 0a 20 20     pTemp = 0;.  
18163 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
18164 20 20 20 70 43 65 6c 6c 20 2d 3d 20 34 3b 0a 20     pCell -= 4;. 
18165 20 20 20 20 20 20 20 2f 2a 20 4f 62 73 63 75 72         /* Obscur
18166 65 20 63 61 73 65 20 66 6f 72 20 6e 6f 6e 2d 6c  e case for non-l
18167 65 61 66 2d 64 61 74 61 20 74 72 65 65 73 3a 20  eaf-data trees: 
18168 49 66 20 74 68 65 20 63 65 6c 6c 20 61 74 20 70  If the cell at p
18169 43 65 6c 6c 20 77 61 73 0a 20 20 20 20 20 20 20  Cell was.       
1816a 20 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73   ** previously s
1816b 74 6f 72 65 64 20 6f 6e 20 61 20 6c 65 61 66 20  tored on a leaf 
1816c 6e 6f 64 65 2c 20 61 6e 64 20 69 74 73 20 72 65  node, and its re
1816d 70 6f 72 74 65 64 20 73 69 7a 65 20 77 61 73 20  ported size was 
1816e 34 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 79 74  4.        ** byt
1816f 65 73 2c 20 74 68 65 6e 20 69 74 20 6d 61 79 20  es, then it may 
18170 61 63 74 75 61 6c 6c 79 20 62 65 20 73 6d 61 6c  actually be smal
18171 6c 65 72 20 74 68 61 6e 20 74 68 69 73 20 0a 20  ler than this . 
18172 20 20 20 20 20 20 20 2a 2a 20 28 73 65 65 20 62         ** (see b
18173 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72  treeParseCellPtr
18174 28 29 2c 20 34 20 62 79 74 65 73 20 69 73 20 74  (), 4 bytes is t
18175 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69 7a 65 20  he minimum size 
18176 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e  of.        ** an
18177 79 20 63 65 6c 6c 29 2e 20 42 75 74 20 69 74 20  y cell). But it 
18178 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20  is important to 
18179 70 61 73 73 20 74 68 65 20 63 6f 72 72 65 63 74  pass the correct
1817a 20 73 69 7a 65 20 74 6f 20 0a 20 20 20 20 20 20   size to .      
1817b 20 20 2a 2a 20 69 6e 73 65 72 74 43 65 6c 6c 28    ** insertCell(
1817c 29 2c 20 73 6f 20 72 65 70 61 72 73 65 20 74 68  ), so reparse th
1817d 65 20 63 65 6c 6c 20 6e 6f 77 2e 0a 20 20 20 20  e cell now..    
1817e 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
1817f 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 69 73  * Note that this
18180 20 63 61 6e 20 6e 65 76 65 72 20 68 61 70 70 65   can never happe
18181 6e 20 69 6e 20 61 6e 20 53 51 4c 69 74 65 20 64  n in an SQLite d
18182 61 74 61 20 66 69 6c 65 2c 20 61 73 20 61 6c 6c  ata file, as all
18183 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65 6c 6c  .        ** cell
18184 73 20 61 72 65 20 61 74 20 6c 65 61 73 74 20 34  s are at least 4
18185 20 62 79 74 65 73 2e 20 49 74 20 6f 6e 6c 79 20   bytes. It only 
18186 68 61 70 70 65 6e 73 20 69 6e 20 62 2d 74 72 65  happens in b-tre
18187 65 73 20 75 73 65 64 0a 20 20 20 20 20 20 20 20  es used.        
18188 2a 2a 20 74 6f 20 65 76 61 6c 75 61 74 65 20 22  ** to evaluate "
18189 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22  IN (SELECT ...)"
1818a 20 61 6e 64 20 73 69 6d 69 6c 61 72 20 63 6c 61   and similar cla
1818b 75 73 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f  uses..        */
1818c 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 7a 43  .        if( szC
1818d 65 6c 6c 5b 6a 5d 3d 3d 34 20 29 7b 0a 20 20 20  ell[j]==4 ){.   
1818e 20 20 20 20 20 20 20 61 73 73 65 72 74 28 6c 65         assert(le
1818f 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 29  afCorrection==4)
18190 3b 0a 20 20 20 20 20 20 20 20 20 20 73 7a 20 3d  ;.          sz =
18191 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61   cellSizePtr(pPa
18192 72 65 6e 74 2c 20 70 43 65 6c 6c 29 3b 0a 20 20  rent, pCell);.  
18193 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
18194 20 20 20 20 20 20 69 4f 76 66 6c 53 70 61 63 65        iOvflSpace
18195 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20 61 73   += sz;.      as
18196 73 65 72 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 70  sert( sz<=pBt->p
18197 61 67 65 53 69 7a 65 2f 34 20 29 3b 0a 20 20 20  ageSize/4 );.   
18198 20 20 20 61 73 73 65 72 74 28 20 69 4f 76 66 6c     assert( iOvfl
18199 53 70 61 63 65 3c 3d 70 42 74 2d 3e 70 61 67 65  Space<=pBt->page
1819a 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20 69 6e  Size );.      in
1819b 73 65 72 74 43 65 6c 6c 28 70 50 61 72 65 6e 74  sertCell(pParent
1819c 2c 20 6e 78 44 69 76 2c 20 70 43 65 6c 6c 2c 20  , nxDiv, pCell, 
1819d 73 7a 2c 20 70 54 65 6d 70 2c 20 70 4e 65 77 2d  sz, pTemp, pNew-
1819e 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20  >pgno, &rc);.   
1819f 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
181a0 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 62 61 6c 61  E_OK ) goto bala
181a1 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  nce_cleanup;.   
181a2 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
181a3 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
181a4 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50  le(pParent->pDbP
181a5 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20 6a  age) );..      j
181a6 2b 2b 3b 0a 20 20 20 20 20 20 6e 78 44 69 76 2b  ++;.      nxDiv+
181a7 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61  +;.    }.  }.  a
181a8 73 73 65 72 74 28 20 6a 3d 3d 6e 43 65 6c 6c 20  ssert( j==nCell 
181a9 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 4f 6c  );.  assert( nOl
181aa 64 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  d>0 );.  assert(
181ab 20 6e 4e 65 77 3e 30 20 29 3b 0a 20 20 69 66 28   nNew>0 );.  if(
181ac 20 28 70 61 67 65 46 6c 61 67 73 20 26 20 50 54   (pageFlags & PT
181ad 46 5f 4c 45 41 46 29 3d 3d 30 20 29 7b 0a 20 20  F_LEAF)==0 ){.  
181ae 20 20 75 38 20 2a 7a 43 68 69 6c 64 20 3d 20 26    u8 *zChild = &
181af 61 70 43 6f 70 79 5b 6e 4f 6c 64 2d 31 5d 2d 3e  apCopy[nOld-1]->
181b0 61 44 61 74 61 5b 38 5d 3b 0a 20 20 20 20 6d 65  aData[8];.    me
181b1 6d 63 70 79 28 26 61 70 4e 65 77 5b 6e 4e 65 77  mcpy(&apNew[nNew
181b2 2d 31 5d 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 7a  -1]->aData[8], z
181b3 43 68 69 6c 64 2c 20 34 29 3b 0a 20 20 7d 0a 0a  Child, 4);.  }..
181b4 20 20 69 66 28 20 69 73 52 6f 6f 74 20 26 26 20    if( isRoot && 
181b5 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d  pParent->nCell==
181b6 30 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 68 64  0 && pParent->hd
181b7 72 4f 66 66 73 65 74 3c 3d 61 70 4e 65 77 5b 30  rOffset<=apNew[0
181b8 5d 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 20 20 20  ]->nFree ){.    
181b9 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65  /* The root page
181ba 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 20 6e   of the b-tree n
181bb 6f 77 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 63  ow contains no c
181bc 65 6c 6c 73 2e 20 54 68 65 20 6f 6e 6c 79 20 73  ells. The only s
181bd 69 62 6c 69 6e 67 0a 20 20 20 20 2a 2a 20 70 61  ibling.    ** pa
181be 67 65 20 69 73 20 74 68 65 20 72 69 67 68 74 2d  ge is the right-
181bf 63 68 69 6c 64 20 6f 66 20 74 68 65 20 70 61 72  child of the par
181c0 65 6e 74 2e 20 43 6f 70 79 20 74 68 65 20 63 6f  ent. Copy the co
181c1 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a 20 20  ntents of the.  
181c2 20 20 2a 2a 20 63 68 69 6c 64 20 70 61 67 65 20    ** child page 
181c3 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 2c  into the parent,
181c4 20 64 65 63 72 65 61 73 69 6e 67 20 74 68 65 20   decreasing the 
181c5 6f 76 65 72 61 6c 6c 20 68 65 69 67 68 74 20 6f  overall height o
181c6 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 62 2d 74  f the.    ** b-t
181c7 72 65 65 20 73 74 72 75 63 74 75 72 65 20 62 79  ree structure by
181c8 20 6f 6e 65 2e 20 54 68 69 73 20 69 73 20 64 65   one. This is de
181c9 73 63 72 69 62 65 64 20 61 73 20 74 68 65 20 22  scribed as the "
181ca 62 61 6c 61 6e 63 65 2d 73 68 61 6c 6c 6f 77 65  balance-shallowe
181cb 72 22 0a 20 20 20 20 2a 2a 20 73 75 62 2d 61 6c  r".    ** sub-al
181cc 67 6f 72 69 74 68 6d 20 69 6e 20 73 6f 6d 65 20  gorithm in some 
181cd 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 2e 0a 20  documentation.. 
181ce 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20     **.    ** If 
181cf 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f 2d  this is an auto-
181d0 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 2c  vacuum database,
181d1 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 63 6f 70   the call to cop
181d2 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 29 20 0a  yNodeContent() .
181d3 20 20 20 20 2a 2a 20 73 65 74 73 20 61 6c 6c 20      ** sets all 
181d4 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72  pointer-map entr
181d5 69 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  ies correspondin
181d6 67 20 74 6f 20 64 61 74 61 62 61 73 65 20 69 6d  g to database im
181d7 61 67 65 20 70 61 67 65 73 20 0a 20 20 20 20 2a  age pages .    *
181d8 2a 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20  * for which the 
181d9 70 6f 69 6e 74 65 72 20 69 73 20 73 74 6f 72 65  pointer is store
181da 64 20 77 69 74 68 69 6e 20 74 68 65 20 63 6f 6e  d within the con
181db 74 65 6e 74 20 62 65 69 6e 67 20 63 6f 70 69 65  tent being copie
181dc 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  d..    **.    **
181dd 20 54 68 65 20 73 65 63 6f 6e 64 20 61 73 73 65   The second asse
181de 72 74 20 62 65 6c 6f 77 20 76 65 72 69 66 69 65  rt below verifie
181df 73 20 74 68 61 74 20 74 68 65 20 63 68 69 6c 64  s that the child
181e0 20 70 61 67 65 20 69 73 20 64 65 66 72 61 67 6d   page is defragm
181e1 65 6e 74 65 64 0a 20 20 20 20 2a 2a 20 28 69 74  ented.    ** (it
181e2 20 6d 75 73 74 20 62 65 2c 20 61 73 20 69 74 20   must be, as it 
181e3 77 61 73 20 6a 75 73 74 20 72 65 63 6f 6e 73 74  was just reconst
181e4 72 75 63 74 65 64 20 75 73 69 6e 67 20 61 73 73  ructed using ass
181e5 65 6d 62 6c 65 50 61 67 65 28 29 29 2e 20 54 68  emblePage()). Th
181e6 69 73 0a 20 20 20 20 2a 2a 20 69 73 20 69 6d 70  is.    ** is imp
181e7 6f 72 74 61 6e 74 20 69 66 20 74 68 65 20 70 61  ortant if the pa
181e8 72 65 6e 74 20 70 61 67 65 20 68 61 70 70 65 6e  rent page happen
181e9 73 20 74 6f 20 62 65 20 70 61 67 65 20 31 20 6f  s to be page 1 o
181ea 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20  f the database. 
181eb 20 20 20 2a 2a 20 69 6d 61 67 65 2e 20 20 2a 2f     ** image.  */
181ec 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 4e 65  .    assert( nNe
181ed 77 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65  w==1 );.    asse
181ee 72 74 28 20 61 70 4e 65 77 5b 30 5d 2d 3e 6e 46  rt( apNew[0]->nF
181ef 72 65 65 20 3d 3d 20 0a 20 20 20 20 20 20 20 20  ree == .        
181f0 28 67 65 74 32 62 79 74 65 28 26 61 70 4e 65 77  (get2byte(&apNew
181f1 5b 30 5d 2d 3e 61 44 61 74 61 5b 35 5d 29 2d 61  [0]->aData[5])-a
181f2 70 4e 65 77 5b 30 5d 2d 3e 63 65 6c 6c 4f 66 66  pNew[0]->cellOff
181f3 73 65 74 2d 61 70 4e 65 77 5b 30 5d 2d 3e 6e 43  set-apNew[0]->nC
181f4 65 6c 6c 2a 32 29 20 0a 20 20 20 20 29 3b 0a 20  ell*2) .    );. 
181f5 20 20 20 63 6f 70 79 4e 6f 64 65 43 6f 6e 74 65     copyNodeConte
181f6 6e 74 28 61 70 4e 65 77 5b 30 5d 2c 20 70 50 61  nt(apNew[0], pPa
181f7 72 65 6e 74 2c 20 26 72 63 29 3b 0a 20 20 20 20  rent, &rc);.    
181f8 66 72 65 65 50 61 67 65 28 61 70 4e 65 77 5b 30  freePage(apNew[0
181f9 5d 2c 20 26 72 63 29 3b 0a 20 20 7d 65 6c 73 65  ], &rc);.  }else
181fa 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55   if( ISAUTOVACUU
181fb 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 46 69 78 20  M ){.    /* Fix 
181fc 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  the pointer-map 
181fd 65 6e 74 72 69 65 73 20 66 6f 72 20 61 6c 6c 20  entries for all 
181fe 74 68 65 20 63 65 6c 6c 73 20 74 68 61 74 20 77  the cells that w
181ff 65 72 65 20 73 68 69 66 74 65 64 20 61 72 6f 75  ere shifted arou
18200 6e 64 2e 20 0a 20 20 20 20 2a 2a 20 54 68 65 72  nd. .    ** Ther
18201 65 20 61 72 65 20 73 65 76 65 72 61 6c 20 64 69  e are several di
18202 66 66 65 72 65 6e 74 20 74 79 70 65 73 20 6f 66  fferent types of
18203 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74   pointer-map ent
18204 72 69 65 73 20 74 68 61 74 20 6e 65 65 64 20 74  ries that need t
18205 6f 0a 20 20 20 20 2a 2a 20 62 65 20 64 65 61 6c  o.    ** be deal
18206 74 20 77 69 74 68 20 62 79 20 74 68 69 73 20 72  t with by this r
18207 6f 75 74 69 6e 65 2e 20 53 6f 6d 65 20 6f 66 20  outine. Some of 
18208 74 68 65 73 65 20 68 61 76 65 20 62 65 65 6e 20  these have been 
18209 73 65 74 20 61 6c 72 65 61 64 79 2c 20 62 75 74  set already, but
1820a 0a 20 20 20 20 2a 2a 20 6d 61 6e 79 20 68 61 76  .    ** many hav
1820b 65 20 6e 6f 74 2e 20 54 68 65 20 66 6f 6c 6c 6f  e not. The follo
1820c 77 69 6e 67 20 69 73 20 61 20 73 75 6d 6d 61 72  wing is a summar
1820d 79 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  y:.    **.    **
1820e 20 20 20 31 29 20 54 68 65 20 65 6e 74 72 69 65     1) The entrie
1820f 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
18210 68 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70 61  h new sibling pa
18211 67 65 73 20 74 68 61 74 20 77 65 72 65 20 6e 6f  ges that were no
18212 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 73 69  t.    **      si
18213 62 6c 69 6e 67 73 20 77 68 65 6e 20 74 68 69 73  blings when this
18214 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 61   function was ca
18215 6c 6c 65 64 2e 20 54 68 65 73 65 20 68 61 76 65  lled. These have
18216 20 61 6c 72 65 61 64 79 0a 20 20 20 20 2a 2a 20   already.    ** 
18217 20 20 20 20 20 62 65 65 6e 20 73 65 74 2e 20 57       been set. W
18218 65 20 64 6f 6e 27 74 20 6e 65 65 64 20 74 6f 20  e don't need to 
18219 77 6f 72 72 79 20 61 62 6f 75 74 20 6f 6c 64 20  worry about old 
1821a 73 69 62 6c 69 6e 67 73 20 74 68 61 74 20 77 65  siblings that we
1821b 72 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 6d  re.    **      m
1821c 6f 76 65 64 20 74 6f 20 74 68 65 20 66 72 65 65  oved to the free
1821d 2d 6c 69 73 74 20 2d 20 74 68 65 20 66 72 65 65  -list - the free
1821e 50 61 67 65 28 29 20 63 6f 64 65 20 68 61 73 20  Page() code has 
1821f 74 61 6b 65 6e 20 63 61 72 65 0a 20 20 20 20 2a  taken care.    *
18220 2a 20 20 20 20 20 20 6f 66 20 74 68 6f 73 65 2e  *      of those.
18221 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
18222 20 32 29 20 54 68 65 20 70 6f 69 6e 74 65 72 2d   2) The pointer-
18223 6d 61 70 20 65 6e 74 72 69 65 73 20 61 73 73 6f  map entries asso
18224 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
18225 66 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 0a 20  first overflow. 
18226 20 20 20 2a 2a 20 20 20 20 20 20 70 61 67 65 20     **      page 
18227 69 6e 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20  in any overflow 
18228 63 68 61 69 6e 73 20 75 73 65 64 20 62 79 20 6e  chains used by n
18229 65 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73  ew divider cells
1822a 2e 20 54 68 65 73 65 20 0a 20 20 20 20 2a 2a 20  . These .    ** 
1822b 20 20 20 20 20 68 61 76 65 20 61 6c 73 6f 20 61       have also a
1822c 6c 72 65 61 64 79 20 62 65 65 6e 20 74 61 6b 65  lready been take
1822d 6e 20 63 61 72 65 20 6f 66 20 62 79 20 74 68 65  n care of by the
1822e 20 69 6e 73 65 72 74 43 65 6c 6c 28 29 20 63 6f   insertCell() co
1822f 64 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  de..    **.    *
18230 2a 20 20 20 33 29 20 49 66 20 74 68 65 20 73 69  *   3) If the si
18231 62 6c 69 6e 67 20 70 61 67 65 73 20 61 72 65 20  bling pages are 
18232 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74 68 65 6e  not leaves, then
18233 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 73   the child pages
18234 20 6f 66 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   of.    **      
18235 63 65 6c 6c 73 20 73 74 6f 72 65 64 20 6f 6e 20  cells stored on 
18236 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65  the sibling page
18237 73 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65  s may need to be
18238 20 75 70 64 61 74 65 64 2e 0a 20 20 20 20 2a 2a   updated..    **
18239 0a 20 20 20 20 2a 2a 20 20 20 34 29 20 49 66 20  .    **   4) If 
1823a 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65  the sibling page
1823b 73 20 61 72 65 20 6e 6f 74 20 69 6e 74 65 72 6e  s are not intern
1823c 61 6c 20 69 6e 74 6b 65 79 20 6e 6f 64 65 73 2c  al intkey nodes,
1823d 20 74 68 65 6e 20 61 6e 79 0a 20 20 20 20 2a 2a   then any.    **
1823e 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70        overflow p
1823f 61 67 65 73 20 75 73 65 64 20 62 79 20 74 68 65  ages used by the
18240 73 65 20 63 65 6c 6c 73 20 6d 61 79 20 6e 65 65  se cells may nee
18241 64 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 0a  d to be updated.
18242 20 20 20 20 2a 2a 20 20 20 20 20 20 28 69 6e 74      **      (int
18243 65 72 6e 61 6c 20 69 6e 74 6b 65 79 20 6e 6f 64  ernal intkey nod
18244 65 73 20 6e 65 76 65 72 20 63 6f 6e 74 61 69 6e  es never contain
18245 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 6f 76 65   pointers to ove
18246 72 66 6c 6f 77 20 70 61 67 65 73 29 2e 0a 20 20  rflow pages)..  
18247 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 35 29    **.    **   5)
18248 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 20   If the sibling 
18249 70 61 67 65 73 20 61 72 65 20 6e 6f 74 20 6c 65  pages are not le
1824a 61 76 65 73 2c 20 74 68 65 6e 20 74 68 65 20 70  aves, then the p
1824b 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 2a  ointer-map.    *
1824c 2a 20 20 20 20 20 20 65 6e 74 72 69 65 73 20 66  *      entries f
1824d 6f 72 20 74 68 65 20 72 69 67 68 74 2d 63 68 69  or the right-chi
1824e 6c 64 20 70 61 67 65 73 20 6f 66 20 65 61 63 68  ld pages of each
1824f 20 73 69 62 6c 69 6e 67 20 6d 61 79 20 6e 65 65   sibling may nee
18250 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 74 6f  d.    **      to
18251 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20 20 20   be updated..   
18252 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 61 73 65 73   **.    ** Cases
18253 20 31 20 61 6e 64 20 32 20 61 72 65 20 64 65 61   1 and 2 are dea
18254 6c 74 20 77 69 74 68 20 61 62 6f 76 65 20 62 79  lt with above by
18255 20 6f 74 68 65 72 20 63 6f 64 65 2e 20 54 68 65   other code. The
18256 20 6e 65 78 74 0a 20 20 20 20 2a 2a 20 62 6c 6f   next.    ** blo
18257 63 6b 20 64 65 61 6c 73 20 77 69 74 68 20 63 61  ck deals with ca
18258 73 65 73 20 33 20 61 6e 64 20 34 20 61 6e 64 20  ses 3 and 4 and 
18259 74 68 65 20 6f 6e 65 20 61 66 74 65 72 20 74 68  the one after th
1825a 61 74 2c 20 63 61 73 65 20 35 2e 20 53 69 6e 63  at, case 5. Sinc
1825b 65 0a 20 20 20 20 2a 2a 20 73 65 74 74 69 6e 67  e.    ** setting
1825c 20 61 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65   a pointer map e
1825d 6e 74 72 79 20 69 73 20 61 20 72 65 6c 61 74 69  ntry is a relati
1825e 76 65 6c 79 20 65 78 70 65 6e 73 69 76 65 20 6f  vely expensive o
1825f 70 65 72 61 74 69 6f 6e 2c 20 74 68 69 73 0a 20  peration, this. 
18260 20 20 20 2a 2a 20 63 6f 64 65 20 6f 6e 6c 79 20     ** code only 
18261 73 65 74 73 20 70 6f 69 6e 74 65 72 20 6d 61 70  sets pointer map
18262 20 65 6e 74 72 69 65 73 20 66 6f 72 20 63 68 69   entries for chi
18263 6c 64 20 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70  ld or overflow p
18264 61 67 65 73 20 74 68 61 74 20 68 61 76 65 0a 20  ages that have. 
18265 20 20 20 2a 2a 20 61 63 74 75 61 6c 6c 79 20 6d     ** actually m
18266 6f 76 65 64 20 62 65 74 77 65 65 6e 20 70 61 67  oved between pag
18267 65 73 2e 20 20 2a 2f 0a 20 20 20 20 4d 65 6d 50  es.  */.    MemP
18268 61 67 65 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65  age *pNew = apNe
18269 77 5b 30 5d 3b 0a 20 20 20 20 4d 65 6d 50 61 67  w[0];.    MemPag
1826a 65 20 2a 70 4f 6c 64 20 3d 20 61 70 43 6f 70 79  e *pOld = apCopy
1826b 5b 30 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 4f 76  [0];.    int nOv
1826c 65 72 66 6c 6f 77 20 3d 20 70 4f 6c 64 2d 3e 6e  erflow = pOld->n
1826d 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 69 6e  Overflow;.    in
1826e 74 20 69 4e 65 78 74 4f 6c 64 20 3d 20 70 4f 6c  t iNextOld = pOl
1826f 64 2d 3e 6e 43 65 6c 6c 20 2b 20 6e 4f 76 65 72  d->nCell + nOver
18270 66 6c 6f 77 3b 0a 20 20 20 20 69 6e 74 20 69 4f  flow;.    int iO
18271 76 65 72 66 6c 6f 77 20 3d 20 28 6e 4f 76 65 72  verflow = (nOver
18272 66 6c 6f 77 20 3f 20 70 4f 6c 64 2d 3e 61 4f 76  flow ? pOld->aOv
18273 66 6c 5b 30 5d 2e 69 64 78 20 3a 20 2d 31 29 3b  fl[0].idx : -1);
18274 0a 20 20 20 20 6a 20 3d 20 30 3b 20 20 20 20 20  .    j = 0;     
18275 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18276 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
18277 6e 74 20 27 6f 6c 64 27 20 73 69 62 6c 69 6e 67  nt 'old' sibling
18278 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 6b 20 3d   page */.    k =
18279 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1827a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1827b 2f 2a 20 43 75 72 72 65 6e 74 20 27 6e 65 77 27  /* Current 'new'
1827c 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20 2a 2f   sibling page */
1827d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
1827e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  nCell; i++){.   
1827f 20 20 20 69 6e 74 20 69 73 44 69 76 69 64 65 72     int isDivider
18280 20 3d 20 30 3b 0a 20 20 20 20 20 20 77 68 69 6c   = 0;.      whil
18281 65 28 20 69 3d 3d 69 4e 65 78 74 4f 6c 64 20 29  e( i==iNextOld )
18282 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 65 6c  {.        /* Cel
18283 6c 20 69 20 69 73 20 74 68 65 20 63 65 6c 6c 20  l i is the cell 
18284 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c  immediately foll
18285 6f 77 69 6e 67 20 74 68 65 20 6c 61 73 74 20 63  owing the last c
18286 65 6c 6c 20 6f 6e 20 6f 6c 64 0a 20 20 20 20 20  ell on old.     
18287 20 20 20 2a 2a 20 73 69 62 6c 69 6e 67 20 70 61     ** sibling pa
18288 67 65 20 6a 2e 20 49 66 20 74 68 65 20 73 69 62  ge j. If the sib
18289 6c 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 6c 65  lings are not le
1828a 61 66 20 70 61 67 65 73 20 6f 66 20 61 6e 0a 20  af pages of an. 
1828b 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 6b 65 79         ** intkey
1828c 20 62 2d 74 72 65 65 2c 20 74 68 65 6e 20 63 65   b-tree, then ce
1828d 6c 6c 20 69 20 77 61 73 20 61 20 64 69 76 69 64  ll i was a divid
1828e 65 72 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20  er cell. */.    
1828f 20 20 20 20 70 4f 6c 64 20 3d 20 61 70 43 6f 70      pOld = apCop
18290 79 5b 2b 2b 6a 5d 3b 0a 20 20 20 20 20 20 20 20  y[++j];.        
18291 69 4e 65 78 74 4f 6c 64 20 3d 20 69 20 2b 20 21  iNextOld = i + !
18292 6c 65 61 66 44 61 74 61 20 2b 20 70 4f 6c 64 2d  leafData + pOld-
18293 3e 6e 43 65 6c 6c 20 2b 20 70 4f 6c 64 2d 3e 6e  >nCell + pOld->n
18294 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 20 20  Overflow;.      
18295 20 20 69 66 28 20 70 4f 6c 64 2d 3e 6e 4f 76 65    if( pOld->nOve
18296 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20  rflow ){.       
18297 20 20 20 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 70     nOverflow = p
18298 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a  Old->nOverflow;.
18299 20 20 20 20 20 20 20 20 20 20 69 4f 76 65 72 66            iOverf
1829a 6c 6f 77 20 3d 20 69 20 2b 20 21 6c 65 61 66 44  low = i + !leafD
1829b 61 74 61 20 2b 20 70 4f 6c 64 2d 3e 61 4f 76 66  ata + pOld->aOvf
1829c 6c 5b 30 5d 2e 69 64 78 3b 0a 20 20 20 20 20 20  l[0].idx;.      
1829d 20 20 7d 0a 20 20 20 20 20 20 20 20 69 73 44 69    }.        isDi
1829e 76 69 64 65 72 20 3d 20 21 6c 65 61 66 44 61 74  vider = !leafDat
1829f 61 3b 20 20 0a 20 20 20 20 20 20 7d 0a 0a 20 20  a;  .      }..  
182a0 20 20 20 20 61 73 73 65 72 74 28 6e 4f 76 65 72      assert(nOver
182a1 66 6c 6f 77 3e 30 20 7c 7c 20 69 4f 76 65 72 66  flow>0 || iOverf
182a2 6c 6f 77 3c 69 20 29 3b 0a 20 20 20 20 20 20 61  low<i );.      a
182a3 73 73 65 72 74 28 6e 4f 76 65 72 66 6c 6f 77 3c  ssert(nOverflow<
182a4 32 20 7c 7c 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c  2 || pOld->aOvfl
182a5 5b 30 5d 2e 69 64 78 3d 3d 70 4f 6c 64 2d 3e 61  [0].idx==pOld->a
182a6 4f 76 66 6c 5b 31 5d 2e 69 64 78 2d 31 29 3b 0a  Ovfl[1].idx-1);.
182a7 20 20 20 20 20 20 61 73 73 65 72 74 28 6e 4f 76        assert(nOv
182a8 65 72 66 6c 6f 77 3c 33 20 7c 7c 20 70 4f 6c 64  erflow<3 || pOld
182a9 2d 3e 61 4f 76 66 6c 5b 31 5d 2e 69 64 78 3d 3d  ->aOvfl[1].idx==
182aa 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 32 5d 2e 69  pOld->aOvfl[2].i
182ab 64 78 2d 31 29 3b 0a 20 20 20 20 20 20 69 66 28  dx-1);.      if(
182ac 20 69 3d 3d 69 4f 76 65 72 66 6c 6f 77 20 29 7b   i==iOverflow ){
182ad 0a 20 20 20 20 20 20 20 20 69 73 44 69 76 69 64  .        isDivid
182ae 65 72 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  er = 1;.        
182af 69 66 28 20 28 2d 2d 6e 4f 76 65 72 66 6c 6f 77  if( (--nOverflow
182b0 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  )>0 ){.         
182b1 20 69 4f 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20   iOverflow++;.  
182b2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
182b3 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d 63 6e  .      if( i==cn
182b4 74 4e 65 77 5b 6b 5d 20 29 7b 0a 20 20 20 20 20  tNew[k] ){.     
182b5 20 20 20 2f 2a 20 43 65 6c 6c 20 69 20 69 73 20     /* Cell i is 
182b6 74 68 65 20 63 65 6c 6c 20 69 6d 6d 65 64 69 61  the cell immedia
182b7 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74  tely following t
182b8 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 6f 6e 20  he last cell on 
182b9 6e 65 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 73  new.        ** s
182ba 69 62 6c 69 6e 67 20 70 61 67 65 20 6b 2e 20 49  ibling page k. I
182bb 66 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 61  f the siblings a
182bc 72 65 20 6e 6f 74 20 6c 65 61 66 20 70 61 67 65  re not leaf page
182bd 73 20 6f 66 20 61 6e 0a 20 20 20 20 20 20 20 20  s of an.        
182be 2a 2a 20 69 6e 74 6b 65 79 20 62 2d 74 72 65 65  ** intkey b-tree
182bf 2c 20 74 68 65 6e 20 63 65 6c 6c 20 69 20 69 73  , then cell i is
182c0 20 61 20 64 69 76 69 64 65 72 20 63 65 6c 6c 2e   a divider cell.
182c1 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4e 65    */.        pNe
182c2 77 20 3d 20 61 70 4e 65 77 5b 2b 2b 6b 5d 3b 0a  w = apNew[++k];.
182c3 20 20 20 20 20 20 20 20 69 66 28 20 21 6c 65 61          if( !lea
182c4 66 44 61 74 61 20 29 20 63 6f 6e 74 69 6e 75 65  fData ) continue
182c5 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
182c6 61 73 73 65 72 74 28 20 6a 3c 6e 4f 6c 64 20 29  assert( j<nOld )
182c7 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
182c8 6b 3c 6e 4e 65 77 20 29 3b 0a 0a 20 20 20 20 20  k<nNew );..     
182c9 20 2f 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20   /* If the cell 
182ca 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 64  was originally d
182cb 69 76 69 64 65 72 20 63 65 6c 6c 20 28 61 6e 64  ivider cell (and
182cc 20 69 73 20 6e 6f 74 20 6e 6f 77 29 20 6f 72 0a   is not now) or.
182cd 20 20 20 20 20 20 2a 2a 20 61 6e 20 6f 76 65 72        ** an over
182ce 66 6c 6f 77 20 63 65 6c 6c 2c 20 6f 72 20 69 66  flow cell, or if
182cf 20 74 68 65 20 63 65 6c 6c 20 77 61 73 20 6c 6f   the cell was lo
182d0 63 61 74 65 64 20 6f 6e 20 61 20 64 69 66 66 65  cated on a diffe
182d1 72 65 6e 74 20 73 69 62 6c 69 6e 67 0a 20 20 20  rent sibling.   
182d2 20 20 20 2a 2a 20 70 61 67 65 20 62 65 66 6f 72     ** page befor
182d3 65 20 74 68 65 20 62 61 6c 61 6e 63 69 6e 67 2c  e the balancing,
182d4 20 74 68 65 6e 20 74 68 65 20 70 6f 69 6e 74 65   then the pointe
182d5 72 20 6d 61 70 20 65 6e 74 72 69 65 73 20 61 73  r map entries as
182d6 73 6f 63 69 61 74 65 64 0a 20 20 20 20 20 20 2a  sociated.      *
182d7 2a 20 77 69 74 68 20 61 6e 79 20 63 68 69 6c 64  * with any child
182d8 20 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   or overflow pag
182d9 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 75 70  es need to be up
182da 64 61 74 65 64 2e 20 20 2a 2f 0a 20 20 20 20 20  dated.  */.     
182db 20 69 66 28 20 69 73 44 69 76 69 64 65 72 20 7c   if( isDivider |
182dc 7c 20 70 4f 6c 64 2d 3e 70 67 6e 6f 21 3d 70 4e  | pOld->pgno!=pN
182dd 65 77 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20  ew->pgno ){.    
182de 20 20 20 20 69 66 28 20 21 6c 65 61 66 43 6f 72      if( !leafCor
182df 72 65 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20  rection ){.     
182e0 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70       ptrmapPut(p
182e1 42 74 2c 20 67 65 74 34 62 79 74 65 28 61 70 43  Bt, get4byte(apC
182e2 65 6c 6c 5b 69 5d 29 2c 20 50 54 52 4d 41 50 5f  ell[i]), PTRMAP_
182e3 42 54 52 45 45 2c 20 70 4e 65 77 2d 3e 70 67 6e  BTREE, pNew->pgn
182e4 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20  o, &rc);.       
182e5 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 73   }.        if( s
182e6 7a 43 65 6c 6c 5b 69 5d 3e 70 4e 65 77 2d 3e 6d  zCell[i]>pNew->m
182e7 69 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20  inLocal ){.     
182e8 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 4f 76       ptrmapPutOv
182e9 66 6c 50 74 72 28 70 4e 65 77 2c 20 61 70 43 65  flPtr(pNew, apCe
182ea 6c 6c 5b 69 5d 2c 20 26 72 63 29 3b 0a 20 20 20  ll[i], &rc);.   
182eb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
182ec 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 6c     }..    if( !l
182ed 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 20 29 7b  eafCorrection ){
182ee 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
182ef 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20 20  i<nNew; i++){.  
182f0 20 20 20 20 20 20 75 33 32 20 6b 65 79 20 3d 20        u32 key = 
182f1 67 65 74 34 62 79 74 65 28 26 61 70 4e 65 77 5b  get4byte(&apNew[
182f2 69 5d 2d 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20  i]->aData[8]);. 
182f3 20 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74         ptrmapPut
182f4 28 70 42 74 2c 20 6b 65 79 2c 20 50 54 52 4d 41  (pBt, key, PTRMA
182f5 50 5f 42 54 52 45 45 2c 20 61 70 4e 65 77 5b 69  P_BTREE, apNew[i
182f6 5d 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20  ]->pgno, &rc);. 
182f7 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69       }.    }..#i
182f8 66 20 30 0a 20 20 20 20 2f 2a 20 54 68 65 20 70  f 0.    /* The p
182f9 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65 73 28  trmapCheckPages(
182fa 29 20 63 6f 6e 74 61 69 6e 73 20 61 73 73 65 72  ) contains asser
182fb 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74  t() statements t
182fc 68 61 74 20 76 65 72 69 66 79 20 74 68 61 74 0a  hat verify that.
182fd 20 20 20 20 2a 2a 20 61 6c 6c 20 70 6f 69 6e 74      ** all point
182fe 65 72 20 6d 61 70 20 70 61 67 65 73 20 61 72 65  er map pages are
182ff 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e 20   set correctly. 
18300 54 68 69 73 20 69 73 20 68 65 6c 70 66 75 6c 20  This is helpful 
18301 77 68 69 6c 65 20 0a 20 20 20 20 2a 2a 20 64 65  while .    ** de
18302 62 75 67 67 69 6e 67 2e 20 54 68 69 73 20 69 73  bugging. This is
18303 20 75 73 75 61 6c 6c 79 20 64 69 73 61 62 6c 65   usually disable
18304 64 20 62 65 63 61 75 73 65 20 61 20 63 6f 72 72  d because a corr
18305 75 70 74 20 64 61 74 61 62 61 73 65 20 6d 61 79  upt database may
18306 0a 20 20 20 20 2a 2a 20 63 61 75 73 65 20 61 6e  .    ** cause an
18307 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
18308 65 6e 74 20 74 6f 20 66 61 69 6c 2e 20 20 2a 2f  ent to fail.  */
18309 0a 20 20 20 20 70 74 72 6d 61 70 43 68 65 63 6b  .    ptrmapCheck
1830a 50 61 67 65 73 28 61 70 4e 65 77 2c 20 6e 4e 65  Pages(apNew, nNe
1830b 77 29 3b 0a 20 20 20 20 70 74 72 6d 61 70 43 68  w);.    ptrmapCh
1830c 65 63 6b 50 61 67 65 73 28 26 70 50 61 72 65 6e  eckPages(&pParen
1830d 74 2c 20 31 29 3b 0a 23 65 6e 64 69 66 0a 20 20  t, 1);.#endif.  
1830e 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  }..  assert( pPa
1830f 72 65 6e 74 2d 3e 69 73 49 6e 69 74 20 29 3b 0a  rent->isInit );.
18310 20 20 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43    TRACE(("BALANC
18311 45 3a 20 66 69 6e 69 73 68 65 64 3a 20 6f 6c 64  E: finished: old
18312 3d 25 64 20 6e 65 77 3d 25 64 20 63 65 6c 6c 73  =%d new=%d cells
18313 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20  =%d\n",.        
18314 20 20 6e 4f 6c 64 2c 20 6e 4e 65 77 2c 20 6e 43    nOld, nNew, nC
18315 65 6c 6c 29 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  ell));..  /*.  *
18316 2a 20 43 6c 65 61 6e 75 70 20 62 65 66 6f 72 65  * Cleanup before
18317 20 72 65 74 75 72 6e 69 6e 67 2e 0a 20 20 2a 2f   returning..  */
18318 0a 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70  .balance_cleanup
18319 3a 0a 20 20 73 71 6c 69 74 65 33 53 63 72 61 74  :.  sqlite3Scrat
1831a 63 68 46 72 65 65 28 61 70 43 65 6c 6c 29 3b 0a  chFree(apCell);.
1831b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c    for(i=0; i<nOl
1831c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 65 6c  d; i++){.    rel
1831d 65 61 73 65 50 61 67 65 28 61 70 4f 6c 64 5b 69  easePage(apOld[i
1831e 5d 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  ]);.  }.  for(i=
1831f 30 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b  0; i<nNew; i++){
18320 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65  .    releasePage
18321 28 61 70 4e 65 77 5b 69 5d 29 3b 0a 20 20 7d 0a  (apNew[i]);.  }.
18322 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
18323 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
18324 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
18325 77 68 65 6e 20 74 68 65 20 72 6f 6f 74 20 70 61  when the root pa
18326 67 65 20 6f 66 20 61 20 62 2d 74 72 65 65 20 73  ge of a b-tree s
18327 74 72 75 63 74 75 72 65 20 69 73 0a 2a 2a 20 6f  tructure is.** o
18328 76 65 72 66 75 6c 6c 20 28 68 61 73 20 6f 6e 65  verfull (has one
18329 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f   or more overflo
1832a 77 20 70 61 67 65 73 29 2e 0a 2a 2a 0a 2a 2a 20  w pages)..**.** 
1832b 41 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65  A new child page
1832c 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e   is allocated an
1832d 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  d the contents o
1832e 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f  f the current ro
1832f 6f 74 0a 2a 2a 20 70 61 67 65 2c 20 69 6e 63 6c  ot.** page, incl
18330 75 64 69 6e 67 20 6f 76 65 72 66 6c 6f 77 20 63  uding overflow c
18331 65 6c 6c 73 2c 20 61 72 65 20 63 6f 70 69 65 64  ells, are copied
18332 20 69 6e 74 6f 20 74 68 65 20 63 68 69 6c 64 2e   into the child.
18333 20 54 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67   The root.** pag
18334 65 20 69 73 20 74 68 65 6e 20 6f 76 65 72 77 72  e is then overwr
18335 69 74 74 65 6e 20 74 6f 20 6d 61 6b 65 20 69 74  itten to make it
18336 20 61 6e 20 65 6d 70 74 79 20 70 61 67 65 20 77   an empty page w
18337 69 74 68 20 74 68 65 20 72 69 67 68 74 2d 63 68  ith the right-ch
18338 69 6c 64 20 0a 2a 2a 20 70 6f 69 6e 74 65 72 20  ild .** pointer 
18339 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20  pointing to the 
1833a 6e 65 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  new page..**.** 
1833b 42 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  Before returning
1833c 2c 20 61 6c 6c 20 70 6f 69 6e 74 65 72 2d 6d 61  , all pointer-ma
1833d 70 20 65 6e 74 72 69 65 73 20 63 6f 72 72 65 73  p entries corres
1833e 70 6f 6e 64 69 6e 67 20 74 6f 20 70 61 67 65 73  ponding to pages
1833f 20 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6e 65   .** that the ne
18340 77 20 63 68 69 6c 64 2d 70 61 67 65 20 6e 6f 77  w child-page now
18341 20 63 6f 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65   contains pointe
18342 72 73 20 74 6f 20 61 72 65 20 75 70 64 61 74 65  rs to are update
18343 64 2e 20 54 68 65 0a 2a 2a 20 65 6e 74 72 79 20  d. The.** entry 
18344 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
18345 20 74 68 65 20 6e 65 77 20 72 69 67 68 74 2d 63   the new right-c
18346 68 69 6c 64 20 70 6f 69 6e 74 65 72 20 6f 66 20  hild pointer of 
18347 74 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65  the root.** page
18348 20 69 73 20 61 6c 73 6f 20 75 70 64 61 74 65 64   is also updated
18349 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65  ..**.** If succe
1834a 73 73 66 75 6c 2c 20 2a 70 70 43 68 69 6c 64 20  ssful, *ppChild 
1834b 69 73 20 73 65 74 20 74 6f 20 63 6f 6e 74 61 69  is set to contai
1834c 6e 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  n a reference to
1834d 20 74 68 65 20 63 68 69 6c 64 20 0a 2a 2a 20 70   the child .** p
1834e 61 67 65 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f  age and SQLITE_O
1834f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  K is returned. I
18350 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20  n this case the 
18351 63 61 6c 6c 65 72 20 69 73 20 72 65 71 75 69 72  caller is requir
18352 65 64 0a 2a 2a 20 74 6f 20 63 61 6c 6c 20 72 65  ed.** to call re
18353 6c 65 61 73 65 50 61 67 65 28 29 20 6f 6e 20 2a  leasePage() on *
18354 70 70 43 68 69 6c 64 20 65 78 61 63 74 6c 79 20  ppChild exactly 
18355 6f 6e 63 65 2e 20 49 66 20 61 6e 20 65 72 72 6f  once. If an erro
18356 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 61 6e 20  r occurs,.** an 
18357 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
18358 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 43 68  turned and *ppCh
18359 69 6c 64 20 69 73 20 73 65 74 20 74 6f 20 30 2e  ild is set to 0.
1835a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
1835b 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 4d 65  alance_deeper(Me
1835c 6d 50 61 67 65 20 2a 70 52 6f 6f 74 2c 20 4d 65  mPage *pRoot, Me
1835d 6d 50 61 67 65 20 2a 2a 70 70 43 68 69 6c 64 29  mPage **ppChild)
1835e 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  {.  int rc;     
1835f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18360 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 61 6c     /* Return val
18361 75 65 20 66 72 6f 6d 20 73 75 62 70 72 6f 63 65  ue from subproce
18362 64 75 72 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 61  dures */.  MemPa
18363 67 65 20 2a 70 43 68 69 6c 64 20 3d 20 30 3b 20  ge *pChild = 0; 
18364 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
18365 6e 74 65 72 20 74 6f 20 61 20 6e 65 77 20 63 68  nter to a new ch
18366 69 6c 64 20 70 61 67 65 20 2a 2f 0a 20 20 50 67  ild page */.  Pg
18367 6e 6f 20 70 67 6e 6f 43 68 69 6c 64 20 3d 20 30  no pgnoChild = 0
18368 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
18369 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74  Page number of t
1836a 68 65 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67  he new child pag
1836b 65 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20  e */.  BtShared 
1836c 2a 70 42 74 20 3d 20 70 52 6f 6f 74 2d 3e 70 42  *pBt = pRoot->pB
1836d 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 42 54 72  t;    /* The BTr
1836e 65 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  ee */..  assert(
1836f 20 70 52 6f 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f   pRoot->nOverflo
18370 77 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  w>0 );.  assert(
18371 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
18372 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
18373 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 70 52  );..  /* Make pR
18374 6f 6f 74 2c 20 74 68 65 20 72 6f 6f 74 20 70 61  oot, the root pa
18375 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65  ge of the b-tree
18376 2c 20 77 72 69 74 61 62 6c 65 2e 20 41 6c 6c 6f  , writable. Allo
18377 63 61 74 65 20 61 20 6e 65 77 20 0a 20 20 2a 2a  cate a new .  **
18378 20 70 61 67 65 20 74 68 61 74 20 77 69 6c 6c 20   page that will 
18379 62 65 63 6f 6d 65 20 74 68 65 20 6e 65 77 20 72  become the new r
1837a 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 70 50  ight-child of pP
1837b 61 67 65 2e 20 43 6f 70 79 20 74 68 65 20 63 6f  age. Copy the co
1837c 6e 74 65 6e 74 73 0a 20 20 2a 2a 20 6f 66 20 74  ntents.  ** of t
1837d 68 65 20 6e 6f 64 65 20 73 74 6f 72 65 64 20 6f  he node stored o
1837e 6e 20 70 52 6f 6f 74 20 69 6e 74 6f 20 74 68 65  n pRoot into the
1837f 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 2e   new child page.
18380 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  .  */.  rc = sql
18381 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
18382 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a  Root->pDbPage);.
18383 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
18384 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  _OK ){.    rc = 
18385 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67  allocateBtreePag
18386 65 28 70 42 74 2c 26 70 43 68 69 6c 64 2c 26 70  e(pBt,&pChild,&p
18387 67 6e 6f 43 68 69 6c 64 2c 70 52 6f 6f 74 2d 3e  gnoChild,pRoot->
18388 70 67 6e 6f 2c 30 29 3b 0a 20 20 20 20 63 6f 70  pgno,0);.    cop
18389 79 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 70 52 6f  yNodeContent(pRo
1838a 6f 74 2c 20 70 43 68 69 6c 64 2c 20 26 72 63 29  ot, pChild, &rc)
1838b 3b 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f  ;.    if( ISAUTO
1838c 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20  VACUUM ){.      
1838d 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70  ptrmapPut(pBt, p
1838e 67 6e 6f 43 68 69 6c 64 2c 20 50 54 52 4d 41 50  gnoChild, PTRMAP
1838f 5f 42 54 52 45 45 2c 20 70 52 6f 6f 74 2d 3e 70  _BTREE, pRoot->p
18390 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d  gno, &rc);.    }
18391 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20 29 7b  .  }.  if( rc ){
18392 0a 20 20 20 20 2a 70 70 43 68 69 6c 64 20 3d 20  .    *ppChild = 
18393 30 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61  0;.    releasePa
18394 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 20 20 20  ge(pChild);.    
18395 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
18396 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
18397 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
18398 28 70 43 68 69 6c 64 2d 3e 70 44 62 50 61 67 65  (pChild->pDbPage
18399 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
1839a 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
1839b 74 65 61 62 6c 65 28 70 52 6f 6f 74 2d 3e 70 44  teable(pRoot->pD
1839c 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65  bPage) );.  asse
1839d 72 74 28 20 70 43 68 69 6c 64 2d 3e 6e 43 65 6c  rt( pChild->nCel
1839e 6c 3d 3d 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 20  l==pRoot->nCell 
1839f 29 3b 0a 0a 20 20 54 52 41 43 45 28 28 22 42 41  );..  TRACE(("BA
183a0 4c 41 4e 43 45 3a 20 63 6f 70 79 20 72 6f 6f 74  LANCE: copy root
183a1 20 25 64 20 69 6e 74 6f 20 25 64 5c 6e 22 2c 20   %d into %d\n", 
183a2 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 20 70 43 68  pRoot->pgno, pCh
183a3 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 0a 20 20  ild->pgno));..  
183a4 2f 2a 20 43 6f 70 79 20 74 68 65 20 6f 76 65 72  /* Copy the over
183a5 66 6c 6f 77 20 63 65 6c 6c 73 20 66 72 6f 6d 20  flow cells from 
183a6 70 52 6f 6f 74 20 74 6f 20 70 43 68 69 6c 64 20  pRoot to pChild 
183a7 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 70 43 68 69  */.  memcpy(pChi
183a8 6c 64 2d 3e 61 4f 76 66 6c 2c 20 70 52 6f 6f 74  ld->aOvfl, pRoot
183a9 2d 3e 61 4f 76 66 6c 2c 20 70 52 6f 6f 74 2d 3e  ->aOvfl, pRoot->
183aa 6e 4f 76 65 72 66 6c 6f 77 2a 73 69 7a 65 6f 66  nOverflow*sizeof
183ab 28 70 52 6f 6f 74 2d 3e 61 4f 76 66 6c 5b 30 5d  (pRoot->aOvfl[0]
183ac 29 29 3b 0a 20 20 70 43 68 69 6c 64 2d 3e 6e 4f  ));.  pChild->nO
183ad 76 65 72 66 6c 6f 77 20 3d 20 70 52 6f 6f 74 2d  verflow = pRoot-
183ae 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 0a 20 20 2f  >nOverflow;..  /
183af 2a 20 5a 65 72 6f 20 74 68 65 20 63 6f 6e 74 65  * Zero the conte
183b0 6e 74 73 20 6f 66 20 70 52 6f 6f 74 2e 20 54 68  nts of pRoot. Th
183b1 65 6e 20 69 6e 73 74 61 6c 6c 20 70 43 68 69 6c  en install pChil
183b2 64 20 61 73 20 74 68 65 20 72 69 67 68 74 2d 63  d as the right-c
183b3 68 69 6c 64 2e 20 2a 2f 0a 20 20 7a 65 72 6f 50  hild. */.  zeroP
183b4 61 67 65 28 70 52 6f 6f 74 2c 20 70 43 68 69 6c  age(pRoot, pChil
183b5 64 2d 3e 61 44 61 74 61 5b 30 5d 20 26 20 7e 50  d->aData[0] & ~P
183b6 54 46 5f 4c 45 41 46 29 3b 0a 20 20 70 75 74 34  TF_LEAF);.  put4
183b7 62 79 74 65 28 26 70 52 6f 6f 74 2d 3e 61 44 61  byte(&pRoot->aDa
183b8 74 61 5b 70 52 6f 6f 74 2d 3e 68 64 72 4f 66 66  ta[pRoot->hdrOff
183b9 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 43 68 69 6c  set+8], pgnoChil
183ba 64 29 3b 0a 0a 20 20 2a 70 70 43 68 69 6c 64 20  d);..  *ppChild 
183bb 3d 20 70 43 68 69 6c 64 3b 0a 20 20 72 65 74 75  = pChild;.  retu
183bc 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
183bd 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20  ./*.** The page 
183be 74 68 61 74 20 70 43 75 72 20 63 75 72 72 65 6e  that pCur curren
183bf 74 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 68 61  tly points to ha
183c0 73 20 6a 75 73 74 20 62 65 65 6e 20 6d 6f 64 69  s just been modi
183c1 66 69 65 64 20 69 6e 0a 2a 2a 20 73 6f 6d 65 20  fied in.** some 
183c2 77 61 79 2e 20 54 68 69 73 20 66 75 6e 63 74 69  way. This functi
183c3 6f 6e 20 66 69 67 75 72 65 73 20 6f 75 74 20 69  on figures out i
183c4 66 20 74 68 69 73 20 6d 6f 64 69 66 69 63 61 74  f this modificat
183c5 69 6f 6e 20 6d 65 61 6e 73 20 74 68 65 0a 2a 2a  ion means the.**
183c6 20 74 72 65 65 20 6e 65 65 64 73 20 74 6f 20 62   tree needs to b
183c7 65 20 62 61 6c 61 6e 63 65 64 2c 20 61 6e 64 20  e balanced, and 
183c8 69 66 20 73 6f 20 63 61 6c 6c 73 20 74 68 65 20  if so calls the 
183c9 61 70 70 72 6f 70 72 69 61 74 65 20 62 61 6c 61  appropriate bala
183ca 6e 63 69 6e 67 20 0a 2a 2a 20 72 6f 75 74 69 6e  ncing .** routin
183cb 65 2e 20 42 61 6c 61 6e 63 69 6e 67 20 72 6f 75  e. Balancing rou
183cc 74 69 6e 65 73 20 61 72 65 3a 0a 2a 2a 0a 2a 2a  tines are:.**.**
183cd 20 20 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b     balance_quick
183ce 28 29 0a 2a 2a 20 20 20 62 61 6c 61 6e 63 65 5f  ().**   balance_
183cf 64 65 65 70 65 72 28 29 0a 2a 2a 20 20 20 62 61  deeper().**   ba
183d0 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 0a  lance_nonroot().
183d1 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61  */.static int ba
183d2 6c 61 6e 63 65 28 42 74 43 75 72 73 6f 72 20 2a  lance(BtCursor *
183d3 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 20  pCur){.  int rc 
183d4 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63  = SQLITE_OK;.  c
183d5 6f 6e 73 74 20 69 6e 74 20 6e 4d 69 6e 20 3d 20  onst int nMin = 
183d6 70 43 75 72 2d 3e 70 42 74 2d 3e 75 73 61 62 6c  pCur->pBt->usabl
183d7 65 53 69 7a 65 20 2a 20 32 20 2f 20 33 3b 0a 20  eSize * 2 / 3;. 
183d8 20 75 38 20 61 42 61 6c 61 6e 63 65 51 75 69 63   u8 aBalanceQuic
183d9 6b 53 70 61 63 65 5b 31 33 5d 3b 0a 20 20 75 38  kSpace[13];.  u8
183da 20 2a 70 46 72 65 65 20 3d 20 30 3b 0a 0a 20 20   *pFree = 0;..  
183db 54 45 53 54 4f 4e 4c 59 28 20 69 6e 74 20 62 61  TESTONLY( int ba
183dc 6c 61 6e 63 65 5f 71 75 69 63 6b 5f 63 61 6c 6c  lance_quick_call
183dd 65 64 20 3d 20 30 20 29 3b 0a 20 20 54 45 53 54  ed = 0 );.  TEST
183de 4f 4e 4c 59 28 20 69 6e 74 20 62 61 6c 61 6e 63  ONLY( int balanc
183df 65 5f 64 65 65 70 65 72 5f 63 61 6c 6c 65 64 20  e_deeper_called 
183e0 3d 20 30 20 29 3b 0a 0a 20 20 64 6f 20 7b 0a 20  = 0 );..  do {. 
183e1 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70     int iPage = p
183e2 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20  Cur->iPage;.    
183e3 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d  MemPage *pPage =
183e4 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50   pCur->apPage[iP
183e5 61 67 65 5d 3b 0a 0a 20 20 20 20 69 66 28 20 69  age];..    if( i
183e6 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Page==0 ){.     
183e7 20 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65   if( pPage->nOve
183e8 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20  rflow ){.       
183e9 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67   /* The root pag
183ea 65 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 20  e of the b-tree 
183eb 69 73 20 6f 76 65 72 66 75 6c 6c 2e 20 49 6e 20  is overfull. In 
183ec 74 68 69 73 20 63 61 73 65 20 63 61 6c 6c 20 74  this case call t
183ed 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 61  he.        ** ba
183ee 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 29 20 66  lance_deeper() f
183ef 75 6e 63 74 69 6f 6e 20 74 6f 20 63 72 65 61 74  unction to creat
183f0 65 20 61 20 6e 65 77 20 63 68 69 6c 64 20 66 6f  e a new child fo
183f1 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 0a  r the root-page.
183f2 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 63          ** and c
183f3 6f 70 79 20 74 68 65 20 63 75 72 72 65 6e 74 20  opy the current 
183f4 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
183f5 72 6f 6f 74 2d 70 61 67 65 20 74 6f 20 69 74 2e  root-page to it.
183f6 20 54 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20   The.        ** 
183f7 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f  next iteration o
183f8 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 20 77 69  f the do-loop wi
183f9 6c 6c 20 62 61 6c 61 6e 63 65 20 74 68 65 20 63  ll balance the c
183fa 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 20 20 20  hild page..     
183fb 20 20 20 2a 2f 20 0a 20 20 20 20 20 20 20 20 61     */ .        a
183fc 73 73 65 72 74 28 20 28 62 61 6c 61 6e 63 65 5f  ssert( (balance_
183fd 64 65 65 70 65 72 5f 63 61 6c 6c 65 64 2b 2b 29  deeper_called++)
183fe 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72  ==0 );.        r
183ff 63 20 3d 20 62 61 6c 61 6e 63 65 5f 64 65 65 70  c = balance_deep
18400 65 72 28 70 50 61 67 65 2c 20 26 70 43 75 72 2d  er(pPage, &pCur-
18401 3e 61 70 50 61 67 65 5b 31 5d 29 3b 0a 20 20 20  >apPage[1]);.   
18402 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
18403 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
18404 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20      pCur->iPage 
18405 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70  = 1;.          p
18406 43 75 72 2d 3e 61 69 49 64 78 5b 30 5d 20 3d 20  Cur->aiIdx[0] = 
18407 30 3b 0a 20 20 20 20 20 20 20 20 20 20 70 43 75  0;.          pCu
18408 72 2d 3e 61 69 49 64 78 5b 31 5d 20 3d 20 30 3b  r->aiIdx[1] = 0;
18409 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
1840a 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  t( pCur->apPage[
1840b 31 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 3b  1]->nOverflow );
1840c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1840d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1840e 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
1840f 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61     }else if( pPa
18410 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30  ge->nOverflow==0
18411 20 26 26 20 70 50 61 67 65 2d 3e 6e 46 72 65 65   && pPage->nFree
18412 3c 3d 6e 4d 69 6e 20 29 7b 0a 20 20 20 20 20 20  <=nMin ){.      
18413 62 72 65 61 6b 3b 0a 20 20 20 20 7d 65 6c 73 65  break;.    }else
18414 7b 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20  {.      MemPage 
18415 2a 20 63 6f 6e 73 74 20 70 50 61 72 65 6e 74 20  * const pParent 
18416 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69  = pCur->apPage[i
18417 50 61 67 65 2d 31 5d 3b 0a 20 20 20 20 20 20 69  Page-1];.      i
18418 6e 74 20 63 6f 6e 73 74 20 69 49 64 78 20 3d 20  nt const iIdx = 
18419 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67  pCur->aiIdx[iPag
1841a 65 2d 31 5d 3b 0a 0a 20 20 20 20 20 20 72 63 20  e-1];..      rc 
1841b 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
1841c 69 74 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62  ite(pParent->pDb
1841d 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Page);.      if(
1841e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1841f 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
18420 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e  _OMIT_QUICKBALAN
18421 43 45 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  CE.        if( p
18422 50 61 67 65 2d 3e 68 61 73 44 61 74 61 0a 20 20  Page->hasData.  
18423 20 20 20 20 20 20 20 26 26 20 70 50 61 67 65 2d         && pPage-
18424 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 0a 20 20  >nOverflow==1.  
18425 20 20 20 20 20 20 20 26 26 20 70 50 61 67 65 2d         && pPage-
18426 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d 70  >aOvfl[0].idx==p
18427 50 61 67 65 2d 3e 6e 43 65 6c 6c 0a 20 20 20 20  Page->nCell.    
18428 20 20 20 20 20 26 26 20 70 50 61 72 65 6e 74 2d       && pParent-
18429 3e 70 67 6e 6f 21 3d 31 0a 20 20 20 20 20 20 20  >pgno!=1.       
1842a 20 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43    && pParent->nC
1842b 65 6c 6c 3d 3d 69 49 64 78 0a 20 20 20 20 20 20  ell==iIdx.      
1842c 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f    ){.          /
1842d 2a 20 43 61 6c 6c 20 62 61 6c 61 6e 63 65 5f 71  * Call balance_q
1842e 75 69 63 6b 28 29 20 74 6f 20 63 72 65 61 74 65  uick() to create
1842f 20 61 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 6f   a new sibling o
18430 66 20 70 50 61 67 65 20 6f 6e 20 77 68 69 63 68  f pPage on which
18431 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 6f  .          ** to
18432 20 73 74 6f 72 65 20 74 68 65 20 6f 76 65 72 66   store the overf
18433 6c 6f 77 20 63 65 6c 6c 2e 20 62 61 6c 61 6e 63  low cell. balanc
18434 65 5f 71 75 69 63 6b 28 29 20 69 6e 73 65 72 74  e_quick() insert
18435 73 20 61 20 6e 65 77 20 63 65 6c 6c 0a 20 20 20  s a new cell.   
18436 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 70         ** into p
18437 50 61 72 65 6e 74 2c 20 77 68 69 63 68 20 6d 61  Parent, which ma
18438 79 20 63 61 75 73 65 20 70 50 61 72 65 6e 74 20  y cause pParent 
18439 6f 76 65 72 66 6c 6f 77 2e 20 49 66 20 74 68 69  overflow. If thi
1843a 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 68  s.          ** h
1843b 61 70 70 65 6e 73 2c 20 74 68 65 20 6e 65 78 74  appens, the next
1843c 20 69 6e 74 65 72 61 74 69 6f 6e 20 6f 66 20 74   interation of t
1843d 68 65 20 64 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20  he do-loop will 
1843e 62 61 6c 61 6e 63 65 20 70 50 61 72 65 6e 74 20  balance pParent 
1843f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 75 73  .          ** us
18440 65 20 65 69 74 68 65 72 20 62 61 6c 61 6e 63 65  e either balance
18441 5f 6e 6f 6e 72 6f 6f 74 28 29 20 6f 72 20 62 61  _nonroot() or ba
18442 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 29 2e 20  lance_deeper(). 
18443 55 6e 74 69 6c 20 74 68 69 73 0a 20 20 20 20 20  Until this.     
18444 20 20 20 20 20 2a 2a 20 68 61 70 70 65 6e 73 2c       ** happens,
18445 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65   the overflow ce
18446 6c 6c 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  ll is stored in 
18447 74 68 65 20 61 42 61 6c 61 6e 63 65 51 75 69 63  the aBalanceQuic
18448 6b 53 70 61 63 65 5b 5d 0a 20 20 20 20 20 20 20  kSpace[].       
18449 20 20 20 2a 2a 20 62 75 66 66 65 72 2e 20 0a 20     ** buffer. . 
1844a 20 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20           **.    
1844b 20 20 20 20 20 20 2a 2a 20 54 68 65 20 70 75 72        ** The pur
1844c 70 6f 73 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  pose of the foll
1844d 6f 77 69 6e 67 20 61 73 73 65 72 74 28 29 20 69  owing assert() i
1844e 73 20 74 6f 20 63 68 65 63 6b 20 74 68 61 74 20  s to check that 
1844f 6f 6e 6c 79 20 61 0a 20 20 20 20 20 20 20 20 20  only a.         
18450 20 2a 2a 20 73 69 6e 67 6c 65 20 63 61 6c 6c 20   ** single call 
18451 74 6f 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b  to balance_quick
18452 28 29 20 69 73 20 6d 61 64 65 20 66 6f 72 20 65  () is made for e
18453 61 63 68 20 63 61 6c 6c 20 74 6f 20 74 68 69 73  ach call to this
18454 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 75  .          ** fu
18455 6e 63 74 69 6f 6e 2e 20 49 66 20 74 68 69 73 20  nction. If this 
18456 77 65 72 65 20 6e 6f 74 20 76 65 72 69 66 69 65  were not verifie
18457 64 2c 20 61 20 73 75 62 74 6c 65 20 62 75 67 20  d, a subtle bug 
18458 69 6e 76 6f 6c 76 69 6e 67 20 72 65 75 73 65 0a  involving reuse.
18459 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20            ** of 
1845a 74 68 65 20 61 42 61 6c 61 6e 63 65 51 75 69 63  the aBalanceQuic
1845b 6b 53 70 61 63 65 5b 5d 20 6d 69 67 68 74 20 73  kSpace[] might s
1845c 6e 65 61 6b 20 69 6e 2e 0a 20 20 20 20 20 20 20  neak in..       
1845d 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
1845e 61 73 73 65 72 74 28 20 28 62 61 6c 61 6e 63 65  assert( (balance
1845f 5f 71 75 69 63 6b 5f 63 61 6c 6c 65 64 2b 2b 29  _quick_called++)
18460 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20  ==0 );.         
18461 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 71 75   rc = balance_qu
18462 69 63 6b 28 70 50 61 72 65 6e 74 2c 20 70 50 61  ick(pParent, pPa
18463 67 65 2c 20 61 42 61 6c 61 6e 63 65 51 75 69 63  ge, aBalanceQuic
18464 6b 53 70 61 63 65 29 3b 0a 20 20 20 20 20 20 20  kSpace);.       
18465 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20   }else.#endif.  
18466 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20        {.        
18467 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61 73    /* In this cas
18468 65 2c 20 63 61 6c 6c 20 62 61 6c 61 6e 63 65 5f  e, call balance_
18469 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f 20 72 65 64  nonroot() to red
1846a 69 73 74 72 69 62 75 74 65 20 63 65 6c 6c 73 0a  istribute cells.
1846b 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 74            ** bet
1846c 77 65 65 6e 20 70 50 61 67 65 20 61 6e 64 20 75  ween pPage and u
1846d 70 20 74 6f 20 32 20 6f 66 20 69 74 73 20 73 69  p to 2 of its si
1846e 62 6c 69 6e 67 20 70 61 67 65 73 2e 20 54 68 69  bling pages. Thi
1846f 73 20 69 6e 76 6f 6c 76 65 73 0a 20 20 20 20 20  s involves.     
18470 20 20 20 20 20 2a 2a 20 6d 6f 64 69 66 79 69 6e       ** modifyin
18471 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  g the contents o
18472 66 20 70 50 61 72 65 6e 74 2c 20 77 68 69 63 68  f pParent, which
18473 20 6d 61 79 20 63 61 75 73 65 20 70 50 61 72 65   may cause pPare
18474 6e 74 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20  nt to.          
18475 2a 2a 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75  ** become overfu
18476 6c 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e  ll or underfull.
18477 20 54 68 65 20 6e 65 78 74 20 69 74 65 72 61 74   The next iterat
18478 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f  ion of the do-lo
18479 6f 70 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  op.          ** 
1847a 77 69 6c 6c 20 62 61 6c 61 6e 63 65 20 74 68 65  will balance the
1847b 20 70 61 72 65 6e 74 20 70 61 67 65 20 74 6f 20   parent page to 
1847c 63 6f 72 72 65 63 74 20 74 68 69 73 2e 0a 20 20  correct this..  
1847d 20 20 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20          ** .    
1847e 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20        ** If the 
1847f 70 61 72 65 6e 74 20 70 61 67 65 20 62 65 63 6f  parent page beco
18480 6d 65 73 20 6f 76 65 72 66 75 6c 6c 2c 20 74 68  mes overfull, th
18481 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20  e overflow cell 
18482 6f 72 20 63 65 6c 6c 73 0a 20 20 20 20 20 20 20  or cells.       
18483 20 20 20 2a 2a 20 61 72 65 20 73 74 6f 72 65 64     ** are stored
18484 20 69 6e 20 74 68 65 20 70 53 70 61 63 65 20 62   in the pSpace b
18485 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 20  uffer allocated 
18486 69 6d 6d 65 64 69 61 74 65 6c 79 20 62 65 6c 6f  immediately belo
18487 77 2e 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  w. .          **
18488 20 41 20 73 75 62 73 65 71 75 65 6e 74 20 69 74   A subsequent it
18489 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64  eration of the d
1848a 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20 64 65 61 6c  o-loop will deal
1848b 20 77 69 74 68 20 74 68 69 73 20 62 79 0a 20 20   with this by.  
1848c 20 20 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 69          ** calli
1848d 6e 67 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f  ng balance_nonro
1848e 6f 74 28 29 20 28 62 61 6c 61 6e 63 65 5f 64 65  ot() (balance_de
1848f 65 70 65 72 28 29 20 6d 61 79 20 62 65 20 63 61  eper() may be ca
18490 6c 6c 65 64 20 66 69 72 73 74 2c 0a 20 20 20 20  lled first,.    
18491 20 20 20 20 20 20 2a 2a 20 62 75 74 20 69 74 20        ** but it 
18492 64 6f 65 73 6e 27 74 20 64 65 61 6c 20 77 69 74  doesn't deal wit
18493 68 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73  h overflow cells
18494 20 2d 20 6a 75 73 74 20 6d 6f 76 65 73 20 74 68   - just moves th
18495 65 6d 20 74 6f 20 61 0a 20 20 20 20 20 20 20 20  em to a.        
18496 20 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 70    ** different p
18497 61 67 65 29 2e 20 4f 6e 63 65 20 74 68 69 73 20  age). Once this 
18498 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20  subsequent call 
18499 74 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f  to balance_nonro
1849a 6f 74 28 29 20 0a 20 20 20 20 20 20 20 20 20 20  ot() .          
1849b 2a 2a 20 68 61 73 20 63 6f 6d 70 6c 65 74 65 64  ** has completed
1849c 2c 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20  , it is safe to 
1849d 72 65 6c 65 61 73 65 20 74 68 65 20 70 53 70 61  release the pSpa
1849e 63 65 20 62 75 66 66 65 72 20 75 73 65 64 20 62  ce buffer used b
1849f 79 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74  y.          ** t
184a0 68 65 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c  he previous call
184a1 2c 20 61 73 20 74 68 65 20 6f 76 65 72 66 6c 6f  , as the overflo
184a2 77 20 63 65 6c 6c 20 64 61 74 61 20 77 69 6c 6c  w cell data will
184a3 20 68 61 76 65 20 62 65 65 6e 20 0a 20 20 20 20   have been .    
184a4 20 20 20 20 20 20 2a 2a 20 63 6f 70 69 65 64 20        ** copied 
184a5 65 69 74 68 65 72 20 69 6e 74 6f 20 74 68 65 20  either into the 
184a6 62 6f 64 79 20 6f 66 20 61 20 64 61 74 61 62 61  body of a databa
184a7 73 65 20 70 61 67 65 20 6f 72 20 69 6e 74 6f 20  se page or into 
184a8 74 68 65 20 6e 65 77 0a 20 20 20 20 20 20 20 20  the new.        
184a9 20 20 2a 2a 20 70 53 70 61 63 65 20 62 75 66 66    ** pSpace buff
184aa 65 72 20 70 61 73 73 65 64 20 74 6f 20 74 68 65  er passed to the
184ab 20 6c 61 74 74 65 72 20 63 61 6c 6c 20 74 6f 20   latter call to 
184ac 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28  balance_nonroot(
184ad 29 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  )..          */.
184ae 20 20 20 20 20 20 20 20 20 20 75 38 20 2a 70 53            u8 *pS
184af 70 61 63 65 20 3d 20 73 71 6c 69 74 65 33 50 61  pace = sqlite3Pa
184b0 67 65 4d 61 6c 6c 6f 63 28 70 43 75 72 2d 3e 70  geMalloc(pCur->p
184b1 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20  Bt->pageSize);. 
184b2 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 61           rc = ba
184b3 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 70 50  lance_nonroot(pP
184b4 61 72 65 6e 74 2c 20 69 49 64 78 2c 20 70 53 70  arent, iIdx, pSp
184b5 61 63 65 2c 20 69 50 61 67 65 3d 3d 31 29 3b 0a  ace, iPage==1);.
184b6 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 46            if( pF
184b7 72 65 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ree ){.         
184b8 20 20 20 2f 2a 20 49 66 20 70 46 72 65 65 20 69     /* If pFree i
184b9 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 74 20 70  s not NULL, it p
184ba 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 70 53 70  oints to the pSp
184bb 61 63 65 20 62 75 66 66 65 72 20 75 73 65 64 20  ace buffer used 
184bc 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
184bd 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61  by a previous ca
184be 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f  ll to balance_no
184bf 6e 72 6f 6f 74 28 29 2e 20 49 74 73 20 63 6f 6e  nroot(). Its con
184c0 74 65 6e 74 73 20 61 72 65 0a 20 20 20 20 20 20  tents are.      
184c1 20 20 20 20 20 20 2a 2a 20 6e 6f 77 20 73 74 6f        ** now sto
184c2 72 65 64 20 65 69 74 68 65 72 20 6f 6e 20 72 65  red either on re
184c3 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 67 65  al database page
184c4 73 20 6f 72 20 77 69 74 68 69 6e 20 74 68 65 20  s or within the 
184c5 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
184c6 6e 65 77 20 70 53 70 61 63 65 20 62 75 66 66 65  new pSpace buffe
184c7 72 2c 20 73 6f 20 69 74 20 6d 61 79 20 62 65 20  r, so it may be 
184c8 73 61 66 65 6c 79 20 66 72 65 65 64 20 68 65 72  safely freed her
184c9 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  e. */.          
184ca 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65    sqlite3PageFre
184cb 65 28 70 46 72 65 65 29 3b 0a 20 20 20 20 20 20  e(pFree);.      
184cc 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20      }..         
184cd 20 2f 2a 20 54 68 65 20 70 53 70 61 63 65 20 62   /* The pSpace b
184ce 75 66 66 65 72 20 77 69 6c 6c 20 62 65 20 66 72  uffer will be fr
184cf 65 65 64 20 61 66 74 65 72 20 74 68 65 20 6e 65  eed after the ne
184d0 78 74 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 20  xt call to.     
184d1 20 20 20 20 20 2a 2a 20 62 61 6c 61 6e 63 65 5f       ** balance_
184d2 6e 6f 6e 72 6f 6f 74 28 29 2c 20 6f 72 20 6a 75  nonroot(), or ju
184d3 73 74 20 62 65 66 6f 72 65 20 74 68 69 73 20 66  st before this f
184d4 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2c  unction returns,
184d5 20 77 68 69 63 68 65 76 65 72 0a 20 20 20 20 20   whichever.     
184d6 20 20 20 20 20 2a 2a 20 63 6f 6d 65 73 20 66 69       ** comes fi
184d7 72 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  rst. */.        
184d8 20 20 70 46 72 65 65 20 3d 20 70 53 70 61 63 65    pFree = pSpace
184d9 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
184da 20 20 7d 0a 0a 20 20 20 20 20 20 70 50 61 67 65    }..      pPage
184db 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b  ->nOverflow = 0;
184dc 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e  ..      /* The n
184dd 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66  ext iteration of
184de 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 20 62 61 6c   the do-loop bal
184df 61 6e 63 65 73 20 74 68 65 20 70 61 72 65 6e 74  ances the parent
184e0 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20   page. */.      
184e1 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67  releasePage(pPag
184e2 65 29 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e  e);.      pCur->
184e3 69 50 61 67 65 2d 2d 3b 0a 20 20 20 20 7d 0a 20  iPage--;.    }. 
184e4 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c   }while( rc==SQL
184e5 49 54 45 5f 4f 4b 20 29 3b 0a 0a 20 20 69 66 28  ITE_OK );..  if(
184e6 20 70 46 72 65 65 20 29 7b 0a 20 20 20 20 73 71   pFree ){.    sq
184e7 6c 69 74 65 33 50 61 67 65 46 72 65 65 28 70 46  lite3PageFree(pF
184e8 72 65 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ree);.  }.  retu
184e9 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
184ea 20 49 6e 73 65 72 74 20 61 20 6e 65 77 20 72 65   Insert a new re
184eb 63 6f 72 64 20 69 6e 74 6f 20 74 68 65 20 42 54  cord into the BT
184ec 72 65 65 2e 20 20 54 68 65 20 6b 65 79 20 69 73  ree.  The key is
184ed 20 67 69 76 65 6e 20 62 79 20 28 70 4b 65 79 2c   given by (pKey,
184ee 6e 4b 65 79 29 0a 2a 2a 20 61 6e 64 20 74 68 65  nKey).** and the
184ef 20 64 61 74 61 20 69 73 20 67 69 76 65 6e 20 62   data is given b
184f0 79 20 28 70 44 61 74 61 2c 6e 44 61 74 61 29 2e  y (pData,nData).
184f1 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20    The cursor is 
184f2 75 73 65 64 20 6f 6e 6c 79 20 74 6f 0a 2a 2a 20  used only to.** 
184f3 64 65 66 69 6e 65 20 77 68 61 74 20 74 61 62 6c  define what tabl
184f4 65 20 74 68 65 20 72 65 63 6f 72 64 20 73 68 6f  e the record sho
184f5 75 6c 64 20 62 65 20 69 6e 73 65 72 74 65 64 20  uld be inserted 
184f6 69 6e 74 6f 2e 20 20 54 68 65 20 63 75 72 73 6f  into.  The curso
184f7 72 0a 2a 2a 20 69 73 20 6c 65 66 74 20 70 6f 69  r.** is left poi
184f8 6e 74 69 6e 67 20 61 74 20 61 20 72 61 6e 64 6f  nting at a rando
184f9 6d 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  m location..**.*
184fa 2a 20 46 6f 72 20 61 6e 20 49 4e 54 4b 45 59 20  * For an INTKEY 
184fb 74 61 62 6c 65 2c 20 6f 6e 6c 79 20 74 68 65 20  table, only the 
184fc 6e 4b 65 79 20 76 61 6c 75 65 20 6f 66 20 74 68  nKey value of th
184fd 65 20 6b 65 79 20 69 73 20 75 73 65 64 2e 20 20  e key is used.  
184fe 70 4b 65 79 20 69 73 0a 2a 2a 20 69 67 6e 6f 72  pKey is.** ignor
184ff 65 64 2e 20 20 46 6f 72 20 61 20 5a 45 52 4f 44  ed.  For a ZEROD
18500 41 54 41 20 74 61 62 6c 65 2c 20 74 68 65 20 70  ATA table, the p
18501 44 61 74 61 20 61 6e 64 20 6e 44 61 74 61 20 61  Data and nData a
18502 72 65 20 62 6f 74 68 20 69 67 6e 6f 72 65 64 2e  re both ignored.
18503 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 65  .**.** If the se
18504 65 6b 52 65 73 75 6c 74 20 70 61 72 61 6d 65 74  ekResult paramet
18505 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  er is non-zero, 
18506 74 68 65 6e 20 61 20 73 75 63 63 65 73 73 66 75  then a successfu
18507 6c 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 4d 6f 76  l call to.** Mov
18508 65 74 6f 55 6e 70 61 63 6b 65 64 28 29 20 74 6f  etoUnpacked() to
18509 20 73 65 65 6b 20 63 75 72 73 6f 72 20 70 43 75   seek cursor pCu
1850a 72 20 74 6f 20 28 70 4b 65 79 2c 20 6e 4b 65 79  r to (pKey, nKey
1850b 29 20 68 61 73 20 61 6c 72 65 61 64 79 0a 2a 2a  ) has already.**
1850c 20 62 65 65 6e 20 70 65 72 66 6f 72 6d 65 64 2e   been performed.
1850d 20 73 65 65 6b 52 65 73 75 6c 74 20 69 73 20 74   seekResult is t
1850e 68 65 20 73 65 61 72 63 68 20 72 65 73 75 6c 74  he search result
1850f 20 72 65 74 75 72 6e 65 64 20 28 61 20 6e 65 67   returned (a neg
18510 61 74 69 76 65 0a 2a 2a 20 6e 75 6d 62 65 72 20  ative.** number 
18511 69 66 20 70 43 75 72 20 70 6f 69 6e 74 73 20 61  if pCur points a
18512 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20  t an entry that 
18513 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  is smaller than 
18514 28 70 4b 65 79 2c 20 6e 4b 65 79 29 2c 20 6f 72  (pKey, nKey), or
18515 0a 2a 2a 20 61 20 70 6f 73 69 74 69 76 65 20 76  .** a positive v
18516 61 6c 75 65 20 69 66 20 70 43 75 72 20 70 6f 69  alue if pCur poi
18517 6e 74 73 20 61 74 20 61 6e 20 65 74 72 79 20 74  nts at an etry t
18518 68 61 74 20 69 73 20 6c 61 72 67 65 72 20 74 68  hat is larger th
18519 61 6e 20 0a 2a 2a 20 28 70 4b 65 79 2c 20 6e 4b  an .** (pKey, nK
1851a 65 79 29 29 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  ey)). .**.** If 
1851b 74 68 65 20 73 65 65 6b 52 65 73 75 6c 74 20 70  the seekResult p
1851c 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d  arameter is non-
1851d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 63  zero, then the c
1851e 61 6c 6c 65 72 20 67 75 61 72 61 6e 74 65 65 73  aller guarantees
1851f 20 74 68 61 74 0a 2a 2a 20 63 75 72 73 6f 72 20   that.** cursor 
18520 70 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67  pCur is pointing
18521 20 61 74 20 74 68 65 20 65 78 69 73 74 69 6e 67   at the existing
18522 20 63 6f 70 79 20 6f 66 20 61 20 72 6f 77 20 74   copy of a row t
18523 68 61 74 20 69 73 20 74 6f 20 62 65 0a 2a 2a 20  hat is to be.** 
18524 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20 49 66  overwritten.  If
18525 20 74 68 65 20 73 65 65 6b 52 65 73 75 6c 74 20   the seekResult 
18526 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c 20  parameter is 0, 
18527 74 68 65 6e 20 63 75 72 73 6f 72 20 70 43 75 72  then cursor pCur
18528 20 6d 61 79 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f   may.** point to
18529 20 61 6e 79 20 65 6e 74 72 79 20 6f 72 20 74 6f   any entry or to
1852a 20 6e 6f 20 65 6e 74 72 79 20 61 74 20 61 6c 6c   no entry at all
1852b 20 61 6e 64 20 73 6f 20 74 68 69 73 20 66 75 6e   and so this fun
1852c 63 74 69 6f 6e 20 68 61 73 20 74 6f 20 73 65 65  ction has to see
1852d 6b 0a 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20  k.** the cursor 
1852e 62 65 66 6f 72 65 20 74 68 65 20 6e 65 77 20 6b  before the new k
1852f 65 79 20 63 61 6e 20 62 65 20 69 6e 73 65 72 74  ey can be insert
18530 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
18531 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
18532 33 42 74 72 65 65 49 6e 73 65 72 74 28 0a 20 20  3BtreeInsert(.  
18533 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
18534 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
18535 2a 20 49 6e 73 65 72 74 20 64 61 74 61 20 69 6e  * Insert data in
18536 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f 66 20  to the table of 
18537 74 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20  this cursor */. 
18538 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
18539 79 2c 20 69 36 34 20 6e 4b 65 79 2c 20 20 20 20  y, i64 nKey,    
1853a 2f 2a 20 54 68 65 20 6b 65 79 20 6f 66 20 74 68  /* The key of th
1853b 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a  e new record */.
1853c 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44    const void *pD
1853d 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 20  ata, int nData, 
1853e 20 2f 2a 20 54 68 65 20 64 61 74 61 20 6f 66 20   /* The data of 
1853f 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a  the new record *
18540 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 20  /.  int nZero,  
18541 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18542 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
18543 65 78 74 72 61 20 30 20 62 79 74 65 73 20 74 6f  extra 0 bytes to
18544 20 61 70 70 65 6e 64 20 74 6f 20 64 61 74 61 20   append to data 
18545 2a 2f 0a 20 20 69 6e 74 20 61 70 70 65 6e 64 42  */.  int appendB
18546 69 61 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  ias,            
18547 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
18548 68 69 73 20 69 73 20 6c 69 6b 65 6c 79 20 61 6e  his is likely an
18549 20 61 70 70 65 6e 64 20 2a 2f 0a 20 20 69 6e 74   append */.  int
1854a 20 73 65 65 6b 52 65 73 75 6c 74 20 20 20 20 20   seekResult     
1854b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
1854c 65 73 75 6c 74 20 6f 66 20 70 72 69 6f 72 20 4d  esult of prior M
1854d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 29 20  ovetoUnpacked() 
1854e 63 61 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  call */.){.  int
1854f 20 72 63 3b 0a 20 20 69 6e 74 20 6c 6f 63 20 3d   rc;.  int loc =
18550 20 73 65 65 6b 52 65 73 75 6c 74 3b 20 20 20 20   seekResult;    
18551 20 20 20 20 20 20 2f 2a 20 2d 31 3a 20 62 65 66        /* -1: bef
18552 6f 72 65 20 64 65 73 69 72 65 64 20 6c 6f 63 61  ore desired loca
18553 74 69 6f 6e 20 20 2b 31 3a 20 61 66 74 65 72 20  tion  +1: after 
18554 2a 2f 0a 20 20 69 6e 74 20 73 7a 4e 65 77 3b 0a  */.  int szNew;.
18555 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 4d 65 6d    int idx;.  Mem
18556 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 42  Page *pPage;.  B
18557 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e  tree *p = pCur->
18558 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72  pBtree;.  BtShar
18559 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
1855a 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ;.  unsigned cha
1855b 72 20 2a 6f 6c 64 43 65 6c 6c 3b 0a 20 20 75 6e  r *oldCell;.  un
1855c 73 69 67 6e 65 64 20 63 68 61 72 20 2a 6e 65 77  signed char *new
1855d 43 65 6c 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28  Cell = 0;..  if(
1855e 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
1855f 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b 0a 20  URSOR_FAULT ){. 
18560 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
18561 3e 73 6b 69 70 4e 65 78 74 21 3d 53 51 4c 49 54  >skipNext!=SQLIT
18562 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 72 65 74 75  E_OK );.    retu
18563 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78  rn pCur->skipNex
18564 74 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  t;.  }..  assert
18565 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
18566 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73  ex(pCur) );.  as
18567 73 65 72 74 28 20 70 43 75 72 2d 3e 77 72 46 6c  sert( pCur->wrFl
18568 61 67 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61  ag && pBt->inTra
18569 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f  nsaction==TRANS_
1856a 57 52 49 54 45 20 26 26 20 21 70 42 74 2d 3e 72  WRITE && !pBt->r
1856b 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 73 73  eadOnly );.  ass
1856c 65 72 74 28 20 68 61 73 53 68 61 72 65 64 43 61  ert( hasSharedCa
1856d 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20  cheTableLock(p, 
1856e 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20  pCur->pgnoRoot, 
1856f 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 21 3d  pCur->pKeyInfo!=
18570 30 2c 20 32 29 20 29 3b 0a 0a 20 20 2f 2a 20 41  0, 2) );..  /* A
18571 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20 63  ssert that the c
18572 61 6c 6c 65 72 20 68 61 73 20 62 65 65 6e 20 63  aller has been c
18573 6f 6e 73 69 73 74 65 6e 74 2e 20 49 66 20 74 68  onsistent. If th
18574 69 73 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70  is cursor was op
18575 65 6e 65 64 0a 20 20 2a 2a 20 65 78 70 65 63 74  ened.  ** expect
18576 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 62 2d 74  ing an index b-t
18577 72 65 65 2c 20 74 68 65 6e 20 74 68 65 20 63 61  ree, then the ca
18578 6c 6c 65 72 20 73 68 6f 75 6c 64 20 62 65 20 69  ller should be i
18579 6e 73 65 72 74 69 6e 67 20 62 6c 6f 62 0a 20 20  nserting blob.  
1857a 2a 2a 20 6b 65 79 73 20 77 69 74 68 20 6e 6f 20  ** keys with no 
1857b 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 2e  associated data.
1857c 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 77   If the cursor w
1857d 61 73 20 6f 70 65 6e 65 64 20 65 78 70 65 63 74  as opened expect
1857e 69 6e 67 20 61 6e 0a 20 20 2a 2a 20 69 6e 74 6b  ing an.  ** intk
1857f 65 79 20 74 61 62 6c 65 2c 20 74 68 65 20 63 61  ey table, the ca
18580 6c 6c 65 72 20 73 68 6f 75 6c 64 20 62 65 20 69  ller should be i
18581 6e 73 65 72 74 69 6e 67 20 69 6e 74 65 67 65 72  nserting integer
18582 20 6b 65 79 73 20 77 69 74 68 20 61 0a 20 20 2a   keys with a.  *
18583 2a 20 62 6c 6f 62 20 6f 66 20 61 73 73 6f 63 69  * blob of associ
18584 61 74 65 64 20 64 61 74 61 2e 20 20 2a 2f 0a 20  ated data.  */. 
18585 20 61 73 73 65 72 74 28 20 28 70 4b 65 79 3d 3d   assert( (pKey==
18586 30 29 3d 3d 28 70 43 75 72 2d 3e 70 4b 65 79 49  0)==(pCur->pKeyI
18587 6e 66 6f 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a  nfo==0) );..  /*
18588 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 69   If this is an i
18589 6e 73 65 72 74 20 69 6e 74 6f 20 61 20 74 61 62  nsert into a tab
1858a 6c 65 20 62 2d 74 72 65 65 2c 20 69 6e 76 61 6c  le b-tree, inval
1858b 69 64 61 74 65 20 61 6e 79 20 69 6e 63 72 62 6c  idate any incrbl
1858c 6f 62 20 0a 20 20 2a 2a 20 63 75 72 73 6f 72 73  ob .  ** cursors
1858d 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 72 6f 77   open on the row
1858e 20 62 65 69 6e 67 20 72 65 70 6c 61 63 65 64 20   being replaced 
1858f 28 61 73 73 75 6d 69 6e 67 20 74 68 69 73 20 69  (assuming this i
18590 73 20 61 20 72 65 70 6c 61 63 65 0a 20 20 2a 2a  s a replace.  **
18591 20 6f 70 65 72 61 74 69 6f 6e 20 2d 20 69 66 20   operation - if 
18592 69 74 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 66  it is not, the f
18593 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 20 6e 6f  ollowing is a no
18594 2d 6f 70 29 2e 20 20 2a 2f 0a 20 20 69 66 28 20  -op).  */.  if( 
18595 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d  pCur->pKeyInfo==
18596 30 20 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64  0 ){.    invalid
18597 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f  ateIncrblobCurso
18598 72 73 28 70 2c 20 6e 4b 65 79 2c 20 30 29 3b 0a  rs(p, nKey, 0);.
18599 20 20 7d 0a 0a 20 20 2f 2a 20 53 61 76 65 20 74    }..  /* Save t
1859a 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20  he positions of 
1859b 61 6e 79 20 6f 74 68 65 72 20 63 75 72 73 6f 72  any other cursor
1859c 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73 20 74  s open on this t
1859d 61 62 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  able..  **.  ** 
1859e 49 6e 20 73 6f 6d 65 20 63 61 73 65 73 2c 20 74  In some cases, t
1859f 68 65 20 63 61 6c 6c 20 74 6f 20 62 74 72 65 65  he call to btree
185a0 4d 6f 76 65 74 6f 28 29 20 62 65 6c 6f 77 20 69  Moveto() below i
185a1 73 20 61 20 6e 6f 2d 6f 70 2e 20 46 6f 72 0a 20  s a no-op. For. 
185a2 20 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 77 68 65   ** example, whe
185a3 6e 20 69 6e 73 65 72 74 69 6e 67 20 64 61 74 61  n inserting data
185a4 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 77 69   into a table wi
185a5 74 68 20 61 75 74 6f 2d 67 65 6e 65 72 61 74 65  th auto-generate
185a6 64 20 69 6e 74 65 67 65 72 0a 20 20 2a 2a 20 6b  d integer.  ** k
185a7 65 79 73 2c 20 74 68 65 20 56 44 42 45 20 6c 61  eys, the VDBE la
185a8 79 65 72 20 69 6e 76 6f 6b 65 73 20 73 71 6c 69  yer invokes sqli
185a9 74 65 33 42 74 72 65 65 4c 61 73 74 28 29 20 74  te3BtreeLast() t
185aa 6f 20 66 69 67 75 72 65 20 6f 75 74 20 74 68 65  o figure out the
185ab 20 0a 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 6b   .  ** integer k
185ac 65 79 20 74 6f 20 75 73 65 2e 20 49 74 20 74 68  ey to use. It th
185ad 65 6e 20 63 61 6c 6c 73 20 74 68 69 73 20 66 75  en calls this fu
185ae 6e 63 74 69 6f 6e 20 74 6f 20 61 63 74 75 61 6c  nction to actual
185af 6c 79 20 69 6e 73 65 72 74 20 74 68 65 20 0a 20  ly insert the . 
185b0 20 2a 2a 20 64 61 74 61 20 69 6e 74 6f 20 74 68   ** data into th
185b1 65 20 69 6e 74 6b 65 79 20 42 2d 54 72 65 65 2e  e intkey B-Tree.
185b2 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 62 74   In this case bt
185b3 72 65 65 4d 6f 76 65 74 6f 28 29 20 72 65 63 6f  reeMoveto() reco
185b4 67 6e 69 7a 65 73 0a 20 20 2a 2a 20 74 68 61 74  gnizes.  ** that
185b5 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 61   the cursor is a
185b6 6c 72 65 61 64 79 20 77 68 65 72 65 20 69 74 20  lready where it 
185b7 6e 65 65 64 73 20 74 6f 20 62 65 20 61 6e 64 20  needs to be and 
185b8 72 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74 0a  returns without.
185b9 20 20 2a 2a 20 64 6f 69 6e 67 20 61 6e 79 20 77    ** doing any w
185ba 6f 72 6b 2e 20 54 6f 20 61 76 6f 69 64 20 74 68  ork. To avoid th
185bb 77 61 72 74 69 6e 67 20 74 68 65 73 65 20 6f 70  warting these op
185bc 74 69 6d 69 7a 61 74 69 6f 6e 73 2c 20 69 74 20  timizations, it 
185bd 69 73 20 69 6d 70 6f 72 74 61 6e 74 0a 20 20 2a  is important.  *
185be 2a 20 6e 6f 74 20 74 6f 20 63 6c 65 61 72 20 74  * not to clear t
185bf 68 65 20 63 75 72 73 6f 72 20 68 65 72 65 2e 0a  he cursor here..
185c0 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 61 76 65    */.  rc = save
185c1 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20  AllCursors(pBt, 
185c2 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20  pCur->pgnoRoot, 
185c3 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20  pCur);.  if( rc 
185c4 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 69  ) return rc;.  i
185c5 66 28 20 21 6c 6f 63 20 29 7b 0a 20 20 20 20 72  f( !loc ){.    r
185c6 63 20 3d 20 62 74 72 65 65 4d 6f 76 65 74 6f 28  c = btreeMoveto(
185c7 70 43 75 72 2c 20 70 4b 65 79 2c 20 6e 4b 65 79  pCur, pKey, nKey
185c8 2c 20 61 70 70 65 6e 64 42 69 61 73 2c 20 26 6c  , appendBias, &l
185c9 6f 63 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  oc);.    if( rc 
185ca 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d  ) return rc;.  }
185cb 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
185cc 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
185cd 56 41 4c 49 44 20 7c 7c 20 28 70 43 75 72 2d 3e  VALID || (pCur->
185ce 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49  eState==CURSOR_I
185cf 4e 56 41 4c 49 44 20 26 26 20 6c 6f 63 29 20 29  NVALID && loc) )
185d0 3b 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75  ;..  pPage = pCu
185d1 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
185d2 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74  iPage];.  assert
185d3 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20  ( pPage->intKey 
185d4 7c 7c 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a 20 20  || nKey>=0 );.  
185d5 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c  assert( pPage->l
185d6 65 61 66 20 7c 7c 20 21 70 50 61 67 65 2d 3e 69  eaf || !pPage->i
185d7 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 54 52 41 43  ntKey );..  TRAC
185d8 45 28 28 22 49 4e 53 45 52 54 3a 20 74 61 62 6c  E(("INSERT: tabl
185d9 65 3d 25 64 20 6e 6b 65 79 3d 25 6c 6c 64 20 6e  e=%d nkey=%lld n
185da 64 61 74 61 3d 25 64 20 70 61 67 65 3d 25 64 20  data=%d page=%d 
185db 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  %s\n",.         
185dc 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c   pCur->pgnoRoot,
185dd 20 6e 4b 65 79 2c 20 6e 44 61 74 61 2c 20 70 50   nKey, nData, pP
185de 61 67 65 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20  age->pgno,.     
185df 20 20 20 20 20 6c 6f 63 3d 3d 30 20 3f 20 22 6f       loc==0 ? "o
185e0 76 65 72 77 72 69 74 65 22 20 3a 20 22 6e 65 77  verwrite" : "new
185e1 20 65 6e 74 72 79 22 29 29 3b 0a 20 20 61 73 73   entry"));.  ass
185e2 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e  ert( pPage->isIn
185e3 69 74 20 29 3b 0a 20 20 61 6c 6c 6f 63 61 74 65  it );.  allocate
185e4 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a  TempSpace(pBt);.
185e5 20 20 6e 65 77 43 65 6c 6c 20 3d 20 70 42 74 2d    newCell = pBt-
185e6 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 69 66  >pTmpSpace;.  if
185e7 28 20 6e 65 77 43 65 6c 6c 3d 3d 30 20 29 20 72  ( newCell==0 ) r
185e8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
185e9 45 4d 3b 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49  EM;.  rc = fillI
185ea 6e 43 65 6c 6c 28 70 50 61 67 65 2c 20 6e 65 77  nCell(pPage, new
185eb 43 65 6c 6c 2c 20 70 4b 65 79 2c 20 6e 4b 65 79  Cell, pKey, nKey
185ec 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20  , pData, nData, 
185ed 6e 5a 65 72 6f 2c 20 26 73 7a 4e 65 77 29 3b 0a  nZero, &szNew);.
185ee 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
185ef 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 61 73  end_insert;.  as
185f0 73 65 72 74 28 20 73 7a 4e 65 77 3d 3d 63 65 6c  sert( szNew==cel
185f1 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20  lSizePtr(pPage, 
185f2 6e 65 77 43 65 6c 6c 29 20 29 3b 0a 20 20 61 73  newCell) );.  as
185f3 73 65 72 74 28 20 73 7a 4e 65 77 3c 3d 4d 58 5f  sert( szNew<=MX_
185f4 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29  CELL_SIZE(pBt) )
185f5 3b 0a 20 20 69 64 78 20 3d 20 70 43 75 72 2d 3e  ;.  idx = pCur->
185f6 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
185f7 65 5d 3b 0a 20 20 69 66 28 20 6c 6f 63 3d 3d 30  e];.  if( loc==0
185f8 20 29 7b 0a 20 20 20 20 75 31 36 20 73 7a 4f 6c   ){.    u16 szOl
185f9 64 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  d;.    assert( i
185fa 64 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  dx<pPage->nCell 
185fb 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
185fc 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
185fd 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  age->pDbPage);. 
185fe 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
185ff 20 20 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65     goto end_inse
18600 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6c  rt;.    }.    ol
18601 64 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c  dCell = findCell
18602 28 70 50 61 67 65 2c 20 69 64 78 29 3b 0a 20 20  (pPage, idx);.  
18603 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65    if( !pPage->le
18604 61 66 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63  af ){.      memc
18605 70 79 28 6e 65 77 43 65 6c 6c 2c 20 6f 6c 64 43  py(newCell, oldC
18606 65 6c 6c 2c 20 34 29 3b 0a 20 20 20 20 7d 0a 20  ell, 4);.    }. 
18607 20 20 20 73 7a 4f 6c 64 20 3d 20 63 65 6c 6c 53     szOld = cellS
18608 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6f 6c  izePtr(pPage, ol
18609 64 43 65 6c 6c 29 3b 0a 20 20 20 20 72 63 20 3d  dCell);.    rc =
1860a 20 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65   clearCell(pPage
1860b 2c 20 6f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 20  , oldCell);.    
1860c 64 72 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20  dropCell(pPage, 
1860d 69 64 78 2c 20 73 7a 4f 6c 64 2c 20 26 72 63 29  idx, szOld, &rc)
1860e 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67  ;.    if( rc ) g
1860f 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a  oto end_insert;.
18610 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 3c    }else if( loc<
18611 30 20 26 26 20 70 50 61 67 65 2d 3e 6e 43 65 6c  0 && pPage->nCel
18612 6c 3e 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  l>0 ){.    asser
18613 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  t( pPage->leaf )
18614 3b 0a 20 20 20 20 69 64 78 20 3d 20 2b 2b 70 43  ;.    idx = ++pC
18615 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
18616 69 50 61 67 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b  iPage];.  }else{
18617 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
18618 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 7d 0a  ge->leaf );.  }.
18619 20 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61    insertCell(pPa
1861a 67 65 2c 20 69 64 78 2c 20 6e 65 77 43 65 6c 6c  ge, idx, newCell
1861b 2c 20 73 7a 4e 65 77 2c 20 30 2c 20 30 2c 20 26  , szNew, 0, 0, &
1861c 72 63 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  rc);.  assert( r
1861d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c!=SQLITE_OK || 
1861e 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 7c  pPage->nCell>0 |
1861f 7c 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  | pPage->nOverfl
18620 6f 77 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  ow>0 );..  /* If
18621 20 6e 6f 20 65 72 72 6f 72 20 68 61 73 20 6f 63   no error has oc
18622 63 75 72 65 64 20 61 6e 64 20 70 50 61 67 65 20  cured and pPage 
18623 68 61 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20  has an overflow 
18624 63 65 6c 6c 2c 20 63 61 6c 6c 20 62 61 6c 61 6e  cell, call balan
18625 63 65 28 29 20 0a 20 20 2a 2a 20 74 6f 20 72 65  ce() .  ** to re
18626 64 69 73 74 72 69 62 75 74 65 20 74 68 65 20 63  distribute the c
18627 65 6c 6c 73 20 77 69 74 68 69 6e 20 74 68 65 20  ells within the 
18628 74 72 65 65 2e 20 53 69 6e 63 65 20 62 61 6c 61  tree. Since bala
18629 6e 63 65 28 29 20 6d 61 79 20 6d 6f 76 65 0a 20  nce() may move. 
1862a 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 2c 20   ** the cursor, 
1862b 7a 65 72 6f 20 74 68 65 20 42 74 43 75 72 73 6f  zero the BtCurso
1862c 72 2e 69 6e 66 6f 2e 6e 53 69 7a 65 20 61 6e 64  r.info.nSize and
1862d 20 42 74 43 75 72 73 6f 72 2e 76 61 6c 69 64 4e   BtCursor.validN
1862e 4b 65 79 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c  Key.  ** variabl
1862f 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 50 72  es..  **.  ** Pr
18630 65 76 69 6f 75 73 20 76 65 72 73 69 6f 6e 73 20  evious versions 
18631 6f 66 20 53 51 4c 69 74 65 20 63 61 6c 6c 65 64  of SQLite called
18632 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 29 20 74 6f   moveToRoot() to
18633 20 6d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72   move the cursor
18634 0a 20 20 2a 2a 20 62 61 63 6b 20 74 6f 20 74 68  .  ** back to th
18635 65 20 72 6f 6f 74 20 70 61 67 65 20 61 73 20 62  e root page as b
18636 61 6c 61 6e 63 65 28 29 20 75 73 65 64 20 74 6f  alance() used to
18637 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20   invalidate the 
18638 63 6f 6e 74 65 6e 74 73 0a 20 20 2a 2a 20 6f 66  contents.  ** of
18639 20 42 74 43 75 72 73 6f 72 2e 61 70 50 61 67 65   BtCursor.apPage
1863a 5b 5d 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e  [] and BtCursor.
1863b 61 69 49 64 78 5b 5d 2e 20 49 6e 73 74 65 61 64  aiIdx[]. Instead
1863c 20 6f 66 20 64 6f 69 6e 67 20 74 68 61 74 2c 0a   of doing that,.
1863d 20 20 2a 2a 20 73 65 74 20 74 68 65 20 63 75 72    ** set the cur
1863e 73 6f 72 20 73 74 61 74 65 20 74 6f 20 22 69 6e  sor state to "in
1863f 76 61 6c 69 64 22 2e 20 54 68 69 73 20 6d 61 6b  valid". This mak
18640 65 73 20 63 6f 6d 6d 6f 6e 20 69 6e 73 65 72 74  es common insert
18641 20 6f 70 65 72 61 74 69 6f 6e 73 0a 20 20 2a 2a   operations.  **
18642 20 73 6c 69 67 68 74 6c 79 20 66 61 73 74 65 72   slightly faster
18643 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 72  ..  **.  ** Ther
18644 65 20 69 73 20 61 20 73 75 62 74 6c 65 20 62 75  e is a subtle bu
18645 74 20 69 6d 70 6f 72 74 61 6e 74 20 6f 70 74 69  t important opti
18646 6d 69 7a 61 74 69 6f 6e 20 68 65 72 65 20 74 6f  mization here to
18647 6f 2e 20 57 68 65 6e 20 69 6e 73 65 72 74 69 6e  o. When insertin
18648 67 0a 20 20 2a 2a 20 6d 75 6c 74 69 70 6c 65 20  g.  ** multiple 
18649 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 6e 20  records into an 
1864a 69 6e 74 6b 65 79 20 62 2d 74 72 65 65 20 75 73  intkey b-tree us
1864b 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 63 75 72  ing a single cur
1864c 73 6f 72 20 28 61 73 20 63 61 6e 0a 20 20 2a 2a  sor (as can.  **
1864d 20 68 61 70 70 65 6e 20 77 68 69 6c 65 20 70 72   happen while pr
1864e 6f 63 65 73 73 69 6e 67 20 61 6e 20 22 49 4e 53  ocessing an "INS
1864f 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c  ERT INTO ... SEL
18650 45 43 54 22 20 73 74 61 74 65 6d 65 6e 74 29 2c  ECT" statement),
18651 20 69 74 0a 20 20 2a 2a 20 69 73 20 61 64 76 61   it.  ** is adva
18652 6e 74 61 67 65 6f 75 73 20 74 6f 20 6c 65 61 76  ntageous to leav
18653 65 20 74 68 65 20 63 75 72 73 6f 72 20 70 6f 69  e the cursor poi
18654 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73  nting to the las
18655 74 20 65 6e 74 72 79 20 69 6e 0a 20 20 2a 2a 20  t entry in.  ** 
18656 74 68 65 20 62 2d 74 72 65 65 20 69 66 20 70 6f  the b-tree if po
18657 73 73 69 62 6c 65 2e 20 49 66 20 74 68 65 20 63  ssible. If the c
18658 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f  ursor is left po
18659 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61  inting to the la
1865a 73 74 0a 20 20 2a 2a 20 65 6e 74 72 79 20 69 6e  st.  ** entry in
1865b 20 74 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 20   the table, and 
1865c 74 68 65 20 6e 65 78 74 20 72 6f 77 20 69 6e 73  the next row ins
1865d 65 72 74 65 64 20 68 61 73 20 61 6e 20 69 6e 74  erted has an int
1865e 65 67 65 72 20 6b 65 79 0a 20 20 2a 2a 20 6c 61  eger key.  ** la
1865f 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 6c 61  rger than the la
18660 72 67 65 73 74 20 65 78 69 73 74 69 6e 67 20 6b  rgest existing k
18661 65 79 2c 20 69 74 20 69 73 20 70 6f 73 73 69 62  ey, it is possib
18662 6c 65 20 74 6f 20 69 6e 73 65 72 74 20 74 68 65  le to insert the
18663 0a 20 20 2a 2a 20 72 6f 77 20 77 69 74 68 6f 75  .  ** row withou
18664 74 20 73 65 65 6b 69 6e 67 20 74 68 65 20 63 75  t seeking the cu
18665 72 73 6f 72 2e 20 54 68 69 73 20 63 61 6e 20 62  rsor. This can b
18666 65 20 61 20 62 69 67 20 70 65 72 66 6f 72 6d 61  e a big performa
18667 6e 63 65 20 62 6f 6f 73 74 2e 0a 20 20 2a 2f 0a  nce boost..  */.
18668 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69    pCur->info.nSi
18669 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e  ze = 0;.  pCur->
1866a 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20  validNKey = 0;. 
1866b 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1866c 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e 4f 76  OK && pPage->nOv
1866d 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 72 63  erflow ){.    rc
1866e 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72 29   = balance(pCur)
1866f 3b 0a 0a 20 20 20 20 2f 2a 20 4d 75 73 74 20 6d  ;..    /* Must m
18670 61 6b 65 20 73 75 72 65 20 6e 4f 76 65 72 66 6c  ake sure nOverfl
18671 6f 77 20 69 73 20 72 65 73 65 74 20 74 6f 20 7a  ow is reset to z
18672 65 72 6f 20 65 76 65 6e 20 69 66 20 74 68 65 20  ero even if the 
18673 62 61 6c 61 6e 63 65 28 29 0a 20 20 20 20 2a 2a  balance().    **
18674 20 66 61 69 6c 73 2e 20 49 6e 74 65 72 6e 61 6c   fails. Internal
18675 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20   data structure 
18676 63 6f 72 72 75 70 74 69 6f 6e 20 77 69 6c 6c 20  corruption will 
18677 72 65 73 75 6c 74 20 6f 74 68 65 72 77 69 73 65  result otherwise
18678 2e 20 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20  . .    ** Also, 
18679 73 65 74 20 74 68 65 20 63 75 72 73 6f 72 20 73  set the cursor s
1867a 74 61 74 65 20 74 6f 20 69 6e 76 61 6c 69 64 2e  tate to invalid.
1867b 20 54 68 69 73 20 73 74 6f 70 73 20 73 61 76 65   This stops save
1867c 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29  CursorPosition()
1867d 0a 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 72 79  .    ** from try
1867e 69 6e 67 20 74 6f 20 73 61 76 65 20 74 68 65 20  ing to save the 
1867f 63 75 72 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e  current position
18680 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e 20   of the cursor. 
18681 20 2a 2f 0a 20 20 20 20 70 43 75 72 2d 3e 61 70   */.    pCur->ap
18682 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
18683 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 30  ]->nOverflow = 0
18684 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61  ;.    pCur->eSta
18685 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41  te = CURSOR_INVA
18686 4c 49 44 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  LID;.  }.  asser
18687 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  t( pCur->apPage[
18688 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f  pCur->iPage]->nO
18689 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 65  verflow==0 );..e
1868a 6e 64 5f 69 6e 73 65 72 74 3a 0a 20 20 72 65 74  nd_insert:.  ret
1868b 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1868c 20 44 65 6c 65 74 65 20 74 68 65 20 65 6e 74 72   Delete the entr
1868d 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f  y that the curso
1868e 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f  r is pointing to
1868f 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a  .  The cursor.**
18690 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
18691 67 20 61 74 20 61 20 61 72 62 69 74 72 61 72 79  g at a arbitrary
18692 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51   location..*/.SQ
18693 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
18694 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c   sqlite3BtreeDel
18695 65 74 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43  ete(BtCursor *pC
18696 75 72 29 7b 0a 20 20 42 74 72 65 65 20 2a 70 20  ur){.  Btree *p 
18697 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a  = pCur->pBtree;.
18698 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
18699 3d 20 70 2d 3e 70 42 74 3b 20 20 20 20 20 20 20  = p->pBt;       
1869a 20 20 20 20 20 20 20 0a 20 20 69 6e 74 20 72 63         .  int rc
1869b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1869c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1869d 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
1869e 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
1869f 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
186a0 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
186a1 74 6f 20 64 65 6c 65 74 65 20 63 65 6c 6c 20 66  to delete cell f
186a2 72 6f 6d 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  rom */.  unsigne
186a3 64 20 63 68 61 72 20 2a 70 43 65 6c 6c 3b 20 20  d char *pCell;  
186a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
186a5 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 65 6c 6c   Pointer to cell
186a6 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20   to delete */.  
186a7 69 6e 74 20 69 43 65 6c 6c 49 64 78 3b 20 20 20  int iCellIdx;   
186a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
186a9 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
186aa 20 63 65 6c 6c 20 74 6f 20 64 65 6c 65 74 65 20   cell to delete 
186ab 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c 6c 44 65  */.  int iCellDe
186ac 70 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  pth;            
186ad 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 70            /* Dep
186ae 74 68 20 6f 66 20 6e 6f 64 65 20 63 6f 6e 74 61  th of node conta
186af 69 6e 69 6e 67 20 70 43 65 6c 6c 20 2a 2f 20 0a  ining pCell */ .
186b0 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
186b1 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
186b2 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
186b3 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
186b4 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  n==TRANS_WRITE )
186b5 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 42 74  ;.  assert( !pBt
186b6 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20  ->readOnly );.  
186b7 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 77 72  assert( pCur->wr
186b8 46 6c 61 67 20 29 3b 0a 20 20 61 73 73 65 72 74  Flag );.  assert
186b9 28 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65  ( hasSharedCache
186ba 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 70 43 75  TableLock(p, pCu
186bb 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75  r->pgnoRoot, pCu
186bc 72 2d 3e 70 4b 65 79 49 6e 66 6f 21 3d 30 2c 20  r->pKeyInfo!=0, 
186bd 32 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  2) );.  assert( 
186be 21 68 61 73 52 65 61 64 43 6f 6e 66 6c 69 63 74  !hasReadConflict
186bf 73 28 70 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52  s(p, pCur->pgnoR
186c0 6f 6f 74 29 20 29 3b 0a 0a 20 20 69 66 28 20 4e  oot) );..  if( N
186c1 45 56 45 52 28 70 43 75 72 2d 3e 61 69 49 64 78  EVER(pCur->aiIdx
186c2 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70  [pCur->iPage]>=p
186c3 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
186c4 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 29  ->iPage]->nCell)
186c5 20 0a 20 20 20 7c 7c 20 4e 45 56 45 52 28 70 43   .   || NEVER(pC
186c6 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53  ur->eState!=CURS
186c7 4f 52 5f 56 41 4c 49 44 29 0a 20 20 29 7b 0a 20  OR_VALID).  ){. 
186c8 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
186c9 5f 45 52 52 4f 52 3b 20 20 2f 2a 20 53 6f 6d 65  _ERROR;  /* Some
186ca 74 68 69 6e 67 20 68 61 73 20 67 6f 6e 65 20 61  thing has gone a
186cb 77 72 79 2e 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f  wry. */.  }..  /
186cc 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 64  * If this is a d
186cd 65 6c 65 74 65 20 6f 70 65 72 61 74 69 6f 6e 20  elete operation 
186ce 74 6f 20 72 65 6d 6f 76 65 20 61 20 72 6f 77 20  to remove a row 
186cf 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 62 2d 74  from a table b-t
186d0 72 65 65 2c 0a 20 20 2a 2a 20 69 6e 76 61 6c 69  ree,.  ** invali
186d1 64 61 74 65 20 61 6e 79 20 69 6e 63 72 62 6c 6f  date any incrblo
186d2 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f  b cursors open o
186d3 6e 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20  n the row being 
186d4 64 65 6c 65 74 65 64 2e 20 20 2a 2f 0a 20 20 69  deleted.  */.  i
186d5 66 28 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66  f( pCur->pKeyInf
186d6 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 76 61  o==0 ){.    inva
186d7 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75  lidateIncrblobCu
186d8 72 73 6f 72 73 28 70 2c 20 70 43 75 72 2d 3e 69  rsors(p, pCur->i
186d9 6e 66 6f 2e 6e 4b 65 79 2c 20 30 29 3b 0a 20 20  nfo.nKey, 0);.  
186da 7d 0a 0a 20 20 69 43 65 6c 6c 44 65 70 74 68 20  }..  iCellDepth 
186db 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20  = pCur->iPage;. 
186dc 20 69 43 65 6c 6c 49 64 78 20 3d 20 70 43 75 72   iCellIdx = pCur
186dd 2d 3e 61 69 49 64 78 5b 69 43 65 6c 6c 44 65 70  ->aiIdx[iCellDep
186de 74 68 5d 3b 0a 20 20 70 50 61 67 65 20 3d 20 70  th];.  pPage = p
186df 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 43 65 6c  Cur->apPage[iCel
186e0 6c 44 65 70 74 68 5d 3b 0a 20 20 70 43 65 6c 6c  lDepth];.  pCell
186e1 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67   = findCell(pPag
186e2 65 2c 20 69 43 65 6c 6c 49 64 78 29 3b 0a 0a 20  e, iCellIdx);.. 
186e3 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 20   /* If the page 
186e4 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 65  containing the e
186e5 6e 74 72 79 20 74 6f 20 64 65 6c 65 74 65 20 69  ntry to delete i
186e6 73 20 6e 6f 74 20 61 20 6c 65 61 66 20 70 61 67  s not a leaf pag
186e7 65 2c 20 6d 6f 76 65 0a 20 20 2a 2a 20 74 68 65  e, move.  ** the
186e8 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 6c   cursor to the l
186e9 61 72 67 65 73 74 20 65 6e 74 72 79 20 69 6e 20  argest entry in 
186ea 74 68 65 20 74 72 65 65 20 74 68 61 74 20 69 73  the tree that is
186eb 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 0a 20 20   smaller than.  
186ec 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 62 65 69  ** the entry bei
186ed 6e 67 20 64 65 6c 65 74 65 64 2e 20 54 68 69 73  ng deleted. This
186ee 20 63 65 6c 6c 20 77 69 6c 6c 20 72 65 70 6c 61   cell will repla
186ef 63 65 20 74 68 65 20 63 65 6c 6c 20 62 65 69 6e  ce the cell bein
186f0 67 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20 66  g deleted.  ** f
186f1 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  rom the internal
186f2 20 6e 6f 64 65 2e 20 54 68 65 20 27 70 72 65 76   node. The 'prev
186f3 69 6f 75 73 27 20 65 6e 74 72 79 20 69 73 20 75  ious' entry is u
186f4 73 65 64 20 66 6f 72 20 74 68 69 73 20 69 6e 73  sed for this ins
186f5 74 65 61 64 0a 20 20 2a 2a 20 6f 66 20 74 68 65  tead.  ** of the
186f6 20 27 6e 65 78 74 27 20 65 6e 74 72 79 2c 20 61   'next' entry, a
186f7 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 65  s the previous e
186f8 6e 74 72 79 20 69 73 20 61 6c 77 61 79 73 20 61  ntry is always a
186f9 20 70 61 72 74 20 6f 66 20 74 68 65 0a 20 20 2a   part of the.  *
186fa 2a 20 73 75 62 2d 74 72 65 65 20 68 65 61 64 65  * sub-tree heade
186fb 64 20 62 79 20 74 68 65 20 63 68 69 6c 64 20 70  d by the child p
186fc 61 67 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  age of the cell 
186fd 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 20 54  being deleted. T
186fe 68 69 73 20 6d 61 6b 65 73 0a 20 20 2a 2a 20 62  his makes.  ** b
186ff 61 6c 61 6e 63 69 6e 67 20 74 68 65 20 74 72 65  alancing the tre
18700 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20  e following the 
18701 64 65 6c 65 74 65 20 6f 70 65 72 61 74 69 6f 6e  delete operation
18702 20 65 61 73 69 65 72 2e 20 20 2a 2f 0a 20 20 69   easier.  */.  i
18703 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20  f( !pPage->leaf 
18704 29 7b 0a 20 20 20 20 69 6e 74 20 6e 6f 74 55 73  ){.    int notUs
18705 65 64 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  ed;.    rc = sql
18706 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75  ite3BtreePreviou
18707 73 28 70 43 75 72 2c 20 26 6e 6f 74 55 73 65 64  s(pCur, &notUsed
18708 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  );.    if( rc ) 
18709 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
1870a 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20 70 6f    /* Save the po
1870b 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f  sitions of any o
1870c 74 68 65 72 20 63 75 72 73 6f 72 73 20 6f 70 65  ther cursors ope
1870d 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 20  n on this table 
1870e 62 65 66 6f 72 65 0a 20 20 2a 2a 20 6d 61 6b 69  before.  ** maki
1870f 6e 67 20 61 6e 79 20 6d 6f 64 69 66 69 63 61 74  ng any modificat
18710 69 6f 6e 73 2e 20 4d 61 6b 65 20 74 68 65 20 70  ions. Make the p
18711 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  age containing t
18712 68 65 20 65 6e 74 72 79 20 74 6f 20 62 65 20 0a  he entry to be .
18713 20 20 2a 2a 20 64 65 6c 65 74 65 64 20 77 72 69    ** deleted wri
18714 74 61 62 6c 65 2e 20 54 68 65 6e 20 66 72 65 65  table. Then free
18715 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20 70 61   any overflow pa
18716 67 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ges associated w
18717 69 74 68 20 74 68 65 20 0a 20 20 2a 2a 20 65 6e  ith the .  ** en
18718 74 72 79 20 61 6e 64 20 66 69 6e 61 6c 6c 79 20  try and finally 
18719 72 65 6d 6f 76 65 20 74 68 65 20 63 65 6c 6c 20  remove the cell 
1871a 69 74 73 65 6c 66 20 66 72 6f 6d 20 77 69 74 68  itself from with
1871b 69 6e 20 74 68 65 20 70 61 67 65 2e 20 20 0a 20  in the page.  . 
1871c 20 2a 2f 0a 20 20 72 63 20 3d 20 73 61 76 65 41   */.  rc = saveA
1871d 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 70  llCursors(pBt, p
1871e 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70  Cur->pgnoRoot, p
1871f 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29  Cur);.  if( rc )
18720 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63   return rc;.  rc
18721 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
18722 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50  rite(pPage->pDbP
18723 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29  age);.  if( rc )
18724 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63   return rc;.  rc
18725 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70 50 61   = clearCell(pPa
18726 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 64 72  ge, pCell);.  dr
18727 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43  opCell(pPage, iC
18728 65 6c 6c 49 64 78 2c 20 63 65 6c 6c 53 69 7a 65  ellIdx, cellSize
18729 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c  Ptr(pPage, pCell
1872a 29 2c 20 26 72 63 29 3b 0a 20 20 69 66 28 20 72  ), &rc);.  if( r
1872b 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  c ) return rc;..
1872c 20 20 2f 2a 20 49 66 20 74 68 65 20 63 65 6c 6c    /* If the cell
1872d 20 64 65 6c 65 74 65 64 20 77 61 73 20 6e 6f 74   deleted was not
1872e 20 6c 6f 63 61 74 65 64 20 6f 6e 20 61 20 6c 65   located on a le
1872f 61 66 20 70 61 67 65 2c 20 74 68 65 6e 20 74 68  af page, then th
18730 65 20 63 75 72 73 6f 72 0a 20 20 2a 2a 20 69 73  e cursor.  ** is
18731 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
18732 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 72 67 65  ing to the large
18733 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  st entry in the 
18734 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64 0a  sub-tree headed.
18735 20 20 2a 2a 20 62 79 20 74 68 65 20 63 68 69 6c    ** by the chil
18736 64 2d 70 61 67 65 20 6f 66 20 74 68 65 20 63 65  d-page of the ce
18737 6c 6c 20 74 68 61 74 20 77 61 73 20 6a 75 73 74  ll that was just
18738 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 61 6e   deleted from an
18739 20 69 6e 74 65 72 6e 61 6c 0a 20 20 2a 2a 20 6e   internal.  ** n
1873a 6f 64 65 2e 20 54 68 65 20 63 65 6c 6c 20 66 72  ode. The cell fr
1873b 6f 6d 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65  om the leaf node
1873c 20 6e 65 65 64 73 20 74 6f 20 62 65 20 6d 6f 76   needs to be mov
1873d 65 64 20 74 6f 20 74 68 65 20 69 6e 74 65 72 6e  ed to the intern
1873e 61 6c 0a 20 20 2a 2a 20 6e 6f 64 65 20 74 6f 20  al.  ** node to 
1873f 72 65 70 6c 61 63 65 20 74 68 65 20 64 65 6c 65  replace the dele
18740 74 65 64 20 63 65 6c 6c 2e 20 20 2a 2f 0a 20 20  ted cell.  */.  
18741 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
18742 20 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20   ){.    MemPage 
18743 2a 70 4c 65 61 66 20 3d 20 70 43 75 72 2d 3e 61  *pLeaf = pCur->a
18744 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
18745 65 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c  e];.    int nCel
18746 6c 3b 0a 20 20 20 20 50 67 6e 6f 20 6e 20 3d 20  l;.    Pgno n = 
18747 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 43 65  pCur->apPage[iCe
18748 6c 6c 44 65 70 74 68 2b 31 5d 2d 3e 70 67 6e 6f  llDepth+1]->pgno
18749 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  ;.    unsigned c
1874a 68 61 72 20 2a 70 54 6d 70 3b 0a 0a 20 20 20 20  har *pTmp;..    
1874b 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c  pCell = findCell
1874c 28 70 4c 65 61 66 2c 20 70 4c 65 61 66 2d 3e 6e  (pLeaf, pLeaf->n
1874d 43 65 6c 6c 2d 31 29 3b 0a 20 20 20 20 6e 43 65  Cell-1);.    nCe
1874e 6c 6c 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72  ll = cellSizePtr
1874f 28 70 4c 65 61 66 2c 20 70 43 65 6c 6c 29 3b 0a  (pLeaf, pCell);.
18750 20 20 20 20 61 73 73 65 72 74 28 20 4d 58 5f 43      assert( MX_C
18751 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 3e 3d 6e  ELL_SIZE(pBt)>=n
18752 43 65 6c 6c 20 29 3b 0a 0a 20 20 20 20 61 6c 6c  Cell );..    all
18753 6f 63 61 74 65 54 65 6d 70 53 70 61 63 65 28 70  ocateTempSpace(p
18754 42 74 29 3b 0a 20 20 20 20 70 54 6d 70 20 3d 20  Bt);.    pTmp = 
18755 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a  pBt->pTmpSpace;.
18756 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
18757 33 50 61 67 65 72 57 72 69 74 65 28 70 4c 65 61  3PagerWrite(pLea
18758 66 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  f->pDbPage);.   
18759 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 67   insertCell(pPag
1875a 65 2c 20 69 43 65 6c 6c 49 64 78 2c 20 70 43 65  e, iCellIdx, pCe
1875b 6c 6c 2d 34 2c 20 6e 43 65 6c 6c 2b 34 2c 20 70  ll-4, nCell+4, p
1875c 54 6d 70 2c 20 6e 2c 20 26 72 63 29 3b 0a 20 20  Tmp, n, &rc);.  
1875d 20 20 64 72 6f 70 43 65 6c 6c 28 70 4c 65 61 66    dropCell(pLeaf
1875e 2c 20 70 4c 65 61 66 2d 3e 6e 43 65 6c 6c 2d 31  , pLeaf->nCell-1
1875f 2c 20 6e 43 65 6c 6c 2c 20 26 72 63 29 3b 0a 20  , nCell, &rc);. 
18760 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
18761 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  rn rc;.  }..  /*
18762 20 42 61 6c 61 6e 63 65 20 74 68 65 20 74 72 65   Balance the tre
18763 65 2e 20 49 66 20 74 68 65 20 65 6e 74 72 79 20  e. If the entry 
18764 64 65 6c 65 74 65 64 20 77 61 73 20 6c 6f 63 61  deleted was loca
18765 74 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 70 61  ted on a leaf pa
18766 67 65 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68  ge,.  ** then th
18767 65 20 63 75 72 73 6f 72 20 73 74 69 6c 6c 20 70  e cursor still p
18768 6f 69 6e 74 73 20 74 6f 20 74 68 61 74 20 70 61  oints to that pa
18769 67 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ge. In this case
1876a 20 74 68 65 20 66 69 72 73 74 0a 20 20 2a 2a 20   the first.  ** 
1876b 63 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 65 28  call to balance(
1876c 29 20 72 65 70 61 69 72 73 20 74 68 65 20 74 72  ) repairs the tr
1876d 65 65 2c 20 61 6e 64 20 74 68 65 20 69 66 28 2e  ee, and the if(.
1876e 2e 2e 29 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73  ..) condition is
1876f 0a 20 20 2a 2a 20 6e 65 76 65 72 20 74 72 75 65  .  ** never true
18770 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 74 68 65  ..  **.  ** Othe
18771 72 77 69 73 65 2c 20 69 66 20 74 68 65 20 65 6e  rwise, if the en
18772 74 72 79 20 64 65 6c 65 74 65 64 20 77 61 73 20  try deleted was 
18773 6f 6e 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 6e  on an internal n
18774 6f 64 65 20 70 61 67 65 2c 20 74 68 65 6e 0a 20  ode page, then. 
18775 20 2a 2a 20 70 43 75 72 20 69 73 20 70 6f 69 6e   ** pCur is poin
18776 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 65 61 66  ting to the leaf
18777 20 70 61 67 65 20 66 72 6f 6d 20 77 68 69 63 68   page from which
18778 20 61 20 63 65 6c 6c 20 77 61 73 20 72 65 6d 6f   a cell was remo
18779 76 65 64 20 74 6f 0a 20 20 2a 2a 20 72 65 70 6c  ved to.  ** repl
1877a 61 63 65 20 74 68 65 20 63 65 6c 6c 20 64 65 6c  ace the cell del
1877b 65 74 65 64 20 66 72 6f 6d 20 74 68 65 20 69 6e  eted from the in
1877c 74 65 72 6e 61 6c 20 6e 6f 64 65 2e 20 54 68 69  ternal node. Thi
1877d 73 20 69 73 20 73 6c 69 67 68 74 6c 79 0a 20 20  s is slightly.  
1877e 2a 2a 20 74 72 69 63 6b 79 20 61 73 20 74 68 65  ** tricky as the
1877f 20 6c 65 61 66 20 6e 6f 64 65 20 6d 61 79 20 62   leaf node may b
18780 65 20 75 6e 64 65 72 66 75 6c 6c 2c 20 61 6e 64  e underfull, and
18781 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f   the internal no
18782 64 65 20 6d 61 79 0a 20 20 2a 2a 20 62 65 20 65  de may.  ** be e
18783 69 74 68 65 72 20 75 6e 64 65 72 20 6f 72 20 6f  ither under or o
18784 76 65 72 66 75 6c 6c 2e 20 49 6e 20 74 68 69 73  verfull. In this
18785 20 63 61 73 65 20 72 75 6e 20 74 68 65 20 62 61   case run the ba
18786 6c 61 6e 63 69 6e 67 20 61 6c 67 6f 72 69 74 68  lancing algorith
18787 6d 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 65  m.  ** on the le
18788 61 66 20 6e 6f 64 65 20 66 69 72 73 74 2e 20 49  af node first. I
18789 66 20 74 68 65 20 62 61 6c 61 6e 63 65 20 70 72  f the balance pr
1878a 6f 63 65 65 64 73 20 66 61 72 20 65 6e 6f 75 67  oceeds far enoug
1878b 68 20 75 70 20 74 68 65 0a 20 20 2a 2a 20 74 72  h up the.  ** tr
1878c 65 65 20 74 68 61 74 20 77 65 20 63 61 6e 20 62  ee that we can b
1878d 65 20 73 75 72 65 20 74 68 61 74 20 61 6e 79 20  e sure that any 
1878e 70 72 6f 62 6c 65 6d 20 69 6e 20 74 68 65 20 69  problem in the i
1878f 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 68 61 73  nternal node has
18790 0a 20 20 2a 2a 20 62 65 65 6e 20 63 6f 72 72 65  .  ** been corre
18791 63 74 65 64 2c 20 73 6f 20 62 65 20 69 74 2e 20  cted, so be it. 
18792 4f 74 68 65 72 77 69 73 65 2c 20 61 66 74 65 72  Otherwise, after
18793 20 62 61 6c 61 6e 63 69 6e 67 20 74 68 65 20 6c   balancing the l
18794 65 61 66 20 6e 6f 64 65 2c 0a 20 20 2a 2a 20 77  eaf node,.  ** w
18795 61 6c 6b 20 74 68 65 20 63 75 72 73 6f 72 20 75  alk the cursor u
18796 70 20 74 68 65 20 74 72 65 65 20 74 6f 20 74 68  p the tree to th
18797 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20  e internal node 
18798 61 6e 64 20 62 61 6c 61 6e 63 65 20 69 74 20 61  and balance it a
18799 73 20 0a 20 20 2a 2a 20 77 65 6c 6c 2e 20 20 2a  s .  ** well.  *
1879a 2f 0a 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65  /.  rc = balance
1879b 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63  (pCur);.  if( rc
1879c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
1879d 43 75 72 2d 3e 69 50 61 67 65 3e 69 43 65 6c 6c  Cur->iPage>iCell
1879e 44 65 70 74 68 20 29 7b 0a 20 20 20 20 77 68 69  Depth ){.    whi
1879f 6c 65 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e  le( pCur->iPage>
187a0 69 43 65 6c 6c 44 65 70 74 68 20 29 7b 0a 20 20  iCellDepth ){.  
187a1 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
187a2 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
187a3 72 2d 3e 69 50 61 67 65 2d 2d 5d 29 3b 0a 20 20  r->iPage--]);.  
187a4 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 62 61 6c    }.    rc = bal
187a5 61 6e 63 65 28 70 43 75 72 29 3b 0a 20 20 7d 0a  ance(pCur);.  }.
187a6 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
187a7 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 6d 6f 76 65  E_OK ){.    move
187a8 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20  ToRoot(pCur);.  
187a9 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
187aa 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
187ab 20 6e 65 77 20 42 54 72 65 65 20 74 61 62 6c 65   new BTree table
187ac 2e 20 20 57 72 69 74 65 20 69 6e 74 6f 20 2a 70  .  Write into *p
187ad 69 54 61 62 6c 65 20 74 68 65 20 70 61 67 65 0a  iTable the page.
187ae 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  ** number for th
187af 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74  e root page of t
187b0 68 65 20 6e 65 77 20 74 61 62 6c 65 2e 0a 2a 2a  he new table..**
187b1 0a 2a 2a 20 54 68 65 20 74 79 70 65 20 6f 66 20  .** The type of 
187b2 74 79 70 65 20 69 73 20 64 65 74 65 72 6d 69 6e  type is determin
187b3 65 64 20 62 79 20 74 68 65 20 66 6c 61 67 73 20  ed by the flags 
187b4 70 61 72 61 6d 65 74 65 72 2e 20 20 4f 6e 6c 79  parameter.  Only
187b5 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e   the.** followin
187b6 67 20 76 61 6c 75 65 73 20 6f 66 20 66 6c 61 67  g values of flag
187b7 73 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20  s are currently 
187b8 69 6e 20 75 73 65 2e 20 20 4f 74 68 65 72 20 76  in use.  Other v
187b9 61 6c 75 65 73 20 66 6f 72 0a 2a 2a 20 66 6c 61  alues for.** fla
187ba 67 73 20 6d 69 67 68 74 20 6e 6f 74 20 77 6f 72  gs might not wor
187bb 6b 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 42 54 52  k:.**.**     BTR
187bc 45 45 5f 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f  EE_INTKEY|BTREE_
187bd 4c 45 41 46 44 41 54 41 20 20 20 20 20 55 73 65  LEAFDATA     Use
187be 64 20 66 6f 72 20 53 51 4c 20 74 61 62 6c 65 73  d for SQL tables
187bf 20 77 69 74 68 20 72 6f 77 69 64 20 6b 65 79 73   with rowid keys
187c0 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 5a 45  .**     BTREE_ZE
187c1 52 4f 44 41 54 41 20 20 20 20 20 20 20 20 20 20  RODATA          
187c2 20 20 20 20 20 20 20 20 55 73 65 64 20 66 6f 72          Used for
187c3 20 53 51 4c 20 69 6e 64 69 63 65 73 0a 2a 2f 0a   SQL indices.*/.
187c4 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65  static int btree
187c5 43 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65  CreateTable(Btre
187c6 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62  e *p, int *piTab
187c7 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a  le, int flags){.
187c8 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
187c9 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50  = p->pBt;.  MemP
187ca 61 67 65 20 2a 70 52 6f 6f 74 3b 0a 20 20 50 67  age *pRoot;.  Pg
187cb 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 69  no pgnoRoot;.  i
187cc 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
187cd 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
187ce 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20  ldsMutex(p) );. 
187cf 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e   assert( pBt->in
187d0 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41  Transaction==TRA
187d1 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73  NS_WRITE );.  as
187d2 73 65 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64  sert( !pBt->read
187d3 4f 6e 6c 79 20 29 3b 0a 0a 23 69 66 64 65 66 20  Only );..#ifdef 
187d4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
187d5 56 41 43 55 55 4d 0a 20 20 72 63 20 3d 20 61 6c  VACUUM.  rc = al
187d6 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28  locateBtreePage(
187d7 70 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67  pBt, &pRoot, &pg
187d8 6e 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20  noRoot, 1, 0);. 
187d9 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72   if( rc ){.    r
187da 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65  eturn rc;.  }.#e
187db 6c 73 65 0a 20 20 69 66 28 20 70 42 74 2d 3e 61  lse.  if( pBt->a
187dc 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20  utoVacuum ){.   
187dd 20 50 67 6e 6f 20 70 67 6e 6f 4d 6f 76 65 3b 20   Pgno pgnoMove; 
187de 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 61 20 70       /* Move a p
187df 61 67 65 20 68 65 72 65 20 74 6f 20 6d 61 6b 65  age here to make
187e0 20 72 6f 6f 6d 20 66 6f 72 20 74 68 65 20 72 6f   room for the ro
187e1 6f 74 2d 70 61 67 65 20 2a 2f 0a 20 20 20 20 4d  ot-page */.    M
187e2 65 6d 50 61 67 65 20 2a 70 50 61 67 65 4d 6f 76  emPage *pPageMov
187e3 65 3b 20 2f 2a 20 54 68 65 20 70 61 67 65 20 74  e; /* The page t
187e4 6f 20 6d 6f 76 65 20 74 6f 2e 20 2a 2f 0a 0a 20  o move to. */.. 
187e5 20 20 20 2f 2a 20 43 72 65 61 74 69 6e 67 20 61     /* Creating a
187e6 20 6e 65 77 20 74 61 62 6c 65 20 6d 61 79 20 70   new table may p
187e7 72 6f 62 61 62 6c 79 20 72 65 71 75 69 72 65 20  robably require 
187e8 6d 6f 76 69 6e 67 20 61 6e 20 65 78 69 73 74 69  moving an existi
187e9 6e 67 20 64 61 74 61 62 61 73 65 0a 20 20 20 20  ng database.    
187ea 2a 2a 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20  ** to make room 
187eb 66 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62 6c  for the new tabl
187ec 65 73 20 72 6f 6f 74 20 70 61 67 65 2e 20 49 6e  es root page. In
187ed 20 63 61 73 65 20 74 68 69 73 20 70 61 67 65 20   case this page 
187ee 74 75 72 6e 73 0a 20 20 20 20 2a 2a 20 6f 75 74  turns.    ** out
187ef 20 74 6f 20 62 65 20 61 6e 20 6f 76 65 72 66 6c   to be an overfl
187f0 6f 77 20 70 61 67 65 2c 20 64 65 6c 65 74 65 20  ow page, delete 
187f1 61 6c 6c 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  all overflow pag
187f2 65 2d 6d 61 70 20 63 61 63 68 65 73 0a 20 20 20  e-map caches.   
187f3 20 2a 2a 20 68 65 6c 64 20 62 79 20 6f 70 65 6e   ** held by open
187f4 20 63 75 72 73 6f 72 73 2e 0a 20 20 20 20 2a 2f   cursors..    */
187f5 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 41  .    invalidateA
187f6 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28  llOverflowCache(
187f7 70 42 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 52 65  pBt);..    /* Re
187f8 61 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ad the value of 
187f9 6d 65 74 61 5b 33 5d 20 66 72 6f 6d 20 74 68 65  meta[3] from the
187fa 20 64 61 74 61 62 61 73 65 20 74 6f 20 64 65 74   database to det
187fb 65 72 6d 69 6e 65 20 77 68 65 72 65 20 74 68 65  ermine where the
187fc 0a 20 20 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67  .    ** root pag
187fd 65 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62  e of the new tab
187fe 6c 65 20 73 68 6f 75 6c 64 20 67 6f 2e 20 6d 65  le should go. me
187ff 74 61 5b 33 5d 20 69 73 20 74 68 65 20 6c 61 72  ta[3] is the lar
18800 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20  gest root-page. 
18801 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 73 6f     ** created so
18802 20 66 61 72 2c 20 73 6f 20 74 68 65 20 6e 65 77   far, so the new
18803 20 72 6f 6f 74 2d 70 61 67 65 20 69 73 20 28 6d   root-page is (m
18804 65 74 61 5b 33 5d 2b 31 29 2e 0a 20 20 20 20 2a  eta[3]+1)..    *
18805 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  /.    sqlite3Btr
18806 65 65 47 65 74 4d 65 74 61 28 70 2c 20 42 54 52  eeGetMeta(p, BTR
18807 45 45 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54 5f  EE_LARGEST_ROOT_
18808 50 41 47 45 2c 20 26 70 67 6e 6f 52 6f 6f 74 29  PAGE, &pgnoRoot)
18809 3b 0a 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b  ;.    pgnoRoot++
1880a 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6e 65  ;..    /* The ne
1880b 77 20 72 6f 6f 74 2d 70 61 67 65 20 6d 61 79 20  w root-page may 
1880c 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64  not be allocated
1880d 20 6f 6e 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61   on a pointer-ma
1880e 70 20 70 61 67 65 2c 20 6f 72 20 74 68 65 0a 20  p page, or the. 
1880f 20 20 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59     ** PENDING_BY
18810 54 45 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a  TE page..    */.
18811 20 20 20 20 77 68 69 6c 65 28 20 70 67 6e 6f 52      while( pgnoR
18812 6f 6f 74 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45  oot==PTRMAP_PAGE
18813 4e 4f 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74  NO(pBt, pgnoRoot
18814 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 70 67 6e  ) ||.        pgn
18815 6f 52 6f 6f 74 3d 3d 50 45 4e 44 49 4e 47 5f 42  oRoot==PENDING_B
18816 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b  YTE_PAGE(pBt) ){
18817 0a 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 2b  .      pgnoRoot+
18818 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  +;.    }.    ass
18819 65 72 74 28 20 70 67 6e 6f 52 6f 6f 74 3e 3d 33  ert( pgnoRoot>=3
1881a 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f   );..    /* Allo
1881b 63 61 74 65 20 61 20 70 61 67 65 2e 20 54 68 65  cate a page. The
1881c 20 70 61 67 65 20 74 68 61 74 20 63 75 72 72 65   page that curre
1881d 6e 74 6c 79 20 72 65 73 69 64 65 73 20 61 74 20  ntly resides at 
1881e 70 67 6e 6f 52 6f 6f 74 20 77 69 6c 6c 0a 20 20  pgnoRoot will.  
1881f 20 20 2a 2a 20 62 65 20 6d 6f 76 65 64 20 74 6f    ** be moved to
18820 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 70   the allocated p
18821 61 67 65 20 28 75 6e 6c 65 73 73 20 74 68 65 20  age (unless the 
18822 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 68  allocated page h
18823 61 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20 74 6f  appens.    ** to
18824 20 72 65 73 69 64 65 20 61 74 20 70 67 6e 6f 52   reside at pgnoR
18825 6f 6f 74 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  oot)..    */.   
18826 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74   rc = allocateBt
18827 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 50  reePage(pBt, &pP
18828 61 67 65 4d 6f 76 65 2c 20 26 70 67 6e 6f 4d 6f  ageMove, &pgnoMo
18829 76 65 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 31 29  ve, pgnoRoot, 1)
1882a 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
1882b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1882c 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
1882d 7d 0a 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 4d  }..    if( pgnoM
1882e 6f 76 65 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 7b  ove!=pgnoRoot ){
1882f 0a 20 20 20 20 20 20 2f 2a 20 70 67 6e 6f 52 6f  .      /* pgnoRo
18830 6f 74 20 69 73 20 74 68 65 20 70 61 67 65 20 74  ot is the page t
18831 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64  hat will be used
18832 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 61   for the root-pa
18833 67 65 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74  ge of.      ** t
18834 68 65 20 6e 65 77 20 74 61 62 6c 65 20 28 61 73  he new table (as
18835 73 75 6d 69 6e 67 20 61 6e 20 65 72 72 6f 72 20  suming an error 
18836 64 69 64 20 6e 6f 74 20 6f 63 63 75 72 29 2e 20  did not occur). 
18837 42 75 74 20 77 65 20 77 65 72 65 0a 20 20 20 20  But we were.    
18838 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70    ** allocated p
18839 67 6e 6f 4d 6f 76 65 2e 20 49 66 20 72 65 71 75  gnoMove. If requ
1883a 69 72 65 64 20 28 69 2e 65 2e 20 69 66 20 69 74  ired (i.e. if it
1883b 20 77 61 73 20 6e 6f 74 20 61 6c 6c 6f 63 61 74   was not allocat
1883c 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 65  ed.      ** by e
1883d 78 74 65 6e 64 69 6e 67 20 74 68 65 20 66 69 6c  xtending the fil
1883e 65 29 2c 20 74 68 65 20 63 75 72 72 65 6e 74 20  e), the current 
1883f 70 61 67 65 20 61 74 20 70 6f 73 69 74 69 6f 6e  page at position
18840 20 70 67 6e 6f 4d 6f 76 65 0a 20 20 20 20 20 20   pgnoMove.      
18841 2a 2a 20 69 73 20 61 6c 72 65 61 64 79 20 6a 6f  ** is already jo
18842 75 72 6e 61 6c 65 64 2e 0a 20 20 20 20 20 20 2a  urnaled..      *
18843 2f 0a 20 20 20 20 20 20 75 38 20 65 54 79 70 65  /.      u8 eType
18844 20 3d 20 30 3b 0a 20 20 20 20 20 20 50 67 6e 6f   = 0;.      Pgno
18845 20 69 50 74 72 50 61 67 65 20 3d 20 30 3b 0a 0a   iPtrPage = 0;..
18846 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
18847 65 28 70 50 61 67 65 4d 6f 76 65 29 3b 0a 0a 20  e(pPageMove);.. 
18848 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65       /* Move the
18849 20 70 61 67 65 20 63 75 72 72 65 6e 74 6c 79 20   page currently 
1884a 61 74 20 70 67 6e 6f 52 6f 6f 74 20 74 6f 20 70  at pgnoRoot to p
1884b 67 6e 6f 4d 6f 76 65 2e 20 2a 2f 0a 20 20 20 20  gnoMove. */.    
1884c 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50    rc = btreeGetP
1884d 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f  age(pBt, pgnoRoo
1884e 74 2c 20 26 70 52 6f 6f 74 2c 20 30 29 3b 0a 20  t, &pRoot, 0);. 
1884f 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
18850 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
18851 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
18852 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20     }.      rc = 
18853 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 70  ptrmapGet(pBt, p
18854 67 6e 6f 52 6f 6f 74 2c 20 26 65 54 79 70 65 2c  gnoRoot, &eType,
18855 20 26 69 50 74 72 50 61 67 65 29 3b 0a 20 20 20   &iPtrPage);.   
18856 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54     if( eType==PT
18857 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 7c 7c  RMAP_ROOTPAGE ||
18858 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46   eType==PTRMAP_F
18859 52 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20  REEPAGE ){.     
1885a 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
1885b 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
1885c 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72     }.      if( r
1885d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1885e 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
1885f 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20  age(pRoot);.    
18860 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
18861 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73       }.      ass
18862 65 72 74 28 20 65 54 79 70 65 21 3d 50 54 52 4d  ert( eType!=PTRM
18863 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 3b 0a 20  AP_ROOTPAGE );. 
18864 20 20 20 20 20 61 73 73 65 72 74 28 20 65 54 79       assert( eTy
18865 70 65 21 3d 50 54 52 4d 41 50 5f 46 52 45 45 50  pe!=PTRMAP_FREEP
18866 41 47 45 20 29 3b 0a 20 20 20 20 20 20 72 63 20  AGE );.      rc 
18867 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28 70  = relocatePage(p
18868 42 74 2c 20 70 52 6f 6f 74 2c 20 65 54 79 70 65  Bt, pRoot, eType
18869 2c 20 69 50 74 72 50 61 67 65 2c 20 70 67 6e 6f  , iPtrPage, pgno
1886a 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Move, 0);.      
1886b 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f  releasePage(pRoo
1886c 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 62  t);..      /* Ob
1886d 74 61 69 6e 20 74 68 65 20 70 61 67 65 20 61 74  tain the page at
1886e 20 70 67 6e 6f 52 6f 6f 74 20 2a 2f 0a 20 20 20   pgnoRoot */.   
1886f 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
18870 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
18871 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
18872 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 62 74   }.      rc = bt
18873 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
18874 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f 74  pgnoRoot, &pRoot
18875 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
18876 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
18877 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
18878 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
18879 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
1887a 67 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e  gerWrite(pRoot->
1887b 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
1887c 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1887d 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c  K ){.        rel
1887e 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b  easePage(pRoot);
1887f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
18880 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
18881 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 52 6f  }else{.      pRo
18882 6f 74 20 3d 20 70 50 61 67 65 4d 6f 76 65 3b 0a  ot = pPageMove;.
18883 20 20 20 20 7d 20 0a 0a 20 20 20 20 2f 2a 20 55      } ..    /* U
18884 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65  pdate the pointe
18885 72 2d 6d 61 70 20 61 6e 64 20 6d 65 74 61 2d 64  r-map and meta-d
18886 61 74 61 20 77 69 74 68 20 74 68 65 20 6e 65 77  ata with the new
18887 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65   root-page numbe
18888 72 2e 20 2a 2f 0a 20 20 20 20 70 74 72 6d 61 70  r. */.    ptrmap
18889 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f  Put(pBt, pgnoRoo
1888a 74 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41  t, PTRMAP_ROOTPA
1888b 47 45 2c 20 30 2c 20 26 72 63 29 3b 0a 20 20 20  GE, 0, &rc);.   
1888c 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
1888d 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f   releasePage(pRo
1888e 6f 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ot);.      retur
1888f 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
18890 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
18891 65 55 70 64 61 74 65 4d 65 74 61 28 70 2c 20 34  eUpdateMeta(p, 4
18892 2c 20 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20  , pgnoRoot);.   
18893 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
18894 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f   releasePage(pRo
18895 6f 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ot);.      retur
18896 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d  n rc;.    }..  }
18897 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 61  else{.    rc = a
18898 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65  llocateBtreePage
18899 28 70 42 74 2c 20 26 70 52 6f 6f 74 2c 20 26 70  (pBt, &pRoot, &p
1889a 67 6e 6f 52 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a  gnoRoot, 1, 0);.
1889b 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
1889c 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64  urn rc;.  }.#end
1889d 69 66 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  if.  assert( sql
1889e 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
1889f 61 62 6c 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50  able(pRoot->pDbP
188a0 61 67 65 29 20 29 3b 0a 20 20 7a 65 72 6f 50 61  age) );.  zeroPa
188a1 67 65 28 70 52 6f 6f 74 2c 20 66 6c 61 67 73 20  ge(pRoot, flags 
188a2 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 73  | PTF_LEAF);.  s
188a3 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
188a4 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29  (pRoot->pDbPage)
188a5 3b 0a 20 20 2a 70 69 54 61 62 6c 65 20 3d 20 28  ;.  *piTable = (
188a6 69 6e 74 29 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20  int)pgnoRoot;.  
188a7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
188a8 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
188a9 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
188aa 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 42  reeCreateTable(B
188ab 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 2a 70 69  tree *p, int *pi
188ac 54 61 62 6c 65 2c 20 69 6e 74 20 66 6c 61 67 73  Table, int flags
188ad 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
188ae 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
188af 28 70 29 3b 0a 20 20 72 63 20 3d 20 62 74 72 65  (p);.  rc = btre
188b0 65 43 72 65 61 74 65 54 61 62 6c 65 28 70 2c 20  eCreateTable(p, 
188b1 70 69 54 61 62 6c 65 2c 20 66 6c 61 67 73 29 3b  piTable, flags);
188b2 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
188b3 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
188b4 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  n rc;.}../*.** E
188b5 72 61 73 65 20 74 68 65 20 67 69 76 65 6e 20 64  rase the given d
188b6 61 74 61 62 61 73 65 20 70 61 67 65 20 61 6e 64  atabase page and
188b7 20 61 6c 6c 20 69 74 73 20 63 68 69 6c 64 72 65   all its childre
188b8 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68  n.  Return.** th
188b9 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 66 72  e page to the fr
188ba 65 65 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69  eelist..*/.stati
188bb 63 20 69 6e 74 20 63 6c 65 61 72 44 61 74 61 62  c int clearDatab
188bc 61 73 65 50 61 67 65 28 0a 20 20 42 74 53 68 61  asePage(.  BtSha
188bd 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20  red *pBt,       
188be 20 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65      /* The BTree
188bf 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
188c0 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 50 67  he table */.  Pg
188c1 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20  no pgno,        
188c2 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62      /* Page numb
188c3 65 72 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 20  er to clear */. 
188c4 20 69 6e 74 20 66 72 65 65 50 61 67 65 46 6c 61   int freePageFla
188c5 67 2c 20 20 20 20 20 2f 2a 20 44 65 61 6c 6c 6f  g,     /* Deallo
188c6 63 61 74 65 20 70 61 67 65 20 69 66 20 74 72 75  cate page if tru
188c7 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 68  e */.  int *pnCh
188c8 61 6e 67 65 0a 29 7b 0a 20 20 4d 65 6d 50 61 67  ange.){.  MemPag
188c9 65 20 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74 20  e *pPage;.  int 
188ca 72 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  rc;.  unsigned c
188cb 68 61 72 20 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e  har *pCell;.  in
188cc 74 20 69 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  t i;..  assert( 
188cd 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
188ce 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  ld(pBt->mutex) )
188cf 3b 0a 20 20 69 66 28 20 70 67 6e 6f 3e 70 61 67  ;.  if( pgno>pag
188d0 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29  erPagecount(pBt)
188d1 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
188d2 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
188d3 50 54 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20  PT;.  }..  rc = 
188d4 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70  getAndInitPage(p
188d5 42 74 2c 20 70 67 6e 6f 2c 20 26 70 50 61 67 65  Bt, pgno, &pPage
188d6 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
188d7 74 75 72 6e 20 72 63 3b 0a 20 20 66 6f 72 28 69  turn rc;.  for(i
188d8 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65  =0; i<pPage->nCe
188d9 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 43  ll; i++){.    pC
188da 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70  ell = findCell(p
188db 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 69 66  Page, i);.    if
188dc 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
188dd 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 63 6c 65  {.      rc = cle
188de 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28 70  arDatabasePage(p
188df 42 74 2c 20 67 65 74 34 62 79 74 65 28 70 43 65  Bt, get4byte(pCe
188e0 6c 6c 29 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65  ll), 1, pnChange
188e1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20  );.      if( rc 
188e2 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61  ) goto cleardata
188e3 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20  basepage_out;.  
188e4 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 63 6c 65    }.    rc = cle
188e5 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43  arCell(pPage, pC
188e6 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ell);.    if( rc
188e7 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61 74   ) goto cleardat
188e8 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20  abasepage_out;. 
188e9 20 7d 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d   }.  if( !pPage-
188ea 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72 63 20  >leaf ){.    rc 
188eb 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 50  = clearDatabaseP
188ec 61 67 65 28 70 42 74 2c 20 67 65 74 34 62 79 74  age(pBt, get4byt
188ed 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b  e(&pPage->aData[
188ee 38 5d 29 2c 20 31 2c 20 70 6e 43 68 61 6e 67 65  8]), 1, pnChange
188ef 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  );.    if( rc ) 
188f0 67 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61  goto cleardataba
188f1 73 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 65  sepage_out;.  }e
188f2 6c 73 65 20 69 66 28 20 70 6e 43 68 61 6e 67 65  lse if( pnChange
188f3 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
188f4 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b  pPage->intKey );
188f5 0a 20 20 20 20 2a 70 6e 43 68 61 6e 67 65 20 2b  .    *pnChange +
188f6 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a  = pPage->nCell;.
188f7 20 20 7d 0a 20 20 69 66 28 20 66 72 65 65 50 61    }.  if( freePa
188f8 67 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 66 72  geFlag ){.    fr
188f9 65 65 50 61 67 65 28 70 50 61 67 65 2c 20 26 72  eePage(pPage, &r
188fa 63 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  c);.  }else if( 
188fb 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67  (rc = sqlite3Pag
188fc 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70  erWrite(pPage->p
188fd 44 62 50 61 67 65 29 29 3d 3d 30 20 29 7b 0a 20  DbPage))==0 ){. 
188fe 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67     zeroPage(pPag
188ff 65 2c 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b  e, pPage->aData[
18900 30 5d 20 7c 20 50 54 46 5f 4c 45 41 46 29 3b 0a  0] | PTF_LEAF);.
18901 20 20 7d 0a 0a 63 6c 65 61 72 64 61 74 61 62 61    }..cleardataba
18902 73 65 70 61 67 65 5f 6f 75 74 3a 0a 20 20 72 65  sepage_out:.  re
18903 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
18904 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
18905 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61  ../*.** Delete a
18906 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ll information f
18907 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 74 61 62  rom a single tab
18908 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  le in the databa
18909 73 65 2e 20 20 69 54 61 62 6c 65 20 69 73 0a 2a  se.  iTable is.*
1890a 2a 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65  * the page numbe
1890b 72 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 6f 66  r of the root of
1890c 20 74 68 65 20 74 61 62 6c 65 2e 20 20 41 66 74   the table.  Aft
1890d 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  er this routine 
1890e 72 65 74 75 72 6e 73 2c 0a 2a 2a 20 74 68 65 20  returns,.** the 
1890f 72 6f 6f 74 20 70 61 67 65 20 69 73 20 65 6d 70  root page is emp
18910 74 79 2c 20 62 75 74 20 73 74 69 6c 6c 20 65 78  ty, but still ex
18911 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ists..**.** This
18912 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 66 61   routine will fa
18913 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4c  il with SQLITE_L
18914 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65 20 61  OCKED if there a
18915 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 72  re any open.** r
18916 65 61 64 20 63 75 72 73 6f 72 73 20 6f 6e 20 74  ead cursors on t
18917 68 65 20 74 61 62 6c 65 2e 20 20 4f 70 65 6e 20  he table.  Open 
18918 77 72 69 74 65 20 63 75 72 73 6f 72 73 20 61 72  write cursors ar
18919 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a 2a  e moved to the.*
1891a 2a 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61  * root of the ta
1891b 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 6e  ble..**.** If pn
1891c 43 68 61 6e 67 65 20 69 73 20 6e 6f 74 20 4e 55  Change is not NU
1891d 4c 4c 2c 20 74 68 65 6e 20 74 61 62 6c 65 20 69  LL, then table i
1891e 54 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61 6e  Table must be an
1891f 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 2e 20 54   intkey table. T
18920 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76 61  he.** integer va
18921 6c 75 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  lue pointed to b
18922 79 20 70 6e 43 68 61 6e 67 65 20 69 73 20 69 6e  y pnChange is in
18923 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65  cremented by the
18924 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6e   number of.** en
18925 74 72 69 65 73 20 69 6e 20 74 68 65 20 74 61 62  tries in the tab
18926 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
18927 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
18928 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65  3BtreeClearTable
18929 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69  (Btree *p, int i
1892a 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 6e 43 68  Table, int *pnCh
1892b 61 6e 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b  ange){.  int rc;
1892c 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
1892d 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c   = p->pBt;.  sql
1892e 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
1892f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
18930 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57  inTrans==TRANS_W
18931 52 49 54 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e  RITE );..  /* In
18932 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 69 6e 63  validate all inc
18933 72 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 6f 70  rblob cursors op
18934 65 6e 20 6f 6e 20 74 61 62 6c 65 20 69 54 61 62  en on table iTab
18935 6c 65 20 28 61 73 73 75 6d 69 6e 67 20 69 54 61  le (assuming iTa
18936 62 6c 65 0a 20 20 2a 2a 20 69 73 20 74 68 65 20  ble.  ** is the 
18937 72 6f 6f 74 20 6f 66 20 61 20 74 61 62 6c 65 20  root of a table 
18938 62 2d 74 72 65 65 20 2d 20 69 66 20 69 74 20 69  b-tree - if it i
18939 73 20 6e 6f 74 2c 20 74 68 65 20 66 6f 6c 6c 6f  s not, the follo
1893a 77 69 6e 67 20 63 61 6c 6c 20 69 73 0a 20 20 2a  wing call is.  *
1893b 2a 20 61 20 6e 6f 2d 6f 70 29 2e 20 20 2a 2f 0a  * a no-op).  */.
1893c 20 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 72    invalidateIncr
1893d 62 6c 6f 62 43 75 72 73 6f 72 73 28 70 2c 20 30  blobCursors(p, 0
1893e 2c 20 31 29 3b 0a 0a 20 20 72 63 20 3d 20 73 61  , 1);..  rc = sa
1893f 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74  veAllCursors(pBt
18940 2c 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20  , (Pgno)iTable, 
18941 30 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45  0);.  if( SQLITE
18942 5f 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 72  _OK==rc ){.    r
18943 63 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73  c = clearDatabas
18944 65 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f  ePage(pBt, (Pgno
18945 29 69 54 61 62 6c 65 2c 20 30 2c 20 70 6e 43 68  )iTable, 0, pnCh
18946 61 6e 67 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  ange);.  }.  sql
18947 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
18948 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
18949 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 61  }../*.** Erase a
1894a 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ll information i
1894b 6e 20 61 20 74 61 62 6c 65 20 61 6e 64 20 61 64  n a table and ad
1894c 64 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68  d the root of th
1894d 65 20 74 61 62 6c 65 20 74 6f 0a 2a 2a 20 74 68  e table to.** th
1894e 65 20 66 72 65 65 6c 69 73 74 2e 20 20 45 78 63  e freelist.  Exc
1894f 65 70 74 2c 20 74 68 65 20 72 6f 6f 74 20 6f 66  ept, the root of
18950 20 74 68 65 20 70 72 69 6e 63 69 70 6c 65 20 74   the principle t
18951 61 62 6c 65 20 28 74 68 65 20 6f 6e 65 20 6f 6e  able (the one on
18952 0a 2a 2a 20 70 61 67 65 20 31 29 20 69 73 20 6e  .** page 1) is n
18953 65 76 65 72 20 61 64 64 65 64 20 74 6f 20 74 68  ever added to th
18954 65 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a  e freelist..**.*
18955 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
18956 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 53 51  ill fail with SQ
18957 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 74  LITE_LOCKED if t
18958 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f 70 65  here are any ope
18959 6e 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 6e 20  n.** cursors on 
1895a 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  the table..**.**
1895b 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d 20 69   If AUTOVACUUM i
1895c 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 74 68  s enabled and th
1895d 65 20 70 61 67 65 20 61 74 20 69 54 61 62 6c 65  e page at iTable
1895e 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61 73 74   is not the last
1895f 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 69 6e  .** root page in
18960 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
18961 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 6c 61 73  le, then the las
18962 74 20 72 6f 6f 74 20 70 61 67 65 20 0a 2a 2a 20  t root page .** 
18963 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
18964 66 69 6c 65 20 69 73 20 6d 6f 76 65 64 20 69 6e  file is moved in
18965 74 6f 20 74 68 65 20 73 6c 6f 74 20 66 6f 72 6d  to the slot form
18966 65 72 6c 79 20 6f 63 63 75 70 69 65 64 20 62 79  erly occupied by
18967 0a 2a 2a 20 69 54 61 62 6c 65 20 61 6e 64 20 74  .** iTable and t
18968 68 61 74 20 6c 61 73 74 20 73 6c 6f 74 20 66 6f  hat last slot fo
18969 72 6d 65 72 6c 79 20 6f 63 63 75 70 69 65 64 20  rmerly occupied 
1896a 62 79 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74  by the last root
1896b 20 70 61 67 65 0a 2a 2a 20 69 73 20 61 64 64 65   page.** is adde
1896c 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73  d to the freelis
1896d 74 20 69 6e 73 74 65 61 64 20 6f 66 20 69 54 61  t instead of iTa
1896e 62 6c 65 2e 20 20 49 6e 20 74 68 69 73 20 73 61  ble.  In this sa
1896f 79 2c 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70  y, all.** root p
18970 61 67 65 73 20 61 72 65 20 6b 65 70 74 20 61 74  ages are kept at
18971 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
18972 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
18973 69 6c 65 2c 20 77 68 69 63 68 0a 2a 2a 20 69 73  ile, which.** is
18974 20 6e 65 63 65 73 73 61 72 79 20 66 6f 72 20 41   necessary for A
18975 55 54 4f 56 41 43 55 55 4d 20 74 6f 20 77 6f 72  UTOVACUUM to wor
18976 6b 20 72 69 67 68 74 2e 20 20 2a 70 69 4d 6f 76  k right.  *piMov
18977 65 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  ed is set to the
18978 20 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72   .** page number
18979 20 74 68 61 74 20 75 73 65 64 20 74 6f 20 62 65   that used to be
1897a 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70   the last root p
1897b 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65 20  age in the file 
1897c 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 6d 6f  before.** the mo
1897d 76 65 2e 20 20 49 66 20 6e 6f 20 70 61 67 65 20  ve.  If no page 
1897e 67 65 74 73 20 6d 6f 76 65 64 2c 20 2a 70 69 4d  gets moved, *piM
1897f 6f 76 65 64 20 69 73 20 73 65 74 20 74 6f 20 30  oved is set to 0
18980 2e 0a 2a 2a 20 54 68 65 20 6c 61 73 74 20 72 6f  ..** The last ro
18981 6f 74 20 70 61 67 65 20 69 73 20 72 65 63 6f 72  ot page is recor
18982 64 65 64 20 69 6e 20 6d 65 74 61 5b 33 5d 20 61  ded in meta[3] a
18983 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 0a  nd the value of.
18984 2a 2a 20 6d 65 74 61 5b 33 5d 20 69 73 20 75 70  ** meta[3] is up
18985 64 61 74 65 64 20 62 79 20 74 68 69 73 20 70 72  dated by this pr
18986 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74  ocedure..*/.stat
18987 69 63 20 69 6e 74 20 62 74 72 65 65 44 72 6f 70  ic int btreeDrop
18988 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20  Table(Btree *p, 
18989 50 67 6e 6f 20 69 54 61 62 6c 65 2c 20 69 6e 74  Pgno iTable, int
1898a 20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e   *piMoved){.  in
1898b 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20  t rc;.  MemPage 
1898c 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 42 74  *pPage = 0;.  Bt
1898d 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
1898e 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28  >pBt;..  assert(
1898f 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
18990 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20  dsMutex(p) );.  
18991 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61  assert( p->inTra
18992 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20  ns==TRANS_WRITE 
18993 29 3b 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 69  );..  /* It is i
18994 6c 6c 65 67 61 6c 20 74 6f 20 64 72 6f 70 20 61  llegal to drop a
18995 20 74 61 62 6c 65 20 69 66 20 61 6e 79 20 63 75   table if any cu
18996 72 73 6f 72 73 20 61 72 65 20 6f 70 65 6e 20 6f  rsors are open o
18997 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62  n the.  ** datab
18998 61 73 65 2e 20 54 68 69 73 20 69 73 20 62 65 63  ase. This is bec
18999 61 75 73 65 20 69 6e 20 61 75 74 6f 2d 76 61 63  ause in auto-vac
1899a 75 75 6d 20 6d 6f 64 65 20 74 68 65 20 62 61 63  uum mode the bac
1899b 6b 65 6e 64 20 6d 61 79 0a 20 20 2a 2a 20 6e 65  kend may.  ** ne
1899c 65 64 20 74 6f 20 6d 6f 76 65 20 61 6e 6f 74 68  ed to move anoth
1899d 65 72 20 72 6f 6f 74 2d 70 61 67 65 20 74 6f 20  er root-page to 
1899e 66 69 6c 6c 20 61 20 67 61 70 20 6c 65 66 74 20  fill a gap left 
1899f 62 79 20 74 68 65 20 64 65 6c 65 74 65 64 0a 20  by the deleted. 
189a0 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 2e 20 49   ** root page. I
189a1 66 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f 72  f an open cursor
189a2 20 77 61 73 20 75 73 69 6e 67 20 74 68 69 73 20   was using this 
189a3 70 61 67 65 20 61 20 70 72 6f 62 6c 65 6d 20 77  page a problem w
189a4 6f 75 6c 64 20 0a 20 20 2a 2a 20 6f 63 63 75 72  ould .  ** occur
189a5 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73  ..  **.  ** This
189a6 20 65 72 72 6f 72 20 69 73 20 63 61 75 67 68 74   error is caught
189a7 20 6c 6f 6e 67 20 62 65 66 6f 72 65 20 63 6f 6e   long before con
189a8 74 72 6f 6c 20 72 65 61 63 68 65 73 20 74 68 69  trol reaches thi
189a9 73 20 70 6f 69 6e 74 2e 0a 20 20 2a 2f 0a 20 20  s point..  */.  
189aa 69 66 28 20 4e 45 56 45 52 28 70 42 74 2d 3e 70  if( NEVER(pBt->p
189ab 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 73  Cursor) ){.    s
189ac 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e  qlite3Connection
189ad 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70  Blocked(p->db, p
189ae 42 74 2d 3e 70 43 75 72 73 6f 72 2d 3e 70 42 74  Bt->pCursor->pBt
189af 72 65 65 2d 3e 64 62 29 3b 0a 20 20 20 20 72 65  ree->db);.    re
189b0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  turn SQLITE_LOCK
189b1 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a  ED_SHAREDCACHE;.
189b2 20 20 7d 0a 0a 20 20 72 63 20 3d 20 62 74 72 65    }..  rc = btre
189b3 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 28 50  eGetPage(pBt, (P
189b4 67 6e 6f 29 69 54 61 62 6c 65 2c 20 26 70 50 61  gno)iTable, &pPa
189b5 67 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  ge, 0);.  if( rc
189b6 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
189b7 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
189b8 65 43 6c 65 61 72 54 61 62 6c 65 28 70 2c 20 69  eClearTable(p, i
189b9 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 69 66 28  Table, 0);.  if(
189ba 20 72 63 20 29 7b 0a 20 20 20 20 72 65 6c 65 61   rc ){.    relea
189bb 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  sePage(pPage);. 
189bc 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
189bd 7d 0a 0a 20 20 2a 70 69 4d 6f 76 65 64 20 3d 20  }..  *piMoved = 
189be 30 3b 0a 0a 20 20 69 66 28 20 69 54 61 62 6c 65  0;..  if( iTable
189bf 3e 31 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c  >1 ){.#ifdef SQL
189c0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
189c1 55 55 4d 0a 20 20 20 20 66 72 65 65 50 61 67 65  UUM.    freePage
189c2 28 70 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20  (pPage, &rc);.  
189c3 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
189c4 61 67 65 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20  age);.#else.    
189c5 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
189c6 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 50 67 6e  uum ){.      Pgn
189c7 6f 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20  o maxRootPgno;. 
189c8 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
189c9 65 47 65 74 4d 65 74 61 28 70 2c 20 42 54 52 45  eGetMeta(p, BTRE
189ca 45 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50  E_LARGEST_ROOT_P
189cb 41 47 45 2c 20 26 6d 61 78 52 6f 6f 74 50 67 6e  AGE, &maxRootPgn
189cc 6f 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 69  o);..      if( i
189cd 54 61 62 6c 65 3d 3d 6d 61 78 52 6f 6f 74 50 67  Table==maxRootPg
189ce 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  no ){.        /*
189cf 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 62 65   If the table be
189d0 69 6e 67 20 64 72 6f 70 70 65 64 20 69 73 20 74  ing dropped is t
189d1 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 74 68  he table with th
189d2 65 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70  e largest root-p
189d3 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e  age.        ** n
189d4 75 6d 62 65 72 20 69 6e 20 74 68 65 20 64 61 74  umber in the dat
189d5 61 62 61 73 65 2c 20 70 75 74 20 74 68 65 20 72  abase, put the r
189d6 6f 6f 74 20 70 61 67 65 20 6f 6e 20 74 68 65 20  oot page on the 
189d7 66 72 65 65 20 6c 69 73 74 2e 20 0a 20 20 20 20  free list. .    
189d8 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 66      */.        f
189d9 72 65 65 50 61 67 65 28 70 50 61 67 65 2c 20 26  reePage(pPage, &
189da 72 63 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c  rc);.        rel
189db 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b  easePage(pPage);
189dc 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
189dd 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
189de 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
189df 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  c;.        }.   
189e0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
189e1 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62    /* The table b
189e2 65 69 6e 67 20 64 72 6f 70 70 65 64 20 64 6f 65  eing dropped doe
189e3 73 20 6e 6f 74 20 68 61 76 65 20 74 68 65 20 6c  s not have the l
189e4 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65  argest root-page
189e5 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62  .        ** numb
189e6 65 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  er in the databa
189e7 73 65 2e 20 53 6f 20 6d 6f 76 65 20 74 68 65 20  se. So move the 
189e8 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 20 69  page that does i
189e9 6e 74 6f 20 74 68 65 20 0a 20 20 20 20 20 20 20  nto the .       
189ea 20 2a 2a 20 67 61 70 20 6c 65 66 74 20 62 79 20   ** gap left by 
189eb 74 68 65 20 64 65 6c 65 74 65 64 20 72 6f 6f 74  the deleted root
189ec 2d 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20 2a  -page..        *
189ed 2f 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67  /.        MemPag
189ee 65 20 2a 70 4d 6f 76 65 3b 0a 20 20 20 20 20 20  e *pMove;.      
189ef 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
189f0 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 63  age);.        rc
189f1 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28   = btreeGetPage(
189f2 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f  pBt, maxRootPgno
189f3 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 20  , &pMove, 0);.  
189f4 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
189f5 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
189f6 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
189f7 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
189f8 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 50    rc = relocateP
189f9 61 67 65 28 70 42 74 2c 20 70 4d 6f 76 65 2c 20  age(pBt, pMove, 
189fa 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c  PTRMAP_ROOTPAGE,
189fb 20 30 2c 20 69 54 61 62 6c 65 2c 20 30 29 3b 0a   0, iTable, 0);.
189fc 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
189fd 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20  age(pMove);.    
189fe 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
189ff 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
18a00 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
18a01 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
18a02 70 4d 6f 76 65 20 3d 20 30 3b 0a 20 20 20 20 20  pMove = 0;.     
18a03 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74     rc = btreeGet
18a04 50 61 67 65 28 70 42 74 2c 20 6d 61 78 52 6f 6f  Page(pBt, maxRoo
18a05 74 50 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c 20 30  tPgno, &pMove, 0
18a06 29 3b 0a 20 20 20 20 20 20 20 20 66 72 65 65 50  );.        freeP
18a07 61 67 65 28 70 4d 6f 76 65 2c 20 26 72 63 29 3b  age(pMove, &rc);
18a08 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
18a09 50 61 67 65 28 70 4d 6f 76 65 29 3b 0a 20 20 20  Page(pMove);.   
18a0a 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
18a0b 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
18a0c 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
18a0d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
18a0e 20 2a 70 69 4d 6f 76 65 64 20 3d 20 6d 61 78 52   *piMoved = maxR
18a0f 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20 7d  ootPgno;.      }
18a10 0a 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74  ..      /* Set t
18a11 68 65 20 6e 65 77 20 27 6d 61 78 2d 72 6f 6f 74  he new 'max-root
18a12 2d 70 61 67 65 27 20 76 61 6c 75 65 20 69 6e 20  -page' value in 
18a13 74 68 65 20 64 61 74 61 62 61 73 65 20 68 65 61  the database hea
18a14 64 65 72 2e 20 54 68 69 73 0a 20 20 20 20 20 20  der. This.      
18a15 2a 2a 20 69 73 20 74 68 65 20 6f 6c 64 20 76 61  ** is the old va
18a16 6c 75 65 20 6c 65 73 73 20 6f 6e 65 2c 20 6c 65  lue less one, le
18a17 73 73 20 6f 6e 65 20 6d 6f 72 65 20 69 66 20 74  ss one more if t
18a18 68 61 74 20 68 61 70 70 65 6e 73 20 74 6f 0a 20  hat happens to. 
18a19 20 20 20 20 20 2a 2a 20 62 65 20 61 20 72 6f 6f       ** be a roo
18a1a 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2c 20 6c  t-page number, l
18a1b 65 73 73 20 6f 6e 65 20 61 67 61 69 6e 20 69 66  ess one again if
18a1c 20 74 68 61 74 20 69 73 20 74 68 65 0a 20 20 20   that is the.   
18a1d 20 20 20 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59     ** PENDING_BY
18a1e 54 45 5f 50 41 47 45 2e 0a 20 20 20 20 20 20 2a  TE_PAGE..      *
18a1f 2f 0a 20 20 20 20 20 20 6d 61 78 52 6f 6f 74 50  /.      maxRootP
18a20 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 77 68 69  gno--;.      whi
18a21 6c 65 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d  le( maxRootPgno=
18a22 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
18a23 47 45 28 70 42 74 29 0a 20 20 20 20 20 20 20 20  GE(pBt).        
18a24 20 20 20 20 20 7c 7c 20 50 54 52 4d 41 50 5f 49       || PTRMAP_I
18a25 53 50 41 47 45 28 70 42 74 2c 20 6d 61 78 52 6f  SPAGE(pBt, maxRo
18a26 6f 74 50 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20  otPgno) ){.     
18a27 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d     maxRootPgno--
18a28 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
18a29 61 73 73 65 72 74 28 20 6d 61 78 52 6f 6f 74 50  assert( maxRootP
18a2a 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54  gno!=PENDING_BYT
18a2b 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a 0a  E_PAGE(pBt) );..
18a2c 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
18a2d 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74  e3BtreeUpdateMet
18a2e 61 28 70 2c 20 34 2c 20 6d 61 78 52 6f 6f 74 50  a(p, 4, maxRootP
18a2f 67 6e 6f 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  gno);.    }else{
18a30 0a 20 20 20 20 20 20 66 72 65 65 50 61 67 65 28  .      freePage(
18a31 70 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20  pPage, &rc);.   
18a32 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
18a33 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  Page);.    }.#en
18a34 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  dif.  }else{.   
18a35 20 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 42 74   /* If sqlite3Bt
18a36 72 65 65 44 72 6f 70 54 61 62 6c 65 20 77 61 73  reeDropTable was
18a37 20 63 61 6c 6c 65 64 20 6f 6e 20 70 61 67 65 20   called on page 
18a38 31 2e 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 72  1..    ** This r
18a39 65 61 6c 6c 79 20 6e 65 76 65 72 20 73 68 6f 75  eally never shou
18a3a 6c 64 20 68 61 70 70 65 6e 20 65 78 63 65 70 74  ld happen except
18a3b 20 69 6e 20 61 20 63 6f 72 72 75 70 74 0a 20 20   in a corrupt.  
18a3c 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 0a    ** database. .
18a3d 20 20 20 20 2a 2f 0a 20 20 20 20 7a 65 72 6f 50      */.    zeroP
18a3e 61 67 65 28 70 50 61 67 65 2c 20 50 54 46 5f 49  age(pPage, PTF_I
18a3f 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 20 29  NTKEY|PTF_LEAF )
18a40 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67  ;.    releasePag
18a41 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20  e(pPage);.  }.  
18a42 72 65 74 75 72 6e 20 72 63 3b 20 20 0a 7d 0a 53  return rc;  .}.S
18a43 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
18a44 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72  t sqlite3BtreeDr
18a45 6f 70 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70  opTable(Btree *p
18a46 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e  , int iTable, in
18a47 74 20 2a 70 69 4d 6f 76 65 64 29 7b 0a 20 20 69  t *piMoved){.  i
18a48 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33  nt rc;.  sqlite3
18a49 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
18a4a 20 72 63 20 3d 20 62 74 72 65 65 44 72 6f 70 54   rc = btreeDropT
18a4b 61 62 6c 65 28 70 2c 20 69 54 61 62 6c 65 2c 20  able(p, iTable, 
18a4c 70 69 4d 6f 76 65 64 29 3b 0a 20 20 73 71 6c 69  piMoved);.  sqli
18a4d 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
18a4e 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
18a4f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  .../*.** This fu
18a50 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20  nction may only 
18a51 62 65 20 63 61 6c 6c 65 64 20 69 66 20 74 68 65  be called if the
18a52 20 62 2d 74 72 65 65 20 63 6f 6e 6e 65 63 74 69   b-tree connecti
18a53 6f 6e 20 61 6c 72 65 61 64 79 0a 2a 2a 20 68 61  on already.** ha
18a54 73 20 61 20 72 65 61 64 20 6f 72 20 77 72 69 74  s a read or writ
18a55 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70  e transaction op
18a56 65 6e 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  en on the databa
18a57 73 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 74  se..**.** Read t
18a58 68 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74  he meta-informat
18a59 69 6f 6e 20 6f 75 74 20 6f 66 20 61 20 64 61 74  ion out of a dat
18a5a 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4d 65 74  abase file.  Met
18a5b 61 5b 30 5d 0a 2a 2a 20 69 73 20 74 68 65 20 6e  a[0].** is the n
18a5c 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70 61  umber of free pa
18a5d 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e  ges currently in
18a5e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
18a5f 4d 65 74 61 5b 31 5d 0a 2a 2a 20 74 68 72 6f 75  Meta[1].** throu
18a60 67 68 20 6d 65 74 61 5b 31 35 5d 20 61 72 65 20  gh meta[15] are 
18a61 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73  available for us
18a62 65 20 62 79 20 68 69 67 68 65 72 20 6c 61 79 65  e by higher laye
18a63 72 73 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20  rs.  Meta[0].** 
18a64 69 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 74 68  is read-only, th
18a65 65 20 6f 74 68 65 72 73 20 61 72 65 20 72 65 61  e others are rea
18a66 64 2f 77 72 69 74 65 2e 0a 2a 2a 20 0a 2a 2a 20  d/write..** .** 
18a67 54 68 65 20 73 63 68 65 6d 61 20 6c 61 79 65 72  The schema layer
18a68 20 6e 75 6d 62 65 72 73 20 6d 65 74 61 20 76 61   numbers meta va
18a69 6c 75 65 73 20 64 69 66 66 65 72 65 6e 74 6c 79  lues differently
18a6a 2e 20 20 41 74 20 74 68 65 20 73 63 68 65 6d 61  .  At the schema
18a6b 0a 2a 2a 20 6c 61 79 65 72 20 28 61 6e 64 20 74  .** layer (and t
18a6c 68 65 20 53 65 74 43 6f 6f 6b 69 65 20 61 6e 64  he SetCookie and
18a6d 20 52 65 61 64 43 6f 6f 6b 69 65 20 6f 70 63 6f   ReadCookie opco
18a6e 64 65 73 29 20 74 68 65 20 6e 75 6d 62 65 72 20  des) the number 
18a6f 6f 66 0a 2a 2a 20 66 72 65 65 20 70 61 67 65 73  of.** free pages
18a70 20 69 73 20 6e 6f 74 20 76 69 73 69 62 6c 65 2e   is not visible.
18a71 20 20 53 6f 20 43 6f 6f 6b 69 65 5b 30 5d 20 69    So Cookie[0] i
18a72 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 4d 65  s the same as Me
18a73 74 61 5b 31 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ta[1]..*/.SQLITE
18a74 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
18a75 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74  lite3BtreeGetMet
18a76 61 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20  a(Btree *p, int 
18a77 69 64 78 2c 20 75 33 32 20 2a 70 4d 65 74 61 29  idx, u32 *pMeta)
18a78 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  {.  BtShared *pB
18a79 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73  t = p->pBt;..  s
18a7a 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
18a7b 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  (p);.  assert( p
18a7c 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f  ->inTrans>TRANS_
18a7d 4e 4f 4e 45 20 29 3b 0a 20 20 61 73 73 65 72 74  NONE );.  assert
18a7e 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65  ( SQLITE_OK==que
18a7f 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62  rySharedCacheTab
18a80 6c 65 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52  leLock(p, MASTER
18a81 5f 52 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b  _ROOT, READ_LOCK
18a82 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
18a83 42 74 2d 3e 70 50 61 67 65 31 20 29 3b 0a 20 20  Bt->pPage1 );.  
18a84 61 73 73 65 72 74 28 20 69 64 78 3e 3d 30 20 26  assert( idx>=0 &
18a85 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 0a 20 20  & idx<=15 );..  
18a86 2a 70 4d 65 74 61 20 3d 20 67 65 74 34 62 79 74  *pMeta = get4byt
18a87 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e  e(&pBt->pPage1->
18a88 61 44 61 74 61 5b 33 36 20 2b 20 69 64 78 2a 34  aData[36 + idx*4
18a89 5d 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 75 74  ]);..  /* If aut
18a8a 6f 2d 76 61 63 75 75 6d 20 69 73 20 64 69 73 61  o-vacuum is disa
18a8b 62 6c 65 64 20 69 6e 20 74 68 69 73 20 62 75 69  bled in this bui
18a8c 6c 64 20 61 6e 64 20 74 68 69 73 20 69 73 20 61  ld and this is a
18a8d 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 20 20  n auto-vacuum.  
18a8e 2a 2a 20 64 61 74 61 62 61 73 65 2c 20 6d 61 72  ** database, mar
18a8f 6b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  k the database a
18a90 73 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 2a 2f  s read-only.  */
18a91 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
18a92 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
18a93 20 69 66 28 20 69 64 78 3d 3d 42 54 52 45 45 5f   if( idx==BTREE_
18a94 4c 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47  LARGEST_ROOT_PAG
18a95 45 20 26 26 20 2a 70 4d 65 74 61 3e 30 20 29 20  E && *pMeta>0 ) 
18a96 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20  pBt->readOnly = 
18a97 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c  1;.#endif..  sql
18a98 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
18a99 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74  );.}../*.** Writ
18a9a 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69  e meta-informati
18a9b 6f 6e 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65  on back into the
18a9c 20 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74 61   database.  Meta
18a9d 5b 30 5d 20 69 73 0a 2a 2a 20 72 65 61 64 2d 6f  [0] is.** read-o
18a9e 6e 6c 79 20 61 6e 64 20 6d 61 79 20 6e 6f 74 20  nly and may not 
18a9f 62 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53  be written..*/.S
18aa0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
18aa1 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70  t sqlite3BtreeUp
18aa2 64 61 74 65 4d 65 74 61 28 42 74 72 65 65 20 2a  dateMeta(Btree *
18aa3 70 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20  p, int idx, u32 
18aa4 69 4d 65 74 61 29 7b 0a 20 20 42 74 53 68 61 72  iMeta){.  BtShar
18aa5 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
18aa6 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ;.  unsigned cha
18aa7 72 20 2a 70 50 31 3b 0a 20 20 69 6e 74 20 72 63  r *pP1;.  int rc
18aa8 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3e  ;.  assert( idx>
18aa9 3d 31 20 26 26 20 69 64 78 3c 3d 31 35 20 29 3b  =1 && idx<=15 );
18aaa 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
18aab 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72  nter(p);.  asser
18aac 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54  t( p->inTrans==T
18aad 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20  RANS_WRITE );.  
18aae 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61  assert( pBt->pPa
18aaf 67 65 31 21 3d 30 20 29 3b 0a 20 20 70 50 31 20  ge1!=0 );.  pP1 
18ab0 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61  = pBt->pPage1->a
18ab1 44 61 74 61 3b 0a 20 20 72 63 20 3d 20 73 71 6c  Data;.  rc = sql
18ab2 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
18ab3 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 50  Bt->pPage1->pDbP
18ab4 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  age);.  if( rc==
18ab5 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
18ab6 20 70 75 74 34 62 79 74 65 28 26 70 50 31 5b 33   put4byte(&pP1[3
18ab7 36 20 2b 20 69 64 78 2a 34 5d 2c 20 69 4d 65 74  6 + idx*4], iMet
18ab8 61 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  a);.#ifndef SQLI
18ab9 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
18aba 55 4d 0a 20 20 20 20 69 66 28 20 69 64 78 3d 3d  UM.    if( idx==
18abb 42 54 52 45 45 5f 49 4e 43 52 5f 56 41 43 55 55  BTREE_INCR_VACUU
18abc 4d 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  M ){.      asser
18abd 74 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  t( pBt->autoVacu
18abe 75 6d 20 7c 7c 20 69 4d 65 74 61 3d 3d 30 20 29  um || iMeta==0 )
18abf 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
18ac0 69 4d 65 74 61 3d 3d 30 20 7c 7c 20 69 4d 65 74  iMeta==0 || iMet
18ac1 61 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 70 42  a==1 );.      pB
18ac2 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20  t->incrVacuum = 
18ac3 28 75 38 29 69 4d 65 74 61 3b 0a 20 20 20 20 7d  (u8)iMeta;.    }
18ac4 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 73 71  .#endif.  }.  sq
18ac5 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
18ac6 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
18ac7 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
18ac8 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45 43 4f 55  TE_OMIT_BTREECOU
18ac9 4e 54 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69 72  NT./*.** The fir
18aca 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 70 43 75  st argument, pCu
18acb 72 2c 20 69 73 20 61 20 63 75 72 73 6f 72 20 6f  r, is a cursor o
18acc 70 65 6e 65 64 20 6f 6e 20 73 6f 6d 65 20 62 2d  pened on some b-
18acd 74 72 65 65 2e 20 43 6f 75 6e 74 20 74 68 65 0a  tree. Count the.
18ace 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74  ** number of ent
18acf 72 69 65 73 20 69 6e 20 74 68 65 20 62 2d 74 72  ries in the b-tr
18ad0 65 65 20 61 6e 64 20 77 72 69 74 65 20 74 68 65  ee and write the
18ad1 20 72 65 73 75 6c 74 20 74 6f 20 2a 70 6e 45 6e   result to *pnEn
18ad2 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54  try..**.** SQLIT
18ad3 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
18ad4 20 69 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f   if the operatio
18ad5 6e 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c  n is successfull
18ad6 79 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20  y executed. .** 
18ad7 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e  Otherwise, if an
18ad8 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e   error is encoun
18ad9 74 65 72 65 64 20 28 69 2e 65 2e 20 61 6e 20 49  tered (i.e. an I
18ada 4f 20 65 72 72 6f 72 20 6f 72 20 64 61 74 61 62  O error or datab
18adb 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f  ase.** corruptio
18adc 6e 29 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72  n) an SQLite err
18add 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
18ade 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ned..*/.SQLITE_P
18adf 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
18ae0 65 33 42 74 72 65 65 43 6f 75 6e 74 28 42 74 43  e3BtreeCount(BtC
18ae1 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 34  ursor *pCur, i64
18ae2 20 2a 70 6e 45 6e 74 72 79 29 7b 0a 20 20 69 36   *pnEntry){.  i6
18ae3 34 20 6e 45 6e 74 72 79 20 3d 20 30 3b 20 20 20  4 nEntry = 0;   
18ae4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18ae5 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72     /* Value to r
18ae6 65 74 75 72 6e 20 69 6e 20 2a 70 6e 45 6e 74 72  eturn in *pnEntr
18ae7 79 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  y */.  int rc;  
18ae8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18ae9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
18aea 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
18aeb 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28  rc = moveToRoot(
18aec 70 43 75 72 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c  pCur);..  /* Unl
18aed 65 73 73 20 61 6e 20 65 72 72 6f 72 20 6f 63 63  ess an error occ
18aee 75 72 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69  urs, the followi
18aef 6e 67 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 65  ng loop runs one
18af0 20 69 74 65 72 61 74 69 6f 6e 20 66 6f 72 20 65   iteration for e
18af1 61 63 68 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e  ach.  ** page in
18af2 20 74 68 65 20 42 2d 54 72 65 65 20 73 74 72 75   the B-Tree stru
18af3 63 74 75 72 65 20 28 6e 6f 74 20 69 6e 63 6c 75  cture (not inclu
18af4 64 69 6e 67 20 6f 76 65 72 66 6c 6f 77 20 70 61  ding overflow pa
18af5 67 65 73 29 2e 20 0a 20 20 2a 2f 0a 20 20 77 68  ges). .  */.  wh
18af6 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
18af7 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 49  OK ){.    int iI
18af8 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
18af9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
18afa 49 6e 64 65 78 20 6f 66 20 63 68 69 6c 64 20 6e  Index of child n
18afb 6f 64 65 20 69 6e 20 70 61 72 65 6e 74 20 2a 2f  ode in parent */
18afc 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  .    MemPage *pP
18afd 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
18afe 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
18aff 6e 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 62  nt page of the b
18b00 2d 74 72 65 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a  -tree */..    /*
18b01 20 49 66 20 74 68 69 73 20 69 73 20 61 20 6c 65   If this is a le
18b02 61 66 20 70 61 67 65 20 6f 72 20 74 68 65 20 74  af page or the t
18b03 72 65 65 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e  ree is not an in
18b04 74 2d 6b 65 79 20 74 72 65 65 2c 20 74 68 65 6e  t-key tree, then
18b05 20 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 70 61   .    ** this pa
18b06 67 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 75 6e  ge contains coun
18b07 74 61 62 6c 65 20 65 6e 74 72 69 65 73 2e 20 49  table entries. I
18b08 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 65 6e 74  ncrement the ent
18b09 72 79 20 63 6f 75 6e 74 65 72 0a 20 20 20 20 2a  ry counter.    *
18b0a 2a 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 20  * accordingly.. 
18b0b 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 20     */.    pPage 
18b0c 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  = pCur->apPage[p
18b0d 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20  Cur->iPage];.   
18b0e 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66   if( pPage->leaf
18b0f 20 7c 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b   || !pPage->intK
18b10 65 79 20 29 7b 0a 20 20 20 20 20 20 6e 45 6e 74  ey ){.      nEnt
18b11 72 79 20 2b 3d 20 70 50 61 67 65 2d 3e 6e 43 65  ry += pPage->nCe
18b12 6c 6c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ll;.    }..    /
18b13 2a 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 61  * pPage is a lea
18b14 66 20 6e 6f 64 65 2e 20 54 68 69 73 20 6c 6f 6f  f node. This loo
18b15 70 20 6e 61 76 69 67 61 74 65 73 20 74 68 65 20  p navigates the 
18b16 63 75 72 73 6f 72 20 73 6f 20 74 68 61 74 20 69  cursor so that i
18b17 74 20 0a 20 20 20 20 2a 2a 20 70 6f 69 6e 74 73  t .    ** points
18b18 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 69 6e   to the first in
18b19 74 65 72 69 6f 72 20 63 65 6c 6c 20 74 68 61 74  terior cell that
18b1a 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68   it points to th
18b1b 65 20 70 61 72 65 6e 74 20 6f 66 0a 20 20 20 20  e parent of.    
18b1c 2a 2a 20 74 68 65 20 6e 65 78 74 20 70 61 67 65  ** the next page
18b1d 20 69 6e 20 74 68 65 20 74 72 65 65 20 74 68 61   in the tree tha
18b1e 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65  t has not yet be
18b1f 65 6e 20 76 69 73 69 74 65 64 2e 20 54 68 65 0a  en visited. The.
18b20 20 20 20 20 2a 2a 20 70 43 75 72 2d 3e 61 69 49      ** pCur->aiI
18b21 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20  dx[pCur->iPage] 
18b22 76 61 6c 75 65 20 69 73 20 73 65 74 20 74 6f 20  value is set to 
18b23 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
18b24 20 70 61 72 65 6e 74 20 63 65 6c 6c 0a 20 20 20   parent cell.   
18b25 20 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 2c   ** of the page,
18b26 20 6f 72 20 74 6f 20 74 68 65 20 6e 75 6d 62 65   or to the numbe
18b27 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 74 68  r of cells in th
18b28 65 20 70 61 67 65 20 69 66 20 74 68 65 20 6e 65  e page if the ne
18b29 78 74 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 74  xt page.    ** t
18b2a 6f 20 76 69 73 69 74 20 69 73 20 74 68 65 20 72  o visit is the r
18b2b 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 69 74  ight-child of it
18b2c 73 20 70 61 72 65 6e 74 2e 0a 20 20 20 20 2a 2a  s parent..    **
18b2d 0a 20 20 20 20 2a 2a 20 49 66 20 61 6c 6c 20 70  .    ** If all p
18b2e 61 67 65 73 20 69 6e 20 74 68 65 20 74 72 65 65  ages in the tree
18b2f 20 68 61 76 65 20 62 65 65 6e 20 76 69 73 69 74   have been visit
18b30 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ed, return SQLIT
18b31 45 5f 4f 4b 20 74 6f 20 74 68 65 0a 20 20 20 20  E_OK to the.    
18b32 2a 2a 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20 2a  ** caller..    *
18b33 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d  /.    if( pPage-
18b34 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 64  >leaf ){.      d
18b35 6f 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  o {.        if( 
18b36 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29  pCur->iPage==0 )
18b37 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  {.          /* A
18b38 6c 6c 20 70 61 67 65 73 20 6f 66 20 74 68 65 20  ll pages of the 
18b39 62 2d 74 72 65 65 20 68 61 76 65 20 62 65 65 6e  b-tree have been
18b3a 20 76 69 73 69 74 65 64 2e 20 52 65 74 75 72 6e   visited. Return
18b3b 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 2a   successfully. *
18b3c 2f 0a 20 20 20 20 20 20 20 20 20 20 2a 70 6e 45  /.          *pnE
18b3d 6e 74 72 79 20 3d 20 6e 45 6e 74 72 79 3b 0a 20  ntry = nEntry;. 
18b3e 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
18b3f 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
18b40 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6d 6f 76     }.        mov
18b41 65 54 6f 50 61 72 65 6e 74 28 70 43 75 72 29 3b  eToParent(pCur);
18b42 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 20 28 20  .      }while ( 
18b43 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
18b44 2d 3e 69 50 61 67 65 5d 3e 3d 70 43 75 72 2d 3e  ->iPage]>=pCur->
18b45 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
18b46 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20  ge]->nCell );.. 
18b47 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78       pCur->aiIdx
18b48 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2b 2b 3b  [pCur->iPage]++;
18b49 0a 20 20 20 20 20 20 70 50 61 67 65 20 3d 20 70  .      pPage = p
18b4a 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
18b4b 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 7d 0a  ->iPage];.    }.
18b4c 0a 20 20 20 20 2f 2a 20 44 65 73 63 65 6e 64 20  .    /* Descend 
18b4d 74 6f 20 74 68 65 20 63 68 69 6c 64 20 6e 6f 64  to the child nod
18b4e 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74 68  e of the cell th
18b4f 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 63 75  at the cursor cu
18b50 72 72 65 6e 74 6c 79 20 0a 20 20 20 20 2a 2a 20  rrently .    ** 
18b51 70 6f 69 6e 74 73 20 61 74 2e 20 54 68 69 73 20  points at. This 
18b52 69 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69  is the right-chi
18b53 6c 64 20 69 66 20 28 69 49 64 78 3d 3d 70 50 61  ld if (iIdx==pPa
18b54 67 65 2d 3e 6e 43 65 6c 6c 29 2e 0a 20 20 20 20  ge->nCell)..    
18b55 2a 2f 0a 20 20 20 20 69 49 64 78 20 3d 20 70 43  */.    iIdx = pC
18b56 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
18b57 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 20  iPage];.    if( 
18b58 69 49 64 78 3d 3d 70 50 61 67 65 2d 3e 6e 43 65  iIdx==pPage->nCe
18b59 6c 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ll ){.      rc =
18b5a 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75   moveToChild(pCu
18b5b 72 2c 20 67 65 74 34 62 79 74 65 28 26 70 50 61  r, get4byte(&pPa
18b5c 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d  ge->aData[pPage-
18b5d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b  >hdrOffset+8]));
18b5e 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
18b5f 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69    rc = moveToChi
18b60 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62 79 74  ld(pCur, get4byt
18b61 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  e(findCell(pPage
18b62 2c 20 69 49 64 78 29 29 29 3b 0a 20 20 20 20 7d  , iIdx)));.    }
18b63 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6e 20 65 72  .  }..  /* An er
18b64 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64  ror has occurred
18b65 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  . Return an erro
18b66 72 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 72 65 74  r code. */.  ret
18b67 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66  urn rc;.}.#endif
18b68 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
18b69 68 65 20 70 61 67 65 72 20 61 73 73 6f 63 69 61  he pager associa
18b6a 74 65 64 20 77 69 74 68 20 61 20 42 54 72 65 65  ted with a BTree
18b6b 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
18b6c 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74  is used for.** t
18b6d 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67  esting and debug
18b6e 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51  ging only..*/.SQ
18b6f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 61 67  LITE_PRIVATE Pag
18b70 65 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  er *sqlite3Btree
18b71 50 61 67 65 72 28 42 74 72 65 65 20 2a 70 29 7b  Pager(Btree *p){
18b72 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74  .  return p->pBt
18b73 2d 3e 70 50 61 67 65 72 3b 0a 7d 0a 0a 23 69 66  ->pPager;.}..#if
18b74 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
18b75 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b  _INTEGRITY_CHECK
18b76 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20  ./*.** Append a 
18b77 6d 65 73 73 61 67 65 20 74 6f 20 74 68 65 20 65  message to the e
18b78 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72  rror message str
18b79 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ing..*/.static v
18b7a 6f 69 64 20 63 68 65 63 6b 41 70 70 65 6e 64 4d  oid checkAppendM
18b7b 73 67 28 0a 20 20 49 6e 74 65 67 72 69 74 79 43  sg(.  IntegrityC
18b7c 6b 20 2a 70 43 68 65 63 6b 2c 0a 20 20 63 68 61  k *pCheck,.  cha
18b7d 72 20 2a 7a 4d 73 67 31 2c 0a 20 20 63 6f 6e 73  r *zMsg1,.  cons
18b7e 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
18b7f 0a 20 20 2e 2e 2e 0a 29 7b 0a 20 20 76 61 5f 6c  .  ....){.  va_l
18b80 69 73 74 20 61 70 3b 0a 20 20 69 66 28 20 21 70  ist ap;.  if( !p
18b81 43 68 65 63 6b 2d 3e 6d 78 45 72 72 20 29 20 72  Check->mxErr ) r
18b82 65 74 75 72 6e 3b 0a 20 20 70 43 68 65 63 6b 2d  eturn;.  pCheck-
18b83 3e 6d 78 45 72 72 2d 2d 3b 0a 20 20 70 43 68 65  >mxErr--;.  pChe
18b84 63 6b 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 76 61  ck->nErr++;.  va
18b85 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d  _start(ap, zForm
18b86 61 74 29 3b 0a 20 20 69 66 28 20 70 43 68 65 63  at);.  if( pChec
18b87 6b 2d 3e 65 72 72 4d 73 67 2e 6e 43 68 61 72 20  k->errMsg.nChar 
18b88 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74  ){.    sqlite3St
18b89 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26 70 43  rAccumAppend(&pC
18b8a 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 22 5c  heck->errMsg, "\
18b8b 6e 22 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66  n", 1);.  }.  if
18b8c 28 20 7a 4d 73 67 31 20 29 7b 0a 20 20 20 20 73  ( zMsg1 ){.    s
18b8d 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70  qlite3StrAccumAp
18b8e 70 65 6e 64 28 26 70 43 68 65 63 6b 2d 3e 65 72  pend(&pCheck->er
18b8f 72 4d 73 67 2c 20 7a 4d 73 67 31 2c 20 2d 31 29  rMsg, zMsg1, -1)
18b90 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
18b91 58 50 72 69 6e 74 66 28 26 70 43 68 65 63 6b 2d  XPrintf(&pCheck-
18b92 3e 65 72 72 4d 73 67 2c 20 31 2c 20 7a 46 6f 72  >errMsg, 1, zFor
18b93 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65  mat, ap);.  va_e
18b94 6e 64 28 61 70 29 3b 0a 20 20 69 66 28 20 70 43  nd(ap);.  if( pC
18b95 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2e 6d 61 6c  heck->errMsg.mal
18b96 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
18b97 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46   pCheck->mallocF
18b98 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d  ailed = 1;.  }.}
18b99 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
18b9a 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59  E_OMIT_INTEGRITY
18b9b 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64  _CHECK */..#ifnd
18b9c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
18b9d 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f  NTEGRITY_CHECK./
18b9e 2a 0a 2a 2a 20 41 64 64 20 31 20 74 6f 20 74 68  *.** Add 1 to th
18b9f 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
18ba0 74 20 66 6f 72 20 70 61 67 65 20 69 50 61 67 65  t for page iPage
18ba1 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 74 68  .  If this is th
18ba2 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 72 65 66 65  e second.** refe
18ba3 72 65 6e 63 65 20 74 6f 20 74 68 65 20 70 61 67  rence to the pag
18ba4 65 2c 20 61 64 64 20 61 6e 20 65 72 72 6f 72 20  e, add an error 
18ba5 6d 65 73 73 61 67 65 20 74 6f 20 70 43 68 65 63  message to pChec
18ba6 6b 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 20 52  k->zErrMsg..** R
18ba7 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 72 65  eturn 1 if there
18ba8 20 61 72 65 20 32 20 6f 72 65 20 6d 6f 72 65 20   are 2 ore more 
18ba9 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68  references to th
18baa 65 20 70 61 67 65 20 61 6e 64 20 30 20 69 66 0a  e page and 0 if.
18bab 2a 2a 20 69 66 20 74 68 69 73 20 69 73 20 74 68  ** if this is th
18bac 65 20 66 69 72 73 74 20 72 65 66 65 72 65 6e 63  e first referenc
18bad 65 20 74 6f 20 74 68 65 20 70 61 67 65 2e 0a 2a  e to the page..*
18bae 2a 0a 2a 2a 20 41 6c 73 6f 20 63 68 65 63 6b 20  *.** Also check 
18baf 74 68 61 74 20 74 68 65 20 70 61 67 65 20 6e 75  that the page nu
18bb0 6d 62 65 72 20 69 73 20 69 6e 20 62 6f 75 6e 64  mber is in bound
18bb1 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
18bb2 20 63 68 65 63 6b 52 65 66 28 49 6e 74 65 67 72   checkRef(Integr
18bb3 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 50  ityCk *pCheck, P
18bb4 67 6e 6f 20 69 50 61 67 65 2c 20 63 68 61 72 20  gno iPage, char 
18bb5 2a 7a 43 6f 6e 74 65 78 74 29 7b 0a 20 20 69 66  *zContext){.  if
18bb6 28 20 69 50 61 67 65 3d 3d 30 20 29 20 72 65 74  ( iPage==0 ) ret
18bb7 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 69 50 61  urn 1;.  if( iPa
18bb8 67 65 3e 70 43 68 65 63 6b 2d 3e 6e 50 61 67 65  ge>pCheck->nPage
18bb9 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70   ){.    checkApp
18bba 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a  endMsg(pCheck, z
18bbb 43 6f 6e 74 65 78 74 2c 20 22 69 6e 76 61 6c 69  Context, "invali
18bbc 64 20 70 61 67 65 20 6e 75 6d 62 65 72 20 25 64  d page number %d
18bbd 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 72  ", iPage);.    r
18bbe 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69  eturn 1;.  }.  i
18bbf 66 28 20 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66  f( pCheck->anRef
18bc0 5b 69 50 61 67 65 5d 3d 3d 31 20 29 7b 0a 20 20  [iPage]==1 ){.  
18bc1 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67    checkAppendMsg
18bc2 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78  (pCheck, zContex
18bc3 74 2c 20 22 32 6e 64 20 72 65 66 65 72 65 6e 63  t, "2nd referenc
18bc4 65 20 74 6f 20 70 61 67 65 20 25 64 22 2c 20 69  e to page %d", i
18bc5 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72  Page);.    retur
18bc6 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 1;.  }.  retur
18bc7 6e 20 20 28 70 43 68 65 63 6b 2d 3e 61 6e 52 65  n  (pCheck->anRe
18bc8 66 5b 69 50 61 67 65 5d 2b 2b 29 3e 31 3b 0a 7d  f[iPage]++)>1;.}
18bc9 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
18bca 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
18bcb 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61  ./*.** Check tha
18bcc 74 20 74 68 65 20 65 6e 74 72 79 20 69 6e 20 74  t the entry in t
18bcd 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 66  he pointer-map f
18bce 6f 72 20 70 61 67 65 20 69 43 68 69 6c 64 20 6d  or page iChild m
18bcf 61 70 73 20 74 6f 20 0a 2a 2a 20 70 61 67 65 20  aps to .** page 
18bd0 69 50 61 72 65 6e 74 2c 20 70 6f 69 6e 74 65 72  iParent, pointer
18bd1 20 74 79 70 65 20 70 74 72 54 79 70 65 2e 20 49   type ptrType. I
18bd2 66 20 6e 6f 74 2c 20 61 70 70 65 6e 64 20 61 6e  f not, append an
18bd3 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a   error message.*
18bd4 2a 20 74 6f 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a  * to pCheck..*/.
18bd5 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 63  static void chec
18bd6 6b 50 74 72 6d 61 70 28 0a 20 20 49 6e 74 65 67  kPtrmap(.  Integ
18bd7 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20  rityCk *pCheck, 
18bd8 20 20 2f 2a 20 49 6e 74 65 67 72 69 74 79 20 63    /* Integrity c
18bd9 68 65 63 6b 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  heck context */.
18bda 20 20 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20    Pgno iChild,  
18bdb 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 69 6c           /* Chil
18bdc 64 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f  d page number */
18bdd 0a 20 20 75 38 20 65 54 79 70 65 2c 20 20 20 20  .  u8 eType,    
18bde 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
18bdf 65 63 74 65 64 20 70 6f 69 6e 74 65 72 20 6d 61  ected pointer ma
18be0 70 20 74 79 70 65 20 2a 2f 0a 20 20 50 67 6e 6f  p type */.  Pgno
18be1 20 69 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20   iParent,       
18be2 20 20 20 2f 2a 20 45 78 70 65 63 74 65 64 20 70     /* Expected p
18be3 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 72 65 6e  ointer map paren
18be4 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f  t page number */
18be5 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78  .  char *zContex
18be6 74 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  t         /* Con
18be7 74 65 78 74 20 64 65 73 63 72 69 70 74 69 6f 6e  text description
18be8 20 28 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72   (used for error
18be9 20 6d 73 67 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e   msg) */.){.  in
18bea 74 20 72 63 3b 0a 20 20 75 38 20 65 50 74 72 6d  t rc;.  u8 ePtrm
18beb 61 70 54 79 70 65 3b 0a 20 20 50 67 6e 6f 20 69  apType;.  Pgno i
18bec 50 74 72 6d 61 70 50 61 72 65 6e 74 3b 0a 0a 20  PtrmapParent;.. 
18bed 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28   rc = ptrmapGet(
18bee 70 43 68 65 63 6b 2d 3e 70 42 74 2c 20 69 43 68  pCheck->pBt, iCh
18bef 69 6c 64 2c 20 26 65 50 74 72 6d 61 70 54 79 70  ild, &ePtrmapTyp
18bf0 65 2c 20 26 69 50 74 72 6d 61 70 50 61 72 65 6e  e, &iPtrmapParen
18bf1 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  t);.  if( rc!=SQ
18bf2 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
18bf3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
18bf4 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  MEM || rc==SQLIT
18bf5 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20  E_IOERR_NOMEM ) 
18bf6 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61  pCheck->mallocFa
18bf7 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 63 68  iled = 1;.    ch
18bf8 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68  eckAppendMsg(pCh
18bf9 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22  eck, zContext, "
18bfa 46 61 69 6c 65 64 20 74 6f 20 72 65 61 64 20 70  Failed to read p
18bfb 74 72 6d 61 70 20 6b 65 79 3d 25 64 22 2c 20 69  trmap key=%d", i
18bfc 43 68 69 6c 64 29 3b 0a 20 20 20 20 72 65 74 75  Child);.    retu
18bfd 72 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 65  rn;.  }..  if( e
18bfe 50 74 72 6d 61 70 54 79 70 65 21 3d 65 54 79 70  PtrmapType!=eTyp
18bff 65 20 7c 7c 20 69 50 74 72 6d 61 70 50 61 72 65  e || iPtrmapPare
18c00 6e 74 21 3d 69 50 61 72 65 6e 74 20 29 7b 0a 20  nt!=iParent ){. 
18c01 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
18c02 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65  g(pCheck, zConte
18c03 78 74 2c 20 0a 20 20 20 20 20 20 22 42 61 64 20  xt, .      "Bad 
18c04 70 74 72 20 6d 61 70 20 65 6e 74 72 79 20 6b 65  ptr map entry ke
18c05 79 3d 25 64 20 65 78 70 65 63 74 65 64 3d 28 25  y=%d expected=(%
18c06 64 2c 25 64 29 20 67 6f 74 3d 28 25 64 2c 25 64  d,%d) got=(%d,%d
18c07 29 22 2c 20 0a 20 20 20 20 20 20 69 43 68 69 6c  )", .      iChil
18c08 64 2c 20 65 54 79 70 65 2c 20 69 50 61 72 65 6e  d, eType, iParen
18c09 74 2c 20 65 50 74 72 6d 61 70 54 79 70 65 2c 20  t, ePtrmapType, 
18c0a 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a  iPtrmapParent);.
18c0b 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a    }.}.#endif../*
18c0c 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 69 6e  .** Check the in
18c0d 74 65 67 72 69 74 79 20 6f 66 20 74 68 65 20 66  tegrity of the f
18c0e 72 65 65 6c 69 73 74 20 6f 72 20 6f 66 20 61 6e  reelist or of an
18c0f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c   overflow page l
18c10 69 73 74 2e 0a 2a 2a 20 56 65 72 69 66 79 20 74  ist..** Verify t
18c11 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  hat the number o
18c12 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20 6c  f pages on the l
18c13 69 73 74 20 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61  ist is N..*/.sta
18c14 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 4c 69  tic void checkLi
18c15 73 74 28 0a 20 20 49 6e 74 65 67 72 69 74 79 43  st(.  IntegrityC
18c16 6b 20 2a 70 43 68 65 63 6b 2c 20 20 2f 2a 20 49  k *pCheck,  /* I
18c17 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 69 6e  ntegrity checkin
18c18 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69  g context */.  i
18c19 6e 74 20 69 73 46 72 65 65 4c 69 73 74 2c 20 20  nt isFreeList,  
18c1a 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72       /* True for
18c1b 20 61 20 66 72 65 65 6c 69 73 74 2e 20 20 46 61   a freelist.  Fa
18c1c 6c 73 65 20 66 6f 72 20 6f 76 65 72 66 6c 6f 77  lse for overflow
18c1d 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 20 20   page list */.  
18c1e 69 6e 74 20 69 50 61 67 65 2c 20 20 20 20 20 20  int iPage,      
18c1f 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75        /* Page nu
18c20 6d 62 65 72 20 66 6f 72 20 66 69 72 73 74 20 70  mber for first p
18c21 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74 20  age in the list 
18c22 2a 2f 0a 20 20 69 6e 74 20 4e 2c 20 20 20 20 20  */.  int N,     
18c23 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78             /* Ex
18c24 70 65 63 74 65 64 20 6e 75 6d 62 65 72 20 6f 66  pected number of
18c25 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c 69   pages in the li
18c26 73 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43  st */.  char *zC
18c27 6f 6e 74 65 78 74 20 20 20 20 20 20 20 20 2f 2a  ontext        /*
18c28 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 65 72 72   Context for err
18c29 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a 29  or messages */.)
18c2a 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
18c2b 20 65 78 70 65 63 74 65 64 20 3d 20 4e 3b 0a 20   expected = N;. 
18c2c 20 69 6e 74 20 69 46 69 72 73 74 20 3d 20 69 50   int iFirst = iP
18c2d 61 67 65 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d  age;.  while( N-
18c2e 2d 20 3e 20 30 20 26 26 20 70 43 68 65 63 6b 2d  - > 0 && pCheck-
18c2f 3e 6d 78 45 72 72 20 29 7b 0a 20 20 20 20 44 62  >mxErr ){.    Db
18c30 50 61 67 65 20 2a 70 4f 76 66 6c 50 61 67 65 3b  Page *pOvflPage;
18c31 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
18c32 61 72 20 2a 70 4f 76 66 6c 44 61 74 61 3b 0a 20  ar *pOvflData;. 
18c33 20 20 20 69 66 28 20 69 50 61 67 65 3c 31 20 29     if( iPage<1 )
18c34 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70  {.      checkApp
18c35 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a  endMsg(pCheck, z
18c36 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20  Context,.       
18c37 20 20 22 25 64 20 6f 66 20 25 64 20 70 61 67 65    "%d of %d page
18c38 73 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20 6f  s missing from o
18c39 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 73 74 61  verflow list sta
18c3a 72 74 69 6e 67 20 61 74 20 25 64 22 2c 0a 20 20  rting at %d",.  
18c3b 20 20 20 20 20 20 20 20 4e 2b 31 2c 20 65 78 70          N+1, exp
18c3c 65 63 74 65 64 2c 20 69 46 69 72 73 74 29 3b 0a  ected, iFirst);.
18c3d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
18c3e 20 7d 0a 20 20 20 20 69 66 28 20 63 68 65 63 6b   }.    if( check
18c3f 52 65 66 28 70 43 68 65 63 6b 2c 20 69 50 61 67  Ref(pCheck, iPag
18c40 65 2c 20 7a 43 6f 6e 74 65 78 74 29 20 29 20 62  e, zContext) ) b
18c41 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 71  reak;.    if( sq
18c42 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 43  lite3PagerGet(pC
18c43 68 65 63 6b 2d 3e 70 50 61 67 65 72 2c 20 28 50  heck->pPager, (P
18c44 67 6e 6f 29 69 50 61 67 65 2c 20 26 70 4f 76 66  gno)iPage, &pOvf
18c45 6c 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20 20  lPage) ){.      
18c46 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70  checkAppendMsg(p
18c47 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c  Check, zContext,
18c48 20 22 66 61 69 6c 65 64 20 74 6f 20 67 65 74 20   "failed to get 
18c49 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 65 29  page %d", iPage)
18c4a 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
18c4b 20 20 20 7d 0a 20 20 20 20 70 4f 76 66 6c 44 61     }.    pOvflDa
18c4c 74 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  ta = (unsigned c
18c4d 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50 61 67  har *)sqlite3Pag
18c4e 65 72 47 65 74 44 61 74 61 28 70 4f 76 66 6c 50  erGetData(pOvflP
18c4f 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 69 73  age);.    if( is
18c50 46 72 65 65 4c 69 73 74 20 29 7b 0a 20 20 20 20  FreeList ){.    
18c51 20 20 69 6e 74 20 6e 20 3d 20 67 65 74 34 62 79    int n = get4by
18c52 74 65 28 26 70 4f 76 66 6c 44 61 74 61 5b 34 5d  te(&pOvflData[4]
18c53 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
18c54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
18c55 4d 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 65  M.      if( pChe
18c56 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63  ck->pBt->autoVac
18c57 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 63  uum ){.        c
18c58 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63  heckPtrmap(pChec
18c59 6b 2c 20 69 50 61 67 65 2c 20 50 54 52 4d 41 50  k, iPage, PTRMAP
18c5a 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a 43  _FREEPAGE, 0, zC
18c5b 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d  ontext);.      }
18c5c 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66  .#endif.      if
18c5d 28 20 6e 3e 70 43 68 65 63 6b 2d 3e 70 42 74 2d  ( n>pCheck->pBt-
18c5e 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32 20  >usableSize/4-2 
18c5f 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b  ){.        check
18c60 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b  AppendMsg(pCheck
18c61 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20  , zContext,.    
18c62 20 20 20 20 20 20 20 22 66 72 65 65 6c 69 73 74         "freelist
18c63 20 6c 65 61 66 20 63 6f 75 6e 74 20 74 6f 6f 20   leaf count too 
18c64 62 69 67 20 6f 6e 20 70 61 67 65 20 25 64 22 2c  big on page %d",
18c65 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20   iPage);.       
18c66 20 4e 2d 2d 3b 0a 20 20 20 20 20 20 7d 65 6c 73   N--;.      }els
18c67 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  e{.        for(i
18c68 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20  =0; i<n; i++){. 
18c69 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 46           Pgno iF
18c6a 72 65 65 50 61 67 65 20 3d 20 67 65 74 34 62 79  reePage = get4by
18c6b 74 65 28 26 70 4f 76 66 6c 44 61 74 61 5b 38 2b  te(&pOvflData[8+
18c6c 69 2a 34 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53  i*4]);.#ifndef S
18c6d 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
18c6e 41 43 55 55 4d 0a 20 20 20 20 20 20 20 20 20 20  ACUUM.          
18c6f 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d  if( pCheck->pBt-
18c70 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20  >autoVacuum ){. 
18c71 20 20 20 20 20 20 20 20 20 20 20 63 68 65 63 6b             check
18c72 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69  Ptrmap(pCheck, i
18c73 46 72 65 65 50 61 67 65 2c 20 50 54 52 4d 41 50  FreePage, PTRMAP
18c74 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a 43  _FREEPAGE, 0, zC
18c75 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20  ontext);.       
18c76 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
18c77 20 20 20 20 20 20 63 68 65 63 6b 52 65 66 28 70        checkRef(p
18c78 43 68 65 63 6b 2c 20 69 46 72 65 65 50 61 67 65  Check, iFreePage
18c79 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20  , zContext);.   
18c7a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 4e       }.        N
18c7b 20 2d 3d 20 6e 3b 0a 20 20 20 20 20 20 7d 0a 20   -= n;.      }. 
18c7c 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
18c7d 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
18c7e 55 55 4d 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20  UUM.    else{.  
18c7f 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 64      /* If this d
18c80 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73  atabase supports
18c81 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 61 6e 64   auto-vacuum and
18c82 20 69 50 61 67 65 20 69 73 20 6e 6f 74 20 74 68   iPage is not th
18c83 65 20 6c 61 73 74 0a 20 20 20 20 20 20 2a 2a 20  e last.      ** 
18c84 70 61 67 65 20 69 6e 20 74 68 69 73 20 6f 76 65  page in this ove
18c85 72 66 6c 6f 77 20 6c 69 73 74 2c 20 63 68 65 63  rflow list, chec
18c86 6b 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74  k that the point
18c87 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72  er-map entry for
18c88 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66 6f  .      ** the fo
18c89 6c 6c 6f 77 69 6e 67 20 70 61 67 65 20 6d 61 74  llowing page mat
18c8a 63 68 65 73 20 69 50 61 67 65 2e 0a 20 20 20 20  ches iPage..    
18c8b 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70    */.      if( p
18c8c 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f  Check->pBt->auto
18c8d 56 61 63 75 75 6d 20 26 26 20 4e 3e 30 20 29 7b  Vacuum && N>0 ){
18c8e 0a 20 20 20 20 20 20 20 20 69 20 3d 20 67 65 74  .        i = get
18c8f 34 62 79 74 65 28 70 4f 76 66 6c 44 61 74 61 29  4byte(pOvflData)
18c90 3b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 50  ;.        checkP
18c91 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69 2c  trmap(pCheck, i,
18c92 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
18c93 32 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65  2, iPage, zConte
18c94 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  xt);.      }.   
18c95 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 50   }.#endif.    iP
18c96 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 70  age = get4byte(p
18c97 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20 20 20 73  OvflData);.    s
18c98 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
18c99 28 70 4f 76 66 6c 50 61 67 65 29 3b 0a 20 20 7d  (pOvflPage);.  }
18c9a 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
18c9b 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49  ITE_OMIT_INTEGRI
18c9c 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66  TY_CHECK */..#if
18c9d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
18c9e 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b  _INTEGRITY_CHECK
18c9f 0a 2f 2a 0a 2a 2a 20 44 6f 20 76 61 72 69 6f 75  ./*.** Do variou
18ca0 73 20 73 61 6e 69 74 79 20 63 68 65 63 6b 73 20  s sanity checks 
18ca1 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65  on a single page
18ca2 20 6f 66 20 61 20 74 72 65 65 2e 20 20 52 65 74   of a tree.  Ret
18ca3 75 72 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20  urn.** the tree 
18ca4 64 65 70 74 68 2e 20 20 52 6f 6f 74 20 70 61 67  depth.  Root pag
18ca5 65 73 20 72 65 74 75 72 6e 20 30 2e 20 20 50 61  es return 0.  Pa
18ca6 72 65 6e 74 73 20 6f 66 20 72 6f 6f 74 20 70 61  rents of root pa
18ca7 67 65 73 0a 2a 2a 20 72 65 74 75 72 6e 20 31 2c  ges.** return 1,
18ca8 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a 2a   and so forth..*
18ca9 2a 20 0a 2a 2a 20 54 68 65 73 65 20 63 68 65 63  * .** These chec
18caa 6b 73 20 61 72 65 20 64 6f 6e 65 3a 0a 2a 2a 0a  ks are done:.**.
18cab 2a 2a 20 20 20 20 20 20 31 2e 20 20 4d 61 6b 65  **      1.  Make
18cac 20 73 75 72 65 20 74 68 61 74 20 63 65 6c 6c 73   sure that cells
18cad 20 61 6e 64 20 66 72 65 65 62 6c 6f 63 6b 73 20   and freeblocks 
18cae 64 6f 20 6e 6f 74 20 6f 76 65 72 6c 61 70 0a 2a  do not overlap.*
18caf 2a 20 20 20 20 20 20 20 20 20 20 62 75 74 20 63  *          but c
18cb0 6f 6d 62 69 6e 65 20 74 6f 20 63 6f 6d 70 6c 65  ombine to comple
18cb1 74 65 6c 79 20 63 6f 76 65 72 20 74 68 65 20 70  tely cover the p
18cb2 61 67 65 2e 0a 2a 2a 20 20 4e 4f 20 20 32 2e 20  age..**  NO  2. 
18cb3 20 4d 61 6b 65 20 73 75 72 65 20 63 65 6c 6c 20   Make sure cell 
18cb4 6b 65 79 73 20 61 72 65 20 69 6e 20 6f 72 64 65  keys are in orde
18cb5 72 2e 0a 2a 2a 20 20 4e 4f 20 20 33 2e 20 20 4d  r..**  NO  3.  M
18cb6 61 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20  ake sure no key 
18cb7 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20  is less than or 
18cb8 65 71 75 61 6c 20 74 6f 20 7a 4c 6f 77 65 72 42  equal to zLowerB
18cb9 6f 75 6e 64 2e 0a 2a 2a 20 20 4e 4f 20 20 34 2e  ound..**  NO  4.
18cba 20 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20 6b    Make sure no k
18cbb 65 79 20 69 73 20 67 72 65 61 74 65 72 20 74 68  ey is greater th
18cbc 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 7a  an or equal to z
18cbd 55 70 70 65 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20  UpperBound..**  
18cbe 20 20 20 20 35 2e 20 20 43 68 65 63 6b 20 74 68      5.  Check th
18cbf 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20 6f  e integrity of o
18cc0 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 0a 2a  verflow pages..*
18cc1 2a 20 20 20 20 20 20 36 2e 20 20 52 65 63 75 72  *      6.  Recur
18cc2 73 69 76 65 6c 79 20 63 61 6c 6c 20 63 68 65 63  sively call chec
18cc3 6b 54 72 65 65 50 61 67 65 20 6f 6e 20 61 6c 6c  kTreePage on all
18cc4 20 63 68 69 6c 64 72 65 6e 2e 0a 2a 2a 20 20 20   children..**   
18cc5 20 20 20 37 2e 20 20 56 65 72 69 66 79 20 74 68     7.  Verify th
18cc6 61 74 20 74 68 65 20 64 65 70 74 68 20 6f 66 20  at the depth of 
18cc7 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 69 73 20  all children is 
18cc8 74 68 65 20 73 61 6d 65 2e 0a 2a 2a 20 20 20 20  the same..**    
18cc9 20 20 38 2e 20 20 4d 61 6b 65 20 73 75 72 65 20    8.  Make sure 
18cca 74 68 69 73 20 70 61 67 65 20 69 73 20 61 74 20  this page is at 
18ccb 6c 65 61 73 74 20 33 33 25 20 66 75 6c 6c 20 6f  least 33% full o
18ccc 72 20 65 6c 73 65 20 69 74 20 69 73 0a 2a 2a 20  r else it is.** 
18ccd 20 20 20 20 20 20 20 20 20 74 68 65 20 72 6f 6f           the roo
18cce 74 20 6f 66 20 74 68 65 20 74 72 65 65 2e 0a 2a  t of the tree..*
18ccf 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65  /.static int che
18cd0 63 6b 54 72 65 65 50 61 67 65 28 0a 20 20 49 6e  ckTreePage(.  In
18cd1 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63  tegrityCk *pChec
18cd2 6b 2c 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 66  k,  /* Context f
18cd3 6f 72 20 74 68 65 20 73 61 6e 69 74 79 20 63 68  or the sanity ch
18cd4 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61  eck */.  int iPa
18cd5 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ge,            /
18cd6 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  * Page number of
18cd7 20 74 68 65 20 70 61 67 65 20 74 6f 20 63 68 65   the page to che
18cd8 63 6b 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 50  ck */.  char *zP
18cd9 61 72 65 6e 74 43 6f 6e 74 65 78 74 20 20 2f 2a  arentContext  /*
18cda 20 50 61 72 65 6e 74 20 63 6f 6e 74 65 78 74 20   Parent context 
18cdb 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20  */.){.  MemPage 
18cdc 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 69 2c  *pPage;.  int i,
18cdd 20 72 63 2c 20 64 65 70 74 68 2c 20 64 32 2c 20   rc, depth, d2, 
18cde 70 67 6e 6f 2c 20 63 6e 74 3b 0a 20 20 69 6e 74  pgno, cnt;.  int
18cdf 20 68 64 72 2c 20 63 65 6c 6c 53 74 61 72 74 3b   hdr, cellStart;
18ce0 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20  .  int nCell;.  
18ce1 75 38 20 2a 64 61 74 61 3b 0a 20 20 42 74 53 68  u8 *data;.  BtSh
18ce2 61 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 6e 74  ared *pBt;.  int
18ce3 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 63   usableSize;.  c
18ce4 68 61 72 20 7a 43 6f 6e 74 65 78 74 5b 31 30 30  har zContext[100
18ce5 5d 3b 0a 20 20 63 68 61 72 20 2a 68 69 74 20 3d  ];.  char *hit =
18ce6 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73   0;..  sqlite3_s
18ce7 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
18ce8 43 6f 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65  Context), zConte
18ce9 78 74 2c 20 22 50 61 67 65 20 25 64 3a 20 22 2c  xt, "Page %d: ",
18cea 20 69 50 61 67 65 29 3b 0a 0a 20 20 2f 2a 20 43   iPage);..  /* C
18ceb 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 61  heck that the pa
18cec 67 65 20 65 78 69 73 74 73 0a 20 20 2a 2f 0a 20  ge exists.  */. 
18ced 20 70 42 74 20 3d 20 70 43 68 65 63 6b 2d 3e 70   pBt = pCheck->p
18cee 42 74 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65  Bt;.  usableSize
18cef 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69   = pBt->usableSi
18cf0 7a 65 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3d  ze;.  if( iPage=
18cf1 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
18cf2 20 69 66 28 20 63 68 65 63 6b 52 65 66 28 70 43   if( checkRef(pC
18cf3 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a 50 61  heck, iPage, zPa
18cf4 72 65 6e 74 43 6f 6e 74 65 78 74 29 20 29 20 72  rentContext) ) r
18cf5 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 28  eturn 0;.  if( (
18cf6 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67  rc = btreeGetPag
18cf7 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 50 61  e(pBt, (Pgno)iPa
18cf8 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29 29 21  ge, &pPage, 0))!
18cf9 3d 30 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41  =0 ){.    checkA
18cfa 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c  ppendMsg(pCheck,
18cfb 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20   zContext,.     
18cfc 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 67 65 74    "unable to get
18cfd 20 74 68 65 20 70 61 67 65 2e 20 65 72 72 6f 72   the page. error
18cfe 20 63 6f 64 65 3d 25 64 22 2c 20 72 63 29 3b 0a   code=%d", rc);.
18cff 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
18d00 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 72 20 4d 65  }..  /* Clear Me
18d01 6d 50 61 67 65 2e 69 73 49 6e 69 74 20 74 6f 20  mPage.isInit to 
18d02 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 6f  make sure the co
18d03 72 72 75 70 74 69 6f 6e 20 64 65 74 65 63 74 69  rruption detecti
18d04 6f 6e 20 63 6f 64 65 20 69 6e 0a 20 20 2a 2a 20  on code in.  ** 
18d05 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20  btreeInitPage() 
18d06 69 73 20 65 78 65 63 75 74 65 64 2e 20 20 2a 2f  is executed.  */
18d07 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74  .  pPage->isInit
18d08 20 3d 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20   = 0;.  if( (rc 
18d09 3d 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28  = btreeInitPage(
18d0a 70 50 61 67 65 29 29 21 3d 30 20 29 7b 0a 20 20  pPage))!=0 ){.  
18d0b 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
18d0c 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 29 3b 20  LITE_CORRUPT ); 
18d0d 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 70 6f 73   /* The only pos
18d0e 73 69 62 6c 65 20 65 72 72 6f 72 20 66 72 6f 6d  sible error from
18d0f 20 49 6e 69 74 50 61 67 65 20 2a 2f 0a 20 20 20   InitPage */.   
18d10 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
18d11 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74  pCheck, zContext
18d12 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
18d13 20 20 20 20 20 20 22 62 74 72 65 65 49 6e 69 74        "btreeInit
18d14 50 61 67 65 28 29 20 72 65 74 75 72 6e 73 20 65  Page() returns e
18d15 72 72 6f 72 20 63 6f 64 65 20 25 64 22 2c 20 72  rror code %d", r
18d16 63 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50  c);.    releaseP
18d17 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20  age(pPage);.    
18d18 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
18d19 20 2f 2a 20 43 68 65 63 6b 20 6f 75 74 20 61 6c   /* Check out al
18d1a 6c 20 74 68 65 20 63 65 6c 6c 73 2e 0a 20 20 2a  l the cells..  *
18d1b 2f 0a 20 20 64 65 70 74 68 20 3d 20 30 3b 0a 20  /.  depth = 0;. 
18d1c 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 67   for(i=0; i<pPag
18d1d 65 2d 3e 6e 43 65 6c 6c 20 26 26 20 70 43 68 65  e->nCell && pChe
18d1e 63 6b 2d 3e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b  ck->mxErr; i++){
18d1f 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a  .    u8 *pCell;.
18d20 20 20 20 20 75 33 32 20 73 7a 3b 0a 20 20 20 20      u32 sz;.    
18d21 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a  CellInfo info;..
18d22 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 70 61 79      /* Check pay
18d23 6c 6f 61 64 20 6f 76 65 72 66 6c 6f 77 20 70 61  load overflow pa
18d24 67 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  ges.    */.    s
18d25 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
18d26 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29  sizeof(zContext)
18d27 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20  , zContext,.    
18d28 20 20 20 20 20 20 20 20 20 22 4f 6e 20 74 72 65           "On tre
18d29 65 20 70 61 67 65 20 25 64 20 63 65 6c 6c 20 25  e page %d cell %
18d2a 64 3a 20 22 2c 20 69 50 61 67 65 2c 20 69 29 3b  d: ", iPage, i);
18d2b 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e  .    pCell = fin
18d2c 64 43 65 6c 6c 28 70 50 61 67 65 2c 69 29 3b 0a  dCell(pPage,i);.
18d2d 20 20 20 20 62 74 72 65 65 50 61 72 73 65 43 65      btreeParseCe
18d2e 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65  llPtr(pPage, pCe
18d2f 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20  ll, &info);.    
18d30 73 7a 20 3d 20 69 6e 66 6f 2e 6e 44 61 74 61 3b  sz = info.nData;
18d31 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d  .    if( !pPage-
18d32 3e 69 6e 74 4b 65 79 20 29 20 73 7a 20 2b 3d 20  >intKey ) sz += 
18d33 28 69 6e 74 29 69 6e 66 6f 2e 6e 4b 65 79 3b 0a  (int)info.nKey;.
18d34 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d      assert( sz==
18d35 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b  info.nPayload );
18d36 0a 20 20 20 20 69 66 28 20 28 73 7a 3e 69 6e 66  .    if( (sz>inf
18d37 6f 2e 6e 4c 6f 63 61 6c 29 20 0a 20 20 20 20 20  o.nLocal) .     
18d38 26 26 20 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e  && (&pCell[info.
18d39 69 4f 76 65 72 66 6c 6f 77 5d 3c 3d 26 70 50 61  iOverflow]<=&pPa
18d3a 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 75  ge->aData[pBt->u
18d3b 73 61 62 6c 65 53 69 7a 65 5d 29 0a 20 20 20 20  sableSize]).    
18d3c 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 50 61  ){.      int nPa
18d3d 67 65 20 3d 20 28 73 7a 20 2d 20 69 6e 66 6f 2e  ge = (sz - info.
18d3e 6e 4c 6f 63 61 6c 20 2b 20 75 73 61 62 6c 65 53  nLocal + usableS
18d3f 69 7a 65 20 2d 20 35 29 2f 28 75 73 61 62 6c 65  ize - 5)/(usable
18d40 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20 20 20 20  Size - 4);.     
18d41 20 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d   Pgno pgnoOvfl =
18d42 20 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c   get4byte(&pCell
18d43 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d  [info.iOverflow]
18d44 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
18d45 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
18d46 4d 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d  M.      if( pBt-
18d47 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20  >autoVacuum ){. 
18d48 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d         checkPtrm
18d49 61 70 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 4f  ap(pCheck, pgnoO
18d4a 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52  vfl, PTRMAP_OVER
18d4b 46 4c 4f 57 31 2c 20 69 50 61 67 65 2c 20 7a 43  FLOW1, iPage, zC
18d4c 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d  ontext);.      }
18d4d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 63 68  .#endif.      ch
18d4e 65 63 6b 4c 69 73 74 28 70 43 68 65 63 6b 2c 20  eckList(pCheck, 
18d4f 30 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 6e 50 61  0, pgnoOvfl, nPa
18d50 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20  ge, zContext);. 
18d51 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65     }..    /* Che
18d52 63 6b 20 73 61 6e 69 74 79 20 6f 66 20 6c 65 66  ck sanity of lef
18d53 74 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 20 20  t child page..  
18d54 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 50    */.    if( !pP
18d55 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
18d56 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79     pgno = get4by
18d57 74 65 28 70 43 65 6c 6c 29 3b 0a 23 69 66 6e 64  te(pCell);.#ifnd
18d58 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
18d59 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20  UTOVACUUM.      
18d5a 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
18d5b 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 63  uum ){.        c
18d5c 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63  heckPtrmap(pChec
18d5d 6b 2c 20 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f  k, pgno, PTRMAP_
18d5e 42 54 52 45 45 2c 20 69 50 61 67 65 2c 20 7a 43  BTREE, iPage, zC
18d5f 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d  ontext);.      }
18d60 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 64 32  .#endif.      d2
18d61 20 3d 20 63 68 65 63 6b 54 72 65 65 50 61 67 65   = checkTreePage
18d62 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a  (pCheck, pgno, z
18d63 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  Context);.      
18d64 69 66 28 20 69 3e 30 20 26 26 20 64 32 21 3d 64  if( i>0 && d2!=d
18d65 65 70 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20  epth ){.        
18d66 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70  checkAppendMsg(p
18d67 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c  Check, zContext,
18d68 20 22 43 68 69 6c 64 20 70 61 67 65 20 64 65 70   "Child page dep
18d69 74 68 20 64 69 66 66 65 72 73 22 29 3b 0a 20 20  th differs");.  
18d6a 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 70 74      }.      dept
18d6b 68 20 3d 20 64 32 3b 0a 20 20 20 20 7d 0a 20 20  h = d2;.    }.  
18d6c 7d 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e  }.  if( !pPage->
18d6d 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f  leaf ){.    pgno
18d6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61   = get4byte(&pPa
18d6f 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d  ge->aData[pPage-
18d70 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a  >hdrOffset+8]);.
18d71 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
18d72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e  intf(sizeof(zCon
18d73 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c  text), zContext,
18d74 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
18d75 20 20 20 20 20 20 20 22 4f 6e 20 70 61 67 65 20         "On page 
18d76 25 64 20 61 74 20 72 69 67 68 74 20 63 68 69 6c  %d at right chil
18d77 64 3a 20 22 2c 20 69 50 61 67 65 29 3b 0a 23 69  d: ", iPage);.#i
18d78 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
18d79 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
18d7a 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61   if( pBt->autoVa
18d7b 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 63 68  cuum ){.      ch
18d7c 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b  eckPtrmap(pCheck
18d7d 2c 20 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42  , pgno, PTRMAP_B
18d7e 54 52 45 45 2c 20 69 50 61 67 65 2c 20 30 29 3b  TREE, iPage, 0);
18d7f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
18d80 20 20 63 68 65 63 6b 54 72 65 65 50 61 67 65 28    checkTreePage(
18d81 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43  pCheck, pgno, zC
18d82 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 0a 20 0a 20  ontext);.  }. . 
18d83 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 63 6f   /* Check for co
18d84 6d 70 6c 65 74 65 20 63 6f 76 65 72 61 67 65 20  mplete coverage 
18d85 6f 66 20 74 68 65 20 70 61 67 65 0a 20 20 2a 2f  of the page.  */
18d86 0a 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d  .  data = pPage-
18d87 3e 61 44 61 74 61 3b 0a 20 20 68 64 72 20 3d 20  >aData;.  hdr = 
18d88 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
18d89 3b 0a 20 20 68 69 74 20 3d 20 73 71 6c 69 74 65  ;.  hit = sqlite
18d8a 33 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74  3PageMalloc( pBt
18d8b 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20  ->pageSize );.  
18d8c 69 66 28 20 68 69 74 3d 3d 30 20 29 7b 0a 20 20  if( hit==0 ){.  
18d8d 20 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63    pCheck->malloc
18d8e 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65  Failed = 1;.  }e
18d8f 6c 73 65 7b 0a 20 20 20 20 75 31 36 20 63 6f 6e  lse{.    u16 con
18d90 74 65 6e 74 4f 66 66 73 65 74 20 3d 20 67 65 74  tentOffset = get
18d91 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
18d92 35 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  5]);.    assert(
18d93 20 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 3c 3d   contentOffset<=
18d94 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 20 20 2f  usableSize );  /
18d95 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74  * Enforced by bt
18d96 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f  reeInitPage() */
18d97 0a 20 20 20 20 6d 65 6d 73 65 74 28 68 69 74 2b  .    memset(hit+
18d98 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 2c 20 30  contentOffset, 0
18d99 2c 20 75 73 61 62 6c 65 53 69 7a 65 2d 63 6f 6e  , usableSize-con
18d9a 74 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20 20  tentOffset);.   
18d9b 20 6d 65 6d 73 65 74 28 68 69 74 2c 20 31 2c 20   memset(hit, 1, 
18d9c 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 29 3b 0a  contentOffset);.
18d9d 20 20 20 20 6e 43 65 6c 6c 20 3d 20 67 65 74 32      nCell = get2
18d9e 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33  byte(&data[hdr+3
18d9f 5d 29 3b 0a 20 20 20 20 63 65 6c 6c 53 74 61 72  ]);.    cellStar
18da0 74 20 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34  t = hdr + 12 - 4
18da1 2a 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20  *pPage->leaf;.  
18da2 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65    for(i=0; i<nCe
18da3 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ll; i++){.      
18da4 69 6e 74 20 70 63 20 3d 20 67 65 74 32 62 79 74  int pc = get2byt
18da5 65 28 26 64 61 74 61 5b 63 65 6c 6c 53 74 61 72  e(&data[cellStar
18da6 74 2b 69 2a 32 5d 29 3b 0a 20 20 20 20 20 20 75  t+i*2]);.      u
18da7 31 36 20 73 69 7a 65 20 3d 20 31 30 32 34 3b 0a  16 size = 1024;.
18da8 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20        int j;.   
18da9 20 20 20 69 66 28 20 70 63 3c 3d 75 73 61 62 6c     if( pc<=usabl
18daa 65 53 69 7a 65 2d 34 20 29 7b 0a 20 20 20 20 20  eSize-4 ){.     
18dab 20 20 20 73 69 7a 65 20 3d 20 63 65 6c 6c 53 69     size = cellSi
18dac 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26 64 61  zePtr(pPage, &da
18dad 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20 20 7d  ta[pc]);.      }
18dae 0a 20 20 20 20 20 20 69 66 28 20 28 70 63 2b 73  .      if( (pc+s
18daf 69 7a 65 2d 31 29 3e 3d 75 73 61 62 6c 65 53 69  ize-1)>=usableSi
18db0 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  ze ){.        ch
18db1 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68  eckAppendMsg(pCh
18db2 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 20  eck, 0, .       
18db3 20 20 20 20 20 22 43 6f 72 72 75 70 74 69 6f 6e       "Corruption
18db4 20 64 65 74 65 63 74 65 64 20 69 6e 20 63 65 6c   detected in cel
18db5 6c 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22  l %d on page %d"
18db6 2c 69 2c 69 50 61 67 65 2c 30 29 3b 0a 20 20 20  ,i,iPage,0);.   
18db7 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
18db8 20 20 66 6f 72 28 6a 3d 70 63 2b 73 69 7a 65 2d    for(j=pc+size-
18db9 31 3b 20 6a 3e 3d 70 63 3b 20 6a 2d 2d 29 20 68  1; j>=pc; j--) h
18dba 69 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d  it[j]++;.      }
18dbb 0a 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 67  .    }.    i = g
18dbc 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  et2byte(&data[hd
18dbd 72 2b 31 5d 29 3b 0a 20 20 20 20 77 68 69 6c 65  r+1]);.    while
18dbe 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 20 20 69  ( i>0 ){.      i
18dbf 6e 74 20 73 69 7a 65 2c 20 6a 3b 0a 20 20 20 20  nt size, j;.    
18dc0 20 20 61 73 73 65 72 74 28 20 69 3c 3d 75 73 61    assert( i<=usa
18dc1 62 6c 65 53 69 7a 65 2d 34 20 29 3b 20 20 20 20  bleSize-4 );    
18dc2 20 2f 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 20   /* Enforced by 
18dc3 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20  btreeInitPage() 
18dc4 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20  */.      size = 
18dc5 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 69  get2byte(&data[i
18dc6 2b 32 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65  +2]);.      asse
18dc7 72 74 28 20 69 2b 73 69 7a 65 3c 3d 75 73 61 62  rt( i+size<=usab
18dc8 6c 65 53 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e  leSize );  /* En
18dc9 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49  forced by btreeI
18dca 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20  nitPage() */.   
18dcb 20 20 20 66 6f 72 28 6a 3d 69 2b 73 69 7a 65 2d     for(j=i+size-
18dcc 31 3b 20 6a 3e 3d 69 3b 20 6a 2d 2d 29 20 68 69  1; j>=i; j--) hi
18dcd 74 5b 6a 5d 2b 2b 3b 0a 20 20 20 20 20 20 6a 20  t[j]++;.      j 
18dce 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
18dcf 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65  [i]);.      asse
18dd0 72 74 28 20 6a 3d 3d 30 20 7c 7c 20 6a 3e 69 2b  rt( j==0 || j>i+
18dd1 73 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66 6f  size );  /* Enfo
18dd2 72 63 65 64 20 62 79 20 62 74 72 65 65 49 6e 69  rced by btreeIni
18dd3 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 20  tPage() */.     
18dd4 20 61 73 73 65 72 74 28 20 6a 3c 3d 75 73 61 62   assert( j<=usab
18dd5 6c 65 53 69 7a 65 2d 34 20 29 3b 20 20 20 2f 2a  leSize-4 );   /*
18dd6 20 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72   Enforced by btr
18dd7 65 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a  eeInitPage() */.
18dd8 20 20 20 20 20 20 69 20 3d 20 6a 3b 0a 20 20 20        i = j;.   
18dd9 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 63 6e 74   }.    for(i=cnt
18dda 3d 30 3b 20 69 3c 75 73 61 62 6c 65 53 69 7a 65  =0; i<usableSize
18ddb 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
18ddc 28 20 68 69 74 5b 69 5d 3d 3d 30 20 29 7b 0a 20  ( hit[i]==0 ){. 
18ddd 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20         cnt++;.  
18dde 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 69      }else if( hi
18ddf 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20  t[i]>1 ){.      
18de0 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67    checkAppendMsg
18de1 28 70 43 68 65 63 6b 2c 20 30 2c 0a 20 20 20 20  (pCheck, 0,.    
18de2 20 20 20 20 20 20 22 4d 75 6c 74 69 70 6c 65 20        "Multiple 
18de3 75 73 65 73 20 66 6f 72 20 62 79 74 65 20 25 64  uses for byte %d
18de4 20 6f 66 20 70 61 67 65 20 25 64 22 2c 20 69 2c   of page %d", i,
18de5 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20   iPage);.       
18de6 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
18de7 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63 6e      }.    if( cn
18de8 74 21 3d 64 61 74 61 5b 68 64 72 2b 37 5d 20 29  t!=data[hdr+7] )
18de9 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70  {.      checkApp
18dea 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 30  endMsg(pCheck, 0
18deb 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 46 72  , .          "Fr
18dec 61 67 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 25  agmentation of %
18ded 64 20 62 79 74 65 73 20 72 65 70 6f 72 74 65 64  d bytes reported
18dee 20 61 73 20 25 64 20 6f 6e 20 70 61 67 65 20 25   as %d on page %
18def 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 63 6e  d",.          cn
18df0 74 2c 20 64 61 74 61 5b 68 64 72 2b 37 5d 2c 20  t, data[hdr+7], 
18df1 69 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20  iPage);.    }.  
18df2 7d 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 46  }.  sqlite3PageF
18df3 72 65 65 28 68 69 74 29 3b 0a 20 20 72 65 6c 65  ree(hit);.  rele
18df4 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a  asePage(pPage);.
18df5 20 20 72 65 74 75 72 6e 20 64 65 70 74 68 2b 31    return depth+1
18df6 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
18df7 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52  LITE_OMIT_INTEGR
18df8 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69  ITY_CHECK */..#i
18df9 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
18dfa 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  T_INTEGRITY_CHEC
18dfb 4b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  K./*.** This rou
18dfc 74 69 6e 65 20 64 6f 65 73 20 61 20 63 6f 6d 70  tine does a comp
18dfd 6c 65 74 65 20 63 68 65 63 6b 20 6f 66 20 74 68  lete check of th
18dfe 65 20 67 69 76 65 6e 20 42 54 72 65 65 20 66 69  e given BTree fi
18dff 6c 65 2e 20 20 61 52 6f 6f 74 5b 5d 20 69 73 0a  le.  aRoot[] is.
18e00 2a 2a 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70  ** an array of p
18e01 61 67 65 73 20 6e 75 6d 62 65 72 73 20 77 65 72  ages numbers wer
18e02 65 20 65 61 63 68 20 70 61 67 65 20 6e 75 6d 62  e each page numb
18e03 65 72 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70  er is the root p
18e04 61 67 65 20 6f 66 0a 2a 2a 20 61 20 74 61 62 6c  age of.** a tabl
18e05 65 2e 20 20 6e 52 6f 6f 74 20 69 73 20 74 68 65  e.  nRoot is the
18e06 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   number of entri
18e07 65 73 20 69 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a  es in aRoot..**.
18e08 2a 2a 20 41 20 72 65 61 64 2d 6f 6e 6c 79 20 6f  ** A read-only o
18e09 72 20 72 65 61 64 2d 77 72 69 74 65 20 74 72 61  r read-write tra
18e0a 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65  nsaction must be
18e0b 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 65 20 63   opened before c
18e0c 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 66  alling.** this f
18e0d 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57  unction..**.** W
18e0e 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20  rite the number 
18e0f 6f 66 20 65 72 72 6f 72 20 73 65 65 6e 20 69 6e  of error seen in
18e10 20 2a 70 6e 45 72 72 2e 20 20 45 78 63 65 70 74   *pnErr.  Except
18e11 20 66 6f 72 20 73 6f 6d 65 20 6d 65 6d 6f 72 79   for some memory
18e12 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  .** allocation e
18e13 72 72 6f 72 73 2c 20 20 61 6e 20 65 72 72 6f 72  rrors,  an error
18e14 20 6d 65 73 73 61 67 65 20 68 65 6c 64 20 69 6e   message held in
18e15 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
18e16 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20   from.** malloc 
18e17 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 2a  is returned if *
18e18 70 6e 45 72 72 20 69 73 20 6e 6f 6e 2d 7a 65 72  pnErr is non-zer
18e19 6f 2e 20 20 49 66 20 2a 70 6e 45 72 72 3d 3d 30  o.  If *pnErr==0
18e1a 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 0a 2a 2a   then NULL is.**
18e1b 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 61   returned.  If a
18e1c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
18e1d 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  on error occurs,
18e1e 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
18e1f 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
18e20 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
18e21 65 33 42 74 72 65 65 49 6e 74 65 67 72 69 74 79  e3BtreeIntegrity
18e22 43 68 65 63 6b 28 0a 20 20 42 74 72 65 65 20 2a  Check(.  Btree *
18e23 70 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74  p,     /* The bt
18e24 72 65 65 20 74 6f 20 62 65 20 63 68 65 63 6b 65  ree to be checke
18e25 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 6f 6f  d */.  int *aRoo
18e26 74 2c 20 20 20 2f 2a 20 41 6e 20 61 72 72 61 79  t,   /* An array
18e27 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 20 6e   of root pages n
18e28 75 6d 62 65 72 73 20 66 6f 72 20 69 6e 64 69 76  umbers for indiv
18e29 69 64 75 61 6c 20 74 72 65 65 73 20 2a 2f 0a 20  idual trees */. 
18e2a 20 69 6e 74 20 6e 52 6f 6f 74 2c 20 20 20 20 2f   int nRoot,    /
18e2b 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
18e2c 69 65 73 20 69 6e 20 61 52 6f 6f 74 5b 5d 20 2a  ies in aRoot[] *
18e2d 2f 0a 20 20 69 6e 74 20 6d 78 45 72 72 2c 20 20  /.  int mxErr,  
18e2e 20 20 2f 2a 20 53 74 6f 70 20 72 65 70 6f 72 74    /* Stop report
18e2f 69 6e 67 20 65 72 72 6f 72 73 20 61 66 74 65 72  ing errors after
18e30 20 74 68 69 73 20 6d 61 6e 79 20 2a 2f 0a 20 20   this many */.  
18e31 69 6e 74 20 2a 70 6e 45 72 72 20 20 20 20 2f 2a  int *pnErr    /*
18e32 20 57 72 69 74 65 20 6e 75 6d 62 65 72 20 6f 66   Write number of
18e33 20 65 72 72 6f 72 73 20 73 65 65 6e 20 74 6f 20   errors seen to 
18e34 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 2a 2f  this variable */
18e35 0a 29 7b 0a 20 20 50 67 6e 6f 20 69 3b 0a 20 20  .){.  Pgno i;.  
18e36 69 6e 74 20 6e 52 65 66 3b 0a 20 20 49 6e 74 65  int nRef;.  Inte
18e37 67 72 69 74 79 43 6b 20 73 43 68 65 63 6b 3b 0a  grityCk sCheck;.
18e38 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
18e39 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 63 68 61 72  = p->pBt;.  char
18e3a 20 7a 45 72 72 5b 31 30 30 5d 3b 0a 0a 20 20 73   zErr[100];..  s
18e3b 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
18e3c 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  (p);.  assert( p
18e3d 2d 3e 69 6e 54 72 61 6e 73 3e 54 52 41 4e 53 5f  ->inTrans>TRANS_
18e3e 4e 4f 4e 45 20 26 26 20 70 42 74 2d 3e 69 6e 54  NONE && pBt->inT
18e3f 72 61 6e 73 61 63 74 69 6f 6e 3e 54 52 41 4e 53  ransaction>TRANS
18e40 5f 4e 4f 4e 45 20 29 3b 0a 20 20 6e 52 65 66 20  _NONE );.  nRef 
18e41 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65  = sqlite3PagerRe
18e42 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67  fcount(pBt->pPag
18e43 65 72 29 3b 0a 20 20 73 43 68 65 63 6b 2e 70 42  er);.  sCheck.pB
18e44 74 20 3d 20 70 42 74 3b 0a 20 20 73 43 68 65 63  t = pBt;.  sChec
18e45 6b 2e 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e  k.pPager = pBt->
18e46 70 50 61 67 65 72 3b 0a 20 20 73 43 68 65 63 6b  pPager;.  sCheck
18e47 2e 6e 50 61 67 65 20 3d 20 70 61 67 65 72 50 61  .nPage = pagerPa
18e48 67 65 63 6f 75 6e 74 28 73 43 68 65 63 6b 2e 70  gecount(sCheck.p
18e49 42 74 29 3b 0a 20 20 73 43 68 65 63 6b 2e 6d 78  Bt);.  sCheck.mx
18e4a 45 72 72 20 3d 20 6d 78 45 72 72 3b 0a 20 20 73  Err = mxErr;.  s
18e4b 43 68 65 63 6b 2e 6e 45 72 72 20 3d 20 30 3b 0a  Check.nErr = 0;.
18e4c 20 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46    sCheck.mallocF
18e4d 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 2a 70 6e  ailed = 0;.  *pn
18e4e 45 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 73  Err = 0;.  if( s
18e4f 43 68 65 63 6b 2e 6e 50 61 67 65 3d 3d 30 20 29  Check.nPage==0 )
18e50 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  {.    sqlite3Btr
18e51 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20  eeLeave(p);.    
18e52 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
18e53 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 3d 20 73  sCheck.anRef = s
18e54 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 28 73  qlite3Malloc( (s
18e55 43 68 65 63 6b 2e 6e 50 61 67 65 2b 31 29 2a 73  Check.nPage+1)*s
18e56 69 7a 65 6f 66 28 73 43 68 65 63 6b 2e 61 6e 52  izeof(sCheck.anR
18e57 65 66 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20  ef[0]) );.  if( 
18e58 21 73 43 68 65 63 6b 2e 61 6e 52 65 66 20 29 7b  !sCheck.anRef ){
18e59 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 31 3b  .    *pnErr = 1;
18e5a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
18e5b 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72  eLeave(p);.    r
18e5c 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66  eturn 0;.  }.  f
18e5d 6f 72 28 69 3d 30 3b 20 69 3c 3d 73 43 68 65 63  or(i=0; i<=sChec
18e5e 6b 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 73  k.nPage; i++){ s
18e5f 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d  Check.anRef[i] =
18e60 20 30 3b 20 7d 0a 20 20 69 20 3d 20 50 45 4e 44   0; }.  i = PEND
18e61 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
18e62 74 29 3b 0a 20 20 69 66 28 20 69 3c 3d 73 43 68  t);.  if( i<=sCh
18e63 65 63 6b 2e 6e 50 61 67 65 20 29 7b 0a 20 20 20  eck.nPage ){.   
18e64 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d   sCheck.anRef[i]
18e65 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c 69   = 1;.  }.  sqli
18e66 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28  te3StrAccumInit(
18e67 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 2c 20  &sCheck.errMsg, 
18e68 7a 45 72 72 2c 20 73 69 7a 65 6f 66 28 7a 45 72  zErr, sizeof(zEr
18e69 72 29 2c 20 32 30 30 30 30 29 3b 0a 0a 20 20 2f  r), 20000);..  /
18e6a 2a 20 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65  * Check the inte
18e6b 67 72 69 74 79 20 6f 66 20 74 68 65 20 66 72 65  grity of the fre
18e6c 65 6c 69 73 74 0a 20 20 2a 2f 0a 20 20 63 68 65  elist.  */.  che
18e6d 63 6b 4c 69 73 74 28 26 73 43 68 65 63 6b 2c 20  ckList(&sCheck, 
18e6e 31 2c 20 67 65 74 34 62 79 74 65 28 26 70 42 74  1, get4byte(&pBt
18e6f 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  ->pPage1->aData[
18e70 33 32 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20  32]),.          
18e71 20 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d    get4byte(&pBt-
18e72 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  >pPage1->aData[3
18e73 36 5d 29 2c 20 22 4d 61 69 6e 20 66 72 65 65 6c  6]), "Main freel
18e74 69 73 74 3a 20 22 29 3b 0a 0a 20 20 2f 2a 20 43  ist: ");..  /* C
18e75 68 65 63 6b 20 61 6c 6c 20 74 68 65 20 74 61 62  heck all the tab
18e76 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  les..  */.  for(
18e77 69 3d 30 3b 20 28 69 6e 74 29 69 3c 6e 52 6f 6f  i=0; (int)i<nRoo
18e78 74 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72  t && sCheck.mxEr
18e79 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  r; i++){.    if(
18e7a 20 61 52 6f 6f 74 5b 69 5d 3d 3d 30 20 29 20 63   aRoot[i]==0 ) c
18e7b 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66  ontinue;.#ifndef
18e7c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
18e7d 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20  OVACUUM.    if( 
18e7e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
18e7f 26 26 20 61 52 6f 6f 74 5b 69 5d 3e 31 20 29 7b  && aRoot[i]>1 ){
18e80 0a 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d  .      checkPtrm
18e81 61 70 28 26 73 43 68 65 63 6b 2c 20 61 52 6f 6f  ap(&sCheck, aRoo
18e82 74 5b 69 5d 2c 20 50 54 52 4d 41 50 5f 52 4f 4f  t[i], PTRMAP_ROO
18e83 54 50 41 47 45 2c 20 30 2c 20 30 29 3b 0a 20 20  TPAGE, 0, 0);.  
18e84 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63    }.#endif.    c
18e85 68 65 63 6b 54 72 65 65 50 61 67 65 28 26 73 43  heckTreePage(&sC
18e86 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20  heck, aRoot[i], 
18e87 22 4c 69 73 74 20 6f 66 20 74 72 65 65 20 72 6f  "List of tree ro
18e88 6f 74 73 3a 20 22 29 3b 0a 20 20 7d 0a 0a 20 20  ots: ");.  }..  
18e89 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 65 76 65  /* Make sure eve
18e8a 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 66  ry page in the f
18e8b 69 6c 65 20 69 73 20 72 65 66 65 72 65 6e 63 65  ile is reference
18e8c 64 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 31  d.  */.  for(i=1
18e8d 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67  ; i<=sCheck.nPag
18e8e 65 20 26 26 20 73 43 68 65 63 6b 2e 6d 78 45 72  e && sCheck.mxEr
18e8f 72 3b 20 69 2b 2b 29 7b 0a 23 69 66 64 65 66 20  r; i++){.#ifdef 
18e90 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
18e91 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 73  VACUUM.    if( s
18e92 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d  Check.anRef[i]==
18e93 30 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b  0 ){.      check
18e94 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63  AppendMsg(&sChec
18e95 6b 2c 20 30 2c 20 22 50 61 67 65 20 25 64 20 69  k, 0, "Page %d i
18e96 73 20 6e 65 76 65 72 20 75 73 65 64 22 2c 20 69  s never used", i
18e97 29 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20  );.    }.#else. 
18e98 20 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74     /* If the dat
18e99 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 61  abase supports a
18e9a 75 74 6f 2d 76 61 63 75 75 6d 2c 20 6d 61 6b 65  uto-vacuum, make
18e9b 20 73 75 72 65 20 6e 6f 20 74 61 62 6c 65 73 20   sure no tables 
18e9c 63 6f 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20 72  contain.    ** r
18e9d 65 66 65 72 65 6e 63 65 73 20 74 6f 20 70 6f 69  eferences to poi
18e9e 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 0a  nter-map pages..
18e9f 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73      */.    if( s
18ea0 43 68 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d  Check.anRef[i]==
18ea1 30 20 26 26 20 0a 20 20 20 20 20 20 20 28 50 54  0 && .       (PT
18ea2 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c  RMAP_PAGENO(pBt,
18ea3 20 69 29 21 3d 69 20 7c 7c 20 21 70 42 74 2d 3e   i)!=i || !pBt->
18ea4 61 75 74 6f 56 61 63 75 75 6d 29 20 29 7b 0a 20  autoVacuum) ){. 
18ea5 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64       checkAppend
18ea6 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20  Msg(&sCheck, 0, 
18ea7 22 50 61 67 65 20 25 64 20 69 73 20 6e 65 76 65  "Page %d is neve
18ea8 72 20 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 20  r used", i);.   
18ea9 20 7d 0a 20 20 20 20 69 66 28 20 73 43 68 65 63   }.    if( sChec
18eaa 6b 2e 61 6e 52 65 66 5b 69 5d 21 3d 30 20 26 26  k.anRef[i]!=0 &&
18eab 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41 50   .       (PTRMAP
18eac 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 3d  _PAGENO(pBt, i)=
18ead 3d 69 20 26 26 20 70 42 74 2d 3e 61 75 74 6f 56  =i && pBt->autoV
18eae 61 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 20  acuum) ){.      
18eaf 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26  checkAppendMsg(&
18eb0 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 6f 69 6e  sCheck, 0, "Poin
18eb1 74 65 72 20 6d 61 70 20 70 61 67 65 20 25 64 20  ter map page %d 
18eb2 69 73 20 72 65 66 65 72 65 6e 63 65 64 22 2c 20  is referenced", 
18eb3 69 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  i);.    }.#endif
18eb4 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  .  }..  /* Make 
18eb5 73 75 72 65 20 74 68 69 73 20 61 6e 61 6c 79 73  sure this analys
18eb6 69 73 20 64 69 64 20 6e 6f 74 20 6c 65 61 76 65  is did not leave
18eb7 20 61 6e 79 20 75 6e 72 65 66 28 29 20 70 61 67   any unref() pag
18eb8 65 73 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 73  es..  ** This is
18eb9 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 63 6f 6e   an internal con
18eba 73 69 73 74 65 6e 63 79 20 63 68 65 63 6b 3b 20  sistency check; 
18ebb 61 6e 20 69 6e 74 65 67 72 69 74 79 20 63 68 65  an integrity che
18ebc 63 6b 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 69  ck.  ** of the i
18ebd 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 2e 0a  ntegrity check..
18ebe 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52    */.  if( NEVER
18ebf 28 6e 52 65 66 20 21 3d 20 73 71 6c 69 74 65 33  (nRef != sqlite3
18ec0 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42  PagerRefcount(pB
18ec1 74 2d 3e 70 50 61 67 65 72 29 29 20 29 7b 0a 20  t->pPager)) ){. 
18ec2 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
18ec3 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 0a 20  g(&sCheck, 0, . 
18ec4 20 20 20 20 20 22 4f 75 74 73 74 61 6e 64 69 6e       "Outstandin
18ec5 67 20 70 61 67 65 20 63 6f 75 6e 74 20 67 6f 65  g page count goe
18ec6 73 20 66 72 6f 6d 20 25 64 20 74 6f 20 25 64 20  s from %d to %d 
18ec7 64 75 72 69 6e 67 20 74 68 69 73 20 61 6e 61 6c  during this anal
18ec8 79 73 69 73 22 2c 0a 20 20 20 20 20 20 6e 52 65  ysis",.      nRe
18ec9 66 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  f, sqlite3PagerR
18eca 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61  efcount(pBt->pPa
18ecb 67 65 72 29 0a 20 20 20 20 29 3b 0a 20 20 7d 0a  ger).    );.  }.
18ecc 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 20 75 70 20  .  /* Clean  up 
18ecd 61 6e 64 20 72 65 70 6f 72 74 20 65 72 72 6f 72  and report error
18ece 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  s..  */.  sqlite
18ecf 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
18ed0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 73    sqlite3_free(s
18ed1 43 68 65 63 6b 2e 61 6e 52 65 66 29 3b 0a 20 20  Check.anRef);.  
18ed2 69 66 28 20 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f  if( sCheck.mallo
18ed3 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73  cFailed ){.    s
18ed4 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65  qlite3StrAccumRe
18ed5 73 65 74 28 26 73 43 68 65 63 6b 2e 65 72 72 4d  set(&sCheck.errM
18ed6 73 67 29 3b 0a 20 20 20 20 2a 70 6e 45 72 72 20  sg);.    *pnErr 
18ed7 3d 20 73 43 68 65 63 6b 2e 6e 45 72 72 2b 31 3b  = sCheck.nErr+1;
18ed8 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
18ed9 20 7d 0a 20 20 2a 70 6e 45 72 72 20 3d 20 73 43   }.  *pnErr = sC
18eda 68 65 63 6b 2e 6e 45 72 72 3b 0a 20 20 69 66 28  heck.nErr;.  if(
18edb 20 73 43 68 65 63 6b 2e 6e 45 72 72 3d 3d 30 20   sCheck.nErr==0 
18edc 29 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75  ) sqlite3StrAccu
18edd 6d 52 65 73 65 74 28 26 73 43 68 65 63 6b 2e 65  mReset(&sCheck.e
18ede 72 72 4d 73 67 29 3b 0a 20 20 72 65 74 75 72 6e  rrMsg);.  return
18edf 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
18ee0 46 69 6e 69 73 68 28 26 73 43 68 65 63 6b 2e 65  Finish(&sCheck.e
18ee1 72 72 4d 73 67 29 3b 0a 7d 0a 23 65 6e 64 69 66  rrMsg);.}.#endif
18ee2 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
18ee3 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20  INTEGRITY_CHECK 
18ee4 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  */../*.** Return
18ee5 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61   the full pathna
18ee6 6d 65 20 6f 66 20 74 68 65 20 75 6e 64 65 72 6c  me of the underl
18ee7 79 69 6e 67 20 64 61 74 61 62 61 73 65 20 66 69  ying database fi
18ee8 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61  le..**.** The pa
18ee9 67 65 72 20 66 69 6c 65 6e 61 6d 65 20 69 73 20  ger filename is 
18eea 69 6e 76 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e  invariant as lon
18eeb 67 20 61 73 20 74 68 65 20 70 61 67 65 72 20 69  g as the pager i
18eec 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20  s.** open so it 
18eed 69 73 20 73 61 66 65 20 74 6f 20 61 63 63 65 73  is safe to acces
18eee 73 20 77 69 74 68 6f 75 74 20 74 68 65 20 42 74  s without the Bt
18eef 53 68 61 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f  Shared mutex..*/
18ef0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18ef1 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
18ef2 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e  te3BtreeGetFilen
18ef3 61 6d 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  ame(Btree *p){. 
18ef4 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d   assert( p->pBt-
18ef5 3e 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20  >pPager!=0 );.  
18ef6 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 61  return sqlite3Pa
18ef7 67 65 72 46 69 6c 65 6e 61 6d 65 28 70 2d 3e 70  gerFilename(p->p
18ef8 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a  Bt->pPager);.}..
18ef9 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
18efa 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65   pathname of the
18efb 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f   journal file fo
18efc 72 20 74 68 69 73 20 64 61 74 61 62 61 73 65 2e  r this database.
18efd 20 54 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76   The return.** v
18efe 61 6c 75 65 20 6f 66 20 74 68 69 73 20 72 6f 75  alue of this rou
18eff 74 69 6e 65 20 69 73 20 74 68 65 20 73 61 6d 65  tine is the same
18f00 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77   regardless of w
18f01 68 65 74 68 65 72 20 74 68 65 20 6a 6f 75 72 6e  hether the journ
18f02 61 6c 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62  al file.** has b
18f03 65 65 6e 20 63 72 65 61 74 65 64 20 6f 72 20 6e  een created or n
18f04 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61  ot..**.** The pa
18f05 67 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ger journal file
18f06 6e 61 6d 65 20 69 73 20 69 6e 76 61 72 69 61 6e  name is invarian
18f07 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65  t as long as the
18f08 20 70 61 67 65 72 20 69 73 0a 2a 2a 20 6f 70 65   pager is.** ope
18f09 6e 20 73 6f 20 69 74 20 69 73 20 73 61 66 65 20  n so it is safe 
18f0a 74 6f 20 61 63 63 65 73 73 20 77 69 74 68 6f 75  to access withou
18f0b 74 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6d  t the BtShared m
18f0c 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  utex..*/.SQLITE_
18f0d 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
18f0e 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  ar *sqlite3Btree
18f0f 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 42  GetJournalname(B
18f10 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65  tree *p){.  asse
18f11 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67  rt( p->pBt->pPag
18f12 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72  er!=0 );.  retur
18f13 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f  n sqlite3PagerJo
18f14 75 72 6e 61 6c 6e 61 6d 65 28 70 2d 3e 70 42 74  urnalname(p->pBt
18f15 2d 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a  ->pPager);.}../*
18f16 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a  .** Return non-z
18f17 65 72 6f 20 69 66 20 61 20 74 72 61 6e 73 61 63  ero if a transac
18f18 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2e 0a  tion is active..
18f19 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18f1a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
18f1b 65 65 49 73 49 6e 54 72 61 6e 73 28 42 74 72 65  eeIsInTrans(Btre
18f1c 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  e *p){.  assert(
18f1d 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33   p==0 || sqlite3
18f1e 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64  _mutex_held(p->d
18f1f 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72  b->mutex) );.  r
18f20 65 74 75 72 6e 20 28 70 20 26 26 20 28 70 2d 3e  eturn (p && (p->
18f21 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57  inTrans==TRANS_W
18f22 52 49 54 45 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  RITE));.}../*.**
18f23 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f   Return non-zero
18f24 20 69 66 20 61 20 72 65 61 64 20 28 6f 72 20 77   if a read (or w
18f25 72 69 74 65 29 20 74 72 61 6e 73 61 63 74 69 6f  rite) transactio
18f26 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a  n is active..*/.
18f27 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
18f28 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  nt sqlite3BtreeI
18f29 73 49 6e 52 65 61 64 54 72 61 6e 73 28 42 74 72  sInReadTrans(Btr
18f2a 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  ee *p){.  assert
18f2b 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ( p );.  assert(
18f2c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
18f2d 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  eld(p->db->mutex
18f2e 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d  ) );.  return p-
18f2f 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f  >inTrans!=TRANS_
18f30 4e 4f 4e 45 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f  NONE;.}..SQLITE_
18f31 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
18f32 74 65 33 42 74 72 65 65 49 73 49 6e 42 61 63 6b  te3BtreeIsInBack
18f33 75 70 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  up(Btree *p){.  
18f34 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20 20 61  assert( p );.  a
18f35 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
18f36 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d  utex_held(p->db-
18f37 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74  >mutex) );.  ret
18f38 75 72 6e 20 70 2d 3e 6e 42 61 63 6b 75 70 21 3d  urn p->nBackup!=
18f39 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  0;.}../*.** This
18f3a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
18f3b 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
18f3c 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20   blob of memory 
18f3d 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a  associated with.
18f3e 2a 2a 20 61 20 73 69 6e 67 6c 65 20 73 68 61 72  ** a single shar
18f3f 65 64 2d 62 74 72 65 65 2e 20 54 68 65 20 6d 65  ed-btree. The me
18f40 6d 6f 72 79 20 69 73 20 75 73 65 64 20 62 79 20  mory is used by 
18f41 63 6c 69 65 6e 74 20 63 6f 64 65 20 66 6f 72 20  client code for 
18f42 69 74 73 20 6f 77 6e 0a 2a 2a 20 70 75 72 70 6f  its own.** purpo
18f43 73 65 73 20 28 66 6f 72 20 65 78 61 6d 70 6c 65  ses (for example
18f44 2c 20 74 6f 20 73 74 6f 72 65 20 61 20 68 69 67  , to store a hig
18f45 68 2d 6c 65 76 65 6c 20 73 63 68 65 6d 61 20 61  h-level schema a
18f46 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 0a  ssociated with .
18f47 2a 2a 20 74 68 65 20 73 68 61 72 65 64 2d 62 74  ** the shared-bt
18f48 72 65 65 29 2e 20 54 68 65 20 62 74 72 65 65 20  ree). The btree 
18f49 6c 61 79 65 72 20 6d 61 6e 61 67 65 73 20 72 65  layer manages re
18f4a 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67  ference counting
18f4b 20 69 73 73 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54   issues..**.** T
18f4c 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68  he first time th
18f4d 69 73 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20  is is called on 
18f4e 61 20 73 68 61 72 65 64 2d 62 74 72 65 65 2c 20  a shared-btree, 
18f4f 6e 42 79 74 65 73 20 62 79 74 65 73 20 6f 66 20  nBytes bytes of 
18f50 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 6c  memory.** are al
18f51 6c 6f 63 61 74 65 64 2c 20 7a 65 72 6f 65 64 2c  located, zeroed,
18f52 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 74 6f   and returned to
18f53 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 46 6f 72   the caller. For
18f54 20 65 61 63 68 20 73 75 62 73 65 71 75 65 6e 74   each subsequent
18f55 20 0a 2a 2a 20 63 61 6c 6c 20 74 68 65 20 6e 42   .** call the nB
18f56 79 74 65 73 20 70 61 72 61 6d 65 74 65 72 20 69  ytes parameter i
18f57 73 20 69 67 6e 6f 72 65 64 20 61 6e 64 20 61 20  s ignored and a 
18f58 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73  pointer to the s
18f59 61 6d 65 20 62 6c 6f 62 0a 2a 2a 20 6f 66 20 6d  ame blob.** of m
18f5a 65 6d 6f 72 79 20 72 65 74 75 72 6e 65 64 2e 20  emory returned. 
18f5b 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 42  .**.** If the nB
18f5c 79 74 65 73 20 70 61 72 61 6d 65 74 65 72 20 69  ytes parameter i
18f5d 73 20 30 20 61 6e 64 20 74 68 65 20 62 6c 6f 62  s 0 and the blob
18f5e 20 6f 66 20 6d 65 6d 6f 72 79 20 68 61 73 20 6e   of memory has n
18f5f 6f 74 20 79 65 74 20 62 65 65 6e 0a 2a 2a 20 61  ot yet been.** a
18f60 6c 6c 6f 63 61 74 65 64 2c 20 61 20 6e 75 6c 6c  llocated, a null
18f61 20 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75   pointer is retu
18f62 72 6e 65 64 2e 20 49 66 20 74 68 65 20 62 6c 6f  rned. If the blo
18f63 62 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  b has already be
18f64 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c  en.** allocated,
18f65 20 69 74 20 69 73 20 72 65 74 75 72 6e 65 64 20   it is returned 
18f66 61 73 20 6e 6f 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a  as normal..**.**
18f67 20 4a 75 73 74 20 62 65 66 6f 72 65 20 74 68 65   Just before the
18f68 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 69 73   shared-btree is
18f69 20 63 6c 6f 73 65 64 2c 20 74 68 65 20 66 75 6e   closed, the fun
18f6a 63 74 69 6f 6e 20 70 61 73 73 65 64 20 61 73 20  ction passed as 
18f6b 74 68 65 20 0a 2a 2a 20 78 46 72 65 65 20 61 72  the .** xFree ar
18f6c 67 75 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20  gument when the 
18f6d 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
18f6e 6e 20 77 61 73 20 6d 61 64 65 20 69 73 20 69 6e  n was made is in
18f6f 76 6f 6b 65 64 20 6f 6e 20 74 68 65 20 0a 2a 2a  voked on the .**
18f70 20 62 6c 6f 62 20 6f 66 20 61 6c 6c 6f 63 61 74   blob of allocat
18f71 65 64 20 6d 65 6d 6f 72 79 2e 20 54 68 69 73 20  ed memory. This 
18f72 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  function should 
18f73 6e 6f 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  not call sqlite3
18f74 5f 66 72 65 65 28 29 0a 2a 2a 20 6f 6e 20 74 68  _free().** on th
18f75 65 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 62 74  e memory, the bt
18f76 72 65 65 20 6c 61 79 65 72 20 64 6f 65 73 20 74  ree layer does t
18f77 68 61 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  hat..*/.SQLITE_P
18f78 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
18f79 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 28  ite3BtreeSchema(
18f7a 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6e 42  Btree *p, int nB
18f7b 79 74 65 73 2c 20 76 6f 69 64 28 2a 78 46 72 65  ytes, void(*xFre
18f7c 65 29 28 76 6f 69 64 20 2a 29 29 7b 0a 20 20 42  e)(void *)){.  B
18f7d 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
18f7e 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33  ->pBt;.  sqlite3
18f7f 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
18f80 20 69 66 28 20 21 70 42 74 2d 3e 70 53 63 68 65   if( !pBt->pSche
18f81 6d 61 20 26 26 20 6e 42 79 74 65 73 20 29 7b 0a  ma && nBytes ){.
18f82 20 20 20 20 70 42 74 2d 3e 70 53 63 68 65 6d 61      pBt->pSchema
18f83 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
18f84 5a 65 72 6f 28 6e 42 79 74 65 73 29 3b 0a 20 20  Zero(nBytes);.  
18f85 20 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 65    pBt->xFreeSche
18f86 6d 61 20 3d 20 78 46 72 65 65 3b 0a 20 20 7d 0a  ma = xFree;.  }.
18f87 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
18f88 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
18f89 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d   pBt->pSchema;.}
18f8a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ../*.** Return S
18f8b 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41  QLITE_LOCKED_SHA
18f8c 52 45 44 43 41 43 48 45 20 69 66 20 61 6e 6f 74  REDCACHE if anot
18f8d 68 65 72 20 75 73 65 72 20 6f 66 20 74 68 65 20  her user of the 
18f8e 73 61 6d 65 20 73 68 61 72 65 64 20 0a 2a 2a 20  same shared .** 
18f8f 62 74 72 65 65 20 61 73 20 74 68 65 20 61 72 67  btree as the arg
18f90 75 6d 65 6e 74 20 68 61 6e 64 6c 65 20 68 6f 6c  ument handle hol
18f91 64 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20  ds an exclusive 
18f92 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 0a 2a 2a 20  lock on the .** 
18f93 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
18f94 62 6c 65 2e 20 4f 74 68 65 72 77 69 73 65 20 53  ble. Otherwise S
18f95 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c  QLITE_OK..*/.SQL
18f96 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
18f97 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65  sqlite3BtreeSche
18f98 6d 61 4c 6f 63 6b 65 64 28 42 74 72 65 65 20 2a  maLocked(Btree *
18f99 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  p){.  int rc;.  
18f9a 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
18f9b 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62  mutex_held(p->db
18f9c 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
18f9d 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
18f9e 70 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72 79  p);.  rc = query
18f9f 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
18fa0 4c 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52  Lock(p, MASTER_R
18fa1 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b  OOT, READ_LOCK);
18fa2 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
18fa3 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d  QLITE_OK || rc==
18fa4 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48  SQLITE_LOCKED_SH
18fa5 41 52 45 44 43 41 43 48 45 20 29 3b 0a 20 20 73  AREDCACHE );.  s
18fa6 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
18fa7 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
18fa8 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51  ;.}...#ifndef SQ
18fa9 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
18faa 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 74  _CACHE./*.** Obt
18fab 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68  ain a lock on th
18fac 65 20 74 61 62 6c 65 20 77 68 6f 73 65 20 72 6f  e table whose ro
18fad 6f 74 20 70 61 67 65 20 69 73 20 69 54 61 62 2e  ot page is iTab.
18fae 20 20 54 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73    The.** lock is
18faf 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66   a write lock if
18fb0 20 69 73 57 72 69 74 65 6c 6f 63 6b 20 69 73 20   isWritelock is 
18fb1 74 72 75 65 20 6f 72 20 61 20 72 65 61 64 20 6c  true or a read l
18fb2 6f 63 6b 0a 2a 2a 20 69 66 20 69 74 20 69 73 20  ock.** if it is 
18fb3 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  false..*/.SQLITE
18fb4 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
18fb5 69 74 65 33 42 74 72 65 65 4c 6f 63 6b 54 61 62  ite3BtreeLockTab
18fb6 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74  le(Btree *p, int
18fb7 20 69 54 61 62 2c 20 75 38 20 69 73 57 72 69 74   iTab, u8 isWrit
18fb8 65 4c 6f 63 6b 29 7b 0a 20 20 69 6e 74 20 72 63  eLock){.  int rc
18fb9 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
18fba 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61  assert( p->inTra
18fbb 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29  ns!=TRANS_NONE )
18fbc 3b 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61  ;.  if( p->shara
18fbd 62 6c 65 20 29 7b 0a 20 20 20 20 75 38 20 6c 6f  ble ){.    u8 lo
18fbe 63 6b 54 79 70 65 20 3d 20 52 45 41 44 5f 4c 4f  ckType = READ_LO
18fbf 43 4b 20 2b 20 69 73 57 72 69 74 65 4c 6f 63 6b  CK + isWriteLock
18fc0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 52 45  ;.    assert( RE
18fc1 41 44 5f 4c 4f 43 4b 2b 31 3d 3d 57 52 49 54 45  AD_LOCK+1==WRITE
18fc2 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 61 73 73  _LOCK );.    ass
18fc3 65 72 74 28 20 69 73 57 72 69 74 65 4c 6f 63 6b  ert( isWriteLock
18fc4 3d 3d 30 20 7c 7c 20 69 73 57 72 69 74 65 4c 6f  ==0 || isWriteLo
18fc5 63 6b 3d 3d 31 20 29 3b 0a 0a 20 20 20 20 73 71  ck==1 );..    sq
18fc6 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
18fc7 70 29 3b 0a 20 20 20 20 72 63 20 3d 20 71 75 65  p);.    rc = que
18fc8 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62  rySharedCacheTab
18fc9 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20  leLock(p, iTab, 
18fca 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 69  lockType);.    i
18fcb 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
18fcc 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
18fcd 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62  etSharedCacheTab
18fce 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 2c 20  leLock(p, iTab, 
18fcf 6c 6f 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 7d  lockType);.    }
18fd0 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
18fd1 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20  eLeave(p);.  }. 
18fd2 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65   return rc;.}.#e
18fd3 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
18fd4 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
18fd5 4f 42 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e  OB./*.** Argumen
18fd6 74 20 70 43 73 72 20 6d 75 73 74 20 62 65 20 61  t pCsr must be a
18fd7 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 66   cursor opened f
18fd8 6f 72 20 77 72 69 74 69 6e 67 20 6f 6e 20 61 6e  or writing on an
18fd9 20 0a 2a 2a 20 49 4e 54 4b 45 59 20 74 61 62 6c   .** INTKEY tabl
18fda 65 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  e currently poin
18fdb 74 69 6e 67 20 61 74 20 61 20 76 61 6c 69 64 20  ting at a valid 
18fdc 74 61 62 6c 65 20 65 6e 74 72 79 2e 20 0a 2a 2a  table entry. .**
18fdd 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d   This function m
18fde 6f 64 69 66 69 65 73 20 74 68 65 20 64 61 74 61  odifies the data
18fdf 20 73 74 6f 72 65 64 20 61 73 20 70 61 72 74 20   stored as part 
18fe0 6f 66 20 74 68 61 74 20 65 6e 74 72 79 2e 0a 2a  of that entry..*
18fe1 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 64 61  *.** Only the da
18fe2 74 61 20 63 6f 6e 74 65 6e 74 20 6d 61 79 20 6f  ta content may o
18fe3 6e 6c 79 20 62 65 20 6d 6f 64 69 66 69 65 64 2c  nly be modified,
18fe4 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69   it is not possi
18fe5 62 6c 65 20 74 6f 20 0a 2a 2a 20 63 68 61 6e 67  ble to .** chang
18fe6 65 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20  e the length of 
18fe7 74 68 65 20 64 61 74 61 20 73 74 6f 72 65 64 2e  the data stored.
18fe8 20 49 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   If this functio
18fe9 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68  n is called with
18fea 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 74  .** parameters t
18feb 68 61 74 20 61 74 74 65 6d 70 74 20 74 6f 20 77  hat attempt to w
18fec 72 69 74 65 20 70 61 73 74 20 74 68 65 20 65 6e  rite past the en
18fed 64 20 6f 66 20 74 68 65 20 65 78 69 73 74 69 6e  d of the existin
18fee 67 20 64 61 74 61 2c 0a 2a 2a 20 6e 6f 20 6d 6f  g data,.** no mo
18fef 64 69 66 69 63 61 74 69 6f 6e 73 20 61 72 65 20  difications are 
18ff0 6d 61 64 65 20 61 6e 64 20 53 51 4c 49 54 45 5f  made and SQLITE_
18ff1 43 4f 52 52 55 50 54 20 69 73 20 72 65 74 75 72  CORRUPT is retur
18ff2 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ned..*/.SQLITE_P
18ff3 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
18ff4 65 33 42 74 72 65 65 50 75 74 44 61 74 61 28 42  e3BtreePutData(B
18ff5 74 43 75 72 73 6f 72 20 2a 70 43 73 72 2c 20 75  tCursor *pCsr, u
18ff6 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61  32 offset, u32 a
18ff7 6d 74 2c 20 76 6f 69 64 20 2a 7a 29 7b 0a 20 20  mt, void *z){.  
18ff8 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74  int rc;.  assert
18ff9 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
18ffa 65 78 28 70 43 73 72 29 20 29 3b 0a 20 20 61 73  ex(pCsr) );.  as
18ffb 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
18ffc 74 65 78 5f 68 65 6c 64 28 70 43 73 72 2d 3e 70  tex_held(pCsr->p
18ffd 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78  Btree->db->mutex
18ffe 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
18fff 43 73 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48  Csr->isIncrblobH
19000 61 6e 64 6c 65 20 29 3b 0a 0a 20 20 72 63 20 3d  andle );..  rc =
19001 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f   restoreCursorPo
19002 73 69 74 69 6f 6e 28 70 43 73 72 29 3b 0a 20 20  sition(pCsr);.  
19003 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
19004 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
19005 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  rc;.  }.  assert
19006 28 20 70 43 73 72 2d 3e 65 53 74 61 74 65 21 3d  ( pCsr->eState!=
19007 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
19008 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 73 72  EK );.  if( pCsr
19009 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52  ->eState!=CURSOR
1900a 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72 65  _VALID ){.    re
1900b 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f 52  turn SQLITE_ABOR
1900c 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65  T;.  }..  /* Che
1900d 63 6b 20 73 6f 6d 65 20 61 73 73 75 6d 70 74 69  ck some assumpti
1900e 6f 6e 73 3a 20 0a 20 20 2a 2a 20 20 20 28 61 29  ons: .  **   (a)
1900f 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6f   the cursor is o
19010 70 65 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 2c  pen for writing,
19011 0a 20 20 2a 2a 20 20 20 28 62 29 20 74 68 65 72  .  **   (b) ther
19012 65 20 69 73 20 61 20 72 65 61 64 2f 77 72 69 74  e is a read/writ
19013 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70  e transaction op
19014 65 6e 2c 0a 20 20 2a 2a 20 20 20 28 63 29 20 74  en,.  **   (c) t
19015 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f  he connection ho
19016 6c 64 73 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b  lds a write-lock
19017 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 28 69   on the table (i
19018 66 20 72 65 71 75 69 72 65 64 29 2c 0a 20 20 2a  f required),.  *
19019 2a 20 20 20 28 64 29 20 74 68 65 72 65 20 61 72  *   (d) there ar
1901a 65 20 6e 6f 20 63 6f 6e 66 6c 69 63 74 69 6e 67  e no conflicting
1901b 20 72 65 61 64 2d 6c 6f 63 6b 73 2c 20 61 6e 64   read-locks, and
1901c 0a 20 20 2a 2a 20 20 20 28 65 29 20 74 68 65 20  .  **   (e) the 
1901d 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 61 74  cursor points at
1901e 20 61 20 76 61 6c 69 64 20 72 6f 77 20 6f 66 20   a valid row of 
1901f 61 6e 20 69 6e 74 4b 65 79 20 74 61 62 6c 65 2e  an intKey table.
19020 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 73  .  */.  if( !pCs
19021 72 2d 3e 77 72 46 6c 61 67 20 29 7b 0a 20 20 20  r->wrFlag ){.   
19022 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52   return SQLITE_R
19023 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 61  EADONLY;.  }.  a
19024 73 73 65 72 74 28 20 21 70 43 73 72 2d 3e 70 42  ssert( !pCsr->pB
19025 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 26 26 20 70  t->readOnly && p
19026 43 73 72 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e  Csr->pBt->inTran
19027 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57  saction==TRANS_W
19028 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74  RITE );.  assert
19029 28 20 68 61 73 53 68 61 72 65 64 43 61 63 68 65  ( hasSharedCache
1902a 54 61 62 6c 65 4c 6f 63 6b 28 70 43 73 72 2d 3e  TableLock(pCsr->
1902b 70 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 67  pBtree, pCsr->pg
1902c 6e 6f 52 6f 6f 74 2c 20 30 2c 20 32 29 20 29 3b  noRoot, 0, 2) );
1902d 0a 20 20 61 73 73 65 72 74 28 20 21 68 61 73 52  .  assert( !hasR
1902e 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 43 73  eadConflicts(pCs
1902f 72 2d 3e 70 42 74 72 65 65 2c 20 70 43 73 72 2d  r->pBtree, pCsr-
19030 3e 70 67 6e 6f 52 6f 6f 74 29 20 29 3b 0a 20 20  >pgnoRoot) );.  
19031 61 73 73 65 72 74 28 20 70 43 73 72 2d 3e 61 70  assert( pCsr->ap
19032 50 61 67 65 5b 70 43 73 72 2d 3e 69 50 61 67 65  Page[pCsr->iPage
19033 5d 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 0a 20 20  ]->intKey );..  
19034 72 65 74 75 72 6e 20 61 63 63 65 73 73 50 61 79  return accessPay
19035 6c 6f 61 64 28 70 43 73 72 2c 20 6f 66 66 73 65  load(pCsr, offse
19036 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e 65  t, amt, (unsigne
19037 64 20 63 68 61 72 20 2a 29 7a 2c 20 31 29 3b 0a  d char *)z, 1);.
19038 7d 0a 0a 2f 2a 20 0a 2a 2a 20 53 65 74 20 61 20  }../* .** Set a 
19039 66 6c 61 67 20 6f 6e 20 74 68 69 73 20 63 75 72  flag on this cur
1903a 73 6f 72 20 74 6f 20 63 61 63 68 65 20 74 68 65  sor to cache the
1903b 20 6c 6f 63 61 74 69 6f 6e 73 20 6f 66 20 70 61   locations of pa
1903c 67 65 73 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a  ges from the .**
1903d 20 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 66   overflow list f
1903e 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72  or the current r
1903f 6f 77 2e 20 54 68 69 73 20 69 73 20 75 73 65 64  ow. This is used
19040 20 62 79 20 63 75 72 73 6f 72 73 20 6f 70 65 6e   by cursors open
19041 65 64 0a 2a 2a 20 66 6f 72 20 69 6e 63 72 65 6d  ed.** for increm
19042 65 6e 74 61 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e  ental blob IO on
19043 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ly..**.** This f
19044 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 61 20 66  unction sets a f
19045 6c 61 67 20 6f 6e 6c 79 2e 20 54 68 65 20 61 63  lag only. The ac
19046 74 75 61 6c 20 70 61 67 65 20 6c 6f 63 61 74 69  tual page locati
19047 6f 6e 20 63 61 63 68 65 0a 2a 2a 20 28 73 74 6f  on cache.** (sto
19048 72 65 64 20 69 6e 20 42 74 43 75 72 73 6f 72 2e  red in BtCursor.
19049 61 4f 76 65 72 66 6c 6f 77 5b 5d 29 20 69 73 20  aOverflow[]) is 
1904a 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 75 73  allocated and us
1904b 65 64 20 62 79 20 66 75 6e 63 74 69 6f 6e 0a 2a  ed by function.*
1904c 2a 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28  * accessPayload(
1904d 29 20 28 74 68 65 20 77 6f 72 6b 65 72 20 66 75  ) (the worker fu
1904e 6e 63 74 69 6f 6e 20 66 6f 72 20 73 71 6c 69 74  nction for sqlit
1904f 65 33 42 74 72 65 65 44 61 74 61 28 29 20 61 6e  e3BtreeData() an
19050 64 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65  d.** sqlite3Btre
19051 65 50 75 74 44 61 74 61 28 29 29 2e 0a 2a 2f 0a  ePutData())..*/.
19052 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
19053 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
19054 43 61 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 74  CacheOverflow(Bt
19055 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
19056 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
19057 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
19058 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
19059 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1905a 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62  pCur->pBtree->db
1905b 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
1905c 73 65 72 74 28 21 70 43 75 72 2d 3e 69 73 49 6e  sert(!pCur->isIn
1905d 63 72 62 6c 6f 62 48 61 6e 64 6c 65 29 3b 0a 20  crblobHandle);. 
1905e 20 61 73 73 65 72 74 28 21 70 43 75 72 2d 3e 61   assert(!pCur->a
1905f 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43 75  Overflow);.  pCu
19060 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e  r->isIncrblobHan
19061 64 6c 65 20 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69  dle = 1;.}.#endi
19062 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
19063 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65 65 2e  ** End of btree.
19064 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
19065 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19066 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19067 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
19068 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 61  ** Begin file ba
19069 63 6b 75 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ckup.c *********
1906a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1906b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1906c 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20 4a 61  */./*.** 2009 Ja
1906d 6e 75 61 72 79 20 32 38 0a 2a 2a 0a 2a 2a 20 54  nuary 28.**.** T
1906e 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
1906f 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
19070 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
19071 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
19072 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
19073 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
19074 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
19075 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
19076 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
19077 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
19078 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
19079 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
1907a 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
1907b 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
1907c 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
1907d 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
1907e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
1907f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19081 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19082 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19083 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
19084 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  file contains th
19085 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
19086 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
19087 62 61 63 6b 75 70 5f 58 58 58 28 29 20 0a 2a 2a  backup_XXX() .**
19088 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 20 61   API functions a
19089 6e 64 20 74 68 65 20 72 65 6c 61 74 65 64 20 66  nd the related f
1908a 65 61 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 24  eatures..**.** $
1908b 49 64 3a 20 62 61 63 6b 75 70 2e 63 2c 76 20 31  Id: backup.c,v 1
1908c 2e 31 39 20 32 30 30 39 2f 30 37 2f 30 36 20 31  .19 2009/07/06 1
1908d 39 3a 30 33 3a 31 33 20 64 72 68 20 45 78 70 20  9:03:13 drh Exp 
1908e 24 0a 2a 2f 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74  $.*/../* Macro t
1908f 6f 20 66 69 6e 64 20 74 68 65 20 6d 69 6e 69 6d  o find the minim
19090 75 6d 20 6f 66 20 74 77 6f 20 6e 75 6d 65 72 69  um of two numeri
19091 63 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66  c values..*/.#if
19092 6e 64 65 66 20 4d 49 4e 0a 23 20 64 65 66 69 6e  ndef MIN.# defin
19093 65 20 4d 49 4e 28 78 2c 79 29 20 28 28 78 29 3c  e MIN(x,y) ((x)<
19094 28 79 29 3f 28 78 29 3a 28 79 29 29 0a 23 65 6e  (y)?(x):(y)).#en
19095 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63  dif../*.** Struc
19096 74 75 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 66  ture allocated f
19097 6f 72 20 65 61 63 68 20 62 61 63 6b 75 70 20 6f  or each backup o
19098 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72  peration..*/.str
19099 75 63 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  uct sqlite3_back
1909a 75 70 20 7b 0a 20 20 73 71 6c 69 74 65 33 2a 20  up {.  sqlite3* 
1909b 70 44 65 73 74 44 62 3b 20 20 20 20 20 20 20 20  pDestDb;        
1909c 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 64  /* Destination d
1909d 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
1909e 2f 0a 20 20 42 74 72 65 65 20 2a 70 44 65 73 74  /.  Btree *pDest
1909f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
190a0 44 65 73 74 69 6e 61 74 69 6f 6e 20 62 2d 74 72  Destination b-tr
190a1 65 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 33 32  ee file */.  u32
190a2 20 69 44 65 73 74 53 63 68 65 6d 61 3b 20 20 20   iDestSchema;   
190a3 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61        /* Origina
190a4 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20  l schema cookie 
190a5 69 6e 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 2a  in destination *
190a6 2f 0a 20 20 69 6e 74 20 62 44 65 73 74 4c 6f 63  /.  int bDestLoc
190a7 6b 65 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ked;         /* 
190a8 54 72 75 65 20 6f 6e 63 65 20 61 20 77 72 69 74  True once a writ
190a9 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  e-transaction is
190aa 20 6f 70 65 6e 20 6f 6e 20 70 44 65 73 74 20 2a   open on pDest *
190ab 2f 0a 0a 20 20 50 67 6e 6f 20 69 4e 65 78 74 3b  /..  Pgno iNext;
190ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
190ad 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   Page number of 
190ae 74 68 65 20 6e 65 78 74 20 73 6f 75 72 63 65 20  the next source 
190af 70 61 67 65 20 74 6f 20 63 6f 70 79 20 2a 2f 0a  page to copy */.
190b0 20 20 73 71 6c 69 74 65 33 2a 20 70 53 72 63 44    sqlite3* pSrcD
190b1 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f  b;         /* So
190b2 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68 61  urce database ha
190b3 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65 20  ndle */.  Btree 
190b4 2a 70 53 72 63 3b 20 20 20 20 20 20 20 20 20 20  *pSrc;          
190b5 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 62 2d 74     /* Source b-t
190b6 72 65 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 69  ree file */..  i
190b7 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
190b8 20 20 20 20 20 20 20 20 2f 2a 20 42 61 63 6b 75          /* Backu
190b9 70 20 70 72 6f 63 65 73 73 20 65 72 72 6f 72 20  p process error 
190ba 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68  code */..  /* Th
190bb 65 73 65 20 74 77 6f 20 76 61 72 69 61 62 6c 65  ese two variable
190bc 73 20 61 72 65 20 73 65 74 20 62 79 20 65 76 65  s are set by eve
190bd 72 79 20 63 61 6c 6c 20 74 6f 20 62 61 63 6b 75  ry call to backu
190be 70 5f 73 74 65 70 28 29 2e 20 54 68 65 79 20 61  p_step(). They a
190bf 72 65 0a 20 20 2a 2a 20 72 65 61 64 20 62 79 20  re.  ** read by 
190c0 63 61 6c 6c 73 20 74 6f 20 62 61 63 6b 75 70 5f  calls to backup_
190c1 72 65 6d 61 69 6e 69 6e 67 28 29 20 61 6e 64 20  remaining() and 
190c2 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74  backup_pagecount
190c3 28 29 2e 0a 20 20 2a 2f 0a 20 20 50 67 6e 6f 20  ()..  */.  Pgno 
190c4 6e 52 65 6d 61 69 6e 69 6e 67 3b 20 20 20 20 20  nRemaining;     
190c5 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
190c6 20 70 61 67 65 73 20 6c 65 66 74 20 74 6f 20 63   pages left to c
190c7 6f 70 79 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 50  opy */.  Pgno nP
190c8 61 67 65 63 6f 75 6e 74 3b 20 20 20 20 20 20 20  agecount;       
190c9 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65    /* Total numbe
190ca 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 63 6f  r of pages to co
190cb 70 79 20 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 41  py */..  int isA
190cc 74 74 61 63 68 65 64 3b 20 20 20 20 20 20 20 20  ttached;        
190cd 20 20 2f 2a 20 54 72 75 65 20 6f 6e 63 65 20 62    /* True once b
190ce 61 63 6b 75 70 20 68 61 73 20 62 65 65 6e 20 72  ackup has been r
190cf 65 67 69 73 74 65 72 65 64 20 77 69 74 68 20 70  egistered with p
190d0 61 67 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ager */.  sqlite
190d1 33 5f 62 61 63 6b 75 70 20 2a 70 4e 65 78 74 3b  3_backup *pNext;
190d2 20 20 20 2f 2a 20 4e 65 78 74 20 62 61 63 6b 75     /* Next backu
190d3 70 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  p associated wit
190d4 68 20 73 6f 75 72 63 65 20 70 61 67 65 72 20 2a  h source pager *
190d5 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 48 52 45  /.};../*.** THRE
190d6 41 44 20 53 41 46 45 54 59 20 4e 4f 54 45 53 3a  AD SAFETY NOTES:
190d7 0a 2a 2a 0a 2a 2a 20 20 20 4f 6e 63 65 20 69 74  .**.**   Once it
190d8 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65   has been create
190d9 64 20 75 73 69 6e 67 20 62 61 63 6b 75 70 5f 69  d using backup_i
190da 6e 69 74 28 29 2c 20 61 20 73 69 6e 67 6c 65 20  nit(), a single 
190db 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 0a 2a  sqlite3_backup.*
190dc 2a 20 20 20 73 74 72 75 63 74 75 72 65 20 6d 61  *   structure ma
190dd 79 20 62 65 20 61 63 63 65 73 73 65 64 20 76 69  y be accessed vi
190de 61 20 74 77 6f 20 67 72 6f 75 70 73 20 6f 66 20  a two groups of 
190df 74 68 72 65 61 64 2d 73 61 66 65 20 65 6e 74 72  thread-safe entr
190e0 79 20 70 6f 69 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20  y points:.**.** 
190e1 20 20 20 20 2a 20 56 69 61 20 74 68 65 20 73 71      * Via the sq
190e2 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 58 58 58  lite3_backup_XXX
190e3 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20  () API function 
190e4 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 61 6e  backup_step() an
190e5 64 20 0a 2a 2a 20 20 20 20 20 20 20 62 61 63 6b  d .**       back
190e6 75 70 5f 66 69 6e 69 73 68 28 29 2e 20 42 6f 74  up_finish(). Bot
190e7 68 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e  h these function
190e8 73 20 6f 62 74 61 69 6e 20 74 68 65 20 73 6f 75  s obtain the sou
190e9 72 63 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  rce database.** 
190ea 20 20 20 20 20 20 68 61 6e 64 6c 65 20 6d 75 74        handle mut
190eb 65 78 20 61 6e 64 20 74 68 65 20 6d 75 74 65 78  ex and the mutex
190ec 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
190ed 20 74 68 65 20 73 6f 75 72 63 65 20 42 74 53 68   the source BtSh
190ee 61 72 65 64 20 0a 2a 2a 20 20 20 20 20 20 20 73  ared .**       s
190ef 74 72 75 63 74 75 72 65 2c 20 69 6e 20 74 68 61  tructure, in tha
190f0 74 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 20  t order..**.**  
190f1 20 20 20 2a 20 56 69 61 20 74 68 65 20 42 61 63     * Via the Bac
190f2 6b 75 70 55 70 64 61 74 65 28 29 20 61 6e 64 20  kupUpdate() and 
190f3 42 61 63 6b 75 70 52 65 73 74 61 72 74 28 29 20  BackupRestart() 
190f4 66 75 6e 63 74 69 6f 6e 73 2c 20 77 68 69 63 68  functions, which
190f5 20 61 72 65 0a 2a 2a 20 20 20 20 20 20 20 69 6e   are.**       in
190f6 76 6f 6b 65 64 20 62 79 20 74 68 65 20 70 61 67  voked by the pag
190f7 65 72 20 6c 61 79 65 72 20 74 6f 20 72 65 70 6f  er layer to repo
190f8 72 74 20 76 61 72 69 6f 75 73 20 73 74 61 74 65  rt various state
190f9 20 63 68 61 6e 67 65 73 20 69 6e 0a 2a 2a 20 20   changes in.**  
190fa 20 20 20 20 20 74 68 65 20 70 61 67 65 20 63 61       the page ca
190fb 63 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  che associated w
190fc 69 74 68 20 74 68 65 20 73 6f 75 72 63 65 20 64  ith the source d
190fd 61 74 61 62 61 73 65 2e 20 54 68 65 20 6d 75 74  atabase. The mut
190fe 65 78 0a 2a 2a 20 20 20 20 20 20 20 61 73 73 6f  ex.**       asso
190ff 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
19100 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20  source database 
19101 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74 75  BtShared structu
19102 72 65 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 0a  re will always .
19103 2a 2a 20 20 20 20 20 20 20 62 65 20 68 65 6c 64  **       be held
19104 20 77 68 65 6e 20 65 69 74 68 65 72 20 6f 66 20   when either of 
19105 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20  these functions 
19106 61 72 65 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a  are invoked..**.
19107 2a 2a 20 20 20 54 68 65 20 6f 74 68 65 72 20 73  **   The other s
19108 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 58 58  qlite3_backup_XX
19109 58 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f 6e  X() API function
1910a 73 2c 20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e  s, backup_remain
1910b 69 6e 67 28 29 20 61 6e 64 0a 2a 2a 20 20 20 62  ing() and.**   b
1910c 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28  ackup_pagecount(
1910d 29 20 61 72 65 20 6e 6f 74 20 74 68 72 65 61 64  ) are not thread
1910e 2d 73 61 66 65 20 66 75 6e 63 74 69 6f 6e 73 2e  -safe functions.
1910f 20 49 66 20 74 68 65 79 20 61 72 65 20 63 61 6c   If they are cal
19110 6c 65 64 0a 2a 2a 20 20 20 77 68 69 6c 65 20 73  led.**   while s
19111 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 65 61 64  ome other thread
19112 20 69 73 20 63 61 6c 6c 69 6e 67 20 62 61 63 6b   is calling back
19113 75 70 5f 73 74 65 70 28 29 20 6f 72 20 62 61 63  up_step() or bac
19114 6b 75 70 5f 66 69 6e 69 73 68 28 29 2c 0a 2a 2a  kup_finish(),.**
19115 20 20 20 74 68 65 20 76 61 6c 75 65 73 20 72 65     the values re
19116 74 75 72 6e 65 64 20 6d 61 79 20 62 65 20 69 6e  turned may be in
19117 76 61 6c 69 64 2e 20 54 68 65 72 65 20 69 73 20  valid. There is 
19118 6e 6f 20 77 61 79 20 66 6f 72 20 61 20 63 61 6c  no way for a cal
19119 6c 20 74 6f 0a 2a 2a 20 20 20 42 61 63 6b 75 70  l to.**   Backup
1911a 55 70 64 61 74 65 28 29 20 6f 72 20 42 61 63 6b  Update() or Back
1911b 75 70 52 65 73 74 61 72 74 28 29 20 74 6f 20 69  upRestart() to i
1911c 6e 74 65 72 66 65 72 65 20 77 69 74 68 20 62 61  nterfere with ba
1911d 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29  ckup_remaining()
1911e 0a 2a 2a 20 20 20 6f 72 20 62 61 63 6b 75 70 5f  .**   or backup_
1911f 70 61 67 65 63 6f 75 6e 74 28 29 2e 0a 2a 2a 0a  pagecount()..**.
19120 2a 2a 20 20 20 44 65 70 65 6e 64 69 6e 67 20 6f  **   Depending o
19121 6e 20 74 68 65 20 53 51 4c 69 74 65 20 63 6f 6e  n the SQLite con
19122 66 69 67 75 72 61 74 69 6f 6e 2c 20 74 68 65 20  figuration, the 
19123 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 73  database handles
19124 20 61 6e 64 2f 6f 72 0a 2a 2a 20 20 20 74 68 65   and/or.**   the
19125 20 42 74 72 65 65 20 6f 62 6a 65 63 74 73 20 6d   Btree objects m
19126 61 79 20 68 61 76 65 20 74 68 65 69 72 20 6f 77  ay have their ow
19127 6e 20 6d 75 74 65 78 65 73 20 74 68 61 74 20 72  n mutexes that r
19128 65 71 75 69 72 65 20 6c 6f 63 6b 69 6e 67 2e 0a  equire locking..
19129 2a 2a 20 20 20 4e 6f 6e 2d 73 68 61 72 61 62 6c  **   Non-sharabl
1912a 65 20 42 74 72 65 65 73 20 28 69 6e 2d 6d 65 6d  e Btrees (in-mem
1912b 6f 72 79 20 64 61 74 61 62 61 73 65 73 20 66 6f  ory databases fo
1912c 72 20 65 78 61 6d 70 6c 65 29 2c 20 64 6f 20 6e  r example), do n
1912d 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20 61 73 73  ot have.**   ass
1912e 6f 63 69 61 74 65 64 20 6d 75 74 65 78 65 73 2e  ociated mutexes.
1912f 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  .*/../*.** Retur
19130 6e 20 61 20 70 6f 69 6e 74 65 72 20 63 6f 72 72  n a pointer corr
19131 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 64 61 74  esponding to dat
19132 61 62 61 73 65 20 7a 44 62 20 28 69 2e 65 2e 20  abase zDb (i.e. 
19133 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70 22 29 0a  "main", "temp").
19134 2a 2a 20 69 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e  ** in connection
19135 20 68 61 6e 64 6c 65 20 70 44 62 2e 20 49 66 20   handle pDb. If 
19136 73 75 63 68 20 61 20 64 61 74 61 62 61 73 65 20  such a database 
19137 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c  cannot be found,
19138 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 4e 55 4c   return.** a NUL
19139 4c 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 77 72  L pointer and wr
1913a 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ite an error mes
1913b 73 61 67 65 20 74 6f 20 70 45 72 72 6f 72 44 62  sage to pErrorDb
1913c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22  ..**.** If the "
1913d 74 65 6d 70 22 20 64 61 74 61 62 61 73 65 20 69  temp" database i
1913e 73 20 72 65 71 75 65 73 74 65 64 2c 20 69 74 20  s requested, it 
1913f 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 6f  may need to be o
19140 70 65 6e 65 64 20 62 79 20 74 68 69 73 20 0a 2a  pened by this .*
19141 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 61  * function. If a
19142 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77  n error occurs w
19143 68 69 6c 65 20 64 6f 69 6e 67 20 73 6f 2c 20 72  hile doing so, r
19144 65 74 75 72 6e 20 30 20 61 6e 64 20 77 72 69 74  eturn 0 and writ
19145 65 20 61 6e 20 0a 2a 2a 20 65 72 72 6f 72 20 6d  e an .** error m
19146 65 73 73 61 67 65 20 74 6f 20 70 45 72 72 6f 72  essage to pError
19147 44 62 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 42 74  Db..*/.static Bt
19148 72 65 65 20 2a 66 69 6e 64 42 74 72 65 65 28 73  ree *findBtree(s
19149 71 6c 69 74 65 33 20 2a 70 45 72 72 6f 72 44 62  qlite3 *pErrorDb
1914a 2c 20 73 71 6c 69 74 65 33 20 2a 70 44 62 2c 20  , sqlite3 *pDb, 
1914b 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 29  const char *zDb)
1914c 7b 0a 20 20 69 6e 74 20 69 20 3d 20 73 71 6c 69  {.  int i = sqli
1914d 74 65 33 46 69 6e 64 44 62 4e 61 6d 65 28 70 44  te3FindDbName(pD
1914e 62 2c 20 7a 44 62 29 3b 0a 0a 20 20 69 66 28 20  b, zDb);..  if( 
1914f 69 3d 3d 31 20 29 7b 0a 20 20 20 20 50 61 72 73  i==1 ){.    Pars
19150 65 20 2a 70 50 61 72 73 65 3b 0a 20 20 20 20 69  e *pParse;.    i
19151 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 70  nt rc = 0;.    p
19152 50 61 72 73 65 20 3d 20 73 71 6c 69 74 65 33 53  Parse = sqlite3S
19153 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28 70 45  tackAllocZero(pE
19154 72 72 6f 72 44 62 2c 20 73 69 7a 65 6f 66 28 2a  rrorDb, sizeof(*
19155 70 50 61 72 73 65 29 29 3b 0a 20 20 20 20 69 66  pParse));.    if
19156 28 20 70 50 61 72 73 65 3d 3d 30 20 29 7b 0a 20  ( pParse==0 ){. 
19157 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
19158 72 28 70 45 72 72 6f 72 44 62 2c 20 53 51 4c 49  r(pErrorDb, SQLI
19159 54 45 5f 4e 4f 4d 45 4d 2c 20 22 6f 75 74 20 6f  TE_NOMEM, "out o
1915a 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 20 20  f memory");.    
1915b 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
1915c 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  MEM;.    }else{.
1915d 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62        pParse->db
1915e 20 3d 20 70 44 62 3b 0a 20 20 20 20 20 20 69 66   = pDb;.      if
1915f 28 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d  ( sqlite3OpenTem
19160 70 44 61 74 61 62 61 73 65 28 70 50 61 72 73 65  pDatabase(pParse
19161 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  ) ){.        sql
19162 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 70  ite3ErrorClear(p
19163 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20  Parse);.        
19164 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 45 72  sqlite3Error(pEr
19165 72 6f 72 44 62 2c 20 70 50 61 72 73 65 2d 3e 72  rorDb, pParse->r
19166 63 2c 20 22 25 73 22 2c 20 70 50 61 72 73 65 2d  c, "%s", pParse-
19167 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20  >zErrMsg);.     
19168 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
19169 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20  RROR;.      }.  
1916a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b      sqlite3Stack
1916b 46 72 65 65 28 70 45 72 72 6f 72 44 62 2c 20 70  Free(pErrorDb, p
1916c 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 0a 20 20  Parse);.    }.  
1916d 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
1916e 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
1916f 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 3c 30  }.  }..  if( i<0
19170 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
19171 72 72 6f 72 28 70 45 72 72 6f 72 44 62 2c 20 53  rror(pErrorDb, S
19172 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 22 75 6e  QLITE_ERROR, "un
19173 6b 6e 6f 77 6e 20 64 61 74 61 62 61 73 65 20 25  known database %
19174 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20 20 72 65  s", zDb);.    re
19175 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 72  turn 0;.  }..  r
19176 65 74 75 72 6e 20 70 44 62 2d 3e 61 44 62 5b 69  eturn pDb->aDb[i
19177 5d 2e 70 42 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ].pBt;.}../*.** 
19178 43 72 65 61 74 65 20 61 6e 20 73 71 6c 69 74 65  Create an sqlite
19179 33 5f 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73  3_backup process
1917a 20 74 6f 20 63 6f 70 79 20 74 68 65 20 63 6f 6e   to copy the con
1917b 74 65 6e 74 73 20 6f 66 20 7a 53 72 63 44 62 20  tents of zSrcDb 
1917c 66 72 6f 6d 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69  from.** connecti
1917d 6f 6e 20 68 61 6e 64 6c 65 20 70 53 72 63 44 62  on handle pSrcDb
1917e 20 74 6f 20 7a 44 65 73 74 44 62 20 69 6e 20 70   to zDestDb in p
1917f 44 65 73 74 44 62 2e 20 49 66 20 73 75 63 63 65  DestDb. If succe
19180 73 73 66 75 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a  ssful, return.**
19181 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
19182 65 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 62 61  e new sqlite3_ba
19183 63 6b 75 70 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a  ckup object..**.
19184 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  ** If an error o
19185 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72  ccurs, NULL is r
19186 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e 20 65  eturned and an e
19187 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 65 72  rror code and er
19188 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 73  ror message.** s
19189 74 6f 72 65 64 20 69 6e 20 64 61 74 61 62 61 73  tored in databas
1918a 65 20 68 61 6e 64 6c 65 20 70 44 65 73 74 44 62  e handle pDestDb
1918b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1918c 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
1918d 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69  sqlite3_backup_i
1918e 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 2a 20  nit(.  sqlite3* 
1918f 70 44 65 73 74 44 62 2c 20 20 20 20 20 20 20 20  pDestDb,        
19190 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19191 44 61 74 61 62 61 73 65 20 74 6f 20 77 72 69 74  Database to writ
19192 65 20 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  e to */.  const 
19193 63 68 61 72 20 2a 7a 44 65 73 74 44 62 2c 20 20  char *zDestDb,  
19194 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19195 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62  /* Name of datab
19196 61 73 65 20 77 69 74 68 69 6e 20 70 44 65 73 74  ase within pDest
19197 44 62 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 2a  Db */.  sqlite3*
19198 20 70 53 72 63 44 62 2c 20 20 20 20 20 20 20 20   pSrcDb,        
19199 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1919a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
1919b 74 69 6f 6e 20 74 6f 20 72 65 61 64 20 66 72 6f  tion to read fro
1919c 6d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  m */.  const cha
1919d 72 20 2a 7a 53 72 63 44 62 20 20 20 20 20 20 20  r *zSrcDb       
1919e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1919f 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65  Name of database
191a0 20 77 69 74 68 69 6e 20 70 53 72 63 44 62 20 2a   within pSrcDb *
191a1 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62  /.){.  sqlite3_b
191a2 61 63 6b 75 70 20 2a 70 3b 20 20 20 20 20 20 20  ackup *p;       
191a3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
191a4 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20  Value to return 
191a5 2a 2f 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 68  */..  /* Lock th
191a6 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
191a7 65 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 64 65  e handle. The de
191a8 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61  stination databa
191a9 73 65 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20 69  se.  ** handle i
191aa 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 20 69 6e 20  s not locked in 
191ab 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 62 75  this routine, bu
191ac 74 20 69 74 20 69 73 20 6c 6f 63 6b 65 64 20 69  t it is locked i
191ad 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 62  n.  ** sqlite3_b
191ae 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 54 68  ackup_step(). Th
191af 65 20 75 73 65 72 20 69 73 20 72 65 71 75 69 72  e user is requir
191b0 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61  ed to ensure tha
191b1 74 20 6e 6f 0a 20 20 2a 2a 20 6f 74 68 65 72 20  t no.  ** other 
191b2 74 68 72 65 61 64 20 61 63 63 65 73 73 65 73 20  thread accesses 
191b3 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
191b4 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 64  handle for the d
191b5 75 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 6f 66 20  uration.  ** of 
191b6 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61  the backup opera
191b7 74 69 6f 6e 2e 20 20 41 6e 79 20 61 74 74 65 6d  tion.  Any attem
191b8 70 74 20 74 6f 20 75 73 65 20 74 68 65 20 64 65  pt to use the de
191b9 73 74 69 6e 61 74 69 6f 6e 0a 20 20 2a 2a 20 64  stination.  ** d
191ba 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
191bb 6f 6e 20 77 68 69 6c 65 20 61 20 62 61 63 6b 75  on while a backu
191bc 70 20 69 73 20 69 6e 20 70 72 6f 67 72 65 73 73  p is in progress
191bd 20 6d 61 79 20 63 61 75 73 65 0a 20 20 2a 2a 20   may cause.  ** 
191be 61 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 20 6f 72  a malfunction or
191bf 20 61 20 64 65 61 64 6c 6f 63 6b 2e 0a 20 20 2a   a deadlock..  *
191c0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  /.  sqlite3_mute
191c1 78 5f 65 6e 74 65 72 28 70 53 72 63 44 62 2d 3e  x_enter(pSrcDb->
191c2 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65  mutex);.  sqlite
191c3 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 44  3_mutex_enter(pD
191c4 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 0a  estDb->mutex);..
191c5 20 20 69 66 28 20 70 53 72 63 44 62 3d 3d 70 44    if( pSrcDb==pD
191c6 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71 6c  estDb ){.    sql
191c7 69 74 65 33 45 72 72 6f 72 28 0a 20 20 20 20 20  ite3Error(.     
191c8 20 20 20 70 44 65 73 74 44 62 2c 20 53 51 4c 49     pDestDb, SQLI
191c9 54 45 5f 45 52 52 4f 52 2c 20 22 73 6f 75 72 63  TE_ERROR, "sourc
191ca 65 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f  e and destinatio
191cb 6e 20 6d 75 73 74 20 62 65 20 64 69 73 74 69 6e  n must be distin
191cc 63 74 22 0a 20 20 20 20 29 3b 0a 20 20 20 20 70  ct".    );.    p
191cd 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 7b 0a   = 0;.  }else {.
191ce 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20      /* Allocate 
191cf 73 70 61 63 65 20 66 6f 72 20 61 20 6e 65 77 20  space for a new 
191d0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f  sqlite3_backup o
191d1 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 70 20 3d  bject */.    p =
191d2 20 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70   (sqlite3_backup
191d3 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   *)sqlite3_mallo
191d4 63 28 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33  c(sizeof(sqlite3
191d5 5f 62 61 63 6b 75 70 29 29 3b 0a 20 20 20 20 69  _backup));.    i
191d6 66 28 20 21 70 20 29 7b 0a 20 20 20 20 20 20 73  f( !p ){.      s
191d7 71 6c 69 74 65 33 45 72 72 6f 72 28 70 44 65 73  qlite3Error(pDes
191d8 74 44 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  tDb, SQLITE_NOME
191d9 4d 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  M, 0);.    }.  }
191da 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61 6c  ..  /* If the al
191db 6c 6f 63 61 74 69 6f 6e 20 73 75 63 63 65 65 64  location succeed
191dc 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 74 68 65  ed, populate the
191dd 20 6e 65 77 20 6f 62 6a 65 63 74 2e 20 2a 2f 0a   new object. */.
191de 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6d    if( p ){.    m
191df 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65  emset(p, 0, size
191e0 6f 66 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  of(sqlite3_backu
191e1 70 29 29 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63  p));.    p->pSrc
191e2 20 3d 20 66 69 6e 64 42 74 72 65 65 28 70 44 65   = findBtree(pDe
191e3 73 74 44 62 2c 20 70 53 72 63 44 62 2c 20 7a 53  stDb, pSrcDb, zS
191e4 72 63 44 62 29 3b 0a 20 20 20 20 70 2d 3e 70 44  rcDb);.    p->pD
191e5 65 73 74 20 3d 20 66 69 6e 64 42 74 72 65 65 28  est = findBtree(
191e6 70 44 65 73 74 44 62 2c 20 70 44 65 73 74 44 62  pDestDb, pDestDb
191e7 2c 20 7a 44 65 73 74 44 62 29 3b 0a 20 20 20 20  , zDestDb);.    
191e8 70 2d 3e 70 44 65 73 74 44 62 20 3d 20 70 44 65  p->pDestDb = pDe
191e9 73 74 44 62 3b 0a 20 20 20 20 70 2d 3e 70 53 72  stDb;.    p->pSr
191ea 63 44 62 20 3d 20 70 53 72 63 44 62 3b 0a 20 20  cDb = pSrcDb;.  
191eb 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20 31 3b 0a    p->iNext = 1;.
191ec 20 20 20 20 70 2d 3e 69 73 41 74 74 61 63 68 65      p->isAttache
191ed 64 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66 28 20  d = 0;..    if( 
191ee 30 3d 3d 70 2d 3e 70 53 72 63 20 7c 7c 20 30 3d  0==p->pSrc || 0=
191ef 3d 70 2d 3e 70 44 65 73 74 20 29 7b 0a 20 20 20  =p->pDest ){.   
191f0 20 20 20 2f 2a 20 4f 6e 65 20 28 6f 72 20 62 6f     /* One (or bo
191f1 74 68 29 20 6f 66 20 74 68 65 20 6e 61 6d 65 64  th) of the named
191f2 20 64 61 74 61 62 61 73 65 73 20 64 69 64 20 6e   databases did n
191f3 6f 74 20 65 78 69 73 74 2e 20 41 6e 20 65 72 72  ot exist. An err
191f4 6f 72 20 68 61 73 0a 20 20 20 20 20 20 2a 2a 20  or has.      ** 
191f5 61 6c 72 65 61 64 79 20 62 65 65 6e 20 77 72 69  already been wri
191f6 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 70 44  tten into the pD
191f7 65 73 74 44 62 20 68 61 6e 64 6c 65 2e 20 41 6c  estDb handle. Al
191f8 6c 20 74 68 61 74 20 69 73 20 6c 65 66 74 0a 20  l that is left. 
191f9 20 20 20 20 20 2a 2a 20 74 6f 20 64 6f 20 68 65       ** to do he
191fa 72 65 20 69 73 20 66 72 65 65 20 74 68 65 20 73  re is free the s
191fb 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 73 74  qlite3_backup st
191fc 72 75 63 74 75 72 65 2e 0a 20 20 20 20 20 20 2a  ructure..      *
191fd 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  /.      sqlite3_
191fe 66 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 70  free(p);.      p
191ff 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
19200 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70    if( p ){.    p
19201 2d 3e 70 53 72 63 2d 3e 6e 42 61 63 6b 75 70 2b  ->pSrc->nBackup+
19202 2b 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  +;.  }..  sqlite
19203 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 44  3_mutex_leave(pD
19204 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  estDb->mutex);. 
19205 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
19206 65 61 76 65 28 70 53 72 63 44 62 2d 3e 6d 75 74  eave(pSrcDb->mut
19207 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b  ex);.  return p;
19208 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65  .}../*.** Argume
19209 6e 74 20 72 63 20 69 73 20 61 6e 20 53 51 4c 69  nt rc is an SQLi
1920a 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 52  te error code. R
1920b 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68  eturn true if th
1920c 69 73 20 65 72 72 6f 72 20 69 73 20 0a 2a 2a 20  is error is .** 
1920d 63 6f 6e 73 69 64 65 72 65 64 20 66 61 74 61 6c  considered fatal
1920e 20 69 66 20 65 6e 63 6f 75 6e 74 65 72 65 64 20   if encountered 
1920f 64 75 72 69 6e 67 20 61 20 62 61 63 6b 75 70 20  during a backup 
19210 6f 70 65 72 61 74 69 6f 6e 2e 20 41 6c 6c 20 65  operation. All e
19211 72 72 6f 72 73 0a 2a 2a 20 61 72 65 20 63 6f 6e  rrors.** are con
19212 73 69 64 65 72 65 64 20 66 61 74 61 6c 20 65 78  sidered fatal ex
19213 63 65 70 74 20 66 6f 72 20 53 51 4c 49 54 45 5f  cept for SQLITE_
19214 42 55 53 59 20 61 6e 64 20 53 51 4c 49 54 45 5f  BUSY and SQLITE_
19215 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 73 74 61 74 69  LOCKED..*/.stati
19216 63 20 69 6e 74 20 69 73 46 61 74 61 6c 45 72 72  c int isFatalErr
19217 6f 72 28 69 6e 74 20 72 63 29 7b 0a 20 20 72 65  or(int rc){.  re
19218 74 75 72 6e 20 28 72 63 21 3d 53 51 4c 49 54 45  turn (rc!=SQLITE
19219 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54  _OK && rc!=SQLIT
1921a 45 5f 42 55 53 59 20 26 26 20 41 4c 57 41 59 53  E_BUSY && ALWAYS
1921b 28 72 63 21 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b  (rc!=SQLITE_LOCK
1921c 45 44 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  ED));.}../*.** P
1921d 61 72 61 6d 65 74 65 72 20 7a 53 72 63 44 61 74  arameter zSrcDat
1921e 61 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 62 75  a points to a bu
1921f 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ffer containing 
19220 74 68 65 20 64 61 74 61 20 66 6f 72 20 0a 2a 2a  the data for .**
19221 20 70 61 67 65 20 69 53 72 63 50 67 20 66 72 6f   page iSrcPg fro
19222 6d 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74  m the source dat
19223 61 62 61 73 65 2e 20 43 6f 70 79 20 74 68 69 73  abase. Copy this
19224 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 0a   data into the .
19225 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64  ** destination d
19226 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74  atabase..*/.stat
19227 69 63 20 69 6e 74 20 62 61 63 6b 75 70 4f 6e 65  ic int backupOne
19228 50 61 67 65 28 73 71 6c 69 74 65 33 5f 62 61 63  Page(sqlite3_bac
19229 6b 75 70 20 2a 70 2c 20 50 67 6e 6f 20 69 53 72  kup *p, Pgno iSr
1922a 63 50 67 2c 20 63 6f 6e 73 74 20 75 38 20 2a 7a  cPg, const u8 *z
1922b 53 72 63 44 61 74 61 29 7b 0a 20 20 50 61 67 65  SrcData){.  Page
1922c 72 20 2a 20 63 6f 6e 73 74 20 70 44 65 73 74 50  r * const pDestP
1922d 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74  ager = sqlite3Bt
1922e 72 65 65 50 61 67 65 72 28 70 2d 3e 70 44 65 73  reePager(p->pDes
1922f 74 29 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20  t);.  const int 
19230 6e 53 72 63 50 67 73 7a 20 3d 20 73 71 6c 69 74  nSrcPgsz = sqlit
19231 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69  e3BtreeGetPageSi
19232 7a 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 69  ze(p->pSrc);.  i
19233 6e 74 20 6e 44 65 73 74 50 67 73 7a 20 3d 20 73  nt nDestPgsz = s
19234 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61  qlite3BtreeGetPa
19235 67 65 53 69 7a 65 28 70 2d 3e 70 44 65 73 74 29  geSize(p->pDest)
19236 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 43  ;.  const int nC
19237 6f 70 79 20 3d 20 4d 49 4e 28 6e 53 72 63 50 67  opy = MIN(nSrcPg
19238 73 7a 2c 20 6e 44 65 73 74 50 67 73 7a 29 3b 0a  sz, nDestPgsz);.
19239 20 20 63 6f 6e 73 74 20 69 36 34 20 69 45 6e 64    const i64 iEnd
1923a 20 3d 20 28 69 36 34 29 69 53 72 63 50 67 2a 28   = (i64)iSrcPg*(
1923b 69 36 34 29 6e 53 72 63 50 67 73 7a 3b 0a 0a 20  i64)nSrcPgsz;.. 
1923c 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1923d 5f 4f 4b 3b 0a 20 20 69 36 34 20 69 4f 66 66 3b  _OK;.  i64 iOff;
1923e 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 62  ..  assert( p->b
1923f 44 65 73 74 4c 6f 63 6b 65 64 20 29 3b 0a 20 20  DestLocked );.  
19240 61 73 73 65 72 74 28 20 21 69 73 46 61 74 61 6c  assert( !isFatal
19241 45 72 72 6f 72 28 70 2d 3e 72 63 29 20 29 3b 0a  Error(p->rc) );.
19242 20 20 61 73 73 65 72 74 28 20 69 53 72 63 50 67    assert( iSrcPg
19243 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  !=PENDING_BYTE_P
19244 41 47 45 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74  AGE(p->pSrc->pBt
19245 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a  ) );.  assert( z
19246 53 72 63 44 61 74 61 20 29 3b 0a 0a 20 20 2f 2a  SrcData );..  /*
19247 20 43 61 74 63 68 20 74 68 65 20 63 61 73 65 20   Catch the case 
19248 77 68 65 72 65 20 74 68 65 20 64 65 73 74 69 6e  where the destin
19249 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 2d 6d  ation is an in-m
1924a 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 61  emory database a
1924b 6e 64 20 74 68 65 0a 20 20 2a 2a 20 70 61 67 65  nd the.  ** page
1924c 20 73 69 7a 65 73 20 6f 66 20 74 68 65 20 73 6f   sizes of the so
1924d 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61  urce and destina
1924e 74 69 6f 6e 20 64 69 66 66 65 72 2e 20 0a 20 20  tion differ. .  
1924f 2a 2f 0a 20 20 69 66 28 20 6e 53 72 63 50 67 73  */.  if( nSrcPgs
19250 7a 21 3d 6e 44 65 73 74 50 67 73 7a 20 26 26 20  z!=nDestPgsz && 
19251 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 4d 65  sqlite3PagerIsMe
19252 6d 64 62 28 73 71 6c 69 74 65 33 42 74 72 65 65  mdb(sqlite3Btree
19253 50 61 67 65 72 28 70 2d 3e 70 44 65 73 74 29 29  Pager(p->pDest))
19254 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
19255 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20  ITE_READONLY;.  
19256 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f  }..  /* This loo
19257 70 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20  p runs once for 
19258 65 61 63 68 20 64 65 73 74 69 6e 61 74 69 6f 6e  each destination
19259 20 70 61 67 65 20 73 70 61 6e 6e 65 64 20 62 79   page spanned by
1925a 20 74 68 65 20 73 6f 75 72 63 65 20 0a 20 20 2a   the source .  *
1925b 2a 20 70 61 67 65 2e 20 46 6f 72 20 65 61 63 68  * page. For each
1925c 20 69 74 65 72 61 74 69 6f 6e 2c 20 76 61 72 69   iteration, vari
1925d 61 62 6c 65 20 69 4f 66 66 20 69 73 20 73 65 74  able iOff is set
1925e 20 74 6f 20 74 68 65 20 62 79 74 65 20 6f 66 66   to the byte off
1925f 73 65 74 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20  set.  ** of the 
19260 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65  destination page
19261 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 4f 66  ..  */.  for(iOf
19262 66 3d 69 45 6e 64 2d 28 69 36 34 29 6e 53 72 63  f=iEnd-(i64)nSrc
19263 50 67 73 7a 3b 20 72 63 3d 3d 53 51 4c 49 54 45  Pgsz; rc==SQLITE
19264 5f 4f 4b 20 26 26 20 69 4f 66 66 3c 69 45 6e 64  _OK && iOff<iEnd
19265 3b 20 69 4f 66 66 2b 3d 6e 44 65 73 74 50 67 73  ; iOff+=nDestPgs
19266 7a 29 7b 0a 20 20 20 20 44 62 50 61 67 65 20 2a  z){.    DbPage *
19267 70 44 65 73 74 50 67 20 3d 20 30 3b 0a 20 20 20  pDestPg = 0;.   
19268 20 50 67 6e 6f 20 69 44 65 73 74 20 3d 20 28 50   Pgno iDest = (P
19269 67 6e 6f 29 28 69 4f 66 66 2f 6e 44 65 73 74 50  gno)(iOff/nDestP
1926a 67 73 7a 29 2b 31 3b 0a 20 20 20 20 69 66 28 20  gsz)+1;.    if( 
1926b 69 44 65 73 74 3d 3d 50 45 4e 44 49 4e 47 5f 42  iDest==PENDING_B
1926c 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 44 65 73  YTE_PAGE(p->pDes
1926d 74 2d 3e 70 42 74 29 20 29 20 63 6f 6e 74 69 6e  t->pBt) ) contin
1926e 75 65 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49  ue;.    if( SQLI
1926f 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c  TE_OK==(rc = sql
19270 69 74 65 33 50 61 67 65 72 47 65 74 28 70 44 65  ite3PagerGet(pDe
19271 73 74 50 61 67 65 72 2c 20 69 44 65 73 74 2c 20  stPager, iDest, 
19272 26 70 44 65 73 74 50 67 29 29 0a 20 20 20 20 20  &pDestPg)).     
19273 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72  && SQLITE_OK==(r
19274 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
19275 57 72 69 74 65 28 70 44 65 73 74 50 67 29 29 0a  Write(pDestPg)).
19276 20 20 20 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e      ){.      con
19277 73 74 20 75 38 20 2a 7a 49 6e 20 3d 20 26 7a 53  st u8 *zIn = &zS
19278 72 63 44 61 74 61 5b 69 4f 66 66 25 6e 53 72 63  rcData[iOff%nSrc
19279 50 67 73 7a 5d 3b 0a 20 20 20 20 20 20 75 38 20  Pgsz];.      u8 
1927a 2a 7a 44 65 73 74 44 61 74 61 20 3d 20 73 71 6c  *zDestData = sql
1927b 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61  ite3PagerGetData
1927c 28 70 44 65 73 74 50 67 29 3b 0a 20 20 20 20 20  (pDestPg);.     
1927d 20 75 38 20 2a 7a 4f 75 74 20 3d 20 26 7a 44 65   u8 *zOut = &zDe
1927e 73 74 44 61 74 61 5b 69 4f 66 66 25 6e 44 65 73  stData[iOff%nDes
1927f 74 50 67 73 7a 5d 3b 0a 0a 20 20 20 20 20 20 2f  tPgsz];..      /
19280 2a 20 43 6f 70 79 20 74 68 65 20 64 61 74 61 20  * Copy the data 
19281 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20  from the source 
19282 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20 64 65  page into the de
19283 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 2e 0a  stination page..
19284 20 20 20 20 20 20 2a 2a 20 54 68 65 6e 20 63 6c        ** Then cl
19285 65 61 72 20 74 68 65 20 42 74 72 65 65 20 6c 61  ear the Btree la
19286 79 65 72 20 4d 65 6d 50 61 67 65 2e 69 73 49 6e  yer MemPage.isIn
19287 69 74 20 66 6c 61 67 2e 20 42 6f 74 68 20 74 68  it flag. Both th
19288 69 73 20 6d 6f 64 75 6c 65 0a 20 20 20 20 20 20  is module.      
19289 2a 2a 20 61 6e 64 20 74 68 65 20 70 61 67 65 72  ** and the pager
1928a 20 63 6f 64 65 20 75 73 65 20 74 68 69 73 20 74   code use this t
1928b 72 69 63 6b 20 28 63 6c 65 61 72 69 6e 67 20 74  rick (clearing t
1928c 68 65 20 66 69 72 73 74 20 62 79 74 65 0a 20 20  he first byte.  
1928d 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 61      ** of the pa
1928e 67 65 20 27 65 78 74 72 61 27 20 73 70 61 63 65  ge 'extra' space
1928f 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 74   to invalidate t
19290 68 65 20 42 74 72 65 65 20 6c 61 79 65 72 73 0a  he Btree layers.
19291 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65 64 20        ** cached 
19292 70 61 72 73 65 20 6f 66 20 74 68 65 20 70 61 67  parse of the pag
19293 65 29 2e 20 4d 65 6d 50 61 67 65 2e 69 73 49 6e  e). MemPage.isIn
19294 69 74 20 69 73 20 6d 61 72 6b 65 64 20 0a 20 20  it is marked .  
19295 20 20 20 20 2a 2a 20 22 4d 55 53 54 20 42 45 20      ** "MUST BE 
19296 46 49 52 53 54 22 20 66 6f 72 20 74 68 69 73 20  FIRST" for this 
19297 70 75 72 70 6f 73 65 2e 0a 20 20 20 20 20 20 2a  purpose..      *
19298 2f 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a  /.      memcpy(z
19299 4f 75 74 2c 20 7a 49 6e 2c 20 6e 43 6f 70 79 29  Out, zIn, nCopy)
1929a 3b 0a 20 20 20 20 20 20 28 28 75 38 20 2a 29 73  ;.      ((u8 *)s
1929b 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78  qlite3PagerGetEx
1929c 74 72 61 28 70 44 65 73 74 50 67 29 29 5b 30 5d  tra(pDestPg))[0]
1929d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
1929e 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
1929f 66 28 70 44 65 73 74 50 67 29 3b 0a 20 20 7d 0a  f(pDestPg);.  }.
192a0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
192a1 0a 2f 2a 0a 2a 2a 20 49 66 20 70 46 69 6c 65 20  ./*.** If pFile 
192a2 69 73 20 63 75 72 72 65 6e 74 6c 79 20 6c 61 72  is currently lar
192a3 67 65 72 20 74 68 61 6e 20 69 53 69 7a 65 20 62  ger than iSize b
192a4 79 74 65 73 2c 20 74 68 65 6e 20 74 72 75 6e 63  ytes, then trunc
192a5 61 74 65 20 69 74 20 74 6f 0a 2a 2a 20 65 78 61  ate it to.** exa
192a6 63 74 6c 79 20 69 53 69 7a 65 20 62 79 74 65 73  ctly iSize bytes
192a7 2e 20 49 66 20 70 46 69 6c 65 20 69 73 20 6e 6f  . If pFile is no
192a8 74 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 53  t larger than iS
192a9 69 7a 65 20 62 79 74 65 73 2c 20 74 68 65 6e 0a  ize bytes, then.
192aa 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** this function
192ab 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a   is a no-op..**.
192ac 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
192ad 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e  _OK if everythin
192ae 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c  g is successful,
192af 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65 72   or an SQLite er
192b0 72 6f 72 20 0a 2a 2a 20 63 6f 64 65 20 69 66 20  ror .** code if 
192b1 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e  an error occurs.
192b2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
192b3 61 63 6b 75 70 54 72 75 6e 63 61 74 65 46 69 6c  ackupTruncateFil
192b4 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
192b5 70 46 69 6c 65 2c 20 69 36 34 20 69 53 69 7a 65  pFile, i64 iSize
192b6 29 7b 0a 20 20 69 36 34 20 69 43 75 72 72 65 6e  ){.  i64 iCurren
192b7 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71  t;.  int rc = sq
192b8 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
192b9 70 46 69 6c 65 2c 20 26 69 43 75 72 72 65 6e 74  pFile, &iCurrent
192ba 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
192bb 49 54 45 5f 4f 4b 20 26 26 20 69 43 75 72 72 65  ITE_OK && iCurre
192bc 6e 74 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20 20  nt>iSize ){.    
192bd 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72  rc = sqlite3OsTr
192be 75 6e 63 61 74 65 28 70 46 69 6c 65 2c 20 69 53  uncate(pFile, iS
192bf 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ize);.  }.  retu
192c0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
192c1 52 65 67 69 73 74 65 72 20 74 68 69 73 20 62 61  Register this ba
192c2 63 6b 75 70 20 6f 62 6a 65 63 74 20 77 69 74 68  ckup object with
192c3 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20   the associated 
192c4 73 6f 75 72 63 65 20 70 61 67 65 72 20 66 6f 72  source pager for
192c5 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 73 20 77 68  .** callbacks wh
192c6 65 6e 20 70 61 67 65 73 20 61 72 65 20 63 68 61  en pages are cha
192c7 6e 67 65 64 20 6f 72 20 74 68 65 20 63 61 63 68  nged or the cach
192c8 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a  e invalidated..*
192c9 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 74  /.static void at
192ca 74 61 63 68 42 61 63 6b 75 70 4f 62 6a 65 63 74  tachBackupObject
192cb 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
192cc 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62  *p){.  sqlite3_b
192cd 61 63 6b 75 70 20 2a 2a 70 70 3b 0a 20 20 61 73  ackup **pp;.  as
192ce 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
192cf 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 2d 3e  eeHoldsMutex(p->
192d0 70 53 72 63 29 20 29 3b 0a 20 20 70 70 20 3d 20  pSrc) );.  pp = 
192d1 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 63 6b  sqlite3PagerBack
192d2 75 70 50 74 72 28 73 71 6c 69 74 65 33 42 74 72  upPtr(sqlite3Btr
192d3 65 65 50 61 67 65 72 28 70 2d 3e 70 53 72 63 29  eePager(p->pSrc)
192d4 29 3b 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20  );.  p->pNext = 
192d5 2a 70 70 3b 0a 20 20 2a 70 70 20 3d 20 70 3b 0a  *pp;.  *pp = p;.
192d6 20 20 70 2d 3e 69 73 41 74 74 61 63 68 65 64 20    p->isAttached 
192d7 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  = 1;.}../*.** Co
192d8 70 79 20 6e 50 61 67 65 20 70 61 67 65 73 20 66  py nPage pages f
192d9 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 62  rom the source b
192da 2d 74 72 65 65 20 74 6f 20 74 68 65 20 64 65 73  -tree to the des
192db 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  tination..*/.SQL
192dc 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
192dd 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28  te3_backup_step(
192de 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
192df 70 2c 20 69 6e 74 20 6e 50 61 67 65 29 7b 0a 20  p, int nPage){. 
192e0 20 69 6e 74 20 72 63 3b 0a 0a 20 20 73 71 6c 69   int rc;..  sqli
192e1 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
192e2 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78  p->pSrcDb->mutex
192e3 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
192e4 65 45 6e 74 65 72 28 70 2d 3e 70 53 72 63 29 3b  eEnter(p->pSrc);
192e5 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44  .  if( p->pDestD
192e6 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  b ){.    sqlite3
192e7 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e  _mutex_enter(p->
192e8 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b  pDestDb->mutex);
192e9 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 70 2d 3e  .  }..  rc = p->
192ea 72 63 3b 0a 20 20 69 66 28 20 21 69 73 46 61 74  rc;.  if( !isFat
192eb 61 6c 45 72 72 6f 72 28 72 63 29 20 29 7b 0a 20  alError(rc) ){. 
192ec 20 20 20 50 61 67 65 72 20 2a 20 63 6f 6e 73 74     Pager * const
192ed 20 70 53 72 63 50 61 67 65 72 20 3d 20 73 71 6c   pSrcPager = sql
192ee 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70  ite3BtreePager(p
192ef 2d 3e 70 53 72 63 29 3b 20 20 20 20 20 2f 2a 20  ->pSrc);     /* 
192f0 53 6f 75 72 63 65 20 70 61 67 65 72 20 2a 2f 0a  Source pager */.
192f1 20 20 20 20 50 61 67 65 72 20 2a 20 63 6f 6e 73      Pager * cons
192f2 74 20 70 44 65 73 74 50 61 67 65 72 20 3d 20 73  t pDestPager = s
192f3 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
192f4 28 70 2d 3e 70 44 65 73 74 29 3b 20 20 20 2f 2a  (p->pDest);   /*
192f5 20 44 65 73 74 20 70 61 67 65 72 20 2a 2f 0a 20   Dest pager */. 
192f6 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20     int ii;      
192f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
192f8 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
192f9 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20  r variable */.  
192fa 20 20 69 6e 74 20 6e 53 72 63 50 61 67 65 20 3d    int nSrcPage =
192fb 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20 20 20   -1;            
192fc 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
192fd 73 6f 75 72 63 65 20 64 62 20 69 6e 20 70 61 67  source db in pag
192fe 65 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 62 43  es */.    int bC
192ff 6c 6f 73 65 54 72 61 6e 73 20 3d 20 30 3b 20 20  loseTrans = 0;  
19300 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19301 54 72 75 65 20 69 66 20 73 72 63 20 64 62 20 72  True if src db r
19302 65 71 75 69 72 65 73 20 75 6e 6c 6f 63 6b 69 6e  equires unlockin
19303 67 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20  g */..    /* If 
19304 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 72  the source pager
19305 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e   is currently in
19306 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63   a write-transac
19307 74 69 6f 6e 2c 20 72 65 74 75 72 6e 0a 20 20 20  tion, return.   
19308 20 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20   ** SQLITE_BUSY 
19309 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 20  immediately..   
1930a 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70   */.    if( p->p
1930b 44 65 73 74 44 62 20 26 26 20 70 2d 3e 70 53 72  DestDb && p->pSr
1930c 63 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  c->pBt->inTransa
1930d 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49  ction==TRANS_WRI
1930e 54 45 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  TE ){.      rc =
1930f 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
19310 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
19311 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
19312 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63     }..    /* Loc
19313 6b 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  k the destinatio
19314 6e 20 64 61 74 61 62 61 73 65 2c 20 69 66 20 69  n database, if i
19315 74 20 69 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 20  t is not locked 
19316 61 6c 72 65 61 64 79 2e 20 2a 2f 0a 20 20 20 20  already. */.    
19317 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72  if( SQLITE_OK==r
19318 63 20 26 26 20 70 2d 3e 62 44 65 73 74 4c 6f 63  c && p->bDestLoc
19319 6b 65 64 3d 3d 30 0a 20 20 20 20 20 26 26 20 53  ked==0.     && S
1931a 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
1931b 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69  sqlite3BtreeBegi
1931c 6e 54 72 61 6e 73 28 70 2d 3e 70 44 65 73 74 2c  nTrans(p->pDest,
1931d 20 32 29 29 20 0a 20 20 20 20 29 7b 0a 20 20 20   2)) .    ){.   
1931e 20 20 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b 65     p->bDestLocke
1931f 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 73 71 6c  d = 1;.      sql
19320 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
19321 28 70 2d 3e 70 44 65 73 74 2c 20 42 54 52 45 45  (p->pDest, BTREE
19322 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c  _SCHEMA_VERSION,
19323 20 26 70 2d 3e 69 44 65 73 74 53 63 68 65 6d 61   &p->iDestSchema
19324 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
19325 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
19326 6f 70 65 6e 20 72 65 61 64 2d 74 72 61 6e 73 61  open read-transa
19327 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73 6f 75  ction on the sou
19328 72 63 65 20 64 61 74 61 62 61 73 65 2c 20 6f 70  rce database, op
19329 65 6e 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 6e 6f  en.    ** one no
1932a 77 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74  w. If a transact
1932b 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 20 68 65  ion is opened he
1932c 72 65 2c 20 74 68 65 6e 20 69 74 20 77 69 6c 6c  re, then it will
1932d 20 62 65 20 63 6c 6f 73 65 64 0a 20 20 20 20 2a   be closed.    *
1932e 2a 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 75  * before this fu
1932f 6e 63 74 69 6f 6e 20 65 78 69 74 73 2e 0a 20 20  nction exits..  
19330 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d    */.    if( rc=
19331 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d  =SQLITE_OK && 0=
19332 3d 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49  =sqlite3BtreeIsI
19333 6e 52 65 61 64 54 72 61 6e 73 28 70 2d 3e 70 53  nReadTrans(p->pS
19334 72 63 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20  rc) ){.      rc 
19335 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65  = sqlite3BtreeBe
19336 67 69 6e 54 72 61 6e 73 28 70 2d 3e 70 53 72 63  ginTrans(p->pSrc
19337 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 43 6c 6f  , 0);.      bClo
19338 73 65 54 72 61 6e 73 20 3d 20 31 3b 0a 20 20 20  seTrans = 1;.   
19339 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77   }.  .    /* Now
1933a 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 61   that there is a
1933b 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68   read-lock on th
1933c 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  e source databas
1933d 65 2c 20 71 75 65 72 79 20 74 68 65 0a 20 20 20  e, query the.   
1933e 20 2a 2a 20 73 6f 75 72 63 65 20 70 61 67 65 72   ** source pager
1933f 20 66 6f 72 20 74 68 65 20 6e 75 6d 62 65 72 20   for the number 
19340 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
19341 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f  database..    */
19342 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
19343 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
19344 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
19345 72 50 61 67 65 63 6f 75 6e 74 28 70 53 72 63 50  rPagecount(pSrcP
19346 61 67 65 72 2c 20 26 6e 53 72 63 50 61 67 65 29  ager, &nSrcPage)
19347 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  ;.    }.    for(
19348 69 69 3d 30 3b 20 28 6e 50 61 67 65 3c 30 20 7c  ii=0; (nPage<0 |
19349 7c 20 69 69 3c 6e 50 61 67 65 29 20 26 26 20 70  | ii<nPage) && p
1934a 2d 3e 69 4e 65 78 74 3c 3d 28 50 67 6e 6f 29 6e  ->iNext<=(Pgno)n
1934b 53 72 63 50 61 67 65 20 26 26 20 21 72 63 3b 20  SrcPage && !rc; 
1934c 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 6f 6e  ii++){.      con
1934d 73 74 20 50 67 6e 6f 20 69 53 72 63 50 67 20 3d  st Pgno iSrcPg =
1934e 20 70 2d 3e 69 4e 65 78 74 3b 20 20 20 20 20 20   p->iNext;      
1934f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f             /* So
19350 75 72 63 65 20 70 61 67 65 20 6e 75 6d 62 65 72  urce page number
19351 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 53   */.      if( iS
19352 72 63 50 67 21 3d 50 45 4e 44 49 4e 47 5f 42 59  rcPg!=PENDING_BY
19353 54 45 5f 50 41 47 45 28 70 2d 3e 70 53 72 63 2d  TE_PAGE(p->pSrc-
19354 3e 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20  >pBt) ){.       
19355 20 44 62 50 61 67 65 20 2a 70 53 72 63 50 67 3b   DbPage *pSrcPg;
19356 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19357 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19358 53 6f 75 72 63 65 20 70 61 67 65 20 6f 62 6a 65  Source page obje
19359 63 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63  ct */.        rc
1935a 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47   = sqlite3PagerG
1935b 65 74 28 70 53 72 63 50 61 67 65 72 2c 20 69 53  et(pSrcPager, iS
1935c 72 63 50 67 2c 20 26 70 53 72 63 50 67 29 3b 0a  rcPg, &pSrcPg);.
1935d 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
1935e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1935f 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 63 6b         rc = back
19360 75 70 4f 6e 65 50 61 67 65 28 70 2c 20 69 53 72  upOnePage(p, iSr
19361 63 50 67 2c 20 73 71 6c 69 74 65 33 50 61 67 65  cPg, sqlite3Page
19362 72 47 65 74 44 61 74 61 28 70 53 72 63 50 67 29  rGetData(pSrcPg)
19363 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
19364 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
19365 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20  SrcPg);.        
19366 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
19367 70 2d 3e 69 4e 65 78 74 2b 2b 3b 0a 20 20 20 20  p->iNext++;.    
19368 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  }.    if( rc==SQ
19369 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1936a 20 70 2d 3e 6e 50 61 67 65 63 6f 75 6e 74 20 3d   p->nPagecount =
1936b 20 6e 53 72 63 50 61 67 65 3b 0a 20 20 20 20 20   nSrcPage;.     
1936c 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 6e 67 20 3d   p->nRemaining =
1936d 20 6e 53 72 63 50 61 67 65 2b 31 2d 70 2d 3e 69   nSrcPage+1-p->i
1936e 4e 65 78 74 3b 0a 20 20 20 20 20 20 69 66 28 20  Next;.      if( 
1936f 70 2d 3e 69 4e 65 78 74 3e 28 50 67 6e 6f 29 6e  p->iNext>(Pgno)n
19370 53 72 63 50 61 67 65 20 29 7b 0a 20 20 20 20 20  SrcPage ){.     
19371 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44     rc = SQLITE_D
19372 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ONE;.      }else
19373 20 69 66 28 20 21 70 2d 3e 69 73 41 74 74 61 63   if( !p->isAttac
19374 68 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61  hed ){.        a
19375 74 74 61 63 68 42 61 63 6b 75 70 4f 62 6a 65 63  ttachBackupObjec
19376 74 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  t(p);.      }.  
19377 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 55 70    }.  .    /* Up
19378 64 61 74 65 20 74 68 65 20 73 63 68 65 6d 61 20  date the schema 
19379 76 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 69 6e  version field in
1937a 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
1937b 20 64 61 74 61 62 61 73 65 2e 20 54 68 69 73 0a   database. This.
1937c 20 20 20 20 2a 2a 20 69 73 20 74 6f 20 6d 61 6b      ** is to mak
1937d 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20  e sure that the 
1937e 73 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20 72  schema-version r
1937f 65 61 6c 6c 79 20 64 6f 65 73 20 63 68 61 6e 67  eally does chang
19380 65 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20  e in.    ** the 
19381 63 61 73 65 20 77 68 65 72 65 20 74 68 65 20 73  case where the s
19382 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e  ource and destin
19383 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 73 20  ation databases 
19384 68 61 76 65 20 74 68 65 0a 20 20 20 20 2a 2a 20  have the.    ** 
19385 73 61 6d 65 20 73 63 68 65 6d 61 20 76 65 72 73  same schema vers
19386 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ion..    */.    
19387 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
19388 4f 4e 45 20 0a 20 20 20 20 20 26 26 20 28 72 63  ONE .     && (rc
19389 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 55   = sqlite3BtreeU
1938a 70 64 61 74 65 4d 65 74 61 28 70 2d 3e 70 44 65  pdateMeta(p->pDe
1938b 73 74 2c 31 2c 70 2d 3e 69 44 65 73 74 53 63 68  st,1,p->iDestSch
1938c 65 6d 61 2b 31 29 29 3d 3d 53 51 4c 49 54 45 5f  ema+1))==SQLITE_
1938d 4f 4b 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  OK.    ){.      
1938e 63 6f 6e 73 74 20 69 6e 74 20 6e 53 72 63 50 61  const int nSrcPa
1938f 67 65 73 69 7a 65 20 3d 20 73 71 6c 69 74 65 33  gesize = sqlite3
19390 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65  BtreeGetPageSize
19391 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 20  (p->pSrc);.     
19392 20 63 6f 6e 73 74 20 69 6e 74 20 6e 44 65 73 74   const int nDest
19393 50 61 67 65 73 69 7a 65 20 3d 20 73 71 6c 69 74  Pagesize = sqlit
19394 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69  e3BtreeGetPageSi
19395 7a 65 28 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20  ze(p->pDest);.  
19396 20 20 20 20 69 6e 74 20 6e 44 65 73 74 54 72 75      int nDestTru
19397 6e 63 61 74 65 3b 0a 20 20 0a 20 20 20 20 20 20  ncate;.  .      
19398 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29  if( p->pDestDb )
19399 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1939a 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
1939b 68 65 6d 61 28 70 2d 3e 70 44 65 73 74 44 62 2c  hema(p->pDestDb,
1939c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20   0);.      }..  
1939d 20 20 20 20 2f 2a 20 53 65 74 20 6e 44 65 73 74      /* Set nDest
1939e 54 72 75 6e 63 61 74 65 20 74 6f 20 74 68 65 20  Truncate to the 
1939f 66 69 6e 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  final number of 
193a0 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 65 73  pages in the des
193a1 74 69 6e 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a  tination.      *
193a2 2a 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20  * database. The 
193a3 63 6f 6d 70 6c 69 63 61 74 69 6f 6e 20 68 65 72  complication her
193a4 65 20 69 73 20 74 68 61 74 20 74 68 65 20 64 65  e is that the de
193a5 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 0a 20  stination page. 
193a6 20 20 20 20 20 2a 2a 20 73 69 7a 65 20 6d 61 79       ** size may
193a7 20 62 65 20 64 69 66 66 65 72 65 6e 74 20 74 6f   be different to
193a8 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65   the source page
193a9 20 73 69 7a 65 2e 20 0a 20 20 20 20 20 20 2a 2a   size. .      **
193aa 0a 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 65  .      ** If the
193ab 20 73 6f 75 72 63 65 20 70 61 67 65 20 73 69 7a   source page siz
193ac 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61  e is smaller tha
193ad 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  n the destinatio
193ae 6e 20 70 61 67 65 20 73 69 7a 65 2c 20 0a 20 20  n page size, .  
193af 20 20 20 20 2a 2a 20 72 6f 75 6e 64 20 75 70 2e      ** round up.
193b0 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
193b1 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  e call to sqlite
193b2 33 4f 73 54 72 75 6e 63 61 74 65 28 29 20 62 65  3OsTruncate() be
193b3 6c 6f 77 20 77 69 6c 6c 0a 20 20 20 20 20 20 2a  low will.      *
193b4 2a 20 66 69 78 20 74 68 65 20 73 69 7a 65 20 6f  * fix the size o
193b5 66 20 74 68 65 20 66 69 6c 65 2e 20 48 6f 77 65  f the file. Howe
193b6 76 65 72 20 69 74 20 69 73 20 69 6d 70 6f 72 74  ver it is import
193b7 61 6e 74 20 74 6f 20 63 61 6c 6c 0a 20 20 20 20  ant to call.    
193b8 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65    ** sqlite3Page
193b9 72 54 72 75 6e 63 61 74 65 49 6d 61 67 65 28 29  rTruncateImage()
193ba 20 68 65 72 65 20 73 6f 20 74 68 61 74 20 61 6e   here so that an
193bb 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 0a  y pages in the .
193bc 20 20 20 20 20 20 2a 2a 20 64 65 73 74 69 6e 61        ** destina
193bd 74 69 6f 6e 20 66 69 6c 65 20 74 68 61 74 20 6c  tion file that l
193be 69 65 20 62 65 79 6f 6e 64 20 74 68 65 20 6e 44  ie beyond the nD
193bf 65 73 74 54 72 75 6e 63 61 74 65 20 70 61 67 65  estTruncate page
193c0 20 6d 61 72 6b 20 61 72 65 0a 20 20 20 20 20 20   mark are.      
193c1 2a 2a 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 62 79  ** journalled by
193c2 20 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73   PagerCommitPhas
193c3 65 4f 6e 65 28 29 20 62 65 66 6f 72 65 20 74 68  eOne() before th
193c4 65 79 20 61 72 65 20 64 65 73 74 72 6f 79 65 64  ey are destroyed
193c5 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 74 68 65  .      ** by the
193c6 20 66 69 6c 65 20 74 72 75 6e 63 61 74 69 6f 6e   file truncation
193c7 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
193c8 20 69 66 28 20 6e 53 72 63 50 61 67 65 73 69 7a   if( nSrcPagesiz
193c9 65 3c 6e 44 65 73 74 50 61 67 65 73 69 7a 65 20  e<nDestPagesize 
193ca 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72  ){.        int r
193cb 61 74 69 6f 20 3d 20 6e 44 65 73 74 50 61 67 65  atio = nDestPage
193cc 73 69 7a 65 2f 6e 53 72 63 50 61 67 65 73 69 7a  size/nSrcPagesiz
193cd 65 3b 0a 20 20 20 20 20 20 20 20 6e 44 65 73 74  e;.        nDest
193ce 54 72 75 6e 63 61 74 65 20 3d 20 28 6e 53 72 63  Truncate = (nSrc
193cf 50 61 67 65 2b 72 61 74 69 6f 2d 31 29 2f 72 61  Page+ratio-1)/ra
193d0 74 69 6f 3b 0a 20 20 20 20 20 20 20 20 69 66 28  tio;.        if(
193d1 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 3d 3d   nDestTruncate==
193d2 28 69 6e 74 29 50 45 4e 44 49 4e 47 5f 42 59 54  (int)PENDING_BYT
193d3 45 5f 50 41 47 45 28 70 2d 3e 70 44 65 73 74 2d  E_PAGE(p->pDest-
193d4 3e 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20  >pBt) ){.       
193d5 20 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65     nDestTruncate
193d6 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  --;.        }.  
193d7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
193d8 20 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65     nDestTruncate
193d9 20 3d 20 6e 53 72 63 50 61 67 65 20 2a 20 28 6e   = nSrcPage * (n
193da 53 72 63 50 61 67 65 73 69 7a 65 2f 6e 44 65 73  SrcPagesize/nDes
193db 74 50 61 67 65 73 69 7a 65 29 3b 0a 20 20 20 20  tPagesize);.    
193dc 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
193dd 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d  3PagerTruncateIm
193de 61 67 65 28 70 44 65 73 74 50 61 67 65 72 2c 20  age(pDestPager, 
193df 6e 44 65 73 74 54 72 75 6e 63 61 74 65 29 3b 0a  nDestTruncate);.
193e0 0a 20 20 20 20 20 20 69 66 28 20 6e 53 72 63 50  .      if( nSrcP
193e1 61 67 65 73 69 7a 65 3c 6e 44 65 73 74 50 61 67  agesize<nDestPag
193e2 65 73 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20  esize ){.       
193e3 20 2f 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63   /* If the sourc
193e4 65 20 70 61 67 65 2d 73 69 7a 65 20 69 73 20 73  e page-size is s
193e5 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20  maller than the 
193e6 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65  destination page
193e7 2d 73 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 2a  -size,.        *
193e8 2a 20 74 77 6f 20 65 78 74 72 61 20 74 68 69 6e  * two extra thin
193e9 67 73 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 68  gs may need to h
193ea 61 70 70 65 6e 3a 0a 20 20 20 20 20 20 20 20 2a  appen:.        *
193eb 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 2a  *.        **   *
193ec 20 54 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   The destination
193ed 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20   may need to be 
193ee 74 72 75 6e 63 61 74 65 64 2c 20 61 6e 64 0a 20  truncated, and. 
193ef 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
193f0 20 20 2a 2a 20 20 20 2a 20 44 61 74 61 20 73 74    **   * Data st
193f1 6f 72 65 64 20 6f 6e 20 74 68 65 20 70 61 67 65  ored on the page
193f2 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f  s immediately fo
193f3 6c 6c 6f 77 69 6e 67 20 74 68 65 20 0a 20 20 20  llowing the .   
193f4 20 20 20 20 20 2a 2a 20 20 20 20 20 70 65 6e 64       **     pend
193f5 69 6e 67 2d 62 79 74 65 20 70 61 67 65 20 69 6e  ing-byte page in
193f6 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
193f7 62 61 73 65 20 6d 61 79 20 6e 65 65 64 20 74 6f  base may need to
193f8 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 20   be.        **  
193f9 20 20 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74     copied into t
193fa 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64  he destination d
193fb 61 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 20  atabase..       
193fc 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e 73   */.        cons
193fd 74 20 69 36 34 20 69 53 69 7a 65 20 3d 20 28 69  t i64 iSize = (i
193fe 36 34 29 6e 53 72 63 50 61 67 65 73 69 7a 65 20  64)nSrcPagesize 
193ff 2a 20 28 69 36 34 29 6e 53 72 63 50 61 67 65 3b  * (i64)nSrcPage;
19400 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
19401 5f 66 69 6c 65 20 2a 20 63 6f 6e 73 74 20 70 46  _file * const pF
19402 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67  ile = sqlite3Pag
19403 65 72 46 69 6c 65 28 70 44 65 73 74 50 61 67 65  erFile(pDestPage
19404 72 29 3b 0a 0a 20 20 20 20 20 20 20 20 61 73 73  r);..        ass
19405 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20  ert( pFile );.  
19406 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 69        assert( (i
19407 36 34 29 6e 44 65 73 74 54 72 75 6e 63 61 74 65  64)nDestTruncate
19408 2a 28 69 36 34 29 6e 44 65 73 74 50 61 67 65 73  *(i64)nDestPages
19409 69 7a 65 20 3e 3d 20 69 53 69 7a 65 20 7c 7c 20  ize >= iSize || 
1940a 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  (.              
1940b 6e 44 65 73 74 54 72 75 6e 63 61 74 65 3d 3d 28  nDestTruncate==(
1940c 69 6e 74 29 28 50 45 4e 44 49 4e 47 5f 42 59 54  int)(PENDING_BYT
1940d 45 5f 50 41 47 45 28 70 2d 3e 70 44 65 73 74 2d  E_PAGE(p->pDest-
1940e 3e 70 42 74 29 2d 31 29 0a 20 20 20 20 20 20 20  >pBt)-1).       
1940f 20 20 20 20 26 26 20 69 53 69 7a 65 3e 3d 50 45      && iSize>=PE
19410 4e 44 49 4e 47 5f 42 59 54 45 20 26 26 20 69 53  NDING_BYTE && iS
19411 69 7a 65 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54  ize<=PENDING_BYT
19412 45 2b 6e 44 65 73 74 50 61 67 65 73 69 7a 65 0a  E+nDestPagesize.
19413 20 20 20 20 20 20 20 20 29 29 3b 0a 20 20 20 20          ));.    
19414 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f      if( SQLITE_O
19415 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K==(rc = sqlite3
19416 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65  PagerCommitPhase
19417 4f 6e 65 28 70 44 65 73 74 50 61 67 65 72 2c 20  One(pDestPager, 
19418 30 2c 20 31 29 29 0a 20 20 20 20 20 20 20 20 20  0, 1)).         
19419 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72  && SQLITE_OK==(r
1941a 63 20 3d 20 62 61 63 6b 75 70 54 72 75 6e 63 61  c = backupTrunca
1941b 74 65 46 69 6c 65 28 70 46 69 6c 65 2c 20 69 53  teFile(pFile, iS
1941c 69 7a 65 29 29 0a 20 20 20 20 20 20 20 20 20 26  ize)).         &
1941d 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63  & SQLITE_OK==(rc
1941e 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53   = sqlite3PagerS
1941f 79 6e 63 28 70 44 65 73 74 50 61 67 65 72 29 29  ync(pDestPager))
19420 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20  .        ){.    
19421 20 20 20 20 20 20 69 36 34 20 69 4f 66 66 3b 0a        i64 iOff;.
19422 20 20 20 20 20 20 20 20 20 20 69 36 34 20 69 45            i64 iE
19423 6e 64 20 3d 20 4d 49 4e 28 50 45 4e 44 49 4e 47  nd = MIN(PENDING
19424 5f 42 59 54 45 20 2b 20 6e 44 65 73 74 50 61 67  _BYTE + nDestPag
19425 65 73 69 7a 65 2c 20 69 53 69 7a 65 29 3b 0a 20  esize, iSize);. 
19426 20 20 20 20 20 20 20 20 20 66 6f 72 28 0a 20 20           for(.  
19427 20 20 20 20 20 20 20 20 20 20 69 4f 66 66 3d 50            iOff=P
19428 45 4e 44 49 4e 47 5f 42 59 54 45 2b 6e 53 72 63  ENDING_BYTE+nSrc
19429 50 61 67 65 73 69 7a 65 3b 20 0a 20 20 20 20 20  Pagesize; .     
1942a 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54         rc==SQLIT
1942b 45 5f 4f 4b 20 26 26 20 69 4f 66 66 3c 69 45 6e  E_OK && iOff<iEn
1942c 64 3b 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  d; .            
1942d 69 4f 66 66 2b 3d 6e 53 72 63 50 61 67 65 73 69  iOff+=nSrcPagesi
1942e 7a 65 0a 20 20 20 20 20 20 20 20 20 20 29 7b 0a  ze.          ){.
1942f 20 20 20 20 20 20 20 20 20 20 20 20 50 67 48 64              PgHd
19430 72 20 2a 70 53 72 63 50 67 20 3d 20 30 3b 0a 20  r *pSrcPg = 0;. 
19431 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
19432 20 50 67 6e 6f 20 69 53 72 63 50 67 20 3d 20 28   Pgno iSrcPg = (
19433 50 67 6e 6f 29 28 28 69 4f 66 66 2f 6e 53 72 63  Pgno)((iOff/nSrc
19434 50 61 67 65 73 69 7a 65 29 2b 31 29 3b 0a 20 20  Pagesize)+1);.  
19435 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
19436 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70  qlite3PagerGet(p
19437 53 72 63 50 61 67 65 72 2c 20 69 53 72 63 50 67  SrcPager, iSrcPg
19438 2c 20 26 70 53 72 63 50 67 29 3b 0a 20 20 20 20  , &pSrcPg);.    
19439 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
1943a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1943b 20 20 20 20 20 20 20 20 20 20 20 75 38 20 2a 7a             u8 *z
1943c 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61  Data = sqlite3Pa
1943d 67 65 72 47 65 74 44 61 74 61 28 70 53 72 63 50  gerGetData(pSrcP
1943e 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  g);.            
1943f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
19440 57 72 69 74 65 28 70 46 69 6c 65 2c 20 7a 44 61  Write(pFile, zDa
19441 74 61 2c 20 6e 53 72 63 50 61 67 65 73 69 7a 65  ta, nSrcPagesize
19442 2c 20 69 4f 66 66 29 3b 0a 20 20 20 20 20 20 20  , iOff);.       
19443 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
19444 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
19445 6e 72 65 66 28 70 53 72 63 50 67 29 3b 0a 20 20  nref(pSrcPg);.  
19446 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
19447 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
19448 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
19449 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
1944a 50 68 61 73 65 4f 6e 65 28 70 44 65 73 74 50 61  PhaseOne(pDestPa
1944b 67 65 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  ger, 0, 0);.    
1944c 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20    }.  .      /* 
1944d 46 69 6e 69 73 68 20 63 6f 6d 6d 69 74 74 69 6e  Finish committin
1944e 67 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  g the transactio
1944f 6e 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61  n to the destina
19450 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e 20 2a  tion database. *
19451 2f 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49  /.      if( SQLI
19452 54 45 5f 4f 4b 3d 3d 72 63 0a 20 20 20 20 20 20  TE_OK==rc.      
19453 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28   && SQLITE_OK==(
19454 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
19455 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28  eCommitPhaseTwo(
19456 70 2d 3e 70 44 65 73 74 29 29 0a 20 20 20 20 20  p->pDest)).     
19457 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
19458 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20   SQLITE_DONE;.  
19459 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20      }.    }.  . 
1945a 20 20 20 2f 2a 20 49 66 20 62 43 6c 6f 73 65 54     /* If bCloseT
1945b 72 61 6e 73 20 69 73 20 74 72 75 65 2c 20 74 68  rans is true, th
1945c 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
1945d 20 6f 70 65 6e 65 64 20 61 20 72 65 61 64 20 74   opened a read t
1945e 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 2a  ransaction.    *
1945f 2a 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20  * on the source 
19460 64 61 74 61 62 61 73 65 2e 20 43 6c 6f 73 65 20  database. Close 
19461 74 68 65 20 72 65 61 64 20 74 72 61 6e 73 61 63  the read transac
19462 74 69 6f 6e 20 68 65 72 65 2e 20 54 68 65 72 65  tion here. There
19463 20 69 73 0a 20 20 20 20 2a 2a 20 6e 6f 20 6e 65   is.    ** no ne
19464 65 64 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20  ed to check the 
19465 72 65 74 75 72 6e 20 76 61 6c 75 65 73 20 6f 66  return values of
19466 20 74 68 65 20 62 74 72 65 65 20 6d 65 74 68 6f   the btree metho
19467 64 73 20 68 65 72 65 2c 20 61 73 0a 20 20 20 20  ds here, as.    
19468 2a 2a 20 22 63 6f 6d 6d 69 74 74 69 6e 67 22 20  ** "committing" 
19469 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e  a read-only tran
1946a 73 61 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66  saction cannot f
1946b 61 69 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ail..    */.    
1946c 69 66 28 20 62 43 6c 6f 73 65 54 72 61 6e 73 20  if( bCloseTrans 
1946d 29 7b 0a 20 20 20 20 20 20 54 45 53 54 4f 4e 4c  ){.      TESTONL
1946e 59 28 20 69 6e 74 20 72 63 32 20 29 3b 0a 20 20  Y( int rc2 );.  
1946f 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 63      TESTONLY( rc
19470 32 20 20 3d 20 29 20 73 71 6c 69 74 65 33 42 74  2  = ) sqlite3Bt
19471 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e  reeCommitPhaseOn
19472 65 28 70 2d 3e 70 53 72 63 2c 20 30 29 3b 0a 20  e(p->pSrc, 0);. 
19473 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 20 72       TESTONLY( r
19474 63 32 20 7c 3d 20 29 20 73 71 6c 69 74 65 33 42  c2 |= ) sqlite3B
19475 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54  treeCommitPhaseT
19476 77 6f 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 20  wo(p->pSrc);.   
19477 20 20 20 61 73 73 65 72 74 28 20 72 63 32 3d 3d     assert( rc2==
19478 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20  SQLITE_OK );.   
19479 20 7d 0a 20 20 0a 20 20 20 20 70 2d 3e 72 63 20   }.  .    p->rc 
1947a 3d 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20  = rc;.  }.  if( 
1947b 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20  p->pDestDb ){.  
1947c 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1947d 6c 65 61 76 65 28 70 2d 3e 70 44 65 73 74 44 62  leave(p->pDestDb
1947e 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20  ->mutex);.  }.  
1947f 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
19480 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 73 71  e(p->pSrc);.  sq
19481 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
19482 65 28 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74  e(p->pSrcDb->mut
19483 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
19484 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61  ;.}../*.** Relea
19485 73 65 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 73  se all resources
19486 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
19487 20 61 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   an sqlite3_back
19488 75 70 2a 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53  up* handle..*/.S
19489 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1948a 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e  lite3_backup_fin
1948b 69 73 68 28 73 71 6c 69 74 65 33 5f 62 61 63 6b  ish(sqlite3_back
1948c 75 70 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65  up *p){.  sqlite
1948d 33 5f 62 61 63 6b 75 70 20 2a 2a 70 70 3b 20 20  3_backup **pp;  
1948e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1948f 2a 20 50 74 72 20 74 6f 20 68 65 61 64 20 6f 66  * Ptr to head of
19490 20 70 61 67 65 72 73 20 62 61 63 6b 75 70 20 6c   pagers backup l
19491 69 73 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ist */.  sqlite3
19492 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20  _mutex *mutex;  
19493 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19494 20 4d 75 74 65 78 20 74 6f 20 70 72 6f 74 65 63   Mutex to protec
19495 74 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73  t source databas
19496 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  e */.  int rc;  
19497 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19498 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
19499 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a  alue to return *
1949a 2f 0a 0a 20 20 2f 2a 20 45 6e 74 65 72 20 74 68  /..  /* Enter th
1949b 65 20 6d 75 74 65 78 65 73 20 2a 2f 0a 20 20 69  e mutexes */.  i
1949c 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
1949d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71   SQLITE_OK;.  sq
1949e 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
1949f 72 28 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74  r(p->pSrcDb->mut
194a0 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  ex);.  sqlite3Bt
194a1 72 65 65 45 6e 74 65 72 28 70 2d 3e 70 53 72 63  reeEnter(p->pSrc
194a2 29 3b 0a 20 20 6d 75 74 65 78 20 3d 20 70 2d 3e  );.  mutex = p->
194a3 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 3b 0a 20  pSrcDb->mutex;. 
194a4 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20   if( p->pDestDb 
194a5 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  ){.    sqlite3_m
194a6 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 44  utex_enter(p->pD
194a7 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  estDb->mutex);. 
194a8 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 61 63 68 20   }..  /* Detach 
194a9 74 68 69 73 20 62 61 63 6b 75 70 20 66 72 6f 6d  this backup from
194aa 20 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65   the source page
194ab 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70  r. */.  if( p->p
194ac 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 70 2d  DestDb ){.    p-
194ad 3e 70 53 72 63 2d 3e 6e 42 61 63 6b 75 70 2d 2d  >pSrc->nBackup--
194ae 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69  ;.  }.  if( p->i
194af 73 41 74 74 61 63 68 65 64 20 29 7b 0a 20 20 20  sAttached ){.   
194b0 20 70 70 20 3d 20 73 71 6c 69 74 65 33 50 61 67   pp = sqlite3Pag
194b1 65 72 42 61 63 6b 75 70 50 74 72 28 73 71 6c 69  erBackupPtr(sqli
194b2 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d  te3BtreePager(p-
194b3 3e 70 53 72 63 29 29 3b 0a 20 20 20 20 77 68 69  >pSrc));.    whi
194b4 6c 65 28 20 2a 70 70 21 3d 70 20 29 7b 0a 20 20  le( *pp!=p ){.  
194b5 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70 29 2d      pp = &(*pp)-
194b6 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20  >pNext;.    }.  
194b7 20 20 2a 70 70 20 3d 20 70 2d 3e 70 4e 65 78 74    *pp = p->pNext
194b8 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61  ;.  }..  /* If a
194b9 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
194ba 73 74 69 6c 6c 20 6f 70 65 6e 20 6f 6e 20 74 68  still open on th
194bb 65 20 42 74 72 65 65 2c 20 72 6f 6c 6c 20 69 74  e Btree, roll it
194bc 20 62 61 63 6b 2e 20 2a 2f 0a 20 20 73 71 6c 69   back. */.  sqli
194bd 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b  te3BtreeRollback
194be 28 70 2d 3e 70 44 65 73 74 29 3b 0a 0a 20 20 2f  (p->pDest);..  /
194bf 2a 20 53 65 74 20 74 68 65 20 65 72 72 6f 72 20  * Set the error 
194c0 63 6f 64 65 20 6f 66 20 74 68 65 20 64 65 73 74  code of the dest
194c1 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65  ination database
194c2 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 72 63   handle. */.  rc
194c3 20 3d 20 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54   = (p->rc==SQLIT
194c4 45 5f 44 4f 4e 45 29 20 3f 20 53 51 4c 49 54 45  E_DONE) ? SQLITE
194c5 5f 4f 4b 20 3a 20 70 2d 3e 72 63 3b 0a 20 20 73  _OK : p->rc;.  s
194c6 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 70  qlite3Error(p->p
194c7 44 65 73 74 44 62 2c 20 72 63 2c 20 30 29 3b 0a  DestDb, rc, 0);.
194c8 0a 20 20 2f 2a 20 45 78 69 74 20 74 68 65 20 6d  .  /* Exit the m
194c9 75 74 65 78 65 73 20 61 6e 64 20 66 72 65 65 20  utexes and free 
194ca 74 68 65 20 62 61 63 6b 75 70 20 63 6f 6e 74 65  the backup conte
194cb 78 74 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f  xt structure. */
194cc 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44  .  if( p->pDestD
194cd 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  b ){.    sqlite3
194ce 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e  _mutex_leave(p->
194cf 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29 3b  pDestDb->mutex);
194d0 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
194d1 72 65 65 4c 65 61 76 65 28 70 2d 3e 70 53 72 63  reeLeave(p->pSrc
194d2 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73  );.  if( p->pDes
194d3 74 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  tDb ){.    sqlit
194d4 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a  e3_free(p);.  }.
194d5 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
194d6 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20  leave(mutex);.  
194d7 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
194d8 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
194d9 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73  umber of pages s
194da 74 69 6c 6c 20 74 6f 20 62 65 20 62 61 63 6b 65  till to be backe
194db 64 20 75 70 20 61 73 20 6f 66 20 74 68 65 20 6d  d up as of the m
194dc 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 63 61  ost recent.** ca
194dd 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61  ll to sqlite3_ba
194de 63 6b 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a  ckup_step()..*/.
194df 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
194e0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72 65  qlite3_backup_re
194e1 6d 61 69 6e 69 6e 67 28 73 71 6c 69 74 65 33 5f  maining(sqlite3_
194e2 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 72 65  backup *p){.  re
194e3 74 75 72 6e 20 70 2d 3e 6e 52 65 6d 61 69 6e 69  turn p->nRemaini
194e4 6e 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ng;.}../*.** Ret
194e5 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e 75  urn the total nu
194e6 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
194e7 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
194e8 62 61 73 65 20 61 73 20 6f 66 20 74 68 65 20 6d  base as of the m
194e9 6f 73 74 20 0a 2a 2a 20 72 65 63 65 6e 74 20 63  ost .** recent c
194ea 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62  all to sqlite3_b
194eb 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 0a 2a 2f  ackup_step()..*/
194ec 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
194ed 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70  sqlite3_backup_p
194ee 61 67 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33  agecount(sqlite3
194ef 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 72  _backup *p){.  r
194f0 65 74 75 72 6e 20 70 2d 3e 6e 50 61 67 65 63 6f  eturn p->nPageco
194f1 75 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  unt;.}../*.** Th
194f2 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
194f3 61 6c 6c 65 64 20 61 66 74 65 72 20 74 68 65 20  alled after the 
194f4 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 61 67 65  contents of page
194f5 20 69 50 61 67 65 20 6f 66 20 74 68 65 0a 2a 2a   iPage of the.**
194f6 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   source database
194f7 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66   have been modif
194f8 69 65 64 2e 20 49 66 20 70 61 67 65 20 69 50 61  ied. If page iPa
194f9 67 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  ge has already b
194fa 65 65 6e 20 0a 2a 2a 20 63 6f 70 69 65 64 20 69  een .** copied i
194fb 6e 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74  nto the destinat
194fc 69 6f 6e 20 64 61 74 61 62 61 73 65 2c 20 74 68  ion database, th
194fd 65 6e 20 74 68 65 20 64 61 74 61 20 77 72 69 74  en the data writ
194fe 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 64 65  ten to the.** de
194ff 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 6e 6f 77  stination is now
19500 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 20 54 68   invalidated. Th
19501 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 63 6f  e destination co
19502 70 79 20 6f 66 20 69 50 61 67 65 20 6e 65 65 64  py of iPage need
19503 73 0a 2a 2a 20 74 6f 20 62 65 20 75 70 64 61 74  s.** to be updat
19504 65 64 20 77 69 74 68 20 74 68 65 20 6e 65 77 20  ed with the new 
19505 64 61 74 61 20 62 65 66 6f 72 65 20 74 68 65 20  data before the 
19506 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
19507 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 2e   is.** complete.
19508 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 61 73 73  .**.** It is ass
19509 75 6d 65 64 20 74 68 61 74 20 74 68 65 20 6d 75  umed that the mu
1950a 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77  tex associated w
1950b 69 74 68 20 74 68 65 20 42 74 53 68 61 72 65 64  ith the BtShared
1950c 20 6f 62 6a 65 63 74 0a 2a 2a 20 63 6f 72 72 65   object.** corre
1950d 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20  sponding to the 
1950e 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20  source database 
1950f 69 73 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69  is held when thi
19510 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a  s function is.**
19511 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49   called..*/.SQLI
19512 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
19513 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64  sqlite3BackupUpd
19514 61 74 65 28 73 71 6c 69 74 65 33 5f 62 61 63 6b  ate(sqlite3_back
19515 75 70 20 2a 70 42 61 63 6b 75 70 2c 20 50 67 6e  up *pBackup, Pgn
19516 6f 20 69 50 61 67 65 2c 20 63 6f 6e 73 74 20 75  o iPage, const u
19517 38 20 2a 61 44 61 74 61 29 7b 0a 20 20 73 71 6c  8 *aData){.  sql
19518 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20  ite3_backup *p; 
19519 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1951a 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61    /* Iterator va
1951b 72 69 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28  riable */.  for(
1951c 70 3d 70 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d  p=pBackup; p; p=
1951d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61  p->pNext){.    a
1951e 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1951f 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72  utex_held(p->pSr
19520 63 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  c->pBt->mutex) )
19521 3b 0a 20 20 20 20 69 66 28 20 21 69 73 46 61 74  ;.    if( !isFat
19522 61 6c 45 72 72 6f 72 28 70 2d 3e 72 63 29 20 26  alError(p->rc) &
19523 26 20 69 50 61 67 65 3c 70 2d 3e 69 4e 65 78 74  & iPage<p->iNext
19524 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65   ){.      /* The
19525 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73 20   backup process 
19526 70 20 68 61 73 20 61 6c 72 65 61 64 79 20 63 6f  p has already co
19527 70 69 65 64 20 70 61 67 65 20 69 50 61 67 65 2e  pied page iPage.
19528 20 42 75 74 20 6e 6f 77 20 69 74 0a 20 20 20 20   But now it.    
19529 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f    ** has been mo
1952a 64 69 66 69 65 64 20 62 79 20 61 20 74 72 61 6e  dified by a tran
1952b 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 73  saction on the s
1952c 6f 75 72 63 65 20 70 61 67 65 72 2e 20 43 6f 70  ource pager. Cop
1952d 79 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e  y.      ** the n
1952e 65 77 20 64 61 74 61 20 69 6e 74 6f 20 74 68 65  ew data into the
1952f 20 62 61 63 6b 75 70 2e 0a 20 20 20 20 20 20 2a   backup..      *
19530 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d  /.      int rc =
19531 20 62 61 63 6b 75 70 4f 6e 65 50 61 67 65 28 70   backupOnePage(p
19532 2c 20 69 50 61 67 65 2c 20 61 44 61 74 61 29 3b  , iPage, aData);
19533 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72  .      assert( r
19534 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26  c!=SQLITE_BUSY &
19535 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 4c 4f 43  & rc!=SQLITE_LOC
19536 4b 45 44 20 29 3b 0a 20 20 20 20 20 20 69 66 28  KED );.      if(
19537 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
19538 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20  {.        p->rc 
19539 3d 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20  = rc;.      }.  
1953a 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
1953b 20 52 65 73 74 61 72 74 20 74 68 65 20 62 61 63   Restart the bac
1953c 6b 75 70 20 70 72 6f 63 65 73 73 2e 20 54 68 69  kup process. Thi
1953d 73 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  s is called when
1953e 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72   the pager layer
1953f 0a 2a 2a 20 64 65 74 65 63 74 73 20 74 68 61 74  .** detects that
19540 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
19541 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20  s been modified 
19542 62 79 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 64  by an external d
19543 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65  atabase.** conne
19544 63 74 69 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63  ction. In this c
19545 61 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20  ase there is no 
19546 77 61 79 20 6f 66 20 6b 6e 6f 77 69 6e 67 20 77  way of knowing w
19547 68 69 63 68 20 6f 66 20 74 68 65 0a 2a 2a 20 70  hich of the.** p
19548 61 67 65 73 20 74 68 61 74 20 68 61 76 65 20 62  ages that have b
19549 65 65 6e 20 63 6f 70 69 65 64 20 69 6e 74 6f 20  een copied into 
1954a 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
1954b 64 61 74 61 62 61 73 65 20 61 72 65 20 73 74 69  database are sti
1954c 6c 6c 20 0a 2a 2a 20 76 61 6c 69 64 20 61 6e 64  ll .** valid and
1954d 20 77 68 69 63 68 20 61 72 65 20 6e 6f 74 2c 20   which are not, 
1954e 73 6f 20 74 68 65 20 65 6e 74 69 72 65 20 70 72  so the entire pr
1954f 6f 63 65 73 73 20 6e 65 65 64 73 20 74 6f 20 62  ocess needs to b
19550 65 20 72 65 73 74 61 72 74 65 64 2e 0a 2a 2a 0a  e restarted..**.
19551 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d 65 64  ** It is assumed
19552 20 74 68 61 74 20 74 68 65 20 6d 75 74 65 78 20   that the mutex 
19553 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
19554 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a  the BtShared obj
19555 65 63 74 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e  ect.** correspon
19556 64 69 6e 67 20 74 6f 20 74 68 65 20 73 6f 75 72  ding to the sour
19557 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20 68  ce database is h
19558 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75  eld when this fu
19559 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c  nction is.** cal
1955a 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  led..*/.SQLITE_P
1955b 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1955c 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74  te3BackupRestart
1955d 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
1955e 2a 70 42 61 63 6b 75 70 29 7b 0a 20 20 73 71 6c  *pBackup){.  sql
1955f 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20  ite3_backup *p; 
19560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19561 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61    /* Iterator va
19562 72 69 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28  riable */.  for(
19563 70 3d 70 42 61 63 6b 75 70 3b 20 70 3b 20 70 3d  p=pBackup; p; p=
19564 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61  p->pNext){.    a
19565 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
19566 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 53 72  utex_held(p->pSr
19567 63 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  c->pBt->mutex) )
19568 3b 0a 20 20 20 20 70 2d 3e 69 4e 65 78 74 20 3d  ;.    p->iNext =
19569 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64   1;.  }.}..#ifnd
1956a 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1956b 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 6f 70 79  ACUUM./*.** Copy
1956c 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 63 6f   the complete co
1956d 6e 74 65 6e 74 20 6f 66 20 70 42 74 46 72 6f 6d  ntent of pBtFrom
1956e 20 69 6e 74 6f 20 70 42 74 54 6f 2e 20 20 41 20   into pBtTo.  A 
1956f 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d  transaction.** m
19570 75 73 74 20 62 65 20 61 63 74 69 76 65 20 66 6f  ust be active fo
19571 72 20 62 6f 74 68 20 66 69 6c 65 73 2e 0a 2a 2a  r both files..**
19572 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20  .** The size of 
19573 66 69 6c 65 20 70 54 6f 20 6d 61 79 20 62 65 20  file pTo may be 
19574 72 65 64 75 63 65 64 20 62 79 20 74 68 69 73 20  reduced by this 
19575 6f 70 65 72 61 74 69 6f 6e 2e 20 49 66 20 61 6e  operation. If an
19576 79 74 68 69 6e 67 20 0a 2a 2a 20 67 6f 65 73 20  ything .** goes 
19577 77 72 6f 6e 67 2c 20 74 68 65 20 74 72 61 6e 73  wrong, the trans
19578 61 63 74 69 6f 6e 20 6f 6e 20 70 54 6f 20 69 73  action on pTo is
19579 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66   rolled back. If
1957a 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65   successful, the
1957b 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e   .** transaction
1957c 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62 65   is committed be
1957d 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a  fore returning..
1957e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1957f 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
19580 65 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65  eeCopyFile(Btree
19581 20 2a 70 54 6f 2c 20 42 74 72 65 65 20 2a 70 46   *pTo, Btree *pF
19582 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  rom){.  int rc;.
19583 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70    sqlite3_backup
19584 20 62 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72   b;.  sqlite3Btr
19585 65 65 45 6e 74 65 72 28 70 54 6f 29 3b 0a 20 20  eeEnter(pTo);.  
19586 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
19587 72 28 70 46 72 6f 6d 29 3b 0a 0a 20 20 2f 2a 20  r(pFrom);..  /* 
19588 53 65 74 20 75 70 20 61 6e 20 73 71 6c 69 74 65  Set up an sqlite
19589 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 2e  3_backup object.
1958a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 2e   sqlite3_backup.
1958b 70 44 65 73 74 44 62 20 6d 75 73 74 20 62 65 20  pDestDb must be 
1958c 73 65 74 0a 20 20 2a 2a 20 74 6f 20 30 2e 20 54  set.  ** to 0. T
1958d 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74  his is used by t
1958e 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
1958f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 62 61  ns of sqlite3_ba
19590 63 6b 75 70 5f 73 74 65 70 28 29 0a 20 20 2a 2a  ckup_step().  **
19591 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 62 61 63   and sqlite3_bac
19592 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 74 6f 20  kup_finish() to 
19593 64 65 74 65 63 74 20 74 68 61 74 20 74 68 65 79  detect that they
19594 20 61 72 65 20 62 65 69 6e 67 20 63 61 6c 6c 65   are being calle
19595 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 68 69 73  d.  ** from this
19596 20 66 75 6e 63 74 69 6f 6e 2c 20 6e 6f 74 20 64   function, not d
19597 69 72 65 63 74 6c 79 20 62 79 20 74 68 65 20 75  irectly by the u
19598 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73  ser..  */.  mems
19599 65 74 28 26 62 2c 20 30 2c 20 73 69 7a 65 6f 66  et(&b, 0, sizeof
1959a 28 62 29 29 3b 0a 20 20 62 2e 70 53 72 63 44 62  (b));.  b.pSrcDb
1959b 20 3d 20 70 46 72 6f 6d 2d 3e 64 62 3b 0a 20 20   = pFrom->db;.  
1959c 62 2e 70 53 72 63 20 3d 20 70 46 72 6f 6d 3b 0a  b.pSrc = pFrom;.
1959d 20 20 62 2e 70 44 65 73 74 20 3d 20 70 54 6f 3b    b.pDest = pTo;
1959e 0a 20 20 62 2e 69 4e 65 78 74 20 3d 20 31 3b 0a  .  b.iNext = 1;.
1959f 0a 20 20 2f 2a 20 30 78 37 46 46 46 46 46 46 46  .  /* 0x7FFFFFFF
195a0 20 69 73 20 74 68 65 20 68 61 72 64 20 6c 69 6d   is the hard lim
195a1 69 74 20 66 6f 72 20 74 68 65 20 6e 75 6d 62 65  it for the numbe
195a2 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 20  r of pages in a 
195a3 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 66 69  database.  ** fi
195a4 6c 65 2e 20 42 79 20 70 61 73 73 69 6e 67 20 74  le. By passing t
195a5 68 69 73 20 61 73 20 74 68 65 20 6e 75 6d 62 65  his as the numbe
195a6 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 63 6f  r of pages to co
195a7 70 79 20 74 6f 0a 20 20 2a 2a 20 73 71 6c 69 74  py to.  ** sqlit
195a8 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
195a9 2c 20 77 65 20 63 61 6e 20 67 75 61 72 61 6e 74  , we can guarant
195aa 65 65 20 74 68 61 74 20 74 68 65 20 63 6f 70 79  ee that the copy
195ab 20 66 69 6e 69 73 68 65 73 20 0a 20 20 2a 2a 20   finishes .  ** 
195ac 77 69 74 68 69 6e 20 61 20 73 69 6e 67 6c 65 20  within a single 
195ad 63 61 6c 6c 20 28 75 6e 6c 65 73 73 20 61 6e 20  call (unless an 
195ae 65 72 72 6f 72 20 6f 63 63 75 72 73 29 2e 20 54  error occurs). T
195af 68 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74  he assert() stat
195b0 65 6d 65 6e 74 0a 20 20 2a 2a 20 63 68 65 63 6b  ement.  ** check
195b1 73 20 74 68 69 73 20 61 73 73 75 6d 70 74 69 6f  s this assumptio
195b2 6e 20 2d 20 28 70 2d 3e 72 63 29 20 73 68 6f 75  n - (p->rc) shou
195b3 6c 64 20 62 65 20 73 65 74 20 74 6f 20 65 69 74  ld be set to eit
195b4 68 65 72 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20  her SQLITE_DONE 
195b5 0a 20 20 2a 2a 20 6f 72 20 61 6e 20 65 72 72 6f  .  ** or an erro
195b6 72 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 73  r code..  */.  s
195b7 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
195b8 65 70 28 26 62 2c 20 30 78 37 46 46 46 46 46 46  ep(&b, 0x7FFFFFF
195b9 46 29 3b 0a 20 20 61 73 73 65 72 74 28 20 62 2e  F);.  assert( b.
195ba 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc!=SQLITE_OK );
195bb 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
195bc 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 26 62  backup_finish(&b
195bd 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
195be 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 54  ITE_OK ){.    pT
195bf 6f 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  o->pBt->pageSize
195c0 46 69 78 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 0a  Fixed = 0;.  }..
195c1 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
195c2 61 76 65 28 70 46 72 6f 6d 29 3b 0a 20 20 73 71  ave(pFrom);.  sq
195c3 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
195c4 70 54 6f 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  pTo);.  return r
195c5 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  c;.}.#endif /* S
195c6 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55  QLITE_OMIT_VACUU
195c7 4d 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  M */../*********
195c8 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 61 63  ***** End of bac
195c9 6b 75 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  kup.c **********
195ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
195cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
195cc 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
195cd 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
195ce 20 76 64 62 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a   vdbemem.c *****
195cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
195d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
195d1 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34  ****/./*.** 2004
195d2 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68   May 26.**.** Th
195d3 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
195d4 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
195d5 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
195d6 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
195d7 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
195d8 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
195d9 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
195da 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
195db 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
195dc 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
195dd 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
195de 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
195df 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
195e0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
195e1 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
195e2 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
195e3 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
195e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
195e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
195e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
195e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
195e8 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
195e9 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
195ea 63 6f 64 65 20 75 73 65 20 74 6f 20 6d 61 6e 69  code use to mani
195eb 70 75 6c 61 74 65 20 22 4d 65 6d 22 20 73 74 72  pulate "Mem" str
195ec 75 63 74 75 72 65 2e 20 20 41 20 22 4d 65 6d 22  ucture.  A "Mem"
195ed 0a 2a 2a 20 73 74 6f 72 65 73 20 61 20 73 69 6e  .** stores a sin
195ee 67 6c 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65  gle value in the
195ef 20 56 44 42 45 2e 20 20 4d 65 6d 20 69 73 20 61   VDBE.  Mem is a
195f0 6e 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75  n opaque structu
195f1 72 65 20 76 69 73 69 62 6c 65 0a 2a 2a 20 6f 6e  re visible.** on
195f2 6c 79 20 77 69 74 68 69 6e 20 74 68 65 20 56 44  ly within the VD
195f3 42 45 2e 20 20 49 6e 74 65 72 66 61 63 65 20 72  BE.  Interface r
195f4 6f 75 74 69 6e 65 73 20 72 65 66 65 72 20 74 6f  outines refer to
195f5 20 61 20 4d 65 6d 20 75 73 69 6e 67 20 74 68 65   a Mem using the
195f6 0a 2a 2a 20 6e 61 6d 65 20 73 71 6c 69 74 65 5f  .** name sqlite_
195f7 76 61 6c 75 65 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  value.**.** $Id:
195f8 20 76 64 62 65 6d 65 6d 2e 63 2c 76 20 31 2e 31   vdbemem.c,v 1.1
195f9 35 32 20 32 30 30 39 2f 30 37 2f 32 32 20 31 38  52 2009/07/22 18
195fa 3a 30 37 3a 34 31 20 64 72 68 20 45 78 70 20 24  :07:41 drh Exp $
195fb 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  .*/../*.** Call 
195fc 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78  sqlite3VdbeMemEx
195fd 70 61 6e 64 42 6c 6f 62 28 29 20 6f 6e 20 74 68  pandBlob() on th
195fe 65 20 73 75 70 70 6c 69 65 64 20 76 61 6c 75 65  e supplied value
195ff 20 28 74 79 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20   (type Mem*).** 
19600 50 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a  P if required..*
19601 2f 0a 23 64 65 66 69 6e 65 20 65 78 70 61 6e 64  /.#define expand
19602 42 6c 6f 62 28 50 29 20 28 28 28 50 29 2d 3e 66  Blob(P) (((P)->f
19603 6c 61 67 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73  lags&MEM_Zero)?s
19604 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70  qlite3VdbeMemExp
19605 61 6e 64 42 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f  andBlob(P):0)../
19606 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20  *.** If pMem is 
19607 61 6e 20 6f 62 6a 65 63 74 20 77 69 74 68 20 61  an object with a
19608 20 76 61 6c 69 64 20 73 74 72 69 6e 67 20 72 65   valid string re
19609 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c 20 74 68  presentation, th
1960a 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 6e  is routine.** en
1960b 73 75 72 65 73 20 74 68 65 20 69 6e 74 65 72 6e  sures the intern
1960c 61 6c 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20  al encoding for 
1960d 74 68 65 20 73 74 72 69 6e 67 20 72 65 70 72 65  the string repre
1960e 73 65 6e 74 61 74 69 6f 6e 20 69 73 0a 2a 2a 20  sentation is.** 
1960f 27 64 65 73 69 72 65 64 45 6e 63 27 2c 20 6f 6e  'desiredEnc', on
19610 65 20 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 38  e of SQLITE_UTF8
19611 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  , SQLITE_UTF16LE
19612 20 6f 72 20 53 51 4c 49 54 45 5f 55 54 46 31 36   or SQLITE_UTF16
19613 42 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65  BE..**.** If pMe
19614 6d 20 69 73 20 6e 6f 74 20 61 20 73 74 72 69 6e  m is not a strin
19615 67 20 6f 62 6a 65 63 74 2c 20 6f 72 20 74 68 65  g object, or the
19616 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65   encoding of the
19617 20 73 74 72 69 6e 67 0a 2a 2a 20 72 65 70 72 65   string.** repre
19618 73 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 6c 72  sentation is alr
19619 65 61 64 79 20 73 74 6f 72 65 64 20 75 73 69 6e  eady stored usin
1961a 67 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  g the requested 
1961b 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74  encoding, then t
1961c 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69  his.** routine i
1961d 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a  s a no-op..**.**
1961e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
1961f 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 63 6f  turned if the co
19620 6e 76 65 72 73 69 6f 6e 20 69 73 20 73 75 63 63  nversion is succ
19621 65 73 73 66 75 6c 20 28 6f 72 20 6e 6f 74 20 72  essful (or not r
19622 65 71 75 69 72 65 64 29 2e 0a 2a 2a 20 53 51 4c  equired)..** SQL
19623 49 54 45 5f 4e 4f 4d 45 4d 20 6d 61 79 20 62 65  ITE_NOMEM may be
19624 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 6d   returned if a m
19625 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 64 75  alloc() fails du
19626 72 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 0a  ring conversion.
19627 2a 2a 20 62 65 74 77 65 65 6e 20 66 6f 72 6d 61  ** between forma
19628 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ts..*/.SQLITE_PR
19629 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1962a 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64  3VdbeChangeEncod
1962b 69 6e 67 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69  ing(Mem *pMem, i
1962c 6e 74 20 64 65 73 69 72 65 64 45 6e 63 29 7b 0a  nt desiredEnc){.
1962d 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65    int rc;.  asse
1962e 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73  rt( (pMem->flags
1962f 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20  &MEM_RowSet)==0 
19630 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 65 73  );.  assert( des
19631 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f  iredEnc==SQLITE_
19632 55 54 46 38 20 7c 7c 20 64 65 73 69 72 65 64 45  UTF8 || desiredE
19633 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  nc==SQLITE_UTF16
19634 4c 45 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c  LE.           ||
19635 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c   desiredEnc==SQL
19636 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20  ITE_UTF16BE );. 
19637 20 69 66 28 20 21 28 70 4d 65 6d 2d 3e 66 6c 61   if( !(pMem->fla
19638 67 73 26 4d 45 4d 5f 53 74 72 29 20 7c 7c 20 70  gs&MEM_Str) || p
19639 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72 65  Mem->enc==desire
1963a 64 45 6e 63 20 29 7b 0a 20 20 20 20 72 65 74 75  dEnc ){.    retu
1963b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
1963c 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d  }.  assert( pMem
1963d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ->db==0 || sqlit
1963e 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d  e3_mutex_held(pM
1963f 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  em->db->mutex) )
19640 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
19641 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 72 65 74  OMIT_UTF16.  ret
19642 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
19643 3b 0a 23 65 6c 73 65 0a 0a 20 20 2f 2a 20 4d 65  ;.#else..  /* Me
19644 6d 54 72 61 6e 73 6c 61 74 65 28 29 20 6d 61 79  mTranslate() may
19645 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
19646 4b 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  K or SQLITE_NOME
19647 4d 2e 20 49 66 20 4e 4f 4d 45 4d 20 69 73 20 72  M. If NOMEM is r
19648 65 74 75 72 6e 65 64 2c 0a 20 20 2a 2a 20 74 68  eturned,.  ** th
19649 65 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  en the encoding 
1964a 6f 66 20 74 68 65 20 76 61 6c 75 65 20 6d 61 79  of the value may
1964b 20 6e 6f 74 20 68 61 76 65 20 63 68 61 6e 67 65   not have change
1964c 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73  d..  */.  rc = s
1964d 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61  qlite3VdbeMemTra
1964e 6e 73 6c 61 74 65 28 70 4d 65 6d 2c 20 28 75 38  nslate(pMem, (u8
1964f 29 64 65 73 69 72 65 64 45 6e 63 29 3b 0a 20 20  )desiredEnc);.  
19650 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54  assert(rc==SQLIT
19651 45 5f 4f 4b 20 20 20 20 7c 7c 20 72 63 3d 3d 53  E_OK    || rc==S
19652 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20  QLITE_NOMEM);.  
19653 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54  assert(rc==SQLIT
19654 45 5f 4f 4b 20 20 20 20 7c 7c 20 70 4d 65 6d 2d  E_OK    || pMem-
19655 3e 65 6e 63 21 3d 64 65 73 69 72 65 64 45 6e 63  >enc!=desiredEnc
19656 29 3b 0a 20 20 61 73 73 65 72 74 28 72 63 3d 3d  );.  assert(rc==
19657 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20  SQLITE_NOMEM || 
19658 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72  pMem->enc==desir
19659 65 64 45 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e  edEnc);.  return
1965a 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f   rc;.#endif.}../
1965b 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 70  *.** Make sure p
1965c 4d 65 6d 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f  Mem->z points to
1965d 20 61 20 77 72 69 74 61 62 6c 65 20 61 6c 6c 6f   a writable allo
1965e 63 61 74 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61  cation of at lea
1965f 73 74 20 0a 2a 2a 20 6e 20 62 79 74 65 73 2e 0a  st .** n bytes..
19660 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d  **.** If the mem
19661 6f 72 79 20 63 65 6c 6c 20 63 75 72 72 65 6e 74  ory cell current
19662 6c 79 20 63 6f 6e 74 61 69 6e 73 20 73 74 72 69  ly contains stri
19663 6e 67 20 6f 72 20 62 6c 6f 62 20 64 61 74 61 0a  ng or blob data.
19664 2a 2a 20 61 6e 64 20 74 68 65 20 74 68 69 72 64  ** and the third
19665 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64   argument passed
19666 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
19667 6e 20 69 73 20 74 72 75 65 2c 20 74 68 65 20 0a  n is true, the .
19668 2a 2a 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65  ** current conte
19669 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69  nt of the cell i
1966a 73 20 70 72 65 73 65 72 76 65 64 2e 20 4f 74 68  s preserved. Oth
1966b 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 0a 2a  erwise, it may.*
1966c 2a 20 62 65 20 64 69 73 63 61 72 64 65 64 2e 20  * be discarded. 
1966d 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e   .**.** This fun
1966e 63 74 69 6f 6e 20 73 65 74 73 20 74 68 65 20 4d  ction sets the M
1966f 45 4d 5f 44 79 6e 20 66 6c 61 67 20 61 6e 64 20  EM_Dyn flag and 
19670 63 6c 65 61 72 73 20 61 6e 79 20 78 44 65 6c 20  clears any xDel 
19671 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 49 74 20  callback..** It 
19672 61 6c 73 6f 20 63 6c 65 61 72 73 20 4d 45 4d 5f  also clears MEM_
19673 45 70 68 65 6d 20 61 6e 64 20 4d 45 4d 5f 53 74  Ephem and MEM_St
19674 61 74 69 63 2e 20 49 66 20 74 68 65 20 70 72 65  atic. If the pre
19675 73 65 72 76 65 20 66 6c 61 67 20 69 73 20 0a 2a  serve flag is .*
19676 2a 20 6e 6f 74 20 73 65 74 2c 20 4d 65 6d 2e 6e  * not set, Mem.n
19677 20 69 73 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 53   is zeroed..*/.S
19678 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
19679 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
1967a 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  Grow(Mem *pMem, 
1967b 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73 65  int n, int prese
1967c 72 76 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20  rve){.  assert( 
1967d 31 20 3e 3d 0a 20 20 20 20 28 28 70 4d 65 6d 2d  1 >=.    ((pMem-
1967e 3e 7a 4d 61 6c 6c 6f 63 20 26 26 20 70 4d 65 6d  >zMalloc && pMem
1967f 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d 70 4d 65 6d 2d  ->zMalloc==pMem-
19680 3e 7a 29 20 3f 20 31 20 3a 20 30 29 20 2b 0a 20  >z) ? 1 : 0) +. 
19681 20 20 20 28 28 28 70 4d 65 6d 2d 3e 66 6c 61 67     (((pMem->flag
19682 73 26 4d 45 4d 5f 44 79 6e 29 26 26 70 4d 65 6d  s&MEM_Dyn)&&pMem
19683 2d 3e 78 44 65 6c 29 20 3f 20 31 20 3a 20 30 29  ->xDel) ? 1 : 0)
19684 20 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e   + .    ((pMem->
19685 66 6c 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29  flags&MEM_Ephem)
19686 20 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20 20 20   ? 1 : 0) + .   
19687 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d   ((pMem->flags&M
19688 45 4d 5f 53 74 61 74 69 63 29 20 3f 20 31 20 3a  EM_Static) ? 1 :
19689 20 30 29 0a 20 20 29 3b 0a 20 20 61 73 73 65 72   0).  );.  asser
1968a 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26  t( (pMem->flags&
1968b 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29  MEM_RowSet)==0 )
1968c 3b 0a 0a 20 20 69 66 28 20 6e 3c 33 32 20 29 20  ;..  if( n<32 ) 
1968d 6e 20 3d 20 33 32 3b 0a 20 20 69 66 28 20 73 71  n = 32;.  if( sq
1968e 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a  lite3DbMallocSiz
1968f 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d  e(pMem->db, pMem
19690 2d 3e 7a 4d 61 6c 6c 6f 63 29 3c 6e 20 29 7b 0a  ->zMalloc)<n ){.
19691 20 20 20 20 69 66 28 20 70 72 65 73 65 72 76 65      if( preserve
19692 20 26 26 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65   && pMem->z==pMe
19693 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20  m->zMalloc ){.  
19694 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d      pMem->z = pM
19695 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73 71  em->zMalloc = sq
19696 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72  lite3DbReallocOr
19697 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70  Free(pMem->db, p
19698 4d 65 6d 2d 3e 7a 2c 20 6e 29 3b 0a 20 20 20 20  Mem->z, n);.    
19699 20 20 70 72 65 73 65 72 76 65 20 3d 20 30 3b 0a    preserve = 0;.
1969a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1969b 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
1969c 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a  Mem->db, pMem->z
1969d 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 70  Malloc);.      p
1969e 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 73  Mem->zMalloc = s
1969f 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
196a0 77 28 70 4d 65 6d 2d 3e 64 62 2c 20 6e 29 3b 0a  w(pMem->db, n);.
196a1 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28      }.  }..  if(
196a2 20 70 4d 65 6d 2d 3e 7a 20 26 26 20 70 72 65 73   pMem->z && pres
196a3 65 72 76 65 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d  erve && pMem->zM
196a4 61 6c 6c 6f 63 20 26 26 20 70 4d 65 6d 2d 3e 7a  alloc && pMem->z
196a5 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20  !=pMem->zMalloc 
196a6 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d  ){.    memcpy(pM
196a7 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 70 4d 65  em->zMalloc, pMe
196a8 6d 2d 3e 7a 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a  m->z, pMem->n);.
196a9 20 20 7d 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e    }.  if( pMem->
196aa 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 20 26 26  flags&MEM_Dyn &&
196ab 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 29 7b 0a 20   pMem->xDel ){. 
196ac 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 28 28 76     pMem->xDel((v
196ad 6f 69 64 20 2a 29 28 70 4d 65 6d 2d 3e 7a 29 29  oid *)(pMem->z))
196ae 3b 0a 20 20 7d 0a 0a 20 20 70 4d 65 6d 2d 3e 7a  ;.  }..  pMem->z
196af 20 3d 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63   = pMem->zMalloc
196b0 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 3d  ;.  if( pMem->z=
196b1 3d 30 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e  =0 ){.    pMem->
196b2 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
196b3 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
196b4 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28  Mem->flags &= ~(
196b5 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74  MEM_Ephem|MEM_St
196b6 61 74 69 63 29 3b 0a 20 20 7d 0a 20 20 70 4d 65  atic);.  }.  pMe
196b7 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 72  m->xDel = 0;.  r
196b8 65 74 75 72 6e 20 28 70 4d 65 6d 2d 3e 7a 20 3f  eturn (pMem->z ?
196b9 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c   SQLITE_OK : SQL
196ba 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f  ITE_NOMEM);.}../
196bb 2a 0a 2a 2a 20 4d 61 6b 65 20 74 68 65 20 67 69  *.** Make the gi
196bc 76 65 6e 20 4d 65 6d 20 6f 62 6a 65 63 74 20 4d  ven Mem object M
196bd 45 4d 5f 44 79 6e 2e 20 20 49 6e 20 6f 74 68 65  EM_Dyn.  In othe
196be 72 20 77 6f 72 64 73 2c 20 6d 61 6b 65 20 69 74  r words, make it
196bf 20 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6e 79 20   so.** that any 
196c0 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 63 6f 6e  TEXT or BLOB con
196c1 74 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20 69  tent is stored i
196c2 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  n memory obtaine
196c3 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63  d from.** malloc
196c4 28 29 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79  ().  In this way
196c5 2c 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74  , we know that t
196c6 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 73 61 66  he memory is saf
196c7 65 20 74 6f 20 62 65 0a 2a 2a 20 6f 76 65 72 77  e to be.** overw
196c8 72 69 74 74 65 6e 20 6f 72 20 61 6c 74 65 72 65  ritten or altere
196c9 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  d..**.** Return 
196ca 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
196cb 63 65 73 73 20 6f 72 20 53 51 4c 49 54 45 5f 4e  cess or SQLITE_N
196cc 4f 4d 45 4d 20 69 66 20 6d 61 6c 6c 6f 63 20 66  OMEM if malloc f
196cd 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ails..*/.SQLITE_
196ce 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
196cf 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72  te3VdbeMemMakeWr
196d0 69 74 65 61 62 6c 65 28 4d 65 6d 20 2a 70 4d 65  iteable(Mem *pMe
196d1 6d 29 7b 0a 20 20 69 6e 74 20 66 3b 0a 20 20 61  m){.  int f;.  a
196d2 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d  ssert( pMem->db=
196d3 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  =0 || sqlite3_mu
196d4 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64  tex_held(pMem->d
196d5 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  b->mutex) );.  a
196d6 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c  ssert( (pMem->fl
196d7 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d  ags&MEM_RowSet)=
196d8 3d 30 20 29 3b 0a 20 20 65 78 70 61 6e 64 42 6c  =0 );.  expandBl
196d9 6f 62 28 70 4d 65 6d 29 3b 0a 20 20 66 20 3d 20  ob(pMem);.  f = 
196da 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69  pMem->flags;.  i
196db 66 28 20 28 66 26 28 4d 45 4d 5f 53 74 72 7c 4d  f( (f&(MEM_Str|M
196dc 45 4d 5f 42 6c 6f 62 29 29 20 26 26 20 70 4d 65  EM_Blob)) && pMe
196dd 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c  m->z!=pMem->zMal
196de 6c 6f 63 20 29 7b 0a 20 20 20 20 69 66 28 20 73  loc ){.    if( s
196df 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
196e0 77 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 20  w(pMem, pMem->n 
196e1 2b 20 32 2c 20 31 29 20 29 7b 0a 20 20 20 20 20  + 2, 1) ){.     
196e2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
196e3 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OMEM;.    }.    
196e4 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d  pMem->z[pMem->n]
196e5 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e   = 0;.    pMem->
196e6 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30  z[pMem->n+1] = 0
196e7 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  ;.    pMem->flag
196e8 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20  s |= MEM_Term;. 
196e9 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c   }..  return SQL
196ea 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
196eb 20 49 66 20 74 68 65 20 67 69 76 65 6e 20 4d 65   If the given Me
196ec 6d 2a 20 68 61 73 20 61 20 7a 65 72 6f 2d 66 69  m* has a zero-fi
196ed 6c 6c 65 64 20 74 61 69 6c 2c 20 74 75 72 6e 20  lled tail, turn 
196ee 69 74 20 69 6e 74 6f 20 61 6e 20 6f 72 64 69 6e  it into an ordin
196ef 61 72 79 0a 2a 2a 20 62 6c 6f 62 20 73 74 6f 72  ary.** blob stor
196f0 65 64 20 69 6e 20 64 79 6e 61 6d 69 63 61 6c 6c  ed in dynamicall
196f1 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63  y allocated spac
196f2 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  e..*/.#ifndef SQ
196f3 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
196f4 4f 42 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  OB.SQLITE_PRIVAT
196f5 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
196f6 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d  eMemExpandBlob(M
196f7 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 66 28  em *pMem){.  if(
196f8 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d   pMem->flags & M
196f9 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 69  EM_Zero ){.    i
196fa 6e 74 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 73  nt nByte;.    as
196fb 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67  sert( pMem->flag
196fc 73 26 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20  s&MEM_Blob );.  
196fd 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
196fe 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65  >flags&MEM_RowSe
196ff 74 29 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  t)==0 );.    ass
19700 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30  ert( pMem->db==0
19701 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   || sqlite3_mute
19702 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d  x_held(pMem->db-
19703 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 20 20  >mutex) );..    
19704 2f 2a 20 53 65 74 20 6e 42 79 74 65 20 74 6f 20  /* Set nByte to 
19705 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
19706 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20  tes required to 
19707 73 74 6f 72 65 20 74 68 65 20 65 78 70 61 6e 64  store the expand
19708 65 64 20 62 6c 6f 62 2e 20 2a 2f 0a 20 20 20 20  ed blob. */.    
19709 6e 42 79 74 65 20 3d 20 70 4d 65 6d 2d 3e 6e 20  nByte = pMem->n 
1970a 2b 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b  + pMem->u.nZero;
1970b 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 3c 3d  .    if( nByte<=
1970c 30 20 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65  0 ){.      nByte
1970d 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 1;.    }.    
1970e 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
1970f 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79  emGrow(pMem, nBy
19710 74 65 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20  te, 1) ){.      
19711 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
19712 4d 45 4d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  MEM;.    }..    
19713 6d 65 6d 73 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b  memset(&pMem->z[
19714 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65  pMem->n], 0, pMe
19715 6d 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20  m->u.nZero);.   
19716 20 70 4d 65 6d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d   pMem->n += pMem
19717 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 70  ->u.nZero;.    p
19718 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28  Mem->flags &= ~(
19719 4d 45 4d 5f 5a 65 72 6f 7c 4d 45 4d 5f 54 65 72  MEM_Zero|MEM_Ter
1971a 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  m);.  }.  return
1971b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65   SQLITE_OK;.}.#e
1971c 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  ndif.../*.** Mak
1971d 65 20 73 75 72 65 20 74 68 65 20 67 69 76 65 6e  e sure the given
1971e 20 4d 65 6d 20 69 73 20 5c 75 30 30 30 30 20 74   Mem is \u0000 t
1971f 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51  erminated..*/.SQ
19720 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
19721 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e   sqlite3VdbeMemN
19722 75 6c 54 65 72 6d 69 6e 61 74 65 28 4d 65 6d 20  ulTerminate(Mem 
19723 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74  *pMem){.  assert
19724 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
19725 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
19726 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
19727 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 28 70  tex) );.  if( (p
19728 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
19729 5f 54 65 72 6d 29 21 3d 30 20 7c 7c 20 28 70 4d  _Term)!=0 || (pM
1972a 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
1972b 53 74 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72  Str)==0 ){.    r
1972c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1972d 20 20 20 2f 2a 20 4e 6f 74 68 69 6e 67 20 74 6f     /* Nothing to
1972e 20 64 6f 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28   do */.  }.  if(
1972f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47   sqlite3VdbeMemG
19730 72 6f 77 28 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e  row(pMem, pMem->
19731 6e 2b 32 2c 20 31 29 20 29 7b 0a 20 20 20 20 72  n+2, 1) ){.    r
19732 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
19733 45 4d 3b 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e  EM;.  }.  pMem->
19734 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a  z[pMem->n] = 0;.
19735 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e    pMem->z[pMem->
19736 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d  n+1] = 0;.  pMem
19737 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54  ->flags |= MEM_T
19738 65 72 6d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  erm;.  return SQ
19739 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
1973a 2a 20 41 64 64 20 4d 45 4d 5f 53 74 72 20 74 6f  * Add MEM_Str to
1973b 20 74 68 65 20 73 65 74 20 6f 66 20 72 65 70 72   the set of repr
1973c 65 73 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20  esentations for 
1973d 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 2e 20 20  the given Mem.  
1973e 4e 75 6d 62 65 72 73 0a 2a 2a 20 61 72 65 20 63  Numbers.** are c
1973f 6f 6e 76 65 72 74 65 64 20 75 73 69 6e 67 20 73  onverted using s
19740 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
19741 29 2e 20 20 43 6f 6e 76 65 72 74 69 6e 67 20 61  ).  Converting a
19742 20 42 4c 4f 42 20 74 6f 20 61 20 73 74 72 69 6e   BLOB to a strin
19743 67 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  g.** is a no-op.
19744 0a 2a 2a 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20  .**.** Existing 
19745 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20  representations 
19746 4d 45 4d 5f 49 6e 74 20 61 6e 64 20 4d 45 4d 5f  MEM_Int and MEM_
19747 52 65 61 6c 20 61 72 65 20 2a 6e 6f 74 2a 20 69  Real are *not* i
19748 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a  nvalidated..**.*
19749 2a 20 41 20 4d 45 4d 5f 4e 75 6c 6c 20 76 61 6c  * A MEM_Null val
1974a 75 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65  ue will never be
1974b 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20   passed to this 
1974c 66 75 6e 63 74 69 6f 6e 2e 20 54 68 69 73 20 66  function. This f
1974d 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 73  unction is.** us
1974e 65 64 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e  ed for convertin
1974f 67 20 76 61 6c 75 65 73 20 74 6f 20 74 65 78 74  g values to text
19750 20 66 6f 72 20 72 65 74 75 72 6e 69 6e 67 20 74   for returning t
19751 6f 20 74 68 65 20 75 73 65 72 20 28 69 2e 65 2e  o the user (i.e.
19752 20 76 69 61 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   via.** sqlite3_
19753 76 61 6c 75 65 5f 74 65 78 74 28 29 29 2c 20 6f  value_text()), o
19754 72 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 74  r for ensuring t
19755 68 61 74 20 76 61 6c 75 65 73 20 74 6f 20 62 65  hat values to be
19756 20 75 73 65 64 20 61 73 20 62 74 72 65 65 0a 2a   used as btree.*
19757 2a 20 6b 65 79 73 20 61 72 65 20 73 74 72 69 6e  * keys are strin
19758 67 73 2e 20 49 6e 20 74 68 65 20 66 6f 72 6d 65  gs. In the forme
19759 72 20 63 61 73 65 20 61 20 4e 55 4c 4c 20 70 6f  r case a NULL po
1975a 69 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65  inter is returne
1975b 64 20 74 68 65 0a 2a 2a 20 75 73 65 72 20 61 6e  d the.** user an
1975c 64 20 74 68 65 20 6c 61 74 65 72 20 69 73 20 61  d the later is a
1975d 6e 20 69 6e 74 65 72 6e 61 6c 20 70 72 6f 67 72  n internal progr
1975e 61 6d 6d 69 6e 67 20 65 72 72 6f 72 2e 0a 2a 2f  amming error..*/
1975f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
19760 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
19761 65 6d 53 74 72 69 6e 67 69 66 79 28 4d 65 6d 20  emStringify(Mem 
19762 2a 70 4d 65 6d 2c 20 69 6e 74 20 65 6e 63 29 7b  *pMem, int enc){
19763 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
19764 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 66 67 20  TE_OK;.  int fg 
19765 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20  = pMem->flags;. 
19766 20 63 6f 6e 73 74 20 69 6e 74 20 6e 42 79 74 65   const int nByte
19767 20 3d 20 33 32 3b 0a 0a 20 20 61 73 73 65 72 74   = 32;..  assert
19768 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
19769 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1976a 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
1976b 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
1976c 28 20 21 28 66 67 26 4d 45 4d 5f 5a 65 72 6f 29  ( !(fg&MEM_Zero)
1976d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 28   );.  assert( !(
1976e 66 67 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f  fg&(MEM_Str|MEM_
1976f 42 6c 6f 62 29 29 20 29 3b 0a 20 20 61 73 73 65  Blob)) );.  asse
19770 72 74 28 20 66 67 26 28 4d 45 4d 5f 49 6e 74 7c  rt( fg&(MEM_Int|
19771 4d 45 4d 5f 52 65 61 6c 29 20 29 3b 0a 20 20 61  MEM_Real) );.  a
19772 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c  ssert( (pMem->fl
19773 61 67 73 26 4d 45 4d 5f 52 6f 77 53 65 74 29 3d  ags&MEM_RowSet)=
19774 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
19775 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e  EIGHT_BYTE_ALIGN
19776 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 0a  MENT(pMem) );...
19777 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
19778 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e  eMemGrow(pMem, n
19779 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20  Byte, 0) ){.    
1977a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
1977b 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46  MEM;.  }..  /* F
1977c 6f 72 20 61 20 52 65 61 6c 20 6f 72 20 49 6e 74  or a Real or Int
1977d 65 67 65 72 2c 20 75 73 65 20 73 71 6c 69 74 65  eger, use sqlite
1977e 33 5f 6d 70 72 69 6e 74 66 28 29 20 74 6f 20 70  3_mprintf() to p
1977f 72 6f 64 75 63 65 20 74 68 65 20 55 54 46 2d 38  roduce the UTF-8
19780 0a 20 20 2a 2a 20 73 74 72 69 6e 67 20 72 65 70  .  ** string rep
19781 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
19782 68 65 20 76 61 6c 75 65 2e 20 54 68 65 6e 2c 20  he value. Then, 
19783 69 66 20 74 68 65 20 72 65 71 75 69 72 65 64 20  if the required 
19784 65 6e 63 6f 64 69 6e 67 0a 20 20 2a 2a 20 69 73  encoding.  ** is
19785 20 55 54 46 2d 31 36 6c 65 20 6f 72 20 55 54 46   UTF-16le or UTF
19786 2d 31 36 62 65 20 64 6f 20 61 20 74 72 61 6e 73  -16be do a trans
19787 6c 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 0a 20 20  lation..  ** .  
19788 2a 2a 20 46 49 58 20 4d 45 3a 20 49 74 20 77 6f  ** FIX ME: It wo
19789 75 6c 64 20 62 65 20 62 65 74 74 65 72 20 69 66  uld be better if
1978a 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1978b 66 28 29 20 63 6f 75 6c 64 20 64 6f 20 55 54 46  f() could do UTF
1978c 2d 31 36 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  -16..  */.  if( 
1978d 66 67 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a  fg & MEM_Int ){.
1978e 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1978f 69 6e 74 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d  intf(nByte, pMem
19790 2d 3e 7a 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65  ->z, "%lld", pMe
19791 6d 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65  m->u.i);.  }else
19792 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 66 67  {.    assert( fg
19793 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20   & MEM_Real );. 
19794 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
19795 6e 74 66 28 6e 42 79 74 65 2c 20 70 4d 65 6d 2d  ntf(nByte, pMem-
19796 3e 7a 2c 20 22 25 21 2e 31 35 67 22 2c 20 70 4d  >z, "%!.15g", pM
19797 65 6d 2d 3e 72 29 3b 0a 20 20 7d 0a 20 20 70 4d  em->r);.  }.  pM
19798 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53  em->n = sqlite3S
19799 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29  trlen30(pMem->z)
1979a 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20  ;.  pMem->enc = 
1979b 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 70  SQLITE_UTF8;.  p
1979c 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Mem->flags |= ME
1979d 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a  M_Str|MEM_Term;.
1979e 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1979f 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65 6d  ngeEncoding(pMem
197a0 2c 20 65 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e  , enc);.  return
197a1 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65   rc;.}../*.** Me
197a2 6d 6f 72 79 20 63 65 6c 6c 20 70 4d 65 6d 20 63  mory cell pMem c
197a3 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e 74  ontains the cont
197a4 65 78 74 20 6f 66 20 61 6e 20 61 67 67 72 65 67  ext of an aggreg
197a5 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  ate function..**
197a6 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   This routine ca
197a7 6c 6c 73 20 74 68 65 20 66 69 6e 61 6c 69 7a 65  lls the finalize
197a8 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61 74   method for that
197a9 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 0a   function.  The.
197aa 2a 2a 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  ** result of the
197ab 20 61 67 67 72 65 67 61 74 65 20 69 73 20 73 74   aggregate is st
197ac 6f 72 65 64 20 62 61 63 6b 20 69 6e 74 6f 20 70  ored back into p
197ad 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  Mem..**.** Retur
197ae 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69  n SQLITE_ERROR i
197af 66 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20  f the finalizer 
197b0 72 65 70 6f 72 74 73 20 61 6e 20 65 72 72 6f 72  reports an error
197b1 2e 20 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20  .  SQLITE_OK.** 
197b2 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51  otherwise..*/.SQ
197b3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
197b4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46   sqlite3VdbeMemF
197b5 69 6e 61 6c 69 7a 65 28 4d 65 6d 20 2a 70 4d 65  inalize(Mem *pMe
197b6 6d 2c 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e  m, FuncDef *pFun
197b7 63 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  c){.  int rc = S
197b8 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
197b9 41 4c 57 41 59 53 28 70 46 75 6e 63 20 26 26 20  ALWAYS(pFunc && 
197ba 70 46 75 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a 65  pFunc->xFinalize
197bb 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
197bc 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 20  _context ctx;.  
197bd 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
197be 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
197bf 6c 29 21 3d 30 20 7c 7c 20 70 46 75 6e 63 3d 3d  l)!=0 || pFunc==
197c0 70 4d 65 6d 2d 3e 75 2e 70 44 65 66 20 29 3b 0a  pMem->u.pDef );.
197c1 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
197c2 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ->db==0 || sqlit
197c3 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d  e3_mutex_held(pM
197c4 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  em->db->mutex) )
197c5 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 63 74  ;.    memset(&ct
197c6 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 63 74 78  x, 0, sizeof(ctx
197c7 29 29 3b 0a 20 20 20 20 63 74 78 2e 73 2e 66 6c  ));.    ctx.s.fl
197c8 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a  ags = MEM_Null;.
197c9 20 20 20 20 63 74 78 2e 73 2e 64 62 20 3d 20 70      ctx.s.db = p
197ca 4d 65 6d 2d 3e 64 62 3b 0a 20 20 20 20 63 74 78  Mem->db;.    ctx
197cb 2e 70 4d 65 6d 20 3d 20 70 4d 65 6d 3b 0a 20 20  .pMem = pMem;.  
197cc 20 20 63 74 78 2e 70 46 75 6e 63 20 3d 20 70 46    ctx.pFunc = pF
197cd 75 6e 63 3b 0a 20 20 20 20 70 46 75 6e 63 2d 3e  unc;.    pFunc->
197ce 78 46 69 6e 61 6c 69 7a 65 28 26 63 74 78 29 3b  xFinalize(&ctx);
197cf 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 3d 3d  .    assert( 0==
197d0 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d  (pMem->flags&MEM
197d1 5f 44 79 6e 29 20 26 26 20 21 70 4d 65 6d 2d 3e  _Dyn) && !pMem->
197d2 78 44 65 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69  xDel );.    sqli
197d3 74 65 33 44 62 46 72 65 65 28 70 4d 65 6d 2d 3e  te3DbFree(pMem->
197d4 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f  db, pMem->zMallo
197d5 63 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70  c);.    memcpy(p
197d6 4d 65 6d 2c 20 26 63 74 78 2e 73 2c 20 73 69 7a  Mem, &ctx.s, siz
197d7 65 6f 66 28 63 74 78 2e 73 29 29 3b 0a 20 20 20  eof(ctx.s));.   
197d8 20 72 63 20 3d 20 63 74 78 2e 69 73 45 72 72 6f   rc = ctx.isErro
197d9 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  r;.  }.  return 
197da 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  rc;.}../*.** If 
197db 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  the memory cell 
197dc 63 6f 6e 74 61 69 6e 73 20 61 20 73 74 72 69 6e  contains a strin
197dd 67 20 76 61 6c 75 65 20 74 68 61 74 20 6d 75 73  g value that mus
197de 74 20 62 65 20 66 72 65 65 64 20 62 79 0a 2a 2a  t be freed by.**
197df 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 65 78 74   invoking an ext
197e0 65 72 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 2c 20  ernal callback, 
197e1 66 72 65 65 20 69 74 20 6e 6f 77 2e 20 43 61 6c  free it now. Cal
197e2 6c 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69  ling this functi
197e3 6f 6e 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 66  on.** does not f
197e4 72 65 65 20 61 6e 79 20 4d 65 6d 2e 7a 4d 61 6c  ree any Mem.zMal
197e5 6c 6f 63 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 53  loc buffer..*/.S
197e6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
197e7 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
197e8 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c  mReleaseExternal
197e9 28 4d 65 6d 20 2a 70 29 7b 0a 20 20 61 73 73 65  (Mem *p){.  asse
197ea 72 74 28 20 70 2d 3e 64 62 3d 3d 30 20 7c 7c 20  rt( p->db==0 || 
197eb 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
197ec 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ld(p->db->mutex)
197ed 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
197ee 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41  p->flags & MEM_A
197ef 67 67 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65  gg );.  testcase
197f0 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  ( p->flags & MEM
197f1 5f 44 79 6e 20 29 3b 0a 20 20 74 65 73 74 63 61  _Dyn );.  testca
197f2 73 65 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d  se( p->flags & M
197f3 45 4d 5f 52 6f 77 53 65 74 20 29 3b 0a 20 20 74  EM_RowSet );.  t
197f4 65 73 74 63 61 73 65 28 20 70 2d 3e 66 6c 61 67  estcase( p->flag
197f5 73 20 26 20 4d 45 4d 5f 46 72 61 6d 65 20 29 3b  s & MEM_Frame );
197f6 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26  .  if( p->flags&
197f7 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d 5f 44 79 6e  (MEM_Agg|MEM_Dyn
197f8 7c 4d 45 4d 5f 52 6f 77 53 65 74 7c 4d 45 4d 5f  |MEM_RowSet|MEM_
197f9 46 72 61 6d 65 29 20 29 7b 0a 20 20 20 20 69 66  Frame) ){.    if
197fa 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 41  ( p->flags&MEM_A
197fb 67 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  gg ){.      sqli
197fc 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69  te3VdbeMemFinali
197fd 7a 65 28 70 2c 20 70 2d 3e 75 2e 70 44 65 66 29  ze(p, p->u.pDef)
197fe 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
197ff 28 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  (p->flags & MEM_
19800 41 67 67 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20  Agg)==0 );.     
19801 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
19802 65 6c 65 61 73 65 28 70 29 3b 0a 20 20 20 20 7d  elease(p);.    }
19803 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67  else if( p->flag
19804 73 26 4d 45 4d 5f 44 79 6e 20 26 26 20 70 2d 3e  s&MEM_Dyn && p->
19805 78 44 65 6c 20 29 7b 0a 20 20 20 20 20 20 61 73  xDel ){.      as
19806 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67 73 26  sert( (p->flags&
19807 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29  MEM_RowSet)==0 )
19808 3b 0a 20 20 20 20 20 20 70 2d 3e 78 44 65 6c 28  ;.      p->xDel(
19809 28 76 6f 69 64 20 2a 29 70 2d 3e 7a 29 3b 0a 20  (void *)p->z);. 
1980a 20 20 20 20 20 70 2d 3e 78 44 65 6c 20 3d 20 30       p->xDel = 0
1980b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
1980c 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77  p->flags&MEM_Row
1980d 53 65 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Set ){.      sql
1980e 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28  ite3RowSetClear(
1980f 70 2d 3e 75 2e 70 52 6f 77 53 65 74 29 3b 0a 20  p->u.pRowSet);. 
19810 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e     }else if( p->
19811 66 6c 61 67 73 26 4d 45 4d 5f 46 72 61 6d 65 20  flags&MEM_Frame 
19812 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
19813 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70  VdbeMemSetNull(p
19814 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
19815 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6e  /*.** Release an
19816 79 20 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 79  y memory held by
19817 20 74 68 65 20 4d 65 6d 2e 20 54 68 69 73 20 6d   the Mem. This m
19818 61 79 20 6c 65 61 76 65 20 74 68 65 20 4d 65 6d  ay leave the Mem
19819 20 69 6e 20 61 6e 0a 2a 2a 20 69 6e 63 6f 6e 73   in an.** incons
1981a 69 73 74 65 6e 74 20 73 74 61 74 65 2c 20 66 6f  istent state, fo
1981b 72 20 65 78 61 6d 70 6c 65 20 77 69 74 68 20 28  r example with (
1981c 4d 65 6d 2e 7a 3d 3d 30 29 20 61 6e 64 0a 2a 2a  Mem.z==0) and.**
1981d 20 28 4d 65 6d 2e 74 79 70 65 3d 3d 53 51 4c 49   (Mem.type==SQLI
1981e 54 45 5f 54 45 58 54 29 2e 0a 2a 2f 0a 53 51 4c  TE_TEXT)..*/.SQL
1981f 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
19820 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
19821 65 6c 65 61 73 65 28 4d 65 6d 20 2a 70 29 7b 0a  elease(Mem *p){.
19822 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
19823 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28  ReleaseExternal(
19824 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  p);.  sqlite3DbF
19825 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 7a 4d  ree(p->db, p->zM
19826 61 6c 6c 6f 63 29 3b 0a 20 20 70 2d 3e 7a 20 3d  alloc);.  p->z =
19827 20 30 3b 0a 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63   0;.  p->zMalloc
19828 20 3d 20 30 3b 0a 20 20 70 2d 3e 78 44 65 6c 20   = 0;.  p->xDel 
19829 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  = 0;.}../*.** Co
1982a 6e 76 65 72 74 20 61 20 36 34 2d 62 69 74 20 49  nvert a 64-bit I
1982b 45 45 45 20 64 6f 75 62 6c 65 20 69 6e 74 6f 20  EEE double into 
1982c 61 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20  a 64-bit signed 
1982d 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 66 20 74  integer..** If t
1982e 68 65 20 64 6f 75 62 6c 65 20 69 73 20 74 6f 6f  he double is too
1982f 20 6c 61 72 67 65 2c 20 72 65 74 75 72 6e 20 30   large, return 0
19830 78 38 30 30 30 30 30 30 30 30 30 30 30 30 30 30  x800000000000000
19831 30 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 73 79  0..**.** Most sy
19832 73 74 65 6d 73 20 61 70 70 65 61 72 20 74 6f 20  stems appear to 
19833 64 6f 20 74 68 69 73 20 73 69 6d 70 6c 79 20 62  do this simply b
19834 79 20 61 73 73 69 67 6e 69 6e 67 0a 2a 2a 20 76  y assigning.** v
19835 61 72 69 61 62 6c 65 73 20 61 6e 64 20 77 69 74  ariables and wit
19836 68 6f 75 74 20 74 68 65 20 65 78 74 72 61 20 72  hout the extra r
19837 61 6e 67 65 20 74 65 73 74 73 2e 20 20 42 75 74  ange tests.  But
19838 0a 2a 2a 20 74 68 65 72 65 20 61 72 65 20 72 65  .** there are re
19839 70 6f 72 74 73 20 74 68 61 74 20 77 69 6e 64 6f  ports that windo
1983a 77 73 20 74 68 72 6f 77 73 20 61 6e 20 65 78 70  ws throws an exp
1983b 65 63 74 69 6f 6e 0a 2a 2a 20 69 66 20 74 68 65  ection.** if the
1983c 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
1983d 76 61 6c 75 65 20 69 73 20 6f 75 74 20 6f 66 20  value is out of 
1983e 72 61 6e 67 65 2e 20 28 53 65 65 20 74 69 63 6b  range. (See tick
1983f 65 74 20 23 32 38 38 30 2e 29 0a 2a 2a 20 42 65  et #2880.).** Be
19840 63 61 75 73 65 20 77 65 20 64 6f 20 6e 6f 74 20  cause we do not 
19841 63 6f 6d 70 6c 65 74 65 6c 79 20 75 6e 64 65 72  completely under
19842 73 74 61 6e 64 20 74 68 65 20 70 72 6f 62 6c 65  stand the proble
19843 6d 2c 20 77 65 20 77 69 6c 6c 0a 2a 2a 20 74 61  m, we will.** ta
19844 6b 65 20 74 68 65 20 63 6f 6e 73 65 72 76 61 74  ke the conservat
19845 69 76 65 20 61 70 70 72 6f 61 63 68 20 61 6e 64  ive approach and
19846 20 61 6c 77 61 79 73 20 64 6f 20 72 61 6e 67 65   always do range
19847 20 74 65 73 74 73 0a 2a 2a 20 62 65 66 6f 72 65   tests.** before
19848 20 61 74 74 65 6d 70 74 69 6e 67 20 74 68 65 20   attempting the 
19849 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2f 0a 73  conversion..*/.s
1984a 74 61 74 69 63 20 69 36 34 20 64 6f 75 62 6c 65  tatic i64 double
1984b 54 6f 49 6e 74 36 34 28 64 6f 75 62 6c 65 20 72  ToInt64(double r
1984c 29 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 61 6e  ){.  /*.  ** Man
1984d 79 20 63 6f 6d 70 69 6c 65 72 73 20 77 65 20 65  y compilers we e
1984e 6e 63 6f 75 6e 74 65 72 20 64 6f 20 6e 6f 74 20  ncounter do not 
1984f 64 65 66 69 6e 65 20 63 6f 6e 73 74 61 6e 74 73  define constants
19850 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d 69   for the.  ** mi
19851 6e 69 6d 75 6d 20 61 6e 64 20 6d 61 78 69 6d 75  nimum and maximu
19852 6d 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72  m 64-bit integer
19853 73 2c 20 6f 72 20 74 68 65 79 20 64 65 66 69 6e  s, or they defin
19854 65 20 74 68 65 6d 0a 20 20 2a 2a 20 69 6e 63 6f  e them.  ** inco
19855 6e 73 69 73 74 65 6e 74 6c 79 2e 20 20 41 6e 64  nsistently.  And
19856 20 6d 61 6e 79 20 64 6f 20 6e 6f 74 20 75 6e 64   many do not und
19857 65 72 73 74 61 6e 64 20 74 68 65 20 22 4c 4c 22  erstand the "LL"
19858 20 6e 6f 74 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20   notation..  ** 
19859 53 6f 20 77 65 20 64 65 66 69 6e 65 20 6f 75 72  So we define our
1985a 20 6f 77 6e 20 73 74 61 74 69 63 20 63 6f 6e 73   own static cons
1985b 74 61 6e 74 73 20 68 65 72 65 20 75 73 69 6e 67  tants here using
1985c 20 6e 6f 74 68 69 6e 67 0a 20 20 2a 2a 20 6c 61   nothing.  ** la
1985d 72 67 65 72 20 74 68 61 6e 20 61 20 33 32 2d 62  rger than a 32-b
1985e 69 74 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74  it integer const
1985f 61 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74  ant..  */.  stat
19860 69 63 20 63 6f 6e 73 74 20 69 36 34 20 6d 61 78  ic const i64 max
19861 49 6e 74 20 3d 20 4c 41 52 47 45 53 54 5f 49 4e  Int = LARGEST_IN
19862 54 36 34 3b 0a 20 20 73 74 61 74 69 63 20 63 6f  T64;.  static co
19863 6e 73 74 20 69 36 34 20 6d 69 6e 49 6e 74 20 3d  nst i64 minInt =
19864 20 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 3b   SMALLEST_INT64;
19865 0a 0a 20 20 69 66 28 20 72 3c 28 64 6f 75 62 6c  ..  if( r<(doubl
19866 65 29 6d 69 6e 49 6e 74 20 29 7b 0a 20 20 20 20  e)minInt ){.    
19867 72 65 74 75 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20  return minInt;. 
19868 20 7d 65 6c 73 65 20 69 66 28 20 72 3e 28 64 6f   }else if( r>(do
19869 75 62 6c 65 29 6d 61 78 49 6e 74 20 29 7b 0a 20  uble)maxInt ){. 
1986a 20 20 20 2f 2a 20 6d 69 6e 49 6e 74 20 69 73 20     /* minInt is 
1986b 63 6f 72 72 65 63 74 20 68 65 72 65 20 2d 20 6e  correct here - n
1986c 6f 74 20 6d 61 78 49 6e 74 2e 20 20 49 74 20 74  ot maxInt.  It t
1986d 75 72 6e 73 20 6f 75 74 20 74 68 61 74 20 61 73  urns out that as
1986e 73 69 67 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 61  signing.    ** a
1986f 20 76 65 72 79 20 6c 61 72 67 65 20 70 6f 73 69   very large posi
19870 74 69 76 65 20 6e 75 6d 62 65 72 20 74 6f 20 61  tive number to a
19871 6e 20 69 6e 74 65 67 65 72 20 72 65 73 75 6c 74  n integer result
19872 73 20 69 6e 20 61 20 76 65 72 79 20 6c 61 72 67  s in a very larg
19873 65 0a 20 20 20 20 2a 2a 20 6e 65 67 61 74 69 76  e.    ** negativ
19874 65 20 69 6e 74 65 67 65 72 2e 20 20 54 68 69 73  e integer.  This
19875 20 6d 61 6b 65 73 20 6e 6f 20 73 65 6e 73 65 2c   makes no sense,
19876 20 62 75 74 20 69 74 20 69 73 20 77 68 61 74 20   but it is what 
19877 78 38 36 20 68 61 72 64 77 61 72 65 0a 20 20 20  x86 hardware.   
19878 20 2a 2a 20 64 6f 65 73 20 73 6f 20 66 6f 72 20   ** does so for 
19879 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 65  compatibility we
1987a 20 77 69 6c 6c 20 64 6f 20 74 68 65 20 73 61 6d   will do the sam
1987b 65 20 69 6e 20 73 6f 66 74 77 61 72 65 2e 20 2a  e in software. *
1987c 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 69 6e  /.    return min
1987d 49 6e 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  Int;.  }else{.  
1987e 20 20 72 65 74 75 72 6e 20 28 69 36 34 29 72 3b    return (i64)r;
1987f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
19880 74 75 72 6e 20 73 6f 6d 65 20 6b 69 6e 64 20 6f  turn some kind o
19881 66 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  f integer value 
19882 77 68 69 63 68 20 69 73 20 74 68 65 20 62 65 73  which is the bes
19883 74 20 77 65 20 63 61 6e 20 64 6f 0a 2a 2a 20 61  t we can do.** a
19884 74 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74  t representing t
19885 68 65 20 76 61 6c 75 65 20 74 68 61 74 20 2a 70  he value that *p
19886 4d 65 6d 20 64 65 73 63 72 69 62 65 73 20 61 73  Mem describes as
19887 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20   an integer..** 
19888 49 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 69 6e  If pMem is an in
19889 74 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20  teger, then the 
1988a 76 61 6c 75 65 20 69 73 20 65 78 61 63 74 2e 20  value is exact. 
1988b 20 49 66 20 70 4d 65 6d 20 69 73 0a 2a 2a 20 61   If pMem is.** a
1988c 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20   floating-point 
1988d 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72  then the value r
1988e 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 69  eturned is the i
1988f 6e 74 65 67 65 72 20 70 61 72 74 2e 0a 2a 2a 20  nteger part..** 
19890 49 66 20 70 4d 65 6d 20 69 73 20 61 20 73 74 72  If pMem is a str
19891 69 6e 67 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65  ing or blob, the
19892 6e 20 77 65 20 6d 61 6b 65 20 61 6e 20 61 74 74  n we make an att
19893 65 6d 70 74 20 74 6f 20 63 6f 6e 76 65 72 74 0a  empt to convert.
19894 2a 2a 20 69 74 20 69 6e 74 6f 20 61 20 69 6e 74  ** it into a int
19895 65 67 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20  eger and return 
19896 74 68 61 74 2e 20 20 49 66 20 70 4d 65 6d 20 72  that.  If pMem r
19897 65 70 72 65 73 65 6e 74 73 20 61 6e 0a 2a 2a 20  epresents an.** 
19898 61 6e 20 53 51 4c 2d 4e 55 4c 4c 20 76 61 6c 75  an SQL-NULL valu
19899 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a  e, return 0..**.
1989a 2a 2a 20 49 66 20 70 4d 65 6d 20 72 65 70 72 65  ** If pMem repre
1989b 73 65 6e 74 73 20 61 20 73 74 72 69 6e 67 20 76  sents a string v
1989c 61 6c 75 65 2c 20 69 74 73 20 65 6e 63 6f 64 69  alue, its encodi
1989d 6e 67 20 6d 69 67 68 74 20 62 65 20 63 68 61 6e  ng might be chan
1989e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ged..*/.SQLITE_P
1989f 52 49 56 41 54 45 20 69 36 34 20 73 71 6c 69 74  RIVATE i64 sqlit
198a0 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 4d  e3VdbeIntValue(M
198a1 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74  em *pMem){.  int
198a2 20 66 6c 61 67 73 3b 0a 20 20 61 73 73 65 72 74   flags;.  assert
198a3 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pMem->db==0 ||
198a4 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
198a5 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75  eld(pMem->db->mu
198a6 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
198a7 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49  ( EIGHT_BYTE_ALI
198a8 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a  GNMENT(pMem) );.
198a9 20 20 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e    flags = pMem->
198aa 66 6c 61 67 73 3b 0a 20 20 69 66 28 20 66 6c 61  flags;.  if( fla
198ab 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a  gs & MEM_Int ){.
198ac 20 20 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d      return pMem-
198ad 3e 75 2e 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66  >u.i;.  }else if
198ae 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65  ( flags & MEM_Re
198af 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  al ){.    return
198b0 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 70   doubleToInt64(p
198b1 4d 65 6d 2d 3e 72 29 3b 0a 20 20 7d 65 6c 73 65  Mem->r);.  }else
198b2 20 69 66 28 20 66 6c 61 67 73 20 26 20 28 4d 45   if( flags & (ME
198b3 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20  M_Str|MEM_Blob) 
198b4 29 7b 0a 20 20 20 20 69 36 34 20 76 61 6c 75 65  ){.    i64 value
198b5 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  ;.    pMem->flag
198b6 73 20 7c 3d 20 4d 45 4d 5f 53 74 72 3b 0a 20 20  s |= MEM_Str;.  
198b7 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
198b8 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28  eChangeEncoding(
198b9 70 4d 65 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46  pMem, SQLITE_UTF
198ba 38 29 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c  8).       || sql
198bb 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65  ite3VdbeMemNulTe
198bc 72 6d 69 6e 61 74 65 28 70 4d 65 6d 29 20 29 7b  rminate(pMem) ){
198bd 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
198be 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
198bf 74 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20  t( pMem->z );.  
198c0 20 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28    sqlite3Atoi64(
198c1 70 4d 65 6d 2d 3e 7a 2c 20 26 76 61 6c 75 65 29  pMem->z, &value)
198c2 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c  ;.    return val
198c3 75 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ue;.  }else{.   
198c4 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d   return 0;.  }.}
198c5 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
198c6 68 65 20 62 65 73 74 20 72 65 70 72 65 73 65 6e  he best represen
198c7 74 61 74 69 6f 6e 20 6f 66 20 70 4d 65 6d 20 74  tation of pMem t
198c8 68 61 74 20 77 65 20 63 61 6e 20 67 65 74 20 69  hat we can get i
198c9 6e 74 6f 20 61 0a 2a 2a 20 64 6f 75 62 6c 65 2e  nto a.** double.
198ca 20 20 49 66 20 70 4d 65 6d 20 69 73 20 61 6c 72    If pMem is alr
198cb 65 61 64 79 20 61 20 64 6f 75 62 6c 65 20 6f 72  eady a double or
198cc 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 72 65 74   an integer, ret
198cd 75 72 6e 20 69 74 73 0a 2a 2a 20 76 61 6c 75 65  urn its.** value
198ce 2e 20 20 49 66 20 69 74 20 69 73 20 61 20 73 74  .  If it is a st
198cf 72 69 6e 67 20 6f 72 20 62 6c 6f 62 2c 20 74 72  ring or blob, tr
198d0 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20  y to convert it 
198d1 74 6f 20 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 20  to a double..** 
198d2 49 66 20 69 74 20 69 73 20 61 20 4e 55 4c 4c 2c  If it is a NULL,
198d3 20 72 65 74 75 72 6e 20 30 2e 30 2e 0a 2a 2f 0a   return 0.0..*/.
198d4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 64  SQLITE_PRIVATE d
198d5 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 56 64 62  ouble sqlite3Vdb
198d6 65 52 65 61 6c 56 61 6c 75 65 28 4d 65 6d 20 2a  eRealValue(Mem *
198d7 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28  pMem){.  assert(
198d8 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20   pMem->db==0 || 
198d9 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
198da 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74  ld(pMem->db->mut
198db 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
198dc 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47   EIGHT_BYTE_ALIG
198dd 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 20  NMENT(pMem) );. 
198de 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   if( pMem->flags
198df 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20   & MEM_Real ){. 
198e0 20 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e     return pMem->
198e1 72 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  r;.  }else if( p
198e2 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
198e3 5f 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75  _Int ){.    retu
198e4 72 6e 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d  rn (double)pMem-
198e5 3e 75 2e 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66  >u.i;.  }else if
198e6 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20  ( pMem->flags & 
198e7 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f  (MEM_Str|MEM_Blo
198e8 62 29 20 29 7b 0a 20 20 20 20 2f 2a 20 28 64 6f  b) ){.    /* (do
198e9 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f  uble)0 In case o
198ea 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
198eb 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20  OATING_POINT... 
198ec 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 76 61  */.    double va
198ed 6c 20 3d 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20  l = (double)0;. 
198ee 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c     pMem->flags |
198ef 3d 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20 69  = MEM_Str;.    i
198f0 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  f( sqlite3VdbeCh
198f1 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d 65  angeEncoding(pMe
198f2 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 0a  m, SQLITE_UTF8).
198f3 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65         || sqlite
198f4 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69  3VdbeMemNulTermi
198f5 6e 61 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20 20  nate(pMem) ){.  
198f6 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30      /* (double)0
198f7 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49   In case of SQLI
198f8 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
198f9 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20  _POINT... */.   
198fa 20 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62 6c     return (doubl
198fb 65 29 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  e)0;.    }.    a
198fc 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 20 29  ssert( pMem->z )
198fd 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f  ;.    sqlite3Ato
198fe 46 28 70 4d 65 6d 2d 3e 7a 2c 20 26 76 61 6c 29  F(pMem->z, &val)
198ff 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c  ;.    return val
19900 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
19901 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63  * (double)0 In c
19902 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d  ase of SQLITE_OM
19903 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
19904 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75  T... */.    retu
19905 72 6e 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20  rn (double)0;.  
19906 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 4d  }.}../*.** The M
19907 45 4d 20 73 74 72 75 63 74 75 72 65 20 69 73 20  EM structure is 
19908 61 6c 72 65 61 64 79 20 61 20 4d 45 4d 5f 52 65  already a MEM_Re
19909 61 6c 2e 20 20 54 72 79 20 74 6f 20 61 6c 73 6f  al.  Try to also
1990a 20 6d 61 6b 65 20 69 74 20 61 0a 2a 2a 20 4d 45   make it a.** ME
1990b 4d 5f 49 6e 74 20 69 66 20 77 65 20 63 61 6e 2e  M_Int if we can.
1990c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1990d 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1990e 64 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69  dbeIntegerAffini
1990f 74 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20  ty(Mem *pMem){. 
19910 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66   assert( pMem->f
19911 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20  lags & MEM_Real 
19912 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d  );.  assert( (pM
19913 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
19914 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20  RowSet)==0 );.  
19915 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62  assert( pMem->db
19916 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
19917 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e  utex_held(pMem->
19918 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
19919 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59  assert( EIGHT_BY
1991a 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65  TE_ALIGNMENT(pMe
1991b 6d 29 20 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 75  m) );..  pMem->u
1991c 2e 69 20 3d 20 64 6f 75 62 6c 65 54 6f 49 6e 74  .i = doubleToInt
1991d 36 34 28 70 4d 65 6d 2d 3e 72 29 3b 0a 0a 20 20  64(pMem->r);..  
1991e 2f 2a 20 4f 6e 6c 79 20 6d 61 72 6b 20 74 68 65  /* Only mark the
1991f 20 76 61 6c 75 65 20 61 73 20 61 6e 20 69 6e 74   value as an int
19920 65 67 65 72 20 69 66 0a 20 20 2a 2a 0a 20 20 2a  eger if.  **.  *
19921 2a 20 20 20 20 28 31 29 20 74 68 65 20 72 6f 75  *    (1) the rou
19922 6e 64 2d 74 72 69 70 20 63 6f 6e 76 65 72 73 69  nd-trip conversi
19923 6f 6e 20 72 65 61 6c 2d 3e 69 6e 74 2d 3e 72 65  on real->int->re
19924 61 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2c 20 61  al is a no-op, a
19925 6e 64 0a 20 20 2a 2a 20 20 20 20 28 32 29 20 54  nd.  **    (2) T
19926 68 65 20 69 6e 74 65 67 65 72 20 69 73 20 6e 65  he integer is ne
19927 69 74 68 65 72 20 74 68 65 20 6c 61 72 67 65 73  ither the larges
19928 74 20 6e 6f 72 20 74 68 65 20 73 6d 61 6c 6c 65  t nor the smalle
19929 73 74 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 70  st.  **        p
1992a 6f 73 73 69 62 6c 65 20 69 6e 74 65 67 65 72 20  ossible integer 
1992b 28 74 69 63 6b 65 74 20 23 33 39 32 32 29 0a 20  (ticket #3922). 
1992c 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 65 63   **.  ** The sec
1992d 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20 74 65  ond and third te
1992e 72 6d 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f  rms in the follo
1992f 77 69 6e 67 20 63 6f 6e 64 69 74 69 6f 6e 61 6c  wing conditional
19930 20 65 6e 66 6f 72 63 65 73 0a 20 20 2a 2a 20 74   enforces.  ** t
19931 68 65 20 73 65 63 6f 6e 64 20 63 6f 6e 64 69 74  he second condit
19932 69 6f 6e 20 75 6e 64 65 72 20 74 68 65 20 61 73  ion under the as
19933 73 75 6d 70 74 69 6f 6e 20 74 68 61 74 20 61 64  sumption that ad
19934 64 69 74 69 6f 6e 20 6f 76 65 72 66 6c 6f 77 20  dition overflow 
19935 63 61 75 73 65 73 0a 20 20 2a 2a 20 76 61 6c 75  causes.  ** valu
19936 65 73 20 74 6f 20 77 72 61 70 20 61 72 6f 75 6e  es to wrap aroun
19937 64 2e 20 20 4f 6e 20 78 38 36 20 68 61 72 64 77  d.  On x86 hardw
19938 61 72 65 2c 20 74 68 65 20 74 68 69 72 64 20 74  are, the third t
19939 65 72 6d 20 69 73 20 61 6c 77 61 79 73 0a 20 20  erm is always.  
1993a 2a 2a 20 74 72 75 65 20 61 6e 64 20 63 6f 75 6c  ** true and coul
1993b 64 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 42  d be omitted.  B
1993c 75 74 20 77 65 20 6c 65 61 76 65 20 69 74 20 69  ut we leave it i
1993d 6e 20 62 65 63 61 75 73 65 20 6f 74 68 65 72 0a  n because other.
1993e 20 20 2a 2a 20 61 72 63 68 69 74 65 63 74 75 72    ** architectur
1993f 65 73 20 6d 69 67 68 74 20 62 65 68 61 76 65 20  es might behave 
19940 64 69 66 66 65 72 65 6e 74 6c 79 2e 0a 20 20 2a  differently..  *
19941 2f 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 72 3d  /.  if( pMem->r=
19942 3d 28 64 6f 75 62 6c 65 29 70 4d 65 6d 2d 3e 75  =(double)pMem->u
19943 2e 69 20 26 26 20 70 4d 65 6d 2d 3e 75 2e 69 3e  .i && pMem->u.i>
19944 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 0a 20  SMALLEST_INT64. 
19945 20 20 20 20 20 26 26 20 41 4c 57 41 59 53 28 70       && ALWAYS(p
19946 4d 65 6d 2d 3e 75 2e 69 3c 4c 41 52 47 45 53 54  Mem->u.i<LARGEST
19947 5f 49 4e 54 36 34 29 20 29 7b 0a 20 20 20 20 70  _INT64) ){.    p
19948 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45  Mem->flags |= ME
19949 4d 5f 49 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  M_Int;.  }.}../*
1994a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70 4d 65 6d  .** Convert pMem
1994b 20 74 6f 20 74 79 70 65 20 69 6e 74 65 67 65 72   to type integer
1994c 2e 20 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6e  .  Invalidate an
1994d 79 20 70 72 69 6f 72 20 72 65 70 72 65 73 65 6e  y prior represen
1994e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49  tations..*/.SQLI
1994f 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
19950 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74  qlite3VdbeMemInt
19951 65 67 65 72 69 66 79 28 4d 65 6d 20 2a 70 4d 65  egerify(Mem *pMe
19952 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d  m){.  assert( pM
19953 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
19954 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
19955 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
19956 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   );.  assert( (p
19957 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
19958 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20  _RowSet)==0 );. 
19959 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42   assert( EIGHT_B
1995a 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d  YTE_ALIGNMENT(pM
1995b 65 6d 29 20 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e  em) );..  pMem->
1995c 75 2e 69 20 3d 20 73 71 6c 69 74 65 33 56 64 62  u.i = sqlite3Vdb
1995d 65 49 6e 74 56 61 6c 75 65 28 70 4d 65 6d 29 3b  eIntValue(pMem);
1995e 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  .  MemSetTypeFla
1995f 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 49 6e 74 29  g(pMem, MEM_Int)
19960 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
19961 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
19962 6f 6e 76 65 72 74 20 70 4d 65 6d 20 73 6f 20 74  onvert pMem so t
19963 68 61 74 20 69 74 20 69 73 20 6f 66 20 74 79 70  hat it is of typ
19964 65 20 4d 45 4d 5f 52 65 61 6c 2e 0a 2a 2a 20 49  e MEM_Real..** I
19965 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72  nvalidate any pr
19966 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69  ior representati
19967 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ons..*/.SQLITE_P
19968 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19969 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79  e3VdbeMemRealify
1996a 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61  (Mem *pMem){.  a
1996b 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d  ssert( pMem->db=
1996c 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  =0 || sqlite3_mu
1996d 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64  tex_held(pMem->d
1996e 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  b->mutex) );.  a
1996f 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54  ssert( EIGHT_BYT
19970 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d  E_ALIGNMENT(pMem
19971 29 20 29 3b 0a 0a 20 20 70 4d 65 6d 2d 3e 72 20  ) );..  pMem->r 
19972 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61  = sqlite3VdbeRea
19973 6c 56 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20  lValue(pMem);.  
19974 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
19975 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a  Mem, MEM_Real);.
19976 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
19977 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  OK;.}../*.** Con
19978 76 65 72 74 20 70 4d 65 6d 20 73 6f 20 74 68 61  vert pMem so tha
19979 74 20 69 74 20 68 61 73 20 74 79 70 65 73 20 4d  t it has types M
1997a 45 4d 5f 52 65 61 6c 20 6f 72 20 4d 45 4d 5f 49  EM_Real or MEM_I
1997b 6e 74 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 49  nt or both..** I
1997c 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72  nvalidate any pr
1997d 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69  ior representati
1997e 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ons..*/.SQLITE_P
1997f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19980 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66  e3VdbeMemNumerif
19981 79 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20  y(Mem *pMem){.  
19982 64 6f 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20  double r1, r2;. 
19983 20 69 36 34 20 69 3b 0a 20 20 61 73 73 65 72 74   i64 i;.  assert
19984 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  ( (pMem->flags &
19985 20 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65   (MEM_Int|MEM_Re
19986 61 6c 7c 4d 45 4d 5f 4e 75 6c 6c 29 29 3d 3d 30  al|MEM_Null))==0
19987 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   );.  assert( (p
19988 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45  Mem->flags & (ME
19989 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29  M_Blob|MEM_Str))
1998a 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
1998b 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20   pMem->db==0 || 
1998c 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1998d 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74  ld(pMem->db->mut
1998e 65 78 29 20 29 3b 0a 20 20 72 31 20 3d 20 73 71  ex) );.  r1 = sq
1998f 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c  lite3VdbeRealVal
19990 75 65 28 70 4d 65 6d 29 3b 0a 20 20 69 20 3d 20  ue(pMem);.  i = 
19991 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 72 31  doubleToInt64(r1
19992 29 3b 0a 20 20 72 32 20 3d 20 28 64 6f 75 62 6c  );.  r2 = (doubl
19993 65 29 69 3b 0a 20 20 69 66 28 20 72 31 3d 3d 72  e)i;.  if( r1==r
19994 32 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  2 ){.    sqlite3
19995 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66  VdbeMemIntegerif
19996 79 28 70 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65  y(pMem);.  }else
19997 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 72 20 3d 20  {.    pMem->r = 
19998 72 31 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79  r1;.    MemSetTy
19999 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d  peFlag(pMem, MEM
1999a 5f 52 65 61 6c 29 3b 0a 20 20 7d 0a 20 20 72 65  _Real);.  }.  re
1999b 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1999c 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
1999d 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c  any previous val
1999e 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 76  ue and set the v
1999f 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 2a  alue stored in *
199a0 70 4d 65 6d 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f  pMem to NULL..*/
199a1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
199a2 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
199a3 4d 65 6d 53 65 74 4e 75 6c 6c 28 4d 65 6d 20 2a  MemSetNull(Mem *
199a4 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 4d 65  pMem){.  if( pMe
199a5 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 46  m->flags & MEM_F
199a6 72 61 6d 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  rame ){.    sqli
199a7 74 65 33 56 64 62 65 46 72 61 6d 65 44 65 6c 65  te3VdbeFrameDele
199a8 74 65 28 70 4d 65 6d 2d 3e 75 2e 70 46 72 61 6d  te(pMem->u.pFram
199a9 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4d  e);.  }.  if( pM
199aa 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
199ab 52 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 73 71  RowSet ){.    sq
199ac 6c 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72  lite3RowSetClear
199ad 28 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74  (pMem->u.pRowSet
199ae 29 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54  );.  }.  MemSetT
199af 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45  ypeFlag(pMem, ME
199b0 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 70 4d 65 6d 2d  M_Null);.  pMem-
199b1 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e  >type = SQLITE_N
199b2 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  ULL;.}../*.** De
199b3 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75  lete any previou
199b4 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20  s value and set 
199b5 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20  the value to be 
199b6 61 20 42 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68  a BLOB of length
199b7 0a 2a 2a 20 6e 20 63 6f 6e 74 61 69 6e 69 6e 67  .** n containing
199b8 20 61 6c 6c 20 7a 65 72 6f 73 2e 0a 2a 2f 0a 53   all zeros..*/.S
199b9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
199ba 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
199bb 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d  mSetZeroBlob(Mem
199bc 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 29 7b 0a   *pMem, int n){.
199bd 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
199be 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20  Release(pMem);. 
199bf 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
199c0 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f  EM_Blob|MEM_Zero
199c1 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d  ;.  pMem->type =
199c2 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20   SQLITE_BLOB;.  
199c3 70 4d 65 6d 2d 3e 6e 20 3d 20 30 3b 0a 20 20 69  pMem->n = 0;.  i
199c4 66 28 20 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a  f( n<0 ) n = 0;.
199c5 20 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 20    pMem->u.nZero 
199c6 3d 20 6e 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63  = n;.  pMem->enc
199c7 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a   = SQLITE_UTF8;.
199c8 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
199c9 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 73  MIT_INCRBLOB.  s
199ca 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
199cb 77 28 70 4d 65 6d 2c 20 6e 2c 20 30 29 3b 0a 20  w(pMem, n, 0);. 
199cc 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 29 7b 0a   if( pMem->z ){.
199cd 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e 3b      pMem->n = n;
199ce 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 4d 65 6d  .    memset(pMem
199cf 2d 3e 7a 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a  ->z, 0, n);.  }.
199d0 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
199d1 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69  Delete any previ
199d2 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65  ous value and se
199d3 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72  t the value stor
199d4 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20 76  ed in *pMem to v
199d5 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73 74 20  al,.** manifest 
199d6 74 79 70 65 20 49 4e 54 45 47 45 52 2e 0a 2a 2f  type INTEGER..*/
199d7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
199d8 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
199d9 4d 65 6d 53 65 74 49 6e 74 36 34 28 4d 65 6d 20  MemSetInt64(Mem 
199da 2a 70 4d 65 6d 2c 20 69 36 34 20 76 61 6c 29 7b  *pMem, i64 val){
199db 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
199dc 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a  mRelease(pMem);.
199dd 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 76 61    pMem->u.i = va
199de 6c 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  l;.  pMem->flags
199df 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4d   = MEM_Int;.  pM
199e0 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  em->type = SQLIT
199e1 45 5f 49 4e 54 45 47 45 52 3b 0a 7d 0a 0a 2f 2a  E_INTEGER;.}../*
199e2 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70  .** Delete any p
199e3 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e  revious value an
199e4 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20  d set the value 
199e5 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20  stored in *pMem 
199e6 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66  to val,.** manif
199e7 65 73 74 20 74 79 70 65 20 52 45 41 4c 2e 0a 2a  est type REAL..*
199e8 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
199e9 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
199ea 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 4d 65  eMemSetDouble(Me
199eb 6d 20 2a 70 4d 65 6d 2c 20 64 6f 75 62 6c 65 20  m *pMem, double 
199ec 76 61 6c 29 7b 0a 20 20 69 66 28 20 73 71 6c 69  val){.  if( sqli
199ed 74 65 33 49 73 4e 61 4e 28 76 61 6c 29 20 29 7b  te3IsNaN(val) ){
199ee 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
199ef 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29  MemSetNull(pMem)
199f0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
199f1 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
199f2 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20  ease(pMem);.    
199f3 70 4d 65 6d 2d 3e 72 20 3d 20 76 61 6c 3b 0a 20  pMem->r = val;. 
199f4 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
199f5 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 20 20 70   MEM_Real;.    p
199f6 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
199f7 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a 7d 0a  TE_FLOAT;.  }.}.
199f8 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e  ./*.** Delete an
199f9 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65  y previous value
199fa 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c   and set the val
199fb 75 65 20 6f 66 20 70 4d 65 6d 20 74 6f 20 62 65  ue of pMem to be
199fc 20 61 6e 0a 2a 2a 20 65 6d 70 74 79 20 62 6f 6f   an.** empty boo
199fd 6c 65 61 6e 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53  lean index..*/.S
199fe 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
199ff 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
19a00 6d 53 65 74 52 6f 77 53 65 74 28 4d 65 6d 20 2a  mSetRowSet(Mem *
19a01 70 4d 65 6d 29 7b 0a 20 20 73 71 6c 69 74 65 33  pMem){.  sqlite3
19a02 20 2a 64 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b   *db = pMem->db;
19a03 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30  .  assert( db!=0
19a04 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   );.  assert( (p
19a05 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
19a06 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20  _RowSet)==0 );. 
19a07 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
19a08 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20  elease(pMem);.  
19a09 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20  pMem->zMalloc = 
19a0a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
19a0b 61 77 28 64 62 2c 20 36 34 29 3b 0a 20 20 69 66  aw(db, 64);.  if
19a0c 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
19a0d 65 64 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e  ed ){.    pMem->
19a0e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
19a0f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
19a10 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 4d 61  ssert( pMem->zMa
19a11 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 70 4d 65 6d  lloc );.    pMem
19a12 2d 3e 75 2e 70 52 6f 77 53 65 74 20 3d 20 73 71  ->u.pRowSet = sq
19a13 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 69 74 28  lite3RowSetInit(
19a14 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f  db, pMem->zMallo
19a15 63 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  c, .            
19a16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19a17 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
19a18 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64  e3DbMallocSize(d
19a19 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63  b, pMem->zMalloc
19a1a 29 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ));.    assert( 
19a1b 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77 53 65 74 21  pMem->u.pRowSet!
19a1c 3d 30 20 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e  =0 );.    pMem->
19a1d 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 6f 77 53  flags = MEM_RowS
19a1e 65 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  et;.  }.}../*.**
19a1f 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
19a20 74 68 65 20 4d 65 6d 20 6f 62 6a 65 63 74 20 63  the Mem object c
19a21 6f 6e 74 61 69 6e 73 20 61 20 54 45 58 54 20 6f  ontains a TEXT o
19a22 72 20 42 4c 4f 42 20 74 68 61 74 20 69 73 0a 2a  r BLOB that is.*
19a23 2a 20 74 6f 6f 20 6c 61 72 67 65 20 2d 20 77 68  * too large - wh
19a24 6f 73 65 20 73 69 7a 65 20 65 78 63 65 65 64 73  ose size exceeds
19a25 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47   SQLITE_MAX_LENG
19a26 54 48 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  TH..*/.SQLITE_PR
19a27 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
19a28 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 4d  3VdbeMemTooBig(M
19a29 65 6d 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  em *p){.  assert
19a2a 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a 20 20  ( p->db!=0 );.  
19a2b 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 28  if( p->flags & (
19a2c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62  MEM_Str|MEM_Blob
19a2d 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  ) ){.    int n =
19a2e 20 70 2d 3e 6e 3b 0a 20 20 20 20 69 66 28 20 70   p->n;.    if( p
19a2f 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65  ->flags & MEM_Ze
19a30 72 6f 20 29 7b 0a 20 20 20 20 20 20 6e 20 2b 3d  ro ){.      n +=
19a31 20 70 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20   p->u.nZero;.   
19a32 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 3e   }.    return n>
19a33 70 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  p->db->aLimit[SQ
19a34 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
19a35 48 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  H];.  }.  return
19a36 20 30 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69   0; .}../*.** Si
19a37 7a 65 20 6f 66 20 73 74 72 75 63 74 20 4d 65 6d  ze of struct Mem
19a38 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 20 74   not including t
19a39 68 65 20 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 6d  he Mem.zMalloc m
19a3a 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ember..*/.#defin
19a3b 65 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 20 28 73  e MEMCELLSIZE (s
19a3c 69 7a 65 5f 74 29 28 26 28 28 28 4d 65 6d 20 2a  ize_t)(&(((Mem *
19a3d 29 30 29 2d 3e 7a 4d 61 6c 6c 6f 63 29 29 0a 0a  )0)->zMalloc))..
19a3e 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 6e 20 73 68  /*.** Make an sh
19a3f 61 6c 6c 6f 77 20 63 6f 70 79 20 6f 66 20 70 46  allow copy of pF
19a40 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e 20 20 50  rom into pTo.  P
19a41 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73 20 6f 66  rior contents of
19a42 0a 2a 2a 20 70 54 6f 20 61 72 65 20 66 72 65 65  .** pTo are free
19a43 64 2e 20 20 54 68 65 20 70 46 72 6f 6d 2d 3e 7a  d.  The pFrom->z
19a44 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 20 64 75   field is not du
19a45 70 6c 69 63 61 74 65 64 2e 20 20 49 66 0a 2a 2a  plicated.  If.**
19a46 20 70 46 72 6f 6d 2d 3e 7a 20 69 73 20 75 73 65   pFrom->z is use
19a47 64 2c 20 74 68 65 6e 20 70 54 6f 2d 3e 7a 20 70  d, then pTo->z p
19a48 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 61 6d  oints to the sam
19a49 65 20 74 68 69 6e 67 20 61 73 20 70 46 72 6f 6d  e thing as pFrom
19a4a 2d 3e 7a 0a 2a 2a 20 61 6e 64 20 66 6c 61 67 73  ->z.** and flags
19a4b 20 67 65 74 73 20 73 72 63 54 79 70 65 20 28 65   gets srcType (e
19a4c 69 74 68 65 72 20 4d 45 4d 5f 45 70 68 65 6d 20  ither MEM_Ephem 
19a4d 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63 29 2e 0a  or MEM_Static)..
19a4e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19a4f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
19a50 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79  beMemShallowCopy
19a51 28 4d 65 6d 20 2a 70 54 6f 2c 20 63 6f 6e 73 74  (Mem *pTo, const
19a52 20 4d 65 6d 20 2a 70 46 72 6f 6d 2c 20 69 6e 74   Mem *pFrom, int
19a53 20 73 72 63 54 79 70 65 29 7b 0a 20 20 61 73 73   srcType){.  ass
19a54 65 72 74 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61  ert( (pFrom->fla
19a55 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29  gs & MEM_RowSet)
19a56 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ==0 );.  sqlite3
19a57 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78  VdbeMemReleaseEx
19a58 74 65 72 6e 61 6c 28 70 54 6f 29 3b 0a 20 20 6d  ternal(pTo);.  m
19a59 65 6d 63 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d  emcpy(pTo, pFrom
19a5a 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a  , MEMCELLSIZE);.
19a5b 20 20 70 54 6f 2d 3e 78 44 65 6c 20 3d 20 30 3b    pTo->xDel = 0;
19a5c 0a 20 20 69 66 28 20 28 70 46 72 6f 6d 2d 3e 66  .  if( (pFrom->f
19a5d 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 21 3d 30  lags&MEM_Dyn)!=0
19a5e 20 7c 7c 20 70 46 72 6f 6d 2d 3e 7a 3d 3d 70 46   || pFrom->z==pF
19a5f 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a  rom->zMalloc ){.
19a60 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 26      pTo->flags &
19a61 3d 20 7e 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f  = ~(MEM_Dyn|MEM_
19a62 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d  Static|MEM_Ephem
19a63 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
19a64 72 63 54 79 70 65 3d 3d 4d 45 4d 5f 45 70 68 65  rcType==MEM_Ephe
19a65 6d 20 7c 7c 20 73 72 63 54 79 70 65 3d 3d 4d 45  m || srcType==ME
19a66 4d 5f 53 74 61 74 69 63 20 29 3b 0a 20 20 20 20  M_Static );.    
19a67 70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d 20 73 72  pTo->flags |= sr
19a68 63 54 79 70 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  cType;.  }.}../*
19a69 0a 2a 2a 20 4d 61 6b 65 20 61 20 66 75 6c 6c 20  .** Make a full 
19a6a 63 6f 70 79 20 6f 66 20 70 46 72 6f 6d 20 69 6e  copy of pFrom in
19a6b 74 6f 20 70 54 6f 2e 20 20 50 72 69 6f 72 20 63  to pTo.  Prior c
19a6c 6f 6e 74 65 6e 74 73 20 6f 66 20 70 54 6f 20 61  ontents of pTo a
19a6d 72 65 0a 2a 2a 20 66 72 65 65 64 20 62 65 66 6f  re.** freed befo
19a6e 72 65 20 74 68 65 20 63 6f 70 79 20 69 73 20 6d  re the copy is m
19a6f 61 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ade..*/.SQLITE_P
19a70 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19a71 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 4d 65  e3VdbeMemCopy(Me
19a72 6d 20 2a 70 54 6f 2c 20 63 6f 6e 73 74 20 4d 65  m *pTo, const Me
19a73 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 6e 74  m *pFrom){.  int
19a74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
19a75 0a 0a 20 20 61 73 73 65 72 74 28 20 28 70 46 72  ..  assert( (pFr
19a76 6f 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  om->flags & MEM_
19a77 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20  RowSet)==0 );.  
19a78 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
19a79 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 54  leaseExternal(pT
19a7a 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f  o);.  memcpy(pTo
19a7b 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c  , pFrom, MEMCELL
19a7c 53 49 5a 45 29 3b 0a 20 20 70 54 6f 2d 3e 66 6c  SIZE);.  pTo->fl
19a7d 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b  ags &= ~MEM_Dyn;
19a7e 0a 0a 20 20 69 66 28 20 70 54 6f 2d 3e 66 6c 61  ..  if( pTo->fla
19a7f 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f  gs&(MEM_Str|MEM_
19a80 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69 66 28  Blob) ){.    if(
19a81 20 30 3d 3d 28 70 46 72 6f 6d 2d 3e 66 6c 61 67   0==(pFrom->flag
19a82 73 26 4d 45 4d 5f 53 74 61 74 69 63 29 20 29 7b  s&MEM_Static) ){
19a83 0a 20 20 20 20 20 20 70 54 6f 2d 3e 66 6c 61 67  .      pTo->flag
19a84 73 20 7c 3d 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a  s |= MEM_Ephem;.
19a85 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
19a86 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69  e3VdbeMemMakeWri
19a87 74 65 61 62 6c 65 28 70 54 6f 29 3b 0a 20 20 20  teable(pTo);.   
19a88 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
19a89 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72   rc;.}../*.** Tr
19a8a 61 6e 73 66 65 72 20 74 68 65 20 63 6f 6e 74 65  ansfer the conte
19a8b 6e 74 73 20 6f 66 20 70 46 72 6f 6d 20 74 6f 20  nts of pFrom to 
19a8c 70 54 6f 2e 20 41 6e 79 20 65 78 69 73 74 69 6e  pTo. Any existin
19a8d 67 20 76 61 6c 75 65 20 69 6e 20 70 54 6f 20 69  g value in pTo i
19a8e 73 0a 2a 2a 20 66 72 65 65 64 2e 20 49 66 20 70  s.** freed. If p
19a8f 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20 65 70  From contains ep
19a90 68 65 6d 65 72 61 6c 20 64 61 74 61 2c 20 61 20  hemeral data, a 
19a91 63 6f 70 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2a  copy is made..**
19a92 0a 2a 2a 20 70 46 72 6f 6d 20 63 6f 6e 74 61 69  .** pFrom contai
19a93 6e 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 77  ns an SQL NULL w
19a94 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
19a95 20 72 65 74 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c   returns..*/.SQL
19a96 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
19a97 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
19a98 6f 76 65 28 4d 65 6d 20 2a 70 54 6f 2c 20 4d 65  ove(Mem *pTo, Me
19a99 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 61 73 73  m *pFrom){.  ass
19a9a 65 72 74 28 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d  ert( pFrom->db==
19a9b 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
19a9c 65 78 5f 68 65 6c 64 28 70 46 72 6f 6d 2d 3e 64  ex_held(pFrom->d
19a9d 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  b->mutex) );.  a
19a9e 73 73 65 72 74 28 20 70 54 6f 2d 3e 64 62 3d 3d  ssert( pTo->db==
19a9f 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
19aa0 65 78 5f 68 65 6c 64 28 70 54 6f 2d 3e 64 62 2d  ex_held(pTo->db-
19aa1 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
19aa2 65 72 74 28 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d  ert( pFrom->db==
19aa3 30 20 7c 7c 20 70 54 6f 2d 3e 64 62 3d 3d 30 20  0 || pTo->db==0 
19aa4 7c 7c 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 70 54  || pFrom->db==pT
19aa5 6f 2d 3e 64 62 20 29 3b 0a 0a 20 20 73 71 6c 69  o->db );..  sqli
19aa6 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
19aa7 65 28 70 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79  e(pTo);.  memcpy
19aa8 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 73 69 7a  (pTo, pFrom, siz
19aa9 65 6f 66 28 4d 65 6d 29 29 3b 0a 20 20 70 46 72  eof(Mem));.  pFr
19aaa 6f 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  om->flags = MEM_
19aab 4e 75 6c 6c 3b 0a 20 20 70 46 72 6f 6d 2d 3e 78  Null;.  pFrom->x
19aac 44 65 6c 20 3d 20 30 3b 0a 20 20 70 46 72 6f 6d  Del = 0;.  pFrom
19aad 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 7d  ->zMalloc = 0;.}
19aae 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
19aaf 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 4d 65  he value of a Me
19ab0 6d 20 74 6f 20 62 65 20 61 20 73 74 72 69 6e 67  m to be a string
19ab1 20 6f 72 20 61 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a   or a BLOB..**.*
19ab2 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 6d 61 6e  * The memory man
19ab3 61 67 65 6d 65 6e 74 20 73 74 72 61 74 65 67 79  agement strategy
19ab4 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20   depends on the 
19ab5 76 61 6c 75 65 20 6f 66 20 74 68 65 20 78 44 65  value of the xDe
19ab6 6c 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 2e 20  l.** parameter. 
19ab7 49 66 20 74 68 65 20 76 61 6c 75 65 20 70 61 73  If the value pas
19ab8 73 65 64 20 69 73 20 53 51 4c 49 54 45 5f 54 52  sed is SQLITE_TR
19ab9 41 4e 53 49 45 4e 54 2c 20 74 68 65 6e 20 74 68  ANSIENT, then th
19aba 65 20 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20  e .** string is 
19abb 63 6f 70 69 65 64 20 69 6e 74 6f 20 61 20 28 70  copied into a (p
19abc 6f 73 73 69 62 6c 79 20 65 78 69 73 74 69 6e 67  ossibly existing
19abd 29 20 62 75 66 66 65 72 20 6d 61 6e 61 67 65 64  ) buffer managed
19abe 20 62 79 20 74 68 65 20 0a 2a 2a 20 4d 65 6d 20   by the .** Mem 
19abf 73 74 72 75 63 74 75 72 65 2e 20 4f 74 68 65 72  structure. Other
19ac0 77 69 73 65 2c 20 61 6e 79 20 65 78 69 73 74 69  wise, any existi
19ac1 6e 67 20 62 75 66 66 65 72 20 69 73 20 66 72 65  ng buffer is fre
19ac2 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 70 6f  ed and the.** po
19ac3 69 6e 74 65 72 20 63 6f 70 69 65 64 2e 0a 2a 2a  inter copied..**
19ac4 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 72 69 6e  .** If the strin
19ac5 67 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 28  g is too large (
19ac6 69 66 20 69 74 20 65 78 63 65 65 64 73 20 74 68  if it exceeds th
19ac7 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  e SQLITE_LIMIT_L
19ac8 45 4e 47 54 48 0a 2a 2a 20 73 69 7a 65 20 6c 69  ENGTH.** size li
19ac9 6d 69 74 29 20 74 68 65 6e 20 6e 6f 20 6d 65 6d  mit) then no mem
19aca 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f  ory allocation o
19acb 63 63 75 72 73 2e 20 20 49 66 20 74 68 65 20 73  ccurs.  If the s
19acc 74 72 69 6e 67 20 63 61 6e 20 62 65 0a 2a 2a 20  tring can be.** 
19acd 73 74 6f 72 65 64 20 77 69 74 68 6f 75 74 20 61  stored without a
19ace 6c 6c 6f 63 61 74 69 6e 67 20 6d 65 6d 6f 72 79  llocating memory
19acf 2c 20 74 68 65 6e 20 69 74 20 69 73 2e 20 20 49  , then it is.  I
19ad0 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  f a memory alloc
19ad1 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 72 65 71 75  ation.** is requ
19ad2 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68  ired to store th
19ad3 65 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 76  e string, then v
19ad4 61 6c 75 65 20 6f 66 20 70 4d 65 6d 20 69 73 20  alue of pMem is 
19ad5 75 6e 63 68 61 6e 67 65 64 2e 20 20 49 6e 0a 2a  unchanged.  In.*
19ad6 2a 20 65 69 74 68 65 72 20 63 61 73 65 2c 20 53  * either case, S
19ad7 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 69 73 20  QLITE_TOOBIG is 
19ad8 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  returned..*/.SQL
19ad9 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
19ada 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
19adb 74 53 74 72 28 0a 20 20 4d 65 6d 20 2a 70 4d 65  tStr(.  Mem *pMe
19adc 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  m,          /* M
19add 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 73 65  emory cell to se
19ade 74 20 74 6f 20 73 74 72 69 6e 67 20 76 61 6c 75  t to string valu
19adf 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
19ae0 72 20 2a 7a 2c 20 20 20 20 20 20 2f 2a 20 53 74  r *z,      /* St
19ae1 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a  ring pointer */.
19ae2 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20    int n,        
19ae3 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 69        /* Bytes i
19ae4 6e 20 73 74 72 69 6e 67 2c 20 6f 72 20 6e 65 67  n string, or neg
19ae5 61 74 69 76 65 20 2a 2f 0a 20 20 75 38 20 65 6e  ative */.  u8 en
19ae6 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  c,             /
19ae7 2a 20 45 6e 63 6f 64 69 6e 67 20 6f 66 20 7a 2e  * Encoding of z.
19ae8 20 20 30 20 66 6f 72 20 42 4c 4f 42 73 20 2a 2f    0 for BLOBs */
19ae9 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28  .  void (*xDel)(
19aea 76 6f 69 64 2a 29 20 2f 2a 20 44 65 73 74 72 75  void*) /* Destru
19aeb 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  ctor function */
19aec 0a 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20  .){.  int nByte 
19aed 3d 20 6e 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77  = n;      /* New
19aee 20 76 61 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d   value for pMem-
19aef 3e 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 6d  >n */.  int iLim
19af0 69 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  it;         /* M
19af1 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73  aximum allowed s
19af2 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 73 69  tring or blob si
19af3 7a 65 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61 67  ze */.  u16 flag
19af4 73 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e  s = 0;      /* N
19af5 65 77 20 76 61 6c 75 65 20 66 6f 72 20 70 4d 65  ew value for pMe
19af6 6d 2d 3e 66 6c 61 67 73 20 2a 2f 0a 0a 20 20 61  m->flags */..  a
19af7 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d  ssert( pMem->db=
19af8 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  =0 || sqlite3_mu
19af9 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64  tex_held(pMem->d
19afa 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  b->mutex) );.  a
19afb 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c  ssert( (pMem->fl
19afc 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74  ags & MEM_RowSet
19afd 29 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  )==0 );..  /* If
19afe 20 7a 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69   z is a NULL poi
19aff 6e 74 65 72 2c 20 73 65 74 20 70 4d 65 6d 20 74  nter, set pMem t
19b00 6f 20 63 6f 6e 74 61 69 6e 20 61 6e 20 53 51 4c  o contain an SQL
19b01 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28 20   NULL. */.  if( 
19b02 21 7a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  !z ){.    sqlite
19b03 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28  3VdbeMemSetNull(
19b04 70 4d 65 6d 29 3b 0a 20 20 20 20 72 65 74 75 72  pMem);.    retur
19b05 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
19b06 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 64 62  ..  if( pMem->db
19b07 20 29 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d   ){.    iLimit =
19b08 20 70 4d 65 6d 2d 3e 64 62 2d 3e 61 4c 69 6d 69   pMem->db->aLimi
19b09 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
19b0a 45 4e 47 54 48 5d 3b 0a 20 20 7d 65 6c 73 65 7b  ENGTH];.  }else{
19b0b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 53 51  .    iLimit = SQ
19b0c 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 3b  LITE_MAX_LENGTH;
19b0d 0a 20 20 7d 0a 20 20 66 6c 61 67 73 20 3d 20 28  .  }.  flags = (
19b0e 65 6e 63 3d 3d 30 3f 4d 45 4d 5f 42 6c 6f 62 3a  enc==0?MEM_Blob:
19b0f 4d 45 4d 5f 53 74 72 29 3b 0a 20 20 69 66 28 20  MEM_Str);.  if( 
19b10 6e 42 79 74 65 3c 30 20 29 7b 0a 20 20 20 20 61  nByte<0 ){.    a
19b11 73 73 65 72 74 28 20 65 6e 63 21 3d 30 20 29 3b  ssert( enc!=0 );
19b12 0a 20 20 20 20 69 66 28 20 65 6e 63 3d 3d 53 51  .    if( enc==SQ
19b13 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20  LITE_UTF8 ){.   
19b14 20 20 20 66 6f 72 28 6e 42 79 74 65 3d 30 3b 20     for(nByte=0; 
19b15 6e 42 79 74 65 3c 3d 69 4c 69 6d 69 74 20 26 26  nByte<=iLimit &&
19b16 20 7a 5b 6e 42 79 74 65 5d 3b 20 6e 42 79 74 65   z[nByte]; nByte
19b17 2b 2b 29 7b 7d 0a 20 20 20 20 7d 65 6c 73 65 7b  ++){}.    }else{
19b18 0a 20 20 20 20 20 20 66 6f 72 28 6e 42 79 74 65  .      for(nByte
19b19 3d 30 3b 20 6e 42 79 74 65 3c 3d 69 4c 69 6d 69  =0; nByte<=iLimi
19b1a 74 20 26 26 20 28 7a 5b 6e 42 79 74 65 5d 20 7c  t && (z[nByte] |
19b1b 20 7a 5b 6e 42 79 74 65 2b 31 5d 29 3b 20 6e 42   z[nByte+1]); nB
19b1c 79 74 65 2b 3d 32 29 7b 7d 0a 20 20 20 20 7d 0a  yte+=2){}.    }.
19b1d 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 4d 45 4d      flags |= MEM
19b1e 5f 54 65 72 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  _Term;.  }..  /*
19b1f 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62   The following b
19b20 6c 6f 63 6b 20 73 65 74 73 20 74 68 65 20 6e 65  lock sets the ne
19b21 77 20 76 61 6c 75 65 73 20 6f 66 20 4d 65 6d 2e  w values of Mem.
19b22 7a 20 61 6e 64 20 4d 65 6d 2e 78 44 65 6c 2e 20  z and Mem.xDel. 
19b23 49 74 0a 20 20 2a 2a 20 61 6c 73 6f 20 73 65 74  It.  ** also set
19b24 73 20 61 20 66 6c 61 67 20 69 6e 20 6c 6f 63 61  s a flag in loca
19b25 6c 20 76 61 72 69 61 62 6c 65 20 22 66 6c 61 67  l variable "flag
19b26 73 22 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74  s" to indicate t
19b27 68 65 20 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 6d  he memory.  ** m
19b28 61 6e 61 67 65 6d 65 6e 74 20 28 6f 6e 65 20 6f  anagement (one o
19b29 66 20 4d 45 4d 5f 44 79 6e 20 6f 72 20 4d 45 4d  f MEM_Dyn or MEM
19b2a 5f 53 74 61 74 69 63 29 2e 0a 20 20 2a 2f 0a 20  _Static)..  */. 
19b2b 20 69 66 28 20 78 44 65 6c 3d 3d 53 51 4c 49 54   if( xDel==SQLIT
19b2c 45 5f 54 52 41 4e 53 49 45 4e 54 20 29 7b 0a 20  E_TRANSIENT ){. 
19b2d 20 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20     int nAlloc = 
19b2e 6e 42 79 74 65 3b 0a 20 20 20 20 69 66 28 20 66  nByte;.    if( f
19b2f 6c 61 67 73 26 4d 45 4d 5f 54 65 72 6d 20 29 7b  lags&MEM_Term ){
19b30 0a 20 20 20 20 20 20 6e 41 6c 6c 6f 63 20 2b 3d  .      nAlloc +=
19b31 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54   (enc==SQLITE_UT
19b32 46 38 3f 31 3a 32 29 3b 0a 20 20 20 20 7d 0a 20  F8?1:2);.    }. 
19b33 20 20 20 69 66 28 20 6e 42 79 74 65 3e 69 4c 69     if( nByte>iLi
19b34 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74  mit ){.      ret
19b35 75 72 6e 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49  urn SQLITE_TOOBI
19b36 47 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  G;.    }.    if(
19b37 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47   sqlite3VdbeMemG
19b38 72 6f 77 28 70 4d 65 6d 2c 20 6e 41 6c 6c 6f 63  row(pMem, nAlloc
19b39 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 72 65  , 0) ){.      re
19b3a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
19b3b 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d  M;.    }.    mem
19b3c 63 70 79 28 70 4d 65 6d 2d 3e 7a 2c 20 7a 2c 20  cpy(pMem->z, z, 
19b3d 6e 41 6c 6c 6f 63 29 3b 0a 20 20 7d 65 6c 73 65  nAlloc);.  }else
19b3e 20 69 66 28 20 78 44 65 6c 3d 3d 53 51 4c 49 54   if( xDel==SQLIT
19b3f 45 5f 44 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20  E_DYNAMIC ){.   
19b40 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
19b41 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20  elease(pMem);.  
19b42 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20    pMem->zMalloc 
19b43 3d 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61  = pMem->z = (cha
19b44 72 20 2a 29 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d  r *)z;.    pMem-
19b45 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 7d 65 6c  >xDel = 0;.  }el
19b46 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  se{.    sqlite3V
19b47 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d  dbeMemRelease(pM
19b48 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a  em);.    pMem->z
19b49 20 3d 20 28 63 68 61 72 20 2a 29 7a 3b 0a 20 20   = (char *)z;.  
19b4a 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 78    pMem->xDel = x
19b4b 44 65 6c 3b 0a 20 20 20 20 66 6c 61 67 73 20 7c  Del;.    flags |
19b4c 3d 20 28 28 78 44 65 6c 3d 3d 53 51 4c 49 54 45  = ((xDel==SQLITE
19b4d 5f 53 54 41 54 49 43 29 3f 4d 45 4d 5f 53 74 61  _STATIC)?MEM_Sta
19b4e 74 69 63 3a 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20  tic:MEM_Dyn);.  
19b4f 7d 0a 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6e  }..  pMem->n = n
19b50 42 79 74 65 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c  Byte;.  pMem->fl
19b51 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 70  ags = flags;.  p
19b52 4d 65 6d 2d 3e 65 6e 63 20 3d 20 28 65 6e 63 3d  Mem->enc = (enc=
19b53 3d 30 20 3f 20 53 51 4c 49 54 45 5f 55 54 46 38  =0 ? SQLITE_UTF8
19b54 20 3a 20 65 6e 63 29 3b 0a 20 20 70 4d 65 6d 2d   : enc);.  pMem-
19b55 3e 74 79 70 65 20 3d 20 28 65 6e 63 3d 3d 30 20  >type = (enc==0 
19b56 3f 20 53 51 4c 49 54 45 5f 42 4c 4f 42 20 3a 20  ? SQLITE_BLOB : 
19b57 53 51 4c 49 54 45 5f 54 45 58 54 29 3b 0a 0a 23  SQLITE_TEXT);..#
19b58 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
19b59 49 54 5f 55 54 46 31 36 0a 20 20 69 66 28 20 70  IT_UTF16.  if( p
19b5a 4d 65 6d 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45  Mem->enc!=SQLITE
19b5b 5f 55 54 46 38 20 26 26 20 73 71 6c 69 74 65 33  _UTF8 && sqlite3
19b5c 56 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d  VdbeMemHandleBom
19b5d 28 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 72 65  (pMem) ){.    re
19b5e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
19b5f 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  M;.  }.#endif.. 
19b60 20 69 66 28 20 6e 42 79 74 65 3e 69 4c 69 6d 69   if( nByte>iLimi
19b61 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
19b62 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20  SQLITE_TOOBIG;. 
19b63 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c   }..  return SQL
19b64 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
19b65 20 43 6f 6d 70 61 72 65 20 74 68 65 20 76 61 6c   Compare the val
19b66 75 65 73 20 63 6f 6e 74 61 69 6e 65 64 20 62 79  ues contained by
19b67 20 74 68 65 20 74 77 6f 20 6d 65 6d 6f 72 79 20   the two memory 
19b68 63 65 6c 6c 73 2c 20 72 65 74 75 72 6e 69 6e 67  cells, returning
19b69 0a 2a 2a 20 6e 65 67 61 74 69 76 65 2c 20 7a 65  .** negative, ze
19b6a 72 6f 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69  ro or positive i
19b6b 66 20 70 4d 65 6d 31 20 69 73 20 6c 65 73 73 20  f pMem1 is less 
19b6c 74 68 61 6e 2c 20 65 71 75 61 6c 20 74 6f 2c 20  than, equal to, 
19b6d 6f 72 20 67 72 65 61 74 65 72 0a 2a 2a 20 74 68  or greater.** th
19b6e 61 6e 20 70 4d 65 6d 32 2e 20 53 6f 72 74 69 6e  an pMem2. Sortin
19b6f 67 20 6f 72 64 65 72 20 69 73 20 4e 55 4c 4c 27  g order is NULL'
19b70 73 20 66 69 72 73 74 2c 20 66 6f 6c 6c 6f 77 65  s first, followe
19b71 64 20 62 79 20 6e 75 6d 62 65 72 73 20 28 69 6e  d by numbers (in
19b72 74 65 67 65 72 73 0a 2a 2a 20 61 6e 64 20 72 65  tegers.** and re
19b73 61 6c 73 29 20 73 6f 72 74 65 64 20 6e 75 6d 65  als) sorted nume
19b74 72 69 63 61 6c 6c 79 2c 20 66 6f 6c 6c 6f 77 65  rically, followe
19b75 64 20 62 79 20 74 65 78 74 20 6f 72 64 65 72 65  d by text ordere
19b76 64 20 62 79 20 74 68 65 20 63 6f 6c 6c 61 74 69  d by the collati
19b77 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 70  ng.** sequence p
19b78 43 6f 6c 6c 20 61 6e 64 20 66 69 6e 61 6c 6c 79  Coll and finally
19b79 20 62 6c 6f 62 27 73 20 6f 72 64 65 72 65 64 20   blob's ordered 
19b7a 62 79 20 6d 65 6d 63 6d 70 28 29 2e 0a 2a 2a 0a  by memcmp()..**.
19b7b 2a 2a 20 54 77 6f 20 4e 55 4c 4c 20 76 61 6c 75  ** Two NULL valu
19b7c 65 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65  es are considere
19b7d 64 20 65 71 75 61 6c 20 62 79 20 74 68 69 73 20  d equal by this 
19b7e 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  function..*/.SQL
19b7f 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
19b80 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72  sqlite3MemCompar
19b81 65 28 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 4d 65  e(const Mem *pMe
19b82 6d 31 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70  m1, const Mem *p
19b83 4d 65 6d 32 2c 20 63 6f 6e 73 74 20 43 6f 6c 6c  Mem2, const Coll
19b84 53 65 71 20 2a 70 43 6f 6c 6c 29 7b 0a 20 20 69  Seq *pColl){.  i
19b85 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 66 31 2c  nt rc;.  int f1,
19b86 20 66 32 3b 0a 20 20 69 6e 74 20 63 6f 6d 62 69   f2;.  int combi
19b87 6e 65 64 5f 66 6c 61 67 73 3b 0a 0a 20 20 2f 2a  ned_flags;..  /*
19b88 20 49 6e 74 65 72 63 68 61 6e 67 65 20 70 4d 65   Interchange pMe
19b89 6d 31 20 61 6e 64 20 70 4d 65 6d 32 20 69 66 20  m1 and pMem2 if 
19b8a 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  the collating se
19b8b 71 75 65 6e 63 65 20 73 70 65 63 69 66 69 65 73  quence specifies
19b8c 0a 20 20 2a 2a 20 44 45 53 43 20 6f 72 64 65 72  .  ** DESC order
19b8d 2e 0a 20 20 2a 2f 0a 20 20 66 31 20 3d 20 70 4d  ..  */.  f1 = pM
19b8e 65 6d 31 2d 3e 66 6c 61 67 73 3b 0a 20 20 66 32  em1->flags;.  f2
19b8f 20 3d 20 70 4d 65 6d 32 2d 3e 66 6c 61 67 73 3b   = pMem2->flags;
19b90 0a 20 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67  .  combined_flag
19b91 73 20 3d 20 66 31 7c 66 32 3b 0a 20 20 61 73 73  s = f1|f2;.  ass
19b92 65 72 74 28 20 28 63 6f 6d 62 69 6e 65 64 5f 66  ert( (combined_f
19b93 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65  lags & MEM_RowSe
19b94 74 29 3d 3d 30 20 29 3b 0a 20 0a 20 20 2f 2a 20  t)==0 );. .  /* 
19b95 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20  If one value is 
19b96 4e 55 4c 4c 2c 20 69 74 20 69 73 20 6c 65 73 73  NULL, it is less
19b97 20 74 68 61 6e 20 74 68 65 20 6f 74 68 65 72 2e   than the other.
19b98 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 0a   If both values.
19b99 20 20 2a 2a 20 61 72 65 20 4e 55 4c 4c 2c 20 72    ** are NULL, r
19b9a 65 74 75 72 6e 20 30 2e 0a 20 20 2a 2f 0a 20 20  eturn 0..  */.  
19b9b 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61  if( combined_fla
19b9c 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20  gs&MEM_Null ){. 
19b9d 20 20 20 72 65 74 75 72 6e 20 28 66 32 26 4d 45     return (f2&ME
19b9e 4d 5f 4e 75 6c 6c 29 20 2d 20 28 66 31 26 4d 45  M_Null) - (f1&ME
19b9f 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20  M_Null);.  }..  
19ba0 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c 75 65 20  /* If one value 
19ba1 69 73 20 61 20 6e 75 6d 62 65 72 20 61 6e 64 20  is a number and 
19ba2 74 68 65 20 6f 74 68 65 72 20 69 73 20 6e 6f 74  the other is not
19ba3 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73 20  , the number is 
19ba4 6c 65 73 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f  less..  ** If bo
19ba5 74 68 20 61 72 65 20 6e 75 6d 62 65 72 73 2c 20  th are numbers, 
19ba6 63 6f 6d 70 61 72 65 20 61 73 20 72 65 61 6c 73  compare as reals
19ba7 20 69 66 20 6f 6e 65 20 69 73 20 61 20 72 65 61   if one is a rea
19ba8 6c 2c 20 6f 72 20 61 73 20 69 6e 74 65 67 65 72  l, or as integer
19ba9 73 0a 20 20 2a 2a 20 69 66 20 62 6f 74 68 20 76  s.  ** if both v
19baa 61 6c 75 65 73 20 61 72 65 20 69 6e 74 65 67 65  alues are intege
19bab 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63  rs..  */.  if( c
19bac 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 28 4d  ombined_flags&(M
19bad 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29  EM_Int|MEM_Real)
19bae 20 29 7b 0a 20 20 20 20 69 66 28 20 21 28 66 31   ){.    if( !(f1
19baf 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65  &(MEM_Int|MEM_Re
19bb0 61 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65  al)) ){.      re
19bb1 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
19bb2 20 20 69 66 28 20 21 28 66 32 26 28 4d 45 4d 5f    if( !(f2&(MEM_
19bb3 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29  Int|MEM_Real)) )
19bb4 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d  {.      return -
19bb5 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  1;.    }.    if(
19bb6 20 28 66 31 20 26 20 66 32 20 26 20 4d 45 4d 5f   (f1 & f2 & MEM_
19bb7 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Int)==0 ){.     
19bb8 20 64 6f 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a   double r1, r2;.
19bb9 20 20 20 20 20 20 69 66 28 20 28 66 31 26 4d 45        if( (f1&ME
19bba 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20  M_Real)==0 ){.  
19bbb 20 20 20 20 20 20 72 31 20 3d 20 28 64 6f 75 62        r1 = (doub
19bbc 6c 65 29 70 4d 65 6d 31 2d 3e 75 2e 69 3b 0a 20  le)pMem1->u.i;. 
19bbd 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
19bbe 20 20 20 20 72 31 20 3d 20 70 4d 65 6d 31 2d 3e      r1 = pMem1->
19bbf 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  r;.      }.     
19bc0 20 69 66 28 20 28 66 32 26 4d 45 4d 5f 52 65 61   if( (f2&MEM_Rea
19bc1 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  l)==0 ){.       
19bc2 20 72 32 20 3d 20 28 64 6f 75 62 6c 65 29 70 4d   r2 = (double)pM
19bc3 65 6d 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20  em2->u.i;.      
19bc4 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72  }else{.        r
19bc5 32 20 3d 20 70 4d 65 6d 32 2d 3e 72 3b 0a 20 20  2 = pMem2->r;.  
19bc6 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
19bc7 72 31 3c 72 32 20 29 20 72 65 74 75 72 6e 20 2d  r1<r2 ) return -
19bc8 31 3b 0a 20 20 20 20 20 20 69 66 28 20 72 31 3e  1;.      if( r1>
19bc9 72 32 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  r2 ) return 1;. 
19bca 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
19bcb 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
19bcc 61 73 73 65 72 74 28 20 66 31 26 4d 45 4d 5f 49  assert( f1&MEM_I
19bcd 6e 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  nt );.      asse
19bce 72 74 28 20 66 32 26 4d 45 4d 5f 49 6e 74 20 29  rt( f2&MEM_Int )
19bcf 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d  ;.      if( pMem
19bd0 31 2d 3e 75 2e 69 20 3c 20 70 4d 65 6d 32 2d 3e  1->u.i < pMem2->
19bd1 75 2e 69 20 29 20 72 65 74 75 72 6e 20 2d 31 3b  u.i ) return -1;
19bd2 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31  .      if( pMem1
19bd3 2d 3e 75 2e 69 20 3e 20 70 4d 65 6d 32 2d 3e 75  ->u.i > pMem2->u
19bd4 2e 69 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  .i ) return 1;. 
19bd5 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
19bd6 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
19bd7 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 61  f one value is a
19bd8 20 73 74 72 69 6e 67 20 61 6e 64 20 74 68 65 20   string and the 
19bd9 6f 74 68 65 72 20 69 73 20 61 20 62 6c 6f 62 2c  other is a blob,
19bda 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 6c   the string is l
19bdb 65 73 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74  ess..  ** If bot
19bdc 68 20 61 72 65 20 73 74 72 69 6e 67 73 2c 20 63  h are strings, c
19bdd 6f 6d 70 61 72 65 20 75 73 69 6e 67 20 74 68 65  ompare using the
19bde 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74   collating funct
19bdf 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ions..  */.  if(
19be0 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26   combined_flags&
19be1 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 69  MEM_Str ){.    i
19be2 66 28 20 28 66 31 20 26 20 4d 45 4d 5f 53 74 72  f( (f1 & MEM_Str
19be3 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  )==0 ){.      re
19be4 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
19be5 20 20 69 66 28 20 28 66 32 20 26 20 4d 45 4d 5f    if( (f2 & MEM_
19be6 53 74 72 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Str)==0 ){.     
19be7 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20   return -1;.    
19be8 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  }..    assert( p
19be9 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 70 4d 65 6d 32  Mem1->enc==pMem2
19bea 2d 3e 65 6e 63 20 29 3b 0a 20 20 20 20 61 73 73  ->enc );.    ass
19beb 65 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d  ert( pMem1->enc=
19bec 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20  =SQLITE_UTF8 || 
19bed 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4d 65  .            pMe
19bee 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f  m1->enc==SQLITE_
19bef 55 54 46 31 36 4c 45 20 7c 7c 20 70 4d 65 6d 31  UTF16LE || pMem1
19bf0 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  ->enc==SQLITE_UT
19bf1 46 31 36 42 45 20 29 3b 0a 0a 20 20 20 20 2f 2a  F16BE );..    /*
19bf2 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   The collation s
19bf3 65 71 75 65 6e 63 65 20 6d 75 73 74 20 62 65 20  equence must be 
19bf4 64 65 66 69 6e 65 64 20 61 74 20 74 68 69 73 20  defined at this 
19bf5 70 6f 69 6e 74 2c 20 65 76 65 6e 20 69 66 0a 20  point, even if. 
19bf6 20 20 20 2a 2a 20 74 68 65 20 75 73 65 72 20 64     ** the user d
19bf7 65 6c 65 74 65 73 20 74 68 65 20 63 6f 6c 6c 61  eletes the colla
19bf8 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 61 66  tion sequence af
19bf9 74 65 72 20 74 68 65 20 76 64 62 65 20 70 72 6f  ter the vdbe pro
19bfa 67 72 61 6d 20 69 73 0a 20 20 20 20 2a 2a 20 63  gram is.    ** c
19bfb 6f 6d 70 69 6c 65 64 20 28 74 68 69 73 20 77 61  ompiled (this wa
19bfc 73 20 6e 6f 74 20 61 6c 77 61 79 73 20 74 68 65  s not always the
19bfd 20 63 61 73 65 29 2e 0a 20 20 20 20 2a 2f 0a 20   case)..    */. 
19bfe 20 20 20 61 73 73 65 72 74 28 20 21 70 43 6f 6c     assert( !pCol
19bff 6c 20 7c 7c 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70  l || pColl->xCmp
19c00 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 43 6f   );..    if( pCo
19c01 6c 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ll ){.      if( 
19c02 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 70 43 6f 6c  pMem1->enc==pCol
19c03 6c 2d 3e 65 6e 63 20 29 7b 0a 20 20 20 20 20 20  l->enc ){.      
19c04 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e 67 73    /* The strings
19c05 20 61 72 65 20 61 6c 72 65 61 64 79 20 69 6e 20   are already in 
19c06 74 68 65 20 63 6f 72 72 65 63 74 20 65 6e 63 6f  the correct enco
19c07 64 69 6e 67 2e 20 20 43 61 6c 6c 20 74 68 65 0a  ding.  Call the.
19c08 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 61          ** compa
19c09 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 64  rison function d
19c0a 69 72 65 63 74 6c 79 20 2a 2f 0a 20 20 20 20 20  irectly */.     
19c0b 20 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 2d     return pColl-
19c0c 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73  >xCmp(pColl->pUs
19c0d 65 72 2c 70 4d 65 6d 31 2d 3e 6e 2c 70 4d 65 6d  er,pMem1->n,pMem
19c0e 31 2d 3e 7a 2c 70 4d 65 6d 32 2d 3e 6e 2c 70 4d  1->z,pMem2->n,pM
19c0f 65 6d 32 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 7d  em2->z);.      }
19c10 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f  else{.        co
19c11 6e 73 74 20 76 6f 69 64 20 2a 76 31 2c 20 2a 76  nst void *v1, *v
19c12 32 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  2;.        int n
19c13 31 2c 20 6e 32 3b 0a 20 20 20 20 20 20 20 20 4d  1, n2;.        M
19c14 65 6d 20 63 31 3b 0a 20 20 20 20 20 20 20 20 4d  em c1;.        M
19c15 65 6d 20 63 32 3b 0a 20 20 20 20 20 20 20 20 6d  em c2;.        m
19c16 65 6d 73 65 74 28 26 63 31 2c 20 30 2c 20 73 69  emset(&c1, 0, si
19c17 7a 65 6f 66 28 63 31 29 29 3b 0a 20 20 20 20 20  zeof(c1));.     
19c18 20 20 20 6d 65 6d 73 65 74 28 26 63 32 2c 20 30     memset(&c2, 0
19c19 2c 20 73 69 7a 65 6f 66 28 63 32 29 29 3b 0a 20  , sizeof(c2));. 
19c1a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
19c1b 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79  beMemShallowCopy
19c1c 28 26 63 31 2c 20 70 4d 65 6d 31 2c 20 4d 45 4d  (&c1, pMem1, MEM
19c1d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20 20 20  _Ephem);.       
19c1e 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
19c1f 68 61 6c 6c 6f 77 43 6f 70 79 28 26 63 32 2c 20  hallowCopy(&c2, 
19c20 70 4d 65 6d 32 2c 20 4d 45 4d 5f 45 70 68 65 6d  pMem2, MEM_Ephem
19c21 29 3b 0a 20 20 20 20 20 20 20 20 76 31 20 3d 20  );.        v1 = 
19c22 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
19c23 28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  ((sqlite3_value*
19c24 29 26 63 31 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63  )&c1, pColl->enc
19c25 29 3b 0a 20 20 20 20 20 20 20 20 6e 31 20 3d 20  );.        n1 = 
19c26 76 31 3d 3d 30 20 3f 20 30 20 3a 20 63 31 2e 6e  v1==0 ? 0 : c1.n
19c27 3b 0a 20 20 20 20 20 20 20 20 76 32 20 3d 20 73  ;.        v2 = s
19c28 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
19c29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
19c2a 26 63 32 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29  &c2, pColl->enc)
19c2b 3b 0a 20 20 20 20 20 20 20 20 6e 32 20 3d 20 76  ;.        n2 = v
19c2c 32 3d 3d 30 20 3f 20 30 20 3a 20 63 32 2e 6e 3b  2==0 ? 0 : c2.n;
19c2d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 43  .        rc = pC
19c2e 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d  oll->xCmp(pColl-
19c2f 3e 70 55 73 65 72 2c 20 6e 31 2c 20 76 31 2c 20  >pUser, n1, v1, 
19c30 6e 32 2c 20 76 32 29 3b 0a 20 20 20 20 20 20 20  n2, v2);.       
19c31 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
19c32 65 6c 65 61 73 65 28 26 63 31 29 3b 0a 20 20 20  elease(&c1);.   
19c33 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
19c34 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 32 29 3b  MemRelease(&c2);
19c35 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
19c36 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
19c37 7d 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 4e 55  }.    /* If a NU
19c38 4c 4c 20 70 6f 69 6e 74 65 72 20 77 61 73 20 70  LL pointer was p
19c39 61 73 73 65 64 20 61 73 20 74 68 65 20 63 6f 6c  assed as the col
19c3a 6c 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 66  late function, f
19c3b 61 6c 6c 20 74 68 72 6f 75 67 68 0a 20 20 20 20  all through.    
19c3c 2a 2a 20 74 6f 20 74 68 65 20 62 6c 6f 62 20 63  ** to the blob c
19c3d 61 73 65 20 61 6e 64 20 75 73 65 20 6d 65 6d 63  ase and use memc
19c3e 6d 70 28 29 2e 20 20 2a 2f 0a 20 20 7d 0a 20 0a  mp().  */.  }. .
19c3f 20 20 2f 2a 20 42 6f 74 68 20 76 61 6c 75 65 73    /* Both values
19c40 20 6d 75 73 74 20 62 65 20 62 6c 6f 62 73 2e 20   must be blobs. 
19c41 20 43 6f 6d 70 61 72 65 20 75 73 69 6e 67 20 6d   Compare using m
19c42 65 6d 63 6d 70 28 29 2e 20 20 2a 2f 0a 20 20 72  emcmp().  */.  r
19c43 63 20 3d 20 6d 65 6d 63 6d 70 28 70 4d 65 6d 31  c = memcmp(pMem1
19c44 2d 3e 7a 2c 20 70 4d 65 6d 32 2d 3e 7a 2c 20 28  ->z, pMem2->z, (
19c45 70 4d 65 6d 31 2d 3e 6e 3e 70 4d 65 6d 32 2d 3e  pMem1->n>pMem2->
19c46 6e 29 3f 70 4d 65 6d 32 2d 3e 6e 3a 70 4d 65 6d  n)?pMem2->n:pMem
19c47 31 2d 3e 6e 29 3b 0a 20 20 69 66 28 20 72 63 3d  1->n);.  if( rc=
19c48 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70  =0 ){.    rc = p
19c49 4d 65 6d 31 2d 3e 6e 20 2d 20 70 4d 65 6d 32 2d  Mem1->n - pMem2-
19c4a 3e 6e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  >n;.  }.  return
19c4b 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f   rc;.}../*.** Mo
19c4c 76 65 20 64 61 74 61 20 6f 75 74 20 6f 66 20 61  ve data out of a
19c4d 20 62 74 72 65 65 20 6b 65 79 20 6f 72 20 64 61   btree key or da
19c4e 74 61 20 66 69 65 6c 64 20 61 6e 64 20 69 6e 74  ta field and int
19c4f 6f 20 61 20 4d 65 6d 20 73 74 72 75 63 74 75 72  o a Mem structur
19c50 65 2e 0a 2a 2a 20 54 68 65 20 64 61 74 61 20 6f  e..** The data o
19c51 72 20 6b 65 79 20 69 73 20 74 61 6b 65 6e 20 66  r key is taken f
19c52 72 6f 6d 20 74 68 65 20 65 6e 74 72 79 20 74 68  rom the entry th
19c53 61 74 20 70 43 75 72 20 69 73 20 63 75 72 72 65  at pCur is curre
19c54 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a  ntly pointing.**
19c55 20 74 6f 2e 20 20 6f 66 66 73 65 74 20 61 6e 64   to.  offset and
19c56 20 61 6d 74 20 64 65 74 65 72 6d 69 6e 65 20 77   amt determine w
19c57 68 61 74 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74  hat portion of t
19c58 68 65 20 64 61 74 61 20 6f 72 20 6b 65 79 20 74  he data or key t
19c59 6f 20 72 65 74 72 69 65 76 65 2e 0a 2a 2a 20 6b  o retrieve..** k
19c5a 65 79 20 69 73 20 74 72 75 65 20 74 6f 20 67 65  ey is true to ge
19c5b 74 20 74 68 65 20 6b 65 79 20 6f 72 20 66 61 6c  t the key or fal
19c5c 73 65 20 74 6f 20 67 65 74 20 64 61 74 61 2e 20  se to get data. 
19c5d 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 77   The result is w
19c5e 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74  ritten.** into t
19c5f 68 65 20 70 4d 65 6d 20 65 6c 65 6d 65 6e 74 2e  he pMem element.
19c60 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4d 65 6d 20  .**.** The pMem 
19c61 73 74 72 75 63 74 75 72 65 20 69 73 20 61 73 73  structure is ass
19c62 75 6d 65 64 20 74 6f 20 62 65 20 75 6e 69 6e 69  umed to be unini
19c63 74 69 61 6c 69 7a 65 64 2e 20 20 41 6e 79 20 70  tialized.  Any p
19c64 72 69 6f 72 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20  rior content.** 
19c65 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77  is overwritten w
19c66 69 74 68 6f 75 74 20 62 65 69 6e 67 20 66 72 65  ithout being fre
19c67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69  ed..**.** If thi
19c68 73 20 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 20  s routine fails 
19c69 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 20 28  for any reason (
19c6a 6d 61 6c 6c 6f 63 20 72 65 74 75 72 6e 73 20 4e  malloc returns N
19c6b 55 4c 4c 20 6f 72 20 75 6e 61 62 6c 65 0a 2a 2a  ULL or unable.**
19c6c 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 74 68   to read from th
19c6d 65 20 64 69 73 6b 29 20 74 68 65 6e 20 74 68 65  e disk) then the
19c6e 20 70 4d 65 6d 20 69 73 20 6c 65 66 74 20 69 6e   pMem is left in
19c6f 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74   an inconsistent
19c70 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54   state..*/.SQLIT
19c71 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
19c72 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d  lite3VdbeMemFrom
19c73 42 74 72 65 65 28 0a 20 20 42 74 43 75 72 73 6f  Btree(.  BtCurso
19c74 72 20 2a 70 43 75 72 2c 20 20 20 2f 2a 20 43 75  r *pCur,   /* Cu
19c75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74  rsor pointing at
19c76 20 72 65 63 6f 72 64 20 74 6f 20 72 65 74 72 69   record to retri
19c77 65 76 65 2e 20 2a 2f 0a 20 20 69 6e 74 20 6f 66  eve. */.  int of
19c78 66 73 65 74 2c 20 20 20 20 20 20 20 2f 2a 20 4f  fset,       /* O
19c79 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20 73  ffset from the s
19c7a 74 61 72 74 20 6f 66 20 64 61 74 61 20 74 6f 20  tart of data to 
19c7b 72 65 74 75 72 6e 20 62 79 74 65 73 20 66 72 6f  return bytes fro
19c7c 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c  m. */.  int amt,
19c7d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
19c7e 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20  ber of bytes to 
19c7f 72 65 74 75 72 6e 2e 20 2a 2f 0a 20 20 69 6e 74  return. */.  int
19c80 20 6b 65 79 2c 20 20 20 20 20 20 20 20 20 20 2f   key,          /
19c81 2a 20 49 66 20 74 72 75 65 2c 20 72 65 74 72 69  * If true, retri
19c82 65 76 65 20 66 72 6f 6d 20 74 68 65 20 62 74 72  eve from the btr
19c83 65 65 20 6b 65 79 2c 20 6e 6f 74 20 64 61 74 61  ee key, not data
19c84 2e 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d  . */.  Mem *pMem
19c85 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
19c86 20 52 65 74 75 72 6e 20 64 61 74 61 20 69 6e 20   Return data in 
19c87 74 68 69 73 20 4d 65 6d 20 73 74 72 75 63 74 75  this Mem structu
19c88 72 65 2e 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72  re. */.){.  char
19c89 20 2a 7a 44 61 74 61 3b 20 20 20 20 20 20 20 20   *zData;        
19c8a 2f 2a 20 44 61 74 61 20 66 72 6f 6d 20 74 68 65  /* Data from the
19c8b 20 62 74 72 65 65 20 6c 61 79 65 72 20 2a 2f 0a   btree layer */.
19c8c 20 20 69 6e 74 20 61 76 61 69 6c 61 62 6c 65 20    int available 
19c8d 3d 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20  = 0;  /* Number 
19c8e 6f 66 20 62 79 74 65 73 20 61 76 61 69 6c 61 62  of bytes availab
19c8f 6c 65 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20  le on the local 
19c90 62 74 72 65 65 20 70 61 67 65 20 2a 2f 0a 20 20  btree page */.  
19c91 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
19c92 4f 4b 3b 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f  OK; /* Return co
19c93 64 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  de */..  assert(
19c94 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
19c95 73 6f 72 49 73 56 61 6c 69 64 28 70 43 75 72 29  sorIsValid(pCur)
19c96 20 29 3b 0a 0a 20 20 2f 2a 20 4e 6f 74 65 3a 20   );..  /* Note: 
19c97 74 68 65 20 63 61 6c 6c 73 20 74 6f 20 42 74 72  the calls to Btr
19c98 65 65 4b 65 79 46 65 74 63 68 28 29 20 61 6e 64  eeKeyFetch() and
19c99 20 44 61 74 61 46 65 74 63 68 28 29 20 62 65 6c   DataFetch() bel
19c9a 6f 77 20 61 73 73 65 72 74 28 29 20 0a 20 20 2a  ow assert() .  *
19c9b 2a 20 74 68 61 74 20 62 6f 74 68 20 74 68 65 20  * that both the 
19c9c 42 74 53 68 61 72 65 64 20 61 6e 64 20 64 61 74  BtShared and dat
19c9d 61 62 61 73 65 20 68 61 6e 64 6c 65 20 6d 75 74  abase handle mut
19c9e 65 78 65 73 20 61 72 65 20 68 65 6c 64 2e 20 2a  exes are held. *
19c9f 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65  /.  assert( (pMe
19ca0 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  m->flags & MEM_R
19ca1 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 69  owSet)==0 );.  i
19ca2 66 28 20 6b 65 79 20 29 7b 0a 20 20 20 20 7a 44  f( key ){.    zD
19ca3 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29 73 71  ata = (char *)sq
19ca4 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46 65 74  lite3BtreeKeyFet
19ca5 63 68 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61  ch(pCur, &availa
19ca6 62 6c 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ble);.  }else{. 
19ca7 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68 61 72     zData = (char
19ca8 20 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 44   *)sqlite3BtreeD
19ca9 61 74 61 46 65 74 63 68 28 70 43 75 72 2c 20 26  ataFetch(pCur, &
19caa 61 76 61 69 6c 61 62 6c 65 29 3b 0a 20 20 7d 0a  available);.  }.
19cab 20 20 61 73 73 65 72 74 28 20 7a 44 61 74 61 21    assert( zData!
19cac 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 6f 66 66  =0 );..  if( off
19cad 73 65 74 2b 61 6d 74 3c 3d 61 76 61 69 6c 61 62  set+amt<=availab
19cae 6c 65 20 26 26 20 28 70 4d 65 6d 2d 3e 66 6c 61  le && (pMem->fla
19caf 67 73 26 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29  gs&MEM_Dyn)==0 )
19cb0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
19cb1 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d  eMemRelease(pMem
19cb2 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d  );.    pMem->z =
19cb3 20 26 7a 44 61 74 61 5b 6f 66 66 73 65 74 5d 3b   &zData[offset];
19cb4 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  .    pMem->flags
19cb5 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f   = MEM_Blob|MEM_
19cb6 45 70 68 65 6d 3b 0a 20 20 7d 65 6c 73 65 20 69  Ephem;.  }else i
19cb7 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72  f( SQLITE_OK==(r
19cb8 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
19cb9 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 61 6d 74  emGrow(pMem, amt
19cba 2b 32 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 70  +2, 0)) ){.    p
19cbb 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
19cbc 5f 42 6c 6f 62 7c 4d 45 4d 5f 44 79 6e 7c 4d 45  _Blob|MEM_Dyn|ME
19cbd 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 70 4d 65 6d  M_Term;.    pMem
19cbe 2d 3e 65 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70  ->enc = 0;.    p
19cbf 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
19cc0 54 45 5f 42 4c 4f 42 3b 0a 20 20 20 20 69 66 28  TE_BLOB;.    if(
19cc1 20 6b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63   key ){.      rc
19cc2 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b   = sqlite3BtreeK
19cc3 65 79 28 70 43 75 72 2c 20 6f 66 66 73 65 74 2c  ey(pCur, offset,
19cc4 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a   amt, pMem->z);.
19cc5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
19cc6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
19cc7 65 65 44 61 74 61 28 70 43 75 72 2c 20 6f 66 66  eeData(pCur, off
19cc8 73 65 74 2c 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e  set, amt, pMem->
19cc9 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d  z);.    }.    pM
19cca 65 6d 2d 3e 7a 5b 61 6d 74 5d 20 3d 20 30 3b 0a  em->z[amt] = 0;.
19ccb 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 2b      pMem->z[amt+
19ccc 31 5d 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  1] = 0;.    if( 
19ccd 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
19cce 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
19ccf 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65  beMemRelease(pMe
19cd0 6d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  m);.    }.  }.  
19cd1 70 4d 65 6d 2d 3e 6e 20 3d 20 61 6d 74 3b 0a 0a  pMem->n = amt;..
19cd2 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
19cd3 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  /* This function
19cd4 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62   is only availab
19cd5 6c 65 20 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 69  le internally, i
19cd6 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66  t is not part of
19cd7 20 74 68 65 0a 2a 2a 20 65 78 74 65 72 6e 61 6c   the.** external
19cd8 20 41 50 49 2e 20 49 74 20 77 6f 72 6b 73 20 69   API. It works i
19cd9 6e 20 61 20 73 69 6d 69 6c 61 72 20 77 61 79 20  n a similar way 
19cda 74 6f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  to sqlite3_value
19cdb 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 65 78 63 65  _text(),.** exce
19cdc 70 74 20 74 68 65 20 64 61 74 61 20 72 65 74 75  pt the data retu
19cdd 72 6e 65 64 20 69 73 20 69 6e 20 74 68 65 20 65  rned is in the e
19cde 6e 63 6f 64 69 6e 67 20 73 70 65 63 69 66 69 65  ncoding specifie
19cdf 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64 0a  d by the second.
19ce0 2a 2a 20 70 61 72 61 6d 65 74 65 72 2c 20 77 68  ** parameter, wh
19ce1 69 63 68 20 6d 75 73 74 20 62 65 20 6f 6e 65 20  ich must be one 
19ce2 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42  of SQLITE_UTF16B
19ce3 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  E, SQLITE_UTF16L
19ce4 45 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 55  E or.** SQLITE_U
19ce5 54 46 38 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36  TF8..**.** (2006
19ce6 2d 30 32 2d 31 36 3a 29 20 20 54 68 65 20 65 6e  -02-16:)  The en
19ce7 63 20 76 61 6c 75 65 20 63 61 6e 20 62 65 20 6f  c value can be o
19ce8 72 2d 65 64 20 77 69 74 68 20 53 51 4c 49 54 45  r-ed with SQLITE
19ce9 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 2e 0a  _UTF16_ALIGNED..
19cea 2a 2a 20 49 66 20 74 68 61 74 20 69 73 20 74 68  ** If that is th
19ceb 65 20 63 61 73 65 2c 20 74 68 65 6e 20 74 68 65  e case, then the
19cec 20 72 65 73 75 6c 74 20 6d 75 73 74 20 62 65 20   result must be 
19ced 61 6c 69 67 6e 65 64 20 6f 6e 20 61 6e 20 65 76  aligned on an ev
19cee 65 6e 20 62 79 74 65 0a 2a 2a 20 62 6f 75 6e 64  en byte.** bound
19cef 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ary..*/.SQLITE_P
19cf0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69  RIVATE const voi
19cf1 64 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 54  d *sqlite3ValueT
19cf2 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  ext(sqlite3_valu
19cf3 65 2a 20 70 56 61 6c 2c 20 75 38 20 65 6e 63 29  e* pVal, u8 enc)
19cf4 7b 0a 20 20 69 66 28 20 21 70 56 61 6c 20 29 20  {.  if( !pVal ) 
19cf5 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 61 73 73  return 0;..  ass
19cf6 65 72 74 28 20 70 56 61 6c 2d 3e 64 62 3d 3d 30  ert( pVal->db==0
19cf7 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   || sqlite3_mute
19cf8 78 5f 68 65 6c 64 28 70 56 61 6c 2d 3e 64 62 2d  x_held(pVal->db-
19cf9 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
19cfa 65 72 74 28 20 28 65 6e 63 26 33 29 3d 3d 28 65  ert( (enc&3)==(e
19cfb 6e 63 26 7e 53 51 4c 49 54 45 5f 55 54 46 31 36  nc&~SQLITE_UTF16
19cfc 5f 41 4c 49 47 4e 45 44 29 20 29 3b 0a 20 20 61  _ALIGNED) );.  a
19cfd 73 73 65 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c  ssert( (pVal->fl
19cfe 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74  ags & MEM_RowSet
19cff 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 70  )==0 );..  if( p
19d00 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e  Val->flags&MEM_N
19d01 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ull ){.    retur
19d02 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  n 0;.  }.  asser
19d03 74 28 20 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29  t( (MEM_Blob>>3)
19d04 20 3d 3d 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20   == MEM_Str );. 
19d05 20 70 56 61 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20   pVal->flags |= 
19d06 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d  (pVal->flags & M
19d07 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 65  EM_Blob)>>3;.  e
19d08 78 70 61 6e 64 42 6c 6f 62 28 70 56 61 6c 29 3b  xpandBlob(pVal);
19d09 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61  .  if( pVal->fla
19d0a 67 73 26 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20  gs&MEM_Str ){.  
19d0b 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
19d0c 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61 6c  ngeEncoding(pVal
19d0d 2c 20 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f  , enc & ~SQLITE_
19d0e 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 3b 0a  UTF16_ALIGNED);.
19d0f 20 20 20 20 69 66 28 20 28 65 6e 63 20 26 20 53      if( (enc & S
19d10 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47  QLITE_UTF16_ALIG
19d11 4e 45 44 29 21 3d 30 20 26 26 20 31 3d 3d 28 31  NED)!=0 && 1==(1
19d12 26 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49  &SQLITE_PTR_TO_I
19d13 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20 29 7b 0a  NT(pVal->z)) ){.
19d14 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70        assert( (p
19d15 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45  Val->flags & (ME
19d16 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74  M_Ephem|MEM_Stat
19d17 69 63 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 20  ic))!=0 );.     
19d18 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
19d19 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65  MemMakeWriteable
19d1a 28 70 56 61 6c 29 21 3d 53 51 4c 49 54 45 5f 4f  (pVal)!=SQLITE_O
19d1b 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  K ){.        ret
19d1c 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 0;.      }. 
19d1d 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
19d1e 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e  VdbeMemNulTermin
19d1f 61 74 65 28 70 56 61 6c 29 3b 0a 20 20 7d 65 6c  ate(pVal);.  }el
19d20 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
19d21 28 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d  (pVal->flags&MEM
19d22 5f 42 6c 6f 62 29 3d 3d 30 20 29 3b 0a 20 20 20  _Blob)==0 );.   
19d23 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
19d24 74 72 69 6e 67 69 66 79 28 70 56 61 6c 2c 20 65  tringify(pVal, e
19d25 6e 63 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  nc);.    assert(
19d26 20 30 3d 3d 28 31 26 53 51 4c 49 54 45 5f 50 54   0==(1&SQLITE_PT
19d27 52 5f 54 4f 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a  R_TO_INT(pVal->z
19d28 29 29 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  )) );.  }.  asse
19d29 72 74 28 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65  rt(pVal->enc==(e
19d2a 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46  nc & ~SQLITE_UTF
19d2b 31 36 5f 41 4c 49 47 4e 45 44 29 20 7c 7c 20 70  16_ALIGNED) || p
19d2c 56 61 6c 2d 3e 64 62 3d 3d 30 0a 20 20 20 20 20  Val->db==0.     
19d2d 20 20 20 20 20 20 20 20 20 7c 7c 20 70 56 61 6c           || pVal
19d2e 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
19d2f 65 64 20 29 3b 0a 20 20 69 66 28 20 70 56 61 6c  ed );.  if( pVal
19d30 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53  ->enc==(enc & ~S
19d31 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47  QLITE_UTF16_ALIG
19d32 4e 45 44 29 20 29 7b 0a 20 20 20 20 72 65 74 75  NED) ){.    retu
19d33 72 6e 20 70 56 61 6c 2d 3e 7a 3b 0a 20 20 7d 65  rn pVal->z;.  }e
19d34 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
19d35 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
19d36 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 71 6c  Create a new sql
19d37 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
19d38 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
19d39 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76 61 6c  VATE sqlite3_val
19d3a 75 65 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65  ue *sqlite3Value
19d3b 4e 65 77 28 73 71 6c 69 74 65 33 20 2a 64 62 29  New(sqlite3 *db)
19d3c 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 73 71 6c  {.  Mem *p = sql
19d3d 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
19d3e 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29  (db, sizeof(*p))
19d3f 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ;.  if( p ){.   
19d40 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f   p->flags = MEM_
19d41 4e 75 6c 6c 3b 0a 20 20 20 20 70 2d 3e 74 79 70  Null;.    p->typ
19d42 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b  e = SQLITE_NULL;
19d43 0a 20 20 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b  .    p->db = db;
19d44 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
19d45 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  .}../*.** Create
19d46 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 76   a new sqlite3_v
19d47 61 6c 75 65 20 6f 62 6a 65 63 74 2c 20 63 6f 6e  alue object, con
19d48 74 61 69 6e 69 6e 67 20 74 68 65 20 76 61 6c 75  taining the valu
19d49 65 20 6f 66 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a  e of pExpr..**.*
19d4a 2a 20 54 68 69 73 20 6f 6e 6c 79 20 77 6f 72 6b  * This only work
19d4b 73 20 66 6f 72 20 76 65 72 79 20 73 69 6d 70 6c  s for very simpl
19d4c 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68  e expressions th
19d4d 61 74 20 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e  at consist of on
19d4e 65 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 74 6f  e constant.** to
19d4f 6b 65 6e 20 28 69 2e 65 2e 20 22 35 22 2c 20 22  ken (i.e. "5", "
19d50 35 2e 31 22 2c 20 22 27 61 20 73 74 72 69 6e 67  5.1", "'a string
19d51 27 22 29 2e 20 49 66 20 74 68 65 20 65 78 70 72  '"). If the expr
19d52 65 73 73 69 6f 6e 20 63 61 6e 0a 2a 2a 20 62 65  ession can.** be
19d53 20 63 6f 6e 76 65 72 74 65 64 20 64 69 72 65 63   converted direc
19d54 74 6c 79 20 69 6e 74 6f 20 61 20 76 61 6c 75 65  tly into a value
19d55 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65  , then the value
19d56 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e   is allocated an
19d57 64 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 77  d.** a pointer w
19d58 72 69 74 74 65 6e 20 74 6f 20 2a 70 70 56 61 6c  ritten to *ppVal
19d59 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20  . The caller is 
19d5a 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
19d5b 64 65 61 6c 6c 6f 63 61 74 69 6e 67 0a 2a 2a 20  deallocating.** 
19d5c 74 68 65 20 76 61 6c 75 65 20 62 79 20 70 61 73  the value by pas
19d5d 73 69 6e 67 20 69 74 20 74 6f 20 73 71 6c 69 74  sing it to sqlit
19d5e 65 33 56 61 6c 75 65 46 72 65 65 28 29 20 6c 61  e3ValueFree() la
19d5f 74 65 72 20 6f 6e 2e 20 49 66 20 74 68 65 20 65  ter on. If the e
19d60 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 63 61 6e  xpression.** can
19d61 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64  not be converted
19d62 20 74 6f 20 61 20 76 61 6c 75 65 2c 20 74 68 65   to a value, the
19d63 6e 20 2a 70 70 56 61 6c 20 69 73 20 73 65 74 20  n *ppVal is set 
19d64 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49  to NULL..*/.SQLI
19d65 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
19d66 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45  qlite3ValueFromE
19d67 78 70 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  xpr(.  sqlite3 *
19d68 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
19d69 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
19d6a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
19d6b 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20   Expr *pExpr,   
19d6c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
19d6d 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  e expression to 
19d6e 65 76 61 6c 75 61 74 65 20 2a 2f 0a 20 20 75 38  evaluate */.  u8
19d6f 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20   enc,           
19d70 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64          /* Encod
19d71 69 6e 67 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20  ing to use */.  
19d72 75 38 20 61 66 66 69 6e 69 74 79 2c 20 20 20 20  u8 affinity,    
19d73 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 66 66            /* Aff
19d74 69 6e 69 74 79 20 74 6f 20 75 73 65 20 2a 2f 0a  inity to use */.
19d75 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
19d76 2a 2a 70 70 56 61 6c 20 20 20 20 20 2f 2a 20 57  **ppVal     /* W
19d77 72 69 74 65 20 74 68 65 20 6e 65 77 20 76 61 6c  rite the new val
19d78 75 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  ue here */.){.  
19d79 69 6e 74 20 6f 70 3b 0a 20 20 63 68 61 72 20 2a  int op;.  char *
19d7a 7a 56 61 6c 20 3d 20 30 3b 0a 20 20 73 71 6c 69  zVal = 0;.  sqli
19d7b 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20  te3_value *pVal 
19d7c 3d 20 30 3b 0a 0a 20 20 69 66 28 20 21 70 45 78  = 0;..  if( !pEx
19d7d 70 72 20 29 7b 0a 20 20 20 20 2a 70 70 56 61 6c  pr ){.    *ppVal
19d7e 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 0;.    return
19d7f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
19d80 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70    op = pExpr->op
19d81 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 52  ;.  if( op==TK_R
19d82 45 47 49 53 54 45 52 20 29 7b 0a 20 20 20 20 6f  EGISTER ){.    o
19d83 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 32 3b 0a  p = pExpr->op2;.
19d84 20 20 7d 0a 0a 20 20 69 66 28 20 6f 70 3d 3d 54    }..  if( op==T
19d85 4b 5f 53 54 52 49 4e 47 20 7c 7c 20 6f 70 3d 3d  K_STRING || op==
19d86 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 6f 70 3d 3d  TK_FLOAT || op==
19d87 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20  TK_INTEGER ){.  
19d88 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33    pVal = sqlite3
19d89 56 61 6c 75 65 4e 65 77 28 64 62 29 3b 0a 20 20  ValueNew(db);.  
19d8a 20 20 69 66 28 20 70 56 61 6c 3d 3d 30 20 29 20    if( pVal==0 ) 
19d8b 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20  goto no_mem;.   
19d8c 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
19d8d 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49  erty(pExpr, EP_I
19d8e 6e 74 56 61 6c 75 65 29 20 29 7b 0a 20 20 20 20  ntValue) ){.    
19d8f 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
19d90 53 65 74 49 6e 74 36 34 28 70 56 61 6c 2c 20 28  SetInt64(pVal, (
19d91 69 36 34 29 70 45 78 70 72 2d 3e 75 2e 69 56 61  i64)pExpr->u.iVa
19d92 6c 75 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  lue);.    }else{
19d93 0a 20 20 20 20 20 20 7a 56 61 6c 20 3d 20 73 71  .      zVal = sq
19d94 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
19d95 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  , pExpr->u.zToke
19d96 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 56  n);.      if( zV
19d97 61 6c 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f  al==0 ) goto no_
19d98 6d 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  mem;.      sqlit
19d99 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 70 56  e3ValueSetStr(pV
19d9a 61 6c 2c 20 2d 31 2c 20 7a 56 61 6c 2c 20 53 51  al, -1, zVal, SQ
19d9b 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
19d9c 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20  E_DYNAMIC);.    
19d9d 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 46 4c 4f    if( op==TK_FLO
19d9e 41 54 20 29 20 70 56 61 6c 2d 3e 74 79 70 65 20  AT ) pVal->type 
19d9f 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a  = SQLITE_FLOAT;.
19da0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 6f      }.    if( (o
19da1 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c  p==TK_INTEGER ||
19da2 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 20   op==TK_FLOAT ) 
19da3 26 26 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c  && affinity==SQL
19da4 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a  ITE_AFF_NONE ){.
19da5 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c        sqlite3Val
19da6 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28  ueApplyAffinity(
19da7 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 41 46 46  pVal, SQLITE_AFF
19da8 5f 4e 55 4d 45 52 49 43 2c 20 53 51 4c 49 54 45  _NUMERIC, SQLITE
19da9 5f 55 54 46 38 29 3b 0a 20 20 20 20 7d 65 6c 73  _UTF8);.    }els
19daa 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
19dab 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69  ValueApplyAffini
19dac 74 79 28 70 56 61 6c 2c 20 61 66 66 69 6e 69 74  ty(pVal, affinit
19dad 79 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b  y, SQLITE_UTF8);
19dae 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65  .    }.    if( e
19daf 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc!=SQLITE_UTF8 
19db0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
19db1 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69  VdbeChangeEncodi
19db2 6e 67 28 70 56 61 6c 2c 20 65 6e 63 29 3b 0a 20  ng(pVal, enc);. 
19db3 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
19db4 20 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20 29   op==TK_UMINUS )
19db5 20 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54   {.    if( SQLIT
19db6 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 61 6c  E_OK==sqlite3Val
19db7 75 65 46 72 6f 6d 45 78 70 72 28 64 62 2c 70 45  ueFromExpr(db,pE
19db8 78 70 72 2d 3e 70 4c 65 66 74 2c 65 6e 63 2c 61  xpr->pLeft,enc,a
19db9 66 66 69 6e 69 74 79 2c 26 70 56 61 6c 29 20 29  ffinity,&pVal) )
19dba 7b 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e 75 2e  {.      pVal->u.
19dbb 69 20 3d 20 2d 31 20 2a 20 70 56 61 6c 2d 3e 75  i = -1 * pVal->u
19dbc 2e 69 3b 0a 20 20 20 20 20 20 2f 2a 20 28 64 6f  .i;.      /* (do
19dbd 75 62 6c 65 29 2d 31 20 49 6e 20 63 61 73 65 20  uble)-1 In case 
19dbe 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  of SQLITE_OMIT_F
19dbf 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e  LOATING_POINT...
19dc0 20 2a 2f 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e   */.      pVal->
19dc1 72 20 3d 20 28 64 6f 75 62 6c 65 29 2d 31 20 2a  r = (double)-1 *
19dc2 20 70 56 61 6c 2d 3e 72 3b 0a 20 20 20 20 7d 0a   pVal->r;.    }.
19dc3 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
19dc4 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54  TE_OMIT_BLOB_LIT
19dc5 45 52 41 4c 0a 20 20 65 6c 73 65 20 69 66 28 20  ERAL.  else if( 
19dc6 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 7b 0a 20  op==TK_BLOB ){. 
19dc7 20 20 20 69 6e 74 20 6e 56 61 6c 3b 0a 20 20 20     int nVal;.   
19dc8 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
19dc9 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 78 27  u.zToken[0]=='x'
19dca 20 7c 7c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f   || pExpr->u.zTo
19dcb 6b 65 6e 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20  ken[0]=='X' );. 
19dcc 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
19dcd 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d 27  ->u.zToken[1]=='
19dce 5c 27 27 20 29 3b 0a 20 20 20 20 70 56 61 6c 20  \'' );.    pVal 
19dcf 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65  = sqlite3ValueNe
19dd0 77 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 21  w(db);.    if( !
19dd1 70 56 61 6c 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d  pVal ) goto no_m
19dd2 65 6d 3b 0a 20 20 20 20 7a 56 61 6c 20 3d 20 26  em;.    zVal = &
19dd3 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b  pExpr->u.zToken[
19dd4 32 5d 3b 0a 20 20 20 20 6e 56 61 6c 20 3d 20 73  2];.    nVal = s
19dd5 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
19dd6 56 61 6c 29 2d 31 3b 0a 20 20 20 20 61 73 73 65  Val)-1;.    asse
19dd7 72 74 28 20 7a 56 61 6c 5b 6e 56 61 6c 5d 3d 3d  rt( zVal[nVal]==
19dd8 27 5c 27 27 20 29 3b 0a 20 20 20 20 73 71 6c 69  '\'' );.    sqli
19dd9 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
19dda 28 70 56 61 6c 2c 20 73 71 6c 69 74 65 33 48 65  (pVal, sqlite3He
19ddb 78 54 6f 42 6c 6f 62 28 64 62 2c 20 7a 56 61 6c  xToBlob(db, zVal
19ddc 2c 20 6e 56 61 6c 29 2c 20 6e 56 61 6c 2f 32 2c  , nVal), nVal/2,
19ddd 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
19dde 20 20 20 20 20 20 20 20 20 20 30 2c 20 53 51 4c            0, SQL
19ddf 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20  ITE_DYNAMIC);.  
19de0 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2a 70 70 56  }.#endif..  *ppV
19de1 61 6c 20 3d 20 70 56 61 6c 3b 0a 20 20 72 65 74  al = pVal;.  ret
19de2 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  urn SQLITE_OK;..
19de3 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61  no_mem:.  db->ma
19de4 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
19de5 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
19de6 64 62 2c 20 7a 56 61 6c 29 3b 0a 20 20 73 71 6c  db, zVal);.  sql
19de7 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 56  ite3ValueFree(pV
19de8 61 6c 29 3b 0a 20 20 2a 70 70 56 61 6c 20 3d 20  al);.  *ppVal = 
19de9 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  0;.  return SQLI
19dea 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a 0a 2f 2a 0a  TE_NOMEM;.}../*.
19deb 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 73 74  ** Change the st
19dec 72 69 6e 67 20 76 61 6c 75 65 20 6f 66 20 61 6e  ring value of an
19ded 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
19dee 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f  bject.*/.SQLITE_
19def 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
19df0 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28  ite3ValueSetStr(
19df1 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
19df2 20 2a 76 2c 20 20 20 20 20 2f 2a 20 56 61 6c 75   *v,     /* Valu
19df3 65 20 74 6f 20 62 65 20 73 65 74 20 2a 2f 0a 20  e to be set */. 
19df4 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20   int n,         
19df5 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
19df6 20 6f 66 20 73 74 72 69 6e 67 20 7a 20 2a 2f 0a   of string z */.
19df7 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c    const void *z,
19df8 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20          /* Text 
19df9 6f 66 20 74 68 65 20 6e 65 77 20 73 74 72 69 6e  of the new strin
19dfa 67 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20  g */.  u8 enc,  
19dfb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19dfc 45 6e 63 6f 64 69 6e 67 20 74 6f 20 75 73 65 20  Encoding to use 
19dfd 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c  */.  void (*xDel
19dfe 29 28 76 6f 69 64 2a 29 20 20 20 2f 2a 20 44 65  )(void*)   /* De
19dff 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68 65  structor for the
19e00 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20   string */.){.  
19e01 69 66 28 20 76 20 29 20 73 71 6c 69 74 65 33 56  if( v ) sqlite3V
19e02 64 62 65 4d 65 6d 53 65 74 53 74 72 28 28 4d 65  dbeMemSetStr((Me
19e03 6d 20 2a 29 76 2c 20 7a 2c 20 6e 2c 20 65 6e 63  m *)v, z, n, enc
19e04 2c 20 78 44 65 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  , xDel);.}../*.*
19e05 2a 20 46 72 65 65 20 61 6e 20 73 71 6c 69 74 65  * Free an sqlite
19e06 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a  3_value object.*
19e07 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19e08 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c   void sqlite3Val
19e09 75 65 46 72 65 65 28 73 71 6c 69 74 65 33 5f 76  ueFree(sqlite3_v
19e0a 61 6c 75 65 20 2a 76 29 7b 0a 20 20 69 66 28 20  alue *v){.  if( 
19e0b 21 76 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73  !v ) return;.  s
19e0c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
19e0d 65 61 73 65 28 28 4d 65 6d 20 2a 29 76 29 3b 0a  ease((Mem *)v);.
19e0e 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
19e0f 28 28 4d 65 6d 2a 29 76 29 2d 3e 64 62 2c 20 76  ((Mem*)v)->db, v
19e10 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
19e11 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
19e12 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 73 71   bytes in the sq
19e13 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
19e14 63 74 20 61 73 73 75 6d 69 6e 67 0a 2a 2a 20 74  ct assuming.** t
19e15 68 61 74 20 69 74 20 75 73 65 73 20 74 68 65 20  hat it uses the 
19e16 65 6e 63 6f 64 69 6e 67 20 22 65 6e 63 22 0a 2a  encoding "enc".*
19e17 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19e18 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75   int sqlite3Valu
19e19 65 42 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76  eBytes(sqlite3_v
19e1a 61 6c 75 65 20 2a 70 56 61 6c 2c 20 75 38 20 65  alue *pVal, u8 e
19e1b 6e 63 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20  nc){.  Mem *p = 
19e1c 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 69 66  (Mem*)pVal;.  if
19e1d 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45  ( (p->flags & ME
19e1e 4d 5f 42 6c 6f 62 29 21 3d 30 20 7c 7c 20 73 71  M_Blob)!=0 || sq
19e1f 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70  lite3ValueText(p
19e20 56 61 6c 2c 20 65 6e 63 29 20 29 7b 0a 20 20 20  Val, enc) ){.   
19e21 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20   if( p->flags & 
19e22 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20  MEM_Zero ){.    
19e23 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 20 2b 20    return p->n + 
19e24 70 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20  p->u.nZero;.    
19e25 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74  }else{.      ret
19e26 75 72 6e 20 70 2d 3e 6e 3b 0a 20 20 20 20 7d 0a  urn p->n;.    }.
19e27 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
19e28 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
19e29 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 6d 65  ** End of vdbeme
19e2a 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  m.c ************
19e2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19e2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19e2d 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
19e2e 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64  ** Begin file vd
19e2f 62 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  beaux.c ********
19e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19e31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19e32 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65  */./*.** 2003 Se
19e33 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20  ptember 6.**.** 
19e34 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
19e35 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
19e36 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
19e37 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
19e38 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
19e39 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
19e3a 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
19e3b 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
19e3c 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
19e3d 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
19e3e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
19e3f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
19e40 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
19e41 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
19e42 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
19e43 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
19e44 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
19e45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19e46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19e47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19e48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19e49 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
19e4a 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
19e4b 6f 64 65 20 75 73 65 64 20 66 6f 72 20 63 72 65  ode used for cre
19e4c 61 74 69 6e 67 2c 20 64 65 73 74 72 6f 79 69 6e  ating, destroyin
19e4d 67 2c 20 61 6e 64 20 70 6f 70 75 6c 61 74 69 6e  g, and populatin
19e4e 67 0a 2a 2a 20 61 20 56 44 42 45 20 28 6f 72 20  g.** a VDBE (or 
19e4f 61 6e 20 22 73 71 6c 69 74 65 33 5f 73 74 6d 74  an "sqlite3_stmt
19e50 22 20 61 73 20 69 74 20 69 73 20 6b 6e 6f 77 6e  " as it is known
19e51 20 74 6f 20 74 68 65 20 6f 75 74 73 69 64 65 20   to the outside 
19e52 77 6f 72 6c 64 2e 29 20 20 50 72 69 6f 72 0a 2a  world.)  Prior.*
19e53 2a 20 74 6f 20 76 65 72 73 69 6f 6e 20 32 2e 38  * to version 2.8
19e54 2e 37 2c 20 61 6c 6c 20 74 68 69 73 20 63 6f 64  .7, all this cod
19e55 65 20 77 61 73 20 63 6f 6d 62 69 6e 65 64 20 69  e was combined i
19e56 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 63 20 73  nto the vdbe.c s
19e57 6f 75 72 63 65 20 66 69 6c 65 2e 0a 2a 2a 20 42  ource file..** B
19e58 75 74 20 74 68 61 74 20 66 69 6c 65 20 77 61 73  ut that file was
19e59 20 67 65 74 74 69 6e 67 20 74 6f 6f 20 62 69 67   getting too big
19e5a 20 73 6f 20 74 68 69 73 20 73 75 62 72 6f 75 74   so this subrout
19e5b 69 6e 65 73 20 77 65 72 65 20 73 70 6c 69 74 20  ines were split 
19e5c 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  out..**.** $Id: 
19e5d 76 64 62 65 61 75 78 2e 63 2c 76 20 31 2e 34 38  vdbeaux.c,v 1.48
19e5e 30 20 32 30 30 39 2f 30 38 2f 30 38 20 31 38 3a  0 2009/08/08 18:
19e5f 30 31 3a 30 38 20 64 72 68 20 45 78 70 20 24 0a  01:08 drh Exp $.
19e60 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e  */..../*.** When
19e61 20 64 65 62 75 67 67 69 6e 67 20 74 68 65 20 63   debugging the c
19e62 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 69 6e  ode generator in
19e63 20 61 20 73 79 6d 62 6f 6c 69 63 20 64 65 62 75   a symbolic debu
19e64 67 67 65 72 2c 20 6f 6e 65 20 63 61 6e 0a 2a 2a  gger, one can.**
19e65 20 73 65 74 20 74 68 65 20 73 71 6c 69 74 65 33   set the sqlite3
19e66 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20 74  VdbeAddopTrace t
19e67 6f 20 31 20 61 6e 64 20 61 6c 6c 20 6f 70 63 6f  o 1 and all opco
19e68 64 65 73 20 77 69 6c 6c 20 62 65 20 70 72 69 6e  des will be prin
19e69 74 65 64 0a 2a 2a 20 61 73 20 74 68 65 79 20 61  ted.** as they a
19e6a 72 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  re added to the 
19e6b 69 6e 73 74 72 75 63 74 69 6f 6e 20 73 74 72 65  instruction stre
19e6c 61 6d 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  am..*/.#ifdef SQ
19e6d 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54  LITE_DEBUG.SQLIT
19e6e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
19e6f 6c 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 72  lite3VdbeAddopTr
19e70 61 63 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  ace = 0;.#endif.
19e71 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
19e72 20 6e 65 77 20 76 69 72 74 75 61 6c 20 64 61 74   new virtual dat
19e73 61 62 61 73 65 20 65 6e 67 69 6e 65 2e 0a 2a 2f  abase engine..*/
19e74 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
19e75 56 64 62 65 20 2a 73 71 6c 69 74 65 33 56 64 62  Vdbe *sqlite3Vdb
19e76 65 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 20  eCreate(sqlite3 
19e77 2a 64 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b  *db){.  Vdbe *p;
19e78 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 44 62  .  p = sqlite3Db
19e79 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
19e7a 69 7a 65 6f 66 28 56 64 62 65 29 20 29 3b 0a 20  izeof(Vdbe) );. 
19e7b 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
19e7c 72 6e 20 30 3b 0a 20 20 70 2d 3e 64 62 20 3d 20  rn 0;.  p->db = 
19e7d 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 70 56  db;.  if( db->pV
19e7e 64 62 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 70  dbe ){.    db->p
19e7f 56 64 62 65 2d 3e 70 50 72 65 76 20 3d 20 70 3b  Vdbe->pPrev = p;
19e80 0a 20 20 7d 0a 20 20 70 2d 3e 70 4e 65 78 74 20  .  }.  p->pNext 
19e81 3d 20 64 62 2d 3e 70 56 64 62 65 3b 0a 20 20 70  = db->pVdbe;.  p
19e82 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 64  ->pPrev = 0;.  d
19e83 62 2d 3e 70 56 64 62 65 20 3d 20 70 3b 0a 20 20  b->pVdbe = p;.  
19e84 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f  p->magic = VDBE_
19e85 4d 41 47 49 43 5f 49 4e 49 54 3b 0a 20 20 72 65  MAGIC_INIT;.  re
19e86 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
19e87 20 52 65 6d 65 6d 62 65 72 20 74 68 65 20 53 51   Remember the SQ
19e88 4c 20 73 74 72 69 6e 67 20 66 6f 72 20 61 20 70  L string for a p
19e89 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
19e8a 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
19e8b 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
19e8c 33 56 64 62 65 53 65 74 53 71 6c 28 56 64 62 65  3VdbeSetSql(Vdbe
19e8d 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   *p, const char 
19e8e 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69  *z, int n, int i
19e8f 73 50 72 65 70 61 72 65 56 32 29 7b 0a 20 20 69  sPrepareV2){.  i
19e90 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
19e91 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
19e92 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 69 66 28  OMIT_TRACE.  if(
19e93 20 21 69 73 50 72 65 70 61 72 65 56 32 20 29 20   !isPrepareV2 ) 
19e94 72 65 74 75 72 6e 3b 0a 23 65 6e 64 69 66 0a 20  return;.#endif. 
19e95 20 61 73 73 65 72 74 28 20 70 2d 3e 7a 53 71 6c   assert( p->zSql
19e96 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e 7a 53 71 6c  ==0 );.  p->zSql
19e97 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e   = sqlite3DbStrN
19e98 44 75 70 28 70 2d 3e 64 62 2c 20 7a 2c 20 6e 29  Dup(p->db, z, n)
19e99 3b 0a 20 20 70 2d 3e 69 73 50 72 65 70 61 72 65  ;.  p->isPrepare
19e9a 56 32 20 3d 20 69 73 50 72 65 70 61 72 65 56 32  V2 = isPrepareV2
19e9b 20 3f 20 31 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a   ? 1 : 0;.}../*.
19e9c 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 53 51  ** Return the SQ
19e9d 4c 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  L associated wit
19e9e 68 20 61 20 70 72 65 70 61 72 65 64 20 73 74 61  h a prepared sta
19e9f 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45  tement.*/.SQLITE
19ea0 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
19ea1 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28 73 71 6c  *sqlite3_sql(sql
19ea2 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
19ea3 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  ){.  Vdbe *p = (
19ea4 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20  Vdbe *)pStmt;.  
19ea5 72 65 74 75 72 6e 20 28 70 2d 3e 69 73 50 72 65  return (p->isPre
19ea6 70 61 72 65 56 32 20 3f 20 70 2d 3e 7a 53 71 6c  pareV2 ? p->zSql
19ea7 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   : 0);.}../*.** 
19ea8 53 77 61 70 20 61 6c 6c 20 63 6f 6e 74 65 6e 74  Swap all content
19ea9 20 62 65 74 77 65 65 6e 20 74 77 6f 20 56 44 42   between two VDB
19eaa 45 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f  E structures..*/
19eab 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
19eac 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
19ead 53 77 61 70 28 56 64 62 65 20 2a 70 41 2c 20 56  Swap(Vdbe *pA, V
19eae 64 62 65 20 2a 70 42 29 7b 0a 20 20 56 64 62 65  dbe *pB){.  Vdbe
19eaf 20 74 6d 70 2c 20 2a 70 54 6d 70 3b 0a 20 20 63   tmp, *pTmp;.  c
19eb0 68 61 72 20 2a 7a 54 6d 70 3b 0a 20 20 74 6d 70  har *zTmp;.  tmp
19eb1 20 3d 20 2a 70 41 3b 0a 20 20 2a 70 41 20 3d 20   = *pA;.  *pA = 
19eb2 2a 70 42 3b 0a 20 20 2a 70 42 20 3d 20 74 6d 70  *pB;.  *pB = tmp
19eb3 3b 0a 20 20 70 54 6d 70 20 3d 20 70 41 2d 3e 70  ;.  pTmp = pA->p
19eb4 4e 65 78 74 3b 0a 20 20 70 41 2d 3e 70 4e 65 78  Next;.  pA->pNex
19eb5 74 20 3d 20 70 42 2d 3e 70 4e 65 78 74 3b 0a 20  t = pB->pNext;. 
19eb6 20 70 42 2d 3e 70 4e 65 78 74 20 3d 20 70 54 6d   pB->pNext = pTm
19eb7 70 3b 0a 20 20 70 54 6d 70 20 3d 20 70 41 2d 3e  p;.  pTmp = pA->
19eb8 70 50 72 65 76 3b 0a 20 20 70 41 2d 3e 70 50 72  pPrev;.  pA->pPr
19eb9 65 76 20 3d 20 70 42 2d 3e 70 50 72 65 76 3b 0a  ev = pB->pPrev;.
19eba 20 20 70 42 2d 3e 70 50 72 65 76 20 3d 20 70 54    pB->pPrev = pT
19ebb 6d 70 3b 0a 20 20 7a 54 6d 70 20 3d 20 70 41 2d  mp;.  zTmp = pA-
19ebc 3e 7a 53 71 6c 3b 0a 20 20 70 41 2d 3e 7a 53 71  >zSql;.  pA->zSq
19ebd 6c 20 3d 20 70 42 2d 3e 7a 53 71 6c 3b 0a 20 20  l = pB->zSql;.  
19ebe 70 42 2d 3e 7a 53 71 6c 20 3d 20 7a 54 6d 70 3b  pB->zSql = zTmp;
19ebf 0a 20 20 70 42 2d 3e 69 73 50 72 65 70 61 72 65  .  pB->isPrepare
19ec0 56 32 20 3d 20 70 41 2d 3e 69 73 50 72 65 70 61  V2 = pA->isPrepa
19ec1 72 65 56 32 3b 0a 7d 0a 0a 23 69 66 64 65 66 20  reV2;.}..#ifdef 
19ec2 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a  SQLITE_DEBUG./*.
19ec3 2a 2a 20 54 75 72 6e 20 74 72 61 63 69 6e 67 20  ** Turn tracing 
19ec4 6f 6e 20 6f 72 20 6f 66 66 0a 2a 2f 0a 53 51 4c  on or off.*/.SQL
19ec5 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
19ec6 20 73 71 6c 69 74 65 33 56 64 62 65 54 72 61 63   sqlite3VdbeTrac
19ec7 65 28 56 64 62 65 20 2a 70 2c 20 46 49 4c 45 20  e(Vdbe *p, FILE 
19ec8 2a 74 72 61 63 65 29 7b 0a 20 20 70 2d 3e 74 72  *trace){.  p->tr
19ec9 61 63 65 20 3d 20 74 72 61 63 65 3b 0a 7d 0a 23  ace = trace;.}.#
19eca 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 73  endif../*.** Res
19ecb 69 7a 65 20 74 68 65 20 56 64 62 65 2e 61 4f 70  ize the Vdbe.aOp
19ecc 20 61 72 72 61 79 20 73 6f 20 74 68 61 74 20 69   array so that i
19ecd 74 20 69 73 20 61 74 20 6c 65 61 73 74 20 6f 6e  t is at least on
19ece 65 20 6f 70 20 6c 61 72 67 65 72 20 74 68 61 6e  e op larger than
19ecf 20 0a 2a 2a 20 69 74 20 77 61 73 2e 0a 2a 2a 0a   .** it was..**.
19ed0 2a 2a 20 49 66 20 61 6e 20 6f 75 74 2d 6f 66 2d  ** If an out-of-
19ed1 6d 65 6d 6f 72 79 20 65 72 72 6f 72 20 6f 63 63  memory error occ
19ed2 75 72 73 20 77 68 69 6c 65 20 72 65 73 69 7a 69  urs while resizi
19ed3 6e 67 20 74 68 65 20 61 72 72 61 79 2c 20 72 65  ng the array, re
19ed4 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e  turn.** SQLITE_N
19ed5 4f 4d 45 4d 2e 20 49 6e 20 74 68 69 73 20 63 61  OMEM. In this ca
19ed6 73 65 20 56 64 62 65 2e 61 4f 70 20 61 6e 64 20  se Vdbe.aOp and 
19ed7 56 64 62 65 2e 6e 4f 70 41 6c 6c 6f 63 20 72 65  Vdbe.nOpAlloc re
19ed8 6d 61 69 6e 20 0a 2a 2a 20 75 6e 63 68 61 6e 67  main .** unchang
19ed9 65 64 20 28 74 68 69 73 20 69 73 20 73 6f 20 74  ed (this is so t
19eda 68 61 74 20 61 6e 79 20 6f 70 63 6f 64 65 73 20  hat any opcodes 
19edb 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65  already allocate
19edc 64 20 63 61 6e 20 62 65 20 0a 2a 2a 20 63 6f 72  d can be .** cor
19edd 72 65 63 74 6c 79 20 64 65 61 6c 6c 6f 63 61 74  rectly deallocat
19ede 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68  ed along with th
19edf 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 56 64  e rest of the Vd
19ee0 62 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  be)..*/.static i
19ee1 6e 74 20 67 72 6f 77 4f 70 41 72 72 61 79 28 56  nt growOpArray(V
19ee2 64 62 65 20 2a 70 29 7b 0a 20 20 56 64 62 65 4f  dbe *p){.  VdbeO
19ee3 70 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 6e  p *pNew;.  int n
19ee4 4e 65 77 20 3d 20 28 70 2d 3e 6e 4f 70 41 6c 6c  New = (p->nOpAll
19ee5 6f 63 20 3f 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63  oc ? p->nOpAlloc
19ee6 2a 32 20 3a 20 28 69 6e 74 29 28 31 30 32 34 2f  *2 : (int)(1024/
19ee7 73 69 7a 65 6f 66 28 4f 70 29 29 29 3b 0a 20 20  sizeof(Op)));.  
19ee8 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  pNew = sqlite3Db
19ee9 52 65 61 6c 6c 6f 63 28 70 2d 3e 64 62 2c 20 70  Realloc(p->db, p
19eea 2d 3e 61 4f 70 2c 20 6e 4e 65 77 2a 73 69 7a 65  ->aOp, nNew*size
19eeb 6f 66 28 4f 70 29 29 3b 0a 20 20 69 66 28 20 70  of(Op));.  if( p
19eec 4e 65 77 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 4f  New ){.    p->nO
19eed 70 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33  pAlloc = sqlite3
19eee 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e  DbMallocSize(p->
19eef 64 62 2c 20 70 4e 65 77 29 2f 73 69 7a 65 6f 66  db, pNew)/sizeof
19ef0 28 4f 70 29 3b 0a 20 20 20 20 70 2d 3e 61 4f 70  (Op);.    p->aOp
19ef1 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 72   = pNew;.  }.  r
19ef2 65 74 75 72 6e 20 28 70 4e 65 77 20 3f 20 53 51  eturn (pNew ? SQ
19ef3 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45  LITE_OK : SQLITE
19ef4 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  _NOMEM);.}../*.*
19ef5 2a 20 41 64 64 20 61 20 6e 65 77 20 69 6e 73 74  * Add a new inst
19ef6 72 75 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 6c  ruction to the l
19ef7 69 73 74 20 6f 66 20 69 6e 73 74 72 75 63 74 69  ist of instructi
19ef8 6f 6e 73 20 63 75 72 72 65 6e 74 20 69 6e 20 74  ons current in t
19ef9 68 65 0a 2a 2a 20 56 44 42 45 2e 20 20 52 65 74  he.** VDBE.  Ret
19efa 75 72 6e 20 74 68 65 20 61 64 64 72 65 73 73 20  urn the address 
19efb 6f 66 20 74 68 65 20 6e 65 77 20 69 6e 73 74 72  of the new instr
19efc 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 50 61  uction..**.** Pa
19efd 72 61 6d 65 74 65 72 73 3a 0a 2a 2a 0a 2a 2a 20  rameters:.**.** 
19efe 20 20 20 70 20 20 20 20 20 20 20 20 20 20 20 20     p            
19eff 20 20 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68     Pointer to th
19f00 65 20 56 44 42 45 0a 2a 2a 0a 2a 2a 20 20 20 20  e VDBE.**.**    
19f01 6f 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20  op              
19f02 54 68 65 20 6f 70 63 6f 64 65 20 66 6f 72 20 74  The opcode for t
19f03 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a  his instruction.
19f04 2a 2a 0a 2a 2a 20 20 20 20 70 31 2c 20 70 32 2c  **.**    p1, p2,
19f05 20 70 33 20 20 20 20 20 20 4f 70 65 72 61 6e 64   p3      Operand
19f06 73 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20  s.**.** Use the 
19f07 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
19f08 76 65 4c 61 62 65 6c 28 29 20 66 75 6e 63 74 69  veLabel() functi
19f09 6f 6e 20 74 6f 20 66 69 78 20 61 6e 20 61 64 64  on to fix an add
19f0a 72 65 73 73 20 61 6e 64 0a 2a 2a 20 74 68 65 20  ress and.** the 
19f0b 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
19f0c 65 50 34 28 29 20 66 75 6e 63 74 69 6f 6e 20 74  eP4() function t
19f0d 6f 20 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c  o change the val
19f0e 75 65 20 6f 66 20 74 68 65 20 50 34 0a 2a 2a 20  ue of the P4.** 
19f0f 6f 70 65 72 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49  operand..*/.SQLI
19f10 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
19f11 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
19f12 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70  (Vdbe *p, int op
19f13 2c 20 69 6e 74 20 70 31 2c 20 69 6e 74 20 70 32  , int p1, int p2
19f14 2c 20 69 6e 74 20 70 33 29 7b 0a 20 20 69 6e 74  , int p3){.  int
19f15 20 69 3b 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f   i;.  VdbeOp *pO
19f16 70 3b 0a 0a 20 20 69 20 3d 20 70 2d 3e 6e 4f 70  p;..  i = p->nOp
19f17 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d  ;.  assert( p->m
19f18 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
19f19 5f 49 4e 49 54 20 29 3b 0a 20 20 61 73 73 65 72  _INIT );.  asser
19f1a 74 28 20 6f 70 3e 30 20 26 26 20 6f 70 3c 30 78  t( op>0 && op<0x
19f1b 66 66 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e  ff );.  if( p->n
19f1c 4f 70 41 6c 6c 6f 63 3c 3d 69 20 29 7b 0a 20 20  OpAlloc<=i ){.  
19f1d 20 20 69 66 28 20 67 72 6f 77 4f 70 41 72 72 61    if( growOpArra
19f1e 79 28 70 29 20 29 7b 0a 20 20 20 20 20 20 72 65  y(p) ){.      re
19f1f 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
19f20 7d 0a 20 20 70 2d 3e 6e 4f 70 2b 2b 3b 0a 20 20  }.  p->nOp++;.  
19f21 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d  pOp = &p->aOp[i]
19f22 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20  ;.  pOp->opcode 
19f23 3d 20 28 75 38 29 6f 70 3b 0a 20 20 70 4f 70 2d  = (u8)op;.  pOp-
19f24 3e 70 35 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e  >p5 = 0;.  pOp->
19f25 70 31 20 3d 20 70 31 3b 0a 20 20 70 4f 70 2d 3e  p1 = p1;.  pOp->
19f26 70 32 20 3d 20 70 32 3b 0a 20 20 70 4f 70 2d 3e  p2 = p2;.  pOp->
19f27 70 33 20 3d 20 70 33 3b 0a 20 20 70 4f 70 2d 3e  p3 = p3;.  pOp->
19f28 70 34 2e 70 20 3d 20 30 3b 0a 20 20 70 4f 70 2d  p4.p = 0;.  pOp-
19f29 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54  >p4type = P4_NOT
19f2a 55 53 45 44 3b 0a 20 20 70 2d 3e 65 78 70 69 72  USED;.  p->expir
19f2b 65 64 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 53  ed = 0;.#ifdef S
19f2c 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 70 4f  QLITE_DEBUG.  pO
19f2d 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b  p->zComment = 0;
19f2e 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64  .  if( sqlite3Vd
19f2f 62 65 41 64 64 6f 70 54 72 61 63 65 20 29 20 73  beAddopTrace ) s
19f30 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f  qlite3VdbePrintO
19f31 70 28 30 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b  p(0, i, &p->aOp[
19f32 69 5d 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64  i]);.#endif.#ifd
19f33 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a  ef VDBE_PROFILE.
19f34 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73 20 3d 20    pOp->cycles = 
19f35 30 3b 0a 20 20 70 4f 70 2d 3e 63 6e 74 20 3d 20  0;.  pOp->cnt = 
19f36 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75  0;.#endif.  retu
19f37 72 6e 20 69 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  rn i;.}.SQLITE_P
19f38 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19f39 65 33 56 64 62 65 41 64 64 4f 70 30 28 56 64 62  e3VdbeAddOp0(Vdb
19f3a 65 20 2a 70 2c 20 69 6e 74 20 6f 70 29 7b 0a 20  e *p, int op){. 
19f3b 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56   return sqlite3V
19f3c 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c  dbeAddOp3(p, op,
19f3d 20 30 2c 20 30 2c 20 30 29 3b 0a 7d 0a 53 51 4c   0, 0, 0);.}.SQL
19f3e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
19f3f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
19f40 31 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6f  1(Vdbe *p, int o
19f41 70 2c 20 69 6e 74 20 70 31 29 7b 0a 20 20 72 65  p, int p1){.  re
19f42 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65  turn sqlite3Vdbe
19f43 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70 31  AddOp3(p, op, p1
19f44 2c 20 30 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54  , 0, 0);.}.SQLIT
19f45 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
19f46 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
19f47 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c  Vdbe *p, int op,
19f48 20 69 6e 74 20 70 31 2c 20 69 6e 74 20 70 32 29   int p1, int p2)
19f49 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
19f4a 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20  e3VdbeAddOp3(p, 
19f4b 6f 70 2c 20 70 31 2c 20 70 32 2c 20 30 29 3b 0a  op, p1, p2, 0);.
19f4c 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e  }.../*.** Add an
19f4d 20 6f 70 63 6f 64 65 20 74 68 61 74 20 69 6e 63   opcode that inc
19f4e 6c 75 64 65 73 20 74 68 65 20 70 34 20 76 61 6c  ludes the p4 val
19f4f 75 65 20 61 73 20 61 20 70 6f 69 6e 74 65 72 2e  ue as a pointer.
19f50 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19f51 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
19f52 62 65 41 64 64 4f 70 34 28 0a 20 20 56 64 62 65  beAddOp4(.  Vdbe
19f53 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
19f54 2f 2a 20 41 64 64 20 74 68 65 20 6f 70 63 6f 64  /* Add the opcod
19f55 65 20 74 6f 20 74 68 69 73 20 56 4d 20 2a 2f 0a  e to this VM */.
19f56 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20    int op,       
19f57 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77        /* The new
19f58 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74   opcode */.  int
19f59 20 70 31 2c 20 20 20 20 20 20 20 20 20 20 20 20   p1,            
19f5a 20 2f 2a 20 54 68 65 20 50 31 20 6f 70 65 72 61   /* The P1 opera
19f5b 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 32 2c 20  nd */.  int p2, 
19f5c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
19f5d 68 65 20 50 32 20 6f 70 65 72 61 6e 64 20 2a 2f  he P2 operand */
19f5e 0a 20 20 69 6e 74 20 70 33 2c 20 20 20 20 20 20  .  int p3,      
19f5f 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 33         /* The P3
19f60 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 63 6f   operand */.  co
19f61 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 2c 20 20  nst char *zP4,  
19f62 20 20 2f 2a 20 54 68 65 20 50 34 20 6f 70 65 72    /* The P4 oper
19f63 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 34 74  and */.  int p4t
19f64 79 70 65 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ype          /* 
19f65 50 34 20 6f 70 65 72 61 6e 64 20 74 79 70 65 20  P4 operand type 
19f66 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 64 64 72  */.){.  int addr
19f67 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
19f68 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20  dOp3(p, op, p1, 
19f69 70 32 2c 20 70 33 29 3b 0a 20 20 73 71 6c 69 74  p2, p3);.  sqlit
19f6a 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 70  e3VdbeChangeP4(p
19f6b 2c 20 61 64 64 72 2c 20 7a 50 34 2c 20 70 34 74  , addr, zP4, p4t
19f6c 79 70 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 61  ype);.  return a
19f6d 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72  ddr;.}../*.** Cr
19f6e 65 61 74 65 20 61 20 6e 65 77 20 73 79 6d 62 6f  eate a new symbo
19f6f 6c 69 63 20 6c 61 62 65 6c 20 66 6f 72 20 61 6e  lic label for an
19f70 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61   instruction tha
19f71 74 20 68 61 73 20 79 65 74 20 74 6f 20 62 65 0a  t has yet to be.
19f72 2a 2a 20 63 6f 64 65 64 2e 20 20 54 68 65 20 73  ** coded.  The s
19f73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20 69 73  ymbolic label is
19f74 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 61 20 6e   really just a n
19f75 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 2e 20  egative number. 
19f76 20 54 68 65 0a 2a 2a 20 6c 61 62 65 6c 20 63 61   The.** label ca
19f77 6e 20 62 65 20 75 73 65 64 20 61 73 20 74 68 65  n be used as the
19f78 20 50 32 20 76 61 6c 75 65 20 6f 66 20 61 6e 20   P2 value of an 
19f79 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4c 61 74 65  operation.  Late
19f7a 72 2c 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 6c  r, when.** the l
19f7b 61 62 65 6c 20 69 73 20 72 65 73 6f 6c 76 65 64  abel is resolved
19f7c 20 74 6f 20 61 20 73 70 65 63 69 66 69 63 20 61   to a specific a
19f7d 64 64 72 65 73 73 2c 20 74 68 65 20 56 44 42 45  ddress, the VDBE
19f7e 20 77 69 6c 6c 20 73 63 61 6e 0a 2a 2a 20 74 68   will scan.** th
19f7f 72 6f 75 67 68 20 69 74 73 20 6f 70 65 72 61 74  rough its operat
19f80 69 6f 6e 20 6c 69 73 74 20 61 6e 64 20 63 68 61  ion list and cha
19f81 6e 67 65 20 61 6c 6c 20 76 61 6c 75 65 73 20 6f  nge all values o
19f82 66 20 50 32 20 77 68 69 63 68 20 6d 61 74 63 68  f P2 which match
19f83 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c 20 69 6e  .** the label in
19f84 74 6f 20 74 68 65 20 72 65 73 6f 6c 76 65 64 20  to the resolved 
19f85 61 64 64 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54  address..**.** T
19f86 68 65 20 56 44 42 45 20 6b 6e 6f 77 73 20 74 68  he VDBE knows th
19f87 61 74 20 61 20 50 32 20 76 61 6c 75 65 20 69 73  at a P2 value is
19f88 20 61 20 6c 61 62 65 6c 20 62 65 63 61 75 73 65   a label because
19f89 20 6c 61 62 65 6c 73 20 61 72 65 0a 2a 2a 20 61   labels are.** a
19f8a 6c 77 61 79 73 20 6e 65 67 61 74 69 76 65 20 61  lways negative a
19f8b 6e 64 20 50 32 20 76 61 6c 75 65 73 20 61 72 65  nd P2 values are
19f8c 20 73 75 70 70 6f 73 65 20 74 6f 20 62 65 20 6e   suppose to be n
19f8d 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 20  on-negative..** 
19f8e 48 65 6e 63 65 2c 20 61 20 6e 65 67 61 74 69 76  Hence, a negativ
19f8f 65 20 50 32 20 76 61 6c 75 65 20 69 73 20 61 20  e P2 value is a 
19f90 6c 61 62 65 6c 20 74 68 61 74 20 68 61 73 20 79  label that has y
19f91 65 74 20 74 6f 20 62 65 20 72 65 73 6f 6c 76 65  et to be resolve
19f92 64 2e 0a 2a 2a 0a 2a 2a 20 5a 65 72 6f 20 69 73  d..**.** Zero is
19f93 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20 6d   returned if a m
19f94 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a  alloc() fails..*
19f95 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
19f96 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
19f97 4d 61 6b 65 4c 61 62 65 6c 28 56 64 62 65 20 2a  MakeLabel(Vdbe *
19f98 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  p){.  int i;.  i
19f99 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 2b 2b 3b 0a   = p->nLabel++;.
19f9a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67    assert( p->mag
19f9b 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49  ic==VDBE_MAGIC_I
19f9c 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 69 3e 3d  NIT );.  if( i>=
19f9d 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 20 29  p->nLabelAlloc )
19f9e 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 2d  {.    int n = p-
19f9f 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 2a 32 20 2b  >nLabelAlloc*2 +
19fa0 20 35 3b 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65   5;.    p->aLabe
19fa1 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  l = sqlite3DbRea
19fa2 6c 6c 6f 63 4f 72 46 72 65 65 28 70 2d 3e 64 62  llocOrFree(p->db
19fa3 2c 20 70 2d 3e 61 4c 61 62 65 6c 2c 0a 20 20 20  , p->aLabel,.   
19fa4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19fa5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19fa6 20 20 20 20 6e 2a 73 69 7a 65 6f 66 28 70 2d 3e      n*sizeof(p->
19fa7 61 4c 61 62 65 6c 5b 30 5d 29 29 3b 0a 20 20 20  aLabel[0]));.   
19fa8 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 20   p->nLabelAlloc 
19fa9 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
19faa 63 53 69 7a 65 28 70 2d 3e 64 62 2c 20 70 2d 3e  cSize(p->db, p->
19fab 61 4c 61 62 65 6c 29 2f 73 69 7a 65 6f 66 28 70  aLabel)/sizeof(p
19fac 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29 3b 0a 20 20  ->aLabel[0]);.  
19fad 7d 0a 20 20 69 66 28 20 70 2d 3e 61 4c 61 62 65  }.  if( p->aLabe
19fae 6c 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4c 61 62  l ){.    p->aLab
19faf 65 6c 5b 69 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a  el[i] = -1;.  }.
19fb0 20 20 72 65 74 75 72 6e 20 2d 31 2d 69 3b 0a 7d    return -1-i;.}
19fb1 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20  ../*.** Resolve 
19fb2 6c 61 62 65 6c 20 22 78 22 20 74 6f 20 62 65 20  label "x" to be 
19fb3 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74  the address of t
19fb4 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74  he next instruct
19fb5 69 6f 6e 20 74 6f 0a 2a 2a 20 62 65 20 69 6e 73  ion to.** be ins
19fb6 65 72 74 65 64 2e 20 20 54 68 65 20 70 61 72 61  erted.  The para
19fb7 6d 65 74 65 72 20 22 78 22 20 6d 75 73 74 20 68  meter "x" must h
19fb8 61 76 65 20 62 65 65 6e 20 6f 62 74 61 69 6e 65  ave been obtaine
19fb9 64 20 66 72 6f 6d 0a 2a 2a 20 61 20 70 72 69 6f  d from.** a prio
19fba 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  r call to sqlite
19fbb 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 29  3VdbeMakeLabel()
19fbc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19fbd 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
19fbe 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
19fbf 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 78 29  (Vdbe *p, int x)
19fc0 7b 0a 20 20 69 6e 74 20 6a 20 3d 20 2d 31 2d 78  {.  int j = -1-x
19fc1 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d  ;.  assert( p->m
19fc2 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
19fc3 5f 49 4e 49 54 20 29 3b 0a 20 20 61 73 73 65 72  _INIT );.  asser
19fc4 74 28 20 6a 3e 3d 30 20 26 26 20 6a 3c 70 2d 3e  t( j>=0 && j<p->
19fc5 6e 4c 61 62 65 6c 20 29 3b 0a 20 20 69 66 28 20  nLabel );.  if( 
19fc6 70 2d 3e 61 4c 61 62 65 6c 20 29 7b 0a 20 20 20  p->aLabel ){.   
19fc7 20 70 2d 3e 61 4c 61 62 65 6c 5b 6a 5d 20 3d 20   p->aLabel[j] = 
19fc8 70 2d 3e 6e 4f 70 3b 0a 20 20 7d 0a 7d 0a 0a 23  p->nOp;.  }.}..#
19fc9 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
19fca 55 47 20 2f 2a 20 73 71 6c 69 74 65 33 41 73 73  UG /* sqlite3Ass
19fcb 65 72 74 4d 61 79 41 62 6f 72 74 28 29 20 6c 6f  ertMayAbort() lo
19fcc 67 69 63 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  gic */../*.** Th
19fcd 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 79 70 65  e following type
19fce 20 61 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61 72   and function ar
19fcf 65 20 75 73 65 64 20 74 6f 20 69 74 65 72 61 74  e used to iterat
19fd0 65 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 6f 70  e through all op
19fd1 63 6f 64 65 73 0a 2a 2a 20 69 6e 20 61 20 56 64  codes.** in a Vd
19fd2 62 65 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 20  be main program 
19fd3 61 6e 64 20 65 61 63 68 20 6f 66 20 74 68 65 20  and each of the 
19fd4 73 75 62 2d 70 72 6f 67 72 61 6d 73 20 28 74 72  sub-programs (tr
19fd5 69 67 67 65 72 73 29 20 69 74 20 6d 61 79 20 0a  iggers) it may .
19fd6 2a 2a 20 69 6e 76 6f 6b 65 20 64 69 72 65 63 74  ** invoke direct
19fd7 6c 79 20 6f 72 20 69 6e 64 69 72 65 63 74 6c 79  ly or indirectly
19fd8 2e 20 49 74 20 73 68 6f 75 6c 64 20 62 65 20 75  . It should be u
19fd9 73 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  sed as follows:.
19fda 2a 2a 0a 2a 2a 20 20 20 4f 70 20 2a 70 4f 70 3b  **.**   Op *pOp;
19fdb 0a 2a 2a 20 20 20 56 64 62 65 4f 70 49 74 65 72  .**   VdbeOpIter
19fdc 20 73 49 74 65 72 3b 0a 2a 2a 0a 2a 2a 20 20 20   sIter;.**.**   
19fdd 6d 65 6d 73 65 74 28 26 73 49 74 65 72 2c 20 30  memset(&sIter, 0
19fde 2c 20 73 69 7a 65 6f 66 28 73 49 74 65 72 29 29  , sizeof(sIter))
19fdf 3b 0a 2a 2a 20 20 20 73 49 74 65 72 2e 76 20 3d  ;.**   sIter.v =
19fe0 20 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   v;             
19fe1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
19fe2 2f 20 76 20 69 73 20 6f 66 20 74 79 70 65 20 56  / v is of type V
19fe3 64 62 65 2a 20 0a 2a 2a 20 20 20 77 68 69 6c 65  dbe* .**   while
19fe4 28 20 28 70 4f 70 20 3d 20 6f 70 49 74 65 72 4e  ( (pOp = opIterN
19fe5 65 78 74 28 26 73 49 74 65 72 29 29 20 29 7b 0a  ext(&sIter)) ){.
19fe6 2a 2a 20 20 20 20 20 2f 2f 20 44 6f 20 73 6f 6d  **     // Do som
19fe7 65 74 68 69 6e 67 20 77 69 74 68 20 70 4f 70 0a  ething with pOp.
19fe8 2a 2a 20 20 20 7d 0a 2a 2a 20 20 20 73 71 6c 69  **   }.**   sqli
19fe9 74 65 33 44 62 46 72 65 65 28 76 2d 3e 64 62 2c  te3DbFree(v->db,
19fea 20 73 49 74 65 72 2e 61 70 53 75 62 29 3b 0a 2a   sIter.apSub);.*
19feb 2a 20 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  * .*/.typedef st
19fec 72 75 63 74 20 56 64 62 65 4f 70 49 74 65 72 20  ruct VdbeOpIter 
19fed 56 64 62 65 4f 70 49 74 65 72 3b 0a 73 74 72 75  VdbeOpIter;.stru
19fee 63 74 20 56 64 62 65 4f 70 49 74 65 72 20 7b 0a  ct VdbeOpIter {.
19fef 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20    Vdbe *v;      
19ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19ff1 56 64 62 65 20 74 6f 20 69 74 65 72 61 74 65 20  Vdbe to iterate 
19ff2 74 68 72 6f 75 67 68 20 74 68 65 20 6f 70 63 6f  through the opco
19ff3 64 65 73 20 6f 66 20 2a 2f 0a 20 20 53 75 62 50  des of */.  SubP
19ff4 72 6f 67 72 61 6d 20 2a 2a 61 70 53 75 62 3b 20  rogram **apSub; 
19ff5 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20         /* Array 
19ff6 6f 66 20 73 75 62 70 72 6f 67 72 61 6d 73 20 2a  of subprograms *
19ff7 2f 0a 20 20 69 6e 74 20 6e 53 75 62 3b 20 20 20  /.  int nSub;   
19ff8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
19ff9 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
19ffa 69 65 73 20 69 6e 20 61 70 53 75 62 20 2a 2f 0a  ies in apSub */.
19ffb 20 20 69 6e 74 20 69 41 64 64 72 3b 20 20 20 20    int iAddr;    
19ffc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19ffd 41 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74 20  Address of next 
19ffe 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 72  instruction to r
19fff 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  eturn */.  int i
1a000 53 75 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  Sub;            
1a001 20 20 20 20 20 20 2f 2a 20 30 20 3d 20 6d 61 69        /* 0 = mai
1a002 6e 20 70 72 6f 67 72 61 6d 2c 20 31 20 3d 20 66  n program, 1 = f
1a003 69 72 73 74 20 73 75 62 2d 70 72 6f 67 72 61 6d  irst sub-program
1a004 20 65 74 63 2e 20 2a 2f 0a 7d 3b 0a 73 74 61 74   etc. */.};.stat
1a005 69 63 20 4f 70 20 2a 6f 70 49 74 65 72 4e 65 78  ic Op *opIterNex
1a006 74 28 56 64 62 65 4f 70 49 74 65 72 20 2a 70 29  t(VdbeOpIter *p)
1a007 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 2d  {.  Vdbe *v = p-
1a008 3e 76 3b 0a 20 20 4f 70 20 2a 70 52 65 74 20 3d  >v;.  Op *pRet =
1a009 20 30 3b 0a 20 20 4f 70 20 2a 61 4f 70 3b 0a 20   0;.  Op *aOp;. 
1a00a 20 69 6e 74 20 6e 4f 70 3b 0a 0a 20 20 69 66 28   int nOp;..  if(
1a00b 20 70 2d 3e 69 53 75 62 3c 3d 70 2d 3e 6e 53 75   p->iSub<=p->nSu
1a00c 62 20 29 7b 0a 0a 20 20 20 20 69 66 28 20 70 2d  b ){..    if( p-
1a00d 3e 69 53 75 62 3d 3d 30 20 29 7b 0a 20 20 20 20  >iSub==0 ){.    
1a00e 20 20 61 4f 70 20 3d 20 76 2d 3e 61 4f 70 3b 0a    aOp = v->aOp;.
1a00f 20 20 20 20 20 20 6e 4f 70 20 3d 20 76 2d 3e 6e        nOp = v->n
1a010 4f 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  Op;.    }else{. 
1a011 20 20 20 20 20 61 4f 70 20 3d 20 70 2d 3e 61 70       aOp = p->ap
1a012 53 75 62 5b 70 2d 3e 69 53 75 62 2d 31 5d 2d 3e  Sub[p->iSub-1]->
1a013 61 4f 70 3b 0a 20 20 20 20 20 20 6e 4f 70 20 3d  aOp;.      nOp =
1a014 20 70 2d 3e 61 70 53 75 62 5b 70 2d 3e 69 53 75   p->apSub[p->iSu
1a015 62 2d 31 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d  b-1]->nOp;.    }
1a016 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
1a017 69 41 64 64 72 3c 6e 4f 70 20 29 3b 0a 0a 20 20  iAddr<nOp );..  
1a018 20 20 70 52 65 74 20 3d 20 26 61 4f 70 5b 70 2d    pRet = &aOp[p-
1a019 3e 69 41 64 64 72 5d 3b 0a 20 20 20 20 70 2d 3e  >iAddr];.    p->
1a01a 69 41 64 64 72 2b 2b 3b 0a 20 20 20 20 69 66 28  iAddr++;.    if(
1a01b 20 70 2d 3e 69 41 64 64 72 3d 3d 6e 4f 70 20 29   p->iAddr==nOp )
1a01c 7b 0a 20 20 20 20 20 20 70 2d 3e 69 53 75 62 2b  {.      p->iSub+
1a01d 2b 3b 0a 20 20 20 20 20 20 70 2d 3e 69 41 64 64  +;.      p->iAdd
1a01e 72 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 0a  r = 0;.    }.  .
1a01f 20 20 20 20 69 66 28 20 70 52 65 74 2d 3e 70 34      if( pRet->p4
1a020 74 79 70 65 3d 3d 50 34 5f 53 55 42 50 52 4f 47  type==P4_SUBPROG
1a021 52 41 4d 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  RAM ){.      int
1a022 20 6e 42 79 74 65 20 3d 20 28 70 2d 3e 6e 53 75   nByte = (p->nSu
1a023 62 2b 31 29 2a 73 69 7a 65 6f 66 28 53 75 62 50  b+1)*sizeof(SubP
1a024 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 20 20 20  rogram*);.      
1a025 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72  int j;.      for
1a026 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 53 75 62 3b  (j=0; j<p->nSub;
1a027 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69   j++){.        i
1a028 66 28 20 70 2d 3e 61 70 53 75 62 5b 6a 5d 3d 3d  f( p->apSub[j]==
1a029 70 52 65 74 2d 3e 70 34 2e 70 50 72 6f 67 72 61  pRet->p4.pProgra
1a02a 6d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  m ) break;.     
1a02b 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6a 3d 3d   }.      if( j==
1a02c 70 2d 3e 6e 53 75 62 20 29 7b 0a 20 20 20 20 20  p->nSub ){.     
1a02d 20 20 20 70 2d 3e 61 70 53 75 62 20 3d 20 73 71     p->apSub = sq
1a02e 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72  lite3DbReallocOr
1a02f 46 72 65 65 28 76 2d 3e 64 62 2c 20 70 2d 3e 61  Free(v->db, p->a
1a030 70 53 75 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20  pSub, nByte);.  
1a031 20 20 20 20 20 20 69 66 28 20 21 70 2d 3e 61 70        if( !p->ap
1a032 53 75 62 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Sub ){.         
1a033 20 70 52 65 74 20 3d 20 30 3b 0a 20 20 20 20 20   pRet = 0;.     
1a034 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1a035 20 20 20 20 70 2d 3e 61 70 53 75 62 5b 70 2d 3e      p->apSub[p->
1a036 6e 53 75 62 2b 2b 5d 20 3d 20 70 52 65 74 2d 3e  nSub++] = pRet->
1a037 70 34 2e 70 50 72 6f 67 72 61 6d 3b 0a 20 20 20  p4.pProgram;.   
1a038 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
1a039 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
1a03a 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pRet;.}../*.*
1a03b 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20 70  * Check if the p
1a03c 72 6f 67 72 61 6d 20 73 74 6f 72 65 64 20 69 6e  rogram stored in
1a03d 20 74 68 65 20 56 4d 20 61 73 73 6f 63 69 61 74   the VM associat
1a03e 65 64 20 77 69 74 68 20 70 50 61 72 73 65 20 6d  ed with pParse m
1a03f 61 79 0a 2a 2a 20 74 68 72 6f 77 20 61 6e 20 41  ay.** throw an A
1a040 42 4f 52 54 20 65 78 63 65 70 74 69 6f 6e 20 28  BORT exception (
1a041 63 61 75 73 69 6e 67 20 74 68 65 20 73 74 61 74  causing the stat
1a042 65 6d 65 6e 74 2c 20 62 75 74 20 6e 6f 74 20 65  ement, but not e
1a043 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f  ntire transactio
1a044 6e 0a 2a 2a 20 74 6f 20 62 65 20 72 6f 6c 6c 65  n.** to be rolle
1a045 64 20 62 61 63 6b 29 2e 20 54 68 69 73 20 63 6f  d back). This co
1a046 6e 64 69 74 69 6f 6e 20 69 73 20 74 72 75 65 20  ndition is true 
1a047 69 66 20 74 68 65 20 6d 61 69 6e 20 70 72 6f 67  if the main prog
1a048 72 61 6d 20 6f 72 20 61 6e 79 0a 2a 2a 20 73 75  ram or any.** su
1a049 62 2d 70 72 6f 67 72 61 6d 73 20 63 6f 6e 74 61  b-programs conta
1a04a 69 6e 73 20 61 6e 79 20 6f 66 20 74 68 65 20 66  ins any of the f
1a04b 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
1a04c 20 20 2a 20 20 4f 50 5f 48 61 6c 74 20 77 69 74    *  OP_Halt wit
1a04d 68 20 50 31 3d 53 51 4c 49 54 45 5f 43 4f 4e 53  h P1=SQLITE_CONS
1a04e 54 52 41 49 4e 54 20 61 6e 64 20 50 32 3d 4f 45  TRAINT and P2=OE
1a04f 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20 20 2a 20 20  _Abort..**   *  
1a050 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 20 77 69  OP_HaltIfNull wi
1a051 74 68 20 50 31 3d 53 51 4c 49 54 45 5f 43 4f 4e  th P1=SQLITE_CON
1a052 53 54 52 41 49 4e 54 20 61 6e 64 20 50 32 3d 4f  STRAINT and P2=O
1a053 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20 20 2a 20  E_Abort..**   * 
1a054 20 4f 50 5f 44 65 73 74 72 6f 79 0a 2a 2a 20 20   OP_Destroy.**  
1a055 20 2a 20 20 4f 50 5f 56 55 70 64 61 74 65 0a 2a   *  OP_VUpdate.*
1a056 2a 20 20 20 2a 20 20 4f 50 5f 56 52 65 6e 61 6d  *   *  OP_VRenam
1a057 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 46 6b 43  e.**   *  OP_FkC
1a058 6f 75 6e 74 65 72 20 77 69 74 68 20 50 32 3d 3d  ounter with P2==
1a059 30 20 28 69 6d 6d 65 64 69 61 74 65 20 66 6f 72  0 (immediate for
1a05a 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
1a05b 69 6e 74 29 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20  int).**.** Then 
1a05c 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 76  check that the v
1a05d 61 6c 75 65 20 6f 66 20 50 61 72 73 65 2e 6d 61  alue of Parse.ma
1a05e 79 41 62 6f 72 74 20 69 73 20 74 72 75 65 20 69  yAbort is true i
1a05f 66 20 61 6e 0a 2a 2a 20 41 42 4f 52 54 20 6d 61  f an.** ABORT ma
1a060 79 20 62 65 20 74 68 72 6f 77 6e 2c 20 6f 72 20  y be thrown, or 
1a061 66 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65 2e  false otherwise.
1a062 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
1a063 69 74 20 64 6f 65 73 0a 2a 2a 20 6d 61 74 63 68  it does.** match
1a064 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72  , or false other
1a065 77 69 73 65 2e 20 54 68 69 73 20 66 75 6e 63 74  wise. This funct
1a066 69 6f 6e 20 69 73 20 69 6e 74 65 6e 64 65 64 20  ion is intended 
1a067 74 6f 20 62 65 20 75 73 65 64 20 61 73 0a 2a 2a  to be used as.**
1a068 20 70 61 72 74 20 6f 66 20 61 6e 20 61 73 73 65   part of an asse
1a069 72 74 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20  rt statement in 
1a06a 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e 20 53 69  the compiler. Si
1a06b 6d 69 6c 61 72 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20  milar to:.**.** 
1a06c 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1a06d 33 56 64 62 65 41 73 73 65 72 74 4d 61 79 41 62  3VdbeAssertMayAb
1a06e 6f 72 74 28 70 50 61 72 73 65 2d 3e 70 56 64 62  ort(pParse->pVdb
1a06f 65 2c 20 70 50 61 72 73 65 2d 3e 6d 61 79 41 62  e, pParse->mayAb
1a070 6f 72 74 29 20 29 3b 0a 2a 2f 0a 53 51 4c 49 54  ort) );.*/.SQLIT
1a071 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1a072 6c 69 74 65 33 56 64 62 65 41 73 73 65 72 74 4d  lite3VdbeAssertM
1a073 61 79 41 62 6f 72 74 28 56 64 62 65 20 2a 76 2c  ayAbort(Vdbe *v,
1a074 20 69 6e 74 20 6d 61 79 41 62 6f 72 74 29 7b 0a   int mayAbort){.
1a075 20 20 69 6e 74 20 68 61 73 41 62 6f 72 74 20 3d    int hasAbort =
1a076 20 30 3b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20   0;.  Op *pOp;. 
1a077 20 56 64 62 65 4f 70 49 74 65 72 20 73 49 74 65   VdbeOpIter sIte
1a078 72 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 49 74  r;.  memset(&sIt
1a079 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 49  er, 0, sizeof(sI
1a07a 74 65 72 29 29 3b 0a 20 20 73 49 74 65 72 2e 76  ter));.  sIter.v
1a07b 20 3d 20 76 3b 0a 0a 20 20 77 68 69 6c 65 28 20   = v;..  while( 
1a07c 28 70 4f 70 20 3d 20 6f 70 49 74 65 72 4e 65 78  (pOp = opIterNex
1a07d 74 28 26 73 49 74 65 72 29 29 21 3d 30 20 29 7b  t(&sIter))!=0 ){
1a07e 0a 20 20 20 20 69 6e 74 20 6f 70 63 6f 64 65 20  .    int opcode 
1a07f 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 20  = pOp->opcode;. 
1a080 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f     if( opcode==O
1a081 50 5f 44 65 73 74 72 6f 79 20 7c 7c 20 6f 70 63  P_Destroy || opc
1a082 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20  ode==OP_VUpdate 
1a083 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 52  || opcode==OP_VR
1a084 65 6e 61 6d 65 20 0a 23 69 66 6e 64 65 66 20 53  ename .#ifndef S
1a085 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
1a086 47 4e 5f 4b 45 59 0a 20 20 20 20 20 7c 7c 20 28  GN_KEY.     || (
1a087 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6b 43 6f 75  opcode==OP_FkCou
1a088 6e 74 65 72 20 26 26 20 70 4f 70 2d 3e 70 31 3d  nter && pOp->p1=
1a089 3d 30 20 26 26 20 70 4f 70 2d 3e 70 32 3d 3d 31  =0 && pOp->p2==1
1a08a 29 20 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 7c  ) .#endif.     |
1a08b 7c 20 28 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 48  | ((opcode==OP_H
1a08c 61 6c 74 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f  alt || opcode==O
1a08d 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 29 20 0a 20  P_HaltIfNull) . 
1a08e 20 20 20 20 20 26 26 20 28 70 4f 70 2d 3e 70 31       && (pOp->p1
1a08f 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  ==SQLITE_CONSTRA
1a090 49 4e 54 20 26 26 20 70 4f 70 2d 3e 70 32 3d 3d  INT && pOp->p2==
1a091 4f 45 5f 41 62 6f 72 74 29 29 0a 20 20 20 20 29  OE_Abort)).    )
1a092 7b 0a 20 20 20 20 20 20 68 61 73 41 62 6f 72 74  {.      hasAbort
1a093 20 3d 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61   = 1;.      brea
1a094 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  k;.    }.  }.  s
1a095 71 6c 69 74 65 33 44 62 46 72 65 65 28 76 2d 3e  qlite3DbFree(v->
1a096 64 62 2c 20 73 49 74 65 72 2e 61 70 53 75 62 29  db, sIter.apSub)
1a097 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 74  ;..  /* Return t
1a098 72 75 65 20 69 66 20 68 61 73 41 62 6f 72 74 3d  rue if hasAbort=
1a099 3d 6d 61 79 41 62 6f 72 74 2e 20 4f 72 20 69 66  =mayAbort. Or if
1a09a 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72   a malloc failur
1a09b 65 20 6f 63 63 75 72 65 64 2e 0a 20 20 2a 2a 20  e occured..  ** 
1a09c 49 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 65 64  If malloc failed
1a09d 2c 20 74 68 65 6e 20 74 68 65 20 77 68 69 6c 65  , then the while
1a09e 28 29 20 6c 6f 6f 70 20 61 62 6f 76 65 20 6d 61  () loop above ma
1a09f 79 20 6e 6f 74 20 68 61 76 65 20 69 74 65 72 61  y not have itera
1a0a0 74 65 64 0a 20 20 2a 2a 20 74 68 72 6f 75 67 68  ted.  ** through
1a0a1 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 61 6e 64   all opcodes and
1a0a2 20 68 61 73 41 62 6f 72 74 20 6d 61 79 20 62 65   hasAbort may be
1a0a3 20 73 65 74 20 69 6e 63 6f 72 72 65 63 74 6c 79   set incorrectly
1a0a4 2e 20 52 65 74 75 72 6e 0a 20 20 2a 2a 20 74 72  . Return.  ** tr
1a0a5 75 65 20 66 6f 72 20 74 68 69 73 20 63 61 73 65  ue for this case
1a0a6 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65 20   to prevent the 
1a0a7 61 73 73 65 72 74 28 29 20 69 6e 20 74 68 65 20  assert() in the 
1a0a8 63 61 6c 6c 65 72 73 20 66 72 61 6d 65 0a 20 20  callers frame.  
1a0a9 2a 2a 20 66 72 6f 6d 20 66 61 69 6c 69 6e 67 2e  ** from failing.
1a0aa 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 20    */.  return ( 
1a0ab 76 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  v->db->mallocFai
1a0ac 6c 65 64 20 7c 7c 20 68 61 73 41 62 6f 72 74 3d  led || hasAbort=
1a0ad 3d 6d 61 79 41 62 6f 72 74 20 29 3b 0a 7d 0a 23  =mayAbort );.}.#
1a0ae 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1a0af 44 45 42 55 47 20 2d 20 74 68 65 20 73 71 6c 69  DEBUG - the sqli
1a0b0 74 65 33 41 73 73 65 72 74 4d 61 79 41 62 6f 72  te3AssertMayAbor
1a0b1 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  t() function */.
1a0b2 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f  ./*.** Loop thro
1a0b3 75 67 68 20 74 68 65 20 70 72 6f 67 72 61 6d 20  ugh the program 
1a0b4 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 50 32 20 76  looking for P2 v
1a0b5 61 6c 75 65 73 20 74 68 61 74 20 61 72 65 20 6e  alues that are n
1a0b6 65 67 61 74 69 76 65 0a 2a 2a 20 6f 6e 20 6a 75  egative.** on ju
1a0b7 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e  mp instructions.
1a0b8 20 20 45 61 63 68 20 73 75 63 68 20 76 61 6c 75    Each such valu
1a0b9 65 20 69 73 20 61 20 6c 61 62 65 6c 2e 20 20 52  e is a label.  R
1a0ba 65 73 6f 6c 76 65 20 74 68 65 0a 2a 2a 20 6c 61  esolve the.** la
1a0bb 62 65 6c 20 62 79 20 73 65 74 74 69 6e 67 20 74  bel by setting t
1a0bc 68 65 20 50 32 20 76 61 6c 75 65 20 74 6f 20 69  he P2 value to i
1a0bd 74 73 20 63 6f 72 72 65 63 74 20 6e 6f 6e 2d 7a  ts correct non-z
1a0be 65 72 6f 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a  ero value..**.**
1a0bf 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
1a0c0 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 61 66 74   called once aft
1a0c1 65 72 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 68  er all opcodes h
1a0c2 61 76 65 20 62 65 65 6e 20 69 6e 73 65 72 74 65  ave been inserte
1a0c3 64 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 6c  d..**.** Variabl
1a0c4 65 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 20  e *pMaxFuncArgs 
1a0c5 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6d 61  is set to the ma
1a0c6 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 61  ximum value of a
1a0c7 6e 79 20 50 32 20 61 72 67 75 6d 65 6e 74 20 0a  ny P2 argument .
1a0c8 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f 46 75 6e 63  ** to an OP_Func
1a0c9 74 69 6f 6e 2c 20 4f 50 5f 41 67 67 53 74 65 70  tion, OP_AggStep
1a0ca 20 6f 72 20 4f 50 5f 56 46 69 6c 74 65 72 20 6f   or OP_VFilter o
1a0cb 70 63 6f 64 65 2e 20 54 68 69 73 20 69 73 20 75  pcode. This is u
1a0cc 73 65 64 20 62 79 20 0a 2a 2a 20 73 71 6c 69 74  sed by .** sqlit
1a0cd 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28  e3VdbeMakeReady(
1a0ce 29 20 74 6f 20 73 69 7a 65 20 74 68 65 20 56 64  ) to size the Vd
1a0cf 62 65 2e 61 70 41 72 67 5b 5d 20 61 72 72 61 79  be.apArg[] array
1a0d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1a0d1 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73   resolveP2Values
1a0d2 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 2a 70  (Vdbe *p, int *p
1a0d3 4d 61 78 46 75 6e 63 41 72 67 73 29 7b 0a 20 20  MaxFuncArgs){.  
1a0d4 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 4d 61  int i;.  int nMa
1a0d5 78 41 72 67 73 20 3d 20 2a 70 4d 61 78 46 75 6e  xArgs = *pMaxFun
1a0d6 63 41 72 67 73 3b 0a 20 20 4f 70 20 2a 70 4f 70  cArgs;.  Op *pOp
1a0d7 3b 0a 20 20 69 6e 74 20 2a 61 4c 61 62 65 6c 20  ;.  int *aLabel 
1a0d8 3d 20 70 2d 3e 61 4c 61 62 65 6c 3b 0a 20 20 70  = p->aLabel;.  p
1a0d9 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a  ->readOnly = 1;.
1a0da 20 20 66 6f 72 28 70 4f 70 3d 70 2d 3e 61 4f 70    for(pOp=p->aOp
1a0db 2c 20 69 3d 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e  , i=p->nOp-1; i>
1a0dc 3d 30 3b 20 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b  =0; i--, pOp++){
1a0dd 0a 20 20 20 20 75 38 20 6f 70 63 6f 64 65 20 3d  .    u8 opcode =
1a0de 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a 0a 20   pOp->opcode;.. 
1a0df 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f     if( opcode==O
1a0e0 50 5f 46 75 6e 63 74 69 6f 6e 20 7c 7c 20 6f 70  P_Function || op
1a0e1 63 6f 64 65 3d 3d 4f 50 5f 41 67 67 53 74 65 70  code==OP_AggStep
1a0e2 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f   ){.      if( pO
1a0e3 70 2d 3e 70 35 3e 6e 4d 61 78 41 72 67 73 20 29  p->p5>nMaxArgs )
1a0e4 20 6e 4d 61 78 41 72 67 73 20 3d 20 70 4f 70 2d   nMaxArgs = pOp-
1a0e5 3e 70 35 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  >p5;.#ifndef SQL
1a0e6 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1a0e7 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20  TABLE.    }else 
1a0e8 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56  if( opcode==OP_V
1a0e9 55 70 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20  Update ){.      
1a0ea 69 66 28 20 70 4f 70 2d 3e 70 32 3e 6e 4d 61 78  if( pOp->p2>nMax
1a0eb 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20  Args ) nMaxArgs 
1a0ec 3d 20 70 4f 70 2d 3e 70 32 3b 0a 23 65 6e 64 69  = pOp->p2;.#endi
1a0ed 66 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  f.    }else if( 
1a0ee 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 6e 73  opcode==OP_Trans
1a0ef 61 63 74 69 6f 6e 20 26 26 20 70 4f 70 2d 3e 70  action && pOp->p
1a0f0 32 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d  2!=0 ){.      p-
1a0f1 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 30 3b 0a 23  >readOnly = 0;.#
1a0f2 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1a0f3 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
1a0f4 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70      }else if( op
1a0f5 63 6f 64 65 3d 3d 4f 50 5f 56 46 69 6c 74 65 72  code==OP_VFilter
1a0f6 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b   ){.      int n;
1a0f7 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1a0f8 2d 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20 33 20 29  ->nOp - i >= 3 )
1a0f9 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1a0fa 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d  pOp[-1].opcode==
1a0fb 4f 50 5f 49 6e 74 65 67 65 72 20 29 3b 0a 20 20  OP_Integer );.  
1a0fc 20 20 20 20 6e 20 3d 20 70 4f 70 5b 2d 31 5d 2e      n = pOp[-1].
1a0fd 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e  p1;.      if( n>
1a0fe 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41  nMaxArgs ) nMaxA
1a0ff 72 67 73 20 3d 20 6e 3b 0a 23 65 6e 64 69 66 0a  rgs = n;.#endif.
1a100 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 73      }..    if( s
1a101 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65  qlite3VdbeOpcode
1a102 48 61 73 50 72 6f 70 65 72 74 79 28 6f 70 63 6f  HasProperty(opco
1a103 64 65 2c 20 4f 50 46 4c 47 5f 4a 55 4d 50 29 20  de, OPFLG_JUMP) 
1a104 26 26 20 70 4f 70 2d 3e 70 32 3c 30 20 29 7b 0a  && pOp->p2<0 ){.
1a105 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2d 31        assert( -1
1a106 2d 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4c 61 62  -pOp->p2<p->nLab
1a107 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d  el );.      pOp-
1a108 3e 70 32 20 3d 20 61 4c 61 62 65 6c 5b 2d 31 2d  >p2 = aLabel[-1-
1a109 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 7d 0a  pOp->p2];.    }.
1a10a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46    }.  sqlite3DbF
1a10b 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c  ree(p->db, p->aL
1a10c 61 62 65 6c 29 3b 0a 20 20 70 2d 3e 61 4c 61 62  abel);.  p->aLab
1a10d 65 6c 20 3d 20 30 3b 0a 0a 20 20 2a 70 4d 61 78  el = 0;..  *pMax
1a10e 46 75 6e 63 41 72 67 73 20 3d 20 6e 4d 61 78 41  FuncArgs = nMaxA
1a10f 72 67 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  rgs;.}../*.** Re
1a110 74 75 72 6e 20 74 68 65 20 61 64 64 72 65 73 73  turn the address
1a111 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73   of the next ins
1a112 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 69  truction to be i
1a113 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49  nserted..*/.SQLI
1a114 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1a115 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
1a116 74 41 64 64 72 28 56 64 62 65 20 2a 70 29 7b 0a  tAddr(Vdbe *p){.
1a117 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67    assert( p->mag
1a118 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49  ic==VDBE_MAGIC_I
1a119 4e 49 54 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  NIT );.  return 
1a11a 70 2d 3e 6e 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  p->nOp;.}../*.**
1a11b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   This function r
1a11c 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
1a11d 20 74 6f 20 74 68 65 20 61 72 72 61 79 20 6f 66   to the array of
1a11e 20 6f 70 63 6f 64 65 73 20 61 73 73 6f 63 69 61   opcodes associa
1a11f 74 65 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20  ted with.** the 
1a120 56 64 62 65 20 70 61 73 73 65 64 20 61 73 20 74  Vdbe passed as t
1a121 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
1a122 74 2e 20 49 74 20 69 73 20 74 68 65 20 63 61 6c  t. It is the cal
1a123 6c 65 72 73 20 72 65 73 70 6f 6e 73 69 62 69 6c  lers responsibil
1a124 69 74 79 0a 2a 2a 20 74 6f 20 61 72 72 61 6e 67  ity.** to arrang
1a125 65 20 66 6f 72 20 74 68 65 20 72 65 74 75 72 6e  e for the return
1a126 65 64 20 61 72 72 61 79 20 74 6f 20 62 65 20 65  ed array to be e
1a127 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 20  ventually freed 
1a128 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a 20 76 64  using the .** vd
1a129 62 65 46 72 65 65 4f 70 41 72 72 61 79 28 29 20  beFreeOpArray() 
1a12a 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
1a12b 42 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  Before returning
1a12c 2c 20 2a 70 6e 4f 70 20 69 73 20 73 65 74 20 74  , *pnOp is set t
1a12d 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  o the number of 
1a12e 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 72  entries in the r
1a12f 65 74 75 72 6e 65 64 0a 2a 2a 20 61 72 72 61 79  eturned.** array
1a130 2e 20 41 6c 73 6f 2c 20 2a 70 6e 4d 61 78 41 72  . Also, *pnMaxAr
1a131 67 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  g is set to the 
1a132 6c 61 72 67 65 72 20 6f 66 20 69 74 73 20 63 75  larger of its cu
1a133 72 72 65 6e 74 20 76 61 6c 75 65 20 61 6e 64 20  rrent value and 
1a134 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  .** the number o
1a135 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  f entries in the
1a136 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 72   Vdbe.apArg[] ar
1a137 72 61 79 20 72 65 71 75 69 72 65 64 20 74 6f 20  ray required to 
1a138 65 78 65 63 75 74 65 20 74 68 65 20 0a 2a 2a 20  execute the .** 
1a139 72 65 74 75 72 6e 65 64 20 70 72 6f 67 72 61 6d  returned program
1a13a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1a13b 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69  ATE VdbeOp *sqli
1a13c 74 65 33 56 64 62 65 54 61 6b 65 4f 70 41 72 72  te3VdbeTakeOpArr
1a13d 61 79 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20  ay(Vdbe *p, int 
1a13e 2a 70 6e 4f 70 2c 20 69 6e 74 20 2a 70 6e 4d 61  *pnOp, int *pnMa
1a13f 78 41 72 67 29 7b 0a 20 20 56 64 62 65 4f 70 20  xArg){.  VdbeOp 
1a140 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20  *aOp = p->aOp;. 
1a141 20 61 73 73 65 72 74 28 20 61 4f 70 20 26 26 20   assert( aOp && 
1a142 21 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  !p->db->mallocFa
1a143 69 6c 65 64 20 29 3b 0a 0a 20 20 2f 2a 20 43 68  iled );..  /* Ch
1a144 65 63 6b 20 74 68 61 74 20 73 71 6c 69 74 65 33  eck that sqlite3
1a145 56 64 62 65 55 73 65 73 42 74 72 65 65 28 29 20  VdbeUsesBtree() 
1a146 77 61 73 20 6e 6f 74 20 63 61 6c 6c 65 64 20 6f  was not called o
1a147 6e 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 61  n this VM */.  a
1a148 73 73 65 72 74 28 20 70 2d 3e 61 4d 75 74 65 78  ssert( p->aMutex
1a149 2e 6e 4d 75 74 65 78 3d 3d 30 20 29 3b 0a 0a 20  .nMutex==0 );.. 
1a14a 20 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73   resolveP2Values
1a14b 28 70 2c 20 70 6e 4d 61 78 41 72 67 29 3b 0a 20  (p, pnMaxArg);. 
1a14c 20 2a 70 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b   *pnOp = p->nOp;
1a14d 0a 20 20 70 2d 3e 61 4f 70 20 3d 20 30 3b 0a 20  .  p->aOp = 0;. 
1a14e 20 72 65 74 75 72 6e 20 61 4f 70 3b 0a 7d 0a 0a   return aOp;.}..
1a14f 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 77 68 6f 6c  /*.** Add a whol
1a150 65 20 6c 69 73 74 20 6f 66 20 6f 70 65 72 61 74  e list of operat
1a151 69 6f 6e 73 20 74 6f 20 74 68 65 20 6f 70 65 72  ions to the oper
1a152 61 74 69 6f 6e 20 73 74 61 63 6b 2e 20 20 52 65  ation stack.  Re
1a153 74 75 72 6e 20 74 68 65 0a 2a 2a 20 61 64 64 72  turn the.** addr
1a154 65 73 73 20 6f 66 20 74 68 65 20 66 69 72 73 74  ess of the first
1a155 20 6f 70 65 72 61 74 69 6f 6e 20 61 64 64 65 64   operation added
1a156 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1a157 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
1a158 64 62 65 41 64 64 4f 70 4c 69 73 74 28 56 64 62  dbeAddOpList(Vdb
1a159 65 20 2a 70 2c 20 69 6e 74 20 6e 4f 70 2c 20 56  e *p, int nOp, V
1a15a 64 62 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20  dbeOpList const 
1a15b 2a 61 4f 70 29 7b 0a 20 20 69 6e 74 20 61 64 64  *aOp){.  int add
1a15c 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  r;.  assert( p->
1a15d 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49  magic==VDBE_MAGI
1a15e 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20  C_INIT );.  if( 
1a15f 70 2d 3e 6e 4f 70 20 2b 20 6e 4f 70 20 3e 20 70  p->nOp + nOp > p
1a160 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 26 26 20 67 72  ->nOpAlloc && gr
1a161 6f 77 4f 70 41 72 72 61 79 28 70 29 20 29 7b 0a  owOpArray(p) ){.
1a162 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
1a163 7d 0a 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f  }.  addr = p->nO
1a164 70 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  p;.  if( ALWAYS(
1a165 6e 4f 70 3e 30 29 20 29 7b 0a 20 20 20 20 69 6e  nOp>0) ){.    in
1a166 74 20 69 3b 0a 20 20 20 20 56 64 62 65 4f 70 4c  t i;.    VdbeOpL
1a167 69 73 74 20 63 6f 6e 73 74 20 2a 70 49 6e 20 3d  ist const *pIn =
1a168 20 61 4f 70 3b 0a 20 20 20 20 66 6f 72 28 69 3d   aOp;.    for(i=
1a169 30 3b 20 69 3c 6e 4f 70 3b 20 69 2b 2b 2c 20 70  0; i<nOp; i++, p
1a16a 49 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74  In++){.      int
1a16b 20 70 32 20 3d 20 70 49 6e 2d 3e 70 32 3b 0a 20   p2 = pIn->p2;. 
1a16c 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 75       VdbeOp *pOu
1a16d 74 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 64  t = &p->aOp[i+ad
1a16e 64 72 5d 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d  dr];.      pOut-
1a16f 3e 6f 70 63 6f 64 65 20 3d 20 70 49 6e 2d 3e 6f  >opcode = pIn->o
1a170 70 63 6f 64 65 3b 0a 20 20 20 20 20 20 70 4f 75  pcode;.      pOu
1a171 74 2d 3e 70 31 20 3d 20 70 49 6e 2d 3e 70 31 3b  t->p1 = pIn->p1;
1a172 0a 20 20 20 20 20 20 69 66 28 20 70 32 3c 30 20  .      if( p2<0 
1a173 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70  && sqlite3VdbeOp
1a174 63 6f 64 65 48 61 73 50 72 6f 70 65 72 74 79 28  codeHasProperty(
1a175 70 4f 75 74 2d 3e 6f 70 63 6f 64 65 2c 20 4f 50  pOut->opcode, OP
1a176 46 4c 47 5f 4a 55 4d 50 29 20 29 7b 0a 20 20 20  FLG_JUMP) ){.   
1a177 20 20 20 20 20 70 4f 75 74 2d 3e 70 32 20 3d 20       pOut->p2 = 
1a178 61 64 64 72 20 2b 20 41 44 44 52 28 70 32 29 3b  addr + ADDR(p2);
1a179 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1a17a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 32 20 3d        pOut->p2 =
1a17b 20 70 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   p2;.      }.   
1a17c 20 20 20 70 4f 75 74 2d 3e 70 33 20 3d 20 70 49     pOut->p3 = pI
1a17d 6e 2d 3e 70 33 3b 0a 20 20 20 20 20 20 70 4f 75  n->p3;.      pOu
1a17e 74 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e  t->p4type = P4_N
1a17f 4f 54 55 53 45 44 3b 0a 20 20 20 20 20 20 70 4f  OTUSED;.      pO
1a180 75 74 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20  ut->p4.p = 0;.  
1a181 20 20 20 20 70 4f 75 74 2d 3e 70 35 20 3d 20 30      pOut->p5 = 0
1a182 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
1a183 44 45 42 55 47 0a 20 20 20 20 20 20 70 4f 75 74  DEBUG.      pOut
1a184 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a  ->zComment = 0;.
1a185 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
1a186 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20  3VdbeAddopTrace 
1a187 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1a188 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 30 2c  e3VdbePrintOp(0,
1a189 20 69 2b 61 64 64 72 2c 20 26 70 2d 3e 61 4f 70   i+addr, &p->aOp
1a18a 5b 69 2b 61 64 64 72 5d 29 3b 0a 20 20 20 20 20  [i+addr]);.     
1a18b 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a   }.#endif.    }.
1a18c 20 20 20 20 70 2d 3e 6e 4f 70 20 2b 3d 20 6e 4f      p->nOp += nO
1a18d 70 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  p;.  }.  return 
1a18e 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  addr;.}../*.** C
1a18f 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20  hange the value 
1a190 6f 66 20 74 68 65 20 50 31 20 6f 70 65 72 61 6e  of the P1 operan
1a191 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63  d for a specific
1a192 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a   instruction..**
1a193 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
1a194 20 75 73 65 66 75 6c 20 77 68 65 6e 20 61 20 6c   useful when a l
1a195 61 72 67 65 20 70 72 6f 67 72 61 6d 20 69 73 20  arge program is 
1a196 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a 2a 2a  loaded from a.**
1a197 20 73 74 61 74 69 63 20 61 72 72 61 79 20 75 73   static array us
1a198 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62 65 41  ing sqlite3VdbeA
1a199 64 64 4f 70 4c 69 73 74 20 62 75 74 20 77 65 20  ddOpList but we 
1a19a 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61 0a 2a  want to make a.*
1a19b 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63 68 61 6e  * few minor chan
1a19c 67 65 73 20 74 6f 20 74 68 65 20 70 72 6f 67 72  ges to the progr
1a19d 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  am..*/.SQLITE_PR
1a19e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1a19f 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 56  e3VdbeChangeP1(V
1a1a0 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72  dbe *p, int addr
1a1a1 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73  , int val){.  as
1a1a2 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20  sert( p!=0 );.  
1a1a3 61 73 73 65 72 74 28 20 61 64 64 72 3e 3d 30 20  assert( addr>=0 
1a1a4 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 3e  );.  if( p->nOp>
1a1a5 61 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e 61  addr ){.    p->a
1a1a6 4f 70 5b 61 64 64 72 5d 2e 70 31 20 3d 20 76 61  Op[addr].p1 = va
1a1a7 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  l;.  }.}../*.** 
1a1a8 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65  Change the value
1a1a9 20 6f 66 20 74 68 65 20 50 32 20 6f 70 65 72 61   of the P2 opera
1a1aa 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69  nd for a specifi
1a1ab 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  c instruction..*
1a1ac 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
1a1ad 73 20 75 73 65 66 75 6c 20 66 6f 72 20 73 65 74  s useful for set
1a1ae 74 69 6e 67 20 61 20 6a 75 6d 70 20 64 65 73 74  ting a jump dest
1a1af 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  ination..*/.SQLI
1a1b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1a1b1 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1a1b2 65 50 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  eP2(Vdbe *p, int
1a1b3 20 61 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b   addr, int val){
1a1b4 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20  .  assert( p!=0 
1a1b5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61 64 64  );.  assert( add
1a1b6 72 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d  r>=0 );.  if( p-
1a1b7 3e 6e 4f 70 3e 61 64 64 72 20 29 7b 0a 20 20 20  >nOp>addr ){.   
1a1b8 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 32   p->aOp[addr].p2
1a1b9 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f   = val;.  }.}../
1a1ba 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
1a1bb 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 33 20  value of the P3 
1a1bc 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70  operand for a sp
1a1bd 65 63 69 66 69 63 20 69 6e 73 74 72 75 63 74 69  ecific instructi
1a1be 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
1a1bf 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1a1c0 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 56  e3VdbeChangeP3(V
1a1c1 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72  dbe *p, int addr
1a1c2 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73  , int val){.  as
1a1c3 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20  sert( p!=0 );.  
1a1c4 61 73 73 65 72 74 28 20 61 64 64 72 3e 3d 30 20  assert( addr>=0 
1a1c5 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 3e  );.  if( p->nOp>
1a1c6 61 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e 61  addr ){.    p->a
1a1c7 4f 70 5b 61 64 64 72 5d 2e 70 33 20 3d 20 76 61  Op[addr].p3 = va
1a1c8 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  l;.  }.}../*.** 
1a1c9 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65  Change the value
1a1ca 20 6f 66 20 74 68 65 20 50 35 20 6f 70 65 72 61   of the P5 opera
1a1cb 6e 64 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20  nd for the most 
1a1cc 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 61 64 64 65  recently.** adde
1a1cd 64 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a  d operation..*/.
1a1ce 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1a1cf 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43  oid sqlite3VdbeC
1a1d0 68 61 6e 67 65 50 35 28 56 64 62 65 20 2a 70 2c  hangeP5(Vdbe *p,
1a1d1 20 75 38 20 76 61 6c 29 7b 0a 20 20 61 73 73 65   u8 val){.  asse
1a1d2 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 66  rt( p!=0 );.  if
1a1d3 28 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20 20 20  ( p->aOp ){.    
1a1d4 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30  assert( p->nOp>0
1a1d5 20 29 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 70   );.    p->aOp[p
1a1d6 2d 3e 6e 4f 70 2d 31 5d 2e 70 35 20 3d 20 76 61  ->nOp-1].p5 = va
1a1d7 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  l;.  }.}../*.** 
1a1d8 43 68 61 6e 67 65 20 74 68 65 20 50 32 20 6f 70  Change the P2 op
1a1d9 65 72 61 6e 64 20 6f 66 20 69 6e 73 74 72 75 63  erand of instruc
1a1da 74 69 6f 6e 20 61 64 64 72 20 73 6f 20 74 68 61  tion addr so tha
1a1db 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a  t it points to.*
1a1dc 2a 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66  * the address of
1a1dd 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75   the next instru
1a1de 63 74 69 6f 6e 20 74 6f 20 62 65 20 63 6f 64 65  ction to be code
1a1df 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
1a1e0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1a1e1 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 56 64  3VdbeJumpHere(Vd
1a1e2 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29  be *p, int addr)
1a1e3 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  {.  sqlite3VdbeC
1a1e4 68 61 6e 67 65 50 32 28 70 2c 20 61 64 64 72 2c  hangeP2(p, addr,
1a1e5 20 70 2d 3e 6e 4f 70 29 3b 0a 7d 0a 0a 0a 2f 2a   p->nOp);.}.../*
1a1e6 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74  .** If the input
1a1e7 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75   FuncDef structu
1a1e8 72 65 20 69 73 20 65 70 68 65 6d 65 72 61 6c 2c  re is ephemeral,
1a1e9 20 74 68 65 6e 20 66 72 65 65 20 69 74 2e 20 20   then free it.  
1a1ea 49 66 0a 2a 2a 20 74 68 65 20 46 75 6e 63 44 65  If.** the FuncDe
1a1eb 66 20 69 73 20 6e 6f 74 20 65 70 68 65 72 6d 61  f is not epherma
1a1ec 6c 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 68 69  l, then do nothi
1a1ed 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ng..*/.static vo
1a1ee 69 64 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c  id freeEphemeral
1a1ef 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33  Function(sqlite3
1a1f0 20 2a 64 62 2c 20 46 75 6e 63 44 65 66 20 2a 70   *db, FuncDef *p
1a1f1 44 65 66 29 7b 0a 20 20 69 66 28 20 41 4c 57 41  Def){.  if( ALWA
1a1f2 59 53 28 70 44 65 66 29 20 26 26 20 28 70 44 65  YS(pDef) && (pDe
1a1f3 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f->flags & SQLIT
1a1f4 45 5f 46 55 4e 43 5f 45 50 48 45 4d 29 21 3d 30  E_FUNC_EPHEM)!=0
1a1f5 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
1a1f6 62 46 72 65 65 28 64 62 2c 20 70 44 65 66 29 3b  bFree(db, pDef);
1a1f7 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  .  }.}../*.** De
1a1f8 6c 65 74 65 20 61 20 50 34 20 76 61 6c 75 65 20  lete a P4 value 
1a1f9 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2f  if necessary..*/
1a1fa 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65  .static void fre
1a1fb 65 50 34 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  eP4(sqlite3 *db,
1a1fc 20 69 6e 74 20 70 34 74 79 70 65 2c 20 76 6f 69   int p4type, voi
1a1fd 64 20 2a 70 34 29 7b 0a 20 20 69 66 28 20 70 34  d *p4){.  if( p4
1a1fe 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28 20   ){.    switch( 
1a1ff 70 34 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20  p4type ){.      
1a200 63 61 73 65 20 50 34 5f 52 45 41 4c 3a 0a 20 20  case P4_REAL:.  
1a201 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 36      case P4_INT6
1a202 34 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34  4:.      case P4
1a203 5f 4d 50 52 49 4e 54 46 3a 0a 20 20 20 20 20 20  _MPRINTF:.      
1a204 63 61 73 65 20 50 34 5f 44 59 4e 41 4d 49 43 3a  case P4_DYNAMIC:
1a205 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 4b  .      case P4_K
1a206 45 59 49 4e 46 4f 3a 0a 20 20 20 20 20 20 63 61  EYINFO:.      ca
1a207 73 65 20 50 34 5f 49 4e 54 41 52 52 41 59 3a 0a  se P4_INTARRAY:.
1a208 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45        case P4_KE
1a209 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 3a 20 7b  YINFO_HANDOFF: {
1a20a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1a20b 44 62 46 72 65 65 28 64 62 2c 20 70 34 29 3b 0a  DbFree(db, p4);.
1a20c 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
1a20d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
1a20e 65 20 50 34 5f 56 44 42 45 46 55 4e 43 3a 20 7b  e P4_VDBEFUNC: {
1a20f 0a 20 20 20 20 20 20 20 20 56 64 62 65 46 75 6e  .        VdbeFun
1a210 63 20 2a 70 56 64 62 65 46 75 6e 63 20 3d 20 28  c *pVdbeFunc = (
1a211 56 64 62 65 46 75 6e 63 20 2a 29 70 34 3b 0a 20  VdbeFunc *)p4;. 
1a212 20 20 20 20 20 20 20 66 72 65 65 45 70 68 65 6d         freeEphem
1a213 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28 64 62 2c  eralFunction(db,
1a214 20 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e   pVdbeFunc->pFun
1a215 63 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  c);.        sqli
1a216 74 65 33 56 64 62 65 44 65 6c 65 74 65 41 75 78  te3VdbeDeleteAux
1a217 44 61 74 61 28 70 56 64 62 65 46 75 6e 63 2c 20  Data(pVdbeFunc, 
1a218 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
1a219 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 56  te3DbFree(db, pV
1a21a 64 62 65 46 75 6e 63 29 3b 0a 20 20 20 20 20 20  dbeFunc);.      
1a21b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
1a21c 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 46  .      case P4_F
1a21d 55 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 20  UNCDEF: {.      
1a21e 20 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c 46    freeEphemeralF
1a21f 75 6e 63 74 69 6f 6e 28 64 62 2c 20 28 46 75 6e  unction(db, (Fun
1a220 63 44 65 66 2a 29 70 34 29 3b 0a 20 20 20 20 20  cDef*)p4);.     
1a221 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1a222 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f  }.      case P4_
1a223 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 20 20 73  MEM: {.        s
1a224 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
1a225 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
1a226 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  p4);.        bre
1a227 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
1a228 20 20 63 61 73 65 20 50 34 5f 56 54 41 42 20 3a    case P4_VTAB :
1a229 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   {.        sqlit
1a22a 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 28 56 54  e3VtabUnlock((VT
1a22b 61 62 6c 65 20 2a 29 70 34 29 3b 0a 20 20 20 20  able *)p4);.    
1a22c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1a22d 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34   }.      case P4
1a22e 5f 53 55 42 50 52 4f 47 52 41 4d 20 3a 20 7b 0a  _SUBPROGRAM : {.
1a22f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1a230 64 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 74 65  dbeProgramDelete
1a231 28 64 62 2c 20 28 53 75 62 50 72 6f 67 72 61 6d  (db, (SubProgram
1a232 20 2a 29 70 34 2c 20 31 29 3b 0a 20 20 20 20 20   *)p4, 1);.     
1a233 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1a234 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  }.    }.  }.}../
1a235 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 73 70  *.** Free the sp
1a236 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  ace allocated fo
1a237 72 20 61 4f 70 20 61 6e 64 20 61 6e 79 20 70 34  r aOp and any p4
1a238 20 76 61 6c 75 65 73 20 61 6c 6c 6f 63 61 74 65   values allocate
1a239 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6f 70 63  d for the.** opc
1a23a 6f 64 65 73 20 63 6f 6e 74 61 69 6e 65 64 20 77  odes contained w
1a23b 69 74 68 69 6e 2e 20 49 66 20 61 4f 70 20 69 73  ithin. If aOp is
1a23c 20 6e 6f 74 20 4e 55 4c 4c 20 69 74 20 69 73 20   not NULL it is 
1a23d 61 73 73 75 6d 65 64 20 74 6f 20 63 6f 6e 74 61  assumed to conta
1a23e 69 6e 20 0a 2a 2a 20 6e 4f 70 20 65 6e 74 72 69  in .** nOp entri
1a23f 65 73 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76  es. .*/.static v
1a240 6f 69 64 20 76 64 62 65 46 72 65 65 4f 70 41 72  oid vdbeFreeOpAr
1a241 72 61 79 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ray(sqlite3 *db,
1a242 20 4f 70 20 2a 61 4f 70 2c 20 69 6e 74 20 6e 4f   Op *aOp, int nO
1a243 70 29 7b 0a 20 20 69 66 28 20 61 4f 70 20 29 7b  p){.  if( aOp ){
1a244 0a 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20  .    Op *pOp;.  
1a245 20 20 66 6f 72 28 70 4f 70 3d 61 4f 70 3b 20 70    for(pOp=aOp; p
1a246 4f 70 3c 26 61 4f 70 5b 6e 4f 70 5d 3b 20 70 4f  Op<&aOp[nOp]; pO
1a247 70 2b 2b 29 7b 0a 20 20 20 20 20 20 66 72 65 65  p++){.      free
1a248 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79  P4(db, pOp->p4ty
1a249 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a  pe, pOp->p4.p);.
1a24a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
1a24b 42 55 47 0a 20 20 20 20 20 20 73 71 6c 69 74 65  BUG.      sqlite
1a24c 33 44 62 46 72 65 65 28 64 62 2c 20 70 4f 70 2d  3DbFree(db, pOp-
1a24d 3e 7a 43 6f 6d 6d 65 6e 74 29 3b 0a 23 65 6e 64  >zComment);.#end
1a24e 69 66 20 20 20 20 20 0a 20 20 20 20 7d 0a 20 20  if     .    }.  
1a24f 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  }.  sqlite3DbFre
1a250 65 28 64 62 2c 20 61 4f 70 29 3b 0a 7d 0a 0a 2f  e(db, aOp);.}../
1a251 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74  *.** Decrement t
1a252 68 65 20 72 65 66 2d 63 6f 75 6e 74 20 6f 6e 20  he ref-count on 
1a253 74 68 65 20 53 75 62 50 72 6f 67 72 61 6d 20 73  the SubProgram s
1a254 74 72 75 63 74 75 72 65 20 70 61 73 73 65 64 20  tructure passed 
1a255 61 73 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64  as the.** second
1a256 20 61 72 67 75 6d 65 6e 74 2e 20 49 66 20 74 68   argument. If th
1a257 65 20 72 65 66 2d 63 6f 75 6e 74 20 72 65 61 63  e ref-count reac
1a258 68 65 73 20 7a 65 72 6f 2c 20 66 72 65 65 20 74  hes zero, free t
1a259 68 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  he structure..**
1a25a 0a 2a 2a 20 54 68 65 20 61 72 72 61 79 20 6f 66  .** The array of
1a25b 20 56 44 42 45 20 6f 70 63 6f 64 65 73 20 73 74   VDBE opcodes st
1a25c 6f 72 65 64 20 61 73 20 53 75 62 50 72 6f 67 72  ored as SubProgr
1a25d 61 6d 2e 61 4f 70 20 69 73 20 66 72 65 65 64 20  am.aOp is freed 
1a25e 69 66 0a 2a 2a 20 65 69 74 68 65 72 20 74 68 65  if.** either the
1a25f 20 72 65 66 2d 63 6f 75 6e 74 20 72 65 61 63 68   ref-count reach
1a260 65 73 20 7a 65 72 6f 20 6f 72 20 70 61 72 61 6d  es zero or param
1a261 65 74 65 72 20 66 72 65 65 6f 70 20 69 73 20 6e  eter freeop is n
1a262 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53  on-zero..**.** S
1a263 69 6e 63 65 20 74 68 65 20 61 72 72 61 79 20 6f  ince the array o
1a264 66 20 6f 70 63 6f 64 65 73 20 70 6f 69 6e 74 65  f opcodes pointe
1a265 64 20 74 6f 20 62 79 20 53 75 62 50 72 6f 67 72  d to by SubProgr
1a266 61 6d 2e 61 4f 70 20 6d 61 79 20 64 69 72 65 63  am.aOp may direc
1a267 74 6c 79 0a 2a 2a 20 6f 72 20 69 6e 64 69 72 65  tly.** or indire
1a268 63 74 6c 79 20 63 6f 6e 74 61 69 6e 20 61 20 72  ctly contain a r
1a269 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20  eference to the 
1a26a 53 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63  SubProgram struc
1a26b 74 75 72 65 20 69 74 73 65 6c 66 2e 0a 2a 2a 20  ture itself..** 
1a26c 42 79 20 70 61 73 73 69 6e 67 20 61 20 6e 6f 6e  By passing a non
1a26d 2d 7a 65 72 6f 20 66 72 65 65 6f 70 20 70 61 72  -zero freeop par
1a26e 61 6d 65 74 65 72 2c 20 74 68 65 20 63 61 6c 6c  ameter, the call
1a26f 65 72 20 6d 61 79 20 65 6e 73 75 72 65 20 74 68  er may ensure th
1a270 61 74 20 61 6c 6c 0a 2a 2a 20 53 75 62 50 72 6f  at all.** SubPro
1a271 67 72 61 6d 20 73 74 72 75 63 74 75 72 65 73 20  gram structures 
1a272 61 6e 64 20 74 68 65 69 72 20 61 4f 70 20 61 72  and their aOp ar
1a273 72 61 79 73 20 61 72 65 20 66 72 65 65 64 2c 20  rays are freed, 
1a274 65 76 65 6e 20 77 68 65 6e 20 74 68 65 72 65 0a  even when there.
1a275 2a 2a 20 61 72 65 20 73 75 63 68 20 63 69 72 63  ** are such circ
1a276 75 6c 61 72 20 72 65 66 65 72 65 6e 63 65 73 2e  ular references.
1a277 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1a278 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1a279 64 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 74 65  dbeProgramDelete
1a27a 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 75  (sqlite3 *db, Su
1a27b 62 50 72 6f 67 72 61 6d 20 2a 70 2c 20 69 6e 74  bProgram *p, int
1a27c 20 66 72 65 65 6f 70 29 7b 0a 20 20 69 66 28 20   freeop){.  if( 
1a27d 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  p ){.    assert(
1a27e 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20   p->nRef>0 );.  
1a27f 20 20 69 66 28 20 66 72 65 65 6f 70 20 7c 7c 20    if( freeop || 
1a280 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 7b 0a 20 20  p->nRef==1 ){.  
1a281 20 20 20 20 4f 70 20 2a 61 4f 70 20 3d 20 70 2d      Op *aOp = p-
1a282 3e 61 4f 70 3b 0a 20 20 20 20 20 20 70 2d 3e 61  >aOp;.      p->a
1a283 4f 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 76 64  Op = 0;.      vd
1a284 62 65 46 72 65 65 4f 70 41 72 72 61 79 28 64 62  beFreeOpArray(db
1a285 2c 20 61 4f 70 2c 20 70 2d 3e 6e 4f 70 29 3b 0a  , aOp, p->nOp);.
1a286 20 20 20 20 20 20 70 2d 3e 6e 4f 70 20 3d 20 30        p->nOp = 0
1a287 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6e  ;.    }.    p->n
1a288 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70  Ref--;.    if( p
1a289 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20  ->nRef==0 ){.   
1a28a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1a28b 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 7d 0a 20  (db, p);.    }. 
1a28c 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61   }.}.../*.** Cha
1a28d 6e 67 65 20 4e 20 6f 70 63 6f 64 65 73 20 73 74  nge N opcodes st
1a28e 61 72 74 69 6e 67 20 61 74 20 61 64 64 72 20 74  arting at addr t
1a28f 6f 20 4e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 53 51 4c  o No-ops..*/.SQL
1a290 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1a291 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1a292 67 65 54 6f 4e 6f 6f 70 28 56 64 62 65 20 2a 70  geToNoop(Vdbe *p
1a293 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20  , int addr, int 
1a294 4e 29 7b 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70  N){.  if( p->aOp
1a295 20 29 7b 0a 20 20 20 20 56 64 62 65 4f 70 20 2a   ){.    VdbeOp *
1a296 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64  pOp = &p->aOp[ad
1a297 64 72 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  dr];.    sqlite3
1a298 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20   *db = p->db;.  
1a299 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a    while( N-- ){.
1a29a 20 20 20 20 20 20 66 72 65 65 50 34 28 64 62 2c        freeP4(db,
1a29b 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f   pOp->p4type, pO
1a29c 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 20 20 20 20  p->p4.p);.      
1a29d 6d 65 6d 73 65 74 28 70 4f 70 2c 20 30 2c 20 73  memset(pOp, 0, s
1a29e 69 7a 65 6f 66 28 70 4f 70 5b 30 5d 29 29 3b 0a  izeof(pOp[0]));.
1a29f 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64        pOp->opcod
1a2a0 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20  e = OP_Noop;.   
1a2a1 20 20 20 70 4f 70 2b 2b 3b 0a 20 20 20 20 7d 0a     pOp++;.    }.
1a2a2 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61    }.}../*.** Cha
1a2a3 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  nge the value of
1a2a4 20 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20   the P4 operand 
1a2a5 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69  for a specific i
1a2a6 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54  nstruction..** T
1a2a7 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
1a2a8 73 65 66 75 6c 20 77 68 65 6e 20 61 20 6c 61 72  seful when a lar
1a2a9 67 65 20 70 72 6f 67 72 61 6d 20 69 73 20 6c 6f  ge program is lo
1a2aa 61 64 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73  aded from a.** s
1a2ab 74 61 74 69 63 20 61 72 72 61 79 20 75 73 69 6e  tatic array usin
1a2ac 67 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  g sqlite3VdbeAdd
1a2ad 4f 70 4c 69 73 74 20 62 75 74 20 77 65 20 77 61  OpList but we wa
1a2ae 6e 74 20 74 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20  nt to make a.** 
1a2af 66 65 77 20 6d 69 6e 6f 72 20 63 68 61 6e 67 65  few minor change
1a2b0 73 20 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d  s to the program
1a2b1 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 3e 3d 30 20  ..**.** If n>=0 
1a2b2 74 68 65 6e 20 74 68 65 20 50 34 20 6f 70 65 72  then the P4 oper
1a2b3 61 6e 64 20 69 73 20 64 79 6e 61 6d 69 63 2c 20  and is dynamic, 
1a2b4 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 61 20 63  meaning that a c
1a2b5 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 73 74  opy of.** the st
1a2b6 72 69 6e 67 20 69 73 20 6d 61 64 65 20 69 6e 74  ring is made int
1a2b7 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  o memory obtaine
1a2b8 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d  d from sqlite3_m
1a2b9 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 41 20 76 61  alloc()..** A va
1a2ba 6c 75 65 20 6f 66 20 6e 3d 3d 30 20 6d 65 61 6e  lue of n==0 mean
1a2bb 73 20 63 6f 70 79 20 62 79 74 65 73 20 6f 66 20  s copy bytes of 
1a2bc 7a 50 34 20 75 70 20 74 6f 20 61 6e 64 20 69 6e  zP4 up to and in
1a2bd 63 6c 75 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66  cluding the.** f
1a2be 69 72 73 74 20 6e 75 6c 6c 20 62 79 74 65 2e 20  irst null byte. 
1a2bf 20 49 66 20 6e 3e 30 20 74 68 65 6e 20 63 6f 70   If n>0 then cop
1a2c0 79 20 6e 2b 31 20 62 79 74 65 73 20 6f 66 20 7a  y n+1 bytes of z
1a2c1 50 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 3d 3d  P4..**.** If n==
1a2c2 50 34 5f 4b 45 59 49 4e 46 4f 20 69 74 20 6d 65  P4_KEYINFO it me
1a2c3 61 6e 73 20 74 68 61 74 20 7a 50 34 20 69 73 20  ans that zP4 is 
1a2c4 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b  a pointer to a K
1a2c5 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
1a2c6 2e 0a 2a 2a 20 41 20 63 6f 70 79 20 69 73 20 6d  ..** A copy is m
1a2c7 61 64 65 20 6f 66 20 74 68 65 20 4b 65 79 49 6e  ade of the KeyIn
1a2c8 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 6e 74  fo structure int
1a2c9 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  o memory obtaine
1a2ca 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65  d from.** sqlite
1a2cb 33 5f 6d 61 6c 6c 6f 63 2c 20 74 6f 20 62 65 20  3_malloc, to be 
1a2cc 66 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56  freed when the V
1a2cd 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a 65 64  dbe is finalized
1a2ce 2e 0a 2a 2a 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e  ..** n==P4_KEYIN
1a2cf 46 4f 5f 48 41 4e 44 4f 46 46 20 69 6e 64 69 63  FO_HANDOFF indic
1a2d0 61 74 65 73 20 74 68 61 74 20 7a 50 34 20 70 6f  ates that zP4 po
1a2d1 69 6e 74 73 20 74 6f 20 61 20 4b 65 79 49 6e 66  ints to a KeyInf
1a2d2 6f 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 73  o structure.** s
1a2d3 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20  tored in memory 
1a2d4 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20  that the caller 
1a2d5 68 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  has obtained fro
1a2d6 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  m sqlite3_malloc
1a2d7 2e 20 54 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 72  . The .** caller
1a2d8 20 73 68 6f 75 6c 64 20 6e 6f 74 20 66 72 65 65   should not free
1a2d9 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c   the allocation,
1a2da 20 69 74 20 77 69 6c 6c 20 62 65 20 66 72 65 65   it will be free
1a2db 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20  d when the Vdbe 
1a2dc 69 73 0a 2a 2a 20 66 69 6e 61 6c 69 7a 65 64 2e  is.** finalized.
1a2dd 0a 2a 2a 20 0a 2a 2a 20 4f 74 68 65 72 20 76 61  .** .** Other va
1a2de 6c 75 65 73 20 6f 66 20 6e 20 28 50 34 5f 53 54  lues of n (P4_ST
1a2df 41 54 49 43 2c 20 50 34 5f 43 4f 4c 4c 53 45 51  ATIC, P4_COLLSEQ
1a2e0 20 65 74 63 2e 29 20 69 6e 64 69 63 61 74 65 20   etc.) indicate 
1a2e1 74 68 61 74 20 7a 50 34 20 70 6f 69 6e 74 73 0a  that zP4 points.
1a2e2 2a 2a 20 74 6f 20 61 20 73 74 72 69 6e 67 20 6f  ** to a string o
1a2e3 72 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  r structure that
1a2e4 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74   is guaranteed t
1a2e5 6f 20 65 78 69 73 74 20 66 6f 72 20 74 68 65 20  o exist for the 
1a2e6 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74  lifetime of.** t
1a2e7 68 65 20 56 64 62 65 2e 20 49 6e 20 74 68 65 73  he Vdbe. In thes
1a2e8 65 20 63 61 73 65 73 20 77 65 20 63 61 6e 20 6a  e cases we can j
1a2e9 75 73 74 20 63 6f 70 79 20 74 68 65 20 70 6f 69  ust copy the poi
1a2ea 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  nter..**.** If a
1a2eb 64 64 72 3c 30 20 74 68 65 6e 20 63 68 61 6e 67  ddr<0 then chang
1a2ec 65 20 50 34 20 6f 6e 20 74 68 65 20 6d 6f 73 74  e P4 on the most
1a2ed 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 65 72 74   recently insert
1a2ee 65 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  ed instruction..
1a2ef 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a2f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1a2f1 62 65 43 68 61 6e 67 65 50 34 28 56 64 62 65 20  beChangeP4(Vdbe 
1a2f2 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 63 6f  *p, int addr, co
1a2f3 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 2c 20 69  nst char *zP4, i
1a2f4 6e 74 20 6e 29 7b 0a 20 20 4f 70 20 2a 70 4f 70  nt n){.  Op *pOp
1a2f5 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ;.  sqlite3 *db;
1a2f6 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20  .  assert( p!=0 
1a2f7 29 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b  );.  db = p->db;
1a2f8 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61  .  assert( p->ma
1a2f9 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic==VDBE_MAGIC_
1a2fa 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 70 2d  INIT );.  if( p-
1a2fb 3e 61 4f 70 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d  >aOp==0 || db->m
1a2fc 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1a2fd 20 20 20 69 66 20 28 20 6e 21 3d 50 34 5f 4b 45     if ( n!=P4_KE
1a2fe 59 49 4e 46 4f 20 26 26 20 6e 21 3d 50 34 5f 56  YINFO && n!=P4_V
1a2ff 54 41 42 20 29 20 7b 0a 20 20 20 20 20 20 66 72  TAB ) {.      fr
1a300 65 65 50 34 28 64 62 2c 20 6e 2c 20 28 76 6f 69  eeP4(db, n, (voi
1a301 64 2a 29 2a 28 63 68 61 72 2a 2a 29 26 7a 50 34  d*)*(char**)&zP4
1a302 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  );.    }.    ret
1a303 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  urn;.  }.  asser
1a304 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 20  t( p->nOp>0 );. 
1a305 20 61 73 73 65 72 74 28 20 61 64 64 72 3c 70 2d   assert( addr<p-
1a306 3e 6e 4f 70 20 29 3b 0a 20 20 69 66 28 20 61 64  >nOp );.  if( ad
1a307 64 72 3c 30 20 29 7b 0a 20 20 20 20 61 64 64 72  dr<0 ){.    addr
1a308 20 3d 20 70 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20   = p->nOp - 1;. 
1a309 20 7d 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61   }.  pOp = &p->a
1a30a 4f 70 5b 61 64 64 72 5d 3b 0a 20 20 66 72 65 65  Op[addr];.  free
1a30b 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79  P4(db, pOp->p4ty
1a30c 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a  pe, pOp->p4.p);.
1a30d 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30 3b    pOp->p4.p = 0;
1a30e 0a 20 20 69 66 28 20 6e 3d 3d 50 34 5f 49 4e 54  .  if( n==P4_INT
1a30f 33 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 74  32 ){.    /* Not
1a310 65 3a 20 74 68 69 73 20 63 61 73 74 20 69 73 20  e: this cast is 
1a311 73 61 66 65 2c 20 62 65 63 61 75 73 65 20 74 68  safe, because th
1a312 65 20 6f 72 69 67 69 6e 20 64 61 74 61 20 70 6f  e origin data po
1a313 69 6e 74 20 77 61 73 20 61 6e 20 69 6e 74 0a 20  int was an int. 
1a314 20 20 20 2a 2a 20 74 68 61 74 20 77 61 73 20 63     ** that was c
1a315 61 73 74 20 74 6f 20 61 20 28 63 6f 6e 73 74 20  ast to a (const 
1a316 63 68 61 72 20 2a 29 2e 20 2a 2f 0a 20 20 20 20  char *). */.    
1a317 70 4f 70 2d 3e 70 34 2e 69 20 3d 20 53 51 4c 49  pOp->p4.i = SQLI
1a318 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 7a 50  TE_PTR_TO_INT(zP
1a319 34 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74  4);.    pOp->p4t
1a31a 79 70 65 20 3d 20 50 34 5f 49 4e 54 33 32 3b 0a  ype = P4_INT32;.
1a31b 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 50 34 3d    }else if( zP4=
1a31c 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70  =0 ){.    pOp->p
1a31d 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20 70 4f 70  4.p = 0;.    pOp
1a31e 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f  ->p4type = P4_NO
1a31f 54 55 53 45 44 3b 0a 20 20 7d 65 6c 73 65 20 69  TUSED;.  }else i
1a320 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f  f( n==P4_KEYINFO
1a321 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20   ){.    KeyInfo 
1a322 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69  *pKeyInfo;.    i
1a323 6e 74 20 6e 46 69 65 6c 64 2c 20 6e 42 79 74 65  nt nField, nByte
1a324 3b 0a 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d 20  ;..    nField = 
1a325 28 28 4b 65 79 49 6e 66 6f 2a 29 7a 50 34 29 2d  ((KeyInfo*)zP4)-
1a326 3e 6e 46 69 65 6c 64 3b 0a 20 20 20 20 6e 42 79  >nField;.    nBy
1a327 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 4b 65  te = sizeof(*pKe
1a328 79 49 6e 66 6f 29 20 2b 20 28 6e 46 69 65 6c 64  yInfo) + (nField
1a329 2d 31 29 2a 73 69 7a 65 6f 66 28 70 4b 65 79 49  -1)*sizeof(pKeyI
1a32a 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 30 5d 29 20 2b  nfo->aColl[0]) +
1a32b 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 70 4b 65   nField;.    pKe
1a32c 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 4d  yInfo = sqlite3M
1a32d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 20 29 3b 0a  alloc( nByte );.
1a32e 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79      pOp->p4.pKey
1a32f 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b  Info = pKeyInfo;
1a330 0a 20 20 20 20 69 66 28 20 70 4b 65 79 49 6e 66  .    if( pKeyInf
1a331 6f 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 61  o ){.      u8 *a
1a332 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 20  SortOrder;.     
1a333 20 6d 65 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f   memcpy(pKeyInfo
1a334 2c 20 7a 50 34 2c 20 6e 42 79 74 65 29 3b 0a 20  , zP4, nByte);. 
1a335 20 20 20 20 20 61 53 6f 72 74 4f 72 64 65 72 20       aSortOrder 
1a336 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72  = pKeyInfo->aSor
1a337 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 69 66  tOrder;.      if
1a338 28 20 61 53 6f 72 74 4f 72 64 65 72 20 29 7b 0a  ( aSortOrder ){.
1a339 20 20 20 20 20 20 20 20 70 4b 65 79 49 6e 66 6f          pKeyInfo
1a33a 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28  ->aSortOrder = (
1a33b 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 26  unsigned char*)&
1a33c 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b  pKeyInfo->aColl[
1a33d 6e 46 69 65 6c 64 5d 3b 0a 20 20 20 20 20 20 20  nField];.       
1a33e 20 6d 65 6d 63 70 79 28 70 4b 65 79 49 6e 66 6f   memcpy(pKeyInfo
1a33f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 2c 20 61 53  ->aSortOrder, aS
1a340 6f 72 74 4f 72 64 65 72 2c 20 6e 46 69 65 6c 64  ortOrder, nField
1a341 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1a342 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50   pOp->p4type = P
1a343 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 20 20 7d  4_KEYINFO;.    }
1a344 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d 3e 64  else{.      p->d
1a345 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1a346 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e  = 1;.      pOp->
1a347 70 34 74 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55  p4type = P4_NOTU
1a348 53 45 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  SED;.    }.  }el
1a349 73 65 20 69 66 28 20 6e 3d 3d 50 34 5f 4b 45 59  se if( n==P4_KEY
1a34a 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 29 7b 0a  INFO_HANDOFF ){.
1a34b 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20      pOp->p4.p = 
1a34c 28 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20  (void*)zP4;.    
1a34d 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34  pOp->p4type = P4
1a34e 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 7d 65 6c 73  _KEYINFO;.  }els
1a34f 65 20 69 66 28 20 6e 3d 3d 50 34 5f 56 54 41 42  e if( n==P4_VTAB
1a350 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e   ){.    pOp->p4.
1a351 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34 3b 0a  p = (void*)zP4;.
1a352 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20      pOp->p4type 
1a353 3d 20 50 34 5f 56 54 41 42 3b 0a 20 20 20 20 73  = P4_VTAB;.    s
1a354 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 28  qlite3VtabLock((
1a355 56 54 61 62 6c 65 20 2a 29 7a 50 34 29 3b 0a 20  VTable *)zP4);. 
1a356 20 20 20 61 73 73 65 72 74 28 20 28 28 56 54 61     assert( ((VTa
1a357 62 6c 65 20 2a 29 7a 50 34 29 2d 3e 64 62 3d 3d  ble *)zP4)->db==
1a358 70 2d 3e 64 62 20 29 3b 0a 20 20 7d 65 6c 73 65  p->db );.  }else
1a359 20 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20   if( n<0 ){.    
1a35a 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69  pOp->p4.p = (voi
1a35b 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d  d*)zP4;.    pOp-
1a35c 3e 70 34 74 79 70 65 20 3d 20 28 73 69 67 6e 65  >p4type = (signe
1a35d 64 20 63 68 61 72 29 6e 3b 0a 20 20 7d 65 6c 73  d char)n;.  }els
1a35e 65 7b 0a 20 20 20 20 69 66 28 20 6e 3d 3d 30 20  e{.    if( n==0 
1a35f 29 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72  ) n = sqlite3Str
1a360 6c 65 6e 33 30 28 7a 50 34 29 3b 0a 20 20 20 20  len30(zP4);.    
1a361 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 73 71 6c 69  pOp->p4.z = sqli
1a362 74 65 33 44 62 53 74 72 4e 44 75 70 28 70 2d 3e  te3DbStrNDup(p->
1a363 64 62 2c 20 7a 50 34 2c 20 6e 29 3b 0a 20 20 20  db, zP4, n);.   
1a364 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50   pOp->p4type = P
1a365 34 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 7d 0a 7d  4_DYNAMIC;.  }.}
1a366 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
1a367 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
1a368 65 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 65  e comment on the
1a369 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
1a36a 6c 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75 63  ly coded instruc
1a36b 74 69 6f 6e 2e 20 20 4f 72 0a 2a 2a 20 69 6e 73  tion.  Or.** ins
1a36c 65 72 74 20 61 20 4e 6f 2d 6f 70 20 61 6e 64 20  ert a No-op and 
1a36d 61 64 64 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20  add the comment 
1a36e 74 6f 20 74 68 61 74 20 6e 65 77 20 69 6e 73 74  to that new inst
1a36f 72 75 63 74 69 6f 6e 2e 20 20 54 68 69 73 0a 2a  ruction.  This.*
1a370 2a 20 6d 61 6b 65 73 20 74 68 65 20 63 6f 64 65  * makes the code
1a371 20 65 61 73 69 65 72 20 74 6f 20 72 65 61 64 20   easier to read 
1a372 64 75 72 69 6e 67 20 64 65 62 75 67 67 69 6e 67  during debugging
1a373 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 69 73 20  .  None of this 
1a374 68 61 70 70 65 6e 73 0a 2a 2a 20 69 6e 20 61 20  happens.** in a 
1a375 70 72 6f 64 75 63 74 69 6f 6e 20 62 75 69 6c 64  production build
1a376 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1a377 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1a378 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64 62 65  VdbeComment(Vdbe
1a379 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   *p, const char 
1a37a 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a  *zFormat, ...){.
1a37b 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
1a37c 69 66 28 20 21 70 20 29 20 72 65 74 75 72 6e 3b  if( !p ) return;
1a37d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f  .  assert( p->nO
1a37e 70 3e 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30  p>0 || p->aOp==0
1a37f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
1a380 3e 61 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f  >aOp==0 || p->aO
1a381 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d  p[p->nOp-1].zCom
1a382 6d 65 6e 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62  ment==0 || p->db
1a383 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1a384 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 20 29  ;.  if( p->nOp )
1a385 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 70 7a 20  {.    char **pz 
1a386 3d 20 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70  = &p->aOp[p->nOp
1a387 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20  -1].zComment;.  
1a388 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a    va_start(ap, z
1a389 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 73 71 6c  Format);.    sql
1a38a 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62  ite3DbFree(p->db
1a38b 2c 20 2a 70 7a 29 3b 0a 20 20 20 20 2a 70 7a 20  , *pz);.    *pz 
1a38c 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74  = sqlite3VMPrint
1a38d 66 28 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 74  f(p->db, zFormat
1a38e 2c 20 61 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e  , ap);.    va_en
1a38f 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c  d(ap);.  }.}.SQL
1a390 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1a391 20 73 71 6c 69 74 65 33 56 64 62 65 4e 6f 6f 70   sqlite3VdbeNoop
1a392 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c  Comment(Vdbe *p,
1a393 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
1a394 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  rmat, ...){.  va
1a395 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 66 28 20  _list ap;.  if( 
1a396 21 70 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73  !p ) return;.  s
1a397 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30  qlite3VdbeAddOp0
1a398 28 70 2c 20 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 20  (p, OP_Noop);.  
1a399 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30  assert( p->nOp>0
1a39a 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b   || p->aOp==0 );
1a39b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4f  .  assert( p->aO
1a39c 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70  p==0 || p->aOp[p
1a39d 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e  ->nOp-1].zCommen
1a39e 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d  t==0 || p->db->m
1a39f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
1a3a0 20 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20   if( p->nOp ){. 
1a3a1 20 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26     char **pz = &
1a3a2 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d  p->aOp[p->nOp-1]
1a3a3 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76  .zComment;.    v
1a3a4 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72  a_start(ap, zFor
1a3a5 6d 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  mat);.    sqlite
1a3a6 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a  3DbFree(p->db, *
1a3a7 70 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73  pz);.    *pz = s
1a3a8 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 70  qlite3VMPrintf(p
1a3a9 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61  ->db, zFormat, a
1a3aa 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 61  p);.    va_end(a
1a3ab 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66  p);.  }.}.#endif
1a3ac 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a    /* NDEBUG */..
1a3ad 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
1a3ae 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 20 67 69   opcode for a gi
1a3af 76 65 6e 20 61 64 64 72 65 73 73 2e 20 20 49 66  ven address.  If
1a3b0 20 74 68 65 20 61 64 64 72 65 73 73 20 69 73 20   the address is 
1a3b1 2d 31 2c 20 74 68 65 6e 0a 2a 2a 20 72 65 74 75  -1, then.** retu
1a3b2 72 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  rn the most rece
1a3b3 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20 6f 70  ntly inserted op
1a3b4 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  code..**.** If a
1a3b5 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
1a3b6 6f 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63  on error has occ
1a3b7 75 72 72 65 64 20 70 72 69 6f 72 20 74 6f 20 74  urred prior to t
1a3b8 68 65 20 63 61 6c 6c 69 6e 67 20 6f 66 20 74 68  he calling of th
1a3b9 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2c 20 74  is.** routine, t
1a3ba 68 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  hen a pointer to
1a3bb 20 61 20 64 75 6d 6d 79 20 56 64 62 65 4f 70 20   a dummy VdbeOp 
1a3bc 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64  will be returned
1a3bd 2e 20 20 54 68 61 74 20 6f 70 63 6f 64 65 0a 2a  .  That opcode.*
1a3be 2a 20 69 73 20 72 65 61 64 61 62 6c 65 20 61 6e  * is readable an
1a3bf 64 20 77 72 69 74 61 62 6c 65 2c 20 62 75 74 20  d writable, but 
1a3c0 69 74 20 68 61 73 20 6e 6f 20 65 66 66 65 63 74  it has no effect
1a3c1 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 6f 66  .  The return of
1a3c2 20 61 20 64 75 6d 6d 79 0a 2a 2a 20 6f 70 63 6f   a dummy.** opco
1a3c3 64 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 63 61  de allows the ca
1a3c4 6c 6c 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 66  ll to continue f
1a3c5 75 6e 63 74 69 6f 6e 69 6e 67 20 61 66 74 65 72  unctioning after
1a3c6 20 61 20 4f 4f 4d 20 66 61 75 6c 74 20 77 69 74   a OOM fault wit
1a3c7 68 6f 75 74 0a 2a 2a 20 68 61 76 69 6e 67 20 74  hout.** having t
1a3c8 6f 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69  o check to see i
1a3c9 66 20 74 68 65 20 72 65 74 75 72 6e 20 66 72 6f  f the return fro
1a3ca 6d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  m this routine i
1a3cb 73 20 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 65  s a valid pointe
1a3cc 72 2e 0a 2a 2a 0a 2a 2a 20 41 62 6f 75 74 20 74  r..**.** About t
1a3cd 68 65 20 23 69 66 64 65 66 20 53 51 4c 49 54 45  he #ifdef SQLITE
1a3ce 5f 4f 4d 49 54 5f 54 52 41 43 45 3a 20 20 4e 6f  _OMIT_TRACE:  No
1a3cf 72 6d 61 6c 6c 79 2c 20 74 68 69 73 20 72 6f 75  rmally, this rou
1a3d0 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61  tine is never ca
1a3d1 6c 6c 65 64 0a 2a 2a 20 75 6e 6c 65 73 73 20 70  lled.** unless p
1a3d2 2d 3e 6e 4f 70 3e 30 2e 20 20 54 68 69 73 20 69  ->nOp>0.  This i
1a3d3 73 20 62 65 63 61 75 73 65 20 69 6e 20 74 68 65  s because in the
1a3d4 20 61 62 73 65 6e 73 65 20 6f 66 20 53 51 4c 49   absense of SQLI
1a3d5 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 2c 0a 2a  TE_OMIT_TRACE,.*
1a3d6 2a 20 61 6e 20 4f 50 5f 54 72 61 63 65 20 69 6e  * an OP_Trace in
1a3d7 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6c 77  struction is alw
1a3d8 61 79 73 20 69 6e 73 65 72 74 65 64 20 62 79 20  ays inserted by 
1a3d9 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 28 29  sqlite3VdbeGet()
1a3da 20 61 73 20 73 6f 6f 6e 20 61 73 0a 2a 2a 20 61   as soon as.** a
1a3db 20 6e 65 77 20 56 44 42 45 20 69 73 20 63 72 65   new VDBE is cre
1a3dc 61 74 65 64 2e 20 20 53 6f 20 77 65 20 61 72 65  ated.  So we are
1a3dd 20 66 72 65 65 20 74 6f 20 73 65 74 20 61 64 64   free to set add
1a3de 72 20 74 6f 20 70 2d 3e 6e 4f 70 2d 31 20 77 69  r to p->nOp-1 wi
1a3df 74 68 6f 75 74 0a 2a 2a 20 68 61 76 69 6e 67 20  thout.** having 
1a3e0 74 6f 20 64 6f 75 62 6c 65 2d 63 68 65 63 6b 20  to double-check 
1a3e1 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
1a3e2 74 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  t the result is 
1a3e3 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 42 75  non-negative. Bu
1a3e4 74 0a 2a 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f  t.** if SQLITE_O
1a3e5 4d 49 54 5f 54 52 41 43 45 20 69 73 20 64 65 66  MIT_TRACE is def
1a3e6 69 6e 65 64 2c 20 74 68 65 20 4f 50 5f 54 72 61  ined, the OP_Tra
1a3e7 63 65 20 69 73 20 6f 6d 69 74 74 65 64 20 61 6e  ce is omitted an
1a3e8 64 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f 0a  d we do need to.
1a3e9 2a 2a 20 63 68 65 63 6b 20 74 68 65 20 76 61 6c  ** check the val
1a3ea 75 65 20 6f 66 20 70 2d 3e 6e 4f 70 2d 31 20 62  ue of p->nOp-1 b
1a3eb 65 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67  efore continuing
1a3ec 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1a3ed 41 54 45 20 56 64 62 65 4f 70 20 2a 73 71 6c 69  ATE VdbeOp *sqli
1a3ee 74 65 33 56 64 62 65 47 65 74 4f 70 28 56 64 62  te3VdbeGetOp(Vdb
1a3ef 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 29 7b  e *p, int addr){
1a3f0 0a 20 20 73 74 61 74 69 63 20 56 64 62 65 4f 70  .  static VdbeOp
1a3f1 20 64 75 6d 6d 79 3b 0a 20 20 61 73 73 65 72 74   dummy;.  assert
1a3f2 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45  ( p->magic==VDBE
1a3f3 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20  _MAGIC_INIT );. 
1a3f4 20 69 66 28 20 61 64 64 72 3c 30 20 29 7b 0a 23   if( addr<0 ){.#
1a3f5 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
1a3f6 54 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28 20  T_TRACE.    if( 
1a3f7 70 2d 3e 6e 4f 70 3d 3d 30 20 29 20 72 65 74 75  p->nOp==0 ) retu
1a3f8 72 6e 20 26 64 75 6d 6d 79 3b 0a 23 65 6e 64 69  rn &dummy;.#endi
1a3f9 66 0a 20 20 20 20 61 64 64 72 20 3d 20 70 2d 3e  f.    addr = p->
1a3fa 6e 4f 70 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 61  nOp - 1;.  }.  a
1a3fb 73 73 65 72 74 28 20 28 61 64 64 72 3e 3d 30 20  ssert( (addr>=0 
1a3fc 26 26 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 29 20  && addr<p->nOp) 
1a3fd 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  || p->db->malloc
1a3fe 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20  Failed );.  if( 
1a3ff 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  p->db->mallocFai
1a400 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72  led ){.    retur
1a401 6e 20 26 64 75 6d 6d 79 3b 0a 20 20 7d 65 6c 73  n &dummy;.  }els
1a402 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 70  e{.    return &p
1a403 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 20 7d  ->aOp[addr];.  }
1a404 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .}..#if !defined
1a405 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50  (SQLITE_OMIT_EXP
1a406 4c 41 49 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65  LAIN) || !define
1a407 64 28 4e 44 45 42 55 47 29 20 5c 0a 20 20 20 20  d(NDEBUG) \.    
1a408 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45   || defined(VDBE
1a409 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66  _PROFILE) || def
1a40a 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
1a40b 47 29 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65  G)./*.** Compute
1a40c 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 64   a string that d
1a40d 65 73 63 72 69 62 65 73 20 74 68 65 20 50 34 20  escribes the P4 
1a40e 70 61 72 61 6d 65 74 65 72 20 66 6f 72 20 61 6e  parameter for an
1a40f 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 55 73 65 20   opcode..** Use 
1a410 7a 54 65 6d 70 20 66 6f 72 20 61 6e 79 20 72 65  zTemp for any re
1a411 71 75 69 72 65 64 20 74 65 6d 70 6f 72 61 72 79  quired temporary
1a412 20 62 75 66 66 65 72 20 73 70 61 63 65 2e 0a 2a   buffer space..*
1a413 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 64  /.static char *d
1a414 69 73 70 6c 61 79 50 34 28 4f 70 20 2a 70 4f 70  isplayP4(Op *pOp
1a415 2c 20 63 68 61 72 20 2a 7a 54 65 6d 70 2c 20 69  , char *zTemp, i
1a416 6e 74 20 6e 54 65 6d 70 29 7b 0a 20 20 63 68 61  nt nTemp){.  cha
1a417 72 20 2a 7a 50 34 20 3d 20 7a 54 65 6d 70 3b 0a  r *zP4 = zTemp;.
1a418 20 20 61 73 73 65 72 74 28 20 6e 54 65 6d 70 3e    assert( nTemp>
1a419 3d 32 30 20 29 3b 0a 20 20 73 77 69 74 63 68 28  =20 );.  switch(
1a41a 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 29 7b 0a   pOp->p4type ){.
1a41b 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49      case P4_KEYI
1a41c 4e 46 4f 5f 53 54 41 54 49 43 3a 0a 20 20 20 20  NFO_STATIC:.    
1a41d 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 3a  case P4_KEYINFO:
1a41e 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 2c 20   {.      int i, 
1a41f 6a 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f  j;.      KeyInfo
1a420 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70   *pKeyInfo = pOp
1a421 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20  ->p4.pKeyInfo;. 
1a422 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
1a423 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65  rintf(nTemp, zTe
1a424 6d 70 2c 20 22 6b 65 79 69 6e 66 6f 28 25 64 22  mp, "keyinfo(%d"
1a425 2c 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65  , pKeyInfo->nFie
1a426 6c 64 29 3b 0a 20 20 20 20 20 20 69 20 3d 20 73  ld);.      i = s
1a427 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
1a428 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 66 6f 72  Temp);.      for
1a429 28 6a 3d 30 3b 20 6a 3c 70 4b 65 79 49 6e 66 6f  (j=0; j<pKeyInfo
1a42a 2d 3e 6e 46 69 65 6c 64 3b 20 6a 2b 2b 29 7b 0a  ->nField; j++){.
1a42b 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20          CollSeq 
1a42c 2a 70 43 6f 6c 6c 20 3d 20 70 4b 65 79 49 6e 66  *pColl = pKeyInf
1a42d 6f 2d 3e 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20  o->aColl[j];.   
1a42e 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29       if( pColl )
1a42f 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  {.          int 
1a430 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
1a431 6e 33 30 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65  n30(pColl->zName
1a432 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
1a433 20 69 2b 6e 3e 6e 54 65 6d 70 2d 36 20 29 7b 0a   i+n>nTemp-6 ){.
1a434 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63              memc
1a435 70 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 2e  py(&zTemp[i],",.
1a436 2e 2e 22 2c 34 29 3b 0a 20 20 20 20 20 20 20 20  ..",4);.        
1a437 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1a438 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
1a439 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2c   zTemp[i++] = ',
1a43a 27 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ';.          if(
1a43b 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74   pKeyInfo->aSort
1a43c 4f 72 64 65 72 20 26 26 20 70 4b 65 79 49 6e 66  Order && pKeyInf
1a43d 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6a 5d  o->aSortOrder[j]
1a43e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1a43f 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 2d 27  zTemp[i++] = '-'
1a440 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1a441 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
1a442 7a 54 65 6d 70 5b 69 5d 2c 20 70 43 6f 6c 6c 2d  zTemp[i], pColl-
1a443 3e 7a 4e 61 6d 65 2c 6e 2b 31 29 3b 0a 20 20 20  >zName,n+1);.   
1a444 20 20 20 20 20 20 20 69 20 2b 3d 20 6e 3b 0a 20         i += n;. 
1a445 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
1a446 20 69 2b 34 3c 6e 54 65 6d 70 2d 36 20 29 7b 0a   i+4<nTemp-6 ){.
1a447 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79            memcpy
1a448 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c 6e 69 6c  (&zTemp[i],",nil
1a449 22 2c 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ",4);.          
1a44a 69 20 2b 3d 20 34 3b 0a 20 20 20 20 20 20 20 20  i += 4;.        
1a44b 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
1a44c 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20 27 29 27  zTemp[i++] = ')'
1a44d 3b 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b 69 5d  ;.      zTemp[i]
1a44e 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65   = 0;.      asse
1a44f 72 74 28 20 69 3c 6e 54 65 6d 70 20 29 3b 0a 20  rt( i<nTemp );. 
1a450 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1a451 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 43 4f  }.    case P4_CO
1a452 4c 4c 53 45 51 3a 20 7b 0a 20 20 20 20 20 20 43  LLSEQ: {.      C
1a453 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20  ollSeq *pColl = 
1a454 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3b 0a 20  pOp->p4.pColl;. 
1a455 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
1a456 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65  rintf(nTemp, zTe
1a457 6d 70 2c 20 22 63 6f 6c 6c 73 65 71 28 25 2e 32  mp, "collseq(%.2
1a458 30 73 29 22 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61  0s)", pColl->zNa
1a459 6d 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  me);.      break
1a45a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
1a45b 20 50 34 5f 46 55 4e 43 44 45 46 3a 20 7b 0a 20   P4_FUNCDEF: {. 
1a45c 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44       FuncDef *pD
1a45d 65 66 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75  ef = pOp->p4.pFu
1a45e 6e 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  nc;.      sqlite
1a45f 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70  3_snprintf(nTemp
1a460 2c 20 7a 54 65 6d 70 2c 20 22 25 73 28 25 64 29  , zTemp, "%s(%d)
1a461 22 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20  ", pDef->zName, 
1a462 70 44 65 66 2d 3e 6e 41 72 67 29 3b 0a 20 20 20  pDef->nArg);.   
1a463 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1a464 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 36      case P4_INT6
1a465 34 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  4: {.      sqlit
1a466 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d  e3_snprintf(nTem
1a467 70 2c 20 7a 54 65 6d 70 2c 20 22 25 6c 6c 64 22  p, zTemp, "%lld"
1a468 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 49 36 34 29  , *pOp->p4.pI64)
1a469 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1a46a 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34     }.    case P4
1a46b 5f 49 4e 54 33 32 3a 20 7b 0a 20 20 20 20 20 20  _INT32: {.      
1a46c 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1a46d 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22  (nTemp, zTemp, "
1a46e 25 64 22 2c 20 70 4f 70 2d 3e 70 34 2e 69 29 3b  %d", pOp->p4.i);
1a46f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1a470 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f    }.    case P4_
1a471 52 45 41 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71  REAL: {.      sq
1a472 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
1a473 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 2e  Temp, zTemp, "%.
1a474 31 36 67 22 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70  16g", *pOp->p4.p
1a475 52 65 61 6c 29 3b 0a 20 20 20 20 20 20 62 72 65  Real);.      bre
1a476 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
1a477 73 65 20 50 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20  se P4_MEM: {.   
1a478 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 70     Mem *pMem = p
1a479 4f 70 2d 3e 70 34 2e 70 4d 65 6d 3b 0a 20 20 20  Op->p4.pMem;.   
1a47a 20 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d     assert( (pMem
1a47b 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
1a47c 6c 6c 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ll)==0 );.      
1a47d 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  if( pMem->flags 
1a47e 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20  & MEM_Str ){.   
1a47f 20 20 20 20 20 7a 50 34 20 3d 20 70 4d 65 6d 2d       zP4 = pMem-
1a480 3e 7a 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  >z;.      }else 
1a481 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  if( pMem->flags 
1a482 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20  & MEM_Int ){.   
1a483 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
1a484 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65  rintf(nTemp, zTe
1a485 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 70 4d 65 6d  mp, "%lld", pMem
1a486 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 20 20 7d 65  ->u.i);.      }e
1a487 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c  lse if( pMem->fl
1a488 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20 29  ags & MEM_Real )
1a489 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1a48a 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70  3_snprintf(nTemp
1a48b 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22  , zTemp, "%.16g"
1a48c 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a 20 20 20 20  , pMem->r);.    
1a48d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1a48e 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66   assert( pMem->f
1a48f 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20  lags & MEM_Blob 
1a490 29 3b 0a 20 20 20 20 20 20 20 20 7a 50 34 20 3d  );.        zP4 =
1a491 20 22 28 62 6c 6f 62 29 22 3b 0a 20 20 20 20 20   "(blob)";.     
1a492 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
1a493 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
1a494 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1a495 4c 54 41 42 4c 45 0a 20 20 20 20 63 61 73 65 20  LTABLE.    case 
1a496 50 34 5f 56 54 41 42 3a 20 7b 0a 20 20 20 20 20  P4_VTAB: {.     
1a497 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
1a498 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70  Vtab = pOp->p4.p
1a499 56 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 20  Vtab->pVtab;.   
1a49a 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1a49b 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70  ntf(nTemp, zTemp
1a49c 2c 20 22 76 74 61 62 3a 25 70 3a 25 70 22 2c 20  , "vtab:%p:%p", 
1a49d 70 56 74 61 62 2c 20 70 56 74 61 62 2d 3e 70 4d  pVtab, pVtab->pM
1a49e 6f 64 75 6c 65 29 3b 0a 20 20 20 20 20 20 62 72  odule);.      br
1a49f 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
1a4a0 66 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49 4e  f.    case P4_IN
1a4a1 54 41 52 52 41 59 3a 20 7b 0a 20 20 20 20 20 20  TARRAY: {.      
1a4a2 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1a4a3 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22  (nTemp, zTemp, "
1a4a4 69 6e 74 61 72 72 61 79 22 29 3b 0a 20 20 20 20  intarray");.    
1a4a5 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1a4a6 20 20 20 63 61 73 65 20 50 34 5f 53 55 42 50 52     case P4_SUBPR
1a4a7 4f 47 52 41 4d 3a 20 7b 0a 20 20 20 20 20 20 73  OGRAM: {.      s
1a4a8 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1a4a9 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 70  nTemp, zTemp, "p
1a4aa 72 6f 67 72 61 6d 22 29 3b 0a 20 20 20 20 20 20  rogram");.      
1a4ab 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1a4ac 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
1a4ad 20 20 7a 50 34 20 3d 20 70 4f 70 2d 3e 70 34 2e    zP4 = pOp->p4.
1a4ae 7a 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 50 34  z;.      if( zP4
1a4af 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  ==0 ){.        z
1a4b0 50 34 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 20 20  P4 = zTemp;.    
1a4b1 20 20 20 20 7a 54 65 6d 70 5b 30 5d 20 3d 20 30      zTemp[0] = 0
1a4b2 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1a4b3 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a 50    }.  assert( zP
1a4b4 34 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e  4!=0 );.  return
1a4b5 20 7a 50 34 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a   zP4;.}.#endif..
1a4b6 2f 2a 0a 2a 2a 20 44 65 63 6c 61 72 65 20 74 6f  /*.** Declare to
1a4b7 20 74 68 65 20 56 64 62 65 20 74 68 61 74 20 74   the Vdbe that t
1a4b8 68 65 20 42 54 72 65 65 20 6f 62 6a 65 63 74 20  he BTree object 
1a4b9 61 74 20 64 62 2d 3e 61 44 62 5b 69 5d 20 69 73  at db->aDb[i] is
1a4ba 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45   used..*/.SQLITE
1a4bb 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1a4bc 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72  lite3VdbeUsesBtr
1a4bd 65 65 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20  ee(Vdbe *p, int 
1a4be 69 29 7b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 0a  i){.  int mask;.
1a4bf 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
1a4c0 26 20 69 3c 70 2d 3e 64 62 2d 3e 6e 44 62 20 26  & i<p->db->nDb &
1a4c1 26 20 69 3c 73 69 7a 65 6f 66 28 75 33 32 29 2a  & i<sizeof(u32)*
1a4c2 38 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  8 );.  assert( i
1a4c3 3c 28 69 6e 74 29 73 69 7a 65 6f 66 28 70 2d 3e  <(int)sizeof(p->
1a4c4 62 74 72 65 65 4d 61 73 6b 29 2a 38 20 29 3b 0a  btreeMask)*8 );.
1a4c5 20 20 6d 61 73 6b 20 3d 20 28 28 75 33 32 29 31    mask = ((u32)1
1a4c6 29 3c 3c 69 3b 0a 20 20 69 66 28 20 28 70 2d 3e  )<<i;.  if( (p->
1a4c7 62 74 72 65 65 4d 61 73 6b 20 26 20 6d 61 73 6b  btreeMask & mask
1a4c8 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 62  )==0 ){.    p->b
1a4c9 74 72 65 65 4d 61 73 6b 20 7c 3d 20 6d 61 73 6b  treeMask |= mask
1a4ca 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
1a4cb 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 65  eeMutexArrayInse
1a4cc 72 74 28 26 70 2d 3e 61 4d 75 74 65 78 2c 20 70  rt(&p->aMutex, p
1a4cd 2d 3e 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  ->db->aDb[i].pBt
1a4ce 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 23 69 66 20 64  );.  }.}...#if d
1a4cf 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46  efined(VDBE_PROF
1a4d0 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  ILE) || defined(
1a4d1 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a  SQLITE_DEBUG)./*
1a4d2 0a 2a 2a 20 50 72 69 6e 74 20 61 20 73 69 6e 67  .** Print a sing
1a4d3 6c 65 20 6f 70 63 6f 64 65 2e 20 20 54 68 69 73  le opcode.  This
1a4d4 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
1a4d5 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 6f   for debugging o
1a4d6 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nly..*/.SQLITE_P
1a4d7 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1a4d8 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 46  te3VdbePrintOp(F
1a4d9 49 4c 45 20 2a 70 4f 75 74 2c 20 69 6e 74 20 70  ILE *pOut, int p
1a4da 63 2c 20 4f 70 20 2a 70 4f 70 29 7b 0a 20 20 63  c, Op *pOp){.  c
1a4db 68 61 72 20 2a 7a 50 34 3b 0a 20 20 63 68 61 72  har *zP4;.  char
1a4dc 20 7a 50 74 72 5b 35 30 5d 3b 0a 20 20 73 74 61   zPtr[50];.  sta
1a4dd 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
1a4de 7a 46 6f 72 6d 61 74 31 20 3d 20 22 25 34 64 20  zFormat1 = "%4d 
1a4df 25 2d 31 33 73 20 25 34 64 20 25 34 64 20 25 34  %-13s %4d %4d %4
1a4e0 64 20 25 2d 34 73 20 25 2e 32 58 20 25 73 5c 6e  d %-4s %.2X %s\n
1a4e1 22 3b 0a 20 20 69 66 28 20 70 4f 75 74 3d 3d 30  ";.  if( pOut==0
1a4e2 20 29 20 70 4f 75 74 20 3d 20 73 74 64 6f 75 74   ) pOut = stdout
1a4e3 3b 0a 20 20 7a 50 34 20 3d 20 64 69 73 70 6c 61  ;.  zP4 = displa
1a4e4 79 50 34 28 70 4f 70 2c 20 7a 50 74 72 2c 20 73  yP4(pOp, zPtr, s
1a4e5 69 7a 65 6f 66 28 7a 50 74 72 29 29 3b 0a 20 20  izeof(zPtr));.  
1a4e6 66 70 72 69 6e 74 66 28 70 4f 75 74 2c 20 7a 46  fprintf(pOut, zF
1a4e7 6f 72 6d 61 74 31 2c 20 70 63 2c 20 0a 20 20 20  ormat1, pc, .   
1a4e8 20 20 20 73 71 6c 69 74 65 33 4f 70 63 6f 64 65     sqlite3Opcode
1a4e9 4e 61 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64 65  Name(pOp->opcode
1a4ea 29 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d  ), pOp->p1, pOp-
1a4eb 3e 70 32 2c 20 70 4f 70 2d 3e 70 33 2c 20 7a 50  >p2, pOp->p3, zP
1a4ec 34 2c 20 70 4f 70 2d 3e 70 35 2c 0a 23 69 66 64  4, pOp->p5,.#ifd
1a4ed 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
1a4ee 20 20 20 20 20 20 70 4f 70 2d 3e 7a 43 6f 6d 6d        pOp->zComm
1a4ef 65 6e 74 20 3f 20 70 4f 70 2d 3e 7a 43 6f 6d 6d  ent ? pOp->zComm
1a4f0 65 6e 74 20 3a 20 22 22 0a 23 65 6c 73 65 0a 20  ent : "".#else. 
1a4f1 20 20 20 20 20 22 22 0a 23 65 6e 64 69 66 0a 20       "".#endif. 
1a4f2 20 29 3b 0a 20 20 66 66 6c 75 73 68 28 70 4f 75   );.  fflush(pOu
1a4f3 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  t);.}.#endif../*
1a4f4 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 6e 20 61  .** Release an a
1a4f5 72 72 61 79 20 6f 66 20 4e 20 4d 65 6d 20 65 6c  rray of N Mem el
1a4f6 65 6d 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63  ements.*/.static
1a4f7 20 76 6f 69 64 20 72 65 6c 65 61 73 65 4d 65 6d   void releaseMem
1a4f8 41 72 72 61 79 28 4d 65 6d 20 2a 70 2c 20 69 6e  Array(Mem *p, in
1a4f9 74 20 4e 29 7b 0a 20 20 69 66 28 20 70 20 26 26  t N){.  if( p &&
1a4fa 20 4e 20 29 7b 0a 20 20 20 20 4d 65 6d 20 2a 70   N ){.    Mem *p
1a4fb 45 6e 64 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  End;.    sqlite3
1a4fc 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20   *db = p->db;.  
1a4fd 20 20 75 38 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c    u8 malloc_fail
1a4fe 65 64 20 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ed = db->mallocF
1a4ff 61 69 6c 65 64 3b 0a 20 20 20 20 66 6f 72 28 70  ailed;.    for(p
1a500 45 6e 64 3d 26 70 5b 4e 5d 3b 20 70 3c 70 45 6e  End=&p[N]; p<pEn
1a501 64 3b 20 70 2b 2b 29 7b 0a 20 20 20 20 20 20 61  d; p++){.      a
1a502 73 73 65 72 74 28 20 28 26 70 5b 31 5d 29 3d 3d  ssert( (&p[1])==
1a503 70 45 6e 64 20 7c 7c 20 70 5b 30 5d 2e 64 62 3d  pEnd || p[0].db=
1a504 3d 70 5b 31 5d 2e 64 62 20 29 3b 0a 0a 20 20 20  =p[1].db );..   
1a505 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b     /* This block
1a506 20 69 73 20 72 65 61 6c 6c 79 20 61 6e 20 69 6e   is really an in
1a507 6c 69 6e 65 64 20 76 65 72 73 69 6f 6e 20 6f 66  lined version of
1a508 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1a509 65 6c 65 61 73 65 28 29 0a 20 20 20 20 20 20 2a  elease().      *
1a50a 2a 20 74 68 61 74 20 74 61 6b 65 73 20 61 64 76  * that takes adv
1a50b 61 6e 74 61 67 65 20 6f 66 20 74 68 65 20 66 61  antage of the fa
1a50c 63 74 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f  ct that the memo
1a50d 72 79 20 63 65 6c 6c 20 76 61 6c 75 65 20 69 73  ry cell value is
1a50e 20 0a 20 20 20 20 20 20 2a 2a 20 62 65 69 6e 67   .      ** being
1a50f 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20 61 66 74   set to NULL aft
1a510 65 72 20 72 65 6c 65 61 73 69 6e 67 20 61 6e 79  er releasing any
1a511 20 64 79 6e 61 6d 69 63 20 72 65 73 6f 75 72 63   dynamic resourc
1a512 65 73 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  es..      **.   
1a513 20 20 20 2a 2a 20 54 68 65 20 6a 75 73 74 69 66     ** The justif
1a514 69 63 61 74 69 6f 6e 20 66 6f 72 20 64 75 70 6c  ication for dupl
1a515 69 63 61 74 69 6e 67 20 63 6f 64 65 20 69 73 20  icating code is 
1a516 74 68 61 74 20 61 63 63 6f 72 64 69 6e 67 20 74  that according t
1a517 6f 20 0a 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c  o .      ** call
1a518 67 72 69 6e 64 2c 20 74 68 69 73 20 63 61 75 73  grind, this caus
1a519 65 73 20 61 20 63 65 72 74 61 69 6e 20 74 65 73  es a certain tes
1a51a 74 20 63 61 73 65 20 74 6f 20 68 69 74 20 74 68  t case to hit th
1a51b 65 20 43 50 55 20 34 2e 37 20 0a 20 20 20 20 20  e CPU 4.7 .     
1a51c 20 2a 2a 20 70 65 72 63 65 6e 74 20 6c 65 73 73   ** percent less
1a51d 20 28 78 38 36 20 6c 69 6e 75 78 2c 20 67 63 63   (x86 linux, gcc
1a51e 20 76 65 72 73 69 6f 6e 20 34 2e 31 2e 32 2c 20   version 4.1.2, 
1a51f 2d 4f 36 29 20 74 68 61 6e 20 69 66 20 0a 20 20  -O6) than if .  
1a520 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 65      ** sqlite3Me
1a521 6d 52 65 6c 65 61 73 65 28 29 20 77 65 72 65 20  mRelease() were 
1a522 63 61 6c 6c 65 64 20 66 72 6f 6d 20 68 65 72 65  called from here
1a523 2e 20 57 69 74 68 20 2d 4f 32 2c 20 74 68 69 73  . With -O2, this
1a524 20 6a 75 6d 70 73 0a 20 20 20 20 20 20 2a 2a 20   jumps.      ** 
1a525 74 6f 20 36 2e 36 20 70 65 72 63 65 6e 74 2e 20  to 6.6 percent. 
1a526 54 68 65 20 74 65 73 74 20 63 61 73 65 20 69 73  The test case is
1a527 20 69 6e 73 65 72 74 69 6e 67 20 31 30 30 30 20   inserting 1000 
1a528 72 6f 77 73 20 69 6e 74 6f 20 61 20 74 61 62 6c  rows into a tabl
1a529 65 20 0a 20 20 20 20 20 20 2a 2a 20 77 69 74 68  e .      ** with
1a52a 20 6e 6f 20 69 6e 64 65 78 65 73 20 75 73 69 6e   no indexes usin
1a52b 67 20 61 20 73 69 6e 67 6c 65 20 70 72 65 70 61  g a single prepa
1a52c 72 65 64 20 49 4e 53 45 52 54 20 73 74 61 74 65  red INSERT state
1a52d 6d 65 6e 74 2c 20 62 69 6e 64 28 29 20 0a 20 20  ment, bind() .  
1a52e 20 20 20 20 2a 2a 20 61 6e 64 20 72 65 73 65 74      ** and reset
1a52f 28 29 2e 20 49 6e 73 65 72 74 73 20 61 72 65 20  (). Inserts are 
1a530 67 72 6f 75 70 65 64 20 69 6e 74 6f 20 61 20 74  grouped into a t
1a531 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20  ransaction..    
1a532 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70    */.      if( p
1a533 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 41 67 67  ->flags&(MEM_Agg
1a534 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 46 72 61  |MEM_Dyn|MEM_Fra
1a535 6d 65 7c 4d 45 4d 5f 52 6f 77 53 65 74 29 20 29  me|MEM_RowSet) )
1a536 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1a537 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
1a538 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  p);.      }else 
1a539 69 66 28 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 29  if( p->zMalloc )
1a53a 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1a53b 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
1a53c 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20  Malloc);.       
1a53d 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b   p->zMalloc = 0;
1a53e 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
1a53f 70 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  p->flags = MEM_N
1a540 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  ull;.    }.    d
1a541 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1a542 3d 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b  = malloc_failed;
1a543 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  .  }.}../*.** De
1a544 6c 65 74 65 20 61 20 56 64 62 65 46 72 61 6d 65  lete a VdbeFrame
1a545 20 6f 62 6a 65 63 74 20 61 6e 64 20 69 74 73 20   object and its 
1a546 63 6f 6e 74 65 6e 74 73 2e 20 56 64 62 65 46 72  contents. VdbeFr
1a547 61 6d 65 20 6f 62 6a 65 63 74 73 20 61 72 65 0a  ame objects are.
1a548 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  ** allocated by 
1a549 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 6f  the OP_Program o
1a54a 70 63 6f 64 65 20 69 6e 20 73 71 6c 69 74 65 33  pcode in sqlite3
1a54b 56 64 62 65 45 78 65 63 28 29 2e 0a 2a 2f 0a 53  VdbeExec()..*/.S
1a54c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1a54d 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 72  id sqlite3VdbeFr
1a54e 61 6d 65 44 65 6c 65 74 65 28 56 64 62 65 46 72  ameDelete(VdbeFr
1a54f 61 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  ame *p){.  int i
1a550 3b 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 20 3d 20  ;.  Mem *aMem = 
1a551 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 70 29 3b  VdbeFrameMem(p);
1a552 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 2a  .  VdbeCursor **
1a553 61 70 43 73 72 20 3d 20 28 56 64 62 65 43 75 72  apCsr = (VdbeCur
1a554 73 6f 72 20 2a 2a 29 26 61 4d 65 6d 5b 70 2d 3e  sor **)&aMem[p->
1a555 6e 43 68 69 6c 64 4d 65 6d 5d 3b 0a 20 20 66 6f  nChildMem];.  fo
1a556 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 68 69  r(i=0; i<p->nChi
1a557 6c 64 43 73 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  ldCsr; i++){.   
1a558 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65   sqlite3VdbeFree
1a559 43 75 72 73 6f 72 28 70 2d 3e 76 2c 20 61 70 43  Cursor(p->v, apC
1a55a 73 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 72 65  sr[i]);.  }.  re
1a55b 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 61 4d  leaseMemArray(aM
1a55c 65 6d 2c 20 70 2d 3e 6e 43 68 69 6c 64 4d 65 6d  em, p->nChildMem
1a55d 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
1a55e 65 65 28 70 2d 3e 76 2d 3e 64 62 2c 20 70 29 3b  ee(p->v->db, p);
1a55f 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  .}...#ifdef SQLI
1a560 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59  TE_ENABLE_MEMORY
1a561 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 53 51 4c 49  _MANAGEMENT.SQLI
1a562 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1a563 71 6c 69 74 65 33 56 64 62 65 52 65 6c 65 61 73  qlite3VdbeReleas
1a564 65 42 75 66 66 65 72 73 28 56 64 62 65 20 2a 70  eBuffers(Vdbe *p
1a565 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69  ){.  int ii;.  i
1a566 6e 74 20 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20  nt nFree = 0;.  
1a567 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1a568 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62  mutex_held(p->db
1a569 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f  ->mutex) );.  fo
1a56a 72 28 69 69 3d 31 3b 20 69 69 3c 3d 70 2d 3e 6e  r(ii=1; ii<=p->n
1a56b 4d 65 6d 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  Mem; ii++){.    
1a56c 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26 70 2d 3e  Mem *pMem = &p->
1a56d 61 4d 65 6d 5b 69 69 5d 3b 0a 20 20 20 20 69 66  aMem[ii];.    if
1a56e 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20  ( pMem->flags & 
1a56f 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20 20  MEM_RowSet ){.  
1a570 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53 65      sqlite3RowSe
1a571 74 43 6c 65 61 72 28 70 4d 65 6d 2d 3e 75 2e 70  tClear(pMem->u.p
1a572 52 6f 77 53 65 74 29 3b 0a 20 20 20 20 7d 0a 20  RowSet);.    }. 
1a573 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 26     if( pMem->z &
1a574 26 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45  & pMem->flags&ME
1a575 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 61  M_Dyn ){.      a
1a576 73 73 65 72 74 28 20 21 70 4d 65 6d 2d 3e 78 44  ssert( !pMem->xD
1a577 65 6c 20 29 3b 0a 20 20 20 20 20 20 6e 46 72 65  el );.      nFre
1a578 65 20 2b 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  e += sqlite3DbMa
1a579 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d 2d 3e 64  llocSize(pMem->d
1a57a 62 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20  b, pMem->z);.   
1a57b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1a57c 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a  mRelease(pMem);.
1a57d 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1a57e 72 6e 20 6e 46 72 65 65 3b 0a 7d 0a 23 65 6e 64  rn nFree;.}.#end
1a57f 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
1a580 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a  TE_OMIT_EXPLAIN.
1a581 2f 2a 0a 2a 2a 20 47 69 76 65 20 61 20 6c 69 73  /*.** Give a lis
1a582 74 69 6e 67 20 6f 66 20 74 68 65 20 70 72 6f 67  ting of the prog
1a583 72 61 6d 20 69 6e 20 74 68 65 20 76 69 72 74 75  ram in the virtu
1a584 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a  al machine..**.*
1a585 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20  * The interface 
1a586 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 73  is the same as s
1a587 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29  qlite3VdbeExec()
1a588 2e 20 20 42 75 74 20 69 6e 73 74 65 61 64 20 6f  .  But instead o
1a589 66 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 74 68 65  f.** running the
1a58a 20 63 6f 64 65 2c 20 69 74 20 69 6e 76 6f 6b 65   code, it invoke
1a58b 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f  s the callback o
1a58c 6e 63 65 20 66 6f 72 20 65 61 63 68 20 69 6e 73  nce for each ins
1a58d 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69  truction..** Thi
1a58e 73 20 66 65 61 74 75 72 65 20 69 73 20 75 73 65  s feature is use
1a58f 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 22  d to implement "
1a590 45 58 50 4c 41 49 4e 22 2e 0a 2a 2a 0a 2a 2a 20  EXPLAIN"..**.** 
1a591 57 68 65 6e 20 70 2d 3e 65 78 70 6c 61 69 6e 3d  When p->explain=
1a592 3d 31 2c 20 65 61 63 68 20 69 6e 73 74 72 75 63  =1, each instruc
1a593 74 69 6f 6e 20 69 73 20 6c 69 73 74 65 64 2e 20  tion is listed. 
1a594 20 57 68 65 6e 0a 2a 2a 20 70 2d 3e 65 78 70 6c   When.** p->expl
1a595 61 69 6e 3d 3d 32 2c 20 6f 6e 6c 79 20 4f 50 5f  ain==2, only OP_
1a596 45 78 70 6c 61 69 6e 20 69 6e 73 74 72 75 63 74  Explain instruct
1a597 69 6f 6e 73 20 61 72 65 20 6c 69 73 74 65 64 20  ions are listed 
1a598 61 6e 64 20 74 68 65 73 65 0a 2a 2a 20 61 72 65  and these.** are
1a599 20 73 68 6f 77 6e 20 69 6e 20 61 20 64 69 66 66   shown in a diff
1a59a 65 72 65 6e 74 20 66 6f 72 6d 61 74 2e 20 20 70  erent format.  p
1a59b 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 69 73 20  ->explain==2 is 
1a59c 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
1a59d 74 0a 2a 2a 20 45 58 50 4c 41 49 4e 20 51 55 45  t.** EXPLAIN QUE
1a59e 52 59 20 50 4c 41 4e 2e 0a 2a 2f 0a 53 51 4c 49  RY PLAN..*/.SQLI
1a59f 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1a5a0 71 6c 69 74 65 33 56 64 62 65 4c 69 73 74 28 0a  qlite3VdbeList(.
1a5a1 20 20 56 64 62 65 20 2a 70 20 20 20 20 20 20 20    Vdbe *p       
1a5a2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1a5a3 68 65 20 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 20  he VDBE */.){.  
1a5a4 69 6e 74 20 6e 52 6f 77 3b 20 20 20 20 20 20 20  int nRow;       
1a5a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a5a6 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75       /* Total nu
1a5a7 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 6f 20  mber of rows to 
1a5a8 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20  return */.  int 
1a5a9 6e 53 75 62 20 3d 20 30 3b 20 20 20 20 20 20 20  nSub = 0;       
1a5aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a5ab 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 75   /* Number of su
1a5ac 62 2d 76 64 62 65 73 20 73 65 65 6e 20 73 6f 20  b-vdbes seen so 
1a5ad 66 61 72 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67  far */.  SubProg
1a5ae 72 61 6d 20 2a 2a 61 70 53 75 62 20 3d 20 30 3b  ram **apSub = 0;
1a5af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a5b0 20 41 72 72 61 79 20 6f 66 20 73 75 62 2d 76 64   Array of sub-vd
1a5b1 62 65 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 53  bes */.  Mem *pS
1a5b2 75 62 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  ub = 0;.  sqlite
1a5b3 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20  3 *db = p->db;. 
1a5b4 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63   int i;.  int rc
1a5b5 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1a5b6 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 70 2d 3e 70  Mem *pMem = p->p
1a5b7 52 65 73 75 6c 74 53 65 74 20 3d 20 26 70 2d 3e  ResultSet = &p->
1a5b8 61 4d 65 6d 5b 31 5d 3b 0a 0a 20 20 61 73 73 65  aMem[1];..  asse
1a5b9 72 74 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29  rt( p->explain )
1a5ba 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d  ;.  assert( p->m
1a5bb 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
1a5bc 5f 52 55 4e 20 29 3b 0a 20 20 61 73 73 65 72 74  _RUN );.  assert
1a5bd 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c  ( db->magic==SQL
1a5be 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29  ITE_MAGIC_BUSY )
1a5bf 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72  ;.  assert( p->r
1a5c0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
1a5c1 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55  p->rc==SQLITE_BU
1a5c2 53 59 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c  SY || p->rc==SQL
1a5c3 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 0a 20 20  ITE_NOMEM );..  
1a5c4 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20 74  /* Even though t
1a5c5 68 69 73 20 6f 70 63 6f 64 65 20 64 6f 65 73 20  his opcode does 
1a5c6 6e 6f 74 20 75 73 65 20 64 79 6e 61 6d 69 63 20  not use dynamic 
1a5c7 73 74 72 69 6e 67 73 20 66 6f 72 0a 20 20 2a 2a  strings for.  **
1a5c8 20 74 68 65 20 72 65 73 75 6c 74 2c 20 72 65 73   the result, res
1a5c9 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 6d 61 79 20  ult columns may 
1a5ca 62 65 63 6f 6d 65 20 64 79 6e 61 6d 69 63 20 69  become dynamic i
1a5cb 66 20 74 68 65 20 75 73 65 72 20 63 61 6c 6c 73  f the user calls
1a5cc 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f  .  ** sqlite3_co
1a5cd 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c 20 63  lumn_text16(), c
1a5ce 61 75 73 69 6e 67 20 61 20 74 72 61 6e 73 6c 61  ausing a transla
1a5cf 74 69 6f 6e 20 74 6f 20 55 54 46 2d 31 36 20 65  tion to UTF-16 e
1a5d0 6e 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20  ncoding..  */.  
1a5d1 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28  releaseMemArray(
1a5d2 70 4d 65 6d 2c 20 38 29 3b 0a 0a 20 20 69 66 28  pMem, 8);..  if(
1a5d3 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e   p->rc==SQLITE_N
1a5d4 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 54  OMEM ){.    /* T
1a5d5 68 69 73 20 68 61 70 70 65 6e 73 20 69 66 20 61  his happens if a
1a5d6 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 69 64 65   malloc() inside
1a5d7 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
1a5d8 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29  e3_column_text()
1a5d9 20 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74   or.    ** sqlit
1a5da 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
1a5db 28 29 20 66 61 69 6c 65 64 2e 20 20 2a 2f 0a 20  () failed.  */. 
1a5dc 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
1a5dd 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74  led = 1;.    ret
1a5de 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
1a5df 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75  ;.  }..  /* Figu
1a5e0 72 65 20 6f 75 74 20 74 6f 74 61 6c 20 6e 75 6d  re out total num
1a5e1 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 74  ber of rows that
1a5e2 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65   will be returne
1a5e3 64 20 62 79 20 74 68 69 73 20 0a 20 20 2a 2a 20  d by this .  ** 
1a5e4 45 58 50 4c 41 49 4e 20 70 72 6f 67 72 61 6d 2e  EXPLAIN program.
1a5e5 20 20 2a 2f 0a 20 20 6e 52 6f 77 20 3d 20 70 2d    */.  nRow = p-
1a5e6 3e 6e 4f 70 3b 0a 20 20 69 66 28 20 70 2d 3e 65  >nOp;.  if( p->e
1a5e7 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20  xplain==1 ){.   
1a5e8 20 70 53 75 62 20 3d 20 26 70 2d 3e 61 4d 65 6d   pSub = &p->aMem
1a5e9 5b 39 5d 3b 0a 20 20 20 20 69 66 28 20 70 53 75  [9];.    if( pSu
1a5ea 62 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f  b->flags&MEM_Blo
1a5eb 62 20 29 7b 0a 20 20 20 20 20 20 6e 53 75 62 20  b ){.      nSub 
1a5ec 3d 20 70 53 75 62 2d 3e 6e 2f 73 69 7a 65 6f 66  = pSub->n/sizeof
1a5ed 28 56 64 62 65 2a 29 3b 0a 20 20 20 20 20 20 61  (Vdbe*);.      a
1a5ee 70 53 75 62 20 3d 20 28 53 75 62 50 72 6f 67 72  pSub = (SubProgr
1a5ef 61 6d 20 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20  am **)pSub->z;. 
1a5f0 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30     }.    for(i=0
1a5f1 3b 20 69 3c 6e 53 75 62 3b 20 69 2b 2b 29 7b 0a  ; i<nSub; i++){.
1a5f2 20 20 20 20 20 20 6e 52 6f 77 20 2b 3d 20 61 70        nRow += ap
1a5f3 53 75 62 5b 69 5d 2d 3e 6e 4f 70 3b 0a 20 20 20  Sub[i]->nOp;.   
1a5f4 20 7d 0a 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20   }.  }..  do{.  
1a5f5 20 20 69 20 3d 20 70 2d 3e 70 63 2b 2b 3b 0a 20    i = p->pc++;. 
1a5f6 20 7d 77 68 69 6c 65 28 20 69 3c 6e 52 6f 77 20   }while( i<nRow 
1a5f7 26 26 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32  && p->explain==2
1a5f8 20 26 26 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70   && p->aOp[i].op
1a5f9 63 6f 64 65 21 3d 4f 50 5f 45 78 70 6c 61 69 6e  code!=OP_Explain
1a5fa 20 29 3b 0a 20 20 69 66 28 20 69 3e 3d 6e 52 6f   );.  if( i>=nRo
1a5fb 77 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d  w ){.    p->rc =
1a5fc 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
1a5fd 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45  rc = SQLITE_DONE
1a5fe 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64 62  ;.  }else if( db
1a5ff 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74  ->u1.isInterrupt
1a600 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20  ed ){.    p->rc 
1a601 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55  = SQLITE_INTERRU
1a602 50 54 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  PT;.    rc = SQL
1a603 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73  ITE_ERROR;.    s
1a604 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
1a605 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c  &p->zErrMsg, db,
1a606 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45 72   "%s", sqlite3Er
1a607 72 53 74 72 28 70 2d 3e 72 63 29 29 3b 0a 20 20  rStr(p->rc));.  
1a608 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20  }else{.    char 
1a609 2a 7a 3b 0a 20 20 20 20 4f 70 20 2a 70 4f 70 3b  *z;.    Op *pOp;
1a60a 0a 20 20 20 20 69 66 28 20 69 3c 70 2d 3e 6e 4f  .    if( i<p->nO
1a60b 70 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 20 3d  p ){.      pOp =
1a60c 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20   &p->aOp[i];.   
1a60d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e   }else{.      in
1a60e 74 20 6a 3b 0a 20 20 20 20 20 20 69 20 2d 3d 20  t j;.      i -= 
1a60f 70 2d 3e 6e 4f 70 3b 0a 20 20 20 20 20 20 66 6f  p->nOp;.      fo
1a610 72 28 6a 3d 30 3b 20 69 3e 3d 61 70 53 75 62 5b  r(j=0; i>=apSub[
1a611 6a 5d 2d 3e 6e 4f 70 3b 20 6a 2b 2b 29 7b 0a 20  j]->nOp; j++){. 
1a612 20 20 20 20 20 20 20 69 20 2d 3d 20 61 70 53 75         i -= apSu
1a613 62 5b 6a 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20 20  b[j]->nOp;.     
1a614 20 7d 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 26   }.      pOp = &
1a615 61 70 53 75 62 5b 6a 5d 2d 3e 61 4f 70 5b 69 5d  apSub[j]->aOp[i]
1a616 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1a617 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b  p->explain==1 ){
1a618 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  .      pMem->fla
1a619 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20  gs = MEM_Int;.  
1a61a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d      pMem->type =
1a61b 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b   SQLITE_INTEGER;
1a61c 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69  .      pMem->u.i
1a61d 20 3d 20 69 3b 20 20 20 20 20 20 20 20 20 20 20   = i;           
1a61e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a61f 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20       /* Program 
1a620 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20  counter */.     
1a621 20 70 4d 65 6d 2b 2b 3b 0a 20 20 0a 20 20 20 20   pMem++;.  .    
1a622 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
1a623 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 53  MEM_Static|MEM_S
1a624 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20  tr|MEM_Term;.   
1a625 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68     pMem->z = (ch
1a626 61 72 2a 29 73 71 6c 69 74 65 33 4f 70 63 6f 64  ar*)sqlite3Opcod
1a627 65 4e 61 6d 65 28 70 4f 70 2d 3e 6f 70 63 6f 64  eName(pOp->opcod
1a628 65 29 3b 20 20 2f 2a 20 4f 70 63 6f 64 65 20 2a  e);  /* Opcode *
1a629 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  /.      assert( 
1a62a 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20  pMem->z!=0 );.  
1a62b 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71      pMem->n = sq
1a62c 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4d  lite3Strlen30(pM
1a62d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d  em->z);.      pM
1a62e 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  em->type = SQLIT
1a62f 45 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 70 4d  E_TEXT;.      pM
1a630 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45  em->enc = SQLITE
1a631 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 4d 65  _UTF8;.      pMe
1a632 6d 2b 2b 3b 0a 0a 20 20 20 20 20 20 69 66 28 20  m++;..      if( 
1a633 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
1a634 53 55 42 50 52 4f 47 52 41 4d 20 29 7b 0a 20 20  SUBPROGRAM ){.  
1a635 20 20 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20        int nByte 
1a636 3d 20 28 6e 53 75 62 2b 31 29 2a 73 69 7a 65 6f  = (nSub+1)*sizeo
1a637 66 28 53 75 62 50 72 6f 67 72 61 6d 2a 29 3b 0a  f(SubProgram*);.
1a638 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20          int j;. 
1a639 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20         for(j=0; 
1a63a 6a 3c 6e 53 75 62 3b 20 6a 2b 2b 29 7b 0a 20 20  j<nSub; j++){.  
1a63b 20 20 20 20 20 20 20 20 69 66 28 20 61 70 53 75          if( apSu
1a63c 62 5b 6a 5d 3d 3d 70 4f 70 2d 3e 70 34 2e 70 50  b[j]==pOp->p4.pP
1a63d 72 6f 67 72 61 6d 20 29 20 62 72 65 61 6b 3b 0a  rogram ) break;.
1a63e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1a63f 20 20 69 66 28 20 6a 3d 3d 6e 53 75 62 20 26 26    if( j==nSub &&
1a640 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69   SQLITE_OK==sqli
1a641 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
1a642 53 75 62 2c 20 6e 42 79 74 65 2c 20 31 29 20 29  Sub, nByte, 1) )
1a643 7b 0a 20 20 20 20 20 20 20 20 20 20 61 70 53 75  {.          apSu
1a644 62 20 3d 20 28 53 75 62 50 72 6f 67 72 61 6d 20  b = (SubProgram 
1a645 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20 20 20 20  **)pSub->z;.    
1a646 20 20 20 20 20 20 61 70 53 75 62 5b 6e 53 75 62        apSub[nSub
1a647 2b 2b 5d 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 50  ++] = pOp->p4.pP
1a648 72 6f 67 72 61 6d 3b 0a 20 20 20 20 20 20 20 20  rogram;.        
1a649 20 20 70 53 75 62 2d 3e 66 6c 61 67 73 20 7c 3d    pSub->flags |=
1a64a 20 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20 20   MEM_Blob;.     
1a64b 20 20 20 20 20 70 53 75 62 2d 3e 6e 20 3d 20 6e       pSub->n = n
1a64c 53 75 62 2a 73 69 7a 65 6f 66 28 53 75 62 50 72  Sub*sizeof(SubPr
1a64d 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 20 20 20 20  ogram*);.       
1a64e 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
1a64f 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  ..    pMem->flag
1a650 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20  s = MEM_Int;.   
1a651 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70   pMem->u.i = pOp
1a652 2d 3e 70 31 3b 20 20 20 20 20 20 20 20 20 20 20  ->p1;           
1a653 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a654 2a 20 50 31 20 2a 2f 0a 20 20 20 20 70 4d 65 6d  * P1 */.    pMem
1a655 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
1a656 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d 65  INTEGER;.    pMe
1a657 6d 2b 2b 3b 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e  m++;..    pMem->
1a658 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
1a659 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d  .    pMem->u.i =
1a65a 20 70 4f 70 2d 3e 70 32 3b 20 20 20 20 20 20 20   pOp->p2;       
1a65b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a65c 20 20 20 2f 2a 20 50 32 20 2a 2f 0a 20 20 20 20     /* P2 */.    
1a65d 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
1a65e 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  ITE_INTEGER;.   
1a65f 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69 66   pMem++;..    if
1a660 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20  ( p->explain==1 
1a661 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  ){.      pMem->f
1a662 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
1a663 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20        pMem->u.i 
1a664 3d 20 70 4f 70 2d 3e 70 33 3b 20 20 20 20 20 20  = pOp->p3;      
1a665 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a666 20 20 20 20 2f 2a 20 50 33 20 2a 2f 0a 20 20 20      /* P3 */.   
1a667 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
1a668 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a  SQLITE_INTEGER;.
1a669 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20        pMem++;.  
1a66a 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c    }..    if( sql
1a66b 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
1a66c 70 4d 65 6d 2c 20 33 32 2c 20 30 29 20 29 7b 20  pMem, 32, 0) ){ 
1a66d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 34             /* P4
1a66e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
1a66f 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ( p->db->mallocF
1a670 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 72  ailed );.      r
1a671 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
1a672 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d  OR;.    }.    pM
1a673 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
1a674 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f  Dyn|MEM_Str|MEM_
1a675 54 65 72 6d 3b 0a 20 20 20 20 7a 20 3d 20 64 69  Term;.    z = di
1a676 73 70 6c 61 79 50 34 28 70 4f 70 2c 20 70 4d 65  splayP4(pOp, pMe
1a677 6d 2d 3e 7a 2c 20 33 32 29 3b 0a 20 20 20 20 69  m->z, 32);.    i
1a678 66 28 20 7a 21 3d 70 4d 65 6d 2d 3e 7a 20 29 7b  f( z!=pMem->z ){
1a679 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1a67a 62 65 4d 65 6d 53 65 74 53 74 72 28 70 4d 65 6d  beMemSetStr(pMem
1a67b 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  , z, -1, SQLITE_
1a67c 55 54 46 38 2c 20 30 29 3b 0a 20 20 20 20 7d 65  UTF8, 0);.    }e
1a67d 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72  lse{.      asser
1a67e 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b  t( pMem->z!=0 );
1a67f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d  .      pMem->n =
1a680 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
1a681 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20  (pMem->z);.     
1a682 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c   pMem->enc = SQL
1a683 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 7d 0a  ITE_UTF8;.    }.
1a684 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d      pMem->type =
1a685 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20   SQLITE_TEXT;.  
1a686 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 69    pMem++;..    i
1a687 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31  f( p->explain==1
1a688 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71   ){.      if( sq
1a689 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
1a68a 28 70 4d 65 6d 2c 20 34 2c 20 30 29 20 29 7b 0a  (pMem, 4, 0) ){.
1a68b 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1a68c 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  p->db->mallocFai
1a68d 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20 72  led );.        r
1a68e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
1a68f 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  OR;.      }.    
1a690 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
1a691 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c  MEM_Dyn|MEM_Str|
1a692 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20  MEM_Term;.      
1a693 70 4d 65 6d 2d 3e 6e 20 3d 20 32 3b 0a 20 20 20  pMem->n = 2;.   
1a694 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1a695 6e 74 66 28 33 2c 20 70 4d 65 6d 2d 3e 7a 2c 20  ntf(3, pMem->z, 
1a696 22 25 2e 32 78 22 2c 20 70 4f 70 2d 3e 70 35 29  "%.2x", pOp->p5)
1a697 3b 20 20 20 2f 2a 20 50 35 20 2a 2f 0a 20 20 20  ;   /* P5 */.   
1a698 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
1a699 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20  SQLITE_TEXT;.   
1a69a 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53     pMem->enc = S
1a69b 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20  QLITE_UTF8;.    
1a69c 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 0a 23 69 66    pMem++;.  .#if
1a69d 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
1a69e 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  .      if( pOp->
1a69f 7a 43 6f 6d 6d 65 6e 74 20 29 7b 0a 20 20 20 20  zComment ){.    
1a6a0 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
1a6a1 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65  = MEM_Str|MEM_Te
1a6a2 72 6d 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d  rm;.        pMem
1a6a3 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 7a 43 6f 6d 6d  ->z = pOp->zComm
1a6a4 65 6e 74 3b 0a 20 20 20 20 20 20 20 20 70 4d 65  ent;.        pMe
1a6a5 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  m->n = sqlite3St
1a6a6 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b  rlen30(pMem->z);
1a6a7 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65  .        pMem->e
1a6a8 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38  nc = SQLITE_UTF8
1a6a9 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  ;.        pMem->
1a6aa 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45  type = SQLITE_TE
1a6ab 58 54 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a  XT;.      }else.
1a6ac 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20  #endif.      {. 
1a6ad 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61         pMem->fla
1a6ae 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 20 20  gs = MEM_Null;  
1a6af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a6b0 20 20 20 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20       /* Comment 
1a6b1 2a 2f 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d  */.        pMem-
1a6b2 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e  >type = SQLITE_N
1a6b3 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ULL;.      }.   
1a6b4 20 7d 0a 0a 20 20 20 20 70 2d 3e 6e 52 65 73 43   }..    p->nResC
1a6b5 6f 6c 75 6d 6e 20 3d 20 38 20 2d 20 35 2a 28 70  olumn = 8 - 5*(p
1a6b6 2d 3e 65 78 70 6c 61 69 6e 2d 31 29 3b 0a 20 20  ->explain-1);.  
1a6b7 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45    p->rc = SQLITE
1a6b8 5f 4f 4b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  _OK;.    rc = SQ
1a6b9 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 7d 0a 20 20  LITE_ROW;.  }.  
1a6ba 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e  return rc;.}.#en
1a6bb 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1a6bc 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 0a 23  IT_EXPLAIN */..#
1a6bd 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
1a6be 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74  UG./*.** Print t
1a6bf 68 65 20 53 51 4c 20 74 68 61 74 20 77 61 73 20  he SQL that was 
1a6c0 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65  used to generate
1a6c1 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e   a VDBE program.
1a6c2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1a6c3 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1a6c4 64 62 65 50 72 69 6e 74 53 71 6c 28 56 64 62 65  dbePrintSql(Vdbe
1a6c5 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20   *p){.  int nOp 
1a6c6 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65  = p->nOp;.  Vdbe
1a6c7 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 66 28 20 6e  Op *pOp;.  if( n
1a6c8 4f 70 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20  Op<1 ) return;. 
1a6c9 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 30   pOp = &p->aOp[0
1a6ca 5d 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70  ];.  if( pOp->op
1a6cb 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26  code==OP_Trace &
1a6cc 26 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29  & pOp->p4.z!=0 )
1a6cd 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
1a6ce 20 2a 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b   *z = pOp->p4.z;
1a6cf 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69  .    while( sqli
1a6d0 74 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20 29  te3Isspace(*z) )
1a6d1 20 7a 2b 2b 3b 0a 20 20 20 20 70 72 69 6e 74 66   z++;.    printf
1a6d2 28 22 53 51 4c 3a 20 5b 25 73 5d 5c 6e 22 2c 20  ("SQL: [%s]\n", 
1a6d3 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66  z);.  }.}.#endif
1a6d4 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
1a6d5 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
1a6d6 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c  ) && defined(SQL
1a6d7 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41  ITE_ENABLE_IOTRA
1a6d8 43 45 29 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20  CE)./*.** Print 
1a6d9 61 6e 20 49 4f 54 52 41 43 45 20 6d 65 73 73 61  an IOTRACE messa
1a6da 67 65 20 73 68 6f 77 69 6e 67 20 53 51 4c 20 63  ge showing SQL c
1a6db 6f 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  ontent..*/.SQLIT
1a6dc 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1a6dd 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 63  qlite3VdbeIOTrac
1a6de 65 53 71 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20  eSql(Vdbe *p){. 
1a6df 20 69 6e 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f   int nOp = p->nO
1a6e0 70 3b 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f 70  p;.  VdbeOp *pOp
1a6e1 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49  ;.  if( sqlite3I
1a6e2 6f 54 72 61 63 65 3d 3d 30 20 29 20 72 65 74 75  oTrace==0 ) retu
1a6e3 72 6e 3b 0a 20 20 69 66 28 20 6e 4f 70 3c 31 20  rn;.  if( nOp<1 
1a6e4 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f 70 20  ) return;.  pOp 
1a6e5 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20  = &p->aOp[0];.  
1a6e6 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
1a6e7 3d 4f 50 5f 54 72 61 63 65 20 26 26 20 70 4f 70  =OP_Trace && pOp
1a6e8 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20 20 20  ->p4.z!=0 ){.   
1a6e9 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 20 63   int i, j;.    c
1a6ea 68 61 72 20 7a 5b 31 30 30 30 5d 3b 0a 20 20 20  har z[1000];.   
1a6eb 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1a6ec 66 28 73 69 7a 65 6f 66 28 7a 29 2c 20 7a 2c 20  f(sizeof(z), z, 
1a6ed 22 25 73 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29  "%s", pOp->p4.z)
1a6ee 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 73  ;.    for(i=0; s
1a6ef 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 7a 5b  qlite3Isspace(z[
1a6f0 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20  i]); i++){}.    
1a6f1 66 6f 72 28 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69  for(j=0; z[i]; i
1a6f2 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  ++){.      if( s
1a6f3 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 7a 5b  qlite3Isspace(z[
1a6f4 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69  i]) ){.        i
1a6f5 66 28 20 7a 5b 69 2d 31 5d 21 3d 27 20 27 20 29  f( z[i-1]!=' ' )
1a6f6 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b  {.          z[j+
1a6f7 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20  +] = ' ';.      
1a6f8 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
1a6f9 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20  .        z[j++] 
1a6fa 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 20 20 7d 0a  = z[i];.      }.
1a6fb 20 20 20 20 7d 0a 20 20 20 20 7a 5b 6a 5d 20 3d      }.    z[j] =
1a6fc 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49   0;.    sqlite3I
1a6fd 6f 54 72 61 63 65 28 22 53 51 4c 20 25 73 5c 6e  oTrace("SQL %s\n
1a6fe 22 2c 20 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  ", z);.  }.}.#en
1a6ff 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f  dif /* !SQLITE_O
1a700 4d 49 54 5f 54 52 41 43 45 20 26 26 20 53 51 4c  MIT_TRACE && SQL
1a701 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41  ITE_ENABLE_IOTRA
1a702 43 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  CE */../*.** All
1a703 6f 63 61 74 65 20 73 70 61 63 65 20 66 72 6f 6d  ocate space from
1a704 20 61 20 66 69 78 65 64 20 73 69 7a 65 20 62 75   a fixed size bu
1a705 66 66 65 72 2e 20 20 4d 61 6b 65 20 2a 70 70 20  ffer.  Make *pp 
1a706 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20  point to the.** 
1a707 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e  allocated space.
1a708 20 20 28 4e 6f 74 65 3a 20 20 70 70 20 69 73 20    (Note:  pp is 
1a709 61 20 63 68 61 72 2a 20 72 61 74 68 65 72 20 74  a char* rather t
1a70a 68 61 6e 20 61 20 76 6f 69 64 2a 2a 20 74 6f 0a  han a void** to.
1a70b 2a 2a 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74  ** work around t
1a70c 68 65 20 70 6f 69 6e 74 65 72 20 61 6c 69 61 73  he pointer alias
1a70d 69 6e 67 20 72 75 6c 65 73 20 6f 66 20 43 2e 29  ing rules of C.)
1a70e 20 20 2a 70 70 20 73 68 6f 75 6c 64 20 69 6e 69    *pp should ini
1a70f 74 69 61 6c 6c 79 0a 2a 2a 20 62 65 20 7a 65 72  tially.** be zer
1a710 6f 2e 20 20 49 66 20 2a 70 70 20 69 73 20 6e 6f  o.  If *pp is no
1a711 74 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61  t zero, that mea
1a712 6e 73 20 74 68 61 74 20 74 68 65 20 73 70 61 63  ns that the spac
1a713 65 20 68 61 73 20 61 6c 72 65 61 64 79 0a 2a 2a  e has already.**
1a714 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20   been allocated 
1a715 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65  and this routine
1a716 20 69 73 20 61 20 6e 6f 6f 70 2e 0a 2a 2a 0a 2a   is a noop..**.*
1a717 2a 20 6e 42 79 74 65 20 69 73 20 74 68 65 20 6e  * nByte is the n
1a718 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
1a719 66 20 73 70 61 63 65 20 6e 65 65 64 65 64 2e 0a  f space needed..
1a71a 2a 2a 0a 2a 2a 20 2a 70 70 46 72 6f 6d 20 70 6f  **.** *ppFrom po
1a71b 69 6e 74 20 74 6f 20 61 76 61 69 6c 61 62 6c 65  int to available
1a71c 20 73 70 61 63 65 20 61 6e 64 20 70 45 6e 64 20   space and pEnd 
1a71d 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 65 6e  points to the en
1a71e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 61 76 61 69  d of the.** avai
1a71f 6c 61 62 6c 65 20 73 70 61 63 65 2e 0a 2a 2a 0a  lable space..**.
1a720 2a 2a 20 2a 70 6e 42 79 74 65 20 69 73 20 61 20  ** *pnByte is a 
1a721 63 6f 75 6e 74 65 72 20 6f 66 20 74 68 65 20 6e  counter of the n
1a722 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
1a723 66 20 73 70 61 63 65 20 74 68 61 74 20 68 61 76  f space that hav
1a724 65 20 66 61 69 6c 65 64 0a 2a 2a 20 74 6f 20 61  e failed.** to a
1a725 6c 6c 6f 63 61 74 65 2e 20 20 49 66 20 74 68 65  llocate.  If the
1a726 72 65 20 69 73 20 69 6e 73 75 66 66 69 63 69 65  re is insufficie
1a727 6e 74 20 73 70 61 63 65 20 69 6e 20 2a 70 70 46  nt space in *ppF
1a728 72 6f 6d 20 74 6f 20 73 61 74 69 73 66 79 20 74  rom to satisfy t
1a729 68 65 0a 2a 2a 20 72 65 71 75 65 73 74 2c 20 74  he.** request, t
1a72a 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 2a 70  hen increment *p
1a72b 6e 42 79 74 65 20 62 79 20 74 68 65 20 61 6d 6f  nByte by the amo
1a72c 75 6e 74 20 6f 66 20 74 68 65 20 72 65 71 75 65  unt of the reque
1a72d 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  st..*/.static vo
1a72e 69 64 20 61 6c 6c 6f 63 53 70 61 63 65 28 0a 20  id allocSpace(. 
1a72f 20 63 68 61 72 20 2a 70 70 2c 20 20 20 20 20 20   char *pp,      
1a730 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a        /* IN/OUT:
1a731 20 53 65 74 20 2a 70 70 20 74 6f 20 70 6f 69 6e   Set *pp to poin
1a732 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 64 20 62  t to allocated b
1a733 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  uffer */.  int n
1a734 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  Byte,           
1a735 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
1a736 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a  es to allocate *
1a737 2f 0a 20 20 75 38 20 2a 2a 70 70 46 72 6f 6d 2c  /.  u8 **ppFrom,
1a738 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f           /* IN/O
1a739 55 54 3a 20 41 6c 6c 6f 63 61 74 65 20 66 72 6f  UT: Allocate fro
1a73a 6d 20 2a 70 70 46 72 6f 6d 20 2a 2f 0a 20 20 75  m *ppFrom */.  u
1a73b 38 20 2a 70 45 6e 64 2c 20 20 20 20 20 20 20 20  8 *pEnd,        
1a73c 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
1a73d 6f 20 31 20 62 79 74 65 20 70 61 73 74 20 74 68  o 1 byte past th
1a73e 65 20 65 6e 64 20 6f 66 20 2a 70 70 46 72 6f 6d  e end of *ppFrom
1a73f 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74   buffer */.  int
1a740 20 2a 70 6e 42 79 74 65 20 20 20 20 20 20 20 20   *pnByte        
1a741 20 20 2f 2a 20 49 66 20 61 6c 6c 6f 63 61 74 69    /* If allocati
1a742 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 6d 61 64  on cannot be mad
1a743 65 2c 20 69 6e 63 72 65 6d 65 6e 74 20 2a 70 6e  e, increment *pn
1a744 42 79 74 65 20 2a 2f 0a 29 7b 0a 20 20 61 73 73  Byte */.){.  ass
1a745 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f  ert( EIGHT_BYTE_
1a746 41 4c 49 47 4e 4d 45 4e 54 28 2a 70 70 46 72 6f  ALIGNMENT(*ppFro
1a747 6d 29 20 29 3b 0a 20 20 69 66 28 20 28 2a 28 76  m) );.  if( (*(v
1a748 6f 69 64 2a 2a 29 70 70 29 3d 3d 30 20 29 7b 0a  oid**)pp)==0 ){.
1a749 20 20 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e      nByte = ROUN
1a74a 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 69  D8(nByte);.    i
1a74b 66 28 20 26 28 2a 70 70 46 72 6f 6d 29 5b 6e 42  f( &(*ppFrom)[nB
1a74c 79 74 65 5d 20 3c 3d 20 70 45 6e 64 20 29 7b 0a  yte] <= pEnd ){.
1a74d 20 20 20 20 20 20 2a 28 76 6f 69 64 2a 2a 29 70        *(void**)p
1a74e 70 20 3d 20 28 76 6f 69 64 20 2a 29 2a 70 70 46  p = (void *)*ppF
1a74f 72 6f 6d 3b 0a 20 20 20 20 20 20 2a 70 70 46 72  rom;.      *ppFr
1a750 6f 6d 20 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20  om += nByte;.   
1a751 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 70   }else{.      *p
1a752 6e 42 79 74 65 20 2b 3d 20 6e 42 79 74 65 3b 0a  nByte += nByte;.
1a753 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
1a754 2a 2a 20 50 72 65 70 61 72 65 20 61 20 76 69 72  ** Prepare a vir
1a755 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 6f 72  tual machine for
1a756 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 54 68 69   execution.  Thi
1a757 73 20 69 6e 76 6f 6c 76 65 73 20 74 68 69 6e 67  s involves thing
1a758 73 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 6c 6c  s such.** as all
1a759 6f 63 61 74 69 6e 67 20 73 74 61 63 6b 20 73 70  ocating stack sp
1a75a 61 63 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  ace and initiali
1a75b 7a 69 6e 67 20 74 68 65 20 70 72 6f 67 72 61 6d  zing the program
1a75c 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 20 41 66 74   counter..** Aft
1a75d 65 72 20 74 68 65 20 56 44 42 45 20 68 61 73 20  er the VDBE has 
1a75e 62 65 20 70 72 65 70 70 65 64 2c 20 69 74 20 63  be prepped, it c
1a75f 61 6e 20 62 65 20 65 78 65 63 75 74 65 64 20 62  an be executed b
1a760 79 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a  y one or more.**
1a761 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65   calls to sqlite
1a762 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 0a 2a  3VdbeExec().  .*
1a763 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65  *.** This is the
1a764 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f 76   only way to mov
1a765 65 20 61 20 56 44 42 45 20 66 72 6f 6d 20 56 44  e a VDBE from VD
1a766 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 74 6f  BE_MAGIC_INIT to
1a767 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f 52  .** VDBE_MAGIC_R
1a768 55 4e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  UN..**.** This f
1a769 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63  unction may be c
1a76a 61 6c 6c 65 64 20 6d 6f 72 65 20 74 68 61 6e 20  alled more than 
1a76b 6f 6e 63 65 20 6f 6e 20 61 20 73 69 6e 67 6c 65  once on a single
1a76c 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
1a76d 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 63  ..** The first c
1a76e 61 6c 6c 20 69 73 20 6d 61 64 65 20 77 68 69 6c  all is made whil
1a76f 65 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20  e compiling the 
1a770 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 53  SQL statement. S
1a771 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c  ubsequent.** cal
1a772 6c 73 20 61 72 65 20 6d 61 64 65 20 61 73 20 70  ls are made as p
1a773 61 72 74 20 6f 66 20 74 68 65 20 70 72 6f 63 65  art of the proce
1a774 73 73 20 6f 66 20 72 65 73 65 74 74 69 6e 67 20  ss of resetting 
1a775 61 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62  a statement to b
1a776 65 0a 2a 2a 20 72 65 2d 65 78 65 63 75 74 65 64  e.** re-executed
1a777 20 28 66 72 6f 6d 20 61 20 63 61 6c 6c 20 74 6f   (from a call to
1a778 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29   sqlite3_reset()
1a779 29 2e 20 54 68 65 20 6e 56 61 72 2c 20 6e 4d 65  ). The nVar, nMe
1a77a 6d 2c 20 6e 43 75 72 73 6f 72 20 0a 2a 2a 20 61  m, nCursor .** a
1a77b 6e 64 20 69 73 45 78 70 6c 61 69 6e 20 70 61 72  nd isExplain par
1a77c 61 6d 65 74 65 72 73 20 61 72 65 20 6f 6e 6c 79  ameters are only
1a77d 20 70 61 73 73 65 64 20 63 6f 72 72 65 63 74 20   passed correct 
1a77e 76 61 6c 75 65 73 20 74 68 65 20 66 69 72 73 74  values the first
1a77f 20 74 69 6d 65 0a 2a 2a 20 74 68 65 20 66 75 6e   time.** the fun
1a780 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e  ction is called.
1a781 20 4f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63   On subsequent c
1a782 61 6c 6c 73 2c 20 66 72 6f 6d 20 73 71 6c 69 74  alls, from sqlit
1a783 65 33 5f 72 65 73 65 74 28 29 2c 20 6e 56 61 72  e3_reset(), nVar
1a784 0a 2a 2a 20 69 73 20 70 61 73 73 65 64 20 2d 31  .** is passed -1
1a785 20 61 6e 64 20 6e 4d 65 6d 2c 20 6e 43 75 72 73   and nMem, nCurs
1a786 6f 72 20 61 6e 64 20 69 73 45 78 70 6c 61 69 6e  or and isExplain
1a787 20 61 72 65 20 61 6c 6c 20 70 61 73 73 65 64 20   are all passed 
1a788 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  zero..*/.SQLITE_
1a789 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1a78a 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64  ite3VdbeMakeRead
1a78b 79 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20  y(.  Vdbe *p,   
1a78c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a78d 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20      /* The VDBE 
1a78e 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 2c 20 20  */.  int nVar,  
1a78f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a790 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1a791 20 27 3f 27 20 73 65 65 20 69 6e 20 74 68 65 20   '?' see in the 
1a792 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  SQL statement */
1a793 0a 20 20 69 6e 74 20 6e 4d 65 6d 2c 20 20 20 20  .  int nMem,    
1a794 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a795 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d    /* Number of m
1a796 65 6d 6f 72 79 20 63 65 6c 6c 73 20 74 6f 20 61  emory cells to a
1a797 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e 74  llocate */.  int
1a798 20 6e 43 75 72 73 6f 72 2c 20 20 20 20 20 20 20   nCursor,       
1a799 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1a79a 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73  umber of cursors
1a79b 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a   to allocate */.
1a79c 20 20 69 6e 74 20 6e 41 72 67 2c 20 20 20 20 20    int nArg,     
1a79d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a79e 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62   /* Maximum numb
1a79f 65 72 20 6f 66 20 61 72 67 73 20 69 6e 20 53 75  er of args in Su
1a7a0 62 50 72 6f 67 72 61 6d 73 20 2a 2f 0a 20 20 69  bPrograms */.  i
1a7a1 6e 74 20 69 73 45 78 70 6c 61 69 6e 2c 20 20 20  nt isExplain,   
1a7a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a7a3 20 54 72 75 65 20 69 66 20 74 68 65 20 45 58 50   True if the EXP
1a7a4 4c 41 49 4e 20 6b 65 79 77 6f 72 64 73 20 69 73  LAIN keywords is
1a7a5 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e   present */.  in
1a7a6 74 20 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61  t usesStmtJourna
1a7a7 6c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  l            /* 
1a7a8 54 72 75 65 20 74 6f 20 73 65 74 20 56 64 62 65  True to set Vdbe
1a7a9 2e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c  .usesStmtJournal
1a7aa 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a   */.){.  int n;.
1a7ab 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1a7ac 70 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74  p->db;..  assert
1a7ad 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ( p!=0 );.  asse
1a7ae 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44  rt( p->magic==VD
1a7af 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b  BE_MAGIC_INIT );
1a7b0 0a 0a 20 20 2f 2a 20 54 68 65 72 65 20 73 68 6f  ..  /* There sho
1a7b1 75 6c 64 20 62 65 20 61 74 20 6c 65 61 73 74 20  uld be at least 
1a7b2 6f 6e 65 20 6f 70 63 6f 64 65 2e 0a 20 20 2a 2f  one opcode..  */
1a7b3 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f  .  assert( p->nO
1a7b4 70 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 74  p>0 );..  /* Set
1a7b5 20 74 68 65 20 6d 61 67 69 63 20 74 6f 20 56 44   the magic to VD
1a7b6 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 73 6f 6f  BE_MAGIC_RUN soo
1a7b7 6e 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20  ner rather than 
1a7b8 6c 61 74 65 72 2e 20 2a 2f 0a 20 20 70 2d 3e 6d  later. */.  p->m
1a7b9 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49  agic = VDBE_MAGI
1a7ba 43 5f 52 55 4e 3b 0a 0a 20 20 2f 2a 20 46 6f 72  C_RUN;..  /* For
1a7bb 20 65 61 63 68 20 63 75 72 73 6f 72 20 72 65 71   each cursor req
1a7bc 75 69 72 65 64 2c 20 61 6c 73 6f 20 61 6c 6c 6f  uired, also allo
1a7bd 63 61 74 65 20 61 20 6d 65 6d 6f 72 79 20 63 65  cate a memory ce
1a7be 6c 6c 2e 20 4d 65 6d 6f 72 79 0a 20 20 2a 2a 20  ll. Memory.  ** 
1a7bf 63 65 6c 6c 73 20 28 6e 4d 65 6d 2b 31 2d 6e 43  cells (nMem+1-nC
1a7c0 75 72 73 6f 72 29 2e 2e 6e 4d 65 6d 2c 20 69 6e  ursor)..nMem, in
1a7c1 63 6c 75 73 69 76 65 2c 20 77 69 6c 6c 20 6e 65  clusive, will ne
1a7c2 76 65 72 20 62 65 20 75 73 65 64 20 62 79 0a 20  ver be used by. 
1a7c3 20 2a 2a 20 74 68 65 20 76 64 62 65 20 70 72 6f   ** the vdbe pro
1a7c4 67 72 61 6d 2e 20 49 6e 73 74 65 61 64 20 74 68  gram. Instead th
1a7c5 65 79 20 61 72 65 20 75 73 65 64 20 74 6f 20 61  ey are used to a
1a7c6 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f  llocate space fo
1a7c7 72 0a 20 20 2a 2a 20 56 64 62 65 43 75 72 73 6f  r.  ** VdbeCurso
1a7c8 72 2f 42 74 43 75 72 73 6f 72 20 73 74 72 75 63  r/BtCursor struc
1a7c9 74 75 72 65 73 2e 20 54 68 65 20 62 6c 6f 62 20  tures. The blob 
1a7ca 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69  of memory associ
1a7cb 61 74 65 64 20 77 69 74 68 20 0a 20 20 2a 2a 20  ated with .  ** 
1a7cc 63 75 72 73 6f 72 20 30 20 69 73 20 73 74 6f 72  cursor 0 is stor
1a7cd 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c  ed in memory cel
1a7ce 6c 20 6e 4d 65 6d 2e 20 4d 65 6d 6f 72 79 20 63  l nMem. Memory c
1a7cf 65 6c 6c 20 28 6e 4d 65 6d 2d 31 29 0a 20 20 2a  ell (nMem-1).  *
1a7d0 2a 20 73 74 6f 72 65 73 20 74 68 65 20 62 6c 6f  * stores the blo
1a7d1 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f  b of memory asso
1a7d2 63 69 61 74 65 64 20 77 69 74 68 20 63 75 72 73  ciated with curs
1a7d3 6f 72 20 31 2c 20 65 74 63 2e 0a 20 20 2a 2a 0a  or 1, etc..  **.
1a7d4 20 20 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 61    ** See also: a
1a7d5 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 29 2e  llocateCursor().
1a7d6 0a 20 20 2a 2f 0a 20 20 6e 4d 65 6d 20 2b 3d 20  .  */.  nMem += 
1a7d7 6e 43 75 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 41  nCursor;..  /* A
1a7d8 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f  llocate space fo
1a7d9 72 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65  r memory registe
1a7da 72 73 2c 20 53 51 4c 20 76 61 72 69 61 62 6c 65  rs, SQL variable
1a7db 73 2c 20 56 44 42 45 20 63 75 72 73 6f 72 73 20  s, VDBE cursors 
1a7dc 61 6e 64 20 0a 20 20 2a 2a 20 61 6e 20 61 72 72  and .  ** an arr
1a7dd 61 79 20 74 6f 20 6d 61 72 73 68 61 6c 20 53 51  ay to marshal SQ
1a7de 4c 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d  L function argum
1a7df 65 6e 74 73 20 69 6e 2e 20 54 68 69 73 20 69 73  ents in. This is
1a7e0 20 6f 6e 6c 79 20 64 6f 6e 65 20 74 68 65 0a 20   only done the. 
1a7e1 20 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 20 74   ** first time t
1a7e2 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
1a7e3 63 61 6c 6c 65 64 20 66 6f 72 20 61 20 67 69 76  called for a giv
1a7e4 65 6e 20 56 44 42 45 2c 20 6e 6f 74 20 77 68 65  en VDBE, not whe
1a7e5 6e 20 69 74 20 69 73 0a 20 20 2a 2a 20 62 65 69  n it is.  ** bei
1a7e6 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 73  ng called from s
1a7e7 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20 74  qlite3_reset() t
1a7e8 6f 20 72 65 73 65 74 20 74 68 65 20 76 69 72 74  o reset the virt
1a7e9 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 20 20 2a  ual machine..  *
1a7ea 2f 0a 20 20 69 66 28 20 6e 56 61 72 3e 3d 30 20  /.  if( nVar>=0 
1a7eb 26 26 20 41 4c 57 41 59 53 28 64 62 2d 3e 6d 61  && ALWAYS(db->ma
1a7ec 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 29 20 29  llocFailed==0) )
1a7ed 7b 0a 20 20 20 20 75 38 20 2a 7a 43 73 72 20 3d  {.    u8 *zCsr =
1a7ee 20 28 75 38 20 2a 29 26 70 2d 3e 61 4f 70 5b 70   (u8 *)&p->aOp[p
1a7ef 2d 3e 6e 4f 70 5d 3b 0a 20 20 20 20 75 38 20 2a  ->nOp];.    u8 *
1a7f0 7a 45 6e 64 20 3d 20 28 75 38 20 2a 29 26 70 2d  zEnd = (u8 *)&p-
1a7f1 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 41 6c 6c 6f 63  >aOp[p->nOpAlloc
1a7f2 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65  ];.    int nByte
1a7f3 3b 0a 20 20 20 20 72 65 73 6f 6c 76 65 50 32 56  ;.    resolveP2V
1a7f4 61 6c 75 65 73 28 70 2c 20 26 6e 41 72 67 29 3b  alues(p, &nArg);
1a7f5 0a 20 20 20 20 70 2d 3e 75 73 65 73 53 74 6d 74  .    p->usesStmt
1a7f6 4a 6f 75 72 6e 61 6c 20 3d 20 28 75 38 29 75 73  Journal = (u8)us
1a7f7 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 3b 0a 20  esStmtJournal;. 
1a7f8 20 20 20 69 66 28 20 69 73 45 78 70 6c 61 69 6e     if( isExplain
1a7f9 20 26 26 20 6e 4d 65 6d 3c 31 30 20 29 7b 0a 20   && nMem<10 ){. 
1a7fa 20 20 20 20 20 6e 4d 65 6d 20 3d 20 31 30 3b 0a       nMem = 10;.
1a7fb 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74      }.    memset
1a7fc 28 7a 43 73 72 2c 20 30 2c 20 7a 45 6e 64 2d 7a  (zCsr, 0, zEnd-z
1a7fd 43 73 72 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b  Csr);.    zCsr +
1a7fe 3d 20 28 7a 43 73 72 20 2d 20 28 75 38 2a 29 30  = (zCsr - (u8*)0
1a7ff 29 26 37 3b 0a 20 20 20 20 61 73 73 65 72 74 28  )&7;.    assert(
1a800 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47   EIGHT_BYTE_ALIG
1a801 4e 4d 45 4e 54 28 7a 43 73 72 29 20 29 3b 0a 0a  NMENT(zCsr) );..
1a802 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 6e      do {.      n
1a803 42 79 74 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  Byte = 0;.      
1a804 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61 72  allocSpace((char
1a805 2a 29 26 70 2d 3e 61 4d 65 6d 2c 20 6e 4d 65 6d  *)&p->aMem, nMem
1a806 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 2c 20 26 7a  *sizeof(Mem), &z
1a807 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74  Csr, zEnd, &nByt
1a808 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53  e);.      allocS
1a809 70 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e  pace((char*)&p->
1a80a 61 56 61 72 2c 20 6e 56 61 72 2a 73 69 7a 65 6f  aVar, nVar*sizeo
1a80b 66 28 4d 65 6d 29 2c 20 26 7a 43 73 72 2c 20 7a  f(Mem), &zCsr, z
1a80c 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20  End, &nByte);.  
1a80d 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 28      allocSpace((
1a80e 63 68 61 72 2a 29 26 70 2d 3e 61 70 41 72 67 2c  char*)&p->apArg,
1a80f 20 6e 41 72 67 2a 73 69 7a 65 6f 66 28 4d 65 6d   nArg*sizeof(Mem
1a810 2a 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c  *), &zCsr, zEnd,
1a811 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20   &nByte);.      
1a812 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61 72  allocSpace((char
1a813 2a 29 26 70 2d 3e 61 7a 56 61 72 2c 20 6e 56 61  *)&p->azVar, nVa
1a814 72 2a 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2c  r*sizeof(char*),
1a815 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e   &zCsr, zEnd, &n
1a816 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c  Byte);.      all
1a817 6f 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 26  ocSpace((char*)&
1a818 70 2d 3e 61 70 43 73 72 2c 20 0a 20 20 20 20 20  p->apCsr, .     
1a819 20 20 20 20 20 20 20 20 20 20 20 20 6e 43 75 72              nCur
1a81a 73 6f 72 2a 73 69 7a 65 6f 66 28 56 64 62 65 43  sor*sizeof(VdbeC
1a81b 75 72 73 6f 72 2a 29 2c 20 26 7a 43 73 72 2c 20  ursor*), &zCsr, 
1a81c 7a 45 6e 64 2c 20 26 6e 42 79 74 65 0a 20 20 20  zEnd, &nByte.   
1a81d 20 20 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20     );.      if( 
1a81e 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 20  nByte ){.       
1a81f 20 70 2d 3e 70 46 72 65 65 20 3d 20 73 71 6c 69   p->pFree = sqli
1a820 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
1a821 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20  db, nByte);.    
1a822 20 20 7d 0a 20 20 20 20 20 20 7a 43 73 72 20 3d    }.      zCsr =
1a823 20 70 2d 3e 70 46 72 65 65 3b 0a 20 20 20 20 20   p->pFree;.     
1a824 20 7a 45 6e 64 20 3d 20 26 7a 43 73 72 5b 6e 42   zEnd = &zCsr[nB
1a825 79 74 65 5d 3b 0a 20 20 20 20 7d 77 68 69 6c 65  yte];.    }while
1a826 28 20 6e 42 79 74 65 20 26 26 20 21 64 62 2d 3e  ( nByte && !db->
1a827 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
1a828 0a 20 20 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20  .    p->nCursor 
1a829 3d 20 28 75 31 36 29 6e 43 75 72 73 6f 72 3b 0a  = (u16)nCursor;.
1a82a 20 20 20 20 69 66 28 20 70 2d 3e 61 56 61 72 20      if( p->aVar 
1a82b 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 56 61 72  ){.      p->nVar
1a82c 20 3d 20 28 75 31 36 29 6e 56 61 72 3b 0a 20 20   = (u16)nVar;.  
1a82d 20 20 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e      for(n=0; n<n
1a82e 56 61 72 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 20  Var; n++){.     
1a82f 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 66 6c     p->aVar[n].fl
1a830 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a  ags = MEM_Null;.
1a831 20 20 20 20 20 20 20 20 70 2d 3e 61 56 61 72 5b          p->aVar[
1a832 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20  n].db = db;.    
1a833 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
1a834 28 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20 20  ( p->aMem ){.   
1a835 20 20 20 70 2d 3e 61 4d 65 6d 2d 2d 3b 20 20 20     p->aMem--;   
1a836 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a837 20 20 20 2f 2a 20 61 4d 65 6d 5b 5d 20 67 6f 65     /* aMem[] goe
1a838 73 20 66 72 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 2a  s from 1..nMem *
1a839 2f 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 65 6d 20  /.      p->nMem 
1a83a 3d 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20  = nMem;         
1a83b 20 20 20 20 20 20 20 20 2f 2a 20 20 20 20 20 20          /*      
1a83c 20 6e 6f 74 20 66 72 6f 6d 20 30 2e 2e 6e 4d 65   not from 0..nMe
1a83d 6d 2d 31 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72  m-1 */.      for
1a83e 28 6e 3d 31 3b 20 6e 3c 3d 6e 4d 65 6d 3b 20 6e  (n=1; n<=nMem; n
1a83f 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  ++){.        p->
1a840 61 4d 65 6d 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20  aMem[n].flags = 
1a841 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20  MEM_Null;.      
1a842 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 20    p->aMem[n].db 
1a843 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20  = db;.      }.  
1a844 20 20 7d 0a 20 20 7d 0a 23 69 66 64 65 66 20 53    }.  }.#ifdef S
1a845 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 66 6f  QLITE_DEBUG.  fo
1a846 72 28 6e 3d 31 3b 20 6e 3c 70 2d 3e 6e 4d 65 6d  r(n=1; n<p->nMem
1a847 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65  ; n++){.    asse
1a848 72 74 28 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64  rt( p->aMem[n].d
1a849 62 3d 3d 64 62 20 29 3b 0a 20 20 7d 0a 23 65 6e  b==db );.  }.#en
1a84a 64 69 66 0a 0a 20 20 70 2d 3e 70 63 20 3d 20 2d  dif..  p->pc = -
1a84b 31 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c  1;.  p->rc = SQL
1a84c 49 54 45 5f 4f 4b 3b 0a 20 20 70 2d 3e 65 72 72  ITE_OK;.  p->err
1a84d 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62  orAction = OE_Ab
1a84e 6f 72 74 3b 0a 20 20 70 2d 3e 65 78 70 6c 61 69  ort;.  p->explai
1a84f 6e 20 7c 3d 20 69 73 45 78 70 6c 61 69 6e 3b 0a  n |= isExplain;.
1a850 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42    p->magic = VDB
1a851 45 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a 20 20 70  E_MAGIC_RUN;.  p
1a852 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20  ->nChange = 0;. 
1a853 20 70 2d 3e 63 61 63 68 65 43 74 72 20 3d 20 31   p->cacheCtr = 1
1a854 3b 0a 20 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46  ;.  p->minWriteF
1a855 69 6c 65 46 6f 72 6d 61 74 20 3d 20 32 35 35 3b  ileFormat = 255;
1a856 0a 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74  .  p->iStatement
1a857 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 56 44 42   = 0;.#ifdef VDB
1a858 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 20  E_PROFILE.  {.  
1a859 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72    int i;.    for
1a85a 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20  (i=0; i<p->nOp; 
1a85b 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61  i++){.      p->a
1a85c 4f 70 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a 20  Op[i].cnt = 0;. 
1a85d 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63       p->aOp[i].c
1a85e 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20 20 20 7d  ycles = 0;.    }
1a85f 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  .  }.#endif.}../
1a860 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 56 44 42  *.** Close a VDB
1a861 45 20 63 75 72 73 6f 72 20 61 6e 64 20 72 65 6c  E cursor and rel
1a862 65 61 73 65 20 61 6c 6c 20 74 68 65 20 72 65 73  ease all the res
1a863 6f 75 72 63 65 73 20 74 68 61 74 20 63 75 72 73  ources that curs
1a864 6f 72 20 0a 2a 2a 20 68 61 70 70 65 6e 73 20 74  or .** happens t
1a865 6f 20 68 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54  o hold..*/.SQLIT
1a866 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1a867 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75  qlite3VdbeFreeCu
1a868 72 73 6f 72 28 56 64 62 65 20 2a 70 2c 20 56 64  rsor(Vdbe *p, Vd
1a869 62 65 43 75 72 73 6f 72 20 2a 70 43 78 29 7b 0a  beCursor *pCx){.
1a86a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29 7b 0a    if( pCx==0 ){.
1a86b 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
1a86c 20 20 69 66 28 20 70 43 78 2d 3e 70 42 74 20 29    if( pCx->pBt )
1a86d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  {.    sqlite3Btr
1a86e 65 65 43 6c 6f 73 65 28 70 43 78 2d 3e 70 42 74  eeClose(pCx->pBt
1a86f 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 43  );.    /* The pC
1a870 78 2d 3e 70 43 75 72 73 6f 72 20 77 69 6c 6c 20  x->pCursor will 
1a871 62 65 20 63 6c 6f 73 65 20 61 75 74 6f 6d 61 74  be close automat
1a872 69 63 61 6c 6c 79 2c 20 69 66 20 69 74 20 65 78  ically, if it ex
1a873 69 73 74 73 2c 20 62 79 0a 20 20 20 20 2a 2a 20  ists, by.    ** 
1a874 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 2e 20  the call above. 
1a875 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  */.  }else if( p
1a876 43 78 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20  Cx->pCursor ){. 
1a877 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
1a878 6c 6f 73 65 43 75 72 73 6f 72 28 70 43 78 2d 3e  loseCursor(pCx->
1a879 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 23 69  pCursor);.  }.#i
1a87a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1a87b 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
1a87c 20 69 66 28 20 70 43 78 2d 3e 70 56 74 61 62 43   if( pCx->pVtabC
1a87d 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71 6c  ursor ){.    sql
1a87e 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
1a87f 20 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20   *pVtabCursor = 
1a880 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f 72  pCx->pVtabCursor
1a881 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69  ;.    const sqli
1a882 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
1a883 75 6c 65 20 3d 20 70 43 78 2d 3e 70 4d 6f 64 75  ule = pCx->pModu
1a884 6c 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61  le;.    p->inVta
1a885 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20  bMethod = 1;.   
1a886 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
1a887 66 65 74 79 4f 66 66 28 70 2d 3e 64 62 29 3b 0a  fetyOff(p->db);.
1a888 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c      pModule->xCl
1a889 6f 73 65 28 70 56 74 61 62 43 75 72 73 6f 72 29  ose(pVtabCursor)
1a88a 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69  ;.    (void)sqli
1a88b 74 65 33 53 61 66 65 74 79 4f 6e 28 70 2d 3e 64  te3SafetyOn(p->d
1a88c 62 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61  b);.    p->inVta
1a88d 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 7d  bMethod = 0;.  }
1a88e 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
1a88f 20 43 6f 70 79 20 74 68 65 20 76 61 6c 75 65 73   Copy the values
1a890 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 56   stored in the V
1a891 64 62 65 46 72 61 6d 65 20 73 74 72 75 63 74 75  dbeFrame structu
1a892 72 65 20 74 6f 20 69 74 73 20 56 64 62 65 2e 20  re to its Vdbe. 
1a893 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 2c  This.** is used,
1a894 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68   for example, wh
1a895 65 6e 20 61 20 74 72 69 67 67 65 72 20 73 75 62  en a trigger sub
1a896 2d 70 72 6f 67 72 61 6d 20 69 73 20 68 61 6c 74  -program is halt
1a897 65 64 20 74 6f 20 72 65 73 74 6f 72 65 0a 2a 2a  ed to restore.**
1a898 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20   control to the 
1a899 6d 61 69 6e 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f  main program..*/
1a89a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a89b 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 46  int sqlite3VdbeF
1a89c 72 61 6d 65 52 65 73 74 6f 72 65 28 56 64 62 65  rameRestore(Vdbe
1a89d 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 29 7b 0a  Frame *pFrame){.
1a89e 20 20 56 64 62 65 20 2a 76 20 3d 20 70 46 72 61    Vdbe *v = pFra
1a89f 6d 65 2d 3e 76 3b 0a 20 20 76 2d 3e 61 4f 70 20  me->v;.  v->aOp 
1a8a0 3d 20 70 46 72 61 6d 65 2d 3e 61 4f 70 3b 0a 20  = pFrame->aOp;. 
1a8a1 20 76 2d 3e 6e 4f 70 20 3d 20 70 46 72 61 6d 65   v->nOp = pFrame
1a8a2 2d 3e 6e 4f 70 3b 0a 20 20 76 2d 3e 61 4d 65 6d  ->nOp;.  v->aMem
1a8a3 20 3d 20 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 3b   = pFrame->aMem;
1a8a4 0a 20 20 76 2d 3e 6e 4d 65 6d 20 3d 20 70 46 72  .  v->nMem = pFr
1a8a5 61 6d 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 76 2d 3e  ame->nMem;.  v->
1a8a6 61 70 43 73 72 20 3d 20 70 46 72 61 6d 65 2d 3e  apCsr = pFrame->
1a8a7 61 70 43 73 72 3b 0a 20 20 76 2d 3e 6e 43 75 72  apCsr;.  v->nCur
1a8a8 73 6f 72 20 3d 20 70 46 72 61 6d 65 2d 3e 6e 43  sor = pFrame->nC
1a8a9 75 72 73 6f 72 3b 0a 20 20 76 2d 3e 64 62 2d 3e  ursor;.  v->db->
1a8aa 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 46 72 61  lastRowid = pFra
1a8ab 6d 65 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20  me->lastRowid;. 
1a8ac 20 76 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70 46   v->nChange = pF
1a8ad 72 61 6d 65 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20  rame->nChange;. 
1a8ae 20 72 65 74 75 72 6e 20 70 46 72 61 6d 65 2d 3e   return pFrame->
1a8af 70 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f  pc;.}../*.** Clo
1a8b0 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a  se all cursors..
1a8b1 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 72 65 6c 65 61  **.** Also relea
1a8b2 73 65 20 61 6e 79 20 64 79 6e 61 6d 69 63 20 6d  se any dynamic m
1a8b3 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 74 68  emory held by th
1a8b4 65 20 56 4d 20 69 6e 20 74 68 65 20 56 64 62 65  e VM in the Vdbe
1a8b5 2e 61 4d 65 6d 20 6d 65 6d 6f 72 79 20 0a 2a 2a  .aMem memory .**
1a8b6 20 63 65 6c 6c 20 61 72 72 61 79 2e 20 54 68 69   cell array. Thi
1a8b7 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 61  s is necessary a
1a8b8 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c  s the memory cel
1a8b9 6c 20 61 72 72 61 79 20 6d 61 79 20 63 6f 6e 74  l array may cont
1a8ba 61 69 6e 0a 2a 2a 20 70 6f 69 6e 74 65 72 73 20  ain.** pointers 
1a8bb 74 6f 20 56 64 62 65 46 72 61 6d 65 20 6f 62 6a  to VdbeFrame obj
1a8bc 65 63 74 73 2c 20 77 68 69 63 68 20 6d 61 79 20  ects, which may 
1a8bd 69 6e 20 74 75 72 6e 20 63 6f 6e 74 61 69 6e 20  in turn contain 
1a8be 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 6f  pointers to.** o
1a8bf 70 65 6e 20 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a  pen cursors..*/.
1a8c0 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 6f 73  static void clos
1a8c1 65 41 6c 6c 43 75 72 73 6f 72 73 28 56 64 62 65  eAllCursors(Vdbe
1a8c2 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70   *p){.  if( p->p
1a8c3 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 56 64 62  Frame ){.    Vdb
1a8c4 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 20 3d  eFrame *pFrame =
1a8c5 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 20 20   p->pFrame;.    
1a8c6 66 6f 72 28 70 46 72 61 6d 65 3d 70 2d 3e 70 46  for(pFrame=p->pF
1a8c7 72 61 6d 65 3b 20 70 46 72 61 6d 65 2d 3e 70 50  rame; pFrame->pP
1a8c8 61 72 65 6e 74 3b 20 70 46 72 61 6d 65 3d 70 46  arent; pFrame=pF
1a8c9 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a  rame->pParent);.
1a8ca 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46      sqlite3VdbeF
1a8cb 72 61 6d 65 52 65 73 74 6f 72 65 28 70 46 72 61  rameRestore(pFra
1a8cc 6d 65 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 46  me);.  }.  p->pF
1a8cd 72 61 6d 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e  rame = 0;.  p->n
1a8ce 46 72 61 6d 65 20 3d 20 30 3b 0a 0a 20 20 69 66  Frame = 0;..  if
1a8cf 28 20 70 2d 3e 61 70 43 73 72 20 29 7b 0a 20 20  ( p->apCsr ){.  
1a8d0 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72    int i;.    for
1a8d1 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 73  (i=0; i<p->nCurs
1a8d2 6f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  or; i++){.      
1a8d3 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 20 3d  VdbeCursor *pC =
1a8d4 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20   p->apCsr[i];.  
1a8d5 20 20 20 20 69 66 28 20 70 43 20 29 7b 0a 20 20      if( pC ){.  
1a8d6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1a8d7 65 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20 70  eFreeCursor(p, p
1a8d8 43 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61  C);.        p->a
1a8d9 70 43 73 72 5b 69 5d 20 3d 20 30 3b 0a 20 20 20  pCsr[i] = 0;.   
1a8da 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
1a8db 20 69 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a   if( p->aMem ){.
1a8dc 20 20 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72      releaseMemAr
1a8dd 72 61 79 28 26 70 2d 3e 61 4d 65 6d 5b 31 5d 2c  ray(&p->aMem[1],
1a8de 20 70 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 7d 0a 7d   p->nMem);.  }.}
1a8df 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20 75 70  ../*.** Clean up
1a8e0 20 74 68 65 20 56 4d 20 61 66 74 65 72 20 65 78   the VM after ex
1a8e1 65 63 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  ecution..**.** T
1a8e2 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
1a8e3 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63   automatically c
1a8e4 6c 6f 73 65 20 61 6e 79 20 63 75 72 73 6f 72 73  lose any cursors
1a8e5 2c 20 6c 69 73 74 73 2c 20 61 6e 64 2f 6f 72 0a  , lists, and/or.
1a8e6 2a 2a 20 73 6f 72 74 65 72 73 20 74 68 61 74 20  ** sorters that 
1a8e7 77 65 72 65 20 6c 65 66 74 20 6f 70 65 6e 2e 20  were left open. 
1a8e8 20 49 74 20 61 6c 73 6f 20 64 65 6c 65 74 65 73   It also deletes
1a8e9 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 0a 2a   the values of.*
1a8ea 2a 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74  * variables in t
1a8eb 68 65 20 61 56 61 72 5b 5d 20 61 72 72 61 79 2e  he aVar[] array.
1a8ec 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1a8ed 43 6c 65 61 6e 75 70 28 56 64 62 65 20 2a 70 29  Cleanup(Vdbe *p)
1a8ee 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
1a8ef 3d 20 70 2d 3e 64 62 3b 0a 0a 23 69 66 64 65 66  = p->db;..#ifdef
1a8f0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
1a8f1 2f 2a 20 45 78 65 63 75 74 65 20 61 73 73 65 72  /* Execute asser
1a8f2 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 74  t() statements t
1a8f3 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68  o ensure that th
1a8f4 65 20 56 64 62 65 2e 61 70 43 73 72 5b 5d 20 61  e Vdbe.apCsr[] a
1a8f5 6e 64 20 0a 20 20 2a 2a 20 56 64 62 65 2e 61 4d  nd .  ** Vdbe.aM
1a8f6 65 6d 5b 5d 20 61 72 72 61 79 73 20 68 61 76 65  em[] arrays have
1a8f7 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6c   already been cl
1a8f8 65 61 6e 65 64 20 75 70 2e 20 20 2a 2f 0a 20 20  eaned up.  */.  
1a8f9 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30  int i;.  for(i=0
1a8fa 3b 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 3b 20  ; i<p->nCursor; 
1a8fb 69 2b 2b 29 20 61 73 73 65 72 74 28 20 70 2d 3e  i++) assert( p->
1a8fc 61 70 43 73 72 3d 3d 30 20 7c 7c 20 70 2d 3e 61  apCsr==0 || p->a
1a8fd 70 43 73 72 5b 69 5d 3d 3d 30 20 29 3b 0a 20 20  pCsr[i]==0 );.  
1a8fe 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 70 2d 3e 6e  for(i=1; i<=p->n
1a8ff 4d 65 6d 3b 20 69 2b 2b 29 20 61 73 73 65 72 74  Mem; i++) assert
1a900 28 20 70 2d 3e 61 4d 65 6d 3d 3d 30 20 7c 7c 20  ( p->aMem==0 || 
1a901 70 2d 3e 61 4d 65 6d 5b 69 5d 2e 66 6c 61 67 73  p->aMem[i].flags
1a902 3d 3d 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 23 65  ==MEM_Null );.#e
1a903 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 44  ndif..  sqlite3D
1a904 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72  bFree(db, p->zEr
1a905 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72  rMsg);.  p->zErr
1a906 4d 73 67 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 52  Msg = 0;.  p->pR
1a907 65 73 75 6c 74 53 65 74 20 3d 20 30 3b 0a 7d 0a  esultSet = 0;.}.
1a908 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e  ./*.** Set the n
1a909 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20  umber of result 
1a90a 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 77 69 6c  columns that wil
1a90b 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 62 79  l be returned by
1a90c 20 74 68 69 73 20 53 51 4c 0a 2a 2a 20 73 74 61   this SQL.** sta
1a90d 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20  tement. This is 
1a90e 6e 6f 77 20 73 65 74 20 61 74 20 63 6f 6d 70 69  now set at compi
1a90f 6c 65 20 74 69 6d 65 2c 20 72 61 74 68 65 72 20  le time, rather 
1a910 74 68 61 6e 20 64 75 72 69 6e 67 0a 2a 2a 20 65  than during.** e
1a911 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 20  xecution of the 
1a912 76 64 62 65 20 70 72 6f 67 72 61 6d 20 73 6f 20  vdbe program so 
1a913 74 68 61 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c  that sqlite3_col
1a914 75 6d 6e 5f 63 6f 75 6e 74 28 29 20 63 61 6e 0a  umn_count() can.
1a915 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20  ** be called on 
1a916 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  an SQL statement
1a917 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33 5f   before sqlite3_
1a918 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  step()..*/.SQLIT
1a919 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1a91a 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
1a91b 43 6f 6c 73 28 56 64 62 65 20 2a 70 2c 20 69 6e  Cols(Vdbe *p, in
1a91c 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 29 7b 0a 20  t nResColumn){. 
1a91d 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a   Mem *pColName;.
1a91e 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74    int n;.  sqlit
1a91f 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  e3 *db = p->db;.
1a920 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72  .  releaseMemArr
1a921 61 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20  ay(p->aColName, 
1a922 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f  p->nResColumn*CO
1a923 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 20 73 71 6c 69  LNAME_N);.  sqli
1a924 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
1a925 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 6e 20  >aColName);.  n 
1a926 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c  = nResColumn*COL
1a927 4e 41 4d 45 5f 4e 3b 0a 20 20 70 2d 3e 6e 52 65  NAME_N;.  p->nRe
1a928 73 43 6f 6c 75 6d 6e 20 3d 20 28 75 31 36 29 6e  sColumn = (u16)n
1a929 52 65 73 43 6f 6c 75 6d 6e 3b 0a 20 20 70 2d 3e  ResColumn;.  p->
1a92a 61 43 6f 6c 4e 61 6d 65 20 3d 20 70 43 6f 6c 4e  aColName = pColN
1a92b 61 6d 65 20 3d 20 28 4d 65 6d 2a 29 73 71 6c 69  ame = (Mem*)sqli
1a92c 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
1a92d 64 62 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a  db, sizeof(Mem)*
1a92e 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 43  n );.  if( p->aC
1a92f 6f 6c 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 75  olName==0 ) retu
1a930 72 6e 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d  rn;.  while( n--
1a931 20 3e 20 30 20 29 7b 0a 20 20 20 20 70 43 6f 6c   > 0 ){.    pCol
1a932 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  Name->flags = ME
1a933 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 43 6f 6c  M_Null;.    pCol
1a934 4e 61 6d 65 2d 3e 64 62 20 3d 20 70 2d 3e 64 62  Name->db = p->db
1a935 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2b 2b  ;.    pColName++
1a936 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  ;.  }.}../*.** S
1a937 65 74 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  et the name of t
1a938 68 65 20 69 64 78 27 74 68 20 63 6f 6c 75 6d 6e  he idx'th column
1a939 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20   to be returned 
1a93a 62 79 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  by the SQL state
1a93b 6d 65 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d 65 20 6d  ment..** zName m
1a93c 75 73 74 20 62 65 20 61 20 70 6f 69 6e 74 65 72  ust be a pointer
1a93d 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69 6e   to a nul termin
1a93e 61 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a  ated string..**.
1a93f 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 75 73  ** This call mus
1a940 74 20 62 65 20 6d 61 64 65 20 61 66 74 65 72 20  t be made after 
1a941 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
1a942 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
1a943 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6e  )..**.** The fin
1a944 61 6c 20 70 61 72 61 6d 65 74 65 72 2c 20 78 44  al parameter, xD
1a945 65 6c 2c 20 6d 75 73 74 20 62 65 20 6f 6e 65 20  el, must be one 
1a946 6f 66 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  of SQLITE_DYNAMI
1a947 43 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  C, SQLITE_STATIC
1a948 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 54 52  .** or SQLITE_TR
1a949 41 4e 53 49 45 4e 54 2e 20 49 66 20 69 74 20 69  ANSIENT. If it i
1a94a 73 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43  s SQLITE_DYNAMIC
1a94b 2c 20 74 68 65 6e 20 74 68 65 20 62 75 66 66 65  , then the buffe
1a94c 72 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20  r pointed.** to 
1a94d 62 79 20 7a 4e 61 6d 65 20 77 69 6c 6c 20 62 65  by zName will be
1a94e 20 66 72 65 65 64 20 62 79 20 73 71 6c 69 74 65   freed by sqlite
1a94f 33 44 62 46 72 65 65 28 29 20 77 68 65 6e 20 74  3DbFree() when t
1a950 68 65 20 76 64 62 65 20 69 73 20 64 65 73 74 72  he vdbe is destr
1a951 6f 79 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  oyed..*/.SQLITE_
1a952 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1a953 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
1a954 65 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20  e(.  Vdbe *p,   
1a955 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a956 20 20 20 20 20 20 2f 2a 20 56 64 62 65 20 62 65        /* Vdbe be
1a957 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 20 2a  ing configured *
1a958 2f 0a 20 20 69 6e 74 20 69 64 78 2c 20 20 20 20  /.  int idx,    
1a959 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a95a 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
1a95b 20 63 6f 6c 75 6d 6e 20 7a 4e 61 6d 65 20 61 70   column zName ap
1a95c 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 20 69 6e  plies to */.  in
1a95d 74 20 76 61 72 2c 20 20 20 20 20 20 20 20 20 20  t var,          
1a95e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a95f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 43 4f 4c  * One of the COL
1a960 4e 41 4d 45 5f 2a 20 63 6f 6e 73 74 61 6e 74 73  NAME_* constants
1a961 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
1a962 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20   *zName,        
1a963 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
1a964 72 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e 74  r to buffer cont
1a965 61 69 6e 69 6e 67 20 6e 61 6d 65 20 2a 2f 0a 20  aining name */. 
1a966 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
1a967 69 64 2a 29 20 20 20 20 20 20 20 20 20 20 20 20  id*)            
1a968 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6d 61 6e 61    /* Memory mana
1a969 67 65 6d 65 6e 74 20 73 74 72 61 74 65 67 79 20  gement strategy 
1a96a 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a 29 7b 0a  for zName */.){.
1a96b 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 20    int rc;.  Mem 
1a96c 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 61 73 73  *pColName;.  ass
1a96d 65 72 74 28 20 69 64 78 3c 70 2d 3e 6e 52 65 73  ert( idx<p->nRes
1a96e 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 61 73 73 65  Column );.  asse
1a96f 72 74 28 20 76 61 72 3c 43 4f 4c 4e 41 4d 45 5f  rt( var<COLNAME_
1a970 4e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64 62  N );.  if( p->db
1a971 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1a972 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 7a  {.    assert( !z
1a973 4e 61 6d 65 20 7c 7c 20 78 44 65 6c 21 3d 53 51  Name || xDel!=SQ
1a974 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 29 3b 0a  LITE_DYNAMIC );.
1a975 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1a976 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61  E_NOMEM;.  }.  a
1a977 73 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 4e 61  ssert( p->aColNa
1a978 6d 65 21 3d 30 20 29 3b 0a 20 20 70 43 6f 6c 4e  me!=0 );.  pColN
1a979 61 6d 65 20 3d 20 26 28 70 2d 3e 61 43 6f 6c 4e  ame = &(p->aColN
1a97a 61 6d 65 5b 69 64 78 2b 76 61 72 2a 70 2d 3e 6e  ame[idx+var*p->n
1a97b 52 65 73 43 6f 6c 75 6d 6e 5d 29 3b 0a 20 20 72  ResColumn]);.  r
1a97c 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
1a97d 65 6d 53 65 74 53 74 72 28 70 43 6f 6c 4e 61 6d  emSetStr(pColNam
1a97e 65 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51  e, zName, -1, SQ
1a97f 4c 49 54 45 5f 55 54 46 38 2c 20 78 44 65 6c 29  LITE_UTF8, xDel)
1a980 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d  ;.  assert( rc!=
1a981 30 20 7c 7c 20 21 7a 4e 61 6d 65 20 7c 7c 20 28  0 || !zName || (
1a982 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 26  pColName->flags&
1a983 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 20 29 3b 0a  MEM_Term)!=0 );.
1a984 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1a985 2f 2a 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72 20  /*.** A read or 
1a986 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
1a987 6e 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74  n may or may not
1a988 20 62 65 20 61 63 74 69 76 65 20 6f 6e 20 64 61   be active on da
1a989 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a  tabase handle.**
1a98a 20 64 62 2e 20 49 66 20 61 20 74 72 61 6e 73 61   db. If a transa
1a98b 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2c  ction is active,
1a98c 20 63 6f 6d 6d 69 74 20 69 74 2e 20 49 66 20 74   commit it. If t
1a98d 68 65 72 65 20 69 73 20 61 0a 2a 2a 20 77 72 69  here is a.** wri
1a98e 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73  te-transaction s
1a98f 70 61 6e 6e 69 6e 67 20 6d 6f 72 65 20 74 68 61  panning more tha
1a990 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66  n one database f
1a991 69 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  ile, this routin
1a992 65 0a 2a 2a 20 74 61 6b 65 73 20 63 61 72 65 20  e.** takes care 
1a993 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  of the master jo
1a994 75 72 6e 61 6c 20 74 72 69 63 6b 65 72 79 2e 0a  urnal trickery..
1a995 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64  */.static int vd
1a996 62 65 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33  beCommit(sqlite3
1a997 20 2a 64 62 2c 20 56 64 62 65 20 2a 70 29 7b 0a   *db, Vdbe *p){.
1a998 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e    int i;.  int n
1a999 54 72 61 6e 73 20 3d 20 30 3b 20 20 2f 2a 20 4e  Trans = 0;  /* N
1a99a 75 6d 62 65 72 20 6f 66 20 64 61 74 61 62 61 73  umber of databas
1a99b 65 73 20 77 69 74 68 20 61 6e 20 61 63 74 69 76  es with an activ
1a99c 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  e write-transact
1a99d 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20  ion */.  int rc 
1a99e 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
1a99f 6e 74 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d  nt needXcommit =
1a9a0 20 30 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   0;..#ifdef SQLI
1a9a1 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
1a9a2 41 42 4c 45 0a 20 20 2f 2a 20 57 69 74 68 20 74  ABLE.  /* With t
1a9a3 68 69 73 20 6f 70 74 69 6f 6e 2c 20 73 71 6c 69  his option, sqli
1a9a4 74 65 33 56 74 61 62 53 79 6e 63 28 29 20 69 73  te3VtabSync() is
1a9a5 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20 73   defined to be s
1a9a6 69 6d 70 6c 79 20 0a 20 20 2a 2a 20 53 51 4c 49  imply .  ** SQLI
1a9a7 54 45 5f 4f 4b 20 73 6f 20 70 20 69 73 20 6e 6f  TE_OK so p is no
1a9a8 74 20 75 73 65 64 2e 20 0a 20 20 2a 2f 0a 20 20  t used. .  */.  
1a9a9 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
1a9aa 28 70 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  (p);.#endif..  /
1a9ab 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 61  * Before doing a
1a9ac 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c 20 63 61  nything else, ca
1a9ad 6c 6c 20 74 68 65 20 78 53 79 6e 63 28 29 20 63  ll the xSync() c
1a9ae 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 79 0a  allback for any.
1a9af 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64    ** virtual mod
1a9b0 75 6c 65 20 74 61 62 6c 65 73 20 77 72 69 74 74  ule tables writt
1a9b1 65 6e 20 69 6e 20 74 68 69 73 20 74 72 61 6e 73  en in this trans
1a9b2 61 63 74 69 6f 6e 2e 20 54 68 69 73 20 68 61 73  action. This has
1a9b3 20 74 6f 0a 20 20 2a 2a 20 62 65 20 64 6f 6e 65   to.  ** be done
1a9b4 20 62 65 66 6f 72 65 20 64 65 74 65 72 6d 69 6e   before determin
1a9b5 69 6e 67 20 77 68 65 74 68 65 72 20 61 20 6d 61  ing whether a ma
1a9b6 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
1a9b7 65 20 69 73 20 0a 20 20 2a 2a 20 72 65 71 75 69  e is .  ** requi
1a9b8 72 65 64 2c 20 61 73 20 61 6e 20 78 53 79 6e 63  red, as an xSync
1a9b9 28 29 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20  () callback may 
1a9ba 61 64 64 20 61 6e 20 61 74 74 61 63 68 65 64 20  add an attached 
1a9bb 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 74 6f  database.  ** to
1a9bc 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
1a9bd 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71  ..  */.  rc = sq
1a9be 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 64 62  lite3VtabSync(db
1a9bf 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a  , &p->zErrMsg);.
1a9c0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1a9c1 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
1a9c2 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  n rc;.  }..  /* 
1a9c3 54 68 69 73 20 6c 6f 6f 70 20 64 65 74 65 72 6d  This loop determ
1a9c4 69 6e 65 73 20 28 61 29 20 69 66 20 74 68 65 20  ines (a) if the 
1a9c5 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 73 68 6f 75  commit hook shou
1a9c6 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 6e  ld be invoked an
1a9c7 64 0a 20 20 2a 2a 20 28 62 29 20 68 6f 77 20 6d  d.  ** (b) how m
1a9c8 61 6e 79 20 64 61 74 61 62 61 73 65 20 66 69 6c  any database fil
1a9c9 65 73 20 68 61 76 65 20 6f 70 65 6e 20 77 72 69  es have open wri
1a9ca 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2c  te transactions,
1a9cb 20 6e 6f 74 20 0a 20 20 2a 2a 20 69 6e 63 6c 75   not .  ** inclu
1a9cc 64 69 6e 67 20 74 68 65 20 74 65 6d 70 20 64 61  ding the temp da
1a9cd 74 61 62 61 73 65 2e 20 28 62 29 20 69 73 20 69  tabase. (b) is i
1a9ce 6d 70 6f 72 74 61 6e 74 20 62 65 63 61 75 73 65  mportant because
1a9cf 20 69 66 20 6d 6f 72 65 20 74 68 61 6e 20 0a 20   if more than . 
1a9d0 20 2a 2a 20 6f 6e 65 20 64 61 74 61 62 61 73 65   ** one database
1a9d1 20 66 69 6c 65 20 68 61 73 20 61 6e 20 6f 70 65   file has an ope
1a9d2 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74  n write transact
1a9d3 69 6f 6e 2c 20 61 20 6d 61 73 74 65 72 20 6a 6f  ion, a master jo
1a9d4 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65 20  urnal.  ** file 
1a9d5 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20  is required for 
1a9d6 61 6e 20 61 74 6f 6d 69 63 20 63 6f 6d 6d 69 74  an atomic commit
1a9d7 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72 28 69 3d  ..  */ .  for(i=
1a9d8 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
1a9d9 2b 29 7b 20 0a 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 69 66 28 20 73 71  .pBt;.    if( sq
1a9dc 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72  lite3BtreeIsInTr
1a9dd 61 6e 73 28 70 42 74 29 20 29 7b 0a 20 20 20 20  ans(pBt) ){.    
1a9de 20 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d 20    needXcommit = 
1a9df 31 3b 0a 20 20 20 20 20 20 69 66 28 20 69 21 3d  1;.      if( i!=
1a9e0 31 20 29 20 6e 54 72 61 6e 73 2b 2b 3b 0a 20 20  1 ) nTrans++;.  
1a9e1 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
1a9e2 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 77   there are any w
1a9e3 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
1a9e4 73 20 61 74 20 61 6c 6c 2c 20 69 6e 76 6f 6b 65  s at all, invoke
1a9e5 20 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b   the commit hook
1a9e6 20 2a 2f 0a 20 20 69 66 28 20 6e 65 65 64 58 63   */.  if( needXc
1a9e7 6f 6d 6d 69 74 20 26 26 20 64 62 2d 3e 78 43 6f  ommit && db->xCo
1a9e8 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 20 29 7b 0a  mmitCallback ){.
1a9e9 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
1a9ea 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
1a9eb 20 20 20 20 72 63 20 3d 20 64 62 2d 3e 78 43 6f      rc = db->xCo
1a9ec 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 28 64 62 2d  mmitCallback(db-
1a9ed 3e 70 43 6f 6d 6d 69 74 41 72 67 29 3b 0a 20 20  >pCommitArg);.  
1a9ee 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
1a9ef 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20  afetyOn(db);.   
1a9f0 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
1a9f1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
1a9f2 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 7d  ONSTRAINT;.    }
1a9f3 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 73  .  }..  /* The s
1a9f4 69 6d 70 6c 65 20 63 61 73 65 20 2d 20 6e 6f 20  imple case - no 
1a9f5 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61  more than one da
1a9f6 74 61 62 61 73 65 20 66 69 6c 65 20 28 6e 6f 74  tabase file (not
1a9f7 20 63 6f 75 6e 74 69 6e 67 20 74 68 65 0a 20 20   counting the.  
1a9f8 2a 2a 20 54 45 4d 50 20 64 61 74 61 62 61 73 65  ** TEMP database
1a9f9 29 20 68 61 73 20 61 20 74 72 61 6e 73 61 63 74  ) has a transact
1a9fa 69 6f 6e 20 61 63 74 69 76 65 2e 20 20 20 54 68  ion active.   Th
1a9fb 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66  ere is no need f
1a9fc 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d 61 73 74  or the.  ** mast
1a9fd 65 72 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a  er-journal..  **
1a9fe 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 72 65 74  .  ** If the ret
1a9ff 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 73 71 6c  urn value of sql
1aa00 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c 65  ite3BtreeGetFile
1aa01 6e 61 6d 65 28 29 20 69 73 20 61 20 7a 65 72 6f  name() is a zero
1aa02 20 6c 65 6e 67 74 68 0a 20 20 2a 2a 20 73 74 72   length.  ** str
1aa03 69 6e 67 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  ing, it means th
1aa04 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
1aa05 69 73 20 3a 6d 65 6d 6f 72 79 3a 20 6f 72 20 61  is :memory: or a
1aa06 20 74 65 6d 70 20 66 69 6c 65 2e 20 20 49 6e 20   temp file.  In 
1aa07 0a 20 20 2a 2a 20 74 68 61 74 20 63 61 73 65 20  .  ** that case 
1aa08 77 65 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72  we do not suppor
1aa09 74 20 61 74 6f 6d 69 63 20 6d 75 6c 74 69 2d 66  t atomic multi-f
1aa0a 69 6c 65 20 63 6f 6d 6d 69 74 73 2c 20 73 6f 20  ile commits, so 
1aa0b 75 73 65 20 74 68 65 20 0a 20 20 2a 2a 20 73 69  use the .  ** si
1aa0c 6d 70 6c 65 20 63 61 73 65 20 74 68 65 6e 20 74  mple case then t
1aa0d 6f 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 30  oo..  */.  if( 0
1aa0e 3d 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  ==sqlite3Strlen3
1aa0f 30 28 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  0(sqlite3BtreeGe
1aa10 74 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44  tFilename(db->aD
1aa11 62 5b 30 5d 2e 70 42 74 29 29 0a 20 20 20 7c 7c  b[0].pBt)).   ||
1aa12 20 6e 54 72 61 6e 73 3c 3d 31 0a 20 20 29 7b 0a   nTrans<=1.  ){.
1aa13 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d      for(i=0; rc=
1aa14 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c  =SQLITE_OK && i<
1aa15 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
1aa16 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20       Btree *pBt 
1aa17 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  = db->aDb[i].pBt
1aa18 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20  ;.      if( pBt 
1aa19 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
1aa1a 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
1aa1b 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20  itPhaseOne(pBt, 
1aa1c 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
1aa1d 7d 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65  }..    /* Do the
1aa1e 20 63 6f 6d 6d 69 74 20 6f 6e 6c 79 20 69 66 20   commit only if 
1aa1f 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 73 75  all databases su
1aa20 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 6d 70 6c  ccessfully compl
1aa21 65 74 65 20 70 68 61 73 65 20 31 2e 20 0a 20 20  ete phase 1. .  
1aa22 20 20 2a 2a 20 49 66 20 6f 6e 65 20 6f 66 20 74    ** If one of t
1aa23 68 65 20 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  he BtreeCommitPh
1aa24 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c 73 20 66  aseOne() calls f
1aa25 61 69 6c 73 2c 20 74 68 69 73 20 69 6e 64 69 63  ails, this indic
1aa26 61 74 65 73 20 61 6e 0a 20 20 20 20 2a 2a 20 49  ates an.    ** I
1aa27 4f 20 65 72 72 6f 72 20 77 68 69 6c 65 20 64 65  O error while de
1aa28 6c 65 74 69 6e 67 20 6f 72 20 74 72 75 6e 63 61  leting or trunca
1aa29 74 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 20 66  ting a journal f
1aa2a 69 6c 65 2e 20 49 74 20 69 73 20 75 6e 6c 69 6b  ile. It is unlik
1aa2b 65 6c 79 2c 0a 20 20 20 20 2a 2a 20 62 75 74 20  ely,.    ** but 
1aa2c 63 6f 75 6c 64 20 68 61 70 70 65 6e 2e 20 49 6e  could happen. In
1aa2d 20 74 68 69 73 20 63 61 73 65 20 61 62 61 6e 64   this case aband
1aa2e 6f 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e  on processing an
1aa2f 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72  d return the err
1aa30 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66  or..    */.    f
1aa31 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49  or(i=0; rc==SQLI
1aa32 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e  TE_OK && i<db->n
1aa33 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  Db; i++){.      
1aa34 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d  Btree *pBt = db-
1aa35 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20  >aDb[i].pBt;.   
1aa36 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20     if( pBt ){.  
1aa37 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1aa38 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
1aa39 73 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20  seTwo(pBt);.    
1aa3a 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
1aa3b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1aa3c 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1aa3d 56 74 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b 0a  VtabCommit(db);.
1aa3e 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
1aa3f 54 68 65 20 63 6f 6d 70 6c 65 78 20 63 61 73 65  The complex case
1aa40 20 2d 20 54 68 65 72 65 20 69 73 20 61 20 6d 75   - There is a mu
1aa41 6c 74 69 2d 66 69 6c 65 20 77 72 69 74 65 2d 74  lti-file write-t
1aa42 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69 76  ransaction activ
1aa43 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 72 65 71  e..  ** This req
1aa44 75 69 72 65 73 20 61 20 6d 61 73 74 65 72 20 6a  uires a master j
1aa45 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 65  ournal file to e
1aa46 6e 73 75 72 65 20 74 68 65 20 74 72 61 6e 73 61  nsure the transa
1aa47 63 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63 6f  ction is.  ** co
1aa48 6d 6d 69 74 74 65 64 20 61 74 6f 6d 69 63 6c 79  mmitted atomicly
1aa49 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ..  */.#ifndef S
1aa4a 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49  QLITE_OMIT_DISKI
1aa4b 4f 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 73 71  O.  else{.    sq
1aa4c 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20  lite3_vfs *pVfs 
1aa4d 3d 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 20 20  = db->pVfs;.    
1aa4e 69 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30  int needSync = 0
1aa4f 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 73  ;.    char *zMas
1aa50 74 65 72 20 3d 20 30 3b 20 20 20 2f 2a 20 46 69  ter = 0;   /* Fi
1aa51 6c 65 2d 6e 61 6d 65 20 66 6f 72 20 74 68 65 20  le-name for the 
1aa52 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 2a  master journal *
1aa53 2f 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74  /.    char const
1aa54 20 2a 7a 4d 61 69 6e 46 69 6c 65 20 3d 20 73 71   *zMainFile = sq
1aa55 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c  lite3BtreeGetFil
1aa56 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d  ename(db->aDb[0]
1aa57 2e 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  .pBt);.    sqlit
1aa58 65 33 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65 72  e3_file *pMaster
1aa59 20 3d 20 30 3b 0a 20 20 20 20 69 36 34 20 6f 66   = 0;.    i64 of
1aa5a 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e  fset = 0;.    in
1aa5b 74 20 72 65 73 3b 0a 0a 20 20 20 20 2f 2a 20 53  t res;..    /* S
1aa5c 65 6c 65 63 74 20 61 20 6d 61 73 74 65 72 20 6a  elect a master j
1aa5d 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65  ournal file name
1aa5e 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20   */.    do {.   
1aa5f 20 20 20 75 33 32 20 69 52 61 6e 64 6f 6d 3b 0a     u32 iRandom;.
1aa60 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1aa61 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29  ree(db, zMaster)
1aa62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
1aa63 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f  randomness(sizeo
1aa64 66 28 69 52 61 6e 64 6f 6d 29 2c 20 26 69 52 61  f(iRandom), &iRa
1aa65 6e 64 6f 6d 29 3b 0a 20 20 20 20 20 20 7a 4d 61  ndom);.      zMa
1aa66 73 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 50  ster = sqlite3MP
1aa67 72 69 6e 74 66 28 64 62 2c 20 22 25 73 2d 6d 6a  rintf(db, "%s-mj
1aa68 25 30 38 58 22 2c 20 7a 4d 61 69 6e 46 69 6c 65  %08X", zMainFile
1aa69 2c 20 69 52 61 6e 64 6f 6d 26 30 78 37 66 66 66  , iRandom&0x7fff
1aa6a 66 66 66 66 29 3b 0a 20 20 20 20 20 20 69 66 28  ffff);.      if(
1aa6b 20 21 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20   !zMaster ){.   
1aa6c 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
1aa6d 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
1aa6e 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  }.      rc = sql
1aa6f 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56 66  ite3OsAccess(pVf
1aa70 73 2c 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49  s, zMaster, SQLI
1aa71 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53  TE_ACCESS_EXISTS
1aa72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 7d 77 68  , &res);.    }wh
1aa73 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
1aa74 4f 4b 20 26 26 20 72 65 73 20 29 3b 0a 20 20 20  OK && res );.   
1aa75 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1aa76 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4f  OK ){.      /* O
1aa77 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20 6a  pen the master j
1aa78 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20 20 20 20  ournal. */.     
1aa79 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f   rc = sqlite3OsO
1aa7a 70 65 6e 4d 61 6c 6c 6f 63 28 70 56 66 73 2c 20  penMalloc(pVfs, 
1aa7b 7a 4d 61 73 74 65 72 2c 20 26 70 4d 61 73 74 65  zMaster, &pMaste
1aa7c 72 2c 20 0a 20 20 20 20 20 20 20 20 20 20 53 51  r, .          SQ
1aa7d 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
1aa7e 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ITE|SQLITE_OPEN_
1aa7f 43 52 45 41 54 45 7c 0a 20 20 20 20 20 20 20 20  CREATE|.        
1aa80 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58    SQLITE_OPEN_EX
1aa81 43 4c 55 53 49 56 45 7c 53 51 4c 49 54 45 5f 4f  CLUSIVE|SQLITE_O
1aa82 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e  PEN_MASTER_JOURN
1aa83 41 4c 2c 20 30 0a 20 20 20 20 20 20 29 3b 0a 20  AL, 0.      );. 
1aa84 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21     }.    if( rc!
1aa85 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1aa86 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1aa87 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a  e(db, zMaster);.
1aa88 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
1aa89 0a 20 20 20 20 7d 0a 20 0a 20 20 20 20 2f 2a 20  .    }. .    /* 
1aa8a 57 72 69 74 65 20 74 68 65 20 6e 61 6d 65 20 6f  Write the name o
1aa8b 66 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20  f each database 
1aa8c 66 69 6c 65 20 69 6e 20 74 68 65 20 74 72 61 6e  file in the tran
1aa8d 73 61 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65  saction into the
1aa8e 20 6e 65 77 0a 20 20 20 20 2a 2a 20 6d 61 73 74   new.    ** mast
1aa8f 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  er journal file.
1aa90 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
1aa91 75 72 73 20 61 74 20 74 68 69 73 20 70 6f 69 6e  urs at this poin
1aa92 74 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a 20 61  t close.    ** a
1aa93 6e 64 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61  nd delete the ma
1aa94 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
1aa95 65 2e 20 41 6c 6c 20 74 68 65 20 69 6e 64 69 76  e. All the indiv
1aa96 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69  idual journal fi
1aa97 6c 65 73 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c  les.    ** still
1aa98 20 68 61 76 65 20 27 6e 75 6c 6c 27 20 61 73 20   have 'null' as 
1aa99 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
1aa9a 61 6c 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 74  al pointer, so t
1aa9b 68 65 79 20 77 69 6c 6c 20 72 6f 6c 6c 0a 20 20  hey will roll.  
1aa9c 20 20 2a 2a 20 62 61 63 6b 20 69 6e 64 65 70 65    ** back indepe
1aa9d 6e 64 65 6e 74 6c 79 20 69 66 20 61 20 66 61 69  ndently if a fai
1aa9e 6c 75 72 65 20 6f 63 63 75 72 73 2e 0a 20 20 20  lure occurs..   
1aa9f 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   */.    for(i=0;
1aaa0 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
1aaa1 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70  {.      Btree *p
1aaa2 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e  Bt = db->aDb[i].
1aaa3 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 69  pBt;.      if( i
1aaa4 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20  ==1 ) continue; 
1aaa5 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20    /* Ignore the 
1aaa6 54 45 4d 50 20 64 61 74 61 62 61 73 65 20 2a 2f  TEMP database */
1aaa7 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
1aaa8 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73  e3BtreeIsInTrans
1aaa9 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20  (pBt) ){.       
1aaaa 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46 69   char const *zFi
1aaab 6c 65 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  le = sqlite3Btre
1aaac 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28  eGetJournalname(
1aaad 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66  pBt);.        if
1aaae 28 20 7a 46 69 6c 65 5b 30 5d 3d 3d 30 20 29 20  ( zFile[0]==0 ) 
1aaaf 63 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 49 67  continue;  /* Ig
1aab0 6e 6f 72 65 20 3a 6d 65 6d 6f 72 79 3a 20 64 61  nore :memory: da
1aab1 74 61 62 61 73 65 73 20 2a 2f 0a 20 20 20 20 20  tabases */.     
1aab2 20 20 20 69 66 28 20 21 6e 65 65 64 53 79 6e 63     if( !needSync
1aab3 20 26 26 20 21 73 71 6c 69 74 65 33 42 74 72 65   && !sqlite3Btre
1aab4 65 53 79 6e 63 44 69 73 61 62 6c 65 64 28 70 42  eSyncDisabled(pB
1aab5 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  t) ){.          
1aab6 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20  needSync = 1;.  
1aab7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1aab8 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
1aab9 69 74 65 28 70 4d 61 73 74 65 72 2c 20 7a 46 69  ite(pMaster, zFi
1aaba 6c 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65  le, sqlite3Strle
1aabb 6e 33 30 28 7a 46 69 6c 65 29 2b 31 2c 20 6f 66  n30(zFile)+1, of
1aabc 66 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6f  fset);.        o
1aabd 66 66 73 65 74 20 2b 3d 20 73 71 6c 69 74 65 33  ffset += sqlite3
1aabe 53 74 72 6c 65 6e 33 30 28 7a 46 69 6c 65 29 2b  Strlen30(zFile)+
1aabf 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  1;.        if( r
1aac0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1aac1 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1aac2 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61  3OsCloseFree(pMa
1aac3 73 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20  ster);.         
1aac4 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65   sqlite3OsDelete
1aac5 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20  (pVfs, zMaster, 
1aac6 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  0);.          sq
1aac7 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1aac8 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20  zMaster);.      
1aac9 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1aaca 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1aacb 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53  .    }..    /* S
1aacc 79 6e 63 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ync the master j
1aacd 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20  ournal file. If 
1aace 74 68 65 20 49 4f 43 41 50 5f 53 45 51 55 45 4e  the IOCAP_SEQUEN
1aacf 54 49 41 4c 20 64 65 76 69 63 65 0a 20 20 20 20  TIAL device.    
1aad0 2a 2a 20 66 6c 61 67 20 69 73 20 73 65 74 20 74  ** flag is set t
1aad1 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69  his is not requi
1aad2 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  red..    */.    
1aad3 69 66 28 20 6e 65 65 64 53 79 6e 63 20 0a 20 20  if( needSync .  
1aad4 20 20 20 26 26 20 30 3d 3d 28 73 71 6c 69 74 65     && 0==(sqlite
1aad5 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74  3OsDeviceCharact
1aad6 65 72 69 73 74 69 63 73 28 70 4d 61 73 74 65 72  eristics(pMaster
1aad7 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  )&SQLITE_IOCAP_S
1aad8 45 51 55 45 4e 54 49 41 4c 29 0a 20 20 20 20 20  EQUENTIAL).     
1aad9 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72  && SQLITE_OK!=(r
1aada 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e  c = sqlite3OsSyn
1aadb 63 28 70 4d 61 73 74 65 72 2c 20 53 51 4c 49 54  c(pMaster, SQLIT
1aadc 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 29 29 0a  E_SYNC_NORMAL)).
1aadd 20 20 20 20 29 7b 0a 20 20 20 20 20 20 73 71 6c      ){.      sql
1aade 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28  ite3OsCloseFree(
1aadf 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20  pMaster);.      
1aae0 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28  sqlite3OsDelete(
1aae1 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30  pVfs, zMaster, 0
1aae2 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1aae3 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74  DbFree(db, zMast
1aae4 65 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  er);.      retur
1aae5 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  n rc;.    }..   
1aae6 20 2f 2a 20 53 79 6e 63 20 61 6c 6c 20 74 68 65   /* Sync all the
1aae7 20 64 62 20 66 69 6c 65 73 20 69 6e 76 6f 6c 76   db files involv
1aae8 65 64 20 69 6e 20 74 68 65 20 74 72 61 6e 73 61  ed in the transa
1aae9 63 74 69 6f 6e 2e 20 54 68 65 20 73 61 6d 65 20  ction. The same 
1aaea 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 73 65 74 73  call.    ** sets
1aaeb 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
1aaec 6e 61 6c 20 70 6f 69 6e 74 65 72 20 69 6e 20 65  nal pointer in e
1aaed 61 63 68 20 69 6e 64 69 76 69 64 75 61 6c 20 6a  ach individual j
1aaee 6f 75 72 6e 61 6c 2e 20 49 66 0a 20 20 20 20 2a  ournal. If.    *
1aaef 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  * an error occur
1aaf0 73 20 68 65 72 65 2c 20 64 6f 20 6e 6f 74 20 64  s here, do not d
1aaf1 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72  elete the master
1aaf2 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20   journal file.. 
1aaf3 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20     **.    ** If 
1aaf4 74 68 65 20 65 72 72 6f 72 20 6f 63 63 75 72 73  the error occurs
1aaf5 20 64 75 72 69 6e 67 20 74 68 65 20 66 69 72 73   during the firs
1aaf6 74 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a  t call to.    **
1aaf7 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
1aaf8 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 2c 20 74  mitPhaseOne(), t
1aaf9 68 65 6e 20 74 68 65 72 65 20 69 73 20 61 20 63  hen there is a c
1aafa 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 0a 20  hance that the. 
1aafb 20 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75     ** master jou
1aafc 72 6e 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 62  rnal file will b
1aafd 65 20 6f 72 70 68 61 6e 65 64 2e 20 42 75 74 20  e orphaned. But 
1aafe 77 65 20 63 61 6e 6e 6f 74 20 64 65 6c 65 74 65  we cannot delete
1aaff 20 69 74 2c 0a 20 20 20 20 2a 2a 20 69 6e 20 63   it,.    ** in c
1ab00 61 73 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ase the master j
1ab01 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65  ournal file name
1ab02 20 77 61 73 20 77 72 69 74 74 65 6e 20 69 6e 74   was written int
1ab03 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20  o the journal.  
1ab04 20 20 2a 2a 20 66 69 6c 65 20 62 65 66 6f 72 65    ** file before
1ab05 20 74 68 65 20 66 61 69 6c 75 72 65 20 6f 63 63   the failure occ
1ab06 75 72 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  urred..    */.  
1ab07 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
1ab08 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62  QLITE_OK && i<db
1ab09 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20  ->nDb; i++){ .  
1ab0a 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d      Btree *pBt =
1ab0b 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
1ab0c 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29  .      if( pBt )
1ab0d 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
1ab0e 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
1ab0f 74 50 68 61 73 65 4f 6e 65 28 70 42 74 2c 20 7a  tPhaseOne(pBt, z
1ab10 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 7d  Master);.      }
1ab11 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
1ab12 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d  e3OsCloseFree(pM
1ab13 61 73 74 65 72 29 3b 0a 20 20 20 20 69 66 28 20  aster);.    if( 
1ab14 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1ab15 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
1ab16 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72  Free(db, zMaster
1ab17 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1ab18 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  rc;.    }..    /
1ab19 2a 20 44 65 6c 65 74 65 20 74 68 65 20 6d 61 73  * Delete the mas
1ab1a 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
1ab1b 2e 20 54 68 69 73 20 63 6f 6d 6d 69 74 73 20 74  . This commits t
1ab1c 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  he transaction. 
1ab1d 41 66 74 65 72 0a 20 20 20 20 2a 2a 20 64 6f 69  After.    ** doi
1ab1e 6e 67 20 74 68 69 73 20 74 68 65 20 64 69 72 65  ng this the dire
1ab1f 63 74 6f 72 79 20 69 73 20 73 79 6e 63 65 64 20  ctory is synced 
1ab20 61 67 61 69 6e 20 62 65 66 6f 72 65 20 61 6e 79  again before any
1ab21 20 69 6e 64 69 76 69 64 75 61 6c 0a 20 20 20 20   individual.    
1ab22 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 66  ** transaction f
1ab23 69 6c 65 73 20 61 72 65 20 64 65 6c 65 74 65 64  iles are deleted
1ab24 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20  ..    */.    rc 
1ab25 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74  = sqlite3OsDelet
1ab26 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c  e(pVfs, zMaster,
1ab27 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   1);.    sqlite3
1ab28 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74  DbFree(db, zMast
1ab29 65 72 29 3b 0a 20 20 20 20 7a 4d 61 73 74 65 72  er);.    zMaster
1ab2a 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63   = 0;.    if( rc
1ab2b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1ab2c 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20   rc;.    }..    
1ab2d 2f 2a 20 41 6c 6c 20 66 69 6c 65 73 20 61 6e 64  /* All files and
1ab2e 20 64 69 72 65 63 74 6f 72 69 65 73 20 68 61 76   directories hav
1ab2f 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 73  e already been s
1ab30 79 6e 63 65 64 2c 20 73 6f 20 74 68 65 20 66 6f  ynced, so the fo
1ab31 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 2a 2a 20 63  llowing.    ** c
1ab32 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 42  alls to sqlite3B
1ab33 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54  treeCommitPhaseT
1ab34 77 6f 28 29 20 61 72 65 20 6f 6e 6c 79 20 63 6c  wo() are only cl
1ab35 6f 73 69 6e 67 20 66 69 6c 65 73 20 61 6e 64 0a  osing files and.
1ab36 20 20 20 20 2a 2a 20 64 65 6c 65 74 69 6e 67 20      ** deleting 
1ab37 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 6a 6f  or truncating jo
1ab38 75 72 6e 61 6c 73 2e 20 49 66 20 73 6f 6d 65 74  urnals. If somet
1ab39 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 20  hing goes wrong 
1ab3a 77 68 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 69  while.    ** thi
1ab3b 73 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20 77  s is happening w
1ab3c 65 20 64 6f 6e 27 74 20 72 65 61 6c 6c 79 20 63  e don't really c
1ab3d 61 72 65 2e 20 54 68 65 20 69 6e 74 65 67 72 69  are. The integri
1ab3e 74 79 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a  ty of the.    **
1ab3f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
1ab40 61 6c 72 65 61 64 79 20 67 75 61 72 61 6e 74 65  already guarante
1ab41 65 64 2c 20 62 75 74 20 73 6f 6d 65 20 73 74 72  ed, but some str
1ab42 61 79 20 27 63 6f 6c 64 27 20 6a 6f 75 72 6e 61  ay 'cold' journa
1ab43 6c 73 0a 20 20 20 20 2a 2a 20 6d 61 79 20 62 65  ls.    ** may be
1ab44 20 6c 79 69 6e 67 20 61 72 6f 75 6e 64 2e 20 52   lying around. R
1ab45 65 74 75 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f  eturning an erro
1ab46 72 20 63 6f 64 65 20 77 6f 6e 27 74 20 68 65 6c  r code won't hel
1ab47 70 20 6d 61 74 74 65 72 73 2e 0a 20 20 20 20 2a  p matters..    *
1ab48 2f 0a 20 20 20 20 64 69 73 61 62 6c 65 5f 73 69  /.    disable_si
1ab49 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72  mulated_io_error
1ab4a 73 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  s();.    sqlite3
1ab4b 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f  BeginBenignMallo
1ab4c 63 28 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  c();.    for(i=0
1ab4d 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
1ab4e 29 7b 20 0a 20 20 20 20 20 20 42 74 72 65 65 20  ){ .      Btree 
1ab4f 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69  *pBt = db->aDb[i
1ab50 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28  ].pBt;.      if(
1ab51 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20   pBt ){.        
1ab52 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
1ab53 69 74 50 68 61 73 65 54 77 6f 28 70 42 74 29 3b  itPhaseTwo(pBt);
1ab54 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1ab55 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e     sqlite3EndBen
1ab56 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20  ignMalloc();.   
1ab57 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65   enable_simulate
1ab58 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 0a  d_io_errors();..
1ab59 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 43      sqlite3VtabC
1ab5a 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23  ommit(db);.  }.#
1ab5b 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20  endif..  return 
1ab5c 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68  rc;.}../* .** Th
1ab5d 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b  is routine check
1ab5e 73 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74  s that the sqlit
1ab5f 65 33 2e 61 63 74 69 76 65 56 64 62 65 43 6e 74  e3.activeVdbeCnt
1ab60 20 63 6f 75 6e 74 20 76 61 72 69 61 62 6c 65 0a   count variable.
1ab61 2a 2a 20 6d 61 74 63 68 65 73 20 74 68 65 20 6e  ** matches the n
1ab62 75 6d 62 65 72 20 6f 66 20 76 64 62 65 27 73 20  umber of vdbe's 
1ab63 69 6e 20 74 68 65 20 6c 69 73 74 20 73 71 6c 69  in the list sqli
1ab64 74 65 33 2e 70 56 64 62 65 20 74 68 61 74 20 61  te3.pVdbe that a
1ab65 72 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20  re.** currently 
1ab66 61 63 74 69 76 65 2e 20 41 6e 20 61 73 73 65 72  active. An asser
1ab67 74 69 6f 6e 20 66 61 69 6c 73 20 69 66 20 74 68  tion fails if th
1ab68 65 20 74 77 6f 20 63 6f 75 6e 74 73 20 64 6f 20  e two counts do 
1ab69 6e 6f 74 20 6d 61 74 63 68 2e 0a 2a 2a 20 54 68  not match..** Th
1ab6a 69 73 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61  is is an interna
1ab6b 6c 20 73 65 6c 66 2d 63 68 65 63 6b 20 6f 6e 6c  l self-check onl
1ab6c 79 20 2d 20 69 74 20 69 73 20 6e 6f 74 20 61 6e  y - it is not an
1ab6d 20 65 73 73 65 6e 74 69 61 6c 20 70 72 6f 63 65   essential proce
1ab6e 73 73 69 6e 67 0a 2a 2a 20 73 74 65 70 2e 0a 2a  ssing.** step..*
1ab6f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 6e  *.** This is a n
1ab70 6f 2d 6f 70 20 69 66 20 4e 44 45 42 55 47 20 69  o-op if NDEBUG i
1ab71 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69  s defined..*/.#i
1ab72 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61  fndef NDEBUG.sta
1ab73 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 41 63  tic void checkAc
1ab74 74 69 76 65 56 64 62 65 43 6e 74 28 73 71 6c 69  tiveVdbeCnt(sqli
1ab75 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 65  te3 *db){.  Vdbe
1ab76 20 2a 70 3b 0a 20 20 69 6e 74 20 63 6e 74 20 3d   *p;.  int cnt =
1ab77 20 30 3b 0a 20 20 69 6e 74 20 6e 57 72 69 74 65   0;.  int nWrite
1ab78 20 3d 20 30 3b 0a 20 20 70 20 3d 20 64 62 2d 3e   = 0;.  p = db->
1ab79 70 56 64 62 65 3b 0a 20 20 77 68 69 6c 65 28 20  pVdbe;.  while( 
1ab7a 70 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  p ){.    if( p->
1ab7b 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49  magic==VDBE_MAGI
1ab7c 43 5f 52 55 4e 20 26 26 20 70 2d 3e 70 63 3e 3d  C_RUN && p->pc>=
1ab7d 30 20 29 7b 0a 20 20 20 20 20 20 63 6e 74 2b 2b  0 ){.      cnt++
1ab7e 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72  ;.      if( p->r
1ab7f 65 61 64 4f 6e 6c 79 3d 3d 30 20 29 20 6e 57 72  eadOnly==0 ) nWr
1ab80 69 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20  ite++;.    }.   
1ab81 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20   p = p->pNext;. 
1ab82 20 7d 0a 20 20 61 73 73 65 72 74 28 20 63 6e 74   }.  assert( cnt
1ab83 3d 3d 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65  ==db->activeVdbe
1ab84 43 6e 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28  Cnt );.  assert(
1ab85 20 6e 57 72 69 74 65 3d 3d 64 62 2d 3e 77 72 69   nWrite==db->wri
1ab86 74 65 56 64 62 65 43 6e 74 20 29 3b 0a 7d 0a 23  teVdbeCnt );.}.#
1ab87 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 63 68 65  else.#define che
1ab88 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e 74 28  ckActiveVdbeCnt(
1ab89 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  x).#endif../*.**
1ab8a 20 46 6f 72 20 65 76 65 72 79 20 42 74 72 65 65   For every Btree
1ab8b 20 74 68 61 74 20 69 6e 20 64 61 74 61 62 61 73   that in databas
1ab8c 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20  e connection db 
1ab8d 77 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 62 65  which .** has be
1ab8e 65 6e 20 6d 6f 64 69 66 69 65 64 2c 20 22 74 72  en modified, "tr
1ab8f 69 70 22 20 6f 72 20 69 6e 76 61 6c 69 64 61 74  ip" or invalidat
1ab90 65 20 65 61 63 68 20 63 75 72 73 6f 72 20 69 6e  e each cursor in
1ab91 0a 2a 2a 20 74 68 61 74 20 42 74 72 65 65 20 6d  .** that Btree m
1ab92 69 67 68 74 20 68 61 76 65 20 62 65 65 6e 20 6d  ight have been m
1ab93 6f 64 69 66 69 65 64 20 73 6f 20 74 68 61 74 20  odified so that 
1ab94 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 63 61  the cursor.** ca
1ab95 6e 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 20  n never be used 
1ab96 61 67 61 69 6e 2e 20 20 54 68 69 73 20 68 61 70  again.  This hap
1ab97 70 65 6e 73 20 77 68 65 6e 20 61 20 72 6f 6c 6c  pens when a roll
1ab98 62 61 63 6b 0a 2a 2a 2a 20 6f 63 63 75 72 73 2e  back.*** occurs.
1ab99 20 20 57 65 20 68 61 76 65 20 74 6f 20 74 72 69    We have to tri
1ab9a 70 20 61 6c 6c 20 74 68 65 20 6f 74 68 65 72 20  p all the other 
1ab9b 63 75 72 73 6f 72 73 2c 20 65 76 65 6e 0a 2a 2a  cursors, even.**
1ab9c 20 63 75 72 73 6f 72 20 66 72 6f 6d 20 6f 74 68   cursor from oth
1ab9d 65 72 20 56 4d 73 20 69 6e 20 64 69 66 66 65 72  er VMs in differ
1ab9e 65 6e 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ent database con
1ab9f 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a 20 73 6f 20  nections,.** so 
1aba0 74 68 61 74 20 6e 6f 6e 65 20 6f 66 20 74 68 65  that none of the
1aba1 6d 20 74 72 79 20 74 6f 20 75 73 65 20 74 68 65  m try to use the
1aba2 20 64 61 74 61 20 61 74 20 77 68 69 63 68 20 74   data at which t
1aba3 68 65 79 0a 2a 2a 20 77 65 72 65 20 70 6f 69 6e  hey.** were poin
1aba4 74 69 6e 67 20 61 6e 64 20 77 68 69 63 68 20 6e  ting and which n
1aba5 6f 77 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e  ow may have been
1aba6 20 63 68 61 6e 67 65 64 20 64 75 65 0a 2a 2a 20   changed due.** 
1aba7 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e  to the rollback.
1aba8 0a 2a 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20  .**.** Remember 
1aba9 74 68 61 74 20 61 20 72 6f 6c 6c 62 61 63 6b 20  that a rollback 
1abaa 63 61 6e 20 64 65 6c 65 74 65 20 74 61 62 6c 65  can delete table
1abab 73 20 63 6f 6d 70 6c 65 74 65 20 61 6e 64 0a 2a  s complete and.*
1abac 2a 20 72 65 6f 72 64 65 72 20 72 6f 6f 74 70 61  * reorder rootpa
1abad 67 65 73 2e 20 20 53 6f 20 69 74 20 69 73 20 6e  ges.  So it is n
1abae 6f 74 20 73 75 66 66 69 63 69 65 6e 74 20 6a 75  ot sufficient ju
1abaf 73 74 20 74 6f 20 73 61 76 65 0a 2a 2a 20 74 68  st to save.** th
1abb0 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 63  e state of the c
1abb1 75 72 73 6f 72 2e 20 20 57 65 20 68 61 76 65 20  ursor.  We have 
1abb2 74 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68  to invalidate th
1abb3 65 20 63 75 72 73 6f 72 0a 2a 2a 20 73 6f 20 74  e cursor.** so t
1abb4 68 61 74 20 69 74 20 69 73 20 6e 65 76 65 72 20  hat it is never 
1abb5 75 73 65 64 20 61 67 61 69 6e 2e 0a 2a 2f 0a 73  used again..*/.s
1abb6 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c  tatic void inval
1abb7 69 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d 6f  idateCursorsOnMo
1abb8 64 69 66 69 65 64 42 74 72 65 65 73 28 73 71 6c  difiedBtrees(sql
1abb9 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74  ite3 *db){.  int
1abba 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   i;.  for(i=0; i
1abbb 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
1abbc 20 20 20 20 42 74 72 65 65 20 2a 70 20 3d 20 64      Btree *p = d
1abbd 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20  b->aDb[i].pBt;. 
1abbe 20 20 20 69 66 28 20 70 20 26 26 20 73 71 6c 69     if( p && sqli
1abbf 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e  te3BtreeIsInTran
1abc0 73 28 70 29 20 29 7b 0a 20 20 20 20 20 20 73 71  s(p) ){.      sq
1abc1 6c 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c  lite3BtreeTripAl
1abc2 6c 43 75 72 73 6f 72 73 28 70 2c 20 53 51 4c 49  lCursors(p, SQLI
1abc3 54 45 5f 41 42 4f 52 54 29 3b 0a 20 20 20 20 7d  TE_ABORT);.    }
1abc4 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  .  }.}../*.** If
1abc5 20 74 68 65 20 56 64 62 65 20 70 61 73 73 65 64   the Vdbe passed
1abc6 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72   as the first ar
1abc7 67 75 6d 65 6e 74 20 6f 70 65 6e 65 64 20 61 20  gument opened a 
1abc8 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61  statement-transa
1abc9 63 74 69 6f 6e 2c 0a 2a 2a 20 63 6c 6f 73 65 20  ction,.** close 
1abca 69 74 20 6e 6f 77 2e 20 41 72 67 75 6d 65 6e 74  it now. Argument
1abcb 20 65 4f 70 20 6d 75 73 74 20 62 65 20 65 69 74   eOp must be eit
1abcc 68 65 72 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f  her SAVEPOINT_RO
1abcd 4c 4c 42 41 43 4b 20 6f 72 0a 2a 2a 20 53 41 56  LLBACK or.** SAV
1abce 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 20  EPOINT_RELEASE. 
1abcf 49 66 20 69 74 20 69 73 20 53 41 56 45 50 4f 49  If it is SAVEPOI
1abd0 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65  NT_ROLLBACK, the
1abd1 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a  n the statement.
1abd2 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  ** transaction i
1abd3 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49  s rolled back. I
1abd4 66 20 65 4f 70 20 69 73 20 53 41 56 45 50 4f 49  f eOp is SAVEPOI
1abd5 4e 54 5f 52 45 4c 45 41 53 45 2c 20 74 68 65 6e  NT_RELEASE, then
1abd6 20 74 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65   the .** stateme
1abd7 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  nt transaction i
1abd8 73 20 63 6f 6d 6d 74 74 65 64 2e 0a 2a 2a 0a 2a  s commtted..**.*
1abd9 2a 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72  * If an IO error
1abda 20 6f 63 63 75 72 73 2c 20 61 6e 20 53 51 4c 49   occurs, an SQLI
1abdb 54 45 5f 49 4f 45 52 52 5f 58 58 58 20 65 72 72  TE_IOERR_XXX err
1abdc 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
1abdd 6e 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69  ned. .** Otherwi
1abde 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f  se SQLITE_OK..*/
1abdf 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1abe0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43  int sqlite3VdbeC
1abe1 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 56 64  loseStatement(Vd
1abe2 62 65 20 2a 70 2c 20 69 6e 74 20 65 4f 70 29 7b  be *p, int eOp){
1abe3 0a 20 20 73 71 6c 69 74 65 33 20 2a 63 6f 6e 73  .  sqlite3 *cons
1abe4 74 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20  t db = p->db;.  
1abe5 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1abe6 4f 4b 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 2d 3e  OK;..  /* If p->
1abe7 69 53 74 61 74 65 6d 65 6e 74 20 69 73 20 67 72  iStatement is gr
1abe8 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c  eater than zero,
1abe9 20 74 68 65 6e 20 74 68 69 73 20 56 64 62 65 20   then this Vdbe 
1abea 6f 70 65 6e 65 64 20 61 20 0a 20 20 2a 2a 20 73  opened a .  ** s
1abeb 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
1abec 74 69 6f 6e 20 74 68 61 74 20 73 68 6f 75 6c 64  tion that should
1abed 20 62 65 20 63 6c 6f 73 65 64 20 68 65 72 65 2e   be closed here.
1abee 20 54 68 65 20 6f 6e 6c 79 20 65 78 63 65 70 74   The only except
1abef 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 74 68 61 74  ion.  ** is that
1abf0 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6d 61 79   an IO error may
1abf1 20 68 61 76 65 20 6f 63 63 75 72 65 64 2c 20 63   have occured, c
1abf2 61 75 73 69 6e 67 20 61 6e 20 65 6d 65 72 67 65  ausing an emerge
1abf3 6e 63 79 20 72 6f 6c 6c 62 61 63 6b 2e 0a 20 20  ncy rollback..  
1abf4 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  ** In this case 
1abf5 28 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d  (db->nStatement=
1abf6 3d 30 29 2c 20 61 6e 64 20 74 68 65 72 65 20 69  =0), and there i
1abf7 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 64 6f 2e  s nothing to do.
1abf8 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  .  */.  if( db->
1abf9 6e 53 74 61 74 65 6d 65 6e 74 20 26 26 20 70 2d  nStatement && p-
1abfa 3e 69 53 74 61 74 65 6d 65 6e 74 20 29 7b 0a 20  >iStatement ){. 
1abfb 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 6f     int i;.    co
1abfc 6e 73 74 20 69 6e 74 20 69 53 61 76 65 70 6f 69  nst int iSavepoi
1abfd 6e 74 20 3d 20 70 2d 3e 69 53 74 61 74 65 6d 65  nt = p->iStateme
1abfe 6e 74 2d 31 3b 0a 0a 20 20 20 20 61 73 73 65 72  nt-1;..    asser
1abff 74 28 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e  t( eOp==SAVEPOIN
1ac00 54 5f 52 4f 4c 4c 42 41 43 4b 20 7c 7c 20 65 4f  T_ROLLBACK || eO
1ac01 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c  p==SAVEPOINT_REL
1ac02 45 41 53 45 29 3b 0a 20 20 20 20 61 73 73 65 72  EASE);.    asser
1ac03 74 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e  t( db->nStatemen
1ac04 74 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  t>0 );.    asser
1ac05 74 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74  t( p->iStatement
1ac06 3d 3d 28 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e  ==(db->nStatemen
1ac07 74 2b 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74  t+db->nSavepoint
1ac08 29 20 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d  ) );..    for(i=
1ac09 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
1ac0a 2b 29 7b 20 0a 20 20 20 20 20 20 69 6e 74 20 72  +){ .      int r
1ac0b 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  c2 = SQLITE_OK;.
1ac0c 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74        Btree *pBt
1ac0d 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
1ac0e 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74  t;.      if( pBt
1ac0f 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
1ac10 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52  eOp==SAVEPOINT_R
1ac11 4f 4c 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20 20  OLLBACK ){.     
1ac12 20 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74       rc2 = sqlit
1ac13 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74  e3BtreeSavepoint
1ac14 28 70 42 74 2c 20 53 41 56 45 50 4f 49 4e 54 5f  (pBt, SAVEPOINT_
1ac15 52 4f 4c 4c 42 41 43 4b 2c 20 69 53 61 76 65 70  ROLLBACK, iSavep
1ac16 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  oint);.        }
1ac17 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 32  .        if( rc2
1ac18 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1ac19 20 20 20 20 20 20 20 20 20 72 63 32 20 3d 20 73           rc2 = s
1ac1a 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70  qlite3BtreeSavep
1ac1b 6f 69 6e 74 28 70 42 74 2c 20 53 41 56 45 50 4f  oint(pBt, SAVEPO
1ac1c 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 69 53 61  INT_RELEASE, iSa
1ac1d 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20  vepoint);.      
1ac1e 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
1ac1f 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1ac20 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
1ac21 72 63 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  rc2;.        }. 
1ac22 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1ac23 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2d   db->nStatement-
1ac24 2d 3b 0a 20 20 20 20 70 2d 3e 69 53 74 61 74 65  -;.    p->iState
1ac25 6d 65 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 2f  ment = 0;..    /
1ac26 2a 20 49 66 20 74 68 65 20 73 74 61 74 65 6d 65  * If the stateme
1ac27 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  nt transaction i
1ac28 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62  s being rolled b
1ac29 61 63 6b 2c 20 61 6c 73 6f 20 72 65 73 74 6f 72  ack, also restor
1ac2a 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 64 61  e the .    ** da
1ac2b 74 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20 64  tabase handles d
1ac2c 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69  eferred constrai
1ac2d 6e 74 20 63 6f 75 6e 74 65 72 20 74 6f 20 74 68  nt counter to th
1ac2e 65 20 76 61 6c 75 65 20 69 74 20 68 61 64 20 77  e value it had w
1ac2f 68 65 6e 20 0a 20 20 20 20 2a 2a 20 74 68 65 20  hen .    ** the 
1ac30 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
1ac31 63 74 69 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64  ction was opened
1ac32 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 4f  .  */.    if( eO
1ac33 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c  p==SAVEPOINT_ROL
1ac34 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20 20 20 64  LBACK ){.      d
1ac35 62 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73  b->nDeferredCons
1ac36 20 3d 20 70 2d 3e 6e 53 74 6d 74 44 65 66 43 6f   = p->nStmtDefCo
1ac37 6e 73 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ns;.    }.  }.  
1ac38 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1ac39 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73  .** If SQLite is
1ac3a 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 73 75 70   compiled to sup
1ac3b 70 6f 72 74 20 73 68 61 72 65 64 2d 63 61 63 68  port shared-cach
1ac3c 65 20 6d 6f 64 65 20 61 6e 64 20 74 6f 20 62 65  e mode and to be
1ac3d 20 74 68 72 65 61 64 73 61 66 65 2c 0a 2a 2a 20   threadsafe,.** 
1ac3e 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 62 74  this routine obt
1ac3f 61 69 6e 73 20 74 68 65 20 6d 75 74 65 78 20 61  ains the mutex a
1ac40 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65  ssociated with e
1ac41 61 63 68 20 42 74 53 68 61 72 65 64 20 73 74 72  ach BtShared str
1ac42 75 63 74 75 72 65 0a 2a 2a 20 74 68 61 74 20 6d  ucture.** that m
1ac43 61 79 20 62 65 20 61 63 63 65 73 73 65 64 20 62  ay be accessed b
1ac44 79 20 74 68 65 20 56 4d 20 70 61 73 73 65 64 20  y the VM passed 
1ac45 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 20  as an argument. 
1ac46 49 6e 20 64 6f 69 6e 67 20 73 6f 20 69 74 0a 2a  In doing so it.*
1ac47 2a 20 73 65 74 73 20 74 68 65 20 42 74 53 68 61  * sets the BtSha
1ac48 72 65 64 2e 64 62 20 6d 65 6d 62 65 72 20 6f 66  red.db member of
1ac49 20 65 61 63 68 20 6f 66 20 74 68 65 20 42 74 53   each of the BtS
1ac4a 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 73  hared structures
1ac4b 2c 20 65 6e 73 75 72 69 6e 67 0a 2a 2a 20 74 68  , ensuring.** th
1ac4c 61 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 62  at the correct b
1ac4d 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c  usy-handler call
1ac4e 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20  back is invoked 
1ac4f 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a  if required..**.
1ac50 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20  ** If SQLite is 
1ac51 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 20 62  not threadsafe b
1ac52 75 74 20 64 6f 65 73 20 73 75 70 70 6f 72 74 20  ut does support 
1ac53 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64  shared-cache mod
1ac54 65 2c 20 74 68 65 6e 0a 2a 2a 20 73 71 6c 69 74  e, then.** sqlit
1ac55 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28  e3BtreeEnterAll(
1ac56 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20  ) is invoked to 
1ac57 73 65 74 20 74 68 65 20 42 74 53 68 61 72 65 64  set the BtShared
1ac58 2e 64 62 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a  .db variables.**
1ac59 20 6f 66 20 61 6c 6c 20 6f 66 20 42 74 53 68 61   of all of BtSha
1ac5a 72 65 64 20 73 74 72 75 63 74 75 72 65 73 20 61  red structures a
1ac5b 63 63 65 73 73 69 62 6c 65 20 76 69 61 20 74 68  ccessible via th
1ac5c 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
1ac5d 65 20 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64  e .** associated
1ac5e 20 77 69 74 68 20 74 68 65 20 56 4d 2e 20 4f 66   with the VM. Of
1ac5f 20 63 6f 75 72 73 65 20 6f 6e 6c 79 20 61 20 73   course only a s
1ac60 75 62 73 65 74 20 6f 66 20 74 68 65 73 65 20 73  ubset of these s
1ac61 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 77 69 6c  tructures.** wil
1ac62 6c 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79  l be accessed by
1ac63 20 74 68 65 20 56 4d 2c 20 61 6e 64 20 77 65 20   the VM, and we 
1ac64 63 6f 75 6c 64 20 75 73 65 20 56 64 62 65 2e 62  could use Vdbe.b
1ac65 74 72 65 65 4d 61 73 6b 20 74 6f 20 66 69 67 75  treeMask to figu
1ac66 72 65 0a 2a 2a 20 74 68 61 74 20 73 75 62 73 65  re.** that subse
1ac67 74 20 6f 75 74 2c 20 62 75 74 20 74 68 65 72 65  t out, but there
1ac68 20 69 73 20 6e 6f 20 61 64 76 61 6e 74 61 67 65   is no advantage
1ac69 20 74 6f 20 64 6f 69 6e 67 20 73 6f 2e 0a 2a 2a   to doing so..**
1ac6a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73  .** If SQLite is
1ac6b 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 20   not threadsafe 
1ac6c 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 73 75 70  and does not sup
1ac6d 70 6f 72 74 20 73 68 61 72 65 64 2d 63 61 63 68  port shared-cach
1ac6e 65 20 6d 6f 64 65 2c 20 74 68 69 73 0a 2a 2a 20  e mode, this.** 
1ac6f 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f  function is a no
1ac70 2d 6f 70 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  -op..*/.#ifndef 
1ac71 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
1ac72 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 45 5f  ED_CACHE.SQLITE_
1ac73 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1ac74 69 74 65 33 56 64 62 65 4d 75 74 65 78 41 72 72  ite3VdbeMutexArr
1ac75 61 79 45 6e 74 65 72 28 56 64 62 65 20 2a 70 29  ayEnter(Vdbe *p)
1ac76 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  {.#if SQLITE_THR
1ac77 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65  EADSAFE.  sqlite
1ac78 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79  3BtreeMutexArray
1ac79 45 6e 74 65 72 28 26 70 2d 3e 61 4d 75 74 65 78  Enter(&p->aMutex
1ac7a 29 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74  );.#else.  sqlit
1ac7b 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28  e3BtreeEnterAll(
1ac7c 70 2d 3e 64 62 29 3b 0a 23 65 6e 64 69 66 0a 7d  p->db);.#endif.}
1ac7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
1ac7e 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
1ac7f 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 74 72  called when a tr
1ac80 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 65 64  ansaction opened
1ac81 20 62 79 20 74 68 65 20 64 61 74 61 62 61 73 65   by the database
1ac82 20 0a 2a 2a 20 68 61 6e 64 6c 65 20 61 73 73 6f   .** handle asso
1ac83 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
1ac84 56 4d 20 70 61 73 73 65 64 20 61 73 20 61 6e 20  VM passed as an 
1ac85 61 72 67 75 6d 65 6e 74 20 69 73 20 61 62 6f 75  argument is abou
1ac86 74 20 74 6f 20 62 65 20 0a 2a 2a 20 63 6f 6d 6d  t to be .** comm
1ac87 69 74 74 65 64 2e 20 49 66 20 74 68 65 72 65 20  itted. If there 
1ac88 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20  are outstanding 
1ac89 64 65 66 65 72 72 65 64 20 66 6f 72 65 69 67 6e  deferred foreign
1ac8a 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 0a   key constraint.
1ac8b 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e 73 2c 20 72  ** violations, r
1ac8c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
1ac8d 4f 52 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53  OR. Otherwise, S
1ac8e 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20  QLITE_OK..**.** 
1ac8f 49 66 20 74 68 65 72 65 20 61 72 65 20 6f 75 74  If there are out
1ac90 73 74 61 6e 64 69 6e 67 20 46 4b 20 76 69 6f 6c  standing FK viol
1ac91 61 74 69 6f 6e 73 20 61 6e 64 20 74 68 69 73 20  ations and this 
1ac92 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
1ac93 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 52 52 4f   .** SQLITE_ERRO
1ac94 52 2c 20 73 65 74 20 74 68 65 20 72 65 73 75 6c  R, set the resul
1ac95 74 20 6f 66 20 74 68 65 20 56 4d 20 74 6f 20 53  t of the VM to S
1ac96 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
1ac97 20 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 61 6e   and write.** an
1ac98 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74   error message t
1ac99 6f 20 69 74 2e 20 54 68 65 6e 20 72 65 74 75 72  o it. Then retur
1ac9a 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a  n SQLITE_ERROR..
1ac9b 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
1ac9c 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b  E_OMIT_FOREIGN_K
1ac9d 45 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  EY.SQLITE_PRIVAT
1ac9e 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
1ac9f 65 43 68 65 63 6b 46 6b 28 56 64 62 65 20 2a 70  eCheckFk(Vdbe *p
1aca0 2c 20 69 6e 74 20 64 65 66 65 72 72 65 64 29 7b  , int deferred){
1aca1 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1aca2 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 28 64   p->db;.  if( (d
1aca3 65 66 65 72 72 65 64 20 26 26 20 64 62 2d 3e 6e  eferred && db->n
1aca4 44 65 66 65 72 72 65 64 43 6f 6e 73 3e 30 29 20  DeferredCons>0) 
1aca5 7c 7c 20 28 21 64 65 66 65 72 72 65 64 20 26 26  || (!deferred &&
1aca6 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 6e   p->nFkConstrain
1aca7 74 3e 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72  t>0) ){.    p->r
1aca8 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54  c = SQLITE_CONST
1aca9 52 41 49 4e 54 3b 0a 20 20 20 20 70 2d 3e 65 72  RAINT;.    p->er
1acaa 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f 41  rorAction = OE_A
1acab 62 6f 72 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  bort;.    sqlite
1acac 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
1acad 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 66 6f 72  ErrMsg, db, "for
1acae 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
1acaf 69 6e 74 20 66 61 69 6c 65 64 22 29 3b 0a 20 20  int failed");.  
1acb0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1acb1 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74  ERROR;.  }.  ret
1acb2 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1acb3 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
1acb4 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
1acb5 61 6c 6c 65 64 20 74 68 65 20 77 68 65 6e 20 61  alled the when a
1acb6 20 56 44 42 45 20 74 72 69 65 73 20 74 6f 20 68   VDBE tries to h
1acb7 61 6c 74 2e 20 20 49 66 20 74 68 65 20 56 44 42  alt.  If the VDB
1acb8 45 0a 2a 2a 20 68 61 73 20 6d 61 64 65 20 63 68  E.** has made ch
1acb9 61 6e 67 65 73 20 61 6e 64 20 69 73 20 69 6e 20  anges and is in 
1acba 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c  autocommit mode,
1acbb 20 74 68 65 6e 20 63 6f 6d 6d 69 74 20 74 68 6f   then commit tho
1acbc 73 65 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 20 20  se.** changes.  
1acbd 49 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20 69 73  If a rollback is
1acbe 20 6e 65 65 64 65 64 2c 20 74 68 65 6e 20 64 6f   needed, then do
1acbf 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a   the rollback..*
1acc0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1acc1 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61  e is the only wa
1acc2 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 73 74  y to move the st
1acc3 61 74 65 20 6f 66 20 61 20 56 4d 20 66 72 6f 6d  ate of a VM from
1acc4 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  .** SQLITE_MAGIC
1acc5 5f 52 55 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d  _RUN to SQLITE_M
1acc6 41 47 49 43 5f 48 41 4c 54 2e 20 20 49 74 20 69  AGIC_HALT.  It i
1acc7 73 20 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a  s harmless to.**
1acc8 20 63 61 6c 6c 20 74 68 69 73 20 6f 6e 20 61 20   call this on a 
1acc9 56 4d 20 74 68 61 74 20 69 73 20 69 6e 20 74 68  VM that is in th
1acca 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48  e SQLITE_MAGIC_H
1accb 41 4c 54 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a  ALT state..**.**
1accc 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   Return an error
1accd 20 63 6f 64 65 2e 20 20 49 66 20 74 68 65 20 63   code.  If the c
1acce 6f 6d 6d 69 74 20 63 6f 75 6c 64 20 6e 6f 74 20  ommit could not 
1accf 63 6f 6d 70 6c 65 74 65 20 62 65 63 61 75 73 65  complete because
1acd0 20 6f 66 0a 2a 2a 20 6c 6f 63 6b 20 63 6f 6e 74   of.** lock cont
1acd1 65 6e 74 69 6f 6e 2c 20 72 65 74 75 72 6e 20 53  ention, return S
1acd2 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 49 66 20  QLITE_BUSY.  If 
1acd3 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72  SQLITE_BUSY is r
1acd4 65 74 75 72 6e 65 64 2c 20 69 74 0a 2a 2a 20 6d  eturned, it.** m
1acd5 65 61 6e 73 20 74 68 65 20 63 6c 6f 73 65 20 64  eans the close d
1acd6 69 64 20 6e 6f 74 20 68 61 70 70 65 6e 20 61 6e  id not happen an
1acd7 64 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65  d needs to be re
1acd8 70 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  peated..*/.SQLIT
1acd9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1acda 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 56 64  lite3VdbeHalt(Vd
1acdb 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63  be *p){.  int rc
1acdc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1acdd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
1acde 64 20 74 6f 20 73 74 6f 72 65 20 74 72 61 6e 73  d to store trans
1acdf 69 65 6e 74 20 72 65 74 75 72 6e 20 63 6f 64 65  ient return code
1ace0 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  s */.  sqlite3 *
1ace1 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f  db = p->db;..  /
1ace2 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
1ace3 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f 67  contains the log
1ace4 69 63 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e  ic that determin
1ace5 65 73 20 69 66 20 61 20 73 74 61 74 65 6d 65 6e  es if a statemen
1ace6 74 20 6f 72 0a 20 20 2a 2a 20 74 72 61 6e 73 61  t or.  ** transa
1ace7 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 6f  ction will be co
1ace8 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65  mmitted or rolle
1ace9 64 20 62 61 63 6b 20 61 73 20 61 20 72 65 73 75  d back as a resu
1acea 6c 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 65  lt of the.  ** e
1aceb 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 69 73  xecution of this
1acec 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
1aced 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20  . .  **.  ** If 
1acee 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  any of the follo
1acef 77 69 6e 67 20 65 72 72 6f 72 73 20 6f 63 63 75  wing errors occu
1acf0 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  r:.  **.  **    
1acf1 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20   SQLITE_NOMEM.  
1acf2 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f  **     SQLITE_IO
1acf3 45 52 52 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c  ERR.  **     SQL
1acf4 49 54 45 5f 46 55 4c 4c 0a 20 20 2a 2a 20 20 20  ITE_FULL.  **   
1acf5 20 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55    SQLITE_INTERRU
1acf6 50 54 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  PT.  **.  ** The
1acf7 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63  n the internal c
1acf8 61 63 68 65 20 6d 69 67 68 74 20 68 61 76 65 20  ache might have 
1acf9 62 65 65 6e 20 6c 65 66 74 20 69 6e 20 61 6e 20  been left in an 
1acfa 69 6e 63 6f 6e 73 69 73 74 65 6e 74 0a 20 20 2a  inconsistent.  *
1acfb 2a 20 73 74 61 74 65 2e 20 20 57 65 20 6e 65 65  * state.  We nee
1acfc 64 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68  d to rollback th
1acfd 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  e statement tran
1acfe 73 61 63 74 69 6f 6e 2c 20 69 66 20 74 68 65 72  saction, if ther
1acff 65 20 69 73 0a 20 20 2a 2a 20 6f 6e 65 2c 20 6f  e is.  ** one, o
1ad00 72 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74  r the complete t
1ad01 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68  ransaction if th
1ad02 65 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d  ere is no statem
1ad03 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ent transaction.
1ad04 0a 20 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d 3e  .  */..  if( p->
1ad05 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1ad06 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20   ){.    p->rc = 
1ad07 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1ad08 7d 0a 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73  }.  closeAllCurs
1ad09 6f 72 73 28 70 29 3b 0a 20 20 69 66 28 20 70 2d  ors(p);.  if( p-
1ad0a 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47  >magic!=VDBE_MAG
1ad0b 49 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72 65  IC_RUN ){.    re
1ad0c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1ad0d 20 20 7d 0a 20 20 63 68 65 63 6b 41 63 74 69 76    }.  checkActiv
1ad0e 65 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 0a 20  eVdbeCnt(db);.. 
1ad0f 20 2f 2a 20 4e 6f 20 63 6f 6d 6d 69 74 20 6f 72   /* No commit or
1ad10 20 72 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 65 64   rollback needed
1ad11 20 69 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20   if the program 
1ad12 6e 65 76 65 72 20 73 74 61 72 74 65 64 20 2a 2f  never started */
1ad13 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20  .  if( p->pc>=0 
1ad14 29 7b 0a 20 20 20 20 69 6e 74 20 6d 72 63 3b 20  ){.    int mrc; 
1ad15 20 20 2f 2a 20 50 72 69 6d 61 72 79 20 65 72 72    /* Primary err
1ad16 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 70 2d 3e  or code from p->
1ad17 72 63 20 2a 2f 0a 20 20 20 20 69 6e 74 20 65 53  rc */.    int eS
1ad18 74 61 74 65 6d 65 6e 74 4f 70 20 3d 20 30 3b 0a  tatementOp = 0;.
1ad19 20 20 20 20 69 6e 74 20 69 73 53 70 65 63 69 61      int isSpecia
1ad1a 6c 45 72 72 6f 72 3b 20 20 20 20 20 20 20 20 20  lError;         
1ad1b 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 74 72 75     /* Set to tru
1ad1c 65 20 69 66 20 61 20 27 73 70 65 63 69 61 6c 27  e if a 'special'
1ad1d 20 65 72 72 6f 72 20 2a 2f 0a 0a 20 20 20 20 2f   error */..    /
1ad1e 2a 20 4c 6f 63 6b 20 61 6c 6c 20 62 74 72 65 65  * Lock all btree
1ad1f 73 20 75 73 65 64 20 62 79 20 74 68 65 20 73 74  s used by the st
1ad20 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 73  atement */.    s
1ad21 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78 41  qlite3VdbeMutexA
1ad22 72 72 61 79 45 6e 74 65 72 28 70 29 3b 0a 0a 20  rrayEnter(p);.. 
1ad23 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20     /* Check for 
1ad24 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69  one of the speci
1ad25 61 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20  al errors */.   
1ad26 20 6d 72 63 20 3d 20 70 2d 3e 72 63 20 26 20 30   mrc = p->rc & 0
1ad27 78 66 66 3b 0a 20 20 20 20 61 73 73 65 72 74 28  xff;.    assert(
1ad28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 49   p->rc!=SQLITE_I
1ad29 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 29 3b 20  OERR_BLOCKED ); 
1ad2a 20 2f 2a 20 54 68 69 73 20 65 72 72 6f 72 20 6e   /* This error n
1ad2b 6f 20 6c 6f 6e 67 65 72 20 65 78 69 73 74 73 20  o longer exists 
1ad2c 2a 2f 0a 20 20 20 20 69 73 53 70 65 63 69 61 6c  */.    isSpecial
1ad2d 45 72 72 6f 72 20 3d 20 6d 72 63 3d 3d 53 51 4c  Error = mrc==SQL
1ad2e 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d 72 63  ITE_NOMEM || mrc
1ad2f 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20  ==SQLITE_IOERR. 
1ad30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ad31 20 20 20 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49      || mrc==SQLI
1ad32 54 45 5f 49 4e 54 45 52 52 55 50 54 20 7c 7c 20  TE_INTERRUPT || 
1ad33 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c  mrc==SQLITE_FULL
1ad34 3b 0a 20 20 20 20 69 66 28 20 69 73 53 70 65 63  ;.    if( isSpec
1ad35 69 61 6c 45 72 72 6f 72 20 29 7b 0a 20 20 20 20  ialError ){.    
1ad36 20 20 2f 2a 20 49 66 20 74 68 65 20 71 75 65 72    /* If the quer
1ad37 79 20 77 61 73 20 72 65 61 64 2d 6f 6e 6c 79 2c  y was read-only,
1ad38 20 77 65 20 6e 65 65 64 20 64 6f 20 6e 6f 20 72   we need do no r
1ad39 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6c 6c 2e 20  ollback at all. 
1ad3a 4f 74 68 65 72 77 69 73 65 2c 0a 20 20 20 20 20  Otherwise,.     
1ad3b 20 2a 2a 20 70 72 6f 63 65 65 64 20 77 69 74 68   ** proceed with
1ad3c 20 74 68 65 20 73 70 65 63 69 61 6c 20 68 61 6e   the special han
1ad3d 64 6c 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a  dling..      */.
1ad3e 20 20 20 20 20 20 69 66 28 20 21 70 2d 3e 72 65        if( !p->re
1ad3f 61 64 4f 6e 6c 79 20 7c 7c 20 6d 72 63 21 3d 53  adOnly || mrc!=S
1ad40 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20  QLITE_INTERRUPT 
1ad41 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28  ){.        if( (
1ad42 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45  mrc==SQLITE_NOME
1ad43 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45  M || mrc==SQLITE
1ad44 5f 46 55 4c 4c 29 20 26 26 20 70 2d 3e 75 73 65  _FULL) && p->use
1ad45 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a  sStmtJournal ){.
1ad46 20 20 20 20 20 20 20 20 20 20 65 53 74 61 74 65            eState
1ad47 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49  mentOp = SAVEPOI
1ad48 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20  NT_ROLLBACK;.   
1ad49 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1ad4a 20 20 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20        /* We are 
1ad4b 66 6f 72 63 65 64 20 74 6f 20 72 6f 6c 6c 20 62  forced to roll b
1ad4c 61 63 6b 20 74 68 65 20 61 63 74 69 76 65 20 74  ack the active t
1ad4d 72 61 6e 73 61 63 74 69 6f 6e 2e 20 42 65 66 6f  ransaction. Befo
1ad4e 72 65 20 64 6f 69 6e 67 0a 20 20 20 20 20 20 20  re doing.       
1ad4f 20 20 20 2a 2a 20 73 6f 2c 20 61 62 6f 72 74 20     ** so, abort 
1ad50 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74 65 6d  any other statem
1ad51 65 6e 74 73 20 74 68 69 73 20 68 61 6e 64 6c 65  ents this handle
1ad52 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20 61   currently has a
1ad53 63 74 69 76 65 2e 0a 20 20 20 20 20 20 20 20 20  ctive..         
1ad54 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 6e   */.          in
1ad55 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73 4f  validateCursorsO
1ad56 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73 28  nModifiedBtrees(
1ad57 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  db);.          s
1ad58 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c  qlite3RollbackAl
1ad59 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20  l(db);.         
1ad5a 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76   sqlite3CloseSav
1ad5b 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 20  epoints(db);.   
1ad5c 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43         db->autoC
1ad5d 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20  ommit = 1;.     
1ad5e 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1ad5f 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b   }..    /* Check
1ad60 20 66 6f 72 20 69 6d 6d 65 64 69 61 74 65 20 66   for immediate f
1ad61 6f 72 65 69 67 6e 20 6b 65 79 20 76 69 6f 6c 61  oreign key viola
1ad62 74 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20 20 69 66  tions. */.    if
1ad63 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f  ( p->rc==SQLITE_
1ad64 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  OK ){.      sqli
1ad65 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28 70  te3VdbeCheckFk(p
1ad66 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20  , 0);.    }.  . 
1ad67 20 20 20 2f 2a 20 49 66 20 74 68 65 20 61 75 74     /* If the aut
1ad68 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73  o-commit flag is
1ad69 20 73 65 74 20 61 6e 64 20 74 68 69 73 20 69 73   set and this is
1ad6a 20 74 68 65 20 6f 6e 6c 79 20 61 63 74 69 76 65   the only active
1ad6b 20 77 72 69 74 65 72 20 0a 20 20 20 20 2a 2a 20   writer .    ** 
1ad6c 56 4d 2c 20 74 68 65 6e 20 77 65 20 64 6f 20 65  VM, then we do e
1ad6d 69 74 68 65 72 20 61 20 63 6f 6d 6d 69 74 20 6f  ither a commit o
1ad6e 72 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 74 68  r rollback of th
1ad6f 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61  e current transa
1ad70 63 74 69 6f 6e 2e 20 0a 20 20 20 20 2a 2a 0a 20  ction. .    **. 
1ad71 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 69 73     ** Note: This
1ad72 20 62 6c 6f 63 6b 20 61 6c 73 6f 20 72 75 6e 73   block also runs
1ad73 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 73   if one of the s
1ad74 70 65 63 69 61 6c 20 65 72 72 6f 72 73 20 68 61  pecial errors ha
1ad75 6e 64 6c 65 64 20 0a 20 20 20 20 2a 2a 20 61 62  ndled .    ** ab
1ad76 6f 76 65 20 68 61 73 20 6f 63 63 75 72 72 65 64  ove has occurred
1ad77 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  . .    */.    if
1ad78 28 20 21 73 71 6c 69 74 65 33 56 74 61 62 49 6e  ( !sqlite3VtabIn
1ad79 53 79 6e 63 28 64 62 29 20 0a 20 20 20 20 20 26  Sync(db) .     &
1ad7a 26 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74  & db->autoCommit
1ad7b 20 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 77 72   .     && db->wr
1ad7c 69 74 65 56 64 62 65 43 6e 74 3d 3d 28 70 2d 3e  iteVdbeCnt==(p->
1ad7d 72 65 61 64 4f 6e 6c 79 3d 3d 30 29 20 0a 20 20  readOnly==0) .  
1ad7e 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70    ){.      if( p
1ad7f 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc==SQLITE_OK 
1ad80 7c 7c 20 28 70 2d 3e 65 72 72 6f 72 41 63 74 69  || (p->errorActi
1ad81 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26 26 20 21  on==OE_Fail && !
1ad82 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72 29 20  isSpecialError) 
1ad83 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  ){.        if( s
1ad84 71 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46  qlite3VdbeCheckF
1ad85 6b 28 70 2c 20 31 29 20 29 7b 0a 20 20 20 20 20  k(p, 1) ){.     
1ad86 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1ad87 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65  eMutexArrayLeave
1ad88 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20  (&p->aMutex);.  
1ad89 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
1ad8a 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
1ad8b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f       }.        /
1ad8c 2a 20 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69  * The auto-commi
1ad8d 74 20 66 6c 61 67 20 69 73 20 74 72 75 65 2c 20  t flag is true, 
1ad8e 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d  the vdbe program
1ad8f 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 20   was successful 
1ad90 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 68  .        ** or h
1ad91 69 74 20 61 6e 20 27 4f 52 20 46 41 49 4c 27 20  it an 'OR FAIL' 
1ad92 63 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64 20 74  constraint and t
1ad93 68 65 72 65 20 61 72 65 20 6e 6f 20 64 65 66 65  here are no defe
1ad94 72 72 65 64 20 66 6f 72 65 69 67 6e 0a 20 20 20  rred foreign.   
1ad95 20 20 20 20 20 2a 2a 20 6b 65 79 20 63 6f 6e 73       ** key cons
1ad96 74 72 61 69 6e 74 73 20 74 6f 20 68 6f 6c 64 20  traints to hold 
1ad97 75 70 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  up the transacti
1ad98 6f 6e 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 61  on. This means a
1ad99 20 63 6f 6d 6d 69 74 20 0a 20 20 20 20 20 20 20   commit .       
1ad9a 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 2e   ** is required.
1ad9b 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20    */.        rc 
1ad9c 3d 20 76 64 62 65 43 6f 6d 6d 69 74 28 64 62 2c  = vdbeCommit(db,
1ad9d 20 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   p);.        if(
1ad9e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59   rc==SQLITE_BUSY
1ad9f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
1ada0 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41  lite3BtreeMutexA
1ada1 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d  rrayLeave(&p->aM
1ada2 75 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 20  utex);.         
1ada3 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
1ada4 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  USY;.        }el
1ada5 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  se if( rc!=SQLIT
1ada6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1ada7 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20    p->rc = rc;.  
1ada8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52          sqlite3R
1ada9 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a  ollbackAll(db);.
1adaa 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
1adab 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 44 65           db->nDe
1adac 66 65 72 72 65 64 43 6f 6e 73 20 3d 20 30 3b 0a  ferredCons = 0;.
1adad 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1adae 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43  3CommitInternalC
1adaf 68 61 6e 67 65 73 28 64 62 29 3b 0a 20 20 20 20  hanges(db);.    
1adb0 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
1adb1 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
1adb2 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62  e3RollbackAll(db
1adb3 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1adb4 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20   db->nStatement 
1adb5 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  = 0;.    }else i
1adb6 66 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 3d  f( eStatementOp=
1adb7 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  =0 ){.      if( 
1adb8 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  p->rc==SQLITE_OK
1adb9 20 7c 7c 20 70 2d 3e 65 72 72 6f 72 41 63 74 69   || p->errorActi
1adba 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29 7b 0a 20  on==OE_Fail ){. 
1adbb 20 20 20 20 20 20 20 65 53 74 61 74 65 6d 65 6e         eStatemen
1adbc 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 5f  tOp = SAVEPOINT_
1adbd 52 45 4c 45 41 53 45 3b 0a 20 20 20 20 20 20 7d  RELEASE;.      }
1adbe 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 72 72 6f  else if( p->erro
1adbf 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41 62 6f 72  rAction==OE_Abor
1adc0 74 20 29 7b 0a 20 20 20 20 20 20 20 20 65 53 74  t ){.        eSt
1adc1 61 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45  atementOp = SAVE
1adc2 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a  POINT_ROLLBACK;.
1adc3 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1adc4 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 43       invalidateC
1adc5 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64  ursorsOnModified
1adc6 42 74 72 65 65 73 28 64 62 29 3b 0a 20 20 20 20  Btrees(db);.    
1adc7 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62      sqlite3Rollb
1adc8 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20  ackAll(db);.    
1adc9 20 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65      sqlite3Close
1adca 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a  Savepoints(db);.
1adcb 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f          db->auto
1adcc 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20  Commit = 1;.    
1adcd 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20    }.    }.  .   
1adce 20 2f 2a 20 49 66 20 65 53 74 61 74 65 6d 65 6e   /* If eStatemen
1adcf 74 4f 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  tOp is non-zero,
1add0 20 74 68 65 6e 20 61 20 73 74 61 74 65 6d 65 6e   then a statemen
1add1 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65  t transaction ne
1add2 65 64 73 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65  eds to.    ** be
1add3 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f   committed or ro
1add4 6c 6c 65 64 20 62 61 63 6b 2e 20 43 61 6c 6c 20  lled back. Call 
1add5 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65  sqlite3VdbeClose
1add6 53 74 61 74 65 6d 65 6e 74 28 29 20 74 6f 0a 20  Statement() to. 
1add7 20 20 20 2a 2a 20 64 6f 20 73 6f 2e 20 49 66 20     ** do so. If 
1add8 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 72  this operation r
1add9 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2c  eturns an error,
1adda 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74   and the current
1addb 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 2a   statement.    *
1addc 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  * error code is 
1addd 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c  SQLITE_OK or SQL
1adde 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20  ITE_CONSTRAINT, 
1addf 74 68 65 6e 20 73 65 74 20 74 68 65 20 65 72 72  then set the err
1ade0 6f 72 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20 74  or.    ** code t
1ade1 6f 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 2e  o the new value.
1ade2 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1ade3 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 29 7b 0a  eStatementOp ){.
1ade4 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1ade5 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65  e3VdbeCloseState
1ade6 6d 65 6e 74 28 70 2c 20 65 53 74 61 74 65 6d 65  ment(p, eStateme
1ade7 6e 74 4f 70 29 3b 0a 20 20 20 20 20 20 69 66 28  ntOp);.      if(
1ade8 20 72 63 20 26 26 20 28 70 2d 3e 72 63 3d 3d 53   rc && (p->rc==S
1ade9 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72  QLITE_OK || p->r
1adea 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  c==SQLITE_CONSTR
1adeb 41 49 4e 54 29 20 29 7b 0a 20 20 20 20 20 20 20  AINT) ){.       
1adec 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20   p->rc = rc;.   
1aded 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1adee 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73  ee(db, p->zErrMs
1adef 67 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a  g);.        p->z
1adf0 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20  ErrMsg = 0;.    
1adf1 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20    }.    }.  .   
1adf2 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73 20   /* If this was 
1adf3 61 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54  an INSERT, UPDAT
1adf4 45 20 6f 72 20 44 45 4c 45 54 45 20 61 6e 64 20  E or DELETE and 
1adf5 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61  no statement tra
1adf6 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20  nsaction.    ** 
1adf7 68 61 73 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20  has been rolled 
1adf8 62 61 63 6b 2c 20 75 70 64 61 74 65 20 74 68 65  back, update the
1adf9 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1adfa 74 69 6f 6e 20 63 68 61 6e 67 65 2d 63 6f 75 6e  tion change-coun
1adfb 74 65 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20  ter. .    */.   
1adfc 20 69 66 28 20 70 2d 3e 63 68 61 6e 67 65 43 6e   if( p->changeCn
1adfd 74 4f 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28  tOn ){.      if(
1adfe 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 21 3d 53   eStatementOp!=S
1adff 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
1ae00 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  K ){.        sql
1ae01 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67  ite3VdbeSetChang
1ae02 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67  es(db, p->nChang
1ae03 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  e);.      }else{
1ae04 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1ae05 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 64  VdbeSetChanges(d
1ae06 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  b, 0);.      }. 
1ae07 20 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20       p->nChange 
1ae08 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20  = 0;.    }.  .  
1ae09 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f 72    /* Rollback or
1ae0a 20 63 6f 6d 6d 69 74 20 61 6e 79 20 73 63 68 65   commit any sche
1ae0b 6d 61 20 63 68 61 6e 67 65 73 20 74 68 61 74 20  ma changes that 
1ae0c 6f 63 63 75 72 72 65 64 2e 20 2a 2f 0a 20 20 20  occurred. */.   
1ae0d 20 69 66 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49   if( p->rc!=SQLI
1ae0e 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 66 6c 61  TE_OK && db->fla
1ae0f 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  gs&SQLITE_Intern
1ae10 43 68 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 20  Changes ){.     
1ae11 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
1ae12 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
1ae13 30 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 66 6c  0);.      db->fl
1ae14 61 67 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73  ags = (db->flags
1ae15 20 7c 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e   | SQLITE_Intern
1ae16 43 68 61 6e 67 65 73 29 3b 0a 20 20 20 20 7d 0a  Changes);.    }.
1ae17 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20  .    /* Release 
1ae18 74 68 65 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20  the locks */.   
1ae19 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74   sqlite3BtreeMut
1ae1a 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d  exArrayLeave(&p-
1ae1b 3e 61 4d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20  >aMutex);.  }.. 
1ae1c 20 2f 2a 20 57 65 20 68 61 76 65 20 73 75 63 63   /* We have succ
1ae1d 65 73 73 66 75 6c 6c 79 20 68 61 6c 74 65 64 20  essfully halted 
1ae1e 61 6e 64 20 63 6c 6f 73 65 64 20 74 68 65 20 56  and closed the V
1ae1f 4d 2e 20 20 52 65 63 6f 72 64 20 74 68 69 73 20  M.  Record this 
1ae20 66 61 63 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70  fact. */.  if( p
1ae21 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 64  ->pc>=0 ){.    d
1ae22 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74  b->activeVdbeCnt
1ae23 2d 2d 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e  --;.    if( !p->
1ae24 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20  readOnly ){.    
1ae25 20 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43    db->writeVdbeC
1ae26 6e 74 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  nt--;.    }.    
1ae27 61 73 73 65 72 74 28 20 64 62 2d 3e 61 63 74 69  assert( db->acti
1ae28 76 65 56 64 62 65 43 6e 74 3e 3d 64 62 2d 3e 77  veVdbeCnt>=db->w
1ae29 72 69 74 65 56 64 62 65 43 6e 74 20 29 3b 0a 20  riteVdbeCnt );. 
1ae2a 20 7d 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20   }.  p->magic = 
1ae2b 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 3b  VDBE_MAGIC_HALT;
1ae2c 0a 20 20 63 68 65 63 6b 41 63 74 69 76 65 56 64  .  checkActiveVd
1ae2d 62 65 43 6e 74 28 64 62 29 3b 0a 20 20 69 66 28  beCnt(db);.  if(
1ae2e 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   p->db->mallocFa
1ae2f 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72  iled ){.    p->r
1ae30 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
1ae31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
1ae32 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66  he auto-commit f
1ae33 6c 61 67 20 69 73 20 73 65 74 20 74 6f 20 74 72  lag is set to tr
1ae34 75 65 2c 20 74 68 65 6e 20 61 6e 79 20 6c 6f 63  ue, then any loc
1ae35 6b 73 20 74 68 61 74 20 77 65 72 65 20 68 65 6c  ks that were hel
1ae36 64 0a 20 20 2a 2a 20 62 79 20 63 6f 6e 6e 65 63  d.  ** by connec
1ae37 74 69 6f 6e 20 64 62 20 68 61 76 65 20 6e 6f 77  tion db have now
1ae38 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 20   been released. 
1ae39 43 61 6c 6c 20 73 71 6c 69 74 65 33 43 6f 6e 6e  Call sqlite3Conn
1ae3a 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29  ectionUnlocked()
1ae3b 20 0a 20 20 2a 2a 20 74 6f 20 69 6e 76 6f 6b 65   .  ** to invoke
1ae3c 20 61 6e 79 20 72 65 71 75 69 72 65 64 20 75 6e   any required un
1ae3d 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c  lock-notify call
1ae3e 62 61 63 6b 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  backs..  */.  if
1ae3f 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74  ( db->autoCommit
1ae40 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43   ){.    sqlite3C
1ae41 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65  onnectionUnlocke
1ae42 64 28 64 62 29 3b 0a 20 20 7d 0a 0a 20 20 61 73  d(db);.  }..  as
1ae43 73 65 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65  sert( db->active
1ae44 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20 64 62 2d  VdbeCnt>0 || db-
1ae45 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c  >autoCommit==0 |
1ae46 7c 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74  | db->nStatement
1ae47 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ==0 );.  return 
1ae48 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
1ae49 2a 0a 2a 2a 20 45 61 63 68 20 56 44 42 45 20 68  *.** Each VDBE h
1ae4a 6f 6c 64 73 20 74 68 65 20 72 65 73 75 6c 74 20  olds the result 
1ae4b 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  of the most rece
1ae4c 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  nt sqlite3_step(
1ae4d 29 20 63 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e  ) call.** in p->
1ae4e 72 63 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  rc.  This routin
1ae4f 65 20 73 65 74 73 20 74 68 61 74 20 72 65 73 75  e sets that resu
1ae50 6c 74 20 62 61 63 6b 20 74 6f 20 53 51 4c 49 54  lt back to SQLIT
1ae51 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  E_OK..*/.SQLITE_
1ae52 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1ae53 69 74 65 33 56 64 62 65 52 65 73 65 74 53 74 65  ite3VdbeResetSte
1ae54 70 52 65 73 75 6c 74 28 56 64 62 65 20 2a 70 29  pResult(Vdbe *p)
1ae55 7b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49  {.  p->rc = SQLI
1ae56 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
1ae57 43 6c 65 61 6e 20 75 70 20 61 20 56 44 42 45 20  Clean up a VDBE 
1ae58 61 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e 20  after execution 
1ae59 62 75 74 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74  but do not delet
1ae5a 65 20 74 68 65 20 56 44 42 45 20 6a 75 73 74 20  e the VDBE just 
1ae5b 79 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 61 6e  yet..** Write an
1ae5c 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73  y error messages
1ae5d 20 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e   into *pzErrMsg.
1ae5e 20 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 73    Return the res
1ae5f 75 6c 74 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  ult code..**.** 
1ae60 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69  After this routi
1ae61 6e 65 20 69 73 20 72 75 6e 2c 20 74 68 65 20 56  ne is run, the V
1ae62 44 42 45 20 73 68 6f 75 6c 64 20 62 65 20 72 65  DBE should be re
1ae63 61 64 79 20 74 6f 20 62 65 20 65 78 65 63 75 74  ady to be execut
1ae64 65 64 0a 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a  ed.** again..**.
1ae65 2a 2a 20 54 6f 20 6c 6f 6f 6b 20 61 74 20 69 74  ** To look at it
1ae66 20 61 6e 6f 74 68 65 72 20 77 61 79 2c 20 74 68   another way, th
1ae67 69 73 20 72 6f 75 74 69 6e 65 20 72 65 73 65 74  is routine reset
1ae68 73 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74  s the state of t
1ae69 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61  he.** virtual ma
1ae6a 63 68 69 6e 65 20 66 72 6f 6d 20 56 44 42 45 5f  chine from VDBE_
1ae6b 4d 41 47 49 43 5f 52 55 4e 20 6f 72 20 56 44 42  MAGIC_RUN or VDB
1ae6c 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 62 61 63  E_MAGIC_HALT bac
1ae6d 6b 20 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47  k to.** VDBE_MAG
1ae6e 49 43 5f 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49  IC_INIT..*/.SQLI
1ae6f 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1ae70 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28  qlite3VdbeReset(
1ae71 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69  Vdbe *p){.  sqli
1ae72 74 65 33 20 2a 64 62 3b 0a 20 20 64 62 20 3d 20  te3 *db;.  db = 
1ae73 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 49 66 20  p->db;..  /* If 
1ae74 74 68 65 20 56 4d 20 64 69 64 20 6e 6f 74 20 72  the VM did not r
1ae75 75 6e 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e  un to completion
1ae76 20 6f 72 20 69 66 20 69 74 20 65 6e 63 6f 75 6e   or if it encoun
1ae77 74 65 72 65 64 20 61 6e 0a 20 20 2a 2a 20 65 72  tered an.  ** er
1ae78 72 6f 72 2c 20 74 68 65 6e 20 69 74 20 6d 69 67  ror, then it mig
1ae79 68 74 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e  ht not have been
1ae7a 20 68 61 6c 74 65 64 20 70 72 6f 70 65 72 6c 79   halted properly
1ae7b 2e 20 20 53 6f 20 68 61 6c 74 0a 20 20 2a 2a 20  .  So halt.  ** 
1ae7c 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 28  it now..  */.  (
1ae7d 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
1ae7e 74 79 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c 69  tyOn(db);.  sqli
1ae7f 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a  te3VdbeHalt(p);.
1ae80 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
1ae81 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a 20  afetyOff(db);.. 
1ae82 20 2f 2a 20 49 66 20 74 68 65 20 56 44 42 45 20   /* If the VDBE 
1ae83 68 61 73 20 62 65 20 72 75 6e 20 65 76 65 6e 20  has be run even 
1ae84 70 61 72 74 69 61 6c 6c 79 2c 20 74 68 65 6e 20  partially, then 
1ae85 74 72 61 6e 73 66 65 72 20 74 68 65 20 65 72 72  transfer the err
1ae86 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 6e 64  or code.  ** and
1ae87 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66   error message f
1ae88 72 6f 6d 20 74 68 65 20 56 44 42 45 20 69 6e 74  rom the VDBE int
1ae89 6f 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  o the main datab
1ae8a 61 73 65 20 73 74 72 75 63 74 75 72 65 2e 20 20  ase structure.  
1ae8b 42 75 74 0a 20 20 2a 2a 20 69 66 20 74 68 65 20  But.  ** if the 
1ae8c 56 44 42 45 20 68 61 73 20 6a 75 73 74 20 62 65  VDBE has just be
1ae8d 65 6e 20 73 65 74 20 74 6f 20 72 75 6e 20 62 75  en set to run bu
1ae8e 74 20 68 61 73 20 6e 6f 74 20 61 63 74 75 61 6c  t has not actual
1ae8f 6c 79 20 65 78 65 63 75 74 65 64 20 61 6e 79 0a  ly executed any.
1ae90 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e    ** instruction
1ae91 73 20 79 65 74 2c 20 6c 65 61 76 65 20 74 68 65  s yet, leave the
1ae92 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 65   main database e
1ae93 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  rror information
1ae94 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f   unchanged..  */
1ae95 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20  .  if( p->pc>=0 
1ae96 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a 45  ){.    if( p->zE
1ae97 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 73  rrMsg ){.      s
1ae98 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
1ae99 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20  nMalloc();.     
1ae9a 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74   sqlite3ValueSet
1ae9b 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 2d 31 2c  Str(db->pErr,-1,
1ae9c 70 2d 3e 7a 45 72 72 4d 73 67 2c 53 51 4c 49 54  p->zErrMsg,SQLIT
1ae9d 45 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f 54 52  E_UTF8,SQLITE_TR
1ae9e 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20  ANSIENT);.      
1ae9f 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
1aea0 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20  Malloc();.      
1aea1 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d  db->errCode = p-
1aea2 3e 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  >rc;.      sqlit
1aea3 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
1aea4 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20  zErrMsg);.      
1aea5 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a  p->zErrMsg = 0;.
1aea6 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d      }else if( p-
1aea7 3e 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  >rc ){.      sql
1aea8 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70 2d  ite3Error(db, p-
1aea9 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c  >rc, 0);.    }el
1aeaa 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
1aeab 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
1aeac 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  E_OK, 0);.    }.
1aead 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72    }else if( p->r
1aeae 63 20 26 26 20 70 2d 3e 65 78 70 69 72 65 64 20  c && p->expired 
1aeaf 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 78  ){.    /* The ex
1aeb0 70 69 72 65 64 20 66 6c 61 67 20 77 61 73 20 73  pired flag was s
1aeb1 65 74 20 6f 6e 20 74 68 65 20 56 44 42 45 20 62  et on the VDBE b
1aeb2 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 20  efore the first 
1aeb3 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 73  call.    ** to s
1aeb4 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 46  qlite3_step(). F
1aeb5 6f 72 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 28  or consistency (
1aeb6 73 69 6e 63 65 20 73 71 6c 69 74 65 33 5f 73 74  since sqlite3_st
1aeb7 65 70 28 29 20 77 61 73 0a 20 20 20 20 2a 2a 20  ep() was.    ** 
1aeb8 63 61 6c 6c 65 64 29 2c 20 73 65 74 20 74 68 65  called), set the
1aeb9 20 64 61 74 61 62 61 73 65 20 65 72 72 6f 72 20   database error 
1aeba 69 6e 20 74 68 69 73 20 63 61 73 65 20 61 73 20  in this case as 
1aebb 77 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  well..    */.   
1aebc 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62   sqlite3Error(db
1aebd 2c 20 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20  , p->rc, 0);.   
1aebe 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74   sqlite3ValueSet
1aebf 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31  Str(db->pErr, -1
1aec0 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 53 51  , p->zErrMsg, SQ
1aec1 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
1aec2 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
1aec3 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1aec4 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b  db, p->zErrMsg);
1aec5 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20  .    p->zErrMsg 
1aec6 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52  = 0;.  }..  /* R
1aec7 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72  eclaim all memor
1aec8 79 20 75 73 65 64 20 62 79 20 74 68 65 20 56 44  y used by the VD
1aec9 42 45 0a 20 20 2a 2f 0a 20 20 43 6c 65 61 6e 75  BE.  */.  Cleanu
1aeca 70 28 70 29 3b 0a 0a 20 20 2f 2a 20 53 61 76 65  p(p);..  /* Save
1aecb 20 70 72 6f 66 69 6c 69 6e 67 20 69 6e 66 6f 72   profiling infor
1aecc 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 69 73  mation from this
1aecd 20 56 44 42 45 20 72 75 6e 2e 0a 20 20 2a 2f 0a   VDBE run..  */.
1aece 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46  #ifdef VDBE_PROF
1aecf 49 4c 45 0a 20 20 7b 0a 20 20 20 20 46 49 4c 45  ILE.  {.    FILE
1aed0 20 2a 6f 75 74 20 3d 20 66 6f 70 65 6e 28 22 76   *out = fopen("v
1aed1 64 62 65 5f 70 72 6f 66 69 6c 65 2e 6f 75 74 22  dbe_profile.out"
1aed2 2c 20 22 61 22 29 3b 0a 20 20 20 20 69 66 28 20  , "a");.    if( 
1aed3 6f 75 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  out ){.      int
1aed4 20 69 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74   i;.      fprint
1aed5 66 28 6f 75 74 2c 20 22 2d 2d 2d 2d 20 22 29 3b  f(out, "---- ");
1aed6 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
1aed7 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a  i<p->nOp; i++){.
1aed8 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28          fprintf(
1aed9 6f 75 74 2c 20 22 25 30 32 78 22 2c 20 70 2d 3e  out, "%02x", p->
1aeda 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b 0a  aOp[i].opcode);.
1aedb 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 70        }.      fp
1aedc 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29  rintf(out, "\n")
1aedd 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
1aede 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b   i<p->nOp; i++){
1aedf 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66  .        fprintf
1aee0 28 6f 75 74 2c 20 22 25 36 64 20 25 31 30 6c 6c  (out, "%6d %10ll
1aee1 64 20 25 38 6c 6c 64 20 22 2c 0a 20 20 20 20 20  d %8lld ",.     
1aee2 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e        p->aOp[i].
1aee3 63 6e 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  cnt,.           
1aee4 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73  p->aOp[i].cycles
1aee5 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e  ,.           p->
1aee6 61 4f 70 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20 70  aOp[i].cnt>0 ? p
1aee7 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2f  ->aOp[i].cycles/
1aee8 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20  p->aOp[i].cnt : 
1aee9 30 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20  0.        );.   
1aeea 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1aeeb 50 72 69 6e 74 4f 70 28 6f 75 74 2c 20 69 2c 20  PrintOp(out, i, 
1aeec 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20  &p->aOp[i]);.   
1aeed 20 20 20 7d 0a 20 20 20 20 20 20 66 63 6c 6f 73     }.      fclos
1aeee 65 28 6f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20  e(out);.    }.  
1aeef 7d 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e 6d 61  }.#endif.  p->ma
1aef0 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43  gic = VDBE_MAGIC
1aef1 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e 20  _INIT;.  return 
1aef2 70 2d 3e 72 63 20 26 20 64 62 2d 3e 65 72 72 4d  p->rc & db->errM
1aef3 61 73 6b 3b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43  ask;.}. ./*.** C
1aef4 6c 65 61 6e 20 75 70 20 61 6e 64 20 64 65 6c 65  lean up and dele
1aef5 74 65 20 61 20 56 44 42 45 20 61 66 74 65 72 20  te a VDBE after 
1aef6 65 78 65 63 75 74 69 6f 6e 2e 20 20 52 65 74 75  execution.  Retu
1aef7 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68  rn an integer wh
1aef8 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 72 65  ich is.** the re
1aef9 73 75 6c 74 20 63 6f 64 65 2e 20 20 57 72 69 74  sult code.  Writ
1aefa 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73  e any error mess
1aefb 61 67 65 20 74 65 78 74 20 69 6e 74 6f 20 2a 70  age text into *p
1aefc 7a 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49  zErrMsg..*/.SQLI
1aefd 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1aefe 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69  qlite3VdbeFinali
1aeff 7a 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69  ze(Vdbe *p){.  i
1af00 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1af01 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69  K;.  if( p->magi
1af02 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  c==VDBE_MAGIC_RU
1af03 4e 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56  N || p->magic==V
1af04 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 29  DBE_MAGIC_HALT )
1af05 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1af06 65 33 56 64 62 65 52 65 73 65 74 28 70 29 3b 0a  e3VdbeReset(p);.
1af07 20 20 20 20 61 73 73 65 72 74 28 20 28 72 63 20      assert( (rc 
1af08 26 20 70 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b  & p->db->errMask
1af09 29 3d 3d 72 63 20 29 3b 0a 20 20 7d 0a 20 20 73  )==rc );.  }.  s
1af0a 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65  qlite3VdbeDelete
1af0b 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
1af0c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  ;.}../*.** Call 
1af0d 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66  the destructor f
1af0e 6f 72 20 65 61 63 68 20 61 75 78 64 61 74 61 20  or each auxdata 
1af0f 65 6e 74 72 79 20 69 6e 20 70 56 64 62 65 46 75  entry in pVdbeFu
1af10 6e 63 20 66 6f 72 20 77 68 69 63 68 0a 2a 2a 20  nc for which.** 
1af11 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
1af12 67 20 62 69 74 20 69 6e 20 6d 61 73 6b 20 69 73  g bit in mask is
1af13 20 63 6c 65 61 72 2e 20 20 41 75 78 64 61 74 61   clear.  Auxdata
1af14 20 65 6e 74 72 69 65 73 20 62 65 79 6f 6e 64 20   entries beyond 
1af15 33 31 0a 2a 2a 20 61 72 65 20 61 6c 77 61 79 73  31.** are always
1af16 20 64 65 73 74 72 6f 79 65 64 2e 20 20 54 6f 20   destroyed.  To 
1af17 64 65 73 74 72 6f 79 20 61 6c 6c 20 61 75 78 64  destroy all auxd
1af18 61 74 61 20 65 6e 74 72 69 65 73 2c 20 63 61 6c  ata entries, cal
1af19 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e  l this.** routin
1af1a 65 20 77 69 74 68 20 6d 61 73 6b 3d 3d 30 2e 0a  e with mask==0..
1af1b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1af1c 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1af1d 62 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28  beDeleteAuxData(
1af1e 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46  VdbeFunc *pVdbeF
1af1f 75 6e 63 2c 20 69 6e 74 20 6d 61 73 6b 29 7b 0a  unc, int mask){.
1af20 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69    int i;.  for(i
1af21 3d 30 3b 20 69 3c 70 56 64 62 65 46 75 6e 63 2d  =0; i<pVdbeFunc-
1af22 3e 6e 41 75 78 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nAux; i++){.   
1af23 20 73 74 72 75 63 74 20 41 75 78 44 61 74 61 20   struct AuxData 
1af24 2a 70 41 75 78 20 3d 20 26 70 56 64 62 65 46 75  *pAux = &pVdbeFu
1af25 6e 63 2d 3e 61 70 41 75 78 5b 69 5d 3b 0a 20 20  nc->apAux[i];.  
1af26 20 20 69 66 28 20 28 69 3e 33 31 20 7c 7c 20 21    if( (i>31 || !
1af27 28 6d 61 73 6b 26 28 28 28 75 33 32 29 31 29 3c  (mask&(((u32)1)<
1af28 3c 69 29 29 29 20 26 26 20 70 41 75 78 2d 3e 70  <i))) && pAux->p
1af29 41 75 78 20 29 7b 0a 20 20 20 20 20 20 69 66 28  Aux ){.      if(
1af2a 20 70 41 75 78 2d 3e 78 44 65 6c 65 74 65 20 29   pAux->xDelete )
1af2b 7b 0a 20 20 20 20 20 20 20 20 70 41 75 78 2d 3e  {.        pAux->
1af2c 78 44 65 6c 65 74 65 28 70 41 75 78 2d 3e 70 41  xDelete(pAux->pA
1af2d 75 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ux);.      }.   
1af2e 20 20 20 70 41 75 78 2d 3e 70 41 75 78 20 3d 20     pAux->pAux = 
1af2f 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  0;.    }.  }.}..
1af30 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20  /*.** Delete an 
1af31 65 6e 74 69 72 65 20 56 44 42 45 2e 0a 2a 2f 0a  entire VDBE..*/.
1af32 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1af33 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44  oid sqlite3VdbeD
1af34 65 6c 65 74 65 28 56 64 62 65 20 2a 70 29 7b 0a  elete(Vdbe *p){.
1af35 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a    sqlite3 *db;..
1af36 20 20 69 66 28 20 4e 45 56 45 52 28 70 3d 3d 30    if( NEVER(p==0
1af37 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62  ) ) return;.  db
1af38 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20   = p->db;.  if( 
1af39 70 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20  p->pPrev ){.    
1af3a 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20  p->pPrev->pNext 
1af3b 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65  = p->pNext;.  }e
1af3c 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
1af3d 20 64 62 2d 3e 70 56 64 62 65 3d 3d 70 20 29 3b   db->pVdbe==p );
1af3e 0a 20 20 20 20 64 62 2d 3e 70 56 64 62 65 20 3d  .    db->pVdbe =
1af3f 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20   p->pNext;.  }. 
1af40 20 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b   if( p->pNext ){
1af41 0a 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70  .    p->pNext->p
1af42 50 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b  Prev = p->pPrev;
1af43 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d 65  .  }.  releaseMe
1af44 6d 41 72 72 61 79 28 70 2d 3e 61 56 61 72 2c 20  mArray(p->aVar, 
1af45 70 2d 3e 6e 56 61 72 29 3b 0a 20 20 72 65 6c 65  p->nVar);.  rele
1af46 61 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61  aseMemArray(p->a
1af47 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65 73  ColName, p->nRes
1af48 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e  Column*COLNAME_N
1af49 29 3b 0a 20 20 76 64 62 65 46 72 65 65 4f 70 41  );.  vdbeFreeOpA
1af4a 72 72 61 79 28 64 62 2c 20 70 2d 3e 61 4f 70 2c  rray(db, p->aOp,
1af4b 20 70 2d 3e 6e 4f 70 29 3b 0a 20 20 73 71 6c 69   p->nOp);.  sqli
1af4c 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
1af4d 3e 61 4c 61 62 65 6c 29 3b 0a 20 20 73 71 6c 69  >aLabel);.  sqli
1af4e 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
1af4f 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 73 71  >aColName);.  sq
1af50 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1af51 70 2d 3e 7a 53 71 6c 29 3b 0a 20 20 70 2d 3e 6d  p->zSql);.  p->m
1af52 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49  agic = VDBE_MAGI
1af53 43 5f 44 45 41 44 3b 0a 20 20 73 71 6c 69 74 65  C_DEAD;.  sqlite
1af54 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 70  3DbFree(db, p->p
1af55 46 72 65 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Free);.  sqlite3
1af56 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 7d  DbFree(db, p);.}
1af57 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72  ../*.** Make sur
1af58 65 20 74 68 65 20 63 75 72 73 6f 72 20 70 20 69  e the cursor p i
1af59 73 20 72 65 61 64 79 20 74 6f 20 72 65 61 64 20  s ready to read 
1af5a 6f 72 20 77 72 69 74 65 20 74 68 65 20 72 6f 77  or write the row
1af5b 20 74 6f 20 77 68 69 63 68 20 69 74 0a 2a 2a 20   to which it.** 
1af5c 77 61 73 20 6c 61 73 74 20 70 6f 73 69 74 69 6f  was last positio
1af5d 6e 65 64 2e 20 20 52 65 74 75 72 6e 20 61 6e 20  ned.  Return an 
1af5e 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e  error code if an
1af5f 20 4f 4f 4d 20 66 61 75 6c 74 20 6f 72 20 49 2f   OOM fault or I/
1af60 4f 20 65 72 72 6f 72 0a 2a 2a 20 70 72 65 76 65  O error.** preve
1af61 6e 74 73 20 75 73 20 66 72 6f 6d 20 70 6f 73 69  nts us from posi
1af62 74 69 6f 6e 69 6e 67 20 74 68 65 20 63 75 72 73  tioning the curs
1af63 6f 72 20 74 6f 20 69 74 73 20 63 6f 72 72 65 63  or to its correc
1af64 74 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a  t position..**.*
1af65 2a 20 49 66 20 61 20 4d 6f 76 65 54 6f 20 6f 70  * If a MoveTo op
1af66 65 72 61 74 69 6f 6e 20 69 73 20 70 65 6e 64 69  eration is pendi
1af67 6e 67 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20  ng on the given 
1af68 63 75 72 73 6f 72 2c 20 74 68 65 6e 20 64 6f 20  cursor, then do 
1af69 74 68 61 74 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e  that.** MoveTo n
1af6a 6f 77 2e 20 20 49 66 20 6e 6f 20 6d 6f 76 65 20  ow.  If no move 
1af6b 69 73 20 70 65 6e 64 69 6e 67 2c 20 63 68 65 63  is pending, chec
1af6c 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20  k to see if the 
1af6d 72 6f 77 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20  row has been.** 
1af6e 64 65 6c 65 74 65 64 20 6f 75 74 20 66 72 6f 6d  deleted out from
1af6f 20 75 6e 64 65 72 20 74 68 65 20 63 75 72 73 6f   under the curso
1af70 72 20 61 6e 64 20 69 66 20 69 74 20 68 61 73 2c  r and if it has,
1af71 20 6d 61 72 6b 20 74 68 65 20 72 6f 77 20 61 73   mark the row as
1af72 0a 2a 2a 20 61 20 4e 55 4c 4c 20 72 6f 77 2e 0a  .** a NULL row..
1af73 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 75 72  **.** If the cur
1af74 73 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 70  sor is already p
1af75 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 63  ointing to the c
1af76 6f 72 72 65 63 74 20 72 6f 77 20 61 6e 64 20 74  orrect row and t
1af77 68 61 74 20 72 6f 77 20 68 61 73 0a 2a 2a 20 6e  hat row has.** n
1af78 6f 74 20 62 65 65 6e 20 64 65 6c 65 74 65 64 20  ot been deleted 
1af79 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72 20 74  out from under t
1af7a 68 65 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20  he cursor, then 
1af7b 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
1af7c 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49  a no-op..*/.SQLI
1af7d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1af7e 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72  qlite3VdbeCursor
1af7f 4d 6f 76 65 74 6f 28 56 64 62 65 43 75 72 73 6f  Moveto(VdbeCurso
1af80 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e  r *p){.  if( p->
1af81 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 29  deferredMoveto )
1af82 7b 0a 20 20 20 20 69 6e 74 20 72 65 73 2c 20 72  {.    int res, r
1af83 63 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  c;.#ifdef SQLITE
1af84 5f 54 45 53 54 0a 20 20 20 20 65 78 74 65 72 6e  _TEST.    extern
1af85 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 61   int sqlite3_sea
1af86 72 63 68 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69  rch_count;.#endi
1af87 66 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  f.    assert( p-
1af88 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20  >isTable );.    
1af89 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1af8a 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28  eMovetoUnpacked(
1af8b 70 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 70  p->pCursor, 0, p
1af8c 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74 2c 20  ->movetoTarget, 
1af8d 30 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66  0, &res);.    if
1af8e 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
1af8f 3b 0a 20 20 20 20 70 2d 3e 6c 61 73 74 52 6f 77  ;.    p->lastRow
1af90 69 64 20 3d 20 70 2d 3e 6d 6f 76 65 74 6f 54 61  id = p->movetoTa
1af91 72 67 65 74 3b 0a 20 20 20 20 70 2d 3e 72 6f 77  rget;.    p->row
1af92 69 64 49 73 56 61 6c 69 64 20 3d 20 41 4c 57 41  idIsValid = ALWA
1af93 59 53 28 72 65 73 3d 3d 30 29 20 3f 31 3a 30 3b  YS(res==0) ?1:0;
1af94 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72  .    if( NEVER(r
1af95 65 73 3c 30 29 20 29 7b 0a 20 20 20 20 20 20 72  es<0) ){.      r
1af96 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1af97 4e 65 78 74 28 70 2d 3e 70 43 75 72 73 6f 72 2c  Next(p->pCursor,
1af98 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20 69 66   &res);.      if
1af99 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
1af9a 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53  ;.    }.#ifdef S
1af9b 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73  QLITE_TEST.    s
1af9c 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f  qlite3_search_co
1af9d 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20  unt++;.#endif.  
1af9e 20 20 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76    p->deferredMov
1af9f 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e  eto = 0;.    p->
1afa0 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41  cacheStatus = CA
1afa1 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 65 6c  CHE_STALE;.  }el
1afa2 73 65 20 69 66 28 20 41 4c 57 41 59 53 28 70 2d  se if( ALWAYS(p-
1afa3 3e 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20  >pCursor) ){.   
1afa4 20 69 6e 74 20 68 61 73 4d 6f 76 65 64 3b 0a 20   int hasMoved;. 
1afa5 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69     int rc = sqli
1afa6 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 61  te3BtreeCursorHa
1afa7 73 4d 6f 76 65 64 28 70 2d 3e 70 43 75 72 73 6f  sMoved(p->pCurso
1afa8 72 2c 20 26 68 61 73 4d 6f 76 65 64 29 3b 0a 20  r, &hasMoved);. 
1afa9 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
1afaa 72 6e 20 72 63 3b 0a 20 20 20 20 69 66 28 20 68  rn rc;.    if( h
1afab 61 73 4d 6f 76 65 64 20 29 7b 0a 20 20 20 20 20  asMoved ){.     
1afac 20 70 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20   p->cacheStatus 
1afad 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20  = CACHE_STALE;. 
1afae 20 20 20 20 20 70 2d 3e 6e 75 6c 6c 52 6f 77 20       p->nullRow 
1afaf 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 1;.    }.  }. 
1afb0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1afb1 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  K;.}../*.** The 
1afb2 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69  following functi
1afb3 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74  ons:.**.** sqlit
1afb4 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
1afb5 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62  ().** sqlite3Vdb
1afb6 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 29  eSerialTypeLen()
1afb7 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53  .** sqlite3VdbeS
1afb8 65 72 69 61 6c 4c 65 6e 28 29 0a 2a 2a 20 73 71  erialLen().** sq
1afb9 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 50  lite3VdbeSerialP
1afba 75 74 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56  ut().** sqlite3V
1afbb 64 62 65 53 65 72 69 61 6c 47 65 74 28 29 0a 2a  dbeSerialGet().*
1afbc 2a 0a 2a 2a 20 65 6e 63 61 70 73 75 6c 61 74 65  *.** encapsulate
1afbd 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 73   the code that s
1afbe 65 72 69 61 6c 69 7a 65 73 20 76 61 6c 75 65 73  erializes values
1afbf 20 66 6f 72 20 73 74 6f 72 61 67 65 20 69 6e 20   for storage in 
1afc0 53 51 4c 69 74 65 0a 2a 2a 20 64 61 74 61 20 61  SQLite.** data a
1afc1 6e 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73  nd index records
1afc2 2e 20 45 61 63 68 20 73 65 72 69 61 6c 69 7a 65  . Each serialize
1afc3 64 20 76 61 6c 75 65 20 63 6f 6e 73 69 73 74 73  d value consists
1afc4 20 6f 66 20 61 0a 2a 2a 20 27 73 65 72 69 61 6c   of a.** 'serial
1afc5 2d 74 79 70 65 27 20 61 6e 64 20 61 20 62 6c 6f  -type' and a blo
1afc6 62 20 6f 66 20 64 61 74 61 2e 20 54 68 65 20 73  b of data. The s
1afc7 65 72 69 61 6c 20 74 79 70 65 20 69 73 20 61 6e  erial type is an
1afc8 20 38 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64   8-byte unsigned
1afc9 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20 73 74 6f  .** integer, sto
1afca 72 65 64 20 61 73 20 61 20 76 61 72 69 6e 74 2e  red as a varint.
1afcb 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 20 53 51 4c  .**.** In an SQL
1afcc 69 74 65 20 69 6e 64 65 78 20 72 65 63 6f 72 64  ite index record
1afcd 2c 20 74 68 65 20 73 65 72 69 61 6c 20 74 79 70  , the serial typ
1afce 65 20 69 73 20 73 74 6f 72 65 64 20 64 69 72 65  e is stored dire
1afcf 63 74 6c 79 20 62 65 66 6f 72 65 0a 2a 2a 20 74  ctly before.** t
1afd0 68 65 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20  he blob of data 
1afd1 74 68 61 74 20 69 74 20 63 6f 72 72 65 73 70 6f  that it correspo
1afd2 6e 64 73 20 74 6f 2e 20 49 6e 20 61 20 74 61 62  nds to. In a tab
1afd3 6c 65 20 72 65 63 6f 72 64 2c 20 61 6c 6c 20 73  le record, all s
1afd4 65 72 69 61 6c 0a 2a 2a 20 74 79 70 65 73 20 61  erial.** types a
1afd5 72 65 20 73 74 6f 72 65 64 20 61 74 20 74 68 65  re stored at the
1afd6 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 72 65   start of the re
1afd7 63 6f 72 64 2c 20 61 6e 64 20 74 68 65 20 62 6c  cord, and the bl
1afd8 6f 62 73 20 6f 66 20 64 61 74 61 20 61 74 0a 2a  obs of data at.*
1afd9 2a 20 74 68 65 20 65 6e 64 2e 20 48 65 6e 63 65  * the end. Hence
1afda 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73   these functions
1afdb 20 61 6c 6c 6f 77 20 74 68 65 20 63 61 6c 6c 65   allow the calle
1afdc 72 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 0a  r to handle the.
1afdd 2a 2a 20 73 65 72 69 61 6c 2d 74 79 70 65 20 61  ** serial-type a
1afde 6e 64 20 64 61 74 61 20 62 6c 6f 62 20 73 65 70  nd data blob sep
1afdf 65 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54  erately..**.** T
1afe0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62  he following tab
1afe1 6c 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65  le describes the
1afe2 20 76 61 72 69 6f 75 73 20 73 74 6f 72 61 67 65   various storage
1afe3 20 63 6c 61 73 73 65 73 20 66 6f 72 20 64 61 74   classes for dat
1afe4 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 65 72 69 61  a:.**.**   seria
1afe5 6c 20 74 79 70 65 20 20 20 20 20 20 20 20 62 79  l type        by
1afe6 74 65 73 20 6f 66 20 64 61 74 61 20 20 20 20 20  tes of data     
1afe7 20 74 79 70 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d   type.**   -----
1afe8 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d  ---------     --
1afe9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20  -------------   
1afea 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
1afeb 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20  .**      0      
1afec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
1afed 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c              NULL
1afee 0a 2a 2a 20 20 20 20 20 20 31 20 20 20 20 20 20  .**      1      
1afef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
1aff0 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e              sign
1aff1 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20  ed integer.**   
1aff2 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20     2            
1aff3 20 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20           2      
1aff4 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74        signed int
1aff5 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 33 20 20  eger.**      3  
1aff6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1aff7 20 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20     3            
1aff8 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a  signed integer.*
1aff9 2a 20 20 20 20 20 20 34 20 20 20 20 20 20 20 20  *      4        
1affa 20 20 20 20 20 20 20 20 20 20 20 20 20 34 20 20               4  
1affb 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64            signed
1affc 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20   integer.**     
1affd 20 35 20 20 20 20 20 20 20 20 20 20 20 20 20 20   5              
1affe 20 20 20 20 20 20 20 36 20 20 20 20 20 20 20 20         6        
1afff 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67      signed integ
1b000 65 72 0a 2a 2a 20 20 20 20 20 20 36 20 20 20 20  er.**      6    
1b001 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b002 20 38 20 20 20 20 20 20 20 20 20 20 20 20 73 69   8            si
1b003 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20  gned integer.** 
1b004 20 20 20 20 20 37 20 20 20 20 20 20 20 20 20 20       7          
1b005 20 20 20 20 20 20 20 20 20 20 20 38 20 20 20 20             8    
1b006 20 20 20 20 20 20 20 20 49 45 45 45 20 66 6c 6f          IEEE flo
1b007 61 74 0a 2a 2a 20 20 20 20 20 20 38 20 20 20 20  at.**      8    
1b008 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b009 20 30 20 20 20 20 20 20 20 20 20 20 20 20 49 6e   0            In
1b00a 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 30  teger constant 0
1b00b 0a 2a 2a 20 20 20 20 20 20 39 20 20 20 20 20 20  .**      9      
1b00c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
1b00d 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74 65              Inte
1b00e 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 31 0a 2a  ger constant 1.*
1b00f 2a 20 20 20 20 20 31 30 2c 31 31 20 20 20 20 20  *     10,11     
1b010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b011 20 20 20 20 20 20 20 20 20 20 72 65 73 65 72 76            reserv
1b012 65 64 20 66 6f 72 20 65 78 70 61 6e 73 69 6f 6e  ed for expansion
1b013 0a 2a 2a 20 20 20 20 4e 3e 3d 31 32 20 61 6e 64  .**    N>=12 and
1b014 20 65 76 65 6e 20 20 20 20 20 20 20 28 4e 2d 31   even       (N-1
1b015 32 29 2f 32 20 20 20 20 20 20 20 20 42 4c 4f 42  2)/2        BLOB
1b016 0a 2a 2a 20 20 20 20 4e 3e 3d 31 33 20 61 6e 64  .**    N>=13 and
1b017 20 6f 64 64 20 20 20 20 20 20 20 20 28 4e 2d 31   odd        (N-1
1b018 33 29 2f 32 20 20 20 20 20 20 20 20 74 65 78 74  3)/2        text
1b019 0a 2a 2a 0a 2a 2a 20 54 68 65 20 38 20 61 6e 64  .**.** The 8 and
1b01a 20 39 20 74 79 70 65 73 20 77 65 72 65 20 61 64   9 types were ad
1b01b 64 65 64 20 69 6e 20 33 2e 33 2e 30 2c 20 66 69  ded in 3.3.0, fi
1b01c 6c 65 20 66 6f 72 6d 61 74 20 34 2e 20 20 50 72  le format 4.  Pr
1b01d 69 6f 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20  ior versions.** 
1b01e 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e  of SQLite will n
1b01f 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68  ot understand th
1b020 6f 73 65 20 73 65 72 69 61 6c 20 74 79 70 65 73  ose serial types
1b021 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ..*/../*.** Retu
1b022 72 6e 20 74 68 65 20 73 65 72 69 61 6c 2d 74 79  rn the serial-ty
1b023 70 65 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65  pe for the value
1b024 20 73 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d 2e   stored in pMem.
1b025 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1b026 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64  TE u32 sqlite3Vd
1b027 62 65 53 65 72 69 61 6c 54 79 70 65 28 4d 65 6d  beSerialType(Mem
1b028 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69 6c 65   *pMem, int file
1b029 5f 66 6f 72 6d 61 74 29 7b 0a 20 20 69 6e 74 20  _format){.  int 
1b02a 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c  flags = pMem->fl
1b02b 61 67 73 3b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20  ags;.  int n;.. 
1b02c 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e   if( flags&MEM_N
1b02d 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ull ){.    retur
1b02e 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66  n 0;.  }.  if( f
1b02f 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 7b 0a  lags&MEM_Int ){.
1b030 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75      /* Figure ou
1b031 74 20 77 68 65 74 68 65 72 20 74 6f 20 75 73 65  t whether to use
1b032 20 31 2c 20 32 2c 20 34 2c 20 36 20 6f 72 20 38   1, 2, 4, 6 or 8
1b033 20 62 79 74 65 73 2e 20 2a 2f 0a 23 20 20 20 64   bytes. */.#   d
1b034 65 66 69 6e 65 20 4d 41 58 5f 36 42 59 54 45 20  efine MAX_6BYTE 
1b035 28 28 28 28 69 36 34 29 30 78 30 30 30 30 38 30  ((((i64)0x000080
1b036 30 30 29 3c 3c 33 32 29 2d 31 29 0a 20 20 20 20  00)<<32)-1).    
1b037 69 36 34 20 69 20 3d 20 70 4d 65 6d 2d 3e 75 2e  i64 i = pMem->u.
1b038 69 3b 0a 20 20 20 20 75 36 34 20 75 3b 0a 20 20  i;.    u64 u;.  
1b039 20 20 69 66 28 20 66 69 6c 65 5f 66 6f 72 6d 61    if( file_forma
1b03a 74 3e 3d 34 20 26 26 20 28 69 26 31 29 3d 3d 69  t>=4 && (i&1)==i
1b03b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1b03c 20 38 2b 28 75 33 32 29 69 3b 0a 20 20 20 20 7d   8+(u32)i;.    }
1b03d 0a 20 20 20 20 75 20 3d 20 69 3c 30 20 3f 20 2d  .    u = i<0 ? -
1b03e 69 20 3a 20 69 3b 0a 20 20 20 20 69 66 28 20 75  i : i;.    if( u
1b03f 3c 3d 31 32 37 20 29 20 72 65 74 75 72 6e 20 31  <=127 ) return 1
1b040 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 33 32 37  ;.    if( u<=327
1b041 36 37 20 29 20 72 65 74 75 72 6e 20 32 3b 0a 20  67 ) return 2;. 
1b042 20 20 20 69 66 28 20 75 3c 3d 38 33 38 38 36 30     if( u<=838860
1b043 37 20 29 20 72 65 74 75 72 6e 20 33 3b 0a 20 20  7 ) return 3;.  
1b044 20 20 69 66 28 20 75 3c 3d 32 31 34 37 34 38 33    if( u<=2147483
1b045 36 34 37 20 29 20 72 65 74 75 72 6e 20 34 3b 0a  647 ) return 4;.
1b046 20 20 20 20 69 66 28 20 75 3c 3d 4d 41 58 5f 36      if( u<=MAX_6
1b047 42 59 54 45 20 29 20 72 65 74 75 72 6e 20 35 3b  BYTE ) return 5;
1b048 0a 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20  .    return 6;. 
1b049 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 26 4d   }.  if( flags&M
1b04a 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 72  EM_Real ){.    r
1b04b 65 74 75 72 6e 20 37 3b 0a 20 20 7d 0a 20 20 61  eturn 7;.  }.  a
1b04c 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 2d  ssert( pMem->db-
1b04d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c  >mallocFailed ||
1b04e 20 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72 7c   flags&(MEM_Str|
1b04f 4d 45 4d 5f 42 6c 6f 62 29 20 29 3b 0a 20 20 6e  MEM_Blob) );.  n
1b050 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 66   = pMem->n;.  if
1b051 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65  ( flags & MEM_Ze
1b052 72 6f 20 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 70  ro ){.    n += p
1b053 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20  Mem->u.nZero;.  
1b054 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30  }.  assert( n>=0
1b055 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28 6e   );.  return ((n
1b056 2a 32 29 20 2b 20 31 32 20 2b 20 28 28 66 6c 61  *2) + 12 + ((fla
1b057 67 73 26 4d 45 4d 5f 53 74 72 29 21 3d 30 29 29  gs&MEM_Str)!=0))
1b058 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
1b059 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20  n the length of 
1b05a 74 68 65 20 64 61 74 61 20 63 6f 72 72 65 73 70  the data corresp
1b05b 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73 75  onding to the su
1b05c 70 70 6c 69 65 64 20 73 65 72 69 61 6c 2d 74 79  pplied serial-ty
1b05d 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  pe..*/.SQLITE_PR
1b05e 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65  IVATE u32 sqlite
1b05f 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c  3VdbeSerialTypeL
1b060 65 6e 28 75 33 32 20 73 65 72 69 61 6c 5f 74 79  en(u32 serial_ty
1b061 70 65 29 7b 0a 20 20 69 66 28 20 73 65 72 69 61  pe){.  if( seria
1b062 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20  l_type>=12 ){.  
1b063 20 20 72 65 74 75 72 6e 20 28 73 65 72 69 61 6c    return (serial
1b064 5f 74 79 70 65 2d 31 32 29 2f 32 3b 0a 20 20 7d  _type-12)/2;.  }
1b065 65 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63  else{.    static
1b066 20 63 6f 6e 73 74 20 75 38 20 61 53 69 7a 65 5b   const u8 aSize[
1b067 5d 20 3d 20 7b 20 30 2c 20 31 2c 20 32 2c 20 33  ] = { 0, 1, 2, 3
1b068 2c 20 34 2c 20 36 2c 20 38 2c 20 38 2c 20 30 2c  , 4, 6, 8, 8, 0,
1b069 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 20 20 20 20   0, 0, 0 };.    
1b06a 72 65 74 75 72 6e 20 61 53 69 7a 65 5b 73 65 72  return aSize[ser
1b06b 69 61 6c 5f 74 79 70 65 5d 3b 0a 20 20 7d 0a 7d  ial_type];.  }.}
1b06c 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72  ../*.** If we ar
1b06d 65 20 6f 6e 20 61 6e 20 61 72 63 68 69 74 65 63  e on an architec
1b06e 74 75 72 65 20 77 69 74 68 20 6d 69 78 65 64 2d  ture with mixed-
1b06f 65 6e 64 69 61 6e 20 66 6c 6f 61 74 69 6e 67 20  endian floating 
1b070 0a 2a 2a 20 70 6f 69 6e 74 73 20 28 65 78 3a 20  .** points (ex: 
1b071 41 52 4d 37 29 20 74 68 65 6e 20 73 77 61 70 20  ARM7) then swap 
1b072 74 68 65 20 6c 6f 77 65 72 20 34 20 62 79 74 65  the lower 4 byte
1b073 73 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 75  s with the .** u
1b074 70 70 65 72 20 34 20 62 79 74 65 73 2e 20 20 52  pper 4 bytes.  R
1b075 65 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74  eturn the result
1b076 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f 73 74  ..**.** For most
1b077 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2c 20   architectures, 
1b078 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  this is a no-op.
1b079 0a 2a 2a 0a 2a 2a 20 28 6c 61 74 65 72 29 3a 20  .**.** (later): 
1b07a 20 49 74 20 69 73 20 72 65 70 6f 72 74 65 64 20   It is reported 
1b07b 74 6f 20 6d 65 20 74 68 61 74 20 74 68 65 20 6d  to me that the m
1b07c 69 78 65 64 2d 65 6e 64 69 61 6e 20 70 72 6f 62  ixed-endian prob
1b07d 6c 65 6d 0a 2a 2a 20 6f 6e 20 41 52 4d 37 20 69  lem.** on ARM7 i
1b07e 73 20 61 6e 20 69 73 73 75 65 20 77 69 74 68 20  s an issue with 
1b07f 47 43 43 2c 20 6e 6f 74 20 77 69 74 68 20 74 68  GCC, not with th
1b080 65 20 41 52 4d 37 20 63 68 69 70 2e 20 20 49 74  e ARM7 chip.  It
1b081 20 73 65 65 6d 73 0a 2a 2a 20 74 68 61 74 20 65   seems.** that e
1b082 61 72 6c 79 20 76 65 72 73 69 6f 6e 73 20 6f 66  arly versions of
1b083 20 47 43 43 20 73 74 6f 72 65 64 20 74 68 65 20   GCC stored the 
1b084 74 77 6f 20 77 6f 72 64 73 20 6f 66 20 61 20 36  two words of a 6
1b085 34 2d 62 69 74 0a 2a 2a 20 66 6c 6f 61 74 20 69  4-bit.** float i
1b086 6e 20 74 68 65 20 77 72 6f 6e 67 20 6f 72 64 65  n the wrong orde
1b087 72 2e 20 20 41 6e 64 20 74 68 61 74 20 65 72 72  r.  And that err
1b088 6f 72 20 68 61 73 20 62 65 65 6e 20 70 72 6f 70  or has been prop
1b089 61 67 61 74 65 64 0a 2a 2a 20 65 76 65 72 20 73  agated.** ever s
1b08a 69 6e 63 65 2e 20 20 54 68 65 20 62 6c 61 6d 65  ince.  The blame
1b08b 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72   is not necessar
1b08c 69 6c 79 20 77 69 74 68 20 47 43 43 2c 20 74 68  ily with GCC, th
1b08d 6f 75 67 68 2e 0a 2a 2a 20 47 43 43 20 6d 69 67  ough..** GCC mig
1b08e 68 74 20 68 61 76 65 20 6a 75 73 74 20 63 6f 70  ht have just cop
1b08f 79 69 6e 67 20 74 68 65 20 70 72 6f 62 6c 65 6d  ying the problem
1b090 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 20 63 6f   from a prior co
1b091 6d 70 69 6c 65 72 2e 0a 2a 2a 20 49 20 61 6d 20  mpiler..** I am 
1b092 61 6c 73 6f 20 74 6f 6c 64 20 74 68 61 74 20 6e  also told that n
1b093 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66  ewer versions of
1b094 20 47 43 43 20 74 68 61 74 20 66 6f 6c 6c 6f 77   GCC that follow
1b095 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20   a different.** 
1b096 41 42 49 20 67 65 74 20 74 68 65 20 62 79 74 65  ABI get the byte
1b097 20 6f 72 64 65 72 20 72 69 67 68 74 2e 0a 2a 2a   order right..**
1b098 0a 2a 2a 20 44 65 76 65 6c 6f 70 65 72 73 20 75  .** Developers u
1b099 73 69 6e 67 20 53 51 4c 69 74 65 20 6f 6e 20 61  sing SQLite on a
1b09a 6e 20 41 52 4d 37 20 73 68 6f 75 6c 64 20 63 6f  n ARM7 should co
1b09b 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 20 74 68  mpile and run th
1b09c 65 69 72 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69  eir.** applicati
1b09d 6f 6e 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54  on using -DSQLIT
1b09e 45 5f 44 45 42 55 47 3d 31 20 61 74 20 6c 65 61  E_DEBUG=1 at lea
1b09f 73 74 20 6f 6e 63 65 2e 20 20 57 69 74 68 20 44  st once.  With D
1b0a0 45 42 55 47 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c  EBUG.** enabled,
1b0a1 20 73 6f 6d 65 20 61 73 73 65 72 74 73 20 62 65   some asserts be
1b0a2 6c 6f 77 20 77 69 6c 6c 20 65 6e 73 75 72 65 20  low will ensure 
1b0a3 74 68 61 74 20 74 68 65 20 62 79 74 65 20 6f 72  that the byte or
1b0a4 64 65 72 20 6f 66 0a 2a 2a 20 66 6c 6f 61 74 69  der of.** floati
1b0a5 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 20  ng point values 
1b0a6 69 73 20 63 6f 72 72 65 63 74 2e 0a 2a 2a 0a 2a  is correct..**.*
1b0a7 2a 20 28 32 30 30 37 2d 30 38 2d 33 30 29 20 20  * (2007-08-30)  
1b0a8 46 72 61 6e 6b 20 76 61 6e 20 56 75 67 74 20 68  Frank van Vugt h
1b0a9 61 73 20 73 74 75 64 69 65 64 20 74 68 69 73 20  as studied this 
1b0aa 70 72 6f 62 6c 65 6d 20 63 6c 6f 73 65 6c 79 0a  problem closely.
1b0ab 2a 2a 20 61 6e 64 20 68 61 73 20 73 65 6e 64 20  ** and has send 
1b0ac 68 69 73 20 66 69 6e 64 69 6e 67 73 20 74 6f 20  his findings to 
1b0ad 74 68 65 20 53 51 4c 69 74 65 20 64 65 76 65 6c  the SQLite devel
1b0ae 6f 70 65 72 73 2e 20 20 46 72 61 6e 6b 0a 2a 2a  opers.  Frank.**
1b0af 20 77 72 69 74 65 73 20 74 68 61 74 20 73 6f 6d   writes that som
1b0b0 65 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20  e Linux kernels 
1b0b1 6f 66 66 65 72 20 66 6c 6f 61 74 69 6e 67 20 70  offer floating p
1b0b2 6f 69 6e 74 20 68 61 72 64 77 61 72 65 0a 2a 2a  oint hardware.**
1b0b3 20 65 6d 75 6c 61 74 69 6f 6e 20 74 68 61 74 20   emulation that 
1b0b4 75 73 65 73 20 6f 6e 6c 79 20 33 32 2d 62 69 74  uses only 32-bit
1b0b5 20 6d 61 6e 74 69 73 73 61 73 20 69 6e 73 74 65   mantissas inste
1b0b6 61 64 20 6f 66 20 61 20 66 75 6c 6c 20 0a 2a 2a  ad of a full .**
1b0b7 20 34 38 2d 62 69 74 73 20 61 73 20 72 65 71 75   48-bits as requ
1b0b8 69 72 65 64 20 62 79 20 74 68 65 20 49 45 45 45  ired by the IEEE
1b0b9 20 73 74 61 6e 64 61 72 64 2e 20 20 28 54 68 69   standard.  (Thi
1b0ba 73 20 69 73 20 74 68 65 0a 2a 2a 20 43 4f 4e 46  s is the.** CONF
1b0bb 49 47 5f 46 50 45 5f 46 41 53 54 46 50 45 20 6f  IG_FPE_FASTFPE o
1b0bc 70 74 69 6f 6e 2e 29 20 20 4f 6e 20 73 75 63 68  ption.)  On such
1b0bd 20 73 79 73 74 65 6d 73 2c 20 66 6c 6f 61 74 69   systems, floati
1b0be 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 62 79 74 65  ng point.** byte
1b0bf 20 73 77 61 70 70 69 6e 67 20 62 65 63 6f 6d 65   swapping become
1b0c0 73 20 76 65 72 79 20 63 6f 6d 70 6c 69 63 61 74  s very complicat
1b0c1 65 64 2e 20 20 54 6f 20 61 76 6f 69 64 20 70 72  ed.  To avoid pr
1b0c2 6f 62 6c 65 6d 73 2c 0a 2a 2a 20 74 68 65 20 6e  oblems,.** the n
1b0c3 65 63 65 73 73 61 72 79 20 62 79 74 65 20 73 77  ecessary byte sw
1b0c4 61 70 70 69 6e 67 20 69 73 20 63 61 72 72 69 65  apping is carrie
1b0c5 64 20 6f 75 74 20 75 73 69 6e 67 20 61 20 36 34  d out using a 64
1b0c6 2d 62 69 74 20 69 6e 74 65 67 65 72 0a 2a 2a 20  -bit integer.** 
1b0c7 72 61 74 68 65 72 20 74 68 61 6e 20 61 20 36 34  rather than a 64
1b0c8 2d 62 69 74 20 66 6c 6f 61 74 2e 20 20 46 72 61  -bit float.  Fra
1b0c9 6e 6b 20 61 73 73 75 72 65 73 20 75 73 20 74 68  nk assures us th
1b0ca 61 74 20 74 68 65 20 63 6f 64 65 20 68 65 72 65  at the code here
1b0cb 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 68 69  .** works for hi
1b0cc 6d 2e 20 20 57 65 2c 20 74 68 65 20 64 65 76 65  m.  We, the deve
1b0cd 6c 6f 70 65 72 73 2c 20 68 61 76 65 20 6e 6f 20  lopers, have no 
1b0ce 77 61 79 20 74 6f 20 69 6e 64 65 70 65 6e 64 65  way to independe
1b0cf 6e 74 6c 79 0a 2a 2a 20 76 65 72 69 66 79 20 74  ntly.** verify t
1b0d0 68 69 73 2c 20 62 75 74 20 46 72 61 6e 6b 20 73  his, but Frank s
1b0d1 65 65 6d 73 20 74 6f 20 6b 6e 6f 77 20 77 68 61  eems to know wha
1b0d2 74 20 68 65 20 69 73 20 74 61 6c 6b 69 6e 67 20  t he is talking 
1b0d3 61 62 6f 75 74 0a 2a 2a 20 73 6f 20 77 65 20 74  about.** so we t
1b0d4 72 75 73 74 20 68 69 6d 2e 0a 2a 2f 0a 23 69 66  rust him..*/.#if
1b0d5 64 65 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44  def SQLITE_MIXED
1b0d6 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c  _ENDIAN_64BIT_FL
1b0d7 4f 41 54 0a 73 74 61 74 69 63 20 75 36 34 20 66  OAT.static u64 f
1b0d8 6c 6f 61 74 53 77 61 70 28 75 36 34 20 69 6e 29  loatSwap(u64 in)
1b0d9 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20  {.  union {.    
1b0da 75 36 34 20 72 3b 0a 20 20 20 20 75 33 32 20 69  u64 r;.    u32 i
1b0db 5b 32 5d 3b 0a 20 20 7d 20 75 3b 0a 20 20 75 33  [2];.  } u;.  u3
1b0dc 32 20 74 3b 0a 0a 20 20 75 2e 72 20 3d 20 69 6e  2 t;..  u.r = in
1b0dd 3b 0a 20 20 74 20 3d 20 75 2e 69 5b 30 5d 3b 0a  ;.  t = u.i[0];.
1b0de 20 20 75 2e 69 5b 30 5d 20 3d 20 75 2e 69 5b 31    u.i[0] = u.i[1
1b0df 5d 3b 0a 20 20 75 2e 69 5b 31 5d 20 3d 20 74 3b  ];.  u.i[1] = t;
1b0e0 0a 20 20 72 65 74 75 72 6e 20 75 2e 72 3b 0a 7d  .  return u.r;.}
1b0e1 0a 23 20 64 65 66 69 6e 65 20 73 77 61 70 4d 69  .# define swapMi
1b0e2 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 58  xedEndianFloat(X
1b0e3 29 20 20 58 20 3d 20 66 6c 6f 61 74 53 77 61 70  )  X = floatSwap
1b0e4 28 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  (X).#else.# defi
1b0e5 6e 65 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69  ne swapMixedEndi
1b0e6 61 6e 46 6c 6f 61 74 28 58 29 0a 23 65 6e 64 69  anFloat(X).#endi
1b0e7 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74  f../*.** Write t
1b0e8 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61  he serialized da
1b0e9 74 61 20 62 6c 6f 62 20 66 6f 72 20 74 68 65 20  ta blob for the 
1b0ea 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20  value stored in 
1b0eb 70 4d 65 6d 20 69 6e 74 6f 20 0a 2a 2a 20 62 75  pMem into .** bu
1b0ec 66 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65 64  f. It is assumed
1b0ed 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72   that the caller
1b0ee 20 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 73   has allocated s
1b0ef 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 2e  ufficient space.
1b0f0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
1b0f1 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 77  umber of bytes w
1b0f2 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 6e 42  ritten..**.** nB
1b0f3 75 66 20 69 73 20 74 68 65 20 61 6d 6f 75 6e 74  uf is the amount
1b0f4 20 6f 66 20 73 70 61 63 65 20 6c 65 66 74 20 69   of space left i
1b0f5 6e 20 62 75 66 5b 5d 2e 20 20 6e 42 75 66 20 6d  n buf[].  nBuf m
1b0f6 75 73 74 20 61 6c 77 61 79 73 20 62 65 0a 2a 2a  ust always be.**
1b0f7 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f   large enough to
1b0f8 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 69 72 65   hold the entire
1b0f9 20 66 69 65 6c 64 2e 20 20 45 78 63 65 70 74 2c   field.  Except,
1b0fa 20 69 66 20 74 68 65 20 66 69 65 6c 64 20 69 73   if the field is
1b0fb 0a 2a 2a 20 61 20 62 6c 6f 62 20 77 69 74 68 20  .** a blob with 
1b0fc 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61  a zero-filled ta
1b0fd 69 6c 2c 20 74 68 65 6e 20 62 75 66 5b 5d 20 6d  il, then buf[] m
1b0fe 69 67 68 74 20 62 65 20 6a 75 73 74 20 74 68 65  ight be just the
1b0ff 20 72 69 67 68 74 0a 2a 2a 20 73 69 7a 65 20 74   right.** size t
1b100 6f 20 68 6f 6c 64 20 65 76 65 72 79 74 68 69 6e  o hold everythin
1b101 67 20 65 78 63 65 70 74 20 66 6f 72 20 74 68 65  g except for the
1b102 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69   zero-filled tai
1b103 6c 2e 20 20 49 66 20 62 75 66 5b 5d 0a 2a 2a 20  l.  If buf[].** 
1b104 69 73 20 6f 6e 6c 79 20 62 69 67 20 65 6e 6f 75  is only big enou
1b105 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e  gh to hold the n
1b106 6f 6e 2d 7a 65 72 6f 20 70 72 65 66 69 78 2c 20  on-zero prefix, 
1b107 74 68 65 6e 20 6f 6e 6c 79 20 77 72 69 74 65 20  then only write 
1b108 74 68 61 74 0a 2a 2a 20 70 72 65 66 69 78 20 69  that.** prefix i
1b109 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 42 75 74 20  nto buf[].  But 
1b10a 69 66 20 62 75 66 5b 5d 20 69 73 20 6c 61 72 67  if buf[] is larg
1b10b 65 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64  e enough to hold
1b10c 20 62 6f 74 68 20 74 68 65 0a 2a 2a 20 70 72 65   both the.** pre
1b10d 66 69 78 20 61 6e 64 20 74 68 65 20 74 61 69 6c  fix and the tail
1b10e 20 74 68 65 6e 20 77 72 69 74 65 20 74 68 65 20   then write the 
1b10f 70 72 65 66 69 78 20 61 6e 64 20 73 65 74 20 74  prefix and set t
1b110 68 65 20 74 61 69 6c 20 74 6f 20 61 6c 6c 0a 2a  he tail to all.*
1b111 2a 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 52  * zeros..**.** R
1b112 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
1b113 20 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 6c   of bytes actual
1b114 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  ly written into 
1b115 62 75 66 5b 5d 2e 20 20 54 68 65 20 6e 75 6d 62  buf[].  The numb
1b116 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 69  er.** of bytes i
1b117 6e 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c 65  n the zero-fille
1b118 64 20 74 61 69 6c 20 69 73 20 69 6e 63 6c 75 64  d tail is includ
1b119 65 64 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e  ed in the return
1b11a 20 76 61 6c 75 65 20 6f 6e 6c 79 0a 2a 2a 20 69   value only.** i
1b11b 66 20 74 68 6f 73 65 20 62 79 74 65 73 20 77 65  f those bytes we
1b11c 72 65 20 7a 65 72 6f 65 64 20 69 6e 20 62 75 66  re zeroed in buf
1b11d 5b 5d 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50  []..*/ .SQLITE_P
1b11e 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74  RIVATE u32 sqlit
1b11f 65 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 28  e3VdbeSerialPut(
1b120 75 38 20 2a 62 75 66 2c 20 69 6e 74 20 6e 42 75  u8 *buf, int nBu
1b121 66 2c 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e  f, Mem *pMem, in
1b122 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a  t file_format){.
1b123 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70    u32 serial_typ
1b124 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53  e = sqlite3VdbeS
1b125 65 72 69 61 6c 54 79 70 65 28 70 4d 65 6d 2c 20  erialType(pMem, 
1b126 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20  file_format);.  
1b127 75 33 32 20 6c 65 6e 3b 0a 0a 20 20 2f 2a 20 49  u32 len;..  /* I
1b128 6e 74 65 67 65 72 20 61 6e 64 20 52 65 61 6c 20  nteger and Real 
1b129 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f  */.  if( serial_
1b12a 74 79 70 65 3c 3d 37 20 26 26 20 73 65 72 69 61  type<=7 && seria
1b12b 6c 5f 74 79 70 65 3e 30 20 29 7b 0a 20 20 20 20  l_type>0 ){.    
1b12c 75 36 34 20 76 3b 0a 20 20 20 20 75 33 32 20 69  u64 v;.    u32 i
1b12d 3b 0a 20 20 20 20 69 66 28 20 73 65 72 69 61 6c  ;.    if( serial
1b12e 5f 74 79 70 65 3d 3d 37 20 29 7b 0a 20 20 20 20  _type==7 ){.    
1b12f 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
1b130 28 76 29 3d 3d 73 69 7a 65 6f 66 28 70 4d 65 6d  (v)==sizeof(pMem
1b131 2d 3e 72 29 20 29 3b 0a 20 20 20 20 20 20 6d 65  ->r) );.      me
1b132 6d 63 70 79 28 26 76 2c 20 26 70 4d 65 6d 2d 3e  mcpy(&v, &pMem->
1b133 72 2c 20 73 69 7a 65 6f 66 28 76 29 29 3b 0a 20  r, sizeof(v));. 
1b134 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 45 6e       swapMixedEn
1b135 64 69 61 6e 46 6c 6f 61 74 28 76 29 3b 0a 20 20  dianFloat(v);.  
1b136 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 76    }else{.      v
1b137 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20   = pMem->u.i;.  
1b138 20 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 69 20    }.    len = i 
1b139 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  = sqlite3VdbeSer
1b13a 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61  ialTypeLen(seria
1b13b 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 61 73 73  l_type);.    ass
1b13c 65 72 74 28 20 6c 65 6e 3c 3d 28 75 33 32 29 6e  ert( len<=(u32)n
1b13d 42 75 66 20 29 3b 0a 20 20 20 20 77 68 69 6c 65  Buf );.    while
1b13e 28 20 69 2d 2d 20 29 7b 0a 20 20 20 20 20 20 62  ( i-- ){.      b
1b13f 75 66 5b 69 5d 20 3d 20 28 75 38 29 28 76 26 30  uf[i] = (u8)(v&0
1b140 78 46 46 29 3b 0a 20 20 20 20 20 20 76 20 3e 3e  xFF);.      v >>
1b141 3d 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  = 8;.    }.    r
1b142 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a  eturn len;.  }..
1b143 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20 62    /* String or b
1b144 6c 6f 62 20 2a 2f 0a 20 20 69 66 28 20 73 65 72  lob */.  if( ser
1b145 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a  ial_type>=12 ){.
1b146 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
1b147 2d 3e 6e 20 2b 20 28 28 70 4d 65 6d 2d 3e 66 6c  ->n + ((pMem->fl
1b148 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 29 3f  ags & MEM_Zero)?
1b149 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3a 30 29  pMem->u.nZero:0)
1b14a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 3d 3d  .             ==
1b14b 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62   (int)sqlite3Vdb
1b14c 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73  eSerialTypeLen(s
1b14d 65 72 69 61 6c 5f 74 79 70 65 29 20 29 3b 0a 20  erial_type) );. 
1b14e 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d     assert( pMem-
1b14f 3e 6e 3c 3d 6e 42 75 66 20 29 3b 0a 20 20 20 20  >n<=nBuf );.    
1b150 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20  len = pMem->n;. 
1b151 20 20 20 6d 65 6d 63 70 79 28 62 75 66 2c 20 70     memcpy(buf, p
1b152 4d 65 6d 2d 3e 7a 2c 20 6c 65 6e 29 3b 0a 20 20  Mem->z, len);.  
1b153 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67    if( pMem->flag
1b154 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a  s & MEM_Zero ){.
1b155 20 20 20 20 20 20 6c 65 6e 20 2b 3d 20 70 4d 65        len += pMe
1b156 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20  m->u.nZero;.    
1b157 20 20 61 73 73 65 72 74 28 20 6e 42 75 66 3e 3d    assert( nBuf>=
1b158 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6c  0 );.      if( l
1b159 65 6e 20 3e 20 28 75 33 32 29 6e 42 75 66 20 29  en > (u32)nBuf )
1b15a 7b 0a 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20  {.        len = 
1b15b 28 75 33 32 29 6e 42 75 66 3b 0a 20 20 20 20 20  (u32)nBuf;.     
1b15c 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28   }.      memset(
1b15d 26 62 75 66 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 30  &buf[pMem->n], 0
1b15e 2c 20 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b 0a  , len-pMem->n);.
1b15f 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
1b160 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   len;.  }..  /* 
1b161 4e 55 4c 4c 20 6f 72 20 63 6f 6e 73 74 61 6e 74  NULL or constant
1b162 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20 20 72 65  s 0 or 1 */.  re
1b163 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
1b164 20 44 65 73 65 72 69 61 6c 69 7a 65 20 74 68 65   Deserialize the
1b165 20 64 61 74 61 20 62 6c 6f 62 20 70 6f 69 6e 74   data blob point
1b166 65 64 20 74 6f 20 62 79 20 62 75 66 20 61 73 20  ed to by buf as 
1b167 73 65 72 69 61 6c 20 74 79 70 65 20 73 65 72 69  serial type seri
1b168 61 6c 5f 74 79 70 65 0a 2a 2a 20 61 6e 64 20 73  al_type.** and s
1b169 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
1b16a 69 6e 20 70 4d 65 6d 2e 20 20 52 65 74 75 72 6e  in pMem.  Return
1b16b 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
1b16c 79 74 65 73 20 72 65 61 64 2e 0a 2a 2f 20 0a 53  ytes read..*/ .S
1b16d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33  QLITE_PRIVATE u3
1b16e 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  2 sqlite3VdbeSer
1b16f 69 61 6c 47 65 74 28 0a 20 20 63 6f 6e 73 74 20  ialGet(.  const 
1b170 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 62  unsigned char *b
1b171 75 66 2c 20 20 20 20 20 2f 2a 20 42 75 66 66 65  uf,     /* Buffe
1b172 72 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a 65  r to deserialize
1b173 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33 32 20 73   from */.  u32 s
1b174 65 72 69 61 6c 5f 74 79 70 65 2c 20 20 20 20 20  erial_type,     
1b175 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 72 69           /* Seri
1b176 61 6c 20 74 79 70 65 20 74 6f 20 64 65 73 65 72  al type to deser
1b177 69 61 6c 69 7a 65 20 2a 2f 0a 20 20 4d 65 6d 20  ialize */.  Mem 
1b178 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20 20 20  *pMem           
1b179 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d            /* Mem
1b17a 6f 72 79 20 63 65 6c 6c 20 74 6f 20 77 72 69 74  ory cell to writ
1b17b 65 20 76 61 6c 75 65 20 69 6e 74 6f 20 2a 2f 0a  e value into */.
1b17c 29 7b 0a 20 20 73 77 69 74 63 68 28 20 73 65 72  ){.  switch( ser
1b17d 69 61 6c 5f 74 79 70 65 20 29 7b 0a 20 20 20 20  ial_type ){.    
1b17e 63 61 73 65 20 31 30 3a 20 20 20 2f 2a 20 52 65  case 10:   /* Re
1b17f 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 72  served for futur
1b180 65 20 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73  e use */.    cas
1b181 65 20 31 31 3a 20 20 20 2f 2a 20 52 65 73 65 72  e 11:   /* Reser
1b182 76 65 64 20 66 6f 72 20 66 75 74 75 72 65 20 75  ved for future u
1b183 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 30  se */.    case 0
1b184 3a 20 7b 20 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a  : {  /* NULL */.
1b185 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
1b186 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
1b187 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1b188 0a 20 20 20 20 63 61 73 65 20 31 3a 20 7b 20 2f  .    case 1: { /
1b189 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20  * 1-byte signed 
1b18a 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20  integer */.     
1b18b 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 73 69   pMem->u.i = (si
1b18c 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30 5d  gned char)buf[0]
1b18d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  ;.      pMem->fl
1b18e 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20  ags = MEM_Int;. 
1b18f 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
1b190 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 32 3a     }.    case 2:
1b191 20 7b 20 2f 2a 20 32 2d 62 79 74 65 20 73 69 67   { /* 2-byte sig
1b192 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20  ned integer */. 
1b193 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d       pMem->u.i =
1b194 20 28 28 28 73 69 67 6e 65 64 20 63 68 61 72 29   (((signed char)
1b195 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 75  buf[0])<<8) | bu
1b196 66 5b 31 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d  f[1];.      pMem
1b197 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
1b198 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  t;.      return 
1b199 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  2;.    }.    cas
1b19a 65 20 33 3a 20 7b 20 2f 2a 20 33 2d 62 79 74 65  e 3: { /* 3-byte
1b19b 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20   signed integer 
1b19c 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75  */.      pMem->u
1b19d 2e 69 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63  .i = (((signed c
1b19e 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 31 36 29  har)buf[0])<<16)
1b19f 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 38 29 20 7c   | (buf[1]<<8) |
1b1a0 20 62 75 66 5b 32 5d 3b 0a 20 20 20 20 20 20 70   buf[2];.      p
1b1a1 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
1b1a2 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75  _Int;.      retu
1b1a3 72 6e 20 33 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 3;.    }.    
1b1a4 63 61 73 65 20 34 3a 20 7b 20 2f 2a 20 34 2d 62  case 4: { /* 4-b
1b1a5 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67  yte signed integ
1b1a6 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d  er */.      pMem
1b1a7 2d 3e 75 2e 69 20 3d 20 28 62 75 66 5b 30 5d 3c  ->u.i = (buf[0]<
1b1a8 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c  <24) | (buf[1]<<
1b1a9 31 36 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c 38  16) | (buf[2]<<8
1b1aa 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20 20  ) | buf[3];.    
1b1ab 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
1b1ac 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72  MEM_Int;.      r
1b1ad 65 74 75 72 6e 20 34 3b 0a 20 20 20 20 7d 0a 20  eturn 4;.    }. 
1b1ae 20 20 20 63 61 73 65 20 35 3a 20 7b 20 2f 2a 20     case 5: { /* 
1b1af 36 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e  6-byte signed in
1b1b0 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 75  teger */.      u
1b1b1 36 34 20 78 20 3d 20 28 28 28 73 69 67 6e 65 64  64 x = (((signed
1b1b2 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c 38   char)buf[0])<<8
1b1b3 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20 20  ) | buf[1];.    
1b1b4 20 20 75 33 32 20 79 20 3d 20 28 62 75 66 5b 32    u32 y = (buf[2
1b1b5 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 33 5d  ]<<24) | (buf[3]
1b1b6 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 34 5d 3c  <<16) | (buf[4]<
1b1b7 3c 38 29 20 7c 20 62 75 66 5b 35 5d 3b 0a 20 20  <8) | buf[5];.  
1b1b8 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29 20      x = (x<<32) 
1b1b9 7c 20 79 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  | y;.      pMem-
1b1ba 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78  >u.i = *(i64*)&x
1b1bb 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  ;.      pMem->fl
1b1bc 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20  ags = MEM_Int;. 
1b1bd 20 20 20 20 20 72 65 74 75 72 6e 20 36 3b 0a 20       return 6;. 
1b1be 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 36 3a     }.    case 6:
1b1bf 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 73 69 67     /* 8-byte sig
1b1c0 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20  ned integer */. 
1b1c1 20 20 20 63 61 73 65 20 37 3a 20 7b 20 2f 2a 20     case 7: { /* 
1b1c2 49 45 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f  IEEE floating po
1b1c3 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 75 36 34  int */.      u64
1b1c4 20 78 3b 0a 20 20 20 20 20 20 75 33 32 20 79 3b   x;.      u32 y;
1b1c5 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44  .#if !defined(ND
1b1c6 45 42 55 47 29 20 26 26 20 21 64 65 66 69 6e 65  EBUG) && !define
1b1c7 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  d(SQLITE_OMIT_FL
1b1c8 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20 20  OATING_POINT).  
1b1c9 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68      /* Verify th
1b1ca 61 74 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20  at integers and 
1b1cb 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76  floating point v
1b1cc 61 6c 75 65 73 20 75 73 65 20 74 68 65 20 73 61  alues use the sa
1b1cd 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 65  me.      ** byte
1b1ce 20 6f 72 64 65 72 2e 20 20 4f 72 2c 20 74 68 61   order.  Or, tha
1b1cf 74 20 69 66 20 53 51 4c 49 54 45 5f 4d 49 58 45  t if SQLITE_MIXE
1b1d0 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46  D_ENDIAN_64BIT_F
1b1d1 4c 4f 41 54 20 69 73 0a 20 20 20 20 20 20 2a 2a  LOAT is.      **
1b1d2 20 64 65 66 69 6e 65 64 20 74 68 61 74 20 36 34   defined that 64
1b1d3 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f  -bit floating po
1b1d4 69 6e 74 20 76 61 6c 75 65 73 20 72 65 61 6c 6c  int values reall
1b1d5 79 20 61 72 65 20 6d 69 78 65 64 0a 20 20 20 20  y are mixed.    
1b1d6 20 20 2a 2a 20 65 6e 64 69 61 6e 2e 0a 20 20 20    ** endian..   
1b1d7 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74     */.      stat
1b1d8 69 63 20 63 6f 6e 73 74 20 75 36 34 20 74 31 20  ic const u64 t1 
1b1d9 3d 20 28 28 75 36 34 29 30 78 33 66 66 30 30 30  = ((u64)0x3ff000
1b1da 30 30 29 3c 3c 33 32 3b 0a 20 20 20 20 20 20 73  00)<<32;.      s
1b1db 74 61 74 69 63 20 63 6f 6e 73 74 20 64 6f 75 62  tatic const doub
1b1dc 6c 65 20 72 31 20 3d 20 31 2e 30 3b 0a 20 20 20  le r1 = 1.0;.   
1b1dd 20 20 20 75 36 34 20 74 32 20 3d 20 74 31 3b 0a     u64 t2 = t1;.
1b1de 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64 45        swapMixedE
1b1df 6e 64 69 61 6e 46 6c 6f 61 74 28 74 32 29 3b 0a  ndianFloat(t2);.
1b1e0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69        assert( si
1b1e1 7a 65 6f 66 28 72 31 29 3d 3d 73 69 7a 65 6f 66  zeof(r1)==sizeof
1b1e2 28 74 32 29 20 26 26 20 6d 65 6d 63 6d 70 28 26  (t2) && memcmp(&
1b1e3 72 31 2c 20 26 74 32 2c 20 73 69 7a 65 6f 66 28  r1, &t2, sizeof(
1b1e4 72 31 29 29 3d 3d 30 20 29 3b 0a 23 65 6e 64 69  r1))==0 );.#endi
1b1e5 66 0a 0a 20 20 20 20 20 20 78 20 3d 20 28 62 75  f..      x = (bu
1b1e6 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66  f[0]<<24) | (buf
1b1e7 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b  [1]<<16) | (buf[
1b1e8 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d 3b  2]<<8) | buf[3];
1b1e9 0a 20 20 20 20 20 20 79 20 3d 20 28 62 75 66 5b  .      y = (buf[
1b1ea 34 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 35  4]<<24) | (buf[5
1b1eb 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 36 5d  ]<<16) | (buf[6]
1b1ec 3c 3c 38 29 20 7c 20 62 75 66 5b 37 5d 3b 0a 20  <<8) | buf[7];. 
1b1ed 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33 32 29       x = (x<<32)
1b1ee 20 7c 20 79 3b 0a 20 20 20 20 20 20 69 66 28 20   | y;.      if( 
1b1ef 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d 36 20 29  serial_type==6 )
1b1f0 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  {.        pMem->
1b1f1 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b  u.i = *(i64*)&x;
1b1f2 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  .        pMem->f
1b1f3 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
1b1f4 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1b1f5 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a       assert( siz
1b1f6 65 6f 66 28 78 29 3d 3d 38 20 26 26 20 73 69 7a  eof(x)==8 && siz
1b1f7 65 6f 66 28 70 4d 65 6d 2d 3e 72 29 3d 3d 38 20  eof(pMem->r)==8 
1b1f8 29 3b 0a 20 20 20 20 20 20 20 20 73 77 61 70 4d  );.        swapM
1b1f9 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28  ixedEndianFloat(
1b1fa 78 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63  x);.        memc
1b1fb 70 79 28 26 70 4d 65 6d 2d 3e 72 2c 20 26 78 2c  py(&pMem->r, &x,
1b1fc 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20   sizeof(x));.   
1b1fd 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
1b1fe 20 3d 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28   = sqlite3IsNaN(
1b1ff 70 4d 65 6d 2d 3e 72 29 20 3f 20 4d 45 4d 5f 4e  pMem->r) ? MEM_N
1b200 75 6c 6c 20 3a 20 4d 45 4d 5f 52 65 61 6c 3b 0a  ull : MEM_Real;.
1b201 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
1b202 74 75 72 6e 20 38 3b 0a 20 20 20 20 7d 0a 20 20  turn 8;.    }.  
1b203 20 20 63 61 73 65 20 38 3a 20 20 20 20 2f 2a 20    case 8:    /* 
1b204 49 6e 74 65 67 65 72 20 30 20 2a 2f 0a 20 20 20  Integer 0 */.   
1b205 20 63 61 73 65 20 39 3a 20 7b 20 20 2f 2a 20 49   case 9: {  /* I
1b206 6e 74 65 67 65 72 20 31 20 2a 2f 0a 20 20 20 20  nteger 1 */.    
1b207 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 65    pMem->u.i = se
1b208 72 69 61 6c 5f 74 79 70 65 2d 38 3b 0a 20 20 20  rial_type-8;.   
1b209 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
1b20a 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20   MEM_Int;.      
1b20b 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a  return 0;.    }.
1b20c 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
1b20d 20 20 20 20 20 75 33 32 20 6c 65 6e 20 3d 20 28       u32 len = (
1b20e 73 65 72 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f  serial_type-12)/
1b20f 32 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a  2;.      pMem->z
1b210 20 3d 20 28 63 68 61 72 20 2a 29 62 75 66 3b 0a   = (char *)buf;.
1b211 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20        pMem->n = 
1b212 6c 65 6e 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  len;.      pMem-
1b213 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 20  >xDel = 0;.     
1b214 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65   if( serial_type
1b215 26 30 78 30 31 20 29 7b 0a 20 20 20 20 20 20 20  &0x01 ){.       
1b216 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
1b217 45 4d 5f 53 74 72 20 7c 20 4d 45 4d 5f 45 70 68  EM_Str | MEM_Eph
1b218 65 6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  em;.      }else{
1b219 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  .        pMem->f
1b21a 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20  lags = MEM_Blob 
1b21b 7c 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20  | MEM_Ephem;.   
1b21c 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72     }.      retur
1b21d 6e 20 6c 65 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d  n len;.    }.  }
1b21e 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
1b21f 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65  ./*.** Given the
1b220 20 6e 4b 65 79 2d 62 79 74 65 20 65 6e 63 6f 64   nKey-byte encod
1b221 69 6e 67 20 6f 66 20 61 20 72 65 63 6f 72 64 20  ing of a record 
1b222 69 6e 20 70 4b 65 79 5b 5d 2c 20 70 61 72 73 65  in pKey[], parse
1b223 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72 64 20 69   the.** record i
1b224 6e 74 6f 20 61 20 55 6e 70 61 63 6b 65 64 52 65  nto a UnpackedRe
1b225 63 6f 72 64 20 73 74 72 75 63 74 75 72 65 2e 20  cord structure. 
1b226 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
1b227 72 20 74 6f 0a 2a 2a 20 74 68 61 74 20 73 74 72  r to.** that str
1b228 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  ucture..**.** Th
1b229 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
1b22a 6f 6e 20 6d 69 67 68 74 20 70 72 6f 76 69 64 65  on might provide
1b22b 20 73 7a 53 70 61 63 65 20 62 79 74 65 73 20 6f   szSpace bytes o
1b22c 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 70 61 63  f memory.** spac
1b22d 65 20 61 74 20 70 53 70 61 63 65 2e 20 20 54 68  e at pSpace.  Th
1b22e 69 73 20 73 70 61 63 65 20 63 61 6e 20 62 65 20  is space can be 
1b22f 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65  used to hold the
1b230 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 56 44 62   returned.** VDb
1b231 65 50 61 72 73 65 64 52 65 63 6f 72 64 20 73 74  eParsedRecord st
1b232 72 75 63 74 75 72 65 20 69 66 20 69 74 20 69 73  ructure if it is
1b233 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2e 20 20   large enough.  
1b234 49 66 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20  If it is.** not 
1b235 62 69 67 20 65 6e 6f 75 67 68 2c 20 73 70 61 63  big enough, spac
1b236 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72  e is obtained fr
1b237 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  om sqlite3_mallo
1b238 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  c()..**.** The r
1b239 65 74 75 72 6e 65 64 20 73 74 72 75 63 74 75 72  eturned structur
1b23a 65 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f 73  e should be clos
1b23b 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a  ed by a call to.
1b23c 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 44 65  ** sqlite3VdbeDe
1b23d 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f  leteUnpackedReco
1b23e 72 64 28 29 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45  rd()..*/ .SQLITE
1b23f 5f 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b 65  _PRIVATE Unpacke
1b240 64 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65 33  dRecord *sqlite3
1b241 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b  VdbeRecordUnpack
1b242 28 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65  (.  KeyInfo *pKe
1b243 79 49 6e 66 6f 2c 20 20 20 20 20 2f 2a 20 49 6e  yInfo,     /* In
1b244 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
1b245 74 68 65 20 72 65 63 6f 72 64 20 66 6f 72 6d 61  the record forma
1b246 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65 79 2c  t */.  int nKey,
1b247 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1b248 20 53 69 7a 65 20 6f 66 20 74 68 65 20 62 69 6e   Size of the bin
1b249 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  ary record */.  
1b24a 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
1b24b 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 69  ,      /* The bi
1b24c 6e 61 72 79 20 72 65 63 6f 72 64 20 2a 2f 0a 20  nary record */. 
1b24d 20 63 68 61 72 20 2a 70 53 70 61 63 65 2c 20 20   char *pSpace,  
1b24e 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 61 6c 69          /* Unali
1b24f 67 6e 65 64 20 73 70 61 63 65 20 61 76 61 69 6c  gned space avail
1b250 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20 74 68 65  able to hold the
1b251 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74   object */.  int
1b252 20 73 7a 53 70 61 63 65 20 20 20 20 20 20 20 20   szSpace        
1b253 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70      /* Size of p
1b254 53 70 61 63 65 5b 5d 20 69 6e 20 62 79 74 65 73  Space[] in bytes
1b255 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75   */.){.  const u
1b256 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b  nsigned char *aK
1b257 65 79 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69  ey = (const unsi
1b258 67 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65 79  gned char *)pKey
1b259 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f  ;.  UnpackedReco
1b25a 72 64 20 2a 70 3b 20 20 2f 2a 20 54 68 65 20 75  rd *p;  /* The u
1b25b 6e 70 61 63 6b 65 64 20 72 65 63 6f 72 64 20 74  npacked record t
1b25c 68 61 74 20 77 65 20 77 69 6c 6c 20 72 65 74 75  hat we will retu
1b25d 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  rn */.  int nByt
1b25e 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  e;          /* M
1b25f 65 6d 6f 72 79 20 73 70 61 63 65 20 6e 65 65 64  emory space need
1b260 65 64 20 74 6f 20 68 6f 6c 64 20 70 2c 20 69 6e  ed to hold p, in
1b261 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20   bytes */.  int 
1b262 64 3b 0a 20 20 75 33 32 20 69 64 78 3b 0a 20 20  d;.  u32 idx;.  
1b263 75 31 36 20 75 3b 20 20 20 20 20 20 20 20 20 20  u16 u;          
1b264 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20      /* Unsigned 
1b265 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  loop counter */.
1b266 20 20 75 33 32 20 73 7a 48 64 72 3b 0a 20 20 4d    u32 szHdr;.  M
1b267 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74 20  em *pMem;.  int 
1b268 6e 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20  nOff;           
1b269 2f 2a 20 49 6e 63 72 65 61 73 65 20 70 53 70 61  /* Increase pSpa
1b26a 63 65 20 62 79 20 74 68 69 73 20 6d 75 63 68 20  ce by this much 
1b26b 74 6f 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 20  to 8-byte align 
1b26c 69 74 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20  it */.  .  /*.  
1b26d 2a 2a 20 57 65 20 77 61 6e 74 20 74 6f 20 73 68  ** We want to sh
1b26e 69 66 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20  ift the pointer 
1b26f 70 53 70 61 63 65 20 75 70 20 73 75 63 68 20 74  pSpace up such t
1b270 68 61 74 20 69 74 20 69 73 20 38 2d 62 79 74 65  hat it is 8-byte
1b271 20 61 6c 69 67 6e 65 64 2e 0a 20 20 2a 2a 20 54   aligned..  ** T
1b272 68 75 73 2c 20 77 65 20 6e 65 65 64 20 74 6f 20  hus, we need to 
1b273 63 61 6c 63 75 6c 61 74 65 20 61 20 76 61 6c 75  calculate a valu
1b274 65 2c 20 6e 4f 66 66 2c 20 62 65 74 77 65 65 6e  e, nOff, between
1b275 20 30 20 61 6e 64 20 37 2c 20 74 6f 20 73 68 69   0 and 7, to shi
1b276 66 74 20 0a 20 20 2a 2a 20 69 74 20 62 79 2e 20  ft .  ** it by. 
1b277 20 49 66 20 70 53 70 61 63 65 20 69 73 20 61 6c   If pSpace is al
1b278 72 65 61 64 79 20 38 2d 62 79 74 65 20 61 6c 69  ready 8-byte ali
1b279 67 6e 65 64 2c 20 6e 4f 66 66 20 73 68 6f 75 6c  gned, nOff shoul
1b27a 64 20 62 65 20 7a 65 72 6f 2e 0a 20 20 2a 2f 0a  d be zero..  */.
1b27b 20 20 6e 4f 66 66 20 3d 20 28 38 20 2d 20 28 53    nOff = (8 - (S
1b27c 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54  QLITE_PTR_TO_INT
1b27d 28 70 53 70 61 63 65 29 20 26 20 37 29 29 20 26  (pSpace) & 7)) &
1b27e 20 37 3b 0a 20 20 70 53 70 61 63 65 20 2b 3d 20   7;.  pSpace += 
1b27f 6e 4f 66 66 3b 0a 20 20 73 7a 53 70 61 63 65 20  nOff;.  szSpace 
1b280 2d 3d 20 6e 4f 66 66 3b 0a 20 20 6e 42 79 74 65  -= nOff;.  nByte
1b281 20 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66   = ROUND8(sizeof
1b282 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 29  (UnpackedRecord)
1b283 29 20 2b 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a  ) + sizeof(Mem)*
1b284 28 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c  (pKeyInfo->nFiel
1b285 64 2b 31 29 3b 0a 20 20 69 66 28 20 6e 42 79 74  d+1);.  if( nByt
1b286 65 3e 73 7a 53 70 61 63 65 20 29 7b 0a 20 20 20  e>szSpace ){.   
1b287 20 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61   p = sqlite3DbMa
1b288 6c 6c 6f 63 52 61 77 28 70 4b 65 79 49 6e 66 6f  llocRaw(pKeyInfo
1b289 2d 3e 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20  ->db, nByte);.  
1b28a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
1b28b 75 72 6e 20 30 3b 0a 20 20 20 20 70 2d 3e 66 6c  urn 0;.    p->fl
1b28c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 4e  ags = UNPACKED_N
1b28d 45 45 44 5f 46 52 45 45 20 7c 20 55 4e 50 41 43  EED_FREE | UNPAC
1b28e 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59  KED_NEED_DESTROY
1b28f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
1b290 20 3d 20 28 55 6e 70 61 63 6b 65 64 52 65 63 6f   = (UnpackedReco
1b291 72 64 2a 29 70 53 70 61 63 65 3b 0a 20 20 20 20  rd*)pSpace;.    
1b292 70 2d 3e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43  p->flags = UNPAC
1b293 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59  KED_NEED_DESTROY
1b294 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4b 65 79 49  ;.  }.  p->pKeyI
1b295 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a  nfo = pKeyInfo;.
1b296 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 70 4b    p->nField = pK
1b297 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b  eyInfo->nField +
1b298 20 31 3b 0a 20 20 70 2d 3e 61 4d 65 6d 20 3d 20   1;.  p->aMem = 
1b299 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29 26 28 28  pMem = (Mem*)&((
1b29a 63 68 61 72 2a 29 70 29 5b 52 4f 55 4e 44 38 28  char*)p)[ROUND8(
1b29b 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52  sizeof(UnpackedR
1b29c 65 63 6f 72 64 29 29 5d 3b 0a 20 20 61 73 73 65  ecord))];.  asse
1b29d 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41  rt( EIGHT_BYTE_A
1b29e 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29  LIGNMENT(pMem) )
1b29f 3b 0a 20 20 69 64 78 20 3d 20 67 65 74 56 61 72  ;.  idx = getVar
1b2a0 69 6e 74 33 32 28 61 4b 65 79 2c 20 73 7a 48 64  int32(aKey, szHd
1b2a1 72 29 3b 0a 20 20 64 20 3d 20 73 7a 48 64 72 3b  r);.  d = szHdr;
1b2a2 0a 20 20 75 20 3d 20 30 3b 0a 20 20 77 68 69 6c  .  u = 0;.  whil
1b2a3 65 28 20 69 64 78 3c 73 7a 48 64 72 20 26 26 20  e( idx<szHdr && 
1b2a4 75 3c 70 2d 3e 6e 46 69 65 6c 64 20 26 26 20 64  u<p->nField && d
1b2a5 3c 3d 6e 4b 65 79 20 29 7b 0a 20 20 20 20 75 33  <=nKey ){.    u3
1b2a6 32 20 73 65 72 69 61 6c 5f 74 79 70 65 3b 0a 0a  2 serial_type;..
1b2a7 20 20 20 20 69 64 78 20 2b 3d 20 67 65 74 56 61      idx += getVa
1b2a8 72 69 6e 74 33 32 28 26 61 4b 65 79 5b 69 64 78  rint32(&aKey[idx
1b2a9 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 29 3b  ], serial_type);
1b2aa 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d  .    pMem->enc =
1b2ab 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a   pKeyInfo->enc;.
1b2ac 20 20 20 20 70 4d 65 6d 2d 3e 64 62 20 3d 20 70      pMem->db = p
1b2ad 4b 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 20  KeyInfo->db;.   
1b2ae 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 30   pMem->flags = 0
1b2af 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c  ;.    pMem->zMal
1b2b0 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 64 20 2b  loc = 0;.    d +
1b2b1 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  = sqlite3VdbeSer
1b2b2 69 61 6c 47 65 74 28 26 61 4b 65 79 5b 64 5d 2c  ialGet(&aKey[d],
1b2b3 20 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 70 4d   serial_type, pM
1b2b4 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b  em);.    pMem++;
1b2b5 0a 20 20 20 20 75 2b 2b 3b 0a 20 20 7d 0a 20 20  .    u++;.  }.  
1b2b6 61 73 73 65 72 74 28 20 75 3c 3d 70 4b 65 79 49  assert( u<=pKeyI
1b2b7 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 20  nfo->nField + 1 
1b2b8 29 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d  );.  p->nField =
1b2b9 20 75 3b 0a 20 20 72 65 74 75 72 6e 20 28 76 6f   u;.  return (vo
1b2ba 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  id*)p;.}../*.** 
1b2bb 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 73  This routine des
1b2bc 74 72 6f 79 73 20 61 20 55 6e 70 61 63 6b 65 64  troys a Unpacked
1b2bd 52 65 63 6f 72 64 20 6f 62 6a 65 63 74 2e 0a 2a  Record object..*
1b2be 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1b2bf 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
1b2c0 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52  eDeleteUnpackedR
1b2c1 65 63 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 65  ecord(UnpackedRe
1b2c2 63 6f 72 64 20 2a 70 29 7b 0a 20 20 69 6e 74 20  cord *p){.  int 
1b2c3 69 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a  i;.  Mem *pMem;.
1b2c4 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20  .  assert( p!=0 
1b2c5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
1b2c6 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44  flags & UNPACKED
1b2c7 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 29 3b  _NEED_DESTROY );
1b2c8 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 4d 65 6d  .  for(i=0, pMem
1b2c9 3d 70 2d 3e 61 4d 65 6d 3b 20 69 3c 70 2d 3e 6e  =p->aMem; i<p->n
1b2ca 46 69 65 6c 64 3b 20 69 2b 2b 2c 20 70 4d 65 6d  Field; i++, pMem
1b2cb 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  ++){.    /* The 
1b2cc 75 6e 70 61 63 6b 65 64 20 72 65 63 6f 72 64 20  unpacked record 
1b2cd 69 73 20 61 6c 77 61 79 73 20 63 6f 6e 73 74 72  is always constr
1b2ce 75 63 74 65 64 20 62 79 20 74 68 65 0a 20 20 20  ucted by the.   
1b2cf 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 55   ** sqlite3VdbeU
1b2d0 6e 70 61 63 6b 52 65 63 6f 72 64 28 29 20 66 75  npackRecord() fu
1b2d1 6e 63 74 69 6f 6e 20 61 62 6f 76 65 2c 20 77 68  nction above, wh
1b2d2 69 63 68 20 6d 61 6b 65 73 20 61 6c 6c 0a 20 20  ich makes all.  
1b2d3 20 20 2a 2a 20 73 74 72 69 6e 67 73 20 61 6e 64    ** strings and
1b2d4 20 62 6c 6f 62 73 20 73 74 61 74 69 63 2e 20 20   blobs static.  
1b2d5 41 6e 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20  And none of the 
1b2d6 65 6c 65 6d 65 6e 74 73 20 61 72 65 0a 20 20 20  elements are.   
1b2d7 20 2a 2a 20 65 76 65 72 20 74 72 61 6e 73 66 6f   ** ever transfo
1b2d8 72 6d 65 64 2c 20 73 6f 20 74 68 65 72 65 20 69  rmed, so there i
1b2d9 73 20 6e 65 76 65 72 20 61 6e 79 74 68 69 6e 67  s never anything
1b2da 20 74 6f 20 64 65 6c 65 74 65 2e 0a 20 20 20 20   to delete..    
1b2db 2a 2f 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52  */.    if( NEVER
1b2dc 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 20  (pMem->zMalloc) 
1b2dd 29 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  ) sqlite3VdbeMem
1b2de 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20  Release(pMem);. 
1b2df 20 7d 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67   }.  if( p->flag
1b2e0 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45  s & UNPACKED_NEE
1b2e1 44 5f 46 52 45 45 20 29 7b 0a 20 20 20 20 73 71  D_FREE ){.    sq
1b2e2 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 70  lite3DbFree(p->p
1b2e3 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 70 29 3b  KeyInfo->db, p);
1b2e4 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
1b2e5 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 70  is function comp
1b2e6 61 72 65 73 20 74 68 65 20 74 77 6f 20 74 61 62  ares the two tab
1b2e7 6c 65 20 72 6f 77 73 20 6f 72 20 69 6e 64 65 78  le rows or index
1b2e8 20 72 65 63 6f 72 64 73 0a 2a 2a 20 73 70 65 63   records.** spec
1b2e9 69 66 69 65 64 20 62 79 20 7b 6e 4b 65 79 31 2c  ified by {nKey1,
1b2ea 20 70 4b 65 79 31 7d 20 61 6e 64 20 70 50 4b 65   pKey1} and pPKe
1b2eb 79 32 2e 20 20 49 74 20 72 65 74 75 72 6e 73 20  y2.  It returns 
1b2ec 61 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f  a negative, zero
1b2ed 0a 2a 2a 20 6f 72 20 70 6f 73 69 74 69 76 65 20  .** or positive 
1b2ee 69 6e 74 65 67 65 72 20 69 66 20 6b 65 79 31 20  integer if key1 
1b2ef 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71  is less than, eq
1b2f0 75 61 6c 20 74 6f 20 6f 72 20 0a 2a 2a 20 67 72  ual to or .** gr
1b2f1 65 61 74 65 72 20 74 68 61 6e 20 6b 65 79 32 2e  eater than key2.
1b2f2 20 20 54 68 65 20 7b 6e 4b 65 79 31 2c 20 70 4b    The {nKey1, pK
1b2f3 65 79 31 7d 20 6b 65 79 20 6d 75 73 74 20 62 65  ey1} key must be
1b2f4 20 61 20 62 6c 6f 62 0a 2a 2a 20 63 72 65 61 74   a blob.** creat
1b2f5 65 64 20 62 79 20 74 68 20 4f 50 5f 4d 61 6b 65  ed by th OP_Make
1b2f6 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 6f 66  Record opcode of
1b2f7 20 74 68 65 20 56 44 42 45 2e 20 20 54 68 65 20   the VDBE.  The 
1b2f8 70 50 4b 65 79 32 0a 2a 2a 20 6b 65 79 20 6d 75  pPKey2.** key mu
1b2f9 73 74 20 62 65 20 61 20 70 61 72 73 65 64 20 6b  st be a parsed k
1b2fa 65 79 20 73 75 63 68 20 61 73 20 6f 62 74 61 69  ey such as obtai
1b2fb 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69  ned from.** sqli
1b2fc 74 65 33 56 64 62 65 50 61 72 73 65 52 65 63 6f  te3VdbeParseReco
1b2fd 72 64 2e 0a 2a 2a 0a 2a 2a 20 4b 65 79 31 20 61  rd..**.** Key1 a
1b2fe 6e 64 20 4b 65 79 32 20 64 6f 20 6e 6f 74 20 68  nd Key2 do not h
1b2ff 61 76 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74  ave to contain t
1b300 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f  he same number o
1b301 66 20 66 69 65 6c 64 73 2e 0a 2a 2a 20 54 68 65  f fields..** The
1b302 20 6b 65 79 20 77 69 74 68 20 66 65 77 65 72 20   key with fewer 
1b303 66 69 65 6c 64 73 20 69 73 20 75 73 75 61 6c 6c  fields is usuall
1b304 79 20 63 6f 6d 70 61 72 65 73 20 6c 65 73 73 20  y compares less 
1b305 74 68 61 6e 20 74 68 65 20 0a 2a 2a 20 6c 6f 6e  than the .** lon
1b306 67 65 72 20 6b 65 79 2e 20 20 48 6f 77 65 76 65  ger key.  Howeve
1b307 72 20 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45  r if the UNPACKE
1b308 44 5f 49 4e 43 52 4b 45 59 20 66 6c 61 67 73 20  D_INCRKEY flags 
1b309 69 6e 20 70 50 4b 65 79 32 20 69 73 20 73 65 74  in pPKey2 is set
1b30a 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 6f 6d 6d  .** and the comm
1b30b 6f 6e 20 70 72 65 66 69 78 65 73 20 61 72 65 20  on prefixes are 
1b30c 65 71 75 61 6c 2c 20 74 68 65 6e 20 6b 65 79 31  equal, then key1
1b30d 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6b 65   is less than ke
1b30e 79 32 2e 0a 2a 2a 20 4f 72 20 69 66 20 74 68 65  y2..** Or if the
1b30f 20 55 4e 50 41 43 4b 45 44 5f 4d 41 54 43 48 5f   UNPACKED_MATCH_
1b310 50 52 45 46 49 58 20 66 6c 61 67 20 69 73 20 73  PREFIX flag is s
1b311 65 74 20 61 6e 64 20 74 68 65 20 70 72 65 66 69  et and the prefi
1b312 78 65 73 20 61 72 65 0a 2a 2a 20 65 71 75 61 6c  xes are.** equal
1b313 2c 20 74 68 65 6e 20 74 68 65 20 6b 65 79 73 20  , then the keys 
1b314 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74  are considered t
1b315 6f 20 62 65 20 65 71 75 61 6c 20 61 6e 64 0a 2a  o be equal and.*
1b316 2a 20 74 68 65 20 70 61 72 74 73 20 62 65 79 6f  * the parts beyo
1b317 6e 64 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72  nd the common pr
1b318 65 66 69 78 20 61 72 65 20 69 67 6e 6f 72 65 64  efix are ignored
1b319 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 55  ..**.** If the U
1b31a 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52  NPACKED_IGNORE_R
1b31b 4f 57 49 44 20 66 6c 61 67 20 69 73 20 73 65 74  OWID flag is set
1b31c 2c 20 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20  , then the last 
1b31d 62 79 74 65 20 6f 66 0a 2a 2a 20 74 68 65 20 68  byte of.** the h
1b31e 65 61 64 65 72 20 6f 66 20 70 4b 65 79 31 20 69  eader of pKey1 i
1b31f 73 20 69 67 6e 6f 72 65 64 2e 20 20 49 74 20 69  s ignored.  It i
1b320 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 70  s assumed that p
1b321 4b 65 79 31 20 69 73 0a 2a 2a 20 61 6e 20 69 6e  Key1 is.** an in
1b322 64 65 78 20 6b 65 79 2c 20 61 6e 64 20 74 68 75  dex key, and thu
1b323 73 20 65 6e 64 73 20 77 69 74 68 20 61 20 72 6f  s ends with a ro
1b324 77 69 64 20 76 61 6c 75 65 2e 20 20 54 68 65 20  wid value.  The 
1b325 6c 61 73 74 20 62 79 74 65 0a 2a 2a 20 6f 66 20  last byte.** of 
1b326 74 68 65 20 68 65 61 64 65 72 20 77 69 6c 6c 20  the header will 
1b327 74 68 65 72 65 66 6f 72 65 20 62 65 20 74 68 65  therefore be the
1b328 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20   serial type of 
1b329 74 68 65 20 72 6f 77 69 64 3a 0a 2a 2a 20 6f 6e  the rowid:.** on
1b32a 65 20 6f 66 20 31 2c 20 32 2c 20 33 2c 20 34 2c  e of 1, 2, 3, 4,
1b32b 20 35 2c 20 36 2c 20 38 2c 20 6f 72 20 39 20 2d   5, 6, 8, or 9 -
1b32c 20 74 68 65 20 69 6e 74 65 67 65 72 20 73 65 72   the integer ser
1b32d 69 61 6c 20 74 79 70 65 73 2e 0a 2a 2a 20 54 68  ial types..** Th
1b32e 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66  e serial type of
1b32f 20 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 64   the final rowid
1b330 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20   will always be 
1b331 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a  a single byte..*
1b332 2a 20 42 79 20 69 67 6e 6f 72 69 6e 67 20 74 68  * By ignoring th
1b333 69 73 20 6c 61 73 74 20 62 79 74 65 20 6f 66 20  is last byte of 
1b334 74 68 65 20 68 65 61 64 65 72 2c 20 77 65 20 66  the header, we f
1b335 6f 72 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69  orce the compari
1b336 73 6f 6e 0a 2a 2a 20 74 6f 20 69 67 6e 6f 72 65  son.** to ignore
1b337 20 74 68 65 20 72 6f 77 69 64 20 61 74 20 74 68   the rowid at th
1b338 65 20 65 6e 64 20 6f 66 20 6b 65 79 31 2e 0a 2a  e end of key1..*
1b339 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1b33a 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
1b33b 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 0a 20  RecordCompare(. 
1b33c 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73   int nKey1, cons
1b33d 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 2f  t void *pKey1, /
1b33e 2a 20 4c 65 66 74 20 6b 65 79 20 2a 2f 0a 20 20  * Left key */.  
1b33f 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a  UnpackedRecord *
1b340 70 50 4b 65 79 32 20 20 20 20 20 20 20 20 2f 2a  pPKey2        /*
1b341 20 52 69 67 68 74 20 6b 65 79 20 2a 2f 0a 29 7b   Right key */.){
1b342 0a 20 20 69 6e 74 20 64 31 3b 20 20 20 20 20 20  .  int d1;      
1b343 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
1b344 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e  into aKey[] of n
1b345 65 78 74 20 64 61 74 61 20 65 6c 65 6d 65 6e 74  ext data element
1b346 20 2a 2f 0a 20 20 75 33 32 20 69 64 78 31 3b 20   */.  u32 idx1; 
1b347 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
1b348 65 74 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f  et into aKey[] o
1b349 66 20 6e 65 78 74 20 68 65 61 64 65 72 20 65 6c  f next header el
1b34a 65 6d 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20 73  ement */.  u32 s
1b34b 7a 48 64 72 31 3b 20 20 20 20 20 20 20 20 2f 2a  zHdr1;        /*
1b34c 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
1b34d 20 69 6e 20 68 65 61 64 65 72 20 2a 2f 0a 20 20   in header */.  
1b34e 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 69 6e 74  int i = 0;.  int
1b34f 20 6e 46 69 65 6c 64 3b 0a 20 20 69 6e 74 20 72   nField;.  int r
1b350 63 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75  c = 0;.  const u
1b351 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4b  nsigned char *aK
1b352 65 79 31 20 3d 20 28 63 6f 6e 73 74 20 75 6e 73  ey1 = (const uns
1b353 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 4b 65  igned char *)pKe
1b354 79 31 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70  y1;.  KeyInfo *p
1b355 4b 65 79 49 6e 66 6f 3b 0a 20 20 4d 65 6d 20 6d  KeyInfo;.  Mem m
1b356 65 6d 31 3b 0a 0a 20 20 70 4b 65 79 49 6e 66 6f  em1;..  pKeyInfo
1b357 20 3d 20 70 50 4b 65 79 32 2d 3e 70 4b 65 79 49   = pPKey2->pKeyI
1b358 6e 66 6f 3b 0a 20 20 6d 65 6d 31 2e 65 6e 63 20  nfo;.  mem1.enc 
1b359 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b  = pKeyInfo->enc;
1b35a 0a 20 20 6d 65 6d 31 2e 64 62 20 3d 20 70 4b 65  .  mem1.db = pKe
1b35b 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 6d 65 6d  yInfo->db;.  mem
1b35c 31 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 6d  1.flags = 0;.  m
1b35d 65 6d 31 2e 75 2e 69 20 3d 20 30 3b 20 20 2f 2a  em1.u.i = 0;  /*
1b35e 20 6e 6f 74 20 6e 65 65 64 65 64 2c 20 68 65 72   not needed, her
1b35f 65 20 74 6f 20 73 69 6c 65 6e 63 65 20 63 6f 6d  e to silence com
1b360 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 20 2a 2f  piler warning */
1b361 0a 20 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 20  .  mem1.zMalloc 
1b362 3d 20 30 3b 0a 20 20 0a 20 20 69 64 78 31 20 3d  = 0;.  .  idx1 =
1b363 20 67 65 74 56 61 72 69 6e 74 33 32 28 61 4b 65   getVarint32(aKe
1b364 79 31 2c 20 73 7a 48 64 72 31 29 3b 0a 20 20 64  y1, szHdr1);.  d
1b365 31 20 3d 20 73 7a 48 64 72 31 3b 0a 20 20 69 66  1 = szHdr1;.  if
1b366 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20  ( pPKey2->flags 
1b367 26 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52  & UNPACKED_IGNOR
1b368 45 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 73  E_ROWID ){.    s
1b369 7a 48 64 72 31 2d 2d 3b 0a 20 20 7d 0a 20 20 6e  zHdr1--;.  }.  n
1b36a 46 69 65 6c 64 20 3d 20 70 4b 65 79 49 6e 66 6f  Field = pKeyInfo
1b36b 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 77 68 69 6c  ->nField;.  whil
1b36c 65 28 20 69 64 78 31 3c 73 7a 48 64 72 31 20 26  e( idx1<szHdr1 &
1b36d 26 20 69 3c 70 50 4b 65 79 32 2d 3e 6e 46 69 65  & i<pPKey2->nFie
1b36e 6c 64 20 29 7b 0a 20 20 20 20 75 33 32 20 73 65  ld ){.    u32 se
1b36f 72 69 61 6c 5f 74 79 70 65 31 3b 0a 0a 20 20 20  rial_type1;..   
1b370 20 2f 2a 20 52 65 61 64 20 74 68 65 20 73 65 72   /* Read the ser
1b371 69 61 6c 20 74 79 70 65 73 20 66 6f 72 20 74 68  ial types for th
1b372 65 20 6e 65 78 74 20 65 6c 65 6d 65 6e 74 20 69  e next element i
1b373 6e 20 65 61 63 68 20 6b 65 79 2e 20 2a 2f 0a 20  n each key. */. 
1b374 20 20 20 69 64 78 31 20 2b 3d 20 67 65 74 56 61     idx1 += getVa
1b375 72 69 6e 74 33 32 28 20 61 4b 65 79 31 2b 69 64  rint32( aKey1+id
1b376 78 31 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 31  x1, serial_type1
1b377 20 29 3b 0a 20 20 20 20 69 66 28 20 64 31 3e 3d   );.    if( d1>=
1b378 6e 4b 65 79 31 20 26 26 20 73 71 6c 69 74 65 33  nKey1 && sqlite3
1b379 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65  VdbeSerialTypeLe
1b37a 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 31 29 3e  n(serial_type1)>
1b37b 30 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20  0 ) break;..    
1b37c 2f 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 76  /* Extract the v
1b37d 61 6c 75 65 73 20 74 6f 20 62 65 20 63 6f 6d 70  alues to be comp
1b37e 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ared..    */.   
1b37f 20 64 31 20 2b 3d 20 73 71 6c 69 74 65 33 56 64   d1 += sqlite3Vd
1b380 62 65 53 65 72 69 61 6c 47 65 74 28 26 61 4b 65  beSerialGet(&aKe
1b381 79 31 5b 64 31 5d 2c 20 73 65 72 69 61 6c 5f 74  y1[d1], serial_t
1b382 79 70 65 31 2c 20 26 6d 65 6d 31 29 3b 0a 0a 20  ype1, &mem1);.. 
1b383 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d     /* Do the com
1b384 70 61 72 69 73 6f 6e 0a 20 20 20 20 2a 2f 0a 20  parison.    */. 
1b385 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4d     rc = sqlite3M
1b386 65 6d 43 6f 6d 70 61 72 65 28 26 6d 65 6d 31 2c  emCompare(&mem1,
1b387 20 26 70 50 4b 65 79 32 2d 3e 61 4d 65 6d 5b 69   &pPKey2->aMem[i
1b388 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ],.             
1b389 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 3c                i<
1b38a 6e 46 69 65 6c 64 20 3f 20 70 4b 65 79 49 6e 66  nField ? pKeyInf
1b38b 6f 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3a 20 30 29  o->aColl[i] : 0)
1b38c 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 30 20  ;.    if( rc!=0 
1b38d 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ){.      break;.
1b38e 20 20 20 20 7d 0a 20 20 20 20 69 2b 2b 3b 0a 20      }.    i++;. 
1b38f 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 20 6d 65 6d 6f   }..  /* No memo
1b390 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73  ry allocation is
1b391 20 65 76 65 72 20 75 73 65 64 20 6f 6e 20 6d 65   ever used on me
1b392 6d 31 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56  m1. */.  if( NEV
1b393 45 52 28 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63 29  ER(mem1.zMalloc)
1b394 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   ) sqlite3VdbeMe
1b395 6d 52 65 6c 65 61 73 65 28 26 6d 65 6d 31 29 3b  mRelease(&mem1);
1b396 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 50 52  ..  /* If the PR
1b397 45 46 49 58 5f 53 45 41 52 43 48 20 66 6c 61 67  EFIX_SEARCH flag
1b398 20 69 73 20 73 65 74 20 61 6e 64 20 61 6c 6c 20   is set and all 
1b399 66 69 65 6c 64 73 20 65 78 63 65 70 74 20 74 68  fields except th
1b39a 65 20 66 69 6e 61 6c 0a 20 20 2a 2a 20 72 6f 77  e final.  ** row
1b39b 69 64 20 66 69 65 6c 64 20 77 65 72 65 20 65 71  id field were eq
1b39c 75 61 6c 2c 20 74 68 65 6e 20 63 6c 65 61 72 20  ual, then clear 
1b39d 74 68 65 20 50 52 45 46 49 58 5f 53 45 41 52 43  the PREFIX_SEARC
1b39e 48 20 66 6c 61 67 20 61 6e 64 20 73 65 74 20 0a  H flag and set .
1b39f 20 20 2a 2a 20 70 50 4b 65 79 32 2d 3e 72 6f 77    ** pPKey2->row
1b3a0 69 64 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  id to the value 
1b3a1 6f 66 20 74 68 65 20 72 6f 77 69 64 20 66 69 65  of the rowid fie
1b3a2 6c 64 20 69 6e 20 28 70 4b 65 79 31 2c 20 6e 4b  ld in (pKey1, nK
1b3a3 65 79 31 29 2e 0a 20 20 2a 2a 20 54 68 69 73 20  ey1)..  ** This 
1b3a4 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 4f  is used by the O
1b3a5 50 5f 49 73 55 6e 69 71 75 65 20 6f 70 63 6f 64  P_IsUnique opcod
1b3a6 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70  e..  */.  if( (p
1b3a7 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55  PKey2->flags & U
1b3a8 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53  NPACKED_PREFIX_S
1b3a9 45 41 52 43 48 29 20 26 26 20 69 3d 3d 28 70 50  EARCH) && i==(pP
1b3aa 4b 65 79 32 2d 3e 6e 46 69 65 6c 64 2d 31 29 20  Key2->nField-1) 
1b3ab 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  ){.    assert( i
1b3ac 64 78 31 3d 3d 73 7a 48 64 72 31 20 26 26 20 72  dx1==szHdr1 && r
1b3ad 63 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  c );.    assert(
1b3ae 20 6d 65 6d 31 2e 66 6c 61 67 73 20 26 20 4d 45   mem1.flags & ME
1b3af 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 70 50 4b  M_Int );.    pPK
1b3b0 65 79 32 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 55  ey2->flags &= ~U
1b3b1 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53  NPACKED_PREFIX_S
1b3b2 45 41 52 43 48 3b 0a 20 20 20 20 70 50 4b 65 79  EARCH;.    pPKey
1b3b3 32 2d 3e 72 6f 77 69 64 20 3d 20 6d 65 6d 31 2e  2->rowid = mem1.
1b3b4 75 2e 69 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  u.i;.  }..  if( 
1b3b5 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  rc==0 ){.    /* 
1b3b6 72 63 3d 3d 30 20 68 65 72 65 20 6d 65 61 6e 73  rc==0 here means
1b3b7 20 74 68 61 74 20 6f 6e 65 20 6f 66 20 74 68 65   that one of the
1b3b8 20 6b 65 79 73 20 72 61 6e 20 6f 75 74 20 6f 66   keys ran out of
1b3b9 20 66 69 65 6c 64 73 20 61 6e 64 0a 20 20 20 20   fields and.    
1b3ba 2a 2a 20 61 6c 6c 20 74 68 65 20 66 69 65 6c 64  ** all the field
1b3bb 73 20 75 70 20 74 6f 20 74 68 61 74 20 70 6f 69  s up to that poi
1b3bc 6e 74 20 77 65 72 65 20 65 71 75 61 6c 2e 20 49  nt were equal. I
1b3bd 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49  f the UNPACKED_I
1b3be 4e 43 52 4b 45 59 0a 20 20 20 20 2a 2a 20 66 6c  NCRKEY.    ** fl
1b3bf 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20  ag is set, then 
1b3c0 62 72 65 61 6b 20 74 68 65 20 74 69 65 20 62 79  break the tie by
1b3c1 20 74 72 65 61 74 69 6e 67 20 6b 65 79 32 20 61   treating key2 a
1b3c2 73 20 6c 61 72 67 65 72 2e 0a 20 20 20 20 2a 2a  s larger..    **
1b3c3 20 49 66 20 74 68 65 20 55 50 41 43 4b 45 44 5f   If the UPACKED_
1b3c4 50 52 45 46 49 58 5f 4d 41 54 43 48 20 66 6c 61  PREFIX_MATCH fla
1b3c5 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 6b  g is set, then k
1b3c6 65 79 73 20 77 69 74 68 20 63 6f 6d 6d 6f 6e 20  eys with common 
1b3c7 70 72 65 66 69 78 65 73 0a 20 20 20 20 2a 2a 20  prefixes.    ** 
1b3c8 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74  are considered t
1b3c9 6f 20 62 65 20 65 71 75 61 6c 2e 20 20 4f 74 68  o be equal.  Oth
1b3ca 65 72 77 69 73 65 2c 20 74 68 65 20 6c 6f 6e 67  erwise, the long
1b3cb 65 72 20 6b 65 79 20 69 73 20 74 68 65 20 0a 20  er key is the . 
1b3cc 20 20 20 2a 2a 20 6c 61 72 67 65 72 2e 20 20 41     ** larger.  A
1b3cd 73 20 69 74 20 68 61 70 70 65 6e 73 2c 20 74 68  s it happens, th
1b3ce 65 20 70 50 4b 65 79 32 20 77 69 6c 6c 20 61 6c  e pPKey2 will al
1b3cf 77 61 79 73 20 62 65 20 74 68 65 20 6c 6f 6e 67  ways be the long
1b3d0 65 72 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 65  er.    ** if the
1b3d1 72 65 20 69 73 20 61 20 64 69 66 66 65 72 65 6e  re is a differen
1b3d2 63 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ce..    */.    i
1b3d3 66 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73  f( pPKey2->flags
1b3d4 20 26 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52   & UNPACKED_INCR
1b3d5 4b 45 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20  KEY ){.      rc 
1b3d6 3d 20 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20  = -1;.    }else 
1b3d7 69 66 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67  if( pPKey2->flag
1b3d8 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 45  s & UNPACKED_PRE
1b3d9 46 49 58 5f 4d 41 54 43 48 20 29 7b 0a 20 20 20  FIX_MATCH ){.   
1b3da 20 20 20 2f 2a 20 4c 65 61 76 65 20 72 63 3d 3d     /* Leave rc==
1b3db 30 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 20 69  0 */.    }else i
1b3dc 66 28 20 69 64 78 31 3c 73 7a 48 64 72 31 20 29  f( idx1<szHdr1 )
1b3dd 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a  {.      rc = 1;.
1b3de 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
1b3df 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72  ( pKeyInfo->aSor
1b3e0 74 4f 72 64 65 72 20 26 26 20 69 3c 70 4b 65 79  tOrder && i<pKey
1b3e1 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 0a 20 20 20  Info->nField.   
1b3e2 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 70              && p
1b3e3 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72  KeyInfo->aSortOr
1b3e4 64 65 72 5b 69 5d 20 29 7b 0a 20 20 20 20 72 63  der[i] ){.    rc
1b3e5 20 3d 20 2d 72 63 3b 0a 20 20 7d 0a 0a 20 20 72   = -rc;.  }..  r
1b3e6 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 20 0a 0a 2f  eturn rc;.}. ../
1b3e7 2a 0a 2a 2a 20 70 43 75 72 20 70 6f 69 6e 74 73  *.** pCur points
1b3e8 20 61 74 20 61 6e 20 69 6e 64 65 78 20 65 6e 74   at an index ent
1b3e9 72 79 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  ry created using
1b3ea 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f   the OP_MakeReco
1b3eb 72 64 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 52 65  rd opcode..** Re
1b3ec 61 64 20 74 68 65 20 72 6f 77 69 64 20 28 74 68  ad the rowid (th
1b3ed 65 20 6c 61 73 74 20 66 69 65 6c 64 20 69 6e 20  e last field in 
1b3ee 74 68 65 20 72 65 63 6f 72 64 29 20 61 6e 64 20  the record) and 
1b3ef 73 74 6f 72 65 20 69 74 20 69 6e 20 2a 72 6f 77  store it in *row
1b3f0 69 64 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  id..** Return SQ
1b3f1 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79  LITE_OK if every
1b3f2 74 68 69 6e 67 20 77 6f 72 6b 73 2c 20 6f 72 20  thing works, or 
1b3f3 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74  an error code ot
1b3f4 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 70  herwise..**.** p
1b3f5 43 75 72 20 6d 69 67 68 74 20 62 65 20 70 6f 69  Cur might be poi
1b3f6 6e 74 69 6e 67 20 74 6f 20 74 65 78 74 20 6f 62  nting to text ob
1b3f7 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 20 63 6f  tained from a co
1b3f8 72 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66  rrupt database f
1b3f9 69 6c 65 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 63  ile..** So the c
1b3fa 6f 6e 74 65 6e 74 20 63 61 6e 6e 6f 74 20 62 65  ontent cannot be
1b3fb 20 74 72 75 73 74 65 64 2e 20 20 44 6f 20 61 70   trusted.  Do ap
1b3fc 70 72 6f 70 72 69 61 74 65 20 63 68 65 63 6b 73  propriate checks
1b3fd 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e   on the content.
1b3fe 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1b3ff 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
1b400 62 65 49 64 78 52 6f 77 69 64 28 73 71 6c 69 74  beIdxRowid(sqlit
1b401 65 33 20 2a 64 62 2c 20 42 74 43 75 72 73 6f 72  e3 *db, BtCursor
1b402 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 72 6f 77   *pCur, i64 *row
1b403 69 64 29 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c  id){.  i64 nCell
1b404 4b 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  Key = 0;.  int r
1b405 63 3b 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 20  c;.  u32 szHdr; 
1b406 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
1b407 66 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a  f the header */.
1b408 20 20 75 33 32 20 74 79 70 65 52 6f 77 69 64 3b    u32 typeRowid;
1b409 20 20 20 20 2f 2a 20 53 65 72 69 61 6c 20 74 79      /* Serial ty
1b40a 70 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20  pe of the rowid 
1b40b 2a 2f 0a 20 20 75 33 32 20 6c 65 6e 52 6f 77 69  */.  u32 lenRowi
1b40c 64 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f  d;     /* Size o
1b40d 66 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20  f the rowid */. 
1b40e 20 4d 65 6d 20 6d 2c 20 76 3b 0a 0a 20 20 55 4e   Mem m, v;..  UN
1b40f 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64  USED_PARAMETER(d
1b410 62 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68  b);..  /* Get th
1b411 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 69 6e  e size of the in
1b412 64 65 78 20 65 6e 74 72 79 2e 20 20 4f 6e 6c 79  dex entry.  Only
1b413 20 69 6e 64 69 63 65 73 20 65 6e 74 72 69 65 73   indices entries
1b414 20 6f 66 20 6c 65 73 73 0a 20 20 2a 2a 20 74 68   of less.  ** th
1b415 61 6e 20 32 47 69 42 20 61 72 65 20 73 75 70 70  an 2GiB are supp
1b416 6f 72 74 20 2d 20 61 6e 79 74 68 69 6e 67 20 6c  ort - anything l
1b417 61 72 67 65 20 6d 75 73 74 20 62 65 20 64 61 74  arge must be dat
1b418 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e  abase corruption
1b419 2e 0a 20 20 2a 2a 20 41 6e 79 20 63 6f 72 72 75  ..  ** Any corru
1b41a 70 74 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65  ption is detecte
1b41b 64 20 69 6e 20 73 71 6c 69 74 65 33 42 74 72 65  d in sqlite3Btre
1b41c 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 2c  eParseCellPtr(),
1b41d 20 74 68 6f 75 67 68 2c 20 73 6f 0a 20 20 2a 2a   though, so.  **
1b41e 20 74 68 69 73 20 63 6f 64 65 20 63 61 6e 20 73   this code can s
1b41f 61 66 65 6c 79 20 61 73 73 75 6d 65 20 74 68 61  afely assume tha
1b420 74 20 6e 43 65 6c 6c 4b 65 79 20 69 73 20 33 32  t nCellKey is 32
1b421 2d 62 69 74 73 20 20 0a 20 20 2a 2f 0a 20 20 61  -bits  .  */.  a
1b422 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
1b423 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64  reeCursorIsValid
1b424 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d  (pCur) );.  rc =
1b425 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79   sqlite3BtreeKey
1b426 53 69 7a 65 28 70 43 75 72 2c 20 26 6e 43 65 6c  Size(pCur, &nCel
1b427 6c 4b 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28  lKey);.  assert(
1b428 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1b429 3b 20 20 20 20 20 2f 2a 20 70 43 75 72 20 69 73  ;     /* pCur is
1b42a 20 61 6c 77 61 79 73 20 76 61 6c 69 64 20 73 6f   always valid so
1b42b 20 4b 65 79 53 69 7a 65 20 63 61 6e 6e 6f 74 20   KeySize cannot 
1b42c 66 61 69 6c 20 2a 2f 0a 20 20 61 73 73 65 72 74  fail */.  assert
1b42d 28 20 28 6e 43 65 6c 6c 4b 65 79 20 26 20 53 51  ( (nCellKey & SQ
1b42e 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29 3d 3d 28  LITE_MAX_U32)==(
1b42f 75 36 34 29 6e 43 65 6c 6c 4b 65 79 20 29 3b 0a  u64)nCellKey );.
1b430 0a 20 20 2f 2a 20 52 65 61 64 20 69 6e 20 74 68  .  /* Read in th
1b431 65 20 63 6f 6d 70 6c 65 74 65 20 63 6f 6e 74 65  e complete conte
1b432 6e 74 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  nt of the index 
1b433 65 6e 74 72 79 20 2a 2f 0a 20 20 6d 65 6d 73 65  entry */.  memse
1b434 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28  t(&m, 0, sizeof(
1b435 6d 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  m));.  rc = sqli
1b436 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74  te3VdbeMemFromBt
1b437 72 65 65 28 70 43 75 72 2c 20 30 2c 20 28 69 6e  ree(pCur, 0, (in
1b438 74 29 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26  t)nCellKey, 1, &
1b439 6d 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a  m);.  if( rc ){.
1b43a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1b43b 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 69 6e 64   }..  /* The ind
1b43c 65 78 20 65 6e 74 72 79 20 6d 75 73 74 20 62 65  ex entry must be
1b43d 67 69 6e 20 77 69 74 68 20 61 20 68 65 61 64 65  gin with a heade
1b43e 72 20 73 69 7a 65 20 2a 2f 0a 20 20 28 76 6f 69  r size */.  (voi
1b43f 64 29 67 65 74 56 61 72 69 6e 74 33 32 28 28 75  d)getVarint32((u
1b440 38 2a 29 6d 2e 7a 2c 20 73 7a 48 64 72 29 3b 0a  8*)m.z, szHdr);.
1b441 20 20 74 65 73 74 63 61 73 65 28 20 73 7a 48 64    testcase( szHd
1b442 72 3d 3d 33 20 29 3b 0a 20 20 74 65 73 74 63 61  r==3 );.  testca
1b443 73 65 28 20 73 7a 48 64 72 3d 3d 6d 2e 6e 20 29  se( szHdr==m.n )
1b444 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79  ;.  if( unlikely
1b445 28 73 7a 48 64 72 3c 33 20 7c 7c 20 28 69 6e 74  (szHdr<3 || (int
1b446 29 73 7a 48 64 72 3e 6d 2e 6e 29 20 29 7b 0a 20  )szHdr>m.n) ){. 
1b447 20 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69     goto idx_rowi
1b448 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20  d_corruption;.  
1b449 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 6c 61 73 74  }..  /* The last
1b44a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 69 6e   field of the in
1b44b 64 65 78 20 73 68 6f 75 6c 64 20 62 65 20 61 6e  dex should be an
1b44c 20 69 6e 74 65 67 65 72 20 2d 20 74 68 65 20 52   integer - the R
1b44d 4f 57 49 44 2e 0a 20 20 2a 2a 20 56 65 72 69 66  OWID..  ** Verif
1b44e 79 20 74 68 61 74 20 74 68 65 20 6c 61 73 74 20  y that the last 
1b44f 65 6e 74 72 79 20 72 65 61 6c 6c 79 20 69 73 20  entry really is 
1b450 61 6e 20 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 20  an integer. */. 
1b451 20 28 76 6f 69 64 29 67 65 74 56 61 72 69 6e 74   (void)getVarint
1b452 33 32 28 28 75 38 2a 29 26 6d 2e 7a 5b 73 7a 48  32((u8*)&m.z[szH
1b453 64 72 2d 31 5d 2c 20 74 79 70 65 52 6f 77 69 64  dr-1], typeRowid
1b454 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 74  );.  testcase( t
1b455 79 70 65 52 6f 77 69 64 3d 3d 31 20 29 3b 0a 20  ypeRowid==1 );. 
1b456 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65 52   testcase( typeR
1b457 6f 77 69 64 3d 3d 32 20 29 3b 0a 20 20 74 65 73  owid==2 );.  tes
1b458 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 64  tcase( typeRowid
1b459 3d 3d 33 20 29 3b 0a 20 20 74 65 73 74 63 61 73  ==3 );.  testcas
1b45a 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 34 20  e( typeRowid==4 
1b45b 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 74  );.  testcase( t
1b45c 79 70 65 52 6f 77 69 64 3d 3d 35 20 29 3b 0a 20  ypeRowid==5 );. 
1b45d 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65 52   testcase( typeR
1b45e 6f 77 69 64 3d 3d 36 20 29 3b 0a 20 20 74 65 73  owid==6 );.  tes
1b45f 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 64  tcase( typeRowid
1b460 3d 3d 38 20 29 3b 0a 20 20 74 65 73 74 63 61 73  ==8 );.  testcas
1b461 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 39 20  e( typeRowid==9 
1b462 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c  );.  if( unlikel
1b463 79 28 74 79 70 65 52 6f 77 69 64 3c 31 20 7c 7c  y(typeRowid<1 ||
1b464 20 74 79 70 65 52 6f 77 69 64 3e 39 20 7c 7c 20   typeRowid>9 || 
1b465 74 79 70 65 52 6f 77 69 64 3d 3d 37 29 20 29 7b  typeRowid==7) ){
1b466 0a 20 20 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f  .    goto idx_ro
1b467 77 69 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0a  wid_corruption;.
1b468 20 20 7d 0a 20 20 6c 65 6e 52 6f 77 69 64 20 3d    }.  lenRowid =
1b469 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1b46a 61 6c 54 79 70 65 4c 65 6e 28 74 79 70 65 52 6f  alTypeLen(typeRo
1b46b 77 69 64 29 3b 0a 20 20 74 65 73 74 63 61 73 65  wid);.  testcase
1b46c 28 20 28 75 33 32 29 6d 2e 6e 3d 3d 73 7a 48 64  ( (u32)m.n==szHd
1b46d 72 2b 6c 65 6e 52 6f 77 69 64 20 29 3b 0a 20 20  r+lenRowid );.  
1b46e 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28 28 75 33  if( unlikely((u3
1b46f 32 29 6d 2e 6e 3c 73 7a 48 64 72 2b 6c 65 6e 52  2)m.n<szHdr+lenR
1b470 6f 77 69 64 29 20 29 7b 0a 20 20 20 20 67 6f 74  owid) ){.    got
1b471 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72  o idx_rowid_corr
1b472 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a 20 20 2f  uption;.  }..  /
1b473 2a 20 46 65 74 63 68 20 74 68 65 20 69 6e 74 65  * Fetch the inte
1b474 67 65 72 20 6f 66 66 20 74 68 65 20 65 6e 64 20  ger off the end 
1b475 6f 66 20 74 68 65 20 69 6e 64 65 78 20 72 65 63  of the index rec
1b476 6f 72 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ord */.  sqlite3
1b477 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 75  VdbeSerialGet((u
1b478 38 2a 29 26 6d 2e 7a 5b 6d 2e 6e 2d 6c 65 6e 52  8*)&m.z[m.n-lenR
1b479 6f 77 69 64 5d 2c 20 74 79 70 65 52 6f 77 69 64  owid], typeRowid
1b47a 2c 20 26 76 29 3b 0a 20 20 2a 72 6f 77 69 64 20  , &v);.  *rowid 
1b47b 3d 20 76 2e 75 2e 69 3b 0a 20 20 73 71 6c 69 74  = v.u.i;.  sqlit
1b47c 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
1b47d 28 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  (&m);.  return S
1b47e 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20  QLITE_OK;..  /* 
1b47f 4a 75 6d 70 20 68 65 72 65 20 69 66 20 64 61 74  Jump here if dat
1b480 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e  abase corruption
1b481 20 69 73 20 64 65 74 65 63 74 65 64 20 61 66 74   is detected aft
1b482 65 72 20 6d 20 68 61 73 20 62 65 65 6e 0a 20 20  er m has been.  
1b483 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46  ** allocated.  F
1b484 72 65 65 20 74 68 65 20 6d 20 6f 62 6a 65 63 74  ree the m object
1b485 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49   and return SQLI
1b486 54 45 5f 43 4f 52 52 55 50 54 2e 20 2a 2f 0a 69  TE_CORRUPT. */.i
1b487 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 74  dx_rowid_corrupt
1b488 69 6f 6e 3a 0a 20 20 74 65 73 74 63 61 73 65 28  ion:.  testcase(
1b489 20 6d 2e 7a 4d 61 6c 6c 6f 63 21 3d 30 20 29 3b   m.zMalloc!=0 );
1b48a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1b48b 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20  mRelease(&m);.  
1b48c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
1b48d 52 52 55 50 54 5f 42 4b 50 54 3b 0a 7d 0a 0a 2f  RRUPT_BKPT;.}../
1b48e 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65  *.** Compare the
1b48f 20 6b 65 79 20 6f 66 20 74 68 65 20 69 6e 64 65   key of the inde
1b490 78 20 65 6e 74 72 79 20 74 68 61 74 20 63 75 72  x entry that cur
1b491 73 6f 72 20 70 43 20 69 73 20 70 6f 69 6e 74 69  sor pC is pointi
1b492 6e 67 20 74 6f 20 61 67 61 69 6e 73 74 0a 2a 2a  ng to against.**
1b493 20 74 68 65 20 6b 65 79 20 73 74 72 69 6e 67 20   the key string 
1b494 69 6e 20 70 55 6e 70 61 63 6b 65 64 2e 20 20 57  in pUnpacked.  W
1b495 72 69 74 65 20 69 6e 74 6f 20 2a 70 52 65 73 20  rite into *pRes 
1b496 61 20 6e 75 6d 62 65 72 0a 2a 2a 20 74 68 61 74  a number.** that
1b497 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 7a 65   is negative, ze
1b498 72 6f 2c 20 6f 72 20 70 6f 73 69 74 69 76 65 20  ro, or positive 
1b499 69 66 20 70 43 20 69 73 20 6c 65 73 73 20 74 68  if pC is less th
1b49a 61 6e 2c 20 65 71 75 61 6c 20 74 6f 2c 0a 2a 2a  an, equal to,.**
1b49b 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e   or greater than
1b49c 20 70 55 6e 70 61 63 6b 65 64 2e 20 20 52 65 74   pUnpacked.  Ret
1b49d 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e  urn SQLITE_OK on
1b49e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20   success..**.** 
1b49f 70 55 6e 70 61 63 6b 65 64 20 69 73 20 65 69 74  pUnpacked is eit
1b4a0 68 65 72 20 63 72 65 61 74 65 64 20 77 69 74 68  her created with
1b4a1 6f 75 74 20 61 20 72 6f 77 69 64 20 6f 72 20 69  out a rowid or i
1b4a2 73 20 74 72 75 6e 63 61 74 65 64 20 73 6f 20 74  s truncated so t
1b4a3 68 61 74 20 69 74 0a 2a 2a 20 6f 6d 69 74 73 20  hat it.** omits 
1b4a4 74 68 65 20 72 6f 77 69 64 20 61 74 20 74 68 65  the rowid at the
1b4a5 20 65 6e 64 2e 20 20 54 68 65 20 72 6f 77 69 64   end.  The rowid
1b4a6 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   at the end of t
1b4a7 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 0a 2a  he index entry.*
1b4a8 2a 20 69 73 20 69 67 6e 6f 72 65 64 20 61 73 20  * is ignored as 
1b4a9 77 65 6c 6c 2e 20 20 48 65 6e 63 65 2c 20 74 68  well.  Hence, th
1b4aa 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20  is routine only 
1b4ab 63 6f 6d 70 61 72 65 73 20 74 68 65 20 70 72 65  compares the pre
1b4ac 66 69 78 65 73 20 0a 2a 2a 20 6f 66 20 74 68 65  fixes .** of the
1b4ad 20 6b 65 79 73 20 70 72 69 6f 72 20 74 6f 20 74   keys prior to t
1b4ae 68 65 20 66 69 6e 61 6c 20 72 6f 77 69 64 2c 20  he final rowid, 
1b4af 6e 6f 74 20 74 68 65 20 65 6e 74 69 72 65 20 6b  not the entire k
1b4b0 65 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ey..*/.SQLITE_PR
1b4b1 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1b4b2 33 56 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61  3VdbeIdxKeyCompa
1b4b3 72 65 28 0a 20 20 56 64 62 65 43 75 72 73 6f 72  re(.  VdbeCursor
1b4b4 20 2a 70 43 2c 20 20 20 20 20 20 20 20 20 20 20   *pC,           
1b4b5 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20    /* The cursor 
1b4b6 74 6f 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e  to compare again
1b4b7 73 74 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64  st */.  Unpacked
1b4b8 52 65 63 6f 72 64 20 2a 70 55 6e 70 61 63 6b 65  Record *pUnpacke
1b4b9 64 2c 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20  d,  /* Unpacked 
1b4ba 76 65 72 73 69 6f 6e 20 6f 66 20 6b 65 79 20 74  version of key t
1b4bb 6f 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73  o compare agains
1b4bc 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 72 65 73 20  t */.  int *res 
1b4bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b4be 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
1b4bf 63 6f 6d 70 61 72 69 73 6f 6e 20 72 65 73 75 6c  comparison resul
1b4c0 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  t here */.){.  i
1b4c1 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 3b  64 nCellKey = 0;
1b4c2 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 43  .  int rc;.  BtC
1b4c3 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 43  ursor *pCur = pC
1b4c4 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 4d 65 6d  ->pCursor;.  Mem
1b4c5 20 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73   m;..  assert( s
1b4c6 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f  qlite3BtreeCurso
1b4c7 72 49 73 56 61 6c 69 64 28 70 43 75 72 29 20 29  rIsValid(pCur) )
1b4c8 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1b4c9 42 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75  BtreeKeySize(pCu
1b4ca 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20  r, &nCellKey);. 
1b4cb 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
1b4cc 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20 2f 2a 20  ITE_OK );    /* 
1b4cd 70 43 75 72 20 69 73 20 61 6c 77 61 79 73 20 76  pCur is always v
1b4ce 61 6c 69 64 20 73 6f 20 4b 65 79 53 69 7a 65 20  alid so KeySize 
1b4cf 63 61 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20  cannot fail */. 
1b4d0 20 2f 2a 20 6e 43 65 6c 6c 4b 65 79 20 77 69 6c   /* nCellKey wil
1b4d1 6c 20 61 6c 77 61 79 73 20 62 65 20 62 65 74 77  l always be betw
1b4d2 65 65 6e 20 30 20 61 6e 64 20 30 78 66 66 66 66  een 0 and 0xffff
1b4d3 66 66 66 66 20 62 65 63 61 75 73 65 20 6f 66 20  ffff because of 
1b4d4 74 68 65 20 73 61 79 0a 20 20 2a 2a 20 74 68 61  the say.  ** tha
1b4d5 74 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c  t btreeParseCell
1b4d6 50 74 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65  Ptr() and sqlite
1b4d7 33 47 65 74 56 61 72 69 6e 74 33 32 28 29 20 61  3GetVarint32() a
1b4d8 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 2a  re implemented *
1b4d9 2f 0a 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79  /.  if( nCellKey
1b4da 3c 3d 30 20 7c 7c 20 6e 43 65 6c 6c 4b 65 79 3e  <=0 || nCellKey>
1b4db 30 78 37 66 66 66 66 66 66 66 20 29 7b 0a 20 20  0x7fffffff ){.  
1b4dc 20 20 2a 72 65 73 20 3d 20 30 3b 0a 20 20 20 20    *res = 0;.    
1b4dd 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
1b4de 52 52 55 50 54 3b 0a 20 20 7d 0a 20 20 6d 65 6d  RRUPT;.  }.  mem
1b4df 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f  set(&m, 0, sizeo
1b4e0 66 28 6d 29 29 3b 0a 20 20 72 63 20 3d 20 73 71  f(m));.  rc = sq
1b4e1 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d  lite3VdbeMemFrom
1b4e2 42 74 72 65 65 28 70 43 2d 3e 70 43 75 72 73 6f  Btree(pC->pCurso
1b4e3 72 2c 20 30 2c 20 28 69 6e 74 29 6e 43 65 6c 6c  r, 0, (int)nCell
1b4e4 4b 65 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 20 69  Key, 1, &m);.  i
1b4e5 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74  f( rc ){.    ret
1b4e6 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73  urn rc;.  }.  as
1b4e7 73 65 72 74 28 20 70 55 6e 70 61 63 6b 65 64 2d  sert( pUnpacked-
1b4e8 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45  >flags & UNPACKE
1b4e9 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 20 29  D_IGNORE_ROWID )
1b4ea 3b 0a 20 20 2a 72 65 73 20 3d 20 73 71 6c 69 74  ;.  *res = sqlit
1b4eb 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70  e3VdbeRecordComp
1b4ec 61 72 65 28 6d 2e 6e 2c 20 6d 2e 7a 2c 20 70 55  are(m.n, m.z, pU
1b4ed 6e 70 61 63 6b 65 64 29 3b 0a 20 20 73 71 6c 69  npacked);.  sqli
1b4ee 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
1b4ef 65 28 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20  e(&m);.  return 
1b4f0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
1b4f1 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1b4f2 20 73 65 74 73 20 74 68 65 20 76 61 6c 75 65 20   sets the value 
1b4f3 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 62  to be returned b
1b4f4 79 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  y subsequent cal
1b4f5 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33  ls to.** sqlite3
1b4f6 5f 63 68 61 6e 67 65 73 28 29 20 6f 6e 20 74 68  _changes() on th
1b4f7 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
1b4f8 65 20 27 64 62 27 2e 20 0a 2a 2f 0a 53 51 4c 49  e 'db'. .*/.SQLI
1b4f9 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1b4fa 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 68  sqlite3VdbeSetCh
1b4fb 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64  anges(sqlite3 *d
1b4fc 62 2c 20 69 6e 74 20 6e 43 68 61 6e 67 65 29 7b  b, int nChange){
1b4fd 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1b4fe 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
1b4ff 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 64 62  ->mutex) );.  db
1b500 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 6e 43 68 61  ->nChange = nCha
1b501 6e 67 65 3b 0a 20 20 64 62 2d 3e 6e 54 6f 74 61  nge;.  db->nTota
1b502 6c 43 68 61 6e 67 65 20 2b 3d 20 6e 43 68 61 6e  lChange += nChan
1b503 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  ge;.}../*.** Set
1b504 20 61 20 66 6c 61 67 20 69 6e 20 74 68 65 20 76   a flag in the v
1b505 64 62 65 20 74 6f 20 75 70 64 61 74 65 20 74 68  dbe to update th
1b506 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72  e change counter
1b507 20 77 68 65 6e 20 69 74 20 69 73 20 66 69 6e 61   when it is fina
1b508 6c 69 73 65 64 0a 2a 2a 20 6f 72 20 72 65 73 65  lised.** or rese
1b509 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
1b50a 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1b50b 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65  3VdbeCountChange
1b50c 73 28 56 64 62 65 20 2a 76 29 7b 0a 20 20 76 2d  s(Vdbe *v){.  v-
1b50d 3e 63 68 61 6e 67 65 43 6e 74 4f 6e 20 3d 20 31  >changeCntOn = 1
1b50e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20  ;.}../*.** Mark 
1b50f 65 76 65 72 79 20 70 72 65 70 61 72 65 64 20 73  every prepared s
1b510 74 61 74 65 6d 65 6e 74 20 61 73 73 6f 63 69 61  tatement associa
1b511 74 65 64 20 77 69 74 68 20 61 20 64 61 74 61 62  ted with a datab
1b512 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a  ase connection.*
1b513 2a 20 61 73 20 65 78 70 69 72 65 64 2e 0a 2a 2a  * as expired..**
1b514 0a 2a 2a 20 41 6e 20 65 78 70 69 72 65 64 20 73  .** An expired s
1b515 74 61 74 65 6d 65 6e 74 20 6d 65 61 6e 73 20 74  tatement means t
1b516 68 61 74 20 72 65 63 6f 6d 70 69 6c 61 74 69 6f  hat recompilatio
1b517 6e 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65  n of the stateme
1b518 6e 74 20 69 73 0a 2a 2a 20 72 65 63 6f 6d 6d 65  nt is.** recomme
1b519 6e 64 2e 20 20 53 74 61 74 65 6d 65 6e 74 73 20  nd.  Statements 
1b51a 65 78 70 69 72 65 20 77 68 65 6e 20 74 68 69 6e  expire when thin
1b51b 67 73 20 68 61 70 70 65 6e 20 74 68 61 74 20 6d  gs happen that m
1b51c 61 6b 65 20 74 68 65 69 72 0a 2a 2a 20 70 72 6f  ake their.** pro
1b51d 67 72 61 6d 73 20 6f 62 73 6f 6c 65 74 65 2e 20  grams obsolete. 
1b51e 20 52 65 6d 6f 76 69 6e 67 20 75 73 65 72 2d 64   Removing user-d
1b51f 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73  efined functions
1b520 20 6f 72 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a   or collating.**
1b521 20 73 65 71 75 65 6e 63 65 73 2c 20 6f 72 20 63   sequences, or c
1b522 68 61 6e 67 69 6e 67 20 61 6e 20 61 75 74 68 6f  hanging an autho
1b523 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  rization functio
1b524 6e 20 61 72 65 20 74 68 65 20 74 79 70 65 73 20  n are the types 
1b525 6f 66 0a 2a 2a 20 74 68 69 6e 67 73 20 74 68 61  of.** things tha
1b526 74 20 6d 61 6b 65 20 70 72 65 70 61 72 65 64 20  t make prepared 
1b527 73 74 61 74 65 6d 65 6e 74 73 20 6f 62 73 6f 6c  statements obsol
1b528 65 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ete..*/.SQLITE_P
1b529 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1b52a 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65  te3ExpirePrepare
1b52b 64 53 74 61 74 65 6d 65 6e 74 73 28 73 71 6c 69  dStatements(sqli
1b52c 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 64 62 65  te3 *db){.  Vdbe
1b52d 20 2a 70 3b 0a 20 20 66 6f 72 28 70 20 3d 20 64   *p;.  for(p = d
1b52e 62 2d 3e 70 56 64 62 65 3b 20 70 3b 20 70 3d 70  b->pVdbe; p; p=p
1b52f 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d  ->pNext){.    p-
1b530 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0a 20 20  >expired = 1;.  
1b531 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  }.}../*.** Retur
1b532 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  n the database a
1b533 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
1b534 68 65 20 56 64 62 65 2e 0a 2a 2f 0a 53 51 4c 49  he Vdbe..*/.SQLI
1b535 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74  TE_PRIVATE sqlit
1b536 65 33 20 2a 73 71 6c 69 74 65 33 56 64 62 65 44  e3 *sqlite3VdbeD
1b537 62 28 56 64 62 65 20 2a 76 29 7b 0a 20 20 72 65  b(Vdbe *v){.  re
1b538 74 75 72 6e 20 76 2d 3e 64 62 3b 0a 7d 0a 0a 2f  turn v->db;.}../
1b539 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
1b53a 6e 64 20 6f 66 20 76 64 62 65 61 75 78 2e 63 20  nd of vdbeaux.c 
1b53b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b53c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b53d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1b53e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
1b53f 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 61 70  egin file vdbeap
1b540 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  i.c ************
1b541 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b542 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1b543 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 36  *.** 2004 May 26
1b544 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
1b545 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1b546 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1b547 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
1b548 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
1b549 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
1b54a 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
1b54b 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
1b54c 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
1b54d 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
1b54e 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1b54f 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1b550 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
1b551 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
1b552 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
1b553 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
1b554 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
1b555 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
1b556 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b557 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b558 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b559 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1b55a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
1b55b 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73  contains code us
1b55c 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 41  e to implement A
1b55d 50 49 73 20 74 68 61 74 20 61 72 65 20 70 61 72  PIs that are par
1b55e 74 20 6f 66 20 74 68 65 0a 2a 2a 20 56 44 42 45  t of the.** VDBE
1b55f 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62  ..**.** $Id: vdb
1b560 65 61 70 69 2e 63 2c 76 20 31 2e 31 36 37 20 32  eapi.c,v 1.167 2
1b561 30 30 39 2f 30 36 2f 32 35 20 30 31 3a 34 37 3a  009/06/25 01:47:
1b562 31 32 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  12 drh Exp $.*/.
1b563 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1b564 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a  OMIT_DEPRECATED.
1b565 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55  /*.** Return TRU
1b566 45 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 6f 66 20  E (non-zero) of 
1b567 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 73 75  the statement su
1b568 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67  pplied as an arg
1b569 75 6d 65 6e 74 20 6e 65 65 64 73 0a 2a 2a 20 74  ument needs.** t
1b56a 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 2e  o be recompiled.
1b56b 20 20 41 20 73 74 61 74 65 6d 65 6e 74 20 6e 65    A statement ne
1b56c 65 64 73 20 74 6f 20 62 65 20 72 65 63 6f 6d 70  eds to be recomp
1b56d 69 6c 65 64 20 77 68 65 6e 65 76 65 72 20 74 68  iled whenever th
1b56e 65 0a 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20 65  e.** execution e
1b56f 6e 76 69 72 6f 6e 6d 65 6e 74 20 63 68 61 6e 67  nvironment chang
1b570 65 73 20 69 6e 20 61 20 77 61 79 20 74 68 61 74  es in a way that
1b571 20 77 6f 75 6c 64 20 61 6c 74 65 72 20 74 68 65   would alter the
1b572 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 74 68 61 74   program.** that
1b573 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
1b574 28 29 20 67 65 6e 65 72 61 74 65 73 2e 20 20 46  () generates.  F
1b575 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 6e  or example, if n
1b576 65 77 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 0a  ew functions or.
1b577 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  ** collating seq
1b578 75 65 6e 63 65 73 20 61 72 65 20 72 65 67 69 73  uences are regis
1b579 74 65 72 65 64 20 6f 72 20 69 66 20 61 6e 20 61  tered or if an a
1b57a 75 74 68 6f 72 69 7a 65 72 20 66 75 6e 63 74 69  uthorizer functi
1b57b 6f 6e 20 69 73 0a 2a 2a 20 61 64 64 65 64 20 6f  on is.** added o
1b57c 72 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51  r changed..*/.SQ
1b57d 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1b57e 69 74 65 33 5f 65 78 70 69 72 65 64 28 73 71 6c  ite3_expired(sql
1b57f 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1b580 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  ){.  Vdbe *p = (
1b581 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72  Vdbe*)pStmt;.  r
1b582 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d  eturn p==0 || p-
1b583 3e 65 78 70 69 72 65 64 3b 0a 7d 0a 23 65 6e 64  >expired;.}.#end
1b584 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  if../*.** The fo
1b585 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20  llowing routine 
1b586 64 65 73 74 72 6f 79 73 20 61 20 76 69 72 74 75  destroys a virtu
1b587 61 6c 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20  al machine that 
1b588 69 73 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a  is created by.**
1b589 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d   the sqlite3_com
1b58a 70 69 6c 65 28 29 20 72 6f 75 74 69 6e 65 2e 20  pile() routine. 
1b58b 54 68 65 20 69 6e 74 65 67 65 72 20 72 65 74 75  The integer retu
1b58c 72 6e 65 64 20 69 73 20 61 6e 20 53 51 4c 49 54  rned is an SQLIT
1b58d 45 5f 0a 2a 2a 20 73 75 63 63 65 73 73 2f 66 61  E_.** success/fa
1b58e 69 6c 75 72 65 20 63 6f 64 65 20 74 68 61 74 20  ilure code that 
1b58f 64 65 73 63 72 69 62 65 73 20 74 68 65 20 72 65  describes the re
1b590 73 75 6c 74 20 6f 66 20 65 78 65 63 75 74 69 6e  sult of executin
1b591 67 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a  g the virtual.**
1b592 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20   machine..**.** 
1b593 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74  This routine set
1b594 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  s the error code
1b595 20 61 6e 64 20 73 74 72 69 6e 67 20 72 65 74 75   and string retu
1b596 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74  rned by.** sqlit
1b597 65 33 5f 65 72 72 63 6f 64 65 28 29 2c 20 73 71  e3_errcode(), sq
1b598 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61  lite3_errmsg() a
1b599 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  nd sqlite3_errms
1b59a 67 31 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  g16()..*/.SQLITE
1b59b 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1b59c 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65  _finalize(sqlite
1b59d 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a  3_stmt *pStmt){.
1b59e 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20    int rc;.  if( 
1b59f 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20  pStmt==0 ){.    
1b5a0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1b5a1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64 62    }else{.    Vdb
1b5a2 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29 70 53  e *v = (Vdbe*)pS
1b5a3 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  tmt;.    sqlite3
1b5a4 20 2a 64 62 20 3d 20 76 2d 3e 64 62 3b 0a 23 69   *db = v->db;.#i
1b5a5 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
1b5a6 41 46 45 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  AFE.    sqlite3_
1b5a7 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 76  mutex *mutex = v
1b5a8 2d 3e 64 62 2d 3e 6d 75 74 65 78 3b 0a 23 65 6e  ->db->mutex;.#en
1b5a9 64 69 66 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  dif.    sqlite3_
1b5aa 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65  mutex_enter(mute
1b5ab 78 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  x);.    rc = sql
1b5ac 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65  ite3VdbeFinalize
1b5ad 28 76 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  (v);.    rc = sq
1b5ae 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c  lite3ApiExit(db,
1b5af 20 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   rc);.    sqlite
1b5b0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75  3_mutex_leave(mu
1b5b1 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  tex);.  }.  retu
1b5b2 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1b5b3 54 65 72 6d 69 6e 61 74 65 20 74 68 65 20 63 75  Terminate the cu
1b5b4 72 72 65 6e 74 20 65 78 65 63 75 74 69 6f 6e 20  rrent execution 
1b5b5 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d  of an SQL statem
1b5b6 65 6e 74 20 61 6e 64 20 72 65 73 65 74 20 69 74  ent and reset it
1b5b7 0a 2a 2a 20 62 61 63 6b 20 74 6f 20 69 74 73 20  .** back to its 
1b5b8 73 74 61 72 74 69 6e 67 20 73 74 61 74 65 20 73  starting state s
1b5b9 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 62 65  o that it can be
1b5ba 20 72 65 75 73 65 64 2e 20 41 20 73 75 63 63 65   reused. A succe
1b5bb 73 73 20 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a 20  ss code from.** 
1b5bc 74 68 65 20 70 72 69 6f 72 20 65 78 65 63 75 74  the prior execut
1b5bd 69 6f 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ion is returned.
1b5be 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
1b5bf 69 6e 65 20 73 65 74 73 20 74 68 65 20 65 72 72  ine sets the err
1b5c0 6f 72 20 63 6f 64 65 20 61 6e 64 20 73 74 72 69  or code and stri
1b5c1 6e 67 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a  ng returned by.*
1b5c2 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  * sqlite3_errcod
1b5c3 65 28 29 2c 20 73 71 6c 69 74 65 33 5f 65 72 72  e(), sqlite3_err
1b5c4 6d 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74 65  msg() and sqlite
1b5c5 33 5f 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a 2f  3_errmsg16()..*/
1b5c6 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1b5c7 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 73 71  sqlite3_reset(sq
1b5c8 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1b5c9 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  t){.  int rc;.  
1b5ca 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a  if( pStmt==0 ){.
1b5cb 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1b5cc 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OK;.  }else{.   
1b5cd 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65   Vdbe *v = (Vdbe
1b5ce 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c  *)pStmt;.    sql
1b5cf 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
1b5d0 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  (v->db->mutex);.
1b5d1 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1b5d2 56 64 62 65 52 65 73 65 74 28 76 29 3b 0a 20 20  VdbeReset(v);.  
1b5d3 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b    sqlite3VdbeMak
1b5d4 65 52 65 61 64 79 28 76 2c 20 2d 31 2c 20 30 2c  eReady(v, -1, 0,
1b5d5 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20   0, 0, 0, 0);.  
1b5d6 20 20 61 73 73 65 72 74 28 20 28 72 63 20 26 20    assert( (rc & 
1b5d7 28 76 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29  (v->db->errMask)
1b5d8 29 3d 3d 72 63 20 29 3b 0a 20 20 20 20 72 63 20  )==rc );.    rc 
1b5d9 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
1b5da 28 76 2d 3e 64 62 2c 20 72 63 29 3b 0a 20 20 20  (v->db, rc);.   
1b5db 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
1b5dc 65 61 76 65 28 76 2d 3e 64 62 2d 3e 6d 75 74 65  eave(v->db->mute
1b5dd 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  x);.  }.  return
1b5de 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65   rc;.}../*.** Se
1b5df 74 20 61 6c 6c 20 74 68 65 20 70 61 72 61 6d 65  t all the parame
1b5e0 74 65 72 73 20 69 6e 20 74 68 65 20 63 6f 6d 70  ters in the comp
1b5e1 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  iled SQL stateme
1b5e2 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53  nt to NULL..*/.S
1b5e3 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1b5e4 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64  lite3_clear_bind
1b5e5 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d  ings(sqlite3_stm
1b5e6 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74  t *pStmt){.  int
1b5e7 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   i;.  int rc = S
1b5e8 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 56 64 62 65  QLITE_OK;.  Vdbe
1b5e9 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74   *p = (Vdbe*)pSt
1b5ea 6d 74 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  mt;.#if SQLITE_T
1b5eb 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69  HREADSAFE.  sqli
1b5ec 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
1b5ed 20 3d 20 28 28 56 64 62 65 2a 29 70 53 74 6d 74   = ((Vdbe*)pStmt
1b5ee 29 2d 3e 64 62 2d 3e 6d 75 74 65 78 3b 0a 23 65  )->db->mutex;.#e
1b5ef 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ndif.  sqlite3_m
1b5f0 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78  utex_enter(mutex
1b5f1 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
1b5f2 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20  p->nVar; i++){. 
1b5f3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1b5f4 6d 52 65 6c 65 61 73 65 28 26 70 2d 3e 61 56 61  mRelease(&p->aVa
1b5f5 72 5b 69 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 56  r[i]);.    p->aV
1b5f6 61 72 5b 69 5d 2e 66 6c 61 67 73 20 3d 20 4d 45  ar[i].flags = ME
1b5f7 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 0a 20 20 73 71  M_Null;.  }.  sq
1b5f8 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1b5f9 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  e(mutex);.  retu
1b5fa 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a  rn rc;.}.../****
1b5fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b5fc 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33  ******** sqlite3
1b5fd 5f 76 61 6c 75 65 5f 20 20 2a 2a 2a 2a 2a 2a 2a  _value_  *******
1b5fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b5ff 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20  ********.** The 
1b600 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e  following routin
1b601 65 73 20 65 78 74 72 61 63 74 20 69 6e 66 6f 72  es extract infor
1b602 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 4d 65  mation from a Me
1b603 6d 20 6f 72 20 73 71 6c 69 74 65 33 5f 76 61 6c  m or sqlite3_val
1b604 75 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  ue.** structure.
1b605 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
1b606 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
1b607 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71  e3_value_blob(sq
1b608 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
1b609 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28  l){.  Mem *p = (
1b60a 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28  Mem*)pVal;.  if(
1b60b 20 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d   p->flags & (MEM
1b60c 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29  _Blob|MEM_Str) )
1b60d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1b60e 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 70  eMemExpandBlob(p
1b60f 29 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20  );.    p->flags 
1b610 26 3d 20 7e 4d 45 4d 5f 53 74 72 3b 0a 20 20 20  &= ~MEM_Str;.   
1b611 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d   p->flags |= MEM
1b612 5f 42 6c 6f 62 3b 0a 20 20 20 20 72 65 74 75 72  _Blob;.    retur
1b613 6e 20 70 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b  n p->z;.  }else{
1b614 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
1b615 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 70  te3_value_text(p
1b616 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49  Val);.  }.}.SQLI
1b617 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1b618 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 73  e3_value_bytes(s
1b619 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
1b61a 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  al){.  return sq
1b61b 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28  lite3ValueBytes(
1b61c 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  pVal, SQLITE_UTF
1b61d 38 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  8);.}.SQLITE_API
1b61e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
1b61f 75 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74  ue_bytes16(sqlit
1b620 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b  e3_value *pVal){
1b621 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
1b622 33 56 61 6c 75 65 42 79 74 65 73 28 70 56 61 6c  3ValueBytes(pVal
1b623 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  , SQLITE_UTF16NA
1b624 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  TIVE);.}.SQLITE_
1b625 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74  API double sqlit
1b626 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28  e3_value_double(
1b627 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
1b628 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  Val){.  return s
1b629 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61  qlite3VdbeRealVa
1b62a 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b  lue((Mem*)pVal);
1b62b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
1b62c 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t sqlite3_value_
1b62d 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  int(sqlite3_valu
1b62e 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75  e *pVal){.  retu
1b62f 72 6e 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56  rn (int)sqlite3V
1b630 64 62 65 49 6e 74 56 61 6c 75 65 28 28 4d 65 6d  dbeIntValue((Mem
1b631 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54  *)pVal);.}.SQLIT
1b632 45 5f 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74  E_API sqlite_int
1b633 36 34 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  64 sqlite3_value
1b634 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 76  _int64(sqlite3_v
1b635 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72  alue *pVal){.  r
1b636 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62  eturn sqlite3Vdb
1b637 65 49 6e 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29  eIntValue((Mem*)
1b638 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  pVal);.}.SQLITE_
1b639 41 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  API const unsign
1b63a 65 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  ed char *sqlite3
1b63b 5f 76 61 6c 75 65 5f 74 65 78 74 28 73 71 6c 69  _value_text(sqli
1b63c 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29  te3_value *pVal)
1b63d 7b 0a 20 20 72 65 74 75 72 6e 20 28 63 6f 6e 73  {.  return (cons
1b63e 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
1b63f 2a 29 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65  *)sqlite3ValueTe
1b640 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f  xt(pVal, SQLITE_
1b641 55 54 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66  UTF8);.}.#ifndef
1b642 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1b643 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  16.SQLITE_API co
1b644 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
1b645 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73  3_value_text16(s
1b646 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 56  qlite3_value* pV
1b647 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  al){.  return sq
1b648 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70  lite3ValueText(p
1b649 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  Val, SQLITE_UTF1
1b64a 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49  6NATIVE);.}.SQLI
1b64b 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
1b64c 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  d *sqlite3_value
1b64d 5f 74 65 78 74 31 36 62 65 28 73 71 6c 69 74 65  _text16be(sqlite
1b64e 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a  3_value *pVal){.
1b64f 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
1b650 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20  ValueText(pVal, 
1b651 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 29 3b  SQLITE_UTF16BE);
1b652 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  .}.SQLITE_API co
1b653 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
1b654 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65  3_value_text16le
1b655 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
1b656 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20  pVal){.  return 
1b657 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
1b658 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54  (pVal, SQLITE_UT
1b659 46 31 36 4c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66  F16LE);.}.#endif
1b65a 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1b65b 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f  UTF16 */.SQLITE_
1b65c 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1b65d 76 61 6c 75 65 5f 74 79 70 65 28 73 71 6c 69 74  value_type(sqlit
1b65e 65 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b  e3_value* pVal){
1b65f 0a 20 20 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e  .  return pVal->
1b660 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  type;.}../******
1b661 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b662 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 72  ****** sqlite3_r
1b663 65 73 75 6c 74 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a  esult_  ********
1b664 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b665 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66  *******.** The f
1b666 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
1b667 73 20 61 72 65 20 75 73 65 64 20 62 79 20 75 73  s are used by us
1b668 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  er-defined funct
1b669 69 6f 6e 73 20 74 6f 20 73 70 65 63 69 66 79 0a  ions to specify.
1b66a 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  ** the function 
1b66b 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  result..**.** Th
1b66c 65 20 73 65 74 53 74 72 4f 72 45 72 72 6f 72 28  e setStrOrError(
1b66d 29 20 66 75 6e 74 69 6f 6e 20 63 61 6c 6c 73 20  ) funtion calls 
1b66e 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1b66f 74 53 74 72 28 29 20 74 6f 20 73 74 6f 72 65 20  tStr() to store 
1b670 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 61 73  the.** result as
1b671 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f   a string or blo
1b672 62 20 62 75 74 20 69 66 20 74 68 65 20 73 74 72  b but if the str
1b673 69 6e 67 20 6f 72 20 62 6c 6f 62 20 69 73 20 74  ing or blob is t
1b674 6f 6f 20 6c 61 72 67 65 2c 20 69 74 0a 2a 2a 20  oo large, it.** 
1b675 74 68 65 6e 20 73 65 74 73 20 74 68 65 20 65 72  then sets the er
1b676 72 6f 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49  ror code to SQLI
1b677 54 45 5f 54 4f 4f 42 49 47 0a 2a 2f 0a 73 74 61  TE_TOOBIG.*/.sta
1b678 74 69 63 20 76 6f 69 64 20 73 65 74 52 65 73 75  tic void setResu
1b679 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 0a 20 20  ltStrOrError(.  
1b67a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1b67b 2a 70 43 74 78 2c 20 20 2f 2a 20 46 75 6e 63 74  *pCtx,  /* Funct
1b67c 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ion context */. 
1b67d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
1b67e 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69           /* Stri
1b67f 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20  ng pointer */.  
1b680 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20  int n,          
1b681 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73          /* Bytes
1b682 20 69 6e 20 73 74 72 69 6e 67 2c 20 6f 72 20 6e   in string, or n
1b683 65 67 61 74 69 76 65 20 2a 2f 0a 20 20 75 38 20  egative */.  u8 
1b684 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  enc,            
1b685 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67       /* Encoding
1b686 20 6f 66 20 7a 2e 20 20 30 20 66 6f 72 20 42 4c   of z.  0 for BL
1b687 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  OBs */.  void (*
1b688 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 20  xDel)(void*)    
1b689 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66   /* Destructor f
1b68a 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20  unction */.){.  
1b68b 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
1b68c 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e  emSetStr(&pCtx->
1b68d 73 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 44  s, z, n, enc, xD
1b68e 65 6c 29 3d 3d 53 51 4c 49 54 45 5f 54 4f 4f 42  el)==SQLITE_TOOB
1b68f 49 47 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  IG ){.    sqlite
1b690 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  3_result_error_t
1b691 6f 6f 62 69 67 28 70 43 74 78 29 3b 0a 20 20 7d  oobig(pCtx);.  }
1b692 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  .}.SQLITE_API vo
1b693 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
1b694 74 5f 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65  t_blob(.  sqlite
1b695 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c  3_context *pCtx,
1b696 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   .  const void *
1b697 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20  z, .  int n, .  
1b698 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69  void (*xDel)(voi
1b699 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74  d *).){.  assert
1b69a 28 20 6e 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65  ( n>=0 );.  asse
1b69b 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1b69c 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64  x_held(pCtx->s.d
1b69d 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73  b->mutex) );.  s
1b69e 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72  etResultStrOrErr
1b69f 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 30  or(pCtx, z, n, 0
1b6a0 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54  , xDel);.}.SQLIT
1b6a1 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
1b6a2 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65  e3_result_double
1b6a3 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1b6a4 20 2a 70 43 74 78 2c 20 64 6f 75 62 6c 65 20 72   *pCtx, double r
1b6a5 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Val){.  assert( 
1b6a6 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1b6a7 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
1b6a8 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
1b6a9 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62  e3VdbeMemSetDoub
1b6aa 6c 65 28 26 70 43 74 78 2d 3e 73 2c 20 72 56 61  le(&pCtx->s, rVa
1b6ab 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  l);.}.SQLITE_API
1b6ac 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
1b6ad 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c 69 74  sult_error(sqlit
1b6ae 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
1b6af 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c  , const char *z,
1b6b0 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72   int n){.  asser
1b6b1 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1b6b2 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
1b6b3 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43  ->mutex) );.  pC
1b6b4 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51  tx->isError = SQ
1b6b5 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 73 71  LITE_ERROR;.  sq
1b6b6 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
1b6b7 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20  tr(&pCtx->s, z, 
1b6b8 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  n, SQLITE_UTF8, 
1b6b9 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
1b6ba 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  );.}.#ifndef SQL
1b6bb 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53  ITE_OMIT_UTF16.S
1b6bc 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
1b6bd 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
1b6be 72 6f 72 31 36 28 73 71 6c 69 74 65 33 5f 63 6f  ror16(sqlite3_co
1b6bf 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 63 6f 6e  ntext *pCtx, con
1b6c0 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20  st void *z, int 
1b6c1 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  n){.  assert( sq
1b6c2 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1b6c3 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74  (pCtx->s.db->mut
1b6c4 65 78 29 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69  ex) );.  pCtx->i
1b6c5 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f  sError = SQLITE_
1b6c6 45 52 52 4f 52 3b 0a 20 20 73 71 6c 69 74 65 33  ERROR;.  sqlite3
1b6c7 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70  VdbeMemSetStr(&p
1b6c8 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51  Ctx->s, z, n, SQ
1b6c9 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
1b6ca 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
1b6cb 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 53 51  NT);.}.#endif.SQ
1b6cc 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
1b6cd 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
1b6ce 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1b6cf 20 2a 70 43 74 78 2c 20 69 6e 74 20 69 56 61 6c   *pCtx, int iVal
1b6d0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
1b6d1 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1b6d2 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
1b6d3 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  x) );.  sqlite3V
1b6d4 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26  dbeMemSetInt64(&
1b6d5 70 43 74 78 2d 3e 73 2c 20 28 69 36 34 29 69 56  pCtx->s, (i64)iV
1b6d6 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  al);.}.SQLITE_AP
1b6d7 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
1b6d8 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c 69  esult_int64(sqli
1b6d9 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
1b6da 78 2c 20 69 36 34 20 69 56 61 6c 29 7b 0a 20 20  x, i64 iVal){.  
1b6db 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1b6dc 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d  mutex_held(pCtx-
1b6dd 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
1b6de 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1b6df 6d 53 65 74 49 6e 74 36 34 28 26 70 43 74 78 2d  mSetInt64(&pCtx-
1b6e0 3e 73 2c 20 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c  >s, iVal);.}.SQL
1b6e1 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
1b6e2 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
1b6e3 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1b6e4 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72   *pCtx){.  asser
1b6e5 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1b6e6 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
1b6e7 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
1b6e8 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e  lite3VdbeMemSetN
1b6e9 75 6c 6c 28 26 70 43 74 78 2d 3e 73 29 3b 0a 7d  ull(&pCtx->s);.}
1b6ea 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1b6eb 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1b6ec 74 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33 5f  text(.  sqlite3_
1b6ed 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a  context *pCtx, .
1b6ee 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c    const char *z,
1b6ef 20 0a 20 20 69 6e 74 20 6e 2c 0a 20 20 76 6f 69   .  int n,.  voi
1b6f0 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a  d (*xDel)(void *
1b6f1 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  ).){.  assert( s
1b6f2 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1b6f3 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75  d(pCtx->s.db->mu
1b6f4 74 65 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73  tex) );.  setRes
1b6f5 75 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43  ultStrOrError(pC
1b6f6 74 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45  tx, z, n, SQLITE
1b6f7 5f 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a 7d 0a  _UTF8, xDel);.}.
1b6f8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1b6f9 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45  MIT_UTF16.SQLITE
1b6fa 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
1b6fb 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28  3_result_text16(
1b6fc 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
1b6fd 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e  xt *pCtx, .  con
1b6fe 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69  st void *z, .  i
1b6ff 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a  nt n, .  void (*
1b700 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b  xDel)(void *).){
1b701 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1b702 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
1b703 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  tx->s.db->mutex)
1b704 20 29 3b 0a 20 20 73 65 74 52 65 73 75 6c 74 53   );.  setResultS
1b705 74 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c 20  trOrError(pCtx, 
1b706 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46  z, n, SQLITE_UTF
1b707 31 36 4e 41 54 49 56 45 2c 20 78 44 65 6c 29 3b  16NATIVE, xDel);
1b708 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  .}.SQLITE_API vo
1b709 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
1b70a 74 5f 74 65 78 74 31 36 62 65 28 0a 20 20 73 71  t_text16be(.  sq
1b70b 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1b70c 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f  Ctx, .  const vo
1b70d 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c  id *z, .  int n,
1b70e 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29   .  void (*xDel)
1b70f 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73  (void *).){.  as
1b710 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1b711 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
1b712 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1b713 20 73 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45   setResultStrOrE
1b714 72 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c  rror(pCtx, z, n,
1b715 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c   SQLITE_UTF16BE,
1b716 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45   xDel);.}.SQLITE
1b717 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
1b718 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c  3_result_text16l
1b719 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  e(.  sqlite3_con
1b71a 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63  text *pCtx, .  c
1b71b 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20  onst void *z, . 
1b71c 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20   int n, .  void 
1b71d 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a  (*xDel)(void *).
1b71e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
1b71f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1b720 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
1b721 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75 6c  x) );.  setResul
1b722 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74 78  tStrOrError(pCtx
1b723 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55  , z, n, SQLITE_U
1b724 54 46 31 36 4c 45 2c 20 78 44 65 6c 29 3b 0a 7d  TF16LE, xDel);.}
1b725 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1b726 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a  E_OMIT_UTF16 */.
1b727 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1b728 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
1b729 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e  alue(sqlite3_con
1b72a 74 65 78 74 20 2a 70 43 74 78 2c 20 73 71 6c 69  text *pCtx, sqli
1b72b 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75  te3_value *pValu
1b72c 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  e){.  assert( sq
1b72d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1b72e 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74  (pCtx->s.db->mut
1b72f 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ex) );.  sqlite3
1b730 56 64 62 65 4d 65 6d 43 6f 70 79 28 26 70 43 74  VdbeMemCopy(&pCt
1b731 78 2d 3e 73 2c 20 70 56 61 6c 75 65 29 3b 0a 7d  x->s, pValue);.}
1b732 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1b733 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1b734 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33  zeroblob(sqlite3
1b735 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
1b736 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74  int n){.  assert
1b737 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1b738 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d  held(pCtx->s.db-
1b739 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c  >mutex) );.  sql
1b73a 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65  ite3VdbeMemSetZe
1b73b 72 6f 42 6c 6f 62 28 26 70 43 74 78 2d 3e 73 2c  roBlob(&pCtx->s,
1b73c 20 6e 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50   n);.}.SQLITE_AP
1b73d 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
1b73e 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65  esult_error_code
1b73f 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1b740 20 2a 70 43 74 78 2c 20 69 6e 74 20 65 72 72 43   *pCtx, int errC
1b741 6f 64 65 29 7b 0a 20 20 70 43 74 78 2d 3e 69 73  ode){.  pCtx->is
1b742 45 72 72 6f 72 20 3d 20 65 72 72 43 6f 64 65 3b  Error = errCode;
1b743 0a 20 20 69 66 28 20 70 43 74 78 2d 3e 73 2e 66  .  if( pCtx->s.f
1b744 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20  lags & MEM_Null 
1b745 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1b746 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74  beMemSetStr(&pCt
1b747 78 2d 3e 73 2c 20 73 71 6c 69 74 65 33 45 72 72  x->s, sqlite3Err
1b748 53 74 72 28 65 72 72 43 6f 64 65 29 2c 20 2d 31  Str(errCode), -1
1b749 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
1b74a 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
1b74b 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f  TE_UTF8, SQLITE_
1b74c 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a  STATIC);.  }.}..
1b74d 2f 2a 20 46 6f 72 63 65 20 61 6e 20 53 51 4c 49  /* Force an SQLI
1b74e 54 45 5f 54 4f 4f 42 49 47 20 65 72 72 6f 72 2e  TE_TOOBIG error.
1b74f 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76   */.SQLITE_API v
1b750 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
1b751 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28  lt_error_toobig(
1b752 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1b753 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74  *pCtx){.  assert
1b754 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1b755 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d  held(pCtx->s.db-
1b756 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74  >mutex) );.  pCt
1b757 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c  x->isError = SQL
1b758 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 73 71  ITE_TOOBIG;.  sq
1b759 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53  lite3VdbeMemSetS
1b75a 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 22 73 74  tr(&pCtx->s, "st
1b75b 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f 6f  ring or blob too
1b75c 20 62 69 67 22 2c 20 2d 31 2c 20 0a 20 20 20 20   big", -1, .    
1b75d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b75e 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20     SQLITE_UTF8, 
1b75f 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
1b760 7d 0a 0a 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f  }../* An SQLITE_
1b761 4e 4f 4d 45 4d 20 65 72 72 6f 72 2e 20 2a 2f 0a  NOMEM error. */.
1b762 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1b763 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
1b764 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74  rror_nomem(sqlit
1b765 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
1b766 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
1b767 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1b768 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
1b769 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  x) );.  sqlite3V
1b76a 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70  dbeMemSetNull(&p
1b76b 43 74 78 2d 3e 73 29 3b 0a 20 20 70 43 74 78 2d  Ctx->s);.  pCtx-
1b76c 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54  >isError = SQLIT
1b76d 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 70 43 74 78 2d  E_NOMEM;.  pCtx-
1b76e 3e 73 2e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  >s.db->mallocFai
1b76f 6c 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  led = 1;.}../*.*
1b770 2a 20 45 78 65 63 75 74 65 20 74 68 65 20 73 74  * Execute the st
1b771 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2c 20 65  atement pStmt, e
1b772 69 74 68 65 72 20 75 6e 74 69 6c 20 61 20 72 6f  ither until a ro
1b773 77 20 6f 66 20 64 61 74 61 20 69 73 20 72 65 61  w of data is rea
1b774 64 79 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65  dy, the.** state
1b775 6d 65 6e 74 20 69 73 20 63 6f 6d 70 6c 65 74 65  ment is complete
1b776 6c 79 20 65 78 65 63 75 74 65 64 20 6f 72 20 61  ly executed or a
1b777 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a  n error occurs..
1b778 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
1b779 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  ne implements th
1b77a 65 20 62 75 6c 6b 20 6f 66 20 74 68 65 20 6c 6f  e bulk of the lo
1b77b 67 69 63 20 62 65 68 69 6e 64 20 74 68 65 20 73  gic behind the s
1b77c 71 6c 69 74 65 5f 73 74 65 70 28 29 0a 2a 2a 20  qlite_step().** 
1b77d 41 50 49 2e 20 20 54 68 65 20 6f 6e 6c 79 20 74  API.  The only t
1b77e 68 69 6e 67 20 6f 6d 69 74 74 65 64 20 69 73 20  hing omitted is 
1b77f 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20 72 65  the automatic re
1b780 63 6f 6d 70 69 6c 65 20 69 66 20 61 20 0a 2a 2a  compile if a .**
1b781 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 20 68   schema change h
1b782 61 73 20 6f 63 63 75 72 72 65 64 2e 20 20 54 68  as occurred.  Th
1b783 61 74 20 64 65 74 61 69 6c 20 69 73 20 68 61 6e  at detail is han
1b784 64 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 6f  dled by the.** o
1b785 75 74 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65  uter sqlite3_ste
1b786 70 28 29 20 77 72 61 70 70 65 72 20 70 72 6f 63  p() wrapper proc
1b787 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  edure..*/.static
1b788 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 65 70   int sqlite3Step
1b789 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c  (Vdbe *p){.  sql
1b78a 69 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20  ite3 *db;.  int 
1b78b 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 29  rc;..  assert(p)
1b78c 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63  ;.  if( p->magic
1b78d 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  !=VDBE_MAGIC_RUN
1b78e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
1b78f 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
1b790 7d 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74  }..  /* Assert t
1b791 68 61 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73  hat malloc() has
1b792 20 6e 6f 74 20 66 61 69 6c 65 64 20 2a 2f 0a 20   not failed */. 
1b793 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69   db = p->db;.  i
1b794 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
1b795 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72  led ){.    retur
1b796 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
1b797 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e 70 63    }..  if( p->pc
1b798 3c 3d 30 20 26 26 20 70 2d 3e 65 78 70 69 72 65  <=0 && p->expire
1b799 64 20 29 7b 0a 20 20 20 20 69 66 28 20 41 4c 57  d ){.    if( ALW
1b79a 41 59 53 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54  AYS(p->rc==SQLIT
1b79b 45 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 20 20 70  E_OK) ){.      p
1b79c 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 53 43  ->rc = SQLITE_SC
1b79d 48 45 4d 41 3b 0a 20 20 20 20 7d 0a 20 20 20 20  HEMA;.    }.    
1b79e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
1b79f 52 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  R;.    goto end_
1b7a0 6f 66 5f 73 74 65 70 3b 0a 20 20 7d 0a 20 20 69  of_step;.  }.  i
1b7a1 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1b7a2 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 70 2d  On(db) ){.    p-
1b7a3 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53  >rc = SQLITE_MIS
1b7a4 55 53 45 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  USE;.    return 
1b7a5 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
1b7a6 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 63 3c 30   }.  if( p->pc<0
1b7a7 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   ){.    /* If th
1b7a8 65 72 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72  ere are no other
1b7a9 20 73 74 61 74 65 6d 65 6e 74 73 20 63 75 72 72   statements curr
1b7aa 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 2c 20 74  ently running, t
1b7ab 68 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 65 74  hen.    ** reset
1b7ac 20 74 68 65 20 69 6e 74 65 72 72 75 70 74 20 66   the interrupt f
1b7ad 6c 61 67 2e 20 20 54 68 69 73 20 70 72 65 76 65  lag.  This preve
1b7ae 6e 74 73 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  nts a call to sq
1b7af 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 0a  lite3_interrupt.
1b7b0 20 20 20 20 2a 2a 20 66 72 6f 6d 20 69 6e 74 65      ** from inte
1b7b1 72 72 75 70 74 69 6e 67 20 61 20 73 74 61 74 65  rrupting a state
1b7b2 6d 65 6e 74 20 74 68 61 74 20 68 61 73 20 6e 6f  ment that has no
1b7b3 74 20 79 65 74 20 73 74 61 72 74 65 64 2e 0a 20  t yet started.. 
1b7b4 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62     */.    if( db
1b7b5 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3d  ->activeVdbeCnt=
1b7b6 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e  =0 ){.      db->
1b7b7 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64  u1.isInterrupted
1b7b8 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20   = 0;.    }..   
1b7b9 20 61 73 73 65 72 74 28 20 64 62 2d 3e 77 72 69   assert( db->wri
1b7ba 74 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20 64  teVdbeCnt>0 || d
1b7bb 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30  b->autoCommit==0
1b7bc 20 7c 7c 20 64 62 2d 3e 6e 44 65 66 65 72 72 65   || db->nDeferre
1b7bd 64 43 6f 6e 73 3d 3d 30 20 29 3b 0a 0a 23 69 66  dCons==0 );..#if
1b7be 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1b7bf 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28 20 64  _TRACE.    if( d
1b7c0 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26 20 21  b->xProfile && !
1b7c1 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b  db->init.busy ){
1b7c2 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 4e  .      double rN
1b7c3 6f 77 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ow;.      sqlite
1b7c4 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 64  3OsCurrentTime(d
1b7c5 62 2d 3e 70 56 66 73 2c 20 26 72 4e 6f 77 29 3b  b->pVfs, &rNow);
1b7c6 0a 20 20 20 20 20 20 70 2d 3e 73 74 61 72 74 54  .      p->startT
1b7c7 69 6d 65 20 3d 20 28 75 36 34 29 28 28 72 4e 6f  ime = (u64)((rNo
1b7c8 77 20 2d 20 28 69 6e 74 29 72 4e 6f 77 29 2a 33  w - (int)rNow)*3
1b7c9 36 30 30 2e 30 2a 32 34 2e 30 2a 31 30 30 30 30  600.0*24.0*10000
1b7ca 30 30 30 30 30 2e 30 29 3b 0a 20 20 20 20 7d 0a  00000.0);.    }.
1b7cb 23 65 6e 64 69 66 0a 0a 20 20 20 20 64 62 2d 3e  #endif..    db->
1b7cc 61 63 74 69 76 65 56 64 62 65 43 6e 74 2b 2b 3b  activeVdbeCnt++;
1b7cd 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 65 61 64  .    if( p->read
1b7ce 4f 6e 6c 79 3d 3d 30 20 29 20 64 62 2d 3e 77 72  Only==0 ) db->wr
1b7cf 69 74 65 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 20  iteVdbeCnt++;.  
1b7d0 20 20 70 2d 3e 70 63 20 3d 20 30 3b 0a 20 20 7d    p->pc = 0;.  }
1b7d1 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1b7d2 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 69  OMIT_EXPLAIN.  i
1b7d3 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 7b  f( p->explain ){
1b7d4 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1b7d5 33 56 64 62 65 4c 69 73 74 28 70 29 3b 0a 20 20  3VdbeList(p);.  
1b7d6 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20  }else.#endif /* 
1b7d7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
1b7d8 41 49 4e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 72  AIN */.  {.    r
1b7d9 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 45  c = sqlite3VdbeE
1b7da 78 65 63 28 70 29 3b 0a 20 20 7d 0a 0a 20 20 69  xec(p);.  }..  i
1b7db 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1b7dc 4f 66 66 28 64 62 29 20 29 7b 0a 20 20 20 20 72  Off(db) ){.    r
1b7dd 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  c = SQLITE_MISUS
1b7de 45 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20  E;.  }..#ifndef 
1b7df 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
1b7e0 45 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68  E.  /* Invoke th
1b7e1 65 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61  e profile callba
1b7e2 63 6b 20 69 66 20 74 68 65 72 65 20 69 73 20 6f  ck if there is o
1b7e3 6e 65 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  ne.  */.  if( rc
1b7e4 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20  !=SQLITE_ROW && 
1b7e5 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26 20  db->xProfile && 
1b7e6 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26  !db->init.busy &
1b7e7 26 20 70 2d 3e 7a 53 71 6c 20 29 7b 0a 20 20 20  & p->zSql ){.   
1b7e8 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20   double rNow;.  
1b7e9 20 20 75 36 34 20 65 6c 61 70 73 65 54 69 6d 65    u64 elapseTime
1b7ea 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73  ;..    sqlite3Os
1b7eb 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e  CurrentTime(db->
1b7ec 70 56 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20  pVfs, &rNow);.  
1b7ed 20 20 65 6c 61 70 73 65 54 69 6d 65 20 3d 20 28    elapseTime = (
1b7ee 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28 69 6e  u64)((rNow - (in
1b7ef 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a 32  t)rNow)*3600.0*2
1b7f0 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e 30  4.0*1000000000.0
1b7f1 29 3b 0a 20 20 20 20 65 6c 61 70 73 65 54 69 6d  );.    elapseTim
1b7f2 65 20 2d 3d 20 70 2d 3e 73 74 61 72 74 54 69 6d  e -= p->startTim
1b7f3 65 3b 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f 66  e;.    db->xProf
1b7f4 69 6c 65 28 64 62 2d 3e 70 50 72 6f 66 69 6c 65  ile(db->pProfile
1b7f5 41 72 67 2c 20 70 2d 3e 7a 53 71 6c 2c 20 65 6c  Arg, p->zSql, el
1b7f6 61 70 73 65 54 69 6d 65 29 3b 0a 20 20 7d 0a 23  apseTime);.  }.#
1b7f7 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 65 72 72  endif..  db->err
1b7f8 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 69 66 28  Code = rc;.  if(
1b7f9 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73   SQLITE_NOMEM==s
1b7fa 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 2d  qlite3ApiExit(p-
1b7fb 3e 64 62 2c 20 70 2d 3e 72 63 29 20 29 7b 0a 20  >db, p->rc) ){. 
1b7fc 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54     p->rc = SQLIT
1b7fd 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 65 6e 64  E_NOMEM;.  }.end
1b7fe 5f 6f 66 5f 73 74 65 70 3a 0a 20 20 2f 2a 20 41  _of_step:.  /* A
1b7ff 74 20 74 68 69 73 20 70 6f 69 6e 74 20 6c 6f 63  t this point loc
1b800 61 6c 20 76 61 72 69 61 62 6c 65 20 72 63 20 68  al variable rc h
1b801 6f 6c 64 73 20 74 68 65 20 76 61 6c 75 65 20 74  olds the value t
1b802 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 0a 20  hat should be . 
1b803 20 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 66 20   ** returned if 
1b804 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 77  this statement w
1b805 61 73 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e  as compiled usin
1b806 67 20 74 68 65 20 6c 65 67 61 63 79 20 0a 20 20  g the legacy .  
1b807 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  ** sqlite3_prepa
1b808 72 65 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20  re() interface. 
1b809 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  According to the
1b80a 20 64 6f 63 73 2c 20 74 68 69 73 20 63 61 6e 20   docs, this can 
1b80b 6f 6e 6c 79 0a 20 20 2a 2a 20 62 65 20 6f 6e 65  only.  ** be one
1b80c 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69   of the values i
1b80d 6e 20 74 68 65 20 66 69 72 73 74 20 61 73 73 65  n the first asse
1b80e 72 74 28 29 20 62 65 6c 6f 77 2e 20 56 61 72 69  rt() below. Vari
1b80f 61 62 6c 65 20 70 2d 3e 72 63 20 0a 20 20 2a 2a  able p->rc .  **
1b810 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 76 61   contains the va
1b811 6c 75 65 20 74 68 61 74 20 77 6f 75 6c 64 20 62  lue that would b
1b812 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 73 71  e returned if sq
1b813 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
1b814 20 0a 20 20 2a 2a 20 77 65 72 65 20 63 61 6c 6c   .  ** were call
1b815 65 64 20 6f 6e 20 73 74 61 74 65 6d 65 6e 74 20  ed on statement 
1b816 70 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  p..  */.  assert
1b817 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc==SQLITE_ROW
1b818 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f    || rc==SQLITE_
1b819 44 4f 4e 45 20 20 20 7c 7c 20 72 63 3d 3d 53 51  DONE   || rc==SQ
1b81a 4c 49 54 45 5f 45 52 52 4f 52 20 0a 20 20 20 20  LITE_ERROR .    
1b81b 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45     || rc==SQLITE
1b81c 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d 53 51 4c  _BUSY || rc==SQL
1b81d 49 54 45 5f 4d 49 53 55 53 45 0a 20 20 29 3b 0a  ITE_MISUSE.  );.
1b81e 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 21    assert( p->rc!
1b81f 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 70  =SQLITE_ROW && p
1b820 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ->rc!=SQLITE_DON
1b821 45 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 73  E );.  if( p->is
1b822 50 72 65 70 61 72 65 56 32 20 26 26 20 72 63 21  PrepareV2 && rc!
1b823 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 72  =SQLITE_ROW && r
1b824 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c!=SQLITE_DONE )
1b825 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  {.    /* If this
1b826 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 70   statement was p
1b827 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 73 71  repared using sq
1b828 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1b829 28 29 2c 20 61 6e 64 20 61 6e 0a 20 20 20 20 2a  (), and an.    *
1b82a 2a 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75  * error has occu
1b82b 72 65 64 2c 20 74 68 65 6e 20 72 65 74 75 72 6e  red, then return
1b82c 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
1b82d 69 6e 20 70 2d 3e 72 63 20 74 6f 20 74 68 65 0a  in p->rc to the.
1b82e 20 20 20 20 2a 2a 20 63 61 6c 6c 65 72 2e 20 53      ** caller. S
1b82f 65 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  et the error cod
1b830 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
1b831 65 20 68 61 6e 64 6c 65 20 74 6f 20 74 68 65 20  e handle to the 
1b832 73 61 6d 65 20 76 61 6c 75 65 2e 0a 20 20 20 20  same value..    
1b833 2a 2f 20 0a 20 20 20 20 72 63 20 3d 20 64 62 2d  */ .    rc = db-
1b834 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63  >errCode = p->rc
1b835 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  ;.  }.  return (
1b836 72 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3b  rc&db->errMask);
1b837 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69  .}../*.** This i
1b838 73 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20  s the top-level 
1b839 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
1b83a 66 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  f sqlite3_step()
1b83b 2e 20 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c 69 74  .  Call.** sqlit
1b83c 65 33 53 74 65 70 28 29 20 74 6f 20 64 6f 20 6d  e3Step() to do m
1b83d 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b 2e  ost of the work.
1b83e 20 20 49 66 20 61 20 73 63 68 65 6d 61 20 65 72    If a schema er
1b83f 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 63  ror occurs,.** c
1b840 61 6c 6c 20 73 71 6c 69 74 65 33 52 65 70 72 65  all sqlite3Repre
1b841 70 61 72 65 28 29 20 61 6e 64 20 74 72 79 20 61  pare() and try a
1b842 67 61 69 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  gain..*/.SQLITE_
1b843 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1b844 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d  step(sqlite3_stm
1b845 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74  t *pStmt){.  int
1b846 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53   rc = SQLITE_MIS
1b847 55 53 45 3b 0a 20 20 69 66 28 20 70 53 74 6d 74  USE;.  if( pStmt
1b848 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20   ){.    int cnt 
1b849 3d 20 30 3b 0a 20 20 20 20 56 64 62 65 20 2a 76  = 0;.    Vdbe *v
1b84a 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b   = (Vdbe*)pStmt;
1b84b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
1b84c 20 3d 20 76 2d 3e 64 62 3b 0a 20 20 20 20 73 71   = v->db;.    sq
1b84d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
1b84e 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
1b84f 20 20 77 68 69 6c 65 28 20 28 72 63 20 3d 20 73    while( (rc = s
1b850 71 6c 69 74 65 33 53 74 65 70 28 76 29 29 3d 3d  qlite3Step(v))==
1b851 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 0a 20 20  SQLITE_SCHEMA.  
1b852 20 20 20 20 20 20 20 20 20 26 26 20 63 6e 74 2b           && cnt+
1b853 2b 20 3c 20 35 0a 20 20 20 20 20 20 20 20 20 20  + < 5.          
1b854 20 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65   && (rc = sqlite
1b855 33 52 65 70 72 65 70 61 72 65 28 76 29 29 3d 3d  3Reprepare(v))==
1b856 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1b857 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74     sqlite3_reset
1b858 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 76  (pStmt);.      v
1b859 2d 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 20  ->expired = 0;. 
1b85a 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d     }.    if( rc=
1b85b 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 26  =SQLITE_SCHEMA &
1b85c 26 20 41 4c 57 41 59 53 28 76 2d 3e 69 73 50 72  & ALWAYS(v->isPr
1b85d 65 70 61 72 65 56 32 29 20 26 26 20 41 4c 57 41  epareV2) && ALWA
1b85e 59 53 28 64 62 2d 3e 70 45 72 72 29 20 29 7b 0a  YS(db->pErr) ){.
1b85f 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61        /* This ca
1b860 73 65 20 6f 63 63 75 72 73 20 61 66 74 65 72 20  se occurs after 
1b861 66 61 69 6c 69 6e 67 20 74 6f 20 72 65 63 6f 6d  failing to recom
1b862 70 69 6c 65 20 61 6e 20 73 71 6c 20 73 74 61 74  pile an sql stat
1b863 65 6d 65 6e 74 2e 20 0a 20 20 20 20 20 20 2a 2a  ement. .      **
1b864 20 54 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   The error messa
1b865 67 65 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 20  ge from the SQL 
1b866 63 6f 6d 70 69 6c 65 72 20 68 61 73 20 61 6c 72  compiler has alr
1b867 65 61 64 79 20 62 65 65 6e 20 6c 6f 61 64 65 64  eady been loaded
1b868 20 0a 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20   .      ** into 
1b869 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
1b86a 64 6c 65 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20  dle. This block 
1b86b 63 6f 70 69 65 73 20 74 68 65 20 65 72 72 6f 72  copies the error
1b86c 20 6d 65 73 73 61 67 65 20 0a 20 20 20 20 20 20   message .      
1b86d 2a 2a 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ** from the data
1b86e 62 61 73 65 20 68 61 6e 64 6c 65 20 69 6e 74 6f  base handle into
1b86f 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 61   the statement a
1b870 6e 64 20 73 65 74 73 20 74 68 65 20 73 74 61 74  nd sets the stat
1b871 65 6d 65 6e 74 0a 20 20 20 20 20 20 2a 2a 20 70  ement.      ** p
1b872 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 74  rogram counter t
1b873 6f 20 30 20 74 6f 20 65 6e 73 75 72 65 20 74 68  o 0 to ensure th
1b874 61 74 20 77 68 65 6e 20 74 68 65 20 73 74 61 74  at when the stat
1b875 65 6d 65 6e 74 20 69 73 20 0a 20 20 20 20 20 20  ement is .      
1b876 2a 2a 20 66 69 6e 61 6c 69 7a 65 64 20 6f 72 20  ** finalized or 
1b877 72 65 73 65 74 20 74 68 65 20 70 61 72 73 65 72  reset the parser
1b878 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
1b879 73 20 61 76 61 69 6c 61 62 6c 65 20 76 69 61 0a  s available via.
1b87a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33        ** sqlite3
1b87b 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71  _errmsg() and sq
1b87c 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 2e  lite3_errcode().
1b87d 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1b87e 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72  const char *zErr
1b87f 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
1b880 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
1b881 65 78 74 28 64 62 2d 3e 70 45 72 72 29 3b 20 0a  ext(db->pErr); .
1b882 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1b883 72 65 65 28 64 62 2c 20 76 2d 3e 7a 45 72 72 4d  ree(db, v->zErrM
1b884 73 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  sg);.      if( !
1b885 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1b886 20 29 7b 0a 20 20 20 20 20 20 20 20 76 2d 3e 7a   ){.        v->z
1b887 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
1b888 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a 45 72  DbStrDup(db, zEr
1b889 72 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65  r);.      } else
1b88a 20 7b 0a 20 20 20 20 20 20 20 20 76 2d 3e 7a 45   {.        v->zE
1b88b 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 20  rrMsg = 0;.     
1b88c 20 20 20 76 2d 3e 72 63 20 3d 20 53 51 4c 49 54     v->rc = SQLIT
1b88d 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
1b88e 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
1b88f 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
1b890 62 2c 20 72 63 29 3b 0a 20 20 20 20 73 71 6c 69  b, rc);.    sqli
1b891 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1b892 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a  db->mutex);.  }.
1b893 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1b894 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68  /*.** Extract th
1b895 65 20 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d  e user data from
1b896 20 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   a sqlite3_conte
1b897 78 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64  xt structure and
1b898 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69   return a.** poi
1b899 6e 74 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53  nter to it..*/.S
1b89a 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
1b89b 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
1b89c 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  a(sqlite3_contex
1b89d 74 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  t *p){.  assert(
1b89e 20 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 29   p && p->pFunc )
1b89f 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 46  ;.  return p->pF
1b8a0 75 6e 63 2d 3e 70 55 73 65 72 44 61 74 61 3b 0a  unc->pUserData;.
1b8a1 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74  }../*.** Extract
1b8a2 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20 66   the user data f
1b8a3 72 6f 6d 20 61 20 73 71 6c 69 74 65 33 5f 63 6f  rom a sqlite3_co
1b8a4 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 65 20  ntext structure 
1b8a5 61 6e 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20  and return a.** 
1b8a6 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 0a 2a  pointer to it..*
1b8a7 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  /.SQLITE_API sql
1b8a8 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 63 6f  ite3 *sqlite3_co
1b8a9 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
1b8aa 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1b8ab 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
1b8ac 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a   && p->pFunc );.
1b8ad 20 20 72 65 74 75 72 6e 20 70 2d 3e 73 2e 64 62    return p->s.db
1b8ae 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  ;.}../*.** The f
1b8af 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 68 65 20  ollowing is the 
1b8b0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
1b8b1 66 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f  f an SQL functio
1b8b2 6e 20 74 68 61 74 20 61 6c 77 61 79 73 0a 2a 2a  n that always.**
1b8b3 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 20 65   fails with an e
1b8b4 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 61  rror message sta
1b8b5 74 69 6e 67 20 74 68 61 74 20 74 68 65 20 66 75  ting that the fu
1b8b6 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69  nction is used i
1b8b7 6e 20 74 68 65 0a 2a 2a 20 77 72 6f 6e 67 20 63  n the.** wrong c
1b8b8 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 73 71 6c  ontext.  The sql
1b8b9 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75  ite3_overload_fu
1b8ba 6e 63 74 69 6f 6e 28 29 20 41 50 49 20 6d 69 67  nction() API mig
1b8bb 68 74 20 63 6f 6e 73 74 72 75 63 74 0a 2a 2a 20  ht construct.** 
1b8bc 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  SQL function tha
1b8bd 74 20 75 73 65 20 74 68 69 73 20 72 6f 75 74 69  t use this routi
1b8be 6e 65 20 73 6f 20 74 68 61 74 20 74 68 65 20 66  ne so that the f
1b8bf 75 6e 63 74 69 6f 6e 73 20 77 69 6c 6c 20 65 78  unctions will ex
1b8c0 69 73 74 0a 2a 2a 20 66 6f 72 20 6e 61 6d 65 20  ist.** for name 
1b8c1 72 65 73 6f 6c 75 74 69 6f 6e 20 62 75 74 20 61  resolution but a
1b8c2 72 65 20 61 63 74 75 61 6c 6c 79 20 6f 76 65 72  re actually over
1b8c3 6c 6f 61 64 65 64 20 62 79 20 74 68 65 20 78 46  loaded by the xF
1b8c4 69 6e 64 46 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d  indFunction.** m
1b8c5 65 74 68 6f 64 20 6f 66 20 76 69 72 74 75 61 6c  ethod of virtual
1b8c6 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49   tables..*/.SQLI
1b8c7 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1b8c8 73 71 6c 69 74 65 33 49 6e 76 61 6c 69 64 46 75  sqlite3InvalidFu
1b8c9 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65  nction(.  sqlite
1b8ca 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
1b8cb 78 74 2c 20 20 2f 2a 20 54 68 65 20 66 75 6e 63  xt,  /* The func
1b8cc 74 69 6f 6e 20 63 61 6c 6c 69 6e 67 20 63 6f 6e  tion calling con
1b8cd 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 4e 6f  text */.  int No
1b8ce 74 55 73 65 64 2c 20 20 20 20 20 20 20 20 20 20  tUsed,          
1b8cf 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1b8d0 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74  f arguments to t
1b8d1 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  he function */. 
1b8d2 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1b8d3 2a 4e 6f 74 55 73 65 64 32 20 20 20 2f 2a 20 56  *NotUsed2   /* V
1b8d4 61 6c 75 65 20 6f 66 20 65 61 63 68 20 61 72 67  alue of each arg
1b8d5 75 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 63 6f  ument */.){.  co
1b8d6 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20  nst char *zName 
1b8d7 3d 20 63 6f 6e 74 65 78 74 2d 3e 70 46 75 6e 63  = context->pFunc
1b8d8 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 63 68 61 72 20  ->zName;.  char 
1b8d9 2a 7a 45 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f  *zErr;.  UNUSED_
1b8da 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73  PARAMETER2(NotUs
1b8db 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20  ed, NotUsed2);. 
1b8dc 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f   zErr = sqlite3_
1b8dd 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 20 20 22  mprintf(.      "
1b8de 75 6e 61 62 6c 65 20 74 6f 20 75 73 65 20 66 75  unable to use fu
1b8df 6e 63 74 69 6f 6e 20 25 73 20 69 6e 20 74 68 65  nction %s in the
1b8e0 20 72 65 71 75 65 73 74 65 64 20 63 6f 6e 74 65   requested conte
1b8e1 78 74 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73  xt", zName);.  s
1b8e2 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
1b8e3 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72  ror(context, zEr
1b8e4 72 2c 20 2d 31 29 3b 0a 20 20 73 71 6c 69 74 65  r, -1);.  sqlite
1b8e5 33 5f 66 72 65 65 28 7a 45 72 72 29 3b 0a 7d 0a  3_free(zErr);.}.
1b8e6 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
1b8e7 6f 72 20 72 65 74 75 72 6e 20 74 68 65 20 61 67  or return the ag
1b8e8 67 72 65 67 61 74 65 20 63 6f 6e 74 65 78 74 20  gregate context 
1b8e9 66 6f 72 20 61 20 75 73 65 72 20 66 75 6e 63 74  for a user funct
1b8ea 69 6f 6e 2e 20 20 41 20 6e 65 77 0a 2a 2a 20 63  ion.  A new.** c
1b8eb 6f 6e 74 65 78 74 20 69 73 20 61 6c 6c 6f 63 61  ontext is alloca
1b8ec 74 65 64 20 6f 6e 20 74 68 65 20 66 69 72 73 74  ted on the first
1b8ed 20 63 61 6c 6c 2e 20 20 53 75 62 73 65 71 75 65   call.  Subseque
1b8ee 6e 74 20 63 61 6c 6c 73 20 72 65 74 75 72 6e 20  nt calls return 
1b8ef 74 68 65 0a 2a 2a 20 73 61 6d 65 20 63 6f 6e 74  the.** same cont
1b8f0 65 78 74 20 74 68 61 74 20 77 61 73 20 72 65 74  ext that was ret
1b8f1 75 72 6e 65 64 20 6f 6e 20 70 72 69 6f 72 20 63  urned on prior c
1b8f2 61 6c 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  alls..*/.SQLITE_
1b8f3 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  API void *sqlite
1b8f4 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  3_aggregate_cont
1b8f5 65 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ext(sqlite3_cont
1b8f6 65 78 74 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74  ext *p, int nByt
1b8f7 65 29 7b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b  e){.  Mem *pMem;
1b8f8 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20  .  assert( p && 
1b8f9 70 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70  p->pFunc && p->p
1b8fa 46 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20  Func->xStep );. 
1b8fb 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1b8fc 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 73  _mutex_held(p->s
1b8fd 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1b8fe 20 70 4d 65 6d 20 3d 20 70 2d 3e 70 4d 65 6d 3b   pMem = p->pMem;
1b8ff 0a 20 20 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c  .  if( (pMem->fl
1b900 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d  ags & MEM_Agg)==
1b901 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 42 79  0 ){.    if( nBy
1b902 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  te==0 ){.      s
1b903 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1b904 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 4d 65  easeExternal(pMe
1b905 6d 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  m);.      pMem->
1b906 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1b907 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20  ;.      pMem->z 
1b908 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 0;.    }else{.
1b909 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1b90a 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e  eMemGrow(pMem, n
1b90b 42 79 74 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Byte, 0);.      
1b90c 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
1b90d 4d 5f 41 67 67 3b 0a 20 20 20 20 20 20 70 4d 65  M_Agg;.      pMe
1b90e 6d 2d 3e 75 2e 70 44 65 66 20 3d 20 70 2d 3e 70  m->u.pDef = p->p
1b90f 46 75 6e 63 3b 0a 20 20 20 20 20 20 69 66 28 20  Func;.      if( 
1b910 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 20  pMem->z ){.     
1b911 20 20 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e     memset(pMem->
1b912 7a 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20  z, 0, nByte);.  
1b913 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1b914 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29    return (void*)
1b915 70 4d 65 6d 2d 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a  pMem->z;.}../*.*
1b916 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61 75 78  * Return the aux
1b917 69 6c 61 72 79 20 64 61 74 61 20 70 6f 69 6e 74  ilary data point
1b918 65 72 2c 20 69 66 20 61 6e 79 2c 20 66 6f 72 20  er, if any, for 
1b919 74 68 65 20 69 41 72 67 27 74 68 20 61 72 67 75  the iArg'th argu
1b91a 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 75  ment to.** the u
1b91b 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66  ser-function def
1b91c 69 6e 65 64 20 62 79 20 70 43 74 78 2e 0a 2a 2f  ined by pCtx..*/
1b91d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1b91e 20 2a 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75   *sqlite3_get_au
1b91f 78 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63 6f  xdata(sqlite3_co
1b920 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74  ntext *pCtx, int
1b921 20 69 41 72 67 29 7b 0a 20 20 56 64 62 65 46 75   iArg){.  VdbeFu
1b922 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 0a 0a  nc *pVdbeFunc;..
1b923 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1b924 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
1b925 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
1b926 29 3b 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d  );.  pVdbeFunc =
1b927 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63   pCtx->pVdbeFunc
1b928 3b 0a 20 20 69 66 28 20 21 70 56 64 62 65 46 75  ;.  if( !pVdbeFu
1b929 6e 63 20 7c 7c 20 69 41 72 67 3e 3d 70 56 64 62  nc || iArg>=pVdb
1b92a 65 46 75 6e 63 2d 3e 6e 41 75 78 20 7c 7c 20 69  eFunc->nAux || i
1b92b 41 72 67 3c 30 20 29 7b 0a 20 20 20 20 72 65 74  Arg<0 ){.    ret
1b92c 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  urn 0;.  }.  ret
1b92d 75 72 6e 20 70 56 64 62 65 46 75 6e 63 2d 3e 61  urn pVdbeFunc->a
1b92e 70 41 75 78 5b 69 41 72 67 5d 2e 70 41 75 78 3b  pAux[iArg].pAux;
1b92f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
1b930 65 20 61 75 78 69 6c 61 72 79 20 64 61 74 61 20  e auxilary data 
1b931 70 6f 69 6e 74 65 72 20 61 6e 64 20 64 65 6c 65  pointer and dele
1b932 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 66 6f 72  te function, for
1b933 20 74 68 65 20 69 41 72 67 27 74 68 0a 2a 2a 20   the iArg'th.** 
1b934 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
1b935 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65  user-function de
1b936 66 69 6e 65 64 20 62 79 20 70 43 74 78 2e 20 41  fined by pCtx. A
1b937 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75  ny previous valu
1b938 65 20 69 73 0a 2a 2a 20 64 65 6c 65 74 65 64 20  e is.** deleted 
1b939 62 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 64  by calling the d
1b93a 65 6c 65 74 65 20 66 75 6e 63 74 69 6f 6e 20 73  elete function s
1b93b 70 65 63 69 66 69 65 64 20 77 68 65 6e 20 69 74  pecified when it
1b93c 20 77 61 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c   was set..*/.SQL
1b93d 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
1b93e 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
1b93f 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
1b940 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 69 6e  ext *pCtx, .  in
1b941 74 20 69 41 72 67 2c 20 0a 20 20 76 6f 69 64 20  t iArg, .  void 
1b942 2a 70 41 75 78 2c 20 0a 20 20 76 6f 69 64 20 28  *pAux, .  void (
1b943 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64 2a 29  *xDelete)(void*)
1b944 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 41 75 78  .){.  struct Aux
1b945 44 61 74 61 20 2a 70 41 75 78 44 61 74 61 3b 0a  Data *pAuxData;.
1b946 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62    VdbeFunc *pVdb
1b947 65 46 75 6e 63 3b 0a 20 20 69 66 28 20 69 41 72  eFunc;.  if( iAr
1b948 67 3c 30 20 29 20 67 6f 74 6f 20 66 61 69 6c 65  g<0 ) goto faile
1b949 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  d;..  assert( sq
1b94a 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1b94b 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74  (pCtx->s.db->mut
1b94c 65 78 29 20 29 3b 0a 20 20 70 56 64 62 65 46 75  ex) );.  pVdbeFu
1b94d 6e 63 20 3d 20 70 43 74 78 2d 3e 70 56 64 62 65  nc = pCtx->pVdbe
1b94e 46 75 6e 63 3b 0a 20 20 69 66 28 20 21 70 56 64  Func;.  if( !pVd
1b94f 62 65 46 75 6e 63 20 7c 7c 20 70 56 64 62 65 46  beFunc || pVdbeF
1b950 75 6e 63 2d 3e 6e 41 75 78 3c 3d 69 41 72 67 20  unc->nAux<=iArg 
1b951 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 75 78 20  ){.    int nAux 
1b952 3d 20 28 70 56 64 62 65 46 75 6e 63 20 3f 20 70  = (pVdbeFunc ? p
1b953 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 20 3a  VdbeFunc->nAux :
1b954 20 30 29 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61   0);.    int nMa
1b955 6c 6c 6f 63 20 3d 20 73 69 7a 65 6f 66 28 56 64  lloc = sizeof(Vd
1b956 62 65 46 75 6e 63 29 20 2b 20 73 69 7a 65 6f 66  beFunc) + sizeof
1b957 28 73 74 72 75 63 74 20 41 75 78 44 61 74 61 29  (struct AuxData)
1b958 2a 69 41 72 67 3b 0a 20 20 20 20 70 56 64 62 65  *iArg;.    pVdbe
1b959 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 44 62  Func = sqlite3Db
1b95a 52 65 61 6c 6c 6f 63 28 70 43 74 78 2d 3e 73 2e  Realloc(pCtx->s.
1b95b 64 62 2c 20 70 56 64 62 65 46 75 6e 63 2c 20 6e  db, pVdbeFunc, n
1b95c 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 69 66 28  Malloc);.    if(
1b95d 20 21 70 56 64 62 65 46 75 6e 63 20 29 7b 0a 20   !pVdbeFunc ){. 
1b95e 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64       goto failed
1b95f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 74 78  ;.    }.    pCtx
1b960 2d 3e 70 56 64 62 65 46 75 6e 63 20 3d 20 70 56  ->pVdbeFunc = pV
1b961 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 6d 65 6d  dbeFunc;.    mem
1b962 73 65 74 28 26 70 56 64 62 65 46 75 6e 63 2d 3e  set(&pVdbeFunc->
1b963 61 70 41 75 78 5b 6e 41 75 78 5d 2c 20 30 2c 20  apAux[nAux], 0, 
1b964 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 41 75  sizeof(struct Au
1b965 78 44 61 74 61 29 2a 28 69 41 72 67 2b 31 2d 6e  xData)*(iArg+1-n
1b966 41 75 78 29 29 3b 0a 20 20 20 20 70 56 64 62 65  Aux));.    pVdbe
1b967 46 75 6e 63 2d 3e 6e 41 75 78 20 3d 20 69 41 72  Func->nAux = iAr
1b968 67 2b 31 3b 0a 20 20 20 20 70 56 64 62 65 46 75  g+1;.    pVdbeFu
1b969 6e 63 2d 3e 70 46 75 6e 63 20 3d 20 70 43 74 78  nc->pFunc = pCtx
1b96a 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20  ->pFunc;.  }..  
1b96b 70 41 75 78 44 61 74 61 20 3d 20 26 70 56 64 62  pAuxData = &pVdb
1b96c 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 69 41 72  eFunc->apAux[iAr
1b96d 67 5d 3b 0a 20 20 69 66 28 20 70 41 75 78 44 61  g];.  if( pAuxDa
1b96e 74 61 2d 3e 70 41 75 78 20 26 26 20 70 41 75 78  ta->pAux && pAux
1b96f 44 61 74 61 2d 3e 78 44 65 6c 65 74 65 20 29 7b  Data->xDelete ){
1b970 0a 20 20 20 20 70 41 75 78 44 61 74 61 2d 3e 78  .    pAuxData->x
1b971 44 65 6c 65 74 65 28 70 41 75 78 44 61 74 61 2d  Delete(pAuxData-
1b972 3e 70 41 75 78 29 3b 0a 20 20 7d 0a 20 20 70 41  >pAux);.  }.  pA
1b973 75 78 44 61 74 61 2d 3e 70 41 75 78 20 3d 20 70  uxData->pAux = p
1b974 41 75 78 3b 0a 20 20 70 41 75 78 44 61 74 61 2d  Aux;.  pAuxData-
1b975 3e 78 44 65 6c 65 74 65 20 3d 20 78 44 65 6c 65  >xDelete = xDele
1b976 74 65 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 66  te;.  return;..f
1b977 61 69 6c 65 64 3a 0a 20 20 69 66 28 20 78 44 65  ailed:.  if( xDe
1b978 6c 65 74 65 20 29 7b 0a 20 20 20 20 78 44 65 6c  lete ){.    xDel
1b979 65 74 65 28 70 41 75 78 29 3b 0a 20 20 7d 0a 7d  ete(pAux);.  }.}
1b97a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1b97b 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
1b97c 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
1b97d 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65  e number of time
1b97e 73 20 74 68 65 20 53 74 65 70 20 66 75 6e 63 74  s the Step funct
1b97f 69 6f 6e 20 6f 66 20 61 20 61 67 67 72 65 67 61  ion of a aggrega
1b980 74 65 20 68 61 73 20 62 65 65 6e 20 0a 2a 2a 20  te has been .** 
1b981 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  called..**.** Th
1b982 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64  is function is d
1b983 65 70 72 65 63 61 74 65 64 2e 20 20 44 6f 20 6e  eprecated.  Do n
1b984 6f 74 20 75 73 65 20 69 74 20 66 6f 72 20 6e 65  ot use it for ne
1b985 77 20 63 6f 64 65 2e 20 20 49 74 20 69 73 0a 2a  w code.  It is.*
1b986 2a 20 70 72 6f 76 69 64 65 20 6f 6e 6c 79 20 74  * provide only t
1b987 6f 20 61 76 6f 69 64 20 62 72 65 61 6b 69 6e 67  o avoid breaking
1b988 20 6c 65 67 61 63 79 20 63 6f 64 65 2e 20 20 4e   legacy code.  N
1b989 65 77 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  ew aggregate fun
1b98a 63 74 69 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65  ction.** impleme
1b98b 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20  ntations should 
1b98c 6b 65 65 70 20 74 68 65 69 72 20 6f 77 6e 20 63  keep their own c
1b98d 6f 75 6e 74 73 20 77 69 74 68 69 6e 20 74 68 65  ounts within the
1b98e 69 72 20 61 67 67 72 65 67 61 74 65 0a 2a 2a 20  ir aggregate.** 
1b98f 63 6f 6e 74 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49  context..*/.SQLI
1b990 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1b991 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75  e3_aggregate_cou
1b992 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  nt(sqlite3_conte
1b993 78 74 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  xt *p){.  assert
1b994 28 20 70 20 26 26 20 70 2d 3e 70 4d 65 6d 20 26  ( p && p->pMem &
1b995 26 20 70 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d  & p->pFunc && p-
1b996 3e 70 46 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b  >pFunc->xStep );
1b997 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 4d 65  .  return p->pMe
1b998 6d 2d 3e 6e 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  m->n;.}.#endif..
1b999 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
1b99a 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
1b99b 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ns in the result
1b99c 20 73 65 74 20 66 6f 72 20 74 68 65 20 73 74 61   set for the sta
1b99d 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f  tement pStmt..*/
1b99e 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1b99f 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63  sqlite3_column_c
1b9a0 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  ount(sqlite3_stm
1b9a1 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62  t *pStmt){.  Vdb
1b9a2 65 20 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 2a  e *pVm = (Vdbe *
1b9a3 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e  )pStmt;.  return
1b9a4 20 70 56 6d 20 3f 20 70 56 6d 2d 3e 6e 52 65 73   pVm ? pVm->nRes
1b9a5 43 6f 6c 75 6d 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f  Column : 0;.}../
1b9a6 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1b9a7 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73  number of values
1b9a8 20 61 76 61 69 6c 61 62 6c 65 20 66 72 6f 6d 20   available from 
1b9a9 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20  the current row 
1b9aa 6f 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e  of the.** curren
1b9ab 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 73 74  tly executing st
1b9ac 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a  atement pStmt..*
1b9ad 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1b9ae 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f   sqlite3_data_co
1b9af 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  unt(sqlite3_stmt
1b9b0 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65   *pStmt){.  Vdbe
1b9b1 20 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29   *pVm = (Vdbe *)
1b9b2 70 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d  pStmt;.  if( pVm
1b9b3 3d 3d 30 20 7c 7c 20 70 56 6d 2d 3e 70 52 65 73  ==0 || pVm->pRes
1b9b4 75 6c 74 53 65 74 3d 3d 30 20 29 20 72 65 74 75  ultSet==0 ) retu
1b9b5 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70  rn 0;.  return p
1b9b6 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a  Vm->nResColumn;.
1b9b7 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  }.../*.** Check 
1b9b8 74 6f 20 73 65 65 20 69 66 20 63 6f 6c 75 6d 6e  to see if column
1b9b9 20 69 43 6f 6c 20 6f 66 20 74 68 65 20 67 69 76   iCol of the giv
1b9ba 65 6e 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  en statement is 
1b9bb 76 61 6c 69 64 2e 20 20 49 66 0a 2a 2a 20 69 74  valid.  If.** it
1b9bc 20 69 73 2c 20 72 65 74 75 72 6e 20 61 20 70 6f   is, return a po
1b9bd 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4d 65 6d  inter to the Mem
1b9be 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20 6f   for the value o
1b9bf 66 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a 2a  f that column..*
1b9c0 2a 20 49 66 20 69 43 6f 6c 20 69 73 20 6e 6f 74  * If iCol is not
1b9c1 20 76 61 6c 69 64 2c 20 72 65 74 75 72 6e 20 61   valid, return a
1b9c2 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65   pointer to a Me
1b9c3 6d 20 77 68 69 63 68 20 68 61 73 20 61 20 76 61  m which has a va
1b9c4 6c 75 65 0a 2a 2a 20 6f 66 20 4e 55 4c 4c 2e 0a  lue.** of NULL..
1b9c5 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 20 2a 63  */.static Mem *c
1b9c6 6f 6c 75 6d 6e 4d 65 6d 28 73 71 6c 69 74 65 33  olumnMem(sqlite3
1b9c7 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1b9c8 74 20 69 29 7b 0a 20 20 56 64 62 65 20 2a 70 56  t i){.  Vdbe *pV
1b9c9 6d 3b 0a 20 20 69 6e 74 20 76 61 6c 73 3b 0a 20  m;.  int vals;. 
1b9ca 20 4d 65 6d 20 2a 70 4f 75 74 3b 0a 0a 20 20 70   Mem *pOut;..  p
1b9cb 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74  Vm = (Vdbe *)pSt
1b9cc 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d 20 26 26  mt;.  if( pVm &&
1b9cd 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74   pVm->pResultSet
1b9ce 21 3d 30 20 26 26 20 69 3c 70 56 6d 2d 3e 6e 52  !=0 && i<pVm->nR
1b9cf 65 73 43 6f 6c 75 6d 6e 20 26 26 20 69 3e 3d 30  esColumn && i>=0
1b9d0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
1b9d1 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d  mutex_enter(pVm-
1b9d2 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  >db->mutex);.   
1b9d3 20 76 61 6c 73 20 3d 20 73 71 6c 69 74 65 33 5f   vals = sqlite3_
1b9d4 64 61 74 61 5f 63 6f 75 6e 74 28 70 53 74 6d 74  data_count(pStmt
1b9d5 29 3b 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 70  );.    pOut = &p
1b9d6 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 5b 69  Vm->pResultSet[i
1b9d7 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ];.  }else{.    
1b9d8 2f 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  /* If the value 
1b9d9 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65  passed as the se
1b9da 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
1b9db 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 72   out of range, r
1b9dc 65 74 75 72 6e 0a 20 20 20 20 2a 2a 20 61 20 70  eturn.    ** a p
1b9dd 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 6f  ointer to the fo
1b9de 6c 6c 6f 77 69 6e 67 20 73 74 61 74 69 63 20 4d  llowing static M
1b9df 65 6d 20 6f 62 6a 65 63 74 20 77 68 69 63 68 20  em object which 
1b9e0 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 20 20 20  contains the.   
1b9e1 20 2a 2a 20 76 61 6c 75 65 20 53 51 4c 20 4e 55   ** value SQL NU
1b9e2 4c 4c 2e 20 45 76 65 6e 20 74 68 6f 75 67 68 20  LL. Even though 
1b9e3 74 68 65 20 4d 65 6d 20 73 74 72 75 63 74 75 72  the Mem structur
1b9e4 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 6c  e contains an el
1b9e5 65 6d 65 6e 74 0a 20 20 20 20 2a 2a 20 6f 66 20  ement.    ** of 
1b9e6 74 79 70 65 20 69 36 34 2c 20 6f 6e 20 63 65 72  type i64, on cer
1b9e7 74 61 69 6e 20 61 72 63 68 69 74 65 63 74 75 72  tain architectur
1b9e8 65 20 28 78 38 36 29 20 77 69 74 68 20 63 65 72  e (x86) with cer
1b9e9 74 61 69 6e 20 63 6f 6d 70 69 6c 65 72 0a 20 20  tain compiler.  
1b9ea 20 20 2a 2a 20 73 77 69 74 63 68 65 73 20 28 2d    ** switches (-
1b9eb 4f 73 29 2c 20 67 63 63 20 6d 61 79 20 61 6c 69  Os), gcc may ali
1b9ec 67 6e 20 74 68 69 73 20 4d 65 6d 20 6f 62 6a 65  gn this Mem obje
1b9ed 63 74 20 6f 6e 20 61 20 34 2d 62 79 74 65 20 62  ct on a 4-byte b
1b9ee 6f 75 6e 64 61 72 79 0a 20 20 20 20 2a 2a 20 69  oundary.    ** i
1b9ef 6e 73 74 65 61 64 20 6f 66 20 61 6e 20 38 2d 62  nstead of an 8-b
1b9f0 79 74 65 20 6f 6e 65 2e 20 54 68 69 73 20 61 6c  yte one. This al
1b9f1 6c 20 77 6f 72 6b 73 20 66 69 6e 65 2c 20 65 78  l works fine, ex
1b9f2 63 65 70 74 20 74 68 61 74 20 77 68 65 6e 0a 20  cept that when. 
1b9f3 20 20 20 2a 2a 20 72 75 6e 6e 69 6e 67 20 77 69     ** running wi
1b9f4 74 68 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  th SQLITE_DEBUG 
1b9f5 64 65 66 69 6e 65 64 20 74 68 65 20 53 51 4c 69  defined the SQLi
1b9f6 74 65 20 63 6f 64 65 20 73 6f 6d 65 74 69 6d 65  te code sometime
1b9f7 73 20 61 73 73 65 72 74 28 29 73 0a 20 20 20 20  s assert()s.    
1b9f8 2a 2a 20 74 68 61 74 20 61 20 4d 65 6d 20 73 74  ** that a Mem st
1b9f9 72 75 63 74 75 72 65 20 69 73 20 6c 6f 63 61 74  ructure is locat
1b9fa 65 64 20 6f 6e 20 61 6e 20 38 2d 62 79 74 65 20  ed on an 8-byte 
1b9fb 62 6f 75 6e 64 61 72 79 2e 20 54 6f 20 70 72 65  boundary. To pre
1b9fc 76 65 6e 74 0a 20 20 20 20 2a 2a 20 74 68 69 73  vent.    ** this
1b9fd 20 61 73 73 65 72 74 28 29 20 66 72 6f 6d 20 66   assert() from f
1b9fe 61 69 6c 69 6e 67 2c 20 77 68 65 6e 20 62 75 69  ailing, when bui
1b9ff 6c 64 69 6e 67 20 77 69 74 68 20 53 51 4c 49 54  lding with SQLIT
1ba00 45 5f 44 45 42 55 47 20 64 65 66 69 6e 65 64 0a  E_DEBUG defined.
1ba01 20 20 20 20 2a 2a 20 75 73 69 6e 67 20 67 63 63      ** using gcc
1ba02 2c 20 66 6f 72 63 65 20 6e 75 6c 6c 4d 65 6d 20  , force nullMem 
1ba03 74 6f 20 62 65 20 38 2d 62 79 74 65 20 61 6c 69  to be 8-byte ali
1ba04 67 6e 65 64 20 75 73 69 6e 67 20 74 68 65 20 6d  gned using the m
1ba05 61 67 69 63 61 6c 0a 20 20 20 20 2a 2a 20 5f 5f  agical.    ** __
1ba06 61 74 74 72 69 62 75 74 65 5f 5f 28 28 61 6c 69  attribute__((ali
1ba07 67 6e 65 64 28 38 29 29 29 20 6d 61 63 72 6f 2e  gned(8))) macro.
1ba08 20 20 2a 2f 0a 20 20 20 20 73 74 61 74 69 63 20    */.    static 
1ba09 63 6f 6e 73 74 20 4d 65 6d 20 6e 75 6c 6c 4d 65  const Mem nullMe
1ba0a 6d 20 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  m .#if defined(S
1ba0b 51 4c 49 54 45 5f 44 45 42 55 47 29 20 26 26 20  QLITE_DEBUG) && 
1ba0c 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f  defined(__GNUC__
1ba0d 29 0a 20 20 20 20 20 20 5f 5f 61 74 74 72 69 62  ).      __attrib
1ba0e 75 74 65 5f 5f 28 28 61 6c 69 67 6e 65 64 28 38  ute__((aligned(8
1ba0f 29 29 29 20 0a 23 65 6e 64 69 66 0a 20 20 20 20  ))) .#endif.    
1ba10 20 20 3d 20 7b 7b 30 7d 2c 20 28 64 6f 75 62 6c    = {{0}, (doubl
1ba11 65 29 30 2c 20 30 2c 20 22 22 2c 20 30 2c 20 4d  e)0, 0, "", 0, M
1ba12 45 4d 5f 4e 75 6c 6c 2c 20 53 51 4c 49 54 45 5f  EM_Null, SQLITE_
1ba13 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20 30 20 7d 3b  NULL, 0, 0, 0 };
1ba14 0a 0a 20 20 20 20 69 66 28 20 70 56 6d 20 26 26  ..    if( pVm &&
1ba15 20 41 4c 57 41 59 53 28 70 56 6d 2d 3e 64 62 29   ALWAYS(pVm->db)
1ba16 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1ba17 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 56  3_mutex_enter(pV
1ba18 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  m->db->mutex);. 
1ba19 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1ba1a 72 28 70 56 6d 2d 3e 64 62 2c 20 53 51 4c 49 54  r(pVm->db, SQLIT
1ba1b 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20  E_RANGE, 0);.   
1ba1c 20 7d 0a 20 20 20 20 70 4f 75 74 20 3d 20 28 4d   }.    pOut = (M
1ba1d 65 6d 2a 29 26 6e 75 6c 6c 4d 65 6d 3b 0a 20 20  em*)&nullMem;.  
1ba1e 7d 0a 20 20 72 65 74 75 72 6e 20 70 4f 75 74 3b  }.  return pOut;
1ba1f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
1ba20 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
1ba21 64 20 61 66 74 65 72 20 69 6e 76 6f 6b 69 6e 67  d after invoking
1ba22 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   an sqlite3_valu
1ba23 65 5f 58 58 58 20 66 75 6e 63 74 69 6f 6e 20 6f  e_XXX function o
1ba24 6e 20 61 20 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 76  n a .** column v
1ba25 61 6c 75 65 20 28 69 2e 65 2e 20 61 20 76 61 6c  alue (i.e. a val
1ba26 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 65  ue returned by e
1ba27 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 53 51 4c  valuating an SQL
1ba28 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74   expression in t
1ba29 68 65 0a 2a 2a 20 73 65 6c 65 63 74 20 6c 69 73  he.** select lis
1ba2a 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74  t of a SELECT st
1ba2b 61 74 65 6d 65 6e 74 29 20 74 68 61 74 20 6d 61  atement) that ma
1ba2c 79 20 63 61 75 73 65 20 61 20 6d 61 6c 6c 6f 63  y cause a malloc
1ba2d 28 29 20 66 61 69 6c 75 72 65 2e 20 49 66 20 0a  () failure. If .
1ba2e 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20  ** malloc() has 
1ba2f 66 61 69 6c 65 64 2c 20 74 68 65 20 74 68 72 65  failed, the thre
1ba30 61 64 73 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  ads mallocFailed
1ba31 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 65 64   flag is cleared
1ba32 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 0a   and the result.
1ba33 2a 2a 20 63 6f 64 65 20 6f 66 20 73 74 61 74 65  ** code of state
1ba34 6d 65 6e 74 20 70 53 74 6d 74 20 73 65 74 20 74  ment pStmt set t
1ba35 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a  o SQLITE_NOMEM..
1ba36 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 69 63 61 6c  **.** Specifical
1ba37 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61 6c 6c  ly, this is call
1ba38 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 3a 0a  ed from within:.
1ba39 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  **.**     sqlite
1ba3a 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 29 0a 2a  3_column_int().*
1ba3b 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f  *     sqlite3_co
1ba3c 6c 75 6d 6e 5f 69 6e 74 36 34 28 29 0a 2a 2a 20  lumn_int64().** 
1ba3d 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75      sqlite3_colu
1ba3e 6d 6e 5f 74 65 78 74 28 29 0a 2a 2a 20 20 20 20  mn_text().**    
1ba3f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1ba40 74 65 78 74 31 36 28 29 0a 2a 2a 20 20 20 20 20  text16().**     
1ba41 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 72  sqlite3_column_r
1ba42 65 61 6c 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c  eal().**     sql
1ba43 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
1ba44 73 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  s().**     sqlit
1ba45 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31  e3_column_bytes1
1ba46 36 28 29 0a 2a 2a 0a 2a 2a 20 42 75 74 20 6e 6f  6().**.** But no
1ba47 74 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f  t for sqlite3_co
1ba48 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 77 68 69  lumn_blob(), whi
1ba49 63 68 20 6e 65 76 65 72 20 63 61 6c 6c 73 20 6d  ch never calls m
1ba4a 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74  alloc()..*/.stat
1ba4b 69 63 20 76 6f 69 64 20 63 6f 6c 75 6d 6e 4d 61  ic void columnMa
1ba4c 6c 6c 6f 63 46 61 69 6c 75 72 65 28 73 71 6c 69  llocFailure(sqli
1ba4d 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
1ba4e 0a 7b 0a 20 20 2f 2a 20 49 66 20 6d 61 6c 6c 6f  .{.  /* If mallo
1ba4f 63 28 29 20 66 61 69 6c 65 64 20 64 75 72 69 6e  c() failed durin
1ba50 67 20 61 6e 20 65 6e 63 6f 64 69 6e 67 20 63 6f  g an encoding co
1ba51 6e 76 65 72 73 69 6f 6e 20 77 69 74 68 69 6e 20  nversion within 
1ba52 61 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  an.  ** sqlite3_
1ba53 63 6f 6c 75 6d 6e 5f 58 58 58 20 41 50 49 2c 20  column_XXX API, 
1ba54 74 68 65 6e 20 73 65 74 20 74 68 65 20 72 65 74  then set the ret
1ba55 75 72 6e 20 63 6f 64 65 20 6f 66 20 74 68 65 20  urn code of the 
1ba56 73 74 61 74 65 6d 65 6e 74 20 74 6f 0a 20 20 2a  statement to.  *
1ba57 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20  * SQLITE_NOMEM. 
1ba58 54 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f  The next call to
1ba59 20 5f 73 74 65 70 28 29 20 28 69 66 20 61 6e 79   _step() (if any
1ba5a 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51  ) will return SQ
1ba5b 4c 49 54 45 5f 45 52 52 4f 52 0a 20 20 2a 2a 20  LITE_ERROR.  ** 
1ba5c 61 6e 64 20 5f 66 69 6e 61 6c 69 7a 65 28 29 20  and _finalize() 
1ba5d 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e 4f 4d 45  will return NOME
1ba5e 4d 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 20 2a  M..  */.  Vdbe *
1ba5f 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d  p = (Vdbe *)pStm
1ba60 74 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  t;.  if( p ){.  
1ba61 20 20 70 2d 3e 72 63 20 3d 20 73 71 6c 69 74 65    p->rc = sqlite
1ba62 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20  3ApiExit(p->db, 
1ba63 70 2d 3e 72 63 29 3b 0a 20 20 20 20 73 71 6c 69  p->rc);.    sqli
1ba64 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1ba65 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  p->db->mutex);. 
1ba66 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a   }.}../*********
1ba67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ba68 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  *** sqlite3_colu
1ba69 6d 6e 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  mn_  ***********
1ba6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ba6b 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ****.** The foll
1ba6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61  owing routines a
1ba6d 72 65 20 75 73 65 64 20 74 6f 20 61 63 63 65 73  re used to acces
1ba6e 73 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68  s elements of th
1ba6f 65 20 63 75 72 72 65 6e 74 20 72 6f 77 0a 2a 2a  e current row.**
1ba70 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
1ba71 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  et..*/.SQLITE_AP
1ba72 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
1ba73 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
1ba74 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  b(sqlite3_stmt *
1ba75 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
1ba76 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c   const void *val
1ba77 3b 0a 20 20 76 61 6c 20 3d 20 73 71 6c 69 74 65  ;.  val = sqlite
1ba78 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 20 63 6f  3_value_blob( co
1ba79 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29  lumnMem(pStmt,i)
1ba7a 20 29 3b 0a 20 20 2f 2a 20 45 76 65 6e 20 74 68   );.  /* Even th
1ba7b 6f 75 67 68 20 74 68 65 72 65 20 69 73 20 6e 6f  ough there is no
1ba7c 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72   encoding conver
1ba7d 73 69 6f 6e 2c 20 76 61 6c 75 65 5f 62 6c 6f 62  sion, value_blob
1ba7e 28 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 6e 65  () might.  ** ne
1ba7f 65 64 20 74 6f 20 63 61 6c 6c 20 6d 61 6c 6c 6f  ed to call mallo
1ba80 63 28 29 20 74 6f 20 65 78 70 61 6e 64 20 74 68  c() to expand th
1ba81 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 7a 65  e result of a ze
1ba82 72 6f 62 6c 6f 62 28 29 20 0a 20 20 2a 2a 20 65  roblob() .  ** e
1ba83 78 70 72 65 73 73 69 6f 6e 2e 20 0a 20 20 2a 2f  xpression. .  */
1ba84 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
1ba85 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20  ailure(pStmt);. 
1ba86 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53   return val;.}.S
1ba87 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1ba88 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
1ba89 65 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  es(sqlite3_stmt 
1ba8a 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a  *pStmt, int i){.
1ba8b 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69    int val = sqli
1ba8c 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
1ba8d 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74   columnMem(pStmt
1ba8e 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d  ,i) );.  columnM
1ba8f 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74  allocFailure(pSt
1ba90 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61  mt);.  return va
1ba91 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  l;.}.SQLITE_API 
1ba92 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  int sqlite3_colu
1ba93 6d 6e 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74  mn_bytes16(sqlit
1ba94 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1ba95 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61  int i){.  int va
1ba96 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
1ba97 65 5f 62 79 74 65 73 31 36 28 20 63 6f 6c 75 6d  e_bytes16( colum
1ba98 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b  nMem(pStmt,i) );
1ba99 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
1ba9a 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20  ailure(pStmt);. 
1ba9b 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53   return val;.}.S
1ba9c 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65  QLITE_API double
1ba9d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1ba9e 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73  double(sqlite3_s
1ba9f 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1baa0 69 29 7b 0a 20 20 64 6f 75 62 6c 65 20 76 61 6c  i){.  double val
1baa1 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
1baa2 5f 64 6f 75 62 6c 65 28 20 63 6f 6c 75 6d 6e 4d  _double( columnM
1baa3 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20  em(pStmt,i) );. 
1baa4 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69   columnMallocFai
1baa5 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72  lure(pStmt);.  r
1baa6 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c  eturn val;.}.SQL
1baa7 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1baa8 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73  te3_column_int(s
1baa9 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1baaa 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e  mt, int i){.  in
1baab 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f  t val = sqlite3_
1baac 76 61 6c 75 65 5f 69 6e 74 28 20 63 6f 6c 75 6d  value_int( colum
1baad 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b  nMem(pStmt,i) );
1baae 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
1baaf 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20  ailure(pStmt);. 
1bab0 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53   return val;.}.S
1bab1 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
1bab2 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 63  _int64 sqlite3_c
1bab3 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c 69  olumn_int64(sqli
1bab4 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1bab5 20 69 6e 74 20 69 29 7b 0a 20 20 73 71 6c 69 74   int i){.  sqlit
1bab6 65 5f 69 6e 74 36 34 20 76 61 6c 20 3d 20 73 71  e_int64 val = sq
1bab7 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36  lite3_value_int6
1bab8 34 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74  4( columnMem(pSt
1bab9 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d  mt,i) );.  colum
1baba 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70  nMallocFailure(p
1babb 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  Stmt);.  return 
1babc 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  val;.}.SQLITE_AP
1babd 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  I const unsigned
1babe 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
1babf 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74  olumn_text(sqlit
1bac0 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1bac1 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20  int i){.  const 
1bac2 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 76  unsigned char *v
1bac3 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  al = sqlite3_val
1bac4 75 65 5f 74 65 78 74 28 20 63 6f 6c 75 6d 6e 4d  ue_text( columnM
1bac5 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20  em(pStmt,i) );. 
1bac6 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69   columnMallocFai
1bac7 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72  lure(pStmt);.  r
1bac8 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c  eturn val;.}.SQL
1bac9 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
1baca 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 5f 63  value *sqlite3_c
1bacb 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69  olumn_value(sqli
1bacc 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1bacd 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d 20 2a   int i){.  Mem *
1bace 70 4f 75 74 20 3d 20 63 6f 6c 75 6d 6e 4d 65 6d  pOut = columnMem
1bacf 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 69 66  (pStmt, i);.  if
1bad0 28 20 70 4f 75 74 2d 3e 66 6c 61 67 73 26 4d 45  ( pOut->flags&ME
1bad1 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20  M_Static ){.    
1bad2 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  pOut->flags &= ~
1bad3 4d 45 4d 5f 53 74 61 74 69 63 3b 0a 20 20 20 20  MEM_Static;.    
1bad4 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  pOut->flags |= M
1bad5 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 0a 20 20  EM_Ephem;.  }.  
1bad6 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c  columnMallocFail
1bad7 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65  ure(pStmt);.  re
1bad8 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f 76 61  turn (sqlite3_va
1bad9 6c 75 65 20 2a 29 70 4f 75 74 3b 0a 7d 0a 23 69  lue *)pOut;.}.#i
1bada 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1badb 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41  T_UTF16.SQLITE_A
1badc 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
1badd 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
1bade 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  xt16(sqlite3_stm
1badf 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
1bae0 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  {.  const void *
1bae1 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  val = sqlite3_va
1bae2 6c 75 65 5f 74 65 78 74 31 36 28 20 63 6f 6c 75  lue_text16( colu
1bae3 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29  mnMem(pStmt,i) )
1bae4 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63  ;.  columnMalloc
1bae5 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a  Failure(pStmt);.
1bae6 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a    return val;.}.
1bae7 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1bae8 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53  _OMIT_UTF16 */.S
1bae9 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1baea 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
1baeb 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
1baec 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
1baed 20 69 6e 74 20 69 54 79 70 65 20 3d 20 73 71 6c   int iType = sql
1baee 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
1baef 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74   columnMem(pStmt
1baf0 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d  ,i) );.  columnM
1baf1 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74  allocFailure(pSt
1baf2 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 54  mt);.  return iT
1baf3 79 70 65 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66  ype;.}../* The f
1baf4 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f  ollowing functio
1baf5 6e 20 69 73 20 65 78 70 65 72 69 6d 65 6e 74 61  n is experimenta
1baf6 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20 74 6f  l and subject to
1baf7 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 65   change or.** re
1baf8 6d 6f 76 61 6c 20 2a 2f 0a 2f 2a 69 6e 74 20 73  moval */./*int s
1baf9 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 75  qlite3_column_nu
1bafa 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74  meric_type(sqlit
1bafb 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1bafc 69 6e 74 20 69 29 7b 0a 2a 2a 20 20 72 65 74 75  int i){.**  retu
1bafd 72 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  rn sqlite3_value
1bafe 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 20 63  _numeric_type( c
1baff 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69  olumnMem(pStmt,i
1bb00 29 20 29 3b 0a 2a 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a  ) );.**}.*/../*.
1bb01 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 4e  ** Convert the N
1bb02 2d 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20 70  -th element of p
1bb03 53 74 6d 74 2d 3e 70 43 6f 6c 4e 61 6d 65 5b 5d  Stmt->pColName[]
1bb04 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 75   into a string u
1bb05 73 69 6e 67 0a 2a 2a 20 78 46 75 6e 63 28 29 20  sing.** xFunc() 
1bb06 74 68 65 6e 20 72 65 74 75 72 6e 20 74 68 61 74  then return that
1bb07 20 73 74 72 69 6e 67 2e 20 20 49 66 20 4e 20 69   string.  If N i
1bb08 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20  s out of range, 
1bb09 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20  return 0..**.** 
1bb0a 54 68 65 72 65 20 61 72 65 20 75 70 20 74 6f 20  There are up to 
1bb0b 35 20 6e 61 6d 65 73 20 66 6f 72 20 65 61 63 68  5 names for each
1bb0c 20 63 6f 6c 75 6d 6e 2e 20 20 75 73 65 54 79 70   column.  useTyp
1bb0d 65 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69  e determines whi
1bb0e 63 68 0a 2a 2a 20 6e 61 6d 65 20 69 73 20 72 65  ch.** name is re
1bb0f 74 75 72 6e 65 64 2e 20 20 48 65 72 65 20 61 72  turned.  Here ar
1bb10 65 20 74 68 65 20 6e 61 6d 65 73 3a 0a 2a 2a 0a  e the names:.**.
1bb11 2a 2a 20 20 20 20 30 20 20 20 20 20 20 54 68 65  **    0      The
1bb12 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61 73 20   column name as 
1bb13 69 74 20 73 68 6f 75 6c 64 20 62 65 20 64 69 73  it should be dis
1bb14 70 6c 61 79 65 64 20 66 6f 72 20 6f 75 74 70 75  played for outpu
1bb15 74 0a 2a 2a 20 20 20 20 31 20 20 20 20 20 20 54  t.**    1      T
1bb16 68 65 20 64 61 74 61 74 79 70 65 20 6e 61 6d 65  he datatype name
1bb17 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 0a   for the column.
1bb18 2a 2a 20 20 20 20 32 20 20 20 20 20 20 54 68 65  **    2      The
1bb19 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   name of the dat
1bb1a 61 62 61 73 65 20 74 68 61 74 20 74 68 65 20 63  abase that the c
1bb1b 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72  olumn derives fr
1bb1c 6f 6d 0a 2a 2a 20 20 20 20 33 20 20 20 20 20 20  om.**    3      
1bb1d 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
1bb1e 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 63  table that the c
1bb1f 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72  olumn derives fr
1bb20 6f 6d 0a 2a 2a 20 20 20 20 34 20 20 20 20 20 20  om.**    4      
1bb21 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
1bb22 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61  table column tha
1bb23 74 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c  t the result col
1bb24 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d  umn derives from
1bb25 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65  .**.** If the re
1bb26 73 75 6c 74 20 69 73 20 6e 6f 74 20 61 20 73 69  sult is not a si
1bb27 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65  mple column refe
1bb28 72 65 6e 63 65 20 28 69 66 20 69 74 20 69 73 20  rence (if it is 
1bb29 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  an expression.**
1bb2a 20 6f 72 20 61 20 63 6f 6e 73 74 61 6e 74 29 20   or a constant) 
1bb2b 74 68 65 6e 20 75 73 65 54 79 70 65 73 20 32 2c  then useTypes 2,
1bb2c 20 33 2c 20 61 6e 64 20 34 20 72 65 74 75 72 6e   3, and 4 return
1bb2d 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63   NULL..*/.static
1bb2e 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 63 6f 6c   const void *col
1bb2f 75 6d 6e 4e 61 6d 65 28 0a 20 20 73 71 6c 69 74  umnName(.  sqlit
1bb30 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 0a  e3_stmt *pStmt,.
1bb31 20 20 69 6e 74 20 4e 2c 0a 20 20 63 6f 6e 73 74    int N,.  const
1bb32 20 76 6f 69 64 20 2a 28 2a 78 46 75 6e 63 29 28   void *(*xFunc)(
1bb33 4d 65 6d 2a 29 2c 0a 20 20 69 6e 74 20 75 73 65  Mem*),.  int use
1bb34 54 79 70 65 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  Type.){.  const 
1bb35 76 6f 69 64 20 2a 72 65 74 20 3d 20 30 3b 0a 20  void *ret = 0;. 
1bb36 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65   Vdbe *p = (Vdbe
1bb37 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20   *)pStmt;.  int 
1bb38 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  n;.  sqlite3 *db
1bb39 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 0a 20 20 61   = p->db;.  .  a
1bb3a 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a  ssert( db!=0 );.
1bb3b 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f    n = sqlite3_co
1bb3c 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d 74  lumn_count(pStmt
1bb3d 29 3b 0a 20 20 69 66 28 20 4e 3c 6e 20 26 26 20  );.  if( N<n && 
1bb3e 4e 3e 3d 30 20 29 7b 0a 20 20 20 20 4e 20 2b 3d  N>=0 ){.    N +=
1bb3f 20 75 73 65 54 79 70 65 2a 6e 3b 0a 20 20 20 20   useType*n;.    
1bb40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1bb41 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
1bb42 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
1bb43 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20  mallocFailed==0 
1bb44 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 78 46 75  );.    ret = xFu
1bb45 6e 63 28 26 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b  nc(&p->aColName[
1bb46 4e 5d 29 3b 0a 20 20 20 20 20 2f 2a 20 41 20 6d  N]);.     /* A m
1bb47 61 6c 6c 6f 63 20 6d 61 79 20 68 61 76 65 20 66  alloc may have f
1bb48 61 69 6c 65 64 20 69 6e 73 69 64 65 20 6f 66 20  ailed inside of 
1bb49 74 68 65 20 78 46 75 6e 63 28 29 20 63 61 6c 6c  the xFunc() call
1bb4a 2e 20 49 66 20 74 68 69 73 0a 20 20 20 20 2a 2a  . If this.    **
1bb4b 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 63 6c   is the case, cl
1bb4c 65 61 72 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61  ear the mallocFa
1bb4d 69 6c 65 64 20 66 6c 61 67 20 61 6e 64 20 72 65  iled flag and re
1bb4e 74 75 72 6e 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a  turn NULL..    *
1bb4f 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61  /.    if( db->ma
1bb50 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
1bb51 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
1bb52 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20  iled = 0;.      
1bb53 72 65 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  ret = 0;.    }. 
1bb54 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
1bb55 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
1bb56 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1bb57 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ret;.}../*.** Re
1bb58 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66  turn the name of
1bb59 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20   the Nth column 
1bb5a 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  of the result se
1bb5b 74 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 51  t returned by SQ
1bb5c 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 70  L.** statement p
1bb5d 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  Stmt..*/.SQLITE_
1bb5e 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
1bb5f 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
1bb60 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ame(sqlite3_stmt
1bb61 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b   *pStmt, int N){
1bb62 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e  .  return column
1bb63 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d  Name(.      pStm
1bb64 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69  t, N, (const voi
1bb65 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69  d*(*)(Mem*))sqli
1bb66 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20  te3_value_text, 
1bb67 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d  COLNAME_NAME);.}
1bb68 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1bb69 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54  OMIT_UTF16.SQLIT
1bb6a 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
1bb6b 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
1bb6c 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f  _name16(sqlite3_
1bb6d 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1bb6e 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f   N){.  return co
1bb6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20  lumnName(.      
1bb70 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74  pStmt, N, (const
1bb71 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29   void*(*)(Mem*))
1bb72 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1bb73 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  xt16, COLNAME_NA
1bb74 4d 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  ME);.}.#endif../
1bb75 2a 0a 2a 2a 20 43 6f 6e 73 74 72 61 69 6e 74 3a  *.** Constraint:
1bb76 20 20 49 66 20 79 6f 75 20 68 61 76 65 20 45 4e    If you have EN
1bb77 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41  ABLE_COLUMN_META
1bb78 44 41 54 41 20 74 68 65 6e 20 79 6f 75 20 6d 75  DATA then you mu
1bb79 73 74 0a 2a 2a 20 6e 6f 74 20 64 65 66 69 6e 65  st.** not define
1bb7a 20 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 2e 0a   OMIT_DECLTYPE..
1bb7b 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
1bb7c 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54  QLITE_OMIT_DECLT
1bb7d 59 50 45 29 20 26 26 20 64 65 66 69 6e 65 64 28  YPE) && defined(
1bb7e 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f  SQLITE_ENABLE_CO
1bb7f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 29 0a 23  LUMN_METADATA).#
1bb80 20 65 72 72 6f 72 20 22 4d 75 73 74 20 6e 6f 74   error "Must not
1bb81 20 64 65 66 69 6e 65 20 62 6f 74 68 20 53 51 4c   define both SQL
1bb82 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50  ITE_OMIT_DECLTYP
1bb83 45 20 5c 0a 20 20 20 20 20 20 20 20 20 61 6e 64  E \.         and
1bb84 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43   SQLITE_ENABLE_C
1bb85 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 22 0a  OLUMN_METADATA".
1bb86 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
1bb87 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c  SQLITE_OMIT_DECL
1bb88 54 59 50 45 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  TYPE./*.** Retur
1bb89 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 63  n the column dec
1bb8a 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 28 69  laration type (i
1bb8b 66 20 61 70 70 6c 69 63 61 62 6c 65 29 20 6f 66  f applicable) of
1bb8c 20 74 68 65 20 27 69 27 74 68 20 63 6f 6c 75 6d   the 'i'th colum
1bb8d 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 72 65 73 75  n.** of the resu
1bb8e 6c 74 20 73 65 74 20 6f 66 20 53 51 4c 20 73 74  lt set of SQL st
1bb8f 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a  atement pStmt..*
1bb90 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
1bb91 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
1bb92 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65  _column_decltype
1bb93 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1bb94 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  Stmt, int N){.  
1bb95 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d  return columnNam
1bb96 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20  e(.      pStmt, 
1bb97 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28  N, (const void*(
1bb98 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33  *)(Mem*))sqlite3
1bb99 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c  _value_text, COL
1bb9a 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a  NAME_DECLTYPE);.
1bb9b 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
1bb9c 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49  _OMIT_UTF16.SQLI
1bb9d 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
1bb9e 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  d *sqlite3_colum
1bb9f 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 73 71 6c  n_decltype16(sql
1bba0 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1bba1 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75  , int N){.  retu
1bba2 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20  rn columnName(. 
1bba3 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28       pStmt, N, (
1bba4 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d  const void*(*)(M
1bba5 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c  em*))sqlite3_val
1bba6 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41  ue_text16, COLNA
1bba7 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a 7d 0a  ME_DECLTYPE);.}.
1bba8 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1bba9 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 23  _OMIT_UTF16 */.#
1bbaa 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1bbab 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 2a 2f  OMIT_DECLTYPE */
1bbac 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
1bbad 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45  ENABLE_COLUMN_ME
1bbae 54 41 44 41 54 41 0a 2f 2a 0a 2a 2a 20 52 65 74  TADATA./*.** Ret
1bbaf 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  urn the name of 
1bbb0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 72 6f  the database fro
1bbb1 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 6c 74  m which a result
1bbb2 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e   column derives.
1bbb3 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  .** NULL is retu
1bbb4 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73 75  rned if the resu
1bbb5 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20  lt column is an 
1bbb6 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f  expression or co
1bbb7 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79  nstant or.** any
1bbb8 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68  thing else which
1bbb9 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69   is not an unabi
1bbba 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20  guous reference 
1bbbb 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63 6f  to a database co
1bbbc 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lumn..*/.SQLITE_
1bbbd 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
1bbbe 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
1bbbf 61 74 61 62 61 73 65 5f 6e 61 6d 65 28 73 71 6c  atabase_name(sql
1bbc0 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1bbc1 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75  , int N){.  retu
1bbc2 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20  rn columnName(. 
1bbc3 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28       pStmt, N, (
1bbc4 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d  const void*(*)(M
1bbc5 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c  em*))sqlite3_val
1bbc6 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45  ue_text, COLNAME
1bbc7 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d 0a 23 69  _DATABASE);.}.#i
1bbc8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1bbc9 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41  T_UTF16.SQLITE_A
1bbca 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
1bbcb 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61  qlite3_column_da
1bbcc 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 73 71  tabase_name16(sq
1bbcd 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1bbce 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74  t, int N){.  ret
1bbcf 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a  urn columnName(.
1bbd0 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20        pStmt, N, 
1bbd1 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28  (const void*(*)(
1bbd2 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61  Mem*))sqlite3_va
1bbd3 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e  lue_text16, COLN
1bbd4 41 4d 45 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d  AME_DATABASE);.}
1bbd5 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1bbd6 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a  E_OMIT_UTF16 */.
1bbd7 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
1bbd8 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  e name of the ta
1bbd9 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61  ble from which a
1bbda 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64   result column d
1bbdb 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20  erives..** NULL 
1bbdc 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74  is returned if t
1bbdd 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  he result column
1bbde 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f   is an expressio
1bbdf 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72  n or constant or
1bbe0 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73  .** anything els
1bbe1 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61  e which is not a
1bbe2 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 65 66  n unabiguous ref
1bbe3 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 74 61  erence to a data
1bbe4 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a  base column..*/.
1bbe5 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
1bbe6 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
1bbe7 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
1bbe8 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1bbe9 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  Stmt, int N){.  
1bbea 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d  return columnNam
1bbeb 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20  e(.      pStmt, 
1bbec 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28  N, (const void*(
1bbed 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33  *)(Mem*))sqlite3
1bbee 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c  _value_text, COL
1bbef 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23  NAME_TABLE);.}.#
1bbf0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1bbf1 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f  IT_UTF16.SQLITE_
1bbf2 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
1bbf3 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
1bbf4 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69  able_name16(sqli
1bbf5 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1bbf6 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72   int N){.  retur
1bbf7 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20  n columnName(.  
1bbf8 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63      pStmt, N, (c
1bbf9 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65  onst void*(*)(Me
1bbfa 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  m*))sqlite3_valu
1bbfb 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d  e_text16, COLNAM
1bbfc 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 65 6e 64  E_TABLE);.}.#end
1bbfd 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1bbfe 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a  T_UTF16 */../*.*
1bbff 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d  * Return the nam
1bc00 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63  e of the table c
1bc01 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68 69 63 68  olumn from which
1bc02 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e   a result column
1bc03 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c   derives..** NUL
1bc04 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  L is returned if
1bc05 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75   the result colu
1bc06 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73 73  mn is an express
1bc07 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20  ion or constant 
1bc08 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65  or.** anything e
1bc09 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74  lse which is not
1bc0a 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72   an unabiguous r
1bc0b 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61  eference to a da
1bc0c 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a  tabase column..*
1bc0d 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
1bc0e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
1bc0f 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  _column_origin_n
1bc10 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ame(sqlite3_stmt
1bc11 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b   *pStmt, int N){
1bc12 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e  .  return column
1bc13 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d  Name(.      pStm
1bc14 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69  t, N, (const voi
1bc15 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69  d*(*)(Mem*))sqli
1bc16 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20  te3_value_text, 
1bc17 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b  COLNAME_COLUMN);
1bc18 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  .}.#ifndef SQLIT
1bc19 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c  E_OMIT_UTF16.SQL
1bc1a 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
1bc1b 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
1bc1c 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36  mn_origin_name16
1bc1d 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1bc1e 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  Stmt, int N){.  
1bc1f 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d  return columnNam
1bc20 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20  e(.      pStmt, 
1bc21 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28  N, (const void*(
1bc22 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33  *)(Mem*))sqlite3
1bc23 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43  _value_text16, C
1bc24 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b 0a  OLNAME_COLUMN);.
1bc25 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1bc26 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
1bc27 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1bc28 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f  E_ENABLE_COLUMN_
1bc29 4d 45 54 41 44 41 54 41 20 2a 2f 0a 0a 0a 2f 2a  METADATA */.../*
1bc2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bc2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73  ************** s
1bc2c 71 6c 69 74 65 33 5f 62 69 6e 64 5f 20 20 2a 2a  qlite3_bind_  **
1bc2d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bc2e 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a 2a  *********.** .**
1bc2f 20 52 6f 75 74 69 6e 65 73 20 75 73 65 64 20 74   Routines used t
1bc30 6f 20 61 74 74 61 63 68 20 76 61 6c 75 65 73 20  o attach values 
1bc31 74 6f 20 77 69 6c 64 63 61 72 64 73 20 69 6e 20  to wildcards in 
1bc32 61 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73  a compiled SQL s
1bc33 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 2f 2a 0a  tatement..*/./*.
1bc34 2a 2a 20 55 6e 62 69 6e 64 20 74 68 65 20 76 61  ** Unbind the va
1bc35 6c 75 65 20 62 6f 75 6e 64 20 74 6f 20 76 61 72  lue bound to var
1bc36 69 61 62 6c 65 20 69 20 69 6e 20 76 69 72 74 75  iable i in virtu
1bc37 61 6c 20 6d 61 63 68 69 6e 65 20 70 2e 20 54 68  al machine p. Th
1bc38 69 73 20 69 73 20 74 68 65 20 0a 2a 2a 20 74 68  is is the .** th
1bc39 65 20 73 61 6d 65 20 61 73 20 62 69 6e 64 69 6e  e same as bindin
1bc3a 67 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20 74  g a NULL value t
1bc3b 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 49 66  o the column. If
1bc3c 20 74 68 65 20 22 69 22 20 70 61 72 61 6d 65 74   the "i" paramet
1bc3d 65 72 20 69 73 0a 2a 2a 20 6f 75 74 20 6f 66 20  er is.** out of 
1bc3e 72 61 6e 67 65 2c 20 74 68 65 6e 20 53 51 4c 49  range, then SQLI
1bc3f 54 45 5f 52 41 4e 47 45 20 69 73 20 72 65 74 75  TE_RANGE is retu
1bc40 72 6e 65 64 2e 20 4f 74 68 65 77 69 73 65 20 53  rned. Othewise S
1bc41 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20  QLITE_OK..**.** 
1bc42 41 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 61  A successful eva
1bc43 6c 75 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20  luation of this 
1bc44 72 6f 75 74 69 6e 65 20 61 63 71 75 69 72 65 73  routine acquires
1bc45 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 70 2e   the mutex on p.
1bc46 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20 69 73  .** the mutex is
1bc47 20 72 65 6c 65 61 73 65 64 20 69 66 20 61 6e 79   released if any
1bc48 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 20 6f   kind of error o
1bc49 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ccurs..**.** The
1bc4a 20 65 72 72 6f 72 20 63 6f 64 65 20 73 74 6f 72   error code stor
1bc4b 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 20 70  ed in database p
1bc4c 2d 3e 64 62 20 69 73 20 6f 76 65 72 77 72 69 74  ->db is overwrit
1bc4d 74 65 6e 20 77 69 74 68 20 74 68 65 20 72 65 74  ten with the ret
1bc4e 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20  urn.** value in 
1bc4f 61 6e 79 20 63 61 73 65 2e 0a 2a 2f 0a 73 74 61  any case..*/.sta
1bc50 74 69 63 20 69 6e 74 20 76 64 62 65 55 6e 62 69  tic int vdbeUnbi
1bc51 6e 64 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20  nd(Vdbe *p, int 
1bc52 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b  i){.  Mem *pVar;
1bc53 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
1bc54 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
1bc55 53 45 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  SE;.  sqlite3_mu
1bc56 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62 2d  tex_enter(p->db-
1bc57 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70  >mutex);.  if( p
1bc58 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41  ->magic!=VDBE_MA
1bc59 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 70 63  GIC_RUN || p->pc
1bc5a 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  >=0 ){.    sqlit
1bc5b 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53  e3Error(p->db, S
1bc5c 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 30 29  QLITE_MISUSE, 0)
1bc5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
1bc5e 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d  tex_leave(p->db-
1bc5f 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74  >mutex);.    ret
1bc60 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
1bc61 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 3c 31  E;.  }.  if( i<1
1bc62 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29 7b   || i>p->nVar ){
1bc63 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
1bc64 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f  r(p->db, SQLITE_
1bc65 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20 20 73  RANGE, 0);.    s
1bc66 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1bc67 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ve(p->db->mutex)
1bc68 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
1bc69 49 54 45 5f 52 41 4e 47 45 3b 0a 20 20 7d 0a 20  ITE_RANGE;.  }. 
1bc6a 20 69 2d 2d 3b 0a 20 20 70 56 61 72 20 3d 20 26   i--;.  pVar = &
1bc6b 70 2d 3e 61 56 61 72 5b 69 5d 3b 0a 20 20 73 71  p->aVar[i];.  sq
1bc6c 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
1bc6d 61 73 65 28 70 56 61 72 29 3b 0a 20 20 70 56 61  ase(pVar);.  pVa
1bc6e 72 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  r->flags = MEM_N
1bc6f 75 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 45 72  ull;.  sqlite3Er
1bc70 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54  ror(p->db, SQLIT
1bc71 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 72 65 74 75  E_OK, 0);.  retu
1bc72 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1bc73 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61 20 74 65  ./*.** Bind a te
1bc74 78 74 20 6f 72 20 42 4c 4f 42 20 76 61 6c 75 65  xt or BLOB value
1bc75 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1bc76 62 69 6e 64 54 65 78 74 28 0a 20 20 73 71 6c 69  bindText(.  sqli
1bc77 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1bc78 20 20 20 2f 2a 20 54 68 65 20 73 74 61 74 65 6d     /* The statem
1bc79 65 6e 74 20 74 6f 20 62 69 6e 64 20 61 67 61 69  ent to bind agai
1bc7a 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20  nst */.  int i, 
1bc7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bc7c 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20  /* Index of the 
1bc7d 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62 69 6e  parameter to bin
1bc7e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  d */.  const voi
1bc7f 64 20 2a 7a 44 61 74 61 2c 20 20 20 20 20 2f 2a  d *zData,     /*
1bc80 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   Pointer to the 
1bc81 64 61 74 61 20 74 6f 20 62 65 20 62 6f 75 6e 64  data to be bound
1bc82 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c   */.  int nData,
1bc83 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1bc84 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1bc85 6f 66 20 64 61 74 61 20 74 6f 20 62 65 20 62 6f  of data to be bo
1bc86 75 6e 64 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  und */.  void (*
1bc87 78 44 65 6c 29 28 76 6f 69 64 2a 29 2c 20 20 20  xDel)(void*),   
1bc88 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f  /* Destructor fo
1bc89 72 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20  r the data */.  
1bc8a 75 38 20 65 6e 63 6f 64 69 6e 67 20 20 20 20 20  u8 encoding     
1bc8b 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69         /* Encodi
1bc8c 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 61 20  ng for the data 
1bc8d 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 70 20  */.){.  Vdbe *p 
1bc8e 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b  = (Vdbe *)pStmt;
1bc8f 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20 20  .  Mem *pVar;.  
1bc90 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  int rc;..  rc = 
1bc91 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29  vdbeUnbind(p, i)
1bc92 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
1bc93 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
1bc94 20 7a 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20   zData!=0 ){.   
1bc95 20 20 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 56     pVar = &p->aV
1bc96 61 72 5b 69 2d 31 5d 3b 0a 20 20 20 20 20 20 72  ar[i-1];.      r
1bc97 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
1bc98 65 6d 53 65 74 53 74 72 28 70 56 61 72 2c 20 7a  emSetStr(pVar, z
1bc99 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 65 6e 63  Data, nData, enc
1bc9a 6f 64 69 6e 67 2c 20 78 44 65 6c 29 3b 0a 20 20  oding, xDel);.  
1bc9b 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1bc9c 54 45 5f 4f 4b 20 26 26 20 65 6e 63 6f 64 69 6e  TE_OK && encodin
1bc9d 67 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  g!=0 ){.        
1bc9e 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
1bc9f 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70  ChangeEncoding(p
1bca0 56 61 72 2c 20 45 4e 43 28 70 2d 3e 64 62 29 29  Var, ENC(p->db))
1bca1 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1bca2 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e  sqlite3Error(p->
1bca3 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20 20  db, rc, 0);.    
1bca4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
1bca5 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 72 63 29  iExit(p->db, rc)
1bca6 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
1bca7 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1bca8 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  p->db->mutex);. 
1bca9 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1bcaa 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61  }.../*.** Bind a
1bcab 20 62 6c 6f 62 20 76 61 6c 75 65 20 74 6f 20 61   blob value to a
1bcac 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  n SQL statement 
1bcad 76 61 72 69 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  variable..*/.SQL
1bcae 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1bcaf 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 0a 20  te3_bind_blob(. 
1bcb0 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
1bcb1 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c 20  Stmt, .  int i, 
1bcb2 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
1bcb3 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44 61  Data, .  int nDa
1bcb4 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44  ta, .  void (*xD
1bcb5 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20  el)(void*).){.  
1bcb6 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78 74 28  return bindText(
1bcb7 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61 2c  pStmt, i, zData,
1bcb8 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 30 29   nData, xDel, 0)
1bcb9 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
1bcba 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
1bcbb 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73  double(sqlite3_s
1bcbc 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1bcbd 69 2c 20 64 6f 75 62 6c 65 20 72 56 61 6c 75 65  i, double rValue
1bcbe 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56  ){.  int rc;.  V
1bcbf 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a  dbe *p = (Vdbe *
1bcc0 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76  )pStmt;.  rc = v
1bcc1 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b  dbeUnbind(p, i);
1bcc2 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1bcc3 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
1bcc4 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75  te3VdbeMemSetDou
1bcc5 62 6c 65 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31  ble(&p->aVar[i-1
1bcc6 5d 2c 20 72 56 61 6c 75 65 29 3b 0a 20 20 20 20  ], rValue);.    
1bcc7 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1bcc8 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  ave(p->db->mutex
1bcc9 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1bcca 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  rc;.}.SQLITE_API
1bccb 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
1bccc 64 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74  d_int(sqlite3_st
1bccd 6d 74 20 2a 70 2c 20 69 6e 74 20 69 2c 20 69 6e  mt *p, int i, in
1bcce 74 20 69 56 61 6c 75 65 29 7b 0a 20 20 72 65 74  t iValue){.  ret
1bccf 75 72 6e 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  urn sqlite3_bind
1bcd0 5f 69 6e 74 36 34 28 70 2c 20 69 2c 20 28 69 36  _int64(p, i, (i6
1bcd1 34 29 69 56 61 6c 75 65 29 3b 0a 7d 0a 53 51 4c  4)iValue);.}.SQL
1bcd2 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1bcd3 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73  te3_bind_int64(s
1bcd4 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1bcd5 6d 74 2c 20 69 6e 74 20 69 2c 20 73 71 6c 69 74  mt, int i, sqlit
1bcd6 65 5f 69 6e 74 36 34 20 69 56 61 6c 75 65 29 7b  e_int64 iValue){
1bcd7 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62  .  int rc;.  Vdb
1bcd8 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70  e *p = (Vdbe *)p
1bcd9 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62  Stmt;.  rc = vdb
1bcda 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20  eUnbind(p, i);. 
1bcdb 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1bcdc 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
1bcdd 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34  3VdbeMemSetInt64
1bcde 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c 20  (&p->aVar[i-1], 
1bcdf 69 56 61 6c 75 65 29 3b 0a 20 20 20 20 73 71 6c  iValue);.    sql
1bce0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1bce1 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  (p->db->mutex);.
1bce2 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1bce3 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
1bce4 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e  t sqlite3_bind_n
1bce5 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ull(sqlite3_stmt
1bce6 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b   *pStmt, int i){
1bce7 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62  .  int rc;.  Vdb
1bce8 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53  e *p = (Vdbe*)pS
1bce9 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 65  tmt;.  rc = vdbe
1bcea 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20  Unbind(p, i);.  
1bceb 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1bcec 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  K ){.    sqlite3
1bced 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e  _mutex_leave(p->
1bcee 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a  db->mutex);.  }.
1bcef 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53    return rc;.}.S
1bcf0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1bcf1 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28  lite3_bind_text(
1bcf2 20 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74   .  sqlite3_stmt
1bcf3 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20   *pStmt, .  int 
1bcf4 69 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  i, .  const char
1bcf5 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20   *zData, .  int 
1bcf6 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28  nData, .  void (
1bcf7 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b  *xDel)(void*).){
1bcf8 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65  .  return bindTe
1bcf9 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61  xt(pStmt, i, zDa
1bcfa 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c  ta, nData, xDel,
1bcfb 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d   SQLITE_UTF8);.}
1bcfc 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1bcfd 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54  OMIT_UTF16.SQLIT
1bcfe 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1bcff 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28 0a 20  3_bind_text16(. 
1bd00 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
1bd01 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c 20  Stmt, .  int i, 
1bd02 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
1bd03 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44 61  Data, .  int nDa
1bd04 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44  ta, .  void (*xD
1bd05 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20  el)(void*).){.  
1bd06 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78 74 28  return bindText(
1bd07 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61 2c  pStmt, i, zData,
1bd08 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 53 51   nData, xDel, SQ
1bd09 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
1bd0a 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
1bd0b 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
1bd0c 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
1bd0d 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
1bd0e 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73 74  value(sqlite3_st
1bd0f 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
1bd10 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  , const sqlite3_
1bd11 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29 7b 0a  value *pValue){.
1bd12 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 77 69 74    int rc;.  swit
1bd13 63 68 28 20 70 56 61 6c 75 65 2d 3e 74 79 70 65  ch( pValue->type
1bd14 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   ){.    case SQL
1bd15 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20  ITE_INTEGER: {. 
1bd16 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1bd17 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 53 74  3_bind_int64(pSt
1bd18 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e 75  mt, i, pValue->u
1bd19 2e 69 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  .i);.      break
1bd1a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
1bd1b 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 7b   SQLITE_FLOAT: {
1bd1c 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1bd1d 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28  te3_bind_double(
1bd1e 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65  pStmt, i, pValue
1bd1f 2d 3e 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ->r);.      brea
1bd20 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
1bd21 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 7b  e SQLITE_BLOB: {
1bd22 0a 20 20 20 20 20 20 69 66 28 20 70 56 61 6c 75  .      if( pValu
1bd23 65 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a  e->flags & MEM_Z
1bd24 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ero ){.        r
1bd25 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  c = sqlite3_bind
1bd26 5f 7a 65 72 6f 62 6c 6f 62 28 70 53 74 6d 74 2c  _zeroblob(pStmt,
1bd27 20 69 2c 20 70 56 61 6c 75 65 2d 3e 75 2e 6e 5a   i, pValue->u.nZ
1bd28 65 72 6f 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ero);.      }els
1bd29 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  e{.        rc = 
1bd2a 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
1bd2b 62 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c  b(pStmt, i, pVal
1bd2c 75 65 2d 3e 7a 2c 20 70 56 61 6c 75 65 2d 3e 6e  ue->z, pValue->n
1bd2d 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e  ,SQLITE_TRANSIEN
1bd2e 54 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  T);.      }.    
1bd2f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1bd30 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54     case SQLITE_T
1bd31 45 58 54 3a 20 7b 0a 20 20 20 20 20 20 72 63 20  EXT: {.      rc 
1bd32 3d 20 62 69 6e 64 54 65 78 74 28 70 53 74 6d 74  = bindText(pStmt
1bd33 2c 69 2c 20 20 70 56 61 6c 75 65 2d 3e 7a 2c 20  ,i,  pValue->z, 
1bd34 70 56 61 6c 75 65 2d 3e 6e 2c 20 53 51 4c 49 54  pValue->n, SQLIT
1bd35 45 5f 54 52 41 4e 53 49 45 4e 54 2c 0a 20 20 20  E_TRANSIENT,.   
1bd36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bd37 20 20 20 20 20 20 20 20 20 20 20 70 56 61 6c 75             pValu
1bd38 65 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 62  e->enc);.      b
1bd39 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1bd3a 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
1bd3b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
1bd3c 6e 64 5f 6e 75 6c 6c 28 70 53 74 6d 74 2c 20 69  nd_null(pStmt, i
1bd3d 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
1bd3e 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1bd3f 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f  rn rc;.}.SQLITE_
1bd40 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1bd41 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 71  bind_zeroblob(sq
1bd42 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1bd43 74 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20 6e 29  t, int i, int n)
1bd44 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64  {.  int rc;.  Vd
1bd45 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29  be *p = (Vdbe *)
1bd46 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64  pStmt;.  rc = vd
1bd47 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a  beUnbind(p, i);.
1bd48 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1bd49 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
1bd4a 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f  e3VdbeMemSetZero
1bd4b 42 6c 6f 62 28 26 70 2d 3e 61 56 61 72 5b 69 2d  Blob(&p->aVar[i-
1bd4c 31 5d 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c 69  1], n);.    sqli
1bd4d 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1bd4e 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  p->db->mutex);. 
1bd4f 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1bd50 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1bd51 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 69  the number of wi
1bd52 6c 64 63 61 72 64 73 20 74 68 61 74 20 63 61 6e  ldcards that can
1bd53 20 62 65 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20   be potentially 
1bd54 62 6f 75 6e 64 20 74 6f 2e 0a 2a 2a 20 54 68 69  bound to..** Thi
1bd55 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 64 64  s routine is add
1bd56 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 44 42  ed to support DB
1bd57 44 3a 3a 53 51 4c 69 74 65 2e 20 20 0a 2a 2f 0a  D::SQLite.  .*/.
1bd58 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1bd59 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
1bd5a 6d 65 74 65 72 5f 63 6f 75 6e 74 28 73 71 6c 69  meter_count(sqli
1bd5b 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
1bd5c 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56  {.  Vdbe *p = (V
1bd5d 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65  dbe*)pStmt;.  re
1bd5e 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e 56 61 72  turn p ? p->nVar
1bd5f 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43   : 0;.}../*.** C
1bd60 72 65 61 74 65 20 61 20 6d 61 70 70 69 6e 67 20  reate a mapping 
1bd61 66 72 6f 6d 20 76 61 72 69 61 62 6c 65 20 6e 75  from variable nu
1bd62 6d 62 65 72 73 20 74 6f 20 76 61 72 69 61 62 6c  mbers to variabl
1bd63 65 20 6e 61 6d 65 73 0a 2a 2a 20 69 6e 20 74 68  e names.** in th
1bd64 65 20 56 64 62 65 2e 61 7a 56 61 72 5b 5d 20 61  e Vdbe.azVar[] a
1bd65 72 72 61 79 2c 20 69 66 20 73 75 63 68 20 61 20  rray, if such a 
1bd66 6d 61 70 70 69 6e 67 20 64 6f 65 73 20 6e 6f 74  mapping does not
1bd67 20 61 6c 72 65 61 64 79 0a 2a 2a 20 65 78 69 73   already.** exis
1bd68 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
1bd69 64 20 63 72 65 61 74 65 56 61 72 4d 61 70 28 56  d createVarMap(V
1bd6a 64 62 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 21  dbe *p){.  if( !
1bd6b 70 2d 3e 6f 6b 56 61 72 20 29 7b 0a 20 20 20 20  p->okVar ){.    
1bd6c 69 6e 74 20 6a 3b 0a 20 20 20 20 4f 70 20 2a 70  int j;.    Op *p
1bd6d 4f 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  Op;.    sqlite3_
1bd6e 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64  mutex_enter(p->d
1bd6f 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 2f  b->mutex);.    /
1bd70 2a 20 54 68 65 20 72 61 63 65 20 63 6f 6e 64 69  * The race condi
1bd71 74 69 6f 6e 20 68 65 72 65 20 69 73 20 68 61 72  tion here is har
1bd72 6d 6c 65 73 73 2e 20 20 49 66 20 74 77 6f 20 74  mless.  If two t
1bd73 68 72 65 61 64 73 20 63 61 6c 6c 20 74 68 69 73  hreads call this
1bd74 0a 20 20 20 20 2a 2a 20 72 6f 75 74 69 6e 65 20  .    ** routine 
1bd75 6f 6e 20 74 68 65 20 73 61 6d 65 20 56 64 62 65  on the same Vdbe
1bd76 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
1bd77 65 2c 20 74 68 65 79 20 62 6f 74 68 20 6d 69 67  e, they both mig
1bd78 68 74 20 65 6e 64 0a 20 20 20 20 2a 2a 20 75 70  ht end.    ** up
1bd79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68   initializing th
1bd7a 65 20 56 64 62 65 2e 61 7a 56 61 72 5b 5d 20 61  e Vdbe.azVar[] a
1bd7b 72 72 61 79 2e 20 20 54 68 61 74 20 69 73 20 61  rray.  That is a
1bd7c 20 6c 69 74 74 6c 65 20 65 78 74 72 61 0a 20 20   little extra.  
1bd7d 20 20 2a 2a 20 77 6f 72 6b 20 62 75 74 20 69 74    ** work but it
1bd7e 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20   results in the 
1bd7f 73 61 6d 65 20 61 6e 73 77 65 72 2e 0a 20 20 20  same answer..   
1bd80 20 2a 2f 0a 20 20 20 20 66 6f 72 28 6a 3d 30 2c   */.    for(j=0,
1bd81 20 70 4f 70 3d 70 2d 3e 61 4f 70 3b 20 6a 3c 70   pOp=p->aOp; j<p
1bd82 2d 3e 6e 4f 70 3b 20 6a 2b 2b 2c 20 70 4f 70 2b  ->nOp; j++, pOp+
1bd83 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f  +){.      if( pO
1bd84 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 61  p->opcode==OP_Va
1bd85 72 69 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20  riable ){.      
1bd86 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1bd87 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 3d  1>0 && pOp->p1<=
1bd88 70 2d 3e 6e 56 61 72 20 29 3b 0a 20 20 20 20 20  p->nVar );.     
1bd89 20 20 20 70 2d 3e 61 7a 56 61 72 5b 70 4f 70 2d     p->azVar[pOp-
1bd8a 3e 70 31 2d 31 5d 20 3d 20 70 4f 70 2d 3e 70 34  >p1-1] = pOp->p4
1bd8b 2e 7a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  .z;.      }.    
1bd8c 7d 0a 20 20 20 20 70 2d 3e 6f 6b 56 61 72 20 3d  }.    p->okVar =
1bd8d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   1;.    sqlite3_
1bd8e 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64  mutex_leave(p->d
1bd8f 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 7d  b->mutex);.  }.}
1bd90 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1bd91 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 77 69 6c  he name of a wil
1bd92 64 63 61 72 64 20 70 61 72 61 6d 65 74 65 72 2e  dcard parameter.
1bd93 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66    Return NULL if
1bd94 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a 20 69 73   the index.** is
1bd95 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f 72   out of range or
1bd96 20 69 66 20 74 68 65 20 77 69 6c 64 63 61 72 64   if the wildcard
1bd97 20 69 73 20 75 6e 6e 61 6d 65 64 2e 0a 2a 2a 0a   is unnamed..**.
1bd98 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20 69 73  ** The result is
1bd99 20 61 6c 77 61 79 73 20 55 54 46 2d 38 2e 0a 2a   always UTF-8..*
1bd9a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
1bd9b 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
1bd9c 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
1bd9d 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d  name(sqlite3_stm
1bd9e 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
1bd9f 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56  {.  Vdbe *p = (V
1bda0 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69 66  dbe*)pStmt;.  if
1bda1 28 20 70 3d 3d 30 20 7c 7c 20 69 3c 31 20 7c 7c  ( p==0 || i<1 ||
1bda2 20 69 3e 70 2d 3e 6e 56 61 72 20 29 7b 0a 20 20   i>p->nVar ){.  
1bda3 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
1bda4 20 20 63 72 65 61 74 65 56 61 72 4d 61 70 28 70    createVarMap(p
1bda5 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 61  );.  return p->a
1bda6 7a 56 61 72 5b 69 2d 31 5d 3b 0a 7d 0a 0a 2f 2a  zVar[i-1];.}../*
1bda7 0a 2a 2a 20 47 69 76 65 6e 20 61 20 77 69 6c 64  .** Given a wild
1bda8 63 61 72 64 20 70 61 72 61 6d 65 74 65 72 20 6e  card parameter n
1bda9 61 6d 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20  ame, return the 
1bdaa 69 6e 64 65 78 20 6f 66 20 74 68 65 20 76 61 72  index of the var
1bdab 69 61 62 6c 65 0a 2a 2a 20 77 69 74 68 20 74 68  iable.** with th
1bdac 61 74 20 6e 61 6d 65 2e 20 20 49 66 20 74 68 65  at name.  If the
1bdad 72 65 20 69 73 20 6e 6f 20 76 61 72 69 61 62 6c  re is no variabl
1bdae 65 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e  e with the given
1bdaf 20 6e 61 6d 65 2c 0a 2a 2a 20 72 65 74 75 72 6e   name,.** return
1bdb0 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50   0..*/.SQLITE_AP
1bdb1 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
1bdb2 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64  nd_parameter_ind
1bdb3 65 78 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ex(sqlite3_stmt 
1bdb4 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20 63 68  *pStmt, const ch
1bdb5 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 56 64  ar *zName){.  Vd
1bdb6 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70  be *p = (Vdbe*)p
1bdb7 53 74 6d 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  Stmt;.  int i;. 
1bdb8 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20   if( p==0 ){.   
1bdb9 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
1bdba 20 63 72 65 61 74 65 56 61 72 4d 61 70 28 70 29   createVarMap(p)
1bdbb 3b 20 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 29  ; .  if( zName )
1bdbc 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
1bdbd 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a  <p->nVar; i++){.
1bdbe 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
1bdbf 20 2a 7a 20 3d 20 70 2d 3e 61 7a 56 61 72 5b 69   *z = p->azVar[i
1bdc0 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 20 26  ];.      if( z &
1bdc1 26 20 73 74 72 63 6d 70 28 7a 2c 7a 4e 61 6d 65  & strcmp(z,zName
1bdc2 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
1bdc3 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20  return i+1;.    
1bdc4 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
1bdc5 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
1bdc6 2a 2a 20 54 72 61 6e 73 66 65 72 20 61 6c 6c 20  ** Transfer all 
1bdc7 62 69 6e 64 69 6e 67 73 20 66 72 6f 6d 20 74 68  bindings from th
1bdc8 65 20 66 69 72 73 74 20 73 74 61 74 65 6d 65 6e  e first statemen
1bdc9 74 20 6f 76 65 72 20 74 6f 20 74 68 65 20 73 65  t over to the se
1bdca 63 6f 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  cond..*/.SQLITE_
1bdcb 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1bdcc 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69  te3TransferBindi
1bdcd 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ngs(sqlite3_stmt
1bdce 20 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71 6c   *pFromStmt, sql
1bdcf 69 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53 74  ite3_stmt *pToSt
1bdd0 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 46 72  mt){.  Vdbe *pFr
1bdd1 6f 6d 20 3d 20 28 56 64 62 65 2a 29 70 46 72 6f  om = (Vdbe*)pFro
1bdd2 6d 53 74 6d 74 3b 0a 20 20 56 64 62 65 20 2a 70  mStmt;.  Vdbe *p
1bdd3 54 6f 20 3d 20 28 56 64 62 65 2a 29 70 54 6f 53  To = (Vdbe*)pToS
1bdd4 74 6d 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  tmt;.  int i;.  
1bdd5 61 73 73 65 72 74 28 20 70 54 6f 2d 3e 64 62 3d  assert( pTo->db=
1bdd6 3d 70 46 72 6f 6d 2d 3e 64 62 20 29 3b 0a 20 20  =pFrom->db );.  
1bdd7 61 73 73 65 72 74 28 20 70 54 6f 2d 3e 6e 56 61  assert( pTo->nVa
1bdd8 72 3d 3d 70 46 72 6f 6d 2d 3e 6e 56 61 72 20 29  r==pFrom->nVar )
1bdd9 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
1bdda 78 5f 65 6e 74 65 72 28 70 54 6f 2d 3e 64 62 2d  x_enter(pTo->db-
1bddb 3e 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72 28 69  >mutex);.  for(i
1bddc 3d 30 3b 20 69 3c 70 46 72 6f 6d 2d 3e 6e 56 61  =0; i<pFrom->nVa
1bddd 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  r; i++){.    sql
1bdde 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28  ite3VdbeMemMove(
1bddf 26 70 54 6f 2d 3e 61 56 61 72 5b 69 5d 2c 20 26  &pTo->aVar[i], &
1bde0 70 46 72 6f 6d 2d 3e 61 56 61 72 5b 69 5d 29 3b  pFrom->aVar[i]);
1bde1 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
1bde2 75 74 65 78 5f 6c 65 61 76 65 28 70 54 6f 2d 3e  utex_leave(pTo->
1bde3 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
1bde4 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1bde5 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
1bde6 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
1bde7 44 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74  D./*.** Deprecat
1bde8 65 64 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 65  ed external inte
1bde9 72 66 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c  rface.  Internal
1bdea 2f 63 6f 72 65 20 53 51 4c 69 74 65 20 63 6f 64  /core SQLite cod
1bdeb 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c  e.** should call
1bdec 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 65 72   sqlite3Transfer
1bded 42 69 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20  Bindings..**.** 
1bdee 49 73 20 69 73 20 6d 69 73 75 73 65 20 74 6f 20  Is is misuse to 
1bdef 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e  call this routin
1bdf0 65 20 77 69 74 68 20 73 74 61 74 65 6d 65 6e 74  e with statement
1bdf1 73 20 66 72 6f 6d 20 64 69 66 66 65 72 65 6e 74  s from different
1bdf2 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e  .** database con
1bdf3 6e 65 63 74 69 6f 6e 73 2e 20 20 42 75 74 20 61  nections.  But a
1bdf4 73 20 74 68 69 73 20 69 73 20 61 20 64 65 70 72  s this is a depr
1bdf5 65 63 61 74 65 64 20 69 6e 74 65 72 66 61 63 65  ecated interface
1bdf6 2c 20 77 65 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74  , we.** will not
1bdf7 20 62 6f 74 68 65 72 20 74 6f 20 63 68 65 63 6b   bother to check
1bdf8 20 66 6f 72 20 74 68 61 74 20 63 6f 6e 64 69 74   for that condit
1bdf9 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ion..**.** If th
1bdfa 65 20 74 77 6f 20 73 74 61 74 65 6d 65 6e 74 73  e two statements
1bdfb 20 63 6f 6e 74 61 69 6e 20 61 20 64 69 66 66 65   contain a diffe
1bdfc 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 62  rent number of b
1bdfd 69 6e 64 69 6e 67 73 2c 20 74 68 65 6e 0a 2a 2a  indings, then.**
1bdfe 20 61 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52   an SQLITE_ERROR
1bdff 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4e   is returned.  N
1be00 6f 74 68 69 6e 67 20 65 6c 73 65 20 63 61 6e 20  othing else can 
1be01 67 6f 20 77 72 6f 6e 67 2c 20 73 6f 20 6f 74 68  go wrong, so oth
1be02 65 72 77 69 73 65 0a 2a 2a 20 53 51 4c 49 54 45  erwise.** SQLITE
1be03 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
1be04 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
1be05 6e 74 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73  nt sqlite3_trans
1be06 66 65 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c  fer_bindings(sql
1be07 69 74 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d  ite3_stmt *pFrom
1be08 53 74 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 74  Stmt, sqlite3_st
1be09 6d 74 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20  mt *pToStmt){.  
1be0a 56 64 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56  Vdbe *pFrom = (V
1be0b 64 62 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a  dbe*)pFromStmt;.
1be0c 20 20 56 64 62 65 20 2a 70 54 6f 20 3d 20 28 56    Vdbe *pTo = (V
1be0d 64 62 65 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20  dbe*)pToStmt;.  
1be0e 69 66 28 20 70 46 72 6f 6d 2d 3e 6e 56 61 72 21  if( pFrom->nVar!
1be0f 3d 70 54 6f 2d 3e 6e 56 61 72 20 29 7b 0a 20 20  =pTo->nVar ){.  
1be10 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1be11 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74  ERROR;.  }.  ret
1be12 75 72 6e 20 73 71 6c 69 74 65 33 54 72 61 6e 73  urn sqlite3Trans
1be13 66 65 72 42 69 6e 64 69 6e 67 73 28 70 46 72 6f  ferBindings(pFro
1be14 6d 53 74 6d 74 2c 20 70 54 6f 53 74 6d 74 29 3b  mStmt, pToStmt);
1be15 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
1be16 20 52 65 74 75 72 6e 20 74 68 65 20 73 71 6c 69   Return the sqli
1be17 74 65 33 2a 20 64 61 74 61 62 61 73 65 20 68 61  te3* database ha
1be18 6e 64 6c 65 20 74 6f 20 77 68 69 63 68 20 74 68  ndle to which th
1be19 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
1be1a 6d 65 6e 74 20 67 69 76 65 6e 0a 2a 2a 20 69 6e  ment given.** in
1be1b 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 62 65   the argument be
1be1c 6c 6f 6e 67 73 2e 20 20 54 68 69 73 20 69 73 20  longs.  This is 
1be1d 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
1be1e 65 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77 61  e handle that wa
1be1f 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 61  s.** the first a
1be20 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 73  rgument to the s
1be21 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29  qlite3_prepare()
1be22 20 74 68 61 74 20 77 61 73 20 75 73 65 64 20 74   that was used t
1be23 6f 20 63 72 65 61 74 65 0a 2a 2a 20 74 68 65 20  o create.** the 
1be24 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65  statement in the
1be25 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2f   first place..*/
1be26 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
1be27 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f  te3 *sqlite3_db_
1be28 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 73  handle(sqlite3_s
1be29 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 72  tmt *pStmt){.  r
1be2a 65 74 75 72 6e 20 70 53 74 6d 74 20 3f 20 28 28  eturn pStmt ? ((
1be2b 56 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 64 62  Vdbe*)pStmt)->db
1be2c 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   : 0;.}../*.** R
1be2d 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
1be2e 74 6f 20 74 68 65 20 6e 65 78 74 20 70 72 65 70  to the next prep
1be2f 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61  ared statement a
1be30 66 74 65 72 20 70 53 74 6d 74 20 61 73 73 6f 63  fter pStmt assoc
1be31 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 64 61  iated.** with da
1be32 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1be33 6e 20 70 44 62 2e 20 20 49 66 20 70 53 74 6d 74  n pDb.  If pStmt
1be34 20 69 73 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e   is NULL, return
1be35 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 70 72   the first.** pr
1be36 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
1be37 20 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73   for the databas
1be38 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 52  e connection.  R
1be39 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68  eturn NULL if th
1be3a 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 6d 6f  ere.** are no mo
1be3b 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  re..*/.SQLITE_AP
1be3c 49 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  I sqlite3_stmt *
1be3d 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d  sqlite3_next_stm
1be3e 74 28 73 71 6c 69 74 65 33 20 2a 70 44 62 2c 20  t(sqlite3 *pDb, 
1be3f 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1be40 74 6d 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  tmt){.  sqlite3_
1be41 73 74 6d 74 20 2a 70 4e 65 78 74 3b 0a 20 20 73  stmt *pNext;.  s
1be42 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
1be43 65 72 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b 0a  er(pDb->mutex);.
1be44 20 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29    if( pStmt==0 )
1be45 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 28 73  {.    pNext = (s
1be46 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 70 44 62  qlite3_stmt*)pDb
1be47 2d 3e 70 56 64 62 65 3b 0a 20 20 7d 65 6c 73 65  ->pVdbe;.  }else
1be48 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 28 73  {.    pNext = (s
1be49 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 28 28 56  qlite3_stmt*)((V
1be4a 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 70 4e 65  dbe*)pStmt)->pNe
1be4b 78 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  xt;.  }.  sqlite
1be4c 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 44  3_mutex_leave(pD
1be4d 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
1be4e 75 72 6e 20 70 4e 65 78 74 3b 0a 7d 0a 0a 2f 2a  urn pNext;.}../*
1be4f 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76  .** Return the v
1be50 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75 73  alue of a status
1be51 20 63 6f 75 6e 74 65 72 20 66 6f 72 20 61 20 70   counter for a p
1be52 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
1be53 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  t.*/.SQLITE_API 
1be54 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  int sqlite3_stmt
1be55 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65 33 5f  _status(sqlite3_
1be56 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1be57 20 6f 70 2c 20 69 6e 74 20 72 65 73 65 74 46 6c   op, int resetFl
1be58 61 67 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 64  ag){.  Vdbe *pVd
1be59 62 65 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d  be = (Vdbe*)pStm
1be5a 74 3b 0a 20 20 69 6e 74 20 76 20 3d 20 70 56 64  t;.  int v = pVd
1be5b 62 65 2d 3e 61 43 6f 75 6e 74 65 72 5b 6f 70 2d  be->aCounter[op-
1be5c 31 5d 3b 0a 20 20 69 66 28 20 72 65 73 65 74 46  1];.  if( resetF
1be5d 6c 61 67 20 29 20 70 56 64 62 65 2d 3e 61 43 6f  lag ) pVdbe->aCo
1be5e 75 6e 74 65 72 5b 6f 70 2d 31 5d 20 3d 20 30 3b  unter[op-1] = 0;
1be5f 0a 20 20 72 65 74 75 72 6e 20 76 3b 0a 7d 0a 0a  .  return v;.}..
1be60 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1be61 45 6e 64 20 6f 66 20 76 64 62 65 61 70 69 2e 63  End of vdbeapi.c
1be62 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1be63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1be64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1be65 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1be66 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 2e  Begin file vdbe.
1be67 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1be68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1be69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1be6a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
1be6b 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
1be6c 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
1be6d 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
1be6e 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
1be6f 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
1be70 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
1be71 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
1be72 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
1be73 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
1be74 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
1be75 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
1be76 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
1be77 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
1be78 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
1be79 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
1be7a 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
1be7b 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
1be7c 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
1be7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1be7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1be7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1be80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1be81 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  ******.** The co
1be82 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
1be83 69 6d 70 6c 65 6d 65 6e 74 73 20 65 78 65 63 75  implements execu
1be84 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f 66 20 74  tion method of t
1be85 68 65 20 0a 2a 2a 20 56 69 72 74 75 61 6c 20 44  he .** Virtual D
1be86 61 74 61 62 61 73 65 20 45 6e 67 69 6e 65 20 28  atabase Engine (
1be87 56 44 42 45 29 2e 20 20 41 20 73 65 70 61 72 61  VDBE).  A separa
1be88 74 65 20 66 69 6c 65 20 28 22 76 64 62 65 61 75  te file ("vdbeau
1be89 78 2e 63 22 29 0a 2a 2a 20 68 61 6e 64 6c 65 73  x.c").** handles
1be8a 20 68 6f 75 73 65 6b 65 65 70 69 6e 67 20 64 65   housekeeping de
1be8b 74 61 69 6c 73 20 73 75 63 68 20 61 73 20 63 72  tails such as cr
1be8c 65 61 74 69 6e 67 20 61 6e 64 20 64 65 6c 65 74  eating and delet
1be8d 69 6e 67 0a 2a 2a 20 56 44 42 45 20 69 6e 73 74  ing.** VDBE inst
1be8e 61 6e 63 65 73 2e 20 20 54 68 69 73 20 66 69 6c  ances.  This fil
1be8f 65 20 69 73 20 73 6f 6c 65 6c 79 20 69 6e 74 65  e is solely inte
1be90 72 65 73 74 65 64 20 69 6e 20 65 78 65 63 75 74  rested in execut
1be91 69 6e 67 0a 2a 2a 20 74 68 65 20 56 44 42 45 20  ing.** the VDBE 
1be92 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49  program..**.** I
1be93 6e 20 74 68 65 20 65 78 74 65 72 6e 61 6c 20 69  n the external i
1be94 6e 74 65 72 66 61 63 65 2c 20 61 6e 20 22 73 71  nterface, an "sq
1be95 6c 69 74 65 33 5f 73 74 6d 74 2a 22 20 69 73 20  lite3_stmt*" is 
1be96 61 6e 20 6f 70 61 71 75 65 20 70 6f 69 6e 74 65  an opaque pointe
1be97 72 0a 2a 2a 20 74 6f 20 61 20 56 44 42 45 2e 0a  r.** to a VDBE..
1be98 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 20 70 61  **.** The SQL pa
1be99 72 73 65 72 20 67 65 6e 65 72 61 74 65 73 20 61  rser generates a
1be9a 20 70 72 6f 67 72 61 6d 20 77 68 69 63 68 20 69   program which i
1be9b 73 20 74 68 65 6e 20 65 78 65 63 75 74 65 64 20  s then executed 
1be9c 62 79 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 74  by.** the VDBE t
1be9d 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66  o do the work of
1be9e 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
1be9f 6e 74 2e 20 20 56 44 42 45 20 70 72 6f 67 72 61  nt.  VDBE progra
1bea0 6d 73 20 61 72 65 20 0a 2a 2a 20 73 69 6d 69 6c  ms are .** simil
1bea1 61 72 20 69 6e 20 66 6f 72 6d 20 74 6f 20 61 73  ar in form to as
1bea2 73 65 6d 62 6c 79 20 6c 61 6e 67 75 61 67 65 2e  sembly language.
1bea3 20 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f    The program co
1bea4 6e 73 69 73 74 73 20 6f 66 0a 2a 2a 20 61 20 6c  nsists of.** a l
1bea5 69 6e 65 61 72 20 73 65 71 75 65 6e 63 65 20 6f  inear sequence o
1bea6 66 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 45  f operations.  E
1bea7 61 63 68 20 6f 70 65 72 61 74 69 6f 6e 20 68 61  ach operation ha
1bea8 73 20 61 6e 20 6f 70 63 6f 64 65 20 0a 2a 2a 20  s an opcode .** 
1bea9 61 6e 64 20 35 20 6f 70 65 72 61 6e 64 73 2e 20  and 5 operands. 
1beaa 20 4f 70 65 72 61 6e 64 73 20 50 31 2c 20 50 32   Operands P1, P2
1beab 2c 20 61 6e 64 20 50 33 20 61 72 65 20 69 6e 74  , and P3 are int
1beac 65 67 65 72 73 2e 20 20 4f 70 65 72 61 6e 64 20  egers.  Operand 
1bead 50 34 20 0a 2a 2a 20 69 73 20 61 20 6e 75 6c 6c  P4 .** is a null
1beae 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69  -terminated stri
1beaf 6e 67 2e 20 20 4f 70 65 72 61 6e 64 20 50 35 20  ng.  Operand P5 
1beb0 69 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 63  is an unsigned c
1beb1 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 46 65 77  haracter..** Few
1beb2 20 6f 70 63 6f 64 65 73 20 75 73 65 20 61 6c 6c   opcodes use all
1beb3 20 35 20 6f 70 65 72 61 6e 64 73 2e 0a 2a 2a 0a   5 operands..**.
1beb4 2a 2a 20 43 6f 6d 70 75 74 61 74 69 6f 6e 20 72  ** Computation r
1beb5 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65  esults are store
1beb6 64 20 6f 6e 20 61 20 73 65 74 20 6f 66 20 72 65  d on a set of re
1beb7 67 69 73 74 65 72 73 20 6e 75 6d 62 65 72 65 64  gisters numbered
1beb8 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69   beginning.** wi
1beb9 74 68 20 31 20 61 6e 64 20 67 6f 69 6e 67 20 75  th 1 and going u
1beba 70 20 74 6f 20 56 64 62 65 2e 6e 4d 65 6d 2e 20  p to Vdbe.nMem. 
1bebb 20 45 61 63 68 20 72 65 67 69 73 74 65 72 20 63   Each register c
1bebc 61 6e 20 73 74 6f 72 65 0a 2a 2a 20 65 69 74 68  an store.** eith
1bebd 65 72 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 61  er an integer, a
1bebe 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64   null-terminated
1bebf 20 73 74 72 69 6e 67 2c 20 61 20 66 6c 6f 61 74   string, a float
1bec0 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 6e 75 6d  ing point.** num
1bec1 62 65 72 2c 20 6f 72 20 74 68 65 20 53 51 4c 20  ber, or the SQL 
1bec2 22 4e 55 4c 4c 22 20 76 61 6c 75 65 2e 20 20 41  "NULL" value.  A
1bec3 6e 20 69 6d 70 6c 69 63 69 74 20 63 6f 6e 76 65  n implicit conve
1bec4 72 73 69 6f 6e 20 66 72 6f 6d 20 6f 6e 65 0a 2a  rsion from one.*
1bec5 2a 20 74 79 70 65 20 74 6f 20 74 68 65 20 6f 74  * type to the ot
1bec6 68 65 72 20 6f 63 63 75 72 73 20 61 73 20 6e 65  her occurs as ne
1bec7 63 65 73 73 61 72 79 2e 0a 2a 2a 20 0a 2a 2a 20  cessary..** .** 
1bec8 4d 6f 73 74 20 6f 66 20 74 68 65 20 63 6f 64 65  Most of the code
1bec9 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73   in this file is
1beca 20 74 61 6b 65 6e 20 75 70 20 62 79 20 74 68 65   taken up by the
1becb 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63   sqlite3VdbeExec
1becc 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77  ().** function w
1becd 68 69 63 68 20 64 6f 65 73 20 74 68 65 20 77 6f  hich does the wo
1bece 72 6b 20 6f 66 20 69 6e 74 65 72 70 72 65 74 69  rk of interpreti
1becf 6e 67 20 61 20 56 44 42 45 20 70 72 6f 67 72 61  ng a VDBE progra
1bed0 6d 2e 0a 2a 2a 20 42 75 74 20 6f 74 68 65 72 20  m..** But other 
1bed1 72 6f 75 74 69 6e 65 73 20 61 72 65 20 61 6c 73  routines are als
1bed2 6f 20 70 72 6f 76 69 64 65 64 20 74 6f 20 68 65  o provided to he
1bed3 6c 70 20 69 6e 20 62 75 69 6c 64 69 6e 67 20 75  lp in building u
1bed4 70 0a 2a 2a 20 61 20 70 72 6f 67 72 61 6d 20 69  p.** a program i
1bed5 6e 73 74 72 75 63 74 69 6f 6e 20 62 79 20 69 6e  nstruction by in
1bed6 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  struction..**.**
1bed7 20 56 61 72 69 6f 75 73 20 73 63 72 69 70 74 73   Various scripts
1bed8 20 73 63 61 6e 20 74 68 69 73 20 73 6f 75 72 63   scan this sourc
1bed9 65 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 20  e file in order 
1beda 74 6f 20 67 65 6e 65 72 61 74 65 20 48 54 4d 4c  to generate HTML
1bedb 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f  .** documentatio
1bedc 6e 2c 20 68 65 61 64 65 72 73 20 66 69 6c 65 73  n, headers files
1bedd 2c 20 6f 72 20 6f 74 68 65 72 20 64 65 72 69 76  , or other deriv
1bede 65 64 20 66 69 6c 65 73 2e 20 20 54 68 65 20 66  ed files.  The f
1bedf 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 66 20  ormatting.** of 
1bee0 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  the code in this
1bee1 20 66 69 6c 65 20 69 73 2c 20 74 68 65 72 65 66   file is, theref
1bee2 6f 72 65 2c 20 69 6d 70 6f 72 74 61 6e 74 2e 20  ore, important. 
1bee3 20 53 65 65 20 6f 74 68 65 72 20 63 6f 6d 6d 65   See other comme
1bee4 6e 74 73 0a 2a 2a 20 69 6e 20 74 68 69 73 20 66  nts.** in this f
1bee5 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73 2e  ile for details.
1bee6 20 20 49 66 20 69 6e 20 64 6f 75 62 74 2c 20 64    If in doubt, d
1bee7 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66 72  o not deviate fr
1bee8 6f 6d 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 63  om existing.** c
1bee9 6f 6d 6d 65 6e 74 69 6e 67 20 61 6e 64 20 69 6e  ommenting and in
1beea 64 65 6e 74 61 74 69 6f 6e 20 70 72 61 63 74 69  dentation practi
1beeb 63 65 73 20 77 68 65 6e 20 63 68 61 6e 67 69 6e  ces when changin
1beec 67 20 6f 72 20 61 64 64 69 6e 67 20 63 6f 64 65  g or adding code
1beed 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62  ..**.** $Id: vdb
1beee 65 2e 63 2c 76 20 31 2e 38 37 34 20 32 30 30 39  e.c,v 1.874 2009
1beef 2f 30 37 2f 32 34 20 31 37 3a 35 38 3a 35 33 20  /07/24 17:58:53 
1bef0 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
1bef1 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  $.*/../*.** The 
1bef2 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c  following global
1bef3 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63   variable is inc
1bef4 72 65 6d 65 6e 74 65 64 20 65 76 65 72 79 20 74  remented every t
1bef5 69 6d 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a 20  ime a cursor.** 
1bef6 6d 6f 76 65 73 2c 20 65 69 74 68 65 72 20 62 79  moves, either by
1bef7 20 74 68 65 20 4f 50 5f 53 65 65 6b 58 58 2c 20   the OP_SeekXX, 
1bef8 4f 50 5f 4e 65 78 74 2c 20 6f 72 20 4f 50 5f 50  OP_Next, or OP_P
1bef9 72 65 76 20 6f 70 63 6f 64 65 73 2e 20 20 54 68  rev opcodes.  Th
1befa 65 20 74 65 73 74 0a 2a 2a 20 70 72 6f 63 65 64  e test.** proced
1befb 75 72 65 73 20 75 73 65 20 74 68 69 73 20 69 6e  ures use this in
1befc 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b  formation to mak
1befd 65 20 73 75 72 65 20 74 68 61 74 20 69 6e 64 69  e sure that indi
1befe 63 65 73 20 61 72 65 0a 2a 2a 20 77 6f 72 6b 69  ces are.** worki
1beff 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54  ng correctly.  T
1bf00 68 69 73 20 76 61 72 69 61 62 6c 65 20 68 61 73  his variable has
1bf01 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68   no function oth
1bf02 65 72 20 74 68 61 6e 20 74 6f 0a 2a 2a 20 68 65  er than to.** he
1bf03 6c 70 20 76 65 72 69 66 79 20 74 68 65 20 63 6f  lp verify the co
1bf04 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20  rrect operation 
1bf05 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a  of the library..
1bf06 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
1bf07 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
1bf08 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 61   int sqlite3_sea
1bf09 72 63 68 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23  rch_count = 0;.#
1bf0a 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65  endif../*.** Whe
1bf0b 6e 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61  n this global va
1bf0c 72 69 61 62 6c 65 20 69 73 20 70 6f 73 69 74 69  riable is positi
1bf0d 76 65 2c 20 69 74 20 67 65 74 73 20 64 65 63 72  ve, it gets decr
1bf0e 65 6d 65 6e 74 65 64 20 6f 6e 63 65 20 62 65 66  emented once bef
1bf0f 6f 72 65 0a 2a 2a 20 65 61 63 68 20 69 6e 73 74  ore.** each inst
1bf10 72 75 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 56  ruction in the V
1bf11 44 42 45 2e 20 20 57 68 65 6e 20 72 65 61 63 68  DBE.  When reach
1bf12 65 73 20 7a 65 72 6f 2c 20 74 68 65 20 75 31 2e  es zero, the u1.
1bf13 69 73 49 6e 74 65 72 72 75 70 74 65 64 0a 2a 2a  isInterrupted.**
1bf14 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71   field of the sq
1bf15 6c 69 74 65 33 20 73 74 72 75 63 74 75 72 65 20  lite3 structure 
1bf16 69 73 20 73 65 74 20 69 6e 20 6f 72 64 65 72 20  is set in order 
1bf17 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e 64 20  to simulate and 
1bf18 69 6e 74 65 72 72 75 70 74 2e 0a 2a 2a 0a 2a 2a  interrupt..**.**
1bf19 20 54 68 69 73 20 66 61 63 69 6c 69 74 79 20 69   This facility i
1bf1a 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69  s used for testi
1bf1b 6e 67 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79  ng purposes only
1bf1c 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 66  .  It does not f
1bf1d 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 6e 20 61 6e  unction.** in an
1bf1e 20 6f 72 64 69 6e 61 72 79 20 62 75 69 6c 64 2e   ordinary build.
1bf1f 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
1bf20 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
1bf21 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6e  I int sqlite3_in
1bf22 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 20 3d 20  terrupt_count = 
1bf23 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0;.#endif../*.**
1bf24 20 54 68 65 20 6e 65 78 74 20 67 6c 6f 62 61 6c   The next global
1bf25 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63   variable is inc
1bf26 72 65 6d 65 6e 74 65 64 20 65 61 63 68 20 74 79  remented each ty
1bf27 70 65 20 74 68 65 20 4f 50 5f 53 6f 72 74 20 6f  pe the OP_Sort o
1bf28 70 63 6f 64 65 0a 2a 2a 20 69 73 20 65 78 65 63  pcode.** is exec
1bf29 75 74 65 64 2e 20 20 54 68 65 20 74 65 73 74 20  uted.  The test 
1bf2a 70 72 6f 63 65 64 75 72 65 73 20 75 73 65 20 74  procedures use t
1bf2b 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  his information 
1bf2c 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
1bf2d 74 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 69 73 20  t.** sorting is 
1bf2e 6f 63 63 75 72 72 69 6e 67 20 6f 72 20 6e 6f 74  occurring or not
1bf2f 20 6f 63 63 75 72 72 69 6e 67 20 61 74 20 61 70   occurring at ap
1bf30 70 72 6f 70 72 69 61 74 65 20 74 69 6d 65 73 2e  propriate times.
1bf31 20 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65     This variable
1bf32 0a 2a 2a 20 68 61 73 20 6e 6f 20 66 75 6e 63 74  .** has no funct
1bf33 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74  ion other than t
1bf34 6f 20 68 65 6c 70 20 76 65 72 69 66 79 20 74 68  o help verify th
1bf35 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74  e correct operat
1bf36 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 69  ion of the.** li
1bf37 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66  brary..*/.#ifdef
1bf38 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
1bf39 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1bf3a 74 65 33 5f 73 6f 72 74 5f 63 6f 75 6e 74 20 3d  te3_sort_count =
1bf3b 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a   0;.#endif../*.*
1bf3c 2a 20 54 68 65 20 6e 65 78 74 20 67 6c 6f 62 61  * The next globa
1bf3d 6c 20 76 61 72 69 61 62 6c 65 20 72 65 63 6f 72  l variable recor
1bf3e 64 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  ds the size of t
1bf3f 68 65 20 6c 61 72 67 65 73 74 20 4d 45 4d 5f 42  he largest MEM_B
1bf40 6c 6f 62 0a 2a 2a 20 6f 72 20 4d 45 4d 5f 53 74  lob.** or MEM_St
1bf41 72 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20  r that has been 
1bf42 75 73 65 64 20 62 79 20 61 20 56 44 42 45 20 6f  used by a VDBE o
1bf43 70 63 6f 64 65 2e 20 20 54 68 65 20 74 65 73 74  pcode.  The test
1bf44 20 70 72 6f 63 65 64 75 72 65 73 0a 2a 2a 20 75   procedures.** u
1bf45 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74  se this informat
1bf46 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  ion to make sure
1bf47 20 74 68 61 74 20 74 68 65 20 7a 65 72 6f 2d 62   that the zero-b
1bf48 6c 6f 62 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74  lob functionalit
1bf49 79 0a 2a 2a 20 69 73 20 77 6f 72 6b 69 6e 67 20  y.** is working 
1bf4a 63 6f 72 72 65 63 74 6c 79 2e 20 20 20 54 68 69  correctly.   Thi
1bf4b 73 20 76 61 72 69 61 62 6c 65 20 68 61 73 20 6e  s variable has n
1bf4c 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74 68 65 72  o function other
1bf4d 20 74 68 61 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70   than to.** help
1bf4e 20 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72   verify the corr
1bf4f 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66  ect operation of
1bf50 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f   the library..*/
1bf51 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
1bf52 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  EST.SQLITE_API i
1bf53 6e 74 20 73 71 6c 69 74 65 33 5f 6d 61 78 5f 62  nt sqlite3_max_b
1bf54 6c 6f 62 73 69 7a 65 20 3d 20 30 3b 0a 73 74 61  lobsize = 0;.sta
1bf55 74 69 63 20 76 6f 69 64 20 75 70 64 61 74 65 4d  tic void updateM
1bf56 61 78 42 6c 6f 62 73 69 7a 65 28 4d 65 6d 20 2a  axBlobsize(Mem *
1bf57 70 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c  p){.  if( (p->fl
1bf58 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d  ags & (MEM_Str|M
1bf59 45 4d 5f 42 6c 6f 62 29 29 21 3d 30 20 26 26 20  EM_Blob))!=0 && 
1bf5a 70 2d 3e 6e 3e 73 71 6c 69 74 65 33 5f 6d 61 78  p->n>sqlite3_max
1bf5b 5f 62 6c 6f 62 73 69 7a 65 20 29 7b 0a 20 20 20  _blobsize ){.   
1bf5c 20 73 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f   sqlite3_max_blo
1bf5d 62 73 69 7a 65 20 3d 20 70 2d 3e 6e 3b 0a 20 20  bsize = p->n;.  
1bf5e 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  }.}.#endif../*.*
1bf5f 2a 20 54 68 65 20 6e 65 78 74 20 67 6c 6f 62 61  * The next globa
1bf60 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20 69 6e  l variable is in
1bf61 63 72 65 6d 65 6e 74 65 64 20 65 61 63 68 20 74  cremented each t
1bf62 79 70 65 20 74 68 65 20 4f 50 5f 46 6f 75 6e 64  ype the OP_Found
1bf63 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 20 65 78   opcode.** is ex
1bf64 65 63 75 74 65 64 2e 20 54 68 69 73 20 69 73 20  ecuted. This is 
1bf65 75 73 65 64 20 74 6f 20 74 65 73 74 20 77 68 65  used to test whe
1bf66 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
1bf67 66 6f 72 65 69 67 6e 20 6b 65 79 0a 2a 2a 20 6f  foreign key.** o
1bf68 70 65 72 61 74 69 6f 6e 20 69 6d 70 6c 65 6d 65  peration impleme
1bf69 6e 74 65 64 20 75 73 69 6e 67 20 4f 50 5f 46 6b  nted using OP_Fk
1bf6a 49 73 5a 65 72 6f 20 69 73 20 77 6f 72 6b 69 6e  IsZero is workin
1bf6b 67 2e 20 54 68 69 73 20 76 61 72 69 61 62 6c 65  g. This variable
1bf6c 0a 2a 2a 20 68 61 73 20 6e 6f 20 66 75 6e 63 74  .** has no funct
1bf6d 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20 74  ion other than t
1bf6e 6f 20 68 65 6c 70 20 76 65 72 69 66 79 20 74 68  o help verify th
1bf6f 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74  e correct operat
1bf70 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 69  ion of the.** li
1bf71 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66  brary..*/.#ifdef
1bf72 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
1bf73 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1bf74 74 65 33 5f 66 6f 75 6e 64 5f 63 6f 75 6e 74 20  te3_found_count 
1bf75 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  = 0;.#endif../*.
1bf76 2a 2a 20 54 65 73 74 20 61 20 72 65 67 69 73 74  ** Test a regist
1bf77 65 72 20 74 6f 20 73 65 65 20 69 66 20 69 74 20  er to see if it 
1bf78 65 78 63 65 65 64 73 20 74 68 65 20 63 75 72 72  exceeds the curr
1bf79 65 6e 74 20 6d 61 78 69 6d 75 6d 20 62 6c 6f 62  ent maximum blob
1bf7a 20 73 69 7a 65 2e 0a 2a 2a 20 49 66 20 69 74 20   size..** If it 
1bf7b 64 6f 65 73 2c 20 72 65 63 6f 72 64 20 74 68 65  does, record the
1bf7c 20 6e 65 77 20 6d 61 78 69 6d 75 6d 20 62 6c 6f   new maximum blo
1bf7d 62 20 73 69 7a 65 2e 0a 2a 2f 0a 23 69 66 20 64  b size..*/.#if d
1bf7e 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45  efined(SQLITE_TE
1bf7f 53 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  ST) && !defined(
1bf80 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c  SQLITE_OMIT_BUIL
1bf81 54 49 4e 5f 54 45 53 54 29 0a 23 20 64 65 66 69  TIN_TEST).# defi
1bf82 6e 65 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c  ne UPDATE_MAX_BL
1bf83 4f 42 53 49 5a 45 28 50 29 20 20 75 70 64 61 74  OBSIZE(P)  updat
1bf84 65 4d 61 78 42 6c 6f 62 73 69 7a 65 28 50 29 0a  eMaxBlobsize(P).
1bf85 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 55  #else.# define U
1bf86 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49  PDATE_MAX_BLOBSI
1bf87 5a 45 28 50 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ZE(P).#endif../*
1bf88 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20  .** Convert the 
1bf89 67 69 76 65 6e 20 72 65 67 69 73 74 65 72 20 69  given register i
1bf8a 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 69 66 20  nto a string if 
1bf8b 69 74 20 69 73 6e 27 74 20 6f 6e 65 0a 2a 2a 20  it isn't one.** 
1bf8c 61 6c 72 65 61 64 79 2e 20 52 65 74 75 72 6e 20  already. Return 
1bf8d 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 6d 61  non-zero if a ma
1bf8e 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a 2a 2f  lloc() fails..*/
1bf8f 0a 23 64 65 66 69 6e 65 20 53 74 72 69 6e 67 69  .#define Stringi
1bf90 66 79 28 50 2c 20 65 6e 63 29 20 5c 0a 20 20 20  fy(P, enc) \.   
1bf91 69 66 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 28  if(((P)->flags&(
1bf92 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62  MEM_Str|MEM_Blob
1bf93 29 29 3d 3d 30 20 26 26 20 73 71 6c 69 74 65 33  ))==0 && sqlite3
1bf94 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79  VdbeMemStringify
1bf95 28 50 2c 65 6e 63 29 29 20 5c 0a 20 20 20 20 20  (P,enc)) \.     
1bf96 7b 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 20 7d  { goto no_mem; }
1bf97 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 65 70 68 65 6d  ../*.** An ephem
1bf98 65 72 61 6c 20 73 74 72 69 6e 67 20 76 61 6c 75  eral string valu
1bf99 65 20 28 73 69 67 6e 69 66 69 65 64 20 62 79 20  e (signified by 
1bf9a 74 68 65 20 4d 45 4d 5f 45 70 68 65 6d 20 66 6c  the MEM_Ephem fl
1bf9b 61 67 29 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20  ag) contains.** 
1bf9c 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 64  a pointer to a d
1bf9d 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63  ynamically alloc
1bf9e 61 74 65 64 20 73 74 72 69 6e 67 20 77 68 65 72  ated string wher
1bf9f 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 6e 74  e some other ent
1bfa0 69 74 79 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e  ity.** is respon
1bfa1 73 69 62 6c 65 20 66 6f 72 20 64 65 61 6c 6c 6f  sible for deallo
1bfa2 63 61 74 69 6e 67 20 74 68 61 74 20 73 74 72 69  cating that stri
1bfa3 6e 67 2e 20 20 42 65 63 61 75 73 65 20 74 68 65  ng.  Because the
1bfa4 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 64 6f 65   register.** doe
1bfa5 73 20 6e 6f 74 20 63 6f 6e 74 72 6f 6c 20 74 68  s not control th
1bfa6 65 20 73 74 72 69 6e 67 2c 20 69 74 20 6d 69 67  e string, it mig
1bfa7 68 74 20 62 65 20 64 65 6c 65 74 65 64 20 77 69  ht be deleted wi
1bfa8 74 68 6f 75 74 20 74 68 65 20 72 65 67 69 73 74  thout the regist
1bfa9 65 72 0a 2a 2a 20 6b 6e 6f 77 69 6e 67 20 69 74  er.** knowing it
1bfaa 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1bfab 74 69 6e 65 20 63 6f 6e 76 65 72 74 73 20 61 6e  tine converts an
1bfac 20 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e   ephemeral strin
1bfad 67 20 69 6e 74 6f 20 61 20 64 79 6e 61 6d 69 63  g into a dynamic
1bfae 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 0a 2a  ally allocated.*
1bfaf 2a 20 73 74 72 69 6e 67 20 74 68 61 74 20 74 68  * string that th
1bfb0 65 20 72 65 67 69 73 74 65 72 20 69 74 73 65 6c  e register itsel
1bfb1 66 20 63 6f 6e 74 72 6f 6c 73 2e 20 20 49 6e 20  f controls.  In 
1bfb2 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 74 0a  other words, it.
1bfb3 2a 2a 20 63 6f 6e 76 65 72 74 73 20 61 6e 20 4d  ** converts an M
1bfb4 45 4d 5f 45 70 68 65 6d 20 73 74 72 69 6e 67 20  EM_Ephem string 
1bfb5 69 6e 74 6f 20 61 6e 20 4d 45 4d 5f 44 79 6e 20  into an MEM_Dyn 
1bfb6 73 74 72 69 6e 67 2e 0a 2a 2f 0a 23 64 65 66 69  string..*/.#defi
1bfb7 6e 65 20 44 65 65 70 68 65 6d 65 72 61 6c 69 7a  ne Deephemeraliz
1bfb8 65 28 50 29 20 5c 0a 20 20 20 69 66 28 20 28 28  e(P) \.   if( ((
1bfb9 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 45 70  P)->flags&MEM_Ep
1bfba 68 65 6d 29 21 3d 30 20 5c 0a 20 20 20 20 20 20  hem)!=0 \.      
1bfbb 20 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d   && sqlite3VdbeM
1bfbc 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28  emMakeWriteable(
1bfbd 50 29 20 29 7b 20 67 6f 74 6f 20 6e 6f 5f 6d 65  P) ){ goto no_me
1bfbe 6d 3b 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  m;}../*.** Call 
1bfbf 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78  sqlite3VdbeMemEx
1bfc0 70 61 6e 64 42 6c 6f 62 28 29 20 6f 6e 20 74 68  pandBlob() on th
1bfc1 65 20 73 75 70 70 6c 69 65 64 20 76 61 6c 75 65  e supplied value
1bfc2 20 28 74 79 70 65 20 4d 65 6d 2a 29 0a 2a 2a 20   (type Mem*).** 
1bfc3 50 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a  P if required..*
1bfc4 2f 0a 23 64 65 66 69 6e 65 20 45 78 70 61 6e 64  /.#define Expand
1bfc5 42 6c 6f 62 28 50 29 20 28 28 28 50 29 2d 3e 66  Blob(P) (((P)->f
1bfc6 6c 61 67 73 26 4d 45 4d 5f 5a 65 72 6f 29 3f 73  lags&MEM_Zero)?s
1bfc7 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70  qlite3VdbeMemExp
1bfc8 61 6e 64 42 6c 6f 62 28 50 29 3a 30 29 0a 0a 2f  andBlob(P):0)../
1bfc9 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 4d  *.** Argument pM
1bfca 65 6d 20 70 6f 69 6e 74 73 20 61 74 20 61 20 72  em points at a r
1bfcb 65 67 69 73 74 65 72 20 74 68 61 74 20 77 69 6c  egister that wil
1bfcc 6c 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 61  l be passed to a
1bfcd 0a 2a 2a 20 75 73 65 72 2d 64 65 66 69 6e 65 64  .** user-defined
1bfce 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20 72 65 74   function or ret
1bfcf 75 72 6e 65 64 20 74 6f 20 74 68 65 20 75 73 65  urned to the use
1bfd0 72 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 20  r as the result 
1bfd1 6f 66 20 61 20 71 75 65 72 79 2e 0a 2a 2a 20 54  of a query..** T
1bfd2 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
1bfd3 6e 74 2c 20 27 64 62 5f 65 6e 63 27 20 69 73 20  nt, 'db_enc' is 
1bfd4 74 68 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  the text encodin
1bfd5 67 20 75 73 65 64 20 62 79 20 74 68 65 20 76 64  g used by the vd
1bfd6 62 65 20 66 6f 72 0a 2a 2a 20 72 65 67 69 73 74  be for.** regist
1bfd7 65 72 20 76 61 72 69 61 62 6c 65 73 2e 20 20 54  er variables.  T
1bfd8 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73  his routine sets
1bfd9 20 74 68 65 20 70 4d 65 6d 2d 3e 65 6e 63 20 61   the pMem->enc a
1bfda 6e 64 20 70 4d 65 6d 2d 3e 74 79 70 65 0a 2a 2a  nd pMem->type.**
1bfdb 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20   variables used 
1bfdc 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  by the sqlite3_v
1bfdd 61 6c 75 65 5f 2a 28 29 20 72 6f 75 74 69 6e 65  alue_*() routine
1bfde 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73 74  s..*/.#define st
1bfdf 6f 72 65 54 79 70 65 49 6e 66 6f 28 41 2c 42 29  oreTypeInfo(A,B)
1bfe0 20 5f 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28   _storeTypeInfo(
1bfe1 41 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 5f  A).static void _
1bfe2 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 4d 65  storeTypeInfo(Me
1bfe3 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20  m *pMem){.  int 
1bfe4 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c  flags = pMem->fl
1bfe5 61 67 73 3b 0a 20 20 69 66 28 20 66 6c 61 67 73  ags;.  if( flags
1bfe6 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20   & MEM_Null ){. 
1bfe7 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
1bfe8 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 7d  SQLITE_NULL;.  }
1bfe9 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67  .  else if( flag
1bfea 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20  s & MEM_Int ){. 
1bfeb 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
1bfec 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a  SQLITE_INTEGER;.
1bfed 20 20 7d 0a 20 20 65 6c 73 65 20 69 66 28 20 66    }.  else if( f
1bfee 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20  lags & MEM_Real 
1bfef 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70  ){.    pMem->typ
1bff0 65 20 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54  e = SQLITE_FLOAT
1bff1 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 20 69 66 28  ;.  }.  else if(
1bff2 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72   flags & MEM_Str
1bff3 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79   ){.    pMem->ty
1bff4 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54  pe = SQLITE_TEXT
1bff5 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
1bff6 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
1bff7 54 45 5f 42 4c 4f 42 3b 0a 20 20 7d 0a 7d 0a 0a  TE_BLOB;.  }.}..
1bff8 2f 2a 0a 2a 2a 20 50 72 6f 70 65 72 74 69 65 73  /*.** Properties
1bff9 20 6f 66 20 6f 70 63 6f 64 65 73 2e 20 20 54 68   of opcodes.  Th
1bffa 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49  e OPFLG_INITIALI
1bffb 5a 45 52 20 6d 61 63 72 6f 20 69 73 0a 2a 2a 20  ZER macro is.** 
1bffc 63 72 65 61 74 65 64 20 62 79 20 6d 6b 6f 70 63  created by mkopc
1bffd 6f 64 65 68 2e 61 77 6b 20 64 75 72 69 6e 67 20  odeh.awk during 
1bffe 63 6f 6d 70 69 6c 61 74 69 6f 6e 2e 20 20 44 61  compilation.  Da
1bfff 74 61 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a  ta is obtained.*
1c000 2a 20 66 72 6f 6d 20 74 68 65 20 63 6f 6d 6d 65  * from the comme
1c001 6e 74 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  nts following th
1c002 65 20 22 63 61 73 65 20 4f 50 5f 78 78 78 78 3a  e "case OP_xxxx:
1c003 22 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 0a  " statements in.
1c004 2a 2a 20 74 68 69 73 20 66 69 6c 65 2e 20 20 0a  ** this file.  .
1c005 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
1c006 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f 70  unsigned char op
1c007 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 5d 20 3d  codeProperty[] =
1c008 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a   OPFLG_INITIALIZ
1c009 45 52 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ER;../*.** Retur
1c00a 6e 20 74 72 75 65 20 69 66 20 61 6e 20 6f 70 63  n true if an opc
1c00b 6f 64 65 20 68 61 73 20 61 6e 79 20 6f 66 20 74  ode has any of t
1c00c 68 65 20 4f 50 46 4c 47 5f 78 78 78 20 70 72 6f  he OPFLG_xxx pro
1c00d 70 65 72 74 69 65 73 0a 2a 2a 20 73 70 65 63 69  perties.** speci
1c00e 66 69 65 64 20 62 79 20 6d 61 73 6b 2e 0a 2a 2f  fied by mask..*/
1c00f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1c010 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4f  int sqlite3VdbeO
1c011 70 63 6f 64 65 48 61 73 50 72 6f 70 65 72 74 79  pcodeHasProperty
1c012 28 69 6e 74 20 6f 70 63 6f 64 65 2c 20 69 6e 74  (int opcode, int
1c013 20 6d 61 73 6b 29 7b 0a 20 20 61 73 73 65 72 74   mask){.  assert
1c014 28 20 6f 70 63 6f 64 65 3e 30 20 26 26 20 6f 70  ( opcode>0 && op
1c015 63 6f 64 65 3c 28 69 6e 74 29 73 69 7a 65 6f 66  code<(int)sizeof
1c016 28 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 29  (opcodeProperty)
1c017 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 6f 70   );.  return (op
1c018 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 6f 70 63  codeProperty[opc
1c019 6f 64 65 5d 26 6d 61 73 6b 29 21 3d 30 3b 0a 7d  ode]&mask)!=0;.}
1c01a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
1c01b 20 56 64 62 65 43 75 72 73 6f 72 20 6e 75 6d 62   VdbeCursor numb
1c01c 65 72 20 69 43 75 72 2e 20 20 52 65 74 75 72 6e  er iCur.  Return
1c01d 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74   a pointer to it
1c01e 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 0a 2a  .  Return NULL.*
1c01f 2a 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20  * if we run out 
1c020 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74  of memory..*/.st
1c021 61 74 69 63 20 56 64 62 65 43 75 72 73 6f 72 20  atic VdbeCursor 
1c022 2a 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28  *allocateCursor(
1c023 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20  .  Vdbe *p,     
1c024 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1c025 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  virtual machine 
1c026 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20  */.  int iCur,  
1c027 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
1c028 64 65 78 20 6f 66 20 74 68 65 20 6e 65 77 20 56  dex of the new V
1c029 64 62 65 43 75 72 73 6f 72 20 2a 2f 0a 20 20 69  dbeCursor */.  i
1c02a 6e 74 20 6e 46 69 65 6c 64 2c 20 20 20 20 20 20  nt nField,      
1c02b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1c02c 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20  f fields in the 
1c02d 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 2a  table or index *
1c02e 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20  /.  int iDb,    
1c02f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65            /* Whe
1c030 6e 20 64 61 74 61 62 61 73 65 20 74 68 65 20 63  n database the c
1c031 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 74 6f  ursor belongs to
1c032 2c 20 6f 72 20 2d 31 20 2a 2f 0a 20 20 69 6e 74  , or -1 */.  int
1c033 20 69 73 42 74 72 65 65 43 75 72 73 6f 72 20 20   isBtreeCursor  
1c034 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 42     /* True for B
1c035 2d 54 72 65 65 2e 20 20 46 61 6c 73 65 20 66 6f  -Tree.  False fo
1c036 72 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 6f  r pseudo-table o
1c037 72 20 76 74 61 62 20 2a 2f 0a 29 7b 0a 20 20 2f  r vtab */.){.  /
1c038 2a 20 46 69 6e 64 20 74 68 65 20 6d 65 6d 6f 72  * Find the memor
1c039 79 20 63 65 6c 6c 20 74 68 61 74 20 77 69 6c 6c  y cell that will
1c03a 20 62 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72   be used to stor
1c03b 65 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65  e the blob of me
1c03c 6d 6f 72 79 0a 20 20 2a 2a 20 72 65 71 75 69 72  mory.  ** requir
1c03d 65 64 20 66 6f 72 20 74 68 69 73 20 56 64 62 65  ed for this Vdbe
1c03e 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65  Cursor structure
1c03f 2e 20 49 74 20 69 73 20 63 6f 6e 76 65 6e 69 65  . It is convenie
1c040 6e 74 20 74 6f 20 75 73 65 20 61 20 0a 20 20 2a  nt to use a .  *
1c041 2a 20 76 64 62 65 20 6d 65 6d 6f 72 79 20 63 65  * vdbe memory ce
1c042 6c 6c 20 74 6f 20 6d 61 6e 61 67 65 20 74 68 65  ll to manage the
1c043 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
1c044 6f 6e 20 72 65 71 75 69 72 65 64 20 66 6f 72 20  on required for 
1c045 61 0a 20 20 2a 2a 20 56 64 62 65 43 75 72 73 6f  a.  ** VdbeCurso
1c046 72 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20  r structure for 
1c047 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65  the following re
1c048 61 73 6f 6e 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  asons:.  **.  **
1c049 20 20 20 2a 20 53 6f 6d 65 74 69 6d 65 73 20 63     * Sometimes c
1c04a 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 72  ursor numbers ar
1c04b 65 20 75 73 65 64 20 66 6f 72 20 61 20 63 6f 75  e used for a cou
1c04c 70 6c 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74  ple of different
1c04d 0a 20 20 2a 2a 20 20 20 20 20 70 75 72 70 6f 73  .  **     purpos
1c04e 65 73 20 69 6e 20 61 20 76 64 62 65 20 70 72 6f  es in a vdbe pro
1c04f 67 72 61 6d 2e 20 54 68 65 20 64 69 66 66 65 72  gram. The differ
1c050 65 6e 74 20 75 73 65 73 20 6d 69 67 68 74 20 72  ent uses might r
1c051 65 71 75 69 72 65 0a 20 20 2a 2a 20 20 20 20 20  equire.  **     
1c052 64 69 66 66 65 72 65 6e 74 20 73 69 7a 65 64 20  different sized 
1c053 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 20 4d 65 6d  allocations. Mem
1c054 6f 72 79 20 63 65 6c 6c 73 20 70 72 6f 76 69 64  ory cells provid
1c055 65 20 67 72 6f 77 61 62 6c 65 0a 20 20 2a 2a 20  e growable.  ** 
1c056 20 20 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e      allocations.
1c057 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57  .  **.  **   * W
1c058 68 65 6e 20 75 73 69 6e 67 20 45 4e 41 42 4c 45  hen using ENABLE
1c059 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45  _MEMORY_MANAGEME
1c05a 4e 54 2c 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  NT, memory cell 
1c05b 62 75 66 66 65 72 73 20 63 61 6e 0a 20 20 2a 2a  buffers can.  **
1c05c 20 20 20 20 20 62 65 20 66 72 65 65 64 20 6c 61       be freed la
1c05d 7a 69 6c 79 20 76 69 61 20 74 68 65 20 73 71 6c  zily via the sql
1c05e 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
1c05f 6f 72 79 28 29 20 41 50 49 2e 20 54 68 69 73 0a  ory() API. This.
1c060 20 20 2a 2a 20 20 20 20 20 6d 69 6e 69 6d 69 7a    **     minimiz
1c061 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  es the number of
1c062 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c 73 20 6d 61   malloc calls ma
1c063 64 65 20 62 79 20 74 68 65 20 73 79 73 74 65 6d  de by the system
1c064 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 65 6d 6f  ..  **.  ** Memo
1c065 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 63 75 72  ry cells for cur
1c066 73 6f 72 73 20 61 72 65 20 61 6c 6c 6f 63 61 74  sors are allocat
1c067 65 64 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66  ed at the top of
1c068 20 74 68 65 20 61 64 64 72 65 73 73 0a 20 20 2a   the address.  *
1c069 2a 20 73 70 61 63 65 2e 20 4d 65 6d 6f 72 79 20  * space. Memory 
1c06a 63 65 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 29 20 63  cell (p->nMem) c
1c06b 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 63 75  orresponds to cu
1c06c 72 73 6f 72 20 30 2e 20 53 70 61 63 65 20 66 6f  rsor 0. Space fo
1c06d 72 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20 31 20  r.  ** cursor 1 
1c06e 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20 6d 65  is managed by me
1c06f 6d 6f 72 79 20 63 65 6c 6c 20 28 70 2d 3e 6e 4d  mory cell (p->nM
1c070 65 6d 2d 31 29 2c 20 65 74 63 2e 0a 20 20 2a 2f  em-1), etc..  */
1c071 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26  .  Mem *pMem = &
1c072 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 2d  p->aMem[p->nMem-
1c073 69 43 75 72 5d 3b 0a 0a 20 20 69 6e 74 20 6e 42  iCur];..  int nB
1c074 79 74 65 3b 0a 20 20 56 64 62 65 43 75 72 73 6f  yte;.  VdbeCurso
1c075 72 20 2a 70 43 78 20 3d 20 30 3b 0a 20 20 6e 42  r *pCx = 0;.  nB
1c076 79 74 65 20 3d 20 0a 20 20 20 20 20 20 73 69 7a  yte = .      siz
1c077 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 20  eof(VdbeCursor) 
1c078 2b 20 0a 20 20 20 20 20 20 28 69 73 42 74 72 65  + .      (isBtre
1c079 65 43 75 72 73 6f 72 3f 73 71 6c 69 74 65 33 42  eCursor?sqlite3B
1c07a 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 28 29  treeCursorSize()
1c07b 3a 30 29 20 2b 20 0a 20 20 20 20 20 20 32 2a 6e  :0) + .      2*n
1c07c 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 75 33 32  Field*sizeof(u32
1c07d 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43  );..  assert( iC
1c07e 75 72 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b  ur<p->nCursor );
1c07f 0a 20 20 69 66 28 20 70 2d 3e 61 70 43 73 72 5b  .  if( p->apCsr[
1c080 69 43 75 72 5d 20 29 7b 0a 20 20 20 20 73 71 6c  iCur] ){.    sql
1c081 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73  ite3VdbeFreeCurs
1c082 6f 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b 69  or(p, p->apCsr[i
1c083 43 75 72 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 70  Cur]);.    p->ap
1c084 43 73 72 5b 69 43 75 72 5d 20 3d 20 30 3b 0a 20  Csr[iCur] = 0;. 
1c085 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f   }.  if( SQLITE_
1c086 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 64 62 65 4d  OK==sqlite3VdbeM
1c087 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79  emGrow(pMem, nBy
1c088 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 70 2d  te, 0) ){.    p-
1c089 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 3d 20 70  >apCsr[iCur] = p
1c08a 43 78 20 3d 20 28 56 64 62 65 43 75 72 73 6f 72  Cx = (VdbeCursor
1c08b 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 6d  *)pMem->z;.    m
1c08c 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30  emset(pMem->z, 0
1c08d 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 70 43  , nByte);.    pC
1c08e 78 2d 3e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20  x->iDb = iDb;.  
1c08f 20 20 70 43 78 2d 3e 6e 46 69 65 6c 64 20 3d 20    pCx->nField = 
1c090 6e 46 69 65 6c 64 3b 0a 20 20 20 20 69 66 28 20  nField;.    if( 
1c091 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20 20 20 20  nField ){.      
1c092 70 43 78 2d 3e 61 54 79 70 65 20 3d 20 28 75 33  pCx->aType = (u3
1c093 32 20 2a 29 26 70 4d 65 6d 2d 3e 7a 5b 73 69 7a  2 *)&pMem->z[siz
1c094 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 5d  eof(VdbeCursor)]
1c095 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1c096 69 73 42 74 72 65 65 43 75 72 73 6f 72 20 29 7b  isBtreeCursor ){
1c097 0a 20 20 20 20 20 20 70 43 78 2d 3e 70 43 75 72  .      pCx->pCur
1c098 73 6f 72 20 3d 20 28 42 74 43 75 72 73 6f 72 2a  sor = (BtCursor*
1c099 29 0a 20 20 20 20 20 20 20 20 20 20 26 70 4d 65  ).          &pMe
1c09a 6d 2d 3e 7a 5b 73 69 7a 65 6f 66 28 56 64 62 65  m->z[sizeof(Vdbe
1c09b 43 75 72 73 6f 72 29 2b 32 2a 6e 46 69 65 6c 64  Cursor)+2*nField
1c09c 2a 73 69 7a 65 6f 66 28 75 33 32 29 5d 3b 0a 20  *sizeof(u32)];. 
1c09d 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
1c09e 6e 20 70 43 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n pCx;.}../*.** 
1c09f 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 61  Try to convert a
1c0a0 20 76 61 6c 75 65 20 69 6e 74 6f 20 61 20 6e 75   value into a nu
1c0a1 6d 65 72 69 63 20 72 65 70 72 65 73 65 6e 74 61  meric representa
1c0a2 74 69 6f 6e 20 69 66 20 77 65 20 63 61 6e 0a 2a  tion if we can.*
1c0a3 2a 20 64 6f 20 73 6f 20 77 69 74 68 6f 75 74 20  * do so without 
1c0a4 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74  loss of informat
1c0a5 69 6f 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  ion.  In other w
1c0a6 6f 72 64 73 2c 20 69 66 20 74 68 65 20 73 74 72  ords, if the str
1c0a7 69 6e 67 0a 2a 2a 20 6c 6f 6f 6b 73 20 6c 69 6b  ing.** looks lik
1c0a8 65 20 61 20 6e 75 6d 62 65 72 2c 20 63 6f 6e 76  e a number, conv
1c0a9 65 72 74 20 69 74 20 69 6e 74 6f 20 61 20 6e 75  ert it into a nu
1c0aa 6d 62 65 72 2e 20 20 49 66 20 69 74 20 64 6f 65  mber.  If it doe
1c0ab 73 20 6e 6f 74 0a 2a 2a 20 6c 6f 6f 6b 20 6c 69  s not.** look li
1c0ac 6b 65 20 61 20 6e 75 6d 62 65 72 2c 20 6c 65 61  ke a number, lea
1c0ad 76 65 20 69 74 20 61 6c 6f 6e 65 2e 0a 2a 2f 0a  ve it alone..*/.
1c0ae 73 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 6c  static void appl
1c0af 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79  yNumericAffinity
1c0b0 28 4d 65 6d 20 2a 70 52 65 63 29 7b 0a 20 20 69  (Mem *pRec){.  i
1c0b1 66 28 20 28 70 52 65 63 2d 3e 66 6c 61 67 73 20  f( (pRec->flags 
1c0b2 26 20 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f  & (MEM_Real|MEM_
1c0b3 49 6e 74 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Int))==0 ){.    
1c0b4 69 6e 74 20 72 65 61 6c 6e 75 6d 3b 0a 20 20 20  int realnum;.   
1c0b5 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e   sqlite3VdbeMemN
1c0b6 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 52 65 63  ulTerminate(pRec
1c0b7 29 3b 0a 20 20 20 20 69 66 28 20 28 70 52 65 63  );.    if( (pRec
1c0b8 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29  ->flags&MEM_Str)
1c0b9 0a 20 20 20 20 20 20 20 20 20 26 26 20 73 71 6c  .         && sql
1c0ba 69 74 65 33 49 73 4e 75 6d 62 65 72 28 70 52 65  ite3IsNumber(pRe
1c0bb 63 2d 3e 7a 2c 20 26 72 65 61 6c 6e 75 6d 2c 20  c->z, &realnum, 
1c0bc 70 52 65 63 2d 3e 65 6e 63 29 20 29 7b 0a 20 20  pRec->enc) ){.  
1c0bd 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20      i64 value;. 
1c0be 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1c0bf 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70  ChangeEncoding(p
1c0c0 52 65 63 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Rec, SQLITE_UTF8
1c0c1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 72 65  );.      if( !re
1c0c2 61 6c 6e 75 6d 20 26 26 20 73 71 6c 69 74 65 33  alnum && sqlite3
1c0c3 41 74 6f 69 36 34 28 70 52 65 63 2d 3e 7a 2c 20  Atoi64(pRec->z, 
1c0c4 26 76 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 20  &value) ){.     
1c0c5 20 20 20 70 52 65 63 2d 3e 75 2e 69 20 3d 20 76     pRec->u.i = v
1c0c6 61 6c 75 65 3b 0a 20 20 20 20 20 20 20 20 4d 65  alue;.        Me
1c0c7 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 52 65  mSetTypeFlag(pRe
1c0c8 63 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20  c, MEM_Int);.   
1c0c9 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1c0ca 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1c0cb 52 65 61 6c 69 66 79 28 70 52 65 63 29 3b 0a 20  Realify(pRec);. 
1c0cc 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
1c0cd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73  .}../*.** Proces
1c0ce 73 69 6e 67 20 69 73 20 64 65 74 65 72 6d 69 6e  sing is determin
1c0cf 65 20 62 79 20 74 68 65 20 61 66 66 69 6e 69 74  e by the affinit
1c0d0 79 20 70 61 72 61 6d 65 74 65 72 3a 0a 2a 2a 0a  y parameter:.**.
1c0d1 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e  ** SQLITE_AFF_IN
1c0d2 54 45 47 45 52 3a 0a 2a 2a 20 53 51 4c 49 54 45  TEGER:.** SQLITE
1c0d3 5f 41 46 46 5f 52 45 41 4c 3a 0a 2a 2a 20 53 51  _AFF_REAL:.** SQ
1c0d4 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
1c0d5 3a 0a 2a 2a 20 20 20 20 54 72 79 20 74 6f 20 63  :.**    Try to c
1c0d6 6f 6e 76 65 72 74 20 70 52 65 63 20 74 6f 20 61  onvert pRec to a
1c0d7 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 73  n integer repres
1c0d8 65 6e 74 61 74 69 6f 6e 20 6f 72 20 61 20 0a 2a  entation or a .*
1c0d9 2a 20 20 20 20 66 6c 6f 61 74 69 6e 67 2d 70 6f  *    floating-po
1c0da 69 6e 74 20 72 65 70 72 65 73 65 6e 74 61 74 69  int representati
1c0db 6f 6e 20 69 66 20 61 6e 20 69 6e 74 65 67 65 72  on if an integer
1c0dc 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a   representation.
1c0dd 2a 2a 20 20 20 20 69 73 20 6e 6f 74 20 70 6f 73  **    is not pos
1c0de 73 69 62 6c 65 2e 20 20 4e 6f 74 65 20 74 68 61  sible.  Note tha
1c0df 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 72 65  t the integer re
1c0e0 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 73 0a  presentation is.
1c0e1 2a 2a 20 20 20 20 61 6c 77 61 79 73 20 70 72 65  **    always pre
1c0e2 66 65 72 72 65 64 2c 20 65 76 65 6e 20 69 66 20  ferred, even if 
1c0e3 74 68 65 20 61 66 66 69 6e 69 74 79 20 69 73 20  the affinity is 
1c0e4 52 45 41 4c 2c 20 62 65 63 61 75 73 65 0a 2a 2a  REAL, because.**
1c0e5 20 20 20 20 61 6e 20 69 6e 74 65 67 65 72 20 72      an integer r
1c0e6 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 73  epresentation is
1c0e7 20 6d 6f 72 65 20 73 70 61 63 65 20 65 66 66 69   more space effi
1c0e8 63 69 65 6e 74 20 6f 6e 20 64 69 73 6b 2e 0a 2a  cient on disk..*
1c0e9 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f  *.** SQLITE_AFF_
1c0ea 54 45 58 54 3a 0a 2a 2a 20 20 20 20 43 6f 6e 76  TEXT:.**    Conv
1c0eb 65 72 74 20 70 52 65 63 20 74 6f 20 61 20 74 65  ert pRec to a te
1c0ec 78 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  xt representatio
1c0ed 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f  n..**.** SQLITE_
1c0ee 41 46 46 5f 4e 4f 4e 45 3a 0a 2a 2a 20 20 20 20  AFF_NONE:.**    
1c0ef 4e 6f 2d 6f 70 2e 20 20 70 52 65 63 20 69 73 20  No-op.  pRec is 
1c0f0 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74  unchanged..*/.st
1c0f1 61 74 69 63 20 76 6f 69 64 20 61 70 70 6c 79 41  atic void applyA
1c0f2 66 66 69 6e 69 74 79 28 0a 20 20 4d 65 6d 20 2a  ffinity(.  Mem *
1c0f3 70 52 65 63 2c 20 20 20 20 20 20 20 20 20 20 2f  pRec,          /
1c0f4 2a 20 54 68 65 20 76 61 6c 75 65 20 74 6f 20 61  * The value to a
1c0f5 70 70 6c 79 20 61 66 66 69 6e 69 74 79 20 74 6f  pply affinity to
1c0f6 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e   */.  char affin
1c0f7 69 74 79 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  ity,      /* The
1c0f8 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62 65 20   affinity to be 
1c0f9 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 75 38 20  applied */.  u8 
1c0fa 65 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20  enc             
1c0fb 20 2f 2a 20 55 73 65 20 74 68 69 73 20 74 65 78   /* Use this tex
1c0fc 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 29 7b  t encoding */.){
1c0fd 0a 20 20 69 66 28 20 61 66 66 69 6e 69 74 79 3d  .  if( affinity=
1c0fe 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54  =SQLITE_AFF_TEXT
1c0ff 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20   ){.    /* Only 
1c100 61 74 74 65 6d 70 74 20 74 68 65 20 63 6f 6e 76  attempt the conv
1c101 65 72 73 69 6f 6e 20 74 6f 20 54 45 58 54 20 69  ersion to TEXT i
1c102 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 69 6e  f there is an in
1c103 74 65 67 65 72 20 6f 72 20 72 65 61 6c 0a 20 20  teger or real.  
1c104 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74    ** representat
1c105 69 6f 6e 20 28 62 6c 6f 62 20 61 6e 64 20 4e 55  ion (blob and NU
1c106 4c 4c 20 64 6f 20 6e 6f 74 20 67 65 74 20 63 6f  LL do not get co
1c107 6e 76 65 72 74 65 64 29 20 62 75 74 20 6e 6f 20  nverted) but no 
1c108 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a 20 72 65  string.    ** re
1c109 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 20 20  presentation..  
1c10a 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 30 3d 3d    */.    if( 0==
1c10b 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d  (pRec->flags&MEM
1c10c 5f 53 74 72 29 20 26 26 20 28 70 52 65 63 2d 3e  _Str) && (pRec->
1c10d 66 6c 61 67 73 26 28 4d 45 4d 5f 52 65 61 6c 7c  flags&(MEM_Real|
1c10e 4d 45 4d 5f 49 6e 74 29 29 20 29 7b 0a 20 20 20  MEM_Int)) ){.   
1c10f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1c110 6d 53 74 72 69 6e 67 69 66 79 28 70 52 65 63 2c  mStringify(pRec,
1c111 20 65 6e 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20   enc);.    }.   
1c112 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 3d 20   pRec->flags &= 
1c113 7e 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49  ~(MEM_Real|MEM_I
1c114 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  nt);.  }else if(
1c115 20 61 66 66 69 6e 69 74 79 21 3d 53 51 4c 49 54   affinity!=SQLIT
1c116 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20  E_AFF_NONE ){.  
1c117 20 20 61 73 73 65 72 74 28 20 61 66 66 69 6e 69    assert( affini
1c118 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49  ty==SQLITE_AFF_I
1c119 4e 54 45 47 45 52 20 7c 7c 20 61 66 66 69 6e 69  NTEGER || affini
1c11a 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52  ty==SQLITE_AFF_R
1c11b 45 41 4c 0a 20 20 20 20 20 20 20 20 20 20 20 20  EAL.            
1c11c 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d 53 51   || affinity==SQ
1c11d 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
1c11e 20 29 3b 0a 20 20 20 20 61 70 70 6c 79 4e 75 6d   );.    applyNum
1c11f 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 52 65  ericAffinity(pRe
1c120 63 29 3b 0a 20 20 20 20 69 66 28 20 70 52 65 63  c);.    if( pRec
1c121 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65  ->flags & MEM_Re
1c122 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  al ){.      sqli
1c123 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 41 66  te3VdbeIntegerAf
1c124 66 69 6e 69 74 79 28 70 52 65 63 29 3b 0a 20 20  finity(pRec);.  
1c125 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
1c126 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20   Try to convert 
1c127 74 68 65 20 74 79 70 65 20 6f 66 20 61 20 66 75  the type of a fu
1c128 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20  nction argument 
1c129 6f 72 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75  or a result colu
1c12a 6d 6e 0a 2a 2a 20 69 6e 74 6f 20 61 20 6e 75 6d  mn.** into a num
1c12b 65 72 69 63 20 72 65 70 72 65 73 65 6e 74 61 74  eric representat
1c12c 69 6f 6e 2e 20 20 55 73 65 20 65 69 74 68 65 72  ion.  Use either
1c12d 20 49 4e 54 45 47 45 52 20 6f 72 20 52 45 41 4c   INTEGER or REAL
1c12e 20 77 68 69 63 68 65 76 65 72 0a 2a 2a 20 69 73   whichever.** is
1c12f 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20 20 42   appropriate.  B
1c130 75 74 20 6f 6e 6c 79 20 64 6f 20 74 68 65 20 63  ut only do the c
1c131 6f 6e 76 65 72 73 69 6f 6e 20 69 66 20 69 74 20  onversion if it 
1c132 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74 68  is possible with
1c133 6f 75 74 0a 2a 2a 20 6c 6f 73 73 20 6f 66 20 69  out.** loss of i
1c134 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 20 72  nformation and r
1c135 65 74 75 72 6e 20 74 68 65 20 72 65 76 69 73 65  eturn the revise
1c136 64 20 74 79 70 65 20 6f 66 20 74 68 65 20 61 72  d type of the ar
1c137 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  gument..**.** Th
1c138 69 73 20 69 73 20 61 6e 20 45 58 50 45 52 49 4d  is is an EXPERIM
1c139 45 4e 54 41 4c 20 61 70 69 20 61 6e 64 20 69 73  ENTAL api and is
1c13a 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e   subject to chan
1c13b 67 65 20 6f 72 20 72 65 6d 6f 76 61 6c 2e 0a 2a  ge or removal..*
1c13c 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1c13d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e   sqlite3_value_n
1c13e 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69  umeric_type(sqli
1c13f 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29  te3_value *pVal)
1c140 7b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20  {.  Mem *pMem = 
1c141 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 61 70  (Mem*)pVal;.  ap
1c142 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69  plyNumericAffini
1c143 74 79 28 70 4d 65 6d 29 3b 0a 20 20 73 74 6f 72  ty(pMem);.  stor
1c144 65 54 79 70 65 49 6e 66 6f 28 70 4d 65 6d 2c 20  eTypeInfo(pMem, 
1c145 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4d 65  0);.  return pMe
1c146 6d 2d 3e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  m->type;.}../*.*
1c147 2a 20 45 78 70 6f 72 74 65 64 20 76 65 72 73 69  * Exported versi
1c148 6f 6e 20 6f 66 20 61 70 70 6c 79 41 66 66 69 6e  on of applyAffin
1c149 69 74 79 28 29 2e 20 54 68 69 73 20 6f 6e 65 20  ity(). This one 
1c14a 77 6f 72 6b 73 20 6f 6e 20 73 71 6c 69 74 65 33  works on sqlite3
1c14b 5f 76 61 6c 75 65 2a 2c 20 0a 2a 2a 20 6e 6f 74  _value*, .** not
1c14c 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 4d 65   the internal Me
1c14d 6d 2a 20 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49  m* type..*/.SQLI
1c14e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1c14f 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c  sqlite3ValueAppl
1c150 79 41 66 66 69 6e 69 74 79 28 0a 20 20 73 71 6c  yAffinity(.  sql
1c151 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
1c152 2c 20 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79  , .  u8 affinity
1c153 2c 20 0a 20 20 75 38 20 65 6e 63 0a 29 7b 0a 20  , .  u8 enc.){. 
1c154 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 28   applyAffinity((
1c155 4d 65 6d 20 2a 29 70 56 61 6c 2c 20 61 66 66 69  Mem *)pVal, affi
1c156 6e 69 74 79 2c 20 65 6e 63 29 3b 0a 7d 0a 0a 23  nity, enc);.}..#
1c157 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
1c158 55 47 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61  UG./*.** Write a
1c159 20 6e 69 63 65 20 73 74 72 69 6e 67 20 72 65 70   nice string rep
1c15a 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
1c15b 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 63  he contents of c
1c15c 65 6c 6c 20 70 4d 65 6d 0a 2a 2a 20 69 6e 74 6f  ell pMem.** into
1c15d 20 62 75 66 66 65 72 20 7a 42 75 66 2c 20 6c 65   buffer zBuf, le
1c15e 6e 67 74 68 20 6e 42 75 66 2e 0a 2a 2f 0a 53 51  ngth nBuf..*/.SQ
1c15f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1c160 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
1c161 50 72 65 74 74 79 50 72 69 6e 74 28 4d 65 6d 20  PrettyPrint(Mem 
1c162 2a 70 4d 65 6d 2c 20 63 68 61 72 20 2a 7a 42 75  *pMem, char *zBu
1c163 66 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43 73 72  f){.  char *zCsr
1c164 20 3d 20 7a 42 75 66 3b 0a 20 20 69 6e 74 20 66   = zBuf;.  int f
1c165 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a   = pMem->flags;.
1c166 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
1c167 63 68 61 72 20 2a 63 6f 6e 73 74 20 65 6e 63 6e  char *const encn
1c168 61 6d 65 73 5b 5d 20 3d 20 7b 22 28 58 29 22 2c  ames[] = {"(X)",
1c169 20 22 28 38 29 22 2c 20 22 28 31 36 4c 45 29 22   "(8)", "(16LE)"
1c16a 2c 20 22 28 31 36 42 45 29 22 7d 3b 0a 0a 20 20  , "(16BE)"};..  
1c16b 69 66 28 20 66 26 4d 45 4d 5f 42 6c 6f 62 20 29  if( f&MEM_Blob )
1c16c 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  {.    int i;.   
1c16d 20 63 68 61 72 20 63 3b 0a 20 20 20 20 69 66 28   char c;.    if(
1c16e 20 66 20 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a   f & MEM_Dyn ){.
1c16f 20 20 20 20 20 20 63 20 3d 20 27 7a 27 3b 0a 20        c = 'z';. 
1c170 20 20 20 20 20 61 73 73 65 72 74 28 20 28 66 20       assert( (f 
1c171 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45  & (MEM_Static|ME
1c172 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a  M_Ephem))==0 );.
1c173 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20      }else if( f 
1c174 26 20 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b 0a  & MEM_Static ){.
1c175 20 20 20 20 20 20 63 20 3d 20 27 74 27 3b 0a 20        c = 't';. 
1c176 20 20 20 20 20 61 73 73 65 72 74 28 20 28 66 20       assert( (f 
1c177 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45  & (MEM_Dyn|MEM_E
1c178 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20  phem))==0 );.   
1c179 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d   }else if( f & M
1c17a 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 20 20 20  EM_Ephem ){.    
1c17b 20 20 63 20 3d 20 27 65 27 3b 0a 20 20 20 20 20    c = 'e';.     
1c17c 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d   assert( (f & (M
1c17d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79  EM_Static|MEM_Dy
1c17e 6e 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65  n))==0 );.    }e
1c17f 6c 73 65 7b 0a 20 20 20 20 20 20 63 20 3d 20 27  lse{.      c = '
1c180 73 27 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73  s';.    }..    s
1c181 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1c182 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 63 22 2c  100, zCsr, "%c",
1c183 20 63 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d   c);.    zCsr +=
1c184 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
1c185 28 7a 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69  (zCsr);.    sqli
1c186 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30  te3_snprintf(100
1c187 2c 20 7a 43 73 72 2c 20 22 25 64 5b 22 2c 20 70  , zCsr, "%d[", p
1c188 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20 7a 43 73  Mem->n);.    zCs
1c189 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  r += sqlite3Strl
1c18a 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20  en30(zCsr);.    
1c18b 66 6f 72 28 69 3d 30 3b 20 69 3c 31 36 20 26 26  for(i=0; i<16 &&
1c18c 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29   i<pMem->n; i++)
1c18d 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
1c18e 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43  snprintf(100, zC
1c18f 73 72 2c 20 22 25 30 32 58 22 2c 20 28 28 69 6e  sr, "%02X", ((in
1c190 74 29 70 4d 65 6d 2d 3e 7a 5b 69 5d 20 26 20 30  t)pMem->z[i] & 0
1c191 78 46 46 29 29 3b 0a 20 20 20 20 20 20 7a 43 73  xFF));.      zCs
1c192 72 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  r += sqlite3Strl
1c193 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20  en30(zCsr);.    
1c194 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  }.    for(i=0; i
1c195 3c 31 36 20 26 26 20 69 3c 70 4d 65 6d 2d 3e 6e  <16 && i<pMem->n
1c196 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68  ; i++){.      ch
1c197 61 72 20 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 69  ar z = pMem->z[i
1c198 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 3c 33  ];.      if( z<3
1c199 32 20 7c 7c 20 7a 3e 31 32 36 20 29 20 2a 7a 43  2 || z>126 ) *zC
1c19a 73 72 2b 2b 20 3d 20 27 2e 27 3b 0a 20 20 20 20  sr++ = '.';.    
1c19b 20 20 65 6c 73 65 20 2a 7a 43 73 72 2b 2b 20 3d    else *zCsr++ =
1c19c 20 7a 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73   z;.    }..    s
1c19d 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1c19e 31 30 30 2c 20 7a 43 73 72 2c 20 22 5d 25 73 22  100, zCsr, "]%s"
1c19f 2c 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d  , encnames[pMem-
1c1a0 3e 65 6e 63 5d 29 3b 0a 20 20 20 20 7a 43 73 72  >enc]);.    zCsr
1c1a1 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65   += sqlite3Strle
1c1a2 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 69  n30(zCsr);.    i
1c1a3 66 28 20 66 20 26 20 4d 45 4d 5f 5a 65 72 6f 20  f( f & MEM_Zero 
1c1a4 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1c1a5 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a  _snprintf(100, z
1c1a6 43 73 72 2c 22 2b 25 64 7a 22 2c 70 4d 65 6d 2d  Csr,"+%dz",pMem-
1c1a7 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 20  >u.nZero);.     
1c1a8 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33   zCsr += sqlite3
1c1a9 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a  Strlen30(zCsr);.
1c1aa 20 20 20 20 7d 0a 20 20 20 20 2a 7a 43 73 72 20      }.    *zCsr 
1c1ab 3d 20 27 5c 30 27 3b 0a 20 20 7d 65 6c 73 65 20  = '\0';.  }else 
1c1ac 69 66 28 20 66 20 26 20 4d 45 4d 5f 53 74 72 20  if( f & MEM_Str 
1c1ad 29 7b 0a 20 20 20 20 69 6e 74 20 6a 2c 20 6b 3b  ){.    int j, k;
1c1ae 0a 20 20 20 20 7a 42 75 66 5b 30 5d 20 3d 20 27  .    zBuf[0] = '
1c1af 20 27 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20   ';.    if( f & 
1c1b0 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20  MEM_Dyn ){.     
1c1b1 20 7a 42 75 66 5b 31 5d 20 3d 20 27 7a 27 3b 0a   zBuf[1] = 'z';.
1c1b2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 66        assert( (f
1c1b3 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d   & (MEM_Static|M
1c1b4 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b  EM_Ephem))==0 );
1c1b5 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66  .    }else if( f
1c1b6 20 26 20 4d 45 4d 5f 53 74 61 74 69 63 20 29 7b   & MEM_Static ){
1c1b7 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d  .      zBuf[1] =
1c1b8 20 27 74 27 3b 0a 20 20 20 20 20 20 61 73 73 65   't';.      asse
1c1b9 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 44 79  rt( (f & (MEM_Dy
1c1ba 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30  n|MEM_Ephem))==0
1c1bb 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   );.    }else if
1c1bc 28 20 66 20 26 20 4d 45 4d 5f 45 70 68 65 6d 20  ( f & MEM_Ephem 
1c1bd 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d  ){.      zBuf[1]
1c1be 20 3d 20 27 65 27 3b 0a 20 20 20 20 20 20 61 73   = 'e';.      as
1c1bf 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f  sert( (f & (MEM_
1c1c0 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29  Static|MEM_Dyn))
1c1c1 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ==0 );.    }else
1c1c2 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20  {.      zBuf[1] 
1c1c3 3d 20 27 73 27 3b 0a 20 20 20 20 7d 0a 20 20 20  = 's';.    }.   
1c1c4 20 6b 20 3d 20 32 3b 0a 20 20 20 20 73 71 6c 69   k = 2;.    sqli
1c1c5 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30  te3_snprintf(100
1c1c6 2c 20 26 7a 42 75 66 5b 6b 5d 2c 20 22 25 64 22  , &zBuf[k], "%d"
1c1c7 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20  , pMem->n);.    
1c1c8 6b 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  k += sqlite3Strl
1c1c9 65 6e 33 30 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a  en30(&zBuf[k]);.
1c1ca 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20      zBuf[k++] = 
1c1cb 27 5b 27 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30  '[';.    for(j=0
1c1cc 3b 20 6a 3c 31 35 20 26 26 20 6a 3c 70 4d 65 6d  ; j<15 && j<pMem
1c1cd 2d 3e 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  ->n; j++){.     
1c1ce 20 75 38 20 63 20 3d 20 70 4d 65 6d 2d 3e 7a 5b   u8 c = pMem->z[
1c1cf 6a 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3e  j];.      if( c>
1c1d0 3d 30 78 32 30 20 26 26 20 63 3c 30 78 37 66 20  =0x20 && c<0x7f 
1c1d1 29 7b 0a 20 20 20 20 20 20 20 20 7a 42 75 66 5b  ){.        zBuf[
1c1d2 6b 2b 2b 5d 20 3d 20 63 3b 0a 20 20 20 20 20 20  k++] = c;.      
1c1d3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a  }else{.        z
1c1d4 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 2e 27 3b 0a  Buf[k++] = '.';.
1c1d5 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1c1d6 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5d    zBuf[k++] = ']
1c1d7 27 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ';.    sqlite3_s
1c1d8 6e 70 72 69 6e 74 66 28 31 30 30 2c 26 7a 42 75  nprintf(100,&zBu
1c1d9 66 5b 6b 5d 2c 20 65 6e 63 6e 61 6d 65 73 5b 70  f[k], encnames[p
1c1da 4d 65 6d 2d 3e 65 6e 63 5d 29 3b 0a 20 20 20 20  Mem->enc]);.    
1c1db 6b 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  k += sqlite3Strl
1c1dc 65 6e 33 30 28 26 7a 42 75 66 5b 6b 5d 29 3b 0a  en30(&zBuf[k]);.
1c1dd 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20      zBuf[k++] = 
1c1de 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a  0;.  }.}.#endif.
1c1df 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
1c1e0 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74  EBUG./*.** Print
1c1e1 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20   the value of a 
1c1e2 72 65 67 69 73 74 65 72 20 66 6f 72 20 74 72 61  register for tra
1c1e3 63 69 6e 67 20 70 75 72 70 6f 73 65 73 3a 0a 2a  cing purposes:.*
1c1e4 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65  /.static void me
1c1e5 6d 54 72 61 63 65 50 72 69 6e 74 28 46 49 4c 45  mTracePrint(FILE
1c1e6 20 2a 6f 75 74 2c 20 4d 65 6d 20 2a 70 29 7b 0a   *out, Mem *p){.
1c1e7 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26    if( p->flags &
1c1e8 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20   MEM_Null ){.   
1c1e9 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20   fprintf(out, " 
1c1ea 4e 55 4c 4c 22 29 3b 0a 20 20 7d 65 6c 73 65 20  NULL");.  }else 
1c1eb 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20  if( (p->flags & 
1c1ec 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72  (MEM_Int|MEM_Str
1c1ed 29 29 3d 3d 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d  ))==(MEM_Int|MEM
1c1ee 5f 53 74 72 29 20 29 7b 0a 20 20 20 20 66 70 72  _Str) ){.    fpr
1c1ef 69 6e 74 66 28 6f 75 74 2c 20 22 20 73 69 3a 25  intf(out, " si:%
1c1f0 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a 20  lld", p->u.i);. 
1c1f1 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c   }else if( p->fl
1c1f2 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b  ags & MEM_Int ){
1c1f3 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74  .    fprintf(out
1c1f4 2c 20 22 20 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e  , " i:%lld", p->
1c1f5 75 2e 69 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  u.i);.#ifndef SQ
1c1f6 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
1c1f7 4e 47 5f 50 4f 49 4e 54 0a 20 20 7d 65 6c 73 65  NG_POINT.  }else
1c1f8 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20   if( p->flags & 
1c1f9 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20  MEM_Real ){.    
1c1fa 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 72  fprintf(out, " r
1c1fb 3a 25 67 22 2c 20 70 2d 3e 72 29 3b 0a 23 65 6e  :%g", p->r);.#en
1c1fc 64 69 66 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  dif.  }else if( 
1c1fd 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  p->flags & MEM_R
1c1fe 6f 77 53 65 74 20 29 7b 0a 20 20 20 20 66 70 72  owSet ){.    fpr
1c1ff 69 6e 74 66 28 6f 75 74 2c 20 22 20 28 72 6f 77  intf(out, " (row
1c200 73 65 74 29 22 29 3b 0a 20 20 7d 65 6c 73 65 7b  set)");.  }else{
1c201 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 32  .    char zBuf[2
1c202 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  00];.    sqlite3
1c203 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69  VdbeMemPrettyPri
1c204 6e 74 28 70 2c 20 7a 42 75 66 29 3b 0a 20 20 20  nt(p, zBuf);.   
1c205 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20   fprintf(out, " 
1c206 22 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28  ");.    fprintf(
1c207 6f 75 74 2c 20 22 25 73 22 2c 20 7a 42 75 66 29  out, "%s", zBuf)
1c208 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76  ;.  }.}.static v
1c209 6f 69 64 20 72 65 67 69 73 74 65 72 54 72 61 63  oid registerTrac
1c20a 65 28 46 49 4c 45 20 2a 6f 75 74 2c 20 69 6e 74  e(FILE *out, int
1c20b 20 69 52 65 67 2c 20 4d 65 6d 20 2a 70 29 7b 0a   iReg, Mem *p){.
1c20c 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
1c20d 52 45 47 5b 25 64 5d 20 3d 20 22 2c 20 69 52 65  REG[%d] = ", iRe
1c20e 67 29 3b 0a 20 20 6d 65 6d 54 72 61 63 65 50 72  g);.  memTracePr
1c20f 69 6e 74 28 6f 75 74 2c 20 70 29 3b 0a 20 20 66  int(out, p);.  f
1c210 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22  printf(out, "\n"
1c211 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  );.}.#endif..#if
1c212 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
1c213 0a 23 20 20 64 65 66 69 6e 65 20 52 45 47 49 53  .#  define REGIS
1c214 54 45 52 5f 54 52 41 43 45 28 52 2c 4d 29 20 69  TER_TRACE(R,M) i
1c215 66 28 70 2d 3e 74 72 61 63 65 29 72 65 67 69 73  f(p->trace)regis
1c216 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63  terTrace(p->trac
1c217 65 2c 52 2c 4d 29 0a 23 65 6c 73 65 0a 23 20 20  e,R,M).#else.#  
1c218 64 65 66 69 6e 65 20 52 45 47 49 53 54 45 52 5f  define REGISTER_
1c219 54 52 41 43 45 28 52 2c 4d 29 0a 23 65 6e 64 69  TRACE(R,M).#endi
1c21a 66 0a 0a 0a 23 69 66 64 65 66 20 56 44 42 45 5f  f...#ifdef VDBE_
1c21b 50 52 4f 46 49 4c 45 0a 0a 2f 2a 20 0a 2a 2a 20  PROFILE../* .** 
1c21c 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e  hwtime.h contain
1c21d 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c  s inline assembl
1c21e 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c  er code for impl
1c21f 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67  ementing .** hig
1c220 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69  h-performance ti
1c221 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a  ming routines..*
1c222 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
1c223 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65  * Include hwtime
1c224 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
1c225 20 6f 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a   of vdbe.c *****
1c226 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c227 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
1c228 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74  * Begin file hwt
1c229 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ime.h **********
1c22a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c22b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c22c 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79  /./*.** 2008 May
1c22d 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   27.**.** The au
1c22e 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
1c22f 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
1c230 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
1c231 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
1c232 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
1c233 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
1c234 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
1c235 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
1c236 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
1c237 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
1c238 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
1c239 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
1c23a 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
1c23b 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
1c23c 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
1c23d 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
1c23e 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
1c23f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c241 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c242 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c243 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
1c244 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
1c245 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65   inline asm code
1c246 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20   for retrieving 
1c247 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63  "high-performanc
1c248 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66  e".** counters f
1c249 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 50 55  or x86 class CPU
1c24a 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77  s..**.** $Id: hw
1c24b 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 30  time.h,v 1.3 200
1c24c 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 35  8/08/01 14:33:15
1c24d 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a   shane Exp $.*/.
1c24e 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f  #ifndef _HWTIME_
1c24f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49  H_.#define _HWTI
1c250 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ME_H_../*.** The
1c251 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
1c252 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e  ne only works on
1c253 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28   pentium-class (
1c254 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73  or newer) proces
1c255 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73  sors..** It uses
1c256 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64   the RDTSC opcod
1c257 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79  e to read the cy
1c258 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20  cle count value 
1c259 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72  out of the.** pr
1c25a 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75  ocessor and retu
1c25b 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20  rns that value. 
1c25c 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65   This can be use
1c25d 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a  d for high-res.*
1c25e 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a  * profiling..*/.
1c25f 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47  #if (defined(__G
1c260 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65  NUC__) || define
1c261 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20  d(_MSC_VER)) && 
1c262 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64  \.      (defined
1c263 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65  (i386) || define
1c264 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64  d(__i386__) || d
1c265 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29  efined(_M_IX86))
1c266 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 28  ..  #if defined(
1c267 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69  __GNUC__)..  __i
1c268 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75  nline__ sqlite_u
1c269 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74  int64 sqlite3Hwt
1c26a 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20  ime(void){.     
1c26b 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c  unsigned int lo,
1c26c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f   hi;.     __asm_
1c26d 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
1c26e 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28  "rdtsc" : "=a" (
1c26f 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b  lo), "=d" (hi));
1c270 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71  .     return (sq
1c271 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c  lite_uint64)hi <
1c272 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a  < 32 | lo;.  }..
1c273 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28    #elif defined(
1c274 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64  _MSC_VER)..  __d
1c275 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f  eclspec(naked) _
1c276 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75  _inline sqlite_u
1c277 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71  int64 __cdecl sq
1c278 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
1c279 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a  ){.     __asm {.
1c27a 20 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 20          rdtsc.  
1c27b 20 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20        ret       
1c27c 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61  ; return value a
1c27d 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d  t EDX:EAX.     }
1c27e 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a  .  }..  #endif..
1c27f 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f  #elif (defined(_
1c280 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69  _GNUC__) && defi
1c281 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29  ned(__x86_64__))
1c282 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73  ..  __inline__ s
1c283 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
1c284 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
1c285 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64  {.      unsigned
1c286 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20   long val;.     
1c287 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74   __asm__ __volat
1c288 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a  ile__ ("rdtsc" :
1c289 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20   "=A" (val));.  
1c28a 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a      return val;.
1c28b 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66    }. .#elif (def
1c28c 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26  ined(__GNUC__) &
1c28d 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f  & defined(__ppc_
1c28e 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f  _))..  __inline_
1c28f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  _ sqlite_uint64 
1c290 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f  sqlite3Hwtime(vo
1c291 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67  id){.      unsig
1c292 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65  ned long long re
1c293 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69  tval;.      unsi
1c294 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a  gned long junk;.
1c295 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f        __asm__ __
1c296 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c  volatile__ ("\n\
1c297 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 20  .          1:   
1c298 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c     mftbu   %1\n\
1c299 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1c29a 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e     mftb    %L0\n
1c29b 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
1c29c 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e      mftbu   %0\n
1c29d 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
1c29e 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25      cmpw    %0,%
1c29f 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20  1\n\.           
1c2a0 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 31         bne     1
1c2a1 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  b".             
1c2a2 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74       : "=r" (ret
1c2a3 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b  val), "=r" (junk
1c2a4 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ));.      return
1c2a5 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65   retval;.  }..#e
1c2a6 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65  lse..  #error Ne
1c2a7 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ed implementatio
1c2a8 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69  n of sqlite3Hwti
1c2a9 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c  me() for your pl
1c2aa 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20  atform...  /*.  
1c2ab 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69  ** To compile wi
1c2ac 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69  thout implementi
1c2ad 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  ng sqlite3Hwtime
1c2ae 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74  () for your plat
1c2af 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63  form,.  ** you c
1c2b0 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62  an remove the ab
1c2b1 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75  ove #error and u
1c2b2 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  se the following
1c2b3 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74  .  ** stub funct
1c2b4 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c  ion.  You will l
1c2b5 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f  ose timing suppo
1c2b6 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a  rt for many.  **
1c2b7 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 6e   of the debuggin
1c2b8 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74  g and testing ut
1c2b9 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 20  ilities, but it 
1c2ba 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c  should at.  ** l
1c2bb 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64  east compile and
1c2bc 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54   run..  */.SQLIT
1c2bd 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69  E_PRIVATE   sqli
1c2be 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
1c2bf 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72  3Hwtime(void){ r
1c2c0 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75  eturn ((sqlite_u
1c2c1 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e  int64)0); }..#en
1c2c2 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21  dif..#endif /* !
1c2c3 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f  defined(_HWTIME_
1c2c4 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  H_) */../*******
1c2c5 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68  ******* End of h
1c2c6 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  wtime.h ********
1c2c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c2c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c2c9 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
1c2ca 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
1c2cb 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
1c2cc 20 6f 66 66 20 69 6e 20 76 64 62 65 2e 63 20 2a   off in vdbe.c *
1c2cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c2ce 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 65 6e 64 69 66 0a  ******/..#endif.
1c2cf 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 45 43 4b  ./*.** The CHECK
1c2d0 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 20 6d  _FOR_INTERRUPT m
1c2d1 61 63 72 6f 20 64 65 66 69 6e 65 64 20 68 65 72  acro defined her
1c2d2 65 20 6c 6f 6f 6b 73 20 74 6f 20 73 65 65 20 69  e looks to see i
1c2d3 66 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33  f the.** sqlite3
1c2d4 5f 69 6e 74 65 72 72 75 70 74 28 29 20 72 6f 75  _interrupt() rou
1c2d5 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20 63 61  tine has been ca
1c2d6 6c 6c 65 64 2e 20 20 49 66 20 69 74 20 68 61 73  lled.  If it has
1c2d7 20 62 65 65 6e 2c 20 74 68 65 6e 0a 2a 2a 20 70   been, then.** p
1c2d8 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 74 68 65  rocessing of the
1c2d9 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 69 73   VDBE program is
1c2da 20 69 6e 74 65 72 72 75 70 74 65 64 2e 0a 2a 2a   interrupted..**
1c2db 0a 2a 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 61  .** This macro a
1c2dc 64 64 65 64 20 74 6f 20 65 76 65 72 79 20 69 6e  dded to every in
1c2dd 73 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 64  struction that d
1c2de 6f 65 73 20 61 20 6a 75 6d 70 20 69 6e 20 6f 72  oes a jump in or
1c2df 64 65 72 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d  der to.** implem
1c2e0 65 6e 74 20 61 20 6c 6f 6f 70 2e 20 20 54 68 69  ent a loop.  Thi
1c2e1 73 20 74 65 73 74 20 75 73 65 64 20 74 6f 20 62  s test used to b
1c2e2 65 20 6f 6e 20 65 76 65 72 79 20 73 69 6e 67 6c  e on every singl
1c2e3 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c 0a 2a  e instruction,.*
1c2e4 2a 20 62 75 74 20 74 68 61 74 20 6d 65 61 6e 74  * but that meant
1c2e5 20 77 65 20 6d 6f 72 65 20 74 65 73 74 69 6e 67   we more testing
1c2e6 20 74 68 61 74 20 77 65 20 6e 65 65 64 65 64 2e   that we needed.
1c2e7 20 20 42 79 20 6f 6e 6c 79 20 74 65 73 74 69 6e    By only testin
1c2e8 67 20 74 68 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e  g the.** flag on
1c2e9 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f   jump instructio
1c2ea 6e 73 2c 20 77 65 20 67 65 74 20 61 20 28 73 6d  ns, we get a (sm
1c2eb 61 6c 6c 29 20 73 70 65 65 64 20 69 6d 70 72 6f  all) speed impro
1c2ec 76 65 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69  vement..*/.#defi
1c2ed 6e 65 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54  ne CHECK_FOR_INT
1c2ee 45 52 52 55 50 54 20 5c 0a 20 20 20 69 66 28 20  ERRUPT \.   if( 
1c2ef 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75  db->u1.isInterru
1c2f0 70 74 65 64 20 29 20 67 6f 74 6f 20 61 62 6f 72  pted ) goto abor
1c2f1 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72 72 75  t_due_to_interru
1c2f2 70 74 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  pt;..#ifdef SQLI
1c2f3 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 20  TE_DEBUG.static 
1c2f4 69 6e 74 20 66 69 6c 65 45 78 69 73 74 73 28 73  int fileExists(s
1c2f5 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73  qlite3 *db, cons
1c2f6 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 29 7b 0a  t char *zFile){.
1c2f7 20 20 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a 20    int res = 0;. 
1c2f8 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
1c2f9 5f 4f 4b 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  _OK;.#ifdef SQLI
1c2fa 54 45 5f 54 45 53 54 0a 20 20 2f 2a 20 49 66 20  TE_TEST.  /* If 
1c2fb 77 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79  we are currently
1c2fc 20 74 65 73 74 69 6e 67 20 49 4f 20 65 72 72 6f   testing IO erro
1c2fd 72 73 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20  rs, then do not 
1c2fe 63 61 6c 6c 20 4f 73 41 63 63 65 73 73 28 29 20  call OsAccess() 
1c2ff 74 6f 0a 20 20 2a 2a 20 74 65 73 74 20 66 6f 72  to.  ** test for
1c300 20 74 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66   the presence of
1c301 20 7a 46 69 6c 65 2e 20 54 68 69 73 20 69 73 20   zFile. This is 
1c302 62 65 63 61 75 73 65 20 61 6e 79 20 49 4f 20 65  because any IO e
1c303 72 72 6f 72 20 74 68 61 74 0a 20 20 2a 2a 20 6f  rror that.  ** o
1c304 63 63 75 72 73 20 68 65 72 65 20 77 69 6c 6c 20  ccurs here will 
1c305 6e 6f 74 20 62 65 20 72 65 70 6f 72 74 65 64 2c  not be reported,
1c306 20 63 61 75 73 69 6e 67 20 74 68 65 20 74 65 73   causing the tes
1c307 74 20 74 6f 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a  t to fail..  */.
1c308 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c    extern int sql
1c309 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
1c30a 6e 64 69 6e 67 3b 0a 20 20 69 66 28 20 73 71 6c  nding;.  if( sql
1c30b 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
1c30c 6e 64 69 6e 67 3c 3d 30 20 29 0a 23 65 6e 64 69  nding<=0 ).#endi
1c30d 66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  f.    rc = sqlit
1c30e 65 33 4f 73 41 63 63 65 73 73 28 64 62 2d 3e 70  e3OsAccess(db->p
1c30f 56 66 73 2c 20 7a 46 69 6c 65 2c 20 53 51 4c 49  Vfs, zFile, SQLI
1c310 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53  TE_ACCESS_EXISTS
1c311 2c 20 26 72 65 73 29 3b 0a 20 20 72 65 74 75 72  , &res);.  retur
1c312 6e 20 28 72 65 73 20 26 26 20 72 63 3d 3d 53 51  n (res && rc==SQ
1c313 4c 49 54 45 5f 4f 4b 29 3b 0a 7d 0a 23 65 6e 64  LITE_OK);.}.#end
1c314 69 66 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  if..#ifndef NDEB
1c315 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  UG./*.** This fu
1c316 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 63  nction is only c
1c317 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69  alled from withi
1c318 6e 20 61 6e 20 61 73 73 65 72 74 28 29 20 65 78  n an assert() ex
1c319 70 72 65 73 73 69 6f 6e 2e 20 49 74 0a 2a 2a 20  pression. It.** 
1c31a 63 68 65 63 6b 73 20 74 68 61 74 20 74 68 65 20  checks that the 
1c31b 73 71 6c 69 74 65 33 2e 6e 54 72 61 6e 73 61 63  sqlite3.nTransac
1c31c 74 69 6f 6e 20 76 61 72 69 61 62 6c 65 20 69 73  tion variable is
1c31d 20 63 6f 72 72 65 63 74 6c 79 20 73 65 74 20 74   correctly set t
1c31e 6f 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20  o.** the number 
1c31f 6f 66 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69  of non-transacti
1c320 6f 6e 20 73 61 76 65 70 6f 69 6e 74 73 20 63 75  on savepoints cu
1c321 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 0a  rrently in the .
1c322 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73  ** linked list s
1c323 74 61 72 74 69 6e 67 20 61 74 20 73 71 6c 69 74  tarting at sqlit
1c324 65 33 2e 70 53 61 76 65 70 6f 69 6e 74 2e 0a 2a  e3.pSavepoint..*
1c325 2a 20 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a 0a  * .** Usage:.**.
1c326 2a 2a 20 20 20 20 20 61 73 73 65 72 74 28 20 63  **     assert( c
1c327 68 65 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75  heckSavepointCou
1c328 6e 74 28 64 62 29 20 29 3b 0a 2a 2f 0a 73 74 61  nt(db) );.*/.sta
1c329 74 69 63 20 69 6e 74 20 63 68 65 63 6b 53 61 76  tic int checkSav
1c32a 65 70 6f 69 6e 74 43 6f 75 6e 74 28 73 71 6c 69  epointCount(sqli
1c32b 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20  te3 *db){.  int 
1c32c 6e 20 3d 20 30 3b 0a 20 20 53 61 76 65 70 6f 69  n = 0;.  Savepoi
1c32d 6e 74 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 64  nt *p;.  for(p=d
1c32e 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 20 70  b->pSavepoint; p
1c32f 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 20 6e 2b  ; p=p->pNext) n+
1c330 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d  +;.  assert( n==
1c331 28 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20  (db->nSavepoint 
1c332 2b 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74  + db->isTransact
1c333 69 6f 6e 53 61 76 65 70 6f 69 6e 74 29 20 29 3b  ionSavepoint) );
1c334 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23  .  return 1;.}.#
1c335 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 65  endif../*.** Exe
1c336 63 75 74 65 20 61 73 20 6d 75 63 68 20 6f 66 20  cute as much of 
1c337 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 61  a VDBE program a
1c338 73 20 77 65 20 63 61 6e 20 74 68 65 6e 20 72 65  s we can then re
1c339 74 75 72 6e 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69  turn..**.** sqli
1c33a 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79  te3VdbeMakeReady
1c33b 28 29 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65  () must be calle
1c33c 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f  d before this ro
1c33d 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74  utine in order t
1c33e 6f 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 70  o.** close the p
1c33f 72 6f 67 72 61 6d 20 77 69 74 68 20 61 20 66 69  rogram with a fi
1c340 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 61 6e 64 20  nal OP_Halt and 
1c341 74 6f 20 73 65 74 20 75 70 20 74 68 65 20 63 61  to set up the ca
1c342 6c 6c 62 61 63 6b 73 0a 2a 2a 20 61 6e 64 20 74  llbacks.** and t
1c343 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
1c344 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
1c345 57 68 65 6e 65 76 65 72 20 61 20 72 6f 77 20 6f  Whenever a row o
1c346 72 20 72 65 73 75 6c 74 20 64 61 74 61 20 69 73  r result data is
1c347 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 69 73   available, this
1c348 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65 69   routine will ei
1c349 74 68 65 72 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74  ther.** invoke t
1c34a 68 65 20 72 65 73 75 6c 74 20 63 61 6c 6c 62 61  he result callba
1c34b 63 6b 20 28 69 66 20 74 68 65 72 65 20 69 73 20  ck (if there is 
1c34c 6f 6e 65 29 20 6f 72 20 72 65 74 75 72 6e 20 77  one) or return w
1c34d 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f 52 4f  ith.** SQLITE_RO
1c34e 57 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 61  W..**.** If an a
1c34f 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74  ttempt is made t
1c350 6f 20 6f 70 65 6e 20 61 20 6c 6f 63 6b 65 64 20  o open a locked 
1c351 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 74  database, then t
1c352 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77  his routine.** w
1c353 69 6c 6c 20 65 69 74 68 65 72 20 69 6e 76 6f 6b  ill either invok
1c354 65 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62  e the busy callb
1c355 61 63 6b 20 28 69 66 20 74 68 65 72 65 20 69 73  ack (if there is
1c356 20 6f 6e 65 29 20 6f 72 20 69 74 20 77 69 6c 6c   one) or it will
1c357 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54  .** return SQLIT
1c358 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20 49 66  E_BUSY..**.** If
1c359 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
1c35a 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  , an error messa
1c35b 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f  ge is written to
1c35c 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
1c35d 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  .** from sqlite3
1c35e 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 2d  _malloc() and p-
1c35f 3e 7a 45 72 72 4d 73 67 20 69 73 20 6d 61 64 65  >zErrMsg is made
1c360 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61   to point to tha
1c361 74 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54 68 65  t memory..** The
1c362 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73   error code is s
1c363 74 6f 72 65 64 20 69 6e 20 70 2d 3e 72 63 20 61  tored in p->rc a
1c364 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  nd this routine 
1c365 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 45  returns SQLITE_E
1c366 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  RROR..**.** If t
1c367 68 65 20 63 61 6c 6c 62 61 63 6b 20 65 76 65 72  he callback ever
1c368 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
1c369 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f 67  o, then the prog
1c36a 72 61 6d 20 65 78 69 74 73 0a 2a 2a 20 69 6d 6d  ram exits.** imm
1c36b 65 64 69 61 74 65 6c 79 2e 20 20 54 68 65 72 65  ediately.  There
1c36c 20 77 69 6c 6c 20 62 65 20 6e 6f 20 65 72 72 6f   will be no erro
1c36d 72 20 6d 65 73 73 61 67 65 20 62 75 74 20 74 68  r message but th
1c36e 65 20 70 2d 3e 72 63 20 66 69 65 6c 64 20 69 73  e p->rc field is
1c36f 0a 2a 2a 20 73 65 74 20 74 6f 20 53 51 4c 49 54  .** set to SQLIT
1c370 45 5f 41 42 4f 52 54 20 61 6e 64 20 74 68 69 73  E_ABORT and this
1c371 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65   routine will re
1c372 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
1c373 52 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72  R..**.** A memor
1c374 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  y allocation err
1c375 6f 72 20 63 61 75 73 65 73 20 70 2d 3e 72 63 20  or causes p->rc 
1c376 74 6f 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c  to be set to SQL
1c377 49 54 45 5f 4e 4f 4d 45 4d 20 61 6e 64 20 74 68  ITE_NOMEM and th
1c378 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f  is.** routine to
1c379 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
1c37a 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65  RROR..**.** Othe
1c37b 72 20 66 61 74 61 6c 20 65 72 72 6f 72 73 20 72  r fatal errors r
1c37c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
1c37d 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20  OR..**.** After 
1c37e 74 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73  this routine has
1c37f 20 66 69 6e 69 73 68 65 64 2c 20 73 71 6c 69 74   finished, sqlit
1c380 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 29  e3VdbeFinalize()
1c381 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 75 73   should be.** us
1c382 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 74  ed to clean up t
1c383 68 65 20 6d 65 73 73 20 74 68 61 74 20 77 61 73  he mess that was
1c384 20 6c 65 66 74 20 62 65 68 69 6e 64 2e 0a 2a 2f   left behind..*/
1c385 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1c386 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 45  int sqlite3VdbeE
1c387 78 65 63 28 0a 20 20 56 64 62 65 20 2a 70 20 20  xec(.  Vdbe *p  
1c388 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c389 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f    /* The VDBE */
1c38a 0a 29 7b 0a 20 20 69 6e 74 20 70 63 3b 20 20 20  .){.  int pc;   
1c38b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c38c 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 6d 20   /* The program 
1c38d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20  counter */.  Op 
1c38e 2a 70 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20  *pOp;           
1c38f 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
1c390 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a  nt operation */.
1c391 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1c392 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 2f 2a 20  E_OK;        /* 
1c393 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20  Value to return 
1c394 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
1c395 20 3d 20 70 2d 3e 64 62 3b 20 20 20 20 20 20 20   = p->db;       
1c396 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
1c397 2a 2f 0a 20 20 75 38 20 65 6e 63 6f 64 69 6e 67  */.  u8 encoding
1c398 20 3d 20 45 4e 43 28 64 62 29 3b 20 20 20 20 20   = ENC(db);     
1c399 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
1c39a 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 4d 65  encoding */.  Me
1c39b 6d 20 2a 70 49 6e 31 20 3d 20 30 3b 20 20 20 20  m *pIn1 = 0;    
1c39c 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73 74 20           /* 1st 
1c39d 69 6e 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f  input operand */
1c39e 0a 20 20 4d 65 6d 20 2a 70 49 6e 32 20 3d 20 30  .  Mem *pIn2 = 0
1c39f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1c3a0 20 32 6e 64 20 69 6e 70 75 74 20 6f 70 65 72 61   2nd input opera
1c3a1 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e  nd */.  Mem *pIn
1c3a2 33 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  3 = 0;          
1c3a3 20 20 20 2f 2a 20 33 72 64 20 69 6e 70 75 74 20     /* 3rd input 
1c3a4 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d  operand */.  Mem
1c3a5 20 2a 70 4f 75 74 20 3d 20 30 3b 20 20 20 20 20   *pOut = 0;     
1c3a6 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75          /* Outpu
1c3a7 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 75  t operand */.  u
1c3a8 38 20 6f 70 50 72 6f 70 65 72 74 79 3b 0a 20 20  8 opProperty;.  
1c3a9 69 6e 74 20 69 43 6f 6d 70 61 72 65 20 3d 20 30  int iCompare = 0
1c3aa 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  ;          /* Re
1c3ab 73 75 6c 74 20 6f 66 20 6c 61 73 74 20 4f 50 5f  sult of last OP_
1c3ac 43 6f 6d 70 61 72 65 20 6f 70 65 72 61 74 69 6f  Compare operatio
1c3ad 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 50 65 72  n */.  int *aPer
1c3ae 6d 75 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20  mute = 0;       
1c3af 20 20 2f 2a 20 50 65 72 6d 75 74 61 74 69 6f 6e    /* Permutation
1c3b0 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20   of columns for 
1c3b1 4f 50 5f 43 6f 6d 70 61 72 65 20 2a 2f 0a 23 69  OP_Compare */.#i
1c3b2 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c  fdef VDBE_PROFIL
1c3b3 45 0a 20 20 75 36 34 20 73 74 61 72 74 3b 20 20  E.  u64 start;  
1c3b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1c3b5 2a 20 43 50 55 20 63 6c 6f 63 6b 20 63 6f 75 6e  * CPU clock coun
1c3b6 74 20 61 74 20 73 74 61 72 74 20 6f 66 20 6f 70  t at start of op
1c3b7 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72  code */.  int or
1c3b8 69 67 50 63 3b 20 20 20 20 20 20 20 20 20 20 20  igPc;           
1c3b9 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20       /* Program 
1c3ba 63 6f 75 6e 74 65 72 20 61 74 20 73 74 61 72 74  counter at start
1c3bb 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f 0a 23 65   of opcode */.#e
1c3bc 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c  ndif.#ifndef SQL
1c3bd 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53  ITE_OMIT_PROGRES
1c3be 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 69 6e 74  S_CALLBACK.  int
1c3bf 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20   nProgressOps = 
1c3c0 30 3b 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64  0;      /* Opcod
1c3c1 65 73 20 65 78 65 63 75 74 65 64 20 73 69 6e 63  es executed sinc
1c3c2 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62  e progress callb
1c3c3 61 63 6b 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  ack. */.#endif. 
1c3c4 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   /**************
1c3c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c3c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c3c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c3c8 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 41 75 74 6f  ******.  ** Auto
1c3c9 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61  matically genera
1c3ca 74 65 64 20 63 6f 64 65 0a 20 20 2a 2a 0a 20 20  ted code.  **.  
1c3cb 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
1c3cc 20 75 6e 69 6f 6e 20 69 73 20 61 75 74 6f 6d 61   union is automa
1c3cd 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65  tically generate
1c3ce 64 20 62 79 20 74 68 65 0a 20 20 2a 2a 20 76 64  d by the.  ** vd
1c3cf 62 65 2d 63 6f 6d 70 72 65 73 73 2e 74 63 6c 20  be-compress.tcl 
1c3d0 73 63 72 69 70 74 2e 20 20 54 68 65 20 70 75 72  script.  The pur
1c3d1 70 6f 73 65 20 6f 66 20 74 68 69 73 20 75 6e 69  pose of this uni
1c3d2 6f 6e 20 69 73 20 74 6f 0a 20 20 2a 2a 20 72 65  on is to.  ** re
1c3d3 64 75 63 65 20 74 68 65 20 61 6d 6f 75 6e 74 20  duce the amount 
1c3d4 6f 66 20 73 74 61 63 6b 20 73 70 61 63 65 20 72  of stack space r
1c3d5 65 71 75 69 72 65 64 20 62 79 20 74 68 69 73 20  equired by this 
1c3d6 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53  function..  ** S
1c3d7 65 65 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 74  ee comments in t
1c3d8 68 65 20 76 64 62 65 2d 63 6f 6d 70 72 65 73 73  he vdbe-compress
1c3d9 2e 74 63 6c 20 73 63 72 69 70 74 20 66 6f 72 20  .tcl script for 
1c3da 64 65 74 61 69 6c 73 2e 0a 20 20 2a 2f 0a 20 20  details..  */.  
1c3db 75 6e 69 6f 6e 20 76 64 62 65 45 78 65 63 55 6e  union vdbeExecUn
1c3dc 69 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75 63 74  ion {.    struct
1c3dd 20 4f 50 5f 59 69 65 6c 64 5f 73 74 61 63 6b 5f   OP_Yield_stack_
1c3de 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74  vars {.      int
1c3df 20 70 63 44 65 73 74 3b 0a 20 20 20 20 7d 20 61   pcDest;.    } a
1c3e0 61 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  a;.    struct OP
1c3e1 5f 56 61 72 69 61 62 6c 65 5f 73 74 61 63 6b 5f  _Variable_stack_
1c3e2 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74  vars {.      int
1c3e3 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 2f 2a   p1;          /*
1c3e4 20 56 61 72 69 61 62 6c 65 20 74 6f 20 63 6f 70   Variable to cop
1c3e5 79 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20 20 20  y from */.      
1c3e6 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20 20  int p2;         
1c3e7 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20   /* Register to 
1c3e8 63 6f 70 79 20 74 6f 20 2a 2f 0a 20 20 20 20 20  copy to */.     
1c3e9 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20   int n;         
1c3ea 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76    /* Number of v
1c3eb 61 6c 75 65 73 20 6c 65 66 74 20 74 6f 20 63 6f  alues left to co
1c3ec 70 79 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20  py */.      Mem 
1c3ed 2a 70 56 61 72 3b 20 20 20 20 20 20 20 2f 2a 20  *pVar;       /* 
1c3ee 56 61 6c 75 65 20 62 65 69 6e 67 20 74 72 61 6e  Value being tran
1c3ef 73 66 65 72 72 65 64 20 2a 2f 0a 20 20 20 20 7d  sferred */.    }
1c3f0 20 61 62 3b 0a 20 20 20 20 73 74 72 75 63 74 20   ab;.    struct 
1c3f1 4f 50 5f 4d 6f 76 65 5f 73 74 61 63 6b 5f 76 61  OP_Move_stack_va
1c3f2 72 73 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20  rs {.      char 
1c3f3 2a 7a 4d 61 6c 6c 6f 63 3b 20 20 20 2f 2a 20 48  *zMalloc;   /* H
1c3f4 6f 6c 64 69 6e 67 20 76 61 72 69 61 62 6c 65 20  olding variable 
1c3f5 66 6f 72 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65  for allocated me
1c3f6 6d 6f 72 79 20 2a 2f 0a 20 20 20 20 20 20 69 6e  mory */.      in
1c3f7 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f  t n;           /
1c3f8 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 67 69  * Number of regi
1c3f9 73 74 65 72 73 20 6c 65 66 74 20 74 6f 20 63 6f  sters left to co
1c3fa 70 79 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  py */.      int 
1c3fb 70 31 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p1;          /* 
1c3fc 52 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79  Register to copy
1c3fd 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20 20 20 69   from */.      i
1c3fe 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20 20 20  nt p2;          
1c3ff 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 63  /* Register to c
1c400 6f 70 79 20 74 6f 20 2a 2f 0a 20 20 20 20 7d 20  opy to */.    } 
1c401 61 63 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  ac;.    struct O
1c402 50 5f 52 65 73 75 6c 74 52 6f 77 5f 73 74 61 63  P_ResultRow_stac
1c403 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 4d  k_vars {.      M
1c404 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 20 20 20  em *pMem;.      
1c405 69 6e 74 20 69 3b 0a 20 20 20 20 7d 20 61 64 3b  int i;.    } ad;
1c406 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 43  .    struct OP_C
1c407 6f 6e 63 61 74 5f 73 74 61 63 6b 5f 76 61 72 73  oncat_stack_vars
1c408 20 7b 0a 20 20 20 20 20 20 69 36 34 20 6e 42 79   {.      i64 nBy
1c409 74 65 3b 0a 20 20 20 20 7d 20 61 65 3b 0a 20 20  te;.    } ae;.  
1c40a 20 20 73 74 72 75 63 74 20 4f 50 5f 52 65 6d 61    struct OP_Rema
1c40b 69 6e 64 65 72 5f 73 74 61 63 6b 5f 76 61 72 73  inder_stack_vars
1c40c 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 66 6c 61   {.      int fla
1c40d 67 73 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62  gs;      /* Comb
1c40e 69 6e 65 64 20 4d 45 4d 5f 2a 20 66 6c 61 67 73  ined MEM_* flags
1c40f 20 66 72 6f 6d 20 62 6f 74 68 20 69 6e 70 75 74   from both input
1c410 73 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 69  s */.      i64 i
1c411 41 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  A;         /* In
1c412 74 65 67 65 72 20 76 61 6c 75 65 20 6f 66 20 6c  teger value of l
1c413 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  eft operand */. 
1c414 20 20 20 20 20 69 36 34 20 69 42 3b 20 20 20 20       i64 iB;    
1c415 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20       /* Integer 
1c416 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74 20 6f  value of right o
1c417 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20  perand */.      
1c418 64 6f 75 62 6c 65 20 72 41 3b 20 20 20 20 20 20  double rA;      
1c419 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20 6f 66  /* Real value of
1c41a 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f   left operand */
1c41b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 42  .      double rB
1c41c 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76  ;      /* Real v
1c41d 61 6c 75 65 20 6f 66 20 72 69 67 68 74 20 6f 70  alue of right op
1c41e 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 7d 20 61  erand */.    } a
1c41f 66 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  f;.    struct OP
1c420 5f 46 75 6e 63 74 69 6f 6e 5f 73 74 61 63 6b 5f  _Function_stack_
1c421 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74  vars {.      int
1c422 20 69 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70   i;.      Mem *p
1c423 41 72 67 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Arg;.      sqlit
1c424 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a  e3_context ctx;.
1c425 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61        sqlite3_va
1c426 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 0a 20 20 20  lue **apVal;.   
1c427 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 7d 20     int n;.    } 
1c428 61 67 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  ag;.    struct O
1c429 50 5f 53 68 69 66 74 52 69 67 68 74 5f 73 74 61  P_ShiftRight_sta
1c42a 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c42b 69 36 34 20 61 3b 0a 20 20 20 20 20 20 69 36 34  i64 a;.      i64
1c42c 20 62 3b 0a 20 20 20 20 7d 20 61 68 3b 0a 20 20   b;.    } ah;.  
1c42d 20 20 73 74 72 75 63 74 20 4f 50 5f 47 65 5f 73    struct OP_Ge_s
1c42e 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c42f 20 20 69 6e 74 20 72 65 73 3b 20 20 20 20 20 20    int res;      
1c430 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20        /* Result 
1c431 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  of the compariso
1c432 6e 20 6f 66 20 70 49 6e 31 20 61 67 61 69 6e 73  n of pIn1 agains
1c433 74 20 70 49 6e 33 20 2a 2f 0a 20 20 20 20 20 20  t pIn3 */.      
1c434 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20 20  char affinity;  
1c435 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20      /* Affinity 
1c436 74 6f 20 75 73 65 20 66 6f 72 20 63 6f 6d 70 61  to use for compa
1c437 72 69 73 6f 6e 20 2a 2f 0a 20 20 20 20 7d 20 61  rison */.    } a
1c438 69 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  i;.    struct OP
1c439 5f 43 6f 6d 70 61 72 65 5f 73 74 61 63 6b 5f 76  _Compare_stack_v
1c43a 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  ars {.      int 
1c43b 6e 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  n;.      int i;.
1c43c 20 20 20 20 20 20 69 6e 74 20 70 31 3b 0a 20 20        int p1;.  
1c43d 20 20 20 20 69 6e 74 20 70 32 3b 0a 20 20 20 20      int p2;.    
1c43e 20 20 63 6f 6e 73 74 20 4b 65 79 49 6e 66 6f 20    const KeyInfo 
1c43f 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20  *pKeyInfo;.     
1c440 20 69 6e 74 20 69 64 78 3b 0a 20 20 20 20 20 20   int idx;.      
1c441 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20  CollSeq *pColl; 
1c442 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20     /* Collating 
1c443 73 65 71 75 65 6e 63 65 20 74 6f 20 75 73 65 20  sequence to use 
1c444 6f 6e 20 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a  on this term */.
1c445 20 20 20 20 20 20 69 6e 74 20 62 52 65 76 3b 20        int bRev; 
1c446 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
1c447 20 66 6f 72 20 44 45 53 43 45 4e 44 49 4e 47 20   for DESCENDING 
1c448 73 6f 72 74 20 6f 72 64 65 72 20 2a 2f 0a 20 20  sort order */.  
1c449 20 20 7d 20 61 6a 3b 0a 20 20 20 20 73 74 72 75    } aj;.    stru
1c44a 63 74 20 4f 50 5f 4f 72 5f 73 74 61 63 6b 5f 76  ct OP_Or_stack_v
1c44b 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  ars {.      int 
1c44c 76 31 3b 20 20 20 20 2f 2a 20 4c 65 66 74 20 6f  v1;    /* Left o
1c44d 70 65 72 61 6e 64 3a 20 20 30 3d 3d 46 41 4c 53  perand:  0==FALS
1c44e 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 3d 3d 55  E, 1==TRUE, 2==U
1c44f 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a  NKNOWN or NULL *
1c450 2f 0a 20 20 20 20 20 20 69 6e 74 20 76 32 3b 20  /.      int v2; 
1c451 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72     /* Right oper
1c452 61 6e 64 3a 20 30 3d 3d 46 41 4c 53 45 2c 20 31  and: 0==FALSE, 1
1c453 3d 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f  ==TRUE, 2==UNKNO
1c454 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20  WN or NULL */.  
1c455 20 20 7d 20 61 6b 3b 0a 20 20 20 20 73 74 72 75    } ak;.    stru
1c456 63 74 20 4f 50 5f 49 66 4e 6f 74 5f 73 74 61 63  ct OP_IfNot_stac
1c457 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
1c458 6e 74 20 63 3b 0a 20 20 20 20 7d 20 61 6c 3b 0a  nt c;.    } al;.
1c459 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 43 6f      struct OP_Co
1c45a 6c 75 6d 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20  lumn_stack_vars 
1c45b 7b 0a 20 20 20 20 20 20 75 33 32 20 70 61 79 6c  {.      u32 payl
1c45c 6f 61 64 53 69 7a 65 3b 20 20 20 2f 2a 20 4e 75  oadSize;   /* Nu
1c45d 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
1c45e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20   the record */. 
1c45f 20 20 20 20 20 69 36 34 20 70 61 79 6c 6f 61 64       i64 payload
1c460 53 69 7a 65 36 34 3b 20 2f 2a 20 4e 75 6d 62 65  Size64; /* Numbe
1c461 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
1c462 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20  e record */.    
1c463 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20    int p1;       
1c464 20 20 20 20 20 2f 2a 20 50 31 20 76 61 6c 75 65       /* P1 value
1c465 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20 2a   of the opcode *
1c466 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b 20  /.      int p2; 
1c467 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f             /* co
1c468 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 74 6f 20 72  lumn number to r
1c469 65 74 72 69 65 76 65 20 2a 2f 0a 20 20 20 20 20  etrieve */.     
1c46a 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1c46b 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20      /* The VDBE 
1c46c 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 20 20  cursor */.      
1c46d 63 68 61 72 20 2a 7a 52 65 63 3b 20 20 20 20 20  char *zRec;     
1c46e 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
1c46f 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64   complete record
1c470 2d 64 61 74 61 20 2a 2f 0a 20 20 20 20 20 20 42  -data */.      B
1c471 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 20  tCursor *pCrsr; 
1c472 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 63    /* The BTree c
1c473 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 20 20 75  ursor */.      u
1c474 33 32 20 2a 61 54 79 70 65 3b 20 20 20 20 20 20  32 *aType;      
1c475 20 20 2f 2a 20 61 54 79 70 65 5b 69 5d 20 68 6f    /* aType[i] ho
1c476 6c 64 73 20 74 68 65 20 6e 75 6d 65 72 69 63 20  lds the numeric 
1c477 74 79 70 65 20 6f 66 20 74 68 65 20 69 2d 74 68  type of the i-th
1c478 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20   column */.     
1c479 20 75 33 32 20 2a 61 4f 66 66 73 65 74 3b 20 20   u32 *aOffset;  
1c47a 20 20 20 20 2f 2a 20 61 4f 66 66 73 65 74 5b 69      /* aOffset[i
1c47b 5d 20 69 73 20 6f 66 66 73 65 74 20 74 6f 20 73  ] is offset to s
1c47c 74 61 72 74 20 6f 66 20 64 61 74 61 20 66 6f 72  tart of data for
1c47d 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   i-th column */.
1c47e 20 20 20 20 20 20 69 6e 74 20 6e 46 69 65 6c 64        int nField
1c47f 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62  ;        /* numb
1c480 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20  er of fields in 
1c481 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
1c482 20 20 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20      int len;    
1c483 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 65         /* The le
1c484 6e 67 74 68 20 6f 66 20 74 68 65 20 73 65 72 69  ngth of the seri
1c485 61 6c 69 7a 65 64 20 64 61 74 61 20 66 6f 72 20  alized data for 
1c486 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  the column */.  
1c487 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20      int i;      
1c488 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
1c489 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20  ounter */.      
1c48a 63 68 61 72 20 2a 7a 44 61 74 61 3b 20 20 20 20  char *zData;    
1c48b 20 20 20 2f 2a 20 50 61 72 74 20 6f 66 20 74 68     /* Part of th
1c48c 65 20 72 65 63 6f 72 64 20 62 65 69 6e 67 20 64  e record being d
1c48d 65 63 6f 64 65 64 20 2a 2f 0a 20 20 20 20 20 20  ecoded */.      
1c48e 4d 65 6d 20 2a 70 44 65 73 74 3b 20 20 20 20 20  Mem *pDest;     
1c48f 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77     /* Where to w
1c490 72 69 74 65 20 74 68 65 20 65 78 74 72 61 63 74  rite the extract
1c491 65 64 20 76 61 6c 75 65 20 2a 2f 0a 20 20 20 20  ed value */.    
1c492 20 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20 20 20    Mem sMem;     
1c493 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 74 6f 72       /* For stor
1c494 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 62  ing the record b
1c495 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f 0a  eing decoded */.
1c496 20 20 20 20 20 20 75 38 20 2a 7a 49 64 78 3b 20        u8 *zIdx; 
1c497 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
1c498 78 20 69 6e 74 6f 20 68 65 61 64 65 72 20 2a 2f  x into header */
1c499 0a 20 20 20 20 20 20 75 38 20 2a 7a 45 6e 64 48  .      u8 *zEndH
1c49a 64 72 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  dr;       /* Poi
1c49b 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20 62 79  nter to first by
1c49c 74 65 20 61 66 74 65 72 20 74 68 65 20 68 65 61  te after the hea
1c49d 64 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 33 32  der */.      u32
1c49e 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20   offset;        
1c49f 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 74  /* Offset into t
1c4a0 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 20  he data */.     
1c4a1 20 75 36 34 20 6f 66 66 73 65 74 36 34 3b 20 20   u64 offset64;  
1c4a2 20 20 20 20 2f 2a 20 36 34 2d 62 69 74 20 6f 66      /* 64-bit of
1c4a3 66 73 65 74 2e 20 20 36 34 20 62 69 74 73 20 6e  fset.  64 bits n
1c4a4 65 65 64 65 64 20 74 6f 20 63 61 74 63 68 20 6f  eeded to catch o
1c4a5 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 20 20 20  verflow */.     
1c4a6 20 69 6e 74 20 73 7a 48 64 72 3b 20 20 20 20 20   int szHdr;     
1c4a7 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
1c4a8 68 65 20 68 65 61 64 65 72 20 73 69 7a 65 20 66  he header size f
1c4a9 69 65 6c 64 20 61 74 20 73 74 61 72 74 20 6f 66  ield at start of
1c4aa 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20   record */.     
1c4ab 20 69 6e 74 20 61 76 61 69 6c 3b 20 20 20 20 20   int avail;     
1c4ac 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1c4ad 20 62 79 74 65 73 20 6f 66 20 61 76 61 69 6c 61   bytes of availa
1c4ae 62 6c 65 20 64 61 74 61 20 2a 2f 0a 20 20 20 20  ble data */.    
1c4af 20 20 4d 65 6d 20 2a 70 52 65 67 3b 20 20 20 20    Mem *pReg;    
1c4b0 20 20 20 20 20 2f 2a 20 50 73 65 75 64 6f 54 61       /* PseudoTa
1c4b1 62 6c 65 20 69 6e 70 75 74 20 72 65 67 69 73 74  ble input regist
1c4b2 65 72 20 2a 2f 0a 20 20 20 20 7d 20 61 6d 3b 0a  er */.    } am;.
1c4b3 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 41 66      struct OP_Af
1c4b4 66 69 6e 69 74 79 5f 73 74 61 63 6b 5f 76 61 72  finity_stack_var
1c4b5 73 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a  s {.      char *
1c4b6 7a 41 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20  zAffinity;   /* 
1c4b7 54 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20  The affinity to 
1c4b8 62 65 20 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20  be applied */.  
1c4b9 20 20 20 20 4d 65 6d 20 2a 70 44 61 74 61 30 3b      Mem *pData0;
1c4ba 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
1c4bb 72 65 67 69 73 74 65 72 20 74 6f 20 77 68 69 63  register to whic
1c4bc 68 20 74 6f 20 61 70 70 6c 79 20 61 66 66 69 6e  h to apply affin
1c4bd 69 74 79 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d  ity */.      Mem
1c4be 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 20 20 20   *pLast;        
1c4bf 2f 2a 20 4c 61 73 74 20 72 65 67 69 73 74 65 72  /* Last register
1c4c0 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 70 70   to which to app
1c4c1 6c 79 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a 20  ly affinity */. 
1c4c2 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63 3b 20       Mem *pRec; 
1c4c3 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
1c4c4 6e 74 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20  nt register */. 
1c4c5 20 20 20 7d 20 61 6e 3b 0a 20 20 20 20 73 74 72     } an;.    str
1c4c6 75 63 74 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  uct OP_MakeRecor
1c4c7 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  d_stack_vars {. 
1c4c8 20 20 20 20 20 75 38 20 2a 7a 4e 65 77 52 65 63       u8 *zNewRec
1c4c9 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41  ord;        /* A
1c4ca 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20   buffer to hold 
1c4cb 74 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65  the data for the
1c4cc 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20   new record */. 
1c4cd 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63 3b 20       Mem *pRec; 
1c4ce 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1c4cf 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f  he new record */
1c4d0 0a 20 20 20 20 20 20 75 36 34 20 6e 44 61 74 61  .      u64 nData
1c4d1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1c4d2 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
1c4d3 20 6f 66 20 64 61 74 61 20 73 70 61 63 65 20 2a   of data space *
1c4d4 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 48 64 72  /.      int nHdr
1c4d5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1c4d6 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
1c4d7 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 61 63  s of header spac
1c4d8 65 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 6e  e */.      i64 n
1c4d9 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 20  Byte;           
1c4da 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20    /* Data space 
1c4db 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69  required for thi
1c4dc 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20  s record */.    
1c4dd 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20    int nZero;    
1c4de 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1c4df 65 72 20 6f 66 20 7a 65 72 6f 20 62 79 74 65 73  er of zero bytes
1c4e0 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   at the end of t
1c4e1 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20  he record */.   
1c4e2 20 20 20 69 6e 74 20 6e 56 61 72 69 6e 74 3b 20     int nVarint; 
1c4e3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1c4e4 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
1c4e5 61 20 76 61 72 69 6e 74 20 2a 2f 0a 20 20 20 20  a varint */.    
1c4e6 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70    u32 serial_typ
1c4e7 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65  e;       /* Type
1c4e8 20 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20 20 20   field */.      
1c4e9 4d 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20  Mem *pData0;    
1c4ea 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
1c4eb 66 69 65 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62  field to be comb
1c4ec 69 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65  ined into the re
1c4ed 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65  cord */.      Me
1c4ee 6d 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 20 20  m *pLast;       
1c4ef 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 66 69 65       /* Last fie
1c4f0 6c 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64  ld of the record
1c4f1 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 46   */.      int nF
1c4f2 69 65 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20  ield;           
1c4f3 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69   /* Number of fi
1c4f4 65 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f  elds in the reco
1c4f5 72 64 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72  rd */.      char
1c4f6 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 20 20   *zAffinity;    
1c4f7 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69     /* The affini
1c4f8 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68  ty string for th
1c4f9 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20  e record */.    
1c4fa 20 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61    int file_forma
1c4fb 74 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65  t;       /* File
1c4fc 20 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 20 66   format to use f
1c4fd 6f 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20  or encoding */. 
1c4fe 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20       int i;     
1c4ff 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1c500 70 61 63 65 20 75 73 65 64 20 69 6e 20 7a 4e 65  pace used in zNe
1c501 77 52 65 63 6f 72 64 5b 5d 20 2a 2f 0a 20 20 20  wRecord[] */.   
1c502 20 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20     int len;     
1c503 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
1c504 67 74 68 20 6f 66 20 61 20 66 69 65 6c 64 20 2a  gth of a field *
1c505 2f 0a 20 20 20 20 7d 20 61 6f 3b 0a 20 20 20 20  /.    } ao;.    
1c506 73 74 72 75 63 74 20 4f 50 5f 43 6f 75 6e 74 5f  struct OP_Count_
1c507 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c508 20 20 20 69 36 34 20 6e 45 6e 74 72 79 3b 0a 20     i64 nEntry;. 
1c509 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70       BtCursor *p
1c50a 43 72 73 72 3b 0a 20 20 20 20 7d 20 61 70 3b 0a  Crsr;.    } ap;.
1c50b 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 61      struct OP_Sa
1c50c 76 65 70 6f 69 6e 74 5f 73 74 61 63 6b 5f 76 61  vepoint_stack_va
1c50d 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 70  rs {.      int p
1c50e 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
1c50f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
1c510 6c 75 65 20 6f 66 20 50 31 20 6f 70 65 72 61 6e  lue of P1 operan
1c511 64 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20  d */.      char 
1c512 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *zName;         
1c513 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
1c514 6d 65 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 20  me of savepoint 
1c515 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 4e 61  */.      int nNa
1c516 6d 65 3b 0a 20 20 20 20 20 20 53 61 76 65 70 6f  me;.      Savepo
1c517 69 6e 74 20 2a 70 4e 65 77 3b 0a 20 20 20 20 20  int *pNew;.     
1c518 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76   Savepoint *pSav
1c519 65 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 53 61  epoint;.      Sa
1c51a 76 65 70 6f 69 6e 74 20 2a 70 54 6d 70 3b 0a 20  vepoint *pTmp;. 
1c51b 20 20 20 20 20 69 6e 74 20 69 53 61 76 65 70 6f       int iSavepo
1c51c 69 6e 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  int;.      int i
1c51d 69 3b 0a 20 20 20 20 7d 20 61 71 3b 0a 20 20 20  i;.    } aq;.   
1c51e 20 73 74 72 75 63 74 20 4f 50 5f 41 75 74 6f 43   struct OP_AutoC
1c51f 6f 6d 6d 69 74 5f 73 74 61 63 6b 5f 76 61 72 73  ommit_stack_vars
1c520 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 65 73   {.      int des
1c521 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b 0a  iredAutoCommit;.
1c522 20 20 20 20 20 20 69 6e 74 20 69 52 6f 6c 6c 62        int iRollb
1c523 61 63 6b 3b 0a 20 20 20 20 20 20 69 6e 74 20 74  ack;.      int t
1c524 75 72 6e 4f 6e 41 43 3b 0a 20 20 20 20 7d 20 61  urnOnAC;.    } a
1c525 72 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  r;.    struct OP
1c526 5f 54 72 61 6e 73 61 63 74 69 6f 6e 5f 73 74 61  _Transaction_sta
1c527 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c528 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20 20  Btree *pBt;.    
1c529 7d 20 61 73 3b 0a 20 20 20 20 73 74 72 75 63 74  } as;.    struct
1c52a 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 5f 73   OP_ReadCookie_s
1c52b 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c52c 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 20    int iMeta;.   
1c52d 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20     int iDb;.    
1c52e 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b 0a 20    int iCookie;. 
1c52f 20 20 20 7d 20 61 74 3b 0a 20 20 20 20 73 74 72     } at;.    str
1c530 75 63 74 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65  uct OP_SetCookie
1c531 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c532 20 20 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 20      Db *pDb;.   
1c533 20 7d 20 61 75 3b 0a 20 20 20 20 73 74 72 75 63   } au;.    struc
1c534 74 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69  t OP_VerifyCooki
1c535 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  e_stack_vars {. 
1c536 20 20 20 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a       int iMeta;.
1c537 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74        Btree *pBt
1c538 3b 0a 20 20 20 20 7d 20 61 76 3b 0a 20 20 20 20  ;.    } av;.    
1c539 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e 57 72  struct OP_OpenWr
1c53a 69 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ite_stack_vars {
1c53b 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69 65 6c  .      int nFiel
1c53c 64 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f  d;.      KeyInfo
1c53d 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20   *pKeyInfo;.    
1c53e 20 20 69 6e 74 20 70 32 3b 0a 20 20 20 20 20 20    int p2;.      
1c53f 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 20 69  int iDb;.      i
1c540 6e 74 20 77 72 46 6c 61 67 3b 0a 20 20 20 20 20  nt wrFlag;.     
1c541 20 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 20 20   Btree *pX;.    
1c542 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1c543 75 72 3b 0a 20 20 20 20 20 20 44 62 20 2a 70 44  ur;.      Db *pD
1c544 62 3b 0a 20 20 20 20 7d 20 61 77 3b 0a 20 20 20  b;.    } aw;.   
1c545 20 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e 45   struct OP_OpenE
1c546 70 68 65 6d 65 72 61 6c 5f 73 74 61 63 6b 5f 76  phemeral_stack_v
1c547 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65  ars {.      Vdbe
1c548 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 20  Cursor *pCx;.   
1c549 20 7d 20 61 78 3b 0a 20 20 20 20 73 74 72 75 63   } ax;.    struc
1c54a 74 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 5f  t OP_OpenPseudo_
1c54b 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c54c 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70     VdbeCursor *p
1c54d 43 78 3b 0a 20 20 20 20 7d 20 61 79 3b 0a 20 20  Cx;.    } ay;.  
1c54e 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65 65 6b    struct OP_Seek
1c54f 47 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  Gt_stack_vars {.
1c550 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20        int res;. 
1c551 20 20 20 20 20 69 6e 74 20 6f 63 3b 0a 20 20 20       int oc;.   
1c552 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70     VdbeCursor *p
1c553 43 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b 65  C;.      Unpacke
1c554 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 20 20  dRecord r;.     
1c555 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20 20   int nField;.   
1c556 20 20 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20     i64 iKey;    
1c557 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 20 77    /* The rowid w
1c558 65 20 61 72 65 20 74 6f 20 73 65 65 6b 20 74 6f  e are to seek to
1c559 20 2a 2f 0a 20 20 20 20 7d 20 61 7a 3b 0a 20 20   */.    } az;.  
1c55a 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65 65 6b    struct OP_Seek
1c55b 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c55c 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a      VdbeCursor *
1c55d 70 43 3b 0a 20 20 20 20 7d 20 62 61 3b 0a 20 20  pC;.    } ba;.  
1c55e 20 20 73 74 72 75 63 74 20 4f 50 5f 46 6f 75 6e    struct OP_Foun
1c55f 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  d_stack_vars {. 
1c560 20 20 20 20 20 69 6e 74 20 61 6c 72 65 61 64 79       int already
1c561 45 78 69 73 74 73 3b 0a 20 20 20 20 20 20 56 64  Exists;.      Vd
1c562 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
1c563 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
1c564 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72     UnpackedRecor
1c565 64 20 2a 70 49 64 78 4b 65 79 3b 0a 20 20 20 20  d *pIdxKey;.    
1c566 20 20 63 68 61 72 20 61 54 65 6d 70 52 65 63 5b    char aTempRec[
1c567 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e  ROUND8(sizeof(Un
1c568 70 61 63 6b 65 64 52 65 63 6f 72 64 29 29 20 2b  packedRecord)) +
1c569 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 33 20 2b   sizeof(Mem)*3 +
1c56a 20 37 5d 3b 0a 20 20 20 20 7d 20 62 62 3b 0a 20   7];.    } bb;. 
1c56b 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 73 55     struct OP_IsU
1c56c 6e 69 71 75 65 5f 73 74 61 63 6b 5f 76 61 72 73  nique_stack_vars
1c56d 20 7b 0a 20 20 20 20 20 20 75 31 36 20 69 69 3b   {.      u16 ii;
1c56e 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f  .      VdbeCurso
1c56f 72 20 2a 70 43 78 3b 0a 20 20 20 20 20 20 42 74  r *pCx;.      Bt
1c570 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20  Cursor *pCrsr;. 
1c571 20 20 20 20 20 75 31 36 20 6e 46 69 65 6c 64 3b       u16 nField;
1c572 0a 20 20 20 20 20 20 4d 65 6d 20 2a 61 4d 65 6d  .      Mem *aMem
1c573 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64  ;.      Unpacked
1c574 52 65 63 6f 72 64 20 72 3b 20 20 20 20 20 20 20  Record r;       
1c575 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 2d             /* B-
1c576 54 72 65 65 20 69 6e 64 65 78 20 73 65 61 72 63  Tree index searc
1c577 68 20 6b 65 79 20 2a 2f 0a 20 20 20 20 20 20 69  h key */.      i
1c578 36 34 20 52 3b 20 20 20 20 20 20 20 20 20 20 20  64 R;           
1c579 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c57a 20 20 2f 2a 20 52 6f 77 69 64 20 73 74 6f 72 65    /* Rowid store
1c57b 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  d in register P3
1c57c 20 2a 2f 0a 20 20 20 20 7d 20 62 63 3b 0a 20 20   */.    } bc;.  
1c57d 20 20 73 74 72 75 63 74 20 4f 50 5f 4e 6f 74 45    struct OP_NotE
1c57e 78 69 73 74 73 5f 73 74 61 63 6b 5f 76 61 72 73  xists_stack_vars
1c57f 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72   {.      VdbeCur
1c580 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42  sor *pC;.      B
1c581 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1c582 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20        int res;. 
1c583 20 20 20 20 20 75 36 34 20 69 4b 65 79 3b 0a 20       u64 iKey;. 
1c584 20 20 20 7d 20 62 64 3b 0a 20 20 20 20 73 74 72     } bd;.    str
1c585 75 63 74 20 4f 50 5f 4e 65 77 52 6f 77 69 64 5f  uct OP_NewRowid_
1c586 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c587 20 20 20 69 36 34 20 76 3b 20 20 20 20 20 20 20     i64 v;       
1c588 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1c589 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20   new rowid */.  
1c58a 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a      VdbeCursor *
1c58b 70 43 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75  pC;        /* Cu
1c58c 72 73 6f 72 20 6f 66 20 74 61 62 6c 65 20 74 6f  rsor of table to
1c58d 20 67 65 74 20 74 68 65 20 6e 65 77 20 72 6f 77   get the new row
1c58e 69 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  id */.      int 
1c58f 72 65 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  res;            
1c590 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20     /* Result of 
1c591 61 6e 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  an sqlite3BtreeL
1c592 61 73 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 69  ast() */.      i
1c593 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20  nt cnt;         
1c594 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72        /* Counter
1c595 20 74 6f 20 6c 69 6d 69 74 20 74 68 65 20 6e 75   to limit the nu
1c596 6d 62 65 72 20 6f 66 20 73 65 61 72 63 68 65 73  mber of searches
1c597 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70   */.      Mem *p
1c598 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Mem;            
1c599 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
1c59a 64 69 6e 67 20 6c 61 72 67 65 73 74 20 72 6f 77  ding largest row
1c59b 69 64 20 66 6f 72 20 41 55 54 4f 49 4e 43 52 45  id for AUTOINCRE
1c59c 4d 45 4e 54 20 2a 2f 0a 20 20 20 20 20 20 56 64  MENT */.      Vd
1c59d 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b  beFrame *pFrame;
1c59e 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 66 72 61       /* Root fra
1c59f 6d 65 20 6f 66 20 56 44 42 45 20 2a 2f 0a 20 20  me of VDBE */.  
1c5a0 20 20 7d 20 62 65 3b 0a 20 20 20 20 73 74 72 75    } be;.    stru
1c5a1 63 74 20 4f 50 5f 49 6e 73 65 72 74 5f 73 74 61  ct OP_Insert_sta
1c5a2 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c5a3 4d 65 6d 20 2a 70 44 61 74 61 3b 20 20 20 20 20  Mem *pData;     
1c5a4 20 20 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20 68 6f    /* MEM cell ho
1c5a5 6c 64 69 6e 67 20 64 61 74 61 20 66 6f 72 20 74  lding data for t
1c5a6 68 65 20 72 65 63 6f 72 64 20 74 6f 20 62 65 20  he record to be 
1c5a7 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 20 20  inserted */.    
1c5a8 20 20 4d 65 6d 20 2a 70 4b 65 79 3b 20 20 20 20    Mem *pKey;    
1c5a9 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20      /* MEM cell 
1c5aa 68 6f 6c 64 69 6e 67 20 6b 65 79 20 20 66 6f 72  holding key  for
1c5ab 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20   the record */. 
1c5ac 20 20 20 20 20 69 36 34 20 69 4b 65 79 3b 20 20       i64 iKey;  
1c5ad 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e         /* The in
1c5ae 74 65 67 65 72 20 52 4f 57 49 44 20 6f 72 20 6b  teger ROWID or k
1c5af 65 79 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72  ey for the recor
1c5b0 64 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64  d to be inserted
1c5b1 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 43 75   */.      VdbeCu
1c5b2 72 73 6f 72 20 2a 70 43 3b 20 20 20 2f 2a 20 43  rsor *pC;   /* C
1c5b3 75 72 73 6f 72 20 74 6f 20 74 61 62 6c 65 20 69  ursor to table i
1c5b4 6e 74 6f 20 77 68 69 63 68 20 69 6e 73 65 72 74  nto which insert
1c5b5 20 69 73 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20   is written */. 
1c5b6 20 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20       int nZero; 
1c5b7 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1c5b8 20 6f 66 20 7a 65 72 6f 2d 62 79 74 65 73 20 74   of zero-bytes t
1c5b9 6f 20 61 70 70 65 6e 64 20 2a 2f 0a 20 20 20 20  o append */.    
1c5ba 20 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c 74    int seekResult
1c5bb 3b 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66  ;   /* Result of
1c5bc 20 70 72 69 6f 72 20 73 65 65 6b 20 6f 72 20 30   prior seek or 0
1c5bd 20 69 66 20 6e 6f 20 55 53 45 53 45 45 4b 52 45   if no USESEEKRE
1c5be 53 55 4c 54 20 66 6c 61 67 20 2a 2f 0a 20 20 20  SULT flag */.   
1c5bf 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1c5c0 44 62 3b 20 20 2f 2a 20 64 61 74 61 62 61 73 65  Db;  /* database
1c5c1 20 6e 61 6d 65 20 2d 20 75 73 65 64 20 62 79 20   name - used by 
1c5c2 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20  the update hook 
1c5c3 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  */.      const c
1c5c4 68 61 72 20 2a 7a 54 62 6c 3b 20 2f 2a 20 54 61  har *zTbl; /* Ta
1c5c5 62 6c 65 20 6e 61 6d 65 20 2d 20 75 73 65 64 20  ble name - used 
1c5c6 62 79 20 74 68 65 20 6f 70 64 61 74 65 20 68 6f  by the opdate ho
1c5c7 6f 6b 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  ok */.      int 
1c5c8 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  op;           /*
1c5c9 20 4f 70 63 6f 64 65 20 66 6f 72 20 75 70 64 61   Opcode for upda
1c5ca 74 65 20 68 6f 6f 6b 3a 20 53 51 4c 49 54 45 5f  te hook: SQLITE_
1c5cb 55 50 44 41 54 45 20 6f 72 20 53 51 4c 49 54 45  UPDATE or SQLITE
1c5cc 5f 49 4e 53 45 52 54 20 2a 2f 0a 20 20 20 20 7d  _INSERT */.    }
1c5cd 20 62 66 3b 0a 20 20 20 20 73 74 72 75 63 74 20   bf;.    struct 
1c5ce 4f 50 5f 44 65 6c 65 74 65 5f 73 74 61 63 6b 5f  OP_Delete_stack_
1c5cf 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 36 34  vars {.      i64
1c5d0 20 69 4b 65 79 3b 0a 20 20 20 20 20 20 56 64 62   iKey;.      Vdb
1c5d1 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20  eCursor *pC;.   
1c5d2 20 7d 20 62 67 3b 0a 20 20 20 20 73 74 72 75 63   } bg;.    struc
1c5d3 74 20 4f 50 5f 52 6f 77 44 61 74 61 5f 73 74 61  t OP_RowData_sta
1c5d4 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c5d5 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1c5d6 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a        BtCursor *
1c5d7 70 43 72 73 72 3b 0a 20 20 20 20 20 20 75 33 32  pCrsr;.      u32
1c5d8 20 6e 3b 0a 20 20 20 20 20 20 69 36 34 20 6e 36   n;.      i64 n6
1c5d9 34 3b 0a 20 20 20 20 7d 20 62 68 3b 0a 20 20 20  4;.    } bh;.   
1c5da 20 73 74 72 75 63 74 20 4f 50 5f 52 6f 77 69 64   struct OP_Rowid
1c5db 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c5dc 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a      VdbeCursor *
1c5dd 70 43 3b 0a 20 20 20 20 20 20 69 36 34 20 76 3b  pC;.      i64 v;
1c5de 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76  .      sqlite3_v
1c5df 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20  tab *pVtab;.    
1c5e0 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
1c5e1 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b  module *pModule;
1c5e2 0a 20 20 20 20 7d 20 62 69 3b 0a 20 20 20 20 73  .    } bi;.    s
1c5e3 74 72 75 63 74 20 4f 50 5f 4e 75 6c 6c 52 6f 77  truct OP_NullRow
1c5e4 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c5e5 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a      VdbeCursor *
1c5e6 70 43 3b 0a 20 20 20 20 7d 20 62 6a 3b 0a 20 20  pC;.    } bj;.  
1c5e7 20 20 73 74 72 75 63 74 20 4f 50 5f 4c 61 73 74    struct OP_Last
1c5e8 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c5e9 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a      VdbeCursor *
1c5ea 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73  pC;.      BtCurs
1c5eb 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20  or *pCrsr;.     
1c5ec 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 7d 20   int res;.    } 
1c5ed 62 6b 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  bk;.    struct O
1c5ee 50 5f 52 65 77 69 6e 64 5f 73 74 61 63 6b 5f 76  P_Rewind_stack_v
1c5ef 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65  ars {.      Vdbe
1c5f0 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20  Cursor *pC;.    
1c5f1 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
1c5f2 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73  r;.      int res
1c5f3 3b 0a 20 20 20 20 7d 20 62 6c 3b 0a 20 20 20 20  ;.    } bl;.    
1c5f4 73 74 72 75 63 74 20 4f 50 5f 4e 65 78 74 5f 73  struct OP_Next_s
1c5f5 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c5f6 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1c5f7 3b 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72  ;.      BtCursor
1c5f8 20 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69   *pCrsr;.      i
1c5f9 6e 74 20 72 65 73 3b 0a 20 20 20 20 7d 20 62 6d  nt res;.    } bm
1c5fa 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c5fb 49 64 78 49 6e 73 65 72 74 5f 73 74 61 63 6b 5f  IdxInsert_stack_
1c5fc 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62  vars {.      Vdb
1c5fd 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20  eCursor *pC;.   
1c5fe 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72     BtCursor *pCr
1c5ff 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4b  sr;.      int nK
1c600 65 79 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  ey;.      const 
1c601 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 20 20 20 20  char *zKey;.    
1c602 7d 20 62 6e 3b 0a 20 20 20 20 73 74 72 75 63 74  } bn;.    struct
1c603 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 5f 73 74   OP_IdxDelete_st
1c604 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c605 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1c606 0a 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20  .      BtCursor 
1c607 2a 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e  *pCrsr;.      in
1c608 74 20 72 65 73 3b 0a 20 20 20 20 20 20 55 6e 70  t res;.      Unp
1c609 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20  ackedRecord r;. 
1c60a 20 20 20 7d 20 62 6f 3b 0a 20 20 20 20 73 74 72     } bo;.    str
1c60b 75 63 74 20 4f 50 5f 49 64 78 52 6f 77 69 64 5f  uct OP_IdxRowid_
1c60c 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c60d 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72     BtCursor *pCr
1c60e 73 72 3b 0a 20 20 20 20 20 20 56 64 62 65 43 75  sr;.      VdbeCu
1c60f 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20  rsor *pC;.      
1c610 69 36 34 20 72 6f 77 69 64 3b 0a 20 20 20 20 7d  i64 rowid;.    }
1c611 20 62 70 3b 0a 20 20 20 20 73 74 72 75 63 74 20   bp;.    struct 
1c612 4f 50 5f 49 64 78 47 45 5f 73 74 61 63 6b 5f 76  OP_IdxGE_stack_v
1c613 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65  ars {.      Vdbe
1c614 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20  Cursor *pC;.    
1c615 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20    int res;.     
1c616 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
1c617 72 3b 0a 20 20 20 20 7d 20 62 71 3b 0a 20 20 20  r;.    } bq;.   
1c618 20 73 74 72 75 63 74 20 4f 50 5f 44 65 73 74 72   struct OP_Destr
1c619 6f 79 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  oy_stack_vars {.
1c61a 20 20 20 20 20 20 69 6e 74 20 69 4d 6f 76 65 64        int iMoved
1c61b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6e 74  ;.      int iCnt
1c61c 3b 0a 20 20 20 20 20 20 56 64 62 65 20 2a 70 56  ;.      Vdbe *pV
1c61d 64 62 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  dbe;.      int i
1c61e 44 62 3b 0a 20 20 20 20 7d 20 62 72 3b 0a 20 20  Db;.    } br;.  
1c61f 20 20 73 74 72 75 63 74 20 4f 50 5f 43 6c 65 61    struct OP_Clea
1c620 72 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  r_stack_vars {. 
1c621 20 20 20 20 20 69 6e 74 20 6e 43 68 61 6e 67 65       int nChange
1c622 3b 0a 20 20 20 20 7d 20 62 73 3b 0a 20 20 20 20  ;.    } bs;.    
1c623 73 74 72 75 63 74 20 4f 50 5f 43 72 65 61 74 65  struct OP_Create
1c624 54 61 62 6c 65 5f 73 74 61 63 6b 5f 76 61 72 73  Table_stack_vars
1c625 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 67 6e   {.      int pgn
1c626 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 66 6c 61  o;.      int fla
1c627 67 73 3b 0a 20 20 20 20 20 20 44 62 20 2a 70 44  gs;.      Db *pD
1c628 62 3b 0a 20 20 20 20 7d 20 62 74 3b 0a 20 20 20  b;.    } bt;.   
1c629 20 73 74 72 75 63 74 20 4f 50 5f 50 61 72 73 65   struct OP_Parse
1c62a 53 63 68 65 6d 61 5f 73 74 61 63 6b 5f 76 61 72  Schema_stack_var
1c62b 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44  s {.      int iD
1c62c 62 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  b;.      const c
1c62d 68 61 72 20 2a 7a 4d 61 73 74 65 72 3b 0a 20 20  har *zMaster;.  
1c62e 20 20 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a      char *zSql;.
1c62f 20 20 20 20 20 20 49 6e 69 74 44 61 74 61 20 69        InitData i
1c630 6e 69 74 44 61 74 61 3b 0a 20 20 20 20 7d 20 62  nitData;.    } b
1c631 75 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  u;.    struct OP
1c632 5f 49 6e 74 65 67 72 69 74 79 43 6b 5f 73 74 61  _IntegrityCk_sta
1c633 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c634 69 6e 74 20 6e 52 6f 6f 74 3b 20 20 20 20 20 20  int nRoot;      
1c635 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 62  /* Number of tab
1c636 6c 65 73 20 74 6f 20 63 68 65 63 6b 2e 20 20 28  les to check.  (
1c637 4e 75 6d 62 65 72 20 6f 66 20 72 6f 6f 74 20 70  Number of root p
1c638 61 67 65 73 2e 29 20 2a 2f 0a 20 20 20 20 20 20  ages.) */.      
1c639 69 6e 74 20 2a 61 52 6f 6f 74 3b 20 20 20 20 20  int *aRoot;     
1c63a 2f 2a 20 41 72 72 61 79 20 6f 66 20 72 6f 6f 74  /* Array of root
1c63b 70 61 67 65 20 6e 75 6d 62 65 72 73 20 66 6f 72  page numbers for
1c63c 20 74 61 62 6c 65 73 20 74 6f 20 62 65 20 63 68   tables to be ch
1c63d 65 63 6b 65 64 20 2a 2f 0a 20 20 20 20 20 20 69  ecked */.      i
1c63e 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20 2f  nt j;          /
1c63f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
1c640 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 45 72 72  /.      int nErr
1c641 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
1c642 72 20 6f 66 20 65 72 72 6f 72 73 20 72 65 70 6f  r of errors repo
1c643 72 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 63 68  rted */.      ch
1c644 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 2f 2a  ar *z;        /*
1c645 20 54 65 78 74 20 6f 66 20 74 68 65 20 65 72 72   Text of the err
1c646 6f 72 20 72 65 70 6f 72 74 20 2a 2f 0a 20 20 20  or report */.   
1c647 20 20 20 4d 65 6d 20 2a 70 6e 45 72 72 3b 20 20     Mem *pnErr;  
1c648 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 6b     /* Register k
1c649 65 65 70 69 6e 67 20 74 72 61 63 6b 20 6f 66 20  eeping track of 
1c64a 65 72 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67  errors remaining
1c64b 20 2a 2f 0a 20 20 20 20 7d 20 62 76 3b 0a 20 20   */.    } bv;.  
1c64c 20 20 73 74 72 75 63 74 20 4f 50 5f 52 6f 77 53    struct OP_RowS
1c64d 65 74 41 64 64 5f 73 74 61 63 6b 5f 76 61 72 73  etAdd_stack_vars
1c64e 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 49   {.      Mem *pI
1c64f 64 78 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70  dx;.      Mem *p
1c650 56 61 6c 3b 0a 20 20 20 20 7d 20 62 77 3b 0a 20  Val;.    } bw;. 
1c651 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 6f 77     struct OP_Row
1c652 53 65 74 52 65 61 64 5f 73 74 61 63 6b 5f 76 61  SetRead_stack_va
1c653 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a  rs {.      Mem *
1c654 70 49 64 78 3b 0a 20 20 20 20 20 20 69 36 34 20  pIdx;.      i64 
1c655 76 61 6c 3b 0a 20 20 20 20 7d 20 62 78 3b 0a 20  val;.    } bx;. 
1c656 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 6f 77     struct OP_Row
1c657 53 65 74 54 65 73 74 5f 73 74 61 63 6b 5f 76 61  SetTest_stack_va
1c658 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  rs {.      int i
1c659 53 65 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 65  Set;.      int e
1c65a 78 69 73 74 73 3b 0a 20 20 20 20 7d 20 62 79 3b  xists;.    } by;
1c65b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 50  .    struct OP_P
1c65c 72 6f 67 72 61 6d 5f 73 74 61 63 6b 5f 76 61 72  rogram_stack_var
1c65d 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4d  s {.      int nM
1c65e 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  em;             
1c65f 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d    /* Number of m
1c660 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 20  emory registers 
1c661 66 6f 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20  for sub-program 
1c662 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 42 79  */.      int nBy
1c663 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  te;             
1c664 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 72 75 6e   /* Bytes of run
1c665 74 69 6d 65 20 73 70 61 63 65 20 72 65 71 75 69  time space requi
1c666 72 65 64 20 66 6f 72 20 73 75 62 2d 70 72 6f 67  red for sub-prog
1c667 72 61 6d 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d  ram */.      Mem
1c668 20 2a 70 52 74 3b 20 20 20 20 20 20 20 20 20 20   *pRt;          
1c669 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
1c66a 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 72 75 6e   to allocate run
1c66b 74 69 6d 65 20 73 70 61 63 65 20 2a 2f 0a 20 20  time space */.  
1c66c 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20      Mem *pMem;  
1c66d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55              /* U
1c66e 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 74  sed to iterate t
1c66f 68 72 6f 75 67 68 20 6d 65 6d 6f 72 79 20 63 65  hrough memory ce
1c670 6c 6c 73 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d  lls */.      Mem
1c671 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20 20   *pEnd;         
1c672 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 6d 65 6d       /* Last mem
1c673 6f 72 79 20 63 65 6c 6c 20 69 6e 20 6e 65 77 20  ory cell in new 
1c674 61 72 72 61 79 20 2a 2f 0a 20 20 20 20 20 20 56  array */.      V
1c675 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65  dbeFrame *pFrame
1c676 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 64  ;      /* New vd
1c677 62 65 20 66 72 61 6d 65 20 74 6f 20 65 78 65 63  be frame to exec
1c678 75 74 65 20 69 6e 20 2a 2f 0a 20 20 20 20 20 20  ute in */.      
1c679 53 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f  SubProgram *pPro
1c67a 67 72 61 6d 3b 20 20 20 2f 2a 20 53 75 62 2d 70  gram;   /* Sub-p
1c67b 72 6f 67 72 61 6d 20 74 6f 20 65 78 65 63 75 74  rogram to execut
1c67c 65 20 2a 2f 0a 20 20 20 20 20 20 76 6f 69 64 20  e */.      void 
1c67d 2a 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *t;             
1c67e 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 69 64 65 6e     /* Token iden
1c67f 74 69 66 79 69 6e 67 20 74 72 69 67 67 65 72 20  tifying trigger 
1c680 2a 2f 0a 20 20 20 20 7d 20 62 7a 3b 0a 20 20 20  */.    } bz;.   
1c681 20 73 74 72 75 63 74 20 4f 50 5f 50 61 72 61 6d   struct OP_Param
1c682 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c683 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70      VdbeFrame *p
1c684 46 72 61 6d 65 3b 0a 20 20 20 20 20 20 4d 65 6d  Frame;.      Mem
1c685 20 2a 70 49 6e 3b 0a 20 20 20 20 7d 20 63 61 3b   *pIn;.    } ca;
1c686 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4d  .    struct OP_M
1c687 65 6d 4d 61 78 5f 73 74 61 63 6b 5f 76 61 72 73  emMax_stack_vars
1c688 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 49   {.      Mem *pI
1c689 6e 31 3b 0a 20 20 20 20 20 20 56 64 62 65 46 72  n1;.      VdbeFr
1c68a 61 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 20 20 20  ame *pFrame;.   
1c68b 20 7d 20 63 62 3b 0a 20 20 20 20 73 74 72 75 63   } cb;.    struc
1c68c 74 20 4f 50 5f 41 67 67 53 74 65 70 5f 73 74 61  t OP_AggStep_sta
1c68d 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c68e 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20 69 6e 74  int n;.      int
1c68f 20 69 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70   i;.      Mem *p
1c690 4d 65 6d 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a  Mem;.      Mem *
1c691 70 52 65 63 3b 0a 20 20 20 20 20 20 73 71 6c 69  pRec;.      sqli
1c692 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b  te3_context ctx;
1c693 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76  .      sqlite3_v
1c694 61 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 0a 20 20  alue **apVal;.  
1c695 20 20 7d 20 63 63 3b 0a 20 20 20 20 73 74 72 75    } cc;.    stru
1c696 63 74 20 4f 50 5f 41 67 67 46 69 6e 61 6c 5f 73  ct OP_AggFinal_s
1c697 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c698 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20    Mem *pMem;.   
1c699 20 7d 20 63 64 3b 0a 20 20 20 20 73 74 72 75 63   } cd;.    struc
1c69a 74 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d 5f  t OP_IncrVacuum_
1c69b 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c69c 20 20 20 42 74 72 65 65 20 2a 70 42 74 3b 0a 20     Btree *pBt;. 
1c69d 20 20 20 7d 20 63 65 3b 0a 20 20 20 20 73 74 72     } ce;.    str
1c69e 75 63 74 20 4f 50 5f 56 42 65 67 69 6e 5f 73 74  uct OP_VBegin_st
1c69f 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c6a0 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62 3b 0a   VTable *pVTab;.
1c6a1 20 20 20 20 7d 20 63 66 3b 0a 20 20 20 20 73 74      } cf;.    st
1c6a2 72 75 63 74 20 4f 50 5f 56 4f 70 65 6e 5f 73 74  ruct OP_VOpen_st
1c6a3 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c6a4 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75   VdbeCursor *pCu
1c6a5 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  r;.      sqlite3
1c6a6 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56  _vtab_cursor *pV
1c6a7 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20 20  tabCursor;.     
1c6a8 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
1c6a9 56 74 61 62 3b 0a 20 20 20 20 20 20 73 71 6c 69  Vtab;.      sqli
1c6aa 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
1c6ab 75 6c 65 3b 0a 20 20 20 20 7d 20 63 67 3b 0a 20  ule;.    } cg;. 
1c6ac 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56 46 69     struct OP_VFi
1c6ad 6c 74 65 72 5f 73 74 61 63 6b 5f 76 61 72 73 20  lter_stack_vars 
1c6ae 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 41 72 67  {.      int nArg
1c6af 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 51 75 65  ;.      int iQue
1c6b0 72 79 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  ry;.      const 
1c6b1 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a  sqlite3_module *
1c6b2 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 20 20 4d  pModule;.      M
1c6b3 65 6d 20 2a 70 51 75 65 72 79 3b 0a 20 20 20 20  em *pQuery;.    
1c6b4 20 20 4d 65 6d 20 2a 70 41 72 67 63 3b 0a 20 20    Mem *pArgc;.  
1c6b5 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
1c6b6 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75  _cursor *pVtabCu
1c6b7 72 73 6f 72 3b 0a 20 20 20 20 20 20 73 71 6c 69  rsor;.      sqli
1c6b8 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b  te3_vtab *pVtab;
1c6b9 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f  .      VdbeCurso
1c6ba 72 20 2a 70 43 75 72 3b 0a 20 20 20 20 20 20 69  r *pCur;.      i
1c6bb 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 69 6e  nt res;.      in
1c6bc 74 20 69 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a  t i;.      Mem *
1c6bd 2a 61 70 41 72 67 3b 0a 20 20 20 20 7d 20 63 68  *apArg;.    } ch
1c6be 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c6bf 56 43 6f 6c 75 6d 6e 5f 73 74 61 63 6b 5f 76 61  VColumn_stack_va
1c6c0 72 73 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  rs {.      sqlit
1c6c1 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a  e3_vtab *pVtab;.
1c6c2 20 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69        const sqli
1c6c3 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
1c6c4 75 6c 65 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a  ule;.      Mem *
1c6c5 70 44 65 73 74 3b 0a 20 20 20 20 20 20 73 71 6c  pDest;.      sql
1c6c6 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 43 6f  ite3_context sCo
1c6c7 6e 74 65 78 74 3b 0a 20 20 20 20 7d 20 63 69 3b  ntext;.    } ci;
1c6c8 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56  .    struct OP_V
1c6c9 4e 65 78 74 5f 73 74 61 63 6b 5f 76 61 72 73 20  Next_stack_vars 
1c6ca 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
1c6cb 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20  vtab *pVtab;.   
1c6cc 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33     const sqlite3
1c6cd 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65  _module *pModule
1c6ce 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b  ;.      int res;
1c6cf 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f  .      VdbeCurso
1c6d0 72 20 2a 70 43 75 72 3b 0a 20 20 20 20 7d 20 63  r *pCur;.    } c
1c6d1 6a 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  j;.    struct OP
1c6d2 5f 56 52 65 6e 61 6d 65 5f 73 74 61 63 6b 5f 76  _VRename_stack_v
1c6d3 61 72 73 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  ars {.      sqli
1c6d4 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b  te3_vtab *pVtab;
1c6d5 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4e 61 6d  .      Mem *pNam
1c6d6 65 3b 0a 20 20 20 20 7d 20 63 6b 3b 0a 20 20 20  e;.    } ck;.   
1c6d7 20 73 74 72 75 63 74 20 4f 50 5f 56 55 70 64 61   struct OP_VUpda
1c6d8 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  te_stack_vars {.
1c6d9 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74        sqlite3_vt
1c6da 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20  ab *pVtab;.     
1c6db 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
1c6dc 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 20 20  *pModule;.      
1c6dd 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20 20 20  int nArg;.      
1c6de 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 73 71 6c  int i;.      sql
1c6df 69 74 65 5f 69 6e 74 36 34 20 72 6f 77 69 64 3b  ite_int64 rowid;
1c6e0 0a 20 20 20 20 20 20 4d 65 6d 20 2a 2a 61 70 41  .      Mem **apA
1c6e1 72 67 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70  rg;.      Mem *p
1c6e2 58 3b 0a 20 20 20 20 7d 20 63 6c 3b 0a 20 20 20  X;.    } cl;.   
1c6e3 20 73 74 72 75 63 74 20 4f 50 5f 50 61 67 65 63   struct OP_Pagec
1c6e4 6f 75 6e 74 5f 73 74 61 63 6b 5f 76 61 72 73 20  ount_stack_vars 
1c6e5 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 31 3b 0a  {.      int p1;.
1c6e6 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 3b        int nPage;
1c6e7 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a 70 50  .      Pager *pP
1c6e8 61 67 65 72 3b 0a 20 20 20 20 7d 20 63 6d 3b 0a  ager;.    } cm;.
1c6e9 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 54 72      struct OP_Tr
1c6ea 61 63 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ace_stack_vars {
1c6eb 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 72  .      char *zTr
1c6ec 61 63 65 3b 0a 20 20 20 20 7d 20 63 6e 3b 0a 20  ace;.    } cn;. 
1c6ed 20 7d 20 75 3b 0a 20 20 2f 2a 20 45 6e 64 20 61   } u;.  /* End a
1c6ee 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e  utomatically gen
1c6ef 65 72 61 74 65 64 20 63 6f 64 65 0a 20 20 2a 2a  erated code.  **
1c6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c6f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c6f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c6f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c6f4 2a 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70  **/..  assert( p
1c6f5 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
1c6f6 47 49 43 5f 52 55 4e 20 29 3b 20 20 2f 2a 20 73  GIC_RUN );  /* s
1c6f7 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 76 65  qlite3_step() ve
1c6f8 72 69 66 69 65 73 20 74 68 69 73 20 2a 2f 0a 20  rifies this */. 
1c6f9 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 67   assert( db->mag
1c6fa 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43  ic==SQLITE_MAGIC
1c6fb 5f 42 55 53 59 20 29 3b 0a 20 20 73 71 6c 69 74  _BUSY );.  sqlit
1c6fc 65 33 56 64 62 65 4d 75 74 65 78 41 72 72 61 79  e3VdbeMutexArray
1c6fd 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20  Enter(p);.  if( 
1c6fe 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  p->rc==SQLITE_NO
1c6ff 4d 45 4d 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  MEM ){.    /* Th
1c700 69 73 20 68 61 70 70 65 6e 73 20 69 66 20 61 20  is happens if a 
1c701 6d 61 6c 6c 6f 63 28 29 20 69 6e 73 69 64 65 20  malloc() inside 
1c702 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
1c703 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20  3_column_text() 
1c704 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  or.    ** sqlite
1c705 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28  3_column_text16(
1c706 29 20 66 61 69 6c 65 64 2e 20 20 2a 2f 0a 20 20  ) failed.  */.  
1c707 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
1c708 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e   }.  assert( p->
1c709 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
1c70a 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 42   p->rc==SQLITE_B
1c70b 55 53 59 20 29 3b 0a 20 20 70 2d 3e 72 63 20 3d  USY );.  p->rc =
1c70c 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73   SQLITE_OK;.  as
1c70d 73 65 72 74 28 20 70 2d 3e 65 78 70 6c 61 69 6e  sert( p->explain
1c70e 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e 70 52 65 73  ==0 );.  p->pRes
1c70f 75 6c 74 53 65 74 20 3d 20 30 3b 0a 20 20 64 62  ultSet = 0;.  db
1c710 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e 6e 42  ->busyHandler.nB
1c711 75 73 79 20 3d 20 30 3b 0a 20 20 43 48 45 43 4b  usy = 0;.  CHECK
1c712 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a  _FOR_INTERRUPT;.
1c713 20 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54    sqlite3VdbeIOT
1c714 72 61 63 65 53 71 6c 28 70 29 3b 0a 23 69 66 64  raceSql(p);.#ifd
1c715 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
1c716 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65    sqlite3BeginBe
1c717 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20  nignMalloc();.  
1c718 69 66 28 20 70 2d 3e 70 63 3d 3d 30 20 0a 20 20  if( p->pc==0 .  
1c719 20 26 26 20 28 28 70 2d 3e 64 62 2d 3e 66 6c 61   && ((p->db->fla
1c71a 67 73 20 26 20 53 51 4c 49 54 45 5f 56 64 62 65  gs & SQLITE_Vdbe
1c71b 4c 69 73 74 69 6e 67 29 20 7c 7c 20 66 69 6c 65  Listing) || file
1c71c 45 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 65  Exists(db, "vdbe
1c71d 5f 65 78 70 6c 61 69 6e 22 29 29 0a 20 20 29 7b  _explain")).  ){
1c71e 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
1c71f 70 72 69 6e 74 66 28 22 56 44 42 45 20 50 72 6f  printf("VDBE Pro
1c720 67 72 61 6d 20 4c 69 73 74 69 6e 67 3a 5c 6e 22  gram Listing:\n"
1c721 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1c722 62 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20  bePrintSql(p);. 
1c723 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d     for(i=0; i<p-
1c724 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nOp; i++){.    
1c725 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69    sqlite3VdbePri
1c726 6e 74 4f 70 28 73 74 64 6f 75 74 2c 20 69 2c 20  ntOp(stdout, i, 
1c727 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20 20  &p->aOp[i]);.   
1c728 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 66 69 6c   }.  }.  if( fil
1c729 65 45 78 69 73 74 73 28 64 62 2c 20 22 76 64 62  eExists(db, "vdb
1c72a 65 5f 74 72 61 63 65 22 29 20 29 7b 0a 20 20 20  e_trace") ){.   
1c72b 20 70 2d 3e 74 72 61 63 65 20 3d 20 73 74 64 6f   p->trace = stdo
1c72c 75 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ut;.  }.  sqlite
1c72d 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  3EndBenignMalloc
1c72e 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 66 6f 72  ();.#endif.  for
1c72f 28 70 63 3d 70 2d 3e 70 63 3b 20 72 63 3d 3d 53  (pc=p->pc; rc==S
1c730 51 4c 49 54 45 5f 4f 4b 3b 20 70 63 2b 2b 29 7b  QLITE_OK; pc++){
1c731 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3e  .    assert( pc>
1c732 3d 30 20 26 26 20 70 63 3c 70 2d 3e 6e 4f 70 20  =0 && pc<p->nOp 
1c733 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d  );.    if( db->m
1c734 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f  allocFailed ) go
1c735 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 23 69 66 64 65  to no_mem;.#ifde
1c736 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20  f VDBE_PROFILE. 
1c737 20 20 20 6f 72 69 67 50 63 20 3d 20 70 63 3b 0a     origPc = pc;.
1c738 20 20 20 20 73 74 61 72 74 20 3d 20 73 71 6c 69      start = sqli
1c739 74 65 33 48 77 74 69 6d 65 28 29 3b 0a 23 65 6e  te3Hwtime();.#en
1c73a 64 69 66 0a 20 20 20 20 70 4f 70 20 3d 20 26 70  dif.    pOp = &p
1c73b 2d 3e 61 4f 70 5b 70 63 5d 3b 0a 0a 20 20 20 20  ->aOp[pc];..    
1c73c 2f 2a 20 4f 6e 6c 79 20 61 6c 6c 6f 77 20 74 72  /* Only allow tr
1c73d 61 63 69 6e 67 20 69 66 20 53 51 4c 49 54 45 5f  acing if SQLITE_
1c73e 44 45 42 55 47 20 69 73 20 64 65 66 69 6e 65 64  DEBUG is defined
1c73f 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20  ..    */.#ifdef 
1c740 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
1c741 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b   if( p->trace ){
1c742 0a 20 20 20 20 20 20 69 66 28 20 70 63 3d 3d 30  .      if( pc==0
1c743 20 29 7b 0a 20 20 20 20 20 20 20 20 70 72 69 6e   ){.        prin
1c744 74 66 28 22 56 44 42 45 20 45 78 65 63 75 74 69  tf("VDBE Executi
1c745 6f 6e 20 54 72 61 63 65 3a 5c 6e 22 29 3b 0a 20  on Trace:\n");. 
1c746 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1c747 62 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20  bePrintSql(p);. 
1c748 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
1c749 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28  ite3VdbePrintOp(
1c74a 70 2d 3e 74 72 61 63 65 2c 20 70 63 2c 20 70 4f  p->trace, pc, pO
1c74b 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  p);.    }.    if
1c74c 28 20 70 2d 3e 74 72 61 63 65 3d 3d 30 20 26 26  ( p->trace==0 &&
1c74d 20 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20   pc==0 ){.      
1c74e 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69  sqlite3BeginBeni
1c74f 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20  gnMalloc();.    
1c750 20 20 69 66 28 20 66 69 6c 65 45 78 69 73 74 73    if( fileExists
1c751 28 64 62 2c 20 22 76 64 62 65 5f 73 71 6c 74 72  (db, "vdbe_sqltr
1c752 61 63 65 22 29 20 29 7b 0a 20 20 20 20 20 20 20  ace") ){.       
1c753 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e   sqlite3VdbePrin
1c754 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 20 20 7d  tSql(p);.      }
1c755 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e  .      sqlite3En
1c756 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
1c757 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
1c758 20 20 20 20 0a 0a 20 20 20 20 2f 2a 20 43 68 65      ..    /* Che
1c759 63 6b 20 74 6f 20 73 65 65 20 69 66 20 77 65 20  ck to see if we 
1c75a 6e 65 65 64 20 74 6f 20 73 69 6d 75 6c 61 74 65  need to simulate
1c75b 20 61 6e 20 69 6e 74 65 72 72 75 70 74 2e 20 20   an interrupt.  
1c75c 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e  This only happen
1c75d 73 0a 20 20 20 20 2a 2a 20 69 66 20 77 65 20 68  s.    ** if we h
1c75e 61 76 65 20 61 20 73 70 65 63 69 61 6c 20 74 65  ave a special te
1c75f 73 74 20 62 75 69 6c 64 2e 0a 20 20 20 20 2a 2f  st build..    */
1c760 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
1c761 45 53 54 0a 20 20 20 20 69 66 28 20 73 71 6c 69  EST.    if( sqli
1c762 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f  te3_interrupt_co
1c763 75 6e 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 73  unt>0 ){.      s
1c764 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
1c765 5f 63 6f 75 6e 74 2d 2d 3b 0a 20 20 20 20 20 20  _count--;.      
1c766 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  if( sqlite3_inte
1c767 72 72 75 70 74 5f 63 6f 75 6e 74 3d 3d 30 20 29  rrupt_count==0 )
1c768 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1c769 33 5f 69 6e 74 65 72 72 75 70 74 28 64 62 29 3b  3_interrupt(db);
1c76a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
1c76b 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
1c76c 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52  QLITE_OMIT_PROGR
1c76d 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 20  ESS_CALLBACK.   
1c76e 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20 70 72 6f   /* Call the pro
1c76f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 69  gress callback i
1c770 66 20 69 74 20 69 73 20 63 6f 6e 66 69 67 75 72  f it is configur
1c771 65 64 20 61 6e 64 20 74 68 65 20 72 65 71 75 69  ed and the requi
1c772 72 65 64 20 6e 75 6d 62 65 72 0a 20 20 20 20 2a  red number.    *
1c773 2a 20 6f 66 20 56 44 42 45 20 6f 70 73 20 68 61  * of VDBE ops ha
1c774 76 65 20 62 65 65 6e 20 65 78 65 63 75 74 65 64  ve been executed
1c775 20 28 65 69 74 68 65 72 20 73 69 6e 63 65 20 74   (either since t
1c776 68 69 73 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  his invocation o
1c777 66 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33  f.    ** sqlite3
1c778 56 64 62 65 45 78 65 63 28 29 20 6f 72 20 73 69  VdbeExec() or si
1c779 6e 63 65 20 6c 61 73 74 20 74 69 6d 65 20 74 68  nce last time th
1c77a 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62  e progress callb
1c77b 61 63 6b 20 77 61 73 20 63 61 6c 6c 65 64 29 2e  ack was called).
1c77c 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 70  .    ** If the p
1c77d 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b  rogress callback
1c77e 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
1c77f 6f 2c 20 65 78 69 74 20 74 68 65 20 76 69 72 74  o, exit the virt
1c780 75 61 6c 20 6d 61 63 68 69 6e 65 20 77 69 74 68  ual machine with
1c781 0a 20 20 20 20 2a 2a 20 61 20 72 65 74 75 72 6e  .    ** a return
1c782 20 63 6f 64 65 20 53 51 4c 49 54 45 5f 41 42 4f   code SQLITE_ABO
1c783 52 54 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  RT..    */.    i
1c784 66 28 20 64 62 2d 3e 78 50 72 6f 67 72 65 73 73  f( db->xProgress
1c785 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64 62   ){.      if( db
1c786 2d 3e 6e 50 72 6f 67 72 65 73 73 4f 70 73 3d 3d  ->nProgressOps==
1c787 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 29 7b 0a  nProgressOps ){.
1c788 20 20 20 20 20 20 20 20 69 6e 74 20 70 72 63 3b          int prc;
1c789 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
1c78a 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
1c78b 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  ) ) goto abort_d
1c78c 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20  ue_to_misuse;.  
1c78d 20 20 20 20 20 20 70 72 63 20 3d 64 62 2d 3e 78        prc =db->x
1c78e 50 72 6f 67 72 65 73 73 28 64 62 2d 3e 70 50 72  Progress(db->pPr
1c78f 6f 67 72 65 73 73 41 72 67 29 3b 0a 20 20 20 20  ogressArg);.    
1c790 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
1c791 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f  afetyOn(db) ) go
1c792 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1c793 6d 69 73 75 73 65 3b 0a 20 20 20 20 20 20 20 20  misuse;.        
1c794 69 66 28 20 70 72 63 21 3d 30 20 29 7b 0a 20 20  if( prc!=0 ){.  
1c795 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
1c796 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20  ITE_INTERRUPT;. 
1c797 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64           goto vd
1c798 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 20  be_error_halt;. 
1c799 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1c79a 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20   nProgressOps = 
1c79b 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
1c79c 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 2b 2b 3b   nProgressOps++;
1c79d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
1c79e 20 20 20 2f 2a 20 44 6f 20 63 6f 6d 6d 6f 6e 20     /* Do common 
1c79f 73 65 74 75 70 20 70 72 6f 63 65 73 73 69 6e 67  setup processing
1c7a0 20 66 6f 72 20 61 6e 79 20 6f 70 63 6f 64 65 20   for any opcode 
1c7a1 74 68 61 74 20 69 73 20 6d 61 72 6b 65 64 0a 20  that is marked. 
1c7a2 20 20 20 2a 2a 20 77 69 74 68 20 74 68 65 20 22     ** with the "
1c7a3 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 22  out2-prerelease"
1c7a4 20 74 61 67 2e 20 20 53 75 63 68 20 6f 70 63 6f   tag.  Such opco
1c7a5 64 65 73 20 68 61 76 65 20 61 20 73 69 6e 67 6c  des have a singl
1c7a6 65 0a 20 20 20 20 2a 2a 20 6f 75 74 70 75 74 20  e.    ** output 
1c7a7 77 68 69 63 68 20 69 73 20 73 70 65 63 69 66 69  which is specifi
1c7a8 65 64 20 62 79 20 74 68 65 20 50 32 20 70 61 72  ed by the P2 par
1c7a9 61 6d 65 74 65 72 2e 20 20 54 68 65 20 50 32 20  ameter.  The P2 
1c7aa 72 65 67 69 73 74 65 72 0a 20 20 20 20 2a 2a 20  register.    ** 
1c7ab 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74  is initialized t
1c7ac 6f 20 61 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f  o a NULL..    */
1c7ad 0a 20 20 20 20 6f 70 50 72 6f 70 65 72 74 79 20  .    opProperty 
1c7ae 3d 20 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79  = opcodeProperty
1c7af 5b 70 4f 70 2d 3e 6f 70 63 6f 64 65 5d 3b 0a 20  [pOp->opcode];. 
1c7b0 20 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72     if( (opProper
1c7b1 74 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32 5f  ty & OPFLG_OUT2_
1c7b2 50 52 45 52 45 4c 45 41 53 45 29 21 3d 30 20 29  PRERELEASE)!=0 )
1c7b3 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1c7b4 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20  pOp->p2>0 );.   
1c7b5 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1c7b6 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  p2<=p->nMem );. 
1c7b7 20 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e       pOut = &p->
1c7b8 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
1c7b9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1c7ba 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e  MemReleaseExtern
1c7bb 61 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 20 20  al(pOut);.      
1c7bc 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pOut->flags = ME
1c7bd 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 70 4f  M_Null;.      pO
1c7be 75 74 2d 3e 6e 20 3d 20 30 3b 0a 20 20 20 20 7d  ut->n = 0;.    }
1c7bf 65 6c 73 65 0a 20 0a 20 20 20 20 2f 2a 20 44 6f  else. .    /* Do
1c7c0 20 63 6f 6d 6d 6f 6e 20 73 65 74 75 70 20 66 6f   common setup fo
1c7c1 72 20 6f 70 63 6f 64 65 73 20 6d 61 72 6b 65 64  r opcodes marked
1c7c2 20 77 69 74 68 20 6f 6e 65 20 6f 66 20 74 68 65   with one of the
1c7c3 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 2a   following.    *
1c7c4 2a 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6f  * combinations o
1c7c5 66 20 70 72 6f 70 65 72 74 69 65 73 2e 0a 20 20  f properties..  
1c7c6 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
1c7c7 20 20 20 20 20 20 69 6e 31 0a 20 20 20 20 2a 2a        in1.    **
1c7c8 20 20 20 20 20 20 20 20 20 20 20 69 6e 31 20 69             in1 i
1c7c9 6e 32 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  n2.    **       
1c7ca 20 20 20 20 69 6e 31 20 69 6e 32 20 6f 75 74 33      in1 in2 out3
1c7cb 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
1c7cc 20 20 69 6e 31 20 69 6e 33 0a 20 20 20 20 2a 2a    in1 in3.    **
1c7cd 0a 20 20 20 20 2a 2a 20 56 61 72 69 61 62 6c 65  .    ** Variable
1c7ce 73 20 70 49 6e 31 2c 20 70 49 6e 32 2c 20 61 6e  s pIn1, pIn2, an
1c7cf 64 20 70 49 6e 33 20 61 72 65 20 6d 61 64 65 20  d pIn3 are made 
1c7d0 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 70 70 72  to point to appr
1c7d1 6f 70 72 69 61 74 65 0a 20 20 20 20 2a 2a 20 72  opriate.    ** r
1c7d2 65 67 69 73 74 65 72 73 20 66 6f 72 20 69 6e 70  egisters for inp
1c7d3 75 74 73 2e 20 20 56 61 72 69 61 62 6c 65 20 70  uts.  Variable p
1c7d4 4f 75 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  Out points to th
1c7d5 65 20 6f 75 74 70 75 74 20 72 65 67 69 73 74 65  e output registe
1c7d6 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  r..    */.    if
1c7d7 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20  ( (opProperty & 
1c7d8 4f 50 46 4c 47 5f 49 4e 31 29 21 3d 30 20 29 7b  OPFLG_IN1)!=0 ){
1c7d9 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1c7da 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 20 20  Op->p1>0 );.    
1c7db 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1c7dc 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  1<=p->nMem );.  
1c7dd 20 20 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61      pIn1 = &p->a
1c7de 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Mem[pOp->p1];.  
1c7df 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41      REGISTER_TRA
1c7e0 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31  CE(pOp->p1, pIn1
1c7e1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 6f 70  );.      if( (op
1c7e2 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47  Property & OPFLG
1c7e3 5f 49 4e 32 29 21 3d 30 20 29 7b 0a 20 20 20 20  _IN2)!=0 ){.    
1c7e4 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1c7e5 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 20  >p2>0 );.       
1c7e6 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
1c7e7 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20  <=p->nMem );.   
1c7e8 20 20 20 20 20 70 49 6e 32 20 3d 20 26 70 2d 3e       pIn2 = &p->
1c7e9 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
1c7ea 20 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f         REGISTER_
1c7eb 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70  TRACE(pOp->p2, p
1c7ec 49 6e 32 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a  In2);.        /*
1c7ed 20 41 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6d   As currently im
1c7ee 70 6c 65 6d 65 6e 74 65 64 2c 20 69 6e 32 20 69  plemented, in2 i
1c7ef 6d 70 6c 69 65 73 20 6f 75 74 33 2e 20 20 54 68  mplies out3.  Th
1c7f0 65 72 65 20 69 73 20 6e 6f 20 72 65 61 73 6f 6e  ere is no reason
1c7f1 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 68 79 20  .        ** why 
1c7f2 74 68 69 73 20 68 61 73 20 74 6f 20 62 65 2c 20  this has to be, 
1c7f3 69 74 20 6a 75 73 74 20 77 6f 72 6b 65 64 20 6f  it just worked o
1c7f4 75 74 20 74 68 61 74 20 77 61 79 2e 20 2a 2f 0a  ut that way. */.
1c7f5 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1c7f6 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50  (opProperty & OP
1c7f7 46 4c 47 5f 4f 55 54 33 29 21 3d 30 20 29 3b 0a  FLG_OUT3)!=0 );.
1c7f8 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1c7f9 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20  pOp->p3>0 );.   
1c7fa 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1c7fb 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p3<=p->nMem );
1c7fc 0a 20 20 20 20 20 20 20 20 70 4f 75 74 20 3d 20  .        pOut = 
1c7fd 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33  &p->aMem[pOp->p3
1c7fe 5d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  ];.      }else i
1c7ff 66 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26  f( (opProperty &
1c800 20 4f 50 46 4c 47 5f 49 4e 33 29 21 3d 30 20 29   OPFLG_IN3)!=0 )
1c801 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
1c802 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20  ( pOp->p3>0 );. 
1c803 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1c804 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p3<=p->nMem 
1c805 29 3b 0a 20 20 20 20 20 20 20 20 70 49 6e 33 20  );.        pIn3 
1c806 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
1c807 70 33 5d 3b 0a 20 20 20 20 20 20 20 20 52 45 47  p3];.        REG
1c808 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1c809 3e 70 33 2c 20 70 49 6e 33 29 3b 0a 20 20 20 20  >p3, pIn3);.    
1c80a 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66    }.    }else if
1c80b 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20  ( (opProperty & 
1c80c 4f 50 46 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b  OPFLG_IN2)!=0 ){
1c80d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1c80e 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20  Op->p2>0 );.    
1c80f 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1c810 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  2<=p->nMem );.  
1c811 20 20 20 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61      pIn2 = &p->a
1c812 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20  Mem[pOp->p2];.  
1c813 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41      REGISTER_TRA
1c814 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32  CE(pOp->p2, pIn2
1c815 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
1c816 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f   (opProperty & O
1c817 50 46 4c 47 5f 49 4e 33 29 21 3d 30 20 29 7b 0a  PFLG_IN3)!=0 ){.
1c818 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1c819 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20  p->p3>0 );.     
1c81a 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33   assert( pOp->p3
1c81b 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20  <=p->nMem );.   
1c81c 20 20 20 70 49 6e 33 20 3d 20 26 70 2d 3e 61 4d     pIn3 = &p->aM
1c81d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20  em[pOp->p3];.   
1c81e 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43     REGISTER_TRAC
1c81f 45 28 70 4f 70 2d 3e 70 33 2c 20 70 49 6e 33 29  E(pOp->p3, pIn3)
1c820 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 77 69  ;.    }..    swi
1c821 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  tch( pOp->opcode
1c822 20 29 7b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ){../**********
1c823 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c824 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c825 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c826 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c827 2a 2a 2a 0a 2a 2a 20 57 68 61 74 20 66 6f 6c 6c  ***.** What foll
1c828 6f 77 73 20 69 73 20 61 20 6d 61 73 73 69 76 65  ows is a massive
1c829 20 73 77 69 74 63 68 20 73 74 61 74 65 6d 65 6e   switch statemen
1c82a 74 20 77 68 65 72 65 20 65 61 63 68 20 63 61 73  t where each cas
1c82b 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 0a 2a  e implements a.*
1c82c 2a 20 73 65 70 61 72 61 74 65 20 69 6e 73 74 72  * separate instr
1c82d 75 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 76 69  uction in the vi
1c82e 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20  rtual machine.  
1c82f 49 66 20 77 65 20 66 6f 6c 6c 6f 77 20 74 68 65  If we follow the
1c830 20 75 73 75 61 6c 0a 2a 2a 20 69 6e 64 65 6e 74   usual.** indent
1c831 61 74 69 6f 6e 20 63 6f 6e 76 65 6e 74 69 6f 6e  ation convention
1c832 73 2c 20 65 61 63 68 20 63 61 73 65 20 73 68 6f  s, each case sho
1c833 75 6c 64 20 62 65 20 69 6e 64 65 6e 74 65 64 20  uld be indented 
1c834 62 79 20 36 20 73 70 61 63 65 73 2e 20 20 42 75  by 6 spaces.  Bu
1c835 74 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 20 6c  t.** that is a l
1c836 6f 74 20 6f 66 20 77 61 73 74 65 64 20 73 70 61  ot of wasted spa
1c837 63 65 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 6d  ce on the left m
1c838 61 72 67 69 6e 2e 20 20 53 6f 20 74 68 65 20 63  argin.  So the c
1c839 6f 64 65 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68  ode within.** th
1c83a 65 20 73 77 69 74 63 68 20 73 74 61 74 65 6d 65  e switch stateme
1c83b 6e 74 20 77 69 6c 6c 20 62 72 65 61 6b 20 77 69  nt will break wi
1c83c 74 68 20 63 6f 6e 76 65 6e 74 69 6f 6e 20 61 6e  th convention an
1c83d 64 20 62 65 20 66 6c 75 73 68 2d 6c 65 66 74 2e  d be flush-left.
1c83e 20 41 6e 6f 74 68 65 72 0a 2a 2a 20 62 69 67 20   Another.** big 
1c83f 63 6f 6d 6d 65 6e 74 20 28 73 69 6d 69 6c 61 72  comment (similar
1c840 20 74 6f 20 74 68 69 73 20 6f 6e 65 29 20 77 69   to this one) wi
1c841 6c 6c 20 6d 61 72 6b 20 74 68 65 20 70 6f 69 6e  ll mark the poin
1c842 74 20 69 6e 20 74 68 65 20 63 6f 64 65 20 77 68  t in the code wh
1c843 65 72 65 0a 2a 2a 20 77 65 20 74 72 61 6e 73 69  ere.** we transi
1c844 74 69 6f 6e 20 62 61 63 6b 20 74 6f 20 6e 6f 72  tion back to nor
1c845 6d 61 6c 20 69 6e 64 65 6e 74 61 74 69 6f 6e 2e  mal indentation.
1c846 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 61  .**.** The forma
1c847 74 74 69 6e 67 20 6f 66 20 65 61 63 68 20 63 61  tting of each ca
1c848 73 65 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e  se is important.
1c849 20 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 66    The makefile f
1c84a 6f 72 20 53 51 4c 69 74 65 0a 2a 2a 20 67 65 6e  or SQLite.** gen
1c84b 65 72 61 74 65 73 20 74 77 6f 20 43 20 66 69 6c  erates two C fil
1c84c 65 73 20 22 6f 70 63 6f 64 65 73 2e 68 22 20 61  es "opcodes.h" a
1c84d 6e 64 20 22 6f 70 63 6f 64 65 73 2e 63 22 20 62  nd "opcodes.c" b
1c84e 79 20 73 63 61 6e 6e 69 6e 67 20 74 68 69 73 0a  y scanning this.
1c84f 2a 2a 20 66 69 6c 65 20 6c 6f 6f 6b 69 6e 67 20  ** file looking 
1c850 66 6f 72 20 6c 69 6e 65 73 20 74 68 61 74 20 62  for lines that b
1c851 65 67 69 6e 20 77 69 74 68 20 22 63 61 73 65 20  egin with "case 
1c852 4f 50 5f 22 2e 20 20 54 68 65 20 6f 70 63 6f 64  OP_".  The opcod
1c853 65 73 2e 68 20 66 69 6c 65 73 0a 2a 2a 20 77 69  es.h files.** wi
1c854 6c 6c 20 62 65 20 66 69 6c 6c 65 64 20 77 69 74  ll be filled wit
1c855 68 20 23 64 65 66 69 6e 65 73 20 74 68 61 74 20  h #defines that 
1c856 67 69 76 65 20 75 6e 69 71 75 65 20 69 6e 74 65  give unique inte
1c857 67 65 72 20 76 61 6c 75 65 73 20 74 6f 20 65 61  ger values to ea
1c858 63 68 0a 2a 2a 20 6f 70 63 6f 64 65 20 61 6e 64  ch.** opcode and
1c859 20 74 68 65 20 6f 70 63 6f 64 65 73 2e 63 20 66   the opcodes.c f
1c85a 69 6c 65 20 69 73 20 66 69 6c 6c 65 64 20 77 69  ile is filled wi
1c85b 74 68 20 61 6e 20 61 72 72 61 79 20 6f 66 20 73  th an array of s
1c85c 74 72 69 6e 67 73 20 77 68 65 72 65 0a 2a 2a 20  trings where.** 
1c85d 65 61 63 68 20 73 74 72 69 6e 67 20 69 73 20 74  each string is t
1c85e 68 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65  he symbolic name
1c85f 20 66 6f 72 20 74 68 65 20 63 6f 72 72 65 73 70   for the corresp
1c860 6f 6e 64 69 6e 67 20 6f 70 63 6f 64 65 2e 20 20  onding opcode.  
1c861 49 66 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 73  If the.** case s
1c862 74 61 74 65 6d 65 6e 74 20 69 73 20 66 6f 6c 6c  tatement is foll
1c863 6f 77 65 64 20 62 79 20 61 20 63 6f 6d 6d 65 6e  owed by a commen
1c864 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 2f  t of the form "/
1c865 23 20 73 61 6d 65 20 61 73 20 2e 2e 2e 20 23 2f  # same as ... #/
1c866 22 0a 2a 2a 20 74 68 61 74 20 63 6f 6d 6d 65 6e  ".** that commen
1c867 74 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74  t is used to det
1c868 65 72 6d 69 6e 65 20 74 68 65 20 70 61 72 74 69  ermine the parti
1c869 63 75 6c 61 72 20 76 61 6c 75 65 20 6f 66 20 74  cular value of t
1c86a 68 65 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a  he opcode..**.**
1c86b 20 4f 74 68 65 72 20 6b 65 79 77 6f 72 64 73 20   Other keywords 
1c86c 69 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 74  in the comment t
1c86d 68 61 74 20 66 6f 6c 6c 6f 77 73 20 65 61 63 68  hat follows each
1c86e 20 63 61 73 65 20 61 72 65 20 75 73 65 64 20 74   case are used t
1c86f 6f 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74 20 74  o.** construct t
1c870 68 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c  he OPFLG_INITIAL
1c871 49 5a 45 52 20 76 61 6c 75 65 20 74 68 61 74 20  IZER value that 
1c872 69 6e 69 74 69 61 6c 69 7a 65 73 20 6f 70 63 6f  initializes opco
1c873 64 65 50 72 6f 70 65 72 74 79 5b 5d 2e 0a 2a 2a  deProperty[]..**
1c874 20 4b 65 79 77 6f 72 64 73 20 69 6e 63 6c 75 64   Keywords includ
1c875 65 3a 20 69 6e 31 2c 20 69 6e 32 2c 20 69 6e 33  e: in1, in2, in3
1c876 2c 20 6f 75 74 32 5f 70 72 65 72 65 6c 65 61 73  , out2_prereleas
1c877 65 2c 20 6f 75 74 32 2c 20 6f 75 74 33 2e 20 20  e, out2, out3.  
1c878 53 65 65 0a 2a 2a 20 74 68 65 20 6d 6b 6f 70 63  See.** the mkopc
1c879 6f 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20  odeh.awk script 
1c87a 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
1c87b 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  nformation..**.*
1c87c 2a 20 44 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20  * Documentation 
1c87d 61 62 6f 75 74 20 56 44 42 45 20 6f 70 63 6f 64  about VDBE opcod
1c87e 65 73 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  es is generated 
1c87f 62 79 20 73 63 61 6e 6e 69 6e 67 20 74 68 69 73  by scanning this
1c880 20 66 69 6c 65 0a 2a 2a 20 66 6f 72 20 6c 69 6e   file.** for lin
1c881 65 73 20 6f 66 20 74 68 61 74 20 63 6f 6e 74 61  es of that conta
1c882 69 6e 20 22 4f 70 63 6f 64 65 3a 22 2e 20 20 54  in "Opcode:".  T
1c883 68 61 74 20 6c 69 6e 65 20 61 6e 64 20 61 6c 6c  hat line and all
1c884 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63   subsequent.** c
1c885 6f 6d 6d 65 6e 74 20 6c 69 6e 65 73 20 61 72 65  omment lines are
1c886 20 75 73 65 64 20 69 6e 20 74 68 65 20 67 65 6e   used in the gen
1c887 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f  eration of the o
1c888 70 63 6f 64 65 2e 68 74 6d 6c 20 64 6f 63 75 6d  pcode.html docum
1c889 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 66 69 6c 65  entation.** file
1c88a 2e 0a 2a 2a 0a 2a 2a 20 53 55 4d 4d 41 52 59 3a  ..**.** SUMMARY:
1c88b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 46 6f 72 6d 61  .**.**     Forma
1c88c 74 74 69 6e 67 20 69 73 20 69 6d 70 6f 72 74 61  tting is importa
1c88d 6e 74 20 74 6f 20 73 63 72 69 70 74 73 20 74 68  nt to scripts th
1c88e 61 74 20 73 63 61 6e 20 74 68 69 73 20 66 69 6c  at scan this fil
1c88f 65 2e 0a 2a 2a 20 20 20 20 20 44 6f 20 6e 6f 74  e..**     Do not
1c890 20 64 65 76 69 61 74 65 20 66 72 6f 6d 20 74 68   deviate from th
1c891 65 20 66 6f 72 6d 61 74 74 69 6e 67 20 73 74 79  e formatting sty
1c892 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  le currently in 
1c893 75 73 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  use..**.********
1c894 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c895 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c896 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c897 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c898 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64  *****/../* Opcod
1c899 65 3a 20 20 47 6f 74 6f 20 2a 20 50 32 20 2a 20  e:  Goto * P2 * 
1c89a 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6e 20 75 6e 63  * *.**.** An unc
1c89b 6f 6e 64 69 74 69 6f 6e 61 6c 20 6a 75 6d 70 20  onditional jump 
1c89c 74 6f 20 61 64 64 72 65 73 73 20 50 32 2e 0a 2a  to address P2..*
1c89d 2a 20 54 68 65 20 6e 65 78 74 20 69 6e 73 74 72  * The next instr
1c89e 75 63 74 69 6f 6e 20 65 78 65 63 75 74 65 64 20  uction executed 
1c89f 77 69 6c 6c 20 62 65 20 0a 2a 2a 20 74 68 65 20  will be .** the 
1c8a0 6f 6e 65 20 61 74 20 69 6e 64 65 78 20 50 32 20  one at index P2 
1c8a1 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69  from the beginni
1c8a2 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 70 72 6f  ng of.** the pro
1c8a3 67 72 61 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  gram..*/.case OP
1c8a4 5f 47 6f 74 6f 3a 20 7b 20 20 20 20 20 20 20 20  _Goto: {        
1c8a5 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a       /* jump */.
1c8a6 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45    CHECK_FOR_INTE
1c8a7 52 52 55 50 54 3b 0a 20 20 70 63 20 3d 20 70 4f  RRUPT;.  pc = pO
1c8a8 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 62 72 65  p->p2 - 1;.  bre
1c8a9 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1c8aa 3a 20 20 47 6f 73 75 62 20 50 31 20 50 32 20 2a  :  Gosub P1 P2 *
1c8ab 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65   * *.**.** Write
1c8ac 20 74 68 65 20 63 75 72 72 65 6e 74 20 61 64 64   the current add
1c8ad 72 65 73 73 20 6f 6e 74 6f 20 72 65 67 69 73 74  ress onto regist
1c8ae 65 72 20 50 31 0a 2a 2a 20 61 6e 64 20 74 68 65  er P1.** and the
1c8af 6e 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73  n jump to addres
1c8b0 73 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  s P2..*/.case OP
1c8b1 5f 47 6f 73 75 62 3a 20 7b 20 20 20 20 20 20 20  _Gosub: {       
1c8b2 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a       /* jump */.
1c8b3 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1c8b4 31 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  1>0 );.  assert(
1c8b5 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65   pOp->p1<=p->nMe
1c8b6 6d 20 29 3b 0a 20 20 70 49 6e 31 20 3d 20 26 70  m );.  pIn1 = &p
1c8b7 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b  ->aMem[pOp->p1];
1c8b8 0a 20 20 61 73 73 65 72 74 28 20 28 70 49 6e 31  .  assert( (pIn1
1c8b9 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44 79  ->flags & MEM_Dy
1c8ba 6e 29 3d 3d 30 20 29 3b 0a 20 20 70 49 6e 31 2d  n)==0 );.  pIn1-
1c8bb 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74  >flags = MEM_Int
1c8bc 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 3d 20  ;.  pIn1->u.i = 
1c8bd 70 63 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54  pc;.  REGISTER_T
1c8be 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49  RACE(pOp->p1, pI
1c8bf 6e 31 29 3b 0a 20 20 70 63 20 3d 20 70 4f 70 2d  n1);.  pc = pOp-
1c8c0 3e 70 32 20 2d 20 31 3b 0a 20 20 62 72 65 61 6b  >p2 - 1;.  break
1c8c1 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1c8c2 20 52 65 74 75 72 6e 20 50 31 20 2a 20 2a 20 2a   Return P1 * * *
1c8c3 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f   *.**.** Jump to
1c8c4 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75   the next instru
1c8c5 63 74 69 6f 6e 20 61 66 74 65 72 20 74 68 65 20  ction after the 
1c8c6 61 64 64 72 65 73 73 20 69 6e 20 72 65 67 69 73  address in regis
1c8c7 74 65 72 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20  ter P1..*/.case 
1c8c8 4f 50 5f 52 65 74 75 72 6e 3a 20 7b 20 20 20 20  OP_Return: {    
1c8c9 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f         /* in1 */
1c8ca 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d  .  assert( pIn1-
1c8cb 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74  >flags & MEM_Int
1c8cc 20 29 3b 0a 20 20 70 63 20 3d 20 28 69 6e 74 29   );.  pc = (int)
1c8cd 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 62 72 65  pIn1->u.i;.  bre
1c8ce 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1c8cf 3a 20 20 59 69 65 6c 64 20 50 31 20 2a 20 2a 20  :  Yield P1 * * 
1c8d0 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 77 61 70 20 74  * *.**.** Swap t
1c8d1 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74  he program count
1c8d2 65 72 20 77 69 74 68 20 74 68 65 20 76 61 6c 75  er with the valu
1c8d3 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
1c8d4 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 59 69 65  ..*/.case OP_Yie
1c8d5 6c 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  ld: {           
1c8d6 20 2f 2a 20 69 6e 31 20 2a 2f 0a 23 69 66 20 30   /* in1 */.#if 0
1c8d7 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1c8d8 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1c8d9 75 2e 61 61 20 2a 2f 0a 20 20 69 6e 74 20 70 63  u.aa */.  int pc
1c8da 44 65 73 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  Dest;.#endif /* 
1c8db 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1c8dc 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 61 20  moved into u.aa 
1c8dd 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 49  */.  assert( (pI
1c8de 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n1->flags & MEM_
1c8df 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 70 49 6e  Dyn)==0 );.  pIn
1c8e0 31 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49  1->flags = MEM_I
1c8e1 6e 74 3b 0a 20 20 75 2e 61 61 2e 70 63 44 65 73  nt;.  u.aa.pcDes
1c8e2 74 20 3d 20 28 69 6e 74 29 70 49 6e 31 2d 3e 75  t = (int)pIn1->u
1c8e3 2e 69 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20  .i;.  pIn1->u.i 
1c8e4 3d 20 70 63 3b 0a 20 20 52 45 47 49 53 54 45 52  = pc;.  REGISTER
1c8e5 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20  _TRACE(pOp->p1, 
1c8e6 70 49 6e 31 29 3b 0a 20 20 70 63 20 3d 20 75 2e  pIn1);.  pc = u.
1c8e7 61 61 2e 70 63 44 65 73 74 3b 0a 20 20 62 72 65  aa.pcDest;.  bre
1c8e8 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1c8e9 3a 20 20 48 61 6c 74 49 66 4e 75 6c 6c 20 20 50  :  HaltIfNull  P
1c8ea 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a  1 P2 P3 P4 *.**.
1c8eb 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 76 61 6c  ** Check the val
1c8ec 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1c8ed 33 2e 20 20 49 66 20 69 73 20 69 73 20 4e 55 4c  3.  If is is NUL
1c8ee 4c 20 74 68 65 6e 20 48 61 6c 74 20 75 73 69 6e  L then Halt usin
1c8ef 67 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 50  g.** parameter P
1c8f0 31 2c 20 50 32 2c 20 61 6e 64 20 50 34 20 61 73  1, P2, and P4 as
1c8f1 20 69 66 20 74 68 69 73 20 77 65 72 65 20 61 20   if this were a 
1c8f2 48 61 6c 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  Halt instruction
1c8f3 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 76 61 6c  .  If the.** val
1c8f4 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1c8f5 33 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74  3 is not NULL, t
1c8f6 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
1c8f7 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
1c8f8 63 61 73 65 20 4f 50 5f 48 61 6c 74 49 66 4e 75  case OP_HaltIfNu
1c8f9 6c 6c 3a 20 7b 20 20 20 20 20 20 2f 2a 20 69 6e  ll: {      /* in
1c8fa 33 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 33  3 */.  if( (pIn3
1c8fb 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
1c8fc 6c 6c 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a  ll)==0 ) break;.
1c8fd 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67    /* Fall throug
1c8fe 68 20 69 6e 74 6f 20 4f 50 5f 48 61 6c 74 20 2a  h into OP_Halt *
1c8ff 2f 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  /.}../* Opcode: 
1c900 20 48 61 6c 74 20 50 31 20 50 32 20 2a 20 50 34   Halt P1 P2 * P4
1c901 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 69 74 20 69 6d   *.**.** Exit im
1c902 6d 65 64 69 61 74 65 6c 79 2e 20 20 41 6c 6c 20  mediately.  All 
1c903 6f 70 65 6e 20 63 75 72 73 6f 72 73 2c 20 65 74  open cursors, et
1c904 63 20 61 72 65 20 63 6c 6f 73 65 64 0a 2a 2a 20  c are closed.** 
1c905 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a  automatically..*
1c906 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 72  *.** P1 is the r
1c907 65 73 75 6c 74 20 63 6f 64 65 20 72 65 74 75 72  esult code retur
1c908 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 65  ned by sqlite3_e
1c909 78 65 63 28 29 2c 20 73 71 6c 69 74 65 33 5f 72  xec(), sqlite3_r
1c90a 65 73 65 74 28 29 2c 0a 2a 2a 20 6f 72 20 73 71  eset(),.** or sq
1c90b 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
1c90c 2e 20 20 46 6f 72 20 61 20 6e 6f 72 6d 61 6c 20  .  For a normal 
1c90d 68 61 6c 74 2c 20 74 68 69 73 20 73 68 6f 75 6c  halt, this shoul
1c90e 64 20 62 65 20 53 51 4c 49 54 45 5f 4f 4b 20 28  d be SQLITE_OK (
1c90f 30 29 2e 0a 2a 2a 20 46 6f 72 20 65 72 72 6f 72  0)..** For error
1c910 73 2c 20 69 74 20 63 61 6e 20 62 65 20 73 6f 6d  s, it can be som
1c911 65 20 6f 74 68 65 72 20 76 61 6c 75 65 2e 20 20  e other value.  
1c912 49 66 20 50 31 21 3d 30 20 74 68 65 6e 20 50 32  If P1!=0 then P2
1c913 20 77 69 6c 6c 20 64 65 74 65 72 6d 69 6e 65 0a   will determine.
1c914 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ** whether or no
1c915 74 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68  t to rollback th
1c916 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61  e current transa
1c917 63 74 69 6f 6e 2e 20 20 44 6f 20 6e 6f 74 20 72  ction.  Do not r
1c918 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 50 32  ollback.** if P2
1c919 3d 3d 4f 45 5f 46 61 69 6c 2e 20 44 6f 20 74 68  ==OE_Fail. Do th
1c91a 65 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 50 32  e rollback if P2
1c91b 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 2e 20 20  ==OE_Rollback.  
1c91c 49 66 20 50 32 3d 3d 4f 45 5f 41 62 6f 72 74 2c  If P2==OE_Abort,
1c91d 0a 2a 2a 20 74 68 65 6e 20 62 61 63 6b 20 6f 75  .** then back ou
1c91e 74 20 61 6c 6c 20 63 68 61 6e 67 65 73 20 74 68  t all changes th
1c91f 61 74 20 68 61 76 65 20 6f 63 63 75 72 72 65 64  at have occurred
1c920 20 64 75 72 69 6e 67 20 74 68 69 73 20 65 78 65   during this exe
1c921 63 75 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a  cution of the.**
1c922 20 56 44 42 45 2c 20 62 75 74 20 64 6f 20 6e 6f   VDBE, but do no
1c923 74 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74  t rollback the t
1c924 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 2a 2a 0a  ransaction. .**.
1c925 2a 2a 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20  ** If P4 is not 
1c926 6e 75 6c 6c 20 74 68 65 6e 20 69 74 20 69 73 20  null then it is 
1c927 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
1c928 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54   string..**.** T
1c929 68 65 72 65 20 69 73 20 61 6e 20 69 6d 70 6c 69  here is an impli
1c92a 65 64 20 22 48 61 6c 74 20 30 20 30 20 30 22 20  ed "Halt 0 0 0" 
1c92b 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 73 65  instruction inse
1c92c 72 74 65 64 20 61 74 20 74 68 65 20 76 65 72 79  rted at the very
1c92d 20 65 6e 64 20 6f 66 0a 2a 2a 20 65 76 65 72 79   end of.** every
1c92e 20 70 72 6f 67 72 61 6d 2e 20 20 53 6f 20 61 20   program.  So a 
1c92f 6a 75 6d 70 20 70 61 73 74 20 74 68 65 20 6c 61  jump past the la
1c930 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f  st instruction o
1c931 66 20 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a 2a  f the program.**
1c932 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
1c933 65 78 65 63 75 74 69 6e 67 20 48 61 6c 74 2e 0a  executing Halt..
1c934 2a 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c 74 3a  */.case OP_Halt:
1c935 20 7b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31   {.  if( pOp->p1
1c936 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
1c937 2d 3e 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 20  ->pFrame ){.    
1c938 2f 2a 20 48 61 6c 74 20 74 68 65 20 73 75 62 2d  /* Halt the sub-
1c939 70 72 6f 67 72 61 6d 2e 20 52 65 74 75 72 6e 20  program. Return 
1c93a 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 70  control to the p
1c93b 61 72 65 6e 74 20 66 72 61 6d 65 2e 20 2a 2f 0a  arent frame. */.
1c93c 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70      VdbeFrame *p
1c93d 46 72 61 6d 65 20 3d 20 70 2d 3e 70 46 72 61 6d  Frame = p->pFram
1c93e 65 3b 0a 20 20 20 20 70 2d 3e 70 46 72 61 6d 65  e;.    p->pFrame
1c93f 20 3d 20 70 46 72 61 6d 65 2d 3e 70 50 61 72 65   = pFrame->pPare
1c940 6e 74 3b 0a 20 20 20 20 70 2d 3e 6e 46 72 61 6d  nt;.    p->nFram
1c941 65 2d 2d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  e--;.    sqlite3
1c942 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 64  VdbeSetChanges(d
1c943 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a  b, p->nChange);.
1c944 20 20 20 20 70 63 20 3d 20 73 71 6c 69 74 65 33      pc = sqlite3
1c945 56 64 62 65 46 72 61 6d 65 52 65 73 74 6f 72 65  VdbeFrameRestore
1c946 28 70 46 72 61 6d 65 29 3b 0a 20 20 20 20 69 66  (pFrame);.    if
1c947 28 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 49 67  ( pOp->p2==OE_Ig
1c948 6e 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a  nore ){.      /*
1c949 20 49 6e 73 74 72 75 63 74 69 6f 6e 20 70 63 20   Instruction pc 
1c94a 69 73 20 74 68 65 20 4f 50 5f 50 72 6f 67 72 61  is the OP_Progra
1c94b 6d 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74  m that invoked t
1c94c 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 0a  he sub-program .
1c94d 20 20 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74        ** current
1c94e 6c 79 20 62 65 69 6e 67 20 68 61 6c 74 65 64 2e  ly being halted.
1c94f 20 49 66 20 74 68 65 20 70 32 20 69 6e 73 74 72   If the p2 instr
1c950 75 63 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 4f  uction of this O
1c951 50 5f 48 61 6c 74 0a 20 20 20 20 20 20 2a 2a 20  P_Halt.      ** 
1c952 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 73  instruction is s
1c953 65 74 20 74 6f 20 4f 45 5f 49 67 6e 6f 72 65 2c  et to OE_Ignore,
1c954 20 74 68 65 6e 20 74 68 65 20 73 75 62 2d 70 72   then the sub-pr
1c955 6f 67 72 61 6d 20 69 73 20 74 68 72 6f 77 69 6e  ogram is throwin
1c956 67 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 49 47  g.      ** an IG
1c957 4e 4f 52 45 20 65 78 63 65 70 74 69 6f 6e 2e 20  NORE exception. 
1c958 49 6e 20 74 68 69 73 20 63 61 73 65 20 6a 75 6d  In this case jum
1c959 70 20 74 6f 20 74 68 65 20 61 64 64 72 65 73 73  p to the address
1c95a 20 73 70 65 63 69 66 69 65 64 0a 20 20 20 20 20   specified.     
1c95b 20 2a 2a 20 61 73 20 74 68 65 20 70 32 20 6f 66   ** as the p2 of
1c95c 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 4f 50 5f   the calling OP_
1c95d 50 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 20  Program.  */.   
1c95e 20 20 20 70 63 20 3d 20 70 2d 3e 61 4f 70 5b 70     pc = p->aOp[p
1c95f 63 5d 2e 70 32 2d 31 3b 0a 20 20 20 20 7d 0a 20  c].p2-1;.    }. 
1c960 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20     break;.  }.. 
1c961 20 70 2d 3e 72 63 20 3d 20 70 4f 70 2d 3e 70 31   p->rc = pOp->p1
1c962 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 69  ;.  p->errorActi
1c963 6f 6e 20 3d 20 28 75 38 29 70 4f 70 2d 3e 70 32  on = (u8)pOp->p2
1c964 3b 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a  ;.  p->pc = pc;.
1c965 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e 7a 20    if( pOp->p4.z 
1c966 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  ){.    sqlite3Se
1c967 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
1c968 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 70  Msg, db, "%s", p
1c969 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 7d 0a 20  Op->p4.z);.  }. 
1c96a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1c96b 65 48 61 6c 74 28 70 29 3b 0a 20 20 61 73 73 65  eHalt(p);.  asse
1c96c 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42  rt( rc==SQLITE_B
1c96d 55 53 59 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54  USY || rc==SQLIT
1c96e 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  E_OK || rc==SQLI
1c96f 54 45 5f 45 52 52 4f 52 20 29 3b 0a 20 20 69 66  TE_ERROR );.  if
1c970 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53  ( rc==SQLITE_BUS
1c971 59 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d  Y ){.    p->rc =
1c972 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
1c973 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  Y;.  }else{.    
1c974 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
1c975 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d  TE_OK || p->rc==
1c976 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
1c977 54 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  T );.    assert(
1c978 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc==SQLITE_OK |
1c979 7c 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 43  | db->nDeferredC
1c97a 6f 6e 73 3e 30 20 29 3b 0a 20 20 20 20 72 63 20  ons>0 );.    rc 
1c97b 3d 20 70 2d 3e 72 63 20 3f 20 53 51 4c 49 54 45  = p->rc ? SQLITE
1c97c 5f 45 52 52 4f 52 20 3a 20 53 51 4c 49 54 45 5f  _ERROR : SQLITE_
1c97d 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 67 6f 74 6f  DONE;.  }.  goto
1c97e 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a   vdbe_return;.}.
1c97f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 74 65  ./* Opcode: Inte
1c980 67 65 72 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ger P1 P2 * * *.
1c981 2a 2a 0a 2a 2a 20 54 68 65 20 33 32 2d 62 69 74  **.** The 32-bit
1c982 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 50   integer value P
1c983 31 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74  1 is written int
1c984 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a  o register P2..*
1c985 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 65 67 65  /.case OP_Intege
1c986 72 3a 20 7b 20 20 20 20 20 20 20 20 20 2f 2a 20  r: {         /* 
1c987 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1c988 2a 2f 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  */.  pOut->flags
1c989 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f   = MEM_Int;.  pO
1c98a 75 74 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70  ut->u.i = pOp->p
1c98b 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  1;.  break;.}../
1c98c 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 74 36 34 20  * Opcode: Int64 
1c98d 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a  * P2 * P4 *.**.*
1c98e 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65  * P4 is a pointe
1c98f 72 20 74 6f 20 61 20 36 34 2d 62 69 74 20 69 6e  r to a 64-bit in
1c990 74 65 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2a 20  teger value..** 
1c991 57 72 69 74 65 20 74 68 61 74 20 76 61 6c 75 65  Write that value
1c992 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   into register P
1c993 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e  2..*/.case OP_In
1c994 74 36 34 3a 20 7b 20 20 20 20 20 20 20 20 20 20  t64: {          
1c995 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65   /* out2-prerele
1c996 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ase */.  assert(
1c997 20 70 4f 70 2d 3e 70 34 2e 70 49 36 34 21 3d 30   pOp->p4.pI64!=0
1c998 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67   );.  pOut->flag
1c999 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70  s = MEM_Int;.  p
1c99a 4f 75 74 2d 3e 75 2e 69 20 3d 20 2a 70 4f 70 2d  Out->u.i = *pOp-
1c99b 3e 70 34 2e 70 49 36 34 3b 0a 20 20 62 72 65 61  >p4.pI64;.  brea
1c99c 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1c99d 20 52 65 61 6c 20 2a 20 50 32 20 2a 20 50 34 20   Real * P2 * P4 
1c99e 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20  *.**.** P4 is a 
1c99f 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 36 34 2d  pointer to a 64-
1c9a0 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  bit floating poi
1c9a1 6e 74 20 76 61 6c 75 65 2e 0a 2a 2a 20 57 72 69  nt value..** Wri
1c9a2 74 65 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e  te that value in
1c9a3 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  to register P2..
1c9a4 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61 6c 3a  */.case OP_Real:
1c9a5 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a   {            /*
1c9a6 20 73 61 6d 65 20 61 73 20 54 4b 5f 46 4c 4f 41   same as TK_FLOA
1c9a7 54 2c 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61  T, out2-prerelea
1c9a8 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d 3e 66 6c  se */.  pOut->fl
1c9a9 61 67 73 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a  ags = MEM_Real;.
1c9aa 20 20 61 73 73 65 72 74 28 20 21 73 71 6c 69 74    assert( !sqlit
1c9ab 65 33 49 73 4e 61 4e 28 2a 70 4f 70 2d 3e 70 34  e3IsNaN(*pOp->p4
1c9ac 2e 70 52 65 61 6c 29 20 29 3b 0a 20 20 70 4f 75  .pReal) );.  pOu
1c9ad 74 2d 3e 72 20 3d 20 2a 70 4f 70 2d 3e 70 34 2e  t->r = *pOp->p4.
1c9ae 70 52 65 61 6c 3b 0a 20 20 62 72 65 61 6b 3b 0a  pReal;.  break;.
1c9af 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 74  }../* Opcode: St
1c9b0 72 69 6e 67 38 20 2a 20 50 32 20 2a 20 50 34 20  ring8 * P2 * P4 
1c9b1 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 70 6f 69 6e 74  *.**.** P4 point
1c9b2 73 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69  s to a nul termi
1c9b3 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69  nated UTF-8 stri
1c9b4 6e 67 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20  ng. This opcode 
1c9b5 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 0a  is transformed .
1c9b6 2a 2a 20 69 6e 74 6f 20 61 6e 20 4f 50 5f 53 74  ** into an OP_St
1c9b7 72 69 6e 67 20 62 65 66 6f 72 65 20 69 74 20 69  ring before it i
1c9b8 73 20 65 78 65 63 75 74 65 64 20 66 6f 72 20 74  s executed for t
1c9b9 68 65 20 66 69 72 73 74 20 74 69 6d 65 2e 0a 2a  he first time..*
1c9ba 2f 0a 63 61 73 65 20 4f 50 5f 53 74 72 69 6e 67  /.case OP_String
1c9bb 38 3a 20 7b 20 20 20 20 20 20 20 20 20 2f 2a 20  8: {         /* 
1c9bc 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 49 4e  same as TK_STRIN
1c9bd 47 2c 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61  G, out2-prerelea
1c9be 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  se */.  assert( 
1c9bf 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a  pOp->p4.z!=0 );.
1c9c0 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20    pOp->opcode = 
1c9c1 4f 50 5f 53 74 72 69 6e 67 3b 0a 20 20 70 4f 70  OP_String;.  pOp
1c9c2 2d 3e 70 31 20 3d 20 73 71 6c 69 74 65 33 53 74  ->p1 = sqlite3St
1c9c3 72 6c 65 6e 33 30 28 70 4f 70 2d 3e 70 34 2e 7a  rlen30(pOp->p4.z
1c9c4 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
1c9c5 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20  TE_OMIT_UTF16.  
1c9c6 69 66 28 20 65 6e 63 6f 64 69 6e 67 21 3d 53 51  if( encoding!=SQ
1c9c7 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20  LITE_UTF8 ){.   
1c9c8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1c9c9 65 4d 65 6d 53 65 74 53 74 72 28 70 4f 75 74 2c  eMemSetStr(pOut,
1c9ca 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 2d 31 2c 20   pOp->p4.z, -1, 
1c9cb 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c  SQLITE_UTF8, SQL
1c9cc 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
1c9cd 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1c9ce 54 4f 4f 42 49 47 20 29 20 67 6f 74 6f 20 74 6f  TOOBIG ) goto to
1c9cf 6f 5f 62 69 67 3b 0a 20 20 20 20 69 66 28 20 53  o_big;.    if( S
1c9d0 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
1c9d1 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64  3VdbeChangeEncod
1c9d2 69 6e 67 28 70 4f 75 74 2c 20 65 6e 63 6f 64 69  ing(pOut, encodi
1c9d3 6e 67 29 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  ng) ) goto no_me
1c9d4 6d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  m;.    assert( p
1c9d5 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d 70 4f  Out->zMalloc==pO
1c9d6 75 74 2d 3e 7a 20 29 3b 0a 20 20 20 20 61 73 73  ut->z );.    ass
1c9d7 65 72 74 28 20 70 4f 75 74 2d 3e 66 6c 61 67 73  ert( pOut->flags
1c9d8 20 26 20 4d 45 4d 5f 44 79 6e 20 29 3b 0a 20 20   & MEM_Dyn );.  
1c9d9 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20    pOut->zMalloc 
1c9da 3d 20 30 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66  = 0;.    pOut->f
1c9db 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 61 74  lags |= MEM_Stat
1c9dc 69 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c  ic;.    pOut->fl
1c9dd 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b  ags &= ~MEM_Dyn;
1c9de 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34  .    if( pOp->p4
1c9df 74 79 70 65 3d 3d 50 34 5f 44 59 4e 41 4d 49 43  type==P4_DYNAMIC
1c9e0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1c9e1 33 44 62 46 72 65 65 28 64 62 2c 20 70 4f 70 2d  3DbFree(db, pOp-
1c9e2 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 7d 0a 20 20  >p4.z);.    }.  
1c9e3 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20    pOp->p4type = 
1c9e4 50 34 5f 44 59 4e 41 4d 49 43 3b 0a 20 20 20 20  P4_DYNAMIC;.    
1c9e5 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 70 4f 75 74  pOp->p4.z = pOut
1c9e6 2d 3e 7a 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 31  ->z;.    pOp->p1
1c9e7 20 3d 20 70 4f 75 74 2d 3e 6e 3b 0a 20 20 7d 0a   = pOut->n;.  }.
1c9e8 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 4f 70  #endif.  if( pOp
1c9e9 2d 3e 70 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b  ->p1>db->aLimit[
1c9ea 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
1c9eb 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f  GTH] ){.    goto
1c9ec 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20   too_big;.  }.  
1c9ed 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20  /* Fall through 
1c9ee 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 65  to the next case
1c9ef 2c 20 4f 50 5f 53 74 72 69 6e 67 20 2a 2f 0a 7d  , OP_String */.}
1c9f0 0a 20 20 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53  .  ./* Opcode: S
1c9f1 74 72 69 6e 67 20 50 31 20 50 32 20 2a 20 50 34  tring P1 P2 * P4
1c9f2 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 72   *.**.** The str
1c9f3 69 6e 67 20 76 61 6c 75 65 20 50 34 20 6f 66 20  ing value P4 of 
1c9f4 6c 65 6e 67 74 68 20 50 31 20 28 62 79 74 65 73  length P1 (bytes
1c9f5 29 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72  ) is stored in r
1c9f6 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63  egister P2..*/.c
1c9f7 61 73 65 20 4f 50 5f 53 74 72 69 6e 67 3a 20 7b  ase OP_String: {
1c9f8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74            /* out
1c9f9 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a  2-prerelease */.
1c9fa 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1c9fb 34 2e 7a 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74  4.z!=0 );.  pOut
1c9fc 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74  ->flags = MEM_St
1c9fd 72 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d  r|MEM_Static|MEM
1c9fe 5f 54 65 72 6d 3b 0a 20 20 70 4f 75 74 2d 3e 7a  _Term;.  pOut->z
1c9ff 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20   = pOp->p4.z;.  
1ca00 70 4f 75 74 2d 3e 6e 20 3d 20 70 4f 70 2d 3e 70  pOut->n = pOp->p
1ca01 31 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d  1;.  pOut->enc =
1ca02 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 50 44   encoding;.  UPD
1ca03 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45  ATE_MAX_BLOBSIZE
1ca04 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b  (pOut);.  break;
1ca05 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e  .}../* Opcode: N
1ca06 75 6c 6c 20 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a  ull * P2 * * *.*
1ca07 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 4e 55 4c  *.** Write a NUL
1ca08 4c 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  L into register 
1ca09 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e  P2..*/.case OP_N
1ca0a 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20  ull: {          
1ca0b 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65   /* out2-prerele
1ca0c 61 73 65 20 2a 2f 0a 20 20 62 72 65 61 6b 3b 0a  ase */.  break;.
1ca0d 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42  }.../* Opcode: B
1ca0e 6c 6f 62 20 50 31 20 50 32 20 2a 20 50 34 0a 2a  lob P1 P2 * P4.*
1ca0f 2a 0a 2a 2a 20 50 34 20 70 6f 69 6e 74 73 20 74  *.** P4 points t
1ca10 6f 20 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61  o a blob of data
1ca11 20 50 31 20 62 79 74 65 73 20 6c 6f 6e 67 2e 20   P1 bytes long. 
1ca12 20 53 74 6f 72 65 20 74 68 69 73 0a 2a 2a 20 62   Store this.** b
1ca13 6c 6f 62 20 69 6e 20 72 65 67 69 73 74 65 72 20  lob in register 
1ca14 50 32 2e 20 54 68 69 73 20 69 6e 73 74 72 75 63  P2. This instruc
1ca15 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 64 65  tion is not code
1ca16 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79  d directly.** by
1ca17 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e 20 49   the compiler. I
1ca18 6e 73 74 65 61 64 2c 20 74 68 65 20 63 6f 6d 70  nstead, the comp
1ca19 69 6c 65 72 20 6c 61 79 65 72 20 73 70 65 63 69  iler layer speci
1ca1a 66 69 65 73 0a 2a 2a 20 61 6e 20 4f 50 5f 48 65  fies.** an OP_He
1ca1b 78 42 6c 6f 62 20 6f 70 63 6f 64 65 2c 20 77 69  xBlob opcode, wi
1ca1c 74 68 20 74 68 65 20 68 65 78 20 73 74 72 69 6e  th the hex strin
1ca1d 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  g representation
1ca1e 20 6f 66 0a 2a 2a 20 74 68 65 20 62 6c 6f 62 20   of.** the blob 
1ca1f 61 73 20 50 34 2e 20 54 68 69 73 20 6f 70 63 6f  as P4. This opco
1ca20 64 65 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65  de is transforme
1ca21 64 20 74 6f 20 61 6e 20 4f 50 5f 42 6c 6f 62 0a  d to an OP_Blob.
1ca22 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 69 6d  ** the first tim
1ca23 65 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64  e it is executed
1ca24 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 6c 6f  ..*/.case OP_Blo
1ca25 62 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  b: {            
1ca26 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72      /* out2-prer
1ca27 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65  elease */.  asse
1ca28 72 74 28 20 70 4f 70 2d 3e 70 31 20 3c 3d 20 53  rt( pOp->p1 <= S
1ca29 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48  QLITE_MAX_LENGTH
1ca2a 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   );.  sqlite3Vdb
1ca2b 65 4d 65 6d 53 65 74 53 74 72 28 70 4f 75 74 2c  eMemSetStr(pOut,
1ca2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 70 4f 70 2d   pOp->p4.z, pOp-
1ca2d 3e 70 31 2c 20 30 2c 20 30 29 3b 0a 20 20 70 4f  >p1, 0, 0);.  pO
1ca2e 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69  ut->enc = encodi
1ca2f 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58  ng;.  UPDATE_MAX
1ca30 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b  _BLOBSIZE(pOut);
1ca31 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1ca32 4f 70 63 6f 64 65 3a 20 56 61 72 69 61 62 6c 65  Opcode: Variable
1ca33 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a   P1 P2 P3 P4 *.*
1ca34 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 74 68  *.** Transfer th
1ca35 65 20 76 61 6c 75 65 73 20 6f 66 20 62 6f 75 6e  e values of boun
1ca36 64 20 70 61 72 61 6d 65 74 65 72 73 20 50 31 2e  d parameters P1.
1ca37 2e 50 31 2b 50 33 2d 31 20 69 6e 74 6f 20 72 65  .P1+P3-1 into re
1ca38 67 69 73 74 65 72 73 0a 2a 2a 20 50 32 2e 2e 50  gisters.** P2..P
1ca39 32 2b 50 33 2d 31 2e 0a 2a 2a 0a 2a 2a 20 49 66  2+P3-1..**.** If
1ca3a 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69   the parameter i
1ca3b 73 20 6e 61 6d 65 64 2c 20 74 68 65 6e 20 69 74  s named, then it
1ca3c 73 20 6e 61 6d 65 20 61 70 70 65 61 72 73 20 69  s name appears i
1ca3d 6e 20 50 34 20 61 6e 64 20 50 33 3d 3d 31 2e 0a  n P4 and P3==1..
1ca3e 2a 2a 20 54 68 65 20 50 34 20 76 61 6c 75 65 20  ** The P4 value 
1ca3f 69 73 20 75 73 65 64 20 62 79 20 73 71 6c 69 74  is used by sqlit
1ca40 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
1ca41 72 5f 6e 61 6d 65 28 29 2e 0a 2a 2f 0a 63 61 73  r_name()..*/.cas
1ca42 65 20 4f 50 5f 56 61 72 69 61 62 6c 65 3a 20 7b  e OP_Variable: {
1ca43 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1ca44 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1ca45 20 69 6e 74 6f 20 75 2e 61 62 20 2a 2f 0a 20 20   into u.ab */.  
1ca46 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20 20  int p1;         
1ca47 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 74 6f 20   /* Variable to 
1ca48 63 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a 20 20 69  copy from */.  i
1ca49 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20 20 20  nt p2;          
1ca4a 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 63  /* Register to c
1ca4b 6f 70 79 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20  opy to */.  int 
1ca4c 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  n;           /* 
1ca4d 4e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73  Number of values
1ca4e 20 6c 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f   left to copy */
1ca4f 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 20 20 20  .  Mem *pVar;   
1ca50 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 62 65 69      /* Value bei
1ca51 6e 67 20 74 72 61 6e 73 66 65 72 72 65 64 20 2a  ng transferred *
1ca52 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  /.#endif /* loca
1ca53 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1ca54 64 20 69 6e 74 6f 20 75 2e 61 62 20 2a 2f 0a 0a  d into u.ab */..
1ca55 20 20 75 2e 61 62 2e 70 31 20 3d 20 70 4f 70 2d    u.ab.p1 = pOp-
1ca56 3e 70 31 20 2d 20 31 3b 0a 20 20 75 2e 61 62 2e  >p1 - 1;.  u.ab.
1ca57 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20  p2 = pOp->p2;.  
1ca58 75 2e 61 62 2e 6e 20 3d 20 70 4f 70 2d 3e 70 33  u.ab.n = pOp->p3
1ca59 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 62  ;.  assert( u.ab
1ca5a 2e 70 31 3e 3d 30 20 26 26 20 75 2e 61 62 2e 70  .p1>=0 && u.ab.p
1ca5b 31 2b 75 2e 61 62 2e 6e 3c 3d 70 2d 3e 6e 56 61  1+u.ab.n<=p->nVa
1ca5c 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75  r );.  assert( u
1ca5d 2e 61 62 2e 70 32 3e 3d 31 20 26 26 20 75 2e 61  .ab.p2>=1 && u.a
1ca5e 62 2e 70 32 2b 75 2e 61 62 2e 6e 2d 31 3c 3d 70  b.p2+u.ab.n-1<=p
1ca5f 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 61 73 73 65  ->nMem );.  asse
1ca60 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30  rt( pOp->p4.z==0
1ca61 20 7c 7c 20 70 4f 70 2d 3e 70 33 3d 3d 31 20 29   || pOp->p3==1 )
1ca62 3b 0a 0a 20 20 77 68 69 6c 65 28 20 75 2e 61 62  ;..  while( u.ab
1ca63 2e 6e 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20  .n-- > 0 ){.    
1ca64 75 2e 61 62 2e 70 56 61 72 20 3d 20 26 70 2d 3e  u.ab.pVar = &p->
1ca65 61 56 61 72 5b 75 2e 61 62 2e 70 31 2b 2b 5d 3b  aVar[u.ab.p1++];
1ca66 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1ca67 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 75 2e  VdbeMemTooBig(u.
1ca68 61 62 2e 70 56 61 72 29 20 29 7b 0a 20 20 20 20  ab.pVar) ){.    
1ca69 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
1ca6a 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 20 3d      }.    pOut =
1ca6b 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 62 2e 70   &p->aMem[u.ab.p
1ca6c 32 2b 2b 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65  2++];.    sqlite
1ca6d 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45  3VdbeMemReleaseE
1ca6e 78 74 65 72 6e 61 6c 28 70 4f 75 74 29 3b 0a 20  xternal(pOut);. 
1ca6f 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d     pOut->flags =
1ca70 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 73   MEM_Null;.    s
1ca71 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61  qlite3VdbeMemSha
1ca72 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 2c 20 75  llowCopy(pOut, u
1ca73 2e 61 62 2e 70 56 61 72 2c 20 4d 45 4d 5f 53 74  .ab.pVar, MEM_St
1ca74 61 74 69 63 29 3b 0a 20 20 20 20 55 50 44 41 54  atic);.    UPDAT
1ca75 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70  E_MAX_BLOBSIZE(p
1ca76 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61  Out);.  }.  brea
1ca77 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1ca78 20 4d 6f 76 65 20 50 31 20 50 32 20 50 33 20 2a   Move P1 P2 P3 *
1ca79 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68   *.**.** Move th
1ca7a 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69  e values in regi
1ca7b 73 74 65 72 20 50 31 2e 2e 50 31 2b 50 33 2d 31  ster P1..P1+P3-1
1ca7c 20 6f 76 65 72 20 69 6e 74 6f 0a 2a 2a 20 72 65   over into.** re
1ca7d 67 69 73 74 65 72 73 20 50 32 2e 2e 50 32 2b 50  gisters P2..P2+P
1ca7e 33 2d 31 2e 20 20 52 65 67 69 73 74 65 72 73 20  3-1.  Registers 
1ca7f 50 31 2e 2e 50 31 2b 50 31 2d 31 20 61 72 65 0a  P1..P1+P1-1 are.
1ca80 2a 2a 20 6c 65 66 74 20 68 6f 6c 64 69 6e 67 20  ** left holding 
1ca81 61 20 4e 55 4c 4c 2e 20 20 49 74 20 69 73 20 61  a NULL.  It is a
1ca82 6e 20 65 72 72 6f 72 20 66 6f 72 20 72 65 67 69  n error for regi
1ca83 73 74 65 72 20 72 61 6e 67 65 73 0a 2a 2a 20 50  ster ranges.** P
1ca84 31 2e 2e 50 31 2b 50 33 2d 31 20 61 6e 64 20 50  1..P1+P3-1 and P
1ca85 32 2e 2e 50 32 2b 50 33 2d 31 20 74 6f 20 6f 76  2..P2+P3-1 to ov
1ca86 65 72 6c 61 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f  erlap..*/.case O
1ca87 50 5f 4d 6f 76 65 3a 20 7b 0a 23 69 66 20 30 20  P_Move: {.#if 0 
1ca88 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1ca89 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1ca8a 2e 61 63 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  .ac */.  char *z
1ca8b 4d 61 6c 6c 6f 63 3b 20 20 20 2f 2a 20 48 6f 6c  Malloc;   /* Hol
1ca8c 64 69 6e 67 20 76 61 72 69 61 62 6c 65 20 66 6f  ding variable fo
1ca8d 72 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f  r allocated memo
1ca8e 72 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20  ry */.  int n;  
1ca8f 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1ca90 65 72 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  er of registers 
1ca91 6c 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a  left to copy */.
1ca92 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20    int p1;       
1ca93 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74     /* Register t
1ca94 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a 20  o copy from */. 
1ca95 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20   int p2;        
1ca96 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f    /* Register to
1ca97 20 63 6f 70 79 20 74 6f 20 2a 2f 0a 23 65 6e 64   copy to */.#end
1ca98 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1ca99 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1ca9a 20 75 2e 61 63 20 2a 2f 0a 0a 20 20 75 2e 61 63   u.ac */..  u.ac
1ca9b 2e 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20  .n = pOp->p3;.  
1ca9c 75 2e 61 63 2e 70 31 20 3d 20 70 4f 70 2d 3e 70  u.ac.p1 = pOp->p
1ca9d 31 3b 0a 20 20 75 2e 61 63 2e 70 32 20 3d 20 70  1;.  u.ac.p2 = p
1ca9e 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74  Op->p2;.  assert
1ca9f 28 20 75 2e 61 63 2e 6e 3e 30 20 26 26 20 75 2e  ( u.ac.n>0 && u.
1caa0 61 63 2e 70 31 3e 30 20 26 26 20 75 2e 61 63 2e  ac.p1>0 && u.ac.
1caa1 70 32 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74  p2>0 );.  assert
1caa2 28 20 75 2e 61 63 2e 70 31 2b 75 2e 61 63 2e 6e  ( u.ac.p1+u.ac.n
1caa3 3c 3d 75 2e 61 63 2e 70 32 20 7c 7c 20 75 2e 61  <=u.ac.p2 || u.a
1caa4 63 2e 70 32 2b 75 2e 61 63 2e 6e 3c 3d 75 2e 61  c.p2+u.ac.n<=u.a
1caa5 63 2e 70 31 20 29 3b 0a 0a 20 20 70 49 6e 31 20  c.p1 );..  pIn1 
1caa6 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 63 2e  = &p->aMem[u.ac.
1caa7 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70  p1];.  pOut = &p
1caa8 2d 3e 61 4d 65 6d 5b 75 2e 61 63 2e 70 32 5d 3b  ->aMem[u.ac.p2];
1caa9 0a 20 20 77 68 69 6c 65 28 20 75 2e 61 63 2e 6e  .  while( u.ac.n
1caaa 2d 2d 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  -- ){.    assert
1caab 28 20 70 4f 75 74 3c 3d 26 70 2d 3e 61 4d 65 6d  ( pOut<=&p->aMem
1caac 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 20 20  [p->nMem] );.   
1caad 20 61 73 73 65 72 74 28 20 70 49 6e 31 3c 3d 26   assert( pIn1<=&
1caae 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d  p->aMem[p->nMem]
1caaf 20 29 3b 0a 20 20 20 20 75 2e 61 63 2e 7a 4d 61   );.    u.ac.zMa
1cab0 6c 6c 6f 63 20 3d 20 70 4f 75 74 2d 3e 7a 4d 61  lloc = pOut->zMa
1cab1 6c 6c 6f 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e  lloc;.    pOut->
1cab2 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20  zMalloc = 0;.   
1cab3 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
1cab4 6f 76 65 28 70 4f 75 74 2c 20 70 49 6e 31 29 3b  ove(pOut, pIn1);
1cab5 0a 20 20 20 20 70 49 6e 31 2d 3e 7a 4d 61 6c 6c  .    pIn1->zMall
1cab6 6f 63 20 3d 20 75 2e 61 63 2e 7a 4d 61 6c 6c 6f  oc = u.ac.zMallo
1cab7 63 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f  c;.    REGISTER_
1cab8 54 52 41 43 45 28 75 2e 61 63 2e 70 32 2b 2b 2c  TRACE(u.ac.p2++,
1cab9 20 70 4f 75 74 29 3b 0a 20 20 20 20 70 49 6e 31   pOut);.    pIn1
1caba 2b 2b 3b 0a 20 20 20 20 70 4f 75 74 2b 2b 3b 0a  ++;.    pOut++;.
1cabb 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1cabc 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 70 79 20  /* Opcode: Copy 
1cabd 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1cabe 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66  * Make a copy of
1cabf 20 72 65 67 69 73 74 65 72 20 50 31 20 69 6e 74   register P1 int
1cac0 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a  o register P2..*
1cac1 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75  *.** This instru
1cac2 63 74 69 6f 6e 20 6d 61 6b 65 73 20 61 20 64 65  ction makes a de
1cac3 65 70 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76  ep copy of the v
1cac4 61 6c 75 65 2e 20 20 41 20 64 75 70 6c 69 63 61  alue.  A duplica
1cac5 74 65 0a 2a 2a 20 69 73 20 6d 61 64 65 20 6f 66  te.** is made of
1cac6 20 61 6e 79 20 73 74 72 69 6e 67 20 6f 72 20 62   any string or b
1cac7 6c 6f 62 20 63 6f 6e 73 74 61 6e 74 2e 20 20 53  lob constant.  S
1cac8 65 65 20 61 6c 73 6f 20 4f 50 5f 53 43 6f 70 79  ee also OP_SCopy
1cac9 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 70  ..*/.case OP_Cop
1caca 79 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  y: {            
1cacb 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73   /* in1 */.  ass
1cacc 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29  ert( pOp->p2>0 )
1cacd 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1cace 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p2<=p->nMem );.
1cacf 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65    pOut = &p->aMe
1cad0 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73  m[pOp->p2];.  as
1cad1 73 65 72 74 28 20 70 4f 75 74 21 3d 70 49 6e 31  sert( pOut!=pIn1
1cad2 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   );.  sqlite3Vdb
1cad3 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28  eMemShallowCopy(
1cad4 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45 4d 5f  pOut, pIn1, MEM_
1cad5 45 70 68 65 6d 29 3b 0a 20 20 44 65 65 70 68 65  Ephem);.  Deephe
1cad6 6d 65 72 61 6c 69 7a 65 28 70 4f 75 74 29 3b 0a  meralize(pOut);.
1cad7 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1cad8 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b  (pOp->p2, pOut);
1cad9 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1cada 4f 70 63 6f 64 65 3a 20 53 43 6f 70 79 20 50 31  Opcode: SCopy P1
1cadb 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1cadc 4d 61 6b 65 20 61 20 73 68 61 6c 6c 6f 77 20 63  Make a shallow c
1cadd 6f 70 79 20 6f 66 20 72 65 67 69 73 74 65 72 20  opy of register 
1cade 50 31 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  P1 into register
1cadf 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20   P2..**.** This 
1cae0 69 6e 73 74 72 75 63 74 69 6f 6e 20 6d 61 6b 65  instruction make
1cae1 73 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79  s a shallow copy
1cae2 20 6f 66 20 74 68 65 20 76 61 6c 75 65 2e 20 20   of the value.  
1cae3 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20  If the value.** 
1cae4 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62  is a string or b
1cae5 6c 6f 62 2c 20 74 68 65 6e 20 74 68 65 20 63 6f  lob, then the co
1cae6 70 79 20 69 73 20 6f 6e 6c 79 20 61 20 70 6f 69  py is only a poi
1cae7 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 6f  nter to the.** o
1cae8 72 69 67 69 6e 61 6c 20 61 6e 64 20 68 65 6e 63  riginal and henc
1cae9 65 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61  e if the origina
1caea 6c 20 63 68 61 6e 67 65 73 20 73 6f 20 77 69 6c  l changes so wil
1caeb 6c 20 74 68 65 20 63 6f 70 79 2e 0a 2a 2a 20 57  l the copy..** W
1caec 6f 72 73 65 2c 20 69 66 20 74 68 65 20 6f 72 69  orse, if the ori
1caed 67 69 6e 61 6c 20 69 73 20 64 65 61 6c 6c 6f 63  ginal is dealloc
1caee 61 74 65 64 2c 20 74 68 65 20 63 6f 70 79 20 62  ated, the copy b
1caef 65 63 6f 6d 65 73 20 69 6e 76 61 6c 69 64 2e 0a  ecomes invalid..
1caf0 2a 2a 20 54 68 75 73 20 74 68 65 20 70 72 6f 67  ** Thus the prog
1caf1 72 61 6d 20 6d 75 73 74 20 67 75 61 72 61 6e 74  ram must guarant
1caf2 65 65 20 74 68 61 74 20 74 68 65 20 6f 72 69 67  ee that the orig
1caf3 69 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 63 68  inal will not ch
1caf4 61 6e 67 65 0a 2a 2a 20 64 75 72 69 6e 67 20 74  ange.** during t
1caf5 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20 74  he lifetime of t
1caf6 68 65 20 63 6f 70 79 2e 20 20 55 73 65 20 4f 50  he copy.  Use OP
1caf7 5f 43 6f 70 79 20 74 6f 20 6d 61 6b 65 20 61 20  _Copy to make a 
1caf8 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63 6f 70 79  complete.** copy
1caf9 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 43 6f  ..*/.case OP_SCo
1cafa 70 79 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  py: {           
1cafb 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 52 45 47   /* in1 */.  REG
1cafc 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1cafd 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 61 73  >p1, pIn1);.  as
1cafe 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20  sert( pOp->p2>0 
1caff 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
1cb00 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p2<=p->nMem );
1cb01 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d  .  pOut = &p->aM
1cb02 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61  em[pOp->p2];.  a
1cb03 73 73 65 72 74 28 20 70 4f 75 74 21 3d 70 49 6e  ssert( pOut!=pIn
1cb04 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  1 );.  sqlite3Vd
1cb05 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79  beMemShallowCopy
1cb06 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45 4d  (pOut, pIn1, MEM
1cb07 5f 45 70 68 65 6d 29 3b 0a 20 20 52 45 47 49 53  _Ephem);.  REGIS
1cb08 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1cb09 32 2c 20 70 4f 75 74 29 3b 0a 20 20 62 72 65 61  2, pOut);.  brea
1cb0a 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1cb0b 20 52 65 73 75 6c 74 52 6f 77 20 50 31 20 50 32   ResultRow P1 P2
1cb0c 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65   * * *.**.** The
1cb0d 20 72 65 67 69 73 74 65 72 73 20 50 31 20 74 68   registers P1 th
1cb0e 72 6f 75 67 68 20 50 31 2b 50 32 2d 31 20 63 6f  rough P1+P2-1 co
1cb0f 6e 74 61 69 6e 20 61 20 73 69 6e 67 6c 65 20 72  ntain a single r
1cb10 6f 77 20 6f 66 0a 2a 2a 20 72 65 73 75 6c 74 73  ow of.** results
1cb11 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 63 61  . This opcode ca
1cb12 75 73 65 73 20 74 68 65 20 73 71 6c 69 74 65 33  uses the sqlite3
1cb13 5f 73 74 65 70 28 29 20 63 61 6c 6c 20 74 6f 20  _step() call to 
1cb14 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 77 69 74  terminate.** wit
1cb15 68 20 61 6e 20 53 51 4c 49 54 45 5f 52 4f 57 20  h an SQLITE_ROW 
1cb16 72 65 74 75 72 6e 20 63 6f 64 65 20 61 6e 64 20  return code and 
1cb17 69 74 20 73 65 74 73 20 75 70 20 74 68 65 20 73  it sets up the s
1cb18 71 6c 69 74 65 33 5f 73 74 6d 74 0a 2a 2a 20 73  qlite3_stmt.** s
1cb19 74 72 75 63 74 75 72 65 20 74 6f 20 70 72 6f 76  tructure to prov
1cb1a 69 64 65 20 61 63 63 65 73 73 20 74 6f 20 74 68  ide access to th
1cb1b 65 20 74 6f 70 20 50 31 20 76 61 6c 75 65 73 20  e top P1 values 
1cb1c 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a  as the result.**
1cb1d 20 72 6f 77 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50   row..*/.case OP
1cb1e 5f 52 65 73 75 6c 74 52 6f 77 3a 20 7b 0a 23 69  _ResultRow: {.#i
1cb1f 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1cb20 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1cb21 74 6f 20 75 2e 61 64 20 2a 2f 0a 20 20 4d 65 6d  to u.ad */.  Mem
1cb22 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e 74 20 69 3b   *pMem;.  int i;
1cb23 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1cb24 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1cb25 20 69 6e 74 6f 20 75 2e 61 64 20 2a 2f 0a 20 20   into u.ad */.  
1cb26 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 73 43  assert( p->nResC
1cb27 6f 6c 75 6d 6e 3d 3d 70 4f 70 2d 3e 70 32 20 29  olumn==pOp->p2 )
1cb28 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1cb29 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  >p1>0 );.  asser
1cb2a 74 28 20 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e  t( pOp->p1+pOp->
1cb2b 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b  p2<=p->nMem+1 );
1cb2c 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 73  ..  /* If this s
1cb2d 74 61 74 65 6d 65 6e 74 20 68 61 73 20 76 69 6f  tatement has vio
1cb2e 6c 61 74 65 64 20 69 6d 6d 65 64 69 61 74 65 20  lated immediate 
1cb2f 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
1cb30 74 72 61 69 6e 74 73 2c 20 64 6f 0a 20 20 2a 2a  traints, do.  **
1cb31 20 6e 6f 74 20 72 65 74 75 72 6e 20 74 68 65 20   not return the 
1cb32 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 6d  number of rows m
1cb33 6f 64 69 66 69 65 64 2e 20 41 6e 64 20 64 6f 20  odified. And do 
1cb34 6e 6f 74 20 52 45 4c 45 41 53 45 20 74 68 65 20  not RELEASE the 
1cb35 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 74  statement.  ** t
1cb36 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 74 20 6e  ransaction. It n
1cb37 65 65 64 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65  eeds to be rolle
1cb38 64 20 62 61 63 6b 2e 20 20 2a 2f 0a 20 20 69 66  d back.  */.  if
1cb39 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
1cb3a 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 68   = sqlite3VdbeCh
1cb3b 65 63 6b 46 6b 28 70 2c 20 30 29 29 20 29 7b 0a  eckFk(p, 0)) ){.
1cb3c 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
1cb3d 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 43 6f 75  flags&SQLITE_Cou
1cb3e 6e 74 52 6f 77 73 20 29 3b 0a 20 20 20 20 61 73  ntRows );.    as
1cb3f 73 65 72 74 28 20 70 2d 3e 75 73 65 73 53 74 6d  sert( p->usesStm
1cb40 74 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 20 20  tJournal );.    
1cb41 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  break;.  }..  /*
1cb42 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43   If the SQLITE_C
1cb43 6f 75 6e 74 52 6f 77 73 20 66 6c 61 67 20 69 73  ountRows flag is
1cb44 20 73 65 74 20 69 6e 20 73 71 6c 69 74 65 33 2e   set in sqlite3.
1cb45 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74 68 65 6e  flags mask, then
1cb46 0a 20 20 2a 2a 20 44 4d 4c 20 73 74 61 74 65 6d  .  ** DML statem
1cb47 65 6e 74 73 20 69 6e 76 6f 6b 65 20 74 68 69 73  ents invoke this
1cb48 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 74 75 72   opcode to retur
1cb49 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
1cb4a 72 6f 77 73 0a 20 20 2a 2a 20 6d 6f 64 69 66 69  rows.  ** modifi
1cb4b 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20  ed to the user. 
1cb4c 54 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79  This is the only
1cb4d 20 77 61 79 20 74 68 61 74 20 61 20 56 4d 20 74   way that a VM t
1cb4e 68 61 74 0a 20 20 2a 2a 20 6f 70 65 6e 73 20 61  hat.  ** opens a
1cb4f 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
1cb50 61 63 74 69 6f 6e 20 6d 61 79 20 69 6e 76 6f 6b  action may invok
1cb51 65 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a 20  e this opcode.. 
1cb52 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 63 61 73 65   **.  ** In case
1cb53 20 74 68 69 73 20 69 73 20 73 75 63 68 20 61 20   this is such a 
1cb54 73 74 61 74 65 6d 65 6e 74 2c 20 63 6c 6f 73 65  statement, close
1cb55 20 61 6e 79 20 73 74 61 74 65 6d 65 6e 74 20 74   any statement t
1cb56 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20  ransaction.  ** 
1cb57 6f 70 65 6e 65 64 20 62 79 20 74 68 69 73 20 56  opened by this V
1cb58 4d 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  M before returni
1cb59 6e 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68  ng control to th
1cb5a 65 20 75 73 65 72 2e 20 54 68 69 73 20 69 73 20  e user. This is 
1cb5b 74 6f 0a 20 20 2a 2a 20 65 6e 73 75 72 65 20 74  to.  ** ensure t
1cb5c 68 61 74 20 73 74 61 74 65 6d 65 6e 74 2d 74 72  hat statement-tr
1cb5d 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 61  ansactions are a
1cb5e 6c 77 61 79 73 20 6e 65 73 74 65 64 2c 20 6e 6f  lways nested, no
1cb5f 74 20 6f 76 65 72 6c 61 70 70 69 6e 67 2e 0a 20  t overlapping.. 
1cb60 20 2a 2a 20 49 66 20 74 68 65 20 6f 70 65 6e 20   ** If the open 
1cb61 73 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61  statement-transa
1cb62 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6c 6f  ction is not clo
1cb63 73 65 64 20 68 65 72 65 2c 20 74 68 65 6e 20 74  sed here, then t
1cb64 68 65 20 75 73 65 72 0a 20 20 2a 2a 20 6d 61 79  he user.  ** may
1cb65 20 73 74 65 70 20 61 6e 6f 74 68 65 72 20 56 4d   step another VM
1cb66 20 74 68 61 74 20 6f 70 65 6e 73 20 69 74 73 20   that opens its 
1cb67 6f 77 6e 20 73 74 61 74 65 6d 65 6e 74 20 74 72  own statement tr
1cb68 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 0a  ansaction. This.
1cb69 20 20 2a 2a 20 6d 61 79 20 6c 65 61 64 20 74 6f    ** may lead to
1cb6a 20 6f 76 65 72 6c 61 70 70 69 6e 67 20 73 74 61   overlapping sta
1cb6b 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
1cb6c 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  ons..  **.  ** T
1cb6d 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61  he statement tra
1cb6e 6e 73 61 63 74 69 6f 6e 20 69 73 20 6e 65 76 65  nsaction is neve
1cb6f 72 20 61 20 74 6f 70 2d 6c 65 76 65 6c 20 74 72  r a top-level tr
1cb70 61 6e 73 61 63 74 69 6f 6e 2e 20 20 48 65 6e 63  ansaction.  Henc
1cb71 65 0a 20 20 2a 2a 20 74 68 65 20 52 45 4c 45 41  e.  ** the RELEA
1cb72 53 45 20 63 61 6c 6c 20 62 65 6c 6f 77 20 63 61  SE call below ca
1cb73 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e 0a 20 20  n never fail..  
1cb74 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  */.  assert( p->
1cb75 69 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20 7c 7c  iStatement==0 ||
1cb76 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54   db->flags&SQLIT
1cb77 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 3b 0a 20  E_CountRows );. 
1cb78 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1cb79 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 28  eCloseStatement(
1cb7a 70 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c  p, SAVEPOINT_REL
1cb7b 45 41 53 45 29 3b 0a 20 20 69 66 28 20 4e 45 56  EASE);.  if( NEV
1cb7c 45 52 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  ER(rc!=SQLITE_OK
1cb7d 29 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a  ) ){.    break;.
1cb7e 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 76 61 6c 69    }..  /* Invali
1cb7f 64 61 74 65 20 61 6c 6c 20 65 70 68 65 6d 65 72  date all ephemer
1cb80 61 6c 20 63 75 72 73 6f 72 20 72 6f 77 20 63 61  al cursor row ca
1cb81 63 68 65 73 20 2a 2f 0a 20 20 70 2d 3e 63 61 63  ches */.  p->cac
1cb82 68 65 43 74 72 20 3d 20 28 70 2d 3e 63 61 63 68  heCtr = (p->cach
1cb83 65 43 74 72 20 2b 20 32 29 7c 31 3b 0a 0a 20 20  eCtr + 2)|1;..  
1cb84 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
1cb85 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20   results of the 
1cb86 63 75 72 72 65 6e 74 20 72 6f 77 20 61 72 65 20  current row are 
1cb87 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 0a  \000 terminated.
1cb88 20 20 2a 2a 20 61 6e 64 20 68 61 76 65 20 61 6e    ** and have an
1cb89 20 61 73 73 69 67 6e 65 64 20 74 79 70 65 2e 20   assigned type. 
1cb8a 20 54 68 65 20 72 65 73 75 6c 74 73 20 61 72 65   The results are
1cb8b 20 64 65 2d 65 70 68 65 6d 65 72 61 6c 69 7a 65   de-ephemeralize
1cb8c 64 20 61 73 0a 20 20 2a 2a 20 61 73 20 73 69 64  d as.  ** as sid
1cb8d 65 20 65 66 66 65 63 74 2e 0a 20 20 2a 2f 0a 20  e effect..  */. 
1cb8e 20 75 2e 61 64 2e 70 4d 65 6d 20 3d 20 70 2d 3e   u.ad.pMem = p->
1cb8f 70 52 65 73 75 6c 74 53 65 74 20 3d 20 26 70 2d  pResultSet = &p-
1cb90 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  >aMem[pOp->p1];.
1cb91 20 20 66 6f 72 28 75 2e 61 64 2e 69 3d 30 3b 20    for(u.ad.i=0; 
1cb92 75 2e 61 64 2e 69 3c 70 4f 70 2d 3e 70 32 3b 20  u.ad.i<pOp->p2; 
1cb93 75 2e 61 64 2e 69 2b 2b 29 7b 0a 20 20 20 20 73  u.ad.i++){.    s
1cb94 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c  qlite3VdbeMemNul
1cb95 54 65 72 6d 69 6e 61 74 65 28 26 75 2e 61 64 2e  Terminate(&u.ad.
1cb96 70 4d 65 6d 5b 75 2e 61 64 2e 69 5d 29 3b 0a 20  pMem[u.ad.i]);. 
1cb97 20 20 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f     storeTypeInfo
1cb98 28 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e 61 64  (&u.ad.pMem[u.ad
1cb99 2e 69 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a  .i], encoding);.
1cb9a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41      REGISTER_TRA
1cb9b 43 45 28 70 4f 70 2d 3e 70 31 2b 75 2e 61 64 2e  CE(pOp->p1+u.ad.
1cb9c 69 2c 20 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e  i, &u.ad.pMem[u.
1cb9d 61 64 2e 69 5d 29 3b 0a 20 20 7d 0a 20 20 69 66  ad.i]);.  }.  if
1cb9e 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
1cb9f 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  ed ) goto no_mem
1cba0 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 53  ;..  /* Return S
1cba1 51 4c 49 54 45 5f 52 4f 57 0a 20 20 2a 2f 0a 20  QLITE_ROW.  */. 
1cba2 20 70 2d 3e 70 63 20 3d 20 70 63 20 2b 20 31 3b   p->pc = pc + 1;
1cba3 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52  .  rc = SQLITE_R
1cba4 4f 57 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f  OW;.  goto vdbe_
1cba5 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70  return;.}../* Op
1cba6 63 6f 64 65 3a 20 43 6f 6e 63 61 74 20 50 31 20  code: Concat P1 
1cba7 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  P2 P3 * *.**.** 
1cba8 41 64 64 20 74 68 65 20 74 65 78 74 20 69 6e 20  Add the text in 
1cba9 72 65 67 69 73 74 65 72 20 50 31 20 6f 6e 74 6f  register P1 onto
1cbaa 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
1cbab 74 65 78 74 20 69 6e 0a 2a 2a 20 72 65 67 69 73  text in.** regis
1cbac 74 65 72 20 50 32 20 61 6e 64 20 73 74 6f 72 65  ter P2 and store
1cbad 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72   the result in r
1cbae 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49  egister P3..** I
1cbaf 66 20 65 69 74 68 65 72 20 74 68 65 20 50 31 20  f either the P1 
1cbb0 6f 72 20 50 32 20 74 65 78 74 20 61 72 65 20 4e  or P2 text are N
1cbb1 55 4c 4c 20 74 68 65 6e 20 73 74 6f 72 65 20 4e  ULL then store N
1cbb2 55 4c 4c 20 69 6e 20 50 33 2e 0a 2a 2a 0a 2a 2a  ULL in P3..**.**
1cbb3 20 20 20 50 33 20 3d 20 50 32 20 7c 7c 20 50 31     P3 = P2 || P1
1cbb4 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c  .**.** It is ill
1cbb5 65 67 61 6c 20 66 6f 72 20 50 31 20 61 6e 64 20  egal for P1 and 
1cbb6 50 33 20 74 6f 20 62 65 20 74 68 65 20 73 61 6d  P3 to be the sam
1cbb7 65 20 72 65 67 69 73 74 65 72 2e 20 53 6f 6d 65  e register. Some
1cbb8 74 69 6d 65 73 2c 0a 2a 2a 20 69 66 20 50 33 20  times,.** if P3 
1cbb9 69 73 20 74 68 65 20 73 61 6d 65 20 72 65 67 69  is the same regi
1cbba 73 74 65 72 20 61 73 20 50 32 2c 20 74 68 65 20  ster as P2, the 
1cbbb 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69  implementation i
1cbbc 73 20 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 76 6f  s able.** to avo
1cbbd 69 64 20 61 20 6d 65 6d 63 70 79 28 29 2e 0a 2a  id a memcpy()..*
1cbbe 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6e 63 61 74  /.case OP_Concat
1cbbf 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  : {           /*
1cbc0 20 73 61 6d 65 20 61 73 20 54 4b 5f 43 4f 4e 43   same as TK_CONC
1cbc1 41 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75  AT, in1, in2, ou
1cbc2 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20  t3 */.#if 0  /* 
1cbc3 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1cbc4 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 65 20  moved into u.ae 
1cbc5 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74 65 3b 0a  */.  i64 nByte;.
1cbc6 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1cbc7 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1cbc8 69 6e 74 6f 20 75 2e 61 65 20 2a 2f 0a 0a 20 20  into u.ae */..  
1cbc9 61 73 73 65 72 74 28 20 70 49 6e 31 21 3d 70 4f  assert( pIn1!=pO
1cbca 75 74 20 29 3b 0a 20 20 69 66 28 20 28 70 49 6e  ut );.  if( (pIn
1cbcb 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d  1->flags | pIn2-
1cbcc 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d 5f 4e 75  >flags) & MEM_Nu
1cbcd 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ll ){.    sqlite
1cbce 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28  3VdbeMemSetNull(
1cbcf 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 65 61 6b  pOut);.    break
1cbd0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 45 78 70 61  ;.  }.  if( Expa
1cbd1 6e 64 42 6c 6f 62 28 70 49 6e 31 29 20 7c 7c 20  ndBlob(pIn1) || 
1cbd2 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 32 29  ExpandBlob(pIn2)
1cbd3 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
1cbd4 20 20 53 74 72 69 6e 67 69 66 79 28 70 49 6e 31    Stringify(pIn1
1cbd5 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 53  , encoding);.  S
1cbd6 74 72 69 6e 67 69 66 79 28 70 49 6e 32 2c 20 65  tringify(pIn2, e
1cbd7 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 75 2e 61 65  ncoding);.  u.ae
1cbd8 2e 6e 42 79 74 65 20 3d 20 70 49 6e 31 2d 3e 6e  .nByte = pIn1->n
1cbd9 20 2b 20 70 49 6e 32 2d 3e 6e 3b 0a 20 20 69 66   + pIn2->n;.  if
1cbda 28 20 75 2e 61 65 2e 6e 42 79 74 65 3e 64 62 2d  ( u.ae.nByte>db-
1cbdb 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
1cbdc 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a  IMIT_LENGTH] ){.
1cbdd 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67      goto too_big
1cbde 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79  ;.  }.  MemSetTy
1cbdf 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
1cbe0 5f 53 74 72 29 3b 0a 20 20 69 66 28 20 73 71 6c  _Str);.  if( sql
1cbe1 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
1cbe2 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e 61 65 2e  pOut, (int)u.ae.
1cbe3 6e 42 79 74 65 2b 32 2c 20 70 4f 75 74 3d 3d 70  nByte+2, pOut==p
1cbe4 49 6e 32 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  In2) ){.    goto
1cbe5 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 69   no_mem;.  }.  i
1cbe6 66 28 20 70 4f 75 74 21 3d 70 49 6e 32 20 29 7b  f( pOut!=pIn2 ){
1cbe7 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4f 75 74  .    memcpy(pOut
1cbe8 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 7a 2c 20 70 49  ->z, pIn2->z, pI
1cbe9 6e 32 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20 6d 65  n2->n);.  }.  me
1cbea 6d 63 70 79 28 26 70 4f 75 74 2d 3e 7a 5b 70 49  mcpy(&pOut->z[pI
1cbeb 6e 32 2d 3e 6e 5d 2c 20 70 49 6e 31 2d 3e 7a 2c  n2->n], pIn1->z,
1cbec 20 70 49 6e 31 2d 3e 6e 29 3b 0a 20 20 70 4f 75   pIn1->n);.  pOu
1cbed 74 2d 3e 7a 5b 75 2e 61 65 2e 6e 42 79 74 65 5d  t->z[u.ae.nByte]
1cbee 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 7a 5b   = 0;.  pOut->z[
1cbef 75 2e 61 65 2e 6e 42 79 74 65 2b 31 5d 20 3d 20  u.ae.nByte+1] = 
1cbf0 30 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  0;.  pOut->flags
1cbf1 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20   |= MEM_Term;.  
1cbf2 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 29 75  pOut->n = (int)u
1cbf3 2e 61 65 2e 6e 42 79 74 65 3b 0a 20 20 70 4f 75  .ae.nByte;.  pOu
1cbf4 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e  t->enc = encodin
1cbf5 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f  g;.  UPDATE_MAX_
1cbf6 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a  BLOBSIZE(pOut);.
1cbf7 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1cbf8 70 63 6f 64 65 3a 20 41 64 64 20 50 31 20 50 32  pcode: Add P1 P2
1cbf9 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64   P3 * *.**.** Ad
1cbfa 64 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  d the value in r
1cbfb 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 74 68  egister P1 to th
1cbfc 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1cbfd 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74  ter P2.** and st
1cbfe 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ore the result i
1cbff 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  n register P3..*
1cc00 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75  * If either inpu
1cc01 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72  t is NULL, the r
1cc02 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a  esult is NULL..*
1cc03 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 6c  /./* Opcode: Mul
1cc04 74 69 70 6c 79 20 50 31 20 50 32 20 50 33 20 2a  tiply P1 P2 P3 *
1cc05 20 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 75 6c 74   *.**.**.** Mult
1cc06 69 70 6c 79 20 74 68 65 20 76 61 6c 75 65 20 69  iply the value i
1cc07 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 62 79  n register P1 by
1cc08 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1cc09 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64  gister P2.** and
1cc0a 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
1cc0b 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  t in register P3
1cc0c 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69  ..** If either i
1cc0d 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68  nput is NULL, th
1cc0e 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c  e result is NULL
1cc0f 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1cc10 53 75 62 74 72 61 63 74 20 50 31 20 50 32 20 50  Subtract P1 P2 P
1cc11 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 75 62 74  3 * *.**.** Subt
1cc12 72 61 63 74 20 74 68 65 20 76 61 6c 75 65 20 69  ract the value i
1cc13 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 66 72  n register P1 fr
1cc14 6f 6d 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  om the value in 
1cc15 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61  register P2.** a
1cc16 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  nd store the res
1cc17 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20  ult in register 
1cc18 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  P3..** If either
1cc19 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20   input is NULL, 
1cc1a 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55  the result is NU
1cc1b 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  LL..*/./* Opcode
1cc1c 3a 20 44 69 76 69 64 65 20 50 31 20 50 32 20 50  : Divide P1 P2 P
1cc1d 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 69 76 69  3 * *.**.** Divi
1cc1e 64 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  de the value in 
1cc1f 72 65 67 69 73 74 65 72 20 50 31 20 62 79 20 74  register P1 by t
1cc20 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1cc21 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73  ster P2.** and s
1cc22 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
1cc23 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 28  in register P3 (
1cc24 50 33 3d 50 32 2f 50 31 29 2e 20 49 66 20 74 68  P3=P2/P1). If th
1cc25 65 20 76 61 6c 75 65 20 69 6e 20 0a 2a 2a 20 72  e value in .** r
1cc26 65 67 69 73 74 65 72 20 50 31 20 69 73 20 7a 65  egister P1 is ze
1cc27 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73  ro, then the res
1cc28 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 20 49 66 20  ult is NULL. If 
1cc29 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20  either input is 
1cc2a 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65  .** NULL, the re
1cc2b 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f  sult is NULL..*/
1cc2c 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 6d 61  ./* Opcode: Rema
1cc2d 69 6e 64 65 72 20 50 31 20 50 32 20 50 33 20 2a  inder P1 P2 P3 *
1cc2e 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65   *.**.** Compute
1cc2f 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 61   the remainder a
1cc30 66 74 65 72 20 69 6e 74 65 67 65 72 20 64 69 76  fter integer div
1cc31 69 73 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 6c  ision of the val
1cc32 75 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65  ue in.** registe
1cc33 72 20 50 31 20 62 79 20 74 68 65 20 76 61 6c 75  r P1 by the valu
1cc34 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32  e in register P2
1cc35 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72   and store the r
1cc36 65 73 75 6c 74 20 69 6e 20 50 33 2e 20 0a 2a 2a  esult in P3. .**
1cc37 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e   If the value in
1cc38 20 72 65 67 69 73 74 65 72 20 50 32 20 69 73 20   register P2 is 
1cc39 7a 65 72 6f 20 74 68 65 20 72 65 73 75 6c 74 20  zero the result 
1cc3a 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 65  is NULL..** If e
1cc3b 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73  ither operand is
1cc3c 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c   NULL, the resul
1cc3d 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61  t is NULL..*/.ca
1cc3e 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20 20 20  se OP_Add:      
1cc3f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1cc40 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 2c  same as TK_PLUS,
1cc41 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20   in1, in2, out3 
1cc42 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 75 62 74 72  */.case OP_Subtr
1cc43 61 63 74 3a 20 20 20 20 20 20 20 20 20 20 20 20  act:            
1cc44 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1cc45 4d 49 4e 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c  MINUS, in1, in2,
1cc46 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50   out3 */.case OP
1cc47 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 20 20  _Multiply:      
1cc48 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
1cc49 61 73 20 54 4b 5f 53 54 41 52 2c 20 69 6e 31 2c  as TK_STAR, in1,
1cc4a 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61   in2, out3 */.ca
1cc4b 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 20 20  se OP_Divide:   
1cc4c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1cc4d 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c 41 53 48  same as TK_SLASH
1cc4e 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33  , in1, in2, out3
1cc4f 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 6d 61   */.case OP_Rema
1cc50 69 6e 64 65 72 3a 20 7b 20 20 20 20 20 20 20 20  inder: {        
1cc51 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1cc52 5f 52 45 4d 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  _REM, in1, in2, 
1cc53 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f  out3 */.#if 0  /
1cc54 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1cc55 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1cc56 66 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  f */.  int flags
1cc57 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62 69 6e  ;      /* Combin
1cc58 65 64 20 4d 45 4d 5f 2a 20 66 6c 61 67 73 20 66  ed MEM_* flags f
1cc59 72 6f 6d 20 62 6f 74 68 20 69 6e 70 75 74 73 20  rom both inputs 
1cc5a 2a 2f 0a 20 20 69 36 34 20 69 41 3b 20 20 20 20  */.  i64 iA;    
1cc5b 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20       /* Integer 
1cc5c 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 20 6f 70  value of left op
1cc5d 65 72 61 6e 64 20 2a 2f 0a 20 20 69 36 34 20 69  erand */.  i64 i
1cc5e 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  B;         /* In
1cc5f 74 65 67 65 72 20 76 61 6c 75 65 20 6f 66 20 72  teger value of r
1cc60 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  ight operand */.
1cc61 20 20 64 6f 75 62 6c 65 20 72 41 3b 20 20 20 20    double rA;    
1cc62 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20    /* Real value 
1cc63 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20  of left operand 
1cc64 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 42 3b 20  */.  double rB; 
1cc65 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c       /* Real val
1cc66 75 65 20 6f 66 20 72 69 67 68 74 20 6f 70 65 72  ue of right oper
1cc67 61 6e 64 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a  and */.#endif /*
1cc68 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1cc69 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 66   moved into u.af
1cc6a 20 2a 2f 0a 0a 20 20 61 70 70 6c 79 4e 75 6d 65   */..  applyNume
1cc6b 72 69 63 41 66 66 69 6e 69 74 79 28 70 49 6e 31  ricAffinity(pIn1
1cc6c 29 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69  );.  applyNumeri
1cc6d 63 41 66 66 69 6e 69 74 79 28 70 49 6e 32 29 3b  cAffinity(pIn2);
1cc6e 0a 20 20 75 2e 61 66 2e 66 6c 61 67 73 20 3d 20  .  u.af.flags = 
1cc6f 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49  pIn1->flags | pI
1cc70 6e 32 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28  n2->flags;.  if(
1cc71 20 28 75 2e 61 66 2e 66 6c 61 67 73 20 26 20 4d   (u.af.flags & M
1cc72 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29 20 67 6f  EM_Null)!=0 ) go
1cc73 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65  to arithmetic_re
1cc74 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20  sult_is_null;.  
1cc75 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73  if( (pIn1->flags
1cc76 20 26 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26   & pIn2->flags &
1cc77 20 4d 45 4d 5f 49 6e 74 29 3d 3d 4d 45 4d 5f 49   MEM_Int)==MEM_I
1cc78 6e 74 20 29 7b 0a 20 20 20 20 75 2e 61 66 2e 69  nt ){.    u.af.i
1cc79 41 20 3d 20 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20  A = pIn1->u.i;. 
1cc7a 20 20 20 75 2e 61 66 2e 69 42 20 3d 20 70 49 6e     u.af.iB = pIn
1cc7b 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 73 77 69 74  2->u.i;.    swit
1cc7c 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20  ch( pOp->opcode 
1cc7d 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50  ){.      case OP
1cc7e 5f 41 64 64 3a 20 20 20 20 20 20 20 20 20 75 2e  _Add:         u.
1cc7f 61 66 2e 69 42 20 2b 3d 20 75 2e 61 66 2e 69 41  af.iB += u.af.iA
1cc80 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;       break;. 
1cc81 20 20 20 20 20 63 61 73 65 20 4f 50 5f 53 75 62       case OP_Sub
1cc82 74 72 61 63 74 3a 20 20 20 20 75 2e 61 66 2e 69  tract:    u.af.i
1cc83 42 20 2d 3d 20 75 2e 61 66 2e 69 41 3b 20 20 20  B -= u.af.iA;   
1cc84 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1cc85 20 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c   case OP_Multipl
1cc86 79 3a 20 20 20 20 75 2e 61 66 2e 69 42 20 2a 3d  y:    u.af.iB *=
1cc87 20 75 2e 61 66 2e 69 41 3b 20 20 20 20 20 20 20   u.af.iA;       
1cc88 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
1cc89 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 7b 0a 20  e OP_Divide: {. 
1cc8a 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e         if( u.af.
1cc8b 69 41 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69  iA==0 ) goto ari
1cc8c 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69  thmetic_result_i
1cc8d 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20  s_null;.        
1cc8e 2f 2a 20 44 69 76 69 64 69 6e 67 20 74 68 65 20  /* Dividing the 
1cc8f 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65  largest possible
1cc90 20 6e 65 67 61 74 69 76 65 20 36 34 2d 62 69 74   negative 64-bit
1cc91 20 69 6e 74 65 67 65 72 20 28 31 3c 3c 36 33 29   integer (1<<63)
1cc92 20 62 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 2d   by.        ** -
1cc93 31 20 72 65 74 75 72 6e 73 20 61 6e 20 69 6e 74  1 returns an int
1cc94 65 67 65 72 20 74 6f 6f 20 6c 61 72 67 65 20 74  eger too large t
1cc95 6f 20 73 74 6f 72 65 20 69 6e 20 61 20 36 34 2d  o store in a 64-
1cc96 62 69 74 20 64 61 74 61 2d 74 79 70 65 2e 20 4f  bit data-type. O
1cc97 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 6d  n.        ** som
1cc98 65 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2c  e architectures,
1cc99 20 74 68 65 20 76 61 6c 75 65 20 6f 76 65 72 66   the value overf
1cc9a 6c 6f 77 73 20 74 6f 20 28 31 3c 3c 36 33 29 2e  lows to (1<<63).
1cc9b 20 4f 6e 20 6f 74 68 65 72 73 2c 0a 20 20 20 20   On others,.    
1cc9c 20 20 20 20 2a 2a 20 61 20 53 49 47 46 50 45 20      ** a SIGFPE 
1cc9d 69 73 20 69 73 73 75 65 64 2e 20 54 68 65 20 66  is issued. The f
1cc9e 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65  ollowing stateme
1cc9f 6e 74 20 6e 6f 72 6d 61 6c 69 7a 65 73 20 74 68  nt normalizes th
1cca0 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 65  is.        ** be
1cca1 68 61 76 69 6f 72 20 73 6f 20 74 68 61 74 20 61  havior so that a
1cca2 6c 6c 20 61 72 63 68 69 74 65 63 74 75 72 65 73  ll architectures
1cca3 20 62 65 68 61 76 65 20 61 73 20 69 66 20 69 6e   behave as if in
1cca4 74 65 67 65 72 0a 20 20 20 20 20 20 20 20 2a 2a  teger.        **
1cca5 20 6f 76 65 72 66 6c 6f 77 20 6f 63 63 75 72 72   overflow occurr
1cca6 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ed..        */. 
1cca7 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e         if( u.af.
1cca8 69 41 3d 3d 2d 31 20 26 26 20 75 2e 61 66 2e 69  iA==-1 && u.af.i
1cca9 42 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36  B==SMALLEST_INT6
1ccaa 34 20 29 20 75 2e 61 66 2e 69 41 20 3d 20 31 3b  4 ) u.af.iA = 1;
1ccab 0a 20 20 20 20 20 20 20 20 75 2e 61 66 2e 69 42  .        u.af.iB
1ccac 20 2f 3d 20 75 2e 61 66 2e 69 41 3b 0a 20 20 20   /= u.af.iA;.   
1ccad 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1ccae 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c    }.      defaul
1ccaf 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28  t: {.        if(
1ccb0 20 75 2e 61 66 2e 69 41 3d 3d 30 20 29 20 67 6f   u.af.iA==0 ) go
1ccb1 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65  to arithmetic_re
1ccb2 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20  sult_is_null;.  
1ccb3 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69        if( u.af.i
1ccb4 41 3d 3d 2d 31 20 29 20 75 2e 61 66 2e 69 41 20  A==-1 ) u.af.iA 
1ccb5 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 75 2e 61  = 1;.        u.a
1ccb6 66 2e 69 42 20 25 3d 20 75 2e 61 66 2e 69 41 3b  f.iB %= u.af.iA;
1ccb7 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
1ccb8 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1ccb9 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e    pOut->u.i = u.
1ccba 61 66 2e 69 42 3b 0a 20 20 20 20 4d 65 6d 53 65  af.iB;.    MemSe
1ccbb 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
1ccbc 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65 6c 73  MEM_Int);.  }els
1ccbd 65 7b 0a 20 20 20 20 75 2e 61 66 2e 72 41 20 3d  e{.    u.af.rA =
1ccbe 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c   sqlite3VdbeReal
1ccbf 56 61 6c 75 65 28 70 49 6e 31 29 3b 0a 20 20 20  Value(pIn1);.   
1ccc0 20 75 2e 61 66 2e 72 42 20 3d 20 73 71 6c 69 74   u.af.rB = sqlit
1ccc1 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28  e3VdbeRealValue(
1ccc2 70 49 6e 32 29 3b 0a 20 20 20 20 73 77 69 74 63  pIn2);.    switc
1ccc3 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29  h( pOp->opcode )
1ccc4 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f  {.      case OP_
1ccc5 41 64 64 3a 20 20 20 20 20 20 20 20 20 75 2e 61  Add:         u.a
1ccc6 66 2e 72 42 20 2b 3d 20 75 2e 61 66 2e 72 41 3b  f.rB += u.af.rA;
1ccc7 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1ccc8 20 20 20 20 63 61 73 65 20 4f 50 5f 53 75 62 74      case OP_Subt
1ccc9 72 61 63 74 3a 20 20 20 20 75 2e 61 66 2e 72 42  ract:    u.af.rB
1ccca 20 2d 3d 20 75 2e 61 66 2e 72 41 3b 20 20 20 20   -= u.af.rA;    
1cccb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1cccc 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79  case OP_Multiply
1cccd 3a 20 20 20 20 75 2e 61 66 2e 72 42 20 2a 3d 20  :    u.af.rB *= 
1ccce 75 2e 61 66 2e 72 41 3b 20 20 20 20 20 20 20 62  u.af.rA;       b
1cccf 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
1ccd0 20 4f 50 5f 44 69 76 69 64 65 3a 20 7b 0a 20 20   OP_Divide: {.  
1ccd1 20 20 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65        /* (double
1ccd2 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51  )0 In case of SQ
1ccd3 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
1ccd4 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20  NG_POINT... */. 
1ccd5 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e         if( u.af.
1ccd6 72 41 3d 3d 28 64 6f 75 62 6c 65 29 30 20 29 20  rA==(double)0 ) 
1ccd7 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f  goto arithmetic_
1ccd8 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a  result_is_null;.
1ccd9 20 20 20 20 20 20 20 20 75 2e 61 66 2e 72 42 20          u.af.rB 
1ccda 2f 3d 20 75 2e 61 66 2e 72 41 3b 0a 20 20 20 20  /= u.af.rA;.    
1ccdb 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1ccdc 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74   }.      default
1ccdd 3a 20 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 66  : {.        u.af
1ccde 2e 69 41 20 3d 20 28 69 36 34 29 75 2e 61 66 2e  .iA = (i64)u.af.
1ccdf 72 41 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66  rA;.        u.af
1cce0 2e 69 42 20 3d 20 28 69 36 34 29 75 2e 61 66 2e  .iB = (i64)u.af.
1cce1 72 42 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  rB;.        if( 
1cce2 75 2e 61 66 2e 69 41 3d 3d 30 20 29 20 67 6f 74  u.af.iA==0 ) got
1cce3 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73  o arithmetic_res
1cce4 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20  ult_is_null;.   
1cce5 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41       if( u.af.iA
1cce6 3d 3d 2d 31 20 29 20 75 2e 61 66 2e 69 41 20 3d  ==-1 ) u.af.iA =
1cce7 20 31 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 66   1;.        u.af
1cce8 2e 72 42 20 3d 20 28 64 6f 75 62 6c 65 29 28 75  .rB = (double)(u
1cce9 2e 61 66 2e 69 42 20 25 20 75 2e 61 66 2e 69 41  .af.iB % u.af.iA
1ccea 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
1cceb 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1ccec 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49      if( sqlite3I
1cced 73 4e 61 4e 28 75 2e 61 66 2e 72 42 29 20 29 7b  sNaN(u.af.rB) ){
1ccee 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 72 69 74  .      goto arit
1ccef 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73  hmetic_result_is
1ccf0 5f 6e 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20  _null;.    }.   
1ccf1 20 70 4f 75 74 2d 3e 72 20 3d 20 75 2e 61 66 2e   pOut->r = u.af.
1ccf2 72 42 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79  rB;.    MemSetTy
1ccf3 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
1ccf4 5f 52 65 61 6c 29 3b 0a 20 20 20 20 69 66 28 20  _Real);.    if( 
1ccf5 28 75 2e 61 66 2e 66 6c 61 67 73 20 26 20 4d 45  (u.af.flags & ME
1ccf6 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20  M_Real)==0 ){.  
1ccf7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 49      sqlite3VdbeI
1ccf8 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 70  ntegerAffinity(p
1ccf9 4f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Out);.    }.  }.
1ccfa 20 20 62 72 65 61 6b 3b 0a 0a 61 72 69 74 68 6d    break;..arithm
1ccfb 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e  etic_result_is_n
1ccfc 75 6c 6c 3a 0a 20 20 73 71 6c 69 74 65 33 56 64  ull:.  sqlite3Vd
1ccfd 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75  beMemSetNull(pOu
1ccfe 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  t);.  break;.}..
1ccff 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 6c 53  /* Opcode: CollS
1cd00 65 71 20 2a 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20  eq * * P4.**.** 
1cd01 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  P4 is a pointer 
1cd02 74 6f 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72  to a CollSeq str
1cd03 75 63 74 2e 20 49 66 20 74 68 65 20 6e 65 78 74  uct. If the next
1cd04 20 63 61 6c 6c 20 74 6f 20 61 20 75 73 65 72 20   call to a user 
1cd05 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6f 72 20 61  function.** or a
1cd06 67 67 72 65 67 61 74 65 20 63 61 6c 6c 73 20 73  ggregate calls s
1cd07 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c  qlite3GetFuncCol
1cd08 6c 53 65 71 28 29 2c 20 74 68 69 73 20 63 6f 6c  lSeq(), this col
1cd09 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
1cd0a 77 69 6c 6c 0a 2a 2a 20 62 65 20 72 65 74 75 72  will.** be retur
1cd0b 6e 65 64 2e 20 54 68 69 73 20 69 73 20 75 73 65  ned. This is use
1cd0c 64 20 62 79 20 74 68 65 20 62 75 69 6c 74 2d 69  d by the built-i
1cd0d 6e 20 6d 69 6e 28 29 2c 20 6d 61 78 28 29 20 61  n min(), max() a
1cd0e 6e 64 20 6e 75 6c 6c 69 66 28 29 0a 2a 2a 20 66  nd nullif().** f
1cd0f 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20  unctions..**.** 
1cd10 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 75 73  The interface us
1cd11 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65 6d  ed by the implem
1cd12 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
1cd13 61 66 6f 72 65 6d 65 6e 74 69 6f 6e 65 64 20 66  aforementioned f
1cd14 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 72  unctions.** to r
1cd15 65 74 72 69 65 76 65 20 74 68 65 20 63 6f 6c 6c  etrieve the coll
1cd16 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 73  ation sequence s
1cd17 65 74 20 62 79 20 74 68 69 73 20 6f 70 63 6f 64  et by this opcod
1cd18 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62  e is not availab
1cd19 6c 65 0a 2a 2a 20 70 75 62 6c 69 63 6c 79 2c 20  le.** publicly, 
1cd1a 6f 6e 6c 79 20 74 6f 20 75 73 65 72 20 66 75 6e  only to user fun
1cd1b 63 74 69 6f 6e 73 20 64 65 66 69 6e 65 64 20 69  ctions defined i
1cd1c 6e 20 66 75 6e 63 2e 63 2e 0a 2a 2f 0a 63 61 73  n func.c..*/.cas
1cd1d 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 3a 20 7b 0a  e OP_CollSeq: {.
1cd1e 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1cd1f 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45  4type==P4_COLLSE
1cd20 51 20 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  Q );.  break;.}.
1cd21 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 75 6e 63  ./* Opcode: Func
1cd22 74 69 6f 6e 20 50 31 20 50 32 20 50 33 20 50 34  tion P1 P2 P3 P4
1cd23 20 50 35 0a 2a 2a 0a 2a 2a 20 49 6e 76 6f 6b 65   P5.**.** Invoke
1cd24 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e   a user function
1cd25 20 28 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65   (P4 is a pointe
1cd26 72 20 74 6f 20 61 20 46 75 6e 63 74 69 6f 6e 20  r to a Function 
1cd27 73 74 72 75 63 74 75 72 65 20 74 68 61 74 0a 2a  structure that.*
1cd28 2a 20 64 65 66 69 6e 65 73 20 74 68 65 20 66 75  * defines the fu
1cd29 6e 63 74 69 6f 6e 29 20 77 69 74 68 20 50 35 20  nction) with P5 
1cd2a 61 72 67 75 6d 65 6e 74 73 20 74 61 6b 65 6e 20  arguments taken 
1cd2b 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20 50 32  from register P2
1cd2c 20 61 6e 64 0a 2a 2a 20 73 75 63 63 65 73 73 6f   and.** successo
1cd2d 72 73 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20  rs.  The result 
1cd2e 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  of the function 
1cd2f 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67  is stored in reg
1cd30 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 52 65 67  ister P3..** Reg
1cd31 69 73 74 65 72 20 50 33 20 6d 75 73 74 20 6e 6f  ister P3 must no
1cd32 74 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20  t be one of the 
1cd33 66 75 6e 63 74 69 6f 6e 20 69 6e 70 75 74 73 2e  function inputs.
1cd34 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 33  .**.** P1 is a 3
1cd35 32 2d 62 69 74 20 62 69 74 6d 61 73 6b 20 69 6e  2-bit bitmask in
1cd36 64 69 63 61 74 69 6e 67 20 77 68 65 74 68 65 72  dicating whether
1cd37 20 6f 72 20 6e 6f 74 20 65 61 63 68 20 61 72 67   or not each arg
1cd38 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 0a 2a 2a  ument to the .**
1cd39 20 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 64 65   function was de
1cd3a 74 65 72 6d 69 6e 65 64 20 74 6f 20 62 65 20 63  termined to be c
1cd3b 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d 70 69  onstant at compi
1cd3c 6c 65 20 74 69 6d 65 2e 20 49 66 20 74 68 65 20  le time. If the 
1cd3d 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e  first.** argumen
1cd3e 74 20 77 61 73 20 63 6f 6e 73 74 61 6e 74 20 74  t was constant t
1cd3f 68 65 6e 20 62 69 74 20 30 20 6f 66 20 50 31 20  hen bit 0 of P1 
1cd40 69 73 20 73 65 74 2e 20 54 68 69 73 20 69 73 20  is set. This is 
1cd41 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
1cd42 65 0a 2a 2a 20 77 68 65 74 68 65 72 20 6d 65 74  e.** whether met
1cd43 61 20 64 61 74 61 20 61 73 73 6f 63 69 61 74 65  a data associate
1cd44 64 20 77 69 74 68 20 61 20 75 73 65 72 20 66 75  d with a user fu
1cd45 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20  nction argument 
1cd46 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c  using the.** sql
1cd47 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
1cd48 28 29 20 41 50 49 20 6d 61 79 20 62 65 20 73 61  () API may be sa
1cd49 66 65 6c 79 20 72 65 74 61 69 6e 65 64 20 75 6e  fely retained un
1cd4a 74 69 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20  til the next.** 
1cd4b 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68  invocation of th
1cd4c 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a  is opcode..**.**
1cd4d 20 53 65 65 20 61 6c 73 6f 3a 20 41 67 67 53 74   See also: AggSt
1cd4e 65 70 20 61 6e 64 20 41 67 67 46 69 6e 61 6c 0a  ep and AggFinal.
1cd4f 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 75 6e 63 74  */.case OP_Funct
1cd50 69 6f 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a  ion: {.#if 0  /*
1cd51 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1cd52 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 67   moved into u.ag
1cd53 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d   */.  int i;.  M
1cd54 65 6d 20 2a 70 41 72 67 3b 0a 20 20 73 71 6c 69  em *pArg;.  sqli
1cd55 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b  te3_context ctx;
1cd56 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
1cd57 20 2a 2a 61 70 56 61 6c 3b 0a 20 20 69 6e 74 20   **apVal;.  int 
1cd58 6e 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  n;.#endif /* loc
1cd59 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1cd5a 65 64 20 69 6e 74 6f 20 75 2e 61 67 20 2a 2f 0a  ed into u.ag */.
1cd5b 0a 20 20 75 2e 61 67 2e 6e 20 3d 20 70 4f 70 2d  .  u.ag.n = pOp-
1cd5c 3e 70 35 3b 0a 20 20 75 2e 61 67 2e 61 70 56 61  >p5;.  u.ag.apVa
1cd5d 6c 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20  l = p->apArg;.  
1cd5e 61 73 73 65 72 74 28 20 75 2e 61 67 2e 61 70 56  assert( u.ag.apV
1cd5f 61 6c 20 7c 7c 20 75 2e 61 67 2e 6e 3d 3d 30 20  al || u.ag.n==0 
1cd60 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 75 2e  );..  assert( u.
1cd61 61 67 2e 6e 3d 3d 30 20 7c 7c 20 28 70 4f 70 2d  ag.n==0 || (pOp-
1cd62 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32  >p2>0 && pOp->p2
1cd63 2b 75 2e 61 67 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d  +u.ag.n<=p->nMem
1cd64 2b 31 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  +1) );.  assert(
1cd65 20 70 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 32   pOp->p3<pOp->p2
1cd66 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70   || pOp->p3>=pOp
1cd67 2d 3e 70 32 2b 75 2e 61 67 2e 6e 20 29 3b 0a 20  ->p2+u.ag.n );. 
1cd68 20 75 2e 61 67 2e 70 41 72 67 20 3d 20 26 70 2d   u.ag.pArg = &p-
1cd69 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a  >aMem[pOp->p2];.
1cd6a 20 20 66 6f 72 28 75 2e 61 67 2e 69 3d 30 3b 20    for(u.ag.i=0; 
1cd6b 75 2e 61 67 2e 69 3c 75 2e 61 67 2e 6e 3b 20 75  u.ag.i<u.ag.n; u
1cd6c 2e 61 67 2e 69 2b 2b 2c 20 75 2e 61 67 2e 70 41  .ag.i++, u.ag.pA
1cd6d 72 67 2b 2b 29 7b 0a 20 20 20 20 75 2e 61 67 2e  rg++){.    u.ag.
1cd6e 61 70 56 61 6c 5b 75 2e 61 67 2e 69 5d 20 3d 20  apVal[u.ag.i] = 
1cd6f 75 2e 61 67 2e 70 41 72 67 3b 0a 20 20 20 20 73  u.ag.pArg;.    s
1cd70 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 75 2e 61  toreTypeInfo(u.a
1cd71 67 2e 70 41 72 67 2c 20 65 6e 63 6f 64 69 6e 67  g.pArg, encoding
1cd72 29 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f  );.    REGISTER_
1cd73 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 75  TRACE(pOp->p2, u
1cd74 2e 61 67 2e 70 41 72 67 29 3b 0a 20 20 7d 0a 0a  .ag.pArg);.  }..
1cd75 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1cd76 34 74 79 70 65 3d 3d 50 34 5f 46 55 4e 43 44 45  4type==P4_FUNCDE
1cd77 46 20 7c 7c 20 70 4f 70 2d 3e 70 34 74 79 70 65  F || pOp->p4type
1cd78 3d 3d 50 34 5f 56 44 42 45 46 55 4e 43 20 29 3b  ==P4_VDBEFUNC );
1cd79 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79  .  if( pOp->p4ty
1cd7a 70 65 3d 3d 50 34 5f 46 55 4e 43 44 45 46 20 29  pe==P4_FUNCDEF )
1cd7b 7b 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 2e 70  {.    u.ag.ctx.p
1cd7c 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34 2e 70  Func = pOp->p4.p
1cd7d 46 75 6e 63 3b 0a 20 20 20 20 75 2e 61 67 2e 63  Func;.    u.ag.c
1cd7e 74 78 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 30  tx.pVdbeFunc = 0
1cd7f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75  ;.  }else{.    u
1cd80 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46 75 6e  .ag.ctx.pVdbeFun
1cd81 63 20 3d 20 28 56 64 62 65 46 75 6e 63 2a 29 70  c = (VdbeFunc*)p
1cd82 4f 70 2d 3e 70 34 2e 70 56 64 62 65 46 75 6e 63  Op->p4.pVdbeFunc
1cd83 3b 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 2e 70  ;.    u.ag.ctx.p
1cd84 46 75 6e 63 20 3d 20 75 2e 61 67 2e 63 74 78 2e  Func = u.ag.ctx.
1cd85 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63  pVdbeFunc->pFunc
1cd86 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
1cd87 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f   pOp->p3>0 && pO
1cd88 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p3<=p->nMem )
1cd89 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61  ;.  pOut = &p->a
1cd8a 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20  Mem[pOp->p3];.  
1cd8b 75 2e 61 67 2e 63 74 78 2e 73 2e 66 6c 61 67 73  u.ag.ctx.s.flags
1cd8c 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 75   = MEM_Null;.  u
1cd8d 2e 61 67 2e 63 74 78 2e 73 2e 64 62 20 3d 20 64  .ag.ctx.s.db = d
1cd8e 62 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e 73 2e  b;.  u.ag.ctx.s.
1cd8f 78 44 65 6c 20 3d 20 30 3b 0a 20 20 75 2e 61 67  xDel = 0;.  u.ag
1cd90 2e 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63 20 3d  .ctx.s.zMalloc =
1cd91 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6f 75   0;..  /* The ou
1cd92 74 70 75 74 20 63 65 6c 6c 20 6d 61 79 20 61 6c  tput cell may al
1cd93 72 65 61 64 79 20 68 61 76 65 20 61 20 62 75 66  ready have a buf
1cd94 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 2e 20 4d  fer allocated. M
1cd95 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 70 6f 69  ove.  ** the poi
1cd96 6e 74 65 72 20 74 6f 20 75 2e 61 67 2e 63 74 78  nter to u.ag.ctx
1cd97 2e 73 20 73 6f 20 69 6e 20 63 61 73 65 20 74 68  .s so in case th
1cd98 65 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20  e user-function 
1cd99 63 61 6e 20 75 73 65 0a 20 20 2a 2a 20 74 68 65  can use.  ** the
1cd9a 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74   already allocat
1cd9b 65 64 20 62 75 66 66 65 72 20 69 6e 73 74 65 61  ed buffer instea
1cd9c 64 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20  d of allocating 
1cd9d 61 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a  a new one..  */.
1cd9e 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1cd9f 4d 6f 76 65 28 26 75 2e 61 67 2e 63 74 78 2e 73  Move(&u.ag.ctx.s
1cda0 2c 20 70 4f 75 74 29 3b 0a 20 20 4d 65 6d 53 65  , pOut);.  MemSe
1cda1 74 54 79 70 65 46 6c 61 67 28 26 75 2e 61 67 2e  tTypeFlag(&u.ag.
1cda2 63 74 78 2e 73 2c 20 4d 45 4d 5f 4e 75 6c 6c 29  ctx.s, MEM_Null)
1cda3 3b 0a 0a 20 20 75 2e 61 67 2e 63 74 78 2e 69 73  ;..  u.ag.ctx.is
1cda4 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 69 66 28  Error = 0;.  if(
1cda5 20 75 2e 61 67 2e 63 74 78 2e 70 46 75 6e 63 2d   u.ag.ctx.pFunc-
1cda6 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
1cda7 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b  FUNC_NEEDCOLL ){
1cda8 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70  .    assert( pOp
1cda9 3e 70 2d 3e 61 4f 70 20 29 3b 0a 20 20 20 20 61  >p->aOp );.    a
1cdaa 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 70  ssert( pOp[-1].p
1cdab 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45  4type==P4_COLLSE
1cdac 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  Q );.    assert(
1cdad 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d   pOp[-1].opcode=
1cdae 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b 0a 20  =OP_CollSeq );. 
1cdaf 20 20 20 75 2e 61 67 2e 63 74 78 2e 70 43 6f 6c     u.ag.ctx.pCol
1cdb0 6c 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70 34 2e 70  l = pOp[-1].p4.p
1cdb1 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Coll;.  }.  if( 
1cdb2 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
1cdb3 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72  (db) ) goto abor
1cdb4 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
1cdb5 0a 20 20 28 2a 75 2e 61 67 2e 63 74 78 2e 70 46  .  (*u.ag.ctx.pF
1cdb6 75 6e 63 2d 3e 78 46 75 6e 63 29 28 26 75 2e 61  unc->xFunc)(&u.a
1cdb7 67 2e 63 74 78 2c 20 75 2e 61 67 2e 6e 2c 20 75  g.ctx, u.ag.n, u
1cdb8 2e 61 67 2e 61 70 56 61 6c 29 3b 0a 20 20 69 66  .ag.apVal);.  if
1cdb9 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1cdba 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 73 71 6c  n(db) ){.    sql
1cdbb 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
1cdbc 73 65 28 26 75 2e 61 67 2e 63 74 78 2e 73 29 3b  se(&u.ag.ctx.s);
1cdbd 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f  .    goto abort_
1cdbe 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20  due_to_misuse;. 
1cdbf 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c   }.  if( db->mal
1cdc0 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
1cdc1 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20   /* Even though 
1cdc2 61 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 66  a malloc() has f
1cdc3 61 69 6c 65 64 2c 20 74 68 65 20 69 6d 70 6c 65  ailed, the imple
1cdc4 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
1cdc5 0a 20 20 20 20 2a 2a 20 75 73 65 72 20 66 75 6e  .    ** user fun
1cdc6 63 74 69 6f 6e 20 6d 61 79 20 68 61 76 65 20 63  ction may have c
1cdc7 61 6c 6c 65 64 20 61 6e 20 73 71 6c 69 74 65 33  alled an sqlite3
1cdc8 5f 72 65 73 75 6c 74 5f 58 58 58 28 29 20 66 75  _result_XXX() fu
1cdc9 6e 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 74 6f  nction.    ** to
1cdca 20 72 65 74 75 72 6e 20 61 20 76 61 6c 75 65 2e   return a value.
1cdcb 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
1cdcc 61 6c 6c 20 72 65 6c 65 61 73 65 73 20 61 6e 79  all releases any
1cdcd 20 72 65 73 6f 75 72 63 65 73 0a 20 20 20 20 2a   resources.    *
1cdce 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  * associated wit
1cdcf 68 20 73 75 63 68 20 61 20 76 61 6c 75 65 2e 0a  h such a value..
1cdd0 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f      **.    ** No
1cdd1 74 65 3a 20 4d 61 79 62 65 20 4d 65 6d 52 65 6c  te: Maybe MemRel
1cdd2 65 61 73 65 28 29 20 73 68 6f 75 6c 64 20 62 65  ease() should be
1cdd3 20 63 61 6c 6c 65 64 20 69 66 20 73 71 6c 69 74   called if sqlit
1cdd4 65 33 53 61 66 65 74 79 4f 6e 28 29 0a 20 20 20  e3SafetyOn().   
1cdd5 20 2a 2a 20 66 61 69 6c 73 20 61 6c 73 6f 20 28   ** fails also (
1cdd6 74 68 65 20 69 66 28 2e 2e 2e 29 20 73 74 61 74  the if(...) stat
1cdd7 65 6d 65 6e 74 20 61 62 6f 76 65 29 2e 20 42 75  ement above). Bu
1cdd8 74 20 69 66 20 70 65 6f 70 6c 65 20 61 72 65 0a  t if people are.
1cdd9 20 20 20 20 2a 2a 20 6d 69 73 75 73 69 6e 67 20      ** misusing 
1cdda 73 71 6c 69 74 65 2c 20 74 68 65 79 20 68 61 76  sqlite, they hav
1cddb 65 20 62 69 67 67 65 72 20 70 72 6f 62 6c 65 6d  e bigger problem
1cddc 73 20 74 68 61 6e 20 61 20 6c 65 61 6b 65 64 20  s than a leaked 
1cddd 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  value..    */.  
1cdde 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1cddf 52 65 6c 65 61 73 65 28 26 75 2e 61 67 2e 63 74  Release(&u.ag.ct
1cde0 78 2e 73 29 3b 0a 20 20 20 20 67 6f 74 6f 20 6e  x.s);.    goto n
1cde1 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  o_mem;.  }..  /*
1cde2 20 49 66 20 61 6e 79 20 61 75 78 69 6c 69 61 72   If any auxiliar
1cde3 79 20 64 61 74 61 20 66 75 6e 63 74 69 6f 6e 73  y data functions
1cde4 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65   have been calle
1cde5 64 20 62 79 20 74 68 69 73 20 75 73 65 72 20 66  d by this user f
1cde6 75 6e 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20 69 6d  unction,.  ** im
1cde7 6d 65 64 69 61 74 65 6c 79 20 63 61 6c 6c 20 74  mediately call t
1cde8 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f  he destructor fo
1cde9 72 20 61 6e 79 20 6e 6f 6e 2d 73 74 61 74 69 63  r any non-static
1cdea 20 76 61 6c 75 65 73 2e 0a 20 20 2a 2f 0a 20 20   values..  */.  
1cdeb 69 66 28 20 75 2e 61 67 2e 63 74 78 2e 70 56 64  if( u.ag.ctx.pVd
1cdec 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20 73 71  beFunc ){.    sq
1cded 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 41  lite3VdbeDeleteA
1cdee 75 78 44 61 74 61 28 75 2e 61 67 2e 63 74 78 2e  uxData(u.ag.ctx.
1cdef 70 56 64 62 65 46 75 6e 63 2c 20 70 4f 70 2d 3e  pVdbeFunc, pOp->
1cdf0 70 31 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34  p1);.    pOp->p4
1cdf1 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 75 2e 61  .pVdbeFunc = u.a
1cdf2 67 2e 63 74 78 2e 70 56 64 62 65 46 75 6e 63 3b  g.ctx.pVdbeFunc;
1cdf3 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65  .    pOp->p4type
1cdf4 20 3d 20 50 34 5f 56 44 42 45 46 55 4e 43 3b 0a   = P4_VDBEFUNC;.
1cdf5 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
1cdf6 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
1cdf7 65 64 20 61 6e 20 65 72 72 6f 72 2c 20 74 68 72  ed an error, thr
1cdf8 6f 77 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20  ow an exception 
1cdf9 2a 2f 0a 20 20 69 66 28 20 75 2e 61 67 2e 63 74  */.  if( u.ag.ct
1cdfa 78 2e 69 73 45 72 72 6f 72 20 29 7b 0a 20 20 20  x.isError ){.   
1cdfb 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
1cdfc 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  g(&p->zErrMsg, d
1cdfd 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33  b, "%s", sqlite3
1cdfe 5f 76 61 6c 75 65 5f 74 65 78 74 28 26 75 2e 61  _value_text(&u.a
1cdff 67 2e 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72  g.ctx.s));.    r
1ce00 63 20 3d 20 75 2e 61 67 2e 63 74 78 2e 69 73 45  c = u.ag.ctx.isE
1ce01 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rror;.  }..  /* 
1ce02 43 6f 70 79 20 74 68 65 20 72 65 73 75 6c 74 20  Copy the result 
1ce03 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  of the function 
1ce04 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 33  into register P3
1ce05 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
1ce06 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28  eChangeEncoding(
1ce07 26 75 2e 61 67 2e 63 74 78 2e 73 2c 20 65 6e 63  &u.ag.ctx.s, enc
1ce08 6f 64 69 6e 67 29 3b 0a 20 20 73 71 6c 69 74 65  oding);.  sqlite
1ce09 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 70 4f 75  3VdbeMemMove(pOu
1ce0a 74 2c 20 26 75 2e 61 67 2e 63 74 78 2e 73 29 3b  t, &u.ag.ctx.s);
1ce0b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64  .  if( sqlite3Vd
1ce0c 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 4f 75 74  beMemTooBig(pOut
1ce0d 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f  ) ){.    goto to
1ce0e 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 52 45 47  o_big;.  }.  REG
1ce0f 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1ce10 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 55 50  >p3, pOut);.  UP
1ce11 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a  DATE_MAX_BLOBSIZ
1ce12 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b  E(pOut);.  break
1ce13 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1ce14 42 69 74 41 6e 64 20 50 31 20 50 32 20 50 33 20  BitAnd P1 P2 P3 
1ce15 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74  * *.**.** Take t
1ce16 68 65 20 62 69 74 2d 77 69 73 65 20 41 4e 44 20  he bit-wise AND 
1ce17 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e  of the values in
1ce18 20 72 65 67 69 73 74 65 72 20 50 31 20 61 6e 64   register P1 and
1ce19 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 6f 72 65   P2 and.** store
1ce1a 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72   the result in r
1ce1b 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49  egister P3..** I
1ce1c 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69  f either input i
1ce1d 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75  s NULL, the resu
1ce1e 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f  lt is NULL..*/./
1ce1f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 4f 72 20  * Opcode: BitOr 
1ce20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1ce21 2a 2a 20 54 61 6b 65 20 74 68 65 20 62 69 74 2d  ** Take the bit-
1ce22 77 69 73 65 20 4f 52 20 6f 66 20 74 68 65 20 76  wise OR of the v
1ce23 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65  alues in registe
1ce24 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a  r P1 and P2 and.
1ce25 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  ** store the res
1ce26 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20  ult in register 
1ce27 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  P3..** If either
1ce28 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20   input is NULL, 
1ce29 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55  the result is NU
1ce2a 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  LL..*/./* Opcode
1ce2b 3a 20 53 68 69 66 74 4c 65 66 74 20 50 31 20 50  : ShiftLeft P1 P
1ce2c 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53  2 P3 * *.**.** S
1ce2d 68 69 66 74 20 74 68 65 20 69 6e 74 65 67 65 72  hift the integer
1ce2e 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1ce2f 65 72 20 50 32 20 74 6f 20 74 68 65 20 6c 65 66  er P2 to the lef
1ce30 74 20 62 79 20 74 68 65 0a 2a 2a 20 6e 75 6d 62  t by the.** numb
1ce31 65 72 20 6f 66 20 62 69 74 73 20 73 70 65 63 69  er of bits speci
1ce32 66 69 65 64 20 62 79 20 74 68 65 20 69 6e 74 65  fied by the inte
1ce33 67 65 72 20 69 6e 20 72 65 67 69 73 65 72 20 50  ger in regiser P
1ce34 31 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20  1..** Store the 
1ce35 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74  result in regist
1ce36 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74  er P3..** If eit
1ce37 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c  her input is NUL
1ce38 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  L, the result is
1ce39 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63   NULL..*/./* Opc
1ce3a 6f 64 65 3a 20 53 68 69 66 74 52 69 67 68 74 20  ode: ShiftRight 
1ce3b 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1ce3c 2a 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e 74  ** Shift the int
1ce3d 65 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65  eger value in re
1ce3e 67 69 73 74 65 72 20 50 32 20 74 6f 20 74 68 65  gister P2 to the
1ce3f 20 72 69 67 68 74 20 62 79 20 74 68 65 0a 2a 2a   right by the.**
1ce40 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20   number of bits 
1ce41 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65  specified by the
1ce42 20 69 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69   integer in regi
1ce43 73 74 65 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72  ster P1..** Stor
1ce44 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20  e the result in 
1ce45 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20  register P3..** 
1ce46 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20  If either input 
1ce47 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73  is NULL, the res
1ce48 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  ult is NULL..*/.
1ce49 63 61 73 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20  case OP_BitAnd: 
1ce4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ce4b 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49  /* same as TK_BI
1ce4c 54 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  TAND, in1, in2, 
1ce4d 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f  out3 */.case OP_
1ce4e 42 69 74 4f 72 3a 20 20 20 20 20 20 20 20 20 20  BitOr:          
1ce4f 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
1ce50 61 73 20 54 4b 5f 42 49 54 4f 52 2c 20 69 6e 31  as TK_BITOR, in1
1ce51 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63  , in2, out3 */.c
1ce52 61 73 65 20 4f 50 5f 53 68 69 66 74 4c 65 66 74  ase OP_ShiftLeft
1ce53 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  :              /
1ce54 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48  * same as TK_LSH
1ce55 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  IFT, in1, in2, o
1ce56 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  ut3 */.case OP_S
1ce57 68 69 66 74 52 69 67 68 74 3a 20 7b 20 20 20 20  hiftRight: {    
1ce58 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1ce59 73 20 54 4b 5f 52 53 48 49 46 54 2c 20 69 6e 31  s TK_RSHIFT, in1
1ce5a 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 23  , in2, out3 */.#
1ce5b 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1ce5c 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1ce5d 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a 20 20 69 36  nto u.ah */.  i6
1ce5e 34 20 61 3b 0a 20 20 69 36 34 20 62 3b 0a 23 65  4 a;.  i64 b;.#e
1ce5f 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1ce60 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1ce61 74 6f 20 75 2e 61 68 20 2a 2f 0a 0a 20 20 69 66  to u.ah */..  if
1ce62 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c  ( (pIn1->flags |
1ce63 20 70 49 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20   pIn2->flags) & 
1ce64 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
1ce65 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1ce66 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20  tNull(pOut);.   
1ce67 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e   break;.  }.  u.
1ce68 61 68 2e 61 20 3d 20 73 71 6c 69 74 65 33 56 64  ah.a = sqlite3Vd
1ce69 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29  beIntValue(pIn2)
1ce6a 3b 0a 20 20 75 2e 61 68 2e 62 20 3d 20 73 71 6c  ;.  u.ah.b = sql
1ce6b 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65  ite3VdbeIntValue
1ce6c 28 70 49 6e 31 29 3b 0a 20 20 73 77 69 74 63 68  (pIn1);.  switch
1ce6d 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b  ( pOp->opcode ){
1ce6e 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 42 69 74  .    case OP_Bit
1ce6f 41 6e 64 3a 20 20 20 20 20 20 75 2e 61 68 2e 61  And:      u.ah.a
1ce70 20 26 3d 20 75 2e 61 68 2e 62 3b 20 20 20 20 20   &= u.ah.b;     
1ce71 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
1ce72 4f 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20  OP_BitOr:       
1ce73 75 2e 61 68 2e 61 20 7c 3d 20 75 2e 61 68 2e 62  u.ah.a |= u.ah.b
1ce74 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  ;     break;.   
1ce75 20 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c 65   case OP_ShiftLe
1ce76 66 74 3a 20 20 20 75 2e 61 68 2e 61 20 3c 3c 3d  ft:   u.ah.a <<=
1ce77 20 75 2e 61 68 2e 62 3b 20 20 20 20 62 72 65 61   u.ah.b;    brea
1ce78 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  k;.    default: 
1ce79 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70   assert( pOp->op
1ce7a 63 6f 64 65 3d 3d 4f 50 5f 53 68 69 66 74 52 69  code==OP_ShiftRi
1ce7b 67 68 74 20 29 3b 0a 20 20 20 20 20 20 20 20 20  ght );.         
1ce7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ce7d 75 2e 61 68 2e 61 20 3e 3e 3d 20 75 2e 61 68 2e  u.ah.a >>= u.ah.
1ce7e 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  b;    break;.  }
1ce7f 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75  .  pOut->u.i = u
1ce80 2e 61 68 2e 61 3b 0a 20 20 4d 65 6d 53 65 74 54  .ah.a;.  MemSetT
1ce81 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
1ce82 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b  M_Int);.  break;
1ce83 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41  .}../* Opcode: A
1ce84 64 64 49 6d 6d 20 20 50 31 20 50 32 20 2a 20 2a  ddImm  P1 P2 * *
1ce85 20 2a 0a 2a 2a 20 0a 2a 2a 20 41 64 64 20 74 68   *.** .** Add th
1ce86 65 20 63 6f 6e 73 74 61 6e 74 20 50 32 20 74 6f  e constant P2 to
1ce87 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1ce88 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 54 68  gister P1..** Th
1ce89 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c 77 61  e result is alwa
1ce8a 79 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a  ys an integer..*
1ce8b 2a 0a 2a 2a 20 54 6f 20 66 6f 72 63 65 20 61 6e  *.** To force an
1ce8c 79 20 72 65 67 69 73 74 65 72 20 74 6f 20 62 65  y register to be
1ce8d 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 6a 75 73   an integer, jus
1ce8e 74 20 61 64 64 20 30 2e 0a 2a 2f 0a 63 61 73 65  t add 0..*/.case
1ce8f 20 4f 50 5f 41 64 64 49 6d 6d 3a 20 7b 20 20 20   OP_AddImm: {   
1ce90 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20           /* in1 
1ce91 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
1ce92 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49  MemIntegerify(pI
1ce93 6e 31 29 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69  n1);.  pIn1->u.i
1ce94 20 2b 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 62   += pOp->p2;.  b
1ce95 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1ce96 64 65 3a 20 4d 75 73 74 42 65 49 6e 74 20 50 31  de: MustBeInt P1
1ce97 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20 0a 2a 2a   P2 * * *.** .**
1ce98 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65   Force the value
1ce99 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1ce9a 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72  to be an integer
1ce9b 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a  .  If the value.
1ce9c 2a 2a 20 69 6e 20 50 31 20 69 73 20 6e 6f 74 20  ** in P1 is not 
1ce9d 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 20 63  an integer and c
1ce9e 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74  annot be convert
1ce9f 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67  ed into an integ
1cea0 65 72 0a 2a 2a 20 77 69 74 68 6f 75 74 20 64 61  er.** without da
1cea1 74 61 20 6c 6f 73 73 2c 20 74 68 65 6e 20 6a 75  ta loss, then ju
1cea2 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  mp immediately t
1cea3 6f 20 50 32 2c 20 6f 72 20 69 66 20 50 32 3d 3d  o P2, or if P2==
1cea4 30 0a 2a 2a 20 72 61 69 73 65 20 61 6e 20 53 51  0.** raise an SQ
1cea5 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 20 65 78  LITE_MISMATCH ex
1cea6 63 65 70 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65  ception..*/.case
1cea7 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 3a 20 7b   OP_MustBeInt: {
1cea8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a              /* j
1cea9 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 70  ump, in1 */.  ap
1ceaa 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31  plyAffinity(pIn1
1ceab 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  , SQLITE_AFF_NUM
1ceac 45 52 49 43 2c 20 65 6e 63 6f 64 69 6e 67 29 3b  ERIC, encoding);
1cead 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c  .  if( (pIn1->fl
1ceae 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d  ags & MEM_Int)==
1ceaf 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 4f 70  0 ){.    if( pOp
1ceb0 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ->p2==0 ){.     
1ceb1 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53   rc = SQLITE_MIS
1ceb2 4d 41 54 43 48 3b 0a 20 20 20 20 20 20 67 6f 74  MATCH;.      got
1ceb3 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
1ceb4 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  rror;.    }else{
1ceb5 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  .      pc = pOp-
1ceb6 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20  >p2 - 1;.    }. 
1ceb7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 65 6d 53   }else{.    MemS
1ceb8 65 74 54 79 70 65 46 6c 61 67 28 70 49 6e 31 2c  etTypeFlag(pIn1,
1ceb9 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20   MEM_Int);.  }. 
1ceba 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1cebb 63 6f 64 65 3a 20 52 65 61 6c 41 66 66 69 6e 69  code: RealAffini
1cebc 74 79 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  ty P1 * * * *.**
1cebd 0a 2a 2a 20 49 66 20 72 65 67 69 73 74 65 72 20  .** If register 
1cebe 50 31 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65  P1 holds an inte
1cebf 67 65 72 20 63 6f 6e 76 65 72 74 20 69 74 20 74  ger convert it t
1cec0 6f 20 61 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a  o a real value..
1cec1 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64  **.** This opcod
1cec2 65 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 65  e is used when e
1cec3 78 74 72 61 63 74 69 6e 67 20 69 6e 66 6f 72 6d  xtracting inform
1cec4 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 63 6f 6c  ation from a col
1cec5 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61 73 20  umn that.** has 
1cec6 52 45 41 4c 20 61 66 66 69 6e 69 74 79 2e 20 20  REAL affinity.  
1cec7 53 75 63 68 20 63 6f 6c 75 6d 6e 20 76 61 6c 75  Such column valu
1cec8 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 20  es may still be 
1cec9 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 69 6e 74  stored as.** int
1ceca 65 67 65 72 73 2c 20 66 6f 72 20 73 70 61 63 65  egers, for space
1cecb 20 65 66 66 69 63 69 65 6e 63 79 2c 20 62 75 74   efficiency, but
1cecc 20 61 66 74 65 72 20 65 78 74 72 61 63 74 69 6f   after extractio
1cecd 6e 20 77 65 20 77 61 6e 74 20 74 68 65 6d 0a 2a  n we want them.*
1cece 2a 20 74 6f 20 68 61 76 65 20 6f 6e 6c 79 20 61  * to have only a
1cecf 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a   real value..*/.
1ced0 63 61 73 65 20 4f 50 5f 52 65 61 6c 41 66 66 69  case OP_RealAffi
1ced1 6e 69 74 79 3a 20 7b 20 20 20 20 20 20 20 20 20  nity: {         
1ced2 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20           /* in1 
1ced3 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66  */.  if( pIn1->f
1ced4 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29  lags & MEM_Int )
1ced5 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1ced6 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 6e 31  eMemRealify(pIn1
1ced7 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  );.  }.  break;.
1ced8 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
1ced9 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a 20 4f  E_OMIT_CAST./* O
1ceda 70 63 6f 64 65 3a 20 54 6f 54 65 78 74 20 50 31  pcode: ToText P1
1cedb 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46   * * * *.**.** F
1cedc 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 20 69  orce the value i
1cedd 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f  n register P1 to
1cede 20 62 65 20 74 65 78 74 2e 0a 2a 2a 20 49 66 20   be text..** If 
1cedf 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e 75 6d  the value is num
1cee0 65 72 69 63 2c 20 63 6f 6e 76 65 72 74 20 69 74  eric, convert it
1cee1 20 74 6f 20 61 20 73 74 72 69 6e 67 20 75 73 69   to a string usi
1cee2 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 76 61  ng the.** equiva
1cee3 6c 65 6e 74 20 6f 66 20 70 72 69 6e 74 66 28 29  lent of printf()
1cee4 2e 20 20 42 6c 6f 62 20 76 61 6c 75 65 73 20 61  .  Blob values a
1cee5 72 65 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64  re unchanged and
1cee6 0a 2a 2a 20 61 72 65 20 61 66 74 65 72 77 61 72  .** are afterwar
1cee7 64 73 20 73 69 6d 70 6c 79 20 69 6e 74 65 72 70  ds simply interp
1cee8 72 65 74 65 64 20 61 73 20 74 65 78 74 2e 0a 2a  reted as text..*
1cee9 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75  *.** A NULL valu
1ceea 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 64  e is not changed
1ceeb 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65   by this routine
1ceec 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e 55  .  It remains NU
1ceed 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54  LL..*/.case OP_T
1ceee 6f 54 65 78 74 3a 20 7b 20 20 20 20 20 20 20 20  oText: {        
1ceef 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1cef0 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45 58 54 2c  e as TK_TO_TEXT,
1cef1 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 70 49   in1 */.  if( pI
1cef2 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n1->flags & MEM_
1cef3 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20  Null ) break;.  
1cef4 61 73 73 65 72 74 28 20 4d 45 4d 5f 53 74 72 3d  assert( MEM_Str=
1cef5 3d 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 29  =(MEM_Blob>>3) )
1cef6 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20  ;.  pIn1->flags 
1cef7 7c 3d 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 26  |= (pIn1->flags&
1cef8 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20  MEM_Blob)>>3;.  
1cef9 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49  applyAffinity(pI
1cefa 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54  n1, SQLITE_AFF_T
1cefb 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a  EXT, encoding);.
1cefc 20 20 72 63 20 3d 20 45 78 70 61 6e 64 42 6c 6f    rc = ExpandBlo
1cefd 62 28 70 49 6e 31 29 3b 0a 20 20 61 73 73 65 72  b(pIn1);.  asser
1cefe 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  t( pIn1->flags &
1ceff 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 62 2d 3e   MEM_Str || db->
1cf00 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
1cf01 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 3d    pIn1->flags &=
1cf02 20 7e 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52   ~(MEM_Int|MEM_R
1cf03 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d  eal|MEM_Blob|MEM
1cf04 5f 5a 65 72 6f 29 3b 0a 20 20 55 50 44 41 54 45  _Zero);.  UPDATE
1cf05 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 49  _MAX_BLOBSIZE(pI
1cf06 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  n1);.  break;.}.
1cf07 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 42 6c  ./* Opcode: ToBl
1cf08 6f 62 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  ob P1 * * * *.**
1cf09 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61  .** Force the va
1cf0a 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1cf0b 50 31 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 2e  P1 to be a BLOB.
1cf0c 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
1cf0d 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63 6f 6e   is numeric, con
1cf0e 76 65 72 74 20 69 74 20 74 6f 20 61 20 73 74 72  vert it to a str
1cf0f 69 6e 67 20 66 69 72 73 74 2e 0a 2a 2a 20 53 74  ing first..** St
1cf10 72 69 6e 67 73 20 61 72 65 20 73 69 6d 70 6c 79  rings are simply
1cf11 20 72 65 69 6e 74 65 72 70 72 65 74 65 64 20 61   reinterpreted a
1cf12 73 20 62 6c 6f 62 73 20 77 69 74 68 20 6e 6f 20  s blobs with no 
1cf13 63 68 61 6e 67 65 0a 2a 2a 20 74 6f 20 74 68 65  change.** to the
1cf14 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61   underlying data
1cf15 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76  ..**.** A NULL v
1cf16 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e  alue is not chan
1cf17 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74  ged by this rout
1cf18 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73  ine.  It remains
1cf19 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f   NULL..*/.case O
1cf1a 50 5f 54 6f 42 6c 6f 62 3a 20 7b 20 20 20 20 20  P_ToBlob: {     
1cf1b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1cf1c 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 42 4c  same as TK_TO_BL
1cf1d 4f 42 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28  OB, in1 */.  if(
1cf1e 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d   pIn1->flags & M
1cf1f 45 4d 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b  EM_Null ) break;
1cf20 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c  .  if( (pIn1->fl
1cf21 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 3d  ags & MEM_Blob)=
1cf22 3d 30 20 29 7b 0a 20 20 20 20 61 70 70 6c 79 41  =0 ){.    applyA
1cf23 66 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 51  ffinity(pIn1, SQ
1cf24 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 65  LITE_AFF_TEXT, e
1cf25 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 61 73  ncoding);.    as
1cf26 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67  sert( pIn1->flag
1cf27 73 20 26 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64  s & MEM_Str || d
1cf28 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1cf29 29 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70  );.    MemSetTyp
1cf2a 65 46 6c 61 67 28 70 49 6e 31 2c 20 4d 45 4d 5f  eFlag(pIn1, MEM_
1cf2b 42 6c 6f 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Blob);.  }else{.
1cf2c 20 20 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20      pIn1->flags 
1cf2d 26 3d 20 7e 28 4d 45 4d 5f 54 79 70 65 4d 61 73  &= ~(MEM_TypeMas
1cf2e 6b 26 7e 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20  k&~MEM_Blob);.  
1cf2f 7d 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42  }.  UPDATE_MAX_B
1cf30 4c 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20  LOBSIZE(pIn1);. 
1cf31 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1cf32 63 6f 64 65 3a 20 54 6f 4e 75 6d 65 72 69 63 20  code: ToNumeric 
1cf33 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  P1 * * * *.**.**
1cf34 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65   Force the value
1cf35 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1cf36 74 6f 20 62 65 20 6e 75 6d 65 72 69 63 20 28 65  to be numeric (e
1cf37 69 74 68 65 72 20 61 6e 0a 2a 2a 20 69 6e 74 65  ither an.** inte
1cf38 67 65 72 20 6f 72 20 61 20 66 6c 6f 61 74 69 6e  ger or a floatin
1cf39 67 2d 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 29  g-point number.)
1cf3a 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65  .** If the value
1cf3b 20 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62   is text or blob
1cf3c 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74  , try to convert
1cf3d 20 69 74 20 74 6f 20 61 6e 20 75 73 69 6e 67 20   it to an using 
1cf3e 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e  the.** equivalen
1cf3f 74 20 6f 66 20 61 74 6f 69 28 29 20 6f 72 20 61  t of atoi() or a
1cf40 74 6f 66 28 29 20 61 6e 64 20 73 74 6f 72 65 20  tof() and store 
1cf41 30 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e  0 if no such con
1cf42 76 65 72 73 69 6f 6e 20 0a 2a 2a 20 69 73 20 70  version .** is p
1cf43 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41  ossible..**.** A
1cf44 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e   NULL value is n
1cf45 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68  ot changed by th
1cf46 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20  is routine.  It 
1cf47 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f  remains NULL..*/
1cf48 0a 63 61 73 65 20 4f 50 5f 54 6f 4e 75 6d 65 72  .case OP_ToNumer
1cf49 69 63 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  ic: {           
1cf4a 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1cf4b 73 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2c  s TK_TO_NUMERIC,
1cf4c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70   in1 */.  if( (p
1cf4d 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45  In1->flags & (ME
1cf4e 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 49 6e 74 7c 4d  M_Null|MEM_Int|M
1cf4f 45 4d 5f 52 65 61 6c 29 29 3d 3d 30 20 29 7b 0a  EM_Real))==0 ){.
1cf50 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1cf51 65 6d 4e 75 6d 65 72 69 66 79 28 70 49 6e 31 29  emNumerify(pIn1)
1cf52 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1cf53 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1cf54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 0a  E_OMIT_CAST */..
1cf55 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 49 6e 74  /* Opcode: ToInt
1cf56 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a   P1 * * * *.**.*
1cf57 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75  * Force the valu
1cf58 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
1cf59 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 2e 20   be an integer. 
1cf5a 20 49 66 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65   If.** The value
1cf5b 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 20   is currently a 
1cf5c 72 65 61 6c 20 6e 75 6d 62 65 72 2c 20 64 72 6f  real number, dro
1cf5d 70 20 69 74 73 20 66 72 61 63 74 69 6f 6e 61 6c  p its fractional
1cf5e 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20 74 68 65   part..** If the
1cf5f 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 20 6f   value is text o
1cf60 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63  r blob, try to c
1cf61 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 6e 20  onvert it to an 
1cf62 69 6e 74 65 67 65 72 20 75 73 69 6e 67 20 74 68  integer using th
1cf63 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20  e.** equivalent 
1cf64 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 20 73 74  of atoi() and st
1cf65 6f 72 65 20 30 20 69 66 20 6e 6f 20 73 75 63 68  ore 0 if no such
1cf66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70   conversion is p
1cf67 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41  ossible..**.** A
1cf68 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e   NULL value is n
1cf69 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68  ot changed by th
1cf6a 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20  is routine.  It 
1cf6b 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f  remains NULL..*/
1cf6c 0a 63 61 73 65 20 4f 50 5f 54 6f 49 6e 74 3a 20  .case OP_ToInt: 
1cf6d 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
1cf6e 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1cf6f 5f 54 4f 5f 49 4e 54 2c 20 69 6e 31 20 2a 2f 0a  _TO_INT, in1 */.
1cf70 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61    if( (pIn1->fla
1cf71 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d  gs & MEM_Null)==
1cf72 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
1cf73 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66  VdbeMemIntegerif
1cf74 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62  y(pIn1);.  }.  b
1cf75 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  reak;.}..#ifndef
1cf76 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53   SQLITE_OMIT_CAS
1cf77 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 52  T./* Opcode: ToR
1cf78 65 61 6c 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a  eal P1 * * * *.*
1cf79 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76  *.** Force the v
1cf7a 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1cf7b 20 50 31 20 74 6f 20 62 65 20 61 20 66 6c 6f 61   P1 to be a floa
1cf7c 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65  ting point numbe
1cf7d 72 2e 0a 2a 2a 20 49 66 20 54 68 65 20 76 61 6c  r..** If The val
1cf7e 75 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  ue is currently 
1cf7f 61 6e 20 69 6e 74 65 67 65 72 2c 20 63 6f 6e 76  an integer, conv
1cf80 65 72 74 20 69 74 2e 0a 2a 2a 20 49 66 20 74 68  ert it..** If th
1cf81 65 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 20  e value is text 
1cf82 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20  or blob, try to 
1cf83 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 6e  convert it to an
1cf84 20 69 6e 74 65 67 65 72 20 75 73 69 6e 67 20 74   integer using t
1cf85 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74  he.** equivalent
1cf86 20 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 20 73   of atoi() and s
1cf87 74 6f 72 65 20 30 2e 30 20 69 66 20 6e 6f 20 73  tore 0.0 if no s
1cf88 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69  uch conversion i
1cf89 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a  s possible..**.*
1cf8a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69  * A NULL value i
1cf8b 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79  s not changed by
1cf8c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20   this routine.  
1cf8d 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e  It remains NULL.
1cf8e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 52 65  .*/.case OP_ToRe
1cf8f 61 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  al: {           
1cf90 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1cf91 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 2c 20 69 6e  s TK_TO_REAL, in
1cf92 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31  1 */.  if( (pIn1
1cf93 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
1cf94 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  ll)==0 ){.    sq
1cf95 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c  lite3VdbeMemReal
1cf96 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20  ify(pIn1);.  }. 
1cf97 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1cf98 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1cf99 43 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f  CAST */../* Opco
1cf9a 64 65 3a 20 4c 74 20 50 31 20 50 32 20 50 33 20  de: Lt P1 P2 P3 
1cf9b 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70  P4 P5.**.** Comp
1cf9c 61 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 69  are the values i
1cf9d 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 61 6e  n register P1 an
1cf9e 64 20 50 33 2e 20 20 49 66 20 72 65 67 28 50 33  d P3.  If reg(P3
1cf9f 29 3c 72 65 67 28 50 31 29 20 74 68 65 6e 0a 2a  )<reg(P1) then.*
1cfa0 2a 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73  * jump to addres
1cfa1 73 20 50 32 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66  s P2.  .**.** If
1cfa2 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50   the SQLITE_JUMP
1cfa3 49 46 4e 55 4c 4c 20 62 69 74 20 6f 66 20 50 35  IFNULL bit of P5
1cfa4 20 69 73 20 73 65 74 20 61 6e 64 20 65 69 74 68   is set and eith
1cfa5 65 72 20 72 65 67 28 50 31 29 20 6f 72 0a 2a 2a  er reg(P1) or.**
1cfa6 20 72 65 67 28 50 33 29 20 69 73 20 4e 55 4c 4c   reg(P3) is NULL
1cfa7 20 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a   then take the j
1cfa8 75 6d 70 2e 20 20 49 66 20 74 68 65 20 53 51 4c  ump.  If the SQL
1cfa9 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 0a  ITE_JUMPIFNULL .
1cfaa 2a 2a 20 62 69 74 20 69 73 20 63 6c 65 61 72 20  ** bit is clear 
1cfab 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 20 69  then fall thru i
1cfac 66 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64  f either operand
1cfad 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   is NULL..**.** 
1cfae 54 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d  The SQLITE_AFF_M
1cfaf 41 53 4b 20 70 6f 72 74 69 6f 6e 20 6f 66 20 50  ASK portion of P
1cfb0 35 20 6d 75 73 74 20 62 65 20 61 6e 20 61 66 66  5 must be an aff
1cfb1 69 6e 69 74 79 20 63 68 61 72 61 63 74 65 72 20  inity character 
1cfb2 2d 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f  -.** SQLITE_AFF_
1cfb3 54 45 58 54 2c 20 53 51 4c 49 54 45 5f 41 46 46  TEXT, SQLITE_AFF
1cfb4 5f 49 4e 54 45 47 45 52 2c 20 61 6e 64 20 73 6f  _INTEGER, and so
1cfb5 20 66 6f 72 74 68 2e 20 41 6e 20 61 74 74 65 6d   forth. An attem
1cfb6 70 74 20 69 73 20 6d 61 64 65 20 0a 2a 2a 20 74  pt is made .** t
1cfb7 6f 20 63 6f 65 72 63 65 20 62 6f 74 68 20 69 6e  o coerce both in
1cfb8 70 75 74 73 20 61 63 63 6f 72 64 69 6e 67 20 74  puts according t
1cfb9 6f 20 74 68 69 73 20 61 66 66 69 6e 69 74 79 20  o this affinity 
1cfba 62 65 66 6f 72 65 20 74 68 65 0a 2a 2a 20 63 6f  before the.** co
1cfbb 6d 70 61 72 69 73 6f 6e 20 69 73 20 6d 61 64 65  mparison is made
1cfbc 2e 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f  . If the SQLITE_
1cfbd 41 46 46 5f 4d 41 53 4b 20 69 73 20 30 78 30 30  AFF_MASK is 0x00
1cfbe 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 0a 2a  , then numeric.*
1cfbf 2a 20 61 66 66 69 6e 69 74 79 20 69 73 20 75 73  * affinity is us
1cfc0 65 64 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68  ed. Note that th
1cfc1 65 20 61 66 66 69 6e 69 74 79 20 63 6f 6e 76 65  e affinity conve
1cfc2 72 73 69 6f 6e 73 20 61 72 65 20 73 74 6f 72 65  rsions are store
1cfc3 64 0a 2a 2a 20 62 61 63 6b 20 69 6e 74 6f 20 74  d.** back into t
1cfc4 68 65 20 69 6e 70 75 74 20 72 65 67 69 73 74 65  he input registe
1cfc5 72 73 20 50 31 20 61 6e 64 20 50 33 2e 20 20 53  rs P1 and P3.  S
1cfc6 6f 20 74 68 69 73 20 6f 70 63 6f 64 65 20 63 61  o this opcode ca
1cfc7 6e 20 63 61 75 73 65 0a 2a 2a 20 70 65 72 73 69  n cause.** persi
1cfc8 73 74 65 6e 74 20 63 68 61 6e 67 65 73 20 74 6f  stent changes to
1cfc9 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e   registers P1 an
1cfca 64 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65  d P3..**.** Once
1cfcb 20 61 6e 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73   any conversions
1cfcc 20 68 61 76 65 20 74 61 6b 65 6e 20 70 6c 61 63   have taken plac
1cfcd 65 2c 20 61 6e 64 20 6e 65 69 74 68 65 72 20 76  e, and neither v
1cfce 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20 0a 2a  alue is NULL, .*
1cfcf 2a 20 74 68 65 20 76 61 6c 75 65 73 20 61 72 65  * the values are
1cfd0 20 63 6f 6d 70 61 72 65 64 2e 20 49 66 20 62 6f   compared. If bo
1cfd1 74 68 20 76 61 6c 75 65 73 20 61 72 65 20 62 6c  th values are bl
1cfd2 6f 62 73 20 74 68 65 6e 20 6d 65 6d 63 6d 70 28  obs then memcmp(
1cfd3 29 20 69 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20  ) is.** used to 
1cfd4 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 72 65  determine the re
1cfd5 73 75 6c 74 73 20 6f 66 20 74 68 65 20 63 6f 6d  sults of the com
1cfd6 70 61 72 69 73 6f 6e 2e 20 20 49 66 20 62 6f 74  parison.  If bot
1cfd7 68 20 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20  h values.** are 
1cfd8 74 65 78 74 2c 20 74 68 65 6e 20 74 68 65 20 61  text, then the a
1cfd9 70 70 72 6f 70 72 69 61 74 65 20 63 6f 6c 6c 61  ppropriate colla
1cfda 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 70  ting function sp
1cfdb 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a 20 50 34  ecified in.** P4
1cfdc 20 69 73 20 20 75 73 65 64 20 74 6f 20 64 6f 20   is  used to do 
1cfdd 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20  the comparison. 
1cfde 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 73 70   If P4 is not sp
1cfdf 65 63 69 66 69 65 64 20 74 68 65 6e 0a 2a 2a 20  ecified then.** 
1cfe0 6d 65 6d 63 6d 70 28 29 20 69 73 20 75 73 65 64  memcmp() is used
1cfe1 20 74 6f 20 63 6f 6d 70 61 72 65 20 74 65 78 74   to compare text
1cfe2 20 73 74 72 69 6e 67 2e 20 20 49 66 20 62 6f 74   string.  If bot
1cfe3 68 20 76 61 6c 75 65 73 20 61 72 65 0a 2a 2a 20  h values are.** 
1cfe4 6e 75 6d 65 72 69 63 2c 20 74 68 65 6e 20 61 20  numeric, then a 
1cfe5 6e 75 6d 65 72 69 63 20 63 6f 6d 70 61 72 69 73  numeric comparis
1cfe6 6f 6e 20 69 73 20 75 73 65 64 2e 20 49 66 20 74  on is used. If t
1cfe7 68 65 20 74 77 6f 20 76 61 6c 75 65 73 0a 2a 2a  he two values.**
1cfe8 20 61 72 65 20 6f 66 20 64 69 66 66 65 72 65 6e   are of differen
1cfe9 74 20 74 79 70 65 73 2c 20 74 68 65 6e 20 6e 75  t types, then nu
1cfea 6d 62 65 72 73 20 61 72 65 20 63 6f 6e 73 69 64  mbers are consid
1cfeb 65 72 65 64 20 6c 65 73 73 20 74 68 61 6e 0a 2a  ered less than.*
1cfec 2a 20 73 74 72 69 6e 67 73 20 61 6e 64 20 73 74  * strings and st
1cfed 72 69 6e 67 73 20 61 72 65 20 63 6f 6e 73 69 64  rings are consid
1cfee 65 72 65 64 20 6c 65 73 73 20 74 68 61 6e 20 62  ered less than b
1cfef 6c 6f 62 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  lobs..**.** If t
1cff0 68 65 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50  he SQLITE_STOREP
1cff1 32 20 62 69 74 20 6f 66 20 50 35 20 69 73 20 73  2 bit of P5 is s
1cff2 65 74 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20  et, then do not 
1cff3 6a 75 6d 70 2e 20 20 49 6e 73 74 65 61 64 2c 0a  jump.  Instead,.
1cff4 2a 2a 20 73 74 6f 72 65 20 61 20 62 6f 6f 6c 65  ** store a boole
1cff5 61 6e 20 72 65 73 75 6c 74 20 28 65 69 74 68 65  an result (eithe
1cff6 72 20 30 2c 20 6f 72 20 31 2c 20 6f 72 20 4e 55  r 0, or 1, or NU
1cff7 4c 4c 29 20 69 6e 20 72 65 67 69 73 74 65 72 20  LL) in register 
1cff8 50 32 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  P2..*/./* Opcode
1cff9 3a 20 4e 65 20 50 31 20 50 32 20 50 33 20 50 34  : Ne P1 P2 P3 P4
1cffa 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77   P5.**.** This w
1cffb 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74  orks just like t
1cffc 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63  he Lt opcode exc
1cffd 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d  ept that the jum
1cffe 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a  p is taken if.**
1cfff 20 74 68 65 20 6f 70 65 72 61 6e 64 73 20 69 6e   the operands in
1d000 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e   registers P1 an
1d001 64 20 50 33 20 61 72 65 20 6e 6f 74 20 65 71 75  d P3 are not equ
1d002 61 6c 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20  al.  See the Lt 
1d003 6f 70 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 61 64  opcode for.** ad
1d004 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
1d005 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53  tion..**.** If S
1d006 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 69 73 20  QLITE_NULLEQ is 
1d007 73 65 74 20 69 6e 20 50 35 20 74 68 65 6e 20 74  set in P5 then t
1d008 68 65 20 72 65 73 75 6c 74 20 6f 66 20 63 6f 6d  he result of com
1d009 70 61 72 69 73 6f 6e 20 69 73 20 61 6c 77 61 79  parison is alway
1d00a 73 20 65 69 74 68 65 72 0a 2a 2a 20 74 72 75 65  s either.** true
1d00b 20 6f 72 20 66 61 6c 73 65 20 61 6e 64 20 69 73   or false and is
1d00c 20 6e 65 76 65 72 20 4e 55 4c 4c 2e 20 20 49 66   never NULL.  If
1d00d 20 62 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 61   both operands a
1d00e 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65  re NULL then the
1d00f 20 72 65 73 75 6c 74 0a 2a 2a 20 6f 66 20 63 6f   result.** of co
1d010 6d 70 61 72 69 73 6f 6e 20 69 73 20 66 61 6c 73  mparison is fals
1d011 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 70  e.  If either op
1d012 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68  erand is NULL th
1d013 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  en the result is
1d014 20 74 72 75 65 2e 0a 2a 2a 20 49 66 20 6e 65 69   true..** If nei
1d015 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20  ther operand is 
1d016 4e 55 4c 4c 20 74 68 65 20 74 68 65 20 72 65 73  NULL the the res
1d017 75 6c 74 20 69 73 20 74 68 65 20 73 61 6d 65 20  ult is the same 
1d018 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20 69  as it would be i
1d019 66 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f  f.** the SQLITE_
1d01a 4e 55 4c 4c 45 51 20 66 6c 61 67 20 77 65 72 65  NULLEQ flag were
1d01b 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 50 35   omitted from P5
1d01c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1d01d 45 71 20 50 31 20 50 32 20 50 33 20 50 34 20 50  Eq P1 P2 P3 P4 P
1d01e 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72  5.**.** This wor
1d01f 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65  ks just like the
1d020 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70   Lt opcode excep
1d021 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20  t that the jump 
1d022 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74  is taken if.** t
1d023 68 65 20 6f 70 65 72 61 6e 64 73 20 69 6e 20 72  he operands in r
1d024 65 67 69 73 74 65 72 73 20 50 31 20 61 6e 64 20  egisters P1 and 
1d025 50 33 20 61 72 65 20 65 71 75 61 6c 2e 0a 2a 2a  P3 are equal..**
1d026 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f   See the Lt opco
1d027 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  de for additiona
1d028 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  l information..*
1d029 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4e  *.** If SQLITE_N
1d02a 55 4c 4c 45 51 20 69 73 20 73 65 74 20 69 6e 20  ULLEQ is set in 
1d02b 50 35 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  P5 then the resu
1d02c 6c 74 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e  lt of comparison
1d02d 20 69 73 20 61 6c 77 61 79 73 20 65 69 74 68 65   is always eithe
1d02e 72 0a 2a 2a 20 74 72 75 65 20 6f 72 20 66 61 6c  r.** true or fal
1d02f 73 65 20 61 6e 64 20 69 73 20 6e 65 76 65 72 20  se and is never 
1d030 4e 55 4c 4c 2e 20 20 49 66 20 62 6f 74 68 20 6f  NULL.  If both o
1d031 70 65 72 61 6e 64 73 20 61 72 65 20 4e 55 4c 4c  perands are NULL
1d032 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74   then the result
1d033 0a 2a 2a 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f  .** of compariso
1d034 6e 20 69 73 20 74 72 75 65 2e 20 20 49 66 20 65  n is true.  If e
1d035 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73  ither operand is
1d036 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 72   NULL then the r
1d037 65 73 75 6c 74 20 69 73 20 66 61 6c 73 65 2e 0a  esult is false..
1d038 2a 2a 20 49 66 20 6e 65 69 74 68 65 72 20 6f 70  ** If neither op
1d039 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68  erand is NULL th
1d03a 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  e the result is 
1d03b 74 68 65 20 73 61 6d 65 20 61 73 20 69 74 20 77  the same as it w
1d03c 6f 75 6c 64 20 62 65 20 69 66 0a 2a 2a 20 74 68  ould be if.** th
1d03d 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20  e SQLITE_NULLEQ 
1d03e 66 6c 61 67 20 77 65 72 65 20 6f 6d 69 74 74 65  flag were omitte
1d03f 64 20 66 72 6f 6d 20 50 35 2e 0a 2a 2f 0a 2f 2a  d from P5..*/./*
1d040 20 4f 70 63 6f 64 65 3a 20 4c 65 20 50 31 20 50   Opcode: Le P1 P
1d041 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a  2 P3 P4 P5.**.**
1d042 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74   This works just
1d043 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63   like the Lt opc
1d044 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20  ode except that 
1d045 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65  the jump is take
1d046 6e 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74  n if.** the cont
1d047 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20  ent of register 
1d048 50 33 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  P3 is less than 
1d049 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  or equal to the 
1d04a 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65  content of.** re
1d04b 67 69 73 74 65 72 20 50 31 2e 20 20 53 65 65 20  gister P1.  See 
1d04c 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f  the Lt opcode fo
1d04d 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
1d04e 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20  ormation..*/./* 
1d04f 4f 70 63 6f 64 65 3a 20 47 74 20 50 31 20 50 32  Opcode: Gt P1 P2
1d050 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20   P3 P4 P5.**.** 
1d051 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20  This works just 
1d052 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f  like the Lt opco
1d053 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74  de except that t
1d054 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e  he jump is taken
1d055 20 69 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65   if.** the conte
1d056 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50  nt of register P
1d057 33 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  3 is greater tha
1d058 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  n the content of
1d059 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 2e  .** register P1.
1d05a 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63    See the Lt opc
1d05b 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  ode for addition
1d05c 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
1d05d 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 65  */./* Opcode: Ge
1d05e 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a   P1 P2 P3 P4 P5.
1d05f 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73  **.** This works
1d060 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c   just like the L
1d061 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20  t opcode except 
1d062 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73  that the jump is
1d063 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65   taken if.** the
1d064 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69   content of regi
1d065 73 74 65 72 20 50 33 20 69 73 20 67 72 65 61 74  ster P3 is great
1d066 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  er than or equal
1d067 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20   to the content 
1d068 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50  of.** register P
1d069 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f  1.  See the Lt o
1d06a 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69  pcode for additi
1d06b 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
1d06c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 45 71 3a  ..*/.case OP_Eq:
1d06d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1d06e 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 45 51 2c  * same as TK_EQ,
1d06f 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20   jump, in1, in3 
1d070 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 3a 20 20  */.case OP_Ne:  
1d071 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d072 73 61 6d 65 20 61 73 20 54 4b 5f 4e 45 2c 20 6a  same as TK_NE, j
1d073 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f  ump, in1, in3 */
1d074 0a 63 61 73 65 20 4f 50 5f 4c 74 3a 20 20 20 20  .case OP_Lt:    
1d075 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1d076 6d 65 20 61 73 20 54 4b 5f 4c 54 2c 20 6a 75 6d  me as TK_LT, jum
1d077 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63  p, in1, in3 */.c
1d078 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20 20 20 20  ase OP_Le:      
1d079 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
1d07a 20 61 73 20 54 4b 5f 4c 45 2c 20 6a 75 6d 70 2c   as TK_LE, jump,
1d07b 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73   in1, in3 */.cas
1d07c 65 20 4f 50 5f 47 74 3a 20 20 20 20 20 20 20 20  e OP_Gt:        
1d07d 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1d07e 73 20 54 4b 5f 47 54 2c 20 6a 75 6d 70 2c 20 69  s TK_GT, jump, i
1d07f 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20  n1, in3 */.case 
1d080 4f 50 5f 47 65 3a 20 7b 20 20 20 20 20 20 20 20  OP_Ge: {        
1d081 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1d082 54 4b 5f 47 45 2c 20 6a 75 6d 70 2c 20 69 6e 31  TK_GE, jump, in1
1d083 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20  , in3 */.#if 0  
1d084 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1d085 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1d086 61 69 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 3b  ai */.  int res;
1d087 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
1d088 65 73 75 6c 74 20 6f 66 20 74 68 65 20 63 6f 6d  esult of the com
1d089 70 61 72 69 73 6f 6e 20 6f 66 20 70 49 6e 31 20  parison of pIn1 
1d08a 61 67 61 69 6e 73 74 20 70 49 6e 33 20 2a 2f 0a  against pIn3 */.
1d08b 20 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b    char affinity;
1d08c 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74        /* Affinit
1d08d 79 20 74 6f 20 75 73 65 20 66 6f 72 20 63 6f 6d  y to use for com
1d08e 70 61 72 69 73 6f 6e 20 2a 2f 0a 23 65 6e 64 69  parison */.#endi
1d08f 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1d090 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1d091 75 2e 61 69 20 2a 2f 0a 0a 20 20 69 66 28 20 28  u.ai */..  if( (
1d092 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49  pIn1->flags | pI
1d093 6e 33 2d 3e 66 6c 61 67 73 29 26 4d 45 4d 5f 4e  n3->flags)&MEM_N
1d094 75 6c 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 6e  ull ){.    /* On
1d095 65 20 6f 72 20 62 6f 74 68 20 6f 70 65 72 61 6e  e or both operan
1d096 64 73 20 61 72 65 20 4e 55 4c 4c 20 2a 2f 0a 20  ds are NULL */. 
1d097 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26     if( pOp->p5 &
1d098 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 29   SQLITE_NULLEQ )
1d099 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 53 51  {.      /* If SQ
1d09a 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 69 73 20 73  LITE_NULLEQ is s
1d09b 65 74 20 28 77 68 69 63 68 20 77 69 6c 6c 20 6f  et (which will o
1d09c 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 20 74 68  nly happen if th
1d09d 65 20 6f 70 65 72 61 74 6f 72 20 69 73 0a 20 20  e operator is.  
1d09e 20 20 20 20 2a 2a 20 4f 50 5f 45 71 20 6f 72 20      ** OP_Eq or 
1d09f 4f 50 5f 4e 65 29 20 74 68 65 6e 20 74 61 6b 65  OP_Ne) then take
1d0a0 20 74 68 65 20 6a 75 6d 70 20 6f 72 20 6e 6f 74   the jump or not
1d0a1 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68   depending on wh
1d0a2 65 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 6f  ether.      ** o
1d0a3 72 20 6e 6f 74 20 62 6f 74 68 20 6f 70 65 72 61  r not both opera
1d0a4 6e 64 73 20 61 72 65 20 6e 75 6c 6c 2e 0a 20 20  nds are null..  
1d0a5 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73      */.      ass
1d0a6 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  ert( pOp->opcode
1d0a7 3d 3d 4f 50 5f 45 71 20 7c 7c 20 70 4f 70 2d 3e  ==OP_Eq || pOp->
1d0a8 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 65 20 29 3b  opcode==OP_Ne );
1d0a9 0a 20 20 20 20 20 20 75 2e 61 69 2e 72 65 73 20  .      u.ai.res 
1d0aa 3d 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  = (pIn1->flags &
1d0ab 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d   pIn3->flags & M
1d0ac 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 3b 0a 20 20 20  EM_Null)==0;.   
1d0ad 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a   }else{.      /*
1d0ae 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 69   SQLITE_NULLEQ i
1d0af 73 20 63 6c 65 61 72 20 61 6e 64 20 61 74 20 6c  s clear and at l
1d0b0 65 61 73 74 20 6f 6e 65 20 6f 70 65 72 61 6e 64  east one operand
1d0b1 20 69 73 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20   is NULL,.      
1d0b2 2a 2a 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  ** then the resu
1d0b3 6c 74 20 69 73 20 61 6c 77 61 79 73 20 4e 55 4c  lt is always NUL
1d0b4 4c 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20  L..      ** The 
1d0b5 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66  jump is taken if
1d0b6 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50   the SQLITE_JUMP
1d0b7 49 46 4e 55 4c 4c 20 62 69 74 20 69 73 20 73 65  IFNULL bit is se
1d0b8 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  t..      */.    
1d0b9 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20    if( pOp->p5 & 
1d0ba 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 29  SQLITE_STOREP2 )
1d0bb 7b 0a 20 20 20 20 20 20 20 20 70 4f 75 74 20 3d  {.        pOut =
1d0bc 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1d0bd 32 5d 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 53  2];.        MemS
1d0be 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c  etTypeFlag(pOut,
1d0bf 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 20 20   MEM_Null);.    
1d0c0 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41      REGISTER_TRA
1d0c1 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74  CE(pOp->p2, pOut
1d0c2 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  );.      }else i
1d0c3 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c  f( pOp->p5 & SQL
1d0c4 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 29  ITE_JUMPIFNULL )
1d0c5 7b 0a 20 20 20 20 20 20 20 20 70 63 20 3d 20 70  {.        pc = p
1d0c6 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 20 20 20 20  Op->p2-1;.      
1d0c7 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
1d0c8 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
1d0c9 20 20 2f 2a 20 4e 65 69 74 68 65 72 20 6f 70 65    /* Neither ope
1d0ca 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2e 20 20 44  rand is NULL.  D
1d0cb 6f 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20  o a comparison. 
1d0cc 2a 2f 0a 20 20 20 20 75 2e 61 69 2e 61 66 66 69  */.    u.ai.affi
1d0cd 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 35 20 26  nity = pOp->p5 &
1d0ce 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b   SQLITE_AFF_MASK
1d0cf 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 69 2e 61  ;.    if( u.ai.a
1d0d0 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 20  ffinity ){.     
1d0d1 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70   applyAffinity(p
1d0d2 49 6e 31 2c 20 75 2e 61 69 2e 61 66 66 69 6e 69  In1, u.ai.affini
1d0d3 74 79 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20  ty, encoding);. 
1d0d4 20 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69       applyAffini
1d0d5 74 79 28 70 49 6e 33 2c 20 75 2e 61 69 2e 61 66  ty(pIn3, u.ai.af
1d0d6 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67  finity, encoding
1d0d7 29 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d  );.      if( db-
1d0d8 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
1d0d9 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20  goto no_mem;.   
1d0da 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20   }..    assert( 
1d0db 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
1d0dc 43 4f 4c 4c 53 45 51 20 7c 7c 20 70 4f 70 2d 3e  COLLSEQ || pOp->
1d0dd 70 34 2e 70 43 6f 6c 6c 3d 3d 30 20 29 3b 0a 20  p4.pColl==0 );. 
1d0de 20 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49     ExpandBlob(pI
1d0df 6e 31 29 3b 0a 20 20 20 20 45 78 70 61 6e 64 42  n1);.    ExpandB
1d0e0 6c 6f 62 28 70 49 6e 33 29 3b 0a 20 20 20 20 75  lob(pIn3);.    u
1d0e1 2e 61 69 2e 72 65 73 20 3d 20 73 71 6c 69 74 65  .ai.res = sqlite
1d0e2 33 4d 65 6d 43 6f 6d 70 61 72 65 28 70 49 6e 33  3MemCompare(pIn3
1d0e3 2c 20 70 49 6e 31 2c 20 70 4f 70 2d 3e 70 34 2e  , pIn1, pOp->p4.
1d0e4 70 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20 73 77  pColl);.  }.  sw
1d0e5 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64  itch( pOp->opcod
1d0e6 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4f 50  e ){.    case OP
1d0e7 5f 45 71 3a 20 20 20 20 75 2e 61 69 2e 72 65 73  _Eq:    u.ai.res
1d0e8 20 3d 20 75 2e 61 69 2e 72 65 73 3d 3d 30 3b 20   = u.ai.res==0; 
1d0e9 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
1d0ea 61 73 65 20 4f 50 5f 4e 65 3a 20 20 20 20 75 2e  ase OP_Ne:    u.
1d0eb 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65  ai.res = u.ai.re
1d0ec 73 21 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b  s!=0;     break;
1d0ed 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4c 74 3a  .    case OP_Lt:
1d0ee 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 75      u.ai.res = u
1d0ef 2e 61 69 2e 72 65 73 3c 30 3b 20 20 20 20 20 20  .ai.res<0;      
1d0f0 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
1d0f1 4f 50 5f 4c 65 3a 20 20 20 20 75 2e 61 69 2e 72  OP_Le:    u.ai.r
1d0f2 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 3c 3d 30  es = u.ai.res<=0
1d0f3 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  ;     break;.   
1d0f4 20 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20 20   case OP_Gt:    
1d0f5 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e  u.ai.res = u.ai.
1d0f6 72 65 73 3e 30 3b 20 20 20 20 20 20 62 72 65 61  res>0;      brea
1d0f7 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  k;.    default: 
1d0f8 20 20 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d        u.ai.res =
1d0f9 20 75 2e 61 69 2e 72 65 73 3e 3d 30 3b 20 20 20   u.ai.res>=0;   
1d0fa 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20    break;.  }..  
1d0fb 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51  if( pOp->p5 & SQ
1d0fc 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 29 7b 0a  LITE_STOREP2 ){.
1d0fd 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61      pOut = &p->a
1d0fe 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20  Mem[pOp->p2];.  
1d0ff 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1d100 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b  (pOut, MEM_Int);
1d101 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d  .    pOut->u.i =
1d102 20 75 2e 61 69 2e 72 65 73 3b 0a 20 20 20 20 52   u.ai.res;.    R
1d103 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
1d104 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20  p->p2, pOut);.  
1d105 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 69 2e 72  }else if( u.ai.r
1d106 65 73 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70  es ){.    pc = p
1d107 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20  Op->p2-1;.  }.  
1d108 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1d109 6f 64 65 3a 20 50 65 72 6d 75 74 61 74 69 6f 6e  ode: Permutation
1d10a 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a   * * * P4 *.**.*
1d10b 2a 20 53 65 74 20 74 68 65 20 70 65 72 6d 75 74  * Set the permut
1d10c 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68  ation used by th
1d10d 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 6f 70 65  e OP_Compare ope
1d10e 72 61 74 6f 72 20 74 6f 20 62 65 20 74 68 65 20  rator to be the 
1d10f 61 72 72 61 79 0a 2a 2a 20 6f 66 20 69 6e 74 65  array.** of inte
1d110 67 65 72 73 20 69 6e 20 50 34 2e 0a 2a 2a 0a 2a  gers in P4..**.*
1d111 2a 20 54 68 65 20 70 65 72 6d 75 74 61 74 69 6f  * The permutatio
1d112 6e 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20  n is only valid 
1d113 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20 4f  until the next O
1d114 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 2c 20 4f  P_Permutation, O
1d115 50 5f 43 6f 6d 70 61 72 65 2c 0a 2a 2a 20 4f 50  P_Compare,.** OP
1d116 5f 48 61 6c 74 2c 20 6f 72 20 4f 50 5f 52 65 73  _Halt, or OP_Res
1d117 75 6c 74 52 6f 77 2e 20 20 54 79 70 69 63 61 6c  ultRow.  Typical
1d118 6c 79 20 74 68 65 20 4f 50 5f 50 65 72 6d 75 74  ly the OP_Permut
1d119 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 63 63  ation should occ
1d11a 75 72 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c  ur.** immediatel
1d11b 79 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 4f  y prior to the O
1d11c 50 5f 43 6f 6d 70 61 72 65 2e 0a 2a 2f 0a 63 61  P_Compare..*/.ca
1d11d 73 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f  se OP_Permutatio
1d11e 6e 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20 70  n: {.  assert( p
1d11f 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 49  Op->p4type==P4_I
1d120 4e 54 41 52 52 41 59 20 29 3b 0a 20 20 61 73 73  NTARRAY );.  ass
1d121 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 61 69 20  ert( pOp->p4.ai 
1d122 29 3b 0a 20 20 61 50 65 72 6d 75 74 65 20 3d 20  );.  aPermute = 
1d123 70 4f 70 2d 3e 70 34 2e 61 69 3b 0a 20 20 62 72  pOp->p4.ai;.  br
1d124 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1d125 65 3a 20 43 6f 6d 70 61 72 65 20 50 31 20 50 32  e: Compare P1 P2
1d126 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43   P3 P4 *.**.** C
1d127 6f 6d 70 61 72 65 20 74 6f 20 76 65 63 74 6f 72  ompare to vector
1d128 73 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 69  s of registers i
1d129 6e 20 72 65 67 28 50 31 29 2e 2e 72 65 67 28 50  n reg(P1)..reg(P
1d12a 31 2b 50 33 2d 31 29 20 28 61 6c 6c 20 74 68 69  1+P3-1) (all thi
1d12b 73 0a 2a 2a 20 6f 6e 65 20 22 41 22 29 20 61 6e  s.** one "A") an
1d12c 64 20 69 6e 20 72 65 67 28 50 32 29 2e 2e 72 65  d in reg(P2)..re
1d12d 67 28 50 32 2b 50 33 2d 31 29 20 28 22 42 22 29  g(P2+P3-1) ("B")
1d12e 2e 20 20 53 61 76 65 20 74 68 65 20 72 65 73 75  .  Save the resu
1d12f 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6d  lt of.** the com
1d130 70 61 72 69 73 6f 6e 20 66 6f 72 20 75 73 65 20  parison for use 
1d131 62 79 20 74 68 65 20 6e 65 78 74 20 4f 50 5f 4a  by the next OP_J
1d132 75 6d 70 20 69 6e 73 74 72 75 63 74 2e 0a 2a 2a  ump instruct..**
1d133 0a 2a 2a 20 50 34 20 69 73 20 61 20 4b 65 79 49  .** P4 is a KeyI
1d134 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68  nfo structure th
1d135 61 74 20 64 65 66 69 6e 65 73 20 63 6f 6c 6c 61  at defines colla
1d136 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 61  ting sequences a
1d137 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f 72 64 65 72  nd sort.** order
1d138 73 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 61 72  s for the compar
1d139 69 73 6f 6e 2e 20 20 54 68 65 20 70 65 72 6d 75  ison.  The permu
1d13a 74 61 74 69 6f 6e 20 61 70 70 6c 69 65 73 20 74  tation applies t
1d13b 6f 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 6f  o registers.** o
1d13c 6e 6c 79 2e 20 20 54 68 65 20 4b 65 79 49 6e 66  nly.  The KeyInf
1d13d 6f 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 75  o elements are u
1d13e 73 65 64 20 73 65 71 75 65 6e 74 69 61 6c 6c 79  sed sequentially
1d13f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6d 70  ..**.** The comp
1d140 61 72 69 73 6f 6e 20 69 73 20 61 20 73 6f 72 74  arison is a sort
1d141 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20 73 6f 20   comparison, so 
1d142 4e 55 4c 4c 73 20 63 6f 6d 70 61 72 65 20 65 71  NULLs compare eq
1d143 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c 73 20 61 72  ual,.** NULLs ar
1d144 65 20 6c 65 73 73 20 74 68 61 6e 20 6e 75 6d 62  e less than numb
1d145 65 72 73 2c 20 6e 75 6d 62 65 72 73 20 61 72 65  ers, numbers are
1d146 20 6c 65 73 73 20 74 68 61 6e 20 73 74 72 69 6e   less than strin
1d147 67 73 2c 0a 2a 2a 20 61 6e 64 20 73 74 72 69 6e  gs,.** and strin
1d148 67 73 20 61 72 65 20 6c 65 73 73 20 74 68 61 6e  gs are less than
1d149 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63 61 73 65 20   blobs..*/.case 
1d14a 4f 50 5f 43 6f 6d 70 61 72 65 3a 20 7b 0a 23 69  OP_Compare: {.#i
1d14b 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1d14c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1d14d 74 6f 20 75 2e 61 6a 20 2a 2f 0a 20 20 69 6e 74  to u.aj */.  int
1d14e 20 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69   n;.  int i;.  i
1d14f 6e 74 20 70 31 3b 0a 20 20 69 6e 74 20 70 32 3b  nt p1;.  int p2;
1d150 0a 20 20 63 6f 6e 73 74 20 4b 65 79 49 6e 66 6f  .  const KeyInfo
1d151 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 69 6e   *pKeyInfo;.  in
1d152 74 20 69 64 78 3b 0a 20 20 43 6f 6c 6c 53 65 71  t idx;.  CollSeq
1d153 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43   *pColl;    /* C
1d154 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
1d155 65 20 74 6f 20 75 73 65 20 6f 6e 20 74 68 69 73  e to use on this
1d156 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 62   term */.  int b
1d157 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Rev;          /*
1d158 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 45 4e   True for DESCEN
1d159 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 65 72 20  DING sort order 
1d15a 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  */.#endif /* loc
1d15b 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1d15c 65 64 20 69 6e 74 6f 20 75 2e 61 6a 20 2a 2f 0a  ed into u.aj */.
1d15d 0a 20 20 75 2e 61 6a 2e 6e 20 3d 20 70 4f 70 2d  .  u.aj.n = pOp-
1d15e 3e 70 33 3b 0a 20 20 75 2e 61 6a 2e 70 4b 65 79  >p3;.  u.aj.pKey
1d15f 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70  Info = pOp->p4.p
1d160 4b 65 79 49 6e 66 6f 3b 0a 20 20 61 73 73 65 72  KeyInfo;.  asser
1d161 74 28 20 75 2e 61 6a 2e 6e 3e 30 20 29 3b 0a 20  t( u.aj.n>0 );. 
1d162 20 61 73 73 65 72 74 28 20 75 2e 61 6a 2e 70 4b   assert( u.aj.pK
1d163 65 79 49 6e 66 6f 21 3d 30 20 29 3b 0a 20 20 75  eyInfo!=0 );.  u
1d164 2e 61 6a 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31  .aj.p1 = pOp->p1
1d165 3b 0a 20 20 75 2e 61 6a 2e 70 32 20 3d 20 70 4f  ;.  u.aj.p2 = pO
1d166 70 2d 3e 70 32 3b 0a 23 69 66 20 53 51 4c 49 54  p->p2;.#if SQLIT
1d167 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 61 50  E_DEBUG.  if( aP
1d168 65 72 6d 75 74 65 20 29 7b 0a 20 20 20 20 69 6e  ermute ){.    in
1d169 74 20 6b 2c 20 6d 78 20 3d 20 30 3b 0a 20 20 20  t k, mx = 0;.   
1d16a 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 75 2e 61 6a   for(k=0; k<u.aj
1d16b 2e 6e 3b 20 6b 2b 2b 29 20 69 66 28 20 61 50 65  .n; k++) if( aPe
1d16c 72 6d 75 74 65 5b 6b 5d 3e 6d 78 20 29 20 6d 78  rmute[k]>mx ) mx
1d16d 20 3d 20 61 50 65 72 6d 75 74 65 5b 6b 5d 3b 0a   = aPermute[k];.
1d16e 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6a      assert( u.aj
1d16f 2e 70 31 3e 30 20 26 26 20 75 2e 61 6a 2e 70 31  .p1>0 && u.aj.p1
1d170 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29  +mx<=p->nMem+1 )
1d171 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e  ;.    assert( u.
1d172 61 6a 2e 70 32 3e 30 20 26 26 20 75 2e 61 6a 2e  aj.p2>0 && u.aj.
1d173 70 32 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31  p2+mx<=p->nMem+1
1d174 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   );.  }else{.   
1d175 20 61 73 73 65 72 74 28 20 75 2e 61 6a 2e 70 31   assert( u.aj.p1
1d176 3e 30 20 26 26 20 75 2e 61 6a 2e 70 31 2b 75 2e  >0 && u.aj.p1+u.
1d177 61 6a 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20  aj.n<=p->nMem+1 
1d178 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  );.    assert( u
1d179 2e 61 6a 2e 70 32 3e 30 20 26 26 20 75 2e 61 6a  .aj.p2>0 && u.aj
1d17a 2e 70 32 2b 75 2e 61 6a 2e 6e 3c 3d 70 2d 3e 6e  .p2+u.aj.n<=p->n
1d17b 4d 65 6d 2b 31 20 29 3b 0a 20 20 7d 0a 23 65 6e  Mem+1 );.  }.#en
1d17c 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45  dif /* SQLITE_DE
1d17d 42 55 47 20 2a 2f 0a 20 20 66 6f 72 28 75 2e 61  BUG */.  for(u.a
1d17e 6a 2e 69 3d 30 3b 20 75 2e 61 6a 2e 69 3c 75 2e  j.i=0; u.aj.i<u.
1d17f 61 6a 2e 6e 3b 20 75 2e 61 6a 2e 69 2b 2b 29 7b  aj.n; u.aj.i++){
1d180 0a 20 20 20 20 75 2e 61 6a 2e 69 64 78 20 3d 20  .    u.aj.idx = 
1d181 61 50 65 72 6d 75 74 65 20 3f 20 61 50 65 72 6d  aPermute ? aPerm
1d182 75 74 65 5b 75 2e 61 6a 2e 69 5d 20 3a 20 75 2e  ute[u.aj.i] : u.
1d183 61 6a 2e 69 3b 0a 20 20 20 20 52 45 47 49 53 54  aj.i;.    REGIST
1d184 45 52 5f 54 52 41 43 45 28 75 2e 61 6a 2e 70 31  ER_TRACE(u.aj.p1
1d185 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 70 2d 3e 61  +u.aj.idx, &p->a
1d186 4d 65 6d 5b 75 2e 61 6a 2e 70 31 2b 75 2e 61 6a  Mem[u.aj.p1+u.aj
1d187 2e 69 64 78 5d 29 3b 0a 20 20 20 20 52 45 47 49  .idx]);.    REGI
1d188 53 54 45 52 5f 54 52 41 43 45 28 75 2e 61 6a 2e  STER_TRACE(u.aj.
1d189 70 32 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 70 2d  p2+u.aj.idx, &p-
1d18a 3e 61 4d 65 6d 5b 75 2e 61 6a 2e 70 32 2b 75 2e  >aMem[u.aj.p2+u.
1d18b 61 6a 2e 69 64 78 5d 29 3b 0a 20 20 20 20 61 73  aj.idx]);.    as
1d18c 73 65 72 74 28 20 75 2e 61 6a 2e 69 3c 75 2e 61  sert( u.aj.i<u.a
1d18d 6a 2e 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65  j.pKeyInfo->nFie
1d18e 6c 64 20 29 3b 0a 20 20 20 20 75 2e 61 6a 2e 70  ld );.    u.aj.p
1d18f 43 6f 6c 6c 20 3d 20 75 2e 61 6a 2e 70 4b 65 79  Coll = u.aj.pKey
1d190 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 75 2e 61 6a  Info->aColl[u.aj
1d191 2e 69 5d 3b 0a 20 20 20 20 75 2e 61 6a 2e 62 52  .i];.    u.aj.bR
1d192 65 76 20 3d 20 75 2e 61 6a 2e 70 4b 65 79 49 6e  ev = u.aj.pKeyIn
1d193 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 75  fo->aSortOrder[u
1d194 2e 61 6a 2e 69 5d 3b 0a 20 20 20 20 69 43 6f 6d  .aj.i];.    iCom
1d195 70 61 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 65  pare = sqlite3Me
1d196 6d 43 6f 6d 70 61 72 65 28 26 70 2d 3e 61 4d 65  mCompare(&p->aMe
1d197 6d 5b 75 2e 61 6a 2e 70 31 2b 75 2e 61 6a 2e 69  m[u.aj.p1+u.aj.i
1d198 64 78 5d 2c 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e  dx], &p->aMem[u.
1d199 61 6a 2e 70 32 2b 75 2e 61 6a 2e 69 64 78 5d 2c  aj.p2+u.aj.idx],
1d19a 20 75 2e 61 6a 2e 70 43 6f 6c 6c 29 3b 0a 20 20   u.aj.pColl);.  
1d19b 20 20 69 66 28 20 69 43 6f 6d 70 61 72 65 20 29    if( iCompare )
1d19c 7b 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 6a  {.      if( u.aj
1d19d 2e 62 52 65 76 20 29 20 69 43 6f 6d 70 61 72 65  .bRev ) iCompare
1d19e 20 3d 20 2d 69 43 6f 6d 70 61 72 65 3b 0a 20 20   = -iCompare;.  
1d19f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1d1a0 0a 20 20 7d 0a 20 20 61 50 65 72 6d 75 74 65 20  .  }.  aPermute 
1d1a1 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  = 0;.  break;.}.
1d1a2 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4a 75 6d 70  ./* Opcode: Jump
1d1a3 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1d1a4 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20  .** Jump to the 
1d1a5 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 74 20 61  instruction at a
1d1a6 64 64 72 65 73 73 20 50 31 2c 20 50 32 2c 20 6f  ddress P1, P2, o
1d1a7 72 20 50 33 20 64 65 70 65 6e 64 69 6e 67 20 6f  r P3 depending o
1d1a8 6e 20 77 68 65 74 68 65 72 0a 2a 2a 20 69 6e 20  n whether.** in 
1d1a9 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
1d1aa 4f 50 5f 43 6f 6d 70 61 72 65 20 69 6e 73 74 72  OP_Compare instr
1d1ab 75 63 74 69 6f 6e 20 74 68 65 20 50 31 20 76 65  uction the P1 ve
1d1ac 63 74 6f 72 20 77 61 73 20 6c 65 73 73 20 74 68  ctor was less th
1d1ad 61 6e 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c 20  an.** equal to, 
1d1ae 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  or greater than 
1d1af 74 68 65 20 50 32 20 76 65 63 74 6f 72 2c 20 72  the P2 vector, r
1d1b0 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2f 0a  espectively..*/.
1d1b1 63 61 73 65 20 4f 50 5f 4a 75 6d 70 3a 20 7b 20  case OP_Jump: { 
1d1b2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a              /* j
1d1b3 75 6d 70 20 2a 2f 0a 20 20 69 66 28 20 69 43 6f  ump */.  if( iCo
1d1b4 6d 70 61 72 65 3c 30 20 29 7b 0a 20 20 20 20 70  mpare<0 ){.    p
1d1b5 63 20 3d 20 70 4f 70 2d 3e 70 31 20 2d 20 31 3b  c = pOp->p1 - 1;
1d1b6 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 43 6f  .  }else if( iCo
1d1b7 6d 70 61 72 65 3d 3d 30 20 29 7b 0a 20 20 20 20  mpare==0 ){.    
1d1b8 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1d1b9 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
1d1ba 63 20 3d 20 70 4f 70 2d 3e 70 33 20 2d 20 31 3b  c = pOp->p3 - 1;
1d1bb 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1d1bc 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 6e 64 20  ./* Opcode: And 
1d1bd 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1d1be 2a 2a 20 54 61 6b 65 20 74 68 65 20 6c 6f 67 69  ** Take the logi
1d1bf 63 61 6c 20 41 4e 44 20 6f 66 20 74 68 65 20 76  cal AND of the v
1d1c0 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65  alues in registe
1d1c1 72 73 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64  rs P1 and P2 and
1d1c2 0a 2a 2a 20 77 72 69 74 65 20 74 68 65 20 72 65  .** write the re
1d1c3 73 75 6c 74 20 69 6e 74 6f 20 72 65 67 69 73 74  sult into regist
1d1c4 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  er P3..**.** If 
1d1c5 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20  either P1 or P2 
1d1c6 69 73 20 30 20 28 66 61 6c 73 65 29 20 74 68 65  is 0 (false) the
1d1c7 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  n the result is 
1d1c8 30 20 65 76 65 6e 20 69 66 0a 2a 2a 20 74 68 65  0 even if.** the
1d1c9 20 6f 74 68 65 72 20 69 6e 70 75 74 20 69 73 20   other input is 
1d1ca 4e 55 4c 4c 2e 20 20 41 20 4e 55 4c 4c 20 61 6e  NULL.  A NULL an
1d1cb 64 20 74 72 75 65 20 6f 72 20 74 77 6f 20 4e 55  d true or two NU
1d1cc 4c 4c 73 20 67 69 76 65 0a 2a 2a 20 61 20 4e 55  LLs give.** a NU
1d1cd 4c 4c 20 6f 75 74 70 75 74 2e 0a 2a 2f 0a 2f 2a  LL output..*/./*
1d1ce 20 4f 70 63 6f 64 65 3a 20 4f 72 20 50 31 20 50   Opcode: Or P1 P
1d1cf 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54  2 P3 * *.**.** T
1d1d0 61 6b 65 20 74 68 65 20 6c 6f 67 69 63 61 6c 20  ake the logical 
1d1d1 4f 52 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73  OR of the values
1d1d2 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1d1d3 61 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74  and P2 and.** st
1d1d4 6f 72 65 20 74 68 65 20 61 6e 73 77 65 72 20 69  ore the answer i
1d1d5 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  n register P3..*
1d1d6 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50  *.** If either P
1d1d7 31 20 6f 72 20 50 32 20 69 73 20 6e 6f 6e 7a 65  1 or P2 is nonze
1d1d8 72 6f 20 28 74 72 75 65 29 20 74 68 65 6e 20 74  ro (true) then t
1d1d9 68 65 20 72 65 73 75 6c 74 20 69 73 20 31 20 28  he result is 1 (
1d1da 74 72 75 65 29 0a 2a 2a 20 65 76 65 6e 20 69 66  true).** even if
1d1db 20 74 68 65 20 6f 74 68 65 72 20 69 6e 70 75 74   the other input
1d1dc 20 69 73 20 4e 55 4c 4c 2e 20 20 41 20 4e 55 4c   is NULL.  A NUL
1d1dd 4c 20 61 6e 64 20 66 61 6c 73 65 20 6f 72 20 74  L and false or t
1d1de 77 6f 20 4e 55 4c 4c 73 0a 2a 2a 20 67 69 76 65  wo NULLs.** give
1d1df 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 74 2e 0a   a NULL output..
1d1e0 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 6e 64 3a 20  */.case OP_And: 
1d1e1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d1e2 73 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 2c 20  same as TK_AND, 
1d1e3 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a  in1, in2, out3 *
1d1e4 2f 0a 63 61 73 65 20 4f 50 5f 4f 72 3a 20 7b 20  /.case OP_Or: { 
1d1e5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
1d1e6 61 6d 65 20 61 73 20 54 4b 5f 4f 52 2c 20 69 6e  ame as TK_OR, in
1d1e7 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a  1, in2, out3 */.
1d1e8 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1d1e9 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1d1ea 69 6e 74 6f 20 75 2e 61 6b 20 2a 2f 0a 20 20 69  into u.ak */.  i
1d1eb 6e 74 20 76 31 3b 20 20 20 20 2f 2a 20 4c 65 66  nt v1;    /* Lef
1d1ec 74 20 6f 70 65 72 61 6e 64 3a 20 20 30 3d 3d 46  t operand:  0==F
1d1ed 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32  ALSE, 1==TRUE, 2
1d1ee 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c  ==UNKNOWN or NUL
1d1ef 4c 20 2a 2f 0a 20 20 69 6e 74 20 76 32 3b 20 20  L */.  int v2;  
1d1f0 20 20 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 61    /* Right opera
1d1f1 6e 64 3a 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d  nd: 0==FALSE, 1=
1d1f2 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57  =TRUE, 2==UNKNOW
1d1f3 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 23 65 6e  N or NULL */.#en
1d1f4 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1d1f5 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1d1f6 6f 20 75 2e 61 6b 20 2a 2f 0a 0a 20 20 69 66 28  o u.ak */..  if(
1d1f7 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d   pIn1->flags & M
1d1f8 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75  EM_Null ){.    u
1d1f9 2e 61 6b 2e 76 31 20 3d 20 32 3b 0a 20 20 7d 65  .ak.v1 = 2;.  }e
1d1fa 6c 73 65 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 31  lse{.    u.ak.v1
1d1fb 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e   = sqlite3VdbeIn
1d1fc 74 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 3b  tValue(pIn1)!=0;
1d1fd 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 32 2d  .  }.  if( pIn2-
1d1fe 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
1d1ff 6c 20 29 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 32  l ){.    u.ak.v2
1d200 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 2;.  }else{. 
1d201 20 20 20 75 2e 61 6b 2e 76 32 20 3d 20 73 71 6c     u.ak.v2 = sql
1d202 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65  ite3VdbeIntValue
1d203 28 70 49 6e 32 29 21 3d 30 3b 0a 20 20 7d 0a 20  (pIn2)!=0;.  }. 
1d204 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
1d205 3d 3d 4f 50 5f 41 6e 64 20 29 7b 0a 20 20 20 20  ==OP_And ){.    
1d206 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
1d207 69 67 6e 65 64 20 63 68 61 72 20 61 6e 64 5f 6c  igned char and_l
1d208 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 30 2c  ogic[] = { 0, 0,
1d209 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20   0, 0, 1, 2, 0, 
1d20a 32 2c 20 32 20 7d 3b 0a 20 20 20 20 75 2e 61 6b  2, 2 };.    u.ak
1d20b 2e 76 31 20 3d 20 61 6e 64 5f 6c 6f 67 69 63 5b  .v1 = and_logic[
1d20c 75 2e 61 6b 2e 76 31 2a 33 2b 75 2e 61 6b 2e 76  u.ak.v1*3+u.ak.v
1d20d 32 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  2];.  }else{.   
1d20e 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
1d20f 73 69 67 6e 65 64 20 63 68 61 72 20 6f 72 5f 6c  signed char or_l
1d210 6f 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 31 2c  ogic[] = { 0, 1,
1d211 20 32 2c 20 31 2c 20 31 2c 20 31 2c 20 32 2c 20   2, 1, 1, 1, 2, 
1d212 31 2c 20 32 20 7d 3b 0a 20 20 20 20 75 2e 61 6b  1, 2 };.    u.ak
1d213 2e 76 31 20 3d 20 6f 72 5f 6c 6f 67 69 63 5b 75  .v1 = or_logic[u
1d214 2e 61 6b 2e 76 31 2a 33 2b 75 2e 61 6b 2e 76 32  .ak.v1*3+u.ak.v2
1d215 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 75 2e 61  ];.  }.  if( u.a
1d216 6b 2e 76 31 3d 3d 32 20 29 7b 0a 20 20 20 20 4d  k.v1==2 ){.    M
1d217 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f  emSetTypeFlag(pO
1d218 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20  ut, MEM_Null);. 
1d219 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4f 75 74   }else{.    pOut
1d21a 2d 3e 75 2e 69 20 3d 20 75 2e 61 6b 2e 76 31 3b  ->u.i = u.ak.v1;
1d21b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46  .    MemSetTypeF
1d21c 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e  lag(pOut, MEM_In
1d21d 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  t);.  }.  break;
1d21e 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e  .}../* Opcode: N
1d21f 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ot P1 P2 * * *.*
1d220 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74  *.** Interpret t
1d221 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1d222 73 74 65 72 20 50 31 20 61 73 20 61 20 62 6f 6f  ster P1 as a boo
1d223 6c 65 61 6e 20 76 61 6c 75 65 2e 20 20 53 74 6f  lean value.  Sto
1d224 72 65 20 74 68 65 0a 2a 2a 20 62 6f 6f 6c 65 61  re the.** boolea
1d225 6e 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 69 6e 20  n complement in 
1d226 72 65 67 69 73 74 65 72 20 50 32 2e 20 20 49 66  register P2.  If
1d227 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1d228 67 69 73 74 65 72 20 50 31 20 69 73 20 0a 2a 2a  gister P1 is .**
1d229 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 20 4e 55   NULL, then a NU
1d22a 4c 4c 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  LL is stored in 
1d22b 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e  P2..*/.case OP_N
1d22c 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  ot: {           
1d22d 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1d22e 54 4b 5f 4e 4f 54 2c 20 69 6e 31 20 2a 2f 0a 20  TK_NOT, in1 */. 
1d22f 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d   pOut = &p->aMem
1d230 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 66 28  [pOp->p2];.  if(
1d231 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d   pIn1->flags & M
1d232 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73  EM_Null ){.    s
1d233 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
1d234 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 7d 65  Null(pOut);.  }e
1d235 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
1d236 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28  VdbeMemSetInt64(
1d237 70 4f 75 74 2c 20 21 73 71 6c 69 74 65 33 56 64  pOut, !sqlite3Vd
1d238 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29  beIntValue(pIn1)
1d239 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  );.  }.  break;.
1d23a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69  }../* Opcode: Bi
1d23b 74 4e 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 2a  tNot P1 P2 * * *
1d23c 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74  .**.** Interpret
1d23d 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
1d23e 72 65 67 69 73 74 65 72 20 50 31 20 61 73 20 61  register P1 as a
1d23f 6e 20 69 6e 74 65 67 65 72 2e 20 20 53 74 6f 72  n integer.  Stor
1d240 65 20 74 68 65 0a 2a 2a 20 6f 6e 65 73 2d 63 6f  e the.** ones-co
1d241 6d 70 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20  mplement of the 
1d242 50 31 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65  P1 value into re
1d243 67 69 73 74 65 72 20 50 32 2e 20 20 49 66 20 50  gister P2.  If P
1d244 31 20 68 6f 6c 64 73 0a 2a 2a 20 61 20 4e 55 4c  1 holds.** a NUL
1d245 4c 20 74 68 65 6e 20 73 74 6f 72 65 20 61 20 4e  L then store a N
1d246 55 4c 4c 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61  ULL in P2..*/.ca
1d247 73 65 20 4f 50 5f 42 69 74 4e 6f 74 3a 20 7b 20  se OP_BitNot: { 
1d248 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
1d249 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54  ame as TK_BITNOT
1d24a 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 4f 75 74 20  , in1 */.  pOut 
1d24b 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
1d24c 70 32 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d  p2];.  if( pIn1-
1d24d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
1d24e 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  l ){.    sqlite3
1d24f 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70  VdbeMemSetNull(p
1d250 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Out);.  }else{. 
1d251 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1d252 6d 53 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20  mSetInt64(pOut, 
1d253 7e 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56  ~sqlite3VdbeIntV
1d254 61 6c 75 65 28 70 49 6e 31 29 29 3b 0a 20 20 7d  alue(pIn1));.  }
1d255 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1d256 4f 70 63 6f 64 65 3a 20 49 66 20 50 31 20 50 32  Opcode: If P1 P2
1d257 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75   P3 * *.**.** Ju
1d258 6d 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20  mp to P2 if the 
1d259 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
1d25a 72 20 50 31 20 69 73 20 74 72 75 65 2e 20 20 54  r P1 is true.  T
1d25b 68 65 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 69  he value is.** i
1d25c 73 20 63 6f 6e 73 69 64 65 72 65 64 20 74 72 75  s considered tru
1d25d 65 20 69 66 20 69 74 20 69 73 20 6e 75 6d 65 72  e if it is numer
1d25e 69 63 20 61 6e 64 20 6e 6f 6e 2d 7a 65 72 6f 2e  ic and non-zero.
1d25f 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a    If the value.*
1d260 2a 20 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20  * in P1 is NULL 
1d261 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75  then take the ju
1d262 6d 70 20 69 66 20 50 33 20 69 73 20 74 72 75 65  mp if P3 is true
1d263 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1d264 49 66 4e 6f 74 20 50 31 20 50 32 20 50 33 20 2a  IfNot P1 P2 P3 *
1d265 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f   *.**.** Jump to
1d266 20 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65   P2 if the value
1d267 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1d268 69 73 20 46 61 6c 73 65 2e 20 20 54 68 65 20 76  is False.  The v
1d269 61 6c 75 65 20 69 73 0a 2a 2a 20 69 73 20 63 6f  alue is.** is co
1d26a 6e 73 69 64 65 72 65 64 20 74 72 75 65 20 69 66  nsidered true if
1d26b 20 69 74 20 68 61 73 20 61 20 6e 75 6d 65 72 69   it has a numeri
1d26c 63 20 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f 2e  c value of zero.
1d26d 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a    If the value.*
1d26e 2a 20 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20  * in P1 is NULL 
1d26f 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75  then take the ju
1d270 6d 70 20 69 66 20 50 33 20 69 73 20 74 72 75 65  mp if P3 is true
1d271 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 3a  ..*/.case OP_If:
1d272 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d273 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f   /* jump, in1 */
1d274 0a 63 61 73 65 20 4f 50 5f 49 66 4e 6f 74 3a 20  .case OP_IfNot: 
1d275 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {            /* 
1d276 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 23 69 66  jump, in1 */.#if
1d277 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1d278 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1d279 6f 20 75 2e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20  o u.al */.  int 
1d27a 63 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  c;.#endif /* loc
1d27b 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1d27c 65 64 20 69 6e 74 6f 20 75 2e 61 6c 20 2a 2f 0a  ed into u.al */.
1d27d 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67    if( pIn1->flag
1d27e 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a  s & MEM_Null ){.
1d27f 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20 70 4f 70      u.al.c = pOp
1d280 2d 3e 70 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23  ->p3;.  }else{.#
1d281 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
1d282 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
1d283 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20 73 71  .    u.al.c = sq
1d284 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
1d285 65 28 70 49 6e 31 29 21 3d 30 3b 0a 23 65 6c 73  e(pIn1)!=0;.#els
1d286 65 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20 73  e.    u.al.c = s
1d287 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61  qlite3VdbeRealVa
1d288 6c 75 65 28 70 49 6e 31 29 21 3d 30 2e 30 3b 0a  lue(pIn1)!=0.0;.
1d289 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 70  #endif.    if( p
1d28a 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49  Op->opcode==OP_I
1d28b 66 4e 6f 74 20 29 20 75 2e 61 6c 2e 63 20 3d 20  fNot ) u.al.c = 
1d28c 21 75 2e 61 6c 2e 63 3b 0a 20 20 7d 0a 20 20 69  !u.al.c;.  }.  i
1d28d 66 28 20 75 2e 61 6c 2e 63 20 29 7b 0a 20 20 20  f( u.al.c ){.   
1d28e 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b   pc = pOp->p2-1;
1d28f 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1d290 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73 4e 75  ./* Opcode: IsNu
1d291 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ll P1 P2 * * *.*
1d292 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20  *.** Jump to P2 
1d293 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  if the value in 
1d294 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20 4e  register P1 is N
1d295 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ULL..*/.case OP_
1d296 49 73 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20  IsNull: {       
1d297 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1d298 54 4b 5f 49 53 4e 55 4c 4c 2c 20 6a 75 6d 70 2c  TK_ISNULL, jump,
1d299 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70   in1 */.  if( (p
1d29a 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
1d29b 5f 4e 75 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20  _Null)!=0 ){.   
1d29c 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1d29d 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  1;.  }.  break;.
1d29e 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f  }../* Opcode: No
1d29f 74 4e 75 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20  tNull P1 P2 * * 
1d2a0 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20  *.**.** Jump to 
1d2a1 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20  P2 if the value 
1d2a2 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69  in register P1 i
1d2a3 73 20 6e 6f 74 20 4e 55 4c 4c 2e 20 20 0a 2a 2f  s not NULL.  .*/
1d2a4 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c  .case OP_NotNull
1d2a5 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f  : {            /
1d2a6 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54  * same as TK_NOT
1d2a7 4e 55 4c 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31 20  NULL, jump, in1 
1d2a8 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  */.  if( (pIn1->
1d2a9 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1d2aa 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d  )==0 ){.    pc =
1d2ab 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1d2ac 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1d2ad 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 75 6d 6e 20   Opcode: Column 
1d2ae 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a  P1 P2 P3 P4 P5.*
1d2af 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74  *.** Interpret t
1d2b0 68 65 20 64 61 74 61 20 74 68 61 74 20 63 75 72  he data that cur
1d2b1 73 6f 72 20 50 31 20 70 6f 69 6e 74 73 20 74 6f  sor P1 points to
1d2b2 20 61 73 20 61 20 73 74 72 75 63 74 75 72 65 20   as a structure 
1d2b3 62 75 69 6c 74 20 75 73 69 6e 67 0a 2a 2a 20 74  built using.** t
1d2b4 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e  he MakeRecord in
1d2b5 73 74 72 75 63 74 69 6f 6e 2e 20 20 28 53 65 65  struction.  (See
1d2b6 20 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 20   the MakeRecord 
1d2b7 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74  opcode for addit
1d2b8 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61  ional.** informa
1d2b9 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 66  tion about the f
1d2ba 6f 72 6d 61 74 20 6f 66 20 74 68 65 20 64 61 74  ormat of the dat
1d2bb 61 2e 29 20 20 45 78 74 72 61 63 74 20 74 68 65  a.)  Extract the
1d2bc 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 0a 2a 2a   P2-th column.**
1d2bd 20 66 72 6f 6d 20 74 68 69 73 20 72 65 63 6f 72   from this recor
1d2be 64 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65  d.  If there are
1d2bf 20 6c 65 73 73 20 74 68 61 74 20 28 50 32 2b 31   less that (P2+1
1d2c0 29 20 0a 2a 2a 20 76 61 6c 75 65 73 20 69 6e 20  ) .** values in 
1d2c1 74 68 65 20 72 65 63 6f 72 64 2c 20 65 78 74 72  the record, extr
1d2c2 61 63 74 20 61 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  act a NULL..**.*
1d2c3 2a 20 54 68 65 20 76 61 6c 75 65 20 65 78 74 72  * The value extr
1d2c4 61 63 74 65 64 20 69 73 20 73 74 6f 72 65 64 20  acted is stored 
1d2c5 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a  in register P3..
1d2c6 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f 6c  **.** If the col
1d2c7 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 66 65 77  umn contains few
1d2c8 65 72 20 74 68 61 6e 20 50 32 20 66 69 65 6c 64  er than P2 field
1d2c9 73 2c 20 74 68 65 6e 20 65 78 74 72 61 63 74 20  s, then extract 
1d2ca 61 20 4e 55 4c 4c 2e 20 20 4f 72 2c 0a 2a 2a 20  a NULL.  Or,.** 
1d2cb 69 66 20 74 68 65 20 50 34 20 61 72 67 75 6d 65  if the P4 argume
1d2cc 6e 74 20 69 73 20 61 20 50 34 5f 4d 45 4d 20 75  nt is a P4_MEM u
1d2cd 73 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  se the value of 
1d2ce 74 68 65 20 50 34 20 61 72 67 75 6d 65 6e 74 20  the P4 argument 
1d2cf 61 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74  as.** the result
1d2d0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f  ..**.** If the O
1d2d1 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 45  PFLAG_CLEARCACHE
1d2d2 20 62 69 74 20 69 73 20 73 65 74 20 6f 6e 20 50   bit is set on P
1d2d3 35 20 61 6e 64 20 50 31 20 69 73 20 61 20 70 73  5 and P1 is a ps
1d2d4 65 75 64 6f 2d 74 61 62 6c 65 20 63 75 72 73 6f  eudo-table curso
1d2d5 72 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 63  r,.** then the c
1d2d6 61 63 68 65 20 6f 66 20 74 68 65 20 63 75 72 73  ache of the curs
1d2d7 6f 72 20 69 73 20 72 65 73 65 74 20 70 72 69 6f  or is reset prio
1d2d8 72 20 74 6f 20 65 78 74 72 61 63 74 69 6e 67 20  r to extracting 
1d2d9 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54  the column..** T
1d2da 68 65 20 66 69 72 73 74 20 4f 50 5f 43 6f 6c 75  he first OP_Colu
1d2db 6d 6e 20 61 67 61 69 6e 73 74 20 61 20 70 73 65  mn against a pse
1d2dc 75 64 6f 2d 74 61 62 6c 65 20 61 66 74 65 72 20  udo-table after 
1d2dd 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
1d2de 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 72 65 67 69   content.** regi
1d2df 73 74 65 72 20 68 61 73 20 63 68 61 6e 67 65 64  ster has changed
1d2e0 20 73 68 6f 75 6c 64 20 68 61 76 65 20 74 68 69   should have thi
1d2e1 73 20 62 69 74 20 73 65 74 2e 0a 2a 2f 0a 63 61  s bit set..*/.ca
1d2e2 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 3a 20 7b 0a  se OP_Column: {.
1d2e3 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1d2e4 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1d2e5 69 6e 74 6f 20 75 2e 61 6d 20 2a 2f 0a 20 20 75  into u.am */.  u
1d2e6 33 32 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20  32 payloadSize; 
1d2e7 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
1d2e8 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f  ytes in the reco
1d2e9 72 64 20 2a 2f 0a 20 20 69 36 34 20 70 61 79 6c  rd */.  i64 payl
1d2ea 6f 61 64 53 69 7a 65 36 34 3b 20 2f 2a 20 4e 75  oadSize64; /* Nu
1d2eb 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
1d2ec 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20   the record */. 
1d2ed 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20 20   int p1;        
1d2ee 20 20 20 20 2f 2a 20 50 31 20 76 61 6c 75 65 20      /* P1 value 
1d2ef 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20 2a 2f  of the opcode */
1d2f0 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20 20  .  int p2;      
1d2f1 20 20 20 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20        /* column 
1d2f2 6e 75 6d 62 65 72 20 74 6f 20 72 65 74 72 69 65  number to retrie
1d2f3 76 65 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73  ve */.  VdbeCurs
1d2f4 6f 72 20 2a 70 43 3b 20 20 20 20 2f 2a 20 54 68  or *pC;    /* Th
1d2f5 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 2a 2f  e VDBE cursor */
1d2f6 0a 20 20 63 68 61 72 20 2a 7a 52 65 63 3b 20 20  .  char *zRec;  
1d2f7 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
1d2f8 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 72 65 63   to complete rec
1d2f9 6f 72 64 2d 64 61 74 61 20 2a 2f 0a 20 20 42 74  ord-data */.  Bt
1d2fa 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 20 20  Cursor *pCrsr;  
1d2fb 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20 63 75   /* The BTree cu
1d2fc 72 73 6f 72 20 2a 2f 0a 20 20 75 33 32 20 2a 61  rsor */.  u32 *a
1d2fd 54 79 70 65 3b 20 20 20 20 20 20 20 20 2f 2a 20  Type;        /* 
1d2fe 61 54 79 70 65 5b 69 5d 20 68 6f 6c 64 73 20 74  aType[i] holds t
1d2ff 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70 65 20  he numeric type 
1d300 6f 66 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75  of the i-th colu
1d301 6d 6e 20 2a 2f 0a 20 20 75 33 32 20 2a 61 4f 66  mn */.  u32 *aOf
1d302 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 61 4f  fset;      /* aO
1d303 66 66 73 65 74 5b 69 5d 20 69 73 20 6f 66 66 73  ffset[i] is offs
1d304 65 74 20 74 6f 20 73 74 61 72 74 20 6f 66 20 64  et to start of d
1d305 61 74 61 20 66 6f 72 20 69 2d 74 68 20 63 6f 6c  ata for i-th col
1d306 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69  umn */.  int nFi
1d307 65 6c 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e  eld;        /* n
1d308 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20  umber of fields 
1d309 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f  in the record */
1d30a 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20  .  int len;     
1d30b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 6e        /* The len
1d30c 67 74 68 20 6f 66 20 74 68 65 20 73 65 72 69 61  gth of the seria
1d30d 6c 69 7a 65 64 20 64 61 74 61 20 66 6f 72 20 74  lized data for t
1d30e 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69  he column */.  i
1d30f 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
1d310 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
1d311 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 61  r */.  char *zDa
1d312 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 72  ta;       /* Par
1d313 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20  t of the record 
1d314 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f  being decoded */
1d315 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 20 20  .  Mem *pDest;  
1d316 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74        /* Where t
1d317 6f 20 77 72 69 74 65 20 74 68 65 20 65 78 74 72  o write the extr
1d318 61 63 74 65 64 20 76 61 6c 75 65 20 2a 2f 0a 20  acted value */. 
1d319 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20 20 20 20   Mem sMem;      
1d31a 20 20 20 20 2f 2a 20 46 6f 72 20 73 74 6f 72 69      /* For stori
1d31b 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 62 65  ng the record be
1d31c 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f 0a 20  ing decoded */. 
1d31d 20 75 38 20 2a 7a 49 64 78 3b 20 20 20 20 20 20   u8 *zIdx;      
1d31e 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74      /* Index int
1d31f 6f 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 38  o header */.  u8
1d320 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20 20 20 20   *zEndHdr;      
1d321 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66   /* Pointer to f
1d322 69 72 73 74 20 62 79 74 65 20 61 66 74 65 72 20  irst byte after 
1d323 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  the header */.  
1d324 75 33 32 20 6f 66 66 73 65 74 3b 20 20 20 20 20  u32 offset;     
1d325 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74     /* Offset int
1d326 6f 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20  o the data */.  
1d327 75 36 34 20 6f 66 66 73 65 74 36 34 3b 20 20 20  u64 offset64;   
1d328 20 20 20 2f 2a 20 36 34 2d 62 69 74 20 6f 66 66     /* 64-bit off
1d329 73 65 74 2e 20 20 36 34 20 62 69 74 73 20 6e 65  set.  64 bits ne
1d32a 65 64 65 64 20 74 6f 20 63 61 74 63 68 20 6f 76  eded to catch ov
1d32b 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20  erflow */.  int 
1d32c 73 7a 48 64 72 3b 20 20 20 20 20 20 20 20 20 2f  szHdr;         /
1d32d 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 68 65  * Size of the he
1d32e 61 64 65 72 20 73 69 7a 65 20 66 69 65 6c 64 20  ader size field 
1d32f 61 74 20 73 74 61 72 74 20 6f 66 20 72 65 63 6f  at start of reco
1d330 72 64 20 2a 2f 0a 20 20 69 6e 74 20 61 76 61 69  rd */.  int avai
1d331 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  l;         /* Nu
1d332 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
1d333 20 61 76 61 69 6c 61 62 6c 65 20 64 61 74 61 20   available data 
1d334 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 67 3b 20  */.  Mem *pReg; 
1d335 20 20 20 20 20 20 20 20 2f 2a 20 50 73 65 75 64          /* Pseud
1d336 6f 54 61 62 6c 65 20 69 6e 70 75 74 20 72 65 67  oTable input reg
1d337 69 73 74 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20  ister */.#endif 
1d338 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1d339 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1d33a 61 6d 20 2a 2f 0a 0a 0a 20 20 75 2e 61 6d 2e 70  am */...  u.am.p
1d33b 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75  1 = pOp->p1;.  u
1d33c 2e 61 6d 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32  .am.p2 = pOp->p2
1d33d 3b 0a 20 20 75 2e 61 6d 2e 70 43 20 3d 20 30 3b  ;.  u.am.pC = 0;
1d33e 0a 20 20 6d 65 6d 73 65 74 28 26 75 2e 61 6d 2e  .  memset(&u.am.
1d33f 73 4d 65 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28  sMem, 0, sizeof(
1d340 75 2e 61 6d 2e 73 4d 65 6d 29 29 3b 0a 20 20 61  u.am.sMem));.  a
1d341 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 31 3c 70  ssert( u.am.p1<p
1d342 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61  ->nCursor );.  a
1d343 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30  ssert( pOp->p3>0
1d344 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e   && pOp->p3<=p->
1d345 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 61 6d 2e 70  nMem );.  u.am.p
1d346 44 65 73 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  Dest = &p->aMem[
1d347 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 4d 65 6d 53  pOp->p3];.  MemS
1d348 65 74 54 79 70 65 46 6c 61 67 28 75 2e 61 6d 2e  etTypeFlag(u.am.
1d349 70 44 65 73 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29  pDest, MEM_Null)
1d34a 3b 0a 20 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20  ;.  u.am.zRec = 
1d34b 30 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 62 6c  0;..  /* This bl
1d34c 6f 63 6b 20 73 65 74 73 20 74 68 65 20 76 61 72  ock sets the var
1d34d 69 61 62 6c 65 20 75 2e 61 6d 2e 70 61 79 6c 6f  iable u.am.paylo
1d34e 61 64 53 69 7a 65 20 74 6f 20 62 65 20 74 68 65  adSize to be the
1d34f 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
1d350 0a 20 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 74  .  ** bytes in t
1d351 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2a 0a  he record..  **.
1d352 20 20 2a 2a 20 75 2e 61 6d 2e 7a 52 65 63 20 69    ** u.am.zRec i
1d353 73 20 73 65 74 20 74 6f 20 62 65 20 74 68 65 20  s set to be the 
1d354 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66  complete text of
1d355 20 74 68 65 20 72 65 63 6f 72 64 20 69 66 20 69   the record if i
1d356 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a  t is available..
1d357 20 20 2a 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74    ** The complet
1d358 65 20 72 65 63 6f 72 64 20 74 65 78 74 20 69 73  e record text is
1d359 20 61 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c   always availabl
1d35a 65 20 66 6f 72 20 70 73 65 75 64 6f 2d 74 61 62  e for pseudo-tab
1d35b 6c 65 73 0a 20 20 2a 2a 20 49 66 20 74 68 65 20  les.  ** If the 
1d35c 72 65 63 6f 72 64 20 69 73 20 73 74 6f 72 65 64  record is stored
1d35d 20 69 6e 20 61 20 63 75 72 73 6f 72 2c 20 74 68   in a cursor, th
1d35e 65 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72  e complete recor
1d35f 64 20 74 65 78 74 0a 20 20 2a 2a 20 6d 69 67 68  d text.  ** migh
1d360 74 20 62 65 20 61 76 61 69 6c 61 62 6c 65 20 69  t be available i
1d361 6e 20 74 68 65 20 20 75 2e 61 6d 2e 70 43 2d 3e  n the  u.am.pC->
1d362 61 52 6f 77 20 63 61 63 68 65 2e 20 20 4f 72 20  aRow cache.  Or 
1d363 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 2e  it might not be.
1d364 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 64 61 74  .  ** If the dat
1d365 61 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c 65  a is unavailable
1d366 2c 20 20 75 2e 61 6d 2e 7a 52 65 63 20 69 73 20  ,  u.am.zRec is 
1d367 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 2a  set to NULL..  *
1d368 2a 0a 20 20 2a 2a 20 57 65 20 61 6c 73 6f 20 63  *.  ** We also c
1d369 6f 6d 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65  ompute the numbe
1d36a 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
1d36b 74 68 65 20 72 65 63 6f 72 64 2e 20 20 46 6f 72  the record.  For
1d36c 20 63 75 72 73 6f 72 73 2c 0a 20 20 2a 2a 20 74   cursors,.  ** t
1d36d 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
1d36e 75 6d 6e 73 20 69 73 20 73 74 6f 72 65 64 20 69  umns is stored i
1d36f 6e 20 74 68 65 20 56 64 62 65 43 75 72 73 6f 72  n the VdbeCursor
1d370 2e 6e 46 69 65 6c 64 20 65 6c 65 6d 65 6e 74 2e  .nField element.
1d371 0a 20 20 2a 2f 0a 20 20 75 2e 61 6d 2e 70 43 20  .  */.  u.am.pC 
1d372 3d 20 70 2d 3e 61 70 43 73 72 5b 75 2e 61 6d 2e  = p->apCsr[u.am.
1d373 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
1d374 2e 61 6d 2e 70 43 21 3d 30 20 29 3b 0a 23 69 66  .am.pC!=0 );.#if
1d375 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1d376 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
1d377 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 43 2d  assert( u.am.pC-
1d378 3e 70 56 74 61 62 43 75 72 73 6f 72 3d 3d 30 20  >pVtabCursor==0 
1d379 29 3b 0a 23 65 6e 64 69 66 0a 20 20 75 2e 61 6d  );.#endif.  u.am
1d37a 2e 70 43 72 73 72 20 3d 20 75 2e 61 6d 2e 70 43  .pCrsr = u.am.pC
1d37b 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28  ->pCursor;.  if(
1d37c 20 75 2e 61 6d 2e 70 43 72 73 72 21 3d 30 20 29   u.am.pCrsr!=0 )
1d37d 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63  {.    /* The rec
1d37e 6f 72 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ord is stored in
1d37f 20 61 20 42 2d 54 72 65 65 20 2a 2f 0a 20 20 20   a B-Tree */.   
1d380 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1d381 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e  eCursorMoveto(u.
1d382 61 6d 2e 70 43 29 3b 0a 20 20 20 20 69 66 28 20  am.pC);.    if( 
1d383 72 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  rc ) goto abort_
1d384 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1d385 20 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 6e    if( u.am.pC->n
1d386 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20  ullRow ){.      
1d387 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d388 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20   = 0;.    }else 
1d389 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 63 61 63  if( u.am.pC->cac
1d38a 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e 63 61 63  heStatus==p->cac
1d38b 68 65 43 74 72 20 29 7b 0a 20 20 20 20 20 20 75  heCtr ){.      u
1d38c 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20  .am.payloadSize 
1d38d 3d 20 75 2e 61 6d 2e 70 43 2d 3e 70 61 79 6c 6f  = u.am.pC->paylo
1d38e 61 64 53 69 7a 65 3b 0a 20 20 20 20 20 20 75 2e  adSize;.      u.
1d38f 61 6d 2e 7a 52 65 63 20 3d 20 28 63 68 61 72 2a  am.zRec = (char*
1d390 29 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f 77 3b 0a  )u.am.pC->aRow;.
1d391 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e      }else if( u.
1d392 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65 78 20 29  am.pC->isIndex )
1d393 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1d394 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
1d395 6f 72 49 73 56 61 6c 69 64 28 75 2e 61 6d 2e 70  orIsValid(u.am.p
1d396 43 72 73 72 29 20 29 3b 0a 20 20 20 20 20 20 72  Crsr) );.      r
1d397 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1d398 4b 65 79 53 69 7a 65 28 75 2e 61 6d 2e 70 43 72  KeySize(u.am.pCr
1d399 73 72 2c 20 26 75 2e 61 6d 2e 70 61 79 6c 6f 61  sr, &u.am.payloa
1d39a 64 53 69 7a 65 36 34 29 3b 0a 20 20 20 20 20 20  dSize64);.      
1d39b 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
1d39c 54 45 5f 4f 4b 20 29 3b 20 20 20 2f 2a 20 54 72  TE_OK );   /* Tr
1d39d 75 65 20 62 65 63 61 75 73 65 20 6f 66 20 43 75  ue because of Cu
1d39e 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20 63 61 6c  rsorMoveto() cal
1d39f 6c 20 61 62 6f 76 65 20 2a 2f 0a 20 20 20 20 20  l above */.     
1d3a0 20 2f 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65   /* sqlite3Btree
1d3a1 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20 75  ParseCellPtr() u
1d3a2 73 65 73 20 67 65 74 56 61 72 69 6e 74 33 32 28  ses getVarint32(
1d3a3 29 20 74 6f 20 65 78 74 72 61 63 74 20 74 68 65  ) to extract the
1d3a4 0a 20 20 20 20 20 20 2a 2a 20 70 61 79 6c 6f 61  .      ** payloa
1d3a5 64 20 73 69 7a 65 2c 20 73 6f 20 69 74 20 69 73  d size, so it is
1d3a6 20 69 6d 70 6f 73 73 69 62 6c 65 20 66 6f 72 20   impossible for 
1d3a7 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d3a8 36 34 20 74 6f 20 62 65 0a 20 20 20 20 20 20 2a  64 to be.      *
1d3a9 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 33 32  * larger than 32
1d3aa 20 62 69 74 73 2e 20 2a 2f 0a 20 20 20 20 20 20   bits. */.      
1d3ab 61 73 73 65 72 74 28 20 28 75 2e 61 6d 2e 70 61  assert( (u.am.pa
1d3ac 79 6c 6f 61 64 53 69 7a 65 36 34 20 26 20 53 51  yloadSize64 & SQ
1d3ad 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29 3d 3d 28  LITE_MAX_U32)==(
1d3ae 75 36 34 29 75 2e 61 6d 2e 70 61 79 6c 6f 61 64  u64)u.am.payload
1d3af 53 69 7a 65 36 34 20 29 3b 0a 20 20 20 20 20 20  Size64 );.      
1d3b0 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d3b1 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e 70 61 79   = (u32)u.am.pay
1d3b2 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20 20 20 20  loadSize64;.    
1d3b3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
1d3b4 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1d3b5 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75  eCursorIsValid(u
1d3b6 2e 61 6d 2e 70 43 72 73 72 29 20 29 3b 0a 20 20  .am.pCrsr) );.  
1d3b7 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1d3b8 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 75 2e  BtreeDataSize(u.
1d3b9 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e 61 6d 2e  am.pCrsr, &u.am.
1d3ba 70 61 79 6c 6f 61 64 53 69 7a 65 29 3b 0a 20 20  payloadSize);.  
1d3bb 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
1d3bc 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f  SQLITE_OK );   /
1d3bd 2a 20 44 61 74 61 53 69 7a 65 28 29 20 63 61 6e  * DataSize() can
1d3be 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 20 20  not fail */.    
1d3bf 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e  }.  }else if( u.
1d3c0 61 6d 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62  am.pC->pseudoTab
1d3c1 6c 65 52 65 67 3e 30 20 29 7b 0a 20 20 20 20 75  leReg>0 ){.    u
1d3c2 2e 61 6d 2e 70 52 65 67 20 3d 20 26 70 2d 3e 61  .am.pReg = &p->a
1d3c3 4d 65 6d 5b 75 2e 61 6d 2e 70 43 2d 3e 70 73 65  Mem[u.am.pC->pse
1d3c4 75 64 6f 54 61 62 6c 65 52 65 67 5d 3b 0a 20 20  udoTableReg];.  
1d3c5 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70    assert( u.am.p
1d3c6 52 65 67 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Reg->flags & MEM
1d3c7 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20 75 2e 61  _Blob );.    u.a
1d3c8 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20  m.payloadSize = 
1d3c9 75 2e 61 6d 2e 70 52 65 67 2d 3e 6e 3b 0a 20 20  u.am.pReg->n;.  
1d3ca 20 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20 75 2e    u.am.zRec = u.
1d3cb 61 6d 2e 70 52 65 67 2d 3e 7a 3b 0a 20 20 20 20  am.pReg->z;.    
1d3cc 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.am.pC->cacheSt
1d3cd 61 74 75 73 20 3d 20 28 70 4f 70 2d 3e 70 35 26  atus = (pOp->p5&
1d3ce 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41 43 48  OPFLAG_CLEARCACH
1d3cf 45 29 20 3f 20 43 41 43 48 45 5f 53 54 41 4c 45  E) ? CACHE_STALE
1d3d0 20 3a 20 70 2d 3e 63 61 63 68 65 43 74 72 3b 0a   : p->cacheCtr;.
1d3d1 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d      assert( u.am
1d3d2 2e 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 20  .payloadSize==0 
1d3d3 7c 7c 20 75 2e 61 6d 2e 7a 52 65 63 21 3d 30 20  || u.am.zRec!=0 
1d3d4 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1d3d5 2f 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 65 20  /* Consider the 
1d3d6 72 6f 77 20 74 6f 20 62 65 20 4e 55 4c 4c 20 2a  row to be NULL *
1d3d7 2f 0a 20 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f  /.    u.am.paylo
1d3d8 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 0a  adSize = 0;.  }.
1d3d9 0a 20 20 2f 2a 20 49 66 20 75 2e 61 6d 2e 70 61  .  /* If u.am.pa
1d3da 79 6c 6f 61 64 53 69 7a 65 20 69 73 20 30 2c 20  yloadSize is 0, 
1d3db 74 68 65 6e 20 6a 75 73 74 20 73 74 6f 72 65 20  then just store 
1d3dc 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 66 28 20  a NULL */.  if( 
1d3dd 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d3de 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ==0 ){.    asser
1d3df 74 28 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66  t( u.am.pDest->f
1d3e0 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b  lags&MEM_Null );
1d3e1 0a 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c  .    goto op_col
1d3e2 75 6d 6e 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 61  umn_out;.  }.  a
1d3e3 73 73 65 72 74 28 20 64 62 2d 3e 61 4c 69 6d 69  ssert( db->aLimi
1d3e4 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
1d3e5 45 4e 47 54 48 5d 3e 3d 30 20 29 3b 0a 20 20 69  ENGTH]>=0 );.  i
1d3e6 66 28 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53  f( u.am.payloadS
1d3e7 69 7a 65 20 3e 20 28 75 33 32 29 64 62 2d 3e 61  ize > (u32)db->a
1d3e8 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
1d3e9 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20  IT_LENGTH] ){.  
1d3ea 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
1d3eb 20 20 7d 0a 0a 20 20 75 2e 61 6d 2e 6e 46 69 65    }..  u.am.nFie
1d3ec 6c 64 20 3d 20 75 2e 61 6d 2e 70 43 2d 3e 6e 46  ld = u.am.pC->nF
1d3ed 69 65 6c 64 3b 0a 20 20 61 73 73 65 72 74 28 20  ield;.  assert( 
1d3ee 75 2e 61 6d 2e 70 32 3c 75 2e 61 6d 2e 6e 46 69  u.am.p2<u.am.nFi
1d3ef 65 6c 64 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61  eld );..  /* Rea
1d3f0 64 20 61 6e 64 20 70 61 72 73 65 20 74 68 65 20  d and parse the 
1d3f1 74 61 62 6c 65 20 68 65 61 64 65 72 2e 20 20 53  table header.  S
1d3f2 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73  tore the results
1d3f3 20 6f 66 20 74 68 65 20 70 61 72 73 65 0a 20 20   of the parse.  
1d3f4 2a 2a 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f  ** into the reco
1d3f5 72 64 20 68 65 61 64 65 72 20 63 61 63 68 65 20  rd header cache 
1d3f6 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75  fields of the cu
1d3f7 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61  rsor..  */.  u.a
1d3f8 6d 2e 61 54 79 70 65 20 3d 20 75 2e 61 6d 2e 70  m.aType = u.am.p
1d3f9 43 2d 3e 61 54 79 70 65 3b 0a 20 20 69 66 28 20  C->aType;.  if( 
1d3fa 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.am.pC->cacheSt
1d3fb 61 74 75 73 3d 3d 70 2d 3e 63 61 63 68 65 43 74  atus==p->cacheCt
1d3fc 72 20 29 7b 0a 20 20 20 20 75 2e 61 6d 2e 61 4f  r ){.    u.am.aO
1d3fd 66 66 73 65 74 20 3d 20 75 2e 61 6d 2e 70 43 2d  ffset = u.am.pC-
1d3fe 3e 61 4f 66 66 73 65 74 3b 0a 20 20 7d 65 6c 73  >aOffset;.  }els
1d3ff 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 75 2e  e{.    assert(u.
1d400 61 6d 2e 61 54 79 70 65 29 3b 0a 20 20 20 20 75  am.aType);.    u
1d401 2e 61 6d 2e 61 76 61 69 6c 20 3d 20 30 3b 0a 20  .am.avail = 0;. 
1d402 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 4f 66 66     u.am.pC->aOff
1d403 73 65 74 20 3d 20 75 2e 61 6d 2e 61 4f 66 66 73  set = u.am.aOffs
1d404 65 74 20 3d 20 26 75 2e 61 6d 2e 61 54 79 70 65  et = &u.am.aType
1d405 5b 75 2e 61 6d 2e 6e 46 69 65 6c 64 5d 3b 0a 20  [u.am.nField];. 
1d406 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 70 61 79 6c     u.am.pC->payl
1d407 6f 61 64 53 69 7a 65 20 3d 20 75 2e 61 6d 2e 70  oadSize = u.am.p
1d408 61 79 6c 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20  ayloadSize;.    
1d409 75 2e 61 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.am.pC->cacheSt
1d40a 61 74 75 73 20 3d 20 70 2d 3e 63 61 63 68 65 43  atus = p->cacheC
1d40b 74 72 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75  tr;..    /* Figu
1d40c 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20  re out how many 
1d40d 62 79 74 65 73 20 61 72 65 20 69 6e 20 74 68 65  bytes are in the
1d40e 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 69   header */.    i
1d40f 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a  f( u.am.zRec ){.
1d410 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61        u.am.zData
1d411 20 3d 20 75 2e 61 6d 2e 7a 52 65 63 3b 0a 20 20   = u.am.zRec;.  
1d412 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
1d413 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e  f( u.am.pC->isIn
1d414 64 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 75  dex ){.        u
1d415 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 28 63 68 61  .am.zData = (cha
1d416 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 4b  r*)sqlite3BtreeK
1d417 65 79 46 65 74 63 68 28 75 2e 61 6d 2e 70 43 72  eyFetch(u.am.pCr
1d418 73 72 2c 20 26 75 2e 61 6d 2e 61 76 61 69 6c 29  sr, &u.am.avail)
1d419 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1d41a 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74         u.am.zDat
1d41b 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  a = (char*)sqlit
1d41c 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68  e3BtreeDataFetch
1d41d 28 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e  (u.am.pCrsr, &u.
1d41e 61 6d 2e 61 76 61 69 6c 29 3b 0a 20 20 20 20 20  am.avail);.     
1d41f 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 4b   }.      /* If K
1d420 65 79 46 65 74 63 68 28 29 2f 44 61 74 61 46 65  eyFetch()/DataFe
1d421 74 63 68 28 29 20 6d 61 6e 61 67 65 64 20 74 6f  tch() managed to
1d422 20 67 65 74 20 74 68 65 20 65 6e 74 69 72 65 20   get the entire 
1d423 70 61 79 6c 6f 61 64 2c 0a 20 20 20 20 20 20 2a  payload,.      *
1d424 2a 20 73 61 76 65 20 74 68 65 20 70 61 79 6c 6f  * save the paylo
1d425 61 64 20 69 6e 20 74 68 65 20 75 2e 61 6d 2e 70  ad in the u.am.p
1d426 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20 20  C->aRow cache.  
1d427 54 68 61 74 20 77 69 6c 6c 20 73 61 76 65 20 75  That will save u
1d428 73 20 66 72 6f 6d 0a 20 20 20 20 20 20 2a 2a 20  s from.      ** 
1d429 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b 65 20 61  having to make a
1d42a 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c 6c 73 20  dditional calls 
1d42b 74 6f 20 66 65 74 63 68 20 74 68 65 20 63 6f 6e  to fetch the con
1d42c 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20 6f 66 0a  tent portion of.
1d42d 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65 63        ** the rec
1d42e 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ord..      */.  
1d42f 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d      assert( u.am
1d430 2e 61 76 61 69 6c 3e 3d 30 20 29 3b 0a 20 20 20  .avail>=0 );.   
1d431 20 20 20 69 66 28 20 75 2e 61 6d 2e 70 61 79 6c     if( u.am.payl
1d432 6f 61 64 53 69 7a 65 20 3c 3d 20 28 75 33 32 29  oadSize <= (u32)
1d433 75 2e 61 6d 2e 61 76 61 69 6c 20 29 7b 0a 20 20  u.am.avail ){.  
1d434 20 20 20 20 20 20 75 2e 61 6d 2e 7a 52 65 63 20        u.am.zRec 
1d435 3d 20 75 2e 61 6d 2e 7a 44 61 74 61 3b 0a 20 20  = u.am.zData;.  
1d436 20 20 20 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61        u.am.pC->a
1d437 52 6f 77 20 3d 20 28 75 38 2a 29 75 2e 61 6d 2e  Row = (u8*)u.am.
1d438 7a 44 61 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c  zData;.      }el
1d439 73 65 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d  se{.        u.am
1d43a 2e 70 43 2d 3e 61 52 6f 77 20 3d 20 30 3b 0a 20  .pC->aRow = 0;. 
1d43b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1d43c 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
1d43d 67 20 61 73 73 65 72 74 20 69 73 20 74 72 75 65  g assert is true
1d43e 20 69 6e 20 61 6c 6c 20 63 61 73 65 73 20 61 63   in all cases ac
1d43f 63 65 70 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a  cept when.    **
1d440 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1d441 6c 65 20 68 61 73 20 62 65 65 6e 20 63 6f 72 72  le has been corr
1d442 75 70 74 65 64 20 65 78 74 65 72 6e 61 6c 6c 79  upted externally
1d443 2e 0a 20 20 20 20 2a 2a 20 20 20 20 61 73 73 65  ..    **    asse
1d444 72 74 28 20 75 2e 61 6d 2e 7a 52 65 63 21 3d 30  rt( u.am.zRec!=0
1d445 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d   || u.am.avail>=
1d446 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d447 20 7c 7c 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d   || u.am.avail>=
1d448 39 20 29 3b 20 2a 2f 0a 20 20 20 20 75 2e 61 6d  9 ); */.    u.am
1d449 2e 73 7a 48 64 72 20 3d 20 67 65 74 56 61 72 69  .szHdr = getVari
1d44a 6e 74 33 32 28 28 75 38 2a 29 75 2e 61 6d 2e 7a  nt32((u8*)u.am.z
1d44b 44 61 74 61 2c 20 75 2e 61 6d 2e 6f 66 66 73 65  Data, u.am.offse
1d44c 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65  t);..    /* Make
1d44d 20 73 75 72 65 20 61 20 63 6f 72 72 75 70 74 20   sure a corrupt 
1d44e 64 61 74 61 62 61 73 65 20 68 61 73 20 6e 6f 74  database has not
1d44f 20 67 69 76 65 6e 20 75 73 20 61 6e 20 6f 76 65   given us an ove
1d450 72 73 69 7a 65 20 68 65 61 64 65 72 2e 0a 20 20  rsize header..  
1d451 20 20 2a 2a 20 44 6f 20 74 68 69 73 20 6e 6f 77    ** Do this now
1d452 20 74 6f 20 61 76 6f 69 64 20 61 6e 20 6f 76 65   to avoid an ove
1d453 72 73 69 7a 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  rsize memory all
1d454 6f 63 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 0a  ocation..    **.
1d455 20 20 20 20 2a 2a 20 54 79 70 65 20 65 6e 74 72      ** Type entr
1d456 69 65 73 20 63 61 6e 20 62 65 20 62 65 74 77 65  ies can be betwe
1d457 65 6e 20 31 20 61 6e 64 20 35 20 62 79 74 65 73  en 1 and 5 bytes
1d458 20 65 61 63 68 2e 20 20 42 75 74 20 34 20 61 6e   each.  But 4 an
1d459 64 20 35 20 62 79 74 65 0a 20 20 20 20 2a 2a 20  d 5 byte.    ** 
1d45a 74 79 70 65 73 20 75 73 65 20 73 6f 20 6d 75 63  types use so muc
1d45b 68 20 64 61 74 61 20 73 70 61 63 65 20 74 68 61  h data space tha
1d45c 74 20 74 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79  t there can only
1d45d 20 62 65 20 34 30 39 36 20 61 6e 64 20 33 32 20   be 4096 and 32 
1d45e 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 6d 2c 20  of.    ** them, 
1d45f 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20 53  respectively.  S
1d460 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65  o the maximum he
1d461 61 64 65 72 20 6c 65 6e 67 74 68 20 72 65 73 75  ader length resu
1d462 6c 74 73 20 66 72 6f 6d 20 61 0a 20 20 20 20 2a  lts from a.    *
1d463 2a 20 33 2d 62 79 74 65 20 74 79 70 65 20 66 6f  * 3-byte type fo
1d464 72 20 65 61 63 68 20 6f 66 20 74 68 65 20 6d 61  r each of the ma
1d465 78 69 6d 75 6d 20 6f 66 20 33 32 37 36 38 20 63  ximum of 32768 c
1d466 6f 6c 75 6d 6e 73 20 70 6c 75 73 20 74 68 72 65  olumns plus thre
1d467 65 0a 20 20 20 20 2a 2a 20 65 78 74 72 61 20 62  e.    ** extra b
1d468 79 74 65 73 20 66 6f 72 20 74 68 65 20 68 65 61  ytes for the hea
1d469 64 65 72 20 6c 65 6e 67 74 68 20 69 74 73 65 6c  der length itsel
1d46a 66 2e 20 20 33 32 37 36 38 2a 33 20 2b 20 33 20  f.  32768*3 + 3 
1d46b 3d 20 39 38 33 30 37 2e 0a 20 20 20 20 2a 2f 0a  = 98307..    */.
1d46c 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 6f 66 66      if( u.am.off
1d46d 73 65 74 20 3e 20 39 38 33 30 37 20 29 7b 0a 20  set > 98307 ){. 
1d46e 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1d46f 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
1d470 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c       goto op_col
1d471 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a  umn_out;.    }..
1d472 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 69      /* Compute i
1d473 6e 20 75 2e 61 6d 2e 6c 65 6e 20 74 68 65 20 6e  n u.am.len the n
1d474 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
1d475 66 20 64 61 74 61 20 77 65 20 6e 65 65 64 20 74  f data we need t
1d476 6f 20 72 65 61 64 20 69 6e 20 6f 72 64 65 72 0a  o read in order.
1d477 20 20 20 20 2a 2a 20 74 6f 20 67 65 74 20 75 2e      ** to get u.
1d478 61 6d 2e 6e 46 69 65 6c 64 20 74 79 70 65 20 76  am.nField type v
1d479 61 6c 75 65 73 2e 20 20 75 2e 61 6d 2e 6f 66 66  alues.  u.am.off
1d47a 73 65 74 20 69 73 20 61 6e 20 75 70 70 65 72 20  set is an upper 
1d47b 62 6f 75 6e 64 20 6f 6e 20 74 68 69 73 2e 20 20  bound on this.  
1d47c 42 75 74 0a 20 20 20 20 2a 2a 20 75 2e 61 6d 2e  But.    ** u.am.
1d47d 6e 46 69 65 6c 64 20 6d 69 67 68 74 20 62 65 20  nField might be 
1d47e 73 69 67 6e 69 66 69 63 61 6e 74 6c 79 20 6c 65  significantly le
1d47f 73 73 20 74 68 61 6e 20 74 68 65 20 74 72 75 65  ss than the true
1d480 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
1d481 6e 73 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65  ns.    ** in the
1d482 20 74 61 62 6c 65 2c 20 61 6e 64 20 69 6e 20 74   table, and in t
1d483 68 61 74 20 63 61 73 65 2c 20 35 2a 75 2e 61 6d  hat case, 5*u.am
1d484 2e 6e 46 69 65 6c 64 2b 33 20 6d 69 67 68 74 20  .nField+3 might 
1d485 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  be smaller than 
1d486 75 2e 61 6d 2e 6f 66 66 73 65 74 2e 0a 20 20 20  u.am.offset..   
1d487 20 2a 2a 20 57 65 20 77 61 6e 74 20 74 6f 20 6d   ** We want to m
1d488 69 6e 69 6d 69 7a 65 20 75 2e 61 6d 2e 6c 65 6e  inimize u.am.len
1d489 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6c 69 6d   in order to lim
1d48a 69 74 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  it the size of t
1d48b 68 65 20 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a  he memory.    **
1d48c 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 65 73 70   allocation, esp
1d48d 65 63 69 61 6c 6c 79 20 69 66 20 61 20 63 6f 72  ecially if a cor
1d48e 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69  rupt database fi
1d48f 6c 65 20 68 61 73 20 63 61 75 73 65 64 20 75 2e  le has caused u.
1d490 61 6d 2e 6f 66 66 73 65 74 0a 20 20 20 20 2a 2a  am.offset.    **
1d491 20 74 6f 20 62 65 20 6f 76 65 72 73 69 7a 65 64   to be oversized
1d492 2e 20 4f 66 66 73 65 74 20 69 73 20 6c 69 6d 69  . Offset is limi
1d493 74 65 64 20 74 6f 20 39 38 33 30 37 20 61 62 6f  ted to 98307 abo
1d494 76 65 2e 20 20 42 75 74 20 39 38 33 30 37 20 6d  ve.  But 98307 m
1d495 69 67 68 74 0a 20 20 20 20 2a 2a 20 73 74 69 6c  ight.    ** stil
1d496 6c 20 65 78 63 65 65 64 20 52 6f 62 73 6f 6e 20  l exceed Robson 
1d497 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
1d498 6e 20 6c 69 6d 69 74 73 20 6f 6e 20 73 6f 6d 65  n limits on some
1d499 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 73 2e   configurations.
1d49a 0a 20 20 20 20 2a 2a 20 4f 6e 20 73 79 73 74 65  .    ** On syste
1d49b 6d 73 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 74  ms that cannot t
1d49c 6f 6c 65 72 61 74 65 20 6c 61 72 67 65 20 6d 65  olerate large me
1d49d 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  mory allocations
1d49e 2c 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35 2b  , u.am.nField*5+
1d49f 33 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 6c 69  3.    ** will li
1d4a0 6b 65 6c 79 20 62 65 20 6d 75 63 68 20 73 6d 61  kely be much sma
1d4a1 6c 6c 65 72 20 73 69 6e 63 65 20 75 2e 61 6d 2e  ller since u.am.
1d4a2 6e 46 69 65 6c 64 20 77 69 6c 6c 20 6c 69 6b 65  nField will like
1d4a3 6c 79 20 62 65 20 6c 65 73 73 20 74 68 61 6e 0a  ly be less than.
1d4a4 20 20 20 20 2a 2a 20 32 30 20 6f 72 20 73 6f 2e      ** 20 or so.
1d4a5 20 20 54 68 69 73 20 69 6e 73 75 72 65 73 20 74    This insures t
1d4a6 68 61 74 20 52 6f 62 73 6f 6e 20 6d 65 6d 6f 72  hat Robson memor
1d4a7 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 69 6d  y allocation lim
1d4a8 69 74 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e  its are.    ** n
1d4a9 6f 74 20 65 78 63 65 65 64 65 64 20 65 76 65 6e  ot exceeded even
1d4aa 20 66 6f 72 20 63 6f 72 72 75 70 74 20 64 61 74   for corrupt dat
1d4ab 61 62 61 73 65 20 66 69 6c 65 73 2e 0a 20 20 20  abase files..   
1d4ac 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 2e 6c 65 6e   */.    u.am.len
1d4ad 20 3d 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35   = u.am.nField*5
1d4ae 20 2b 20 33 3b 0a 20 20 20 20 69 66 28 20 75 2e   + 3;.    if( u.
1d4af 61 6d 2e 6c 65 6e 20 3e 20 28 69 6e 74 29 75 2e  am.len > (int)u.
1d4b0 61 6d 2e 6f 66 66 73 65 74 20 29 20 75 2e 61 6d  am.offset ) u.am
1d4b1 2e 6c 65 6e 20 3d 20 28 69 6e 74 29 75 2e 61 6d  .len = (int)u.am
1d4b2 2e 6f 66 66 73 65 74 3b 0a 0a 20 20 20 20 2f 2a  .offset;..    /*
1d4b3 20 54 68 65 20 4b 65 79 46 65 74 63 68 28 29 20   The KeyFetch() 
1d4b4 6f 72 20 44 61 74 61 46 65 74 63 68 28 29 20 61  or DataFetch() a
1d4b5 62 6f 76 65 20 61 72 65 20 66 61 73 74 20 61 6e  bove are fast an
1d4b6 64 20 77 69 6c 6c 20 67 65 74 20 74 68 65 20 65  d will get the e
1d4b7 6e 74 69 72 65 0a 20 20 20 20 2a 2a 20 72 65 63  ntire.    ** rec
1d4b8 6f 72 64 20 68 65 61 64 65 72 20 69 6e 20 6d 6f  ord header in mo
1d4b9 73 74 20 63 61 73 65 73 2e 20 20 42 75 74 20 74  st cases.  But t
1d4ba 68 65 79 20 77 69 6c 6c 20 66 61 69 6c 20 74 6f  hey will fail to
1d4bb 20 67 65 74 20 74 68 65 20 63 6f 6d 70 6c 65 74   get the complet
1d4bc 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20  e.    ** record 
1d4bd 68 65 61 64 65 72 20 69 66 20 74 68 65 20 72 65  header if the re
1d4be 63 6f 72 64 20 68 65 61 64 65 72 20 64 6f 65 73  cord header does
1d4bf 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 20 73 69   not fit on a si
1d4c0 6e 67 6c 65 20 70 61 67 65 0a 20 20 20 20 2a 2a  ngle page.    **
1d4c1 20 69 6e 20 74 68 65 20 42 2d 54 72 65 65 2e 20   in the B-Tree. 
1d4c2 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 70 65   When that happe
1d4c3 6e 73 2c 20 75 73 65 20 73 71 6c 69 74 65 33 56  ns, use sqlite3V
1d4c4 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28  dbeMemFromBtree(
1d4c5 29 20 74 6f 0a 20 20 20 20 2a 2a 20 61 63 71 75  ) to.    ** acqu
1d4c6 69 72 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65  ire the complete
1d4c7 20 68 65 61 64 65 72 20 74 65 78 74 2e 0a 20 20   header text..  
1d4c8 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 75 2e    */.    if( !u.
1d4c9 61 6d 2e 7a 52 65 63 20 26 26 20 75 2e 61 6d 2e  am.zRec && u.am.
1d4ca 61 76 61 69 6c 3c 75 2e 61 6d 2e 6c 65 6e 20 29  avail<u.am.len )
1d4cb 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 73 4d 65  {.      u.am.sMe
1d4cc 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20  m.flags = 0;.   
1d4cd 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e 64 62 20     u.am.sMem.db 
1d4ce 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = 0;.      rc = 
1d4cf 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72  sqlite3VdbeMemFr
1d4d0 6f 6d 42 74 72 65 65 28 75 2e 61 6d 2e 70 43 72  omBtree(u.am.pCr
1d4d1 73 72 2c 20 30 2c 20 75 2e 61 6d 2e 6c 65 6e 2c  sr, 0, u.am.len,
1d4d2 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65   u.am.pC->isInde
1d4d3 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d 29 3b 0a  x, &u.am.sMem);.
1d4d4 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1d4d5 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1d4d6 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d     goto op_colum
1d4d7 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20  n_out;.      }. 
1d4d8 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61 20       u.am.zData 
1d4d9 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20  = u.am.sMem.z;. 
1d4da 20 20 20 7d 0a 20 20 20 20 75 2e 61 6d 2e 7a 45     }.    u.am.zE
1d4db 6e 64 48 64 72 20 3d 20 28 75 38 20 2a 29 26 75  ndHdr = (u8 *)&u
1d4dc 2e 61 6d 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 6c  .am.zData[u.am.l
1d4dd 65 6e 5d 3b 0a 20 20 20 20 75 2e 61 6d 2e 7a 49  en];.    u.am.zI
1d4de 64 78 20 3d 20 28 75 38 20 2a 29 26 75 2e 61 6d  dx = (u8 *)&u.am
1d4df 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 73 7a 48 64  .zData[u.am.szHd
1d4e0 72 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e  r];..    /* Scan
1d4e1 20 74 68 65 20 68 65 61 64 65 72 20 61 6e 64 20   the header and 
1d4e2 75 73 65 20 69 74 20 74 6f 20 66 69 6c 6c 20 69  use it to fill i
1d4e3 6e 20 74 68 65 20 75 2e 61 6d 2e 61 54 79 70 65  n the u.am.aType
1d4e4 5b 5d 20 61 6e 64 20 75 2e 61 6d 2e 61 4f 66 66  [] and u.am.aOff
1d4e5 73 65 74 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72  set[].    ** arr
1d4e6 61 79 73 2e 20 20 75 2e 61 6d 2e 61 54 79 70 65  ays.  u.am.aType
1d4e7 5b 75 2e 61 6d 2e 69 5d 20 77 69 6c 6c 20 63 6f  [u.am.i] will co
1d4e8 6e 74 61 69 6e 20 74 68 65 20 74 79 70 65 20 69  ntain the type i
1d4e9 6e 74 65 67 65 72 20 66 6f 72 20 74 68 65 20 75  nteger for the u
1d4ea 2e 61 6d 2e 69 2d 74 68 0a 20 20 20 20 2a 2a 20  .am.i-th.    ** 
1d4eb 63 6f 6c 75 6d 6e 20 61 6e 64 20 75 2e 61 6d 2e  column and u.am.
1d4ec 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d 20  aOffset[u.am.i] 
1d4ed 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65  will contain the
1d4ee 20 75 2e 61 6d 2e 6f 66 66 73 65 74 20 66 72 6f   u.am.offset fro
1d4ef 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a  m the beginning.
1d4f0 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65      ** of the re
1d4f1 63 6f 72 64 20 74 6f 20 74 68 65 20 73 74 61 72  cord to the star
1d4f2 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f  t of the data fo
1d4f3 72 20 74 68 65 20 75 2e 61 6d 2e 69 2d 74 68 20  r the u.am.i-th 
1d4f4 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20  column.    */.  
1d4f5 20 20 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20    u.am.offset64 
1d4f6 3d 20 75 2e 61 6d 2e 6f 66 66 73 65 74 3b 0a 20  = u.am.offset;. 
1d4f7 20 20 20 66 6f 72 28 75 2e 61 6d 2e 69 3d 30 3b     for(u.am.i=0;
1d4f8 20 75 2e 61 6d 2e 69 3c 75 2e 61 6d 2e 6e 46 69   u.am.i<u.am.nFi
1d4f9 65 6c 64 3b 20 75 2e 61 6d 2e 69 2b 2b 29 7b 0a  eld; u.am.i++){.
1d4fa 20 20 20 20 20 20 69 66 28 20 75 2e 61 6d 2e 7a        if( u.am.z
1d4fb 49 64 78 3c 75 2e 61 6d 2e 7a 45 6e 64 48 64 72  Idx<u.am.zEndHdr
1d4fc 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d   ){.        u.am
1d4fd 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d  .aOffset[u.am.i]
1d4fe 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e 6f 66 66   = (u32)u.am.off
1d4ff 73 65 74 36 34 3b 0a 20 20 20 20 20 20 20 20 75  set64;.        u
1d500 2e 61 6d 2e 7a 49 64 78 20 2b 3d 20 67 65 74 56  .am.zIdx += getV
1d501 61 72 69 6e 74 33 32 28 75 2e 61 6d 2e 7a 49 64  arint32(u.am.zId
1d502 78 2c 20 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e  x, u.am.aType[u.
1d503 61 6d 2e 69 5d 29 3b 0a 20 20 20 20 20 20 20 20  am.i]);.        
1d504 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20 2b 3d  u.am.offset64 +=
1d505 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1d506 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6d 2e 61  alTypeLen(u.am.a
1d507 54 79 70 65 5b 75 2e 61 6d 2e 69 5d 29 3b 0a 20  Type[u.am.i]);. 
1d508 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1d509 20 20 20 20 2f 2a 20 49 66 20 75 2e 61 6d 2e 69      /* If u.am.i
1d50a 20 69 73 20 6c 65 73 73 20 74 68 61 74 20 75 2e   is less that u.
1d50b 61 6d 2e 6e 46 69 65 6c 64 2c 20 74 68 65 6e 20  am.nField, then 
1d50c 74 68 65 72 65 20 61 72 65 20 6c 65 73 73 20 66  there are less f
1d50d 69 65 6c 64 73 20 69 6e 20 74 68 69 73 0a 20 20  ields in this.  
1d50e 20 20 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 20        ** record 
1d50f 74 68 61 6e 20 53 65 74 4e 75 6d 43 6f 6c 75 6d  than SetNumColum
1d510 6e 73 20 69 6e 64 69 63 61 74 65 64 20 74 68 65  ns indicated the
1d511 72 65 20 61 72 65 20 63 6f 6c 75 6d 6e 73 20 69  re are columns i
1d512 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  n the.        **
1d513 20 74 61 62 6c 65 2e 20 53 65 74 20 74 68 65 20   table. Set the 
1d514 75 2e 61 6d 2e 6f 66 66 73 65 74 20 66 6f 72 20  u.am.offset for 
1d515 61 6e 79 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e  any extra column
1d516 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e  s not present in
1d517 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20  .        ** the 
1d518 72 65 63 6f 72 64 20 74 6f 20 30 2e 20 54 68 69  record to 0. Thi
1d519 73 20 74 65 6c 6c 73 20 63 6f 64 65 20 62 65 6c  s tells code bel
1d51a 6f 77 20 74 6f 20 73 74 6f 72 65 20 61 20 4e 55  ow to store a NU
1d51b 4c 4c 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  LL.        ** in
1d51c 73 74 65 61 64 20 6f 66 20 64 65 73 65 72 69 61  stead of deseria
1d51d 6c 69 7a 69 6e 67 20 61 20 76 61 6c 75 65 20 66  lizing a value f
1d51e 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 0a  rom the record..
1d51f 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1d520 20 20 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b     u.am.aOffset[
1d521 75 2e 61 6d 2e 69 5d 20 3d 20 30 3b 0a 20 20 20  u.am.i] = 0;.   
1d522 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
1d523 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1d524 65 61 73 65 28 26 75 2e 61 6d 2e 73 4d 65 6d 29  ease(&u.am.sMem)
1d525 3b 0a 20 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e  ;.    u.am.sMem.
1d526 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1d527 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20  ;..    /* If we 
1d528 68 61 76 65 20 72 65 61 64 20 6d 6f 72 65 20 68  have read more h
1d529 65 61 64 65 72 20 64 61 74 61 20 74 68 61 6e 20  eader data than 
1d52a 77 61 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  was contained in
1d52b 20 74 68 65 20 68 65 61 64 65 72 2c 0a 20 20 20   the header,.   
1d52c 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 65 6e   ** or if the en
1d52d 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69  d of the last fi
1d52e 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 62  eld appears to b
1d52f 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f  e past the end o
1d530 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 63  f the.    ** rec
1d531 6f 72 64 2c 20 6f 72 20 69 66 20 74 68 65 20 65  ord, or if the e
1d532 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66  nd of the last f
1d533 69 65 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20  ield appears to 
1d534 62 65 20 62 65 66 6f 72 65 20 74 68 65 20 65 6e  be before the en
1d535 64 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  d.    ** of the 
1d536 72 65 63 6f 72 64 20 28 77 68 65 6e 20 61 6c 6c  record (when all
1d537 20 66 69 65 6c 64 73 20 70 72 65 73 65 6e 74 29   fields present)
1d538 2c 20 74 68 65 6e 20 77 65 20 6d 75 73 74 20 62  , then we must b
1d539 65 20 64 65 61 6c 69 6e 67 0a 20 20 20 20 2a 2a  e dealing.    **
1d53a 20 77 69 74 68 20 61 20 63 6f 72 72 75 70 74 20   with a corrupt 
1d53b 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f  database..    */
1d53c 0a 20 20 20 20 69 66 28 20 28 75 2e 61 6d 2e 7a  .    if( (u.am.z
1d53d 49 64 78 20 3e 20 75 2e 61 6d 2e 7a 45 6e 64 48  Idx > u.am.zEndH
1d53e 64 72 29 7c 7c 20 28 75 2e 61 6d 2e 6f 66 66 73  dr)|| (u.am.offs
1d53f 65 74 36 34 20 3e 20 75 2e 61 6d 2e 70 61 79 6c  et64 > u.am.payl
1d540 6f 61 64 53 69 7a 65 29 0a 20 20 20 20 20 7c 7c  oadSize).     ||
1d541 20 28 75 2e 61 6d 2e 7a 49 64 78 3d 3d 75 2e 61   (u.am.zIdx==u.a
1d542 6d 2e 7a 45 6e 64 48 64 72 20 26 26 20 75 2e 61  m.zEndHdr && u.a
1d543 6d 2e 6f 66 66 73 65 74 36 34 21 3d 28 75 36 34  m.offset64!=(u64
1d544 29 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a  )u.am.payloadSiz
1d545 65 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  e) ){.      rc =
1d546 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1d547 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f  BKPT;.      goto
1d548 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a   op_column_out;.
1d549 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
1d54a 47 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69  Get the column i
1d54b 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 49 66 20 75  nformation. If u
1d54c 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d  .am.aOffset[u.am
1d54d 2e 70 32 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f  .p2] is non-zero
1d54e 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 64 65 73 65  , then.  ** dese
1d54f 72 69 61 6c 69 7a 65 20 74 68 65 20 76 61 6c 75  rialize the valu
1d550 65 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f 72  e from the recor
1d551 64 2e 20 49 66 20 75 2e 61 6d 2e 61 4f 66 66 73  d. If u.am.aOffs
1d552 65 74 5b 75 2e 61 6d 2e 70 32 5d 20 69 73 20 7a  et[u.am.p2] is z
1d553 65 72 6f 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74  ero,.  ** then t
1d554 68 65 72 65 20 61 72 65 20 6e 6f 74 20 65 6e 6f  here are not eno
1d555 75 67 68 20 66 69 65 6c 64 73 20 69 6e 20 74 68  ugh fields in th
1d556 65 20 72 65 63 6f 72 64 20 74 6f 20 73 61 74 69  e record to sati
1d557 73 66 79 20 74 68 65 0a 20 20 2a 2a 20 72 65 71  sfy the.  ** req
1d558 75 65 73 74 2e 20 20 49 6e 20 74 68 69 73 20 63  uest.  In this c
1d559 61 73 65 2c 20 73 65 74 20 74 68 65 20 76 61 6c  ase, set the val
1d55a 75 65 20 4e 55 4c 4c 20 6f 72 20 74 6f 20 50 34  ue NULL or to P4
1d55b 20 69 66 20 50 34 20 69 73 0a 20 20 2a 2a 20 61   if P4 is.  ** a
1d55c 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65   pointer to a Me
1d55d 6d 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2f 0a 20  m object..  */. 
1d55e 20 69 66 28 20 75 2e 61 6d 2e 61 4f 66 66 73 65   if( u.am.aOffse
1d55f 74 5b 75 2e 61 6d 2e 70 32 5d 20 29 7b 0a 20 20  t[u.am.p2] ){.  
1d560 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
1d561 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69  LITE_OK );.    i
1d562 66 28 20 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a  f( u.am.zRec ){.
1d563 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1d564 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72  eMemReleaseExter
1d565 6e 61 6c 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b  nal(u.am.pDest);
1d566 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1d567 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38 20  beSerialGet((u8 
1d568 2a 29 26 75 2e 61 6d 2e 7a 52 65 63 5b 75 2e 61  *)&u.am.zRec[u.a
1d569 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70  m.aOffset[u.am.p
1d56a 32 5d 5d 2c 20 75 2e 61 6d 2e 61 54 79 70 65 5b  2]], u.am.aType[
1d56b 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 70  u.am.p2], u.am.p
1d56c 44 65 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Dest);.    }else
1d56d 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 6c 65 6e  {.      u.am.len
1d56e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
1d56f 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6d  rialTypeLen(u.am
1d570 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32 5d 29  .aType[u.am.p2])
1d571 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1d572 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 61 6d  dbeMemMove(&u.am
1d573 2e 73 4d 65 6d 2c 20 75 2e 61 6d 2e 70 44 65 73  .sMem, u.am.pDes
1d574 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  t);.      rc = s
1d575 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f  qlite3VdbeMemFro
1d576 6d 42 74 72 65 65 28 75 2e 61 6d 2e 70 43 72 73  mBtree(u.am.pCrs
1d577 72 2c 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b  r, u.am.aOffset[
1d578 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 6c  u.am.p2], u.am.l
1d579 65 6e 2c 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49  en, u.am.pC->isI
1d57a 6e 64 65 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d  ndex, &u.am.sMem
1d57b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
1d57c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1d57d 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f        goto op_co
1d57e 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20  lumn_out;.      
1d57f 7d 0a 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61  }.      u.am.zDa
1d580 74 61 20 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a  ta = u.am.sMem.z
1d581 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1d582 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38  dbeSerialGet((u8
1d583 2a 29 75 2e 61 6d 2e 7a 44 61 74 61 2c 20 75 2e  *)u.am.zData, u.
1d584 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32  am.aType[u.am.p2
1d585 5d 2c 20 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a  ], u.am.pDest);.
1d586 20 20 20 20 7d 0a 20 20 20 20 75 2e 61 6d 2e 70      }.    u.am.p
1d587 44 65 73 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f  Dest->enc = enco
1d588 64 69 6e 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ding;.  }else{. 
1d589 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79     if( pOp->p4ty
1d58a 70 65 3d 3d 50 34 5f 4d 45 4d 20 29 7b 0a 20 20  pe==P4_MEM ){.  
1d58b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1d58c 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 75 2e  emShallowCopy(u.
1d58d 61 6d 2e 70 44 65 73 74 2c 20 70 4f 70 2d 3e 70  am.pDest, pOp->p
1d58e 34 2e 70 4d 65 6d 2c 20 4d 45 4d 5f 53 74 61 74  4.pMem, MEM_Stat
1d58f 69 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ic);.    }else{.
1d590 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e        assert( u.
1d591 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 26  am.pDest->flags&
1d592 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20  MEM_Null );.    
1d593 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77  }.  }..  /* If w
1d594 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  e dynamically al
1d595 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 74 6f  located space to
1d596 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20 28   hold the data (
1d597 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69  in the.  ** sqli
1d598 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74  te3VdbeMemFromBt
1d599 72 65 65 28 29 20 63 61 6c 6c 20 61 62 6f 76 65  ree() call above
1d59a 29 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20  ) then transfer 
1d59b 63 6f 6e 74 72 6f 6c 20 6f 66 20 74 68 61 74 0a  control of that.
1d59c 20 20 2a 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79    ** dynamically
1d59d 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65   allocated space
1d59e 20 6f 76 65 72 20 74 6f 20 74 68 65 20 75 2e 61   over to the u.a
1d59f 6d 2e 70 44 65 73 74 20 73 74 72 75 63 74 75 72  m.pDest structur
1d5a0 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 70 72 65  e..  ** This pre
1d5a1 76 65 6e 74 73 20 61 20 6d 65 6d 6f 72 79 20 63  vents a memory c
1d5a2 6f 70 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  opy..  */.  if( 
1d5a3 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f  u.am.sMem.zMallo
1d5a4 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  c ){.    assert(
1d5a5 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3d 3d 75 2e   u.am.sMem.z==u.
1d5a6 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20  am.sMem.zMalloc 
1d5a7 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  );.    assert( !
1d5a8 28 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61  (u.am.pDest->fla
1d5a9 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 20 29 3b  gs & MEM_Dyn) );
1d5aa 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 28 75  .    assert( !(u
1d5ab 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73  .am.pDest->flags
1d5ac 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d   & (MEM_Blob|MEM
1d5ad 5f 53 74 72 29 29 20 7c 7c 20 75 2e 61 6d 2e 70  _Str)) || u.am.p
1d5ae 44 65 73 74 2d 3e 7a 3d 3d 75 2e 61 6d 2e 73 4d  Dest->z==u.am.sM
1d5af 65 6d 2e 7a 20 29 3b 0a 20 20 20 20 75 2e 61 6d  em.z );.    u.am
1d5b0 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 3d  .pDest->flags &=
1d5b1 20 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d   ~(MEM_Ephem|MEM
1d5b2 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 75 2e  _Static);.    u.
1d5b3 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20  am.pDest->flags 
1d5b4 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20  |= MEM_Term;.   
1d5b5 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 20 3d   u.am.pDest->z =
1d5b6 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 20   u.am.sMem.z;.  
1d5b7 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 4d    u.am.pDest->zM
1d5b8 61 6c 6c 6f 63 20 3d 20 75 2e 61 6d 2e 73 4d 65  alloc = u.am.sMe
1d5b9 6d 2e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a  m.zMalloc;.  }..
1d5ba 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1d5bb 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62  beMemMakeWriteab
1d5bc 6c 65 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a  le(u.am.pDest);.
1d5bd 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3a 0a  .op_column_out:.
1d5be 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
1d5bf 42 53 49 5a 45 28 75 2e 61 6d 2e 70 44 65 73 74  BSIZE(u.am.pDest
1d5c0 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52  );.  REGISTER_TR
1d5c1 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 61  ACE(pOp->p3, u.a
1d5c2 6d 2e 70 44 65 73 74 29 3b 0a 20 20 62 72 65 61  m.pDest);.  brea
1d5c3 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1d5c4 20 41 66 66 69 6e 69 74 79 20 50 31 20 50 32 20   Affinity P1 P2 
1d5c5 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 41 70 70  * P4 *.**.** App
1d5c6 6c 79 20 61 66 66 69 6e 69 74 69 65 73 20 74 6f  ly affinities to
1d5c7 20 61 20 72 61 6e 67 65 20 6f 66 20 50 32 20 72   a range of P2 r
1d5c8 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e  egisters startin
1d5c9 67 20 77 69 74 68 20 50 31 2e 0a 2a 2a 0a 2a 2a  g with P1..**.**
1d5ca 20 50 34 20 69 73 20 61 20 73 74 72 69 6e 67 20   P4 is a string 
1d5cb 74 68 61 74 20 69 73 20 50 32 20 63 68 61 72 61  that is P2 chara
1d5cc 63 74 65 72 73 20 6c 6f 6e 67 2e 20 54 68 65 20  cters long. The 
1d5cd 6e 74 68 20 63 68 61 72 61 63 74 65 72 20 6f 66  nth character of
1d5ce 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69   the.** string i
1d5cf 6e 64 69 63 61 74 65 73 20 74 68 65 20 63 6f 6c  ndicates the col
1d5d0 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74 68 61  umn affinity tha
1d5d1 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64  t should be used
1d5d2 20 66 6f 72 20 74 68 65 20 6e 74 68 0a 2a 2a 20   for the nth.** 
1d5d3 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74  memory cell in t
1d5d4 68 65 20 72 61 6e 67 65 2e 0a 2a 2f 0a 63 61 73  he range..*/.cas
1d5d5 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 3a 20 7b  e OP_Affinity: {
1d5d6 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1d5d7 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1d5d8 20 69 6e 74 6f 20 75 2e 61 6e 20 2a 2f 0a 20 20   into u.an */.  
1d5d9 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b  char *zAffinity;
1d5da 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69     /* The affini
1d5db 74 79 20 74 6f 20 62 65 20 61 70 70 6c 69 65 64  ty to be applied
1d5dc 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61   */.  Mem *pData
1d5dd 30 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73  0;       /* Firs
1d5de 74 20 72 65 67 69 73 74 65 72 20 74 6f 20 77 68  t register to wh
1d5df 69 63 68 20 74 6f 20 61 70 70 6c 79 20 61 66 66  ich to apply aff
1d5e0 69 6e 69 74 79 20 2a 2f 0a 20 20 4d 65 6d 20 2a  inity */.  Mem *
1d5e1 70 4c 61 73 74 3b 20 20 20 20 20 20 20 20 2f 2a  pLast;        /*
1d5e2 20 4c 61 73 74 20 72 65 67 69 73 74 65 72 20 74   Last register t
1d5e3 6f 20 77 68 69 63 68 20 74 6f 20 61 70 70 6c 79  o which to apply
1d5e4 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a 20 20 4d   affinity */.  M
1d5e5 65 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 20 20  em *pRec;       
1d5e6 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 72 65 67    /* Current reg
1d5e7 69 73 74 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20  ister */.#endif 
1d5e8 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1d5e9 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1d5ea 61 6e 20 2a 2f 0a 0a 20 20 75 2e 61 6e 2e 7a 41  an */..  u.an.zA
1d5eb 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70  ffinity = pOp->p
1d5ec 34 2e 7a 3b 0a 20 20 75 2e 61 6e 2e 70 44 61 74  4.z;.  u.an.pDat
1d5ed 61 30 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  a0 = &p->aMem[pO
1d5ee 70 2d 3e 70 31 5d 3b 0a 20 20 75 2e 61 6e 2e 70  p->p1];.  u.an.p
1d5ef 4c 61 73 74 20 3d 20 26 75 2e 61 6e 2e 70 44 61  Last = &u.an.pDa
1d5f0 74 61 30 5b 70 4f 70 2d 3e 70 32 2d 31 5d 3b 0a  ta0[pOp->p2-1];.
1d5f1 20 20 66 6f 72 28 75 2e 61 6e 2e 70 52 65 63 3d    for(u.an.pRec=
1d5f2 75 2e 61 6e 2e 70 44 61 74 61 30 3b 20 75 2e 61  u.an.pData0; u.a
1d5f3 6e 2e 70 52 65 63 3c 3d 75 2e 61 6e 2e 70 4c 61  n.pRec<=u.an.pLa
1d5f4 73 74 3b 20 75 2e 61 6e 2e 70 52 65 63 2b 2b 29  st; u.an.pRec++)
1d5f5 7b 0a 20 20 20 20 45 78 70 61 6e 64 42 6c 6f 62  {.    ExpandBlob
1d5f6 28 75 2e 61 6e 2e 70 52 65 63 29 3b 0a 20 20 20  (u.an.pRec);.   
1d5f7 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 75   applyAffinity(u
1d5f8 2e 61 6e 2e 70 52 65 63 2c 20 75 2e 61 6e 2e 7a  .an.pRec, u.an.z
1d5f9 41 66 66 69 6e 69 74 79 5b 75 2e 61 6e 2e 70 52  Affinity[u.an.pR
1d5fa 65 63 2d 75 2e 61 6e 2e 70 44 61 74 61 30 5d 2c  ec-u.an.pData0],
1d5fb 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a   encoding);.  }.
1d5fc 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1d5fd 70 63 6f 64 65 3a 20 4d 61 6b 65 52 65 63 6f 72  pcode: MakeRecor
1d5fe 64 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a  d P1 P2 P3 P4 *.
1d5ff 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 50 32  **.** Convert P2
1d600 20 72 65 67 69 73 74 65 72 73 20 62 65 67 69 6e   registers begin
1d601 6e 69 6e 67 20 77 69 74 68 20 50 31 20 69 6e 74  ning with P1 int
1d602 6f 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79  o a single entry
1d603 0a 2a 2a 20 73 75 69 74 61 62 6c 65 20 66 6f 72  .** suitable for
1d604 20 75 73 65 20 61 73 20 61 20 64 61 74 61 20 72   use as a data r
1d605 65 63 6f 72 64 20 69 6e 20 61 20 64 61 74 61 62  ecord in a datab
1d606 61 73 65 20 74 61 62 6c 65 20 6f 72 20 61 73 20  ase table or as 
1d607 61 20 6b 65 79 0a 2a 2a 20 69 6e 20 61 6e 20 69  a key.** in an i
1d608 6e 64 65 78 2e 20 20 54 68 65 20 64 65 74 61 69  ndex.  The detai
1d609 6c 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74  ls of the format
1d60a 20 61 72 65 20 69 72 72 65 6c 65 76 61 6e 74 20   are irrelevant 
1d60b 61 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 74 68  as long as.** th
1d60c 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f  e OP_Column opco
1d60d 64 65 20 63 61 6e 20 64 65 63 6f 64 65 20 74 68  de can decode th
1d60e 65 20 72 65 63 6f 72 64 20 6c 61 74 65 72 2e 0a  e record later..
1d60f 2a 2a 20 52 65 66 65 72 20 74 6f 20 73 6f 75 72  ** Refer to sour
1d610 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74 73  ce code comments
1d611 20 66 6f 72 20 74 68 65 20 64 65 74 61 69 6c 73   for the details
1d612 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 0a 2a   of the record.*
1d613 2a 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20  * format..**.** 
1d614 50 34 20 6d 61 79 20 62 65 20 61 20 73 74 72 69  P4 may be a stri
1d615 6e 67 20 74 68 61 74 20 69 73 20 50 32 20 63 68  ng that is P2 ch
1d616 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20  aracters long.  
1d617 54 68 65 20 6e 74 68 20 63 68 61 72 61 63 74 65  The nth characte
1d618 72 20 6f 66 20 74 68 65 0a 2a 2a 20 73 74 72 69  r of the.** stri
1d619 6e 67 20 69 6e 64 69 63 61 74 65 73 20 74 68 65  ng indicates the
1d61a 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79   column affinity
1d61b 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
1d61c 75 73 65 64 20 66 6f 72 20 74 68 65 20 6e 74 68  used for the nth
1d61d 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65  .** field of the
1d61e 20 69 6e 64 65 78 20 6b 65 79 2e 0a 2a 2a 0a 2a   index key..**.*
1d61f 2a 20 54 68 65 20 6d 61 70 70 69 6e 67 20 66 72  * The mapping fr
1d620 6f 6d 20 63 68 61 72 61 63 74 65 72 20 74 6f 20  om character to 
1d621 61 66 66 69 6e 69 74 79 20 69 73 20 67 69 76 65  affinity is give
1d622 6e 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f  n by the SQLITE_
1d623 41 46 46 5f 0a 2a 2a 20 6d 61 63 72 6f 73 20 64  AFF_.** macros d
1d624 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65  efined in sqlite
1d625 49 6e 74 2e 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  Int.h..**.** If 
1d626 50 34 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20  P4 is NULL then 
1d627 61 6c 6c 20 69 6e 64 65 78 20 66 69 65 6c 64 73  all index fields
1d628 20 68 61 76 65 20 74 68 65 20 61 66 66 69 6e 69   have the affini
1d629 74 79 20 4e 4f 4e 45 2e 0a 2a 2f 0a 63 61 73 65  ty NONE..*/.case
1d62a 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 3a 20   OP_MakeRecord: 
1d62b 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
1d62c 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1d62d 64 20 69 6e 74 6f 20 75 2e 61 6f 20 2a 2f 0a 20  d into u.ao */. 
1d62e 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f 72 64 3b   u8 *zNewRecord;
1d62f 20 20 20 20 20 20 20 20 2f 2a 20 41 20 62 75 66          /* A buf
1d630 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  fer to hold the 
1d631 64 61 74 61 20 66 6f 72 20 74 68 65 20 6e 65 77  data for the new
1d632 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 4d 65 6d   record */.  Mem
1d633 20 2a 70 52 65 63 3b 20 20 20 20 20 20 20 20 20   *pRec;         
1d634 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72      /* The new r
1d635 65 63 6f 72 64 20 2a 2f 0a 20 20 75 36 34 20 6e  ecord */.  u64 n
1d636 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  Data;           
1d637 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
1d638 79 74 65 73 20 6f 66 20 64 61 74 61 20 73 70 61  ytes of data spa
1d639 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 64 72  ce */.  int nHdr
1d63a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1d63b 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
1d63c 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 61 63  s of header spac
1d63d 65 20 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74 65  e */.  i64 nByte
1d63e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1d63f 20 44 61 74 61 20 73 70 61 63 65 20 72 65 71 75   Data space requ
1d640 69 72 65 64 20 66 6f 72 20 74 68 69 73 20 72 65  ired for this re
1d641 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a  cord */.  int nZ
1d642 65 72 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  ero;            
1d643 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65   /* Number of ze
1d644 72 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 20  ro bytes at the 
1d645 65 6e 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72  end of the recor
1d646 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72 69  d */.  int nVari
1d647 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  nt;           /*
1d648 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
1d649 20 69 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f 0a   in a varint */.
1d64a 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70    u32 serial_typ
1d64b 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65  e;       /* Type
1d64c 20 66 69 65 6c 64 20 2a 2f 0a 20 20 4d 65 6d 20   field */.  Mem 
1d64d 2a 70 44 61 74 61 30 3b 20 20 20 20 20 20 20 20  *pData0;        
1d64e 20 20 20 2f 2a 20 46 69 72 73 74 20 66 69 65 6c     /* First fiel
1d64f 64 20 74 6f 20 62 65 20 63 6f 6d 62 69 6e 65 64  d to be combined
1d650 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f 72 64   into the record
1d651 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4c 61 73 74   */.  Mem *pLast
1d652 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1d653 4c 61 73 74 20 66 69 65 6c 64 20 6f 66 20 74 68  Last field of th
1d654 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e  e record */.  in
1d655 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20  t nField;       
1d656 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1d657 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20  f fields in the 
1d658 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 68 61 72  record */.  char
1d659 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 20 20   *zAffinity;    
1d65a 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69     /* The affini
1d65b 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68  ty string for th
1d65c 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e  e record */.  in
1d65d 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 20 20  t file_format;  
1d65e 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 66 6f 72       /* File for
1d65f 6d 61 74 20 74 6f 20 75 73 65 20 66 6f 72 20 65  mat to use for e
1d660 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74  ncoding */.  int
1d661 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
1d662 20 20 20 20 2f 2a 20 53 70 61 63 65 20 75 73 65      /* Space use
1d663 64 20 69 6e 20 7a 4e 65 77 52 65 63 6f 72 64 5b  d in zNewRecord[
1d664 5d 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b 20  ] */.  int len; 
1d665 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1d666 20 4c 65 6e 67 74 68 20 6f 66 20 61 20 66 69 65   Length of a fie
1d667 6c 64 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20  ld */.#endif /* 
1d668 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1d669 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6f 20  moved into u.ao 
1d66a 2a 2f 0a 0a 20 20 2f 2a 20 41 73 73 75 6d 69 6e  */..  /* Assumin
1d66b 67 20 74 68 65 20 72 65 63 6f 72 64 20 63 6f 6e  g the record con
1d66c 74 61 69 6e 73 20 4e 20 66 69 65 6c 64 73 2c 20  tains N fields, 
1d66d 74 68 65 20 72 65 63 6f 72 64 20 66 6f 72 6d 61  the record forma
1d66e 74 20 6c 6f 6f 6b 73 0a 20 20 2a 2a 20 6c 69 6b  t looks.  ** lik
1d66f 65 20 74 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a  e this:.  **.  *
1d670 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  * --------------
1d671 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d672 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d673 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d674 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20  ----------.  ** 
1d675 7c 20 68 64 72 2d 73 69 7a 65 20 7c 20 74 79 70  | hdr-size | typ
1d676 65 20 30 20 7c 20 74 79 70 65 20 31 20 7c 20 2e  e 0 | type 1 | .
1d677 2e 2e 20 7c 20 74 79 70 65 20 4e 2d 31 20 7c 20  .. | type N-1 | 
1d678 64 61 74 61 30 20 7c 20 2e 2e 2e 20 7c 20 64 61  data0 | ... | da
1d679 74 61 20 4e 2d 31 20 7c 0a 20 20 2a 2a 20 2d 2d  ta N-1 |.  ** --
1d67a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d67b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d67c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d67d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d67e 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 0a 20 20 2a 2a  ------.  **.  **
1d67f 20 44 61 74 61 28 30 29 20 69 73 20 74 61 6b 65   Data(0) is take
1d680 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20  n from register 
1d681 50 31 2e 20 20 44 61 74 61 28 31 29 20 63 6f 6d  P1.  Data(1) com
1d682 65 73 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72  es from register
1d683 20 50 31 2b 31 0a 20 20 2a 2a 20 61 6e 64 20 73   P1+1.  ** and s
1d684 6f 20 66 72 6f 74 68 2e 0a 20 20 2a 2a 0a 20 20  o froth..  **.  
1d685 2a 2a 20 45 61 63 68 20 74 79 70 65 20 66 69 65  ** Each type fie
1d686 6c 64 20 69 73 20 61 20 76 61 72 69 6e 74 20 72  ld is a varint r
1d687 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20  epresenting the 
1d688 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74  serial type of t
1d689 68 65 0a 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f  he.  ** correspo
1d68a 6e 64 69 6e 67 20 64 61 74 61 20 65 6c 65 6d 65  nding data eleme
1d68b 6e 74 20 28 73 65 65 20 73 71 6c 69 74 65 33 56  nt (see sqlite3V
1d68c 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 29 29  dbeSerialType())
1d68d 2e 20 54 68 65 0a 20 20 2a 2a 20 68 64 72 2d 73  . The.  ** hdr-s
1d68e 69 7a 65 20 66 69 65 6c 64 20 69 73 20 61 6c 73  ize field is als
1d68f 6f 20 61 20 76 61 72 69 6e 74 20 77 68 69 63 68  o a varint which
1d690 20 69 73 20 74 68 65 20 6f 66 66 73 65 74 20 66   is the offset f
1d691 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  rom the beginnin
1d692 67 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65  g.  ** of the re
1d693 63 6f 72 64 20 74 6f 20 64 61 74 61 30 2e 0a 20  cord to data0.. 
1d694 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 44 61 74 61   */.  u.ao.nData
1d695 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a   = 0;         /*
1d696 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
1d697 20 6f 66 20 64 61 74 61 20 73 70 61 63 65 20 2a   of data space *
1d698 2f 0a 20 20 75 2e 61 6f 2e 6e 48 64 72 20 3d 20  /.  u.ao.nHdr = 
1d699 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  0;          /* N
1d69a 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
1d69b 66 20 68 65 61 64 65 72 20 73 70 61 63 65 20 2a  f header space *
1d69c 2f 0a 20 20 75 2e 61 6f 2e 6e 42 79 74 65 20 3d  /.  u.ao.nByte =
1d69d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44   0;         /* D
1d69e 61 74 61 20 73 70 61 63 65 20 72 65 71 75 69 72  ata space requir
1d69f 65 64 20 66 6f 72 20 74 68 69 73 20 72 65 63 6f  ed for this reco
1d6a0 72 64 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 5a 65  rd */.  u.ao.nZe
1d6a1 72 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ro = 0;         
1d6a2 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72  /* Number of zer
1d6a3 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 20 65  o bytes at the e
1d6a4 6e 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64  nd of the record
1d6a5 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 46 69 65 6c   */.  u.ao.nFiel
1d6a6 64 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75  d = pOp->p1;.  u
1d6a7 2e 61 6f 2e 7a 41 66 66 69 6e 69 74 79 20 3d 20  .ao.zAffinity = 
1d6a8 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 61 73 73  pOp->p4.z;.  ass
1d6a9 65 72 74 28 20 75 2e 61 6f 2e 6e 46 69 65 6c 64  ert( u.ao.nField
1d6aa 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3e 30 20  >0 && pOp->p2>0 
1d6ab 26 26 20 70 4f 70 2d 3e 70 32 2b 75 2e 61 6f 2e  && pOp->p2+u.ao.
1d6ac 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e 4d 65 6d 2b  nField<=p->nMem+
1d6ad 31 20 29 3b 0a 20 20 75 2e 61 6f 2e 70 44 61 74  1 );.  u.ao.pDat
1d6ae 61 30 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e  a0 = &p->aMem[u.
1d6af 61 6f 2e 6e 46 69 65 6c 64 5d 3b 0a 20 20 75 2e  ao.nField];.  u.
1d6b0 61 6f 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d  ao.nField = pOp-
1d6b1 3e 70 32 3b 0a 20 20 75 2e 61 6f 2e 70 4c 61 73  >p2;.  u.ao.pLas
1d6b2 74 20 3d 20 26 75 2e 61 6f 2e 70 44 61 74 61 30  t = &u.ao.pData0
1d6b3 5b 75 2e 61 6f 2e 6e 46 69 65 6c 64 2d 31 5d 3b  [u.ao.nField-1];
1d6b4 0a 20 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f 72  .  u.ao.file_for
1d6b5 6d 61 74 20 3d 20 70 2d 3e 6d 69 6e 57 72 69 74  mat = p->minWrit
1d6b6 65 46 69 6c 65 46 6f 72 6d 61 74 3b 0a 0a 20 20  eFileFormat;..  
1d6b7 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20  /* Loop through 
1d6b8 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61  the elements tha
1d6b9 74 20 77 69 6c 6c 20 6d 61 6b 65 20 75 70 20 74  t will make up t
1d6ba 68 65 20 72 65 63 6f 72 64 20 74 6f 20 66 69 67  he record to fig
1d6bb 75 72 65 0a 20 20 2a 2a 20 6f 75 74 20 68 6f 77  ure.  ** out how
1d6bc 20 6d 75 63 68 20 73 70 61 63 65 20 69 73 20 72   much space is r
1d6bd 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 65 20  equired for the 
1d6be 6e 65 77 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2f  new record..  */
1d6bf 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70 52 65 63  .  for(u.ao.pRec
1d6c0 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b 20 75 2e  =u.ao.pData0; u.
1d6c1 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f 2e 70 4c  ao.pRec<=u.ao.pL
1d6c2 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65 63 2b 2b  ast; u.ao.pRec++
1d6c3 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 61 6f 2e  ){.    if( u.ao.
1d6c4 7a 41 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20  zAffinity ){.   
1d6c5 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79     applyAffinity
1d6c6 28 75 2e 61 6f 2e 70 52 65 63 2c 20 75 2e 61 6f  (u.ao.pRec, u.ao
1d6c7 2e 7a 41 66 66 69 6e 69 74 79 5b 75 2e 61 6f 2e  .zAffinity[u.ao.
1d6c8 70 52 65 63 2d 75 2e 61 6f 2e 70 44 61 74 61 30  pRec-u.ao.pData0
1d6c9 5d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20  ], encoding);.  
1d6ca 20 20 7d 0a 20 20 20 20 69 66 28 20 75 2e 61 6f    }.    if( u.ao
1d6cb 2e 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d  .pRec->flags&MEM
1d6cc 5f 5a 65 72 6f 20 26 26 20 75 2e 61 6f 2e 70 52  _Zero && u.ao.pR
1d6cd 65 63 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 20  ec->n>0 ){.     
1d6ce 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45   sqlite3VdbeMemE
1d6cf 78 70 61 6e 64 42 6c 6f 62 28 75 2e 61 6f 2e 70  xpandBlob(u.ao.p
1d6d0 52 65 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Rec);.    }.    
1d6d1 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65  u.ao.serial_type
1d6d2 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
1d6d3 72 69 61 6c 54 79 70 65 28 75 2e 61 6f 2e 70 52  rialType(u.ao.pR
1d6d4 65 63 2c 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f  ec, u.ao.file_fo
1d6d5 72 6d 61 74 29 3b 0a 20 20 20 20 75 2e 61 6f 2e  rmat);.    u.ao.
1d6d6 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 62  len = sqlite3Vdb
1d6d7 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75  eSerialTypeLen(u
1d6d8 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 29  .ao.serial_type)
1d6d9 3b 0a 20 20 20 20 75 2e 61 6f 2e 6e 44 61 74 61  ;.    u.ao.nData
1d6da 20 2b 3d 20 75 2e 61 6f 2e 6c 65 6e 3b 0a 20 20   += u.ao.len;.  
1d6db 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20 73    u.ao.nHdr += s
1d6dc 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28  qlite3VarintLen(
1d6dd 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65  u.ao.serial_type
1d6de 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 6f 2e  );.    if( u.ao.
1d6df 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pRec->flags & ME
1d6e0 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20  M_Zero ){.      
1d6e1 2f 2a 20 4f 6e 6c 79 20 70 75 72 65 20 7a 65 72  /* Only pure zer
1d6e2 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f 42 73 20 63  o-filled BLOBs c
1d6e3 61 6e 20 62 65 20 69 6e 70 75 74 20 74 6f 20 74  an be input to t
1d6e4 68 69 73 20 4f 70 63 6f 64 65 2e 0a 20 20 20 20  his Opcode..    
1d6e5 20 20 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 61    ** We do not a
1d6e6 6c 6c 6f 77 20 62 6c 6f 62 73 20 77 69 74 68 20  llow blobs with 
1d6e7 61 20 70 72 65 66 69 78 20 61 6e 64 20 61 20 7a  a prefix and a z
1d6e8 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2e  ero-filled tail.
1d6e9 20 2a 2f 0a 20 20 20 20 20 20 75 2e 61 6f 2e 6e   */.      u.ao.n
1d6ea 5a 65 72 6f 20 2b 3d 20 75 2e 61 6f 2e 70 52 65  Zero += u.ao.pRe
1d6eb 63 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20  c->u.nZero;.    
1d6ec 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 6f 2e 6c  }else if( u.ao.l
1d6ed 65 6e 20 29 7b 0a 20 20 20 20 20 20 75 2e 61 6f  en ){.      u.ao
1d6ee 2e 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 20 20  .nZero = 0;.    
1d6ef 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20  }.  }..  /* Add 
1d6f0 74 68 65 20 69 6e 69 74 69 61 6c 20 68 65 61 64  the initial head
1d6f1 65 72 20 76 61 72 69 6e 74 20 61 6e 64 20 74 6f  er varint and to
1d6f2 74 61 6c 20 74 68 65 20 73 69 7a 65 20 2a 2f 0a  tal the size */.
1d6f3 20 20 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20 75    u.ao.nHdr += u
1d6f4 2e 61 6f 2e 6e 56 61 72 69 6e 74 20 3d 20 73 71  .ao.nVarint = sq
1d6f5 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75  lite3VarintLen(u
1d6f6 2e 61 6f 2e 6e 48 64 72 29 3b 0a 20 20 69 66 28  .ao.nHdr);.  if(
1d6f7 20 75 2e 61 6f 2e 6e 56 61 72 69 6e 74 3c 73 71   u.ao.nVarint<sq
1d6f8 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75  lite3VarintLen(u
1d6f9 2e 61 6f 2e 6e 48 64 72 29 20 29 7b 0a 20 20 20  .ao.nHdr) ){.   
1d6fa 20 75 2e 61 6f 2e 6e 48 64 72 2b 2b 3b 0a 20 20   u.ao.nHdr++;.  
1d6fb 7d 0a 20 20 75 2e 61 6f 2e 6e 42 79 74 65 20 3d  }.  u.ao.nByte =
1d6fc 20 75 2e 61 6f 2e 6e 48 64 72 2b 75 2e 61 6f 2e   u.ao.nHdr+u.ao.
1d6fd 6e 44 61 74 61 2d 75 2e 61 6f 2e 6e 5a 65 72 6f  nData-u.ao.nZero
1d6fe 3b 0a 20 20 69 66 28 20 75 2e 61 6f 2e 6e 42 79  ;.  if( u.ao.nBy
1d6ff 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  te>db->aLimit[SQ
1d700 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
1d701 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74  H] ){.    goto t
1d702 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20 2f  oo_big;.  }..  /
1d703 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20  * Make sure the 
1d704 6f 75 74 70 75 74 20 72 65 67 69 73 74 65 72 20  output register 
1d705 68 61 73 20 61 20 62 75 66 66 65 72 20 6c 61 72  has a buffer lar
1d706 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 73 74 6f  ge enough to sto
1d707 72 65 0a 20 20 2a 2a 20 74 68 65 20 6e 65 77 20  re.  ** the new 
1d708 72 65 63 6f 72 64 2e 20 54 68 65 20 6f 75 74 70  record. The outp
1d709 75 74 20 72 65 67 69 73 74 65 72 20 28 70 4f 70  ut register (pOp
1d70a 2d 3e 70 33 29 20 69 73 20 6e 6f 74 20 61 6c 6c  ->p3) is not all
1d70b 6f 77 65 64 20 74 6f 0a 20 20 2a 2a 20 62 65 20  owed to.  ** be 
1d70c 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74  one of the input
1d70d 20 72 65 67 69 73 74 65 72 73 20 28 62 65 63 61   registers (beca
1d70e 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  use the followin
1d70f 67 20 63 61 6c 6c 20 74 6f 0a 20 20 2a 2a 20 73  g call to.  ** s
1d710 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
1d711 77 28 29 20 63 6f 75 6c 64 20 63 6c 6f 62 62 65  w() could clobbe
1d712 72 20 74 68 65 20 76 61 6c 75 65 20 62 65 66 6f  r the value befo
1d713 72 65 20 69 74 20 69 73 20 75 73 65 64 29 2e 0a  re it is used)..
1d714 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
1d715 4f 70 2d 3e 70 33 3c 70 4f 70 2d 3e 70 31 20 7c  Op->p3<pOp->p1 |
1d716 7c 20 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e  | pOp->p3>=pOp->
1d717 70 31 2b 70 4f 70 2d 3e 70 32 20 29 3b 0a 20 20  p1+pOp->p2 );.  
1d718 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pOut = &p->aMem[
1d719 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20  pOp->p3];.  if( 
1d71a 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
1d71b 6f 77 28 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e  ow(pOut, (int)u.
1d71c 61 6f 2e 6e 42 79 74 65 2c 20 30 29 20 29 7b 0a  ao.nByte, 0) ){.
1d71d 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b      goto no_mem;
1d71e 0a 20 20 7d 0a 20 20 75 2e 61 6f 2e 7a 4e 65 77  .  }.  u.ao.zNew
1d71f 52 65 63 6f 72 64 20 3d 20 28 75 38 20 2a 29 70  Record = (u8 *)p
1d720 4f 75 74 2d 3e 7a 3b 0a 0a 20 20 2f 2a 20 57 72  Out->z;..  /* Wr
1d721 69 74 65 20 74 68 65 20 72 65 63 6f 72 64 20 2a  ite the record *
1d722 2f 0a 20 20 75 2e 61 6f 2e 69 20 3d 20 70 75 74  /.  u.ao.i = put
1d723 56 61 72 69 6e 74 33 32 28 75 2e 61 6f 2e 7a 4e  Varint32(u.ao.zN
1d724 65 77 52 65 63 6f 72 64 2c 20 75 2e 61 6f 2e 6e  ewRecord, u.ao.n
1d725 48 64 72 29 3b 0a 20 20 66 6f 72 28 75 2e 61 6f  Hdr);.  for(u.ao
1d726 2e 70 52 65 63 3d 75 2e 61 6f 2e 70 44 61 74 61  .pRec=u.ao.pData
1d727 30 3b 20 75 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e  0; u.ao.pRec<=u.
1d728 61 6f 2e 70 4c 61 73 74 3b 20 75 2e 61 6f 2e 70  ao.pLast; u.ao.p
1d729 52 65 63 2b 2b 29 7b 0a 20 20 20 20 75 2e 61 6f  Rec++){.    u.ao
1d72a 2e 73 65 72 69 61 6c 5f 74 79 70 65 20 3d 20 73  .serial_type = s
1d72b 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1d72c 54 79 70 65 28 75 2e 61 6f 2e 70 52 65 63 2c 20  Type(u.ao.pRec, 
1d72d 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f 72 6d 61 74  u.ao.file_format
1d72e 29 3b 0a 20 20 20 20 75 2e 61 6f 2e 69 20 2b 3d  );.    u.ao.i +=
1d72f 20 70 75 74 56 61 72 69 6e 74 33 32 28 26 75 2e   putVarint32(&u.
1d730 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64 5b 75 2e  ao.zNewRecord[u.
1d731 61 6f 2e 69 5d 2c 20 75 2e 61 6f 2e 73 65 72 69  ao.i], u.ao.seri
1d732 61 6c 5f 74 79 70 65 29 3b 20 20 20 20 20 20 2f  al_type);      /
1d733 2a 20 73 65 72 69 61 6c 20 74 79 70 65 20 2a 2f  * serial type */
1d734 0a 20 20 7d 0a 20 20 66 6f 72 28 75 2e 61 6f 2e  .  }.  for(u.ao.
1d735 70 52 65 63 3d 75 2e 61 6f 2e 70 44 61 74 61 30  pRec=u.ao.pData0
1d736 3b 20 75 2e 61 6f 2e 70 52 65 63 3c 3d 75 2e 61  ; u.ao.pRec<=u.a
1d737 6f 2e 70 4c 61 73 74 3b 20 75 2e 61 6f 2e 70 52  o.pLast; u.ao.pR
1d738 65 63 2b 2b 29 7b 20 20 2f 2a 20 73 65 72 69 61  ec++){  /* seria
1d739 6c 20 64 61 74 61 20 2a 2f 0a 20 20 20 20 75 2e  l data */.    u.
1d73a 61 6f 2e 69 20 2b 3d 20 73 71 6c 69 74 65 33 56  ao.i += sqlite3V
1d73b 64 62 65 53 65 72 69 61 6c 50 75 74 28 26 75 2e  dbeSerialPut(&u.
1d73c 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64 5b 75 2e  ao.zNewRecord[u.
1d73d 61 6f 2e 69 5d 2c 20 28 69 6e 74 29 28 75 2e 61  ao.i], (int)(u.a
1d73e 6f 2e 6e 42 79 74 65 2d 75 2e 61 6f 2e 69 29 2c  o.nByte-u.ao.i),
1d73f 20 75 2e 61 6f 2e 70 52 65 63 2c 75 2e 61 6f 2e   u.ao.pRec,u.ao.
1d740 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20  file_format);.  
1d741 7d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6f  }.  assert( u.ao
1d742 2e 69 3d 3d 75 2e 61 6f 2e 6e 42 79 74 65 20 29  .i==u.ao.nByte )
1d743 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  ;..  assert( pOp
1d744 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p3>0 && pOp->p
1d745 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  3<=p->nMem );.  
1d746 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 29 75  pOut->n = (int)u
1d747 2e 61 6f 2e 6e 42 79 74 65 3b 0a 20 20 70 4f 75  .ao.nByte;.  pOu
1d748 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42  t->flags = MEM_B
1d749 6c 6f 62 20 7c 20 4d 45 4d 5f 44 79 6e 3b 0a 20  lob | MEM_Dyn;. 
1d74a 20 70 4f 75 74 2d 3e 78 44 65 6c 20 3d 20 30 3b   pOut->xDel = 0;
1d74b 0a 20 20 69 66 28 20 75 2e 61 6f 2e 6e 5a 65 72  .  if( u.ao.nZer
1d74c 6f 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 75  o ){.    pOut->u
1d74d 2e 6e 5a 65 72 6f 20 3d 20 75 2e 61 6f 2e 6e 5a  .nZero = u.ao.nZ
1d74e 65 72 6f 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66  ero;.    pOut->f
1d74f 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 5a 65 72 6f  lags |= MEM_Zero
1d750 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e  ;.  }.  pOut->en
1d751 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b  c = SQLITE_UTF8;
1d752 20 20 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65    /* In case the
1d753 20 62 6c 6f 62 20 69 73 20 65 76 65 72 20 63 6f   blob is ever co
1d754 6e 76 65 72 74 65 64 20 74 6f 20 74 65 78 74 20  nverted to text 
1d755 2a 2f 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52  */.  REGISTER_TR
1d756 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75  ACE(pOp->p3, pOu
1d757 74 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58  t);.  UPDATE_MAX
1d758 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b  _BLOBSIZE(pOut);
1d759 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1d75a 4f 70 63 6f 64 65 3a 20 43 6f 75 6e 74 20 50 31  Opcode: Count P1
1d75b 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1d75c 53 74 6f 72 65 20 74 68 65 20 6e 75 6d 62 65 72  Store the number
1d75d 20 6f 66 20 65 6e 74 72 69 65 73 20 28 61 6e 20   of entries (an 
1d75e 69 6e 74 65 67 65 72 20 76 61 6c 75 65 29 20 69  integer value) i
1d75f 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69  n the table or i
1d760 6e 64 65 78 20 0a 2a 2a 20 6f 70 65 6e 65 64 20  ndex .** opened 
1d761 62 79 20 63 75 72 73 6f 72 20 50 31 20 69 6e 20  by cursor P1 in 
1d762 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2f 0a 23  register P2.*/.#
1d763 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1d764 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 63 61  IT_BTREECOUNT.ca
1d765 73 65 20 4f 50 5f 43 6f 75 6e 74 3a 20 7b 20 20  se OP_Count: {  
1d766 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70         /* out2-p
1d767 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66  rerelease */.#if
1d768 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1d769 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1d76a 6f 20 75 2e 61 70 20 2a 2f 0a 20 20 69 36 34 20  o u.ap */.  i64 
1d76b 6e 45 6e 74 72 79 3b 0a 20 20 42 74 43 75 72 73  nEntry;.  BtCurs
1d76c 6f 72 20 2a 70 43 72 73 72 3b 0a 23 65 6e 64 69  or *pCrsr;.#endi
1d76d 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1d76e 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1d76f 75 2e 61 70 20 2a 2f 0a 0a 20 20 75 2e 61 70 2e  u.ap */..  u.ap.
1d770 70 43 72 73 72 20 3d 20 70 2d 3e 61 70 43 73 72  pCrsr = p->apCsr
1d771 5b 70 4f 70 2d 3e 70 31 5d 2d 3e 70 43 75 72 73  [pOp->p1]->pCurs
1d772 6f 72 3b 0a 20 20 69 66 28 20 75 2e 61 70 2e 70  or;.  if( u.ap.p
1d773 43 72 73 72 20 29 7b 0a 20 20 20 20 72 63 20 3d  Crsr ){.    rc =
1d774 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 75   sqlite3BtreeCou
1d775 6e 74 28 75 2e 61 70 2e 70 43 72 73 72 2c 20 26  nt(u.ap.pCrsr, &
1d776 75 2e 61 70 2e 6e 45 6e 74 72 79 29 3b 0a 20 20  u.ap.nEntry);.  
1d777 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 70 2e  }else{.    u.ap.
1d778 6e 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 7d 0a  nEntry = 0;.  }.
1d779 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20    pOut->flags = 
1d77a 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 2d  MEM_Int;.  pOut-
1d77b 3e 75 2e 69 20 3d 20 75 2e 61 70 2e 6e 45 6e 74  >u.i = u.ap.nEnt
1d77c 72 79 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23  ry;.  break;.}.#
1d77d 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65  endif../* Opcode
1d77e 3a 20 53 61 76 65 70 6f 69 6e 74 20 50 31 20 2a  : Savepoint P1 *
1d77f 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70   * P4 *.**.** Op
1d780 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f 72 20 72  en, release or r
1d781 6f 6c 6c 62 61 63 6b 20 74 68 65 20 73 61 76 65  ollback the save
1d782 70 6f 69 6e 74 20 6e 61 6d 65 64 20 62 79 20 70  point named by p
1d783 61 72 61 6d 65 74 65 72 20 50 34 2c 20 64 65 70  arameter P4, dep
1d784 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65  ending.** on the
1d785 20 76 61 6c 75 65 20 6f 66 20 50 31 2e 20 54 6f   value of P1. To
1d786 20 6f 70 65 6e 20 61 20 6e 65 77 20 73 61 76 65   open a new save
1d787 70 6f 69 6e 74 2c 20 50 31 3d 3d 30 2e 20 54 6f  point, P1==0. To
1d788 20 72 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74   release (commit
1d789 29 20 61 6e 0a 2a 2a 20 65 78 69 73 74 69 6e 67  ) an.** existing
1d78a 20 73 61 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d   savepoint, P1==
1d78b 31 2c 20 6f 72 20 74 6f 20 72 6f 6c 6c 62 61 63  1, or to rollbac
1d78c 6b 20 61 6e 20 65 78 69 73 74 69 6e 67 20 73 61  k an existing sa
1d78d 76 65 70 6f 69 6e 74 20 50 31 3d 3d 32 2e 0a 2a  vepoint P1==2..*
1d78e 2f 0a 63 61 73 65 20 4f 50 5f 53 61 76 65 70 6f  /.case OP_Savepo
1d78f 69 6e 74 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a  int: {.#if 0  /*
1d790 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1d791 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 71   moved into u.aq
1d792 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 20   */.  int p1;   
1d793 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d794 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f        /* Value o
1d795 66 20 50 31 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  f P1 operand */.
1d796 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
1d797 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d798 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 73 61 76    /* Name of sav
1d799 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  epoint */.  int 
1d79a 6e 4e 61 6d 65 3b 0a 20 20 53 61 76 65 70 6f 69  nName;.  Savepoi
1d79b 6e 74 20 2a 70 4e 65 77 3b 0a 20 20 53 61 76 65  nt *pNew;.  Save
1d79c 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f 69 6e  point *pSavepoin
1d79d 74 3b 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a  t;.  Savepoint *
1d79e 70 54 6d 70 3b 0a 20 20 69 6e 74 20 69 53 61 76  pTmp;.  int iSav
1d79f 65 70 6f 69 6e 74 3b 0a 20 20 69 6e 74 20 69 69  epoint;.  int ii
1d7a0 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1d7a1 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1d7a2 64 20 69 6e 74 6f 20 75 2e 61 71 20 2a 2f 0a 0a  d into u.aq */..
1d7a3 20 20 75 2e 61 71 2e 70 31 20 3d 20 70 4f 70 2d    u.aq.p1 = pOp-
1d7a4 3e 70 31 3b 0a 20 20 75 2e 61 71 2e 7a 4e 61 6d  >p1;.  u.aq.zNam
1d7a5 65 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 0a  e = pOp->p4.z;..
1d7a6 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74    /* Assert that
1d7a7 20 74 68 65 20 75 2e 61 71 2e 70 31 20 70 61 72   the u.aq.p1 par
1d7a8 61 6d 65 74 65 72 20 69 73 20 76 61 6c 69 64 2e  ameter is valid.
1d7a9 20 41 6c 73 6f 20 74 68 61 74 20 69 66 20 74 68   Also that if th
1d7aa 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e 0a 20  ere is no open. 
1d7ab 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c   ** transaction,
1d7ac 20 74 68 65 6e 20 74 68 65 72 65 20 63 61 6e 6e   then there cann
1d7ad 6f 74 20 62 65 20 61 6e 79 20 73 61 76 65 70 6f  ot be any savepo
1d7ae 69 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  ints..  */.  ass
1d7af 65 72 74 28 20 64 62 2d 3e 70 53 61 76 65 70 6f  ert( db->pSavepo
1d7b0 69 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 75  int==0 || db->au
1d7b1 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 3b 0a 20  toCommit==0 );. 
1d7b2 20 61 73 73 65 72 74 28 20 75 2e 61 71 2e 70 31   assert( u.aq.p1
1d7b3 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49  ==SAVEPOINT_BEGI
1d7b4 4e 7c 7c 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45  N||u.aq.p1==SAVE
1d7b5 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 7c 7c 75  POINT_RELEASE||u
1d7b6 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e  .aq.p1==SAVEPOIN
1d7b7 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20  T_ROLLBACK );.  
1d7b8 61 73 73 65 72 74 28 20 64 62 2d 3e 70 53 61 76  assert( db->pSav
1d7b9 65 70 6f 69 6e 74 20 7c 7c 20 64 62 2d 3e 69 73  epoint || db->is
1d7ba 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70  TransactionSavep
1d7bb 6f 69 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73  oint==0 );.  ass
1d7bc 65 72 74 28 20 63 68 65 63 6b 53 61 76 65 70 6f  ert( checkSavepo
1d7bd 69 6e 74 43 6f 75 6e 74 28 64 62 29 20 29 3b 0a  intCount(db) );.
1d7be 0a 20 20 69 66 28 20 75 2e 61 71 2e 70 31 3d 3d  .  if( u.aq.p1==
1d7bf 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e 20  SAVEPOINT_BEGIN 
1d7c0 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 77  ){.    if( db->w
1d7c1 72 69 74 65 56 64 62 65 43 6e 74 3e 30 20 29 7b  riteVdbeCnt>0 ){
1d7c2 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e 65 77 20  .      /* A new 
1d7c3 73 61 76 65 70 6f 69 6e 74 20 63 61 6e 6e 6f 74  savepoint cannot
1d7c4 20 62 65 20 63 72 65 61 74 65 64 20 69 66 20 74   be created if t
1d7c5 68 65 72 65 20 61 72 65 20 61 63 74 69 76 65 20  here are active 
1d7c6 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 73  write.      ** s
1d7c7 74 61 74 65 6d 65 6e 74 73 20 28 69 2e 65 2e 20  tatements (i.e. 
1d7c8 6f 70 65 6e 20 72 65 61 64 2f 77 72 69 74 65 20  open read/write 
1d7c9 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62  incremental blob
1d7ca 20 68 61 6e 64 6c 65 73 29 2e 0a 20 20 20 20 20   handles)..     
1d7cb 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
1d7cc 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
1d7cd 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e  ErrMsg, db, "can
1d7ce 6e 6f 74 20 6f 70 65 6e 20 73 61 76 65 70 6f 69  not open savepoi
1d7cf 6e 74 20 2d 20 22 0a 20 20 20 20 20 20 20 20 22  nt - ".        "
1d7d0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69  SQL statements i
1d7d1 6e 20 70 72 6f 67 72 65 73 73 22 29 3b 0a 20 20  n progress");.  
1d7d2 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1d7d3 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  BUSY;.    }else{
1d7d4 0a 20 20 20 20 20 20 75 2e 61 71 2e 6e 4e 61 6d  .      u.aq.nNam
1d7d5 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  e = sqlite3Strle
1d7d6 6e 33 30 28 75 2e 61 71 2e 7a 4e 61 6d 65 29 3b  n30(u.aq.zName);
1d7d7 0a 0a 20 20 20 20 20 20 2f 2a 20 43 72 65 61 74  ..      /* Creat
1d7d8 65 20 61 20 6e 65 77 20 73 61 76 65 70 6f 69 6e  e a new savepoin
1d7d9 74 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a  t structure. */.
1d7da 20 20 20 20 20 20 75 2e 61 71 2e 70 4e 65 77 20        u.aq.pNew 
1d7db 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
1d7dc 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28  cRaw(db, sizeof(
1d7dd 53 61 76 65 70 6f 69 6e 74 29 2b 75 2e 61 71 2e  Savepoint)+u.aq.
1d7de 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 20 20  nName+1);.      
1d7df 69 66 28 20 75 2e 61 71 2e 70 4e 65 77 20 29 7b  if( u.aq.pNew ){
1d7e0 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e 70 4e  .        u.aq.pN
1d7e1 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61  ew->zName = (cha
1d7e2 72 20 2a 29 26 75 2e 61 71 2e 70 4e 65 77 5b 31  r *)&u.aq.pNew[1
1d7e3 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  ];.        memcp
1d7e4 79 28 75 2e 61 71 2e 70 4e 65 77 2d 3e 7a 4e 61  y(u.aq.pNew->zNa
1d7e5 6d 65 2c 20 75 2e 61 71 2e 7a 4e 61 6d 65 2c 20  me, u.aq.zName, 
1d7e6 75 2e 61 71 2e 6e 4e 61 6d 65 2b 31 29 3b 0a 0a  u.aq.nName+1);..
1d7e7 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
1d7e8 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e 20 74  ere is no open t
1d7e9 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e  ransaction, then
1d7ea 20 6d 61 72 6b 20 74 68 69 73 20 61 73 20 61 20   mark this as a 
1d7eb 73 70 65 63 69 61 6c 0a 20 20 20 20 20 20 20 20  special.        
1d7ec 2a 2a 20 22 74 72 61 6e 73 61 63 74 69 6f 6e 20  ** "transaction 
1d7ed 73 61 76 65 70 6f 69 6e 74 22 2e 20 2a 2f 0a 20  savepoint". */. 
1d7ee 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61         if( db->a
1d7ef 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20  utoCommit ){.   
1d7f0 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43         db->autoC
1d7f1 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20  ommit = 0;.     
1d7f2 20 20 20 20 20 64 62 2d 3e 69 73 54 72 61 6e 73       db->isTrans
1d7f3 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20  actionSavepoint 
1d7f4 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  = 1;.        }el
1d7f5 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62  se{.          db
1d7f6 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2b 2b 3b 0a  ->nSavepoint++;.
1d7f7 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20          }..     
1d7f8 20 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 6e     /* Link the n
1d7f9 65 77 20 73 61 76 65 70 6f 69 6e 74 20 69 6e 74  ew savepoint int
1d7fa 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  o the database h
1d7fb 61 6e 64 6c 65 27 73 20 6c 69 73 74 2e 20 2a 2f  andle's list. */
1d7fc 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e 70 4e  .        u.aq.pN
1d7fd 65 77 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e  ew->pNext = db->
1d7fe 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20  pSavepoint;.    
1d7ff 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70 6f 69      db->pSavepoi
1d800 6e 74 20 3d 20 75 2e 61 71 2e 70 4e 65 77 3b 0a  nt = u.aq.pNew;.
1d801 20 20 20 20 20 20 20 20 75 2e 61 71 2e 70 4e 65          u.aq.pNe
1d802 77 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73  w->nDeferredCons
1d803 20 3d 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64   = db->nDeferred
1d804 43 6f 6e 73 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Cons;.      }.  
1d805 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
1d806 20 75 2e 61 71 2e 69 53 61 76 65 70 6f 69 6e 74   u.aq.iSavepoint
1d807 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 46 69   = 0;..    /* Fi
1d808 6e 64 20 74 68 65 20 6e 61 6d 65 64 20 73 61 76  nd the named sav
1d809 65 70 6f 69 6e 74 2e 20 49 66 20 74 68 65 72 65  epoint. If there
1d80a 20 69 73 20 6e 6f 20 73 75 63 68 20 73 61 76 65   is no such save
1d80b 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 6e 0a 20  point, then an. 
1d80c 20 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69     ** an error i
1d80d 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68  s returned to th
1d80e 65 20 75 73 65 72 2e 20 20 2a 2f 0a 20 20 20 20  e user.  */.    
1d80f 66 6f 72 28 0a 20 20 20 20 20 20 75 2e 61 71 2e  for(.      u.aq.
1d810 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 64 62 2d  pSavepoint = db-
1d811 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20  >pSavepoint;.   
1d812 20 20 20 75 2e 61 71 2e 70 53 61 76 65 70 6f 69     u.aq.pSavepoi
1d813 6e 74 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  nt && sqlite3Str
1d814 49 43 6d 70 28 75 2e 61 71 2e 70 53 61 76 65 70  ICmp(u.aq.pSavep
1d815 6f 69 6e 74 2d 3e 7a 4e 61 6d 65 2c 20 75 2e 61  oint->zName, u.a
1d816 71 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  q.zName);.      
1d817 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 20  u.aq.pSavepoint 
1d818 3d 20 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e  = u.aq.pSavepoin
1d819 74 2d 3e 70 4e 65 78 74 0a 20 20 20 20 29 7b 0a  t->pNext.    ){.
1d81a 20 20 20 20 20 20 75 2e 61 71 2e 69 53 61 76 65        u.aq.iSave
1d81b 70 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20  point++;.    }. 
1d81c 20 20 20 69 66 28 20 21 75 2e 61 71 2e 70 53 61     if( !u.aq.pSa
1d81d 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 20  vepoint ){.     
1d81e 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
1d81f 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  g(&p->zErrMsg, d
1d820 62 2c 20 22 6e 6f 20 73 75 63 68 20 73 61 76 65  b, "no such save
1d821 70 6f 69 6e 74 3a 20 25 73 22 2c 20 75 2e 61 71  point: %s", u.aq
1d822 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72  .zName);.      r
1d823 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
1d824 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 0a  ;.    }else if(.
1d825 20 20 20 20 20 20 20 20 64 62 2d 3e 77 72 69 74          db->writ
1d826 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20 28 75  eVdbeCnt>0 || (u
1d827 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e  .aq.p1==SAVEPOIN
1d828 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20 64 62  T_ROLLBACK && db
1d829 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e  ->activeVdbeCnt>
1d82a 31 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  1).    ){.      
1d82b 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f 73  /* It is not pos
1d82c 73 69 62 6c 65 20 74 6f 20 72 65 6c 65 61 73 65  sible to release
1d82d 20 28 63 6f 6d 6d 69 74 29 20 61 20 73 61 76 65   (commit) a save
1d82e 70 6f 69 6e 74 20 69 66 20 74 68 65 72 65 20 61  point if there a
1d82f 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 63 74 69  re.      ** acti
1d830 76 65 20 77 72 69 74 65 20 73 74 61 74 65 6d 65  ve write stateme
1d831 6e 74 73 2e 20 49 74 20 69 73 20 6e 6f 74 20 70  nts. It is not p
1d832 6f 73 73 69 62 6c 65 20 74 6f 20 72 6f 6c 6c 62  ossible to rollb
1d833 61 63 6b 20 61 20 73 61 76 65 70 6f 69 6e 74 0a  ack a savepoint.
1d834 20 20 20 20 20 20 2a 2a 20 69 66 20 74 68 65 72        ** if ther
1d835 65 20 61 72 65 20 61 6e 79 20 61 63 74 69 76 65  e are any active
1d836 20 73 74 61 74 65 6d 65 6e 74 73 20 61 74 20 61   statements at a
1d837 6c 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ll..      */.   
1d838 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
1d839 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
1d83a 20 64 62 2c 0a 20 20 20 20 20 20 20 20 22 63 61   db,.        "ca
1d83b 6e 6e 6f 74 20 25 73 20 73 61 76 65 70 6f 69 6e  nnot %s savepoin
1d83c 74 20 2d 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  t - SQL statemen
1d83d 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 2c  ts in progress",
1d83e 0a 20 20 20 20 20 20 20 20 28 75 2e 61 71 2e 70  .        (u.aq.p
1d83f 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c  1==SAVEPOINT_ROL
1d840 4c 42 41 43 4b 20 3f 20 22 72 6f 6c 6c 62 61 63  LBACK ? "rollbac
1d841 6b 22 3a 20 22 72 65 6c 65 61 73 65 22 29 0a 20  k": "release"). 
1d842 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 72 63       );.      rc
1d843 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a   = SQLITE_BUSY;.
1d844 20 20 20 20 7d 65 6c 73 65 7b 0a 0a 20 20 20 20      }else{..    
1d845 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 77    /* Determine w
1d846 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
1d847 69 73 20 69 73 20 61 20 74 72 61 6e 73 61 63 74  is is a transact
1d848 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 2e 20 49  ion savepoint. I
1d849 66 20 73 6f 2c 0a 20 20 20 20 20 20 2a 2a 20 61  f so,.      ** a
1d84a 6e 64 20 74 68 69 73 20 69 73 20 61 20 52 45 4c  nd this is a REL
1d84b 45 41 53 45 20 63 6f 6d 6d 61 6e 64 2c 20 74 68  EASE command, th
1d84c 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  en the current t
1d84d 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20 20  ransaction.     
1d84e 20 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 74 65 64   ** is committed
1d84f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
1d850 20 69 6e 74 20 69 73 54 72 61 6e 73 61 63 74 69   int isTransacti
1d851 6f 6e 20 3d 20 75 2e 61 71 2e 70 53 61 76 65 70  on = u.aq.pSavep
1d852 6f 69 6e 74 2d 3e 70 4e 65 78 74 3d 3d 30 20 26  oint->pNext==0 &
1d853 26 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74  & db->isTransact
1d854 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20  ionSavepoint;.  
1d855 20 20 20 20 69 66 28 20 69 73 54 72 61 6e 73 61      if( isTransa
1d856 63 74 69 6f 6e 20 26 26 20 75 2e 61 71 2e 70 31  ction && u.aq.p1
1d857 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45  ==SAVEPOINT_RELE
1d858 41 53 45 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ASE ){.        i
1d859 66 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33  f( (rc = sqlite3
1d85a 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c 20 31  VdbeCheckFk(p, 1
1d85b 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  ))!=SQLITE_OK ){
1d85c 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
1d85d 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20  vdbe_return;.   
1d85e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64       }.        d
1d85f 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20  b->autoCommit = 
1d860 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  1;.        if( s
1d861 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 70  qlite3VdbeHalt(p
1d862 29 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29  )==SQLITE_BUSY )
1d863 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70  {.          p->p
1d864 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 20 20  c = pc;.        
1d865 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74    db->autoCommit
1d866 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
1d867 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c  p->rc = rc = SQL
1d868 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20  ITE_BUSY;.      
1d869 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65      goto vdbe_re
1d86a 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a  turn;.        }.
1d86b 20 20 20 20 20 20 20 20 64 62 2d 3e 69 73 54 72          db->isTr
1d86c 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69  ansactionSavepoi
1d86d 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  nt = 0;.        
1d86e 72 63 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 20 20  rc = p->rc;.    
1d86f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1d870 20 75 2e 61 71 2e 69 53 61 76 65 70 6f 69 6e 74   u.aq.iSavepoint
1d871 20 3d 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e   = db->nSavepoin
1d872 74 20 2d 20 75 2e 61 71 2e 69 53 61 76 65 70 6f  t - u.aq.iSavepo
1d873 69 6e 74 20 2d 20 31 3b 0a 20 20 20 20 20 20 20  int - 1;.       
1d874 20 66 6f 72 28 75 2e 61 71 2e 69 69 3d 30 3b 20   for(u.aq.ii=0; 
1d875 75 2e 61 71 2e 69 69 3c 64 62 2d 3e 6e 44 62 3b  u.aq.ii<db->nDb;
1d876 20 75 2e 61 71 2e 69 69 2b 2b 29 7b 0a 20 20 20   u.aq.ii++){.   
1d877 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1d878 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e  te3BtreeSavepoin
1d879 74 28 64 62 2d 3e 61 44 62 5b 75 2e 61 71 2e 69  t(db->aDb[u.aq.i
1d87a 69 5d 2e 70 42 74 2c 20 75 2e 61 71 2e 70 31 2c  i].pBt, u.aq.p1,
1d87b 20 75 2e 61 71 2e 69 53 61 76 65 70 6f 69 6e 74   u.aq.iSavepoint
1d87c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
1d87d 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1d87e 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f  {.            go
1d87f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1d880 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 20  error;.         
1d881 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
1d882 20 20 20 20 20 69 66 28 20 75 2e 61 71 2e 70 31       if( u.aq.p1
1d883 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  ==SAVEPOINT_ROLL
1d884 42 41 43 4b 20 26 26 20 28 64 62 2d 3e 66 6c 61  BACK && (db->fla
1d885 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  gs&SQLITE_Intern
1d886 43 68 61 6e 67 65 73 29 21 3d 30 20 29 7b 0a 20  Changes)!=0 ){. 
1d887 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1d888 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74  ExpirePreparedSt
1d889 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20  atements(db);.  
1d88a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52          sqlite3R
1d88b 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65  esetInternalSche
1d88c 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 20 20 20  ma(db, 0);.     
1d88d 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20     }.      }..  
1d88e 20 20 20 20 2f 2a 20 52 65 67 61 72 64 6c 65 73      /* Regardles
1d88f 73 20 6f 66 20 77 68 65 74 68 65 72 20 74 68 69  s of whether thi
1d890 73 20 69 73 20 61 20 52 45 4c 45 41 53 45 20 6f  s is a RELEASE o
1d891 72 20 52 4f 4c 4c 42 41 43 4b 2c 20 64 65 73 74  r ROLLBACK, dest
1d892 72 6f 79 20 61 6c 6c 0a 20 20 20 20 20 20 2a 2a  roy all.      **
1d893 20 73 61 76 65 70 6f 69 6e 74 73 20 6e 65 73 74   savepoints nest
1d894 65 64 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65  ed inside of the
1d895 20 73 61 76 65 70 6f 69 6e 74 20 62 65 69 6e 67   savepoint being
1d896 20 6f 70 65 72 61 74 65 64 20 6f 6e 2e 20 2a 2f   operated on. */
1d897 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 64 62  .      while( db
1d898 2d 3e 70 53 61 76 65 70 6f 69 6e 74 21 3d 75 2e  ->pSavepoint!=u.
1d899 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 29 7b  aq.pSavepoint ){
1d89a 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e 70 54  .        u.aq.pT
1d89b 6d 70 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f  mp = db->pSavepo
1d89c 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 64 62 2d  int;.        db-
1d89d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 75 2e  >pSavepoint = u.
1d89e 61 71 2e 70 54 6d 70 2d 3e 70 4e 65 78 74 3b 0a  aq.pTmp->pNext;.
1d89f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
1d8a0 62 46 72 65 65 28 64 62 2c 20 75 2e 61 71 2e 70  bFree(db, u.aq.p
1d8a1 54 6d 70 29 3b 0a 20 20 20 20 20 20 20 20 64 62  Tmp);.        db
1d8a2 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2d 2d 3b 0a  ->nSavepoint--;.
1d8a3 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
1d8a4 2a 20 49 66 20 69 74 20 69 73 20 61 20 52 45 4c  * If it is a REL
1d8a5 45 41 53 45 2c 20 74 68 65 6e 20 64 65 73 74 72  EASE, then destr
1d8a6 6f 79 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74  oy the savepoint
1d8a7 20 62 65 69 6e 67 20 6f 70 65 72 61 74 65 64 20   being operated 
1d8a8 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 74 6f 6f 2e  on.      ** too.
1d8a9 20 49 66 20 69 74 20 69 73 20 61 20 52 4f 4c 4c   If it is a ROLL
1d8aa 42 41 43 4b 20 54 4f 2c 20 74 68 65 6e 20 73 65  BACK TO, then se
1d8ab 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
1d8ac 64 65 66 65 72 72 65 64 0a 20 20 20 20 20 20 2a  deferred.      *
1d8ad 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f  * constraint vio
1d8ae 6c 61 74 69 6f 6e 73 20 70 72 65 73 65 6e 74 20  lations present 
1d8af 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
1d8b0 74 6f 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f  to the value sto
1d8b1 72 65 64 0a 20 20 20 20 20 20 2a 2a 20 77 68 65  red.      ** whe
1d8b2 6e 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20  n the savepoint 
1d8b3 77 61 73 20 63 72 65 61 74 65 64 2e 20 20 2a 2f  was created.  */
1d8b4 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 71 2e  .      if( u.aq.
1d8b5 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45  p1==SAVEPOINT_RE
1d8b6 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 20 20 20  LEASE ){.       
1d8b7 20 61 73 73 65 72 74 28 20 75 2e 61 71 2e 70 53   assert( u.aq.pS
1d8b8 61 76 65 70 6f 69 6e 74 3d 3d 64 62 2d 3e 70 53  avepoint==db->pS
1d8b9 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 20 20 20  avepoint );.    
1d8ba 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70 6f 69      db->pSavepoi
1d8bb 6e 74 20 3d 20 75 2e 61 71 2e 70 53 61 76 65 70  nt = u.aq.pSavep
1d8bc 6f 69 6e 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  oint->pNext;.   
1d8bd 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1d8be 65 65 28 64 62 2c 20 75 2e 61 71 2e 70 53 61 76  ee(db, u.aq.pSav
1d8bf 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 20  epoint);.       
1d8c0 20 69 66 28 20 21 69 73 54 72 61 6e 73 61 63 74   if( !isTransact
1d8c1 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ion ){.         
1d8c2 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2d   db->nSavepoint-
1d8c3 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  -;.        }.   
1d8c4 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1d8c5 20 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 43    db->nDeferredC
1d8c6 6f 6e 73 20 3d 20 75 2e 61 71 2e 70 53 61 76 65  ons = u.aq.pSave
1d8c7 70 6f 69 6e 74 2d 3e 6e 44 65 66 65 72 72 65 64  point->nDeferred
1d8c8 43 6f 6e 73 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Cons;.      }.  
1d8c9 20 20 7d 0a 20 20 7d 0a 0a 20 20 62 72 65 61 6b    }.  }..  break
1d8ca 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1d8cb 41 75 74 6f 43 6f 6d 6d 69 74 20 50 31 20 50 32  AutoCommit P1 P2
1d8cc 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 65 74   * * *.**.** Set
1d8cd 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 75   the database au
1d8ce 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 20 74  to-commit flag t
1d8cf 6f 20 50 31 20 28 31 20 6f 72 20 30 29 2e 20 49  o P1 (1 or 0). I
1d8d0 66 20 50 32 20 69 73 20 74 72 75 65 2c 20 72 6f  f P2 is true, ro
1d8d1 6c 6c 0a 2a 2a 20 62 61 63 6b 20 61 6e 79 20 63  ll.** back any c
1d8d2 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 65 20  urrently active 
1d8d3 62 74 72 65 65 20 74 72 61 6e 73 61 63 74 69 6f  btree transactio
1d8d4 6e 73 2e 20 49 66 20 74 68 65 72 65 20 61 72 65  ns. If there are
1d8d5 20 61 6e 79 20 61 63 74 69 76 65 0a 2a 2a 20 56   any active.** V
1d8d6 4d 73 20 28 61 70 61 72 74 20 66 72 6f 6d 20 74  Ms (apart from t
1d8d7 68 69 73 20 6f 6e 65 29 2c 20 74 68 65 6e 20 61  his one), then a
1d8d8 20 52 4f 4c 4c 42 41 43 4b 20 66 61 69 6c 73 2e   ROLLBACK fails.
1d8d9 20 20 41 20 43 4f 4d 4d 49 54 20 66 61 69 6c 73    A COMMIT fails
1d8da 20 69 66 0a 2a 2a 20 74 68 65 72 65 20 61 72 65   if.** there are
1d8db 20 61 63 74 69 76 65 20 77 72 69 74 69 6e 67 20   active writing 
1d8dc 56 4d 73 20 6f 72 20 61 63 74 69 76 65 20 56 4d  VMs or active VM
1d8dd 73 20 74 68 61 74 20 75 73 65 20 73 68 61 72 65  s that use share
1d8de 64 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54  d cache..**.** T
1d8df 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1d8e0 63 61 75 73 65 73 20 74 68 65 20 56 4d 20 74 6f  causes the VM to
1d8e1 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f   halt..*/.case O
1d8e2 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 3a 20 7b 0a  P_AutoCommit: {.
1d8e3 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1d8e4 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1d8e5 69 6e 74 6f 20 75 2e 61 72 20 2a 2f 0a 20 20 69  into u.ar */.  i
1d8e6 6e 74 20 64 65 73 69 72 65 64 41 75 74 6f 43 6f  nt desiredAutoCo
1d8e7 6d 6d 69 74 3b 0a 20 20 69 6e 74 20 69 52 6f 6c  mmit;.  int iRol
1d8e8 6c 62 61 63 6b 3b 0a 20 20 69 6e 74 20 74 75 72  lback;.  int tur
1d8e9 6e 4f 6e 41 43 3b 0a 23 65 6e 64 69 66 20 2f 2a  nOnAC;.#endif /*
1d8ea 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1d8eb 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 72   moved into u.ar
1d8ec 20 2a 2f 0a 0a 20 20 75 2e 61 72 2e 64 65 73 69   */..  u.ar.desi
1d8ed 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 20 3d 20  redAutoCommit = 
1d8ee 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 72 2e  pOp->p1;.  u.ar.
1d8ef 69 52 6f 6c 6c 62 61 63 6b 20 3d 20 70 4f 70 2d  iRollback = pOp-
1d8f0 3e 70 32 3b 0a 20 20 75 2e 61 72 2e 74 75 72 6e  >p2;.  u.ar.turn
1d8f1 4f 6e 41 43 20 3d 20 75 2e 61 72 2e 64 65 73 69  OnAC = u.ar.desi
1d8f2 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 20 26 26  redAutoCommit &&
1d8f3 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74   !db->autoCommit
1d8f4 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 72  ;.  assert( u.ar
1d8f5 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d  .desiredAutoComm
1d8f6 69 74 3d 3d 31 20 7c 7c 20 75 2e 61 72 2e 64 65  it==1 || u.ar.de
1d8f7 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d  siredAutoCommit=
1d8f8 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1d8f9 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f  u.ar.desiredAuto
1d8fa 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20 75 2e 61  Commit==1 || u.a
1d8fb 72 2e 69 52 6f 6c 6c 62 61 63 6b 3d 3d 30 20 29  r.iRollback==0 )
1d8fc 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e  ;.  assert( db->
1d8fd 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e 30 20  activeVdbeCnt>0 
1d8fe 29 3b 20 20 2f 2a 20 41 74 20 6c 65 61 73 74 20  );  /* At least 
1d8ff 74 68 69 73 20 6f 6e 65 20 56 4d 20 69 73 20 61  this one VM is a
1d900 63 74 69 76 65 20 2a 2f 0a 0a 20 20 69 66 28 20  ctive */..  if( 
1d901 75 2e 61 72 2e 74 75 72 6e 4f 6e 41 43 20 26 26  u.ar.turnOnAC &&
1d902 20 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b 20   u.ar.iRollback 
1d903 26 26 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62  && db->activeVdb
1d904 65 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a  eCnt>1 ){.    /*
1d905 20 49 66 20 74 68 69 73 20 69 6e 73 74 72 75 63   If this instruc
1d906 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20  tion implements 
1d907 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64 20 6f  a ROLLBACK and o
1d908 74 68 65 72 20 56 4d 73 20 61 72 65 0a 20 20 20  ther VMs are.   
1d909 20 2a 2a 20 73 74 69 6c 6c 20 72 75 6e 6e 69 6e   ** still runnin
1d90a 67 2c 20 61 6e 64 20 61 20 74 72 61 6e 73 61 63  g, and a transac
1d90b 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2c 20  tion is active, 
1d90c 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  return an error 
1d90d 69 6e 64 69 63 61 74 69 6e 67 0a 20 20 20 20 2a  indicating.    *
1d90e 2a 20 74 68 61 74 20 74 68 65 20 6f 74 68 65 72  * that the other
1d90f 20 56 4d 73 20 6d 75 73 74 20 63 6f 6d 70 6c 65   VMs must comple
1d910 74 65 20 66 69 72 73 74 2e 0a 20 20 20 20 2a 2f  te first..    */
1d911 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
1d912 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
1d913 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74 20 72  g, db, "cannot r
1d914 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61 63 74  ollback transact
1d915 69 6f 6e 20 2d 20 22 0a 20 20 20 20 20 20 20 20  ion - ".        
1d916 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20  "SQL statements 
1d917 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b 0a 20  in progress");. 
1d918 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42     rc = SQLITE_B
1d919 55 53 59 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  USY;.  }else if(
1d91a 20 75 2e 61 72 2e 74 75 72 6e 4f 6e 41 43 20 26   u.ar.turnOnAC &
1d91b 26 20 21 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63  & !u.ar.iRollbac
1d91c 6b 20 26 26 20 64 62 2d 3e 77 72 69 74 65 56 64  k && db->writeVd
1d91d 62 65 43 6e 74 3e 30 20 29 7b 0a 20 20 20 20 2f  beCnt>0 ){.    /
1d91e 2a 20 49 66 20 74 68 69 73 20 69 6e 73 74 72 75  * If this instru
1d91f 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73  ction implements
1d920 20 61 20 43 4f 4d 4d 49 54 20 61 6e 64 20 6f 74   a COMMIT and ot
1d921 68 65 72 20 56 4d 73 20 61 72 65 20 77 72 69 74  her VMs are writ
1d922 69 6e 67 0a 20 20 20 20 2a 2a 20 72 65 74 75 72  ing.    ** retur
1d923 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e 64 69 63  n an error indic
1d924 61 74 69 6e 67 20 74 68 61 74 20 74 68 65 20 6f  ating that the o
1d925 74 68 65 72 20 56 4d 73 20 6d 75 73 74 20 63 6f  ther VMs must co
1d926 6d 70 6c 65 74 65 20 66 69 72 73 74 2e 0a 20 20  mplete first..  
1d927 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
1d928 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
1d929 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e  rrMsg, db, "cann
1d92a 6f 74 20 63 6f 6d 6d 69 74 20 74 72 61 6e 73 61  ot commit transa
1d92b 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20 20 20 20  ction - ".      
1d92c 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74    "SQL statement
1d92d 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b  s in progress");
1d92e 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1d92f 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 20 69  _BUSY;.  }else i
1d930 66 28 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41  f( u.ar.desiredA
1d931 75 74 6f 43 6f 6d 6d 69 74 21 3d 64 62 2d 3e 61  utoCommit!=db->a
1d932 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20  utoCommit ){.   
1d933 20 69 66 28 20 75 2e 61 72 2e 69 52 6f 6c 6c 62   if( u.ar.iRollb
1d934 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ack ){.      ass
1d935 65 72 74 28 20 75 2e 61 72 2e 64 65 73 69 72 65  ert( u.ar.desire
1d936 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 29  dAutoCommit==1 )
1d937 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
1d938 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a  ollbackAll(db);.
1d939 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f        db->autoCo
1d93a 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 7d 65  mmit = 1;.    }e
1d93b 6c 73 65 20 69 66 28 20 28 72 63 20 3d 20 73 71  lse if( (rc = sq
1d93c 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46 6b  lite3VdbeCheckFk
1d93d 28 70 2c 20 31 29 29 21 3d 53 51 4c 49 54 45 5f  (p, 1))!=SQLITE_
1d93e 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  OK ){.      goto
1d93f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20   vdbe_return;.  
1d940 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64    }else{.      d
1d941 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20  b->autoCommit = 
1d942 28 75 38 29 75 2e 61 72 2e 64 65 73 69 72 65 64  (u8)u.ar.desired
1d943 41 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 20 20  AutoCommit;.    
1d944 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
1d945 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c 49 54 45  eHalt(p)==SQLITE
1d946 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20  _BUSY ){.       
1d947 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20   p->pc = pc;.   
1d948 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d       db->autoCom
1d949 6d 69 74 20 3d 20 28 75 38 29 28 31 2d 75 2e 61  mit = (u8)(1-u.a
1d94a 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d  r.desiredAutoCom
1d94b 6d 69 74 29 3b 0a 20 20 20 20 20 20 20 20 70 2d  mit);.        p-
1d94c 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54  >rc = rc = SQLIT
1d94d 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20  E_BUSY;.        
1d94e 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e  goto vdbe_return
1d94f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1d950 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
1d951 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20 29 3b  nStatement==0 );
1d952 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73  .    sqlite3Clos
1d953 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b  eSavepoints(db);
1d954 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d  .    if( p->rc==
1d955 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1d956 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44     rc = SQLITE_D
1d957 4f 4e 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ONE;.    }else{.
1d958 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1d959 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20  E_ERROR;.    }. 
1d95a 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74     goto vdbe_ret
1d95b 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  urn;.  }else{.  
1d95c 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1d95d 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
1d95e 64 62 2c 0a 20 20 20 20 20 20 20 20 28 21 75 2e  db,.        (!u.
1d95f 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f  ar.desiredAutoCo
1d960 6d 6d 69 74 29 3f 22 63 61 6e 6e 6f 74 20 73 74  mmit)?"cannot st
1d961 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f  art a transactio
1d962 6e 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73  n within a trans
1d963 61 63 74 69 6f 6e 22 3a 28 0a 20 20 20 20 20 20  action":(.      
1d964 20 20 28 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63    (u.ar.iRollbac
1d965 6b 29 3f 22 63 61 6e 6e 6f 74 20 72 6f 6c 6c 62  k)?"cannot rollb
1d966 61 63 6b 20 2d 20 6e 6f 20 74 72 61 6e 73 61 63  ack - no transac
1d967 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 22 3a  tion is active":
1d968 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1d969 20 20 20 20 22 63 61 6e 6e 6f 74 20 63 6f 6d 6d      "cannot comm
1d96a 69 74 20 2d 20 6e 6f 20 74 72 61 6e 73 61 63 74  it - no transact
1d96b 69 6f 6e 20 69 73 20 61 63 74 69 76 65 22 29 29  ion is active"))
1d96c 3b 0a 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ;..    rc = SQLI
1d96d 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
1d96e 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1d96f 6f 64 65 3a 20 54 72 61 6e 73 61 63 74 69 6f 6e  ode: Transaction
1d970 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1d971 2a 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73  ** Begin a trans
1d972 61 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 72 61  action.  The tra
1d973 6e 73 61 63 74 69 6f 6e 20 65 6e 64 73 20 77 68  nsaction ends wh
1d974 65 6e 20 61 20 43 6f 6d 6d 69 74 20 6f 72 20 52  en a Commit or R
1d975 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 70 63 6f 64  ollback.** opcod
1d976 65 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  e is encountered
1d977 2e 20 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20  .  Depending on 
1d978 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20  the ON CONFLICT 
1d979 73 65 74 74 69 6e 67 2c 20 74 68 65 0a 2a 2a 20  setting, the.** 
1d97a 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 69 67 68  transaction migh
1d97b 74 20 61 6c 73 6f 20 62 65 20 72 6f 6c 6c 65 64  t also be rolled
1d97c 20 62 61 63 6b 20 69 66 20 61 6e 20 65 72 72 6f   back if an erro
1d97d 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  r is encountered
1d97e 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68  ..**.** P1 is th
1d97f 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64  e index of the d
1d980 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20  atabase file on 
1d981 77 68 69 63 68 20 74 68 65 20 74 72 61 6e 73 61  which the transa
1d982 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 73 74 61 72  ction is.** star
1d983 74 65 64 2e 20 20 49 6e 64 65 78 20 30 20 69 73  ted.  Index 0 is
1d984 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
1d985 73 65 20 66 69 6c 65 20 61 6e 64 20 69 6e 64 65  se file and inde
1d986 78 20 31 20 69 73 20 74 68 65 0a 2a 2a 20 66 69  x 1 is the.** fi
1d987 6c 65 20 75 73 65 64 20 66 6f 72 20 74 65 6d 70  le used for temp
1d988 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 49  orary tables.  I
1d989 6e 64 69 63 65 73 20 6f 66 20 32 20 6f 72 20 6d  ndices of 2 or m
1d98a 6f 72 65 20 61 72 65 20 75 73 65 64 20 66 6f 72  ore are used for
1d98b 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 64 61 74  .** attached dat
1d98c 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  abases..**.** If
1d98d 20 50 32 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c   P2 is non-zero,
1d98e 20 74 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72   then a write-tr
1d98f 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61  ansaction is sta
1d990 72 74 65 64 2e 20 20 41 20 52 45 53 45 52 56 45  rted.  A RESERVE
1d991 44 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f 62 74  D lock is.** obt
1d992 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74  ained on the dat
1d993 61 62 61 73 65 20 66 69 6c 65 20 77 68 65 6e 20  abase file when 
1d994 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  a write-transact
1d995 69 6f 6e 20 69 73 20 73 74 61 72 74 65 64 2e 20  ion is started. 
1d996 20 4e 6f 0a 2a 2a 20 6f 74 68 65 72 20 70 72 6f   No.** other pro
1d997 63 65 73 73 20 63 61 6e 20 73 74 61 72 74 20 61  cess can start a
1d998 6e 6f 74 68 65 72 20 77 72 69 74 65 20 74 72 61  nother write tra
1d999 6e 73 61 63 74 69 6f 6e 20 77 68 69 6c 65 20 74  nsaction while t
1d99a 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  his transaction 
1d99b 69 73 0a 2a 2a 20 75 6e 64 65 72 77 61 79 2e 20  is.** underway. 
1d99c 20 53 74 61 72 74 69 6e 67 20 61 20 77 72 69 74   Starting a writ
1d99d 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6c  e transaction al
1d99e 73 6f 20 63 72 65 61 74 65 73 20 61 20 72 6f 6c  so creates a rol
1d99f 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 41  lback journal. A
1d9a0 0a 2a 2a 20 77 72 69 74 65 20 74 72 61 6e 73 61  .** write transa
1d9a1 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74  ction must be st
1d9a2 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 6e 79  arted before any
1d9a3 20 63 68 61 6e 67 65 73 20 63 61 6e 20 62 65 20   changes can be 
1d9a4 6d 61 64 65 20 74 6f 20 74 68 65 0a 2a 2a 20 64  made to the.** d
1d9a5 61 74 61 62 61 73 65 2e 20 20 49 66 20 50 32 20  atabase.  If P2 
1d9a6 69 73 20 32 20 6f 72 20 67 72 65 61 74 65 72 20  is 2 or greater 
1d9a7 74 68 65 6e 20 61 6e 20 45 58 43 4c 55 53 49 56  then an EXCLUSIV
1d9a8 45 20 6c 6f 63 6b 20 69 73 20 61 6c 73 6f 20 6f  E lock is also o
1d9a9 62 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68  btained.** on th
1d9aa 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  e file..**.** If
1d9ab 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63   a write-transac
1d9ac 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65 64 20  tion is started 
1d9ad 61 6e 64 20 74 68 65 20 56 64 62 65 2e 75 73 65  and the Vdbe.use
1d9ae 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 66 6c 61  sStmtJournal fla
1d9af 67 20 69 73 0a 2a 2a 20 74 72 75 65 20 28 74 68  g is.** true (th
1d9b0 69 73 20 66 6c 61 67 20 69 73 20 73 65 74 20 69  is flag is set i
1d9b1 66 20 74 68 65 20 56 64 62 65 20 6d 61 79 20 6d  f the Vdbe may m
1d9b2 6f 64 69 66 79 20 6d 6f 72 65 20 74 68 61 6e 20  odify more than 
1d9b3 6f 6e 65 20 72 6f 77 20 61 6e 64 20 6d 61 79 0a  one row and may.
1d9b4 2a 2a 20 74 68 72 6f 77 20 61 6e 20 41 42 4f 52  ** throw an ABOR
1d9b5 54 20 65 78 63 65 70 74 69 6f 6e 29 2c 20 61 20  T exception), a 
1d9b6 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61  statement transa
1d9b7 63 74 69 6f 6e 20 6d 61 79 20 61 6c 73 6f 20 62  ction may also b
1d9b8 65 20 6f 70 65 6e 65 64 2e 0a 2a 2a 20 4d 6f 72  e opened..** Mor
1d9b9 65 20 73 70 65 63 69 66 69 63 61 6c 6c 79 2c 20  e specifically, 
1d9ba 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  a statement tran
1d9bb 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65  saction is opene
1d9bc 64 20 69 66 66 20 74 68 65 20 64 61 74 61 62 61  d iff the databa
1d9bd 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  se.** connection
1d9be 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 6e 6f   is currently no
1d9bf 74 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20  t in autocommit 
1d9c0 6d 6f 64 65 2c 20 6f 72 20 69 66 20 74 68 65 72  mode, or if ther
1d9c1 65 20 61 72 65 20 6f 74 68 65 72 0a 2a 2a 20 61  e are other.** a
1d9c2 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73  ctive statements
1d9c3 2e 20 41 20 73 74 61 74 65 6d 65 6e 74 20 74 72  . A statement tr
1d9c4 61 6e 73 61 63 74 69 6f 6e 20 61 6c 6c 6f 77 73  ansaction allows
1d9c5 20 74 68 65 20 61 66 66 65 63 74 73 20 6f 66 20   the affects of 
1d9c6 74 68 69 73 0a 2a 2a 20 56 44 42 45 20 74 6f 20  this.** VDBE to 
1d9c7 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61  be rolled back a
1d9c8 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20 77 69  fter an error wi
1d9c9 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20  thout having to 
1d9ca 72 6f 6c 6c 20 62 61 63 6b 20 74 68 65 0a 2a 2a  roll back the.**
1d9cb 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74   entire transact
1d9cc 69 6f 6e 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72  ion. If no error
1d9cd 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c   is encountered,
1d9ce 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74   the statement t
1d9cf 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77 69  ransaction.** wi
1d9d0 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ll automatically
1d9d1 20 63 6f 6d 6d 69 74 20 77 68 65 6e 20 74 68 65   commit when the
1d9d2 20 56 44 42 45 20 68 61 6c 74 73 2e 0a 2a 2a 0a   VDBE halts..**.
1d9d3 2a 2a 20 49 66 20 50 32 20 69 73 20 7a 65 72 6f  ** If P2 is zero
1d9d4 2c 20 74 68 65 6e 20 61 20 72 65 61 64 2d 6c 6f  , then a read-lo
1d9d5 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 6f  ck is obtained o
1d9d6 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
1d9d7 69 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ile..*/.case OP_
1d9d8 54 72 61 6e 73 61 63 74 69 6f 6e 3a 20 7b 0a 23  Transaction: {.#
1d9d9 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1d9da 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1d9db 6e 74 6f 20 75 2e 61 73 20 2a 2f 0a 20 20 42 74  nto u.as */.  Bt
1d9dc 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66  ree *pBt;.#endif
1d9dd 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1d9de 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1d9df 2e 61 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  .as */..  assert
1d9e0 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1d9e1 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20  pOp->p1<db->nDb 
1d9e2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d  );.  assert( (p-
1d9e3 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c  >btreeMask & (1<
1d9e4 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b  <pOp->p1))!=0 );
1d9e5 0a 20 20 75 2e 61 73 2e 70 42 74 20 3d 20 64 62  .  u.as.pBt = db
1d9e6 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70  ->aDb[pOp->p1].p
1d9e7 42 74 3b 0a 0a 20 20 69 66 28 20 75 2e 61 73 2e  Bt;..  if( u.as.
1d9e8 70 42 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  pBt ){.    rc = 
1d9e9 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69  sqlite3BtreeBegi
1d9ea 6e 54 72 61 6e 73 28 75 2e 61 73 2e 70 42 74 2c  nTrans(u.as.pBt,
1d9eb 20 70 4f 70 2d 3e 70 32 29 3b 0a 20 20 20 20 69   pOp->p2);.    i
1d9ec 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55  f( rc==SQLITE_BU
1d9ed 53 59 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70  SY ){.      p->p
1d9ee 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20 70 2d  c = pc;.      p-
1d9ef 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54  >rc = rc = SQLIT
1d9f0 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 67 6f  E_BUSY;.      go
1d9f1 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a  to vdbe_return;.
1d9f2 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
1d9f3 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1d9f4 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f       goto abort_
1d9f5 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1d9f6 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 4f 70    }..    if( pOp
1d9f7 2d 3e 70 32 20 26 26 20 70 2d 3e 75 73 65 73 53  ->p2 && p->usesS
1d9f8 74 6d 74 4a 6f 75 72 6e 61 6c 0a 20 20 20 20 20  tmtJournal.     
1d9f9 26 26 20 28 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  && (db->autoComm
1d9fa 69 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 63 74  it==0 || db->act
1d9fb 69 76 65 56 64 62 65 43 6e 74 3e 31 29 0a 20 20  iveVdbeCnt>1).  
1d9fc 20 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72    ){.      asser
1d9fd 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  t( sqlite3BtreeI
1d9fe 73 49 6e 54 72 61 6e 73 28 75 2e 61 73 2e 70 42  sInTrans(u.as.pB
1d9ff 74 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  t) );.      if( 
1da00 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 30  p->iStatement==0
1da01 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
1da02 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65  rt( db->nStateme
1da03 6e 74 3e 3d 30 20 26 26 20 64 62 2d 3e 6e 53 61  nt>=0 && db->nSa
1da04 76 65 70 6f 69 6e 74 3e 3d 30 20 29 3b 0a 20 20  vepoint>=0 );.  
1da05 20 20 20 20 20 20 64 62 2d 3e 6e 53 74 61 74 65        db->nState
1da06 6d 65 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20  ment++;.        
1da07 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20  p->iStatement = 
1da08 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 2b  db->nSavepoint +
1da09 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3b   db->nStatement;
1da0a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
1da0b 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1da0c 42 65 67 69 6e 53 74 6d 74 28 75 2e 61 73 2e 70  BeginStmt(u.as.p
1da0d 42 74 2c 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e  Bt, p->iStatemen
1da0e 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 53 74  t);..      /* St
1da0f 6f 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ore the current 
1da10 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74  value of the dat
1da11 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20 64 65  abase handles de
1da12 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e  ferred constrain
1da13 74 0a 20 20 20 20 20 20 2a 2a 20 63 6f 75 6e 74  t.      ** count
1da14 65 72 2e 20 49 66 20 74 68 65 20 73 74 61 74 65  er. If the state
1da15 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
1da16 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 6f 6c   needs to be rol
1da17 6c 65 64 20 62 61 63 6b 2c 0a 20 20 20 20 20 20  led back,.      
1da18 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
1da19 74 68 69 73 20 63 6f 75 6e 74 65 72 20 6e 65 65  this counter nee
1da1a 64 73 20 74 6f 20 62 65 20 72 65 73 74 6f 72 65  ds to be restore
1da1b 64 20 74 6f 6f 2e 20 20 2a 2f 0a 20 20 20 20 20  d too.  */.     
1da1c 20 70 2d 3e 6e 53 74 6d 74 44 65 66 43 6f 6e 73   p->nStmtDefCons
1da1d 20 3d 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64   = db->nDeferred
1da1e 43 6f 6e 73 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Cons;.    }.  }.
1da1f 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1da20 70 63 6f 64 65 3a 20 52 65 61 64 43 6f 6f 6b 69  pcode: ReadCooki
1da21 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  e P1 P2 P3 * *.*
1da22 2a 0a 2a 2a 20 52 65 61 64 20 63 6f 6f 6b 69 65  *.** Read cookie
1da23 20 6e 75 6d 62 65 72 20 50 33 20 66 72 6f 6d 20   number P3 from 
1da24 64 61 74 61 62 61 73 65 20 50 31 20 61 6e 64 20  database P1 and 
1da25 77 72 69 74 65 20 69 74 20 69 6e 74 6f 20 72 65  write it into re
1da26 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20 50 33  gister P2..** P3
1da27 3d 3d 31 20 69 73 20 74 68 65 20 73 63 68 65 6d  ==1 is the schem
1da28 61 20 76 65 72 73 69 6f 6e 2e 20 20 50 33 3d 3d  a version.  P3==
1da29 32 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73  2 is the databas
1da2a 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20 50 33 3d  e format..** P3=
1da2b 3d 33 20 69 73 20 74 68 65 20 72 65 63 6f 6d 6d  =3 is the recomm
1da2c 65 6e 64 65 64 20 70 61 67 65 72 20 63 61 63 68  ended pager cach
1da2d 65 20 73 69 7a 65 2c 20 61 6e 64 20 73 6f 20 66  e size, and so f
1da2e 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20 69 73 0a  orth.  P1==0 is.
1da2f 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  ** the main data
1da30 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 50 31  base file and P1
1da31 3d 3d 31 20 69 73 20 74 68 65 20 64 61 74 61 62  ==1 is the datab
1da32 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 74 6f  ase file used to
1da33 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d 70 6f 72   store.** tempor
1da34 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a  ary tables..**.*
1da35 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20  * There must be 
1da36 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74  a read-lock on t
1da37 68 65 20 64 61 74 61 62 61 73 65 20 28 65 69 74  he database (eit
1da38 68 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f  her a transactio
1da39 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 73 74 61  n.** must be sta
1da3a 72 74 65 64 20 6f 72 20 74 68 65 72 65 20 6d 75  rted or there mu
1da3b 73 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 63 75  st be an open cu
1da3c 72 73 6f 72 29 20 62 65 66 6f 72 65 0a 2a 2a 20  rsor) before.** 
1da3d 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20 69  executing this i
1da3e 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63  nstruction..*/.c
1da3f 61 73 65 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69  ase OP_ReadCooki
1da40 65 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  e: {            
1da41 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
1da42 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20  lease */.#if 0  
1da43 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1da44 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1da45 61 74 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65 74  at */.  int iMet
1da46 61 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20  a;.  int iDb;.  
1da47 69 6e 74 20 69 43 6f 6f 6b 69 65 3b 0a 23 65 6e  int iCookie;.#en
1da48 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1da49 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1da4a 6f 20 75 2e 61 74 20 2a 2f 0a 0a 20 20 75 2e 61  o u.at */..  u.a
1da4b 74 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b  t.iDb = pOp->p1;
1da4c 0a 20 20 75 2e 61 74 2e 69 43 6f 6f 6b 69 65 20  .  u.at.iCookie 
1da4d 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73  = pOp->p3;.  ass
1da4e 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 53 51 4c  ert( pOp->p3<SQL
1da4f 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45 54 41  ITE_N_BTREE_META
1da50 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e   );.  assert( u.
1da51 61 74 2e 69 44 62 3e 3d 30 20 26 26 20 75 2e 61  at.iDb>=0 && u.a
1da52 74 2e 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b  t.iDb<db->nDb );
1da53 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61  .  assert( db->a
1da54 44 62 5b 75 2e 61 74 2e 69 44 62 5d 2e 70 42 74  Db[u.at.iDb].pBt
1da55 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
1da56 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26   (p->btreeMask &
1da57 20 28 31 3c 3c 75 2e 61 74 2e 69 44 62 29 29 21   (1<<u.at.iDb))!
1da58 3d 30 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33  =0 );..  sqlite3
1da59 42 74 72 65 65 47 65 74 4d 65 74 61 28 64 62 2d  BtreeGetMeta(db-
1da5a 3e 61 44 62 5b 75 2e 61 74 2e 69 44 62 5d 2e 70  >aDb[u.at.iDb].p
1da5b 42 74 2c 20 75 2e 61 74 2e 69 43 6f 6f 6b 69 65  Bt, u.at.iCookie
1da5c 2c 20 28 75 33 32 20 2a 29 26 75 2e 61 74 2e 69  , (u32 *)&u.at.i
1da5d 4d 65 74 61 29 3b 0a 20 20 70 4f 75 74 2d 3e 75  Meta);.  pOut->u
1da5e 2e 69 20 3d 20 75 2e 61 74 2e 69 4d 65 74 61 3b  .i = u.at.iMeta;
1da5f 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  .  MemSetTypeFla
1da60 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29  g(pOut, MEM_Int)
1da61 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1da62 20 4f 70 63 6f 64 65 3a 20 53 65 74 43 6f 6f 6b   Opcode: SetCook
1da63 69 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  ie P1 P2 P3 * *.
1da64 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20  **.** Write the 
1da65 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73  content of regis
1da66 74 65 72 20 50 33 20 28 69 6e 74 65 72 70 72 65  ter P3 (interpre
1da67 74 65 64 20 61 73 20 61 6e 20 69 6e 74 65 67 65  ted as an intege
1da68 72 29 0a 2a 2a 20 69 6e 74 6f 20 63 6f 6f 6b 69  r).** into cooki
1da69 65 20 6e 75 6d 62 65 72 20 50 32 20 6f 66 20 64  e number P2 of d
1da6a 61 74 61 62 61 73 65 20 50 31 2e 20 20 50 32 3d  atabase P1.  P2=
1da6b 3d 31 20 69 73 20 74 68 65 20 73 63 68 65 6d 61  =1 is the schema
1da6c 20 76 65 72 73 69 6f 6e 2e 20 20 0a 2a 2a 20 50   version.  .** P
1da6d 32 3d 3d 32 20 69 73 20 74 68 65 20 64 61 74 61  2==2 is the data
1da6e 62 61 73 65 20 66 6f 72 6d 61 74 2e 20 50 32 3d  base format. P2=
1da6f 3d 33 20 69 73 20 74 68 65 20 72 65 63 6f 6d 6d  =3 is the recomm
1da70 65 6e 64 65 64 20 70 61 67 65 72 20 63 61 63 68  ended pager cach
1da71 65 20 0a 2a 2a 20 73 69 7a 65 2c 20 61 6e 64 20  e .** size, and 
1da72 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30  so forth.  P1==0
1da73 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64 61 74   is the main dat
1da74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 50  abase file and P
1da75 31 3d 3d 31 20 69 73 20 74 68 65 20 0a 2a 2a 20  1==1 is the .** 
1da76 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73  database file us
1da77 65 64 20 74 6f 20 73 74 6f 72 65 20 74 65 6d 70  ed to store temp
1da78 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a 2a  orary tables..**
1da79 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63 74 69 6f  .** A transactio
1da7a 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 74 65  n must be starte
1da7b 64 20 62 65 66 6f 72 65 20 65 78 65 63 75 74 69  d before executi
1da7c 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a  ng this opcode..
1da7d 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 74 43 6f  */.case OP_SetCo
1da7e 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 2f 2a  okie: {       /*
1da7f 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f   in3 */.#if 0  /
1da80 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1da81 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1da82 75 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b 0a  u */.  Db *pDb;.
1da83 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1da84 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1da85 69 6e 74 6f 20 75 2e 61 75 20 2a 2f 0a 20 20 61  into u.au */.  a
1da86 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 53  ssert( pOp->p2<S
1da87 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45  QLITE_N_BTREE_ME
1da88 54 41 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  TA );.  assert( 
1da89 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f  pOp->p1>=0 && pO
1da8a 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b  p->p1<db->nDb );
1da8b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62  .  assert( (p->b
1da8c 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70  treeMask & (1<<p
1da8d 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20  Op->p1))!=0 );. 
1da8e 20 75 2e 61 75 2e 70 44 62 20 3d 20 26 64 62 2d   u.au.pDb = &db-
1da8f 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  >aDb[pOp->p1];. 
1da90 20 61 73 73 65 72 74 28 20 75 2e 61 75 2e 70 44   assert( u.au.pD
1da91 62 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 73  b->pBt!=0 );.  s
1da92 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74  qlite3VdbeMemInt
1da93 65 67 65 72 69 66 79 28 70 49 6e 33 29 3b 0a 20  egerify(pIn3);. 
1da94 20 2f 2a 20 53 65 65 20 6e 6f 74 65 20 61 62 6f   /* See note abo
1da95 75 74 20 69 6e 64 65 78 20 73 68 69 66 74 69 6e  ut index shiftin
1da96 67 20 6f 6e 20 4f 50 5f 52 65 61 64 43 6f 6f 6b  g on OP_ReadCook
1da97 69 65 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  ie */.  rc = sql
1da98 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d  ite3BtreeUpdateM
1da99 65 74 61 28 75 2e 61 75 2e 70 44 62 2d 3e 70 42  eta(u.au.pDb->pB
1da9a 74 2c 20 70 4f 70 2d 3e 70 32 2c 20 28 69 6e 74  t, pOp->p2, (int
1da9b 29 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 69  )pIn3->u.i);.  i
1da9c 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 42 54 52 45  f( pOp->p2==BTRE
1da9d 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e  E_SCHEMA_VERSION
1da9e 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20   ){.    /* When 
1da9f 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  the schema cooki
1daa0 65 20 63 68 61 6e 67 65 73 2c 20 72 65 63 6f 72  e changes, recor
1daa1 64 20 74 68 65 20 6e 65 77 20 63 6f 6f 6b 69 65  d the new cookie
1daa2 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20   internally */. 
1daa3 20 20 20 75 2e 61 75 2e 70 44 62 2d 3e 70 53 63     u.au.pDb->pSc
1daa4 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f  hema->schema_coo
1daa5 6b 69 65 20 3d 20 28 69 6e 74 29 70 49 6e 33 2d  kie = (int)pIn3-
1daa6 3e 75 2e 69 3b 0a 20 20 20 20 64 62 2d 3e 66 6c  >u.i;.    db->fl
1daa7 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e  ags |= SQLITE_In
1daa8 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 20 7d  ternChanges;.  }
1daa9 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 32  else if( pOp->p2
1daaa 3d 3d 42 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52  ==BTREE_FILE_FOR
1daab 4d 41 54 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65  MAT ){.    /* Re
1daac 63 6f 72 64 20 63 68 61 6e 67 65 73 20 69 6e 20  cord changes in 
1daad 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20  the file format 
1daae 2a 2f 0a 20 20 20 20 75 2e 61 75 2e 70 44 62 2d  */.    u.au.pDb-
1daaf 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66  >pSchema->file_f
1dab0 6f 72 6d 61 74 20 3d 20 28 75 38 29 70 49 6e 33  ormat = (u8)pIn3
1dab1 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 69 66 28  ->u.i;.  }.  if(
1dab2 20 70 4f 70 2d 3e 70 31 3d 3d 31 20 29 7b 0a 20   pOp->p1==1 ){. 
1dab3 20 20 20 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65     /* Invalidate
1dab4 20 61 6c 6c 20 70 72 65 70 61 72 65 64 20 73 74   all prepared st
1dab5 61 74 65 6d 65 6e 74 73 20 77 68 65 6e 65 76 65  atements wheneve
1dab6 72 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62  r the TEMP datab
1dab7 61 73 65 0a 20 20 20 20 2a 2a 20 73 63 68 65 6d  ase.    ** schem
1dab8 61 20 69 73 20 63 68 61 6e 67 65 64 2e 20 20 54  a is changed.  T
1dab9 69 63 6b 65 74 20 23 31 36 34 34 20 2a 2f 0a 20  icket #1644 */. 
1daba 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65     sqlite3Expire
1dabb 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
1dabc 74 73 28 64 62 29 3b 0a 20 20 7d 0a 20 20 62 72  ts(db);.  }.  br
1dabd 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1dabe 65 3a 20 56 65 72 69 66 79 43 6f 6f 6b 69 65 20  e: VerifyCookie 
1dabf 50 31 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 43 68  P1 P2 *.**.** Ch
1dac0 65 63 6b 20 74 68 65 20 76 61 6c 75 65 20 6f 66  eck the value of
1dac1 20 67 6c 6f 62 61 6c 20 64 61 74 61 62 61 73 65   global database
1dac2 20 70 61 72 61 6d 65 74 65 72 20 6e 75 6d 62 65   parameter numbe
1dac3 72 20 30 20 28 74 68 65 0a 2a 2a 20 73 63 68 65  r 0 (the.** sche
1dac4 6d 61 20 76 65 72 73 69 6f 6e 29 20 61 6e 64 20  ma version) and 
1dac5 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20  make sure it is 
1dac6 65 71 75 61 6c 20 74 6f 20 50 32 2e 20 20 0a 2a  equal to P2.  .*
1dac7 2a 20 50 31 20 69 73 20 74 68 65 20 64 61 74 61  * P1 is the data
1dac8 62 61 73 65 20 6e 75 6d 62 65 72 20 77 68 69 63  base number whic
1dac9 68 20 69 73 20 30 20 66 6f 72 20 74 68 65 20 6d  h is 0 for the m
1daca 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c  ain database fil
1dacb 65 0a 2a 2a 20 61 6e 64 20 31 20 66 6f 72 20 74  e.** and 1 for t
1dacc 68 65 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20  he file holding 
1dacd 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
1dace 20 61 6e 64 20 73 6f 6d 65 20 68 69 67 68 65 72   and some higher
1dacf 20 6e 75 6d 62 65 72 0a 2a 2a 20 66 6f 72 20 61   number.** for a
1dad0 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73  uxiliary databas
1dad1 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  es..**.** The co
1dad2 6f 6b 69 65 20 63 68 61 6e 67 65 73 20 69 74 73  okie changes its
1dad3 20 76 61 6c 75 65 20 77 68 65 6e 65 76 65 72 20   value whenever 
1dad4 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
1dad5 65 6d 61 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 20  ema changes..** 
1dad6 54 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 69  This operation i
1dad7 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 63 74  s used to detect
1dad8 20 77 68 65 6e 20 74 68 61 74 20 74 68 65 20 63   when that the c
1dad9 6f 6f 6b 69 65 20 68 61 73 20 63 68 61 6e 67 65  ookie has change
1dada 64 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 68  d.** and that th
1dadb 65 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65 73  e current proces
1dadc 73 20 6e 65 65 64 73 20 74 6f 20 72 65 72 65 61  s needs to rerea
1dadd 64 20 74 68 65 20 73 63 68 65 6d 61 2e 0a 2a 2a  d the schema..**
1dade 0a 2a 2a 20 45 69 74 68 65 72 20 61 20 74 72 61  .** Either a tra
1dadf 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 73 20 74  nsaction needs t
1dae0 6f 20 68 61 76 65 20 62 65 65 6e 20 73 74 61 72  o have been star
1dae1 74 65 64 20 6f 72 20 61 6e 20 4f 50 5f 4f 70 65  ted or an OP_Ope
1dae2 6e 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65  n needs.** to be
1dae3 20 65 78 65 63 75 74 65 64 20 28 74 6f 20 65 73   executed (to es
1dae4 74 61 62 6c 69 73 68 20 61 20 72 65 61 64 20 6c  tablish a read l
1dae5 6f 63 6b 29 20 62 65 66 6f 72 65 20 74 68 69 73  ock) before this
1dae6 20 6f 70 63 6f 64 65 20 69 73 0a 2a 2a 20 69 6e   opcode is.** in
1dae7 76 6f 6b 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f  voked..*/.case O
1dae8 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 3a 20  P_VerifyCookie: 
1dae9 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
1daea 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1daeb 64 20 69 6e 74 6f 20 75 2e 61 76 20 2a 2f 0a 20  d into u.av */. 
1daec 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 42 74   int iMeta;.  Bt
1daed 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66  ree *pBt;.#endif
1daee 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1daef 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1daf0 2e 61 76 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  .av */.  assert(
1daf1 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
1daf2 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29  Op->p1<db->nDb )
1daf3 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e  ;.  assert( (p->
1daf4 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c  btreeMask & (1<<
1daf5 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a  pOp->p1))!=0 );.
1daf6 20 20 75 2e 61 76 2e 70 42 74 20 3d 20 64 62 2d    u.av.pBt = db-
1daf7 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42  >aDb[pOp->p1].pB
1daf8 74 3b 0a 20 20 69 66 28 20 75 2e 61 76 2e 70 42  t;.  if( u.av.pB
1daf9 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
1dafa 42 74 72 65 65 47 65 74 4d 65 74 61 28 75 2e 61  BtreeGetMeta(u.a
1dafb 76 2e 70 42 74 2c 20 42 54 52 45 45 5f 53 43 48  v.pBt, BTREE_SCH
1dafc 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 28 75 33  EMA_VERSION, (u3
1dafd 32 20 2a 29 26 75 2e 61 76 2e 69 4d 65 74 61 29  2 *)&u.av.iMeta)
1dafe 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75  ;.  }else{.    u
1daff 2e 61 76 2e 69 4d 65 74 61 20 3d 20 30 3b 0a 20  .av.iMeta = 0;. 
1db00 20 7d 0a 20 20 69 66 28 20 75 2e 61 76 2e 69 4d   }.  if( u.av.iM
1db01 65 74 61 21 3d 70 4f 70 2d 3e 70 32 20 29 7b 0a  eta!=pOp->p2 ){.
1db02 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1db03 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67  e(db, p->zErrMsg
1db04 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73  );.    p->zErrMs
1db05 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  g = sqlite3DbStr
1db06 44 75 70 28 64 62 2c 20 22 64 61 74 61 62 61 73  Dup(db, "databas
1db07 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 68 61  e schema has cha
1db08 6e 67 65 64 22 29 3b 0a 20 20 20 20 2f 2a 20 49  nged");.    /* I
1db09 66 20 74 68 65 20 73 63 68 65 6d 61 2d 63 6f 6f  f the schema-coo
1db0a 6b 69 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74  kie from the dat
1db0b 61 62 61 73 65 20 66 69 6c 65 20 6d 61 74 63 68  abase file match
1db0c 65 73 20 74 68 65 20 63 6f 6f 6b 69 65 0a 20 20  es the cookie.  
1db0d 20 20 2a 2a 20 73 74 6f 72 65 64 20 77 69 74 68    ** stored with
1db0e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72   the in-memory r
1db0f 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
1db10 20 74 68 65 20 73 63 68 65 6d 61 2c 20 64 6f 0a   the schema, do.
1db11 20 20 20 20 2a 2a 20 6e 6f 74 20 72 65 6c 6f 61      ** not reloa
1db12 64 20 74 68 65 20 73 63 68 65 6d 61 20 66 72 6f  d the schema fro
1db13 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  m the database f
1db14 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ile..    **.    
1db15 2a 2a 20 49 66 20 76 69 72 74 75 61 6c 2d 74 61  ** If virtual-ta
1db16 62 6c 65 73 20 61 72 65 20 69 6e 20 75 73 65 2c  bles are in use,
1db17 20 74 68 69 73 20 69 73 20 6e 6f 74 20 6a 75 73   this is not jus
1db18 74 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  t an optimizatio
1db19 6e 2e 0a 20 20 20 20 2a 2a 20 4f 66 74 65 6e 2c  n..    ** Often,
1db1a 20 76 2d 74 61 62 6c 65 73 20 73 74 6f 72 65 20   v-tables store 
1db1b 74 68 65 69 72 20 64 61 74 61 20 69 6e 20 6f 74  their data in ot
1db1c 68 65 72 20 53 51 4c 69 74 65 20 74 61 62 6c 65  her SQLite table
1db1d 73 2c 20 77 68 69 63 68 0a 20 20 20 20 2a 2a 20  s, which.    ** 
1db1e 61 72 65 20 71 75 65 72 69 65 64 20 66 72 6f 6d  are queried from
1db1f 20 77 69 74 68 69 6e 20 78 4e 65 78 74 28 29 20   within xNext() 
1db20 61 6e 64 20 6f 74 68 65 72 20 76 2d 74 61 62 6c  and other v-tabl
1db21 65 20 6d 65 74 68 6f 64 73 20 75 73 69 6e 67 0a  e methods using.
1db22 20 20 20 20 2a 2a 20 70 72 65 70 61 72 65 64 20      ** prepared 
1db23 71 75 65 72 69 65 73 2e 20 49 66 20 73 75 63 68  queries. If such
1db24 20 61 20 71 75 65 72 79 20 69 73 20 6f 75 74 2d   a query is out-
1db25 6f 66 2d 64 61 74 65 2c 20 77 65 20 64 6f 20 6e  of-date, we do n
1db26 6f 74 20 77 61 6e 74 20 74 6f 0a 20 20 20 20 2a  ot want to.    *
1db27 2a 20 64 69 73 63 61 72 64 20 74 68 65 20 64 61  * discard the da
1db28 74 61 62 61 73 65 20 73 63 68 65 6d 61 2c 20 61  tabase schema, a
1db29 73 20 74 68 65 20 75 73 65 72 20 63 6f 64 65 20  s the user code 
1db2a 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65  implementing the
1db2b 0a 20 20 20 20 2a 2a 20 76 2d 74 61 62 6c 65 20  .    ** v-table 
1db2c 77 6f 75 6c 64 20 68 61 76 65 20 74 6f 20 62 65  would have to be
1db2d 20 72 65 61 64 79 20 66 6f 72 20 74 68 65 20 73   ready for the s
1db2e 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
1db2f 63 74 75 72 65 20 69 74 73 65 6c 66 0a 20 20 20  cture itself.   
1db30 20 2a 2a 20 74 6f 20 62 65 20 69 6e 76 61 6c 69   ** to be invali
1db31 64 61 74 65 64 20 77 68 65 6e 65 76 65 72 20 73  dated whenever s
1db32 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69 73  qlite3_step() is
1db33 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74   called from wit
1db34 68 69 6e 0a 20 20 20 20 2a 2a 20 61 20 76 2d 74  hin.    ** a v-t
1db35 61 62 6c 65 20 6d 65 74 68 6f 64 2e 0a 20 20 20  able method..   
1db36 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e   */.    if( db->
1db37 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 53 63  aDb[pOp->p1].pSc
1db38 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f  hema->schema_coo
1db39 6b 69 65 21 3d 75 2e 61 76 2e 69 4d 65 74 61 20  kie!=u.av.iMeta 
1db3a 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1db3b 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
1db3c 65 6d 61 28 64 62 2c 20 70 4f 70 2d 3e 70 31 29  ema(db, pOp->p1)
1db3d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c  ;.    }..    sql
1db3e 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72  ite3ExpirePrepar
1db3f 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29  edStatements(db)
1db40 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
1db41 45 5f 53 43 48 45 4d 41 3b 0a 20 20 7d 0a 20 20  E_SCHEMA;.  }.  
1db42 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1db43 6f 64 65 3a 20 4f 70 65 6e 52 65 61 64 20 50 31  ode: OpenRead P1
1db44 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a   P2 P3 P4 P5.**.
1db45 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61 64 2d 6f  ** Open a read-o
1db46 6e 6c 79 20 63 75 72 73 6f 72 20 66 6f 72 20 74  nly cursor for t
1db47 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  he database tabl
1db48 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67  e whose root pag
1db49 65 20 69 73 0a 2a 2a 20 50 32 20 69 6e 20 61 20  e is.** P2 in a 
1db4a 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
1db4b 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  The database fil
1db4c 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20  e is determined 
1db4d 62 79 20 50 33 2e 20 0a 2a 2a 20 50 33 3d 3d 30  by P3. .** P3==0
1db4e 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 20   means the main 
1db4f 64 61 74 61 62 61 73 65 2c 20 50 33 3d 3d 31 20  database, P3==1 
1db50 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 61  means the databa
1db51 73 65 20 75 73 65 64 20 66 6f 72 20 0a 2a 2a 20  se used for .** 
1db52 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73  temporary tables
1db53 2c 20 61 6e 64 20 50 33 3e 31 20 6d 65 61 6e 73  , and P3>1 means
1db54 20 75 73 65 64 20 74 68 65 20 63 6f 72 72 65 73   used the corres
1db55 70 6f 6e 64 69 6e 67 20 61 74 74 61 63 68 65 64  ponding attached
1db56 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 47  .** database.  G
1db57 69 76 65 20 74 68 65 20 6e 65 77 20 63 75 72 73  ive the new curs
1db58 6f 72 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72  or an identifier
1db59 20 6f 66 20 50 31 2e 20 20 54 68 65 20 50 31 0a   of P1.  The P1.
1db5a 2a 2a 20 76 61 6c 75 65 73 20 6e 65 65 64 20 6e  ** values need n
1db5b 6f 74 20 62 65 20 63 6f 6e 74 69 67 75 6f 75 73  ot be contiguous
1db5c 20 62 75 74 20 61 6c 6c 20 50 31 20 76 61 6c 75   but all P1 valu
1db5d 65 73 20 73 68 6f 75 6c 64 20 62 65 20 73 6d 61  es should be sma
1db5e 6c 6c 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20  ll integers..** 
1db5f 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 66  It is an error f
1db60 6f 72 20 50 31 20 74 6f 20 62 65 20 6e 65 67 61  or P1 to be nega
1db61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  tive..**.** If P
1db62 35 21 3d 30 20 74 68 65 6e 20 75 73 65 20 74 68  5!=0 then use th
1db63 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67  e content of reg
1db64 69 73 74 65 72 20 50 32 20 61 73 20 74 68 65 20  ister P2 as the 
1db65 72 6f 6f 74 20 70 61 67 65 2c 20 6e 6f 74 0a 2a  root page, not.*
1db66 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50  * the value of P
1db67 32 20 69 74 73 65 6c 66 2e 0a 2a 2a 0a 2a 2a 20  2 itself..**.** 
1db68 54 68 65 72 65 20 77 69 6c 6c 20 62 65 20 61 20  There will be a 
1db69 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  read lock on the
1db6a 20 64 61 74 61 62 61 73 65 20 77 68 65 6e 65 76   database whenev
1db6b 65 72 20 74 68 65 72 65 20 69 73 20 61 6e 0a 2a  er there is an.*
1db6c 2a 20 6f 70 65 6e 20 63 75 72 73 6f 72 2e 20 20  * open cursor.  
1db6d 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
1db6e 77 61 73 20 75 6e 6c 6f 63 6b 65 64 20 70 72 69  was unlocked pri
1db6f 6f 72 20 74 6f 20 74 68 69 73 20 69 6e 73 74 72  or to this instr
1db70 75 63 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 61  uction.** then a
1db71 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20 61 63   read lock is ac
1db72 71 75 69 72 65 64 20 61 73 20 70 61 72 74 20 6f  quired as part o
1db73 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  f this instructi
1db74 6f 6e 2e 20 20 41 20 72 65 61 64 0a 2a 2a 20 6c  on.  A read.** l
1db75 6f 63 6b 20 61 6c 6c 6f 77 73 20 6f 74 68 65 72  ock allows other
1db76 20 70 72 6f 63 65 73 73 65 73 20 74 6f 20 72 65   processes to re
1db77 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ad the database 
1db78 62 75 74 20 70 72 6f 68 69 62 69 74 73 0a 2a 2a  but prohibits.**
1db79 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65   any other proce
1db7a 73 73 20 66 72 6f 6d 20 6d 6f 64 69 66 79 69 6e  ss from modifyin
1db7b 67 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  g the database. 
1db7c 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 69   The read lock i
1db7d 73 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20 77 68  s.** released wh
1db7e 65 6e 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 61  en all cursors a
1db7f 72 65 20 63 6c 6f 73 65 64 2e 20 20 49 66 20 74  re closed.  If t
1db80 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1db81 61 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 67  attempts.** to g
1db82 65 74 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 62  et a read lock b
1db83 75 74 20 66 61 69 6c 73 2c 20 74 68 65 20 73 63  ut fails, the sc
1db84 72 69 70 74 20 74 65 72 6d 69 6e 61 74 65 73 20  ript terminates 
1db85 77 69 74 68 20 61 6e 0a 2a 2a 20 53 51 4c 49 54  with an.** SQLIT
1db86 45 5f 42 55 53 59 20 65 72 72 6f 72 20 63 6f 64  E_BUSY error cod
1db87 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20  e..**.** The P4 
1db88 76 61 6c 75 65 20 6d 61 79 20 62 65 20 65 69 74  value may be eit
1db89 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72 20 28  her an integer (
1db8a 50 34 5f 49 4e 54 33 32 29 20 6f 72 20 61 20 70  P4_INT32) or a p
1db8b 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 20 4b  ointer to.** a K
1db8c 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
1db8d 20 28 50 34 5f 4b 45 59 49 4e 46 4f 29 2e 20 49   (P4_KEYINFO). I
1db8e 66 20 69 74 20 69 73 20 61 20 70 6f 69 6e 74 65  f it is a pointe
1db8f 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 0a  r to a KeyInfo .
1db90 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74 68  ** structure, th
1db91 65 6e 20 73 61 69 64 20 73 74 72 75 63 74 75 72  en said structur
1db92 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 63 6f  e defines the co
1db93 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61 74  ntent and collat
1db94 69 6e 67 20 0a 2a 2a 20 73 65 71 75 65 6e 63 65  ing .** sequence
1db95 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 62 65   of the index be
1db96 69 6e 67 20 6f 70 65 6e 65 64 2e 20 4f 74 68 65  ing opened. Othe
1db97 72 77 69 73 65 2c 20 69 66 20 50 34 20 69 73 20  rwise, if P4 is 
1db98 61 6e 20 69 6e 74 65 67 65 72 20 0a 2a 2a 20 76  an integer .** v
1db99 61 6c 75 65 2c 20 69 74 20 69 73 20 73 65 74 20  alue, it is set 
1db9a 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
1db9b 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
1db9c 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  table..**.** See
1db9d 20 61 6c 73 6f 20 4f 70 65 6e 57 72 69 74 65 2e   also OpenWrite.
1db9e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f  .*/./* Opcode: O
1db9f 70 65 6e 57 72 69 74 65 20 50 31 20 50 32 20 50  penWrite P1 P2 P
1dba0 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70  3 P4 P5.**.** Op
1dba1 65 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65 20  en a read/write 
1dba2 63 75 72 73 6f 72 20 6e 61 6d 65 64 20 50 31 20  cursor named P1 
1dba3 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20  on the table or 
1dba4 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74  index whose root
1dba5 0a 2a 2a 20 70 61 67 65 20 69 73 20 50 32 2e 20  .** page is P2. 
1dba6 20 4f 72 20 69 66 20 50 35 21 3d 30 20 75 73 65   Or if P5!=0 use
1dba7 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
1dba8 72 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 66  register P2 to f
1dba9 69 6e 64 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20  ind the.** root 
1dbaa 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  page..**.** The 
1dbab 50 34 20 76 61 6c 75 65 20 6d 61 79 20 62 65 20  P4 value may be 
1dbac 65 69 74 68 65 72 20 61 6e 20 69 6e 74 65 67 65  either an intege
1dbad 72 20 28 50 34 5f 49 4e 54 33 32 29 20 6f 72 20  r (P4_INT32) or 
1dbae 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20  a pointer to.** 
1dbaf 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  a KeyInfo struct
1dbb0 75 72 65 20 28 50 34 5f 4b 45 59 49 4e 46 4f 29  ure (P4_KEYINFO)
1dbb1 2e 20 49 66 20 69 74 20 69 73 20 61 20 70 6f 69  . If it is a poi
1dbb2 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66  nter to a KeyInf
1dbb3 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c  o .** structure,
1dbb4 20 74 68 65 6e 20 73 61 69 64 20 73 74 72 75 63   then said struc
1dbb5 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 68 65  ture defines the
1dbb6 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c   content and col
1dbb7 6c 61 74 69 6e 67 20 0a 2a 2a 20 73 65 71 75 65  lating .** seque
1dbb8 6e 63 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78  nce of the index
1dbb9 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20 4f   being opened. O
1dbba 74 68 65 72 77 69 73 65 2c 20 69 66 20 50 34 20  therwise, if P4 
1dbbb 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 0a 2a  is an integer .*
1dbbc 2a 20 76 61 6c 75 65 2c 20 69 74 20 69 73 20 73  * value, it is s
1dbbd 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72  et to the number
1dbbe 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
1dbbf 68 65 20 74 61 62 6c 65 2c 20 6f 72 20 74 6f 20  he table, or to 
1dbc0 74 68 65 0a 2a 2a 20 6c 61 72 67 65 73 74 20 69  the.** largest i
1dbc1 6e 64 65 78 20 6f 66 20 61 6e 79 20 63 6f 6c 75  ndex of any colu
1dbc2 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  mn of the table 
1dbc3 74 68 61 74 20 69 73 20 61 63 74 75 61 6c 6c 79  that is actually
1dbc4 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   used..**.** Thi
1dbc5 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 6f  s instruction wo
1dbc6 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 4f 70  rks just like Op
1dbc7 65 6e 52 65 61 64 20 65 78 63 65 70 74 20 74 68  enRead except th
1dbc8 61 74 20 69 74 20 6f 70 65 6e 73 20 74 68 65 20  at it opens the 
1dbc9 63 75 72 73 6f 72 0a 2a 2a 20 69 6e 20 72 65 61  cursor.** in rea
1dbca 64 2f 77 72 69 74 65 20 6d 6f 64 65 2e 20 20 46  d/write mode.  F
1dbcb 6f 72 20 61 20 67 69 76 65 6e 20 74 61 62 6c 65  or a given table
1dbcc 2c 20 74 68 65 72 65 20 63 61 6e 20 62 65 20 6f  , there can be o
1dbcd 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65 61 64 2d  ne or more read-
1dbce 6f 6e 6c 79 0a 2a 2a 20 63 75 72 73 6f 72 73 20  only.** cursors 
1dbcf 6f 72 20 61 20 73 69 6e 67 6c 65 20 72 65 61 64  or a single read
1dbd0 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 62 75  /write cursor bu
1dbd1 74 20 6e 6f 74 20 62 6f 74 68 2e 0a 2a 2a 0a 2a  t not both..**.*
1dbd2 2a 20 53 65 65 20 61 6c 73 6f 20 4f 70 65 6e 52  * See also OpenR
1dbd3 65 61 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ead..*/.case OP_
1dbd4 4f 70 65 6e 52 65 61 64 3a 0a 63 61 73 65 20 4f  OpenRead:.case O
1dbd5 50 5f 4f 70 65 6e 57 72 69 74 65 3a 20 7b 0a 23  P_OpenWrite: {.#
1dbd6 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1dbd7 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1dbd8 6e 74 6f 20 75 2e 61 77 20 2a 2f 0a 20 20 69 6e  nto u.aw */.  in
1dbd9 74 20 6e 46 69 65 6c 64 3b 0a 20 20 4b 65 79 49  t nField;.  KeyI
1dbda 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20  nfo *pKeyInfo;. 
1dbdb 20 69 6e 74 20 70 32 3b 0a 20 20 69 6e 74 20 69   int p2;.  int i
1dbdc 44 62 3b 0a 20 20 69 6e 74 20 77 72 46 6c 61 67  Db;.  int wrFlag
1dbdd 3b 0a 20 20 42 74 72 65 65 20 2a 70 58 3b 0a 20  ;.  Btree *pX;. 
1dbde 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75   VdbeCursor *pCu
1dbdf 72 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 23 65  r;.  Db *pDb;.#e
1dbe0 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1dbe1 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1dbe2 74 6f 20 75 2e 61 77 20 2a 2f 0a 0a 20 20 75 2e  to u.aw */..  u.
1dbe3 61 77 2e 6e 46 69 65 6c 64 20 3d 20 30 3b 0a 20  aw.nField = 0;. 
1dbe4 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 20 3d   u.aw.pKeyInfo =
1dbe5 20 30 3b 0a 20 20 75 2e 61 77 2e 70 32 20 3d 20   0;.  u.aw.p2 = 
1dbe6 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 77 2e  pOp->p2;.  u.aw.
1dbe7 69 44 62 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20  iDb = pOp->p3;. 
1dbe8 20 61 73 73 65 72 74 28 20 75 2e 61 77 2e 69 44   assert( u.aw.iD
1dbe9 62 3e 3d 30 20 26 26 20 75 2e 61 77 2e 69 44 62  b>=0 && u.aw.iDb
1dbea 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73  <db->nDb );.  as
1dbeb 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d  sert( (p->btreeM
1dbec 61 73 6b 20 26 20 28 31 3c 3c 75 2e 61 77 2e 69  ask & (1<<u.aw.i
1dbed 44 62 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e 61  Db))!=0 );.  u.a
1dbee 77 2e 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62  w.pDb = &db->aDb
1dbef 5b 75 2e 61 77 2e 69 44 62 5d 3b 0a 20 20 75 2e  [u.aw.iDb];.  u.
1dbf0 61 77 2e 70 58 20 3d 20 75 2e 61 77 2e 70 44 62  aw.pX = u.aw.pDb
1dbf1 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28  ->pBt;.  assert(
1dbf2 20 75 2e 61 77 2e 70 58 21 3d 30 20 29 3b 0a 20   u.aw.pX!=0 );. 
1dbf3 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65   if( pOp->opcode
1dbf4 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 29  ==OP_OpenWrite )
1dbf5 7b 0a 20 20 20 20 75 2e 61 77 2e 77 72 46 6c 61  {.    u.aw.wrFla
1dbf6 67 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 75  g = 1;.    if( u
1dbf7 2e 61 77 2e 70 44 62 2d 3e 70 53 63 68 65 6d 61  .aw.pDb->pSchema
1dbf8 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3c 20  ->file_format < 
1dbf9 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46  p->minWriteFileF
1dbfa 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 20 20 70  ormat ){.      p
1dbfb 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f  ->minWriteFileFo
1dbfc 72 6d 61 74 20 3d 20 75 2e 61 77 2e 70 44 62 2d  rmat = u.aw.pDb-
1dbfd 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66  >pSchema->file_f
1dbfe 6f 72 6d 61 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  ormat;.    }.  }
1dbff 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 77 2e 77  else{.    u.aw.w
1dc00 72 46 6c 61 67 20 3d 20 30 3b 0a 20 20 7d 0a 20  rFlag = 0;.  }. 
1dc01 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 7b 0a   if( pOp->p5 ){.
1dc02 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 77      assert( u.aw
1dc03 2e 70 32 3e 30 20 29 3b 0a 20 20 20 20 61 73 73  .p2>0 );.    ass
1dc04 65 72 74 28 20 75 2e 61 77 2e 70 32 3c 3d 70 2d  ert( u.aw.p2<=p-
1dc05 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 70 49 6e  >nMem );.    pIn
1dc06 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61  2 = &p->aMem[u.a
1dc07 77 2e 70 32 5d 3b 0a 20 20 20 20 73 71 6c 69 74  w.p2];.    sqlit
1dc08 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72  e3VdbeMemInteger
1dc09 69 66 79 28 70 49 6e 32 29 3b 0a 20 20 20 20 75  ify(pIn2);.    u
1dc0a 2e 61 77 2e 70 32 20 3d 20 28 69 6e 74 29 70 49  .aw.p2 = (int)pI
1dc0b 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20 20 2f 2a 20  n2->u.i;.    /* 
1dc0c 54 68 65 20 75 2e 61 77 2e 70 32 20 76 61 6c 75  The u.aw.p2 valu
1dc0d 65 20 61 6c 77 61 79 73 20 63 6f 6d 65 73 20 66  e always comes f
1dc0e 72 6f 6d 20 61 20 70 72 69 6f 72 20 4f 50 5f 43  rom a prior OP_C
1dc0f 72 65 61 74 65 54 61 62 6c 65 20 6f 70 63 6f 64  reateTable opcod
1dc10 65 20 61 6e 64 0a 20 20 20 20 2a 2a 20 74 68 61  e and.    ** tha
1dc11 74 20 6f 70 63 6f 64 65 20 77 69 6c 6c 20 61 6c  t opcode will al
1dc12 77 61 79 73 20 73 65 74 20 74 68 65 20 75 2e 61  ways set the u.a
1dc13 77 2e 70 32 20 76 61 6c 75 65 20 74 6f 20 32 20  w.p2 value to 2 
1dc14 6f 72 20 6d 6f 72 65 20 6f 72 20 65 6c 73 65 20  or more or else 
1dc15 66 61 69 6c 2e 0a 20 20 20 20 2a 2a 20 49 66 20  fail..    ** If 
1dc16 74 68 65 72 65 20 77 65 72 65 20 61 20 66 61 69  there were a fai
1dc17 6c 75 72 65 2c 20 74 68 65 20 70 72 65 70 61 72  lure, the prepar
1dc18 65 64 20 73 74 61 74 65 6d 65 6e 74 20 77 6f 75  ed statement wou
1dc19 6c 64 20 68 61 76 65 20 68 61 6c 74 65 64 0a 20  ld have halted. 
1dc1a 20 20 20 2a 2a 20 62 65 66 6f 72 65 20 72 65 61     ** before rea
1dc1b 63 68 69 6e 67 20 74 68 69 73 20 69 6e 73 74 72  ching this instr
1dc1c 75 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 69  uction. */.    i
1dc1d 66 28 20 4e 45 56 45 52 28 75 2e 61 77 2e 70 32  f( NEVER(u.aw.p2
1dc1e 3c 32 29 20 29 20 7b 0a 20 20 20 20 20 20 72 63  <2) ) {.      rc
1dc1f 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
1dc20 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f  T_BKPT;.      go
1dc21 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1dc22 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d  error;.    }.  }
1dc23 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79  .  if( pOp->p4ty
1dc24 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 29  pe==P4_KEYINFO )
1dc25 7b 0a 20 20 20 20 75 2e 61 77 2e 70 4b 65 79 49  {.    u.aw.pKeyI
1dc26 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b  nfo = pOp->p4.pK
1dc27 65 79 49 6e 66 6f 3b 0a 20 20 20 20 75 2e 61 77  eyInfo;.    u.aw
1dc28 2e 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d  .pKeyInfo->enc =
1dc29 20 45 4e 43 28 70 2d 3e 64 62 29 3b 0a 20 20 20   ENC(p->db);.   
1dc2a 20 75 2e 61 77 2e 6e 46 69 65 6c 64 20 3d 20 75   u.aw.nField = u
1dc2b 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46  .aw.pKeyInfo->nF
1dc2c 69 65 6c 64 2b 31 3b 0a 20 20 7d 65 6c 73 65 20  ield+1;.  }else 
1dc2d 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d  if( pOp->p4type=
1dc2e 3d 50 34 5f 49 4e 54 33 32 20 29 7b 0a 20 20 20  =P4_INT32 ){.   
1dc2f 20 75 2e 61 77 2e 6e 46 69 65 6c 64 20 3d 20 70   u.aw.nField = p
1dc30 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 7d 0a 20 20  Op->p4.i;.  }.  
1dc31 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1dc32 3d 30 20 29 3b 0a 20 20 75 2e 61 77 2e 70 43 75  =0 );.  u.aw.pCu
1dc33 72 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73  r = allocateCurs
1dc34 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 75  or(p, pOp->p1, u
1dc35 2e 61 77 2e 6e 46 69 65 6c 64 2c 20 75 2e 61 77  .aw.nField, u.aw
1dc36 2e 69 44 62 2c 20 31 29 3b 0a 20 20 69 66 28 20  .iDb, 1);.  if( 
1dc37 75 2e 61 77 2e 70 43 75 72 3d 3d 30 20 29 20 67  u.aw.pCur==0 ) g
1dc38 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 75 2e  oto no_mem;.  u.
1dc39 61 77 2e 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77  aw.pCur->nullRow
1dc3a 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20 73 71 6c   = 1;.  rc = sql
1dc3b 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28  ite3BtreeCursor(
1dc3c 75 2e 61 77 2e 70 58 2c 20 75 2e 61 77 2e 70 32  u.aw.pX, u.aw.p2
1dc3d 2c 20 75 2e 61 77 2e 77 72 46 6c 61 67 2c 20 75  , u.aw.wrFlag, u
1dc3e 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 2c 20 75 2e  .aw.pKeyInfo, u.
1dc3f 61 77 2e 70 43 75 72 2d 3e 70 43 75 72 73 6f 72  aw.pCur->pCursor
1dc40 29 3b 0a 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e  );.  u.aw.pCur->
1dc41 70 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 61 77 2e  pKeyInfo = u.aw.
1dc42 70 4b 65 79 49 6e 66 6f 3b 0a 0a 20 20 2f 2a 20  pKeyInfo;..  /* 
1dc43 53 69 6e 63 65 20 69 74 20 70 65 72 66 6f 72 6d  Since it perform
1dc44 73 20 6e 6f 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  s no memory allo
1dc45 63 61 74 69 6f 6e 20 6f 72 20 49 4f 2c 20 74 68  cation or IO, th
1dc46 65 20 6f 6e 6c 79 20 76 61 6c 75 65 73 20 74 68  e only values th
1dc47 61 74 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 42  at.  ** sqlite3B
1dc48 74 72 65 65 43 75 72 73 6f 72 28 29 20 6d 61 79  treeCursor() may
1dc49 20 72 65 74 75 72 6e 20 61 72 65 20 53 51 4c 49   return are SQLI
1dc4a 54 45 5f 45 4d 50 54 59 20 61 6e 64 20 53 51 4c  TE_EMPTY and SQL
1dc4b 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2a 20 53 51 4c  ITE_OK..  ** SQL
1dc4c 49 54 45 5f 45 4d 50 54 59 20 69 73 20 6f 6e 6c  ITE_EMPTY is onl
1dc4d 79 20 72 65 74 75 72 6e 65 64 20 77 68 65 6e 20  y returned when 
1dc4e 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 6f 70  attempting to op
1dc4f 65 6e 20 74 68 65 20 74 61 62 6c 65 0a 20 20 2a  en the table.  *
1dc50 2a 20 72 6f 6f 74 65 64 20 61 74 20 70 61 67 65  * rooted at page
1dc51 20 31 20 6f 66 20 61 20 7a 65 72 6f 2d 62 79 74   1 of a zero-byt
1dc52 65 20 64 61 74 61 62 61 73 65 2e 20 20 2a 2f 0a  e database.  */.
1dc53 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
1dc54 4c 49 54 45 5f 45 4d 50 54 59 20 7c 7c 20 72 63  LITE_EMPTY || rc
1dc55 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  ==SQLITE_OK );. 
1dc56 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1dc57 45 4d 50 54 59 20 29 7b 0a 20 20 20 20 75 2e 61  EMPTY ){.    u.a
1dc58 77 2e 70 43 75 72 2d 3e 70 43 75 72 73 6f 72 20  w.pCur->pCursor 
1dc59 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  = 0;.    rc = SQ
1dc5a 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
1dc5b 2f 2a 20 53 65 74 20 74 68 65 20 56 64 62 65 43  /* Set the VdbeC
1dc5c 75 72 73 6f 72 2e 69 73 54 61 62 6c 65 20 61 6e  ursor.isTable an
1dc5d 64 20 69 73 49 6e 64 65 78 20 76 61 72 69 61 62  d isIndex variab
1dc5e 6c 65 73 2e 20 50 72 65 76 69 6f 75 73 20 76 65  les. Previous ve
1dc5f 72 73 69 6f 6e 73 20 6f 66 0a 20 20 2a 2a 20 53  rsions of.  ** S
1dc60 51 4c 69 74 65 20 75 73 65 64 20 74 6f 20 63 68  QLite used to ch
1dc61 65 63 6b 20 69 66 20 74 68 65 20 72 6f 6f 74 2d  eck if the root-
1dc62 70 61 67 65 20 66 6c 61 67 73 20 77 65 72 65 20  page flags were 
1dc63 73 61 6e 65 20 61 74 20 74 68 69 73 20 70 6f 69  sane at this poi
1dc64 6e 74 0a 20 20 2a 2a 20 61 6e 64 20 72 65 70 6f  nt.  ** and repo
1dc65 72 74 20 64 61 74 61 62 61 73 65 20 63 6f 72 72  rt database corr
1dc66 75 70 74 69 6f 6e 20 69 66 20 74 68 65 79 20 77  uption if they w
1dc67 65 72 65 20 6e 6f 74 2c 20 62 75 74 20 74 68 69  ere not, but thi
1dc68 73 20 63 68 65 63 6b 20 68 61 73 0a 20 20 2a 2a  s check has.  **
1dc69 20 73 69 6e 63 65 20 6d 6f 76 65 64 20 69 6e 74   since moved int
1dc6a 6f 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65  o the btree laye
1dc6b 72 2e 20 20 2a 2f 0a 20 20 75 2e 61 77 2e 70 43  r.  */.  u.aw.pC
1dc6c 75 72 2d 3e 69 73 54 61 62 6c 65 20 3d 20 70 4f  ur->isTable = pO
1dc6d 70 2d 3e 70 34 74 79 70 65 21 3d 50 34 5f 4b 45  p->p4type!=P4_KE
1dc6e 59 49 4e 46 4f 3b 0a 20 20 75 2e 61 77 2e 70 43  YINFO;.  u.aw.pC
1dc6f 75 72 2d 3e 69 73 49 6e 64 65 78 20 3d 20 21 75  ur->isIndex = !u
1dc70 2e 61 77 2e 70 43 75 72 2d 3e 69 73 54 61 62 6c  .aw.pCur->isTabl
1dc71 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  e;.  break;.}../
1dc72 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 45 70  * Opcode: OpenEp
1dc73 68 65 6d 65 72 61 6c 20 50 31 20 50 32 20 2a 20  hemeral P1 P2 * 
1dc74 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20  P4 *.**.** Open 
1dc75 61 20 6e 65 77 20 63 75 72 73 6f 72 20 50 31 20  a new cursor P1 
1dc76 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74  to a transient t
1dc77 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72  able..** The cur
1dc78 73 6f 72 20 69 73 20 61 6c 77 61 79 73 20 6f 70  sor is always op
1dc79 65 6e 65 64 20 72 65 61 64 2f 77 72 69 74 65 20  ened read/write 
1dc7a 65 76 65 6e 20 69 66 20 0a 2a 2a 20 74 68 65 20  even if .** the 
1dc7b 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 69 73  main database is
1dc7c 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 54 68 65   read-only.  The
1dc7d 20 74 72 61 6e 73 69 65 6e 74 20 6f 72 20 76 69   transient or vi
1dc7e 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 69  rtual.** table i
1dc7f 73 20 64 65 6c 65 74 65 64 20 61 75 74 6f 6d 61  s deleted automa
1dc80 74 69 63 61 6c 6c 79 20 77 68 65 6e 20 74 68 65  tically when the
1dc81 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f 73 65   cursor is close
1dc82 64 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 74  d..**.** P2 is t
1dc83 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
1dc84 75 6d 6e 73 20 69 6e 20 74 68 65 20 76 69 72 74  umns in the virt
1dc85 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68  ual table..** Th
1dc86 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20  e cursor points 
1dc87 74 6f 20 61 20 42 54 72 65 65 20 74 61 62 6c 65  to a BTree table
1dc88 20 69 66 20 50 34 3d 3d 30 20 61 6e 64 20 74 6f   if P4==0 and to
1dc89 20 61 20 42 54 72 65 65 20 69 6e 64 65 78 0a 2a   a BTree index.*
1dc8a 2a 20 69 66 20 50 34 20 69 73 20 6e 6f 74 20 30  * if P4 is not 0
1dc8b 2e 20 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20  .  If P4 is not 
1dc8c 4e 55 4c 4c 2c 20 69 74 20 70 6f 69 6e 74 73 20  NULL, it points 
1dc8d 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72  to a KeyInfo str
1dc8e 75 63 74 75 72 65 0a 2a 2a 20 74 68 61 74 20 64  ucture.** that d
1dc8f 65 66 69 6e 65 73 20 74 68 65 20 66 6f 72 6d 61  efines the forma
1dc90 74 20 6f 66 20 6b 65 79 73 20 69 6e 20 74 68 65  t of keys in the
1dc91 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68   index..**.** Th
1dc92 69 73 20 6f 70 63 6f 64 65 20 77 61 73 20 6f 6e  is opcode was on
1dc93 63 65 20 63 61 6c 6c 65 64 20 4f 70 65 6e 54 65  ce called OpenTe
1dc94 6d 70 2e 20 20 42 75 74 20 74 68 61 74 20 63 72  mp.  But that cr
1dc95 65 61 74 65 64 0a 2a 2a 20 63 6f 6e 66 75 73 69  eated.** confusi
1dc96 6f 6e 20 62 65 63 61 75 73 65 20 74 68 65 20 74  on because the t
1dc97 65 72 6d 20 22 74 65 6d 70 20 74 61 62 6c 65 22  erm "temp table"
1dc98 2c 20 6d 69 67 68 74 20 72 65 66 65 72 20 65 69  , might refer ei
1dc99 74 68 65 72 0a 2a 2a 20 74 6f 20 61 20 54 45 4d  ther.** to a TEM
1dc9a 50 20 74 61 62 6c 65 20 61 74 20 74 68 65 20 53  P table at the S
1dc9b 51 4c 20 6c 65 76 65 6c 2c 20 6f 72 20 74 6f 20  QL level, or to 
1dc9c 61 20 74 61 62 6c 65 20 6f 70 65 6e 65 64 20 62  a table opened b
1dc9d 79 0a 2a 2a 20 74 68 69 73 20 6f 70 63 6f 64 65  y.** this opcode
1dc9e 2e 20 20 54 68 65 6e 20 74 68 69 73 20 6f 70 63  .  Then this opc
1dc9f 6f 64 65 20 77 61 73 20 63 61 6c 6c 20 4f 70 65  ode was call Ope
1dca0 6e 56 69 72 74 75 61 6c 2e 20 20 42 75 74 0a 2a  nVirtual.  But.*
1dca1 2a 20 74 68 61 74 20 63 72 65 61 74 65 64 20 63  * that created c
1dca2 6f 6e 66 75 73 69 6f 6e 20 77 69 74 68 20 74 68  onfusion with th
1dca3 65 20 77 68 6f 6c 65 20 76 69 72 74 75 61 6c 2d  e whole virtual-
1dca4 74 61 62 6c 65 20 69 64 65 61 2e 0a 2a 2f 0a 63  table idea..*/.c
1dca5 61 73 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d  ase OP_OpenEphem
1dca6 65 72 61 6c 3a 20 7b 0a 23 69 66 20 30 20 20 2f  eral: {.#if 0  /
1dca7 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1dca8 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1dca9 78 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f  x */.  VdbeCurso
1dcaa 72 20 2a 70 43 78 3b 0a 23 65 6e 64 69 66 20 2f  r *pCx;.#endif /
1dcab 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1dcac 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1dcad 78 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f  x */.  static co
1dcae 6e 73 74 20 69 6e 74 20 6f 70 65 6e 46 6c 61 67  nst int openFlag
1dcaf 73 20 3d 0a 20 20 20 20 20 20 53 51 4c 49 54 45  s =.      SQLITE
1dcb0 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20  _OPEN_READWRITE 
1dcb1 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f  |.      SQLITE_O
1dcb2 50 45 4e 5f 43 52 45 41 54 45 20 7c 0a 20 20 20  PEN_CREATE |.   
1dcb3 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45     SQLITE_OPEN_E
1dcb4 58 43 4c 55 53 49 56 45 20 7c 0a 20 20 20 20 20  XCLUSIVE |.     
1dcb5 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c   SQLITE_OPEN_DEL
1dcb6 45 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a 20 20 20  ETEONCLOSE |.   
1dcb7 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54     SQLITE_OPEN_T
1dcb8 52 41 4e 53 49 45 4e 54 5f 44 42 3b 0a 0a 20 20  RANSIENT_DB;..  
1dcb9 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1dcba 3d 30 20 29 3b 0a 20 20 75 2e 61 78 2e 70 43 78  =0 );.  u.ax.pCx
1dcbb 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f   = allocateCurso
1dcbc 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f  r(p, pOp->p1, pO
1dcbd 70 2d 3e 70 32 2c 20 2d 31 2c 20 31 29 3b 0a 20  p->p2, -1, 1);. 
1dcbe 20 69 66 28 20 75 2e 61 78 2e 70 43 78 3d 3d 30   if( u.ax.pCx==0
1dcbf 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
1dcc0 20 20 75 2e 61 78 2e 70 43 78 2d 3e 6e 75 6c 6c    u.ax.pCx->null
1dcc1 52 6f 77 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20  Row = 1;.  rc = 
1dcc2 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63 74  sqlite3BtreeFact
1dcc3 6f 72 79 28 64 62 2c 20 30 2c 20 31 2c 20 53 51  ory(db, 0, 1, SQ
1dcc4 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 54 45 4d  LITE_DEFAULT_TEM
1dcc5 50 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20 6f 70  P_CACHE_SIZE, op
1dcc6 65 6e 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20  enFlags,.       
1dcc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dcc8 20 20 20 20 26 75 2e 61 78 2e 70 43 78 2d 3e 70      &u.ax.pCx->p
1dcc9 42 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  Bt);.  if( rc==S
1dcca 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1dccb 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1dccc 65 42 65 67 69 6e 54 72 61 6e 73 28 75 2e 61 78  eBeginTrans(u.ax
1dccd 2e 70 43 78 2d 3e 70 42 74 2c 20 31 29 3b 0a 20  .pCx->pBt, 1);. 
1dcce 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c   }.  if( rc==SQL
1dccf 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a  ITE_OK ){.    /*
1dcd0 20 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74 20   If a transient 
1dcd1 69 6e 64 65 78 20 69 73 20 72 65 71 75 69 72 65  index is require
1dcd2 64 2c 20 63 72 65 61 74 65 20 69 74 20 62 79 20  d, create it by 
1dcd3 63 61 6c 6c 69 6e 67 0a 20 20 20 20 2a 2a 20 73  calling.    ** s
1dcd4 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74  qlite3BtreeCreat
1dcd5 65 54 61 62 6c 65 28 29 20 77 69 74 68 20 74 68  eTable() with th
1dcd6 65 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41  e BTREE_ZERODATA
1dcd7 20 66 6c 61 67 20 62 65 66 6f 72 65 0a 20 20 20   flag before.   
1dcd8 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 69 74 2e 20   ** opening it. 
1dcd9 49 66 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74  If a transient t
1dcda 61 62 6c 65 20 69 73 20 72 65 71 75 69 72 65 64  able is required
1dcdb 2c 20 6a 75 73 74 20 75 73 65 20 74 68 65 0a 20  , just use the. 
1dcdc 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61     ** automatica
1dcdd 6c 6c 79 20 63 72 65 61 74 65 64 20 74 61 62 6c  lly created tabl
1dcde 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65  e with root-page
1dcdf 20 31 20 28 61 6e 20 49 4e 54 4b 45 59 20 74 61   1 (an INTKEY ta
1dce0 62 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ble)..    */.   
1dce1 20 69 66 28 20 70 4f 70 2d 3e 70 34 2e 70 4b 65   if( pOp->p4.pKe
1dce2 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 69  yInfo ){.      i
1dce3 6e 74 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 61  nt pgno;.      a
1dce4 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79  ssert( pOp->p4ty
1dce5 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 29  pe==P4_KEYINFO )
1dce6 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
1dce7 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54  ite3BtreeCreateT
1dce8 61 62 6c 65 28 75 2e 61 78 2e 70 43 78 2d 3e 70  able(u.ax.pCx->p
1dce9 42 74 2c 20 26 70 67 6e 6f 2c 20 42 54 52 45 45  Bt, &pgno, BTREE
1dcea 5f 5a 45 52 4f 44 41 54 41 29 3b 0a 20 20 20 20  _ZERODATA);.    
1dceb 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1dcec 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 61  _OK ){.        a
1dced 73 73 65 72 74 28 20 70 67 6e 6f 3d 3d 4d 41 53  ssert( pgno==MAS
1dcee 54 45 52 5f 52 4f 4f 54 2b 31 20 29 3b 0a 20 20  TER_ROOT+1 );.  
1dcef 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1dcf0 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 75 2e  e3BtreeCursor(u.
1dcf1 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20 70 67 6e  ax.pCx->pBt, pgn
1dcf2 6f 2c 20 31 2c 0a 20 20 20 20 20 20 20 20 20 20  o, 1,.          
1dcf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dcf4 20 20 20 20 20 20 28 4b 65 79 49 6e 66 6f 2a 29        (KeyInfo*)
1dcf5 70 4f 70 2d 3e 70 34 2e 7a 2c 20 75 2e 61 78 2e  pOp->p4.z, u.ax.
1dcf6 70 43 78 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20  pCx->pCursor);. 
1dcf7 20 20 20 20 20 20 20 75 2e 61 78 2e 70 43 78 2d         u.ax.pCx-
1dcf8 3e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d  >pKeyInfo = pOp-
1dcf9 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  >p4.pKeyInfo;.  
1dcfa 20 20 20 20 20 20 75 2e 61 78 2e 70 43 78 2d 3e        u.ax.pCx->
1dcfb 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20  pKeyInfo->enc = 
1dcfc 45 4e 43 28 70 2d 3e 64 62 29 3b 0a 20 20 20 20  ENC(p->db);.    
1dcfd 20 20 7d 0a 20 20 20 20 20 20 75 2e 61 78 2e 70    }.      u.ax.p
1dcfe 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20 30 3b  Cx->isTable = 0;
1dcff 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1dd00 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1dd01 72 65 65 43 75 72 73 6f 72 28 75 2e 61 78 2e 70  reeCursor(u.ax.p
1dd02 43 78 2d 3e 70 42 74 2c 20 4d 41 53 54 45 52 5f  Cx->pBt, MASTER_
1dd03 52 4f 4f 54 2c 20 31 2c 20 30 2c 20 75 2e 61 78  ROOT, 1, 0, u.ax
1dd04 2e 70 43 78 2d 3e 70 43 75 72 73 6f 72 29 3b 0a  .pCx->pCursor);.
1dd05 20 20 20 20 20 20 75 2e 61 78 2e 70 43 78 2d 3e        u.ax.pCx->
1dd06 69 73 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20  isTable = 1;.   
1dd07 20 7d 0a 20 20 7d 0a 20 20 75 2e 61 78 2e 70 43   }.  }.  u.ax.pC
1dd08 78 2d 3e 69 73 49 6e 64 65 78 20 3d 20 21 75 2e  x->isIndex = !u.
1dd09 61 78 2e 70 43 78 2d 3e 69 73 54 61 62 6c 65 3b  ax.pCx->isTable;
1dd0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1dd0b 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 50 73 65 75  Opcode: OpenPseu
1dd0c 64 6f 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  do P1 P2 P3 * *.
1dd0d 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77  **.** Open a new
1dd0e 20 63 75 72 73 6f 72 20 74 68 61 74 20 70 6f 69   cursor that poi
1dd0f 6e 74 73 20 74 6f 20 61 20 66 61 6b 65 20 74 61  nts to a fake ta
1dd10 62 6c 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ble that contain
1dd11 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 72 6f  s a single.** ro
1dd12 77 20 6f 66 20 64 61 74 61 2e 20 20 54 68 65 20  w of data.  The 
1dd13 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 61 74 20  content of that 
1dd14 6f 6e 65 20 72 6f 77 20 69 6e 20 74 68 65 20 63  one row in the c
1dd15 6f 6e 74 65 6e 74 20 6f 66 20 6d 65 6d 6f 72 79  ontent of memory
1dd16 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 32 2e  .** register P2.
1dd17 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
1dd18 2c 20 63 75 72 73 6f 72 20 50 31 20 62 65 63 6f  , cursor P1 beco
1dd19 6d 65 73 20 61 6e 20 61 6c 69 61 73 20 66 6f 72  mes an alias for
1dd1a 20 74 68 65 20 0a 2a 2a 20 4d 45 4d 5f 42 6c 6f   the .** MEM_Blo
1dd1b 62 20 63 6f 6e 74 65 6e 74 20 63 6f 6e 74 61 69  b content contai
1dd1c 6e 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20  ned in register 
1dd1d 50 32 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 73 65 75  P2..**.** A pseu
1dd1e 64 6f 2d 74 61 62 6c 65 20 63 72 65 61 74 65 64  do-table created
1dd1f 20 62 79 20 74 68 69 73 20 6f 70 63 6f 64 65 20   by this opcode 
1dd20 69 73 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20  is used to hold 
1dd21 74 68 65 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20  the a single.** 
1dd22 72 6f 77 20 6f 75 74 70 75 74 20 66 72 6f 6d 20  row output from 
1dd23 74 68 65 20 73 6f 72 74 65 72 20 73 6f 20 74 68  the sorter so th
1dd24 61 74 20 74 68 65 20 72 6f 77 20 63 61 6e 20 62  at the row can b
1dd25 65 20 64 65 63 6f 6d 70 6f 73 65 64 20 69 6e 74  e decomposed int
1dd26 6f 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20  o.** individual 
1dd27 63 6f 6c 75 6d 6e 73 20 75 73 69 6e 67 20 74 68  columns using th
1dd28 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f  e OP_Column opco
1dd29 64 65 2e 20 20 54 68 65 20 4f 50 5f 43 6f 6c 75  de.  The OP_Colu
1dd2a 6d 6e 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 20  mn opcode.** is 
1dd2b 74 68 65 20 6f 6e 6c 79 20 63 75 72 73 6f 72 20  the only cursor 
1dd2c 6f 70 63 6f 64 65 20 74 68 61 74 20 77 6f 72 6b  opcode that work
1dd2d 73 20 77 69 74 68 20 61 20 70 73 65 75 64 6f 2d  s with a pseudo-
1dd2e 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 50 33 20  table..**.** P3 
1dd2f 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
1dd30 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72   fields in the r
1dd31 65 63 6f 72 64 73 20 74 68 61 74 20 77 69 6c 6c  ecords that will
1dd32 20 62 65 20 73 74 6f 72 65 64 20 62 79 0a 2a 2a   be stored by.**
1dd33 20 74 68 65 20 70 73 65 75 64 6f 2d 74 61 62 6c   the pseudo-tabl
1dd34 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70  e..*/.case OP_Op
1dd35 65 6e 50 73 65 75 64 6f 3a 20 7b 0a 23 69 66 20  enPseudo: {.#if 
1dd36 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1dd37 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1dd38 20 75 2e 61 79 20 2a 2f 0a 20 20 56 64 62 65 43   u.ay */.  VdbeC
1dd39 75 72 73 6f 72 20 2a 70 43 78 3b 0a 23 65 6e 64  ursor *pCx;.#end
1dd3a 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1dd3b 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1dd3c 20 75 2e 61 79 20 2a 2f 0a 0a 20 20 61 73 73 65   u.ay */..  asse
1dd3d 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 29  rt( pOp->p1>=0 )
1dd3e 3b 0a 20 20 75 2e 61 79 2e 70 43 78 20 3d 20 61  ;.  u.ay.pCx = a
1dd3f 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c  llocateCursor(p,
1dd40 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70   pOp->p1, pOp->p
1dd41 33 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 69 66 28  3, -1, 0);.  if(
1dd42 20 75 2e 61 79 2e 70 43 78 3d 3d 30 20 29 20 67   u.ay.pCx==0 ) g
1dd43 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 75 2e  oto no_mem;.  u.
1dd44 61 79 2e 70 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20  ay.pCx->nullRow 
1dd45 3d 20 31 3b 0a 20 20 75 2e 61 79 2e 70 43 78 2d  = 1;.  u.ay.pCx-
1dd46 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 20  >pseudoTableReg 
1dd47 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61  = pOp->p2;.  u.a
1dd48 79 2e 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d  y.pCx->isTable =
1dd49 20 31 3b 0a 20 20 75 2e 61 79 2e 70 43 78 2d 3e   1;.  u.ay.pCx->
1dd4a 69 73 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 62  isIndex = 0;.  b
1dd4b 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1dd4c 64 65 3a 20 43 6c 6f 73 65 20 50 31 20 2a 20 2a  de: Close P1 * *
1dd4d 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 65   * *.**.** Close
1dd4e 20 61 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f   a cursor previo
1dd4f 75 73 6c 79 20 6f 70 65 6e 65 64 20 61 73 20 50  usly opened as P
1dd50 31 2e 20 20 49 66 20 50 31 20 69 73 20 6e 6f 74  1.  If P1 is not
1dd51 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 6f 70  .** currently op
1dd52 65 6e 2c 20 74 68 69 73 20 69 6e 73 74 72 75 63  en, this instruc
1dd53 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tion is a no-op.
1dd54 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6c 6f 73  .*/.case OP_Clos
1dd55 65 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20 70  e: {.  assert( p
1dd56 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
1dd57 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  ->p1<p->nCursor 
1dd58 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1dd59 46 72 65 65 43 75 72 73 6f 72 28 70 2c 20 70 2d  FreeCursor(p, p-
1dd5a 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 29  >apCsr[pOp->p1])
1dd5b 3b 0a 20 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  ;.  p->apCsr[pOp
1dd5c 2d 3e 70 31 5d 20 3d 20 30 3b 0a 20 20 62 72 65  ->p1] = 0;.  bre
1dd5d 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1dd5e 3a 20 53 65 65 6b 47 65 20 50 31 20 50 32 20 50  : SeekGe P1 P2 P
1dd5f 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20  3 P4 *.**.** If 
1dd60 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73  cursor P1 refers
1dd61 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c 65   to an SQL table
1dd62 20 28 42 2d 54 72 65 65 20 74 68 61 74 20 75 73   (B-Tree that us
1dd63 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73 29  es integer keys)
1dd64 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76 61  , .** use the va
1dd65 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1dd66 50 33 20 61 73 20 74 68 65 20 6b 65 79 2e 20 20  P3 as the key.  
1dd67 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66  If cursor P1 ref
1dd68 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51  ers .** to an SQ
1dd69 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 50 33  L index, then P3
1dd6a 20 69 73 20 74 68 65 20 66 69 72 73 74 20 69 6e   is the first in
1dd6b 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50 34 20   an array of P4 
1dd6c 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20 74 68  registers .** th
1dd6d 61 74 20 61 72 65 20 75 73 65 64 20 61 73 20 61  at are used as a
1dd6e 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78  n unpacked index
1dd6f 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70   key. .**.** Rep
1dd70 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f 72 20 50  osition cursor P
1dd71 31 20 73 6f 20 74 68 61 74 20 20 69 74 20 70 6f  1 so that  it po
1dd72 69 6e 74 73 20 74 6f 20 74 68 65 20 73 6d 61 6c  ints to the smal
1dd73 6c 65 73 74 20 65 6e 74 72 79 20 74 68 61 74 20  lest entry that 
1dd74 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20 74  .** is greater t
1dd75 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
1dd76 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 49  the key value. I
1dd77 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72  f there are no r
1dd78 65 63 6f 72 64 73 20 0a 2a 2a 20 67 72 65 61 74  ecords .** great
1dd79 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  er than or equal
1dd7a 20 74 6f 20 74 68 65 20 6b 65 79 20 61 6e 64 20   to the key and 
1dd7b 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20  P2 is not zero, 
1dd7c 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e  then jump to P2.
1dd7d 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
1dd7e 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64   Found, NotFound
1dd7f 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 65 65 6b  , Distinct, Seek
1dd80 4c 74 2c 20 53 65 65 6b 47 74 2c 20 53 65 65 6b  Lt, SeekGt, Seek
1dd81 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  Le.*/./* Opcode:
1dd82 20 53 65 65 6b 47 74 20 50 31 20 50 32 20 50 33   SeekGt P1 P2 P3
1dd83 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 63   P4 *.**.** If c
1dd84 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20  ursor P1 refers 
1dd85 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20  to an SQL table 
1dd86 28 42 2d 54 72 65 65 20 74 68 61 74 20 75 73 65  (B-Tree that use
1dd87 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73 29 2c  s integer keys),
1dd88 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76 61 6c   .** use the val
1dd89 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1dd8a 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63  3 as a key. If c
1dd8b 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20  ursor P1 refers 
1dd8c 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e  .** to an SQL in
1dd8d 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20  dex, then P3 is 
1dd8e 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20  the first in an 
1dd8f 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69  array of P4 regi
1dd90 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61  sters .** that a
1dd91 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e  re used as an un
1dd92 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79  packed index key
1dd93 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74  . .**.** Reposit
1dd94 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f  ion cursor P1 so
1dd95 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73   that  it points
1dd96 20 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73 74   to the smallest
1dd97 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20   entry that .** 
1dd98 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
1dd99 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 49  the key value. I
1dd9a 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72  f there are no r
1dd9b 65 63 6f 72 64 73 20 67 72 65 61 74 65 72 20 74  ecords greater t
1dd9c 68 61 6e 20 0a 2a 2a 20 74 68 65 20 6b 65 79 20  han .** the key 
1dd9d 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65  and P2 is not ze
1dd9e 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f  ro, then jump to
1dd9f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61   P2..**.** See a
1dda0 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46  lso: Found, NotF
1dda1 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20  ound, Distinct, 
1dda2 53 65 65 6b 4c 74 2c 20 53 65 65 6b 47 65 2c 20  SeekLt, SeekGe, 
1dda3 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63  SeekLe.*/./* Opc
1dda4 6f 64 65 3a 20 53 65 65 6b 4c 74 20 50 31 20 50  ode: SeekLt P1 P
1dda5 32 20 50 33 20 50 34 20 2a 20 0a 2a 2a 0a 2a 2a  2 P3 P4 * .**.**
1dda6 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65   If cursor P1 re
1dda7 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 74  fers to an SQL t
1dda8 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68 61  able (B-Tree tha
1dda9 74 20 75 73 65 73 20 69 6e 74 65 67 65 72 20 6b  t uses integer k
1ddaa 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68  eys), .** use th
1ddab 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1ddac 74 65 72 20 50 33 20 61 73 20 61 20 6b 65 79 2e  ter P3 as a key.
1ddad 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72 65   If cursor P1 re
1ddae 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 53  fers .** to an S
1ddaf 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 50  QL index, then P
1ddb0 33 20 69 73 20 74 68 65 20 66 69 72 73 74 20 69  3 is the first i
1ddb1 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50 34  n an array of P4
1ddb2 20 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20 74   registers .** t
1ddb3 68 61 74 20 61 72 65 20 75 73 65 64 20 61 73 20  hat are used as 
1ddb4 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65  an unpacked inde
1ddb5 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65  x key. .**.** Re
1ddb6 70 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f 72 20  position cursor 
1ddb7 50 31 20 73 6f 20 74 68 61 74 20 20 69 74 20 70  P1 so that  it p
1ddb8 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6c 61 72  oints to the lar
1ddb9 67 65 73 74 20 65 6e 74 72 79 20 74 68 61 74 20  gest entry that 
1ddba 0a 2a 2a 20 69 73 20 6c 65 73 73 20 74 68 61 6e  .** is less than
1ddbb 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20   the key value. 
1ddbc 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  If there are no 
1ddbd 72 65 63 6f 72 64 73 20 6c 65 73 73 20 74 68 61  records less tha
1ddbe 6e 20 0a 2a 2a 20 74 68 65 20 6b 65 79 20 61 6e  n .** the key an
1ddbf 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f  d P2 is not zero
1ddc0 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50  , then jump to P
1ddc1 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  2..**.** See als
1ddc2 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75  o: Found, NotFou
1ddc3 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 53 65  nd, Distinct, Se
1ddc4 65 6b 47 74 2c 20 53 65 65 6b 47 65 2c 20 53 65  ekGt, SeekGe, Se
1ddc5 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  ekLe.*/./* Opcod
1ddc6 65 3a 20 53 65 65 6b 4c 65 20 50 31 20 50 32 20  e: SeekLe P1 P2 
1ddc7 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66  P3 P4 *.**.** If
1ddc8 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72   cursor P1 refer
1ddc9 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62 6c  s to an SQL tabl
1ddca 65 20 28 42 2d 54 72 65 65 20 74 68 61 74 20 75  e (B-Tree that u
1ddcb 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79 73  ses integer keys
1ddcc 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20 76  ), .** use the v
1ddcd 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1ddce 20 50 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66   P3 as a key. If
1ddcf 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72   cursor P1 refer
1ddd0 73 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20  s .** to an SQL 
1ddd1 69 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69  index, then P3 i
1ddd2 73 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61  s the first in a
1ddd3 6e 20 61 72 72 61 79 20 6f 66 20 50 34 20 72 65  n array of P4 re
1ddd4 67 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74  gisters .** that
1ddd5 20 61 72 65 20 75 73 65 64 20 61 73 20 61 6e 20   are used as an 
1ddd6 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b  unpacked index k
1ddd7 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73  ey. .**.** Repos
1ddd8 69 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20  ition cursor P1 
1ddd9 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74  so that it point
1ddda 73 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74  s to the largest
1dddb 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20   entry that .** 
1dddc 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20  is less than or 
1dddd 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79  equal to the key
1ddde 20 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65   value. If there
1dddf 20 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20   are no records 
1dde0 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 6f 72  .** less than or
1dde1 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65   equal to the ke
1dde2 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20  y and P2 is not 
1dde3 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20  zero, then jump 
1dde4 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  to P2..**.** See
1dde5 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f   also: Found, No
1dde6 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74  tFound, Distinct
1dde7 2c 20 53 65 65 6b 47 74 2c 20 53 65 65 6b 47 65  , SeekGt, SeekGe
1dde8 2c 20 53 65 65 6b 4c 74 0a 2a 2f 0a 63 61 73 65  , SeekLt.*/.case
1dde9 20 4f 50 5f 53 65 65 6b 4c 74 3a 20 20 20 20 20   OP_SeekLt:     
1ddea 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33      /* jump, in3
1ddeb 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b   */.case OP_Seek
1ddec 4c 65 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 6a  Le:         /* j
1dded 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65  ump, in3 */.case
1ddee 20 4f 50 5f 53 65 65 6b 47 65 3a 20 20 20 20 20   OP_SeekGe:     
1ddef 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33      /* jump, in3
1ddf0 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b   */.case OP_Seek
1ddf1 47 74 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6a  Gt: {       /* j
1ddf2 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20  ump, in3 */.#if 
1ddf3 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1ddf4 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1ddf5 20 75 2e 61 7a 20 2a 2f 0a 20 20 69 6e 74 20 72   u.az */.  int r
1ddf6 65 73 3b 0a 20 20 69 6e 74 20 6f 63 3b 0a 20 20  es;.  int oc;.  
1ddf7 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1ddf8 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
1ddf9 20 72 3b 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64   r;.  int nField
1ddfa 3b 0a 20 20 69 36 34 20 69 4b 65 79 3b 20 20 20  ;.  i64 iKey;   
1ddfb 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 20     /* The rowid 
1ddfc 77 65 20 61 72 65 20 74 6f 20 73 65 65 6b 20 74  we are to seek t
1ddfd 6f 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  o */.#endif /* l
1ddfe 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1ddff 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 7a 20 2a  oved into u.az *
1de00 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  /..  assert( pOp
1de01 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
1de02 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b  p1<p->nCursor );
1de03 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1de04 70 32 21 3d 30 20 29 3b 0a 20 20 75 2e 61 7a 2e  p2!=0 );.  u.az.
1de05 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f  pC = p->apCsr[pO
1de06 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  p->p1];.  assert
1de07 28 20 75 2e 61 7a 2e 70 43 21 3d 30 20 29 3b 0a  ( u.az.pC!=0 );.
1de08 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 70    assert( u.az.p
1de09 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65  C->pseudoTableRe
1de0a 67 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 75 2e  g==0 );.  if( u.
1de0b 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d  az.pC->pCursor!=
1de0c 30 20 29 7b 0a 20 20 20 20 75 2e 61 7a 2e 6f 63  0 ){.    u.az.oc
1de0d 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a   = pOp->opcode;.
1de0e 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 6e 75 6c      u.az.pC->nul
1de0f 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20 69 66  lRow = 0;.    if
1de10 28 20 75 2e 61 7a 2e 70 43 2d 3e 69 73 54 61 62  ( u.az.pC->isTab
1de11 6c 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  le ){.      /* T
1de12 68 65 20 69 6e 70 75 74 20 76 61 6c 75 65 20 69  he input value i
1de13 6e 20 50 33 20 6d 69 67 68 74 20 62 65 20 6f 66  n P3 might be of
1de14 20 61 6e 79 20 74 79 70 65 3a 20 69 6e 74 65 67   any type: integ
1de15 65 72 2c 20 72 65 61 6c 2c 20 73 74 72 69 6e 67  er, real, string
1de16 2c 0a 20 20 20 20 20 20 2a 2a 20 62 6c 6f 62 2c  ,.      ** blob,
1de17 20 6f 72 20 4e 55 4c 4c 2e 20 20 42 75 74 20 69   or NULL.  But i
1de18 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 6e  t needs to be an
1de19 20 69 6e 74 65 67 65 72 20 62 65 66 6f 72 65 20   integer before 
1de1a 77 65 20 63 61 6e 20 64 6f 0a 20 20 20 20 20 20  we can do.      
1de1b 2a 2a 20 74 68 65 20 73 65 65 6b 2c 20 73 6f 20  ** the seek, so 
1de1c 63 6f 76 65 72 74 20 69 74 2e 20 2a 2f 0a 20 20  covert it. */.  
1de1d 20 20 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63      applyNumeric
1de1e 41 66 66 69 6e 69 74 79 28 70 49 6e 33 29 3b 0a  Affinity(pIn3);.
1de1f 20 20 20 20 20 20 75 2e 61 7a 2e 69 4b 65 79 20        u.az.iKey 
1de20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74  = sqlite3VdbeInt
1de21 56 61 6c 75 65 28 70 49 6e 33 29 3b 0a 20 20 20  Value(pIn3);.   
1de22 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69     u.az.pC->rowi
1de23 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 0a 20  dIsValid = 0;.. 
1de24 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50       /* If the P
1de25 33 20 76 61 6c 75 65 20 63 6f 75 6c 64 20 6e 6f  3 value could no
1de26 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69  t be converted i
1de27 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 77  nto an integer w
1de28 69 74 68 6f 75 74 0a 20 20 20 20 20 20 2a 2a 20  ithout.      ** 
1de29 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74  loss of informat
1de2a 69 6f 6e 2c 20 74 68 65 6e 20 73 70 65 63 69 61  ion, then specia
1de2b 6c 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20  l processing is 
1de2c 72 65 71 75 69 72 65 64 2e 2e 2e 20 2a 2f 0a 20  required... */. 
1de2d 20 20 20 20 20 69 66 28 20 28 70 49 6e 33 2d 3e       if( (pIn3->
1de2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29  flags & MEM_Int)
1de2f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ==0 ){.        i
1de30 66 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20  f( (pIn3->flags 
1de31 26 20 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29  & MEM_Real)==0 )
1de32 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  {.          /* I
1de33 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 63  f the P3 value c
1de34 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72 74  annot be convert
1de35 65 64 20 69 6e 74 6f 20 61 6e 79 20 6b 69 6e 64  ed into any kind
1de36 20 6f 66 20 61 20 6e 75 6d 62 65 72 2c 0a 20 20   of a number,.  
1de37 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20          ** then 
1de38 74 68 65 20 73 65 65 6b 20 69 73 20 6e 6f 74 20  the seek is not 
1de39 70 6f 73 73 69 62 6c 65 2c 20 73 6f 20 6a 75 6d  possible, so jum
1de3a 70 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20 20 20  p to P2 */.     
1de3b 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70       pc = pOp->p
1de3c 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  2 - 1;.         
1de3d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
1de3e 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  }.        /* If 
1de3f 77 65 20 72 65 61 63 68 20 74 68 69 73 20 70 6f  we reach this po
1de40 69 6e 74 2c 20 74 68 65 6e 20 74 68 65 20 50 33  int, then the P3
1de41 20 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20 61   value must be a
1de42 20 66 6c 6f 61 74 69 6e 67 0a 20 20 20 20 20 20   floating.      
1de43 20 20 2a 2a 20 70 6f 69 6e 74 20 6e 75 6d 62 65    ** point numbe
1de44 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73  r. */.        as
1de45 73 65 72 74 28 20 28 70 49 6e 33 2d 3e 66 6c 61  sert( (pIn3->fla
1de46 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 21 3d  gs & MEM_Real)!=
1de47 30 20 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66  0 );..        if
1de48 28 20 75 2e 61 7a 2e 69 4b 65 79 3d 3d 53 4d 41  ( u.az.iKey==SMA
1de49 4c 4c 45 53 54 5f 49 4e 54 36 34 20 26 26 20 28  LLEST_INT64 && (
1de4a 70 49 6e 33 2d 3e 72 3c 28 64 6f 75 62 6c 65 29  pIn3->r<(double)
1de4b 75 2e 61 7a 2e 69 4b 65 79 20 7c 7c 20 70 49 6e  u.az.iKey || pIn
1de4c 33 2d 3e 72 3e 30 29 20 29 7b 0a 20 20 20 20 20  3->r>0) ){.     
1de4d 20 20 20 20 20 2f 2a 20 54 68 65 20 50 33 20 76       /* The P3 v
1de4e 61 6c 75 65 20 69 73 20 74 6f 6f 20 6c 61 72 67  alue is too larg
1de4f 65 20 69 6e 20 6d 61 67 6e 69 74 75 64 65 20 74  e in magnitude t
1de50 6f 20 62 65 20 65 78 70 72 65 73 73 65 64 20 61  o be expressed a
1de51 73 20 61 6e 0a 20 20 20 20 20 20 20 20 20 20 2a  s an.          *
1de52 2a 20 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 20 20  * integer. */.  
1de53 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 72 65 73          u.az.res
1de54 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   = 1;.          
1de55 69 66 28 20 70 49 6e 33 2d 3e 72 3c 30 20 29 7b  if( pIn3->r<0 ){
1de56 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
1de57 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65   u.az.oc==OP_See
1de58 6b 47 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d  kGt || u.az.oc==
1de59 4f 50 5f 53 65 65 6b 47 65 20 29 7b 0a 20 20 20  OP_SeekGe ){.   
1de5a 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
1de5b 73 71 6c 69 74 65 33 42 74 72 65 65 46 69 72 73  sqlite3BtreeFirs
1de5c 74 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73  t(u.az.pC->pCurs
1de5d 6f 72 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a  or, &u.az.res);.
1de5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
1de5f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1de60 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1de61 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20  _to_error;.     
1de62 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1de63 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1de64 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 6f        if( u.az.o
1de65 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20  c==OP_SeekLt || 
1de66 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b  u.az.oc==OP_Seek
1de67 4c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  Le ){.          
1de68 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1de69 42 74 72 65 65 4c 61 73 74 28 75 2e 61 7a 2e 70  BtreeLast(u.az.p
1de6a 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 61  C->pCursor, &u.a
1de6b 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20  z.res);.        
1de6c 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1de6d 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61  LITE_OK ) goto a
1de6e 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1de6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  r;.            }
1de70 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1de71 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e         if( u.az.
1de72 72 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20  res ){.         
1de73 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
1de74 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  - 1;.          }
1de75 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
1de76 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ;.        }else 
1de77 69 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f  if( u.az.oc==OP_
1de78 53 65 65 6b 4c 74 20 7c 7c 20 75 2e 61 7a 2e 6f  SeekLt || u.az.o
1de79 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 29 7b 0a  c==OP_SeekGe ){.
1de7a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
1de7b 20 74 68 65 20 63 65 69 6c 69 6e 67 28 29 20 66   the ceiling() f
1de7c 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e 76 65  unction to conve
1de7d 72 74 20 72 65 61 6c 2d 3e 69 6e 74 20 2a 2f 0a  rt real->int */.
1de7e 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49            if( pI
1de7f 6e 33 2d 3e 72 20 3e 20 28 64 6f 75 62 6c 65 29  n3->r > (double)
1de80 75 2e 61 7a 2e 69 4b 65 79 20 29 20 75 2e 61 7a  u.az.iKey ) u.az
1de81 2e 69 4b 65 79 2b 2b 3b 0a 20 20 20 20 20 20 20  .iKey++;.       
1de82 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1de83 20 20 2f 2a 20 55 73 65 20 74 68 65 20 66 6c 6f    /* Use the flo
1de84 6f 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f  or() function to
1de85 20 63 6f 6e 76 65 72 74 20 72 65 61 6c 2d 3e 69   convert real->i
1de86 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  nt */.          
1de87 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 6f 63 3d  assert( u.az.oc=
1de88 3d 4f 50 5f 53 65 65 6b 4c 65 20 7c 7c 20 75 2e  =OP_SeekLe || u.
1de89 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74  az.oc==OP_SeekGt
1de8a 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66   );.          if
1de8b 28 20 70 49 6e 33 2d 3e 72 20 3c 20 28 64 6f 75  ( pIn3->r < (dou
1de8c 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 20 29 20  ble)u.az.iKey ) 
1de8d 75 2e 61 7a 2e 69 4b 65 79 2d 2d 3b 0a 20 20 20  u.az.iKey--;.   
1de8e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
1de8f 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1de90 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61  3BtreeMovetoUnpa
1de91 63 6b 65 64 28 75 2e 61 7a 2e 70 43 2d 3e 70 43  cked(u.az.pC->pC
1de92 75 72 73 6f 72 2c 20 30 2c 20 28 75 36 34 29 75  ursor, 0, (u64)u
1de93 2e 61 7a 2e 69 4b 65 79 2c 20 30 2c 20 26 75 2e  .az.iKey, 0, &u.
1de94 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 69  az.res);.      i
1de95 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1de96 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
1de97 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
1de98 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ror;.      }.   
1de99 20 20 20 69 66 28 20 75 2e 61 7a 2e 72 65 73 3d     if( u.az.res=
1de9a 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e  =0 ){.        u.
1de9b 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61  az.pC->rowidIsVa
1de9c 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  lid = 1;.       
1de9d 20 75 2e 61 7a 2e 70 43 2d 3e 6c 61 73 74 52 6f   u.az.pC->lastRo
1de9e 77 69 64 20 3d 20 75 2e 61 7a 2e 69 4b 65 79 3b  wid = u.az.iKey;
1de9f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
1dea0 73 65 7b 0a 20 20 20 20 20 20 75 2e 61 7a 2e 6e  se{.      u.az.n
1dea1 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 34 2e  Field = pOp->p4.
1dea2 69 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  i;.      assert(
1dea3 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34   pOp->p4type==P4
1dea4 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 20 20  _INT32 );.      
1dea5 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 6e 46 69  assert( u.az.nFi
1dea6 65 6c 64 3e 30 20 29 3b 0a 20 20 20 20 20 20 75  eld>0 );.      u
1dea7 2e 61 7a 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d  .az.r.pKeyInfo =
1dea8 20 75 2e 61 7a 2e 70 43 2d 3e 70 4b 65 79 49 6e   u.az.pC->pKeyIn
1dea9 66 6f 3b 0a 20 20 20 20 20 20 75 2e 61 7a 2e 72  fo;.      u.az.r
1deaa 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29 75  .nField = (u16)u
1deab 2e 61 7a 2e 6e 46 69 65 6c 64 3b 0a 20 20 20 20  .az.nField;.    
1deac 20 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f    if( u.az.oc==O
1dead 50 5f 53 65 65 6b 47 74 20 7c 7c 20 75 2e 61 7a  P_SeekGt || u.az
1deae 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29  .oc==OP_SeekLe )
1deaf 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 72  {.        u.az.r
1deb0 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45  .flags = UNPACKE
1deb1 44 5f 49 4e 43 52 4b 45 59 3b 0a 20 20 20 20 20  D_INCRKEY;.     
1deb2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1deb3 75 2e 61 7a 2e 72 2e 66 6c 61 67 73 20 3d 20 30  u.az.r.flags = 0
1deb4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1deb5 75 2e 61 7a 2e 72 2e 61 4d 65 6d 20 3d 20 26 70  u.az.r.aMem = &p
1deb6 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b  ->aMem[pOp->p3];
1deb7 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1deb8 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e  te3BtreeMovetoUn
1deb9 70 61 63 6b 65 64 28 75 2e 61 7a 2e 70 43 2d 3e  packed(u.az.pC->
1deba 70 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a 2e 72  pCursor, &u.az.r
1debb 2c 20 30 2c 20 30 2c 20 26 75 2e 61 7a 2e 72 65  , 0, 0, &u.az.re
1debc 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  s);.      if( rc
1debd 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1debe 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72         goto abor
1debf 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1dec0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 75 2e        }.      u.
1dec1 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61  az.pC->rowidIsVa
1dec2 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  lid = 0;.    }. 
1dec3 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 64 65 66 65     u.az.pC->defe
1dec4 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a  rredMoveto = 0;.
1dec5 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 63 61 63      u.az.pC->cac
1dec6 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
1dec7 5f 53 54 41 4c 45 3b 0a 23 69 66 64 65 66 20 53  _STALE;.#ifdef S
1dec8 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73  QLITE_TEST.    s
1dec9 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f  qlite3_search_co
1deca 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20  unt++;.#endif.  
1decb 20 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f    if( u.az.oc==O
1decc 50 5f 53 65 65 6b 47 65 20 7c 7c 20 75 2e 61 7a  P_SeekGe || u.az
1decd 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 29  .oc==OP_SeekGt )
1dece 7b 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a  {.      if( u.az
1decf 2e 72 65 73 3c 30 20 7c 7c 20 28 75 2e 61 7a 2e  .res<0 || (u.az.
1ded0 72 65 73 3d 3d 30 20 26 26 20 75 2e 61 7a 2e 6f  res==0 && u.az.o
1ded1 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 29 20 29 7b  c==OP_SeekGt) ){
1ded2 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
1ded3 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 75  lite3BtreeNext(u
1ded4 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c  .az.pC->pCursor,
1ded5 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20   &u.az.res);.   
1ded6 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1ded7 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62  ITE_OK ) goto ab
1ded8 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1ded9 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 70  ;.        u.az.p
1deda 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  C->rowidIsValid 
1dedb 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 0;.      }else
1dedc 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 72  {.        u.az.r
1dedd 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  es = 0;.      }.
1dede 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1dedf 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 6f 63   assert( u.az.oc
1dee0 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 75  ==OP_SeekLt || u
1dee1 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c  .az.oc==OP_SeekL
1dee2 65 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 75  e );.      if( u
1dee3 2e 61 7a 2e 72 65 73 3e 30 20 7c 7c 20 28 75 2e  .az.res>0 || (u.
1dee4 61 7a 2e 72 65 73 3d 3d 30 20 26 26 20 75 2e 61  az.res==0 && u.a
1dee5 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 29  z.oc==OP_SeekLt)
1dee6 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
1dee7 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65   sqlite3BtreePre
1dee8 76 69 6f 75 73 28 75 2e 61 7a 2e 70 43 2d 3e 70  vious(u.az.pC->p
1dee9 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 65  Cursor, &u.az.re
1deea 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  s);.        if( 
1deeb 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
1deec 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1deed 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20  o_error;.       
1deee 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49   u.az.pC->rowidI
1deef 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20  sValid = 0;.    
1def0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1def1 20 2f 2a 20 75 2e 61 7a 2e 72 65 73 20 6d 69 67   /* u.az.res mig
1def2 68 74 20 62 65 20 6e 65 67 61 74 69 76 65 20 62  ht be negative b
1def3 65 63 61 75 73 65 20 74 68 65 20 74 61 62 6c 65  ecause the table
1def4 20 69 73 20 65 6d 70 74 79 2e 20 20 43 68 65 63   is empty.  Chec
1def5 6b 20 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20  k to.        ** 
1def6 73 65 65 20 69 66 20 74 68 69 73 20 69 73 20 74  see if this is t
1def7 68 65 20 63 61 73 65 2e 0a 20 20 20 20 20 20 20  he case..       
1def8 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 2e 61 7a   */.        u.az
1def9 2e 72 65 73 20 3d 20 73 71 6c 69 74 65 33 42 74  .res = sqlite3Bt
1defa 72 65 65 45 6f 66 28 75 2e 61 7a 2e 70 43 2d 3e  reeEof(u.az.pC->
1defb 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20  pCursor);.      
1defc 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  }.    }.    asse
1defd 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b  rt( pOp->p2>0 );
1defe 0a 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 72 65  .    if( u.az.re
1deff 73 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20  s ){.      pc = 
1df00 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20  pOp->p2 - 1;.   
1df01 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
1df02 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20  /* This happens 
1df03 77 68 65 6e 20 61 74 74 65 6d 70 74 69 6e 67 20  when attempting 
1df04 74 6f 20 6f 70 65 6e 20 74 68 65 20 73 71 6c 69  to open the sqli
1df05 74 65 33 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  te3_master table
1df06 0a 20 20 20 20 2a 2a 20 66 6f 72 20 72 65 61 64  .    ** for read
1df07 20 61 63 63 65 73 73 20 72 65 74 75 72 6e 73 20   access returns 
1df08 53 51 4c 49 54 45 5f 45 4d 50 54 59 2e 20 49 6e  SQLITE_EMPTY. In
1df09 20 74 68 69 73 20 63 61 73 65 20 61 6c 77 61 79   this case alway
1df0a 73 0a 20 20 20 20 2a 2a 20 74 61 6b 65 20 74 68  s.    ** take th
1df0b 65 20 6a 75 6d 70 20 28 73 69 6e 63 65 20 74 68  e jump (since th
1df0c 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f 72  ere are no recor
1df0d 64 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 29  ds in the table)
1df0e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 63 20  ..    */.    pc 
1df0f 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1df10 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1df11 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 20 50  * Opcode: Seek P
1df12 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1df13 20 50 31 20 69 73 20 61 6e 20 6f 70 65 6e 20 74   P1 is an open t
1df14 61 62 6c 65 20 63 75 72 73 6f 72 20 61 6e 64 20  able cursor and 
1df15 50 32 20 69 73 20 61 20 72 6f 77 69 64 20 69 6e  P2 is a rowid in
1df16 74 65 67 65 72 2e 20 20 41 72 72 61 6e 67 65 0a  teger.  Arrange.
1df17 2a 2a 20 66 6f 72 20 50 31 20 74 6f 20 6d 6f 76  ** for P1 to mov
1df18 65 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69  e so that it poi
1df19 6e 74 73 20 74 6f 20 74 68 65 20 72 6f 77 69 64  nts to the rowid
1df1a 20 67 69 76 65 6e 20 62 79 20 50 32 2e 0a 2a 2a   given by P2..**
1df1b 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 63 74 75  .** This is actu
1df1c 61 6c 6c 79 20 61 20 64 65 66 65 72 72 65 64 20  ally a deferred 
1df1d 73 65 65 6b 2e 20 20 4e 6f 74 68 69 6e 67 20 61  seek.  Nothing a
1df1e 63 74 75 61 6c 6c 79 20 68 61 70 70 65 6e 73 20  ctually happens 
1df1f 75 6e 74 69 6c 0a 2a 2a 20 74 68 65 20 63 75 72  until.** the cur
1df20 73 6f 72 20 69 73 20 75 73 65 64 20 74 6f 20 72  sor is used to r
1df21 65 61 64 20 61 20 72 65 63 6f 72 64 2e 20 20 54  ead a record.  T
1df22 68 61 74 20 77 61 79 2c 20 69 66 20 6e 6f 20 72  hat way, if no r
1df23 65 61 64 73 0a 2a 2a 20 6f 63 63 75 72 2c 20 6e  eads.** occur, n
1df24 6f 20 75 6e 6e 65 63 65 73 73 61 72 79 20 49 2f  o unnecessary I/
1df25 4f 20 68 61 70 70 65 6e 73 2e 0a 2a 2f 0a 63 61  O happens..*/.ca
1df26 73 65 20 4f 50 5f 53 65 65 6b 3a 20 7b 20 20 20  se OP_Seek: {   
1df27 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69 66 20 30   /* in2 */.#if 0
1df28 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1df29 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1df2a 75 2e 62 61 20 2a 2f 0a 20 20 56 64 62 65 43 75  u.ba */.  VdbeCu
1df2b 72 73 6f 72 20 2a 70 43 3b 0a 23 65 6e 64 69 66  rsor *pC;.#endif
1df2c 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1df2d 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1df2e 2e 62 61 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  .ba */..  assert
1df2f 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1df30 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73  pOp->p1<p->nCurs
1df31 6f 72 20 29 3b 0a 20 20 75 2e 62 61 2e 70 43 20  or );.  u.ba.pC 
1df32 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  = p->apCsr[pOp->
1df33 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
1df34 2e 62 61 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69  .ba.pC!=0 );.  i
1df35 66 28 20 41 4c 57 41 59 53 28 75 2e 62 61 2e 70  f( ALWAYS(u.ba.p
1df36 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 29 20 29  C->pCursor!=0) )
1df37 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e  {.    assert( u.
1df38 62 61 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29  ba.pC->isTable )
1df39 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 2d 3e 6e  ;.    u.ba.pC->n
1df3a 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20  ullRow = 0;.    
1df3b 75 2e 62 61 2e 70 43 2d 3e 6d 6f 76 65 74 6f 54  u.ba.pC->movetoT
1df3c 61 72 67 65 74 20 3d 20 73 71 6c 69 74 65 33 56  arget = sqlite3V
1df3d 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 32  dbeIntValue(pIn2
1df3e 29 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 2d 3e  );.    u.ba.pC->
1df3f 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30  rowidIsValid = 0
1df40 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 2d 3e 64  ;.    u.ba.pC->d
1df41 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20  eferredMoveto = 
1df42 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  1;.  }.  break;.
1df43 7d 0a 20 20 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  }.  ../* Opcode:
1df44 20 46 6f 75 6e 64 20 50 31 20 50 32 20 50 33 20   Found P1 P2 P3 
1df45 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74  * *.**.** Regist
1df46 65 72 20 50 33 20 68 6f 6c 64 73 20 61 20 62 6c  er P3 holds a bl
1df47 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62  ob constructed b
1df48 79 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20 20 50  y MakeRecord.  P
1df49 31 20 69 73 20 61 6e 20 69 6e 64 65 78 2e 0a 2a  1 is an index..*
1df4a 2a 20 49 66 20 61 6e 20 65 6e 74 72 79 20 74 68  * If an entry th
1df4b 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 20 76  at matches the v
1df4c 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1df4d 20 70 33 20 65 78 69 73 74 73 20 69 6e 20 50 31   p3 exists in P1
1df4e 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 74 6f   then.** jump to
1df4f 20 50 32 2e 20 20 49 66 20 74 68 65 20 50 33 20   P2.  If the P3 
1df50 76 61 6c 75 65 20 64 6f 65 73 20 6e 6f 74 20 6d  value does not m
1df51 61 74 63 68 20 61 6e 79 20 65 6e 74 72 79 20 69  atch any entry i
1df52 6e 20 50 31 0a 2a 2a 20 74 68 65 6e 20 66 61 6c  n P1.** then fal
1df53 6c 20 74 68 72 75 2e 20 20 54 68 65 20 50 31 20  l thru.  The P1 
1df54 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70  cursor is left p
1df55 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 20 6d  ointing at the m
1df56 61 74 63 68 69 6e 67 20 65 6e 74 72 79 0a 2a 2a  atching entry.**
1df57 20 69 66 20 69 74 20 65 78 69 73 74 73 2e 0a 2a   if it exists..*
1df58 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75  *.** This instru
1df59 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f  ction is used to
1df5a 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 49   implement the I
1df5b 4e 20 6f 70 65 72 61 74 6f 72 20 77 68 65 72 65  N operator where
1df5c 20 74 68 65 0a 2a 2a 20 6c 65 66 74 2d 68 61 6e   the.** left-han
1df5d 64 20 73 69 64 65 20 69 73 20 61 20 53 45 4c 45  d side is a SELE
1df5e 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 50  CT statement.  P
1df5f 31 20 6d 61 79 20 62 65 20 61 20 74 72 75 65 20  1 may be a true 
1df60 69 6e 64 65 78 2c 20 6f 72 20 69 74 0a 2a 2a 20  index, or it.** 
1df61 6d 61 79 20 62 65 20 61 20 74 65 6d 70 6f 72 61  may be a tempora
1df62 72 79 20 69 6e 64 65 78 20 74 68 61 74 20 68 6f  ry index that ho
1df63 6c 64 73 20 74 68 65 20 72 65 73 75 6c 74 73 20  lds the results 
1df64 6f 66 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a  of the SELECT.**
1df65 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 20 54 68   statement.   Th
1df66 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  is instruction i
1df67 73 20 61 6c 73 6f 20 75 73 65 64 20 74 6f 20 69  s also used to i
1df68 6d 70 6c 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20  mplement the.** 
1df69 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64  DISTINCT keyword
1df6a 20 69 6e 20 53 45 4c 45 43 54 20 73 74 61 74 65   in SELECT state
1df6b 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ments..**.** Thi
1df6c 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 68  s instruction ch
1df6d 65 63 6b 73 20 69 66 20 69 6e 64 65 78 20 50 31  ecks if index P1
1df6e 20 63 6f 6e 74 61 69 6e 73 20 61 20 72 65 63 6f   contains a reco
1df6f 72 64 20 66 6f 72 20 77 68 69 63 68 20 0a 2a 2a  rd for which .**
1df70 20 74 68 65 20 66 69 72 73 74 20 4e 20 73 65 72   the first N ser
1df71 69 61 6c 69 7a 65 64 20 76 61 6c 75 65 73 20 65  ialized values e
1df72 78 61 63 74 6c 79 20 6d 61 74 63 68 20 74 68 65  xactly match the
1df73 20 4e 20 73 65 72 69 61 6c 69 7a 65 64 20 76 61   N serialized va
1df74 6c 75 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 72  lues.** in the r
1df75 65 63 6f 72 64 20 69 6e 20 72 65 67 69 73 74 65  ecord in registe
1df76 72 20 50 33 2c 20 77 68 65 72 65 20 4e 20 69 73  r P3, where N is
1df77 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65   the total numbe
1df78 72 20 6f 66 20 76 61 6c 75 65 73 20 69 6e 0a 2a  r of values in.*
1df79 2a 20 74 68 65 20 50 33 20 72 65 63 6f 72 64 20  * the P3 record 
1df7a 28 74 68 65 20 50 33 20 72 65 63 6f 72 64 20 69  (the P3 record i
1df7b 73 20 61 20 70 72 65 66 69 78 20 6f 66 20 74 68  s a prefix of th
1df7c 65 20 50 31 20 72 65 63 6f 72 64 29 2e 20 0a 2a  e P1 record). .*
1df7d 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 4e  *.** See also: N
1df7e 6f 74 46 6f 75 6e 64 2c 20 49 73 55 6e 69 71 75  otFound, IsUniqu
1df7f 65 2c 20 4e 6f 74 45 78 69 73 74 73 0a 2a 2f 0a  e, NotExists.*/.
1df80 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 46 6f  /* Opcode: NotFo
1df81 75 6e 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a  und P1 P2 P3 * *
1df82 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  .**.** Register 
1df83 50 33 20 68 6f 6c 64 73 20 61 20 62 6c 6f 62 20  P3 holds a blob 
1df84 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 4d  constructed by M
1df85 61 6b 65 52 65 63 6f 72 64 2e 20 20 50 31 20 69  akeRecord.  P1 i
1df86 73 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e 20 20  s.** an index.  
1df87 49 66 20 6e 6f 20 65 6e 74 72 79 20 65 78 69 73  If no entry exis
1df88 74 73 20 69 6e 20 50 31 20 74 68 61 74 20 6d 61  ts in P1 that ma
1df89 74 63 68 65 73 20 74 68 65 20 62 6c 6f 62 20 74  tches the blob t
1df8a 68 65 6e 20 6a 75 6d 70 0a 2a 2a 20 74 6f 20 50  hen jump.** to P
1df8b 32 2e 20 20 49 66 20 61 6e 20 65 6e 74 72 79 20  2.  If an entry 
1df8c 64 6f 65 73 20 65 78 69 73 74 69 6e 67 2c 20 66  does existing, f
1df8d 61 6c 6c 20 74 68 72 6f 75 67 68 2e 20 20 54 68  all through.  Th
1df8e 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74  e cursor is left
1df8f 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  .** pointing to 
1df90 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 6d  the entry that m
1df91 61 74 63 68 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65  atches..**.** Se
1df92 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e  e also: Found, N
1df93 6f 74 45 78 69 73 74 73 2c 20 49 73 55 6e 69 71  otExists, IsUniq
1df94 75 65 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f  ue.*/.case OP_No
1df95 74 46 6f 75 6e 64 3a 20 20 20 20 20 20 20 2f 2a  tFound:       /*
1df96 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61   jump, in3 */.ca
1df97 73 65 20 4f 50 5f 46 6f 75 6e 64 3a 20 7b 20 20  se OP_Found: {  
1df98 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69        /* jump, i
1df99 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20  n3 */.#if 0  /* 
1df9a 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1df9b 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 62 20  moved into u.bb 
1df9c 2a 2f 0a 20 20 69 6e 74 20 61 6c 72 65 61 64 79  */.  int already
1df9d 45 78 69 73 74 73 3b 0a 20 20 56 64 62 65 43 75  Exists;.  VdbeCu
1df9e 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 6e 74 20  rsor *pC;.  int 
1df9f 72 65 73 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52  res;.  UnpackedR
1dfa0 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 0a  ecord *pIdxKey;.
1dfa1 20 20 63 68 61 72 20 61 54 65 6d 70 52 65 63 5b    char aTempRec[
1dfa2 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e  ROUND8(sizeof(Un
1dfa3 70 61 63 6b 65 64 52 65 63 6f 72 64 29 29 20 2b  packedRecord)) +
1dfa4 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 33 20 2b   sizeof(Mem)*3 +
1dfa5 20 37 5d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c   7];.#endif /* l
1dfa6 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1dfa7 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 62 20 2a  oved into u.bb *
1dfa8 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
1dfa9 5f 54 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f  _TEST.  sqlite3_
1dfaa 66 6f 75 6e 64 5f 63 6f 75 6e 74 2b 2b 3b 0a 23  found_count++;.#
1dfab 65 6e 64 69 66 0a 0a 20 20 75 2e 62 62 2e 61 6c  endif..  u.bb.al
1dfac 72 65 61 64 79 45 78 69 73 74 73 20 3d 20 30 3b  readyExists = 0;
1dfad 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1dfae 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
1dfaf 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1dfb0 20 75 2e 62 62 2e 70 43 20 3d 20 70 2d 3e 61 70   u.bb.pC = p->ap
1dfb1 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1dfb2 61 73 73 65 72 74 28 20 75 2e 62 62 2e 70 43 21  assert( u.bb.pC!
1dfb3 3d 30 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41  =0 );.  if( ALWA
1dfb4 59 53 28 75 2e 62 62 2e 70 43 2d 3e 70 43 75 72  YS(u.bb.pC->pCur
1dfb5 73 6f 72 21 3d 30 29 20 29 7b 0a 0a 20 20 20 20  sor!=0) ){..    
1dfb6 61 73 73 65 72 74 28 20 75 2e 62 62 2e 70 43 2d  assert( u.bb.pC-
1dfb7 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20  >isTable==0 );. 
1dfb8 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d     assert( pIn3-
1dfb9 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f  >flags & MEM_Blo
1dfba 62 20 29 3b 0a 20 20 20 20 45 78 70 61 6e 64 42  b );.    ExpandB
1dfbb 6c 6f 62 28 70 49 6e 33 29 3b 0a 20 20 20 20 75  lob(pIn3);.    u
1dfbc 2e 62 62 2e 70 49 64 78 4b 65 79 20 3d 20 73 71  .bb.pIdxKey = sq
1dfbd 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55  lite3VdbeRecordU
1dfbe 6e 70 61 63 6b 28 75 2e 62 62 2e 70 43 2d 3e 70  npack(u.bb.pC->p
1dfbf 4b 65 79 49 6e 66 6f 2c 20 70 49 6e 33 2d 3e 6e  KeyInfo, pIn3->n
1dfc0 2c 20 70 49 6e 33 2d 3e 7a 2c 0a 20 20 20 20 20  , pIn3->z,.     
1dfc1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dfc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dfc3 20 75 2e 62 62 2e 61 54 65 6d 70 52 65 63 2c 20   u.bb.aTempRec, 
1dfc4 73 69 7a 65 6f 66 28 75 2e 62 62 2e 61 54 65 6d  sizeof(u.bb.aTem
1dfc5 70 52 65 63 29 29 3b 0a 20 20 20 20 69 66 28 20  pRec));.    if( 
1dfc6 75 2e 62 62 2e 70 49 64 78 4b 65 79 3d 3d 30 20  u.bb.pIdxKey==0 
1dfc7 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f  ){.      goto no
1dfc8 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _mem;.    }.    
1dfc9 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
1dfca 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20 20  =OP_Found ){.   
1dfcb 20 20 20 75 2e 62 62 2e 70 49 64 78 4b 65 79 2d     u.bb.pIdxKey-
1dfcc 3e 66 6c 61 67 73 20 7c 3d 20 55 4e 50 41 43 4b  >flags |= UNPACK
1dfcd 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 3b  ED_PREFIX_MATCH;
1dfce 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
1dfcf 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
1dfd0 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 62 2e  toUnpacked(u.bb.
1dfd1 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 75 2e 62  pC->pCursor, u.b
1dfd2 62 2e 70 49 64 78 4b 65 79 2c 20 30 2c 20 30 2c  b.pIdxKey, 0, 0,
1dfd3 20 26 75 2e 62 62 2e 72 65 73 29 3b 0a 20 20 20   &u.bb.res);.   
1dfd4 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65   sqlite3VdbeDele
1dfd5 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  teUnpackedRecord
1dfd6 28 75 2e 62 62 2e 70 49 64 78 4b 65 79 29 3b 0a  (u.bb.pIdxKey);.
1dfd7 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1dfd8 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 62  TE_OK ){.      b
1dfd9 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1dfda 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69 73  u.bb.alreadyExis
1dfdb 74 73 20 3d 20 28 75 2e 62 62 2e 72 65 73 3d 3d  ts = (u.bb.res==
1dfdc 30 29 3b 0a 20 20 20 20 75 2e 62 62 2e 70 43 2d  0);.    u.bb.pC-
1dfdd 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
1dfde 3d 20 30 3b 0a 20 20 20 20 75 2e 62 62 2e 70 43  = 0;.    u.bb.pC
1dfdf 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
1dfe0 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d  CACHE_STALE;.  }
1dfe1 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f  .  if( pOp->opco
1dfe2 64 65 3d 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a  de==OP_Found ){.
1dfe3 20 20 20 20 69 66 28 20 75 2e 62 62 2e 61 6c 72      if( u.bb.alr
1dfe4 65 61 64 79 45 78 69 73 74 73 20 29 20 70 63 20  eadyExists ) pc 
1dfe5 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1dfe6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20   }else{.    if( 
1dfe7 21 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69  !u.bb.alreadyExi
1dfe8 73 74 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e  sts ) pc = pOp->
1dfe9 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72  p2 - 1;.  }.  br
1dfea 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1dfeb 65 3a 20 49 73 55 6e 69 71 75 65 20 50 31 20 50  e: IsUnique P1 P
1dfec 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  2 P3 P4 *.**.** 
1dfed 43 75 72 73 6f 72 20 50 31 20 69 73 20 6f 70 65  Cursor P1 is ope
1dfee 6e 20 6f 6e 20 61 6e 20 69 6e 64 65 78 2e 20 20  n on an index.  
1dfef 53 6f 20 69 74 20 68 61 73 20 6e 6f 20 64 61 74  So it has no dat
1dff0 61 20 61 6e 64 20 69 74 73 20 6b 65 79 20 63 6f  a and its key co
1dff1 6e 73 69 73 74 73 20 0a 2a 2a 20 6f 66 20 61 20  nsists .** of a 
1dff2 72 65 63 6f 72 64 20 67 65 6e 65 72 61 74 65 64  record generated
1dff3 20 62 79 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72   by OP_MakeRecor
1dff4 64 20 77 68 65 72 65 20 74 68 65 20 6c 61 73 74  d where the last
1dff5 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 0a 2a   field is the .*
1dff6 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 65  * rowid of the e
1dff7 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 69 6e  ntry that the in
1dff8 64 65 78 20 72 65 66 65 72 73 20 74 6f 2e 0a 2a  dex refers to..*
1dff9 2a 0a 2a 2a 20 54 68 65 20 50 33 20 72 65 67 69  *.** The P3 regi
1dffa 73 74 65 72 20 63 6f 6e 74 61 69 6e 73 20 61 6e  ster contains an
1dffb 20 69 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20   integer record 
1dffc 6e 75 6d 62 65 72 2e 20 43 61 6c 6c 20 74 68 69  number. Call thi
1dffd 73 20 72 65 63 6f 72 64 20 0a 2a 2a 20 6e 75 6d  s record .** num
1dffe 62 65 72 20 52 2e 20 52 65 67 69 73 74 65 72 20  ber R. Register 
1dfff 50 34 20 69 73 20 74 68 65 20 66 69 72 73 74 20  P4 is the first 
1e000 69 6e 20 61 20 73 65 74 20 6f 66 20 4e 20 63 6f  in a set of N co
1e001 6e 74 69 67 75 6f 75 73 20 72 65 67 69 73 74 65  ntiguous registe
1e002 72 73 0a 2a 2a 20 74 68 61 74 20 6d 61 6b 65 20  rs.** that make 
1e003 75 70 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69  up an unpacked i
1e004 6e 64 65 78 20 6b 65 79 20 74 68 61 74 20 63 61  ndex key that ca
1e005 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20 63  n be used with c
1e006 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 65  ursor P1..** The
1e007 20 76 61 6c 75 65 20 6f 66 20 4e 20 63 61 6e 20   value of N can 
1e008 62 65 20 69 6e 66 65 72 72 65 64 20 66 72 6f 6d  be inferred from
1e009 20 74 68 65 20 63 75 72 73 6f 72 2e 20 4e 20 69   the cursor. N i
1e00a 6e 63 6c 75 64 65 73 20 74 68 65 20 72 6f 77 69  ncludes the rowi
1e00b 64 0a 2a 2a 20 76 61 6c 75 65 20 61 70 70 65 6e  d.** value appen
1e00c 64 65 64 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  ded to the end o
1e00d 66 20 74 68 65 20 69 6e 64 65 78 20 72 65 63 6f  f the index reco
1e00e 72 64 2e 20 54 68 69 73 20 72 6f 77 69 64 20 76  rd. This rowid v
1e00f 61 6c 75 65 20 6d 61 79 0a 2a 2a 20 6f 72 20 6d  alue may.** or m
1e010 61 79 20 6e 6f 74 20 62 65 20 74 68 65 20 73 61  ay not be the sa
1e011 6d 65 20 61 73 20 52 2e 0a 2a 2a 0a 2a 2a 20 49  me as R..**.** I
1e012 66 20 61 6e 79 20 6f 66 20 74 68 65 20 4e 20 72  f any of the N r
1e013 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69  egisters beginni
1e014 6e 67 20 77 69 74 68 20 72 65 67 69 73 74 65 72  ng with register
1e015 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61 20 4e   P4 contains a N
1e016 55 4c 4c 0a 2a 2a 20 76 61 6c 75 65 2c 20 6a 75  ULL.** value, ju
1e017 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  mp immediately t
1e018 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65  o P2..**.** Othe
1e019 72 77 69 73 65 2c 20 74 68 69 73 20 69 6e 73 74  rwise, this inst
1e01a 72 75 63 74 69 6f 6e 20 63 68 65 63 6b 73 20 69  ruction checks i
1e01b 66 20 63 75 72 73 6f 72 20 50 31 20 63 6f 6e 74  f cursor P1 cont
1e01c 61 69 6e 73 20 61 6e 20 65 6e 74 72 79 0a 2a 2a  ains an entry.**
1e01d 20 77 68 65 72 65 20 74 68 65 20 66 69 72 73 74   where the first
1e01e 20 28 4e 2d 31 29 20 66 69 65 6c 64 73 20 6d 61   (N-1) fields ma
1e01f 74 63 68 20 62 75 74 20 74 68 65 20 72 6f 77 69  tch but the rowi
1e020 64 20 76 61 6c 75 65 20 61 74 20 74 68 65 20 65  d value at the e
1e021 6e 64 0a 2a 2a 20 6f 66 20 74 68 65 20 69 6e 64  nd.** of the ind
1e022 65 78 20 65 6e 74 72 79 20 69 73 20 6e 6f 74 20  ex entry is not 
1e023 52 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  R. If there is n
1e024 6f 20 73 75 63 68 20 65 6e 74 72 79 2c 20 63 6f  o such entry, co
1e025 6e 74 72 6f 6c 20 6a 75 6d 70 73 0a 2a 2a 20 74  ntrol jumps.** t
1e026 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 50 32  o instruction P2
1e027 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65  . Otherwise, the
1e028 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 63 6f   rowid of the co
1e029 6e 66 6c 69 63 74 69 6e 67 20 69 6e 64 65 78 0a  nflicting index.
1e02a 2a 2a 20 65 6e 74 72 79 20 69 73 20 63 6f 70 69  ** entry is copi
1e02b 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 20 50  ed to register P
1e02c 33 20 61 6e 64 20 63 6f 6e 74 72 6f 6c 20 66 61  3 and control fa
1e02d 6c 6c 73 20 74 68 72 6f 75 67 68 20 74 6f 20 74  lls through to t
1e02e 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 73 74 72  he next.** instr
1e02f 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65  uction..**.** Se
1e030 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f 75 6e 64  e also: NotFound
1e031 2c 20 4e 6f 74 45 78 69 73 74 73 2c 20 46 6f 75  , NotExists, Fou
1e032 6e 64 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 73  nd.*/.case OP_Is
1e033 55 6e 69 71 75 65 3a 20 7b 20 20 20 20 20 20 20  Unique: {       
1e034 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f   /* jump, in3 */
1e035 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1e036 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1e037 20 69 6e 74 6f 20 75 2e 62 63 20 2a 2f 0a 20 20   into u.bc */.  
1e038 75 31 36 20 69 69 3b 0a 20 20 56 64 62 65 43 75  u16 ii;.  VdbeCu
1e039 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 42 74 43  rsor *pCx;.  BtC
1e03a 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20  ursor *pCrsr;.  
1e03b 75 31 36 20 6e 46 69 65 6c 64 3b 0a 20 20 4d 65  u16 nField;.  Me
1e03c 6d 20 2a 61 4d 65 6d 3b 0a 20 20 55 6e 70 61 63  m *aMem;.  Unpac
1e03d 6b 65 64 52 65 63 6f 72 64 20 72 3b 20 20 20 20  kedRecord r;    
1e03e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1e03f 20 42 2d 54 72 65 65 20 69 6e 64 65 78 20 73 65   B-Tree index se
1e040 61 72 63 68 20 6b 65 79 20 2a 2f 0a 20 20 69 36  arch key */.  i6
1e041 34 20 52 3b 20 20 20 20 20 20 20 20 20 20 20 20  4 R;            
1e042 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e043 20 2f 2a 20 52 6f 77 69 64 20 73 74 6f 72 65 64   /* Rowid stored
1e044 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20   in register P3 
1e045 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  */.#endif /* loc
1e046 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e047 65 64 20 69 6e 74 6f 20 75 2e 62 63 20 2a 2f 0a  ed into u.bc */.
1e048 0a 20 20 75 2e 62 63 2e 61 4d 65 6d 20 3d 20 26  .  u.bc.aMem = &
1e049 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 34 2e  p->aMem[pOp->p4.
1e04a 69 5d 3b 0a 20 20 2f 2a 20 41 73 73 65 72 74 20  i];.  /* Assert 
1e04b 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 73 20  that the values 
1e04c 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 50 31  of parameters P1
1e04d 20 61 6e 64 20 50 34 20 61 72 65 20 69 6e 20 72   and P4 are in r
1e04e 61 6e 67 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72  ange. */.  asser
1e04f 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d  t( pOp->p4type==
1e050 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 61 73  P4_INT32 );.  as
1e051 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 69 3e  sert( pOp->p4.i>
1e052 30 20 26 26 20 70 4f 70 2d 3e 70 34 2e 69 3c 3d  0 && pOp->p4.i<=
1e053 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 61 73 73  p->nMem );.  ass
1e054 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
1e055 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43  && pOp->p1<p->nC
1e056 75 72 73 6f 72 20 29 3b 0a 0a 20 20 2f 2a 20 46  ursor );..  /* F
1e057 69 6e 64 20 74 68 65 20 69 6e 64 65 78 20 63 75  ind the index cu
1e058 72 73 6f 72 2e 20 2a 2f 0a 20 20 75 2e 62 63 2e  rsor. */.  u.bc.
1e059 70 43 78 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  pCx = p->apCsr[p
1e05a 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1e05b 74 28 20 75 2e 62 63 2e 70 43 78 2d 3e 64 65 66  t( u.bc.pCx->def
1e05c 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29  erredMoveto==0 )
1e05d 3b 0a 20 20 75 2e 62 63 2e 70 43 78 2d 3e 73 65  ;.  u.bc.pCx->se
1e05e 65 6b 52 65 73 75 6c 74 20 3d 20 30 3b 0a 20 20  ekResult = 0;.  
1e05f 75 2e 62 63 2e 70 43 78 2d 3e 63 61 63 68 65 53  u.bc.pCx->cacheS
1e060 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54  tatus = CACHE_ST
1e061 41 4c 45 3b 0a 20 20 75 2e 62 63 2e 70 43 72 73  ALE;.  u.bc.pCrs
1e062 72 20 3d 20 75 2e 62 63 2e 70 43 78 2d 3e 70 43  r = u.bc.pCx->pC
1e063 75 72 73 6f 72 3b 0a 0a 20 20 2f 2a 20 49 66 20  ursor;..  /* If 
1e064 61 6e 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65  any of the value
1e065 73 20 61 72 65 20 4e 55 4c 4c 2c 20 74 61 6b 65  s are NULL, take
1e066 20 74 68 65 20 6a 75 6d 70 2e 20 2a 2f 0a 20 20   the jump. */.  
1e067 75 2e 62 63 2e 6e 46 69 65 6c 64 20 3d 20 75 2e  u.bc.nField = u.
1e068 62 63 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f  bc.pCx->pKeyInfo
1e069 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 66 6f 72 28  ->nField;.  for(
1e06a 75 2e 62 63 2e 69 69 3d 30 3b 20 75 2e 62 63 2e  u.bc.ii=0; u.bc.
1e06b 69 69 3c 75 2e 62 63 2e 6e 46 69 65 6c 64 3b 20  ii<u.bc.nField; 
1e06c 75 2e 62 63 2e 69 69 2b 2b 29 7b 0a 20 20 20 20  u.bc.ii++){.    
1e06d 69 66 28 20 75 2e 62 63 2e 61 4d 65 6d 5b 75 2e  if( u.bc.aMem[u.
1e06e 62 63 2e 69 69 5d 2e 66 6c 61 67 73 20 26 20 4d  bc.ii].flags & M
1e06f 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20  EM_Null ){.     
1e070 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1e071 31 3b 0a 20 20 20 20 20 20 75 2e 62 63 2e 70 43  1;.      u.bc.pC
1e072 72 73 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 62  rsr = 0;.      b
1e073 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
1e074 20 20 61 73 73 65 72 74 28 20 28 75 2e 62 63 2e    assert( (u.bc.
1e075 61 4d 65 6d 5b 75 2e 62 63 2e 6e 46 69 65 6c 64  aMem[u.bc.nField
1e076 5d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ].flags & MEM_Nu
1e077 6c 6c 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28  ll)==0 );..  if(
1e078 20 75 2e 62 63 2e 70 43 72 73 72 21 3d 30 20 29   u.bc.pCrsr!=0 )
1e079 7b 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74  {.    /* Populat
1e07a 65 20 74 68 65 20 69 6e 64 65 78 20 73 65 61 72  e the index sear
1e07b 63 68 20 6b 65 79 2e 20 2a 2f 0a 20 20 20 20 75  ch key. */.    u
1e07c 2e 62 63 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d  .bc.r.pKeyInfo =
1e07d 20 75 2e 62 63 2e 70 43 78 2d 3e 70 4b 65 79 49   u.bc.pCx->pKeyI
1e07e 6e 66 6f 3b 0a 20 20 20 20 75 2e 62 63 2e 72 2e  nfo;.    u.bc.r.
1e07f 6e 46 69 65 6c 64 20 3d 20 75 2e 62 63 2e 6e 46  nField = u.bc.nF
1e080 69 65 6c 64 20 2b 20 31 3b 0a 20 20 20 20 75 2e  ield + 1;.    u.
1e081 62 63 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50  bc.r.flags = UNP
1e082 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41  ACKED_PREFIX_SEA
1e083 52 43 48 3b 0a 20 20 20 20 75 2e 62 63 2e 72 2e  RCH;.    u.bc.r.
1e084 61 4d 65 6d 20 3d 20 75 2e 62 63 2e 61 4d 65 6d  aMem = u.bc.aMem
1e085 3b 0a 0a 20 20 20 20 2f 2a 20 45 78 74 72 61 63  ;..    /* Extrac
1e086 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 75  t the value of u
1e087 2e 62 63 2e 52 20 66 72 6f 6d 20 72 65 67 69 73  .bc.R from regis
1e088 74 65 72 20 50 33 2e 20 2a 2f 0a 20 20 20 20 73  ter P3. */.    s
1e089 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74  qlite3VdbeMemInt
1e08a 65 67 65 72 69 66 79 28 70 49 6e 33 29 3b 0a 20  egerify(pIn3);. 
1e08b 20 20 20 75 2e 62 63 2e 52 20 3d 20 70 49 6e 33     u.bc.R = pIn3
1e08c 2d 3e 75 2e 69 3b 0a 0a 20 20 20 20 2f 2a 20 53  ->u.i;..    /* S
1e08d 65 61 72 63 68 20 74 68 65 20 42 2d 54 72 65 65  earch the B-Tree
1e08e 20 69 6e 64 65 78 2e 20 49 66 20 6e 6f 20 63 6f   index. If no co
1e08f 6e 66 6c 69 63 74 69 6e 67 20 72 65 63 6f 72 64  nflicting record
1e090 20 69 73 20 66 6f 75 6e 64 2c 20 6a 75 6d 70 0a   is found, jump.
1e091 20 20 20 20 2a 2a 20 74 6f 20 50 32 2e 20 4f 74      ** to P2. Ot
1e092 68 65 72 77 69 73 65 2c 20 63 6f 70 79 20 74 68  herwise, copy th
1e093 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 63  e rowid of the c
1e094 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 63 6f 72  onflicting recor
1e095 64 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 67 69  d to.    ** regi
1e096 73 74 65 72 20 50 33 20 61 6e 64 20 66 61 6c 6c  ster P3 and fall
1e097 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20   through to the 
1e098 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  next instruction
1e099 2e 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73  .  */.    rc = s
1e09a 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74  qlite3BtreeMovet
1e09b 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 63 2e 70  oUnpacked(u.bc.p
1e09c 43 72 73 72 2c 20 26 75 2e 62 63 2e 72 2c 20 30  Crsr, &u.bc.r, 0
1e09d 2c 20 30 2c 20 26 75 2e 62 63 2e 70 43 78 2d 3e  , 0, &u.bc.pCx->
1e09e 73 65 65 6b 52 65 73 75 6c 74 29 3b 0a 20 20 20  seekResult);.   
1e09f 20 69 66 28 20 28 75 2e 62 63 2e 72 2e 66 6c 61   if( (u.bc.r.fla
1e0a0 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 50 52  gs & UNPACKED_PR
1e0a1 45 46 49 58 5f 53 45 41 52 43 48 29 20 7c 7c 20  EFIX_SEARCH) || 
1e0a2 75 2e 62 63 2e 72 2e 72 6f 77 69 64 3d 3d 75 2e  u.bc.r.rowid==u.
1e0a3 62 63 2e 52 20 29 7b 0a 20 20 20 20 20 20 70 63  bc.R ){.      pc
1e0a4 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1e0a5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1e0a6 20 70 49 6e 33 2d 3e 75 2e 69 20 3d 20 75 2e 62   pIn3->u.i = u.b
1e0a7 63 2e 72 2e 72 6f 77 69 64 3b 0a 20 20 20 20 7d  c.r.rowid;.    }
1e0a8 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1e0a9 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 45  ./* Opcode: NotE
1e0aa 78 69 73 74 73 20 50 31 20 50 32 20 50 33 20 2a  xists P1 P2 P3 *
1e0ab 20 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65   *.**.** Use the
1e0ac 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69   content of regi
1e0ad 73 74 65 72 20 50 33 20 61 73 20 61 20 69 6e 74  ster P3 as a int
1e0ae 65 67 65 72 20 6b 65 79 2e 20 20 49 66 20 61 20  eger key.  If a 
1e0af 72 65 63 6f 72 64 20 0a 2a 2a 20 77 69 74 68 20  record .** with 
1e0b0 74 68 61 74 20 6b 65 79 20 64 6f 65 73 20 6e 6f  that key does no
1e0b1 74 20 65 78 69 73 74 20 69 6e 20 74 61 62 6c 65  t exist in table
1e0b2 20 6f 66 20 50 31 2c 20 74 68 65 6e 20 6a 75 6d   of P1, then jum
1e0b3 70 20 74 6f 20 50 32 2e 20 0a 2a 2a 20 49 66 20  p to P2. .** If 
1e0b4 74 68 65 20 72 65 63 6f 72 64 20 64 6f 65 73 20  the record does 
1e0b5 65 78 69 73 74 2c 20 74 68 65 6e 20 66 61 6c 6c  exist, then fall
1e0b6 20 74 68 72 75 2e 20 20 54 68 65 20 63 75 72 73   thru.  The curs
1e0b7 6f 72 20 69 73 20 6c 65 66 74 20 0a 2a 2a 20 70  or is left .** p
1e0b8 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 72  ointing to the r
1e0b9 65 63 6f 72 64 20 69 66 20 69 74 20 65 78 69 73  ecord if it exis
1e0ba 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69  ts..**.** The di
1e0bb 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e  fference between
1e0bc 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20   this operation 
1e0bd 61 6e 64 20 4e 6f 74 46 6f 75 6e 64 20 69 73 20  and NotFound is 
1e0be 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 6f 70 65  that this.** ope
1e0bf 72 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74  ration assumes t
1e0c0 68 65 20 6b 65 79 20 69 73 20 61 6e 20 69 6e 74  he key is an int
1e0c1 65 67 65 72 20 61 6e 64 20 74 68 61 74 20 50 31  eger and that P1
1e0c2 20 69 73 20 61 20 74 61 62 6c 65 20 77 68 65 72   is a table wher
1e0c3 65 61 73 0a 2a 2a 20 4e 6f 74 46 6f 75 6e 64 20  eas.** NotFound 
1e0c4 61 73 73 75 6d 65 73 20 6b 65 79 20 69 73 20 61  assumes key is a
1e0c5 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65   blob constructe
1e0c6 64 20 66 72 6f 6d 20 4d 61 6b 65 52 65 63 6f 72  d from MakeRecor
1e0c7 64 20 61 6e 64 0a 2a 2a 20 50 31 20 69 73 20 61  d and.** P1 is a
1e0c8 6e 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 53  n index..**.** S
1e0c9 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20  ee also: Found, 
1e0ca 4e 6f 74 46 6f 75 6e 64 2c 20 49 73 55 6e 69 71  NotFound, IsUniq
1e0cb 75 65 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f  ue.*/.case OP_No
1e0cc 74 45 78 69 73 74 73 3a 20 7b 20 20 20 20 20 20  tExists: {      
1e0cd 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a    /* jump, in3 *
1e0ce 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
1e0cf 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e0d0 64 20 69 6e 74 6f 20 75 2e 62 64 20 2a 2f 0a 20  d into u.bd */. 
1e0d1 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1e0d2 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72  .  BtCursor *pCr
1e0d3 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20  sr;.  int res;. 
1e0d4 20 75 36 34 20 69 4b 65 79 3b 0a 23 65 6e 64 69   u64 iKey;.#endi
1e0d5 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1e0d6 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e0d7 75 2e 62 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72  u.bd */..  asser
1e0d8 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26  t( pIn3->flags &
1e0d9 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 61 73   MEM_Int );.  as
1e0da 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1e0db 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
1e0dc 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 64  Cursor );.  u.bd
1e0dd 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  .pC = p->apCsr[p
1e0de 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1e0df 74 28 20 75 2e 62 64 2e 70 43 21 3d 30 20 29 3b  t( u.bd.pC!=0 );
1e0e0 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e  .  assert( u.bd.
1e0e1 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20  pC->isTable );. 
1e0e2 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e 70 43   assert( u.bd.pC
1e0e3 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67  ->pseudoTableReg
1e0e4 3d 3d 30 20 29 3b 0a 20 20 75 2e 62 64 2e 70 43  ==0 );.  u.bd.pC
1e0e5 72 73 72 20 3d 20 75 2e 62 64 2e 70 43 2d 3e 70  rsr = u.bd.pC->p
1e0e6 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 75 2e  Cursor;.  if( u.
1e0e7 62 64 2e 70 43 72 73 72 21 3d 30 20 29 7b 0a 20  bd.pCrsr!=0 ){. 
1e0e8 20 20 20 75 2e 62 64 2e 72 65 73 20 3d 20 30 3b     u.bd.res = 0;
1e0e9 0a 20 20 20 20 75 2e 62 64 2e 69 4b 65 79 20 3d  .    u.bd.iKey =
1e0ea 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 20   pIn3->u.i;.    
1e0eb 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1e0ec 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28  eMovetoUnpacked(
1e0ed 75 2e 62 64 2e 70 43 72 73 72 2c 20 30 2c 20 75  u.bd.pCrsr, 0, u
1e0ee 2e 62 64 2e 69 4b 65 79 2c 20 30 2c 20 26 75 2e  .bd.iKey, 0, &u.
1e0ef 62 64 2e 72 65 73 29 3b 0a 20 20 20 20 75 2e 62  bd.res);.    u.b
1e0f0 64 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 20  d.pC->lastRowid 
1e0f1 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20  = pIn3->u.i;.   
1e0f2 20 75 2e 62 64 2e 70 43 2d 3e 72 6f 77 69 64 49   u.bd.pC->rowidI
1e0f3 73 56 61 6c 69 64 20 3d 20 75 2e 62 64 2e 72 65  sValid = u.bd.re
1e0f4 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20 75  s==0 ?1:0;.    u
1e0f5 2e 62 64 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20  .bd.pC->nullRow 
1e0f6 3d 20 30 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43  = 0;.    u.bd.pC
1e0f7 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
1e0f8 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20  CACHE_STALE;.   
1e0f9 20 75 2e 62 64 2e 70 43 2d 3e 64 65 66 65 72 72   u.bd.pC->deferr
1e0fa 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20  edMoveto = 0;.  
1e0fb 20 20 69 66 28 20 75 2e 62 64 2e 72 65 73 21 3d    if( u.bd.res!=
1e0fc 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20  0 ){.      pc = 
1e0fd 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20  pOp->p2 - 1;.   
1e0fe 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e     assert( u.bd.
1e0ff 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
1e100 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ==0 );.    }.   
1e101 20 75 2e 62 64 2e 70 43 2d 3e 73 65 65 6b 52 65   u.bd.pC->seekRe
1e102 73 75 6c 74 20 3d 20 75 2e 62 64 2e 72 65 73 3b  sult = u.bd.res;
1e103 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
1e104 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 68   This happens wh
1e105 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f  en an attempt to
1e106 20 6f 70 65 6e 20 61 20 72 65 61 64 20 63 75 72   open a read cur
1e107 73 6f 72 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a  sor on the.    *
1e108 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  * sqlite_master 
1e109 74 61 62 6c 65 20 72 65 74 75 72 6e 73 20 53 51  table returns SQ
1e10a 4c 49 54 45 5f 45 4d 50 54 59 2e 0a 20 20 20 20  LITE_EMPTY..    
1e10b 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  */.    pc = pOp-
1e10c 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 61 73 73  >p2 - 1;.    ass
1e10d 65 72 74 28 20 75 2e 62 64 2e 70 43 2d 3e 72 6f  ert( u.bd.pC->ro
1e10e 77 69 64 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b  widIsValid==0 );
1e10f 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 73 65  .    u.bd.pC->se
1e110 65 6b 52 65 73 75 6c 74 20 3d 20 30 3b 0a 20 20  ekResult = 0;.  
1e111 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1e112 20 4f 70 63 6f 64 65 3a 20 53 65 71 75 65 6e 63   Opcode: Sequenc
1e113 65 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  e P1 P2 * * *.**
1e114 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 6e 65 78  .** Find the nex
1e115 74 20 61 76 61 69 6c 61 62 6c 65 20 73 65 71 75  t available sequ
1e116 65 6e 63 65 20 6e 75 6d 62 65 72 20 66 6f 72 20  ence number for 
1e117 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 57 72  cursor P1..** Wr
1e118 69 74 65 20 74 68 65 20 73 65 71 75 65 6e 63 65  ite the sequence
1e119 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 72 65 67   number into reg
1e11a 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20 54 68 65  ister P2..** The
1e11b 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72   sequence number
1e11c 20 6f 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69   on the cursor i
1e11d 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 61 66  s incremented af
1e11e 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e 73 74  ter this.** inst
1e11f 72 75 63 74 69 6f 6e 2e 20 20 0a 2a 2f 0a 63 61  ruction.  .*/.ca
1e120 73 65 20 4f 50 5f 53 65 71 75 65 6e 63 65 3a 20  se OP_Sequence: 
1e121 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f  {           /* o
1e122 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
1e123 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  /.  assert( pOp-
1e124 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
1e125 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  1<p->nCursor );.
1e126 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43    assert( p->apC
1e127 73 72 5b 70 4f 70 2d 3e 70 31 5d 21 3d 30 20 29  sr[pOp->p1]!=0 )
1e128 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20  ;.  pOut->u.i = 
1e129 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
1e12a 5d 2d 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b 0a 20  ]->seqCount++;. 
1e12b 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28   MemSetTypeFlag(
1e12c 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a  pOut, MEM_Int);.
1e12d 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20    break;.}.../* 
1e12e 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f 77 69 64  Opcode: NewRowid
1e12f 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1e130 0a 2a 2a 20 47 65 74 20 61 20 6e 65 77 20 69 6e  .** Get a new in
1e131 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e 75 6d  teger record num
1e132 62 65 72 20 28 61 2e 6b 2e 61 20 22 72 6f 77 69  ber (a.k.a "rowi
1e133 64 22 29 20 75 73 65 64 20 61 73 20 74 68 65 20  d") used as the 
1e134 6b 65 79 20 74 6f 20 61 20 74 61 62 6c 65 2e 0a  key to a table..
1e135 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75  ** The record nu
1e136 6d 62 65 72 20 69 73 20 6e 6f 74 20 70 72 65 76  mber is not prev
1e137 69 6f 75 73 6c 79 20 75 73 65 64 20 61 73 20 61  iously used as a
1e138 20 6b 65 79 20 69 6e 20 74 68 65 20 64 61 74 61   key in the data
1e139 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 74 68  base.** table th
1e13a 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 6f 69  at cursor P1 poi
1e13b 6e 74 73 20 74 6f 2e 20 20 54 68 65 20 6e 65 77  nts to.  The new
1e13c 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69   record number i
1e13d 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 77 72 69  s written.** wri
1e13e 74 74 65 6e 20 74 6f 20 72 65 67 69 73 74 65 72  tten to register
1e13f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 33   P2..**.** If P3
1e140 3e 30 20 74 68 65 6e 20 50 33 20 69 73 20 61 20  >0 then P3 is a 
1e141 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 65 20  register in the 
1e142 72 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20 74 68  root frame of th
1e143 69 73 20 56 44 42 45 20 74 68 61 74 20 68 6f 6c  is VDBE that hol
1e144 64 73 20 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65  ds .** the large
1e145 73 74 20 70 72 65 76 69 6f 75 73 6c 79 20 67 65  st previously ge
1e146 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64 20 6e  nerated record n
1e147 75 6d 62 65 72 2e 20 4e 6f 20 6e 65 77 20 72 65  umber. No new re
1e148 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 61 72 65  cord numbers are
1e149 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62  .** allowed to b
1e14a 65 20 6c 65 73 73 20 74 68 61 6e 20 74 68 69 73  e less than this
1e14b 20 76 61 6c 75 65 2e 20 57 68 65 6e 20 74 68 69   value. When thi
1e14c 73 20 76 61 6c 75 65 20 72 65 61 63 68 65 73 20  s value reaches 
1e14d 69 74 73 20 6d 61 78 69 6d 75 6d 2c 20 0a 2a 2a  its maximum, .**
1e14e 20 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 65   a SQLITE_FULL e
1e14f 72 72 6f 72 20 69 73 20 67 65 6e 65 72 61 74 65  rror is generate
1e150 64 2e 20 54 68 65 20 50 33 20 72 65 67 69 73 74  d. The P3 regist
1e151 65 72 20 69 73 20 75 70 64 61 74 65 64 20 77 69  er is updated wi
1e152 74 68 20 74 68 65 20 27 0a 2a 2a 20 67 65 6e 65  th the '.** gene
1e153 72 61 74 65 64 20 72 65 63 6f 72 64 20 6e 75 6d  rated record num
1e154 62 65 72 2e 20 54 68 69 73 20 50 33 20 6d 65 63  ber. This P3 mec
1e155 68 61 6e 69 73 6d 20 69 73 20 75 73 65 64 20 74  hanism is used t
1e156 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74  o help implement
1e157 20 74 68 65 0a 2a 2a 20 41 55 54 4f 49 4e 43 52   the.** AUTOINCR
1e158 45 4d 45 4e 54 20 66 65 61 74 75 72 65 2e 0a 2a  EMENT feature..*
1e159 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 77 52 6f 77  /.case OP_NewRow
1e15a 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  id: {           
1e15b 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61  /* out2-prerelea
1e15c 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20  se */.#if 0  /* 
1e15d 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1e15e 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 65 20  moved into u.be 
1e15f 2a 2f 0a 20 20 69 36 34 20 76 3b 20 20 20 20 20  */.  i64 v;     
1e160 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1e161 68 65 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a  he new rowid */.
1e162 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1e163 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73  ;        /* Curs
1e164 6f 72 20 6f 66 20 74 61 62 6c 65 20 74 6f 20 67  or of table to g
1e165 65 74 20 74 68 65 20 6e 65 77 20 72 6f 77 69 64  et the new rowid
1e166 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 3b 20 20   */.  int res;  
1e167 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1e168 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 73 71 6c  Result of an sql
1e169 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 29 20  ite3BtreeLast() 
1e16a 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20  */.  int cnt;   
1e16b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
1e16c 6f 75 6e 74 65 72 20 74 6f 20 6c 69 6d 69 74 20  ounter to limit 
1e16d 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65  the number of se
1e16e 61 72 63 68 65 73 20 2a 2f 0a 20 20 4d 65 6d 20  arches */.  Mem 
1e16f 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20  *pMem;          
1e170 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68     /* Register h
1e171 6f 6c 64 69 6e 67 20 6c 61 72 67 65 73 74 20 72  olding largest r
1e172 6f 77 69 64 20 66 6f 72 20 41 55 54 4f 49 4e 43  owid for AUTOINC
1e173 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 56 64 62 65  REMENT */.  Vdbe
1e174 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20  Frame *pFrame;  
1e175 20 20 20 2f 2a 20 52 6f 6f 74 20 66 72 61 6d 65     /* Root frame
1e176 20 6f 66 20 56 44 42 45 20 2a 2f 0a 23 65 6e 64   of VDBE */.#end
1e177 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1e178 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e179 20 75 2e 62 65 20 2a 2f 0a 0a 20 20 75 2e 62 65   u.be */..  u.be
1e17a 2e 76 20 3d 20 30 3b 0a 20 20 75 2e 62 65 2e 72  .v = 0;.  u.be.r
1e17b 65 73 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  es = 0;.  assert
1e17c 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1e17d 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73  pOp->p1<p->nCurs
1e17e 6f 72 20 29 3b 0a 20 20 75 2e 62 65 2e 70 43 20  or );.  u.be.pC 
1e17f 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  = p->apCsr[pOp->
1e180 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
1e181 2e 62 65 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69  .be.pC!=0 );.  i
1e182 66 28 20 4e 45 56 45 52 28 75 2e 62 65 2e 70 43  f( NEVER(u.be.pC
1e183 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 29 20 29 7b  ->pCursor==0) ){
1e184 0a 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 72 6f  .    /* The zero
1e185 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
1e186 61 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 68 61  above is all tha
1e187 74 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20  t is needed */. 
1e188 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54   }else{.    /* T
1e189 68 65 20 6e 65 78 74 20 72 6f 77 69 64 20 6f 72  he next rowid or
1e18a 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 28   record number (
1e18b 64 69 66 66 65 72 65 6e 74 20 74 65 72 6d 73 20  different terms 
1e18c 66 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20  for the same.   
1e18d 20 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 6f 62   ** thing) is ob
1e18e 74 61 69 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d  tained in a two-
1e18f 73 74 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0a  step algorithm..
1e190 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69      **.    ** Fi
1e191 72 73 74 20 77 65 20 61 74 74 65 6d 70 74 20 74  rst we attempt t
1e192 6f 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65  o find the large
1e193 73 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69  st existing rowi
1e194 64 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20 20  d and add one.  
1e195 20 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20 42    ** to that.  B
1e196 75 74 20 69 66 20 74 68 65 20 6c 61 72 67 65 73  ut if the larges
1e197 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64  t existing rowid
1e198 20 69 73 20 61 6c 72 65 61 64 79 20 74 68 65 20   is already the 
1e199 6d 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 70  maximum.    ** p
1e19a 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2c  ositive integer,
1e19b 20 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c 6c   we have to fall
1e19c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20   through to the 
1e19d 73 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 72  second.    ** pr
1e19e 6f 62 61 62 69 6c 69 73 74 69 63 20 61 6c 67 6f  obabilistic algo
1e19f 72 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 20  rithm.    **.   
1e1a0 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61   ** The second a
1e1a1 6c 67 6f 72 69 74 68 6d 20 69 73 20 74 6f 20 73  lgorithm is to s
1e1a2 65 6c 65 63 74 20 61 20 72 6f 77 69 64 20 61 74  elect a rowid at
1e1a3 20 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65 20   random and see 
1e1a4 69 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c 72  if.    ** it alr
1e1a5 65 61 64 79 20 65 78 69 73 74 73 20 69 6e 20 74  eady exists in t
1e1a6 68 65 20 74 61 62 6c 65 2e 20 20 49 66 20 69 74  he table.  If it
1e1a7 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c   does not exist,
1e1a8 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20   we have.    ** 
1e1a9 73 75 63 63 65 65 64 65 64 2e 20 20 49 66 20 74  succeeded.  If t
1e1aa 68 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20  he random rowid 
1e1ab 64 6f 65 73 20 65 78 69 73 74 2c 20 77 65 20 73  does exist, we s
1e1ac 65 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e 65 0a  elect a new one.
1e1ad 20 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 20 61      ** and try a
1e1ae 67 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 30 20  gain, up to 100 
1e1af 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  times..    */.  
1e1b0 20 20 61 73 73 65 72 74 28 20 75 2e 62 65 2e 70    assert( u.be.p
1e1b1 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20  C->isTable );.  
1e1b2 20 20 75 2e 62 65 2e 63 6e 74 20 3d 20 30 3b 0a    u.be.cnt = 0;.
1e1b3 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 33  .#ifdef SQLITE_3
1e1b4 32 42 49 54 5f 52 4f 57 49 44 0a 23 20 20 20 64  2BIT_ROWID.#   d
1e1b5 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20  efine MAX_ROWID 
1e1b6 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c 73 65  0x7fffffff.#else
1e1b7 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d  .    /* Some com
1e1b8 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20  pilers complain 
1e1b9 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e 74 73 20  about constants 
1e1ba 6f 66 20 74 68 65 20 66 6f 72 6d 20 30 78 37 66  of the form 0x7f
1e1bb 66 66 66 66 66 66 66 66 66 66 66 66 66 66 2e 0a  ffffffffffffff..
1e1bc 20 20 20 20 2a 2a 20 4f 74 68 65 72 73 20 63 6f      ** Others co
1e1bd 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 30 78 37  mplain about 0x7
1e1be 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66  ffffffffffffffff
1e1bf 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  LL.  The followi
1e1c0 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d 73 0a 20  ng macro seems. 
1e1c1 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76 69 64 65     ** to provide
1e1c2 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 77 68   the constant wh
1e1c3 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c 6c 20 63  ile making all c
1e1c4 6f 6d 70 69 6c 65 72 73 20 68 61 70 70 79 2e 0a  ompilers happy..
1e1c5 20 20 20 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e      */.#   defin
1e1c6 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28 69 36  e MAX_ROWID  (i6
1e1c7 34 29 28 20 28 28 28 75 36 34 29 30 78 37 66 66  4)( (((u64)0x7ff
1e1c8 66 66 66 66 66 29 3c 3c 33 32 29 20 7c 20 28 75  fffff)<<32) | (u
1e1c9 36 34 29 30 78 66 66 66 66 66 66 66 66 20 29 0a  64)0xffffffff ).
1e1ca 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20  #endif..    if( 
1e1cb 21 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e  !u.be.pC->useRan
1e1cc 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20  domRowid ){.    
1e1cd 20 20 75 2e 62 65 2e 76 20 3d 20 73 71 6c 69 74    u.be.v = sqlit
1e1ce 65 33 42 74 72 65 65 47 65 74 43 61 63 68 65 64  e3BtreeGetCached
1e1cf 52 6f 77 69 64 28 75 2e 62 65 2e 70 43 2d 3e 70  Rowid(u.be.pC->p
1e1d0 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 69  Cursor);.      i
1e1d1 66 28 20 75 2e 62 65 2e 76 3d 3d 30 20 29 7b 0a  f( u.be.v==0 ){.
1e1d2 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1e1d3 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 75 2e  ite3BtreeLast(u.
1e1d4 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  be.pC->pCursor, 
1e1d5 26 75 2e 62 65 2e 72 65 73 29 3b 0a 20 20 20 20  &u.be.res);.    
1e1d6 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1e1d7 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1e1d8 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
1e1d9 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
1e1da 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
1e1db 28 20 75 2e 62 65 2e 72 65 73 20 29 7b 0a 20 20  ( u.be.res ){.  
1e1dc 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 20 3d          u.be.v =
1e1dd 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73   1;.        }els
1e1de 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  e{.          ass
1e1df 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1e1e0 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75  eCursorIsValid(u
1e1e1 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29  .be.pC->pCursor)
1e1e2 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63   );.          rc
1e1e3 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b   = sqlite3BtreeK
1e1e4 65 79 53 69 7a 65 28 75 2e 62 65 2e 70 43 2d 3e  eySize(u.be.pC->
1e1e5 70 43 75 72 73 6f 72 2c 20 26 75 2e 62 65 2e 76  pCursor, &u.be.v
1e1e6 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  );.          ass
1e1e7 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
1e1e8 4f 4b 20 29 3b 20 20 20 2f 2a 20 43 61 6e 6e 6f  OK );   /* Canno
1e1e9 74 20 66 61 69 6c 20 66 6f 6c 6c 6f 77 69 6e 67  t fail following
1e1ea 20 42 74 72 65 65 4c 61 73 74 28 29 20 2a 2f 0a   BtreeLast() */.
1e1eb 20 20 20 20 20 20 20 20 20 20 69 66 28 20 75 2e            if( u.
1e1ec 62 65 2e 76 3d 3d 4d 41 58 5f 52 4f 57 49 44 20  be.v==MAX_ROWID 
1e1ed 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 75  ){.            u
1e1ee 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e 64 6f  .be.pC->useRando
1e1ef 6d 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20  mRowid = 1;.    
1e1f0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1e1f1 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 2b           u.be.v+
1e1f2 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  +;.          }. 
1e1f3 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1e1f4 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1e1f5 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d  _OMIT_AUTOINCREM
1e1f6 45 4e 54 0a 20 20 20 20 20 20 69 66 28 20 70 4f  ENT.      if( pO
1e1f7 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20 20 20 20  p->p3 ){.       
1e1f8 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20   /* Assert that 
1e1f9 50 33 20 69 73 20 61 20 76 61 6c 69 64 20 6d 65  P3 is a valid me
1e1fa 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20  mory cell. */.  
1e1fb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1e1fc 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20  p->p3>0 );.     
1e1fd 20 20 20 69 66 28 20 70 2d 3e 70 46 72 61 6d 65     if( p->pFrame
1e1fe 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f   ){.          fo
1e1ff 72 28 75 2e 62 65 2e 70 46 72 61 6d 65 3d 70 2d  r(u.be.pFrame=p-
1e200 3e 70 46 72 61 6d 65 3b 20 75 2e 62 65 2e 70 46  >pFrame; u.be.pF
1e201 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 3b 20 75  rame->pParent; u
1e202 2e 62 65 2e 70 46 72 61 6d 65 3d 75 2e 62 65 2e  .be.pFrame=u.be.
1e203 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29  pFrame->pParent)
1e204 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  ;.          /* A
1e205 73 73 65 72 74 20 74 68 61 74 20 50 33 20 69 73  ssert that P3 is
1e206 20 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20   a valid memory 
1e207 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20  cell. */.       
1e208 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1e209 70 33 3c 3d 75 2e 62 65 2e 70 46 72 61 6d 65 2d  p3<=u.be.pFrame-
1e20a 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20  >nMem );.       
1e20b 20 20 20 75 2e 62 65 2e 70 4d 65 6d 20 3d 20 26     u.be.pMem = &
1e20c 75 2e 62 65 2e 70 46 72 61 6d 65 2d 3e 61 4d 65  u.be.pFrame->aMe
1e20d 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20  m[pOp->p3];.    
1e20e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1e20f 20 20 20 20 20 2f 2a 20 41 73 73 65 72 74 20 74       /* Assert t
1e210 68 61 74 20 50 33 20 69 73 20 61 20 76 61 6c 69  hat P3 is a vali
1e211 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a  d memory cell. *
1e212 2f 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  /.          asse
1e213 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e  rt( pOp->p3<=p->
1e214 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20  nMem );.        
1e215 20 20 75 2e 62 65 2e 70 4d 65 6d 20 3d 20 26 70    u.be.pMem = &p
1e216 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b  ->aMem[pOp->p3];
1e217 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  .        }..    
1e218 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41      REGISTER_TRA
1e219 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 62 65  CE(pOp->p3, u.be
1e21a 2e 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20  .pMem);.        
1e21b 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e  sqlite3VdbeMemIn
1e21c 74 65 67 65 72 69 66 79 28 75 2e 62 65 2e 70 4d  tegerify(u.be.pM
1e21d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  em);.        ass
1e21e 65 72 74 28 20 28 75 2e 62 65 2e 70 4d 65 6d 2d  ert( (u.be.pMem-
1e21f 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74  >flags & MEM_Int
1e220 29 21 3d 30 20 29 3b 20 20 2f 2a 20 6d 65 6d 28  )!=0 );  /* mem(
1e221 50 33 29 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74  P3) holds an int
1e222 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20  eger */.        
1e223 69 66 28 20 75 2e 62 65 2e 70 4d 65 6d 2d 3e 75  if( u.be.pMem->u
1e224 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 7c 7c  .i==MAX_ROWID ||
1e225 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e   u.be.pC->useRan
1e226 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20  domRowid ){.    
1e227 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1e228 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20  E_FULL;.        
1e229 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65    goto abort_due
1e22a 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20  _to_error;.     
1e22b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
1e22c 20 75 2e 62 65 2e 76 3c 75 2e 62 65 2e 70 4d 65   u.be.v<u.be.pMe
1e22d 6d 2d 3e 75 2e 69 2b 31 20 29 7b 0a 20 20 20 20  m->u.i+1 ){.    
1e22e 20 20 20 20 20 20 75 2e 62 65 2e 76 20 3d 20 75        u.be.v = u
1e22f 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 20 2b 20  .be.pMem->u.i + 
1e230 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
1e231 20 20 20 20 20 75 2e 62 65 2e 70 4d 65 6d 2d 3e       u.be.pMem->
1e232 75 2e 69 20 3d 20 75 2e 62 65 2e 76 3b 0a 20 20  u.i = u.be.v;.  
1e233 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20      }.#endif..  
1e234 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1e235 53 65 74 43 61 63 68 65 64 52 6f 77 69 64 28 75  SetCachedRowid(u
1e236 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c  .be.pC->pCursor,
1e237 20 75 2e 62 65 2e 76 3c 4d 41 58 5f 52 4f 57 49   u.be.v<MAX_ROWI
1e238 44 20 3f 20 75 2e 62 65 2e 76 2b 31 20 3a 20 30  D ? u.be.v+1 : 0
1e239 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
1e23a 20 75 2e 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e   u.be.pC->useRan
1e23b 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20  domRowid ){.    
1e23c 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1e23d 33 3d 3d 30 20 29 3b 20 20 2f 2a 20 57 65 20 63  3==0 );  /* We c
1e23e 61 6e 6e 6f 74 20 62 65 20 69 6e 20 72 61 6e 64  annot be in rand
1e23f 6f 6d 20 72 6f 77 69 64 20 6d 6f 64 65 20 69 66  om rowid mode if
1e240 20 74 68 69 73 20 69 73 0a 20 20 20 20 20 20 20   this is.       
1e241 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e242 20 20 20 20 20 20 2a 2a 20 61 6e 20 41 55 54 4f        ** an AUTO
1e243 49 4e 43 52 45 4d 45 4e 54 20 74 61 62 6c 65 2e  INCREMENT table.
1e244 20 2a 2f 0a 20 20 20 20 20 20 75 2e 62 65 2e 76   */.      u.be.v
1e245 20 3d 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64   = db->lastRowid
1e246 3b 0a 20 20 20 20 20 20 75 2e 62 65 2e 63 6e 74  ;.      u.be.cnt
1e247 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f 7b 0a   = 0;.      do{.
1e248 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 62 65          if( u.be
1e249 2e 63 6e 74 3d 3d 30 20 26 26 20 28 75 2e 62 65  .cnt==0 && (u.be
1e24a 2e 76 26 30 78 66 66 66 66 66 66 29 3d 3d 75 2e  .v&0xffffff)==u.
1e24b 62 65 2e 76 20 29 7b 0a 20 20 20 20 20 20 20 20  be.v ){.        
1e24c 20 20 75 2e 62 65 2e 76 2b 2b 3b 0a 20 20 20 20    u.be.v++;.    
1e24d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1e24e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e       sqlite3_ran
1e24f 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 75  domness(sizeof(u
1e250 2e 62 65 2e 76 29 2c 20 26 75 2e 62 65 2e 76 29  .be.v), &u.be.v)
1e251 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
1e252 75 2e 62 65 2e 63 6e 74 3c 35 20 29 20 75 2e 62  u.be.cnt<5 ) u.b
1e253 65 2e 76 20 26 3d 20 30 78 66 66 66 66 66 66 3b  e.v &= 0xffffff;
1e254 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1e255 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1e256 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b  treeMovetoUnpack
1e257 65 64 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72  ed(u.be.pC->pCur
1e258 73 6f 72 2c 20 30 2c 20 28 75 36 34 29 75 2e 62  sor, 0, (u64)u.b
1e259 65 2e 76 2c 20 30 2c 20 26 75 2e 62 65 2e 72 65  e.v, 0, &u.be.re
1e25a 73 29 3b 0a 20 20 20 20 20 20 20 20 75 2e 62 65  s);.        u.be
1e25b 2e 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 77  .cnt++;.      }w
1e25c 68 69 6c 65 28 20 75 2e 62 65 2e 63 6e 74 3c 31  hile( u.be.cnt<1
1e25d 30 30 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45  00 && rc==SQLITE
1e25e 5f 4f 4b 20 26 26 20 75 2e 62 65 2e 72 65 73 3d  _OK && u.be.res=
1e25f 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  =0 );.      if( 
1e260 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1e261 20 75 2e 62 65 2e 72 65 73 3d 3d 30 20 29 7b 0a   u.be.res==0 ){.
1e262 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
1e263 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20  ITE_FULL;.      
1e264 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65    goto abort_due
1e265 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20  _to_error;.     
1e266 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 62   }.    }.    u.b
1e267 65 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c  e.pC->rowidIsVal
1e268 69 64 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 65  id = 0;.    u.be
1e269 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76  .pC->deferredMov
1e26a 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62  eto = 0;.    u.b
1e26b 65 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75  e.pC->cacheStatu
1e26c 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
1e26d 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79 70  .  }.  MemSetTyp
1e26e 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f  eFlag(pOut, MEM_
1e26f 49 6e 74 29 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e  Int);.  pOut->u.
1e270 69 20 3d 20 75 2e 62 65 2e 76 3b 0a 20 20 62 72  i = u.be.v;.  br
1e271 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1e272 65 3a 20 49 6e 73 65 72 74 20 50 31 20 50 32 20  e: Insert P1 P2 
1e273 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 57  P3 P4 P5.**.** W
1e274 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e  rite an entry in
1e275 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f 66 20  to the table of 
1e276 63 75 72 73 6f 72 20 50 31 2e 20 20 41 20 6e 65  cursor P1.  A ne
1e277 77 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20 63 72  w entry is.** cr
1e278 65 61 74 65 64 20 69 66 20 69 74 20 64 6f 65 73  eated if it does
1e279 6e 27 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  n't already exis
1e27a 74 20 6f 72 20 74 68 65 20 64 61 74 61 20 66 6f  t or the data fo
1e27b 72 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a  r an existing.**
1e27c 20 65 6e 74 72 79 20 69 73 20 6f 76 65 72 77 72   entry is overwr
1e27d 69 74 74 65 6e 2e 20 20 54 68 65 20 64 61 74 61  itten.  The data
1e27e 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 4d 45   is the value ME
1e27f 4d 5f 42 6c 6f 62 20 73 74 6f 72 65 64 20 69 6e  M_Blob stored in
1e280 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6e 75 6d   register.** num
1e281 62 65 72 20 50 32 2e 20 54 68 65 20 6b 65 79 20  ber P2. The key 
1e282 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67  is stored in reg
1e283 69 73 74 65 72 20 50 33 2e 20 54 68 65 20 6b 65  ister P3. The ke
1e284 79 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61 20 4d  y must.** be a M
1e285 45 4d 5f 49 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  EM_Int..**.** If
1e286 20 74 68 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41   the OPFLAG_NCHA
1e287 4e 47 45 20 66 6c 61 67 20 6f 66 20 50 35 20 69  NGE flag of P5 i
1e288 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20  s set, then the 
1e289 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e 74  row change count
1e28a 20 69 73 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74   is.** increment
1e28b 65 64 20 28 6f 74 68 65 72 77 69 73 65 20 6e 6f  ed (otherwise no
1e28c 74 29 2e 20 20 49 66 20 74 68 65 20 4f 50 46 4c  t).  If the OPFL
1e28d 41 47 5f 4c 41 53 54 52 4f 57 49 44 20 66 6c 61  AG_LASTROWID fla
1e28e 67 20 6f 66 20 50 35 20 69 73 20 73 65 74 2c 0a  g of P5 is set,.
1e28f 2a 2a 20 74 68 65 6e 20 72 6f 77 69 64 20 69 73  ** then rowid is
1e290 20 73 74 6f 72 65 64 20 66 6f 72 20 73 75 62 73   stored for subs
1e291 65 71 75 65 6e 74 20 72 65 74 75 72 6e 20 62 79  equent return by
1e292 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   the.** sqlite3_
1e293 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
1e294 64 28 29 20 66 75 6e 63 74 69 6f 6e 20 28 6f 74  d() function (ot
1e295 68 65 72 77 69 73 65 20 69 74 20 69 73 20 75 6e  herwise it is un
1e296 6d 6f 64 69 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a  modified)..**.**
1e297 20 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 55   If the OPFLAG_U
1e298 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66 6c 61  SESEEKRESULT fla
1e299 67 20 6f 66 20 50 35 20 69 73 20 73 65 74 20 61  g of P5 is set a
1e29a 6e 64 20 69 66 20 74 68 65 20 72 65 73 75 6c 74  nd if the result
1e29b 20 6f 66 0a 2a 2a 20 74 68 65 20 6c 61 73 74 20   of.** the last 
1e29c 73 65 65 6b 20 6f 70 65 72 61 74 69 6f 6e 20 28  seek operation (
1e29d 4f 50 5f 4e 6f 74 45 78 69 73 74 73 29 20 77 61  OP_NotExists) wa
1e29e 73 20 61 20 73 75 63 63 65 73 73 2c 20 74 68 65  s a success, the
1e29f 6e 20 74 68 69 73 0a 2a 2a 20 6f 70 65 72 61 74  n this.** operat
1e2a0 69 6f 6e 20 77 69 6c 6c 20 6e 6f 74 20 61 74 74  ion will not att
1e2a1 65 6d 70 74 20 74 6f 20 66 69 6e 64 20 74 68 65  empt to find the
1e2a2 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 6f 77   appropriate row
1e2a3 20 62 65 66 6f 72 65 20 64 6f 69 6e 67 0a 2a 2a   before doing.**
1e2a4 20 74 68 65 20 69 6e 73 65 72 74 20 62 75 74 20   the insert but 
1e2a5 77 69 6c 6c 20 69 6e 73 74 65 61 64 20 6f 76 65  will instead ove
1e2a6 72 77 72 69 74 65 20 74 68 65 20 72 6f 77 20 74  rwrite the row t
1e2a7 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 69  hat the cursor i
1e2a8 73 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 70  s.** currently p
1e2a9 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 50 72 65  ointing to.  Pre
1e2aa 73 75 6d 61 62 6c 79 2c 20 74 68 65 20 70 72 69  sumably, the pri
1e2ab 6f 72 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20  or OP_NotExists 
1e2ac 6f 70 63 6f 64 65 0a 2a 2a 20 68 61 73 20 61 6c  opcode.** has al
1e2ad 72 65 61 64 79 20 70 6f 73 69 74 69 6f 6e 65 64  ready positioned
1e2ae 20 74 68 65 20 63 75 72 73 6f 72 20 63 6f 72 72   the cursor corr
1e2af 65 63 74 6c 79 2e 20 20 54 68 69 73 20 69 73 20  ectly.  This is 
1e2b0 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a  an optimization.
1e2b1 2a 2a 20 74 68 61 74 20 62 6f 6f 73 74 73 20 70  ** that boosts p
1e2b2 65 72 66 6f 72 6d 61 6e 63 65 20 62 79 20 61 76  erformance by av
1e2b3 6f 69 64 69 6e 67 20 72 65 64 75 6e 64 61 6e 74  oiding redundant
1e2b4 20 73 65 65 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66   seeks..**.** If
1e2b5 20 74 68 65 20 4f 50 46 4c 41 47 5f 49 53 55 50   the OPFLAG_ISUP
1e2b6 44 41 54 45 20 66 6c 61 67 20 69 73 20 73 65 74  DATE flag is set
1e2b7 2c 20 74 68 65 6e 20 74 68 69 73 20 6f 70 63 6f  , then this opco
1e2b8 64 65 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e  de is part of an
1e2b9 0a 2a 2a 20 55 50 44 41 54 45 20 6f 70 65 72 61  .** UPDATE opera
1e2ba 74 69 6f 6e 2e 20 20 4f 74 68 65 72 77 69 73 65  tion.  Otherwise
1e2bb 20 28 69 66 20 74 68 65 20 66 6c 61 67 20 69 73   (if the flag is
1e2bc 20 63 6c 65 61 72 29 20 74 68 65 6e 20 74 68 69   clear) then thi
1e2bd 73 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 20 70  s opcode.** is p
1e2be 61 72 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 54  art of an INSERT
1e2bf 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 54 68 65   operation.  The
1e2c0 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 20 6f   difference is o
1e2c1 6e 6c 79 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f  nly important to
1e2c2 0a 2a 2a 20 74 68 65 20 75 70 64 61 74 65 20 68  .** the update h
1e2c3 6f 6f 6b 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d  ook..**.** Param
1e2c4 65 74 65 72 20 50 34 20 6d 61 79 20 70 6f 69 6e  eter P4 may poin
1e2c5 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 63 6f  t to a string co
1e2c6 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62  ntaining the tab
1e2c7 6c 65 2d 6e 61 6d 65 2c 20 6f 72 0a 2a 2a 20 6d  le-name, or.** m
1e2c8 61 79 20 62 65 20 4e 55 4c 4c 2e 20 49 66 20 69  ay be NULL. If i
1e2c9 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74  t is not NULL, t
1e2ca 68 65 6e 20 74 68 65 20 75 70 64 61 74 65 2d 68  hen the update-h
1e2cb 6f 6f 6b 20 0a 2a 2a 20 28 73 71 6c 69 74 65 33  ook .** (sqlite3
1e2cc 2e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b  .xUpdateCallback
1e2cd 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 6f 6c  ) is invoked fol
1e2ce 6c 6f 77 69 6e 67 20 61 20 73 75 63 63 65 73 73  lowing a success
1e2cf 66 75 6c 20 69 6e 73 65 72 74 2e 0a 2a 2a 0a 2a  ful insert..**.*
1e2d0 2a 20 28 57 41 52 4e 49 4e 47 2f 54 4f 44 4f 3a  * (WARNING/TODO:
1e2d1 20 49 66 20 50 31 20 69 73 20 61 20 70 73 65 75   If P1 is a pseu
1e2d2 64 6f 2d 63 75 72 73 6f 72 20 61 6e 64 20 50 32  do-cursor and P2
1e2d3 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a   is dynamically.
1e2d4 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68  ** allocated, th
1e2d5 65 6e 20 6f 77 6e 65 72 73 68 69 70 20 6f 66 20  en ownership of 
1e2d6 50 32 20 69 73 20 74 72 61 6e 73 66 65 72 72 65  P2 is transferre
1e2d7 64 20 74 6f 20 74 68 65 20 70 73 65 75 64 6f 2d  d to the pseudo-
1e2d8 63 75 72 73 6f 72 0a 2a 2a 20 61 6e 64 20 72 65  cursor.** and re
1e2d9 67 69 73 74 65 72 20 50 32 20 62 65 63 6f 6d 65  gister P2 become
1e2da 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 49 66  s ephemeral.  If
1e2db 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 63   the cursor is c
1e2dc 68 61 6e 67 65 64 2c 20 74 68 65 0a 2a 2a 20 76  hanged, the.** v
1e2dd 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65 72  alue of register
1e2de 20 50 32 20 77 69 6c 6c 20 74 68 65 6e 20 63 68   P2 will then ch
1e2df 61 6e 67 65 2e 20 20 4d 61 6b 65 20 73 75 72 65  ange.  Make sure
1e2e0 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 0a 2a   this does not.*
1e2e1 2a 20 63 61 75 73 65 20 61 6e 79 20 70 72 6f 62  * cause any prob
1e2e2 6c 65 6d 73 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 69  lems.).**.** Thi
1e2e3 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 6e  s instruction on
1e2e4 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 74 61 62 6c  ly works on tabl
1e2e5 65 73 2e 20 20 54 68 65 20 65 71 75 69 76 61 6c  es.  The equival
1e2e6 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a  ent instruction.
1e2e7 2a 2a 20 66 6f 72 20 69 6e 64 69 63 65 73 20 69  ** for indices i
1e2e8 73 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2e 0a  s OP_IdxInsert..
1e2e9 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 73 65 72  */.case OP_Inser
1e2ea 74 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  t: {.#if 0  /* l
1e2eb 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e2ec 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 66 20 2a  oved into u.bf *
1e2ed 2f 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61 3b 20  /.  Mem *pData; 
1e2ee 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 6c        /* MEM cel
1e2ef 6c 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 20 66  l holding data f
1e2f0 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f  or the record to
1e2f1 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   be inserted */.
1e2f2 20 20 4d 65 6d 20 2a 70 4b 65 79 3b 20 20 20 20    Mem *pKey;    
1e2f3 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20      /* MEM cell 
1e2f4 68 6f 6c 64 69 6e 67 20 6b 65 79 20 20 66 6f 72  holding key  for
1e2f5 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20   the record */. 
1e2f6 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20   i64 iKey;      
1e2f7 20 20 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 65     /* The intege
1e2f8 72 20 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 66  r ROWID or key f
1e2f9 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f  or the record to
1e2fa 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   be inserted */.
1e2fb 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1e2fc 3b 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f  ;   /* Cursor to
1e2fd 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
1e2fe 68 20 69 6e 73 65 72 74 20 69 73 20 77 72 69 74  h insert is writ
1e2ff 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65  ten */.  int nZe
1e300 72 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ro;        /* Nu
1e301 6d 62 65 72 20 6f 66 20 7a 65 72 6f 2d 62 79 74  mber of zero-byt
1e302 65 73 20 74 6f 20 61 70 70 65 6e 64 20 2a 2f 0a  es to append */.
1e303 20 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c 74    int seekResult
1e304 3b 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66  ;   /* Result of
1e305 20 70 72 69 6f 72 20 73 65 65 6b 20 6f 72 20 30   prior seek or 0
1e306 20 69 66 20 6e 6f 20 55 53 45 53 45 45 4b 52 45   if no USESEEKRE
1e307 53 55 4c 54 20 66 6c 61 67 20 2a 2f 0a 20 20 63  SULT flag */.  c
1e308 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20  onst char *zDb; 
1e309 20 2f 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d   /* database nam
1e30a 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 65 20  e - used by the 
1e30b 75 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20  update hook */. 
1e30c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 62   const char *zTb
1e30d 6c 3b 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65  l; /* Table name
1e30e 20 2d 20 75 73 65 64 20 62 79 20 74 68 65 20 6f   - used by the o
1e30f 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20  pdate hook */.  
1e310 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20  int op;         
1e311 20 20 2f 2a 20 4f 70 63 6f 64 65 20 66 6f 72 20    /* Opcode for 
1e312 75 70 64 61 74 65 20 68 6f 6f 6b 3a 20 53 51 4c  update hook: SQL
1e313 49 54 45 5f 55 50 44 41 54 45 20 6f 72 20 53 51  ITE_UPDATE or SQ
1e314 4c 49 54 45 5f 49 4e 53 45 52 54 20 2a 2f 0a 23  LITE_INSERT */.#
1e315 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1e316 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1e317 6e 74 6f 20 75 2e 62 66 20 2a 2f 0a 0a 20 20 75  nto u.bf */..  u
1e318 2e 62 66 2e 70 44 61 74 61 20 3d 20 26 70 2d 3e  .bf.pData = &p->
1e319 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
1e31a 20 75 2e 62 66 2e 70 4b 65 79 20 3d 20 26 70 2d   u.bf.pKey = &p-
1e31b 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  >aMem[pOp->p3];.
1e31c 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1e31d 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
1e31e 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1e31f 75 2e 62 66 2e 70 43 20 3d 20 70 2d 3e 61 70 43  u.bf.pC = p->apC
1e320 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61  sr[pOp->p1];.  a
1e321 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 21 3d  ssert( u.bf.pC!=
1e322 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75  0 );.  assert( u
1e323 2e 62 66 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21  .bf.pC->pCursor!
1e324 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1e325 75 2e 62 66 2e 70 43 2d 3e 70 73 65 75 64 6f 54  u.bf.pC->pseudoT
1e326 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20 20  ableReg==0 );.  
1e327 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 4b 65  assert( u.bf.pKe
1e328 79 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  y->flags & MEM_I
1e329 6e 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  nt );.  assert( 
1e32a 75 2e 62 66 2e 70 43 2d 3e 69 73 54 61 62 6c 65  u.bf.pC->isTable
1e32b 20 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54   );.  REGISTER_T
1e32c 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 75 2e  RACE(pOp->p2, u.
1e32d 62 66 2e 70 44 61 74 61 29 3b 0a 20 20 52 45 47  bf.pData);.  REG
1e32e 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1e32f 3e 70 33 2c 20 75 2e 62 66 2e 70 4b 65 79 29 3b  >p3, u.bf.pKey);
1e330 0a 0a 20 20 75 2e 62 66 2e 69 4b 65 79 20 3d 20  ..  u.bf.iKey = 
1e331 75 2e 62 66 2e 70 4b 65 79 2d 3e 75 2e 69 3b 0a  u.bf.pKey->u.i;.
1e332 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20    if( pOp->p5 & 
1e333 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29  OPFLAG_NCHANGE )
1e334 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20   p->nChange++;. 
1e335 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 4f   if( pOp->p5 & O
1e336 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20  PFLAG_LASTROWID 
1e337 29 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20  ) db->lastRowid 
1e338 3d 20 75 2e 62 66 2e 70 4b 65 79 2d 3e 75 2e 69  = u.bf.pKey->u.i
1e339 3b 0a 20 20 69 66 28 20 75 2e 62 66 2e 70 44 61  ;.  if( u.bf.pDa
1e33a 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  ta->flags & MEM_
1e33b 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 2e 62 66  Null ){.    u.bf
1e33c 2e 70 44 61 74 61 2d 3e 7a 20 3d 20 30 3b 0a 20  .pData->z = 0;. 
1e33d 20 20 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 6e     u.bf.pData->n
1e33e 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
1e33f 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e     assert( u.bf.
1e340 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 28  pData->flags & (
1e341 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72  MEM_Blob|MEM_Str
1e342 29 20 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 66 2e  ) );.  }.  u.bf.
1e343 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 28 28 70  seekResult = ((p
1e344 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f  Op->p5 & OPFLAG_
1e345 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29 20 3f  USESEEKRESULT) ?
1e346 20 75 2e 62 66 2e 70 43 2d 3e 73 65 65 6b 52 65   u.bf.pC->seekRe
1e347 73 75 6c 74 20 3a 20 30 29 3b 0a 20 20 69 66 28  sult : 0);.  if(
1e348 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 66 6c 61   u.bf.pData->fla
1e349 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b  gs & MEM_Zero ){
1e34a 0a 20 20 20 20 75 2e 62 66 2e 6e 5a 65 72 6f 20  .    u.bf.nZero 
1e34b 3d 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 75 2e  = u.bf.pData->u.
1e34c 6e 5a 65 72 6f 3b 0a 20 20 7d 65 6c 73 65 7b 0a  nZero;.  }else{.
1e34d 20 20 20 20 75 2e 62 66 2e 6e 5a 65 72 6f 20 3d      u.bf.nZero =
1e34e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   0;.  }.  sqlite
1e34f 33 42 74 72 65 65 53 65 74 43 61 63 68 65 64 52  3BtreeSetCachedR
1e350 6f 77 69 64 28 75 2e 62 66 2e 70 43 2d 3e 70 43  owid(u.bf.pC->pC
1e351 75 72 73 6f 72 2c 20 30 29 3b 0a 20 20 72 63 20  ursor, 0);.  rc 
1e352 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e  = sqlite3BtreeIn
1e353 73 65 72 74 28 75 2e 62 66 2e 70 43 2d 3e 70 43  sert(u.bf.pC->pC
1e354 75 72 73 6f 72 2c 20 30 2c 20 75 2e 62 66 2e 69  ursor, 0, u.bf.i
1e355 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Key,.           
1e356 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75                 u
1e357 2e 62 66 2e 70 44 61 74 61 2d 3e 7a 2c 20 75 2e  .bf.pData->z, u.
1e358 62 66 2e 70 44 61 74 61 2d 3e 6e 2c 20 75 2e 62  bf.pData->n, u.b
1e359 66 2e 6e 5a 65 72 6f 2c 0a 20 20 20 20 20 20 20  f.nZero,.       
1e35a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e35b 20 20 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46     pOp->p5 & OPF
1e35c 4c 41 47 5f 41 50 50 45 4e 44 2c 20 75 2e 62 66  LAG_APPEND, u.bf
1e35d 2e 73 65 65 6b 52 65 73 75 6c 74 0a 20 20 29 3b  .seekResult.  );
1e35e 0a 20 20 75 2e 62 66 2e 70 43 2d 3e 72 6f 77 69  .  u.bf.pC->rowi
1e35f 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  dIsValid = 0;.  
1e360 75 2e 62 66 2e 70 43 2d 3e 64 65 66 65 72 72 65  u.bf.pC->deferre
1e361 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 75  dMoveto = 0;.  u
1e362 2e 62 66 2e 70 43 2d 3e 63 61 63 68 65 53 74 61  .bf.pC->cacheSta
1e363 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c  tus = CACHE_STAL
1e364 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  E;..  /* Invoke 
1e365 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20  the update-hook 
1e366 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a  if required. */.
1e367 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1e368 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 61  _OK && db->xUpda
1e369 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f  teCallback && pO
1e36a 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 75  p->p4.z ){.    u
1e36b 2e 62 66 2e 7a 44 62 20 3d 20 64 62 2d 3e 61 44  .bf.zDb = db->aD
1e36c 62 5b 75 2e 62 66 2e 70 43 2d 3e 69 44 62 5d 2e  b[u.bf.pC->iDb].
1e36d 7a 4e 61 6d 65 3b 0a 20 20 20 20 75 2e 62 66 2e  zName;.    u.bf.
1e36e 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a  zTbl = pOp->p4.z
1e36f 3b 0a 20 20 20 20 75 2e 62 66 2e 6f 70 20 3d 20  ;.    u.bf.op = 
1e370 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c  ((pOp->p5 & OPFL
1e371 41 47 5f 49 53 55 50 44 41 54 45 29 20 3f 20 53  AG_ISUPDATE) ? S
1e372 51 4c 49 54 45 5f 55 50 44 41 54 45 20 3a 20 53  QLITE_UPDATE : S
1e373 51 4c 49 54 45 5f 49 4e 53 45 52 54 29 3b 0a 20  QLITE_INSERT);. 
1e374 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e     assert( u.bf.
1e375 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20  pC->isTable );. 
1e376 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61     db->xUpdateCa
1e377 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 64 61  llback(db->pUpda
1e378 74 65 41 72 67 2c 20 75 2e 62 66 2e 6f 70 2c 20  teArg, u.bf.op, 
1e379 75 2e 62 66 2e 7a 44 62 2c 20 75 2e 62 66 2e 7a  u.bf.zDb, u.bf.z
1e37a 54 62 6c 2c 20 75 2e 62 66 2e 69 4b 65 79 29 3b  Tbl, u.bf.iKey);
1e37b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62  .    assert( u.b
1e37c 66 2e 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a  f.pC->iDb>=0 );.
1e37d 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1e37e 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 6c 65 74  /* Opcode: Delet
1e37f 65 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a  e P1 P2 * P4 *.*
1e380 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20  *.** Delete the 
1e381 72 65 63 6f 72 64 20 61 74 20 77 68 69 63 68 20  record at which 
1e382 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 69 73  the P1 cursor is
1e383 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
1e384 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ing..**.** The c
1e385 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 6c 65  ursor will be le
1e386 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 65  ft pointing at e
1e387 69 74 68 65 72 20 74 68 65 20 6e 65 78 74 20 6f  ither the next o
1e388 72 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a  r the previous.*
1e389 2a 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65 20  * record in the 
1e38a 74 61 62 6c 65 2e 20 49 66 20 69 74 20 69 73 20  table. If it is 
1e38b 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74  left pointing at
1e38c 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f 72 64   the next record
1e38d 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65  , then.** the ne
1e38e 78 74 20 4e 65 78 74 20 69 6e 73 74 72 75 63 74  xt Next instruct
1e38f 69 6f 6e 20 77 69 6c 6c 20 62 65 20 61 20 6e 6f  ion will be a no
1e390 2d 6f 70 2e 20 20 48 65 6e 63 65 20 69 74 20 69  -op.  Hence it i
1e391 73 20 4f 4b 20 74 6f 20 64 65 6c 65 74 65 0a 2a  s OK to delete.*
1e392 2a 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20  * a record from 
1e393 77 69 74 68 69 6e 20 61 6e 20 4e 65 78 74 20 6c  within an Next l
1e394 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  oop..**.** If th
1e395 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45  e OPFLAG_NCHANGE
1e396 20 66 6c 61 67 20 6f 66 20 50 32 20 69 73 20 73   flag of P2 is s
1e397 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77  et, then the row
1e398 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 73   change count is
1e399 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20  .** incremented 
1e39a 28 6f 74 68 65 72 77 69 73 65 20 6e 6f 74 29 2e  (otherwise not).
1e39b 0a 2a 2a 0a 2a 2a 20 50 31 20 6d 75 73 74 20 6e  .**.** P1 must n
1e39c 6f 74 20 62 65 20 70 73 65 75 64 6f 2d 74 61 62  ot be pseudo-tab
1e39d 6c 65 2e 20 20 49 74 20 68 61 73 20 74 6f 20 62  le.  It has to b
1e39e 65 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 77  e a real table w
1e39f 69 74 68 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20  ith.** multiple 
1e3a0 72 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  rows..**.** If P
1e3a1 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74  4 is not NULL, t
1e3a2 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 6e 61  hen it is the na
1e3a3 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
1e3a4 74 68 61 74 20 50 31 20 69 73 0a 2a 2a 20 70 6f  that P1 is.** po
1e3a5 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20  inting to.  The 
1e3a6 75 70 64 61 74 65 20 68 6f 6f 6b 20 77 69 6c 6c  update hook will
1e3a7 20 62 65 20 69 6e 76 6f 6b 65 64 2c 20 69 66 20   be invoked, if 
1e3a8 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 20 49 66  it exists..** If
1e3a9 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20   P4 is not NULL 
1e3aa 74 68 65 6e 20 74 68 65 20 50 31 20 63 75 72 73  then the P1 curs
1e3ab 6f 72 20 6d 75 73 74 20 68 61 76 65 20 62 65 65  or must have bee
1e3ac 6e 20 70 6f 73 69 74 69 6f 6e 65 64 0a 2a 2a 20  n positioned.** 
1e3ad 75 73 69 6e 67 20 4f 50 5f 4e 6f 74 46 6f 75 6e  using OP_NotFoun
1e3ae 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b  d prior to invok
1e3af 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 2e  ing this opcode.
1e3b0 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 65 6c 65  .*/.case OP_Dele
1e3b1 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  te: {.#if 0  /* 
1e3b2 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1e3b3 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 67 20  moved into u.bg 
1e3b4 2a 2f 0a 20 20 69 36 34 20 69 4b 65 79 3b 0a 20  */.  i64 iKey;. 
1e3b5 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1e3b6 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1e3b7 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1e3b8 20 69 6e 74 6f 20 75 2e 62 67 20 2a 2f 0a 0a 20   into u.bg */.. 
1e3b9 20 75 2e 62 67 2e 69 4b 65 79 20 3d 20 30 3b 0a   u.bg.iKey = 0;.
1e3ba 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1e3bb 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
1e3bc 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1e3bd 75 2e 62 67 2e 70 43 20 3d 20 70 2d 3e 61 70 43  u.bg.pC = p->apC
1e3be 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61  sr[pOp->p1];.  a
1e3bf 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 21 3d  ssert( u.bg.pC!=
1e3c0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75  0 );.  assert( u
1e3c1 2e 62 67 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21  .bg.pC->pCursor!
1e3c2 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e 6c 79 20 76  =0 );  /* Only v
1e3c3 61 6c 69 64 20 66 6f 72 20 72 65 61 6c 20 74 61  alid for real ta
1e3c4 62 6c 65 73 2c 20 6e 6f 20 70 73 65 75 64 6f 74  bles, no pseudot
1e3c5 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49  ables */..  /* I
1e3c6 66 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f  f the update-hoo
1e3c7 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65  k will be invoke
1e3c8 64 2c 20 73 65 74 20 75 2e 62 67 2e 69 4b 65 79  d, set u.bg.iKey
1e3c9 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20 6f 66   to the rowid of
1e3ca 20 74 68 65 0a 20 20 2a 2a 20 72 6f 77 20 62 65   the.  ** row be
1e3cb 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 20 20 2a  ing deleted..  *
1e3cc 2f 0a 20 20 69 66 28 20 64 62 2d 3e 78 55 70 64  /.  if( db->xUpd
1e3cd 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70  ateCallback && p
1e3ce 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20  Op->p4.z ){.    
1e3cf 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 2d  assert( u.bg.pC-
1e3d0 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20  >isTable );.    
1e3d1 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 2d  assert( u.bg.pC-
1e3d2 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29 3b  >rowidIsValid );
1e3d3 20 20 2f 2a 20 6c 61 73 74 52 6f 77 69 64 20 73    /* lastRowid s
1e3d4 65 74 20 62 79 20 70 72 65 76 69 6f 75 73 20 4f  et by previous O
1e3d5 50 5f 4e 6f 74 46 6f 75 6e 64 20 2a 2f 0a 20 20  P_NotFound */.  
1e3d6 20 20 75 2e 62 67 2e 69 4b 65 79 20 3d 20 75 2e    u.bg.iKey = u.
1e3d7 62 67 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64  bg.pC->lastRowid
1e3d8 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  ;.  }..  /* The 
1e3d9 4f 50 5f 44 65 6c 65 74 65 20 6f 70 63 6f 64 65  OP_Delete opcode
1e3da 20 61 6c 77 61 79 73 20 66 6f 6c 6c 6f 77 73 20   always follows 
1e3db 61 6e 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20  an OP_NotExists 
1e3dc 6f 72 20 4f 50 5f 4c 61 73 74 20 6f 72 0a 20 20  or OP_Last or.  
1e3dd 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 6e 20  ** OP_Column on 
1e3de 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 20 77  the same table w
1e3df 69 74 68 6f 75 74 20 61 6e 79 20 69 6e 74 65 72  ithout any inter
1e3e0 76 65 6e 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  vening operation
1e3e1 73 20 74 68 61 74 0a 20 20 2a 2a 20 6d 69 67 68  s that.  ** migh
1e3e2 74 20 6d 6f 76 65 20 6f 72 20 69 6e 76 61 6c 69  t move or invali
1e3e3 64 61 74 65 20 74 68 65 20 63 75 72 73 6f 72 2e  date the cursor.
1e3e4 20 20 48 65 6e 63 65 20 63 75 72 73 6f 72 20 75    Hence cursor u
1e3e5 2e 62 67 2e 70 43 20 69 73 20 61 6c 77 61 79 73  .bg.pC is always
1e3e6 20 70 6f 69 6e 74 69 6e 67 0a 20 20 2a 2a 20 74   pointing.  ** t
1e3e7 6f 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20  o the row to be 
1e3e8 64 65 6c 65 74 65 64 20 61 6e 64 20 74 68 65 20  deleted and the 
1e3e9 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f  sqlite3VdbeCurso
1e3ea 72 4d 6f 76 65 74 6f 28 29 20 6f 70 65 72 61 74  rMoveto() operat
1e3eb 69 6f 6e 0a 20 20 2a 2a 20 62 65 6c 6f 77 20 69  ion.  ** below i
1e3ec 73 20 61 6c 77 61 79 73 20 61 20 6e 6f 2d 6f 70  s always a no-op
1e3ed 20 61 6e 64 20 63 61 6e 6e 6f 74 20 66 61 69 6c   and cannot fail
1e3ee 2e 20 20 57 65 20 77 69 6c 6c 20 72 75 6e 20 69  .  We will run i
1e3ef 74 20 61 6e 79 68 6f 77 2c 20 74 68 6f 75 67 68  t anyhow, though
1e3f0 2c 0a 20 20 2a 2a 20 74 6f 20 67 75 61 72 64 20  ,.  ** to guard 
1e3f1 61 67 61 69 6e 73 74 20 66 75 74 75 72 65 20 63  against future c
1e3f2 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 63 6f  hanges to the co
1e3f3 64 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a 20 20  de generator..  
1e3f4 2a 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 75 2e  **/.  assert( u.
1e3f5 62 67 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d  bg.pC->deferredM
1e3f6 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63  oveto==0 );.  rc
1e3f7 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
1e3f8 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 67 2e  rsorMoveto(u.bg.
1e3f9 70 43 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52  pC);.  if( NEVER
1e3fa 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20  (rc!=SQLITE_OK) 
1e3fb 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1e3fc 5f 74 6f 5f 65 72 72 6f 72 3b 0a 0a 20 20 73 71  _to_error;..  sq
1e3fd 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63  lite3BtreeSetCac
1e3fe 68 65 64 52 6f 77 69 64 28 75 2e 62 67 2e 70 43  hedRowid(u.bg.pC
1e3ff 2d 3e 70 43 75 72 73 6f 72 2c 20 30 29 3b 0a 20  ->pCursor, 0);. 
1e400 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1e401 65 65 44 65 6c 65 74 65 28 75 2e 62 67 2e 70 43  eeDelete(u.bg.pC
1e402 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 75 2e  ->pCursor);.  u.
1e403 62 67 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74  bg.pC->cacheStat
1e404 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45  us = CACHE_STALE
1e405 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74  ;..  /* Invoke t
1e406 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69  he update-hook i
1e407 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20  f required. */. 
1e408 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1e409 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 61 74  OK && db->xUpdat
1e40a 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70  eCallback && pOp
1e40b 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 63 6f  ->p4.z ){.    co
1e40c 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20  nst char *zDb = 
1e40d 64 62 2d 3e 61 44 62 5b 75 2e 62 67 2e 70 43 2d  db->aDb[u.bg.pC-
1e40e 3e 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  >iDb].zName;.   
1e40f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 62   const char *zTb
1e410 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20  l = pOp->p4.z;. 
1e411 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61     db->xUpdateCa
1e412 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 64 61  llback(db->pUpda
1e413 74 65 41 72 67 2c 20 53 51 4c 49 54 45 5f 44 45  teArg, SQLITE_DE
1e414 4c 45 54 45 2c 20 7a 44 62 2c 20 7a 54 62 6c 2c  LETE, zDb, zTbl,
1e415 20 75 2e 62 67 2e 69 4b 65 79 29 3b 0a 20 20 20   u.bg.iKey);.   
1e416 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43   assert( u.bg.pC
1e417 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a  ->iDb>=0 );.  }.
1e418 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 26 20    if( pOp->p2 & 
1e419 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29  OPFLAG_NCHANGE )
1e41a 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20   p->nChange++;. 
1e41b 20 62 72 65 61 6b 3b 0a 7d 0a 2f 2a 20 4f 70 63   break;.}./* Opc
1e41c 6f 64 65 3a 20 52 65 73 65 74 43 6f 75 6e 74 20  ode: ResetCount 
1e41d 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  * * * * *.**.** 
1e41e 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  The value of the
1e41f 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20   change counter 
1e420 69 73 20 63 6f 70 69 65 64 20 74 6f 20 74 68 65  is copied to the
1e421 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
1e422 0a 2a 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e 74  .** change count
1e423 65 72 20 28 72 65 74 75 72 6e 65 64 20 62 79 20  er (returned by 
1e424 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  subsequent calls
1e425 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 68 61 6e   to sqlite3_chan
1e426 67 65 73 28 29 29 2e 0a 2a 2a 20 54 68 65 6e 20  ges())..** Then 
1e427 74 68 65 20 56 4d 73 20 69 6e 74 65 72 6e 61 6c  the VMs internal
1e428 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20   change counter 
1e429 72 65 73 65 74 73 20 74 6f 20 30 2e 0a 2a 2a 20  resets to 0..** 
1e42a 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20  This is used by 
1e42b 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73  trigger programs
1e42c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73  ..*/.case OP_Res
1e42d 65 74 43 6f 75 6e 74 3a 20 7b 0a 20 20 73 71 6c  etCount: {.  sql
1e42e 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67  ite3VdbeSetChang
1e42f 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67  es(db, p->nChang
1e430 65 29 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65  e);.  p->nChange
1e431 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d   = 0;.  break;.}
1e432 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77  ../* Opcode: Row
1e433 44 61 74 61 20 50 31 20 50 32 20 2a 20 2a 20 2a  Data P1 P2 * * *
1e434 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74  .**.** Write int
1e435 6f 20 72 65 67 69 73 74 65 72 20 50 32 20 74 68  o register P2 th
1e436 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 20 64  e complete row d
1e437 61 74 61 20 66 6f 72 20 63 75 72 73 6f 72 20 50  ata for cursor P
1e438 31 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e  1..** There is n
1e439 6f 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e  o interpretation
1e43a 20 6f 66 20 74 68 65 20 64 61 74 61 2e 20 20 0a   of the data.  .
1e43b 2a 2a 20 49 74 20 69 73 20 6a 75 73 74 20 63 6f  ** It is just co
1e43c 70 69 65 64 20 6f 6e 74 6f 20 74 68 65 20 50 32  pied onto the P2
1e43d 20 72 65 67 69 73 74 65 72 20 65 78 61 63 74 6c   register exactl
1e43e 79 20 61 73 20 0a 2a 2a 20 69 74 20 69 73 20 66  y as .** it is f
1e43f 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74 61  ound in the data
1e440 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  base file..**.**
1e441 20 49 66 20 74 68 65 20 50 31 20 63 75 72 73 6f   If the P1 curso
1e442 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69  r must be pointi
1e443 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20 72 6f  ng to a valid ro
1e444 77 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 6f  w (not a NULL ro
1e445 77 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61 6c 20  w).** of a real 
1e446 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65  table, not a pse
1e447 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a  udo-table..*/./*
1e448 20 4f 70 63 6f 64 65 3a 20 52 6f 77 4b 65 79 20   Opcode: RowKey 
1e449 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1e44a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72 65 67  * Write into reg
1e44b 69 73 74 65 72 20 50 32 20 74 68 65 20 63 6f 6d  ister P2 the com
1e44c 70 6c 65 74 65 20 72 6f 77 20 6b 65 79 20 66 6f  plete row key fo
1e44d 72 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20  r cursor P1..** 
1e44e 54 68 65 72 65 20 69 73 20 6e 6f 20 69 6e 74 65  There is no inte
1e44f 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 74 68  rpretation of th
1e450 65 20 64 61 74 61 2e 20 20 0a 2a 2a 20 54 68 65  e data.  .** The
1e451 20 6b 65 79 20 69 73 20 63 6f 70 69 65 64 20 6f   key is copied o
1e452 6e 74 6f 20 74 68 65 20 50 33 20 72 65 67 69 73  nto the P3 regis
1e453 74 65 72 20 65 78 61 63 74 6c 79 20 61 73 20 0a  ter exactly as .
1e454 2a 2a 20 69 74 20 69 73 20 66 6f 75 6e 64 20 69  ** it is found i
1e455 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
1e456 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ile..**.** If th
1e457 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74  e P1 cursor must
1e458 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20   be pointing to 
1e459 61 20 76 61 6c 69 64 20 72 6f 77 20 28 6e 6f 74  a valid row (not
1e45a 20 61 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a 2a 20   a NULL row).** 
1e45b 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c  of a real table,
1e45c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61   not a pseudo-ta
1e45d 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ble..*/.case OP_
1e45e 52 6f 77 4b 65 79 3a 0a 63 61 73 65 20 4f 50 5f  RowKey:.case OP_
1e45f 52 6f 77 44 61 74 61 3a 20 7b 0a 23 69 66 20 30  RowData: {.#if 0
1e460 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1e461 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e462 75 2e 62 68 20 2a 2f 0a 20 20 56 64 62 65 43 75  u.bh */.  VdbeCu
1e463 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75  rsor *pC;.  BtCu
1e464 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 75  rsor *pCrsr;.  u
1e465 33 32 20 6e 3b 0a 20 20 69 36 34 20 6e 36 34 3b  32 n;.  i64 n64;
1e466 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1e467 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1e468 20 69 6e 74 6f 20 75 2e 62 68 20 2a 2f 0a 0a 20   into u.bh */.. 
1e469 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d   pOut = &p->aMem
1e46a 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 0a 20 20 2f 2a  [pOp->p2];..  /*
1e46b 20 4e 6f 74 65 20 74 68 61 74 20 52 6f 77 4b 65   Note that RowKe
1e46c 79 20 61 6e 64 20 52 6f 77 44 61 74 61 20 61 72  y and RowData ar
1e46d 65 20 72 65 61 6c 6c 79 20 65 78 61 63 74 6c 79  e really exactly
1e46e 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 72 75   the same instru
1e46f 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 72  ction */.  asser
1e470 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
1e471 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72   pOp->p1<p->nCur
1e472 73 6f 72 20 29 3b 0a 20 20 75 2e 62 68 2e 70 43  sor );.  u.bh.pC
1e473 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d   = p->apCsr[pOp-
1e474 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  >p1];.  assert( 
1e475 75 2e 62 68 2e 70 43 2d 3e 69 73 54 61 62 6c 65  u.bh.pC->isTable
1e476 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d   || pOp->opcode=
1e477 3d 4f 50 5f 52 6f 77 4b 65 79 20 29 3b 0a 20 20  =OP_RowKey );.  
1e478 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 43 2d  assert( u.bh.pC-
1e479 3e 69 73 49 6e 64 65 78 20 7c 7c 20 70 4f 70 2d  >isIndex || pOp-
1e47a 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 44  >opcode==OP_RowD
1e47b 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ata );.  assert(
1e47c 20 75 2e 62 68 2e 70 43 21 3d 30 20 29 3b 0a 20   u.bh.pC!=0 );. 
1e47d 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 43   assert( u.bh.pC
1e47e 2d 3e 6e 75 6c 6c 52 6f 77 3d 3d 30 20 29 3b 0a  ->nullRow==0 );.
1e47f 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70    assert( u.bh.p
1e480 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65  C->pseudoTableRe
1e481 67 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  g==0 );.  assert
1e482 28 20 75 2e 62 68 2e 70 43 2d 3e 70 43 75 72 73  ( u.bh.pC->pCurs
1e483 6f 72 21 3d 30 20 29 3b 0a 20 20 75 2e 62 68 2e  or!=0 );.  u.bh.
1e484 70 43 72 73 72 20 3d 20 75 2e 62 68 2e 70 43 2d  pCrsr = u.bh.pC-
1e485 3e 70 43 75 72 73 6f 72 3b 0a 20 20 61 73 73 65  >pCursor;.  asse
1e486 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
1e487 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75 2e  CursorIsValid(u.
1e488 62 68 2e 70 43 72 73 72 29 20 29 3b 0a 0a 20 20  bh.pCrsr) );..  
1e489 2f 2a 20 54 68 65 20 4f 50 5f 52 6f 77 4b 65 79  /* The OP_RowKey
1e48a 20 61 6e 64 20 4f 50 5f 52 6f 77 44 61 74 61 20   and OP_RowData 
1e48b 6f 70 63 6f 64 65 73 20 61 6c 77 61 79 73 20 66  opcodes always f
1e48c 6f 6c 6c 6f 77 20 4f 50 5f 4e 6f 74 45 78 69 73  ollow OP_NotExis
1e48d 74 73 20 6f 72 0a 20 20 2a 2a 20 4f 50 5f 52 65  ts or.  ** OP_Re
1e48e 77 69 6e 64 2f 4f 70 5f 4e 65 78 74 20 77 69 74  wind/Op_Next wit
1e48f 68 20 6e 6f 20 69 6e 74 65 72 76 65 6e 69 6e 67  h no intervening
1e490 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 74 68   instructions th
1e491 61 74 20 6d 69 67 68 74 20 69 6e 76 61 6c 69 64  at might invalid
1e492 61 74 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 72  ate.  ** the cur
1e493 73 6f 72 2e 20 20 48 65 6e 63 65 20 74 68 65 20  sor.  Hence the 
1e494 66 6f 6c 6c 6f 77 69 6e 67 20 73 71 6c 69 74 65  following sqlite
1e495 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74  3VdbeCursorMovet
1e496 6f 28 29 20 63 61 6c 6c 20 69 73 20 61 6c 77 61  o() call is alwa
1e497 79 73 0a 20 20 2a 2a 20 61 20 6e 6f 2d 6f 70 20  ys.  ** a no-op 
1e498 61 6e 64 20 63 61 6e 20 6e 65 76 65 72 20 66 61  and can never fa
1e499 69 6c 2e 20 20 42 75 74 20 77 65 20 6c 65 61 76  il.  But we leav
1e49a 65 20 69 74 20 69 6e 20 70 6c 61 63 65 20 61 73  e it in place as
1e49b 20 61 20 73 61 66 65 74 79 2e 0a 20 20 2a 2f 0a   a safety..  */.
1e49c 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70    assert( u.bh.p
1e49d 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
1e49e 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73  o==0 );.  rc = s
1e49f 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72  qlite3VdbeCursor
1e4a0 4d 6f 76 65 74 6f 28 75 2e 62 68 2e 70 43 29 3b  Moveto(u.bh.pC);
1e4a1 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63 21  .  if( NEVER(rc!
1e4a2 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 20 67 6f  =SQLITE_OK) ) go
1e4a3 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1e4a4 65 72 72 6f 72 3b 0a 0a 20 20 69 66 28 20 75 2e  error;..  if( u.
1e4a5 62 68 2e 70 43 2d 3e 69 73 49 6e 64 65 78 20 29  bh.pC->isIndex )
1e4a6 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 75  {.    assert( !u
1e4a7 2e 62 68 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20  .bh.pC->isTable 
1e4a8 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
1e4a9 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28  te3BtreeKeySize(
1e4aa 75 2e 62 68 2e 70 43 72 73 72 2c 20 26 75 2e 62  u.bh.pCrsr, &u.b
1e4ab 68 2e 6e 36 34 29 3b 0a 20 20 20 20 61 73 73 65  h.n64);.    asse
1e4ac 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
1e4ad 4b 20 29 3b 20 20 20 20 2f 2a 20 54 72 75 65 20  K );    /* True 
1e4ae 62 65 63 61 75 73 65 20 6f 66 20 43 75 72 73 6f  because of Curso
1e4af 72 4d 6f 76 65 74 6f 28 29 20 63 61 6c 6c 20 61  rMoveto() call a
1e4b0 62 6f 76 65 20 2a 2f 0a 20 20 20 20 69 66 28 20  bove */.    if( 
1e4b1 75 2e 62 68 2e 6e 36 34 3e 64 62 2d 3e 61 4c 69  u.bh.n64>db->aLi
1e4b2 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
1e4b3 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20  _LENGTH] ){.    
1e4b4 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
1e4b5 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 68 2e 6e      }.    u.bh.n
1e4b6 20 3d 20 28 75 33 32 29 75 2e 62 68 2e 6e 36 34   = (u32)u.bh.n64
1e4b7 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
1e4b8 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1e4b9 44 61 74 61 53 69 7a 65 28 75 2e 62 68 2e 70 43  DataSize(u.bh.pC
1e4ba 72 73 72 2c 20 26 75 2e 62 68 2e 6e 29 3b 0a 20  rsr, &u.bh.n);. 
1e4bb 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
1e4bc 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20 2f  QLITE_OK );    /
1e4bd 2a 20 44 61 74 61 53 69 7a 65 28 29 20 63 61 6e  * DataSize() can
1e4be 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 20 20  not fail */.    
1e4bf 69 66 28 20 75 2e 62 68 2e 6e 3e 28 75 33 32 29  if( u.bh.n>(u32)
1e4c0 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
1e4c1 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20  E_LIMIT_LENGTH] 
1e4c2 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 6f  ){.      goto to
1e4c3 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 7d  o_big;.    }.  }
1e4c4 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64  .  if( sqlite3Vd
1e4c5 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20  beMemGrow(pOut, 
1e4c6 75 2e 62 68 2e 6e 2c 20 30 29 20 29 7b 0a 20 20  u.bh.n, 0) ){.  
1e4c7 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
1e4c8 20 7d 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 75   }.  pOut->n = u
1e4c9 2e 62 68 2e 6e 3b 0a 20 20 4d 65 6d 53 65 74 54  .bh.n;.  MemSetT
1e4ca 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
1e4cb 4d 5f 42 6c 6f 62 29 3b 0a 20 20 69 66 28 20 75  M_Blob);.  if( u
1e4cc 2e 62 68 2e 70 43 2d 3e 69 73 49 6e 64 65 78 20  .bh.pC->isIndex 
1e4cd 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
1e4ce 74 65 33 42 74 72 65 65 4b 65 79 28 75 2e 62 68  te3BtreeKey(u.bh
1e4cf 2e 70 43 72 73 72 2c 20 30 2c 20 75 2e 62 68 2e  .pCrsr, 0, u.bh.
1e4d0 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d  n, pOut->z);.  }
1e4d1 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73  else{.    rc = s
1e4d2 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28  qlite3BtreeData(
1e4d3 75 2e 62 68 2e 70 43 72 73 72 2c 20 30 2c 20 75  u.bh.pCrsr, 0, u
1e4d4 2e 62 68 2e 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b  .bh.n, pOut->z);
1e4d5 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e 63  .  }.  pOut->enc
1e4d6 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 20   = SQLITE_UTF8; 
1e4d7 20 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65 20   /* In case the 
1e4d8 62 6c 6f 62 20 69 73 20 65 76 65 72 20 63 61 73  blob is ever cas
1e4d9 74 20 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20 55  t to text */.  U
1e4da 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49  PDATE_MAX_BLOBSI
1e4db 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61  ZE(pOut);.  brea
1e4dc 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1e4dd 20 52 6f 77 69 64 20 50 31 20 50 32 20 2a 20 2a   Rowid P1 P2 * *
1e4de 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 69   *.**.** Store i
1e4df 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 61 6e  n register P2 an
1e4e0 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69   integer which i
1e4e1 73 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65  s the key of the
1e4e2 20 74 61 62 6c 65 20 65 6e 74 72 79 20 74 68 61   table entry tha
1e4e3 74 0a 2a 2a 20 50 31 20 69 73 20 63 75 72 72 65  t.** P1 is curre
1e4e4 6e 74 6c 79 20 70 6f 69 6e 74 20 74 6f 2e 0a 2a  ntly point to..*
1e4e5 2a 0a 2a 2a 20 50 31 20 63 61 6e 20 62 65 20 65  *.** P1 can be e
1e4e6 69 74 68 65 72 20 61 6e 20 6f 72 64 69 6e 61 72  ither an ordinar
1e4e7 79 20 74 61 62 6c 65 20 6f 72 20 61 20 76 69 72  y table or a vir
1e4e8 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65  tual table.  The
1e4e9 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 62 65  re used to.** be
1e4ea 20 61 20 73 65 70 61 72 61 74 65 20 4f 50 5f 56   a separate OP_V
1e4eb 52 6f 77 69 64 20 6f 70 63 6f 64 65 20 66 6f 72  Rowid opcode for
1e4ec 20 75 73 65 20 77 69 74 68 20 76 69 72 74 75 61   use with virtua
1e4ed 6c 20 74 61 62 6c 65 73 2c 20 62 75 74 20 74 68  l tables, but th
1e4ee 69 73 0a 2a 2a 20 6f 6e 65 20 6f 70 63 6f 64 65  is.** one opcode
1e4ef 20 6e 6f 77 20 77 6f 72 6b 73 20 66 6f 72 20 62   now works for b
1e4f0 6f 74 68 20 74 61 62 6c 65 20 74 79 70 65 73 2e  oth table types.
1e4f1 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 69  .*/.case OP_Rowi
1e4f2 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  d: {            
1e4f3 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65       /* out2-pre
1e4f4 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30  release */.#if 0
1e4f5 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1e4f6 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e4f7 75 2e 62 69 20 2a 2f 0a 20 20 56 64 62 65 43 75  u.bi */.  VdbeCu
1e4f8 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 36 34 20  rsor *pC;.  i64 
1e4f9 76 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  v;.  sqlite3_vta
1e4fa 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73  b *pVtab;.  cons
1e4fb 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
1e4fc 20 2a 70 4d 6f 64 75 6c 65 3b 0a 23 65 6e 64 69   *pModule;.#endi
1e4fd 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1e4fe 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e4ff 75 2e 62 69 20 2a 2f 0a 0a 20 20 61 73 73 65 72  u.bi */..  asser
1e500 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
1e501 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72   pOp->p1<p->nCur
1e502 73 6f 72 20 29 3b 0a 20 20 75 2e 62 69 2e 70 43  sor );.  u.bi.pC
1e503 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d   = p->apCsr[pOp-
1e504 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  >p1];.  assert( 
1e505 75 2e 62 69 2e 70 43 21 3d 30 20 29 3b 0a 20 20  u.bi.pC!=0 );.  
1e506 61 73 73 65 72 74 28 20 75 2e 62 69 2e 70 43 2d  assert( u.bi.pC-
1e507 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3d  >pseudoTableReg=
1e508 3d 30 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 69  =0 );.  if( u.bi
1e509 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a  .pC->nullRow ){.
1e50a 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e      /* Do nothin
1e50b 67 20 73 6f 20 74 68 61 74 20 72 65 67 5b 50 32  g so that reg[P2
1e50c 5d 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 20 2a  ] remains NULL *
1e50d 2f 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  /.    break;.  }
1e50e 65 6c 73 65 20 69 66 28 20 75 2e 62 69 2e 70 43  else if( u.bi.pC
1e50f 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
1e510 20 29 7b 0a 20 20 20 20 75 2e 62 69 2e 76 20 3d   ){.    u.bi.v =
1e511 20 75 2e 62 69 2e 70 43 2d 3e 6d 6f 76 65 74 6f   u.bi.pC->moveto
1e512 54 61 72 67 65 74 3b 0a 23 69 66 6e 64 65 66 20  Target;.#ifndef 
1e513 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1e514 55 41 4c 54 41 42 4c 45 0a 20 20 7d 65 6c 73 65  UALTABLE.  }else
1e515 20 69 66 28 20 75 2e 62 69 2e 70 43 2d 3e 70 56   if( u.bi.pC->pV
1e516 74 61 62 43 75 72 73 6f 72 20 29 7b 0a 20 20 20  tabCursor ){.   
1e517 20 75 2e 62 69 2e 70 56 74 61 62 20 3d 20 75 2e   u.bi.pVtab = u.
1e518 62 69 2e 70 43 2d 3e 70 56 74 61 62 43 75 72 73  bi.pC->pVtabCurs
1e519 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20 75  or->pVtab;.    u
1e51a 2e 62 69 2e 70 4d 6f 64 75 6c 65 20 3d 20 75 2e  .bi.pModule = u.
1e51b 62 69 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c  bi.pVtab->pModul
1e51c 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  e;.    assert( u
1e51d 2e 62 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f  .bi.pModule->xRo
1e51e 77 69 64 20 29 3b 0a 20 20 20 20 69 66 28 20 73  wid );.    if( s
1e51f 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
1e520 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  db) ) goto abort
1e521 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
1e522 20 20 20 20 72 63 20 3d 20 75 2e 62 69 2e 70 4d      rc = u.bi.pM
1e523 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 28 75 2e  odule->xRowid(u.
1e524 62 69 2e 70 43 2d 3e 70 56 74 61 62 43 75 72 73  bi.pC->pVtabCurs
1e525 6f 72 2c 20 26 75 2e 62 69 2e 76 29 3b 0a 20 20  or, &u.bi.v);.  
1e526 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1e527 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b  db, p->zErrMsg);
1e528 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20  .    p->zErrMsg 
1e529 3d 20 75 2e 62 69 2e 70 56 74 61 62 2d 3e 7a 45  = u.bi.pVtab->zE
1e52a 72 72 4d 73 67 3b 0a 20 20 20 20 75 2e 62 69 2e  rrMsg;.    u.bi.
1e52b 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d  pVtab->zErrMsg =
1e52c 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   0;.    if( sqli
1e52d 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20  te3SafetyOn(db) 
1e52e 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1e52f 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 23 65 6e 64  _to_misuse;.#end
1e530 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1e531 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a  T_VIRTUALTABLE *
1e532 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  /.  }else{.    a
1e533 73 73 65 72 74 28 20 75 2e 62 69 2e 70 43 2d 3e  ssert( u.bi.pC->
1e534 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20  pCursor!=0 );.  
1e535 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1e536 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75  beCursorMoveto(u
1e537 2e 62 69 2e 70 43 29 3b 0a 20 20 20 20 69 66 28  .bi.pC);.    if(
1e538 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 74   rc ) goto abort
1e539 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20  _due_to_error;. 
1e53a 20 20 20 69 66 28 20 75 2e 62 69 2e 70 43 2d 3e     if( u.bi.pC->
1e53b 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29 7b 0a  rowidIsValid ){.
1e53c 20 20 20 20 20 20 75 2e 62 69 2e 76 20 3d 20 75        u.bi.v = u
1e53d 2e 62 69 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69  .bi.pC->lastRowi
1e53e 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  d;.    }else{.  
1e53f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1e540 42 74 72 65 65 4b 65 79 53 69 7a 65 28 75 2e 62  BtreeKeySize(u.b
1e541 69 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26  i.pC->pCursor, &
1e542 75 2e 62 69 2e 76 29 3b 0a 20 20 20 20 20 20 61  u.bi.v);.      a
1e543 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
1e544 45 5f 4f 4b 20 29 3b 20 20 2f 2a 20 41 6c 77 61  E_OK );  /* Alwa
1e545 79 73 20 73 6f 20 62 65 63 61 75 73 65 20 6f 66  ys so because of
1e546 20 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20   CursorMoveto() 
1e547 61 62 6f 76 65 20 2a 2f 0a 20 20 20 20 7d 0a 20  above */.    }. 
1e548 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d   }.  pOut->u.i =
1e549 20 75 2e 62 69 2e 76 3b 0a 20 20 4d 65 6d 53 65   u.bi.v;.  MemSe
1e54a 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
1e54b 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61  MEM_Int);.  brea
1e54c 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1e54d 20 4e 75 6c 6c 52 6f 77 20 50 31 20 2a 20 2a 20   NullRow P1 * * 
1e54e 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 74  * *.**.** Move t
1e54f 68 65 20 63 75 72 73 6f 72 20 50 31 20 74 6f 20  he cursor P1 to 
1e550 61 20 6e 75 6c 6c 20 72 6f 77 2e 20 20 41 6e 79  a null row.  Any
1e551 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 65 72 61   OP_Column opera
1e552 74 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 6f 63  tions.** that oc
1e553 63 75 72 20 77 68 69 6c 65 20 74 68 65 20 63 75  cur while the cu
1e554 72 73 6f 72 20 69 73 20 6f 6e 20 74 68 65 20 6e  rsor is on the n
1e555 75 6c 6c 20 72 6f 77 20 77 69 6c 6c 20 61 6c 77  ull row will alw
1e556 61 79 73 0a 2a 2a 20 77 72 69 74 65 20 61 20 4e  ays.** write a N
1e557 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ULL..*/.case OP_
1e558 4e 75 6c 6c 52 6f 77 3a 20 7b 0a 23 69 66 20 30  NullRow: {.#if 0
1e559 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1e55a 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e55b 75 2e 62 6a 20 2a 2f 0a 20 20 56 64 62 65 43 75  u.bj */.  VdbeCu
1e55c 72 73 6f 72 20 2a 70 43 3b 0a 23 65 6e 64 69 66  rsor *pC;.#endif
1e55d 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1e55e 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1e55f 2e 62 6a 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  .bj */..  assert
1e560 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1e561 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73  pOp->p1<p->nCurs
1e562 6f 72 20 29 3b 0a 20 20 75 2e 62 6a 2e 70 43 20  or );.  u.bj.pC 
1e563 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  = p->apCsr[pOp->
1e564 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
1e565 2e 62 6a 2e 70 43 21 3d 30 20 29 3b 0a 20 20 75  .bj.pC!=0 );.  u
1e566 2e 62 6a 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20  .bj.pC->nullRow 
1e567 3d 20 31 3b 0a 20 20 75 2e 62 6a 2e 70 43 2d 3e  = 1;.  u.bj.pC->
1e568 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30  rowidIsValid = 0
1e569 3b 0a 20 20 69 66 28 20 75 2e 62 6a 2e 70 43 2d  ;.  if( u.bj.pC-
1e56a 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20  >pCursor ){.    
1e56b 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61  sqlite3BtreeClea
1e56c 72 43 75 72 73 6f 72 28 75 2e 62 6a 2e 70 43 2d  rCursor(u.bj.pC-
1e56d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 20  >pCursor);.  }. 
1e56e 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1e56f 63 6f 64 65 3a 20 4c 61 73 74 20 50 31 20 50 32  code: Last P1 P2
1e570 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65   * * *.**.** The
1e571 20 6e 65 78 74 20 75 73 65 20 6f 66 20 74 68 65   next use of the
1e572 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c 75 6d 6e   Rowid or Column
1e573 20 6f 72 20 4e 65 78 74 20 69 6e 73 74 72 75 63   or Next instruc
1e574 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a 2a 2a 20  tion for P1 .** 
1e575 77 69 6c 6c 20 72 65 66 65 72 20 74 6f 20 74 68  will refer to th
1e576 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20  e last entry in 
1e577 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62  the database tab
1e578 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 20  le or index..** 
1e579 49 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20  If the table or 
1e57a 69 6e 64 65 78 20 69 73 20 65 6d 70 74 79 20 61  index is empty a
1e57b 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e 20 6a 75  nd P2>0, then ju
1e57c 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  mp immediately t
1e57d 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50 32 20 69  o P2..** If P2 i
1e57e 73 20 30 20 6f 72 20 69 66 20 74 68 65 20 74 61  s 0 or if the ta
1e57f 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20  ble or index is 
1e580 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61 6c 6c 20  not empty, fall 
1e581 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68  through.** to th
1e582 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74  e following inst
1e583 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65  ruction..*/.case
1e584 20 4f 50 5f 4c 61 73 74 3a 20 7b 20 20 20 20 20   OP_Last: {     
1e585 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69     /* jump */.#i
1e586 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1e587 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e588 74 6f 20 75 2e 62 6b 20 2a 2f 0a 20 20 56 64 62  to u.bk */.  Vdb
1e589 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42  eCursor *pC;.  B
1e58a 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1e58b 20 20 69 6e 74 20 72 65 73 3b 0a 23 65 6e 64 69    int res;.#endi
1e58c 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1e58d 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e58e 75 2e 62 6b 20 2a 2f 0a 0a 20 20 61 73 73 65 72  u.bk */..  asser
1e58f 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
1e590 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72   pOp->p1<p->nCur
1e591 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6b 2e 70 43  sor );.  u.bk.pC
1e592 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d   = p->apCsr[pOp-
1e593 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  >p1];.  assert( 
1e594 75 2e 62 6b 2e 70 43 21 3d 30 20 29 3b 0a 20 20  u.bk.pC!=0 );.  
1e595 75 2e 62 6b 2e 70 43 72 73 72 20 3d 20 75 2e 62  u.bk.pCrsr = u.b
1e596 6b 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20  k.pC->pCursor;. 
1e597 20 69 66 28 20 75 2e 62 6b 2e 70 43 72 73 72 3d   if( u.bk.pCrsr=
1e598 3d 30 20 29 7b 0a 20 20 20 20 75 2e 62 6b 2e 72  =0 ){.    u.bk.r
1e599 65 73 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b  es = 1;.  }else{
1e59a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1e59b 33 42 74 72 65 65 4c 61 73 74 28 75 2e 62 6b 2e  3BtreeLast(u.bk.
1e59c 70 43 72 73 72 2c 20 26 75 2e 62 6b 2e 72 65 73  pCrsr, &u.bk.res
1e59d 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 6b 2e 70 43  );.  }.  u.bk.pC
1e59e 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29  ->nullRow = (u8)
1e59f 75 2e 62 6b 2e 72 65 73 3b 0a 20 20 75 2e 62 6b  u.bk.res;.  u.bk
1e5a0 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76  .pC->deferredMov
1e5a1 65 74 6f 20 3d 20 30 3b 0a 20 20 75 2e 62 6b 2e  eto = 0;.  u.bk.
1e5a2 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
1e5a3 20 3d 20 30 3b 0a 20 20 75 2e 62 6b 2e 70 43 2d   = 0;.  u.bk.pC-
1e5a4 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43  >cacheStatus = C
1e5a5 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 69 66  ACHE_STALE;.  if
1e5a6 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 75  ( pOp->p2>0 && u
1e5a7 2e 62 6b 2e 72 65 73 20 29 7b 0a 20 20 20 20 70  .bk.res ){.    p
1e5a8 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1e5a9 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1e5aa 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 6f 72  ../* Opcode: Sor
1e5ab 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  t P1 P2 * * *.**
1e5ac 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20  .** This opcode 
1e5ad 64 6f 65 73 20 65 78 61 63 74 6c 79 20 74 68 65  does exactly the
1e5ae 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20 4f   same thing as O
1e5af 50 5f 52 65 77 69 6e 64 20 65 78 63 65 70 74 20  P_Rewind except 
1e5b0 74 68 61 74 0a 2a 2a 20 69 74 20 69 6e 63 72 65  that.** it incre
1e5b1 6d 65 6e 74 73 20 61 6e 20 75 6e 64 6f 63 75 6d  ments an undocum
1e5b2 65 6e 74 65 64 20 67 6c 6f 62 61 6c 20 76 61 72  ented global var
1e5b3 69 61 62 6c 65 20 75 73 65 64 20 66 6f 72 20 74  iable used for t
1e5b4 65 73 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 6f  esting..**.** So
1e5b5 72 74 69 6e 67 20 69 73 20 61 63 63 6f 6d 70 6c  rting is accompl
1e5b6 69 73 68 65 64 20 62 79 20 77 72 69 74 69 6e 67  ished by writing
1e5b7 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 20   records into a 
1e5b8 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 2c 0a 2a  sorting index,.*
1e5b9 2a 20 74 68 65 6e 20 72 65 77 69 6e 64 69 6e 67  * then rewinding
1e5ba 20 74 68 61 74 20 69 6e 64 65 78 20 61 6e 64 20   that index and 
1e5bb 70 6c 61 79 69 6e 67 20 69 74 20 62 61 63 6b 20  playing it back 
1e5bc 66 72 6f 6d 20 62 65 67 69 6e 6e 69 6e 67 20 74  from beginning t
1e5bd 6f 0a 2a 2a 20 65 6e 64 2e 20 20 57 65 20 75 73  o.** end.  We us
1e5be 65 20 74 68 65 20 4f 50 5f 53 6f 72 74 20 6f 70  e the OP_Sort op
1e5bf 63 6f 64 65 20 69 6e 73 74 65 61 64 20 6f 66 20  code instead of 
1e5c0 4f 50 5f 52 65 77 69 6e 64 20 74 6f 20 64 6f 20  OP_Rewind to do 
1e5c1 74 68 65 0a 2a 2a 20 72 65 77 69 6e 64 69 6e 67  the.** rewinding
1e5c2 20 73 6f 20 74 68 61 74 20 74 68 65 20 67 6c 6f   so that the glo
1e5c3 62 61 6c 20 76 61 72 69 61 62 6c 65 20 77 69 6c  bal variable wil
1e5c4 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e 74 65 64  l be incremented
1e5c5 20 61 6e 64 0a 2a 2a 20 72 65 67 72 65 73 73 69   and.** regressi
1e5c6 6f 6e 20 74 65 73 74 73 20 63 61 6e 20 64 65 74  on tests can det
1e5c7 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f  ermine whether o
1e5c8 72 20 6e 6f 74 20 74 68 65 20 6f 70 74 69 6d 69  r not the optimi
1e5c9 7a 65 72 20 69 73 0a 2a 2a 20 63 6f 72 72 65 63  zer is.** correc
1e5ca 74 6c 79 20 6f 70 74 69 6d 69 7a 69 6e 67 20 6f  tly optimizing o
1e5cb 75 74 20 73 6f 72 74 73 2e 0a 2a 2f 0a 63 61 73  ut sorts..*/.cas
1e5cc 65 20 4f 50 5f 53 6f 72 74 3a 20 7b 20 20 20 20  e OP_Sort: {    
1e5cd 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23      /* jump */.#
1e5ce 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
1e5cf 54 0a 20 20 73 71 6c 69 74 65 33 5f 73 6f 72 74  T.  sqlite3_sort
1e5d0 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69  _count++;.  sqli
1e5d1 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74  te3_search_count
1e5d2 2d 2d 3b 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e  --;.#endif.  p->
1e5d3 61 43 6f 75 6e 74 65 72 5b 53 51 4c 49 54 45 5f  aCounter[SQLITE_
1e5d4 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 2d  STMTSTATUS_SORT-
1e5d5 31 5d 2b 2b 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20  1]++;.  /* Fall 
1e5d6 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f 50 5f  through into OP_
1e5d7 52 65 77 69 6e 64 20 2a 2f 0a 7d 0a 2f 2a 20 4f  Rewind */.}./* O
1e5d8 70 63 6f 64 65 3a 20 52 65 77 69 6e 64 20 50 31  pcode: Rewind P1
1e5d9 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1e5da 54 68 65 20 6e 65 78 74 20 75 73 65 20 6f 66 20  The next use of 
1e5db 74 68 65 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c  the Rowid or Col
1e5dc 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 74  umn or Next inst
1e5dd 72 75 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a  ruction for P1 .
1e5de 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72 20 74 6f  ** will refer to
1e5df 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79   the first entry
1e5e0 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1e5e1 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e   table or index.
1e5e2 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65  .** If the table
1e5e3 20 6f 72 20 69 6e 64 65 78 20 69 73 20 65 6d 70   or index is emp
1e5e4 74 79 20 61 6e 64 20 50 32 3e 30 2c 20 74 68 65  ty and P2>0, the
1e5e5 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65  n jump immediate
1e5e6 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20  ly to P2..** If 
1e5e7 50 32 20 69 73 20 30 20 6f 72 20 69 66 20 74 68  P2 is 0 or if th
1e5e8 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  e table or index
1e5e9 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66   is not empty, f
1e5ea 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74  all through.** t
1e5eb 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  o the following 
1e5ec 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a  instruction..*/.
1e5ed 63 61 73 65 20 4f 50 5f 52 65 77 69 6e 64 3a 20  case OP_Rewind: 
1e5ee 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70  {        /* jump
1e5ef 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f   */.#if 0  /* lo
1e5f0 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1e5f1 76 65 64 20 69 6e 74 6f 20 75 2e 62 6c 20 2a 2f  ved into u.bl */
1e5f2 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
1e5f3 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  C;.  BtCursor *p
1e5f4 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b  Crsr;.  int res;
1e5f5 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1e5f6 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1e5f7 20 69 6e 74 6f 20 75 2e 62 6c 20 2a 2f 0a 0a 20   into u.bl */.. 
1e5f8 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
1e5f9 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70  >=0 && pOp->p1<p
1e5fa 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75  ->nCursor );.  u
1e5fb 2e 62 6c 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73  .bl.pC = p->apCs
1e5fc 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  r[pOp->p1];.  as
1e5fd 73 65 72 74 28 20 75 2e 62 6c 2e 70 43 21 3d 30  sert( u.bl.pC!=0
1e5fe 20 29 3b 0a 20 20 69 66 28 20 28 75 2e 62 6c 2e   );.  if( (u.bl.
1e5ff 70 43 72 73 72 20 3d 20 75 2e 62 6c 2e 70 43 2d  pCrsr = u.bl.pC-
1e600 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a  >pCursor)!=0 ){.
1e601 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1e602 42 74 72 65 65 46 69 72 73 74 28 75 2e 62 6c 2e  BtreeFirst(u.bl.
1e603 70 43 72 73 72 2c 20 26 75 2e 62 6c 2e 72 65 73  pCrsr, &u.bl.res
1e604 29 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 43 2d 3e  );.    u.bl.pC->
1e605 61 74 46 69 72 73 74 20 3d 20 75 2e 62 6c 2e 72  atFirst = u.bl.r
1e606 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20  es==0 ?1:0;.    
1e607 75 2e 62 6c 2e 70 43 2d 3e 64 65 66 65 72 72 65  u.bl.pC->deferre
1e608 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20  dMoveto = 0;.   
1e609 20 75 2e 62 6c 2e 70 43 2d 3e 63 61 63 68 65 53   u.bl.pC->cacheS
1e60a 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54  tatus = CACHE_ST
1e60b 41 4c 45 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 43  ALE;.    u.bl.pC
1e60c 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d  ->rowidIsValid =
1e60d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
1e60e 20 75 2e 62 6c 2e 72 65 73 20 3d 20 31 3b 0a 20   u.bl.res = 1;. 
1e60f 20 7d 0a 20 20 75 2e 62 6c 2e 70 43 2d 3e 6e 75   }.  u.bl.pC->nu
1e610 6c 6c 52 6f 77 20 3d 20 28 75 38 29 75 2e 62 6c  llRow = (u8)u.bl
1e611 2e 72 65 73 3b 0a 20 20 61 73 73 65 72 74 28 20  .res;.  assert( 
1e612 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70  pOp->p2>0 && pOp
1e613 2d 3e 70 32 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20  ->p2<p->nOp );. 
1e614 20 69 66 28 20 75 2e 62 6c 2e 72 65 73 20 29 7b   if( u.bl.res ){
1e615 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
1e616 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65  2 - 1;.  }.  bre
1e617 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1e618 3a 20 4e 65 78 74 20 50 31 20 50 32 20 2a 20 2a  : Next P1 P2 * *
1e619 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63 65   *.**.** Advance
1e61a 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68   cursor P1 so th
1e61b 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  at it points to 
1e61c 74 68 65 20 6e 65 78 74 20 6b 65 79 2f 64 61 74  the next key/dat
1e61d 61 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a 2a  a pair in its.**
1e61e 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e   table or index.
1e61f 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e    If there are n
1e620 6f 20 6d 6f 72 65 20 6b 65 79 2f 76 61 6c 75 65  o more key/value
1e621 20 70 61 69 72 73 20 74 68 65 6e 20 66 61 6c 6c   pairs then fall
1e622 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74   through.** to t
1e623 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73  he following ins
1e624 74 72 75 63 74 69 6f 6e 2e 20 20 42 75 74 20 69  truction.  But i
1e625 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 64 76  f the cursor adv
1e626 61 6e 63 65 20 77 61 73 20 73 75 63 63 65 73 73  ance was success
1e627 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 6d 6d  ful,.** jump imm
1e628 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a  ediately to P2..
1e629 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 75 72  **.** The P1 cur
1e62a 73 6f 72 20 6d 75 73 74 20 62 65 20 66 6f 72 20  sor must be for 
1e62b 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f  a real table, no
1e62c 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  t a pseudo-table
1e62d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
1e62e 3a 20 50 72 65 76 0a 2a 2f 0a 2f 2a 20 4f 70 63  : Prev.*/./* Opc
1e62f 6f 64 65 3a 20 50 72 65 76 20 50 31 20 50 32 20  ode: Prev P1 P2 
1e630 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 61 63 6b  * * *.**.** Back
1e631 20 75 70 20 63 75 72 73 6f 72 20 50 31 20 73 6f   up cursor P1 so
1e632 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20   that it points 
1e633 74 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  to the previous 
1e634 6b 65 79 2f 64 61 74 61 20 70 61 69 72 20 69 6e  key/data pair in
1e635 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f 72   its.** table or
1e636 20 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65 72   index.  If ther
1e637 65 20 69 73 20 6e 6f 20 70 72 65 76 69 6f 75 73  e is no previous
1e638 20 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73   key/value pairs
1e639 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75   then fall throu
1e63a 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c  gh.** to the fol
1e63b 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69  lowing instructi
1e63c 6f 6e 2e 20 20 42 75 74 20 69 66 20 74 68 65 20  on.  But if the 
1e63d 63 75 72 73 6f 72 20 62 61 63 6b 75 70 20 77 61  cursor backup wa
1e63e 73 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a  s successful,.**
1e63f 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c   jump immediatel
1e640 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54  y to P2..**.** T
1e641 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73  he P1 cursor mus
1e642 74 20 62 65 20 66 6f 72 20 61 20 72 65 61 6c 20  t be for a real 
1e643 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65  table, not a pse
1e644 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61  udo-table..*/.ca
1e645 73 65 20 4f 50 5f 50 72 65 76 3a 20 20 20 20 20  se OP_Prev:     
1e646 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a       /* jump */.
1e647 63 61 73 65 20 4f 50 5f 4e 65 78 74 3a 20 7b 20  case OP_Next: { 
1e648 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a         /* jump *
1e649 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
1e64a 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e64b 64 20 69 6e 74 6f 20 75 2e 62 6d 20 2a 2f 0a 20  d into u.bm */. 
1e64c 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1e64d 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72  .  BtCursor *pCr
1e64e 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 23  sr;.  int res;.#
1e64f 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1e650 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1e651 6e 74 6f 20 75 2e 62 6d 20 2a 2f 0a 0a 20 20 43  nto u.bm */..  C
1e652 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55  HECK_FOR_INTERRU
1e653 50 54 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  PT;.  assert( pO
1e654 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d  p->p1>=0 && pOp-
1e655 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  >p1<p->nCursor )
1e656 3b 0a 20 20 75 2e 62 6d 2e 70 43 20 3d 20 70 2d  ;.  u.bm.pC = p-
1e657 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
1e658 0a 20 20 69 66 28 20 75 2e 62 6d 2e 70 43 3d 3d  .  if( u.bm.pC==
1e659 30 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b 20  0 ){.    break; 
1e65a 20 2f 2a 20 53 65 65 20 74 69 63 6b 65 74 20 23   /* See ticket #
1e65b 32 32 37 33 20 2a 2f 0a 20 20 7d 0a 20 20 75 2e  2273 */.  }.  u.
1e65c 62 6d 2e 70 43 72 73 72 20 3d 20 75 2e 62 6d 2e  bm.pCrsr = u.bm.
1e65d 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69  pC->pCursor;.  i
1e65e 66 28 20 75 2e 62 6d 2e 70 43 72 73 72 3d 3d 30  f( u.bm.pCrsr==0
1e65f 20 29 7b 0a 20 20 20 20 75 2e 62 6d 2e 70 43 2d   ){.    u.bm.pC-
1e660 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20  >nullRow = 1;.  
1e661 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 75    break;.  }.  u
1e662 2e 62 6d 2e 72 65 73 20 3d 20 31 3b 0a 20 20 61  .bm.res = 1;.  a
1e663 73 73 65 72 74 28 20 75 2e 62 6d 2e 70 43 2d 3e  ssert( u.bm.pC->
1e664 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d  deferredMoveto==
1e665 30 20 29 3b 0a 20 20 72 63 20 3d 20 70 4f 70 2d  0 );.  rc = pOp-
1e666 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 65 78 74  >opcode==OP_Next
1e667 20 3f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e   ? sqlite3BtreeN
1e668 65 78 74 28 75 2e 62 6d 2e 70 43 72 73 72 2c 20  ext(u.bm.pCrsr, 
1e669 26 75 2e 62 6d 2e 72 65 73 29 20 3a 0a 20 20 20  &u.bm.res) :.   
1e66a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e66b 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1e66c 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28  e3BtreePrevious(
1e66d 75 2e 62 6d 2e 70 43 72 73 72 2c 20 26 75 2e 62  u.bm.pCrsr, &u.b
1e66e 6d 2e 72 65 73 29 3b 0a 20 20 75 2e 62 6d 2e 70  m.res);.  u.bm.p
1e66f 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 38  C->nullRow = (u8
1e670 29 75 2e 62 6d 2e 72 65 73 3b 0a 20 20 75 2e 62  )u.bm.res;.  u.b
1e671 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75  m.pC->cacheStatu
1e672 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b  s = CACHE_STALE;
1e673 0a 20 20 69 66 28 20 75 2e 62 6d 2e 72 65 73 3d  .  if( u.bm.res=
1e674 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70  =0 ){.    pc = p
1e675 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20  Op->p2 - 1;.    
1e676 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 20 70 2d  if( pOp->p5 ) p-
1e677 3e 61 43 6f 75 6e 74 65 72 5b 70 4f 70 2d 3e 70  >aCounter[pOp->p
1e678 35 2d 31 5d 2b 2b 3b 0a 23 69 66 64 65 66 20 53  5-1]++;.#ifdef S
1e679 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20 73  QLITE_TEST.    s
1e67a 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f  qlite3_search_co
1e67b 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20  unt++;.#endif.  
1e67c 7d 0a 20 20 75 2e 62 6d 2e 70 43 2d 3e 72 6f 77  }.  u.bm.pC->row
1e67d 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20  idIsValid = 0;. 
1e67e 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1e67f 63 6f 64 65 3a 20 49 64 78 49 6e 73 65 72 74 20  code: IdxInsert 
1e680 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a  P1 P2 P3 * P5.**
1e681 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 32 20  .** Register P2 
1e682 68 6f 6c 64 73 20 61 20 53 51 4c 20 69 6e 64 65  holds a SQL inde
1e683 78 20 6b 65 79 20 6d 61 64 65 20 75 73 69 6e 67  x key made using
1e684 20 74 68 65 0a 2a 2a 20 4d 61 6b 65 52 65 63 6f   the.** MakeReco
1e685 72 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e  rd instructions.
1e686 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 72    This opcode wr
1e687 69 74 65 73 20 74 68 61 74 20 6b 65 79 0a 2a 2a  ites that key.**
1e688 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 65 78 20   into the index 
1e689 50 31 2e 20 20 44 61 74 61 20 66 6f 72 20 74 68  P1.  Data for th
1e68a 65 20 65 6e 74 72 79 20 69 73 20 6e 69 6c 2e 0a  e entry is nil..
1e68b 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 61 20 66 6c  **.** P3 is a fl
1e68c 61 67 20 74 68 61 74 20 70 72 6f 76 69 64 65 73  ag that provides
1e68d 20 61 20 68 69 6e 74 20 74 6f 20 74 68 65 20 62   a hint to the b
1e68e 2d 74 72 65 65 20 6c 61 79 65 72 20 74 68 61 74  -tree layer that
1e68f 20 74 68 69 73 0a 2a 2a 20 69 6e 73 65 72 74 20   this.** insert 
1e690 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20  is likely to be 
1e691 61 6e 20 61 70 70 65 6e 64 2e 0a 2a 2a 0a 2a 2a  an append..**.**
1e692 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   This instructio
1e693 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72  n only works for
1e694 20 69 6e 64 69 63 65 73 2e 20 20 54 68 65 20 65   indices.  The e
1e695 71 75 69 76 61 6c 65 6e 74 20 69 6e 73 74 72 75  quivalent instru
1e696 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 61 62  ction.** for tab
1e697 6c 65 73 20 69 73 20 4f 50 5f 49 6e 73 65 72 74  les is OP_Insert
1e698 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78  ..*/.case OP_Idx
1e699 49 6e 73 65 72 74 3a 20 7b 20 20 20 20 20 20 20  Insert: {       
1e69a 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69 66 20 30   /* in2 */.#if 0
1e69b 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1e69c 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e69d 75 2e 62 6e 20 2a 2f 0a 20 20 56 64 62 65 43 75  u.bn */.  VdbeCu
1e69e 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75  rsor *pC;.  BtCu
1e69f 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69  rsor *pCrsr;.  i
1e6a0 6e 74 20 6e 4b 65 79 3b 0a 20 20 63 6f 6e 73 74  nt nKey;.  const
1e6a1 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 23 65 6e   char *zKey;.#en
1e6a2 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1e6a3 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e6a4 6f 20 75 2e 62 6e 20 2a 2f 0a 0a 20 20 61 73 73  o u.bn */..  ass
1e6a5 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
1e6a6 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43  && pOp->p1<p->nC
1e6a7 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6e 2e  ursor );.  u.bn.
1e6a8 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f  pC = p->apCsr[pO
1e6a9 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  p->p1];.  assert
1e6aa 28 20 75 2e 62 6e 2e 70 43 21 3d 30 20 29 3b 0a  ( u.bn.pC!=0 );.
1e6ab 20 20 61 73 73 65 72 74 28 20 70 49 6e 32 2d 3e    assert( pIn2->
1e6ac 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62  flags & MEM_Blob
1e6ad 20 29 3b 0a 20 20 75 2e 62 6e 2e 70 43 72 73 72   );.  u.bn.pCrsr
1e6ae 20 3d 20 75 2e 62 6e 2e 70 43 2d 3e 70 43 75 72   = u.bn.pC->pCur
1e6af 73 6f 72 3b 0a 20 20 69 66 28 20 41 4c 57 41 59  sor;.  if( ALWAY
1e6b0 53 28 75 2e 62 6e 2e 70 43 72 73 72 21 3d 30 29  S(u.bn.pCrsr!=0)
1e6b1 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1e6b2 75 2e 62 6e 2e 70 43 2d 3e 69 73 54 61 62 6c 65  u.bn.pC->isTable
1e6b3 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  ==0 );.    rc = 
1e6b4 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 32 29  ExpandBlob(pIn2)
1e6b5 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
1e6b6 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1e6b7 20 75 2e 62 6e 2e 6e 4b 65 79 20 3d 20 70 49 6e   u.bn.nKey = pIn
1e6b8 32 2d 3e 6e 3b 0a 20 20 20 20 20 20 75 2e 62 6e  2->n;.      u.bn
1e6b9 2e 7a 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 7a 3b  .zKey = pIn2->z;
1e6ba 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1e6bb 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 75  te3BtreeInsert(u
1e6bc 2e 62 6e 2e 70 43 72 73 72 2c 20 75 2e 62 6e 2e  .bn.pCrsr, u.bn.
1e6bd 7a 4b 65 79 2c 20 75 2e 62 6e 2e 6e 4b 65 79 2c  zKey, u.bn.nKey,
1e6be 20 22 22 2c 20 30 2c 20 30 2c 20 70 4f 70 2d 3e   "", 0, 0, pOp->
1e6bf 70 33 2c 0a 20 20 20 20 20 20 20 20 20 20 28 28  p3,.          ((
1e6c0 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47  pOp->p5 & OPFLAG
1e6c1 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29 20  _USESEEKRESULT) 
1e6c2 3f 20 75 2e 62 6e 2e 70 43 2d 3e 73 65 65 6b 52  ? u.bn.pC->seekR
1e6c3 65 73 75 6c 74 20 3a 20 30 29 0a 20 20 20 20 20  esult : 0).     
1e6c4 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1e6c5 28 20 75 2e 62 6e 2e 70 43 2d 3e 64 65 66 65 72  ( u.bn.pC->defer
1e6c6 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a  redMoveto==0 );.
1e6c7 20 20 20 20 20 20 75 2e 62 6e 2e 70 43 2d 3e 63        u.bn.pC->c
1e6c8 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43  acheStatus = CAC
1e6c9 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 7d 0a  HE_STALE;.    }.
1e6ca 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1e6cb 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 44 65  /* Opcode: IdxDe
1e6cc 6c 65 74 65 20 50 31 20 50 32 20 50 33 20 2a 20  lete P1 P2 P3 * 
1e6cd 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74  *.**.** The cont
1e6ce 65 6e 74 20 6f 66 20 50 33 20 72 65 67 69 73 74  ent of P3 regist
1e6cf 65 72 73 20 73 74 61 72 74 69 6e 67 20 61 74 20  ers starting at 
1e6d0 72 65 67 69 73 74 65 72 20 50 32 20 66 6f 72 6d  register P2 form
1e6d1 0a 2a 2a 20 61 6e 20 75 6e 70 61 63 6b 65 64 20  .** an unpacked 
1e6d2 69 6e 64 65 78 20 6b 65 79 2e 20 54 68 69 73 20  index key. This 
1e6d3 6f 70 63 6f 64 65 20 72 65 6d 6f 76 65 73 20 74  opcode removes t
1e6d4 68 61 74 20 65 6e 74 72 79 20 66 72 6f 6d 20 74  hat entry from t
1e6d5 68 65 20 0a 2a 2a 20 69 6e 64 65 78 20 6f 70 65  he .** index ope
1e6d6 6e 65 64 20 62 79 20 63 75 72 73 6f 72 20 50 31  ned by cursor P1
1e6d7 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78  ..*/.case OP_Idx
1e6d8 44 65 6c 65 74 65 3a 20 7b 0a 23 69 66 20 30 20  Delete: {.#if 0 
1e6d9 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1e6da 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1e6db 2e 62 6f 20 2a 2f 0a 20 20 56 64 62 65 43 75 72  .bo */.  VdbeCur
1e6dc 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72  sor *pC;.  BtCur
1e6dd 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e  sor *pCrsr;.  in
1e6de 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 6b 65  t res;.  Unpacke
1e6df 64 52 65 63 6f 72 64 20 72 3b 0a 23 65 6e 64 69  dRecord r;.#endi
1e6e0 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1e6e1 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e6e2 75 2e 62 6f 20 2a 2f 0a 0a 20 20 61 73 73 65 72  u.bo */..  asser
1e6e3 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a  t( pOp->p3>0 );.
1e6e4 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1e6e5 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 70  2>0 && pOp->p2+p
1e6e6 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 2b  Op->p3<=p->nMem+
1e6e7 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  1 );.  assert( p
1e6e8 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
1e6e9 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  ->p1<p->nCursor 
1e6ea 29 3b 0a 20 20 75 2e 62 6f 2e 70 43 20 3d 20 70  );.  u.bo.pC = p
1e6eb 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d  ->apCsr[pOp->p1]
1e6ec 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 6f  ;.  assert( u.bo
1e6ed 2e 70 43 21 3d 30 20 29 3b 0a 20 20 75 2e 62 6f  .pC!=0 );.  u.bo
1e6ee 2e 70 43 72 73 72 20 3d 20 75 2e 62 6f 2e 70 43  .pCrsr = u.bo.pC
1e6ef 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28  ->pCursor;.  if(
1e6f0 20 41 4c 57 41 59 53 28 75 2e 62 6f 2e 70 43 72   ALWAYS(u.bo.pCr
1e6f1 73 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 75 2e  sr!=0) ){.    u.
1e6f2 62 6f 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20  bo.r.pKeyInfo = 
1e6f3 75 2e 62 6f 2e 70 43 2d 3e 70 4b 65 79 49 6e 66  u.bo.pC->pKeyInf
1e6f4 6f 3b 0a 20 20 20 20 75 2e 62 6f 2e 72 2e 6e 46  o;.    u.bo.r.nF
1e6f5 69 65 6c 64 20 3d 20 28 75 31 36 29 70 4f 70 2d  ield = (u16)pOp-
1e6f6 3e 70 33 3b 0a 20 20 20 20 75 2e 62 6f 2e 72 2e  >p3;.    u.bo.r.
1e6f7 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 75  flags = 0;.    u
1e6f8 2e 62 6f 2e 72 2e 61 4d 65 6d 20 3d 20 26 70 2d  .bo.r.aMem = &p-
1e6f9 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a  >aMem[pOp->p2];.
1e6fa 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1e6fb 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63  BtreeMovetoUnpac
1e6fc 6b 65 64 28 75 2e 62 6f 2e 70 43 72 73 72 2c 20  ked(u.bo.pCrsr, 
1e6fd 26 75 2e 62 6f 2e 72 2c 20 30 2c 20 30 2c 20 26  &u.bo.r, 0, 0, &
1e6fe 75 2e 62 6f 2e 72 65 73 29 3b 0a 20 20 20 20 69  u.bo.res);.    i
1e6ff 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1e700 20 26 26 20 75 2e 62 6f 2e 72 65 73 3d 3d 30 20   && u.bo.res==0 
1e701 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
1e702 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65  lite3BtreeDelete
1e703 28 75 2e 62 6f 2e 70 43 72 73 72 29 3b 0a 20 20  (u.bo.pCrsr);.  
1e704 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
1e705 75 2e 62 6f 2e 70 43 2d 3e 64 65 66 65 72 72 65  u.bo.pC->deferre
1e706 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20  dMoveto==0 );.  
1e707 20 20 75 2e 62 6f 2e 70 43 2d 3e 63 61 63 68 65    u.bo.pC->cache
1e708 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53  Status = CACHE_S
1e709 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 62 72 65 61  TALE;.  }.  brea
1e70a 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1e70b 20 49 64 78 52 6f 77 69 64 20 50 31 20 50 32 20   IdxRowid P1 P2 
1e70c 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74  * * *.**.** Writ
1e70d 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  e into register 
1e70e 50 32 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68  P2 an integer wh
1e70f 69 63 68 20 69 73 20 74 68 65 20 6c 61 73 74 20  ich is the last 
1e710 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72 65 63  entry in the rec
1e711 6f 72 64 20 61 74 0a 2a 2a 20 74 68 65 20 65 6e  ord at.** the en
1e712 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b  d of the index k
1e713 65 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ey pointed to by
1e714 20 63 75 72 73 6f 72 20 50 31 2e 20 20 54 68 69   cursor P1.  Thi
1e715 73 20 69 6e 74 65 67 65 72 20 73 68 6f 75 6c 64  s integer should
1e716 20 62 65 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64   be.** the rowid
1e717 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 65 6e   of the table en
1e718 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 68 69  try to which thi
1e719 73 20 69 6e 64 65 78 20 65 6e 74 72 79 20 70 6f  s index entry po
1e71a 69 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  ints..**.** See 
1e71b 61 6c 73 6f 3a 20 52 6f 77 69 64 2c 20 4d 61 6b  also: Rowid, Mak
1e71c 65 52 65 63 6f 72 64 2e 0a 2a 2f 0a 63 61 73 65  eRecord..*/.case
1e71d 20 4f 50 5f 49 64 78 52 6f 77 69 64 3a 20 7b 20   OP_IdxRowid: { 
1e71e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1e71f 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1e720 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
1e721 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e722 65 64 20 69 6e 74 6f 20 75 2e 62 70 20 2a 2f 0a  ed into u.bp */.
1e723 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
1e724 72 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  r;.  VdbeCursor 
1e725 2a 70 43 3b 0a 20 20 69 36 34 20 72 6f 77 69 64  *pC;.  i64 rowid
1e726 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1e727 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e728 64 20 69 6e 74 6f 20 75 2e 62 70 20 2a 2f 0a 0a  d into u.bp */..
1e729 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1e72a 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
1e72b 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1e72c 75 2e 62 70 2e 70 43 20 3d 20 70 2d 3e 61 70 43  u.bp.pC = p->apC
1e72d 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61  sr[pOp->p1];.  a
1e72e 73 73 65 72 74 28 20 75 2e 62 70 2e 70 43 21 3d  ssert( u.bp.pC!=
1e72f 30 20 29 3b 0a 20 20 75 2e 62 70 2e 70 43 72 73  0 );.  u.bp.pCrs
1e730 72 20 3d 20 75 2e 62 70 2e 70 43 2d 3e 70 43 75  r = u.bp.pC->pCu
1e731 72 73 6f 72 3b 0a 20 20 69 66 28 20 41 4c 57 41  rsor;.  if( ALWA
1e732 59 53 28 75 2e 62 70 2e 70 43 72 73 72 21 3d 30  YS(u.bp.pCrsr!=0
1e733 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  ) ){.    rc = sq
1e734 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d  lite3VdbeCursorM
1e735 6f 76 65 74 6f 28 75 2e 62 70 2e 70 43 29 3b 0a  oveto(u.bp.pC);.
1e736 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 63      if( NEVER(rc
1e737 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  ) ) goto abort_d
1e738 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
1e739 20 61 73 73 65 72 74 28 20 75 2e 62 70 2e 70 43   assert( u.bp.pC
1e73a 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
1e73b 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==0 );.    asser
1e73c 74 28 20 75 2e 62 70 2e 70 43 2d 3e 69 73 54 61  t( u.bp.pC->isTa
1e73d 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66  ble==0 );.    if
1e73e 28 20 21 75 2e 62 70 2e 70 43 2d 3e 6e 75 6c 6c  ( !u.bp.pC->null
1e73f 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 72 63 20  Row ){.      rc 
1e740 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78  = sqlite3VdbeIdx
1e741 52 6f 77 69 64 28 64 62 2c 20 75 2e 62 70 2e 70  Rowid(db, u.bp.p
1e742 43 72 73 72 2c 20 26 75 2e 62 70 2e 72 6f 77 69  Crsr, &u.bp.rowi
1e743 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  d);.      if( rc
1e744 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1e745 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72         goto abor
1e746 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1e747 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 4d 65        }.      Me
1e748 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75  mSetTypeFlag(pOu
1e749 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20  t, MEM_Int);.   
1e74a 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75     pOut->u.i = u
1e74b 2e 62 70 2e 72 6f 77 69 64 3b 0a 20 20 20 20 7d  .bp.rowid;.    }
1e74c 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1e74d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 47  ./* Opcode: IdxG
1e74e 45 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35  E P1 P2 P3 P4 P5
1e74f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 72 65  .**.** The P4 re
1e750 67 69 73 74 65 72 20 76 61 6c 75 65 73 20 62 65  gister values be
1e751 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 33 20  ginning with P3 
1e752 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64  form an unpacked
1e753 20 69 6e 64 65 78 20 0a 2a 2a 20 6b 65 79 20 74   index .** key t
1e754 68 61 74 20 6f 6d 69 74 73 20 74 68 65 20 52 4f  hat omits the RO
1e755 57 49 44 2e 20 20 43 6f 6d 70 61 72 65 20 74 68  WID.  Compare th
1e756 69 73 20 6b 65 79 20 76 61 6c 75 65 20 61 67 61  is key value aga
1e757 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78 20 0a  inst the index .
1e758 2a 2a 20 74 68 61 74 20 50 31 20 69 73 20 63 75  ** that P1 is cu
1e759 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67  rrently pointing
1e75a 20 74 6f 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68   to, ignoring th
1e75b 65 20 52 4f 57 49 44 20 6f 6e 20 74 68 65 20 50  e ROWID on the P
1e75c 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49  1 index..**.** I
1e75d 66 20 74 68 65 20 50 31 20 69 6e 64 65 78 20 65  f the P1 index e
1e75e 6e 74 72 79 20 69 73 20 67 72 65 61 74 65 72 20  ntry is greater 
1e75f 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
1e760 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 0a 2a   the key value.*
1e761 2a 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50  * then jump to P
1e762 32 2e 20 20 4f 74 68 65 72 77 69 73 65 20 66 61  2.  Otherwise fa
1e763 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  ll through to th
1e764 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69  e next instructi
1e765 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20  on..**.** If P5 
1e766 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e  is non-zero then
1e767 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20 69   the key value i
1e768 73 20 69 6e 63 72 65 61 73 65 64 20 62 79 20 61  s increased by a
1e769 6e 20 65 70 73 69 6c 6f 6e 20 0a 2a 2a 20 70 72  n epsilon .** pr
1e76a 69 6f 72 20 74 6f 20 74 68 65 20 63 6f 6d 70 61  ior to the compa
1e76b 72 69 73 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b  rison.  This mak
1e76c 65 20 74 68 65 20 6f 70 63 6f 64 65 20 77 6f 72  e the opcode wor
1e76d 6b 20 6c 69 6b 65 20 49 64 78 47 54 20 65 78 63  k like IdxGT exc
1e76e 65 70 74 0a 2a 2a 20 74 68 61 74 20 69 66 20 74  ept.** that if t
1e76f 68 65 20 6b 65 79 20 66 72 6f 6d 20 72 65 67 69  he key from regi
1e770 73 74 65 72 20 50 33 20 69 73 20 61 20 70 72 65  ster P3 is a pre
1e771 66 69 78 20 6f 66 20 74 68 65 20 6b 65 79 20 69  fix of the key i
1e772 6e 20 74 68 65 20 63 75 72 73 6f 72 2c 0a 2a 2a  n the cursor,.**
1e773 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 66   the result is f
1e774 61 6c 73 65 20 77 68 65 72 65 61 73 20 69 74 20  alse whereas it 
1e775 77 6f 75 6c 64 20 62 65 20 74 72 75 65 20 77 69  would be true wi
1e776 74 68 20 49 64 78 47 54 2e 0a 2a 2f 0a 2f 2a 20  th IdxGT..*/./* 
1e777 4f 70 63 6f 64 65 3a 20 49 64 78 4c 54 20 50 31  Opcode: IdxLT P1
1e778 20 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a   P2 P3 * P5.**.*
1e779 2a 20 54 68 65 20 50 34 20 72 65 67 69 73 74 65  * The P4 registe
1e77a 72 20 76 61 6c 75 65 73 20 62 65 67 69 6e 6e 69  r values beginni
1e77b 6e 67 20 77 69 74 68 20 50 33 20 66 6f 72 6d 20  ng with P3 form 
1e77c 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65  an unpacked inde
1e77d 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61 74 20 6f  x .** key that o
1e77e 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44 2e 20  mits the ROWID. 
1e77f 20 43 6f 6d 70 61 72 65 20 74 68 69 73 20 6b 65   Compare this ke
1e780 79 20 76 61 6c 75 65 20 61 67 61 69 6e 73 74 20  y value against 
1e781 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 74 68  the index .** th
1e782 61 74 20 50 31 20 69 73 20 63 75 72 72 65 6e 74  at P1 is current
1e783 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2c 20  ly pointing to, 
1e784 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 52 4f 57  ignoring the ROW
1e785 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69 6e 64  ID on the P1 ind
1e786 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ex..**.** If the
1e787 20 50 31 20 69 6e 64 65 78 20 65 6e 74 72 79 20   P1 index entry 
1e788 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65  is less than the
1e789 20 6b 65 79 20 76 61 6c 75 65 20 74 68 65 6e 20   key value then 
1e78a 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 20 4f  jump to P2..** O
1e78b 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 74 68  therwise fall th
1e78c 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78  rough to the nex
1e78d 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  t instruction..*
1e78e 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 20 6e 6f  *.** If P5 is no
1e78f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 65 20  n-zero then the 
1e790 6b 65 79 20 76 61 6c 75 65 20 69 73 20 69 6e 63  key value is inc
1e791 72 65 61 73 65 64 20 62 79 20 61 6e 20 65 70 73  reased by an eps
1e792 69 6c 6f 6e 20 70 72 69 6f 72 20 0a 2a 2a 20 74  ilon prior .** t
1e793 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  o the comparison
1e794 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68  .  This makes th
1e795 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20 6c 69  e opcode work li
1e796 6b 65 20 49 64 78 4c 45 2e 0a 2a 2f 0a 63 61 73  ke IdxLE..*/.cas
1e797 65 20 4f 50 5f 49 64 78 4c 54 3a 20 20 20 20 20  e OP_IdxLT:     
1e798 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1e799 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78  3 */.case OP_Idx
1e79a 47 45 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  GE: {        /* 
1e79b 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 66  jump, in3 */.#if
1e79c 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1e79d 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e79e 6f 20 75 2e 62 71 20 2a 2f 0a 20 20 56 64 62 65  o u.bq */.  Vdbe
1e79f 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 6e  Cursor *pC;.  in
1e7a0 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 6b 65  t res;.  Unpacke
1e7a1 64 52 65 63 6f 72 64 20 72 3b 0a 23 65 6e 64 69  dRecord r;.#endi
1e7a2 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1e7a3 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e7a4 75 2e 62 71 20 2a 2f 0a 0a 20 20 61 73 73 65 72  u.bq */..  asser
1e7a5 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
1e7a6 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72   pOp->p1<p->nCur
1e7a7 73 6f 72 20 29 3b 0a 20 20 75 2e 62 71 2e 70 43  sor );.  u.bq.pC
1e7a8 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d   = p->apCsr[pOp-
1e7a9 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  >p1];.  assert( 
1e7aa 75 2e 62 71 2e 70 43 21 3d 30 20 29 3b 0a 20 20  u.bq.pC!=0 );.  
1e7ab 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 71 2e  if( ALWAYS(u.bq.
1e7ac 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 29 20  pC->pCursor!=0) 
1e7ad 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  ){.    assert( u
1e7ae 2e 62 71 2e 70 43 2d 3e 64 65 66 65 72 72 65 64  .bq.pC->deferred
1e7af 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20  Moveto==0 );.   
1e7b0 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 35   assert( pOp->p5
1e7b1 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 35 3d 3d  ==0 || pOp->p5==
1e7b2 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  1 );.    assert(
1e7b3 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34   pOp->p4type==P4
1e7b4 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 75 2e  _INT32 );.    u.
1e7b5 62 71 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20  bq.r.pKeyInfo = 
1e7b6 75 2e 62 71 2e 70 43 2d 3e 70 4b 65 79 49 6e 66  u.bq.pC->pKeyInf
1e7b7 6f 3b 0a 20 20 20 20 75 2e 62 71 2e 72 2e 6e 46  o;.    u.bq.r.nF
1e7b8 69 65 6c 64 20 3d 20 28 75 31 36 29 70 4f 70 2d  ield = (u16)pOp-
1e7b9 3e 70 34 2e 69 3b 0a 20 20 20 20 69 66 28 20 70  >p4.i;.    if( p
1e7ba 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 20 20  Op->p5 ){.      
1e7bb 75 2e 62 71 2e 72 2e 66 6c 61 67 73 20 3d 20 55  u.bq.r.flags = U
1e7bc 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20  NPACKED_INCRKEY 
1e7bd 7c 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52  | UNPACKED_IGNOR
1e7be 45 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 65 6c  E_ROWID;.    }el
1e7bf 73 65 7b 0a 20 20 20 20 20 20 75 2e 62 71 2e 72  se{.      u.bq.r
1e7c0 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45  .flags = UNPACKE
1e7c1 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 3b 0a  D_IGNORE_ROWID;.
1e7c2 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 71 2e 72      }.    u.bq.r
1e7c3 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d  .aMem = &p->aMem
1e7c4 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 72  [pOp->p3];.    r
1e7c5 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49  c = sqlite3VdbeI
1e7c6 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 75 2e 62  dxKeyCompare(u.b
1e7c7 71 2e 70 43 2c 20 26 75 2e 62 71 2e 72 2c 20 26  q.pC, &u.bq.r, &
1e7c8 75 2e 62 71 2e 72 65 73 29 3b 0a 20 20 20 20 69  u.bq.res);.    i
1e7c9 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  f( pOp->opcode==
1e7ca 4f 50 5f 49 64 78 4c 54 20 29 7b 0a 20 20 20 20  OP_IdxLT ){.    
1e7cb 20 20 75 2e 62 71 2e 72 65 73 20 3d 20 2d 75 2e    u.bq.res = -u.
1e7cc 62 71 2e 72 65 73 3b 0a 20 20 20 20 7d 65 6c 73  bq.res;.    }els
1e7cd 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e{.      assert(
1e7ce 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
1e7cf 5f 49 64 78 47 45 20 29 3b 0a 20 20 20 20 20 20  _IdxGE );.      
1e7d0 75 2e 62 71 2e 72 65 73 2b 2b 3b 0a 20 20 20 20  u.bq.res++;.    
1e7d1 7d 0a 20 20 20 20 69 66 28 20 75 2e 62 71 2e 72  }.    if( u.bq.r
1e7d2 65 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 63  es>0 ){.      pc
1e7d3 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 20 3b   = pOp->p2 - 1 ;
1e7d4 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65  .    }.  }.  bre
1e7d5 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1e7d6 3a 20 44 65 73 74 72 6f 79 20 50 31 20 50 32 20  : Destroy P1 P2 
1e7d7 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c  P3 * *.**.** Del
1e7d8 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20 64 61  ete an entire da
1e7d9 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20  tabase table or 
1e7da 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74  index whose root
1e7db 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74   page in the dat
1e7dc 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 73  abase.** file is
1e7dd 20 67 69 76 65 6e 20 62 79 20 50 31 2e 0a 2a 2a   given by P1..**
1e7de 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65  .** The table be
1e7df 69 6e 67 20 64 65 73 74 72 6f 79 65 64 20 69 73  ing destroyed is
1e7e0 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74   in the main dat
1e7e1 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 33  abase file if P3
1e7e2 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 33 3d 3d  ==0.  If.** P3==
1e7e3 31 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65  1 then the table
1e7e4 20 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73 20   to be clear is 
1e7e5 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79  in the auxiliary
1e7e6 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a   database file.*
1e7e7 2a 20 74 68 61 74 20 69 73 20 75 73 65 64 20 74  * that is used t
1e7e8 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20 63  o store tables c
1e7e9 72 65 61 74 65 20 75 73 69 6e 67 20 43 52 45 41  reate using CREA
1e7ea 54 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41 42  TE TEMPORARY TAB
1e7eb 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 54  LE..**.** If AUT
1e7ec 4f 56 41 43 55 55 4d 20 69 73 20 65 6e 61 62 6c  OVACUUM is enabl
1e7ed 65 64 20 74 68 65 6e 20 69 74 20 69 73 20 70 6f  ed then it is po
1e7ee 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74  ssible that anot
1e7ef 68 65 72 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a  her root page.**
1e7f0 20 6d 69 67 68 74 20 62 65 20 6d 6f 76 65 64 20   might be moved 
1e7f1 69 6e 74 6f 20 74 68 65 20 6e 65 77 6c 79 20 64  into the newly d
1e7f2 65 6c 65 74 65 64 20 72 6f 6f 74 20 70 61 67 65  eleted root page
1e7f3 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65   in order to kee
1e7f4 70 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61  p all.** root pa
1e7f5 67 65 73 20 63 6f 6e 74 69 67 75 6f 75 73 20 61  ges contiguous a
1e7f6 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
1e7f7 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  of the database.
1e7f8 20 20 54 68 65 20 66 6f 72 6d 65 72 0a 2a 2a 20    The former.** 
1e7f9 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f 6f  value of the roo
1e7fa 74 20 70 61 67 65 20 74 68 61 74 20 6d 6f 76 65  t page that move
1e7fb 64 20 2d 20 69 74 73 20 76 61 6c 75 65 20 62 65  d - its value be
1e7fc 66 6f 72 65 20 74 68 65 20 6d 6f 76 65 20 6f 63  fore the move oc
1e7fd 63 75 72 72 65 64 20 2d 0a 2a 2a 20 69 73 20 73  curred -.** is s
1e7fe 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65  tored in registe
1e7ff 72 20 50 32 2e 20 20 49 66 20 6e 6f 20 70 61 67  r P2.  If no pag
1e800 65 20 0a 2a 2a 20 6d 6f 76 65 6d 65 6e 74 20 77  e .** movement w
1e801 61 73 20 72 65 71 75 69 72 65 64 20 28 62 65 63  as required (bec
1e802 61 75 73 65 20 74 68 65 20 74 61 62 6c 65 20 62  ause the table b
1e803 65 69 6e 67 20 64 72 6f 70 70 65 64 20 77 61 73  eing dropped was
1e804 20 61 6c 72 65 61 64 79 20 0a 2a 2a 20 74 68 65   already .** the
1e805 20 6c 61 73 74 20 6f 6e 65 20 69 6e 20 74 68 65   last one in the
1e806 20 64 61 74 61 62 61 73 65 29 20 74 68 65 6e 20   database) then 
1e807 61 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 65 64  a zero is stored
1e808 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e   in register P2.
1e809 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55  .** If AUTOVACUU
1e80a 4d 20 69 73 20 64 69 73 61 62 6c 65 64 20 74 68  M is disabled th
1e80b 65 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 74 6f  en a zero is sto
1e80c 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20  red in register 
1e80d 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  P2..**.** See al
1e80e 73 6f 3a 20 43 6c 65 61 72 0a 2a 2f 0a 63 61 73  so: Clear.*/.cas
1e80f 65 20 4f 50 5f 44 65 73 74 72 6f 79 3a 20 7b 20  e OP_Destroy: { 
1e810 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72      /* out2-prer
1e811 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20  elease */.#if 0 
1e812 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1e813 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1e814 2e 62 72 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 6f  .br */.  int iMo
1e815 76 65 64 3b 0a 20 20 69 6e 74 20 69 43 6e 74 3b  ved;.  int iCnt;
1e816 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b 0a  .  Vdbe *pVdbe;.
1e817 20 20 69 6e 74 20 69 44 62 3b 0a 23 65 6e 64 69    int iDb;.#endi
1e818 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1e819 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e81a 75 2e 62 72 20 2a 2f 0a 23 69 66 6e 64 65 66 20  u.br */.#ifndef 
1e81b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1e81c 55 41 4c 54 41 42 4c 45 0a 20 20 75 2e 62 72 2e  UALTABLE.  u.br.
1e81d 69 43 6e 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28  iCnt = 0;.  for(
1e81e 75 2e 62 72 2e 70 56 64 62 65 3d 64 62 2d 3e 70  u.br.pVdbe=db->p
1e81f 56 64 62 65 3b 20 75 2e 62 72 2e 70 56 64 62 65  Vdbe; u.br.pVdbe
1e820 3b 20 75 2e 62 72 2e 70 56 64 62 65 20 3d 20 75  ; u.br.pVdbe = u
1e821 2e 62 72 2e 70 56 64 62 65 2d 3e 70 4e 65 78 74  .br.pVdbe->pNext
1e822 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 62 72 2e  ){.    if( u.br.
1e823 70 56 64 62 65 2d 3e 6d 61 67 69 63 3d 3d 56 44  pVdbe->magic==VD
1e824 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 26 20  BE_MAGIC_RUN && 
1e825 75 2e 62 72 2e 70 56 64 62 65 2d 3e 69 6e 56 74  u.br.pVdbe->inVt
1e826 61 62 4d 65 74 68 6f 64 3c 32 20 26 26 20 75 2e  abMethod<2 && u.
1e827 62 72 2e 70 56 64 62 65 2d 3e 70 63 3e 3d 30 20  br.pVdbe->pc>=0 
1e828 29 7b 0a 20 20 20 20 20 20 75 2e 62 72 2e 69 43  ){.      u.br.iC
1e829 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  nt++;.    }.  }.
1e82a 23 65 6c 73 65 0a 20 20 75 2e 62 72 2e 69 43 6e  #else.  u.br.iCn
1e82b 74 20 3d 20 64 62 2d 3e 61 63 74 69 76 65 56 64  t = db->activeVd
1e82c 62 65 43 6e 74 3b 0a 23 65 6e 64 69 66 0a 20 20  beCnt;.#endif.  
1e82d 69 66 28 20 75 2e 62 72 2e 69 43 6e 74 3e 31 20  if( u.br.iCnt>1 
1e82e 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
1e82f 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 70  TE_LOCKED;.    p
1e830 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20  ->errorAction = 
1e831 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 7d 65 6c 73  OE_Abort;.  }els
1e832 65 7b 0a 20 20 20 20 75 2e 62 72 2e 69 44 62 20  e{.    u.br.iDb 
1e833 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20 61  = pOp->p3;.    a
1e834 73 73 65 72 74 28 20 75 2e 62 72 2e 69 43 6e 74  ssert( u.br.iCnt
1e835 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==1 );.    asser
1e836 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b  t( (p->btreeMask
1e837 20 26 20 28 31 3c 3c 75 2e 62 72 2e 69 44 62 29   & (1<<u.br.iDb)
1e838 29 21 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d  )!=0 );.    rc =
1e839 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f   sqlite3BtreeDro
1e83a 70 54 61 62 6c 65 28 64 62 2d 3e 61 44 62 5b 75  pTable(db->aDb[u
1e83b 2e 62 72 2e 69 44 62 5d 2e 70 42 74 2c 20 70 4f  .br.iDb].pBt, pO
1e83c 70 2d 3e 70 31 2c 20 26 75 2e 62 72 2e 69 4d 6f  p->p1, &u.br.iMo
1e83d 76 65 64 29 3b 0a 20 20 20 20 4d 65 6d 53 65 74  ved);.    MemSet
1e83e 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d  TypeFlag(pOut, M
1e83f 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 70 4f 75  EM_Int);.    pOu
1e840 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 72 2e 69 4d  t->u.i = u.br.iM
1e841 6f 76 65 64 3b 0a 23 69 66 6e 64 65 66 20 53 51  oved;.#ifndef SQ
1e842 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
1e843 43 55 55 4d 0a 20 20 20 20 69 66 28 20 72 63 3d  CUUM.    if( rc=
1e844 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 75 2e  =SQLITE_OK && u.
1e845 62 72 2e 69 4d 6f 76 65 64 21 3d 30 20 29 7b 0a  br.iMoved!=0 ){.
1e846 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6f        sqlite3Roo
1e847 74 50 61 67 65 4d 6f 76 65 64 28 26 64 62 2d 3e  tPageMoved(&db->
1e848 61 44 62 5b 75 2e 62 72 2e 69 44 62 5d 2c 20 75  aDb[u.br.iDb], u
1e849 2e 62 72 2e 69 4d 6f 76 65 64 2c 20 70 4f 70 2d  .br.iMoved, pOp-
1e84a 3e 70 31 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  >p1);.    }.#end
1e84b 69 66 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  if.  }.  break;.
1e84c 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6c  }../* Opcode: Cl
1e84d 65 61 72 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a  ear P1 P2 P3.**.
1e84e 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 63 6f  ** Delete all co
1e84f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64 61  ntents of the da
1e850 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20  tabase table or 
1e851 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74  index whose root
1e852 20 70 61 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20   page.** in the 
1e853 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73  database file is
1e854 20 67 69 76 65 6e 20 62 79 20 50 31 2e 20 20 42   given by P1.  B
1e855 75 74 2c 20 75 6e 6c 69 6b 65 20 44 65 73 74 72  ut, unlike Destr
1e856 6f 79 2c 20 64 6f 20 6e 6f 74 0a 2a 2a 20 72 65  oy, do not.** re
1e857 6d 6f 76 65 20 74 68 65 20 74 61 62 6c 65 20 6f  move the table o
1e858 72 20 69 6e 64 65 78 20 66 72 6f 6d 20 74 68 65  r index from the
1e859 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
1e85a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20  **.** The table 
1e85b 62 65 69 6e 67 20 63 6c 65 61 72 20 69 73 20 69  being clear is i
1e85c 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  n the main datab
1e85d 61 73 65 20 66 69 6c 65 20 69 66 20 50 32 3d 3d  ase file if P2==
1e85e 30 2e 20 20 49 66 0a 2a 2a 20 50 32 3d 3d 31 20  0.  If.** P2==1 
1e85f 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 74  then the table t
1e860 6f 20 62 65 20 63 6c 65 61 72 20 69 73 20 69 6e  o be clear is in
1e861 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 64   the auxiliary d
1e862 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20  atabase file.** 
1e863 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f 20  that is used to 
1e864 73 74 6f 72 65 20 74 61 62 6c 65 73 20 63 72 65  store tables cre
1e865 61 74 65 20 75 73 69 6e 67 20 43 52 45 41 54 45  ate using CREATE
1e866 20 54 45 4d 50 4f 52 41 52 59 20 54 41 42 4c 45   TEMPORARY TABLE
1e867 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50  ..**.** If the P
1e868 33 20 76 61 6c 75 65 20 69 73 20 6e 6f 6e 2d 7a  3 value is non-z
1e869 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 74 61  ero, then the ta
1e86a 62 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f 20  ble referred to 
1e86b 6d 75 73 74 20 62 65 20 61 6e 0a 2a 2a 20 69 6e  must be an.** in
1e86c 74 6b 65 79 20 74 61 62 6c 65 20 28 61 6e 20 53  tkey table (an S
1e86d 51 4c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 6e  QL table, not an
1e86e 20 69 6e 64 65 78 29 2e 20 49 6e 20 74 68 69 73   index). In this
1e86f 20 63 61 73 65 20 74 68 65 20 72 6f 77 20 63 68   case the row ch
1e870 61 6e 67 65 20 0a 2a 2a 20 63 6f 75 6e 74 20 69  ange .** count i
1e871 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79  s incremented by
1e872 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
1e873 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65  ows in the table
1e874 20 62 65 69 6e 67 20 63 6c 65 61 72 65 64 2e 20   being cleared. 
1e875 0a 2a 2a 20 49 66 20 50 33 20 69 73 20 67 72 65  .** If P3 is gre
1e876 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20  ater than zero, 
1e877 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 73  then the value s
1e878 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65  tored in registe
1e879 72 20 50 33 20 69 73 0a 2a 2a 20 61 6c 73 6f 20  r P3 is.** also 
1e87a 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74  incremented by t
1e87b 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
1e87c 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 62  s in the table b
1e87d 65 69 6e 67 20 63 6c 65 61 72 65 64 2e 0a 2a 2a  eing cleared..**
1e87e 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 44 65  .** See also: De
1e87f 73 74 72 6f 79 0a 2a 2f 0a 63 61 73 65 20 4f 50  stroy.*/.case OP
1e880 5f 43 6c 65 61 72 3a 20 7b 0a 23 69 66 20 30 20  _Clear: {.#if 0 
1e881 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1e882 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1e883 2e 62 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68  .bs */.  int nCh
1e884 61 6e 67 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  ange;.#endif /* 
1e885 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1e886 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 73 20  moved into u.bs 
1e887 2a 2f 0a 0a 20 20 75 2e 62 73 2e 6e 43 68 61 6e  */..  u.bs.nChan
1e888 67 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  ge = 0;.  assert
1e889 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20  ( (p->btreeMask 
1e88a 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 32 29 29 21  & (1<<pOp->p2))!
1e88b 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  =0 );.  rc = sql
1e88c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61  ite3BtreeClearTa
1e88d 62 6c 65 28 0a 20 20 20 20 20 20 64 62 2d 3e 61  ble(.      db->a
1e88e 44 62 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42 74 2c  Db[pOp->p2].pBt,
1e88f 20 70 4f 70 2d 3e 70 31 2c 20 28 70 4f 70 2d 3e   pOp->p1, (pOp->
1e890 70 33 20 3f 20 26 75 2e 62 73 2e 6e 43 68 61 6e  p3 ? &u.bs.nChan
1e891 67 65 20 3a 20 30 29 0a 20 20 29 3b 0a 20 20 69  ge : 0).  );.  i
1e892 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 20  f( pOp->p3 ){.  
1e893 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 2b 3d 20    p->nChange += 
1e894 75 2e 62 73 2e 6e 43 68 61 6e 67 65 3b 0a 20 20  u.bs.nChange;.  
1e895 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 3e 30 20    if( pOp->p3>0 
1e896 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d  ){.      p->aMem
1e897 5b 70 4f 70 2d 3e 70 33 5d 2e 75 2e 69 20 2b 3d  [pOp->p3].u.i +=
1e898 20 75 2e 62 73 2e 6e 43 68 61 6e 67 65 3b 0a 20   u.bs.nChange;. 
1e899 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b     }.  }.  break
1e89a 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1e89b 43 72 65 61 74 65 54 61 62 6c 65 20 50 31 20 50  CreateTable P1 P
1e89c 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c  2 * * *.**.** Al
1e89d 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 74 61 62  locate a new tab
1e89e 6c 65 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64  le in the main d
1e89f 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20  atabase file if 
1e8a0 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65 0a  P1==0 or in the.
1e8a1 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61 74  ** auxiliary dat
1e8a2 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 31  abase file if P1
1e8a3 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 74 74  ==1 or in an att
1e8a4 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20 69  ached database i
1e8a5 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69 74  f.** P1>1.  Writ
1e8a6 65 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  e the root page 
1e8a7 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65  number of the ne
1e8a8 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a 20  w table into.** 
1e8a9 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 0a 2a  register P2.**.*
1e8aa 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65  * The difference
1e8ab 20 62 65 74 77 65 65 6e 20 61 20 74 61 62 6c 65   between a table
1e8ac 20 61 6e 64 20 61 6e 20 69 6e 64 65 78 20 69 73   and an index is
1e8ad 20 74 68 69 73 3a 20 20 41 20 74 61 62 6c 65 20   this:  A table 
1e8ae 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20 61 20 34  must.** have a 4
1e8af 2d 62 79 74 65 20 69 6e 74 65 67 65 72 20 6b 65  -byte integer ke
1e8b0 79 20 61 6e 64 20 63 61 6e 20 68 61 76 65 20 61  y and can have a
1e8b1 72 62 69 74 72 61 72 79 20 64 61 74 61 2e 20 20  rbitrary data.  
1e8b2 41 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 61 73 20  An index.** has 
1e8b3 61 6e 20 61 72 62 69 74 72 61 72 79 20 6b 65 79  an arbitrary key
1e8b4 20 62 75 74 20 6e 6f 20 64 61 74 61 2e 0a 2a 2a   but no data..**
1e8b5 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43 72  .** See also: Cr
1e8b6 65 61 74 65 49 6e 64 65 78 0a 2a 2f 0a 2f 2a 20  eateIndex.*/./* 
1e8b7 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 65 49 6e  Opcode: CreateIn
1e8b8 64 65 78 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  dex P1 P2 * * *.
1e8b9 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  **.** Allocate a
1e8ba 20 6e 65 77 20 69 6e 64 65 78 20 69 6e 20 74 68   new index in th
1e8bb 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
1e8bc 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f 72  file if P1==0 or
1e8bd 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69 6c   in the.** auxil
1e8be 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66 69  iary database fi
1e8bf 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f 72 20 69  le if P1==1 or i
1e8c0 6e 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61  n an attached da
1e8c1 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 50 31 3e  tabase if.** P1>
1e8c2 31 2e 20 20 57 72 69 74 65 20 74 68 65 20 72 6f  1.  Write the ro
1e8c3 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  ot page number o
1e8c4 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20  f the new table 
1e8c5 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72  into.** register
1e8c6 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 64   P2..**.** See d
1e8c7 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e 20  ocumentation on 
1e8c8 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 66  OP_CreateTable f
1e8c9 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
1e8ca 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61  formation..*/.ca
1e8cb 73 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64 65  se OP_CreateInde
1e8cc 78 3a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  x:            /*
1e8cd 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65   out2-prerelease
1e8ce 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 72 65 61   */.case OP_Crea
1e8cf 74 65 54 61 62 6c 65 3a 20 7b 20 20 20 20 20 20  teTable: {      
1e8d0 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72      /* out2-prer
1e8d1 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20  elease */.#if 0 
1e8d2 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1e8d3 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1e8d4 2e 62 74 20 2a 2f 0a 20 20 69 6e 74 20 70 67 6e  .bt */.  int pgn
1e8d5 6f 3b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a  o;.  int flags;.
1e8d6 20 20 44 62 20 2a 70 44 62 3b 0a 23 65 6e 64 69    Db *pDb;.#endi
1e8d7 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1e8d8 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e8d9 75 2e 62 74 20 2a 2f 0a 0a 20 20 75 2e 62 74 2e  u.bt */..  u.bt.
1e8da 70 67 6e 6f 20 3d 20 30 3b 0a 20 20 61 73 73 65  pgno = 0;.  asse
1e8db 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1e8dc 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44  & pOp->p1<db->nD
1e8dd 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  b );.  assert( (
1e8de 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28  p->btreeMask & (
1e8df 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20  1<<pOp->p1))!=0 
1e8e0 29 3b 0a 20 20 75 2e 62 74 2e 70 44 62 20 3d 20  );.  u.bt.pDb = 
1e8e1 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31  &db->aDb[pOp->p1
1e8e2 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62  ];.  assert( u.b
1e8e3 74 2e 70 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b  t.pDb->pBt!=0 );
1e8e4 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f  .  if( pOp->opco
1e8e5 64 65 3d 3d 4f 50 5f 43 72 65 61 74 65 54 61 62  de==OP_CreateTab
1e8e6 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 75 2e 62  le ){.    /* u.b
1e8e7 74 2e 66 6c 61 67 73 20 3d 20 42 54 52 45 45 5f  t.flags = BTREE_
1e8e8 49 4e 54 4b 45 59 3b 20 2a 2f 0a 20 20 20 20 75  INTKEY; */.    u
1e8e9 2e 62 74 2e 66 6c 61 67 73 20 3d 20 42 54 52 45  .bt.flags = BTRE
1e8ea 45 5f 4c 45 41 46 44 41 54 41 7c 42 54 52 45 45  E_LEAFDATA|BTREE
1e8eb 5f 49 4e 54 4b 45 59 3b 0a 20 20 7d 65 6c 73 65  _INTKEY;.  }else
1e8ec 7b 0a 20 20 20 20 75 2e 62 74 2e 66 6c 61 67 73  {.    u.bt.flags
1e8ed 20 3d 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54   = BTREE_ZERODAT
1e8ee 41 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71  A;.  }.  rc = sq
1e8ef 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65  lite3BtreeCreate
1e8f0 54 61 62 6c 65 28 75 2e 62 74 2e 70 44 62 2d 3e  Table(u.bt.pDb->
1e8f1 70 42 74 2c 20 26 75 2e 62 74 2e 70 67 6e 6f 2c  pBt, &u.bt.pgno,
1e8f2 20 75 2e 62 74 2e 66 6c 61 67 73 29 3b 0a 20 20   u.bt.flags);.  
1e8f3 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 74  pOut->u.i = u.bt
1e8f4 2e 70 67 6e 6f 3b 0a 20 20 4d 65 6d 53 65 74 54  .pgno;.  MemSetT
1e8f5 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45  ypeFlag(pOut, ME
1e8f6 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b  M_Int);.  break;
1e8f7 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50  .}../* Opcode: P
1e8f8 61 72 73 65 53 63 68 65 6d 61 20 50 31 20 50 32  arseSchema P1 P2
1e8f9 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65   * P4 *.**.** Re
1e8fa 61 64 20 61 6e 64 20 70 61 72 73 65 20 61 6c 6c  ad and parse all
1e8fb 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68   entries from th
1e8fc 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20  e SQLITE_MASTER 
1e8fd 74 61 62 6c 65 20 6f 66 20 64 61 74 61 62 61 73  table of databas
1e8fe 65 20 50 31 0a 2a 2a 20 74 68 61 74 20 6d 61 74  e P1.** that mat
1e8ff 63 68 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  ch the WHERE cla
1e900 75 73 65 20 50 34 2e 20 20 50 32 20 69 73 20 74  use P4.  P2 is t
1e901 68 65 20 22 66 6f 72 63 65 22 20 66 6c 61 67 2e  he "force" flag.
1e902 20 20 20 41 6c 77 61 79 73 20 64 6f 0a 2a 2a 20     Always do.** 
1e903 74 68 65 20 70 61 72 73 69 6e 67 20 69 66 20 50  the parsing if P
1e904 32 20 69 73 20 74 72 75 65 2e 20 20 49 66 20 50  2 is true.  If P
1e905 32 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e  2 is false, then
1e906 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
1e907 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 20 69 66 20 74   a.** no-op if t
1e908 68 65 20 73 63 68 65 6d 61 20 69 73 20 6e 6f 74  he schema is not
1e909 20 63 75 72 72 65 6e 74 6c 79 20 6c 6f 61 64 65   currently loade
1e90a 64 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  d.  In other wor
1e90b 64 73 2c 20 69 66 20 50 32 0a 2a 2a 20 69 73 20  ds, if P2.** is 
1e90c 66 61 6c 73 65 2c 20 74 68 65 20 53 51 4c 49 54  false, the SQLIT
1e90d 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 69  E_MASTER table i
1e90e 73 20 6f 6e 6c 79 20 70 61 72 73 65 64 20 69 66  s only parsed if
1e90f 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65   the rest of the
1e910 0a 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 61 6c  .** schema is al
1e911 72 65 61 64 79 20 6c 6f 61 64 65 64 20 69 6e 74  ready loaded int
1e912 6f 20 74 68 65 20 73 79 6d 62 6f 6c 20 74 61 62  o the symbol tab
1e913 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f  le..**.** This o
1e914 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68  pcode invokes th
1e915 65 20 70 61 72 73 65 72 20 74 6f 20 63 72 65 61  e parser to crea
1e916 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c  te a new virtual
1e917 20 6d 61 63 68 69 6e 65 2c 0a 2a 2a 20 74 68 65   machine,.** the
1e918 6e 20 72 75 6e 73 20 74 68 65 20 6e 65 77 20 76  n runs the new v
1e919 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20  irtual machine. 
1e91a 20 49 74 20 69 73 20 74 68 75 73 20 61 20 72 65   It is thus a re
1e91b 2d 65 6e 74 72 61 6e 74 20 6f 70 63 6f 64 65 2e  -entrant opcode.
1e91c 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 72 73  .*/.case OP_Pars
1e91d 65 53 63 68 65 6d 61 3a 20 7b 0a 23 69 66 20 30  eSchema: {.#if 0
1e91e 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1e91f 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e920 75 2e 62 75 20 2a 2f 0a 20 20 69 6e 74 20 69 44  u.bu */.  int iD
1e921 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  b;.  const char 
1e922 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 63 68 61 72  *zMaster;.  char
1e923 20 2a 7a 53 71 6c 3b 0a 20 20 49 6e 69 74 44 61   *zSql;.  InitDa
1e924 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 23 65 6e  ta initData;.#en
1e925 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1e926 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e927 6f 20 75 2e 62 75 20 2a 2f 0a 0a 20 20 75 2e 62  o u.bu */..  u.b
1e928 75 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31 3b  u.iDb = pOp->p1;
1e929 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 75 2e  .  assert( u.bu.
1e92a 69 44 62 3e 3d 30 20 26 26 20 75 2e 62 75 2e 69  iDb>=0 && u.bu.i
1e92b 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20  Db<db->nDb );.. 
1e92c 20 2f 2a 20 49 66 20 70 4f 70 2d 3e 70 32 20 69   /* If pOp->p2 i
1e92d 73 20 30 2c 20 74 68 65 6e 20 74 68 69 73 20 6f  s 0, then this o
1e92e 70 63 6f 64 65 20 69 73 20 62 65 69 6e 67 20 65  pcode is being e
1e92f 78 65 63 75 74 65 64 20 74 6f 20 72 65 61 64 20  xecuted to read 
1e930 61 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 20 72 6f  a.  ** single ro
1e931 77 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 20 74  w, for example t
1e932 68 65 20 72 6f 77 20 63 6f 72 72 65 73 70 6f 6e  he row correspon
1e933 64 69 6e 67 20 74 6f 20 61 20 6e 65 77 20 69 6e  ding to a new in
1e934 64 65 78 0a 20 20 2a 2a 20 63 72 65 61 74 65 64  dex.  ** created
1e935 20 62 79 20 74 68 69 73 20 56 44 42 45 2c 20 66   by this VDBE, f
1e936 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 6d  rom the sqlite_m
1e937 61 73 74 65 72 20 74 61 62 6c 65 2e 20 49 74 20  aster table. It 
1e938 6f 6e 6c 79 0a 20 20 2a 2a 20 64 6f 65 73 20 74  only.  ** does t
1e939 68 69 73 20 69 66 20 74 68 65 20 63 6f 72 72 65  his if the corre
1e93a 73 70 6f 6e 64 69 6e 67 20 69 6e 2d 6d 65 6d 6f  sponding in-memo
1e93b 72 79 20 73 63 68 65 6d 61 20 69 73 20 63 75 72  ry schema is cur
1e93c 72 65 6e 74 6c 79 0a 20 20 2a 2a 20 6c 6f 61 64  rently.  ** load
1e93d 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74  ed. Otherwise, t
1e93e 68 65 20 6e 65 77 20 69 6e 64 65 78 20 64 65 66  he new index def
1e93f 69 6e 69 74 69 6f 6e 20 63 61 6e 20 62 65 20 6c  inition can be l
1e940 6f 61 64 65 64 20 61 6c 6f 6e 67 0a 20 20 2a 2a  oaded along.  **
1e941 20 77 69 74 68 20 74 68 65 20 72 65 73 74 20 6f   with the rest o
1e942 66 20 74 68 65 20 73 63 68 65 6d 61 20 77 68 65  f the schema whe
1e943 6e 20 69 74 20 69 73 20 72 65 71 75 69 72 65 64  n it is required
1e944 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6c 74 68  ..  **.  ** Alth
1e945 6f 75 67 68 20 74 68 65 20 6d 75 74 65 78 20 6f  ough the mutex o
1e946 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6f  n the BtShared o
1e947 62 6a 65 63 74 20 74 68 61 74 20 63 6f 72 72 65  bject that corre
1e948 73 70 6f 6e 64 73 20 74 6f 0a 20 20 2a 2a 20 64  sponds to.  ** d
1e949 61 74 61 62 61 73 65 20 75 2e 62 75 2e 69 44 62  atabase u.bu.iDb
1e94a 20 28 74 68 65 20 64 61 74 61 62 61 73 65 20 63   (the database c
1e94b 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 73 71  ontaining the sq
1e94c 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
1e94d 65 0a 20 20 2a 2a 20 72 65 61 64 20 62 79 20 74  e.  ** read by t
1e94e 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 29  his instruction)
1e94f 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 68 65   is currently he
1e950 6c 64 2c 20 69 74 20 69 73 20 6e 65 63 65 73 73  ld, it is necess
1e951 61 72 79 20 74 6f 0a 20 20 2a 2a 20 6f 62 74 61  ary to.  ** obta
1e952 69 6e 20 74 68 65 20 6d 75 74 65 78 65 73 20 6f  in the mutexes o
1e953 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64  n all attached d
1e954 61 74 61 62 61 73 65 73 20 62 65 66 6f 72 65 20  atabases before 
1e955 63 68 65 63 6b 69 6e 67 20 69 66 0a 20 20 2a 2a  checking if.  **
1e956 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66 20 75   the schema of u
1e957 2e 62 75 2e 69 44 62 20 69 73 20 6c 6f 61 64 65  .bu.iDb is loade
1e958 64 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75  d. This is becau
1e959 73 65 2c 20 61 74 20 74 68 65 20 73 74 61 72 74  se, at the start
1e95a 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 73 71 6c   of.  ** the sql
1e95b 69 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c 6c  ite3_exec() call
1e95c 20 62 65 6c 6f 77 2c 20 53 51 4c 69 74 65 20 77   below, SQLite w
1e95d 69 6c 6c 20 69 6e 76 6f 6b 65 0a 20 20 2a 2a 20  ill invoke.  ** 
1e95e 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1e95f 72 41 6c 6c 28 29 2e 20 49 66 20 61 6c 6c 20 6d  rAll(). If all m
1e960 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61  utexes are not a
1e961 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 74 68 65  lready held, the
1e962 0a 20 20 2a 2a 20 75 2e 62 75 2e 69 44 62 20 6d  .  ** u.bu.iDb m
1e963 75 74 65 78 20 6d 61 79 20 62 65 20 74 65 6d 70  utex may be temp
1e964 6f 72 61 72 69 6c 79 20 72 65 6c 65 61 73 65 64  orarily released
1e965 20 74 6f 20 61 76 6f 69 64 20 64 65 61 64 6c 6f   to avoid deadlo
1e966 63 6b 2e 20 49 66 0a 20 20 2a 2a 20 74 68 69 73  ck. If.  ** this
1e967 20 68 61 70 70 65 6e 73 2c 20 74 68 65 6e 20 73   happens, then s
1e968 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 65 61 64  ome other thread
1e969 20 6d 61 79 20 64 65 6c 65 74 65 20 74 68 65 20   may delete the 
1e96a 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20 73  in-memory.  ** s
1e96b 63 68 65 6d 61 20 6f 66 20 64 61 74 61 62 61 73  chema of databas
1e96c 65 20 75 2e 62 75 2e 69 44 62 20 62 65 66 6f 72  e u.bu.iDb befor
1e96d 65 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  e the SQL statem
1e96e 65 6e 74 20 72 75 6e 73 2e 20 54 68 65 20 73 63  ent runs. The sc
1e96f 68 65 6d 61 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e  hema.  ** will n
1e970 6f 74 20 62 65 20 72 65 6c 6f 61 64 65 64 20 62  ot be reloaded b
1e971 65 63 75 61 73 65 20 74 68 65 20 64 62 2d 3e 69  ecuase the db->i
1e972 6e 69 74 2e 62 75 73 79 20 66 6c 61 67 20 69 73  nit.busy flag is
1e973 20 73 65 74 2e 20 54 68 69 73 0a 20 20 2a 2a 20   set. This.  ** 
1e974 63 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 61 20  can result in a 
1e975 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 3a 20  "no such table: 
1e976 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 20 6f  sqlite_master" o
1e977 72 20 22 6d 61 6c 66 6f 72 6d 65 64 0a 20 20 2a  r "malformed.  *
1e978 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  * database schem
1e979 61 22 20 65 72 72 6f 72 20 62 65 69 6e 67 20 72  a" error being r
1e97a 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 75  eturned to the u
1e97b 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ser..  */.  asse
1e97c 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
1e97d 48 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d 3e 61  HoldsMutex(db->a
1e97e 44 62 5b 75 2e 62 75 2e 69 44 62 5d 2e 70 42 74  Db[u.bu.iDb].pBt
1e97f 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  ) );.  sqlite3Bt
1e980 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b  reeEnterAll(db);
1e981 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 7c  .  if( pOp->p2 |
1e982 7c 20 44 62 48 61 73 50 72 6f 70 65 72 74 79 28  | DbHasProperty(
1e983 64 62 2c 20 75 2e 62 75 2e 69 44 62 2c 20 44 42  db, u.bu.iDb, DB
1e984 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 29  _SchemaLoaded) )
1e985 7b 0a 20 20 20 20 75 2e 62 75 2e 7a 4d 61 73 74  {.    u.bu.zMast
1e986 65 72 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c  er = SCHEMA_TABL
1e987 45 28 75 2e 62 75 2e 69 44 62 29 3b 0a 20 20 20  E(u.bu.iDb);.   
1e988 20 75 2e 62 75 2e 69 6e 69 74 44 61 74 61 2e 64   u.bu.initData.d
1e989 62 20 3d 20 64 62 3b 0a 20 20 20 20 75 2e 62 75  b = db;.    u.bu
1e98a 2e 69 6e 69 74 44 61 74 61 2e 69 44 62 20 3d 20  .initData.iDb = 
1e98b 70 4f 70 2d 3e 70 31 3b 0a 20 20 20 20 75 2e 62  pOp->p1;.    u.b
1e98c 75 2e 69 6e 69 74 44 61 74 61 2e 70 7a 45 72 72  u.initData.pzErr
1e98d 4d 73 67 20 3d 20 26 70 2d 3e 7a 45 72 72 4d 73  Msg = &p->zErrMs
1e98e 67 3b 0a 20 20 20 20 75 2e 62 75 2e 7a 53 71 6c  g;.    u.bu.zSql
1e98f 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
1e990 66 28 64 62 2c 0a 20 20 20 20 20 20 20 22 53 45  f(db,.       "SE
1e991 4c 45 43 54 20 6e 61 6d 65 2c 20 72 6f 6f 74 70  LECT name, rootp
1e992 61 67 65 2c 20 73 71 6c 20 46 52 4f 4d 20 27 25  age, sql FROM '%
1e993 71 27 2e 25 73 20 57 48 45 52 45 20 25 73 22 2c  q'.%s WHERE %s",
1e994 0a 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b  .       db->aDb[
1e995 75 2e 62 75 2e 69 44 62 5d 2e 7a 4e 61 6d 65 2c  u.bu.iDb].zName,
1e996 20 75 2e 62 75 2e 7a 4d 61 73 74 65 72 2c 20 70   u.bu.zMaster, p
1e997 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 69  Op->p4.z);.    i
1e998 66 28 20 75 2e 62 75 2e 7a 53 71 6c 3d 3d 30 20  f( u.bu.zSql==0 
1e999 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
1e99a 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
1e99b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 76 6f  }else{.      (vo
1e99c 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
1e99d 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 61  Off(db);.      a
1e99e 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e  ssert( db->init.
1e99f 62 75 73 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20  busy==0 );.     
1e9a0 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d   db->init.busy =
1e9a1 20 31 3b 0a 20 20 20 20 20 20 75 2e 62 75 2e 69   1;.      u.bu.i
1e9a2 6e 69 74 44 61 74 61 2e 72 63 20 3d 20 53 51 4c  nitData.rc = SQL
1e9a3 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 61 73  ITE_OK;.      as
1e9a4 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f  sert( !db->mallo
1e9a5 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20  cFailed );.     
1e9a6 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78   rc = sqlite3_ex
1e9a7 65 63 28 64 62 2c 20 75 2e 62 75 2e 7a 53 71 6c  ec(db, u.bu.zSql
1e9a8 2c 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c  , sqlite3InitCal
1e9a9 6c 62 61 63 6b 2c 20 26 75 2e 62 75 2e 69 6e 69  lback, &u.bu.ini
1e9aa 74 44 61 74 61 2c 20 30 29 3b 0a 20 20 20 20 20  tData, 0);.     
1e9ab 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1e9ac 4f 4b 20 29 20 72 63 20 3d 20 75 2e 62 75 2e 69  OK ) rc = u.bu.i
1e9ad 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20 20 20  nitData.rc;.    
1e9ae 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1e9af 64 62 2c 20 75 2e 62 75 2e 7a 53 71 6c 29 3b 0a  db, u.bu.zSql);.
1e9b0 20 20 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 62        db->init.b
1e9b1 75 73 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 28  usy = 0;.      (
1e9b2 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
1e9b3 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 7d 0a  tyOn(db);.    }.
1e9b4 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72    }.  sqlite3Btr
1e9b5 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a  eeLeaveAll(db);.
1e9b6 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1e9b7 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 67 6f  _NOMEM ){.    go
1e9b8 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20  to no_mem;.  }. 
1e9b9 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 20 21   break;.}..#if !
1e9ba 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
1e9bb 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 0a 2f 2a 20  MIT_ANALYZE)./* 
1e9bc 4f 70 63 6f 64 65 3a 20 4c 6f 61 64 41 6e 61 6c  Opcode: LoadAnal
1e9bd 79 73 69 73 20 50 31 20 2a 20 2a 20 2a 20 2a 0a  ysis P1 * * * *.
1e9be 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 73  **.** Read the s
1e9bf 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c  qlite_stat1 tabl
1e9c0 65 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 50  e for database P
1e9c1 31 20 61 6e 64 20 6c 6f 61 64 20 74 68 65 20 63  1 and load the c
1e9c2 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 61  ontent.** of tha
1e9c3 74 20 74 61 62 6c 65 20 69 6e 74 6f 20 74 68 65  t table into the
1e9c4 20 69 6e 74 65 72 6e 61 6c 20 69 6e 64 65 78 20   internal index 
1e9c5 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69  hash table.  Thi
1e9c6 73 20 77 69 6c 6c 20 63 61 75 73 65 0a 2a 2a 20  s will cause.** 
1e9c7 74 68 65 20 61 6e 61 6c 79 73 69 73 20 74 6f 20  the analysis to 
1e9c8 62 65 20 75 73 65 64 20 77 68 65 6e 20 70 72 65  be used when pre
1e9c9 70 61 72 69 6e 67 20 61 6c 6c 20 73 75 62 73 65  paring all subse
1e9ca 71 75 65 6e 74 20 71 75 65 72 69 65 73 2e 0a 2a  quent queries..*
1e9cb 2f 0a 63 61 73 65 20 4f 50 5f 4c 6f 61 64 41 6e  /.case OP_LoadAn
1e9cc 61 6c 79 73 69 73 3a 20 7b 0a 20 20 61 73 73 65  alysis: {.  asse
1e9cd 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1e9ce 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44  & pOp->p1<db->nD
1e9cf 62 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  b );.  rc = sqli
1e9d0 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28  te3AnalysisLoad(
1e9d1 64 62 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20  db, pOp->p1);.  
1e9d2 62 72 65 61 6b 3b 20 20 0a 7d 0a 23 65 6e 64 69  break;  .}.#endi
1e9d3 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
1e9d4 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a  LITE_OMIT_ANALYZ
1e9d5 45 29 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65  E) */../* Opcode
1e9d6 3a 20 44 72 6f 70 54 61 62 6c 65 20 50 31 20 2a  : DropTable P1 *
1e9d7 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65   * P4 *.**.** Re
1e9d8 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e 61  move the interna
1e9d9 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61  l (in-memory) da
1e9da 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74 68  ta structures th
1e9db 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20 74  at describe.** t
1e9dc 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 50  he table named P
1e9dd 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31  4 in database P1
1e9de 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65  .  This is calle
1e9df 64 20 61 66 74 65 72 20 61 20 74 61 62 6c 65 0a  d after a table.
1e9e0 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69 6e  ** is dropped in
1e9e1 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20 74   order to keep t
1e9e2 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72  he internal repr
1e9e3 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  esentation of th
1e9e4 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e 73  e.** schema cons
1e9e5 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 61 74  istent with what
1e9e6 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a   is on disk..*/.
1e9e7 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c  case OP_DropTabl
1e9e8 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 55 6e  e: {.  sqlite3Un
1e9e9 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 62  linkAndDeleteTab
1e9ea 6c 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20  le(db, pOp->p1, 
1e9eb 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72  pOp->p4.z);.  br
1e9ec 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1e9ed 65 3a 20 44 72 6f 70 49 6e 64 65 78 20 50 31 20  e: DropIndex P1 
1e9ee 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52  * * P4 *.**.** R
1e9ef 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e  emove the intern
1e9f0 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64  al (in-memory) d
1e9f1 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74  ata structures t
1e9f2 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20  hat describe.** 
1e9f3 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 64 20  the index named 
1e9f4 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 50  P4 in database P
1e9f5 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c  1.  This is call
1e9f6 65 64 20 61 66 74 65 72 20 61 6e 20 69 6e 64 65  ed after an inde
1e9f7 78 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20  x.** is dropped 
1e9f8 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70  in order to keep
1e9f9 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65   the internal re
1e9fa 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
1e9fb 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f  the.** schema co
1e9fc 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68  nsistent with wh
1e9fd 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a  at is on disk..*
1e9fe 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 49 6e  /.case OP_DropIn
1e9ff 64 65 78 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33  dex: {.  sqlite3
1ea00 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49  UnlinkAndDeleteI
1ea01 6e 64 65 78 28 64 62 2c 20 70 4f 70 2d 3e 70 31  ndex(db, pOp->p1
1ea02 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20  , pOp->p4.z);.  
1ea03 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1ea04 6f 64 65 3a 20 44 72 6f 70 54 72 69 67 67 65 72  ode: DropTrigger
1ea05 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a   P1 * * P4 *.**.
1ea06 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 69 6e  ** Remove the in
1ea07 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72  ternal (in-memor
1ea08 79 29 20 64 61 74 61 20 73 74 72 75 63 74 75 72  y) data structur
1ea09 65 73 20 74 68 61 74 20 64 65 73 63 72 69 62 65  es that describe
1ea0a 0a 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72 20  .** the trigger 
1ea0b 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74 61  named P4 in data
1ea0c 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 20 69  base P1.  This i
1ea0d 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61  s called after a
1ea0e 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 73 20 64   trigger.** is d
1ea0f 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 20  ropped in order 
1ea10 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 65  to keep the inte
1ea11 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74  rnal representat
1ea12 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63  ion of the.** sc
1ea13 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 20  hema consistent 
1ea14 77 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e 20  with what is on 
1ea15 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  disk..*/.case OP
1ea16 5f 44 72 6f 70 54 72 69 67 67 65 72 3a 20 7b 0a  _DropTrigger: {.
1ea17 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41    sqlite3UnlinkA
1ea18 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72 28  ndDeleteTrigger(
1ea19 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70  db, pOp->p1, pOp
1ea1a 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b  ->p4.z);.  break
1ea1b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51  ;.}...#ifndef SQ
1ea1c 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52  LITE_OMIT_INTEGR
1ea1d 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 20 4f 70 63  ITY_CHECK./* Opc
1ea1e 6f 64 65 3a 20 49 6e 74 65 67 72 69 74 79 43 6b  ode: IntegrityCk
1ea1f 20 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a 2a   P1 P2 P3 * P5.*
1ea20 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 6e 61 6c 79  *.** Do an analy
1ea21 73 69 73 20 6f 66 20 74 68 65 20 63 75 72 72 65  sis of the curre
1ea22 6e 74 6c 79 20 6f 70 65 6e 20 64 61 74 61 62 61  ntly open databa
1ea23 73 65 2e 20 20 53 74 6f 72 65 20 69 6e 0a 2a 2a  se.  Store in.**
1ea24 20 72 65 67 69 73 74 65 72 20 50 31 20 74 68 65   register P1 the
1ea25 20 74 65 78 74 20 6f 66 20 61 6e 20 65 72 72 6f   text of an erro
1ea26 72 20 6d 65 73 73 61 67 65 20 64 65 73 63 72 69  r message descri
1ea27 62 69 6e 67 20 61 6e 79 20 70 72 6f 62 6c 65 6d  bing any problem
1ea28 73 2e 0a 2a 2a 20 49 66 20 6e 6f 20 70 72 6f 62  s..** If no prob
1ea29 6c 65 6d 73 20 61 72 65 20 66 6f 75 6e 64 2c 20  lems are found, 
1ea2a 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e 20  store a NULL in 
1ea2b 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 0a  register P1..**.
1ea2c 2a 2a 20 54 68 65 20 72 65 67 69 73 74 65 72 20  ** The register 
1ea2d 50 33 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  P3 contains the 
1ea2e 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
1ea2f 66 20 61 6c 6c 6f 77 65 64 20 65 72 72 6f 72 73  f allowed errors
1ea30 2e 0a 2a 2a 20 41 74 20 6d 6f 73 74 20 72 65 67  ..** At most reg
1ea31 28 50 33 29 20 65 72 72 6f 72 73 20 77 69 6c 6c  (P3) errors will
1ea32 20 62 65 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a   be reported..**
1ea33 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
1ea34 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 73 74   the analysis st
1ea35 6f 70 73 20 61 73 20 73 6f 6f 6e 20 61 73 20 72  ops as soon as r
1ea36 65 67 28 50 31 29 20 65 72 72 6f 72 73 20 61 72  eg(P1) errors ar
1ea37 65 20 0a 2a 2a 20 73 65 65 6e 2e 20 20 52 65 67  e .** seen.  Reg
1ea38 28 50 31 29 20 69 73 20 75 70 64 61 74 65 64 20  (P1) is updated 
1ea39 77 69 74 68 20 74 68 65 20 6e 75 6d 62 65 72 20  with the number 
1ea3a 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61 69 6e  of errors remain
1ea3b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  ing..**.** The r
1ea3c 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 73  oot page numbers
1ea3d 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 69   of all tables i
1ea3e 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  n the database a
1ea3f 72 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 73 74  re integer.** st
1ea40 6f 72 65 64 20 69 6e 20 72 65 67 28 50 31 29 2c  ored in reg(P1),
1ea41 20 72 65 67 28 50 31 2b 31 29 2c 20 72 65 67 28   reg(P1+1), reg(
1ea42 50 31 2b 32 29 2c 20 2e 2e 2e 2e 20 20 54 68 65  P1+2), ....  The
1ea43 72 65 20 61 72 65 20 50 32 20 74 61 62 6c 65 73  re are P2 tables
1ea44 0a 2a 2a 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a 2a  .** total..**.**
1ea45 20 49 66 20 50 35 20 69 73 20 6e 6f 74 20 7a 65   If P5 is not ze
1ea46 72 6f 2c 20 74 68 65 20 63 68 65 63 6b 20 69 73  ro, the check is
1ea47 20 64 6f 6e 65 20 6f 6e 20 74 68 65 20 61 75 78   done on the aux
1ea48 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 0a  iliary database.
1ea49 2a 2a 20 66 69 6c 65 2c 20 6e 6f 74 20 74 68 65  ** file, not the
1ea4a 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
1ea4b 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ile..**.** This 
1ea4c 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64 20 74  opcode is used t
1ea4d 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
1ea4e 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 20  integrity_check 
1ea4f 70 72 61 67 6d 61 2e 0a 2a 2f 0a 63 61 73 65 20  pragma..*/.case 
1ea50 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 3a 20  OP_IntegrityCk: 
1ea51 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
1ea52 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1ea53 64 20 69 6e 74 6f 20 75 2e 62 76 20 2a 2f 0a 20  d into u.bv */. 
1ea54 20 69 6e 74 20 6e 52 6f 6f 74 3b 20 20 20 20 20   int nRoot;     
1ea55 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 61   /* Number of ta
1ea56 62 6c 65 73 20 74 6f 20 63 68 65 63 6b 2e 20 20  bles to check.  
1ea57 28 4e 75 6d 62 65 72 20 6f 66 20 72 6f 6f 74 20  (Number of root 
1ea58 70 61 67 65 73 2e 29 20 2a 2f 0a 20 20 69 6e 74  pages.) */.  int
1ea59 20 2a 61 52 6f 6f 74 3b 20 20 20 20 20 2f 2a 20   *aRoot;     /* 
1ea5a 41 72 72 61 79 20 6f 66 20 72 6f 6f 74 70 61 67  Array of rootpag
1ea5b 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 74 61  e numbers for ta
1ea5c 62 6c 65 73 20 74 6f 20 62 65 20 63 68 65 63 6b  bles to be check
1ea5d 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20  ed */.  int j;  
1ea5e 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
1ea5f 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  counter */.  int
1ea60 20 6e 45 72 72 3b 20 20 20 20 20 20 20 2f 2a 20   nErr;       /* 
1ea61 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  Number of errors
1ea62 20 72 65 70 6f 72 74 65 64 20 2a 2f 0a 20 20 63   reported */.  c
1ea63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 2f  har *z;        /
1ea64 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 65 72  * Text of the er
1ea65 72 6f 72 20 72 65 70 6f 72 74 20 2a 2f 0a 20 20  ror report */.  
1ea66 4d 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20 20 20  Mem *pnErr;     
1ea67 2f 2a 20 52 65 67 69 73 74 65 72 20 6b 65 65 70  /* Register keep
1ea68 69 6e 67 20 74 72 61 63 6b 20 6f 66 20 65 72 72  ing track of err
1ea69 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 20 2a 2f  ors remaining */
1ea6a 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1ea6b 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1ea6c 20 69 6e 74 6f 20 75 2e 62 76 20 2a 2f 0a 0a 20   into u.bv */.. 
1ea6d 20 75 2e 62 76 2e 6e 52 6f 6f 74 20 3d 20 70 4f   u.bv.nRoot = pO
1ea6e 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 28  p->p2;.  assert(
1ea6f 20 75 2e 62 76 2e 6e 52 6f 6f 74 3e 30 20 29 3b   u.bv.nRoot>0 );
1ea70 0a 20 20 75 2e 62 76 2e 61 52 6f 6f 74 20 3d 20  .  u.bv.aRoot = 
1ea71 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
1ea72 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e  aw(db, sizeof(in
1ea73 74 29 2a 28 75 2e 62 76 2e 6e 52 6f 6f 74 2b 31  t)*(u.bv.nRoot+1
1ea74 29 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 76 2e  ) );.  if( u.bv.
1ea75 61 52 6f 6f 74 3d 3d 30 20 29 20 67 6f 74 6f 20  aRoot==0 ) goto 
1ea76 6e 6f 5f 6d 65 6d 3b 0a 20 20 61 73 73 65 72 74  no_mem;.  assert
1ea77 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70  ( pOp->p3>0 && p
1ea78 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p3<=p->nMem 
1ea79 29 3b 0a 20 20 75 2e 62 76 2e 70 6e 45 72 72 20  );.  u.bv.pnErr 
1ea7a 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
1ea7b 70 33 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28  p3];.  assert( (
1ea7c 75 2e 62 76 2e 70 6e 45 72 72 2d 3e 66 6c 61 67  u.bv.pnErr->flag
1ea7d 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20  s & MEM_Int)!=0 
1ea7e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 75 2e  );.  assert( (u.
1ea7f 62 76 2e 70 6e 45 72 72 2d 3e 66 6c 61 67 73 20  bv.pnErr->flags 
1ea80 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42  & (MEM_Str|MEM_B
1ea81 6c 6f 62 29 29 3d 3d 30 20 29 3b 0a 20 20 70 49  lob))==0 );.  pI
1ea82 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  n1 = &p->aMem[pO
1ea83 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 75 2e  p->p1];.  for(u.
1ea84 62 76 2e 6a 3d 30 3b 20 75 2e 62 76 2e 6a 3c 75  bv.j=0; u.bv.j<u
1ea85 2e 62 76 2e 6e 52 6f 6f 74 3b 20 75 2e 62 76 2e  .bv.nRoot; u.bv.
1ea86 6a 2b 2b 29 7b 0a 20 20 20 20 75 2e 62 76 2e 61  j++){.    u.bv.a
1ea87 52 6f 6f 74 5b 75 2e 62 76 2e 6a 5d 20 3d 20 28  Root[u.bv.j] = (
1ea88 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62 65 49  int)sqlite3VdbeI
1ea89 6e 74 56 61 6c 75 65 28 26 70 49 6e 31 5b 75 2e  ntValue(&pIn1[u.
1ea8a 62 76 2e 6a 5d 29 3b 0a 20 20 7d 0a 20 20 75 2e  bv.j]);.  }.  u.
1ea8b 62 76 2e 61 52 6f 6f 74 5b 75 2e 62 76 2e 6a 5d  bv.aRoot[u.bv.j]
1ea8c 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
1ea8d 70 4f 70 2d 3e 70 35 3c 64 62 2d 3e 6e 44 62 20  pOp->p5<db->nDb 
1ea8e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d  );.  assert( (p-
1ea8f 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c  >btreeMask & (1<
1ea90 3c 70 4f 70 2d 3e 70 35 29 29 21 3d 30 20 29 3b  <pOp->p5))!=0 );
1ea91 0a 20 20 75 2e 62 76 2e 7a 20 3d 20 73 71 6c 69  .  u.bv.z = sqli
1ea92 74 65 33 42 74 72 65 65 49 6e 74 65 67 72 69 74  te3BtreeIntegrit
1ea93 79 43 68 65 63 6b 28 64 62 2d 3e 61 44 62 5b 70  yCheck(db->aDb[p
1ea94 4f 70 2d 3e 70 35 5d 2e 70 42 74 2c 20 75 2e 62  Op->p5].pBt, u.b
1ea95 76 2e 61 52 6f 6f 74 2c 20 75 2e 62 76 2e 6e 52  v.aRoot, u.bv.nR
1ea96 6f 6f 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  oot,.           
1ea97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ea98 20 20 20 20 20 20 28 69 6e 74 29 75 2e 62 76 2e        (int)u.bv.
1ea99 70 6e 45 72 72 2d 3e 75 2e 69 2c 20 26 75 2e 62  pnErr->u.i, &u.b
1ea9a 76 2e 6e 45 72 72 29 3b 0a 20 20 73 71 6c 69 74  v.nErr);.  sqlit
1ea9b 65 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e 62  e3DbFree(db, u.b
1ea9c 76 2e 61 52 6f 6f 74 29 3b 0a 20 20 75 2e 62 76  v.aRoot);.  u.bv
1ea9d 2e 70 6e 45 72 72 2d 3e 75 2e 69 20 2d 3d 20 75  .pnErr->u.i -= u
1ea9e 2e 62 76 2e 6e 45 72 72 3b 0a 20 20 73 71 6c 69  .bv.nErr;.  sqli
1ea9f 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c  te3VdbeMemSetNul
1eaa0 6c 28 70 49 6e 31 29 3b 0a 20 20 69 66 28 20 75  l(pIn1);.  if( u
1eaa1 2e 62 76 2e 6e 45 72 72 3d 3d 30 20 29 7b 0a 20  .bv.nErr==0 ){. 
1eaa2 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 76 2e     assert( u.bv.
1eaa3 7a 3d 3d 30 20 29 3b 0a 20 20 7d 65 6c 73 65 20  z==0 );.  }else 
1eaa4 69 66 28 20 75 2e 62 76 2e 7a 3d 3d 30 20 29 7b  if( u.bv.z==0 ){
1eaa5 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  .    goto no_mem
1eaa6 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
1eaa7 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
1eaa8 53 74 72 28 70 49 6e 31 2c 20 75 2e 62 76 2e 7a  Str(pIn1, u.bv.z
1eaa9 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54 46  , -1, SQLITE_UTF
1eaaa 38 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29  8, sqlite3_free)
1eaab 3b 0a 20 20 7d 0a 20 20 55 50 44 41 54 45 5f 4d  ;.  }.  UPDATE_M
1eaac 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 49 6e 31  AX_BLOBSIZE(pIn1
1eaad 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1eaae 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70  ChangeEncoding(p
1eaaf 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a  In1, encoding);.
1eab0 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
1eab1 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
1eab2 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b  _INTEGRITY_CHECK
1eab3 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20   */../* Opcode: 
1eab4 52 6f 77 53 65 74 41 64 64 20 50 31 20 50 32 20  RowSetAdd P1 P2 
1eab5 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 73 65  * * *.**.** Inse
1eab6 72 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 76  rt the integer v
1eab7 61 6c 75 65 20 68 65 6c 64 20 62 79 20 72 65 67  alue held by reg
1eab8 69 73 74 65 72 20 50 32 20 69 6e 74 6f 20 61 20  ister P2 into a 
1eab9 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 0a 2a 2a  boolean index.**
1eaba 20 68 65 6c 64 20 69 6e 20 72 65 67 69 73 74 65   held in registe
1eabb 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61  r P1..**.** An a
1eabc 73 73 65 72 74 69 6f 6e 20 66 61 69 6c 73 20 69  ssertion fails i
1eabd 66 20 50 32 20 69 73 20 6e 6f 74 20 61 6e 20 69  f P2 is not an i
1eabe 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63 61 73 65 20  nteger..*/.case 
1eabf 4f 50 5f 52 6f 77 53 65 74 41 64 64 3a 20 7b 20  OP_RowSetAdd: { 
1eac0 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a        /* in2 */.
1eac1 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1eac2 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1eac3 69 6e 74 6f 20 75 2e 62 77 20 2a 2f 0a 20 20 4d  into u.bw */.  M
1eac4 65 6d 20 2a 70 49 64 78 3b 0a 20 20 4d 65 6d 20  em *pIdx;.  Mem 
1eac5 2a 70 56 61 6c 3b 0a 23 65 6e 64 69 66 20 2f 2a  *pVal;.#endif /*
1eac6 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1eac7 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 77   moved into u.bw
1eac8 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f   */.  assert( pO
1eac9 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e  p->p1>0 && pOp->
1eaca 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  p1<=p->nMem );. 
1eacb 20 75 2e 62 77 2e 70 49 64 78 20 3d 20 26 70 2d   u.bw.pIdx = &p-
1eacc 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  >aMem[pOp->p1];.
1eacd 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1eace 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3c 3d  2>0 && pOp->p2<=
1eacf 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 62  p->nMem );.  u.b
1ead0 77 2e 70 56 61 6c 20 3d 20 26 70 2d 3e 61 4d 65  w.pVal = &p->aMe
1ead1 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73  m[pOp->p2];.  as
1ead2 73 65 72 74 28 20 28 75 2e 62 77 2e 70 56 61 6c  sert( (u.bw.pVal
1ead3 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
1ead4 74 29 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28  t)!=0 );.  if( (
1ead5 75 2e 62 77 2e 70 49 64 78 2d 3e 66 6c 61 67 73  u.bw.pIdx->flags
1ead6 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d   & MEM_RowSet)==
1ead7 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
1ead8 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 65 74  VdbeMemSetRowSet
1ead9 28 75 2e 62 77 2e 70 49 64 78 29 3b 0a 20 20 20  (u.bw.pIdx);.   
1eada 20 69 66 28 20 28 75 2e 62 77 2e 70 49 64 78 2d   if( (u.bw.pIdx-
1eadb 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
1eadc 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f 20 6e  Set)==0 ) goto n
1eadd 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c  o_mem;.  }.  sql
1eade 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74  ite3RowSetInsert
1eadf 28 75 2e 62 77 2e 70 49 64 78 2d 3e 75 2e 70 52  (u.bw.pIdx->u.pR
1eae0 6f 77 53 65 74 2c 20 75 2e 62 77 2e 70 56 61 6c  owSet, u.bw.pVal
1eae1 2d 3e 75 2e 69 29 3b 0a 20 20 62 72 65 61 6b 3b  ->u.i);.  break;
1eae2 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52  .}../* Opcode: R
1eae3 6f 77 53 65 74 52 65 61 64 20 50 31 20 50 32 20  owSetRead P1 P2 
1eae4 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 74  P3 * *.**.** Ext
1eae5 72 61 63 74 20 74 68 65 20 73 6d 61 6c 6c 65 73  ract the smalles
1eae6 74 20 76 61 6c 75 65 20 66 72 6f 6d 20 62 6f 6f  t value from boo
1eae7 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 20 61 6e  lean index P1 an
1eae8 64 20 70 75 74 20 74 68 61 74 20 76 61 6c 75 65  d put that value
1eae9 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65   into.** registe
1eaea 72 20 50 33 2e 20 20 4f 72 2c 20 69 66 20 62 6f  r P3.  Or, if bo
1eaeb 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 20 69  olean index P1 i
1eaec 73 20 69 6e 69 74 69 61 6c 6c 79 20 65 6d 70 74  s initially empt
1eaed 79 2c 20 6c 65 61 76 65 20 50 33 0a 2a 2a 20 75  y, leave P3.** u
1eaee 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 6a 75 6d  nchanged and jum
1eaef 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e  p to instruction
1eaf0 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f   P2..*/.case OP_
1eaf1 52 6f 77 53 65 74 52 65 61 64 3a 20 7b 20 20 20  RowSetRead: {   
1eaf2 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 6f 75 74      /* jump, out
1eaf3 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  3 */.#if 0  /* l
1eaf4 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1eaf5 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 78 20 2a  oved into u.bx *
1eaf6 2f 0a 20 20 4d 65 6d 20 2a 70 49 64 78 3b 0a 20  /.  Mem *pIdx;. 
1eaf7 20 69 36 34 20 76 61 6c 3b 0a 23 65 6e 64 69 66   i64 val;.#endif
1eaf8 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1eaf9 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1eafa 2e 62 78 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  .bx */.  assert(
1eafb 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f   pOp->p1>0 && pO
1eafc 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p1<=p->nMem )
1eafd 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e  ;.  CHECK_FOR_IN
1eafe 54 45 52 52 55 50 54 3b 0a 20 20 75 2e 62 78 2e  TERRUPT;.  u.bx.
1eaff 70 49 64 78 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pIdx = &p->aMem[
1eb00 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 4f 75 74  pOp->p1];.  pOut
1eb01 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1eb02 3e 70 33 5d 3b 0a 20 20 69 66 28 20 28 75 2e 62  >p3];.  if( (u.b
1eb03 78 2e 70 49 64 78 2d 3e 66 6c 61 67 73 20 26 20  x.pIdx->flags & 
1eb04 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 0a 20  MEM_RowSet)==0. 
1eb05 20 20 7c 7c 20 73 71 6c 69 74 65 33 52 6f 77 53    || sqlite3RowS
1eb06 65 74 4e 65 78 74 28 75 2e 62 78 2e 70 49 64 78  etNext(u.bx.pIdx
1eb07 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 26 75 2e  ->u.pRowSet, &u.
1eb08 62 78 2e 76 61 6c 29 3d 3d 30 0a 20 20 29 7b 0a  bx.val)==0.  ){.
1eb09 20 20 20 20 2f 2a 20 54 68 65 20 62 6f 6f 6c 65      /* The boole
1eb0a 61 6e 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74  an index is empt
1eb0b 79 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  y */.    sqlite3
1eb0c 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 75  VdbeMemSetNull(u
1eb0d 2e 62 78 2e 70 49 64 78 29 3b 0a 20 20 20 20 70  .bx.pIdx);.    p
1eb0e 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1eb0f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
1eb10 20 41 20 76 61 6c 75 65 20 77 61 73 20 70 75 6c   A value was pul
1eb11 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 69 6e 64  led from the ind
1eb12 65 78 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  ex */.    assert
1eb13 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70  ( pOp->p3>0 && p
1eb14 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p3<=p->nMem 
1eb15 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1eb16 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70 4f  beMemSetInt64(pO
1eb17 75 74 2c 20 75 2e 62 78 2e 76 61 6c 29 3b 0a 20  ut, u.bx.val);. 
1eb18 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1eb19 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65 74  * Opcode: RowSet
1eb1a 54 65 73 74 20 50 31 20 50 32 20 50 33 20 50 34  Test P1 P2 P3 P4
1eb1b 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  .**.** Register 
1eb1c 50 33 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f  P3 is assumed to
1eb1d 20 68 6f 6c 64 20 61 20 36 34 2d 62 69 74 20 69   hold a 64-bit i
1eb1e 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 20 49 66  nteger value. If
1eb1f 20 72 65 67 69 73 74 65 72 20 50 31 0a 2a 2a 20   register P1.** 
1eb20 63 6f 6e 74 61 69 6e 73 20 61 20 52 6f 77 53 65  contains a RowSe
1eb21 74 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 61  t object and tha
1eb22 74 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20  t RowSet object 
1eb23 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20  contains.** the 
1eb24 76 61 6c 75 65 20 68 65 6c 64 20 69 6e 20 50 33  value held in P3
1eb25 2c 20 6a 75 6d 70 20 74 6f 20 72 65 67 69 73 74  , jump to regist
1eb26 65 72 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65  er P2. Otherwise
1eb27 2c 20 69 6e 73 65 72 74 20 74 68 65 0a 2a 2a 20  , insert the.** 
1eb28 69 6e 74 65 67 65 72 20 69 6e 20 50 33 20 69 6e  integer in P3 in
1eb29 74 6f 20 74 68 65 20 52 6f 77 53 65 74 20 61 6e  to the RowSet an
1eb2a 64 20 63 6f 6e 74 69 6e 75 65 20 6f 6e 20 74 6f  d continue on to
1eb2b 20 74 68 65 0a 2a 2a 20 6e 65 78 74 20 6f 70 63   the.** next opc
1eb2c 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 52  ode..**.** The R
1eb2d 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 69 73 20  owSet object is 
1eb2e 6f 70 74 69 6d 69 7a 65 64 20 66 6f 72 20 74 68  optimized for th
1eb2f 65 20 63 61 73 65 20 77 68 65 72 65 20 73 75 63  e case where suc
1eb30 63 65 73 73 69 76 65 20 73 65 74 73 0a 2a 2a 20  cessive sets.** 
1eb31 6f 66 20 69 6e 74 65 67 65 72 73 2c 20 77 68 65  of integers, whe
1eb32 72 65 20 65 61 63 68 20 73 65 74 20 63 6f 6e 74  re each set cont
1eb33 61 69 6e 73 20 6e 6f 20 64 75 70 6c 69 63 61 74  ains no duplicat
1eb34 65 73 2e 20 45 61 63 68 20 73 65 74 0a 2a 2a 20  es. Each set.** 
1eb35 6f 66 20 76 61 6c 75 65 73 20 69 73 20 69 64 65  of values is ide
1eb36 6e 74 69 66 69 65 64 20 62 79 20 61 20 75 6e 69  ntified by a uni
1eb37 71 75 65 20 50 34 20 76 61 6c 75 65 2e 20 54 68  que P4 value. Th
1eb38 65 20 66 69 72 73 74 20 73 65 74 0a 2a 2a 20 6d  e first set.** m
1eb39 75 73 74 20 68 61 76 65 20 50 34 3d 3d 30 2c 20  ust have P4==0, 
1eb3a 74 68 65 20 66 69 6e 61 6c 20 73 65 74 20 50 34  the final set P4
1eb3b 3d 2d 31 2e 20 20 50 34 20 6d 75 73 74 20 62 65  =-1.  P4 must be
1eb3c 20 65 69 74 68 65 72 20 2d 31 20 6f 72 0a 2a 2a   either -1 or.**
1eb3d 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 20   non-negative.  
1eb3e 46 6f 72 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65  For non-negative
1eb3f 20 76 61 6c 75 65 73 20 6f 66 20 50 34 20 6f 6e   values of P4 on
1eb40 6c 79 20 74 68 65 20 6c 6f 77 65 72 20 34 0a 2a  ly the lower 4.*
1eb41 2a 20 62 69 74 73 20 61 72 65 20 73 69 67 6e 69  * bits are signi
1eb42 66 69 63 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  ficant..**.** Th
1eb43 69 73 20 61 6c 6c 6f 77 73 20 6f 70 74 69 6d 69  is allows optimi
1eb44 7a 61 74 69 6f 6e 73 3a 20 28 61 29 20 77 68 65  zations: (a) whe
1eb45 6e 20 50 34 3d 3d 30 20 74 68 65 72 65 20 69 73  n P4==0 there is
1eb46 20 6e 6f 20 6e 65 65 64 20 74 6f 20 74 65 73 74   no need to test
1eb47 0a 2a 2a 20 74 68 65 20 72 6f 77 73 65 74 20 6f  .** the rowset o
1eb48 62 6a 65 63 74 20 66 6f 72 20 50 33 2c 20 61 73  bject for P3, as
1eb49 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65   it is guarantee
1eb4a 64 20 6e 6f 74 20 74 6f 20 63 6f 6e 74 61 69 6e  d not to contain
1eb4b 20 69 74 2c 0a 2a 2a 20 28 62 29 20 77 68 65 6e   it,.** (b) when
1eb4c 20 50 34 3d 3d 2d 31 20 74 68 65 72 65 20 69 73   P4==-1 there is
1eb4d 20 6e 6f 20 6e 65 65 64 20 74 6f 20 69 6e 73 65   no need to inse
1eb4e 72 74 20 74 68 65 20 76 61 6c 75 65 2c 20 61 73  rt the value, as
1eb4f 20 69 74 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65   it will.** neve
1eb50 72 20 62 65 20 74 65 73 74 65 64 20 66 6f 72 2c  r be tested for,
1eb51 20 61 6e 64 20 28 63 29 20 77 68 65 6e 20 61 20   and (c) when a 
1eb52 76 61 6c 75 65 20 74 68 61 74 20 69 73 20 70 61  value that is pa
1eb53 72 74 20 6f 66 20 73 65 74 20 58 20 69 73 0a 2a  rt of set X is.*
1eb54 2a 20 69 6e 73 65 72 74 65 64 2c 20 74 68 65 72  * inserted, ther
1eb55 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20  e is no need to 
1eb56 73 65 61 72 63 68 20 74 6f 20 73 65 65 20 69 66  search to see if
1eb57 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20   the same value 
1eb58 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c  was.** previousl
1eb59 79 20 69 6e 73 65 72 74 65 64 20 61 73 20 70 61  y inserted as pa
1eb5a 72 74 20 6f 66 20 73 65 74 20 58 20 28 6f 6e 6c  rt of set X (onl
1eb5b 79 20 69 66 20 69 74 20 77 61 73 20 70 72 65 76  y if it was prev
1eb5c 69 6f 75 73 6c 79 0a 2a 2a 20 69 6e 73 65 72 74  iously.** insert
1eb5d 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 73 6f  ed as part of so
1eb5e 6d 65 20 6f 74 68 65 72 20 73 65 74 29 2e 0a 2a  me other set)..*
1eb5f 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 65 74  /.case OP_RowSet
1eb60 54 65 73 74 3a 20 7b 20 20 20 20 20 20 20 20 20  Test: {         
1eb61 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a              /* j
1eb62 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f  ump, in1, in3 */
1eb63 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1eb64 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1eb65 20 69 6e 74 6f 20 75 2e 62 79 20 2a 2f 0a 20 20   into u.by */.  
1eb66 69 6e 74 20 69 53 65 74 3b 0a 20 20 69 6e 74 20  int iSet;.  int 
1eb67 65 78 69 73 74 73 3b 0a 23 65 6e 64 69 66 20 2f  exists;.#endif /
1eb68 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1eb69 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1eb6a 79 20 2a 2f 0a 0a 20 20 75 2e 62 79 2e 69 53 65  y */..  u.by.iSe
1eb6b 74 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20  t = pOp->p4.i;. 
1eb6c 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e 66   assert( pIn3->f
1eb6d 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a  lags&MEM_Int );.
1eb6e 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69  .  /* If there i
1eb6f 73 20 61 6e 79 74 68 69 6e 67 20 6f 74 68 65 72  s anything other
1eb70 20 74 68 61 6e 20 61 20 72 6f 77 73 65 74 20 6f   than a rowset o
1eb71 62 6a 65 63 74 20 69 6e 20 6d 65 6d 6f 72 79 20  bject in memory 
1eb72 63 65 6c 6c 20 50 31 2c 0a 20 20 2a 2a 20 64 65  cell P1,.  ** de
1eb73 6c 65 74 65 20 69 74 20 6e 6f 77 20 61 6e 64 20  lete it now and 
1eb74 69 6e 69 74 69 61 6c 69 7a 65 20 50 31 20 77 69  initialize P1 wi
1eb75 74 68 20 61 6e 20 65 6d 70 74 79 20 72 6f 77 73  th an empty rows
1eb76 65 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70  et.  */.  if( (p
1eb77 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
1eb78 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b 0a 20  _RowSet)==0 ){. 
1eb79 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1eb7a 6d 53 65 74 52 6f 77 53 65 74 28 70 49 6e 31 29  mSetRowSet(pIn1)
1eb7b 3b 0a 20 20 20 20 69 66 28 20 28 70 49 6e 31 2d  ;.    if( (pIn1-
1eb7c 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77  >flags & MEM_Row
1eb7d 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f 20 6e  Set)==0 ) goto n
1eb7e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 61 73  o_mem;.  }..  as
1eb7f 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70  sert( pOp->p4typ
1eb80 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20  e==P4_INT32 );. 
1eb81 20 61 73 73 65 72 74 28 20 75 2e 62 79 2e 69 53   assert( u.by.iS
1eb82 65 74 3d 3d 2d 31 20 7c 7c 20 75 2e 62 79 2e 69  et==-1 || u.by.i
1eb83 53 65 74 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20  Set>=0 );.  if( 
1eb84 75 2e 62 79 2e 69 53 65 74 20 29 7b 0a 20 20 20  u.by.iSet ){.   
1eb85 20 75 2e 62 79 2e 65 78 69 73 74 73 20 3d 20 73   u.by.exists = s
1eb86 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73 74  qlite3RowSetTest
1eb87 28 70 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 74  (pIn1->u.pRowSet
1eb88 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1eb89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb8a 20 28 75 38 29 28 75 2e 62 79 2e 69 53 65 74 3e   (u8)(u.by.iSet>
1eb8b 3d 30 20 3f 20 75 2e 62 79 2e 69 53 65 74 20 26  =0 ? u.by.iSet &
1eb8c 20 30 78 66 20 3a 20 30 78 66 66 29 2c 0a 20 20   0xf : 0xff),.  
1eb8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb8e 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 6e               pIn
1eb8f 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 69 66 28  3->u.i);.    if(
1eb90 20 75 2e 62 79 2e 65 78 69 73 74 73 20 29 7b 0a   u.by.exists ){.
1eb91 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e        pc = pOp->
1eb92 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 62 72  p2 - 1;.      br
1eb93 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
1eb94 20 69 66 28 20 75 2e 62 79 2e 69 53 65 74 3e 3d   if( u.by.iSet>=
1eb95 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
1eb96 52 6f 77 53 65 74 49 6e 73 65 72 74 28 70 49 6e  RowSetInsert(pIn
1eb97 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 70 49  1->u.pRowSet, pI
1eb98 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 7d 0a 20 20  n3->u.i);.  }.  
1eb99 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64  break;.}...#ifnd
1eb9a 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
1eb9b 52 49 47 47 45 52 0a 0a 2f 2a 20 4f 70 63 6f 64  RIGGER../* Opcod
1eb9c 65 3a 20 50 72 6f 67 72 61 6d 20 50 31 20 50 32  e: Program P1 P2
1eb9d 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 45   P3 P4 *.**.** E
1eb9e 78 65 63 75 74 65 20 74 68 65 20 74 72 69 67 67  xecute the trigg
1eb9f 65 72 20 70 72 6f 67 72 61 6d 20 70 61 73 73 65  er program passe
1eba0 64 20 61 73 20 50 34 20 28 74 79 70 65 20 50 34  d as P4 (type P4
1eba1 5f 53 55 42 50 52 4f 47 52 41 4d 29 2e 20 0a 2a  _SUBPROGRAM). .*
1eba2 2a 0a 2a 2a 20 50 31 20 63 6f 6e 74 61 69 6e 73  *.** P1 contains
1eba3 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20   the address of 
1eba4 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  the memory cell 
1eba5 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
1eba6 65 20 66 69 72 73 74 20 6d 65 6d 6f 72 79 20 0a  e first memory .
1eba7 2a 2a 20 63 65 6c 6c 20 69 6e 20 61 6e 20 61 72  ** cell in an ar
1eba8 72 61 79 20 6f 66 20 76 61 6c 75 65 73 20 75 73  ray of values us
1eba9 65 64 20 61 73 20 61 72 67 75 6d 65 6e 74 73 20  ed as arguments 
1ebaa 74 6f 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72  to the sub-progr
1ebab 61 6d 2e 20 50 32 20 0a 2a 2a 20 63 6f 6e 74 61  am. P2 .** conta
1ebac 69 6e 73 20 74 68 65 20 61 64 64 72 65 73 73 20  ins the address 
1ebad 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66 20 74 68  to jump to if th
1ebae 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 74 68  e sub-program th
1ebaf 72 6f 77 73 20 61 6e 20 49 47 4e 4f 52 45 20 0a  rows an IGNORE .
1ebb0 2a 2a 20 65 78 63 65 70 74 69 6f 6e 20 75 73 69  ** exception usi
1ebb1 6e 67 20 74 68 65 20 52 41 49 53 45 28 29 20 66  ng the RAISE() f
1ebb2 75 6e 63 74 69 6f 6e 2e 20 52 65 67 69 73 74 65  unction. Registe
1ebb3 72 20 50 33 20 63 6f 6e 74 61 69 6e 73 20 74 68  r P3 contains th
1ebb4 65 20 61 64 64 72 65 73 73 20 0a 2a 2a 20 6f 66  e address .** of
1ebb5 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69   a memory cell i
1ebb6 6e 20 74 68 69 73 20 28 74 68 65 20 70 61 72 65  n this (the pare
1ebb7 6e 74 29 20 56 4d 20 74 68 61 74 20 69 73 20 75  nt) VM that is u
1ebb8 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  sed to allocate 
1ebb9 74 68 65 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 72  the .** memory r
1ebba 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 73  equired by the s
1ebbb 75 62 2d 76 64 62 65 20 61 74 20 72 75 6e 74 69  ub-vdbe at runti
1ebbc 6d 65 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20  me..**.** P4 is 
1ebbd 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
1ebbe 20 56 4d 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74   VM containing t
1ebbf 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  he trigger progr
1ebc0 61 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50  am..*/.case OP_P
1ebc1 72 6f 67 72 61 6d 3a 20 7b 20 20 20 20 20 20 20  rogram: {       
1ebc2 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20   /* jump */.#if 
1ebc3 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1ebc4 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1ebc5 20 75 2e 62 7a 20 2a 2f 0a 20 20 69 6e 74 20 6e   u.bz */.  int n
1ebc6 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Mem;            
1ebc7 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1ebc8 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73  memory registers
1ebc9 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72 61 6d   for sub-program
1ebca 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b   */.  int nByte;
1ebcb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ebcc 20 42 79 74 65 73 20 6f 66 20 72 75 6e 74 69 6d   Bytes of runtim
1ebcd 65 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64  e space required
1ebce 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72 61 6d   for sub-program
1ebcf 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 74 3b 20   */.  Mem *pRt; 
1ebd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ebd1 20 52 65 67 69 73 74 65 72 20 74 6f 20 61 6c 6c   Register to all
1ebd2 6f 63 61 74 65 20 72 75 6e 74 69 6d 65 20 73 70  ocate runtime sp
1ebd3 61 63 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d  ace */.  Mem *pM
1ebd4 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  em;             
1ebd5 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65 72   /* Used to iter
1ebd6 61 74 65 20 74 68 72 6f 75 67 68 20 6d 65 6d 6f  ate through memo
1ebd7 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 4d 65  ry cells */.  Me
1ebd8 6d 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20  m *pEnd;        
1ebd9 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 6d 65        /* Last me
1ebda 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 6e 65 77  mory cell in new
1ebdb 20 61 72 72 61 79 20 2a 2f 0a 20 20 56 64 62 65   array */.  Vdbe
1ebdc 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20  Frame *pFrame;  
1ebdd 20 20 20 20 2f 2a 20 4e 65 77 20 76 64 62 65 20      /* New vdbe 
1ebde 66 72 61 6d 65 20 74 6f 20 65 78 65 63 75 74 65  frame to execute
1ebdf 20 69 6e 20 2a 2f 0a 20 20 53 75 62 50 72 6f 67   in */.  SubProg
1ebe0 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 20  ram *pProgram;  
1ebe1 20 2f 2a 20 53 75 62 2d 70 72 6f 67 72 61 6d 20   /* Sub-program 
1ebe2 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 20 20  to execute */.  
1ebe3 76 6f 69 64 20 2a 74 3b 20 20 20 20 20 20 20 20  void *t;        
1ebe4 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e          /* Token
1ebe5 20 69 64 65 6e 74 69 66 79 69 6e 67 20 74 72 69   identifying tri
1ebe6 67 67 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20 2f  gger */.#endif /
1ebe7 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1ebe8 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1ebe9 7a 20 2a 2f 0a 0a 20 20 75 2e 62 7a 2e 70 50 72  z */..  u.bz.pPr
1ebea 6f 67 72 61 6d 20 3d 20 70 4f 70 2d 3e 70 34 2e  ogram = pOp->p4.
1ebeb 70 50 72 6f 67 72 61 6d 3b 0a 20 20 75 2e 62 7a  pProgram;.  u.bz
1ebec 2e 70 52 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  .pRt = &p->aMem[
1ebed 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 65  pOp->p3];.  asse
1ebee 72 74 28 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61  rt( u.bz.pProgra
1ebef 6d 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 0a 20 20 2f  m->nOp>0 );..  /
1ebf0 2a 20 49 66 20 74 68 65 20 70 35 20 66 6c 61 67  * If the p5 flag
1ebf1 20 69 73 20 63 6c 65 61 72 2c 20 74 68 65 6e 20   is clear, then 
1ebf2 72 65 63 75 72 73 69 76 65 20 69 6e 76 6f 63 61  recursive invoca
1ebf3 74 69 6f 6e 20 6f 66 20 74 72 69 67 67 65 72 73  tion of triggers
1ebf4 20 69 73 0a 20 20 2a 2a 20 64 69 73 61 62 6c 65   is.  ** disable
1ebf5 64 20 66 6f 72 20 62 61 63 6b 77 61 72 64 73 20  d for backwards 
1ebf6 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 28 70  compatibility (p
1ebf7 35 20 69 73 20 73 65 74 20 69 66 20 74 68 69 73  5 is set if this
1ebf8 20 73 75 62 2d 70 72 6f 67 72 61 6d 0a 20 20 2a   sub-program.  *
1ebf9 2a 20 69 73 20 72 65 61 6c 6c 79 20 61 20 74 72  * is really a tr
1ebfa 69 67 67 65 72 2c 20 6e 6f 74 20 61 20 66 6f 72  igger, not a for
1ebfb 65 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 2c  eign key action,
1ebfc 20 61 6e 64 20 74 68 65 20 66 6c 61 67 20 73 65   and the flag se
1ebfd 74 0a 20 20 2a 2a 20 61 6e 64 20 63 6c 65 61 72  t.  ** and clear
1ebfe 65 64 20 62 79 20 74 68 65 20 22 50 52 41 47 4d  ed by the "PRAGM
1ebff 41 20 72 65 63 75 72 73 69 76 65 5f 74 72 69 67  A recursive_trig
1ec00 67 65 72 73 22 20 63 6f 6d 6d 61 6e 64 20 69 73  gers" command is
1ec01 20 63 6c 65 61 72 29 2e 0a 20 20 2a 2a 0a 20 20   clear)..  **.  
1ec02 2a 2a 20 49 74 20 69 73 20 72 65 63 75 72 73 69  ** It is recursi
1ec03 76 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66  ve invocation of
1ec04 20 74 72 69 67 67 65 72 73 2c 20 61 74 20 74 68   triggers, at th
1ec05 65 20 53 51 4c 20 6c 65 76 65 6c 2c 20 74 68 61  e SQL level, tha
1ec06 74 20 69 73 0a 20 20 2a 2a 20 64 69 73 61 62 6c  t is.  ** disabl
1ec07 65 64 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73 65  ed. In some case
1ec08 73 20 61 20 73 69 6e 67 6c 65 20 74 72 69 67 67  s a single trigg
1ec09 65 72 20 6d 61 79 20 67 65 6e 65 72 61 74 65 20  er may generate 
1ec0a 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 0a 20 20  more than one.  
1ec0b 2a 2a 20 53 75 62 50 72 6f 67 72 61 6d 20 28 69  ** SubProgram (i
1ec0c 66 20 74 68 65 20 74 72 69 67 67 65 72 20 6d 61  f the trigger ma
1ec0d 79 20 62 65 20 65 78 65 63 75 74 65 64 20 77 69  y be executed wi
1ec0e 74 68 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  th more than one
1ec0f 20 64 69 66 66 65 72 65 6e 74 0a 20 20 2a 2a 20   different.  ** 
1ec10 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 6f  ON CONFLICT algo
1ec11 72 69 74 68 6d 29 2e 20 53 75 62 50 72 6f 67 72  rithm). SubProgr
1ec12 61 6d 20 73 74 72 75 63 74 75 72 65 73 20 61 73  am structures as
1ec13 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 0a  sociated with a.
1ec14 20 20 2a 2a 20 73 69 6e 67 6c 65 20 74 72 69 67    ** single trig
1ec15 67 65 72 20 61 6c 6c 20 68 61 76 65 20 74 68 65  ger all have the
1ec16 20 73 61 6d 65 20 76 61 6c 75 65 20 66 6f 72 20   same value for 
1ec17 74 68 65 20 53 75 62 50 72 6f 67 72 61 6d 2e 74  the SubProgram.t
1ec18 6f 6b 65 6e 0a 20 20 2a 2a 20 76 61 72 69 61 62  oken.  ** variab
1ec19 6c 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f  le.  */.  if( pO
1ec1a 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 75 2e 62  p->p5 ){.    u.b
1ec1b 7a 2e 74 20 3d 20 75 2e 62 7a 2e 70 50 72 6f 67  z.t = u.bz.pProg
1ec1c 72 61 6d 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20 20  ram->token;.    
1ec1d 66 6f 72 28 75 2e 62 7a 2e 70 46 72 61 6d 65 3d  for(u.bz.pFrame=
1ec1e 70 2d 3e 70 46 72 61 6d 65 3b 20 75 2e 62 7a 2e  p->pFrame; u.bz.
1ec1f 70 46 72 61 6d 65 20 26 26 20 75 2e 62 7a 2e 70  pFrame && u.bz.p
1ec20 46 72 61 6d 65 2d 3e 74 6f 6b 65 6e 21 3d 75 2e  Frame->token!=u.
1ec21 62 7a 2e 74 3b 20 75 2e 62 7a 2e 70 46 72 61 6d  bz.t; u.bz.pFram
1ec22 65 3d 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 70  e=u.bz.pFrame->p
1ec23 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 69 66 28  Parent);.    if(
1ec24 20 75 2e 62 7a 2e 70 46 72 61 6d 65 20 29 20 62   u.bz.pFrame ) b
1ec25 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  reak;.  }..  if(
1ec26 20 70 2d 3e 6e 46 72 61 6d 65 3e 3d 64 62 2d 3e   p->nFrame>=db->
1ec27 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
1ec28 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 50 54  MIT_TRIGGER_DEPT
1ec29 48 5d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  H] ){.    rc = S
1ec2a 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
1ec2b 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
1ec2c 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  g(&p->zErrMsg, d
1ec2d 62 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 6c 65 76  b, "too many lev
1ec2e 65 6c 73 20 6f 66 20 74 72 69 67 67 65 72 20 72  els of trigger r
1ec2f 65 63 75 72 73 69 6f 6e 22 29 3b 0a 20 20 20 20  ecursion");.    
1ec30 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  break;.  }..  /*
1ec31 20 52 65 67 69 73 74 65 72 20 75 2e 62 7a 2e 70   Register u.bz.p
1ec32 52 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 74  Rt is used to st
1ec33 6f 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 72  ore the memory r
1ec34 65 71 75 69 72 65 64 20 74 6f 20 73 61 76 65 20  equired to save 
1ec35 74 68 65 20 73 74 61 74 65 0a 20 20 2a 2a 20 6f  the state.  ** o
1ec36 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 72  f the current pr
1ec37 6f 67 72 61 6d 2c 20 61 6e 64 20 74 68 65 20 6d  ogram, and the m
1ec38 65 6d 6f 72 79 20 72 65 71 75 69 72 65 64 20 61  emory required a
1ec39 74 20 72 75 6e 74 69 6d 65 20 74 6f 20 65 78 65  t runtime to exe
1ec3a 63 75 74 65 0a 20 20 2a 2a 20 74 68 65 20 74 72  cute.  ** the tr
1ec3b 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 20 49  igger program. I
1ec3c 66 20 74 68 69 73 20 74 72 69 67 67 65 72 20 68  f this trigger h
1ec3d 61 73 20 62 65 65 6e 20 66 69 72 65 64 20 62 65  as been fired be
1ec3e 66 6f 72 65 2c 20 74 68 65 6e 20 75 2e 62 7a 2e  fore, then u.bz.
1ec3f 70 52 74 0a 20 20 2a 2a 20 69 73 20 61 6c 72 65  pRt.  ** is alre
1ec40 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 4f  ady allocated. O
1ec41 74 68 65 72 77 69 73 65 2c 20 69 74 20 6d 75 73  therwise, it mus
1ec42 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64  t be initialized
1ec43 2e 20 20 2a 2f 0a 20 20 69 66 28 20 28 75 2e 62  .  */.  if( (u.b
1ec44 7a 2e 70 52 74 2d 3e 66 6c 61 67 73 26 4d 45 4d  z.pRt->flags&MEM
1ec45 5f 46 72 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20  _Frame)==0 ){.  
1ec46 20 20 2f 2a 20 53 75 62 50 72 6f 67 72 61 6d 2e    /* SubProgram.
1ec47 6e 4d 65 6d 20 69 73 20 73 65 74 20 74 6f 20 74  nMem is set to t
1ec48 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 65 6d  he number of mem
1ec49 6f 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 62  ory cells used b
1ec4a 79 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 72 6f  y the.    ** pro
1ec4b 67 72 61 6d 20 73 74 6f 72 65 64 20 69 6e 20 53  gram stored in S
1ec4c 75 62 50 72 6f 67 72 61 6d 2e 61 4f 70 2e 20 41  ubProgram.aOp. A
1ec4d 73 20 77 65 6c 6c 20 61 73 20 74 68 65 73 65 2c  s well as these,
1ec4e 20 6f 6e 65 20 6d 65 6d 6f 72 79 0a 20 20 20 20   one memory.    
1ec4f 2a 2a 20 63 65 6c 6c 20 69 73 20 72 65 71 75 69  ** cell is requi
1ec50 72 65 64 20 66 6f 72 20 65 61 63 68 20 63 75 72  red for each cur
1ec51 73 6f 72 20 75 73 65 64 20 62 79 20 74 68 65 20  sor used by the 
1ec52 70 72 6f 67 72 61 6d 2e 20 53 65 74 20 6c 6f 63  program. Set loc
1ec53 61 6c 0a 20 20 20 20 2a 2a 20 76 61 72 69 61 62  al.    ** variab
1ec54 6c 65 20 75 2e 62 7a 2e 6e 4d 65 6d 20 28 61 6e  le u.bz.nMem (an
1ec55 64 20 6c 61 74 65 72 2c 20 56 64 62 65 46 72 61  d later, VdbeFra
1ec56 6d 65 2e 6e 43 68 69 6c 64 4d 65 6d 29 20 74 6f  me.nChildMem) to
1ec57 20 74 68 69 73 20 76 61 6c 75 65 2e 0a 20 20 20   this value..   
1ec58 20 2a 2f 0a 20 20 20 20 75 2e 62 7a 2e 6e 4d 65   */.    u.bz.nMe
1ec59 6d 20 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61  m = u.bz.pProgra
1ec5a 6d 2d 3e 6e 4d 65 6d 20 2b 20 75 2e 62 7a 2e 70  m->nMem + u.bz.p
1ec5b 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3b 0a 20  Program->nCsr;. 
1ec5c 20 20 20 75 2e 62 7a 2e 6e 42 79 74 65 20 3d 20     u.bz.nByte = 
1ec5d 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 56 64  ROUND8(sizeof(Vd
1ec5e 62 65 46 72 61 6d 65 29 29 0a 20 20 20 20 20 20  beFrame)).      
1ec5f 20 20 20 20 20 20 20 20 2b 20 75 2e 62 7a 2e 6e          + u.bz.n
1ec60 4d 65 6d 20 2a 20 73 69 7a 65 6f 66 28 4d 65 6d  Mem * sizeof(Mem
1ec61 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
1ec62 2b 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d  + u.bz.pProgram-
1ec63 3e 6e 43 73 72 20 2a 20 73 69 7a 65 6f 66 28 56  >nCsr * sizeof(V
1ec64 64 62 65 43 75 72 73 6f 72 20 2a 29 3b 0a 20 20  dbeCursor *);.  
1ec65 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 20 3d 20    u.bz.pFrame = 
1ec66 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
1ec67 65 72 6f 28 64 62 2c 20 75 2e 62 7a 2e 6e 42 79  ero(db, u.bz.nBy
1ec68 74 65 29 3b 0a 20 20 20 20 69 66 28 20 21 75 2e  te);.    if( !u.
1ec69 62 7a 2e 70 46 72 61 6d 65 20 29 7b 0a 20 20 20  bz.pFrame ){.   
1ec6a 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a     goto no_mem;.
1ec6b 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
1ec6c 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
1ec6d 75 2e 62 7a 2e 70 52 74 29 3b 0a 20 20 20 20 75  u.bz.pRt);.    u
1ec6e 2e 62 7a 2e 70 52 74 2d 3e 66 6c 61 67 73 20 3d  .bz.pRt->flags =
1ec6f 20 4d 45 4d 5f 46 72 61 6d 65 3b 0a 20 20 20 20   MEM_Frame;.    
1ec70 75 2e 62 7a 2e 70 52 74 2d 3e 75 2e 70 46 72 61  u.bz.pRt->u.pFra
1ec71 6d 65 20 3d 20 75 2e 62 7a 2e 70 46 72 61 6d 65  me = u.bz.pFrame
1ec72 3b 0a 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 61  ;..    u.bz.pFra
1ec73 6d 65 2d 3e 76 20 3d 20 70 3b 0a 20 20 20 20 75  me->v = p;.    u
1ec74 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69  .bz.pFrame->nChi
1ec75 6c 64 4d 65 6d 20 3d 20 75 2e 62 7a 2e 6e 4d 65  ldMem = u.bz.nMe
1ec76 6d 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 61  m;.    u.bz.pFra
1ec77 6d 65 2d 3e 6e 43 68 69 6c 64 43 73 72 20 3d 20  me->nChildCsr = 
1ec78 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e  u.bz.pProgram->n
1ec79 43 73 72 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46  Csr;.    u.bz.pF
1ec7a 72 61 6d 65 2d 3e 70 63 20 3d 20 70 63 3b 0a 20  rame->pc = pc;. 
1ec7b 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e     u.bz.pFrame->
1ec7c 61 4d 65 6d 20 3d 20 70 2d 3e 61 4d 65 6d 3b 0a  aMem = p->aMem;.
1ec7d 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d      u.bz.pFrame-
1ec7e 3e 6e 4d 65 6d 20 3d 20 70 2d 3e 6e 4d 65 6d 3b  >nMem = p->nMem;
1ec7f 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65  .    u.bz.pFrame
1ec80 2d 3e 61 70 43 73 72 20 3d 20 70 2d 3e 61 70 43  ->apCsr = p->apC
1ec81 73 72 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72  sr;.    u.bz.pFr
1ec82 61 6d 65 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 70  ame->nCursor = p
1ec83 2d 3e 6e 43 75 72 73 6f 72 3b 0a 20 20 20 20 75  ->nCursor;.    u
1ec84 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 61 4f 70 20  .bz.pFrame->aOp 
1ec85 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 20 20 75 2e  = p->aOp;.    u.
1ec86 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 4f 70 20 3d  bz.pFrame->nOp =
1ec87 20 70 2d 3e 6e 4f 70 3b 0a 20 20 20 20 75 2e 62   p->nOp;.    u.b
1ec88 7a 2e 70 46 72 61 6d 65 2d 3e 74 6f 6b 65 6e 20  z.pFrame->token 
1ec89 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d  = u.bz.pProgram-
1ec8a 3e 74 6f 6b 65 6e 3b 0a 0a 20 20 20 20 75 2e 62  >token;..    u.b
1ec8b 7a 2e 70 45 6e 64 20 3d 20 26 56 64 62 65 46 72  z.pEnd = &VdbeFr
1ec8c 61 6d 65 4d 65 6d 28 75 2e 62 7a 2e 70 46 72 61  ameMem(u.bz.pFra
1ec8d 6d 65 29 5b 75 2e 62 7a 2e 70 46 72 61 6d 65 2d  me)[u.bz.pFrame-
1ec8e 3e 6e 43 68 69 6c 64 4d 65 6d 5d 3b 0a 20 20 20  >nChildMem];.   
1ec8f 20 66 6f 72 28 75 2e 62 7a 2e 70 4d 65 6d 3d 56   for(u.bz.pMem=V
1ec90 64 62 65 46 72 61 6d 65 4d 65 6d 28 75 2e 62 7a  dbeFrameMem(u.bz
1ec91 2e 70 46 72 61 6d 65 29 3b 20 75 2e 62 7a 2e 70  .pFrame); u.bz.p
1ec92 4d 65 6d 21 3d 75 2e 62 7a 2e 70 45 6e 64 3b 20  Mem!=u.bz.pEnd; 
1ec93 75 2e 62 7a 2e 70 4d 65 6d 2b 2b 29 7b 0a 20 20  u.bz.pMem++){.  
1ec94 20 20 20 20 75 2e 62 7a 2e 70 4d 65 6d 2d 3e 66      u.bz.pMem->f
1ec95 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b  lags = MEM_Null;
1ec96 0a 20 20 20 20 20 20 75 2e 62 7a 2e 70 4d 65 6d  .      u.bz.pMem
1ec97 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 7d  ->db = db;.    }
1ec98 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e  .  }else{.    u.
1ec99 62 7a 2e 70 46 72 61 6d 65 20 3d 20 75 2e 62 7a  bz.pFrame = u.bz
1ec9a 2e 70 52 74 2d 3e 75 2e 70 46 72 61 6d 65 3b 0a  .pRt->u.pFrame;.
1ec9b 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 7a      assert( u.bz
1ec9c 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4d 65 6d 2b  .pProgram->nMem+
1ec9d 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e  u.bz.pProgram->n
1ec9e 43 73 72 3d 3d 75 2e 62 7a 2e 70 46 72 61 6d 65  Csr==u.bz.pFrame
1ec9f 2d 3e 6e 43 68 69 6c 64 4d 65 6d 20 29 3b 0a 20  ->nChildMem );. 
1eca0 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 7a 2e     assert( u.bz.
1eca1 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3d 3d  pProgram->nCsr==
1eca2 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 68  u.bz.pFrame->nCh
1eca3 69 6c 64 43 73 72 20 29 3b 0a 20 20 20 20 61 73  ildCsr );.    as
1eca4 73 65 72 74 28 20 70 63 3d 3d 75 2e 62 7a 2e 70  sert( pc==u.bz.p
1eca5 46 72 61 6d 65 2d 3e 70 63 20 29 3b 0a 20 20 7d  Frame->pc );.  }
1eca6 0a 0a 20 20 70 2d 3e 6e 46 72 61 6d 65 2b 2b 3b  ..  p->nFrame++;
1eca7 0a 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e  .  u.bz.pFrame->
1eca8 70 50 61 72 65 6e 74 20 3d 20 70 2d 3e 70 46 72  pParent = p->pFr
1eca9 61 6d 65 3b 0a 20 20 75 2e 62 7a 2e 70 46 72 61  ame;.  u.bz.pFra
1ecaa 6d 65 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20  me->lastRowid = 
1ecab 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20  db->lastRowid;. 
1ecac 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43   u.bz.pFrame->nC
1ecad 68 61 6e 67 65 20 3d 20 70 2d 3e 6e 43 68 61 6e  hange = p->nChan
1ecae 67 65 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65  ge;.  p->nChange
1ecaf 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 46 72 61 6d   = 0;.  p->pFram
1ecb0 65 20 3d 20 75 2e 62 7a 2e 70 46 72 61 6d 65 3b  e = u.bz.pFrame;
1ecb1 0a 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 26 56 64  .  p->aMem = &Vd
1ecb2 62 65 46 72 61 6d 65 4d 65 6d 28 75 2e 62 7a 2e  beFrameMem(u.bz.
1ecb3 70 46 72 61 6d 65 29 5b 2d 31 5d 3b 0a 20 20 70  pFrame)[-1];.  p
1ecb4 2d 3e 6e 4d 65 6d 20 3d 20 75 2e 62 7a 2e 70 46  ->nMem = u.bz.pF
1ecb5 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d 3b  rame->nChildMem;
1ecb6 0a 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d 20  .  p->nCursor = 
1ecb7 28 75 31 36 29 75 2e 62 7a 2e 70 46 72 61 6d 65  (u16)u.bz.pFrame
1ecb8 2d 3e 6e 43 68 69 6c 64 43 73 72 3b 0a 20 20 70  ->nChildCsr;.  p
1ecb9 2d 3e 61 70 43 73 72 20 3d 20 28 56 64 62 65 43  ->apCsr = (VdbeC
1ecba 75 72 73 6f 72 20 2a 2a 29 26 70 2d 3e 61 4d 65  ursor **)&p->aMe
1ecbb 6d 5b 70 2d 3e 6e 4d 65 6d 2b 31 5d 3b 0a 20 20  m[p->nMem+1];.  
1ecbc 70 2d 3e 61 4f 70 20 3d 20 75 2e 62 7a 2e 70 50  p->aOp = u.bz.pP
1ecbd 72 6f 67 72 61 6d 2d 3e 61 4f 70 3b 0a 20 20 70  rogram->aOp;.  p
1ecbe 2d 3e 6e 4f 70 20 3d 20 75 2e 62 7a 2e 70 50 72  ->nOp = u.bz.pPr
1ecbf 6f 67 72 61 6d 2d 3e 6e 4f 70 3b 0a 20 20 70 63  ogram->nOp;.  pc
1ecc0 20 3d 20 2d 31 3b 0a 0a 20 20 62 72 65 61 6b 3b   = -1;..  break;
1ecc1 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50  .}../* Opcode: P
1ecc2 61 72 61 6d 20 50 31 20 50 32 20 2a 20 2a 20 2a  aram P1 P2 * * *
1ecc3 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  .**.** This opco
1ecc4 64 65 20 69 73 20 6f 6e 6c 79 20 65 76 65 72 20  de is only ever 
1ecc5 70 72 65 73 65 6e 74 20 69 6e 20 73 75 62 2d 70  present in sub-p
1ecc6 72 6f 67 72 61 6d 73 20 63 61 6c 6c 65 64 20 76  rograms called v
1ecc7 69 61 20 74 68 65 20 0a 2a 2a 20 4f 50 5f 50 72  ia the .** OP_Pr
1ecc8 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f  ogram instructio
1ecc9 6e 2e 20 43 6f 70 79 20 61 20 76 61 6c 75 65 20  n. Copy a value 
1ecca 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64  currently stored
1eccb 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 0a 2a 2a   in a memory .**
1eccc 20 63 65 6c 6c 20 6f 66 20 74 68 65 20 63 61 6c   cell of the cal
1eccd 6c 69 6e 67 20 28 70 61 72 65 6e 74 29 20 66 72  ling (parent) fr
1ecce 61 6d 65 20 74 6f 20 63 65 6c 6c 20 50 32 20 69  ame to cell P2 i
1eccf 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 72  n the current fr
1ecd0 61 6d 65 73 20 0a 2a 2a 20 61 64 64 72 65 73 73  ames .** address
1ecd1 20 73 70 61 63 65 2e 20 54 68 69 73 20 69 73 20   space. This is 
1ecd2 75 73 65 64 20 62 79 20 74 72 69 67 67 65 72 20  used by trigger 
1ecd3 70 72 6f 67 72 61 6d 73 20 74 6f 20 61 63 63 65  programs to acce
1ecd4 73 73 20 74 68 65 20 6e 65 77 2e 2a 20 0a 2a 2a  ss the new.* .**
1ecd5 20 61 6e 64 20 6f 6c 64 2e 2a 20 76 61 6c 75 65   and old.* value
1ecd6 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 64 64  s..**.** The add
1ecd7 72 65 73 73 20 6f 66 20 74 68 65 20 63 65 6c 6c  ress of the cell
1ecd8 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 66   in the parent f
1ecd9 72 61 6d 65 20 69 73 20 64 65 74 65 72 6d 69 6e  rame is determin
1ecda 65 64 20 62 79 20 61 64 64 69 6e 67 0a 2a 2a 20  ed by adding.** 
1ecdb 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
1ecdc 20 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f 20   P1 argument to 
1ecdd 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
1ecde 20 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f 20   P1 argument to 
1ecdf 74 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 4f  the.** calling O
1ece0 50 5f 50 72 6f 67 72 61 6d 20 69 6e 73 74 72 75  P_Program instru
1ece1 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f  ction..*/.case O
1ece2 50 5f 50 61 72 61 6d 3a 20 7b 20 20 20 20 20 20  P_Param: {      
1ece3 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65       /* out2-pre
1ece4 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30  release */.#if 0
1ece5 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1ece6 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1ece7 75 2e 63 61 20 2a 2f 0a 20 20 56 64 62 65 46 72  u.ca */.  VdbeFr
1ece8 61 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 20 20 4d  ame *pFrame;.  M
1ece9 65 6d 20 2a 70 49 6e 3b 0a 23 65 6e 64 69 66 20  em *pIn;.#endif 
1ecea 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1eceb 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1ecec 63 61 20 2a 2f 0a 20 20 75 2e 63 61 2e 70 46 72  ca */.  u.ca.pFr
1eced 61 6d 65 20 3d 20 70 2d 3e 70 46 72 61 6d 65 3b  ame = p->pFrame;
1ecee 0a 20 20 75 2e 63 61 2e 70 49 6e 20 3d 20 26 75  .  u.ca.pIn = &u
1ecef 2e 63 61 2e 70 46 72 61 6d 65 2d 3e 61 4d 65 6d  .ca.pFrame->aMem
1ecf0 5b 70 4f 70 2d 3e 70 31 20 2b 20 75 2e 63 61 2e  [pOp->p1 + u.ca.
1ecf1 70 46 72 61 6d 65 2d 3e 61 4f 70 5b 75 2e 63 61  pFrame->aOp[u.ca
1ecf2 2e 70 46 72 61 6d 65 2d 3e 70 63 5d 2e 70 31 5d  .pFrame->pc].p1]
1ecf3 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
1ecf4 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f  emShallowCopy(pO
1ecf5 75 74 2c 20 75 2e 63 61 2e 70 49 6e 2c 20 4d 45  ut, u.ca.pIn, ME
1ecf6 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 62 72 65 61  M_Ephem);.  brea
1ecf7 6b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  k;.}..#endif /* 
1ecf8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1ecf9 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a 0a  MIT_TRIGGER */..
1ecfa 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1ecfb 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a  MIT_FOREIGN_KEY.
1ecfc 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6b 43 6f 75  /* Opcode: FkCou
1ecfd 6e 74 65 72 20 50 31 20 50 32 20 2a 20 2a 20 2a  nter P1 P2 * * *
1ecfe 0a 2a 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74  .**.** Increment
1ecff 20 61 20 22 63 6f 6e 73 74 72 61 69 6e 74 20 63   a "constraint c
1ed00 6f 75 6e 74 65 72 22 20 62 79 20 50 32 20 28 50  ounter" by P2 (P
1ed01 32 20 6d 61 79 20 62 65 20 6e 65 67 61 74 69 76  2 may be negativ
1ed02 65 20 6f 72 20 70 6f 73 69 74 69 76 65 29 2e 0a  e or positive)..
1ed03 2a 2a 20 49 66 20 50 31 20 69 73 20 6e 6f 6e 2d  ** If P1 is non-
1ed04 7a 65 72 6f 2c 20 74 68 65 20 64 61 74 61 62 61  zero, the databa
1ed05 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f  se constraint co
1ed06 75 6e 74 65 72 20 69 73 20 69 6e 63 72 65 6d 65  unter is increme
1ed07 6e 74 65 64 20 0a 2a 2a 20 28 64 65 66 65 72 72  nted .** (deferr
1ed08 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  ed foreign key c
1ed09 6f 6e 73 74 72 61 69 6e 74 73 29 2e 20 4f 74 68  onstraints). Oth
1ed0a 65 72 77 69 73 65 2c 20 69 66 20 50 31 20 69 73  erwise, if P1 is
1ed0b 20 7a 65 72 6f 2c 20 74 68 65 20 0a 2a 2a 20 73   zero, the .** s
1ed0c 74 61 74 65 6d 65 6e 74 20 63 6f 75 6e 74 65 72  tatement counter
1ed0d 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20   is incremented 
1ed0e 28 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 69  (immediate forei
1ed0f 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
1ed10 74 73 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ts)..*/.case OP_
1ed11 46 6b 43 6f 75 6e 74 65 72 3a 20 7b 0a 20 20 69  FkCounter: {.  i
1ed12 66 28 20 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20  f( pOp->p1 ){.  
1ed13 20 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 43    db->nDeferredC
1ed14 6f 6e 73 20 2b 3d 20 70 4f 70 2d 3e 70 32 3b 0a  ons += pOp->p2;.
1ed15 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e    }else{.    p->
1ed16 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 20 2b 3d  nFkConstraint +=
1ed17 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 7d 0a 20 20   pOp->p2;.  }.  
1ed18 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1ed19 6f 64 65 3a 20 46 6b 49 66 5a 65 72 6f 20 50 31  ode: FkIfZero P1
1ed1a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1ed1b 54 68 69 73 20 6f 70 63 6f 64 65 20 74 65 73 74  This opcode test
1ed1c 73 20 69 66 20 61 20 66 6f 72 65 69 67 6e 20 6b  s if a foreign k
1ed1d 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f  ey constraint-co
1ed1e 75 6e 74 65 72 20 69 73 20 63 75 72 72 65 6e 74  unter is current
1ed1f 6c 79 20 7a 65 72 6f 2e 0a 2a 2a 20 49 66 20 73  ly zero..** If s
1ed20 6f 2c 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72  o, jump to instr
1ed21 75 63 74 69 6f 6e 20 50 32 2e 20 4f 74 68 65 72  uction P2. Other
1ed22 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 6f 75  wise, fall throu
1ed23 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 0a  gh to the next .
1ed24 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  ** instruction..
1ed25 2a 2a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 6e  **.** If P1 is n
1ed26 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68  on-zero, then th
1ed27 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20  e jump is taken 
1ed28 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  if the database 
1ed29 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e 74  constraint-count
1ed2a 65 72 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 28 74  er.** is zero (t
1ed2b 68 65 20 6f 6e 65 20 74 68 61 74 20 63 6f 75 6e  he one that coun
1ed2c 74 73 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73  ts deferred cons
1ed2d 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e  traint violation
1ed2e 73 29 2e 20 49 66 20 50 31 20 69 73 0a 2a 2a 20  s). If P1 is.** 
1ed2f 7a 65 72 6f 2c 20 74 68 65 20 6a 75 6d 70 20 69  zero, the jump i
1ed30 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20 73  s taken if the s
1ed31 74 61 74 65 6d 65 6e 74 20 63 6f 6e 73 74 72 61  tatement constra
1ed32 69 6e 74 2d 63 6f 75 6e 74 65 72 20 69 73 20 7a  int-counter is z
1ed33 65 72 6f 0a 2a 2a 20 28 69 6d 6d 65 64 69 61 74  ero.** (immediat
1ed34 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  e foreign key co
1ed35 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69  nstraint violati
1ed36 6f 6e 73 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  ons)..*/.case OP
1ed37 5f 46 6b 49 66 5a 65 72 6f 3a 20 7b 20 20 20 20  _FkIfZero: {    
1ed38 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a       /* jump */.
1ed39 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 20 29 7b    if( pOp->p1 ){
1ed3a 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6e 44 65  .    if( db->nDe
1ed3b 66 65 72 72 65 64 43 6f 6e 73 3d 3d 30 20 29 20  ferredCons==0 ) 
1ed3c 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a  pc = pOp->p2-1;.
1ed3d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28    }else{.    if(
1ed3e 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 6e   p->nFkConstrain
1ed3f 74 3d 3d 30 20 29 20 70 63 20 3d 20 70 4f 70 2d  t==0 ) pc = pOp-
1ed40 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 65  >p2-1;.  }.  bre
1ed41 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
1ed42 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1ed43 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 20  MIT_FOREIGN_KEY 
1ed44 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
1ed45 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52  TE_OMIT_AUTOINCR
1ed46 45 4d 45 4e 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a  EMENT./* Opcode:
1ed47 20 4d 65 6d 4d 61 78 20 50 31 20 50 32 20 2a 20   MemMax P1 P2 * 
1ed48 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20  * *.**.** P1 is 
1ed49 61 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 68  a register in th
1ed4a 65 20 72 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20  e root frame of 
1ed4b 74 68 69 73 20 56 4d 20 28 74 68 65 20 72 6f 6f  this VM (the roo
1ed4c 74 20 66 72 61 6d 65 20 69 73 0a 2a 2a 20 64 69  t frame is.** di
1ed4d 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65  fferent from the
1ed4e 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65 20 69   current frame i
1ed4f 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  f this instructi
1ed50 6f 6e 20 69 73 20 62 65 69 6e 67 20 65 78 65 63  on is being exec
1ed51 75 74 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 61  uted.** within a
1ed52 20 73 75 62 2d 70 72 6f 67 72 61 6d 29 2e 20 53   sub-program). S
1ed53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  et the value of 
1ed54 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 74  register P1 to t
1ed55 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 0a 2a  he maximum of .*
1ed56 2a 20 69 74 73 20 63 75 72 72 65 6e 74 20 76 61  * its current va
1ed57 6c 75 65 20 61 6e 64 20 74 68 65 20 76 61 6c 75  lue and the valu
1ed58 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32  e in register P2
1ed59 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73  ..**.** This ins
1ed5a 74 72 75 63 74 69 6f 6e 20 74 68 72 6f 77 73 20  truction throws 
1ed5b 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 20  an error if the 
1ed5c 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20 6e  memory cell is n
1ed5d 6f 74 20 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a 20  ot initially.** 
1ed5e 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63  an integer..*/.c
1ed5f 61 73 65 20 4f 50 5f 4d 65 6d 4d 61 78 3a 20 7b  ase OP_MemMax: {
1ed60 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 2a          /* in2 *
1ed61 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
1ed62 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1ed63 64 20 69 6e 74 6f 20 75 2e 63 62 20 2a 2f 0a 20  d into u.cb */. 
1ed64 20 4d 65 6d 20 2a 70 49 6e 31 3b 0a 20 20 56 64   Mem *pIn1;.  Vd
1ed65 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b  beFrame *pFrame;
1ed66 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1ed67 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1ed68 20 69 6e 74 6f 20 75 2e 63 62 20 2a 2f 0a 20 20   into u.cb */.  
1ed69 69 66 28 20 70 2d 3e 70 46 72 61 6d 65 20 29 7b  if( p->pFrame ){
1ed6a 0a 20 20 20 20 66 6f 72 28 75 2e 63 62 2e 70 46  .    for(u.cb.pF
1ed6b 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 3b 20  rame=p->pFrame; 
1ed6c 75 2e 63 62 2e 70 46 72 61 6d 65 2d 3e 70 50 61  u.cb.pFrame->pPa
1ed6d 72 65 6e 74 3b 20 75 2e 63 62 2e 70 46 72 61 6d  rent; u.cb.pFram
1ed6e 65 3d 75 2e 63 62 2e 70 46 72 61 6d 65 2d 3e 70  e=u.cb.pFrame->p
1ed6f 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 75 2e 63  Parent);.    u.c
1ed70 62 2e 70 49 6e 31 20 3d 20 26 75 2e 63 62 2e 70  b.pIn1 = &u.cb.p
1ed71 46 72 61 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70 2d  Frame->aMem[pOp-
1ed72 3e 70 31 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  >p1];.  }else{. 
1ed73 20 20 20 75 2e 63 62 2e 70 49 6e 31 20 3d 20 26     u.cb.pIn1 = &
1ed74 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d  p->aMem[pOp->p1]
1ed75 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
1ed76 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
1ed77 28 75 2e 63 62 2e 70 49 6e 31 29 3b 0a 20 20 73  (u.cb.pIn1);.  s
1ed78 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74  qlite3VdbeMemInt
1ed79 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b 0a 20  egerify(pIn2);. 
1ed7a 20 69 66 28 20 75 2e 63 62 2e 70 49 6e 31 2d 3e   if( u.cb.pIn1->
1ed7b 75 2e 69 3c 70 49 6e 32 2d 3e 75 2e 69 29 7b 0a  u.i<pIn2->u.i){.
1ed7c 20 20 20 20 75 2e 63 62 2e 70 49 6e 31 2d 3e 75      u.cb.pIn1->u
1ed7d 2e 69 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b 0a  .i = pIn2->u.i;.
1ed7e 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23    }.  break;.}.#
1ed7f 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1ed80 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45  OMIT_AUTOINCREME
1ed81 4e 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65  NT */../* Opcode
1ed82 3a 20 49 66 50 6f 73 20 50 31 20 50 32 20 2a 20  : IfPos P1 P2 * 
1ed83 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  * *.**.** If the
1ed84 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74   value of regist
1ed85 65 72 20 50 31 20 69 73 20 31 20 6f 72 20 67 72  er P1 is 1 or gr
1ed86 65 61 74 65 72 2c 20 6a 75 6d 70 20 74 6f 20 50  eater, jump to P
1ed87 32 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69  2..**.** It is i
1ed88 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74 68  llegal to use th
1ed89 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f  is instruction o
1ed8a 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68 61  n a register tha
1ed8b 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f  t does.** not co
1ed8c 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 72  ntain an integer
1ed8d 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20  .  An assertion 
1ed8e 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 6c  fault will resul
1ed8f 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a 2f  t if you try..*/
1ed90 0a 63 61 73 65 20 4f 50 5f 49 66 50 6f 73 3a 20  .case OP_IfPos: 
1ed91 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70  {        /* jump
1ed92 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65 72  , in1 */.  asser
1ed93 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d  t( pIn1->flags&M
1ed94 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28 20  EM_Int );.  if( 
1ed95 70 49 6e 31 2d 3e 75 2e 69 3e 30 20 29 7b 0a 20  pIn1->u.i>0 ){. 
1ed96 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
1ed97 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61   - 1;.  }.  brea
1ed98 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1ed99 20 49 66 4e 65 67 20 50 31 20 50 32 20 2a 20 2a   IfNeg P1 P2 * *
1ed9a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20   *.**.** If the 
1ed9b 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65  value of registe
1ed9c 72 20 50 31 20 69 73 20 6c 65 73 73 20 74 68 61  r P1 is less tha
1ed9d 6e 20 7a 65 72 6f 2c 20 6a 75 6d 70 20 74 6f 20  n zero, jump to 
1ed9e 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73  P2. .**.** It is
1ed9f 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20   illegal to use 
1eda0 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  this instruction
1eda1 20 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74   on a register t
1eda2 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20  hat does.** not 
1eda3 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67  contain an integ
1eda4 65 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f  er.  An assertio
1eda5 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73  n fault will res
1eda6 75 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a  ult if you try..
1eda7 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 4e 65 67  */.case OP_IfNeg
1eda8 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75  : {        /* ju
1eda9 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73  mp, in1 */.  ass
1edaa 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73  ert( pIn1->flags
1edab 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66  &MEM_Int );.  if
1edac 28 20 70 49 6e 31 2d 3e 75 2e 69 3c 30 20 29 7b  ( pIn1->u.i<0 ){
1edad 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e  .     pc = pOp->
1edae 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72  p2 - 1;.  }.  br
1edaf 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1edb0 65 3a 20 49 66 5a 65 72 6f 20 50 31 20 50 32 20  e: IfZero P1 P2 
1edb1 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74  * * *.**.** If t
1edb2 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69  he value of regi
1edb3 73 74 65 72 20 50 31 20 69 73 20 65 78 61 63 74  ster P1 is exact
1edb4 6c 79 20 30 2c 20 6a 75 6d 70 20 74 6f 20 50 32  ly 0, jump to P2
1edb5 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69  . .**.** It is i
1edb6 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74 68  llegal to use th
1edb7 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f  is instruction o
1edb8 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68 61  n a register tha
1edb9 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63 6f  t does.** not co
1edba 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65 72  ntain an integer
1edbb 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20  .  An assertion 
1edbc 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75 6c  fault will resul
1edbd 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a 2f  t if you try..*/
1edbe 0a 63 61 73 65 20 4f 50 5f 49 66 5a 65 72 6f 3a  .case OP_IfZero:
1edbf 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
1edc0 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65  p, in1 */.  asse
1edc1 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26  rt( pIn1->flags&
1edc2 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28  MEM_Int );.  if(
1edc3 20 70 49 6e 31 2d 3e 75 2e 69 3d 3d 30 20 29 7b   pIn1->u.i==0 ){
1edc4 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e  .     pc = pOp->
1edc5 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72  p2 - 1;.  }.  br
1edc6 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1edc7 65 3a 20 41 67 67 53 74 65 70 20 2a 20 50 32 20  e: AggStep * P2 
1edc8 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 45  P3 P4 P5.**.** E
1edc9 78 65 63 75 74 65 20 74 68 65 20 73 74 65 70 20  xecute the step 
1edca 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20  function for an 
1edcb 61 67 67 72 65 67 61 74 65 2e 20 20 54 68 65 0a  aggregate.  The.
1edcc 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20  ** function has 
1edcd 50 35 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 20  P5 arguments.   
1edce 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  P4 is a pointer 
1edcf 74 6f 20 74 68 65 20 46 75 6e 63 44 65 66 0a 2a  to the FuncDef.*
1edd0 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  * structure that
1edd1 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 66   specifies the f
1edd2 75 6e 63 74 69 6f 6e 2e 20 20 55 73 65 20 72 65  unction.  Use re
1edd3 67 69 73 74 65 72 0a 2a 2a 20 50 33 20 61 73 20  gister.** P3 as 
1edd4 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 2e  the accumulator.
1edd5 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 35 20 61 72  .**.** The P5 ar
1edd6 67 75 6d 65 6e 74 73 20 61 72 65 20 74 61 6b 65  guments are take
1edd7 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20  n from register 
1edd8 50 32 20 61 6e 64 20 69 74 73 0a 2a 2a 20 73 75  P2 and its.** su
1edd9 63 63 65 73 73 6f 72 73 2e 0a 2a 2f 0a 63 61 73  ccessors..*/.cas
1edda 65 20 4f 50 5f 41 67 67 53 74 65 70 3a 20 7b 0a  e OP_AggStep: {.
1eddb 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1eddc 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1eddd 69 6e 74 6f 20 75 2e 63 63 20 2a 2f 0a 20 20 69  into u.cc */.  i
1edde 6e 74 20 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  nt n;.  int i;. 
1eddf 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 4d 65   Mem *pMem;.  Me
1ede0 6d 20 2a 70 52 65 63 3b 0a 20 20 73 71 6c 69 74  m *pRec;.  sqlit
1ede1 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a  e3_context ctx;.
1ede2 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
1ede3 2a 2a 61 70 56 61 6c 3b 0a 23 65 6e 64 69 66 20  **apVal;.#endif 
1ede4 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1ede5 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1ede6 63 63 20 2a 2f 0a 0a 20 20 75 2e 63 63 2e 6e 20  cc */..  u.cc.n 
1ede7 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 61 73 73  = pOp->p5;.  ass
1ede8 65 72 74 28 20 75 2e 63 63 2e 6e 3e 3d 30 20 29  ert( u.cc.n>=0 )
1ede9 3b 0a 20 20 75 2e 63 63 2e 70 52 65 63 20 3d 20  ;.  u.cc.pRec = 
1edea 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32  &p->aMem[pOp->p2
1edeb 5d 3b 0a 20 20 75 2e 63 63 2e 61 70 56 61 6c 20  ];.  u.cc.apVal 
1edec 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61 73  = p->apArg;.  as
1eded 73 65 72 74 28 20 75 2e 63 63 2e 61 70 56 61 6c  sert( u.cc.apVal
1edee 20 7c 7c 20 75 2e 63 63 2e 6e 3d 3d 30 20 29 3b   || u.cc.n==0 );
1edef 0a 20 20 66 6f 72 28 75 2e 63 63 2e 69 3d 30 3b  .  for(u.cc.i=0;
1edf0 20 75 2e 63 63 2e 69 3c 75 2e 63 63 2e 6e 3b 20   u.cc.i<u.cc.n; 
1edf1 75 2e 63 63 2e 69 2b 2b 2c 20 75 2e 63 63 2e 70  u.cc.i++, u.cc.p
1edf2 52 65 63 2b 2b 29 7b 0a 20 20 20 20 75 2e 63 63  Rec++){.    u.cc
1edf3 2e 61 70 56 61 6c 5b 75 2e 63 63 2e 69 5d 20 3d  .apVal[u.cc.i] =
1edf4 20 75 2e 63 63 2e 70 52 65 63 3b 0a 20 20 20 20   u.cc.pRec;.    
1edf5 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 75 2e  storeTypeInfo(u.
1edf6 63 63 2e 70 52 65 63 2c 20 65 6e 63 6f 64 69 6e  cc.pRec, encodin
1edf7 67 29 3b 0a 20 20 7d 0a 20 20 75 2e 63 63 2e 63  g);.  }.  u.cc.c
1edf8 74 78 2e 70 46 75 6e 63 20 3d 20 70 4f 70 2d 3e  tx.pFunc = pOp->
1edf9 70 34 2e 70 46 75 6e 63 3b 0a 20 20 61 73 73 65  p4.pFunc;.  asse
1edfa 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26  rt( pOp->p3>0 &&
1edfb 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65   pOp->p3<=p->nMe
1edfc 6d 20 29 3b 0a 20 20 75 2e 63 63 2e 63 74 78 2e  m );.  u.cc.ctx.
1edfd 70 4d 65 6d 20 3d 20 75 2e 63 63 2e 70 4d 65 6d  pMem = u.cc.pMem
1edfe 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1edff 3e 70 33 5d 3b 0a 20 20 75 2e 63 63 2e 70 4d 65  >p3];.  u.cc.pMe
1ee00 6d 2d 3e 6e 2b 2b 3b 0a 20 20 75 2e 63 63 2e 63  m->n++;.  u.cc.c
1ee01 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d  tx.s.flags = MEM
1ee02 5f 4e 75 6c 6c 3b 0a 20 20 75 2e 63 63 2e 63 74  _Null;.  u.cc.ct
1ee03 78 2e 73 2e 7a 20 3d 20 30 3b 0a 20 20 75 2e 63  x.s.z = 0;.  u.c
1ee04 63 2e 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63 20  c.ctx.s.zMalloc 
1ee05 3d 20 30 3b 0a 20 20 75 2e 63 63 2e 63 74 78 2e  = 0;.  u.cc.ctx.
1ee06 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 75 2e  s.xDel = 0;.  u.
1ee07 63 63 2e 63 74 78 2e 73 2e 64 62 20 3d 20 64 62  cc.ctx.s.db = db
1ee08 3b 0a 20 20 75 2e 63 63 2e 63 74 78 2e 69 73 45  ;.  u.cc.ctx.isE
1ee09 72 72 6f 72 20 3d 20 30 3b 0a 20 20 75 2e 63 63  rror = 0;.  u.cc
1ee0a 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 30 3b 0a  .ctx.pColl = 0;.
1ee0b 20 20 69 66 28 20 75 2e 63 63 2e 63 74 78 2e 70    if( u.cc.ctx.p
1ee0c 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53 51  Func->flags & SQ
1ee0d 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f  LITE_FUNC_NEEDCO
1ee0e 4c 4c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  LL ){.    assert
1ee0f 28 20 70 4f 70 3e 70 2d 3e 61 4f 70 20 29 3b 0a  ( pOp>p->aOp );.
1ee10 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b      assert( pOp[
1ee11 2d 31 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f 43  -1].p4type==P4_C
1ee12 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 20 20 61 73  OLLSEQ );.    as
1ee13 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70  sert( pOp[-1].op
1ee14 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71  code==OP_CollSeq
1ee15 20 29 3b 0a 20 20 20 20 75 2e 63 63 2e 63 74 78   );.    u.cc.ctx
1ee16 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d  .pColl = pOp[-1]
1ee17 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20  .p4.pColl;.  }. 
1ee18 20 28 75 2e 63 63 2e 63 74 78 2e 70 46 75 6e 63   (u.cc.ctx.pFunc
1ee19 2d 3e 78 53 74 65 70 29 28 26 75 2e 63 63 2e 63  ->xStep)(&u.cc.c
1ee1a 74 78 2c 20 75 2e 63 63 2e 6e 2c 20 75 2e 63 63  tx, u.cc.n, u.cc
1ee1b 2e 61 70 56 61 6c 29 3b 0a 20 20 69 66 28 20 75  .apVal);.  if( u
1ee1c 2e 63 63 2e 63 74 78 2e 69 73 45 72 72 6f 72 20  .cc.ctx.isError 
1ee1d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  ){.    sqlite3Se
1ee1e 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
1ee1f 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73  Msg, db, "%s", s
1ee20 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1ee21 74 28 26 75 2e 63 63 2e 63 74 78 2e 73 29 29 3b  t(&u.cc.ctx.s));
1ee22 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 63 2e 63  .    rc = u.cc.c
1ee23 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a  tx.isError;.  }.
1ee24 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1ee25 52 65 6c 65 61 73 65 28 26 75 2e 63 63 2e 63 74  Release(&u.cc.ct
1ee26 78 2e 73 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  x.s);.  break;.}
1ee27 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67  ../* Opcode: Agg
1ee28 46 69 6e 61 6c 20 50 31 20 50 32 20 2a 20 50 34  Final P1 P2 * P4
1ee29 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65   *.**.** Execute
1ee2a 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20 66   the finalizer f
1ee2b 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20 61  unction for an a
1ee2c 67 67 72 65 67 61 74 65 2e 20 20 50 31 20 69 73  ggregate.  P1 is
1ee2d 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c  .** the memory l
1ee2e 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20  ocation that is 
1ee2f 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20  the accumulator 
1ee30 66 6f 72 20 74 68 65 20 61 67 67 72 65 67 61 74  for the aggregat
1ee31 65 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 74  e..**.** P2 is t
1ee32 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  he number of arg
1ee33 75 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65 20  uments that the 
1ee34 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 74 61  step function ta
1ee35 6b 65 73 20 61 6e 64 0a 2a 2a 20 50 34 20 69 73  kes and.** P4 is
1ee36 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
1ee37 65 20 46 75 6e 63 44 65 66 20 66 6f 72 20 74 68  e FuncDef for th
1ee38 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  is function.  Th
1ee39 65 20 50 32 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  e P2.** argument
1ee3a 20 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79 20   is not used by 
1ee3b 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 49 74  this opcode.  It
1ee3c 20 69 73 20 6f 6e 6c 79 20 74 68 65 72 65 20 74   is only there t
1ee3d 6f 20 64 69 73 61 6d 62 69 67 75 61 74 65 0a 2a  o disambiguate.*
1ee3e 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  * functions that
1ee3f 20 63 61 6e 20 74 61 6b 65 20 76 61 72 79 69 6e   can take varyin
1ee40 67 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 72 67  g numbers of arg
1ee41 75 6d 65 6e 74 73 2e 20 20 54 68 65 0a 2a 2a 20  uments.  The.** 
1ee42 50 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f  P4 argument is o
1ee43 6e 6c 79 20 6e 65 65 64 65 64 20 66 6f 72 20 74  nly needed for t
1ee44 68 65 20 64 65 67 65 6e 65 72 61 74 65 20 63 61  he degenerate ca
1ee45 73 65 20 77 68 65 72 65 0a 2a 2a 20 74 68 65 20  se where.** the 
1ee46 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 77 61  step function wa
1ee47 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79  s not previously
1ee48 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 63 61 73 65   called..*/.case
1ee49 20 4f 50 5f 41 67 67 46 69 6e 61 6c 3a 20 7b 0a   OP_AggFinal: {.
1ee4a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1ee4b 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1ee4c 69 6e 74 6f 20 75 2e 63 64 20 2a 2f 0a 20 20 4d  into u.cd */.  M
1ee4d 65 6d 20 2a 70 4d 65 6d 3b 0a 23 65 6e 64 69 66  em *pMem;.#endif
1ee4e 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1ee4f 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1ee50 2e 63 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  .cd */.  assert(
1ee51 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f   pOp->p1>0 && pO
1ee52 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p1<=p->nMem )
1ee53 3b 0a 20 20 75 2e 63 64 2e 70 4d 65 6d 20 3d 20  ;.  u.cd.pMem = 
1ee54 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31  &p->aMem[pOp->p1
1ee55 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 75 2e  ];.  assert( (u.
1ee56 63 64 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  cd.pMem->flags &
1ee57 20 7e 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f   ~(MEM_Null|MEM_
1ee58 41 67 67 29 29 3d 3d 30 20 29 3b 0a 20 20 72 63  Agg))==0 );.  rc
1ee59 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   = sqlite3VdbeMe
1ee5a 6d 46 69 6e 61 6c 69 7a 65 28 75 2e 63 64 2e 70  mFinalize(u.cd.p
1ee5b 4d 65 6d 2c 20 70 4f 70 2d 3e 70 34 2e 70 46 75  Mem, pOp->p4.pFu
1ee5c 6e 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b  nc);.  if( rc ){
1ee5d 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
1ee5e 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
1ee5f 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c  g, db, "%s", sql
1ee60 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
1ee61 75 2e 63 64 2e 70 4d 65 6d 29 29 3b 0a 20 20 7d  u.cd.pMem));.  }
1ee62 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
1ee63 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 75 2e 63  angeEncoding(u.c
1ee64 64 2e 70 4d 65 6d 2c 20 65 6e 63 6f 64 69 6e 67  d.pMem, encoding
1ee65 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f  );.  UPDATE_MAX_
1ee66 42 4c 4f 42 53 49 5a 45 28 75 2e 63 64 2e 70 4d  BLOBSIZE(u.cd.pM
1ee67 65 6d 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  em);.  if( sqlit
1ee68 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28  e3VdbeMemTooBig(
1ee69 75 2e 63 64 2e 70 4d 65 6d 29 20 29 7b 0a 20 20  u.cd.pMem) ){.  
1ee6a 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
1ee6b 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1ee6c 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
1ee6d 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d  LITE_OMIT_VACUUM
1ee6e 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
1ee6f 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48  LITE_OMIT_ATTACH
1ee70 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 63  )./* Opcode: Vac
1ee71 75 75 6d 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  uum * * * * *.**
1ee72 0a 2a 2a 20 56 61 63 75 75 6d 20 74 68 65 20 65  .** Vacuum the e
1ee73 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 2e 20  ntire database. 
1ee74 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 69 6c   This opcode wil
1ee75 6c 20 63 61 75 73 65 20 6f 74 68 65 72 20 76 69  l cause other vi
1ee76 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65  rtual.** machine
1ee77 73 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20  s to be created 
1ee78 61 6e 64 20 72 75 6e 2e 20 20 49 74 20 6d 61 79  and run.  It may
1ee79 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 66   not be called f
1ee7a 72 6f 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 61 20  rom within.** a 
1ee7b 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a  transaction..*/.
1ee7c 63 61 73 65 20 4f 50 5f 56 61 63 75 75 6d 3a 20  case OP_Vacuum: 
1ee7d 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  {.  if( sqlite3S
1ee7e 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67  afetyOff(db) ) g
1ee7f 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1ee80 5f 6d 69 73 75 73 65 3b 20 0a 20 20 72 63 20 3d  _misuse; .  rc =
1ee81 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75 75   sqlite3RunVacuu
1ee82 6d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  m(&p->zErrMsg, d
1ee83 62 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  b);.  if( sqlite
1ee84 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20  3SafetyOn(db) ) 
1ee85 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1ee86 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 62 72 65 61  o_misuse;.  brea
1ee87 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  k;.}.#endif..#if
1ee88 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1ee89 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
1ee8a 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 63  )./* Opcode: Inc
1ee8b 72 56 61 63 75 75 6d 20 50 31 20 50 32 20 2a 20  rVacuum P1 P2 * 
1ee8c 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 65 72 66 6f 72  * *.**.** Perfor
1ee8d 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70 20  m a single step 
1ee8e 6f 66 20 74 68 65 20 69 6e 63 72 65 6d 65 6e 74  of the increment
1ee8f 61 6c 20 76 61 63 75 75 6d 20 70 72 6f 63 65 64  al vacuum proced
1ee90 75 72 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 50 31  ure on.** the P1
1ee91 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74 68   database. If th
1ee92 65 20 76 61 63 75 75 6d 20 68 61 73 20 66 69 6e  e vacuum has fin
1ee93 69 73 68 65 64 2c 20 6a 75 6d 70 20 74 6f 20 69  ished, jump to i
1ee94 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 50 32  nstruction.** P2
1ee95 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c  . Otherwise, fal
1ee96 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65  l through to the
1ee97 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
1ee98 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e  n..*/.case OP_In
1ee99 63 72 56 61 63 75 75 6d 3a 20 7b 20 20 20 20 20  crVacuum: {     
1ee9a 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69     /* jump */.#i
1ee9b 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1ee9c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1ee9d 74 6f 20 75 2e 63 65 20 2a 2f 0a 20 20 42 74 72  to u.ce */.  Btr
1ee9e 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66 20  ee *pBt;.#endif 
1ee9f 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1eea0 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1eea1 63 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  ce */..  assert(
1eea2 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
1eea3 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29  Op->p1<db->nDb )
1eea4 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e  ;.  assert( (p->
1eea5 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c  btreeMask & (1<<
1eea6 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a  pOp->p1))!=0 );.
1eea7 20 20 75 2e 63 65 2e 70 42 74 20 3d 20 64 62 2d    u.ce.pBt = db-
1eea8 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42  >aDb[pOp->p1].pB
1eea9 74 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  t;.  rc = sqlite
1eeaa 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d  3BtreeIncrVacuum
1eeab 28 75 2e 63 65 2e 70 42 74 29 3b 0a 20 20 69 66  (u.ce.pBt);.  if
1eeac 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
1eead 45 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f  E ){.    pc = pO
1eeae 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 72  p->p2 - 1;.    r
1eeaf 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1eeb0 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65   }.  break;.}.#e
1eeb1 6e 64 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  ndif../* Opcode:
1eeb2 20 45 78 70 69 72 65 20 50 31 20 2a 20 2a 20 2a   Expire P1 * * *
1eeb3 20 2a 0a 2a 2a 0a 2a 2a 20 43 61 75 73 65 20 70   *.**.** Cause p
1eeb4 72 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65  recompiled state
1eeb5 6d 65 6e 74 73 20 74 6f 20 62 65 63 6f 6d 65 20  ments to become 
1eeb6 65 78 70 69 72 65 64 2e 20 41 6e 20 65 78 70 69  expired. An expi
1eeb7 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  red statement.**
1eeb8 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 20 65   fails with an e
1eeb9 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 53 51 4c  rror code of SQL
1eeba 49 54 45 5f 53 43 48 45 4d 41 20 69 66 20 69 74  ITE_SCHEMA if it
1eebb 20 69 73 20 65 76 65 72 20 65 78 65 63 75 74 65   is ever execute
1eebc 64 20 0a 2a 2a 20 28 76 69 61 20 73 71 6c 69 74  d .** (via sqlit
1eebd 65 33 5f 73 74 65 70 28 29 29 2e 0a 2a 2a 20 0a  e3_step())..** .
1eebe 2a 2a 20 49 66 20 50 31 20 69 73 20 30 2c 20 74  ** If P1 is 0, t
1eebf 68 65 6e 20 61 6c 6c 20 53 51 4c 20 73 74 61 74  hen all SQL stat
1eec0 65 6d 65 6e 74 73 20 62 65 63 6f 6d 65 20 65 78  ements become ex
1eec1 70 69 72 65 64 2e 20 49 66 20 50 31 20 69 73 20  pired. If P1 is 
1eec2 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65  non-zero,.** the
1eec3 6e 20 6f 6e 6c 79 20 74 68 65 20 63 75 72 72 65  n only the curre
1eec4 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 73  ntly executing s
1eec5 74 61 74 65 6d 65 6e 74 20 69 73 20 61 66 66 65  tatement is affe
1eec6 63 74 65 64 2e 20 0a 2a 2f 0a 63 61 73 65 20 4f  cted. .*/.case O
1eec7 50 5f 45 78 70 69 72 65 3a 20 7b 0a 20 20 69 66  P_Expire: {.  if
1eec8 28 20 21 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20  ( !pOp->p1 ){.  
1eec9 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50    sqlite3ExpireP
1eeca 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74  reparedStatement
1eecb 73 28 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  s(db);.  }else{.
1eecc 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d      p->expired =
1eecd 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   1;.  }.  break;
1eece 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
1eecf 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
1eed0 41 43 48 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ACHE./* Opcode: 
1eed1 54 61 62 6c 65 4c 6f 63 6b 20 50 31 20 50 32 20  TableLock P1 P2 
1eed2 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 62  P3 P4 *.**.** Ob
1eed3 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 61  tain a lock on a
1eed4 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c   particular tabl
1eed5 65 2e 20 54 68 69 73 20 69 6e 73 74 72 75 63 74  e. This instruct
1eed6 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64  ion is only used
1eed7 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 73 68 61   when.** the sha
1eed8 72 65 64 2d 63 61 63 68 65 20 66 65 61 74 75 72  red-cache featur
1eed9 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 0a 2a  e is enabled. .*
1eeda 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20 69  *.** P1 is the i
1eedb 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
1eedc 62 61 73 65 20 69 6e 20 73 71 6c 69 74 65 33 2e  base in sqlite3.
1eedd 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 74  aDb[] of the dat
1eede 61 62 61 73 65 0a 2a 2a 20 6f 6e 20 77 68 69 63  abase.** on whic
1eedf 68 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 63  h the lock is ac
1eee0 71 75 69 72 65 64 2e 20 20 41 20 72 65 61 64 6c  quired.  A readl
1eee1 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20  ock is obtained 
1eee2 69 66 20 50 33 3d 3d 30 20 6f 72 0a 2a 2a 20 61  if P3==0 or.** a
1eee3 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 50   write lock if P
1eee4 33 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 63  3==1..**.** P2 c
1eee5 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f 6f 74  ontains the root
1eee6 2d 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 62  -page of the tab
1eee7 6c 65 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a  le to lock..**.*
1eee8 2a 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61 20  * P4 contains a 
1eee9 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
1eeea 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
1eeeb 20 62 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 20 54   being locked. T
1eeec 68 69 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 75  his is only.** u
1eeed 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20  sed to generate 
1eeee 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
1eeef 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e   if the lock can
1eef0 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e  not be obtained.
1eef1 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 61 62 6c  .*/.case OP_Tabl
1eef2 65 4c 6f 63 6b 3a 20 7b 0a 20 20 75 38 20 69 73  eLock: {.  u8 is
1eef3 57 72 69 74 65 4c 6f 63 6b 20 3d 20 28 75 38 29  WriteLock = (u8)
1eef4 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 66 28 20 69  pOp->p3;.  if( i
1eef5 73 57 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 30 3d  sWriteLock || 0=
1eef6 3d 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49  =(db->flags&SQLI
1eef7 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74  TE_ReadUncommitt
1eef8 65 64 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 70  ed) ){.    int p
1eef9 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a 20 20  1 = pOp->p1; .  
1eefa 20 20 61 73 73 65 72 74 28 20 70 31 3e 3d 30 20    assert( p1>=0 
1eefb 26 26 20 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b  && p1<db->nDb );
1eefc 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d  .    assert( (p-
1eefd 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c  >btreeMask & (1<
1eefe 3c 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 20 20  <p1))!=0 );.    
1eeff 61 73 73 65 72 74 28 20 69 73 57 72 69 74 65 4c  assert( isWriteL
1ef00 6f 63 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69 74  ock==0 || isWrit
1ef01 65 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20 20  eLock==1 );.    
1ef02 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1ef03 65 4c 6f 63 6b 54 61 62 6c 65 28 64 62 2d 3e 61  eLockTable(db->a
1ef04 44 62 5b 70 31 5d 2e 70 42 74 2c 20 70 4f 70 2d  Db[p1].pBt, pOp-
1ef05 3e 70 32 2c 20 69 73 57 72 69 74 65 4c 6f 63 6b  >p2, isWriteLock
1ef06 29 3b 0a 20 20 20 20 69 66 28 20 28 72 63 26 30  );.    if( (rc&0
1ef07 78 46 46 29 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43  xFF)==SQLITE_LOC
1ef08 4b 45 44 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e  KED ){.      con
1ef09 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f 70  st char *z = pOp
1ef0a 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 73 71  ->p4.z;.      sq
1ef0b 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1ef0c 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
1ef0d 22 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20  "database table 
1ef0e 69 73 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c 20  is locked: %s", 
1ef0f 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  z);.    }.  }.  
1ef10 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
1ef11 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  /* SQLITE_OMIT_S
1ef12 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a  HARED_CACHE */..
1ef13 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1ef14 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1ef15 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 42 65 67  ./* Opcode: VBeg
1ef16 69 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a  in * * * P4 *.**
1ef17 0a 2a 2a 20 50 34 20 6d 61 79 20 62 65 20 61 20  .** P4 may be a 
1ef18 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71  pointer to an sq
1ef19 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63  lite3_vtab struc
1ef1a 74 75 72 65 2e 20 49 66 20 73 6f 2c 20 63 61 6c  ture. If so, cal
1ef1b 6c 20 74 68 65 20 0a 2a 2a 20 78 42 65 67 69 6e  l the .** xBegin
1ef1c 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61 74   method for that
1ef1d 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c   table..**.** Al
1ef1e 73 6f 2c 20 77 68 65 74 68 65 72 20 6f 72 20 6e  so, whether or n
1ef1f 6f 74 20 50 34 20 69 73 20 73 65 74 2c 20 63 68  ot P4 is set, ch
1ef20 65 63 6b 20 74 68 61 74 20 74 68 69 73 20 69 73  eck that this is
1ef21 20 6e 6f 74 20 62 65 69 6e 67 20 63 61 6c 6c 65   not being calle
1ef22 64 20 66 72 6f 6d 0a 2a 2a 20 77 69 74 68 69 6e  d from.** within
1ef23 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 61   a callback to a
1ef24 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 78   virtual table x
1ef25 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 2e 20 49  Sync() method. I
1ef26 66 20 69 74 20 69 73 2c 20 74 68 65 20 65 72 72  f it is, the err
1ef27 6f 72 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c 20  or.** code will 
1ef28 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45  be set to SQLITE
1ef29 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 63 61 73 65  _LOCKED..*/.case
1ef2a 20 4f 50 5f 56 42 65 67 69 6e 3a 20 7b 0a 23 69   OP_VBegin: {.#i
1ef2b 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1ef2c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1ef2d 74 6f 20 75 2e 63 66 20 2a 2f 0a 20 20 56 54 61  to u.cf */.  VTa
1ef2e 62 6c 65 20 2a 70 56 54 61 62 3b 0a 23 65 6e 64  ble *pVTab;.#end
1ef2f 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1ef30 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1ef31 20 75 2e 63 66 20 2a 2f 0a 20 20 75 2e 63 66 2e   u.cf */.  u.cf.
1ef32 70 56 54 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e  pVTab = pOp->p4.
1ef33 70 56 74 61 62 3b 0a 20 20 72 63 20 3d 20 73 71  pVtab;.  rc = sq
1ef34 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 28 64  lite3VtabBegin(d
1ef35 62 2c 20 75 2e 63 66 2e 70 56 54 61 62 29 3b 0a  b, u.cf.pVTab);.
1ef36 20 20 69 66 28 20 75 2e 63 66 2e 70 56 54 61 62    if( u.cf.pVTab
1ef37 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
1ef38 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72  bFree(db, p->zEr
1ef39 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45  rMsg);.    p->zE
1ef3a 72 72 4d 73 67 20 3d 20 75 2e 63 66 2e 70 56 54  rrMsg = u.cf.pVT
1ef3b 61 62 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72 4d  ab->pVtab->zErrM
1ef3c 73 67 3b 0a 20 20 20 20 75 2e 63 66 2e 70 56 54  sg;.    u.cf.pVT
1ef3d 61 62 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72 4d  ab->pVtab->zErrM
1ef3e 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 72  sg = 0;.  }.  br
1ef3f 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1ef40 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1ef41 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69  TUALTABLE */..#i
1ef42 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1ef43 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f  T_VIRTUALTABLE./
1ef44 2a 20 4f 70 63 6f 64 65 3a 20 56 43 72 65 61 74  * Opcode: VCreat
1ef45 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a  e P1 * * P4 *.**
1ef46 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e 61  .** P4 is the na
1ef47 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20  me of a virtual 
1ef48 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61 73  table in databas
1ef49 65 20 50 31 2e 20 43 61 6c 6c 20 74 68 65 20 78  e P1. Call the x
1ef4a 43 72 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a 2a  Create method.**
1ef4b 20 66 6f 72 20 74 68 61 74 20 74 61 62 6c 65 2e   for that table.
1ef4c 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 43 72 65  .*/.case OP_VCre
1ef4d 61 74 65 3a 20 7b 0a 20 20 72 63 20 3d 20 73 71  ate: {.  rc = sq
1ef4e 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 72 65  lite3VtabCallCre
1ef4f 61 74 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c  ate(db, pOp->p1,
1ef50 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 26 70 2d 3e   pOp->p4.z, &p->
1ef51 7a 45 72 72 4d 73 67 29 3b 0a 20 20 62 72 65 61  zErrMsg);.  brea
1ef52 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  k;.}.#endif /* S
1ef53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1ef54 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e  ALTABLE */..#ifn
1ef55 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1ef56 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20  VIRTUALTABLE./* 
1ef57 4f 70 63 6f 64 65 3a 20 56 44 65 73 74 72 6f 79  Opcode: VDestroy
1ef58 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a   P1 * * P4 *.**.
1ef59 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e 61 6d  ** P4 is the nam
1ef5a 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74  e of a virtual t
1ef5b 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61 73 65  able in database
1ef5c 20 50 31 2e 20 20 43 61 6c 6c 20 74 68 65 20 78   P1.  Call the x
1ef5d 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 0a 2a  Destroy method.*
1ef5e 2a 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 2e  * of that table.
1ef5f 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 44 65 73  .*/.case OP_VDes
1ef60 74 72 6f 79 3a 20 7b 0a 20 20 70 2d 3e 69 6e 56  troy: {.  p->inV
1ef61 74 61 62 4d 65 74 68 6f 64 20 3d 20 32 3b 0a 20  tabMethod = 2;. 
1ef62 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61   rc = sqlite3Vta
1ef63 62 43 61 6c 6c 44 65 73 74 72 6f 79 28 64 62 2c  bCallDestroy(db,
1ef64 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70   pOp->p1, pOp->p
1ef65 34 2e 7a 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 61  4.z);.  p->inVta
1ef66 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 62  bMethod = 0;.  b
1ef67 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  reak;.}.#endif /
1ef68 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
1ef69 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23  RTUALTABLE */..#
1ef6a 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1ef6b 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
1ef6c 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4f 70 65 6e  /* Opcode: VOpen
1ef6d 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a   P1 * * P4 *.**.
1ef6e 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  ** P4 is a point
1ef6f 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20  er to a virtual 
1ef70 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e  table object, an
1ef71 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74   sqlite3_vtab st
1ef72 72 75 63 74 75 72 65 2e 0a 2a 2a 20 50 31 20 69  ructure..** P1 i
1ef73 73 20 61 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  s a cursor numbe
1ef74 72 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20  r.  This opcode 
1ef75 6f 70 65 6e 73 20 61 20 63 75 72 73 6f 72 20 74  opens a cursor t
1ef76 6f 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a  o the virtual.**
1ef77 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65   table and store
1ef78 73 20 74 68 61 74 20 63 75 72 73 6f 72 20 69 6e  s that cursor in
1ef79 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f   P1..*/.case OP_
1ef7a 56 4f 70 65 6e 3a 20 7b 0a 23 69 66 20 30 20 20  VOpen: {.#if 0  
1ef7b 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1ef7c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1ef7d 63 67 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73  cg */.  VdbeCurs
1ef7e 6f 72 20 2a 70 43 75 72 3b 0a 20 20 73 71 6c 69  or *pCur;.  sqli
1ef7f 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
1ef80 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20  *pVtabCursor;.  
1ef81 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
1ef82 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  tab;.  sqlite3_m
1ef83 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a  odule *pModule;.
1ef84 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1ef85 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1ef86 69 6e 74 6f 20 75 2e 63 67 20 2a 2f 0a 0a 20 20  into u.cg */..  
1ef87 75 2e 63 67 2e 70 43 75 72 20 3d 20 30 3b 0a 20  u.cg.pCur = 0;. 
1ef88 20 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73 6f   u.cg.pVtabCurso
1ef89 72 20 3d 20 30 3b 0a 20 20 75 2e 63 67 2e 70 56  r = 0;.  u.cg.pV
1ef8a 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56  tab = pOp->p4.pV
1ef8b 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 75 2e  tab->pVtab;.  u.
1ef8c 63 67 2e 70 4d 6f 64 75 6c 65 20 3d 20 28 73 71  cg.pModule = (sq
1ef8d 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 75  lite3_module *)u
1ef8e 2e 63 67 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75  .cg.pVtab->pModu
1ef8f 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 75 2e 63  le;.  assert(u.c
1ef90 67 2e 70 56 74 61 62 20 26 26 20 75 2e 63 67 2e  g.pVtab && u.cg.
1ef91 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 69 66 28 20  pModule);.  if( 
1ef92 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
1ef93 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72  (db) ) goto abor
1ef94 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
1ef95 0a 20 20 72 63 20 3d 20 75 2e 63 67 2e 70 4d 6f  .  rc = u.cg.pMo
1ef96 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 75 2e 63 67  dule->xOpen(u.cg
1ef97 2e 70 56 74 61 62 2c 20 26 75 2e 63 67 2e 70 56  .pVtab, &u.cg.pV
1ef98 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 73 71  tabCursor);.  sq
1ef99 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1ef9a 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70  p->zErrMsg);.  p
1ef9b 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 67  ->zErrMsg = u.cg
1ef9c 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b  .pVtab->zErrMsg;
1ef9d 0a 20 20 75 2e 63 67 2e 70 56 74 61 62 2d 3e 7a  .  u.cg.pVtab->z
1ef9e 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 66  ErrMsg = 0;.  if
1ef9f 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1efa0 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  n(db) ) goto abo
1efa1 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1efa2 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ;.  if( SQLITE_O
1efa3 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 2f 2a 20  K==rc ){.    /* 
1efa4 49 6e 69 74 69 61 6c 69 7a 65 20 73 71 6c 69 74  Initialize sqlit
1efa5 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 62  e3_vtab_cursor b
1efa6 61 73 65 20 63 6c 61 73 73 20 2a 2f 0a 20 20 20  ase class */.   
1efa7 20 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73 6f   u.cg.pVtabCurso
1efa8 72 2d 3e 70 56 74 61 62 20 3d 20 75 2e 63 67 2e  r->pVtab = u.cg.
1efa9 70 56 74 61 62 3b 0a 0a 20 20 20 20 2f 2a 20 49  pVtab;..    /* I
1efaa 6e 69 74 69 61 6c 69 73 65 20 76 64 62 65 20 63  nitialise vdbe c
1efab 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0a  ursor object */.
1efac 20 20 20 20 75 2e 63 67 2e 70 43 75 72 20 3d 20      u.cg.pCur = 
1efad 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70  allocateCursor(p
1efae 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 2d 31  , pOp->p1, 0, -1
1efaf 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 75 2e  , 0);.    if( u.
1efb0 63 67 2e 70 43 75 72 20 29 7b 0a 20 20 20 20 20  cg.pCur ){.     
1efb1 20 75 2e 63 67 2e 70 43 75 72 2d 3e 70 56 74 61   u.cg.pCur->pVta
1efb2 62 43 75 72 73 6f 72 20 3d 20 75 2e 63 67 2e 70  bCursor = u.cg.p
1efb3 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20  VtabCursor;.    
1efb4 20 20 75 2e 63 67 2e 70 43 75 72 2d 3e 70 4d 6f    u.cg.pCur->pMo
1efb5 64 75 6c 65 20 3d 20 75 2e 63 67 2e 70 56 74 61  dule = u.cg.pVta
1efb6 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 2d 3e  bCursor->pVtab->
1efb7 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 65 6c  pModule;.    }el
1efb8 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61  se{.      db->ma
1efb9 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
1efba 20 20 20 20 20 20 75 2e 63 67 2e 70 4d 6f 64 75        u.cg.pModu
1efbb 6c 65 2d 3e 78 43 6c 6f 73 65 28 75 2e 63 67 2e  le->xClose(u.cg.
1efbc 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20  pVtabCursor);.  
1efbd 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b    }.  }.  break;
1efbe 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1efbf 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1efc0 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65  TABLE */..#ifnde
1efc1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
1efc2 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70  RTUALTABLE./* Op
1efc3 63 6f 64 65 3a 20 56 46 69 6c 74 65 72 20 50 31  code: VFilter P1
1efc4 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a   P2 P3 P4 *.**.*
1efc5 2a 20 50 31 20 69 73 20 61 20 63 75 72 73 6f 72  * P1 is a cursor
1efc6 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 56 4f   opened using VO
1efc7 70 65 6e 2e 20 20 50 32 20 69 73 20 61 6e 20 61  pen.  P2 is an a
1efc8 64 64 72 65 73 73 20 74 6f 20 6a 75 6d 70 20 74  ddress to jump t
1efc9 6f 20 69 66 0a 2a 2a 20 74 68 65 20 66 69 6c 74  o if.** the filt
1efca 65 72 65 64 20 72 65 73 75 6c 74 20 73 65 74 20  ered result set 
1efcb 69 73 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20  is empty..**.** 
1efcc 50 34 20 69 73 20 65 69 74 68 65 72 20 4e 55 4c  P4 is either NUL
1efcd 4c 20 6f 72 20 61 20 73 74 72 69 6e 67 20 74 68  L or a string th
1efce 61 74 20 77 61 73 20 67 65 6e 65 72 61 74 65 64  at was generated
1efcf 20 62 79 20 74 68 65 20 78 42 65 73 74 49 6e 64   by the xBestInd
1efd0 65 78 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20  ex.** method of 
1efd1 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 65  the module.  The
1efd2 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20   interpretation 
1efd3 6f 66 20 74 68 65 20 50 34 20 73 74 72 69 6e 67  of the P4 string
1efd4 20 69 73 20 6c 65 66 74 0a 2a 2a 20 74 6f 20 74   is left.** to t
1efd5 68 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d  he module implem
1efd6 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  entation..**.** 
1efd7 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f  This opcode invo
1efd8 6b 65 73 20 74 68 65 20 78 46 69 6c 74 65 72 20  kes the xFilter 
1efd9 6d 65 74 68 6f 64 20 6f 6e 20 74 68 65 20 76 69  method on the vi
1efda 72 74 75 61 6c 20 74 61 62 6c 65 20 73 70 65 63  rtual table spec
1efdb 69 66 69 65 64 0a 2a 2a 20 62 79 20 50 31 2e 20  ified.** by P1. 
1efdc 20 54 68 65 20 69 6e 74 65 67 65 72 20 71 75 65   The integer que
1efdd 72 79 20 70 6c 61 6e 20 70 61 72 61 6d 65 74 65  ry plan paramete
1efde 72 20 74 6f 20 78 46 69 6c 74 65 72 20 69 73 20  r to xFilter is 
1efdf 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74  stored in regist
1efe0 65 72 0a 2a 2a 20 50 33 2e 20 52 65 67 69 73 74  er.** P3. Regist
1efe1 65 72 20 50 33 2b 31 20 73 74 6f 72 65 73 20 74  er P3+1 stores t
1efe2 68 65 20 61 72 67 63 20 70 61 72 61 6d 65 74 65  he argc paramete
1efe3 72 20 74 6f 20 62 65 20 70 61 73 73 65 64 20 74  r to be passed t
1efe4 6f 20 74 68 65 0a 2a 2a 20 78 46 69 6c 74 65 72  o the.** xFilter
1efe5 20 6d 65 74 68 6f 64 2e 20 52 65 67 69 73 74 65   method. Registe
1efe6 72 73 20 50 33 2b 32 2e 2e 50 33 2b 31 2b 61 72  rs P3+2..P3+1+ar
1efe7 67 63 20 61 72 65 20 74 68 65 20 61 72 67 63 0a  gc are the argc.
1efe8 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 70 61  ** additional pa
1efe9 72 61 6d 65 74 65 72 73 20 77 68 69 63 68 20 61  rameters which a
1efea 72 65 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20  re passed to.** 
1efeb 78 46 69 6c 74 65 72 20 61 73 20 61 72 67 76 2e  xFilter as argv.
1efec 20 52 65 67 69 73 74 65 72 20 50 33 2b 32 20 62   Register P3+2 b
1efed 65 63 6f 6d 65 73 20 61 72 67 76 5b 30 5d 20 77  ecomes argv[0] w
1efee 68 65 6e 20 70 61 73 73 65 64 20 74 6f 20 78 46  hen passed to xF
1efef 69 6c 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a  ilter..**.** A j
1eff0 75 6d 70 20 69 73 20 6d 61 64 65 20 74 6f 20 50  ump is made to P
1eff1 32 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20  2 if the result 
1eff2 73 65 74 20 61 66 74 65 72 20 66 69 6c 74 65 72  set after filter
1eff3 69 6e 67 20 77 6f 75 6c 64 20 62 65 20 65 6d 70  ing would be emp
1eff4 74 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56  ty..*/.case OP_V
1eff5 46 69 6c 74 65 72 3a 20 7b 20 20 20 2f 2a 20 6a  Filter: {   /* j
1eff6 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a  ump */.#if 0  /*
1eff7 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1eff8 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 68   moved into u.ch
1eff9 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 3b 0a   */.  int nArg;.
1effa 20 20 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 20    int iQuery;.  
1effb 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
1effc 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20  dule *pModule;. 
1effd 20 4d 65 6d 20 2a 70 51 75 65 72 79 3b 0a 20 20   Mem *pQuery;.  
1effe 4d 65 6d 20 2a 70 41 72 67 63 3b 0a 20 20 73 71  Mem *pArgc;.  sq
1efff 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
1f000 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a  r *pVtabCursor;.
1f001 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
1f002 70 56 74 61 62 3b 0a 20 20 56 64 62 65 43 75 72  pVtab;.  VdbeCur
1f003 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e 74  sor *pCur;.  int
1f004 20 72 65 73 3b 0a 20 20 69 6e 74 20 69 3b 0a 20   res;.  int i;. 
1f005 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 23 65   Mem **apArg;.#e
1f006 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1f007 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1f008 74 6f 20 75 2e 63 68 20 2a 2f 0a 0a 20 20 75 2e  to u.ch */..  u.
1f009 63 68 2e 70 51 75 65 72 79 20 3d 20 26 70 2d 3e  ch.pQuery = &p->
1f00a 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20  aMem[pOp->p3];. 
1f00b 20 75 2e 63 68 2e 70 41 72 67 63 20 3d 20 26 75   u.ch.pArgc = &u
1f00c 2e 63 68 2e 70 51 75 65 72 79 5b 31 5d 3b 0a 20  .ch.pQuery[1];. 
1f00d 20 75 2e 63 68 2e 70 43 75 72 20 3d 20 70 2d 3e   u.ch.pCur = p->
1f00e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  apCsr[pOp->p1];.
1f00f 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1f010 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 63 68 2e 70  (pOp->p3, u.ch.p
1f011 51 75 65 72 79 29 3b 0a 20 20 61 73 73 65 72 74  Query);.  assert
1f012 28 20 75 2e 63 68 2e 70 43 75 72 2d 3e 70 56 74  ( u.ch.pCur->pVt
1f013 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e  abCursor );.  u.
1f014 63 68 2e 70 56 74 61 62 43 75 72 73 6f 72 20 3d  ch.pVtabCursor =
1f015 20 75 2e 63 68 2e 70 43 75 72 2d 3e 70 56 74 61   u.ch.pCur->pVta
1f016 62 43 75 72 73 6f 72 3b 0a 20 20 75 2e 63 68 2e  bCursor;.  u.ch.
1f017 70 56 74 61 62 20 3d 20 75 2e 63 68 2e 70 56 74  pVtab = u.ch.pVt
1f018 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b  abCursor->pVtab;
1f019 0a 20 20 75 2e 63 68 2e 70 4d 6f 64 75 6c 65 20  .  u.ch.pModule 
1f01a 3d 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e 70 4d  = u.ch.pVtab->pM
1f01b 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a 20 47 72 61  odule;..  /* Gra
1f01c 62 20 74 68 65 20 69 6e 64 65 78 20 6e 75 6d 62  b the index numb
1f01d 65 72 20 61 6e 64 20 61 72 67 63 20 70 61 72 61  er and argc para
1f01e 6d 65 74 65 72 73 20 2a 2f 0a 20 20 61 73 73 65  meters */.  asse
1f01f 72 74 28 20 28 75 2e 63 68 2e 70 51 75 65 72 79  rt( (u.ch.pQuery
1f020 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 29  ->flags&MEM_Int)
1f021 21 3d 30 20 26 26 20 75 2e 63 68 2e 70 41 72 67  !=0 && u.ch.pArg
1f022 63 2d 3e 66 6c 61 67 73 3d 3d 4d 45 4d 5f 49 6e  c->flags==MEM_In
1f023 74 20 29 3b 0a 20 20 75 2e 63 68 2e 6e 41 72 67  t );.  u.ch.nArg
1f024 20 3d 20 28 69 6e 74 29 75 2e 63 68 2e 70 41 72   = (int)u.ch.pAr
1f025 67 63 2d 3e 75 2e 69 3b 0a 20 20 75 2e 63 68 2e  gc->u.i;.  u.ch.
1f026 69 51 75 65 72 79 20 3d 20 28 69 6e 74 29 75 2e  iQuery = (int)u.
1f027 63 68 2e 70 51 75 65 72 79 2d 3e 75 2e 69 3b 0a  ch.pQuery->u.i;.
1f028 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65  .  /* Invoke the
1f029 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20   xFilter method 
1f02a 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 2e 63 68 2e  */.  {.    u.ch.
1f02b 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e 63  res = 0;.    u.c
1f02c 68 2e 61 70 41 72 67 20 3d 20 70 2d 3e 61 70 41  h.apArg = p->apA
1f02d 72 67 3b 0a 20 20 20 20 66 6f 72 28 75 2e 63 68  rg;.    for(u.ch
1f02e 2e 69 20 3d 20 30 3b 20 75 2e 63 68 2e 69 3c 75  .i = 0; u.ch.i<u
1f02f 2e 63 68 2e 6e 41 72 67 3b 20 75 2e 63 68 2e 69  .ch.nArg; u.ch.i
1f030 2b 2b 29 7b 0a 20 20 20 20 20 20 75 2e 63 68 2e  ++){.      u.ch.
1f031 61 70 41 72 67 5b 75 2e 63 68 2e 69 5d 20 3d 20  apArg[u.ch.i] = 
1f032 26 75 2e 63 68 2e 70 41 72 67 63 5b 75 2e 63 68  &u.ch.pArgc[u.ch
1f033 2e 69 2b 31 5d 3b 0a 20 20 20 20 20 20 73 74 6f  .i+1];.      sto
1f034 72 65 54 79 70 65 49 6e 66 6f 28 75 2e 63 68 2e  reTypeInfo(u.ch.
1f035 61 70 41 72 67 5b 75 2e 63 68 2e 69 5d 2c 20 30  apArg[u.ch.i], 0
1f036 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  );.    }..    if
1f037 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1f038 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  ff(db) ) goto ab
1f039 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1f03a 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74 61 62  e;.    p->inVtab
1f03b 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20 20 20  Method = 1;.    
1f03c 72 63 20 3d 20 75 2e 63 68 2e 70 4d 6f 64 75 6c  rc = u.ch.pModul
1f03d 65 2d 3e 78 46 69 6c 74 65 72 28 75 2e 63 68 2e  e->xFilter(u.ch.
1f03e 70 56 74 61 62 43 75 72 73 6f 72 2c 20 75 2e 63  pVtabCursor, u.c
1f03f 68 2e 69 51 75 65 72 79 2c 20 70 4f 70 2d 3e 70  h.iQuery, pOp->p
1f040 34 2e 7a 2c 20 75 2e 63 68 2e 6e 41 72 67 2c 20  4.z, u.ch.nArg, 
1f041 75 2e 63 68 2e 61 70 41 72 67 29 3b 0a 20 20 20  u.ch.apArg);.   
1f042 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64   p->inVtabMethod
1f043 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 0;.    sqlite
1f044 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
1f045 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e  ErrMsg);.    p->
1f046 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 68 2e 70  zErrMsg = u.ch.p
1f047 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20  Vtab->zErrMsg;. 
1f048 20 20 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e 7a     u.ch.pVtab->z
1f049 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20  ErrMsg = 0;.    
1f04a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1f04b 4b 20 29 7b 0a 20 20 20 20 20 20 75 2e 63 68 2e  K ){.      u.ch.
1f04c 72 65 73 20 3d 20 75 2e 63 68 2e 70 4d 6f 64 75  res = u.ch.pModu
1f04d 6c 65 2d 3e 78 45 6f 66 28 75 2e 63 68 2e 70 56  le->xEof(u.ch.pV
1f04e 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  tabCursor);.    
1f04f 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  }.    if( sqlite
1f050 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20  3SafetyOn(db) ) 
1f051 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1f052 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 20 20 69  o_misuse;..    i
1f053 66 28 20 75 2e 63 68 2e 72 65 73 20 29 7b 0a 20  f( u.ch.res ){. 
1f054 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70       pc = pOp->p
1f055 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  2 - 1;.    }.  }
1f056 0a 20 20 75 2e 63 68 2e 70 43 75 72 2d 3e 6e 75  .  u.ch.pCur->nu
1f057 6c 6c 52 6f 77 20 3d 20 30 3b 0a 0a 20 20 62 72  llRow = 0;..  br
1f058 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1f059 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1f05a 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69  TUALTABLE */..#i
1f05b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1f05c 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f  T_VIRTUALTABLE./
1f05d 2a 20 4f 70 63 6f 64 65 3a 20 56 43 6f 6c 75 6d  * Opcode: VColum
1f05e 6e 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  n P1 P2 P3 * *.*
1f05f 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 76  *.** Store the v
1f060 61 6c 75 65 20 6f 66 20 74 68 65 20 50 32 2d 74  alue of the P2-t
1f061 68 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20 74  h column of.** t
1f062 68 65 20 72 6f 77 20 6f 66 20 74 68 65 20 76 69  he row of the vi
1f063 72 74 75 61 6c 2d 74 61 62 6c 65 20 74 68 61 74  rtual-table that
1f064 20 74 68 65 20 0a 2a 2a 20 50 31 20 63 75 72 73   the .** P1 curs
1f065 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74  or is pointing t
1f066 6f 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  o into register 
1f067 50 33 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56  P3..*/.case OP_V
1f068 43 6f 6c 75 6d 6e 3a 20 7b 0a 23 69 66 20 30 20  Column: {.#if 0 
1f069 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1f06a 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1f06b 2e 63 69 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  .ci */.  sqlite3
1f06c 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20  _vtab *pVtab;.  
1f06d 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
1f06e 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20  dule *pModule;. 
1f06f 20 4d 65 6d 20 2a 70 44 65 73 74 3b 0a 20 20 73   Mem *pDest;.  s
1f070 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73  qlite3_context s
1f071 43 6f 6e 74 65 78 74 3b 0a 23 65 6e 64 69 66 20  Context;.#endif 
1f072 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1f073 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1f074 63 69 20 2a 2f 0a 0a 20 20 56 64 62 65 43 75 72  ci */..  VdbeCur
1f075 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61  sor *pCur = p->a
1f076 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  pCsr[pOp->p1];. 
1f077 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70   assert( pCur->p
1f078 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20  VtabCursor );.  
1f079 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e  assert( pOp->p3>
1f07a 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d  0 && pOp->p3<=p-
1f07b 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 63 69 2e  >nMem );.  u.ci.
1f07c 70 44 65 73 74 20 3d 20 26 70 2d 3e 61 4d 65 6d  pDest = &p->aMem
1f07d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28  [pOp->p3];.  if(
1f07e 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29   pCur->nullRow )
1f07f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1f080 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 75 2e 63 69  eMemSetNull(u.ci
1f081 2e 70 44 65 73 74 29 3b 0a 20 20 20 20 62 72 65  .pDest);.    bre
1f082 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e 63 69 2e 70  ak;.  }.  u.ci.p
1f083 56 74 61 62 20 3d 20 70 43 75 72 2d 3e 70 56 74  Vtab = pCur->pVt
1f084 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b  abCursor->pVtab;
1f085 0a 20 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 20  .  u.ci.pModule 
1f086 3d 20 75 2e 63 69 2e 70 56 74 61 62 2d 3e 70 4d  = u.ci.pVtab->pM
1f087 6f 64 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28  odule;.  assert(
1f088 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78   u.ci.pModule->x
1f089 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 6d 65 6d 73  Column );.  mems
1f08a 65 74 28 26 75 2e 63 69 2e 73 43 6f 6e 74 65 78  et(&u.ci.sContex
1f08b 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 2e 63  t, 0, sizeof(u.c
1f08c 69 2e 73 43 6f 6e 74 65 78 74 29 29 3b 0a 0a 20  i.sContext));.. 
1f08d 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 20 63   /* The output c
1f08e 65 6c 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 20  ell may already 
1f08f 68 61 76 65 20 61 20 62 75 66 66 65 72 20 61 6c  have a buffer al
1f090 6c 6f 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20  located. Move.  
1f091 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  ** the current c
1f092 6f 6e 74 65 6e 74 73 20 74 6f 20 75 2e 63 69 2e  ontents to u.ci.
1f093 73 43 6f 6e 74 65 78 74 2e 73 20 73 6f 20 69 6e  sContext.s so in
1f094 20 63 61 73 65 20 74 68 65 20 75 73 65 72 2d 66   case the user-f
1f095 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 63 61 6e  unction.  ** can
1f096 20 75 73 65 20 74 68 65 20 61 6c 72 65 61 64 79   use the already
1f097 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65   allocated buffe
1f098 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c  r instead of all
1f099 6f 63 61 74 69 6e 67 20 61 0a 20 20 2a 2a 20 6e  ocating a.  ** n
1f09a 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 73  ew one..  */.  s
1f09b 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76  qlite3VdbeMemMov
1f09c 65 28 26 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74  e(&u.ci.sContext
1f09d 2e 73 2c 20 75 2e 63 69 2e 70 44 65 73 74 29 3b  .s, u.ci.pDest);
1f09e 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  .  MemSetTypeFla
1f09f 67 28 26 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74  g(&u.ci.sContext
1f0a0 2e 73 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a  .s, MEM_Null);..
1f0a1 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
1f0a2 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74  etyOff(db) ) got
1f0a3 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1f0a4 69 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 75 2e  isuse;.  rc = u.
1f0a5 63 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6c  ci.pModule->xCol
1f0a6 75 6d 6e 28 70 43 75 72 2d 3e 70 56 74 61 62 43  umn(pCur->pVtabC
1f0a7 75 72 73 6f 72 2c 20 26 75 2e 63 69 2e 73 43 6f  ursor, &u.ci.sCo
1f0a8 6e 74 65 78 74 2c 20 70 4f 70 2d 3e 70 32 29 3b  ntext, pOp->p2);
1f0a9 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1f0aa 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29  (db, p->zErrMsg)
1f0ab 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d  ;.  p->zErrMsg =
1f0ac 20 75 2e 63 69 2e 70 56 74 61 62 2d 3e 7a 45 72   u.ci.pVtab->zEr
1f0ad 72 4d 73 67 3b 0a 20 20 75 2e 63 69 2e 70 56 74  rMsg;.  u.ci.pVt
1f0ae 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ab->zErrMsg = 0;
1f0af 0a 20 20 69 66 28 20 75 2e 63 69 2e 73 43 6f 6e  .  if( u.ci.sCon
1f0b0 74 65 78 74 2e 69 73 45 72 72 6f 72 20 29 7b 0a  text.isError ){.
1f0b1 20 20 20 20 72 63 20 3d 20 75 2e 63 69 2e 73 43      rc = u.ci.sC
1f0b2 6f 6e 74 65 78 74 2e 69 73 45 72 72 6f 72 3b 0a  ontext.isError;.
1f0b3 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74    }..  /* Copy t
1f0b4 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  he result of the
1f0b5 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 65   function to the
1f0b6 20 50 33 20 72 65 67 69 73 74 65 72 2e 20 57 65   P3 register. We
1f0b7 0a 20 20 2a 2a 20 64 6f 20 74 68 69 73 20 72 65  .  ** do this re
1f0b8 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74  gardless of whet
1f0b9 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72  her or not an er
1f0ba 72 6f 72 20 6f 63 63 75 72 72 65 64 20 74 6f 20  ror occurred to 
1f0bb 65 6e 73 75 72 65 20 61 6e 79 0a 20 20 2a 2a 20  ensure any.  ** 
1f0bc 64 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 61 74 69  dynamic allocati
1f0bd 6f 6e 20 69 6e 20 75 2e 63 69 2e 73 43 6f 6e 74  on in u.ci.sCont
1f0be 65 78 74 2e 73 20 28 61 20 4d 65 6d 20 73 74 72  ext.s (a Mem str
1f0bf 75 63 74 29 20 69 73 20 20 72 65 6c 65 61 73 65  uct) is  release
1f0c0 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  d..  */.  sqlite
1f0c1 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64  3VdbeChangeEncod
1f0c2 69 6e 67 28 26 75 2e 63 69 2e 73 43 6f 6e 74 65  ing(&u.ci.sConte
1f0c3 78 74 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b  xt.s, encoding);
1f0c4 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43  .  REGISTER_TRAC
1f0c5 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 63 69 2e  E(pOp->p3, u.ci.
1f0c6 70 44 65 73 74 29 3b 0a 20 20 73 71 6c 69 74 65  pDest);.  sqlite
1f0c7 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 75 2e 63  3VdbeMemMove(u.c
1f0c8 69 2e 70 44 65 73 74 2c 20 26 75 2e 63 69 2e 73  i.pDest, &u.ci.s
1f0c9 43 6f 6e 74 65 78 74 2e 73 29 3b 0a 20 20 55 50  Context.s);.  UP
1f0ca 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a  DATE_MAX_BLOBSIZ
1f0cb 45 28 75 2e 63 69 2e 70 44 65 73 74 29 3b 0a 0a  E(u.ci.pDest);..
1f0cc 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
1f0cd 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20  etyOn(db) ){.   
1f0ce 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1f0cf 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 7d 0a 20  to_misuse;.  }. 
1f0d0 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
1f0d1 4d 65 6d 54 6f 6f 42 69 67 28 75 2e 63 69 2e 70  MemTooBig(u.ci.p
1f0d2 44 65 73 74 29 20 29 7b 0a 20 20 20 20 67 6f 74  Dest) ){.    got
1f0d3 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20  o too_big;.  }. 
1f0d4 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1f0d5 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1f0d6 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a  VIRTUALTABLE */.
1f0d7 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1f0d8 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1f0d9 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4e 65  E./* Opcode: VNe
1f0da 78 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  xt P1 P2 * * *.*
1f0db 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 76 69 72  *.** Advance vir
1f0dc 74 75 61 6c 20 74 61 62 6c 65 20 50 31 20 74 6f  tual table P1 to
1f0dd 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 69 6e   the next row in
1f0de 20 69 74 73 20 72 65 73 75 6c 74 20 73 65 74 20   its result set 
1f0df 61 6e 64 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 69  and.** jump to i
1f0e0 6e 73 74 72 75 63 74 69 6f 6e 20 50 32 2e 20 20  nstruction P2.  
1f0e1 4f 72 2c 20 69 66 20 74 68 65 20 76 69 72 74 75  Or, if the virtu
1f0e2 61 6c 20 74 61 62 6c 65 20 68 61 73 20 72 65 61  al table has rea
1f0e3 63 68 65 64 0a 2a 2a 20 74 68 65 20 65 6e 64 20  ched.** the end 
1f0e4 6f 66 20 69 74 73 20 72 65 73 75 6c 74 20 73 65  of its result se
1f0e5 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72  t, then fall thr
1f0e6 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74  ough to the next
1f0e7 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f   instruction..*/
1f0e8 0a 63 61 73 65 20 4f 50 5f 56 4e 65 78 74 3a 20  .case OP_VNext: 
1f0e9 7b 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23  {   /* jump */.#
1f0ea 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1f0eb 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1f0ec 6e 74 6f 20 75 2e 63 6a 20 2a 2f 0a 20 20 73 71  nto u.cj */.  sq
1f0ed 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
1f0ee 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  b;.  const sqlit
1f0ef 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
1f0f0 6c 65 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20  le;.  int res;. 
1f0f1 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75   VdbeCursor *pCu
1f0f2 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  r;.#endif /* loc
1f0f3 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1f0f4 65 64 20 69 6e 74 6f 20 75 2e 63 6a 20 2a 2f 0a  ed into u.cj */.
1f0f5 0a 20 20 75 2e 63 6a 2e 72 65 73 20 3d 20 30 3b  .  u.cj.res = 0;
1f0f6 0a 20 20 75 2e 63 6a 2e 70 43 75 72 20 3d 20 70  .  u.cj.pCur = p
1f0f7 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d  ->apCsr[pOp->p1]
1f0f8 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63 6a  ;.  assert( u.cj
1f0f9 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73  .pCur->pVtabCurs
1f0fa 6f 72 20 29 3b 0a 20 20 69 66 28 20 75 2e 63 6a  or );.  if( u.cj
1f0fb 2e 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29  .pCur->nullRow )
1f0fc 7b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  {.    break;.  }
1f0fd 0a 20 20 75 2e 63 6a 2e 70 56 74 61 62 20 3d 20  .  u.cj.pVtab = 
1f0fe 75 2e 63 6a 2e 70 43 75 72 2d 3e 70 56 74 61 62  u.cj.pCur->pVtab
1f0ff 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20  Cursor->pVtab;. 
1f100 20 75 2e 63 6a 2e 70 4d 6f 64 75 6c 65 20 3d 20   u.cj.pModule = 
1f101 75 2e 63 6a 2e 70 56 74 61 62 2d 3e 70 4d 6f 64  u.cj.pVtab->pMod
1f102 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 75  ule;.  assert( u
1f103 2e 63 6a 2e 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65  .cj.pModule->xNe
1f104 78 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f  xt );..  /* Invo
1f105 6b 65 20 74 68 65 20 78 4e 65 78 74 28 29 20 6d  ke the xNext() m
1f106 65 74 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f 64  ethod of the mod
1f107 75 6c 65 2e 20 54 68 65 72 65 20 69 73 20 6e 6f  ule. There is no
1f108 20 77 61 79 20 66 6f 72 20 74 68 65 0a 20 20 2a   way for the.  *
1f109 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 69 6d 70  * underlying imp
1f10a 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 6f 20 72  lementation to r
1f10b 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69  eturn an error i
1f10c 66 20 6f 6e 65 20 6f 63 63 75 72 73 20 64 75 72  f one occurs dur
1f10d 69 6e 67 0a 20 20 2a 2a 20 78 4e 65 78 74 28 29  ing.  ** xNext()
1f10e 2e 20 49 6e 73 74 65 61 64 2c 20 69 66 20 61 6e  . Instead, if an
1f10f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74   error occurs, t
1f110 72 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  rue is returned 
1f111 28 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74  (indicating that
1f112 0a 20 20 2a 2a 20 64 61 74 61 20 69 73 20 61 76  .  ** data is av
1f113 61 69 6c 61 62 6c 65 29 20 61 6e 64 20 74 68 65  ailable) and the
1f114 20 65 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75   error code retu
1f115 72 6e 65 64 20 77 68 65 6e 20 78 43 6f 6c 75 6d  rned when xColum
1f116 6e 20 6f 72 0a 20 20 2a 2a 20 73 6f 6d 65 20 6f  n or.  ** some o
1f117 74 68 65 72 20 6d 65 74 68 6f 64 20 69 73 20 6e  ther method is n
1f118 65 78 74 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 74  ext invoked on t
1f119 68 65 20 73 61 76 65 20 76 69 72 74 75 61 6c 20  he save virtual 
1f11a 74 61 62 6c 65 20 63 75 72 73 6f 72 2e 0a 20 20  table cursor..  
1f11b 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
1f11c 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20  SafetyOff(db) ) 
1f11d 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1f11e 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 70 2d 3e 69  o_misuse;.  p->i
1f11f 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b  nVtabMethod = 1;
1f120 0a 20 20 72 63 20 3d 20 75 2e 63 6a 2e 70 4d 6f  .  rc = u.cj.pMo
1f121 64 75 6c 65 2d 3e 78 4e 65 78 74 28 75 2e 63 6a  dule->xNext(u.cj
1f122 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73  .pCur->pVtabCurs
1f123 6f 72 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 61 62  or);.  p->inVtab
1f124 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 73 71  Method = 0;.  sq
1f125 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1f126 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70  p->zErrMsg);.  p
1f127 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 6a  ->zErrMsg = u.cj
1f128 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b  .pVtab->zErrMsg;
1f129 0a 20 20 75 2e 63 6a 2e 70 56 74 61 62 2d 3e 7a  .  u.cj.pVtab->z
1f12a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 66  ErrMsg = 0;.  if
1f12b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1f12c 29 7b 0a 20 20 20 20 75 2e 63 6a 2e 72 65 73 20  ){.    u.cj.res 
1f12d 3d 20 75 2e 63 6a 2e 70 4d 6f 64 75 6c 65 2d 3e  = u.cj.pModule->
1f12e 78 45 6f 66 28 75 2e 63 6a 2e 70 43 75 72 2d 3e  xEof(u.cj.pCur->
1f12f 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20  pVtabCursor);.  
1f130 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  }.  if( sqlite3S
1f131 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f  afetyOn(db) ) go
1f132 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1f133 6d 69 73 75 73 65 3b 0a 0a 20 20 69 66 28 20 21  misuse;..  if( !
1f134 75 2e 63 6a 2e 72 65 73 20 29 7b 0a 20 20 20 20  u.cj.res ){.    
1f135 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 64  /* If there is d
1f136 61 74 61 2c 20 6a 75 6d 70 20 74 6f 20 50 32 20  ata, jump to P2 
1f137 2a 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  */.    pc = pOp-
1f138 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p2 - 1;.  }.  b
1f139 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  reak;.}.#endif /
1f13a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
1f13b 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23  RTUALTABLE */..#
1f13c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f13d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
1f13e 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 52 65 6e 61  /* Opcode: VRena
1f13f 6d 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a  me P1 * * P4 *.*
1f140 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69  *.** P4 is a poi
1f141 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 61  nter to a virtua
1f142 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20  l table object, 
1f143 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  an sqlite3_vtab 
1f144 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68  structure..** Th
1f145 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65  is opcode invoke
1f146 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  s the correspond
1f147 69 6e 67 20 78 52 65 6e 61 6d 65 20 6d 65 74 68  ing xRename meth
1f148 6f 64 2e 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a  od. The value.**
1f149 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1f14a 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65  is passed as the
1f14b 20 7a 4e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20   zName argument 
1f14c 74 6f 20 74 68 65 20 78 52 65 6e 61 6d 65 20 6d  to the xRename m
1f14d 65 74 68 6f 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f  ethod..*/.case O
1f14e 50 5f 56 52 65 6e 61 6d 65 3a 20 7b 0a 23 69 66  P_VRename: {.#if
1f14f 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1f150 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1f151 6f 20 75 2e 63 6b 20 2a 2f 0a 20 20 73 71 6c 69  o u.ck */.  sqli
1f152 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b  te3_vtab *pVtab;
1f153 0a 20 20 4d 65 6d 20 2a 70 4e 61 6d 65 3b 0a 23  .  Mem *pName;.#
1f154 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1f155 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1f156 6e 74 6f 20 75 2e 63 6b 20 2a 2f 0a 0a 20 20 75  nto u.ck */..  u
1f157 2e 63 6b 2e 70 56 74 61 62 20 3d 20 70 4f 70 2d  .ck.pVtab = pOp-
1f158 3e 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 61 62  >p4.pVtab->pVtab
1f159 3b 0a 20 20 75 2e 63 6b 2e 70 4e 61 6d 65 20 3d  ;.  u.ck.pName =
1f15a 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1f15b 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e  1];.  assert( u.
1f15c 63 6b 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c  ck.pVtab->pModul
1f15d 65 2d 3e 78 52 65 6e 61 6d 65 20 29 3b 0a 20 20  e->xRename );.  
1f15e 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1f15f 4f 70 2d 3e 70 31 2c 20 75 2e 63 6b 2e 70 4e 61  Op->p1, u.ck.pNa
1f160 6d 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75  me);.  assert( u
1f161 2e 63 6b 2e 70 4e 61 6d 65 2d 3e 66 6c 61 67 73  .ck.pName->flags
1f162 20 26 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20   & MEM_Str );.  
1f163 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
1f164 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20  yOff(db) ) goto 
1f165 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73  abort_due_to_mis
1f166 75 73 65 3b 0a 20 20 72 63 20 3d 20 75 2e 63 6b  use;.  rc = u.ck
1f167 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d  .pVtab->pModule-
1f168 3e 78 52 65 6e 61 6d 65 28 75 2e 63 6b 2e 70 56  >xRename(u.ck.pV
1f169 74 61 62 2c 20 75 2e 63 6b 2e 70 4e 61 6d 65 2d  tab, u.ck.pName-
1f16a 3e 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  >z);.  sqlite3Db
1f16b 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72  Free(db, p->zErr
1f16c 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d  Msg);.  p->zErrM
1f16d 73 67 20 3d 20 75 2e 63 6b 2e 70 56 74 61 62 2d  sg = u.ck.pVtab-
1f16e 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 75 2e 63 6b  >zErrMsg;.  u.ck
1f16f 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20  .pVtab->zErrMsg 
1f170 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  = 0;.  if( sqlit
1f171 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29  e3SafetyOn(db) )
1f172 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1f173 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 62 72  to_misuse;..  br
1f174 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  eak;.}.#endif..#
1f175 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f176 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
1f177 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 55 70 64 61  /* Opcode: VUpda
1f178 74 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a  te P1 P2 P3 P4 *
1f179 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 70  .**.** P4 is a p
1f17a 6f 69 6e 74 65 72 20 74 6f 20 61 20 76 69 72 74  ointer to a virt
1f17b 75 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74  ual table object
1f17c 2c 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74 61  , an sqlite3_vta
1f17d 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20  b structure..** 
1f17e 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f  This opcode invo
1f17f 6b 65 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f  kes the correspo
1f180 6e 64 69 6e 67 20 78 55 70 64 61 74 65 20 6d 65  nding xUpdate me
1f181 74 68 6f 64 2e 20 50 32 20 76 61 6c 75 65 73 0a  thod. P2 values.
1f182 2a 2a 20 61 72 65 20 63 6f 6e 74 69 67 75 6f 75  ** are contiguou
1f183 73 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 73  s memory cells s
1f184 74 61 72 74 69 6e 67 20 61 74 20 50 33 20 74 6f  tarting at P3 to
1f185 20 70 61 73 73 20 74 6f 20 74 68 65 20 78 55 70   pass to the xUp
1f186 64 61 74 65 20 0a 2a 2a 20 69 6e 76 6f 63 61 74  date .** invocat
1f187 69 6f 6e 2e 20 54 68 65 20 76 61 6c 75 65 20 69  ion. The value i
1f188 6e 20 72 65 67 69 73 74 65 72 20 28 50 33 2b 50  n register (P3+P
1f189 32 2d 31 29 20 63 6f 72 72 65 73 70 6f 6e 64 73  2-1) corresponds
1f18a 20 74 6f 20 74 68 65 20 0a 2a 2a 20 70 32 74 68   to the .** p2th
1f18b 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
1f18c 61 72 67 76 20 61 72 72 61 79 20 70 61 73 73 65  argv array passe
1f18d 64 20 74 6f 20 78 55 70 64 61 74 65 2e 0a 2a 2a  d to xUpdate..**
1f18e 0a 2a 2a 20 54 68 65 20 78 55 70 64 61 74 65 20  .** The xUpdate 
1f18f 6d 65 74 68 6f 64 20 77 69 6c 6c 20 64 6f 20 61  method will do a
1f190 20 44 45 4c 45 54 45 20 6f 72 20 61 6e 20 49 4e   DELETE or an IN
1f191 53 45 52 54 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a  SERT or both..**
1f192 20 54 68 65 20 61 72 67 76 5b 30 5d 20 65 6c 65   The argv[0] ele
1f193 6d 65 6e 74 20 28 77 68 69 63 68 20 63 6f 72 72  ment (which corr
1f194 65 73 70 6f 6e 64 73 20 74 6f 20 6d 65 6d 6f 72  esponds to memor
1f195 79 20 63 65 6c 6c 20 50 33 29 0a 2a 2a 20 69 73  y cell P3).** is
1f196 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 61 20   the rowid of a 
1f197 72 6f 77 20 74 6f 20 64 65 6c 65 74 65 2e 20 20  row to delete.  
1f198 49 66 20 61 72 67 76 5b 30 5d 20 69 73 20 4e 55  If argv[0] is NU
1f199 4c 4c 20 74 68 65 6e 20 6e 6f 20 0a 2a 2a 20 64  LL then no .** d
1f19a 65 6c 65 74 69 6f 6e 20 6f 63 63 75 72 73 2e 20  eletion occurs. 
1f19b 20 54 68 65 20 61 72 67 76 5b 31 5d 20 65 6c 65   The argv[1] ele
1f19c 6d 65 6e 74 20 69 73 20 74 68 65 20 72 6f 77 69  ment is the rowi
1f19d 64 20 6f 66 20 74 68 65 20 6e 65 77 20 0a 2a 2a  d of the new .**
1f19e 20 72 6f 77 2e 20 20 54 68 69 73 20 63 61 6e 20   row.  This can 
1f19f 62 65 20 4e 55 4c 4c 20 74 6f 20 68 61 76 65 20  be NULL to have 
1f1a0 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
1f1a1 65 20 73 65 6c 65 63 74 20 74 68 65 20 6e 65 77  e select the new
1f1a2 20 0a 2a 2a 20 72 6f 77 69 64 20 66 6f 72 20 69   .** rowid for i
1f1a3 74 73 65 6c 66 2e 20 20 54 68 65 20 73 75 62 73  tself.  The subs
1f1a4 65 71 75 65 6e 74 20 65 6c 65 6d 65 6e 74 73 20  equent elements 
1f1a5 69 6e 20 74 68 65 20 61 72 72 61 79 20 61 72 65  in the array are
1f1a6 20 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 73 20   .** the values 
1f1a7 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
1f1a8 65 20 6e 65 77 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a  e new row..**.**
1f1a9 20 49 66 20 50 32 3d 3d 31 20 74 68 65 6e 20 6e   If P2==1 then n
1f1aa 6f 20 69 6e 73 65 72 74 20 69 73 20 70 65 72 66  o insert is perf
1f1ab 6f 72 6d 65 64 2e 20 20 61 72 67 76 5b 30 5d 20  ormed.  argv[0] 
1f1ac 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 0a  is the rowid of.
1f1ad 2a 2a 20 61 20 72 6f 77 20 74 6f 20 64 65 6c 65  ** a row to dele
1f1ae 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20  te..**.** P1 is 
1f1af 61 20 62 6f 6f 6c 65 61 6e 20 66 6c 61 67 2e 20  a boolean flag. 
1f1b0 49 66 20 69 74 20 69 73 20 73 65 74 20 74 6f 20  If it is set to 
1f1b1 74 72 75 65 20 61 6e 64 20 74 68 65 20 78 55 70  true and the xUp
1f1b2 64 61 74 65 20 63 61 6c 6c 0a 2a 2a 20 69 73 20  date call.** is 
1f1b3 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65 6e  successful, then
1f1b4 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
1f1b5 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 6c  ned by sqlite3_l
1f1b6 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64  ast_insert_rowid
1f1b7 28 29 20 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f  () .** is set to
1f1b8 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
1f1b9 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20  e rowid for the 
1f1ba 72 6f 77 20 6a 75 73 74 20 69 6e 73 65 72 74 65  row just inserte
1f1bb 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 55  d..*/.case OP_VU
1f1bc 70 64 61 74 65 3a 20 7b 0a 23 69 66 20 30 20 20  pdate: {.#if 0  
1f1bd 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1f1be 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1f1bf 63 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  cl */.  sqlite3_
1f1c0 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 73  vtab *pVtab;.  s
1f1c1 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
1f1c2 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 6e 41  Module;.  int nA
1f1c3 72 67 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  rg;.  int i;.  s
1f1c4 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 6f 77 69  qlite_int64 rowi
1f1c5 64 3b 0a 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67  d;.  Mem **apArg
1f1c6 3b 0a 20 20 4d 65 6d 20 2a 70 58 3b 0a 23 65 6e  ;.  Mem *pX;.#en
1f1c7 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1f1c8 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1f1c9 6f 20 75 2e 63 6c 20 2a 2f 0a 0a 20 20 75 2e 63  o u.cl */..  u.c
1f1ca 6c 2e 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70  l.pVtab = pOp->p
1f1cb 34 2e 70 56 74 61 62 2d 3e 70 56 74 61 62 3b 0a  4.pVtab->pVtab;.
1f1cc 20 20 75 2e 63 6c 2e 70 4d 6f 64 75 6c 65 20 3d    u.cl.pModule =
1f1cd 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65   (sqlite3_module
1f1ce 20 2a 29 75 2e 63 6c 2e 70 56 74 61 62 2d 3e 70   *)u.cl.pVtab->p
1f1cf 4d 6f 64 75 6c 65 3b 0a 20 20 75 2e 63 6c 2e 6e  Module;.  u.cl.n
1f1d0 41 72 67 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20  Arg = pOp->p2;. 
1f1d1 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34   assert( pOp->p4
1f1d2 74 79 70 65 3d 3d 50 34 5f 56 54 41 42 20 29 3b  type==P4_VTAB );
1f1d3 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e  .  if( ALWAYS(u.
1f1d4 63 6c 2e 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64  cl.pModule->xUpd
1f1d5 61 74 65 29 20 29 7b 0a 20 20 20 20 75 2e 63 6c  ate) ){.    u.cl
1f1d6 2e 61 70 41 72 67 20 3d 20 70 2d 3e 61 70 41 72  .apArg = p->apAr
1f1d7 67 3b 0a 20 20 20 20 75 2e 63 6c 2e 70 58 20 3d  g;.    u.cl.pX =
1f1d8 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1f1d9 33 5d 3b 0a 20 20 20 20 66 6f 72 28 75 2e 63 6c  3];.    for(u.cl
1f1da 2e 69 3d 30 3b 20 75 2e 63 6c 2e 69 3c 75 2e 63  .i=0; u.cl.i<u.c
1f1db 6c 2e 6e 41 72 67 3b 20 75 2e 63 6c 2e 69 2b 2b  l.nArg; u.cl.i++
1f1dc 29 7b 0a 20 20 20 20 20 20 73 74 6f 72 65 54 79  ){.      storeTy
1f1dd 70 65 49 6e 66 6f 28 75 2e 63 6c 2e 70 58 2c 20  peInfo(u.cl.pX, 
1f1de 30 29 3b 0a 20 20 20 20 20 20 75 2e 63 6c 2e 61  0);.      u.cl.a
1f1df 70 41 72 67 5b 75 2e 63 6c 2e 69 5d 20 3d 20 75  pArg[u.cl.i] = u
1f1e0 2e 63 6c 2e 70 58 3b 0a 20 20 20 20 20 20 75 2e  .cl.pX;.      u.
1f1e1 63 6c 2e 70 58 2b 2b 3b 0a 20 20 20 20 7d 0a 20  cl.pX++;.    }. 
1f1e2 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61     if( sqlite3Sa
1f1e3 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f  fetyOff(db) ) go
1f1e4 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1f1e5 6d 69 73 75 73 65 3b 0a 20 20 20 20 72 63 20 3d  misuse;.    rc =
1f1e6 20 75 2e 63 6c 2e 70 4d 6f 64 75 6c 65 2d 3e 78   u.cl.pModule->x
1f1e7 55 70 64 61 74 65 28 75 2e 63 6c 2e 70 56 74 61  Update(u.cl.pVta
1f1e8 62 2c 20 75 2e 63 6c 2e 6e 41 72 67 2c 20 75 2e  b, u.cl.nArg, u.
1f1e9 63 6c 2e 61 70 41 72 67 2c 20 26 75 2e 63 6c 2e  cl.apArg, &u.cl.
1f1ea 72 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69  rowid);.    sqli
1f1eb 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
1f1ec 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70  >zErrMsg);.    p
1f1ed 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 6c  ->zErrMsg = u.cl
1f1ee 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b  .pVtab->zErrMsg;
1f1ef 0a 20 20 20 20 75 2e 63 6c 2e 70 56 74 61 62 2d  .    u.cl.pVtab-
1f1f0 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  >zErrMsg = 0;.  
1f1f1 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
1f1f2 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f  etyOn(db) ) goto
1f1f3 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
1f1f4 73 75 73 65 3b 0a 20 20 20 20 69 66 28 20 72 63  suse;.    if( rc
1f1f5 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
1f1f6 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 20 20  Op->p1 ){.      
1f1f7 61 73 73 65 72 74 28 20 75 2e 63 6c 2e 6e 41 72  assert( u.cl.nAr
1f1f8 67 3e 31 20 26 26 20 75 2e 63 6c 2e 61 70 41 72  g>1 && u.cl.apAr
1f1f9 67 5b 30 5d 20 26 26 20 28 75 2e 63 6c 2e 61 70  g[0] && (u.cl.ap
1f1fa 41 72 67 5b 30 5d 2d 3e 66 6c 61 67 73 26 4d 45  Arg[0]->flags&ME
1f1fb 4d 5f 4e 75 6c 6c 29 20 29 3b 0a 20 20 20 20 20  M_Null) );.     
1f1fc 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d   db->lastRowid =
1f1fd 20 75 2e 63 6c 2e 72 6f 77 69 64 3b 0a 20 20 20   u.cl.rowid;.   
1f1fe 20 7d 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67   }.    p->nChang
1f1ff 65 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  e++;.  }.  break
1f200 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
1f201 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1f202 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64  LTABLE */..#ifnd
1f203 65 66 20 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ef  SQLITE_OMIT_
1f204 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a  PAGER_PRAGMAS./*
1f205 20 4f 70 63 6f 64 65 3a 20 50 61 67 65 63 6f 75   Opcode: Pagecou
1f206 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  nt P1 P2 * * *.*
1f207 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 63  *.** Write the c
1f208 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66  urrent number of
1f209 20 70 61 67 65 73 20 69 6e 20 64 61 74 61 62 61   pages in databa
1f20a 73 65 20 50 31 20 74 6f 20 6d 65 6d 6f 72 79 20  se P1 to memory 
1f20b 63 65 6c 6c 20 50 32 2e 0a 2a 2f 0a 63 61 73 65  cell P2..*/.case
1f20c 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 3a 20 7b   OP_Pagecount: {
1f20d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f              /* o
1f20e 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
1f20f 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
1f210 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1f211 64 20 69 6e 74 6f 20 75 2e 63 6d 20 2a 2f 0a 20  d into u.cm */. 
1f212 20 69 6e 74 20 70 31 3b 0a 20 20 69 6e 74 20 6e   int p1;.  int n
1f213 50 61 67 65 3b 0a 20 20 50 61 67 65 72 20 2a 70  Page;.  Pager *p
1f214 50 61 67 65 72 3b 0a 23 65 6e 64 69 66 20 2f 2a  Pager;.#endif /*
1f215 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1f216 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6d   moved into u.cm
1f217 20 2a 2f 0a 0a 20 20 75 2e 63 6d 2e 70 31 20 3d   */..  u.cm.p1 =
1f218 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 63 6d   pOp->p1;.  u.cm
1f219 2e 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65  .pPager = sqlite
1f21a 33 42 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e  3BtreePager(db->
1f21b 61 44 62 5b 75 2e 63 6d 2e 70 31 5d 2e 70 42 74  aDb[u.cm.p1].pBt
1f21c 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
1f21d 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
1f21e 75 2e 63 6d 2e 70 50 61 67 65 72 2c 20 26 75 2e  u.cm.pPager, &u.
1f21f 63 6d 2e 6e 50 61 67 65 29 3b 0a 20 20 2f 2a 20  cm.nPage);.  /* 
1f220 4f 50 5f 50 61 67 65 63 6f 75 6e 74 20 69 73 20  OP_Pagecount is 
1f221 61 6c 77 61 79 73 20 63 61 6c 6c 65 64 20 66 72  always called fr
1f222 6f 6d 20 77 69 74 68 69 6e 20 61 20 72 65 61 64  om within a read
1f223 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54   transaction.  T
1f224 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 63 6f 75  he.  ** page cou
1f225 6e 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  nt has already b
1f226 65 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79  een successfully
1f227 20 72 65 61 64 20 61 6e 64 20 63 61 63 68 65 64   read and cached
1f228 2e 20 20 53 6f 20 74 68 65 0a 20 20 2a 2a 20 73  .  So the.  ** s
1f229 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63  qlite3PagerPagec
1f22a 6f 75 6e 74 28 29 20 63 61 6c 6c 20 61 62 6f 76  ount() call abov
1f22b 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 2a  e cannot fail. *
1f22c 2f 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 72  /.  if( ALWAYS(r
1f22d 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b  c==SQLITE_OK) ){
1f22e 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  .    pOut->flags
1f22f 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20   = MEM_Int;.    
1f230 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 63 6d  pOut->u.i = u.cm
1f231 2e 6e 50 61 67 65 3b 0a 20 20 7d 0a 20 20 62 72  .nPage;.  }.  br
1f232 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  eak;.}.#endif..#
1f233 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f234 49 54 5f 54 52 41 43 45 0a 2f 2a 20 4f 70 63 6f  IT_TRACE./* Opco
1f235 64 65 3a 20 54 72 61 63 65 20 2a 20 2a 20 2a 20  de: Trace * * * 
1f236 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 72  P4 *.**.** If tr
1f237 61 63 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64  acing is enabled
1f238 20 28 62 79 20 74 68 65 20 73 71 6c 69 74 65 33   (by the sqlite3
1f239 5f 74 72 61 63 65 28 29 29 20 69 6e 74 65 72 66  _trace()) interf
1f23a 61 63 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65  ace, then.** the
1f23b 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 63 6f   UTF-8 string co
1f23c 6e 74 61 69 6e 65 64 20 69 6e 20 50 34 20 69 73  ntained in P4 is
1f23d 20 65 6d 69 74 74 65 64 20 6f 6e 20 74 68 65 20   emitted on the 
1f23e 74 72 61 63 65 20 63 61 6c 6c 62 61 63 6b 2e 0a  trace callback..
1f23f 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 72 61 63 65  */.case OP_Trace
1f240 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f  : {.#if 0  /* lo
1f241 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1f242 76 65 64 20 69 6e 74 6f 20 75 2e 63 6e 20 2a 2f  ved into u.cn */
1f243 0a 20 20 63 68 61 72 20 2a 7a 54 72 61 63 65 3b  .  char *zTrace;
1f244 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1f245 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1f246 20 69 6e 74 6f 20 75 2e 63 6e 20 2a 2f 0a 0a 20   into u.cn */.. 
1f247 20 75 2e 63 6e 2e 7a 54 72 61 63 65 20 3d 20 28   u.cn.zTrace = (
1f248 70 4f 70 2d 3e 70 34 2e 7a 20 3f 20 70 4f 70 2d  pOp->p4.z ? pOp-
1f249 3e 70 34 2e 7a 20 3a 20 70 2d 3e 7a 53 71 6c 29  >p4.z : p->zSql)
1f24a 3b 0a 20 20 69 66 28 20 75 2e 63 6e 2e 7a 54 72  ;.  if( u.cn.zTr
1f24b 61 63 65 20 29 7b 0a 20 20 20 20 69 66 28 20 64  ace ){.    if( d
1f24c 62 2d 3e 78 54 72 61 63 65 20 29 7b 0a 20 20 20  b->xTrace ){.   
1f24d 20 20 20 64 62 2d 3e 78 54 72 61 63 65 28 64 62     db->xTrace(db
1f24e 2d 3e 70 54 72 61 63 65 41 72 67 2c 20 75 2e 63  ->pTraceArg, u.c
1f24f 6e 2e 7a 54 72 61 63 65 29 3b 0a 20 20 20 20 7d  n.zTrace);.    }
1f250 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
1f251 45 42 55 47 0a 20 20 20 20 69 66 28 20 28 64 62  EBUG.    if( (db
1f252 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
1f253 5f 53 71 6c 54 72 61 63 65 29 21 3d 30 20 29 7b  _SqlTrace)!=0 ){
1f254 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65  .      sqlite3De
1f255 62 75 67 50 72 69 6e 74 66 28 22 53 51 4c 2d 74  bugPrintf("SQL-t
1f256 72 61 63 65 3a 20 25 73 5c 6e 22 2c 20 75 2e 63  race: %s\n", u.c
1f257 6e 2e 7a 54 72 61 63 65 29 3b 0a 20 20 20 20 7d  n.zTrace);.    }
1f258 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1f259 45 5f 44 45 42 55 47 20 2a 2f 0a 20 20 7d 0a 20  E_DEBUG */.  }. 
1f25a 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1f25b 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f  .../* Opcode: No
1f25c 6f 70 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a  op * * * * *.**.
1f25d 2a 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20  ** Do nothing.  
1f25e 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
1f25f 20 69 73 20 6f 66 74 65 6e 20 75 73 65 66 75 6c   is often useful
1f260 20 61 73 20 61 20 6a 75 6d 70 0a 2a 2a 20 64 65   as a jump.** de
1f261 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a  stination..*/./*
1f262 0a 2a 2a 20 54 68 65 20 6d 61 67 69 63 20 45 78  .** The magic Ex
1f263 70 6c 61 69 6e 20 6f 70 63 6f 64 65 20 61 72 65  plain opcode are
1f264 20 6f 6e 6c 79 20 69 6e 73 65 72 74 65 64 20 77   only inserted w
1f265 68 65 6e 20 65 78 70 6c 61 69 6e 3d 3d 32 20 28  hen explain==2 (
1f266 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 6f 20 73  which.** is to s
1f267 61 79 20 77 68 65 6e 20 74 68 65 20 45 58 50 4c  ay when the EXPL
1f268 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 20 73  AIN QUERY PLAN s
1f269 79 6e 74 61 78 20 69 73 20 75 73 65 64 2e 29 0a  yntax is used.).
1f26a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 72  ** This opcode r
1f26b 65 63 6f 72 64 73 20 69 6e 66 6f 72 6d 61 74 69  ecords informati
1f26c 6f 6e 20 66 72 6f 6d 20 74 68 65 20 6f 70 74 69  on from the opti
1f26d 6d 69 7a 65 72 2e 20 20 49 74 20 69 73 20 74 68  mizer.  It is th
1f26e 65 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73  e.** the same as
1f26f 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 69 73 20   a no-op.  This 
1f270 6f 70 63 6f 64 65 73 6e 65 76 65 72 20 61 70 70  opcodesnever app
1f271 65 61 72 73 20 69 6e 20 61 20 72 65 61 6c 20 56  ears in a real V
1f272 4d 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 64 65  M program..*/.de
1f273 66 61 75 6c 74 3a 20 7b 20 20 20 20 20 20 20 20  fault: {        
1f274 20 20 2f 2a 20 54 68 69 73 20 69 73 20 72 65 61    /* This is rea
1f275 6c 6c 79 20 4f 50 5f 4e 6f 6f 70 20 61 6e 64 20  lly OP_Noop and 
1f276 4f 50 5f 45 78 70 6c 61 69 6e 20 2a 2f 0a 20 20  OP_Explain */.  
1f277 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  break;.}../*****
1f278 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f279 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f27a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f27b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f27c 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20  ********.** The 
1f27d 63 61 73 65 73 20 6f 66 20 74 68 65 20 73 77 69  cases of the swi
1f27e 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 61 62  tch statement ab
1f27f 6f 76 65 20 74 68 69 73 20 6c 69 6e 65 20 73 68  ove this line sh
1f280 6f 75 6c 64 20 61 6c 6c 20 62 65 20 69 6e 64 65  ould all be inde
1f281 6e 74 65 64 0a 2a 2a 20 62 79 20 36 20 73 70 61  nted.** by 6 spa
1f282 63 65 73 2e 20 20 42 75 74 20 74 68 65 20 6c 65  ces.  But the le
1f283 66 74 2d 6d 6f 73 74 20 36 20 73 70 61 63 65 73  ft-most 6 spaces
1f284 20 68 61 76 65 20 62 65 65 6e 20 72 65 6d 6f 76   have been remov
1f285 65 64 20 74 6f 20 69 6d 70 72 6f 76 65 20 74 68  ed to improve th
1f286 65 0a 2a 2a 20 72 65 61 64 61 62 69 6c 69 74 79  e.** readability
1f287 2e 20 20 46 72 6f 6d 20 74 68 69 73 20 70 6f 69  .  From this poi
1f288 6e 74 20 6f 6e 20 64 6f 77 6e 2c 20 74 68 65 20  nt on down, the 
1f289 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 74 69  normal indentati
1f28a 6f 6e 20 72 75 6c 65 73 20 61 72 65 0a 2a 2a 20  on rules are.** 
1f28b 72 65 73 74 6f 72 65 64 2e 0a 2a 2a 2a 2a 2a 2a  restored..******
1f28c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f28d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f28e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f28f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f290 2a 2a 2a 2a 2a 2a 2a 2f 0a 20 20 20 20 7d 0a 0a  *******/.    }..
1f291 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46  #ifdef VDBE_PROF
1f292 49 4c 45 0a 20 20 20 20 7b 0a 20 20 20 20 20 20  ILE.    {.      
1f293 75 36 34 20 65 6c 61 70 73 65 64 20 3d 20 73 71  u64 elapsed = sq
1f294 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 2d 20  lite3Hwtime() - 
1f295 73 74 61 72 74 3b 0a 20 20 20 20 20 20 70 4f 70  start;.      pOp
1f296 2d 3e 63 79 63 6c 65 73 20 2b 3d 20 65 6c 61 70  ->cycles += elap
1f297 73 65 64 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e  sed;.      pOp->
1f298 63 6e 74 2b 2b 3b 0a 23 69 66 20 30 0a 20 20 20  cnt++;.#if 0.   
1f299 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64       fprintf(std
1f29a 6f 75 74 2c 20 22 25 31 30 6c 6c 75 20 22 2c 20  out, "%10llu ", 
1f29b 65 6c 61 70 73 65 64 29 3b 0a 20 20 20 20 20 20  elapsed);.      
1f29c 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69    sqlite3VdbePri
1f29d 6e 74 4f 70 28 73 74 64 6f 75 74 2c 20 6f 72 69  ntOp(stdout, ori
1f29e 67 50 63 2c 20 26 70 2d 3e 61 4f 70 5b 6f 72 69  gPc, &p->aOp[ori
1f29f 67 50 63 5d 29 3b 0a 23 65 6e 64 69 66 0a 20 20  gPc]);.#endif.  
1f2a0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
1f2a1 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  /* The following
1f2a2 20 63 6f 64 65 20 61 64 64 73 20 6e 6f 74 68 69   code adds nothi
1f2a3 6e 67 20 74 6f 20 74 68 65 20 61 63 74 75 61 6c  ng to the actual
1f2a4 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 20   functionality. 
1f2a5 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 72 6f     ** of the pro
1f2a6 67 72 61 6d 2e 20 20 49 74 20 69 73 20 6f 6e 6c  gram.  It is onl
1f2a7 79 20 68 65 72 65 20 66 6f 72 20 74 65 73 74 69  y here for testi
1f2a8 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67  ng and debugging
1f2a9 2e 0a 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65 20  ..    ** On the 
1f2aa 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 74 20 64  other hand, it d
1f2ab 6f 65 73 20 62 75 72 6e 20 43 50 55 20 63 79 63  oes burn CPU cyc
1f2ac 6c 65 73 20 65 76 65 72 79 20 74 69 6d 65 20 74  les every time t
1f2ad 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20 74 68  hrough.    ** th
1f2ae 65 20 65 76 61 6c 75 61 74 6f 72 20 6c 6f 6f 70  e evaluator loop
1f2af 2e 20 20 53 6f 20 77 65 20 63 61 6e 20 6c 65 61  .  So we can lea
1f2b0 76 65 20 69 74 20 6f 75 74 20 77 68 65 6e 20 4e  ve it out when N
1f2b1 44 45 42 55 47 20 69 73 20 64 65 66 69 6e 65 64  DEBUG is defined
1f2b2 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66  ..    */.#ifndef
1f2b3 20 4e 44 45 42 55 47 0a 20 20 20 20 61 73 73 65   NDEBUG.    asse
1f2b4 72 74 28 20 70 63 3e 3d 2d 31 20 26 26 20 70 63  rt( pc>=-1 && pc
1f2b5 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 0a 23 69 66 64  <p->nOp );..#ifd
1f2b6 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
1f2b7 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65      if( p->trace
1f2b8 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63   ){.      if( rc
1f2b9 21 3d 30 20 29 20 66 70 72 69 6e 74 66 28 70 2d  !=0 ) fprintf(p-
1f2ba 3e 74 72 61 63 65 2c 22 72 63 3d 25 64 5c 6e 22  >trace,"rc=%d\n"
1f2bb 2c 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ,rc);.      if( 
1f2bc 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46  opProperty & OPF
1f2bd 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41  LG_OUT2_PRERELEA
1f2be 53 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  SE ){.        re
1f2bf 67 69 73 74 65 72 54 72 61 63 65 28 70 2d 3e 74  gisterTrace(p->t
1f2c0 72 61 63 65 2c 20 70 4f 70 2d 3e 70 32 2c 20 70  race, pOp->p2, p
1f2c1 4f 75 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Out);.      }.  
1f2c2 20 20 20 20 69 66 28 20 6f 70 50 72 6f 70 65 72      if( opProper
1f2c3 74 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 20  ty & OPFLG_OUT3 
1f2c4 29 7b 0a 20 20 20 20 20 20 20 20 72 65 67 69 73  ){.        regis
1f2c5 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63  terTrace(p->trac
1f2c6 65 2c 20 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74  e, pOp->p3, pOut
1f2c7 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
1f2c8 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49  .#endif  /* SQLI
1f2c9 54 45 5f 44 45 42 55 47 20 2a 2f 0a 23 65 6e 64  TE_DEBUG */.#end
1f2ca 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f  if  /* NDEBUG */
1f2cb 0a 20 20 7d 20 20 2f 2a 20 54 68 65 20 65 6e 64  .  }  /* The end
1f2cc 20 6f 66 20 74 68 65 20 66 6f 72 28 3b 3b 29 20   of the for(;;) 
1f2cd 6c 6f 6f 70 20 74 68 65 20 6c 6f 6f 70 73 20 74  loop the loops t
1f2ce 68 72 6f 75 67 68 20 6f 70 63 6f 64 65 73 20 2a  hrough opcodes *
1f2cf 2f 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 72 65  /..  /* If we re
1f2d0 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20  ach this point, 
1f2d1 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 65 78  it means that ex
1f2d2 65 63 75 74 69 6f 6e 20 69 73 20 66 69 6e 69 73  ecution is finis
1f2d3 68 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 61 6e  hed with.  ** an
1f2d4 20 65 72 72 6f 72 20 6f 66 20 73 6f 6d 65 20 6b   error of some k
1f2d5 69 6e 64 2e 0a 20 20 2a 2f 0a 76 64 62 65 5f 65  ind..  */.vdbe_e
1f2d6 72 72 6f 72 5f 68 61 6c 74 3a 0a 20 20 61 73 73  rror_halt:.  ass
1f2d7 65 72 74 28 20 72 63 20 29 3b 0a 20 20 70 2d 3e  ert( rc );.  p->
1f2d8 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74  rc = rc;.  sqlit
1f2d9 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20  e3VdbeHalt(p);. 
1f2da 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1f2db 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 64 62  IOERR_NOMEM ) db
1f2dc 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
1f2dd 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54   1;.  rc = SQLIT
1f2de 45 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a 20 54  E_ERROR;..  /* T
1f2df 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 20  his is the only 
1f2e0 77 61 79 20 6f 75 74 20 6f 66 20 74 68 69 73 20  way out of this 
1f2e1 70 72 6f 63 65 64 75 72 65 2e 20 20 57 65 20 68  procedure.  We h
1f2e2 61 76 65 20 74 6f 0a 20 20 2a 2a 20 72 65 6c 65  ave to.  ** rele
1f2e3 61 73 65 20 74 68 65 20 6d 75 74 65 78 65 73 20  ase the mutexes 
1f2e4 6f 6e 20 62 74 72 65 65 73 20 74 68 61 74 20 77  on btrees that w
1f2e5 65 72 65 20 61 63 71 75 69 72 65 64 20 61 74 20  ere acquired at 
1f2e6 74 68 65 0a 20 20 2a 2a 20 74 6f 70 2e 20 2a 2f  the.  ** top. */
1f2e7 0a 76 64 62 65 5f 72 65 74 75 72 6e 3a 0a 20 20  .vdbe_return:.  
1f2e8 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
1f2e9 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e  xArrayLeave(&p->
1f2ea 61 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  aMutex);.  retur
1f2eb 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70  n rc;..  /* Jump
1f2ec 20 74 6f 20 68 65 72 65 20 69 66 20 61 20 73 74   to here if a st
1f2ed 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 6c 61 72  ring or blob lar
1f2ee 67 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f  ger than SQLITE_
1f2ef 4d 41 58 5f 4c 45 4e 47 54 48 0a 20 20 2a 2a 20  MAX_LENGTH.  ** 
1f2f0 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a  is encountered..
1f2f1 20 20 2a 2f 0a 74 6f 6f 5f 62 69 67 3a 0a 20 20    */.too_big:.  
1f2f2 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
1f2f3 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62  (&p->zErrMsg, db
1f2f4 2c 20 22 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f  , "string or blo
1f2f5 62 20 74 6f 6f 20 62 69 67 22 29 3b 0a 20 20 72  b too big");.  r
1f2f6 63 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49  c = SQLITE_TOOBI
1f2f7 47 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65  G;.  goto vdbe_e
1f2f8 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a  rror_halt;..  /*
1f2f9 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66   Jump to here if
1f2fa 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c   a malloc() fail
1f2fb 73 2e 0a 20 20 2a 2f 0a 6e 6f 5f 6d 65 6d 3a 0a  s..  */.no_mem:.
1f2fc 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
1f2fd 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65  ed = 1;.  sqlite
1f2fe 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
1f2ff 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 6f 75 74  ErrMsg, db, "out
1f300 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20   of memory");.  
1f301 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
1f302 4d 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65  M;.  goto vdbe_e
1f303 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a  rror_halt;..  /*
1f304 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f   Jump to here fo
1f305 72 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  r an SQLITE_MISU
1f306 53 45 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 61  SE error..  */.a
1f307 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
1f308 73 65 3a 0a 20 20 72 63 20 3d 20 53 51 4c 49 54  se:.  rc = SQLIT
1f309 45 5f 4d 49 53 55 53 45 3b 0a 20 20 2f 2a 20 46  E_MISUSE;.  /* F
1f30a 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 61 62  all thru into ab
1f30b 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1f30c 20 2a 2f 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74   */..  /* Jump t
1f30d 6f 20 68 65 72 65 20 66 6f 72 20 61 6e 79 20 6f  o here for any o
1f30e 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 66 61 74  ther kind of fat
1f30f 61 6c 20 65 72 72 6f 72 2e 20 20 54 68 65 20 22  al error.  The "
1f310 72 63 22 20 76 61 72 69 61 62 6c 65 0a 20 20 2a  rc" variable.  *
1f311 2a 20 73 68 6f 75 6c 64 20 68 6f 6c 64 20 74 68  * should hold th
1f312 65 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 2e 0a  e error number..
1f313 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74    */.abort_due_t
1f314 6f 5f 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 72  o_error:.  asser
1f315 74 28 20 70 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30  t( p->zErrMsg==0
1f316 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61   );.  if( db->ma
1f317 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 63 20  llocFailed ) rc 
1f318 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
1f319 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1f31a 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a  _IOERR_NOMEM ){.
1f31b 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
1f31c 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
1f31d 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69  , db, "%s", sqli
1f31e 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a  te3ErrStr(rc));.
1f31f 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f    }.  goto vdbe_
1f320 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f  error_halt;..  /
1f321 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69  * Jump to here i
1f322 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  f the sqlite3_in
1f323 74 65 72 72 75 70 74 28 29 20 41 50 49 20 73 65  terrupt() API se
1f324 74 73 20 74 68 65 20 69 6e 74 65 72 72 75 70 74  ts the interrupt
1f325 0a 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 2a 2f  .  ** flag..  */
1f326 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e  .abort_due_to_in
1f327 74 65 72 72 75 70 74 3a 0a 20 20 61 73 73 65 72  terrupt:.  asser
1f328 74 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65  t( db->u1.isInte
1f329 72 72 75 70 74 65 64 20 29 3b 0a 20 20 72 63 20  rrupted );.  rc 
1f32a 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55  = SQLITE_INTERRU
1f32b 50 54 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63  PT;.  p->rc = rc
1f32c 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 74 53 74  ;.  sqlite3SetSt
1f32d 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
1f32e 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69  , db, "%s", sqli
1f32f 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a  te3ErrStr(rc));.
1f330 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f    goto vdbe_erro
1f331 72 5f 68 61 6c 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  r_halt;.}../****
1f332 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
1f333 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f vdbe.c *******
1f334 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f335 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f336 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
1f337 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
1f338 20 66 69 6c 65 20 76 64 62 65 62 6c 6f 62 2e 63   file vdbeblob.c
1f339 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1f33a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f33b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
1f33c 20 32 30 30 37 20 4d 61 79 20 31 0a 2a 2a 0a 2a   2007 May 1.**.*
1f33d 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
1f33e 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
1f33f 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
1f340 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
1f341 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
1f342 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
1f343 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
1f344 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
1f345 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
1f346 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
1f347 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
1f348 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
1f349 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
1f34a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
1f34b 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
1f34c 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
1f34d 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
1f34e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
1f34f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f351 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f352 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
1f353 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
1f354 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f  ins code used to
1f355 20 69 6d 70 6c 65 6d 65 6e 74 20 69 6e 63 72 65   implement incre
1f356 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 2e  mental BLOB I/O.
1f357 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65  .**.** $Id: vdbe
1f358 62 6c 6f 62 2e 63 2c 76 20 31 2e 33 35 20 32 30  blob.c,v 1.35 20
1f359 30 39 2f 30 37 2f 30 32 20 30 37 3a 34 37 3a 33  09/07/02 07:47:3
1f35a 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  3 danielk1977 Ex
1f35b 70 20 24 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66  p $.*/...#ifndef
1f35c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
1f35d 52 42 4c 4f 42 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c  RBLOB../*.** Val
1f35e 69 64 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a  id sqlite3_blob*
1f35f 20 68 61 6e 64 6c 65 73 20 70 6f 69 6e 74 20 74   handles point t
1f360 6f 20 49 6e 63 72 62 6c 6f 62 20 73 74 72 75 63  o Incrblob struc
1f361 74 75 72 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65  tures..*/.typede
1f362 66 20 73 74 72 75 63 74 20 49 6e 63 72 62 6c 6f  f struct Incrblo
1f363 62 20 49 6e 63 72 62 6c 6f 62 3b 0a 73 74 72 75  b Incrblob;.stru
1f364 63 74 20 49 6e 63 72 62 6c 6f 62 20 7b 0a 20 20  ct Incrblob {.  
1f365 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20  int flags;      
1f366 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20          /* Copy 
1f367 6f 66 20 22 66 6c 61 67 73 22 20 70 61 73 73 65  of "flags" passe
1f368 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 6c 6f  d to sqlite3_blo
1f369 62 5f 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e  b_open() */.  in
1f36a 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20  t nByte;        
1f36b 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
1f36c 20 6f 70 65 6e 20 62 6c 6f 62 2c 20 69 6e 20 62   open blob, in b
1f36d 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 4f  ytes */.  int iO
1f36e 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20  ffset;          
1f36f 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74    /* Byte offset
1f370 20 6f 66 20 62 6c 6f 62 20 69 6e 20 63 75 72 73   of blob in curs
1f371 6f 72 20 64 61 74 61 20 2a 2f 0a 20 20 42 74 43  or data */.  BtC
1f372 75 72 73 6f 72 20 2a 70 43 73 72 3b 20 20 20 20  ursor *pCsr;    
1f373 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70       /* Cursor p
1f374 6f 69 6e 74 69 6e 67 20 61 74 20 62 6c 6f 62 20  ointing at blob 
1f375 72 6f 77 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  row */.  sqlite3
1f376 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20  _stmt *pStmt;   
1f377 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 68 6f   /* Statement ho
1f378 6c 64 69 6e 67 20 63 75 72 73 6f 72 20 6f 70 65  lding cursor ope
1f379 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  n */.  sqlite3 *
1f37a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  db;            /
1f37b 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 64  * The associated
1f37c 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 7d 3b 0a   database */.};.
1f37d 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 62 6c  ./*.** Open a bl
1f37e 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51  ob handle..*/.SQ
1f37f 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1f380 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a  ite3_blob_open(.
1f381 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 20    sqlite3* db,  
1f382 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1f383 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1f384 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  tion */.  const 
1f385 63 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 20  char *zDb,      
1f386 20 20 2f 2a 20 54 68 65 20 61 74 74 61 63 68 65    /* The attache
1f387 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61  d database conta
1f388 69 6e 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a  ining the blob *
1f389 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1f38a 7a 54 61 62 6c 65 2c 20 20 20 20 20 2f 2a 20 54  zTable,     /* T
1f38b 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  he table contain
1f38c 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a  ing the blob */.
1f38d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
1f38e 6f 6c 75 6d 6e 2c 20 20 20 20 2f 2a 20 54 68 65  olumn,    /* The
1f38f 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 69   column containi
1f390 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20  ng the blob */. 
1f391 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 52   sqlite_int64 iR
1f392 6f 77 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ow,      /* The 
1f393 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  row containing t
1f394 68 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 69 6e 74  he glob */.  int
1f395 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
1f396 20 20 20 20 20 2f 2a 20 54 72 75 65 20 2d 3e 20       /* True -> 
1f397 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73  read/write acces
1f398 73 2c 20 66 61 6c 73 65 20 2d 3e 20 72 65 61 64  s, false -> read
1f399 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 71 6c 69 74  -only */.  sqlit
1f39a 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62  e3_blob **ppBlob
1f39b 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72     /* Handle for
1f39c 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 62   accessing the b
1f39d 6c 6f 62 20 72 65 74 75 72 6e 65 64 20 68 65 72  lob returned her
1f39e 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 41  e */.){.  int nA
1f39f 74 74 65 6d 70 74 20 3d 20 30 3b 0a 20 20 69 6e  ttempt = 0;.  in
1f3a0 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20  t iCol;         
1f3a1 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
1f3a2 66 20 7a 43 6f 6c 75 6d 6e 20 69 6e 20 72 6f 77  f zColumn in row
1f3a3 2d 72 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 2f 2a  -record */..  /*
1f3a4 20 54 68 69 73 20 56 44 42 45 20 70 72 6f 67 72   This VDBE progr
1f3a5 61 6d 20 73 65 65 6b 73 20 61 20 62 74 72 65 65  am seeks a btree
1f3a6 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 69   cursor to the i
1f3a7 64 65 6e 74 69 66 69 65 64 20 0a 20 20 2a 2a 20  dentified .  ** 
1f3a8 64 62 2f 74 61 62 6c 65 2f 72 6f 77 20 65 6e 74  db/table/row ent
1f3a9 72 79 2e 20 54 68 65 20 72 65 61 73 6f 6e 20 66  ry. The reason f
1f3aa 6f 72 20 75 73 69 6e 67 20 61 20 76 64 62 65 20  or using a vdbe 
1f3ab 70 72 6f 67 72 61 6d 20 69 6e 73 74 65 61 64 0a  program instead.
1f3ac 20 20 2a 2a 20 6f 66 20 77 72 69 74 69 6e 67 20    ** of writing 
1f3ad 63 6f 64 65 20 74 6f 20 75 73 65 20 74 68 65 20  code to use the 
1f3ae 62 2d 74 72 65 65 20 6c 61 79 65 72 20 64 69 72  b-tree layer dir
1f3af 65 63 74 6c 79 20 69 73 20 74 68 61 74 20 74 68  ectly is that th
1f3b0 65 0a 20 20 2a 2a 20 76 64 62 65 20 70 72 6f 67  e.  ** vdbe prog
1f3b1 72 61 6d 20 77 69 6c 6c 20 74 61 6b 65 20 61 64  ram will take ad
1f3b2 76 61 6e 74 61 67 65 20 6f 66 20 74 68 65 20 76  vantage of the v
1f3b3 61 72 69 6f 75 73 20 74 72 61 6e 73 61 63 74 69  arious transacti
1f3b4 6f 6e 2c 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67  on,.  ** locking
1f3b5 20 61 6e 64 20 65 72 72 6f 72 20 68 61 6e 64 6c   and error handl
1f3b6 69 6e 67 20 69 6e 66 72 61 73 74 72 75 63 74 75  ing infrastructu
1f3b7 72 65 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68  re built into th
1f3b8 65 20 76 64 62 65 2e 0a 20 20 2a 2a 0a 20 20 2a  e vdbe..  **.  *
1f3b9 2a 20 41 66 74 65 72 20 73 65 65 6b 69 6e 67 20  * After seeking 
1f3ba 74 68 65 20 63 75 72 73 6f 72 2c 20 74 68 65 20  the cursor, the 
1f3bb 76 64 62 65 20 65 78 65 63 75 74 65 73 20 61 6e  vdbe executes an
1f3bc 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2e 0a 20   OP_ResultRow.. 
1f3bd 20 2a 2a 20 43 6f 64 65 20 65 78 74 65 72 6e 61   ** Code externa
1f3be 6c 20 74 6f 20 74 68 65 20 56 64 62 65 20 74 68  l to the Vdbe th
1f3bf 65 6e 20 22 62 6f 72 72 6f 77 73 22 20 74 68 65  en "borrows" the
1f3c0 20 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20 61   b-tree cursor a
1f3c1 6e 64 0a 20 20 2a 2a 20 75 73 65 73 20 69 74 20  nd.  ** uses it 
1f3c2 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
1f3c3 20 62 6c 6f 62 5f 72 65 61 64 28 29 2c 20 62 6c   blob_read(), bl
1f3c4 6f 62 5f 77 72 69 74 65 28 29 20 61 6e 64 20 0a  ob_write() and .
1f3c5 20 20 2a 2a 20 62 6c 6f 62 5f 62 79 74 65 73 28    ** blob_bytes(
1f3c6 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a  ) functions..  *
1f3c7 2a 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 74  *.  ** The sqlit
1f3c8 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 20  e3_blob_close() 
1f3c9 66 75 6e 63 74 69 6f 6e 20 66 69 6e 61 6c 69 7a  function finaliz
1f3ca 65 73 20 74 68 65 20 76 64 62 65 20 70 72 6f 67  es the vdbe prog
1f3cb 72 61 6d 2c 0a 20 20 2a 2a 20 77 68 69 63 68 20  ram,.  ** which 
1f3cc 63 6c 6f 73 65 73 20 74 68 65 20 62 2d 74 72 65  closes the b-tre
1f3cd 65 20 63 75 72 73 6f 72 20 61 6e 64 20 28 70 6f  e cursor and (po
1f3ce 73 73 69 62 6c 79 29 20 63 6f 6d 6d 69 74 73 20  ssibly) commits 
1f3cf 74 68 65 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61  the .  ** transa
1f3d0 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 74  ction..  */.  st
1f3d1 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f  atic const VdbeO
1f3d2 70 4c 69 73 74 20 6f 70 65 6e 42 6c 6f 62 5b 5d  pList openBlob[]
1f3d3 20 3d 20 7b 0a 20 20 20 20 7b 4f 50 5f 54 72 61   = {.    {OP_Tra
1f3d4 6e 73 61 63 74 69 6f 6e 2c 20 30 2c 20 30 2c 20  nsaction, 0, 0, 
1f3d5 30 7d 2c 20 20 20 20 20 2f 2a 20 30 3a 20 53 74  0},     /* 0: St
1f3d6 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f  art a transactio
1f3d7 6e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 56 65 72  n */.    {OP_Ver
1f3d8 69 66 79 43 6f 6f 6b 69 65 2c 20 30 2c 20 30 2c  ifyCookie, 0, 0,
1f3d9 20 30 7d 2c 20 20 20 20 2f 2a 20 31 3a 20 43 68   0},    /* 1: Ch
1f3da 65 63 6b 20 74 68 65 20 73 63 68 65 6d 61 20 63  eck the schema c
1f3db 6f 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 7b 4f 50  ookie */.    {OP
1f3dc 5f 54 61 62 6c 65 4c 6f 63 6b 2c 20 30 2c 20 30  _TableLock, 0, 0
1f3dd 2c 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 32  , 0},       /* 2
1f3de 3a 20 41 63 71 75 69 72 65 20 61 20 72 65 61 64  : Acquire a read
1f3df 20 6f 72 20 77 72 69 74 65 20 6c 6f 63 6b 20 2a   or write lock *
1f3e0 2f 0a 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66  /..    /* One of
1f3e1 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   the following t
1f3e2 77 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  wo instructions 
1f3e3 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 61  is replaced by a
1f3e4 6e 20 4f 50 5f 4e 6f 6f 70 2e 20 2a 2f 0a 20 20  n OP_Noop. */.  
1f3e5 20 20 7b 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20    {OP_OpenRead, 
1f3e6 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20  0, 0, 0},       
1f3e7 20 2f 2a 20 33 3a 20 4f 70 65 6e 20 63 75 72 73   /* 3: Open curs
1f3e8 6f 72 20 30 20 66 6f 72 20 72 65 61 64 69 6e 67  or 0 for reading
1f3e9 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f 70 65 6e   */.    {OP_Open
1f3ea 57 72 69 74 65 2c 20 30 2c 20 30 2c 20 30 7d 2c  Write, 0, 0, 0},
1f3eb 20 20 20 20 20 20 20 2f 2a 20 34 3a 20 4f 70 65         /* 4: Ope
1f3ec 6e 20 63 75 72 73 6f 72 20 30 20 66 6f 72 20 72  n cursor 0 for r
1f3ed 65 61 64 2f 77 72 69 74 65 20 2a 2f 0a 0a 20 20  ead/write */..  
1f3ee 20 20 7b 4f 50 5f 56 61 72 69 61 62 6c 65 2c 20    {OP_Variable, 
1f3ef 31 2c 20 31 2c 20 31 7d 2c 20 20 20 20 20 20 20  1, 1, 1},       
1f3f0 20 2f 2a 20 35 3a 20 50 75 73 68 20 74 68 65 20   /* 5: Push the 
1f3f1 72 6f 77 69 64 20 74 6f 20 74 68 65 20 73 74 61  rowid to the sta
1f3f2 63 6b 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4e 6f  ck */.    {OP_No
1f3f3 74 45 78 69 73 74 73 2c 20 30 2c 20 39 2c 20 31  tExists, 0, 9, 1
1f3f4 7d 2c 20 20 20 20 20 20 20 2f 2a 20 36 3a 20 53  },       /* 6: S
1f3f5 65 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20 2a  eek the cursor *
1f3f6 2f 0a 20 20 20 20 7b 4f 50 5f 43 6f 6c 75 6d 6e  /.    {OP_Column
1f3f7 2c 20 30 2c 20 30 2c 20 31 7d 2c 20 20 20 20 20  , 0, 0, 1},     
1f3f8 20 20 20 20 20 2f 2a 20 37 20 20 2a 2f 0a 20 20       /* 7  */.  
1f3f9 20 20 7b 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c    {OP_ResultRow,
1f3fa 20 31 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20   1, 0, 0},      
1f3fb 20 2f 2a 20 38 20 20 2a 2f 0a 20 20 20 20 7b 4f   /* 8  */.    {O
1f3fc 50 5f 43 6c 6f 73 65 2c 20 30 2c 20 30 2c 20 30  P_Close, 0, 0, 0
1f3fd 7d 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  },           /* 
1f3fe 39 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 48 61  9  */.    {OP_Ha
1f3ff 6c 74 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20  lt, 0, 0, 0},   
1f400 20 20 20 20 20 20 20 20 20 2f 2a 20 31 30 20 2a           /* 10 *
1f401 2f 0a 20 20 7d 3b 0a 0a 20 20 56 64 62 65 20 2a  /.  };..  Vdbe *
1f402 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20  v = 0;.  int rc 
1f403 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63  = SQLITE_OK;.  c
1f404 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 20  har *zErr = 0;. 
1f405 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
1f406 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a  Parse *pParse;..
1f407 20 20 2a 70 70 42 6c 6f 62 20 3d 20 30 3b 0a 20    *ppBlob = 0;. 
1f408 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1f409 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
1f40a 0a 20 20 70 50 61 72 73 65 20 3d 20 73 71 6c 69  .  pParse = sqli
1f40b 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77  te3StackAllocRaw
1f40c 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 50 61  (db, sizeof(*pPa
1f40d 72 73 65 29 29 3b 0a 20 20 69 66 28 20 70 50 61  rse));.  if( pPa
1f40e 72 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63  rse==0 ){.    rc
1f40f 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
1f410 0a 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f  .    goto blob_o
1f411 70 65 6e 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 64  pen_out;.  }.  d
1f412 6f 20 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70  o {.    memset(p
1f413 50 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66  Parse, 0, sizeof
1f414 28 50 61 72 73 65 29 29 3b 0a 20 20 20 20 70 50  (Parse));.    pP
1f415 61 72 73 65 2d 3e 64 62 20 3d 20 64 62 3b 0a 0a  arse->db = db;..
1f416 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
1f417 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20  afetyOn(db) ){. 
1f418 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1f419 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20  ee(db, zErr);.  
1f41a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b      sqlite3Stack
1f41b 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 29  Free(db, pParse)
1f41c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
1f41d 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
1f41e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 72 65  mutex);.      re
1f41f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
1f420 53 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73  SE;.    }..    s
1f421 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1f422 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 70 54 61  All(db);.    pTa
1f423 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  b = sqlite3Locat
1f424 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30  eTable(pParse, 0
1f425 2c 20 7a 54 61 62 6c 65 2c 20 7a 44 62 29 3b 0a  , zTable, zDb);.
1f426 20 20 20 20 69 66 28 20 70 54 61 62 20 26 26 20      if( pTab && 
1f427 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
1f428 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20  ){.      pTab = 
1f429 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  0;.      sqlite3
1f42a 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1f42b 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 69   "cannot open vi
1f42c 72 74 75 61 6c 20 74 61 62 6c 65 3a 20 25 73 22  rtual table: %s"
1f42d 2c 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d  , zTable);.    }
1f42e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1f42f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 69 66  OMIT_VIEW.    if
1f430 28 20 70 54 61 62 20 26 26 20 70 54 61 62 2d 3e  ( pTab && pTab->
1f431 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20  pSelect ){.     
1f432 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20   pTab = 0;.     
1f433 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1f434 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74  (pParse, "cannot
1f435 20 6f 70 65 6e 20 76 69 65 77 3a 20 25 73 22 2c   open view: %s",
1f436 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a   zTable);.    }.
1f437 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 21  #endif.    if( !
1f438 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 69 66  pTab ){.      if
1f439 28 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73  ( pParse->zErrMs
1f43a 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  g ){.        sql
1f43b 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
1f43c 45 72 72 29 3b 0a 20 20 20 20 20 20 20 20 7a 45  Err);.        zE
1f43d 72 72 20 3d 20 70 50 61 72 73 65 2d 3e 7a 45 72  rr = pParse->zEr
1f43e 72 4d 73 67 3b 0a 20 20 20 20 20 20 20 20 70 50  rMsg;.        pP
1f43f 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  arse->zErrMsg = 
1f440 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
1f441 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
1f442 4f 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29  OR;.      (void)
1f443 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
1f444 28 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  (db);.      sqli
1f445 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c  te3BtreeLeaveAll
1f446 28 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  (db);.      goto
1f447 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a   blob_open_out;.
1f448 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4e 6f      }..    /* No
1f449 77 20 73 65 61 72 63 68 20 70 54 61 62 20 66 6f  w search pTab fo
1f44a 72 20 74 68 65 20 65 78 61 63 74 20 63 6f 6c 75  r the exact colu
1f44b 6d 6e 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69  mn. */.    for(i
1f44c 43 6f 6c 3d 30 3b 20 69 43 6f 6c 20 3c 20 70 54  Col=0; iCol < pT
1f44d 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b  ab->nCol; iCol++
1f44e 29 20 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71  ) {.      if( sq
1f44f 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 54 61  lite3StrICmp(pTa
1f450 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e  b->aCol[iCol].zN
1f451 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 29 3d 3d 30  ame, zColumn)==0
1f452 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61   ){.        brea
1f453 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
1f454 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70  .    if( iCol==p
1f455 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20  Tab->nCol ){.   
1f456 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1f457 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20  (db, zErr);.    
1f458 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33    zErr = sqlite3
1f459 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20  MPrintf(db, "no 
1f45a 73 75 63 68 20 63 6f 6c 75 6d 6e 3a 20 5c 22 25  such column: \"%
1f45b 73 5c 22 22 2c 20 7a 43 6f 6c 75 6d 6e 29 3b 0a  s\"", zColumn);.
1f45c 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1f45d 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 28  E_ERROR;.      (
1f45e 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
1f45f 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20  tyOff(db);.     
1f460 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
1f461 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20  veAll(db);.     
1f462 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f   goto blob_open_
1f463 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  out;.    }..    
1f464 2f 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  /* If the value 
1f465 69 73 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20  is being opened 
1f466 66 6f 72 20 77 72 69 74 69 6e 67 2c 20 63 68 65  for writing, che
1f467 63 6b 20 74 68 61 74 20 74 68 65 0a 20 20 20 20  ck that the.    
1f468 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74  ** column is not
1f469 20 69 6e 64 65 78 65 64 2c 20 61 6e 64 20 74 68   indexed, and th
1f46a 61 74 20 69 74 20 69 73 20 6e 6f 74 20 70 61 72  at it is not par
1f46b 74 20 6f 66 20 61 20 66 6f 72 65 69 67 6e 20 6b  t of a foreign k
1f46c 65 79 2e 20 0a 20 20 20 20 2a 2a 20 49 74 20 69  ey. .    ** It i
1f46d 73 20 61 67 61 69 6e 73 74 20 74 68 65 20 72 75  s against the ru
1f46e 6c 65 73 20 74 6f 20 6f 70 65 6e 20 61 20 63 6f  les to open a co
1f46f 6c 75 6d 6e 20 74 6f 20 77 68 69 63 68 20 65 69  lumn to which ei
1f470 74 68 65 72 20 6f 66 20 74 68 65 73 65 0a 20 20  ther of these.  
1f471 20 20 2a 2a 20 64 65 73 63 72 69 70 74 69 6f 6e    ** description
1f472 73 20 61 70 70 6c 69 65 73 20 66 6f 72 20 77 72  s applies for wr
1f473 69 74 69 6e 67 2e 20 20 2a 2f 0a 20 20 20 20 69  iting.  */.    i
1f474 66 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20  f( flags ){.    
1f475 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
1f476 61 75 6c 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  ault = 0;.      
1f477 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 23 69 66  Index *pIdx;.#if
1f478 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1f479 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 20  _FOREIGN_KEY.   
1f47a 20 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73     if( db->flags
1f47b 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b  &SQLITE_ForeignK
1f47c 65 79 73 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  eys ){.        /
1f47d 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  * Check that the
1f47e 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 70   column is not p
1f47f 61 72 74 20 6f 66 20 61 6e 20 46 4b 20 63 68 69  art of an FK chi
1f480 6c 64 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f  ld key definitio
1f481 6e 2e 20 49 74 0a 20 20 20 20 20 20 20 20 2a 2a  n. It.        **
1f482 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72   is not necessar
1f483 79 20 74 6f 20 63 68 65 63 6b 20 69 66 20 69 74  y to check if it
1f484 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 70 61   is part of a pa
1f485 72 65 6e 74 20 6b 65 79 2c 20 61 73 20 70 61 72  rent key, as par
1f486 65 6e 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 6b  ent.        ** k
1f487 65 79 20 63 6f 6c 75 6d 6e 73 20 6d 75 73 74 20  ey columns must 
1f488 62 65 20 69 6e 64 65 78 65 64 2e 20 54 68 65 20  be indexed. The 
1f489 63 68 65 63 6b 20 62 65 6c 6f 77 20 77 69 6c 6c  check below will
1f48a 20 70 69 63 6b 20 75 70 20 74 68 69 73 20 0a 20   pick up this . 
1f48b 20 20 20 20 20 20 20 2a 2a 20 63 61 73 65 2e 20         ** case. 
1f48c 20 2a 2f 0a 20 20 20 20 20 20 20 20 46 4b 65 79   */.        FKey
1f48d 20 2a 70 46 4b 65 79 3b 0a 20 20 20 20 20 20 20   *pFKey;.       
1f48e 20 66 6f 72 28 70 46 4b 65 79 3d 70 54 61 62 2d   for(pFKey=pTab-
1f48f 3e 70 46 4b 65 79 3b 20 70 46 4b 65 79 3b 20 70  >pFKey; pFKey; p
1f490 46 4b 65 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78  FKey=pFKey->pNex
1f491 74 46 72 6f 6d 29 7b 0a 20 20 20 20 20 20 20 20  tFrom){.        
1f492 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20    int j;.       
1f493 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46     for(j=0; j<pF
1f494 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b  Key->nCol; j++){
1f495 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
1f496 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 6a 5d 2e   pFKey->aCol[j].
1f497 69 46 72 6f 6d 3d 3d 69 43 6f 6c 20 29 7b 0a 20  iFrom==iCol ){. 
1f498 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 61               zFa
1f499 75 6c 74 20 3d 20 22 66 6f 72 65 69 67 6e 20 6b  ult = "foreign k
1f49a 65 79 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ey";.           
1f49b 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20   }.          }. 
1f49c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1f49d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 66 6f  .#endif.      fo
1f49e 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
1f49f 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
1f4a0 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
1f4a1 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20        int j;.   
1f4a2 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
1f4a3 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a  pIdx->nColumn; j
1f4a4 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
1f4a5 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d  f( pIdx->aiColum
1f4a6 6e 5b 6a 5d 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20  n[j]==iCol ){.  
1f4a7 20 20 20 20 20 20 20 20 20 20 7a 46 61 75 6c 74            zFault
1f4a8 20 3d 20 22 69 6e 64 65 78 65 64 22 3b 0a 20 20   = "indexed";.  
1f4a9 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1f4aa 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1f4ab 20 20 69 66 28 20 7a 46 61 75 6c 74 20 29 7b 0a    if( zFault ){.
1f4ac 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
1f4ad 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b  bFree(db, zErr);
1f4ae 0a 20 20 20 20 20 20 20 20 7a 45 72 72 20 3d 20  .        zErr = 
1f4af 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
1f4b0 62 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20  b, "cannot open 
1f4b1 25 73 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 72  %s column for wr
1f4b2 69 74 69 6e 67 22 2c 20 7a 46 61 75 6c 74 29 3b  iting", zFault);
1f4b3 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
1f4b4 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
1f4b5 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
1f4b6 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
1f4b7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
1f4b8 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29  treeLeaveAll(db)
1f4b9 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62  ;.        goto b
1f4ba 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20  lob_open_out;.  
1f4bb 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
1f4bc 20 76 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65   v = sqlite3Vdbe
1f4bd 43 72 65 61 74 65 28 64 62 29 3b 0a 20 20 20 20  Create(db);.    
1f4be 69 66 28 20 76 20 29 7b 0a 20 20 20 20 20 20 69  if( v ){.      i
1f4bf 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  nt iDb = sqlite3
1f4c0 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62  SchemaToIndex(db
1f4c1 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
1f4c2 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1f4c3 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20  dbeAddOpList(v, 
1f4c4 73 69 7a 65 6f 66 28 6f 70 65 6e 42 6c 6f 62 29  sizeof(openBlob)
1f4c5 2f 73 69 7a 65 6f 66 28 56 64 62 65 4f 70 4c 69  /sizeof(VdbeOpLi
1f4c6 73 74 29 2c 20 6f 70 65 6e 42 6c 6f 62 29 3b 0a  st), openBlob);.
1f4c7 20 20 20 20 20 20 66 6c 61 67 73 20 3d 20 21 21        flags = !!
1f4c8 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
1f4c9 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20         /* flags 
1f4ca 3d 20 28 66 6c 61 67 73 20 3f 20 31 20 3a 20 30  = (flags ? 1 : 0
1f4cb 29 3b 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20  ); */..      /* 
1f4cc 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50  Configure the OP
1f4cd 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a  _Transaction */.
1f4ce 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f4cf 65 43 68 61 6e 67 65 50 31 28 76 2c 20 30 2c 20  eChangeP1(v, 0, 
1f4d0 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  iDb);.      sqli
1f4d1 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
1f4d2 76 2c 20 30 2c 20 66 6c 61 67 73 29 3b 0a 0a 20  v, 0, flags);.. 
1f4d3 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72       /* Configur
1f4d4 65 20 74 68 65 20 4f 50 5f 56 65 72 69 66 79 43  e the OP_VerifyC
1f4d5 6f 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 20 20 73  ookie */.      s
1f4d6 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1f4d7 50 31 28 76 2c 20 31 2c 20 69 44 62 29 3b 0a 20  P1(v, 1, iDb);. 
1f4d8 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f4d9 43 68 61 6e 67 65 50 32 28 76 2c 20 31 2c 20 70  ChangeP2(v, 1, p
1f4da 54 61 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63  Tab->pSchema->sc
1f4db 68 65 6d 61 5f 63 6f 6f 6b 69 65 29 3b 0a 0a 20  hema_cookie);.. 
1f4dc 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72       /* Make sur
1f4dd 65 20 61 20 6d 75 74 65 78 20 69 73 20 68 65 6c  e a mutex is hel
1f4de 64 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 74  d on the table t
1f4df 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 2a 2f  o be accessed */
1f4e0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1f4e1 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69  beUsesBtree(v, i
1f4e2 44 62 29 3b 20 0a 0a 20 20 20 20 20 20 2f 2a 20  Db); ..      /* 
1f4e3 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50  Configure the OP
1f4e4 5f 54 61 62 6c 65 4c 6f 63 6b 20 69 6e 73 74 72  _TableLock instr
1f4e5 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  uction */.      
1f4e6 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1f4e7 65 50 31 28 76 2c 20 32 2c 20 69 44 62 29 3b 0a  eP1(v, 2, iDb);.
1f4e8 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f4e9 65 43 68 61 6e 67 65 50 32 28 76 2c 20 32 2c 20  eChangeP2(v, 2, 
1f4ea 70 54 61 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20  pTab->tnum);.   
1f4eb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1f4ec 61 6e 67 65 50 33 28 76 2c 20 32 2c 20 66 6c 61  angeP3(v, 2, fla
1f4ed 67 73 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  gs);.      sqlit
1f4ee 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
1f4ef 2c 20 32 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  , 2, pTab->zName
1f4f0 2c 20 50 34 5f 54 52 41 4e 53 49 45 4e 54 29 3b  , P4_TRANSIENT);
1f4f1 0a 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76  ..      /* Remov
1f4f2 65 20 65 69 74 68 65 72 20 74 68 65 20 4f 50 5f  e either the OP_
1f4f3 4f 70 65 6e 57 72 69 74 65 20 6f 72 20 4f 70 65  OpenWrite or Ope
1f4f4 6e 52 65 61 64 2e 20 53 65 74 20 74 68 65 20 50  nRead. Set the P
1f4f5 32 20 0a 20 20 20 20 20 20 2a 2a 20 70 61 72 61  2 .      ** para
1f4f6 6d 65 74 65 72 20 6f 66 20 74 68 65 20 6f 74 68  meter of the oth
1f4f7 65 72 20 74 6f 20 70 54 61 62 2d 3e 74 6e 75 6d  er to pTab->tnum
1f4f8 2e 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  .  */.      sqli
1f4f9 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e  te3VdbeChangeToN
1f4fa 6f 6f 70 28 76 2c 20 34 20 2d 20 66 6c 61 67 73  oop(v, 4 - flags
1f4fb 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
1f4fc 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
1f4fd 76 2c 20 33 20 2b 20 66 6c 61 67 73 2c 20 70 54  v, 3 + flags, pT
1f4fe 61 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20  ab->tnum);.     
1f4ff 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1f500 67 65 50 33 28 76 2c 20 33 20 2b 20 66 6c 61 67  geP3(v, 3 + flag
1f501 73 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 20 20  s, iDb);..      
1f502 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65  /* Configure the
1f503 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
1f504 6e 73 2e 20 43 6f 6e 66 69 67 75 72 65 20 74 68  ns. Configure th
1f505 65 20 63 75 72 73 6f 72 20 74 6f 0a 20 20 20 20  e cursor to.    
1f506 20 20 2a 2a 20 74 68 69 6e 6b 20 74 68 61 74 20    ** think that 
1f507 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 6f 6e  the table has on
1f508 65 20 6d 6f 72 65 20 63 6f 6c 75 6d 6e 20 74 68  e more column th
1f509 61 6e 20 69 74 20 72 65 61 6c 6c 79 0a 20 20 20  an it really.   
1f50a 20 20 20 2a 2a 20 64 6f 65 73 2e 20 41 6e 20 4f     ** does. An O
1f50b 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 72 65 74 72  P_Column to retr
1f50c 69 65 76 65 20 74 68 69 73 20 69 6d 61 67 69 6e  ieve this imagin
1f50d 61 72 79 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 0a  ary column will.
1f50e 20 20 20 20 20 20 2a 2a 20 61 6c 77 61 79 73 20        ** always 
1f50f 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 20 4e 55  return an SQL NU
1f510 4c 4c 2e 20 54 68 69 73 20 69 73 20 75 73 65 66  LL. This is usef
1f511 75 6c 20 62 65 63 61 75 73 65 20 69 74 20 6d 65  ul because it me
1f512 61 6e 73 0a 20 20 20 20 20 20 2a 2a 20 77 65 20  ans.      ** we 
1f513 63 61 6e 20 69 6e 76 6f 6b 65 20 4f 50 5f 43 6f  can invoke OP_Co
1f514 6c 75 6d 6e 20 74 6f 20 66 69 6c 6c 20 69 6e 20  lumn to fill in 
1f515 74 68 65 20 76 64 62 65 20 63 75 72 73 6f 72 73  the vdbe cursors
1f516 20 74 79 70 65 20 0a 20 20 20 20 20 20 2a 2a 20   type .      ** 
1f517 61 6e 64 20 6f 66 66 73 65 74 20 63 61 63 68 65  and offset cache
1f518 20 77 69 74 68 6f 75 74 20 63 61 75 73 69 6e 67   without causing
1f519 20 61 6e 79 20 49 4f 2e 0a 20 20 20 20 20 20 2a   any IO..      *
1f51a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  /.      sqlite3V
1f51b 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 33  dbeChangeP4(v, 3
1f51c 2b 66 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f 49  +flags, SQLITE_I
1f51d 4e 54 5f 54 4f 5f 50 54 52 28 70 54 61 62 2d 3e  NT_TO_PTR(pTab->
1f51e 6e 43 6f 6c 2b 31 29 2c 50 34 5f 49 4e 54 33 32  nCol+1),P4_INT32
1f51f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1f520 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20  VdbeChangeP2(v, 
1f521 37 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a  7, pTab->nCol);.
1f522 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d        if( !db->m
1f523 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1f524 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1f525 62 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 31  beMakeReady(v, 1
1f526 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 29  , 1, 1, 0, 0, 0)
1f527 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1f528 20 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33 42     .    sqlite3B
1f529 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29  treeLeaveAll(db)
1f52a 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1f52b 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
1f52c 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72  .    if( NEVER(r
1f52d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 7c 7c  c!=SQLITE_OK) ||
1f52e 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1f52f 64 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  d ){.      goto 
1f530 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20  blob_open_out;. 
1f531 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
1f532 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 28 73 71  3_bind_int64((sq
1f533 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 2c 20  lite3_stmt *)v, 
1f534 31 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 63  1, iRow);.    rc
1f535 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
1f536 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29  (sqlite3_stmt *)
1f537 76 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  v);.    if( rc!=
1f538 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
1f539 20 20 20 20 6e 41 74 74 65 6d 70 74 2b 2b 3b 0a      nAttempt++;.
1f53a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1f53b 65 33 5f 66 69 6e 61 6c 69 7a 65 28 28 73 71 6c  e3_finalize((sql
1f53c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a  ite3_stmt *)v);.
1f53d 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1f53e 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20  ree(db, zErr);. 
1f53f 20 20 20 20 20 7a 45 72 72 20 3d 20 73 71 6c 69       zErr = sqli
1f540 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 73  te3MPrintf(db, s
1f541 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62  qlite3_errmsg(db
1f542 29 29 3b 0a 20 20 20 20 20 20 76 20 3d 20 30 3b  ));.      v = 0;
1f543 0a 20 20 20 20 7d 0a 20 20 7d 20 77 68 69 6c 65  .    }.  } while
1f544 28 20 6e 41 74 74 65 6d 70 74 3c 35 20 26 26 20  ( nAttempt<5 && 
1f545 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d  rc==SQLITE_SCHEM
1f546 41 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d  A );..  if( rc==
1f547 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
1f548 20 20 2f 2a 20 54 68 65 20 72 6f 77 2d 72 65 63    /* The row-rec
1f549 6f 72 64 20 68 61 73 20 62 65 65 6e 20 6f 70 65  ord has been ope
1f54a 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79  ned successfully
1f54b 2e 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  . Check that the
1f54c 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69  .    ** column i
1f54d 6e 20 71 75 65 73 74 69 6f 6e 20 63 6f 6e 74 61  n question conta
1f54e 69 6e 73 20 74 65 78 74 20 6f 72 20 61 20 62 6c  ins text or a bl
1f54f 6f 62 2e 20 49 66 20 69 74 20 63 6f 6e 74 61 69  ob. If it contai
1f550 6e 73 0a 20 20 20 20 2a 2a 20 74 65 78 74 2c 20  ns.    ** text, 
1f551 69 74 20 69 73 20 75 70 20 74 6f 20 74 68 65 20  it is up to the 
1f552 63 61 6c 6c 65 72 20 74 6f 20 67 65 74 20 74 68  caller to get th
1f553 65 20 65 6e 63 6f 64 69 6e 67 20 72 69 67 68 74  e encoding right
1f554 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e 63  ..    */.    Inc
1f555 72 62 6c 6f 62 20 2a 70 42 6c 6f 62 3b 0a 20 20  rblob *pBlob;.  
1f556 20 20 75 33 32 20 74 79 70 65 20 3d 20 76 2d 3e    u32 type = v->
1f557 61 70 43 73 72 5b 30 5d 2d 3e 61 54 79 70 65 5b  apCsr[0]->aType[
1f558 69 43 6f 6c 5d 3b 0a 0a 20 20 20 20 69 66 28 20  iCol];..    if( 
1f559 74 79 70 65 3c 31 32 20 29 7b 0a 20 20 20 20 20  type<12 ){.     
1f55a 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1f55b 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20  b, zErr);.      
1f55c 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50  zErr = sqlite3MP
1f55d 72 69 6e 74 66 28 64 62 2c 20 22 63 61 6e 6e 6f  rintf(db, "canno
1f55e 74 20 6f 70 65 6e 20 76 61 6c 75 65 20 6f 66 20  t open value of 
1f55f 74 79 70 65 20 25 73 22 2c 0a 20 20 20 20 20 20  type %s",.      
1f560 20 20 20 20 74 79 70 65 3d 3d 30 3f 22 6e 75 6c      type==0?"nul
1f561 6c 22 3a 20 74 79 70 65 3d 3d 37 3f 22 72 65 61  l": type==7?"rea
1f562 6c 22 3a 20 22 69 6e 74 65 67 65 72 22 0a 20 20  l": "integer".  
1f563 20 20 20 20 29 3b 0a 20 20 20 20 20 20 72 63 20      );.      rc 
1f564 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
1f565 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f        goto blob_
1f566 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  open_out;.    }.
1f567 20 20 20 20 70 42 6c 6f 62 20 3d 20 28 49 6e 63      pBlob = (Inc
1f568 72 62 6c 6f 62 20 2a 29 73 71 6c 69 74 65 33 44  rblob *)sqlite3D
1f569 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
1f56a 73 69 7a 65 6f 66 28 49 6e 63 72 62 6c 6f 62 29  sizeof(Incrblob)
1f56b 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d  );.    if( db->m
1f56c 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1f56d 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1f56e 65 65 28 64 62 2c 20 70 42 6c 6f 62 29 3b 0a 20  ee(db, pBlob);. 
1f56f 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f       goto blob_o
1f570 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  pen_out;.    }. 
1f571 20 20 20 70 42 6c 6f 62 2d 3e 66 6c 61 67 73 20     pBlob->flags 
1f572 3d 20 66 6c 61 67 73 3b 0a 20 20 20 20 70 42 6c  = flags;.    pBl
1f573 6f 62 2d 3e 70 43 73 72 20 3d 20 20 76 2d 3e 61  ob->pCsr =  v->a
1f574 70 43 73 72 5b 30 5d 2d 3e 70 43 75 72 73 6f 72  pCsr[0]->pCursor
1f575 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
1f576 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 42  eeEnterCursor(pB
1f577 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20  lob->pCsr);.    
1f578 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68  sqlite3BtreeCach
1f579 65 4f 76 65 72 66 6c 6f 77 28 70 42 6c 6f 62 2d  eOverflow(pBlob-
1f57a 3e 70 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69  >pCsr);.    sqli
1f57b 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72  te3BtreeLeaveCur
1f57c 73 6f 72 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29  sor(pBlob->pCsr)
1f57d 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 53 74  ;.    pBlob->pSt
1f57e 6d 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74  mt = (sqlite3_st
1f57f 6d 74 20 2a 29 76 3b 0a 20 20 20 20 70 42 6c 6f  mt *)v;.    pBlo
1f580 62 2d 3e 69 4f 66 66 73 65 74 20 3d 20 76 2d 3e  b->iOffset = v->
1f581 61 70 43 73 72 5b 30 5d 2d 3e 61 4f 66 66 73 65  apCsr[0]->aOffse
1f582 74 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 70 42 6c  t[iCol];.    pBl
1f583 6f 62 2d 3e 6e 42 79 74 65 20 3d 20 73 71 6c 69  ob->nByte = sqli
1f584 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
1f585 65 4c 65 6e 28 74 79 70 65 29 3b 0a 20 20 20 20  eLen(type);.    
1f586 70 42 6c 6f 62 2d 3e 64 62 20 3d 20 64 62 3b 0a  pBlob->db = db;.
1f587 20 20 20 20 2a 70 70 42 6c 6f 62 20 3d 20 28 73      *ppBlob = (s
1f588 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29 70 42  qlite3_blob *)pB
1f589 6c 6f 62 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  lob;.    rc = SQ
1f58a 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65  LITE_OK;.  }else
1f58b 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1f58c 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
1f58d 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
1f58e 29 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20 73 71  );.    zErr = sq
1f58f 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
1f590 20 22 6e 6f 20 73 75 63 68 20 72 6f 77 69 64 3a   "no such rowid:
1f591 20 25 6c 6c 64 22 2c 20 69 52 6f 77 29 3b 0a 20   %lld", iRow);. 
1f592 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
1f593 52 52 4f 52 3b 0a 20 20 7d 0a 0a 62 6c 6f 62 5f  RROR;.  }..blob_
1f594 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20  open_out:.  if( 
1f595 76 20 26 26 20 28 72 63 21 3d 53 51 4c 49 54 45  v && (rc!=SQLITE
1f596 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  _OK || db->mallo
1f597 63 46 61 69 6c 65 64 29 20 29 7b 0a 20 20 20 20  cFailed) ){.    
1f598 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c  sqlite3VdbeFinal
1f599 69 7a 65 28 76 29 3b 0a 20 20 7d 0a 20 20 73 71  ize(v);.  }.  sq
1f59a 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72  lite3Error(db, r
1f59b 63 2c 20 7a 45 72 72 29 3b 0a 20 20 73 71 6c 69  c, zErr);.  sqli
1f59c 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45  te3DbFree(db, zE
1f59d 72 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 74  rr);.  sqlite3St
1f59e 61 63 6b 46 72 65 65 28 64 62 2c 20 70 50 61 72  ackFree(db, pPar
1f59f 73 65 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  se);.  rc = sqli
1f5a0 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
1f5a1 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  c);.  sqlite3_mu
1f5a2 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
1f5a3 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
1f5a4 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  c;.}../*.** Clos
1f5a5 65 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20  e a blob handle 
1f5a6 74 68 61 74 20 77 61 73 20 70 72 65 76 69 6f 75  that was previou
1f5a7 73 6c 79 20 63 72 65 61 74 65 64 20 75 73 69 6e  sly created usin
1f5a8 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 6c 6f  g.** sqlite3_blo
1f5a9 62 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 53 51 4c  b_open()..*/.SQL
1f5aa 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1f5ab 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 73  te3_blob_close(s
1f5ac 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c  qlite3_blob *pBl
1f5ad 6f 62 29 7b 0a 20 20 49 6e 63 72 62 6c 6f 62 20  ob){.  Incrblob 
1f5ae 2a 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a  *p = (Incrblob *
1f5af 29 70 42 6c 6f 62 3b 0a 20 20 69 6e 74 20 72 63  )pBlob;.  int rc
1f5b0 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ;.  sqlite3 *db;
1f5b1 0a 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ..  if( p ){.   
1f5b2 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20   db = p->db;.   
1f5b3 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1f5b4 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
1f5b5 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1f5b6 33 5f 66 69 6e 61 6c 69 7a 65 28 70 2d 3e 70 53  3_finalize(p->pS
1f5b7 74 6d 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  tmt);.    sqlite
1f5b8 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a  3DbFree(db, p);.
1f5b9 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1f5ba 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
1f5bb 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  x);.  }else{.   
1f5bc 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1f5bd 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1f5be 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f  ;.}../*.** Perfo
1f5bf 72 6d 20 61 20 72 65 61 64 20 6f 72 20 77 72 69  rm a read or wri
1f5c0 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20  te operation on 
1f5c1 61 20 62 6c 6f 62 0a 2a 2f 0a 73 74 61 74 69 63  a blob.*/.static
1f5c2 20 69 6e 74 20 62 6c 6f 62 52 65 61 64 57 72 69   int blobReadWri
1f5c3 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c  te(.  sqlite3_bl
1f5c4 6f 62 20 2a 70 42 6c 6f 62 2c 20 0a 20 20 76 6f  ob *pBlob, .  vo
1f5c5 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c  id *z, .  int n,
1f5c6 20 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 2c   .  int iOffset,
1f5c7 20 0a 20 20 69 6e 74 20 28 2a 78 43 61 6c 6c 29   .  int (*xCall)
1f5c8 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 2c  (BtCursor*, u32,
1f5c9 20 75 33 32 2c 20 76 6f 69 64 2a 29 0a 29 7b 0a   u32, void*).){.
1f5ca 20 20 69 6e 74 20 72 63 3b 0a 20 20 49 6e 63 72    int rc;.  Incr
1f5cb 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62  blob *p = (Incrb
1f5cc 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 56  lob *)pBlob;.  V
1f5cd 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65  dbe *v;.  sqlite
1f5ce 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 70 3d  3 *db;..  if( p=
1f5cf 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  =0 ) return SQLI
1f5d0 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 64 62 20  TE_MISUSE;.  db 
1f5d1 3d 20 70 2d 3e 64 62 3b 0a 20 20 73 71 6c 69 74  = p->db;.  sqlit
1f5d2 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
1f5d3 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 76 20 3d  b->mutex);.  v =
1f5d4 20 28 56 64 62 65 2a 29 70 2d 3e 70 53 74 6d 74   (Vdbe*)p->pStmt
1f5d5 3b 0a 0a 20 20 69 66 28 20 6e 3c 30 20 7c 7c 20  ;..  if( n<0 || 
1f5d6 69 4f 66 66 73 65 74 3c 30 20 7c 7c 20 28 69 4f  iOffset<0 || (iO
1f5d7 66 66 73 65 74 2b 6e 29 3e 70 2d 3e 6e 42 79 74  ffset+n)>p->nByt
1f5d8 65 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65 71 75  e ){.    /* Requ
1f5d9 65 73 74 20 69 73 20 6f 75 74 20 6f 66 20 72 61  est is out of ra
1f5da 6e 67 65 2e 20 52 65 74 75 72 6e 20 61 20 74 72  nge. Return a tr
1f5db 61 6e 73 69 65 6e 74 20 65 72 72 6f 72 2e 20 2a  ansient error. *
1f5dc 2f 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  /.    rc = SQLIT
1f5dd 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c  E_ERROR;.    sql
1f5de 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
1f5df 4c 49 54 45 5f 45 52 52 4f 52 2c 20 30 29 3b 0a  LITE_ERROR, 0);.
1f5e0 20 20 7d 20 65 6c 73 65 20 69 66 28 20 76 3d 3d    } else if( v==
1f5e1 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74  0 ){.    /* If t
1f5e2 68 65 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65  here is no state
1f5e3 6d 65 6e 74 20 68 61 6e 64 6c 65 2c 20 74 68 65  ment handle, the
1f5e4 6e 20 74 68 65 20 62 6c 6f 62 2d 68 61 6e 64 6c  n the blob-handl
1f5e5 65 20 68 61 73 0a 20 20 20 20 2a 2a 20 61 6c 72  e has.    ** alr
1f5e6 65 61 64 79 20 62 65 65 6e 20 69 6e 76 61 6c 69  eady been invali
1f5e7 64 61 74 65 64 2e 20 52 65 74 75 72 6e 20 53 51  dated. Return SQ
1f5e8 4c 49 54 45 5f 41 42 4f 52 54 20 69 6e 20 74 68  LITE_ABORT in th
1f5e9 69 73 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a  is case..    */.
1f5ea 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1f5eb 41 42 4f 52 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ABORT;.  }else{.
1f5ec 20 20 20 20 2f 2a 20 43 61 6c 6c 20 65 69 74 68      /* Call eith
1f5ed 65 72 20 42 74 72 65 65 44 61 74 61 28 29 20 6f  er BtreeData() o
1f5ee 72 20 42 74 72 65 65 50 75 74 44 61 74 61 28 29  r BtreePutData()
1f5ef 2e 20 49 66 20 53 51 4c 49 54 45 5f 41 42 4f 52  . If SQLITE_ABOR
1f5f0 54 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 74 75  T is.    ** retu
1f5f1 72 6e 65 64 2c 20 63 6c 65 61 6e 2d 75 70 20 74  rned, clean-up t
1f5f2 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e  he statement han
1f5f3 64 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  dle..    */.    
1f5f4 61 73 73 65 72 74 28 20 64 62 20 3d 3d 20 76 2d  assert( db == v-
1f5f5 3e 64 62 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  >db );.    sqlit
1f5f6 65 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73  e3BtreeEnterCurs
1f5f7 6f 72 28 70 2d 3e 70 43 73 72 29 3b 0a 20 20 20  or(p->pCsr);.   
1f5f8 20 72 63 20 3d 20 78 43 61 6c 6c 28 70 2d 3e 70   rc = xCall(p->p
1f5f9 43 73 72 2c 20 69 4f 66 66 73 65 74 2b 70 2d 3e  Csr, iOffset+p->
1f5fa 69 4f 66 66 73 65 74 2c 20 6e 2c 20 7a 29 3b 0a  iOffset, n, z);.
1f5fb 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1f5fc 4c 65 61 76 65 43 75 72 73 6f 72 28 70 2d 3e 70  LeaveCursor(p->p
1f5fd 43 73 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63  Csr);.    if( rc
1f5fe 3d 3d 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 29  ==SQLITE_ABORT )
1f5ff 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1f600 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a  dbeFinalize(v);.
1f601 20 20 20 20 20 20 70 2d 3e 70 53 74 6d 74 20 3d        p->pStmt =
1f602 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
1f603 20 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65       db->errCode
1f604 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 76 2d 3e   = rc;.      v->
1f605 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 7d 0a 20  rc = rc;.    }. 
1f606 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65   }.  rc = sqlite
1f607 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29  3ApiExit(db, rc)
1f608 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
1f609 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
1f60a 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
1f60b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64  .}../*.** Read d
1f60c 61 74 61 20 66 72 6f 6d 20 61 20 62 6c 6f 62 20  ata from a blob 
1f60d 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  handle..*/.SQLIT
1f60e 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1f60f 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69  3_blob_read(sqli
1f610 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c  te3_blob *pBlob,
1f611 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c   void *z, int n,
1f612 20 69 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20   int iOffset){. 
1f613 20 72 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64   return blobRead
1f614 57 72 69 74 65 28 70 42 6c 6f 62 2c 20 7a 2c 20  Write(pBlob, z, 
1f615 6e 2c 20 69 4f 66 66 73 65 74 2c 20 73 71 6c 69  n, iOffset, sqli
1f616 74 65 33 42 74 72 65 65 44 61 74 61 29 3b 0a 7d  te3BtreeData);.}
1f617 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61  ../*.** Write da
1f618 74 61 20 74 6f 20 61 20 62 6c 6f 62 20 68 61 6e  ta to a blob han
1f619 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  dle..*/.SQLITE_A
1f61a 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
1f61b 6c 6f 62 5f 77 72 69 74 65 28 73 71 6c 69 74 65  lob_write(sqlite
1f61c 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63  3_blob *pBlob, c
1f61d 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e  onst void *z, in
1f61e 74 20 6e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74  t n, int iOffset
1f61f 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 6c 6f 62  ){.  return blob
1f620 52 65 61 64 57 72 69 74 65 28 70 42 6c 6f 62 2c  ReadWrite(pBlob,
1f621 20 28 76 6f 69 64 20 2a 29 7a 2c 20 6e 2c 20 69   (void *)z, n, i
1f622 4f 66 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42  Offset, sqlite3B
1f623 74 72 65 65 50 75 74 44 61 74 61 29 3b 0a 7d 0a  treePutData);.}.
1f624 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 61 20 62  ./*.** Query a b
1f625 6c 6f 62 20 68 61 6e 64 6c 65 20 66 6f 72 20 74  lob handle for t
1f626 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64  he size of the d
1f627 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49  ata..**.** The I
1f628 6e 63 72 62 6c 6f 62 2e 6e 42 79 74 65 20 66 69  ncrblob.nByte fi
1f629 65 6c 64 20 69 73 20 66 69 78 65 64 20 66 6f 72  eld is fixed for
1f62a 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66   the lifetime of
1f62b 20 74 68 65 20 49 6e 63 72 62 6c 6f 62 0a 2a 2a   the Incrblob.**
1f62c 20 73 6f 20 6e 6f 20 6d 75 74 65 78 20 69 73 20   so no mutex is 
1f62d 72 65 71 75 69 72 65 64 20 66 6f 72 20 61 63 63  required for acc
1f62e 65 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ess..*/.SQLITE_A
1f62f 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
1f630 6c 6f 62 5f 62 79 74 65 73 28 73 71 6c 69 74 65  lob_bytes(sqlite
1f631 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a  3_blob *pBlob){.
1f632 20 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20    Incrblob *p = 
1f633 28 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f  (Incrblob *)pBlo
1f634 62 3b 0a 20 20 72 65 74 75 72 6e 20 70 20 3f 20  b;.  return p ? 
1f635 70 2d 3e 6e 42 79 74 65 20 3a 20 30 3b 0a 7d 0a  p->nByte : 0;.}.
1f636 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64  .#endif /* #ifnd
1f637 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
1f638 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 2a 2a  NCRBLOB */../***
1f639 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
1f63a 6f 66 20 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a  of vdbeblob.c **
1f63b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f63c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f63d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
1f63e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
1f63f 6e 20 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63  n file journal.c
1f640 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1f641 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f642 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
1f643 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 32  * 2007 August 22
1f644 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
1f645 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1f646 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1f647 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
1f648 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
1f649 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
1f64a 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
1f64b 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
1f64c 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
1f64d 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
1f64e 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1f64f 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1f650 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
1f651 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
1f652 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
1f653 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
1f654 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
1f655 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
1f656 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f657 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f658 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f659 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1f65a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20  **.** @(#) $Id: 
1f65b 6a 6f 75 72 6e 61 6c 2e 63 2c 76 20 31 2e 39 20  journal.c,v 1.9 
1f65c 32 30 30 39 2f 30 31 2f 32 30 20 31 37 3a 30 36  2009/01/20 17:06
1f65d 3a 32 37 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :27 danielk1977 
1f65e 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 64 65 66  Exp $.*/..#ifdef
1f65f 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41   SQLITE_ENABLE_A
1f660 54 4f 4d 49 43 5f 57 52 49 54 45 0a 0a 2f 2a 0a  TOMIC_WRITE../*.
1f661 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70  ** This file imp
1f662 6c 65 6d 65 6e 74 73 20 61 20 73 70 65 63 69 61  lements a specia
1f663 6c 20 6b 69 6e 64 20 6f 66 20 73 71 6c 69 74 65  l kind of sqlite
1f664 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 20 75 73  3_file object us
1f665 65 64 0a 2a 2a 20 62 79 20 53 51 4c 69 74 65 20  ed.** by SQLite 
1f666 74 6f 20 63 72 65 61 74 65 20 6a 6f 75 72 6e 61  to create journa
1f667 6c 20 66 69 6c 65 73 20 69 66 20 74 68 65 20 61  l files if the a
1f668 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69  tomic-write opti
1f669 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 65  mization.** is e
1f66a 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  nabled..**.** Th
1f66b 65 20 64 69 73 74 69 6e 63 74 69 76 65 20 63 68  e distinctive ch
1f66c 61 72 61 63 74 65 72 69 73 74 69 63 20 6f 66 20  aracteristic of 
1f66d 74 68 69 73 20 73 71 6c 69 74 65 33 5f 66 69 6c  this sqlite3_fil
1f66e 65 20 69 73 20 74 68 61 74 20 74 68 65 0a 2a 2a  e is that the.**
1f66f 20 61 63 74 75 61 6c 20 6f 6e 20 64 69 73 6b 20   actual on disk 
1f670 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 20  file is created 
1f671 6c 61 7a 69 6c 79 2e 20 57 68 65 6e 20 74 68 65  lazily. When the
1f672 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64   file is created
1f673 2c 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20  ,.** the caller 
1f674 73 70 65 63 69 66 69 65 73 20 61 20 62 75 66 66  specifies a buff
1f675 65 72 20 73 69 7a 65 20 66 6f 72 20 61 6e 20 69  er size for an i
1f676 6e 2d 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20  n-memory buffer 
1f677 74 6f 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f  to.** be used to
1f678 20 73 65 72 76 69 63 65 20 72 65 61 64 28 29 20   service read() 
1f679 61 6e 64 20 77 72 69 74 65 28 29 20 72 65 71 75  and write() requ
1f67a 65 73 74 73 2e 20 54 68 65 20 61 63 74 75 61 6c  ests. The actual
1f67b 20 66 69 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b   file.** on disk
1f67c 20 69 73 20 6e 6f 74 20 63 72 65 61 74 65 64 20   is not created 
1f67d 6f 72 20 70 6f 70 75 6c 61 74 65 64 20 75 6e 74  or populated unt
1f67e 69 6c 20 65 69 74 68 65 72 3a 0a 2a 2a 0a 2a 2a  il either:.**.**
1f67f 20 20 20 31 29 20 54 68 65 20 69 6e 2d 6d 65 6d     1) The in-mem
1f680 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69  ory representati
1f681 6f 6e 20 67 72 6f 77 73 20 74 6f 6f 20 6c 61 72  on grows too lar
1f682 67 65 20 66 6f 72 20 74 68 65 20 61 6c 6c 6f 63  ge for the alloc
1f683 61 74 65 64 20 0a 2a 2a 20 20 20 20 20 20 62 75  ated .**      bu
1f684 66 66 65 72 2c 20 6f 72 0a 2a 2a 20 20 20 32 29  ffer, or.**   2)
1f685 20 54 68 65 20 73 71 6c 69 74 65 33 4a 6f 75 72   The sqlite3Jour
1f686 6e 61 6c 43 72 65 61 74 65 28 29 20 66 75 6e 63  nalCreate() func
1f687 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a  tion is called..
1f688 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4a 6f  */..../*.** A Jo
1f689 75 72 6e 61 6c 46 69 6c 65 20 6f 62 6a 65 63 74  urnalFile object
1f68a 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f   is a subclass o
1f68b 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 75  f sqlite3_file u
1f68c 73 65 64 20 62 79 0a 2a 2a 20 61 73 20 61 6e 20  sed by.** as an 
1f68d 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65  open file handle
1f68e 20 66 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   for journal fil
1f68f 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4a 6f  es..*/.struct Jo
1f690 75 72 6e 61 6c 46 69 6c 65 20 7b 0a 20 20 73 71  urnalFile {.  sq
1f691 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
1f692 20 2a 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f 2a   *pMethod;    /*
1f693 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 6f 6e 20   I/O methods on 
1f694 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f  journal files */
1f695 0a 20 20 69 6e 74 20 6e 42 75 66 3b 20 20 20 20  .  int nBuf;    
1f696 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f697 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 42     /* Size of zB
1f698 75 66 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f  uf[] in bytes */
1f699 0a 20 20 63 68 61 72 20 2a 7a 42 75 66 3b 20 20  .  char *zBuf;  
1f69a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f69b 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 62     /* Space to b
1f69c 75 66 66 65 72 20 6a 6f 75 72 6e 61 6c 20 77 72  uffer journal wr
1f69d 69 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 53  ites */.  int iS
1f69e 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
1f69f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f            /* Amo
1f6a0 75 6e 74 20 6f 66 20 7a 42 75 66 5b 5d 20 63 75  unt of zBuf[] cu
1f6a1 72 72 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a  rrently used */.
1f6a2 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20    int flags;    
1f6a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f6a4 20 20 2f 2a 20 78 4f 70 65 6e 20 66 6c 61 67 73    /* xOpen flags
1f6a5 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66   */.  sqlite3_vf
1f6a6 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20  s *pVfs;        
1f6a7 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65        /* The "re
1f6a8 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 56  al" underlying V
1f6a9 46 53 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  FS */.  sqlite3_
1f6aa 66 69 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20  file *pReal;    
1f6ab 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22          /* The "
1f6ac 72 65 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67  real" underlying
1f6ad 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
1f6ae 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
1f6af 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20   *zJournal;     
1f6b0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
1f6b1 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
1f6b2 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20  e */.};.typedef 
1f6b3 73 74 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 69  struct JournalFi
1f6b4 6c 65 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 3b 0a  le JournalFile;.
1f6b5 0a 2f 2a 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65  ./*.** If it doe
1f6b6 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78  s not already ex
1f6b7 69 73 74 73 2c 20 63 72 65 61 74 65 20 61 6e 64  ists, create and
1f6b8 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 6e   populate the on
1f6b9 2d 64 69 73 6b 20 66 69 6c 65 20 0a 2a 2a 20 66  -disk file .** f
1f6ba 6f 72 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 70  or JournalFile p
1f6bb 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1f6bc 63 72 65 61 74 65 46 69 6c 65 28 4a 6f 75 72 6e  createFile(Journ
1f6bd 61 6c 46 69 6c 65 20 2a 70 29 7b 0a 20 20 69 6e  alFile *p){.  in
1f6be 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
1f6bf 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61  ;.  if( !p->pRea
1f6c0 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  l ){.    sqlite3
1f6c1 5f 66 69 6c 65 20 2a 70 52 65 61 6c 20 3d 20 28  _file *pReal = (
1f6c2 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 26  sqlite3_file *)&
1f6c3 70 5b 31 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73  p[1];.    rc = s
1f6c4 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 2d 3e  qlite3OsOpen(p->
1f6c5 70 56 66 73 2c 20 70 2d 3e 7a 4a 6f 75 72 6e 61  pVfs, p->zJourna
1f6c6 6c 2c 20 70 52 65 61 6c 2c 20 70 2d 3e 66 6c 61  l, pReal, p->fla
1f6c7 67 73 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  gs, 0);.    if( 
1f6c8 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1f6c9 0a 20 20 20 20 20 20 70 2d 3e 70 52 65 61 6c 20  .      p->pReal 
1f6ca 3d 20 70 52 65 61 6c 3b 0a 20 20 20 20 20 20 69  = pReal;.      i
1f6cb 66 28 20 70 2d 3e 69 53 69 7a 65 3e 30 20 29 7b  f( p->iSize>0 ){
1f6cc 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1f6cd 70 2d 3e 69 53 69 7a 65 3c 3d 70 2d 3e 6e 42 75  p->iSize<=p->nBu
1f6ce 66 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  f);.        rc =
1f6cf 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
1f6d0 70 2d 3e 70 52 65 61 6c 2c 20 70 2d 3e 7a 42 75  p->pReal, p->zBu
1f6d1 66 2c 20 70 2d 3e 69 53 69 7a 65 2c 20 30 29 3b  f, p->iSize, 0);
1f6d2 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1f6d3 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1f6d4 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74  }../*.** Close t
1f6d5 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74  he file..*/.stat
1f6d6 69 63 20 69 6e 74 20 6a 72 6e 6c 43 6c 6f 73 65  ic int jrnlClose
1f6d7 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
1f6d8 4a 66 64 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46  Jfd){.  JournalF
1f6d9 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61  ile *p = (Journa
1f6da 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20  lFile *)pJfd;.  
1f6db 69 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a  if( p->pReal ){.
1f6dc 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
1f6dd 73 65 28 70 2d 3e 70 52 65 61 6c 29 3b 0a 20 20  se(p->pReal);.  
1f6de 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  }.  sqlite3_free
1f6df 28 70 2d 3e 7a 42 75 66 29 3b 0a 20 20 72 65 74  (p->zBuf);.  ret
1f6e0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1f6e1 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74  ../*.** Read dat
1f6e2 61 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e  a from the file.
1f6e3 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a  .*/.static int j
1f6e4 72 6e 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74  rnlRead(.  sqlit
1f6e5 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20  e3_file *pJfd,  
1f6e6 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c    /* The journal
1f6e7 20 66 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68   file from which
1f6e8 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f   to read */.  vo
1f6e9 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20  id *zBuf,       
1f6ea 20 20 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20       /* Put the 
1f6eb 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a  results here */.
1f6ec 20 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20    int iAmt,     
1f6ed 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1f6ee 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72  er of bytes to r
1f6ef 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f  ead */.  sqlite_
1f6f0 69 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20  int64 iOfst     
1f6f1 2f 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67  /* Begin reading
1f6f2 20 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20   at this offset 
1f6f3 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  */.){.  int rc =
1f6f4 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f   SQLITE_OK;.  Jo
1f6f5 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28  urnalFile *p = (
1f6f6 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a  JournalFile *)pJ
1f6f7 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65  fd;.  if( p->pRe
1f6f8 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  al ){.    rc = s
1f6f9 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 2d 3e  qlite3OsRead(p->
1f6fa 70 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d  pReal, zBuf, iAm
1f6fb 74 2c 20 69 4f 66 73 74 29 3b 0a 20 20 7d 65 6c  t, iOfst);.  }el
1f6fc 73 65 20 69 66 28 20 28 69 41 6d 74 2b 69 4f 66  se if( (iAmt+iOf
1f6fd 73 74 29 3e 70 2d 3e 69 53 69 7a 65 20 29 7b 0a  st)>p->iSize ){.
1f6fe 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1f6ff 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44  IOERR_SHORT_READ
1f700 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d  ;.  }else{.    m
1f701 65 6d 63 70 79 28 7a 42 75 66 2c 20 26 70 2d 3e  emcpy(zBuf, &p->
1f702 7a 42 75 66 5b 69 4f 66 73 74 5d 2c 20 69 41 6d  zBuf[iOfst], iAm
1f703 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  t);.  }.  return
1f704 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72   rc;.}../*.** Wr
1f705 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65 20  ite data to the 
1f706 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
1f707 69 6e 74 20 6a 72 6e 6c 57 72 69 74 65 28 0a 20  int jrnlWrite(. 
1f708 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
1f709 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a  Jfd,    /* The j
1f70a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f  ournal file into
1f70b 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20   which to write 
1f70c 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
1f70d 2a 7a 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54  *zBuf,      /* T
1f70e 61 6b 65 20 64 61 74 61 20 74 6f 20 62 65 20 77  ake data to be w
1f70f 72 69 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65  ritten from here
1f710 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20   */.  int iAmt, 
1f711 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f712 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1f713 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71  to write */.  sq
1f714 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74  lite_int64 iOfst
1f715 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72       /* Begin wr
1f716 69 74 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66  iting at this of
1f717 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69  fset into the fi
1f718 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  le */.){.  int r
1f719 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1f71a 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20   JournalFile *p 
1f71b 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  = (JournalFile *
1f71c 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 21 70 2d  )pJfd;.  if( !p-
1f71d 3e 70 52 65 61 6c 20 26 26 20 28 69 4f 66 73 74  >pReal && (iOfst
1f71e 2b 69 41 6d 74 29 3e 70 2d 3e 6e 42 75 66 20 29  +iAmt)>p->nBuf )
1f71f 7b 0a 20 20 20 20 72 63 20 3d 20 63 72 65 61 74  {.    rc = creat
1f720 65 46 69 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20  eFile(p);.  }.  
1f721 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1f722 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  K ){.    if( p->
1f723 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 20 20 72  pReal ){.      r
1f724 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
1f725 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 7a 42 75  te(p->pReal, zBu
1f726 66 2c 20 69 41 6d 74 2c 20 69 4f 66 73 74 29 3b  f, iAmt, iOfst);
1f727 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1f728 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a 42 75    memcpy(&p->zBu
1f729 66 5b 69 4f 66 73 74 5d 2c 20 7a 42 75 66 2c 20  f[iOfst], zBuf, 
1f72a 69 41 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28  iAmt);.      if(
1f72b 20 70 2d 3e 69 53 69 7a 65 3c 28 69 4f 66 73 74   p->iSize<(iOfst
1f72c 2b 69 41 6d 74 29 20 29 7b 0a 20 20 20 20 20 20  +iAmt) ){.      
1f72d 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 28 69 4f    p->iSize = (iO
1f72e 66 73 74 2b 69 41 6d 74 29 3b 0a 20 20 20 20 20  fst+iAmt);.     
1f72f 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
1f730 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1f731 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20  ** Truncate the 
1f732 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
1f733 69 6e 74 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65  int jrnlTruncate
1f734 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
1f735 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36  Jfd, sqlite_int6
1f736 34 20 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72  4 size){.  int r
1f737 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1f738 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20   JournalFile *p 
1f739 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  = (JournalFile *
1f73a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e  )pJfd;.  if( p->
1f73b 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20  pReal ){.    rc 
1f73c 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63  = sqlite3OsTrunc
1f73d 61 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 73 69  ate(p->pReal, si
1f73e 7a 65 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ze);.  }else if(
1f73f 20 73 69 7a 65 3c 70 2d 3e 69 53 69 7a 65 20 29   size<p->iSize )
1f740 7b 0a 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d  {.    p->iSize =
1f741 20 73 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74   size;.  }.  ret
1f742 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1f743 20 53 79 6e 63 20 74 68 65 20 66 69 6c 65 2e 0a   Sync the file..
1f744 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72  */.static int jr
1f745 6e 6c 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66  nlSync(sqlite3_f
1f746 69 6c 65 20 2a 70 4a 66 64 2c 20 69 6e 74 20 66  ile *pJfd, int f
1f747 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b  lags){.  int rc;
1f748 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  .  JournalFile *
1f749 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65  p = (JournalFile
1f74a 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70   *)pJfd;.  if( p
1f74b 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72  ->pReal ){.    r
1f74c 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e  c = sqlite3OsSyn
1f74d 63 28 70 2d 3e 70 52 65 61 6c 2c 20 66 6c 61 67  c(p->pReal, flag
1f74e 73 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  s);.  }else{.   
1f74f 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1f750 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1f751 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79  ;.}../*.** Query
1f752 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
1f753 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a   file in bytes..
1f754 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72  */.static int jr
1f755 6e 6c 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74  nlFileSize(sqlit
1f756 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73  e3_file *pJfd, s
1f757 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 53 69  qlite_int64 *pSi
1f758 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ze){.  int rc = 
1f759 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75  SQLITE_OK;.  Jou
1f75a 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a  rnalFile *p = (J
1f75b 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66  ournalFile *)pJf
1f75c 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61  d;.  if( p->pRea
1f75d 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  l ){.    rc = sq
1f75e 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
1f75f 70 2d 3e 70 52 65 61 6c 2c 20 70 53 69 7a 65 29  p->pReal, pSize)
1f760 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a  ;.  }else{.    *
1f761 70 53 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f  pSize = (sqlite_
1f762 69 6e 74 36 34 29 20 70 2d 3e 69 53 69 7a 65 3b  int64) p->iSize;
1f763 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1f764 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65  ;.}../*.** Table
1f765 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20   of methods for 
1f766 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 73 71 6c 69  JournalFile sqli
1f767 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e  te3_file object.
1f768 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63  .*/.static struc
1f769 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
1f76a 68 6f 64 73 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  hods JournalFile
1f76b 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c  Methods = {.  1,
1f76c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f76d 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6a 72  iVersion */.  jr
1f76e 6e 6c 43 6c 6f 73 65 2c 20 20 20 20 20 2f 2a 20  nlClose,     /* 
1f76f 78 43 6c 6f 73 65 20 2a 2f 0a 20 20 6a 72 6e 6c  xClose */.  jrnl
1f770 52 65 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52  Read,      /* xR
1f771 65 61 64 20 2a 2f 0a 20 20 6a 72 6e 6c 57 72 69  ead */.  jrnlWri
1f772 74 65 2c 20 20 20 20 20 2f 2a 20 78 57 72 69 74  te,     /* xWrit
1f773 65 20 2a 2f 0a 20 20 6a 72 6e 6c 54 72 75 6e 63  e */.  jrnlTrunc
1f774 61 74 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61  ate,  /* xTrunca
1f775 74 65 20 2a 2f 0a 20 20 6a 72 6e 6c 53 79 6e 63  te */.  jrnlSync
1f776 2c 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20  ,      /* xSync 
1f777 2a 2f 0a 20 20 6a 72 6e 6c 46 69 6c 65 53 69 7a  */.  jrnlFileSiz
1f778 65 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65  e,  /* xFileSize
1f779 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
1f77a 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f       /* xLock */
1f77b 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20  .  0,           
1f77c 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a    /* xUnlock */.
1f77d 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
1f77e 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76   /* xCheckReserv
1f77f 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20  edLock */.  0,  
1f780 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46             /* xF
1f781 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20  ileControl */.  
1f782 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0,             /
1f783 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f  * xSectorSize */
1f784 0a 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20  .  0            
1f785 20 20 2f 2a 20 78 44 65 76 69 63 65 43 68 61 72    /* xDeviceChar
1f786 61 63 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d  acteristics */.}
1f787 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61  ;../* .** Open a
1f788 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a   journal file..*
1f789 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1f78a 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72   int sqlite3Jour
1f78b 6e 61 6c 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74  nalOpen(.  sqlit
1f78c 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20  e3_vfs *pVfs,   
1f78d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53        /* The VFS
1f78e 20 74 6f 20 75 73 65 20 66 6f 72 20 61 63 74 75   to use for actu
1f78f 61 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20  al file I/O */. 
1f790 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
1f791 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  me,         /* N
1f792 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  ame of the journ
1f793 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c  al file */.  sql
1f794 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c  ite3_file *pJfd,
1f795 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c          /* Preal
1f796 6c 6f 63 61 74 65 64 2c 20 62 6c 61 6e 6b 20 66  located, blank f
1f797 69 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ile handle */.  
1f798 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20  int flags,      
1f799 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70             /* Op
1f79a 65 6e 69 6e 67 20 66 6c 61 67 73 20 2a 2f 0a 20  ening flags */. 
1f79b 20 69 6e 74 20 6e 42 75 66 20 20 20 20 20 20 20   int nBuf       
1f79c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
1f79d 79 74 65 73 20 62 75 66 66 65 72 65 64 20 62 65  ytes buffered be
1f79e 66 6f 72 65 20 6f 70 65 6e 69 6e 67 20 74 68 65  fore opening the
1f79f 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4a 6f   file */.){.  Jo
1f7a0 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28  urnalFile *p = (
1f7a1 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a  JournalFile *)pJ
1f7a2 66 64 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20  fd;.  memset(p, 
1f7a3 30 2c 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61  0, sqlite3Journa
1f7a4 6c 53 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20  lSize(pVfs));.  
1f7a5 69 66 28 20 6e 42 75 66 3e 30 20 29 7b 0a 20 20  if( nBuf>0 ){.  
1f7a6 20 20 70 2d 3e 7a 42 75 66 20 3d 20 73 71 6c 69    p->zBuf = sqli
1f7a7 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42  te3MallocZero(nB
1f7a8 75 66 29 3b 0a 20 20 20 20 69 66 28 20 21 70 2d  uf);.    if( !p-
1f7a9 3e 7a 42 75 66 20 29 7b 0a 20 20 20 20 20 20 72  >zBuf ){.      r
1f7aa 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
1f7ab 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  EM;.    }.  }els
1f7ac 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  e{.    return sq
1f7ad 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73  lite3OsOpen(pVfs
1f7ae 2c 20 7a 4e 61 6d 65 2c 20 70 4a 66 64 2c 20 66  , zName, pJfd, f
1f7af 6c 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  lags, 0);.  }.  
1f7b0 70 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4a 6f  p->pMethod = &Jo
1f7b1 75 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73  urnalFileMethods
1f7b2 3b 0a 20 20 70 2d 3e 6e 42 75 66 20 3d 20 6e 42  ;.  p->nBuf = nB
1f7b3 75 66 3b 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d  uf;.  p->flags =
1f7b4 20 66 6c 61 67 73 3b 0a 20 20 70 2d 3e 7a 4a 6f   flags;.  p->zJo
1f7b5 75 72 6e 61 6c 20 3d 20 7a 4e 61 6d 65 3b 0a 20  urnal = zName;. 
1f7b6 20 70 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b   p->pVfs = pVfs;
1f7b7 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1f7b8 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  _OK;.}../*.** If
1f7b9 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 70 20   the argument p 
1f7ba 70 6f 69 6e 74 73 20 74 6f 20 61 20 4a 6f 75 72  points to a Jour
1f7bb 6e 61 6c 46 69 6c 65 20 73 74 72 75 63 74 75 72  nalFile structur
1f7bc 65 2c 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72  e, and the under
1f7bd 6c 79 69 6e 67 0a 2a 2a 20 66 69 6c 65 20 68 61  lying.** file ha
1f7be 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63  s not yet been c
1f7bf 72 65 61 74 65 64 2c 20 63 72 65 61 74 65 20 69  reated, create i
1f7c0 74 20 6e 6f 77 2e 0a 2a 2f 0a 53 51 4c 49 54 45  t now..*/.SQLITE
1f7c1 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1f7c2 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74  ite3JournalCreat
1f7c3 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
1f7c4 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 4d 65  p){.  if( p->pMe
1f7c5 74 68 6f 64 73 21 3d 26 4a 6f 75 72 6e 61 6c 46  thods!=&JournalF
1f7c6 69 6c 65 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20  ileMethods ){.  
1f7c7 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1f7c8 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  OK;.  }.  return
1f7c9 20 63 72 65 61 74 65 46 69 6c 65 28 28 4a 6f 75   createFile((Jou
1f7ca 72 6e 61 6c 46 69 6c 65 20 2a 29 70 29 3b 0a 7d  rnalFile *)p);.}
1f7cb 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20  ../* .** Return 
1f7cc 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
1f7cd 74 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20  tes required to 
1f7ce 73 74 6f 72 65 20 61 20 4a 6f 75 72 6e 61 6c 46  store a JournalF
1f7cf 69 6c 65 20 74 68 61 74 20 75 73 65 73 20 76 66  ile that uses vf
1f7d0 73 0a 2a 2a 20 70 56 66 73 20 74 6f 20 63 72 65  s.** pVfs to cre
1f7d1 61 74 65 20 74 68 65 20 75 6e 64 65 72 6c 79 69  ate the underlyi
1f7d2 6e 67 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 73  ng on-disk files
1f7d3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1f7d4 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a  ATE int sqlite3J
1f7d5 6f 75 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74  ournalSize(sqlit
1f7d6 65 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20  e3_vfs *pVfs){. 
1f7d7 20 72 65 74 75 72 6e 20 28 70 56 66 73 2d 3e 73   return (pVfs->s
1f7d8 7a 4f 73 46 69 6c 65 2b 73 69 7a 65 6f 66 28 4a  zOsFile+sizeof(J
1f7d9 6f 75 72 6e 61 6c 46 69 6c 65 29 29 3b 0a 7d 0a  ournalFile));.}.
1f7da 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  #endif../*******
1f7db 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6a  ******* End of j
1f7dc 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a  ournal.c *******
1f7dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f7de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f7df 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
1f7e0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
1f7e1 6c 65 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20  le memjournal.c 
1f7e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f7e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f7e4 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
1f7e5 30 38 20 4f 63 74 6f 62 65 72 20 37 0a 2a 2a 0a  08 October 7.**.
1f7e6 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
1f7e7 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
1f7e8 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
1f7e9 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
1f7ea 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
1f7eb 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
1f7ec 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
1f7ed 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
1f7ee 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
1f7ef 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1f7f0 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
1f7f1 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
1f7f2 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
1f7f3 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1f7f4 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
1f7f5 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
1f7f6 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
1f7f7 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
1f7f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f7f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f7fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f7fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
1f7fc 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
1f7fd 61 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f  ains code use to
1f7fe 20 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 6e   implement an in
1f7ff 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b  -memory rollback
1f800 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65   journal..** The
1f801 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62   in-memory rollb
1f802 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 75  ack journal is u
1f803 73 65 64 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 74  sed to journal t
1f804 72 61 6e 73 61 63 74 69 6f 6e 73 20 66 6f 72 0a  ransactions for.
1f805 2a 2a 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 64 61  ** ":memory:" da
1f806 74 61 62 61 73 65 73 20 61 6e 64 20 77 68 65 6e  tabases and when
1f807 20 74 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64   the journal_mod
1f808 65 3d 4d 45 4d 4f 52 59 20 70 72 61 67 6d 61 20  e=MEMORY pragma 
1f809 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 40  is used..**.** @
1f80a 28 23 29 20 24 49 64 3a 20 6d 65 6d 6a 6f 75 72  (#) $Id: memjour
1f80b 6e 61 6c 2e 63 2c 76 20 31 2e 31 32 20 32 30 30  nal.c,v 1.12 200
1f80c 39 2f 30 35 2f 30 34 20 31 31 3a 34 32 3a 33 30  9/05/04 11:42:30
1f80d 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
1f80e 20 24 0a 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72   $.*/../* Forwar
1f80f 64 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  d references to 
1f810 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 74 75  internal structu
1f811 72 65 73 20 2a 2f 0a 74 79 70 65 64 65 66 20 73  res */.typedef s
1f812 74 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e 61 6c  truct MemJournal
1f813 20 4d 65 6d 4a 6f 75 72 6e 61 6c 3b 0a 74 79 70   MemJournal;.typ
1f814 65 64 65 66 20 73 74 72 75 63 74 20 46 69 6c 65  edef struct File
1f815 50 6f 69 6e 74 20 46 69 6c 65 50 6f 69 6e 74 3b  Point FilePoint;
1f816 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
1f817 46 69 6c 65 43 68 75 6e 6b 20 46 69 6c 65 43 68  FileChunk FileCh
1f818 75 6e 6b 3b 0a 0a 2f 2a 20 53 70 61 63 65 20 74  unk;../* Space t
1f819 6f 20 68 6f 6c 64 20 74 68 65 20 72 6f 6c 6c 62  o hold the rollb
1f81a 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61  ack journal is a
1f81b 6c 6c 6f 63 61 74 65 64 20 69 6e 20 69 6e 63 72  llocated in incr
1f81c 65 6d 65 6e 74 73 20 6f 66 0a 2a 2a 20 74 68 69  ements of.** thi
1f81d 73 20 6d 61 6e 79 20 62 79 74 65 73 2e 0a 2a 2a  s many bytes..**
1f81e 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 63 68 6f  .** The size cho
1f81f 73 65 6e 20 69 73 20 61 20 6c 69 74 74 6c 65 20  sen is a little 
1f820 6c 65 73 73 20 74 68 61 6e 20 61 20 70 6f 77 65  less than a powe
1f821 72 20 6f 66 20 74 77 6f 2e 20 20 54 68 61 74 20  r of two.  That 
1f822 77 61 79 2c 0a 2a 2a 20 74 68 65 20 46 69 6c 65  way,.** the File
1f823 43 68 75 6e 6b 20 6f 62 6a 65 63 74 20 77 69 6c  Chunk object wil
1f824 6c 20 68 61 76 65 20 61 20 73 69 7a 65 20 74 68  l have a size th
1f825 61 74 20 61 6c 6d 6f 73 74 20 65 78 61 63 74 6c  at almost exactl
1f826 79 20 66 69 6c 6c 73 0a 2a 2a 20 61 20 70 6f 77  y fills.** a pow
1f827 65 72 2d 6f 66 2d 74 77 6f 20 61 6c 6c 6f 63 61  er-of-two alloca
1f828 74 69 6f 6e 2e 20 20 54 68 69 73 20 6d 69 6d 69  tion.  This mimi
1f829 6d 69 7a 65 73 20 77 61 73 74 65 64 20 73 70 61  mizes wasted spa
1f82a 63 65 20 69 6e 20 70 6f 77 65 72 2d 6f 66 2d 74  ce in power-of-t
1f82b 77 6f 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c  wo.** memory all
1f82c 6f 63 61 74 6f 72 73 2e 0a 2a 2f 0a 23 64 65 66  ocators..*/.#def
1f82d 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e  ine JOURNAL_CHUN
1f82e 4b 53 49 5a 45 20 28 28 69 6e 74 29 28 31 30 32  KSIZE ((int)(102
1f82f 34 2d 73 69 7a 65 6f 66 28 46 69 6c 65 43 68 75  4-sizeof(FileChu
1f830 6e 6b 2a 29 29 29 0a 0a 2f 2a 20 4d 61 63 72 6f  nk*)))../* Macro
1f831 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d 69 6e   to find the min
1f832 69 6d 75 6d 20 6f 66 20 74 77 6f 20 6e 75 6d 65  imum of two nume
1f833 72 69 63 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23  ric values..*/.#
1f834 69 66 6e 64 65 66 20 4d 49 4e 0a 23 20 64 65 66  ifndef MIN.# def
1f835 69 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28 28 78  ine MIN(x,y) ((x
1f836 29 3c 28 79 29 3f 28 78 29 3a 28 79 29 29 0a 23  )<(y)?(x):(y)).#
1f837 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
1f838 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
1f839 6c 20 69 73 20 63 6f 6d 70 6f 73 65 64 20 6f 66  l is composed of
1f83a 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f   a linked list o
1f83b 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72  f these structur
1f83c 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 69  es..*/.struct Fi
1f83d 6c 65 43 68 75 6e 6b 20 7b 0a 20 20 46 69 6c 65  leChunk {.  File
1f83e 43 68 75 6e 6b 20 2a 70 4e 65 78 74 3b 20 20 20  Chunk *pNext;   
1f83f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1f840 65 78 74 20 63 68 75 6e 6b 20 69 6e 20 74 68 65  ext chunk in the
1f841 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38   journal */.  u8
1f842 20 7a 43 68 75 6e 6b 5b 4a 4f 55 52 4e 41 4c 5f   zChunk[JOURNAL_
1f843 43 48 55 4e 4b 53 49 5a 45 5d 3b 20 20 20 2f 2a  CHUNKSIZE];   /*
1f844 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73   Content of this
1f845 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   chunk */.};../*
1f846 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
1f847 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73  of this object s
1f848 65 72 76 65 73 20 61 73 20 61 20 63 75 72 73 6f  erves as a curso
1f849 72 20 69 6e 74 6f 20 74 68 65 20 72 6f 6c 6c 62  r into the rollb
1f84a 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20  ack journal..** 
1f84b 54 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 62  The cursor can b
1f84c 65 20 65 69 74 68 65 72 20 66 6f 72 20 72 65 61  e either for rea
1f84d 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 2e  ding or writing.
1f84e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 69 6c 65 50  .*/.struct FileP
1f84f 6f 69 6e 74 20 7b 0a 20 20 73 71 6c 69 74 65 33  oint {.  sqlite3
1f850 5f 69 6e 74 36 34 20 69 4f 66 66 73 65 74 3b 20  _int64 iOffset; 
1f851 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
1f852 65 74 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69  et from the begi
1f853 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 66 69 6c  nning of the fil
1f854 65 20 2a 2f 0a 20 20 46 69 6c 65 43 68 75 6e 6b  e */.  FileChunk
1f855 20 2a 70 43 68 75 6e 6b 3b 20 20 20 20 20 20 20   *pChunk;       
1f856 20 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 66         /* Specif
1f857 69 63 20 63 68 75 6e 6b 20 69 6e 74 6f 20 77 68  ic chunk into wh
1f858 69 63 68 20 63 75 72 73 6f 72 20 70 6f 69 6e 74  ich cursor point
1f859 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  s */.};../*.** T
1f85a 68 69 73 20 73 75 62 63 6c 61 73 73 20 69 73 20  his subclass is 
1f85b 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71  a subclass of sq
1f85c 6c 69 74 65 33 5f 66 69 6c 65 2e 20 20 45 61 63  lite3_file.  Eac
1f85d 68 20 6f 70 65 6e 20 6d 65 6d 6f 72 79 2d 6a 6f  h open memory-jo
1f85e 75 72 6e 61 6c 0a 2a 2a 20 69 73 20 61 6e 20 69  urnal.** is an i
1f85f 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
1f860 63 6c 61 73 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  class..*/.struct
1f861 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 7b 0a 20 20   MemJournal {.  
1f862 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
1f863 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 20 20  ds *pMethod;    
1f864 2f 2a 20 50 61 72 65 6e 74 20 63 6c 61 73 73 2e  /* Parent class.
1f865 20 4d 55 53 54 20 42 45 20 46 49 52 53 54 20 2a   MUST BE FIRST *
1f866 2f 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70  /.  FileChunk *p
1f867 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 20  First;          
1f868 20 20 20 20 2f 2a 20 48 65 61 64 20 6f 66 20 69      /* Head of i
1f869 6e 2d 6d 65 6d 6f 72 79 20 63 68 75 6e 6b 2d 6c  n-memory chunk-l
1f86a 69 73 74 20 2a 2f 0a 20 20 46 69 6c 65 50 6f 69  ist */.  FilePoi
1f86b 6e 74 20 65 6e 64 70 6f 69 6e 74 3b 20 20 20 20  nt endpoint;    
1f86c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
1f86d 74 65 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  ter to the end o
1f86e 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20  f the file */.  
1f86f 46 69 6c 65 50 6f 69 6e 74 20 72 65 61 64 70 6f  FilePoint readpo
1f870 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  int;            
1f871 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68  /* Pointer to th
1f872 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73  e end of the las
1f873 74 20 78 52 65 61 64 28 29 20 2a 2f 0a 7d 3b 0a  t xRead() */.};.
1f874 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61  ./*.** Read data
1f875 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d 65 6d   from the in-mem
1f876 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ory journal file
1f877 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20 69  .  This is the i
1f878 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a  mplementation.**
1f879 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
1f87a 76 66 73 2e 78 52 65 61 64 20 6d 65 74 68 6f 64  vfs.xRead method
1f87b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1f87c 6d 65 6d 6a 72 6e 6c 52 65 61 64 28 0a 20 20 73  memjrnlRead(.  s
1f87d 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66  qlite3_file *pJf
1f87e 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75  d,    /* The jou
1f87f 72 6e 61 6c 20 66 69 6c 65 20 66 72 6f 6d 20 77  rnal file from w
1f880 68 69 63 68 20 74 6f 20 72 65 61 64 20 2a 2f 0a  hich to read */.
1f881 20 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20    void *zBuf,   
1f882 20 20 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20           /* Put 
1f883 74 68 65 20 72 65 73 75 6c 74 73 20 68 65 72 65  the results here
1f884 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20   */.  int iAmt, 
1f885 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f886 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1f887 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c  to read */.  sql
1f888 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20  ite_int64 iOfst 
1f889 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61      /* Begin rea
1f88a 64 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66  ding at this off
1f88b 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 4a  set */.){.  MemJ
1f88c 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d  ournal *p = (Mem
1f88d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a  Journal *)pJfd;.
1f88e 20 20 75 38 20 2a 7a 4f 75 74 20 3d 20 7a 42 75    u8 *zOut = zBu
1f88f 66 3b 0a 20 20 69 6e 74 20 6e 52 65 61 64 20 3d  f;.  int nRead =
1f890 20 69 41 6d 74 3b 0a 20 20 69 6e 74 20 69 43 68   iAmt;.  int iCh
1f891 75 6e 6b 4f 66 66 73 65 74 3b 0a 20 20 46 69 6c  unkOffset;.  Fil
1f892 65 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 0a  eChunk *pChunk;.
1f893 0a 20 20 2f 2a 20 53 51 4c 69 74 65 20 6e 65 76  .  /* SQLite nev
1f894 65 72 20 74 72 69 65 73 20 74 6f 20 72 65 61 64  er tries to read
1f895 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66   past the end of
1f896 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72   a rollback jour
1f897 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 61 73  nal file */.  as
1f898 73 65 72 74 28 20 69 4f 66 73 74 2b 69 41 6d 74  sert( iOfst+iAmt
1f899 3c 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f  <=p->endpoint.iO
1f89a 66 66 73 65 74 20 29 3b 0a 0a 20 20 69 66 28 20  ffset );..  if( 
1f89b 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66  p->readpoint.iOf
1f89c 66 73 65 74 21 3d 69 4f 66 73 74 20 7c 7c 20 69  fset!=iOfst || i
1f89d 4f 66 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73  Ofst==0 ){.    s
1f89e 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66  qlite3_int64 iOf
1f89f 66 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70  f = 0;.    for(p
1f8a0 43 68 75 6e 6b 3d 70 2d 3e 70 46 69 72 73 74 3b  Chunk=p->pFirst;
1f8a1 20 0a 20 20 20 20 20 20 20 20 41 4c 57 41 59 53   .        ALWAYS
1f8a2 28 70 43 68 75 6e 6b 29 20 26 26 20 28 69 4f 66  (pChunk) && (iOf
1f8a3 66 2b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53  f+JOURNAL_CHUNKS
1f8a4 49 5a 45 29 3c 3d 69 4f 66 73 74 3b 0a 20 20 20  IZE)<=iOfst;.   
1f8a5 20 20 20 20 20 70 43 68 75 6e 6b 3d 70 43 68 75       pChunk=pChu
1f8a6 6e 6b 2d 3e 70 4e 65 78 74 0a 20 20 20 20 29 7b  nk->pNext.    ){
1f8a7 0a 20 20 20 20 20 20 69 4f 66 66 20 2b 3d 20 4a  .      iOff += J
1f8a8 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45  OURNAL_CHUNKSIZE
1f8a9 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
1f8aa 0a 20 20 20 20 70 43 68 75 6e 6b 20 3d 20 70 2d  .    pChunk = p-
1f8ab 3e 72 65 61 64 70 6f 69 6e 74 2e 70 43 68 75 6e  >readpoint.pChun
1f8ac 6b 3b 0a 20 20 7d 0a 0a 20 20 69 43 68 75 6e 6b  k;.  }..  iChunk
1f8ad 4f 66 66 73 65 74 20 3d 20 28 69 6e 74 29 28 69  Offset = (int)(i
1f8ae 4f 66 73 74 25 4a 4f 55 52 4e 41 4c 5f 43 48 55  Ofst%JOURNAL_CHU
1f8af 4e 4b 53 49 5a 45 29 3b 0a 20 20 64 6f 20 7b 0a  NKSIZE);.  do {.
1f8b0 20 20 20 20 69 6e 74 20 69 53 70 61 63 65 20 3d      int iSpace =
1f8b1 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49   JOURNAL_CHUNKSI
1f8b2 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65  ZE - iChunkOffse
1f8b3 74 3b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 70 79  t;.    int nCopy
1f8b4 20 3d 20 4d 49 4e 28 6e 52 65 61 64 2c 20 28 4a   = MIN(nRead, (J
1f8b5 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45  OURNAL_CHUNKSIZE
1f8b6 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 29   - iChunkOffset)
1f8b7 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4f  );.    memcpy(zO
1f8b8 75 74 2c 20 26 70 43 68 75 6e 6b 2d 3e 7a 43 68  ut, &pChunk->zCh
1f8b9 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74  unk[iChunkOffset
1f8ba 5d 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 7a  ], nCopy);.    z
1f8bb 4f 75 74 20 2b 3d 20 6e 43 6f 70 79 3b 0a 20 20  Out += nCopy;.  
1f8bc 20 20 6e 52 65 61 64 20 2d 3d 20 69 53 70 61 63    nRead -= iSpac
1f8bd 65 3b 0a 20 20 20 20 69 43 68 75 6e 6b 4f 66 66  e;.    iChunkOff
1f8be 73 65 74 20 3d 20 30 3b 0a 20 20 7d 20 77 68 69  set = 0;.  } whi
1f8bf 6c 65 28 20 6e 52 65 61 64 3e 3d 30 20 26 26 20  le( nRead>=0 && 
1f8c0 28 70 43 68 75 6e 6b 3d 70 43 68 75 6e 6b 2d 3e  (pChunk=pChunk->
1f8c1 70 4e 65 78 74 29 21 3d 30 20 26 26 20 6e 52 65  pNext)!=0 && nRe
1f8c2 61 64 3e 30 20 29 3b 0a 20 20 70 2d 3e 72 65 61  ad>0 );.  p->rea
1f8c3 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 3d  dpoint.iOffset =
1f8c4 20 69 4f 66 73 74 2b 69 41 6d 74 3b 0a 20 20 70   iOfst+iAmt;.  p
1f8c5 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 70 43 68 75  ->readpoint.pChu
1f8c6 6e 6b 20 3d 20 70 43 68 75 6e 6b 3b 0a 0a 20 20  nk = pChunk;..  
1f8c7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1f8c8 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  ;.}../*.** Write
1f8c9 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 6c   data to the fil
1f8ca 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1f8cb 20 6d 65 6d 6a 72 6e 6c 57 72 69 74 65 28 0a 20   memjrnlWrite(. 
1f8cc 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
1f8cd 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a  Jfd,    /* The j
1f8ce 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f  ournal file into
1f8cf 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20   which to write 
1f8d0 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
1f8d1 2a 7a 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54  *zBuf,      /* T
1f8d2 61 6b 65 20 64 61 74 61 20 74 6f 20 62 65 20 77  ake data to be w
1f8d3 72 69 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65  ritten from here
1f8d4 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20   */.  int iAmt, 
1f8d5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f8d6 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1f8d7 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71  to write */.  sq
1f8d8 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74  lite_int64 iOfst
1f8d9 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72       /* Begin wr
1f8da 69 74 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66  iting at this of
1f8db 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69  fset into the fi
1f8dc 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 4a 6f  le */.){.  MemJo
1f8dd 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a  urnal *p = (MemJ
1f8de 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20  ournal *)pJfd;. 
1f8df 20 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 69 41   int nWrite = iA
1f8e0 6d 74 3b 0a 20 20 75 38 20 2a 7a 57 72 69 74 65  mt;.  u8 *zWrite
1f8e1 20 3d 20 28 75 38 20 2a 29 7a 42 75 66 3b 0a 0a   = (u8 *)zBuf;..
1f8e2 20 20 2f 2a 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72    /* An in-memor
1f8e3 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73  y journal file s
1f8e4 68 6f 75 6c 64 20 6f 6e 6c 79 20 65 76 65 72 20  hould only ever 
1f8e5 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f 2e 20  be appended to. 
1f8e6 52 61 6e 64 6f 6d 0a 20 20 2a 2a 20 61 63 63 65  Random.  ** acce
1f8e7 73 73 20 77 72 69 74 65 73 20 61 72 65 20 6e 6f  ss writes are no
1f8e8 74 20 72 65 71 75 69 72 65 64 20 62 79 20 73 71  t required by sq
1f8e9 6c 69 74 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  lite..  */.  ass
1f8ea 65 72 74 28 20 69 4f 66 73 74 3d 3d 70 2d 3e 65  ert( iOfst==p->e
1f8eb 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20  ndpoint.iOffset 
1f8ec 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
1f8ed 4d 45 54 45 52 28 69 4f 66 73 74 29 3b 0a 0a 20  METER(iOfst);.. 
1f8ee 20 77 68 69 6c 65 28 20 6e 57 72 69 74 65 3e 30   while( nWrite>0
1f8ef 20 29 7b 0a 20 20 20 20 46 69 6c 65 43 68 75 6e   ){.    FileChun
1f8f0 6b 20 2a 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 65  k *pChunk = p->e
1f8f1 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a  ndpoint.pChunk;.
1f8f2 20 20 20 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66      int iChunkOf
1f8f3 66 73 65 74 20 3d 20 28 69 6e 74 29 28 70 2d 3e  fset = (int)(p->
1f8f4 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74  endpoint.iOffset
1f8f5 25 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49  %JOURNAL_CHUNKSI
1f8f6 5a 45 29 3b 0a 20 20 20 20 69 6e 74 20 69 53 70  ZE);.    int iSp
1f8f7 61 63 65 20 3d 20 4d 49 4e 28 6e 57 72 69 74 65  ace = MIN(nWrite
1f8f8 2c 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53  , JOURNAL_CHUNKS
1f8f9 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73  IZE - iChunkOffs
1f8fa 65 74 29 3b 0a 0a 20 20 20 20 69 66 28 20 69 43  et);..    if( iC
1f8fb 68 75 6e 6b 4f 66 66 73 65 74 3d 3d 30 20 29 7b  hunkOffset==0 ){
1f8fc 0a 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 68  .      /* New ch
1f8fd 75 6e 6b 20 69 73 20 72 65 71 75 69 72 65 64 20  unk is required 
1f8fe 74 6f 20 65 78 74 65 6e 64 20 74 68 65 20 66 69  to extend the fi
1f8ff 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 46 69 6c  le. */.      Fil
1f900 65 43 68 75 6e 6b 20 2a 70 4e 65 77 20 3d 20 73  eChunk *pNew = s
1f901 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
1f902 7a 65 6f 66 28 46 69 6c 65 43 68 75 6e 6b 29 29  zeof(FileChunk))
1f903 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65  ;.      if( !pNe
1f904 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  w ){.        ret
1f905 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
1f906 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a  _NOMEM;.      }.
1f907 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65 78        pNew->pNex
1f908 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28  t = 0;.      if(
1f909 20 70 43 68 75 6e 6b 20 29 7b 0a 20 20 20 20 20   pChunk ){.     
1f90a 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 46     assert( p->pF
1f90b 69 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20  irst );.        
1f90c 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 20 3d 20  pChunk->pNext = 
1f90d 70 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 6c 73  pNew;.      }els
1f90e 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  e{.        asser
1f90f 74 28 20 21 70 2d 3e 70 46 69 72 73 74 20 29 3b  t( !p->pFirst );
1f910 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 46 69 72  .        p->pFir
1f911 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20  st = pNew;.     
1f912 20 7d 0a 20 20 20 20 20 20 70 2d 3e 65 6e 64 70   }.      p->endp
1f913 6f 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70 4e  oint.pChunk = pN
1f914 65 77 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d  ew;.    }..    m
1f915 65 6d 63 70 79 28 26 70 2d 3e 65 6e 64 70 6f 69  emcpy(&p->endpoi
1f916 6e 74 2e 70 43 68 75 6e 6b 2d 3e 7a 43 68 75 6e  nt.pChunk->zChun
1f917 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74 5d 2c  k[iChunkOffset],
1f918 20 7a 57 72 69 74 65 2c 20 69 53 70 61 63 65 29   zWrite, iSpace)
1f919 3b 0a 20 20 20 20 7a 57 72 69 74 65 20 2b 3d 20  ;.    zWrite += 
1f91a 69 53 70 61 63 65 3b 0a 20 20 20 20 6e 57 72 69  iSpace;.    nWri
1f91b 74 65 20 2d 3d 20 69 53 70 61 63 65 3b 0a 20 20  te -= iSpace;.  
1f91c 20 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f    p->endpoint.iO
1f91d 66 66 73 65 74 20 2b 3d 20 69 53 70 61 63 65 3b  ffset += iSpace;
1f91e 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53  .  }..  return S
1f91f 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
1f920 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20  ** Truncate the 
1f921 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
1f922 69 6e 74 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63  int memjrnlTrunc
1f923 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ate(sqlite3_file
1f924 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69   *pJfd, sqlite_i
1f925 6e 74 36 34 20 73 69 7a 65 29 7b 0a 20 20 4d 65  nt64 size){.  Me
1f926 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d  mJournal *p = (M
1f927 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64  emJournal *)pJfd
1f928 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70  ;.  FileChunk *p
1f929 43 68 75 6e 6b 3b 0a 20 20 61 73 73 65 72 74 28  Chunk;.  assert(
1f92a 73 69 7a 65 3d 3d 30 29 3b 0a 20 20 55 4e 55 53  size==0);.  UNUS
1f92b 45 44 5f 50 41 52 41 4d 45 54 45 52 28 73 69 7a  ED_PARAMETER(siz
1f92c 65 29 3b 0a 20 20 70 43 68 75 6e 6b 20 3d 20 70  e);.  pChunk = p
1f92d 2d 3e 70 46 69 72 73 74 3b 0a 20 20 77 68 69 6c  ->pFirst;.  whil
1f92e 65 28 20 70 43 68 75 6e 6b 20 29 7b 0a 20 20 20  e( pChunk ){.   
1f92f 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 54 6d 70   FileChunk *pTmp
1f930 20 3d 20 70 43 68 75 6e 6b 3b 0a 20 20 20 20 70   = pChunk;.    p
1f931 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 2d 3e  Chunk = pChunk->
1f932 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74  pNext;.    sqlit
1f933 65 33 5f 66 72 65 65 28 70 54 6d 70 29 3b 0a 20  e3_free(pTmp);. 
1f934 20 7d 0a 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a   }.  sqlite3MemJ
1f935 6f 75 72 6e 61 6c 4f 70 65 6e 28 70 4a 66 64 29  ournalOpen(pJfd)
1f936 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1f937 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
1f938 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a  lose the file..*
1f939 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d  /.static int mem
1f93a 6a 72 6e 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65  jrnlClose(sqlite
1f93b 33 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20  3_file *pJfd){. 
1f93c 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65   memjrnlTruncate
1f93d 28 70 4a 66 64 2c 20 30 29 3b 0a 20 20 72 65 74  (pJfd, 0);.  ret
1f93e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1f93f 0a 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68  .../*.** Sync th
1f940 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 79  e file..**.** Sy
1f941 6e 63 69 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d 6f  ncing an in-memo
1f942 72 79 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 20  ry journal is a 
1f943 6e 6f 2d 6f 70 2e 20 20 41 6e 64 2c 20 69 6e 20  no-op.  And, in 
1f944 66 61 63 74 2c 20 74 68 69 73 20 72 6f 75 74 69  fact, this routi
1f945 6e 65 0a 2a 2a 20 69 73 20 6e 65 76 65 72 20 63  ne.** is never c
1f946 61 6c 6c 65 64 20 69 6e 20 61 20 77 6f 72 6b 69  alled in a worki
1f947 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ng implementatio
1f948 6e 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65  n.  This impleme
1f949 6e 74 61 74 69 6f 6e 0a 2a 2a 20 65 78 69 73 74  ntation.** exist
1f94a 73 20 70 75 72 65 6c 79 20 61 73 20 61 20 63 6f  s purely as a co
1f94b 6e 74 69 6e 67 65 6e 63 79 2c 20 69 6e 20 63 61  ntingency, in ca
1f94c 73 65 20 73 6f 6d 65 20 6d 61 6c 66 75 6e 63 74  se some malfunct
1f94d 69 6f 6e 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65  ion in some othe
1f94e 72 0a 2a 2a 20 70 61 72 74 20 6f 66 20 53 51 4c  r.** part of SQL
1f94f 69 74 65 20 63 61 75 73 65 73 20 53 79 6e 63 20  ite causes Sync 
1f950 74 6f 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20  to be called by 
1f951 6d 69 73 74 61 6b 65 2e 0a 2a 2f 0a 73 74 61 74  mistake..*/.stat
1f952 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 53 79  ic int memjrnlSy
1f953 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  nc(sqlite3_file 
1f954 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f  *NotUsed, int No
1f955 74 55 73 65 64 32 29 7b 20 20 20 2f 2a 4e 4f 5f  tUsed2){   /*NO_
1f956 54 45 53 54 2a 2f 0a 20 20 55 4e 55 53 45 44 5f  TEST*/.  UNUSED_
1f957 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73  PARAMETER2(NotUs
1f958 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 20 20  ed, NotUsed2);  
1f959 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f95a 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a        /*NO_TEST*
1f95b 2f 0a 20 20 61 73 73 65 72 74 28 20 30 20 29 3b  /.  assert( 0 );
1f95c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f95d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f95e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f95f 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 72   /*NO_TEST*/.  r
1f960 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1f961 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f962 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f963 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f              /*NO
1f964 5f 54 45 53 54 2a 2f 0a 7d 20 20 20 20 20 20 20  _TEST*/.}       
1f965 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f966 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f967 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f968 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54         /*NO_TEST
1f969 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20  */../*.** Query 
1f96a 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
1f96b 66 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a 2a  file in bytes..*
1f96c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d  /.static int mem
1f96d 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 28 73 71 6c  jrnlFileSize(sql
1f96e 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c  ite3_file *pJfd,
1f96f 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
1f970 53 69 7a 65 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72  Size){.  MemJour
1f971 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75  nal *p = (MemJou
1f972 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 2a  rnal *)pJfd;.  *
1f973 70 53 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f  pSize = (sqlite_
1f974 69 6e 74 36 34 29 20 70 2d 3e 65 6e 64 70 6f 69  int64) p->endpoi
1f975 6e 74 2e 69 4f 66 66 73 65 74 3b 0a 20 20 72 65  nt.iOffset;.  re
1f976 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1f977 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f  }../*.** Table o
1f978 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 4d 65  f methods for Me
1f979 6d 4a 6f 75 72 6e 61 6c 20 73 71 6c 69 74 65 33  mJournal sqlite3
1f97a 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f  _file object..*/
1f97b 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 73  .static struct s
1f97c 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
1f97d 73 20 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68  s MemJournalMeth
1f97e 6f 64 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20  ods = {.  1,    
1f97f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69              /* i
1f980 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6d 65 6d  Version */.  mem
1f981 6a 72 6e 6c 43 6c 6f 73 65 2c 20 20 20 20 20 2f  jrnlClose,     /
1f982 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a 20 20 6d 65  * xClose */.  me
1f983 6d 6a 72 6e 6c 52 65 61 64 2c 20 20 20 20 20 20  mjrnlRead,      
1f984 2f 2a 20 78 52 65 61 64 20 2a 2f 0a 20 20 6d 65  /* xRead */.  me
1f985 6d 6a 72 6e 6c 57 72 69 74 65 2c 20 20 20 20 20  mjrnlWrite,     
1f986 2f 2a 20 78 57 72 69 74 65 20 2a 2f 0a 20 20 6d  /* xWrite */.  m
1f987 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c 20  emjrnlTruncate, 
1f988 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f   /* xTruncate */
1f989 0a 20 20 6d 65 6d 6a 72 6e 6c 53 79 6e 63 2c 20  .  memjrnlSync, 
1f98a 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f       /* xSync */
1f98b 0a 20 20 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53 69  .  memjrnlFileSi
1f98c 7a 65 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a  ze,  /* xFileSiz
1f98d 65 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20  e */.  0,       
1f98e 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63           /* xLoc
1f98f 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20  k */.  0,       
1f990 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c           /* xUnl
1f991 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20  ock */.  0,     
1f992 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43             /* xC
1f993 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
1f994 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
1f995 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65          /* xFile
1f996 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20  Control */.  0, 
1f997 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1f998 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f  * xSectorSize */
1f999 0a 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20  .  0            
1f99a 20 20 20 20 20 2f 2a 20 78 44 65 76 69 63 65 43       /* xDeviceC
1f99b 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 2a  haracteristics *
1f99c 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 65  /.};../* .** Ope
1f99d 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  n a journal file
1f99e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1f99f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1f9a0 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73  MemJournalOpen(s
1f9a1 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66  qlite3_file *pJf
1f9a2 64 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c  d){.  MemJournal
1f9a3 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61   *p = (MemJourna
1f9a4 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 61 73 73 65  l *)pJfd;.  asse
1f9a5 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41  rt( EIGHT_BYTE_A
1f9a6 4c 49 47 4e 4d 45 4e 54 28 70 29 20 29 3b 0a 20  LIGNMENT(p) );. 
1f9a7 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 71   memset(p, 0, sq
1f9a8 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53  lite3MemJournalS
1f9a9 69 7a 65 28 29 29 3b 0a 20 20 70 2d 3e 70 4d 65  ize());.  p->pMe
1f9aa 74 68 6f 64 20 3d 20 26 4d 65 6d 4a 6f 75 72 6e  thod = &MemJourn
1f9ab 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a  alMethods;.}../*
1f9ac 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
1f9ad 69 66 20 74 68 65 20 66 69 6c 65 2d 68 61 6e 64  if the file-hand
1f9ae 6c 65 20 70 61 73 73 65 64 20 61 73 20 61 6e 20  le passed as an 
1f9af 61 72 67 75 6d 65 6e 74 20 69 73 20 0a 2a 2a 20  argument is .** 
1f9b0 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75  an in-memory jou
1f9b1 72 6e 61 6c 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rnal .*/.SQLITE_
1f9b2 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1f9b3 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28  te3IsMemJournal(
1f9b4 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
1f9b5 66 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 4a  fd){.  return pJ
1f9b6 66 64 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 26 4d  fd->pMethods==&M
1f9b7 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73  emJournalMethods
1f9b8 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75  ;.}../* .** Retu
1f9b9 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
1f9ba 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20   bytes required 
1f9bb 74 6f 20 73 74 6f 72 65 20 61 20 4d 65 6d 4a 6f  to store a MemJo
1f9bc 75 72 6e 61 6c 20 74 68 61 74 20 75 73 65 73 20  urnal that uses 
1f9bd 76 66 73 0a 2a 2a 20 70 56 66 73 20 74 6f 20 63  vfs.** pVfs to c
1f9be 72 65 61 74 65 20 74 68 65 20 75 6e 64 65 72 6c  reate the underl
1f9bf 79 69 6e 67 20 6f 6e 2d 64 69 73 6b 20 66 69 6c  ying on-disk fil
1f9c0 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
1f9c1 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1f9c2 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28  3MemJournalSize(
1f9c3 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20  void){.  return 
1f9c4 73 69 7a 65 6f 66 28 4d 65 6d 4a 6f 75 72 6e 61  sizeof(MemJourna
1f9c5 6c 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  l);.}../********
1f9c6 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65  ****** End of me
1f9c7 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a  mjournal.c *****
1f9c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f9c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f9ca 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
1f9cb 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
1f9cc 65 20 77 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a 2a  e walker.c *****
1f9cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f9ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f9cf 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
1f9d0 38 20 41 75 67 75 73 74 20 31 36 0a 2a 2a 0a 2a  8 August 16.**.*
1f9d1 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
1f9d2 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
1f9d3 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
1f9d4 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
1f9d5 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
1f9d6 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
1f9d7 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
1f9d8 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
1f9d9 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
1f9da 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
1f9db 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
1f9dc 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
1f9dd 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
1f9de 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
1f9df 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
1f9e0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
1f9e1 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
1f9e2 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
1f9e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f9e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f9e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f9e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
1f9e7 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
1f9e8 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66   routines used f
1f9e9 6f 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70  or walking the p
1f9ea 61 72 73 65 72 20 74 72 65 65 20 66 6f 72 0a 2a  arser tree for.*
1f9eb 2a 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  * an SQL stateme
1f9ec 6e 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 77  nt..**.** $Id: w
1f9ed 61 6c 6b 65 72 2e 63 2c 76 20 31 2e 37 20 32 30  alker.c,v 1.7 20
1f9ee 30 39 2f 30 36 2f 31 35 20 32 33 3a 31 35 3a 35  09/06/15 23:15:5
1f9ef 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  9 drh Exp $.*/..
1f9f0 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65  ./*.** Walk an e
1f9f1 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20  xpression tree. 
1f9f2 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c   Invoke the call
1f9f3 62 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61  back once for ea
1f9f4 63 68 20 6e 6f 64 65 0a 2a 2a 20 6f 66 20 74 68  ch node.** of th
1f9f5 65 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 77 68  e expression, wh
1f9f6 69 6c 65 20 64 65 63 65 6e 64 69 6e 67 2e 20 20  ile decending.  
1f9f7 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c  (In other words,
1f9f8 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a   the callback.**
1f9f9 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 65 66 6f   is invoked befo
1f9fa 72 65 20 76 69 73 69 74 69 6e 67 20 63 68 69 6c  re visiting chil
1f9fb 64 72 65 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65  dren.).**.** The
1f9fc 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72   return value fr
1f9fd 6f 6d 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  om the callback 
1f9fe 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 6f 66  should be one of
1f9ff 20 74 68 65 20 57 52 43 5f 2a 0a 2a 2a 20 63 6f   the WRC_*.** co
1fa00 6e 73 74 61 6e 74 73 20 74 6f 20 73 70 65 63 69  nstants to speci
1fa01 66 79 20 68 6f 77 20 74 6f 20 70 72 6f 63 65 65  fy how to procee
1fa02 64 20 77 69 74 68 20 74 68 65 20 77 61 6c 6b 2e  d with the walk.
1fa03 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 43 6f  .**.**    WRC_Co
1fa04 6e 74 69 6e 75 65 20 20 20 20 20 20 43 6f 6e 74  ntinue      Cont
1fa05 69 6e 75 65 20 64 65 73 63 65 6e 64 69 6e 67 20  inue descending 
1fa06 64 6f 77 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a  down the tree..*
1fa07 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 50 72 75 6e  *.**    WRC_Prun
1fa08 65 20 20 20 20 20 20 20 20 20 44 6f 20 6e 6f 74  e         Do not
1fa09 20 64 65 73 63 65 6e 64 20 69 6e 74 6f 20 63 68   descend into ch
1fa0a 69 6c 64 20 6e 6f 64 65 73 2e 20 20 42 75 74 20  ild nodes.  But 
1fa0b 61 6c 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20  allow.**        
1fa0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
1fa0d 65 20 77 61 6c 6b 20 74 6f 20 63 6f 6e 74 69 6e  e walk to contin
1fa0e 75 65 20 77 69 74 68 20 73 69 62 6c 69 6e 67 20  ue with sibling 
1fa0f 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  nodes..**.**    
1fa10 57 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20  WRC_Abort       
1fa11 20 20 44 6f 20 6e 6f 20 6d 6f 72 65 20 63 61 6c    Do no more cal
1fa12 6c 62 61 63 6b 73 2e 20 20 55 6e 77 69 6e 64 20  lbacks.  Unwind 
1fa13 74 68 65 20 73 74 61 63 6b 20 61 6e 64 0a 2a 2a  the stack and.**
1fa14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa15 20 20 20 20 20 20 72 65 74 75 72 6e 20 74 68 65        return the
1fa16 20 74 6f 70 2d 6c 65 76 65 6c 20 77 61 6c 6b 20   top-level walk 
1fa17 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  call..**.** The 
1fa18 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f  return value fro
1fa19 6d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  m this routine i
1fa1a 73 20 57 52 43 5f 41 62 6f 72 74 20 74 6f 20 61  s WRC_Abort to a
1fa1b 62 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 65 20  bandon the tree 
1fa1c 77 61 6c 6b 0a 2a 2a 20 61 6e 64 20 57 52 43 5f  walk.** and WRC_
1fa1d 43 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6e 74  Continue to cont
1fa1e 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  inue..*/.SQLITE_
1fa1f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1fa20 74 65 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b  te3WalkExpr(Walk
1fa21 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70  er *pWalker, Exp
1fa22 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74  r *pExpr){.  int
1fa23 20 72 63 3b 0a 20 20 69 66 28 20 70 45 78 70 72   rc;.  if( pExpr
1fa24 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43  ==0 ) return WRC
1fa25 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 74 65 73  _Continue;.  tes
1fa26 74 63 61 73 65 28 20 45 78 70 72 48 61 73 50 72  tcase( ExprHasPr
1fa27 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
1fa28 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 3b 0a 20  _TokenOnly) );. 
1fa29 20 74 65 73 74 63 61 73 65 28 20 45 78 70 72 48   testcase( ExprH
1fa2a 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
1fa2b 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b  , EP_Reduced) );
1fa2c 0a 20 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d  .  rc = pWalker-
1fa2d 3e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 28 70  >xExprCallback(p
1fa2e 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 29 3b 0a  Walker, pExpr);.
1fa2f 20 20 69 66 28 20 72 63 3d 3d 57 52 43 5f 43 6f    if( rc==WRC_Co
1fa30 6e 74 69 6e 75 65 0a 20 20 20 20 20 20 20 20 20  ntinue.         
1fa31 20 20 20 20 20 26 26 20 21 45 78 70 72 48 61 73       && !ExprHas
1fa32 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70  AnyProperty(pExp
1fa33 72 2c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20  r,EP_TokenOnly) 
1fa34 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
1fa35 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b  e3WalkExpr(pWalk
1fa36 65 72 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  er, pExpr->pLeft
1fa37 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1fa38 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 73 71  bort;.    if( sq
1fa39 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57  lite3WalkExpr(pW
1fa3a 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 52  alker, pExpr->pR
1fa3b 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 57  ight) ) return W
1fa3c 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66  RC_Abort;.    if
1fa3d 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
1fa3e 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  y(pExpr, EP_xIsS
1fa3f 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20  elect) ){.      
1fa40 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53  if( sqlite3WalkS
1fa41 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70  elect(pWalker, p
1fa42 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29  Expr->x.pSelect)
1fa43 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
1fa44 6f 72 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ort;.    }else{.
1fa45 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
1fa46 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57  3WalkExprList(pW
1fa47 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e  alker, pExpr->x.
1fa48 70 4c 69 73 74 29 20 29 20 72 65 74 75 72 6e 20  pList) ) return 
1fa49 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d  WRC_Abort;.    }
1fa4a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
1fa4b 20 26 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a   & WRC_Abort;.}.
1fa4c 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69  ./*.** Call sqli
1fa4d 74 65 33 57 61 6c 6b 45 78 70 72 28 29 20 66 6f  te3WalkExpr() fo
1fa4e 72 20 65 76 65 72 79 20 65 78 70 72 65 73 73 69  r every expressi
1fa4f 6f 6e 20 69 6e 20 6c 69 73 74 20 70 20 6f 72 20  on in list p or 
1fa50 75 6e 74 69 6c 0a 2a 2a 20 61 6e 20 61 62 6f 72  until.** an abor
1fa51 74 20 72 65 71 75 65 73 74 20 69 73 20 73 65 65  t request is see
1fa52 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
1fa53 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1fa54 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 57 61 6c  WalkExprList(Wal
1fa55 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78  ker *pWalker, Ex
1fa56 70 72 4c 69 73 74 20 2a 70 29 7b 0a 20 20 69 6e  prList *p){.  in
1fa57 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 45 78  t i;.  struct Ex
1fa58 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
1fa59 65 6d 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  em;.  if( p ){. 
1fa5a 20 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 45 78 70     for(i=p->nExp
1fa5b 72 2c 20 70 49 74 65 6d 3d 70 2d 3e 61 3b 20 69  r, pItem=p->a; i
1fa5c 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b  >0; i--, pItem++
1fa5d 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ){.      if( sql
1fa5e 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61  ite3WalkExpr(pWa
1fa5f 6c 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 45 78  lker, pItem->pEx
1fa60 70 72 29 20 29 20 72 65 74 75 72 6e 20 57 52 43  pr) ) return WRC
1fa61 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20  _Abort;.    }.  
1fa62 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43  }.  return WRC_C
1fa63 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  ontinue;.}../*.*
1fa64 2a 20 57 61 6c 6b 20 61 6c 6c 20 65 78 70 72 65  * Walk all expre
1fa65 73 73 69 6f 6e 73 20 61 73 73 6f 63 69 61 74 65  ssions associate
1fa66 64 20 77 69 74 68 20 53 45 4c 45 43 54 20 73 74  d with SELECT st
1fa67 61 74 65 6d 65 6e 74 20 70 2e 20 20 44 6f 0a 2a  atement p.  Do.*
1fa68 2a 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 74 68 65  * not invoke the
1fa69 20 53 45 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b   SELECT callback
1fa6a 20 6f 6e 20 70 2c 20 62 75 74 20 64 6f 20 28 6f   on p, but do (o
1fa6b 66 20 63 6f 75 72 73 65 29 20 69 6e 76 6f 6b 65  f course) invoke
1fa6c 0a 2a 2a 20 61 6e 79 20 65 78 70 72 20 63 61 6c  .** any expr cal
1fa6d 6c 62 61 63 6b 73 20 61 6e 64 20 53 45 4c 45 43  lbacks and SELEC
1fa6e 54 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 61 74  T callbacks that
1fa6f 20 63 6f 6d 65 20 66 72 6f 6d 20 73 75 62 71 75   come from subqu
1fa70 65 72 69 65 73 2e 0a 2a 2a 20 52 65 74 75 72 6e  eries..** Return
1fa71 20 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 52   WRC_Abort or WR
1fa72 43 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53  C_Continue..*/.S
1fa73 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1fa74 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c  t sqlite3WalkSel
1fa75 65 63 74 45 78 70 72 28 57 61 6c 6b 65 72 20 2a  ectExpr(Walker *
1fa76 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20  pWalker, Select 
1fa77 2a 70 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74  *p){.  if( sqlit
1fa78 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70  e3WalkExprList(p
1fa79 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 45 4c 69 73  Walker, p->pELis
1fa7a 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  t) ) return WRC_
1fa7b 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c  Abort;.  if( sql
1fa7c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61  ite3WalkExpr(pWa
1fa7d 6c 6b 65 72 2c 20 70 2d 3e 70 57 68 65 72 65 29  lker, p->pWhere)
1fa7e 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
1fa7f 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ort;.  if( sqlit
1fa80 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70  e3WalkExprList(p
1fa81 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 47 72 6f 75  Walker, p->pGrou
1fa82 70 42 79 29 20 29 20 72 65 74 75 72 6e 20 57 52  pBy) ) return WR
1fa83 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73  C_Abort;.  if( s
1fa84 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70  qlite3WalkExpr(p
1fa85 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 48 61 76 69  Walker, p->pHavi
1fa86 6e 67 29 20 29 20 72 65 74 75 72 6e 20 57 52 43  ng) ) return WRC
1fa87 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71  _Abort;.  if( sq
1fa88 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73  lite3WalkExprLis
1fa89 74 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f  t(pWalker, p->pO
1fa8a 72 64 65 72 42 79 29 20 29 20 72 65 74 75 72 6e  rderBy) ) return
1fa8b 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66   WRC_Abort;.  if
1fa8c 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70  ( sqlite3WalkExp
1fa8d 72 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4c  r(pWalker, p->pL
1fa8e 69 6d 69 74 29 20 29 20 72 65 74 75 72 6e 20 57  imit) ) return W
1fa8f 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20  RC_Abort;.  if( 
1fa90 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28  sqlite3WalkExpr(
1fa91 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 66 66  pWalker, p->pOff
1fa92 73 65 74 29 20 29 20 72 65 74 75 72 6e 20 57 52  set) ) return WR
1fa93 43 5f 41 62 6f 72 74 3b 0a 20 20 72 65 74 75 72  C_Abort;.  retur
1fa94 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a  n WRC_Continue;.
1fa95 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 74 68  }../*.** Walk th
1fa96 65 20 70 61 72 73 65 20 74 72 65 65 73 20 61 73  e parse trees as
1fa97 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 6c  sociated with al
1fa98 6c 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20  l subqueries in 
1fa99 74 68 65 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75  the.** FROM clau
1fa9a 73 65 20 6f 66 20 53 45 4c 45 43 54 20 73 74 61  se of SELECT sta
1fa9b 74 65 6d 65 6e 74 20 70 2e 20 20 44 6f 20 6e 6f  tement p.  Do no
1fa9c 74 20 69 6e 76 6f 6b 65 20 74 68 65 20 73 65 6c  t invoke the sel
1fa9d 65 63 74 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20  ect.** callback 
1fa9e 6f 6e 20 70 2c 20 62 75 74 20 64 6f 20 69 6e 76  on p, but do inv
1fa9f 6f 6b 65 20 69 74 20 6f 6e 20 65 61 63 68 20 46  oke it on each F
1faa0 52 4f 4d 20 63 6c 61 75 73 65 20 73 75 62 71 75  ROM clause subqu
1faa1 65 72 79 0a 2a 2a 20 61 6e 64 20 6f 6e 20 61 6e  ery.** and on an
1faa2 79 20 73 75 62 71 75 65 72 69 65 73 20 66 75 72  y subqueries fur
1faa3 74 68 65 72 20 64 6f 77 6e 20 69 6e 20 74 68 65  ther down in the
1faa4 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 0a   tree.  Return .
1faa5 2a 2a 20 57 52 43 5f 41 62 6f 72 74 20 6f 72 20  ** WRC_Abort or 
1faa6 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 2a 2f  WRC_Continue;.*/
1faa7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1faa8 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53  int sqlite3WalkS
1faa9 65 6c 65 63 74 46 72 6f 6d 28 57 61 6c 6b 65 72  electFrom(Walker
1faaa 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63   *pWalker, Selec
1faab 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74  t *p){.  SrcList
1faac 20 2a 70 53 72 63 3b 0a 20 20 69 6e 74 20 69 3b   *pSrc;.  int i;
1faad 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
1faae 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a  t_item *pItem;..
1faaf 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63    pSrc = p->pSrc
1fab0 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70  ;.  if( ALWAYS(p
1fab1 53 72 63 29 20 29 7b 0a 20 20 20 20 66 6f 72 28  Src) ){.    for(
1fab2 69 3d 70 53 72 63 2d 3e 6e 53 72 63 2c 20 70 49  i=pSrc->nSrc, pI
1fab3 74 65 6d 3d 70 53 72 63 2d 3e 61 3b 20 69 3e 30  tem=pSrc->a; i>0
1fab4 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i--, pItem++){
1fab5 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
1fab6 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61  e3WalkSelect(pWa
1fab7 6c 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 53 65  lker, pItem->pSe
1fab8 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 20  lect) ){.       
1fab9 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1faba 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  t;.      }.    }
1fabb 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 52  .  }.  return WR
1fabc 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 20 0a 0a  C_Continue;.} ..
1fabd 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74  /*.** Call sqlit
1fabe 65 33 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72  e3WalkExpr() for
1fabf 20 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f   every expressio
1fac0 6e 20 69 6e 20 53 65 6c 65 63 74 20 73 74 61 74  n in Select stat
1fac1 65 6d 65 6e 74 20 70 2e 0a 2a 2a 20 49 6e 76 6f  ement p..** Invo
1fac2 6b 65 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65  ke sqlite3WalkSe
1fac3 6c 65 63 74 28 29 20 66 6f 72 20 73 75 62 71 75  lect() for subqu
1fac4 65 72 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f  eries in the FRO
1fac5 4d 20 63 6c 61 75 73 65 20 61 6e 64 0a 2a 2a 20  M clause and.** 
1fac6 6f 6e 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20  on the compound 
1fac7 73 65 6c 65 63 74 20 63 68 61 69 6e 2c 20 70 2d  select chain, p-
1fac8 3e 70 50 72 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52  >pPrior..**.** R
1fac9 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e  eturn WRC_Contin
1faca 75 65 20 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20  ue under normal 
1facb 63 6f 6e 64 69 74 69 6f 6e 73 2e 20 20 52 65 74  conditions.  Ret
1facc 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 20 69 66  urn WRC_Abort if
1facd 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 6e 20  .** there is an 
1face 61 62 6f 72 74 20 72 65 71 75 65 73 74 2e 0a 2a  abort request..*
1facf 2a 0a 2a 2a 20 49 66 20 74 68 65 20 57 61 6c 6b  *.** If the Walk
1fad0 65 72 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65  er does not have
1fad1 20 61 6e 20 78 53 65 6c 65 63 74 43 61 6c 6c 62   an xSelectCallb
1fad2 61 63 6b 28 29 20 74 68 65 6e 20 74 68 69 73 20  ack() then this 
1fad3 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20  routine.** is a 
1fad4 6e 6f 2d 6f 70 20 72 65 74 75 72 6e 69 6e 67 20  no-op returning 
1fad5 57 52 43 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f  WRC_Continue..*/
1fad6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1fad7 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53  int sqlite3WalkS
1fad8 65 6c 65 63 74 28 57 61 6c 6b 65 72 20 2a 70 57  elect(Walker *pW
1fad9 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70  alker, Select *p
1fada 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  ){.  int rc;.  i
1fadb 66 28 20 70 3d 3d 30 20 7c 7c 20 70 57 61 6c 6b  f( p==0 || pWalk
1fadc 65 72 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62  er->xSelectCallb
1fadd 61 63 6b 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ack==0 ) return 
1fade 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20  WRC_Continue;.  
1fadf 72 63 20 3d 20 57 52 43 5f 43 6f 6e 74 69 6e 75  rc = WRC_Continu
1fae0 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 20 29  e;.  while( p  )
1fae1 7b 0a 20 20 20 20 72 63 20 3d 20 70 57 61 6c 6b  {.    rc = pWalk
1fae2 65 72 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62  er->xSelectCallb
1fae3 61 63 6b 28 70 57 61 6c 6b 65 72 2c 20 70 29 3b  ack(pWalker, p);
1fae4 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 62 72  .    if( rc ) br
1fae5 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  eak;.    if( sql
1fae6 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78  ite3WalkSelectEx
1fae7 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 29 20 29  pr(pWalker, p) )
1fae8 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1fae9 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  t;.    if( sqlit
1faea 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d  e3WalkSelectFrom
1faeb 28 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 72  (pWalker, p) ) r
1faec 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
1faed 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 50 72 69  .    p = p->pPri
1faee 6f 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  or;.  }.  return
1faef 20 72 63 20 26 20 57 52 43 5f 41 62 6f 72 74 3b   rc & WRC_Abort;
1faf0 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
1faf1 2a 2a 2a 20 45 6e 64 20 6f 66 20 77 61 6c 6b 65  *** End of walke
1faf2 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.c ************
1faf3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1faf4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1faf5 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
1faf6 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72  *** Begin file r
1faf7 65 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  esolve.c *******
1faf8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1faf9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fafa 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41  **/./*.** 2008 A
1fafb 75 67 75 73 74 20 31 38 0a 2a 2a 0a 2a 2a 20 54  ugust 18.**.** T
1fafc 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
1fafd 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
1fafe 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
1faff 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
1fb00 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
1fb01 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
1fb02 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
1fb03 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
1fb04 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
1fb05 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
1fb06 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
1fb07 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
1fb08 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
1fb09 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
1fb0a 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
1fb0b 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
1fb0c 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
1fb0d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fb0e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fb0f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fb10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fb11 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
1fb12 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
1fb13 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66   routines used f
1fb14 6f 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70  or walking the p
1fb15 61 72 73 65 72 20 74 72 65 65 20 61 6e 64 0a 2a  arser tree and.*
1fb16 2a 20 72 65 73 6f 6c 76 65 20 61 6c 6c 20 69 64  * resolve all id
1fb17 65 6e 74 69 66 69 65 72 73 20 62 79 20 61 73 73  entifiers by ass
1fb18 6f 63 69 61 74 69 6e 67 20 74 68 65 6d 20 77 69  ociating them wi
1fb19 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a  th a particular.
1fb1a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c  ** table and col
1fb1b 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  umn..**.** $Id: 
1fb1c 72 65 73 6f 6c 76 65 2e 63 2c 76 20 31 2e 33 30  resolve.c,v 1.30
1fb1d 20 32 30 30 39 2f 30 36 2f 31 35 20 32 33 3a 31   2009/06/15 23:1
1fb1e 35 3a 35 39 20 64 72 68 20 45 78 70 20 24 0a 2a  5:59 drh Exp $.*
1fb1f 2f 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 68  /../*.** Turn th
1fb20 65 20 70 45 78 70 72 20 65 78 70 72 65 73 73 69  e pExpr expressi
1fb21 6f 6e 20 69 6e 74 6f 20 61 6e 20 61 6c 69 61 73  on into an alias
1fb22 20 66 6f 72 20 74 68 65 20 69 43 6f 6c 2d 74 68   for the iCol-th
1fb23 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a   column of the.*
1fb24 2a 20 72 65 73 75 6c 74 20 73 65 74 20 69 6e 20  * result set in 
1fb25 70 45 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  pEList..**.** If
1fb26 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
1fb27 63 6f 6c 75 6d 6e 20 69 73 20 61 20 73 69 6d 70  column is a simp
1fb28 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65  le column refere
1fb29 6e 63 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72  nce, then this r
1fb2a 6f 75 74 69 6e 65 0a 2a 2a 20 6d 61 6b 65 73 20  outine.** makes 
1fb2b 61 6e 20 65 78 61 63 74 20 63 6f 70 79 2e 20 20  an exact copy.  
1fb2c 42 75 74 20 66 6f 72 20 61 6e 79 20 6f 74 68 65  But for any othe
1fb2d 72 20 6b 69 6e 64 20 6f 66 20 65 78 70 72 65 73  r kind of expres
1fb2e 73 69 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20 72 6f  sion, this.** ro
1fb2f 75 74 69 6e 65 20 6d 61 6b 65 20 61 20 63 6f 70  utine make a cop
1fb30 79 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  y of the result 
1fb31 73 65 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68  set column as th
1fb32 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  e argument to th
1fb33 65 0a 2a 2a 20 54 4b 5f 41 53 20 6f 70 65 72 61  e.** TK_AS opera
1fb34 74 6f 72 2e 20 20 54 68 65 20 54 4b 5f 41 53 20  tor.  The TK_AS 
1fb35 6f 70 65 72 61 74 6f 72 20 63 61 75 73 65 73 20  operator causes 
1fb36 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  the expression t
1fb37 6f 20 62 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65  o be.** evaluate
1fb38 64 20 6a 75 73 74 20 6f 6e 63 65 20 61 6e 64 20  d just once and 
1fb39 74 68 65 6e 20 72 65 75 73 65 64 20 66 6f 72 20  then reused for 
1fb3a 65 61 63 68 20 61 6c 69 61 73 2e 0a 2a 2a 0a 2a  each alias..**.*
1fb3b 2a 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f 72  * The reason for
1fb3c 20 73 75 70 70 72 65 73 73 69 6e 67 20 74 68 65   suppressing the
1fb3d 20 54 4b 5f 41 53 20 74 65 72 6d 20 77 68 65 6e   TK_AS term when
1fb3e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
1fb3f 69 73 20 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 63  is a simple.** c
1fb40 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20  olumn reference 
1fb41 69 73 20 73 6f 20 74 68 61 74 20 74 68 65 20 63  is so that the c
1fb42 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20  olumn reference 
1fb43 77 69 6c 6c 20 62 65 20 72 65 63 6f 67 6e 69 7a  will be recogniz
1fb44 65 64 20 61 73 0a 2a 2a 20 75 73 61 62 6c 65 20  ed as.** usable 
1fb45 62 79 20 69 6e 64 69 63 65 73 20 77 69 74 68 69  by indices withi
1fb46 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
1fb47 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f  se processing lo
1fb48 67 69 63 2e 20 0a 2a 2a 0a 2a 2a 20 48 61 63 6b  gic. .**.** Hack
1fb49 3a 20 20 54 68 65 20 54 4b 5f 41 53 20 6f 70 65  :  The TK_AS ope
1fb4a 72 61 74 6f 72 20 69 73 20 69 6e 68 69 62 69 74  rator is inhibit
1fb4b 65 64 20 69 66 20 7a 54 79 70 65 5b 30 5d 3d 3d  ed if zType[0]==
1fb4c 27 47 27 2e 20 20 54 68 69 73 20 6d 65 61 6e 73  'G'.  This means
1fb4d 0a 2a 2a 20 74 68 61 74 20 69 6e 20 61 20 47 52  .** that in a GR
1fb4e 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2c 20 74  OUP BY clause, t
1fb4f 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
1fb50 20 65 76 61 6c 75 61 74 65 64 20 74 77 69 63 65   evaluated twice
1fb51 2e 20 20 48 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20  .  Hence:.**.** 
1fb52 20 20 20 20 53 45 4c 45 43 54 20 72 61 6e 64 6f      SELECT rando
1fb53 6d 28 29 25 35 20 41 53 20 78 2c 20 63 6f 75 6e  m()%5 AS x, coun
1fb54 74 28 2a 29 20 46 52 4f 4d 20 74 61 62 20 47 52  t(*) FROM tab GR
1fb55 4f 55 50 20 42 59 20 78 0a 2a 2a 0a 2a 2a 20 49  OUP BY x.**.** I
1fb56 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a  s equivalent to:
1fb57 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43  .**.**     SELEC
1fb58 54 20 72 61 6e 64 6f 6d 28 29 25 35 20 41 53 20  T random()%5 AS 
1fb59 78 2c 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d  x, count(*) FROM
1fb5a 20 74 61 62 20 47 52 4f 55 50 20 42 59 20 72 61   tab GROUP BY ra
1fb5b 6e 64 6f 6d 28 29 25 35 0a 2a 2a 0a 2a 2a 20 54  ndom()%5.**.** T
1fb5c 68 65 20 72 65 73 75 6c 74 20 6f 66 20 72 61 6e  he result of ran
1fb5d 64 6f 6d 28 29 25 35 20 69 6e 20 74 68 65 20 47  dom()%5 in the G
1fb5e 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 69  ROUP BY clause i
1fb5f 73 20 70 72 6f 62 61 62 6c 79 20 64 69 66 66 65  s probably diffe
1fb60 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  rent.** from the
1fb61 20 72 65 73 75 6c 74 20 69 6e 20 74 68 65 20 72   result in the r
1fb62 65 73 75 6c 74 2d 73 65 74 2e 20 20 57 65 20 6d  esult-set.  We m
1fb63 69 67 68 74 20 66 69 78 20 74 68 69 73 20 73 6f  ight fix this so
1fb64 6d 65 64 61 79 2e 20 20 4f 72 0a 2a 2a 20 74 68  meday.  Or.** th
1fb65 65 6e 20 61 67 61 69 6e 2c 20 77 65 20 6d 69 67  en again, we mig
1fb66 68 74 20 6e 6f 74 2e 2e 2e 0a 2a 2f 0a 73 74 61  ht not....*/.sta
1fb67 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c 76 65  tic void resolve
1fb68 41 6c 69 61 73 28 0a 20 20 50 61 72 73 65 20 2a  Alias(.  Parse *
1fb69 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
1fb6a 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
1fb6b 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  xt */.  ExprList
1fb6c 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 2f   *pEList,      /
1fb6d 2a 20 41 20 72 65 73 75 6c 74 20 73 65 74 20 2a  * A result set *
1fb6e 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20  /.  int iCol,   
1fb6f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20             /* A 
1fb70 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65  column in the re
1fb71 73 75 6c 74 20 73 65 74 2e 20 20 30 2e 2e 70 45  sult set.  0..pE
1fb72 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20 2a 2f  List->nExpr-1 */
1fb73 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  .  Expr *pExpr, 
1fb74 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 61            /* Tra
1fb75 6e 73 66 6f 72 6d 20 74 68 69 73 20 69 6e 74 6f  nsform this into
1fb76 20 61 6e 20 61 6c 69 61 73 20 74 6f 20 74 68 65   an alias to the
1fb77 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20   result set */. 
1fb78 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79   const char *zTy
1fb79 70 65 20 20 20 20 20 20 2f 2a 20 22 47 52 4f 55  pe      /* "GROU
1fb7a 50 22 20 6f 72 20 22 4f 52 44 45 52 22 20 6f 72  P" or "ORDER" or
1fb7b 20 22 22 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72   "" */.){.  Expr
1fb7c 20 2a 70 4f 72 69 67 3b 20 20 20 20 20 20 20 20   *pOrig;        
1fb7d 20 20 20 2f 2a 20 54 68 65 20 69 43 6f 6c 2d 74     /* The iCol-t
1fb7e 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  h column of the 
1fb7f 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20  result set */.  
1fb80 45 78 70 72 20 2a 70 44 75 70 3b 20 20 20 20 20  Expr *pDup;     
1fb81 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f         /* Copy o
1fb82 66 20 70 4f 72 69 67 20 2a 2f 0a 20 20 73 71 6c  f pOrig */.  sql
1fb83 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
1fb84 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
1fb85 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
1fb86 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43 6f  /..  assert( iCo
1fb87 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 45 4c  l>=0 && iCol<pEL
1fb88 69 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20  ist->nExpr );.  
1fb89 70 4f 72 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e  pOrig = pEList->
1fb8a 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20  a[iCol].pExpr;. 
1fb8b 20 61 73 73 65 72 74 28 20 70 4f 72 69 67 21 3d   assert( pOrig!=
1fb8c 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
1fb8d 4f 72 69 67 2d 3e 66 6c 61 67 73 20 26 20 45 50  Orig->flags & EP
1fb8e 5f 52 65 73 6f 6c 76 65 64 20 29 3b 0a 20 20 64  _Resolved );.  d
1fb8f 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
1fb90 20 20 69 66 28 20 70 4f 72 69 67 2d 3e 6f 70 21    if( pOrig->op!
1fb91 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 7a 54  =TK_COLUMN && zT
1fb92 79 70 65 5b 30 5d 21 3d 27 47 27 20 29 7b 0a 20  ype[0]!='G' ){. 
1fb93 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65     pDup = sqlite
1fb94 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72  3ExprDup(db, pOr
1fb95 69 67 2c 20 30 29 3b 0a 20 20 20 20 70 44 75 70  ig, 0);.    pDup
1fb96 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
1fb97 70 50 61 72 73 65 2c 20 54 4b 5f 41 53 2c 20 70  pParse, TK_AS, p
1fb98 44 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  Dup, 0, 0);.    
1fb99 69 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72 65  if( pDup==0 ) re
1fb9a 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 70 45  turn;.    if( pE
1fb9b 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41  List->a[iCol].iA
1fb9c 6c 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20  lias==0 ){.     
1fb9d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d   pEList->a[iCol]
1fb9e 2e 69 41 6c 69 61 73 20 3d 20 28 75 31 36 29 28  .iAlias = (u16)(
1fb9f 2b 2b 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73  ++pParse->nAlias
1fba0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 44 75  );.    }.    pDu
1fba1 70 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 4c 69  p->iTable = pELi
1fba2 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69  st->a[iCol].iAli
1fba3 61 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  as;.  }else if( 
1fba4 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
1fba5 70 4f 72 69 67 2c 20 45 50 5f 49 6e 74 56 61 6c  pOrig, EP_IntVal
1fba6 75 65 29 20 7c 7c 20 70 4f 72 69 67 2d 3e 75 2e  ue) || pOrig->u.
1fba7 7a 54 6f 6b 65 6e 3d 3d 30 20 29 7b 0a 20 20 20  zToken==0 ){.   
1fba8 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45   pDup = sqlite3E
1fba9 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 69 67  xprDup(db, pOrig
1fbaa 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 44  , 0);.    if( pD
1fbab 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  up==0 ) return;.
1fbac 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61    }else{.    cha
1fbad 72 20 2a 7a 54 6f 6b 65 6e 20 3d 20 70 4f 72 69  r *zToken = pOri
1fbae 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20  g->u.zToken;.   
1fbaf 20 61 73 73 65 72 74 28 20 7a 54 6f 6b 65 6e 21   assert( zToken!
1fbb0 3d 30 20 29 3b 0a 20 20 20 20 70 4f 72 69 67 2d  =0 );.    pOrig-
1fbb1 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20  >u.zToken = 0;. 
1fbb2 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65     pDup = sqlite
1fbb3 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72  3ExprDup(db, pOr
1fbb4 69 67 2c 20 30 29 3b 0a 20 20 20 20 70 4f 72 69  ig, 0);.    pOri
1fbb5 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 7a 54  g->u.zToken = zT
1fbb6 6f 6b 65 6e 3b 0a 20 20 20 20 69 66 28 20 70 44  oken;.    if( pD
1fbb7 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  up==0 ) return;.
1fbb8 20 20 20 20 61 73 73 65 72 74 28 20 28 70 44 75      assert( (pDu
1fbb9 70 2d 3e 66 6c 61 67 73 20 26 20 28 45 50 5f 52  p->flags & (EP_R
1fbba 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f  educed|EP_TokenO
1fbbb 6e 6c 79 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20  nly))==0 );.    
1fbbc 70 44 75 70 2d 3e 66 6c 61 67 73 32 20 7c 3d 20  pDup->flags2 |= 
1fbbd 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65  EP2_MallocedToke
1fbbe 6e 3b 0a 20 20 20 20 70 44 75 70 2d 3e 75 2e 7a  n;.    pDup->u.z
1fbbf 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33 44  Token = sqlite3D
1fbc0 62 53 74 72 44 75 70 28 64 62 2c 20 7a 54 6f 6b  bStrDup(db, zTok
1fbc1 65 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  en);.  }.  if( p
1fbc2 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50  Expr->flags & EP
1fbc3 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20  _ExpCollate ){. 
1fbc4 20 20 20 70 44 75 70 2d 3e 70 43 6f 6c 6c 20 3d     pDup->pColl =
1fbc5 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a 20   pExpr->pColl;. 
1fbc6 20 20 20 70 44 75 70 2d 3e 66 6c 61 67 73 20 7c     pDup->flags |
1fbc7 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b  = EP_ExpCollate;
1fbc8 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78  .  }.  sqlite3Ex
1fbc9 70 72 43 6c 65 61 72 28 64 62 2c 20 70 45 78 70  prClear(db, pExp
1fbca 72 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 45 78  r);.  memcpy(pEx
1fbcb 70 72 2c 20 70 44 75 70 2c 20 73 69 7a 65 6f 66  pr, pDup, sizeof
1fbcc 28 2a 70 45 78 70 72 29 29 3b 0a 20 20 73 71 6c  (*pExpr));.  sql
1fbcd 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1fbce 44 75 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  Dup);.}../*.** G
1fbcf 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66  iven the name of
1fbd0 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65   a column of the
1fbd1 20 66 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72 20 59   form X.Y.Z or Y
1fbd2 2e 5a 20 6f 72 20 6a 75 73 74 20 5a 2c 20 6c 6f  .Z or just Z, lo
1fbd3 6f 6b 20 75 70 0a 2a 2a 20 74 68 61 74 20 6e 61  ok up.** that na
1fbd4 6d 65 20 69 6e 20 74 68 65 20 73 65 74 20 6f 66  me in the set of
1fbd5 20 73 6f 75 72 63 65 20 74 61 62 6c 65 73 20 69   source tables i
1fbd6 6e 20 70 53 72 63 4c 69 73 74 20 61 6e 64 20 6d  n pSrcList and m
1fbd7 61 6b 65 20 74 68 65 20 70 45 78 70 72 20 0a 2a  ake the pExpr .*
1fbd8 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64  * expression nod
1fbd9 65 20 72 65 66 65 72 20 62 61 63 6b 20 74 6f 20  e refer back to 
1fbda 74 68 61 74 20 73 6f 75 72 63 65 20 63 6f 6c 75  that source colu
1fbdb 6d 6e 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  mn.  The followi
1fbdc 6e 67 20 63 68 61 6e 67 65 73 0a 2a 2a 20 61 72  ng changes.** ar
1fbdd 65 20 6d 61 64 65 20 74 6f 20 70 45 78 70 72 3a  e made to pExpr:
1fbde 0a 2a 2a 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d  .**.**    pExpr-
1fbdf 3e 69 44 62 20 20 20 20 20 20 20 20 20 20 20 53  >iDb           S
1fbe0 65 74 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20  et the index in 
1fbe1 64 62 2d 3e 61 44 62 5b 5d 20 6f 66 20 74 68 65  db->aDb[] of the
1fbe2 20 64 61 74 61 62 61 73 65 20 58 0a 2a 2a 20 20   database X.**  
1fbe3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbe4 20 20 20 20 20 20 20 28 65 76 65 6e 20 69 66 20         (even if 
1fbe5 58 20 69 73 20 69 6d 70 6c 69 65 64 29 2e 0a 2a  X is implied)..*
1fbe6 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62  *    pExpr->iTab
1fbe7 6c 65 20 20 20 20 20 20 20 20 53 65 74 20 74 6f  le        Set to
1fbe8 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62   the cursor numb
1fbe9 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  er for the table
1fbea 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 20 20 20   obtained.**    
1fbeb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbec 20 20 20 20 20 66 72 6f 6d 20 70 53 72 63 4c 69       from pSrcLi
1fbed 73 74 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d  st..**    pExpr-
1fbee 3e 70 54 61 62 20 20 20 20 20 20 20 20 20 20 50  >pTab          P
1fbef 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62  oints to the Tab
1fbf0 6c 65 20 73 74 72 75 63 74 75 72 65 20 6f 66 20  le structure of 
1fbf1 58 2e 59 20 28 65 76 65 6e 20 69 66 0a 2a 2a 20  X.Y (even if.** 
1fbf2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbf3 20 20 20 20 20 20 20 20 58 20 61 6e 64 2f 6f 72          X and/or
1fbf4 20 59 20 61 72 65 20 69 6d 70 6c 69 65 64 2e 29   Y are implied.)
1fbf5 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 43  .**    pExpr->iC
1fbf6 6f 6c 75 6d 6e 20 20 20 20 20 20 20 53 65 74 20  olumn       Set 
1fbf7 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75  to the column nu
1fbf8 6d 62 65 72 20 77 69 74 68 69 6e 20 74 68 65 20  mber within the 
1fbf9 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 70 45 78  table..**    pEx
1fbfa 70 72 2d 3e 6f 70 20 20 20 20 20 20 20 20 20 20  pr->op          
1fbfb 20 20 53 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55    Set to TK_COLU
1fbfc 4d 4e 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d  MN..**    pExpr-
1fbfd 3e 70 4c 65 66 74 20 20 20 20 20 20 20 20 20 41  >pLeft         A
1fbfe 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  ny expression th
1fbff 69 73 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20  is points to is 
1fc00 64 65 6c 65 74 65 64 0a 2a 2a 20 20 20 20 70 45  deleted.**    pE
1fc01 78 70 72 2d 3e 70 52 69 67 68 74 20 20 20 20 20  xpr->pRight     
1fc02 20 20 20 41 6e 79 20 65 78 70 72 65 73 73 69 6f     Any expressio
1fc03 6e 20 74 68 69 73 20 70 6f 69 6e 74 73 20 74 6f  n this points to
1fc04 20 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a   is deleted..**.
1fc05 2a 2a 20 54 68 65 20 7a 44 62 20 76 61 72 69 61  ** The zDb varia
1fc06 62 6c 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20  ble is the name 
1fc07 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1fc08 28 74 68 65 20 22 58 22 29 2e 20 20 54 68 69 73  (the "X").  This
1fc09 20 76 61 6c 75 65 20 6d 61 79 20 62 65 0a 2a 2a   value may be.**
1fc0a 20 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67 20 74 68   NULL meaning th
1fc0b 61 74 20 6e 61 6d 65 20 69 73 20 6f 66 20 74 68  at name is of th
1fc0c 65 20 66 6f 72 6d 20 59 2e 5a 20 6f 72 20 5a 2e  e form Y.Z or Z.
1fc0d 20 20 41 6e 79 20 61 76 61 69 6c 61 62 6c 65 20    Any available 
1fc0e 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 61 6e 20  database.** can 
1fc0f 62 65 20 75 73 65 64 2e 20 20 54 68 65 20 7a 54  be used.  The zT
1fc10 61 62 6c 65 20 76 61 72 69 61 62 6c 65 20 69 73  able variable is
1fc11 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
1fc12 20 74 61 62 6c 65 20 28 74 68 65 20 22 59 22 29   table (the "Y")
1fc13 2e 20 20 54 68 69 73 0a 2a 2a 20 76 61 6c 75 65  .  This.** value
1fc14 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69 66 20   can be NULL if 
1fc15 7a 44 62 20 69 73 20 61 6c 73 6f 20 4e 55 4c 4c  zDb is also NULL
1fc16 2e 20 20 49 66 20 7a 54 61 62 6c 65 20 69 73 20  .  If zTable is 
1fc17 4e 55 4c 4c 20 69 74 0a 2a 2a 20 6d 65 61 6e 73  NULL it.** means
1fc18 20 74 68 61 74 20 74 68 65 20 66 6f 72 6d 20 6f   that the form o
1fc19 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 5a 20  f the name is Z 
1fc1a 61 6e 64 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73  and that columns
1fc1b 20 66 72 6f 6d 20 61 6e 79 20 74 61 62 6c 65 0a   from any table.
1fc1c 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a  ** can be used..
1fc1d 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 61 6d  **.** If the nam
1fc1e 65 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 73 6f  e cannot be reso
1fc1f 6c 76 65 64 20 75 6e 61 6d 62 69 67 75 6f 75 73  lved unambiguous
1fc20 6c 79 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72  ly, leave an err
1fc21 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 6e  or message.** in
1fc22 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 74 75   pParse and retu
1fc23 72 6e 20 57 52 43 5f 41 62 6f 72 74 2e 20 20 52  rn WRC_Abort.  R
1fc24 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 20  eturn WRC_Prune 
1fc25 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73  on success..*/.s
1fc26 74 61 74 69 63 20 69 6e 74 20 6c 6f 6f 6b 75 70  tatic int lookup
1fc27 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70  Name(.  Parse *p
1fc28 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20  Parse,       /* 
1fc29 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
1fc2a 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ext */.  const c
1fc2b 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 2f 2a  har *zDb,     /*
1fc2c 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   Name of the dat
1fc2d 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67  abase containing
1fc2e 20 74 61 62 6c 65 2c 20 6f 72 20 4e 55 4c 4c 20   table, or NULL 
1fc2f 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1fc30 2a 7a 54 61 62 2c 20 20 20 20 2f 2a 20 4e 61 6d  *zTab,    /* Nam
1fc31 65 20 6f 66 20 74 61 62 6c 65 20 63 6f 6e 74 61  e of table conta
1fc32 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c 20 6f 72  ining column, or
1fc33 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74   NULL */.  const
1fc34 20 63 68 61 72 20 2a 7a 43 6f 6c 2c 20 20 20 20   char *zCol,    
1fc35 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63  /* Name of the c
1fc36 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e 61 6d 65  olumn. */.  Name
1fc37 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20  Context *pNC,   
1fc38 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63 6f 6e   /* The name con
1fc39 74 65 78 74 20 75 73 65 64 20 74 6f 20 72 65 73  text used to res
1fc3a 6f 6c 76 65 20 74 68 65 20 6e 61 6d 65 20 2a 2f  olve the name */
1fc3b 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20  .  Expr *pExpr  
1fc3c 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20          /* Make 
1fc3d 74 68 69 73 20 45 58 50 52 20 6e 6f 64 65 20 70  this EXPR node p
1fc3e 6f 69 6e 74 20 74 6f 20 74 68 65 20 73 65 6c 65  oint to the sele
1fc3f 63 74 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 29  cted column */.)
1fc40 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20  {.  int i, j;   
1fc41 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
1fc42 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69   counters */.  i
1fc43 6e 74 20 63 6e 74 20 3d 20 30 3b 20 20 20 20 20  nt cnt = 0;     
1fc44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc45 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61   /* Number of ma
1fc46 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 20 6e 61  tching column na
1fc47 6d 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74  mes */.  int cnt
1fc48 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20  Tab = 0;        
1fc49 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1fc4a 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e 67  mber of matching
1fc4b 20 74 61 62 6c 65 20 6e 61 6d 65 73 20 2a 2f 0a   table names */.
1fc4c 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1fc4d 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20  pParse->db;     
1fc4e 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
1fc4f 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
1fc50 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  /.  struct SrcLi
1fc51 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20  st_item *pItem; 
1fc52 20 20 20 20 20 20 2f 2a 20 55 73 65 20 66 6f 72        /* Use for
1fc53 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 70 53   looping over pS
1fc54 72 63 4c 69 73 74 20 69 74 65 6d 73 20 2a 2f 0a  rcList items */.
1fc55 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
1fc56 5f 69 74 65 6d 20 2a 70 4d 61 74 63 68 20 3d 20  _item *pMatch = 
1fc57 30 3b 20 20 2f 2a 20 54 68 65 20 6d 61 74 63 68  0;  /* The match
1fc58 69 6e 67 20 70 53 72 63 4c 69 73 74 20 69 74 65  ing pSrcList ite
1fc59 6d 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65  m */.  NameConte
1fc5a 78 74 20 2a 70 54 6f 70 4e 43 20 3d 20 70 4e 43  xt *pTopNC = pNC
1fc5b 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73  ;        /* Firs
1fc5c 74 20 6e 61 6d 65 63 6f 6e 74 65 78 74 20 69 6e  t namecontext in
1fc5d 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 53   the list */.  S
1fc5e 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d  chema *pSchema =
1fc5f 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1fc60 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 74 68   /* Schema of th
1fc61 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  e expression */.
1fc62 20 20 69 6e 74 20 69 73 54 72 69 67 67 65 72 20    int isTrigger 
1fc63 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  = 0;..  assert( 
1fc64 70 4e 43 20 29 3b 20 20 20 20 20 2f 2a 20 74 68  pNC );     /* th
1fc65 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 63  e name context c
1fc66 61 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 20 2a  annot be NULL. *
1fc67 2f 0a 20 20 61 73 73 65 72 74 28 20 7a 43 6f 6c  /.  assert( zCol
1fc68 20 29 3b 20 20 20 20 2f 2a 20 54 68 65 20 5a 20   );    /* The Z 
1fc69 69 6e 20 58 2e 59 2e 5a 20 63 61 6e 6e 6f 74 20  in X.Y.Z cannot 
1fc6a 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 61 73 73  be NULL */.  ass
1fc6b 65 72 74 28 20 7e 45 78 70 72 48 61 73 41 6e 79  ert( ~ExprHasAny
1fc6c 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
1fc6d 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f  EP_TokenOnly|EP_
1fc6e 52 65 64 75 63 65 64 29 20 29 3b 0a 0a 20 20 2f  Reduced) );..  /
1fc6f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
1fc70 20 6e 6f 64 65 20 74 6f 20 6e 6f 2d 6d 61 74 63   node to no-matc
1fc71 68 20 2a 2f 0a 20 20 70 45 78 70 72 2d 3e 69 54  h */.  pExpr->iT
1fc72 61 62 6c 65 20 3d 20 2d 31 3b 0a 20 20 70 45 78  able = -1;.  pEx
1fc73 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20  pr->pTab = 0;.  
1fc74 45 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62  ExprSetIrreducib
1fc75 6c 65 28 70 45 78 70 72 29 3b 0a 0a 20 20 2f 2a  le(pExpr);..  /*
1fc76 20 53 74 61 72 74 20 61 74 20 74 68 65 20 69 6e   Start at the in
1fc77 6e 65 72 2d 6d 6f 73 74 20 63 6f 6e 74 65 78 74  ner-most context
1fc78 20 61 6e 64 20 6d 6f 76 65 20 6f 75 74 77 61 72   and move outwar
1fc79 64 20 75 6e 74 69 6c 20 61 20 6d 61 74 63 68 20  d until a match 
1fc7a 69 73 20 66 6f 75 6e 64 20 2a 2f 0a 20 20 77 68  is found */.  wh
1fc7b 69 6c 65 28 20 70 4e 43 20 26 26 20 63 6e 74 3d  ile( pNC && cnt=
1fc7c 3d 30 20 29 7b 0a 20 20 20 20 45 78 70 72 4c 69  =0 ){.    ExprLi
1fc7d 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 20 20  st *pEList;.    
1fc7e 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73  SrcList *pSrcLis
1fc7f 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73  t = pNC->pSrcLis
1fc80 74 3b 0a 0a 20 20 20 20 69 66 28 20 70 53 72 63  t;..    if( pSrc
1fc81 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 66 6f  List ){.      fo
1fc82 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 53 72  r(i=0, pItem=pSr
1fc83 63 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 53 72 63  cList->a; i<pSrc
1fc84 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c  List->nSrc; i++,
1fc85 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
1fc86 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
1fc87 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b          int iDb;
1fc88 0a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20  .        Column 
1fc89 2a 70 43 6f 6c 3b 0a 20 20 0a 20 20 20 20 20 20  *pCol;.  .      
1fc8a 20 20 70 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e    pTab = pItem->
1fc8b 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 61 73  pTab;.        as
1fc8c 73 65 72 74 28 20 70 54 61 62 21 3d 30 20 26 26  sert( pTab!=0 &&
1fc8d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 21 3d 30 20   pTab->zName!=0 
1fc8e 29 3b 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d  );.        iDb =
1fc8f 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
1fc90 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e  Index(db, pTab->
1fc91 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20  pSchema);.      
1fc92 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e    assert( pTab->
1fc93 6e 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20  nCol>0 );.      
1fc94 20 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20    if( zTab ){.  
1fc95 20 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65          if( pIte
1fc96 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20 20  m->zAlias ){.   
1fc97 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
1fc98 54 61 62 4e 61 6d 65 20 3d 20 70 49 74 65 6d 2d  TabName = pItem-
1fc99 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20 20 20  >zAlias;.       
1fc9a 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1fc9b 53 74 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65  StrICmp(zTabName
1fc9c 2c 20 7a 54 61 62 29 21 3d 30 20 29 20 63 6f 6e  , zTab)!=0 ) con
1fc9d 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20  tinue;.         
1fc9e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1fc9f 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61      char *zTabNa
1fca0 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  me = pTab->zName
1fca1 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
1fca2 28 20 4e 45 56 45 52 28 7a 54 61 62 4e 61 6d 65  ( NEVER(zTabName
1fca3 3d 3d 30 29 20 7c 7c 20 73 71 6c 69 74 65 33 53  ==0) || sqlite3S
1fca4 74 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 2c  trICmp(zTabName,
1fca5 20 7a 54 61 62 29 21 3d 30 20 29 7b 0a 20 20 20   zTab)!=0 ){.   
1fca6 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69             conti
1fca7 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nue;.           
1fca8 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69   }.            i
1fca9 66 28 20 7a 44 62 21 3d 30 20 26 26 20 73 71 6c  f( zDb!=0 && sql
1fcaa 69 74 65 33 53 74 72 49 43 6d 70 28 64 62 2d 3e  ite3StrICmp(db->
1fcab 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20  aDb[iDb].zName, 
1fcac 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  zDb)!=0 ){.     
1fcad 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75           continu
1fcae 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  e;.            }
1fcaf 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1fcb0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
1fcb1 66 28 20 30 3d 3d 28 63 6e 74 54 61 62 2b 2b 29  f( 0==(cntTab++)
1fcb2 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 45   ){.          pE
1fcb3 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49  xpr->iTable = pI
1fcb4 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20  tem->iCursor;.  
1fcb5 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70          pExpr->p
1fcb6 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 20  Tab = pTab;.    
1fcb7 20 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d 20        pSchema = 
1fcb8 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20  pTab->pSchema;. 
1fcb9 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 20           pMatch 
1fcba 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 20  = pItem;.       
1fcbb 20 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a   }.        for(j
1fcbc 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d 3e 61  =0, pCol=pTab->a
1fcbd 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f  Col; j<pTab->nCo
1fcbe 6c 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b  l; j++, pCol++){
1fcbf 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73  .          if( s
1fcc0 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43  qlite3StrICmp(pC
1fcc1 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29  ol->zName, zCol)
1fcc2 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
1fcc3 20 20 20 49 64 4c 69 73 74 20 2a 70 55 73 69 6e     IdList *pUsin
1fcc4 67 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63  g;.            c
1fcc5 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  nt++;.          
1fcc6 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20    pExpr->iTable 
1fcc7 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  = pItem->iCursor
1fcc8 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45  ;.            pE
1fcc9 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 62  xpr->pTab = pTab
1fcca 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4d  ;.            pM
1fccb 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a 20 20  atch = pItem;.  
1fccc 20 20 20 20 20 20 20 20 20 20 70 53 63 68 65 6d            pSchem
1fccd 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  a = pTab->pSchem
1fcce 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f  a;.            /
1fccf 2a 20 53 75 62 73 74 69 74 75 74 65 20 74 68 65  * Substitute the
1fcd0 20 72 6f 77 69 64 20 28 63 6f 6c 75 6d 6e 20 2d   rowid (column -
1fcd1 31 29 20 66 6f 72 20 74 68 65 20 49 4e 54 45 47  1) for the INTEG
1fcd2 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a  ER PRIMARY KEY *
1fcd3 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45  /.            pE
1fcd4 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 6a  xpr->iColumn = j
1fcd5 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f 20  ==pTab->iPKey ? 
1fcd6 2d 31 20 3a 20 28 69 31 36 29 6a 3b 0a 20 20 20  -1 : (i16)j;.   
1fcd7 20 20 20 20 20 20 20 20 20 69 66 28 20 69 3c 70           if( i<p
1fcd8 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 20  SrcList->nSrc-1 
1fcd9 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
1fcda 20 69 66 28 20 70 49 74 65 6d 5b 31 5d 2e 6a 6f   if( pItem[1].jo
1fcdb 69 6e 74 79 70 65 20 26 20 4a 54 5f 4e 41 54 55  intype & JT_NATU
1fcdc 52 41 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  RAL ){.         
1fcdd 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69         /* If thi
1fcde 73 20 6d 61 74 63 68 20 6f 63 63 75 72 72 65 64  s match occurred
1fcdf 20 69 6e 20 74 68 65 20 6c 65 66 74 20 74 61 62   in the left tab
1fce0 6c 65 20 6f 66 20 61 20 6e 61 74 75 72 61 6c 20  le of a natural 
1fce1 6a 6f 69 6e 2c 0a 20 20 20 20 20 20 20 20 20 20  join,.          
1fce2 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 73 6b        ** then sk
1fce3 69 70 20 74 68 65 20 72 69 67 68 74 20 74 61 62  ip the right tab
1fce4 6c 65 20 74 6f 20 61 76 6f 69 64 20 61 20 64 75  le to avoid a du
1fce5 70 6c 69 63 61 74 65 20 6d 61 74 63 68 20 2a 2f  plicate match */
1fce6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1fce7 20 70 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20   pItem++;.      
1fce8 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20            i++;. 
1fce9 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c               }el
1fcea 73 65 20 69 66 28 20 28 70 55 73 69 6e 67 20 3d  se if( (pUsing =
1fceb 20 70 49 74 65 6d 5b 31 5d 2e 70 55 73 69 6e 67   pItem[1].pUsing
1fcec 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
1fced 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
1fcee 69 73 20 6d 61 74 63 68 20 6f 63 63 75 72 73 20  is match occurs 
1fcef 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74  on a column that
1fcf0 20 69 73 20 69 6e 20 74 68 65 20 55 53 49 4e 47   is in the USING
1fcf1 20 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20   clause.        
1fcf2 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 61 20          ** of a 
1fcf3 6a 6f 69 6e 2c 20 73 6b 69 70 20 74 68 65 20 73  join, skip the s
1fcf4 65 61 72 63 68 20 6f 66 20 74 68 65 20 72 69 67  earch of the rig
1fcf5 68 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20  ht table of the 
1fcf6 6a 6f 69 6e 0a 20 20 20 20 20 20 20 20 20 20 20  join.           
1fcf7 20 20 20 20 20 2a 2a 20 74 6f 20 61 76 6f 69 64       ** to avoid
1fcf8 20 61 20 64 75 70 6c 69 63 61 74 65 20 6d 61 74   a duplicate mat
1fcf9 63 68 20 74 68 65 72 65 2e 20 2a 2f 0a 20 20 20  ch there. */.   
1fcfa 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
1fcfb 20 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   k;.            
1fcfc 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70      for(k=0; k<p
1fcfd 55 73 69 6e 67 2d 3e 6e 49 64 3b 20 6b 2b 2b 29  Using->nId; k++)
1fcfe 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
1fcff 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
1fd00 74 72 49 43 6d 70 28 70 55 73 69 6e 67 2d 3e 61  trICmp(pUsing->a
1fd01 5b 6b 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29  [k].zName, zCol)
1fd02 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
1fd03 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d             pItem
1fd04 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ++;.            
1fd05 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20          i++;.   
1fd06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fd07 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
1fd08 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1fd09 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
1fd0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
1fd0b 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1fd0c 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
1fd0d 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1fd0e 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1fd0f 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
1fd10 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
1fd11 20 20 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76      /* If we hav
1fd12 65 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 72 65  e not already re
1fd13 73 6f 6c 76 65 64 20 74 68 65 20 6e 61 6d 65 2c  solved the name,
1fd14 20 74 68 65 6e 20 6d 61 79 62 65 20 0a 20 20 20   then maybe .   
1fd15 20 2a 2a 20 69 74 20 69 73 20 61 20 6e 65 77 2e   ** it is a new.
1fd16 2a 20 6f 72 20 6f 6c 64 2e 2a 20 74 72 69 67 67  * or old.* trigg
1fd17 65 72 20 61 72 67 75 6d 65 6e 74 20 72 65 66 65  er argument refe
1fd18 72 65 6e 63 65 0a 20 20 20 20 2a 2f 0a 20 20 20  rence.    */.   
1fd19 20 69 66 28 20 7a 44 62 3d 3d 30 20 26 26 20 7a   if( zDb==0 && z
1fd1a 54 61 62 21 3d 30 20 26 26 20 63 6e 74 3d 3d 30  Tab!=0 && cnt==0
1fd1b 20 26 26 20 70 50 61 72 73 65 2d 3e 70 54 72 69   && pParse->pTri
1fd1c 67 67 65 72 54 61 62 21 3d 30 20 29 7b 0a 20 20  ggerTab!=0 ){.  
1fd1d 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 70 50 61      int op = pPa
1fd1e 72 73 65 2d 3e 65 54 72 69 67 67 65 72 4f 70 3b  rse->eTriggerOp;
1fd1f 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54  .      Table *pT
1fd20 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73  ab = 0;.      as
1fd21 73 65 72 74 28 20 6f 70 3d 3d 54 4b 5f 44 45 4c  sert( op==TK_DEL
1fd22 45 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 50  ETE || op==TK_UP
1fd23 44 41 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49  DATE || op==TK_I
1fd24 4e 53 45 52 54 20 29 3b 0a 20 20 20 20 20 20 69  NSERT );.      i
1fd25 66 28 20 6f 70 21 3d 54 4b 5f 44 45 4c 45 54 45  f( op!=TK_DELETE
1fd26 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
1fd27 6d 70 28 22 6e 65 77 22 2c 7a 54 61 62 29 20 3d  mp("new",zTab) =
1fd28 3d 20 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70  = 0 ){.        p
1fd29 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 31  Expr->iTable = 1
1fd2a 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d  ;.        pTab =
1fd2b 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65   pParse->pTrigge
1fd2c 72 54 61 62 3b 0a 20 20 20 20 20 20 7d 65 6c 73  rTab;.      }els
1fd2d 65 20 69 66 28 20 6f 70 21 3d 54 4b 5f 49 4e 53  e if( op!=TK_INS
1fd2e 45 52 54 20 26 26 20 73 71 6c 69 74 65 33 53 74  ERT && sqlite3St
1fd2f 72 49 43 6d 70 28 22 6f 6c 64 22 2c 7a 54 61 62  rICmp("old",zTab
1fd30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
1fd31 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20  pExpr->iTable = 
1fd32 30 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20  0;.        pTab 
1fd33 3d 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67  = pParse->pTrigg
1fd34 65 72 54 61 62 3b 0a 20 20 20 20 20 20 7d 0a 0a  erTab;.      }..
1fd35 20 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29        if( pTab )
1fd36 7b 20 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  { .        int i
1fd37 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 53 63  Col;.        pSc
1fd38 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63  hema = pTab->pSc
1fd39 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 63 6e  hema;.        cn
1fd3a 74 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20  tTab++;.        
1fd3b 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f 77  if( sqlite3IsRow
1fd3c 69 64 28 7a 43 6f 6c 29 20 29 7b 0a 20 20 20 20  id(zCol) ){.    
1fd3d 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 2d 31 3b        iCol = -1;
1fd3e 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
1fd3f 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 43            for(iC
1fd40 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d  ol=0; iCol<pTab-
1fd41 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a  >nCol; iCol++){.
1fd42 20 20 20 20 20 20 20 20 20 20 20 20 43 6f 6c 75              Colu
1fd43 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 54 61 62  mn *pCol = &pTab
1fd44 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20  ->aCol[iCol];.  
1fd45 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71            if( sq
1fd46 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f  lite3StrICmp(pCo
1fd47 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d  l->zName, zCol)=
1fd48 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
1fd49 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 54      if( iCol==pT
1fd4a 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20  ab->iPKey ){.   
1fd4b 20 20 20 20 20 20 20 20 20 20 20 20 20 69 43 6f               iCo
1fd4c 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20  l = -1;.        
1fd4d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1fd4e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1fd4f 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1fd50 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
1fd51 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f  .        if( iCo
1fd52 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a  l<pTab->nCol ){.
1fd53 20 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b            cnt++;
1fd54 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69  .          if( i
1fd55 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  Col<0 ){.       
1fd56 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 69       pExpr->affi
1fd57 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46  nity = SQLITE_AF
1fd58 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20  F_INTEGER;.     
1fd59 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
1fd5a 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 30 20  Expr->iTable==0 
1fd5b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74  ){.            t
1fd5c 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d 3d 33  estcase( iCol==3
1fd5d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  1 );.           
1fd5e 20 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d   testcase( iCol=
1fd5f 3d 33 32 20 29 3b 0a 20 20 20 20 20 20 20 20 20  =32 );.         
1fd60 20 20 20 70 50 61 72 73 65 2d 3e 6f 6c 64 6d 61     pParse->oldma
1fd61 73 6b 20 7c 3d 20 28 69 43 6f 6c 3e 3d 33 32 20  sk |= (iCol>=32 
1fd62 3f 20 30 78 66 66 66 66 66 66 66 66 20 3a 20 28  ? 0xffffffff : (
1fd63 28 28 75 33 32 29 31 29 3c 3c 69 43 6f 6c 29 29  ((u32)1)<<iCol))
1fd64 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1fd65 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
1fd66 43 6f 6c 75 6d 6e 20 3d 20 28 69 31 36 29 69 43  Column = (i16)iC
1fd67 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 70 45  ol;.          pE
1fd68 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 62  xpr->pTab = pTab
1fd69 3b 0a 20 20 20 20 20 20 20 20 20 20 69 73 54 72  ;.          isTr
1fd6a 69 67 67 65 72 20 3d 20 31 3b 0a 20 20 20 20 20  igger = 1;.     
1fd6b 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1fd6c 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65   }.#endif /* !de
1fd6d 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1fd6e 54 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a 20  T_TRIGGER) */.. 
1fd6f 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 50 65 72     /*.    ** Per
1fd70 68 61 70 73 20 74 68 65 20 6e 61 6d 65 20 69 73  haps the name is
1fd71 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
1fd72 74 68 65 20 52 4f 57 49 44 0a 20 20 20 20 2a 2f  the ROWID.    */
1fd73 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20  .    if( cnt==0 
1fd74 26 26 20 63 6e 74 54 61 62 3d 3d 31 20 26 26 20  && cntTab==1 && 
1fd75 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 7a  sqlite3IsRowid(z
1fd76 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 63 6e  Col) ){.      cn
1fd77 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 45 78  t = 1;.      pEx
1fd78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31  pr->iColumn = -1
1fd79 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61  ;.      pExpr->a
1fd7a 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45  ffinity = SQLITE
1fd7b 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20  _AFF_INTEGER;.  
1fd7c 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20    }..    /*.    
1fd7d 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20  ** If the input 
1fd7e 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 5a  is of the form Z
1fd7f 20 28 6e 6f 74 20 59 2e 5a 20 6f 72 20 58 2e 59   (not Y.Z or X.Y
1fd80 2e 5a 29 20 74 68 65 6e 20 74 68 65 20 6e 61 6d  .Z) then the nam
1fd81 65 20 5a 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74  e Z.    ** might
1fd82 20 72 65 66 65 72 20 74 6f 20 61 6e 20 72 65 73   refer to an res
1fd83 75 6c 74 2d 73 65 74 20 61 6c 69 61 73 2e 20 20  ult-set alias.  
1fd84 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66 6f  This happens, fo
1fd85 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 0a  r example, when.
1fd86 20 20 20 20 2a 2a 20 77 65 20 61 72 65 20 72 65      ** we are re
1fd87 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e  solving names in
1fd88 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
1fd89 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
1fd8a 6e 67 20 63 6f 6d 6d 61 6e 64 3a 0a 20 20 20 20  ng command:.    
1fd8b 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 53 45  **.    **     SE
1fd8c 4c 45 43 54 20 61 2b 62 20 41 53 20 78 20 46 52  LECT a+b AS x FR
1fd8d 4f 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20 78  OM table WHERE x
1fd8e 3c 31 30 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20  <10;.    **.    
1fd8f 2a 2a 20 49 6e 20 63 61 73 65 73 20 6c 69 6b 65  ** In cases like
1fd90 20 74 68 69 73 2c 20 72 65 70 6c 61 63 65 20 70   this, replace p
1fd91 45 78 70 72 20 77 69 74 68 20 61 20 63 6f 70 79  Expr with a copy
1fd92 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
1fd93 6f 6e 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 66  on that.    ** f
1fd94 6f 72 6d 73 20 74 68 65 20 72 65 73 75 6c 74 20  orms the result 
1fd95 73 65 74 20 65 6e 74 72 79 20 28 22 61 2b 62 22  set entry ("a+b"
1fd96 20 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 29   in the example)
1fd97 20 61 6e 64 20 72 65 74 75 72 6e 20 69 6d 6d 65   and return imme
1fd98 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20  diately..    ** 
1fd99 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 65 78  Note that the ex
1fd9a 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20  pression in the 
1fd9b 72 65 73 75 6c 74 20 73 65 74 20 73 68 6f 75 6c  result set shoul
1fd9c 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62  d have already b
1fd9d 65 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c  een.    ** resol
1fd9e 76 65 64 20 62 79 20 74 68 65 20 74 69 6d 65 20  ved by the time 
1fd9f 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
1fda0 20 69 73 20 72 65 73 6f 6c 76 65 64 2e 0a 20 20   is resolved..  
1fda1 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74    */.    if( cnt
1fda2 3d 3d 30 20 26 26 20 28 70 45 4c 69 73 74 20 3d  ==0 && (pEList =
1fda3 20 70 4e 43 2d 3e 70 45 4c 69 73 74 29 21 3d 30   pNC->pEList)!=0
1fda4 20 26 26 20 7a 54 61 62 3d 3d 30 20 29 7b 0a 20   && zTab==0 ){. 
1fda5 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
1fda6 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6a  pEList->nExpr; j
1fda7 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61  ++){.        cha
1fda8 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 2d  r *zAs = pEList-
1fda9 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  >a[j].zName;.   
1fdaa 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30 20       if( zAs!=0 
1fdab 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  && sqlite3StrICm
1fdac 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20  p(zAs, zCol)==0 
1fdad 29 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78 70  ){.          Exp
1fdae 72 20 2a 70 4f 72 69 67 3b 0a 20 20 20 20 20 20  r *pOrig;.      
1fdaf 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
1fdb0 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20 70  r->pLeft==0 && p
1fdb1 45 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20  Expr->pRight==0 
1fdb2 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  );.          ass
1fdb3 65 72 74 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c  ert( pExpr->x.pL
1fdb4 69 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ist==0 );.      
1fdb5 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
1fdb6 72 2d 3e 78 2e 70 53 65 6c 65 63 74 3d 3d 30 20  r->x.pSelect==0 
1fdb7 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 72  );.          pOr
1fdb8 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a  ig = pEList->a[j
1fdb9 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20  ].pExpr;.       
1fdba 20 20 20 69 66 28 20 21 70 4e 43 2d 3e 61 6c 6c     if( !pNC->all
1fdbb 6f 77 41 67 67 20 26 26 20 45 78 70 72 48 61 73  owAgg && ExprHas
1fdbc 50 72 6f 70 65 72 74 79 28 70 4f 72 69 67 2c 20  Property(pOrig, 
1fdbd 45 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20  EP_Agg) ){.     
1fdbe 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
1fdbf 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1fdc0 6d 69 73 75 73 65 20 6f 66 20 61 6c 69 61 73 65  misuse of aliase
1fdc1 64 20 61 67 67 72 65 67 61 74 65 20 25 73 22 2c  d aggregate %s",
1fdc2 20 7a 41 73 29 3b 0a 20 20 20 20 20 20 20 20 20   zAs);.         
1fdc3 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
1fdc4 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ort;.          }
1fdc5 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 6f 6c  .          resol
1fdc6 76 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c 20  veAlias(pParse, 
1fdc7 70 45 4c 69 73 74 2c 20 6a 2c 20 70 45 78 70 72  pEList, j, pExpr
1fdc8 2c 20 22 22 29 3b 0a 20 20 20 20 20 20 20 20 20  , "");.         
1fdc9 20 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20   cnt = 1;.      
1fdca 20 20 20 20 70 4d 61 74 63 68 20 3d 20 30 3b 0a      pMatch = 0;.
1fdcb 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
1fdcc 28 20 7a 54 61 62 3d 3d 30 20 26 26 20 7a 44 62  ( zTab==0 && zDb
1fdcd 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20  ==0 );.         
1fdce 20 67 6f 74 6f 20 6c 6f 6f 6b 75 70 6e 61 6d 65   goto lookupname
1fdcf 5f 65 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a  _end;.        }.
1fdd0 20 20 20 20 20 20 7d 20 0a 20 20 20 20 7d 0a 0a        } .    }..
1fdd1 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74      /* Advance t
1fdd2 6f 20 74 68 65 20 6e 65 78 74 20 6e 61 6d 65 20  o the next name 
1fdd3 63 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 6c 6f  context.  The lo
1fdd4 6f 70 20 77 69 6c 6c 20 65 78 69 74 20 77 68 65  op will exit whe
1fdd5 6e 20 65 69 74 68 65 72 0a 20 20 20 20 2a 2a 20  n either.    ** 
1fdd6 77 65 20 68 61 76 65 20 61 20 6d 61 74 63 68 20  we have a match 
1fdd7 28 63 6e 74 3e 30 29 20 6f 72 20 77 68 65 6e 20  (cnt>0) or when 
1fdd8 77 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6e 61  we run out of na
1fdd9 6d 65 20 63 6f 6e 74 65 78 74 73 2e 0a 20 20 20  me contexts..   
1fdda 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d   */.    if( cnt=
1fddb 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4e 43 20  =0 ){.      pNC 
1fddc 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20  = pNC->pNext;.  
1fddd 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20    }.  }..  /*.  
1fdde 2a 2a 20 49 66 20 58 20 61 6e 64 20 59 20 61 72  ** If X and Y ar
1fddf 65 20 4e 55 4c 4c 20 28 69 6e 20 6f 74 68 65 72  e NULL (in other
1fde0 20 77 6f 72 64 73 20 69 66 20 6f 6e 6c 79 20 74   words if only t
1fde1 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 5a  he column name Z
1fde2 20 69 73 0a 20 20 2a 2a 20 73 75 70 70 6c 69 65   is.  ** supplie
1fde3 64 29 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65  d) and the value
1fde4 20 6f 66 20 5a 20 69 73 20 65 6e 63 6c 6f 73 65   of Z is enclose
1fde5 64 20 69 6e 20 64 6f 75 62 6c 65 2d 71 75 6f 74  d in double-quot
1fde6 65 73 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 5a 20  es, then.  ** Z 
1fde7 69 73 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65  is a string lite
1fde8 72 61 6c 20 69 66 20 69 74 20 64 6f 65 73 6e 27  ral if it doesn'
1fde9 74 20 6d 61 74 63 68 20 61 6e 79 20 63 6f 6c 75  t match any colu
1fdea 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 6e 20 74 68  mn names.  In th
1fdeb 61 74 0a 20 20 2a 2a 20 63 61 73 65 2c 20 77 65  at.  ** case, we
1fdec 20 6e 65 65 64 20 74 6f 20 72 65 74 75 72 6e 20   need to return 
1fded 72 69 67 68 74 20 61 77 61 79 20 61 6e 64 20 6e  right away and n
1fdee 6f 74 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e  ot make any chan
1fdef 67 65 73 20 74 6f 0a 20 20 2a 2a 20 70 45 78 70  ges to.  ** pExp
1fdf0 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 65 63  r..  **.  ** Bec
1fdf1 61 75 73 65 20 6e 6f 20 72 65 66 65 72 65 6e 63  ause no referenc
1fdf2 65 20 77 61 73 20 6d 61 64 65 20 74 6f 20 6f 75  e was made to ou
1fdf3 74 65 72 20 63 6f 6e 74 65 78 74 73 2c 20 74 68  ter contexts, th
1fdf4 65 20 70 4e 43 2d 3e 6e 52 65 66 0a 20 20 2a 2a  e pNC->nRef.  **
1fdf5 20 66 69 65 6c 64 73 20 61 72 65 20 6e 6f 74 20   fields are not 
1fdf6 63 68 61 6e 67 65 64 20 69 6e 20 61 6e 79 20 63  changed in any c
1fdf7 6f 6e 74 65 78 74 2e 0a 20 20 2a 2f 0a 20 20 69  ontext..  */.  i
1fdf8 66 28 20 63 6e 74 3d 3d 30 20 26 26 20 7a 54 61  f( cnt==0 && zTa
1fdf9 62 3d 3d 30 20 26 26 20 45 78 70 72 48 61 73 50  b==0 && ExprHasP
1fdfa 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 45 50  roperty(pExpr,EP
1fdfb 5f 44 62 6c 51 75 6f 74 65 64 29 20 29 7b 0a 20  _DblQuoted) ){. 
1fdfc 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54     pExpr->op = T
1fdfd 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 70 45  K_STRING;.    pE
1fdfe 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20  xpr->pTab = 0;. 
1fdff 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72     return WRC_Pr
1fe00 75 6e 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20  une;.  }..  /*. 
1fe01 20 2a 2a 20 63 6e 74 3d 3d 30 20 6d 65 61 6e 73   ** cnt==0 means
1fe02 20 74 68 65 72 65 20 77 61 73 20 6e 6f 74 20 6d   there was not m
1fe03 61 74 63 68 2e 20 20 63 6e 74 3e 31 20 6d 65 61  atch.  cnt>1 mea
1fe04 6e 73 20 74 68 65 72 65 20 77 65 72 65 20 74 77  ns there were tw
1fe05 6f 20 6f 72 0a 20 20 2a 2a 20 6d 6f 72 65 20 6d  o or.  ** more m
1fe06 61 74 63 68 65 73 2e 20 20 45 69 74 68 65 72 20  atches.  Either 
1fe07 77 61 79 2c 20 77 65 20 68 61 76 65 20 61 6e 20  way, we have an 
1fe08 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66  error..  */.  if
1fe09 28 20 63 6e 74 21 3d 31 20 29 7b 0a 20 20 20 20  ( cnt!=1 ){.    
1fe0a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72  const char *zErr
1fe0b 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20 63 6e 74  ;.    zErr = cnt
1fe0c 3d 3d 30 20 3f 20 22 6e 6f 20 73 75 63 68 20 63  ==0 ? "no such c
1fe0d 6f 6c 75 6d 6e 22 20 3a 20 22 61 6d 62 69 67 75  olumn" : "ambigu
1fe0e 6f 75 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 22  ous column name"
1fe0f 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 20 29 7b  ;.    if( zDb ){
1fe10 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
1fe11 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1fe12 25 73 3a 20 25 73 2e 25 73 2e 25 73 22 2c 20 7a  %s: %s.%s.%s", z
1fe13 45 72 72 2c 20 7a 44 62 2c 20 7a 54 61 62 2c 20  Err, zDb, zTab, 
1fe14 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65  zCol);.    }else
1fe15 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20 20   if( zTab ){.   
1fe16 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1fe17 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20  sg(pParse, "%s: 
1fe18 25 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20 7a 54  %s.%s", zErr, zT
1fe19 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d  ab, zCol);.    }
1fe1a 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
1fe1b 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1fe1c 73 65 2c 20 22 25 73 3a 20 25 73 22 2c 20 7a 45  se, "%s: %s", zE
1fe1d 72 72 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d  rr, zCol);.    }
1fe1e 0a 20 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 45 72  .    pTopNC->nEr
1fe1f 72 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  r++;.  }..  /* I
1fe20 66 20 61 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20  f a column from 
1fe21 61 20 74 61 62 6c 65 20 69 6e 20 70 53 72 63 4c  a table in pSrcL
1fe22 69 73 74 20 69 73 20 72 65 66 65 72 65 6e 63 65  ist is reference
1fe23 64 2c 20 74 68 65 6e 20 72 65 63 6f 72 64 0a 20  d, then record. 
1fe24 20 2a 2a 20 74 68 69 73 20 66 61 63 74 20 69 6e   ** this fact in
1fe25 20 74 68 65 20 70 53 72 63 4c 69 73 74 2e 61 5b   the pSrcList.a[
1fe26 5d 2e 63 6f 6c 55 73 65 64 20 62 69 74 6d 61 73  ].colUsed bitmas
1fe27 6b 2e 20 20 43 6f 6c 75 6d 6e 20 30 20 63 61 75  k.  Column 0 cau
1fe28 73 65 73 0a 20 20 2a 2a 20 62 69 74 20 30 20 74  ses.  ** bit 0 t
1fe29 6f 20 62 65 20 73 65 74 2e 20 20 43 6f 6c 75 6d  o be set.  Colum
1fe2a 6e 20 31 20 73 65 74 73 20 62 69 74 20 31 2e 20  n 1 sets bit 1. 
1fe2b 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20   And so forth.  
1fe2c 49 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6c 75  If the.  ** colu
1fe2d 6d 6e 20 6e 75 6d 62 65 72 20 69 73 20 67 72 65  mn number is gre
1fe2e 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 6e 75  ater than the nu
1fe2f 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e 20  mber of bits in 
1fe30 74 68 65 20 62 69 74 6d 61 73 6b 0a 20 20 2a 2a  the bitmask.  **
1fe31 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 68 69   then set the hi
1fe32 67 68 2d 6f 72 64 65 72 20 62 69 74 20 6f 66 20  gh-order bit of 
1fe33 74 68 65 20 62 69 74 6d 61 73 6b 2e 0a 20 20 2a  the bitmask..  *
1fe34 2f 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69  /.  if( pExpr->i
1fe35 43 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70 4d 61  Column>=0 && pMa
1fe36 74 63 68 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e  tch!=0 ){.    in
1fe37 74 20 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f  t n = pExpr->iCo
1fe38 6c 75 6d 6e 3b 0a 20 20 20 20 74 65 73 74 63 61  lumn;.    testca
1fe39 73 65 28 20 6e 3d 3d 42 4d 53 2d 31 20 29 3b 0a  se( n==BMS-1 );.
1fe3a 20 20 20 20 69 66 28 20 6e 3e 3d 42 4d 53 20 29      if( n>=BMS )
1fe3b 7b 0a 20 20 20 20 20 20 6e 20 3d 20 42 4d 53 2d  {.      n = BMS-
1fe3c 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  1;.    }.    ass
1fe3d 65 72 74 28 20 70 4d 61 74 63 68 2d 3e 69 43 75  ert( pMatch->iCu
1fe3e 72 73 6f 72 3d 3d 70 45 78 70 72 2d 3e 69 54 61  rsor==pExpr->iTa
1fe3f 62 6c 65 20 29 3b 0a 20 20 20 20 70 4d 61 74 63  ble );.    pMatc
1fe40 68 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d 20 28 28  h->colUsed |= ((
1fe41 42 69 74 6d 61 73 6b 29 31 29 3c 3c 6e 3b 0a 20  Bitmask)1)<<n;. 
1fe42 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 75   }..  /* Clean u
1fe43 70 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20 2a  p and return.  *
1fe44 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  /.  sqlite3ExprD
1fe45 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 2d  elete(db, pExpr-
1fe46 3e 70 4c 65 66 74 29 3b 0a 20 20 70 45 78 70 72  >pLeft);.  pExpr
1fe47 2d 3e 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20 73  ->pLeft = 0;.  s
1fe48 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
1fe49 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  (db, pExpr->pRig
1fe4a 68 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52  ht);.  pExpr->pR
1fe4b 69 67 68 74 20 3d 20 30 3b 0a 20 20 70 45 78 70  ight = 0;.  pExp
1fe4c 72 2d 3e 6f 70 20 3d 20 28 69 73 54 72 69 67 67  r->op = (isTrigg
1fe4d 65 72 20 3f 20 54 4b 5f 54 52 49 47 47 45 52 20  er ? TK_TRIGGER 
1fe4e 3a 20 54 4b 5f 43 4f 4c 55 4d 4e 29 3b 0a 6c 6f  : TK_COLUMN);.lo
1fe4f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3a 0a 20 20  okupname_end:.  
1fe50 69 66 28 20 63 6e 74 3d 3d 31 20 29 7b 0a 20 20  if( cnt==1 ){.  
1fe51 20 20 61 73 73 65 72 74 28 20 70 4e 43 21 3d 30    assert( pNC!=0
1fe52 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41   );.    sqlite3A
1fe53 75 74 68 52 65 61 64 28 70 50 61 72 73 65 2c 20  uthRead(pParse, 
1fe54 70 45 78 70 72 2c 20 70 53 63 68 65 6d 61 2c 20  pExpr, pSchema, 
1fe55 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 29 3b 0a  pNC->pSrcList);.
1fe56 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74      /* Increment
1fe57 20 74 68 65 20 6e 52 65 66 20 76 61 6c 75 65 20   the nRef value 
1fe58 6f 6e 20 61 6c 6c 20 6e 61 6d 65 20 63 6f 6e 74  on all name cont
1fe59 65 78 74 73 20 66 72 6f 6d 20 54 6f 70 4e 43 20  exts from TopNC 
1fe5a 75 70 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65  up to.    ** the
1fe5b 20 70 6f 69 6e 74 20 77 68 65 72 65 20 74 68 65   point where the
1fe5c 20 6e 61 6d 65 20 6d 61 74 63 68 65 64 2e 20 2a   name matched. *
1fe5d 2f 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20  /.    for(;;){. 
1fe5e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f       assert( pTo
1fe5f 70 4e 43 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  pNC!=0 );.      
1fe60 70 54 6f 70 4e 43 2d 3e 6e 52 65 66 2b 2b 3b 0a  pTopNC->nRef++;.
1fe61 20 20 20 20 20 20 69 66 28 20 70 54 6f 70 4e 43        if( pTopNC
1fe62 3d 3d 70 4e 43 20 29 20 62 72 65 61 6b 3b 0a 20  ==pNC ) break;. 
1fe63 20 20 20 20 20 70 54 6f 70 4e 43 20 3d 20 70 54       pTopNC = pT
1fe64 6f 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  opNC->pNext;.   
1fe65 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52   }.    return WR
1fe66 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 20 65 6c 73  C_Prune;.  } els
1fe67 65 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57  e {.    return W
1fe68 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 7d 0a  RC_Abort;.  }.}.
1fe69 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
1fe6a 69 6e 65 20 69 73 20 63 61 6c 6c 62 61 63 6b 20  ine is callback 
1fe6b 66 6f 72 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  for sqlite3WalkE
1fe6c 78 70 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 73  xpr()..**.** Res
1fe6d 6f 6c 76 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61  olve symbolic na
1fe6e 6d 65 73 20 69 6e 74 6f 20 54 4b 5f 43 4f 4c 55  mes into TK_COLU
1fe6f 4d 4e 20 6f 70 65 72 61 74 6f 72 73 20 66 6f 72  MN operators for
1fe70 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20   the current.** 
1fe71 6e 6f 64 65 20 69 6e 20 74 68 65 20 65 78 70 72  node in the expr
1fe72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65  ession tree.  Re
1fe73 74 75 72 6e 20 30 20 74 6f 20 63 6f 6e 74 69 6e  turn 0 to contin
1fe74 75 65 20 74 68 65 20 73 65 61 72 63 68 20 64 6f  ue the search do
1fe75 77 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20 6f  wn.** the tree o
1fe76 72 20 32 20 74 6f 20 61 62 6f 72 74 20 74 68 65  r 2 to abort the
1fe77 20 74 72 65 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a   tree walk..**.*
1fe78 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
1fe79 6c 73 6f 20 64 6f 65 73 20 65 72 72 6f 72 20 63  lso does error c
1fe7a 68 65 63 6b 69 6e 67 20 61 6e 64 20 6e 61 6d 65  hecking and name
1fe7b 20 72 65 73 6f 6c 75 74 69 6f 6e 20 66 6f 72 0a   resolution for.
1fe7c 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65  ** function name
1fe7d 73 2e 20 20 54 68 65 20 6f 70 65 72 61 74 6f 72  s.  The operator
1fe7e 20 66 6f 72 20 61 67 67 72 65 67 61 74 65 20 66   for aggregate f
1fe7f 75 6e 63 74 69 6f 6e 73 20 69 73 20 63 68 61 6e  unctions is chan
1fe80 67 65 64 0a 2a 2a 20 74 6f 20 54 4b 5f 41 47 47  ged.** to TK_AGG
1fe81 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74  _FUNCTION..*/.st
1fe82 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65  atic int resolve
1fe83 45 78 70 72 53 74 65 70 28 57 61 6c 6b 65 72 20  ExprStep(Walker 
1fe84 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a  *pWalker, Expr *
1fe85 70 45 78 70 72 29 7b 0a 20 20 4e 61 6d 65 43 6f  pExpr){.  NameCo
1fe86 6e 74 65 78 74 20 2a 70 4e 43 3b 0a 20 20 50 61  ntext *pNC;.  Pa
1fe87 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20 20  rse *pParse;..  
1fe88 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75  pNC = pWalker->u
1fe89 2e 70 4e 43 3b 0a 20 20 61 73 73 65 72 74 28 20  .pNC;.  assert( 
1fe8a 70 4e 43 21 3d 30 20 29 3b 0a 20 20 70 50 61 72  pNC!=0 );.  pPar
1fe8b 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65  se = pNC->pParse
1fe8c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
1fe8d 73 65 3d 3d 70 57 61 6c 6b 65 72 2d 3e 70 50 61  se==pWalker->pPa
1fe8e 72 73 65 20 29 3b 0a 0a 20 20 69 66 28 20 45 78  rse );..  if( Ex
1fe8f 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
1fe90 28 70 45 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c  (pExpr, EP_Resol
1fe91 76 65 64 29 20 29 20 72 65 74 75 72 6e 20 57 52  ved) ) return WR
1fe92 43 5f 50 72 75 6e 65 3b 0a 20 20 45 78 70 72 53  C_Prune;.  ExprS
1fe93 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  etProperty(pExpr
1fe94 2c 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 3b 0a  , EP_Resolved);.
1fe95 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
1fe96 20 69 66 28 20 70 4e 43 2d 3e 70 53 72 63 4c 69   if( pNC->pSrcLi
1fe97 73 74 20 26 26 20 70 4e 43 2d 3e 70 53 72 63 4c  st && pNC->pSrcL
1fe98 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3e 30 20 29 7b  ist->nAlloc>0 ){
1fe99 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53  .    SrcList *pS
1fe9a 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53  rcList = pNC->pS
1fe9b 72 63 4c 69 73 74 3b 0a 20 20 20 20 69 6e 74 20  rcList;.    int 
1fe9c 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  i;.    for(i=0; 
1fe9d 69 3c 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d  i<pNC->pSrcList-
1fe9e 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nSrc; i++){.   
1fe9f 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63 4c     assert( pSrcL
1fea0 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f  ist->a[i].iCurso
1fea1 72 3e 3d 30 20 26 26 20 70 53 72 63 4c 69 73 74  r>=0 && pSrcList
1fea2 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3c 70  ->a[i].iCursor<p
1fea3 50 61 72 73 65 2d 3e 6e 54 61 62 29 3b 0a 20 20  Parse->nTab);.  
1fea4 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20    }.  }.#endif. 
1fea5 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e   switch( pExpr->
1fea6 6f 70 20 29 7b 0a 0a 23 69 66 20 64 65 66 69 6e  op ){..#if defin
1fea7 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
1fea8 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c  _UPDATE_DELETE_L
1fea9 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65  IMIT) && !define
1feaa 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  d(SQLITE_OMIT_SU
1feab 42 51 55 45 52 59 29 0a 20 20 20 20 2f 2a 20 54  BQUERY).    /* T
1feac 68 65 20 73 70 65 63 69 61 6c 20 6f 70 65 72 61  he special opera
1fead 74 6f 72 20 54 4b 5f 52 4f 57 20 6d 65 61 6e 73  tor TK_ROW means
1feae 20 75 73 65 20 74 68 65 20 72 6f 77 69 64 20 66   use the rowid f
1feaf 6f 72 20 74 68 65 20 66 69 72 73 74 0a 20 20 20  or the first.   
1feb0 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68   ** column in th
1feb1 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20  e FROM clause.  
1feb2 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20  This is used by 
1feb3 74 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 52  the LIMIT and OR
1feb4 44 45 52 20 42 59 0a 20 20 20 20 2a 2a 20 63 6c  DER BY.    ** cl
1feb5 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20  ause processing 
1feb6 6f 6e 20 55 50 44 41 54 45 20 61 6e 64 20 44 45  on UPDATE and DE
1feb7 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e  LETE statements.
1feb8 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
1feb9 20 54 4b 5f 52 4f 57 3a 20 7b 0a 20 20 20 20 20   TK_ROW: {.     
1feba 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69   SrcList *pSrcLi
1febb 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69  st = pNC->pSrcLi
1febc 73 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74  st;.      struct
1febd 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
1febe 49 74 65 6d 3b 0a 20 20 20 20 20 20 61 73 73 65  Item;.      asse
1febf 72 74 28 20 70 53 72 63 4c 69 73 74 20 26 26 20  rt( pSrcList && 
1fec0 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d  pSrcList->nSrc==
1fec1 31 20 29 3b 0a 20 20 20 20 20 20 70 49 74 65 6d  1 );.      pItem
1fec2 20 3d 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20   = pSrcList->a; 
1fec3 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70  .      pExpr->op
1fec4 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20   = TK_COLUMN;.  
1fec5 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20      pExpr->pTab 
1fec6 3d 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20  = pItem->pTab;. 
1fec7 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62       pExpr->iTab
1fec8 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72  le = pItem->iCur
1fec9 73 6f 72 3b 0a 20 20 20 20 20 20 70 45 78 70 72  sor;.      pExpr
1feca 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a  ->iColumn = -1;.
1fecb 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66        pExpr->aff
1fecc 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41  inity = SQLITE_A
1fecd 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20  FF_INTEGER;.    
1fece 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
1fecf 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64  endif /* defined
1fed0 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55  (SQLITE_ENABLE_U
1fed1 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d  PDATE_DELETE_LIM
1fed2 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  IT) && !defined(
1fed3 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
1fed4 55 45 52 59 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a  UERY) */..    /*
1fed5 20 41 20 6c 6f 6e 65 20 69 64 65 6e 74 69 66 69   A lone identifi
1fed6 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  er is the name o
1fed7 66 20 61 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20  f a column..    
1fed8 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49  */.    case TK_I
1fed9 44 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72  D: {.      retur
1feda 6e 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61  n lookupName(pPa
1fedb 72 73 65 2c 20 30 2c 20 30 2c 20 70 45 78 70 72  rse, 0, 0, pExpr
1fedc 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 70 4e 43 2c  ->u.zToken, pNC,
1fedd 20 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20   pExpr);.    }. 
1fede 20 0a 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65   .    /* A table
1fedf 20 6e 61 6d 65 20 61 6e 64 20 63 6f 6c 75 6d 6e   name and column
1fee0 20 6e 61 6d 65 3a 20 20 20 20 20 49 44 2e 49 44   name:     ID.ID
1fee1 0a 20 20 20 20 2a 2a 20 4f 72 20 61 20 64 61 74  .    ** Or a dat
1fee2 61 62 61 73 65 2c 20 74 61 62 6c 65 20 61 6e 64  abase, table and
1fee3 20 63 6f 6c 75 6d 6e 3a 20 20 49 44 2e 49 44 2e   column:  ID.ID.
1fee4 49 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  ID.    */.    ca
1fee5 73 65 20 54 4b 5f 44 4f 54 3a 20 7b 0a 20 20 20  se TK_DOT: {.   
1fee6 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1fee7 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 63 6f  Column;.      co
1fee8 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65  nst char *zTable
1fee9 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  ;.      const ch
1feea 61 72 20 2a 7a 44 62 3b 0a 20 20 20 20 20 20 45  ar *zDb;.      E
1feeb 78 70 72 20 2a 70 52 69 67 68 74 3b 0a 0a 20 20  xpr *pRight;..  
1feec 20 20 20 20 2f 2a 20 69 66 28 20 70 53 72 63 4c      /* if( pSrcL
1feed 69 73 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b 20  ist==0 ) break; 
1feee 2a 2f 0a 20 20 20 20 20 20 70 52 69 67 68 74 20  */.      pRight 
1feef 3d 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b  = pExpr->pRight;
1fef0 0a 20 20 20 20 20 20 69 66 28 20 70 52 69 67 68  .      if( pRigh
1fef1 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a  t->op==TK_ID ){.
1fef2 20 20 20 20 20 20 20 20 7a 44 62 20 3d 20 30 3b          zDb = 0;
1fef3 0a 20 20 20 20 20 20 20 20 7a 54 61 62 6c 65 20  .        zTable 
1fef4 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e  = pExpr->pLeft->
1fef5 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20  u.zToken;.      
1fef6 20 20 7a 43 6f 6c 75 6d 6e 20 3d 20 70 52 69 67    zColumn = pRig
1fef7 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20  ht->u.zToken;.  
1fef8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1fef9 20 20 20 61 73 73 65 72 74 28 20 70 52 69 67 68     assert( pRigh
1fefa 74 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29 3b  t->op==TK_DOT );
1fefb 0a 20 20 20 20 20 20 20 20 7a 44 62 20 3d 20 70  .        zDb = p
1fefc 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 75 2e 7a  Expr->pLeft->u.z
1fefd 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 7a  Token;.        z
1fefe 54 61 62 6c 65 20 3d 20 70 52 69 67 68 74 2d 3e  Table = pRight->
1feff 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b  pLeft->u.zToken;
1ff00 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d 6e  .        zColumn
1ff01 20 3d 20 70 52 69 67 68 74 2d 3e 70 52 69 67 68   = pRight->pRigh
1ff02 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20  t->u.zToken;.   
1ff03 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72     }.      retur
1ff04 6e 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50 61  n lookupName(pPa
1ff05 72 73 65 2c 20 7a 44 62 2c 20 7a 54 61 62 6c 65  rse, zDb, zTable
1ff06 2c 20 7a 43 6f 6c 75 6d 6e 2c 20 70 4e 43 2c 20  , zColumn, pNC, 
1ff07 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 0a 20  pExpr);.    }.. 
1ff08 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 66 75     /* Resolve fu
1ff09 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 0a 20 20 20  nction names.   
1ff0a 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   */.    case TK_
1ff0b 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20  CONST_FUNC:.    
1ff0c 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e  case TK_FUNCTION
1ff0d 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69  : {.      ExprLi
1ff0e 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70  st *pList = pExp
1ff0f 72 2d 3e 78 2e 70 4c 69 73 74 3b 20 20 20 20 2f  r->x.pList;    /
1ff10 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 6c  * The argument l
1ff11 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ist */.      int
1ff12 20 6e 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c 69   n = pList ? pLi
1ff13 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 20 20  st->nExpr : 0;  
1ff14 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
1ff15 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20  rguments */.    
1ff16 20 20 69 6e 74 20 6e 6f 5f 73 75 63 68 5f 66 75    int no_such_fu
1ff17 6e 63 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a  nc = 0;       /*
1ff18 20 54 72 75 65 20 69 66 20 6e 6f 20 73 75 63 68   True if no such
1ff19 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73   function exists
1ff1a 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 77 72   */.      int wr
1ff1b 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 30  ong_num_args = 0
1ff1c 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  ;     /* True if
1ff1d 20 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66   wrong number of
1ff1e 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20   arguments */.  
1ff1f 20 20 20 20 69 6e 74 20 69 73 5f 61 67 67 20 3d      int is_agg =
1ff20 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1ff21 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 6e  /* True if is an
1ff22 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
1ff23 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ion */.      int
1ff24 20 61 75 74 68 3b 20 20 20 20 20 20 20 20 20 20   auth;          
1ff25 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 74 68           /* Auth
1ff26 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20 75 73 65  orization to use
1ff27 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f   the function */
1ff28 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20  .      int nId; 
1ff29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff2a 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1ff2b 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 66 75  characters in fu
1ff2c 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20  nction name */. 
1ff2d 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
1ff2e 2a 7a 49 64 3b 20 20 20 20 20 20 20 20 20 20 20  *zId;           
1ff2f 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e   /* The function
1ff30 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20   name. */.      
1ff31 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20 20  FuncDef *pDef;  
1ff32 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
1ff33 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
1ff34 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f   the function */
1ff35 0a 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d 20  .      u8 enc = 
1ff36 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 3b  ENC(pParse->db);
1ff37 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
1ff38 73 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 0a  se encoding */..
1ff39 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1ff3a 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  pExpr->op==TK_CO
1ff3b 4e 53 54 5f 46 55 4e 43 20 29 3b 0a 20 20 20 20  NST_FUNC );.    
1ff3c 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
1ff3d 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
1ff3e 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
1ff3f 29 3b 0a 20 20 20 20 20 20 7a 49 64 20 3d 20 70  );.      zId = p
1ff40 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a  Expr->u.zToken;.
1ff41 20 20 20 20 20 20 6e 49 64 20 3d 20 73 71 6c 69        nId = sqli
1ff42 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 49 64 29  te3Strlen30(zId)
1ff43 3b 0a 20 20 20 20 20 20 70 44 65 66 20 3d 20 73  ;.      pDef = s
1ff44 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
1ff45 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a  on(pParse->db, z
1ff46 49 64 2c 20 6e 49 64 2c 20 6e 2c 20 65 6e 63 2c  Id, nId, n, enc,
1ff47 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70   0);.      if( p
1ff48 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Def==0 ){.      
1ff49 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33    pDef = sqlite3
1ff4a 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61  FindFunction(pPa
1ff4b 72 73 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e 49  rse->db, zId, nI
1ff4c 64 2c 20 2d 31 2c 20 65 6e 63 2c 20 30 29 3b 0a  d, -1, enc, 0);.
1ff4d 20 20 20 20 20 20 20 20 69 66 28 20 70 44 65 66          if( pDef
1ff4e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
1ff4f 20 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d 20   no_such_func = 
1ff50 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  1;.        }else
1ff51 7b 0a 20 20 20 20 20 20 20 20 20 20 77 72 6f 6e  {.          wron
1ff52 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 31 3b 0a  g_num_args = 1;.
1ff53 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1ff54 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69  }else{.        i
1ff55 73 5f 61 67 67 20 3d 20 70 44 65 66 2d 3e 78 46  s_agg = pDef->xF
1ff56 75 6e 63 3d 3d 30 3b 0a 20 20 20 20 20 20 7d 0a  unc==0;.      }.
1ff57 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1ff58 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
1ff59 4e 0a 20 20 20 20 20 20 69 66 28 20 70 44 65 66  N.      if( pDef
1ff5a 20 29 7b 0a 20 20 20 20 20 20 20 20 61 75 74 68   ){.        auth
1ff5b 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43 68   = sqlite3AuthCh
1ff5c 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
1ff5d 54 45 5f 46 55 4e 43 54 49 4f 4e 2c 20 30 2c 20  TE_FUNCTION, 0, 
1ff5e 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b  pDef->zName, 0);
1ff5f 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 75 74  .        if( aut
1ff60 68 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  h!=SQLITE_OK ){.
1ff61 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61 75            if( au
1ff62 74 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20  th==SQLITE_DENY 
1ff63 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ){.            s
1ff64 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1ff65 50 61 72 73 65 2c 20 22 6e 6f 74 20 61 75 74 68  Parse, "not auth
1ff66 6f 72 69 7a 65 64 20 74 6f 20 75 73 65 20 66 75  orized to use fu
1ff67 6e 63 74 69 6f 6e 3a 20 25 73 22 2c 0a 20 20 20  nction: %s",.   
1ff68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff6a 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pDef->zName);. 
1ff6b 20 20 20 20 20 20 20 20 20 20 20 70 4e 43 2d 3e             pNC->
1ff6c 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20  nErr++;.        
1ff6d 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 45    }.          pE
1ff6e 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c  xpr->op = TK_NUL
1ff6f 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  L;.          ret
1ff70 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20  urn WRC_Prune;. 
1ff71 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1ff72 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66  .#endif.      if
1ff73 28 20 69 73 5f 61 67 67 20 26 26 20 21 70 4e 43  ( is_agg && !pNC
1ff74 2d 3e 61 6c 6c 6f 77 41 67 67 20 29 7b 0a 20 20  ->allowAgg ){.  
1ff75 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1ff76 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d  orMsg(pParse, "m
1ff77 69 73 75 73 65 20 6f 66 20 61 67 67 72 65 67 61  isuse of aggrega
1ff78 74 65 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73  te function %.*s
1ff79 28 29 22 2c 20 6e 49 64 2c 7a 49 64 29 3b 0a 20  ()", nId,zId);. 
1ff7a 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72         pNC->nErr
1ff7b 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69 73 5f 61  ++;.        is_a
1ff7c 67 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65  gg = 0;.      }e
1ff7d 6c 73 65 20 69 66 28 20 6e 6f 5f 73 75 63 68 5f  lse if( no_such_
1ff7e 66 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20  func ){.        
1ff7f 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1ff80 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68  pParse, "no such
1ff81 20 66 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a 73 22   function: %.*s"
1ff82 2c 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20  , nId, zId);.   
1ff83 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b       pNC->nErr++
1ff84 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
1ff85 28 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73  ( wrong_num_args
1ff86 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1ff87 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1ff88 73 65 2c 22 77 72 6f 6e 67 20 6e 75 6d 62 65 72  se,"wrong number
1ff89 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f   of arguments to
1ff8a 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 29   function %.*s()
1ff8b 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
1ff8c 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20  nId, zId);.     
1ff8d 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a     pNC->nErr++;.
1ff8e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
1ff8f 28 20 69 73 5f 61 67 67 20 29 7b 0a 20 20 20 20  ( is_agg ){.    
1ff90 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20      pExpr->op = 
1ff91 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3b  TK_AGG_FUNCTION;
1ff92 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 68 61  .        pNC->ha
1ff93 73 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20  sAgg = 1;.      
1ff94 7d 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f 61  }.      if( is_a
1ff95 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41  gg ) pNC->allowA
1ff96 67 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71  gg = 0;.      sq
1ff97 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73  lite3WalkExprLis
1ff98 74 28 70 57 61 6c 6b 65 72 2c 20 70 4c 69 73 74  t(pWalker, pList
1ff99 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f  );.      if( is_
1ff9a 61 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77  agg ) pNC->allow
1ff9b 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 2f  Agg = 1;.      /
1ff9c 2a 20 46 49 58 20 4d 45 3a 20 20 43 6f 6d 70 75  * FIX ME:  Compu
1ff9d 74 65 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69  te pExpr->affini
1ff9e 74 79 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20  ty based on the 
1ff9f 65 78 70 65 63 74 65 64 20 72 65 74 75 72 6e 0a  expected return.
1ffa0 20 20 20 20 20 20 2a 2a 20 74 79 70 65 20 6f 66        ** type of
1ffa1 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 0a 20   the function . 
1ffa2 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65       */.      re
1ffa3 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a  turn WRC_Prune;.
1ffa4 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
1ffa5 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
1ffa6 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53  RY.    case TK_S
1ffa7 45 4c 45 43 54 3a 0a 20 20 20 20 63 61 73 65 20  ELECT:.    case 
1ffa8 54 4b 5f 45 58 49 53 54 53 3a 20 20 74 65 73 74  TK_EXISTS:  test
1ffa9 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d  case( pExpr->op=
1ffaa 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a 23 65  =TK_EXISTS );.#e
1ffab 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54 4b  ndif.    case TK
1ffac 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 74 65 73  _IN: {.      tes
1ffad 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70  tcase( pExpr->op
1ffae 3d 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20 20 20 20  ==TK_IN );.     
1ffaf 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
1ffb0 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78  erty(pExpr, EP_x
1ffb1 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20  IsSelect) ){.   
1ffb2 20 20 20 20 20 69 6e 74 20 6e 52 65 66 20 3d 20       int nRef = 
1ffb3 70 4e 43 2d 3e 6e 52 65 66 3b 0a 23 69 66 6e 64  pNC->nRef;.#ifnd
1ffb4 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
1ffb5 48 45 43 4b 0a 20 20 20 20 20 20 20 20 69 66 28  HECK.        if(
1ffb6 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29 7b   pNC->isCheck ){
1ffb7 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1ffb8 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1ffb9 65 2c 22 73 75 62 71 75 65 72 69 65 73 20 70 72  e,"subqueries pr
1ffba 6f 68 69 62 69 74 65 64 20 69 6e 20 43 48 45 43  ohibited in CHEC
1ffbb 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b  K constraints");
1ffbc 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  .        }.#endi
1ffbd 66 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  f.        sqlite
1ffbe 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c  3WalkSelect(pWal
1ffbf 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53  ker, pExpr->x.pS
1ffc0 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20  elect);.        
1ffc1 61 73 73 65 72 74 28 20 70 4e 43 2d 3e 6e 52 65  assert( pNC->nRe
1ffc2 66 3e 3d 6e 52 65 66 20 29 3b 0a 20 20 20 20 20  f>=nRef );.     
1ffc3 20 20 20 69 66 28 20 6e 52 65 66 21 3d 70 4e 43     if( nRef!=pNC
1ffc4 2d 3e 6e 52 65 66 20 29 7b 0a 20 20 20 20 20 20  ->nRef ){.      
1ffc5 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65      ExprSetPrope
1ffc6 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56 61  rty(pExpr, EP_Va
1ffc7 72 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20  rSelect);.      
1ffc8 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1ffc9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
1ffca 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1ffcb 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 63 61 73  IT_CHECK.    cas
1ffcc 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b  e TK_VARIABLE: {
1ffcd 0a 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d 3e  .      if( pNC->
1ffce 69 73 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 20  isCheck ){.     
1ffcf 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1ffd0 73 67 28 70 50 61 72 73 65 2c 22 70 61 72 61 6d  sg(pParse,"param
1ffd1 65 74 65 72 73 20 70 72 6f 68 69 62 69 74 65 64  eters prohibited
1ffd2 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74 72   in CHECK constr
1ffd3 61 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 7d  aints");.      }
1ffd4 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1ffd5 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20    }.#endif.  }. 
1ffd6 20 72 65 74 75 72 6e 20 28 70 50 61 72 73 65 2d   return (pParse-
1ffd7 3e 6e 45 72 72 20 7c 7c 20 70 50 61 72 73 65 2d  >nErr || pParse-
1ffd8 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
1ffd9 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a  d) ? WRC_Abort :
1ffda 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d   WRC_Continue;.}
1ffdb 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 69  ../*.** pEList i
1ffdc 73 20 61 20 6c 69 73 74 20 6f 66 20 65 78 70 72  s a list of expr
1ffdd 65 73 73 69 6f 6e 73 20 77 68 69 63 68 20 61 72  essions which ar
1ffde 65 20 72 65 61 6c 6c 79 20 74 68 65 20 72 65 73  e really the res
1ffdf 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 0a 2a  ult set of the.*
1ffe0 2a 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65  * a SELECT state
1ffe1 6d 65 6e 74 2e 20 20 70 45 20 69 73 20 61 20 74  ment.  pE is a t
1ffe2 65 72 6d 20 69 6e 20 61 6e 20 4f 52 44 45 52 20  erm in an ORDER 
1ffe3 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63  BY or GROUP BY c
1ffe4 6c 61 75 73 65 2e 0a 2a 2a 20 54 68 69 73 20 72  lause..** This r
1ffe5 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 6f  outine checks to
1ffe6 20 73 65 65 20 69 66 20 70 45 20 69 73 20 61 20   see if pE is a 
1ffe7 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 65  simple identifie
1ffe8 72 20 77 68 69 63 68 20 63 6f 72 72 65 73 70 6f  r which correspo
1ffe9 6e 64 73 0a 2a 2a 20 74 6f 20 74 68 65 20 41 53  nds.** to the AS
1ffea 2d 6e 61 6d 65 20 6f 66 20 6f 6e 65 20 6f 66 20  -name of one of 
1ffeb 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65  the terms of the
1ffec 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
1ffed 2e 20 20 49 66 20 69 74 20 69 73 2c 0a 2a 2a 20  .  If it is,.** 
1ffee 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  this routine ret
1ffef 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 62  urn an integer b
1fff0 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 77  etween 1 and N w
1fff1 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75  here N is the nu
1fff2 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6c 65 6d 65  mber of.** eleme
1fff3 6e 74 73 20 69 6e 20 70 45 4c 69 73 74 2c 20 63  nts in pEList, c
1fff4 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
1fff5 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e 74  the matching ent
1fff6 72 79 2e 20 20 49 66 20 74 68 65 72 65 20 69 73  ry.  If there is
1fff7 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 2c 20 6f 72  .** no match, or
1fff8 20 69 66 20 70 45 20 69 73 20 6e 6f 74 20 61 20   if pE is not a 
1fff9 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69 65  simple identifie
1fffa 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  r, then this rou
1fffb 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 20 30  tine.** return 0
1fffc 2e 0a 2a 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 68  ..**.** pEList h
1fffd 61 73 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64  as been resolved
1fffe 2e 20 20 70 45 20 68 61 73 20 6e 6f 74 2e 0a 2a  .  pE has not..*
1ffff 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73  /.static int res
20000 6f 6c 76 65 41 73 4e 61 6d 65 28 0a 20 20 50 61  olveAsName(.  Pa
20001 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
20002 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
20003 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65  ext for error me
20004 73 73 61 67 65 73 20 2a 2f 0a 20 20 45 78 70 72  ssages */.  Expr
20005 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 2f  List *pEList,  /
20006 2a 20 4c 69 73 74 20 6f 66 20 65 78 70 72 65 73  * List of expres
20007 73 69 6f 6e 73 20 74 6f 20 73 63 61 6e 20 2a 2f  sions to scan */
20008 0a 20 20 45 78 70 72 20 2a 70 45 20 20 20 20 20  .  Expr *pE     
20009 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73        /* Express
2000a 69 6f 6e 20 77 65 20 61 72 65 20 74 72 79 69 6e  ion we are tryin
2000b 67 20 74 6f 20 6d 61 74 63 68 20 2a 2f 0a 29 7b  g to match */.){
2000c 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
2000d 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
2000e 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 55 4e 55 53  unter */..  UNUS
2000f 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 50 61  ED_PARAMETER(pPa
20010 72 73 65 29 3b 0a 0a 20 20 69 66 28 20 70 45 2d  rse);..  if( pE-
20011 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20 20  >op==TK_ID ){.  
20012 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 70    char *zCol = p
20013 45 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20  E->u.zToken;.   
20014 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69   for(i=0; i<pELi
20015 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
20016 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 73  .      char *zAs
20017 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e   = pEList->a[i].
20018 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 69 66 28  zName;.      if(
20019 20 7a 41 73 21 3d 30 20 26 26 20 73 71 6c 69 74   zAs!=0 && sqlit
2001a 65 33 53 74 72 49 43 6d 70 28 7a 41 73 2c 20 7a  e3StrICmp(zAs, z
2001b 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Col)==0 ){.     
2001c 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20     return i+1;. 
2001d 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
2001e 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
2001f 2f 2a 0a 2a 2a 20 70 45 20 69 73 20 61 20 70 6f  /*.** pE is a po
20020 69 6e 74 65 72 20 74 6f 20 61 6e 20 65 78 70 72  inter to an expr
20021 65 73 73 69 6f 6e 20 77 68 69 63 68 20 69 73 20  ession which is 
20022 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e  a single term in
20023 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59   the.** ORDER BY
20024 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53   of a compound S
20025 45 4c 45 43 54 2e 20 20 54 68 65 20 65 78 70 72  ELECT.  The expr
20026 65 73 73 69 6f 6e 20 68 61 73 20 6e 6f 74 20 62  ession has not b
20027 65 65 6e 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f  een.** name reso
20028 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74  lved..**.** At t
20029 68 65 20 70 6f 69 6e 74 20 74 68 69 73 20 72 6f  he point this ro
2002a 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
2002b 20 77 65 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77   we already know
2002c 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 4f 52 44   that the.** ORD
2002d 45 52 20 42 59 20 74 65 72 6d 20 69 73 20 6e 6f  ER BY term is no
2002e 74 20 61 6e 20 69 6e 74 65 67 65 72 20 69 6e 64  t an integer ind
2002f 65 78 20 69 6e 74 6f 20 74 68 65 20 72 65 73 75  ex into the resu
20030 6c 74 20 73 65 74 2e 20 20 54 68 61 74 0a 2a 2a  lt set.  That.**
20031 20 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65 64   case is handled
20032 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20   by the calling 
20033 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 41  routine..**.** A
20034 74 74 65 6d 70 74 20 74 6f 20 6d 61 74 63 68 20  ttempt to match 
20035 70 45 20 61 67 61 69 6e 73 74 20 72 65 73 75 6c  pE against resul
20036 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 69 6e  t set columns in
20037 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 0a 2a   the left-most.*
20038 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  * SELECT stateme
20039 6e 74 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  nt.  Return the 
2003a 69 6e 64 65 78 20 69 20 6f 66 20 74 68 65 20 6d  index i of the m
2003b 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 2c 0a  atching column,.
2003c 2a 2a 20 61 73 20 61 6e 20 69 6e 64 69 63 61 74  ** as an indicat
2003d 69 6f 6e 20 74 6f 20 74 68 65 20 63 61 6c 6c 65  ion to the calle
2003e 72 20 74 68 61 74 20 69 74 20 73 68 6f 75 6c 64  r that it should
2003f 20 73 6f 72 74 20 62 79 20 74 68 65 20 69 2d 74   sort by the i-t
20040 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54 68 65  h column..** The
20041 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d   left-most colum
20042 6e 20 69 73 20 31 2e 20 20 49 6e 20 6f 74 68 65  n is 1.  In othe
20043 72 20 77 6f 72 64 73 2c 20 74 68 65 20 76 61 6c  r words, the val
20044 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74  ue returned is t
20045 68 65 0a 2a 2a 20 73 61 6d 65 20 69 6e 74 65 67  he.** same integ
20046 65 72 20 76 61 6c 75 65 20 74 68 61 74 20 77 6f  er value that wo
20047 75 6c 64 20 62 65 20 75 73 65 64 20 69 6e 20 74  uld be used in t
20048 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
20049 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20   to indicate.** 
2004a 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a  the column..**.*
2004b 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  * If there is no
2004c 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e 20 30   match, return 0
2004d 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 69 66 20  .  Return -1 if 
2004e 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e  an error occurs.
2004f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
20050 65 73 6f 6c 76 65 4f 72 64 65 72 42 79 54 65 72  esolveOrderByTer
20051 6d 54 6f 45 78 70 72 4c 69 73 74 28 0a 20 20 50  mToExprList(.  P
20052 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
20053 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
20054 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d  text for error m
20055 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 53 65 6c  essages */.  Sel
20056 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20  ect *pSelect,   
20057 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74  /* The SELECT st
20058 61 74 65 6d 65 6e 74 20 77 69 74 68 20 74 68 65  atement with the
20059 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2005a 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 20 20   */.  Expr *pE  
2005b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2005c 73 70 65 63 69 66 69 63 20 4f 52 44 45 52 20 42  specific ORDER B
2005d 59 20 74 65 72 6d 20 2a 2f 0a 29 7b 0a 20 20 69  Y term */.){.  i
2005e 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
2005f 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
20060 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  r */.  ExprList 
20061 2a 70 45 4c 69 73 74 3b 20 20 2f 2a 20 54 68 65  *pEList;  /* The
20062 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20   columns of the 
20063 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20  result set */.  
20064 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6e 63 3b 20  NameContext nc; 
20065 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65     /* Name conte
20066 78 74 20 66 6f 72 20 72 65 73 6f 6c 76 69 6e 67  xt for resolving
20067 20 70 45 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74   pE */..  assert
20068 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49  ( sqlite3ExprIsI
20069 6e 74 65 67 65 72 28 70 45 2c 20 26 69 29 3d 3d  nteger(pE, &i)==
2006a 30 20 29 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20  0 );.  pEList = 
2006b 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b  pSelect->pEList;
2006c 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 61  ..  /* Resolve a
2006d 6c 6c 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20  ll names in the 
2006e 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 65 78  ORDER BY term ex
2006f 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2f 0a 20 20  pression.  */.  
20070 6d 65 6d 73 65 74 28 26 6e 63 2c 20 30 2c 20 73  memset(&nc, 0, s
20071 69 7a 65 6f 66 28 6e 63 29 29 3b 0a 20 20 6e 63  izeof(nc));.  nc
20072 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
20073 3b 0a 20 20 6e 63 2e 70 53 72 63 4c 69 73 74 20  ;.  nc.pSrcList 
20074 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b  = pSelect->pSrc;
20075 0a 20 20 6e 63 2e 70 45 4c 69 73 74 20 3d 20 70  .  nc.pEList = p
20076 45 4c 69 73 74 3b 0a 20 20 6e 63 2e 61 6c 6c 6f  EList;.  nc.allo
20077 77 41 67 67 20 3d 20 31 3b 0a 20 20 6e 63 2e 6e  wAgg = 1;.  nc.n
20078 45 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 73  Err = 0;.  if( s
20079 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
2007a 72 4e 61 6d 65 73 28 26 6e 63 2c 20 70 45 29 20  rNames(&nc, pE) 
2007b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2007c 72 6f 72 43 6c 65 61 72 28 70 50 61 72 73 65 29  rorClear(pParse)
2007d 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
2007e 20 20 7d 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f    }..  /* Try to
2007f 20 6d 61 74 63 68 20 74 68 65 20 4f 52 44 45 52   match the ORDER
20080 20 42 59 20 65 78 70 72 65 73 73 69 6f 6e 20 61   BY expression a
20081 67 61 69 6e 73 74 20 61 6e 20 65 78 70 72 65 73  gainst an expres
20082 73 69 6f 6e 0a 20 20 2a 2a 20 69 6e 20 74 68 65  sion.  ** in the
20083 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 52 65   result set.  Re
20084 74 75 72 6e 20 61 6e 20 31 2d 62 61 73 65 64 20  turn an 1-based 
20085 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 74  index of the mat
20086 63 68 69 6e 67 0a 20 20 2a 2a 20 72 65 73 75 6c  ching.  ** resul
20087 74 2d 73 65 74 20 65 6e 74 72 79 2e 0a 20 20 2a  t-set entry..  *
20088 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  /.  for(i=0; i<p
20089 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b  EList->nExpr; i+
2008a 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  +){.    if( sqli
2008b 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70  te3ExprCompare(p
2008c 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70  EList->a[i].pExp
2008d 72 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 20 20  r, pE) ){.      
2008e 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20  return i+1;.    
2008f 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e  }.  }..  /* If n
20090 6f 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e 20  o match, return 
20091 30 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30  0. */.  return 0
20092 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
20093 61 74 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20  ate an ORDER BY 
20094 6f 72 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d  or GROUP BY term
20095 20 6f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 65 72   out-of-range er
20096 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ror..*/.static v
20097 6f 69 64 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66  oid resolveOutOf
20098 52 61 6e 67 65 45 72 72 6f 72 28 0a 20 20 50 61  RangeError(.  Pa
20099 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2009a 20 20 20 20 20 2f 2a 20 54 68 65 20 65 72 72 6f       /* The erro
2009b 72 20 63 6f 6e 74 65 78 74 20 69 6e 74 6f 20 77  r context into w
2009c 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 74 68  hich to write th
2009d 65 20 65 72 72 6f 72 20 2a 2f 0a 20 20 63 6f 6e  e error */.  con
2009e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c 20  st char *zType, 
2009f 20 20 20 20 2f 2a 20 22 4f 52 44 45 52 22 20 6f      /* "ORDER" o
200a0 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a 20 20 69  r "GROUP" */.  i
200a1 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 20 20  nt i,           
200a2 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64        /* The ind
200a3 65 78 20 28 31 2d 62 61 73 65 64 29 20 6f 66 20  ex (1-based) of 
200a4 74 68 65 20 74 65 72 6d 20 6f 75 74 20 6f 66 20  the term out of 
200a5 72 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6d  range */.  int m
200a6 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x               
200a7 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 70 65 72    /* Largest per
200a8 6d 69 73 73 69 62 6c 65 20 76 61 6c 75 65 20 6f  missible value o
200a9 66 20 69 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  f i */.){.  sqli
200aa 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
200ab 73 65 2c 20 0a 20 20 20 20 22 25 72 20 25 73 20  se, .    "%r %s 
200ac 42 59 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 72  BY term out of r
200ad 61 6e 67 65 20 2d 20 73 68 6f 75 6c 64 20 62 65  ange - should be
200ae 20 22 0a 20 20 20 20 22 62 65 74 77 65 65 6e 20   ".    "between 
200af 31 20 61 6e 64 20 25 64 22 2c 20 69 2c 20 7a 54  1 and %d", i, zT
200b0 79 70 65 2c 20 6d 78 29 3b 0a 7d 0a 0a 2f 2a 0a  ype, mx);.}../*.
200b1 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 4f  ** Analyze the O
200b2 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69  RDER BY clause i
200b3 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c  n a compound SEL
200b4 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ECT statement.  
200b5 20 4d 6f 64 69 66 79 0a 2a 2a 20 65 61 63 68 20   Modify.** each 
200b6 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 45  term of the ORDE
200b7 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 61  R BY clause is a
200b8 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65   constant intege
200b9 72 20 62 65 74 77 65 65 6e 20 31 0a 2a 2a 20 61  r between 1.** a
200ba 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69 73 20  nd N where N is 
200bb 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
200bc 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 63 6f 6d  lumns in the com
200bd 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e 0a 2a 2a  pound SELECT..**
200be 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65 72  .** ORDER BY ter
200bf 6d 73 20 74 68 61 74 20 61 72 65 20 61 6c 72 65  ms that are alre
200c0 61 64 79 20 61 6e 20 69 6e 74 65 67 65 72 20 62  ady an integer b
200c1 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20 61  etween 1 and N a
200c2 72 65 0a 2a 2a 20 75 6e 6d 6f 64 69 66 69 65 64  re.** unmodified
200c3 2e 20 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  .  ORDER BY term
200c4 73 20 74 68 61 74 20 61 72 65 20 69 6e 74 65 67  s that are integ
200c5 65 72 73 20 6f 75 74 73 69 64 65 20 74 68 65 20  ers outside the 
200c6 72 61 6e 67 65 20 6f 66 0a 2a 2a 20 31 20 74 68  range of.** 1 th
200c7 72 6f 75 67 68 20 4e 20 67 65 6e 65 72 61 74 65  rough N generate
200c8 20 61 6e 20 65 72 72 6f 72 2e 20 20 4f 52 44 45   an error.  ORDE
200c9 52 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20  R BY terms that 
200ca 61 72 65 20 65 78 70 72 65 73 73 69 6f 6e 73 0a  are expressions.
200cb 2a 2a 20 61 72 65 20 6d 61 74 63 68 65 64 20 61  ** are matched a
200cc 67 61 69 6e 73 74 20 72 65 73 75 6c 74 20 73 65  gainst result se
200cd 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66  t expressions of
200ce 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54   compound SELECT
200cf 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 77 69  .** beginning wi
200d0 74 68 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74  th the left-most
200d1 20 53 45 4c 45 43 54 20 61 6e 64 20 77 6f 72 6b   SELECT and work
200d2 69 6e 67 20 74 6f 77 61 72 64 20 74 68 65 20 72  ing toward the r
200d3 69 67 68 74 2e 0a 2a 2a 20 41 74 20 74 68 65 20  ight..** At the 
200d4 66 69 72 73 74 20 6d 61 74 63 68 2c 20 74 68 65  first match, the
200d5 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 65 73   ORDER BY expres
200d6 73 69 6f 6e 20 69 73 20 74 72 61 6e 73 66 6f 72  sion is transfor
200d7 6d 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20  med into.** the 
200d8 69 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e  integer column n
200d9 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  umber..**.** Ret
200da 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
200db 66 20 65 72 72 6f 72 73 20 73 65 65 6e 2e 0a 2a  f errors seen..*
200dc 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73  /.static int res
200dd 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 4f 72 64 65  olveCompoundOrde
200de 72 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50  rBy(.  Parse *pP
200df 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
200e0 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e  Parsing context.
200e1 20 20 4c 65 61 76 65 20 65 72 72 6f 72 20 6d 65    Leave error me
200e2 73 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 20  ssages here */. 
200e3 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
200e4 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45         /* The SE
200e5 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 63  LECT statement c
200e6 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 4f 52  ontaining the OR
200e7 44 45 52 20 42 59 20 2a 2f 0a 29 7b 0a 20 20 69  DER BY */.){.  i
200e8 6e 74 20 69 3b 0a 20 20 45 78 70 72 4c 69 73 74  nt i;.  ExprList
200e9 20 2a 70 4f 72 64 65 72 42 79 3b 0a 20 20 45 78   *pOrderBy;.  Ex
200ea 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a  prList *pEList;.
200eb 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20    sqlite3 *db;. 
200ec 20 69 6e 74 20 6d 6f 72 65 54 6f 44 6f 20 3d 20   int moreToDo = 
200ed 31 3b 0a 0a 20 20 70 4f 72 64 65 72 42 79 20 3d  1;..  pOrderBy =
200ee 20 70 53 65 6c 65 63 74 2d 3e 70 4f 72 64 65 72   pSelect->pOrder
200ef 42 79 3b 0a 20 20 69 66 28 20 70 4f 72 64 65 72  By;.  if( pOrder
200f0 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  By==0 ) return 0
200f1 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ;.  db = pParse-
200f2 3e 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f  >db;.#if SQLITE_
200f3 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28  MAX_COLUMN.  if(
200f4 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72   pOrderBy->nExpr
200f5 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
200f6 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d  TE_LIMIT_COLUMN]
200f7 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
200f8 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
200f9 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20  "too many terms 
200fa 69 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  in ORDER BY clau
200fb 73 65 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  se");.    return
200fc 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20   1;.  }.#endif. 
200fd 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64   for(i=0; i<pOrd
200fe 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  erBy->nExpr; i++
200ff 29 7b 0a 20 20 20 20 70 4f 72 64 65 72 42 79 2d  ){.    pOrderBy-
20100 3e 61 5b 69 5d 2e 64 6f 6e 65 20 3d 20 30 3b 0a  >a[i].done = 0;.
20101 20 20 7d 0a 20 20 70 53 65 6c 65 63 74 2d 3e 70    }.  pSelect->p
20102 4e 65 78 74 20 3d 20 30 3b 0a 20 20 77 68 69 6c  Next = 0;.  whil
20103 65 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69  e( pSelect->pPri
20104 6f 72 20 29 7b 0a 20 20 20 20 70 53 65 6c 65 63  or ){.    pSelec
20105 74 2d 3e 70 50 72 69 6f 72 2d 3e 70 4e 65 78 74  t->pPrior->pNext
20106 20 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20 20 20   = pSelect;.    
20107 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63  pSelect = pSelec
20108 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20  t->pPrior;.  }. 
20109 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 20   while( pSelect 
2010a 26 26 20 6d 6f 72 65 54 6f 44 6f 20 29 7b 0a 20  && moreToDo ){. 
2010b 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
2010c 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
2010d 20 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 30      moreToDo = 0
2010e 3b 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70  ;.    pEList = p
2010f 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a  Select->pEList;.
20110 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69      assert( pELi
20111 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72  st!=0 );.    for
20112 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64  (i=0, pItem=pOrd
20113 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65  erBy->a; i<pOrde
20114 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c  rBy->nExpr; i++,
20115 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
20116 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a   int iCol = -1;.
20117 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 2c 20        Expr *pE, 
20118 2a 70 44 75 70 3b 0a 20 20 20 20 20 20 69 66 28  *pDup;.      if(
20119 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 29 20 63   pItem->done ) c
2011a 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 70  ontinue;.      p
2011b 45 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  E = pItem->pExpr
2011c 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ;.      if( sqli
2011d 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72  te3ExprIsInteger
2011e 28 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0a 20  (pE, &iCol) ){. 
2011f 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c         if( iCol<
20120 3d 30 20 7c 7c 20 69 43 6f 6c 3e 70 45 4c 69 73  =0 || iCol>pELis
20121 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20  t->nExpr ){.    
20122 20 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 74        resolveOut
20123 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50 61  OfRangeError(pPa
20124 72 73 65 2c 20 22 4f 52 44 45 52 22 2c 20 69 2b  rse, "ORDER", i+
20125 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  1, pEList->nExpr
20126 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  );.          ret
20127 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  urn 1;.        }
20128 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
20129 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73        iCol = res
2012a 6f 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72 73  olveAsName(pPars
2012b 65 2c 20 70 45 4c 69 73 74 2c 20 70 45 29 3b 0a  e, pEList, pE);.
2012c 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c          if( iCol
2012d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
2012e 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45   pDup = sqlite3E
2012f 78 70 72 44 75 70 28 64 62 2c 20 70 45 2c 20 30  xprDup(db, pE, 0
20130 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
20131 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
20132 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ed ){.          
20133 20 20 61 73 73 65 72 74 28 70 44 75 70 29 3b 0a    assert(pDup);.
20134 20 20 20 20 20 20 20 20 20 20 20 20 69 43 6f 6c              iCol
20135 20 3d 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 42   = resolveOrderB
20136 79 54 65 72 6d 54 6f 45 78 70 72 4c 69 73 74 28  yTermToExprList(
20137 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c  pParse, pSelect,
20138 20 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20   pDup);.        
20139 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 71    }.          sq
2013a 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
2013b 64 62 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20  db, pDup);.     
2013c 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
2013d 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 29 7b     if( iCol>0 ){
2013e 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71  .        CollSeq
2013f 20 2a 70 43 6f 6c 6c 20 3d 20 70 45 2d 3e 70 43   *pColl = pE->pC
20140 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  oll;.        int
20141 20 66 6c 61 67 73 20 3d 20 70 45 2d 3e 66 6c 61   flags = pE->fla
20142 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61  gs & EP_ExpColla
20143 74 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  te;.        sqli
20144 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
20145 2c 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 70  , pE);.        p
20146 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45  Item->pExpr = pE
20147 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64   = sqlite3Expr(d
20148 62 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30  b, TK_INTEGER, 0
20149 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
2014a 45 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b  E==0 ) return 1;
2014b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 70 43 6f  .        pE->pCo
2014c 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20  ll = pColl;.    
2014d 20 20 20 20 70 45 2d 3e 66 6c 61 67 73 20 7c 3d      pE->flags |=
2014e 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 7c 20 66   EP_IntValue | f
2014f 6c 61 67 73 3b 0a 20 20 20 20 20 20 20 20 70 45  lags;.        pE
20150 2d 3e 75 2e 69 56 61 6c 75 65 20 3d 20 69 43 6f  ->u.iValue = iCo
20151 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d  l;.        pItem
20152 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 43  ->iCol = (u16)iC
20153 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65  ol;.        pIte
20154 6d 2d 3e 64 6f 6e 65 20 3d 20 31 3b 0a 20 20 20  m->done = 1;.   
20155 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
20156 20 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 31 3b 0a    moreToDo = 1;.
20157 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
20158 20 20 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c    pSelect = pSel
20159 65 63 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a  ect->pNext;.  }.
2015a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72    for(i=0; i<pOr
2015b 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b  derBy->nExpr; i+
2015c 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 4f 72 64  +){.    if( pOrd
2015d 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 3d  erBy->a[i].done=
2015e 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
2015f 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
20160 73 65 2c 20 22 25 72 20 4f 52 44 45 52 20 42 59  se, "%r ORDER BY
20161 20 74 65 72 6d 20 64 6f 65 73 20 6e 6f 74 20 6d   term does not m
20162 61 74 63 68 20 61 6e 79 20 22 0a 20 20 20 20 20  atch any ".     
20163 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 20 69         "column i
20164 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
20165 22 2c 20 69 2b 31 29 3b 0a 20 20 20 20 20 20 72  ", i+1);.      r
20166 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
20167 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
20168 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 65 76  ../*.** Check ev
20169 65 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65 20  ery term in the 
2016a 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55  ORDER BY or GROU
2016b 50 20 42 59 20 63 6c 61 75 73 65 20 70 4f 72 64  P BY clause pOrd
2016c 65 72 42 79 20 6f 66 0a 2a 2a 20 74 68 65 20 53  erBy of.** the S
2016d 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
2016e 70 53 65 6c 65 63 74 2e 20 20 49 66 20 61 6e 79  pSelect.  If any
2016f 20 74 65 72 6d 20 69 73 20 72 65 66 65 72 65 6e   term is referen
20170 63 65 20 74 6f 20 61 0a 2a 2a 20 72 65 73 75 6c  ce to a.** resul
20171 74 20 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e  t set expression
20172 20 28 61 73 20 64 65 74 65 72 6d 69 6e 65 64 20   (as determined 
20173 62 79 20 74 68 65 20 45 78 70 72 4c 69 73 74 2e  by the ExprList.
20174 61 2e 69 43 6f 6c 20 66 69 65 6c 64 29 0a 2a 2a  a.iCol field).**
20175 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 68   then convert th
20176 61 74 20 74 65 72 6d 20 69 6e 74 6f 20 61 20 63  at term into a c
20177 6f 70 79 20 6f 66 20 74 68 65 20 63 6f 72 72 65  opy of the corre
20178 73 70 6f 6e 64 69 6e 67 20 72 65 73 75 6c 74 20  sponding result 
20179 73 65 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a  set.** column..*
2017a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 65 72 72 6f  *.** If any erro
2017b 72 73 20 61 72 65 20 64 65 74 65 63 74 65 64 2c  rs are detected,
2017c 20 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65   add an error me
2017d 73 73 61 67 65 20 74 6f 20 70 50 61 72 73 65 20  ssage to pParse 
2017e 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f  and.** return no
2017f 6e 2d 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e 20  n-zero.  Return 
20180 7a 65 72 6f 20 69 66 20 6e 6f 20 65 72 72 6f 72  zero if no error
20181 73 20 61 72 65 20 73 65 65 6e 2e 0a 2a 2f 0a 53  s are seen..*/.S
20182 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
20183 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  t sqlite3Resolve
20184 4f 72 64 65 72 47 72 6f 75 70 42 79 28 0a 20 20  OrderGroupBy(.  
20185 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
20186 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
20187 20 63 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76 65   context.  Leave
20188 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   error messages 
20189 68 65 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74  here */.  Select
2018a 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20   *pSelect,      
2018b 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74  /* The SELECT st
2018c 61 74 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69  atement containi
2018d 6e 67 20 74 68 65 20 63 6c 61 75 73 65 20 2a 2f  ng the clause */
2018e 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
2018f 64 65 72 42 79 2c 20 20 20 2f 2a 20 54 68 65 20  derBy,   /* The 
20190 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55  ORDER BY or GROU
20191 50 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 62  P BY clause to b
20192 65 20 70 72 6f 63 65 73 73 65 64 20 2a 2f 0a 20  e processed */. 
20193 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79   const char *zTy
20194 70 65 20 20 20 20 20 2f 2a 20 22 4f 52 44 45 52  pe     /* "ORDER
20195 22 20 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a  " or "GROUP" */.
20196 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 71  ){.  int i;.  sq
20197 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
20198 73 65 2d 3e 64 62 3b 0a 20 20 45 78 70 72 4c 69  se->db;.  ExprLi
20199 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73 74  st *pEList;.  st
2019a 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
2019b 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66  em *pItem;..  if
2019c 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 7c 7c  ( pOrderBy==0 ||
2019d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c   pParse->db->mal
2019e 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75  locFailed ) retu
2019f 72 6e 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 45  rn 0;.#if SQLITE
201a0 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66  _MAX_COLUMN.  if
201a1 28 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  ( pOrderBy->nExp
201a2 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  r>db->aLimit[SQL
201a3 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e  ITE_LIMIT_COLUMN
201a4 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ] ){.    sqlite3
201a5 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
201a6 20 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73   "too many terms
201a7 20 69 6e 20 25 73 20 42 59 20 63 6c 61 75 73 65   in %s BY clause
201a8 22 2c 20 7a 54 79 70 65 29 3b 0a 20 20 20 20 72  ", zType);.    r
201a9 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e  eturn 1;.  }.#en
201aa 64 69 66 0a 20 20 70 45 4c 69 73 74 20 3d 20 70  dif.  pEList = p
201ab 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a  Select->pEList;.
201ac 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74    assert( pEList
201ad 21 3d 30 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74  !=0 );  /* sqlit
201ae 65 33 53 65 6c 65 63 74 4e 65 77 28 29 20 67 75  e3SelectNew() gu
201af 61 72 61 6e 74 65 65 73 20 74 68 69 73 20 2a 2f  arantees this */
201b0 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65  .  for(i=0, pIte
201b1 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69  m=pOrderBy->a; i
201b2 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72  <pOrderBy->nExpr
201b3 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
201b4 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e  .    if( pItem->
201b5 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69 66  iCol ){.      if
201b6 28 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 3e 70 45  ( pItem->iCol>pE
201b7 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20  List->nExpr ){. 
201b8 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75         resolveOu
201b9 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50  tOfRangeError(pP
201ba 61 72 73 65 2c 20 7a 54 79 70 65 2c 20 69 2b 31  arse, zType, i+1
201bb 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29  , pEList->nExpr)
201bc 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
201bd 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
201be 20 20 72 65 73 6f 6c 76 65 41 6c 69 61 73 28 70    resolveAlias(p
201bf 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70  Parse, pEList, p
201c0 49 74 65 6d 2d 3e 69 43 6f 6c 2d 31 2c 20 70 49  Item->iCol-1, pI
201c1 74 65 6d 2d 3e 70 45 78 70 72 2c 20 7a 54 79 70  tem->pExpr, zTyp
201c2 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
201c3 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
201c4 2a 2a 20 70 4f 72 64 65 72 42 79 20 69 73 20 61  ** pOrderBy is a
201c5 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52  n ORDER BY or GR
201c6 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 6e  OUP BY clause in
201c7 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
201c8 74 20 70 53 65 6c 65 63 74 2e 0a 2a 2a 20 54 68  t pSelect..** Th
201c9 65 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f  e Name context o
201ca 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  f the SELECT sta
201cb 74 65 6d 65 6e 74 20 69 73 20 70 4e 43 2e 20 20  tement is pNC.  
201cc 7a 54 79 70 65 20 69 73 20 65 69 74 68 65 72 0a  zType is either.
201cd 2a 2a 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47  ** "ORDER" or "G
201ce 52 4f 55 50 22 20 64 65 70 65 6e 64 69 6e 67 20  ROUP" depending 
201cf 6f 6e 20 77 68 69 63 68 20 74 79 70 65 20 6f 66  on which type of
201d0 20 63 6c 61 75 73 65 20 70 4f 72 64 65 72 42 79   clause pOrderBy
201d1 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20   is..**.** This 
201d2 72 6f 75 74 69 6e 65 20 72 65 73 6f 6c 76 65 73  routine resolves
201d3 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68   each term of th
201d4 65 20 63 6c 61 75 73 65 20 69 6e 74 6f 20 61 6e  e clause into an
201d5 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20   expression..** 
201d6 49 66 20 74 68 65 20 6f 72 64 65 72 2d 62 79 20  If the order-by 
201d7 74 65 72 6d 20 69 73 20 61 6e 20 69 6e 74 65 67  term is an integ
201d8 65 72 20 49 20 62 65 74 77 65 65 6e 20 31 20 61  er I between 1 a
201d9 6e 64 20 4e 20 28 77 68 65 72 65 20 4e 20 69 73  nd N (where N is
201da 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f   the.** number o
201db 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
201dc 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74   result set of t
201dd 68 65 20 53 45 4c 45 43 54 29 20 74 68 65 6e 20  he SELECT) then 
201de 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a  the expression.*
201df 2a 20 69 6e 20 74 68 65 20 72 65 73 6f 6c 75 74  * in the resolut
201e0 69 6f 6e 20 69 73 20 61 20 63 6f 70 79 20 6f 66  ion is a copy of
201e1 20 74 68 65 20 49 2d 74 68 20 72 65 73 75 6c 74   the I-th result
201e2 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e  -set expression.
201e3 20 20 49 66 0a 2a 2a 20 74 68 65 20 6f 72 64 65    If.** the orde
201e4 72 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e 20  r-by term is an 
201e5 69 64 65 6e 74 69 66 79 20 74 68 61 74 20 63 6f  identify that co
201e6 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65  rresponds to the
201e7 20 41 53 2d 6e 61 6d 65 20 6f 66 0a 2a 2a 20 61   AS-name of.** a
201e8 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72   result-set expr
201e9 65 73 73 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65  ession, then the
201ea 20 74 65 72 6d 20 72 65 73 6f 6c 76 65 73 20 74   term resolves t
201eb 6f 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a  o a copy of the.
201ec 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78  ** result-set ex
201ed 70 72 65 73 73 69 6f 6e 2e 20 20 4f 74 68 65 72  pression.  Other
201ee 77 69 73 65 2c 20 74 68 65 20 65 78 70 72 65 73  wise, the expres
201ef 73 69 6f 6e 20 69 73 20 72 65 73 6f 6c 76 65 64  sion is resolved
201f0 20 69 6e 0a 2a 2a 20 74 68 65 20 75 73 75 61 6c   in.** the usual
201f1 20 77 61 79 20 2d 20 75 73 69 6e 67 20 73 71 6c   way - using sql
201f2 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
201f3 61 6d 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  ames()..**.** Th
201f4 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  is routine retur
201f5 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
201f6 20 65 72 72 6f 72 73 2e 20 20 49 66 20 65 72 72   errors.  If err
201f7 6f 72 73 20 6f 63 63 75 72 2c 20 74 68 65 6e 0a  ors occur, then.
201f8 2a 2a 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74  ** an appropriat
201f9 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
201fa 6d 69 67 68 74 20 62 65 20 6c 65 66 74 20 69 6e  might be left in
201fb 20 70 50 61 72 73 65 2e 20 20 28 4f 4f 4d 20 65   pParse.  (OOM e
201fc 72 72 6f 72 73 0a 2a 2a 20 65 78 63 65 70 74 65  rrors.** excepte
201fd 64 2e 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  d.).*/.static in
201fe 74 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 47 72  t resolveOrderGr
201ff 6f 75 70 42 79 28 0a 20 20 4e 61 6d 65 43 6f 6e  oupBy(.  NameCon
20200 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 20 2f  text *pNC,     /
20201 2a 20 54 68 65 20 6e 61 6d 65 20 63 6f 6e 74 65  * The name conte
20202 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  xt of the SELECT
20203 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
20204 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c  Select *pSelect,
20205 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c        /* The SEL
20206 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 68 6f  ECT statement ho
20207 6c 64 69 6e 67 20 70 4f 72 64 65 72 42 79 20 2a  lding pOrderBy *
20208 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f  /.  ExprList *pO
20209 72 64 65 72 42 79 2c 20 20 20 2f 2a 20 41 6e 20  rderBy,   /* An 
2020a 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55  ORDER BY or GROU
2020b 50 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20 72  P BY clause to r
2020c 65 73 6f 6c 76 65 20 2a 2f 0a 20 20 63 6f 6e 73  esolve */.  cons
2020d 74 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20 20  t char *zType   
2020e 20 20 2f 2a 20 45 69 74 68 65 72 20 22 4f 52 44    /* Either "ORD
2020f 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22 2c 20  ER" or "GROUP", 
20210 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a  as appropriate *
20211 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20  /.){.  int i;   
20212 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20213 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
20214 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69  unter */.  int i
20215 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
20216 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
20217 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  umn number */.  
20218 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
20219 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20 20 2f  item *pItem;   /
2021a 2a 20 41 20 74 65 72 6d 20 6f 66 20 74 68 65 20  * A term of the 
2021b 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2021c 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  */.  Parse *pPar
2021d 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  se;             
2021e 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2021f 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
20220 6e 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20  nResult;        
20221 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
20222 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e  mber of terms in
20223 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
20224 2a 2f 0a 0a 20 20 69 66 28 20 70 4f 72 64 65 72  */..  if( pOrder
20225 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  By==0 ) return 0
20226 3b 0a 20 20 6e 52 65 73 75 6c 74 20 3d 20 70 53  ;.  nResult = pS
20227 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e  elect->pEList->n
20228 45 78 70 72 3b 0a 20 20 70 50 61 72 73 65 20 3d  Expr;.  pParse =
20229 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20   pNC->pParse;.  
2022a 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70  for(i=0, pItem=p
2022b 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f  OrderBy->a; i<pO
2022c 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69  rderBy->nExpr; i
2022d 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
2022e 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70 49 74    Expr *pE = pIt
2022f 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 69  em->pExpr;.    i
20230 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 41 73 4e  Col = resolveAsN
20231 61 6d 65 28 70 50 61 72 73 65 2c 20 70 53 65 6c  ame(pParse, pSel
20232 65 63 74 2d 3e 70 45 4c 69 73 74 2c 20 70 45 29  ect->pEList, pE)
20233 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 30  ;.    if( iCol>0
20234 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
20235 61 6e 20 41 53 2d 6e 61 6d 65 20 6d 61 74 63 68  an AS-name match
20236 20 69 73 20 66 6f 75 6e 64 2c 20 6d 61 72 6b 20   is found, mark 
20237 74 68 69 73 20 4f 52 44 45 52 20 42 59 20 63 6f  this ORDER BY co
20238 6c 75 6d 6e 20 61 73 20 62 65 69 6e 67 0a 20 20  lumn as being.  
20239 20 20 20 20 2a 2a 20 61 20 63 6f 70 79 20 6f 66      ** a copy of
2023a 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 73   the iCol-th res
2023b 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 2e 20  ult-set column. 
2023c 20 54 68 65 20 73 75 62 73 65 71 75 65 6e 74 20   The subsequent 
2023d 63 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 2a 2a  call to.      **
2023e 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f   sqlite3ResolveO
2023f 72 64 65 72 47 72 6f 75 70 42 79 28 29 20 77 69  rderGroupBy() wi
20240 6c 6c 20 63 6f 6e 76 65 72 74 20 74 68 65 20 65  ll convert the e
20241 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 61 0a 20  xpression to a. 
20242 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 66 20       ** copy of 
20243 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 73 75  the iCol-th resu
20244 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f  lt-set expressio
20245 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 74 65  n. */.      pIte
20246 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69  m->iCol = (u16)i
20247 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69  Col;.      conti
20248 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  nue;.    }.    i
20249 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73  f( sqlite3ExprIs
2024a 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43 6f  Integer(pE, &iCo
2024b 6c 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  l) ){.      /* T
2024c 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  he ORDER BY term
2024d 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63   is an integer c
2024e 6f 6e 73 74 61 6e 74 2e 20 20 41 67 61 69 6e 2c  onstant.  Again,
2024f 20 73 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e 0a   set the column.
20250 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20        ** number 
20251 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 52  so that sqlite3R
20252 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70  esolveOrderGroup
20253 42 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72  By() will conver
20254 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f  t the.      ** o
20255 72 64 65 72 2d 62 79 20 74 65 72 6d 20 74 6f 20  rder-by term to 
20256 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65  a copy of the re
20257 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73  sult-set express
20258 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ion */.      if(
20259 20 69 43 6f 6c 3c 31 20 29 7b 0a 20 20 20 20 20   iCol<1 ){.     
2025a 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52     resolveOutOfR
2025b 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72 73 65  angeError(pParse
2025c 2c 20 7a 54 79 70 65 2c 20 69 2b 31 2c 20 6e 52  , zType, i+1, nR
2025d 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 20 20  esult);.        
2025e 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
2025f 7d 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69  }.      pItem->i
20260 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f 6c 3b  Col = (u16)iCol;
20261 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b  .      continue;
20262 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f  .    }..    /* O
20263 74 68 65 72 77 69 73 65 2c 20 74 72 65 61 74 20  therwise, treat 
20264 74 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72  the ORDER BY ter
20265 6d 20 61 73 20 61 6e 20 6f 72 64 69 6e 61 72 79  m as an ordinary
20266 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
20267 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d     pItem->iCol =
20268 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   0;.    if( sqli
20269 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
2026a 6d 65 73 28 70 4e 43 2c 20 70 45 29 20 29 7b 0a  mes(pNC, pE) ){.
2026b 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
2026c 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
2026d 72 6e 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  rn sqlite3Resolv
2026e 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 70 50  eOrderGroupBy(pP
2026f 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 70  arse, pSelect, p
20270 4f 72 64 65 72 42 79 2c 20 7a 54 79 70 65 29 3b  OrderBy, zType);
20271 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76  .}../*.** Resolv
20272 65 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 53  e names in the S
20273 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
20274 70 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73  p and all of its
20275 20 64 65 73 63 65 6e 64 65 6e 74 73 2e 0a 2a 2f   descendents..*/
20276 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f  .static int reso
20277 6c 76 65 53 65 6c 65 63 74 53 74 65 70 28 57 61  lveSelectStep(Wa
20278 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53  lker *pWalker, S
20279 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 4e 61 6d  elect *p){.  Nam
2027a 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75 74 65 72  eContext *pOuter
2027b 4e 43 3b 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20  NC;  /* Context 
2027c 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
2027d 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 4e  is SELECT */.  N
2027e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20  ameContext sNC; 
2027f 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63         /* Name c
20280 6f 6e 74 65 78 74 20 6f 66 20 74 68 69 73 20 53  ontext of this S
20281 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 69  ELECT */.  int i
20282 73 43 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20  sCompound;      
20283 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 20     /* True if p 
20284 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65  is a compound se
20285 6c 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  lect */.  int nC
20286 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 20 20  ompound;        
20287 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
20288 6f 6d 70 6f 75 6e 64 20 74 65 72 6d 73 20 70 72  ompound terms pr
20289 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72 20 2a  ocessed so far *
2028a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  /.  Parse *pPars
2028b 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  e;          /* P
2028c 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2028d 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
2028e 4c 69 73 74 3b 20 20 20 20 20 20 20 2f 2a 20 52  List;       /* R
2028f 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 65 73  esult set expres
20290 73 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 69  sion list */.  i
20291 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
20292 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
20293 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 70 72  ounter */.  Expr
20294 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20  List *pGroupBy; 
20295 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 50      /* The GROUP
20296 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
20297 53 65 6c 65 63 74 20 2a 70 4c 65 66 74 6d 6f 73  Select *pLeftmos
20298 74 3b 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 2d  t;      /* Left-
20299 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 20 6f  most of SELECT o
2029a 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a  f a compound */.
2029b 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
2029c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2029d 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2029e 20 2a 2f 0a 20 20 0a 0a 20 20 61 73 73 65 72 74   */.  ..  assert
2029f 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  ( p!=0 );.  if( 
202a0 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  p->selFlags & SF
202a1 5f 52 65 73 6f 6c 76 65 64 20 29 7b 0a 20 20 20  _Resolved ){.   
202a2 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e   return WRC_Prun
202a3 65 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 65 72 4e  e;.  }.  pOuterN
202a4 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70  C = pWalker->u.p
202a5 4e 43 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70  NC;.  pParse = p
202a6 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a  Walker->pParse;.
202a7 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
202a8 62 3b 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 6c  b;..  /* Normall
202a9 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45  y sqlite3SelectE
202aa 78 70 61 6e 64 28 29 20 77 69 6c 6c 20 62 65 20  xpand() will be 
202ab 63 61 6c 6c 65 64 20 66 69 72 73 74 20 61 6e 64  called first and
202ac 20 77 69 6c 6c 20 68 61 76 65 0a 20 20 2a 2a 20   will have.  ** 
202ad 61 6c 72 65 61 64 79 20 65 78 70 61 6e 64 65 64  already expanded
202ae 20 74 68 69 73 20 53 45 4c 45 43 54 2e 20 20 48   this SELECT.  H
202af 6f 77 65 76 65 72 2c 20 69 66 20 74 68 69 73 20  owever, if this 
202b0 69 73 20 61 20 73 75 62 71 75 65 72 79 20 77 69  is a subquery wi
202b1 74 68 69 6e 0a 20 20 2a 2a 20 61 6e 20 65 78 70  thin.  ** an exp
202b2 72 65 73 73 69 6f 6e 2c 20 73 71 6c 69 74 65 33  ression, sqlite3
202b3 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
202b4 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65  () will be calle
202b5 64 20 77 69 74 68 6f 75 74 20 61 0a 20 20 2a 2a  d without a.  **
202b6 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73   prior call to s
202b7 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61  qlite3SelectExpa
202b8 6e 64 28 29 2e 20 20 57 68 65 6e 20 74 68 61 74  nd().  When that
202b9 20 68 61 70 70 65 6e 73 2c 20 6c 65 74 0a 20 20   happens, let.  
202ba 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  ** sqlite3Select
202bb 50 72 65 70 28 29 20 64 6f 20 61 6c 6c 20 6f 66  Prep() do all of
202bc 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20   the processing 
202bd 66 6f 72 20 74 68 69 73 20 53 45 4c 45 43 54 2e  for this SELECT.
202be 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c  .  ** sqlite3Sel
202bf 65 63 74 50 72 65 70 28 29 20 77 69 6c 6c 20 69  ectPrep() will i
202c0 6e 76 6f 6b 65 20 62 6f 74 68 20 73 71 6c 69 74  nvoke both sqlit
202c1 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29  e3SelectExpand()
202c2 20 61 6e 64 0a 20 20 2a 2a 20 74 68 69 73 20 72   and.  ** this r
202c3 6f 75 74 69 6e 65 20 69 6e 20 74 68 65 20 63 6f  outine in the co
202c4 72 72 65 63 74 20 6f 72 64 65 72 2e 0a 20 20 2a  rrect order..  *
202c5 2f 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c 46  /.  if( (p->selF
202c6 6c 61 67 73 20 26 20 53 46 5f 45 78 70 61 6e 64  lags & SF_Expand
202c7 65 64 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  ed)==0 ){.    sq
202c8 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28  lite3SelectPrep(
202c9 70 50 61 72 73 65 2c 20 70 2c 20 70 4f 75 74 65  pParse, p, pOute
202ca 72 4e 43 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  rNC);.    return
202cb 20 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c   (pParse->nErr |
202cc 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
202cd 65 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 74 20  ed) ? WRC_Abort 
202ce 3a 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d  : WRC_Prune;.  }
202cf 0a 0a 20 20 69 73 43 6f 6d 70 6f 75 6e 64 20 3d  ..  isCompound =
202d0 20 70 2d 3e 70 50 72 69 6f 72 21 3d 30 3b 0a 20   p->pPrior!=0;. 
202d1 20 6e 43 6f 6d 70 6f 75 6e 64 20 3d 20 30 3b 0a   nCompound = 0;.
202d2 20 20 70 4c 65 66 74 6d 6f 73 74 20 3d 20 70 3b    pLeftmost = p;
202d3 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20  .  while( p ){. 
202d4 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73     assert( (p->s
202d5 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 45 78 70  elFlags & SF_Exp
202d6 61 6e 64 65 64 29 21 3d 30 20 29 3b 0a 20 20 20  anded)!=0 );.   
202d7 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73 65 6c   assert( (p->sel
202d8 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f 6c  Flags & SF_Resol
202d9 76 65 64 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70  ved)==0 );.    p
202da 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46  ->selFlags |= SF
202db 5f 52 65 73 6f 6c 76 65 64 3b 0a 0a 20 20 20 20  _Resolved;..    
202dc 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 65  /* Resolve the e
202dd 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68  xpressions in th
202de 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53  e LIMIT and OFFS
202df 45 54 20 63 6c 61 75 73 65 73 2e 20 54 68 65 73  ET clauses. Thes
202e0 65 0a 20 20 20 20 2a 2a 20 61 72 65 20 6e 6f 74  e.    ** are not
202e1 20 61 6c 6c 6f 77 65 64 20 74 6f 20 72 65 66 65   allowed to refe
202e2 72 20 74 6f 20 61 6e 79 20 6e 61 6d 65 73 2c 20  r to any names, 
202e3 73 6f 20 70 61 73 73 20 61 6e 20 65 6d 70 74 79  so pass an empty
202e4 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 0a 20 20   NameContext..  
202e5 20 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28    */.    memset(
202e6 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28  &sNC, 0, sizeof(
202e7 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70  sNC));.    sNC.p
202e8 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
202e9 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
202ea 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
202eb 26 73 4e 43 2c 20 70 2d 3e 70 4c 69 6d 69 74 29  &sNC, p->pLimit)
202ec 20 7c 7c 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ||.        sqli
202ed 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
202ee 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 4f 66  mes(&sNC, p->pOf
202ef 66 73 65 74 29 20 29 7b 0a 20 20 20 20 20 20 72  fset) ){.      r
202f0 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
202f1 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
202f2 20 53 65 74 20 75 70 20 74 68 65 20 6c 6f 63 61   Set up the loca
202f3 6c 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 74  l name-context t
202f4 6f 20 70 61 73 73 20 74 6f 20 73 71 6c 69 74 65  o pass to sqlite
202f5 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
202f6 73 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65  s() to.    ** re
202f7 73 6f 6c 76 65 20 74 68 65 20 72 65 73 75 6c 74  solve the result
202f8 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20  -set expression 
202f9 6c 69 73 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  list..    */.   
202fa 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20   sNC.allowAgg = 
202fb 31 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 63 4c  1;.    sNC.pSrcL
202fc 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20  ist = p->pSrc;. 
202fd 20 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20 70     sNC.pNext = p
202fe 4f 75 74 65 72 4e 43 3b 0a 20 20 0a 20 20 20 20  OuterNC;.  .    
202ff 2f 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73  /* Resolve names
20300 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
20301 65 74 2e 20 2a 2f 0a 20 20 20 20 70 45 4c 69 73  et. */.    pELis
20302 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20  t = p->pEList;. 
20303 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73     assert( pELis
20304 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 28  t!=0 );.    for(
20305 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e  i=0; i<pEList->n
20306 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
20307 20 20 45 78 70 72 20 2a 70 58 20 3d 20 70 45 4c    Expr *pX = pEL
20308 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b  ist->a[i].pExpr;
20309 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
2030a 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d  e3ResolveExprNam
2030b 65 73 28 26 73 4e 43 2c 20 70 58 29 20 29 7b 0a  es(&sNC, pX) ){.
2030c 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57          return W
2030d 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20  RC_Abort;.      
2030e 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  }.    }.  .    /
2030f 2a 20 52 65 63 75 72 73 69 76 65 6c 79 20 72 65  * Recursively re
20310 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 61  solve names in a
20311 6c 6c 20 73 75 62 71 75 65 72 69 65 73 0a 20 20  ll subqueries.  
20312 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30    */.    for(i=0
20313 3b 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72  ; i<p->pSrc->nSr
20314 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  c; i++){.      s
20315 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
20316 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e  em *pItem = &p->
20317 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20  pSrc->a[i];.    
20318 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 53 65    if( pItem->pSe
20319 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20  lect ){.        
2031a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 61 76  const char *zSav
2031b 65 64 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 72  edContext = pPar
2031c 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74  se->zAuthContext
2031d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49  ;.        if( pI
2031e 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 20 70 50 61  tem->zName ) pPa
2031f 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78  rse->zAuthContex
20320 74 20 3d 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65  t = pItem->zName
20321 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
20322 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61  3ResolveSelectNa
20323 6d 65 73 28 70 50 61 72 73 65 2c 20 70 49 74 65  mes(pParse, pIte
20324 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 70 4f 75 74  m->pSelect, pOut
20325 65 72 4e 43 29 3b 0a 20 20 20 20 20 20 20 20 70  erNC);.        p
20326 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74  Parse->zAuthCont
20327 65 78 74 20 3d 20 7a 53 61 76 65 64 43 6f 6e 74  ext = zSavedCont
20328 65 78 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ext;.        if(
20329 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c   pParse->nErr ||
2032a 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2032b 64 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  d ) return WRC_A
2032c 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  bort;.      }.  
2032d 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66    }.  .    /* If
2032e 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 61 67   there are no ag
2032f 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
20330 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d  s in the result-
20331 73 65 74 2c 20 61 6e 64 20 6e 6f 20 47 52 4f 55  set, and no GROU
20332 50 20 42 59 20 0a 20 20 20 20 2a 2a 20 65 78 70  P BY .    ** exp
20333 72 65 73 73 69 6f 6e 2c 20 64 6f 20 6e 6f 74 20  ression, do not 
20334 61 6c 6c 6f 77 20 61 67 67 72 65 67 61 74 65 73  allow aggregates
20335 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 6f   in any of the o
20336 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 73  ther expressions
20337 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73  ..    */.    ass
20338 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67  ert( (p->selFlag
20339 73 20 26 20 53 46 5f 41 67 67 72 65 67 61 74 65  s & SF_Aggregate
2033a 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 47 72 6f  )==0 );.    pGro
2033b 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70  upBy = p->pGroup
2033c 42 79 3b 0a 20 20 20 20 69 66 28 20 70 47 72 6f  By;.    if( pGro
2033d 75 70 42 79 20 7c 7c 20 73 4e 43 2e 68 61 73 41  upBy || sNC.hasA
2033e 67 67 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 73  gg ){.      p->s
2033f 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 41 67  elFlags |= SF_Ag
20340 67 72 65 67 61 74 65 3b 0a 20 20 20 20 7d 65 6c  gregate;.    }el
20341 73 65 7b 0a 20 20 20 20 20 20 73 4e 43 2e 61 6c  se{.      sNC.al
20342 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20 20 20  lowAgg = 0;.    
20343 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 61  }.  .    /* If a
20344 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20 69   HAVING clause i
20345 73 20 70 72 65 73 65 6e 74 2c 20 74 68 65 6e 20  s present, then 
20346 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 20  there must be a 
20347 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e  GROUP BY clause.
20348 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
20349 70 2d 3e 70 48 61 76 69 6e 67 20 26 26 20 21 70  p->pHaving && !p
2034a 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 20  GroupBy ){.     
2034b 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2034c 28 70 50 61 72 73 65 2c 20 22 61 20 47 52 4f 55  (pParse, "a GROU
2034d 50 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 72  P BY clause is r
2034e 65 71 75 69 72 65 64 20 62 65 66 6f 72 65 20 48  equired before H
2034f 41 56 49 4e 47 22 29 3b 0a 20 20 20 20 20 20 72  AVING");.      r
20350 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
20351 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
20352 20 41 64 64 20 74 68 65 20 65 78 70 72 65 73 73   Add the express
20353 69 6f 6e 20 6c 69 73 74 20 74 6f 20 74 68 65 20  ion list to the 
20354 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 62 65 66  name-context bef
20355 6f 72 65 20 70 61 72 73 69 6e 67 20 74 68 65 0a  ore parsing the.
20356 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 65 78 70      ** other exp
20357 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20  ressions in the 
20358 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
20359 2e 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 61  . This is so tha
2035a 74 0a 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73  t.    ** express
2035b 69 6f 6e 73 20 69 6e 20 74 68 65 20 57 48 45 52  ions in the WHER
2035c 45 20 63 6c 61 75 73 65 20 28 65 74 63 2e 29 20  E clause (etc.) 
2035d 63 61 6e 20 72 65 66 65 72 20 74 6f 20 65 78 70  can refer to exp
2035e 72 65 73 73 69 6f 6e 73 20 62 79 0a 20 20 20 20  ressions by.    
2035f 2a 2a 20 61 6c 69 61 73 65 73 20 69 6e 20 74 68  ** aliases in th
20360 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 20 20  e result set..  
20361 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4d 69 6e 6f    **.    ** Mino
20362 72 20 70 6f 69 6e 74 3a 20 49 66 20 74 68 69 73  r point: If this
20363 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68   is the case, th
20364 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  en the expressio
20365 6e 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 2a 2a  n will be.    **
20366 20 72 65 2d 65 76 61 6c 75 61 74 65 64 20 66 6f   re-evaluated fo
20367 72 20 65 61 63 68 20 72 65 66 65 72 65 6e 63 65  r each reference
20368 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20   to it..    */. 
20369 20 20 20 73 4e 43 2e 70 45 4c 69 73 74 20 3d 20     sNC.pEList = 
2036a 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 69  p->pEList;.    i
2036b 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  f( sqlite3Resolv
2036c 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c  eExprNames(&sNC,
2036d 20 70 2d 3e 70 57 68 65 72 65 29 20 7c 7c 0a 20   p->pWhere) ||. 
2036e 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73        sqlite3Res
2036f 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73  olveExprNames(&s
20370 4e 43 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 0a  NC, p->pHaving).
20371 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74      ){.      ret
20372 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
20373 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65     }..    /* The
20374 20 4f 52 44 45 52 20 42 59 20 61 6e 64 20 47 52   ORDER BY and GR
20375 4f 55 50 20 42 59 20 63 6c 61 75 73 65 73 20 6d  OUP BY clauses m
20376 61 79 20 6e 6f 74 20 72 65 66 65 72 20 74 6f 20  ay not refer to 
20377 74 65 72 6d 73 20 69 6e 0a 20 20 20 20 2a 2a 20  terms in.    ** 
20378 6f 75 74 65 72 20 71 75 65 72 69 65 73 20 0a 20  outer queries . 
20379 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 4e     */.    sNC.pN
2037a 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 73 4e 43  ext = 0;.    sNC
2037b 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 0a  .allowAgg = 1;..
2037c 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 74      /* Process t
2037d 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2037e 73 65 20 66 6f 72 20 73 69 6e 67 6c 65 74 6f 6e  se for singleton
2037f 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
20380 74 73 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 4f  ts..    ** The O
20381 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 66  RDER BY clause f
20382 6f 72 20 63 6f 6d 70 6f 75 6e 64 73 20 53 45 4c  or compounds SEL
20383 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69  ECT statements i
20384 73 20 68 61 6e 64 6c 65 64 0a 20 20 20 20 2a 2a  s handled.    **
20385 20 62 65 6c 6f 77 2c 20 61 66 74 65 72 20 61 6c   below, after al
20386 6c 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d  l of the result-
20387 73 65 74 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20  sets for all of 
20388 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 0a  the elements of.
20389 20 20 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f      ** the compo
2038a 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72 65  und have been re
2038b 73 6f 6c 76 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  solved..    */. 
2038c 20 20 20 69 66 28 20 21 69 73 43 6f 6d 70 6f 75     if( !isCompou
2038d 6e 64 20 26 26 20 72 65 73 6f 6c 76 65 4f 72 64  nd && resolveOrd
2038e 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 2c 20  erGroupBy(&sNC, 
2038f 70 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20  p, p->pOrderBy, 
20390 22 4f 52 44 45 52 22 29 20 29 7b 0a 20 20 20 20  "ORDER") ){.    
20391 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    return WRC_Abo
20392 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  rt;.    }.    if
20393 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
20394 65 64 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ed ){.      retu
20395 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
20396 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65    }.  .    /* Re
20397 73 6f 6c 76 65 20 74 68 65 20 47 52 4f 55 50 20  solve the GROUP 
20398 42 59 20 63 6c 61 75 73 65 2e 20 20 41 74 20 74  BY clause.  At t
20399 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 6d 61  he same time, ma
2039a 6b 65 20 73 75 72 65 20 0a 20 20 20 20 2a 2a 20  ke sure .    ** 
2039b 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61  the GROUP BY cla
2039c 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e  use does not con
2039d 74 61 69 6e 20 61 67 67 72 65 67 61 74 65 20 66  tain aggregate f
2039e 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f  unctions..    */
2039f 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42  .    if( pGroupB
203a0 79 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63  y ){.      struc
203a1 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
203a2 2a 70 49 74 65 6d 3b 0a 20 20 20 20 0a 20 20 20  *pItem;.    .   
203a3 20 20 20 69 66 28 20 72 65 73 6f 6c 76 65 4f 72     if( resolveOr
203a4 64 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 2c  derGroupBy(&sNC,
203a5 20 70 2c 20 70 47 72 6f 75 70 42 79 2c 20 22 47   p, pGroupBy, "G
203a6 52 4f 55 50 22 29 20 7c 7c 20 64 62 2d 3e 6d 61  ROUP") || db->ma
203a7 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
203a8 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
203a9 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a  _Abort;.      }.
203aa 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70        for(i=0, p
203ab 49 74 65 6d 3d 70 47 72 6f 75 70 42 79 2d 3e 61  Item=pGroupBy->a
203ac 3b 20 69 3c 70 47 72 6f 75 70 42 79 2d 3e 6e 45  ; i<pGroupBy->nE
203ad 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  xpr; i++, pItem+
203ae 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
203af 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
203b0 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 45 50  pItem->pExpr, EP
203b1 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20 20  _Agg) ){.       
203b2 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
203b3 73 67 28 70 50 61 72 73 65 2c 20 22 61 67 67 72  sg(pParse, "aggr
203b4 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20  egate functions 
203b5 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20  are not allowed 
203b6 69 6e 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  in ".           
203b7 20 20 20 22 74 68 65 20 47 52 4f 55 50 20 42 59     "the GROUP BY
203b8 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 20 20   clause");.     
203b9 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
203ba 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 7d  Abort;.        }
203bb 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
203bc 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74      /* Advance t
203bd 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d 20  o the next term 
203be 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 0a  of the compound.
203bf 20 20 20 20 2a 2f 0a 20 20 20 20 70 20 3d 20 70      */.    p = p
203c0 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 6e 43  ->pPrior;.    nC
203c1 6f 6d 70 6f 75 6e 64 2b 2b 3b 0a 20 20 7d 0a 0a  ompound++;.  }..
203c2 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65    /* Resolve the
203c3 20 4f 52 44 45 52 20 42 59 20 6f 6e 20 61 20 63   ORDER BY on a c
203c4 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 61  ompound SELECT a
203c5 66 74 65 72 20 61 6c 6c 20 74 65 72 6d 73 20 6f  fter all terms o
203c6 66 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70 6f  f.  ** the compo
203c7 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72 65  und have been re
203c8 73 6f 6c 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  solved..  */.  i
203c9 66 28 20 69 73 43 6f 6d 70 6f 75 6e 64 20 26 26  f( isCompound &&
203ca 20 72 65 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64   resolveCompound
203cb 4f 72 64 65 72 42 79 28 70 50 61 72 73 65 2c 20  OrderBy(pParse, 
203cc 70 4c 65 66 74 6d 6f 73 74 29 20 29 7b 0a 20 20  pLeftmost) ){.  
203cd 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    return WRC_Abo
203ce 72 74 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  rt;.  }..  retur
203cf 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 7d 0a 0a  n WRC_Prune;.}..
203d0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
203d1 6e 65 20 77 61 6c 6b 73 20 61 6e 20 65 78 70 72  ne walks an expr
203d2 65 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64 20  ession tree and 
203d3 72 65 73 6f 6c 76 65 73 20 72 65 66 65 72 65 6e  resolves referen
203d4 63 65 73 20 74 6f 0a 2a 2a 20 74 61 62 6c 65 20  ces to.** table 
203d5 63 6f 6c 75 6d 6e 73 20 61 6e 64 20 72 65 73 75  columns and resu
203d6 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 73 2e 20  lt-set columns. 
203d7 20 41 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   At the same tim
203d8 65 2c 20 64 6f 20 65 72 72 6f 72 0a 2a 2a 20 63  e, do error.** c
203d9 68 65 63 6b 69 6e 67 20 6f 6e 20 66 75 6e 63 74  hecking on funct
203da 69 6f 6e 20 75 73 61 67 65 20 61 6e 64 20 73 65  ion usage and se
203db 74 20 61 20 66 6c 61 67 20 69 66 20 61 6e 79 20  t a flag if any 
203dc 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
203dd 6f 6e 73 0a 2a 2a 20 61 72 65 20 73 65 65 6e 2e  ons.** are seen.
203de 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76  .**.** To resolv
203df 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20  e table columns 
203e0 72 65 66 65 72 65 6e 63 65 73 20 77 65 20 6c 6f  references we lo
203e1 6f 6b 20 66 6f 72 20 6e 6f 64 65 73 20 28 6f 72  ok for nodes (or
203e2 20 73 75 62 74 72 65 65 73 29 20 6f 66 20 74 68   subtrees) of th
203e3 65 20 0a 2a 2a 20 66 6f 72 6d 20 58 2e 59 2e 5a  e .** form X.Y.Z
203e4 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74 20   or Y.Z or just 
203e5 5a 20 77 68 65 72 65 0a 2a 2a 0a 2a 2a 20 20 20  Z where.**.**   
203e6 20 20 20 58 3a 20 20 20 54 68 65 20 6e 61 6d 65     X:   The name
203e7 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 2e 20   of a database. 
203e8 20 45 78 3a 20 20 22 6d 61 69 6e 22 20 6f 72 20   Ex:  "main" or 
203e9 22 74 65 6d 70 22 20 6f 72 0a 2a 2a 20 20 20 20  "temp" or.**    
203ea 20 20 20 20 20 20 20 74 68 65 20 73 79 6d 62 6f         the symbo
203eb 6c 69 63 20 6e 61 6d 65 20 61 73 73 69 67 6e 65  lic name assigne
203ec 64 20 74 6f 20 61 6e 20 41 54 54 41 43 48 2d 65  d to an ATTACH-e
203ed 64 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a  d database..**.*
203ee 2a 20 20 20 20 20 20 59 3a 20 20 20 54 68 65 20  *      Y:   The 
203ef 6e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c 65 20  name of a table 
203f0 69 6e 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65  in a FROM clause
203f1 2e 20 20 4f 72 20 69 6e 20 61 20 74 72 69 67 67  .  Or in a trigg
203f2 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  er.**           
203f3 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69  one of the speci
203f4 61 6c 20 6e 61 6d 65 73 20 22 6f 6c 64 22 20 6f  al names "old" o
203f5 72 20 22 6e 65 77 22 2e 0a 2a 2a 0a 2a 2a 20 20  r "new"..**.**  
203f6 20 20 20 20 5a 3a 20 20 20 54 68 65 20 6e 61 6d      Z:   The nam
203f7 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 6e  e of a column in
203f8 20 74 61 62 6c 65 20 59 2e 0a 2a 2a 0a 2a 2a 20   table Y..**.** 
203f9 54 68 65 20 6e 6f 64 65 20 61 74 20 74 68 65 20  The node at the 
203fa 72 6f 6f 74 20 6f 66 20 74 68 65 20 73 75 62 74  root of the subt
203fb 72 65 65 20 69 73 20 6d 6f 64 69 66 69 65 64 20  ree is modified 
203fc 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
203fd 2a 20 20 20 20 45 78 70 72 2e 6f 70 20 20 20 20  *    Expr.op    
203fe 20 20 20 20 43 68 61 6e 67 65 64 20 74 6f 20 54      Changed to T
203ff 4b 5f 43 4f 4c 55 4d 4e 0a 2a 2a 20 20 20 20 45  K_COLUMN.**    E
20400 78 70 72 2e 70 54 61 62 20 20 20 20 20 20 50 6f  xpr.pTab      Po
20401 69 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62 6c  ints to the Tabl
20402 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 58 2e 59  e object for X.Y
20403 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 43 6f 6c  .**    Expr.iCol
20404 75 6d 6e 20 20 20 54 68 65 20 63 6f 6c 75 6d 6e  umn   The column
20405 20 69 6e 64 65 78 20 69 6e 20 58 2e 59 2e 20 20   index in X.Y.  
20406 2d 31 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64  -1 for the rowid
20407 2e 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 54 61  ..**    Expr.iTa
20408 62 6c 65 20 20 20 20 54 68 65 20 56 44 42 45 20  ble    The VDBE 
20409 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f  cursor number fo
2040a 72 20 58 2e 59 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  r X.Y.**.**.** T
2040b 6f 20 72 65 73 6f 6c 76 65 20 72 65 73 75 6c 74  o resolve result
2040c 2d 73 65 74 20 72 65 66 65 72 65 6e 63 65 73 2c  -set references,
2040d 20 6c 6f 6f 6b 20 66 6f 72 20 65 78 70 72 65 73   look for expres
2040e 73 69 6f 6e 20 6e 6f 64 65 73 20 6f 66 20 74 68  sion nodes of th
2040f 65 0a 2a 2a 20 66 6f 72 6d 20 5a 20 28 77 69 74  e.** form Z (wit
20410 68 20 6e 6f 20 58 20 61 6e 64 20 59 20 70 72 65  h no X and Y pre
20411 66 69 78 29 20 77 68 65 72 65 20 74 68 65 20 5a  fix) where the Z
20412 20 6d 61 74 63 68 65 73 20 74 68 65 20 72 69 67   matches the rig
20413 68 74 2d 68 61 6e 64 0a 2a 2a 20 73 69 7a 65 20  ht-hand.** size 
20414 6f 66 20 61 6e 20 41 53 20 63 6c 61 75 73 65 20  of an AS clause 
20415 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65  in the result-se
20416 74 20 6f 66 20 61 20 53 45 4c 45 43 54 2e 20 20  t of a SELECT.  
20417 54 68 65 20 5a 20 65 78 70 72 65 73 73 69 6f 6e  The Z expression
20418 0a 2a 2a 20 69 73 20 72 65 70 6c 61 63 65 64 20  .** is replaced 
20419 62 79 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  by a copy of the
2041a 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20   left-hand side 
2041b 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65  of the result-se
2041c 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a  t expression..**
2041d 20 54 61 62 6c 65 2d 6e 61 6d 65 20 61 6e 64 20   Table-name and 
2041e 66 75 6e 63 74 69 6f 6e 20 72 65 73 6f 6c 75 74  function resolut
2041f 69 6f 6e 20 6f 63 63 75 72 73 20 6f 6e 20 74 68  ion occurs on th
20420 65 20 73 75 62 73 74 69 74 75 74 65 64 20 65 78  e substituted ex
20421 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65  pression.** tree
20422 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  .  For example, 
20423 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 53  in:.**.**      S
20424 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 2c 20  ELECT a+b AS x, 
20425 63 2b 64 20 41 53 20 79 20 46 52 4f 4d 20 74 31  c+d AS y FROM t1
20426 20 4f 52 44 45 52 20 42 59 20 78 3b 0a 2a 2a 0a   ORDER BY x;.**.
20427 2a 2a 20 54 68 65 20 22 78 22 20 74 65 72 6d 20  ** The "x" term 
20428 6f 66 20 74 68 65 20 6f 72 64 65 72 20 62 79 20  of the order by 
20429 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 22  is replaced by "
2042a 61 2b 62 22 20 74 6f 20 72 65 6e 64 65 72 3a 0a  a+b" to render:.
2042b 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 45 4c 45 43  **.**      SELEC
2042c 54 20 61 2b 62 20 41 53 20 78 2c 20 63 2b 64 20  T a+b AS x, c+d 
2042d 41 53 20 79 20 46 52 4f 4d 20 74 31 20 4f 52 44  AS y FROM t1 ORD
2042e 45 52 20 42 59 20 61 2b 62 3b 0a 2a 2a 0a 2a 2a  ER BY a+b;.**.**
2042f 20 46 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 20   Function calls 
20430 61 72 65 20 63 68 65 63 6b 65 64 20 74 6f 20 6d  are checked to m
20431 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68  ake sure that th
20432 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 0a 2a  e function is .*
20433 2a 20 64 65 66 69 6e 65 64 20 61 6e 64 20 74 68  * defined and th
20434 61 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 6e  at the correct n
20435 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
20436 74 73 20 61 72 65 20 73 70 65 63 69 66 69 65 64  ts are specified
20437 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 75 6e 63  ..** If the func
20438 74 69 6f 6e 20 69 73 20 61 6e 20 61 67 67 72 65  tion is an aggre
20439 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 74  gate function, t
2043a 68 65 6e 20 74 68 65 20 70 4e 43 2d 3e 68 61 73  hen the pNC->has
2043b 41 67 67 20 69 73 0a 2a 2a 20 73 65 74 20 61 6e  Agg is.** set an
2043c 64 20 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20  d the opcode is 
2043d 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 54 4b 5f  changed from TK_
2043e 46 55 4e 43 54 49 4f 4e 20 74 6f 20 54 4b 5f 41  FUNCTION to TK_A
2043f 47 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2a 20  GG_FUNCTION..** 
20440 49 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  If an expression
20441 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67   contains aggreg
20442 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 74 68  ate functions th
20443 65 6e 20 74 68 65 20 45 50 5f 41 67 67 0a 2a 2a  en the EP_Agg.**
20444 20 70 72 6f 70 65 72 74 79 20 6f 6e 20 74 68 65   property on the
20445 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73   expression is s
20446 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 72 72  et..**.** An err
20447 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 6c 65  or message is le
20448 66 74 20 69 6e 20 70 50 61 72 73 65 20 69 66 20  ft in pParse if 
20449 61 6e 79 74 68 69 6e 67 20 69 73 20 61 6d 69 73  anything is amis
2044a 73 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a  s.  The number.*
2044b 2a 20 69 66 20 65 72 72 6f 72 73 20 69 73 20 72  * if errors is r
2044c 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  eturned..*/.SQLI
2044d 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2044e 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
2044f 72 4e 61 6d 65 73 28 20 0a 20 20 4e 61 6d 65 43  rNames( .  NameC
20450 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 20  ontext *pNC,    
20451 20 20 20 2f 2a 20 4e 61 6d 65 73 70 61 63 65 20     /* Namespace 
20452 74 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72 65  to resolve expre
20453 73 73 69 6f 6e 73 20 69 6e 2e 20 2a 2f 0a 20 20  ssions in. */.  
20454 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20  Expr *pExpr     
20455 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
20456 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20  xpression to be 
20457 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f 0a 29 7b 0a  analyzed. */.){.
20458 20 20 69 6e 74 20 73 61 76 65 64 48 61 73 41 67    int savedHasAg
20459 67 3b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a  g;.  Walker w;..
2045a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29    if( pExpr==0 )
2045b 20 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20 53   return 0;.#if S
2045c 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
2045d 45 50 54 48 3e 30 0a 20 20 7b 0a 20 20 20 20 50  EPTH>0.  {.    P
2045e 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70  arse *pParse = p
2045f 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20  NC->pParse;.    
20460 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 43  if( sqlite3ExprC
20461 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72 73  heckHeight(pPars
20462 65 2c 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 68  e, pExpr->nHeigh
20463 74 2b 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e  t+pNC->pParse->n
20464 48 65 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20  Height) ){.     
20465 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
20466 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 48 65  .    pParse->nHe
20467 69 67 68 74 20 2b 3d 20 70 45 78 70 72 2d 3e 6e  ight += pExpr->n
20468 48 65 69 67 68 74 3b 0a 20 20 7d 0a 23 65 6e 64  Height;.  }.#end
20469 69 66 0a 20 20 73 61 76 65 64 48 61 73 41 67 67  if.  savedHasAgg
2046a 20 3d 20 70 4e 43 2d 3e 68 61 73 41 67 67 3b 0a   = pNC->hasAgg;.
2046b 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20    pNC->hasAgg = 
2046c 30 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c  0;.  w.xExprCall
2046d 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78  back = resolveEx
2046e 70 72 53 74 65 70 3b 0a 20 20 77 2e 78 53 65 6c  prStep;.  w.xSel
2046f 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65  ectCallback = re
20470 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 3b  solveSelectStep;
20471 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 4e  .  w.pParse = pN
20472 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 77 2e 75  C->pParse;.  w.u
20473 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20 73 71  .pNC = pNC;.  sq
20474 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 77  lite3WalkExpr(&w
20475 2c 20 70 45 78 70 72 29 3b 0a 23 69 66 20 53 51  , pExpr);.#if SQ
20476 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45  LITE_MAX_EXPR_DE
20477 50 54 48 3e 30 0a 20 20 70 4e 43 2d 3e 70 50 61  PTH>0.  pNC->pPa
20478 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2d 3d 20  rse->nHeight -= 
20479 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a  pExpr->nHeight;.
2047a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 4e 43  #endif.  if( pNC
2047b 2d 3e 6e 45 72 72 3e 30 20 7c 7c 20 77 2e 70 50  ->nErr>0 || w.pP
2047c 61 72 73 65 2d 3e 6e 45 72 72 3e 30 20 29 7b 0a  arse->nErr>0 ){.
2047d 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65      ExprSetPrope
2047e 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 45 72  rty(pExpr, EP_Er
2047f 72 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ror);.  }.  if( 
20480 70 4e 43 2d 3e 68 61 73 41 67 67 20 29 7b 0a 20  pNC->hasAgg ){. 
20481 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72     ExprSetProper
20482 74 79 28 70 45 78 70 72 2c 20 45 50 5f 41 67 67  ty(pExpr, EP_Agg
20483 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  );.  }else if( s
20484 61 76 65 64 48 61 73 41 67 67 20 29 7b 0a 20 20  avedHasAgg ){.  
20485 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20    pNC->hasAgg = 
20486 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
20487 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
20488 70 45 78 70 72 2c 20 45 50 5f 45 72 72 6f 72 29  pExpr, EP_Error)
20489 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f  ;.}.../*.** Reso
2048a 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73 20 69 6e  lve all names in
2048b 20 61 6c 6c 20 65 78 70 72 65 73 73 69 6f 6e 73   all expressions
2048c 20 6f 66 20 61 20 53 45 4c 45 43 54 20 61 6e 64   of a SELECT and
2048d 20 69 6e 20 61 6c 6c 0a 2a 2a 20 64 65 63 65 6e   in all.** decen
2048e 64 65 6e 74 73 20 6f 66 20 74 68 65 20 53 45 4c  dents of the SEL
2048f 45 43 54 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63  ECT, including c
20490 6f 6d 70 6f 75 6e 64 73 20 6f 66 66 20 6f 66 20  ompounds off of 
20491 70 2d 3e 70 50 72 69 6f 72 2c 0a 2a 2a 20 73 75  p->pPrior,.** su
20492 62 71 75 65 72 69 65 73 20 69 6e 20 65 78 70 72  bqueries in expr
20493 65 73 73 69 6f 6e 73 2c 20 61 6e 64 20 73 75 62  essions, and sub
20494 71 75 65 72 69 65 73 20 75 73 65 64 20 61 73 20  queries used as 
20495 46 52 4f 4d 20 63 6c 61 75 73 65 0a 2a 2a 20 74  FROM clause.** t
20496 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  erms..**.** See 
20497 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
20498 70 72 4e 61 6d 65 73 28 29 20 66 6f 72 20 61 20  prNames() for a 
20499 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74  description of t
2049a 68 65 20 6b 69 6e 64 73 20 6f 66 0a 2a 2a 20 74  he kinds of.** t
2049b 72 61 6e 73 66 6f 72 6d 61 74 69 6f 6e 73 20 74  ransformations t
2049c 68 61 74 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a 2a  hat occur..**.**
2049d 20 41 6c 6c 20 53 45 4c 45 43 54 20 73 74 61 74   All SELECT stat
2049e 65 6d 65 6e 74 73 20 73 68 6f 75 6c 64 20 68 61  ements should ha
2049f 76 65 20 62 65 65 6e 20 65 78 70 61 6e 64 65 64  ve been expanded
204a0 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65   using.** sqlite
204a1 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20  3SelectExpand() 
204a2 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e  prior to invokin
204a3 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a  g this routine..
204a4 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
204a5 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
204a6 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73  solveSelectNames
204a7 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
204a8 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  e,         /* Th
204a9 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
204aa 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c   */.  Select *p,
204ab 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
204ac 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  The SELECT state
204ad 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64  ment being coded
204ae 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65  . */.  NameConte
204af 78 74 20 2a 70 4f 75 74 65 72 4e 43 20 20 2f 2a  xt *pOuterNC  /*
204b0 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f   Name context fo
204b1 72 20 70 61 72 65 6e 74 20 53 45 4c 45 43 54 20  r parent SELECT 
204b2 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a  statement */.){.
204b3 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a 20 20 61    Walker w;..  a
204b4 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
204b5 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b   w.xExprCallback
204b6 20 3d 20 72 65 73 6f 6c 76 65 45 78 70 72 53 74   = resolveExprSt
204b7 65 70 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43  ep;.  w.xSelectC
204b8 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76  allback = resolv
204b9 65 53 65 6c 65 63 74 53 74 65 70 3b 0a 20 20 77  eSelectStep;.  w
204ba 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
204bb 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4f  ;.  w.u.pNC = pO
204bc 75 74 65 72 4e 43 3b 0a 20 20 73 71 6c 69 74 65  uterNC;.  sqlite
204bd 33 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c 20  3WalkSelect(&w, 
204be 70 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  p);.}../********
204bf 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 65  ****** End of re
204c0 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  solve.c ********
204c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
204c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
204c3 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
204c4 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
204c5 65 20 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e expr.c *******
204c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
204c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
204c8 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
204c9 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
204ca 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
204cb 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
204cc 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
204cd 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
204ce 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
204cf 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
204d0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
204d1 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
204d2 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
204d3 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
204d4 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
204d5 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
204d6 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
204d7 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
204d8 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
204d9 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
204da 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
204db 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
204dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
204dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
204de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
204df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
204e0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
204e1 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73 65  ins routines use
204e2 64 20 66 6f 72 20 61 6e 61 6c 79 7a 69 6e 67 20  d for analyzing 
204e3 65 78 70 72 65 73 73 69 6f 6e 73 20 61 6e 64 0a  expressions and.
204e4 2a 2a 20 66 6f 72 20 67 65 6e 65 72 61 74 69 6e  ** for generatin
204e5 67 20 56 44 42 45 20 63 6f 64 65 20 74 68 61 74  g VDBE code that
204e6 20 65 76 61 6c 75 61 74 65 73 20 65 78 70 72 65   evaluates expre
204e7 73 73 69 6f 6e 73 20 69 6e 20 53 51 4c 69 74 65  ssions in SQLite
204e8 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ..*/../*.** Retu
204e9 72 6e 20 74 68 65 20 27 61 66 66 69 6e 69 74 79  rn the 'affinity
204ea 27 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73  ' of the express
204eb 69 6f 6e 20 70 45 78 70 72 20 69 66 20 61 6e 79  ion pExpr if any
204ec 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 45 78 70 72  ..**.** If pExpr
204ed 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 61 20   is a column, a 
204ee 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 63  reference to a c
204ef 6f 6c 75 6d 6e 20 76 69 61 20 61 6e 20 27 41 53  olumn via an 'AS
204f0 27 20 61 6c 69 61 73 2c 0a 2a 2a 20 6f 72 20 61  ' alias,.** or a
204f1 20 73 75 62 2d 73 65 6c 65 63 74 20 77 69 74 68   sub-select with
204f2 20 61 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65   a column as the
204f3 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2c 20 74   return value, t
204f4 68 65 6e 20 74 68 65 20 0a 2a 2a 20 61 66 66 69  hen the .** affi
204f5 6e 69 74 79 20 6f 66 20 74 68 61 74 20 63 6f 6c  nity of that col
204f6 75 6d 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e  umn is returned.
204f7 20 4f 74 68 65 72 77 69 73 65 2c 20 30 78 30 30   Otherwise, 0x00
204f8 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a 2a   is returned,.**
204f9 20 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20 61   indicating no a
204fa 66 66 69 6e 69 74 79 20 66 6f 72 20 74 68 65 20  ffinity for the 
204fb 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a  expression..**.*
204fc 2a 20 69 2e 65 2e 20 74 68 65 20 57 48 45 52 45  * i.e. the WHERE
204fd 20 63 6c 61 75 73 65 20 65 78 70 72 65 73 73 73   clause expresss
204fe 69 6f 6e 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c  ions in the foll
204ff 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73  owing statements
20500 20 61 6c 6c 0a 2a 2a 20 68 61 76 65 20 61 6e 20   all.** have an 
20501 61 66 66 69 6e 69 74 79 3a 0a 2a 2a 0a 2a 2a 20  affinity:.**.** 
20502 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28  CREATE TABLE t1(
20503 61 29 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20  a);.** SELECT * 
20504 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61 3b  FROM t1 WHERE a;
20505 0a 2a 2a 20 53 45 4c 45 43 54 20 61 20 41 53 20  .** SELECT a AS 
20506 62 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20  b FROM t1 WHERE 
20507 62 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20 46  b;.** SELECT * F
20508 52 4f 4d 20 74 31 20 57 48 45 52 45 20 28 73 65  ROM t1 WHERE (se
20509 6c 65 63 74 20 61 20 66 72 6f 6d 20 74 31 29 3b  lect a from t1);
2050a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2050b 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33 45  TE char sqlite3E
2050c 78 70 72 41 66 66 69 6e 69 74 79 28 45 78 70 72  xprAffinity(Expr
2050d 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 20   *pExpr){.  int 
2050e 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a  op = pExpr->op;.
2050f 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 45 4c    if( op==TK_SEL
20510 45 43 54 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ECT ){.    asser
20511 74 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 26  t( pExpr->flags&
20512 45 50 5f 78 49 73 53 65 6c 65 63 74 20 29 3b 0a  EP_xIsSelect );.
20513 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
20514 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70  e3ExprAffinity(p
20515 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2d  Expr->x.pSelect-
20516 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  >pEList->a[0].pE
20517 78 70 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65  xpr);.  }.#ifnde
20518 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41  f SQLITE_OMIT_CA
20519 53 54 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  ST.  if( op==TK_
2051a 43 41 53 54 20 29 7b 0a 20 20 20 20 61 73 73 65  CAST ){.    asse
2051b 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
2051c 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49  erty(pExpr, EP_I
2051d 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20  ntValue) );.    
2051e 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 66  return sqlite3Af
2051f 66 69 6e 69 74 79 54 79 70 65 28 70 45 78 70 72  finityType(pExpr
20520 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 7d  ->u.zToken);.  }
20521 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 28 6f  .#endif.  if( (o
20522 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  p==TK_AGG_COLUMN
20523 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d   || op==TK_COLUM
20524 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47 49  N || op==TK_REGI
20525 53 54 45 52 29 20 0a 20 20 20 26 26 20 70 45 78  STER) .   && pEx
20526 70 72 2d 3e 70 54 61 62 21 3d 30 0a 20 20 29 7b  pr->pTab!=0.  ){
20527 0a 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f 52  .    /* op==TK_R
20528 45 47 49 53 54 45 52 20 26 26 20 70 45 78 70 72  EGISTER && pExpr
20529 2d 3e 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e  ->pTab!=0 happen
2052a 73 20 77 68 65 6e 20 70 45 78 70 72 20 77 61 73  s when pExpr was
2052b 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20   originally.    
2052c 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62  ** a TK_COLUMN b
2052d 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c  ut was previousl
2052e 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20  y evaluated and 
2052f 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69  cached in a regi
20530 73 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ster */.    int 
20531 6a 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  j = pExpr->iColu
20532 6d 6e 3b 0a 20 20 20 20 69 66 28 20 6a 3c 30 20  mn;.    if( j<0 
20533 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
20534 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  AFF_INTEGER;.   
20535 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
20536 70 54 61 62 20 26 26 20 6a 3c 70 45 78 70 72 2d  pTab && j<pExpr-
20537 3e 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20  >pTab->nCol );. 
20538 20 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 2d     return pExpr-
20539 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 61  >pTab->aCol[j].a
2053a 66 66 69 6e 69 74 79 3b 0a 20 20 7d 0a 20 20 72  ffinity;.  }.  r
2053b 65 74 75 72 6e 20 70 45 78 70 72 2d 3e 61 66 66  eturn pExpr->aff
2053c 69 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  inity;.}../*.** 
2053d 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e  Set the collatin
2053e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65  g sequence for e
2053f 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20  xpression pExpr 
20540 74 6f 20 62 65 20 74 68 65 20 63 6f 6c 6c 61 74  to be the collat
20541 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20  ing.** sequence 
20542 6e 61 6d 65 64 20 62 79 20 70 54 6f 6b 65 6e 2e  named by pToken.
20543 20 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e     Return a poin
20544 74 65 72 20 74 6f 20 74 68 65 20 72 65 76 69 73  ter to the revis
20545 65 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a  ed expression..*
20546 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20  * The collating 
20547 73 65 71 75 65 6e 63 65 20 69 73 20 6d 61 72 6b  sequence is mark
20548 65 64 20 61 73 20 22 65 78 70 6c 69 63 69 74 22  ed as "explicit"
20549 20 75 73 69 6e 67 20 74 68 65 20 45 50 5f 45 78   using the EP_Ex
2054a 70 43 6f 6c 6c 61 74 65 0a 2a 2a 20 66 6c 61 67  pCollate.** flag
2054b 2e 20 20 41 6e 20 65 78 70 6c 69 63 69 74 20 63  .  An explicit c
2054c 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
2054d 65 20 77 69 6c 6c 20 6f 76 65 72 72 69 64 65 20  e will override 
2054e 69 6d 70 6c 69 63 69 74 0a 2a 2a 20 63 6f 6c 6c  implicit.** coll
2054f 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 2e  ating sequences.
20550 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20551 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
20552 45 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73  ExprSetColl(Pars
20553 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
20554 2a 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a 70  *pExpr, Token *p
20555 43 6f 6c 6c 4e 61 6d 65 29 7b 0a 20 20 63 68 61  CollName){.  cha
20556 72 20 2a 7a 43 6f 6c 6c 20 3d 20 30 3b 20 20 20  r *zColl = 0;   
20557 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 71 75           /* Dequ
20558 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f 6c  oted name of col
20559 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
2055a 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43  */.  CollSeq *pC
2055b 6f 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  oll;.  sqlite3 *
2055c 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2055d 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  .  zColl = sqlit
2055e 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
2055f 64 62 2c 20 70 43 6f 6c 6c 4e 61 6d 65 29 3b 0a  db, pCollName);.
20560 20 20 69 66 28 20 70 45 78 70 72 20 26 26 20 7a    if( pExpr && z
20561 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f 6c  Coll ){.    pCol
20562 6c 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  l = sqlite3Locat
20563 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  eCollSeq(pParse,
20564 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28   zColl);.    if(
20565 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20   pColl ){.      
20566 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 70  pExpr->pColl = p
20567 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 70 45 78 70  Coll;.      pExp
20568 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45  r->flags |= EP_E
20569 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 7d  xpCollate;.    }
2056a 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62  .  }.  sqlite3Db
2056b 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b  Free(db, zColl);
2056c 0a 20 20 72 65 74 75 72 6e 20 70 45 78 70 72 3b  .  return pExpr;
2056d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
2056e 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6c   the default col
2056f 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
20570 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 73 69  for the expressi
20571 6f 6e 20 70 45 78 70 72 2e 20 49 66 0a 2a 2a 20  on pExpr. If.** 
20572 74 68 65 72 65 20 69 73 20 6e 6f 20 64 65 66 61  there is no defa
20573 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79  ult collation ty
20574 70 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f  pe, return 0..*/
20575 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20576 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33  CollSeq *sqlite3
20577 45 78 70 72 43 6f 6c 6c 53 65 71 28 50 61 72 73  ExprCollSeq(Pars
20578 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
20579 2a 70 45 78 70 72 29 7b 0a 20 20 43 6f 6c 6c 53  *pExpr){.  CollS
2057a 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20  eq *pColl = 0;. 
2057b 20 45 78 70 72 20 2a 70 20 3d 20 70 45 78 70 72   Expr *p = pExpr
2057c 3b 0a 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59  ;.  while( ALWAY
2057d 53 28 70 29 20 29 7b 0a 20 20 20 20 69 6e 74 20  S(p) ){.    int 
2057e 6f 70 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20  op;.    pColl = 
2057f 70 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 69 66  p->pColl;.    if
20580 28 20 70 43 6f 6c 6c 20 29 20 62 72 65 61 6b 3b  ( pColl ) break;
20581 0a 20 20 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b  .    op = p->op;
20582 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 54 61 62  .    if( p->pTab
20583 21 3d 30 20 26 26 20 28 0a 20 20 20 20 20 20 20  !=0 && (.       
20584 20 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55   op==TK_AGG_COLU
20585 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c  MN || op==TK_COL
20586 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45  UMN || op==TK_RE
20587 47 49 53 54 45 52 20 7c 7c 20 6f 70 3d 3d 54 4b  GISTER || op==TK
20588 5f 54 52 49 47 47 45 52 0a 20 20 20 20 29 29 7b  _TRIGGER.    )){
20589 0a 20 20 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b  .      /* op==TK
2058a 5f 52 45 47 49 53 54 45 52 20 26 26 20 70 2d 3e  _REGISTER && p->
2058b 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e 73 20  pTab!=0 happens 
2058c 77 68 65 6e 20 70 45 78 70 72 20 77 61 73 20 6f  when pExpr was o
2058d 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 20 20  riginally.      
2058e 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62  ** a TK_COLUMN b
2058f 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c  ut was previousl
20590 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20  y evaluated and 
20591 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69  cached in a regi
20592 73 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 6f  ster */.      co
20593 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b  nst char *zColl;
20594 0a 20 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 70  .      int j = p
20595 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20  ->iColumn;.     
20596 20 69 66 28 20 6a 3e 3d 30 20 29 7b 0a 20 20 20   if( j>=0 ){.   
20597 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62       sqlite3 *db
20598 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
20599 20 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70         zColl = p
2059a 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e  ->pTab->aCol[j].
2059b 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70  zColl;.        p
2059c 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69  Coll = sqlite3Fi
2059d 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e  ndCollSeq(db, EN
2059e 43 28 64 62 29 2c 20 7a 43 6f 6c 6c 2c 20 30 29  C(db), zColl, 0)
2059f 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d  ;.        pExpr-
205a0 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a  >pColl = pColl;.
205a1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
205a2 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  eak;.    }.    i
205a3 66 28 20 6f 70 21 3d 54 4b 5f 43 41 53 54 20 26  f( op!=TK_CAST &
205a4 26 20 6f 70 21 3d 54 4b 5f 55 50 4c 55 53 20 29  & op!=TK_UPLUS )
205a5 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  {.      break;. 
205a6 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e     }.    p = p->
205a7 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 69 66 28  pLeft;.  }.  if(
205a8 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c   sqlite3CheckCol
205a9 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 43 6f  lSeq(pParse, pCo
205aa 6c 6c 29 20 29 7b 20 0a 20 20 20 20 70 43 6f 6c  ll) ){ .    pCol
205ab 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  l = 0;.  }.  ret
205ac 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a  urn pColl;.}../*
205ad 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 6e 20  .** pExpr is an 
205ae 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 63 6f 6d  operand of a com
205af 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72  parison operator
205b0 2e 20 20 61 66 66 32 20 69 73 20 74 68 65 0a 2a  .  aff2 is the.*
205b1 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79 20  * type affinity 
205b2 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6f 70 65  of the other ope
205b3 72 61 6e 64 2e 20 20 54 68 69 73 20 72 6f 75 74  rand.  This rout
205b4 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a  ine returns the.
205b5 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79  ** type affinity
205b6 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
205b7 75 73 65 64 20 66 6f 72 20 74 68 65 20 63 6f 6d  used for the com
205b8 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72  parison operator
205b9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
205ba 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33  ATE char sqlite3
205bb 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28  CompareAffinity(
205bc 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61  Expr *pExpr, cha
205bd 72 20 61 66 66 32 29 7b 0a 20 20 63 68 61 72 20  r aff2){.  char 
205be 61 66 66 31 20 3d 20 73 71 6c 69 74 65 33 45 78  aff1 = sqlite3Ex
205bf 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72  prAffinity(pExpr
205c0 29 3b 0a 20 20 69 66 28 20 61 66 66 31 20 26 26  );.  if( aff1 &&
205c1 20 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a 20   aff2 ){.    /* 
205c2 42 6f 74 68 20 73 69 64 65 73 20 6f 66 20 74 68  Both sides of th
205c3 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 72 65  e comparison are
205c4 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 6f 6e 65   columns. If one
205c5 20 68 61 73 20 6e 75 6d 65 72 69 63 0a 20 20 20   has numeric.   
205c6 20 2a 2a 20 61 66 66 69 6e 69 74 79 2c 20 75 73   ** affinity, us
205c7 65 20 74 68 61 74 2e 20 4f 74 68 65 72 77 69 73  e that. Otherwis
205c8 65 20 75 73 65 20 6e 6f 20 61 66 66 69 6e 69 74  e use no affinit
205c9 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  y..    */.    if
205ca 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72  ( sqlite3IsNumer
205cb 69 63 41 66 66 69 6e 69 74 79 28 61 66 66 31 29  icAffinity(aff1)
205cc 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 4e 75 6d   || sqlite3IsNum
205cd 65 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 66  ericAffinity(aff
205ce 32 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  2) ){.      retu
205cf 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55  rn SQLITE_AFF_NU
205d0 4d 45 52 49 43 3b 0a 20 20 20 20 7d 65 6c 73 65  MERIC;.    }else
205d1 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
205d2 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a  QLITE_AFF_NONE;.
205d3 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
205d4 28 20 21 61 66 66 31 20 26 26 20 21 61 66 66 32  ( !aff1 && !aff2
205d5 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74 68   ){.    /* Neith
205d6 65 72 20 73 69 64 65 20 6f 66 20 74 68 65 20 63  er side of the c
205d7 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20 63  omparison is a c
205d8 6f 6c 75 6d 6e 2e 20 20 43 6f 6d 70 61 72 65 20  olumn.  Compare 
205d9 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 73 75 6c  the.    ** resul
205da 74 73 20 64 69 72 65 63 74 6c 79 2e 0a 20 20 20  ts directly..   
205db 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53   */.    return S
205dc 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a  QLITE_AFF_NONE;.
205dd 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
205de 4f 6e 65 20 73 69 64 65 20 69 73 20 61 20 63 6f  One side is a co
205df 6c 75 6d 6e 2c 20 74 68 65 20 6f 74 68 65 72 20  lumn, the other 
205e0 69 73 20 6e 6f 74 2e 20 55 73 65 20 74 68 65 20  is not. Use the 
205e1 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74 79  columns affinity
205e2 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  . */.    assert(
205e3 20 61 66 66 31 3d 3d 30 20 7c 7c 20 61 66 66 32   aff1==0 || aff2
205e4 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72  ==0 );.    retur
205e5 6e 20 28 61 66 66 31 20 2b 20 61 66 66 32 29 3b  n (aff1 + aff2);
205e6 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45  .  }.}../*.** pE
205e7 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72 69  xpr is a compari
205e8 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20 52  son operator.  R
205e9 65 74 75 72 6e 20 74 68 65 20 74 79 70 65 20 61  eturn the type a
205ea 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f  ffinity that sho
205eb 75 6c 64 0a 2a 2a 20 62 65 20 61 70 70 6c 69 65  uld.** be applie
205ec 64 20 74 6f 20 62 6f 74 68 20 6f 70 65 72 61 6e  d to both operan
205ed 64 73 20 70 72 69 6f 72 20 74 6f 20 64 6f 69 6e  ds prior to doin
205ee 67 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  g the comparison
205ef 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72  ..*/.static char
205f0 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e   comparisonAffin
205f1 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 29  ity(Expr *pExpr)
205f2 7b 0a 20 20 63 68 61 72 20 61 66 66 3b 0a 20 20  {.  char aff;.  
205f3 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f  assert( pExpr->o
205f4 70 3d 3d 54 4b 5f 45 51 20 7c 7c 20 70 45 78 70  p==TK_EQ || pExp
205f5 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c 20  r->op==TK_IN || 
205f6 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 54  pExpr->op==TK_LT
205f7 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 70 45   ||.          pE
205f8 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20 7c  xpr->op==TK_GT |
205f9 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  | pExpr->op==TK_
205fa 47 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d  GE || pExpr->op=
205fb 3d 54 4b 5f 4c 45 20 7c 7c 0a 20 20 20 20 20 20  =TK_LE ||.      
205fc 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54      pExpr->op==T
205fd 4b 5f 4e 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f  K_NE || pExpr->o
205fe 70 3d 3d 54 4b 5f 49 53 20 7c 7c 20 70 45 78 70  p==TK_IS || pExp
205ff 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 4e 4f 54 20  r->op==TK_ISNOT 
20600 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78  );.  assert( pEx
20601 70 72 2d 3e 70 4c 65 66 74 20 29 3b 0a 20 20 61  pr->pLeft );.  a
20602 66 66 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ff = sqlite3Expr
20603 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e  Affinity(pExpr->
20604 70 4c 65 66 74 29 3b 0a 20 20 69 66 28 20 70 45  pLeft);.  if( pE
20605 78 70 72 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20  xpr->pRight ){. 
20606 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33     aff = sqlite3
20607 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28  CompareAffinity(
20608 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 61  pExpr->pRight, a
20609 66 66 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ff);.  }else if(
2060a 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
2060b 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65  (pExpr, EP_xIsSe
2060c 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 61 66 66  lect) ){.    aff
2060d 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72   = sqlite3Compar
2060e 65 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d  eAffinity(pExpr-
2060f 3e 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69  >x.pSelect->pELi
20610 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20  st->a[0].pExpr, 
20611 61 66 66 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  aff);.  }else if
20612 28 20 21 61 66 66 20 29 7b 0a 20 20 20 20 61 66  ( !aff ){.    af
20613 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  f = SQLITE_AFF_N
20614 4f 4e 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ONE;.  }.  retur
20615 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n aff;.}../*.** 
20616 70 45 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61  pExpr is a compa
20617 72 69 73 6f 6e 20 65 78 70 72 65 73 73 69 6f 6e  rison expression
20618 2c 20 65 67 2e 20 27 3d 27 2c 20 27 3c 27 2c 20  , eg. '=', '<', 
20619 49 4e 28 2e 2e 2e 29 20 65 74 63 2e 0a 2a 2a 20  IN(...) etc..** 
2061a 69 64 78 5f 61 66 66 69 6e 69 74 79 20 69 73 20  idx_affinity is 
2061b 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20  the affinity of 
2061c 61 6e 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d  an indexed colum
2061d 6e 2e 20 52 65 74 75 72 6e 20 74 72 75 65 0a 2a  n. Return true.*
2061e 2a 20 69 66 20 74 68 65 20 69 6e 64 65 78 20 77  * if the index w
2061f 69 74 68 20 61 66 66 69 6e 69 74 79 20 69 64 78  ith affinity idx
20620 5f 61 66 66 69 6e 69 74 79 20 6d 61 79 20 62 65  _affinity may be
20621 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
20622 6e 74 0a 2a 2a 20 74 68 65 20 63 6f 6d 70 61 72  nt.** the compar
20623 69 73 6f 6e 20 69 6e 20 70 45 78 70 72 2e 0a 2a  ison in pExpr..*
20624 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
20625 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65   int sqlite3Inde
20626 78 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70 72  xAffinityOk(Expr
20627 20 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69 64   *pExpr, char id
20628 78 5f 61 66 66 69 6e 69 74 79 29 7b 0a 20 20 63  x_affinity){.  c
20629 68 61 72 20 61 66 66 20 3d 20 63 6f 6d 70 61 72  har aff = compar
2062a 69 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 78  isonAffinity(pEx
2062b 70 72 29 3b 0a 20 20 73 77 69 74 63 68 28 20 61  pr);.  switch( a
2062c 66 66 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  ff ){.    case S
2062d 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a 0a  QLITE_AFF_NONE:.
2062e 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
2062f 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
20630 41 46 46 5f 54 45 58 54 3a 0a 20 20 20 20 20 20  AFF_TEXT:.      
20631 72 65 74 75 72 6e 20 69 64 78 5f 61 66 66 69 6e  return idx_affin
20632 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  ity==SQLITE_AFF_
20633 54 45 58 54 3b 0a 20 20 20 20 64 65 66 61 75 6c  TEXT;.    defaul
20634 74 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  t:.      return 
20635 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63  sqlite3IsNumeric
20636 41 66 66 69 6e 69 74 79 28 69 64 78 5f 61 66 66  Affinity(idx_aff
20637 69 6e 69 74 79 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  inity);.  }.}../
20638 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
20639 50 35 20 76 61 6c 75 65 20 74 68 61 74 20 73 68  P5 value that sh
2063a 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72  ould be used for
2063b 20 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61 72   a binary compar
2063c 69 73 6f 6e 0a 2a 2a 20 6f 70 63 6f 64 65 20 28  ison.** opcode (
2063d 4f 50 5f 45 71 2c 20 4f 50 5f 47 65 20 65 74 63  OP_Eq, OP_Ge etc
2063e 2e 29 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61  .) used to compa
2063f 72 65 20 70 45 78 70 72 31 20 61 6e 64 20 70 45  re pExpr1 and pE
20640 78 70 72 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  xpr2..*/.static 
20641 75 38 20 62 69 6e 61 72 79 43 6f 6d 70 61 72 65  u8 binaryCompare
20642 50 35 28 45 78 70 72 20 2a 70 45 78 70 72 31 2c  P5(Expr *pExpr1,
20643 20 45 78 70 72 20 2a 70 45 78 70 72 32 2c 20 69   Expr *pExpr2, i
20644 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a  nt jumpIfNull){.
20645 20 20 75 38 20 61 66 66 20 3d 20 28 63 68 61 72    u8 aff = (char
20646 29 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69  )sqlite3ExprAffi
20647 6e 69 74 79 28 70 45 78 70 72 32 29 3b 0a 20 20  nity(pExpr2);.  
20648 61 66 66 20 3d 20 28 75 38 29 73 71 6c 69 74 65  aff = (u8)sqlite
20649 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79  3CompareAffinity
2064a 28 70 45 78 70 72 31 2c 20 61 66 66 29 20 7c 20  (pExpr1, aff) | 
2064b 28 75 38 29 6a 75 6d 70 49 66 4e 75 6c 6c 3b 0a  (u8)jumpIfNull;.
2064c 20 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a    return aff;.}.
2064d 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
2064e 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63  pointer to the c
2064f 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
20650 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65  e that should be
20651 20 75 73 65 64 20 62 79 0a 2a 2a 20 61 20 62 69   used by.** a bi
20652 6e 61 72 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20  nary comparison 
20653 6f 70 65 72 61 74 6f 72 20 63 6f 6d 70 61 72 69  operator compari
20654 6e 67 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69  ng pLeft and pRi
20655 67 68 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ght..**.** If th
20656 65 20 6c 65 66 74 20 68 61 6e 64 20 65 78 70 72  e left hand expr
20657 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c  ession has a col
20658 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
20659 74 79 70 65 2c 20 74 68 65 6e 20 69 74 20 69 73  type, then it is
2065a 0a 2a 2a 20 75 73 65 64 2e 20 4f 74 68 65 72 77  .** used. Otherw
2065b 69 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  ise the collatio
2065c 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74  n sequence for t
2065d 68 65 20 72 69 67 68 74 20 68 61 6e 64 20 65 78  he right hand ex
2065e 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 75  pression.** is u
2065f 73 65 64 2c 20 6f 72 20 74 68 65 20 64 65 66 61  sed, or the defa
20660 75 6c 74 20 28 42 49 4e 41 52 59 29 20 69 66 20  ult (BINARY) if 
20661 6e 65 69 74 68 65 72 20 65 78 70 72 65 73 73 69  neither expressi
20662 6f 6e 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 69  on has a collati
20663 6e 67 0a 2a 2a 20 74 79 70 65 2e 0a 2a 2a 0a 2a  ng.** type..**.*
20664 2a 20 41 72 67 75 6d 65 6e 74 20 70 52 69 67 68  * Argument pRigh
20665 74 20 28 62 75 74 20 6e 6f 74 20 70 4c 65 66 74  t (but not pLeft
20666 29 20 6d 61 79 20 62 65 20 61 20 6e 75 6c 6c 20  ) may be a null 
20667 70 6f 69 6e 74 65 72 2e 20 49 6e 20 74 68 69 73  pointer. In this
20668 20 63 61 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20   case,.** it is 
20669 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a  not considered..
2066a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2066b 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74  E CollSeq *sqlit
2066c 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43  e3BinaryCompareC
2066d 6f 6c 6c 53 65 71 28 0a 20 20 50 61 72 73 65 20  ollSeq(.  Parse 
2066e 2a 70 50 61 72 73 65 2c 20 0a 20 20 45 78 70 72  *pParse, .  Expr
2066f 20 2a 70 4c 65 66 74 2c 20 0a 20 20 45 78 70 72   *pLeft, .  Expr
20670 20 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20 43 6f   *pRight.){.  Co
20671 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20  llSeq *pColl;.  
20672 61 73 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b  assert( pLeft );
20673 0a 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 66 6c  .  if( pLeft->fl
20674 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c  ags & EP_ExpColl
20675 61 74 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ate ){.    asser
20676 74 28 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20  t( pLeft->pColl 
20677 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70  );.    pColl = p
20678 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d  Left->pColl;.  }
20679 65 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 20  else if( pRight 
2067a 26 26 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73  && pRight->flags
2067b 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65   & EP_ExpCollate
2067c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
2067d 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 20 29 3b  pRight->pColl );
2067e 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 52 69  .    pColl = pRi
2067f 67 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65  ght->pColl;.  }e
20680 6c 73 65 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d  lse{.    pColl =
20681 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c   sqlite3ExprColl
20682 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66  Seq(pParse, pLef
20683 74 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f  t);.    if( !pCo
20684 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c  ll ){.      pCol
20685 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  l = sqlite3ExprC
20686 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
20687 52 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20  Right);.    }.  
20688 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c  }.  return pColl
20689 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
2068a 61 74 65 20 74 68 65 20 6f 70 65 72 61 6e 64 73  ate the operands
2068b 20 66 6f 72 20 61 20 63 6f 6d 70 61 72 69 73 6f   for a compariso
2068c 6e 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 65  n operation.  Be
2068d 66 6f 72 65 0a 2a 2a 20 67 65 6e 65 72 61 74 69  fore.** generati
2068e 6e 67 20 74 68 65 20 63 6f 64 65 20 66 6f 72 20  ng the code for 
2068f 65 61 63 68 20 6f 70 65 72 61 6e 64 2c 20 73 65  each operand, se
20690 74 20 74 68 65 20 45 50 5f 41 6e 79 41 66 66 0a  t the EP_AnyAff.
20691 2a 2a 20 66 6c 61 67 20 6f 6e 20 74 68 65 20 65  ** flag on the e
20692 78 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68 61  xpression so tha
20693 74 20 69 74 20 77 69 6c 6c 20 62 65 20 61 62 6c  t it will be abl
20694 65 20 74 6f 20 75 73 65 64 20 61 0a 2a 2a 20 63  e to used a.** c
20695 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20 76 61 6c  ached column val
20696 75 65 20 74 68 61 74 20 68 61 73 20 70 72 65 76  ue that has prev
20697 69 6f 75 73 6c 79 20 75 6e 64 65 72 67 6f 6e 65  iously undergone
20698 20 61 6e 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20   an.** affinity 
20699 63 68 61 6e 67 65 2e 0a 2a 2f 0a 73 74 61 74 69  change..*/.stati
2069a 63 20 76 6f 69 64 20 63 6f 64 65 43 6f 6d 70 61  c void codeCompa
2069b 72 65 4f 70 65 72 61 6e 64 73 28 0a 20 20 50 61  reOperands(.  Pa
2069c 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2069d 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63  /* Parsing and c
2069e 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63  ode generating c
2069f 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72  ontext */.  Expr
206a0 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a   *pLeft,      /*
206a1 20 54 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e   The left operan
206a2 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 67  d */.  int *pReg
206a3 4c 65 66 74 2c 20 20 20 20 2f 2a 20 52 65 67 69  Left,    /* Regi
206a4 73 74 65 72 20 77 68 65 72 65 20 6c 65 66 74 20  ster where left 
206a5 6f 70 65 72 61 6e 64 20 69 73 20 73 74 6f 72 65  operand is store
206a6 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 46 72 65  d */.  int *pFre
206a7 65 4c 65 66 74 2c 20 20 20 2f 2a 20 46 72 65 65  eLeft,   /* Free
206a8 20 74 68 69 73 20 72 65 67 69 73 74 65 72 20 77   this register w
206a9 68 65 6e 20 64 6f 6e 65 20 2a 2f 0a 20 20 45 78  hen done */.  Ex
206aa 70 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20  pr *pRight,     
206ab 2f 2a 20 54 68 65 20 72 69 67 68 74 20 6f 70 65  /* The right ope
206ac 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  rand */.  int *p
206ad 52 65 67 52 69 67 68 74 2c 20 20 20 2f 2a 20 52  RegRight,   /* R
206ae 65 67 69 73 74 65 72 20 77 68 65 72 65 20 72 69  egister where ri
206af 67 68 74 20 6f 70 65 72 61 6e 64 20 69 73 20 73  ght operand is s
206b0 74 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a  tored */.  int *
206b1 70 46 72 65 65 52 69 67 68 74 20 20 20 2f 2a 20  pFreeRight   /* 
206b2 57 72 69 74 65 20 74 65 6d 70 20 72 65 67 69 73  Write temp regis
206b3 74 65 72 20 66 6f 72 20 72 69 67 68 74 20 6f 70  ter for right op
206b4 65 72 61 6e 64 20 74 68 65 72 65 20 2a 2f 0a 29  erand there */.)
206b5 7b 0a 20 20 77 68 69 6c 65 28 20 70 4c 65 66 74  {.  while( pLeft
206b6 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29  ->op==TK_UPLUS )
206b7 20 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 2d 3e   pLeft = pLeft->
206b8 70 4c 65 66 74 3b 0a 20 20 70 4c 65 66 74 2d 3e  pLeft;.  pLeft->
206b9 66 6c 61 67 73 20 7c 3d 20 45 50 5f 41 6e 79 41  flags |= EP_AnyA
206ba 66 66 3b 0a 20 20 2a 70 52 65 67 4c 65 66 74 20  ff;.  *pRegLeft 
206bb 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
206bc 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 4c  eTemp(pParse, pL
206bd 65 66 74 2c 20 70 46 72 65 65 4c 65 66 74 29 3b  eft, pFreeLeft);
206be 0a 20 20 77 68 69 6c 65 28 20 70 52 69 67 68 74  .  while( pRight
206bf 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29  ->op==TK_UPLUS )
206c0 20 70 52 69 67 68 74 20 3d 20 70 52 69 67 68 74   pRight = pRight
206c1 2d 3e 70 4c 65 66 74 3b 0a 20 20 70 52 69 67 68  ->pLeft;.  pRigh
206c2 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 41  t->flags |= EP_A
206c3 6e 79 41 66 66 3b 0a 20 20 2a 70 52 65 67 52 69  nyAff;.  *pRegRi
206c4 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ght = sqlite3Exp
206c5 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65  rCodeTemp(pParse
206c6 2c 20 70 52 69 67 68 74 2c 20 70 46 72 65 65 52  , pRight, pFreeR
206c7 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ight);.}../*.** 
206c8 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
206c9 72 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f  r a comparison o
206ca 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74  perator..*/.stat
206cb 69 63 20 69 6e 74 20 63 6f 64 65 43 6f 6d 70 61  ic int codeCompa
206cc 72 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  re(.  Parse *pPa
206cd 72 73 65 2c 20 20 20 20 2f 2a 20 54 68 65 20 70  rse,    /* The p
206ce 61 72 73 69 6e 67 20 28 61 6e 64 20 63 6f 64 65  arsing (and code
206cf 20 67 65 6e 65 72 61 74 69 6e 67 29 20 63 6f 6e   generating) con
206d0 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a  text */.  Expr *
206d1 70 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 54  pLeft,      /* T
206d2 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20  he left operand 
206d3 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68  */.  Expr *pRigh
206d4 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69  t,     /* The ri
206d5 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  ght operand */. 
206d6 20 69 6e 74 20 6f 70 63 6f 64 65 2c 20 20 20 20   int opcode,    
206d7 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 61 72     /* The compar
206d8 69 73 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20  ison opcode */. 
206d9 20 69 6e 74 20 69 6e 31 2c 20 69 6e 74 20 69 6e   int in1, int in
206da 32 2c 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68  2, /* Register h
206db 6f 6c 64 69 6e 67 20 6f 70 65 72 61 6e 64 73 20  olding operands 
206dc 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74 2c 20 20  */.  int dest,  
206dd 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68         /* Jump h
206de 65 72 65 20 69 66 20 74 72 75 65 2e 20 20 2a 2f  ere if true.  */
206df 0a 20 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c  .  int jumpIfNul
206e0 6c 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c  l    /* If true,
206e1 20 6a 75 6d 70 20 69 66 20 65 69 74 68 65 72 20   jump if either 
206e2 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20  operand is NULL 
206e3 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 35 3b 0a  */.){.  int p5;.
206e4 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 43 6f    int addr;.  Co
206e5 6c 6c 53 65 71 20 2a 70 34 3b 0a 0a 20 20 70 34  llSeq *p4;..  p4
206e6 20 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79   = sqlite3Binary
206e7 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70  CompareCollSeq(p
206e8 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52  Parse, pLeft, pR
206e9 69 67 68 74 29 3b 0a 20 20 70 35 20 3d 20 62 69  ight);.  p5 = bi
206ea 6e 61 72 79 43 6f 6d 70 61 72 65 50 35 28 70 4c  naryCompareP5(pL
206eb 65 66 74 2c 20 70 52 69 67 68 74 2c 20 6a 75 6d  eft, pRight, jum
206ec 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 61 64 64 72  pIfNull);.  addr
206ed 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
206ee 64 4f 70 34 28 70 50 61 72 73 65 2d 3e 70 56 64  dOp4(pParse->pVd
206ef 62 65 2c 20 6f 70 63 6f 64 65 2c 20 69 6e 32 2c  be, opcode, in2,
206f0 20 64 65 73 74 2c 20 69 6e 31 2c 0a 20 20 20 20   dest, in1,.    
206f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
206f2 20 20 20 20 20 20 20 28 76 6f 69 64 2a 29 70 34         (void*)p4
206f3 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20  , P4_COLLSEQ);. 
206f4 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
206f5 67 65 50 35 28 70 50 61 72 73 65 2d 3e 70 56 64  geP5(pParse->pVd
206f6 62 65 2c 20 28 75 38 29 70 35 29 3b 0a 20 20 69  be, (u8)p5);.  i
206f7 66 28 20 28 70 35 20 26 20 53 51 4c 49 54 45 5f  f( (p5 & SQLITE_
206f8 41 46 46 5f 4d 41 53 4b 29 21 3d 53 51 4c 49 54  AFF_MASK)!=SQLIT
206f9 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20  E_AFF_NONE ){.  
206fa 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
206fb 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
206fc 28 70 50 61 72 73 65 2c 20 69 6e 31 2c 20 31 29  (pParse, in1, 1)
206fd 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
206fe 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
206ff 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e 32  ange(pParse, in2
20700 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , 1);.  }.  retu
20701 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 23 69 66 20  rn addr;.}..#if 
20702 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f  SQLITE_MAX_EXPR_
20703 44 45 50 54 48 3e 30 0a 2f 2a 0a 2a 2a 20 43 68  DEPTH>0./*.** Ch
20704 65 63 6b 20 74 68 61 74 20 61 72 67 75 6d 65 6e  eck that argumen
20705 74 20 6e 48 65 69 67 68 74 20 69 73 20 6c 65 73  t nHeight is les
20706 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  s than or equal 
20707 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a  to the maximum.*
20708 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 64 65 70  * expression dep
20709 74 68 20 61 6c 6c 6f 77 65 64 2e 20 49 66 20 69  th allowed. If i
2070a 74 20 69 73 20 6e 6f 74 2c 20 6c 65 61 76 65 20  t is not, leave 
2070b 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
2070c 20 69 6e 0a 2a 2a 20 70 50 61 72 73 65 2e 0a 2a   in.** pParse..*
2070d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2070e 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
2070f 43 68 65 63 6b 48 65 69 67 68 74 28 50 61 72 73  CheckHeight(Pars
20710 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6e  e *pParse, int n
20711 48 65 69 67 68 74 29 7b 0a 20 20 69 6e 74 20 72  Height){.  int r
20712 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
20713 20 69 6e 74 20 6d 78 48 65 69 67 68 74 20 3d 20   int mxHeight = 
20714 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d  pParse->db->aLim
20715 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
20716 45 58 50 52 5f 44 45 50 54 48 5d 3b 0a 20 20 69  EXPR_DEPTH];.  i
20717 66 28 20 6e 48 65 69 67 68 74 3e 6d 78 48 65 69  f( nHeight>mxHei
20718 67 68 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ght ){.    sqlit
20719 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2071a 65 2c 20 0a 20 20 20 20 20 20 20 22 45 78 70 72  e, .       "Expr
2071b 65 73 73 69 6f 6e 20 74 72 65 65 20 69 73 20 74  ession tree is t
2071c 6f 6f 20 6c 61 72 67 65 20 28 6d 61 78 69 6d 75  oo large (maximu
2071d 6d 20 64 65 70 74 68 20 25 64 29 22 2c 20 6d 78  m depth %d)", mx
2071e 48 65 69 67 68 74 0a 20 20 20 20 29 3b 0a 20 20  Height.    );.  
2071f 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
20720 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ROR;.  }.  retur
20721 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20  n rc;.}../* The 
20722 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20  following three 
20723 66 75 6e 63 74 69 6f 6e 73 2c 20 68 65 69 67 68  functions, heigh
20724 74 4f 66 45 78 70 72 28 29 2c 20 68 65 69 67 68  tOfExpr(), heigh
20725 74 4f 66 45 78 70 72 4c 69 73 74 28 29 0a 2a 2a  tOfExprList().**
20726 20 61 6e 64 20 68 65 69 67 68 74 4f 66 53 65 6c   and heightOfSel
20727 65 63 74 28 29 2c 20 61 72 65 20 75 73 65 64 20  ect(), are used 
20728 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  to determine the
20729 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 0a   maximum height.
2072a 2a 2a 20 6f 66 20 61 6e 79 20 65 78 70 72 65 73  ** of any expres
2072b 73 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72 65  sion tree refere
2072c 6e 63 65 64 20 62 79 20 74 68 65 20 73 74 72 75  nced by the stru
2072d 63 74 75 72 65 20 70 61 73 73 65 64 20 61 73 20  cture passed as 
2072e 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 61 72 67  the.** first arg
2072f 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ument..**.** If 
20730 74 68 69 73 20 6d 61 78 69 6d 75 6d 20 68 65 69  this maximum hei
20731 67 68 74 20 69 73 20 67 72 65 61 74 65 72 20 74  ght is greater t
20732 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20  han the current 
20733 76 61 6c 75 65 20 70 6f 69 6e 74 65 64 0a 2a 2a  value pointed.**
20734 20 74 6f 20 62 79 20 70 6e 48 65 69 67 68 74 2c   to by pnHeight,
20735 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   the second para
20736 6d 65 74 65 72 2c 20 74 68 65 6e 20 73 65 74 20  meter, then set 
20737 2a 70 6e 48 65 69 67 68 74 20 74 6f 20 74 68 61  *pnHeight to tha
20738 74 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73  t.** value..*/.s
20739 74 61 74 69 63 20 76 6f 69 64 20 68 65 69 67 68  tatic void heigh
2073a 74 4f 66 45 78 70 72 28 45 78 70 72 20 2a 70 2c  tOfExpr(Expr *p,
2073b 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 7b   int *pnHeight){
2073c 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
2073d 69 66 28 20 70 2d 3e 6e 48 65 69 67 68 74 3e 2a  if( p->nHeight>*
2073e 70 6e 48 65 69 67 68 74 20 29 7b 0a 20 20 20 20  pnHeight ){.    
2073f 20 20 2a 70 6e 48 65 69 67 68 74 20 3d 20 70 2d    *pnHeight = p-
20740 3e 6e 48 65 69 67 68 74 3b 0a 20 20 20 20 7d 0a  >nHeight;.    }.
20741 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69    }.}.static voi
20742 64 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69  d heightOfExprLi
20743 73 74 28 45 78 70 72 4c 69 73 74 20 2a 70 2c 20  st(ExprList *p, 
20744 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a  int *pnHeight){.
20745 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69    if( p ){.    i
20746 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
20747 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 72 3b 20 69  0; i<p->nExpr; i
20748 2b 2b 29 7b 0a 20 20 20 20 20 20 68 65 69 67 68  ++){.      heigh
20749 74 4f 66 45 78 70 72 28 70 2d 3e 61 5b 69 5d 2e  tOfExpr(p->a[i].
2074a 70 45 78 70 72 2c 20 70 6e 48 65 69 67 68 74 29  pExpr, pnHeight)
2074b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74  ;.    }.  }.}.st
2074c 61 74 69 63 20 76 6f 69 64 20 68 65 69 67 68 74  atic void height
2074d 4f 66 53 65 6c 65 63 74 28 53 65 6c 65 63 74 20  OfSelect(Select 
2074e 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68  *p, int *pnHeigh
2074f 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  t){.  if( p ){. 
20750 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28     heightOfExpr(
20751 70 2d 3e 70 57 68 65 72 65 2c 20 70 6e 48 65 69  p->pWhere, pnHei
20752 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74  ght);.    height
20753 4f 66 45 78 70 72 28 70 2d 3e 70 48 61 76 69 6e  OfExpr(p->pHavin
20754 67 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20  g, pnHeight);.  
20755 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70    heightOfExpr(p
20756 2d 3e 70 4c 69 6d 69 74 2c 20 70 6e 48 65 69 67  ->pLimit, pnHeig
20757 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f  ht);.    heightO
20758 66 45 78 70 72 28 70 2d 3e 70 4f 66 66 73 65 74  fExpr(p->pOffset
20759 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20  , pnHeight);.   
2075a 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73   heightOfExprLis
2075b 74 28 70 2d 3e 70 45 4c 69 73 74 2c 20 70 6e 48  t(p->pEList, pnH
2075c 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67  eight);.    heig
2075d 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e  htOfExprList(p->
2075e 70 47 72 6f 75 70 42 79 2c 20 70 6e 48 65 69 67  pGroupBy, pnHeig
2075f 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f  ht);.    heightO
20760 66 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 4f 72  fExprList(p->pOr
20761 64 65 72 42 79 2c 20 70 6e 48 65 69 67 68 74 29  derBy, pnHeight)
20762 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 53 65  ;.    heightOfSe
20763 6c 65 63 74 28 70 2d 3e 70 50 72 69 6f 72 2c 20  lect(p->pPrior, 
20764 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 7d  pnHeight);.  }.}
20765 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
20766 45 78 70 72 2e 6e 48 65 69 67 68 74 20 76 61 72  Expr.nHeight var
20767 69 61 62 6c 65 20 69 6e 20 74 68 65 20 73 74 72  iable in the str
20768 75 63 74 75 72 65 20 70 61 73 73 65 64 20 61 73  ucture passed as
20769 20 61 6e 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74   an .** argument
2076a 2e 20 41 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  . An expression 
2076b 77 69 74 68 20 6e 6f 20 63 68 69 6c 64 72 65 6e  with no children
2076c 2c 20 45 78 70 72 2e 70 4c 69 73 74 20 6f 72 20  , Expr.pList or 
2076d 0a 2a 2a 20 45 78 70 72 2e 70 53 65 6c 65 63 74  .** Expr.pSelect
2076e 20 6d 65 6d 62 65 72 20 68 61 73 20 61 20 68 65   member has a he
2076f 69 67 68 74 20 6f 66 20 31 2e 20 41 6e 79 20 6f  ight of 1. Any o
20770 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 0a  ther expression.
20771 2a 2a 20 68 61 73 20 61 20 68 65 69 67 68 74 20  ** has a height 
20772 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 78  equal to the max
20773 69 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 20 61  imum height of a
20774 6e 79 20 6f 74 68 65 72 20 0a 2a 2a 20 72 65 66  ny other .** ref
20775 65 72 65 6e 63 65 64 20 45 78 70 72 20 70 6c 75  erenced Expr plu
20776 73 20 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  s one..*/.static
20777 20 76 6f 69 64 20 65 78 70 72 53 65 74 48 65 69   void exprSetHei
20778 67 68 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 20  ght(Expr *p){.  
20779 69 6e 74 20 6e 48 65 69 67 68 74 20 3d 20 30 3b  int nHeight = 0;
2077a 0a 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28  .  heightOfExpr(
2077b 70 2d 3e 70 4c 65 66 74 2c 20 26 6e 48 65 69 67  p->pLeft, &nHeig
2077c 68 74 29 3b 0a 20 20 68 65 69 67 68 74 4f 66 45  ht);.  heightOfE
2077d 78 70 72 28 70 2d 3e 70 52 69 67 68 74 2c 20 26  xpr(p->pRight, &
2077e 6e 48 65 69 67 68 74 29 3b 0a 20 20 69 66 28 20  nHeight);.  if( 
2077f 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
20780 70 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  p, EP_xIsSelect)
20781 20 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f 66   ){.    heightOf
20782 53 65 6c 65 63 74 28 70 2d 3e 78 2e 70 53 65 6c  Select(p->x.pSel
20783 65 63 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a  ect, &nHeight);.
20784 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 65 69    }else{.    hei
20785 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d  ghtOfExprList(p-
20786 3e 78 2e 70 4c 69 73 74 2c 20 26 6e 48 65 69 67  >x.pList, &nHeig
20787 68 74 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e 48  ht);.  }.  p->nH
20788 65 69 67 68 74 20 3d 20 6e 48 65 69 67 68 74 20  eight = nHeight 
20789 2b 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  + 1;.}../*.** Se
2078a 74 20 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67  t the Expr.nHeig
2078b 68 74 20 76 61 72 69 61 62 6c 65 20 75 73 69 6e  ht variable usin
2078c 67 20 74 68 65 20 65 78 70 72 53 65 74 48 65 69  g the exprSetHei
2078d 67 68 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20  ght() function. 
2078e 49 66 0a 2a 2a 20 74 68 65 20 68 65 69 67 68 74  If.** the height
2078f 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
20790 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c   the maximum all
20791 6f 77 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20  owed expression 
20792 64 65 70 74 68 2c 0a 2a 2a 20 6c 65 61 76 65 20  depth,.** leave 
20793 61 6e 20 65 72 72 6f 72 20 69 6e 20 70 50 61 72  an error in pPar
20794 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
20795 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
20796 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28  e3ExprSetHeight(
20797 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
20798 78 70 72 20 2a 70 29 7b 0a 20 20 65 78 70 72 53  xpr *p){.  exprS
20799 65 74 48 65 69 67 68 74 28 70 29 3b 0a 20 20 73  etHeight(p);.  s
2079a 71 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48  qlite3ExprCheckH
2079b 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 2d  eight(pParse, p-
2079c 3e 6e 48 65 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a  >nHeight);.}../*
2079d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d  .** Return the m
2079e 61 78 69 6d 75 6d 20 68 65 69 67 68 74 20 6f 66  aximum height of
2079f 20 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20   any expression 
207a0 74 72 65 65 20 72 65 66 65 72 65 6e 63 65 64 0a  tree referenced.
207a1 2a 2a 20 62 79 20 74 68 65 20 73 65 6c 65 63 74  ** by the select
207a2 20 73 74 61 74 65 6d 65 6e 74 20 70 61 73 73 65   statement passe
207a3 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  d as an argument
207a4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
207a5 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53  ATE int sqlite3S
207a6 65 6c 65 63 74 45 78 70 72 48 65 69 67 68 74 28  electExprHeight(
207a7 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69 6e  Select *p){.  in
207a8 74 20 6e 48 65 69 67 68 74 20 3d 20 30 3b 0a 20  t nHeight = 0;. 
207a9 20 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28   heightOfSelect(
207aa 70 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20  p, &nHeight);.  
207ab 72 65 74 75 72 6e 20 6e 48 65 69 67 68 74 3b 0a  return nHeight;.
207ac 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e  }.#else.  #defin
207ad 65 20 65 78 70 72 53 65 74 48 65 69 67 68 74 28  e exprSetHeight(
207ae 79 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  y).#endif /* SQL
207af 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50  ITE_MAX_EXPR_DEP
207b0 54 48 3e 30 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  TH>0 */../*.** T
207b1 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74  his routine is t
207b2 68 65 20 63 6f 72 65 20 61 6c 6c 6f 63 61 74 6f  he core allocato
207b3 72 20 66 6f 72 20 45 78 70 72 20 6e 6f 64 65 73  r for Expr nodes
207b4 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63  ..**.** Construc
207b5 74 20 61 20 6e 65 77 20 65 78 70 72 65 73 73 69  t a new expressi
207b6 6f 6e 20 6e 6f 64 65 20 61 6e 64 20 72 65 74 75  on node and retu
207b7 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
207b8 69 74 2e 20 20 4d 65 6d 6f 72 79 0a 2a 2a 20 66  it.  Memory.** f
207b9 6f 72 20 74 68 69 73 20 6e 6f 64 65 20 61 6e 64  or this node and
207ba 20 66 6f 72 20 74 68 65 20 70 54 6f 6b 65 6e 20   for the pToken 
207bb 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 73 69  argument is a si
207bc 6e 67 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a  ngle allocation.
207bd 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ** obtained from
207be 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
207bf 28 29 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67  ().  The calling
207c0 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20   function.** is 
207c1 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
207c2 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 68 65 20  making sure the 
207c3 6e 6f 64 65 20 65 76 65 6e 74 75 61 6c 6c 79 20  node eventually 
207c4 67 65 74 73 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a  gets freed..**.*
207c5 2a 20 49 66 20 64 65 71 75 6f 74 65 20 69 73 20  * If dequote is 
207c6 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 74  true, then the t
207c7 6f 6b 65 6e 20 28 69 66 20 69 74 20 65 78 69 73  oken (if it exis
207c8 74 73 29 20 69 73 20 64 65 71 75 6f 74 65 64 2e  ts) is dequoted.
207c9 0a 2a 2a 20 49 66 20 64 65 71 75 6f 74 65 20 69  .** If dequote i
207ca 73 20 66 61 6c 73 65 2c 20 6e 6f 20 64 65 71 75  s false, no dequ
207cb 6f 74 69 6e 67 20 69 73 20 70 65 72 66 6f 72 6d  oting is perform
207cc 61 6e 63 65 2e 20 20 54 68 65 20 64 65 51 75 6f  ance.  The deQuo
207cd 74 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  te.** parameter 
207ce 69 73 20 69 67 6e 6f 72 65 64 20 69 66 20 70 54  is ignored if pT
207cf 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c 20 6f 72 20  oken is NULL or 
207d0 69 66 20 74 68 65 20 74 6f 6b 65 6e 20 64 6f 65  if the token doe
207d1 73 20 6e 6f 74 0a 2a 2a 20 61 70 70 65 61 72 20  s not.** appear 
207d2 74 6f 20 62 65 20 71 75 6f 74 65 64 2e 20 20 49  to be quoted.  I
207d3 66 20 74 68 65 20 71 75 6f 74 65 73 20 77 65 72  f the quotes wer
207d4 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 2e  e of the form ".
207d5 2e 2e 22 20 28 64 6f 75 62 6c 65 2d 71 75 6f 74  .." (double-quot
207d6 65 73 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  es).** then the 
207d7 45 50 5f 44 62 6c 51 75 6f 74 65 64 20 66 6c 61  EP_DblQuoted fla
207d8 67 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65 20  g is set on the 
207d9 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 2e  expression node.
207da 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 61 6c 20 63  .**.** Special c
207db 61 73 65 3a 20 20 49 66 20 6f 70 3d 3d 54 4b 5f  ase:  If op==TK_
207dc 49 4e 54 45 47 45 52 20 61 6e 64 20 70 54 6f 6b  INTEGER and pTok
207dd 65 6e 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73  en points to a s
207de 74 72 69 6e 67 20 74 68 61 74 0a 2a 2a 20 63 61  tring that.** ca
207df 6e 20 62 65 20 74 72 61 6e 73 6c 61 74 65 64 20  n be translated 
207e0 69 6e 74 6f 20 61 20 33 32 2d 62 69 74 20 69 6e  into a 32-bit in
207e1 74 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20  teger, then the 
207e2 74 6f 6b 65 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20  token is not.** 
207e3 73 74 6f 72 65 64 20 69 6e 20 75 2e 7a 54 6f 6b  stored in u.zTok
207e4 65 6e 2e 20 20 49 6e 73 74 65 61 64 2c 20 74 68  en.  Instead, th
207e5 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73  e integer values
207e6 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69   is written.** i
207e7 6e 74 6f 20 75 2e 69 56 61 6c 75 65 20 61 6e 64  nto u.iValue and
207e8 20 74 68 65 20 45 50 5f 49 6e 74 56 61 6c 75 65   the EP_IntValue
207e9 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 20 4e   flag is set.  N
207ea 6f 20 65 78 74 72 61 20 73 74 6f 72 61 67 65 0a  o extra storage.
207eb 2a 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  ** is allocated 
207ec 74 6f 20 68 6f 6c 64 20 74 68 65 20 69 6e 74 65  to hold the inte
207ed 67 65 72 20 74 65 78 74 20 61 6e 64 20 74 68 65  ger text and the
207ee 20 64 65 71 75 6f 74 65 20 66 6c 61 67 20 69 73   dequote flag is
207ef 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c   ignored..*/.SQL
207f0 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
207f1 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c   *sqlite3ExprAll
207f2 6f 63 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  oc(.  sqlite3 *d
207f3 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b,            /*
207f4 20 48 61 6e 64 6c 65 20 66 6f 72 20 73 71 6c 69   Handle for sqli
207f5 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
207f6 29 20 28 6d 61 79 20 62 65 20 6e 75 6c 6c 29 20  ) (may be null) 
207f7 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20  */.  int op,    
207f8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
207f9 45 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64  Expression opcod
207fa 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b  e */.  const Tok
207fb 65 6e 20 2a 70 54 6f 6b 65 6e 2c 20 20 20 20 2f  en *pToken,    /
207fc 2a 20 54 6f 6b 65 6e 20 61 72 67 75 6d 65 6e 74  * Token argument
207fd 2e 20 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c  .  Might be NULL
207fe 20 2a 2f 0a 20 20 69 6e 74 20 64 65 71 75 6f 74   */.  int dequot
207ff 65 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e             /*
20800 20 54 72 75 65 20 74 6f 20 64 65 71 75 6f 74 65   True to dequote
20801 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70   */.){.  Expr *p
20802 4e 65 77 3b 0a 20 20 69 6e 74 20 6e 45 78 74 72  New;.  int nExtr
20803 61 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 56 61  a = 0;.  int iVa
20804 6c 75 65 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20  lue = 0;..  if( 
20805 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 69 66  pToken ){.    if
20806 28 20 6f 70 21 3d 54 4b 5f 49 4e 54 45 47 45 52  ( op!=TK_INTEGER
20807 20 7c 7c 20 70 54 6f 6b 65 6e 2d 3e 7a 3d 3d 30   || pToken->z==0
20808 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71  .          || sq
20809 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 70 54  lite3GetInt32(pT
2080a 6f 6b 65 6e 2d 3e 7a 2c 20 26 69 56 61 6c 75 65  oken->z, &iValue
2080b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 45  )==0 ){.      nE
2080c 78 74 72 61 20 3d 20 70 54 6f 6b 65 6e 2d 3e 6e  xtra = pToken->n
2080d 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  +1;.    }.  }.  
2080e 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  pNew = sqlite3Db
2080f 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
20810 69 7a 65 6f 66 28 45 78 70 72 29 2b 6e 45 78 74  izeof(Expr)+nExt
20811 72 61 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20  ra);.  if( pNew 
20812 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6f 70 20  ){.    pNew->op 
20813 3d 20 28 75 38 29 6f 70 3b 0a 20 20 20 20 70 4e  = (u8)op;.    pN
20814 65 77 2d 3e 69 41 67 67 20 3d 20 2d 31 3b 0a 20  ew->iAgg = -1;. 
20815 20 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b     if( pToken ){
20816 0a 20 20 20 20 20 20 69 66 28 20 6e 45 78 74 72  .      if( nExtr
20817 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  a==0 ){.        
20818 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  pNew->flags |= E
20819 50 5f 49 6e 74 56 61 6c 75 65 3b 0a 20 20 20 20  P_IntValue;.    
2081a 20 20 20 20 70 4e 65 77 2d 3e 75 2e 69 56 61 6c      pNew->u.iVal
2081b 75 65 20 3d 20 69 56 61 6c 75 65 3b 0a 20 20 20  ue = iValue;.   
2081c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2081d 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20 20    int c;.       
2081e 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 20   pNew->u.zToken 
2081f 3d 20 28 63 68 61 72 2a 29 26 70 4e 65 77 5b 31  = (char*)&pNew[1
20820 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  ];.        memcp
20821 79 28 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e  y(pNew->u.zToken
20822 2c 20 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f  , pToken->z, pTo
20823 6b 65 6e 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 20  ken->n);.       
20824 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b   pNew->u.zToken[
20825 70 54 6f 6b 65 6e 2d 3e 6e 5d 20 3d 20 30 3b 0a  pToken->n] = 0;.
20826 20 20 20 20 20 20 20 20 69 66 28 20 64 65 71 75          if( dequ
20827 6f 74 65 20 26 26 20 6e 45 78 74 72 61 3e 3d 33  ote && nExtra>=3
20828 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26   .             &
20829 26 20 28 28 63 20 3d 20 70 54 6f 6b 65 6e 2d 3e  & ((c = pToken->
2082a 7a 5b 30 5d 29 3d 3d 27 5c 27 27 20 7c 7c 20 63  z[0])=='\'' || c
2082b 3d 3d 27 22 27 20 7c 7c 20 63 3d 3d 27 5b 27 20  =='"' || c=='[' 
2082c 7c 7c 20 63 3d 3d 27 60 27 29 20 29 7b 0a 20 20  || c=='`') ){.  
2082d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
2082e 65 71 75 6f 74 65 28 70 4e 65 77 2d 3e 75 2e 7a  equote(pNew->u.z
2082f 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20 20  Token);.        
20830 20 20 69 66 28 20 63 3d 3d 27 22 27 20 29 20 70    if( c=='"' ) p
20831 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50  New->flags |= EP
20832 5f 44 62 6c 51 75 6f 74 65 64 3b 0a 20 20 20 20  _DblQuoted;.    
20833 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
20834 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d    }.#if SQLITE_M
20835 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a  AX_EXPR_DEPTH>0.
20836 20 20 20 20 70 4e 65 77 2d 3e 6e 48 65 69 67 68      pNew->nHeigh
20837 74 20 3d 20 31 3b 0a 23 65 6e 64 69 66 20 20 0a  t = 1;.#endif  .
20838 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65    }.  return pNe
20839 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  w;.}../*.** Allo
2083a 63 61 74 65 20 61 20 6e 65 77 20 65 78 70 72 65  cate a new expre
2083b 73 73 69 6f 6e 20 6e 6f 64 65 20 66 72 6f 6d 20  ssion node from 
2083c 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  a zero-terminate
2083d 64 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 61 73  d token that has
2083e 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e  .** already been
2083f 20 64 65 71 75 6f 74 65 64 2e 0a 2a 2f 0a 53 51   dequoted..*/.SQ
20840 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
20841 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28 0a  r *sqlite3Expr(.
20842 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
20843 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e            /* Han
20844 64 6c 65 20 66 6f 72 20 73 71 6c 69 74 65 33 44  dle for sqlite3D
20845 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 29 20 28 6d  bMallocZero() (m
20846 61 79 20 62 65 20 6e 75 6c 6c 29 20 2a 2f 0a 20  ay be null) */. 
20847 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20   int op,        
20848 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72           /* Expr
20849 65 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f  ession opcode */
2084a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2084b 54 6f 6b 65 6e 20 20 20 20 20 20 2f 2a 20 54 6f  Token      /* To
2084c 6b 65 6e 20 61 72 67 75 6d 65 6e 74 2e 20 20 4d  ken argument.  M
2084d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a  ight be NULL */.
2084e 29 7b 0a 20 20 54 6f 6b 65 6e 20 78 3b 0a 20 20  ){.  Token x;.  
2084f 78 2e 7a 20 3d 20 7a 54 6f 6b 65 6e 3b 0a 20 20  x.z = zToken;.  
20850 78 2e 6e 20 3d 20 7a 54 6f 6b 65 6e 20 3f 20 73  x.n = zToken ? s
20851 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
20852 54 6f 6b 65 6e 29 20 3a 20 30 3b 0a 20 20 72 65  Token) : 0;.  re
20853 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72  turn sqlite3Expr
20854 41 6c 6c 6f 63 28 64 62 2c 20 6f 70 2c 20 26 78  Alloc(db, op, &x
20855 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  , 0);.}../*.** A
20856 74 74 61 63 68 20 73 75 62 74 72 65 65 73 20 70  ttach subtrees p
20857 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 20  Left and pRight 
20858 74 6f 20 74 68 65 20 45 78 70 72 20 6e 6f 64 65  to the Expr node
20859 20 70 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 66   pRoot..**.** If
2085a 20 70 52 6f 6f 74 3d 3d 4e 55 4c 4c 20 74 68 61   pRoot==NULL tha
2085b 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d  t means that a m
2085c 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
2085d 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72   error has occur
2085e 72 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 61 74 20  red..** In that 
2085f 63 61 73 65 2c 20 64 65 6c 65 74 65 20 74 68 65  case, delete the
20860 20 73 75 62 74 72 65 65 73 20 70 4c 65 66 74 20   subtrees pLeft 
20861 61 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2f 0a 53  and pRight..*/.S
20862 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
20863 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 74  id sqlite3ExprAt
20864 74 61 63 68 53 75 62 74 72 65 65 73 28 0a 20 20  tachSubtrees(.  
20865 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 45  sqlite3 *db,.  E
20866 78 70 72 20 2a 70 52 6f 6f 74 2c 0a 20 20 45 78  xpr *pRoot,.  Ex
20867 70 72 20 2a 70 4c 65 66 74 2c 0a 20 20 45 78 70  pr *pLeft,.  Exp
20868 72 20 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20 69  r *pRight.){.  i
20869 66 28 20 70 52 6f 6f 74 3d 3d 30 20 29 7b 0a 20  f( pRoot==0 ){. 
2086a 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d     assert( db->m
2086b 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
2086c 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
2086d 6c 65 74 65 28 64 62 2c 20 70 4c 65 66 74 29 3b  lete(db, pLeft);
2086e 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2086f 44 65 6c 65 74 65 28 64 62 2c 20 70 52 69 67 68  Delete(db, pRigh
20870 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t);.  }else{.   
20871 20 69 66 28 20 70 52 69 67 68 74 20 29 7b 0a 20   if( pRight ){. 
20872 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70 52 69 67       pRoot->pRig
20873 68 74 20 3d 20 70 52 69 67 68 74 3b 0a 20 20 20  ht = pRight;.   
20874 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 66     if( pRight->f
20875 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c  lags & EP_ExpCol
20876 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  late ){.        
20877 70 52 6f 6f 74 2d 3e 66 6c 61 67 73 20 7c 3d 20  pRoot->flags |= 
20878 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20  EP_ExpCollate;. 
20879 20 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70 43         pRoot->pC
2087a 6f 6c 6c 20 3d 20 70 52 69 67 68 74 2d 3e 70 43  oll = pRight->pC
2087b 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  oll;.      }.   
2087c 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74   }.    if( pLeft
2087d 20 29 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 2d   ){.      pRoot-
2087e 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0a  >pLeft = pLeft;.
2087f 20 20 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d        if( pLeft-
20880 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43  >flags & EP_ExpC
20881 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20  ollate ){.      
20882 20 20 70 52 6f 6f 74 2d 3e 66 6c 61 67 73 20 7c    pRoot->flags |
20883 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b  = EP_ExpCollate;
20884 0a 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e  .        pRoot->
20885 70 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e 70  pColl = pLeft->p
20886 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Coll;.      }.  
20887 20 20 7d 0a 20 20 20 20 65 78 70 72 53 65 74 48    }.    exprSetH
20888 65 69 67 68 74 28 70 52 6f 6f 74 29 3b 0a 20 20  eight(pRoot);.  
20889 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  }.}../*.** Alloc
2088a 61 74 65 20 61 20 45 78 70 72 20 6e 6f 64 65 20  ate a Expr node 
2088b 77 68 69 63 68 20 6a 6f 69 6e 73 20 61 73 20 6d  which joins as m
2088c 61 6e 79 20 61 73 20 74 77 6f 20 73 75 62 74 72  any as two subtr
2088d 65 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20 6f  ees..**.** One o
2088e 72 20 62 6f 74 68 20 6f 66 20 74 68 65 20 73 75  r both of the su
2088f 62 74 72 65 65 73 20 63 61 6e 20 62 65 20 4e 55  btrees can be NU
20890 4c 4c 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f  LL.  Return a po
20891 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77  inter to the new
20892 0a 2a 2a 20 45 78 70 72 20 6e 6f 64 65 2e 20 20  .** Expr node.  
20893 4f 72 2c 20 69 66 20 61 6e 20 4f 4f 4d 20 65 72  Or, if an OOM er
20894 72 6f 72 20 6f 63 63 75 72 73 2c 20 73 65 74 20  ror occurs, set 
20895 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
20896 6f 63 46 61 69 6c 65 64 2c 0a 2a 2a 20 66 72 65  ocFailed,.** fre
20897 65 20 74 68 65 20 73 75 62 74 72 65 65 73 20 61  e the subtrees a
20898 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a  nd return NULL..
20899 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2089a 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 50  E Expr *sqlite3P
2089b 45 78 70 72 28 0a 20 20 50 61 72 73 65 20 2a 70  Expr(.  Parse *p
2089c 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
2089d 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
2089e 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20  xt */.  int op, 
2089f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
208a0 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6f 70  /* Expression op
208a1 63 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a  code */.  Expr *
208a2 70 4c 65 66 74 2c 20 20 20 20 20 20 20 20 20 20  pLeft,          
208a3 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e    /* Left operan
208a4 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69  d */.  Expr *pRi
208a5 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ght,           /
208a6 2a 20 52 69 67 68 74 20 6f 70 65 72 61 6e 64 20  * Right operand 
208a7 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e  */.  const Token
208a8 20 2a 70 54 6f 6b 65 6e 20 20 20 20 20 2f 2a 20   *pToken     /* 
208a9 41 72 67 75 6d 65 6e 74 20 74 6f 6b 65 6e 20 2a  Argument token *
208aa 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d  /.){.  Expr *p =
208ab 20 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f   sqlite3ExprAllo
208ac 63 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 6f 70  c(pParse->db, op
208ad 2c 20 70 54 6f 6b 65 6e 2c 20 31 29 3b 0a 20 20  , pToken, 1);.  
208ae 73 71 6c 69 74 65 33 45 78 70 72 41 74 74 61 63  sqlite3ExprAttac
208af 68 53 75 62 74 72 65 65 73 28 70 50 61 72 73 65  hSubtrees(pParse
208b0 2d 3e 64 62 2c 20 70 2c 20 70 4c 65 66 74 2c 20  ->db, p, pLeft, 
208b1 70 52 69 67 68 74 29 3b 0a 20 20 72 65 74 75 72  pRight);.  retur
208b2 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4a 6f  n p;.}../*.** Jo
208b3 69 6e 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f  in two expressio
208b4 6e 73 20 75 73 69 6e 67 20 61 6e 20 41 4e 44 20  ns using an AND 
208b5 6f 70 65 72 61 74 6f 72 2e 20 20 49 66 20 65 69  operator.  If ei
208b6 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20  ther expression 
208b7 69 73 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 6e  is.** NULL, then
208b8 20 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68 65   just return the
208b9 20 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f   other expressio
208ba 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
208bb 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74  VATE Expr *sqlit
208bc 65 33 45 78 70 72 41 6e 64 28 73 71 6c 69 74 65  e3ExprAnd(sqlite
208bd 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 4c 65  3 *db, Expr *pLe
208be 66 74 2c 20 45 78 70 72 20 2a 70 52 69 67 68 74  ft, Expr *pRight
208bf 29 7b 0a 20 20 69 66 28 20 70 4c 65 66 74 3d 3d  ){.  if( pLeft==
208c0 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
208c1 70 52 69 67 68 74 3b 0a 20 20 7d 65 6c 73 65 20  pRight;.  }else 
208c2 69 66 28 20 70 52 69 67 68 74 3d 3d 30 20 29 7b  if( pRight==0 ){
208c3 0a 20 20 20 20 72 65 74 75 72 6e 20 70 4c 65 66  .    return pLef
208c4 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  t;.  }else{.    
208c5 45 78 70 72 20 2a 70 4e 65 77 20 3d 20 73 71 6c  Expr *pNew = sql
208c6 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 64 62  ite3ExprAlloc(db
208c7 2c 20 54 4b 5f 41 4e 44 2c 20 30 2c 20 30 29 3b  , TK_AND, 0, 0);
208c8 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
208c9 41 74 74 61 63 68 53 75 62 74 72 65 65 73 28 64  AttachSubtrees(d
208ca 62 2c 20 70 4e 65 77 2c 20 70 4c 65 66 74 2c 20  b, pNew, pLeft, 
208cb 70 52 69 67 68 74 29 3b 0a 20 20 20 20 72 65 74  pRight);.    ret
208cc 75 72 6e 20 70 4e 65 77 3b 0a 20 20 7d 0a 7d 0a  urn pNew;.  }.}.
208cd 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74  ./*.** Construct
208ce 20 61 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f   a new expressio
208cf 6e 20 6e 6f 64 65 20 66 6f 72 20 61 20 66 75 6e  n node for a fun
208d0 63 74 69 6f 6e 20 77 69 74 68 20 6d 75 6c 74 69  ction with multi
208d1 70 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73  ple.** arguments
208d2 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
208d3 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
208d4 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61  3ExprFunction(Pa
208d5 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70  rse *pParse, Exp
208d6 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 54 6f  rList *pList, To
208d7 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20  ken *pToken){.  
208d8 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 73 71  Expr *pNew;.  sq
208d9 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
208da 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74  se->db;.  assert
208db 28 20 70 54 6f 6b 65 6e 20 29 3b 0a 20 20 70 4e  ( pToken );.  pN
208dc 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ew = sqlite3Expr
208dd 41 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f 46 55 4e  Alloc(db, TK_FUN
208de 43 54 49 4f 4e 2c 20 70 54 6f 6b 65 6e 2c 20 31  CTION, pToken, 1
208df 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30  );.  if( pNew==0
208e0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
208e1 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
208e2 2c 20 70 4c 69 73 74 29 3b 20 2f 2a 20 41 76 6f  , pList); /* Avo
208e3 69 64 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 77  id memory leak w
208e4 68 65 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73  hen malloc fails
208e5 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30   */.    return 0
208e6 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 78 2e  ;.  }.  pNew->x.
208e7 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20  pList = pList;. 
208e8 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
208e9 73 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c 20  sProperty(pNew, 
208ea 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b  EP_xIsSelect) );
208eb 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65  .  sqlite3ExprSe
208ec 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20  tHeight(pParse, 
208ed 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e 20  pNew);.  return 
208ee 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  pNew;.}../*.** A
208ef 73 73 69 67 6e 20 61 20 76 61 72 69 61 62 6c 65  ssign a variable
208f0 20 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 65 78   number to an ex
208f1 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 65 6e  pression that en
208f2 63 6f 64 65 73 20 61 20 77 69 6c 64 63 61 72 64  codes a wildcard
208f3 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 72 69 67 69  .** in the origi
208f4 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  nal SQL statemen
208f5 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63  t.  .**.** Wildc
208f6 61 72 64 73 20 63 6f 6e 73 69 73 74 69 6e 67 20  ards consisting 
208f7 6f 66 20 61 20 73 69 6e 67 6c 65 20 22 3f 22 20  of a single "?" 
208f8 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65  are assigned the
208f9 20 6e 65 78 74 20 73 65 71 75 65 6e 74 69 61 6c   next sequential
208fa 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e 75 6d  .** variable num
208fb 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63  ber..**.** Wildc
208fc 61 72 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  ards of the form
208fd 20 22 3f 6e 6e 6e 22 20 61 72 65 20 61 73 73 69   "?nnn" are assi
208fe 67 6e 65 64 20 74 68 65 20 6e 75 6d 62 65 72 20  gned the number 
208ff 22 6e 6e 6e 22 2e 20 20 57 65 20 6d 61 6b 65 0a  "nnn".  We make.
20900 2a 2a 20 73 75 72 65 20 22 6e 6e 6e 22 20 69 73  ** sure "nnn" is
20901 20 6e 6f 74 20 74 6f 6f 20 62 65 20 74 6f 20 61   not too be to a
20902 76 6f 69 64 20 61 20 64 65 6e 69 61 6c 20 6f 66  void a denial of
20903 20 73 65 72 76 69 63 65 20 61 74 74 61 63 6b 20   service attack 
20904 77 68 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c 20  when.** the SQL 
20905 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73 20  statement comes 
20906 66 72 6f 6d 20 61 6e 20 65 78 74 65 72 6e 61 6c  from an external
20907 20 73 6f 75 72 63 65 2e 0a 2a 2a 0a 2a 2a 20 57   source..**.** W
20908 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65 20  ildcards of the 
20909 66 6f 72 6d 20 22 3a 61 61 61 22 2c 20 22 40 61  form ":aaa", "@a
2090a 61 61 22 2c 20 6f 72 20 22 24 61 61 61 22 20 61  aa", or "$aaa" a
2090b 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 20  re assigned the 
2090c 73 61 6d 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 61  same number.** a
2090d 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 69  s the previous i
2090e 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73  nstance of the s
2090f 61 6d 65 20 77 69 6c 64 63 61 72 64 2e 20 20 4f  ame wildcard.  O
20910 72 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65  r if this is the
20911 20 66 69 72 73 74 0a 2a 2a 20 69 6e 73 74 61 6e   first.** instan
20912 63 65 20 6f 66 20 74 68 65 20 77 69 6c 64 63 61  ce of the wildca
20913 72 64 2c 20 74 68 65 20 6e 65 78 74 20 73 65 71  rd, the next seq
20914 75 65 6e 69 61 6c 20 76 61 72 69 61 62 6c 65 20  uenial variable 
20915 6e 75 6d 62 65 72 20 69 73 0a 2a 2a 20 61 73 73  number is.** ass
20916 69 67 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  igned..*/.SQLITE
20917 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
20918 6c 69 74 65 33 45 78 70 72 41 73 73 69 67 6e 56  lite3ExprAssignV
20919 61 72 4e 75 6d 62 65 72 28 50 61 72 73 65 20 2a  arNumber(Parse *
2091a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45  pParse, Expr *pE
2091b 78 70 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  xpr){.  sqlite3 
2091c 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2091d 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
2091e 7a 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 3d  z;..  if( pExpr=
2091f 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61  =0 ) return;.  a
20920 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 41  ssert( !ExprHasA
20921 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  nyProperty(pExpr
20922 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 7c 45 50  , EP_IntValue|EP
20923 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65  _Reduced|EP_Toke
20924 6e 4f 6e 6c 79 29 20 29 3b 0a 20 20 7a 20 3d 20  nOnly) );.  z = 
20925 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b  pExpr->u.zToken;
20926 0a 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30 20  .  assert( z!=0 
20927 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 5b 30  );.  assert( z[0
20928 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 7a 5b  ]!=0 );.  if( z[
20929 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  1]==0 ){.    /* 
2092a 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20  Wildcard of the 
2092b 66 6f 72 6d 20 22 3f 22 2e 20 20 41 73 73 69 67  form "?".  Assig
2092c 6e 20 74 68 65 20 6e 65 78 74 20 76 61 72 69 61  n the next varia
2092d 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  ble number */.  
2092e 20 20 61 73 73 65 72 74 28 20 7a 5b 30 5d 3d 3d    assert( z[0]==
2092f 27 3f 27 20 29 3b 0a 20 20 20 20 70 45 78 70 72  '?' );.    pExpr
20930 2d 3e 69 54 61 62 6c 65 20 3d 20 2b 2b 70 50 61  ->iTable = ++pPa
20931 72 73 65 2d 3e 6e 56 61 72 3b 0a 20 20 7d 65 6c  rse->nVar;.  }el
20932 73 65 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 3f 27  se if( z[0]=='?'
20933 20 29 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63   ){.    /* Wildc
20934 61 72 64 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ard of the form 
20935 22 3f 6e 6e 6e 22 2e 20 20 43 6f 6e 76 65 72 74  "?nnn".  Convert
20936 20 22 6e 6e 6e 22 20 74 6f 20 61 6e 20 69 6e 74   "nnn" to an int
20937 65 67 65 72 20 61 6e 64 0a 20 20 20 20 2a 2a 20  eger and.    ** 
20938 75 73 65 20 69 74 20 61 73 20 74 68 65 20 76 61  use it as the va
20939 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f  riable number */
2093a 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
2093b 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20  pExpr->iTable = 
2093c 69 20 3d 20 61 74 6f 69 28 28 63 68 61 72 2a 29  i = atoi((char*)
2093d 26 7a 5b 31 5d 29 3b 0a 20 20 20 20 74 65 73 74  &z[1]);.    test
2093e 63 61 73 65 28 20 69 3d 3d 30 20 29 3b 0a 20 20  case( i==0 );.  
2093f 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
20940 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
20941 28 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b  ( i==db->aLimit[
20942 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52  SQLITE_LIMIT_VAR
20943 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 2d 31 20  IABLE_NUMBER]-1 
20944 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
20945 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53   i==db->aLimit[S
20946 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49  QLITE_LIMIT_VARI
20947 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 3b 0a  ABLE_NUMBER] );.
20948 20 20 20 20 69 66 28 20 69 3c 31 20 7c 7c 20 69      if( i<1 || i
20949 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
2094a 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c  TE_LIMIT_VARIABL
2094b 45 5f 4e 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20  E_NUMBER] ){.   
2094c 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2094d 73 67 28 70 50 61 72 73 65 2c 20 22 76 61 72 69  sg(pParse, "vari
2094e 61 62 6c 65 20 6e 75 6d 62 65 72 20 6d 75 73 74  able number must
2094f 20 62 65 20 62 65 74 77 65 65 6e 20 3f 31 20 61   be between ?1 a
20950 6e 64 20 3f 25 64 22 2c 0a 20 20 20 20 20 20 20  nd ?%d",.       
20951 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51     db->aLimit[SQ
20952 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41  LITE_LIMIT_VARIA
20953 42 4c 45 5f 4e 55 4d 42 45 52 5d 29 3b 0a 20 20  BLE_NUMBER]);.  
20954 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 70 50    }.    if( i>pP
20955 61 72 73 65 2d 3e 6e 56 61 72 20 29 7b 0a 20 20  arse->nVar ){.  
20956 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72      pParse->nVar
20957 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 65   = i;.    }.  }e
20958 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64  lse{.    /* Wild
20959 63 61 72 64 73 20 6c 69 6b 65 20 22 3a 61 61 61  cards like ":aaa
2095a 22 2c 20 22 24 61 61 61 22 20 6f 72 20 22 40 61  ", "$aaa" or "@a
2095b 61 61 22 2e 20 20 52 65 75 73 65 20 74 68 65 20  aa".  Reuse the 
2095c 73 61 6d 65 20 76 61 72 69 61 62 6c 65 0a 20 20  same variable.  
2095d 20 20 2a 2a 20 6e 75 6d 62 65 72 20 61 73 20 74    ** number as t
2095e 68 65 20 70 72 69 6f 72 20 61 70 70 65 61 72 61  he prior appeara
2095f 6e 63 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20  nce of the same 
20960 6e 61 6d 65 2c 20 6f 72 20 69 66 20 74 68 65 20  name, or if the 
20961 6e 61 6d 65 0a 20 20 20 20 2a 2a 20 68 61 73 20  name.    ** has 
20962 6e 65 76 65 72 20 61 70 70 65 61 72 65 64 20 62  never appeared b
20963 65 66 6f 72 65 2c 20 72 65 75 73 65 20 74 68 65  efore, reuse the
20964 20 73 61 6d 65 20 76 61 72 69 61 62 6c 65 20 6e   same variable n
20965 75 6d 62 65 72 0a 20 20 20 20 2a 2f 0a 20 20 20  umber.    */.   
20966 20 69 6e 74 20 69 3b 0a 20 20 20 20 75 33 32 20   int i;.    u32 
20967 6e 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74  n;.    n = sqlit
20968 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0a 20  e3Strlen30(z);. 
20969 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50     for(i=0; i<pP
2096a 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 3b 20  arse->nVarExpr; 
2096b 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72  i++){.      Expr
2096c 20 2a 70 45 20 3d 20 70 50 61 72 73 65 2d 3e 61   *pE = pParse->a
2096d 70 56 61 72 45 78 70 72 5b 69 5d 3b 0a 20 20 20  pVarExpr[i];.   
2096e 20 20 20 61 73 73 65 72 74 28 20 70 45 21 3d 30     assert( pE!=0
2096f 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65   );.      if( me
20970 6d 63 6d 70 28 70 45 2d 3e 75 2e 7a 54 6f 6b 65  mcmp(pE->u.zToke
20971 6e 2c 20 7a 2c 20 6e 29 3d 3d 30 20 26 26 20 70  n, z, n)==0 && p
20972 45 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 6e 5d 3d 3d  E->u.zToken[n]==
20973 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 45 78  0 ){.        pEx
20974 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 2d  pr->iTable = pE-
20975 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 20  >iTable;.       
20976 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
20977 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3e      }.    if( i>
20978 3d 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70  =pParse->nVarExp
20979 72 20 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72  r ){.      pExpr
2097a 2d 3e 69 54 61 62 6c 65 20 3d 20 2b 2b 70 50 61  ->iTable = ++pPa
2097b 72 73 65 2d 3e 6e 56 61 72 3b 0a 20 20 20 20 20  rse->nVar;.     
2097c 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 56 61   if( pParse->nVa
2097d 72 45 78 70 72 3e 3d 70 50 61 72 73 65 2d 3e 6e  rExpr>=pParse->n
2097e 56 61 72 45 78 70 72 41 6c 6c 6f 63 2d 31 20 29  VarExprAlloc-1 )
2097f 7b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65  {.        pParse
20980 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 20  ->nVarExprAlloc 
20981 2b 3d 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45  += pParse->nVarE
20982 78 70 72 41 6c 6c 6f 63 20 2b 20 31 30 3b 0a 20  xprAlloc + 10;. 
20983 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61         pParse->a
20984 70 56 61 72 45 78 70 72 20 3d 0a 20 20 20 20 20  pVarExpr =.     
20985 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
20986 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 0a 20  ReallocOrFree(. 
20987 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2c               db,
20988 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
20989 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72  Parse->apVarExpr
2098a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2098b 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72  pParse->nVarExpr
2098c 41 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28 70 50 61  Alloc*sizeof(pPa
2098d 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 5b 30  rse->apVarExpr[0
2098e 5d 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 29  ]).            )
2098f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
20990 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  if( !db->mallocF
20991 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20  ailed ){.       
20992 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
20993 3e 61 70 56 61 72 45 78 70 72 21 3d 30 20 29 3b  >apVarExpr!=0 );
20994 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
20995 3e 61 70 56 61 72 45 78 70 72 5b 70 50 61 72 73  >apVarExpr[pPars
20996 65 2d 3e 6e 56 61 72 45 78 70 72 2b 2b 5d 20 3d  e->nVarExpr++] =
20997 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 7d 0a   pExpr;.      }.
20998 20 20 20 20 7d 0a 20 20 7d 20 0a 20 20 69 66 28      }.  } .  if(
20999 20 21 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 26   !pParse->nErr &
2099a 26 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 3e 64  & pParse->nVar>d
2099b 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
2099c 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f  _LIMIT_VARIABLE_
2099d 4e 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20 20 73  NUMBER] ){.    s
2099e 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2099f 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79  Parse, "too many
209a0 20 53 51 4c 20 76 61 72 69 61 62 6c 65 73 22 29   SQL variables")
209a1 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ;.  }.}../*.** C
209a2 6c 65 61 72 20 61 6e 20 65 78 70 72 65 73 73 69  lear an expressi
209a3 6f 6e 20 73 74 72 75 63 74 75 72 65 20 77 69 74  on structure wit
209a4 68 6f 75 74 20 64 65 6c 65 74 69 6e 67 20 74 68  hout deleting th
209a5 65 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65  e structure itse
209a6 6c 66 2e 0a 2a 2a 20 53 75 62 73 74 72 75 63 74  lf..** Substruct
209a7 75 72 65 20 69 73 20 64 65 6c 65 74 65 64 2e 0a  ure is deleted..
209a8 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
209a9 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
209aa 70 72 43 6c 65 61 72 28 73 71 6c 69 74 65 33 20  prClear(sqlite3 
209ab 2a 64 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20  *db, Expr *p){. 
209ac 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b   assert( p!=0 );
209ad 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 73 41  .  if( !ExprHasA
209ae 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50  nyProperty(p, EP
209af 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20  _TokenOnly) ){. 
209b0 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
209b1 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c 65 66  lete(db, p->pLef
209b2 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  t);.    sqlite3E
209b3 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  xprDelete(db, p-
209b4 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 69 66  >pRight);.    if
209b5 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
209b6 74 79 28 70 2c 20 45 50 5f 52 65 64 75 63 65 64  ty(p, EP_Reduced
209b7 29 20 26 26 20 28 70 2d 3e 66 6c 61 67 73 32 20  ) && (p->flags2 
209b8 26 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f  & EP2_MallocedTo
209b9 6b 65 6e 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ken)!=0 ){.     
209ba 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
209bb 62 2c 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b  b, p->u.zToken);
209bc 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 45  .    }.    if( E
209bd 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
209be 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
209bf 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
209c0 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
209c1 20 70 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a   p->x.pSelect);.
209c2 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
209c3 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
209c4 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 78 2e  Delete(db, p->x.
209c5 70 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20  pList);.    }.  
209c6 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 75 72  }.}../*.** Recur
209c7 73 69 76 65 6c 79 20 64 65 6c 65 74 65 20 61 6e  sively delete an
209c8 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   expression tree
209c9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
209ca 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
209cb 45 78 70 72 44 65 6c 65 74 65 28 73 71 6c 69 74  ExprDelete(sqlit
209cc 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 29  e3 *db, Expr *p)
209cd 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  {.  if( p==0 ) r
209ce 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33  eturn;.  sqlite3
209cf 45 78 70 72 43 6c 65 61 72 28 64 62 2c 20 70 29  ExprClear(db, p)
209d0 3b 0a 20 20 69 66 28 20 21 45 78 70 72 48 61 73  ;.  if( !ExprHas
209d1 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 53  Property(p, EP_S
209d2 74 61 74 69 63 29 20 29 7b 0a 20 20 20 20 73 71  tatic) ){.    sq
209d3 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
209d4 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
209d5 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
209d6 65 72 20 6f 66 20 62 79 74 65 73 20 61 6c 6c 6f  er of bytes allo
209d7 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 65 78  cated for the ex
209d8 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75  pression structu
209d9 72 65 20 0a 2a 2a 20 70 61 73 73 65 64 20 61 73  re .** passed as
209da 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
209db 65 6e 74 2e 20 54 68 69 73 20 69 73 20 61 6c 77  ent. This is alw
209dc 61 79 73 20 6f 6e 65 20 6f 66 20 45 58 50 52 5f  ays one of EXPR_
209dd 46 55 4c 4c 53 49 5a 45 2c 0a 2a 2a 20 45 58 50  FULLSIZE,.** EXP
209de 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 6f 72  R_REDUCEDSIZE or
209df 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53   EXPR_TOKENONLYS
209e0 49 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  IZE..*/.static i
209e1 6e 74 20 65 78 70 72 53 74 72 75 63 74 53 69 7a  nt exprStructSiz
209e2 65 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69 66  e(Expr *p){.  if
209e3 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
209e4 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  y(p, EP_TokenOnl
209e5 79 29 20 29 20 72 65 74 75 72 6e 20 45 58 50 52  y) ) return EXPR
209e6 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 3b 0a  _TOKENONLYSIZE;.
209e7 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
209e8 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65 64 75  perty(p, EP_Redu
209e9 63 65 64 29 20 29 20 72 65 74 75 72 6e 20 45 58  ced) ) return EX
209ea 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 3b 0a  PR_REDUCEDSIZE;.
209eb 20 20 72 65 74 75 72 6e 20 45 58 50 52 5f 46 55    return EXPR_FU
209ec 4c 4c 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  LLSIZE;.}../*.**
209ed 20 54 68 65 20 64 75 70 65 64 45 78 70 72 2a 53   The dupedExpr*S
209ee 69 7a 65 28 29 20 72 6f 75 74 69 6e 65 73 20 65  ize() routines e
209ef 61 63 68 20 72 65 74 75 72 6e 20 74 68 65 20 6e  ach return the n
209f0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72  umber of bytes r
209f1 65 71 75 69 72 65 64 0a 2a 2a 20 74 6f 20 73 74  equired.** to st
209f2 6f 72 65 20 61 20 63 6f 70 79 20 6f 66 20 61 6e  ore a copy of an
209f3 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 65   expression or e
209f4 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20  xpression tree. 
209f5 20 54 68 65 79 20 64 69 66 66 65 72 20 69 6e 0a   They differ in.
209f6 2a 2a 20 68 6f 77 20 6d 75 63 68 20 6f 66 20 74  ** how much of t
209f7 68 65 20 74 72 65 65 20 69 73 20 6d 65 61 73 75  he tree is measu
209f8 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 64  red..**.**     d
209f9 75 70 65 64 45 78 70 72 53 74 72 75 63 74 53 69  upedExprStructSi
209fa 7a 65 28 29 20 20 20 20 20 53 69 7a 65 20 6f 66  ze()     Size of
209fb 20 6f 6e 6c 79 20 74 68 65 20 45 78 70 72 20 73   only the Expr s
209fc 74 72 75 63 74 75 72 65 20 0a 2a 2a 20 20 20 20  tructure .**    
209fd 20 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69   dupedExprNodeSi
209fe 7a 65 28 29 20 20 20 20 20 20 20 53 69 7a 65 20  ze()       Size 
209ff 6f 66 20 45 78 70 72 20 2b 20 73 70 61 63 65 20  of Expr + space 
20a00 66 6f 72 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20  for token.**    
20a01 20 64 75 70 65 64 45 78 70 72 53 69 7a 65 28 29   dupedExprSize()
20a02 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20             Expr 
20a03 2b 20 74 6f 6b 65 6e 20 2b 20 73 75 62 74 72 65  + token + subtre
20a04 65 20 63 6f 6d 70 6f 6e 65 6e 74 73 0a 2a 2a 0a  e components.**.
20a05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
20a0a 2a 20 54 68 65 20 64 75 70 65 64 45 78 70 72 53  * The dupedExprS
20a0b 74 72 75 63 74 53 69 7a 65 28 29 20 66 75 6e 63  tructSize() func
20a0c 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 77 6f  tion returns two
20a0d 20 76 61 6c 75 65 73 20 4f 52 2d 65 64 20 74 6f   values OR-ed to
20a0e 67 65 74 68 65 72 3a 20 20 0a 2a 2a 20 28 31 29  gether:  .** (1)
20a0f 20 74 68 65 20 73 70 61 63 65 20 72 65 71 75 69   the space requi
20a10 72 65 64 20 66 6f 72 20 61 20 63 6f 70 79 20 6f  red for a copy o
20a11 66 20 74 68 65 20 45 78 70 72 20 73 74 72 75 63  f the Expr struc
20a12 74 75 72 65 20 6f 6e 6c 79 20 61 6e 64 20 0a 2a  ture only and .*
20a13 2a 20 28 32 29 20 74 68 65 20 45 50 5f 78 78 78  * (2) the EP_xxx
20a14 20 66 6c 61 67 73 20 74 68 61 74 20 69 6e 64 69   flags that indi
20a15 63 61 74 65 20 77 68 61 74 20 74 68 65 20 73 74  cate what the st
20a16 72 75 63 74 75 72 65 20 73 69 7a 65 20 73 68 6f  ructure size sho
20a17 75 6c 64 20 62 65 2e 0a 2a 2a 20 54 68 65 20 72  uld be..** The r
20a18 65 74 75 72 6e 20 76 61 6c 75 65 73 20 69 73 20  eturn values is 
20a19 61 6c 77 61 79 73 20 6f 6e 65 20 6f 66 3a 0a 2a  always one of:.*
20a1a 2a 0a 2a 2a 20 20 20 20 20 20 45 58 50 52 5f 46  *.**      EXPR_F
20a1b 55 4c 4c 53 49 5a 45 0a 2a 2a 20 20 20 20 20 20  ULLSIZE.**      
20a1c 45 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45  EXPR_REDUCEDSIZE
20a1d 20 20 20 7c 20 45 50 5f 52 65 64 75 63 65 64 0a     | EP_Reduced.
20a1e 2a 2a 20 20 20 20 20 20 45 58 50 52 5f 54 4f 4b  **      EXPR_TOK
20a1f 45 4e 4f 4e 4c 59 53 49 5a 45 20 7c 20 45 50 5f  ENONLYSIZE | EP_
20a20 54 6f 6b 65 6e 4f 6e 6c 79 0a 2a 2a 0a 2a 2a 20  TokenOnly.**.** 
20a21 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  The size of the 
20a22 73 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 65  structure can be
20a23 20 66 6f 75 6e 64 20 62 79 20 6d 61 73 6b 69 6e   found by maskin
20a24 67 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  g the return val
20a25 75 65 0a 2a 2a 20 6f 66 20 74 68 69 73 20 72 6f  ue.** of this ro
20a26 75 74 69 6e 65 20 77 69 74 68 20 30 78 66 66 66  utine with 0xfff
20a27 2e 20 20 54 68 65 20 66 6c 61 67 73 20 63 61 6e  .  The flags can
20a28 20 62 65 20 66 6f 75 6e 64 20 62 79 20 6d 61 73   be found by mas
20a29 6b 69 6e 67 20 74 68 65 0a 2a 2a 20 72 65 74 75  king the.** retu
20a2a 72 6e 20 76 61 6c 75 65 20 77 69 74 68 20 45 50  rn value with EP
20a2b 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65  _Reduced|EP_Toke
20a2c 6e 4f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  nOnly..**.** Not
20a2d 65 20 74 68 61 74 20 77 69 74 68 20 66 6c 61 67  e that with flag
20a2e 73 3d 3d 45 58 50 52 44 55 50 5f 52 45 44 55 43  s==EXPRDUP_REDUC
20a2f 45 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 73  E, this routines
20a30 20 77 6f 72 6b 73 20 6f 6e 20 66 75 6c 6c 2d 73   works on full-s
20a31 69 7a 65 0a 2a 2a 20 28 75 6e 72 65 64 75 63 65  ize.** (unreduce
20a32 64 29 20 45 78 70 72 20 6f 62 6a 65 63 74 73 20  d) Expr objects 
20a33 61 73 20 74 68 65 79 20 6f 72 20 6f 72 69 67 69  as they or origi
20a34 6e 61 6c 6c 79 20 63 6f 6e 73 74 72 75 63 74 65  nally constructe
20a35 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 2e  d by the parser.
20a36 0a 2a 2a 20 44 75 72 69 6e 67 20 65 78 70 72 65  .** During expre
20a37 73 73 69 6f 6e 20 61 6e 61 6c 79 73 69 73 2c 20  ssion analysis, 
20a38 65 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f  extra informatio
20a39 6e 20 69 73 20 63 6f 6d 70 75 74 65 64 20 61 6e  n is computed an
20a3a 64 20 6d 6f 76 65 64 20 69 6e 74 6f 0a 2a 2a 20  d moved into.** 
20a3b 6c 61 74 65 72 20 70 61 72 74 73 20 6f 66 20 74  later parts of t
20a3c 65 68 20 45 78 70 72 20 6f 62 6a 65 63 74 20 61  eh Expr object a
20a3d 6e 64 20 74 68 61 74 20 65 78 74 72 61 20 69 6e  nd that extra in
20a3e 66 6f 72 6d 61 74 69 6f 6e 20 6d 69 67 68 74 20  formation might 
20a3f 67 65 74 20 63 68 6f 70 70 65 64 0a 2a 2a 20 6f  get chopped.** o
20a40 66 66 20 69 66 20 74 68 65 20 65 78 70 72 65 73  ff if the expres
20a41 73 69 6f 6e 20 69 73 20 72 65 64 75 63 65 64 2e  sion is reduced.
20a42 20 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74    Note also that
20a43 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72   it does not wor
20a44 6b 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 61 20 45  k to.** make a E
20a45 58 50 52 44 55 50 5f 52 45 44 55 43 45 20 63 6f  XPRDUP_REDUCE co
20a46 70 79 20 6f 66 20 61 20 72 65 64 75 63 65 64 20  py of a reduced 
20a47 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 49 74 20  expression.  It 
20a48 69 73 20 6f 6e 6c 79 20 6c 65 67 61 6c 0a 2a 2a  is only legal.**
20a49 20 74 6f 20 72 65 64 75 63 65 20 61 20 70 72 69   to reduce a pri
20a4a 73 74 69 6e 65 20 65 78 70 72 65 73 73 69 6f 6e  stine expression
20a4b 20 74 72 65 65 20 66 72 6f 6d 20 74 68 65 20 70   tree from the p
20a4c 61 72 73 65 72 2e 20 20 54 68 65 20 69 6d 70 6c  arser.  The impl
20a4d 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66  ementation.** of
20a4e 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63 74   dupedExprStruct
20a4f 53 69 7a 65 28 29 20 63 6f 6e 74 61 69 6e 20 6d  Size() contain m
20a50 75 6c 74 69 70 6c 65 20 61 73 73 65 72 74 28 29  ultiple assert()
20a51 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74   statements that
20a52 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65   attempt.** to e
20a53 6e 66 6f 72 63 65 20 74 68 69 73 20 63 6f 6e 73  nforce this cons
20a54 74 72 61 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69  traint..*/.stati
20a55 63 20 69 6e 74 20 64 75 70 65 64 45 78 70 72 53  c int dupedExprS
20a56 74 72 75 63 74 53 69 7a 65 28 45 78 70 72 20 2a  tructSize(Expr *
20a57 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20  p, int flags){. 
20a58 20 69 6e 74 20 6e 53 69 7a 65 3b 0a 20 20 61 73   int nSize;.  as
20a59 73 65 72 74 28 20 66 6c 61 67 73 3d 3d 45 58 50  sert( flags==EXP
20a5a 52 44 55 50 5f 52 45 44 55 43 45 20 7c 7c 20 66  RDUP_REDUCE || f
20a5b 6c 61 67 73 3d 3d 30 20 29 3b 20 2f 2a 20 4f 6e  lags==0 ); /* On
20a5c 6c 79 20 6f 6e 65 20 66 6c 61 67 20 76 61 6c 75  ly one flag valu
20a5d 65 20 61 6c 6c 6f 77 65 64 20 2a 2f 0a 20 20 69  e allowed */.  i
20a5e 66 28 20 30 3d 3d 28 66 6c 61 67 73 26 45 58 50  f( 0==(flags&EXP
20a5f 52 44 55 50 5f 52 45 44 55 43 45 29 20 29 7b 0a  RDUP_REDUCE) ){.
20a60 20 20 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 52      nSize = EXPR
20a61 5f 46 55 4c 4c 53 49 5a 45 3b 0a 20 20 7d 65 6c  _FULLSIZE;.  }el
20a62 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
20a63 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65  !ExprHasAnyPrope
20a64 72 74 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f  rty(p, EP_TokenO
20a65 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20  nly|EP_Reduced) 
20a66 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  );.    assert( !
20a67 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
20a68 70 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20  p, EP_FromJoin) 
20a69 29 3b 20 0a 20 20 20 20 61 73 73 65 72 74 28 20  ); .    assert( 
20a6a 28 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32  (p->flags2 & EP2
20a6b 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29 3d  _MallocedToken)=
20a6c 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
20a6d 28 20 28 70 2d 3e 66 6c 61 67 73 32 20 26 20 45  ( (p->flags2 & E
20a6e 50 32 5f 49 72 72 65 64 75 63 69 62 6c 65 29 3d  P2_Irreducible)=
20a6f 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 2d  =0 );.    if( p-
20a70 3e 70 4c 65 66 74 20 7c 7c 20 70 2d 3e 70 52 69  >pLeft || p->pRi
20a71 67 68 74 20 7c 7c 20 70 2d 3e 70 43 6f 6c 6c 20  ght || p->pColl 
20a72 7c 7c 20 70 2d 3e 78 2e 70 4c 69 73 74 20 29 7b  || p->x.pList ){
20a73 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 45  .      nSize = E
20a74 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20  XPR_REDUCEDSIZE 
20a75 7c 20 45 50 5f 52 65 64 75 63 65 64 3b 0a 20 20  | EP_Reduced;.  
20a76 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e    }else{.      n
20a77 53 69 7a 65 20 3d 20 45 58 50 52 5f 54 4f 4b 45  Size = EXPR_TOKE
20a78 4e 4f 4e 4c 59 53 49 5a 45 20 7c 20 45 50 5f 54  NONLYSIZE | EP_T
20a79 6f 6b 65 6e 4f 6e 6c 79 3b 0a 20 20 20 20 7d 0a  okenOnly;.    }.
20a7a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 53 69    }.  return nSi
20a7b 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ze;.}../*.** Thi
20a7c 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
20a7d 6e 73 20 74 68 65 20 73 70 61 63 65 20 69 6e 20  ns the space in 
20a7e 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 74  bytes required t
20a7f 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f 70 79  o store the copy
20a80 20 0a 2a 2a 20 6f 66 20 74 68 65 20 45 78 70 72   .** of the Expr
20a81 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 61   structure and a
20a82 20 63 6f 70 79 20 6f 66 20 74 68 65 20 45 78 70   copy of the Exp
20a83 72 2e 75 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e  r.u.zToken strin
20a84 67 20 28 69 66 20 74 68 61 74 0a 2a 2a 20 73 74  g (if that.** st
20a85 72 69 6e 67 20 69 73 20 64 65 66 69 6e 65 64 2e  ring is defined.
20a86 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ).*/.static int 
20a87 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69 7a  dupedExprNodeSiz
20a88 65 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 66  e(Expr *p, int f
20a89 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79  lags){.  int nBy
20a8a 74 65 20 3d 20 64 75 70 65 64 45 78 70 72 53 74  te = dupedExprSt
20a8b 72 75 63 74 53 69 7a 65 28 70 2c 20 66 6c 61 67  ructSize(p, flag
20a8c 73 29 20 26 20 30 78 66 66 66 3b 0a 20 20 69 66  s) & 0xfff;.  if
20a8d 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
20a8e 74 79 28 70 2c 20 45 50 5f 49 6e 74 56 61 6c 75  ty(p, EP_IntValu
20a8f 65 29 20 26 26 20 70 2d 3e 75 2e 7a 54 6f 6b 65  e) && p->u.zToke
20a90 6e 20 29 7b 0a 20 20 20 20 6e 42 79 74 65 20 2b  n ){.    nByte +
20a91 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
20a92 30 28 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2b 31  0(p->u.zToken)+1
20a93 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 52  ;.  }.  return R
20a94 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 7d 0a  OUND8(nByte);.}.
20a95 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
20a96 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
20a97 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 63 72  s required to cr
20a98 65 61 74 65 20 61 20 64 75 70 6c 69 63 61 74 65  eate a duplicate
20a99 20 6f 66 20 74 68 65 20 0a 2a 2a 20 65 78 70 72   of the .** expr
20a9a 65 73 73 69 6f 6e 20 70 61 73 73 65 64 20 61 73  ession passed as
20a9b 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
20a9c 65 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 20  ent. The second 
20a9d 61 72 67 75 6d 65 6e 74 20 69 73 20 61 0a 2a 2a  argument is a.**
20a9e 20 6d 61 73 6b 20 63 6f 6e 74 61 69 6e 69 6e 67   mask containing
20a9f 20 45 58 50 52 44 55 50 5f 58 58 58 20 66 6c 61   EXPRDUP_XXX fla
20aa0 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61  gs..**.** The va
20aa1 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 63  lue returned inc
20aa2 6c 75 64 65 73 20 73 70 61 63 65 20 74 6f 20 63  ludes space to c
20aa3 72 65 61 74 65 20 61 20 63 6f 70 79 20 6f 66 20  reate a copy of 
20aa4 74 68 65 20 45 78 70 72 20 73 74 72 75 63 74 0a  the Expr struct.
20aa5 2a 2a 20 69 74 73 65 6c 66 20 61 6e 64 20 74 68  ** itself and th
20aa6 65 20 62 75 66 66 65 72 20 72 65 66 65 72 72 65  e buffer referre
20aa7 64 20 74 6f 20 62 79 20 45 78 70 72 2e 75 2e 7a  d to by Expr.u.z
20aa8 54 6f 6b 65 6e 2c 20 69 66 20 61 6e 79 2e 0a 2a  Token, if any..*
20aa9 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 58 50 52  *.** If the EXPR
20aaa 44 55 50 5f 52 45 44 55 43 45 20 66 6c 61 67 20  DUP_REDUCE flag 
20aab 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65  is set, then the
20aac 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e   return value in
20aad 63 6c 75 64 65 73 20 0a 2a 2a 20 73 70 61 63 65  cludes .** space
20aae 20 74 6f 20 64 75 70 6c 69 63 61 74 65 20 61 6c   to duplicate al
20aaf 6c 20 45 78 70 72 20 6e 6f 64 65 73 20 69 6e 20  l Expr nodes in 
20ab0 74 68 65 20 74 72 65 65 20 66 6f 72 6d 65 64 20  the tree formed 
20ab1 62 79 20 45 78 70 72 2e 70 4c 65 66 74 20 0a 2a  by Expr.pLeft .*
20ab2 2a 20 61 6e 64 20 45 78 70 72 2e 70 52 69 67 68  * and Expr.pRigh
20ab3 74 20 76 61 72 69 61 62 6c 65 73 20 28 62 75 74  t variables (but
20ab4 20 6e 6f 74 20 66 6f 72 20 61 6e 79 20 73 74 72   not for any str
20ab5 75 63 74 75 72 65 73 20 70 6f 69 6e 74 65 64 20  uctures pointed 
20ab6 74 6f 20 6f 72 20 0a 2a 2a 20 64 65 73 63 65 6e  to or .** descen
20ab7 64 65 64 20 66 72 6f 6d 20 74 68 65 20 45 78 70  ded from the Exp
20ab8 72 2e 78 2e 70 4c 69 73 74 20 6f 72 20 45 78 70  r.x.pList or Exp
20ab9 72 2e 78 2e 70 53 65 6c 65 63 74 20 76 61 72 69  r.x.pSelect vari
20aba 61 62 6c 65 73 29 2e 0a 2a 2f 0a 73 74 61 74 69  ables)..*/.stati
20abb 63 20 69 6e 74 20 64 75 70 65 64 45 78 70 72 53  c int dupedExprS
20abc 69 7a 65 28 45 78 70 72 20 2a 70 2c 20 69 6e 74  ize(Expr *p, int
20abd 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e   flags){.  int n
20abe 42 79 74 65 20 3d 20 30 3b 0a 20 20 69 66 28 20  Byte = 0;.  if( 
20abf 70 20 29 7b 0a 20 20 20 20 6e 42 79 74 65 20 3d  p ){.    nByte =
20ac0 20 64 75 70 65 64 45 78 70 72 4e 6f 64 65 53 69   dupedExprNodeSi
20ac1 7a 65 28 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20  ze(p, flags);.  
20ac2 20 20 69 66 28 20 66 6c 61 67 73 26 45 58 50 52    if( flags&EXPR
20ac3 44 55 50 5f 52 45 44 55 43 45 20 29 7b 0a 20 20  DUP_REDUCE ){.  
20ac4 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 64 75 70      nByte += dup
20ac5 65 64 45 78 70 72 53 69 7a 65 28 70 2d 3e 70 4c  edExprSize(p->pL
20ac6 65 66 74 2c 20 66 6c 61 67 73 29 20 2b 20 64 75  eft, flags) + du
20ac7 70 65 64 45 78 70 72 53 69 7a 65 28 70 2d 3e 70  pedExprSize(p->p
20ac8 52 69 67 68 74 2c 20 66 6c 61 67 73 29 3b 0a 20  Right, flags);. 
20ac9 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
20aca 6e 20 6e 42 79 74 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  n nByte;.}../*.*
20acb 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
20acc 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 73 71  is similar to sq
20acd 6c 69 74 65 33 45 78 70 72 44 75 70 28 29 2c 20  lite3ExprDup(), 
20ace 65 78 63 65 70 74 20 74 68 61 74 20 69 66 20 70  except that if p
20acf 7a 42 75 66 66 65 72 20 0a 2a 2a 20 69 73 20 6e  zBuffer .** is n
20ad0 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 2a 70 7a  ot NULL then *pz
20ad1 42 75 66 66 65 72 20 69 73 20 61 73 73 75 6d 65  Buffer is assume
20ad2 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20  d to point to a 
20ad3 62 75 66 66 65 72 20 6c 61 72 67 65 20 65 6e 6f  buffer large eno
20ad4 75 67 68 20 0a 2a 2a 20 74 6f 20 73 74 6f 72 65  ugh .** to store
20ad5 20 74 68 65 20 63 6f 70 79 20 6f 66 20 65 78 70   the copy of exp
20ad6 72 65 73 73 69 6f 6e 20 70 2c 20 74 68 65 20 63  ression p, the c
20ad7 6f 70 69 65 73 20 6f 66 20 70 2d 3e 75 2e 7a 54  opies of p->u.zT
20ad8 6f 6b 65 6e 0a 2a 2a 20 28 69 66 20 61 70 70 6c  oken.** (if appl
20ad9 69 63 61 62 6c 65 29 2c 20 61 6e 64 20 74 68 65  icable), and the
20ada 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 70   copies of the p
20adb 2d 3e 70 4c 65 66 74 20 61 6e 64 20 70 2d 3e 70  ->pLeft and p->p
20adc 52 69 67 68 74 20 65 78 70 72 65 73 73 69 6f 6e  Right expression
20add 73 2c 0a 2a 2a 20 69 66 20 61 6e 79 2e 20 42 65  s,.** if any. Be
20ade 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20  fore returning, 
20adf 2a 70 7a 42 75 66 66 65 72 20 69 73 20 73 65 74  *pzBuffer is set
20ae0 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 62 79   to the first by
20ae1 74 65 20 70 61 73 73 65 64 20 74 68 65 0a 2a 2a  te passed the.**
20ae2 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20   portion of the 
20ae3 62 75 66 66 65 72 20 63 6f 70 69 65 64 20 69 6e  buffer copied in
20ae4 74 6f 20 62 79 20 74 68 69 73 20 66 75 6e 63 74  to by this funct
20ae5 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 45  ion..*/.static E
20ae6 78 70 72 20 2a 65 78 70 72 44 75 70 28 73 71 6c  xpr *exprDup(sql
20ae7 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a  ite3 *db, Expr *
20ae8 70 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20 75 38  p, int flags, u8
20ae9 20 2a 2a 70 7a 42 75 66 66 65 72 29 7b 0a 20 20   **pzBuffer){.  
20aea 45 78 70 72 20 2a 70 4e 65 77 20 3d 20 30 3b 20  Expr *pNew = 0; 
20aeb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20aec 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f       /* Value to
20aed 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 66 28   return */.  if(
20aee 20 70 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20   p ){.    const 
20aef 69 6e 74 20 69 73 52 65 64 75 63 65 64 20 3d 20  int isReduced = 
20af0 28 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f 52  (flags&EXPRDUP_R
20af1 45 44 55 43 45 29 3b 0a 20 20 20 20 75 38 20 2a  EDUCE);.    u8 *
20af2 7a 41 6c 6c 6f 63 3b 0a 20 20 20 20 75 33 32 20  zAlloc;.    u32 
20af3 73 74 61 74 69 63 46 6c 61 67 20 3d 20 30 3b 0a  staticFlag = 0;.
20af4 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 7a 42  .    assert( pzB
20af5 75 66 66 65 72 3d 3d 30 20 7c 7c 20 69 73 52 65  uffer==0 || isRe
20af6 64 75 63 65 64 20 29 3b 0a 0a 20 20 20 20 2f 2a  duced );..    /*
20af7 20 46 69 67 75 72 65 20 6f 75 74 20 77 68 65 72   Figure out wher
20af8 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 6e  e to write the n
20af9 65 77 20 45 78 70 72 20 73 74 72 75 63 74 75 72  ew Expr structur
20afa 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 7a  e. */.    if( pz
20afb 42 75 66 66 65 72 20 29 7b 0a 20 20 20 20 20 20  Buffer ){.      
20afc 7a 41 6c 6c 6f 63 20 3d 20 2a 70 7a 42 75 66 66  zAlloc = *pzBuff
20afd 65 72 3b 0a 20 20 20 20 20 20 73 74 61 74 69 63  er;.      static
20afe 46 6c 61 67 20 3d 20 45 50 5f 53 74 61 74 69 63  Flag = EP_Static
20aff 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
20b00 20 20 20 7a 41 6c 6c 6f 63 20 3d 20 73 71 6c 69     zAlloc = sqli
20b01 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
20b02 62 2c 20 64 75 70 65 64 45 78 70 72 53 69 7a 65  b, dupedExprSize
20b03 28 70 2c 20 66 6c 61 67 73 29 29 3b 0a 20 20 20  (p, flags));.   
20b04 20 7d 0a 20 20 20 20 70 4e 65 77 20 3d 20 28 45   }.    pNew = (E
20b05 78 70 72 20 2a 29 7a 41 6c 6c 6f 63 3b 0a 0a 20  xpr *)zAlloc;.. 
20b06 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20     if( pNew ){. 
20b07 20 20 20 20 20 2f 2a 20 53 65 74 20 6e 4e 65 77       /* Set nNew
20b08 53 69 7a 65 20 74 6f 20 74 68 65 20 73 69 7a 65  Size to the size
20b09 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74   allocated for t
20b0a 68 65 20 73 74 72 75 63 74 75 72 65 20 70 6f 69  he structure poi
20b0b 6e 74 65 64 20 74 6f 0a 20 20 20 20 20 20 2a 2a  nted to.      **
20b0c 20 62 79 20 70 4e 65 77 2e 20 54 68 69 73 20 69   by pNew. This i
20b0d 73 20 65 69 74 68 65 72 20 45 58 50 52 5f 46 55  s either EXPR_FU
20b0e 4c 4c 53 49 5a 45 2c 20 45 58 50 52 5f 52 45 44  LLSIZE, EXPR_RED
20b0f 55 43 45 44 53 49 5a 45 20 6f 72 0a 20 20 20 20  UCEDSIZE or.    
20b10 20 20 2a 2a 20 45 58 50 52 5f 54 4f 4b 45 4e 4f    ** EXPR_TOKENO
20b11 4e 4c 59 53 49 5a 45 2e 20 6e 54 6f 6b 65 6e 20  NLYSIZE. nToken 
20b12 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75  is set to the nu
20b13 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 63 6f  mber of bytes co
20b14 6e 73 75 6d 65 64 0a 20 20 20 20 20 20 2a 2a 20  nsumed.      ** 
20b15 62 79 20 74 68 65 20 63 6f 70 79 20 6f 66 20 74  by the copy of t
20b16 68 65 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 73  he p->u.zToken s
20b17 74 72 69 6e 67 20 28 69 66 20 61 6e 79 29 2e 0a  tring (if any)..
20b18 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63        */.      c
20b19 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 6e 53  onst unsigned nS
20b1a 74 72 75 63 74 53 69 7a 65 20 3d 20 64 75 70 65  tructSize = dupe
20b1b 64 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28  dExprStructSize(
20b1c 70 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 20  p, flags);.     
20b1d 20 63 6f 6e 73 74 20 69 6e 74 20 6e 4e 65 77 53   const int nNewS
20b1e 69 7a 65 20 3d 20 6e 53 74 72 75 63 74 53 69 7a  ize = nStructSiz
20b1f 65 20 26 20 30 78 66 66 66 3b 0a 20 20 20 20 20  e & 0xfff;.     
20b20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20   int nToken;.   
20b21 20 20 20 69 66 28 20 21 45 78 70 72 48 61 73 50     if( !ExprHasP
20b22 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 49 6e  roperty(p, EP_In
20b23 74 56 61 6c 75 65 29 20 26 26 20 70 2d 3e 75 2e  tValue) && p->u.
20b24 7a 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20  zToken ){.      
20b25 20 20 6e 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74    nToken = sqlit
20b26 65 33 53 74 72 6c 65 6e 33 30 28 70 2d 3e 75 2e  e3Strlen30(p->u.
20b27 7a 54 6f 6b 65 6e 29 20 2b 20 31 3b 0a 20 20 20  zToken) + 1;.   
20b28 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
20b29 20 20 6e 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20    nToken = 0;.  
20b2a 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
20b2b 69 73 52 65 64 75 63 65 64 20 29 7b 0a 20 20 20  isReduced ){.   
20b2c 20 20 20 20 20 61 73 73 65 72 74 28 20 45 78 70       assert( Exp
20b2d 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20  rHasProperty(p, 
20b2e 45 50 5f 52 65 64 75 63 65 64 29 3d 3d 30 20 29  EP_Reduced)==0 )
20b2f 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  ;.        memcpy
20b30 28 7a 41 6c 6c 6f 63 2c 20 70 2c 20 6e 4e 65 77  (zAlloc, p, nNew
20b31 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c  Size);.      }el
20b32 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  se{.        int 
20b33 6e 53 69 7a 65 20 3d 20 65 78 70 72 53 74 72 75  nSize = exprStru
20b34 63 74 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20  ctSize(p);.     
20b35 20 20 20 6d 65 6d 63 70 79 28 7a 41 6c 6c 6f 63     memcpy(zAlloc
20b36 2c 20 70 2c 20 6e 53 69 7a 65 29 3b 0a 20 20 20  , p, nSize);.   
20b37 20 20 20 20 20 6d 65 6d 73 65 74 28 26 7a 41 6c       memset(&zAl
20b38 6c 6f 63 5b 6e 53 69 7a 65 5d 2c 20 30 2c 20 45  loc[nSize], 0, E
20b39 58 50 52 5f 46 55 4c 4c 53 49 5a 45 2d 6e 53 69  XPR_FULLSIZE-nSi
20b3a 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  ze);.      }..  
20b3b 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 45      /* Set the E
20b3c 50 5f 52 65 64 75 63 65 64 2c 20 45 50 5f 54 6f  P_Reduced, EP_To
20b3d 6b 65 6e 4f 6e 6c 79 2c 20 61 6e 64 20 45 50 5f  kenOnly, and EP_
20b3e 53 74 61 74 69 63 20 66 6c 61 67 73 20 61 70 70  Static flags app
20b3f 72 6f 70 72 69 61 74 65 6c 79 2e 20 2a 2f 0a 20  ropriately. */. 
20b40 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73       pNew->flags
20b41 20 26 3d 20 7e 28 45 50 5f 52 65 64 75 63 65 64   &= ~(EP_Reduced
20b42 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50  |EP_TokenOnly|EP
20b43 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20 20 20  _Static);.      
20b44 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 6e  pNew->flags |= n
20b45 53 74 72 75 63 74 53 69 7a 65 20 26 20 28 45 50  StructSize & (EP
20b46 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65  _Reduced|EP_Toke
20b47 6e 4f 6e 6c 79 29 3b 0a 20 20 20 20 20 20 70 4e  nOnly);.      pN
20b48 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 73 74 61  ew->flags |= sta
20b49 74 69 63 46 6c 61 67 3b 0a 0a 20 20 20 20 20 20  ticFlag;..      
20b4a 2f 2a 20 43 6f 70 79 20 74 68 65 20 70 2d 3e 75  /* Copy the p->u
20b4b 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67 2c 20  .zToken string, 
20b4c 69 66 20 61 6e 79 2e 20 2a 2f 0a 20 20 20 20 20  if any. */.     
20b4d 20 69 66 28 20 6e 54 6f 6b 65 6e 20 29 7b 0a 20   if( nToken ){. 
20b4e 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 6f         char *zTo
20b4f 6b 65 6e 20 3d 20 70 4e 65 77 2d 3e 75 2e 7a 54  ken = pNew->u.zT
20b50 6f 6b 65 6e 20 3d 20 28 63 68 61 72 2a 29 26 7a  oken = (char*)&z
20b51 41 6c 6c 6f 63 5b 6e 4e 65 77 53 69 7a 65 5d 3b  Alloc[nNewSize];
20b52 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
20b53 7a 54 6f 6b 65 6e 2c 20 70 2d 3e 75 2e 7a 54 6f  zToken, p->u.zTo
20b54 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20  ken, nToken);.  
20b55 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28      }..      if(
20b56 20 30 3d 3d 28 28 70 2d 3e 66 6c 61 67 73 7c 70   0==((p->flags|p
20b57 4e 65 77 2d 3e 66 6c 61 67 73 29 20 26 20 45 50  New->flags) & EP
20b58 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20  _TokenOnly) ){. 
20b59 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69         /* Fill i
20b5a 6e 20 74 68 65 20 70 4e 65 77 2d 3e 78 2e 70 53  n the pNew->x.pS
20b5b 65 6c 65 63 74 20 6f 72 20 70 4e 65 77 2d 3e 78  elect or pNew->x
20b5c 2e 70 4c 69 73 74 20 6d 65 6d 62 65 72 2e 20 2a  .pList member. *
20b5d 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 45 78  /.        if( Ex
20b5e 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c  prHasProperty(p,
20b5f 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
20b60 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77  {.          pNew
20b61 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20 73 71  ->x.pSelect = sq
20b62 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64  lite3SelectDup(d
20b63 62 2c 20 70 2d 3e 78 2e 70 53 65 6c 65 63 74 2c  b, p->x.pSelect,
20b64 20 69 73 52 65 64 75 63 65 64 29 3b 0a 20 20 20   isReduced);.   
20b65 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
20b66 20 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e 70 4c        pNew->x.pL
20b67 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ist = sqlite3Exp
20b68 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e  rListDup(db, p->
20b69 78 2e 70 4c 69 73 74 2c 20 69 73 52 65 64 75 63  x.pList, isReduc
20b6a 65 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ed);.        }. 
20b6b 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
20b6c 20 46 69 6c 6c 20 69 6e 20 70 4e 65 77 2d 3e 70   Fill in pNew->p
20b6d 4c 65 66 74 20 61 6e 64 20 70 4e 65 77 2d 3e 70  Left and pNew->p
20b6e 52 69 67 68 74 2e 20 2a 2f 0a 20 20 20 20 20 20  Right. */.      
20b6f 69 66 28 20 45 78 70 72 48 61 73 41 6e 79 50 72  if( ExprHasAnyPr
20b70 6f 70 65 72 74 79 28 70 4e 65 77 2c 20 45 50 5f  operty(pNew, EP_
20b71 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e  Reduced|EP_Token
20b72 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 20  Only) ){.       
20b73 20 7a 41 6c 6c 6f 63 20 2b 3d 20 64 75 70 65 64   zAlloc += duped
20b74 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 70 2c 20  ExprNodeSize(p, 
20b75 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20  flags);.        
20b76 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
20b77 72 74 79 28 70 4e 65 77 2c 20 45 50 5f 52 65 64  rty(pNew, EP_Red
20b78 75 63 65 64 29 20 29 7b 0a 20 20 20 20 20 20 20  uced) ){.       
20b79 20 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d     pNew->pLeft =
20b7a 20 65 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e   exprDup(db, p->
20b7b 70 4c 65 66 74 2c 20 45 58 50 52 44 55 50 5f 52  pLeft, EXPRDUP_R
20b7c 45 44 55 43 45 2c 20 26 7a 41 6c 6c 6f 63 29 3b  EDUCE, &zAlloc);
20b7d 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d  .          pNew-
20b7e 3e 70 52 69 67 68 74 20 3d 20 65 78 70 72 44 75  >pRight = exprDu
20b7f 70 28 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 2c  p(db, p->pRight,
20b80 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 2c   EXPRDUP_REDUCE,
20b81 20 26 7a 41 6c 6c 6f 63 29 3b 0a 20 20 20 20 20   &zAlloc);.     
20b82 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
20b83 20 70 7a 42 75 66 66 65 72 20 29 7b 0a 20 20 20   pzBuffer ){.   
20b84 20 20 20 20 20 20 20 2a 70 7a 42 75 66 66 65 72         *pzBuffer
20b85 20 3d 20 7a 41 6c 6c 6f 63 3b 0a 20 20 20 20 20   = zAlloc;.     
20b86 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
20b87 7b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e  {.        pNew->
20b88 66 6c 61 67 73 32 20 3d 20 30 3b 0a 20 20 20 20  flags2 = 0;.    
20b89 20 20 20 20 69 66 28 20 21 45 78 70 72 48 61 73      if( !ExprHas
20b8a 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45  AnyProperty(p, E
20b8b 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a  P_TokenOnly) ){.
20b8c 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e            pNew->
20b8d 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45  pLeft = sqlite3E
20b8e 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c  xprDup(db, p->pL
20b8f 65 66 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  eft, 0);.       
20b90 20 20 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 20     pNew->pRight 
20b91 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
20b92 28 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 2c 20  (db, p->pRight, 
20b93 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  0);.        }.  
20b94 20 20 20 20 7d 0a 0a 20 20 20 20 7d 0a 20 20 7d      }..    }.  }
20b95 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a  .  return pNew;.
20b96 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  }../*.** The fol
20b97 6c 6f 77 69 6e 67 20 67 72 6f 75 70 20 6f 66 20  lowing group of 
20b98 72 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 64 65  routines make de
20b99 65 70 20 63 6f 70 69 65 73 20 6f 66 20 65 78 70  ep copies of exp
20b9a 72 65 73 73 69 6f 6e 73 2c 0a 2a 2a 20 65 78 70  ressions,.** exp
20b9b 72 65 73 73 69 6f 6e 20 6c 69 73 74 73 2c 20 49  ression lists, I
20b9c 44 20 6c 69 73 74 73 2c 20 61 6e 64 20 73 65 6c  D lists, and sel
20b9d 65 63 74 20 73 74 61 74 65 6d 65 6e 74 73 2e 20  ect statements. 
20b9e 20 54 68 65 20 63 6f 70 69 65 73 20 63 61 6e 0a   The copies can.
20b9f 2a 2a 20 62 65 20 64 65 6c 65 74 65 64 20 28 62  ** be deleted (b
20ba0 79 20 62 65 69 6e 67 20 70 61 73 73 65 64 20 74  y being passed t
20ba1 6f 20 74 68 65 69 72 20 72 65 73 70 65 63 74 69  o their respecti
20ba2 76 65 20 2e 2e 2e 44 65 6c 65 74 65 28 29 20 72  ve ...Delete() r
20ba3 6f 75 74 69 6e 65 73 29 0a 2a 2a 20 77 69 74 68  outines).** with
20ba4 6f 75 74 20 65 66 66 65 63 74 69 6e 67 20 74 68  out effecting th
20ba5 65 20 6f 72 69 67 69 6e 61 6c 73 2e 0a 2a 2a 0a  e originals..**.
20ba6 2a 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f  ** The expressio
20ba7 6e 20 6c 69 73 74 2c 20 49 44 2c 20 61 6e 64 20  n list, ID, and 
20ba8 73 6f 75 72 63 65 20 6c 69 73 74 73 20 72 65 74  source lists ret
20ba9 75 72 6e 20 62 79 20 73 71 6c 69 74 65 33 45 78  urn by sqlite3Ex
20baa 70 72 4c 69 73 74 44 75 70 28 29 2c 0a 2a 2a 20  prListDup(),.** 
20bab 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70  sqlite3IdListDup
20bac 28 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33 53  (), and sqlite3S
20bad 72 63 4c 69 73 74 44 75 70 28 29 20 63 61 6e 20  rcListDup() can 
20bae 6e 6f 74 20 62 65 20 66 75 72 74 68 65 72 20 65  not be further e
20baf 78 70 61 6e 64 65 64 20 0a 2a 2a 20 62 79 20 73  xpanded .** by s
20bb0 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20  ubsequent calls 
20bb1 74 6f 20 73 71 6c 69 74 65 2a 4c 69 73 74 41 70  to sqlite*ListAp
20bb2 70 65 6e 64 28 29 20 72 6f 75 74 69 6e 65 73 2e  pend() routines.
20bb3 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 74 61 62 6c 65  .**.** Any table
20bb4 73 20 74 68 61 74 20 74 68 65 20 53 72 63 4c 69  s that the SrcLi
20bb5 73 74 20 6d 69 67 68 74 20 70 6f 69 6e 74 20 74  st might point t
20bb6 6f 20 61 72 65 20 6e 6f 74 20 64 75 70 6c 69 63  o are not duplic
20bb7 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ated..**.** The 
20bb8 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20  flags parameter 
20bb9 63 6f 6e 74 61 69 6e 73 20 61 20 63 6f 6d 62 69  contains a combi
20bba 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 45 58  nation of the EX
20bbb 50 52 44 55 50 5f 58 58 58 20 66 6c 61 67 73 2e  PRDUP_XXX flags.
20bbc 0a 2a 2a 20 49 66 20 74 68 65 20 45 58 50 52 44  .** If the EXPRD
20bbd 55 50 5f 52 45 44 55 43 45 20 66 6c 61 67 20 69  UP_REDUCE flag i
20bbe 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20  s set, then the 
20bbf 73 74 72 75 63 74 75 72 65 20 72 65 74 75 72 6e  structure return
20bc0 65 64 20 69 73 20 61 0a 2a 2a 20 74 72 75 6e 63  ed is a.** trunc
20bc1 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20  ated version of 
20bc2 74 68 65 20 75 73 75 61 6c 20 45 78 70 72 20 73  the usual Expr s
20bc3 74 72 75 63 74 75 72 65 20 74 68 61 74 20 77 69  tructure that wi
20bc4 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 61 73 0a  ll be stored as.
20bc5 2a 2a 20 70 61 72 74 20 6f 66 20 74 68 65 20 69  ** part of the i
20bc6 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65  n-memory represe
20bc7 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64  ntation of the d
20bc8 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 0a  atabase schema..
20bc9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
20bca 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45  E Expr *sqlite3E
20bcb 78 70 72 44 75 70 28 73 71 6c 69 74 65 33 20 2a  xprDup(sqlite3 *
20bcc 64 62 2c 20 45 78 70 72 20 2a 70 2c 20 69 6e 74  db, Expr *p, int
20bcd 20 66 6c 61 67 73 29 7b 0a 20 20 72 65 74 75 72   flags){.  retur
20bce 6e 20 65 78 70 72 44 75 70 28 64 62 2c 20 70 2c  n exprDup(db, p,
20bcf 20 66 6c 61 67 73 2c 20 30 29 3b 0a 7d 0a 53 51   flags, 0);.}.SQ
20bd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
20bd1 72 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78  rList *sqlite3Ex
20bd2 70 72 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65  prListDup(sqlite
20bd3 33 20 2a 64 62 2c 20 45 78 70 72 4c 69 73 74 20  3 *db, ExprList 
20bd4 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a  *p, int flags){.
20bd5 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4e 65 77    ExprList *pNew
20bd6 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  ;.  struct ExprL
20bd7 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 2c  ist_item *pItem,
20bd8 20 2a 70 4f 6c 64 49 74 65 6d 3b 0a 20 20 69 6e   *pOldItem;.  in
20bd9 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  t i;.  if( p==0 
20bda 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e  ) return 0;.  pN
20bdb 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ew = sqlite3DbMa
20bdc 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65  llocRaw(db, size
20bdd 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 69  of(*pNew) );.  i
20bde 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74  f( pNew==0 ) ret
20bdf 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 69  urn 0;.  pNew->i
20be0 45 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 70  ECursor = 0;.  p
20be1 4e 65 77 2d 3e 6e 45 78 70 72 20 3d 20 70 4e 65  New->nExpr = pNe
20be2 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e  w->nAlloc = p->n
20be3 45 78 70 72 3b 0a 20 20 70 4e 65 77 2d 3e 61 20  Expr;.  pNew->a 
20be4 3d 20 70 49 74 65 6d 20 3d 20 73 71 6c 69 74 65  = pItem = sqlite
20be5 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
20be6 20 20 70 2d 3e 6e 45 78 70 72 2a 73 69 7a 65 6f    p->nExpr*sizeo
20be7 66 28 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20  f(p->a[0]) );.  
20be8 69 66 28 20 70 49 74 65 6d 3d 3d 30 20 29 7b 0a  if( pItem==0 ){.
20be9 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
20bea 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20  e(db, pNew);.   
20beb 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 20 0a   return 0;.  } .
20bec 20 20 70 4f 6c 64 49 74 65 6d 20 3d 20 70 2d 3e    pOldItem = p->
20bed 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  a;.  for(i=0; i<
20bee 70 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70  p->nExpr; i++, p
20bef 49 74 65 6d 2b 2b 2c 20 70 4f 6c 64 49 74 65 6d  Item++, pOldItem
20bf0 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70  ++){.    Expr *p
20bf1 4f 6c 64 45 78 70 72 20 3d 20 70 4f 6c 64 49 74  OldExpr = pOldIt
20bf2 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 70  em->pExpr;.    p
20bf3 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 73 71  Item->pExpr = sq
20bf4 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
20bf5 20 70 4f 6c 64 45 78 70 72 2c 20 66 6c 61 67 73   pOldExpr, flags
20bf6 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e  );.    pItem->zN
20bf7 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ame = sqlite3DbS
20bf8 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74  trDup(db, pOldIt
20bf9 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  em->zName);.    
20bfa 70 49 74 65 6d 2d 3e 7a 53 70 61 6e 20 3d 20 73  pItem->zSpan = s
20bfb 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
20bfc 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 53 70  b, pOldItem->zSp
20bfd 61 6e 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e  an);.    pItem->
20bfe 73 6f 72 74 4f 72 64 65 72 20 3d 20 70 4f 6c 64  sortOrder = pOld
20bff 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b  Item->sortOrder;
20c00 0a 20 20 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65  .    pItem->done
20c01 20 3d 20 30 3b 0a 20 20 20 20 70 49 74 65 6d 2d   = 0;.    pItem-
20c02 3e 69 43 6f 6c 20 3d 20 70 4f 6c 64 49 74 65 6d  >iCol = pOldItem
20c03 2d 3e 69 43 6f 6c 3b 0a 20 20 20 20 70 49 74 65  ->iCol;.    pIte
20c04 6d 2d 3e 69 41 6c 69 61 73 20 3d 20 70 4f 6c 64  m->iAlias = pOld
20c05 49 74 65 6d 2d 3e 69 41 6c 69 61 73 3b 0a 20 20  Item->iAlias;.  
20c06 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b  }.  return pNew;
20c07 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 75 72  .}../*.** If cur
20c08 73 6f 72 73 2c 20 74 72 69 67 67 65 72 73 2c 20  sors, triggers, 
20c09 76 69 65 77 73 20 61 6e 64 20 73 75 62 71 75 65  views and subque
20c0a 72 69 65 73 20 61 72 65 20 61 6c 6c 20 6f 6d 69  ries are all omi
20c0b 74 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65  tted from.** the
20c0c 20 62 75 69 6c 64 2c 20 74 68 65 6e 20 6e 6f 6e   build, then non
20c0d 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
20c0e 6e 67 20 72 6f 75 74 69 6e 65 73 2c 20 65 78 63  ng routines, exc
20c0f 65 70 74 20 66 6f 72 20 0a 2a 2a 20 73 71 6c 69  ept for .** sqli
20c10 74 65 33 53 65 6c 65 63 74 44 75 70 28 29 2c 20  te3SelectDup(), 
20c11 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 2e 20 73  can be called. s
20c12 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28  qlite3SelectDup(
20c13 29 20 69 73 20 73 6f 6d 65 74 69 6d 65 73 0a 2a  ) is sometimes.*
20c14 2a 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20  * called with a 
20c15 4e 55 4c 4c 20 61 72 67 75 6d 65 6e 74 2e 0a 2a  NULL argument..*
20c16 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
20c17 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
20c18 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
20c19 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
20c1a 29 20 5c 0a 20 7c 7c 20 21 64 65 66 69 6e 65 64  ) \. || !defined
20c1b 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42  (SQLITE_OMIT_SUB
20c1c 51 55 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52  QUERY).SQLITE_PR
20c1d 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73  IVATE SrcList *s
20c1e 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70  qlite3SrcListDup
20c1f 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 72  (sqlite3 *db, Sr
20c20 63 4c 69 73 74 20 2a 70 2c 20 69 6e 74 20 66 6c  cList *p, int fl
20c21 61 67 73 29 7b 0a 20 20 53 72 63 4c 69 73 74 20  ags){.  SrcList 
20c22 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a  *pNew;.  int i;.
20c23 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69    int nByte;.  i
20c24 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
20c25 20 30 3b 0a 20 20 6e 42 79 74 65 20 3d 20 73 69   0;.  nByte = si
20c26 7a 65 6f 66 28 2a 70 29 20 2b 20 28 70 2d 3e 6e  zeof(*p) + (p->n
20c27 53 72 63 3e 30 20 3f 20 73 69 7a 65 6f 66 28 70  Src>0 ? sizeof(p
20c28 2d 3e 61 5b 30 5d 29 20 2a 20 28 70 2d 3e 6e 53  ->a[0]) * (p->nS
20c29 72 63 2d 31 29 20 3a 20 30 29 3b 0a 20 20 70 4e  rc-1) : 0);.  pN
20c2a 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ew = sqlite3DbMa
20c2b 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 42 79 74  llocRaw(db, nByt
20c2c 65 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d  e );.  if( pNew=
20c2d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
20c2e 20 70 4e 65 77 2d 3e 6e 53 72 63 20 3d 20 70 4e   pNew->nSrc = pN
20c2f 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e  ew->nAlloc = p->
20c30 6e 53 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nSrc;.  for(i=0;
20c31 20 69 3c 70 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29   i<p->nSrc; i++)
20c32 7b 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63  {.    struct Src
20c33 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49  List_item *pNewI
20c34 74 65 6d 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69  tem = &pNew->a[i
20c35 5d 3b 0a 20 20 20 20 73 74 72 75 63 74 20 53 72  ];.    struct Sr
20c36 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4f 6c 64  cList_item *pOld
20c37 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b  Item = &p->a[i];
20c38 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
20c39 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e  ;.    pNewItem->
20c3a 7a 44 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69  zDatabase = sqli
20c3b 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
20c3c 70 4f 6c 64 49 74 65 6d 2d 3e 7a 44 61 74 61 62  pOldItem->zDatab
20c3d 61 73 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74  ase);.    pNewIt
20c3e 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69  em->zName = sqli
20c3f 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
20c40 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29  pOldItem->zName)
20c41 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e  ;.    pNewItem->
20c42 7a 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33  zAlias = sqlite3
20c43 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c  DbStrDup(db, pOl
20c44 64 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a  dItem->zAlias);.
20c45 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 6a 6f      pNewItem->jo
20c46 69 6e 74 79 70 65 20 3d 20 70 4f 6c 64 49 74 65  intype = pOldIte
20c47 6d 2d 3e 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 20  m->jointype;.   
20c48 20 70 4e 65 77 49 74 65 6d 2d 3e 69 43 75 72 73   pNewItem->iCurs
20c49 6f 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69  or = pOldItem->i
20c4a 43 75 72 73 6f 72 3b 0a 20 20 20 20 70 4e 65 77  Cursor;.    pNew
20c4b 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65  Item->isPopulate
20c4c 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 73  d = pOldItem->is
20c4d 50 6f 70 75 6c 61 74 65 64 3b 0a 20 20 20 20 70  Populated;.    p
20c4e 4e 65 77 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 20  NewItem->zIndex 
20c4f 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
20c50 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e  p(db, pOldItem->
20c51 7a 49 6e 64 65 78 29 3b 0a 20 20 20 20 70 4e 65  zIndex);.    pNe
20c52 77 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65  wItem->notIndexe
20c53 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6e 6f  d = pOldItem->no
20c54 74 49 6e 64 65 78 65 64 3b 0a 20 20 20 20 70 4e  tIndexed;.    pN
20c55 65 77 49 74 65 6d 2d 3e 70 49 6e 64 65 78 20 3d  ewItem->pIndex =
20c56 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 49 6e 64 65   pOldItem->pInde
20c57 78 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70 4e  x;.    pTab = pN
20c58 65 77 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20 70  ewItem->pTab = p
20c59 4f 6c 64 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20  OldItem->pTab;. 
20c5a 20 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20     if( pTab ){. 
20c5b 20 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b       pTab->nRef+
20c5c 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65  +;.    }.    pNe
20c5d 77 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d  wItem->pSelect =
20c5e 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
20c5f 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e  p(db, pOldItem->
20c60 70 53 65 6c 65 63 74 2c 20 66 6c 61 67 73 29 3b  pSelect, flags);
20c61 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70  .    pNewItem->p
20c62 4f 6e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  On = sqlite3Expr
20c63 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d  Dup(db, pOldItem
20c64 2d 3e 70 4f 6e 2c 20 66 6c 61 67 73 29 3b 0a 20  ->pOn, flags);. 
20c65 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 55 73     pNewItem->pUs
20c66 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 49 64 4c  ing = sqlite3IdL
20c67 69 73 74 44 75 70 28 64 62 2c 20 70 4f 6c 64 49  istDup(db, pOldI
20c68 74 65 6d 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20  tem->pUsing);.  
20c69 20 20 70 4e 65 77 49 74 65 6d 2d 3e 63 6f 6c 55    pNewItem->colU
20c6a 73 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e  sed = pOldItem->
20c6b 63 6f 6c 55 73 65 64 3b 0a 20 20 7d 0a 20 20 72  colUsed;.  }.  r
20c6c 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51  eturn pNew;.}.SQ
20c6d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c  LITE_PRIVATE IdL
20c6e 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69  ist *sqlite3IdLi
20c6f 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64  stDup(sqlite3 *d
20c70 62 2c 20 49 64 4c 69 73 74 20 2a 70 29 7b 0a 20  b, IdList *p){. 
20c71 20 49 64 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20   IdList *pNew;. 
20c72 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d   int i;.  if( p=
20c73 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
20c74 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   pNew = sqlite3D
20c75 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73  bMallocRaw(db, s
20c76 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a  izeof(*pNew) );.
20c77 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20    if( pNew==0 ) 
20c78 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77  return 0;.  pNew
20c79 2d 3e 6e 49 64 20 3d 20 70 4e 65 77 2d 3e 6e 41  ->nId = pNew->nA
20c7a 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 49 64 3b 0a 20  lloc = p->nId;. 
20c7b 20 70 4e 65 77 2d 3e 61 20 3d 20 73 71 6c 69 74   pNew->a = sqlit
20c7c 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
20c7d 2c 20 70 2d 3e 6e 49 64 2a 73 69 7a 65 6f 66 28  , p->nId*sizeof(
20c7e 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66  p->a[0]) );.  if
20c7f 28 20 70 4e 65 77 2d 3e 61 3d 3d 30 20 29 7b 0a  ( pNew->a==0 ){.
20c80 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
20c81 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20  e(db, pNew);.   
20c82 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
20c83 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
20c84 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74  Id; i++){.    st
20c85 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d  ruct IdList_item
20c86 20 2a 70 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e   *pNewItem = &pN
20c87 65 77 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74  ew->a[i];.    st
20c88 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d  ruct IdList_item
20c89 20 2a 70 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d   *pOldItem = &p-
20c8a 3e 61 5b 69 5d 3b 0a 20 20 20 20 70 4e 65 77 49  >a[i];.    pNewI
20c8b 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c  tem->zName = sql
20c8c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
20c8d 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65   pOldItem->zName
20c8e 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d  );.    pNewItem-
20c8f 3e 69 64 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d  >idx = pOldItem-
20c90 3e 69 64 78 3b 0a 20 20 7d 0a 20 20 72 65 74 75  >idx;.  }.  retu
20c91 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54  rn pNew;.}.SQLIT
20c92 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63 74  E_PRIVATE Select
20c93 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44   *sqlite3SelectD
20c94 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  up(sqlite3 *db, 
20c95 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 66  Select *p, int f
20c96 6c 61 67 73 29 7b 0a 20 20 53 65 6c 65 63 74 20  lags){.  Select 
20c97 2a 70 4e 65 77 3b 0a 20 20 69 66 28 20 70 3d 3d  *pNew;.  if( p==
20c98 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
20c99 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  pNew = sqlite3Db
20c9a 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69  MallocRaw(db, si
20c9b 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69 66  zeof(*p) );.  if
20c9c 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75  ( pNew==0 ) retu
20c9d 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 70 45  rn 0;.  pNew->pE
20c9e 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
20c9f 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d  prListDup(db, p-
20ca0 3e 70 45 4c 69 73 74 2c 20 66 6c 61 67 73 29 3b  >pEList, flags);
20ca1 0a 20 20 70 4e 65 77 2d 3e 70 53 72 63 20 3d 20  .  pNew->pSrc = 
20ca2 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75  sqlite3SrcListDu
20ca3 70 28 64 62 2c 20 70 2d 3e 70 53 72 63 2c 20 66  p(db, p->pSrc, f
20ca4 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70  lags);.  pNew->p
20ca5 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45  Where = sqlite3E
20ca6 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 57  xprDup(db, p->pW
20ca7 68 65 72 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20  here, flags);.  
20ca8 70 4e 65 77 2d 3e 70 47 72 6f 75 70 42 79 20 3d  pNew->pGroupBy =
20ca9 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
20caa 44 75 70 28 64 62 2c 20 70 2d 3e 70 47 72 6f 75  Dup(db, p->pGrou
20cab 70 42 79 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70  pBy, flags);.  p
20cac 4e 65 77 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73  New->pHaving = s
20cad 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
20cae 2c 20 70 2d 3e 70 48 61 76 69 6e 67 2c 20 66 6c  , p->pHaving, fl
20caf 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f  ags);.  pNew->pO
20cb0 72 64 65 72 42 79 20 3d 20 73 71 6c 69 74 65 33  rderBy = sqlite3
20cb1 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20  ExprListDup(db, 
20cb2 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 66 6c 61  p->pOrderBy, fla
20cb3 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 6f 70 20  gs);.  pNew->op 
20cb4 3d 20 70 2d 3e 6f 70 3b 0a 20 20 70 4e 65 77 2d  = p->op;.  pNew-
20cb5 3e 70 50 72 69 6f 72 20 3d 20 73 71 6c 69 74 65  >pPrior = sqlite
20cb6 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70  3SelectDup(db, p
20cb7 2d 3e 70 50 72 69 6f 72 2c 20 66 6c 61 67 73 29  ->pPrior, flags)
20cb8 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 69 6d 69 74  ;.  pNew->pLimit
20cb9 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
20cba 70 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 2c  p(db, p->pLimit,
20cbb 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d   flags);.  pNew-
20cbc 3e 70 4f 66 66 73 65 74 20 3d 20 73 71 6c 69 74  >pOffset = sqlit
20cbd 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d  e3ExprDup(db, p-
20cbe 3e 70 4f 66 66 73 65 74 2c 20 66 6c 61 67 73 29  >pOffset, flags)
20cbf 3b 0a 20 20 70 4e 65 77 2d 3e 69 4c 69 6d 69 74  ;.  pNew->iLimit
20cc0 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 69 4f   = 0;.  pNew->iO
20cc1 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 70 4e 65  ffset = 0;.  pNe
20cc2 77 2d 3e 73 65 6c 46 6c 61 67 73 20 3d 20 70 2d  w->selFlags = p-
20cc3 3e 73 65 6c 46 6c 61 67 73 20 26 20 7e 53 46 5f  >selFlags & ~SF_
20cc4 55 73 65 73 45 70 68 65 6d 65 72 61 6c 3b 0a 20  UsesEphemeral;. 
20cc5 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 6d 6f 73   pNew->pRightmos
20cc6 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 61  t = 0;.  pNew->a
20cc7 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d  ddrOpenEphm[0] =
20cc8 20 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64   -1;.  pNew->add
20cc9 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d  rOpenEphm[1] = -
20cca 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f  1;.  pNew->addrO
20ccb 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b  penEphm[2] = -1;
20ccc 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a  .  return pNew;.
20ccd 7d 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50  }.#else.SQLITE_P
20cce 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73  RIVATE Select *s
20ccf 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28  qlite3SelectDup(
20cd0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c  sqlite3 *db, Sel
20cd1 65 63 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67  ect *p, int flag
20cd2 73 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d  s){.  assert( p=
20cd3 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 30  =0 );.  return 0
20cd4 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  ;.}.#endif.../*.
20cd5 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65  ** Add a new ele
20cd6 6d 65 6e 74 20 74 6f 20 74 68 65 20 65 6e 64 20  ment to the end 
20cd7 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  of an expression
20cd8 20 6c 69 73 74 2e 20 20 49 66 20 70 4c 69 73 74   list.  If pList
20cd9 20 69 73 0a 2a 2a 20 69 6e 69 74 69 61 6c 6c 79   is.** initially
20cda 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 63 72 65 61   NULL, then crea
20cdb 74 65 20 61 20 6e 65 77 20 65 78 70 72 65 73 73  te a new express
20cdc 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  ion list..**.** 
20cdd 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  If a memory allo
20cde 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63  cation error occ
20cdf 75 72 73 2c 20 74 68 65 20 65 6e 74 69 72 65 20  urs, the entire 
20ce0 6c 69 73 74 20 69 73 20 66 72 65 65 64 20 61 6e  list is freed an
20ce1 64 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74  d.** NULL is ret
20ce2 75 72 6e 65 64 2e 20 20 49 66 20 6e 6f 6e 2d 4e  urned.  If non-N
20ce3 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2c  ULL is returned,
20ce4 20 74 68 65 6e 20 69 74 20 69 73 20 67 75 61 72   then it is guar
20ce5 61 6e 74 65 65 64 0a 2a 2a 20 74 68 61 74 20 74  anteed.** that t
20ce6 68 65 20 6e 65 77 20 65 6e 74 72 79 20 77 61 73  he new entry was
20ce7 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 70   successfully ap
20ce8 70 65 6e 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  pended..*/.SQLIT
20ce9 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69  E_PRIVATE ExprLi
20cea 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c  st *sqlite3ExprL
20ceb 69 73 74 41 70 70 65 6e 64 28 0a 20 20 50 61 72  istAppend(.  Par
20cec 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
20ced 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
20cee 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70  context */.  Exp
20cef 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20  rList *pList,   
20cf0 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20       /* List to 
20cf1 77 68 69 63 68 20 74 6f 20 61 70 70 65 6e 64 2e  which to append.
20cf2 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a   Might be NULL *
20cf3 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 20  /.  Expr *pExpr 
20cf4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
20cf5 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20  xpression to be 
20cf6 61 70 70 65 6e 64 65 64 2e 20 4d 69 67 68 74 20  appended. Might 
20cf7 62 65 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20  be NULL */.){.  
20cf8 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
20cf9 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20  arse->db;.  if( 
20cfa 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20  pList==0 ){.    
20cfb 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 44  pList = sqlite3D
20cfc 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
20cfd 73 69 7a 65 6f 66 28 45 78 70 72 4c 69 73 74 29  sizeof(ExprList)
20cfe 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73   );.    if( pLis
20cff 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f  t==0 ){.      go
20d00 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d  to no_mem;.    }
20d01 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 69  .    assert( pLi
20d02 73 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 20 29 3b  st->nAlloc==0 );
20d03 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c 69 73 74  .  }.  if( pList
20d04 2d 3e 6e 41 6c 6c 6f 63 3c 3d 70 4c 69 73 74 2d  ->nAlloc<=pList-
20d05 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 73 74  >nExpr ){.    st
20d06 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
20d07 65 6d 20 2a 61 3b 0a 20 20 20 20 69 6e 74 20 6e  em *a;.    int n
20d08 20 3d 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63   = pList->nAlloc
20d09 2a 32 20 2b 20 34 3b 0a 20 20 20 20 61 20 3d 20  *2 + 4;.    a = 
20d0a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
20d0b 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 2c 20 6e  (db, pList->a, n
20d0c 2a 73 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61  *sizeof(pList->a
20d0d 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 61  [0]));.    if( a
20d0e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  ==0 ){.      got
20d0f 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a  o no_mem;.    }.
20d10 20 20 20 20 70 4c 69 73 74 2d 3e 61 20 3d 20 61      pList->a = a
20d11 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c  ;.    pList->nAl
20d12 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  loc = sqlite3DbM
20d13 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 61 29  allocSize(db, a)
20d14 2f 73 69 7a 65 6f 66 28 61 5b 30 5d 29 3b 0a 20  /sizeof(a[0]);. 
20d15 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69   }.  assert( pLi
20d16 73 74 2d 3e 61 21 3d 30 20 29 3b 0a 20 20 69 66  st->a!=0 );.  if
20d17 28 20 31 20 29 7b 0a 20 20 20 20 73 74 72 75 63  ( 1 ){.    struc
20d18 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
20d19 2a 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d  *pItem = &pList-
20d1a 3e 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2b  >a[pList->nExpr+
20d1b 2b 5d 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70  +];.    memset(p
20d1c 49 74 65 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Item, 0, sizeof(
20d1d 2a 70 49 74 65 6d 29 29 3b 0a 20 20 20 20 70 49  *pItem));.    pI
20d1e 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45 78  tem->pExpr = pEx
20d1f 70 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  pr;.  }.  return
20d20 20 70 4c 69 73 74 3b 0a 0a 6e 6f 5f 6d 65 6d 3a   pList;..no_mem:
20d21 20 20 20 20 20 0a 20 20 2f 2a 20 41 76 6f 69 64       .  /* Avoid
20d22 20 6c 65 61 6b 69 6e 67 20 6d 65 6d 6f 72 79 20   leaking memory 
20d23 69 66 20 6d 61 6c 6c 6f 63 20 68 61 73 20 66 61  if malloc has fa
20d24 69 6c 65 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  iled. */.  sqlit
20d25 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
20d26 20 70 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74   pExpr);.  sqlit
20d27 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
20d28 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 72  (db, pList);.  r
20d29 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
20d2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 4c 69  * Set the ExprLi
20d2b 73 74 2e 61 5b 5d 2e 7a 4e 61 6d 65 20 65 6c 65  st.a[].zName ele
20d2c 6d 65 6e 74 20 6f 66 20 74 68 65 20 6d 6f 73 74  ment of the most
20d2d 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20   recently added 
20d2e 69 74 65 6d 0a 2a 2a 20 6f 6e 20 74 68 65 20 65  item.** on the e
20d2f 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a  xpression list..
20d30 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 6d 69 67 68  **.** pList migh
20d31 74 20 62 65 20 4e 55 4c 4c 20 66 6f 6c 6c 6f 77  t be NULL follow
20d32 69 6e 67 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72  ing an OOM error
20d33 2e 20 20 42 75 74 20 70 4e 61 6d 65 20 73 68 6f  .  But pName sho
20d34 75 6c 64 20 6e 65 76 65 72 20 62 65 0a 2a 2a 20  uld never be.** 
20d35 4e 55 4c 4c 2e 20 20 49 66 20 61 20 6d 65 6d 6f  NULL.  If a memo
20d36 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61  ry allocation fa
20d37 69 6c 73 2c 20 74 68 65 20 70 50 61 72 73 65 2d  ils, the pParse-
20d38 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
20d39 64 20 66 6c 61 67 0a 2a 2a 20 69 73 20 73 65 74  d flag.** is set
20d3a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
20d3b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
20d3c 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28  ExprListSetName(
20d3d 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
20d3e 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  ,          /* Pa
20d3f 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
20d40 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  .  ExprList *pLi
20d41 73 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 4c 69  st,        /* Li
20d42 73 74 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61  st to which to a
20d43 64 64 20 74 68 65 20 73 70 61 6e 2e 20 2a 2f 0a  dd the span. */.
20d44 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 2c 20    Token *pName, 
20d45 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
20d46 65 20 74 6f 20 62 65 20 61 64 64 65 64 20 2a 2f  e to be added */
20d47 0a 20 20 69 6e 74 20 64 65 71 75 6f 74 65 20 20  .  int dequote  
20d48 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
20d49 75 65 20 74 6f 20 63 61 75 73 65 20 74 68 65 20  ue to cause the 
20d4a 6e 61 6d 65 20 74 6f 20 62 65 20 64 65 71 75 6f  name to be dequo
20d4b 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65  ted */.){.  asse
20d4c 72 74 28 20 70 4c 69 73 74 21 3d 30 20 7c 7c 20  rt( pList!=0 || 
20d4d 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
20d4e 6f 63 46 61 69 6c 65 64 21 3d 30 20 29 3b 0a 20  ocFailed!=0 );. 
20d4f 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20   if( pList ){.  
20d50 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
20d51 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
20d52 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74     assert( pList
20d53 2d 3e 6e 45 78 70 72 3e 30 20 29 3b 0a 20 20 20  ->nExpr>0 );.   
20d54 20 70 49 74 65 6d 20 3d 20 26 70 4c 69 73 74 2d   pItem = &pList-
20d55 3e 61 5b 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2d  >a[pList->nExpr-
20d56 31 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  1];.    assert( 
20d57 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3d 3d 30 20  pItem->zName==0 
20d58 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e  );.    pItem->zN
20d59 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ame = sqlite3DbS
20d5a 74 72 4e 44 75 70 28 70 50 61 72 73 65 2d 3e 64  trNDup(pParse->d
20d5b 62 2c 20 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61  b, pName->z, pNa
20d5c 6d 65 2d 3e 6e 29 3b 0a 20 20 20 20 69 66 28 20  me->n);.    if( 
20d5d 64 65 71 75 6f 74 65 20 26 26 20 70 49 74 65 6d  dequote && pItem
20d5e 2d 3e 7a 4e 61 6d 65 20 29 20 73 71 6c 69 74 65  ->zName ) sqlite
20d5f 33 44 65 71 75 6f 74 65 28 70 49 74 65 6d 2d 3e  3Dequote(pItem->
20d60 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  zName);.  }.}../
20d61 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70  *.** Set the Exp
20d62 72 4c 69 73 74 2e 61 5b 5d 2e 7a 53 70 61 6e 20  rList.a[].zSpan 
20d63 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 6d  element of the m
20d64 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64  ost recently add
20d65 65 64 20 69 74 65 6d 0a 2a 2a 20 6f 6e 20 74 68  ed item.** on th
20d66 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  e expression lis
20d67 74 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 6d  t..**.** pList m
20d68 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 66 6f 6c  ight be NULL fol
20d69 6c 6f 77 69 6e 67 20 61 6e 20 4f 4f 4d 20 65 72  lowing an OOM er
20d6a 72 6f 72 2e 20 20 42 75 74 20 70 53 70 61 6e 20  ror.  But pSpan 
20d6b 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 0a  should never be.
20d6c 2a 2a 20 4e 55 4c 4c 2e 20 20 49 66 20 61 20 6d  ** NULL.  If a m
20d6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
20d6e 20 66 61 69 6c 73 2c 20 74 68 65 20 70 50 61 72   fails, the pPar
20d6f 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  se->db->mallocFa
20d70 69 6c 65 64 20 66 6c 61 67 0a 2a 2a 20 69 73 20  iled flag.** is 
20d71 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  set..*/.SQLITE_P
20d72 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
20d73 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 53 70  te3ExprListSetSp
20d74 61 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  an(.  Parse *pPa
20d75 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  rse,          /*
20d76 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
20d77 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
20d78 70 4c 69 73 74 2c 20 20 20 20 20 20 20 20 2f 2a  pList,        /*
20d79 20 4c 69 73 74 20 74 6f 20 77 68 69 63 68 20 74   List to which t
20d7a 6f 20 61 64 64 20 74 68 65 20 73 70 61 6e 2e 20  o add the span. 
20d7b 2a 2f 0a 20 20 45 78 70 72 53 70 61 6e 20 2a 70  */.  ExprSpan *p
20d7c 53 70 61 6e 20 20 20 20 20 20 20 20 20 2f 2a 20  Span         /* 
20d7d 54 68 65 20 73 70 61 6e 20 74 6f 20 62 65 20 61  The span to be a
20d7e 64 64 65 64 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  dded */.){.  sql
20d7f 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
20d80 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28  e->db;.  assert(
20d81 20 70 4c 69 73 74 21 3d 30 20 7c 7c 20 64 62 2d   pList!=0 || db-
20d82 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 21 3d 30  >mallocFailed!=0
20d83 20 29 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20   );.  if( pList 
20d84 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45 78  ){.    struct Ex
20d85 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  prList_item *pIt
20d86 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70  em = &pList->a[p
20d87 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 5d 3b 0a  List->nExpr-1];.
20d88 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73      assert( pLis
20d89 74 2d 3e 6e 45 78 70 72 3e 30 20 29 3b 0a 20 20  t->nExpr>0 );.  
20d8a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61    assert( db->ma
20d8b 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 70 49  llocFailed || pI
20d8c 74 65 6d 2d 3e 70 45 78 70 72 3d 3d 70 53 70 61  tem->pExpr==pSpa
20d8d 6e 2d 3e 70 45 78 70 72 20 29 3b 0a 20 20 20 20  n->pExpr );.    
20d8e 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
20d8f 2c 20 70 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b  , pItem->zSpan);
20d90 0a 20 20 20 20 70 49 74 65 6d 2d 3e 7a 53 70 61  .    pItem->zSpa
20d91 6e 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  n = sqlite3DbStr
20d92 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29  NDup(db, (char*)
20d93 70 53 70 61 6e 2d 3e 7a 53 74 61 72 74 2c 0a 20  pSpan->zStart,. 
20d94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20d96 20 20 20 28 69 6e 74 29 28 70 53 70 61 6e 2d 3e     (int)(pSpan->
20d97 7a 45 6e 64 20 2d 20 70 53 70 61 6e 2d 3e 7a 53  zEnd - pSpan->zS
20d98 74 61 72 74 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  tart));.  }.}../
20d99 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72  *.** If the expr
20d9a 65 73 73 69 6f 6e 20 6c 69 73 74 20 70 45 4c 69  ession list pELi
20d9b 73 74 20 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65  st contains more
20d9c 20 74 68 61 6e 20 69 4c 69 6d 69 74 20 65 6c 65   than iLimit ele
20d9d 6d 65 6e 74 73 2c 0a 2a 2a 20 6c 65 61 76 65 20  ments,.** leave 
20d9e 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
20d9f 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53   in pParse..*/.S
20da0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
20da1 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  id sqlite3ExprLi
20da2 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 0a 20  stCheckLength(. 
20da3 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a   Parse *pParse,.
20da4 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
20da5 73 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  st,.  const char
20da6 20 2a 7a 4f 62 6a 65 63 74 0a 29 7b 0a 20 20 69   *zObject.){.  i
20da7 6e 74 20 6d 78 20 3d 20 70 50 61 72 73 65 2d 3e  nt mx = pParse->
20da8 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
20da9 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 3b  E_LIMIT_COLUMN];
20daa 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 45 4c  .  testcase( pEL
20dab 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e  ist && pEList->n
20dac 45 78 70 72 3d 3d 6d 78 20 29 3b 0a 20 20 74 65  Expr==mx );.  te
20dad 73 74 63 61 73 65 28 20 70 45 4c 69 73 74 20 26  stcase( pEList &
20dae 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d  & pEList->nExpr=
20daf 3d 6d 78 2b 31 20 29 3b 0a 20 20 69 66 28 20 70  =mx+1 );.  if( p
20db0 45 4c 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d  EList && pEList-
20db1 3e 6e 45 78 70 72 3e 6d 78 20 29 7b 0a 20 20 20  >nExpr>mx ){.   
20db2 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
20db3 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61  (pParse, "too ma
20db4 6e 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 25 73  ny columns in %s
20db5 22 2c 20 7a 4f 62 6a 65 63 74 29 3b 0a 20 20 7d  ", zObject);.  }
20db6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
20db7 20 61 6e 20 65 6e 74 69 72 65 20 65 78 70 72 65   an entire expre
20db8 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2f 0a 53  ssion list..*/.S
20db9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
20dba 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  id sqlite3ExprLi
20dbb 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33  stDelete(sqlite3
20dbc 20 2a 64 62 2c 20 45 78 70 72 4c 69 73 74 20 2a   *db, ExprList *
20dbd 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b  pList){.  int i;
20dbe 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69  .  struct ExprLi
20dbf 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
20dc0 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
20dc1 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72   return;.  asser
20dc2 74 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30 20 7c  t( pList->a!=0 |
20dc3 7c 20 28 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3d  | (pList->nExpr=
20dc4 3d 30 20 26 26 20 70 4c 69 73 74 2d 3e 6e 41 6c  =0 && pList->nAl
20dc5 6c 6f 63 3d 3d 30 29 20 29 3b 0a 20 20 61 73 73  loc==0) );.  ass
20dc6 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70  ert( pList->nExp
20dc7 72 3c 3d 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63  r<=pList->nAlloc
20dc8 20 29 3b 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d   );.  for(pItem=
20dc9 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69  pList->a, i=0; i
20dca 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  <pList->nExpr; i
20dcb 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
20dcc 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
20dcd 65 74 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70  ete(db, pItem->p
20dce 45 78 70 72 29 3b 0a 20 20 20 20 73 71 6c 69 74  Expr);.    sqlit
20dcf 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 74  e3DbFree(db, pIt
20dd0 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  em->zName);.    
20dd1 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
20dd2 2c 20 70 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b  , pItem->zSpan);
20dd3 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62  .  }.  sqlite3Db
20dd4 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 2d 3e  Free(db, pList->
20dd5 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  a);.  sqlite3DbF
20dd6 72 65 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a  ree(db, pList);.
20dd7 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 72  }../*.** These r
20dd8 6f 75 74 69 6e 65 73 20 61 72 65 20 57 61 6c 6b  outines are Walk
20dd9 65 72 20 63 61 6c 6c 62 61 63 6b 73 2e 20 20 57  er callbacks.  W
20dda 61 6c 6b 65 72 2e 75 2e 70 69 20 69 73 20 61 20  alker.u.pi is a 
20ddb 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e  pointer.** to an
20ddc 20 69 6e 74 65 67 65 72 2e 20 20 54 68 65 73 65   integer.  These
20ddd 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 68   routines are ch
20dde 65 63 6b 69 6e 67 20 61 6e 20 65 78 70 72 65 73  ecking an expres
20ddf 73 69 6f 6e 20 74 6f 20 73 65 65 0a 2a 2a 20 69  sion to see.** i
20de0 66 20 69 74 20 69 73 20 61 20 63 6f 6e 73 74 61  f it is a consta
20de1 6e 74 2e 20 20 53 65 74 20 2a 57 61 6c 6b 65 72  nt.  Set *Walker
20de2 2e 75 2e 70 69 20 74 6f 20 30 20 69 66 20 74 68  .u.pi to 0 if th
20de3 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 0a  e expression is.
20de4 2a 2a 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 2e  ** not constant.
20de5 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c  .**.** These cal
20de6 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 73 20 61  lback routines a
20de7 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  re used to imple
20de8 6d 65 6e 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69  ment the followi
20de9 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71  ng:.**.**     sq
20dea 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
20deb 61 6e 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c  ant().**     sql
20dec 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61  ite3ExprIsConsta
20ded 6e 74 4e 6f 74 4a 6f 69 6e 28 29 0a 2a 2a 20 20  ntNotJoin().**  
20dee 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 73     sqlite3ExprIs
20def 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74 69  ConstantOrFuncti
20df0 6f 6e 28 29 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69  on().**.*/.stati
20df1 63 20 69 6e 74 20 65 78 70 72 4e 6f 64 65 49 73  c int exprNodeIs
20df2 43 6f 6e 73 74 61 6e 74 28 57 61 6c 6b 65 72 20  Constant(Walker 
20df3 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a  *pWalker, Expr *
20df4 70 45 78 70 72 29 7b 0a 0a 20 20 2f 2a 20 49 66  pExpr){..  /* If
20df5 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 69 73   pWalker->u.i is
20df6 20 33 20 74 68 65 6e 20 61 6e 79 20 74 65 72 6d   3 then any term
20df7 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
20df8 6f 6e 20 74 68 61 74 20 63 6f 6d 65 73 20 66 72  on that comes fr
20df9 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 4f 4e 20 6f  om.  ** the ON o
20dfa 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20  r USING clauses 
20dfb 6f 66 20 61 20 6a 6f 69 6e 20 64 69 73 71 75 61  of a join disqua
20dfc 6c 69 66 69 65 73 20 74 68 65 20 65 78 70 72 65  lifies the expre
20dfd 73 73 69 6f 6e 0a 20 20 2a 2a 20 66 72 6f 6d 20  ssion.  ** from 
20dfe 62 65 69 6e 67 20 63 6f 6e 73 69 64 65 72 65 64  being considered
20dff 20 63 6f 6e 73 74 61 6e 74 2e 20 2a 2f 0a 20 20   constant. */.  
20e00 69 66 28 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69  if( pWalker->u.i
20e01 3d 3d 33 20 26 26 20 45 78 70 72 48 61 73 41 6e  ==3 && ExprHasAn
20e02 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  yProperty(pExpr,
20e03 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b   EP_FromJoin) ){
20e04 0a 20 20 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e  .    pWalker->u.
20e05 69 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72  i = 0;.    retur
20e06 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d  n WRC_Abort;.  }
20e07 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 70  ..  switch( pExp
20e08 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 2f 2a 20  r->op ){.    /* 
20e09 43 6f 6e 73 69 64 65 72 20 66 75 6e 63 74 69 6f  Consider functio
20e0a 6e 73 20 74 6f 20 62 65 20 63 6f 6e 73 74 61 6e  ns to be constan
20e0b 74 20 69 66 20 61 6c 6c 20 74 68 65 69 72 20 61  t if all their a
20e0c 72 67 75 6d 65 6e 74 73 20 61 72 65 20 63 6f 6e  rguments are con
20e0d 73 74 61 6e 74 0a 20 20 20 20 2a 2a 20 61 6e 64  stant.    ** and
20e0e 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32   pWalker->u.i==2
20e0f 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   */.    case TK_
20e10 46 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 20 20  FUNCTION:.      
20e11 69 66 28 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69  if( pWalker->u.i
20e12 3d 3d 32 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==2 ) return 0;.
20e13 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68        /* Fall th
20e14 72 6f 75 67 68 20 2a 2f 0a 20 20 20 20 63 61 73  rough */.    cas
20e15 65 20 54 4b 5f 49 44 3a 0a 20 20 20 20 63 61 73  e TK_ID:.    cas
20e16 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20  e TK_COLUMN:.   
20e17 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e   case TK_AGG_FUN
20e18 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20  CTION:.    case 
20e19 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20  TK_AGG_COLUMN:. 
20e1a 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
20e1b 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20  Expr->op==TK_ID 
20e1c 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
20e1d 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  e( pExpr->op==TK
20e1e 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20  _COLUMN );.     
20e1f 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72   testcase( pExpr
20e20 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 46 55 4e  ->op==TK_AGG_FUN
20e21 43 54 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 74  CTION );.      t
20e22 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e  estcase( pExpr->
20e23 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d  op==TK_AGG_COLUM
20e24 4e 20 29 3b 0a 20 20 20 20 20 20 70 57 61 6c 6b  N );.      pWalk
20e25 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20  er->u.i = 0;.   
20e26 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
20e27 6f 72 74 3b 0a 20 20 20 20 64 65 66 61 75 6c 74  ort;.    default
20e28 3a 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  :.      testcase
20e29 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
20e2a 53 45 4c 45 43 54 20 29 3b 20 2f 2a 20 73 65 6c  SELECT ); /* sel
20e2b 65 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e  ectNodeIsConstan
20e2c 74 20 77 69 6c 6c 20 64 69 73 61 6c 6c 6f 77 20  t will disallow 
20e2d 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
20e2e 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  e( pExpr->op==TK
20e2f 5f 45 58 49 53 54 53 20 29 3b 20 2f 2a 20 73 65  _EXISTS ); /* se
20e30 6c 65 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61  lectNodeIsConsta
20e31 6e 74 20 77 69 6c 6c 20 64 69 73 61 6c 6c 6f 77  nt will disallow
20e32 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e   */.      return
20e33 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20   WRC_Continue;. 
20e34 20 7d 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20   }.}.static int 
20e35 73 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f 6e 73  selectNodeIsCons
20e36 74 61 6e 74 28 57 61 6c 6b 65 72 20 2a 70 57 61  tant(Walker *pWa
20e37 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 4e 6f  lker, Select *No
20e38 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44  tUsed){.  UNUSED
20e39 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
20e3a 65 64 29 3b 0a 20 20 70 57 61 6c 6b 65 72 2d 3e  ed);.  pWalker->
20e3b 75 2e 69 20 3d 20 30 3b 0a 20 20 72 65 74 75 72  u.i = 0;.  retur
20e3c 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 73  n WRC_Abort;.}.s
20e3d 74 61 74 69 63 20 69 6e 74 20 65 78 70 72 49 73  tatic int exprIs
20e3e 43 6f 6e 73 74 28 45 78 70 72 20 2a 70 2c 20 69  Const(Expr *p, i
20e3f 6e 74 20 69 6e 69 74 46 6c 61 67 29 7b 0a 20 20  nt initFlag){.  
20e40 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 75 2e  Walker w;.  w.u.
20e41 69 20 3d 20 69 6e 69 74 46 6c 61 67 3b 0a 20 20  i = initFlag;.  
20e42 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20  w.xExprCallback 
20e43 3d 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e 73  = exprNodeIsCons
20e44 74 61 6e 74 3b 0a 20 20 77 2e 78 53 65 6c 65 63  tant;.  w.xSelec
20e45 74 43 61 6c 6c 62 61 63 6b 20 3d 20 73 65 6c 65  tCallback = sele
20e46 63 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74  ctNodeIsConstant
20e47 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  ;.  sqlite3WalkE
20e48 78 70 72 28 26 77 2c 20 70 29 3b 0a 20 20 72 65  xpr(&w, p);.  re
20e49 74 75 72 6e 20 77 2e 75 2e 69 3b 0a 7d 0a 0a 2f  turn w.u.i;.}../
20e4a 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70  *.** Walk an exp
20e4b 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52  ression tree.  R
20e4c 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 65  eturn 1 if the e
20e4d 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e  xpression is con
20e4e 73 74 61 6e 74 0a 2a 2a 20 61 6e 64 20 30 20 69  stant.** and 0 i
20e4f 66 20 69 74 20 69 6e 76 6f 6c 76 65 73 20 76 61  f it involves va
20e50 72 69 61 62 6c 65 73 20 6f 72 20 66 75 6e 63 74  riables or funct
20e51 69 6f 6e 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a  ion calls..**.**
20e52 20 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65   For the purpose
20e53 73 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69  s of this functi
20e54 6f 6e 2c 20 61 20 64 6f 75 62 6c 65 2d 71 75 6f  on, a double-quo
20e55 74 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20  ted string (ex: 
20e56 22 61 62 63 22 29 0a 2a 2a 20 69 73 20 63 6f 6e  "abc").** is con
20e57 73 69 64 65 72 65 64 20 61 20 76 61 72 69 61 62  sidered a variab
20e58 6c 65 20 62 75 74 20 61 20 73 69 6e 67 6c 65 2d  le but a single-
20e59 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 28 65  quoted string (e
20e5a 78 3a 20 27 61 62 63 27 29 20 69 73 0a 2a 2a 20  x: 'abc') is.** 
20e5b 61 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53  a constant..*/.S
20e5c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
20e5d 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 43  t sqlite3ExprIsC
20e5e 6f 6e 73 74 61 6e 74 28 45 78 70 72 20 2a 70 29  onstant(Expr *p)
20e5f 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49  {.  return exprI
20e60 73 43 6f 6e 73 74 28 70 2c 20 31 29 3b 0a 7d 0a  sConst(p, 1);.}.
20e61 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65  ./*.** Walk an e
20e62 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20  xpression tree. 
20e63 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   Return 1 if the
20e64 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63   expression is c
20e65 6f 6e 73 74 61 6e 74 0a 2a 2a 20 74 68 61 74 20  onstant.** that 
20e66 64 6f 65 73 20 6e 6f 20 6f 72 69 67 69 6e 61 74  does no originat
20e67 65 20 66 72 6f 6d 20 74 68 65 20 4f 4e 20 6f 72  e from the ON or
20e68 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 6f   USING clauses o
20e69 66 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 20 52 65 74  f a join..** Ret
20e6a 75 72 6e 20 30 20 69 66 20 69 74 20 69 6e 76 6f  urn 0 if it invo
20e6b 6c 76 65 73 20 76 61 72 69 61 62 6c 65 73 20 6f  lves variables o
20e6c 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73  r function calls
20e6d 20 6f 72 20 74 65 72 6d 73 20 66 72 6f 6d 0a 2a   or terms from.*
20e6e 2a 20 61 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47  * an ON or USING
20e6f 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49   clause..*/.SQLI
20e70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
20e71 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73  qlite3ExprIsCons
20e72 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 45 78 70 72  tantNotJoin(Expr
20e73 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65   *p){.  return e
20e74 78 70 72 49 73 43 6f 6e 73 74 28 70 2c 20 33 29  xprIsConst(p, 3)
20e75 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20  ;.}../*.** Walk 
20e76 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72  an expression tr
20e77 65 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66  ee.  Return 1 if
20e78 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
20e79 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 6f  is constant.** o
20e7a 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c  r a function cal
20e7b 6c 20 77 69 74 68 20 63 6f 6e 73 74 61 6e 74 20  l with constant 
20e7c 61 72 67 75 6d 65 6e 74 73 2e 20 20 52 65 74 75  arguments.  Retu
20e7d 72 6e 20 61 6e 64 20 30 20 69 66 20 74 68 65 72  rn and 0 if ther
20e7e 65 0a 2a 2a 20 61 72 65 20 61 6e 79 20 76 61 72  e.** are any var
20e7f 69 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f  iables..**.** Fo
20e80 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f  r the purposes o
20e81 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c  f this function,
20e82 20 61 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64   a double-quoted
20e83 20 73 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62   string (ex: "ab
20e84 63 22 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64  c").** is consid
20e85 65 72 65 64 20 61 20 76 61 72 69 61 62 6c 65 20  ered a variable 
20e86 62 75 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f  but a single-quo
20e87 74 65 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20  ted string (ex: 
20e88 27 61 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63  'abc') is.** a c
20e89 6f 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  onstant..*/.SQLI
20e8a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
20e8b 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73  qlite3ExprIsCons
20e8c 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45  tantOrFunction(E
20e8d 78 70 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  xpr *p){.  retur
20e8e 6e 20 65 78 70 72 49 73 43 6f 6e 73 74 28 70 2c  n exprIsConst(p,
20e8f 20 32 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66   2);.}../*.** If
20e90 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
20e91 70 20 63 6f 64 65 73 20 61 20 63 6f 6e 73 74 61  p codes a consta
20e92 6e 74 20 69 6e 74 65 67 65 72 20 74 68 61 74 20  nt integer that 
20e93 69 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67 68 0a  is small enough.
20e94 2a 2a 20 74 6f 20 66 69 74 20 69 6e 20 61 20 33  ** to fit in a 3
20e95 32 2d 62 69 74 20 69 6e 74 65 67 65 72 2c 20 72  2-bit integer, r
20e96 65 74 75 72 6e 20 31 20 61 6e 64 20 70 75 74 20  eturn 1 and put 
20e97 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
20e98 20 69 6e 74 65 67 65 72 0a 2a 2a 20 69 6e 20 2a   integer.** in *
20e99 70 56 61 6c 75 65 2e 20 20 49 66 20 74 68 65 20  pValue.  If the 
20e9a 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f  expression is no
20e9b 74 20 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20  t an integer or 
20e9c 69 66 20 69 74 20 69 73 20 74 6f 6f 20 62 69 67  if it is too big
20e9d 0a 2a 2a 20 74 6f 20 66 69 74 20 69 6e 20 61 20  .** to fit in a 
20e9e 73 69 67 6e 65 64 20 33 32 2d 62 69 74 20 69 6e  signed 32-bit in
20e9f 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20 30 20  teger, return 0 
20ea0 61 6e 64 20 6c 65 61 76 65 20 2a 70 56 61 6c 75  and leave *pValu
20ea1 65 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a  e unchanged..*/.
20ea2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
20ea3 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49 73  nt sqlite3ExprIs
20ea4 49 6e 74 65 67 65 72 28 45 78 70 72 20 2a 70 2c  Integer(Expr *p,
20ea5 20 69 6e 74 20 2a 70 56 61 6c 75 65 29 7b 0a 20   int *pValue){. 
20ea6 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 69   int rc = 0;.  i
20ea7 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 45 50  f( p->flags & EP
20ea8 5f 49 6e 74 56 61 6c 75 65 20 29 7b 0a 20 20 20  _IntValue ){.   
20ea9 20 2a 70 56 61 6c 75 65 20 3d 20 70 2d 3e 75 2e   *pValue = p->u.
20eaa 69 56 61 6c 75 65 3b 0a 20 20 20 20 72 65 74 75  iValue;.    retu
20eab 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 73 77 69 74  rn 1;.  }.  swit
20eac 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20  ch( p->op ){.   
20ead 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 47 45 52   case TK_INTEGER
20eae 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  : {.      rc = s
20eaf 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 70  qlite3GetInt32(p
20eb0 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 70 56 61 6c  ->u.zToken, pVal
20eb1 75 65 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ue);.      asser
20eb2 74 28 20 72 63 3d 3d 30 20 29 3b 0a 20 20 20 20  t( rc==0 );.    
20eb3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
20eb4 20 20 20 63 61 73 65 20 54 4b 5f 55 50 4c 55 53     case TK_UPLUS
20eb5 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  : {.      rc = s
20eb6 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65  qlite3ExprIsInte
20eb7 67 65 72 28 70 2d 3e 70 4c 65 66 74 2c 20 70 56  ger(p->pLeft, pV
20eb8 61 6c 75 65 29 3b 0a 20 20 20 20 20 20 62 72 65  alue);.      bre
20eb9 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
20eba 73 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a  se TK_UMINUS: {.
20ebb 20 20 20 20 20 20 69 6e 74 20 76 3b 0a 20 20 20        int v;.   
20ebc 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78     if( sqlite3Ex
20ebd 70 72 49 73 49 6e 74 65 67 65 72 28 70 2d 3e 70  prIsInteger(p->p
20ebe 4c 65 66 74 2c 20 26 76 29 20 29 7b 0a 20 20 20  Left, &v) ){.   
20ebf 20 20 20 20 20 2a 70 56 61 6c 75 65 20 3d 20 2d       *pValue = -
20ec0 76 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  v;.        rc = 
20ec1 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
20ec2 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
20ec3 20 20 64 65 66 61 75 6c 74 3a 20 62 72 65 61 6b    default: break
20ec4 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20 29  ;.  }.  if( rc )
20ec5 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 45 78  {.    assert( Ex
20ec6 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
20ec7 28 70 2c 20 45 50 5f 52 65 64 75 63 65 64 7c 45  (p, EP_Reduced|E
20ec8 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 0a 20 20 20  P_TokenOnly).   
20ec9 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28              || (
20eca 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32 5f  p->flags2 & EP2_
20ecb 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29 3d 3d  MallocedToken)==
20ecc 30 20 29 3b 0a 20 20 20 20 70 2d 3e 6f 70 20 3d  0 );.    p->op =
20ecd 20 54 4b 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20   TK_INTEGER;.   
20ece 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f   p->flags |= EP_
20ecf 49 6e 74 56 61 6c 75 65 3b 0a 20 20 20 20 70 2d  IntValue;.    p-
20ed0 3e 75 2e 69 56 61 6c 75 65 20 3d 20 2a 70 56 61  >u.iValue = *pVa
20ed1 6c 75 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  lue;.  }.  retur
20ed2 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
20ed3 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68  eturn TRUE if th
20ed4 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69  e given string i
20ed5 73 20 61 20 72 6f 77 2d 69 64 20 63 6f 6c 75 6d  s a row-id colum
20ed6 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54  n name..*/.SQLIT
20ed7 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
20ed8 6c 69 74 65 33 49 73 52 6f 77 69 64 28 63 6f 6e  lite3IsRowid(con
20ed9 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69  st char *z){.  i
20eda 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
20edb 70 28 7a 2c 20 22 5f 52 4f 57 49 44 5f 22 29 3d  p(z, "_ROWID_")=
20edc 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  =0 ) return 1;. 
20edd 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
20ede 43 6d 70 28 7a 2c 20 22 52 4f 57 49 44 22 29 3d  Cmp(z, "ROWID")=
20edf 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  =0 ) return 1;. 
20ee0 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
20ee1 43 6d 70 28 7a 2c 20 22 4f 49 44 22 29 3d 3d 30  Cmp(z, "OID")==0
20ee2 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72   ) return 1;.  r
20ee3 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
20ee4 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
20ee5 20 77 65 20 61 72 65 20 61 62 6c 65 20 74 6f 20   we are able to 
20ee6 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20  the IN operator 
20ee7 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20  optimization on 
20ee8 61 0a 2a 2a 20 71 75 65 72 79 20 6f 66 20 74 68  a.** query of th
20ee9 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20  e form.**.**    
20eea 20 20 20 78 20 49 4e 20 28 53 45 4c 45 43 54 20     x IN (SELECT 
20eeb 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 57 68 65 72 65  ...).**.** Where
20eec 20 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e 20 63   the SELECT... c
20eed 6c 61 75 73 65 20 69 73 20 61 73 20 73 70 65 63  lause is as spec
20eee 69 66 69 65 64 20 62 79 20 74 68 65 20 70 61 72  ified by the par
20eef 61 6d 65 74 65 72 20 74 6f 20 74 68 69 73 0a 2a  ameter to this.*
20ef0 2a 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a  * routine..**.**
20ef1 20 54 68 65 20 53 65 6c 65 63 74 20 6f 62 6a 65   The Select obje
20ef2 63 74 20 70 61 73 73 65 64 20 69 6e 20 68 61 73  ct passed in has
20ef3 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70 72   already been pr
20ef4 65 70 72 6f 63 65 73 73 65 64 20 61 6e 64 20 6e  eprocessed and n
20ef5 6f 0a 2a 2a 20 65 72 72 6f 72 73 20 68 61 76 65  o.** errors have
20ef6 20 62 65 65 6e 20 66 6f 75 6e 64 2e 0a 2a 2f 0a   been found..*/.
20ef7 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
20ef8 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 73 74 61  MIT_SUBQUERY.sta
20ef9 74 69 63 20 69 6e 74 20 69 73 43 61 6e 64 69 64  tic int isCandid
20efa 61 74 65 46 6f 72 49 6e 4f 70 74 28 53 65 6c 65  ateForInOpt(Sele
20efb 63 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73  ct *p){.  SrcLis
20efc 74 20 2a 70 53 72 63 3b 0a 20 20 45 78 70 72 4c  t *pSrc;.  ExprL
20efd 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 54  ist *pEList;.  T
20efe 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 69 66  able *pTab;.  if
20eff 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
20f00 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
20f01 20 20 20 20 20 2f 2a 20 72 69 67 68 74 2d 68 61       /* right-ha
20f02 6e 64 20 73 69 64 65 20 6f 66 20 49 4e 20 69 73  nd side of IN is
20f03 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 66 28   SELECT */.  if(
20f04 20 70 2d 3e 70 50 72 69 6f 72 20 29 20 72 65 74   p->pPrior ) ret
20f05 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20  urn 0;          
20f06 20 20 20 20 2f 2a 20 4e 6f 74 20 61 20 63 6f 6d      /* Not a com
20f07 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 2a 2f 0a  pound SELECT */.
20f08 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67    if( p->selFlag
20f09 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74  s & (SF_Distinct
20f0a 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29 20 29  |SF_Aggregate) )
20f0b 7b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  {.    testcase( 
20f0c 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28  (p->selFlags & (
20f0d 53 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41  SF_Distinct|SF_A
20f0e 67 67 72 65 67 61 74 65 29 29 3d 3d 53 46 5f 44  ggregate))==SF_D
20f0f 69 73 74 69 6e 63 74 20 29 3b 0a 20 20 20 20 74  istinct );.    t
20f10 65 73 74 63 61 73 65 28 20 28 70 2d 3e 73 65 6c  estcase( (p->sel
20f11 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74  Flags & (SF_Dist
20f12 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74  inct|SF_Aggregat
20f13 65 29 29 3d 3d 53 46 5f 41 67 67 72 65 67 61 74  e))==SF_Aggregat
20f14 65 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  e );.    return 
20f15 30 3b 20 2f 2a 20 4e 6f 20 44 49 53 54 49 4e 43  0; /* No DISTINC
20f16 54 20 6b 65 79 77 6f 72 64 20 61 6e 64 20 6e 6f  T keyword and no
20f17 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
20f18 69 6f 6e 73 20 2a 2f 0a 20 20 7d 0a 20 20 61 73  ions */.  }.  as
20f19 73 65 72 74 28 20 70 2d 3e 70 47 72 6f 75 70 42  sert( p->pGroupB
20f1a 79 3d 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20  y==0 );         
20f1b 20 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20 47       /* Has no G
20f1c 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a  ROUP BY clause *
20f1d 2f 0a 20 20 69 66 28 20 70 2d 3e 70 4c 69 6d 69  /.  if( p->pLimi
20f1e 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20  t ) return 0;   
20f1f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61             /* Ha
20f20 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73  s no LIMIT claus
20f21 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  e */.  assert( p
20f22 2d 3e 70 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20  ->pOffset==0 ); 
20f23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
20f24 20 4e 6f 20 4c 49 4d 49 54 20 6d 65 61 6e 73 20   No LIMIT means 
20f25 6e 6f 20 4f 46 46 53 45 54 20 2a 2f 0a 20 20 69  no OFFSET */.  i
20f26 66 28 20 70 2d 3e 70 57 68 65 72 65 20 29 20 72  f( p->pWhere ) r
20f27 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
20f28 20 20 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20        /* Has no 
20f29 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
20f2a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63    pSrc = p->pSrc
20f2b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 72 63  ;.  assert( pSrc
20f2c 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 53 72  !=0 );.  if( pSr
20f2d 63 2d 3e 6e 53 72 63 21 3d 31 20 29 20 72 65 74  c->nSrc!=1 ) ret
20f2e 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20  urn 0;          
20f2f 2f 2a 20 53 69 6e 67 6c 65 20 74 65 72 6d 20 69  /* Single term i
20f30 6e 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f  n FROM clause */
20f31 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 61 5b 30  .  if( pSrc->a[0
20f32 5d 2e 70 53 65 6c 65 63 74 20 29 20 72 65 74 75  ].pSelect ) retu
20f33 72 6e 20 30 3b 20 20 20 20 20 2f 2a 20 46 52 4f  rn 0;     /* FRO
20f34 4d 20 69 73 20 6e 6f 74 20 61 20 73 75 62 71 75  M is not a subqu
20f35 65 72 79 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20  ery or view */. 
20f36 20 70 54 61 62 20 3d 20 70 53 72 63 2d 3e 61 5b   pTab = pSrc->a[
20f37 30 5d 2e 70 54 61 62 3b 0a 20 20 69 66 28 20 4e  0].pTab;.  if( N
20f38 45 56 45 52 28 70 54 61 62 3d 3d 30 29 20 29 20  EVER(pTab==0) ) 
20f39 72 65 74 75 72 6e 20 30 3b 0a 20 20 61 73 73 65  return 0;.  asse
20f3a 72 74 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63  rt( pTab->pSelec
20f3b 74 3d 3d 30 20 29 3b 20 20 20 20 20 20 20 20 20  t==0 );         
20f3c 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73     /* FROM claus
20f3d 65 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77 20  e is not a view 
20f3e 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75  */.  if( IsVirtu
20f3f 61 6c 28 70 54 61 62 29 20 29 20 72 65 74 75 72  al(pTab) ) retur
20f40 6e 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 46  n 0;        /* F
20f41 52 4f 4d 20 63 6c 61 75 73 65 20 6e 6f 74 20 61  ROM clause not a
20f42 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a   virtual table *
20f43 2f 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e  /.  pEList = p->
20f44 70 45 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 45  pEList;.  if( pE
20f45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 29  List->nExpr!=1 )
20f46 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20   return 0;      
20f47 20 2f 2a 20 4f 6e 65 20 63 6f 6c 75 6d 6e 20 69   /* One column i
20f48 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
20f49 20 2a 2f 0a 20 20 69 66 28 20 70 45 4c 69 73 74   */.  if( pEList
20f4a 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70  ->a[0].pExpr->op
20f4b 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 72 65  !=TK_COLUMN ) re
20f4c 74 75 72 6e 20 30 3b 20 2f 2a 20 52 65 73 75 6c  turn 0; /* Resul
20f4d 74 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 2a 2f  t is a column */
20f4e 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23  .  return 1;.}.#
20f4f 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
20f50 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f  OMIT_SUBQUERY */
20f51 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
20f52 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62 79  ction is used by
20f53 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
20f54 69 6f 6e 20 6f 66 20 74 68 65 20 49 4e 20 28 2e  ion of the IN (.
20f55 2e 2e 29 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a  ..) operator..**
20f56 20 49 74 27 73 20 6a 6f 62 20 69 73 20 74 6f 20   It's job is to 
20f57 66 69 6e 64 20 6f 72 20 63 72 65 61 74 65 20 61  find or create a
20f58 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72   b-tree structur
20f59 65 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 73  e that may be us
20f5a 65 64 0a 2a 2a 20 65 69 74 68 65 72 20 74 6f 20  ed.** either to 
20f5b 74 65 73 74 20 66 6f 72 20 6d 65 6d 62 65 72 73  test for members
20f5c 68 69 70 20 6f 66 20 74 68 65 20 28 2e 2e 2e 29  hip of the (...)
20f5d 20 73 65 74 20 6f 72 20 74 6f 20 69 74 65 72 61   set or to itera
20f5e 74 65 20 74 68 72 6f 75 67 68 0a 2a 2a 20 69 74  te through.** it
20f5f 73 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69 70 70  s members, skipp
20f60 69 6e 67 20 64 75 70 6c 69 63 61 74 65 73 2e 0a  ing duplicates..
20f61 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20  **.** The index 
20f62 6f 66 20 74 68 65 20 63 75 72 73 6f 72 20 6f 70  of the cursor op
20f63 65 6e 65 64 20 6f 6e 20 74 68 65 20 62 2d 74 72  ened on the b-tr
20f64 65 65 20 28 64 61 74 61 62 61 73 65 20 74 61 62  ee (database tab
20f65 6c 65 2c 20 64 61 74 61 62 61 73 65 20 69 6e 64  le, database ind
20f66 65 78 20 0a 2a 2a 20 6f 72 20 65 70 68 65 72 6d  ex .** or epherm
20f67 61 6c 20 74 61 62 6c 65 29 20 69 73 20 73 74 6f  al table) is sto
20f68 72 65 64 20 69 6e 20 70 58 2d 3e 69 54 61 62 6c  red in pX->iTabl
20f69 65 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 75  e before this fu
20f6a 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a  nction returns..
20f6b 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20  ** The returned 
20f6c 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75  value of this fu
20f6d 6e 63 74 69 6f 6e 20 69 6e 64 69 63 61 74 65 73  nction indicates
20f6e 20 74 68 65 20 62 2d 74 72 65 65 20 74 79 70 65   the b-tree type
20f6f 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  , as follows:.**
20f70 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f 52  .**   IN_INDEX_R
20f71 4f 57 49 44 20 2d 20 54 68 65 20 63 75 72 73 6f  OWID - The curso
20f72 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20  r was opened on 
20f73 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  a database table
20f74 2e 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58 5f  ..**   IN_INDEX_
20f75 49 4e 44 45 58 20 2d 20 54 68 65 20 63 75 72 73  INDEX - The curs
20f76 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e  or was opened on
20f77 20 61 20 64 61 74 61 62 61 73 65 20 69 6e 64 65   a database inde
20f78 78 2e 0a 2a 2a 20 20 20 49 4e 5f 49 4e 44 45 58  x..**   IN_INDEX
20f79 5f 45 50 48 20 2d 20 20 20 54 68 65 20 63 75 72  _EPH -   The cur
20f7a 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f  sor was opened o
20f7b 6e 20 61 20 73 70 65 63 69 61 6c 6c 79 20 63 72  n a specially cr
20f7c 65 61 74 65 64 20 61 6e 64 0a 2a 2a 20 20 20 20  eated and.**    
20f7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20f7e 70 6f 70 75 6c 61 74 65 64 20 65 70 68 65 72 65  populated ephere
20f7f 6d 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  mal table..**.**
20f80 20 41 6e 20 65 78 69 73 74 69 6e 67 20 62 2d 74   An existing b-t
20f81 72 65 65 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20  ree may only be 
20f82 75 73 65 64 20 69 66 20 74 68 65 20 53 45 4c 45  used if the SELE
20f83 43 54 20 69 73 20 6f 66 20 74 68 65 20 73 69 6d  CT is of the sim
20f84 70 6c 65 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a  ple.** form:.**.
20f85 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 3c 63  **     SELECT <c
20f86 6f 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62  olumn> FROM <tab
20f87 6c 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  le>.**.** If the
20f88 20 70 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61   prNotFound para
20f89 6d 65 74 65 72 20 69 73 20 30 2c 20 74 68 65 6e  meter is 0, then
20f8a 20 74 68 65 20 62 2d 74 72 65 65 20 77 69 6c 6c   the b-tree will
20f8b 20 62 65 20 75 73 65 64 20 74 6f 20 69 74 65 72   be used to iter
20f8c 61 74 65 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74  ate.** through t
20f8d 68 65 20 73 65 74 20 6d 65 6d 62 65 72 73 2c 20  he set members, 
20f8e 73 6b 69 70 70 69 6e 67 20 61 6e 79 20 64 75 70  skipping any dup
20f8f 6c 69 63 61 74 65 73 2e 20 49 6e 20 74 68 69 73  licates. In this
20f90 20 63 61 73 65 20 61 6e 0a 2a 2a 20 65 70 68 65   case an.** ephe
20f91 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73 74  remal table must
20f92 20 62 65 20 75 73 65 64 20 75 6e 6c 65 73 73 20   be used unless 
20f93 74 68 65 20 73 65 6c 65 63 74 65 64 20 3c 63 6f  the selected <co
20f94 6c 75 6d 6e 3e 20 69 73 20 67 75 61 72 61 6e 74  lumn> is guarant
20f95 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 75 6e 69  eed.** to be uni
20f96 71 75 65 20 2d 20 65 69 74 68 65 72 20 62 65 63  que - either bec
20f97 61 75 73 65 20 69 74 20 69 73 20 61 6e 20 49 4e  ause it is an IN
20f98 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
20f99 59 20 6f 72 20 69 74 0a 2a 2a 20 68 61 73 20 61  Y or it.** has a
20f9a 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69   UNIQUE constrai
20f9b 6e 74 20 6f 72 20 55 4e 49 51 55 45 20 69 6e 64  nt or UNIQUE ind
20f9c 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ex..**.** If the
20f9d 20 70 72 4e 6f 74 46 6f 75 6e 64 20 70 61 72 61   prNotFound para
20f9e 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20  meter is not 0, 
20f9f 74 68 65 6e 20 74 68 65 20 62 2d 74 72 65 65 20  then the b-tree 
20fa0 77 69 6c 6c 20 62 65 20 75 73 65 64 20 0a 2a 2a  will be used .**
20fa1 20 66 6f 72 20 66 61 73 74 20 73 65 74 20 6d 65   for fast set me
20fa2 6d 62 65 72 73 68 69 70 20 74 65 73 74 73 2e 20  mbership tests. 
20fa3 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e 20  In this case an 
20fa4 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65 20  epheremal table 
20fa5 6d 75 73 74 20 0a 2a 2a 20 62 65 20 75 73 65 64  must .** be used
20fa6 20 75 6e 6c 65 73 73 20 3c 63 6f 6c 75 6d 6e 3e   unless <column>
20fa7 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52 20 50   is an INTEGER P
20fa8 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 61 6e  RIMARY KEY or an
20fa9 20 69 6e 64 65 78 20 63 61 6e 20 0a 2a 2a 20 62   index can .** b
20faa 65 20 66 6f 75 6e 64 20 77 69 74 68 20 3c 63 6f  e found with <co
20fab 6c 75 6d 6e 3e 20 61 73 20 69 74 73 20 6c 65 66  lumn> as its lef
20fac 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 2e 0a 2a  t-most column..*
20fad 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 62 2d  *.** When the b-
20fae 74 72 65 65 20 69 73 20 62 65 69 6e 67 20 75 73  tree is being us
20faf 65 64 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69  ed for membershi
20fb0 70 20 74 65 73 74 73 2c 20 74 68 65 20 63 61 6c  p tests, the cal
20fb1 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  ling function.**
20fb2 20 6e 65 65 64 73 20 74 6f 20 6b 6e 6f 77 20 77   needs to know w
20fb3 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
20fb4 65 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  e structure cont
20fb5 61 69 6e 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c  ains an SQL NULL
20fb6 20 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20 6f 72   .** value in or
20fb7 64 65 72 20 74 6f 20 63 6f 72 72 65 63 74 6c 79  der to correctly
20fb8 20 65 76 61 6c 75 61 74 65 20 65 78 70 72 65 73   evaluate expres
20fb9 73 69 6f 6e 73 20 6c 69 6b 65 20 22 58 20 49 4e  sions like "X IN
20fba 20 28 59 2c 20 5a 29 22 2e 0a 2a 2a 20 49 66 20   (Y, Z)"..** If 
20fbb 74 68 65 72 65 20 69 73 20 61 20 63 68 61 6e 63  there is a chanc
20fbc 65 20 74 68 61 74 20 74 68 65 20 62 2d 74 72 65  e that the b-tre
20fbd 65 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20  e might contain 
20fbe 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20 61 74 0a  a NULL value at.
20fbf 2a 2a 20 72 75 6e 74 69 6d 65 2c 20 74 68 65 6e  ** runtime, then
20fc0 20 61 20 72 65 67 69 73 74 65 72 20 69 73 20 61   a register is a
20fc1 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68 65  llocated and the
20fc2 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72   register number
20fc3 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a   written.** to *
20fc4 70 72 4e 6f 74 46 6f 75 6e 64 2e 20 49 66 20 74  prNotFound. If t
20fc5 68 65 72 65 20 69 73 20 6e 6f 20 63 68 61 6e 63  here is no chanc
20fc6 65 20 74 68 61 74 20 74 68 65 20 62 2d 74 72 65  e that the b-tre
20fc7 65 20 63 6f 6e 74 61 69 6e 73 20 61 0a 2a 2a 20  e contains a.** 
20fc8 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 74 68 65 6e  NULL value, then
20fc9 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 69 73 20   *prNotFound is 
20fca 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a  left unchanged..
20fcb 2a 2a 0a 2a 2a 20 49 66 20 61 20 72 65 67 69 73  **.** If a regis
20fcc 74 65 72 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ter is allocated
20fcd 20 61 6e 64 20 69 74 73 20 6c 6f 63 61 74 69 6f   and its locatio
20fce 6e 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 72 4e  n stored in *prN
20fcf 6f 74 46 6f 75 6e 64 2c 20 74 68 65 6e 0a 2a 2a  otFound, then.**
20fd0 20 69 74 73 20 69 6e 69 74 69 61 6c 20 76 61 6c   its initial val
20fd1 75 65 20 69 73 20 4e 55 4c 4c 2e 20 49 66 20 74  ue is NULL. If t
20fd2 68 65 20 62 2d 74 72 65 65 20 64 6f 65 73 20 6e  he b-tree does n
20fd3 6f 74 20 72 65 6d 61 69 6e 20 63 6f 6e 73 74 61  ot remain consta
20fd4 6e 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 75  nt.** for the du
20fd5 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75  ration of the qu
20fd6 65 72 79 20 28 69 2e 65 2e 20 74 68 65 20 53 45  ery (i.e. the SE
20fd7 4c 45 43 54 20 74 68 61 74 20 67 65 6e 65 72 61  LECT that genera
20fd8 74 65 73 20 74 68 65 20 62 2d 74 72 65 65 0a 2a  tes the b-tree.*
20fd9 2a 20 69 73 20 61 20 63 6f 72 72 65 6c 61 74 65  * is a correlate
20fda 64 20 73 75 62 71 75 65 72 79 29 20 74 68 65 6e  d subquery) then
20fdb 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
20fdc 65 20 61 6c 6c 6f 63 61 74 65 64 20 72 65 67 69  e allocated regi
20fdd 73 74 65 72 20 69 73 0a 2a 2a 20 72 65 73 65 74  ster is.** reset
20fde 20 74 6f 20 4e 55 4c 4c 20 65 61 63 68 20 74 69   to NULL each ti
20fdf 6d 65 20 74 68 65 20 62 2d 74 72 65 65 20 69 73  me the b-tree is
20fe0 20 72 65 70 6f 70 75 6c 61 74 65 64 2e 20 54 68   repopulated. Th
20fe1 69 73 20 61 6c 6c 6f 77 73 20 74 68 65 0a 2a 2a  is allows the.**
20fe2 20 63 61 6c 6c 65 72 20 74 6f 20 75 73 65 20 76   caller to use v
20fe3 64 62 65 20 63 6f 64 65 20 65 71 75 69 76 61 6c  dbe code equival
20fe4 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f  ent to the follo
20fe5 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66  wing:.**.**   if
20fe6 28 20 72 65 67 69 73 74 65 72 3d 3d 4e 55 4c 4c  ( register==NULL
20fe7 20 29 7b 0a 2a 2a 20 20 20 20 20 68 61 73 5f 6e   ){.**     has_n
20fe8 75 6c 6c 20 3d 20 3c 74 65 73 74 20 69 66 20 64  ull = <test if d
20fe9 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63 6f  ata structure co
20fea 6e 74 61 69 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a 20  ntains null>.** 
20feb 20 20 20 20 72 65 67 69 73 74 65 72 20 3d 20 31      register = 1
20fec 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 69 6e  .**   }.**.** in
20fed 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20   order to avoid 
20fee 72 75 6e 6e 69 6e 67 20 74 68 65 20 3c 74 65 73  running the <tes
20fef 74 20 69 66 20 64 61 74 61 20 73 74 72 75 63 74  t if data struct
20ff0 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75 6c  ure contains nul
20ff1 6c 3e 0a 2a 2a 20 74 65 73 74 20 6d 6f 72 65 20  l>.** test more 
20ff2 6f 66 74 65 6e 20 74 68 61 6e 20 69 73 20 6e 65  often than is ne
20ff3 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e  cessary..*/.#ifn
20ff4 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
20ff5 53 55 42 51 55 45 52 59 0a 53 51 4c 49 54 45 5f  SUBQUERY.SQLITE_
20ff6 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
20ff7 74 65 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 50  te3FindInIndex(P
20ff8 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
20ff9 70 72 20 2a 70 58 2c 20 69 6e 74 20 2a 70 72 4e  pr *pX, int *prN
20ffa 6f 74 46 6f 75 6e 64 29 7b 0a 20 20 53 65 6c 65  otFound){.  Sele
20ffb 63 74 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20  ct *p;          
20ffc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20ffd 20 20 2f 2a 20 53 45 4c 45 43 54 20 74 6f 20 74    /* SELECT to t
20ffe 68 65 20 72 69 67 68 74 20 6f 66 20 49 4e 20 6f  he right of IN o
20fff 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74  perator */.  int
21000 20 65 54 79 70 65 20 3d 20 30 3b 20 20 20 20 20   eType = 0;     
21001 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21002 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 52 48     /* Type of RH
21003 53 20 74 61 62 6c 65 2e 20 49 4e 5f 49 4e 44 45  S table. IN_INDE
21004 58 5f 2a 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  X_* */.  int iTa
21005 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  b = pParse->nTab
21006 2b 2b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ++;            /
21007 2a 20 43 75 72 73 6f 72 20 6f 66 20 74 68 65 20  * Cursor of the 
21008 52 48 53 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  RHS table */.  i
21009 6e 74 20 6d 75 73 74 42 65 55 6e 69 71 75 65 20  nt mustBeUnique 
2100a 3d 20 28 70 72 4e 6f 74 46 6f 75 6e 64 3d 3d 30  = (prNotFound==0
2100b 29 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20  );   /* True if 
2100c 52 48 53 20 6d 75 73 74 20 62 65 20 75 6e 69 71  RHS must be uniq
2100d 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 43 68 65 63  ue */..  /* Chec
2100e 6b 20 74 6f 20 73 65 65 20 69 66 20 61 6e 20 65  k to see if an e
2100f 78 69 73 74 69 6e 67 20 74 61 62 6c 65 20 6f 72  xisting table or
21010 20 69 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73   index can be us
21011 65 64 20 74 6f 0a 20 20 2a 2a 20 73 61 74 69 73  ed to.  ** satis
21012 66 79 20 74 68 65 20 71 75 65 72 79 2e 20 20 54  fy the query.  T
21013 68 69 73 20 69 73 20 70 72 65 66 65 72 61 62 6c  his is preferabl
21014 65 20 74 6f 20 67 65 6e 65 72 61 74 69 6e 67 20  e to generating 
21015 61 20 6e 65 77 20 0a 20 20 2a 2a 20 65 70 68 65  a new .  ** ephe
21016 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a  meral table..  *
21017 2f 0a 20 20 70 20 3d 20 28 45 78 70 72 48 61 73  /.  p = (ExprHas
21018 50 72 6f 70 65 72 74 79 28 70 58 2c 20 45 50 5f  Property(pX, EP_
21019 78 49 73 53 65 6c 65 63 74 29 20 3f 20 70 58 2d  xIsSelect) ? pX-
2101a 3e 78 2e 70 53 65 6c 65 63 74 20 3a 20 30 29 3b  >x.pSelect : 0);
2101b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 50  .  if( ALWAYS(pP
2101c 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 29 20 26  arse->nErr==0) &
2101d 26 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f 72  & isCandidateFor
2101e 49 6e 4f 70 74 28 70 29 20 29 7b 0a 20 20 20 20  InOpt(p) ){.    
2101f 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
21020 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20  arse->db;       
21021 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
21022 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
21023 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72  .    Expr *pExpr
21024 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b   = p->pEList->a[
21025 30 5d 2e 70 45 78 70 72 3b 20 20 20 2f 2a 20 45  0].pExpr;   /* E
21026 78 70 72 65 73 73 69 6f 6e 20 3c 63 6f 6c 75 6d  xpression <colum
21027 6e 3e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43  n> */.    int iC
21028 6f 6c 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c  ol = pExpr->iCol
21029 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  umn;            
2102a 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c   /* Index of col
2102b 75 6d 6e 20 3c 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a  umn <column> */.
2102c 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71      Vdbe *v = sq
2102d 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
2102e 72 73 65 29 3b 20 20 20 20 20 20 2f 2a 20 56 69  rse);      /* Vi
2102f 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 62 65  rtual machine be
21030 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 20  ing coded */.   
21031 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
21032 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61  ->pSrc->a[0].pTa
21033 62 3b 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65  b;      /* Table
21034 20 3c 74 61 62 6c 65 3e 2e 20 2a 2f 0a 20 20 20   <table>. */.   
21035 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
21036 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21037 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
21038 61 73 65 20 69 64 78 20 66 6f 72 20 70 54 61 62  ase idx for pTab
21039 20 2a 2f 0a 20 20 20 0a 20 20 20 20 2f 2a 20 43   */.   .    /* C
2103a 6f 64 65 20 61 6e 20 4f 50 5f 56 65 72 69 66 79  ode an OP_Verify
2103b 43 6f 6f 6b 69 65 20 61 6e 64 20 4f 50 5f 54 61  Cookie and OP_Ta
2103c 62 6c 65 4c 6f 63 6b 20 66 6f 72 20 3c 74 61 62  bleLock for <tab
2103d 6c 65 3e 2e 20 2a 2f 0a 20 20 20 20 69 44 62 20  le>. */.    iDb 
2103e 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
2103f 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d  oIndex(db, pTab-
21040 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 73  >pSchema);.    s
21041 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79  qlite3CodeVerify
21042 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69  Schema(pParse, i
21043 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
21044 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65  TableLock(pParse
21045 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75  , iDb, pTab->tnu
21046 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  m, 0, pTab->zNam
21047 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73  e);..    /* This
21048 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c   function is onl
21049 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 77  y called from tw
2104a 6f 20 70 6c 61 63 65 73 2e 20 49 6e 20 62 6f 74  o places. In bot
2104b 68 20 63 61 73 65 73 20 74 68 65 20 76 64 62 65  h cases the vdbe
2104c 0a 20 20 20 20 2a 2a 20 68 61 73 20 61 6c 72 65  .    ** has alre
2104d 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74  ady been allocat
2104e 65 64 2e 20 53 6f 20 61 73 73 75 6d 65 20 73 71  ed. So assume sq
2104f 6c 69 74 65 33 47 65 74 56 64 62 65 28 29 20 69  lite3GetVdbe() i
21050 73 20 61 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20  s always.    ** 
21051 73 75 63 63 65 73 73 66 75 6c 20 68 65 72 65 2e  successful here.
21052 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
21053 72 74 28 76 29 3b 0a 20 20 20 20 69 66 28 20 69  rt(v);.    if( i
21054 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 69  Col<0 ){.      i
21055 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72  nt iMem = ++pPar
21056 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20  se->nMem;.      
21057 69 6e 74 20 69 41 64 64 72 3b 0a 0a 20 20 20 20  int iAddr;..    
21058 20 20 69 41 64 64 72 20 3d 20 73 71 6c 69 74 65    iAddr = sqlite
21059 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2105a 50 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a 20 20 20  P_If, iMem);.   
2105b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2105c 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2105d 65 72 2c 20 31 2c 20 69 4d 65 6d 29 3b 0a 0a 20  er, 1, iMem);.. 
2105e 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e       sqlite3Open
2105f 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 54  Table(pParse, iT
21060 61 62 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f  ab, iDb, pTab, O
21061 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 20  P_OpenRead);.   
21062 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e     eType = IN_IN
21063 44 45 58 5f 52 4f 57 49 44 3b 0a 0a 20 20 20 20  DEX_ROWID;..    
21064 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
21065 70 48 65 72 65 28 76 2c 20 69 41 64 64 72 29 3b  pHere(v, iAddr);
21066 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
21067 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20    Index *pIdx;  
21068 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21069 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74         /* Iterat
2106a 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 0a  or variable */..
2106b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c        /* The col
2106c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
2106d 75 73 65 64 20 62 79 20 74 68 65 20 63 6f 6d 70  used by the comp
2106e 61 72 69 73 6f 6e 2e 20 49 66 20 61 6e 20 69 6e  arison. If an in
2106f 64 65 78 20 69 73 20 74 6f 0a 20 20 20 20 20 20  dex is to.      
21070 2a 2a 20 62 65 20 75 73 65 64 20 69 6e 20 70 6c  ** be used in pl
21071 61 63 65 20 6f 66 20 61 20 74 65 6d 70 2d 74 61  ace of a temp-ta
21072 62 6c 65 2c 20 69 74 20 6d 75 73 74 20 62 65 20  ble, it must be 
21073 6f 72 64 65 72 65 64 20 61 63 63 6f 72 64 69 6e  ordered accordin
21074 67 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68  g.      ** to th
21075 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  is collation seq
21076 75 65 6e 63 65 2e 20 20 2a 2f 0a 20 20 20 20 20  uence.  */.     
21077 20 43 6f 6c 6c 53 65 71 20 2a 70 52 65 71 20 3d   CollSeq *pReq =
21078 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f   sqlite3BinaryCo
21079 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61  mpareCollSeq(pPa
2107a 72 73 65 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20  rse, pX->pLeft, 
2107b 70 45 78 70 72 29 3b 0a 0a 20 20 20 20 20 20 2f  pExpr);..      /
2107c 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  * Check that the
2107d 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 77   affinity that w
2107e 69 6c 6c 20 62 65 20 75 73 65 64 20 74 6f 20 70  ill be used to p
2107f 65 72 66 6f 72 6d 20 74 68 65 20 0a 20 20 20 20  erform the .    
21080 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20    ** comparison 
21081 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  is the same as t
21082 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74  he affinity of t
21083 68 65 20 63 6f 6c 75 6d 6e 2e 20 49 66 0a 20 20  he column. If.  
21084 20 20 20 20 2a 2a 20 69 74 20 69 73 20 6e 6f 74      ** it is not
21085 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  , it is not poss
21086 69 62 6c 65 20 74 6f 20 75 73 65 20 61 6e 79 20  ible to use any 
21087 69 6e 64 65 78 2e 0a 20 20 20 20 20 20 2a 2f 0a  index..      */.
21088 20 20 20 20 20 20 63 68 61 72 20 61 66 66 20 3d        char aff =
21089 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e   comparisonAffin
2108a 69 74 79 28 70 58 29 3b 0a 20 20 20 20 20 20 69  ity(pX);.      i
2108b 6e 74 20 61 66 66 69 6e 69 74 79 5f 6f 6b 20 3d  nt affinity_ok =
2108c 20 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f   (pTab->aCol[iCo
2108d 6c 5d 2e 61 66 66 69 6e 69 74 79 3d 3d 61 66 66  l].affinity==aff
2108e 7c 7c 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46  ||aff==SQLITE_AF
2108f 46 5f 4e 4f 4e 45 29 3b 0a 0a 20 20 20 20 20 20  F_NONE);..      
21090 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70  for(pIdx=pTab->p
21091 49 6e 64 65 78 3b 20 70 49 64 78 20 26 26 20 65  Index; pIdx && e
21092 54 79 70 65 3d 3d 30 20 26 26 20 61 66 66 69 6e  Type==0 && affin
21093 69 74 79 5f 6f 6b 3b 20 70 49 64 78 3d 70 49 64  ity_ok; pIdx=pId
21094 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20  x->pNext){.     
21095 20 20 20 69 66 28 20 28 70 49 64 78 2d 3e 61 69     if( (pIdx->ai
21096 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69 43 6f 6c 29  Column[0]==iCol)
21097 0a 20 20 20 20 20 20 20 20 20 26 26 20 73 71 6c  .         && sql
21098 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
21099 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 70 49 64  db, ENC(db), pId
2109a 78 2d 3e 61 7a 43 6f 6c 6c 5b 30 5d 2c 20 30 29  x->azColl[0], 0)
2109b 3d 3d 70 52 65 71 0a 20 20 20 20 20 20 20 20 20  ==pReq.         
2109c 26 26 20 28 21 6d 75 73 74 42 65 55 6e 69 71 75  && (!mustBeUniqu
2109d 65 20 7c 7c 20 28 70 49 64 78 2d 3e 6e 43 6f 6c  e || (pIdx->nCol
2109e 75 6d 6e 3d 3d 31 20 26 26 20 70 49 64 78 2d 3e  umn==1 && pIdx->
2109f 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65  onError!=OE_None
210a0 29 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20  )).        ){.  
210a1 20 20 20 20 20 20 20 20 69 6e 74 20 69 4d 65 6d          int iMem
210a2 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
210a3 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  m;.          int
210a4 20 69 41 64 64 72 3b 0a 20 20 20 20 20 20 20 20   iAddr;.        
210a5 20 20 63 68 61 72 20 2a 70 4b 65 79 3b 0a 20 20    char *pKey;.  
210a6 0a 20 20 20 20 20 20 20 20 20 20 70 4b 65 79 20  .          pKey 
210a7 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65  = (char *)sqlite
210a8 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50  3IndexKeyinfo(pP
210a9 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20  arse, pIdx);.   
210aa 20 20 20 20 20 20 20 69 41 64 64 72 20 3d 20 73         iAddr = s
210ab 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
210ac 28 76 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29  (v, OP_If, iMem)
210ad 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
210ae 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
210af 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20   OP_Integer, 1, 
210b0 69 4d 65 6d 29 3b 0a 20 20 0a 20 20 20 20 20 20  iMem);.  .      
210b1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
210b2 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp4(v, OP_Open
210b3 52 65 61 64 2c 20 69 54 61 62 2c 20 70 49 64 78  Read, iTab, pIdx
210b4 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20  ->tnum, iDb,.   
210b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
210b6 20 20 20 20 20 20 20 20 20 20 20 20 70 4b 65 79              pKey
210b7 2c 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44  ,P4_KEYINFO_HAND
210b8 4f 46 46 29 3b 0a 20 20 20 20 20 20 20 20 20 20  OFF);.          
210b9 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
210ba 22 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d  "%s", pIdx->zNam
210bb 65 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 65  e));.          e
210bc 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f  Type = IN_INDEX_
210bd 49 4e 44 45 58 3b 0a 0a 20 20 20 20 20 20 20 20  INDEX;..        
210be 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
210bf 70 48 65 72 65 28 76 2c 20 69 41 64 64 72 29 3b  pHere(v, iAddr);
210c0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
210c1 72 4e 6f 74 46 6f 75 6e 64 20 26 26 20 21 70 54  rNotFound && !pT
210c2 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 6e  ab->aCol[iCol].n
210c3 6f 74 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20  otNull ){.      
210c4 20 20 20 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e        *prNotFoun
210c5 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  d = ++pParse->nM
210c6 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  em;.          }.
210c7 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
210c8 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69  }.    }.  }..  i
210c9 66 28 20 65 54 79 70 65 3d 3d 30 20 29 7b 0a 20  f( eType==0 ){. 
210ca 20 20 20 2f 2a 20 43 6f 75 6c 64 20 6e 6f 74 20     /* Could not 
210cb 66 6f 75 6e 64 20 61 6e 20 65 78 69 73 74 69 6e  found an existin
210cc 67 20 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20  g able or index 
210cd 74 6f 20 75 73 65 20 61 73 20 74 68 65 20 52 48  to use as the RH
210ce 53 20 62 2d 74 72 65 65 2e 0a 20 20 20 20 2a 2a  S b-tree..    **
210cf 20 57 65 20 77 69 6c 6c 20 68 61 76 65 20 74 6f   We will have to
210d0 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65 70 68   generate an eph
210d1 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 74 6f 20  emeral table to 
210d2 64 6f 20 74 68 65 20 6a 6f 62 2e 0a 20 20 20 20  do the job..    
210d3 2a 2f 0a 20 20 20 20 69 6e 74 20 72 4d 61 79 48  */.    int rMayH
210d4 61 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20  aveNull = 0;.   
210d5 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45   eType = IN_INDE
210d6 58 5f 45 50 48 3b 0a 20 20 20 20 69 66 28 20 70  X_EPH;.    if( p
210d7 72 4e 6f 74 46 6f 75 6e 64 20 29 7b 0a 20 20 20  rNotFound ){.   
210d8 20 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d     *prNotFound =
210d9 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20   rMayHaveNull = 
210da 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
210db 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 58      }else if( pX
210dc 2d 3e 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e  ->pLeft->iColumn
210dd 3c 30 20 26 26 20 21 45 78 70 72 48 61 73 41 6e  <0 && !ExprHasAn
210de 79 50 72 6f 70 65 72 74 79 28 70 58 2c 20 45 50  yProperty(pX, EP
210df 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
210e0 20 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e 5f       eType = IN_
210e1 49 4e 44 45 58 5f 52 4f 57 49 44 3b 0a 20 20 20  INDEX_ROWID;.   
210e2 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f   }.    sqlite3Co
210e3 64 65 53 75 62 73 65 6c 65 63 74 28 70 50 61 72  deSubselect(pPar
210e4 73 65 2c 20 70 58 2c 20 72 4d 61 79 48 61 76 65  se, pX, rMayHave
210e5 4e 75 6c 6c 2c 20 65 54 79 70 65 3d 3d 49 4e 5f  Null, eType==IN_
210e6 49 4e 44 45 58 5f 52 4f 57 49 44 29 3b 0a 20 20  INDEX_ROWID);.  
210e7 7d 65 6c 73 65 7b 0a 20 20 20 20 70 58 2d 3e 69  }else{.    pX->i
210e8 54 61 62 6c 65 20 3d 20 69 54 61 62 3b 0a 20 20  Table = iTab;.  
210e9 7d 0a 20 20 72 65 74 75 72 6e 20 65 54 79 70 65  }.  return eType
210ea 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
210eb 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
210ec 66 6f 72 20 73 63 61 6c 61 72 20 73 75 62 71 75  for scalar subqu
210ed 65 72 69 65 73 20 75 73 65 64 20 61 73 20 61 6e  eries used as an
210ee 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61   expression.** a
210ef 6e 64 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 2e  nd IN operators.
210f0 20 20 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a    Examples:.**.*
210f1 2a 20 20 20 20 20 28 53 45 4c 45 43 54 20 61 20  *     (SELECT a 
210f2 46 52 4f 4d 20 62 29 20 20 20 20 20 20 20 20 20  FROM b)         
210f3 20 2d 2d 20 73 75 62 71 75 65 72 79 0a 2a 2a 20   -- subquery.** 
210f4 20 20 20 20 45 58 49 53 54 53 20 28 53 45 4c 45      EXISTS (SELE
210f5 43 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20 2d  CT a FROM b)   -
210f6 2d 20 45 58 49 53 54 53 20 73 75 62 71 75 65 72  - EXISTS subquer
210f7 79 0a 2a 2a 20 20 20 20 20 78 20 49 4e 20 28 34  y.**     x IN (4
210f8 2c 35 2c 31 31 29 20 20 20 20 20 20 20 20 20 20  ,5,11)          
210f9 20 20 20 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74      -- IN operat
210fa 6f 72 20 77 69 74 68 20 6c 69 73 74 20 6f 6e 20  or with list on 
210fb 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 0a  right-hand side.
210fc 2a 2a 20 20 20 20 20 78 20 49 4e 20 28 53 45 4c  **     x IN (SEL
210fd 45 43 54 20 61 20 46 52 4f 4d 20 62 29 20 20 20  ECT a FROM b)   
210fe 20 20 2d 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72    -- IN operator
210ff 20 77 69 74 68 20 73 75 62 71 75 65 72 79 20 6f   with subquery o
21100 6e 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 0a 2a  n the right.**.*
21101 2a 20 54 68 65 20 70 45 78 70 72 20 70 61 72 61  * The pExpr para
21102 6d 65 74 65 72 20 64 65 73 63 72 69 62 65 73 20  meter describes 
21103 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  the expression t
21104 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  hat contains the
21105 20 49 4e 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20   IN.** operator 
21106 6f 72 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a  or subquery..**.
21107 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72 20  ** If parameter 
21108 69 73 52 6f 77 69 64 20 69 73 20 6e 6f 6e 2d 7a  isRowid is non-z
21109 65 72 6f 2c 20 74 68 65 6e 20 65 78 70 72 65 73  ero, then expres
2110a 73 69 6f 6e 20 70 45 78 70 72 20 69 73 20 67 75  sion pExpr is gu
2110b 61 72 61 6e 74 65 65 64 0a 2a 2a 20 74 6f 20 62  aranteed.** to b
2110c 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c  e of the form "<
2110d 72 6f 77 69 64 3e 20 49 4e 20 28 3f 2c 20 3f 2c  rowid> IN (?, ?,
2110e 20 3f 29 22 2c 20 77 68 65 72 65 20 3c 72 6f 77   ?)", where <row
2110f 69 64 3e 20 69 73 20 61 20 72 65 66 65 72 65 6e  id> is a referen
21110 63 65 0a 2a 2a 20 74 6f 20 73 6f 6d 65 20 69 6e  ce.** to some in
21111 74 65 67 65 72 20 6b 65 79 20 63 6f 6c 75 6d 6e  teger key column
21112 20 6f 66 20 61 20 74 61 62 6c 65 20 42 2d 54 72   of a table B-Tr
21113 65 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ee. In this case
21114 2c 20 75 73 65 20 61 6e 0a 2a 2a 20 69 6e 74 6b  , use an.** intk
21115 65 79 20 42 2d 54 72 65 65 20 74 6f 20 73 74 6f  ey B-Tree to sto
21116 72 65 20 74 68 65 20 73 65 74 20 6f 66 20 49 4e  re the set of IN
21117 28 2e 2e 2e 29 20 76 61 6c 75 65 73 20 69 6e 73  (...) values ins
21118 74 65 61 64 20 6f 66 20 74 68 65 20 75 73 75 61  tead of the usua
21119 6c 0a 2a 2a 20 28 73 6c 6f 77 65 72 29 20 76 61  l.** (slower) va
2111a 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 6b 65  riable length ke
2111b 79 73 20 42 2d 54 72 65 65 2e 0a 2a 2a 0a 2a 2a  ys B-Tree..**.**
2111c 20 49 66 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c   If rMayHaveNull
2111d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68   is non-zero, th
2111e 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  at means that th
2111f 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61  e operation is a
21120 6e 20 49 4e 0a 2a 2a 20 28 6e 6f 74 20 61 20 53  n IN.** (not a S
21121 45 4c 45 43 54 20 6f 72 20 45 58 49 53 54 53 29  ELECT or EXISTS)
21122 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 52 48   and that the RH
21123 53 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 73  S might contains
21124 20 4e 55 4c 4c 73 2e 0a 2a 2a 20 46 75 72 74 68   NULLs..** Furth
21125 65 72 6d 6f 72 65 2c 20 74 68 65 20 49 4e 20 69  ermore, the IN i
21126 73 20 69 6e 20 61 20 57 48 45 52 45 20 63 6c 61  s in a WHERE cla
21127 75 73 65 20 61 6e 64 20 74 68 61 74 20 77 65 20  use and that we 
21128 72 65 61 6c 6c 79 20 77 61 6e 74 0a 2a 2a 20 74  really want.** t
21129 6f 20 69 74 65 72 61 74 65 20 6f 76 65 72 20 74  o iterate over t
2112a 68 65 20 52 48 53 20 6f 66 20 74 68 65 20 49 4e  he RHS of the IN
2112b 20 6f 70 65 72 61 74 6f 72 20 69 6e 20 6f 72 64   operator in ord
2112c 65 72 20 74 6f 20 71 75 69 63 6b 6c 79 20 6c 6f  er to quickly lo
2112d 63 61 74 65 0a 2a 2a 20 61 6c 6c 20 63 6f 72 72  cate.** all corr
2112e 65 73 70 6f 6e 64 69 6e 67 20 4c 48 53 20 65 6c  esponding LHS el
2112f 65 6d 65 6e 74 73 2e 20 20 41 6c 6c 20 74 68 69  ements.  All thi
21130 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 69  s routine does i
21131 73 20 69 6e 69 74 69 61 6c 69 7a 65 0a 2a 2a 20  s initialize.** 
21132 74 68 65 20 72 65 67 69 73 74 65 72 20 67 69 76  the register giv
21133 65 6e 20 62 79 20 72 4d 61 79 48 61 76 65 4e 75  en by rMayHaveNu
21134 6c 6c 20 74 6f 20 4e 55 4c 4c 2e 20 20 43 61 6c  ll to NULL.  Cal
21135 6c 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 77 69  ling routines wi
21136 6c 6c 20 74 61 6b 65 0a 2a 2a 20 63 61 72 65 20  ll take.** care 
21137 6f 66 20 63 68 61 6e 67 69 6e 67 20 74 68 69 73  of changing this
21138 20 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 20   register value 
21139 74 6f 20 6e 6f 6e 2d 4e 55 4c 4c 20 69 66 20 74  to non-NULL if t
2113a 68 65 20 52 48 53 20 69 73 20 4e 55 4c 4c 2d 66  he RHS is NULL-f
2113b 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 4d  ree..**.** If rM
2113c 61 79 48 61 76 65 4e 75 6c 6c 20 69 73 20 7a 65  ayHaveNull is ze
2113d 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74  ro, that means t
2113e 68 61 74 20 74 68 65 20 73 75 62 71 75 65 72 79  hat the subquery
2113f 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 0a 2a   is being used.*
21140 2a 20 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70  * for membership
21141 20 74 65 73 74 69 6e 67 20 6f 6e 6c 79 2e 20 20   testing only.  
21142 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64  There is no need
21143 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 61   to initialize a
21144 6e 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20  ny.** registers 
21145 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 65 20  to indicate the 
21146 70 72 65 73 65 6e 73 65 20 6f 72 20 61 62 73 65  presense or abse
21147 6e 63 65 20 6f 66 20 4e 55 4c 4c 73 20 6f 6e 20  nce of NULLs on 
21148 74 68 65 20 52 48 53 2e 0a 2a 2f 0a 23 69 66 6e  the RHS..*/.#ifn
21149 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2114a 53 55 42 51 55 45 52 59 0a 53 51 4c 49 54 45 5f  SUBQUERY.SQLITE_
2114b 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2114c 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63  ite3CodeSubselec
2114d 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  t(.  Parse *pPar
2114e 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  se,          /* 
2114f 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
21150 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
21151 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
21152 54 68 65 20 49 4e 2c 20 53 45 4c 45 43 54 2c 20  The IN, SELECT, 
21153 6f 72 20 45 58 49 53 54 53 20 6f 70 65 72 61 74  or EXISTS operat
21154 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 72 4d 61 79  or */.  int rMay
21155 48 61 76 65 4e 75 6c 6c 2c 20 20 20 20 20 20 20  HaveNull,       
21156 2f 2a 20 52 65 67 69 73 74 65 72 20 74 68 61 74  /* Register that
21157 20 72 65 63 6f 72 64 73 20 77 68 65 74 68 65 72   records whether
21158 20 4e 55 4c 4c 73 20 65 78 69 73 74 20 69 6e 20   NULLs exist in 
21159 52 48 53 20 2a 2f 0a 20 20 69 6e 74 20 69 73 52  RHS */.  int isR
2115a 6f 77 69 64 20 20 20 20 20 20 20 20 20 20 20 20  owid            
2115b 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 4c 48 53   /* If true, LHS
2115c 20 6f 66 20 49 4e 20 6f 70 65 72 61 74 6f 72 20   of IN operator 
2115d 69 73 20 61 20 72 6f 77 69 64 20 2a 2f 0a 29 7b  is a rowid */.){
2115e 0a 20 20 69 6e 74 20 74 65 73 74 41 64 64 72 20  .  int testAddr 
2115f 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
21160 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
21161 65 2d 74 69 6d 65 20 74 65 73 74 20 61 64 64 72  e-time test addr
21162 65 73 73 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76  ess */.  Vdbe *v
21163 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
21164 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
21165 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20 72   NEVER(v==0) ) r
21166 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33  eturn;.  sqlite3
21167 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50  ExprCachePush(pP
21168 61 72 73 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 69  arse);..  /* Thi
21169 73 20 63 6f 64 65 20 6d 75 73 74 20 62 65 20 72  s code must be r
2116a 75 6e 20 69 6e 20 69 74 73 20 65 6e 74 69 72 65  un in its entire
2116b 74 79 20 65 76 65 72 79 20 74 69 6d 65 20 69 74  ty every time it
2116c 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a   is encountered.
2116d 20 20 2a 2a 20 69 66 20 61 6e 79 20 6f 66 20 74    ** if any of t
2116e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20  he following is 
2116f 74 72 75 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  true:.  **.  ** 
21170 20 20 20 2a 20 20 54 68 65 20 72 69 67 68 74 2d     *  The right-
21171 68 61 6e 64 20 73 69 64 65 20 69 73 20 61 20 63  hand side is a c
21172 6f 72 72 65 6c 61 74 65 64 20 73 75 62 71 75 65  orrelated subque
21173 72 79 0a 20 20 2a 2a 20 20 20 20 2a 20 20 54 68  ry.  **    *  Th
21174 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64  e right-hand sid
21175 65 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69  e is an expressi
21176 6f 6e 20 6c 69 73 74 20 63 6f 6e 74 61 69 6e 69  on list containi
21177 6e 67 20 76 61 72 69 61 62 6c 65 73 0a 20 20 2a  ng variables.  *
21178 2a 20 20 20 20 2a 20 20 57 65 20 61 72 65 20 69  *    *  We are i
21179 6e 73 69 64 65 20 61 20 74 72 69 67 67 65 72 0a  nside a trigger.
2117a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 6c 6c    **.  ** If all
2117b 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 61 72   of the above ar
2117c 65 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 77 65  e false, then we
2117d 20 63 61 6e 20 72 75 6e 20 74 68 69 73 20 63 6f   can run this co
2117e 64 65 20 6a 75 73 74 20 6f 6e 63 65 0a 20 20 2a  de just once.  *
2117f 2a 20 73 61 76 65 20 74 68 65 20 72 65 73 75 6c  * save the resul
21180 74 73 2c 20 61 6e 64 20 72 65 75 73 65 20 74 68  ts, and reuse th
21181 65 20 73 61 6d 65 20 72 65 73 75 6c 74 20 6f 6e  e same result on
21182 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f   subsequent invo
21183 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20  cations..  */.  
21184 69 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 50  if( !ExprHasAnyP
21185 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
21186 50 5f 56 61 72 53 65 6c 65 63 74 29 20 26 26 20  P_VarSelect) && 
21187 21 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65  !pParse->pTrigge
21188 72 54 61 62 20 29 7b 0a 20 20 20 20 69 6e 74 20  rTab ){.    int 
21189 6d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  mem = ++pParse->
2118a 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65  nMem;.    sqlite
2118b 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2118c 50 5f 49 66 2c 20 6d 65 6d 29 3b 0a 20 20 20 20  P_If, mem);.    
2118d 74 65 73 74 41 64 64 72 20 3d 20 73 71 6c 69 74  testAddr = sqlit
2118e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2118f 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 6d  OP_Integer, 1, m
21190 65 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  em);.    assert(
21191 20 74 65 73 74 41 64 64 72 3e 30 20 7c 7c 20 70   testAddr>0 || p
21192 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
21193 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7d 0a 0a  cFailed );.  }..
21194 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d    switch( pExpr-
21195 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20  >op ){.    case 
21196 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 63  TK_IN: {.      c
21197 68 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 20 20  har affinity;.  
21198 20 20 20 20 4b 65 79 49 6e 66 6f 20 6b 65 79 49      KeyInfo keyI
21199 6e 66 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 61  nfo;.      int a
2119a 64 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 41  ddr;        /* A
2119b 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 4f 70 65  ddress of OP_Ope
2119c 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72  nEphemeral instr
2119d 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  uction */.      
2119e 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45  Expr *pLeft = pE
2119f 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 0a 20 20 20  xpr->pLeft;..   
211a0 20 20 20 69 66 28 20 72 4d 61 79 48 61 76 65 4e     if( rMayHaveN
211a1 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ull ){.        s
211a2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
211a3 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
211a4 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20  rMayHaveNull);. 
211a5 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 66       }..      af
211a6 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33  finity = sqlite3
211a7 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 4c 65  ExprAffinity(pLe
211a8 66 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 57  ft);..      /* W
211a9 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20 61  hether this is a
211aa 6e 20 27 78 20 49 4e 28 53 45 4c 45 43 54 2e 2e  n 'x IN(SELECT..
211ab 2e 29 27 20 6f 72 20 61 6e 20 27 78 20 49 4e 28  .)' or an 'x IN(
211ac 3c 65 78 70 72 6c 69 73 74 3e 29 27 0a 20 20 20  <exprlist>)'.   
211ad 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e     ** expression
211ae 20 69 74 20 69 73 20 68 61 6e 64 6c 65 64 20 74   it is handled t
211af 68 65 20 73 61 6d 65 20 77 61 79 2e 20 41 20 76  he same way. A v
211b0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20  irtual table is 
211b1 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c 6c 65 64  .      ** filled
211b2 20 77 69 74 68 20 73 69 6e 67 6c 65 2d 66 69 65   with single-fie
211b3 6c 64 20 69 6e 64 65 78 20 6b 65 79 73 20 72 65  ld index keys re
211b4 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 72  presenting the r
211b5 65 73 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a 20  esults.      ** 
211b6 66 72 6f 6d 20 74 68 65 20 53 45 4c 45 43 54 20  from the SELECT 
211b7 6f 72 20 74 68 65 20 3c 65 78 70 72 6c 69 73 74  or the <exprlist
211b8 3e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  >..      **.    
211b9 20 20 2a 2a 20 49 66 20 74 68 65 20 27 78 27 20    ** If the 'x' 
211ba 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20  expression is a 
211bb 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 6f 72  column value, or
211bc 20 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e 0a 20   the SELECT.... 
211bd 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e       ** statemen
211be 74 20 72 65 74 75 72 6e 73 20 61 20 63 6f 6c 75  t returns a colu
211bf 6d 6e 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74  mn value, then t
211c0 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74  he affinity of t
211c1 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c  hat.      ** col
211c2 75 6d 6e 20 69 73 20 75 73 65 64 20 74 6f 20 62  umn is used to b
211c3 75 69 6c 64 20 74 68 65 20 69 6e 64 65 78 20 6b  uild the index k
211c4 65 79 73 2e 20 49 66 20 62 6f 74 68 20 27 78 27  eys. If both 'x'
211c5 20 61 6e 64 20 74 68 65 0a 20 20 20 20 20 20 2a   and the.      *
211c6 2a 20 53 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74  * SELECT... stat
211c7 65 6d 65 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e  ement are column
211c8 73 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 20  s, then numeric 
211c9 61 66 66 69 6e 69 74 79 20 69 73 20 75 73 65 64  affinity is used
211ca 0a 20 20 20 20 20 20 2a 2a 20 69 66 20 65 69 74  .      ** if eit
211cb 68 65 72 20 63 6f 6c 75 6d 6e 20 68 61 73 20 4e  her column has N
211cc 55 4d 45 52 49 43 20 6f 72 20 49 4e 54 45 47 45  UMERIC or INTEGE
211cd 52 20 61 66 66 69 6e 69 74 79 2e 20 49 66 20 6e  R affinity. If n
211ce 65 69 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20  either.      ** 
211cf 27 78 27 20 6e 6f 72 20 74 68 65 20 53 45 4c 45  'x' nor the SELE
211d0 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 20  CT... statement 
211d1 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65  are columns, the
211d2 6e 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69  n numeric affini
211d3 74 79 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 75  ty.      ** is u
211d4 73 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  sed..      */.  
211d5 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c      pExpr->iTabl
211d6 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  e = pParse->nTab
211d7 2b 2b 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d  ++;.      addr =
211d8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
211d9 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68  p2(v, OP_OpenEph
211da 65 6d 65 72 61 6c 2c 20 70 45 78 70 72 2d 3e 69  emeral, pExpr->i
211db 54 61 62 6c 65 2c 20 21 69 73 52 6f 77 69 64 29  Table, !isRowid)
211dc 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26  ;.      memset(&
211dd 6b 65 79 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65  keyInfo, 0, size
211de 6f 66 28 6b 65 79 49 6e 66 6f 29 29 3b 0a 20 20  of(keyInfo));.  
211df 20 20 20 20 6b 65 79 49 6e 66 6f 2e 6e 46 69 65      keyInfo.nFie
211e0 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 69  ld = 1;..      i
211e1 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  f( ExprHasProper
211e2 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
211e3 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20  Select) ){.     
211e4 20 20 20 2f 2a 20 43 61 73 65 20 31 3a 20 20 20     /* Case 1:   
211e5 20 20 65 78 70 72 20 49 4e 20 28 53 45 4c 45 43    expr IN (SELEC
211e6 54 20 2e 2e 2e 29 0a 20 20 20 20 20 20 20 20 2a  T ...).        *
211e7 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 47 65 6e  *.        ** Gen
211e8 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 77 72  erate code to wr
211e9 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 73 20  ite the results 
211ea 6f 66 20 74 68 65 20 73 65 6c 65 63 74 20 69 6e  of the select in
211eb 74 6f 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  to the temporary
211ec 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c  .        ** tabl
211ed 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  e allocated and 
211ee 6f 70 65 6e 65 64 20 61 62 6f 76 65 2e 0a 20 20  opened above..  
211ef 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
211f0 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74   SelectDest dest
211f1 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72 4c 69  ;.        ExprLi
211f2 73 74 20 2a 70 45 4c 69 73 74 3b 0a 0a 20 20 20  st *pEList;..   
211f3 20 20 20 20 20 61 73 73 65 72 74 28 20 21 69 73       assert( !is
211f4 52 6f 77 69 64 20 29 3b 0a 20 20 20 20 20 20 20  Rowid );.       
211f5 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
211f6 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52  stInit(&dest, SR
211f7 54 5f 53 65 74 2c 20 70 45 78 70 72 2d 3e 69 54  T_Set, pExpr->iT
211f8 61 62 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 64  able);.        d
211f9 65 73 74 2e 61 66 66 69 6e 69 74 79 20 3d 20 28  est.affinity = (
211fa 75 38 29 61 66 66 69 6e 69 74 79 3b 0a 20 20 20  u8)affinity;.   
211fb 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 45       assert( (pE
211fc 78 70 72 2d 3e 69 54 61 62 6c 65 26 30 78 30 30  xpr->iTable&0x00
211fd 30 30 46 46 46 46 29 3d 3d 70 45 78 70 72 2d 3e  00FFFF)==pExpr->
211fe 69 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 20 20  iTable );.      
211ff 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 65 6c    if( sqlite3Sel
21200 65 63 74 28 70 50 61 72 73 65 2c 20 70 45 78 70  ect(pParse, pExp
21201 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20 26 64  r->x.pSelect, &d
21202 65 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  est) ){.        
21203 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
21204 20 20 7d 0a 20 20 20 20 20 20 20 20 70 45 4c 69    }.        pELi
21205 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 53  st = pExpr->x.pS
21206 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20  elect->pEList;. 
21207 20 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59         if( ALWAY
21208 53 28 70 45 4c 69 73 74 21 3d 30 20 26 26 20 70  S(pEList!=0 && p
21209 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30 29 20  EList->nExpr>0) 
2120a 29 7b 20 0a 20 20 20 20 20 20 20 20 20 20 6b 65  ){ .          ke
2120b 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d  yInfo.aColl[0] =
2120c 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f   sqlite3BinaryCo
2120d 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61  mpareCollSeq(pPa
2120e 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
2120f 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
21210 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45   pEList->a[0].pE
21211 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  xpr);.        }.
21212 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
21213 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 21 3d  pExpr->x.pList!=
21214 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0 ){.        /* 
21215 43 61 73 65 20 32 3a 20 20 20 20 20 65 78 70 72  Case 2:     expr
21216 20 49 4e 20 28 65 78 70 72 6c 69 73 74 29 0a 20   IN (exprlist). 
21217 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
21218 20 20 2a 2a 20 46 6f 72 20 65 61 63 68 20 65 78    ** For each ex
21219 70 72 65 73 73 69 6f 6e 2c 20 62 75 69 6c 64 20  pression, build 
2121a 61 6e 20 69 6e 64 65 78 20 6b 65 79 20 66 72 6f  an index key fro
2121b 6d 20 74 68 65 20 65 76 61 6c 75 61 74 69 6f 6e  m the evaluation
2121c 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a 20   and.        ** 
2121d 73 74 6f 72 65 20 69 74 20 69 6e 20 74 68 65 20  store it in the 
2121e 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e  temporary table.
2121f 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20 61 20   If <expr> is a 
21220 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 75 73 65  column, then use
21221 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74  .        ** that
21222 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74   columns affinit
21223 79 20 77 68 65 6e 20 62 75 69 6c 64 69 6e 67 20  y when building 
21224 69 6e 64 65 78 20 6b 65 79 73 2e 20 49 66 20 3c  index keys. If <
21225 65 78 70 72 3e 20 69 73 20 6e 6f 74 0a 20 20 20  expr> is not.   
21226 20 20 20 20 20 2a 2a 20 61 20 63 6f 6c 75 6d 6e       ** a column
21227 2c 20 75 73 65 20 6e 75 6d 65 72 69 63 20 61 66  , use numeric af
21228 66 69 6e 69 74 79 2e 0a 20 20 20 20 20 20 20 20  finity..        
21229 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  */.        int i
2122a 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72 4c 69  ;.        ExprLi
2122b 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70  st *pList = pExp
2122c 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20  r->x.pList;.    
2122d 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
2122e 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
2122f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 2c  .        int r1,
21230 20 72 32 2c 20 72 33 3b 0a 0a 20 20 20 20 20 20   r2, r3;..      
21231 20 20 69 66 28 20 21 61 66 66 69 6e 69 74 79 20    if( !affinity 
21232 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 66 66  ){.          aff
21233 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41  inity = SQLITE_A
21234 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 20  FF_NONE;.       
21235 20 7d 0a 20 20 20 20 20 20 20 20 6b 65 79 49 6e   }.        keyIn
21236 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20 73 71  fo.aColl[0] = sq
21237 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
21238 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
21239 70 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 20  pLeft);..       
2123a 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   /* Loop through
2123b 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e   each expression
2123c 20 69 6e 20 3c 65 78 70 72 6c 69 73 74 3e 2e 20   in <exprlist>. 
2123d 2a 2f 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20  */.        r1 = 
2123e 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
2123f 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  g(pParse);.     
21240 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47     r2 = sqlite3G
21241 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
21242 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
21243 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
21244 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 32 29 3b  OP_Null, 0, r2);
21245 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 70  .        for(i=p
21246 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70 49 74  List->nExpr, pIt
21247 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3e 30  em=pList->a; i>0
21248 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i--, pItem++){
21249 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20  .          Expr 
2124a 2a 70 45 32 20 3d 20 70 49 74 65 6d 2d 3e 70 45  *pE2 = pItem->pE
2124b 78 70 72 3b 0a 0a 20 20 20 20 20 20 20 20 20 20  xpr;..          
2124c 2f 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73  /* If the expres
2124d 73 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 6e 73  sion is not cons
2124e 74 61 6e 74 20 74 68 65 6e 20 77 65 20 77 69 6c  tant then we wil
2124f 6c 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20  l need to.      
21250 20 20 20 20 2a 2a 20 64 69 73 61 62 6c 65 20 74      ** disable t
21251 68 65 20 74 65 73 74 20 74 68 61 74 20 77 61 73  he test that was
21252 20 67 65 6e 65 72 61 74 65 64 20 61 62 6f 76 65   generated above
21253 20 74 68 61 74 20 6d 61 6b 65 73 20 73 75 72 65   that makes sure
21254 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68  .          ** th
21255 69 73 20 63 6f 64 65 20 6f 6e 6c 79 20 65 78 65  is code only exe
21256 63 75 74 65 73 20 6f 6e 63 65 2e 20 20 42 65 63  cutes once.  Bec
21257 61 75 73 65 20 66 6f 72 20 61 20 6e 6f 6e 2d 63  ause for a non-c
21258 6f 6e 73 74 61 6e 74 0a 20 20 20 20 20 20 20 20  onstant.        
21259 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20    ** expression 
2125a 77 65 20 6e 65 65 64 20 74 6f 20 72 65 72 75 6e  we need to rerun
2125b 20 74 68 69 73 20 63 6f 64 65 20 65 61 63 68 20   this code each 
2125c 74 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 20 20  time..          
2125d 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  */.          if(
2125e 20 74 65 73 74 41 64 64 72 20 26 26 20 21 73 71   testAddr && !sq
2125f 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
21260 61 6e 74 28 70 45 32 29 20 29 7b 0a 20 20 20 20  ant(pE2) ){.    
21261 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
21262 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28  dbeChangeToNoop(
21263 76 2c 20 74 65 73 74 41 64 64 72 2d 31 2c 20 32  v, testAddr-1, 2
21264 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74  );.            t
21265 65 73 74 41 64 64 72 20 3d 20 30 3b 0a 20 20 20  estAddr = 0;.   
21266 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
21267 20 20 20 20 2f 2a 20 45 76 61 6c 75 61 74 65 20      /* Evaluate 
21268 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61  the expression a
21269 6e 64 20 69 6e 73 65 72 74 20 69 74 20 69 6e 74  nd insert it int
2126a 6f 20 74 68 65 20 74 65 6d 70 20 74 61 62 6c 65  o the temp table
2126b 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 33   */.          r3
2126c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
2126d 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c  deTarget(pParse,
2126e 20 70 45 32 2c 20 72 31 29 3b 0a 20 20 20 20 20   pE2, r1);.     
2126f 20 20 20 20 20 69 66 28 20 69 73 52 6f 77 69 64       if( isRowid
21270 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
21271 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21272 32 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e  2(v, OP_MustBeIn
21273 74 2c 20 72 33 2c 20 73 71 6c 69 74 65 33 56 64  t, r3, sqlite3Vd
21274 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
21275 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  +2);.           
21276 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21277 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c  p3(v, OP_Insert,
21278 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20   pExpr->iTable, 
21279 72 32 2c 20 72 33 29 3b 0a 20 20 20 20 20 20 20  r2, r3);.       
2127a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2127b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2127c 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61  eAddOp4(v, OP_Ma
2127d 6b 65 52 65 63 6f 72 64 2c 20 72 33 2c 20 31 2c  keRecord, r3, 1,
2127e 20 72 32 2c 20 26 61 66 66 69 6e 69 74 79 2c 20   r2, &affinity, 
2127f 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  1);.            
21280 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
21281 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
21282 50 61 72 73 65 2c 20 72 33 2c 20 31 29 3b 0a 20  Parse, r3, 1);. 
21283 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
21284 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
21285 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70 45  OP_IdxInsert, pE
21286 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 32 29  xpr->iTable, r2)
21287 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
21288 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
21289 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2128a 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
2128b 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2128c 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
2128d 28 70 50 61 72 73 65 2c 20 72 32 29 3b 0a 20 20  (pParse, r2);.  
2128e 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
2128f 21 69 73 52 6f 77 69 64 20 29 7b 0a 20 20 20 20  !isRowid ){.    
21290 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
21291 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2c  hangeP4(v, addr,
21292 20 28 76 6f 69 64 20 2a 29 26 6b 65 79 49 6e 66   (void *)&keyInf
21293 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a  o, P4_KEYINFO);.
21294 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
21295 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
21296 63 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a  case TK_EXISTS:.
21297 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45      case TK_SELE
21298 43 54 3a 0a 20 20 20 20 64 65 66 61 75 6c 74 3a  CT:.    default:
21299 20 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74   {.      /* If t
2129a 68 69 73 20 68 61 73 20 74 6f 20 62 65 20 61 20  his has to be a 
2129b 73 63 61 6c 61 72 20 53 45 4c 45 43 54 2e 20 20  scalar SELECT.  
2129c 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
2129d 20 70 75 74 20 74 68 65 0a 20 20 20 20 20 20 2a   put the.      *
2129e 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20  * value of this 
2129f 73 65 6c 65 63 74 20 69 6e 20 61 20 6d 65 6d 6f  select in a memo
212a0 72 79 20 63 65 6c 6c 20 61 6e 64 20 72 65 63 6f  ry cell and reco
212a1 72 64 20 74 68 65 20 6e 75 6d 62 65 72 0a 20 20  rd the number.  
212a2 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d 65      ** of the me
212a3 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69 43 6f  mory cell in iCo
212a4 6c 75 6d 6e 2e 20 20 49 66 20 74 68 69 73 20 69  lumn.  If this i
212a5 73 20 61 6e 20 45 58 49 53 54 53 2c 20 77 72 69  s an EXISTS, wri
212a6 74 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 69  te.      ** an i
212a7 6e 74 65 67 65 72 20 30 20 28 6e 6f 74 20 65 78  nteger 0 (not ex
212a8 69 73 74 73 29 20 6f 72 20 31 20 28 65 78 69 73  ists) or 1 (exis
212a9 74 73 29 20 69 6e 74 6f 20 61 20 6d 65 6d 6f 72  ts) into a memor
212aa 79 20 63 65 6c 6c 0a 20 20 20 20 20 20 2a 2a 20  y cell.      ** 
212ab 61 6e 64 20 72 65 63 6f 72 64 20 74 68 61 74 20  and record that 
212ac 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 69  memory cell in i
212ad 43 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2f  Column..      */
212ae 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f  .      static co
212af 6e 73 74 20 54 6f 6b 65 6e 20 6f 6e 65 20 3d 20  nst Token one = 
212b0 7b 20 22 31 22 2c 20 31 20 7d 3b 20 20 2f 2a 20  { "1", 1 };  /* 
212b1 54 6f 6b 65 6e 20 66 6f 72 20 6c 69 74 65 72 61  Token for litera
212b2 6c 20 76 61 6c 75 65 20 31 20 2a 2f 0a 20 20 20  l value 1 */.   
212b3 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b     Select *pSel;
212b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
212b5 20 20 20 20 20 20 20 20 20 2f 2a 20 53 45 4c 45           /* SELE
212b6 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  CT statement to 
212b7 65 6e 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20  encode */.      
212b8 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b  SelectDest dest;
212b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
212ba 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20        /* How to 
212bb 64 65 61 6c 20 77 69 74 68 20 53 45 4c 45 43 74  deal with SELECt
212bc 20 72 65 73 75 6c 74 20 2a 2f 0a 0a 20 20 20 20   result */..    
212bd 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70    testcase( pExp
212be 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53  r->op==TK_EXISTS
212bf 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
212c0 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  se( pExpr->op==T
212c1 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20  K_SELECT );.    
212c2 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
212c3 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53 20 7c  >op==TK_EXISTS |
212c4 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  | pExpr->op==TK_
212c5 53 45 4c 45 43 54 20 29 3b 0a 0a 20 20 20 20 20  SELECT );..     
212c6 20 61 73 73 65 72 74 28 20 45 78 70 72 48 61 73   assert( ExprHas
212c7 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
212c8 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b  EP_xIsSelect) );
212c9 0a 20 20 20 20 20 20 70 53 65 6c 20 3d 20 70 45  .      pSel = pE
212ca 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 3b 0a  xpr->x.pSelect;.
212cb 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
212cc 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73  ectDestInit(&des
212cd 74 2c 20 30 2c 20 2b 2b 70 50 61 72 73 65 2d 3e  t, 0, ++pParse->
212ce 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20 69 66 28  nMem);.      if(
212cf 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53   pExpr->op==TK_S
212d0 45 4c 45 43 54 20 29 7b 0a 20 20 20 20 20 20 20  ELECT ){.       
212d1 20 64 65 73 74 2e 65 44 65 73 74 20 3d 20 53 52   dest.eDest = SR
212d2 54 5f 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 73  T_Mem;.        s
212d3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
212d4 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
212d5 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20  dest.iParm);.   
212d6 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
212d7 28 28 76 2c 20 22 49 6e 69 74 20 73 75 62 71 75  ((v, "Init subqu
212d8 65 72 79 20 72 65 73 75 6c 74 22 29 29 3b 0a 20  ery result"));. 
212d9 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
212da 20 20 20 20 64 65 73 74 2e 65 44 65 73 74 20 3d      dest.eDest =
212db 20 53 52 54 5f 45 78 69 73 74 73 3b 0a 20 20 20   SRT_Exists;.   
212dc 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
212dd 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
212de 65 67 65 72 2c 20 30 2c 20 64 65 73 74 2e 69 50  eger, 0, dest.iP
212df 61 72 6d 29 3b 0a 20 20 20 20 20 20 20 20 56 64  arm);.        Vd
212e0 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 49  beComment((v, "I
212e1 6e 69 74 20 45 58 49 53 54 53 20 72 65 73 75 6c  nit EXISTS resul
212e2 74 22 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  t"));.      }.  
212e3 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
212e4 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
212e5 2c 20 70 53 65 6c 2d 3e 70 4c 69 6d 69 74 29 3b  , pSel->pLimit);
212e6 0a 20 20 20 20 20 20 70 53 65 6c 2d 3e 70 4c 69  .      pSel->pLi
212e7 6d 69 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78  mit = sqlite3PEx
212e8 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e  pr(pParse, TK_IN
212e9 54 45 47 45 52 2c 20 30 2c 20 30 2c 20 26 6f 6e  TEGER, 0, 0, &on
212ea 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71  e);.      if( sq
212eb 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72  lite3Select(pPar
212ec 73 65 2c 20 70 53 65 6c 2c 20 26 64 65 73 74 29  se, pSel, &dest)
212ed 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
212ee 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
212ef 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e    pExpr->iColumn
212f0 20 3d 20 28 69 31 36 29 64 65 73 74 2e 69 50 61   = (i16)dest.iPa
212f1 72 6d 3b 0a 20 20 20 20 20 20 45 78 70 72 53 65  rm;.      ExprSe
212f2 74 49 72 72 65 64 75 63 69 62 6c 65 28 70 45 78  tIrreducible(pEx
212f3 70 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  pr);.      break
212f4 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69  ;.    }.  }..  i
212f5 66 28 20 74 65 73 74 41 64 64 72 20 29 7b 0a 20  f( testAddr ){. 
212f6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
212f7 6d 70 48 65 72 65 28 76 2c 20 74 65 73 74 41 64  mpHere(v, testAd
212f8 64 72 2d 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  dr-1);.  }.  sql
212f9 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f 70  ite3ExprCachePop
212fa 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 0a 20 20  (pParse, 1);..  
212fb 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66  return;.}.#endif
212fc 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
212fd 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a  SUBQUERY */../*.
212fe 2a 2a 20 44 75 70 6c 69 63 61 74 65 20 61 6e 20  ** Duplicate an 
212ff 38 2d 62 79 74 65 20 76 61 6c 75 65 0a 2a 2f 0a  8-byte value.*/.
21300 73 74 61 74 69 63 20 63 68 61 72 20 2a 64 75 70  static char *dup
21301 38 62 79 74 65 73 28 56 64 62 65 20 2a 76 2c 20  8bytes(Vdbe *v, 
21302 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 29 7b  const char *in){
21303 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 3d 20 73  .  char *out = s
21304 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
21305 77 28 73 71 6c 69 74 65 33 56 64 62 65 44 62 28  w(sqlite3VdbeDb(
21306 76 29 2c 20 38 29 3b 0a 20 20 69 66 28 20 6f 75  v), 8);.  if( ou
21307 74 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  t ){.    memcpy(
21308 6f 75 74 2c 20 69 6e 2c 20 38 29 3b 0a 20 20 7d  out, in, 8);.  }
21309 0a 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d  .  return out;.}
2130a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
2130b 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20   an instruction 
2130c 74 68 61 74 20 77 69 6c 6c 20 70 75 74 20 74 68  that will put th
2130d 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  e floating point
2130e 0a 2a 2a 20 76 61 6c 75 65 20 64 65 73 63 72 69  .** value descri
2130f 62 65 64 20 62 79 20 7a 5b 30 2e 2e 6e 2d 31 5d  bed by z[0..n-1]
21310 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 69   into register i
21311 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a  Mem..**.** The z
21312 5b 5d 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 70  [] string will p
21313 72 6f 62 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a  robably not be z
21314 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20  ero-terminated. 
21315 20 42 75 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e   But the .** z[n
21316 5d 20 63 68 61 72 61 63 74 65 72 20 69 73 20 67  ] character is g
21317 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20  uaranteed to be 
21318 73 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20 64  something that d
21319 6f 65 73 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20  oes not look.** 
2131a 6c 69 6b 65 20 74 68 65 20 63 6f 6e 74 69 6e 75  like the continu
2131b 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d  ation of the num
2131c 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ber..*/.static v
2131d 6f 69 64 20 63 6f 64 65 52 65 61 6c 28 56 64 62  oid codeReal(Vdb
2131e 65 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72  e *v, const char
2131f 20 2a 7a 2c 20 69 6e 74 20 6e 65 67 61 74 65 46   *z, int negateF
21320 6c 61 67 2c 20 69 6e 74 20 69 4d 65 6d 29 7b 0a  lag, int iMem){.
21321 20 20 69 66 28 20 41 4c 57 41 59 53 28 7a 21 3d    if( ALWAYS(z!=
21322 30 29 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65  0) ){.    double
21323 20 76 61 6c 75 65 3b 0a 20 20 20 20 63 68 61 72   value;.    char
21324 20 2a 7a 56 3b 0a 20 20 20 20 73 71 6c 69 74 65   *zV;.    sqlite
21325 33 41 74 6f 46 28 7a 2c 20 26 76 61 6c 75 65 29  3AtoF(z, &value)
21326 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 73  ;.    assert( !s
21327 71 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 6c 75  qlite3IsNaN(valu
21328 65 29 20 29 3b 20 2f 2a 20 54 68 65 20 6e 65 77  e) ); /* The new
21329 20 41 74 6f 46 20 6e 65 76 65 72 20 72 65 74 75   AtoF never retu
2132a 72 6e 73 20 4e 61 4e 20 2a 2f 0a 20 20 20 20 69  rns NaN */.    i
2132b 66 28 20 6e 65 67 61 74 65 46 6c 61 67 20 29 20  f( negateFlag ) 
2132c 76 61 6c 75 65 20 3d 20 2d 76 61 6c 75 65 3b 0a  value = -value;.
2132d 20 20 20 20 7a 56 20 3d 20 64 75 70 38 62 79 74      zV = dup8byt
2132e 65 73 28 76 2c 20 28 63 68 61 72 2a 29 26 76 61  es(v, (char*)&va
2132f 6c 75 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  lue);.    sqlite
21330 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
21331 50 5f 52 65 61 6c 2c 20 30 2c 20 69 4d 65 6d 2c  P_Real, 0, iMem,
21332 20 30 2c 20 7a 56 2c 20 50 34 5f 52 45 41 4c 29   0, zV, P4_REAL)
21333 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  ;.  }.}.../*.** 
21334 47 65 6e 65 72 61 74 65 20 61 6e 20 69 6e 73 74  Generate an inst
21335 72 75 63 74 69 6f 6e 20 74 68 61 74 20 77 69 6c  ruction that wil
21336 6c 20 70 75 74 20 74 68 65 20 69 6e 74 65 67 65  l put the intege
21337 72 20 64 65 73 63 72 69 62 65 20 62 79 0a 2a 2a  r describe by.**
21338 20 74 65 78 74 20 7a 5b 30 2e 2e 6e 2d 31 5d 20   text z[0..n-1] 
21339 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d  into register iM
2133a 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b  em..**.** The z[
2133b 5d 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72  ] string will pr
2133c 6f 62 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65  obably not be ze
2133d 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20  ro-terminated.  
2133e 42 75 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d  But the .** z[n]
2133f 20 63 68 61 72 61 63 74 65 72 20 69 73 20 67 75   character is gu
21340 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73  aranteed to be s
21341 6f 6d 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f  omething that do
21342 65 73 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c  es not look.** l
21343 69 6b 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61  ike the continua
21344 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62  tion of the numb
21345 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  er..*/.static vo
21346 69 64 20 63 6f 64 65 49 6e 74 65 67 65 72 28 56  id codeInteger(V
21347 64 62 65 20 2a 76 2c 20 45 78 70 72 20 2a 70 45  dbe *v, Expr *pE
21348 78 70 72 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67  xpr, int negFlag
21349 2c 20 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 69  , int iMem){.  i
2134a 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20  f( pExpr->flags 
2134b 26 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 29 7b  & EP_IntValue ){
2134c 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 70 45 78  .    int i = pEx
2134d 70 72 2d 3e 75 2e 69 56 61 6c 75 65 3b 0a 20 20  pr->u.iValue;.  
2134e 20 20 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20    if( negFlag ) 
2134f 69 20 3d 20 2d 69 3b 0a 20 20 20 20 73 71 6c 69  i = -i;.    sqli
21350 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
21351 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20   OP_Integer, i, 
21352 69 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  iMem);.  }else{.
21353 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
21354 7a 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f  z = pExpr->u.zTo
21355 6b 65 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ken;.    assert(
21356 20 7a 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28   z!=0 );.    if(
21357 20 73 71 6c 69 74 65 33 46 69 74 73 49 6e 36 34   sqlite3FitsIn64
21358 42 69 74 73 28 7a 2c 20 6e 65 67 46 6c 61 67 29  Bits(z, negFlag)
21359 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 76 61   ){.      i64 va
2135a 6c 75 65 3b 0a 20 20 20 20 20 20 63 68 61 72 20  lue;.      char 
2135b 2a 7a 56 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  *zV;.      sqlit
2135c 65 33 41 74 6f 69 36 34 28 7a 2c 20 26 76 61 6c  e3Atoi64(z, &val
2135d 75 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  ue);.      if( n
2135e 65 67 46 6c 61 67 20 29 20 76 61 6c 75 65 20 3d  egFlag ) value =
2135f 20 2d 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 7a   -value;.      z
21360 56 20 3d 20 64 75 70 38 62 79 74 65 73 28 76 2c  V = dup8bytes(v,
21361 20 28 63 68 61 72 2a 29 26 76 61 6c 75 65 29 3b   (char*)&value);
21362 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
21363 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49  beAddOp4(v, OP_I
21364 6e 74 36 34 2c 20 30 2c 20 69 4d 65 6d 2c 20 30  nt64, 0, iMem, 0
21365 2c 20 7a 56 2c 20 50 34 5f 49 4e 54 36 34 29 3b  , zV, P4_INT64);
21366 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
21367 20 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 7a 2c    codeReal(v, z,
21368 20 6e 65 67 46 6c 61 67 2c 20 69 4d 65 6d 29 3b   negFlag, iMem);
21369 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
2136a 0a 2a 2a 20 43 6c 65 61 72 20 61 20 63 61 63 68  .** Clear a cach
2136b 65 20 65 6e 74 72 79 2e 0a 2a 2f 0a 73 74 61 74  e entry..*/.stat
2136c 69 63 20 76 6f 69 64 20 63 61 63 68 65 45 6e 74  ic void cacheEnt
2136d 72 79 43 6c 65 61 72 28 50 61 72 73 65 20 2a 70  ryClear(Parse *p
2136e 50 61 72 73 65 2c 20 73 74 72 75 63 74 20 79 43  Parse, struct yC
2136f 6f 6c 43 61 63 68 65 20 2a 70 29 7b 0a 20 20 69  olCache *p){.  i
21370 66 28 20 70 2d 3e 74 65 6d 70 52 65 67 20 29 7b  f( p->tempReg ){
21371 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d  .    if( pParse-
21372 3e 6e 54 65 6d 70 52 65 67 3c 41 72 72 61 79 53  >nTempReg<ArrayS
21373 69 7a 65 28 70 50 61 72 73 65 2d 3e 61 54 65 6d  ize(pParse->aTem
21374 70 52 65 67 29 20 29 7b 0a 20 20 20 20 20 20 70  pReg) ){.      p
21375 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b  Parse->aTempReg[
21376 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67  pParse->nTempReg
21377 2b 2b 5d 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20  ++] = p->iReg;. 
21378 20 20 20 7d 0a 20 20 20 20 70 2d 3e 74 65 6d 70     }.    p->temp
21379 52 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  Reg = 0;.  }.}..
2137a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 69 6e  ./*.** Record in
2137b 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68   the column cach
2137c 65 20 74 68 61 74 20 61 20 70 61 72 74 69 63 75  e that a particu
2137d 6c 61 72 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20  lar column from 
2137e 61 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20  a.** particular 
2137f 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20  table is stored 
21380 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  in a particular 
21381 72 65 67 69 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c  register..*/.SQL
21382 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
21383 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
21384 65 53 74 6f 72 65 28 50 61 72 73 65 20 2a 70 50  eStore(Parse *pP
21385 61 72 73 65 2c 20 69 6e 74 20 69 54 61 62 2c 20  arse, int iTab, 
21386 69 6e 74 20 69 43 6f 6c 2c 20 69 6e 74 20 69 52  int iCol, int iR
21387 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  eg){.  int i;.  
21388 69 6e 74 20 6d 69 6e 4c 72 75 3b 0a 20 20 69 6e  int minLru;.  in
21389 74 20 69 64 78 4c 72 75 3b 0a 20 20 73 74 72 75  t idxLru;.  stru
2138a 63 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b  ct yColCache *p;
2138b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 52 65 67  ..  assert( iReg
2138c 3e 30 20 29 3b 20 20 2f 2a 20 52 65 67 69 73 74  >0 );  /* Regist
2138d 65 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20 61  er numbers are a
2138e 6c 77 61 79 73 20 70 6f 73 69 74 69 76 65 20 2a  lways positive *
2138f 2f 0a 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c  /.  assert( iCol
21390 3e 3d 2d 31 20 26 26 20 69 43 6f 6c 3c 33 32 37  >=-1 && iCol<327
21391 36 38 20 29 3b 20 20 2f 2a 20 46 69 6e 69 74 65  68 );  /* Finite
21392 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 73 20   column numbers 
21393 2a 2f 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 72  */..  /* First r
21394 65 70 6c 61 63 65 20 61 6e 79 20 65 78 69 73 74  eplace any exist
21395 69 6e 67 20 65 6e 74 72 79 20 2a 2f 0a 20 20 66  ing entry */.  f
21396 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65  or(i=0, p=pParse
21397 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53  ->aColCache; i<S
21398 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45  QLITE_N_COLCACHE
21399 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20  ; i++, p++){.   
2139a 20 69 66 28 20 70 2d 3e 69 52 65 67 20 26 26 20   if( p->iReg && 
2139b 70 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20  p->iTable==iTab 
2139c 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 69  && p->iColumn==i
2139d 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 63 61 63  Col ){.      cac
2139e 68 65 45 6e 74 72 79 43 6c 65 61 72 28 70 50 61  heEntryClear(pPa
2139f 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 20 20 70  rse, p);.      p
213a0 2d 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 72 73  ->iLevel = pPars
213a1 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a  e->iCacheLevel;.
213a2 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20        p->iReg = 
213a3 69 52 65 67 3b 0a 20 20 20 20 20 20 70 2d 3e 61  iReg;.      p->a
213a4 66 66 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20  ffChange = 0;.  
213a5 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61      p->lru = pPa
213a6 72 73 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b  rse->iCacheCnt++
213a7 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
213a8 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
213a9 46 69 6e 64 20 61 6e 20 65 6d 70 74 79 20 73 6c  Find an empty sl
213aa 6f 74 20 61 6e 64 20 72 65 70 6c 61 63 65 20 69  ot and replace i
213ab 74 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20  t */.  for(i=0, 
213ac 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61  p=pParse->aColCa
213ad 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f  che; i<SQLITE_N_
213ae 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70  COLCACHE; i++, p
213af 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  ++){.    if( p->
213b0 69 52 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20  iReg==0 ){.     
213b1 20 70 2d 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61   p->iLevel = pPa
213b2 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c  rse->iCacheLevel
213b3 3b 0a 20 20 20 20 20 20 70 2d 3e 69 54 61 62 6c  ;.      p->iTabl
213b4 65 20 3d 20 69 54 61 62 3b 0a 20 20 20 20 20 20  e = iTab;.      
213b5 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f  p->iColumn = iCo
213b6 6c 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67  l;.      p->iReg
213b7 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 20 20 70   = iReg;.      p
213b8 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 30 3b  ->affChange = 0;
213b9 0a 20 20 20 20 20 20 70 2d 3e 74 65 6d 70 52 65  .      p->tempRe
213ba 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e  g = 0;.      p->
213bb 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e 69 43  lru = pParse->iC
213bc 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 20  acheCnt++;.     
213bd 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
213be 20 7d 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63 65   }..  /* Replace
213bf 20 74 68 65 20 6c 61 73 74 20 72 65 63 65 6e 74   the last recent
213c0 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 6d 69 6e  ly used */.  min
213c1 4c 72 75 20 3d 20 30 78 37 66 66 66 66 66 66 66  Lru = 0x7fffffff
213c2 3b 0a 20 20 69 64 78 4c 72 75 20 3d 20 2d 31 3b  ;.  idxLru = -1;
213c3 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50  .  for(i=0, p=pP
213c4 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b  arse->aColCache;
213c5 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43   i<SQLITE_N_COLC
213c6 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b  ACHE; i++, p++){
213c7 0a 20 20 20 20 69 66 28 20 70 2d 3e 6c 72 75 3c  .    if( p->lru<
213c8 6d 69 6e 4c 72 75 20 29 7b 0a 20 20 20 20 20 20  minLru ){.      
213c9 69 64 78 4c 72 75 20 3d 20 69 3b 0a 20 20 20 20  idxLru = i;.    
213ca 20 20 6d 69 6e 4c 72 75 20 3d 20 70 2d 3e 6c 72    minLru = p->lr
213cb 75 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  u;.    }.  }.  i
213cc 66 28 20 41 4c 57 41 59 53 28 69 64 78 4c 72 75  f( ALWAYS(idxLru
213cd 3e 3d 30 29 20 29 7b 0a 20 20 20 20 70 20 3d 20  >=0) ){.    p = 
213ce 26 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63  &pParse->aColCac
213cf 68 65 5b 69 64 78 4c 72 75 5d 3b 0a 20 20 20 20  he[idxLru];.    
213d0 70 2d 3e 69 4c 65 76 65 6c 20 3d 20 70 50 61 72  p->iLevel = pPar
213d1 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3b  se->iCacheLevel;
213d2 0a 20 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d  .    p->iTable =
213d3 20 69 54 61 62 3b 0a 20 20 20 20 70 2d 3e 69 43   iTab;.    p->iC
213d4 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20  olumn = iCol;.  
213d5 20 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 67    p->iReg = iReg
213d6 3b 0a 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e  ;.    p->affChan
213d7 67 65 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 74  ge = 0;.    p->t
213d8 65 6d 70 52 65 67 20 3d 20 30 3b 0a 20 20 20 20  empReg = 0;.    
213d9 70 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d  p->lru = pParse-
213da 3e 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20  >iCacheCnt++;.  
213db 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 7d 0a    return;.  }.}.
213dc 0a 2f 2a 0a 2a 2a 20 49 6e 64 69 63 61 74 65 20  ./*.** Indicate 
213dd 74 68 61 74 20 61 20 72 65 67 69 73 74 65 72 20  that a register 
213de 69 73 20 62 65 69 6e 67 20 6f 76 65 72 77 72 69  is being overwri
213df 74 74 65 6e 2e 20 20 50 75 72 67 65 20 74 68 65  tten.  Purge the
213e0 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 66 72 6f   register.** fro
213e1 6d 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63  m the column cac
213e2 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  he..*/.SQLITE_PR
213e3 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
213e4 65 33 45 78 70 72 43 61 63 68 65 52 65 6d 6f 76  e3ExprCacheRemov
213e5 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
213e6 20 69 6e 74 20 69 52 65 67 29 7b 0a 20 20 69 6e   int iReg){.  in
213e7 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43  t i;.  struct yC
213e8 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f  olCache *p;.  fo
213e9 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d  r(i=0, p=pParse-
213ea 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51  >aColCache; i<SQ
213eb 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b  LITE_N_COLCACHE;
213ec 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20   i++, p++){.    
213ed 69 66 28 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65  if( p->iReg==iRe
213ee 67 20 29 7b 0a 20 20 20 20 20 20 63 61 63 68 65  g ){.      cache
213ef 45 6e 74 72 79 43 6c 65 61 72 28 70 50 61 72 73  EntryClear(pPars
213f0 65 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e  e, p);.      p->
213f1 69 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  iReg = 0;.    }.
213f2 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d    }.}../*.** Rem
213f3 65 6d 62 65 72 20 74 68 65 20 63 75 72 72 65 6e  ember the curren
213f4 74 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 63  t column cache c
213f5 6f 6e 74 65 78 74 2e 20 20 41 6e 79 20 6e 65 77  ontext.  Any new
213f6 20 65 6e 74 72 69 65 73 20 61 64 64 65 64 0a 2a   entries added.*
213f7 2a 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 63  * added to the c
213f8 6f 6c 75 6d 6e 20 63 61 63 68 65 20 61 66 74 65  olumn cache afte
213f9 72 20 74 68 69 73 20 63 61 6c 6c 20 61 72 65 20  r this call are 
213fa 72 65 6d 6f 76 65 64 20 77 68 65 6e 20 74 68 65  removed when the
213fb 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  .** correspondin
213fc 67 20 70 6f 70 20 6f 63 63 75 72 73 2e 0a 2a 2f  g pop occurs..*/
213fd 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
213fe 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
213ff 43 61 63 68 65 50 75 73 68 28 50 61 72 73 65 20  CachePush(Parse 
21400 2a 70 50 61 72 73 65 29 7b 0a 20 20 70 50 61 72  *pParse){.  pPar
21401 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 2b  se->iCacheLevel+
21402 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f  +;.}../*.** Remo
21403 76 65 20 66 72 6f 6d 20 74 68 65 20 63 6f 6c 75  ve from the colu
21404 6d 6e 20 63 61 63 68 65 20 61 6e 79 20 65 6e 74  mn cache any ent
21405 72 69 65 73 20 74 68 61 74 20 77 65 72 65 20 61  ries that were a
21406 64 64 65 64 20 73 69 6e 63 65 20 74 68 65 0a 2a  dded since the.*
21407 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 4e  * the previous N
21408 20 50 75 73 68 20 6f 70 65 72 61 74 69 6f 6e 73   Push operations
21409 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
2140a 73 2c 20 72 65 73 74 6f 72 65 20 74 68 65 20 63  s, restore the c
2140b 61 63 68 65 0a 2a 2a 20 74 6f 20 74 68 65 20 73  ache.** to the s
2140c 74 61 74 65 20 69 74 20 77 61 73 20 69 6e 20 4e  tate it was in N
2140d 20 50 75 73 68 65 73 20 61 67 6f 2e 0a 2a 2f 0a   Pushes ago..*/.
2140e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2140f 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
21410 61 63 68 65 50 6f 70 28 50 61 72 73 65 20 2a 70  achePop(Parse *p
21411 50 61 72 73 65 2c 20 69 6e 74 20 4e 29 7b 0a 20  Parse, int N){. 
21412 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74   int i;.  struct
21413 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20   yColCache *p;. 
21414 20 61 73 73 65 72 74 28 20 4e 3e 30 20 29 3b 0a   assert( N>0 );.
21415 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
21416 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3e 3d 4e  ->iCacheLevel>=N
21417 20 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 69 43   );.  pParse->iC
21418 61 63 68 65 4c 65 76 65 6c 20 2d 3d 20 4e 3b 0a  acheLevel -= N;.
21419 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61    for(i=0, p=pPa
2141a 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20  rse->aColCache; 
2141b 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41  i<SQLITE_N_COLCA
2141c 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a  CHE; i++, p++){.
2141d 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 20      if( p->iReg 
2141e 26 26 20 70 2d 3e 69 4c 65 76 65 6c 3e 70 50 61  && p->iLevel>pPa
2141f 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c  rse->iCacheLevel
21420 20 29 7b 0a 20 20 20 20 20 20 63 61 63 68 65 45   ){.      cacheE
21421 6e 74 72 79 43 6c 65 61 72 28 70 50 61 72 73 65  ntryClear(pParse
21422 2c 20 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69  , p);.      p->i
21423 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  Reg = 0;.    }. 
21424 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e   }.}../*.** When
21425 20 61 20 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e   a cached column
21426 20 69 73 20 72 65 75 73 65 64 2c 20 6d 61 6b 65   is reused, make
21427 20 73 75 72 65 20 74 68 61 74 20 69 74 73 20 72   sure that its r
21428 65 67 69 73 74 65 72 20 69 73 0a 2a 2a 20 6e 6f  egister is.** no
21429 20 6c 6f 6e 67 65 72 20 61 76 61 69 6c 61 62 6c   longer availabl
2142a 65 20 61 73 20 61 20 74 65 6d 70 20 72 65 67 69  e as a temp regi
2142b 73 74 65 72 2e 20 20 74 69 63 6b 65 74 20 23 33  ster.  ticket #3
2142c 38 37 39 3a 20 20 74 68 61 74 20 73 61 6d 65 0a  879:  that same.
2142d 2a 2a 20 72 65 67 69 73 74 65 72 20 6d 69 67 68  ** register migh
2142e 74 20 62 65 20 69 6e 20 74 68 65 20 63 61 63 68  t be in the cach
2142f 65 20 69 6e 20 6d 75 6c 74 69 70 6c 65 20 70 6c  e in multiple pl
21430 61 63 65 73 2c 20 73 6f 20 62 65 20 73 75 72 65  aces, so be sure
21431 20 74 6f 0a 2a 2a 20 67 65 74 20 74 68 65 6d 20   to.** get them 
21432 61 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  all..*/.static v
21433 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
21434 61 63 68 65 50 69 6e 52 65 67 69 73 74 65 72 28  achePinRegister(
21435 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
21436 6e 74 20 69 52 65 67 29 7b 0a 20 20 69 6e 74 20  nt iReg){.  int 
21437 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c  i;.  struct yCol
21438 43 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28  Cache *p;.  for(
21439 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61  i=0, p=pParse->a
2143a 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49  ColCache; i<SQLI
2143b 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69  TE_N_COLCACHE; i
2143c 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66  ++, p++){.    if
2143d 28 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20  ( p->iReg==iReg 
2143e 29 7b 0a 20 20 20 20 20 20 70 2d 3e 74 65 6d 70  ){.      p->temp
2143f 52 65 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  Reg = 0;.    }. 
21440 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65   }.}../*.** Gene
21441 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77  rate code that w
21442 69 6c 6c 20 65 78 74 72 61 63 74 20 74 68 65 20  ill extract the 
21443 69 43 6f 6c 75 6d 6e 2d 74 68 20 63 6f 6c 75 6d  iColumn-th colum
21444 6e 20 66 72 6f 6d 0a 2a 2a 20 74 61 62 6c 65 20  n from.** table 
21445 70 54 61 62 20 61 6e 64 20 73 74 6f 72 65 20 74  pTab and store t
21446 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20  he column value 
21447 69 6e 20 61 20 72 65 67 69 73 74 65 72 2e 20 20  in a register.  
21448 41 6e 20 65 66 66 6f 72 74 0a 2a 2a 20 69 73 20  An effort.** is 
21449 6d 61 64 65 20 74 6f 20 73 74 6f 72 65 20 74 68  made to store th
2144a 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69  e column value i
2144b 6e 20 72 65 67 69 73 74 65 72 20 69 52 65 67 2c  n register iReg,
2144c 20 62 75 74 20 74 68 69 73 20 69 73 0a 2a 2a 20   but this is.** 
2144d 6e 6f 74 20 67 75 61 72 61 6e 74 65 65 64 2e 20  not guaranteed. 
2144e 20 54 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66   The location of
2144f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75   the column valu
21450 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  e is returned..*
21451 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20  *.** There must 
21452 62 65 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f  be an open curso
21453 72 20 74 6f 20 70 54 61 62 20 69 6e 20 69 54 61  r to pTab in iTa
21454 62 6c 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f  ble when this ro
21455 75 74 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c  utine.** is call
21456 65 64 2e 20 20 49 66 20 69 43 6f 6c 75 6d 6e 3c  ed.  If iColumn<
21457 30 20 74 68 65 6e 20 63 6f 64 65 20 69 73 20 67  0 then code is g
21458 65 6e 65 72 61 74 65 64 20 74 68 61 74 20 65 78  enerated that ex
21459 74 72 61 63 74 73 20 74 68 65 20 72 6f 77 69 64  tracts the rowid
2145a 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
2145b 74 69 6e 65 20 6d 69 67 68 74 20 61 74 74 65 6d  tine might attem
2145c 70 74 20 74 6f 20 72 65 75 73 65 20 74 68 65 20  pt to reuse the 
2145d 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 6f 6c  value of the col
2145e 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61 73 20  umn that.** has 
2145f 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6c 6f 61  already been loa
21460 64 65 64 20 69 6e 74 6f 20 61 20 72 65 67 69 73  ded into a regis
21461 74 65 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20  ter.  The value 
21462 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20 62  will always.** b
21463 65 20 75 73 65 64 20 69 66 20 69 74 20 68 61 73  e used if it has
21464 20 6e 6f 74 20 75 6e 64 65 72 67 6f 6e 65 20 61   not undergone a
21465 6e 79 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e  ny affinity chan
21466 67 65 73 2e 20 20 42 75 74 20 69 66 0a 2a 2a 20  ges.  But if.** 
21467 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e  an affinity chan
21468 67 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 2c  ge has occurred,
21469 20 74 68 65 6e 20 74 68 65 20 63 61 63 68 65 64   then the cached
2146a 20 76 61 6c 75 65 20 77 69 6c 6c 20 6f 6e 6c 79   value will only
2146b 20 62 65 0a 2a 2a 20 75 73 65 64 20 69 66 20 61   be.** used if a
2146c 6c 6c 6f 77 41 66 66 43 68 6e 67 20 69 73 20 74  llowAffChng is t
2146d 72 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  rue..*/.SQLITE_P
2146e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2146f 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c  e3ExprCodeGetCol
21470 75 6d 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50  umn(.  Parse *pP
21471 61 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 69  arse,   /* Parsi
21472 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65  ng and code gene
21473 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  rating context *
21474 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
21475 20 20 20 20 20 2f 2a 20 44 65 73 63 72 69 70 74       /* Descript
21476 69 6f 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ion of the table
21477 20 77 65 20 61 72 65 20 72 65 61 64 69 6e 67 20   we are reading 
21478 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 43  from */.  int iC
21479 6f 6c 75 6d 6e 2c 20 20 20 20 20 2f 2a 20 49 6e  olumn,     /* In
2147a 64 65 78 20 6f 66 20 74 68 65 20 74 61 62 6c 65  dex of the table
2147b 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74   column */.  int
2147c 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 2f 2a   iTable,      /*
2147d 20 54 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e   The cursor poin
2147e 74 69 6e 67 20 74 6f 20 74 68 65 20 74 61 62 6c  ting to the tabl
2147f 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52 65 67 2c  e */.  int iReg,
21480 20 20 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65          /* Store
21481 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f   results here */
21482 0a 20 20 69 6e 74 20 61 6c 6c 6f 77 41 66 66 43  .  int allowAffC
21483 68 6e 67 20 2f 2a 20 54 72 75 65 20 69 66 20 70  hng /* True if p
21484 72 69 6f 72 20 61 66 66 69 6e 69 74 79 20 63 68  rior affinity ch
21485 61 6e 67 65 73 20 61 72 65 20 4f 4b 20 2a 2f 0a  anges are OK */.
21486 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
21487 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
21488 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
21489 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20  yColCache *p;.. 
2148a 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72   for(i=0, p=pPar
2148b 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69  se->aColCache; i
2148c 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43  <SQLITE_N_COLCAC
2148d 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20  HE; i++, p++){. 
2148e 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3e 30     if( p->iReg>0
2148f 20 26 26 20 70 2d 3e 69 54 61 62 6c 65 3d 3d 69   && p->iTable==i
21490 54 61 62 6c 65 20 26 26 20 70 2d 3e 69 43 6f 6c  Table && p->iCol
21491 75 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e 0a 20 20 20  umn==iColumn.   
21492 20 20 20 20 20 20 20 20 26 26 20 28 21 70 2d 3e          && (!p->
21493 61 66 66 43 68 61 6e 67 65 20 7c 7c 20 61 6c 6c  affChange || all
21494 6f 77 41 66 66 43 68 6e 67 29 20 29 7b 0a 20 20  owAffChng) ){.  
21495 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20 70 50 61      p->lru = pPa
21496 72 73 65 2d 3e 69 43 61 63 68 65 43 6e 74 2b 2b  rse->iCacheCnt++
21497 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
21498 78 70 72 43 61 63 68 65 50 69 6e 52 65 67 69 73  xprCachePinRegis
21499 74 65 72 28 70 50 61 72 73 65 2c 20 70 2d 3e 69  ter(pParse, p->i
2149a 52 65 67 29 3b 0a 20 20 20 20 20 20 72 65 74 75  Reg);.      retu
2149b 72 6e 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20  rn p->iReg;.    
2149c 7d 0a 20 20 7d 20 20 0a 20 20 61 73 73 65 72 74  }.  }  .  assert
2149d 28 20 76 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  ( v!=0 );.  if( 
2149e 69 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20  iColumn<0 ){.   
2149f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
214a0 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20  p2(v, OP_Rowid, 
214a1 69 54 61 62 6c 65 2c 20 69 52 65 67 29 3b 0a 20  iTable, iReg);. 
214a2 20 7d 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59   }else if( ALWAY
214a3 53 28 70 54 61 62 21 3d 30 29 20 29 7b 0a 20 20  S(pTab!=0) ){.  
214a4 20 20 69 6e 74 20 6f 70 20 3d 20 49 73 56 69 72    int op = IsVir
214a5 74 75 61 6c 28 70 54 61 62 29 20 3f 20 4f 50 5f  tual(pTab) ? OP_
214a6 56 43 6f 6c 75 6d 6e 20 3a 20 4f 50 5f 43 6f 6c  VColumn : OP_Col
214a7 75 6d 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  umn;.    sqlite3
214a8 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 6f 70  VdbeAddOp3(v, op
214a9 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c 75 6d  , iTable, iColum
214aa 6e 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 73 71  n, iReg);.    sq
214ab 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75  lite3ColumnDefau
214ac 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 43 6f 6c  lt(v, pTab, iCol
214ad 75 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 7d 0a  umn, iReg);.  }.
214ae 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
214af 68 65 53 74 6f 72 65 28 70 50 61 72 73 65 2c 20  heStore(pParse, 
214b0 69 54 61 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c  iTable, iColumn,
214b1 20 69 52 65 67 29 3b 0a 20 20 72 65 74 75 72 6e   iReg);.  return
214b2 20 69 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   iReg;.}../*.** 
214b3 43 6c 65 61 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e  Clear all column
214b4 20 63 61 63 68 65 20 65 6e 74 72 69 65 73 2e 0a   cache entries..
214b5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
214b6 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
214b7 70 72 43 61 63 68 65 43 6c 65 61 72 28 50 61 72  prCacheClear(Par
214b8 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69  se *pParse){.  i
214b9 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79  nt i;.  struct y
214ba 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20  ColCache *p;..  
214bb 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73  for(i=0, p=pPars
214bc 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c  e->aColCache; i<
214bd 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48  SQLITE_N_COLCACH
214be 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20  E; i++, p++){.  
214bf 20 20 69 66 28 20 70 2d 3e 69 52 65 67 20 29 7b    if( p->iReg ){
214c0 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e 74 72  .      cacheEntr
214c1 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70  yClear(pParse, p
214c2 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67  );.      p->iReg
214c3 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
214c4 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20  }../*.** Record 
214c5 74 68 65 20 66 61 63 74 20 74 68 61 74 20 61 6e  the fact that an
214c6 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65   affinity change
214c7 20 68 61 73 20 6f 63 63 75 72 72 65 64 20 6f 6e   has occurred on
214c8 20 69 43 6f 75 6e 74 0a 2a 2a 20 72 65 67 69 73   iCount.** regis
214c9 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 77 69  ters starting wi
214ca 74 68 20 69 53 74 61 72 74 2e 0a 2a 2f 0a 53 51  th iStart..*/.SQ
214cb 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
214cc 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63  d sqlite3ExprCac
214cd 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
214ce 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
214cf 69 6e 74 20 69 53 74 61 72 74 2c 20 69 6e 74 20  int iStart, int 
214d0 69 43 6f 75 6e 74 29 7b 0a 20 20 69 6e 74 20 69  iCount){.  int i
214d1 45 6e 64 20 3d 20 69 53 74 61 72 74 20 2b 20 69  End = iStart + i
214d2 43 6f 75 6e 74 20 2d 20 31 3b 0a 20 20 69 6e 74  Count - 1;.  int
214d3 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f   i;.  struct yCo
214d4 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72  lCache *p;.  for
214d5 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e  (i=0, p=pParse->
214d6 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c  aColCache; i<SQL
214d7 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20  ITE_N_COLCACHE; 
214d8 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69  i++, p++){.    i
214d9 6e 74 20 72 20 3d 20 70 2d 3e 69 52 65 67 3b 0a  nt r = p->iReg;.
214da 20 20 20 20 69 66 28 20 72 3e 3d 69 53 74 61 72      if( r>=iStar
214db 74 20 26 26 20 72 3c 3d 69 45 6e 64 20 29 7b 0a  t && r<=iEnd ){.
214dc 20 20 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e        p->affChan
214dd 67 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  ge = 1;.    }.  
214de 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  }.}../*.** Gener
214df 61 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 76 65  ate code to move
214e0 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65   content from re
214e1 67 69 73 74 65 72 73 20 69 46 72 6f 6d 2e 2e 2e  gisters iFrom...
214e2 69 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20  iFrom+nReg-1.** 
214e3 6f 76 65 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f  over to iTo..iTo
214e4 2b 6e 52 65 67 2d 31 2e 20 4b 65 65 70 20 74 68  +nReg-1. Keep th
214e5 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 75  e column cache u
214e6 70 2d 74 6f 2d 64 61 74 65 2e 0a 2a 2f 0a 53 51  p-to-date..*/.SQ
214e7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
214e8 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  d sqlite3ExprCod
214e9 65 4d 6f 76 65 28 50 61 72 73 65 20 2a 70 50 61  eMove(Parse *pPa
214ea 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20  rse, int iFrom, 
214eb 69 6e 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65  int iTo, int nRe
214ec 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  g){.  int i;.  s
214ed 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20  truct yColCache 
214ee 2a 70 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28  *p;.  if( NEVER(
214ef 69 46 72 6f 6d 3d 3d 69 54 6f 29 20 29 20 72 65  iFrom==iTo) ) re
214f0 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56  turn;.  sqlite3V
214f1 64 62 65 41 64 64 4f 70 33 28 70 50 61 72 73 65  dbeAddOp3(pParse
214f2 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 4d 6f 76 65  ->pVdbe, OP_Move
214f3 2c 20 69 46 72 6f 6d 2c 20 69 54 6f 2c 20 6e 52  , iFrom, iTo, nR
214f4 65 67 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  eg);.  for(i=0, 
214f5 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61  p=pParse->aColCa
214f6 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f  che; i<SQLITE_N_
214f7 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70  COLCACHE; i++, p
214f8 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 78 20 3d  ++){.    int x =
214f9 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 69 66   p->iReg;.    if
214fa 28 20 78 3e 3d 69 46 72 6f 6d 20 26 26 20 78 3c  ( x>=iFrom && x<
214fb 69 46 72 6f 6d 2b 6e 52 65 67 20 29 7b 0a 20 20  iFrom+nReg ){.  
214fc 20 20 20 20 70 2d 3e 69 52 65 67 20 2b 3d 20 69      p->iReg += i
214fd 54 6f 2d 69 46 72 6f 6d 3b 0a 20 20 20 20 7d 0a  To-iFrom;.    }.
214fe 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e    }.}../*.** Gen
214ff 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 63 6f  erate code to co
21500 70 79 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20  py content from 
21501 72 65 67 69 73 74 65 72 73 20 69 46 72 6f 6d 2e  registers iFrom.
21502 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a 2a  ..iFrom+nReg-1.*
21503 2a 20 6f 76 65 72 20 74 6f 20 69 54 6f 2e 2e 69  * over to iTo..i
21504 54 6f 2b 6e 52 65 67 2d 31 2e 0a 2a 2f 0a 53 51  To+nReg-1..*/.SQ
21505 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
21506 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  d sqlite3ExprCod
21507 65 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50 61  eCopy(Parse *pPa
21508 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20  rse, int iFrom, 
21509 69 6e 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52 65  int iTo, int nRe
2150a 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  g){.  int i;.  i
2150b 66 28 20 4e 45 56 45 52 28 69 46 72 6f 6d 3d 3d  f( NEVER(iFrom==
2150c 69 54 6f 29 20 29 20 72 65 74 75 72 6e 3b 0a 20  iTo) ) return;. 
2150d 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65 67   for(i=0; i<nReg
2150e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  ; i++){.    sqli
2150f 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 70 50  te3VdbeAddOp2(pP
21510 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f  arse->pVdbe, OP_
21511 43 6f 70 79 2c 20 69 46 72 6f 6d 2b 69 2c 20 69  Copy, iFrom+i, i
21512 54 6f 2b 69 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  To+i);.  }.}../*
21513 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
21514 69 66 20 61 6e 79 20 72 65 67 69 73 74 65 72 20  if any register 
21515 69 6e 20 74 68 65 20 72 61 6e 67 65 20 69 46 72  in the range iFr
21516 6f 6d 2e 2e 69 54 6f 20 28 69 6e 63 6c 75 73 69  om..iTo (inclusi
21517 76 65 29 0a 2a 2a 20 69 73 20 75 73 65 64 20 61  ve).** is used a
21518 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 63 6f  s part of the co
21519 6c 75 6d 6e 20 63 61 63 68 65 2e 0a 2a 2f 0a 73  lumn cache..*/.s
2151a 74 61 74 69 63 20 69 6e 74 20 75 73 65 64 41 73  tatic int usedAs
2151b 43 6f 6c 75 6d 6e 43 61 63 68 65 28 50 61 72 73  ColumnCache(Pars
2151c 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
2151d 46 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 29 7b 0a  From, int iTo){.
2151e 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63    int i;.  struc
2151f 74 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a  t yColCache *p;.
21520 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61    for(i=0, p=pPa
21521 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20  rse->aColCache; 
21522 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41  i<SQLITE_N_COLCA
21523 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a  CHE; i++, p++){.
21524 20 20 20 20 69 6e 74 20 72 20 3d 20 70 2d 3e 69      int r = p->i
21525 52 65 67 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d  Reg;.    if( r>=
21526 69 46 72 6f 6d 20 26 26 20 72 3c 3d 69 54 6f 20  iFrom && r<=iTo 
21527 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a  ) return 1;.  }.
21528 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
21529 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 61 73 74  *.** If the last
2152a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f 64   instruction cod
2152b 65 64 20 69 73 20 61 6e 20 65 70 68 65 6d 65 72  ed is an ephemer
2152c 61 6c 20 63 6f 70 79 20 6f 66 20 61 6e 79 20 6f  al copy of any o
2152d 66 0a 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65  f.** the registe
2152e 72 73 20 69 6e 20 74 68 65 20 6e 52 65 67 20 72  rs in the nReg r
2152f 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69  egisters beginni
21530 6e 67 20 77 69 74 68 20 69 52 65 67 2c 20 74 68  ng with iReg, th
21531 65 6e 0a 2a 2a 20 63 6f 6e 76 65 72 74 20 74 68  en.** convert th
21532 65 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74 69  e last instructi
21533 6f 6e 20 66 72 6f 6d 20 4f 50 5f 53 43 6f 70 79  on from OP_SCopy
21534 20 74 6f 20 4f 50 5f 43 6f 70 79 2e 0a 2a 2f 0a   to OP_Copy..*/.
21535 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
21536 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 48  oid sqlite3ExprH
21537 61 72 64 43 6f 70 79 28 50 61 72 73 65 20 2a 70  ardCopy(Parse *p
21538 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 2c  Parse, int iReg,
21539 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 56 64   int nReg){.  Vd
2153a 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 56 64 62  beOp *pOp;.  Vdb
2153b 65 20 2a 76 3b 0a 0a 20 20 61 73 73 65 72 74 28  e *v;..  assert(
2153c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c   pParse->db->mal
2153d 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29 3b 0a  locFailed==0 );.
2153e 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56    v = pParse->pV
2153f 64 62 65 3b 0a 20 20 61 73 73 65 72 74 28 20 76  dbe;.  assert( v
21540 21 3d 30 20 29 3b 0a 20 20 70 4f 70 20 3d 20 73  !=0 );.  pOp = s
21541 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28  qlite3VdbeGetOp(
21542 76 2c 20 2d 31 29 3b 0a 20 20 61 73 73 65 72 74  v, -1);.  assert
21543 28 20 70 4f 70 21 3d 30 20 29 3b 0a 20 20 69 66  ( pOp!=0 );.  if
21544 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
21545 50 5f 53 43 6f 70 79 20 26 26 20 70 4f 70 2d 3e  P_SCopy && pOp->
21546 70 31 3e 3d 69 52 65 67 20 26 26 20 70 4f 70 2d  p1>=iReg && pOp-
21547 3e 70 31 3c 69 52 65 67 2b 6e 52 65 67 20 29 7b  >p1<iReg+nReg ){
21548 0a 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  .    pOp->opcode
21549 20 3d 20 4f 50 5f 43 6f 70 79 3b 0a 20 20 7d 0a   = OP_Copy;.  }.
2154a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
2154b 65 20 63 6f 64 65 20 74 6f 20 73 74 6f 72 65 20  e code to store 
2154c 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
2154d 20 69 41 6c 69 61 73 2d 74 68 20 61 6c 69 61 73   iAlias-th alias
2154e 20 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20   in register.** 
2154f 74 61 72 67 65 74 2e 20 20 54 68 65 20 66 69 72  target.  The fir
21550 73 74 20 74 69 6d 65 20 74 68 69 73 20 69 73 20  st time this is 
21551 63 61 6c 6c 65 64 2c 20 70 45 78 70 72 20 69 73  called, pExpr is
21552 20 65 76 61 6c 75 61 74 65 64 20 74 6f 20 63 6f   evaluated to co
21553 6d 70 75 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c  mpute.** the val
21554 75 65 20 6f 66 20 74 68 65 20 61 6c 69 61 73 2e  ue of the alias.
21555 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 73    The value is s
21556 74 6f 72 65 64 20 69 6e 20 61 6e 20 61 75 78 69  tored in an auxi
21557 6c 69 61 72 79 20 72 65 67 69 73 74 65 72 0a 2a  liary register.*
21558 2a 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72  * and the number
21559 20 6f 66 20 74 68 61 74 20 72 65 67 69 73 74 65   of that registe
2155a 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  r is returned.  
2155b 4f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 61  On subsequent ca
2155c 6c 6c 73 2c 0a 2a 2a 20 74 68 65 20 72 65 67 69  lls,.** the regi
2155d 73 74 65 72 20 6e 75 6d 62 65 72 20 69 73 20 72  ster number is r
2155e 65 74 75 72 6e 65 64 20 77 69 74 68 6f 75 74 20  eturned without 
2155f 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 79 20 63  generating any c
21560 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  ode..**.** Note 
21561 74 68 61 74 20 69 6e 20 6f 72 64 65 72 20 66 6f  that in order fo
21562 72 20 74 68 69 73 20 74 6f 20 77 6f 72 6b 2c 20  r this to work, 
21563 63 6f 64 65 20 6d 75 73 74 20 62 65 20 67 65 6e  code must be gen
21564 65 72 61 74 65 64 20 69 6e 20 74 68 65 0a 2a 2a  erated in the.**
21565 20 73 61 6d 65 20 6f 72 64 65 72 20 74 68 61 74   same order that
21566 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64 2e   it is executed.
21567 0a 2a 2a 0a 2a 2a 20 41 6c 69 61 73 65 73 20 61  .**.** Aliases a
21568 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74 61 72  re numbered star
21569 74 69 6e 67 20 77 69 74 68 20 31 2e 20 20 53 6f  ting with 1.  So
2156a 20 69 41 6c 69 61 73 20 69 73 20 69 6e 20 74 68   iAlias is in th
2156b 65 20 72 61 6e 67 65 0a 2a 2a 20 6f 66 20 31 20  e range.** of 1 
2156c 74 6f 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61  to pParse->nAlia
2156d 73 20 69 6e 63 6c 75 73 69 76 65 2e 20 20 0a 2a  s inclusive.  .*
2156e 2a 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 61 41 6c  *.** pParse->aAl
2156f 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d 20 72 65  ias[iAlias-1] re
21570 63 6f 72 64 73 20 74 68 65 20 72 65 67 69 73 74  cords the regist
21571 65 72 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20  er number where 
21572 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  the value.** of 
21573 74 68 65 20 69 41 6c 69 61 73 2d 74 68 20 61 6c  the iAlias-th al
21574 69 61 73 20 69 73 20 73 74 6f 72 65 64 2e 20 20  ias is stored.  
21575 49 66 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65  If zero, that me
21576 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  ans that the.** 
21577 61 6c 69 61 73 20 68 61 73 20 6e 6f 74 20 79 65  alias has not ye
21578 74 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 2e  t been computed.
21579 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
2157a 6f 64 65 41 6c 69 61 73 28 50 61 72 73 65 20 2a  odeAlias(Parse *
2157b 70 50 61 72 73 65 2c 20 69 6e 74 20 69 41 6c 69  pParse, int iAli
2157c 61 73 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c  as, Expr *pExpr,
2157d 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 23 69   int target){.#i
2157e 66 20 30 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  f 0.  sqlite3 *d
2157f 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
21580 20 20 69 6e 74 20 69 52 65 67 3b 0a 20 20 69 66    int iReg;.  if
21581 28 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73  ( pParse->nAlias
21582 41 6c 6c 6f 63 3c 70 50 61 72 73 65 2d 3e 6e 41  Alloc<pParse->nA
21583 6c 69 61 73 20 29 7b 0a 20 20 20 20 70 50 61 72  lias ){.    pPar
21584 73 65 2d 3e 61 41 6c 69 61 73 20 3d 20 73 71 6c  se->aAlias = sql
21585 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46  ite3DbReallocOrF
21586 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e  ree(db, pParse->
21587 61 41 6c 69 61 73 2c 0a 20 20 20 20 20 20 20 20  aAlias,.        
21588 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21589 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28           sizeof(
2158a 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 30  pParse->aAlias[0
2158b 5d 29 2a 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61  ])*pParse->nAlia
2158c 73 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  s );.    testcas
2158d 65 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e( db->mallocFai
2158e 6c 65 64 20 26 26 20 70 50 61 72 73 65 2d 3e 6e  led && pParse->n
2158f 41 6c 69 61 73 41 6c 6c 6f 63 3e 30 20 29 3b 0a  AliasAlloc>0 );.
21590 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c      if( db->mall
21591 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72  ocFailed ) retur
21592 6e 20 30 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  n 0;.    memset(
21593 26 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b  &pParse->aAlias[
21594 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c  pParse->nAliasAl
21595 6c 6f 63 5d 2c 20 30 2c 0a 20 20 20 20 20 20 20  loc], 0,.       
21596 20 20 20 20 28 70 50 61 72 73 65 2d 3e 6e 41 6c      (pParse->nAl
21597 69 61 73 2d 70 50 61 72 73 65 2d 3e 6e 41 6c 69  ias-pParse->nAli
21598 61 73 41 6c 6c 6f 63 29 2a 73 69 7a 65 6f 66 28  asAlloc)*sizeof(
21599 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 30  pParse->aAlias[0
2159a 5d 29 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  ]));.    pParse-
2159b 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 20 3d 20 70  >nAliasAlloc = p
2159c 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 3b 0a 20  Parse->nAlias;. 
2159d 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69 41 6c   }.  assert( iAl
2159e 69 61 73 3e 30 20 26 26 20 69 41 6c 69 61 73 3c  ias>0 && iAlias<
2159f 3d 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20  =pParse->nAlias 
215a0 29 3b 0a 20 20 69 52 65 67 20 3d 20 70 50 61 72  );.  iReg = pPar
215a1 73 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69 61  se->aAlias[iAlia
215a2 73 2d 31 5d 3b 0a 20 20 69 66 28 20 69 52 65 67  s-1];.  if( iReg
215a3 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ==0 ){.    if( p
215a4 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76  Parse->iCacheLev
215a5 65 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 52  el>0 ){.      iR
215a6 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  eg = sqlite3Expr
215a7 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73  CodeTarget(pPars
215a8 65 2c 20 70 45 78 70 72 2c 20 74 61 72 67 65 74  e, pExpr, target
215a9 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
215aa 20 20 20 20 69 52 65 67 20 3d 20 2b 2b 70 50 61      iReg = ++pPa
215ab 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20  rse->nMem;.     
215ac 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
215ad 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
215ae 69 52 65 67 29 3b 0a 20 20 20 20 20 20 70 50 61  iReg);.      pPa
215af 72 73 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c 69  rse->aAlias[iAli
215b0 61 73 2d 31 5d 20 3d 20 69 52 65 67 3b 0a 20 20  as-1] = iReg;.  
215b1 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
215b2 20 69 52 65 67 3b 0a 23 65 6c 73 65 0a 20 20 55   iReg;.#else.  U
215b3 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
215b4 69 41 6c 69 61 73 29 3b 0a 20 20 72 65 74 75 72  iAlias);.  retur
215b5 6e 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  n sqlite3ExprCod
215b6 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20  eTarget(pParse, 
215b7 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a  pExpr, target);.
215b8 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
215b9 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e  Generate code in
215ba 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 56  to the current V
215bb 64 62 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20  dbe to evaluate 
215bc 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70  the given.** exp
215bd 72 65 73 73 69 6f 6e 2e 20 20 41 74 74 65 6d 70  ression.  Attemp
215be 74 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72  t to store the r
215bf 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74  esults in regist
215c0 65 72 20 22 74 61 72 67 65 74 22 2e 0a 2a 2a 20  er "target"..** 
215c1 52 65 74 75 72 6e 20 74 68 65 20 72 65 67 69 73  Return the regis
215c2 74 65 72 20 77 68 65 72 65 20 72 65 73 75 6c 74  ter where result
215c3 73 20 61 72 65 20 73 74 6f 72 65 64 2e 0a 2a 2a  s are stored..**
215c4 0a 2a 2a 20 57 69 74 68 20 74 68 69 73 20 72 6f  .** With this ro
215c5 75 74 69 6e 65 2c 20 74 68 65 72 65 20 69 73 20  utine, there is 
215c6 6e 6f 20 67 75 61 72 61 6e 74 65 65 20 74 68 61  no guarantee tha
215c7 74 20 72 65 73 75 6c 74 73 20 77 69 6c 6c 0a 2a  t results will.*
215c8 2a 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 74  * be stored in t
215c9 61 72 67 65 74 2e 20 20 54 68 65 20 72 65 73 75  arget.  The resu
215ca 6c 74 20 6d 69 67 68 74 20 62 65 20 73 74 6f 72  lt might be stor
215cb 65 64 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72  ed in some other
215cc 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 69 66 20  .** register if 
215cd 69 74 20 69 73 20 63 6f 6e 76 65 6e 69 65 6e 74  it is convenient
215ce 20 74 6f 20 64 6f 20 73 6f 2e 20 20 54 68 65 20   to do so.  The 
215cf 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
215d0 0a 2a 2a 20 6d 75 73 74 20 63 68 65 63 6b 20 74  .** must check t
215d1 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 61  he return code a
215d2 6e 64 20 6d 6f 76 65 20 74 68 65 20 72 65 73 75  nd move the resu
215d3 6c 74 73 20 74 6f 20 74 68 65 20 64 65 73 69 72  lts to the desir
215d4 65 64 0a 2a 2a 20 72 65 67 69 73 74 65 72 2e 0a  ed.** register..
215d5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
215d6 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
215d7 72 43 6f 64 65 54 61 72 67 65 74 28 50 61 72 73  rCodeTarget(Pars
215d8 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
215d9 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67  *pExpr, int targ
215da 65 74 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  et){.  Vdbe *v =
215db 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20   pParse->pVdbe; 
215dc 20 2f 2a 20 54 68 65 20 56 4d 20 75 6e 64 65 72   /* The VM under
215dd 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f   construction */
215de 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20  .  int op;      
215df 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
215e0 54 68 65 20 6f 70 63 6f 64 65 20 62 65 69 6e 67  The opcode being
215e1 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20   coded */.  int 
215e2 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 20  inReg = target; 
215e3 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73        /* Results
215e4 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
215e5 74 65 72 20 69 6e 52 65 67 20 2a 2f 0a 20 20 69  ter inReg */.  i
215e6 6e 74 20 72 65 67 46 72 65 65 31 20 3d 20 30 3b  nt regFree1 = 0;
215e7 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e           /* If n
215e8 6f 6e 2d 7a 65 72 6f 20 66 72 65 65 20 74 68 69  on-zero free thi
215e9 73 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69  s temporary regi
215ea 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ster */.  int re
215eb 67 46 72 65 65 32 20 3d 20 30 3b 20 20 20 20 20  gFree2 = 0;     
215ec 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65      /* If non-ze
215ed 72 6f 20 66 72 65 65 20 74 68 69 73 20 74 65 6d  ro free this tem
215ee 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 20  porary register 
215ef 2a 2f 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 2c  */.  int r1, r2,
215f0 20 72 33 2c 20 72 34 3b 20 20 20 20 20 20 20 2f   r3, r4;       /
215f1 2a 20 56 61 72 69 6f 75 73 20 72 65 67 69 73 74  * Various regist
215f2 65 72 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 20 20  er numbers */.  
215f3 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
215f4 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 54 68 65  arse->db; /* The
215f5 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
215f6 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72  tion */..  asser
215f7 74 28 20 74 61 72 67 65 74 3e 30 20 26 26 20 74  t( target>0 && t
215f8 61 72 67 65 74 3c 3d 70 50 61 72 73 65 2d 3e 6e  arget<=pParse->n
215f9 4d 65 6d 20 29 3b 0a 20 20 69 66 28 20 76 3d 3d  Mem );.  if( v==
215fa 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
215fb 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c   pParse->db->mal
215fc 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20  locFailed );.   
215fd 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a   return 0;.  }..
215fe 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29    if( pExpr==0 )
215ff 7b 0a 20 20 20 20 6f 70 20 3d 20 54 4b 5f 4e 55  {.    op = TK_NU
21600 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  LL;.  }else{.   
21601 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b   op = pExpr->op;
21602 0a 20 20 7d 0a 20 20 73 77 69 74 63 68 28 20 6f  .  }.  switch( o
21603 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b  p ){.    case TK
21604 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20  _AGG_COLUMN: {. 
21605 20 20 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 41       AggInfo *pA
21606 67 67 49 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e  ggInfo = pExpr->
21607 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20  pAggInfo;.      
21608 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 63  struct AggInfo_c
21609 6f 6c 20 2a 70 43 6f 6c 20 3d 20 26 70 41 67 67  ol *pCol = &pAgg
2160a 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 70 45 78 70 72  Info->aCol[pExpr
2160b 2d 3e 69 41 67 67 5d 3b 0a 20 20 20 20 20 20 69  ->iAgg];.      i
2160c 66 28 20 21 70 41 67 67 49 6e 66 6f 2d 3e 64 69  f( !pAggInfo->di
2160d 72 65 63 74 4d 6f 64 65 20 29 7b 0a 20 20 20 20  rectMode ){.    
2160e 20 20 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c      assert( pCol
2160f 2d 3e 69 4d 65 6d 3e 30 20 29 3b 0a 20 20 20 20  ->iMem>0 );.    
21610 20 20 20 20 69 6e 52 65 67 20 3d 20 70 43 6f 6c      inReg = pCol
21611 2d 3e 69 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20  ->iMem;.        
21612 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c  break;.      }el
21613 73 65 20 69 66 28 20 70 41 67 67 49 6e 66 6f 2d  se if( pAggInfo-
21614 3e 75 73 65 53 6f 72 74 69 6e 67 49 64 78 20 29  >useSortingIdx )
21615 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
21616 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
21617 50 5f 43 6f 6c 75 6d 6e 2c 20 70 41 67 67 49 6e  P_Column, pAggIn
21618 66 6f 2d 3e 73 6f 72 74 69 6e 67 49 64 78 2c 0a  fo->sortingIdx,.
21619 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2161a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43                pC
2161b 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d  ol->iSorterColum
2161c 6e 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  n, target);.    
2161d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2161e 20 7d 0a 20 20 20 20 20 20 2f 2a 20 4f 74 68 65   }.      /* Othe
2161f 72 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 75  rwise, fall thru
21620 20 69 6e 74 6f 20 74 68 65 20 54 4b 5f 43 4f 4c   into the TK_COL
21621 55 4d 4e 20 63 61 73 65 20 2a 2f 0a 20 20 20 20  UMN case */.    
21622 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f  }.    case TK_CO
21623 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 69 66  LUMN: {.      if
21624 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3c  ( pExpr->iTable<
21625 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0 ){.        /* 
21626 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e  This only happen
21627 73 20 77 68 65 6e 20 63 6f 64 69 6e 67 20 63 68  s when coding ch
21628 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  eck constraints 
21629 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  */.        asser
2162a 74 28 20 70 50 61 72 73 65 2d 3e 63 6b 42 61 73  t( pParse->ckBas
2162b 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69  e>0 );.        i
2162c 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 43  nReg = pExpr->iC
2162d 6f 6c 75 6d 6e 20 2b 20 70 50 61 72 73 65 2d 3e  olumn + pParse->
2162e 63 6b 42 61 73 65 3b 0a 20 20 20 20 20 20 7d 65  ckBase;.      }e
2162f 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 74 65 73  lse{.        tes
21630 74 63 61 73 65 28 20 28 70 45 78 70 72 2d 3e 66  tcase( (pExpr->f
21631 6c 61 67 73 20 26 20 45 50 5f 41 6e 79 41 66 66  lags & EP_AnyAff
21632 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  )!=0 );.        
21633 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45  inReg = sqlite3E
21634 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e  xprCodeGetColumn
21635 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
21636 70 54 61 62 2c 0a 20 20 20 20 20 20 20 20 20 20  pTab,.          
21637 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21638 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43         pExpr->iC
21639 6f 6c 75 6d 6e 2c 20 70 45 78 70 72 2d 3e 69 54  olumn, pExpr->iT
2163a 61 62 6c 65 2c 20 74 61 72 67 65 74 2c 0a 20 20  able, target,.  
2163b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2163c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
2163d 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50  Expr->flags & EP
2163e 5f 41 6e 79 41 66 66 29 3b 0a 20 20 20 20 20 20  _AnyAff);.      
2163f 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
21640 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
21641 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 20  _INTEGER: {.    
21642 20 20 63 6f 64 65 49 6e 74 65 67 65 72 28 76 2c    codeInteger(v,
21643 20 70 45 78 70 72 2c 20 30 2c 20 74 61 72 67 65   pExpr, 0, targe
21644 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  t);.      break;
21645 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
21646 54 4b 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20  TK_FLOAT: {.    
21647 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
21648 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
21649 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29  , EP_IntValue) )
2164a 3b 0a 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c  ;.      codeReal
2164b 28 76 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f  (v, pExpr->u.zTo
2164c 6b 65 6e 2c 20 30 2c 20 74 61 72 67 65 74 29 3b  ken, 0, target);
2164d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2164e 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
2164f 53 54 52 49 4e 47 3a 20 7b 0a 20 20 20 20 20 20  STRING: {.      
21650 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
21651 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
21652 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a  EP_IntValue) );.
21653 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21654 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
21655 72 69 6e 67 38 2c 20 30 2c 20 74 61 72 67 65 74  ring8, 0, target
21656 2c 20 30 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54  , 0, pExpr->u.zT
21657 6f 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20  oken, 0);.      
21658 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
21659 20 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 20 7b   case TK_NULL: {
2165a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2165b 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
2165c 75 6c 6c 2c 20 30 2c 20 74 61 72 67 65 74 29 3b  ull, 0, target);
2165d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2165e 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
2165f 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54  TE_OMIT_BLOB_LIT
21660 45 52 41 4c 0a 20 20 20 20 63 61 73 65 20 54 4b  ERAL.    case TK
21661 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 69  _BLOB: {.      i
21662 6e 74 20 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73  nt n;.      cons
21663 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20  t char *z;.     
21664 20 63 68 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20   char *zBlob;.  
21665 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
21666 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
21667 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29  pr, EP_IntValue)
21668 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
21669 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  ( pExpr->u.zToke
2166a 6e 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 70 45 78  n[0]=='x' || pEx
2166b 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d  pr->u.zToken[0]=
2166c 3d 27 58 27 20 29 3b 0a 20 20 20 20 20 20 61 73  ='X' );.      as
2166d 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a  sert( pExpr->u.z
2166e 54 6f 6b 65 6e 5b 31 5d 3d 3d 27 5c 27 27 20 29  Token[1]=='\'' )
2166f 3b 0a 20 20 20 20 20 20 7a 20 3d 20 26 70 45 78  ;.      z = &pEx
21670 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 32 5d 3b  pr->u.zToken[2];
21671 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c 69 74  .      n = sqlit
21672 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 20 2d 20  e3Strlen30(z) - 
21673 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  1;.      assert(
21674 20 7a 5b 6e 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20   z[n]=='\'' );. 
21675 20 20 20 20 20 7a 42 6c 6f 62 20 3d 20 73 71 6c       zBlob = sql
21676 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 73 71  ite3HexToBlob(sq
21677 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 2c 20  lite3VdbeDb(v), 
21678 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c  z, n);.      sql
21679 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2167a 2c 20 4f 50 5f 42 6c 6f 62 2c 20 6e 2f 32 2c 20  , OP_Blob, n/2, 
2167b 74 61 72 67 65 74 2c 20 30 2c 20 7a 42 6c 6f 62  target, 0, zBlob
2167c 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  , P4_DYNAMIC);. 
2167d 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2167e 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73  }.#endif.    cas
2167f 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b  e TK_VARIABLE: {
21680 0a 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70  .      VdbeOp *p
21681 4f 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  Op;.      assert
21682 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
21683 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74  ty(pExpr, EP_Int
21684 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20  Value) );.      
21685 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75  assert( pExpr->u
21686 2e 7a 54 6f 6b 65 6e 21 3d 30 20 29 3b 0a 20 20  .zToken!=0 );.  
21687 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
21688 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 21 3d  r->u.zToken[0]!=
21689 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  0 );.      if( p
2168a 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31  Expr->u.zToken[1
2168b 5d 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26  ]==0.         &&
2168c 20 28 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56   (pOp = sqlite3V
2168d 64 62 65 47 65 74 4f 70 28 76 2c 20 2d 31 29 29  dbeGetOp(v, -1))
2168e 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 61 72  ->opcode==OP_Var
2168f 69 61 62 6c 65 0a 20 20 20 20 20 20 20 20 20 26  iable.         &
21690 26 20 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70  & pOp->p1+pOp->p
21691 33 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  3==pExpr->iTable
21692 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 4f 70  .         && pOp
21693 2d 3e 70 32 2b 70 4f 70 2d 3e 70 33 3d 3d 74 61  ->p2+pOp->p3==ta
21694 72 67 65 74 0a 20 20 20 20 20 20 20 20 20 26 26  rget.         &&
21695 20 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 0a 20 20   pOp->p4.z==0.  
21696 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 2f      ){.        /
21697 2a 20 49 66 20 74 68 65 20 70 72 65 76 69 6f 75  * If the previou
21698 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 61  s instruction wa
21699 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
2169a 70 72 65 76 69 6f 75 73 20 75 6e 6e 61 6d 65 64  previous unnamed
2169b 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 72 61  .        ** para
2169c 6d 65 74 65 72 20 69 6e 74 6f 20 74 68 65 20 70  meter into the p
2169d 72 65 76 69 6f 75 73 20 72 65 67 69 73 74 65 72  revious register
2169e 2c 20 74 68 65 6e 20 73 69 6d 70 6c 79 20 69 6e  , then simply in
2169f 63 72 65 6d 65 6e 74 20 74 68 65 0a 20 20 20 20  crement the.    
216a0 20 20 20 20 2a 2a 20 72 65 70 65 61 74 20 63 6f      ** repeat co
216a1 75 6e 74 20 6f 6e 20 74 68 65 20 70 72 69 6f 72  unt on the prior
216a2 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 72 61 74   instruction rat
216a3 68 65 72 20 74 68 61 6e 20 6d 61 6b 69 6e 67 20  her than making 
216a4 61 20 6e 65 77 0a 20 20 20 20 20 20 20 20 2a 2a  a new.        **
216a5 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 20 20   instruction..  
216a6 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
216a7 20 70 4f 70 2d 3e 70 33 2b 2b 3b 0a 20 20 20 20   pOp->p3++;.    
216a8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
216a9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
216aa 70 33 28 76 2c 20 4f 50 5f 56 61 72 69 61 62 6c  p3(v, OP_Variabl
216ab 65 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  e, pExpr->iTable
216ac 2c 20 74 61 72 67 65 74 2c 20 31 29 3b 0a 20 20  , target, 1);.  
216ad 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
216ae 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 21 3d 30 20  >u.zToken[1]!=0 
216af 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
216b0 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
216b1 28 76 2c 20 2d 31 2c 20 70 45 78 70 72 2d 3e 75  (v, -1, pExpr->u
216b2 2e 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a 20 20 20  .zToken, 0);.   
216b3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
216b4 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
216b5 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45  }.    case TK_RE
216b6 47 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20  GISTER: {.      
216b7 69 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69  inReg = pExpr->i
216b8 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 62 72 65  Table;.      bre
216b9 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
216ba 73 65 20 54 4b 5f 41 53 3a 20 7b 0a 20 20 20 20  se TK_AS: {.    
216bb 20 20 69 6e 52 65 67 20 3d 20 63 6f 64 65 41 6c    inReg = codeAl
216bc 69 61 73 28 70 50 61 72 73 65 2c 20 70 45 78 70  ias(pParse, pExp
216bd 72 2d 3e 69 54 61 62 6c 65 2c 20 70 45 78 70 72  r->iTable, pExpr
216be 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29  ->pLeft, target)
216bf 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
216c0 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
216c1 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20  ITE_OMIT_CAST.  
216c2 20 20 63 61 73 65 20 54 4b 5f 43 41 53 54 3a 20    case TK_CAST: 
216c3 7b 0a 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65  {.      /* Expre
216c4 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f  ssions of the fo
216c5 72 6d 3a 20 20 20 43 41 53 54 28 70 4c 65 66 74  rm:   CAST(pLeft
216c6 20 41 53 20 74 6f 6b 65 6e 29 20 2a 2f 0a 20 20   AS token) */.  
216c7 20 20 20 20 69 6e 74 20 61 66 66 2c 20 74 6f 5f      int aff, to_
216c8 6f 70 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20  op;.      inReg 
216c9 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
216ca 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20  eTarget(pParse, 
216cb 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61  pExpr->pLeft, ta
216cc 72 67 65 74 29 3b 0a 20 20 20 20 20 20 61 73 73  rget);.      ass
216cd 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
216ce 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
216cf 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20  IntValue) );.   
216d0 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33     aff = sqlite3
216d1 41 66 66 69 6e 69 74 79 54 79 70 65 28 70 45 78  AffinityType(pEx
216d2 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20  pr->u.zToken);. 
216d3 20 20 20 20 20 74 6f 5f 6f 70 20 3d 20 61 66 66       to_op = aff
216d4 20 2d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45   - SQLITE_AFF_TE
216d5 58 54 20 2b 20 4f 50 5f 54 6f 54 65 78 74 3b 0a  XT + OP_ToText;.
216d6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f        assert( to
216d7 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 20  _op==OP_ToText  
216d8 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45    || aff!=SQLITE
216d9 5f 41 46 46 5f 54 45 58 54 20 20 20 20 29 3b 0a  _AFF_TEXT    );.
216da 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f        assert( to
216db 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 20  _op==OP_ToBlob  
216dc 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45    || aff!=SQLITE
216dd 5f 41 46 46 5f 4e 4f 4e 45 20 20 20 20 29 3b 0a  _AFF_NONE    );.
216de 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f        assert( to
216df 5f 6f 70 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69  _op==OP_ToNumeri
216e0 63 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45  c || aff!=SQLITE
216e1 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a  _AFF_NUMERIC );.
216e2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f        assert( to
216e3 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 20 20  _op==OP_ToInt   
216e4 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45    || aff!=SQLITE
216e5 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 29 3b 0a  _AFF_INTEGER );.
216e6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 74 6f        assert( to
216e7 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 20  _op==OP_ToReal  
216e8 20 20 7c 7c 20 61 66 66 21 3d 53 51 4c 49 54 45    || aff!=SQLITE
216e9 5f 41 46 46 5f 52 45 41 4c 20 20 20 20 29 3b 0a  _AFF_REAL    );.
216ea 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
216eb 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74  to_op==OP_ToText
216ec 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
216ed 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f  se( to_op==OP_To
216ee 42 6c 6f 62 20 29 3b 0a 20 20 20 20 20 20 74 65  Blob );.      te
216ef 73 74 63 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f  stcase( to_op==O
216f0 50 5f 54 6f 4e 75 6d 65 72 69 63 20 29 3b 0a 20  P_ToNumeric );. 
216f1 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74       testcase( t
216f2 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 29  o_op==OP_ToInt )
216f3 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
216f4 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65  ( to_op==OP_ToRe
216f5 61 6c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  al );.      if( 
216f6 69 6e 52 65 67 21 3d 74 61 72 67 65 74 20 29 7b  inReg!=target ){
216f7 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
216f8 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
216f9 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74  _SCopy, inReg, t
216fa 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20  arget);.        
216fb 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a  inReg = target;.
216fc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
216fd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
216fe 76 2c 20 74 6f 5f 6f 70 2c 20 69 6e 52 65 67 29  v, to_op, inReg)
216ff 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21700 28 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61  ( usedAsColumnCa
21701 63 68 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65  che(pParse, inRe
21702 67 2c 20 69 6e 52 65 67 29 20 29 3b 0a 20 20 20  g, inReg) );.   
21703 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
21704 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
21705 65 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c  e(pParse, inReg,
21706 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   1);.      break
21707 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f  ;.    }.#endif /
21708 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41  * SQLITE_OMIT_CA
21709 53 54 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54  ST */.    case T
2170a 4b 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 20 54  K_LT:.    case T
2170b 4b 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54  K_LE:.    case T
2170c 4b 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 20 54  K_GT:.    case T
2170d 4b 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 20 54  K_GE:.    case T
2170e 4b 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20 54  K_NE:.    case T
2170f 4b 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 61 73  K_EQ: {.      as
21710 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f  sert( TK_LT==OP_
21711 4c 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  Lt );.      asse
21712 72 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65  rt( TK_LE==OP_Le
21713 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
21714 28 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29  ( TK_GT==OP_Gt )
21715 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
21716 54 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a  TK_GE==OP_Ge );.
21717 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
21718 5f 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20  _EQ==OP_Eq );.  
21719 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e      assert( TK_N
2171a 45 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20  E==OP_Ne );.    
2171b 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
2171c 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 74  TK_LT );.      t
2171d 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
2171e 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  LE );.      test
2171f 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20  case( op==TK_GT 
21720 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21721 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a  e( op==TK_GE );.
21722 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21723 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20  op==TK_EQ );.   
21724 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
21725 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20  =TK_NE );.      
21726 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61  codeCompareOpera
21727 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70  nds(pParse, pExp
21728 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26  r->pLeft, &r1, &
21729 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20  regFree1,.      
2172a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2172b 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70              pExp
2172c 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20  r->pRight, &r2, 
2172d 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20  &regFree2);.    
2172e 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50    codeCompare(pP
2172f 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
21730 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  ft, pExpr->pRigh
21731 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20  t, op,.         
21732 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c           r1, r2,
21733 20 69 6e 52 65 67 2c 20 53 51 4c 49 54 45 5f 53   inReg, SQLITE_S
21734 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 20 74  TOREP2);.      t
21735 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
21736 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65  1==0 );.      te
21737 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32  stcase( regFree2
21738 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65  ==0 );.      bre
21739 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
2173a 73 65 20 54 4b 5f 49 53 3a 0a 20 20 20 20 63 61  se TK_IS:.    ca
2173b 73 65 20 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20  se TK_ISNOT: {. 
2173c 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
2173d 70 3d 3d 54 4b 5f 49 53 20 29 3b 0a 20 20 20 20  p==TK_IS );.    
2173e 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
2173f 54 4b 5f 49 53 4e 4f 54 20 29 3b 0a 20 20 20 20  TK_ISNOT );.    
21740 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65    codeCompareOpe
21741 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45  rands(pParse, pE
21742 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c  xpr->pLeft, &r1,
21743 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20   &regFree1,.    
21744 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21745 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
21746 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32  xpr->pRight, &r2
21747 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20  , &regFree2);.  
21748 20 20 20 20 6f 70 20 3d 20 28 6f 70 3d 3d 54 4b      op = (op==TK
21749 5f 49 53 29 20 3f 20 54 4b 5f 45 51 20 3a 20 54  _IS) ? TK_EQ : T
2174a 4b 5f 4e 45 3b 0a 20 20 20 20 20 20 63 6f 64 65  K_NE;.      code
2174b 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20  Compare(pParse, 
2174c 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45  pExpr->pLeft, pE
2174d 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c  xpr->pRight, op,
2174e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2174f 20 20 20 72 31 2c 20 72 32 2c 20 69 6e 52 65 67     r1, r2, inReg
21750 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32  , SQLITE_STOREP2
21751 20 7c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51   | SQLITE_NULLEQ
21752 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21753 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29  e( regFree1==0 )
21754 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21755 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b  ( regFree2==0 );
21756 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
21757 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
21758 41 4e 44 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  AND:.    case TK
21759 5f 4f 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  _OR:.    case TK
2175a 5f 50 4c 55 53 3a 0a 20 20 20 20 63 61 73 65 20  _PLUS:.    case 
2175b 54 4b 5f 53 54 41 52 3a 0a 20 20 20 20 63 61 73  TK_STAR:.    cas
2175c 65 20 54 4b 5f 4d 49 4e 55 53 3a 0a 20 20 20 20  e TK_MINUS:.    
2175d 63 61 73 65 20 54 4b 5f 52 45 4d 3a 0a 20 20 20  case TK_REM:.   
2175e 20 63 61 73 65 20 54 4b 5f 42 49 54 41 4e 44 3a   case TK_BITAND:
2175f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49 54  .    case TK_BIT
21760 4f 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  OR:.    case TK_
21761 53 4c 41 53 48 3a 0a 20 20 20 20 63 61 73 65 20  SLASH:.    case 
21762 54 4b 5f 4c 53 48 49 46 54 3a 0a 20 20 20 20 63  TK_LSHIFT:.    c
21763 61 73 65 20 54 4b 5f 52 53 48 49 46 54 3a 20 0a  ase TK_RSHIFT: .
21764 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 43      case TK_CONC
21765 41 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65  AT: {.      asse
21766 72 74 28 20 54 4b 5f 41 4e 44 3d 3d 4f 50 5f 41  rt( TK_AND==OP_A
21767 6e 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  nd );.      asse
21768 72 74 28 20 54 4b 5f 4f 52 3d 3d 4f 50 5f 4f 72  rt( TK_OR==OP_Or
21769 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
2176a 28 20 54 4b 5f 50 4c 55 53 3d 3d 4f 50 5f 41 64  ( TK_PLUS==OP_Ad
2176b 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  d );.      asser
2176c 74 28 20 54 4b 5f 4d 49 4e 55 53 3d 3d 4f 50 5f  t( TK_MINUS==OP_
2176d 53 75 62 74 72 61 63 74 20 29 3b 0a 20 20 20 20  Subtract );.    
2176e 20 20 61 73 73 65 72 74 28 20 54 4b 5f 52 45 4d    assert( TK_REM
2176f 3d 3d 4f 50 5f 52 65 6d 61 69 6e 64 65 72 20 29  ==OP_Remainder )
21770 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
21771 54 4b 5f 42 49 54 41 4e 44 3d 3d 4f 50 5f 42 69  TK_BITAND==OP_Bi
21772 74 41 6e 64 20 29 3b 0a 20 20 20 20 20 20 61 73  tAnd );.      as
21773 73 65 72 74 28 20 54 4b 5f 42 49 54 4f 52 3d 3d  sert( TK_BITOR==
21774 4f 50 5f 42 69 74 4f 72 20 29 3b 0a 20 20 20 20  OP_BitOr );.    
21775 20 20 61 73 73 65 72 74 28 20 54 4b 5f 53 4c 41    assert( TK_SLA
21776 53 48 3d 3d 4f 50 5f 44 69 76 69 64 65 20 29 3b  SH==OP_Divide );
21777 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
21778 4b 5f 4c 53 48 49 46 54 3d 3d 4f 50 5f 53 68 69  K_LSHIFT==OP_Shi
21779 66 74 4c 65 66 74 20 29 3b 0a 20 20 20 20 20 20  ftLeft );.      
2177a 61 73 73 65 72 74 28 20 54 4b 5f 52 53 48 49 46  assert( TK_RSHIF
2177b 54 3d 3d 4f 50 5f 53 68 69 66 74 52 69 67 68 74  T==OP_ShiftRight
2177c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
2177d 28 20 54 4b 5f 43 4f 4e 43 41 54 3d 3d 4f 50 5f  ( TK_CONCAT==OP_
2177e 43 6f 6e 63 61 74 20 29 3b 0a 20 20 20 20 20 20  Concat );.      
2177f 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
21780 5f 41 4e 44 20 29 3b 0a 20 20 20 20 20 20 74 65  _AND );.      te
21781 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4f  stcase( op==TK_O
21782 52 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  R );.      testc
21783 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 50 4c 55 53  ase( op==TK_PLUS
21784 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21785 73 65 28 20 6f 70 3d 3d 54 4b 5f 4d 49 4e 55 53  se( op==TK_MINUS
21786 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21787 73 65 28 20 6f 70 3d 3d 54 4b 5f 52 45 4d 20 29  se( op==TK_REM )
21788 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21789 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 41 4e 44 20  ( op==TK_BITAND 
2178a 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
2178b 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 4f 52 20  e( op==TK_BITOR 
2178c 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
2178d 65 28 20 6f 70 3d 3d 54 4b 5f 53 4c 41 53 48 20  e( op==TK_SLASH 
2178e 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
2178f 65 28 20 6f 70 3d 3d 54 4b 5f 4c 53 48 49 46 54  e( op==TK_LSHIFT
21790 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21791 73 65 28 20 6f 70 3d 3d 54 4b 5f 52 53 48 49 46  se( op==TK_RSHIF
21792 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  T );.      testc
21793 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 43  ase( op==TK_CONC
21794 41 54 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d  AT );.      r1 =
21795 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21796 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78  Temp(pParse, pEx
21797 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46  pr->pLeft, &regF
21798 72 65 65 31 29 3b 0a 20 20 20 20 20 20 72 32 20  ree1);.      r2 
21799 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
2179a 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45  eTemp(pParse, pE
2179b 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 65  xpr->pRight, &re
2179c 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 73  gFree2);.      s
2179d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2179e 28 76 2c 20 6f 70 2c 20 72 32 2c 20 72 31 2c 20  (v, op, r2, r1, 
2179f 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 74  target);.      t
217a0 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
217a1 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65  1==0 );.      te
217a2 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32  stcase( regFree2
217a3 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65  ==0 );.      bre
217a4 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
217a5 73 65 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a  se TK_UMINUS: {.
217a6 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66        Expr *pLef
217a7 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  t = pExpr->pLeft
217a8 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
217a9 70 4c 65 66 74 20 29 3b 0a 20 20 20 20 20 20 69  pLeft );.      i
217aa 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b  f( pLeft->op==TK
217ab 5f 46 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20 20  _FLOAT ){.      
217ac 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
217ad 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
217ae 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29  , EP_IntValue) )
217af 3b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 52 65  ;.        codeRe
217b0 61 6c 28 76 2c 20 70 4c 65 66 74 2d 3e 75 2e 7a  al(v, pLeft->u.z
217b1 54 6f 6b 65 6e 2c 20 31 2c 20 74 61 72 67 65 74  Token, 1, target
217b2 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  );.      }else i
217b3 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b  f( pLeft->op==TK
217b4 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20  _INTEGER ){.    
217b5 20 20 20 20 63 6f 64 65 49 6e 74 65 67 65 72 28      codeInteger(
217b6 76 2c 20 70 4c 65 66 74 2c 20 31 2c 20 74 61 72  v, pLeft, 1, tar
217b7 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  get);.      }els
217b8 65 7b 0a 20 20 20 20 20 20 20 20 72 65 67 46 72  e{.        regFr
217b9 65 65 31 20 3d 20 72 31 20 3d 20 73 71 6c 69 74  ee1 = r1 = sqlit
217ba 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
217bb 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71  rse);.        sq
217bc 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
217bd 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
217be 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 72  , r1);.        r
217bf 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  2 = sqlite3ExprC
217c0 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20  odeTemp(pParse, 
217c1 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72  pExpr->pLeft, &r
217c2 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20  egFree2);.      
217c3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
217c4 4f 70 33 28 76 2c 20 4f 50 5f 53 75 62 74 72 61  Op3(v, OP_Subtra
217c5 63 74 2c 20 72 32 2c 20 72 31 2c 20 74 61 72 67  ct, r2, r1, targ
217c6 65 74 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73  et);.        tes
217c7 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d  tcase( regFree2=
217c8 3d 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  =0 );.      }.  
217c9 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61 72 67      inReg = targ
217ca 65 74 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  et;.      break;
217cb 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
217cc 54 4b 5f 42 49 54 4e 4f 54 3a 0a 20 20 20 20 63  TK_BITNOT:.    c
217cd 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20  ase TK_NOT: {.  
217ce 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 42      assert( TK_B
217cf 49 54 4e 4f 54 3d 3d 4f 50 5f 42 69 74 4e 6f 74  ITNOT==OP_BitNot
217d0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
217d1 28 20 54 4b 5f 4e 4f 54 3d 3d 4f 50 5f 4e 6f 74  ( TK_NOT==OP_Not
217d2 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
217d3 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 4e 4f  se( op==TK_BITNO
217d4 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  T );.      testc
217d5 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 20  ase( op==TK_NOT 
217d6 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71  );.      r1 = sq
217d7 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
217d8 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  p(pParse, pExpr-
217d9 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65  >pLeft, &regFree
217da 31 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  1);.      testca
217db 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20  se( regFree1==0 
217dc 29 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d  );.      inReg =
217dd 20 74 61 72 67 65 74 3b 0a 20 20 20 20 20 20 73   target;.      s
217de 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
217df 28 76 2c 20 6f 70 2c 20 72 31 2c 20 69 6e 52 65  (v, op, r1, inRe
217e0 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  g);.      break;
217e1 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
217e2 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63  TK_ISNULL:.    c
217e3 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20  ase TK_NOTNULL: 
217e4 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72  {.      int addr
217e5 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
217e6 54 4b 5f 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73  TK_ISNULL==OP_Is
217e7 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 61 73  Null );.      as
217e8 73 65 72 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c  sert( TK_NOTNULL
217e9 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a  ==OP_NotNull );.
217ea 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
217eb 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b  op==TK_ISNULL );
217ec 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
217ed 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20   op==TK_NOTNULL 
217ee 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
217ef 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
217f0 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 74 61 72  _Integer, 1, tar
217f1 67 65 74 29 3b 0a 20 20 20 20 20 20 72 31 20 3d  get);.      r1 =
217f2 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
217f3 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78  Temp(pParse, pEx
217f4 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46  pr->pLeft, &regF
217f5 72 65 65 31 29 3b 0a 20 20 20 20 20 20 74 65 73  ree1);.      tes
217f6 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
217f7 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 64 64 72  =0 );.      addr
217f8 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
217f9 64 4f 70 31 28 76 2c 20 6f 70 2c 20 72 31 29 3b  dOp1(v, op, r1);
217fa 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
217fb 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
217fc 64 64 49 6d 6d 2c 20 74 61 72 67 65 74 2c 20 2d  ddImm, target, -
217fd 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
217fe 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
217ff 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 62 72   addr);.      br
21800 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
21801 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54  ase TK_AGG_FUNCT
21802 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 41 67 67  ION: {.      Agg
21803 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 45  Info *pInfo = pE
21804 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20  xpr->pAggInfo;. 
21805 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f 3d 3d       if( pInfo==
21806 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  0 ){.        ass
21807 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
21808 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
21809 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20  IntValue) );.   
2180a 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
2180b 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69  rMsg(pParse, "mi
2180c 73 75 73 65 20 6f 66 20 61 67 67 72 65 67 61 74  suse of aggregat
2180d 65 3a 20 25 73 28 29 22 2c 20 70 45 78 70 72 2d  e: %s()", pExpr-
2180e 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20  >u.zToken);.    
2180f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
21810 20 69 6e 52 65 67 20 3d 20 70 49 6e 66 6f 2d 3e   inReg = pInfo->
21811 61 46 75 6e 63 5b 70 45 78 70 72 2d 3e 69 41 67  aFunc[pExpr->iAg
21812 67 5d 2e 69 4d 65 6d 3b 0a 20 20 20 20 20 20 7d  g].iMem;.      }
21813 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
21814 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
21815 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20  CONST_FUNC:.    
21816 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e  case TK_FUNCTION
21817 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69  : {.      ExprLi
21818 73 74 20 2a 70 46 61 72 67 3b 20 20 20 20 20 20  st *pFarg;      
21819 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66 75 6e 63   /* List of func
2181a 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a  tion arguments *
2181b 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 61 72  /.      int nFar
2181c 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  g;             /
2181d 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75 6e 63  * Number of func
2181e 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a  tion arguments *
2181f 2f 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66 20  /.      FuncDef 
21820 2a 70 44 65 66 3b 20 20 20 20 20 20 20 20 20 2f  *pDef;         /
21821 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 64  * The function d
21822 65 66 69 6e 69 74 69 6f 6e 20 6f 62 6a 65 63 74  efinition object
21823 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 49   */.      int nI
21824 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
21825 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68   /* Length of th
21826 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20  e function name 
21827 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 20 20  in bytes */.    
21828 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49    const char *zI
21829 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  d;       /* The 
2182a 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f  function name */
2182b 0a 20 20 20 20 20 20 69 6e 74 20 63 6f 6e 73 74  .      int const
2182c 4d 61 73 6b 20 3d 20 30 3b 20 20 20 20 20 2f 2a  Mask = 0;     /*
2182d 20 4d 61 73 6b 20 6f 66 20 66 75 6e 63 74 69 6f   Mask of functio
2182e 6e 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74  n arguments that
2182f 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20 2a 2f   are constant */
21830 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20  .      int i;   
21831 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21832 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
21833 0a 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d 20  .      u8 enc = 
21834 45 4e 43 28 64 62 29 3b 20 20 20 20 20 20 2f 2a  ENC(db);      /*
21835 20 54 68 65 20 74 65 78 74 20 65 6e 63 6f 64 69   The text encodi
21836 6e 67 20 75 73 65 64 20 62 79 20 74 68 69 73 20  ng used by this 
21837 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20  database */.    
21838 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
21839 20 3d 20 30 3b 20 20 20 20 2f 2a 20 41 20 63 6f   = 0;    /* A co
2183a 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2183b 20 2a 2f 0a 0a 20 20 20 20 20 20 61 73 73 65 72   */..      asser
2183c 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  t( !ExprHasPrope
2183d 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49  rty(pExpr, EP_xI
2183e 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20  sSelect) );.    
2183f 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
21840 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 29 3b  TK_CONST_FUNC );
21841 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21842 20 6f 70 3d 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e   op==TK_FUNCTION
21843 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 45 78   );.      if( Ex
21844 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
21845 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e  (pExpr, EP_Token
21846 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 20  Only) ){.       
21847 20 70 46 61 72 67 20 3d 20 30 3b 0a 20 20 20 20   pFarg = 0;.    
21848 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
21849 20 70 46 61 72 67 20 3d 20 70 45 78 70 72 2d 3e   pFarg = pExpr->
2184a 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 7d  x.pList;.      }
2184b 0a 20 20 20 20 20 20 6e 46 61 72 67 20 3d 20 70  .      nFarg = p
2184c 46 61 72 67 20 3f 20 70 46 61 72 67 2d 3e 6e 45  Farg ? pFarg->nE
2184d 78 70 72 20 3a 20 30 3b 0a 20 20 20 20 20 20 61  xpr : 0;.      a
2184e 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
2184f 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
21850 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20  P_IntValue) );. 
21851 20 20 20 20 20 7a 49 64 20 3d 20 70 45 78 70 72       zId = pExpr
21852 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20  ->u.zToken;.    
21853 20 20 6e 49 64 20 3d 20 73 71 6c 69 74 65 33 53    nId = sqlite3S
21854 74 72 6c 65 6e 33 30 28 7a 49 64 29 3b 0a 20 20  trlen30(zId);.  
21855 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74      pDef = sqlit
21856 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64  e3FindFunction(d
21857 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 6e 46 61  b, zId, nId, nFa
21858 72 67 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20  rg, enc, 0);.   
21859 20 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20 29     if( pDef==0 )
2185a 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2185b 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2185c 2c 20 22 75 6e 6b 6e 6f 77 6e 20 66 75 6e 63 74  , "unknown funct
2185d 69 6f 6e 3a 20 25 2e 2a 73 28 29 22 2c 20 6e 49  ion: %.*s()", nI
2185e 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 20 20  d, zId);.       
2185f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
21860 20 20 20 20 20 20 69 66 28 20 70 46 61 72 67 20        if( pFarg 
21861 29 7b 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20  ){.        r1 = 
21862 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61  sqlite3GetTempRa
21863 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 46 61 72  nge(pParse, nFar
21864 67 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  g);.        sqli
21865 74 65 33 45 78 70 72 43 61 63 68 65 50 75 73 68  te3ExprCachePush
21866 28 70 50 61 72 73 65 29 3b 20 20 20 20 20 2f 2a  (pParse);     /*
21867 20 54 69 63 6b 65 74 20 32 65 61 32 34 32 35 64   Ticket 2ea2425d
21868 33 34 62 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  34be */.        
21869 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45  sqlite3ExprCodeE
2186a 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20  xprList(pParse, 
2186b 70 46 61 72 67 2c 20 72 31 2c 20 31 29 3b 0a 20  pFarg, r1, 1);. 
2186c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2186d 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73  prCachePop(pPars
2186e 65 2c 20 31 29 3b 20 20 20 2f 2a 20 54 69 63 6b  e, 1);   /* Tick
2186f 65 74 20 32 65 61 32 34 32 35 64 33 34 62 65 20  et 2ea2425d34be 
21870 2a 2f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  */.      }else{.
21871 20 20 20 20 20 20 20 20 72 31 20 3d 20 30 3b 0a          r1 = 0;.
21872 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20        }.#ifndef 
21873 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
21874 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 2f  UALTABLE.      /
21875 2a 20 50 6f 73 73 69 62 6c 79 20 6f 76 65 72 6c  * Possibly overl
21876 6f 61 64 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  oad the function
21877 20 69 66 20 74 68 65 20 66 69 72 73 74 20 61 72   if the first ar
21878 67 75 6d 65 6e 74 20 69 73 0a 20 20 20 20 20 20  gument is.      
21879 2a 2a 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  ** a virtual tab
2187a 6c 65 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20  le column..     
2187b 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 46 6f 72   **.      ** For
2187c 20 69 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e 73   infix functions
2187d 20 28 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 52 45   (LIKE, GLOB, RE
2187e 47 45 58 50 2c 20 61 6e 64 20 4d 41 54 43 48 29  GEXP, and MATCH)
2187f 20 75 73 65 20 74 68 65 0a 20 20 20 20 20 20 2a   use the.      *
21880 2a 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  * second argumen
21881 74 2c 20 6e 6f 74 20 74 68 65 20 66 69 72 73 74  t, not the first
21882 2c 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e  , as the argumen
21883 74 20 74 6f 20 74 65 73 74 20 74 6f 0a 20 20 20  t to test to.   
21884 20 20 20 2a 2a 20 73 65 65 20 69 66 20 69 74 20     ** see if it 
21885 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61  is a column in a
21886 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20   virtual table. 
21887 20 54 68 69 73 20 69 73 20 64 6f 6e 65 20 62 65   This is done be
21888 63 61 75 73 65 0a 20 20 20 20 20 20 2a 2a 20 74  cause.      ** t
21889 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20  he left operand 
2188a 6f 66 20 69 6e 66 69 78 20 66 75 6e 63 74 69 6f  of infix functio
2188b 6e 73 20 28 74 68 65 20 6f 70 65 72 61 6e 64 20  ns (the operand 
2188c 77 65 20 77 61 6e 74 20 74 6f 0a 20 20 20 20 20  we want to.     
2188d 20 2a 2a 20 63 6f 6e 74 72 6f 6c 20 6f 76 65 72   ** control over
2188e 6c 6f 61 64 69 6e 67 29 20 65 6e 64 73 20 75 70  loading) ends up
2188f 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
21890 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a 20  rgument to the. 
21891 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e       ** function
21892 2e 20 20 54 68 65 20 65 78 70 72 65 73 73 69 6f  .  The expressio
21893 6e 20 22 41 20 67 6c 6f 62 20 42 22 20 69 73 20  n "A glob B" is 
21894 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20  equivalent to . 
21895 20 20 20 20 20 2a 2a 20 22 67 6c 6f 62 28 42 2c       ** "glob(B,
21896 41 29 2e 20 20 57 65 20 77 61 6e 74 20 74 6f 20  A).  We want to 
21897 75 73 65 20 74 68 65 20 41 20 69 6e 20 22 41 20  use the A in "A 
21898 67 6c 6f 62 20 42 22 20 74 6f 20 74 65 73 74 0a  glob B" to test.
21899 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 66 75 6e        ** for fun
2189a 63 74 69 6f 6e 20 6f 76 65 72 6c 6f 61 64 69 6e  ction overloadin
2189b 67 2e 20 20 42 75 74 20 77 65 20 75 73 65 20 74  g.  But we use t
2189c 68 65 20 42 20 74 65 72 6d 20 69 6e 20 22 67 6c  he B term in "gl
2189d 6f 62 28 42 2c 41 29 22 2e 0a 20 20 20 20 20 20  ob(B,A)"..      
2189e 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 46 61  */.      if( nFa
2189f 72 67 3e 3d 32 20 26 26 20 28 70 45 78 70 72 2d  rg>=2 && (pExpr-
218a0 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 66 69  >flags & EP_Infi
218a1 78 46 75 6e 63 29 20 29 7b 0a 20 20 20 20 20 20  xFunc) ){.      
218a2 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33    pDef = sqlite3
218a3 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63  VtabOverloadFunc
218a4 74 69 6f 6e 28 64 62 2c 20 70 44 65 66 2c 20 6e  tion(db, pDef, n
218a5 46 61 72 67 2c 20 70 46 61 72 67 2d 3e 61 5b 31  Farg, pFarg->a[1
218a6 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  ].pExpr);.      
218a7 7d 65 6c 73 65 20 69 66 28 20 6e 46 61 72 67 3e  }else if( nFarg>
218a8 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65  0 ){.        pDe
218a9 66 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 4f  f = sqlite3VtabO
218aa 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28  verloadFunction(
218ab 64 62 2c 20 70 44 65 66 2c 20 6e 46 61 72 67 2c  db, pDef, nFarg,
218ac 20 70 46 61 72 67 2d 3e 61 5b 30 5d 2e 70 45 78   pFarg->a[0].pEx
218ad 70 72 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  pr);.      }.#en
218ae 64 69 66 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  dif.      for(i=
218af 30 3b 20 69 3c 6e 46 61 72 67 3b 20 69 2b 2b 29  0; i<nFarg; i++)
218b0 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 3c  {.        if( i<
218b1 33 32 20 26 26 20 73 71 6c 69 74 65 33 45 78 70  32 && sqlite3Exp
218b2 72 49 73 43 6f 6e 73 74 61 6e 74 28 70 46 61 72  rIsConstant(pFar
218b3 67 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 29  g->a[i].pExpr) )
218b4 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73  {.          cons
218b5 74 4d 61 73 6b 20 7c 3d 20 28 31 3c 3c 69 29 3b  tMask |= (1<<i);
218b6 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
218b7 20 20 20 69 66 28 20 28 70 44 65 66 2d 3e 66 6c     if( (pDef->fl
218b8 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e  ags & SQLITE_FUN
218b9 43 5f 4e 45 45 44 43 4f 4c 4c 29 21 3d 30 20 26  C_NEEDCOLL)!=0 &
218ba 26 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  & !pColl ){.    
218bb 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71        pColl = sq
218bc 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
218bd 28 70 50 61 72 73 65 2c 20 70 46 61 72 67 2d 3e  (pParse, pFarg->
218be 61 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20  a[i].pExpr);.   
218bf 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
218c0 20 20 20 20 20 69 66 28 20 70 44 65 66 2d 3e 66       if( pDef->f
218c1 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55  lags & SQLITE_FU
218c2 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a 20  NC_NEEDCOLL ){. 
218c3 20 20 20 20 20 20 20 69 66 28 20 21 70 43 6f 6c         if( !pCol
218c4 6c 20 29 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e  l ) pColl = db->
218c5 70 44 66 6c 74 43 6f 6c 6c 3b 20 0a 20 20 20 20  pDfltColl; .    
218c6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
218c7 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6c 6c  ddOp4(v, OP_Coll
218c8 53 65 71 2c 20 30 2c 20 30 2c 20 30 2c 20 28 63  Seq, 0, 0, 0, (c
218c9 68 61 72 20 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f  har *)pColl, P4_
218ca 43 4f 4c 4c 53 45 51 29 3b 0a 20 20 20 20 20 20  COLLSEQ);.      
218cb 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  }.      sqlite3V
218cc 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
218cd 46 75 6e 63 74 69 6f 6e 2c 20 63 6f 6e 73 74 4d  Function, constM
218ce 61 73 6b 2c 20 72 31 2c 20 74 61 72 67 65 74 2c  ask, r1, target,
218cf 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
218d0 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29           (char*)
218d1 70 44 65 66 2c 20 50 34 5f 46 55 4e 43 44 45 46  pDef, P4_FUNCDEF
218d2 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
218d3 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
218d4 28 75 38 29 6e 46 61 72 67 29 3b 0a 20 20 20 20  (u8)nFarg);.    
218d5 20 20 69 66 28 20 6e 46 61 72 67 20 29 7b 0a 20    if( nFarg ){. 
218d6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
218d7 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70  leaseTempRange(p
218d8 50 61 72 73 65 2c 20 72 31 2c 20 6e 46 61 72 67  Parse, r1, nFarg
218d9 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
218da 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
218db 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  eAffinityChange(
218dc 70 50 61 72 73 65 2c 20 72 31 2c 20 6e 46 61 72  pParse, r1, nFar
218dd 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  g);.      break;
218de 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
218df 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
218e0 45 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  ERY.    case TK_
218e1 45 58 49 53 54 53 3a 0a 20 20 20 20 63 61 73 65  EXISTS:.    case
218e2 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 20   TK_SELECT: {.  
218e3 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
218e4 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a 20  ==TK_EXISTS );. 
218e5 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
218e6 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b 0a  p==TK_SELECT );.
218e7 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64        sqlite3Cod
218e8 65 53 75 62 73 65 6c 65 63 74 28 70 50 61 72 73  eSubselect(pPars
218e9 65 2c 20 70 45 78 70 72 2c 20 30 2c 20 30 29 3b  e, pExpr, 0, 0);
218ea 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70  .      inReg = p
218eb 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20  Expr->iColumn;. 
218ec 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
218ed 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e  }.    case TK_IN
218ee 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 4e  : {.      int rN
218ef 6f 74 46 6f 75 6e 64 20 3d 20 30 3b 0a 20 20 20  otFound = 0;.   
218f0 20 20 20 69 6e 74 20 72 4d 61 79 48 61 76 65 4e     int rMayHaveN
218f1 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  ull = 0;.      i
218f2 6e 74 20 6a 32 2c 20 6a 33 2c 20 6a 34 2c 20 6a  nt j2, j3, j4, j
218f3 35 3b 0a 20 20 20 20 20 20 63 68 61 72 20 61 66  5;.      char af
218f4 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 69 6e  finity;.      in
218f5 74 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 20 20  t eType;..      
218f6 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28  VdbeNoopComment(
218f7 28 76 2c 20 22 62 65 67 69 6e 20 49 4e 20 65 78  (v, "begin IN ex
218f8 70 72 20 72 25 64 22 2c 20 74 61 72 67 65 74 29  pr r%d", target)
218f9 29 3b 0a 20 20 20 20 20 20 65 54 79 70 65 20 3d  );.      eType =
218fa 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e   sqlite3FindInIn
218fb 64 65 78 28 70 50 61 72 73 65 2c 20 70 45 78 70  dex(pParse, pExp
218fc 72 2c 20 26 72 4d 61 79 48 61 76 65 4e 75 6c 6c  r, &rMayHaveNull
218fd 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 4d 61  );.      if( rMa
218fe 79 48 61 76 65 4e 75 6c 6c 20 29 7b 0a 20 20 20  yHaveNull ){.   
218ff 20 20 20 20 20 72 4e 6f 74 46 6f 75 6e 64 20 3d       rNotFound =
21900 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
21901 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
21902 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68  /* Figure out th
21903 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 75 73  e affinity to us
21904 65 20 74 6f 20 63 72 65 61 74 65 20 61 20 6b 65  e to create a ke
21905 79 20 66 72 6f 6d 20 74 68 65 20 72 65 73 75 6c  y from the resul
21906 74 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74  ts.      ** of t
21907 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 61  he expression. a
21908 66 66 69 6e 69 74 79 53 74 72 20 73 74 6f 72 65  ffinityStr store
21909 73 20 61 20 73 74 61 74 69 63 20 73 74 72 69 6e  s a static strin
2190a 67 20 73 75 69 74 61 62 6c 65 20 66 6f 72 0a 20  g suitable for. 
2190b 20 20 20 20 20 2a 2a 20 50 34 20 6f 66 20 4f 50       ** P4 of OP
2190c 5f 4d 61 6b 65 52 65 63 6f 72 64 2e 0a 20 20 20  _MakeRecord..   
2190d 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 69     */.      affi
2190e 6e 69 74 79 20 3d 20 63 6f 6d 70 61 72 69 73 6f  nity = compariso
2190f 6e 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 29  nAffinity(pExpr)
21910 3b 0a 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64  ;...      /* Cod
21911 65 20 74 68 65 20 3c 65 78 70 72 3e 20 66 72 6f  e the <expr> fro
21912 6d 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 2e 2e  m "<expr> IN (..
21913 2e 29 22 2e 20 54 68 65 20 74 65 6d 70 6f 72 61  .)". The tempora
21914 72 79 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a  ry table.      *
21915 2a 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20  * pExpr->iTable 
21916 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 76 61 6c  contains the val
21917 75 65 73 20 74 68 61 74 20 6d 61 6b 65 20 75 70  ues that make up
21918 20 74 68 65 20 28 2e 2e 2e 29 20 73 65 74 2e 0a   the (...) set..
21919 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
2191a 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50  qlite3ExprCacheP
2191b 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20 20  ush(pParse);.   
2191c 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
2191d 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  de(pParse, pExpr
2191e 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29  ->pLeft, target)
2191f 3b 0a 20 20 20 20 20 20 6a 32 20 3d 20 73 71 6c  ;.      j2 = sql
21920 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
21921 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 74 61 72  , OP_IsNull, tar
21922 67 65 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  get);.      if( 
21923 65 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f  eType==IN_INDEX_
21924 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 20 20 20  ROWID ){.       
21925 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j3 = sqlite3Vdb
21926 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75  eAddOp1(v, OP_Mu
21927 73 74 42 65 49 6e 74 2c 20 74 61 72 67 65 74 29  stBeInt, target)
21928 3b 0a 20 20 20 20 20 20 20 20 6a 34 20 3d 20 73  ;.        j4 = s
21929 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2192a 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  (v, OP_NotExists
2192b 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c  , pExpr->iTable,
2192c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20   0, target);.   
2192d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2192e 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2192f 65 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74 29  eger, 1, target)
21930 3b 0a 20 20 20 20 20 20 20 20 6a 35 20 3d 20 73  ;.        j5 = s
21931 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30  qlite3VdbeAddOp0
21932 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20  (v, OP_Goto);.  
21933 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21934 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 33 29  eJumpHere(v, j3)
21935 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
21936 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
21937 20 6a 34 29 3b 0a 20 20 20 20 20 20 20 20 73 71   j4);.        sq
21938 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
21939 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
2193a 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
2193b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2193c 72 32 20 3d 20 72 65 67 46 72 65 65 32 20 3d 20  r2 = regFree2 = 
2193d 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
2193e 67 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 20 20  g(pParse);..    
2193f 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20      /* Create a 
21940 72 65 63 6f 72 64 20 61 6e 64 20 74 65 73 74 20  record and test 
21941 66 6f 72 20 73 65 74 20 6d 65 6d 62 65 72 73 68  for set membersh
21942 69 70 2e 20 49 66 20 74 68 65 20 73 65 74 20 63  ip. If the set c
21943 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 20 20  ontains.        
21944 2a 2a 20 74 68 65 20 76 61 6c 75 65 2c 20 74 68  ** the value, th
21945 65 6e 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 65  en jump to the e
21946 6e 64 20 6f 66 20 74 68 65 20 74 65 73 74 20 63  nd of the test c
21947 6f 64 65 2e 20 54 68 65 20 74 61 72 67 65 74 0a  ode. The target.
21948 20 20 20 20 20 20 20 20 2a 2a 20 72 65 67 69 73          ** regis
21949 74 65 72 20 73 74 69 6c 6c 20 63 6f 6e 74 61 69  ter still contai
2194a 6e 73 20 74 68 65 20 74 72 75 65 20 28 31 29 20  ns the true (1) 
2194b 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 74 6f  value written to
2194c 20 69 74 20 65 61 72 6c 69 65 72 2e 0a 20 20 20   it earlier..   
2194d 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
2194e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2194f 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  4(v, OP_MakeReco
21950 72 64 2c 20 74 61 72 67 65 74 2c 20 31 2c 20 72  rd, target, 1, r
21951 32 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 31 29  2, &affinity, 1)
21952 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
21953 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
21954 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 74 61  P_Integer, 1, ta
21955 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6a  rget);.        j
21956 35 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  5 = sqlite3VdbeA
21957 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e  ddOp3(v, OP_Foun
21958 64 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  d, pExpr->iTable
21959 2c 20 30 2c 20 72 32 29 3b 0a 0a 20 20 20 20 20  , 0, r2);..     
2195a 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 65 74     /* If the set
2195b 20 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73 74   membership test
2195c 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 74 68 65   fails, then the
2195d 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 0a   result of the .
2195e 20 20 20 20 20 20 20 20 2a 2a 20 22 78 20 49 4e          ** "x IN
2195f 20 28 2e 2e 2e 29 22 20 65 78 70 72 65 73 73 69   (...)" expressi
21960 6f 6e 20 6d 75 73 74 20 62 65 20 65 69 74 68 65  on must be eithe
21961 72 20 30 20 6f 72 20 4e 55 4c 4c 2e 20 49 66 20  r 0 or NULL. If 
21962 74 68 65 20 73 65 74 0a 20 20 20 20 20 20 20 20  the set.        
21963 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 4e  ** contains no N
21964 55 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68 65 6e  ULL values, then
21965 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 30   the result is 0
21966 2e 20 49 66 20 74 68 65 20 73 65 74 20 0a 20 20  . If the set .  
21967 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e        ** contain
21968 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 4e 55  s one or more NU
21969 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68 65 6e 20  LL values, then 
2196a 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  the result of th
2196b 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 78 70  e.        ** exp
2196c 72 65 73 73 69 6f 6e 20 69 73 20 61 6c 73 6f 20  ression is also 
2196d 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 20 20 2a 2f  NULL..        */
2196e 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 4e 6f  .        if( rNo
2196f 74 46 6f 75 6e 64 3d 3d 30 20 29 7b 0a 20 20 20  tFound==0 ){.   
21970 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62         /* This b
21971 72 61 6e 63 68 20 72 75 6e 73 20 69 66 20 69 74  ranch runs if it
21972 20 69 73 20 6b 6e 6f 77 6e 20 61 74 20 63 6f 6d   is known at com
21973 70 69 6c 65 20 74 69 6d 65 20 28 6e 6f 77 29 20  pile time (now) 
21974 74 68 61 74 20 0a 20 20 20 20 20 20 20 20 20 20  that .          
21975 2a 2a 20 74 68 65 20 73 65 74 20 63 6f 6e 74 61  ** the set conta
21976 69 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 61 6c 75  ins no NULL valu
21977 65 73 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73  es. This happens
21978 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a 20   as the result. 
21979 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 61           ** of a
2197a 20 22 4e 4f 54 20 4e 55 4c 4c 22 20 63 6f 6e 73   "NOT NULL" cons
2197b 74 72 61 69 6e 74 20 69 6e 20 74 68 65 20 64 61  traint in the da
2197c 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 4e  tabase schema. N
2197d 6f 20 6e 65 65 64 0a 20 20 20 20 20 20 20 20 20  o need.         
2197e 20 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65 20   ** to test the 
2197f 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 61  data structure a
21980 74 20 72 75 6e 74 69 6d 65 20 69 6e 20 74 68 69  t runtime in thi
21981 73 20 63 61 73 65 2e 0a 20 20 20 20 20 20 20 20  s case..        
21982 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73    */.          s
21983 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
21984 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
21985 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  0, target);.    
21986 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
21987 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f       /* This blo
21988 63 6b 20 70 6f 70 75 6c 61 74 65 73 20 74 68 65  ck populates the
21989 20 72 4e 6f 74 46 6f 75 6e 64 20 72 65 67 69 73   rNotFound regis
2198a 74 65 72 20 77 69 74 68 20 65 69 74 68 65 72 20  ter with either 
2198b 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 20 20 2a  NULL.          *
2198c 2a 20 6f 72 20 30 20 28 61 6e 20 69 6e 74 65 67  * or 0 (an integ
2198d 65 72 20 76 61 6c 75 65 29 2e 20 49 66 20 74 68  er value). If th
2198e 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65  e data structure
2198f 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 0a 20 20   contains one.  
21990 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d 6f          ** or mo
21991 72 65 20 4e 55 4c 4c 73 2c 20 74 68 65 6e 20 73  re NULLs, then s
21992 65 74 20 72 4e 6f 74 46 6f 75 6e 64 20 74 6f 20  et rNotFound to 
21993 4e 55 4c 4c 2e 20 4f 74 68 65 72 77 69 73 65 2c  NULL. Otherwise,
21994 20 73 65 74 20 69 74 0a 20 20 20 20 20 20 20 20   set it.        
21995 20 20 2a 2a 20 74 6f 20 30 2e 20 49 66 20 72 65    ** to 0. If re
21996 67 69 73 74 65 72 20 72 4d 61 79 48 61 76 65 4e  gister rMayHaveN
21997 75 6c 6c 20 69 73 20 61 6c 72 65 61 64 79 20 73  ull is already s
21998 65 74 20 74 6f 20 73 6f 6d 65 20 76 61 6c 75 65  et to some value
21999 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 74  .          ** ot
2199a 68 65 72 20 74 68 61 6e 20 4e 55 4c 4c 2c 20 74  her than NULL, t
2199b 68 65 6e 20 74 68 65 20 74 65 73 74 20 68 61 73  hen the test has
2199c 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 72 75   already been ru
2199d 6e 20 61 6e 64 20 0a 20 20 20 20 20 20 20 20 20  n and .         
2199e 20 2a 2a 20 72 4e 6f 74 46 6f 75 6e 64 20 69 73   ** rNotFound is
2199f 20 61 6c 72 65 61 64 79 20 70 6f 70 75 6c 61 74   already populat
219a0 65 64 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  ed..          */
219a1 0a 20 20 20 20 20 20 20 20 20 20 73 74 61 74 69  .          stati
219a2 63 20 63 6f 6e 73 74 20 63 68 61 72 20 6e 75 6c  c const char nul
219a3 6c 52 65 63 6f 72 64 5b 5d 20 3d 20 7b 20 30 78  lRecord[] = { 0x
219a4 30 32 2c 20 30 78 30 30 20 7d 3b 0a 20 20 20 20  02, 0x00 };.    
219a5 20 20 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74        j3 = sqlit
219a6 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
219a7 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 4d 61 79  OP_NotNull, rMay
219a8 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  HaveNull);.     
219a9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
219aa 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
219ab 6c 2c 20 30 2c 20 72 4e 6f 74 46 6f 75 6e 64 29  l, 0, rNotFound)
219ac 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
219ad 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
219ae 20 4f 50 5f 42 6c 6f 62 2c 20 32 2c 20 72 4d 61   OP_Blob, 2, rMa
219af 79 48 61 76 65 4e 75 6c 6c 2c 20 30 2c 20 0a 20  yHaveNull, 0, . 
219b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
219b1 20 20 20 20 20 20 20 20 20 20 20 20 6e 75 6c 6c              null
219b2 52 65 63 6f 72 64 2c 20 50 34 5f 53 54 41 54 49  Record, P4_STATI
219b3 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 34  C);.          j4
219b4 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
219b5 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e 64  dOp3(v, OP_Found
219b6 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c  , pExpr->iTable,
219b7 20 30 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c   0, rMayHaveNull
219b8 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
219b9 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
219ba 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
219bb 20 72 4e 6f 74 46 6f 75 6e 64 29 3b 0a 20 20 20   rNotFound);.   
219bc 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
219bd 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 34  beJumpHere(v, j4
219be 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
219bf 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
219c0 28 76 2c 20 6a 33 29 3b 0a 0a 20 20 20 20 20 20  (v, j3);..      
219c1 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20      /* Copy the 
219c2 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65  value of registe
219c3 72 20 72 4e 6f 74 46 6f 75 6e 64 20 28 77 68 69  r rNotFound (whi
219c4 63 68 20 69 73 20 65 69 74 68 65 72 20 4e 55 4c  ch is either NUL
219c5 4c 20 6f 72 20 30 29 0a 20 20 20 20 20 20 20 20  L or 0).        
219c6 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 74 61    ** into the ta
219c7 72 67 65 74 20 72 65 67 69 73 74 65 72 2e 20 54  rget register. T
219c8 68 69 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20  his will be the 
219c9 72 65 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 20  result of the.  
219ca 20 20 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65          ** expre
219cb 73 73 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 20  ssion..         
219cc 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 71   */.          sq
219cd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
219ce 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 4e 6f 74  v, OP_Copy, rNot
219cf 46 6f 75 6e 64 2c 20 74 61 72 67 65 74 29 3b 0a  Found, target);.
219d0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
219d1 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  }.      sqlite3V
219d2 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
219d3 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  2);.      sqlite
219d4 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
219d5 20 6a 35 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   j5);.      sqli
219d6 74 65 33 45 78 70 72 43 61 63 68 65 50 6f 70 28  te3ExprCachePop(
219d7 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20 20 20  pParse, 1);.    
219d8 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
219d9 2c 20 22 65 6e 64 20 49 4e 20 65 78 70 72 20 72  , "end IN expr r
219da 25 64 22 2c 20 74 61 72 67 65 74 29 29 3b 0a 20  %d", target));. 
219db 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
219dc 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f 2a 0a  }.#endif.    /*.
219dd 20 20 20 20 2a 2a 20 20 20 20 78 20 42 45 54 57      **    x BETW
219de 45 45 4e 20 79 20 41 4e 44 20 7a 0a 20 20 20 20  EEN y AND z.    
219df 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 69  **.    ** This i
219e0 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 0a  s equivalent to.
219e1 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
219e2 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20   x>=y AND x<=z. 
219e3 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 58 20 69     **.    ** X i
219e4 73 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70  s stored in pExp
219e5 72 2d 3e 70 4c 65 66 74 2e 0a 20 20 20 20 2a 2a  r->pLeft..    **
219e6 20 59 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20   Y is stored in 
219e7 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b  pExpr->pList->a[
219e8 30 5d 2e 70 45 78 70 72 2e 0a 20 20 20 20 2a 2a  0].pExpr..    **
219e9 20 5a 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20   Z is stored in 
219ea 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b  pExpr->pList->a[
219eb 31 5d 2e 70 45 78 70 72 2e 0a 20 20 20 20 2a 2f  1].pExpr..    */
219ec 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 45 54  .    case TK_BET
219ed 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78  WEEN: {.      Ex
219ee 70 72 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70  pr *pLeft = pExp
219ef 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20  r->pLeft;.      
219f0 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
219f1 69 74 65 6d 20 2a 70 4c 49 74 65 6d 20 3d 20 70  item *pLItem = p
219f2 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61  Expr->x.pList->a
219f3 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 52  ;.      Expr *pR
219f4 69 67 68 74 20 3d 20 70 4c 49 74 65 6d 2d 3e 70  ight = pLItem->p
219f5 45 78 70 72 3b 0a 0a 20 20 20 20 20 20 63 6f 64  Expr;..      cod
219f6 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73  eCompareOperands
219f7 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20  (pParse, pLeft, 
219f8 26 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c 0a  &r1, &regFree1,.
219f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
219fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
219fb 20 20 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26    pRight, &r2, &
219fc 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20  regFree2);.     
219fd 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72   testcase( regFr
219fe 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ee1==0 );.      
219ff 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
21a00 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 72  e2==0 );.      r
21a01 33 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  3 = sqlite3GetTe
21a02 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
21a03 20 20 20 20 20 72 34 20 3d 20 73 71 6c 69 74 65       r4 = sqlite
21a04 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
21a05 73 65 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43  se);.      codeC
21a06 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70  ompare(pParse, p
21a07 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 4f 50  Left, pRight, OP
21a08 5f 47 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  _Ge,.           
21a09 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 72         r1, r2, r
21a0a 33 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50  3, SQLITE_STOREP
21a0b 32 29 3b 0a 20 20 20 20 20 20 70 4c 49 74 65 6d  2);.      pLItem
21a0c 2b 2b 3b 0a 20 20 20 20 20 20 70 52 69 67 68 74  ++;.      pRight
21a0d 20 3d 20 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72   = pLItem->pExpr
21a0e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
21a0f 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
21a10 61 72 73 65 2c 20 72 65 67 46 72 65 65 32 29 3b  arse, regFree2);
21a11 0a 20 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69  .      r2 = sqli
21a12 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
21a13 70 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20  pParse, pRight, 
21a14 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20  &regFree2);.    
21a15 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46    testcase( regF
21a16 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ree2==0 );.     
21a17 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61   codeCompare(pPa
21a18 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69 67  rse, pLeft, pRig
21a19 68 74 2c 20 4f 50 5f 4c 65 2c 20 72 31 2c 20 72  ht, OP_Le, r1, r
21a1a 32 2c 20 72 34 2c 20 53 51 4c 49 54 45 5f 53 54  2, r4, SQLITE_ST
21a1b 4f 52 45 50 32 29 3b 0a 20 20 20 20 20 20 73 71  OREP2);.      sq
21a1c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
21a1d 76 2c 20 4f 50 5f 41 6e 64 2c 20 72 33 2c 20 72  v, OP_And, r3, r
21a1e 34 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  4, target);.    
21a1f 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
21a20 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
21a21 72 33 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  r3);.      sqlit
21a22 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
21a23 28 70 50 61 72 73 65 2c 20 72 34 29 3b 0a 20 20  (pParse, r4);.  
21a24 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
21a25 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 50 4c  .    case TK_UPL
21a26 55 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 65  US: {.      inRe
21a27 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  g = sqlite3ExprC
21a28 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65  odeTarget(pParse
21a29 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
21a2a 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62  target);.      b
21a2b 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
21a2c 20 63 61 73 65 20 54 4b 5f 54 52 49 47 47 45 52   case TK_TRIGGER
21a2d 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  : {.      /* If 
21a2e 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20 54 4b  the opcode is TK
21a2f 5f 54 52 49 47 47 45 52 2c 20 74 68 65 6e 20 74  _TRIGGER, then t
21a30 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
21a31 20 61 20 72 65 66 65 72 65 6e 63 65 0a 20 20 20   a reference.   
21a32 20 20 20 2a 2a 20 74 6f 20 61 20 63 6f 6c 75 6d     ** to a colum
21a33 6e 20 69 6e 20 74 68 65 20 6e 65 77 2e 2a 20 6f  n in the new.* o
21a34 72 20 6f 6c 64 2e 2a 20 70 73 65 75 64 6f 2d 74  r old.* pseudo-t
21a35 61 62 6c 65 73 20 61 76 61 69 6c 61 62 6c 65 20  ables available 
21a36 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 72 69 67  to.      ** trig
21a37 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 20 49 6e  ger programs. In
21a38 20 74 68 69 73 20 63 61 73 65 20 45 78 70 72 2e   this case Expr.
21a39 69 54 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f  iTable is set to
21a3a 20 31 20 66 6f 72 20 74 68 65 0a 20 20 20 20 20   1 for the.     
21a3b 20 2a 2a 20 6e 65 77 2e 2a 20 70 73 65 75 64 6f   ** new.* pseudo
21a3c 2d 74 61 62 6c 65 2c 20 6f 72 20 30 20 66 6f 72  -table, or 0 for
21a3d 20 74 68 65 20 6f 6c 64 2e 2a 20 70 73 65 75 64   the old.* pseud
21a3e 6f 2d 74 61 62 6c 65 2e 20 45 78 70 72 2e 69 43  o-table. Expr.iC
21a3f 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 2a 2a 20 69  olumn.      ** i
21a40 73 20 73 65 74 20 74 6f 20 74 68 65 20 63 6f 6c  s set to the col
21a41 75 6d 6e 20 6f 66 20 74 68 65 20 70 73 65 75 64  umn of the pseud
21a42 6f 2d 74 61 62 6c 65 20 74 6f 20 72 65 61 64 2c  o-table to read,
21a43 20 6f 72 20 74 6f 20 2d 31 20 74 6f 0a 20 20 20   or to -1 to.   
21a44 20 20 20 2a 2a 20 72 65 61 64 20 74 68 65 20 72     ** read the r
21a45 6f 77 69 64 20 66 69 65 6c 64 2e 0a 20 20 20 20  owid field..    
21a46 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68    **.      ** Th
21a47 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
21a48 69 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e  implemented usin
21a49 67 20 61 6e 20 4f 50 5f 50 61 72 61 6d 20 6f 70  g an OP_Param op
21a4a 63 6f 64 65 2e 20 54 68 65 20 70 31 0a 20 20 20  code. The p1.   
21a4b 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 20     ** parameter 
21a4c 69 73 20 73 65 74 20 74 6f 20 30 20 66 6f 72 20  is set to 0 for 
21a4d 61 6e 20 6f 6c 64 2e 72 6f 77 69 64 20 72 65 66  an old.rowid ref
21a4e 65 72 65 6e 63 65 2c 20 6f 72 20 74 6f 20 28 69  erence, or to (i
21a4f 2b 31 29 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  +1).      ** to 
21a50 72 65 66 65 72 65 6e 63 65 20 61 6e 6f 74 68 65  reference anothe
21a51 72 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  r column of the 
21a52 6f 6c 64 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62  old.* pseudo-tab
21a53 6c 65 2c 20 77 68 65 72 65 20 0a 20 20 20 20 20  le, where .     
21a54 20 2a 2a 20 69 20 69 73 20 74 68 65 20 69 6e 64   ** i is the ind
21a55 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  ex of the column
21a56 2e 20 46 6f 72 20 61 20 6e 65 77 2e 72 6f 77 69  . For a new.rowi
21a57 64 20 72 65 66 65 72 65 6e 63 65 2c 20 70 31 20  d reference, p1 
21a58 69 73 0a 20 20 20 20 20 20 2a 2a 20 73 65 74 20  is.      ** set 
21a59 74 6f 20 28 6e 2b 31 29 2c 20 77 68 65 72 65 20  to (n+1), where 
21a5a 6e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  n is the number 
21a5b 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 65 61  of columns in ea
21a5c 63 68 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e  ch pseudo-table.
21a5d 0a 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 61 20  .      ** For a 
21a5e 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 6e 79  reference to any
21a5f 20 6f 74 68 65 72 20 63 6f 6c 75 6d 6e 20 69 6e   other column in
21a60 20 74 68 65 20 6e 65 77 2e 2a 20 70 73 65 75 64   the new.* pseud
21a61 6f 2d 74 61 62 6c 65 2c 20 70 31 0a 20 20 20 20  o-table, p1.    
21a62 20 20 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 28    ** is set to (
21a63 6e 2b 32 2b 69 29 2c 20 77 68 65 72 65 20 6e 20  n+2+i), where n 
21a64 61 6e 64 20 69 20 61 72 65 20 61 73 20 64 65 66  and i are as def
21a65 69 6e 65 64 20 70 72 65 76 69 6f 75 73 6c 79 2e  ined previously.
21a66 20 46 6f 72 0a 20 20 20 20 20 20 2a 2a 20 65 78   For.      ** ex
21a67 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 74 61  ample, if the ta
21a68 62 6c 65 20 6f 6e 20 77 68 69 63 68 20 74 72 69  ble on which tri
21a69 67 67 65 72 73 20 61 72 65 20 62 65 69 6e 67 20  ggers are being 
21a6a 66 69 72 65 64 20 69 73 0a 20 20 20 20 20 20 2a  fired is.      *
21a6b 2a 20 64 65 63 6c 61 72 65 64 20 61 73 3a 0a 20  * declared as:. 
21a6c 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
21a6d 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
21a6e 74 31 28 61 2c 20 62 29 3b 0a 20 20 20 20 20 20  t1(a, b);.      
21a6f 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 6e  **.      ** Then
21a70 20 70 31 20 69 73 20 69 6e 74 65 72 70 72 65 74   p1 is interpret
21a71 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20  ed as follows:. 
21a72 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
21a73 20 20 20 70 31 3d 3d 30 20 20 20 2d 3e 20 20 20     p1==0   ->   
21a74 20 6f 6c 64 2e 72 6f 77 69 64 20 20 20 20 20 70   old.rowid     p
21a75 31 3d 3d 33 20 20 20 2d 3e 20 20 20 20 6e 65 77  1==3   ->    new
21a76 2e 72 6f 77 69 64 0a 20 20 20 20 20 20 2a 2a 20  .rowid.      ** 
21a77 20 20 70 31 3d 3d 31 20 20 20 2d 3e 20 20 20 20    p1==1   ->    
21a78 6f 6c 64 2e 61 20 20 20 20 20 20 20 20 20 70 31  old.a         p1
21a79 3d 3d 34 20 20 20 2d 3e 20 20 20 20 6e 65 77 2e  ==4   ->    new.
21a7a 61 0a 20 20 20 20 20 20 2a 2a 20 20 20 70 31 3d  a.      **   p1=
21a7b 3d 32 20 20 20 2d 3e 20 20 20 20 6f 6c 64 2e 62  =2   ->    old.b
21a7c 20 20 20 20 20 20 20 20 20 70 31 3d 3d 35 20 20           p1==5  
21a7d 20 2d 3e 20 20 20 20 6e 65 77 2e 62 20 20 20 20   ->    new.b    
21a7e 20 20 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20     .      */.   
21a7f 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
21a80 20 70 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20 20   pExpr->pTab;.  
21a81 20 20 20 20 69 6e 74 20 70 31 20 3d 20 70 45 78      int p1 = pEx
21a82 70 72 2d 3e 69 54 61 62 6c 65 20 2a 20 28 70 54  pr->iTable * (pT
21a83 61 62 2d 3e 6e 43 6f 6c 2b 31 29 20 2b 20 31 20  ab->nCol+1) + 1 
21a84 2b 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  + pExpr->iColumn
21a85 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
21a86 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d   pExpr->iTable==
21a87 30 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61 62  0 || pExpr->iTab
21a88 6c 65 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 61  le==1 );.      a
21a89 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 69 43  ssert( pExpr->iC
21a8a 6f 6c 75 6d 6e 3e 3d 2d 31 20 26 26 20 70 45 78  olumn>=-1 && pEx
21a8b 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 70 54 61 62  pr->iColumn<pTab
21a8c 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 20 20  ->nCol );.      
21a8d 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 69 50  assert( pTab->iP
21a8e 4b 65 79 3c 30 20 7c 7c 20 70 45 78 70 72 2d 3e  Key<0 || pExpr->
21a8f 69 43 6f 6c 75 6d 6e 21 3d 70 54 61 62 2d 3e 69  iColumn!=pTab->i
21a90 50 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 61 73  PKey );.      as
21a91 73 65 72 74 28 20 70 31 3e 3d 30 20 26 26 20 70  sert( p1>=0 && p
21a92 31 3c 28 70 54 61 62 2d 3e 6e 43 6f 6c 2a 32 2b  1<(pTab->nCol*2+
21a93 32 29 20 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c  2) );..      sql
21a94 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
21a95 2c 20 4f 50 5f 50 61 72 61 6d 2c 20 70 31 2c 20  , OP_Param, p1, 
21a96 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 56  target);.      V
21a97 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
21a98 25 73 2e 25 73 20 2d 3e 20 24 25 64 22 2c 0a 20  %s.%s -> $%d",. 
21a99 20 20 20 20 20 20 20 28 70 45 78 70 72 2d 3e 69         (pExpr->i
21a9a 54 61 62 6c 65 20 3f 20 22 6e 65 77 22 20 3a 20  Table ? "new" : 
21a9b 22 6f 6c 64 22 29 2c 0a 20 20 20 20 20 20 20 20  "old"),.        
21a9c 28 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c  (pExpr->iColumn<
21a9d 30 20 3f 20 22 72 6f 77 69 64 22 20 3a 20 70 45  0 ? "rowid" : pE
21a9e 78 70 72 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b  xpr->pTab->aCol[
21a9f 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e  pExpr->iColumn].
21aa0 7a 4e 61 6d 65 29 2c 0a 20 20 20 20 20 20 20 20  zName),.        
21aa1 74 61 72 67 65 74 0a 20 20 20 20 20 20 29 29 3b  target.      ));
21aa2 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  ..      /* If th
21aa3 65 20 63 6f 6c 75 6d 6e 20 68 61 73 20 52 45 41  e column has REA
21aa4 4c 20 61 66 66 69 6e 69 74 79 2c 20 69 74 20 6d  L affinity, it m
21aa5 61 79 20 63 75 72 72 65 6e 74 6c 79 20 62 65 20  ay currently be 
21aa6 73 74 6f 72 65 64 20 61 73 20 61 6e 0a 20 20 20  stored as an.   
21aa7 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 55     ** integer. U
21aa8 73 65 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69  se OP_RealAffini
21aa9 74 79 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ty to make sure 
21aaa 69 74 20 69 73 20 72 65 61 6c 6c 79 20 72 65 61  it is really rea
21aab 6c 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  l.  */.      if(
21aac 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e   pExpr->iColumn>
21aad 3d 30 20 0a 20 20 20 20 20 20 20 26 26 20 70 54  =0 .       && pT
21aae 61 62 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 2d 3e  ab->aCol[pExpr->
21aaf 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69 6e 69 74  iColumn].affinit
21ab0 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45  y==SQLITE_AFF_RE
21ab1 41 4c 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  AL.      ){.    
21ab2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21ab3 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 61 6c  ddOp1(v, OP_Real
21ab4 41 66 66 69 6e 69 74 79 2c 20 74 61 72 67 65 74  Affinity, target
21ab5 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
21ab6 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 0a   break;.    }...
21ab7 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 46 6f      /*.    ** Fo
21ab8 72 6d 20 41 3a 0a 20 20 20 20 2a 2a 20 20 20 43  rm A:.    **   C
21ab9 41 53 45 20 78 20 57 48 45 4e 20 65 31 20 54 48  ASE x WHEN e1 TH
21aba 45 4e 20 72 31 20 57 48 45 4e 20 65 32 20 54 48  EN r1 WHEN e2 TH
21abb 45 4e 20 72 32 20 2e 2e 2e 20 57 48 45 4e 20 65  EN r2 ... WHEN e
21abc 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79  N THEN rN ELSE y
21abd 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20   END.    **.    
21abe 2a 2a 20 46 6f 72 6d 20 42 3a 0a 20 20 20 20 2a  ** Form B:.    *
21abf 2a 20 20 20 43 41 53 45 20 57 48 45 4e 20 65 31  *   CASE WHEN e1
21ac0 20 54 48 45 4e 20 72 31 20 57 48 45 4e 20 65 32   THEN r1 WHEN e2
21ac1 20 54 48 45 4e 20 72 32 20 2e 2e 2e 20 57 48 45   THEN r2 ... WHE
21ac2 4e 20 65 4e 20 54 48 45 4e 20 72 4e 20 45 4c 53  N eN THEN rN ELS
21ac3 45 20 79 20 45 4e 44 0a 20 20 20 20 2a 2a 0a 20  E y END.    **. 
21ac4 20 20 20 2a 2a 20 46 6f 72 6d 20 41 20 69 73 20     ** Form A is 
21ac5 63 61 6e 20 62 65 20 74 72 61 6e 73 66 6f 72 6d  can be transform
21ac6 65 64 20 69 6e 74 6f 20 74 68 65 20 65 71 75 69  ed into the equi
21ac7 76 61 6c 65 6e 74 20 66 6f 72 6d 20 42 20 61 73  valent form B as
21ac8 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20 2a 2a   follows:.    **
21ac9 20 20 20 43 41 53 45 20 57 48 45 4e 20 78 3d 65     CASE WHEN x=e
21aca 31 20 54 48 45 4e 20 72 31 20 57 48 45 4e 20 78  1 THEN r1 WHEN x
21acb 3d 65 32 20 54 48 45 4e 20 72 32 20 2e 2e 2e 0a  =e2 THEN r2 ....
21acc 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 57 48      **        WH
21acd 45 4e 20 78 3d 65 4e 20 54 48 45 4e 20 72 4e 20  EN x=eN THEN rN 
21ace 45 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 20 2a  ELSE y END.    *
21acf 2a 0a 20 20 20 20 2a 2a 20 58 20 28 69 66 20 69  *.    ** X (if i
21ad0 74 20 65 78 69 73 74 73 29 20 69 73 20 69 6e 20  t exists) is in 
21ad1 70 45 78 70 72 2d 3e 70 4c 65 66 74 2e 0a 20 20  pExpr->pLeft..  
21ad2 20 20 2a 2a 20 59 20 69 73 20 69 6e 20 70 45 78    ** Y is in pEx
21ad3 70 72 2d 3e 70 52 69 67 68 74 2e 20 20 54 68 65  pr->pRight.  The
21ad4 20 59 20 69 73 20 61 6c 73 6f 20 6f 70 74 69 6f   Y is also optio
21ad5 6e 61 6c 2e 20 20 49 66 20 74 68 65 72 65 20 69  nal.  If there i
21ad6 73 20 6e 6f 0a 20 20 20 20 2a 2a 20 45 4c 53 45  s no.    ** ELSE
21ad7 20 63 6c 61 75 73 65 20 61 6e 64 20 6e 6f 20 6f   clause and no o
21ad8 74 68 65 72 20 74 65 72 6d 20 6d 61 74 63 68 65  ther term matche
21ad9 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  s, then the resu
21ada 6c 74 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a  lt of the.    **
21adb 20 65 78 70 72 73 73 69 6f 6e 20 69 73 20 4e 55   exprssion is NU
21adc 4c 4c 2e 0a 20 20 20 20 2a 2a 20 45 69 20 69 73  LL..    ** Ei is
21add 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 69 73 74   in pExpr->pList
21ade 2d 3e 61 5b 69 2a 32 5d 20 61 6e 64 20 52 69 20  ->a[i*2] and Ri 
21adf 69 73 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d  is pExpr->pList-
21ae0 3e 61 5b 69 2a 32 2b 31 5d 2e 0a 20 20 20 20 2a  >a[i*2+1]..    *
21ae1 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73  *.    ** The res
21ae2 75 6c 74 20 6f 66 20 74 68 65 20 65 78 70 72 65  ult of the expre
21ae3 73 73 69 6f 6e 20 69 73 20 74 68 65 20 52 69 20  ssion is the Ri 
21ae4 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 6d 61  for the first ma
21ae5 74 63 68 69 6e 67 20 45 69 2c 0a 20 20 20 20 2a  tching Ei,.    *
21ae6 2a 20 6f 72 20 69 66 20 74 68 65 72 65 20 69 73  * or if there is
21ae7 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 45 69 2c   no matching Ei,
21ae8 20 74 68 65 20 45 4c 53 45 20 74 65 72 6d 20 59   the ELSE term Y
21ae9 2c 20 6f 72 20 69 66 20 74 68 65 72 65 20 69 73  , or if there is
21aea 0a 20 20 20 20 2a 2a 20 6e 6f 20 45 4c 53 45 20  .    ** no ELSE 
21aeb 74 65 72 6d 2c 20 4e 55 4c 4c 2e 0a 20 20 20 20  term, NULL..    
21aec 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  */.    default: 
21aed 61 73 73 65 72 74 28 20 6f 70 3d 3d 54 4b 5f 43  assert( op==TK_C
21aee 41 53 45 20 29 3b 20 7b 0a 20 20 20 20 20 20 69  ASE ); {.      i
21aef 6e 74 20 65 6e 64 4c 61 62 65 6c 3b 20 20 20 20  nt endLabel;    
21af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21af1 20 2f 2a 20 47 4f 54 4f 20 6c 61 62 65 6c 20 66   /* GOTO label f
21af2 6f 72 20 65 6e 64 20 6f 66 20 43 41 53 45 20 73  or end of CASE s
21af3 74 6d 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  tmt */.      int
21af4 20 6e 65 78 74 43 61 73 65 3b 20 20 20 20 20 20   nextCase;      
21af5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
21af6 2a 20 47 4f 54 4f 20 6c 61 62 65 6c 20 66 6f 72  * GOTO label for
21af7 20 6e 65 78 74 20 57 48 45 4e 20 63 6c 61 75 73   next WHEN claus
21af8 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e  e */.      int n
21af9 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20  Expr;           
21afa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
21afb 32 78 20 6e 75 6d 62 65 72 20 6f 66 20 57 48 45  2x number of WHE
21afc 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 20  N terms */.     
21afd 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
21afe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21aff 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
21b00 65 72 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72  er */.      Expr
21b01 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20  List *pEList;   
21b02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21b03 20 4c 69 73 74 20 6f 66 20 57 48 45 4e 20 74 65   List of WHEN te
21b04 72 6d 73 20 2a 2f 0a 20 20 20 20 20 20 73 74 72  rms */.      str
21b05 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
21b06 6d 20 2a 61 4c 69 73 74 65 6c 65 6d 3b 20 20 2f  m *aListelem;  /
21b07 2a 20 41 72 72 61 79 20 6f 66 20 57 48 45 4e 20  * Array of WHEN 
21b08 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 20 20 45  terms */.      E
21b09 78 70 72 20 6f 70 43 6f 6d 70 61 72 65 3b 20 20  xpr opCompare;  
21b0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21b0b 20 2f 2a 20 54 68 65 20 58 3d 3d 45 69 20 65 78   /* The X==Ei ex
21b0c 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20  pression */.    
21b0d 20 20 45 78 70 72 20 63 61 63 68 65 58 3b 20 20    Expr cacheX;  
21b0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21b0f 20 20 20 20 2f 2a 20 43 61 63 68 65 64 20 65 78      /* Cached ex
21b10 70 72 65 73 73 69 6f 6e 20 58 20 2a 2f 0a 20 20  pression X */.  
21b11 20 20 20 20 45 78 70 72 20 2a 70 58 3b 20 20 20      Expr *pX;   
21b12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21b13 20 20 20 20 20 20 2f 2a 20 54 68 65 20 58 20 65        /* The X e
21b14 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20  xpression */.   
21b15 20 20 20 45 78 70 72 20 2a 70 54 65 73 74 20 3d     Expr *pTest =
21b16 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
21b17 20 20 20 20 20 2f 2a 20 58 3d 3d 45 69 20 28 66       /* X==Ei (f
21b18 6f 72 6d 20 41 29 20 6f 72 20 6a 75 73 74 20 45  orm A) or just E
21b19 69 20 28 66 6f 72 6d 20 42 29 20 2a 2f 0a 20 20  i (form B) */.  
21b1a 20 20 20 20 56 56 41 5f 4f 4e 4c 59 28 20 69 6e      VVA_ONLY( in
21b1b 74 20 69 43 61 63 68 65 4c 65 76 65 6c 20 3d 20  t iCacheLevel = 
21b1c 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65  pParse->iCacheLe
21b1d 76 65 6c 3b 20 29 0a 0a 20 20 20 20 20 20 61 73  vel; )..      as
21b1e 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72  sert( !ExprHasPr
21b1f 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
21b20 5f 78 49 73 53 65 6c 65 63 74 29 20 26 26 20 70  _xIsSelect) && p
21b21 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 29 3b  Expr->x.pList );
21b22 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 28 70  .      assert((p
21b23 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e  Expr->x.pList->n
21b24 45 78 70 72 20 25 20 32 29 20 3d 3d 20 30 29 3b  Expr % 2) == 0);
21b25 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70 45  .      assert(pE
21b26 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45  xpr->x.pList->nE
21b27 78 70 72 20 3e 20 30 29 3b 0a 20 20 20 20 20 20  xpr > 0);.      
21b28 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e  pEList = pExpr->
21b29 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 61  x.pList;.      a
21b2a 4c 69 73 74 65 6c 65 6d 20 3d 20 70 45 4c 69 73  Listelem = pELis
21b2b 74 2d 3e 61 3b 0a 20 20 20 20 20 20 6e 45 78 70  t->a;.      nExp
21b2c 72 20 3d 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70  r = pEList->nExp
21b2d 72 3b 0a 20 20 20 20 20 20 65 6e 64 4c 61 62 65  r;.      endLabe
21b2e 6c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  l = sqlite3VdbeM
21b2f 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
21b30 20 20 20 69 66 28 20 28 70 58 20 3d 20 70 45 78     if( (pX = pEx
21b31 70 72 2d 3e 70 4c 65 66 74 29 21 3d 30 20 29 7b  pr->pLeft)!=0 ){
21b32 0a 20 20 20 20 20 20 20 20 63 61 63 68 65 58 20  .        cacheX 
21b33 3d 20 2a 70 58 3b 0a 20 20 20 20 20 20 20 20 74  = *pX;.        t
21b34 65 73 74 63 61 73 65 28 20 70 58 2d 3e 6f 70 3d  estcase( pX->op=
21b35 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20  =TK_COLUMN );.  
21b36 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21b37 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53  pX->op==TK_REGIS
21b38 54 45 52 20 29 3b 0a 20 20 20 20 20 20 20 20 63  TER );.        c
21b39 61 63 68 65 58 2e 69 54 61 62 6c 65 20 3d 20 73  acheX.iTable = s
21b3a 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65  qlite3ExprCodeTe
21b3b 6d 70 28 70 50 61 72 73 65 2c 20 70 58 2c 20 26  mp(pParse, pX, &
21b3c 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20  regFree1);.     
21b3d 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
21b3e 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20  Free1==0 );.    
21b3f 20 20 20 20 63 61 63 68 65 58 2e 6f 70 20 3d 20      cacheX.op = 
21b40 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20  TK_REGISTER;.   
21b41 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 6f       opCompare.o
21b42 70 20 3d 20 54 4b 5f 45 51 3b 0a 20 20 20 20 20  p = TK_EQ;.     
21b43 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70 4c 65     opCompare.pLe
21b44 66 74 20 3d 20 26 63 61 63 68 65 58 3b 0a 20 20  ft = &cacheX;.  
21b45 20 20 20 20 20 20 70 54 65 73 74 20 3d 20 26 6f        pTest = &o
21b46 70 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20 20 20  pCompare;.      
21b47 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  }.      for(i=0;
21b48 20 69 3c 6e 45 78 70 72 3b 20 69 3d 69 2b 32 29   i<nExpr; i=i+2)
21b49 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
21b4a 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70  3ExprCachePush(p
21b4b 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20  Parse);.        
21b4c 69 66 28 20 70 58 20 29 7b 0a 20 20 20 20 20 20  if( pX ){.      
21b4d 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65 73      assert( pTes
21b4e 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  t!=0 );.        
21b4f 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70 52 69 67    opCompare.pRig
21b50 68 74 20 3d 20 61 4c 69 73 74 65 6c 65 6d 5b 69  ht = aListelem[i
21b51 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20  ].pExpr;.       
21b52 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
21b53 20 20 70 54 65 73 74 20 3d 20 61 4c 69 73 74 65    pTest = aListe
21b54 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  lem[i].pExpr;.  
21b55 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
21b56 6e 65 78 74 43 61 73 65 20 3d 20 73 71 6c 69 74  nextCase = sqlit
21b57 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
21b58 76 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74  v);.        test
21b59 63 61 73 65 28 20 70 54 65 73 74 2d 3e 6f 70 3d  case( pTest->op=
21b5a 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20  =TK_COLUMN );.  
21b5b 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
21b5c 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c  rIfFalse(pParse,
21b5d 20 70 54 65 73 74 2c 20 6e 65 78 74 43 61 73 65   pTest, nextCase
21b5e 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  , SQLITE_JUMPIFN
21b5f 55 4c 4c 29 3b 0a 20 20 20 20 20 20 20 20 74 65  ULL);.        te
21b60 73 74 63 61 73 65 28 20 61 4c 69 73 74 65 6c 65  stcase( aListele
21b61 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70  m[i+1].pExpr->op
21b62 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20  ==TK_COLUMN );. 
21b63 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
21b64 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e   aListelem[i+1].
21b65 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 52 45  pExpr->op==TK_RE
21b66 47 49 53 54 45 52 20 29 3b 0a 20 20 20 20 20 20  GISTER );.      
21b67 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
21b68 65 28 70 50 61 72 73 65 2c 20 61 4c 69 73 74 65  e(pParse, aListe
21b69 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 2c 20  lem[i+1].pExpr, 
21b6a 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20  target);.       
21b6b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21b6c 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
21b6d 2c 20 65 6e 64 4c 61 62 65 6c 29 3b 0a 20 20 20  , endLabel);.   
21b6e 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
21b6f 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c  CachePop(pParse,
21b70 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   1);.        sql
21b71 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
21b72 61 62 65 6c 28 76 2c 20 6e 65 78 74 43 61 73 65  abel(v, nextCase
21b73 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
21b74 20 69 66 28 20 70 45 78 70 72 2d 3e 70 52 69 67   if( pExpr->pRig
21b75 68 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ht ){.        sq
21b76 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75  lite3ExprCachePu
21b77 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  sh(pParse);.    
21b78 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
21b79 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  ode(pParse, pExp
21b7a 72 2d 3e 70 52 69 67 68 74 2c 20 74 61 72 67 65  r->pRight, targe
21b7b 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  t);.        sqli
21b7c 74 65 33 45 78 70 72 43 61 63 68 65 50 6f 70 28  te3ExprCachePop(
21b7d 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20 20 20  pParse, 1);.    
21b7e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
21b7f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21b80 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  p2(v, OP_Null, 0
21b81 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
21b82 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
21b83 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
21b84 64 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72  d || pParse->nEr
21b85 72 3e 30 20 0a 20 20 20 20 20 20 20 20 20 20 20  r>0 .           
21b86 7c 7c 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68  || pParse->iCach
21b87 65 4c 65 76 65 6c 3d 3d 69 43 61 63 68 65 4c 65  eLevel==iCacheLe
21b88 76 65 6c 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  vel );.      sql
21b89 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
21b8a 61 62 65 6c 28 76 2c 20 65 6e 64 4c 61 62 65 6c  abel(v, endLabel
21b8b 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
21b8c 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
21b8d 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
21b8e 52 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 41  R.    case TK_RA
21b8f 49 53 45 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  ISE: {.      ass
21b90 65 72 74 28 20 70 45 78 70 72 2d 3e 61 66 66 69  ert( pExpr->affi
21b91 6e 69 74 79 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63  nity==OE_Rollbac
21b92 6b 20 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c  k .           ||
21b93 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79   pExpr->affinity
21b94 3d 3d 4f 45 5f 41 62 6f 72 74 0a 20 20 20 20 20  ==OE_Abort.     
21b95 20 20 20 20 20 20 7c 7c 20 70 45 78 70 72 2d 3e        || pExpr->
21b96 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 46 61 69  affinity==OE_Fai
21b97 6c 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  l.           || 
21b98 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d  pExpr->affinity=
21b99 3d 4f 45 5f 49 67 6e 6f 72 65 0a 20 20 20 20 20  =OE_Ignore.     
21b9a 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70   );.      if( !p
21b9b 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54  Parse->pTriggerT
21b9c 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ab ){.        sq
21b9d 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
21b9e 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20  arse,.          
21b9f 20 20 20 20 20 20 20 20 20 20 20 20 20 22 52 41               "RA
21ba0 49 53 45 28 29 20 6d 61 79 20 6f 6e 6c 79 20 62  ISE() may only b
21ba1 65 20 75 73 65 64 20 77 69 74 68 69 6e 20 61 20  e used within a 
21ba2 74 72 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 22  trigger-program"
21ba3 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
21ba4 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
21ba5 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 61 66     if( pExpr->af
21ba6 66 69 6e 69 74 79 3d 3d 4f 45 5f 41 62 6f 72 74  finity==OE_Abort
21ba7 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
21ba8 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61 72  te3MayAbort(pPar
21ba9 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  se);.      }.   
21baa 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
21bab 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
21bac 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20  r, EP_IntValue) 
21bad 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78  );.      if( pEx
21bae 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45  pr->affinity==OE
21baf 5f 49 67 6e 6f 72 65 20 29 7b 0a 20 20 20 20 20  _Ignore ){.     
21bb0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21bb1 64 4f 70 34 28 0a 20 20 20 20 20 20 20 20 20 20  dOp4(.          
21bb2 20 20 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51    v, OP_Halt, SQ
21bb3 4c 49 54 45 5f 4f 4b 2c 20 4f 45 5f 49 67 6e 6f  LITE_OK, OE_Igno
21bb4 72 65 2c 20 30 2c 20 70 45 78 70 72 2d 3e 75 2e  re, 0, pExpr->u.
21bb5 7a 54 6f 6b 65 6e 2c 30 29 3b 0a 20 20 20 20 20  zToken,0);.     
21bb6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
21bb7 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74  sqlite3HaltConst
21bb8 72 61 69 6e 74 28 70 50 61 72 73 65 2c 20 70 45  raint(pParse, pE
21bb9 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 2c 20 70  xpr->affinity, p
21bba 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20  Expr->u.zToken, 
21bbb 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  0);.      }..   
21bbc 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
21bbd 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 73 71 6c  #endif.  }.  sql
21bbe 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
21bbf 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 72  eg(pParse, regFr
21bc0 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52  ee1);.  sqlite3R
21bc1 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
21bc2 61 72 73 65 2c 20 72 65 67 46 72 65 65 32 29 3b  arse, regFree2);
21bc3 0a 20 20 72 65 74 75 72 6e 20 69 6e 52 65 67 3b  .  return inReg;
21bc4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
21bc5 74 65 20 63 6f 64 65 20 74 6f 20 65 76 61 6c 75  te code to evalu
21bc6 61 74 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f  ate an expressio
21bc7 6e 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20  n and store the 
21bc8 72 65 73 75 6c 74 73 0a 2a 2a 20 69 6e 74 6f 20  results.** into 
21bc9 61 20 72 65 67 69 73 74 65 72 2e 20 20 52 65 74  a register.  Ret
21bca 75 72 6e 20 74 68 65 20 72 65 67 69 73 74 65 72  urn the register
21bcb 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20 74 68   number where th
21bcc 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 61 72 65  e results.** are
21bcd 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49   stored..**.** I
21bce 66 20 74 68 65 20 72 65 67 69 73 74 65 72 20 69  f the register i
21bcf 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 72 65  s a temporary re
21bd0 67 69 73 74 65 72 20 74 68 61 74 20 63 61 6e 20  gister that can 
21bd1 62 65 20 64 65 61 6c 6c 6f 63 61 74 65 64 2c 0a  be deallocated,.
21bd2 2a 2a 20 74 68 65 6e 20 77 72 69 74 65 20 69 74  ** then write it
21bd3 73 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70  s number into *p
21bd4 52 65 67 2e 20 20 49 66 20 74 68 65 20 72 65 73  Reg.  If the res
21bd5 75 6c 74 20 72 65 67 69 73 74 65 72 20 69 73 20  ult register is 
21bd6 6e 6f 74 0a 2a 2a 20 61 20 74 65 6d 70 6f 72 61  not.** a tempora
21bd7 72 79 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 52  ry, then set *pR
21bd8 65 67 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a 53  eg to zero..*/.S
21bd9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
21bda 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  t sqlite3ExprCod
21bdb 65 54 65 6d 70 28 50 61 72 73 65 20 2a 70 50 61  eTemp(Parse *pPa
21bdc 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72  rse, Expr *pExpr
21bdd 2c 20 69 6e 74 20 2a 70 52 65 67 29 7b 0a 20 20  , int *pReg){.  
21bde 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33  int r1 = sqlite3
21bdf 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
21be0 65 29 3b 0a 20 20 69 6e 74 20 72 32 20 3d 20 73  e);.  int r2 = s
21be1 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61  qlite3ExprCodeTa
21be2 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78  rget(pParse, pEx
21be3 70 72 2c 20 72 31 29 3b 0a 20 20 69 66 28 20 72  pr, r1);.  if( r
21be4 32 3d 3d 72 31 20 29 7b 0a 20 20 20 20 2a 70 52  2==r1 ){.    *pR
21be5 65 67 20 3d 20 72 31 3b 0a 20 20 7d 65 6c 73 65  eg = r1;.  }else
21be6 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c  {.    sqlite3Rel
21be7 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
21be8 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 2a 70 52  se, r1);.    *pR
21be9 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  eg = 0;.  }.  re
21bea 74 75 72 6e 20 72 32 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn r2;.}../*.*
21beb 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
21bec 74 68 61 74 20 77 69 6c 6c 20 65 76 61 6c 75 61  that will evalua
21bed 74 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45  te expression pE
21bee 78 70 72 20 61 6e 64 20 73 74 6f 72 65 20 74 68  xpr and store th
21bef 65 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 20  e.** results in 
21bf0 72 65 67 69 73 74 65 72 20 74 61 72 67 65 74 2e  register target.
21bf1 20 20 54 68 65 20 72 65 73 75 6c 74 73 20 61 72    The results ar
21bf2 65 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20  e guaranteed to 
21bf3 61 70 70 65 61 72 0a 2a 2a 20 69 6e 20 72 65 67  appear.** in reg
21bf4 69 73 74 65 72 20 74 61 72 67 65 74 2e 0a 2a 2f  ister target..*/
21bf5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21bf6 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
21bf7 6f 64 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ode(Parse *pPars
21bf8 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  e, Expr *pExpr, 
21bf9 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20 20 69  int target){.  i
21bfa 6e 74 20 69 6e 52 65 67 3b 0a 0a 20 20 61 73 73  nt inReg;..  ass
21bfb 65 72 74 28 20 74 61 72 67 65 74 3e 30 20 26 26  ert( target>0 &&
21bfc 20 74 61 72 67 65 74 3c 3d 70 50 61 72 73 65 2d   target<=pParse-
21bfd 3e 6e 4d 65 6d 20 29 3b 0a 20 20 69 6e 52 65 67  >nMem );.  inReg
21bfe 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
21bff 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c  deTarget(pParse,
21c00 20 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b   pExpr, target);
21c01 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
21c02 65 2d 3e 70 56 64 62 65 20 7c 7c 20 70 50 61 72  e->pVdbe || pPar
21c03 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  se->db->mallocFa
21c04 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 69 6e  iled );.  if( in
21c05 52 65 67 21 3d 74 61 72 67 65 74 20 26 26 20 70  Reg!=target && p
21c06 50 61 72 73 65 2d 3e 70 56 64 62 65 20 29 7b 0a  Parse->pVdbe ){.
21c07 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21c08 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e 70 56  ddOp2(pParse->pV
21c09 64 62 65 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 69  dbe, OP_SCopy, i
21c0a 6e 52 65 67 2c 20 74 61 72 67 65 74 29 3b 0a 20  nReg, target);. 
21c0b 20 7d 0a 20 20 72 65 74 75 72 6e 20 74 61 72 67   }.  return targ
21c0c 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  et;.}../*.** Gen
21c0d 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20  erate code that 
21c0e 65 76 61 6c 75 74 65 73 20 74 68 65 20 67 69 76  evalutes the giv
21c0f 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e  en expression an
21c10 64 20 70 75 74 73 20 74 68 65 20 72 65 73 75 6c  d puts the resul
21c11 74 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72  t.** in register
21c12 20 74 61 72 67 65 74 2e 0a 2a 2a 0a 2a 2a 20 41   target..**.** A
21c13 6c 73 6f 20 6d 61 6b 65 20 61 20 63 6f 70 79 20  lso make a copy 
21c14 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
21c15 6e 20 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 61  n results into a
21c16 6e 6f 74 68 65 72 20 22 63 61 63 68 65 22 20 72  nother "cache" r
21c17 65 67 69 73 74 65 72 0a 2a 2a 20 61 6e 64 20 6d  egister.** and m
21c18 6f 64 69 66 79 20 74 68 65 20 65 78 70 72 65 73  odify the expres
21c19 73 69 6f 6e 20 73 6f 20 74 68 61 74 20 74 68 65  sion so that the
21c1a 20 6e 65 78 74 20 74 69 6d 65 20 69 74 20 69 73   next time it is
21c1b 20 65 76 61 6c 75 61 74 65 64 2c 0a 2a 2a 20 74   evaluated,.** t
21c1c 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 63  he result is a c
21c1d 6f 70 79 20 6f 66 20 74 68 65 20 63 61 63 68 65  opy of the cache
21c1e 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 0a 2a 2a   register..**.**
21c1f 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
21c20 20 75 73 65 64 20 66 6f 72 20 65 78 70 72 65 73   used for expres
21c21 73 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20 75  sions that are u
21c22 73 65 64 20 6d 75 6c 74 69 70 6c 65 20 0a 2a 2a  sed multiple .**
21c23 20 74 69 6d 65 73 2e 20 20 54 68 65 79 20 61 72   times.  They ar
21c24 65 20 65 76 61 6c 75 61 74 65 64 20 6f 6e 63 65  e evaluated once
21c25 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 73   and the results
21c26 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
21c27 6f 6e 0a 2a 2a 20 61 72 65 20 72 65 75 73 65 64  on.** are reused
21c28 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
21c29 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
21c2a 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28  xprCodeAndCache(
21c2b 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
21c2c 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20  xpr *pExpr, int 
21c2d 74 61 72 67 65 74 29 7b 0a 20 20 56 64 62 65 20  target){.  Vdbe 
21c2e 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
21c2f 62 65 3b 0a 20 20 69 6e 74 20 69 6e 52 65 67 3b  be;.  int inReg;
21c30 0a 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74  .  inReg = sqlit
21c31 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
21c32 65 2c 20 70 45 78 70 72 2c 20 74 61 72 67 65 74  e, pExpr, target
21c33 29 3b 0a 20 20 61 73 73 65 72 74 28 20 74 61 72  );.  assert( tar
21c34 67 65 74 3e 30 20 29 3b 0a 20 20 2f 2a 20 54 68  get>0 );.  /* Th
21c35 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
21c36 6c 6c 65 64 20 66 6f 72 20 74 65 72 6d 73 20 74  lled for terms t
21c37 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41  o INSERT or UPDA
21c38 54 45 2e 20 20 41 6e 64 20 74 68 65 20 6f 6e 6c  TE.  And the onl
21c39 79 0a 20 20 2a 2a 20 6f 74 68 65 72 20 70 6c 61  y.  ** other pla
21c3a 63 65 20 77 68 65 72 65 20 65 78 70 72 65 73 73  ce where express
21c3b 69 6f 6e 73 20 63 61 6e 20 62 65 20 63 6f 6e 76  ions can be conv
21c3c 65 72 74 65 64 20 69 6e 74 6f 20 54 4b 5f 52 45  erted into TK_RE
21c3d 47 49 53 54 45 52 20 69 73 0a 20 20 2a 2a 20 69  GISTER is.  ** i
21c3e 6e 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 70  n WHERE clause p
21c3f 72 6f 63 65 73 73 69 6e 67 2e 20 20 53 6f 20 61  rocessing.  So a
21c40 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6d 70 6c  s currently impl
21c41 65 6d 65 6e 74 65 64 2c 20 74 68 65 72 65 20 69  emented, there i
21c42 73 0a 20 20 2a 2a 20 6e 6f 20 77 61 79 20 66 6f  s.  ** no way fo
21c43 72 20 61 20 54 4b 5f 52 45 47 49 53 54 45 52 20  r a TK_REGISTER 
21c44 74 6f 20 65 78 69 73 74 20 68 65 72 65 2e 20 20  to exist here.  
21c45 42 75 74 20 69 74 20 73 65 65 6d 73 20 70 72 75  But it seems pru
21c46 64 65 6e 74 20 74 6f 0a 20 20 2a 2a 20 6b 65 65  dent to.  ** kee
21c47 70 20 74 68 65 20 41 4c 57 41 59 53 28 29 20 69  p the ALWAYS() i
21c48 6e 20 63 61 73 65 20 74 68 65 20 63 6f 6e 64 69  n case the condi
21c49 74 69 6f 6e 73 20 61 62 6f 76 65 20 63 68 61 6e  tions above chan
21c4a 67 65 20 77 69 74 68 20 66 75 74 75 72 65 0a 20  ge with future. 
21c4b 20 2a 2a 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e   ** modification
21c4c 73 20 6f 72 20 65 6e 68 61 6e 63 65 6d 65 6e 74  s or enhancement
21c4d 73 2e 20 2a 2f 0a 20 20 69 66 28 20 41 4c 57 41  s. */.  if( ALWA
21c4e 59 53 28 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  YS(pExpr->op!=TK
21c4f 5f 52 45 47 49 53 54 45 52 29 20 29 7b 20 20 0a  _REGISTER) ){  .
21c50 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 0a 20 20      int iMem;.  
21c51 20 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73    iMem = ++pPars
21c52 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c  e->nMem;.    sql
21c53 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
21c54 2c 20 4f 50 5f 43 6f 70 79 2c 20 69 6e 52 65 67  , OP_Copy, inReg
21c55 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 70 45 78  , iMem);.    pEx
21c56 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 69 4d 65  pr->iTable = iMe
21c57 6d 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70  m;.    pExpr->op
21c58 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a   = TK_REGISTER;.
21c59 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 6e 52    }.  return inR
21c5a 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  eg;.}../*.** Ret
21c5b 75 72 6e 20 54 52 55 45 20 69 66 20 70 45 78 70  urn TRUE if pExp
21c5c 72 20 69 73 20 61 6e 20 63 6f 6e 73 74 61 6e 74  r is an constant
21c5d 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74   expression that
21c5e 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 0a   is appropriate.
21c5f 2a 2a 20 66 6f 72 20 66 61 63 74 6f 72 69 6e 67  ** for factoring
21c60 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2e 20   out of a loop. 
21c61 20 41 70 70 72 6f 70 72 69 61 74 65 20 65 78 70   Appropriate exp
21c62 72 65 73 73 69 6f 6e 73 20 61 72 65 3a 0a 2a 2a  ressions are:.**
21c63 0a 2a 2a 20 20 20 20 2a 20 20 41 6e 79 20 65 78  .**    *  Any ex
21c64 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 65 76  pression that ev
21c65 61 6c 75 61 74 65 73 20 74 6f 20 74 77 6f 20 6f  aluates to two o
21c66 72 20 6d 6f 72 65 20 6f 70 63 6f 64 65 73 2e 0a  r more opcodes..
21c67 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 6e 79 20  **.**    *  Any 
21c68 4f 50 5f 49 6e 74 65 67 65 72 2c 20 4f 50 5f 52  OP_Integer, OP_R
21c69 65 61 6c 2c 20 4f 50 5f 53 74 72 69 6e 67 2c 20  eal, OP_String, 
21c6a 4f 50 5f 42 6c 6f 62 2c 20 4f 50 5f 4e 75 6c 6c  OP_Blob, OP_Null
21c6b 2c 20 0a 2a 2a 20 20 20 20 20 20 20 6f 72 20 4f  , .**       or O
21c6c 50 5f 56 61 72 69 61 62 6c 65 20 74 68 61 74 20  P_Variable that 
21c6d 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f  does not need to
21c6e 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20 61 20   be placed in a 
21c6f 0a 2a 2a 20 20 20 20 20 20 20 73 70 65 63 69 66  .**       specif
21c70 69 63 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 0a  ic register..**.
21c71 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 70  ** There is no p
21c72 6f 69 6e 74 20 69 6e 20 66 61 63 74 6f 72 69 6e  oint in factorin
21c73 67 20 6f 75 74 20 73 69 6e 67 6c 65 2d 69 6e 73  g out single-ins
21c74 74 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61 6e  truction constan
21c75 74 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73  t.** expressions
21c76 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65   that need to be
21c77 20 70 6c 61 63 65 64 20 69 6e 20 61 20 70 61 72   placed in a par
21c78 74 69 63 75 6c 61 72 20 72 65 67 69 73 74 65 72  ticular register
21c79 2e 20 20 0a 2a 2a 20 57 65 20 63 6f 75 6c 64 20  .  .** We could 
21c7a 66 61 63 74 6f 72 20 74 68 65 6d 20 6f 75 74 2c  factor them out,
21c7b 20 62 75 74 20 74 68 65 6e 20 77 65 20 77 6f 75   but then we wou
21c7c 6c 64 20 65 6e 64 20 75 70 20 61 64 64 69 6e 67  ld end up adding
21c7d 20 61 6e 0a 2a 2a 20 4f 50 5f 53 43 6f 70 79 20   an.** OP_SCopy 
21c7e 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 6d  instruction to m
21c7f 6f 76 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e  ove the value in
21c80 74 6f 20 74 68 65 20 63 6f 72 72 65 63 74 20 72  to the correct r
21c81 65 67 69 73 74 65 72 0a 2a 2a 20 6c 61 74 65 72  egister.** later
21c82 2e 20 20 57 65 20 6d 69 67 68 74 20 61 73 20 77  .  We might as w
21c83 65 6c 6c 20 6a 75 73 74 20 75 73 65 20 74 68 65  ell just use the
21c84 20 6f 72 69 67 69 6e 61 6c 20 69 6e 73 74 72 75   original instru
21c85 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 61 76 6f  ction and.** avo
21c86 69 64 20 74 68 65 20 4f 50 5f 53 43 6f 70 79 2e  id the OP_SCopy.
21c87 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
21c88 73 41 70 70 72 6f 70 72 69 61 74 65 46 6f 72 46  sAppropriateForF
21c89 61 63 74 6f 72 69 6e 67 28 45 78 70 72 20 2a 70  actoring(Expr *p
21c8a 29 7b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65  ){.  if( !sqlite
21c8b 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e  3ExprIsConstantN
21c8c 6f 74 4a 6f 69 6e 28 70 29 20 29 7b 0a 20 20 20  otJoin(p) ){.   
21c8d 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 4f   return 0;  /* O
21c8e 6e 6c 79 20 63 6f 6e 73 74 61 6e 74 20 65 78 70  nly constant exp
21c8f 72 65 73 73 69 6f 6e 73 20 61 72 65 20 61 70 70  ressions are app
21c90 72 6f 70 72 69 61 74 65 20 66 6f 72 20 66 61 63  ropriate for fac
21c91 74 6f 72 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20  toring */.  }.  
21c92 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20  if( (p->flags & 
21c93 45 50 5f 46 69 78 65 64 44 65 73 74 29 3d 3d 30  EP_FixedDest)==0
21c94 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31   ){.    return 1
21c95 3b 20 20 2f 2a 20 41 6e 79 20 63 6f 6e 73 74 61  ;  /* Any consta
21c96 6e 74 20 77 69 74 68 6f 75 74 20 61 20 66 69 78  nt without a fix
21c97 65 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69  ed destination i
21c98 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a 2f  s appropriate */
21c99 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70 2d  .  }.  while( p-
21c9a 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20  >op==TK_UPLUS ) 
21c9b 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20  p = p->pLeft;.  
21c9c 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b  switch( p->op ){
21c9d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
21c9e 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41  OMIT_BLOB_LITERA
21c9f 4c 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 4c  L.    case TK_BL
21ca0 4f 42 3a 0a 23 65 6e 64 69 66 0a 20 20 20 20 63  OB:.#endif.    c
21ca1 61 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a  ase TK_VARIABLE:
21ca2 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54  .    case TK_INT
21ca3 45 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20 54  EGER:.    case T
21ca4 4b 5f 46 4c 4f 41 54 3a 0a 20 20 20 20 63 61 73  K_FLOAT:.    cas
21ca5 65 20 54 4b 5f 4e 55 4c 4c 3a 0a 20 20 20 20 63  e TK_NULL:.    c
21ca6 61 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b  ase TK_STRING: {
21ca7 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21ca8 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20   p->op==TK_BLOB 
21ca9 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21caa 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 56 41 52  e( p->op==TK_VAR
21cab 49 41 42 4c 45 20 29 3b 0a 20 20 20 20 20 20 74  IABLE );.      t
21cac 65 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d  estcase( p->op==
21cad 54 4b 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 20  TK_INTEGER );.  
21cae 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d      testcase( p-
21caf 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 3b  >op==TK_FLOAT );
21cb0 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21cb1 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c 20   p->op==TK_NULL 
21cb2 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21cb3 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 53 54 52  e( p->op==TK_STR
21cb4 49 4e 47 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20  ING );.      /* 
21cb5 53 69 6e 67 6c 65 2d 69 6e 73 74 72 75 63 74 69  Single-instructi
21cb6 6f 6e 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 74  on constants wit
21cb7 68 20 61 20 66 69 78 65 64 20 64 65 73 74 69 6e  h a fixed destin
21cb8 61 74 69 6f 6e 20 61 72 65 0a 20 20 20 20 20 20  ation are.      
21cb9 2a 2a 20 62 65 74 74 65 72 20 64 6f 6e 65 20 69  ** better done i
21cba 6e 2d 6c 69 6e 65 2e 20 20 49 66 20 77 65 20 66  n-line.  If we f
21cbb 61 63 74 6f 72 20 74 68 65 6d 2c 20 74 68 65 79  actor them, they
21cbc 20 77 69 6c 6c 20 6a 75 73 74 20 65 6e 64 0a 20   will just end. 
21cbd 20 20 20 20 20 2a 2a 20 75 70 20 67 65 6e 65 72       ** up gener
21cbe 61 74 69 6e 67 20 61 6e 20 4f 50 5f 53 43 6f 70  ating an OP_SCop
21cbf 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 76 61  y to move the va
21cc0 6c 75 65 20 74 6f 20 74 68 65 20 64 65 73 74 69  lue to the desti
21cc1 6e 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20  nation.      ** 
21cc2 72 65 67 69 73 74 65 72 2e 20 2a 2f 0a 20 20 20  register. */.   
21cc3 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
21cc4 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55   }.    case TK_U
21cc5 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 69  MINUS: {.      i
21cc6 66 28 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d  f( p->pLeft->op=
21cc7 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 70 2d 3e  =TK_FLOAT || p->
21cc8 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e  pLeft->op==TK_IN
21cc9 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20 20 20  TEGER ){.       
21cca 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
21ccb 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
21ccc 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
21ccd 74 3a 20 7b 0a 20 20 20 20 20 20 62 72 65 61 6b  t: {.      break
21cce 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
21ccf 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 1;.}../*.**
21cd0 20 49 66 20 70 45 78 70 72 20 69 73 20 61 20 63   If pExpr is a c
21cd1 6f 6e 73 74 61 6e 74 20 65 78 70 72 65 73 73 69  onstant expressi
21cd2 6f 6e 20 74 68 61 74 20 69 73 20 61 70 70 72 6f  on that is appro
21cd3 70 72 69 61 74 65 20 66 6f 72 0a 2a 2a 20 66 61  priate for.** fa
21cd4 63 74 6f 72 69 6e 67 20 6f 75 74 20 6f 66 20 61  ctoring out of a
21cd5 20 6c 6f 6f 70 2c 20 74 68 65 6e 20 65 76 61 6c   loop, then eval
21cd6 75 61 74 65 20 74 68 65 20 65 78 70 72 65 73 73  uate the express
21cd7 69 6f 6e 0a 2a 2a 20 69 6e 74 6f 20 61 20 72 65  ion.** into a re
21cd8 67 69 73 74 65 72 20 61 6e 64 20 63 6f 6e 76 65  gister and conve
21cd9 72 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  rt the expressio
21cda 6e 20 69 6e 74 6f 20 61 20 54 4b 5f 52 45 47 49  n into a TK_REGI
21cdb 53 54 45 52 0a 2a 2a 20 65 78 70 72 65 73 73 69  STER.** expressi
21cdc 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  on..*/.static in
21cdd 74 20 65 76 61 6c 43 6f 6e 73 74 45 78 70 72 28  t evalConstExpr(
21cde 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c  Walker *pWalker,
21cdf 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20   Expr *pExpr){. 
21ce0 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d   Parse *pParse =
21ce1 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65   pWalker->pParse
21ce2 3b 0a 20 20 73 77 69 74 63 68 28 20 70 45 78 70  ;.  switch( pExp
21ce3 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73  r->op ){.    cas
21ce4 65 20 54 4b 5f 52 45 47 49 53 54 45 52 3a 20 7b  e TK_REGISTER: {
21ce5 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52  .      return WR
21ce6 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 20  C_Prune;.    }. 
21ce7 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54     case TK_FUNCT
21ce8 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  ION:.    case TK
21ce9 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20  _AGG_FUNCTION:. 
21cea 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53 54     case TK_CONST
21ceb 5f 46 55 4e 43 3a 20 7b 0a 20 20 20 20 20 20 2f  _FUNC: {.      /
21cec 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 73 20  * The arguments 
21ced 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 68 61  to a function ha
21cee 76 65 20 61 20 66 69 78 65 64 20 64 65 73 74 69  ve a fixed desti
21cef 6e 61 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2a  nation..      **
21cf0 20 4d 61 72 6b 20 74 68 65 6d 20 74 68 69 73 20   Mark them this 
21cf1 77 61 79 20 74 6f 20 61 76 6f 69 64 20 67 65 6e  way to avoid gen
21cf2 65 72 61 74 65 64 20 75 6e 6e 65 65 64 65 64 20  erated unneeded 
21cf3 4f 50 5f 53 43 6f 70 79 0a 20 20 20 20 20 20 2a  OP_SCopy.      *
21cf4 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20  * instructions. 
21cf5 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
21cf6 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20  ExprList *pList 
21cf7 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
21cf8 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
21cf9 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
21cfa 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65  (pExpr, EP_xIsSe
21cfb 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 69  lect) );.      i
21cfc 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20  f( pList ){.    
21cfd 20 20 20 20 69 6e 74 20 69 20 3d 20 70 4c 69 73      int i = pLis
21cfe 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20  t->nExpr;.      
21cff 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
21d00 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20  t_item *pItem = 
21d01 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20  pList->a;.      
21d02 20 20 66 6f 72 28 3b 20 69 3e 30 3b 20 69 2d 2d    for(; i>0; i--
21d03 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
21d04 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53        if( ALWAYS
21d05 28 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 20 29  (pItem->pExpr) )
21d06 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 66   pItem->pExpr->f
21d07 6c 61 67 73 20 7c 3d 20 45 50 5f 46 69 78 65 64  lags |= EP_Fixed
21d08 44 65 73 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Dest;.        }.
21d09 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
21d0a 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
21d0b 20 69 66 28 20 69 73 41 70 70 72 6f 70 72 69 61   if( isAppropria
21d0c 74 65 46 6f 72 46 61 63 74 6f 72 69 6e 67 28 70  teForFactoring(p
21d0d 45 78 70 72 29 20 29 7b 0a 20 20 20 20 69 6e 74  Expr) ){.    int
21d0e 20 72 31 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e   r1 = ++pParse->
21d0f 6e 4d 65 6d 3b 0a 20 20 20 20 69 6e 74 20 72 32  nMem;.    int r2
21d10 3b 0a 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74  ;.    r2 = sqlit
21d11 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74  e3ExprCodeTarget
21d12 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
21d13 72 31 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 56  r1);.    if( NEV
21d14 45 52 28 72 31 21 3d 72 32 29 20 29 20 73 71 6c  ER(r1!=r2) ) sql
21d15 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
21d16 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a  eg(pParse, r1);.
21d17 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 32 20 3d      pExpr->op2 =
21d18 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 20 20   pExpr->op;.    
21d19 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52  pExpr->op = TK_R
21d1a 45 47 49 53 54 45 52 3b 0a 20 20 20 20 70 45 78  EGISTER;.    pEx
21d1b 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 72 32 3b  pr->iTable = r2;
21d1c 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f  .    return WRC_
21d1d 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 72 65 74  Prune;.  }.  ret
21d1e 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65  urn WRC_Continue
21d1f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 65 76  ;.}../*.** Preev
21d20 61 6c 75 61 74 65 20 63 6f 6e 73 74 61 6e 74 20  aluate constant 
21d21 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 77  subexpressions w
21d22 69 74 68 69 6e 20 70 45 78 70 72 20 61 6e 64 20  ithin pExpr and 
21d23 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20 72 65 73  store the.** res
21d24 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74 65 72  ults in register
21d25 73 2e 20 20 4d 6f 64 69 66 79 20 70 45 78 70 72  s.  Modify pExpr
21d26 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f 6e   so that the con
21d27 73 74 61 6e 74 20 73 75 62 65 78 70 72 65 73 69  stant subexpresi
21d28 6f 6e 73 0a 2a 2a 20 61 72 65 20 54 4b 5f 52 45  ons.** are TK_RE
21d29 47 49 53 54 45 52 20 6f 70 63 6f 64 65 73 20 74  GISTER opcodes t
21d2a 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 68 65  hat refer to the
21d2b 20 70 72 65 63 6f 6d 70 75 74 65 64 20 76 61 6c   precomputed val
21d2c 75 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ues..*/.SQLITE_P
21d2d 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
21d2e 74 65 33 45 78 70 72 43 6f 64 65 43 6f 6e 73 74  te3ExprCodeConst
21d2f 61 6e 74 73 28 50 61 72 73 65 20 2a 70 50 61 72  ants(Parse *pPar
21d30 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29  se, Expr *pExpr)
21d31 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20  {.  Walker w;.  
21d32 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20  w.xExprCallback 
21d33 3d 20 65 76 61 6c 43 6f 6e 73 74 45 78 70 72 3b  = evalConstExpr;
21d34 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c  .  w.xSelectCall
21d35 62 61 63 6b 20 3d 20 30 3b 0a 20 20 77 2e 70 50  back = 0;.  w.pP
21d36 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
21d37 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
21d38 28 26 77 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a  (&w, pExpr);.}..
21d39 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
21d3a 63 6f 64 65 20 74 68 61 74 20 70 75 73 68 65 73  code that pushes
21d3b 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 65 76   the value of ev
21d3c 65 72 79 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74  ery element of t
21d3d 68 65 20 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72  he given.** expr
21d3e 65 73 73 69 6f 6e 20 6c 69 73 74 20 69 6e 74 6f  ession list into
21d3f 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20 72   a sequence of r
21d40 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69  egisters beginni
21d41 6e 67 20 61 74 20 74 61 72 67 65 74 2e 0a 2a 2a  ng at target..**
21d42 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
21d43 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74  umber of element
21d44 73 20 65 76 61 6c 75 61 74 65 64 2e 0a 2a 2f 0a  s evaluated..*/.
21d45 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
21d46 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  nt sqlite3ExprCo
21d47 64 65 45 78 70 72 4c 69 73 74 28 0a 20 20 50 61  deExprList(.  Pa
21d48 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
21d49 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
21d4a 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ext */.  ExprLis
21d4b 74 20 2a 70 4c 69 73 74 2c 20 20 20 2f 2a 20 54  t *pList,   /* T
21d4c 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  he expression li
21d4d 73 74 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a  st to be coded *
21d4e 2f 0a 20 20 69 6e 74 20 74 61 72 67 65 74 2c 20  /.  int target, 
21d4f 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20         /* Where 
21d50 74 6f 20 77 72 69 74 65 20 72 65 73 75 6c 74 73  to write results
21d51 20 2a 2f 0a 20 20 69 6e 74 20 64 6f 48 61 72 64   */.  int doHard
21d52 43 6f 70 79 20 20 20 20 20 2f 2a 20 4d 61 6b 65  Copy     /* Make
21d53 20 61 20 68 61 72 64 20 63 6f 70 79 20 6f 66 20   a hard copy of 
21d54 65 76 65 72 79 20 65 6c 65 6d 65 6e 74 20 2a 2f  every element */
21d55 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 45 78 70  .){.  struct Exp
21d56 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  rList_item *pIte
21d57 6d 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20  m;.  int i, n;. 
21d58 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d   assert( pList!=
21d59 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 74  0 );.  assert( t
21d5a 61 72 67 65 74 3e 30 20 29 3b 0a 20 20 6e 20 3d  arget>0 );.  n =
21d5b 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20   pList->nExpr;. 
21d5c 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74   for(pItem=pList
21d5d 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 6e 3b 20 69  ->a, i=0; i<n; i
21d5e 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
21d5f 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 41 6c    if( pItem->iAl
21d60 69 61 73 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ias ){.      int
21d61 20 69 52 65 67 20 3d 20 63 6f 64 65 41 6c 69 61   iReg = codeAlia
21d62 73 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d  s(pParse, pItem-
21d63 3e 69 41 6c 69 61 73 2c 20 70 49 74 65 6d 2d 3e  >iAlias, pItem->
21d64 70 45 78 70 72 2c 20 74 61 72 67 65 74 2b 69 29  pExpr, target+i)
21d65 3b 0a 20 20 20 20 20 20 56 64 62 65 20 2a 76 20  ;.      Vdbe *v 
21d66 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
21d67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
21d68 69 66 28 20 69 52 65 67 21 3d 74 61 72 67 65 74  if( iReg!=target
21d69 2b 69 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  +i ){.        sq
21d6a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
21d6b 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 69 52 65  v, OP_SCopy, iRe
21d6c 67 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a 20 20  g, target+i);.  
21d6d 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
21d6e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
21d6f 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
21d70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 74 61 72  Item->pExpr, tar
21d71 67 65 74 2b 69 29 3b 0a 20 20 20 20 7d 0a 20 20  get+i);.    }.  
21d72 20 20 69 66 28 20 64 6f 48 61 72 64 43 6f 70 79    if( doHardCopy
21d73 20 26 26 20 21 70 50 61 72 73 65 2d 3e 64 62 2d   && !pParse->db-
21d74 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
21d75 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
21d76 70 72 48 61 72 64 43 6f 70 79 28 70 50 61 72 73  prHardCopy(pPars
21d77 65 2c 20 74 61 72 67 65 74 2c 20 6e 29 3b 0a 20  e, target, n);. 
21d78 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
21d79 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  n n;.}../*.** Ge
21d7a 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
21d7b 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73  a boolean expres
21d7c 73 69 6f 6e 20 73 75 63 68 20 74 68 61 74 20 61  sion such that a
21d7d 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a 2a   jump is made.**
21d7e 20 74 6f 20 74 68 65 20 6c 61 62 65 6c 20 22 64   to the label "d
21d7f 65 73 74 22 20 69 66 20 74 68 65 20 65 78 70 72  est" if the expr
21d80 65 73 73 69 6f 6e 20 69 73 20 74 72 75 65 20 62  ession is true b
21d81 75 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20  ut execution.** 
21d82 63 6f 6e 74 69 6e 75 65 73 20 73 74 72 61 69 67  continues straig
21d83 68 74 20 74 68 72 75 20 69 66 20 74 68 65 20 65  ht thru if the e
21d84 78 70 72 65 73 73 69 6f 6e 20 69 73 20 66 61 6c  xpression is fal
21d85 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  se..**.** If the
21d86 20 65 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c   expression eval
21d87 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28 6e  uates to NULL (n
21d88 65 69 74 68 65 72 20 74 72 75 65 20 6e 6f 72 20  either true nor 
21d89 66 61 6c 73 65 29 2c 20 74 68 65 6e 0a 2a 2a 20  false), then.** 
21d8a 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66  take the jump if
21d8b 20 74 68 65 20 6a 75 6d 70 49 66 4e 75 6c 6c 20   the jumpIfNull 
21d8c 66 6c 61 67 20 69 73 20 53 51 4c 49 54 45 5f 4a  flag is SQLITE_J
21d8d 55 4d 50 49 46 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  UMPIFNULL..**.**
21d8e 20 54 68 69 73 20 63 6f 64 65 20 64 65 70 65 6e   This code depen
21d8f 64 73 20 6f 6e 20 74 68 65 20 66 61 63 74 20 74  ds on the fact t
21d90 68 61 74 20 63 65 72 74 61 69 6e 20 74 6f 6b 65  hat certain toke
21d91 6e 20 76 61 6c 75 65 73 20 28 65 78 3a 20 54 4b  n values (ex: TK
21d92 5f 45 51 29 0a 2a 2a 20 61 72 65 20 74 68 65 20  _EQ).** are the 
21d93 73 61 6d 65 20 61 73 20 6f 70 63 6f 64 65 20 76  same as opcode v
21d94 61 6c 75 65 73 20 28 65 78 3a 20 4f 50 5f 45 71  alues (ex: OP_Eq
21d95 29 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  ) that implement
21d96 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
21d97 6e 67 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e  ng.** operation.
21d98 20 20 53 70 65 63 69 61 6c 20 63 6f 6d 6d 65 6e    Special commen
21d99 74 73 20 69 6e 20 76 64 62 65 2e 63 20 61 6e 64  ts in vdbe.c and
21d9a 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61   the mkopcodeh.a
21d9b 77 6b 20 73 63 72 69 70 74 20 69 6e 0a 2a 2a 20  wk script in.** 
21d9c 74 68 65 20 6d 61 6b 65 20 70 72 6f 63 65 73 73  the make process
21d9d 20 63 61 75 73 65 20 74 68 65 73 65 20 76 61 6c   cause these val
21d9e 75 65 73 20 74 6f 20 61 6c 69 67 6e 2e 20 20 41  ues to align.  A
21d9f 73 73 65 72 74 28 29 73 20 69 6e 20 74 68 65 20  ssert()s in the 
21da0 63 6f 64 65 0a 2a 2a 20 62 65 6c 6f 77 20 76 65  code.** below ve
21da1 72 69 66 79 20 74 68 61 74 20 74 68 65 20 6e 75  rify that the nu
21da2 6d 62 65 72 73 20 61 72 65 20 61 6c 69 67 6e 65  mbers are aligne
21da3 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2f 0a  d correctly..*/.
21da4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
21da5 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49  oid sqlite3ExprI
21da6 66 54 72 75 65 28 50 61 72 73 65 20 2a 70 50 61  fTrue(Parse *pPa
21da7 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72  rse, Expr *pExpr
21da8 2c 20 69 6e 74 20 64 65 73 74 2c 20 69 6e 74 20  , int dest, int 
21da9 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 56  jumpIfNull){.  V
21daa 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
21dab 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f 70  >pVdbe;.  int op
21dac 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46   = 0;.  int regF
21dad 72 65 65 31 20 3d 20 30 3b 0a 20 20 69 6e 74 20  ree1 = 0;.  int 
21dae 72 65 67 46 72 65 65 32 20 3d 20 30 3b 0a 20 20  regFree2 = 0;.  
21daf 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a 20 20 61  int r1, r2;..  a
21db0 73 73 65 72 74 28 20 6a 75 6d 70 49 66 4e 75 6c  ssert( jumpIfNul
21db1 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  l==SQLITE_JUMPIF
21db2 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e 75  NULL || jumpIfNu
21db3 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 4e  ll==0 );.  if( N
21db4 45 56 45 52 28 76 3d 3d 30 29 20 29 20 20 20 20  EVER(v==0) )    
21db5 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 45 78 69   return;  /* Exi
21db6 73 74 61 6e 63 65 20 6f 66 20 56 44 42 45 20 63  stance of VDBE c
21db7 68 65 63 6b 65 64 20 62 79 20 63 61 6c 6c 65 72  hecked by caller
21db8 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28   */.  if( NEVER(
21db9 70 45 78 70 72 3d 3d 30 29 20 29 20 72 65 74 75  pExpr==0) ) retu
21dba 72 6e 3b 20 20 2f 2a 20 4e 6f 20 77 61 79 20 74  rn;  /* No way t
21dbb 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20 2a  his can happen *
21dbc 2f 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e  /.  op = pExpr->
21dbd 6f 70 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70  op;.  switch( op
21dbe 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   ){.    case TK_
21dbf 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  AND: {.      int
21dc0 20 64 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62   d2 = sqlite3Vdb
21dc1 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
21dc2 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a       testcase( j
21dc3 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a  umpIfNull==0 );.
21dc4 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
21dc5 72 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73  rCachePush(pPars
21dc6 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
21dc7 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61  3ExprIfFalse(pPa
21dc8 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
21dc9 74 2c 20 64 32 2c 6a 75 6d 70 49 66 4e 75 6c 6c  t, d2,jumpIfNull
21dca 5e 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55  ^SQLITE_JUMPIFNU
21dcb 4c 4c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  LL);.      sqlit
21dcc 65 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61  e3ExprIfTrue(pPa
21dcd 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  rse, pExpr->pRig
21dce 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66  ht, dest, jumpIf
21dcf 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c  Null);.      sql
21dd0 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
21dd1 61 62 65 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20  abel(v, d2);.   
21dd2 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
21dd3 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31  chePop(pParse, 1
21dd4 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
21dd5 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
21dd6 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20 20 20 74 65  K_OR: {.      te
21dd7 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75  stcase( jumpIfNu
21dd8 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73  ll==0 );.      s
21dd9 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65  qlite3ExprIfTrue
21dda 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
21ddb 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d  pLeft, dest, jum
21ddc 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  pIfNull);.      
21ddd 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75  sqlite3ExprIfTru
21dde 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
21ddf 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a  >pRight, dest, j
21de0 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20  umpIfNull);.    
21de1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
21de2 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20     case TK_NOT: 
21de3 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
21de4 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20  ( jumpIfNull==0 
21de5 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
21de6 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72  ExprIfFalse(pPar
21de7 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
21de8 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75  , dest, jumpIfNu
21de9 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ll);.      break
21dea 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
21deb 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65   TK_LT:.    case
21dec 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65   TK_LE:.    case
21ded 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63 61 73 65   TK_GT:.    case
21dee 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63 61 73 65   TK_GE:.    case
21def 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65   TK_NE:.    case
21df0 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20   TK_EQ: {.      
21df1 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f  assert( TK_LT==O
21df2 50 5f 4c 74 20 29 3b 0a 20 20 20 20 20 20 61 73  P_Lt );.      as
21df3 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f  sert( TK_LE==OP_
21df4 4c 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  Le );.      asse
21df5 72 74 28 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74  rt( TK_GT==OP_Gt
21df6 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
21df7 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29  ( TK_GE==OP_Ge )
21df8 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
21df9 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a  TK_EQ==OP_Eq );.
21dfa 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
21dfb 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20  _NE==OP_Ne );.  
21dfc 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
21dfd 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20  ==TK_LT );.     
21dfe 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
21dff 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65  K_LE );.      te
21e00 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47  stcase( op==TK_G
21e01 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  T );.      testc
21e02 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29  ase( op==TK_GE )
21e03 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21e04 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20  ( op==TK_EQ );. 
21e05 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
21e06 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20  p==TK_NE );.    
21e07 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70    testcase( jump
21e08 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20  IfNull==0 );.   
21e09 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70     codeCompareOp
21e0a 65 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70  erands(pParse, p
21e0b 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31  Expr->pLeft, &r1
21e0c 2c 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20  , &regFree1,.   
21e0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
21e0f 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72  Expr->pRight, &r
21e10 32 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20  2, &regFree2);. 
21e11 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65       codeCompare
21e12 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
21e13 70 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52  pLeft, pExpr->pR
21e14 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20  ight, op,.      
21e15 20 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20              r1, 
21e16 72 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66  r2, dest, jumpIf
21e17 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 74 65 73  Null);.      tes
21e18 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
21e19 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  =0 );.      test
21e1a 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d  case( regFree2==
21e1b 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  0 );.      break
21e1c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
21e1d 20 54 4b 5f 49 53 3a 0a 20 20 20 20 63 61 73 65   TK_IS:.    case
21e1e 20 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20 20 20   TK_ISNOT: {.   
21e1f 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
21e20 3d 54 4b 5f 49 53 20 29 3b 0a 20 20 20 20 20 20  =TK_IS );.      
21e21 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
21e22 5f 49 53 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20  _ISNOT );.      
21e23 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61  codeCompareOpera
21e24 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70  nds(pParse, pExp
21e25 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26  r->pLeft, &r1, &
21e26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20  regFree1,.      
21e27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e28 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70              pExp
21e29 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20  r->pRight, &r2, 
21e2a 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20  &regFree2);.    
21e2b 20 20 6f 70 20 3d 20 28 6f 70 3d 3d 54 4b 5f 49    op = (op==TK_I
21e2c 53 29 20 3f 20 54 4b 5f 45 51 20 3a 20 54 4b 5f  S) ? TK_EQ : TK_
21e2d 4e 45 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f  NE;.      codeCo
21e2e 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 45  mpare(pParse, pE
21e2f 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70  xpr->pLeft, pExp
21e30 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20  r->pRight, op,. 
21e31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e32 20 72 31 2c 20 72 32 2c 20 64 65 73 74 2c 20 53   r1, r2, dest, S
21e33 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 29 3b 0a 20  QLITE_NULLEQ);. 
21e34 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
21e35 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20  egFree1==0 );.  
21e36 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65      testcase( re
21e37 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20  gFree2==0 );.   
21e38 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
21e39 20 20 20 20 63 61 73 65 20 54 4b 5f 49 53 4e 55      case TK_ISNU
21e3a 4c 4c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  LL:.    case TK_
21e3b 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20  NOTNULL: {.     
21e3c 20 61 73 73 65 72 74 28 20 54 4b 5f 49 53 4e 55   assert( TK_ISNU
21e3d 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b  LL==OP_IsNull );
21e3e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
21e3f 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f  K_NOTNULL==OP_No
21e40 74 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 74  tNull );.      t
21e41 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
21e42 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20  ISNULL );.      
21e43 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
21e44 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 20  _NOTNULL );.    
21e45 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78    r1 = sqlite3Ex
21e46 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
21e47 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
21e48 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20   &regFree1);.   
21e49 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21e4a 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72 31 2c 20  dOp2(v, op, r1, 
21e4b 64 65 73 74 29 3b 0a 20 20 20 20 20 20 74 65 73  dest);.      tes
21e4c 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
21e4d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61  =0 );.      brea
21e4e 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
21e4f 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a  e TK_BETWEEN: {.
21e50 20 20 20 20 20 20 2f 2a 20 20 20 20 78 20 42 45        /*    x BE
21e51 54 57 45 45 4e 20 79 20 41 4e 44 20 7a 0a 20 20  TWEEN y AND z.  
21e52 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
21e53 49 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f  Is equivalent to
21e54 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20   .      **.     
21e55 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e 44 20   **    x>=y AND 
21e56 78 3c 3d 7a 0a 20 20 20 20 20 20 2a 2a 0a 20 20  x<=z.      **.  
21e57 20 20 20 20 2a 2a 20 43 6f 64 65 20 69 74 20 61      ** Code it a
21e58 73 20 73 75 63 68 2c 20 74 61 6b 69 6e 67 20 63  s such, taking c
21e59 61 72 65 20 74 6f 20 64 6f 20 74 68 65 20 63 6f  are to do the co
21e5a 6d 6d 6f 6e 20 73 75 62 65 78 70 72 65 73 73 69  mmon subexpressi
21e5b 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d  on.      ** elem
21e5c 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 2e 0a 20  entation of x.. 
21e5d 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 45 78       */.      Ex
21e5e 70 72 20 65 78 70 72 41 6e 64 3b 0a 20 20 20 20  pr exprAnd;.    
21e5f 20 20 45 78 70 72 20 63 6f 6d 70 4c 65 66 74 3b    Expr compLeft;
21e60 0a 20 20 20 20 20 20 45 78 70 72 20 63 6f 6d 70  .      Expr comp
21e61 52 69 67 68 74 3b 0a 20 20 20 20 20 20 45 78 70  Right;.      Exp
21e62 72 20 65 78 70 72 58 3b 0a 0a 20 20 20 20 20 20  r exprX;..      
21e63 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
21e64 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
21e65 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b  EP_xIsSelect) );
21e66 0a 20 20 20 20 20 20 65 78 70 72 58 20 3d 20 2a  .      exprX = *
21e67 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20  pExpr->pLeft;.  
21e68 20 20 20 20 65 78 70 72 41 6e 64 2e 6f 70 20 3d      exprAnd.op =
21e69 20 54 4b 5f 41 4e 44 3b 0a 20 20 20 20 20 20 65   TK_AND;.      e
21e6a 78 70 72 41 6e 64 2e 70 4c 65 66 74 20 3d 20 26  xprAnd.pLeft = &
21e6b 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20 20  compLeft;.      
21e6c 65 78 70 72 41 6e 64 2e 70 52 69 67 68 74 20 3d  exprAnd.pRight =
21e6d 20 26 63 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20   &compRight;.   
21e6e 20 20 20 63 6f 6d 70 4c 65 66 74 2e 6f 70 20 3d     compLeft.op =
21e6f 20 54 4b 5f 47 45 3b 0a 20 20 20 20 20 20 63 6f   TK_GE;.      co
21e70 6d 70 4c 65 66 74 2e 70 4c 65 66 74 20 3d 20 26  mpLeft.pLeft = &
21e71 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d  exprX;.      com
21e72 70 4c 65 66 74 2e 70 52 69 67 68 74 20 3d 20 70  pLeft.pRight = p
21e73 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61  Expr->x.pList->a
21e74 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [0].pExpr;.     
21e75 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70 20 3d 20   compRight.op = 
21e76 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 63 6f 6d  TK_LE;.      com
21e77 70 52 69 67 68 74 2e 70 4c 65 66 74 20 3d 20 26  pRight.pLeft = &
21e78 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d  exprX;.      com
21e79 70 52 69 67 68 74 2e 70 52 69 67 68 74 20 3d 20  pRight.pRight = 
21e7a 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e  pExpr->x.pList->
21e7b 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[1].pExpr;.    
21e7c 20 20 65 78 70 72 58 2e 69 54 61 62 6c 65 20 3d    exprX.iTable =
21e7d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21e7e 54 65 6d 70 28 70 50 61 72 73 65 2c 20 26 65 78  Temp(pParse, &ex
21e7f 70 72 58 2c 20 26 72 65 67 46 72 65 65 31 29 3b  prX, &regFree1);
21e80 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21e81 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a   regFree1==0 );.
21e82 20 20 20 20 20 20 65 78 70 72 58 2e 6f 70 20 3d        exprX.op =
21e83 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20   TK_REGISTER;.  
21e84 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75      testcase( ju
21e85 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20  mpIfNull==0 );. 
21e86 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
21e87 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 26  IfTrue(pParse, &
21e88 65 78 70 72 41 6e 64 2c 20 64 65 73 74 2c 20 6a  exprAnd, dest, j
21e89 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20  umpIfNull);.    
21e8a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
21e8b 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
21e8c 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
21e8d 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
21e8e 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72 65 67  rse, pExpr, &reg
21e8f 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71  Free1);.      sq
21e90 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
21e91 76 2c 20 4f 50 5f 49 66 2c 20 72 31 2c 20 64 65  v, OP_If, r1, de
21e92 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d  st, jumpIfNull!=
21e93 30 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  0);.      testca
21e94 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20  se( regFree1==0 
21e95 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21e96 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30  e( jumpIfNull==0
21e97 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
21e98 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
21e99 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
21e9a 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 72  eg(pParse, regFr
21e9b 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52  ee1);.  sqlite3R
21e9c 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
21e9d 61 72 73 65 2c 20 72 65 67 46 72 65 65 32 29 3b  arse, regFree2);
21e9e 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65    .}../*.** Gene
21e9f 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 20  rate code for a 
21ea0 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69  boolean expressi
21ea1 6f 6e 20 73 75 63 68 20 74 68 61 74 20 61 20 6a  on such that a j
21ea2 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a 2a 20 74  ump is made.** t
21ea3 6f 20 74 68 65 20 6c 61 62 65 6c 20 22 64 65 73  o the label "des
21ea4 74 22 20 69 66 20 74 68 65 20 65 78 70 72 65 73  t" if the expres
21ea5 73 69 6f 6e 20 69 73 20 66 61 6c 73 65 20 62 75  sion is false bu
21ea6 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 63  t execution.** c
21ea7 6f 6e 74 69 6e 75 65 73 20 73 74 72 61 69 67 68  ontinues straigh
21ea8 74 20 74 68 72 75 20 69 66 20 74 68 65 20 65 78  t thru if the ex
21ea9 70 72 65 73 73 69 6f 6e 20 69 73 20 74 72 75 65  pression is true
21eaa 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65  ..**.** If the e
21eab 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c 75 61  xpression evalua
21eac 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28 6e 65 69  tes to NULL (nei
21ead 74 68 65 72 20 74 72 75 65 20 6e 6f 72 20 66 61  ther true nor fa
21eae 6c 73 65 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d  lse) then.** jum
21eaf 70 20 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c 20  p if jumpIfNull 
21eb0 69 73 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  is SQLITE_JUMPIF
21eb1 4e 55 4c 4c 20 6f 72 20 66 61 6c 6c 20 74 68 72  NULL or fall thr
21eb2 6f 75 67 68 20 69 66 20 6a 75 6d 70 49 66 4e 75  ough if jumpIfNu
21eb3 6c 6c 0a 2a 2a 20 69 73 20 30 2e 0a 2a 2f 0a 53  ll.** is 0..*/.S
21eb4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
21eb5 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49 66  id sqlite3ExprIf
21eb6 46 61 6c 73 65 28 50 61 72 73 65 20 2a 70 50 61  False(Parse *pPa
21eb7 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72  rse, Expr *pExpr
21eb8 2c 20 69 6e 74 20 64 65 73 74 2c 20 69 6e 74 20  , int dest, int 
21eb9 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 56  jumpIfNull){.  V
21eba 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
21ebb 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f 70  >pVdbe;.  int op
21ebc 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46   = 0;.  int regF
21ebd 72 65 65 31 20 3d 20 30 3b 0a 20 20 69 6e 74 20  ree1 = 0;.  int 
21ebe 72 65 67 46 72 65 65 32 20 3d 20 30 3b 0a 20 20  regFree2 = 0;.  
21ebf 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a 20 20 61  int r1, r2;..  a
21ec0 73 73 65 72 74 28 20 6a 75 6d 70 49 66 4e 75 6c  ssert( jumpIfNul
21ec1 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  l==SQLITE_JUMPIF
21ec2 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e 75  NULL || jumpIfNu
21ec3 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 4e  ll==0 );.  if( N
21ec4 45 56 45 52 28 76 3d 3d 30 29 20 29 20 72 65 74  EVER(v==0) ) ret
21ec5 75 72 6e 3b 20 2f 2a 20 45 78 69 73 74 61 6e 63  urn; /* Existanc
21ec6 65 20 6f 66 20 56 44 42 45 20 63 68 65 63 6b 65  e of VDBE checke
21ec7 64 20 62 79 20 63 61 6c 6c 65 72 20 2a 2f 0a 20  d by caller */. 
21ec8 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20 29 20   if( pExpr==0 ) 
21ec9 20 20 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a     return;..  /*
21eca 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 70 45   The value of pE
21ecb 78 70 72 2d 3e 6f 70 20 61 6e 64 20 6f 70 20 61  xpr->op and op a
21ecc 72 65 20 72 65 6c 61 74 65 64 20 61 73 20 66 6f  re related as fo
21ecd 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  llows:.  **.  **
21ece 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70         pExpr->op
21ecf 20 20 20 20 20 20 20 20 20 20 20 20 6f 70 0a 20              op. 
21ed0 20 2a 2a 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d   **       ------
21ed1 2d 2d 2d 20 20 20 20 20 20 20 20 20 20 2d 2d 2d  ---          ---
21ed2 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 20 20 20  -------.  **    
21ed3 20 20 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20     TK_ISNULL    
21ed4 20 20 20 20 20 20 4f 50 5f 4e 6f 74 4e 75 6c 6c        OP_NotNull
21ed5 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4e  .  **       TK_N
21ed6 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 4f  OTNULL         O
21ed7 50 5f 49 73 4e 75 6c 6c 0a 20 20 2a 2a 20 20 20  P_IsNull.  **   
21ed8 20 20 20 20 54 4b 5f 4e 45 20 20 20 20 20 20 20      TK_NE       
21ed9 20 20 20 20 20 20 20 4f 50 5f 45 71 0a 20 20 2a         OP_Eq.  *
21eda 2a 20 20 20 20 20 20 20 54 4b 5f 45 51 20 20 20  *       TK_EQ   
21edb 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4e 65             OP_Ne
21edc 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 47  .  **       TK_G
21edd 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f  T              O
21ede 50 5f 4c 65 0a 20 20 2a 2a 20 20 20 20 20 20 20  P_Le.  **       
21edf 54 4b 5f 4c 45 20 20 20 20 20 20 20 20 20 20 20  TK_LE           
21ee0 20 20 20 4f 50 5f 47 74 0a 20 20 2a 2a 20 20 20     OP_Gt.  **   
21ee1 20 20 20 20 54 4b 5f 47 45 20 20 20 20 20 20 20      TK_GE       
21ee2 20 20 20 20 20 20 20 4f 50 5f 4c 74 0a 20 20 2a         OP_Lt.  *
21ee3 2a 20 20 20 20 20 20 20 54 4b 5f 4c 54 20 20 20  *       TK_LT   
21ee4 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 47 65             OP_Ge
21ee5 0a 20 20 2a 2a 0a 20 20 2a 2a 20 46 6f 72 20 6f  .  **.  ** For o
21ee6 74 68 65 72 20 76 61 6c 75 65 73 20 6f 66 20 70  ther values of p
21ee7 45 78 70 72 2d 3e 6f 70 2c 20 6f 70 20 69 73 20  Expr->op, op is 
21ee8 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 75 6e  undefined and un
21ee9 75 73 65 64 2e 0a 20 20 2a 2a 20 54 68 65 20 76  used..  ** The v
21eea 61 6c 75 65 20 6f 66 20 54 4b 5f 20 61 6e 64 20  alue of TK_ and 
21eeb 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 73 20 61 72  OP_ constants ar
21eec 65 20 61 72 72 61 6e 67 65 64 20 73 75 63 68 20  e arranged such 
21eed 74 68 61 74 20 77 65 0a 20 20 2a 2a 20 63 61 6e  that we.  ** can
21eee 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 70   compute the map
21eef 70 69 6e 67 20 61 62 6f 76 65 20 75 73 69 6e 67  ping above using
21ef0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65   the following e
21ef1 78 70 72 65 73 73 69 6f 6e 2e 0a 20 20 2a 2a 20  xpression..  ** 
21ef2 41 73 73 65 72 74 28 29 73 20 76 65 72 69 66 79  Assert()s verify
21ef3 20 74 68 61 74 20 74 68 65 20 63 6f 6d 70 75 74   that the comput
21ef4 61 74 69 6f 6e 20 69 73 20 63 6f 72 72 65 63 74  ation is correct
21ef5 2e 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d 20 28 28  ..  */.  op = ((
21ef6 70 45 78 70 72 2d 3e 6f 70 2b 28 54 4b 5f 49 53  pExpr->op+(TK_IS
21ef7 4e 55 4c 4c 26 31 29 29 5e 31 29 2d 28 54 4b 5f  NULL&1))^1)-(TK_
21ef8 49 53 4e 55 4c 4c 26 31 29 3b 0a 0a 20 20 2f 2a  ISNULL&1);..  /*
21ef9 20 56 65 72 69 66 79 20 63 6f 72 72 65 63 74 20   Verify correct 
21efa 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 54 4b 5f  alignment of TK_
21efb 20 61 6e 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e   and OP_ constan
21efc 74 73 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  ts.  */.  assert
21efd 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ( pExpr->op!=TK_
21efe 49 53 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50  ISNULL || op==OP
21eff 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 61 73  _NotNull );.  as
21f00 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21  sert( pExpr->op!
21f01 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 7c 7c 20 6f  =TK_NOTNULL || o
21f02 70 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a  p==OP_IsNull );.
21f03 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
21f04 3e 6f 70 21 3d 54 4b 5f 4e 45 20 7c 7c 20 6f 70  >op!=TK_NE || op
21f05 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 61 73 73  ==OP_Eq );.  ass
21f06 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d  ert( pExpr->op!=
21f07 54 4b 5f 45 51 20 7c 7c 20 6f 70 3d 3d 4f 50 5f  TK_EQ || op==OP_
21f08 4e 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Ne );.  assert( 
21f09 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 54  pExpr->op!=TK_LT
21f0a 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 47 65 20 29 3b   || op==OP_Ge );
21f0b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72  .  assert( pExpr
21f0c 2d 3e 6f 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 6f  ->op!=TK_LE || o
21f0d 70 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 61 73  p==OP_Gt );.  as
21f0e 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21  sert( pExpr->op!
21f0f 3d 54 4b 5f 47 54 20 7c 7c 20 6f 70 3d 3d 4f 50  =TK_GT || op==OP
21f10 5f 4c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _Le );.  assert(
21f11 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47   pExpr->op!=TK_G
21f12 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 74 20 29  E || op==OP_Lt )
21f13 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78  ;..  switch( pEx
21f14 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61  pr->op ){.    ca
21f15 73 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20  se TK_AND: {.   
21f16 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d     testcase( jum
21f17 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20  pIfNull==0 );.  
21f18 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
21f19 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70  fFalse(pParse, p
21f1a 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73  Expr->pLeft, des
21f1b 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a  t, jumpIfNull);.
21f1c 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
21f1d 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c  rIfFalse(pParse,
21f1e 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20   pExpr->pRight, 
21f1f 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c  dest, jumpIfNull
21f20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
21f21 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
21f22 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20 20 20 69 6e  K_OR: {.      in
21f23 74 20 64 32 20 3d 20 73 71 6c 69 74 65 33 56 64  t d2 = sqlite3Vd
21f24 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
21f25 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21f26 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b  jumpIfNull==0 );
21f27 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
21f28 70 72 43 61 63 68 65 50 75 73 68 28 70 50 61 72  prCachePush(pPar
21f29 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  se);.      sqlit
21f2a 65 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61  e3ExprIfTrue(pPa
21f2b 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
21f2c 74 2c 20 64 32 2c 20 6a 75 6d 70 49 66 4e 75 6c  t, d2, jumpIfNul
21f2d 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  l^SQLITE_JUMPIFN
21f2e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ULL);.      sqli
21f2f 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70  te3ExprIfFalse(p
21f30 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52  Parse, pExpr->pR
21f31 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70  ight, dest, jump
21f32 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73  IfNull);.      s
21f33 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
21f34 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 3b 0a 20  eLabel(v, d2);. 
21f35 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
21f36 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c  CachePop(pParse,
21f37 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   1);.      break
21f38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
21f39 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20   TK_NOT: {.     
21f3a 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72   sqlite3ExprIfTr
21f3b 75 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  ue(pParse, pExpr
21f3c 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a  ->pLeft, dest, j
21f3d 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20  umpIfNull);.    
21f3e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
21f3f 20 20 20 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20     case TK_LT:. 
21f40 20 20 20 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20     case TK_LE:. 
21f41 20 20 20 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20     case TK_GT:. 
21f42 20 20 20 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20     case TK_GE:. 
21f43 20 20 20 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20     case TK_NE:. 
21f44 20 20 20 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b     case TK_EQ: {
21f45 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21f46 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20   op==TK_LT );.  
21f47 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
21f48 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20  ==TK_LE );.     
21f49 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
21f4a 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 74 65  K_GT );.      te
21f4b 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47  stcase( op==TK_G
21f4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  E );.      testc
21f4d 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29  ase( op==TK_EQ )
21f4e 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21f4f 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20  ( op==TK_NE );. 
21f50 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a       testcase( j
21f51 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a  umpIfNull==0 );.
21f52 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72        codeCompar
21f53 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73 65  eOperands(pParse
21f54 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
21f55 26 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c 0a  &r1, &regFree1,.
21f56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f58 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c    pExpr->pRight,
21f59 20 26 72 32 2c 20 26 72 65 67 46 72 65 65 32 29   &r2, &regFree2)
21f5a 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70  ;.      codeComp
21f5b 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  are(pParse, pExp
21f5c 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d  r->pLeft, pExpr-
21f5d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20  >pRight, op,.   
21f5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
21f5f 31 2c 20 72 32 2c 20 64 65 73 74 2c 20 6a 75 6d  1, r2, dest, jum
21f60 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  pIfNull);.      
21f61 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
21f62 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74  e1==0 );.      t
21f63 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
21f64 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72  2==0 );.      br
21f65 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
21f66 61 73 65 20 54 4b 5f 49 53 3a 0a 20 20 20 20 63  ase TK_IS:.    c
21f67 61 73 65 20 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a  ase TK_ISNOT: {.
21f68 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21f69 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53  pExpr->op==TK_IS
21f6a 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21f6b 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  se( pExpr->op==T
21f6c 4b 5f 49 53 4e 4f 54 20 29 3b 0a 20 20 20 20 20  K_ISNOT );.     
21f6d 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72   codeCompareOper
21f6e 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45 78  ands(pParse, pEx
21f6f 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20  pr->pLeft, &r1, 
21f70 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20  &regFree1,.     
21f71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f72 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
21f73 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c  pr->pRight, &r2,
21f74 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20   &regFree2);.   
21f75 20 20 20 6f 70 20 3d 20 28 70 45 78 70 72 2d 3e     op = (pExpr->
21f76 6f 70 3d 3d 54 4b 5f 49 53 29 20 3f 20 54 4b 5f  op==TK_IS) ? TK_
21f77 4e 45 20 3a 20 54 4b 5f 45 51 3b 0a 20 20 20 20  NE : TK_EQ;.    
21f78 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50    codeCompare(pP
21f79 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
21f7a 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  ft, pExpr->pRigh
21f7b 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20 20 20  t, op,.         
21f7c 20 20 20 20 20 20 20 20 20 72 31 2c 20 72 32 2c           r1, r2,
21f7d 20 64 65 73 74 2c 20 53 51 4c 49 54 45 5f 4e 55   dest, SQLITE_NU
21f7e 4c 4c 45 51 29 3b 0a 20 20 20 20 20 20 74 65 73  LLEQ);.      tes
21f7f 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
21f80 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  =0 );.      test
21f81 63 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d  case( regFree2==
21f82 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  0 );.      break
21f83 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
21f84 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20   TK_ISNULL:.    
21f85 63 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a  case TK_NOTNULL:
21f86 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73   {.      testcas
21f87 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c  e( op==TK_ISNULL
21f88 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21f89 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55  se( op==TK_NOTNU
21f8a 4c 4c 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d  LL );.      r1 =
21f8b 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21f8c 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78  Temp(pParse, pEx
21f8d 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46  pr->pLeft, &regF
21f8e 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  ree1);.      sql
21f8f 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
21f90 2c 20 6f 70 2c 20 72 31 2c 20 64 65 73 74 29 3b  , op, r1, dest);
21f91 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21f92 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a   regFree1==0 );.
21f93 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21f94 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42   }.    case TK_B
21f95 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20  ETWEEN: {.      
21f96 2f 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20  /*    x BETWEEN 
21f97 79 20 41 4e 44 20 7a 0a 20 20 20 20 20 20 2a 2a  y AND z.      **
21f98 0a 20 20 20 20 20 20 2a 2a 20 49 73 20 65 71 75  .      ** Is equ
21f99 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20  ivalent to .    
21f9a 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20    **.      **   
21f9b 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20   x>=y AND x<=z. 
21f9c 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
21f9d 20 43 6f 64 65 20 69 74 20 61 73 20 73 75 63 68   Code it as such
21f9e 2c 20 74 61 6b 69 6e 67 20 63 61 72 65 20 74 6f  , taking care to
21f9f 20 64 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 73   do the common s
21fa0 75 62 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20  ubexpression.   
21fa1 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 61 74 69     ** elementati
21fa2 6f 6e 20 6f 66 20 78 2e 0a 20 20 20 20 20 20 2a  on of x..      *
21fa3 2f 0a 20 20 20 20 20 20 45 78 70 72 20 65 78 70  /.      Expr exp
21fa4 72 41 6e 64 3b 0a 20 20 20 20 20 20 45 78 70 72  rAnd;.      Expr
21fa5 20 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20   compLeft;.     
21fa6 20 45 78 70 72 20 63 6f 6d 70 52 69 67 68 74 3b   Expr compRight;
21fa7 0a 20 20 20 20 20 20 45 78 70 72 20 65 78 70 72  .      Expr expr
21fa8 58 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74  X;..      assert
21fa9 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
21faa 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
21fab 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20  Select) );.     
21fac 20 65 78 70 72 58 20 3d 20 2a 70 45 78 70 72 2d   exprX = *pExpr-
21fad 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 65 78  >pLeft;.      ex
21fae 70 72 41 6e 64 2e 6f 70 20 3d 20 54 4b 5f 41 4e  prAnd.op = TK_AN
21faf 44 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64  D;.      exprAnd
21fb0 2e 70 4c 65 66 74 20 3d 20 26 63 6f 6d 70 4c 65  .pLeft = &compLe
21fb1 66 74 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e  ft;.      exprAn
21fb2 64 2e 70 52 69 67 68 74 20 3d 20 26 63 6f 6d 70  d.pRight = &comp
21fb3 52 69 67 68 74 3b 0a 20 20 20 20 20 20 63 6f 6d  Right;.      com
21fb4 70 4c 65 66 74 2e 6f 70 20 3d 20 54 4b 5f 47 45  pLeft.op = TK_GE
21fb5 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74  ;.      compLeft
21fb6 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b  .pLeft = &exprX;
21fb7 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e  .      compLeft.
21fb8 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e  pRight = pExpr->
21fb9 78 2e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  x.pList->a[0].pE
21fba 78 70 72 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52  xpr;.      compR
21fbb 69 67 68 74 2e 6f 70 20 3d 20 54 4b 5f 4c 45 3b  ight.op = TK_LE;
21fbc 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74  .      compRight
21fbd 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58 3b  .pLeft = &exprX;
21fbe 0a 20 20 20 20 20 20 63 6f 6d 70 52 69 67 68 74  .      compRight
21fbf 2e 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d  .pRight = pExpr-
21fc0 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70  >x.pList->a[1].p
21fc1 45 78 70 72 3b 0a 20 20 20 20 20 20 65 78 70 72  Expr;.      expr
21fc2 58 2e 69 54 61 62 6c 65 20 3d 20 73 71 6c 69 74  X.iTable = sqlit
21fc3 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70  e3ExprCodeTemp(p
21fc4 50 61 72 73 65 2c 20 26 65 78 70 72 58 2c 20 26  Parse, &exprX, &
21fc5 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20  regFree1);.     
21fc6 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72   testcase( regFr
21fc7 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ee1==0 );.      
21fc8 65 78 70 72 58 2e 6f 70 20 3d 20 54 4b 5f 52 45  exprX.op = TK_RE
21fc9 47 49 53 54 45 52 3b 0a 20 20 20 20 20 20 74 65  GISTER;.      te
21fca 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75  stcase( jumpIfNu
21fcb 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73  ll==0 );.      s
21fcc 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
21fcd 65 28 70 50 61 72 73 65 2c 20 26 65 78 70 72 41  e(pParse, &exprA
21fce 6e 64 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66  nd, dest, jumpIf
21fcf 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65  Null);.      bre
21fd0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
21fd1 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72  fault: {.      r
21fd2 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  1 = sqlite3ExprC
21fd3 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20  odeTemp(pParse, 
21fd4 70 45 78 70 72 2c 20 26 72 65 67 46 72 65 65 31  pExpr, &regFree1
21fd5 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
21fd6 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
21fd7 5f 49 66 4e 6f 74 2c 20 72 31 2c 20 64 65 73 74  _IfNot, r1, dest
21fd8 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 29  , jumpIfNull!=0)
21fd9 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21fda 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b  ( regFree1==0 );
21fdb 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21fdc 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29   jumpIfNull==0 )
21fdd 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
21fde 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
21fdf 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
21fe0 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65  (pParse, regFree
21fe1 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c  1);.  sqlite3Rel
21fe2 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
21fe3 73 65 2c 20 72 65 67 46 72 65 65 32 29 3b 0a 7d  se, regFree2);.}
21fe4 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20 64 65 65  ../*.** Do a dee
21fe5 70 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20  p comparison of 
21fe6 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 20 74  two expression t
21fe7 72 65 65 73 2e 20 20 52 65 74 75 72 6e 20 54 52  rees.  Return TR
21fe8 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f 29 0a 2a 2a  UE (non-zero).**
21fe9 20 69 66 20 74 68 65 79 20 61 72 65 20 69 64 65   if they are ide
21fea 6e 74 69 63 61 6c 20 61 6e 64 20 72 65 74 75 72  ntical and retur
21feb 6e 20 46 41 4c 53 45 20 69 66 20 74 68 65 79 20  n FALSE if they 
21fec 64 69 66 66 65 72 20 69 6e 20 61 6e 79 20 77 61  differ in any wa
21fed 79 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d  y..**.** Sometim
21fee 65 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  es this routine 
21fef 77 69 6c 6c 20 72 65 74 75 72 6e 20 46 41 4c 53  will return FALS
21ff0 45 20 65 76 65 6e 20 69 66 20 74 68 65 20 74 77  E even if the tw
21ff1 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a  o expressions.**
21ff2 20 72 65 61 6c 6c 79 20 61 72 65 20 65 71 75 69   really are equi
21ff3 76 61 6c 65 6e 74 2e 20 20 49 66 20 77 65 20 63  valent.  If we c
21ff4 61 6e 6e 6f 74 20 70 72 6f 76 65 20 74 68 61 74  annot prove that
21ff5 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73   the expressions
21ff6 20 61 72 65 0a 2a 2a 20 69 64 65 6e 74 69 63 61   are.** identica
21ff7 6c 2c 20 77 65 20 72 65 74 75 72 6e 20 46 41 4c  l, we return FAL
21ff8 53 45 20 6a 75 73 74 20 74 6f 20 62 65 20 73 61  SE just to be sa
21ff9 66 65 2e 20 20 53 6f 20 69 66 20 74 68 69 73 20  fe.  So if this 
21ffa 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72  routine.** retur
21ffb 6e 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 79  ns false, then y
21ffc 6f 75 20 64 6f 20 6e 6f 74 20 72 65 61 6c 6c 79  ou do not really
21ffd 20 6b 6e 6f 77 20 66 6f 72 20 63 65 72 74 61 69   know for certai
21ffe 6e 20 69 66 20 74 68 65 20 74 77 6f 0a 2a 2a 20  n if the two.** 
21fff 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20  expressions are 
22000 74 68 65 20 73 61 6d 65 2e 20 20 42 75 74 20 69  the same.  But i
22001 66 20 79 6f 75 20 67 65 74 20 61 20 54 52 55 45  f you get a TRUE
22002 20 72 65 74 75 72 6e 2c 20 74 68 65 6e 20 79 6f   return, then yo
22003 75 0a 2a 2a 20 63 61 6e 20 62 65 20 73 75 72 65  u.** can be sure
22004 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73   the expressions
22005 20 61 72 65 20 74 68 65 20 73 61 6d 65 2e 20 20   are the same.  
22006 49 6e 20 74 68 65 20 70 6c 61 63 65 73 20 77 68  In the places wh
22007 65 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74  ere.** this rout
22008 69 6e 65 20 69 73 20 75 73 65 64 2c 20 69 74 20  ine is used, it 
22009 64 6f 65 73 20 6e 6f 74 20 68 75 72 74 20 74 6f  does not hurt to
2200a 20 67 65 74 20 61 6e 20 65 78 74 72 61 20 46 41   get an extra FA
2200b 4c 53 45 20 2d 20 74 68 61 74 0a 2a 2a 20 6a 75  LSE - that.** ju
2200c 73 74 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20  st might result 
2200d 69 6e 20 73 6f 6d 65 20 73 6c 69 67 68 74 6c 79  in some slightly
2200e 20 73 6c 6f 77 65 72 20 63 6f 64 65 2e 20 20 42   slower code.  B
2200f 75 74 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20  ut returning.** 
22010 61 6e 20 69 6e 63 6f 72 72 65 63 74 20 54 52 55  an incorrect TRU
22011 45 20 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f 20  E could lead to 
22012 61 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 0a 2a  a malfunction..*
22013 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
22014 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
22015 43 6f 6d 70 61 72 65 28 45 78 70 72 20 2a 70 41  Compare(Expr *pA
22016 2c 20 45 78 70 72 20 2a 70 42 29 7b 0a 20 20 69  , Expr *pB){.  i
22017 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 41 3d 3d  nt i;.  if( pA==
22018 30 7c 7c 70 42 3d 3d 30 20 29 7b 0a 20 20 20 20  0||pB==0 ){.    
22019 72 65 74 75 72 6e 20 70 42 3d 3d 70 41 3b 0a 20  return pB==pA;. 
2201a 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 45 78   }.  assert( !Ex
2201b 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
2201c 28 70 41 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  (pA, EP_TokenOnl
2201d 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b  y|EP_Reduced) );
2201e 0a 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72  .  assert( !Expr
2201f 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70  HasAnyProperty(p
22020 42 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c  B, EP_TokenOnly|
22021 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20  EP_Reduced) );. 
22022 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
22023 65 72 74 79 28 70 41 2c 20 45 50 5f 78 49 73 53  erty(pA, EP_xIsS
22024 65 6c 65 63 74 29 20 7c 7c 20 45 78 70 72 48 61  elect) || ExprHa
22025 73 50 72 6f 70 65 72 74 79 28 70 42 2c 20 45 50  sProperty(pB, EP
22026 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
22027 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
22028 0a 20 20 69 66 28 20 28 70 41 2d 3e 66 6c 61 67  .  if( (pA->flag
22029 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 74 29  s & EP_Distinct)
2202a 21 3d 28 70 42 2d 3e 66 6c 61 67 73 20 26 20 45  !=(pB->flags & E
2202b 50 5f 44 69 73 74 69 6e 63 74 29 20 29 20 72 65  P_Distinct) ) re
2202c 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 41  turn 0;.  if( pA
2202d 2d 3e 6f 70 21 3d 70 42 2d 3e 6f 70 20 29 20 72  ->op!=pB->op ) r
2202e 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 21  eturn 0;.  if( !
2202f 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61  sqlite3ExprCompa
22030 72 65 28 70 41 2d 3e 70 4c 65 66 74 2c 20 70 42  re(pA->pLeft, pB
22031 2d 3e 70 4c 65 66 74 29 20 29 20 72 65 74 75 72  ->pLeft) ) retur
22032 6e 20 30 3b 0a 20 20 69 66 28 20 21 73 71 6c 69  n 0;.  if( !sqli
22033 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70  te3ExprCompare(p
22034 41 2d 3e 70 52 69 67 68 74 2c 20 70 42 2d 3e 70  A->pRight, pB->p
22035 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20  Right) ) return 
22036 30 3b 0a 0a 20 20 69 66 28 20 70 41 2d 3e 78 2e  0;..  if( pA->x.
22037 70 4c 69 73 74 20 26 26 20 70 42 2d 3e 78 2e 70  pList && pB->x.p
22038 4c 69 73 74 20 29 7b 0a 20 20 20 20 69 66 28 20  List ){.    if( 
22039 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78  pA->x.pList->nEx
2203a 70 72 21 3d 70 42 2d 3e 78 2e 70 4c 69 73 74 2d  pr!=pB->x.pList-
2203b 3e 6e 45 78 70 72 20 29 20 72 65 74 75 72 6e 20  >nExpr ) return 
2203c 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  0;.    for(i=0; 
2203d 69 3c 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e  i<pA->x.pList->n
2203e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
2203f 20 20 45 78 70 72 20 2a 70 45 78 70 72 41 20 3d    Expr *pExprA =
22040 20 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b   pA->x.pList->a[
22041 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  i].pExpr;.      
22042 45 78 70 72 20 2a 70 45 78 70 72 42 20 3d 20 70  Expr *pExprB = p
22043 42 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 69 5d  B->x.pList->a[i]
22044 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66  .pExpr;.      if
22045 28 20 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f  ( !sqlite3ExprCo
22046 6d 70 61 72 65 28 70 45 78 70 72 41 2c 20 70 45  mpare(pExprA, pE
22047 78 70 72 42 29 20 29 20 72 65 74 75 72 6e 20 30  xprB) ) return 0
22048 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
22049 69 66 28 20 70 41 2d 3e 78 2e 70 4c 69 73 74 20  if( pA->x.pList 
2204a 7c 7c 20 70 42 2d 3e 78 2e 70 4c 69 73 74 20 29  || pB->x.pList )
2204b 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
2204c 20 20 7d 0a 0a 20 20 69 66 28 20 70 41 2d 3e 69    }..  if( pA->i
2204d 54 61 62 6c 65 21 3d 70 42 2d 3e 69 54 61 62 6c  Table!=pB->iTabl
2204e 65 20 7c 7c 20 70 41 2d 3e 69 43 6f 6c 75 6d 6e  e || pA->iColumn
2204f 21 3d 70 42 2d 3e 69 43 6f 6c 75 6d 6e 20 29 20  !=pB->iColumn ) 
22050 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
22051 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
22052 70 41 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29  pA, EP_IntValue)
22053 20 29 7b 0a 20 20 20 20 69 66 28 20 21 45 78 70   ){.    if( !Exp
22054 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 42 2c  rHasProperty(pB,
22055 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 7c 7c   EP_IntValue) ||
22056 20 70 41 2d 3e 75 2e 69 56 61 6c 75 65 21 3d 70   pA->u.iValue!=p
22057 42 2d 3e 75 2e 69 56 61 6c 75 65 20 29 7b 0a 20  B->u.iValue ){. 
22058 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
22059 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
2205a 20 70 41 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55   pA->op!=TK_COLU
2205b 4d 4e 20 26 26 20 70 41 2d 3e 75 2e 7a 54 6f 6b  MN && pA->u.zTok
2205c 65 6e 20 29 7b 0a 20 20 20 20 69 66 28 20 45 78  en ){.    if( Ex
2205d 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 42  prHasProperty(pB
2205e 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 7c  , EP_IntValue) |
2205f 7c 20 4e 45 56 45 52 28 70 42 2d 3e 75 2e 7a 54  | NEVER(pB->u.zT
22060 6f 6b 65 6e 3d 3d 30 29 20 29 20 72 65 74 75 72  oken==0) ) retur
22061 6e 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  n 0;.    if( sql
22062 69 74 65 33 53 74 72 49 43 6d 70 28 70 41 2d 3e  ite3StrICmp(pA->
22063 75 2e 7a 54 6f 6b 65 6e 2c 70 42 2d 3e 75 2e 7a  u.zToken,pB->u.z
22064 54 6f 6b 65 6e 29 21 3d 30 20 29 7b 0a 20 20 20  Token)!=0 ){.   
22065 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
22066 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
22067 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  1;.}.../*.** Add
22068 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74   a new element t
22069 6f 20 74 68 65 20 70 41 67 67 49 6e 66 6f 2d 3e  o the pAggInfo->
2206a 61 43 6f 6c 5b 5d 20 61 72 72 61 79 2e 20 20 52  aCol[] array.  R
2206b 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20  eturn the index 
2206c 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 65 6c  of.** the new el
2206d 65 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 61  ement.  Return a
2206e 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72   negative number
2206f 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73   if malloc fails
22070 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
22071 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e  addAggInfoColumn
22072 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 41 67  (sqlite3 *db, Ag
22073 67 49 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a 20  gInfo *pInfo){. 
22074 20 69 6e 74 20 69 3b 0a 20 20 70 49 6e 66 6f 2d   int i;.  pInfo-
22075 3e 61 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 41  >aCol = sqlite3A
22076 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20  rrayAllocate(.  
22077 20 20 20 20 20 64 62 2c 0a 20 20 20 20 20 20 20       db,.       
22078 70 49 6e 66 6f 2d 3e 61 43 6f 6c 2c 0a 20 20 20  pInfo->aCol,.   
22079 20 20 20 20 73 69 7a 65 6f 66 28 70 49 6e 66 6f      sizeof(pInfo
2207a 2d 3e 61 43 6f 6c 5b 30 5d 29 2c 0a 20 20 20 20  ->aCol[0]),.    
2207b 20 20 20 33 2c 0a 20 20 20 20 20 20 20 26 70 49     3,.       &pI
2207c 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 2c 0a 20 20  nfo->nColumn,.  
2207d 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f       &pInfo->nCo
2207e 6c 75 6d 6e 41 6c 6c 6f 63 2c 0a 20 20 20 20 20  lumnAlloc,.     
2207f 20 20 26 69 0a 20 20 29 3b 0a 20 20 72 65 74 75    &i.  );.  retu
22080 72 6e 20 69 3b 0a 7d 20 20 20 20 0a 0a 2f 2a 0a  rn i;.}    ../*.
22081 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65  ** Add a new ele
22082 6d 65 6e 74 20 74 6f 20 74 68 65 20 70 41 67 67  ment to the pAgg
22083 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d 20 61 72  Info->aFunc[] ar
22084 72 61 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65  ray.  Return the
22085 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65   index of.** the
22086 20 6e 65 77 20 65 6c 65 6d 65 6e 74 2e 20 20 52   new element.  R
22087 65 74 75 72 6e 20 61 20 6e 65 67 61 74 69 76 65  eturn a negative
22088 20 6e 75 6d 62 65 72 20 69 66 20 6d 61 6c 6c 6f   number if mallo
22089 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74  c fails..*/.stat
2208a 69 63 20 69 6e 74 20 61 64 64 41 67 67 49 6e 66  ic int addAggInf
2208b 6f 46 75 6e 63 28 73 71 6c 69 74 65 33 20 2a 64  oFunc(sqlite3 *d
2208c 62 2c 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66  b, AggInfo *pInf
2208d 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 70  o){.  int i;.  p
2208e 49 6e 66 6f 2d 3e 61 46 75 6e 63 20 3d 20 73 71  Info->aFunc = sq
2208f 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61  lite3ArrayAlloca
22090 74 65 28 0a 20 20 20 20 20 20 20 64 62 2c 20 0a  te(.       db, .
22091 20 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 46         pInfo->aF
22092 75 6e 63 2c 0a 20 20 20 20 20 20 20 73 69 7a 65  unc,.       size
22093 6f 66 28 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b  of(pInfo->aFunc[
22094 30 5d 29 2c 0a 20 20 20 20 20 20 20 33 2c 0a 20  0]),.       3,. 
22095 20 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 46        &pInfo->nF
22096 75 6e 63 2c 0a 20 20 20 20 20 20 20 26 70 49 6e  unc,.       &pIn
22097 66 6f 2d 3e 6e 46 75 6e 63 41 6c 6c 6f 63 2c 0a  fo->nFuncAlloc,.
22098 20 20 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20         &i.  );. 
22099 20 72 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20 20   return i;.}    
2209a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ../*.** This is 
2209b 74 68 65 20 78 45 78 70 72 43 61 6c 6c 62 61 63  the xExprCallbac
2209c 6b 20 66 6f 72 20 61 20 74 72 65 65 20 77 61 6c  k for a tree wal
2209d 6b 65 72 2e 20 20 49 74 20 69 73 20 75 73 65 64  ker.  It is used
2209e 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74   to.** implement
2209f 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c   sqlite3ExprAnal
220a0 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 29 2e  yzeAggregates().
220a1 20 20 53 65 65 20 73 71 6c 69 74 65 33 45 78 70    See sqlite3Exp
220a2 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74  rAnalyzeAggregat
220a3 65 73 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74 69  es.** for additi
220a4 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
220a5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
220a6 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65  analyzeAggregate
220a7 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72  (Walker *pWalker
220a8 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a  , Expr *pExpr){.
220a9 20 20 69 6e 74 20 69 3b 0a 20 20 4e 61 6d 65 43    int i;.  NameC
220aa 6f 6e 74 65 78 74 20 2a 70 4e 43 20 3d 20 70 57  ontext *pNC = pW
220ab 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20  alker->u.pNC;.  
220ac 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20  Parse *pParse = 
220ad 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 53  pNC->pParse;.  S
220ae 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74  rcList *pSrcList
220af 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74   = pNC->pSrcList
220b0 3b 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41 67  ;.  AggInfo *pAg
220b1 67 49 6e 66 6f 20 3d 20 70 4e 43 2d 3e 70 41 67  gInfo = pNC->pAg
220b2 67 49 6e 66 6f 3b 0a 0a 20 20 73 77 69 74 63 68  gInfo;..  switch
220b3 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20  ( pExpr->op ){. 
220b4 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43     case TK_AGG_C
220b5 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65 20  OLUMN:.    case 
220b6 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20  TK_COLUMN: {.   
220b7 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78     testcase( pEx
220b8 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43  pr->op==TK_AGG_C
220b9 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74  OLUMN );.      t
220ba 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e  estcase( pExpr->
220bb 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b  op==TK_COLUMN );
220bc 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20  .      /* Check 
220bd 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 63 6f  to see if the co
220be 6c 75 6d 6e 20 69 73 20 69 6e 20 6f 6e 65 20 6f  lumn is in one o
220bf 66 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20  f the tables in 
220c0 74 68 65 20 46 52 4f 4d 0a 20 20 20 20 20 20 2a  the FROM.      *
220c1 2a 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20  * clause of the 
220c2 61 67 67 72 65 67 61 74 65 20 71 75 65 72 79 20  aggregate query 
220c3 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57  */.      if( ALW
220c4 41 59 53 28 70 53 72 63 4c 69 73 74 21 3d 30 29  AYS(pSrcList!=0)
220c5 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 75   ){.        stru
220c6 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
220c7 2a 70 49 74 65 6d 20 3d 20 70 53 72 63 4c 69 73  *pItem = pSrcLis
220c8 74 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 66 6f  t->a;.        fo
220c9 72 28 69 3d 30 3b 20 69 3c 70 53 72 63 4c 69 73  r(i=0; i<pSrcLis
220ca 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49  t->nSrc; i++, pI
220cb 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  tem++){.        
220cc 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f    struct AggInfo
220cd 5f 63 6f 6c 20 2a 70 43 6f 6c 3b 0a 20 20 20 20  _col *pCol;.    
220ce 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45        assert( !E
220cf 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74  xprHasAnyPropert
220d0 79 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65  y(pExpr, EP_Toke
220d1 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64  nOnly|EP_Reduced
220d2 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ) );.          i
220d3 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  f( pExpr->iTable
220d4 3d 3d 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  ==pItem->iCursor
220d5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
220d6 2f 2a 20 49 66 20 77 65 20 72 65 61 63 68 20 74  /* If we reach t
220d7 68 69 73 20 70 6f 69 6e 74 2c 20 69 74 20 6d 65  his point, it me
220d8 61 6e 73 20 74 68 61 74 20 70 45 78 70 72 20 72  ans that pExpr r
220d9 65 66 65 72 73 20 74 6f 20 61 20 74 61 62 6c 65  efers to a table
220da 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
220db 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 20 46  that is in the F
220dc 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68  ROM clause of th
220dd 65 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72  e aggregate quer
220de 79 2e 20 20 0a 20 20 20 20 20 20 20 20 20 20 20  y.  .           
220df 20 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 20 20   **.            
220e0 2a 2a 20 4d 61 6b 65 20 61 6e 20 65 6e 74 72 79  ** Make an entry
220e1 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20   for the column 
220e2 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f  in pAggInfo->aCo
220e3 6c 5b 5d 20 69 66 20 74 68 65 72 65 0a 20 20 20  l[] if there.   
220e4 20 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 6e           ** is n
220e5 6f 74 20 61 6e 20 65 6e 74 72 79 20 74 68 65 72  ot an entry ther
220e6 65 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 20  e already..     
220e7 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
220e8 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20        int k;.   
220e9 20 20 20 20 20 20 20 20 20 70 43 6f 6c 20 3d 20           pCol = 
220ea 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b 0a  pAggInfo->aCol;.
220eb 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28              for(
220ec 6b 3d 30 3b 20 6b 3c 70 41 67 67 49 6e 66 6f 2d  k=0; k<pAggInfo-
220ed 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b 2c 20 70  >nColumn; k++, p
220ee 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  Col++){.        
220ef 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e        if( pCol->
220f0 69 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e 69  iTable==pExpr->i
220f1 54 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20 20  Table &&.       
220f2 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d             pCol-
220f3 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72 2d  >iColumn==pExpr-
220f4 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20  >iColumn ){.    
220f5 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
220f6 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  k;.             
220f7 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d   }.            }
220f8 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
220f9 20 28 6b 3e 3d 70 41 67 67 49 6e 66 6f 2d 3e 6e   (k>=pAggInfo->n
220fa 43 6f 6c 75 6d 6e 29 0a 20 20 20 20 20 20 20 20  Column).        
220fb 20 20 20 20 20 26 26 20 28 6b 20 3d 20 61 64 64       && (k = add
220fc 41 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28 70 50  AggInfoColumn(pP
220fd 61 72 73 65 2d 3e 64 62 2c 20 70 41 67 67 49 6e  arse->db, pAggIn
220fe 66 6f 29 29 3e 3d 30 20 0a 20 20 20 20 20 20 20  fo))>=0 .       
220ff 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
22100 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 26 70 41        pCol = &pA
22101 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 6b 5d 3b  ggInfo->aCol[k];
22102 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
22103 43 6f 6c 2d 3e 70 54 61 62 20 3d 20 70 45 78 70  Col->pTab = pExp
22104 72 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20  r->pTab;.       
22105 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 54 61         pCol->iTa
22106 62 6c 65 20 3d 20 70 45 78 70 72 2d 3e 69 54 61  ble = pExpr->iTa
22107 62 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ble;.           
22108 20 20 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e     pCol->iColumn
22109 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d   = pExpr->iColum
2210a 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n;.             
2210b 20 70 43 6f 6c 2d 3e 69 4d 65 6d 20 3d 20 2b 2b   pCol->iMem = ++
2210c 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2210d 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c              pCol
2210e 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 20  ->iSorterColumn 
2210f 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20  = -1;.          
22110 20 20 20 20 70 43 6f 6c 2d 3e 70 45 78 70 72 20      pCol->pExpr 
22111 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20  = pExpr;.       
22112 20 20 20 20 20 20 20 69 66 28 20 70 41 67 67 49         if( pAggI
22113 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b  nfo->pGroupBy ){
22114 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
22115 20 69 6e 74 20 6a 2c 20 6e 3b 0a 20 20 20 20 20   int j, n;.     
22116 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 4c             ExprL
22117 69 73 74 20 2a 70 47 42 20 3d 20 70 41 67 67 49  ist *pGB = pAggI
22118 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20  nfo->pGroupBy;. 
22119 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
2211a 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
2211b 74 65 6d 20 2a 70 54 65 72 6d 20 3d 20 70 47 42  tem *pTerm = pGB
2211c 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ->a;.           
2211d 20 20 20 20 20 6e 20 3d 20 70 47 42 2d 3e 6e 45       n = pGB->nE
2211e 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20  xpr;.           
2211f 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
22120 6e 3b 20 6a 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29  n; j++, pTerm++)
22121 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
22122 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70      Expr *pE = p
22123 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20  Term->pExpr;.   
22124 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
22125 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  f( pE->op==TK_CO
22126 4c 55 4d 4e 20 26 26 20 70 45 2d 3e 69 54 61 62  LUMN && pE->iTab
22127 6c 65 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c  le==pExpr->iTabl
22128 65 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20  e &&.           
22129 20 20 20 20 20 20 20 20 20 20 20 70 45 2d 3e 69             pE->i
2212a 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72 2d 3e 69  Column==pExpr->i
2212b 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20  Column ){.      
2212c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43                pC
2212d 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d  ol->iSorterColum
2212e 6e 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20  n = j;.         
2212f 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
22130 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
22131 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
22132 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
22133 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
22134 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e        if( pCol->
22135 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3c 30 20  iSorterColumn<0 
22136 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
22137 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72     pCol->iSorter
22138 43 6f 6c 75 6d 6e 20 3d 20 70 41 67 67 49 6e 66  Column = pAggInf
22139 6f 2d 3e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d  o->nSortingColum
2213a 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  n++;.           
2213b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
2213c 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f   }.            /
2213d 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 77 20 61  * There is now a
2213e 6e 20 65 6e 74 72 79 20 66 6f 72 20 70 45 78 70  n entry for pExp
2213f 72 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61  r in pAggInfo->a
22140 43 6f 6c 5b 5d 20 28 65 69 74 68 65 72 0a 20 20  Col[] (either.  
22141 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 63            ** bec
22142 61 75 73 65 20 69 74 20 77 61 73 20 74 68 65 72  ause it was ther
22143 65 20 62 65 66 6f 72 65 20 6f 72 20 62 65 63 61  e before or beca
22144 75 73 65 20 77 65 20 6a 75 73 74 20 63 72 65 61  use we just crea
22145 74 65 64 20 69 74 29 2e 0a 20 20 20 20 20 20 20  ted it)..       
22146 20 20 20 20 20 2a 2a 20 43 6f 6e 76 65 72 74 20       ** Convert 
22147 74 68 65 20 70 45 78 70 72 20 74 6f 20 62 65 20  the pExpr to be 
22148 61 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20  a TK_AGG_COLUMN 
22149 72 65 66 65 72 72 69 6e 67 20 74 6f 20 74 68 61  referring to tha
2214a 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  t.            **
2214b 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b   pAggInfo->aCol[
2214c 5d 20 65 6e 74 72 79 2e 0a 20 20 20 20 20 20 20  ] entry..       
2214d 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
2214e 20 20 20 20 45 78 70 72 53 65 74 49 72 72 65 64      ExprSetIrred
2214f 75 63 69 62 6c 65 28 70 45 78 70 72 29 3b 0a 20  ucible(pExpr);. 
22150 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
22151 2d 3e 70 41 67 67 49 6e 66 6f 20 3d 20 70 41 67  ->pAggInfo = pAg
22152 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 20  gInfo;.         
22153 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54     pExpr->op = T
22154 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3b 0a 20 20  K_AGG_COLUMN;.  
22155 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
22156 3e 69 41 67 67 20 3d 20 28 69 31 36 29 6b 3b 0a  >iAgg = (i16)k;.
22157 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
22158 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 20 2f  k;.          } /
22159 2a 20 65 6e 64 69 66 20 70 45 78 70 72 2d 3e 69  * endif pExpr->i
2215a 54 61 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e 69 43  Table==pItem->iC
2215b 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20  ursor */.       
2215c 20 7d 20 2f 2a 20 65 6e 64 20 6c 6f 6f 70 20 6f   } /* end loop o
2215d 76 65 72 20 70 53 72 63 4c 69 73 74 20 2a 2f 0a  ver pSrcList */.
2215e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
2215f 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a  turn WRC_Prune;.
22160 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
22161 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 20  K_AGG_FUNCTION: 
22162 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  {.      /* The p
22163 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 74 65  NC->nDepth==0 te
22164 73 74 20 63 61 75 73 65 73 20 61 67 67 72 65 67  st causes aggreg
22165 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e  ate functions in
22166 20 73 75 62 71 75 65 72 69 65 73 0a 20 20 20 20   subqueries.    
22167 20 20 2a 2a 20 74 6f 20 62 65 20 69 67 6e 6f 72    ** to be ignor
22168 65 64 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  ed */.      if( 
22169 70 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 29  pNC->nDepth==0 )
2216a 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65  {.        /* Che
2216b 63 6b 20 74 6f 20 73 65 65 20 69 66 20 70 45 78  ck to see if pEx
2216c 70 72 20 69 73 20 61 20 64 75 70 6c 69 63 61 74  pr is a duplicat
2216d 65 20 6f 66 20 61 6e 6f 74 68 65 72 20 61 67 67  e of another agg
2216e 72 65 67 61 74 65 20 0a 20 20 20 20 20 20 20 20  regate .        
2216f 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74  ** function that
22170 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74   is already in t
22171 68 65 20 70 41 67 67 49 6e 66 6f 20 73 74 72 75  he pAggInfo stru
22172 63 74 75 72 65 0a 20 20 20 20 20 20 20 20 2a 2f  cture.        */
22173 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20  .        struct 
22174 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 49  AggInfo_func *pI
22175 74 65 6d 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e  tem = pAggInfo->
22176 61 46 75 6e 63 3b 0a 20 20 20 20 20 20 20 20 66  aFunc;.        f
22177 6f 72 28 69 3d 30 3b 20 69 3c 70 41 67 67 49 6e  or(i=0; i<pAggIn
22178 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20  fo->nFunc; i++, 
22179 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
2217a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 45      if( sqlite3E
2217b 78 70 72 43 6f 6d 70 61 72 65 28 70 49 74 65 6d  xprCompare(pItem
2217c 2d 3e 70 45 78 70 72 2c 20 70 45 78 70 72 29 20  ->pExpr, pExpr) 
2217d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  ){.            b
2217e 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
2217f 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
22180 20 20 20 20 69 66 28 20 69 3e 3d 70 41 67 67 49      if( i>=pAggI
22181 6e 66 6f 2d 3e 6e 46 75 6e 63 20 29 7b 0a 20 20  nfo->nFunc ){.  
22182 20 20 20 20 20 20 20 20 2f 2a 20 70 45 78 70 72          /* pExpr
22183 20 69 73 20 6f 72 69 67 69 6e 61 6c 2e 20 20 4d   is original.  M
22184 61 6b 65 20 61 20 6e 65 77 20 65 6e 74 72 79 20  ake a new entry 
22185 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75  in pAggInfo->aFu
22186 6e 63 5b 5d 0a 20 20 20 20 20 20 20 20 20 20 2a  nc[].          *
22187 2f 0a 20 20 20 20 20 20 20 20 20 20 75 38 20 65  /.          u8 e
22188 6e 63 20 3d 20 45 4e 43 28 70 50 61 72 73 65 2d  nc = ENC(pParse-
22189 3e 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20  >db);.          
2218a 69 20 3d 20 61 64 64 41 67 67 49 6e 66 6f 46 75  i = addAggInfoFu
2218b 6e 63 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  nc(pParse->db, p
2218c 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  AggInfo);.      
2218d 20 20 20 20 69 66 28 20 69 3e 3d 30 20 29 7b 0a      if( i>=0 ){.
2218e 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65              asse
2218f 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
22190 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78  erty(pExpr, EP_x
22191 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20  IsSelect) );.   
22192 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 20 3d           pItem =
22193 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e   &pAggInfo->aFun
22194 63 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  c[i];.          
22195 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d    pItem->pExpr =
22196 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20   pExpr;.        
22197 20 20 20 20 70 49 74 65 6d 2d 3e 69 4d 65 6d 20      pItem->iMem 
22198 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
22199 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73  ;.            as
2219a 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72  sert( !ExprHasPr
2219b 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
2219c 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20  _IntValue) );.  
2219d 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d            pItem-
2219e 3e 70 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33  >pFunc = sqlite3
2219f 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50 61  FindFunction(pPa
221a0 72 73 65 2d 3e 64 62 2c 0a 20 20 20 20 20 20 20  rse->db,.       
221a1 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70              pExp
221a2 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 73 71 6c  r->u.zToken, sql
221a3 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 45 78  ite3Strlen30(pEx
221a4 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2c 0a 20  pr->u.zToken),. 
221a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
221a6 20 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74    pExpr->x.pList
221a7 20 3f 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73   ? pExpr->x.pLis
221a8 74 2d 3e 6e 45 78 70 72 20 3a 20 30 2c 20 65 6e  t->nExpr : 0, en
221a9 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  c, 0);.         
221aa 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c     if( pExpr->fl
221ab 61 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63  ags & EP_Distinc
221ac 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  t ){.           
221ad 20 20 20 70 49 74 65 6d 2d 3e 69 44 69 73 74 69     pItem->iDisti
221ae 6e 63 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  nct = pParse->nT
221af 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  ab++;.          
221b0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
221b1 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 44         pItem->iD
221b2 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b 0a 20 20  istinct = -1;.  
221b3 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
221b4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
221b5 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b  }.        /* Mak
221b6 65 20 70 45 78 70 72 20 70 6f 69 6e 74 20 74 6f  e pExpr point to
221b7 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
221b8 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63   pAggInfo->aFunc
221b9 5b 5d 20 65 6e 74 72 79 0a 20 20 20 20 20 20 20  [] entry.       
221ba 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65   */.        asse
221bb 72 74 28 20 21 45 78 70 72 48 61 73 41 6e 79 50  rt( !ExprHasAnyP
221bc 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
221bd 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52  P_TokenOnly|EP_R
221be 65 64 75 63 65 64 29 20 29 3b 0a 20 20 20 20 20  educed) );.     
221bf 20 20 20 45 78 70 72 53 65 74 49 72 72 65 64 75     ExprSetIrredu
221c0 63 69 62 6c 65 28 70 45 78 70 72 29 3b 0a 20 20  cible(pExpr);.  
221c1 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 41 67        pExpr->iAg
221c2 67 20 3d 20 28 69 31 36 29 69 3b 0a 20 20 20 20  g = (i16)i;.    
221c3 20 20 20 20 70 45 78 70 72 2d 3e 70 41 67 67 49      pExpr->pAggI
221c4 6e 66 6f 20 3d 20 70 41 67 67 49 6e 66 6f 3b 0a  nfo = pAggInfo;.
221c5 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57          return W
221c6 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 20 20  RC_Prune;.      
221c7 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
221c8 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75  turn WRC_Continu
221c9 65 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  e;.}.static int 
221ca 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65  analyzeAggregate
221cb 73 49 6e 53 65 6c 65 63 74 28 57 61 6c 6b 65 72  sInSelect(Walker
221cc 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63   *pWalker, Selec
221cd 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20 4e  t *pSelect){.  N
221ce 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 20  ameContext *pNC 
221cf 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43  = pWalker->u.pNC
221d0 3b 0a 20 20 69 66 28 20 70 4e 43 2d 3e 6e 44 65  ;.  if( pNC->nDe
221d1 70 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e  pth==0 ){.    pN
221d2 43 2d 3e 6e 44 65 70 74 68 2b 2b 3b 0a 20 20 20  C->nDepth++;.   
221d3 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
221d4 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 53 65 6c  ct(pWalker, pSel
221d5 65 63 74 29 3b 0a 20 20 20 20 70 4e 43 2d 3e 6e  ect);.    pNC->n
221d6 44 65 70 74 68 2d 2d 3b 0a 20 20 20 20 72 65 74  Depth--;.    ret
221d7 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20  urn WRC_Prune;. 
221d8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
221d9 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b  rn WRC_Continue;
221da 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e  .  }.}../*.** An
221db 61 6c 79 7a 65 20 74 68 65 20 67 69 76 65 6e 20  alyze the given 
221dc 65 78 70 72 65 73 73 69 6f 6e 20 6c 6f 6f 6b 69  expression looki
221dd 6e 67 20 66 6f 72 20 61 67 67 72 65 67 61 74 65  ng for aggregate
221de 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 0a 2a   functions and.*
221df 2a 20 66 6f 72 20 76 61 72 69 61 62 6c 65 73 20  * for variables 
221e0 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20  that need to be 
221e1 61 64 64 65 64 20 74 6f 20 74 68 65 20 70 50 61  added to the pPa
221e2 72 73 65 2d 3e 61 41 67 67 5b 5d 20 61 72 72 61  rse->aAgg[] arra
221e3 79 2e 0a 2a 2a 20 4d 61 6b 65 20 61 64 64 69 74  y..** Make addit
221e4 69 6f 6e 61 6c 20 65 6e 74 72 69 65 73 20 74 6f  ional entries to
221e5 20 74 68 65 20 70 50 61 72 73 65 2d 3e 61 41 67   the pParse->aAg
221e6 67 5b 5d 20 61 72 72 61 79 20 61 73 20 6e 65 63  g[] array as nec
221e7 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  essary..**.** Th
221e8 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c  is routine shoul
221e9 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64  d only be called
221ea 20 61 66 74 65 72 20 74 68 65 20 65 78 70 72 65   after the expre
221eb 73 73 69 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a  ssion has been.*
221ec 2a 20 61 6e 61 6c 79 7a 65 64 20 62 79 20 73 71  * analyzed by sq
221ed 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
221ee 4e 61 6d 65 73 28 29 2e 0a 2a 2f 0a 53 51 4c 49  Names()..*/.SQLI
221ef 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
221f0 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79  sqlite3ExprAnaly
221f1 7a 65 41 67 67 72 65 67 61 74 65 73 28 4e 61 6d  zeAggregates(Nam
221f2 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 45  eContext *pNC, E
221f3 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 57  xpr *pExpr){.  W
221f4 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 78 45 78  alker w;.  w.xEx
221f5 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 61 6e 61  prCallback = ana
221f6 6c 79 7a 65 41 67 67 72 65 67 61 74 65 3b 0a 20  lyzeAggregate;. 
221f7 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61   w.xSelectCallba
221f8 63 6b 20 3d 20 61 6e 61 6c 79 7a 65 41 67 67 72  ck = analyzeAggr
221f9 65 67 61 74 65 73 49 6e 53 65 6c 65 63 74 3b 0a  egatesInSelect;.
221fa 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4e 43 3b    w.u.pNC = pNC;
221fb 0a 20 20 61 73 73 65 72 74 28 20 70 4e 43 2d 3e  .  assert( pNC->
221fc 70 53 72 63 4c 69 73 74 21 3d 30 20 29 3b 0a 20  pSrcList!=0 );. 
221fd 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
221fe 28 26 77 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a  (&w, pExpr);.}..
221ff 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74  /*.** Call sqlit
22200 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
22201 72 65 67 61 74 65 73 28 29 20 66 6f 72 20 65 76  regates() for ev
22202 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69  ery expression i
22203 6e 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69  n an.** expressi
22204 6f 6e 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e  on list.  Return
22205 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
22206 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  rrors..**.** If 
22207 61 6e 20 65 72 72 6f 72 20 69 73 20 66 6f 75 6e  an error is foun
22208 64 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20  d, the analysis 
22209 69 73 20 63 75 74 20 73 68 6f 72 74 2e 0a 2a 2f  is cut short..*/
2220a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2220b 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
2220c 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 4e  AnalyzeAggList(N
2220d 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c  ameContext *pNC,
2220e 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
2220f 29 7b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72  ){.  struct Expr
22210 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
22211 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  ;.  int i;.  if(
22212 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 66 6f   pList ){.    fo
22213 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  r(pItem=pList->a
22214 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e  , i=0; i<pList->
22215 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65  nExpr; i++, pIte
22216 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  m++){.      sqli
22217 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67  te3ExprAnalyzeAg
22218 67 72 65 67 61 74 65 73 28 70 4e 43 2c 20 70 49  gregates(pNC, pI
22219 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20  tem->pExpr);.   
2221a 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
2221b 41 6c 6c 6f 63 61 74 65 20 61 20 73 69 6e 67 6c  Allocate a singl
2221c 65 20 6e 65 77 20 72 65 67 69 73 74 65 72 20 66  e new register f
2221d 6f 72 20 75 73 65 20 74 6f 20 68 6f 6c 64 20 73  or use to hold s
2221e 6f 6d 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65  ome intermediate
2221f 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a 53 51 4c 49   result..*/.SQLI
22220 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
22221 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
22222 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b  (Parse *pParse){
22223 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
22224 54 65 6d 70 52 65 67 3d 3d 30 20 29 7b 0a 20 20  TempReg==0 ){.  
22225 20 20 72 65 74 75 72 6e 20 2b 2b 70 50 61 72 73    return ++pPars
22226 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72  e->nMem;.  }.  r
22227 65 74 75 72 6e 20 70 50 61 72 73 65 2d 3e 61 54  eturn pParse->aT
22228 65 6d 70 52 65 67 5b 2d 2d 70 50 61 72 73 65 2d  empReg[--pParse-
22229 3e 6e 54 65 6d 70 52 65 67 5d 3b 0a 7d 0a 0a 2f  >nTempReg];.}../
2222a 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20  *.** Deallocate 
2222b 61 20 72 65 67 69 73 74 65 72 2c 20 6d 61 6b 69  a register, maki
2222c 6e 67 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72  ng available for
2222d 20 72 65 75 73 65 20 66 6f 72 20 73 6f 6d 65 20   reuse for some 
2222e 6f 74 68 65 72 0a 2a 2a 20 70 75 72 70 6f 73 65  other.** purpose
2222f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 72 65 67  ..**.** If a reg
22230 69 73 74 65 72 20 69 73 20 63 75 72 72 65 6e 74  ister is current
22231 6c 79 20 62 65 69 6e 67 20 75 73 65 64 20 62 79  ly being used by
22232 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68   the column cach
22233 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 64  e, then.** the d
22234 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 64 65  allocation is de
22235 66 65 72 72 65 64 20 75 6e 74 69 6c 20 74 68 65  ferred until the
22236 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 6c 69   column cache li
22237 6e 65 20 74 68 61 74 20 75 73 65 73 0a 2a 2a 20  ne that uses.** 
22238 74 68 65 20 72 65 67 69 73 74 65 72 20 62 65 63  the register bec
22239 6f 6d 65 73 20 73 74 61 6c 65 2e 0a 2a 2f 0a 53  omes stale..*/.S
2223a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2223b 69 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  id sqlite3Releas
2223c 65 54 65 6d 70 52 65 67 28 50 61 72 73 65 20 2a  eTempReg(Parse *
2223d 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67  pParse, int iReg
2223e 29 7b 0a 20 20 69 66 28 20 69 52 65 67 20 26 26  ){.  if( iReg &&
2223f 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65   pParse->nTempRe
22240 67 3c 41 72 72 61 79 53 69 7a 65 28 70 50 61 72  g<ArraySize(pPar
22241 73 65 2d 3e 61 54 65 6d 70 52 65 67 29 20 29 7b  se->aTempReg) ){
22242 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
22243 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65  struct yColCache
22244 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30   *p;.    for(i=0
22245 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c  , p=pParse->aCol
22246 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f  Cache; i<SQLITE_
22247 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c  N_COLCACHE; i++,
22248 20 70 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   p++){.      if(
22249 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20 29   p->iReg==iReg )
2224a 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 74 65 6d  {.        p->tem
2224b 70 52 65 67 20 3d 20 31 3b 0a 20 20 20 20 20 20  pReg = 1;.      
2224c 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
2224d 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72  }.    }.    pPar
2224e 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b 70 50 61  se->aTempReg[pPa
2224f 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 2b 2b 5d  rse->nTempReg++]
22250 20 3d 20 69 52 65 67 3b 0a 20 20 7d 0a 7d 0a 0a   = iReg;.  }.}..
22251 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6f  /*.** Allocate o
22252 72 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 20 62  r deallocate a b
22253 6c 6f 63 6b 20 6f 66 20 6e 52 65 67 20 63 6f 6e  lock of nReg con
22254 73 65 63 75 74 69 76 65 20 72 65 67 69 73 74 65  secutive registe
22255 72 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  rs.*/.SQLITE_PRI
22256 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
22257 47 65 74 54 65 6d 70 52 61 6e 67 65 28 50 61 72  GetTempRange(Par
22258 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
22259 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 2c 20  nReg){.  int i, 
2225a 6e 3b 0a 20 20 69 20 3d 20 70 50 61 72 73 65 2d  n;.  i = pParse-
2225b 3e 69 52 61 6e 67 65 52 65 67 3b 0a 20 20 6e 20  >iRangeReg;.  n 
2225c 3d 20 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65  = pParse->nRange
2225d 52 65 67 3b 0a 20 20 69 66 28 20 6e 52 65 67 3c  Reg;.  if( nReg<
2225e 3d 6e 20 26 26 20 21 75 73 65 64 41 73 43 6f 6c  =n && !usedAsCol
2225f 75 6d 6e 43 61 63 68 65 28 70 50 61 72 73 65 2c  umnCache(pParse,
22260 20 69 2c 20 69 2b 6e 2d 31 29 20 29 7b 0a 20 20   i, i+n-1) ){.  
22261 20 20 70 50 61 72 73 65 2d 3e 69 52 61 6e 67 65    pParse->iRange
22262 52 65 67 20 2b 3d 20 6e 52 65 67 3b 0a 20 20 20  Reg += nReg;.   
22263 20 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52   pParse->nRangeR
22264 65 67 20 2d 3d 20 6e 52 65 67 3b 0a 20 20 7d 65  eg -= nReg;.  }e
22265 6c 73 65 7b 0a 20 20 20 20 69 20 3d 20 70 50 61  lse{.    i = pPa
22266 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 20  rse->nMem+1;.   
22267 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d   pParse->nMem +=
22268 20 6e 52 65 67 3b 0a 20 20 7d 0a 20 20 72 65 74   nReg;.  }.  ret
22269 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c 49 54 45 5f  urn i;.}.SQLITE_
2226a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2226b 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
2226c 61 6e 67 65 28 50 61 72 73 65 20 2a 70 50 61 72  ange(Parse *pPar
2226d 73 65 2c 20 69 6e 74 20 69 52 65 67 2c 20 69 6e  se, int iReg, in
2226e 74 20 6e 52 65 67 29 7b 0a 20 20 69 66 28 20 6e  t nReg){.  if( n
2226f 52 65 67 3e 70 50 61 72 73 65 2d 3e 6e 52 61 6e  Reg>pParse->nRan
22270 67 65 52 65 67 20 29 7b 0a 20 20 20 20 70 50 61  geReg ){.    pPa
22271 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67 20 3d  rse->nRangeReg =
22272 20 6e 52 65 67 3b 0a 20 20 20 20 70 50 61 72 73   nReg;.    pPars
22273 65 2d 3e 69 52 61 6e 67 65 52 65 67 20 3d 20 69  e->iRangeReg = i
22274 52 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a  Reg;.  }.}../***
22275 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
22276 6f 66 20 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a  of expr.c ******
22277 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22278 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22279 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2227a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
2227b 6e 20 66 69 6c 65 20 61 6c 74 65 72 2e 63 20 2a  n file alter.c *
2227c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2227d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2227e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
2227f 2a 20 32 30 30 35 20 46 65 62 72 75 61 72 79 20  * 2005 February 
22280 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
22281 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
22282 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
22283 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
22284 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
22285 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
22286 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
22287 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
22288 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
22289 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
2228a 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
2228b 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
2228c 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
2228d 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
2228e 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
2228f 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
22290 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
22291 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
22292 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22293 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22294 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22295 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22296 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
22297 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72  ontains C code r
22298 6f 75 74 69 6e 65 73 20 74 68 61 74 20 75 73 65  outines that use
22299 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 56 44  d to generate VD
2229a 42 45 20 63 6f 64 65 0a 2a 2a 20 74 68 61 74 20  BE code.** that 
2229b 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 41  implements the A
2229c 4c 54 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61  LTER TABLE comma
2229d 6e 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 61  nd..**.** $Id: a
2229e 6c 74 65 72 2e 63 2c 76 20 31 2e 36 32 20 32 30  lter.c,v 1.62 20
2229f 30 39 2f 30 37 2f 32 34 20 31 37 3a 35 38 3a 35  09/07/24 17:58:5
222a0 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  3 danielk1977 Ex
222a1 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  p $.*/../*.** Th
222a2 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  e code in this f
222a3 69 6c 65 20 6f 6e 6c 79 20 65 78 69 73 74 73 20  ile only exists 
222a4 69 66 20 77 65 20 61 72 65 20 6e 6f 74 20 6f 6d  if we are not om
222a5 69 74 74 69 6e 67 20 74 68 65 0a 2a 2a 20 41 4c  itting the.** AL
222a6 54 45 52 20 54 41 42 4c 45 20 6c 6f 67 69 63 20  TER TABLE logic 
222a7 66 72 6f 6d 20 74 68 65 20 62 75 69 6c 64 2e 0a  from the build..
222a8 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
222a9 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c  E_OMIT_ALTERTABL
222aa 45 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  E.../*.** This f
222ab 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
222ac 62 79 20 53 51 4c 20 67 65 6e 65 72 61 74 65 64  by SQL generated
222ad 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
222ae 65 20 0a 2a 2a 20 41 4c 54 45 52 20 54 41 42 4c  e .** ALTER TABL
222af 45 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 66  E command. The f
222b0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73  irst argument is
222b1 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 43   the text of a C
222b2 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72 0a 2a  REATE TABLE or.*
222b3 2a 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 63  * CREATE INDEX c
222b4 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 73 65 63 6f  ommand. The seco
222b5 6e 64 20 69 73 20 61 20 74 61 62 6c 65 20 6e 61  nd is a table na
222b6 6d 65 2e 20 54 68 65 20 74 61 62 6c 65 20 6e 61  me. The table na
222b7 6d 65 20 69 6e 20 0a 2a 2a 20 74 68 65 20 43 52  me in .** the CR
222b8 45 41 54 45 20 54 41 42 4c 45 20 6f 72 20 43 52  EATE TABLE or CR
222b9 45 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 65  EATE INDEX state
222ba 6d 65 6e 74 20 69 73 20 72 65 70 6c 61 63 65 64  ment is replaced
222bb 20 77 69 74 68 20 74 68 65 20 74 68 69 72 64 0a   with the third.
222bc 2a 2a 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20  ** argument and 
222bd 74 68 65 20 72 65 73 75 6c 74 20 72 65 74 75 72  the result retur
222be 6e 65 64 2e 20 45 78 61 6d 70 6c 65 73 3a 0a 2a  ned. Examples:.*
222bf 2a 0a 2a 2a 20 73 71 6c 69 74 65 5f 72 65 6e 61  *.** sqlite_rena
222c0 6d 65 5f 74 61 62 6c 65 28 27 43 52 45 41 54 45  me_table('CREATE
222c1 20 54 41 42 4c 45 20 61 62 63 28 61 2c 20 62 2c   TABLE abc(a, b,
222c2 20 63 29 27 2c 20 27 64 65 66 27 29 0a 2a 2a 20   c)', 'def').** 
222c3 20 20 20 20 2d 3e 20 27 43 52 45 41 54 45 20 54      -> 'CREATE T
222c4 41 42 4c 45 20 64 65 66 28 61 2c 20 62 2c 20 63  ABLE def(a, b, c
222c5 29 27 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 5f  )'.**.** sqlite_
222c6 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 27 43 52  rename_table('CR
222c7 45 41 54 45 20 49 4e 44 45 58 20 69 20 4f 4e 20  EATE INDEX i ON 
222c8 61 62 63 28 61 29 27 2c 20 27 64 65 66 27 29 0a  abc(a)', 'def').
222c9 2a 2a 20 20 20 20 20 2d 3e 20 27 43 52 45 41 54  **     -> 'CREAT
222ca 45 20 49 4e 44 45 58 20 69 20 4f 4e 20 64 65 66  E INDEX i ON def
222cb 28 61 2c 20 62 2c 20 63 29 27 0a 2a 2f 0a 73 74  (a, b, c)'.*/.st
222cc 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d 65  atic void rename
222cd 54 61 62 6c 65 46 75 6e 63 28 0a 20 20 73 71 6c  TableFunc(.  sql
222ce 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
222cf 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74  ntext,.  int Not
222d0 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  Used,.  sqlite3_
222d1 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
222d2 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
222d3 63 6f 6e 73 74 20 2a 7a 53 71 6c 20 3d 20 73 71  const *zSql = sq
222d4 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
222d5 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 75 6e 73  (argv[0]);.  uns
222d6 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74  igned char const
222d7 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20 3d 20 73   *zTableName = s
222d8 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
222d9 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a 20 20 69  t(argv[1]);..  i
222da 6e 74 20 74 6f 6b 65 6e 3b 0a 20 20 54 6f 6b 65  nt token;.  Toke
222db 6e 20 74 6e 61 6d 65 3b 0a 20 20 75 6e 73 69 67  n tname;.  unsig
222dc 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ned char const *
222dd 7a 43 73 72 20 3d 20 7a 53 71 6c 3b 0a 20 20 69  zCsr = zSql;.  i
222de 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 63 68  nt len = 0;.  ch
222df 61 72 20 2a 7a 52 65 74 3b 0a 0a 20 20 73 71 6c  ar *zRet;..  sql
222e0 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74  ite3 *db = sqlit
222e1 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
222e2 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a  ndle(context);..
222e3 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
222e4 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20  ER(NotUsed);..  
222e5 2f 2a 20 54 68 65 20 70 72 69 6e 63 69 70 6c 65  /* The principle
222e6 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20   used to locate 
222e7 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69  the table name i
222e8 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42  n the CREATE TAB
222e9 4c 45 20 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65  LE .  ** stateme
222ea 6e 74 20 69 73 20 74 68 61 74 20 74 68 65 20 74  nt is that the t
222eb 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 74 68 65  able name is the
222ec 20 66 69 72 73 74 20 6e 6f 6e 2d 73 70 61 63 65   first non-space
222ed 20 74 6f 6b 65 6e 20 74 68 61 74 0a 20 20 2a 2a   token that.  **
222ee 20 69 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20   is immediately 
222ef 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 54 4b  followed by a TK
222f0 5f 4c 50 20 6f 72 20 54 4b 5f 55 53 49 4e 47 20  _LP or TK_USING 
222f1 74 6f 6b 65 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66  token..  */.  if
222f2 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20 64 6f  ( zSql ){.    do
222f3 20 7b 0a 20 20 20 20 20 20 69 66 28 20 21 2a 7a   {.      if( !*z
222f4 43 73 72 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  Csr ){.        /
222f5 2a 20 52 61 6e 20 6f 75 74 20 6f 66 20 69 6e 70  * Ran out of inp
222f6 75 74 20 62 65 66 6f 72 65 20 66 69 6e 64 69 6e  ut before findin
222f7 67 20 61 6e 20 6f 70 65 6e 69 6e 67 20 62 72 61  g an opening bra
222f8 63 6b 65 74 2e 20 52 65 74 75 72 6e 20 4e 55 4c  cket. Return NUL
222f9 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65  L. */.        re
222fa 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  turn;.      }.. 
222fb 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68       /* Store th
222fc 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 7a 43 73  e token that zCs
222fd 72 20 70 6f 69 6e 74 73 20 74 6f 20 69 6e 20 74  r points to in t
222fe 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 74  name. */.      t
222ff 6e 61 6d 65 2e 7a 20 3d 20 28 63 68 61 72 2a 29  name.z = (char*)
22300 7a 43 73 72 3b 0a 20 20 20 20 20 20 74 6e 61 6d  zCsr;.      tnam
22301 65 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a 20 20 20 20  e.n = len;..    
22302 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 7a 43 73    /* Advance zCs
22303 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74 6f  r to the next to
22304 6b 65 6e 2e 20 53 74 6f 72 65 20 74 68 61 74 20  ken. Store that 
22305 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 27 74  token type in 't
22306 6f 6b 65 6e 27 2c 0a 20 20 20 20 20 20 2a 2a 20  oken',.      ** 
22307 61 6e 64 20 69 74 73 20 6c 65 6e 67 74 68 20 69  and its length i
22308 6e 20 27 6c 65 6e 27 20 28 74 6f 20 62 65 20 75  n 'len' (to be u
22309 73 65 64 20 6e 65 78 74 20 69 74 65 72 61 74 69  sed next iterati
2230a 6f 6e 20 6f 66 20 74 68 69 73 20 6c 6f 6f 70 29  on of this loop)
2230b 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
2230c 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 7a 43   do {.        zC
2230d 73 72 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20  sr += len;.     
2230e 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33     len = sqlite3
2230f 47 65 74 54 6f 6b 65 6e 28 7a 43 73 72 2c 20 26  GetToken(zCsr, &
22310 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 20  token);.      } 
22311 77 68 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b  while( token==TK
22312 5f 53 50 41 43 45 20 29 3b 0a 20 20 20 20 20 20  _SPACE );.      
22313 61 73 73 65 72 74 28 20 6c 65 6e 3e 30 20 29 3b  assert( len>0 );
22314 0a 20 20 20 20 7d 20 77 68 69 6c 65 28 20 74 6f  .    } while( to
22315 6b 65 6e 21 3d 54 4b 5f 4c 50 20 26 26 20 74 6f  ken!=TK_LP && to
22316 6b 65 6e 21 3d 54 4b 5f 55 53 49 4e 47 20 29 3b  ken!=TK_USING );
22317 0a 0a 20 20 20 20 7a 52 65 74 20 3d 20 73 71 6c  ..    zRet = sql
22318 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
22319 22 25 2e 2a 73 5c 22 25 77 5c 22 25 73 22 2c 20  "%.*s\"%w\"%s", 
2231a 28 28 75 38 2a 29 74 6e 61 6d 65 2e 7a 29 20 2d  ((u8*)tname.z) -
2231b 20 7a 53 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20   zSql, zSql, .  
2231c 20 20 20 20 20 7a 54 61 62 6c 65 4e 61 6d 65 2c       zTableName,
2231d 20 74 6e 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e   tname.z+tname.n
2231e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
2231f 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
22320 78 74 2c 20 7a 52 65 74 2c 20 2d 31 2c 20 53 51  xt, zRet, -1, SQ
22321 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20  LITE_DYNAMIC);. 
22322 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
22323 20 43 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c   C function impl
22324 65 6d 65 6e 74 73 20 61 6e 20 53 51 4c 20 75 73  ements an SQL us
22325 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74  er function that
22326 20 69 73 20 75 73 65 64 20 62 79 20 53 51 4c 20   is used by SQL 
22327 63 6f 64 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65  code.** generate
22328 64 20 62 79 20 74 68 65 20 41 4c 54 45 52 20 54  d by the ALTER T
22329 41 42 4c 45 20 2e 2e 2e 20 52 45 4e 41 4d 45 20  ABLE ... RENAME 
2232a 63 6f 6d 6d 61 6e 64 20 74 6f 20 6d 6f 64 69 66  command to modif
2232b 79 20 74 68 65 20 64 65 66 69 6e 69 74 69 6f 6e  y the definition
2232c 0a 2a 2a 20 6f 66 20 61 6e 79 20 66 6f 72 65 69  .** of any forei
2232d 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
2232e 74 73 20 74 68 61 74 20 75 73 65 20 74 68 65 20  ts that use the 
2232f 74 61 62 6c 65 20 62 65 69 6e 67 20 72 65 6e 61  table being rena
22330 6d 65 64 20 61 73 20 74 68 65 20 0a 2a 2a 20 70  med as the .** p
22331 61 72 65 6e 74 20 74 61 62 6c 65 2e 20 49 74 20  arent table. It 
22332 69 73 20 70 61 73 73 65 64 20 74 68 72 65 65 20  is passed three 
22333 61 72 67 75 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a  arguments:.**.**
22334 20 20 20 31 29 20 54 68 65 20 63 6f 6d 70 6c 65     1) The comple
22335 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 43  te text of the C
22336 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
22337 65 6d 65 6e 74 20 62 65 69 6e 67 20 6d 6f 64 69  ement being modi
22338 66 69 65 64 2c 0a 2a 2a 20 20 20 32 29 20 54 68  fied,.**   2) Th
22339 65 20 6f 6c 64 20 6e 61 6d 65 20 6f 66 20 74 68  e old name of th
2233a 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72 65  e table being re
2233b 6e 61 6d 65 64 2c 20 61 6e 64 0a 2a 2a 20 20 20  named, and.**   
2233c 33 29 20 54 68 65 20 6e 65 77 20 6e 61 6d 65 20  3) The new name 
2233d 6f 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  of the table bei
2233e 6e 67 20 72 65 6e 61 6d 65 64 2e 0a 2a 2a 0a 2a  ng renamed..**.*
2233f 2a 20 49 74 20 72 65 74 75 72 6e 73 20 74 68 65  * It returns the
22340 20 6e 65 77 20 43 52 45 41 54 45 20 54 41 42 4c   new CREATE TABL
22341 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 46 6f 72  E statement. For
22342 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20   example:.**.** 
22343 20 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f    sqlite_rename_
22344 70 61 72 65 6e 74 28 27 43 52 45 41 54 45 20 54  parent('CREATE T
22345 41 42 4c 45 20 74 31 28 61 20 52 45 46 45 52 45  ABLE t1(a REFERE
22346 4e 43 45 53 20 74 32 29 27 2c 20 27 74 32 27 2c  NCES t2)', 't2',
22347 20 27 74 33 27 29 0a 2a 2a 20 20 20 20 20 20 20   't3').**       
22348 2d 3e 20 27 43 52 45 41 54 45 20 54 41 42 4c 45  -> 'CREATE TABLE
22349 20 74 31 28 61 20 52 45 46 45 52 45 4e 43 45 53   t1(a REFERENCES
2234a 20 74 33 29 27 0a 2a 2f 0a 23 69 66 6e 64 65 66   t3)'.*/.#ifndef
2234b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52   SQLITE_OMIT_FOR
2234c 45 49 47 4e 5f 4b 45 59 0a 73 74 61 74 69 63 20  EIGN_KEY.static 
2234d 76 6f 69 64 20 72 65 6e 61 6d 65 50 61 72 65 6e  void renameParen
2234e 74 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  tFunc(.  sqlite3
2234f 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
22350 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  t,.  int NotUsed
22351 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
22352 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 73 71  e **argv.){.  sq
22353 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69  lite3 *db = sqli
22354 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
22355 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a  andle(context);.
22356 20 20 63 68 61 72 20 2a 7a 4f 75 74 70 75 74 20    char *zOutput 
22357 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 52 65  = 0;.  char *zRe
22358 73 75 6c 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64  sult;.  unsigned
22359 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 49 6e   char const *zIn
2235a 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  put = sqlite3_va
2235b 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
2235c 29 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  );.  unsigned ch
2235d 61 72 20 63 6f 6e 73 74 20 2a 7a 4f 6c 64 20 3d  ar const *zOld =
2235e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
2235f 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  ext(argv[1]);.  
22360 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f  unsigned char co
22361 6e 73 74 20 2a 7a 4e 65 77 20 3d 20 73 71 6c 69  nst *zNew = sqli
22362 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
22363 72 67 76 5b 32 5d 29 3b 0a 0a 20 20 75 6e 73 69  rgv[2]);..  unsi
22364 67 6e 65 64 20 63 6f 6e 73 74 20 63 68 61 72 20  gned const char 
22365 2a 7a 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50  *z;         /* P
22366 6f 69 6e 74 65 72 20 74 6f 20 74 6f 6b 65 6e 20  ointer to token 
22367 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20  */.  int n;     
22368 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22369 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
2236a 66 20 74 6f 6b 65 6e 20 7a 20 2a 2f 0a 20 20 69  f token z */.  i
2236b 6e 74 20 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20  nt token;       
2236c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2236d 2a 20 54 79 70 65 20 6f 66 20 74 6f 6b 65 6e 20  * Type of token 
2236e 2a 2f 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  */..  UNUSED_PAR
2236f 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
22370 0a 20 20 66 6f 72 28 7a 3d 7a 49 6e 70 75 74 3b  .  for(z=zInput;
22371 20 2a 7a 3b 20 7a 3d 7a 2b 6e 29 7b 0a 20 20 20   *z; z=z+n){.   
22372 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54   n = sqlite3GetT
22373 6f 6b 65 6e 28 7a 2c 20 26 74 6f 6b 65 6e 29 3b  oken(z, &token);
22374 0a 20 20 20 20 69 66 28 20 74 6f 6b 65 6e 3d 3d  .    if( token==
22375 54 4b 5f 52 45 46 45 52 45 4e 43 45 53 20 29 7b  TK_REFERENCES ){
22376 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 50 61  .      char *zPa
22377 72 65 6e 74 3b 0a 20 20 20 20 20 20 64 6f 20 7b  rent;.      do {
22378 0a 20 20 20 20 20 20 20 20 7a 20 2b 3d 20 6e 3b  .        z += n;
22379 0a 20 20 20 20 20 20 20 20 6e 20 3d 20 73 71 6c  .        n = sql
2237a 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 7a 2c 20  ite3GetToken(z, 
2237b 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d  &token);.      }
2237c 77 68 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b  while( token==TK
2237d 5f 53 50 41 43 45 20 29 3b 0a 0a 20 20 20 20 20  _SPACE );..     
2237e 20 7a 50 61 72 65 6e 74 20 3d 20 73 71 6c 69 74   zParent = sqlit
2237f 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20  e3DbStrNDup(db, 
22380 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 7a 2c  (const char *)z,
22381 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a   n);.      if( z
22382 50 61 72 65 6e 74 3d 3d 30 20 29 20 62 72 65 61  Parent==0 ) brea
22383 6b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  k;.      sqlite3
22384 44 65 71 75 6f 74 65 28 7a 50 61 72 65 6e 74 29  Dequote(zParent)
22385 3b 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73  ;.      if( 0==s
22386 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 28 63  qlite3StrICmp((c
22387 6f 6e 73 74 20 63 68 61 72 20 2a 29 7a 4f 6c 64  onst char *)zOld
22388 2c 20 7a 50 61 72 65 6e 74 29 20 29 7b 0a 20 20  , zParent) ){.  
22389 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4f 75 74        char *zOut
2238a 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
2238b 66 28 64 62 2c 20 22 25 73 25 2e 2a 73 5c 22 25  f(db, "%s%.*s\"%
2238c 77 5c 22 22 2c 20 0a 20 20 20 20 20 20 20 20 20  w\"", .         
2238d 20 20 20 28 7a 4f 75 74 70 75 74 3f 7a 4f 75 74     (zOutput?zOut
2238e 70 75 74 3a 22 22 29 2c 20 7a 2d 7a 49 6e 70 75  put:""), z-zInpu
2238f 74 2c 20 7a 49 6e 70 75 74 2c 20 28 63 6f 6e 73  t, zInput, (cons
22390 74 20 63 68 61 72 20 2a 29 7a 4e 65 77 0a 20 20  t char *)zNew.  
22391 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20        );.       
22392 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
22393 62 2c 20 7a 4f 75 74 70 75 74 29 3b 0a 20 20 20  b, zOutput);.   
22394 20 20 20 20 20 7a 4f 75 74 70 75 74 20 3d 20 7a       zOutput = z
22395 4f 75 74 3b 0a 20 20 20 20 20 20 20 20 7a 49 6e  Out;.        zIn
22396 70 75 74 20 3d 20 26 7a 5b 6e 5d 3b 0a 20 20 20  put = &z[n];.   
22397 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
22398 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 50 61  e3DbFree(db, zPa
22399 72 65 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  rent);.    }.  }
2239a 0a 0a 20 20 7a 52 65 73 75 6c 74 20 3d 20 73 71  ..  zResult = sq
2239b 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2239c 20 22 25 73 25 73 22 2c 20 28 7a 4f 75 74 70 75   "%s%s", (zOutpu
2239d 74 3f 7a 4f 75 74 70 75 74 3a 22 22 29 2c 20 7a  t?zOutput:""), z
2239e 49 6e 70 75 74 29 2c 20 0a 20 20 73 71 6c 69 74  Input), .  sqlit
2239f 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
223a0 6f 6e 74 65 78 74 2c 20 7a 52 65 73 75 6c 74 2c  ontext, zResult,
223a1 20 2d 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41   -1, SQLITE_DYNA
223a2 4d 49 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  MIC);.  sqlite3D
223a3 62 46 72 65 65 28 64 62 2c 20 7a 4f 75 74 70 75  bFree(db, zOutpu
223a4 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  t);.}.#endif..#i
223a5 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
223a6 54 5f 54 52 49 47 47 45 52 0a 2f 2a 20 54 68 69  T_TRIGGER./* Thi
223a7 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
223a8 65 64 20 62 79 20 53 51 4c 20 67 65 6e 65 72 61  ed by SQL genera
223a9 74 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ted to implement
223aa 20 74 68 65 0a 2a 2a 20 41 4c 54 45 52 20 54 41   the.** ALTER TA
223ab 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65  BLE command. The
223ac 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
223ad 69 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 61  is the text of a
223ae 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20   CREATE TRIGGER 
223af 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 54  .** statement. T
223b0 68 65 20 73 65 63 6f 6e 64 20 69 73 20 61 20 74  he second is a t
223b1 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20 74  able name. The t
223b2 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65  able name in the
223b3 20 43 52 45 41 54 45 20 0a 2a 2a 20 54 52 49 47   CREATE .** TRIG
223b4 47 45 52 20 73 74 61 74 65 6d 65 6e 74 20 69 73  GER statement is
223b5 20 72 65 70 6c 61 63 65 64 20 77 69 74 68 20 74   replaced with t
223b6 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
223b7 74 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74  t and the result
223b8 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 54   .** returned. T
223b9 68 69 73 20 69 73 20 61 6e 61 6c 61 67 6f 75 73  his is analagous
223ba 20 74 6f 20 72 65 6e 61 6d 65 54 61 62 6c 65 46   to renameTableF
223bb 75 6e 63 28 29 20 61 62 6f 76 65 2c 20 65 78 63  unc() above, exc
223bc 65 70 74 20 66 6f 72 20 43 52 45 41 54 45 0a 2a  ept for CREATE.*
223bd 2a 20 54 52 49 47 47 45 52 2c 20 6e 6f 74 20 43  * TRIGGER, not C
223be 52 45 41 54 45 20 49 4e 44 45 58 20 61 6e 64 20  REATE INDEX and 
223bf 43 52 45 41 54 45 20 54 41 42 4c 45 2e 0a 2a 2f  CREATE TABLE..*/
223c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6e  .static void ren
223c1 61 6d 65 54 72 69 67 67 65 72 46 75 6e 63 28 0a  ameTriggerFunc(.
223c2 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
223c3 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
223c4 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c  t NotUsed,.  sql
223c5 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
223c6 76 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  v.){.  unsigned 
223c7 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71 6c  char const *zSql
223c8 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
223c9 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
223ca 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
223cb 63 6f 6e 73 74 20 2a 7a 54 61 62 6c 65 4e 61 6d  const *zTableNam
223cc 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
223cd 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b  e_text(argv[1]);
223ce 0a 0a 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 0a 20  ..  int token;. 
223cf 20 54 6f 6b 65 6e 20 74 6e 61 6d 65 3b 0a 20 20   Token tname;.  
223d0 69 6e 74 20 64 69 73 74 20 3d 20 33 3b 0a 20 20  int dist = 3;.  
223d1 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f  unsigned char co
223d2 6e 73 74 20 2a 7a 43 73 72 20 3d 20 7a 53 71 6c  nst *zCsr = zSql
223d3 3b 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b  ;.  int len = 0;
223d4 0a 20 20 63 68 61 72 20 2a 7a 52 65 74 3b 0a 20  .  char *zRet;. 
223d5 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
223d6 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
223d7 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
223d8 29 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  );..  UNUSED_PAR
223d9 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
223da 0a 0a 20 20 2f 2a 20 54 68 65 20 70 72 69 6e 63  ..  /* The princ
223db 69 70 6c 65 20 75 73 65 64 20 74 6f 20 6c 6f 63  iple used to loc
223dc 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61  ate the table na
223dd 6d 65 20 69 6e 20 74 68 65 20 43 52 45 41 54 45  me in the CREATE
223de 20 54 52 49 47 47 45 52 20 0a 20 20 2a 2a 20 73   TRIGGER .  ** s
223df 74 61 74 65 6d 65 6e 74 20 69 73 20 74 68 61 74  tatement is that
223e0 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
223e1 69 73 20 74 68 65 20 66 69 72 73 74 20 74 6f 6b  is the first tok
223e2 65 6e 20 74 68 61 74 20 69 73 20 69 6d 6d 65 64  en that is immed
223e3 69 61 74 65 64 6c 79 0a 20 20 2a 2a 20 70 72 65  iatedly.  ** pre
223e4 63 65 64 65 64 20 62 79 20 65 69 74 68 65 72 20  ceded by either 
223e5 54 4b 5f 4f 4e 20 6f 72 20 54 4b 5f 44 4f 54 20  TK_ON or TK_DOT 
223e6 61 6e 64 20 69 6d 6d 65 64 69 61 74 65 64 6c 79  and immediatedly
223e7 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6f 6e 65   followed by one
223e8 0a 20 20 2a 2a 20 6f 66 20 54 4b 5f 57 48 45 4e  .  ** of TK_WHEN
223e9 2c 20 54 4b 5f 42 45 47 49 4e 20 6f 72 20 54 4b  , TK_BEGIN or TK
223ea 5f 46 4f 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  _FOR..  */.  if(
223eb 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20 64 6f 20   zSql ){.    do 
223ec 7b 0a 0a 20 20 20 20 20 20 69 66 28 20 21 2a 7a  {..      if( !*z
223ed 43 73 72 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  Csr ){.        /
223ee 2a 20 52 61 6e 20 6f 75 74 20 6f 66 20 69 6e 70  * Ran out of inp
223ef 75 74 20 62 65 66 6f 72 65 20 66 69 6e 64 69 6e  ut before findin
223f0 67 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  g the table name
223f1 2e 20 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a  . Return NULL. *
223f2 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  /.        return
223f3 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
223f4 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f   /* Store the to
223f5 6b 65 6e 20 74 68 61 74 20 7a 43 73 72 20 70 6f  ken that zCsr po
223f6 69 6e 74 73 20 74 6f 20 69 6e 20 74 6e 61 6d 65  ints to in tname
223f7 2e 20 2a 2f 0a 20 20 20 20 20 20 74 6e 61 6d 65  . */.      tname
223f8 2e 7a 20 3d 20 28 63 68 61 72 2a 29 7a 43 73 72  .z = (char*)zCsr
223f9 3b 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 6e 20  ;.      tname.n 
223fa 3d 20 6c 65 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a  = len;..      /*
223fb 20 41 64 76 61 6e 63 65 20 7a 43 73 72 20 74 6f   Advance zCsr to
223fc 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 2e   the next token.
223fd 20 53 74 6f 72 65 20 74 68 61 74 20 74 6f 6b 65   Store that toke
223fe 6e 20 74 79 70 65 20 69 6e 20 27 74 6f 6b 65 6e  n type in 'token
223ff 27 2c 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20  ',.      ** and 
22400 69 74 73 20 6c 65 6e 67 74 68 20 69 6e 20 27 6c  its length in 'l
22401 65 6e 27 20 28 74 6f 20 62 65 20 75 73 65 64 20  en' (to be used 
22402 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f  next iteration o
22403 66 20 74 68 69 73 20 6c 6f 6f 70 29 2e 0a 20 20  f this loop)..  
22404 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20      */.      do 
22405 7b 0a 20 20 20 20 20 20 20 20 7a 43 73 72 20 2b  {.        zCsr +
22406 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6c  = len;.        l
22407 65 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  en = sqlite3GetT
22408 6f 6b 65 6e 28 7a 43 73 72 2c 20 26 74 6f 6b 65  oken(zCsr, &toke
22409 6e 29 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65  n);.      }while
2240a 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 53 50 41 43  ( token==TK_SPAC
2240b 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  E );.      asser
2240c 74 28 20 6c 65 6e 3e 30 20 29 3b 0a 0a 20 20 20  t( len>0 );..   
2240d 20 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 27     /* Variable '
2240e 64 69 73 74 27 20 73 74 6f 72 65 73 20 74 68 65  dist' stores the
2240f 20 6e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e   number of token
22410 73 20 72 65 61 64 20 73 69 6e 63 65 20 74 68 65  s read since the
22411 20 6d 6f 73 74 0a 20 20 20 20 20 20 2a 2a 20 72   most.      ** r
22412 65 63 65 6e 74 20 54 4b 5f 44 4f 54 20 6f 72 20  ecent TK_DOT or 
22413 54 4b 5f 4f 4e 2e 20 54 68 69 73 20 6d 65 61 6e  TK_ON. This mean
22414 73 20 74 68 61 74 20 77 68 65 6e 20 61 20 57 48  s that when a WH
22415 45 4e 2c 20 46 4f 52 20 6f 72 20 42 45 47 49 4e  EN, FOR or BEGIN
22416 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65 6e   .      ** token
22417 20 69 73 20 72 65 61 64 20 61 6e 64 20 27 64 69   is read and 'di
22418 73 74 27 20 65 71 75 61 6c 73 20 32 2c 20 74 68  st' equals 2, th
22419 65 20 63 6f 6e 64 69 74 69 6f 6e 20 73 74 61 74  e condition stat
2241a 65 64 20 61 62 6f 76 65 0a 20 20 20 20 20 20 2a  ed above.      *
2241b 2a 20 74 6f 20 62 65 20 6d 65 74 2e 0a 20 20 20  * to be met..   
2241c 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e     **.      ** N
2241d 6f 74 65 20 74 68 61 74 20 4f 4e 20 63 61 6e 6e  ote that ON cann
2241e 6f 74 20 62 65 20 61 20 64 61 74 61 62 61 73 65  ot be a database
2241f 2c 20 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d  , table or colum
22420 6e 20 6e 61 6d 65 2c 20 73 6f 0a 20 20 20 20 20  n name, so.     
22421 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20   ** there is no 
22422 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 20 61 62  need to worry ab
22423 6f 75 74 20 73 79 6e 74 61 78 20 6c 69 6b 65 20  out syntax like 
22424 0a 20 20 20 20 20 20 2a 2a 20 22 43 52 45 41 54  .      ** "CREAT
22425 45 20 54 52 49 47 47 45 52 20 2e 2e 2e 20 4f 4e  E TRIGGER ... ON
22426 20 4f 4e 2e 4f 4e 20 42 45 47 49 4e 20 2e 2e 2e   ON.ON BEGIN ...
22427 22 20 65 74 63 2e 0a 20 20 20 20 20 20 2a 2f 0a  " etc..      */.
22428 20 20 20 20 20 20 64 69 73 74 2b 2b 3b 0a 20 20        dist++;.  
22429 20 20 20 20 69 66 28 20 74 6f 6b 65 6e 3d 3d 54      if( token==T
2242a 4b 5f 44 4f 54 20 7c 7c 20 74 6f 6b 65 6e 3d 3d  K_DOT || token==
2242b 54 4b 5f 4f 4e 20 29 7b 0a 20 20 20 20 20 20 20  TK_ON ){.       
2242c 20 64 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 20   dist = 0;.     
2242d 20 7d 0a 20 20 20 20 7d 20 77 68 69 6c 65 28 20   }.    } while( 
2242e 64 69 73 74 21 3d 32 20 7c 7c 20 28 74 6f 6b 65  dist!=2 || (toke
2242f 6e 21 3d 54 4b 5f 57 48 45 4e 20 26 26 20 74 6f  n!=TK_WHEN && to
22430 6b 65 6e 21 3d 54 4b 5f 46 4f 52 20 26 26 20 74  ken!=TK_FOR && t
22431 6f 6b 65 6e 21 3d 54 4b 5f 42 45 47 49 4e 29 20  oken!=TK_BEGIN) 
22432 29 3b 0a 0a 20 20 20 20 2f 2a 20 56 61 72 69 61  );..    /* Varia
22433 62 6c 65 20 74 6e 61 6d 65 20 6e 6f 77 20 63 6f  ble tname now co
22434 6e 74 61 69 6e 73 20 74 68 65 20 74 6f 6b 65 6e  ntains the token
22435 20 74 68 61 74 20 69 73 20 74 68 65 20 6f 6c 64   that is the old
22436 20 74 61 62 6c 65 2d 6e 61 6d 65 0a 20 20 20 20   table-name.    
22437 2a 2a 20 69 6e 20 74 68 65 20 43 52 45 41 54 45  ** in the CREATE
22438 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65   TRIGGER stateme
22439 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a  nt..    */.    z
2243a 52 65 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Ret = sqlite3MPr
2243b 69 6e 74 66 28 64 62 2c 20 22 25 2e 2a 73 5c 22  intf(db, "%.*s\"
2243c 25 77 5c 22 25 73 22 2c 20 28 28 75 38 2a 29 74  %w\"%s", ((u8*)t
2243d 6e 61 6d 65 2e 7a 29 20 2d 20 7a 53 71 6c 2c 20  name.z) - zSql, 
2243e 7a 53 71 6c 2c 20 0a 20 20 20 20 20 20 20 7a 54  zSql, .       zT
2243f 61 62 6c 65 4e 61 6d 65 2c 20 74 6e 61 6d 65 2e  ableName, tname.
22440 7a 2b 74 6e 61 6d 65 2e 6e 29 3b 0a 20 20 20 20  z+tname.n);.    
22441 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
22442 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65  ext(context, zRe
22443 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 44 59  t, -1, SQLITE_DY
22444 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65  NAMIC);.  }.}.#e
22445 6e 64 69 66 20 20 20 2f 2a 20 21 53 51 4c 49 54  ndif   /* !SQLIT
22446 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a  E_OMIT_TRIGGER *
22447 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65  /../*.** Registe
22448 72 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74  r built-in funct
22449 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 68 65 6c  ions used to hel
2244a 70 20 69 6d 70 6c 65 6d 65 6e 74 20 41 4c 54 45  p implement ALTE
2244b 52 20 54 41 42 4c 45 0a 2a 2f 0a 53 51 4c 49 54  R TABLE.*/.SQLIT
2244c 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2244d 71 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63 74  qlite3AlterFunct
2244e 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62  ions(sqlite3 *db
2244f 29 7b 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61  ){.  sqlite3Crea
22450 74 65 46 75 6e 63 28 64 62 2c 20 22 73 71 6c 69  teFunc(db, "sqli
22451 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 22  te_rename_table"
22452 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  , 2, SQLITE_UTF8
22453 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
22454 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
22455 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63 2c 20 30  nameTableFunc, 0
22456 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  , 0);.#ifndef SQ
22457 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
22458 52 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74  R.  sqlite3Creat
22459 65 46 75 6e 63 28 64 62 2c 20 22 73 71 6c 69 74  eFunc(db, "sqlit
2245a 65 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67 65 72  e_rename_trigger
2245b 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46  ", 2, SQLITE_UTF
2245c 38 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  8, 0,.          
2245d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
2245e 65 6e 61 6d 65 54 72 69 67 67 65 72 46 75 6e 63  enameTriggerFunc
2245f 2c 20 30 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a  , 0, 0);.#endif.
22460 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
22461 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a  MIT_FOREIGN_KEY.
22462 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46    sqlite3CreateF
22463 75 6e 63 28 64 62 2c 20 22 73 71 6c 69 74 65 5f  unc(db, "sqlite_
22464 72 65 6e 61 6d 65 5f 70 61 72 65 6e 74 22 2c 20  rename_parent", 
22465 33 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  3, SQLITE_UTF8, 
22466 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
22467 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6e 61              rena
22468 6d 65 50 61 72 65 6e 74 46 75 6e 63 2c 20 30 2c  meParentFunc, 0,
22469 20 30 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f   0);.#endif.}../
2246a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
2246b 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 63 72  on is used to cr
2246c 65 61 74 65 20 74 68 65 20 74 65 78 74 20 6f 66  eate the text of
2246d 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20   expressions of 
2246e 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20  the form:.**.** 
2246f 20 20 6e 61 6d 65 3d 3c 63 6f 6e 73 74 61 6e 74    name=<constant
22470 31 3e 20 4f 52 20 6e 61 6d 65 3d 3c 63 6f 6e 73  1> OR name=<cons
22471 74 61 6e 74 32 3e 20 4f 52 20 2e 2e 2e 0a 2a 2a  tant2> OR ....**
22472 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20  .** If argument 
22473 7a 57 68 65 72 65 20 69 73 20 4e 55 4c 4c 2c 20  zWhere is NULL, 
22474 74 68 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 73  then a pointer s
22475 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67  tring containing
22476 20 74 68 65 20 74 65 78 74 20 0a 2a 2a 20 22 6e   the text .** "n
22477 61 6d 65 3d 3c 63 6f 6e 73 74 61 6e 74 3e 22 20  ame=<constant>" 
22478 69 73 20 72 65 74 75 72 6e 65 64 2c 20 77 68 65  is returned, whe
22479 72 65 20 3c 63 6f 6e 73 74 61 6e 74 3e 20 69 73  re <constant> is
2247a 20 74 68 65 20 71 75 6f 74 65 64 20 76 65 72 73   the quoted vers
2247b 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 73 74  ion.** of the st
2247c 72 69 6e 67 20 70 61 73 73 65 64 20 61 73 20 61  ring passed as a
2247d 72 67 75 6d 65 6e 74 20 7a 43 6f 6e 73 74 61 6e  rgument zConstan
2247e 74 2e 20 54 68 65 20 72 65 74 75 72 6e 65 64 20  t. The returned 
2247f 62 75 66 66 65 72 20 69 73 0a 2a 2a 20 61 6c 6c  buffer is.** all
22480 6f 63 61 74 65 64 20 75 73 69 6e 67 20 73 71 6c  ocated using sql
22481 69 74 65 33 44 62 4d 61 6c 6c 6f 63 28 29 2e 20  ite3DbMalloc(). 
22482 49 74 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e  It is the respon
22483 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 0a  sibility of the.
22484 2a 2a 20 63 61 6c 6c 65 72 20 74 6f 20 65 6e 73  ** caller to ens
22485 75 72 65 20 74 68 61 74 20 69 74 20 69 73 20 65  ure that it is e
22486 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 2e  ventually freed.
22487 0a 2a 2a 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65  .**.** If argume
22488 6e 74 20 7a 57 68 65 72 65 20 69 73 20 6e 6f 74  nt zWhere is not
22489 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20   NULL, then the 
2248a 73 74 72 69 6e 67 20 72 65 74 75 72 6e 65 64 20  string returned 
2248b 69 73 20 0a 2a 2a 20 22 3c 77 68 65 72 65 3e 20  is .** "<where> 
2248c 4f 52 20 6e 61 6d 65 3d 3c 63 6f 6e 73 74 61 6e  OR name=<constan
2248d 74 3e 22 2c 20 77 68 65 72 65 20 3c 77 68 65 72  t>", where <wher
2248e 65 3e 20 69 73 20 74 68 65 20 63 6f 6e 74 65 6e  e> is the conten
2248f 74 73 20 6f 66 20 7a 57 68 65 72 65 2e 0a 2a 2a  ts of zWhere..**
22490 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 7a 57   In this case zW
22491 68 65 72 65 20 69 73 20 70 61 73 73 65 64 20 74  here is passed t
22492 6f 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28  o sqlite3DbFree(
22493 29 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  ) before returni
22494 6e 67 2e 0a 2a 2a 20 0a 2a 2f 0a 73 74 61 74 69  ng..** .*/.stati
22495 63 20 63 68 61 72 20 2a 77 68 65 72 65 4f 72 4e  c char *whereOrN
22496 61 6d 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ame(sqlite3 *db,
22497 20 63 68 61 72 20 2a 7a 57 68 65 72 65 2c 20 63   char *zWhere, c
22498 68 61 72 20 2a 7a 43 6f 6e 73 74 61 6e 74 29 7b  har *zConstant){
22499 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20  .  char *zNew;. 
2249a 20 69 66 28 20 21 7a 57 68 65 72 65 20 29 7b 0a   if( !zWhere ){.
2249b 20 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74      zNew = sqlit
2249c 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e  e3MPrintf(db, "n
2249d 61 6d 65 3d 25 51 22 2c 20 7a 43 6f 6e 73 74 61  ame=%Q", zConsta
2249e 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  nt);.  }else{.  
2249f 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    zNew = sqlite3
224a0 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 20  MPrintf(db, "%s 
224a1 4f 52 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 57 68  OR name=%Q", zWh
224a2 65 72 65 2c 20 7a 43 6f 6e 73 74 61 6e 74 29 3b  ere, zConstant);
224a3 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
224a4 65 65 28 64 62 2c 20 7a 57 68 65 72 65 29 3b 0a  ee(db, zWhere);.
224a5 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 65    }.  return zNe
224a6 77 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e  w;.}..#if !defin
224a7 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ed(SQLITE_OMIT_F
224a8 4f 52 45 49 47 4e 5f 4b 45 59 29 20 26 26 20 21  OREIGN_KEY) && !
224a9 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
224aa 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 2f 2a 0a  MIT_TRIGGER)./*.
224ab 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20  ** Generate the 
224ac 74 65 78 74 20 6f 66 20 61 20 57 48 45 52 45 20  text of a WHERE 
224ad 65 78 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68  expression which
224ae 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
224af 73 65 6c 65 63 74 20 61 6c 6c 0a 2a 2a 20 74 61  select all.** ta
224b0 62 6c 65 73 20 74 68 61 74 20 68 61 76 65 20 66  bles that have f
224b1 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
224b2 72 61 69 6e 74 73 20 74 68 61 74 20 72 65 66 65  raints that refe
224b3 72 20 74 6f 20 74 61 62 6c 65 20 70 54 61 62 20  r to table pTab 
224b4 28 69 2e 65 2e 0a 2a 2a 20 63 6f 6e 73 74 72 61  (i.e..** constra
224b5 69 6e 74 73 20 66 6f 72 20 77 68 69 63 68 20 70  ints for which p
224b6 54 61 62 20 69 73 20 74 68 65 20 70 61 72 65 6e  Tab is the paren
224b7 74 20 74 61 62 6c 65 29 20 66 72 6f 6d 20 74 68  t table) from th
224b8 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 0a  e sqlite_master.
224b9 2a 2a 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  ** table..*/.sta
224ba 74 69 63 20 63 68 61 72 20 2a 77 68 65 72 65 46  tic char *whereF
224bb 6f 72 65 69 67 6e 4b 65 79 73 28 50 61 72 73 65  oreignKeys(Parse
224bc 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20   *pParse, Table 
224bd 2a 70 54 61 62 29 7b 0a 20 20 46 4b 65 79 20 2a  *pTab){.  FKey *
224be 70 3b 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72  p;.  char *zWher
224bf 65 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 3d 73  e = 0;.  for(p=s
224c0 71 6c 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63  qlite3FkReferenc
224c1 65 73 28 70 54 61 62 29 3b 20 70 3b 20 70 3d 70  es(pTab); p; p=p
224c2 2d 3e 70 4e 65 78 74 54 6f 29 7b 0a 20 20 20 20  ->pNextTo){.    
224c3 7a 57 68 65 72 65 20 3d 20 77 68 65 72 65 4f 72  zWhere = whereOr
224c4 4e 61 6d 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  Name(pParse->db,
224c5 20 7a 57 68 65 72 65 2c 20 70 2d 3e 70 46 72 6f   zWhere, p->pFro
224c6 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20  m->zName);.  }. 
224c7 20 72 65 74 75 72 6e 20 7a 57 68 65 72 65 3b 0a   return zWhere;.
224c8 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
224c9 47 65 6e 65 72 61 74 65 20 74 68 65 20 74 65 78  Generate the tex
224ca 74 20 6f 66 20 61 20 57 48 45 52 45 20 65 78 70  t of a WHERE exp
224cb 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20 63 61  ression which ca
224cc 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 6c  n be used to sel
224cd 65 63 74 20 61 6c 6c 0a 2a 2a 20 74 65 6d 70 6f  ect all.** tempo
224ce 72 61 72 79 20 74 72 69 67 67 65 72 73 20 6f 6e  rary triggers on
224cf 20 74 61 62 6c 65 20 70 54 61 62 20 66 72 6f 6d   table pTab from
224d0 20 74 68 65 20 73 71 6c 69 74 65 5f 74 65 6d 70   the sqlite_temp
224d1 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 49  _master table. I
224d2 66 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 20  f.** table pTab 
224d3 68 61 73 20 6e 6f 20 74 65 6d 70 6f 72 61 72 79  has no temporary
224d4 20 74 72 69 67 67 65 72 73 2c 20 6f 72 20 69 73   triggers, or is
224d5 20 69 74 73 65 6c 66 20 73 74 6f 72 65 64 20 69   itself stored i
224d6 6e 20 74 68 65 20 0a 2a 2a 20 74 65 6d 70 6f 72  n the .** tempor
224d7 61 72 79 20 64 61 74 61 62 61 73 65 2c 20 4e 55  ary database, NU
224d8 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  LL is returned..
224d9 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  */.static char *
224da 77 68 65 72 65 54 65 6d 70 54 72 69 67 67 65 72  whereTempTrigger
224db 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  s(Parse *pParse,
224dc 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20   Table *pTab){. 
224dd 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b   Trigger *pTrig;
224de 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20  .  char *zWhere 
224df 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 53 63 68  = 0;.  const Sch
224e0 65 6d 61 20 2a 70 54 65 6d 70 53 63 68 65 6d 61  ema *pTempSchema
224e1 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61   = pParse->db->a
224e2 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 3b 20 2f  Db[1].pSchema; /
224e3 2a 20 54 65 6d 70 20 64 62 20 73 63 68 65 6d 61  * Temp db schema
224e4 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   */..  /* If the
224e5 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 6c 6f   table is not lo
224e6 63 61 74 65 64 20 69 6e 20 74 68 65 20 74 65 6d  cated in the tem
224e7 70 2d 64 62 20 28 69 6e 20 77 68 69 63 68 20 63  p-db (in which c
224e8 61 73 65 20 4e 55 4c 4c 20 69 73 20 0a 20 20 2a  ase NULL is .  *
224e9 2a 20 72 65 74 75 72 6e 65 64 2c 20 6c 6f 6f 70  * returned, loop
224ea 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 61 62   through the tab
224eb 6c 65 73 20 6c 69 73 74 20 6f 66 20 74 72 69 67  les list of trig
224ec 67 65 72 73 2e 20 46 6f 72 20 65 61 63 68 20 74  gers. For each t
224ed 72 69 67 67 65 72 0a 20 20 2a 2a 20 74 68 61 74  rigger.  ** that
224ee 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20   is not part of 
224ef 74 68 65 20 74 65 6d 70 2d 64 62 20 73 63 68 65  the temp-db sche
224f0 6d 61 2c 20 61 64 64 20 61 20 63 6c 61 75 73 65  ma, add a clause
224f1 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 0a 20   to the WHERE . 
224f2 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 62   ** expression b
224f3 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20 69 6e  eing built up in
224f4 20 7a 57 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20   zWhere..  */.  
224f5 69 66 28 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  if( pTab->pSchem
224f6 61 21 3d 70 54 65 6d 70 53 63 68 65 6d 61 20 29  a!=pTempSchema )
224f7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64  {.    sqlite3 *d
224f8 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
224f9 20 20 20 20 66 6f 72 28 70 54 72 69 67 3d 73 71      for(pTrig=sq
224fa 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 73 74  lite3TriggerList
224fb 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 20  (pParse, pTab); 
224fc 70 54 72 69 67 3b 20 70 54 72 69 67 3d 70 54 72  pTrig; pTrig=pTr
224fd 69 67 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  ig->pNext){.    
224fe 20 20 69 66 28 20 70 54 72 69 67 2d 3e 70 53 63    if( pTrig->pSc
224ff 68 65 6d 61 3d 3d 70 54 65 6d 70 53 63 68 65 6d  hema==pTempSchem
22500 61 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 57 68  a ){.        zWh
22501 65 72 65 20 3d 20 77 68 65 72 65 4f 72 4e 61 6d  ere = whereOrNam
22502 65 28 64 62 2c 20 7a 57 68 65 72 65 2c 20 70 54  e(db, zWhere, pT
22503 72 69 67 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  rig->zName);.   
22504 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
22505 20 72 65 74 75 72 6e 20 7a 57 68 65 72 65 3b 0a   return zWhere;.
22506 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
22507 65 20 63 6f 64 65 20 74 6f 20 64 72 6f 70 20 61  e code to drop a
22508 6e 64 20 72 65 6c 6f 61 64 20 74 68 65 20 69 6e  nd reload the in
22509 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74  ternal represent
2250a 61 74 69 6f 6e 20 6f 66 20 74 61 62 6c 65 0a 2a  ation of table.*
2250b 2a 20 70 54 61 62 20 66 72 6f 6d 20 74 68 65 20  * pTab from the 
2250c 64 61 74 61 62 61 73 65 2c 20 69 6e 63 6c 75 64  database, includ
2250d 69 6e 67 20 74 72 69 67 67 65 72 73 20 61 6e 64  ing triggers and
2250e 20 74 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67   temporary trigg
2250f 65 72 73 2e 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ers..** Argument
22510 20 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61   zName is the na
22511 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
22512 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
22513 73 63 68 65 6d 61 20 61 74 0a 2a 2a 20 74 68 65  schema at.** the
22514 20 74 69 6d 65 20 74 68 65 20 67 65 6e 65 72 61   time the genera
22515 74 65 64 20 63 6f 64 65 20 69 73 20 65 78 65 63  ted code is exec
22516 75 74 65 64 2e 20 54 68 69 73 20 63 61 6e 20 62  uted. This can b
22517 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  e different from
22518 0a 2a 2a 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20  .** pTab->zName 
22519 69 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  if this function
2251a 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64   is being called
2251b 20 74 6f 20 63 6f 64 65 20 70 61 72 74 20 6f 66   to code part of
2251c 20 61 6e 20 0a 2a 2a 20 22 41 4c 54 45 52 20 54   an .** "ALTER T
2251d 41 42 4c 45 20 52 45 4e 41 4d 45 20 54 4f 22 20  ABLE RENAME TO" 
2251e 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74  statement..*/.st
2251f 61 74 69 63 20 76 6f 69 64 20 72 65 6c 6f 61 64  atic void reload
22520 54 61 62 6c 65 53 63 68 65 6d 61 28 50 61 72 73  TableSchema(Pars
22521 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65  e *pParse, Table
22522 20 2a 70 54 61 62 2c 20 63 6f 6e 73 74 20 63 68   *pTab, const ch
22523 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 56 64  ar *zName){.  Vd
22524 62 65 20 2a 76 3b 0a 20 20 63 68 61 72 20 2a 7a  be *v;.  char *z
22525 57 68 65 72 65 3b 0a 20 20 69 6e 74 20 69 44 62  Where;.  int iDb
22526 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
22527 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
22528 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
22529 69 6e 67 20 70 54 61 62 20 2a 2f 0a 23 69 66 6e  ing pTab */.#ifn
2252a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2252b 54 52 49 47 47 45 52 0a 20 20 54 72 69 67 67 65  TRIGGER.  Trigge
2252c 72 20 2a 70 54 72 69 67 3b 0a 23 65 6e 64 69 66  r *pTrig;.#endif
2252d 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ..  v = sqlite3G
2252e 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
2252f 20 20 69 66 28 20 4e 45 56 45 52 28 76 3d 3d 30    if( NEVER(v==0
22530 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73  ) ) return;.  as
22531 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
22532 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65  eeHoldsAllMutexe
22533 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b  s(pParse->db) );
22534 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
22535 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
22536 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e  arse->db, pTab->
22537 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73 65  pSchema);.  asse
22538 72 74 28 20 69 44 62 3e 3d 30 20 29 3b 0a 0a 23  rt( iDb>=0 );..#
22539 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2253a 49 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20  IT_TRIGGER.  /* 
2253b 44 72 6f 70 20 61 6e 79 20 74 61 62 6c 65 20 74  Drop any table t
2253c 72 69 67 67 65 72 73 20 66 72 6f 6d 20 74 68 65  riggers from the
2253d 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61   internal schema
2253e 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 54 72 69 67  . */.  for(pTrig
2253f 3d 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c  =sqlite3TriggerL
22540 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61 62  ist(pParse, pTab
22541 29 3b 20 70 54 72 69 67 3b 20 70 54 72 69 67 3d  ); pTrig; pTrig=
22542 70 54 72 69 67 2d 3e 70 4e 65 78 74 29 7b 0a 20  pTrig->pNext){. 
22543 20 20 20 69 6e 74 20 69 54 72 69 67 44 62 20 3d     int iTrigDb =
22544 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
22545 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
22546 2c 20 70 54 72 69 67 2d 3e 70 53 63 68 65 6d 61  , pTrig->pSchema
22547 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  );.    assert( i
22548 54 72 69 67 44 62 3d 3d 69 44 62 20 7c 7c 20 69  TrigDb==iDb || i
22549 54 72 69 67 44 62 3d 3d 31 20 29 3b 0a 20 20 20  TrigDb==1 );.   
2254a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2254b 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72 69  p4(v, OP_DropTri
2254c 67 67 65 72 2c 20 69 54 72 69 67 44 62 2c 20 30  gger, iTrigDb, 0
2254d 2c 20 30 2c 20 70 54 72 69 67 2d 3e 7a 4e 61 6d  , 0, pTrig->zNam
2254e 65 2c 20 30 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  e, 0);.  }.#endi
2254f 66 0a 0a 20 20 2f 2a 20 44 72 6f 70 20 74 68 65  f..  /* Drop the
22550 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78   table and index
22551 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e   from the intern
22552 61 6c 20 73 63 68 65 6d 61 2e 20 20 2a 2f 0a 20  al schema.  */. 
22553 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22554 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 61 62  p4(v, OP_DropTab
22555 6c 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 70  le, iDb, 0, 0, p
22556 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a  Tab->zName, 0);.
22557 0a 20 20 2f 2a 20 52 65 6c 6f 61 64 20 74 68 65  .  /* Reload the
22558 20 74 61 62 6c 65 2c 20 69 6e 64 65 78 20 61 6e   table, index an
22559 64 20 70 65 72 6d 61 6e 65 6e 74 20 74 72 69 67  d permanent trig
2255a 67 65 72 20 73 63 68 65 6d 61 73 2e 20 2a 2f 0a  ger schemas. */.
2255b 20 20 7a 57 68 65 72 65 20 3d 20 73 71 6c 69 74    zWhere = sqlit
2255c 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72 73 65  e3MPrintf(pParse
2255d 2d 3e 64 62 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d  ->db, "tbl_name=
2255e 25 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69  %Q", zName);.  i
2255f 66 28 20 21 7a 57 68 65 72 65 20 29 20 72 65 74  f( !zWhere ) ret
22560 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  urn;.  sqlite3Vd
22561 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50  beAddOp4(v, OP_P
22562 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62 2c  arseSchema, iDb,
22563 20 30 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20 50   0, 0, zWhere, P
22564 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 0a 23 69 66  4_DYNAMIC);..#if
22565 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
22566 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 4e 6f  _TRIGGER.  /* No
22567 77 2c 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  w, if the table 
22568 69 73 20 6e 6f 74 20 73 74 6f 72 65 64 20 69 6e  is not stored in
22569 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 62 61   the temp databa
2256a 73 65 2c 20 72 65 6c 6f 61 64 20 61 6e 79 20 74  se, reload any t
2256b 65 6d 70 20 0a 20 20 2a 2a 20 74 72 69 67 67 65  emp .  ** trigge
2256c 72 73 2e 20 44 6f 6e 27 74 20 75 73 65 20 49 4e  rs. Don't use IN
2256d 28 2e 2e 2e 29 20 69 6e 20 63 61 73 65 20 53 51  (...) in case SQ
2256e 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
2256f 52 59 20 69 73 20 64 65 66 69 6e 65 64 2e 20 0a  RY is defined. .
22570 20 20 2a 2f 0a 20 20 69 66 28 20 28 7a 57 68 65    */.  if( (zWhe
22571 72 65 3d 77 68 65 72 65 54 65 6d 70 54 72 69 67  re=whereTempTrig
22572 67 65 72 73 28 70 50 61 72 73 65 2c 20 70 54 61  gers(pParse, pTa
22573 62 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71  b))!=0 ){.    sq
22574 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
22575 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d  v, OP_ParseSchem
22576 61 2c 20 31 2c 20 30 2c 20 30 2c 20 7a 57 68 65  a, 1, 0, 0, zWhe
22577 72 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b  re, P4_DYNAMIC);
22578 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  .  }.#endif.}../
22579 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
2257a 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  de to implement 
2257b 74 68 65 20 22 41 4c 54 45 52 20 54 41 42 4c 45  the "ALTER TABLE
2257c 20 78 78 78 20 52 45 4e 41 4d 45 20 54 4f 20 79   xxx RENAME TO y
2257d 79 79 22 20 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2e  yy" .** command.
2257e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
2257f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
22580 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62 6c 65  AlterRenameTable
22581 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
22582 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e,            /*
22583 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e   Parser context.
22584 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
22585 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  Src,            
22586 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20  /* The table to 
22587 72 65 6e 61 6d 65 2e 20 2a 2f 0a 20 20 54 6f 6b  rename. */.  Tok
22588 65 6e 20 2a 70 4e 61 6d 65 20 20 20 20 20 20 20  en *pName       
22589 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65         /* The ne
2258a 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f  w table name. */
2258b 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20  .){.  int iDb;  
2258c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2258d 2f 2a 20 44 61 74 61 62 61 73 65 20 74 68 61 74  /* Database that
2258e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 61   contains the ta
2258f 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ble */.  char *z
22590 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
22591 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61     /* Name of da
22592 74 61 62 61 73 65 20 69 44 62 20 2a 2f 0a 20 20  tabase iDb */.  
22593 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20  Table *pTab;    
22594 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62            /* Tab
22595 6c 65 20 62 65 69 6e 67 20 72 65 6e 61 6d 65 64  le being renamed
22596 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d   */.  char *zNam
22597 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  e = 0;          
22598 2f 2a 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61 74  /* NULL-terminat
22599 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 4e  ed version of pN
2259a 61 6d 65 20 2a 2f 20 0a 20 20 73 71 6c 69 74 65  ame */ .  sqlite
2259b 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2259c 64 62 3b 20 2f 2a 20 44 61 74 61 62 61 73 65 20  db; /* Database 
2259d 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
2259e 69 6e 74 20 6e 54 61 62 4e 61 6d 65 3b 20 20 20  int nTabName;   
2259f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
225a0 62 65 72 20 6f 66 20 55 54 46 2d 38 20 63 68 61  ber of UTF-8 cha
225a1 72 61 63 74 65 72 73 20 69 6e 20 7a 54 61 62 4e  racters in zTabN
225a2 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ame */.  const c
225a3 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 3b 20 20  har *zTabName;  
225a4 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6e     /* Original n
225a5 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
225a6 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 0a 23   */.  Vdbe *v;.#
225a7 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
225a8 49 54 5f 54 52 49 47 47 45 52 0a 20 20 63 68 61  IT_TRIGGER.  cha
225a9 72 20 2a 7a 57 68 65 72 65 20 3d 20 30 3b 20 20  r *zWhere = 0;  
225aa 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20         /* Where 
225ab 63 6c 61 75 73 65 20 74 6f 20 6c 6f 63 61 74 65  clause to locate
225ac 20 74 65 6d 70 20 74 72 69 67 67 65 72 73 20 2a   temp triggers *
225ad 2f 0a 23 65 6e 64 69 66 0a 20 20 56 54 61 62 6c  /.#endif.  VTabl
225ae 65 20 2a 70 56 54 61 62 20 3d 20 30 3b 20 20 20  e *pVTab = 0;   
225af 20 20 20 20 20 2f 2a 20 4e 6f 6e 2d 7a 65 72 6f       /* Non-zero
225b0 20 69 66 20 74 68 69 73 20 69 73 20 61 20 76 2d   if this is a v-
225b1 74 61 62 20 77 69 74 68 20 61 6e 20 78 52 65 6e  tab with an xRen
225b2 61 6d 65 28 29 20 2a 2f 0a 20 20 0a 20 20 69 66  ame() */.  .  if
225b3 28 20 4e 45 56 45 52 28 64 62 2d 3e 6d 61 6c 6c  ( NEVER(db->mall
225b4 6f 63 46 61 69 6c 65 64 29 20 29 20 67 6f 74 6f  ocFailed) ) goto
225b5 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62   exit_rename_tab
225b6 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53  le;.  assert( pS
225b7 72 63 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20  rc->nSrc==1 );. 
225b8 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
225b9 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74  BtreeHoldsAllMut
225ba 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29  exes(pParse->db)
225bb 20 29 3b 0a 0a 20 20 70 54 61 62 20 3d 20 73 71   );..  pTab = sq
225bc 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
225bd 28 70 50 61 72 73 65 2c 20 30 2c 20 70 53 72 63  (pParse, 0, pSrc
225be 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 53  ->a[0].zName, pS
225bf 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61  rc->a[0].zDataba
225c0 73 65 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62  se);.  if( !pTab
225c1 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e   ) goto exit_ren
225c2 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 69 44 62  ame_table;.  iDb
225c3 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
225c4 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
225c5 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
225c6 61 29 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e  a);.  zDb = db->
225c7 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a  aDb[iDb].zName;.
225c8 0a 20 20 2f 2a 20 47 65 74 20 61 20 4e 55 4c 4c  .  /* Get a NULL
225c9 20 74 65 72 6d 69 6e 61 74 65 64 20 76 65 72 73   terminated vers
225ca 69 6f 6e 20 6f 66 20 74 68 65 20 6e 65 77 20 74  ion of the new t
225cb 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20  able name. */.  
225cc 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e  zName = sqlite3N
225cd 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
225ce 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21   pName);.  if( !
225cf 7a 4e 61 6d 65 20 29 20 67 6f 74 6f 20 65 78 69  zName ) goto exi
225d0 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a  t_rename_table;.
225d1 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74  .  /* Check that
225d2 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65   a table or inde
225d3 78 20 6e 61 6d 65 64 20 27 7a 4e 61 6d 65 27 20  x named 'zName' 
225d4 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  does not already
225d5 20 65 78 69 73 74 0a 20 20 2a 2a 20 69 6e 20 64   exist.  ** in d
225d6 61 74 61 62 61 73 65 20 69 44 62 2e 20 49 66 20  atabase iDb. If 
225d7 73 6f 2c 20 74 68 69 73 20 69 73 20 61 6e 20 65  so, this is an e
225d8 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  rror..  */.  if(
225d9 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
225da 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62  e(db, zName, zDb
225db 29 20 7c 7c 20 73 71 6c 69 74 65 33 46 69 6e 64  ) || sqlite3Find
225dc 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c  Index(db, zName,
225dd 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 73 71 6c   zDb) ){.    sql
225de 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
225df 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 74  rse, .        "t
225e0 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20  here is already 
225e1 61 6e 6f 74 68 65 72 20 74 61 62 6c 65 20 6f 72  another table or
225e2 20 69 6e 64 65 78 20 77 69 74 68 20 74 68 69 73   index with this
225e3 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a 4e 61 6d   name: %s", zNam
225e4 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69  e);.    goto exi
225e5 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a  t_rename_table;.
225e6 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73    }..  /* Make s
225e7 75 72 65 20 69 74 20 69 73 20 6e 6f 74 20 61 20  ure it is not a 
225e8 73 79 73 74 65 6d 20 74 61 62 6c 65 20 62 65 69  system table bei
225e9 6e 67 20 61 6c 74 65 72 65 64 2c 20 6f 72 20 61  ng altered, or a
225ea 20 72 65 73 65 72 76 65 64 20 6e 61 6d 65 0a 20   reserved name. 
225eb 20 2a 2a 20 74 68 61 74 20 74 68 65 20 74 61 62   ** that the tab
225ec 6c 65 20 69 73 20 62 65 69 6e 67 20 72 65 6e 61  le is being rena
225ed 6d 65 64 20 74 6f 2e 0a 20 20 2a 2f 0a 20 20 69  med to..  */.  i
225ee 66 28 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  f( sqlite3Strlen
225ef 33 30 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3e  30(pTab->zName)>
225f0 36 20 0a 20 20 20 26 26 20 30 3d 3d 73 71 6c 69  6 .   && 0==sqli
225f1 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54 61 62  te3StrNICmp(pTab
225f2 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65  ->zName, "sqlite
225f3 5f 22 2c 20 37 29 0a 20 20 29 7b 0a 20 20 20 20  _", 7).  ){.    
225f4 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
225f5 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25  pParse, "table %
225f6 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c 74  s may not be alt
225f7 65 72 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61  ered", pTab->zNa
225f8 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78  me);.    goto ex
225f9 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b  it_rename_table;
225fa 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54  .  }.  if( SQLIT
225fb 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68 65  E_OK!=sqlite3Che
225fc 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50 61  ckObjectName(pPa
225fd 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20  rse, zName) ){. 
225fe 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e     goto exit_ren
225ff 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a  ame_table;.  }..
22600 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
22601 4d 49 54 5f 56 49 45 57 0a 20 20 69 66 28 20 70  MIT_VIEW.  if( p
22602 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  Tab->pSelect ){.
22603 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
22604 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69 65  Msg(pParse, "vie
22605 77 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20  w %s may not be 
22606 61 6c 74 65 72 65 64 22 2c 20 70 54 61 62 2d 3e  altered", pTab->
22607 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f  zName);.    goto
22608 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62   exit_rename_tab
22609 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  le;.  }.#endif..
2260a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2260b 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
2260c 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68  N.  /* Invoke th
2260d 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  e authorization 
2260e 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20 20 69  callback. */.  i
2260f 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
22610 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
22611 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 2c 20  TE_ALTER_TABLE, 
22612 7a 44 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  zDb, pTab->zName
22613 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  , 0) ){.    goto
22614 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62   exit_rename_tab
22615 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  le;.  }.#endif..
22616 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
22617 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
22618 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 69  .  if( sqlite3Vi
22619 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73  ewGetColumnNames
2261a 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 29  (pParse, pTab) )
2261b 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  {.    goto exit_
2261c 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20  rename_table;.  
2261d 7d 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61  }.  if( IsVirtua
2261e 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 70  l(pTab) ){.    p
2261f 56 54 61 62 20 3d 20 73 71 6c 69 74 65 33 47 65  VTab = sqlite3Ge
22620 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62  tVTable(db, pTab
22621 29 3b 0a 20 20 20 20 69 66 28 20 70 56 54 61 62  );.    if( pVTab
22622 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  ->pVtab->pModule
22623 2d 3e 78 52 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a  ->xRename==0 ){.
22624 20 20 20 20 20 20 70 56 54 61 62 20 3d 20 30 3b        pVTab = 0;
22625 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
22626 66 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61 20  f..  /* Begin a 
22627 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20  transaction and 
22628 63 6f 64 65 20 74 68 65 20 56 65 72 69 66 79 43  code the VerifyC
22629 6f 6f 6b 69 65 20 66 6f 72 20 64 61 74 61 62 61  ookie for databa
2262a 73 65 20 69 44 62 2e 20 0a 20 20 2a 2a 20 54 68  se iDb. .  ** Th
2262b 65 6e 20 6d 6f 64 69 66 79 20 74 68 65 20 73 63  en modify the sc
2262c 68 65 6d 61 20 63 6f 6f 6b 69 65 20 28 73 69 6e  hema cookie (sin
2262d 63 65 20 74 68 65 20 41 4c 54 45 52 20 54 41 42  ce the ALTER TAB
2262e 4c 45 20 6d 6f 64 69 66 69 65 73 20 74 68 65 0a  LE modifies the.
2262f 20 20 2a 2a 20 73 63 68 65 6d 61 29 2e 20 4f 70    ** schema). Op
22630 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74  en a statement t
22631 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68  ransaction if th
22632 65 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 72  e table is a vir
22633 74 75 61 6c 0a 20 20 2a 2a 20 74 61 62 6c 65 2e  tual.  ** table.
22634 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69  .  */.  v = sqli
22635 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
22636 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  e);.  if( v==0 )
22637 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  {.    goto exit_
22638 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20  rename_table;.  
22639 7d 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e  }.  sqlite3Begin
2263a 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70  WriteOperation(p
2263b 50 61 72 73 65 2c 20 70 56 54 61 62 21 3d 30 2c  Parse, pVTab!=0,
2263c 20 69 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 33   iDb);.  sqlite3
2263d 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61  ChangeCookie(pPa
2263e 72 73 65 2c 20 69 44 62 29 3b 0a 0a 20 20 2f 2a  rse, iDb);..  /*
2263f 20 49 66 20 74 68 69 73 20 69 73 20 61 20 76 69   If this is a vi
22640 72 74 75 61 6c 20 74 61 62 6c 65 2c 20 69 6e 76  rtual table, inv
22641 6f 6b 65 20 74 68 65 20 78 52 65 6e 61 6d 65 28  oke the xRename(
22642 29 20 66 75 6e 63 74 69 6f 6e 20 69 66 0a 20 20  ) function if.  
22643 2a 2a 20 6f 6e 65 20 69 73 20 64 65 66 69 6e 65  ** one is define
22644 64 2e 20 54 68 65 20 78 52 65 6e 61 6d 65 28 29  d. The xRename()
22645 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 20 6d   callback will m
22646 6f 64 69 66 79 20 74 68 65 20 6e 61 6d 65 73 0a  odify the names.
22647 20 20 2a 2a 20 6f 66 20 61 6e 79 20 72 65 73 6f    ** of any reso
22648 75 72 63 65 73 20 75 73 65 64 20 62 79 20 74 68  urces used by th
22649 65 20 76 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d  e v-table implem
2264a 65 6e 74 61 74 69 6f 6e 20 28 69 6e 63 6c 75 64  entation (includ
2264b 69 6e 67 20 6f 74 68 65 72 0a 20 20 2a 2a 20 53  ing other.  ** S
2264c 51 4c 69 74 65 20 74 61 62 6c 65 73 29 20 74 68  QLite tables) th
2264d 61 74 20 61 72 65 20 69 64 65 6e 74 69 66 69 65  at are identifie
2264e 64 20 62 79 20 74 68 65 20 6e 61 6d 65 20 6f 66  d by the name of
2264f 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
22650 6c 65 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66  le..  */.#ifndef
22651 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
22652 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20  TUALTABLE.  if( 
22653 70 56 54 61 62 20 29 7b 0a 20 20 20 20 69 6e 74  pVTab ){.    int
22654 20 69 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e   i = ++pParse->n
22655 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Mem;.    sqlite3
22656 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
22657 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 69 2c 20  _String8, 0, i, 
22658 30 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  0, zName, 0);.  
22659 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2265a 4f 70 34 28 76 2c 20 4f 50 5f 56 52 65 6e 61 6d  Op4(v, OP_VRenam
2265b 65 2c 20 69 2c 20 30 2c 20 30 2c 28 63 6f 6e 73  e, i, 0, 0,(cons
2265c 74 20 63 68 61 72 2a 29 70 56 54 61 62 2c 20 50  t char*)pVTab, P
2265d 34 5f 56 54 41 42 29 3b 0a 20 20 20 20 73 71 6c  4_VTAB);.    sql
2265e 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61  ite3MayAbort(pPa
2265f 72 73 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  rse);.  }.#endif
22660 0a 0a 20 20 2f 2a 20 66 69 67 75 72 65 20 6f 75  ..  /* figure ou
22661 74 20 68 6f 77 20 6d 61 6e 79 20 55 54 46 2d 38  t how many UTF-8
22662 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20   characters are 
22663 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7a 54  in zName */.  zT
22664 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a  abName = pTab->z
22665 4e 61 6d 65 3b 0a 20 20 6e 54 61 62 4e 61 6d 65  Name;.  nTabName
22666 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 43 68   = sqlite3Utf8Ch
22667 61 72 4c 65 6e 28 7a 54 61 62 4e 61 6d 65 2c 20  arLen(zTabName, 
22668 2d 31 29 3b 0a 0a 23 69 66 20 21 64 65 66 69 6e  -1);..#if !defin
22669 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ed(SQLITE_OMIT_F
2266a 4f 52 45 49 47 4e 5f 4b 45 59 29 20 26 26 20 21  OREIGN_KEY) && !
2266b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2266c 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20 69  MIT_TRIGGER).  i
2266d 66 28 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  f( db->flags&SQL
2266e 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20  ITE_ForeignKeys 
2266f 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66 6f 72  ){.    /* If for
22670 65 69 67 6e 2d 6b 65 79 20 73 75 70 70 6f 72 74  eign-key support
22671 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 72 65 77   is enabled, rew
22672 72 69 74 65 20 74 68 65 20 43 52 45 41 54 45 20  rite the CREATE 
22673 54 41 42 4c 45 20 0a 20 20 20 20 2a 2a 20 73 74  TABLE .    ** st
22674 61 74 65 6d 65 6e 74 73 20 63 6f 72 72 65 73 70  atements corresp
22675 6f 6e 64 69 6e 67 20 74 6f 20 61 6c 6c 20 63 68  onding to all ch
22676 69 6c 64 20 74 61 62 6c 65 73 20 6f 66 20 66 6f  ild tables of fo
22677 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
22678 61 69 6e 74 73 0a 20 20 20 20 2a 2a 20 66 6f 72  aints.    ** for
22679 20 77 68 69 63 68 20 74 68 65 20 72 65 6e 61 6d   which the renam
2267a 65 64 20 74 61 62 6c 65 20 69 73 20 74 68 65 20  ed table is the 
2267b 70 61 72 65 6e 74 20 74 61 62 6c 65 2e 20 20 2a  parent table.  *
2267c 2f 0a 20 20 20 20 69 66 28 20 28 7a 57 68 65 72  /.    if( (zWher
2267d 65 3d 77 68 65 72 65 46 6f 72 65 69 67 6e 4b 65  e=whereForeignKe
2267e 79 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29  ys(pParse, pTab)
2267f 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  )!=0 ){.      sq
22680 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
22681 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
22682 20 20 20 20 22 55 50 44 41 54 45 20 73 71 6c 69      "UPDATE sqli
22683 74 65 5f 6d 61 73 74 65 72 20 53 45 54 20 22 0a  te_master SET ".
22684 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 73                "s
22685 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61  ql = sqlite_rena
22686 6d 65 5f 70 61 72 65 6e 74 28 73 71 6c 2c 20 25  me_parent(sql, %
22687 51 2c 20 25 51 29 20 22 0a 20 20 20 20 20 20 20  Q, %Q) ".       
22688 20 20 20 20 20 20 20 22 57 48 45 52 45 20 25 73         "WHERE %s
22689 3b 22 2c 20 7a 54 61 62 4e 61 6d 65 2c 20 7a 4e  ;", zTabName, zN
2268a 61 6d 65 2c 20 7a 57 68 65 72 65 29 3b 0a 20 20  ame, zWhere);.  
2268b 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2268c 65 28 64 62 2c 20 7a 57 68 65 72 65 29 3b 0a 20  e(db, zWhere);. 
2268d 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
2268e 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65  .  /* Modify the
2268f 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
22690 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68 65 20  able to use the 
22691 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20  new table name. 
22692 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 74  */.  sqlite3Nest
22693 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
22694 20 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51        "UPDATE %Q
22695 2e 25 73 20 53 45 54 20 22 0a 23 69 66 64 65 66  .%s SET ".#ifdef
22696 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
22697 47 47 45 52 0a 20 20 20 20 20 20 20 20 20 20 22  GGER.          "
22698 73 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e  sql = sqlite_ren
22699 61 6d 65 5f 74 61 62 6c 65 28 73 71 6c 2c 20 25  ame_table(sql, %
2269a 51 29 2c 20 22 0a 23 65 6c 73 65 0a 20 20 20 20  Q), ".#else.    
2269b 20 20 20 20 20 20 22 73 71 6c 20 3d 20 43 41 53        "sql = CAS
2269c 45 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  E ".            
2269d 22 57 48 45 4e 20 74 79 70 65 20 3d 20 27 74 72  "WHEN type = 'tr
2269e 69 67 67 65 72 27 20 54 48 45 4e 20 73 71 6c 69  igger' THEN sqli
2269f 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67 65  te_rename_trigge
226a0 72 28 73 71 6c 2c 20 25 51 29 22 0a 20 20 20 20  r(sql, %Q)".    
226a1 20 20 20 20 20 20 20 20 22 45 4c 53 45 20 73 71          "ELSE sq
226a2 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c  lite_rename_tabl
226a3 65 28 73 71 6c 2c 20 25 51 29 20 45 4e 44 2c 20  e(sql, %Q) END, 
226a4 22 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  ".#endif.       
226a5 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 25     "tbl_name = %
226a6 51 2c 20 22 0a 20 20 20 20 20 20 20 20 20 20 22  Q, ".          "
226a7 6e 61 6d 65 20 3d 20 43 41 53 45 20 22 0a 20 20  name = CASE ".  
226a8 20 20 20 20 20 20 20 20 20 20 22 57 48 45 4e 20            "WHEN 
226a9 74 79 70 65 3d 27 74 61 62 6c 65 27 20 54 48 45  type='table' THE
226aa 4e 20 25 51 20 22 0a 20 20 20 20 20 20 20 20 20  N %Q ".         
226ab 20 20 20 22 57 48 45 4e 20 6e 61 6d 65 20 4c 49     "WHEN name LI
226ac 4b 45 20 27 73 71 6c 69 74 65 5f 61 75 74 6f 69  KE 'sqlite_autoi
226ad 6e 64 65 78 25 25 27 20 41 4e 44 20 74 79 70 65  ndex%%' AND type
226ae 3d 27 69 6e 64 65 78 27 20 54 48 45 4e 20 22 0a  ='index' THEN ".
226af 20 20 20 20 20 20 20 20 20 20 20 20 20 22 27 73               "'s
226b0 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 5f  qlite_autoindex_
226b1 27 20 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73 74  ' || %Q || subst
226b2 72 28 6e 61 6d 65 2c 25 64 2b 31 38 29 20 22 0a  r(name,%d+18) ".
226b3 20 20 20 20 20 20 20 20 20 20 20 20 22 45 4c 53              "ELS
226b4 45 20 6e 61 6d 65 20 45 4e 44 20 22 0a 20 20 20  E name END ".   
226b5 20 20 20 22 57 48 45 52 45 20 74 62 6c 5f 6e 61     "WHERE tbl_na
226b6 6d 65 3d 25 51 20 41 4e 44 20 22 0a 20 20 20 20  me=%Q AND ".    
226b7 20 20 20 20 20 20 22 28 74 79 70 65 3d 27 74 61        "(type='ta
226b8 62 6c 65 27 20 4f 52 20 74 79 70 65 3d 27 69 6e  ble' OR type='in
226b9 64 65 78 27 20 4f 52 20 74 79 70 65 3d 27 74 72  dex' OR type='tr
226ba 69 67 67 65 72 27 29 3b 22 2c 20 0a 20 20 20 20  igger');", .    
226bb 20 20 7a 44 62 2c 20 53 43 48 45 4d 41 5f 54 41    zDb, SCHEMA_TA
226bc 42 4c 45 28 69 44 62 29 2c 20 7a 4e 61 6d 65 2c  BLE(iDb), zName,
226bd 20 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 0a   zName, zName, .
226be 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
226bf 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20  MIT_TRIGGER.    
226c0 20 20 7a 4e 61 6d 65 2c 0a 23 65 6e 64 69 66 0a    zName,.#endif.
226c1 20 20 20 20 20 20 7a 4e 61 6d 65 2c 20 6e 54 61        zName, nTa
226c2 62 4e 61 6d 65 2c 20 7a 54 61 62 4e 61 6d 65 0a  bName, zTabName.
226c3 20 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51    );..#ifndef SQ
226c4 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
226c5 43 52 45 4d 45 4e 54 0a 20 20 2f 2a 20 49 66 20  CREMENT.  /* If 
226c6 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65  the sqlite_seque
226c7 6e 63 65 20 74 61 62 6c 65 20 65 78 69 73 74 73  nce table exists
226c8 20 69 6e 20 74 68 69 73 20 64 61 74 61 62 61 73   in this databas
226c9 65 2c 20 74 68 65 6e 20 75 70 64 61 74 65 20 0a  e, then update .
226ca 20 20 2a 2a 20 69 74 20 77 69 74 68 20 74 68 65    ** it with the
226cb 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e   new table name.
226cc 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
226cd 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c  te3FindTable(db,
226ce 20 22 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63   "sqlite_sequenc
226cf 65 22 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20  e", zDb) ){.    
226d0 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
226d1 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20  se(pParse,.     
226d2 20 20 20 22 55 50 44 41 54 45 20 5c 22 25 77 5c     "UPDATE \"%w\
226d3 22 2e 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  ".sqlite_sequenc
226d4 65 20 73 65 74 20 6e 61 6d 65 20 3d 20 25 51 20  e set name = %Q 
226d5 57 48 45 52 45 20 6e 61 6d 65 20 3d 20 25 51 22  WHERE name = %Q"
226d6 2c 0a 20 20 20 20 20 20 20 20 7a 44 62 2c 20 7a  ,.        zDb, z
226d7 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  Name, pTab->zNam
226d8 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  e);.  }.#endif..
226d9 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
226da 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a  MIT_TRIGGER.  /*
226db 20 49 66 20 74 68 65 72 65 20 61 72 65 20 54 45   If there are TE
226dc 4d 50 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74  MP triggers on t
226dd 68 69 73 20 74 61 62 6c 65 2c 20 6d 6f 64 69 66  his table, modif
226de 79 20 74 68 65 20 73 71 6c 69 74 65 5f 74 65 6d  y the sqlite_tem
226df 70 5f 6d 61 73 74 65 72 0a 20 20 2a 2a 20 74 61  p_master.  ** ta
226e0 62 6c 65 2e 20 44 6f 6e 27 74 20 64 6f 20 74 68  ble. Don't do th
226e1 69 73 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  is if the table 
226e2 62 65 69 6e 67 20 41 4c 54 45 52 65 64 20 69 73  being ALTERed is
226e3 20 69 74 73 65 6c 66 20 6c 6f 63 61 74 65 64 20   itself located 
226e4 69 6e 0a 20 20 2a 2a 20 74 68 65 20 74 65 6d 70  in.  ** the temp
226e5 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a   database..  */.
226e6 20 20 69 66 28 20 28 7a 57 68 65 72 65 3d 77 68    if( (zWhere=wh
226e7 65 72 65 54 65 6d 70 54 72 69 67 67 65 72 73 28  ereTempTriggers(
226e8 70 50 61 72 73 65 2c 20 70 54 61 62 29 29 21 3d  pParse, pTab))!=
226e9 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
226ea 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72  NestedParse(pPar
226eb 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 55 50  se, .        "UP
226ec 44 41 54 45 20 73 71 6c 69 74 65 5f 74 65 6d 70  DATE sqlite_temp
226ed 5f 6d 61 73 74 65 72 20 53 45 54 20 22 0a 20 20  _master SET ".  
226ee 20 20 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d            "sql =
226ef 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74   sqlite_rename_t
226f0 72 69 67 67 65 72 28 73 71 6c 2c 20 25 51 29 2c  rigger(sql, %Q),
226f1 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22   ".            "
226f2 74 62 6c 5f 6e 61 6d 65 20 3d 20 25 51 20 22 0a  tbl_name = %Q ".
226f3 20 20 20 20 20 20 20 20 20 20 20 20 22 57 48 45              "WHE
226f4 52 45 20 25 73 3b 22 2c 20 7a 4e 61 6d 65 2c 20  RE %s;", zName, 
226f5 7a 4e 61 6d 65 2c 20 7a 57 68 65 72 65 29 3b 0a  zName, zWhere);.
226f6 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
226f7 65 28 64 62 2c 20 7a 57 68 65 72 65 29 3b 0a 20  e(db, zWhere);. 
226f8 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21   }.#endif..#if !
226f9 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
226fa 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29  MIT_FOREIGN_KEY)
226fb 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
226fc 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
226fd 29 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67  ).  if( db->flag
226fe 73 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e  s&SQLITE_Foreign
226ff 4b 65 79 73 20 29 7b 0a 20 20 20 20 46 4b 65 79  Keys ){.    FKey
22700 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 70 3d 73   *p;.    for(p=s
22701 71 6c 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63  qlite3FkReferenc
22702 65 73 28 70 54 61 62 29 3b 20 70 3b 20 70 3d 70  es(pTab); p; p=p
22703 2d 3e 70 4e 65 78 74 54 6f 29 7b 0a 20 20 20 20  ->pNextTo){.    
22704 20 20 54 61 62 6c 65 20 2a 70 46 72 6f 6d 20 3d    Table *pFrom =
22705 20 70 2d 3e 70 46 72 6f 6d 3b 0a 20 20 20 20 20   p->pFrom;.     
22706 20 69 66 28 20 70 46 72 6f 6d 21 3d 70 54 61 62   if( pFrom!=pTab
22707 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 6f   ){.        relo
22708 61 64 54 61 62 6c 65 53 63 68 65 6d 61 28 70 50  adTableSchema(pP
22709 61 72 73 65 2c 20 70 2d 3e 70 46 72 6f 6d 2c 20  arse, p->pFrom, 
2270a 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  pFrom->zName);. 
2270b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
2270c 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 72  .#endif..  /* Dr
2270d 6f 70 20 61 6e 64 20 72 65 6c 6f 61 64 20 74 68  op and reload th
2270e 65 20 69 6e 74 65 72 6e 61 6c 20 74 61 62 6c 65  e internal table
2270f 20 73 63 68 65 6d 61 2e 20 2a 2f 0a 20 20 72 65   schema. */.  re
22710 6c 6f 61 64 54 61 62 6c 65 53 63 68 65 6d 61 28  loadTableSchema(
22711 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 7a 4e  pParse, pTab, zN
22712 61 6d 65 29 3b 0a 0a 65 78 69 74 5f 72 65 6e 61  ame);..exit_rena
22713 6d 65 5f 74 61 62 6c 65 3a 0a 20 20 73 71 6c 69  me_table:.  sqli
22714 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
22715 28 64 62 2c 20 70 53 72 63 29 3b 0a 20 20 73 71  (db, pSrc);.  sq
22716 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
22717 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  zName);.}.../*.*
22718 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
22719 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65  to make sure the
2271a 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d   file format num
2271b 62 65 72 20 69 73 20 61 74 20 6c 65 61 73 74 20  ber is at least 
2271c 6d 69 6e 46 6f 72 6d 61 74 2e 0a 2a 2a 20 54 68  minFormat..** Th
2271d 65 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65  e generated code
2271e 20 77 69 6c 6c 20 69 6e 63 72 65 61 73 65 20 74   will increase t
2271f 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 6e  he file format n
22720 75 6d 62 65 72 20 69 66 20 6e 65 63 65 73 73 61  umber if necessa
22721 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ry..*/.SQLITE_PR
22722 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
22723 65 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72  e3MinimumFileFor
22724 6d 61 74 28 50 61 72 73 65 20 2a 70 50 61 72 73  mat(Parse *pPars
22725 65 2c 20 69 6e 74 20 69 44 62 2c 20 69 6e 74 20  e, int iDb, int 
22726 6d 69 6e 46 6f 72 6d 61 74 29 7b 0a 20 20 56 64  minFormat){.  Vd
22727 62 65 20 2a 76 3b 0a 20 20 76 20 3d 20 73 71 6c  be *v;.  v = sql
22728 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
22729 73 65 29 3b 0a 20 20 2f 2a 20 54 68 65 20 56 44  se);.  /* The VD
2272a 42 45 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62  BE should have b
2272b 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 65  een allocated be
2272c 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74 69 6e  fore this routin
2272d 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a  e is called..  *
2272e 2a 20 49 66 20 74 68 61 74 20 61 6c 6c 6f 63 61  * If that alloca
2272f 74 69 6f 6e 20 66 61 69 6c 65 64 2c 20 77 65 20  tion failed, we 
22730 77 6f 75 6c 64 20 68 61 76 65 20 71 75 69 74 20  would have quit 
22731 62 65 66 6f 72 65 20 72 65 61 63 68 69 6e 67 20  before reaching 
22732 74 68 69 73 0a 20 20 2a 2a 20 70 6f 69 6e 74 20  this.  ** point 
22733 2a 2f 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  */.  if( ALWAYS(
22734 76 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 31  v) ){.    int r1
22735 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
22736 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
22737 20 20 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74    int r2 = sqlit
22738 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
22739 72 73 65 29 3b 0a 20 20 20 20 69 6e 74 20 6a 31  rse);.    int j1
2273a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2273b 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52 65  eAddOp3(v, OP_Re
2273c 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 72  adCookie, iDb, r
2273d 31 2c 20 42 54 52 45 45 5f 46 49 4c 45 5f 46 4f  1, BTREE_FILE_FO
2273e 52 4d 41 54 29 3b 0a 20 20 20 20 73 71 6c 69 74  RMAT);.    sqlit
2273f 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28  e3VdbeUsesBtree(
22740 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c  v, iDb);.    sql
22741 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
22742 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6d 69  , OP_Integer, mi
22743 6e 46 6f 72 6d 61 74 2c 20 72 32 29 3b 0a 20 20  nFormat, r2);.  
22744 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64    j1 = sqlite3Vd
22745 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 47  beAddOp3(v, OP_G
22746 65 2c 20 72 32 2c 20 30 2c 20 72 31 29 3b 0a 20  e, r2, 0, r1);. 
22747 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22748 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f  dOp3(v, OP_SetCo
22749 6f 6b 69 65 2c 20 69 44 62 2c 20 42 54 52 45 45  okie, iDb, BTREE
2274a 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 2c 20 72 32  _FILE_FORMAT, r2
2274b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2274c 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31  beJumpHere(v, j1
2274d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  );.    sqlite3Re
2274e 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2274f 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 73 71  rse, r1);.    sq
22750 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
22751 52 65 67 28 70 50 61 72 73 65 2c 20 72 32 29 3b  Reg(pParse, r2);
22752 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
22753 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
22754 61 6c 6c 65 64 20 61 66 74 65 72 20 61 6e 20 22  alled after an "
22755 41 4c 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20  ALTER TABLE ... 
22756 41 44 44 22 20 73 74 61 74 65 6d 65 6e 74 0a 2a  ADD" statement.*
22757 2a 20 68 61 73 20 62 65 65 6e 20 70 61 72 73 65  * has been parse
22758 64 2e 20 41 72 67 75 6d 65 6e 74 20 70 43 6f 6c  d. Argument pCol
22759 44 65 66 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  Def contains the
2275a 20 74 65 78 74 20 6f 66 20 74 68 65 20 6e 65 77   text of the new
2275b 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e  .** column defin
2275c 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ition..**.** The
2275d 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   Table structure
2275e 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
2275f 6c 65 20 77 61 73 20 65 78 74 65 6e 64 65 64 20  le was extended 
22760 74 6f 20 69 6e 63 6c 75 64 65 0a 2a 2a 20 74 68  to include.** th
22761 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 64 75 72  e new column dur
22762 69 6e 67 20 70 61 72 73 69 6e 67 2e 0a 2a 2f 0a  ing parsing..*/.
22763 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
22764 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72  oid sqlite3Alter
22765 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28  FinishAddColumn(
22766 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
22767 6f 6b 65 6e 20 2a 70 43 6f 6c 44 65 66 29 7b 0a  oken *pColDef){.
22768 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 3b 20 20    Table *pNew;  
22769 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
2276a 6f 70 79 20 6f 66 20 70 50 61 72 73 65 2d 3e 70  opy of pParse->p
2276b 4e 65 77 54 61 62 6c 65 20 2a 2f 0a 20 20 54 61  NewTable */.  Ta
2276c 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20  ble *pTab;      
2276d 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
2276e 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 20 2a   being altered *
2276f 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
22770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
22771 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72   Database number
22772 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
22773 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20   *zDb;          
22774 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 61 6d 65  /* Database name
22775 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
22776 20 2a 7a 54 61 62 3b 20 20 20 20 20 20 20 20 20   *zTab;         
22777 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a 2f  /* Table name */
22778 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 20  .  char *zCol;  
22779 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2277a 4e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20  Null-terminated 
2277b 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f  column definitio
2277c 6e 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70  n */.  Column *p
2277d 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
2277e 20 2f 2a 20 54 68 65 20 6e 65 77 20 63 6f 6c 75   /* The new colu
2277f 6d 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 44  mn */.  Expr *pD
22780 66 6c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  flt;            
22781 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 6c    /* Default val
22782 75 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 63  ue for the new c
22783 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 73 71 6c 69 74  olumn */.  sqlit
22784 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20  e3 *db;         
22785 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
22786 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 3b  base connection;
22787 20 2a 2f 0a 0a 20 20 64 62 20 3d 20 70 50 61 72   */..  db = pPar
22788 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 50  se->db;.  if( pP
22789 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62  arse->nErr || db
2278a 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2278b 20 72 65 74 75 72 6e 3b 0a 20 20 70 4e 65 77 20   return;.  pNew 
2278c 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  = pParse->pNewTa
2278d 62 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ble;.  assert( p
2278e 4e 65 77 20 29 3b 0a 0a 20 20 61 73 73 65 72 74  New );..  assert
2278f 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
22790 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62  ldsAllMutexes(db
22791 29 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c  ) );.  iDb = sql
22792 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
22793 78 28 64 62 2c 20 70 4e 65 77 2d 3e 70 53 63 68  x(db, pNew->pSch
22794 65 6d 61 29 3b 0a 20 20 7a 44 62 20 3d 20 64 62  ema);.  zDb = db
22795 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
22796 3b 0a 20 20 7a 54 61 62 20 3d 20 26 70 4e 65 77  ;.  zTab = &pNew
22797 2d 3e 7a 4e 61 6d 65 5b 31 36 5d 3b 20 20 2f 2a  ->zName[16];  /*
22798 20 53 6b 69 70 20 74 68 65 20 22 73 71 6c 69 74   Skip the "sqlit
22799 65 5f 61 6c 74 65 72 74 61 62 5f 22 20 70 72 65  e_altertab_" pre
2279a 66 69 78 20 6f 6e 20 74 68 65 20 6e 61 6d 65 20  fix on the name 
2279b 2a 2f 0a 20 20 70 43 6f 6c 20 3d 20 26 70 4e 65  */.  pCol = &pNe
2279c 77 2d 3e 61 43 6f 6c 5b 70 4e 65 77 2d 3e 6e 43  w->aCol[pNew->nC
2279d 6f 6c 2d 31 5d 3b 0a 20 20 70 44 66 6c 74 20 3d  ol-1];.  pDflt =
2279e 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 3b 0a 20 20   pCol->pDflt;.  
2279f 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69  pTab = sqlite3Fi
227a0 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62  ndTable(db, zTab
227a1 2c 20 7a 44 62 29 3b 0a 20 20 61 73 73 65 72 74  , zDb);.  assert
227a2 28 20 70 54 61 62 20 29 3b 0a 0a 23 69 66 6e 64  ( pTab );..#ifnd
227a3 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
227a4 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f  UTHORIZATION.  /
227a5 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 61 75 74  * Invoke the aut
227a6 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62  horization callb
227a7 61 63 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71  ack. */.  if( sq
227a8 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
227a9 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4c  Parse, SQLITE_AL
227aa 54 45 52 5f 54 41 42 4c 45 2c 20 7a 44 62 2c 20  TER_TABLE, zDb, 
227ab 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20  pTab->zName, 0) 
227ac 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
227ad 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
227ae 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 76  If the default v
227af 61 6c 75 65 20 66 6f 72 20 74 68 65 20 6e 65 77  alue for the new
227b0 20 63 6f 6c 75 6d 6e 20 77 61 73 20 73 70 65 63   column was spec
227b1 69 66 69 65 64 20 77 69 74 68 20 61 20 0a 20 20  ified with a .  
227b2 2a 2a 20 6c 69 74 65 72 61 6c 20 4e 55 4c 4c 2c  ** literal NULL,
227b3 20 74 68 65 6e 20 73 65 74 20 70 44 66 6c 74 20   then set pDflt 
227b4 74 6f 20 30 2e 20 54 68 69 73 20 73 69 6d 70 6c  to 0. This simpl
227b5 69 66 69 65 73 20 63 68 65 63 6b 69 6e 67 0a 20  ifies checking. 
227b6 20 2a 2a 20 66 6f 72 20 61 6e 20 53 51 4c 20 4e   ** for an SQL N
227b7 55 4c 4c 20 64 65 66 61 75 6c 74 20 62 65 6c 6f  ULL default belo
227b8 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44  w..  */.  if( pD
227b9 66 6c 74 20 26 26 20 70 44 66 6c 74 2d 3e 6f 70  flt && pDflt->op
227ba 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20  ==TK_NULL ){.   
227bb 20 70 44 66 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a   pDflt = 0;.  }.
227bc 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74  .  /* Check that
227bd 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20   the new column 
227be 69 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64  is not specified
227bf 20 61 73 20 50 52 49 4d 41 52 59 20 4b 45 59 20   as PRIMARY KEY 
227c0 6f 72 20 55 4e 49 51 55 45 2e 0a 20 20 2a 2a 20  or UNIQUE..  ** 
227c1 49 66 20 74 68 65 72 65 20 69 73 20 61 20 4e 4f  If there is a NO
227c2 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e  T NULL constrain
227c3 74 2c 20 74 68 65 6e 20 74 68 65 20 64 65 66 61  t, then the defa
227c4 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68  ult value for th
227c5 65 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6d 75  e.  ** column mu
227c6 73 74 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 0a  st not be NULL..
227c7 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 2d    */.  if( pCol-
227c8 3e 69 73 50 72 69 6d 4b 65 79 20 29 7b 0a 20 20  >isPrimKey ){.  
227c9 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
227ca 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f  g(pParse, "Canno
227cb 74 20 61 64 64 20 61 20 50 52 49 4d 41 52 59 20  t add a PRIMARY 
227cc 4b 45 59 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20 20  KEY column");.  
227cd 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
227ce 69 66 28 20 70 4e 65 77 2d 3e 70 49 6e 64 65 78  if( pNew->pIndex
227cf 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
227d0 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
227d1 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 55 4e  "Cannot add a UN
227d2 49 51 55 45 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20  IQUE column");. 
227d3 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
227d4 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 26   if( (db->flags&
227d5 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65  SQLITE_ForeignKe
227d6 79 73 29 20 26 26 20 70 4e 65 77 2d 3e 70 46 4b  ys) && pNew->pFK
227d7 65 79 20 26 26 20 70 44 66 6c 74 20 29 7b 0a 20  ey && pDflt ){. 
227d8 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
227d9 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  sg(pParse, .    
227da 20 20 20 20 22 43 61 6e 6e 6f 74 20 61 64 64 20      "Cannot add 
227db 61 20 52 45 46 45 52 45 4e 43 45 53 20 63 6f 6c  a REFERENCES col
227dc 75 6d 6e 20 77 69 74 68 20 6e 6f 6e 2d 4e 55 4c  umn with non-NUL
227dd 4c 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 22  L default value"
227de 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
227df 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c 2d 3e 6e   }.  if( pCol->n
227e0 6f 74 4e 75 6c 6c 20 26 26 20 21 70 44 66 6c 74  otNull && !pDflt
227e1 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
227e2 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
227e3 0a 20 20 20 20 20 20 20 20 22 43 61 6e 6e 6f 74  .        "Cannot
227e4 20 61 64 64 20 61 20 4e 4f 54 20 4e 55 4c 4c 20   add a NOT NULL 
227e5 63 6f 6c 75 6d 6e 20 77 69 74 68 20 64 65 66 61  column with defa
227e6 75 6c 74 20 76 61 6c 75 65 20 4e 55 4c 4c 22 29  ult value NULL")
227e7 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
227e8 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74  }..  /* Ensure t
227e9 68 65 20 64 65 66 61 75 6c 74 20 65 78 70 72 65  he default expre
227ea 73 73 69 6f 6e 20 69 73 20 73 6f 6d 65 74 68 69  ssion is somethi
227eb 6e 67 20 74 68 61 74 20 73 71 6c 69 74 65 33 56  ng that sqlite3V
227ec 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 29 0a 20  alueFromExpr(). 
227ed 20 2a 2a 20 63 61 6e 20 68 61 6e 64 6c 65 20 28   ** can handle (
227ee 69 2e 65 2e 20 6e 6f 74 20 43 55 52 52 45 4e 54  i.e. not CURRENT
227ef 5f 54 49 4d 45 20 65 74 63 2e 29 0a 20 20 2a 2f  _TIME etc.).  */
227f0 0a 20 20 69 66 28 20 70 44 66 6c 74 20 29 7b 0a  .  if( pDflt ){.
227f1 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
227f2 65 20 2a 70 56 61 6c 3b 0a 20 20 20 20 69 66 28  e *pVal;.    if(
227f3 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f   sqlite3ValueFro
227f4 6d 45 78 70 72 28 64 62 2c 20 70 44 66 6c 74 2c  mExpr(db, pDflt,
227f5 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51   SQLITE_UTF8, SQ
227f6 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 2c 20 26  LITE_AFF_NONE, &
227f7 70 56 61 6c 29 20 29 7b 0a 20 20 20 20 20 20 64  pVal) ){.      d
227f8 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
227f9 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72  = 1;.      retur
227fa 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  n;.    }.    if(
227fb 20 21 70 56 61 6c 20 29 7b 0a 20 20 20 20 20 20   !pVal ){.      
227fc 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
227fd 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20  pParse, "Cannot 
227fe 61 64 64 20 61 20 63 6f 6c 75 6d 6e 20 77 69 74  add a column wit
227ff 68 20 6e 6f 6e 2d 63 6f 6e 73 74 61 6e 74 20 64  h non-constant d
22800 65 66 61 75 6c 74 22 29 3b 0a 20 20 20 20 20 20  efault");.      
22801 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
22802 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72    sqlite3ValueFr
22803 65 65 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 0a 20  ee(pVal);.  }.. 
22804 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65 20 43   /* Modify the C
22805 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
22806 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 7a 43 6f 6c  ement. */.  zCol
22807 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e   = sqlite3DbStrN
22808 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70  Dup(db, (char*)p
22809 43 6f 6c 44 65 66 2d 3e 7a 2c 20 70 43 6f 6c 44  ColDef->z, pColD
2280a 65 66 2d 3e 6e 29 3b 0a 20 20 69 66 28 20 7a 43  ef->n);.  if( zC
2280b 6f 6c 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  ol ){.    char *
2280c 7a 45 6e 64 20 3d 20 26 7a 43 6f 6c 5b 70 43 6f  zEnd = &zCol[pCo
2280d 6c 44 65 66 2d 3e 6e 2d 31 5d 3b 0a 20 20 20 20  lDef->n-1];.    
2280e 77 68 69 6c 65 28 20 7a 45 6e 64 3e 7a 43 6f 6c  while( zEnd>zCol
2280f 20 26 26 20 28 2a 7a 45 6e 64 3d 3d 27 3b 27 20   && (*zEnd==';' 
22810 7c 7c 20 73 71 6c 69 74 65 33 49 73 73 70 61 63  || sqlite3Isspac
22811 65 28 2a 7a 45 6e 64 29 29 20 29 7b 0a 20 20 20  e(*zEnd)) ){.   
22812 20 20 20 2a 7a 45 6e 64 2d 2d 20 3d 20 27 5c 30     *zEnd-- = '\0
22813 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  ';.    }.    sql
22814 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
22815 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
22816 20 22 55 50 44 41 54 45 20 5c 22 25 77 5c 22 2e   "UPDATE \"%w\".
22817 25 73 20 53 45 54 20 22 0a 20 20 20 20 20 20 20  %s SET ".       
22818 20 20 20 22 73 71 6c 20 3d 20 73 75 62 73 74 72     "sql = substr
22819 28 73 71 6c 2c 31 2c 25 64 29 20 7c 7c 20 27 2c  (sql,1,%d) || ',
2281a 20 27 20 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73   ' || %Q || subs
2281b 74 72 28 73 71 6c 2c 25 64 29 20 22 0a 20 20 20  tr(sql,%d) ".   
2281c 20 20 20 20 20 22 57 48 45 52 45 20 74 79 70 65       "WHERE type
2281d 20 3d 20 27 74 61 62 6c 65 27 20 41 4e 44 20 6e   = 'table' AND n
2281e 61 6d 65 20 3d 20 25 51 22 2c 20 0a 20 20 20 20  ame = %Q", .    
2281f 20 20 7a 44 62 2c 20 53 43 48 45 4d 41 5f 54 41    zDb, SCHEMA_TA
22820 42 4c 45 28 69 44 62 29 2c 20 70 4e 65 77 2d 3e  BLE(iDb), pNew->
22821 61 64 64 43 6f 6c 4f 66 66 73 65 74 2c 20 7a 43  addColOffset, zC
22822 6f 6c 2c 20 70 4e 65 77 2d 3e 61 64 64 43 6f 6c  ol, pNew->addCol
22823 4f 66 66 73 65 74 2b 31 2c 0a 20 20 20 20 20 20  Offset+1,.      
22824 7a 54 61 62 0a 20 20 20 20 29 3b 0a 20 20 20 20  zTab.    );.    
22825 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
22826 2c 20 7a 43 6f 6c 29 3b 0a 20 20 7d 0a 0a 20 20  , zCol);.  }..  
22827 2f 2a 20 49 66 20 74 68 65 20 64 65 66 61 75 6c  /* If the defaul
22828 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6e  t value of the n
22829 65 77 20 63 6f 6c 75 6d 6e 20 69 73 20 4e 55 4c  ew column is NUL
2282a 4c 2c 20 74 68 65 6e 20 73 65 74 20 74 68 65 20  L, then set the 
2282b 66 69 6c 65 0a 20 20 2a 2a 20 66 6f 72 6d 61 74  file.  ** format
2282c 20 74 6f 20 32 2e 20 49 66 20 74 68 65 20 64 65   to 2. If the de
2282d 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 74  fault value of t
2282e 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73  he new column is
2282f 20 6e 6f 74 20 4e 55 4c 4c 2c 0a 20 20 2a 2a 20   not NULL,.  ** 
22830 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20  the file format 
22831 62 65 63 6f 6d 65 73 20 33 2e 0a 20 20 2a 2f 0a  becomes 3..  */.
22832 20 20 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d    sqlite3Minimum
22833 46 69 6c 65 46 6f 72 6d 61 74 28 70 50 61 72 73  FileFormat(pPars
22834 65 2c 20 69 44 62 2c 20 70 44 66 6c 74 20 3f 20  e, iDb, pDflt ? 
22835 33 20 3a 20 32 29 3b 0a 0a 20 20 2f 2a 20 52 65  3 : 2);..  /* Re
22836 6c 6f 61 64 20 74 68 65 20 73 63 68 65 6d 61 20  load the schema 
22837 6f 66 20 74 68 65 20 6d 6f 64 69 66 69 65 64 20  of the modified 
22838 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 72 65 6c 6f  table. */.  relo
22839 61 64 54 61 62 6c 65 53 63 68 65 6d 61 28 70 50  adTableSchema(pP
2283a 61 72 73 65 2c 20 70 54 61 62 2c 20 70 54 61 62  arse, pTab, pTab
2283b 2d 3e 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a  ->zName);.}../*.
2283c 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
2283d 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
2283e 65 20 70 61 72 73 65 72 20 61 66 74 65 72 20 74  e parser after t
2283f 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65 20 69 6e  he table-name in
22840 0a 2a 2a 20 61 6e 20 22 41 4c 54 45 52 20 54 41  .** an "ALTER TA
22841 42 4c 45 20 3c 74 61 62 6c 65 2d 6e 61 6d 65 3e  BLE <table-name>
22842 20 41 44 44 22 20 73 74 61 74 65 6d 65 6e 74 20   ADD" statement 
22843 69 73 20 70 61 72 73 65 64 2e 20 41 72 67 75 6d  is parsed. Argum
22844 65 6e 74 20 0a 2a 2a 20 70 53 72 63 20 69 73 20  ent .** pSrc is 
22845 74 68 65 20 66 75 6c 6c 2d 6e 61 6d 65 20 6f 66  the full-name of
22846 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
22847 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20   altered..**.** 
22848 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b  This routine mak
22849 65 73 20 61 20 28 70 61 72 74 69 61 6c 29 20 63  es a (partial) c
2284a 6f 70 79 20 6f 66 20 74 68 65 20 54 61 62 6c 65  opy of the Table
2284b 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 66 6f   structure.** fo
2284c 72 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  r the table bein
2284d 67 20 61 6c 74 65 72 65 64 20 61 6e 64 20 73 65  g altered and se
2284e 74 73 20 50 61 72 73 65 2e 70 4e 65 77 54 61 62  ts Parse.pNewTab
2284f 6c 65 20 74 6f 20 70 6f 69 6e 74 0a 2a 2a 20 74  le to point.** t
22850 6f 20 69 74 2e 20 52 6f 75 74 69 6e 65 73 20 63  o it. Routines c
22851 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72  alled by the par
22852 73 65 72 20 61 73 20 74 68 65 20 63 6f 6c 75 6d  ser as the colum
22853 6e 20 64 65 66 69 6e 69 74 69 6f 6e 0a 2a 2a 20  n definition.** 
22854 69 73 20 70 61 72 73 65 64 20 28 69 2e 65 2e 20  is parsed (i.e. 
22855 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e  sqlite3AddColumn
22856 28 29 29 20 61 64 64 20 74 68 65 20 6e 65 77 20  ()) add the new 
22857 43 6f 6c 75 6d 6e 20 64 61 74 61 20 74 6f 20 0a  Column data to .
22858 2a 2a 20 74 68 65 20 63 6f 70 79 2e 20 54 68 65  ** the copy. The
22859 20 63 6f 70 79 20 6f 66 20 74 68 65 20 54 61 62   copy of the Tab
2285a 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20  le structure is 
2285b 64 65 6c 65 74 65 64 20 62 79 20 74 6f 6b 65 6e  deleted by token
2285c 69 7a 65 2e 63 20 0a 2a 2a 20 61 66 74 65 72 20  ize.c .** after 
2285d 70 61 72 73 69 6e 67 20 69 73 20 66 69 6e 69 73  parsing is finis
2285e 68 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 6f 75 74 69  hed..**.** Routi
2285f 6e 65 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46  ne sqlite3AlterF
22860 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 29  inishAddColumn()
22861 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20   will be called 
22862 74 6f 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63  to complete.** c
22863 6f 64 69 6e 67 20 74 68 65 20 22 41 4c 54 45 52  oding the "ALTER
22864 20 54 41 42 4c 45 20 2e 2e 2e 20 41 44 44 22 20   TABLE ... ADD" 
22865 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51  statement..*/.SQ
22866 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
22867 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 42 65  d sqlite3AlterBe
22868 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72  ginAddColumn(Par
22869 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c  se *pParse, SrcL
2286a 69 73 74 20 2a 70 53 72 63 29 7b 0a 20 20 54 61  ist *pSrc){.  Ta
2286b 62 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 54 61 62  ble *pNew;.  Tab
2286c 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56 64 62 65  le *pTab;.  Vdbe
2286d 20 2a 76 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a   *v;.  int iDb;.
2286e 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e    int i;.  int n
2286f 41 6c 6c 6f 63 3b 0a 20 20 73 71 6c 69 74 65 33  Alloc;.  sqlite3
22870 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
22871 62 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 75 70  b;..  /* Look up
22872 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
22873 20 61 6c 74 65 72 65 64 2e 20 2a 2f 0a 20 20 61   altered. */.  a
22874 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70  ssert( pParse->p
22875 4e 65 77 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20  NewTable==0 );. 
22876 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
22877 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74  BtreeHoldsAllMut
22878 65 78 65 73 28 64 62 29 20 29 3b 0a 20 20 69 66  exes(db) );.  if
22879 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
2287a 65 64 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62  ed ) goto exit_b
2287b 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b  egin_add_column;
2287c 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
2287d 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61  3LocateTable(pPa
2287e 72 73 65 2c 20 30 2c 20 70 53 72 63 2d 3e 61 5b  rse, 0, pSrc->a[
2287f 30 5d 2e 7a 4e 61 6d 65 2c 20 70 53 72 63 2d 3e  0].zName, pSrc->
22880 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b  a[0].zDatabase);
22881 0a 20 20 69 66 28 20 21 70 54 61 62 20 29 20 67  .  if( !pTab ) g
22882 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61  oto exit_begin_a
22883 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 0a 23 69 66 6e  dd_column;..#ifn
22884 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
22885 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69  VIRTUALTABLE.  i
22886 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
22887 62 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  b) ){.    sqlite
22888 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
22889 2c 20 22 76 69 72 74 75 61 6c 20 74 61 62 6c 65  , "virtual table
2288a 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c 74  s may not be alt
2288b 65 72 65 64 22 29 3b 0a 20 20 20 20 67 6f 74 6f  ered");.    goto
2288c 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f   exit_begin_add_
2288d 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 23 65 6e 64  column;.  }.#end
2288e 69 66 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75  if..  /* Make su
2288f 72 65 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61  re this is not a
22890 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 41 4c 54  n attempt to ALT
22891 45 52 20 61 20 76 69 65 77 2e 20 2a 2f 0a 20 20  ER a view. */.  
22892 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63  if( pTab->pSelec
22893 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
22894 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
22895 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 63   "Cannot add a c
22896 6f 6c 75 6d 6e 20 74 6f 20 61 20 76 69 65 77 22  olumn to a view"
22897 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  );.    goto exit
22898 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d  _begin_add_colum
22899 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  n;.  }..  assert
2289a 28 20 70 54 61 62 2d 3e 61 64 64 43 6f 6c 4f 66  ( pTab->addColOf
2289b 66 73 65 74 3e 30 20 29 3b 0a 20 20 69 44 62 20  fset>0 );.  iDb 
2289c 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
2289d 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d  oIndex(db, pTab-
2289e 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 20 20 2f 2a  >pSchema);..  /*
2289f 20 50 75 74 20 61 20 63 6f 70 79 20 6f 66 20 74   Put a copy of t
228a0 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 20  he Table struct 
228a1 69 6e 20 50 61 72 73 65 2e 70 4e 65 77 54 61 62  in Parse.pNewTab
228a2 6c 65 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20  le for the.  ** 
228a3 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e  sqlite3AddColumn
228a4 28 29 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20  () function and 
228a5 66 72 69 65 6e 64 73 20 74 6f 20 6d 6f 64 69 66  friends to modif
228a6 79 2e 20 20 42 75 74 20 6d 6f 64 69 66 79 0a 20  y.  But modify. 
228a7 20 2a 2a 20 74 68 65 20 6e 61 6d 65 20 62 79 20   ** the name by 
228a8 61 64 64 69 6e 67 20 61 6e 20 22 73 71 6c 69 74  adding an "sqlit
228a9 65 5f 61 6c 74 65 72 74 61 62 5f 22 20 70 72 65  e_altertab_" pre
228aa 66 69 78 2e 20 20 42 79 20 61 64 64 69 6e 67 20  fix.  By adding 
228ab 74 68 69 73 0a 20 20 2a 2a 20 70 72 65 66 69 78  this.  ** prefix
228ac 2c 20 77 65 20 69 6e 73 75 72 65 20 74 68 61 74  , we insure that
228ad 20 74 68 65 20 6e 61 6d 65 20 77 69 6c 6c 20 6e   the name will n
228ae 6f 74 20 63 6f 6c 6c 69 64 65 20 77 69 74 68 20  ot collide with 
228af 61 6e 20 65 78 69 73 74 69 6e 67 0a 20 20 2a 2a  an existing.  **
228b0 20 74 61 62 6c 65 20 62 65 63 61 75 73 65 20 75   table because u
228b1 73 65 72 20 74 61 62 6c 65 20 61 72 65 20 6e 6f  ser table are no
228b2 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 68 61 76  t allowed to hav
228b3 65 20 74 68 65 20 22 73 71 6c 69 74 65 5f 22 0a  e the "sqlite_".
228b4 20 20 2a 2a 20 70 72 65 66 69 78 20 6f 6e 20 74    ** prefix on t
228b5 68 65 69 72 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a  heir name..  */.
228b6 20 20 70 4e 65 77 20 3d 20 28 54 61 62 6c 65 2a    pNew = (Table*
228b7 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63  )sqlite3DbMalloc
228b8 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
228b9 54 61 62 6c 65 29 29 3b 0a 20 20 69 66 28 20 21  Table));.  if( !
228ba 70 4e 65 77 20 29 20 67 6f 74 6f 20 65 78 69 74  pNew ) goto exit
228bb 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d  _begin_add_colum
228bc 6e 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e 65  n;.  pParse->pNe
228bd 77 54 61 62 6c 65 20 3d 20 70 4e 65 77 3b 0a 20  wTable = pNew;. 
228be 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b   pNew->nRef = 1;
228bf 0a 20 20 70 4e 65 77 2d 3e 64 62 4d 65 6d 20 3d  .  pNew->dbMem =
228c0 20 70 54 61 62 2d 3e 64 62 4d 65 6d 3b 0a 20 20   pTab->dbMem;.  
228c1 70 4e 65 77 2d 3e 6e 43 6f 6c 20 3d 20 70 54 61  pNew->nCol = pTa
228c2 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 61 73 73 65 72  b->nCol;.  asser
228c3 74 28 20 70 4e 65 77 2d 3e 6e 43 6f 6c 3e 30 20  t( pNew->nCol>0 
228c4 29 3b 0a 20 20 6e 41 6c 6c 6f 63 20 3d 20 28 28  );.  nAlloc = ((
228c5 28 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 29 2f 38  (pNew->nCol-1)/8
228c6 29 2a 38 29 2b 38 3b 0a 20 20 61 73 73 65 72 74  )*8)+8;.  assert
228c7 28 20 6e 41 6c 6c 6f 63 3e 3d 70 4e 65 77 2d 3e  ( nAlloc>=pNew->
228c8 6e 43 6f 6c 20 26 26 20 6e 41 6c 6c 6f 63 25 38  nCol && nAlloc%8
228c9 3d 3d 30 20 26 26 20 6e 41 6c 6c 6f 63 2d 70 4e  ==0 && nAlloc-pN
228ca 65 77 2d 3e 6e 43 6f 6c 3c 38 20 29 3b 0a 20 20  ew->nCol<8 );.  
228cb 70 4e 65 77 2d 3e 61 43 6f 6c 20 3d 20 28 43 6f  pNew->aCol = (Co
228cc 6c 75 6d 6e 2a 29 73 71 6c 69 74 65 33 44 62 4d  lumn*)sqlite3DbM
228cd 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
228ce 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a 6e 41 6c  zeof(Column)*nAl
228cf 6c 6f 63 29 3b 0a 20 20 70 4e 65 77 2d 3e 7a 4e  loc);.  pNew->zN
228d0 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ame = sqlite3MPr
228d1 69 6e 74 66 28 64 62 2c 20 22 73 71 6c 69 74 65  intf(db, "sqlite
228d2 5f 61 6c 74 65 72 74 61 62 5f 25 73 22 2c 20 70  _altertab_%s", p
228d3 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69  Tab->zName);.  i
228d4 66 28 20 21 70 4e 65 77 2d 3e 61 43 6f 6c 20 7c  f( !pNew->aCol |
228d5 7c 20 21 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 29  | !pNew->zName )
228d6 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63  {.    db->malloc
228d7 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
228d8 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f  goto exit_begin_
228d9 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a  add_column;.  }.
228da 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 61    memcpy(pNew->a
228db 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 2c  Col, pTab->aCol,
228dc 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a   sizeof(Column)*
228dd 70 4e 65 77 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 66  pNew->nCol);.  f
228de 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 65 77 2d 3e  or(i=0; i<pNew->
228df 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
228e0 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26  Column *pCol = &
228e1 70 4e 65 77 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a 20  pNew->aCol[i];. 
228e2 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d     pCol->zName =
228e3 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
228e4 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65  (db, pCol->zName
228e5 29 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 43 6f  );.    pCol->zCo
228e6 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 70 43 6f 6c  ll = 0;.    pCol
228e7 2d 3e 7a 54 79 70 65 20 3d 20 30 3b 0a 20 20 20  ->zType = 0;.   
228e8 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20 30   pCol->pDflt = 0
228e9 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 44 66 6c  ;.    pCol->zDfl
228ea 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65  t = 0;.  }.  pNe
228eb 77 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d  w->pSchema = db-
228ec 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d  >aDb[iDb].pSchem
228ed 61 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 43 6f  a;.  pNew->addCo
228ee 6c 4f 66 66 73 65 74 20 3d 20 70 54 61 62 2d 3e  lOffset = pTab->
228ef 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b 0a 20 20  addColOffset;.  
228f0 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a  pNew->nRef = 1;.
228f1 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 74 72  .  /* Begin a tr
228f2 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 69 6e  ansaction and in
228f3 63 72 65 6d 65 6e 74 20 74 68 65 20 73 63 68 65  crement the sche
228f4 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 2a 2f 0a 20  ma cookie.  */. 
228f5 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
228f6 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72  teOperation(pPar
228f7 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 76  se, 0, iDb);.  v
228f8 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
228f9 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
228fa 20 21 76 20 29 20 67 6f 74 6f 20 65 78 69 74 5f   !v ) goto exit_
228fb 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e  begin_add_column
228fc 3b 0a 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67  ;.  sqlite3Chang
228fd 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20  eCookie(pParse, 
228fe 69 44 62 29 3b 0a 0a 65 78 69 74 5f 62 65 67 69  iDb);..exit_begi
228ff 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3a 0a 20 20  n_add_column:.  
22900 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
22901 6c 65 74 65 28 64 62 2c 20 70 53 72 63 29 3b 0a  lete(db, pSrc);.
22902 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64    return;.}.#end
22903 69 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4c  if  /* SQLITE_AL
22904 54 45 52 5f 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a  TER_TABLE */../*
22905 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
22906 64 20 6f 66 20 61 6c 74 65 72 2e 63 20 2a 2a 2a  d of alter.c ***
22907 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22908 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22909 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2290a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
2290b 67 69 6e 20 66 69 6c 65 20 61 6e 61 6c 79 7a 65  gin file analyze
2290c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2290d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2290e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2290f 0a 2a 2a 20 32 30 30 35 20 4a 75 6c 79 20 38 0a  .** 2005 July 8.
22910 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
22911 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
22912 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
22913 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
22914 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
22915 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
22916 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
22917 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
22918 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
22919 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
2291a 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
2291b 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
2291c 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
2291d 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
2291e 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
2291f 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
22920 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
22921 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
22922 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22923 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22924 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22925 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
22926 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
22927 61 69 6e 73 20 63 6f 64 65 20 61 73 73 6f 63 69  ains code associ
22928 61 74 65 64 20 77 69 74 68 20 74 68 65 20 41 4e  ated with the AN
22929 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a  ALYZE command..*
2292a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 61  *.** @(#) $Id: a
2292b 6e 61 6c 79 7a 65 2e 63 2c 76 20 31 2e 35 32 20  nalyze.c,v 1.52 
2292c 32 30 30 39 2f 30 34 2f 31 36 20 31 37 3a 34 35  2009/04/16 17:45
2292d 3a 34 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :48 drh Exp $.*/
2292e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2292f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a 0a 2f 2a  OMIT_ANALYZE../*
22930 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
22931 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20   generates code 
22932 74 68 61 74 20 6f 70 65 6e 73 20 74 68 65 20 73  that opens the s
22933 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c  qlite_stat1 tabl
22934 65 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67  e for.** writing
22935 20 77 69 74 68 20 63 75 72 73 6f 72 20 69 53 74   with cursor iSt
22936 61 74 43 75 72 2e 20 49 66 20 74 68 65 20 6c 69  atCur. If the li
22937 62 72 61 72 79 20 77 61 73 20 62 75 69 6c 74 20  brary was built 
22938 77 69 74 68 20 74 68 65 0a 2a 2a 20 53 51 4c 49  with the.** SQLI
22939 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 20  TE_ENABLE_STAT2 
2293a 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 2c 20 74  macro defined, t
2293b 68 65 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 73  hen the sqlite_s
2293c 74 61 74 32 20 74 61 62 6c 65 20 69 73 0a 2a 2a  tat2 table is.**
2293d 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74   opened for writ
2293e 69 6e 67 20 75 73 69 6e 67 20 63 75 72 73 6f 72  ing using cursor
2293f 20 28 69 53 74 61 74 43 75 72 2b 31 29 0a 2a 2a   (iStatCur+1).**
22940 0a 2a 2a 20 49 66 20 74 68 65 20 73 71 6c 69 74  .** If the sqlit
22941 65 5f 73 74 61 74 31 20 74 61 62 6c 65 73 20 64  e_stat1 tables d
22942 6f 65 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73  oes not previous
22943 6c 79 20 65 78 69 73 74 2c 20 69 74 20 69 73 20  ly exist, it is 
22944 63 72 65 61 74 65 64 2e 0a 2a 2a 20 53 69 6d 69  created..** Simi
22945 6c 61 72 6c 79 2c 20 69 66 20 74 68 65 20 73 71  larly, if the sq
22946 6c 69 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65  lite_stat2 table
22947 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20   does not exist 
22948 61 6e 64 20 74 68 65 20 6c 69 62 72 61 72 79 0a  and the library.
22949 2a 2a 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77  ** is compiled w
2294a 69 74 68 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ith SQLITE_ENABL
2294b 45 5f 53 54 41 54 32 20 64 65 66 69 6e 65 64 2c  E_STAT2 defined,
2294c 20 69 74 20 69 73 20 63 72 65 61 74 65 64 2e 20   it is created. 
2294d 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20  .**.** Argument 
2294e 7a 57 68 65 72 65 20 6d 61 79 20 62 65 20 61 20  zWhere may be a 
2294f 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 75 66  pointer to a buf
22950 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61  fer containing a
22951 20 74 61 62 6c 65 20 6e 61 6d 65 2c 0a 2a 2a 20   table name,.** 
22952 6f 72 20 69 74 20 6d 61 79 20 62 65 20 61 20 4e  or it may be a N
22953 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 49 66 20  ULL pointer. If 
22954 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  it is not NULL, 
22955 74 68 65 6e 20 61 6c 6c 20 65 6e 74 72 69 65 73  then all entries
22956 20 69 6e 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74   in.** the sqlit
22957 65 5f 73 74 61 74 31 20 61 6e 64 20 28 69 66 20  e_stat1 and (if 
22958 61 70 70 6c 69 63 61 62 6c 65 29 20 73 71 6c 69  applicable) sqli
22959 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65 73 20  te_stat2 tables 
2295a 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69  associated.** wi
2295b 74 68 20 74 68 65 20 6e 61 6d 65 64 20 74 61 62  th the named tab
2295c 6c 65 20 61 72 65 20 64 65 6c 65 74 65 64 2e 20  le are deleted. 
2295d 49 66 20 7a 57 68 65 72 65 3d 3d 30 2c 20 74 68  If zWhere==0, th
2295e 65 6e 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72  en code is gener
2295f 61 74 65 64 0a 2a 2a 20 74 6f 20 64 65 6c 65 74  ated.** to delet
22960 65 20 61 6c 6c 20 73 74 61 74 20 74 61 62 6c 65  e all stat table
22961 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 61   entries..*/.sta
22962 74 69 63 20 76 6f 69 64 20 6f 70 65 6e 53 74 61  tic void openSta
22963 74 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20  tTable(.  Parse 
22964 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
22965 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
22966 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44  text */.  int iD
22967 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
22968 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
22969 65 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67  e we are looking
2296a 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74   in */.  int iSt
2296b 61 74 43 75 72 2c 20 20 20 20 20 20 20 20 20 20  atCur,          
2296c 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73 71 6c   /* Open the sql
2296d 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20  ite_stat1 table 
2296e 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72 20 2a  on this cursor *
2296f 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
22970 7a 57 68 65 72 65 20 20 20 20 20 20 2f 2a 20 44  zWhere      /* D
22971 65 6c 65 74 65 20 65 6e 74 72 69 65 73 20 61 73  elete entries as
22972 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
22973 69 73 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20  is table */.){. 
22974 20 73 74 61 74 69 63 20 73 74 72 75 63 74 20 7b   static struct {
22975 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
22976 2a 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73  *zName;.    cons
22977 74 20 63 68 61 72 20 2a 7a 43 6f 6c 73 3b 0a 20  t char *zCols;. 
22978 20 7d 20 61 54 61 62 6c 65 5b 5d 20 3d 20 7b 0a   } aTable[] = {.
22979 20 20 20 20 7b 20 22 73 71 6c 69 74 65 5f 73 74      { "sqlite_st
2297a 61 74 31 22 2c 20 22 74 62 6c 2c 69 64 78 2c 73  at1", "tbl,idx,s
2297b 74 61 74 22 20 7d 2c 0a 23 69 66 64 65 66 20 53  tat" },.#ifdef S
2297c 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41  QLITE_ENABLE_STA
2297d 54 32 0a 20 20 20 20 7b 20 22 73 71 6c 69 74 65  T2.    { "sqlite
2297e 5f 73 74 61 74 32 22 2c 20 22 74 62 6c 2c 69 64  _stat2", "tbl,id
2297f 78 2c 73 61 6d 70 6c 65 6e 6f 2c 73 61 6d 70 6c  x,sampleno,sampl
22980 65 22 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20 7d  e" },.#endif.  }
22981 3b 0a 0a 20 20 69 6e 74 20 61 52 6f 6f 74 5b 5d  ;..  int aRoot[]
22982 20 3d 20 7b 30 2c 20 30 7d 3b 0a 20 20 75 38 20   = {0, 0};.  u8 
22983 61 43 72 65 61 74 65 54 62 6c 5b 5d 20 3d 20 7b  aCreateTbl[] = {
22984 30 2c 20 30 7d 3b 0a 0a 20 20 69 6e 74 20 69 3b  0, 0};..  int i;
22985 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
22986 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44   pParse->db;.  D
22987 62 20 2a 70 44 62 3b 0a 20 20 56 64 62 65 20 2a  b *pDb;.  Vdbe *
22988 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
22989 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
2298a 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( v==0 ) return;
2298b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
2298c 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d  e3BtreeHoldsAllM
2298d 75 74 65 78 65 73 28 64 62 29 20 29 3b 0a 20 20  utexes(db) );.  
2298e 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 56  assert( sqlite3V
2298f 64 62 65 44 62 28 76 29 3d 3d 64 62 20 29 3b 0a  dbeDb(v)==db );.
22990 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62    pDb = &db->aDb
22991 5b 69 44 62 5d 3b 0a 0a 20 20 66 6f 72 28 69 3d  [iDb];..  for(i=
22992 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 61  0; i<ArraySize(a
22993 54 61 62 6c 65 29 3b 20 69 2b 2b 29 7b 0a 20 20  Table); i++){.  
22994 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
22995 61 62 20 3d 20 61 54 61 62 6c 65 5b 69 5d 2e 7a  ab = aTable[i].z
22996 4e 61 6d 65 3b 0a 20 20 20 20 54 61 62 6c 65 20  Name;.    Table 
22997 2a 70 53 74 61 74 3b 0a 20 20 20 20 69 66 28 20  *pStat;.    if( 
22998 28 70 53 74 61 74 20 3d 20 73 71 6c 69 74 65 33  (pStat = sqlite3
22999 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54  FindTable(db, zT
2299a 61 62 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 29 29  ab, pDb->zName))
2299b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ==0 ){.      /* 
2299c 54 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 5b  The sqlite_stat[
2299d 31 32 5d 20 74 61 62 6c 65 20 64 6f 65 73 20 6e  12] table does n
2299e 6f 74 20 65 78 69 73 74 2e 20 43 72 65 61 74 65  ot exist. Create
2299f 20 69 74 2e 20 4e 6f 74 65 20 74 68 61 74 20 61   it. Note that a
229a0 20 0a 20 20 20 20 20 20 2a 2a 20 73 69 64 65 2d   .      ** side-
229a1 65 66 66 65 63 74 20 6f 66 20 74 68 65 20 43 52  effect of the CR
229a2 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
229a3 6d 65 6e 74 20 69 73 20 74 6f 20 6c 65 61 76 65  ment is to leave
229a4 20 74 68 65 20 72 6f 6f 74 70 61 67 65 20 0a 20   the rootpage . 
229a5 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 6e       ** of the n
229a6 65 77 20 74 61 62 6c 65 20 69 6e 20 72 65 67 69  ew table in regi
229a7 73 74 65 72 20 70 50 61 72 73 65 2d 3e 72 65 67  ster pParse->reg
229a8 52 6f 6f 74 2e 20 54 68 69 73 20 69 73 20 69 6d  Root. This is im
229a9 70 6f 72 74 61 6e 74 20 0a 20 20 20 20 20 20 2a  portant .      *
229aa 2a 20 62 65 63 61 75 73 65 20 74 68 65 20 4f 70  * because the Op
229ab 65 6e 57 72 69 74 65 20 6f 70 63 6f 64 65 20 62  enWrite opcode b
229ac 65 6c 6f 77 20 77 69 6c 6c 20 62 65 20 6e 65 65  elow will be nee
229ad 64 69 6e 67 20 69 74 2e 20 2a 2f 0a 20 20 20 20  ding it. */.    
229ae 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
229af 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20  arse(pParse,.   
229b0 20 20 20 20 20 20 20 22 43 52 45 41 54 45 20 54         "CREATE T
229b1 41 42 4c 45 20 25 51 2e 25 73 28 25 73 29 22 2c  ABLE %Q.%s(%s)",
229b2 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 54 61   pDb->zName, zTa
229b3 62 2c 20 61 54 61 62 6c 65 5b 69 5d 2e 7a 43 6f  b, aTable[i].zCo
229b4 6c 73 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  ls.      );.    
229b5 20 20 61 52 6f 6f 74 5b 69 5d 20 3d 20 70 50 61    aRoot[i] = pPa
229b6 72 73 65 2d 3e 72 65 67 52 6f 6f 74 3b 0a 20 20  rse->regRoot;.  
229b7 20 20 20 20 61 43 72 65 61 74 65 54 62 6c 5b 69      aCreateTbl[i
229b8 5d 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65  ] = 1;.    }else
229b9 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74  {.      /* The t
229ba 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69  able already exi
229bb 73 74 73 2e 20 49 66 20 7a 57 68 65 72 65 20 69  sts. If zWhere i
229bc 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 64 65 6c 65  s not NULL, dele
229bd 74 65 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 0a  te all entries .
229be 20 20 20 20 20 20 2a 2a 20 61 73 73 6f 63 69 61        ** associa
229bf 74 65 64 20 77 69 74 68 20 74 68 65 20 74 61 62  ted with the tab
229c0 6c 65 20 7a 57 68 65 72 65 2e 20 49 66 20 7a 57  le zWhere. If zW
229c1 68 65 72 65 20 69 73 20 4e 55 4c 4c 2c 20 64 65  here is NULL, de
229c2 6c 65 74 65 20 74 68 65 0a 20 20 20 20 20 20 2a  lete the.      *
229c3 2a 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74  * entire content
229c4 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20  s of the table. 
229c5 2a 2f 0a 20 20 20 20 20 20 61 52 6f 6f 74 5b 69  */.      aRoot[i
229c6 5d 20 3d 20 70 53 74 61 74 2d 3e 74 6e 75 6d 3b  ] = pStat->tnum;
229c7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 54 61  .      sqlite3Ta
229c8 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20  bleLock(pParse, 
229c9 69 44 62 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 31  iDb, aRoot[i], 1
229ca 2c 20 7a 54 61 62 29 3b 0a 20 20 20 20 20 20 69  , zTab);.      i
229cb 66 28 20 7a 57 68 65 72 65 20 29 7b 0a 20 20 20  f( zWhere ){.   
229cc 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74       sqlite3Nest
229cd 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
229ce 20 20 20 20 20 20 20 20 20 20 20 22 44 45 4c 45             "DELE
229cf 54 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57 48  TE FROM %Q.%s WH
229d0 45 52 45 20 74 62 6c 3d 25 51 22 2c 20 70 44 62  ERE tbl=%Q", pDb
229d1 2d 3e 7a 4e 61 6d 65 2c 20 7a 54 61 62 2c 20 7a  ->zName, zTab, z
229d2 57 68 65 72 65 0a 20 20 20 20 20 20 20 20 29 3b  Where.        );
229d3 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
229d4 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 71 6c        /* The sql
229d5 69 74 65 5f 73 74 61 74 5b 31 32 5d 20 74 61 62  ite_stat[12] tab
229d6 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  le already exist
229d7 73 2e 20 20 44 65 6c 65 74 65 20 61 6c 6c 20 72  s.  Delete all r
229d8 6f 77 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ows. */.        
229d9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
229da 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 61  2(v, OP_Clear, a
229db 52 6f 6f 74 5b 69 5d 2c 20 69 44 62 29 3b 0a 20  Root[i], iDb);. 
229dc 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
229dd 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20  ..  /* Open the 
229de 73 71 6c 69 74 65 5f 73 74 61 74 5b 31 32 5d 20  sqlite_stat[12] 
229df 74 61 62 6c 65 73 20 66 6f 72 20 77 72 69 74 69  tables for writi
229e0 6e 67 2e 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  ng. */.  for(i=0
229e1 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 61 54  ; i<ArraySize(aT
229e2 61 62 6c 65 29 3b 20 69 2b 2b 29 7b 0a 20 20 20  able); i++){.   
229e3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
229e4 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  p3(v, OP_OpenWri
229e5 74 65 2c 20 69 53 74 61 74 43 75 72 2b 69 2c 20  te, iStatCur+i, 
229e6 61 52 6f 6f 74 5b 69 5d 2c 20 69 44 62 29 3b 0a  aRoot[i], iDb);.
229e7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
229e8 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28  hangeP4(v, -1, (
229e9 63 68 61 72 20 2a 29 33 2c 20 50 34 5f 49 4e 54  char *)3, P4_INT
229ea 33 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  32);.    sqlite3
229eb 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
229ec 61 43 72 65 61 74 65 54 62 6c 5b 69 5d 29 3b 0a  aCreateTbl[i]);.
229ed 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e    }.}../*.** Gen
229ee 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64 6f  erate code to do
229ef 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20   an analysis of 
229f0 61 6c 6c 20 69 6e 64 69 63 65 73 20 61 73 73 6f  all indices asso
229f1 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 61  ciated with.** a
229f2 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 2e 0a 2a   single table..*
229f3 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 6e  /.static void an
229f4 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 0a 20  alyzeOneTable(. 
229f5 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
229f6 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
229f7 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
229f8 70 54 61 62 2c 20 20 20 20 20 2f 2a 20 54 61 62  pTab,     /* Tab
229f9 6c 65 20 77 68 6f 73 65 20 69 6e 64 69 63 65 73  le whose indices
229fa 20 61 72 65 20 74 6f 20 62 65 20 61 6e 61 6c 79   are to be analy
229fb 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74  zed */.  int iSt
229fc 61 74 43 75 72 2c 20 20 20 20 2f 2a 20 49 6e 64  atCur,    /* Ind
229fd 65 78 20 6f 66 20 56 64 62 65 43 75 72 73 6f 72  ex of VdbeCursor
229fe 20 74 68 61 74 20 77 72 69 74 65 73 20 74 68 65   that writes the
229ff 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
22a00 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 65  ble */.  int iMe
22a01 6d 20 20 20 20 20 20 20 20 20 2f 2a 20 41 76 61  m         /* Ava
22a02 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 6c 6f  ilable memory lo
22a03 63 61 74 69 6f 6e 73 20 62 65 67 69 6e 20 68 65  cations begin he
22a04 72 65 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  re */.){.  sqlit
22a05 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
22a06 3e 64 62 3b 20 20 20 20 2f 2a 20 44 61 74 61 62  >db;    /* Datab
22a07 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ase handle */.  
22a08 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20  Index *pIdx;    
22a09 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
22a0a 41 6e 20 69 6e 64 65 78 20 74 6f 20 62 65 69 6e  An index to bein
22a0b 67 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20  g analyzed */.  
22a0c 69 6e 74 20 69 49 64 78 43 75 72 3b 20 20 20 20  int iIdxCur;    
22a0d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
22a0e 43 75 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 69  Cursor open on i
22a0f 6e 64 65 78 20 62 65 69 6e 67 20 61 6e 61 6c 79  ndex being analy
22a10 7a 65 64 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76  zed */.  Vdbe *v
22a11 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
22a12 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72        /* The vir
22a13 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 62 65 69  tual machine bei
22a14 6e 67 20 62 75 69 6c 74 20 75 70 20 2a 2f 0a 20  ng built up */. 
22a15 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
22a16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
22a17 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
22a18 0a 20 20 69 6e 74 20 74 6f 70 4f 66 4c 6f 6f 70  .  int topOfLoop
22a19 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
22a1a 2f 2a 20 54 68 65 20 74 6f 70 20 6f 66 20 74 68  /* The top of th
22a1b 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20  e loop */.  int 
22a1c 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20 20 20 20 20  endOfLoop;      
22a1d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
22a1e 65 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  end of the loop 
22a1f 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20  */.  int addr;  
22a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22a21 20 20 2f 2a 20 54 68 65 20 61 64 64 72 65 73 73    /* The address
22a22 20 6f 66 20 61 6e 20 69 6e 73 74 72 75 63 74 69   of an instructi
22a23 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  on */.  int iDb;
22a24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22a25 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
22a26 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
22a27 6e 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20 69  ning pTab */.  i
22a28 6e 74 20 72 65 67 54 61 62 6e 61 6d 65 20 3d 20  nt regTabname = 
22a29 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 2f 2a 20 52  iMem++;     /* R
22a2a 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 69  egister containi
22a2b 6e 67 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f  ng table name */
22a2c 0a 20 20 69 6e 74 20 72 65 67 49 64 78 6e 61 6d  .  int regIdxnam
22a2d 65 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20  e = iMem++;     
22a2e 2f 2a 20 52 65 67 69 73 74 65 72 20 63 6f 6e 74  /* Register cont
22a2f 61 69 6e 69 6e 67 20 69 6e 64 65 78 20 6e 61 6d  aining index nam
22a30 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 53 61  e */.  int regSa
22a31 6d 70 6c 65 6e 6f 20 3d 20 69 4d 65 6d 2b 2b 3b  mpleno = iMem++;
22a32 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
22a33 63 6f 6e 74 61 69 6e 69 6e 67 20 6e 65 78 74 20  containing next 
22a34 73 61 6d 70 6c 65 20 6e 75 6d 62 65 72 20 2a 2f  sample number */
22a35 0a 20 20 69 6e 74 20 72 65 67 43 6f 6c 20 3d 20  .  int regCol = 
22a36 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20  iMem++;         
22a37 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 61 20  /* Content of a 
22a38 63 6f 6c 75 6d 6e 20 61 6e 61 6c 79 7a 65 64 20  column analyzed 
22a39 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72  table */.  int r
22a3a 65 67 52 65 63 20 3d 20 69 4d 65 6d 2b 2b 3b 20  egRec = iMem++; 
22a3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
22a3c 74 65 72 20 68 6f 6c 64 69 6e 67 20 63 6f 6d 70  ter holding comp
22a3d 6c 65 74 65 64 20 72 65 63 6f 72 64 20 2a 2f 0a  leted record */.
22a3e 20 20 69 6e 74 20 72 65 67 54 65 6d 70 20 3d 20    int regTemp = 
22a3f 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 2f  iMem++;        /
22a40 2a 20 54 65 6d 70 6f 72 61 72 79 20 75 73 65 20  * Temporary use 
22a41 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e  register */.  in
22a42 74 20 72 65 67 52 6f 77 69 64 20 3d 20 69 4d 65  t regRowid = iMe
22a43 6d 2b 2b 3b 20 20 20 20 20 20 20 2f 2a 20 52 6f  m++;       /* Ro
22a44 77 69 64 20 66 6f 72 20 74 68 65 20 69 6e 73 65  wid for the inse
22a45 72 74 65 64 20 72 65 63 6f 72 64 20 2a 2f 0a 0a  rted record */..
22a46 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
22a47 41 42 4c 45 5f 53 54 41 54 32 0a 20 20 69 6e 74  ABLE_STAT2.  int
22a48 20 72 65 67 54 65 6d 70 32 20 3d 20 69 4d 65 6d   regTemp2 = iMem
22a49 2b 2b 3b 20 20 20 20 20 20 20 2f 2a 20 54 65 6d  ++;       /* Tem
22a4a 70 6f 72 61 72 79 20 75 73 65 20 72 65 67 69 73  porary use regis
22a4b 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ter */.  int reg
22a4c 53 61 6d 70 6c 65 72 65 63 6e 6f 20 3d 20 69 4d  Samplerecno = iM
22a4d 65 6d 2b 2b 3b 20 2f 2a 20 49 6e 64 65 78 20 6f  em++; /* Index o
22a4e 66 20 6e 65 78 74 20 73 61 6d 70 6c 65 20 74 6f  f next sample to
22a4f 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74   record */.  int
22a50 20 72 65 67 52 65 63 6e 6f 20 3d 20 69 4d 65 6d   regRecno = iMem
22a51 2b 2b 3b 20 20 20 20 20 20 20 2f 2a 20 43 75 72  ++;       /* Cur
22a52 72 65 6e 74 20 73 61 6d 70 6c 65 20 69 6e 64 65  rent sample inde
22a53 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4c 61  x */.  int regLa
22a54 73 74 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20  st = iMem++;    
22a55 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
22a56 6c 61 73 74 20 73 61 6d 70 6c 65 20 74 6f 20 72  last sample to r
22a57 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 72  ecord */.  int r
22a58 65 67 46 69 72 73 74 20 3d 20 69 4d 65 6d 2b 2b  egFirst = iMem++
22a59 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78  ;       /* Index
22a5a 20 6f 66 20 66 69 72 73 74 20 73 61 6d 70 6c 65   of first sample
22a5b 20 74 6f 20 72 65 63 6f 72 64 20 2a 2f 0a 23 65   to record */.#e
22a5c 6e 64 69 66 0a 0a 20 20 76 20 3d 20 73 71 6c 69  ndif..  v = sqli
22a5d 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
22a5e 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 7c  e);.  if( v==0 |
22a5f 7c 20 4e 45 56 45 52 28 70 54 61 62 3d 3d 30 29  | NEVER(pTab==0)
22a60 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65 78   || pTab->pIndex
22a61 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 44 6f  ==0 ){.    /* Do
22a62 20 6e 6f 20 61 6e 61 6c 79 73 69 73 20 66 6f 72   no analysis for
22a63 20 74 61 62 6c 65 73 20 74 68 61 74 20 68 61 76   tables that hav
22a64 65 20 6e 6f 20 69 6e 64 69 63 65 73 20 2a 2f 0a  e no indices */.
22a65 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
22a66 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
22a67 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75  3BtreeHoldsAllMu
22a68 74 65 78 65 73 28 64 62 29 20 29 3b 0a 20 20 69  texes(db) );.  i
22a69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
22a6a 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54  maToIndex(db, pT
22a6b 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
22a6c 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 29  assert( iDb>=0 )
22a6d 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
22a6e 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
22a6f 49 4f 4e 0a 20 20 69 66 28 20 73 71 6c 69 74 65  ION.  if( sqlite
22a70 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
22a71 65 2c 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 5a  e, SQLITE_ANALYZ
22a72 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  E, pTab->zName, 
22a73 30 2c 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62  0,.      db->aDb
22a74 5b 69 44 62 5d 2e 7a 4e 61 6d 65 20 29 20 29 7b  [iDb].zName ) ){
22a75 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
22a76 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 45 73  .#endif..  /* Es
22a77 74 61 62 6c 69 73 68 20 61 20 72 65 61 64 2d 6c  tablish a read-l
22a78 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  ock on the table
22a79 20 61 74 20 74 68 65 20 73 68 61 72 65 64 2d 63   at the shared-c
22a7a 61 63 68 65 20 6c 65 76 65 6c 2e 20 2a 2f 0a 20  ache level. */. 
22a7b 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63   sqlite3TableLoc
22a7c 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70  k(pParse, iDb, p
22a7d 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54  Tab->tnum, 0, pT
22a7e 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 69  ab->zName);..  i
22a7f 49 64 78 43 75 72 20 3d 20 70 50 61 72 73 65 2d  IdxCur = pParse-
22a80 3e 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f 72 28 70  >nTab++;.  for(p
22a81 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
22a82 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
22a83 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  x->pNext){.    i
22a84 6e 74 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e  nt nCol = pIdx->
22a85 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 4b 65 79  nColumn;.    Key
22a86 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73 71 6c  Info *pKey = sql
22a87 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f  ite3IndexKeyinfo
22a88 28 70 50 61 72 73 65 2c 20 70 49 64 78 29 3b 0a  (pParse, pIdx);.
22a89 0a 20 20 20 20 69 66 28 20 69 4d 65 6d 2b 31 2b  .    if( iMem+1+
22a8a 28 6e 43 6f 6c 2a 32 29 3e 70 50 61 72 73 65 2d  (nCol*2)>pParse-
22a8b 3e 6e 4d 65 6d 20 29 7b 0a 20 20 20 20 20 20 70  >nMem ){.      p
22a8c 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 69 4d  Parse->nMem = iM
22a8d 65 6d 2b 31 2b 28 6e 43 6f 6c 2a 32 29 3b 0a 20  em+1+(nCol*2);. 
22a8e 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f 70 65     }..    /* Ope
22a8f 6e 20 61 20 63 75 72 73 6f 72 20 74 6f 20 74 68  n a cursor to th
22a90 65 20 69 6e 64 65 78 20 74 6f 20 62 65 20 61 6e  e index to be an
22a91 61 6c 79 7a 65 64 2e 20 2a 2f 0a 20 20 20 20 61  alyzed. */.    a
22a92 73 73 65 72 74 28 20 69 44 62 3d 3d 73 71 6c 69  ssert( iDb==sqli
22a93 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
22a94 28 64 62 2c 20 70 49 64 78 2d 3e 70 53 63 68 65  (db, pIdx->pSche
22a95 6d 61 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ma) );.    sqlit
22a96 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
22a97 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 49 64  OP_OpenRead, iId
22a98 78 43 75 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d  xCur, pIdx->tnum
22a99 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20 20 28  , iDb,.        (
22a9a 63 68 61 72 20 2a 29 70 4b 65 79 2c 20 50 34 5f  char *)pKey, P4_
22a9b 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
22a9c 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e  ;.    VdbeCommen
22a9d 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49 64 78  t((v, "%s", pIdx
22a9e 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 0a 20 20 20 20  ->zName));..    
22a9f 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20  /* Populate the 
22aa0 72 65 67 69 73 74 65 72 73 20 63 6f 6e 74 61 69  registers contai
22aa1 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 61  ning the table a
22aa2 6e 64 20 69 6e 64 65 78 20 6e 61 6d 65 73 2e 20  nd index names. 
22aa3 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d  */.    if( pTab-
22aa4 3e 70 49 6e 64 65 78 3d 3d 70 49 64 78 20 29 7b  >pIndex==pIdx ){
22aa5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
22aa6 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
22aa7 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 54 61  tring8, 0, regTa
22aa8 62 6e 61 6d 65 2c 20 30 2c 20 70 54 61 62 2d 3e  bname, 0, pTab->
22aa9 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d  zName, 0);.    }
22aaa 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
22aab 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
22aac 69 6e 67 38 2c 20 30 2c 20 72 65 67 49 64 78 6e  ing8, 0, regIdxn
22aad 61 6d 65 2c 20 30 2c 20 70 49 64 78 2d 3e 7a 4e  ame, 0, pIdx->zN
22aae 61 6d 65 2c 20 30 29 3b 0a 0a 23 69 66 64 65 66  ame, 0);..#ifdef
22aaf 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53   SQLITE_ENABLE_S
22ab0 54 41 54 32 0a 0a 20 20 20 20 2f 2a 20 49 66 20  TAT2..    /* If 
22ab1 74 68 69 73 20 69 74 65 72 61 74 69 6f 6e 20 6f  this iteration o
22ab2 66 20 74 68 65 20 6c 6f 6f 70 20 69 73 20 67 65  f the loop is ge
22ab3 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20 74 6f  nerating code to
22ab4 20 61 6e 61 6c 79 7a 65 20 74 68 65 0a 20 20 20   analyze the.   
22ab5 20 2a 2a 20 66 69 72 73 74 20 69 6e 64 65 78 20   ** first index 
22ab6 69 6e 20 74 68 65 20 70 54 61 62 2d 3e 70 49 6e  in the pTab->pIn
22ab7 64 65 78 20 6c 69 73 74 2c 20 74 68 65 6e 20 72  dex list, then r
22ab8 65 67 69 73 74 65 72 20 72 65 67 4c 61 73 74 20  egister regLast 
22ab9 68 61 73 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 62  has.    ** not b
22aba 65 65 6e 20 70 6f 70 75 6c 61 74 65 64 2e 20 49  een populated. I
22abb 6e 20 74 68 69 73 20 63 61 73 65 20 70 6f 70 75  n this case popu
22abc 6c 61 74 65 20 69 74 20 6e 6f 77 2e 20 20 2a 2f  late it now.  */
22abd 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 70  .    if( pTab->p
22abe 49 6e 64 65 78 3d 3d 70 49 64 78 20 29 7b 0a 20  Index==pIdx ){. 
22abf 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
22ac0 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
22ac1 65 67 65 72 2c 20 53 51 4c 49 54 45 5f 49 4e 44  eger, SQLITE_IND
22ac2 45 58 5f 53 41 4d 50 4c 45 53 2c 20 72 65 67 53  EX_SAMPLES, regS
22ac3 61 6d 70 6c 65 72 65 63 6e 6f 29 3b 0a 20 20 20  amplerecno);.   
22ac4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22ac5 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
22ac6 65 72 2c 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  er, SQLITE_INDEX
22ac7 5f 53 41 4d 50 4c 45 53 2a 32 2d 31 2c 20 72 65  _SAMPLES*2-1, re
22ac8 67 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 73 71  gTemp);.      sq
22ac9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
22aca 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 53  v, OP_Integer, S
22acb 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50  QLITE_INDEX_SAMP
22acc 4c 45 53 2a 32 2c 20 72 65 67 54 65 6d 70 32 29  LES*2, regTemp2)
22acd 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
22ace 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
22acf 5f 43 6f 75 6e 74 2c 20 69 49 64 78 43 75 72 2c  _Count, iIdxCur,
22ad0 20 72 65 67 4c 61 73 74 29 3b 0a 20 20 20 20 20   regLast);.     
22ad1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22ad2 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  p2(v, OP_Null, 0
22ad3 2c 20 72 65 67 46 69 72 73 74 29 3b 0a 20 20 20  , regFirst);.   
22ad4 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
22ad5 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
22ad6 50 5f 4c 74 2c 20 72 65 67 53 61 6d 70 6c 65 72  P_Lt, regSampler
22ad7 65 63 6e 6f 2c 20 30 2c 20 72 65 67 4c 61 73 74  ecno, 0, regLast
22ad8 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
22ad9 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
22ada 5f 44 69 76 69 64 65 2c 20 72 65 67 54 65 6d 70  _Divide, regTemp
22adb 32 2c 20 72 65 67 4c 61 73 74 2c 20 72 65 67 46  2, regLast, regF
22adc 69 72 73 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  irst);.      sql
22add 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
22ade 2c 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 2c 20 72  , OP_Multiply, r
22adf 65 67 4c 61 73 74 2c 20 72 65 67 54 65 6d 70 2c  egLast, regTemp,
22ae0 20 72 65 67 4c 61 73 74 29 3b 0a 20 20 20 20 20   regLast);.     
22ae1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22ae2 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  p2(v, OP_AddImm,
22ae3 20 72 65 67 4c 61 73 74 2c 20 53 51 4c 49 54 45   regLast, SQLITE
22ae4 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 2a 32  _INDEX_SAMPLES*2
22ae5 2d 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  -2);.      sqlit
22ae6 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
22ae7 4f 50 5f 44 69 76 69 64 65 2c 20 20 72 65 67 54  OP_Divide,  regT
22ae8 65 6d 70 32 2c 20 72 65 67 4c 61 73 74 2c 20 72  emp2, regLast, r
22ae9 65 67 4c 61 73 74 29 3b 0a 20 20 20 20 20 20 73  egLast);.      s
22aea 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
22aeb 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 20  re(v, addr);.   
22aec 20 7d 0a 0a 20 20 20 20 2f 2a 20 5a 65 72 6f 20   }..    /* Zero 
22aed 74 68 65 20 72 65 67 53 61 6d 70 6c 65 6e 6f 20  the regSampleno 
22aee 61 6e 64 20 72 65 67 52 65 63 6e 6f 20 72 65 67  and regRecno reg
22aef 69 73 74 65 72 73 2e 20 2a 2f 0a 20 20 20 20 73  isters. */.    s
22af0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
22af1 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
22af2 30 2c 20 72 65 67 53 61 6d 70 6c 65 6e 6f 29 3b  0, regSampleno);
22af3 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
22af4 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
22af5 65 67 65 72 2c 20 30 2c 20 72 65 67 52 65 63 6e  eger, 0, regRecn
22af6 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  o);.    sqlite3V
22af7 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
22af8 43 6f 70 79 2c 20 72 65 67 46 69 72 73 74 2c 20  Copy, regFirst, 
22af9 72 65 67 53 61 6d 70 6c 65 72 65 63 6e 6f 29 3b  regSamplerecno);
22afa 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
22afb 54 68 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d  The block of mem
22afc 6f 72 79 20 63 65 6c 6c 73 20 69 6e 69 74 69 61  ory cells initia
22afd 6c 69 7a 65 64 20 68 65 72 65 20 69 73 20 75 73  lized here is us
22afe 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 2e 0a 20  ed as follows.. 
22aff 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
22b00 69 4d 65 6d 3a 20 20 20 20 20 20 20 20 20 20 20  iMem:           
22b01 20 20 20 20 20 0a 20 20 20 20 2a 2a 20 20 20 20       .    **    
22b02 20 20 20 20 54 68 65 20 74 6f 74 61 6c 20 6e 75      The total nu
22b03 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20  mber of rows in 
22b04 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a  the table..    *
22b05 2a 0a 20 20 20 20 2a 2a 20 20 20 20 69 4d 65 6d  *.    **    iMem
22b06 2b 31 20 2e 2e 20 69 4d 65 6d 2b 6e 43 6f 6c 3a  +1 .. iMem+nCol:
22b07 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20   .    **        
22b08 4e 75 6d 62 65 72 20 6f 66 20 64 69 73 74 69 6e  Number of distin
22b09 63 74 20 65 6e 74 72 69 65 73 20 69 6e 20 69 6e  ct entries in in
22b0a 64 65 78 20 63 6f 6e 73 69 64 65 72 69 6e 67 20  dex considering 
22b0b 74 68 65 20 0a 20 20 20 20 2a 2a 20 20 20 20 20  the .    **     
22b0c 20 20 20 6c 65 66 74 2d 6d 6f 73 74 20 4e 20 63     left-most N c
22b0d 6f 6c 75 6d 6e 73 20 6f 6e 6c 79 2c 20 77 68 65  olumns only, whe
22b0e 72 65 20 4e 20 69 73 20 62 65 74 77 65 65 6e 20  re N is between 
22b0f 31 20 61 6e 64 20 6e 43 6f 6c 2c 20 0a 20 20 20  1 and nCol, .   
22b10 20 2a 2a 20 20 20 20 20 20 20 20 69 6e 63 6c 75   **        inclu
22b11 73 69 76 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  sive..    **.   
22b12 20 2a 2a 20 20 20 20 69 4d 65 6d 2b 6e 43 6f 6c   **    iMem+nCol
22b13 2b 31 20 2e 2e 20 4d 65 6d 2b 32 2a 6e 43 6f 6c  +1 .. Mem+2*nCol
22b14 3a 20 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  :  .    **      
22b15 20 20 50 72 65 76 69 6f 75 73 20 76 61 6c 75 65    Previous value
22b16 20 6f 66 20 69 6e 64 65 78 65 64 20 63 6f 6c 75   of indexed colu
22b17 6d 6e 73 2c 20 66 72 6f 6d 20 6c 65 66 74 20 74  mns, from left t
22b18 6f 20 72 69 67 68 74 2e 0a 20 20 20 20 2a 2a 0a  o right..    **.
22b19 20 20 20 20 2a 2a 20 43 65 6c 6c 73 20 69 4d 65      ** Cells iMe
22b1a 6d 20 74 68 72 6f 75 67 68 20 69 4d 65 6d 2b 6e  m through iMem+n
22b1b 43 6f 6c 20 61 72 65 20 69 6e 69 74 69 61 6c 69  Col are initiali
22b1c 7a 65 64 20 74 6f 20 30 2e 20 54 68 65 20 6f 74  zed to 0. The ot
22b1d 68 65 72 73 20 61 72 65 20 0a 20 20 20 20 2a 2a  hers are .    **
22b1e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20   initialized to 
22b1f 63 6f 6e 74 61 69 6e 20 61 6e 20 53 51 4c 20 4e  contain an SQL N
22b20 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ULL..    */.    
22b21 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 6e 43 6f 6c  for(i=0; i<=nCol
22b22 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; i++){.      sq
22b23 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
22b24 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
22b25 2c 20 69 4d 65 6d 2b 69 29 3b 0a 20 20 20 20 7d  , iMem+i);.    }
22b26 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
22b27 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
22b28 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22b29 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
22b2a 30 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69 2b 31  0, iMem+nCol+i+1
22b2b 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
22b2c 20 53 74 61 72 74 20 74 68 65 20 61 6e 61 6c 79   Start the analy
22b2d 73 69 73 20 6c 6f 6f 70 2e 20 54 68 69 73 20 6c  sis loop. This l
22b2e 6f 6f 70 20 72 75 6e 73 20 74 68 72 6f 75 67 68  oop runs through
22b2f 20 61 6c 6c 20 74 68 65 20 65 6e 74 72 69 65 73   all the entries
22b30 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 69   in.    ** the i
22b31 6e 64 65 78 20 62 2d 74 72 65 65 2e 20 20 2a 2f  ndex b-tree.  */
22b32 0a 20 20 20 20 65 6e 64 4f 66 4c 6f 6f 70 20 3d  .    endOfLoop =
22b33 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
22b34 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 73 71  Label(v);.    sq
22b35 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
22b36 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 49  v, OP_Rewind, iI
22b37 64 78 43 75 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70  dxCur, endOfLoop
22b38 29 3b 0a 20 20 20 20 74 6f 70 4f 66 4c 6f 6f 70  );.    topOfLoop
22b39 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
22b3a 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20  rrentAddr(v);.  
22b3b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22b3c 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d  Op2(v, OP_AddImm
22b3d 2c 20 69 4d 65 6d 2c 20 31 29 3b 0a 0a 20 20 20  , iMem, 1);..   
22b3e 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
22b3f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; i++){.      sq
22b40 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
22b41 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49  v, OP_Column, iI
22b42 64 78 43 75 72 2c 20 69 2c 20 72 65 67 43 6f 6c  dxCur, i, regCol
22b43 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  );.#ifdef SQLITE
22b44 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 20 20  _ENABLE_STAT2.  
22b45 20 20 20 20 69 66 28 20 69 3d 3d 30 20 29 7b 0a      if( i==0 ){.
22b46 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b          /* Check
22b47 20 69 66 20 74 68 65 20 72 65 63 6f 72 64 20 74   if the record t
22b48 68 61 74 20 63 75 72 73 6f 72 20 69 49 64 78 43  hat cursor iIdxC
22b49 75 72 20 70 6f 69 6e 74 73 20 74 6f 20 63 6f 6e  ur points to con
22b4a 74 61 69 6e 73 20 61 0a 20 20 20 20 20 20 20 20  tains a.        
22b4b 2a 2a 20 76 61 6c 75 65 20 74 68 61 74 20 73 68  ** value that sh
22b4c 6f 75 6c 64 20 62 65 20 73 74 6f 72 65 64 20 69  ould be stored i
22b4d 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  n the sqlite_sta
22b4e 74 32 20 74 61 62 6c 65 2e 20 49 66 20 73 6f 2c  t2 table. If so,
22b4f 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72  .        ** stor
22b50 65 20 69 74 2e 20 20 2a 2f 0a 20 20 20 20 20 20  e it.  */.      
22b51 20 20 69 6e 74 20 6e 65 20 3d 20 73 71 6c 69 74    int ne = sqlit
22b52 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
22b53 4f 50 5f 4e 65 2c 20 72 65 67 52 65 63 6e 6f 2c  OP_Ne, regRecno,
22b54 20 30 2c 20 72 65 67 53 61 6d 70 6c 65 72 65 63   0, regSamplerec
22b55 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  no);.        ass
22b56 65 72 74 28 20 72 65 67 54 61 62 6e 61 6d 65 2b  ert( regTabname+
22b57 31 3d 3d 72 65 67 49 64 78 6e 61 6d 65 20 0a 20  1==regIdxname . 
22b58 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 72              && r
22b59 65 67 54 61 62 6e 61 6d 65 2b 32 3d 3d 72 65 67  egTabname+2==reg
22b5a 53 61 6d 70 6c 65 6e 6f 0a 20 20 20 20 20 20 20  Sampleno.       
22b5b 20 20 20 20 20 20 26 26 20 72 65 67 54 61 62 6e        && regTabn
22b5c 61 6d 65 2b 33 3d 3d 72 65 67 43 6f 6c 0a 20 20  ame+3==regCol.  
22b5d 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20        );.       
22b5e 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
22b5f 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45 5f 4a  geP5(v, SQLITE_J
22b60 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20  UMPIFNULL);.    
22b61 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22b62 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp4(v, OP_Make
22b63 52 65 63 6f 72 64 2c 20 72 65 67 54 61 62 6e 61  Record, regTabna
22b64 6d 65 2c 20 34 2c 20 72 65 67 52 65 63 2c 20 22  me, 4, regRec, "
22b65 61 61 61 62 22 2c 20 30 29 3b 0a 20 20 20 20 20  aaab", 0);.     
22b66 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22b67 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f  dOp2(v, OP_NewRo
22b68 77 69 64 2c 20 69 53 74 61 74 43 75 72 2b 31 2c  wid, iStatCur+1,
22b69 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
22b6a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22b6b 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65  ddOp3(v, OP_Inse
22b6c 72 74 2c 20 69 53 74 61 74 43 75 72 2b 31 2c 20  rt, iStatCur+1, 
22b6d 72 65 67 52 65 63 2c 20 72 65 67 52 6f 77 69 64  regRec, regRowid
22b6e 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 43  );..        /* C
22b6f 61 6c 63 75 6c 61 74 65 20 6e 65 77 20 76 61 6c  alculate new val
22b70 75 65 73 20 66 6f 72 20 72 65 67 53 61 6d 70 6c  ues for regSampl
22b71 65 72 65 63 6e 6f 20 61 6e 64 20 72 65 67 53 61  erecno and regSa
22b72 6d 70 6c 65 6e 6f 2e 0a 20 20 20 20 20 20 20 20  mpleno..        
22b73 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  **.        **   
22b74 73 61 6d 70 6c 65 6e 6f 20 3d 20 73 61 6d 70 6c  sampleno = sampl
22b75 65 6e 6f 20 2b 20 31 0a 20 20 20 20 20 20 20 20  eno + 1.        
22b76 2a 2a 20 20 20 73 61 6d 70 6c 65 72 65 63 6e 6f  **   samplerecno
22b77 20 3d 20 73 61 6d 70 6c 65 72 65 63 6e 6f 2b 28   = samplerecno+(
22b78 72 65 6d 61 69 6e 69 6e 67 20 72 65 63 6f 72 64  remaining record
22b79 73 29 2f 28 72 65 6d 61 69 6e 69 6e 67 20 73 61  s)/(remaining sa
22b7a 6d 70 6c 65 73 29 0a 20 20 20 20 20 20 20 20 2a  mples).        *
22b7b 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  /.        sqlite
22b7c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
22b7d 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67 53 61 6d  P_AddImm, regSam
22b7e 70 6c 65 6e 6f 2c 20 31 29 3b 0a 20 20 20 20 20  pleno, 1);.     
22b7f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22b80 64 4f 70 33 28 76 2c 20 4f 50 5f 53 75 62 74 72  dOp3(v, OP_Subtr
22b81 61 63 74 2c 20 72 65 67 52 65 63 6e 6f 2c 20 72  act, regRecno, r
22b82 65 67 4c 61 73 74 2c 20 72 65 67 54 65 6d 70 29  egLast, regTemp)
22b83 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
22b84 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
22b85 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67 54 65 6d  P_AddImm, regTem
22b86 70 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20  p, -1);.        
22b87 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22b88 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
22b89 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41   SQLITE_INDEX_SA
22b8a 4d 50 4c 45 53 2c 20 72 65 67 54 65 6d 70 32 29  MPLES, regTemp2)
22b8b 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
22b8c 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
22b8d 50 5f 53 75 62 74 72 61 63 74 2c 20 72 65 67 53  P_Subtract, regS
22b8e 61 6d 70 6c 65 6e 6f 2c 20 72 65 67 54 65 6d 70  ampleno, regTemp
22b8f 32 2c 20 72 65 67 54 65 6d 70 32 29 3b 0a 20 20  2, regTemp2);.  
22b90 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22b91 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44 69  eAddOp3(v, OP_Di
22b92 76 69 64 65 2c 20 72 65 67 54 65 6d 70 32 2c 20  vide, regTemp2, 
22b93 72 65 67 54 65 6d 70 2c 20 72 65 67 54 65 6d 70  regTemp, regTemp
22b94 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
22b95 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
22b96 4f 50 5f 41 64 64 2c 20 72 65 67 53 61 6d 70 6c  OP_Add, regSampl
22b97 65 72 65 63 6e 6f 2c 20 72 65 67 54 65 6d 70 2c  erecno, regTemp,
22b98 20 72 65 67 53 61 6d 70 6c 65 72 65 63 6e 6f 29   regSamplerecno)
22b99 3b 0a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ;..        sqlit
22b9a 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
22b9b 2c 20 6e 65 29 3b 0a 20 20 20 20 20 20 20 20 73  , ne);.        s
22b9c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
22b9d 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72  (v, OP_AddImm, r
22b9e 65 67 52 65 63 6e 6f 2c 20 31 29 3b 0a 20 20 20  egRecno, 1);.   
22b9f 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
22ba0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22ba1 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 2c 20 72  dOp3(v, OP_Ne, r
22ba2 65 67 43 6f 6c 2c 20 30 2c 20 69 4d 65 6d 2b 6e  egCol, 0, iMem+n
22ba3 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 20 20  Col+i+1);.      
22ba4 2f 2a 2a 2a 2a 20 54 4f 44 4f 3a 20 20 61 64 64  /**** TODO:  add
22ba5 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
22ba6 6e 63 65 20 2a 2a 2a 2a 2a 2f 0a 20 20 20 20 20  nce *****/.     
22ba7 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
22ba8 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45 5f 4a  geP5(v, SQLITE_J
22ba9 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20  UMPIFNULL);.    
22baa 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61  }.    if( db->ma
22bab 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
22bac 20 20 20 20 2f 2a 20 49 66 20 61 20 6d 61 6c 6c      /* If a mall
22bad 6f 63 20 66 61 69 6c 75 72 65 20 68 61 73 20 6f  oc failure has o
22bae 63 63 75 72 72 65 64 2c 20 74 68 65 6e 20 74 68  ccurred, then th
22baf 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20  e result of the 
22bb0 65 78 70 72 65 73 73 69 6f 6e 20 0a 20 20 20 20  expression .    
22bb1 20 20 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74    ** passed as t
22bb2 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
22bb3 6e 74 20 74 6f 20 74 68 65 20 63 61 6c 6c 20 74  nt to the call t
22bb4 6f 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d  o sqlite3VdbeJum
22bb5 70 48 65 72 65 28 29 20 0a 20 20 20 20 20 20 2a  pHere() .      *
22bb6 2a 20 62 65 6c 6f 77 20 6d 61 79 20 62 65 20 6e  * below may be n
22bb7 65 67 61 74 69 76 65 2e 20 57 68 69 63 68 20 63  egative. Which c
22bb8 61 75 73 65 73 20 61 6e 20 61 73 73 65 72 74 28  auses an assert(
22bb9 29 20 74 6f 20 66 61 69 6c 20 28 6f 72 20 61 6e  ) to fail (or an
22bba 0a 20 20 20 20 20 20 2a 2a 20 6f 75 74 2d 6f 66  .      ** out-of
22bbb 2d 62 6f 75 6e 64 73 20 77 72 69 74 65 20 69 66  -bounds write if
22bbc 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73   SQLITE_DEBUG is
22bbd 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2e 20 20   not defined).  
22bbe 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  */.      return;
22bbf 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
22bc0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
22bc1 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 4f  OP_Goto, 0, endO
22bc2 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 66 6f 72 28  fLoop);.    for(
22bc3 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
22bc4 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
22bc5 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
22bc6 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
22bc7 6e 74 41 64 64 72 28 76 29 2d 28 6e 43 6f 6c 2a  ntAddr(v)-(nCol*
22bc8 32 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  2));.      sqlit
22bc9 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
22bca 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4d 65 6d 2b  OP_AddImm, iMem+
22bcb 69 2b 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 73  i+1, 1);.      s
22bcc 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
22bcd 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69  (v, OP_Column, i
22bce 49 64 78 43 75 72 2c 20 69 2c 20 69 4d 65 6d 2b  IdxCur, i, iMem+
22bcf 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 7d  nCol+i+1);.    }
22bd0 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20  ..    /* End of 
22bd1 74 68 65 20 61 6e 61 6c 79 73 69 73 20 6c 6f 6f  the analysis loo
22bd2 70 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  p. */.    sqlite
22bd3 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
22bd4 6c 28 76 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b  l(v, endOfLoop);
22bd5 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
22bd6 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
22bd7 74 2c 20 69 49 64 78 43 75 72 2c 20 74 6f 70 4f  t, iIdxCur, topO
22bd8 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 73 71 6c 69  fLoop);.    sqli
22bd9 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
22bda 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 49 64 78 43   OP_Close, iIdxC
22bdb 75 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 74 6f  ur);..    /* Sto
22bdc 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 69  re the results i
22bdd 6e 20 73 71 6c 69 74 65 5f 73 74 61 74 31 2e 0a  n sqlite_stat1..
22bde 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
22bdf 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 73 69  e result is a si
22be0 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 20  ngle row of the 
22be1 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62  sqlite_stat1 tab
22be2 6c 65 2e 20 20 54 68 65 20 66 69 72 73 74 0a 20  le.  The first. 
22be3 20 20 20 2a 2a 20 74 77 6f 20 63 6f 6c 75 6d 6e     ** two column
22be4 73 20 61 72 65 20 74 68 65 20 6e 61 6d 65 73 20  s are the names 
22be5 6f 66 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64  of the table and
22be6 20 69 6e 64 65 78 2e 20 20 54 68 65 20 74 68 69   index.  The thi
22be7 72 64 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a  rd column.    **
22be8 20 69 73 20 61 20 73 74 72 69 6e 67 20 63 6f 6d   is a string com
22be9 70 6f 73 65 64 20 6f 66 20 61 20 6c 69 73 74 20  posed of a list 
22bea 6f 66 20 69 6e 74 65 67 65 72 20 73 74 61 74 69  of integer stati
22beb 73 74 69 63 73 20 61 62 6f 75 74 20 74 68 65 0a  stics about the.
22bec 20 20 20 20 2a 2a 20 69 6e 64 65 78 2e 20 20 54      ** index.  T
22bed 68 65 20 66 69 72 73 74 20 69 6e 74 65 67 65 72  he first integer
22bee 20 69 6e 20 74 68 65 20 6c 69 73 74 20 69 73 20   in the list is 
22bef 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
22bf0 20 6f 66 20 65 6e 74 72 69 65 73 0a 20 20 20 20   of entries.    
22bf1 2a 2a 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e  ** in the index.
22bf2 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 61    There is one a
22bf3 64 64 69 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65  dditional intege
22bf4 72 20 69 6e 20 74 68 65 20 6c 69 73 74 20 66 6f  r in the list fo
22bf5 72 20 65 61 63 68 0a 20 20 20 20 2a 2a 20 63 6f  r each.    ** co
22bf6 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c  lumn of the tabl
22bf7 65 2e 20 20 54 68 69 73 20 61 64 64 69 74 69 6f  e.  This additio
22bf8 6e 61 6c 20 69 6e 74 65 67 65 72 20 69 73 20 61  nal integer is a
22bf9 20 67 75 65 73 73 20 6f 66 20 68 6f 77 20 6d 61   guess of how ma
22bfa 6e 79 0a 20 20 20 20 2a 2a 20 72 6f 77 73 20 6f  ny.    ** rows o
22bfb 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 65 20  f the table the 
22bfc 69 6e 64 65 78 20 77 69 6c 6c 20 73 65 6c 65 63  index will selec
22bfd 74 2e 20 20 49 66 20 44 20 69 73 20 74 68 65 20  t.  If D is the 
22bfe 63 6f 75 6e 74 20 6f 66 20 64 69 73 74 69 6e 63  count of distinc
22bff 74 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 20  t.    ** values 
22c00 61 6e 64 20 4b 20 69 73 20 74 68 65 20 74 6f 74  and K is the tot
22c01 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  al number of row
22c02 73 2c 20 74 68 65 6e 20 74 68 65 20 69 6e 74 65  s, then the inte
22c03 67 65 72 20 69 73 20 63 6f 6d 70 75 74 65 64 0a  ger is computed.
22c04 20 20 20 20 2a 2a 20 61 73 3a 0a 20 20 20 20 2a      ** as:.    *
22c05 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  *.    **        
22c06 49 20 3d 20 28 4b 2b 44 2d 31 29 2f 44 0a 20 20  I = (K+D-1)/D.  
22c07 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 4b    **.    ** If K
22c08 3d 3d 30 20 74 68 65 6e 20 6e 6f 20 65 6e 74 72  ==0 then no entr
22c09 79 20 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 74  y is made into t
22c0a 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20  he sqlite_stat1 
22c0b 74 61 62 6c 65 2e 20 20 0a 20 20 20 20 2a 2a 20  table.  .    ** 
22c0c 49 66 20 4b 3e 30 20 74 68 65 6e 20 69 74 20 69  If K>0 then it i
22c0d 73 20 61 6c 77 61 79 73 20 74 68 65 20 63 61 73  s always the cas
22c0e 65 20 74 68 65 20 44 3e 30 20 73 6f 20 64 69 76  e the D>0 so div
22c0f 69 73 69 6f 6e 20 62 79 20 7a 65 72 6f 0a 20 20  ision by zero.  
22c10 20 20 2a 2a 20 69 73 20 6e 65 76 65 72 20 70 6f    ** is never po
22c11 73 73 69 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20  ssible..    */. 
22c12 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
22c13 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
22c14 50 5f 49 66 4e 6f 74 2c 20 69 4d 65 6d 29 3b 0a  P_IfNot, iMem);.
22c15 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22c16 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70  ddOp2(v, OP_SCop
22c17 79 2c 20 69 4d 65 6d 2c 20 72 65 67 53 61 6d 70  y, iMem, regSamp
22c18 6c 65 6e 6f 29 3b 0a 20 20 20 20 66 6f 72 28 69  leno);.    for(i
22c19 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
22c1a 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
22c1b 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
22c1c 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 54  String8, 0, regT
22c1d 65 6d 70 2c 20 30 2c 20 22 20 22 2c 20 30 29 3b  emp, 0, " ", 0);
22c1e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
22c1f 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
22c20 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70 2c 20  oncat, regTemp, 
22c21 72 65 67 53 61 6d 70 6c 65 6e 6f 2c 20 72 65 67  regSampleno, reg
22c22 53 61 6d 70 6c 65 6e 6f 29 3b 0a 20 20 20 20 20  Sampleno);.     
22c23 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22c24 70 33 28 76 2c 20 4f 50 5f 41 64 64 2c 20 69 4d  p3(v, OP_Add, iM
22c25 65 6d 2c 20 69 4d 65 6d 2b 69 2b 31 2c 20 72 65  em, iMem+i+1, re
22c26 67 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 73 71  gTemp);.      sq
22c27 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
22c28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72 65  v, OP_AddImm, re
22c29 67 54 65 6d 70 2c 20 2d 31 29 3b 0a 20 20 20 20  gTemp, -1);.    
22c2a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22c2b 4f 70 33 28 76 2c 20 4f 50 5f 44 69 76 69 64 65  Op3(v, OP_Divide
22c2c 2c 20 69 4d 65 6d 2b 69 2b 31 2c 20 72 65 67 54  , iMem+i+1, regT
22c2d 65 6d 70 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20  emp, regTemp);. 
22c2e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
22c2f 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 54 6f 49  AddOp1(v, OP_ToI
22c30 6e 74 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20 20  nt, regTemp);.  
22c31 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22c32 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6e 63  ddOp3(v, OP_Conc
22c33 61 74 2c 20 72 65 67 54 65 6d 70 2c 20 72 65 67  at, regTemp, reg
22c34 53 61 6d 70 6c 65 6e 6f 2c 20 72 65 67 53 61 6d  Sampleno, regSam
22c35 70 6c 65 6e 6f 29 3b 0a 20 20 20 20 7d 0a 20 20  pleno);.    }.  
22c36 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22c37 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op4(v, OP_MakeRe
22c38 63 6f 72 64 2c 20 72 65 67 54 61 62 6e 61 6d 65  cord, regTabname
22c39 2c 20 33 2c 20 72 65 67 52 65 63 2c 20 22 61 61  , 3, regRec, "aa
22c3a 61 22 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  a", 0);.    sqli
22c3b 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
22c3c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 53   OP_NewRowid, iS
22c3d 74 61 74 43 75 72 2c 20 72 65 67 52 6f 77 69 64  tatCur, regRowid
22c3e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
22c3f 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
22c40 6e 73 65 72 74 2c 20 69 53 74 61 74 43 75 72 2c  nsert, iStatCur,
22c41 20 72 65 67 52 65 63 2c 20 72 65 67 52 6f 77 69   regRec, regRowi
22c42 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  d);.    sqlite3V
22c43 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f  dbeChangeP5(v, O
22c44 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20  PFLAG_APPEND);. 
22c45 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
22c46 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b  mpHere(v, addr);
22c47 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  .  }.}../*.** Ge
22c48 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
22c49 20 77 69 6c 6c 20 63 61 75 73 65 20 74 68 65 20   will cause the 
22c4a 6d 6f 73 74 20 72 65 63 65 6e 74 20 69 6e 64 65  most recent inde
22c4b 78 20 61 6e 61 6c 79 73 69 73 20 74 6f 0a 2a 2a  x analysis to.**
22c4c 20 62 65 20 6c 61 6f 64 65 64 20 69 6e 74 6f 20   be laoded into 
22c4d 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74 61  internal hash ta
22c4e 62 6c 65 73 20 77 68 65 72 65 20 69 73 20 63 61  bles where is ca
22c4f 6e 20 62 65 20 75 73 65 64 2e 0a 2a 2f 0a 73 74  n be used..*/.st
22c50 61 74 69 63 20 76 6f 69 64 20 6c 6f 61 64 41 6e  atic void loadAn
22c51 61 6c 79 73 69 73 28 50 61 72 73 65 20 2a 70 50  alysis(Parse *pP
22c52 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a  arse, int iDb){.
22c53 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69    Vdbe *v = sqli
22c54 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
22c55 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20  e);.  if( v ){. 
22c56 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22c57 64 4f 70 31 28 76 2c 20 4f 50 5f 4c 6f 61 64 41  dOp1(v, OP_LoadA
22c58 6e 61 6c 79 73 69 73 2c 20 69 44 62 29 3b 0a 20  nalysis, iDb);. 
22c59 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65   }.}../*.** Gene
22c5a 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77  rate code that w
22c5b 69 6c 6c 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73  ill do an analys
22c5c 69 73 20 6f 66 20 61 6e 20 65 6e 74 69 72 65 20  is of an entire 
22c5d 64 61 74 61 62 61 73 65 0a 2a 2f 0a 73 74 61 74  database.*/.stat
22c5e 69 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a 65 44  ic void analyzeD
22c5f 61 74 61 62 61 73 65 28 50 61 72 73 65 20 2a 70  atabase(Parse *p
22c60 50 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b  Parse, int iDb){
22c61 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
22c62 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 53   pParse->db;.  S
22c63 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d  chema *pSchema =
22c64 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53   db->aDb[iDb].pS
22c65 63 68 65 6d 61 3b 20 20 20 20 2f 2a 20 53 63 68  chema;    /* Sch
22c66 65 6d 61 20 6f 66 20 64 61 74 61 62 61 73 65 20  ema of database 
22c67 69 44 62 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65  iDb */.  HashEle
22c68 6d 20 2a 6b 3b 0a 20 20 69 6e 74 20 69 53 74 61  m *k;.  int iSta
22c69 74 43 75 72 3b 0a 20 20 69 6e 74 20 69 4d 65 6d  tCur;.  int iMem
22c6a 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69  ;..  sqlite3Begi
22c6b 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
22c6c 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b  pParse, 0, iDb);
22c6d 0a 20 20 69 53 74 61 74 43 75 72 20 3d 20 70 50  .  iStatCur = pP
22c6e 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20 20 70 50  arse->nTab;.  pP
22c6f 61 72 73 65 2d 3e 6e 54 61 62 20 2b 3d 20 32 3b  arse->nTab += 2;
22c70 0a 20 20 6f 70 65 6e 53 74 61 74 54 61 62 6c 65  .  openStatTable
22c71 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 69 53  (pParse, iDb, iS
22c72 74 61 74 43 75 72 2c 20 30 29 3b 0a 20 20 69 4d  tatCur, 0);.  iM
22c73 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65  em = pParse->nMe
22c74 6d 2b 31 3b 0a 20 20 66 6f 72 28 6b 3d 73 71 6c  m+1;.  for(k=sql
22c75 69 74 65 48 61 73 68 46 69 72 73 74 28 26 70 53  iteHashFirst(&pS
22c76 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29 3b  chema->tblHash);
22c77 20 6b 3b 20 6b 3d 73 71 6c 69 74 65 48 61 73 68   k; k=sqliteHash
22c78 4e 65 78 74 28 6b 29 29 7b 0a 20 20 20 20 54 61  Next(k)){.    Ta
22c79 62 6c 65 20 2a 70 54 61 62 20 3d 20 28 54 61 62  ble *pTab = (Tab
22c7a 6c 65 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61  le*)sqliteHashDa
22c7b 74 61 28 6b 29 3b 0a 20 20 20 20 61 6e 61 6c 79  ta(k);.    analy
22c7c 7a 65 4f 6e 65 54 61 62 6c 65 28 70 50 61 72 73  zeOneTable(pPars
22c7d 65 2c 20 70 54 61 62 2c 20 69 53 74 61 74 43 75  e, pTab, iStatCu
22c7e 72 2c 20 69 4d 65 6d 29 3b 0a 20 20 7d 0a 20 20  r, iMem);.  }.  
22c7f 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28 70 50 61  loadAnalysis(pPa
22c80 72 73 65 2c 20 69 44 62 29 3b 0a 7d 0a 0a 2f 2a  rse, iDb);.}../*
22c81 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
22c82 65 20 74 68 61 74 20 77 69 6c 6c 20 64 6f 20 61  e that will do a
22c83 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 61 20  n analysis of a 
22c84 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 0a  single table in.
22c85 2a 2a 20 61 20 64 61 74 61 62 61 73 65 2e 0a 2a  ** a database..*
22c86 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 6e  /.static void an
22c87 61 6c 79 7a 65 54 61 62 6c 65 28 50 61 72 73 65  alyzeTable(Parse
22c88 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20   *pParse, Table 
22c89 2a 70 54 61 62 29 7b 0a 20 20 69 6e 74 20 69 44  *pTab){.  int iD
22c8a 62 3b 0a 20 20 69 6e 74 20 69 53 74 61 74 43 75  b;.  int iStatCu
22c8b 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54  r;..  assert( pT
22c8c 61 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ab!=0 );.  asser
22c8d 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
22c8e 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70  oldsAllMutexes(p
22c8f 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20 20  Parse->db) );.  
22c90 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
22c91 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
22c92 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  e->db, pTab->pSc
22c93 68 65 6d 61 29 3b 0a 20 20 73 71 6c 69 74 65 33  hema);.  sqlite3
22c94 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74  BeginWriteOperat
22c95 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69  ion(pParse, 0, i
22c96 44 62 29 3b 0a 20 20 69 53 74 61 74 43 75 72 20  Db);.  iStatCur 
22c97 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a  = pParse->nTab;.
22c98 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 2b    pParse->nTab +
22c99 3d 20 32 3b 0a 20 20 6f 70 65 6e 53 74 61 74 54  = 2;.  openStatT
22c9a 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62  able(pParse, iDb
22c9b 2c 20 69 53 74 61 74 43 75 72 2c 20 70 54 61 62  , iStatCur, pTab
22c9c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 61 6e 61 6c  ->zName);.  anal
22c9d 79 7a 65 4f 6e 65 54 61 62 6c 65 28 70 50 61 72  yzeOneTable(pPar
22c9e 73 65 2c 20 70 54 61 62 2c 20 69 53 74 61 74 43  se, pTab, iStatC
22c9f 75 72 2c 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  ur, pParse->nMem
22ca0 2b 31 29 3b 0a 20 20 6c 6f 61 64 41 6e 61 6c 79  +1);.  loadAnaly
22ca1 73 69 73 28 70 50 61 72 73 65 2c 20 69 44 62 29  sis(pParse, iDb)
22ca2 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
22ca3 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65  ate code for the
22ca4 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64   ANALYZE command
22ca5 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 63 61  .  The parser ca
22ca6 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  lls this routine
22ca7 0a 2a 2a 20 77 68 65 6e 20 69 74 20 72 65 63 6f  .** when it reco
22ca8 67 6e 69 7a 65 73 20 61 6e 20 41 4e 41 4c 59 5a  gnizes an ANALYZ
22ca9 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a  E command..**.**
22caa 20 20 20 20 20 20 20 20 41 4e 41 4c 59 5a 45 20          ANALYZE 
22cab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22cac 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 31 0a             -- 1.
22cad 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c 59 5a  **        ANALYZ
22cae 45 20 20 3c 64 61 74 61 62 61 73 65 3e 20 20 20  E  <database>   
22caf 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20               -- 
22cb0 32 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c  2.**        ANAL
22cb1 59 5a 45 20 20 3f 3c 64 61 74 61 62 61 73 65 3e  YZE  ?<database>
22cb2 2e 3f 3c 74 61 62 6c 65 6e 61 6d 65 3e 20 20 2d  .?<tablename>  -
22cb3 2d 20 33 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d 20 31  - 3.**.** Form 1
22cb4 20 63 61 75 73 65 73 20 61 6c 6c 20 69 6e 64 69   causes all indi
22cb5 63 65 73 20 69 6e 20 61 6c 6c 20 61 74 74 61 63  ces in all attac
22cb6 68 65 64 20 64 61 74 61 62 61 73 65 73 20 74 6f  hed databases to
22cb7 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e 0a 2a 2a   be analyzed..**
22cb8 20 46 6f 72 6d 20 32 20 61 6e 61 6c 79 7a 65 73   Form 2 analyzes
22cb9 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 74 68 65   all indices the
22cba 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65   single database
22cbb 20 6e 61 6d 65 64 2e 0a 2a 2a 20 46 6f 72 6d 20   named..** Form 
22cbc 33 20 61 6e 61 6c 79 7a 65 73 20 61 6c 6c 20 69  3 analyzes all i
22cbd 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61 74 65  ndices associate
22cbe 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65 64  d with the named
22cbf 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54   table..*/.SQLIT
22cc0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
22cc1 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28 50 61  qlite3Analyze(Pa
22cc2 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b  rse *pParse, Tok
22cc3 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54 6f 6b 65  en *pName1, Toke
22cc4 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20 20 73 71  n *pName2){.  sq
22cc5 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
22cc6 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44  se->db;.  int iD
22cc7 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68  b;.  int i;.  ch
22cc8 61 72 20 2a 7a 2c 20 2a 7a 44 62 3b 0a 20 20 54  ar *z, *zDb;.  T
22cc9 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 54 6f  able *pTab;.  To
22cca 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65 3b  ken *pTableName;
22ccb 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20  ..  /* Read the 
22ccc 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e  database schema.
22ccd 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
22cce 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20 65 72  urs, leave an er
22ccf 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20 2a 2a  ror message.  **
22cd0 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70 50 61   and code in pPa
22cd1 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e 20 4e  rse and return N
22cd2 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  ULL. */.  assert
22cd3 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
22cd4 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 50  ldsAllMutexes(pP
22cd5 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20 20 69  arse->db) );.  i
22cd6 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
22cd7 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
22cd8 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 72  pParse) ){.    r
22cd9 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73  eturn;.  }..  as
22cda 73 65 72 74 28 20 70 4e 61 6d 65 32 21 3d 30 20  sert( pName2!=0 
22cdb 7c 7c 20 70 4e 61 6d 65 31 3d 3d 30 20 29 3b 0a  || pName1==0 );.
22cdc 20 20 69 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20    if( pName1==0 
22cdd 29 7b 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 31  ){.    /* Form 1
22cde 3a 20 20 41 6e 61 6c 79 7a 65 20 65 76 65 72 79  :  Analyze every
22cdf 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 66 6f 72  thing */.    for
22ce0 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
22ce1 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
22ce2 20 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65   i==1 ) continue
22ce3 3b 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6e 61  ;  /* Do not ana
22ce4 6c 79 7a 65 20 74 68 65 20 54 45 4d 50 20 64 61  lyze the TEMP da
22ce5 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20  tabase */.      
22ce6 61 6e 61 6c 79 7a 65 44 61 74 61 62 61 73 65 28  analyzeDatabase(
22ce7 70 50 61 72 73 65 2c 20 69 29 3b 0a 20 20 20 20  pParse, i);.    
22ce8 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4e  }.  }else if( pN
22ce9 61 6d 65 32 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20  ame2->n==0 ){.  
22cea 20 20 2f 2a 20 46 6f 72 6d 20 32 3a 20 20 41 6e    /* Form 2:  An
22ceb 61 6c 79 7a 65 20 74 68 65 20 64 61 74 61 62 61  alyze the databa
22cec 73 65 20 6f 72 20 74 61 62 6c 65 20 6e 61 6d 65  se or table name
22ced 64 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20 73  d */.    iDb = s
22cee 71 6c 69 74 65 33 46 69 6e 64 44 62 28 64 62 2c  qlite3FindDb(db,
22cef 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 69 66   pName1);.    if
22cf0 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20  ( iDb>=0 ){.    
22cf1 20 20 61 6e 61 6c 79 7a 65 44 61 74 61 62 61 73    analyzeDatabas
22cf2 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  e(pParse, iDb);.
22cf3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
22cf4 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65   z = sqlite3Name
22cf5 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e  FromToken(db, pN
22cf6 61 6d 65 31 29 3b 0a 20 20 20 20 20 20 69 66 28  ame1);.      if(
22cf7 20 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54   z ){.        pT
22cf8 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61  ab = sqlite3Loca
22cf9 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  teTable(pParse, 
22cfa 30 2c 20 7a 2c 20 30 29 3b 0a 20 20 20 20 20 20  0, z, 0);.      
22cfb 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
22cfc 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 20 20  db, z);.        
22cfd 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20  if( pTab ){.    
22cfe 20 20 20 20 20 20 61 6e 61 6c 79 7a 65 54 61 62        analyzeTab
22cff 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 29  le(pParse, pTab)
22d00 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
22d01 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73    }.    }.  }els
22d02 65 7b 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 33  e{.    /* Form 3
22d03 3a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 66 75  : Analyze the fu
22d04 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 20 74 61  lly qualified ta
22d05 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20  ble name */.    
22d06 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f  iDb = sqlite3Two
22d07 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c  PartName(pParse,
22d08 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c   pName1, pName2,
22d09 20 26 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20   &pTableName);. 
22d0a 20 20 20 69 66 28 20 69 44 62 3e 3d 30 20 29 7b     if( iDb>=0 ){
22d0b 0a 20 20 20 20 20 20 7a 44 62 20 3d 20 64 62 2d  .      zDb = db-
22d0c 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b  >aDb[iDb].zName;
22d0d 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  .      z = sqlit
22d0e 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
22d0f 64 62 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b  db, pTableName);
22d10 0a 20 20 20 20 20 20 69 66 28 20 7a 20 29 7b 0a  .      if( z ){.
22d11 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 73          pTab = s
22d12 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c  qlite3LocateTabl
22d13 65 28 70 50 61 72 73 65 2c 20 30 2c 20 7a 2c 20  e(pParse, 0, z, 
22d14 7a 44 62 29 3b 0a 20 20 20 20 20 20 20 20 73 71  zDb);.        sq
22d15 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
22d16 7a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  z);.        if( 
22d17 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20  pTab ){.        
22d18 20 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 70    analyzeTable(p
22d19 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20  Parse, pTab);.  
22d1a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
22d1b 20 20 20 20 7d 20 20 20 0a 20 20 7d 0a 7d 0a 0a      }   .  }.}..
22d1c 2f 2a 0a 2a 2a 20 55 73 65 64 20 74 6f 20 70 61  /*.** Used to pa
22d1d 73 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ss information f
22d1e 72 6f 6d 20 74 68 65 20 61 6e 61 6c 79 7a 65 72  rom the analyzer
22d1f 20 72 65 61 64 65 72 20 74 68 72 6f 75 67 68 20   reader through 
22d20 74 6f 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 62 61  to the.** callba
22d21 63 6b 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 74  ck routine..*/.t
22d22 79 70 65 64 65 66 20 73 74 72 75 63 74 20 61 6e  ypedef struct an
22d23 61 6c 79 73 69 73 49 6e 66 6f 20 61 6e 61 6c 79  alysisInfo analy
22d24 73 69 73 49 6e 66 6f 3b 0a 73 74 72 75 63 74 20  sisInfo;.struct 
22d25 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 7b 0a 20  analysisInfo {. 
22d26 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
22d27 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74  const char *zDat
22d28 61 62 61 73 65 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  abase;.};../*.**
22d29 20 54 68 69 73 20 63 61 6c 6c 62 61 63 6b 20 69   This callback i
22d2a 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66  s invoked once f
22d2b 6f 72 20 65 61 63 68 20 69 6e 64 65 78 20 77 68  or each index wh
22d2c 65 6e 20 72 65 61 64 69 6e 67 20 74 68 65 0a 2a  en reading the.*
22d2d 2a 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74  * sqlite_stat1 t
22d2e 61 62 6c 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 20 20  able.  .**.**   
22d2f 20 20 61 72 67 76 5b 30 5d 20 3d 20 6e 61 6d 65    argv[0] = name
22d30 20 6f 66 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a   of the index.**
22d31 20 20 20 20 20 61 72 67 76 5b 31 5d 20 3d 20 72       argv[1] = r
22d32 65 73 75 6c 74 73 20 6f 66 20 61 6e 61 6c 79 73  esults of analys
22d33 69 73 20 2d 20 6f 6e 20 69 6e 74 65 67 65 72 20  is - on integer 
22d34 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 0a  for each column.
22d35 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6e  */.static int an
22d36 61 6c 79 73 69 73 4c 6f 61 64 65 72 28 76 6f 69  alysisLoader(voi
22d37 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 61 72  d *pData, int ar
22d38 67 63 2c 20 63 68 61 72 20 2a 2a 61 72 67 76 2c  gc, char **argv,
22d39 20 63 68 61 72 20 2a 2a 4e 6f 74 55 73 65 64 29   char **NotUsed)
22d3a 7b 0a 20 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f  {.  analysisInfo
22d3b 20 2a 70 49 6e 66 6f 20 3d 20 28 61 6e 61 6c 79   *pInfo = (analy
22d3c 73 69 73 49 6e 66 6f 2a 29 70 44 61 74 61 3b 0a  sisInfo*)pData;.
22d3d 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b    Index *pIndex;
22d3e 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 75  .  int i, c;.  u
22d3f 6e 73 69 67 6e 65 64 20 69 6e 74 20 76 3b 0a 20  nsigned int v;. 
22d40 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a   const char *z;.
22d41 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
22d42 3d 32 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  =2 );.  UNUSED_P
22d43 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
22d44 64 2c 20 61 72 67 63 29 3b 0a 0a 20 20 69 66 28  d, argc);..  if(
22d45 20 61 72 67 76 3d 3d 30 20 7c 7c 20 61 72 67 76   argv==0 || argv
22d46 5b 30 5d 3d 3d 30 20 7c 7c 20 61 72 67 76 5b 31  [0]==0 || argv[1
22d47 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  ]==0 ){.    retu
22d48 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 49 6e 64  rn 0;.  }.  pInd
22d49 65 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ex = sqlite3Find
22d4a 49 6e 64 65 78 28 70 49 6e 66 6f 2d 3e 64 62 2c  Index(pInfo->db,
22d4b 20 61 72 67 76 5b 30 5d 2c 20 70 49 6e 66 6f 2d   argv[0], pInfo-
22d4c 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69  >zDatabase);.  i
22d4d 66 28 20 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a  f( pIndex==0 ){.
22d4e 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
22d4f 7d 0a 20 20 7a 20 3d 20 61 72 67 76 5b 31 5d 3b  }.  z = argv[1];
22d50 0a 20 20 66 6f 72 28 69 3d 30 3b 20 2a 7a 20 26  .  for(i=0; *z &
22d51 26 20 69 3c 3d 70 49 6e 64 65 78 2d 3e 6e 43 6f  & i<=pIndex->nCo
22d52 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  lumn; i++){.    
22d53 76 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65  v = 0;.    while
22d54 28 20 28 63 3d 7a 5b 30 5d 29 3e 3d 27 30 27 20  ( (c=z[0])>='0' 
22d55 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20  && c<='9' ){.   
22d56 20 20 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 20     v = v*10 + c 
22d57 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 7a 2b 2b  - '0';.      z++
22d58 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 64  ;.    }.    pInd
22d59 65 78 2d 3e 61 69 52 6f 77 45 73 74 5b 69 5d 20  ex->aiRowEst[i] 
22d5a 3d 20 76 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d  = v;.    if( *z=
22d5b 3d 27 20 27 20 29 20 7a 2b 2b 3b 0a 20 20 7d 0a  =' ' ) z++;.  }.
22d5c 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
22d5d 2a 0a 2a 2a 20 49 66 20 74 68 65 20 49 6e 64 65  *.** If the Inde
22d5e 78 2e 61 53 61 6d 70 6c 65 20 76 61 72 69 61 62  x.aSample variab
22d5f 6c 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  le is not NULL, 
22d60 64 65 6c 65 74 65 20 74 68 65 20 61 53 61 6d 70  delete the aSamp
22d61 6c 65 5b 5d 20 61 72 72 61 79 0a 2a 2a 20 61 6e  le[] array.** an
22d62 64 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 2e 0a  d its contents..
22d63 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
22d64 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
22d65 6c 65 74 65 49 6e 64 65 78 53 61 6d 70 6c 65 73  leteIndexSamples
22d66 28 49 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a 23  (Index *pIdx){.#
22d67 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
22d68 42 4c 45 5f 53 54 41 54 32 0a 20 20 69 66 28 20  BLE_STAT2.  if( 
22d69 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 20 29 7b  pIdx->aSample ){
22d6a 0a 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20  .    int j;.    
22d6b 73 71 6c 69 74 65 33 20 2a 64 62 4d 65 6d 20 3d  sqlite3 *dbMem =
22d6c 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 64   pIdx->pTable->d
22d6d 62 4d 65 6d 3b 0a 20 20 20 20 66 6f 72 28 6a 3d  bMem;.    for(j=
22d6e 30 3b 20 6a 3c 53 51 4c 49 54 45 5f 49 4e 44 45  0; j<SQLITE_INDE
22d6f 58 5f 53 41 4d 50 4c 45 53 3b 20 6a 2b 2b 29 7b  X_SAMPLES; j++){
22d70 0a 20 20 20 20 20 20 49 6e 64 65 78 53 61 6d 70  .      IndexSamp
22d71 6c 65 20 2a 70 20 3d 20 26 70 49 64 78 2d 3e 61  le *p = &pIdx->a
22d72 53 61 6d 70 6c 65 5b 6a 5d 3b 0a 20 20 20 20 20  Sample[j];.     
22d73 20 69 66 28 20 70 2d 3e 65 54 79 70 65 3d 3d 53   if( p->eType==S
22d74 51 4c 49 54 45 5f 54 45 58 54 20 7c 7c 20 70 2d  QLITE_TEXT || p-
22d75 3e 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 42  >eType==SQLITE_B
22d76 4c 4f 42 20 29 7b 0a 20 20 20 20 20 20 20 20 73  LOB ){.        s
22d77 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 49 64  qlite3DbFree(pId
22d78 78 2d 3e 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d  x->pTable->dbMem
22d79 2c 20 70 2d 3e 75 2e 7a 29 3b 0a 20 20 20 20 20  , p->u.z);.     
22d7a 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
22d7b 69 74 65 33 44 62 46 72 65 65 28 64 62 4d 65 6d  ite3DbFree(dbMem
22d7c 2c 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 29  , pIdx->aSample)
22d7d 3b 0a 20 20 20 20 70 49 64 78 2d 3e 61 53 61 6d  ;.    pIdx->aSam
22d7e 70 6c 65 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6c  ple = 0;.  }.#el
22d7f 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  se.  UNUSED_PARA
22d80 4d 45 54 45 52 28 70 49 64 78 29 3b 0a 23 65 6e  METER(pIdx);.#en
22d81 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 61  dif.}../*.** Loa
22d82 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  d the content of
22d83 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
22d84 31 20 61 6e 64 20 73 71 6c 69 74 65 5f 73 74 61  1 and sqlite_sta
22d85 74 32 20 74 61 62 6c 65 73 2e 20 54 68 65 0a 2a  t2 tables. The.*
22d86 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 73 71  * contents of sq
22d87 6c 69 74 65 5f 73 74 61 74 31 20 61 72 65 20 75  lite_stat1 are u
22d88 73 65 64 20 74 6f 20 70 6f 70 75 6c 61 74 65 20  sed to populate 
22d89 74 68 65 20 49 6e 64 65 78 2e 61 69 52 6f 77 45  the Index.aiRowE
22d8a 73 74 5b 5d 0a 2a 2a 20 61 72 72 61 79 73 2e 20  st[].** arrays. 
22d8b 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  The contents of 
22d8c 73 71 6c 69 74 65 5f 73 74 61 74 32 20 61 72 65  sqlite_stat2 are
22d8d 20 75 73 65 64 20 74 6f 20 70 6f 70 75 6c 61 74   used to populat
22d8e 65 20 74 68 65 0a 2a 2a 20 49 6e 64 65 78 2e 61  e the.** Index.a
22d8f 53 61 6d 70 6c 65 5b 5d 20 61 72 72 61 79 73 2e  Sample[] arrays.
22d90 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 71  .**.** If the sq
22d91 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65  lite_stat1 table
22d92 20 69 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20   is not present 
22d93 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  in the database,
22d94 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 0a 2a 2a   SQLITE_ERROR.**
22d95 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 6e   is returned. In
22d96 20 74 68 69 73 20 63 61 73 65 2c 20 65 76 65 6e   this case, even
22d97 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   if SQLITE_ENABL
22d98 45 5f 53 54 41 54 32 20 77 61 73 20 64 65 66 69  E_STAT2 was defi
22d99 6e 65 64 20 0a 2a 2a 20 64 75 72 69 6e 67 20 63  ned .** during c
22d9a 6f 6d 70 69 6c 61 74 69 6f 6e 20 61 6e 64 20 74  ompilation and t
22d9b 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20  he sqlite_stat2 
22d9c 74 61 62 6c 65 20 69 73 20 70 72 65 73 65 6e 74  table is present
22d9d 2c 20 6e 6f 20 64 61 74 61 20 69 73 20 0a 2a 2a  , no data is .**
22d9e 20 72 65 61 64 20 66 72 6f 6d 20 69 74 2e 0a 2a   read from it..*
22d9f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 45  *.** If SQLITE_E
22da0 4e 41 42 4c 45 5f 53 54 41 54 32 20 77 61 73 20  NABLE_STAT2 was 
22da1 64 65 66 69 6e 65 64 20 64 75 72 69 6e 67 20 63  defined during c
22da2 6f 6d 70 69 6c 61 74 69 6f 6e 20 61 6e 64 20 74  ompilation and t
22da3 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 5f 73 74  he .** sqlite_st
22da4 61 74 32 20 74 61 62 6c 65 20 69 73 20 6e 6f 74  at2 table is not
22da5 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20   present in the 
22da6 64 61 74 61 62 61 73 65 2c 20 53 51 4c 49 54 45  database, SQLITE
22da7 5f 45 52 52 4f 52 20 69 73 0a 2a 2a 20 72 65 74  _ERROR is.** ret
22da8 75 72 6e 65 64 2e 20 48 6f 77 65 76 65 72 2c 20  urned. However, 
22da9 69 6e 20 74 68 69 73 20 63 61 73 65 2c 20 64 61  in this case, da
22daa 74 61 20 69 73 20 72 65 61 64 20 66 72 6f 6d 20  ta is read from 
22dab 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31  the sqlite_stat1
22dac 0a 2a 2a 20 74 61 62 6c 65 20 28 69 66 20 69 74  .** table (if it
22dad 20 69 73 20 70 72 65 73 65 6e 74 29 20 62 65 66   is present) bef
22dae 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
22daf 2a 0a 2a 2a 20 49 66 20 61 6e 20 4f 4f 4d 20 65  *.** If an OOM e
22db0 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 69  rror occurs, thi
22db1 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79  s function alway
22db2 73 20 73 65 74 73 20 64 62 2d 3e 6d 61 6c 6c 6f  s sets db->mallo
22db3 63 46 61 69 6c 65 64 2e 0a 2a 2a 20 54 68 69 73  cFailed..** This
22db4 20 6d 65 61 6e 73 20 69 66 20 74 68 65 20 63 61   means if the ca
22db5 6c 6c 65 72 20 64 6f 65 73 20 6e 6f 74 20 63 61  ller does not ca
22db6 72 65 20 61 62 6f 75 74 20 6f 74 68 65 72 20 65  re about other e
22db7 72 72 6f 72 73 2c 20 74 68 65 20 72 65 74 75 72  rrors, the retur
22db8 6e 0a 2a 2a 20 63 6f 64 65 20 6d 61 79 20 62 65  n.** code may be
22db9 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c   ignored..*/.SQL
22dba 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
22dbb 73 71 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c  sqlite3AnalysisL
22dbc 6f 61 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  oad(sqlite3 *db,
22dbd 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 61 6e 61   int iDb){.  ana
22dbe 6c 79 73 69 73 49 6e 66 6f 20 73 49 6e 66 6f 3b  lysisInfo sInfo;
22dbf 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 69 3b 0a  .  HashElem *i;.
22dc0 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20    char *zSql;.  
22dc1 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72  int rc;..  asser
22dc2 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62  t( iDb>=0 && iDb
22dc3 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73  <db->nDb );.  as
22dc4 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 44  sert( db->aDb[iD
22dc5 62 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61  b].pBt!=0 );.  a
22dc6 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
22dc7 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 64 62  reeHoldsMutex(db
22dc8 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 29 20  ->aDb[iDb].pBt) 
22dc9 29 3b 0a 0a 20 20 2f 2a 20 43 6c 65 61 72 20 61  );..  /* Clear a
22dca 6e 79 20 70 72 69 6f 72 20 73 74 61 74 69 73 74  ny prior statist
22dcb 69 63 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 73  ics */.  for(i=s
22dcc 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26  qliteHashFirst(&
22dcd 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
22dce 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 29 3b 69  hema->idxHash);i
22dcf 3b 69 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78  ;i=sqliteHashNex
22dd0 74 28 69 29 29 7b 0a 20 20 20 20 49 6e 64 65 78  t(i)){.    Index
22dd1 20 2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65 48   *pIdx = sqliteH
22dd2 61 73 68 44 61 74 61 28 69 29 3b 0a 20 20 20 20  ashData(i);.    
22dd3 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f  sqlite3DefaultRo
22dd4 77 45 73 74 28 70 49 64 78 29 3b 0a 20 20 20 20  wEst(pIdx);.    
22dd5 73 71 6c 69 74 65 33 44 65 6c 65 74 65 49 6e 64  sqlite3DeleteInd
22dd6 65 78 53 61 6d 70 6c 65 73 28 70 49 64 78 29 3b  exSamples(pIdx);
22dd7 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  .  }..  /* Check
22dd8 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
22dd9 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74  e sqlite_stat1 t
22dda 61 62 6c 65 20 65 78 69 73 74 73 20 2a 2f 0a 20  able exists */. 
22ddb 20 73 49 6e 66 6f 2e 64 62 20 3d 20 64 62 3b 0a   sInfo.db = db;.
22ddc 20 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73    sInfo.zDatabas
22ddd 65 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  e = db->aDb[iDb]
22dde 2e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 73 71  .zName;.  if( sq
22ddf 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
22de0 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74 31  b, "sqlite_stat1
22de1 22 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61  ", sInfo.zDataba
22de2 73 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  se)==0 ){.    re
22de3 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
22de4 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 61  R;.  }..  /* Loa
22de5 64 20 6e 65 77 20 73 74 61 74 69 73 74 69 63 73  d new statistics
22de6 20 6f 75 74 20 6f 66 20 74 68 65 20 73 71 6c 69   out of the sqli
22de7 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 2a  te_stat1 table *
22de8 2f 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74  /.  zSql = sqlit
22de9 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 0a 20  e3MPrintf(db, . 
22dea 20 20 20 20 20 22 53 45 4c 45 43 54 20 69 64 78       "SELECT idx
22deb 2c 20 73 74 61 74 20 46 52 4f 4d 20 25 51 2e 73  , stat FROM %Q.s
22dec 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20 73 49  qlite_stat1", sI
22ded 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a  nfo.zDatabase);.
22dee 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29 7b    if( zSql==0 ){
22def 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
22df0 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b  _NOMEM;.  }else{
22df1 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74  .    (void)sqlit
22df2 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
22df3 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
22df4 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 71 6c 2c  3_exec(db, zSql,
22df5 20 61 6e 61 6c 79 73 69 73 4c 6f 61 64 65 72 2c   analysisLoader,
22df6 20 26 73 49 6e 66 6f 2c 20 30 29 3b 0a 20 20 20   &sInfo, 0);.   
22df7 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
22df8 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20  fetyOn(db);.    
22df9 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
22dfa 2c 20 7a 53 71 6c 29 3b 0a 20 20 7d 0a 0a 0a 20  , zSql);.  }... 
22dfb 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 73 74 61   /* Load the sta
22dfc 74 69 73 74 69 63 73 20 66 72 6f 6d 20 74 68 65  tistics from the
22dfd 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61   sqlite_stat2 ta
22dfe 62 6c 65 2e 20 2a 2f 0a 23 69 66 64 65 66 20 53  ble. */.#ifdef S
22dff 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41  QLITE_ENABLE_STA
22e00 54 32 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  T2.  if( rc==SQL
22e01 49 54 45 5f 4f 4b 20 26 26 20 21 73 71 6c 69 74  ITE_OK && !sqlit
22e02 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
22e03 22 73 71 6c 69 74 65 5f 73 74 61 74 32 22 2c 20  "sqlite_stat2", 
22e04 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65 29  sInfo.zDatabase)
22e05 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
22e06 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20  ITE_ERROR;.  }. 
22e07 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
22e08 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
22e09 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 20 3d 20  3_stmt *pStmt = 
22e0a 30 3b 0a 0a 20 20 20 20 7a 53 71 6c 20 3d 20 73  0;..    zSql = s
22e0b 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
22e0c 2c 20 0a 20 20 20 20 20 20 20 20 22 53 45 4c 45  , .        "SELE
22e0d 43 54 20 69 64 78 2c 73 61 6d 70 6c 65 6e 6f 2c  CT idx,sampleno,
22e0e 73 61 6d 70 6c 65 20 46 52 4f 4d 20 25 51 2e 73  sample FROM %Q.s
22e0f 71 6c 69 74 65 5f 73 74 61 74 32 22 2c 20 73 49  qlite_stat2", sI
22e10 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a  nfo.zDatabase);.
22e11 20 20 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b      if( !zSql ){
22e12 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
22e13 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65  TE_NOMEM;.    }e
22e14 6c 73 65 7b 0a 20 20 20 20 20 20 28 76 6f 69 64  lse{.      (void
22e15 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66  )sqlite3SafetyOf
22e16 66 28 64 62 29 3b 0a 20 20 20 20 20 20 72 63 20  f(db);.      rc 
22e17 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
22e18 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20  e(db, zSql, -1, 
22e19 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 20 20  &pStmt, 0);.    
22e1a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
22e1b 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20  afetyOn(db);.   
22e1c 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
22e1d 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 20 20  (db, zSql);.    
22e1e 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  }..    if( rc==S
22e1f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
22e20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
22e21 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20  afetyOff(db);.  
22e22 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74      while( sqlit
22e23 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 3d 3d  e3_step(pStmt)==
22e24 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
22e25 20 20 20 20 20 20 63 68 61 72 20 2a 7a 49 6e 64        char *zInd
22e26 65 78 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c  ex = (char *)sql
22e27 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
22e28 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 20 20  (pStmt, 0);.    
22e29 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20      Index *pIdx 
22e2a 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64  = sqlite3FindInd
22e2b 65 78 28 64 62 2c 20 7a 49 6e 64 65 78 2c 20 73  ex(db, zIndex, s
22e2c 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65 29 3b  Info.zDatabase);
22e2d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64  .        if( pId
22e2e 78 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  x ){.          i
22e2f 6e 74 20 69 53 61 6d 70 6c 65 20 3d 20 73 71 6c  nt iSample = sql
22e30 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28  ite3_column_int(
22e31 70 53 74 6d 74 2c 20 31 29 3b 0a 20 20 20 20 20  pStmt, 1);.     
22e32 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62       sqlite3 *db
22e33 4d 65 6d 20 3d 20 70 49 64 78 2d 3e 70 54 61 62  Mem = pIdx->pTab
22e34 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 20 20 20 20 20  le->dbMem;.     
22e35 20 20 20 20 20 61 73 73 65 72 74 28 20 64 62 4d       assert( dbM
22e36 65 6d 3d 3d 64 62 20 7c 7c 20 64 62 4d 65 6d 3d  em==db || dbMem=
22e37 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  =0 );.          
22e38 69 66 28 20 69 53 61 6d 70 6c 65 3c 53 51 4c 49  if( iSample<SQLI
22e39 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53  TE_INDEX_SAMPLES
22e3a 20 26 26 20 69 53 61 6d 70 6c 65 3e 3d 30 20 29   && iSample>=0 )
22e3b 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e  {.            in
22e3c 74 20 65 54 79 70 65 20 3d 20 73 71 6c 69 74 65  t eType = sqlite
22e3d 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70 53  3_column_type(pS
22e3e 74 6d 74 2c 20 32 29 3b 0a 0a 20 20 20 20 20 20  tmt, 2);..      
22e3f 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e        if( pIdx->
22e40 61 53 61 6d 70 6c 65 3d 3d 30 20 29 7b 0a 20 20  aSample==0 ){.  
22e41 20 20 20 20 20 20 20 20 20 20 20 20 73 74 61 74              stat
22e42 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 73 7a 20  ic const int sz 
22e43 3d 20 73 69 7a 65 6f 66 28 49 6e 64 65 78 53 61  = sizeof(IndexSa
22e44 6d 70 6c 65 29 2a 53 51 4c 49 54 45 5f 49 4e 44  mple)*SQLITE_IND
22e45 45 58 5f 53 41 4d 50 4c 45 53 3b 0a 20 20 20 20  EX_SAMPLES;.    
22e46 20 20 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e            pIdx->
22e47 61 53 61 6d 70 6c 65 20 3d 20 28 49 6e 64 65 78  aSample = (Index
22e48 53 61 6d 70 6c 65 20 2a 29 73 71 6c 69 74 65 33  Sample *)sqlite3
22e49 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 4d  DbMallocZero(dbM
22e4a 65 6d 2c 20 73 7a 29 3b 0a 20 20 20 20 20 20 20  em, sz);.       
22e4b 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d         if( pIdx-
22e4c 3e 61 53 61 6d 70 6c 65 3d 3d 30 20 29 7b 0a 20  >aSample==0 ){. 
22e4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
22e4e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
22e4f 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 1;.           
22e50 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
22e51 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
22e52 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20          }..     
22e53 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
22e54 49 64 78 2d 3e 61 53 61 6d 70 6c 65 20 29 3b 0a  Idx->aSample );.
22e55 20 20 20 20 20 20 20 20 20 20 20 20 7b 0a 20 20              {.  
22e56 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 64 65              Inde
22e57 78 53 61 6d 70 6c 65 20 2a 70 53 61 6d 70 6c 65  xSample *pSample
22e58 20 3d 20 26 70 49 64 78 2d 3e 61 53 61 6d 70 6c   = &pIdx->aSampl
22e59 65 5b 69 53 61 6d 70 6c 65 5d 3b 0a 20 20 20 20  e[iSample];.    
22e5a 20 20 20 20 20 20 20 20 20 20 70 53 61 6d 70 6c            pSampl
22e5b 65 2d 3e 65 54 79 70 65 20 3d 20 28 75 38 29 65  e->eType = (u8)e
22e5c 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 20 20  Type;.          
22e5d 20 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 53      if( eType==S
22e5e 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 7c  QLITE_INTEGER ||
22e5f 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 46   eType==SQLITE_F
22e60 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20 20 20 20  LOAT ){.        
22e61 20 20 20 20 20 20 20 20 70 53 61 6d 70 6c 65 2d          pSample-
22e62 3e 75 2e 72 20 3d 20 73 71 6c 69 74 65 33 5f 63  >u.r = sqlite3_c
22e63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 70 53 74  olumn_double(pSt
22e64 6d 74 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20  mt, 2);.        
22e65 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
22e66 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 54 45  eType==SQLITE_TE
22e67 58 54 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c  XT || eType==SQL
22e68 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20  ITE_BLOB ){.    
22e69 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
22e6a 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e  t char *z = (con
22e6b 73 74 20 63 68 61 72 20 2a 29 28 0a 20 20 20 20  st char *)(.    
22e6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22e6d 28 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 42  (eType==SQLITE_B
22e6e 4c 4f 42 29 20 3f 0a 20 20 20 20 20 20 20 20 20  LOB) ?.         
22e6f 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
22e70 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 70  e3_column_blob(p
22e71 53 74 6d 74 2c 20 32 29 3a 0a 20 20 20 20 20 20  Stmt, 2):.      
22e72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
22e73 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
22e74 74 28 70 53 74 6d 74 2c 20 32 29 0a 20 20 20 20  t(pStmt, 2).    
22e75 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20              );. 
22e76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
22e77 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 63  nt n = sqlite3_c
22e78 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 53 74 6d  olumn_bytes(pStm
22e79 74 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 20  t, 2);.         
22e7a 20 20 20 20 20 20 20 69 66 28 20 6e 3e 32 34 20         if( n>24 
22e7b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
22e7c 20 20 20 20 20 6e 20 3d 20 32 34 3b 0a 20 20 20       n = 24;.   
22e7d 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
22e7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
22e7f 53 61 6d 70 6c 65 2d 3e 6e 42 79 74 65 20 3d 20  Sample->nByte = 
22e80 28 75 38 29 6e 3b 0a 20 20 20 20 20 20 20 20 20  (u8)n;.         
22e81 20 20 20 20 20 20 20 70 53 61 6d 70 6c 65 2d 3e         pSample->
22e82 75 2e 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  u.z = sqlite3DbM
22e83 61 6c 6c 6f 63 52 61 77 28 64 62 4d 65 6d 2c 20  allocRaw(dbMem, 
22e84 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  n);.            
22e85 20 20 20 20 69 66 28 20 70 53 61 6d 70 6c 65 2d      if( pSample-
22e86 3e 75 2e 7a 20 29 7b 0a 20 20 20 20 20 20 20 20  >u.z ){.        
22e87 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79            memcpy
22e88 28 70 53 61 6d 70 6c 65 2d 3e 75 2e 7a 2c 20 7a  (pSample->u.z, z
22e89 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , n);.          
22e8a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
22e8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
22e8c 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
22e8d 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 1;.           
22e8e 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
22e8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a                }.
22e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a                }.
22e91 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
22e92 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
22e93 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
22e94 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 66    rc = sqlite3_f
22e95 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a  inalize(pStmt);.
22e96 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69        (void)sqli
22e97 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b  te3SafetyOn(db);
22e98 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
22e99 66 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  f..  if( rc==SQL
22e9a 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20  ITE_NOMEM ){.   
22e9b 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
22e9c 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74  d = 1;.  }.  ret
22e9d 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 65 6e 64  urn rc;.}...#end
22e9e 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
22e9f 54 5f 41 4e 41 4c 59 5a 45 20 2a 2f 0a 0a 2f 2a  T_ANALYZE */../*
22ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
22ea1 64 20 6f 66 20 61 6e 61 6c 79 7a 65 2e 63 20 2a  d of analyze.c *
22ea2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22ea3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22ea4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
22ea5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
22ea6 67 69 6e 20 66 69 6c 65 20 61 74 74 61 63 68 2e  gin file attach.
22ea7 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
22ea8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22ea9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
22eaa 0a 2a 2a 20 32 30 30 33 20 41 70 72 69 6c 20 36  .** 2003 April 6
22eab 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
22eac 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
22ead 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
22eae 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
22eaf 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
22eb0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
22eb1 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
22eb2 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
22eb3 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
22eb4 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
22eb5 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
22eb6 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
22eb7 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
22eb8 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
22eb9 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
22eba 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
22ebb 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
22ebc 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
22ebd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22ebe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22ebf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
22ec1 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
22ec2 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20  tains code used 
22ec3 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
22ec4 20 41 54 54 41 43 48 20 61 6e 64 20 44 45 54 41   ATTACH and DETA
22ec5 43 48 20 63 6f 6d 6d 61 6e 64 73 2e 0a 2a 2a 0a  CH commands..**.
22ec6 2a 2a 20 24 49 64 3a 20 61 74 74 61 63 68 2e 63  ** $Id: attach.c
22ec7 2c 76 20 31 2e 39 33 20 32 30 30 39 2f 30 35 2f  ,v 1.93 2009/05/
22ec8 33 31 20 32 31 3a 32 31 3a 34 31 20 64 72 68 20  31 21:21:41 drh 
22ec9 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65  Exp $.*/..#ifnde
22eca 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54  f SQLITE_OMIT_AT
22ecb 54 41 43 48 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c  TACH./*.** Resol
22ecc 76 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  ve an expression
22ecd 20 74 68 61 74 20 77 61 73 20 70 61 72 74 20 6f   that was part o
22ece 66 20 61 6e 20 41 54 54 41 43 48 20 6f 72 20 44  f an ATTACH or D
22ecf 45 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e  ETACH statement.
22ed0 20 54 68 69 73 0a 2a 2a 20 69 73 20 73 6c 69 67   This.** is slig
22ed1 68 74 6c 79 20 64 69 66 66 65 72 65 6e 74 20 66  htly different f
22ed2 72 6f 6d 20 72 65 73 6f 6c 76 69 6e 67 20 61 20  rom resolving a 
22ed3 6e 6f 72 6d 61 6c 20 53 51 4c 20 65 78 70 72 65  normal SQL expre
22ed4 73 73 69 6f 6e 2c 20 62 65 63 61 75 73 65 20 73  ssion, because s
22ed5 69 6d 70 6c 65 0a 2a 2a 20 69 64 65 6e 74 69 66  imple.** identif
22ed6 69 65 72 73 20 61 72 65 20 74 72 65 61 74 65 64  iers are treated
22ed7 20 61 73 20 73 74 72 69 6e 67 73 2c 20 6e 6f 74   as strings, not
22ed8 20 70 6f 73 73 69 62 6c 65 20 63 6f 6c 75 6d 6e   possible column
22ed9 20 6e 61 6d 65 73 20 6f 72 20 61 6c 69 61 73 65   names or aliase
22eda 73 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 69 66  s..**.** i.e. if
22edb 20 74 68 65 20 70 61 72 73 65 72 20 73 65 65 73   the parser sees
22edc 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41  :.**.**     ATTA
22edd 43 48 20 44 41 54 41 42 41 53 45 20 61 62 63 20  CH DATABASE abc 
22ede 41 53 20 64 65 66 0a 2a 2a 0a 2a 2a 20 69 74 20  AS def.**.** it 
22edf 74 72 65 61 74 73 20 74 68 65 20 74 77 6f 20 65  treats the two e
22ee0 78 70 72 65 73 73 69 6f 6e 73 20 61 73 20 6c 69  xpressions as li
22ee1 74 65 72 61 6c 20 73 74 72 69 6e 67 73 20 27 61  teral strings 'a
22ee2 62 63 27 20 61 6e 64 20 27 64 65 66 27 20 69 6e  bc' and 'def' in
22ee3 73 74 65 61 64 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b  stead of.** look
22ee4 69 6e 67 20 66 6f 72 20 63 6f 6c 75 6d 6e 73 20  ing for columns 
22ee5 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  of the same name
22ee6 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c  ..**.** This onl
22ee7 79 20 61 70 70 6c 69 65 73 20 74 6f 20 74 68 65  y applies to the
22ee8 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 70 45   root node of pE
22ee9 78 70 72 2c 20 73 6f 20 74 68 65 20 73 74 61 74  xpr, so the stat
22eea 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ement:.**.**    
22eeb 20 41 54 54 41 43 48 20 44 41 54 41 42 41 53 45   ATTACH DATABASE
22eec 20 61 62 63 7c 7c 64 65 66 20 41 53 20 27 64 62   abc||def AS 'db
22eed 32 27 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20 66 61  2'.**.** will fa
22eee 69 6c 20 62 65 63 61 75 73 65 20 6e 65 69 74 68  il because neith
22eef 65 72 20 61 62 63 20 6f 72 20 64 65 66 20 63 61  er abc or def ca
22ef0 6e 20 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a  n be resolved..*
22ef1 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73  /.static int res
22ef2 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 28 4e  olveAttachExpr(N
22ef3 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 61 6d  ameContext *pNam
22ef4 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 0a  e, Expr *pExpr).
22ef5 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
22ef6 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 45  ITE_OK;.  if( pE
22ef7 78 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20 70  xpr ){.    if( p
22ef8 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 44 20  Expr->op!=TK_ID 
22ef9 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
22efa 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
22efb 4e 61 6d 65 73 28 70 4e 61 6d 65 2c 20 70 45 78  Names(pName, pEx
22efc 70 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  pr);.      if( r
22efd 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
22efe 21 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f  !sqlite3ExprIsCo
22eff 6e 73 74 61 6e 74 28 70 45 78 70 72 29 20 29 7b  nstant(pExpr) ){
22f00 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
22f01 45 72 72 6f 72 4d 73 67 28 70 4e 61 6d 65 2d 3e  ErrorMsg(pName->
22f02 70 50 61 72 73 65 2c 20 22 69 6e 76 61 6c 69 64  pParse, "invalid
22f03 20 6e 61 6d 65 3a 20 5c 22 25 73 5c 22 22 2c 20   name: \"%s\"", 
22f04 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29  pExpr->u.zToken)
22f05 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
22f06 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
22f07 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
22f08 7b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f  {.      pExpr->o
22f09 70 20 3d 20 54 4b 5f 53 54 52 49 4e 47 3b 0a 20  p = TK_STRING;. 
22f0a 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
22f0b 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  n rc;.}../*.** A
22f0c 6e 20 53 51 4c 20 75 73 65 72 2d 66 75 6e 63 74  n SQL user-funct
22f0d 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 74  ion registered t
22f0e 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66  o do the work of
22f0f 20 61 6e 20 41 54 54 41 43 48 20 73 74 61 74 65   an ATTACH state
22f10 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20 74 68 72  ment. The.** thr
22f11 65 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  ee arguments to 
22f12 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d  the function com
22f13 65 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20  e directly from 
22f14 61 6e 20 61 74 74 61 63 68 20 73 74 61 74 65 6d  an attach statem
22f15 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41  ent:.**.**     A
22f16 54 54 41 43 48 20 44 41 54 41 42 41 53 45 20 78  TTACH DATABASE x
22f17 20 41 53 20 79 20 4b 45 59 20 7a 0a 2a 2a 0a 2a   AS y KEY z.**.*
22f18 2a 20 20 20 20 20 53 45 4c 45 43 54 20 73 71 6c  *     SELECT sql
22f19 69 74 65 5f 61 74 74 61 63 68 28 78 2c 20 79 2c  ite_attach(x, y,
22f1a 20 7a 29 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65   z).**.** If the
22f1b 20 6f 70 74 69 6f 6e 61 6c 20 22 4b 45 59 20 7a   optional "KEY z
22f1c 22 20 73 79 6e 74 61 78 20 69 73 20 6f 6d 69 74  " syntax is omit
22f1d 74 65 64 2c 20 61 6e 20 53 51 4c 20 4e 55 4c 4c  ted, an SQL NULL
22f1e 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68   is passed as th
22f1f 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 67 75 6d  e.** third argum
22f20 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ent..*/.static v
22f21 6f 69 64 20 61 74 74 61 63 68 46 75 6e 63 28 0a  oid attachFunc(.
22f22 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
22f23 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
22f24 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c  t NotUsed,.  sql
22f25 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
22f26 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  v.){.  int i;.  
22f27 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 73 71  int rc = 0;.  sq
22f28 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69  lite3 *db = sqli
22f29 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
22f2a 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a  andle(context);.
22f2b 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
22f2c 61 6d 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  ame;.  const cha
22f2d 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 44 62 20 2a  r *zFile;.  Db *
22f2e 61 4e 65 77 3b 0a 20 20 63 68 61 72 20 2a 7a 45  aNew;.  char *zE
22f2f 72 72 44 79 6e 20 3d 20 30 3b 0a 0a 20 20 55 4e  rrDyn = 0;..  UN
22f30 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
22f31 6f 74 55 73 65 64 29 3b 0a 0a 20 20 7a 46 69 6c  otUsed);..  zFil
22f32 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  e = (const char 
22f33 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
22f34 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  text(argv[0]);. 
22f35 20 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20   zName = (const 
22f36 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76  char *)sqlite3_v
22f37 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31  alue_text(argv[1
22f38 5d 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 3d  ]);.  if( zFile=
22f39 3d 30 20 29 20 7a 46 69 6c 65 20 3d 20 22 22 3b  =0 ) zFile = "";
22f3a 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20  .  if( zName==0 
22f3b 29 20 7a 4e 61 6d 65 20 3d 20 22 22 3b 0a 0a 20  ) zName = "";.. 
22f3c 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 74 68   /* Check for th
22f3d 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72 72 6f  e following erro
22f3e 72 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  rs:.  **.  **   
22f3f 20 20 2a 20 54 6f 6f 20 6d 61 6e 79 20 61 74 74    * Too many att
22f40 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2c  ached databases,
22f41 0a 20 20 2a 2a 20 20 20 20 20 2a 20 54 72 61 6e  .  **     * Tran
22f42 73 61 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c  saction currentl
22f43 79 20 6f 70 65 6e 0a 20 20 2a 2a 20 20 20 20 20  y open.  **     
22f44 2a 20 53 70 65 63 69 66 69 65 64 20 64 61 74 61  * Specified data
22f45 62 61 73 65 20 6e 61 6d 65 20 61 6c 72 65 61 64  base name alread
22f46 79 20 62 65 69 6e 67 20 75 73 65 64 2e 0a 20 20  y being used..  
22f47 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6e 44 62  */.  if( db->nDb
22f48 3e 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  >=db->aLimit[SQL
22f49 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48  ITE_LIMIT_ATTACH
22f4a 45 44 5d 2b 32 20 29 7b 0a 20 20 20 20 7a 45 72  ED]+2 ){.    zEr
22f4b 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50  rDyn = sqlite3MP
22f4c 72 69 6e 74 66 28 64 62 2c 20 22 74 6f 6f 20 6d  rintf(db, "too m
22f4d 61 6e 79 20 61 74 74 61 63 68 65 64 20 64 61 74  any attached dat
22f4e 61 62 61 73 65 73 20 2d 20 6d 61 78 20 25 64 22  abases - max %d"
22f4f 2c 20 0a 20 20 20 20 20 20 64 62 2d 3e 61 4c 69  , .      db->aLi
22f50 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
22f51 5f 41 54 54 41 43 48 45 44 5d 0a 20 20 20 20 29  _ATTACHED].    )
22f52 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63  ;.    goto attac
22f53 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69  h_error;.  }.  i
22f54 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  f( !db->autoComm
22f55 69 74 20 29 7b 0a 20 20 20 20 7a 45 72 72 44 79  it ){.    zErrDy
22f56 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  n = sqlite3MPrin
22f57 74 66 28 64 62 2c 20 22 63 61 6e 6e 6f 74 20 41  tf(db, "cannot A
22f58 54 54 41 43 48 20 64 61 74 61 62 61 73 65 20 77  TTACH database w
22f59 69 74 68 69 6e 20 74 72 61 6e 73 61 63 74 69 6f  ithin transactio
22f5a 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74  n");.    goto at
22f5b 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a  tach_error;.  }.
22f5c 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
22f5d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
22f5e 63 68 61 72 20 2a 7a 20 3d 20 64 62 2d 3e 61 44  char *z = db->aD
22f5f 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  b[i].zName;.    
22f60 61 73 73 65 72 74 28 20 7a 20 26 26 20 7a 4e 61  assert( z && zNa
22f61 6d 65 20 29 3b 0a 20 20 20 20 69 66 28 20 73 71  me );.    if( sq
22f62 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20  lite3StrICmp(z, 
22f63 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20  zName)==0 ){.   
22f64 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c     zErrDyn = sql
22f65 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
22f66 22 64 61 74 61 62 61 73 65 20 25 73 20 69 73 20  "database %s is 
22f67 61 6c 72 65 61 64 79 20 69 6e 20 75 73 65 22 2c  already in use",
22f68 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 67   zName);.      g
22f69 6f 74 6f 20 61 74 74 61 63 68 5f 65 72 72 6f 72  oto attach_error
22f6a 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
22f6b 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 6e  * Allocate the n
22f6c 65 77 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  ew entry in the 
22f6d 64 62 2d 3e 61 44 62 5b 5d 20 61 72 72 61 79 20  db->aDb[] array 
22f6e 61 6e 64 20 69 6e 69 74 69 61 6c 69 73 65 20 74  and initialise t
22f6f 68 65 20 73 63 68 65 6d 61 0a 20 20 2a 2a 20 68  he schema.  ** h
22f70 61 73 68 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f  ash tables..  */
22f71 0a 20 20 69 66 28 20 64 62 2d 3e 61 44 62 3d 3d  .  if( db->aDb==
22f72 64 62 2d 3e 61 44 62 53 74 61 74 69 63 20 29 7b  db->aDbStatic ){
22f73 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69  .    aNew = sqli
22f74 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
22f75 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44  b, sizeof(db->aD
22f76 62 5b 30 5d 29 2a 33 20 29 3b 0a 20 20 20 20 69  b[0])*3 );.    i
22f77 66 28 20 61 4e 65 77 3d 3d 30 20 29 20 72 65 74  f( aNew==0 ) ret
22f78 75 72 6e 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  urn;.    memcpy(
22f79 61 4e 65 77 2c 20 64 62 2d 3e 61 44 62 2c 20 73  aNew, db->aDb, s
22f7a 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d  izeof(db->aDb[0]
22f7b 29 2a 32 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  )*2);.  }else{. 
22f7c 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65     aNew = sqlite
22f7d 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 64  3DbRealloc(db, d
22f7e 62 2d 3e 61 44 62 2c 20 73 69 7a 65 6f 66 28 64  b->aDb, sizeof(d
22f7f 62 2d 3e 61 44 62 5b 30 5d 29 2a 28 64 62 2d 3e  b->aDb[0])*(db->
22f80 6e 44 62 2b 31 29 20 29 3b 0a 20 20 20 20 69 66  nDb+1) );.    if
22f81 28 20 61 4e 65 77 3d 3d 30 20 29 20 72 65 74 75  ( aNew==0 ) retu
22f82 72 6e 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 61 44  rn;.  }.  db->aD
22f83 62 20 3d 20 61 4e 65 77 3b 0a 20 20 61 4e 65 77  b = aNew;.  aNew
22f84 20 3d 20 26 64 62 2d 3e 61 44 62 5b 64 62 2d 3e   = &db->aDb[db->
22f85 6e 44 62 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61  nDb];.  memset(a
22f86 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a  New, 0, sizeof(*
22f87 61 4e 65 77 29 29 3b 0a 0a 20 20 2f 2a 20 4f 70  aNew));..  /* Op
22f88 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
22f89 66 69 6c 65 2e 20 49 66 20 74 68 65 20 62 74 72  file. If the btr
22f8a 65 65 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  ee is successful
22f8b 6c 79 20 6f 70 65 6e 65 64 2c 20 75 73 65 0a 20  ly opened, use. 
22f8c 20 2a 2a 20 69 74 20 74 6f 20 6f 62 74 61 69 6e   ** it to obtain
22f8d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
22f8e 68 65 6d 61 2e 20 41 74 20 74 68 69 73 20 70 6f  hema. At this po
22f8f 69 6e 74 20 74 68 65 20 73 63 68 65 6d 61 20 6d  int the schema m
22f90 61 79 0a 20 20 2a 2a 20 6f 72 20 6d 61 79 20 6e  ay.  ** or may n
22f91 6f 74 20 62 65 20 69 6e 69 74 69 61 6c 69 73 65  ot be initialise
22f92 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73  d..  */.  rc = s
22f93 71 6c 69 74 65 33 42 74 72 65 65 46 61 63 74 6f  qlite3BtreeFacto
22f94 72 79 28 64 62 2c 20 7a 46 69 6c 65 2c 20 30 2c  ry(db, zFile, 0,
22f95 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
22f96 43 41 43 48 45 5f 53 49 5a 45 2c 0a 20 20 20 20  CACHE_SIZE,.    
22f97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22f98 20 20 20 20 20 20 20 64 62 2d 3e 6f 70 65 6e 46         db->openF
22f99 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 5f 4f 50  lags | SQLITE_OP
22f9a 45 4e 5f 4d 41 49 4e 5f 44 42 2c 0a 20 20 20 20  EN_MAIN_DB,.    
22f9b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22f9c 20 20 20 20 20 20 20 26 61 4e 65 77 2d 3e 70 42         &aNew->pB
22f9d 74 29 3b 0a 20 20 64 62 2d 3e 6e 44 62 2b 2b 3b  t);.  db->nDb++;
22f9e 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
22f9f 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 29 7b 0a  E_CONSTRAINT ){.
22fa0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
22fa1 45 52 52 4f 52 3b 0a 20 20 20 20 7a 45 72 72 44  ERROR;.    zErrD
22fa2 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  yn = sqlite3MPri
22fa3 6e 74 66 28 64 62 2c 20 22 64 61 74 61 62 61 73  ntf(db, "databas
22fa4 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 74  e is already att
22fa5 61 63 68 65 64 22 29 3b 0a 20 20 7d 65 6c 73 65  ached");.  }else
22fa6 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
22fa7 4f 4b 20 29 7b 0a 20 20 20 20 50 61 67 65 72 20  OK ){.    Pager 
22fa8 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 61 4e 65  *pPager;.    aNe
22fa9 77 2d 3e 70 53 63 68 65 6d 61 20 3d 20 73 71 6c  w->pSchema = sql
22faa 69 74 65 33 53 63 68 65 6d 61 47 65 74 28 64 62  ite3SchemaGet(db
22fab 2c 20 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20  , aNew->pBt);.  
22fac 20 20 69 66 28 20 21 61 4e 65 77 2d 3e 70 53 63    if( !aNew->pSc
22fad 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 72 63  hema ){.      rc
22fae 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
22faf 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61  .    }else if( a
22fb0 4e 65 77 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69  New->pSchema->fi
22fb1 6c 65 5f 66 6f 72 6d 61 74 20 26 26 20 61 4e 65  le_format && aNe
22fb2 77 2d 3e 70 53 63 68 65 6d 61 2d 3e 65 6e 63 21  w->pSchema->enc!
22fb3 3d 45 4e 43 28 64 62 29 20 29 7b 0a 20 20 20 20  =ENC(db) ){.    
22fb4 20 20 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69    zErrDyn = sqli
22fb5 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 0a  te3MPrintf(db, .
22fb6 20 20 20 20 20 20 20 20 22 61 74 74 61 63 68 65          "attache
22fb7 64 20 64 61 74 61 62 61 73 65 73 20 6d 75 73 74  d databases must
22fb8 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 74 65   use the same te
22fb9 78 74 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 6d  xt encoding as m
22fba 61 69 6e 20 64 61 74 61 62 61 73 65 22 29 3b 0a  ain database");.
22fbb 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
22fbc 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20  E_ERROR;.    }. 
22fbd 20 20 20 70 50 61 67 65 72 20 3d 20 73 71 6c 69     pPager = sqli
22fbe 74 65 33 42 74 72 65 65 50 61 67 65 72 28 61 4e  te3BtreePager(aN
22fbf 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20 20 73 71  ew->pBt);.    sq
22fc0 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e  lite3PagerLockin
22fc1 67 4d 6f 64 65 28 70 50 61 67 65 72 2c 20 64 62  gMode(pPager, db
22fc2 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 29 3b  ->dfltLockMode);
22fc3 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65  .    sqlite3Page
22fc4 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 70 50 61  rJournalMode(pPa
22fc5 67 65 72 2c 20 64 62 2d 3e 64 66 6c 74 4a 6f 75  ger, db->dfltJou
22fc6 72 6e 61 6c 4d 6f 64 65 29 3b 0a 20 20 7d 0a 20  rnalMode);.  }. 
22fc7 20 61 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 73   aNew->zName = s
22fc8 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
22fc9 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 61 4e 65  b, zName);.  aNe
22fca 77 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65 6c 20  w->safety_level 
22fcb 3d 20 33 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45  = 3;..#if SQLITE
22fcc 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 7b 0a 20  _HAS_CODEC.  {. 
22fcd 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71     extern int sq
22fce 6c 69 74 65 33 43 6f 64 65 63 41 74 74 61 63 68  lite3CodecAttach
22fcf 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20  (sqlite3*, int, 
22fd0 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74  const void*, int
22fd1 29 3b 0a 20 20 20 20 65 78 74 65 72 6e 20 76 6f  );.    extern vo
22fd2 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 63 47  id sqlite3CodecG
22fd3 65 74 4b 65 79 28 73 71 6c 69 74 65 33 2a 2c 20  etKey(sqlite3*, 
22fd4 69 6e 74 2c 20 76 6f 69 64 2a 2a 2c 20 69 6e 74  int, void**, int
22fd5 2a 29 3b 0a 20 20 20 20 69 6e 74 20 6e 4b 65 79  *);.    int nKey
22fd6 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4b 65 79  ;.    char *zKey
22fd7 3b 0a 20 20 20 20 69 6e 74 20 74 20 3d 20 73 71  ;.    int t = sq
22fd8 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
22fd9 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 73  (argv[2]);.    s
22fda 77 69 74 63 68 28 20 74 20 29 7b 0a 20 20 20 20  witch( t ){.    
22fdb 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e    case SQLITE_IN
22fdc 54 45 47 45 52 3a 0a 20 20 20 20 20 20 63 61 73  TEGER:.      cas
22fdd 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 0a  e SQLITE_FLOAT:.
22fde 20 20 20 20 20 20 20 20 7a 45 72 72 44 79 6e 20          zErrDyn 
22fdf 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
22fe0 70 28 64 62 2c 20 22 49 6e 76 61 6c 69 64 20 6b  p(db, "Invalid k
22fe1 65 79 20 76 61 6c 75 65 22 29 3b 0a 20 20 20 20  ey value");.    
22fe2 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
22fe3 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 62  ERROR;.        b
22fe4 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 0a 20  reak;.        . 
22fe5 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45       case SQLITE
22fe6 5f 54 45 58 54 3a 0a 20 20 20 20 20 20 63 61 73  _TEXT:.      cas
22fe7 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 0a 20  e SQLITE_BLOB:. 
22fe8 20 20 20 20 20 20 20 6e 4b 65 79 20 3d 20 73 71         nKey = sq
22fe9 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
22fea 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20  s(argv[2]);.    
22feb 20 20 20 20 7a 4b 65 79 20 3d 20 28 63 68 61 72      zKey = (char
22fec 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
22fed 5f 62 6c 6f 62 28 61 72 67 76 5b 32 5d 29 3b 0a  _blob(argv[2]);.
22fee 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43          sqlite3C
22fef 6f 64 65 63 41 74 74 61 63 68 28 64 62 2c 20 64  odecAttach(db, d
22ff0 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79 2c 20  b->nDb-1, zKey, 
22ff1 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 62  nKey);.        b
22ff2 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20 63 61 73  reak;..      cas
22ff3 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3a 0a 20  e SQLITE_NULL:. 
22ff4 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20 6b 65 79         /* No key
22ff5 20 73 70 65 63 69 66 69 65 64 2e 20 20 55 73 65   specified.  Use
22ff6 20 74 68 65 20 6b 65 79 20 66 72 6f 6d 20 74 68   the key from th
22ff7 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
22ff8 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  */.        sqlit
22ff9 65 33 43 6f 64 65 63 47 65 74 4b 65 79 28 64 62  e3CodecGetKey(db
22ffa 2c 20 30 2c 20 28 76 6f 69 64 2a 2a 29 26 7a 4b  , 0, (void**)&zK
22ffb 65 79 2c 20 26 6e 4b 65 79 29 3b 0a 20 20 20 20  ey, &nKey);.    
22ffc 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 63      sqlite3Codec
22ffd 41 74 74 61 63 68 28 64 62 2c 20 64 62 2d 3e 6e  Attach(db, db->n
22ffe 44 62 2d 31 2c 20 7a 4b 65 79 2c 20 6e 4b 65 79  Db-1, zKey, nKey
22fff 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
23000 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
23001 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  if..  /* If the 
23002 66 69 6c 65 20 77 61 73 20 6f 70 65 6e 65 64 20  file was opened 
23003 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 72 65  successfully, re
23004 61 64 20 74 68 65 20 73 63 68 65 6d 61 20 66 6f  ad the schema fo
23005 72 20 74 68 65 20 6e 65 77 20 64 61 74 61 62 61  r the new databa
23006 73 65 2e 0a 20 20 2a 2a 20 49 66 20 74 68 69 73  se..  ** If this
23007 20 66 61 69 6c 73 2c 20 6f 72 20 69 66 20 6f 70   fails, or if op
23008 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65 20 66  ening the file f
23009 61 69 6c 65 64 2c 20 74 68 65 6e 20 63 6c 6f 73  ailed, then clos
2300a 65 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20 0a  e the file and .
2300b 20 20 2a 2a 20 72 65 6d 6f 76 65 20 74 68 65 20    ** remove the 
2300c 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 64  entry from the d
2300d 62 2d 3e 61 44 62 5b 5d 20 61 72 72 61 79 2e 20  b->aDb[] array. 
2300e 69 2e 65 2e 20 70 75 74 20 65 76 65 72 79 74 68  i.e. put everyth
2300f 69 6e 67 20 62 61 63 6b 20 74 68 65 20 77 61 79  ing back the way
23010 0a 20 20 2a 2a 20 77 65 20 66 6f 75 6e 64 20 69  .  ** we found i
23011 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  t..  */.  if( rc
23012 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
23013 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33     (void)sqlite3
23014 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20  SafetyOn(db);.  
23015 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
23016 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20  terAll(db);.    
23017 72 63 20 3d 20 73 71 6c 69 74 65 33 49 6e 69 74  rc = sqlite3Init
23018 28 64 62 2c 20 26 7a 45 72 72 44 79 6e 29 3b 0a  (db, &zErrDyn);.
23019 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
2301a 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20  LeaveAll(db);.  
2301b 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
2301c 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20  afetyOff(db);.  
2301d 7d 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  }.  if( rc ){.  
2301e 20 20 69 6e 74 20 69 44 62 20 3d 20 64 62 2d 3e    int iDb = db->
2301f 6e 44 62 20 2d 20 31 3b 0a 20 20 20 20 61 73 73  nDb - 1;.    ass
23020 65 72 74 28 20 69 44 62 3e 3d 32 20 29 3b 0a 20  ert( iDb>=2 );. 
23021 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69     if( db->aDb[i
23022 44 62 5d 2e 70 42 74 20 29 7b 0a 20 20 20 20 20  Db].pBt ){.     
23023 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f   sqlite3BtreeClo
23024 73 65 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  se(db->aDb[iDb].
23025 70 42 74 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e  pBt);.      db->
23026 61 44 62 5b 69 44 62 5d 2e 70 42 74 20 3d 20 30  aDb[iDb].pBt = 0
23027 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b  ;.      db->aDb[
23028 69 44 62 5d 2e 70 53 63 68 65 6d 61 20 3d 20 30  iDb].pSchema = 0
23029 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2302a 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c  te3ResetInternal
2302b 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20  Schema(db, 0);. 
2302c 20 20 20 64 62 2d 3e 6e 44 62 20 3d 20 69 44 62     db->nDb = iDb
2302d 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
2302e 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63  LITE_NOMEM || rc
2302f 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e  ==SQLITE_IOERR_N
23030 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 64 62  OMEM ){.      db
23031 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
23032 20 31 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   1;.      sqlite
23033 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
23034 44 79 6e 29 3b 0a 20 20 20 20 20 20 7a 45 72 72  Dyn);.      zErr
23035 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Dyn = sqlite3MPr
23036 69 6e 74 66 28 64 62 2c 20 22 6f 75 74 20 6f 66  intf(db, "out of
23037 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 20 20 7d   memory");.    }
23038 65 6c 73 65 20 69 66 28 20 7a 45 72 72 44 79 6e  else if( zErrDyn
23039 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a 45 72  ==0 ){.      zEr
2303a 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50  rDyn = sqlite3MP
2303b 72 69 6e 74 66 28 64 62 2c 20 22 75 6e 61 62 6c  rintf(db, "unabl
2303c 65 20 74 6f 20 6f 70 65 6e 20 64 61 74 61 62 61  e to open databa
2303d 73 65 3a 20 25 73 22 2c 20 7a 46 69 6c 65 29 3b  se: %s", zFile);
2303e 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20  .    }.    goto 
2303f 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20  attach_error;.  
23040 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e 3b 0a 0a  }.  .  return;..
23041 61 74 74 61 63 68 5f 65 72 72 6f 72 3a 0a 20 20  attach_error:.  
23042 2f 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72  /* Return an err
23043 6f 72 20 69 66 20 77 65 20 67 65 74 20 68 65 72  or if we get her
23044 65 20 2a 2f 0a 20 20 69 66 28 20 7a 45 72 72 44  e */.  if( zErrD
23045 79 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  yn ){.    sqlite
23046 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63  3_result_error(c
23047 6f 6e 74 65 78 74 2c 20 7a 45 72 72 44 79 6e 2c  ontext, zErrDyn,
23048 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   -1);.    sqlite
23049 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72  3DbFree(db, zErr
2304a 44 79 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Dyn);.  }.  if( 
2304b 72 63 20 29 20 73 71 6c 69 74 65 33 5f 72 65 73  rc ) sqlite3_res
2304c 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 63  ult_error_code(c
2304d 6f 6e 74 65 78 74 2c 20 72 63 29 3b 0a 7d 0a 0a  ontext, rc);.}..
2304e 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 75 73 65  /*.** An SQL use
2304f 72 2d 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73  r-function regis
23050 74 65 72 65 64 20 74 6f 20 64 6f 20 74 68 65 20  tered to do the 
23051 77 6f 72 6b 20 6f 66 20 61 6e 20 44 45 54 41 43  work of an DETAC
23052 48 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65  H statement. The
23053 0a 2a 2a 20 74 68 72 65 65 20 61 72 67 75 6d 65  .** three argume
23054 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e 63 74  nts to the funct
23055 69 6f 6e 20 63 6f 6d 65 20 64 69 72 65 63 74 6c  ion come directl
23056 79 20 66 72 6f 6d 20 61 20 64 65 74 61 63 68 20  y from a detach 
23057 73 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a  statement:.**.**
23058 20 20 20 20 20 44 45 54 41 43 48 20 44 41 54 41       DETACH DATA
23059 42 41 53 45 20 78 0a 2a 2a 0a 2a 2a 20 20 20 20  BASE x.**.**    
2305a 20 53 45 4c 45 43 54 20 73 71 6c 69 74 65 5f 64   SELECT sqlite_d
2305b 65 74 61 63 68 28 78 29 0a 2a 2f 0a 73 74 61 74  etach(x).*/.stat
2305c 69 63 20 76 6f 69 64 20 64 65 74 61 63 68 46 75  ic void detachFu
2305d 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
2305e 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
2305f 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20    int NotUsed,. 
23060 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
23061 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74  *argv.){.  const
23062 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 28   char *zName = (
23063 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c  const char *)sql
23064 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
23065 61 72 67 76 5b 30 5d 29 3b 0a 20 20 73 71 6c 69  argv[0]);.  sqli
23066 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65  te3 *db = sqlite
23067 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
23068 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  dle(context);.  
23069 69 6e 74 20 69 3b 0a 20 20 44 62 20 2a 70 44 62  int i;.  Db *pDb
2306a 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 7a 45 72   = 0;.  char zEr
2306b 72 5b 31 32 38 5d 3b 0a 0a 20 20 55 4e 55 53 45  r[128];..  UNUSE
2306c 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
2306d 73 65 64 29 3b 0a 0a 20 20 69 66 28 20 7a 4e 61  sed);..  if( zNa
2306e 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d 65 20 3d 20  me==0 ) zName = 
2306f 22 22 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  "";.  for(i=0; i
23070 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
23071 20 20 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61      pDb = &db->a
23072 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70  Db[i];.    if( p
23073 44 62 2d 3e 70 42 74 3d 3d 30 20 29 20 63 6f 6e  Db->pBt==0 ) con
23074 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 73  tinue;.    if( s
23075 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 44  qlite3StrICmp(pD
23076 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29  b->zName, zName)
23077 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d  ==0 ) break;.  }
23078 0a 0a 20 20 69 66 28 20 69 3e 3d 64 62 2d 3e 6e  ..  if( i>=db->n
23079 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  Db ){.    sqlite
2307a 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
2307b 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c 20 22 6e  f(zErr),zErr, "n
2307c 6f 20 73 75 63 68 20 64 61 74 61 62 61 73 65 3a  o such database:
2307d 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20   %s", zName);.  
2307e 20 20 67 6f 74 6f 20 64 65 74 61 63 68 5f 65 72    goto detach_er
2307f 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  ror;.  }.  if( i
23080 3c 32 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  <2 ){.    sqlite
23081 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
23082 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c 20 22 63  f(zErr),zErr, "c
23083 61 6e 6e 6f 74 20 64 65 74 61 63 68 20 64 61 74  annot detach dat
23084 61 62 61 73 65 20 25 73 22 2c 20 7a 4e 61 6d 65  abase %s", zName
23085 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 61  );.    goto deta
23086 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20  ch_error;.  }.  
23087 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d  if( !db->autoCom
23088 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  mit ){.    sqlit
23089 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
2308a 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 0a  of(zErr), zErr,.
2308b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2308c 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 44 45 54       "cannot DET
2308d 41 43 48 20 64 61 74 61 62 61 73 65 20 77 69 74  ACH database wit
2308e 68 69 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 22  hin transaction"
2308f 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 61  );.    goto deta
23090 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20  ch_error;.  }.  
23091 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  if( sqlite3Btree
23092 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 70 44  IsInReadTrans(pD
23093 62 2d 3e 70 42 74 29 20 7c 7c 20 73 71 6c 69 74  b->pBt) || sqlit
23094 65 33 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75  e3BtreeIsInBacku
23095 70 28 70 44 62 2d 3e 70 42 74 29 20 29 7b 0a 20  p(pDb->pBt) ){. 
23096 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
23097 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
23098 2c 7a 45 72 72 2c 20 22 64 61 74 61 62 61 73 65  ,zErr, "database
23099 20 25 73 20 69 73 20 6c 6f 63 6b 65 64 22 2c 20   %s is locked", 
2309a 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f  zName);.    goto
2309b 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   detach_error;. 
2309c 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72   }..  sqlite3Btr
2309d 65 65 43 6c 6f 73 65 28 70 44 62 2d 3e 70 42 74  eeClose(pDb->pBt
2309e 29 3b 0a 20 20 70 44 62 2d 3e 70 42 74 20 3d 20  );.  pDb->pBt = 
2309f 30 3b 0a 20 20 70 44 62 2d 3e 70 53 63 68 65 6d  0;.  pDb->pSchem
230a0 61 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  a = 0;.  sqlite3
230a1 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
230a2 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 72 65  ema(db, 0);.  re
230a3 74 75 72 6e 3b 0a 0a 64 65 74 61 63 68 5f 65 72  turn;..detach_er
230a4 72 6f 72 3a 0a 20 20 73 71 6c 69 74 65 33 5f 72  ror:.  sqlite3_r
230a5 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
230a6 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a  ext, zErr, -1);.
230a7 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72  }../*.** This pr
230a8 6f 63 65 64 75 72 65 20 67 65 6e 65 72 61 74 65  ocedure generate
230a9 73 20 56 44 42 45 20 63 6f 64 65 20 66 6f 72 20  s VDBE code for 
230aa 61 20 73 69 6e 67 6c 65 20 69 6e 76 6f 63 61 74  a single invocat
230ab 69 6f 6e 20 6f 66 20 65 69 74 68 65 72 20 74 68  ion of either th
230ac 65 0a 2a 2a 20 73 71 6c 69 74 65 5f 64 65 74 61  e.** sqlite_deta
230ad 63 68 28 29 20 6f 72 20 73 71 6c 69 74 65 5f 61  ch() or sqlite_a
230ae 74 74 61 63 68 28 29 20 53 51 4c 20 75 73 65 72  ttach() SQL user
230af 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73   functions..*/.s
230b0 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 41  tatic void codeA
230b1 74 74 61 63 68 28 0a 20 20 50 61 72 73 65 20 2a  ttach(.  Parse *
230b2 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a  pParse,       /*
230b3 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74   The parser cont
230b4 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 74 79 70  ext */.  int typ
230b5 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e,            /*
230b6 20 45 69 74 68 65 72 20 53 51 4c 49 54 45 5f 41   Either SQLITE_A
230b7 54 54 41 43 48 20 6f 72 20 53 51 4c 49 54 45 5f  TTACH or SQLITE_
230b8 44 45 54 41 43 48 20 2a 2f 0a 20 20 46 75 6e 63  DETACH */.  Func
230b9 44 65 66 20 2a 70 46 75 6e 63 2c 20 20 20 20 20  Def *pFunc,     
230ba 20 2f 2a 20 46 75 6e 63 44 65 66 20 77 72 61 70   /* FuncDef wrap
230bb 70 65 72 20 66 6f 72 20 64 65 74 61 63 68 46 75  per for detachFu
230bc 6e 63 28 29 20 6f 72 20 61 74 74 61 63 68 46 75  nc() or attachFu
230bd 6e 63 28 29 20 2a 2f 0a 20 20 45 78 70 72 20 2a  nc() */.  Expr *
230be 70 41 75 74 68 41 72 67 2c 20 20 20 20 20 20 2f  pAuthArg,      /
230bf 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  * Expression to 
230c0 70 61 73 73 20 74 6f 20 61 75 74 68 6f 72 69 7a  pass to authoriz
230c1 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 2a  ation callback *
230c2 2f 0a 20 20 45 78 70 72 20 2a 70 46 69 6c 65 6e  /.  Expr *pFilen
230c3 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ame,     /* Name
230c4 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c   of database fil
230c5 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 44 62  e */.  Expr *pDb
230c6 6e 61 6d 65 2c 20 20 20 20 20 20 20 2f 2a 20 4e  name,       /* N
230c7 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
230c8 61 73 65 20 74 6f 20 75 73 65 20 69 6e 74 65 72  ase to use inter
230c9 6e 61 6c 6c 79 20 2a 2f 0a 20 20 45 78 70 72 20  nally */.  Expr 
230ca 2a 70 4b 65 79 20 20 20 20 20 20 20 20 20 20 20  *pKey           
230cb 2f 2a 20 44 61 74 61 62 61 73 65 20 6b 65 79 20  /* Database key 
230cc 66 6f 72 20 65 6e 63 72 79 70 74 69 6f 6e 20 65  for encryption e
230cd 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  xtension */.){. 
230ce 20 69 6e 74 20 72 63 3b 0a 20 20 4e 61 6d 65 43   int rc;.  NameC
230cf 6f 6e 74 65 78 74 20 73 4e 61 6d 65 3b 0a 20 20  ontext sName;.  
230d0 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74  Vdbe *v;.  sqlit
230d1 65 33 2a 20 64 62 20 3d 20 70 50 61 72 73 65 2d  e3* db = pParse-
230d2 3e 64 62 3b 0a 20 20 69 6e 74 20 72 65 67 41 72  >db;.  int regAr
230d3 67 73 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73  gs;..  memset(&s
230d4 4e 61 6d 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Name, 0, sizeof(
230d5 4e 61 6d 65 43 6f 6e 74 65 78 74 29 29 3b 0a 20  NameContext));. 
230d6 20 73 4e 61 6d 65 2e 70 50 61 72 73 65 20 3d 20   sName.pParse = 
230d7 70 50 61 72 73 65 3b 0a 0a 20 20 69 66 28 20 0a  pParse;..  if( .
230d8 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21        SQLITE_OK!
230d9 3d 28 72 63 20 3d 20 72 65 73 6f 6c 76 65 41 74  =(rc = resolveAt
230da 74 61 63 68 45 78 70 72 28 26 73 4e 61 6d 65 2c  tachExpr(&sName,
230db 20 70 46 69 6c 65 6e 61 6d 65 29 29 20 7c 7c 0a   pFilename)) ||.
230dc 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21        SQLITE_OK!
230dd 3d 28 72 63 20 3d 20 72 65 73 6f 6c 76 65 41 74  =(rc = resolveAt
230de 74 61 63 68 45 78 70 72 28 26 73 4e 61 6d 65 2c  tachExpr(&sName,
230df 20 70 44 62 6e 61 6d 65 29 29 20 7c 7c 0a 20 20   pDbname)) ||.  
230e0 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28      SQLITE_OK!=(
230e1 72 63 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 61  rc = resolveAtta
230e2 63 68 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 70  chExpr(&sName, p
230e3 4b 65 79 29 29 0a 20 20 29 7b 0a 20 20 20 20 70  Key)).  ){.    p
230e4 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20  Parse->nErr++;. 
230e5 20 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65     goto attach_e
230e6 6e 64 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66  nd;.  }..#ifndef
230e7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
230e8 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28  HORIZATION.  if(
230e9 20 70 41 75 74 68 41 72 67 20 29 7b 0a 20 20 20   pAuthArg ){.   
230ea 20 63 68 61 72 20 2a 7a 41 75 74 68 41 72 67 20   char *zAuthArg 
230eb 3d 20 70 41 75 74 68 41 72 67 2d 3e 75 2e 7a 54  = pAuthArg->u.zT
230ec 6f 6b 65 6e 3b 0a 20 20 20 20 69 66 28 20 4e 45  oken;.    if( NE
230ed 56 45 52 28 7a 41 75 74 68 41 72 67 3d 3d 30 29  VER(zAuthArg==0)
230ee 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61   ){.      goto a
230ef 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 20 20 7d  ttach_end;.    }
230f0 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
230f1 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
230f2 65 2c 20 74 79 70 65 2c 20 7a 41 75 74 68 41 72  e, type, zAuthAr
230f3 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66  g, 0, 0);.    if
230f4 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  (rc!=SQLITE_OK )
230f5 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 74 74  {.      goto att
230f6 61 63 68 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 20  ach_end;.    }. 
230f7 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c   }.#endif /* SQL
230f8 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
230f9 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 0a 20 20 76 20  ZATION */...  v 
230fa 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
230fb 28 70 50 61 72 73 65 29 3b 0a 20 20 72 65 67 41  (pParse);.  regA
230fc 72 67 73 20 3d 20 73 71 6c 69 74 65 33 47 65 74  rgs = sqlite3Get
230fd 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65  TempRange(pParse
230fe 2c 20 34 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  , 4);.  sqlite3E
230ff 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
23100 70 46 69 6c 65 6e 61 6d 65 2c 20 72 65 67 41 72  pFilename, regAr
23101 67 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  gs);.  sqlite3Ex
23102 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
23103 44 62 6e 61 6d 65 2c 20 72 65 67 41 72 67 73 2b  Dbname, regArgs+
23104 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  1);.  sqlite3Exp
23105 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 4b  rCode(pParse, pK
23106 65 79 2c 20 72 65 67 41 72 67 73 2b 32 29 3b 0a  ey, regArgs+2);.
23107 0a 20 20 61 73 73 65 72 74 28 20 76 20 7c 7c 20  .  assert( v || 
23108 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
23109 20 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20   );.  if( v ){. 
2310a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2310b 64 4f 70 33 28 76 2c 20 4f 50 5f 46 75 6e 63 74  dOp3(v, OP_Funct
2310c 69 6f 6e 2c 20 30 2c 20 72 65 67 41 72 67 73 2b  ion, 0, regArgs+
2310d 33 2d 70 46 75 6e 63 2d 3e 6e 41 72 67 2c 20 72  3-pFunc->nArg, r
2310e 65 67 41 72 67 73 2b 33 29 3b 0a 20 20 20 20 61  egArgs+3);.    a
2310f 73 73 65 72 74 28 20 70 46 75 6e 63 2d 3e 6e 41  ssert( pFunc->nA
23110 72 67 3d 3d 2d 31 20 7c 7c 20 28 70 46 75 6e 63  rg==-1 || (pFunc
23111 2d 3e 6e 41 72 67 26 30 78 66 66 29 3d 3d 70 46  ->nArg&0xff)==pF
23112 75 6e 63 2d 3e 6e 41 72 67 20 29 3b 0a 20 20 20  unc->nArg );.   
23113 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
23114 67 65 50 35 28 76 2c 20 28 75 38 29 28 70 46 75  geP5(v, (u8)(pFu
23115 6e 63 2d 3e 6e 41 72 67 29 29 3b 0a 20 20 20 20  nc->nArg));.    
23116 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
23117 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 68 61 72  eP4(v, -1, (char
23118 20 2a 29 70 46 75 6e 63 2c 20 50 34 5f 46 55 4e   *)pFunc, P4_FUN
23119 43 44 45 46 29 3b 0a 0a 20 20 20 20 2f 2a 20 43  CDEF);..    /* C
2311a 6f 64 65 20 61 6e 20 4f 50 5f 45 78 70 69 72 65  ode an OP_Expire
2311b 2e 20 46 6f 72 20 61 6e 20 41 54 54 41 43 48 20  . For an ATTACH 
2311c 73 74 61 74 65 6d 65 6e 74 2c 20 73 65 74 20 50  statement, set P
2311d 31 20 74 6f 20 74 72 75 65 20 28 65 78 70 69 72  1 to true (expir
2311e 65 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 73 74  e this.    ** st
2311f 61 74 65 6d 65 6e 74 20 6f 6e 6c 79 29 2e 20 46  atement only). F
23120 6f 72 20 44 45 54 41 43 48 2c 20 73 65 74 20 69  or DETACH, set i
23121 74 20 74 6f 20 66 61 6c 73 65 20 28 65 78 70 69  t to false (expi
23122 72 65 20 61 6c 6c 20 65 78 69 73 74 69 6e 67 0a  re all existing.
23123 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74      ** statement
23124 73 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  s)..    */.    s
23125 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
23126 28 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 28  (v, OP_Expire, (
23127 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 41 54 54  type==SQLITE_ATT
23128 41 43 48 29 29 3b 0a 20 20 7d 0a 20 20 0a 61 74  ACH));.  }.  .at
23129 74 61 63 68 5f 65 6e 64 3a 0a 20 20 73 71 6c 69  tach_end:.  sqli
2312a 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
2312b 2c 20 70 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  , pFilename);.  
2312c 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
2312d 65 28 64 62 2c 20 70 44 62 6e 61 6d 65 29 3b 0a  e(db, pDbname);.
2312e 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
2312f 65 74 65 28 64 62 2c 20 70 4b 65 79 29 3b 0a 7d  ete(db, pKey);.}
23130 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 62  ../*.** Called b
23131 79 20 74 68 65 20 70 61 72 73 65 72 20 74 6f 20  y the parser to 
23132 63 6f 6d 70 69 6c 65 20 61 20 44 45 54 41 43 48  compile a DETACH
23133 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a   statement..**.*
23134 2a 20 20 20 20 20 44 45 54 41 43 48 20 70 44 62  *     DETACH pDb
23135 6e 61 6d 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  name.*/.SQLITE_P
23136 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
23137 74 65 33 44 65 74 61 63 68 28 50 61 72 73 65 20  te3Detach(Parse 
23138 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
23139 44 62 6e 61 6d 65 29 7b 0a 20 20 73 74 61 74 69  Dbname){.  stati
2313a 63 20 46 75 6e 63 44 65 66 20 64 65 74 61 63 68  c FuncDef detach
2313b 5f 66 75 6e 63 20 3d 20 7b 0a 20 20 20 20 31 2c  _func = {.    1,
2313c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2313d 2f 2a 20 6e 41 72 67 20 2a 2f 0a 20 20 20 20 53  /* nArg */.    S
2313e 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 20  QLITE_UTF8,     
2313f 20 2f 2a 20 69 50 72 65 66 45 6e 63 20 2a 2f 0a   /* iPrefEnc */.
23140 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20      0,          
23141 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 2a        /* flags *
23142 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
23143 20 20 20 20 20 20 20 20 2f 2a 20 70 55 73 65 72          /* pUser
23144 44 61 74 61 20 2a 2f 0a 20 20 20 20 30 2c 20 20  Data */.    0,  
23145 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
23146 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 64 65   pNext */.    de
23147 74 61 63 68 46 75 6e 63 2c 20 20 20 20 20 20 20  tachFunc,       
23148 2f 2a 20 78 46 75 6e 63 20 2a 2f 0a 20 20 20 20  /* xFunc */.    
23149 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
2314a 20 20 2f 2a 20 78 53 74 65 70 20 2a 2f 0a 20 20    /* xStep */.  
2314b 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
2314c 20 20 20 20 2f 2a 20 78 46 69 6e 61 6c 69 7a 65      /* xFinalize
2314d 20 2a 2f 0a 20 20 20 20 22 73 71 6c 69 74 65 5f   */.    "sqlite_
2314e 64 65 74 61 63 68 22 2c 20 20 2f 2a 20 7a 4e 61  detach",  /* zNa
2314f 6d 65 20 2a 2f 0a 20 20 20 20 30 20 20 20 20 20  me */.    0     
23150 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70              /* p
23151 48 61 73 68 20 2a 2f 0a 20 20 7d 3b 0a 20 20 63  Hash */.  };.  c
23152 6f 64 65 41 74 74 61 63 68 28 70 50 61 72 73 65  odeAttach(pParse
23153 2c 20 53 51 4c 49 54 45 5f 44 45 54 41 43 48 2c  , SQLITE_DETACH,
23154 20 26 64 65 74 61 63 68 5f 66 75 6e 63 2c 20 70   &detach_func, p
23155 44 62 6e 61 6d 65 2c 20 30 2c 20 30 2c 20 70 44  Dbname, 0, 0, pD
23156 62 6e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  bname);.}../*.**
23157 20 43 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70   Called by the p
23158 61 72 73 65 72 20 74 6f 20 63 6f 6d 70 69 6c 65  arser to compile
23159 20 61 6e 20 41 54 54 41 43 48 20 73 74 61 74 65   an ATTACH state
2315a 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ment..**.**     
2315b 41 54 54 41 43 48 20 70 20 41 53 20 70 44 62 6e  ATTACH p AS pDbn
2315c 61 6d 65 20 4b 45 59 20 70 4b 65 79 0a 2a 2f 0a  ame KEY pKey.*/.
2315d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2315e 6f 69 64 20 73 71 6c 69 74 65 33 41 74 74 61 63  oid sqlite3Attac
2315f 68 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  h(Parse *pParse,
23160 20 45 78 70 72 20 2a 70 2c 20 45 78 70 72 20 2a   Expr *p, Expr *
23161 70 44 62 6e 61 6d 65 2c 20 45 78 70 72 20 2a 70  pDbname, Expr *p
23162 4b 65 79 29 7b 0a 20 20 73 74 61 74 69 63 20 46  Key){.  static F
23163 75 6e 63 44 65 66 20 61 74 74 61 63 68 5f 66 75  uncDef attach_fu
23164 6e 63 20 3d 20 7b 0a 20 20 20 20 33 2c 20 20 20  nc = {.    3,   
23165 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
23166 6e 41 72 67 20 2a 2f 0a 20 20 20 20 53 51 4c 49  nArg */.    SQLI
23167 54 45 5f 55 54 46 38 2c 20 20 20 20 20 20 2f 2a  TE_UTF8,      /*
23168 20 69 50 72 65 66 45 6e 63 20 2a 2f 0a 20 20 20   iPrefEnc */.   
23169 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
2316a 20 20 20 2f 2a 20 66 6c 61 67 73 20 2a 2f 0a 20     /* flags */. 
2316b 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
2316c 20 20 20 20 20 2f 2a 20 70 55 73 65 72 44 61 74       /* pUserDat
2316d 61 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20  a */.    0,     
2316e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e             /* pN
2316f 65 78 74 20 2a 2f 0a 20 20 20 20 61 74 74 61 63  ext */.    attac
23170 68 46 75 6e 63 2c 20 20 20 20 20 20 20 2f 2a 20  hFunc,       /* 
23171 78 46 75 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20  xFunc */.    0, 
23172 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
23173 2a 20 78 53 74 65 70 20 2a 2f 0a 20 20 20 20 30  * xStep */.    0
23174 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
23175 20 2f 2a 20 78 46 69 6e 61 6c 69 7a 65 20 2a 2f   /* xFinalize */
23176 0a 20 20 20 20 22 73 71 6c 69 74 65 5f 61 74 74  .    "sqlite_att
23177 61 63 68 22 2c 20 20 2f 2a 20 7a 4e 61 6d 65 20  ach",  /* zName 
23178 2a 2f 0a 20 20 20 20 30 20 20 20 20 20 20 20 20  */.    0        
23179 20 20 20 20 20 20 20 20 20 2f 2a 20 70 48 61 73           /* pHas
2317a 68 20 2a 2f 0a 20 20 7d 3b 0a 20 20 63 6f 64 65  h */.  };.  code
2317b 41 74 74 61 63 68 28 70 50 61 72 73 65 2c 20 53  Attach(pParse, S
2317c 51 4c 49 54 45 5f 41 54 54 41 43 48 2c 20 26 61  QLITE_ATTACH, &a
2317d 74 74 61 63 68 5f 66 75 6e 63 2c 20 70 2c 20 70  ttach_func, p, p
2317e 2c 20 70 44 62 6e 61 6d 65 2c 20 70 4b 65 79 29  , pDbname, pKey)
2317f 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
23180 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48  LITE_OMIT_ATTACH
23181 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69   */../*.** Initi
23182 61 6c 69 7a 65 20 61 20 44 62 46 69 78 65 72 20  alize a DbFixer 
23183 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 69 73  structure.  This
23184 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65   routine must be
23185 20 63 61 6c 6c 65 64 20 70 72 69 6f 72 0a 2a 2a   called prior.**
23186 20 74 6f 20 70 61 73 73 69 6e 67 20 74 68 65 20   to passing the 
23187 73 74 72 75 63 74 75 72 65 20 74 6f 20 6f 6e 65  structure to one
23188 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 46 69   of the sqliteFi
23189 78 41 41 41 41 28 29 20 72 6f 75 74 69 6e 65 73  xAAAA() routines
2318a 20 62 65 6c 6f 77 2e 0a 2a 2a 0a 2a 2a 20 54 68   below..**.** Th
2318b 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69  e return value i
2318c 6e 64 69 63 61 74 65 73 20 77 68 65 74 68 65 72  ndicates whether
2318d 20 6f 72 20 6e 6f 74 20 66 69 78 61 74 69 6f 6e   or not fixation
2318e 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 20 54   is required.  T
2318f 52 55 45 0a 2a 2a 20 6d 65 61 6e 73 20 77 65 20  RUE.** means we 
23190 64 6f 20 6e 65 65 64 20 74 6f 20 66 69 78 20 74  do need to fix t
23191 68 65 20 64 61 74 61 62 61 73 65 20 72 65 66 65  he database refe
23192 72 65 6e 63 65 73 2c 20 46 41 4c 53 45 20 6d 65  rences, FALSE me
23193 61 6e 73 20 77 65 20 64 6f 20 6e 6f 74 2e 0a 2a  ans we do not..*
23194 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
23195 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 49   int sqlite3FixI
23196 6e 69 74 28 0a 20 20 44 62 46 69 78 65 72 20 2a  nit(.  DbFixer *
23197 70 46 69 78 2c 20 20 20 20 20 20 2f 2a 20 54 68  pFix,      /* Th
23198 65 20 66 69 78 65 72 20 74 6f 20 62 65 20 69 6e  e fixer to be in
23199 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 50  itialized */.  P
2319a 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2319b 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73     /* Error mess
2319c 61 67 65 73 20 77 69 6c 6c 20 62 65 20 77 72 69  ages will be wri
2319d 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69  tten here */.  i
2319e 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20 20  nt iDb,         
2319f 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68     /* This is th
231a0 65 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20  e database that 
231a1 6d 75 73 74 20 62 65 20 75 73 65 64 20 2a 2f 0a  must be used */.
231a2 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
231a3 79 70 65 2c 20 20 2f 2a 20 22 76 69 65 77 22 2c  ype,  /* "view",
231a4 20 22 74 72 69 67 67 65 72 22 2c 20 6f 72 20 22   "trigger", or "
231a5 69 6e 64 65 78 22 20 2a 2f 0a 20 20 63 6f 6e 73  index" */.  cons
231a6 74 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 20  t Token *pName  
231a7 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 76  /* Name of the v
231a8 69 65 77 2c 20 74 72 69 67 67 65 72 2c 20 6f 72  iew, trigger, or
231a9 20 69 6e 64 65 78 20 2a 2f 0a 29 7b 0a 20 20 73   index */.){.  s
231aa 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69  qlite3 *db;..  i
231ab 66 28 20 4e 45 56 45 52 28 69 44 62 3c 30 29 20  f( NEVER(iDb<0) 
231ac 7c 7c 20 69 44 62 3d 3d 31 20 29 20 72 65 74 75  || iDb==1 ) retu
231ad 72 6e 20 30 3b 0a 20 20 64 62 20 3d 20 70 50 61  rn 0;.  db = pPa
231ae 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72  rse->db;.  asser
231af 74 28 20 64 62 2d 3e 6e 44 62 3e 69 44 62 20 29  t( db->nDb>iDb )
231b0 3b 0a 20 20 70 46 69 78 2d 3e 70 50 61 72 73 65  ;.  pFix->pParse
231b1 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 70 46 69   = pParse;.  pFi
231b2 78 2d 3e 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62  x->zDb = db->aDb
231b3 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 70  [iDb].zName;.  p
231b4 46 69 78 2d 3e 7a 54 79 70 65 20 3d 20 7a 54 79  Fix->zType = zTy
231b5 70 65 3b 0a 20 20 70 46 69 78 2d 3e 70 4e 61 6d  pe;.  pFix->pNam
231b6 65 20 3d 20 70 4e 61 6d 65 3b 0a 20 20 72 65 74  e = pName;.  ret
231b7 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 1;.}../*.** 
231b8 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65  The following se
231b9 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 77 61  t of routines wa
231ba 6c 6b 20 74 68 72 6f 75 67 68 20 74 68 65 20 70  lk through the p
231bb 61 72 73 65 20 74 72 65 65 20 61 6e 64 20 61 73  arse tree and as
231bc 73 69 67 6e 0a 2a 2a 20 61 20 73 70 65 63 69 66  sign.** a specif
231bd 69 63 20 64 61 74 61 62 61 73 65 20 74 6f 20 61  ic database to a
231be 6c 6c 20 74 61 62 6c 65 20 72 65 66 65 72 65 6e  ll table referen
231bf 63 65 73 20 77 68 65 72 65 20 74 68 65 20 64 61  ces where the da
231c0 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 77  tabase name.** w
231c1 61 73 20 6c 65 66 74 20 75 6e 73 70 65 63 69 66  as left unspecif
231c2 69 65 64 20 69 6e 20 74 68 65 20 6f 72 69 67 69  ied in the origi
231c3 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  nal SQL statemen
231c4 74 2e 20 20 54 68 65 20 70 46 69 78 20 73 74 72  t.  The pFix str
231c5 75 63 74 75 72 65 0a 2a 2a 20 6d 75 73 74 20 68  ucture.** must h
231c6 61 76 65 20 62 65 65 6e 20 69 6e 69 74 69 61 6c  ave been initial
231c7 69 7a 65 64 20 62 79 20 61 20 70 72 69 6f 72 20  ized by a prior 
231c8 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 46  call to sqlite3F
231c9 69 78 49 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 2a 20  ixInit()..**.** 
231ca 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  These routines a
231cb 72 65 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 20  re used to make 
231cc 73 75 72 65 20 74 68 61 74 20 61 6e 20 69 6e 64  sure that an ind
231cd 65 78 2c 20 74 72 69 67 67 65 72 2c 20 6f 72 0a  ex, trigger, or.
231ce 2a 2a 20 76 69 65 77 20 69 6e 20 6f 6e 65 20 64  ** view in one d
231cf 61 74 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74  atabase does not
231d0 20 72 65 66 65 72 20 74 6f 20 6f 62 6a 65 63 74   refer to object
231d1 73 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74  s in a different
231d2 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 28 45   database..** (E
231d3 78 63 65 70 74 69 6f 6e 3a 20 69 6e 64 69 63 65  xception: indice
231d4 73 2c 20 74 72 69 67 67 65 72 73 2c 20 61 6e 64  s, triggers, and
231d5 20 76 69 65 77 73 20 69 6e 20 74 68 65 20 54 45   views in the TE
231d6 4d 50 20 64 61 74 61 62 61 73 65 20 61 72 65 0a  MP database are.
231d7 2a 2a 20 61 6c 6c 6f 77 65 64 20 74 6f 20 72 65  ** allowed to re
231d8 66 65 72 20 74 6f 20 61 6e 79 74 68 69 6e 67 2e  fer to anything.
231d9 29 20 20 49 66 20 61 20 72 65 66 65 72 65 6e 63  )  If a referenc
231da 65 20 69 73 20 65 78 70 6c 69 63 69 74 6c 79 20  e is explicitly 
231db 6d 61 64 65 0a 2a 2a 20 74 6f 20 61 6e 20 6f 62  made.** to an ob
231dc 6a 65 63 74 20 69 6e 20 61 20 64 69 66 66 65 72  ject in a differ
231dd 65 6e 74 20 64 61 74 61 62 61 73 65 2c 20 61 6e  ent database, an
231de 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
231df 73 20 61 64 64 65 64 20 74 6f 0a 2a 2a 20 70 50  s added to.** pP
231e0 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 61 6e  arse->zErrMsg an
231e1 64 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  d these routines
231e2 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f   return non-zero
231e3 2e 20 20 49 66 20 65 76 65 72 79 74 68 69 6e 67  .  If everything
231e4 0a 2a 2a 20 63 68 65 63 6b 73 20 6f 75 74 2c 20  .** checks out, 
231e5 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72  these routines r
231e6 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49  eturn 0..*/.SQLI
231e7 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
231e8 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74  qlite3FixSrcList
231e9 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46 69  (.  DbFixer *pFi
231ea 78 2c 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74  x,       /* Cont
231eb 65 78 74 20 6f 66 20 74 68 65 20 66 69 78 61 74  ext of the fixat
231ec 69 6f 6e 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ion */.  SrcList
231ed 20 2a 70 4c 69 73 74 20 20 20 20 20 20 20 2f 2a   *pList       /*
231ee 20 54 68 65 20 53 6f 75 72 63 65 20 6c 69 73 74   The Source list
231ef 20 74 6f 20 63 68 65 63 6b 20 61 6e 64 20 6d 6f   to check and mo
231f0 64 69 66 79 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  dify */.){.  int
231f1 20 69 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   i;.  const char
231f2 20 2a 7a 44 62 3b 0a 20 20 73 74 72 75 63 74 20   *zDb;.  struct 
231f3 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
231f4 74 65 6d 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45  tem;..  if( NEVE
231f5 52 28 70 4c 69 73 74 3d 3d 30 29 20 29 20 72 65  R(pList==0) ) re
231f6 74 75 72 6e 20 30 3b 0a 20 20 7a 44 62 20 3d 20  turn 0;.  zDb = 
231f7 70 46 69 78 2d 3e 7a 44 62 3b 0a 20 20 66 6f 72  pFix->zDb;.  for
231f8 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73  (i=0, pItem=pLis
231f9 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  t->a; i<pList->n
231fa 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  Src; i++, pItem+
231fb 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74 65  +){.    if( pIte
231fc 6d 2d 3e 7a 44 61 74 61 62 61 73 65 3d 3d 30 20  m->zDatabase==0 
231fd 29 7b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e  ){.      pItem->
231fe 7a 44 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69  zDatabase = sqli
231ff 74 65 33 44 62 53 74 72 44 75 70 28 70 46 69 78  te3DbStrDup(pFix
23200 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 44  ->pParse->db, zD
23201 62 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  b);.    }else if
23202 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
23203 28 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73  (pItem->zDatabas
23204 65 2c 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20  e,zDb)!=0 ){.   
23205 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
23206 73 67 28 70 46 69 78 2d 3e 70 50 61 72 73 65 2c  sg(pFix->pParse,
23207 0a 20 20 20 20 20 20 20 20 20 22 25 73 20 25 54  .         "%s %T
23208 20 63 61 6e 6e 6f 74 20 72 65 66 65 72 65 6e 63   cannot referenc
23209 65 20 6f 62 6a 65 63 74 73 20 69 6e 20 64 61 74  e objects in dat
2320a 61 62 61 73 65 20 25 73 22 2c 0a 20 20 20 20 20  abase %s",.     
2320b 20 20 20 20 70 46 69 78 2d 3e 7a 54 79 70 65 2c      pFix->zType,
2320c 20 70 46 69 78 2d 3e 70 4e 61 6d 65 2c 20 70 49   pFix->pName, pI
2320d 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b  tem->zDatabase);
2320e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
2320f 0a 20 20 20 20 7d 0a 23 69 66 20 21 64 65 66 69  .    }.#if !defi
23210 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
23211 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65  VIEW) || !define
23212 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
23213 49 47 47 45 52 29 0a 20 20 20 20 69 66 28 20 73  IGGER).    if( s
23214 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28  qlite3FixSelect(
23215 70 46 69 78 2c 20 70 49 74 65 6d 2d 3e 70 53 65  pFix, pItem->pSe
23216 6c 65 63 74 29 20 29 20 72 65 74 75 72 6e 20 31  lect) ) return 1
23217 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
23218 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70  3FixExpr(pFix, p
23219 49 74 65 6d 2d 3e 70 4f 6e 29 20 29 20 72 65 74  Item->pOn) ) ret
2321a 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 20 20  urn 1;.#endif.  
2321b 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
2321c 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
2321d 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c  ITE_OMIT_VIEW) |
2321e 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  | !defined(SQLIT
2321f 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a  E_OMIT_TRIGGER).
23220 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
23221 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c  nt sqlite3FixSel
23222 65 63 74 28 0a 20 20 44 62 46 69 78 65 72 20 2a  ect(.  DbFixer *
23223 70 46 69 78 2c 20 20 20 20 20 20 20 2f 2a 20 43  pFix,       /* C
23224 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69  ontext of the fi
23225 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65  xation */.  Sele
23226 63 74 20 2a 70 53 65 6c 65 63 74 20 20 20 20 20  ct *pSelect     
23227 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73   /* The SELECT s
23228 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 66  tatement to be f
23229 69 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61  ixed to one data
2322a 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69  base */.){.  whi
2322b 6c 65 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20  le( pSelect ){. 
2322c 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
2322d 78 45 78 70 72 4c 69 73 74 28 70 46 69 78 2c 20  xExprList(pFix, 
2322e 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 29  pSelect->pEList)
2322f 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
23230 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   1;.    }.    if
23231 28 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c  ( sqlite3FixSrcL
23232 69 73 74 28 70 46 69 78 2c 20 70 53 65 6c 65 63  ist(pFix, pSelec
23233 74 2d 3e 70 53 72 63 29 20 29 7b 0a 20 20 20 20  t->pSrc) ){.    
23234 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
23235 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  }.    if( sqlite
23236 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70  3FixExpr(pFix, p
23237 53 65 6c 65 63 74 2d 3e 70 57 68 65 72 65 29 20  Select->pWhere) 
23238 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
23239 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  1;.    }.    if(
2323a 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28   sqlite3FixExpr(
2323b 70 46 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70  pFix, pSelect->p
2323c 48 61 76 69 6e 67 29 20 29 7b 0a 20 20 20 20 20  Having) ){.     
2323d 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
2323e 0a 20 20 20 20 70 53 65 6c 65 63 74 20 3d 20 70  .    pSelect = p
2323f 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 3b 0a  Select->pPrior;.
23240 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
23241 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
23242 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45   int sqlite3FixE
23243 78 70 72 28 0a 20 20 44 62 46 69 78 65 72 20 2a  xpr(.  DbFixer *
23244 70 46 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e  pFix,     /* Con
23245 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78 61  text of the fixa
23246 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a  tion */.  Expr *
23247 70 45 78 70 72 20 20 20 20 20 20 20 20 2f 2a 20  pExpr        /* 
23248 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  The expression t
23249 6f 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f 6e  o be fixed to on
2324a 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b  e database */.){
2324b 0a 20 20 77 68 69 6c 65 28 20 70 45 78 70 72 20  .  while( pExpr 
2324c 29 7b 0a 20 20 20 20 69 66 28 20 45 78 70 72 48  ){.    if( ExprH
2324d 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45  asAnyProperty(pE
2324e 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  xpr, EP_TokenOnl
2324f 79 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  y) ) break;.    
23250 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
23251 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49  rty(pExpr, EP_xI
23252 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20  sSelect) ){.    
23253 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
23254 53 65 6c 65 63 74 28 70 46 69 78 2c 20 70 45 78  Select(pFix, pEx
23255 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 20 29  pr->x.pSelect) )
23256 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
23257 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20  else{.      if( 
23258 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69  sqlite3FixExprLi
23259 73 74 28 70 46 69 78 2c 20 70 45 78 70 72 2d 3e  st(pFix, pExpr->
2325a 78 2e 70 4c 69 73 74 29 20 29 20 72 65 74 75 72  x.pList) ) retur
2325b 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  n 1;.    }.    i
2325c 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70  f( sqlite3FixExp
2325d 72 28 70 46 69 78 2c 20 70 45 78 70 72 2d 3e 70  r(pFix, pExpr->p
2325e 52 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20  Right) ){.      
2325f 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
23260 20 20 20 20 70 45 78 70 72 20 3d 20 70 45 78 70      pExpr = pExp
23261 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20  r->pLeft;.  }.  
23262 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c 49  return 0;.}.SQLI
23263 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
23264 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c 69 73  qlite3FixExprLis
23265 74 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46  t(.  DbFixer *pF
23266 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65  ix,     /* Conte
23267 78 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 69  xt of the fixati
23268 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  on */.  ExprList
23269 20 2a 70 4c 69 73 74 20 20 20 20 2f 2a 20 54 68   *pList    /* Th
2326a 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20  e expression to 
2326b 62 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 20  be fixed to one 
2326c 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20  database */.){. 
2326d 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74   int i;.  struct
2326e 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
2326f 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 70 4c 69  pItem;.  if( pLi
23270 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  st==0 ) return 0
23271 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74  ;.  for(i=0, pIt
23272 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70  em=pList->a; i<p
23273 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
23274 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
23275 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78  if( sqlite3FixEx
23276 70 72 28 70 46 69 78 2c 20 70 49 74 65 6d 2d 3e  pr(pFix, pItem->
23277 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20  pExpr) ){.      
23278 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
23279 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
2327a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  }.#endif..#ifnde
2327b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
2327c 49 47 47 45 52 0a 53 51 4c 49 54 45 5f 50 52 49  IGGER.SQLITE_PRI
2327d 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2327e 46 69 78 54 72 69 67 67 65 72 53 74 65 70 28 0a  FixTriggerStep(.
2327f 20 20 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c    DbFixer *pFix,
23280 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
23281 6f 66 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20  of the fixation 
23282 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65 70  */.  TriggerStep
23283 20 2a 70 53 74 65 70 20 2f 2a 20 54 68 65 20 74   *pStep /* The t
23284 72 69 67 67 65 72 20 73 74 65 70 20 62 65 20 66  rigger step be f
23285 69 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61  ixed to one data
23286 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69  base */.){.  whi
23287 6c 65 28 20 70 53 74 65 70 20 29 7b 0a 20 20 20  le( pStep ){.   
23288 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 53   if( sqlite3FixS
23289 65 6c 65 63 74 28 70 46 69 78 2c 20 70 53 74 65  elect(pFix, pSte
2328a 70 2d 3e 70 53 65 6c 65 63 74 29 20 29 7b 0a 20  p->pSelect) ){. 
2328b 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
2328c 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
2328d 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78  ite3FixExpr(pFix
2328e 2c 20 70 53 74 65 70 2d 3e 70 57 68 65 72 65 29  , pStep->pWhere)
2328f 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
23290 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   1;.    }.    if
23291 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72  ( sqlite3FixExpr
23292 4c 69 73 74 28 70 46 69 78 2c 20 70 53 74 65 70  List(pFix, pStep
23293 2d 3e 70 45 78 70 72 4c 69 73 74 29 20 29 7b 0a  ->pExprList) ){.
23294 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
23295 20 20 20 20 7d 0a 20 20 20 20 70 53 74 65 70 20      }.    pStep 
23296 3d 20 70 53 74 65 70 2d 3e 70 4e 65 78 74 3b 0a  = pStep->pNext;.
23297 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
23298 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  }.#endif../*****
23299 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
2329a 20 61 74 74 61 63 68 2e 63 20 2a 2a 2a 2a 2a 2a   attach.c ******
2329b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2329c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2329d 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2329e 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
2329f 66 69 6c 65 20 61 75 74 68 2e 63 20 2a 2a 2a 2a  file auth.c ****
232a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
232a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
232a2 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
232a3 32 30 30 33 20 4a 61 6e 75 61 72 79 20 31 31 0a  2003 January 11.
232a4 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
232a5 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
232a6 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
232a7 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
232a8 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
232a9 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
232aa 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
232ab 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
232ac 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
232ad 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
232ae 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
232af 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
232b0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
232b1 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
232b2 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
232b3 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
232b4 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
232b5 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
232b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
232b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
232b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
232b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
232ba 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
232bb 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74  ains code used t
232bc 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
232bd 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68  sqlite3_set_auth
232be 6f 72 69 7a 65 72 28 29 0a 2a 2a 20 41 50 49 2e  orizer().** API.
232bf 20 20 54 68 69 73 20 66 61 63 69 6c 69 74 79 20    This facility 
232c0 69 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 66  is an optional f
232c1 65 61 74 75 72 65 20 6f 66 20 74 68 65 20 6c 69  eature of the li
232c2 62 72 61 72 79 2e 20 20 45 6d 62 65 64 64 65 64  brary.  Embedded
232c3 0a 2a 2a 20 73 79 73 74 65 6d 73 20 74 68 61 74  .** systems that
232c4 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 68 69   do not need thi
232c5 73 20 66 61 63 69 6c 69 74 79 20 6d 61 79 20 6f  s facility may o
232c6 6d 69 74 20 69 74 20 62 79 20 72 65 63 6f 6d 70  mit it by recomp
232c7 69 6c 69 6e 67 0a 2a 2a 20 74 68 65 20 6c 69 62  iling.** the lib
232c8 72 61 72 79 20 77 69 74 68 20 2d 44 53 51 4c 49  rary with -DSQLI
232c9 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
232ca 41 54 49 4f 4e 3d 31 0a 2a 2a 0a 2a 2a 20 24 49  ATION=1.**.** $I
232cb 64 3a 20 61 75 74 68 2e 63 2c 76 20 31 2e 33 32  d: auth.c,v 1.32
232cc 20 32 30 30 39 2f 30 37 2f 30 32 20 31 38 3a 34   2009/07/02 18:4
232cd 30 3a 33 35 20 64 61 6e 69 65 6c 6b 31 39 37 37  0:35 danielk1977
232ce 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
232cf 20 41 6c 6c 20 6f 66 20 74 68 65 20 63 6f 64 65   All of the code
232d0 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6d 61   in this file ma
232d1 79 20 62 65 20 6f 6d 69 74 74 65 64 20 62 79 20  y be omitted by 
232d2 64 65 66 69 6e 69 6e 67 20 61 20 73 69 6e 67 6c  defining a singl
232d3 65 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23  e.** macro..*/.#
232d4 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
232d5 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
232d6 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 20 63  ../*.** Set or c
232d7 6c 65 61 72 20 74 68 65 20 61 63 63 65 73 73 20  lear the access 
232d8 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75  authorization fu
232d9 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  nction..**.** Th
232da 65 20 61 63 63 65 73 73 20 61 75 74 68 6f 72 69  e access authori
232db 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  zation function 
232dc 69 73 20 62 65 20 63 61 6c 6c 65 64 20 64 75 72  is be called dur
232dd 69 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 61 74  ing the compilat
232de 69 6f 6e 0a 2a 2a 20 70 68 61 73 65 20 74 6f 20  ion.** phase to 
232df 76 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20  verify that the 
232e0 75 73 65 72 20 68 61 73 20 72 65 61 64 20 61 6e  user has read an
232e1 64 2f 6f 72 20 77 72 69 74 65 20 61 63 63 65 73  d/or write acces
232e2 73 20 70 65 72 6d 69 73 73 69 6f 6e 20 6f 6e 0a  s permission on.
232e3 2a 2a 20 76 61 72 69 6f 75 73 20 66 69 65 6c 64  ** various field
232e4 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  s of the databas
232e5 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72  e.  The first ar
232e6 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 61 75  gument to the au
232e7 74 68 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69  th function.** i
232e8 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
232e9 33 72 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  3rd argument to 
232ea 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 54  this routine.  T
232eb 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
232ec 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 61 75 74  nt.** to the aut
232ed 68 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e  h function is on
232ee 65 20 6f 66 20 74 68 65 73 65 20 63 6f 6e 73 74  e of these const
232ef 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ants:.**.**     
232f0 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f    SQLITE_CREATE_
232f1 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53  INDEX.**       S
232f2 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42  QLITE_CREATE_TAB
232f3 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  LE.**       SQLI
232f4 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49  TE_CREATE_TEMP_I
232f5 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51  NDEX.**       SQ
232f6 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50  LITE_CREATE_TEMP
232f7 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20  _TABLE.**       
232f8 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45  SQLITE_CREATE_TE
232f9 4d 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20  MP_TRIGGER.**   
232fa 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54      SQLITE_CREAT
232fb 45 5f 54 45 4d 50 5f 56 49 45 57 0a 2a 2a 20 20  E_TEMP_VIEW.**  
232fc 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41       SQLITE_CREA
232fd 54 45 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20  TE_TRIGGER.**   
232fe 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54      SQLITE_CREAT
232ff 45 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20  E_VIEW.**       
23300 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 0a 2a 2a  SQLITE_DELETE.**
23301 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52         SQLITE_DR
23302 4f 50 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20  OP_INDEX.**     
23303 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 41    SQLITE_DROP_TA
23304 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  BLE.**       SQL
23305 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e  ITE_DROP_TEMP_IN
23306 44 45 58 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  DEX.**       SQL
23307 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 41  ITE_DROP_TEMP_TA
23308 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c  BLE.**       SQL
23309 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52  ITE_DROP_TEMP_TR
2330a 49 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53  IGGER.**       S
2330b 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f  QLITE_DROP_TEMP_
2330c 56 49 45 57 0a 2a 2a 20 20 20 20 20 20 20 53 51  VIEW.**       SQ
2330d 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47 45  LITE_DROP_TRIGGE
2330e 52 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  R.**       SQLIT
2330f 45 5f 44 52 4f 50 5f 56 49 45 57 0a 2a 2a 20 20  E_DROP_VIEW.**  
23310 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4e 53 45       SQLITE_INSE
23311 52 54 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  RT.**       SQLI
23312 54 45 5f 50 52 41 47 4d 41 0a 2a 2a 20 20 20 20  TE_PRAGMA.**    
23313 20 20 20 53 51 4c 49 54 45 5f 52 45 41 44 0a 2a     SQLITE_READ.*
23314 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53  *       SQLITE_S
23315 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 53  ELECT.**       S
23316 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f  QLITE_TRANSACTIO
23317 4e 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  N.**       SQLIT
23318 45 5f 55 50 44 41 54 45 0a 2a 2a 0a 2a 2a 20 54  E_UPDATE.**.** T
23319 68 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75  he third and fou
2331a 72 74 68 20 61 72 67 75 6d 65 6e 74 73 20 74 6f  rth arguments to
2331b 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69   the auth functi
2331c 6f 6e 20 61 72 65 20 74 68 65 20 6e 61 6d 65 20  on are the name 
2331d 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20  of.** the table 
2331e 61 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74  and the column t
2331f 68 61 74 20 61 72 65 20 62 65 69 6e 67 20 61 63  hat are being ac
23320 63 65 73 73 65 64 2e 20 20 54 68 65 20 61 75 74  cessed.  The aut
23321 68 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 68  h function.** sh
23322 6f 75 6c 64 20 72 65 74 75 72 6e 20 65 69 74 68  ould return eith
23323 65 72 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 53 51  er SQLITE_OK, SQ
23324 4c 49 54 45 5f 44 45 4e 59 2c 20 6f 72 20 53 51  LITE_DENY, or SQ
23325 4c 49 54 45 5f 49 47 4e 4f 52 45 2e 20 20 49 66  LITE_IGNORE.  If
23326 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  .** SQLITE_OK is
23327 20 72 65 74 75 72 6e 65 64 2c 20 69 74 20 6d 65   returned, it me
23328 61 6e 73 20 74 68 61 74 20 61 63 63 65 73 73 20  ans that access 
23329 69 73 20 61 6c 6c 6f 77 65 64 2e 20 20 53 51 4c  is allowed.  SQL
2332a 49 54 45 5f 44 45 4e 59 0a 2a 2a 20 6d 65 61 6e  ITE_DENY.** mean
2332b 73 20 74 68 61 74 20 74 68 65 20 53 51 4c 20 73  s that the SQL s
2332c 74 61 74 65 6d 65 6e 74 20 77 69 6c 6c 20 6e 65  tatement will ne
2332d 76 65 72 2d 72 75 6e 20 2d 20 74 68 65 20 73 71  ver-run - the sq
2332e 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c  lite3_exec() cal
2332f 6c 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75 72 6e  l.** will return
23330 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 2e 20   with an error. 
23331 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 6d   SQLITE_IGNORE m
23332 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 53 51  eans that the SQ
23333 4c 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 73  L statement.** s
23334 68 6f 75 6c 64 20 72 75 6e 20 62 75 74 20 61 74  hould run but at
23335 74 65 6d 70 74 73 20 74 6f 20 72 65 61 64 20 74  tempts to read t
23336 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c  he specified col
23337 75 6d 6e 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  umn will return 
23338 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 20 61 74 74 65  NULL.** and atte
23339 6d 70 74 73 20 74 6f 20 77 72 69 74 65 20 74 68  mpts to write th
2333a 65 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 62 65  e column will be
2333b 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20   ignored..**.** 
2333c 53 65 74 74 69 6e 67 20 74 68 65 20 61 75 74 68  Setting the auth
2333d 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 4e 55 4c   function to NUL
2333e 4c 20 64 69 73 61 62 6c 65 73 20 74 68 69 73 20  L disables this 
2333f 68 6f 6f 6b 2e 20 20 54 68 65 20 64 65 66 61 75  hook.  The defau
23340 6c 74 0a 2a 2a 20 73 65 74 74 69 6e 67 20 6f 66  lt.** setting of
23341 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69   the auth functi
23342 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53  on is NULL..*/.S
23343 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
23344 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72  lite3_set_author
23345 69 7a 65 72 28 0a 20 20 73 71 6c 69 74 65 33 20  izer(.  sqlite3 
23346 2a 64 62 2c 0a 20 20 69 6e 74 20 28 2a 78 41 75  *db,.  int (*xAu
23347 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  th)(void*,int,co
23348 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
23349 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
2334a 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c 0a  *,const char*),.
2334b 20 20 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a    void *pArg.){.
2334c 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2334d 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
2334e 3b 0a 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20  ;.  db->xAuth = 
2334f 78 41 75 74 68 3b 0a 20 20 64 62 2d 3e 70 41 75  xAuth;.  db->pAu
23350 74 68 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20  thArg = pArg;.  
23351 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65  sqlite3ExpirePre
23352 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28  paredStatements(
23353 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  db);.  sqlite3_m
23354 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
23355 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
23356 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
23357 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 72 72  .** Write an err
23358 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 74 6f 20  or message into 
23359 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20  pParse->zErrMsg 
2335a 74 68 61 74 20 65 78 70 6c 61 69 6e 73 20 74 68  that explains th
2335b 61 74 20 74 68 65 0a 2a 2a 20 75 73 65 72 2d 73  at the.** user-s
2335c 75 70 70 6c 69 65 64 20 61 75 74 68 6f 72 69 7a  upplied authoriz
2335d 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 72  ation function r
2335e 65 74 75 72 6e 65 64 20 61 6e 20 69 6c 6c 65 67  eturned an illeg
2335f 61 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61  al value..*/.sta
23360 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 41  tic void sqliteA
23361 75 74 68 42 61 64 52 65 74 75 72 6e 43 6f 64 65  uthBadReturnCode
23362 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b  (Parse *pParse){
23363 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d  .  sqlite3ErrorM
23364 73 67 28 70 50 61 72 73 65 2c 20 22 61 75 74 68  sg(pParse, "auth
23365 6f 72 69 7a 65 72 20 6d 61 6c 66 75 6e 63 74 69  orizer malfuncti
23366 6f 6e 22 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  on");.  pParse->
23367 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
23368 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f  R;.}../*.** Invo
23369 6b 65 20 74 68 65 20 61 75 74 68 6f 72 69 7a 61  ke the authoriza
2336a 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 66 6f  tion callback fo
2336b 72 20 70 65 72 6d 69 73 73 69 6f 6e 20 74 6f 20  r permission to 
2336c 72 65 61 64 20 63 6f 6c 75 6d 6e 20 7a 43 6f 6c  read column zCol
2336d 20 66 72 6f 6d 0a 2a 2a 20 74 61 62 6c 65 20 7a   from.** table z
2336e 54 61 62 20 69 6e 20 64 61 74 61 62 61 73 65 20  Tab in database 
2336f 7a 44 62 2e 20 54 68 69 73 20 66 75 6e 63 74 69  zDb. This functi
23370 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  on assumes that 
23371 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  an authorization
23372 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 68 61 73  .** callback has
23373 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65 64   been registered
23374 20 28 69 2e 65 2e 20 74 68 61 74 20 73 71 6c 69   (i.e. that sqli
23375 74 65 33 2e 78 41 75 74 68 20 69 73 20 6e 6f 74  te3.xAuth is not
23376 20 4e 55 4c 4c 29 2e 0a 2a 2a 0a 2a 2a 20 49 66   NULL)..**.** If
23377 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 69   SQLITE_IGNORE i
23378 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 70  s returned and p
23379 45 78 70 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  Expr is not NULL
2337a 2c 20 74 68 65 6e 20 70 45 78 70 72 20 69 73 20  , then pExpr is 
2337b 63 68 61 6e 67 65 64 0a 2a 2a 20 74 6f 20 61 6e  changed.** to an
2337c 20 53 51 4c 20 4e 55 4c 4c 20 65 78 70 72 65 73   SQL NULL expres
2337d 73 69 6f 6e 2e 20 4f 74 68 65 72 77 69 73 65 2c  sion. Otherwise,
2337e 20 69 66 20 70 45 78 70 72 20 69 73 20 4e 55 4c   if pExpr is NUL
2337f 4c 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 49  L, then SQLITE_I
23380 47 4e 4f 52 45 0a 2a 2a 20 69 73 20 74 72 65 61  GNORE.** is trea
23381 74 65 64 20 61 73 20 53 51 4c 49 54 45 5f 44 45  ted as SQLITE_DE
23382 4e 59 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  NY. In this case
23383 20 61 6e 20 65 72 72 6f 72 20 69 73 20 6c 65 66   an error is lef
23384 74 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2f 0a  t in pParse..*/.
23385 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
23386 6e 74 20 73 71 6c 69 74 65 33 41 75 74 68 52 65  nt sqlite3AuthRe
23387 61 64 43 6f 6c 28 0a 20 20 50 61 72 73 65 20 2a  adCol(.  Parse *
23388 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
23389 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2338a 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  parser context *
2338b 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
2338c 7a 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 20  zTab,           
2338d 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d      /* Table nam
2338e 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
2338f 72 20 2a 7a 43 6f 6c 2c 20 20 20 20 20 20 20 20  r *zCol,        
23390 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e         /* Column
23391 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 69   name */.  int i
23392 44 62 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Db              
23393 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
23394 64 65 78 20 6f 66 20 63 6f 6e 74 61 69 6e 69 6e  dex of containin
23395 67 20 64 61 74 61 62 61 73 65 2e 20 2a 2f 0a 29  g database. */.)
23396 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
23397 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
23398 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
23399 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 68 61 72  handle */.  char
2339a 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b   *zDb = db->aDb[
2339b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 20 2f 2a 20 4e  iDb].zName; /* N
2339c 61 6d 65 20 6f 66 20 61 74 74 61 63 68 65 64 20  ame of attached 
2339d 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e  database */.  in
2339e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
2339f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
233a0 20 41 75 74 68 20 63 61 6c 6c 62 61 63 6b 20 72   Auth callback r
233a1 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20  eturn code */.. 
233a2 20 72 63 20 3d 20 64 62 2d 3e 78 41 75 74 68 28   rc = db->xAuth(
233a3 64 62 2d 3e 70 41 75 74 68 41 72 67 2c 20 53 51  db->pAuthArg, SQ
233a4 4c 49 54 45 5f 52 45 41 44 2c 20 7a 54 61 62 2c  LITE_READ, zTab,
233a5 7a 43 6f 6c 2c 7a 44 62 2c 70 50 61 72 73 65 2d  zCol,zDb,pParse-
233a6 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 29 3b 0a  >zAuthContext);.
233a7 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
233a8 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 69 66 28  _DENY ){.    if(
233a9 20 64 62 2d 3e 6e 44 62 3e 32 20 7c 7c 20 69 44   db->nDb>2 || iD
233aa 62 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  b!=0 ){.      sq
233ab 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
233ac 61 72 73 65 2c 20 22 61 63 63 65 73 73 20 74 6f  arse, "access to
233ad 20 25 73 2e 25 73 2e 25 73 20 69 73 20 70 72 6f   %s.%s.%s is pro
233ae 68 69 62 69 74 65 64 22 2c 7a 44 62 2c 7a 54 61  hibited",zDb,zTa
233af 62 2c 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c  b,zCol);.    }el
233b0 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
233b1 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
233b2 2c 20 22 61 63 63 65 73 73 20 74 6f 20 25 73 2e  , "access to %s.
233b3 25 73 20 69 73 20 70 72 6f 68 69 62 69 74 65 64  %s is prohibited
233b4 22 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a  ", zTab, zCol);.
233b5 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65      }.    pParse
233b6 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 55  ->rc = SQLITE_AU
233b7 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  TH;.  }else if( 
233b8 72 63 21 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52  rc!=SQLITE_IGNOR
233b9 45 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f  E && rc!=SQLITE_
233ba 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
233bb 41 75 74 68 42 61 64 52 65 74 75 72 6e 43 6f 64  AuthBadReturnCod
233bc 65 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 20  e(pParse);.  }. 
233bd 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
233be 2a 0a 2a 2a 20 54 68 65 20 70 45 78 70 72 20 73  *.** The pExpr s
233bf 68 6f 75 6c 64 20 62 65 20 61 20 54 4b 5f 43 4f  hould be a TK_CO
233c0 4c 55 4d 4e 20 65 78 70 72 65 73 73 69 6f 6e 2e  LUMN expression.
233c1 20 20 54 68 65 20 74 61 62 6c 65 20 72 65 66 65    The table refe
233c2 72 72 65 64 20 74 6f 0a 2a 2a 20 69 73 20 69 6e  rred to.** is in
233c3 20 70 54 61 62 4c 69 73 74 20 6f 72 20 65 6c 73   pTabList or els
233c4 65 20 69 74 20 69 73 20 74 68 65 20 4e 45 57 20  e it is the NEW 
233c5 6f 72 20 4f 4c 44 20 74 61 62 6c 65 20 6f 66 20  or OLD table of 
233c6 61 20 74 72 69 67 67 65 72 2e 20 20 0a 2a 2a 20  a trigger.  .** 
233c7 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
233c8 69 74 20 69 73 20 4f 4b 20 74 6f 20 72 65 61 64  it is OK to read
233c9 20 74 68 69 73 20 70 61 72 74 69 63 75 6c 61 72   this particular
233ca 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49   column..**.** I
233cb 66 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 74  f the auth funct
233cc 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ion returns SQLI
233cd 54 45 5f 49 47 4e 4f 52 45 2c 20 63 68 61 6e 67  TE_IGNORE, chang
233ce 65 20 74 68 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20  e the TK_COLUMN 
233cf 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  .** instruction 
233d0 69 6e 74 6f 20 61 20 54 4b 5f 4e 55 4c 4c 2e 20  into a TK_NULL. 
233d1 20 49 66 20 74 68 65 20 61 75 74 68 20 66 75 6e   If the auth fun
233d2 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53 51  ction returns SQ
233d3 4c 49 54 45 5f 44 45 4e 59 2c 0a 2a 2a 20 74 68  LITE_DENY,.** th
233d4 65 6e 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65  en generate an e
233d5 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rror..*/.SQLITE_
233d6 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
233d7 69 74 65 33 41 75 74 68 52 65 61 64 28 0a 20 20  ite3AuthRead(.  
233d8 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
233d9 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
233da 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
233db 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20   Expr *pExpr,   
233dc 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78         /* The ex
233dd 70 72 65 73 73 69 6f 6e 20 74 6f 20 63 68 65 63  pression to chec
233de 6b 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  k authorization 
233df 6f 6e 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a  on */.  Schema *
233e0 70 53 63 68 65 6d 61 2c 20 20 20 20 20 20 2f 2a  pSchema,      /*
233e1 20 54 68 65 20 73 63 68 65 6d 61 20 6f 66 20 74   The schema of t
233e2 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  he expression */
233e3 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62  .  SrcList *pTab
233e4 4c 69 73 74 20 20 20 20 20 2f 2a 20 41 6c 6c 20  List     /* All 
233e5 74 61 62 6c 65 20 74 68 61 74 20 70 45 78 70 72  table that pExpr
233e6 20 6d 69 67 68 74 20 72 65 66 65 72 20 74 6f 20   might refer to 
233e7 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  */.){.  sqlite3 
233e8 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
233e9 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20  ;.  Table *pTab 
233ea 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 54 68 65  = 0;      /* The
233eb 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72 65 61   table being rea
233ec 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  d */.  const cha
233ed 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 2f 2a 20  r *zCol;     /* 
233ee 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75  Name of the colu
233ef 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  mn of the table 
233f0 2a 2f 0a 20 20 69 6e 74 20 69 53 72 63 3b 20 20  */.  int iSrc;  
233f1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
233f2 64 65 78 20 69 6e 20 70 54 61 62 4c 69 73 74 2d  dex in pTabList-
233f3 3e 61 5b 5d 20 6f 66 20 74 61 62 6c 65 20 62 65  >a[] of table be
233f4 69 6e 67 20 72 65 61 64 20 2a 2f 0a 20 20 69 6e  ing read */.  in
233f5 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
233f6 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78      /* The index
233f7 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
233f8 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
233f9 72 65 66 65 72 73 20 74 6f 20 2a 2f 0a 20 20 69  refers to */.  i
233fa 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20  nt iCol;        
233fb 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
233fc 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65   column in table
233fd 20 2a 2f 0a 0a 20 20 69 66 28 20 64 62 2d 3e 78   */..  if( db->x
233fe 41 75 74 68 3d 3d 30 20 29 20 72 65 74 75 72 6e  Auth==0 ) return
233ff 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ;.  iDb = sqlite
23400 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
23401 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 63 68 65  Parse->db, pSche
23402 6d 61 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30  ma);.  if( iDb<0
23403 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 61 74   ){.    /* An at
23404 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 61 20  tempt to read a 
23405 63 6f 6c 75 6d 6e 20 6f 75 74 20 6f 66 20 61 20  column out of a 
23406 73 75 62 71 75 65 72 79 20 6f 72 20 6f 74 68 65  subquery or othe
23407 72 0a 20 20 20 20 2a 2a 20 74 65 6d 70 6f 72 61  r.    ** tempora
23408 72 79 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20  ry table. */.   
23409 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
2340a 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f  assert( pExpr->o
2340b 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20  p==TK_COLUMN || 
2340c 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 54 52  pExpr->op==TK_TR
2340d 49 47 47 45 52 20 29 3b 0a 20 20 69 66 28 20 70  IGGER );.  if( p
2340e 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 54 52 49  Expr->op==TK_TRI
2340f 47 47 45 52 20 29 7b 0a 20 20 20 20 70 54 61 62  GGER ){.    pTab
23410 20 3d 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67   = pParse->pTrig
23411 67 65 72 54 61 62 3b 0a 20 20 7d 65 6c 73 65 7b  gerTab;.  }else{
23412 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61  .    assert( pTa
23413 62 4c 69 73 74 20 29 3b 0a 20 20 20 20 66 6f 72  bList );.    for
23414 28 69 53 72 63 3d 30 3b 20 41 4c 57 41 59 53 28  (iSrc=0; ALWAYS(
23415 69 53 72 63 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  iSrc<pTabList->n
23416 53 72 63 29 3b 20 69 53 72 63 2b 2b 29 7b 0a 20  Src); iSrc++){. 
23417 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e       if( pExpr->
23418 69 54 61 62 6c 65 3d 3d 70 54 61 62 4c 69 73 74  iTable==pTabList
23419 2d 3e 61 5b 69 53 72 63 5d 2e 69 43 75 72 73 6f  ->a[iSrc].iCurso
2341a 72 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61  r ){.        pTa
2341b 62 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b  b = pTabList->a[
2341c 69 53 72 63 5d 2e 70 54 61 62 3b 0a 20 20 20 20  iSrc].pTab;.    
2341d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2341e 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69   }.    }.  }.  i
2341f 43 6f 6c 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f  Col = pExpr->iCo
23420 6c 75 6d 6e 3b 0a 20 20 69 66 28 20 4e 45 56 45  lumn;.  if( NEVE
23421 52 28 70 54 61 62 3d 3d 30 29 20 29 20 72 65 74  R(pTab==0) ) ret
23422 75 72 6e 3b 0a 0a 20 20 69 66 28 20 69 43 6f 6c  urn;..  if( iCol
23423 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  >=0 ){.    asser
23424 74 28 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43  t( iCol<pTab->nC
23425 6f 6c 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 20 3d  ol );.    zCol =
23426 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c   pTab->aCol[iCol
23427 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 65  ].zName;.  }else
23428 20 69 66 28 20 70 54 61 62 2d 3e 69 50 4b 65 79   if( pTab->iPKey
23429 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  >=0 ){.    asser
2342a 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c 70  t( pTab->iPKey<p
2342b 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20  Tab->nCol );.   
2342c 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43   zCol = pTab->aC
2342d 6f 6c 5b 70 54 61 62 2d 3e 69 50 4b 65 79 5d 2e  ol[pTab->iPKey].
2342e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  zName;.  }else{.
2342f 20 20 20 20 7a 43 6f 6c 20 3d 20 22 52 4f 57 49      zCol = "ROWI
23430 44 22 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  D";.  }.  assert
23431 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c  ( iDb>=0 && iDb<
23432 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 69 66 28  db->nDb );.  if(
23433 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 3d 3d   SQLITE_IGNORE==
23434 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 43  sqlite3AuthReadC
23435 6f 6c 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d  ol(pParse, pTab-
23436 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 2c 20 69 44  >zName, zCol, iD
23437 62 29 20 29 7b 0a 20 20 20 20 70 45 78 70 72 2d  b) ){.    pExpr-
23438 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20  >op = TK_NULL;. 
23439 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61   }.}../*.** Do a
2343a 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  n authorization 
2343b 63 68 65 63 6b 20 75 73 69 6e 67 20 74 68 65 20  check using the 
2343c 63 6f 64 65 20 61 6e 64 20 61 72 67 75 6d 65 6e  code and argumen
2343d 74 73 20 67 69 76 65 6e 2e 20 20 52 65 74 75 72  ts given.  Retur
2343e 6e 0a 2a 2a 20 65 69 74 68 65 72 20 53 51 4c 49  n.** either SQLI
2343f 54 45 5f 4f 4b 20 28 7a 65 72 6f 29 20 6f 72 20  TE_OK (zero) or 
23440 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 6f 72  SQLITE_IGNORE or
23441 20 53 51 4c 49 54 45 5f 44 45 4e 59 2e 20 20 49   SQLITE_DENY.  I
23442 66 20 53 51 4c 49 54 45 5f 44 45 4e 59 0a 2a 2a  f SQLITE_DENY.**
23443 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68   is returned, th
23444 65 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 75  en the error cou
23445 6e 74 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73  nt and error mes
23446 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 20 61  sage in pParse a
23447 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 20 61  re.** modified a
23448 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f  ppropriately..*/
23449 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2344a 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74 68 43  int sqlite3AuthC
2344b 68 65 63 6b 28 0a 20 20 50 61 72 73 65 20 2a 70  heck(.  Parse *p
2344c 50 61 72 73 65 2c 0a 20 20 69 6e 74 20 63 6f 64  Parse,.  int cod
2344d 65 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  e,.  const char 
2344e 2a 7a 41 72 67 31 2c 0a 20 20 63 6f 6e 73 74 20  *zArg1,.  const 
2344f 63 68 61 72 20 2a 7a 41 72 67 32 2c 0a 20 20 63  char *zArg2,.  c
23450 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67 33  onst char *zArg3
23451 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
23452 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
23453 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20    int rc;..  /* 
23454 44 6f 6e 27 74 20 64 6f 20 61 6e 79 20 61 75 74  Don't do any aut
23455 68 6f 72 69 7a 61 74 69 6f 6e 20 63 68 65 63 6b  horization check
23456 73 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73  s if the databas
23457 65 20 69 73 20 69 6e 69 74 69 61 6c 69 73 69 6e  e is initialisin
23458 67 0a 20 20 2a 2a 20 6f 72 20 69 66 20 74 68 65  g.  ** or if the
23459 20 70 61 72 73 65 72 20 69 73 20 62 65 69 6e 67   parser is being
2345a 20 69 6e 76 6f 6b 65 64 20 66 72 6f 6d 20 77 69   invoked from wi
2345b 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 64 65 63  thin sqlite3_dec
2345c 6c 61 72 65 5f 76 74 61 62 2e 0a 20 20 2a 2f 0a  lare_vtab..  */.
2345d 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62    if( db->init.b
2345e 75 73 79 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52  usy || IN_DECLAR
2345f 45 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 72 65  E_VTAB ){.    re
23460 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
23461 20 20 7d 0a 0a 20 20 69 66 28 20 64 62 2d 3e 78    }..  if( db->x
23462 41 75 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20 72  Auth==0 ){.    r
23463 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
23464 0a 20 20 7d 0a 20 20 72 63 20 3d 20 64 62 2d 3e  .  }.  rc = db->
23465 78 41 75 74 68 28 64 62 2d 3e 70 41 75 74 68 41  xAuth(db->pAuthA
23466 72 67 2c 20 63 6f 64 65 2c 20 7a 41 72 67 31 2c  rg, code, zArg1,
23467 20 7a 41 72 67 32 2c 20 7a 41 72 67 33 2c 20 70   zArg2, zArg3, p
23468 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74  Parse->zAuthCont
23469 65 78 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  ext);.  if( rc==
2346a 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20  SQLITE_DENY ){. 
2346b 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2346c 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 74 20  sg(pParse, "not 
2346d 61 75 74 68 6f 72 69 7a 65 64 22 29 3b 0a 20 20  authorized");.  
2346e 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
2346f 51 4c 49 54 45 5f 41 55 54 48 3b 0a 20 20 7d 65  QLITE_AUTH;.  }e
23470 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49  lse if( rc!=SQLI
23471 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c  TE_OK && rc!=SQL
23472 49 54 45 5f 49 47 4e 4f 52 45 20 29 7b 0a 20 20  ITE_IGNORE ){.  
23473 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 45    rc = SQLITE_DE
23474 4e 59 3b 0a 20 20 20 20 73 71 6c 69 74 65 41 75  NY;.    sqliteAu
23475 74 68 42 61 64 52 65 74 75 72 6e 43 6f 64 65 28  thBadReturnCode(
23476 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 20 20 72  pParse);.  }.  r
23477 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
23478 2a 2a 20 50 75 73 68 20 61 6e 20 61 75 74 68 6f  ** Push an autho
23479 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74  rization context
2347a 2e 20 20 41 66 74 65 72 20 74 68 69 73 20 72 6f  .  After this ro
2347b 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
2347c 20 74 68 65 0a 2a 2a 20 7a 41 72 67 33 20 61 72   the.** zArg3 ar
2347d 67 75 6d 65 6e 74 20 74 6f 20 61 75 74 68 6f 72  gument to author
2347e 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b  ization callback
2347f 73 20 77 69 6c 6c 20 62 65 20 7a 43 6f 6e 74 65  s will be zConte
23480 78 74 20 75 6e 74 69 6c 0a 2a 2a 20 70 6f 70 70  xt until.** popp
23481 65 64 2e 20 20 4f 72 20 69 66 20 70 50 61 72 73  ed.  Or if pPars
23482 65 3d 3d 30 2c 20 74 68 69 73 20 72 6f 75 74 69  e==0, this routi
23483 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ne is a no-op..*
23484 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
23485 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74   void sqlite3Aut
23486 68 43 6f 6e 74 65 78 74 50 75 73 68 28 0a 20 20  hContextPush(.  
23487 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a 20  Parse *pParse,. 
23488 20 41 75 74 68 43 6f 6e 74 65 78 74 20 2a 70 43   AuthContext *pC
23489 6f 6e 74 65 78 74 2c 20 0a 20 20 63 6f 6e 73 74  ontext, .  const
2348a 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 0a   char *zContext.
2348b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ){.  assert( pPa
2348c 72 73 65 20 29 3b 0a 20 20 70 43 6f 6e 74 65 78  rse );.  pContex
2348d 74 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72  t->pParse = pPar
2348e 73 65 3b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e  se;.  pContext->
2348f 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70  zAuthContext = p
23490 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74  Parse->zAuthCont
23491 65 78 74 3b 0a 20 20 70 50 61 72 73 65 2d 3e 7a  ext;.  pParse->z
23492 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a 43  AuthContext = zC
23493 6f 6e 74 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ontext;.}../*.**
23494 20 50 6f 70 20 61 6e 20 61 75 74 68 6f 72 69 7a   Pop an authoriz
23495 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 74 68  ation context th
23496 61 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c  at was previousl
23497 79 20 70 75 73 68 65 64 0a 2a 2a 20 62 79 20 73  y pushed.** by s
23498 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78  qlite3AuthContex
23499 74 50 75 73 68 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tPush.*/.SQLITE_
2349a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2349b 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
2349c 6f 70 28 41 75 74 68 43 6f 6e 74 65 78 74 20 2a  op(AuthContext *
2349d 70 43 6f 6e 74 65 78 74 29 7b 0a 20 20 69 66 28  pContext){.  if(
2349e 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73   pContext->pPars
2349f 65 20 29 7b 0a 20 20 20 20 70 43 6f 6e 74 65 78  e ){.    pContex
234a0 74 2d 3e 70 50 61 72 73 65 2d 3e 7a 41 75 74 68  t->pParse->zAuth
234a1 43 6f 6e 74 65 78 74 20 3d 20 70 43 6f 6e 74 65  Context = pConte
234a2 78 74 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74  xt->zAuthContext
234a3 3b 0a 20 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e  ;.    pContext->
234a4 70 50 61 72 73 65 20 3d 20 30 3b 0a 20 20 7d 0a  pParse = 0;.  }.
234a5 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
234a6 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
234a7 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  ZATION */../****
234a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
234a9 66 20 61 75 74 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f auth.c *******
234aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
234ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
234ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
234ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
234ae 20 66 69 6c 65 20 62 75 69 6c 64 2e 63 20 2a 2a   file build.c **
234af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
234b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
234b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
234b2 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
234b3 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
234b4 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
234b5 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
234b6 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
234b7 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
234b8 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
234b9 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
234ba 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
234bb 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
234bc 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
234bd 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
234be 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
234bf 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
234c0 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
234c1 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
234c2 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
234c3 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
234c4 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
234c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
234c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
234c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
234c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
234c9 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
234ca 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72  ontains C code r
234cb 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 72 65  outines that are
234cc 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 53   called by the S
234cd 51 4c 69 74 65 20 70 61 72 73 65 72 0a 2a 2a 20  QLite parser.** 
234ce 77 68 65 6e 20 73 79 6e 74 61 78 20 72 75 6c 65  when syntax rule
234cf 73 20 61 72 65 20 72 65 64 75 63 65 64 2e 20 20  s are reduced.  
234d0 54 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20  The routines in 
234d1 74 68 69 73 20 66 69 6c 65 20 68 61 6e 64 6c 65  this file handle
234d2 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e   the.** followin
234d3 67 20 6b 69 6e 64 73 20 6f 66 20 53 51 4c 20 73  g kinds of SQL s
234d4 79 6e 74 61 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  yntax:.**.**    
234d5 20 43 52 45 41 54 45 20 54 41 42 4c 45 0a 2a 2a   CREATE TABLE.**
234d6 20 20 20 20 20 44 52 4f 50 20 54 41 42 4c 45 0a       DROP TABLE.
234d7 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 49 4e  **     CREATE IN
234d8 44 45 58 0a 2a 2a 20 20 20 20 20 44 52 4f 50 20  DEX.**     DROP 
234d9 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 63 72 65  INDEX.**     cre
234da 61 74 69 6e 67 20 49 44 20 6c 69 73 74 73 0a 2a  ating ID lists.*
234db 2a 20 20 20 20 20 42 45 47 49 4e 20 54 52 41 4e  *     BEGIN TRAN
234dc 53 41 43 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 43  SACTION.**     C
234dd 4f 4d 4d 49 54 0a 2a 2a 20 20 20 20 20 52 4f 4c  OMMIT.**     ROL
234de 4c 42 41 43 4b 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  LBACK.**.** $Id:
234df 20 62 75 69 6c 64 2e 63 2c 76 20 31 2e 35 35 37   build.c,v 1.557
234e0 20 32 30 30 39 2f 30 37 2f 32 34 20 31 37 3a 35   2009/07/24 17:5
234e1 38 3a 35 33 20 64 61 6e 69 65 6c 6b 31 39 37 37  8:53 danielk1977
234e2 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
234e3 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
234e4 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 6e   called when a n
234e5 65 77 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ew SQL statement
234e6 20 69 73 20 62 65 67 69 6e 6e 69 6e 67 20 74 6f   is beginning to
234e7 0a 2a 2a 20 62 65 20 70 61 72 73 65 64 2e 20 20  .** be parsed.  
234e8 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 70  Initialize the p
234e9 50 61 72 73 65 20 73 74 72 75 63 74 75 72 65 20  Parse structure 
234ea 61 73 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 53 51  as needed..*/.SQ
234eb 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
234ec 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61  d sqlite3BeginPa
234ed 72 73 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  rse(Parse *pPars
234ee 65 2c 20 69 6e 74 20 65 78 70 6c 61 69 6e 46 6c  e, int explainFl
234ef 61 67 29 7b 0a 20 20 70 50 61 72 73 65 2d 3e 65  ag){.  pParse->e
234f0 78 70 6c 61 69 6e 20 3d 20 28 75 38 29 65 78 70  xplain = (u8)exp
234f1 6c 61 69 6e 46 6c 61 67 3b 0a 20 20 70 50 61 72  lainFlag;.  pPar
234f2 73 65 2d 3e 6e 56 61 72 20 3d 20 30 3b 0a 7d 0a  se->nVar = 0;.}.
234f3 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
234f4 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
234f5 45 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c  E./*.** The Tabl
234f6 65 4c 6f 63 6b 20 73 74 72 75 63 74 75 72 65 20  eLock structure 
234f7 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 20  is only used by 
234f8 74 68 65 20 73 71 6c 69 74 65 33 54 61 62 6c 65  the sqlite3Table
234f9 4c 6f 63 6b 28 29 20 61 6e 64 0a 2a 2a 20 63 6f  Lock() and.** co
234fa 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 20 66  deTableLocks() f
234fb 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 72  unctions..*/.str
234fc 75 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20 7b 0a  uct TableLock {.
234fd 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
234fe 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
234ff 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
23500 67 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62  g the table to b
23501 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 6e  e locked */.  in
23502 74 20 69 54 61 62 3b 20 20 20 20 20 20 20 20 20  t iTab;         
23503 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70     /* The root p
23504 61 67 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  age of the table
23505 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f   to be locked */
23506 0a 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63  .  u8 isWriteLoc
23507 6b 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  k;      /* True 
23508 66 6f 72 20 77 72 69 74 65 20 6c 6f 63 6b 2e 20  for write lock. 
23509 20 46 61 6c 73 65 20 66 6f 72 20 61 20 72 65 61   False for a rea
2350a 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e 73  d lock */.  cons
2350b 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20  t char *zName;  
2350c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
2350d 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  table */.};../*.
2350e 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20 66 61  ** Record the fa
2350f 63 74 20 74 68 61 74 20 77 65 20 77 61 6e 74 20  ct that we want 
23510 74 6f 20 6c 6f 63 6b 20 61 20 74 61 62 6c 65 20  to lock a table 
23511 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 0a 2a  at run-time.  .*
23512 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 74  *.** The table t
23513 6f 20 62 65 20 6c 6f 63 6b 65 64 20 68 61 73 20  o be locked has 
23514 72 6f 6f 74 20 70 61 67 65 20 69 54 61 62 20 61  root page iTab a
23515 6e 64 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 64  nd is found in d
23516 61 74 61 62 61 73 65 20 69 44 62 2e 0a 2a 2a 20  atabase iDb..** 
23517 41 20 72 65 61 64 20 6f 72 20 61 20 77 72 69 74  A read or a writ
23518 65 20 6c 6f 63 6b 20 63 61 6e 20 62 65 20 74 61  e lock can be ta
23519 6b 65 6e 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  ken depending on
2351a 20 69 73 57 72 69 74 65 6c 6f 63 6b 2e 0a 2a 2a   isWritelock..**
2351b 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2351c 20 6a 75 73 74 20 72 65 63 6f 72 64 73 20 74 68   just records th
2351d 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65 20  e fact that the 
2351e 6c 6f 63 6b 20 69 73 20 64 65 73 69 72 65 64 2e  lock is desired.
2351f 20 20 54 68 65 0a 2a 2a 20 63 6f 64 65 20 74 6f    The.** code to
23520 20 6d 61 6b 65 20 74 68 65 20 6c 6f 63 6b 20 6f   make the lock o
23521 63 63 75 72 20 69 73 20 67 65 6e 65 72 61 74 65  ccur is generate
23522 64 20 62 79 20 61 20 6c 61 74 65 72 20 63 61 6c  d by a later cal
23523 6c 20 74 6f 0a 2a 2a 20 63 6f 64 65 54 61 62 6c  l to.** codeTabl
23524 65 4c 6f 63 6b 73 28 29 20 77 68 69 63 68 20 6f  eLocks() which o
23525 63 63 75 72 73 20 64 75 72 69 6e 67 20 73 71 6c  ccurs during sql
23526 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67  ite3FinishCoding
23527 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ()..*/.SQLITE_PR
23528 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
23529 65 33 54 61 62 6c 65 4c 6f 63 6b 28 0a 20 20 50  e3TableLock(.  P
2352a 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2352b 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
2352c 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44  text */.  int iD
2352d 62 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b,           /* 
2352e 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74  Index of the dat
2352f 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67  abase containing
23530 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 6c 6f   the table to lo
23531 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62  ck */.  int iTab
23532 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f  ,          /* Ro
23533 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  ot page number o
23534 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62  f the table to b
23535 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 38  e locked */.  u8
23536 20 69 73 57 72 69 74 65 4c 6f 63 6b 2c 20 20 20   isWriteLock,   
23537 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 77   /* True for a w
23538 72 69 74 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63  rite lock */.  c
23539 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2353a 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
2353b 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63   table to be loc
2353c 6b 65 64 20 2a 2f 0a 29 7b 0a 20 20 50 61 72 73  ked */.){.  Pars
2353d 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73  e *pToplevel = s
2353e 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65  qlite3ParseTople
2353f 76 65 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 69  vel(pParse);.  i
23540 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74  nt i;.  int nByt
23541 65 73 3b 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20  es;.  TableLock 
23542 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44  *p;.  assert( iD
23543 62 3e 3d 30 20 29 3b 0a 0a 20 20 66 6f 72 28 69  b>=0 );..  for(i
23544 3d 30 3b 20 69 3c 70 54 6f 70 6c 65 76 65 6c 2d  =0; i<pToplevel-
23545 3e 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 69 2b 2b  >nTableLock; i++
23546 29 7b 0a 20 20 20 20 70 20 3d 20 26 70 54 6f 70  ){.    p = &pTop
23547 6c 65 76 65 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63  level->aTableLoc
23548 6b 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 2d  k[i];.    if( p-
23549 3e 69 44 62 3d 3d 69 44 62 20 26 26 20 70 2d 3e  >iDb==iDb && p->
2354a 69 54 61 62 3d 3d 69 54 61 62 20 29 7b 0a 20 20  iTab==iTab ){.  
2354b 20 20 20 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f      p->isWriteLo
2354c 63 6b 20 3d 20 28 70 2d 3e 69 73 57 72 69 74 65  ck = (p->isWrite
2354d 4c 6f 63 6b 20 7c 7c 20 69 73 57 72 69 74 65 4c  Lock || isWriteL
2354e 6f 63 6b 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ock);.      retu
2354f 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  rn;.    }.  }.. 
23550 20 6e 42 79 74 65 73 20 3d 20 73 69 7a 65 6f 66   nBytes = sizeof
23551 28 54 61 62 6c 65 4c 6f 63 6b 29 20 2a 20 28 70  (TableLock) * (p
23552 54 6f 70 6c 65 76 65 6c 2d 3e 6e 54 61 62 6c 65  Toplevel->nTable
23553 4c 6f 63 6b 2b 31 29 3b 0a 20 20 70 54 6f 70 6c  Lock+1);.  pTopl
23554 65 76 65 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b  evel->aTableLock
23555 20 3d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   =.      sqlite3
23556 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28  DbReallocOrFree(
23557 70 54 6f 70 6c 65 76 65 6c 2d 3e 64 62 2c 20 70  pToplevel->db, p
23558 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 62 6c 65  Toplevel->aTable
23559 4c 6f 63 6b 2c 20 6e 42 79 74 65 73 29 3b 0a 20  Lock, nBytes);. 
2355a 20 69 66 28 20 70 54 6f 70 6c 65 76 65 6c 2d 3e   if( pToplevel->
2355b 61 54 61 62 6c 65 4c 6f 63 6b 20 29 7b 0a 20 20  aTableLock ){.  
2355c 20 20 70 20 3d 20 26 70 54 6f 70 6c 65 76 65 6c    p = &pToplevel
2355d 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b 70 54 6f  ->aTableLock[pTo
2355e 70 6c 65 76 65 6c 2d 3e 6e 54 61 62 6c 65 4c 6f  plevel->nTableLo
2355f 63 6b 2b 2b 5d 3b 0a 20 20 20 20 70 2d 3e 69 44  ck++];.    p->iD
23560 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 2d 3e  b = iDb;.    p->
23561 69 54 61 62 20 3d 20 69 54 61 62 3b 0a 20 20 20  iTab = iTab;.   
23562 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20   p->isWriteLock 
23563 3d 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20  = isWriteLock;. 
23564 20 20 20 70 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e     p->zName = zN
23565 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ame;.  }else{.  
23566 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 54 61    pToplevel->nTa
23567 62 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20  bleLock = 0;.   
23568 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 64 62 2d 3e   pToplevel->db->
23569 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
2356a 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ;.  }.}../*.** C
2356b 6f 64 65 20 61 6e 20 4f 50 5f 54 61 62 6c 65 4c  ode an OP_TableL
2356c 6f 63 6b 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ock instruction 
2356d 66 6f 72 20 65 61 63 68 20 74 61 62 6c 65 20 6c  for each table l
2356e 6f 63 6b 65 64 20 62 79 20 74 68 65 0a 2a 2a 20  ocked by the.** 
2356f 73 74 61 74 65 6d 65 6e 74 20 28 63 6f 6e 66 69  statement (confi
23570 67 75 72 65 64 20 62 79 20 63 61 6c 6c 73 20 74  gured by calls t
23571 6f 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f  o sqlite3TableLo
23572 63 6b 28 29 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  ck())..*/.static
23573 20 76 6f 69 64 20 63 6f 64 65 54 61 62 6c 65 4c   void codeTableL
23574 6f 63 6b 73 28 50 61 72 73 65 20 2a 70 50 61 72  ocks(Parse *pPar
23575 73 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  se){.  int i;.  
23576 56 64 62 65 20 2a 70 56 64 62 65 3b 20 0a 0a 20  Vdbe *pVdbe; .. 
23577 20 70 56 64 62 65 20 3d 20 73 71 6c 69 74 65 33   pVdbe = sqlite3
23578 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
23579 0a 20 20 61 73 73 65 72 74 28 20 70 56 64 62 65  .  assert( pVdbe
2357a 21 3d 30 20 29 3b 20 2f 2a 20 73 71 6c 69 74 65  !=0 ); /* sqlite
2357b 33 47 65 74 56 64 62 65 20 63 61 6e 6e 6f 74 20  3GetVdbe cannot 
2357c 66 61 69 6c 3a 20 56 44 42 45 20 61 6c 72 65 61  fail: VDBE alrea
2357d 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a  dy allocated */.
2357e 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50  .  for(i=0; i<pP
2357f 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b  arse->nTableLock
23580 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c  ; i++){.    Tabl
23581 65 4c 6f 63 6b 20 2a 70 20 3d 20 26 70 50 61 72  eLock *p = &pPar
23582 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b 69  se->aTableLock[i
23583 5d 3b 0a 20 20 20 20 69 6e 74 20 70 31 20 3d 20  ];.    int p1 = 
23584 70 2d 3e 69 44 62 3b 0a 20 20 20 20 73 71 6c 69  p->iDb;.    sqli
23585 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 70 56  te3VdbeAddOp4(pV
23586 64 62 65 2c 20 4f 50 5f 54 61 62 6c 65 4c 6f 63  dbe, OP_TableLoc
23587 6b 2c 20 70 31 2c 20 70 2d 3e 69 54 61 62 2c 20  k, p1, p->iTab, 
23588 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 2c 0a  p->isWriteLock,.
23589 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2358a 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 2c 20        p->zName, 
2358b 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a  P4_STATIC);.  }.
2358c 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e  }.#else.  #defin
2358d 65 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73  e codeTableLocks
2358e 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  (x).#endif../*.*
2358f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
23590 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61  s called after a
23591 20 73 69 6e 67 6c 65 20 53 51 4c 20 73 74 61 74   single SQL stat
23592 65 6d 65 6e 74 20 68 61 73 20 62 65 65 6e 0a 2a  ement has been.*
23593 2a 20 70 61 72 73 65 64 20 61 6e 64 20 61 20 56  * parsed and a V
23594 44 42 45 20 70 72 6f 67 72 61 6d 20 74 6f 20 65  DBE program to e
23595 78 65 63 75 74 65 20 74 68 61 74 20 73 74 61 74  xecute that stat
23596 65 6d 65 6e 74 20 68 61 73 20 62 65 65 6e 0a 2a  ement has been.*
23597 2a 20 70 72 65 70 61 72 65 64 2e 20 20 54 68 69  * prepared.  Thi
23598 73 20 72 6f 75 74 69 6e 65 20 70 75 74 73 20 74  s routine puts t
23599 68 65 20 66 69 6e 69 73 68 69 6e 67 20 74 6f 75  he finishing tou
2359a 63 68 65 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 56  ches on the.** V
2359b 44 42 45 20 70 72 6f 67 72 61 6d 20 61 6e 64 20  DBE program and 
2359c 72 65 73 65 74 73 20 74 68 65 20 70 50 61 72 73  resets the pPars
2359d 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20  e structure for 
2359e 74 68 65 20 6e 65 78 74 0a 2a 2a 20 70 61 72 73  the next.** pars
2359f 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  e..**.** Note th
235a0 61 74 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f  at if an error o
235a1 63 63 75 72 72 65 64 2c 20 69 74 20 6d 69 67 68  ccurred, it migh
235a2 74 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68  t be the case th
235a3 61 74 0a 2a 2a 20 6e 6f 20 56 44 42 45 20 63 6f  at.** no VDBE co
235a4 64 65 20 77 61 73 20 67 65 6e 65 72 61 74 65 64  de was generated
235a5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
235a6 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
235a7 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 50 61 72  FinishCoding(Par
235a8 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73  se *pParse){.  s
235a9 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64  qlite3 *db;.  Vd
235aa 62 65 20 2a 76 3b 0a 0a 20 20 64 62 20 3d 20 70  be *v;..  db = p
235ab 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
235ac 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
235ad 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  d ) return;.  if
235ae 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64  ( pParse->nested
235af 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
235b0 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20   pParse->nErr ) 
235b1 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 42 65  return;..  /* Be
235b2 67 69 6e 20 62 79 20 67 65 6e 65 72 61 74 69 6e  gin by generatin
235b3 67 20 73 6f 6d 65 20 74 65 72 6d 69 6e 61 74 69  g some terminati
235b4 6f 6e 20 63 6f 64 65 20 61 74 20 74 68 65 20 65  on code at the e
235b5 6e 64 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 76  nd of the.  ** v
235b6 64 62 65 20 70 72 6f 67 72 61 6d 0a 20 20 2a 2f  dbe program.  */
235b7 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
235b8 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
235b9 20 61 73 73 65 72 74 28 20 21 70 50 61 72 73 65   assert( !pParse
235ba 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74 65 20 0a  ->isMultiWrite .
235bb 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65         || sqlite
235bc 33 56 64 62 65 41 73 73 65 72 74 4d 61 79 41 62  3VdbeAssertMayAb
235bd 6f 72 74 28 76 2c 20 70 50 61 72 73 65 2d 3e 6d  ort(v, pParse->m
235be 61 79 41 62 6f 72 74 29 29 3b 0a 20 20 69 66 28  ayAbort));.  if(
235bf 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65   v ){.    sqlite
235c0 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f  3VdbeAddOp0(v, O
235c1 50 5f 48 61 6c 74 29 3b 0a 0a 20 20 20 20 2f 2a  P_Halt);..    /*
235c2 20 54 68 65 20 63 6f 6f 6b 69 65 20 6d 61 73 6b   The cookie mask
235c3 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 62 69   contains one bi
235c4 74 20 66 6f 72 20 65 61 63 68 20 64 61 74 61 62  t for each datab
235c5 61 73 65 20 66 69 6c 65 20 6f 70 65 6e 2e 0a 20  ase file open.. 
235c6 20 20 20 2a 2a 20 28 42 69 74 20 30 20 69 73 20     ** (Bit 0 is 
235c7 66 6f 72 20 6d 61 69 6e 2c 20 62 69 74 20 31 20  for main, bit 1 
235c8 69 73 20 66 6f 72 20 74 65 6d 70 2c 20 61 6e 64  is for temp, and
235c9 20 73 6f 20 66 6f 72 74 68 2e 29 20 20 42 69 74   so forth.)  Bit
235ca 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 73 65 74  s are.    ** set
235cb 20 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61   for each databa
235cc 73 65 20 74 68 61 74 20 69 73 20 75 73 65 64 2e  se that is used.
235cd 20 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20    Generate code 
235ce 74 6f 20 73 74 61 72 74 20 61 0a 20 20 20 20 2a  to start a.    *
235cf 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e  * transaction on
235d0 20 65 61 63 68 20 75 73 65 64 20 64 61 74 61 62   each used datab
235d1 61 73 65 20 61 6e 64 20 74 6f 20 76 65 72 69 66  ase and to verif
235d2 79 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f  y the schema coo
235d3 6b 69 65 0a 20 20 20 20 2a 2a 20 6f 6e 20 65 61  kie.    ** on ea
235d4 63 68 20 75 73 65 64 20 64 61 74 61 62 61 73 65  ch used database
235d5 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
235d6 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47   pParse->cookieG
235d7 6f 74 6f 3e 30 20 29 7b 0a 20 20 20 20 20 20 75  oto>0 ){.      u
235d8 33 32 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20 69  32 mask;.      i
235d9 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 20 73 71  nt iDb;.      sq
235da 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
235db 65 28 76 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f  e(v, pParse->coo
235dc 6b 69 65 47 6f 74 6f 2d 31 29 3b 0a 20 20 20 20  kieGoto-1);.    
235dd 20 20 66 6f 72 28 69 44 62 3d 30 2c 20 6d 61 73    for(iDb=0, mas
235de 6b 3d 31 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62  k=1; iDb<db->nDb
235df 3b 20 6d 61 73 6b 3c 3c 3d 31 2c 20 69 44 62 2b  ; mask<<=1, iDb+
235e0 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
235e1 28 6d 61 73 6b 20 26 20 70 50 61 72 73 65 2d 3e  (mask & pParse->
235e2 63 6f 6f 6b 69 65 4d 61 73 6b 29 3d 3d 30 20 29  cookieMask)==0 )
235e3 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
235e4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73     sqlite3VdbeUs
235e5 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b  esBtree(v, iDb);
235e6 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
235e7 56 64 62 65 41 64 64 4f 70 32 28 76 2c 4f 50 5f  VdbeAddOp2(v,OP_
235e8 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 44 62  Transaction, iDb
235e9 2c 20 28 6d 61 73 6b 20 26 20 70 50 61 72 73 65  , (mask & pParse
235ea 2d 3e 77 72 69 74 65 4d 61 73 6b 29 21 3d 30 29  ->writeMask)!=0)
235eb 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62  ;.        if( db
235ec 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29  ->init.busy==0 )
235ed 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
235ee 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
235ef 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 2c  OP_VerifyCookie,
235f0 20 69 44 62 2c 20 70 50 61 72 73 65 2d 3e 63 6f   iDb, pParse->co
235f1 6f 6b 69 65 56 61 6c 75 65 5b 69 44 62 5d 29 3b  okieValue[iDb]);
235f2 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
235f3 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
235f4 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
235f5 42 4c 45 0a 20 20 20 20 20 20 7b 0a 20 20 20 20  BLE.      {.    
235f6 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
235f7 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50     for(i=0; i<pP
235f8 61 72 73 65 2d 3e 6e 56 74 61 62 4c 6f 63 6b 3b  arse->nVtabLock;
235f9 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
235fa 20 63 68 61 72 20 2a 76 74 61 62 20 3d 20 28 63   char *vtab = (c
235fb 68 61 72 20 2a 29 73 71 6c 69 74 65 33 47 65 74  har *)sqlite3Get
235fc 56 54 61 62 6c 65 28 64 62 2c 20 70 50 61 72 73  VTable(db, pPars
235fd 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 5b 69 5d  e->apVtabLock[i]
235fe 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
235ff 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
23600 2c 20 4f 50 5f 56 42 65 67 69 6e 2c 20 30 2c 20  , OP_VBegin, 0, 
23601 30 2c 20 30 2c 20 76 74 61 62 2c 20 50 34 5f 56  0, 0, vtab, P4_V
23602 54 41 42 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  TAB);.        }.
23603 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
23604 6e 56 74 61 62 4c 6f 63 6b 20 3d 20 30 3b 0a 20  nVtabLock = 0;. 
23605 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20       }.#endif.. 
23606 20 20 20 20 20 2f 2a 20 4f 6e 63 65 20 61 6c 6c       /* Once all
23607 20 74 68 65 20 63 6f 6f 6b 69 65 73 20 68 61 76   the cookies hav
23608 65 20 62 65 65 6e 20 76 65 72 69 66 69 65 64 20  e been verified 
23609 61 6e 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73  and transactions
2360a 20 6f 70 65 6e 65 64 2c 20 0a 20 20 20 20 20 20   opened, .      
2360b 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65 20 72 65  ** obtain the re
2360c 71 75 69 72 65 64 20 74 61 62 6c 65 2d 6c 6f 63  quired table-loc
2360d 6b 73 2e 20 54 68 69 73 20 69 73 20 61 20 6e 6f  ks. This is a no
2360e 2d 6f 70 20 75 6e 6c 65 73 73 20 74 68 65 20 0a  -op unless the .
2360f 20 20 20 20 20 20 2a 2a 20 73 68 61 72 65 64 2d        ** shared-
23610 63 61 63 68 65 20 66 65 61 74 75 72 65 20 69 73  cache feature is
23611 20 65 6e 61 62 6c 65 64 2e 0a 20 20 20 20 20 20   enabled..      
23612 2a 2f 0a 20 20 20 20 20 20 63 6f 64 65 54 61 62  */.      codeTab
23613 6c 65 4c 6f 63 6b 73 28 70 50 61 72 73 65 29 3b  leLocks(pParse);
23614 0a 0a 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69  ..      /* Initi
23615 61 6c 69 7a 65 20 61 6e 79 20 41 55 54 4f 49 4e  alize any AUTOIN
23616 43 52 45 4d 45 4e 54 20 64 61 74 61 20 73 74 72  CREMENT data str
23617 75 63 74 75 72 65 73 20 72 65 71 75 69 72 65 64  uctures required
23618 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
23619 20 73 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 72   sqlite3Autoincr
2361a 65 6d 65 6e 74 42 65 67 69 6e 28 70 50 61 72 73  ementBegin(pPars
2361b 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69  e);..      /* Fi
2361c 6e 61 6c 6c 79 2c 20 6a 75 6d 70 20 62 61 63 6b  nally, jump back
2361d 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   to the beginnin
2361e 67 20 6f 66 20 74 68 65 20 65 78 65 63 75 74 61  g of the executa
2361f 62 6c 65 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 20  ble code. */.   
23620 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
23621 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
23622 20 30 2c 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b   0, pParse->cook
23623 69 65 47 6f 74 6f 29 3b 0a 20 20 20 20 7d 0a 20  ieGoto);.    }. 
23624 20 7d 0a 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68   }...  /* Get th
23625 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 72  e VDBE program r
23626 65 61 64 79 20 66 6f 72 20 65 78 65 63 75 74 69  eady for executi
23627 6f 6e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 76 20  on.  */.  if( v 
23628 26 26 20 41 4c 57 41 59 53 28 70 50 61 72 73 65  && ALWAYS(pParse
23629 2d 3e 6e 45 72 72 3d 3d 30 29 20 26 26 20 21 64  ->nErr==0) && !d
2362a 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2362b 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ){.#ifdef SQLITE
2362c 5f 44 45 42 55 47 0a 20 20 20 20 46 49 4c 45 20  _DEBUG.    FILE 
2362d 2a 74 72 61 63 65 20 3d 20 28 64 62 2d 3e 66 6c  *trace = (db->fl
2362e 61 67 73 20 26 20 53 51 4c 49 54 45 5f 56 64 62  ags & SQLITE_Vdb
2362f 65 54 72 61 63 65 29 21 3d 30 20 3f 20 73 74 64  eTrace)!=0 ? std
23630 6f 75 74 20 3a 20 30 3b 0a 20 20 20 20 73 71 6c  out : 0;.    sql
23631 69 74 65 33 56 64 62 65 54 72 61 63 65 28 76 2c  ite3VdbeTrace(v,
23632 20 74 72 61 63 65 29 3b 0a 23 65 6e 64 69 66 0a   trace);.#endif.
23633 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72      assert( pPar
23634 73 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3d  se->iCacheLevel=
23635 3d 30 20 29 3b 20 20 2f 2a 20 44 69 73 61 62 6c  =0 );  /* Disabl
23636 65 73 20 61 6e 64 20 72 65 2d 65 6e 61 62 6c 65  es and re-enable
23637 73 20 6d 61 74 63 68 20 2a 2f 0a 20 20 20 20 2f  s match */.    /
23638 2a 20 41 20 6d 69 6e 69 6d 75 6d 20 6f 66 20 6f  * A minimum of o
23639 6e 65 20 63 75 72 73 6f 72 20 69 73 20 72 65 71  ne cursor is req
2363a 75 69 72 65 64 20 69 66 20 61 75 74 6f 69 6e 63  uired if autoinc
2363b 72 65 6d 65 6e 74 20 69 73 20 75 73 65 64 0a 20  rement is used. 
2363c 20 20 20 2a 20 20 53 65 65 20 74 69 63 6b 65 74     *  See ticket
2363d 20 5b 61 36 39 36 33 37 39 63 31 66 30 38 38 36   [a696379c1f0886
2363e 36 5d 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50  6] */.    if( pP
2363f 61 72 73 65 2d 3e 70 41 69 6e 63 21 3d 30 20 26  arse->pAinc!=0 &
23640 26 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3d 3d  & pParse->nTab==
23641 30 20 29 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  0 ) pParse->nTab
23642 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 1;.    sqlite
23643 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79 28 76  3VdbeMakeReady(v
23644 2c 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 2c 20  , pParse->nVar, 
23645 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2c 0a 20 20  pParse->nMem,.  
23646 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23647 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e         pParse->n
23648 54 61 62 2c 20 70 50 61 72 73 65 2d 3e 6e 4d 61  Tab, pParse->nMa
23649 78 41 72 67 2c 20 70 50 61 72 73 65 2d 3e 65 78  xArg, pParse->ex
2364a 70 6c 61 69 6e 2c 0a 20 20 20 20 20 20 20 20 20  plain,.         
2364b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2364c 70 50 61 72 73 65 2d 3e 69 73 4d 75 6c 74 69 57  pParse->isMultiW
2364d 72 69 74 65 20 26 26 20 70 50 61 72 73 65 2d 3e  rite && pParse->
2364e 6d 61 79 41 62 6f 72 74 29 3b 0a 20 20 20 20 70  mayAbort);.    p
2364f 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49  Parse->rc = SQLI
23650 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 70 50 61  TE_DONE;.    pPa
23651 72 73 65 2d 3e 63 6f 6c 4e 61 6d 65 73 53 65 74  rse->colNamesSet
23652 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66   = 0;.  }else if
23653 28 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51  ( pParse->rc==SQ
23654 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70  LITE_OK ){.    p
23655 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49  Parse->rc = SQLI
23656 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
23657 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d 20 30  pParse->nTab = 0
23658 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  ;.  pParse->nMem
23659 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e   = 0;.  pParse->
2365a 6e 53 65 74 20 3d 20 30 3b 0a 20 20 70 50 61 72  nSet = 0;.  pPar
2365b 73 65 2d 3e 6e 56 61 72 20 3d 20 30 3b 0a 20 20  se->nVar = 0;.  
2365c 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61  pParse->cookieMa
2365d 73 6b 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65  sk = 0;.  pParse
2365e 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 20 3d 20 30  ->cookieGoto = 0
2365f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 75 6e 20 74  ;.}../*.** Run t
23660 68 65 20 70 61 72 73 65 72 20 61 6e 64 20 63 6f  he parser and co
23661 64 65 20 67 65 6e 65 72 61 74 6f 72 20 72 65 63  de generator rec
23662 75 72 73 69 76 65 6c 79 20 69 6e 20 6f 72 64 65  ursively in orde
23663 72 20 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a  r to generate.**
23664 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 53 51   code for the SQ
23665 4c 20 73 74 61 74 65 6d 65 6e 74 20 67 69 76 65  L statement give
23666 6e 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f  n onto the end o
23667 66 20 74 68 65 20 70 50 61 72 73 65 20 63 6f 6e  f the pParse con
23668 74 65 78 74 0a 2a 2a 20 63 75 72 72 65 6e 74 6c  text.** currentl
23669 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  y under construc
2366a 74 69 6f 6e 2e 20 20 57 68 65 6e 20 74 68 65 20  tion.  When the 
2366b 70 61 72 73 65 72 20 69 73 20 72 75 6e 20 72 65  parser is run re
2366c 63 75 72 73 69 76 65 6c 79 0a 2a 2a 20 74 68 69  cursively.** thi
2366d 73 20 77 61 79 2c 20 74 68 65 20 66 69 6e 61 6c  s way, the final
2366e 20 4f 50 5f 48 61 6c 74 20 69 73 20 6e 6f 74 20   OP_Halt is not 
2366f 61 70 70 65 6e 64 65 64 20 61 6e 64 20 6f 74 68  appended and oth
23670 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  er initializatio
23671 6e 0a 2a 2a 20 61 6e 64 20 66 69 6e 61 6c 69 7a  n.** and finaliz
23672 61 74 69 6f 6e 20 73 74 65 70 73 20 61 72 65 20  ation steps are 
23673 6f 6d 69 74 74 65 64 20 62 65 63 61 75 73 65 20  omitted because 
23674 74 68 6f 73 65 20 61 72 65 20 68 61 6e 64 6c 69  those are handli
23675 6e 67 20 62 79 20 74 68 65 0a 2a 2a 20 6f 75 74  ng by the.** out
23676 65 72 6d 6f 73 74 20 70 61 72 73 65 72 2e 0a 2a  ermost parser..*
23677 2a 0a 2a 2a 20 4e 6f 74 20 65 76 65 72 79 74 68  *.** Not everyth
23678 69 6e 67 20 69 73 20 6e 65 73 74 61 62 6c 65 2e  ing is nestable.
23679 20 20 54 68 69 73 20 66 61 63 69 6c 69 74 79 20    This facility 
2367a 69 73 20 64 65 73 69 67 6e 65 64 20 74 6f 20 70  is designed to p
2367b 65 72 6d 69 74 0a 2a 2a 20 49 4e 53 45 52 54 2c  ermit.** INSERT,
2367c 20 55 50 44 41 54 45 2c 20 61 6e 64 20 44 45 4c   UPDATE, and DEL
2367d 45 54 45 20 6f 70 65 72 61 74 69 6f 6e 73 20 61  ETE operations a
2367e 67 61 69 6e 73 74 20 53 51 4c 49 54 45 5f 4d 41  gainst SQLITE_MA
2367f 53 54 45 52 2e 20 20 55 73 65 0a 2a 2a 20 63 61  STER.  Use.** ca
23680 72 65 20 69 66 20 79 6f 75 20 64 65 63 69 64 65  re if you decide
23681 20 74 6f 20 74 72 79 20 74 6f 20 75 73 65 20 74   to try to use t
23682 68 69 73 20 72 6f 75 74 69 6e 65 20 66 6f 72 20  his routine for 
23683 73 6f 6d 65 20 6f 74 68 65 72 20 70 75 72 70 6f  some other purpo
23684 73 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ses..*/.SQLITE_P
23685 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
23686 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 50  te3NestedParse(P
23687 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f  arse *pParse, co
23688 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
23689 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69  t, ...){.  va_li
2368a 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a  st ap;.  char *z
2368b 53 71 6c 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72  Sql;.  char *zEr
2368c 72 4d 73 67 20 3d 20 30 3b 0a 20 20 73 71 6c 69  rMsg = 0;.  sqli
2368d 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2368e 2d 3e 64 62 3b 0a 23 20 64 65 66 69 6e 65 20 53  ->db;.# define S
2368f 41 56 45 5f 53 5a 20 20 28 73 69 7a 65 6f 66 28  AVE_SZ  (sizeof(
23690 50 61 72 73 65 29 20 2d 20 6f 66 66 73 65 74 6f  Parse) - offseto
23691 66 28 50 61 72 73 65 2c 6e 56 61 72 29 29 0a 20  f(Parse,nVar)). 
23692 20 63 68 61 72 20 73 61 76 65 42 75 66 5b 53 41   char saveBuf[SA
23693 56 45 5f 53 5a 5d 3b 0a 0a 20 20 69 66 28 20 70  VE_SZ];..  if( p
23694 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20 72 65  Parse->nErr ) re
23695 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  turn;.  assert( 
23696 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3c 31  pParse->nested<1
23697 30 20 29 3b 20 20 2f 2a 20 4e 65 73 74 69 6e 67  0 );  /* Nesting
23698 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20   should only be 
23699 6f 66 20 6c 69 6d 69 74 65 64 20 64 65 70 74 68  of limited depth
2369a 20 2a 2f 0a 20 20 76 61 5f 73 74 61 72 74 28 61   */.  va_start(a
2369b 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a  p, zFormat);.  z
2369c 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 56 4d 50  Sql = sqlite3VMP
2369d 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61  rintf(db, zForma
2369e 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64  t, ap);.  va_end
2369f 28 61 70 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c  (ap);.  if( zSql
236a0 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
236a1 6e 3b 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63  n;   /* A malloc
236a2 20 6d 75 73 74 20 68 61 76 65 20 66 61 69 6c 65   must have faile
236a3 64 20 2a 2f 0a 20 20 7d 0a 20 20 70 50 61 72 73  d */.  }.  pPars
236a4 65 2d 3e 6e 65 73 74 65 64 2b 2b 3b 0a 20 20 6d  e->nested++;.  m
236a5 65 6d 63 70 79 28 73 61 76 65 42 75 66 2c 20 26  emcpy(saveBuf, &
236a6 70 50 61 72 73 65 2d 3e 6e 56 61 72 2c 20 53 41  pParse->nVar, SA
236a7 56 45 5f 53 5a 29 3b 0a 20 20 6d 65 6d 73 65 74  VE_SZ);.  memset
236a8 28 26 70 50 61 72 73 65 2d 3e 6e 56 61 72 2c 20  (&pParse->nVar, 
236a9 30 2c 20 53 41 56 45 5f 53 5a 29 3b 0a 20 20 73  0, SAVE_SZ);.  s
236aa 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 28  qlite3RunParser(
236ab 70 50 61 72 73 65 2c 20 7a 53 71 6c 2c 20 26 7a  pParse, zSql, &z
236ac 45 72 72 4d 73 67 29 3b 0a 20 20 73 71 6c 69 74  ErrMsg);.  sqlit
236ad 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72  e3DbFree(db, zEr
236ae 72 4d 73 67 29 3b 0a 20 20 73 71 6c 69 74 65 33  rMsg);.  sqlite3
236af 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 29  DbFree(db, zSql)
236b0 3b 0a 20 20 6d 65 6d 63 70 79 28 26 70 50 61 72  ;.  memcpy(&pPar
236b1 73 65 2d 3e 6e 56 61 72 2c 20 73 61 76 65 42 75  se->nVar, saveBu
236b2 66 2c 20 53 41 56 45 5f 53 5a 29 3b 0a 20 20 70  f, SAVE_SZ);.  p
236b3 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 2d 2d 3b  Parse->nested--;
236b4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65  .}../*.** Locate
236b5 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73   the in-memory s
236b6 74 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65  tructure that de
236b7 73 63 72 69 62 65 73 20 61 20 70 61 72 74 69 63  scribes a partic
236b8 75 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a  ular database.**
236b9 20 74 61 62 6c 65 20 67 69 76 65 6e 20 74 68 65   table given the
236ba 20 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 74 61   name of that ta
236bb 62 6c 65 20 61 6e 64 20 28 6f 70 74 69 6f 6e 61  ble and (optiona
236bc 6c 6c 79 29 20 74 68 65 20 6e 61 6d 65 20 6f 66  lly) the name of
236bd 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
236be 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
236bf 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e  table.  Return N
236c0 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64  ULL if not found
236c1 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 44 61 74 61  ..**.** If zData
236c2 62 61 73 65 20 69 73 20 30 2c 20 61 6c 6c 20 64  base is 0, all d
236c3 61 74 61 62 61 73 65 73 20 61 72 65 20 73 65 61  atabases are sea
236c4 72 63 68 65 64 20 66 6f 72 20 74 68 65 20 74 61  rched for the ta
236c5 62 6c 65 20 61 6e 64 20 74 68 65 0a 2a 2a 20 66  ble and the.** f
236c6 69 72 73 74 20 6d 61 74 63 68 69 6e 67 20 74 61  irst matching ta
236c7 62 6c 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ble is returned.
236c8 20 20 28 4e 6f 20 63 68 65 63 6b 69 6e 67 20 66    (No checking f
236c9 6f 72 20 64 75 70 6c 69 63 61 74 65 20 74 61 62  or duplicate tab
236ca 6c 65 0a 2a 2a 20 6e 61 6d 65 73 20 69 73 20 64  le.** names is d
236cb 6f 6e 65 2e 29 20 20 54 68 65 20 73 65 61 72 63  one.)  The searc
236cc 68 20 6f 72 64 65 72 20 69 73 20 54 45 4d 50 20  h order is TEMP 
236cd 66 69 72 73 74 2c 20 74 68 65 6e 20 4d 41 49 4e  first, then MAIN
236ce 2c 20 74 68 65 6e 20 61 6e 79 0a 2a 2a 20 61 75  , then any.** au
236cf 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65  xiliary database
236d0 73 20 61 64 64 65 64 20 75 73 69 6e 67 20 74 68  s added using th
236d1 65 20 41 54 54 41 43 48 20 63 6f 6d 6d 61 6e 64  e ATTACH command
236d2 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
236d3 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61   sqlite3LocateTa
236d4 62 6c 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ble()..*/.SQLITE
236d5 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a  _PRIVATE Table *
236d6 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
236d7 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
236d8 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
236d9 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61   const char *zDa
236da 74 61 62 61 73 65 29 7b 0a 20 20 54 61 62 6c 65  tabase){.  Table
236db 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69   *p = 0;.  int i
236dc 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20  ;.  int nName;. 
236dd 20 61 73 73 65 72 74 28 20 7a 4e 61 6d 65 21 3d   assert( zName!=
236de 30 20 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73  0 );.  nName = s
236df 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
236e0 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 28 69 3d 4f  Name);.  for(i=O
236e1 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64 62  MIT_TEMPDB; i<db
236e2 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
236e3 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f   int j = (i<2) ?
236e4 20 69 5e 31 20 3a 20 69 3b 20 20 20 2f 2a 20 53   i^1 : i;   /* S
236e5 65 61 72 63 68 20 54 45 4d 50 20 62 65 66 6f 72  earch TEMP befor
236e6 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 69 66  e MAIN */.    if
236e7 28 20 7a 44 61 74 61 62 61 73 65 21 3d 30 20 26  ( zDatabase!=0 &
236e8 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  & sqlite3StrICmp
236e9 28 7a 44 61 74 61 62 61 73 65 2c 20 64 62 2d 3e  (zDatabase, db->
236ea 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29 20 29 20  aDb[j].zName) ) 
236eb 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70 20  continue;.    p 
236ec 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e  = sqlite3HashFin
236ed 64 28 26 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53  d(&db->aDb[j].pS
236ee 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20  chema->tblHash, 
236ef 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20  zName, nName);. 
236f0 20 20 20 69 66 28 20 70 20 29 20 62 72 65 61 6b     if( p ) break
236f1 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
236f2 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74  ;.}../*.** Locat
236f3 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  e the in-memory 
236f4 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 64  structure that d
236f5 65 73 63 72 69 62 65 73 20 61 20 70 61 72 74 69  escribes a parti
236f6 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a  cular database.*
236f7 2a 20 74 61 62 6c 65 20 67 69 76 65 6e 20 74 68  * table given th
236f8 65 20 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 74  e name of that t
236f9 61 62 6c 65 20 61 6e 64 20 28 6f 70 74 69 6f 6e  able and (option
236fa 61 6c 6c 79 29 20 74 68 65 20 6e 61 6d 65 20 6f  ally) the name o
236fb 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  f the.** databas
236fc 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
236fd 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20   table.  Return 
236fe 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e  NULL if not foun
236ff 64 2e 20 20 41 6c 73 6f 20 6c 65 61 76 65 20 61  d.  Also leave a
23700 6e 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61  n.** error messa
23701 67 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45  ge in pParse->zE
23702 72 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  rrMsg..**.** The
23703 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77   difference betw
23704 65 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  een this routine
23705 20 61 6e 64 20 73 71 6c 69 74 65 33 46 69 6e 64   and sqlite3Find
23706 54 61 62 6c 65 28 29 20 69 73 20 74 68 61 74 20  Table() is that 
23707 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  this.** routine 
23708 6c 65 61 76 65 73 20 61 6e 20 65 72 72 6f 72 20  leaves an error 
23709 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73  message in pPars
2370a 65 2d 3e 7a 45 72 72 4d 73 67 20 77 68 65 72 65  e->zErrMsg where
2370b 0a 2a 2a 20 73 71 6c 69 74 65 33 46 69 6e 64 54  .** sqlite3FindT
2370c 61 62 6c 65 28 29 20 64 6f 65 73 20 6e 6f 74 2e  able() does not.
2370d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2370e 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65  TE Table *sqlite
2370f 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 0a 20 20  3LocateTable(.  
23710 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
23711 20 20 20 20 20 20 20 2f 2a 20 63 6f 6e 74 65 78         /* contex
23712 74 20 69 6e 20 77 68 69 63 68 20 74 6f 20 72 65  t in which to re
23713 70 6f 72 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20  port errors */. 
23714 20 69 6e 74 20 69 73 56 69 65 77 2c 20 20 20 20   int isView,    
23715 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
23716 69 66 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61  if looking for a
23717 20 56 49 45 57 20 72 61 74 68 65 72 20 74 68 61   VIEW rather tha
23718 6e 20 61 20 54 41 42 4c 45 20 2a 2f 0a 20 20 63  n a TABLE */.  c
23719 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2371a 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  ,     /* Name of
2371b 20 74 68 65 20 74 61 62 6c 65 20 77 65 20 61 72   the table we ar
2371c 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 2a 2f  e looking for */
2371d 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2371e 44 62 61 73 65 20 20 20 20 20 2f 2a 20 4e 61 6d  Dbase     /* Nam
2371f 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
23720 65 2e 20 20 4d 69 67 68 74 20 62 65 20 4e 55 4c  e.  Might be NUL
23721 4c 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20  L */.){.  Table 
23722 2a 70 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74  *p;..  /* Read t
23723 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
23724 6d 61 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  ma. If an error 
23725 6f 63 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e  occurs, leave an
23726 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20   error message. 
23727 20 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20   ** and code in 
23728 70 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 72  pParse and retur
23729 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28  n NULL. */.  if(
2372a 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69   SQLITE_OK!=sqli
2372b 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
2372c 61 72 73 65 29 20 29 7b 0a 20 20 20 20 72 65 74  arse) ){.    ret
2372d 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 20  urn 0;.  }..  p 
2372e 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  = sqlite3FindTab
2372f 6c 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a  le(pParse->db, z
23730 4e 61 6d 65 2c 20 7a 44 62 61 73 65 29 3b 0a 20  Name, zDbase);. 
23731 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20   if( p==0 ){.   
23732 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 73   const char *zMs
23733 67 20 3d 20 69 73 56 69 65 77 20 3f 20 22 6e 6f  g = isView ? "no
23734 20 73 75 63 68 20 76 69 65 77 22 20 3a 20 22 6e   such view" : "n
23735 6f 20 73 75 63 68 20 74 61 62 6c 65 22 3b 0a 20  o such table";. 
23736 20 20 20 69 66 28 20 7a 44 62 61 73 65 20 29 7b     if( zDbase ){
23737 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
23738 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
23739 25 73 3a 20 25 73 2e 25 73 22 2c 20 7a 4d 73 67  %s: %s.%s", zMsg
2373a 2c 20 7a 44 62 61 73 65 2c 20 7a 4e 61 6d 65 29  , zDbase, zName)
2373b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2373c 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2373d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20  sg(pParse, "%s: 
2373e 25 73 22 2c 20 7a 4d 73 67 2c 20 7a 4e 61 6d 65  %s", zMsg, zName
2373f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61  );.    }.    pPa
23740 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61  rse->checkSchema
23741 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 1;.  }.  retu
23742 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  rn p;.}../*.** L
23743 6f 63 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d  ocate the in-mem
23744 6f 72 79 20 73 74 72 75 63 74 75 72 65 20 74 68  ory structure th
23745 61 74 20 64 65 73 63 72 69 62 65 73 20 0a 2a 2a  at describes .**
23746 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e   a particular in
23747 64 65 78 20 67 69 76 65 6e 20 74 68 65 20 6e 61  dex given the na
23748 6d 65 20 6f 66 20 74 68 61 74 20 69 6e 64 65 78  me of that index
23749 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e 61 6d 65  .** and the name
2374a 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
2374b 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
2374c 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 52 65 74  he index..** Ret
2374d 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20  urn NULL if not 
2374e 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  found..**.** If 
2374f 7a 44 61 74 61 62 61 73 65 20 69 73 20 30 2c 20  zDatabase is 0, 
23750 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 61 72  all databases ar
23751 65 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 74  e searched for t
23752 68 65 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20  he.** table and 
23753 74 68 65 20 66 69 72 73 74 20 6d 61 74 63 68 69  the first matchi
23754 6e 67 20 69 6e 64 65 78 20 69 73 20 72 65 74 75  ng index is retu
23755 72 6e 65 64 2e 20 20 28 4e 6f 20 63 68 65 63 6b  rned.  (No check
23756 69 6e 67 0a 2a 2a 20 66 6f 72 20 64 75 70 6c 69  ing.** for dupli
23757 63 61 74 65 20 69 6e 64 65 78 20 6e 61 6d 65 73  cate index names
23758 20 69 73 20 64 6f 6e 65 2e 29 20 20 54 68 65 20   is done.)  The 
23759 73 65 61 72 63 68 20 6f 72 64 65 72 20 69 73 0a  search order is.
2375a 2a 2a 20 54 45 4d 50 20 66 69 72 73 74 2c 20 74  ** TEMP first, t
2375b 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e 20 61  hen MAIN, then a
2375c 6e 79 20 61 75 78 69 6c 69 61 72 79 20 64 61 74  ny auxiliary dat
2375d 61 62 61 73 65 73 20 61 64 64 65 64 0a 2a 2a 20  abases added.** 
2375e 75 73 69 6e 67 20 74 68 65 20 41 54 54 41 43 48  using the ATTACH
2375f 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 53 51 4c   command..*/.SQL
23760 49 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64 65  ITE_PRIVATE Inde
23761 78 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 49 6e  x *sqlite3FindIn
23762 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  dex(sqlite3 *db,
23763 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
23764 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  me, const char *
23765 7a 44 62 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70  zDb){.  Index *p
23766 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20   = 0;.  int i;. 
23767 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c   int nName = sql
23768 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61  ite3Strlen30(zNa
23769 6d 65 29 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d 49  me);.  for(i=OMI
2376a 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e  T_TEMPDB; i<db->
2376b 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  nDb; i++){.    i
2376c 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69  nt j = (i<2) ? i
2376d 5e 31 20 3a 20 69 3b 20 20 2f 2a 20 53 65 61 72  ^1 : i;  /* Sear
2376e 63 68 20 54 45 4d 50 20 62 65 66 6f 72 65 20 4d  ch TEMP before M
2376f 41 49 4e 20 2a 2f 0a 20 20 20 20 53 63 68 65 6d  AIN */.    Schem
23770 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 64 62 2d  a *pSchema = db-
23771 3e 61 44 62 5b 6a 5d 2e 70 53 63 68 65 6d 61 3b  >aDb[j].pSchema;
23772 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 53 63  .    assert( pSc
23773 68 65 6d 61 20 29 3b 0a 20 20 20 20 69 66 28 20  hema );.    if( 
23774 7a 44 62 20 26 26 20 73 71 6c 69 74 65 33 53 74  zDb && sqlite3St
23775 72 49 43 6d 70 28 7a 44 62 2c 20 64 62 2d 3e 61  rICmp(zDb, db->a
23776 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29 20 29 20 63  Db[j].zName) ) c
23777 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70 20 3d  ontinue;.    p =
23778 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64   sqlite3HashFind
23779 28 26 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61  (&pSchema->idxHa
2377a 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  sh, zName, nName
2377b 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29 20 62  );.    if( p ) b
2377c 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75  reak;.  }.  retu
2377d 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn p;.}../*.** R
2377e 65 63 6c 61 69 6d 20 74 68 65 20 6d 65 6d 6f 72  eclaim the memor
2377f 79 20 75 73 65 64 20 62 79 20 61 6e 20 69 6e 64  y used by an ind
23780 65 78 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ex.*/.static voi
23781 64 20 66 72 65 65 49 6e 64 65 78 28 49 6e 64 65  d freeIndex(Inde
23782 78 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  x *p){.  sqlite3
23783 20 2a 64 62 20 3d 20 70 2d 3e 70 54 61 62 6c 65   *db = p->pTable
23784 2d 3e 64 62 4d 65 6d 3b 0a 23 69 66 6e 64 65 66  ->dbMem;.#ifndef
23785 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41   SQLITE_OMIT_ANA
23786 4c 59 5a 45 0a 20 20 73 71 6c 69 74 65 33 44 65  LYZE.  sqlite3De
23787 6c 65 74 65 49 6e 64 65 78 53 61 6d 70 6c 65 73  leteIndexSamples
23788 28 70 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71  (p);.#endif.  sq
23789 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2378a 70 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a 20 20 73  p->zColAff);.  s
2378b 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2378c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   p);.}../*.** Re
2378d 6d 6f 76 65 20 74 68 65 20 67 69 76 65 6e 20 69  move the given i
2378e 6e 64 65 78 20 66 72 6f 6d 20 74 68 65 20 69 6e  ndex from the in
2378f 64 65 78 20 68 61 73 68 20 74 61 62 6c 65 2c 20  dex hash table, 
23790 61 6e 64 20 66 72 65 65 0a 2a 2a 20 69 74 73 20  and free.** its 
23791 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65  memory structure
23792 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 64  s..**.** The ind
23793 65 78 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72  ex is removed fr
23794 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20  om the database 
23795 68 61 73 68 20 74 61 62 6c 65 73 20 62 75 74 0a  hash tables but.
23796 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20 75 6e 6c  ** it is not unl
23797 69 6e 6b 65 64 20 66 72 6f 6d 20 74 68 65 20 54  inked from the T
23798 61 62 6c 65 20 74 68 61 74 20 69 74 20 69 6e 64  able that it ind
23799 65 78 65 73 2e 0a 2a 2a 20 55 6e 6c 69 6e 6b 69  exes..** Unlinki
2379a 6e 67 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c  ng from the Tabl
2379b 65 20 6d 75 73 74 20 62 65 20 64 6f 6e 65 20 62  e must be done b
2379c 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75  y the calling fu
2379d 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  nction..*/.stati
2379e 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  c void sqlite3De
2379f 6c 65 74 65 49 6e 64 65 78 28 49 6e 64 65 78 20  leteIndex(Index 
237a0 2a 70 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 4f  *p){.  Index *pO
237a1 6c 64 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ld;.  const char
237a2 20 2a 7a 4e 61 6d 65 20 3d 20 70 2d 3e 7a 4e 61   *zName = p->zNa
237a3 6d 65 3b 0a 0a 20 20 70 4f 6c 64 20 3d 20 73 71  me;..  pOld = sq
237a4 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
237a5 26 70 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78  &p->pSchema->idx
237a6 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 0a 20 20 20  Hash, zName,.   
237a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
237a8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
237a9 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 2c 20  trlen30(zName), 
237aa 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  0);.  assert( pO
237ab 6c 64 3d 3d 30 20 7c 7c 20 70 4f 6c 64 3d 3d 70  ld==0 || pOld==p
237ac 20 29 3b 0a 20 20 66 72 65 65 49 6e 64 65 78 28   );.  freeIndex(
237ad 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72  p);.}../*.** For
237ae 20 74 68 65 20 69 6e 64 65 78 20 63 61 6c 6c 65   the index calle
237af 64 20 7a 49 64 78 4e 61 6d 65 20 77 68 69 63 68  d zIdxName which
237b0 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 74 68 65   is found in the
237b1 20 64 61 74 61 62 61 73 65 20 69 44 62 2c 0a 2a   database iDb,.*
237b2 2a 20 75 6e 6c 69 6b 65 20 74 68 61 74 20 69 6e  * unlike that in
237b3 64 65 78 20 66 72 6f 6d 20 69 74 73 20 54 61 62  dex from its Tab
237b4 6c 65 20 74 68 65 6e 20 72 65 6d 6f 76 65 20 74  le then remove t
237b5 68 65 20 69 6e 64 65 78 20 66 72 6f 6d 0a 2a 2a  he index from.**
237b6 20 74 68 65 20 69 6e 64 65 78 20 68 61 73 68 20   the index hash 
237b7 74 61 62 6c 65 20 61 6e 64 20 66 72 65 65 20 61  table and free a
237b8 6c 6c 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74  ll memory struct
237b9 75 72 65 73 20 61 73 73 6f 63 69 61 74 65 64 0a  ures associated.
237ba 2a 2a 20 77 69 74 68 20 74 68 65 20 69 6e 64 65  ** with the inde
237bb 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  x..*/.SQLITE_PRI
237bc 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
237bd 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65  3UnlinkAndDelete
237be 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64  Index(sqlite3 *d
237bf 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73  b, int iDb, cons
237c0 74 20 63 68 61 72 20 2a 7a 49 64 78 4e 61 6d 65  t char *zIdxName
237c1 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64  ){.  Index *pInd
237c2 65 78 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20  ex;.  int len;. 
237c3 20 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26   Hash *pHash = &
237c4 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
237c5 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 0a  hema->idxHash;..
237c6 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 53    len = sqlite3S
237c7 74 72 6c 65 6e 33 30 28 7a 49 64 78 4e 61 6d 65  trlen30(zIdxName
237c8 29 3b 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71  );.  pIndex = sq
237c9 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
237ca 70 48 61 73 68 2c 20 7a 49 64 78 4e 61 6d 65 2c  pHash, zIdxName,
237cb 20 6c 65 6e 2c 20 30 29 3b 0a 20 20 69 66 28 20   len, 0);.  if( 
237cc 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 69 66  pIndex ){.    if
237cd 28 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65  ( pIndex->pTable
237ce 2d 3e 70 49 6e 64 65 78 3d 3d 70 49 6e 64 65 78  ->pIndex==pIndex
237cf 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 64 65 78   ){.      pIndex
237d0 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78  ->pTable->pIndex
237d1 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74   = pIndex->pNext
237d2 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
237d3 20 20 20 49 6e 64 65 78 20 2a 70 3b 0a 20 20 20     Index *p;.   
237d4 20 20 20 2f 2a 20 4a 75 73 74 69 66 69 63 61 74     /* Justificat
237d5 69 6f 6e 20 6f 66 20 41 4c 57 41 59 53 28 29 3b  ion of ALWAYS();
237d6 20 20 54 68 65 20 69 6e 64 65 78 20 6d 75 73 74    The index must
237d7 20 62 65 20 6f 6e 20 74 68 65 20 6c 69 73 74 20   be on the list 
237d8 6f 66 0a 20 20 20 20 20 20 2a 2a 20 69 6e 64 69  of.      ** indi
237d9 63 65 73 2e 20 2a 2f 0a 20 20 20 20 20 20 70 20  ces. */.      p 
237da 3d 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65  = pIndex->pTable
237db 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 20  ->pIndex;.      
237dc 77 68 69 6c 65 28 20 41 4c 57 41 59 53 28 70 29  while( ALWAYS(p)
237dd 20 26 26 20 70 2d 3e 70 4e 65 78 74 21 3d 70 49   && p->pNext!=pI
237de 6e 64 65 78 20 29 7b 20 70 20 3d 20 70 2d 3e 70  ndex ){ p = p->p
237df 4e 65 78 74 3b 20 7d 0a 20 20 20 20 20 20 69 66  Next; }.      if
237e0 28 20 41 4c 57 41 59 53 28 70 20 26 26 20 70 2d  ( ALWAYS(p && p-
237e1 3e 70 4e 65 78 74 3d 3d 70 49 6e 64 65 78 29 20  >pNext==pIndex) 
237e2 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 4e  ){.        p->pN
237e3 65 78 74 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e  ext = pIndex->pN
237e4 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ext;.      }.   
237e5 20 7d 0a 20 20 20 20 66 72 65 65 49 6e 64 65 78   }.    freeIndex
237e6 28 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 20 20  (pIndex);.  }.  
237e7 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c  db->flags |= SQL
237e8 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
237e9 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73  s;.}../*.** Eras
237ea 65 20 61 6c 6c 20 73 63 68 65 6d 61 20 69 6e 66  e all schema inf
237eb 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68  ormation from th
237ec 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 68 61 73 68  e in-memory hash
237ed 20 74 61 62 6c 65 73 20 6f 66 0a 2a 2a 20 61 20   tables of.** a 
237ee 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 2e  single database.
237ef 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69    This routine i
237f0 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 63 6c  s called to recl
237f1 61 69 6d 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65  aim memory.** be
237f2 66 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73  fore the databas
237f3 65 20 63 6c 6f 73 65 73 2e 20 20 49 74 20 69 73  e closes.  It is
237f4 20 61 6c 73 6f 20 63 61 6c 6c 65 64 20 64 75 72   also called dur
237f5 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a  ing a rollback.*
237f6 2a 20 69 66 20 74 68 65 72 65 20 77 65 72 65 20  * if there were 
237f7 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 20 64  schema changes d
237f8 75 72 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61  uring the transa
237f9 63 74 69 6f 6e 20 6f 72 20 69 66 20 61 0a 2a 2a  ction or if a.**
237fa 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20 6d   schema-cookie m
237fb 69 73 6d 61 74 63 68 20 6f 63 63 75 72 73 2e 0a  ismatch occurs..
237fc 2a 2a 0a 2a 2a 20 49 66 20 69 44 62 3d 3d 30 20  **.** If iDb==0 
237fd 74 68 65 6e 20 72 65 73 65 74 20 74 68 65 20 69  then reset the i
237fe 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 74  nternal schema t
237ff 61 62 6c 65 73 20 66 6f 72 20 61 6c 6c 20 64 61  ables for all da
23800 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 73 2e  tabase.** files.
23801 20 20 49 66 20 69 44 62 3e 3d 31 20 74 68 65 6e    If iDb>=1 then
23802 20 72 65 73 65 74 20 74 68 65 20 69 6e 74 65 72   reset the inter
23803 6e 61 6c 20 73 63 68 65 6d 61 20 66 6f 72 20 6f  nal schema for o
23804 6e 6c 79 20 74 68 65 0a 2a 2a 20 73 69 6e 67 6c  nly the.** singl
23805 65 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64  e file indicated
23806 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
23807 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
23808 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
23809 65 6d 61 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ema(sqlite3 *db,
2380a 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 69 6e 74   int iDb){.  int
2380b 20 69 2c 20 6a 3b 0a 20 20 61 73 73 65 72 74 28   i, j;.  assert(
2380c 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64   iDb>=0 && iDb<d
2380d 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 69 66 28  b->nDb );..  if(
2380e 20 69 44 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73   iDb==0 ){.    s
2380f 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
23810 41 6c 6c 28 64 62 29 3b 0a 20 20 7d 0a 20 20 66  All(db);.  }.  f
23811 6f 72 28 69 3d 69 44 62 3b 20 69 3c 64 62 2d 3e  or(i=iDb; i<db->
23812 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 44  nDb; i++){.    D
23813 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44  b *pDb = &db->aD
23814 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44  b[i];.    if( pD
23815 62 2d 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20  b->pSchema ){.  
23816 20 20 20 20 61 73 73 65 72 74 28 69 3d 3d 31 20      assert(i==1 
23817 7c 7c 20 28 70 44 62 2d 3e 70 42 74 20 26 26 20  || (pDb->pBt && 
23818 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
23819 73 4d 75 74 65 78 28 70 44 62 2d 3e 70 42 74 29  sMutex(pDb->pBt)
2381a 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ));.      sqlite
2381b 33 53 63 68 65 6d 61 46 72 65 65 28 70 44 62 2d  3SchemaFree(pDb-
2381c 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d  >pSchema);.    }
2381d 0a 20 20 20 20 69 66 28 20 69 44 62 3e 30 20 29  .    if( iDb>0 )
2381e 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61   return;.  }.  a
2381f 73 73 65 72 74 28 20 69 44 62 3d 3d 30 20 29 3b  ssert( iDb==0 );
23820 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20  .  db->flags &= 
23821 7e 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68  ~SQLITE_InternCh
23822 61 6e 67 65 73 3b 0a 20 20 73 71 6c 69 74 65 33  anges;.  sqlite3
23823 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 64  VtabUnlockList(d
23824 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  b);.  sqlite3Btr
23825 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a  eeLeaveAll(db);.
23826 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 6f 72 20  .  /* If one or 
23827 6d 6f 72 65 20 6f 66 20 74 68 65 20 61 75 78 69  more of the auxi
23828 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66  liary database f
23829 69 6c 65 73 20 68 61 73 20 62 65 65 6e 20 63 6c  iles has been cl
2382a 6f 73 65 64 2c 0a 20 20 2a 2a 20 74 68 65 6e 20  osed,.  ** then 
2382b 72 65 6d 6f 76 65 20 74 68 65 6d 20 66 72 6f 6d  remove them from
2382c 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 64   the auxiliary d
2382d 61 74 61 62 61 73 65 20 6c 69 73 74 2e 20 20 57  atabase list.  W
2382e 65 20 74 61 6b 65 20 74 68 65 0a 20 20 2a 2a 20  e take the.  ** 
2382f 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 64  opportunity to d
23830 6f 20 74 68 69 73 20 68 65 72 65 20 73 69 6e 63  o this here sinc
23831 65 20 77 65 20 68 61 76 65 20 6a 75 73 74 20 64  e we have just d
23832 65 6c 65 74 65 64 20 61 6c 6c 20 6f 66 20 74 68  eleted all of th
23833 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20 68 61  e.  ** schema ha
23834 73 68 20 74 61 62 6c 65 73 20 61 6e 64 20 74 68  sh tables and th
23835 65 72 65 66 6f 72 65 20 64 6f 20 6e 6f 74 20 68  erefore do not h
23836 61 76 65 20 74 6f 20 6d 61 6b 65 20 61 6e 79 20  ave to make any 
23837 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 74 6f 20  changes.  ** to 
23838 61 6e 79 20 6f 66 20 74 68 6f 73 65 20 74 61 62  any of those tab
23839 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  les..  */.  for(
2383a 69 3d 6a 3d 32 3b 20 69 3c 64 62 2d 3e 6e 44 62  i=j=2; i<db->nDb
2383b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75  ; i++){.    stru
2383c 63 74 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62  ct Db *pDb = &db
2383d 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66  ->aDb[i];.    if
2383e 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 7b  ( pDb->pBt==0 ){
2383f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
23840 46 72 65 65 28 64 62 2c 20 70 44 62 2d 3e 7a 4e  Free(db, pDb->zN
23841 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 44 62 2d  ame);.      pDb-
23842 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20  >zName = 0;.    
23843 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
23844 7d 0a 20 20 20 20 69 66 28 20 6a 3c 69 20 29 7b  }.    if( j<i ){
23845 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 6a  .      db->aDb[j
23846 5d 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a  ] = db->aDb[i];.
23847 20 20 20 20 7d 0a 20 20 20 20 6a 2b 2b 3b 0a 20      }.    j++;. 
23848 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 64 62 2d   }.  memset(&db-
23849 3e 61 44 62 5b 6a 5d 2c 20 30 2c 20 28 64 62 2d  >aDb[j], 0, (db-
2384a 3e 6e 44 62 2d 6a 29 2a 73 69 7a 65 6f 66 28 64  >nDb-j)*sizeof(d
2384b 62 2d 3e 61 44 62 5b 6a 5d 29 29 3b 0a 20 20 64  b->aDb[j]));.  d
2384c 62 2d 3e 6e 44 62 20 3d 20 6a 3b 0a 20 20 69 66  b->nDb = j;.  if
2384d 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 26 26 20  ( db->nDb<=2 && 
2384e 64 62 2d 3e 61 44 62 21 3d 64 62 2d 3e 61 44 62  db->aDb!=db->aDb
2384f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 6d 65  Static ){.    me
23850 6d 63 70 79 28 64 62 2d 3e 61 44 62 53 74 61 74  mcpy(db->aDbStat
23851 69 63 2c 20 64 62 2d 3e 61 44 62 2c 20 32 2a 73  ic, db->aDb, 2*s
23852 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d  izeof(db->aDb[0]
23853 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  ));.    sqlite3D
23854 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 44  bFree(db, db->aD
23855 62 29 3b 0a 20 20 20 20 64 62 2d 3e 61 44 62 20  b);.    db->aDb 
23856 3d 20 64 62 2d 3e 61 44 62 53 74 61 74 69 63 3b  = db->aDbStatic;
23857 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
23858 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
23859 6c 6c 65 64 20 77 68 65 6e 20 61 20 63 6f 6d 6d  lled when a comm
2385a 69 74 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51  it occurs..*/.SQ
2385b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2385c 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49  d sqlite3CommitI
2385d 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 73  nternalChanges(s
2385e 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 64  qlite3 *db){.  d
2385f 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c  b->flags &= ~SQL
23860 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
23861 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61  s;.}../*.** Clea
23862 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  r the column nam
23863 65 73 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20  es from a table 
23864 6f 72 20 76 69 65 77 2e 0a 2a 2f 0a 73 74 61 74  or view..*/.stat
23865 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 52 65  ic void sqliteRe
23866 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 54  setColumnNames(T
23867 61 62 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20  able *pTable){. 
23868 20 69 6e 74 20 69 3b 0a 20 20 43 6f 6c 75 6d 6e   int i;.  Column
23869 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65   *pCol;.  sqlite
2386a 33 20 2a 64 62 20 3d 20 70 54 61 62 6c 65 2d 3e  3 *db = pTable->
2386b 64 62 4d 65 6d 3b 0a 20 20 74 65 73 74 63 61 73  dbMem;.  testcas
2386c 65 28 20 64 62 3d 3d 30 20 29 3b 0a 20 20 61 73  e( db==0 );.  as
2386d 73 65 72 74 28 20 70 54 61 62 6c 65 21 3d 30 20  sert( pTable!=0 
2386e 29 3b 0a 20 20 69 66 28 20 28 70 43 6f 6c 20 3d  );.  if( (pCol =
2386f 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29 21 3d   pTable->aCol)!=
23870 30 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  0 ){.    for(i=0
23871 3b 20 69 3c 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c  ; i<pTable->nCol
23872 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a  ; i++, pCol++){.
23873 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
23874 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e  ree(db, pCol->zN
23875 61 6d 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ame);.      sqli
23876 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
23877 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 29 3b 0a  , pCol->pDflt);.
23878 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
23879 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 44  ree(db, pCol->zD
2387a 66 6c 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  flt);.      sqli
2387b 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43  te3DbFree(db, pC
2387c 6f 6c 2d 3e 7a 54 79 70 65 29 3b 0a 20 20 20 20  ol->zType);.    
2387d 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2387e 64 62 2c 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 29  db, pCol->zColl)
2387f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
23880 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54  te3DbFree(db, pT
23881 61 62 6c 65 2d 3e 61 43 6f 6c 29 3b 0a 20 20 7d  able->aCol);.  }
23882 0a 20 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 20  .  pTable->aCol 
23883 3d 20 30 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 6e  = 0;.  pTable->n
23884 43 6f 6c 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  Col = 0;.}../*.*
23885 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 6d 65 6d  * Remove the mem
23886 6f 72 79 20 64 61 74 61 20 73 74 72 75 63 74 75  ory data structu
23887 72 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  res associated w
23888 69 74 68 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a  ith the given.**
23889 20 54 61 62 6c 65 2e 20 20 4e 6f 20 63 68 61 6e   Table.  No chan
2388a 67 65 73 20 61 72 65 20 6d 61 64 65 20 74 6f 20  ges are made to 
2388b 64 69 73 6b 20 62 79 20 74 68 69 73 20 72 6f 75  disk by this rou
2388c 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tine..**.** This
2388d 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 20 64 65   routine just de
2388e 6c 65 74 65 73 20 74 68 65 20 64 61 74 61 20 73  letes the data s
2388f 74 72 75 63 74 75 72 65 2e 20 20 49 74 20 64 6f  tructure.  It do
23890 65 73 20 6e 6f 74 20 75 6e 6c 69 6e 6b 0a 2a 2a  es not unlink.**
23891 20 74 68 65 20 74 61 62 6c 65 20 64 61 74 61 20   the table data 
23892 73 74 72 75 63 74 75 72 65 20 66 72 6f 6d 20 74  structure from t
23893 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20  he hash table.  
23894 42 75 74 20 69 74 20 64 6f 65 73 20 64 65 73 74  But it does dest
23895 72 6f 79 0a 2a 2a 20 6d 65 6d 6f 72 79 20 73 74  roy.** memory st
23896 72 75 63 74 75 72 65 73 20 6f 66 20 74 68 65 20  ructures of the 
23897 69 6e 64 69 63 65 73 20 61 6e 64 20 66 6f 72 65  indices and fore
23898 69 67 6e 20 6b 65 79 73 20 61 73 73 6f 63 69 61  ign keys associa
23899 74 65 64 20 77 69 74 68 20 0a 2a 2a 20 74 68 65  ted with .** the
2389a 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54   table..*/.SQLIT
2389b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2389c 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c  qlite3DeleteTabl
2389d 65 28 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 29  e(Table *pTable)
2389e 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  {.  Index *pInde
2389f 78 2c 20 2a 70 4e 65 78 74 3b 0a 20 20 73 71 6c  x, *pNext;.  sql
238a0 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28  ite3 *db;..  if(
238a1 20 70 54 61 62 6c 65 3d 3d 30 20 29 20 72 65 74   pTable==0 ) ret
238a2 75 72 6e 3b 0a 20 20 64 62 20 3d 20 70 54 61 62  urn;.  db = pTab
238a3 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 20 20 74 65 73  le->dbMem;.  tes
238a4 74 63 61 73 65 28 20 64 62 3d 3d 30 20 29 3b 0a  tcase( db==0 );.
238a5 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 65 6c  .  /* Do not del
238a6 65 74 65 20 74 68 65 20 74 61 62 6c 65 20 75 6e  ete the table un
238a7 74 69 6c 20 74 68 65 20 72 65 66 65 72 65 6e 63  til the referenc
238a8 65 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20  e count reaches 
238a9 7a 65 72 6f 2e 20 2a 2f 0a 20 20 70 54 61 62 6c  zero. */.  pTabl
238aa 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28  e->nRef--;.  if(
238ab 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 3e 30 20   pTable->nRef>0 
238ac 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
238ad 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61   }.  assert( pTa
238ae 62 6c 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a  ble->nRef==0 );.
238af 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6c 6c  .  /* Delete all
238b0 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61   indices associa
238b1 74 65 64 20 77 69 74 68 20 74 68 69 73 20 74 61  ted with this ta
238b2 62 6c 65 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70  ble.  */.  for(p
238b3 49 6e 64 65 78 20 3d 20 70 54 61 62 6c 65 2d 3e  Index = pTable->
238b4 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b 20  pIndex; pIndex; 
238b5 70 49 6e 64 65 78 3d 70 4e 65 78 74 29 7b 0a 20  pIndex=pNext){. 
238b6 20 20 20 70 4e 65 78 74 20 3d 20 70 49 6e 64 65     pNext = pInde
238b7 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 61 73  x->pNext;.    as
238b8 73 65 72 74 28 20 70 49 6e 64 65 78 2d 3e 70 53  sert( pIndex->pS
238b9 63 68 65 6d 61 3d 3d 70 54 61 62 6c 65 2d 3e 70  chema==pTable->p
238ba 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 73 71  Schema );.    sq
238bb 6c 69 74 65 33 44 65 6c 65 74 65 49 6e 64 65 78  lite3DeleteIndex
238bc 28 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 0a 20  (pIndex);.  }.. 
238bd 20 2f 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 66   /* Delete any f
238be 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 74 74 61  oreign keys atta
238bf 63 68 65 64 20 74 6f 20 74 68 69 73 20 74 61 62  ched to this tab
238c0 6c 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  le. */.  sqlite3
238c1 46 6b 44 65 6c 65 74 65 28 70 54 61 62 6c 65 29  FkDelete(pTable)
238c2 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74  ;..  /* Delete t
238c3 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
238c4 72 65 20 69 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a  re itself..  */.
238c5 20 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f 6c    sqliteResetCol
238c6 75 6d 6e 4e 61 6d 65 73 28 70 54 61 62 6c 65 29  umnNames(pTable)
238c7 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
238c8 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e  e(db, pTable->zN
238c9 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ame);.  sqlite3D
238ca 62 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65  bFree(db, pTable
238cb 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a 20 20 73 71  ->zColAff);.  sq
238cc 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
238cd 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 70 53  e(db, pTable->pS
238ce 65 6c 65 63 74 29 3b 0a 23 69 66 6e 64 65 66 20  elect);.#ifndef 
238cf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43  SQLITE_OMIT_CHEC
238d0 4b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  K.  sqlite3ExprD
238d1 65 6c 65 74 65 28 64 62 2c 20 70 54 61 62 6c 65  elete(db, pTable
238d2 2d 3e 70 43 68 65 63 6b 29 3b 0a 23 65 6e 64 69  ->pCheck);.#endi
238d3 66 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 43  f.  sqlite3VtabC
238d4 6c 65 61 72 28 70 54 61 62 6c 65 29 3b 0a 20 20  lear(pTable);.  
238d5 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
238d6 2c 20 70 54 61 62 6c 65 29 3b 0a 7d 0a 0a 2f 2a  , pTable);.}../*
238d7 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 67  .** Unlink the g
238d8 69 76 65 6e 20 74 61 62 6c 65 20 66 72 6f 6d 20  iven table from 
238d9 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 73 20  the hash tables 
238da 61 6e 64 20 74 68 65 20 64 65 6c 65 74 65 20 74  and the delete t
238db 68 65 0a 2a 2a 20 74 61 62 6c 65 20 73 74 72 75  he.** table stru
238dc 63 74 75 72 65 20 77 69 74 68 20 61 6c 6c 20 69  cture with all i
238dd 74 73 20 69 6e 64 69 63 65 73 20 61 6e 64 20 66  ts indices and f
238de 6f 72 65 69 67 6e 20 6b 65 79 73 2e 0a 2a 2f 0a  oreign keys..*/.
238df 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
238e0 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e  oid sqlite3Unlin
238e1 6b 41 6e 64 44 65 6c 65 74 65 54 61 62 6c 65 28  kAndDeleteTable(
238e2 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
238e3 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   iDb, const char
238e4 20 2a 7a 54 61 62 4e 61 6d 65 29 7b 0a 20 20 54   *zTabName){.  T
238e5 61 62 6c 65 20 2a 70 3b 0a 20 20 44 62 20 2a 70  able *p;.  Db *p
238e6 44 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64  Db;..  assert( d
238e7 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  b!=0 );.  assert
238e8 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c  ( iDb>=0 && iDb<
238e9 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73  db->nDb );.  ass
238ea 65 72 74 28 20 7a 54 61 62 4e 61 6d 65 20 26 26  ert( zTabName &&
238eb 20 7a 54 61 62 4e 61 6d 65 5b 30 5d 20 29 3b 0a   zTabName[0] );.
238ec 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62    pDb = &db->aDb
238ed 5b 69 44 62 5d 3b 0a 20 20 70 20 3d 20 73 71 6c  [iDb];.  p = sql
238ee 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 26  ite3HashInsert(&
238ef 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62  pDb->pSchema->tb
238f0 6c 48 61 73 68 2c 20 7a 54 61 62 4e 61 6d 65 2c  lHash, zTabName,
238f1 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
238f2 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
238f3 53 74 72 6c 65 6e 33 30 28 7a 54 61 62 4e 61 6d  Strlen30(zTabNam
238f4 65 29 2c 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  e),0);.  sqlite3
238f5 44 65 6c 65 74 65 54 61 62 6c 65 28 70 29 3b 0a  DeleteTable(p);.
238f6 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
238f7 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e  QLITE_InternChan
238f8 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69  ges;.}../*.** Gi
238f9 76 65 6e 20 61 20 74 6f 6b 65 6e 2c 20 72 65 74  ven a token, ret
238fa 75 72 6e 20 61 20 73 74 72 69 6e 67 20 74 68 61  urn a string tha
238fb 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68  t consists of th
238fc 65 20 74 65 78 74 20 6f 66 20 74 68 61 74 0a 2a  e text of that.*
238fd 2a 20 74 6f 6b 65 6e 2e 20 20 53 70 61 63 65 20  * token.  Space 
238fe 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75  to hold the retu
238ff 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20 69  rned string.** i
23900 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
23901 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 61  sqliteMalloc() a
23902 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 65 64  nd must be freed
23903 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a   by the calling.
23904 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a  ** function..**.
23905 2a 2a 20 41 6e 79 20 71 75 6f 74 61 74 69 6f 6e  ** Any quotation
23906 20 6d 61 72 6b 73 20 28 65 78 3a 20 20 22 6e 61   marks (ex:  "na
23907 6d 65 22 2c 20 27 6e 61 6d 65 27 2c 20 5b 6e 61  me", 'name', [na
23908 6d 65 5d 2c 20 6f 72 20 60 6e 61 6d 65 60 29 20  me], or `name`) 
23909 74 68 61 74 0a 2a 2a 20 73 75 72 72 6f 75 6e 64  that.** surround
2390a 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65   the body of the
2390b 20 74 6f 6b 65 6e 20 61 72 65 20 72 65 6d 6f 76   token are remov
2390c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 6f 6b 65 6e 73  ed..**.** Tokens
2390d 20 61 72 65 20 6f 66 74 65 6e 20 6a 75 73 74 20   are often just 
2390e 70 6f 69 6e 74 65 72 73 20 69 6e 74 6f 20 74 68  pointers into th
2390f 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74  e original SQL t
23910 65 78 74 20 61 6e 64 20 73 6f 0a 2a 2a 20 61 72  ext and so.** ar
23911 65 20 6e 6f 74 20 5c 30 30 30 20 74 65 72 6d 69  e not \000 termi
23912 6e 61 74 65 64 20 61 6e 64 20 61 72 65 20 6e 6f  nated and are no
23913 74 20 70 65 72 73 69 73 74 65 6e 74 2e 20 20 54  t persistent.  T
23914 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  he returned stri
23915 6e 67 0a 2a 2a 20 69 73 20 5c 30 30 30 20 74 65  ng.** is \000 te
23916 72 6d 69 6e 61 74 65 64 20 61 6e 64 20 69 73 20  rminated and is 
23917 70 65 72 73 69 73 74 65 6e 74 2e 0a 2a 2f 0a 53  persistent..*/.S
23918 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
23919 61 72 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46  ar *sqlite3NameF
2391a 72 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33  romToken(sqlite3
2391b 20 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61   *db, Token *pNa
2391c 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61  me){.  char *zNa
2391d 6d 65 3b 0a 20 20 69 66 28 20 70 4e 61 6d 65 20  me;.  if( pName 
2391e 29 7b 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73  ){.    zName = s
2391f 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
23920 64 62 2c 20 28 63 68 61 72 2a 29 70 4e 61 6d 65  db, (char*)pName
23921 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e 29 3b 0a  ->z, pName->n);.
23922 20 20 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f      sqlite3Dequo
23923 74 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 7d 65 6c  te(zName);.  }el
23924 73 65 7b 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20  se{.    zName = 
23925 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
23926 7a 4e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  zName;.}../*.** 
23927 4f 70 65 6e 20 74 68 65 20 73 71 6c 69 74 65 5f  Open the sqlite_
23928 6d 61 73 74 65 72 20 74 61 62 6c 65 20 73 74 6f  master table sto
23929 72 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 20  red in database 
2392a 6e 75 6d 62 65 72 20 69 44 62 20 66 6f 72 0a 2a  number iDb for.*
2392b 2a 20 77 72 69 74 69 6e 67 2e 20 54 68 65 20 74  * writing. The t
2392c 61 62 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 75  able is opened u
2392d 73 69 6e 67 20 63 75 72 73 6f 72 20 30 2e 0a 2a  sing cursor 0..*
2392e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2392f 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65   void sqlite3Ope
23930 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 50 61 72  nMasterTable(Par
23931 73 65 20 2a 70 2c 20 69 6e 74 20 69 44 62 29 7b  se *p, int iDb){
23932 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c  .  Vdbe *v = sql
23933 69 74 65 33 47 65 74 56 64 62 65 28 70 29 3b 0a  ite3GetVdbe(p);.
23934 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f    sqlite3TableLo
23935 63 6b 28 70 2c 20 69 44 62 2c 20 4d 41 53 54 45  ck(p, iDb, MASTE
23936 52 5f 52 4f 4f 54 2c 20 31 2c 20 53 43 48 45 4d  R_ROOT, 1, SCHEM
23937 41 5f 54 41 42 4c 45 28 69 44 62 29 29 3b 0a 20  A_TABLE(iDb));. 
23938 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
23939 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  p3(v, OP_OpenWri
2393a 74 65 2c 20 30 2c 20 4d 41 53 54 45 52 5f 52 4f  te, 0, MASTER_RO
2393b 4f 54 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c 69  OT, iDb);.  sqli
2393c 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
2393d 76 2c 20 2d 31 2c 20 28 63 68 61 72 20 2a 29 35  v, -1, (char *)5
2393e 2c 20 50 34 5f 49 4e 54 33 32 29 3b 20 20 2f 2a  , P4_INT32);  /*
2393f 20 35 20 63 6f 6c 75 6d 6e 20 74 61 62 6c 65 20   5 column table 
23940 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 6e 54 61 62  */.  if( p->nTab
23941 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 54  ==0 ){.    p->nT
23942 61 62 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ab = 1;.  }.}../
23943 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a  *.** Parameter z
23944 4e 61 6d 65 20 70 6f 69 6e 74 73 20 74 6f 20 61  Name points to a
23945 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20   nul-terminated 
23946 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e  buffer containin
23947 67 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66  g the name.** of
23948 20 61 20 64 61 74 61 62 61 73 65 20 28 22 6d 61   a database ("ma
23949 69 6e 22 2c 20 22 74 65 6d 70 22 20 6f 72 20 74  in", "temp" or t
2394a 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 20 61 74  he name of an at
2394b 74 61 63 68 65 64 20 64 62 29 2e 20 54 68 69 73  tached db). This
2394c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  .** function ret
2394d 75 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f  urns the index o
2394e 66 20 74 68 65 20 6e 61 6d 65 64 20 64 61 74 61  f the named data
2394f 62 61 73 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b  base in db->aDb[
23950 5d 2c 20 6f 72 0a 2a 2a 20 2d 31 20 69 66 20 74  ], or.** -1 if t
23951 68 65 20 6e 61 6d 65 64 20 64 62 20 63 61 6e 6e  he named db cann
23952 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a  ot be found..*/.
23953 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
23954 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62  nt sqlite3FindDb
23955 4e 61 6d 65 28 73 71 6c 69 74 65 33 20 2a 64 62  Name(sqlite3 *db
23956 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
23957 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20  ame){.  int i = 
23958 2d 31 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44  -1;         /* D
23959 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a  atabase number *
2395a 2f 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b  /.  if( zName ){
2395b 0a 20 20 20 20 44 62 20 2a 70 44 62 3b 0a 20 20  .    Db *pDb;.  
2395c 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65    int n = sqlite
2395d 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29  3Strlen30(zName)
2395e 3b 0a 20 20 20 20 66 6f 72 28 69 3d 28 64 62 2d  ;.    for(i=(db-
2395f 3e 6e 44 62 2d 31 29 2c 20 70 44 62 3d 26 64 62  >nDb-1), pDb=&db
23960 2d 3e 61 44 62 5b 69 5d 3b 20 69 3e 3d 30 3b 20  ->aDb[i]; i>=0; 
23961 69 2d 2d 2c 20 70 44 62 2d 2d 29 7b 0a 20 20 20  i--, pDb--){.   
23962 20 20 20 69 66 28 20 28 21 4f 4d 49 54 5f 54 45     if( (!OMIT_TE
23963 4d 50 44 42 20 7c 7c 20 69 21 3d 31 20 29 20 26  MPDB || i!=1 ) &
23964 26 20 6e 3d 3d 73 71 6c 69 74 65 33 53 74 72 6c  & n==sqlite3Strl
23965 65 6e 33 30 28 70 44 62 2d 3e 7a 4e 61 6d 65 29  en30(pDb->zName)
23966 20 26 26 20 0a 20 20 20 20 20 20 20 20 20 20 30   && .          0
23967 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ==sqlite3StrICmp
23968 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61  (pDb->zName, zNa
23969 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62  me) ){.        b
2396a 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2396b 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
2396c 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65   i;.}../*.** The
2396d 20 74 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 63 6f   token *pName co
2396e 6e 74 61 69 6e 73 20 74 68 65 20 6e 61 6d 65 20  ntains the name 
2396f 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 28 65  of a database (e
23970 69 74 68 65 72 20 22 6d 61 69 6e 22 20 6f 72 0a  ither "main" or.
23971 2a 2a 20 22 74 65 6d 70 22 20 6f 72 20 74 68 65  ** "temp" or the
23972 20 6e 61 6d 65 20 6f 66 20 61 6e 20 61 74 74 61   name of an atta
23973 63 68 65 64 20 64 62 29 2e 20 54 68 69 73 20 72  ched db). This r
23974 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74  outine returns t
23975 68 65 0a 2a 2a 20 69 6e 64 65 78 20 6f 66 20 74  he.** index of t
23976 68 65 20 6e 61 6d 65 64 20 64 61 74 61 62 61 73  he named databas
23977 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 2c 20  e in db->aDb[], 
23978 6f 72 20 2d 31 20 69 66 20 74 68 65 20 6e 61 6d  or -1 if the nam
23979 65 64 20 64 62 20 0a 2a 2a 20 64 6f 65 73 20 6e  ed db .** does n
2397a 6f 74 20 65 78 69 73 74 2e 0a 2a 2f 0a 53 51 4c  ot exist..*/.SQL
2397b 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2397c 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 73 71  sqlite3FindDb(sq
2397d 6c 69 74 65 33 20 2a 64 62 2c 20 54 6f 6b 65 6e  lite3 *db, Token
2397e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20   *pName){.  int 
2397f 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
23980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23981 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d   /* Database num
23982 62 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ber */.  char *z
23983 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
23984 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
23985 20 4e 61 6d 65 20 77 65 20 61 72 65 20 73 65 61   Name we are sea
23986 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 20  rching for */.  
23987 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e  zName = sqlite3N
23988 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
23989 20 70 4e 61 6d 65 29 3b 0a 20 20 69 20 3d 20 73   pName);.  i = s
2398a 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65  qlite3FindDbName
2398b 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73  (db, zName);.  s
2398c 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2398d 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72   zName);.  retur
2398e 6e 20 69 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 74  n i;.}../* The t
2398f 61 62 6c 65 20 6f 72 20 76 69 65 77 20 6f 72 20  able or view or 
23990 74 72 69 67 67 65 72 20 6e 61 6d 65 20 69 73 20  trigger name is 
23991 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 72  passed to this r
23992 6f 75 74 69 6e 65 20 76 69 61 20 74 6f 6b 65 6e  outine via token
23993 73 0a 2a 2a 20 70 4e 61 6d 65 31 20 61 6e 64 20  s.** pName1 and 
23994 70 4e 61 6d 65 32 2e 20 49 66 20 74 68 65 20 74  pName2. If the t
23995 61 62 6c 65 20 6e 61 6d 65 20 77 61 73 20 66 75  able name was fu
23996 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20 66  lly qualified, f
23997 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a  or example:.**.*
23998 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 78  * CREATE TABLE x
23999 78 78 2e 79 79 79 20 28 2e 2e 2e 29 3b 0a 2a 2a  xx.yyy (...);.**
2399a 20 0a 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31   .** Then pName1
2399b 20 69 73 20 73 65 74 20 74 6f 20 22 78 78 78 22   is set to "xxx"
2399c 20 61 6e 64 20 70 4e 61 6d 65 32 20 22 79 79 79   and pName2 "yyy
2399d 22 2e 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20  ". On the other 
2399e 68 61 6e 64 20 69 66 0a 2a 2a 20 74 68 65 20 74  hand if.** the t
2399f 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74  able name is not
239a0 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64   fully qualified
239a1 2c 20 69 2e 65 2e 3a 0a 2a 2a 0a 2a 2a 20 43 52  , i.e.:.**.** CR
239a2 45 41 54 45 20 54 41 42 4c 45 20 79 79 79 28 2e  EATE TABLE yyy(.
239a3 2e 2e 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20  ..);.**.** Then 
239a4 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74 6f  pName1 is set to
239a5 20 22 79 79 79 22 20 61 6e 64 20 70 4e 61 6d 65   "yyy" and pName
239a6 32 20 69 73 20 22 22 2e 0a 2a 2a 0a 2a 2a 20 54  2 is ""..**.** T
239a7 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73  his routine sets
239a8 20 74 68 65 20 2a 70 70 55 6e 71 75 61 6c 20 70   the *ppUnqual p
239a9 6f 69 6e 74 65 72 20 74 6f 20 70 6f 69 6e 74 20  ointer to point 
239aa 61 74 20 74 68 65 20 74 6f 6b 65 6e 20 28 70 4e  at the token (pN
239ab 61 6d 65 31 20 6f 72 0a 2a 2a 20 70 4e 61 6d 65  ame1 or.** pName
239ac 32 29 20 74 68 61 74 20 73 74 6f 72 65 73 20 74  2) that stores t
239ad 68 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74  he unqualified t
239ae 61 62 6c 65 20 6e 61 6d 65 2e 20 20 54 68 65 20  able name.  The 
239af 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20  index of the.** 
239b0 64 61 74 61 62 61 73 65 20 22 78 78 78 22 20 69  database "xxx" i
239b1 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53  s returned..*/.S
239b2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
239b3 74 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74  t sqlite3TwoPart
239b4 4e 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70  Name(.  Parse *p
239b5 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 50  Parse,      /* P
239b6 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20  arsing and code 
239b7 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65  generating conte
239b8 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  xt */.  Token *p
239b9 4e 61 6d 65 31 2c 20 20 20 20 20 20 2f 2a 20 54  Name1,      /* T
239ba 68 65 20 22 78 78 78 22 20 69 6e 20 74 68 65 20  he "xxx" in the 
239bb 6e 61 6d 65 20 22 78 78 78 2e 79 79 79 22 20 6f  name "xxx.yyy" o
239bc 72 20 22 78 78 78 22 20 2a 2f 0a 20 20 54 6f 6b  r "xxx" */.  Tok
239bd 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20  en *pName2,     
239be 20 2f 2a 20 54 68 65 20 22 79 79 79 22 20 69 6e   /* The "yyy" in
239bf 20 74 68 65 20 6e 61 6d 65 20 22 78 78 78 2e 79   the name "xxx.y
239c0 79 79 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  yy" */.  Token *
239c1 2a 70 55 6e 71 75 61 6c 20 20 20 20 20 2f 2a 20  *pUnqual     /* 
239c2 57 72 69 74 65 20 74 68 65 20 75 6e 71 75 61 6c  Write the unqual
239c3 69 66 69 65 64 20 6f 62 6a 65 63 74 20 6e 61 6d  ified object nam
239c4 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  e here */.){.  i
239c5 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
239c6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
239c7 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74  tabase holding t
239c8 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73  he object */.  s
239c9 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
239ca 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20  rse->db;..  if( 
239cb 41 4c 57 41 59 53 28 70 4e 61 6d 65 32 21 3d 30  ALWAYS(pName2!=0
239cc 29 20 26 26 20 70 4e 61 6d 65 32 2d 3e 6e 3e 30  ) && pName2->n>0
239cd 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e   ){.    if( db->
239ce 69 6e 69 74 2e 62 75 73 79 20 29 20 7b 0a 20 20  init.busy ) {.  
239cf 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
239d0 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 6f 72  Msg(pParse, "cor
239d1 72 75 70 74 20 64 61 74 61 62 61 73 65 22 29 3b  rupt database");
239d2 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  .      pParse->n
239d3 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74  Err++;.      ret
239d4 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20  urn -1;.    }.  
239d5 20 20 2a 70 55 6e 71 75 61 6c 20 3d 20 70 4e 61    *pUnqual = pNa
239d6 6d 65 32 3b 0a 20 20 20 20 69 44 62 20 3d 20 73  me2;.    iDb = s
239d7 71 6c 69 74 65 33 46 69 6e 64 44 62 28 64 62 2c  qlite3FindDb(db,
239d8 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 69 66   pName1);.    if
239d9 28 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 20 20  ( iDb<0 ){.     
239da 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
239db 28 70 50 61 72 73 65 2c 20 22 75 6e 6b 6e 6f 77  (pParse, "unknow
239dc 6e 20 64 61 74 61 62 61 73 65 20 25 54 22 2c 20  n database %T", 
239dd 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 20 20 70  pName1);.      p
239de 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20  Parse->nErr++;. 
239df 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a       return -1;.
239e0 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
239e1 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69     assert( db->i
239e2 6e 69 74 2e 69 44 62 3d 3d 30 20 7c 7c 20 64 62  nit.iDb==0 || db
239e3 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 3b 0a 20  ->init.busy );. 
239e4 20 20 20 69 44 62 20 3d 20 64 62 2d 3e 69 6e 69     iDb = db->ini
239e5 74 2e 69 44 62 3b 0a 20 20 20 20 2a 70 55 6e 71  t.iDb;.    *pUnq
239e6 75 61 6c 20 3d 20 70 4e 61 6d 65 31 3b 0a 20 20  ual = pName1;.  
239e7 7d 0a 20 20 72 65 74 75 72 6e 20 69 44 62 3b 0a  }.  return iDb;.
239e8 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
239e9 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f  utine is used to
239ea 20 63 68 65 63 6b 20 69 66 20 74 68 65 20 55 54   check if the UT
239eb 46 2d 38 20 73 74 72 69 6e 67 20 7a 4e 61 6d 65  F-8 string zName
239ec 20 69 73 20 61 20 6c 65 67 61 6c 0a 2a 2a 20 75   is a legal.** u
239ed 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 20  nqualified name 
239ee 66 6f 72 20 61 20 6e 65 77 20 73 63 68 65 6d 61  for a new schema
239ef 20 6f 62 6a 65 63 74 20 28 74 61 62 6c 65 2c 20   object (table, 
239f0 69 6e 64 65 78 2c 20 76 69 65 77 20 6f 72 0a 2a  index, view or.*
239f1 2a 20 74 72 69 67 67 65 72 29 2e 20 41 6c 6c 20  * trigger). All 
239f2 6e 61 6d 65 73 20 61 72 65 20 6c 65 67 61 6c 20  names are legal 
239f3 65 78 63 65 70 74 20 74 68 6f 73 65 20 74 68 61  except those tha
239f4 74 20 62 65 67 69 6e 20 77 69 74 68 20 74 68 65  t begin with the
239f5 20 73 74 72 69 6e 67 0a 2a 2a 20 22 73 71 6c 69   string.** "sqli
239f6 74 65 5f 22 20 28 69 6e 20 75 70 70 65 72 2c 20  te_" (in upper, 
239f7 6c 6f 77 65 72 20 6f 72 20 6d 69 78 65 64 20 63  lower or mixed c
239f8 61 73 65 29 2e 20 54 68 69 73 20 70 6f 72 74 69  ase). This porti
239f9 6f 6e 20 6f 66 20 74 68 65 20 6e 61 6d 65 73 70  on of the namesp
239fa 61 63 65 0a 2a 2a 20 69 73 20 72 65 73 65 72 76  ace.** is reserv
239fb 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20  ed for internal 
239fc 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  use..*/.SQLITE_P
239fd 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
239fe 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d  e3CheckObjectNam
239ff 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
23a00 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
23a01 6d 65 29 7b 0a 20 20 69 66 28 20 21 70 50 61 72  me){.  if( !pPar
23a02 73 65 2d 3e 64 62 2d 3e 69 6e 69 74 2e 62 75 73  se->db->init.bus
23a03 79 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 73  y && pParse->nes
23a04 74 65 64 3d 3d 30 20 0a 20 20 20 20 20 20 20 20  ted==0 .        
23a05 20 20 26 26 20 28 70 50 61 72 73 65 2d 3e 64 62    && (pParse->db
23a06 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
23a07 5f 57 72 69 74 65 53 63 68 65 6d 61 29 3d 3d 30  _WriteSchema)==0
23a08 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 30 3d  .          && 0=
23a09 3d 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70  =sqlite3StrNICmp
23a0a 28 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f  (zName, "sqlite_
23a0b 22 2c 20 37 29 20 29 7b 0a 20 20 20 20 73 71 6c  ", 7) ){.    sql
23a0c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
23a0d 72 73 65 2c 20 22 6f 62 6a 65 63 74 20 6e 61 6d  rse, "object nam
23a0e 65 20 72 65 73 65 72 76 65 64 20 66 6f 72 20 69  e reserved for i
23a0f 6e 74 65 72 6e 61 6c 20 75 73 65 3a 20 25 73 22  nternal use: %s"
23a10 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65  , zName);.    re
23a11 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
23a12 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  R;.  }.  return 
23a13 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
23a14 0a 2a 2a 20 42 65 67 69 6e 20 63 6f 6e 73 74 72  .** Begin constr
23a15 75 63 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62  ucting a new tab
23a16 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  le representatio
23a17 6e 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 54 68  n in memory.  Th
23a18 69 73 20 69 73 0a 2a 2a 20 74 68 65 20 66 69 72  is is.** the fir
23a19 73 74 20 6f 66 20 73 65 76 65 72 61 6c 20 61 63  st of several ac
23a1a 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 74 68  tion routines th
23a1b 61 74 20 67 65 74 20 63 61 6c 6c 65 64 20 69 6e  at get called in
23a1c 20 72 65 73 70 6f 6e 73 65 0a 2a 2a 20 74 6f 20   response.** to 
23a1d 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  a CREATE TABLE s
23a1e 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e 20 70 61  tatement.  In pa
23a1f 72 74 69 63 75 6c 61 72 2c 20 74 68 69 73 20 72  rticular, this r
23a20 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
23a21 0a 2a 2a 20 61 66 74 65 72 20 73 65 65 69 6e 67  .** after seeing
23a22 20 74 6f 6b 65 6e 73 20 22 43 52 45 41 54 45 22   tokens "CREATE"
23a23 20 61 6e 64 20 22 54 41 42 4c 45 22 20 61 6e 64   and "TABLE" and
23a24 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2e   the table name.
23a25 20 54 68 65 20 69 73 54 65 6d 70 0a 2a 2a 20 66   The isTemp.** f
23a26 6c 61 67 20 69 73 20 74 72 75 65 20 69 66 20 74  lag is true if t
23a27 68 65 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20  he table should 
23a28 62 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  be stored in the
23a29 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62   auxiliary datab
23a2a 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 6e 73 74  ase.** file inst
23a2b 65 61 64 20 6f 66 20 69 6e 20 74 68 65 20 6d 61  ead of in the ma
23a2c 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
23a2d 2e 20 20 54 68 69 73 20 69 73 20 6e 6f 72 6d 61  .  This is norma
23a2e 6c 6c 79 20 74 68 65 20 63 61 73 65 0a 2a 2a 20  lly the case.** 
23a2f 77 68 65 6e 20 74 68 65 20 22 54 45 4d 50 22 20  when the "TEMP" 
23a30 6f 72 20 22 54 45 4d 50 4f 52 41 52 59 22 20 6b  or "TEMPORARY" k
23a31 65 79 77 6f 72 64 20 6f 63 63 75 72 73 20 69 6e  eyword occurs in
23a32 20 62 65 74 77 65 65 6e 0a 2a 2a 20 43 52 45 41   between.** CREA
23a33 54 45 20 61 6e 64 20 54 41 42 4c 45 2e 0a 2a 2a  TE and TABLE..**
23a34 0a 2a 2a 20 54 68 65 20 6e 65 77 20 74 61 62 6c  .** The new tabl
23a35 65 20 72 65 63 6f 72 64 20 69 73 20 69 6e 69 74  e record is init
23a36 69 61 6c 69 7a 65 64 20 61 6e 64 20 70 75 74 20  ialized and put 
23a37 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  in pParse->pNewT
23a38 61 62 6c 65 2e 0a 2a 2a 20 41 73 20 6d 6f 72 65  able..** As more
23a39 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54   of the CREATE T
23a3a 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69  ABLE statement i
23a3b 73 20 70 61 72 73 65 64 2c 20 61 64 64 69 74 69  s parsed, additi
23a3c 6f 6e 61 6c 20 61 63 74 69 6f 6e 0a 2a 2a 20 72  onal action.** r
23a3d 6f 75 74 69 6e 65 73 20 77 69 6c 6c 20 62 65 20  outines will be 
23a3e 63 61 6c 6c 65 64 20 74 6f 20 61 64 64 20 6d 6f  called to add mo
23a3f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74  re information t
23a40 6f 20 74 68 69 73 20 72 65 63 6f 72 64 2e 0a 2a  o this record..*
23a41 2a 20 41 74 20 74 68 65 20 65 6e 64 20 6f 66 20  * At the end of 
23a42 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45  the CREATE TABLE
23a43 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 20   statement, the 
23a44 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28  sqlite3EndTable(
23a45 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20  ) routine.** is 
23a46 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 6c 65  called to comple
23a47 74 65 20 74 68 65 20 63 6f 6e 73 74 72 75 63 74  te the construct
23a48 69 6f 6e 20 6f 66 20 74 68 65 20 6e 65 77 20 74  ion of the new t
23a49 61 62 6c 65 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a  able record..*/.
23a4a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
23a4b 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61 72 74  oid sqlite3Start
23a4c 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a  Table(.  Parse *
23a4d 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 50 61 72  pParse,   /* Par
23a4e 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
23a4f 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20   Token *pName1, 
23a50 20 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74 20    /* First part 
23a51 6f 66 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  of the name of t
23a52 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77  he table or view
23a53 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
23a54 6d 65 32 2c 20 20 20 2f 2a 20 53 65 63 6f 6e 64  me2,   /* Second
23a55 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d   part of the nam
23a56 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f  e of the table o
23a57 72 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20  r view */.  int 
23a58 69 73 54 65 6d 70 2c 20 20 20 20 20 20 2f 2a 20  isTemp,      /* 
23a59 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20  True if this is 
23a5a 61 20 54 45 4d 50 20 74 61 62 6c 65 20 2a 2f 0a  a TEMP table */.
23a5b 20 20 69 6e 74 20 69 73 56 69 65 77 2c 20 20 20    int isView,   
23a5c 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
23a5d 69 73 20 69 73 20 61 20 56 49 45 57 20 2a 2f 0a  is is a VIEW */.
23a5e 20 20 69 6e 74 20 69 73 56 69 72 74 75 61 6c 2c    int isVirtual,
23a5f 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
23a60 69 73 20 69 73 20 61 20 56 49 52 54 55 41 4c 20  is is a VIRTUAL 
23a61 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  table */.  int n
23a62 6f 45 72 72 20 20 20 20 20 20 20 20 2f 2a 20 44  oErr        /* D
23a63 6f 20 6e 6f 74 68 69 6e 67 20 69 66 20 74 61 62  o nothing if tab
23a64 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  le already exist
23a65 73 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20  s */.){.  Table 
23a66 2a 70 54 61 62 6c 65 3b 0a 20 20 63 68 61 72 20  *pTable;.  char 
23a67 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 2f 2a 20 54  *zName = 0; /* T
23a68 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6e  he name of the n
23a69 65 77 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71  ew table */.  sq
23a6a 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
23a6b 73 65 2d 3e 64 62 3b 0a 20 20 56 64 62 65 20 2a  se->db;.  Vdbe *
23a6c 76 3b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  v;.  int iDb;   
23a6d 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
23a6e 65 20 6e 75 6d 62 65 72 20 74 6f 20 63 72 65 61  e number to crea
23a6f 74 65 20 74 68 65 20 74 61 62 6c 65 20 69 6e 20  te the table in 
23a70 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  */.  Token *pNam
23a71 65 3b 20 20 20 20 2f 2a 20 55 6e 71 75 61 6c 69  e;    /* Unquali
23a72 66 69 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65  fied name of the
23a73 20 74 61 62 6c 65 20 74 6f 20 63 72 65 61 74 65   table to create
23a74 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 74 61   */..  /* The ta
23a75 62 6c 65 20 6f 72 20 76 69 65 77 20 6e 61 6d 65  ble or view name
23a76 20 74 6f 20 63 72 65 61 74 65 20 69 73 20 70 61   to create is pa
23a77 73 73 65 64 20 74 6f 20 74 68 69 73 20 72 6f 75  ssed to this rou
23a78 74 69 6e 65 20 76 69 61 20 74 6f 6b 65 6e 73 0a  tine via tokens.
23a79 20 20 2a 2a 20 70 4e 61 6d 65 31 20 61 6e 64 20    ** pName1 and 
23a7a 70 4e 61 6d 65 32 2e 20 49 66 20 74 68 65 20 74  pName2. If the t
23a7b 61 62 6c 65 20 6e 61 6d 65 20 77 61 73 20 66 75  able name was fu
23a7c 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20 66  lly qualified, f
23a7d 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a  or example:.  **
23a7e 0a 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41 42  .  ** CREATE TAB
23a7f 4c 45 20 78 78 78 2e 79 79 79 20 28 2e 2e 2e 29  LE xxx.yyy (...)
23a80 3b 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 54 68 65  ;.  ** .  ** The
23a81 6e 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20  n pName1 is set 
23a82 74 6f 20 22 78 78 78 22 20 61 6e 64 20 70 4e 61  to "xxx" and pNa
23a83 6d 65 32 20 22 79 79 79 22 2e 20 4f 6e 20 74 68  me2 "yyy". On th
23a84 65 20 6f 74 68 65 72 20 68 61 6e 64 20 69 66 0a  e other hand if.
23a85 20 20 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 6e    ** the table n
23a86 61 6d 65 20 69 73 20 6e 6f 74 20 66 75 6c 6c 79  ame is not fully
23a87 20 71 75 61 6c 69 66 69 65 64 2c 20 69 2e 65 2e   qualified, i.e.
23a88 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 52 45 41  :.  **.  ** CREA
23a89 54 45 20 54 41 42 4c 45 20 79 79 79 28 2e 2e 2e  TE TABLE yyy(...
23a8a 29 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  );.  **.  ** The
23a8b 6e 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20  n pName1 is set 
23a8c 74 6f 20 22 79 79 79 22 20 61 6e 64 20 70 4e 61  to "yyy" and pNa
23a8d 6d 65 32 20 69 73 20 22 22 2e 0a 20 20 2a 2a 0a  me2 is ""..  **.
23a8e 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c 20 62 65    ** The call be
23a8f 6c 6f 77 20 73 65 74 73 20 74 68 65 20 70 4e 61  low sets the pNa
23a90 6d 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 6f  me pointer to po
23a91 69 6e 74 20 61 74 20 74 68 65 20 74 6f 6b 65 6e  int at the token
23a92 20 28 70 4e 61 6d 65 31 20 6f 72 0a 20 20 2a 2a   (pName1 or.  **
23a93 20 70 4e 61 6d 65 32 29 20 74 68 61 74 20 73 74   pName2) that st
23a94 6f 72 65 73 20 74 68 65 20 75 6e 71 75 61 6c 69  ores the unquali
23a95 66 69 65 64 20 74 61 62 6c 65 20 6e 61 6d 65 2e  fied table name.
23a96 20 54 68 65 20 76 61 72 69 61 62 6c 65 20 69 44   The variable iD
23a97 62 20 69 73 0a 20 20 2a 2a 20 73 65 74 20 74 6f  b is.  ** set to
23a98 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
23a99 65 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20  e database that 
23a9a 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65  the table or vie
23a9b 77 20 69 73 20 74 6f 20 62 65 0a 20 20 2a 2a 20  w is to be.  ** 
23a9c 63 72 65 61 74 65 64 20 69 6e 2e 0a 20 20 2a 2f  created in..  */
23a9d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
23a9e 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72  TwoPartName(pPar
23a9f 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d  se, pName1, pNam
23aa0 65 32 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 20 69  e2, &pName);.  i
23aa1 66 28 20 69 44 62 3c 30 20 29 20 72 65 74 75 72  f( iDb<0 ) retur
23aa2 6e 3b 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54  n;.  if( !OMIT_T
23aa3 45 4d 50 44 42 20 26 26 20 69 73 54 65 6d 70 20  EMPDB && isTemp 
23aa4 26 26 20 69 44 62 3e 31 20 29 7b 0a 20 20 20 20  && iDb>1 ){.    
23aa5 2f 2a 20 49 66 20 63 72 65 61 74 69 6e 67 20 61  /* If creating a
23aa6 20 74 65 6d 70 20 74 61 62 6c 65 2c 20 74 68 65   temp table, the
23aa7 20 6e 61 6d 65 20 6d 61 79 20 6e 6f 74 20 62 65   name may not be
23aa8 20 71 75 61 6c 69 66 69 65 64 20 2a 2f 0a 20 20   qualified */.  
23aa9 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
23aaa 67 28 70 50 61 72 73 65 2c 20 22 74 65 6d 70 6f  g(pParse, "tempo
23aab 72 61 72 79 20 74 61 62 6c 65 20 6e 61 6d 65 20  rary table name 
23aac 6d 75 73 74 20 62 65 20 75 6e 71 75 61 6c 69 66  must be unqualif
23aad 69 65 64 22 29 3b 0a 20 20 20 20 72 65 74 75 72  ied");.    retur
23aae 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 4f 4d  n;.  }.  if( !OM
23aaf 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 73 54  IT_TEMPDB && isT
23ab0 65 6d 70 20 29 20 69 44 62 20 3d 20 31 3b 0a 0a  emp ) iDb = 1;..
23ab1 20 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54    pParse->sNameT
23ab2 6f 6b 65 6e 20 3d 20 2a 70 4e 61 6d 65 3b 0a 20  oken = *pName;. 
23ab3 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   zName = sqlite3
23ab4 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
23ab5 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  , pName);.  if( 
23ab6 7a 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 75 72  zName==0 ) retur
23ab7 6e 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  n;.  if( SQLITE_
23ab8 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b  OK!=sqlite3Check
23ab9 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73  ObjectName(pPars
23aba 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20  e, zName) ){.   
23abb 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c   goto begin_tabl
23abc 65 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69  e_error;.  }.  i
23abd 66 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3d  f( db->init.iDb=
23abe 3d 31 20 29 20 69 73 54 65 6d 70 20 3d 20 31 3b  =1 ) isTemp = 1;
23abf 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
23ac0 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
23ac1 4f 4e 0a 20 20 61 73 73 65 72 74 28 20 28 69 73  ON.  assert( (is
23ac2 54 65 6d 70 20 26 20 31 29 3d 3d 69 73 54 65 6d  Temp & 1)==isTem
23ac3 70 20 29 3b 0a 20 20 7b 0a 20 20 20 20 69 6e 74  p );.  {.    int
23ac4 20 63 6f 64 65 3b 0a 20 20 20 20 63 68 61 72 20   code;.    char 
23ac5 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69  *zDb = db->aDb[i
23ac6 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 69  Db].zName;.    i
23ac7 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
23ac8 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
23ac9 54 45 5f 49 4e 53 45 52 54 2c 20 53 43 48 45 4d  TE_INSERT, SCHEM
23aca 41 5f 54 41 42 4c 45 28 69 73 54 65 6d 70 29 2c  A_TABLE(isTemp),
23acb 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20   0, zDb) ){.    
23acc 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62    goto begin_tab
23acd 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a  le_error;.    }.
23ace 20 20 20 20 69 66 28 20 69 73 56 69 65 77 20 29      if( isView )
23acf 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49  {.      if( !OMI
23ad0 54 5f 54 45 4d 50 44 42 20 26 26 20 69 73 54 65  T_TEMPDB && isTe
23ad1 6d 70 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f  mp ){.        co
23ad2 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41  de = SQLITE_CREA
23ad3 54 45 5f 54 45 4d 50 5f 56 49 45 57 3b 0a 20 20  TE_TEMP_VIEW;.  
23ad4 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
23ad5 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45     code = SQLITE
23ad6 5f 43 52 45 41 54 45 5f 56 49 45 57 3b 0a 20 20  _CREATE_VIEW;.  
23ad7 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
23ad8 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49 54  .      if( !OMIT
23ad9 5f 54 45 4d 50 44 42 20 26 26 20 69 73 54 65 6d  _TEMPDB && isTem
23ada 70 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 64  p ){.        cod
23adb 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54  e = SQLITE_CREAT
23adc 45 5f 54 45 4d 50 5f 54 41 42 4c 45 3b 0a 20 20  E_TEMP_TABLE;.  
23add 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
23ade 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45     code = SQLITE
23adf 5f 43 52 45 41 54 45 5f 54 41 42 4c 45 3b 0a 20  _CREATE_TABLE;. 
23ae0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
23ae1 20 69 66 28 20 21 69 73 56 69 72 74 75 61 6c 20   if( !isVirtual 
23ae2 26 26 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  && sqlite3AuthCh
23ae3 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65  eck(pParse, code
23ae4 2c 20 7a 4e 61 6d 65 2c 20 30 2c 20 7a 44 62 29  , zName, 0, zDb)
23ae5 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62   ){.      goto b
23ae6 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72  egin_table_error
23ae7 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
23ae8 69 66 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75  if..  /* Make su
23ae9 72 65 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  re the new table
23aea 20 6e 61 6d 65 20 64 6f 65 73 20 6e 6f 74 20 63   name does not c
23aeb 6f 6c 6c 69 64 65 20 77 69 74 68 20 61 6e 20 65  ollide with an e
23aec 78 69 73 74 69 6e 67 0a 20 20 2a 2a 20 69 6e 64  xisting.  ** ind
23aed 65 78 20 6f 72 20 74 61 62 6c 65 20 6e 61 6d 65  ex or table name
23aee 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74   in the same dat
23aef 61 62 61 73 65 2e 20 20 49 73 73 75 65 20 61 6e  abase.  Issue an
23af0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
23af1 66 0a 20 20 2a 2a 20 69 74 20 64 6f 65 73 2e 20  f.  ** it does. 
23af2 54 68 65 20 65 78 63 65 70 74 69 6f 6e 20 69 73  The exception is
23af3 20 69 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e   if the statemen
23af4 74 20 62 65 69 6e 67 20 70 61 72 73 65 64 20 77  t being parsed w
23af5 61 73 20 70 61 73 73 65 64 0a 20 20 2a 2a 20 74  as passed.  ** t
23af6 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 64 65 63  o an sqlite3_dec
23af7 6c 61 72 65 5f 76 74 61 62 28 29 20 63 61 6c 6c  lare_vtab() call
23af8 2e 20 49 6e 20 74 68 61 74 20 63 61 73 65 20 6f  . In that case o
23af9 6e 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e  nly the column n
23afa 61 6d 65 73 0a 20 20 2a 2a 20 61 6e 64 20 74 79  ames.  ** and ty
23afb 70 65 73 20 77 69 6c 6c 20 62 65 20 75 73 65 64  pes will be used
23afc 2c 20 73 6f 20 74 68 65 72 65 20 69 73 20 6e 6f  , so there is no
23afd 20 6e 65 65 64 20 74 6f 20 74 65 73 74 20 66 6f   need to test fo
23afe 72 20 6e 61 6d 65 73 70 61 63 65 0a 20 20 2a 2a  r namespace.  **
23aff 20 63 6f 6c 6c 69 73 69 6f 6e 73 2e 0a 20 20 2a   collisions..  *
23b00 2f 0a 20 20 69 66 28 20 21 49 4e 5f 44 45 43 4c  /.  if( !IN_DECL
23b01 41 52 45 5f 56 54 41 42 20 29 7b 0a 20 20 20 20  ARE_VTAB ){.    
23b02 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
23b03 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
23b04 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20  (pParse) ){.    
23b05 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62    goto begin_tab
23b06 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a  le_error;.    }.
23b07 20 20 20 20 70 54 61 62 6c 65 20 3d 20 73 71 6c      pTable = sql
23b08 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62  ite3FindTable(db
23b09 2c 20 7a 4e 61 6d 65 2c 20 64 62 2d 3e 61 44 62  , zName, db->aDb
23b0a 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20  [iDb].zName);.  
23b0b 20 20 69 66 28 20 70 54 61 62 6c 65 20 29 7b 0a    if( pTable ){.
23b0c 20 20 20 20 20 20 69 66 28 20 21 6e 6f 45 72 72        if( !noErr
23b0d 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
23b0e 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
23b0f 73 65 2c 20 22 74 61 62 6c 65 20 25 54 20 61 6c  se, "table %T al
23b10 72 65 61 64 79 20 65 78 69 73 74 73 22 2c 20 70  ready exists", p
23b11 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Name);.      }. 
23b12 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f       goto begin_
23b13 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20  table_error;.   
23b14 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74   }.    if( sqlit
23b15 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20  e3FindIndex(db, 
23b16 7a 4e 61 6d 65 2c 20 30 29 21 3d 30 20 26 26 20  zName, 0)!=0 && 
23b17 28 69 44 62 3d 3d 30 20 7c 7c 20 21 64 62 2d 3e  (iDb==0 || !db->
23b18 69 6e 69 74 2e 62 75 73 79 29 20 29 7b 0a 20 20  init.busy) ){.  
23b19 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
23b1a 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 68 65  Msg(pParse, "the
23b1b 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 6e  re is already an
23b1c 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 25 73 22   index named %s"
23b1d 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  , zName);.      
23b1e 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65  goto begin_table
23b1f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20  _error;.    }.  
23b20 7d 0a 0a 20 20 70 54 61 62 6c 65 20 3d 20 73 71  }..  pTable = sq
23b21 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
23b22 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 61 62  o(db, sizeof(Tab
23b23 6c 65 29 29 3b 0a 20 20 69 66 28 20 70 54 61 62  le));.  if( pTab
23b24 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d  le==0 ){.    db-
23b25 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
23b26 31 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72  1;.    pParse->r
23b27 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
23b28 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45  ;.    pParse->nE
23b29 72 72 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 62  rr++;.    goto b
23b2a 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72  egin_table_error
23b2b 3b 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e  ;.  }.  pTable->
23b2c 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20  zName = zName;. 
23b2d 20 70 54 61 62 6c 65 2d 3e 69 50 4b 65 79 20 3d   pTable->iPKey =
23b2e 20 2d 31 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 70   -1;.  pTable->p
23b2f 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62  Schema = db->aDb
23b30 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 0a 20  [iDb].pSchema;. 
23b31 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 20 3d 20   pTable->nRef = 
23b32 31 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 64 62 4d  1;.  pTable->dbM
23b33 65 6d 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  em = 0;.  assert
23b34 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  ( pParse->pNewTa
23b35 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 70 50 61 72  ble==0 );.  pPar
23b36 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20  se->pNewTable = 
23b37 70 54 61 62 6c 65 3b 0a 0a 20 20 2f 2a 20 49 66  pTable;..  /* If
23b38 20 74 68 69 73 20 69 73 20 74 68 65 20 6d 61 67   this is the mag
23b39 69 63 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  ic sqlite_sequen
23b3a 63 65 20 74 61 62 6c 65 20 75 73 65 64 20 62 79  ce table used by
23b3b 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 2c 0a   autoincrement,.
23b3c 20 20 2a 2a 20 74 68 65 6e 20 72 65 63 6f 72 64    ** then record
23b3d 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
23b3e 69 73 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20  is table in the 
23b3f 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74  main database st
23b40 72 75 63 74 75 72 65 0a 20 20 2a 2a 20 73 6f 20  ructure.  ** so 
23b41 74 68 61 74 20 49 4e 53 45 52 54 20 63 61 6e 20  that INSERT can 
23b42 66 69 6e 64 20 74 68 65 20 74 61 62 6c 65 20 65  find the table e
23b43 61 73 69 6c 79 2e 0a 20 20 2a 2f 0a 23 69 66 6e  asily..  */.#ifn
23b44 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
23b45 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20  AUTOINCREMENT.  
23b46 69 66 28 20 21 70 50 61 72 73 65 2d 3e 6e 65 73  if( !pParse->nes
23b47 74 65 64 20 26 26 20 73 74 72 63 6d 70 28 7a 4e  ted && strcmp(zN
23b48 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 73 65 71  ame, "sqlite_seq
23b49 75 65 6e 63 65 22 29 3d 3d 30 20 29 7b 0a 20 20  uence")==0 ){.  
23b4a 20 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d    pTable->pSchem
23b4b 61 2d 3e 70 53 65 71 54 61 62 20 3d 20 70 54 61  a->pSeqTab = pTa
23b4c 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ble;.  }.#endif.
23b4d 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67 65 6e 65  .  /* Begin gene
23b4e 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64 65 20  rating the code 
23b4f 74 68 61 74 20 77 69 6c 6c 20 69 6e 73 65 72 74  that will insert
23b50 20 74 68 65 20 74 61 62 6c 65 20 72 65 63 6f 72   the table recor
23b51 64 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20  d into.  ** the 
23b52 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61  SQLITE_MASTER ta
23b53 62 6c 65 2e 20 20 4e 6f 74 65 20 69 6e 20 70 61  ble.  Note in pa
23b54 72 74 69 63 75 6c 61 72 20 74 68 61 74 20 77 65  rticular that we
23b55 20 6d 75 73 74 20 67 6f 20 61 68 65 61 64 0a 20   must go ahead. 
23b56 20 2a 2a 20 61 6e 64 20 61 6c 6c 6f 63 61 74 65   ** and allocate
23b57 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62   the record numb
23b58 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  er for the table
23b59 20 65 6e 74 72 79 20 6e 6f 77 2e 20 20 42 65 66   entry now.  Bef
23b5a 6f 72 65 20 61 6e 79 0a 20 20 2a 2a 20 50 52 49  ore any.  ** PRI
23b5b 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51  MARY KEY or UNIQ
23b5c 55 45 20 6b 65 79 77 6f 72 64 73 20 61 72 65 20  UE keywords are 
23b5d 70 61 72 73 65 64 2e 20 20 54 68 6f 73 65 20 6b  parsed.  Those k
23b5e 65 79 77 6f 72 64 73 20 77 69 6c 6c 20 63 61 75  eywords will cau
23b5f 73 65 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 20  se.  ** indices 
23b60 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 61 6e  to be created an
23b61 64 20 74 68 65 20 74 61 62 6c 65 20 72 65 63 6f  d the table reco
23b62 72 64 20 6d 75 73 74 20 63 6f 6d 65 20 62 65 66  rd must come bef
23b63 6f 72 65 20 74 68 65 20 0a 20 20 2a 2a 20 69 6e  ore the .  ** in
23b64 64 69 63 65 73 2e 20 20 48 65 6e 63 65 2c 20 74  dices.  Hence, t
23b65 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  he record number
23b66 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 6d   for the table m
23b67 75 73 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64  ust be allocated
23b68 0a 20 20 2a 2a 20 6e 6f 77 2e 0a 20 20 2a 2f 0a  .  ** now..  */.
23b69 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e    if( !db->init.
23b6a 62 75 73 79 20 26 26 20 28 76 20 3d 20 73 71 6c  busy && (v = sql
23b6b 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
23b6c 73 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69  se))!=0 ){.    i
23b6d 6e 74 20 6a 31 3b 0a 20 20 20 20 69 6e 74 20 66  nt j1;.    int f
23b6e 69 6c 65 46 6f 72 6d 61 74 3b 0a 20 20 20 20 69  ileFormat;.    i
23b6f 6e 74 20 72 65 67 31 2c 20 72 65 67 32 2c 20 72  nt reg1, reg2, r
23b70 65 67 33 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  eg3;.    sqlite3
23b71 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74  BeginWriteOperat
23b72 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69  ion(pParse, 0, i
23b73 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  Db);..#ifndef SQ
23b74 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
23b75 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20 69  LTABLE.    if( i
23b76 73 56 69 72 74 75 61 6c 20 29 7b 0a 20 20 20 20  sVirtual ){.    
23b77 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
23b78 4f 70 30 28 76 2c 20 4f 50 5f 56 42 65 67 69 6e  Op0(v, OP_VBegin
23b79 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
23b7a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66  .    /* If the f
23b7b 69 6c 65 20 66 6f 72 6d 61 74 20 61 6e 64 20 65  ile format and e
23b7c 6e 63 6f 64 69 6e 67 20 69 6e 20 74 68 65 20 64  ncoding in the d
23b7d 61 74 61 62 61 73 65 20 68 61 76 65 20 6e 6f 74  atabase have not
23b7e 20 62 65 65 6e 20 73 65 74 2c 20 0a 20 20 20 20   been set, .    
23b7f 2a 2a 20 73 65 74 20 74 68 65 6d 20 6e 6f 77 2e  ** set them now.
23b80 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 67 31  .    */.    reg1
23b81 20 3d 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f   = pParse->regRo
23b82 77 69 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  wid = ++pParse->
23b83 6e 4d 65 6d 3b 0a 20 20 20 20 72 65 67 32 20 3d  nMem;.    reg2 =
23b84 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74   pParse->regRoot
23b85 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
23b86 6d 3b 0a 20 20 20 20 72 65 67 33 20 3d 20 2b 2b  m;.    reg3 = ++
23b87 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
23b88 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
23b89 4f 70 33 28 76 2c 20 4f 50 5f 52 65 61 64 43 6f  Op3(v, OP_ReadCo
23b8a 6f 6b 69 65 2c 20 69 44 62 2c 20 72 65 67 33 2c  okie, iDb, reg3,
23b8b 20 42 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d   BTREE_FILE_FORM
23b8c 41 54 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  AT);.    sqlite3
23b8d 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c  VdbeUsesBtree(v,
23b8e 20 69 44 62 29 3b 0a 20 20 20 20 6a 31 20 3d 20   iDb);.    j1 = 
23b8f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23b90 31 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 33  1(v, OP_If, reg3
23b91 29 3b 0a 20 20 20 20 66 69 6c 65 46 6f 72 6d 61  );.    fileForma
23b92 74 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 26  t = (db->flags &
23b93 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69   SQLITE_LegacyFi
23b94 6c 65 46 6d 74 29 21 3d 30 20 3f 0a 20 20 20 20  leFmt)!=0 ?.    
23b95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20                1 
23b96 3a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c  : SQLITE_MAX_FIL
23b97 45 5f 46 4f 52 4d 41 54 3b 0a 20 20 20 20 73 71  E_FORMAT;.    sq
23b98 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
23b99 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 66  v, OP_Integer, f
23b9a 69 6c 65 46 6f 72 6d 61 74 2c 20 72 65 67 33 29  ileFormat, reg3)
23b9b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
23b9c 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65  eAddOp3(v, OP_Se
23b9d 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 42 54  tCookie, iDb, BT
23b9e 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 2c  REE_FILE_FORMAT,
23b9f 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69   reg3);.    sqli
23ba0 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
23ba1 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 45 4e 43   OP_Integer, ENC
23ba2 28 64 62 29 2c 20 72 65 67 33 29 3b 0a 20 20 20  (db), reg3);.   
23ba3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
23ba4 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b  p3(v, OP_SetCook
23ba5 69 65 2c 20 69 44 62 2c 20 42 54 52 45 45 5f 54  ie, iDb, BTREE_T
23ba6 45 58 54 5f 45 4e 43 4f 44 49 4e 47 2c 20 72 65  EXT_ENCODING, re
23ba7 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  g3);.    sqlite3
23ba8 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
23ba9 6a 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69  j1);..    /* Thi
23baa 73 20 6a 75 73 74 20 63 72 65 61 74 65 73 20 61  s just creates a
23bab 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72 20 72 65   place-holder re
23bac 63 6f 72 64 20 69 6e 20 74 68 65 20 73 71 6c 69  cord in the sqli
23bad 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e  te_master table.
23bae 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65 63 6f  .    ** The reco
23baf 72 64 20 63 72 65 61 74 65 64 20 64 6f 65 73 20  rd created does 
23bb0 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 79 74  not contain anyt
23bb1 68 69 6e 67 20 79 65 74 2e 20 20 49 74 20 77 69  hing yet.  It wi
23bb2 6c 6c 20 62 65 20 72 65 70 6c 61 63 65 64 0a 20  ll be replaced. 
23bb3 20 20 20 2a 2a 20 62 79 20 74 68 65 20 72 65 61     ** by the rea
23bb4 6c 20 65 6e 74 72 79 20 69 6e 20 63 6f 64 65 20  l entry in code 
23bb5 67 65 6e 65 72 61 74 65 64 20 61 74 20 73 71 6c  generated at sql
23bb6 69 74 65 33 45 6e 64 54 61 62 6c 65 28 29 2e 0a  ite3EndTable()..
23bb7 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
23bb8 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20  e rowid for the 
23bb9 6e 65 77 20 65 6e 74 72 79 20 69 73 20 6c 65 66  new entry is lef
23bba 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 50  t in register pP
23bbb 61 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 2e 0a  arse->regRowid..
23bbc 20 20 20 20 2a 2a 20 54 68 65 20 72 6f 6f 74 20      ** The root 
23bbd 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74  page number of t
23bbe 68 65 20 6e 65 77 20 74 61 62 6c 65 20 69 73 20  he new table is 
23bbf 6c 65 66 74 20 69 6e 20 72 65 67 20 70 50 61 72  left in reg pPar
23bc0 73 65 2d 3e 72 65 67 52 6f 6f 74 2e 0a 20 20 20  se->regRoot..   
23bc1 20 2a 2a 20 54 68 65 20 72 6f 77 69 64 20 61 6e   ** The rowid an
23bc2 64 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62  d root page numb
23bc3 65 72 20 76 61 6c 75 65 73 20 61 72 65 20 6e 65  er values are ne
23bc4 65 64 65 64 20 62 79 20 74 68 65 20 63 6f 64 65  eded by the code
23bc5 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 73 71 6c   that.    ** sql
23bc6 69 74 65 33 45 6e 64 54 61 62 6c 65 20 77 69 6c  ite3EndTable wil
23bc7 6c 20 67 65 6e 65 72 61 74 65 2e 0a 20 20 20 20  l generate..    
23bc8 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
23bc9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
23bca 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
23bcb 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
23bcc 4c 54 41 42 4c 45 29 0a 20 20 20 20 69 66 28 20  LTABLE).    if( 
23bcd 69 73 56 69 65 77 20 7c 7c 20 69 73 56 69 72 74  isView || isVirt
23bce 75 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ual ){.      sql
23bcf 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
23bd0 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
23bd1 20 72 65 67 32 29 3b 0a 20 20 20 20 7d 65 6c 73   reg2);.    }els
23bd2 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20  e.#endif.    {. 
23bd3 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23bd4 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 72 65  AddOp2(v, OP_Cre
23bd5 61 74 65 54 61 62 6c 65 2c 20 69 44 62 2c 20 72  ateTable, iDb, r
23bd6 65 67 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  eg2);.    }.    
23bd7 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74 65  sqlite3OpenMaste
23bd8 72 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69  rTable(pParse, i
23bd9 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Db);.    sqlite3
23bda 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
23bdb 5f 4e 65 77 52 6f 77 69 64 2c 20 30 2c 20 72 65  _NewRowid, 0, re
23bdc 67 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  g1);.    sqlite3
23bdd 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
23bde 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 33 29 3b  _Null, 0, reg3);
23bdf 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
23be0 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73  AddOp3(v, OP_Ins
23be1 65 72 74 2c 20 30 2c 20 72 65 67 33 2c 20 72 65  ert, 0, reg3, re
23be2 67 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  g1);.    sqlite3
23be3 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
23be4 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a  OPFLAG_APPEND);.
23be5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23be6 64 64 4f 70 30 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp0(v, OP_Clos
23be7 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f  e);.  }..  /* No
23be8 72 6d 61 6c 20 28 6e 6f 6e 2d 65 72 72 6f 72 29  rmal (non-error)
23be9 20 72 65 74 75 72 6e 2e 20 2a 2f 0a 20 20 72 65   return. */.  re
23bea 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49 66 20 61  turn;..  /* If a
23beb 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
23bec 77 65 20 6a 75 6d 70 20 68 65 72 65 20 2a 2f 0a  we jump here */.
23bed 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f  begin_table_erro
23bee 72 3a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  r:.  sqlite3DbFr
23bef 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20  ee(db, zName);. 
23bf0 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a   return;.}../*.*
23bf1 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 69 73 20  * This macro is 
23bf2 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 65 20  used to compare 
23bf3 74 77 6f 20 73 74 72 69 6e 67 73 20 69 6e 20 61  two strings in a
23bf4 20 63 61 73 65 2d 69 6e 73 65 6e 73 69 74 69 76   case-insensitiv
23bf5 65 20 6d 61 6e 6e 65 72 2e 0a 2a 2a 20 49 74 20  e manner..** It 
23bf6 69 73 20 73 6c 69 67 68 74 6c 79 20 66 61 73 74  is slightly fast
23bf7 65 72 20 74 68 61 6e 20 63 61 6c 6c 69 6e 67 20  er than calling 
23bf8 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 29  sqlite3StrICmp()
23bf9 20 64 69 72 65 63 74 6c 79 2c 20 62 75 74 0a 2a   directly, but.*
23bfa 2a 20 70 72 6f 64 75 63 65 73 20 6c 61 72 67 65  * produces large
23bfb 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 57 41  r code..**.** WA
23bfc 52 4e 49 4e 47 3a 20 54 68 69 73 20 6d 61 63 72  RNING: This macr
23bfd 6f 20 69 73 20 6e 6f 74 20 63 6f 6d 70 61 74 69  o is not compati
23bfe 62 6c 65 20 77 69 74 68 20 74 68 65 20 73 74 72  ble with the str
23bff 63 6d 70 28 29 20 66 61 6d 69 6c 79 2e 20 49 74  cmp() family. It
23c00 0a 2a 2a 20 72 65 74 75 72 6e 73 20 74 72 75 65  .** returns true
23c01 20 69 66 20 74 68 65 20 74 77 6f 20 73 74 72 69   if the two stri
23c02 6e 67 73 20 61 72 65 20 65 71 75 61 6c 2c 20 6f  ngs are equal, o
23c03 74 68 65 72 77 69 73 65 20 66 61 6c 73 65 2e 0a  therwise false..
23c04 2a 2f 0a 23 64 65 66 69 6e 65 20 53 54 52 49 43  */.#define STRIC
23c05 4d 50 28 78 2c 20 79 29 20 28 5c 0a 73 71 6c 69  MP(x, y) (\.sqli
23c06 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
23c07 2a 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  *(unsigned char 
23c08 2a 29 28 78 29 5d 3d 3d 20 20 20 5c 0a 73 71 6c  *)(x)]==   \.sql
23c09 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72  ite3UpperToLower
23c0a 5b 2a 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  [*(unsigned char
23c0b 20 2a 29 28 79 29 5d 20 20 20 20 20 5c 0a 26 26   *)(y)]     \.&&
23c0c 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
23c0d 28 78 29 2b 31 2c 28 79 29 2b 31 29 3d 3d 30 20  (x)+1,(y)+1)==0 
23c0e 29 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e  )../*.** Add a n
23c0f 65 77 20 63 6f 6c 75 6d 6e 20 74 6f 20 74 68 65  ew column to the
23c10 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79   table currently
23c11 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74   being construct
23c12 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61  ed..**.** The pa
23c13 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  rser calls this 
23c14 72 6f 75 74 69 6e 65 20 6f 6e 63 65 20 66 6f 72  routine once for
23c15 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 64 65 63   each column dec
23c16 6c 61 72 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20 61  laration.** in a
23c17 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
23c18 61 74 65 6d 65 6e 74 2e 20 20 73 71 6c 69 74 65  atement.  sqlite
23c19 33 53 74 61 72 74 54 61 62 6c 65 28 29 20 67 65  3StartTable() ge
23c1a 74 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 66 69 72  ts called.** fir
23c1b 73 74 20 74 6f 20 67 65 74 20 74 68 69 6e 67 73  st to get things
23c1c 20 67 6f 69 6e 67 2e 20 20 54 68 65 6e 20 74 68   going.  Then th
23c1d 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
23c1e 6c 6c 65 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a  lled for each.**
23c1f 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49   column..*/.SQLI
23c20 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
23c21 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e  sqlite3AddColumn
23c22 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
23c23 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20  Token *pName){. 
23c24 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74   Table *p;.  int
23c25 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20   i;.  char *z;. 
23c26 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20   Column *pCol;. 
23c27 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
23c28 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
23c29 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e   (p = pParse->pN
23c2a 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65  ewTable)==0 ) re
23c2b 74 75 72 6e 3b 0a 23 69 66 20 53 51 4c 49 54 45  turn;.#if SQLITE
23c2c 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66  _MAX_COLUMN.  if
23c2d 28 20 70 2d 3e 6e 43 6f 6c 2b 31 3e 64 62 2d 3e  ( p->nCol+1>db->
23c2e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
23c2f 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20  MIT_COLUMN] ){. 
23c30 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
23c31 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20  sg(pParse, "too 
23c32 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 6f 6e 20  many columns on 
23c33 25 73 22 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0a  %s", p->zName);.
23c34 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
23c35 23 65 6e 64 69 66 0a 20 20 7a 20 3d 20 73 71 6c  #endif.  z = sql
23c36 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
23c37 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20  n(db, pName);.  
23c38 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72  if( z==0 ) retur
23c39 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  n;.  for(i=0; i<
23c3a 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20  p->nCol; i++){. 
23c3b 20 20 20 69 66 28 20 53 54 52 49 43 4d 50 28 7a     if( STRICMP(z
23c3c 2c 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61  , p->aCol[i].zNa
23c3d 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  me) ){.      sql
23c3e 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
23c3f 72 73 65 2c 20 22 64 75 70 6c 69 63 61 74 65 20  rse, "duplicate 
23c40 63 6f 6c 75 6d 6e 20 6e 61 6d 65 3a 20 25 73 22  column name: %s"
23c41 2c 20 7a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , z);.      sqli
23c42 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29  te3DbFree(db, z)
23c43 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
23c44 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
23c45 28 70 2d 3e 6e 43 6f 6c 20 26 20 30 78 37 29 3d  (p->nCol & 0x7)=
23c46 3d 30 20 29 7b 0a 20 20 20 20 43 6f 6c 75 6d 6e  =0 ){.    Column
23c47 20 2a 61 4e 65 77 3b 0a 20 20 20 20 61 4e 65 77   *aNew;.    aNew
23c48 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
23c49 6c 6f 63 28 64 62 2c 70 2d 3e 61 43 6f 6c 2c 28  loc(db,p->aCol,(
23c4a 70 2d 3e 6e 43 6f 6c 2b 38 29 2a 73 69 7a 65 6f  p->nCol+8)*sizeo
23c4b 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 3b 0a  f(p->aCol[0]));.
23c4c 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20      if( aNew==0 
23c4d 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
23c4e 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20  DbFree(db, z);. 
23c4f 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
23c50 20 7d 0a 20 20 20 20 70 2d 3e 61 43 6f 6c 20 3d   }.    p->aCol =
23c51 20 61 4e 65 77 3b 0a 20 20 7d 0a 20 20 70 43 6f   aNew;.  }.  pCo
23c52 6c 20 3d 20 26 70 2d 3e 61 43 6f 6c 5b 70 2d 3e  l = &p->aCol[p->
23c53 6e 43 6f 6c 5d 3b 0a 20 20 6d 65 6d 73 65 74 28  nCol];.  memset(
23c54 70 43 6f 6c 2c 20 30 2c 20 73 69 7a 65 6f 66 28  pCol, 0, sizeof(
23c55 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 3b 0a 20 20  p->aCol[0]));.  
23c56 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 3b  pCol->zName = z;
23c57 0a 20 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  . .  /* If there
23c58 20 69 73 20 6e 6f 20 74 79 70 65 20 73 70 65 63   is no type spec
23c59 69 66 69 65 64 2c 20 63 6f 6c 75 6d 6e 73 20 68  ified, columns h
23c5a 61 76 65 20 74 68 65 20 64 65 66 61 75 6c 74 20  ave the default 
23c5b 61 66 66 69 6e 69 74 79 0a 20 20 2a 2a 20 27 4e  affinity.  ** 'N
23c5c 4f 4e 45 27 2e 20 49 66 20 74 68 65 72 65 20 69  ONE'. If there i
23c5d 73 20 61 20 74 79 70 65 20 73 70 65 63 69 66 69  s a type specifi
23c5e 65 64 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 33  ed, then sqlite3
23c5f 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 29 20  AddColumnType() 
23c60 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 63 61 6c  will.  ** be cal
23c61 6c 65 64 20 6e 65 78 74 20 74 6f 20 73 65 74 20  led next to set 
23c62 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 63  pCol->affinity c
23c63 6f 72 72 65 63 74 6c 79 2e 0a 20 20 2a 2f 0a 20  orrectly..  */. 
23c64 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20   pCol->affinity 
23c65 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  = SQLITE_AFF_NON
23c66 45 3b 0a 20 20 70 2d 3e 6e 43 6f 6c 2b 2b 3b 0a  E;.  p->nCol++;.
23c67 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
23c68 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
23c69 62 79 20 74 68 65 20 70 61 72 73 65 72 20 77 68  by the parser wh
23c6a 69 6c 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  ile in the middl
23c6b 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e 67 20  e of.** parsing 
23c6c 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  a CREATE TABLE s
23c6d 74 61 74 65 6d 65 6e 74 2e 20 20 41 20 22 4e 4f  tatement.  A "NO
23c6e 54 20 4e 55 4c 4c 22 20 63 6f 6e 73 74 72 61 69  T NULL" constrai
23c6f 6e 74 20 68 61 73 0a 2a 2a 20 62 65 65 6e 20 73  nt has.** been s
23c70 65 65 6e 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 2e  een on a column.
23c71 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73    This routine s
23c72 65 74 73 20 74 68 65 20 6e 6f 74 4e 75 6c 6c 20  ets the notNull 
23c73 66 6c 61 67 20 6f 6e 0a 2a 2a 20 74 68 65 20 63  flag on.** the c
23c74 6f 6c 75 6d 6e 20 63 75 72 72 65 6e 74 6c 79 20  olumn currently 
23c75 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69  under constructi
23c76 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
23c77 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
23c78 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72  e3AddNotNull(Par
23c79 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
23c7a 6f 6e 45 72 72 6f 72 29 7b 0a 20 20 54 61 62 6c  onError){.  Tabl
23c7b 65 20 2a 70 3b 0a 20 20 70 20 3d 20 70 50 61 72  e *p;.  p = pPar
23c7c 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20  se->pNewTable;. 
23c7d 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 4e 45 56   if( p==0 || NEV
23c7e 45 52 28 70 2d 3e 6e 43 6f 6c 3c 31 29 20 29 20  ER(p->nCol<1) ) 
23c7f 72 65 74 75 72 6e 3b 0a 20 20 70 2d 3e 61 43 6f  return;.  p->aCo
23c80 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d 2e 6e 6f 74  l[p->nCol-1].not
23c81 4e 75 6c 6c 20 3d 20 28 75 38 29 6f 6e 45 72 72  Null = (u8)onErr
23c82 6f 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 63 61  or;.}../*.** Sca
23c83 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 79 70  n the column typ
23c84 65 20 6e 61 6d 65 20 7a 54 79 70 65 20 28 6c 65  e name zType (le
23c85 6e 67 74 68 20 6e 54 79 70 65 29 20 61 6e 64 20  ngth nType) and 
23c86 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 61 73  return the.** as
23c87 73 6f 63 69 61 74 65 64 20 61 66 66 69 6e 69 74  sociated affinit
23c88 79 20 74 79 70 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  y type..**.** Th
23c89 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20  is routine does 
23c8a 61 20 63 61 73 65 2d 69 6e 64 65 70 65 6e 64 65  a case-independe
23c8b 6e 74 20 73 65 61 72 63 68 20 6f 66 20 7a 54 79  nt search of zTy
23c8c 70 65 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 73  pe for the .** s
23c8d 75 62 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65  ubstrings in the
23c8e 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65   following table
23c8f 2e 20 49 66 20 6f 6e 65 20 6f 66 20 74 68 65 20  . If one of the 
23c90 73 75 62 73 74 72 69 6e 67 73 20 69 73 0a 2a 2a  substrings is.**
23c91 20 66 6f 75 6e 64 2c 20 74 68 65 20 63 6f 72 72   found, the corr
23c92 65 73 70 6f 6e 64 69 6e 67 20 61 66 66 69 6e 69  esponding affini
23c93 74 79 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  ty is returned. 
23c94 49 66 20 7a 54 79 70 65 20 63 6f 6e 74 61 69 6e  If zType contain
23c95 73 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f  s.** more than o
23c96 6e 65 20 6f 66 20 74 68 65 20 73 75 62 73 74 72  ne of the substr
23c97 69 6e 67 73 2c 20 65 6e 74 72 69 65 73 20 74 6f  ings, entries to
23c98 77 61 72 64 20 74 68 65 20 74 6f 70 20 6f 66 20  ward the top of 
23c99 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 74 61  .** the table ta
23c9a 6b 65 20 70 72 69 6f 72 69 74 79 2e 20 46 6f 72  ke priority. For
23c9b 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 7a 54 79   example, if zTy
23c9c 70 65 20 69 73 20 27 42 4c 4f 42 49 4e 54 27 2c  pe is 'BLOBINT',
23c9d 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f   .** SQLITE_AFF_
23c9e 49 4e 54 45 47 45 52 20 69 73 20 72 65 74 75 72  INTEGER is retur
23c9f 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 75 62 73 74  ned..**.** Subst
23ca0 72 69 6e 67 20 20 20 20 20 7c 20 41 66 66 69 6e  ring     | Affin
23ca1 69 74 79 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  ity.** ---------
23ca2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
23ca3 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 27 49 4e 54 27  -------.** 'INT'
23ca4 20 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54           | SQLIT
23ca5 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 0a 2a 2a  E_AFF_INTEGER.**
23ca6 20 27 43 48 41 52 27 20 20 20 20 20 20 20 20 7c   'CHAR'        |
23ca7 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
23ca8 0a 2a 2a 20 27 43 4c 4f 42 27 20 20 20 20 20 20  .** 'CLOB'      
23ca9 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54    | SQLITE_AFF_T
23caa 45 58 54 0a 2a 2a 20 27 54 45 58 54 27 20 20 20  EXT.** 'TEXT'   
23cab 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46       | SQLITE_AF
23cac 46 5f 54 45 58 54 0a 2a 2a 20 27 42 4c 4f 42 27  F_TEXT.** 'BLOB'
23cad 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45          | SQLITE
23cae 5f 41 46 46 5f 4e 4f 4e 45 0a 2a 2a 20 27 52 45  _AFF_NONE.** 'RE
23caf 41 4c 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c  AL'        | SQL
23cb0 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 20  ITE_AFF_REAL.** 
23cb1 27 46 4c 4f 41 27 20 20 20 20 20 20 20 20 7c 20  'FLOA'        | 
23cb2 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a  SQLITE_AFF_REAL.
23cb3 2a 2a 20 27 44 4f 55 42 27 20 20 20 20 20 20 20  ** 'DOUB'       
23cb4 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45   | SQLITE_AFF_RE
23cb5 41 4c 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 6e 65  AL.**.** If none
23cb6 20 6f 66 20 74 68 65 20 73 75 62 73 74 72 69 6e   of the substrin
23cb7 67 73 20 69 6e 20 74 68 65 20 61 62 6f 76 65 20  gs in the above 
23cb8 74 61 62 6c 65 20 61 72 65 20 66 6f 75 6e 64 2c  table are found,
23cb9 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  .** SQLITE_AFF_N
23cba 55 4d 45 52 49 43 20 69 73 20 72 65 74 75 72 6e  UMERIC is return
23cbb 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
23cbc 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74  IVATE char sqlit
23cbd 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 63  e3AffinityType(c
23cbe 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 29 7b  onst char *zIn){
23cbf 0a 20 20 75 33 32 20 68 20 3d 20 30 3b 0a 20 20  .  u32 h = 0;.  
23cc0 63 68 61 72 20 61 66 66 20 3d 20 53 51 4c 49 54  char aff = SQLIT
23cc1 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 0a  E_AFF_NUMERIC;..
23cc2 20 20 69 66 28 20 7a 49 6e 20 29 20 77 68 69 6c    if( zIn ) whil
23cc3 65 28 20 7a 49 6e 5b 30 5d 20 29 7b 0a 20 20 20  e( zIn[0] ){.   
23cc4 20 68 20 3d 20 28 68 3c 3c 38 29 20 2b 20 73 71   h = (h<<8) + sq
23cc5 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
23cc6 72 5b 28 2a 7a 49 6e 29 26 30 78 66 66 5d 3b 0a  r[(*zIn)&0xff];.
23cc7 20 20 20 20 7a 49 6e 2b 2b 3b 0a 20 20 20 20 69      zIn++;.    i
23cc8 66 28 20 68 3d 3d 28 28 27 63 27 3c 3c 32 34 29  f( h==(('c'<<24)
23cc9 2b 28 27 68 27 3c 3c 31 36 29 2b 28 27 61 27 3c  +('h'<<16)+('a'<
23cca 3c 38 29 2b 27 72 27 29 20 29 7b 20 20 20 20 20  <8)+'r') ){     
23ccb 20 20 20 20 20 20 20 20 2f 2a 20 43 48 41 52 20          /* CHAR 
23ccc 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53  */.      aff = S
23ccd 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 20  QLITE_AFF_TEXT; 
23cce 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68  .    }else if( h
23ccf 3d 3d 28 28 27 63 27 3c 3c 32 34 29 2b 28 27 6c  ==(('c'<<24)+('l
23cd0 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b  '<<16)+('o'<<8)+
23cd1 27 62 27 29 20 29 7b 20 20 20 20 20 20 20 2f 2a  'b') ){       /*
23cd2 20 43 4c 4f 42 20 2a 2f 0a 20 20 20 20 20 20 61   CLOB */.      a
23cd3 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  ff = SQLITE_AFF_
23cd4 54 45 58 54 3b 0a 20 20 20 20 7d 65 6c 73 65 20  TEXT;.    }else 
23cd5 69 66 28 20 68 3d 3d 28 28 27 74 27 3c 3c 32 34  if( h==(('t'<<24
23cd6 29 2b 28 27 65 27 3c 3c 31 36 29 2b 28 27 78 27  )+('e'<<16)+('x'
23cd7 3c 3c 38 29 2b 27 74 27 29 20 29 7b 20 20 20 20  <<8)+'t') ){    
23cd8 20 20 20 2f 2a 20 54 45 58 54 20 2a 2f 0a 20 20     /* TEXT */.  
23cd9 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45      aff = SQLITE
23cda 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 7d  _AFF_TEXT;.    }
23cdb 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 62  else if( h==(('b
23cdc 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29  '<<24)+('l'<<16)
23cdd 2b 28 27 6f 27 3c 3c 38 29 2b 27 62 27 29 20 20  +('o'<<8)+'b')  
23cde 20 20 20 20 20 20 20 20 2f 2a 20 42 4c 4f 42 20          /* BLOB 
23cdf 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 20 28 61  */.        && (a
23ce0 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ff==SQLITE_AFF_N
23ce1 55 4d 45 52 49 43 20 7c 7c 20 61 66 66 3d 3d 53  UMERIC || aff==S
23ce2 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 29 20  QLITE_AFF_REAL) 
23ce3 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53  ){.      aff = S
23ce4 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a  QLITE_AFF_NONE;.
23ce5 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
23ce6 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
23ce7 4e 54 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  NT.    }else if(
23ce8 20 68 3d 3d 28 28 27 72 27 3c 3c 32 34 29 2b 28   h==(('r'<<24)+(
23ce9 27 65 27 3c 3c 31 36 29 2b 28 27 61 27 3c 3c 38  'e'<<16)+('a'<<8
23cea 29 2b 27 6c 27 29 20 20 20 20 20 20 20 20 20 20  )+'l')          
23ceb 2f 2a 20 52 45 41 4c 20 2a 2f 0a 20 20 20 20 20  /* REAL */.     
23cec 20 20 20 26 26 20 61 66 66 3d 3d 53 51 4c 49 54     && aff==SQLIT
23ced 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b  E_AFF_NUMERIC ){
23cee 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c  .      aff = SQL
23cef 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 20 20  ITE_AFF_REAL;.  
23cf0 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28    }else if( h==(
23cf1 28 27 66 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c  ('f'<<24)+('l'<<
23cf2 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 61 27  16)+('o'<<8)+'a'
23cf3 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 4c  )          /* FL
23cf4 4f 41 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26  OA */.        &&
23cf5 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46   aff==SQLITE_AFF
23cf6 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 20  _NUMERIC ){.    
23cf7 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41    aff = SQLITE_A
23cf8 46 46 5f 52 45 41 4c 3b 0a 20 20 20 20 7d 65 6c  FF_REAL;.    }el
23cf9 73 65 20 69 66 28 20 68 3d 3d 28 28 27 64 27 3c  se if( h==(('d'<
23cfa 3c 32 34 29 2b 28 27 6f 27 3c 3c 31 36 29 2b 28  <24)+('o'<<16)+(
23cfb 27 75 27 3c 3c 38 29 2b 27 62 27 29 20 20 20 20  'u'<<8)+'b')    
23cfc 20 20 20 20 20 20 2f 2a 20 44 4f 55 42 20 2a 2f        /* DOUB */
23cfd 0a 20 20 20 20 20 20 20 20 26 26 20 61 66 66 3d  .        && aff=
23cfe 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45  =SQLITE_AFF_NUME
23cff 52 49 43 20 29 7b 0a 20 20 20 20 20 20 61 66 66  RIC ){.      aff
23d00 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45   = SQLITE_AFF_RE
23d01 41 4c 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d  AL;.#endif.    }
23d02 65 6c 73 65 20 69 66 28 20 28 68 26 30 78 30 30  else if( (h&0x00
23d03 46 46 46 46 46 46 29 3d 3d 28 28 27 69 27 3c 3c  FFFFFF)==(('i'<<
23d04 31 36 29 2b 28 27 6e 27 3c 3c 38 29 2b 27 74 27  16)+('n'<<8)+'t'
23d05 29 20 29 7b 20 20 20 20 2f 2a 20 49 4e 54 20 2a  ) ){    /* INT *
23d06 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51  /.      aff = SQ
23d07 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
23d08 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
23d09 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
23d0a 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rn aff;.}../*.**
23d0b 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
23d0c 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70   called by the p
23d0d 61 72 73 65 72 20 77 68 69 6c 65 20 69 6e 20 74  arser while in t
23d0e 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a 20  he middle of.** 
23d0f 70 61 72 73 69 6e 67 20 61 20 43 52 45 41 54 45  parsing a CREATE
23d10 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
23d11 2e 20 20 54 68 65 20 70 46 69 72 73 74 20 74 6f  .  The pFirst to
23d12 6b 65 6e 20 69 73 20 74 68 65 20 66 69 72 73 74  ken is the first
23d13 0a 2a 2a 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65  .** token in the
23d14 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74 6f 6b   sequence of tok
23d15 65 6e 73 20 74 68 61 74 20 64 65 73 63 72 69 62  ens that describ
23d16 65 20 74 68 65 20 74 79 70 65 20 6f 66 20 74 68  e the type of th
23d17 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 63 75 72 72  e.** column curr
23d18 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73  ently under cons
23d19 74 72 75 63 74 69 6f 6e 2e 20 20 20 70 4c 61 73  truction.   pLas
23d1a 74 20 69 73 20 74 68 65 20 6c 61 73 74 20 74 6f  t is the last to
23d1b 6b 65 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 73 65  ken.** in the se
23d1c 71 75 65 6e 63 65 2e 20 20 55 73 65 20 74 68 69  quence.  Use thi
23d1d 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f  s information to
23d1e 20 63 6f 6e 73 74 72 75 63 74 20 61 20 73 74 72   construct a str
23d1f 69 6e 67 0a 2a 2a 20 74 68 61 74 20 63 6f 6e 74  ing.** that cont
23d20 61 69 6e 73 20 74 68 65 20 74 79 70 65 6e 61 6d  ains the typenam
23d21 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20  e of the column 
23d22 61 6e 64 20 73 74 6f 72 65 20 74 68 61 74 20 73  and store that s
23d23 74 72 69 6e 67 0a 2a 2a 20 69 6e 20 7a 54 79 70  tring.** in zTyp
23d24 65 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52  e..*/ .SQLITE_PR
23d25 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
23d26 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28  e3AddColumnType(
23d27 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
23d28 6f 6b 65 6e 20 2a 70 54 79 70 65 29 7b 0a 20 20  oken *pType){.  
23d29 54 61 62 6c 65 20 2a 70 3b 0a 20 20 43 6f 6c 75  Table *p;.  Colu
23d2a 6d 6e 20 2a 70 43 6f 6c 3b 0a 0a 20 20 70 20 3d  mn *pCol;..  p =
23d2b 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
23d2c 6c 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c  le;.  if( p==0 |
23d2d 7c 20 4e 45 56 45 52 28 70 2d 3e 6e 43 6f 6c 3c  | NEVER(p->nCol<
23d2e 31 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70  1) ) return;.  p
23d2f 43 6f 6c 20 3d 20 26 70 2d 3e 61 43 6f 6c 5b 70  Col = &p->aCol[p
23d30 2d 3e 6e 43 6f 6c 2d 31 5d 3b 0a 20 20 61 73 73  ->nCol-1];.  ass
23d31 65 72 74 28 20 70 43 6f 6c 2d 3e 7a 54 79 70 65  ert( pCol->zType
23d32 3d 3d 30 20 29 3b 0a 20 20 70 43 6f 6c 2d 3e 7a  ==0 );.  pCol->z
23d33 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 4e 61  Type = sqlite3Na
23d34 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72  meFromToken(pPar
23d35 73 65 2d 3e 64 62 2c 20 70 54 79 70 65 29 3b 0a  se->db, pType);.
23d36 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79    pCol->affinity
23d37 20 3d 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69   = sqlite3Affini
23d38 74 79 54 79 70 65 28 70 43 6f 6c 2d 3e 7a 54 79  tyType(pCol->zTy
23d39 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  pe);.}../*.** Th
23d3a 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
23d3b 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  the default valu
23d3c 65 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72  e for the most r
23d3d 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 63 6f  ecently added co
23d3e 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 74  lumn.** of the t
23d3f 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 75  able currently u
23d40 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
23d41 6e 2e 0a 2a 2a 0a 2a 2a 20 44 65 66 61 75 6c 74  n..**.** Default
23d42 20 76 61 6c 75 65 20 65 78 70 72 65 73 73 69 6f   value expressio
23d43 6e 73 20 6d 75 73 74 20 62 65 20 63 6f 6e 73 74  ns must be const
23d44 61 6e 74 2e 20 20 52 61 69 73 65 20 61 6e 20 65  ant.  Raise an e
23d45 78 63 65 70 74 69 6f 6e 20 69 66 20 74 68 69 73  xception if this
23d46 0a 2a 2a 20 69 73 20 6e 6f 74 20 74 68 65 20 63  .** is not the c
23d47 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ase..**.** This 
23d48 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
23d49 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20  d by the parser 
23d4a 77 68 69 6c 65 20 69 6e 20 74 68 65 20 6d 69 64  while in the mid
23d4b 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e  dle of.** parsin
23d4c 67 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  g a CREATE TABLE
23d4d 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53   statement..*/.S
23d4e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
23d4f 69 64 20 73 71 6c 69 74 65 33 41 64 64 44 65 66  id sqlite3AddDef
23d50 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65 20  aultValue(Parse 
23d51 2a 70 50 61 72 73 65 2c 20 45 78 70 72 53 70 61  *pParse, ExprSpa
23d52 6e 20 2a 70 53 70 61 6e 29 7b 0a 20 20 54 61 62  n *pSpan){.  Tab
23d53 6c 65 20 2a 70 3b 0a 20 20 43 6f 6c 75 6d 6e 20  le *p;.  Column 
23d54 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33  *pCol;.  sqlite3
23d55 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
23d56 62 3b 0a 20 20 70 20 3d 20 70 50 61 72 73 65 2d  b;.  p = pParse-
23d57 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66  >pNewTable;.  if
23d58 28 20 70 21 3d 30 20 29 7b 0a 20 20 20 20 70 43  ( p!=0 ){.    pC
23d59 6f 6c 20 3d 20 26 28 70 2d 3e 61 43 6f 6c 5b 70  ol = &(p->aCol[p
23d5a 2d 3e 6e 43 6f 6c 2d 31 5d 29 3b 0a 20 20 20 20  ->nCol-1]);.    
23d5b 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 70 72  if( !sqlite3Expr
23d5c 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63  IsConstantOrFunc
23d5d 74 69 6f 6e 28 70 53 70 61 6e 2d 3e 70 45 78 70  tion(pSpan->pExp
23d5e 72 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  r) ){.      sqli
23d5f 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
23d60 73 65 2c 20 22 64 65 66 61 75 6c 74 20 76 61 6c  se, "default val
23d61 75 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 5b 25 73  ue of column [%s
23d62 5d 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74 61 6e  ] is not constan
23d63 74 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 43  t",.          pC
23d64 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ol->zName);.    
23d65 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20  }else{.      /* 
23d66 41 20 63 6f 70 79 20 6f 66 20 70 45 78 70 72 20  A copy of pExpr 
23d67 69 73 20 75 73 65 64 20 69 6e 73 74 65 61 64 20  is used instead 
23d68 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 2c  of the original,
23d69 20 61 73 20 70 45 78 70 72 20 63 6f 6e 74 61 69   as pExpr contai
23d6a 6e 73 0a 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65  ns.      ** toke
23d6b 6e 73 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f  ns that point to
23d6c 20 76 6f 6c 61 74 69 6c 65 20 6d 65 6d 6f 72 79   volatile memory
23d6d 2e 20 54 68 65 20 27 73 70 61 6e 27 20 6f 66 20  . The 'span' of 
23d6e 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 20  the expression. 
23d6f 20 20 20 20 20 2a 2a 20 69 73 20 72 65 71 75 69       ** is requi
23d70 72 65 64 20 62 79 20 70 72 61 67 6d 61 20 74 61  red by pragma ta
23d71 62 6c 65 5f 69 6e 66 6f 2e 0a 20 20 20 20 20 20  ble_info..      
23d72 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
23d73 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
23d74 43 6f 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20 20 20  Col->pDflt);.   
23d75 20 20 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20 3d     pCol->pDflt =
23d76 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
23d77 64 62 2c 20 70 53 70 61 6e 2d 3e 70 45 78 70 72  db, pSpan->pExpr
23d78 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45  , EXPRDUP_REDUCE
23d79 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
23d7a 44 62 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d  DbFree(db, pCol-
23d7b 3e 7a 44 66 6c 74 29 3b 0a 20 20 20 20 20 20 70  >zDflt);.      p
23d7c 43 6f 6c 2d 3e 7a 44 66 6c 74 20 3d 20 73 71 6c  Col->zDflt = sql
23d7d 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62  ite3DbStrNDup(db
23d7e 2c 20 28 63 68 61 72 2a 29 70 53 70 61 6e 2d 3e  , (char*)pSpan->
23d7f 7a 53 74 61 72 74 2c 0a 20 20 20 20 20 20 20 20  zStart,.        
23d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23d81 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69 6e               (in
23d82 74 29 28 70 53 70 61 6e 2d 3e 7a 45 6e 64 20 2d  t)(pSpan->zEnd -
23d83 20 70 53 70 61 6e 2d 3e 7a 53 74 61 72 74 29 29   pSpan->zStart))
23d84 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
23d85 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
23d86 64 62 2c 20 70 53 70 61 6e 2d 3e 70 45 78 70 72  db, pSpan->pExpr
23d87 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 69  );.}../*.** Desi
23d88 67 6e 61 74 65 20 74 68 65 20 50 52 49 4d 41 52  gnate the PRIMAR
23d89 59 20 4b 45 59 20 66 6f 72 20 74 68 65 20 74 61  Y KEY for the ta
23d8a 62 6c 65 2e 20 20 70 4c 69 73 74 20 69 73 20 61  ble.  pList is a
23d8b 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 73 20 0a   list of names .
23d8c 2a 2a 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 68  ** of columns th
23d8d 61 74 20 66 6f 72 6d 20 74 68 65 20 70 72 69 6d  at form the prim
23d8e 61 72 79 20 6b 65 79 2e 20 20 49 66 20 70 4c 69  ary key.  If pLi
23d8f 73 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e  st is NULL, then
23d90 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 72 65 63   the.** most rec
23d91 65 6e 74 6c 79 20 61 64 64 65 64 20 63 6f 6c 75  ently added colu
23d92 6d 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  mn of the table 
23d93 69 73 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  is the primary k
23d94 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 61 62 6c  ey..**.** A tabl
23d95 65 20 63 61 6e 20 68 61 76 65 20 61 74 20 6d 6f  e can have at mo
23d96 73 74 20 6f 6e 65 20 70 72 69 6d 61 72 79 20 6b  st one primary k
23d97 65 79 2e 20 20 49 66 20 74 68 65 20 74 61 62 6c  ey.  If the tabl
23d98 65 20 61 6c 72 65 61 64 79 20 68 61 73 0a 2a 2a  e already has.**
23d99 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 28   a primary key (
23d9a 61 6e 64 20 74 68 69 73 20 69 73 20 74 68 65 20  and this is the 
23d9b 73 65 63 6f 6e 64 20 70 72 69 6d 61 72 79 20 6b  second primary k
23d9c 65 79 29 20 74 68 65 6e 20 63 72 65 61 74 65 20  ey) then create 
23d9d 61 6e 0a 2a 2a 20 65 72 72 6f 72 2e 0a 2a 2a 0a  an.** error..**.
23d9e 2a 2a 20 49 66 20 74 68 65 20 50 52 49 4d 41 52  ** If the PRIMAR
23d9f 59 20 4b 45 59 20 69 73 20 6f 6e 20 61 20 73 69  Y KEY is on a si
23da0 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 77 68 6f 73  ngle column whos
23da1 65 20 64 61 74 61 74 79 70 65 20 69 73 20 49 4e  e datatype is IN
23da2 54 45 47 45 52 2c 0a 2a 2a 20 74 68 65 6e 20 77  TEGER,.** then w
23da3 65 20 77 69 6c 6c 20 74 72 79 20 74 6f 20 75 73  e will try to us
23da4 65 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 61 73  e that column as
23da5 20 74 68 65 20 72 6f 77 69 64 2e 20 20 53 65 74   the rowid.  Set
23da6 20 74 68 65 20 54 61 62 6c 65 2e 69 50 4b 65 79   the Table.iPKey
23da7 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65  .** field of the
23da8 20 74 61 62 6c 65 20 75 6e 64 65 72 20 63 6f 6e   table under con
23da9 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20  struction to be 
23daa 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
23dab 0a 2a 2a 20 49 4e 54 45 47 45 52 20 50 52 49 4d  .** INTEGER PRIM
23dac 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 2e 20  ARY KEY column. 
23dad 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20   Table.iPKey is 
23dae 73 65 74 20 74 6f 20 2d 31 20 69 66 20 74 68 65  set to -1 if the
23daf 72 65 20 69 73 0a 2a 2a 20 6e 6f 20 49 4e 54 45  re is.** no INTE
23db0 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2e  GER PRIMARY KEY.
23db1 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6b 65  .**.** If the ke
23db2 79 20 69 73 20 6e 6f 74 20 61 6e 20 49 4e 54 45  y is not an INTE
23db3 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c  GER PRIMARY KEY,
23db4 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 20 75   then create a u
23db5 6e 69 71 75 65 0a 2a 2a 20 69 6e 64 65 78 20 66  nique.** index f
23db6 6f 72 20 74 68 65 20 6b 65 79 2e 20 20 4e 6f 20  or the key.  No 
23db7 69 6e 64 65 78 20 69 73 20 63 72 65 61 74 65 64  index is created
23db8 20 66 6f 72 20 49 4e 54 45 47 45 52 20 50 52 49   for INTEGER PRI
23db9 4d 41 52 59 20 4b 45 59 73 2e 0a 2a 2f 0a 53 51  MARY KEYs..*/.SQ
23dba 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
23dbb 64 20 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d  d sqlite3AddPrim
23dbc 61 72 79 4b 65 79 28 0a 20 20 50 61 72 73 65 20  aryKey(.  Parse 
23dbd 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 50  *pParse,    /* P
23dbe 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
23dbf 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  /.  ExprList *pL
23dc0 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66  ist,  /* List of
23dc1 20 66 69 65 6c 64 20 6e 61 6d 65 73 20 74 6f 20   field names to 
23dc2 62 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20  be indexed */.  
23dc3 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20  int onError,    
23dc4 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20    /* What to do 
23dc5 77 69 74 68 20 61 20 75 6e 69 71 75 65 6e 65 73  with a uniquenes
23dc6 73 20 63 6f 6e 66 6c 69 63 74 20 2a 2f 0a 20 20  s conflict */.  
23dc7 69 6e 74 20 61 75 74 6f 49 6e 63 2c 20 20 20 20  int autoInc,    
23dc8 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
23dc9 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 6b   AUTOINCREMENT k
23dca 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e  eyword is presen
23dcb 74 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 4f  t */.  int sortO
23dcc 72 64 65 72 20 20 20 20 20 2f 2a 20 53 51 4c 49  rder     /* SQLI
23dcd 54 45 5f 53 4f 5f 41 53 43 20 6f 72 20 53 51 4c  TE_SO_ASC or SQL
23dce 49 54 45 5f 53 4f 5f 44 45 53 43 20 2a 2f 0a 29  ITE_SO_DESC */.)
23dcf 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20  {.  Table *pTab 
23dd0 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  = pParse->pNewTa
23dd1 62 6c 65 3b 0a 20 20 63 68 61 72 20 2a 7a 54 79  ble;.  char *zTy
23dd2 70 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 43  pe = 0;.  int iC
23dd3 6f 6c 20 3d 20 2d 31 2c 20 69 3b 0a 20 20 69 66  ol = -1, i;.  if
23dd4 28 20 70 54 61 62 3d 3d 30 20 7c 7c 20 49 4e 5f  ( pTab==0 || IN_
23dd5 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29 20 67  DECLARE_VTAB ) g
23dd6 6f 74 6f 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f  oto primary_key_
23dd7 65 78 69 74 3b 0a 20 20 69 66 28 20 70 54 61 62  exit;.  if( pTab
23dd8 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f  ->tabFlags & TF_
23dd9 48 61 73 50 72 69 6d 61 72 79 4b 65 79 20 29 7b  HasPrimaryKey ){
23dda 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
23ddb 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20  rMsg(pParse, .  
23ddc 20 20 20 20 22 74 61 62 6c 65 20 5c 22 25 73 5c      "table \"%s\
23ddd 22 20 68 61 73 20 6d 6f 72 65 20 74 68 61 6e 20  " has more than 
23dde 6f 6e 65 20 70 72 69 6d 61 72 79 20 6b 65 79 22  one primary key"
23ddf 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
23de0 20 20 20 20 67 6f 74 6f 20 70 72 69 6d 61 72 79      goto primary
23de1 5f 6b 65 79 5f 65 78 69 74 3b 0a 20 20 7d 0a 20  _key_exit;.  }. 
23de2 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20   pTab->tabFlags 
23de3 7c 3d 20 54 46 5f 48 61 73 50 72 69 6d 61 72 79  |= TF_HasPrimary
23de4 4b 65 79 3b 0a 20 20 69 66 28 20 70 4c 69 73 74  Key;.  if( pList
23de5 3d 3d 30 20 29 7b 0a 20 20 20 20 69 43 6f 6c 20  ==0 ){.    iCol 
23de6 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 2d 20 31  = pTab->nCol - 1
23de7 3b 0a 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c  ;.    pTab->aCol
23de8 5b 69 43 6f 6c 5d 2e 69 73 50 72 69 6d 4b 65 79  [iCol].isPrimKey
23de9 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 1;.  }else{. 
23dea 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c     for(i=0; i<pL
23deb 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
23dec 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 43 6f 6c  {.      for(iCol
23ded 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e  =0; iCol<pTab->n
23dee 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20  Col; iCol++){.  
23def 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
23df0 33 53 74 72 49 43 6d 70 28 70 4c 69 73 74 2d 3e  3StrICmp(pList->
23df1 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 70 54 61 62  a[i].zName, pTab
23df2 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61  ->aCol[iCol].zNa
23df3 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  me)==0 ){.      
23df4 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
23df5 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
23df6 20 20 20 69 66 28 20 69 43 6f 6c 3c 70 54 61 62     if( iCol<pTab
23df7 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20  ->nCol ){.      
23df8 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f    pTab->aCol[iCo
23df9 6c 5d 2e 69 73 50 72 69 6d 4b 65 79 20 3d 20 31  l].isPrimKey = 1
23dfa 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
23dfb 20 20 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e      if( pList->n
23dfc 45 78 70 72 3e 31 20 29 20 69 43 6f 6c 20 3d 20  Expr>1 ) iCol = 
23dfd 2d 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 43  -1;.  }.  if( iC
23dfe 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 54  ol>=0 && iCol<pT
23dff 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20  ab->nCol ){.    
23e00 7a 54 79 70 65 20 3d 20 70 54 61 62 2d 3e 61 43  zType = pTab->aC
23e01 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 70 65 3b 0a  ol[iCol].zType;.
23e02 20 20 7d 0a 20 20 69 66 28 20 7a 54 79 70 65 20    }.  if( zType 
23e03 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  && sqlite3StrICm
23e04 70 28 7a 54 79 70 65 2c 20 22 49 4e 54 45 47 45  p(zType, "INTEGE
23e05 52 22 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 26  R")==0.        &
23e06 26 20 73 6f 72 74 4f 72 64 65 72 3d 3d 53 51 4c  & sortOrder==SQL
23e07 49 54 45 5f 53 4f 5f 41 53 43 20 29 7b 0a 20 20  ITE_SO_ASC ){.  
23e08 20 20 70 54 61 62 2d 3e 69 50 4b 65 79 20 3d 20    pTab->iPKey = 
23e09 69 43 6f 6c 3b 0a 20 20 20 20 70 54 61 62 2d 3e  iCol;.    pTab->
23e0a 6b 65 79 43 6f 6e 66 20 3d 20 28 75 38 29 6f 6e  keyConf = (u8)on
23e0b 45 72 72 6f 72 3b 0a 20 20 20 20 61 73 73 65 72  Error;.    asser
23e0c 74 28 20 61 75 74 6f 49 6e 63 3d 3d 30 20 7c 7c  t( autoInc==0 ||
23e0d 20 61 75 74 6f 49 6e 63 3d 3d 31 20 29 3b 0a 20   autoInc==1 );. 
23e0e 20 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67     pTab->tabFlag
23e0f 73 20 7c 3d 20 61 75 74 6f 49 6e 63 2a 54 46 5f  s |= autoInc*TF_
23e10 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 3b 0a 20  Autoincrement;. 
23e11 20 7d 65 6c 73 65 20 69 66 28 20 61 75 74 6f 49   }else if( autoI
23e12 6e 63 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51  nc ){.#ifndef SQ
23e13 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
23e14 43 52 45 4d 45 4e 54 0a 20 20 20 20 73 71 6c 69  CREMENT.    sqli
23e15 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
23e16 73 65 2c 20 22 41 55 54 4f 49 4e 43 52 45 4d 45  se, "AUTOINCREME
23e17 4e 54 20 69 73 20 6f 6e 6c 79 20 61 6c 6c 6f 77  NT is only allow
23e18 65 64 20 6f 6e 20 61 6e 20 22 0a 20 20 20 20 20  ed on an ".     
23e19 20 20 22 49 4e 54 45 47 45 52 20 50 52 49 4d 41    "INTEGER PRIMA
23e1a 52 59 20 4b 45 59 22 29 3b 0a 23 65 6e 64 69 66  RY KEY");.#endif
23e1b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 49 6e  .  }else{.    In
23e1c 64 65 78 20 2a 70 3b 0a 20 20 20 20 70 20 3d 20  dex *p;.    p = 
23e1d 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64  sqlite3CreateInd
23e1e 65 78 28 70 50 61 72 73 65 2c 20 30 2c 20 30 2c  ex(pParse, 0, 0,
23e1f 20 30 2c 20 70 4c 69 73 74 2c 20 6f 6e 45 72 72   0, pList, onErr
23e20 6f 72 2c 20 30 2c 20 30 2c 20 73 6f 72 74 4f 72  or, 0, 0, sortOr
23e21 64 65 72 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  der, 0);.    if(
23e22 20 70 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61   p ){.      p->a
23e23 75 74 6f 49 6e 64 65 78 20 3d 20 32 3b 0a 20 20  utoIndex = 2;.  
23e24 20 20 7d 0a 20 20 20 20 70 4c 69 73 74 20 3d 20    }.    pList = 
23e25 30 3b 0a 20 20 7d 0a 0a 70 72 69 6d 61 72 79 5f  0;.  }..primary_
23e26 6b 65 79 5f 65 78 69 74 3a 0a 20 20 73 71 6c 69  key_exit:.  sqli
23e27 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
23e28 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4c  e(pParse->db, pL
23e29 69 73 74 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a  ist);.  return;.
23e2a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e  }../*.** Add a n
23e2b 65 77 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61  ew CHECK constra
23e2c 69 6e 74 20 74 6f 20 74 68 65 20 74 61 62 6c 65  int to the table
23e2d 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72   currently under
23e2e 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a   construction..*
23e2f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
23e30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64   void sqlite3Add
23e31 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e 74 28  CheckConstraint(
23e32 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
23e33 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20  ,    /* Parsing 
23e34 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70  context */.  Exp
23e35 72 20 2a 70 43 68 65 63 6b 45 78 70 72 20 20 2f  r *pCheckExpr  /
23e36 2a 20 54 68 65 20 63 68 65 63 6b 20 65 78 70 72  * The check expr
23e37 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73  ession */.){.  s
23e38 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
23e39 72 73 65 2d 3e 64 62 3b 0a 23 69 66 6e 64 65 66  rse->db;.#ifndef
23e3a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45   SQLITE_OMIT_CHE
23e3b 43 4b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  CK.  Table *pTab
23e3c 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
23e3d 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 54 61 62  able;.  if( pTab
23e3e 20 26 26 20 21 49 4e 5f 44 45 43 4c 41 52 45 5f   && !IN_DECLARE_
23e3f 56 54 41 42 20 29 7b 0a 20 20 20 20 70 54 61 62  VTAB ){.    pTab
23e40 2d 3e 70 43 68 65 63 6b 20 3d 20 73 71 6c 69 74  ->pCheck = sqlit
23e41 65 33 45 78 70 72 41 6e 64 28 64 62 2c 20 70 54  e3ExprAnd(db, pT
23e42 61 62 2d 3e 70 43 68 65 63 6b 2c 20 70 43 68 65  ab->pCheck, pChe
23e43 63 6b 45 78 70 72 29 3b 0a 20 20 7d 65 6c 73 65  ckExpr);.  }else
23e44 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20  .#endif.  {.    
23e45 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
23e46 65 28 64 62 2c 20 70 43 68 65 63 6b 45 78 70 72  e(db, pCheckExpr
23e47 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
23e48 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  Set the collatio
23e49 6e 20 66 75 6e 63 74 69 6f 6e 20 6f 66 20 74 68  n function of th
23e4a 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
23e4b 70 61 72 73 65 64 20 74 61 62 6c 65 20 63 6f 6c  parsed table col
23e4c 75 6d 6e 0a 2a 2a 20 74 6f 20 74 68 65 20 43 6f  umn.** to the Co
23e4d 6c 6c 53 65 71 20 67 69 76 65 6e 2e 0a 2a 2f 0a  llSeq given..*/.
23e4e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
23e4f 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f  oid sqlite3AddCo
23e50 6c 6c 61 74 65 54 79 70 65 28 50 61 72 73 65 20  llateType(Parse 
23e51 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a  *pParse, Token *
23e52 70 54 6f 6b 65 6e 29 7b 0a 20 20 54 61 62 6c 65  pToken){.  Table
23e53 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20   *p;.  int i;.  
23e54 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20  char *zColl;    
23e55 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 71            /* Deq
23e56 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f  uoted name of co
23e57 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
23e58 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
23e59 62 3b 0a 0a 20 20 69 66 28 20 28 70 20 3d 20 70  b;..  if( (p = p
23e5a 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
23e5b 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  )==0 ) return;. 
23e5c 20 69 20 3d 20 70 2d 3e 6e 43 6f 6c 2d 31 3b 0a   i = p->nCol-1;.
23e5d 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
23e5e 62 3b 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c  b;.  zColl = sql
23e5f 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
23e60 6e 28 64 62 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20  n(db, pToken);. 
23e61 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29 20 72 65   if( !zColl ) re
23e62 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20 73 71 6c  turn;..  if( sql
23e63 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65  ite3LocateCollSe
23e64 71 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 29  q(pParse, zColl)
23e65 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70   ){.    Index *p
23e66 49 64 78 3b 0a 20 20 20 20 70 2d 3e 61 43 6f 6c  Idx;.    p->aCol
23e67 5b 69 5d 2e 7a 43 6f 6c 6c 20 3d 20 7a 43 6f 6c  [i].zColl = zCol
23e68 6c 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20  l;.  .    /* If 
23e69 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 64 65  the column is de
23e6a 63 6c 61 72 65 64 20 61 73 20 22 3c 6e 61 6d 65  clared as "<name
23e6b 3e 20 50 52 49 4d 41 52 59 20 4b 45 59 20 43 4f  > PRIMARY KEY CO
23e6c 4c 4c 41 54 45 20 3c 74 79 70 65 3e 22 2c 0a 20  LLATE <type>",. 
23e6d 20 20 20 2a 2a 20 74 68 65 6e 20 61 6e 20 69 6e     ** then an in
23e6e 64 65 78 20 6d 61 79 20 68 61 76 65 20 62 65 65  dex may have bee
23e6f 6e 20 63 72 65 61 74 65 64 20 6f 6e 20 74 68 69  n created on thi
23e70 73 20 63 6f 6c 75 6d 6e 20 62 65 66 6f 72 65 20  s column before 
23e71 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c 6c 61  the.    ** colla
23e72 74 69 6f 6e 20 74 79 70 65 20 77 61 73 20 61 64  tion type was ad
23e73 64 65 64 2e 20 43 6f 72 72 65 63 74 20 74 68 69  ded. Correct thi
23e74 73 20 69 66 20 69 74 20 69 73 20 74 68 65 20 63  s if it is the c
23e75 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ase..    */.    
23e76 66 6f 72 28 70 49 64 78 3d 70 2d 3e 70 49 6e 64  for(pIdx=p->pInd
23e77 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
23e78 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
23e79 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d     assert( pIdx-
23e7a 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20  >nColumn==1 );. 
23e7b 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 61       if( pIdx->a
23e7c 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69 20 29 7b  iColumn[0]==i ){
23e7d 0a 20 20 20 20 20 20 20 20 70 49 64 78 2d 3e 61  .        pIdx->a
23e7e 7a 43 6f 6c 6c 5b 30 5d 20 3d 20 70 2d 3e 61 43  zColl[0] = p->aC
23e7f 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20  ol[i].zColl;.   
23e80 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c     }.    }.  }el
23e81 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  se{.    sqlite3D
23e82 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29  bFree(db, zColl)
23e83 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
23e84 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
23e85 75 72 6e 73 20 74 68 65 20 63 6f 6c 6c 61 74 69  urns the collati
23e86 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20  on sequence for 
23e87 64 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 20  database native 
23e88 74 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67  text.** encoding
23e89 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74   identified by t
23e8a 68 65 20 73 74 72 69 6e 67 20 7a 4e 61 6d 65 2c  he string zName,
23e8b 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a   length nName..*
23e8c 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 71 75  *.** If the requ
23e8d 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20  ested collation 
23e8e 73 65 71 75 65 6e 63 65 20 69 73 20 6e 6f 74 20  sequence is not 
23e8f 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72 20 6e 6f  available, or no
23e90 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 69  t available.** i
23e91 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  n the database n
23e92 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 2c 20  ative encoding, 
23e93 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61  the collation fa
23e94 63 74 6f 72 79 20 69 73 20 69 6e 76 6f 6b 65 64  ctory is invoked
23e95 20 74 6f 0a 2a 2a 20 72 65 71 75 65 73 74 20 69   to.** request i
23e96 74 2e 20 49 66 20 74 68 65 20 63 6f 6c 6c 61 74  t. If the collat
23e97 69 6f 6e 20 66 61 63 74 6f 72 79 20 64 6f 65 73  ion factory does
23e98 20 6e 6f 74 20 73 75 70 70 6c 79 20 73 75 63 68   not supply such
23e99 20 61 20 73 65 71 75 65 6e 63 65 2c 0a 2a 2a 20   a sequence,.** 
23e9a 61 6e 64 20 74 68 65 20 73 65 71 75 65 6e 63 65  and the sequence
23e9b 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 69 6e   is available in
23e9c 20 61 6e 6f 74 68 65 72 20 74 65 78 74 20 65 6e   another text en
23e9d 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 74 68 61  coding, then tha
23e9e 74 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64  t is.** returned
23e9f 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20   instead..**.** 
23ea0 49 66 20 6e 6f 20 76 65 72 73 69 6f 6e 73 20 6f  If no versions o
23ea1 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  f the requested 
23ea2 63 6f 6c 6c 61 74 69 6f 6e 73 20 73 65 71 75 65  collations seque
23ea3 6e 63 65 20 61 72 65 20 61 76 61 69 6c 61 62 6c  nce are availabl
23ea4 65 2c 20 6f 72 0a 2a 2a 20 61 6e 6f 74 68 65 72  e, or.** another
23ea5 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e   error occurs, N
23ea6 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20  ULL is returned 
23ea7 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  and an error mes
23ea8 73 61 67 65 20 77 72 69 74 74 65 6e 20 69 6e 74  sage written int
23ea9 6f 0a 2a 2a 20 70 50 61 72 73 65 2e 0a 2a 2a 0a  o.** pParse..**.
23eaa 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
23eab 69 73 20 61 20 77 72 61 70 70 65 72 20 61 72 6f  is a wrapper aro
23eac 75 6e 64 20 73 71 6c 69 74 65 33 46 69 6e 64 43  und sqlite3FindC
23ead 6f 6c 6c 53 65 71 28 29 2e 20 20 54 68 69 73 20  ollSeq().  This 
23eae 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 6e 76 6f 6b  routine.** invok
23eaf 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  es the collation
23eb0 20 66 61 63 74 6f 72 79 20 69 66 20 74 68 65 20   factory if the 
23eb1 6e 61 6d 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20  named collation 
23eb2 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 0a  cannot be found.
23eb3 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61 74 65 73  ** and generates
23eb4 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
23eb5 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  e..**.** See als
23eb6 6f 3a 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f  o: sqlite3FindCo
23eb7 6c 6c 53 65 71 28 29 2c 20 73 71 6c 69 74 65 33  llSeq(), sqlite3
23eb8 47 65 74 43 6f 6c 6c 53 65 71 28 29 0a 2a 2f 0a  GetCollSeq().*/.
23eb9 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43  SQLITE_PRIVATE C
23eba 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 4c  ollSeq *sqlite3L
23ebb 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 50 61 72  ocateCollSeq(Par
23ebc 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73  se *pParse, cons
23ebd 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a  t char *zName){.
23ebe 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
23ebf 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 75 38  pParse->db;.  u8
23ec0 20 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a   enc = ENC(db);.
23ec1 20 20 75 38 20 69 6e 69 74 62 75 73 79 20 3d 20    u8 initbusy = 
23ec2 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3b 0a 20  db->init.busy;. 
23ec3 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
23ec4 0a 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69  ..  pColl = sqli
23ec5 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64  te3FindCollSeq(d
23ec6 62 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c 20 69  b, enc, zName, i
23ec7 6e 69 74 62 75 73 79 29 3b 0a 20 20 69 66 28 20  nitbusy);.  if( 
23ec8 21 69 6e 69 74 62 75 73 79 20 26 26 20 28 21 70  !initbusy && (!p
23ec9 43 6f 6c 6c 20 7c 7c 20 21 70 43 6f 6c 6c 2d 3e  Coll || !pColl->
23eca 78 43 6d 70 29 20 29 7b 0a 20 20 20 20 70 43 6f  xCmp) ){.    pCo
23ecb 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 43  ll = sqlite3GetC
23ecc 6f 6c 6c 53 65 71 28 64 62 2c 20 65 6e 63 2c 20  ollSeq(db, enc, 
23ecd 70 43 6f 6c 6c 2c 20 7a 4e 61 6d 65 29 3b 0a 20  pColl, zName);. 
23ece 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 7b     if( !pColl ){
23ecf 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
23ed0 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
23ed1 6e 6f 20 73 75 63 68 20 63 6f 6c 6c 61 74 69 6f  no such collatio
23ed2 6e 20 73 65 71 75 65 6e 63 65 3a 20 25 73 22 2c  n sequence: %s",
23ed3 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20   zName);.    }. 
23ed4 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 43 6f   }..  return pCo
23ed5 6c 6c 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65  ll;.}.../*.** Ge
23ed6 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
23ed7 20 77 69 6c 6c 20 69 6e 63 72 65 6d 65 6e 74 20   will increment 
23ed8 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  the schema cooki
23ed9 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 63 68  e..**.** The sch
23eda 65 6d 61 20 63 6f 6f 6b 69 65 20 69 73 20 75 73  ema cookie is us
23edb 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
23edc 77 68 65 6e 20 74 68 65 20 73 63 68 65 6d 61 20  when the schema 
23edd 66 6f 72 20 74 68 65 0a 2a 2a 20 64 61 74 61 62  for the.** datab
23ede 61 73 65 20 63 68 61 6e 67 65 73 2e 20 20 41 66  ase changes.  Af
23edf 74 65 72 20 65 61 63 68 20 73 63 68 65 6d 61 20  ter each schema 
23ee0 63 68 61 6e 67 65 2c 20 74 68 65 20 63 6f 6f 6b  change, the cook
23ee1 69 65 20 76 61 6c 75 65 0a 2a 2a 20 63 68 61 6e  ie value.** chan
23ee2 67 65 73 2e 20 20 57 68 65 6e 20 61 20 70 72 6f  ges.  When a pro
23ee3 63 65 73 73 20 66 69 72 73 74 20 72 65 61 64 73  cess first reads
23ee4 20 74 68 65 20 73 63 68 65 6d 61 20 69 74 20 72   the schema it r
23ee5 65 63 6f 72 64 73 20 74 68 65 0a 2a 2a 20 63 6f  ecords the.** co
23ee6 6f 6b 69 65 2e 20 20 54 68 65 72 65 61 66 74 65  okie.  Thereafte
23ee7 72 2c 20 77 68 65 6e 65 76 65 72 20 69 74 20 67  r, whenever it g
23ee8 6f 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68  oes to access th
23ee9 65 20 64 61 74 61 62 61 73 65 2c 0a 2a 2a 20 69  e database,.** i
23eea 74 20 63 68 65 63 6b 73 20 74 68 65 20 63 6f 6f  t checks the coo
23eeb 6b 69 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  kie to make sure
23eec 20 74 68 65 20 73 63 68 65 6d 61 20 68 61 73 20   the schema has 
23eed 6e 6f 74 20 63 68 61 6e 67 65 64 0a 2a 2a 20 73  not changed.** s
23eee 69 6e 63 65 20 69 74 20 77 61 73 20 6c 61 73 74  ince it was last
23eef 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   read..**.** Thi
23ef0 73 20 70 6c 61 6e 20 69 73 20 6e 6f 74 20 63 6f  s plan is not co
23ef1 6d 70 6c 65 74 65 6c 79 20 62 75 6c 6c 65 74 2d  mpletely bullet-
23ef2 70 72 6f 6f 66 2e 20 20 49 74 20 69 73 20 70 6f  proof.  It is po
23ef3 73 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 74 68  ssible for.** th
23ef4 65 20 73 63 68 65 6d 61 20 74 6f 20 63 68 61 6e  e schema to chan
23ef5 67 65 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  ge multiple time
23ef6 73 20 61 6e 64 20 66 6f 72 20 74 68 65 20 63 6f  s and for the co
23ef7 6f 6b 69 65 20 74 6f 20 62 65 0a 2a 2a 20 73 65  okie to be.** se
23ef8 74 20 62 61 63 6b 20 74 6f 20 70 72 69 6f 72 20  t back to prior 
23ef9 76 61 6c 75 65 2e 20 20 42 75 74 20 73 63 68 65  value.  But sche
23efa 6d 61 20 63 68 61 6e 67 65 73 20 61 72 65 20 69  ma changes are i
23efb 6e 66 72 65 71 75 65 6e 74 0a 2a 2a 20 61 6e 64  nfrequent.** and
23efc 20 74 68 65 20 70 72 6f 62 61 62 69 6c 69 74 79   the probability
23efd 20 6f 66 20 68 69 74 74 69 6e 67 20 74 68 65 20   of hitting the 
23efe 73 61 6d 65 20 63 6f 6f 6b 69 65 20 76 61 6c 75  same cookie valu
23eff 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 31 20 63  e is only.** 1 c
23f00 68 61 6e 63 65 20 69 6e 20 32 5e 33 32 2e 20 20  hance in 2^32.  
23f01 53 6f 20 77 65 27 72 65 20 73 61 66 65 20 65 6e  So we're safe en
23f02 6f 75 67 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ough..*/.SQLITE_
23f03 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
23f04 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65  ite3ChangeCookie
23f05 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
23f06 69 6e 74 20 69 44 62 29 7b 0a 20 20 69 6e 74 20  int iDb){.  int 
23f07 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
23f08 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
23f09 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
23f0a 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 56 64  pParse->db;.  Vd
23f0b 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
23f0c 70 56 64 62 65 3b 0a 20 20 73 71 6c 69 74 65 33  pVdbe;.  sqlite3
23f0d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
23f0e 5f 49 6e 74 65 67 65 72 2c 20 64 62 2d 3e 61 44  _Integer, db->aD
23f0f 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e  b[iDb].pSchema->
23f10 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 2b 31 2c  schema_cookie+1,
23f11 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56   r1);.  sqlite3V
23f12 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
23f13 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20  SetCookie, iDb, 
23f14 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52  BTREE_SCHEMA_VER
23f15 53 49 4f 4e 2c 20 72 31 29 3b 0a 20 20 73 71 6c  SION, r1);.  sql
23f16 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
23f17 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a  eg(pParse, r1);.
23f18 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 61 73 75 72 65  }../*.** Measure
23f19 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
23f1a 68 61 72 61 63 74 65 72 73 20 6e 65 65 64 65 64  haracters needed
23f1b 20 74 6f 20 6f 75 74 70 75 74 20 74 68 65 20 67   to output the g
23f1c 69 76 65 6e 0a 2a 2a 20 69 64 65 6e 74 69 66 69  iven.** identifi
23f1d 65 72 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20  er.  The number 
23f1e 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64 65  returned include
23f1f 73 20 61 6e 79 20 71 75 6f 74 65 73 20 75 73 65  s any quotes use
23f20 64 0a 2a 2a 20 62 75 74 20 64 6f 65 73 20 6e 6f  d.** but does no
23f21 74 20 69 6e 63 6c 75 64 65 20 74 68 65 20 6e 75  t include the nu
23f22 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a  ll terminator..*
23f23 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69 6d 61 74  *.** The estimat
23f24 65 20 69 73 20 63 6f 6e 73 65 72 76 61 74 69 76  e is conservativ
23f25 65 2e 20 20 49 74 20 6d 69 67 68 74 20 62 65 20  e.  It might be 
23f26 6c 61 72 67 65 72 20 74 68 61 74 20 77 68 61 74  larger that what
23f27 20 69 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 6e 65   is.** really ne
23f28 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eded..*/.static 
23f29 69 6e 74 20 69 64 65 6e 74 4c 65 6e 67 74 68 28  int identLength(
23f2a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a  const char *z){.
23f2b 20 20 69 6e 74 20 6e 3b 0a 20 20 66 6f 72 28 6e    int n;.  for(n
23f2c 3d 30 3b 20 2a 7a 3b 20 6e 2b 2b 2c 20 7a 2b 2b  =0; *z; n++, z++
23f2d 29 7b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27  ){.    if( *z=='
23f2e 22 27 20 29 7b 20 6e 2b 2b 3b 20 7d 0a 20 20 7d  "' ){ n++; }.  }
23f2f 0a 20 20 72 65 74 75 72 6e 20 6e 20 2b 20 32 3b  .  return n + 2;
23f30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69  .}../*.** The fi
23f31 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73  rst parameter is
23f32 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
23f33 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e 20   output buffer. 
23f34 54 68 65 20 73 65 63 6f 6e 64 20 0a 2a 2a 20 70  The second .** p
23f35 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f  arameter is a po
23f36 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 74 65  inter to an inte
23f37 67 65 72 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ger that contain
23f38 73 20 74 68 65 20 6f 66 66 73 65 74 20 61 74 0a  s the offset at.
23f39 2a 2a 20 77 68 69 63 68 20 74 6f 20 77 72 69 74  ** which to writ
23f3a 65 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75  e into the outpu
23f3b 74 20 62 75 66 66 65 72 2e 20 54 68 69 73 20 66  t buffer. This f
23f3c 75 6e 63 74 69 6f 6e 20 63 6f 70 69 65 73 20 74  unction copies t
23f3d 68 65 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e  he.** nul-termin
23f3e 61 74 65 64 20 73 74 72 69 6e 67 20 70 6f 69 6e  ated string poin
23f3f 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 74 68  ted to by the th
23f40 69 72 64 20 70 61 72 61 6d 65 74 65 72 2c 20 7a  ird parameter, z
23f41 53 69 67 6e 65 64 49 64 65 6e 74 2c 0a 2a 2a 20  SignedIdent,.** 
23f42 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 65 64  to the specified
23f43 20 6f 66 66 73 65 74 20 69 6e 20 74 68 65 20 62   offset in the b
23f44 75 66 66 65 72 20 61 6e 64 20 75 70 64 61 74 65  uffer and update
23f45 73 20 2a 70 49 64 78 20 74 6f 20 72 65 66 65 72  s *pIdx to refer
23f46 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74  .** to the first
23f47 20 62 79 74 65 20 61 66 74 65 72 20 74 68 65 20   byte after the 
23f48 6c 61 73 74 20 62 79 74 65 20 77 72 69 74 74 65  last byte writte
23f49 6e 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  n before returni
23f4a 6e 67 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68  ng..** .** If th
23f4b 65 20 73 74 72 69 6e 67 20 7a 53 69 67 6e 65 64  e string zSigned
23f4c 49 64 65 6e 74 20 63 6f 6e 73 69 73 74 73 20 65  Ident consists e
23f4d 6e 74 69 72 65 6c 79 20 6f 66 20 61 6c 70 68 61  ntirely of alpha
23f4e 2d 6e 75 6d 65 72 69 63 0a 2a 2a 20 63 68 61 72  -numeric.** char
23f4f 61 63 74 65 72 73 2c 20 64 6f 65 73 20 6e 6f 74  acters, does not
23f50 20 62 65 67 69 6e 20 77 69 74 68 20 61 20 64 69   begin with a di
23f51 67 69 74 20 61 6e 64 20 69 73 20 6e 6f 74 20 61  git and is not a
23f52 6e 20 53 51 4c 20 6b 65 79 77 6f 72 64 2c 0a 2a  n SQL keyword,.*
23f53 2a 20 74 68 65 6e 20 69 74 20 69 73 20 63 6f 70  * then it is cop
23f54 69 65 64 20 74 6f 20 74 68 65 20 6f 75 74 70 75  ied to the outpu
23f55 74 20 62 75 66 66 65 72 20 65 78 61 63 74 6c 79  t buffer exactly
23f56 20 61 73 20 69 74 20 69 73 2e 20 4f 74 68 65 72   as it is. Other
23f57 77 69 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20 71  wise,.** it is q
23f58 75 6f 74 65 64 20 75 73 69 6e 67 20 64 6f 75 62  uoted using doub
23f59 6c 65 2d 71 75 6f 74 65 73 2e 0a 2a 2f 0a 73 74  le-quotes..*/.st
23f5a 61 74 69 63 20 76 6f 69 64 20 69 64 65 6e 74 50  atic void identP
23f5b 75 74 28 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  ut(char *z, int 
23f5c 2a 70 49 64 78 2c 20 63 68 61 72 20 2a 7a 53 69  *pIdx, char *zSi
23f5d 67 6e 65 64 49 64 65 6e 74 29 7b 0a 20 20 75 6e  gnedIdent){.  un
23f5e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 49 64  signed char *zId
23f5f 65 6e 74 20 3d 20 28 75 6e 73 69 67 6e 65 64 20  ent = (unsigned 
23f60 63 68 61 72 2a 29 7a 53 69 67 6e 65 64 49 64 65  char*)zSignedIde
23f61 6e 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20  nt;.  int i, j, 
23f62 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20 69 20 3d  needQuote;.  i =
23f63 20 2a 70 49 64 78 3b 0a 0a 20 20 66 6f 72 28 6a   *pIdx;..  for(j
23f64 3d 30 3b 20 7a 49 64 65 6e 74 5b 6a 5d 3b 20 6a  =0; zIdent[j]; j
23f65 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 21 73 71  ++){.    if( !sq
23f66 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28 7a 49 64  lite3Isalnum(zId
23f67 65 6e 74 5b 6a 5d 29 20 26 26 20 7a 49 64 65 6e  ent[j]) && zIden
23f68 74 5b 6a 5d 21 3d 27 5f 27 20 29 20 62 72 65 61  t[j]!='_' ) brea
23f69 6b 3b 0a 20 20 7d 0a 20 20 6e 65 65 64 51 75 6f  k;.  }.  needQuo
23f6a 74 65 20 3d 20 73 71 6c 69 74 65 33 49 73 64 69  te = sqlite3Isdi
23f6b 67 69 74 28 7a 49 64 65 6e 74 5b 30 5d 29 20 7c  git(zIdent[0]) |
23f6c 7c 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64  | sqlite3Keyword
23f6d 43 6f 64 65 28 7a 49 64 65 6e 74 2c 20 6a 29 21  Code(zIdent, j)!
23f6e 3d 54 4b 5f 49 44 3b 0a 20 20 69 66 28 20 21 6e  =TK_ID;.  if( !n
23f6f 65 65 64 51 75 6f 74 65 20 29 7b 0a 20 20 20 20  eedQuote ){.    
23f70 6e 65 65 64 51 75 6f 74 65 20 3d 20 7a 49 64 65  needQuote = zIde
23f71 6e 74 5b 6a 5d 3b 0a 20 20 7d 0a 0a 20 20 69 66  nt[j];.  }..  if
23f72 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 7a 5b  ( needQuote ) z[
23f73 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20 20 66 6f  i++] = '"';.  fo
23f74 72 28 6a 3d 30 3b 20 7a 49 64 65 6e 74 5b 6a 5d  r(j=0; zIdent[j]
23f75 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 7a 5b 69 2b  ; j++){.    z[i+
23f76 2b 5d 20 3d 20 7a 49 64 65 6e 74 5b 6a 5d 3b 0a  +] = zIdent[j];.
23f77 20 20 20 20 69 66 28 20 7a 49 64 65 6e 74 5b 6a      if( zIdent[j
23f78 5d 3d 3d 27 22 27 20 29 20 7a 5b 69 2b 2b 5d 20  ]=='"' ) z[i++] 
23f79 3d 20 27 22 27 3b 0a 20 20 7d 0a 20 20 69 66 28  = '"';.  }.  if(
23f7a 20 6e 65 65 64 51 75 6f 74 65 20 29 20 7a 5b 69   needQuote ) z[i
23f7b 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20 20 7a 5b 69  ++] = '"';.  z[i
23f7c 5d 20 3d 20 30 3b 0a 20 20 2a 70 49 64 78 20 3d  ] = 0;.  *pIdx =
23f7d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e   i;.}../*.** Gen
23f7e 65 72 61 74 65 20 61 20 43 52 45 41 54 45 20 54  erate a CREATE T
23f7f 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 61  ABLE statement a
23f80 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74  ppropriate for t
23f81 68 65 20 67 69 76 65 6e 0a 2a 2a 20 74 61 62 6c  he given.** tabl
23f82 65 2e 20 20 4d 65 6d 6f 72 79 20 74 6f 20 68 6f  e.  Memory to ho
23f83 6c 64 20 74 68 65 20 74 65 78 74 20 6f 66 20 74  ld the text of t
23f84 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
23f85 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d  obtained.** from
23f86 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20   sqliteMalloc() 
23f87 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72 65 65  and must be free
23f88 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67  d by the calling
23f89 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74   function..*/.st
23f8a 61 74 69 63 20 63 68 61 72 20 2a 63 72 65 61 74  atic char *creat
23f8b 65 54 61 62 6c 65 53 74 6d 74 28 73 71 6c 69 74  eTableStmt(sqlit
23f8c 65 33 20 2a 64 62 2c 20 54 61 62 6c 65 20 2a 70  e3 *db, Table *p
23f8d 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6b 2c 20 6e  ){.  int i, k, n
23f8e 3b 0a 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b  ;.  char *zStmt;
23f8f 0a 20 20 63 68 61 72 20 2a 7a 53 65 70 2c 20 2a  .  char *zSep, *
23f90 7a 53 65 70 32 2c 20 2a 7a 45 6e 64 3b 0a 20 20  zSep2, *zEnd;.  
23f91 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20  Column *pCol;.  
23f92 6e 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 43 6f  n = 0;.  for(pCo
23f93 6c 20 3d 20 70 2d 3e 61 43 6f 6c 2c 20 69 3d 30  l = p->aCol, i=0
23f94 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  ; i<p->nCol; i++
23f95 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 6e  , pCol++){.    n
23f96 20 2b 3d 20 69 64 65 6e 74 4c 65 6e 67 74 68 28   += identLength(
23f97 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 20 2b 20 35  pCol->zName) + 5
23f98 3b 0a 20 20 7d 0a 20 20 6e 20 2b 3d 20 69 64 65  ;.  }.  n += ide
23f99 6e 74 4c 65 6e 67 74 68 28 70 2d 3e 7a 4e 61 6d  ntLength(p->zNam
23f9a 65 29 3b 0a 20 20 69 66 28 20 6e 3c 35 30 20 29  e);.  if( n<50 )
23f9b 7b 20 0a 20 20 20 20 7a 53 65 70 20 3d 20 22 22  { .    zSep = ""
23f9c 3b 0a 20 20 20 20 7a 53 65 70 32 20 3d 20 22 2c  ;.    zSep2 = ",
23f9d 22 3b 0a 20 20 20 20 7a 45 6e 64 20 3d 20 22 29  ";.    zEnd = ")
23f9e 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ";.  }else{.    
23f9f 7a 53 65 70 20 3d 20 22 5c 6e 20 20 22 3b 0a 20  zSep = "\n  ";. 
23fa0 20 20 20 7a 53 65 70 32 20 3d 20 22 2c 5c 6e 20     zSep2 = ",\n 
23fa1 20 22 3b 0a 20 20 20 20 7a 45 6e 64 20 3d 20 22   ";.    zEnd = "
23fa2 5c 6e 29 22 3b 0a 20 20 7d 0a 20 20 6e 20 2b 3d  \n)";.  }.  n +=
23fa3 20 33 35 20 2b 20 36 2a 70 2d 3e 6e 43 6f 6c 3b   35 + 6*p->nCol;
23fa4 0a 20 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74  .  zStmt = sqlit
23fa5 65 33 4d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20  e3Malloc( n );. 
23fa6 20 69 66 28 20 7a 53 74 6d 74 3d 3d 30 20 29 7b   if( zStmt==0 ){
23fa7 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  .    db->mallocF
23fa8 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 72  ailed = 1;.    r
23fa9 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73  eturn 0;.  }.  s
23faa 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
23fab 6e 2c 20 7a 53 74 6d 74 2c 20 22 43 52 45 41 54  n, zStmt, "CREAT
23fac 45 20 54 41 42 4c 45 20 22 29 3b 0a 20 20 6b 20  E TABLE ");.  k 
23fad 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
23fae 30 28 7a 53 74 6d 74 29 3b 0a 20 20 69 64 65 6e  0(zStmt);.  iden
23faf 74 50 75 74 28 7a 53 74 6d 74 2c 20 26 6b 2c 20  tPut(zStmt, &k, 
23fb0 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7a 53 74  p->zName);.  zSt
23fb1 6d 74 5b 6b 2b 2b 5d 20 3d 20 27 28 27 3b 0a 20  mt[k++] = '(';. 
23fb2 20 66 6f 72 28 70 43 6f 6c 3d 70 2d 3e 61 43 6f   for(pCol=p->aCo
23fb3 6c 2c 20 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f  l, i=0; i<p->nCo
23fb4 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b  l; i++, pCol++){
23fb5 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
23fb6 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 61  t char * const a
23fb7 7a 54 79 70 65 5b 5d 20 3d 20 7b 0a 20 20 20 20  zType[] = {.    
23fb8 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 46      /* SQLITE_AF
23fb9 46 5f 54 45 58 54 20 20 20 20 2a 2f 20 22 20 54  F_TEXT    */ " T
23fba 45 58 54 22 2c 0a 20 20 20 20 20 20 20 20 2f 2a  EXT",.        /*
23fbb 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45   SQLITE_AFF_NONE
23fbc 20 20 20 20 2a 2f 20 22 22 2c 0a 20 20 20 20 20      */ "",.     
23fbd 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 46 46     /* SQLITE_AFF
23fbe 5f 4e 55 4d 45 52 49 43 20 2a 2f 20 22 20 4e 55  _NUMERIC */ " NU
23fbf 4d 22 2c 0a 20 20 20 20 20 20 20 20 2f 2a 20 53  M",.        /* S
23fc0 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
23fc1 52 20 2a 2f 20 22 20 49 4e 54 22 2c 0a 20 20 20  R */ " INT",.   
23fc2 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41       /* SQLITE_A
23fc3 46 46 5f 52 45 41 4c 20 20 20 20 2a 2f 20 22 20  FF_REAL    */ " 
23fc4 52 45 41 4c 22 0a 20 20 20 20 7d 3b 0a 20 20 20  REAL".    };.   
23fc5 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20 20 63 6f   int len;.    co
23fc6 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 3b  nst char *zType;
23fc7 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  ..    sqlite3_sn
23fc8 70 72 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74  printf(n-k, &zSt
23fc9 6d 74 5b 6b 5d 2c 20 7a 53 65 70 29 3b 0a 20 20  mt[k], zSep);.  
23fca 20 20 6b 20 2b 3d 20 73 71 6c 69 74 65 33 53 74    k += sqlite3St
23fcb 72 6c 65 6e 33 30 28 26 7a 53 74 6d 74 5b 6b 5d  rlen30(&zStmt[k]
23fcc 29 3b 0a 20 20 20 20 7a 53 65 70 20 3d 20 7a 53  );.    zSep = zS
23fcd 65 70 32 3b 0a 20 20 20 20 69 64 65 6e 74 50 75  ep2;.    identPu
23fce 74 28 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 43 6f  t(zStmt, &k, pCo
23fcf 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 61  l->zName);.    a
23fd0 73 73 65 72 74 28 20 70 43 6f 6c 2d 3e 61 66 66  ssert( pCol->aff
23fd1 69 6e 69 74 79 2d 53 51 4c 49 54 45 5f 41 46 46  inity-SQLITE_AFF
23fd2 5f 54 45 58 54 20 3e 3d 20 30 20 29 3b 0a 20 20  _TEXT >= 0 );.  
23fd3 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 2d 3e    assert( pCol->
23fd4 61 66 66 69 6e 69 74 79 2d 53 51 4c 49 54 45 5f  affinity-SQLITE_
23fd5 41 46 46 5f 54 45 58 54 20 3c 20 73 69 7a 65 6f  AFF_TEXT < sizeo
23fd6 66 28 61 7a 54 79 70 65 29 2f 73 69 7a 65 6f 66  f(azType)/sizeof
23fd7 28 61 7a 54 79 70 65 5b 30 5d 29 20 29 3b 0a 20  (azType[0]) );. 
23fd8 20 20 20 74 65 73 74 63 61 73 65 28 20 70 43 6f     testcase( pCo
23fd9 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c  l->affinity==SQL
23fda 49 54 45 5f 41 46 46 5f 54 45 58 54 20 29 3b 0a  ITE_AFF_TEXT );.
23fdb 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 43      testcase( pC
23fdc 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51  ol->affinity==SQ
23fdd 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 3b  LITE_AFF_NONE );
23fde 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70  .    testcase( p
23fdf 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53  Col->affinity==S
23fe0 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
23fe1 43 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  C );.    testcas
23fe2 65 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74  e( pCol->affinit
23fe3 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e  y==SQLITE_AFF_IN
23fe4 54 45 47 45 52 20 29 3b 0a 20 20 20 20 74 65 73  TEGER );.    tes
23fe5 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 66 66  tcase( pCol->aff
23fe6 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46  inity==SQLITE_AF
23fe7 46 5f 52 45 41 4c 20 29 3b 0a 20 20 20 20 0a 20  F_REAL );.    . 
23fe8 20 20 20 7a 54 79 70 65 20 3d 20 61 7a 54 79 70     zType = azTyp
23fe9 65 5b 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79  e[pCol->affinity
23fea 20 2d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45   - SQLITE_AFF_TE
23feb 58 54 5d 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 73  XT];.    len = s
23fec 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
23fed 54 79 70 65 29 3b 0a 20 20 20 20 61 73 73 65 72  Type);.    asser
23fee 74 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74  t( pCol->affinit
23fef 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  y==SQLITE_AFF_NO
23ff0 4e 45 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  NE .            
23ff1 7c 7c 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74  || pCol->affinit
23ff2 79 3d 3d 73 71 6c 69 74 65 33 41 66 66 69 6e 69  y==sqlite3Affini
23ff3 74 79 54 79 70 65 28 7a 54 79 70 65 29 20 29 3b  tyType(zType) );
23ff4 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 53 74  .    memcpy(&zSt
23ff5 6d 74 5b 6b 5d 2c 20 7a 54 79 70 65 2c 20 6c 65  mt[k], zType, le
23ff6 6e 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 6c 65 6e  n);.    k += len
23ff7 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6b 3c  ;.    assert( k<
23ff8 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  =n );.  }.  sqli
23ff9 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 2d 6b  te3_snprintf(n-k
23ffa 2c 20 26 7a 53 74 6d 74 5b 6b 5d 2c 20 22 25 73  , &zStmt[k], "%s
23ffb 22 2c 20 7a 45 6e 64 29 3b 0a 20 20 72 65 74 75  ", zEnd);.  retu
23ffc 72 6e 20 7a 53 74 6d 74 3b 0a 7d 0a 0a 2f 2a 0a  rn zStmt;.}../*.
23ffd 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
23ffe 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65 70  is called to rep
23fff 6f 72 74 20 74 68 65 20 66 69 6e 61 6c 20 22 29  ort the final ")
24000 22 20 74 68 61 74 20 74 65 72 6d 69 6e 61 74 65  " that terminate
24001 73 0a 2a 2a 20 61 20 43 52 45 41 54 45 20 54 41  s.** a CREATE TA
24002 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  BLE statement..*
24003 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 73  *.** The table s
24004 74 72 75 63 74 75 72 65 20 74 68 61 74 20 6f 74  tructure that ot
24005 68 65 72 20 61 63 74 69 6f 6e 20 72 6f 75 74 69  her action routi
24006 6e 65 73 20 68 61 76 65 20 62 65 65 6e 20 62 75  nes have been bu
24007 69 6c 64 69 6e 67 0a 2a 2a 20 69 73 20 61 64 64  ilding.** is add
24008 65 64 20 74 6f 20 74 68 65 20 69 6e 74 65 72 6e  ed to the intern
24009 61 6c 20 68 61 73 68 20 74 61 62 6c 65 73 2c 20  al hash tables, 
2400a 61 73 73 75 6d 69 6e 67 20 6e 6f 20 65 72 72 6f  assuming no erro
2400b 72 73 20 68 61 76 65 0a 2a 2a 20 6f 63 63 75 72  rs have.** occur
2400c 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 6e  red..**.** An en
2400d 74 72 79 20 66 6f 72 20 74 68 65 20 74 61 62 6c  try for the tabl
2400e 65 20 69 73 20 6d 61 64 65 20 69 6e 20 74 68 65  e is made in the
2400f 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 6f 6e   master table on
24010 20 64 69 73 6b 2c 20 75 6e 6c 65 73 73 0a 2a 2a   disk, unless.**
24011 20 74 68 69 73 20 69 73 20 61 20 74 65 6d 70 6f   this is a tempo
24012 72 61 72 79 20 74 61 62 6c 65 20 6f 72 20 64 62  rary table or db
24013 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 31 2e 20  ->init.busy==1. 
24014 20 57 68 65 6e 20 64 62 2d 3e 69 6e 69 74 2e 62   When db->init.b
24015 75 73 79 3d 3d 31 0a 2a 2a 20 69 74 20 6d 65 61  usy==1.** it mea
24016 6e 73 20 77 65 20 61 72 65 20 72 65 61 64 69 6e  ns we are readin
24017 67 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  g the sqlite_mas
24018 74 65 72 20 74 61 62 6c 65 20 62 65 63 61 75 73  ter table becaus
24019 65 20 77 65 20 6a 75 73 74 0a 2a 2a 20 63 6f 6e  e we just.** con
2401a 6e 65 63 74 65 64 20 74 6f 20 74 68 65 20 64 61  nected to the da
2401b 74 61 62 61 73 65 20 6f 72 20 62 65 63 61 75 73  tabase or becaus
2401c 65 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  e the sqlite_mas
2401d 74 65 72 20 74 61 62 6c 65 20 68 61 73 0a 2a 2a  ter table has.**
2401e 20 72 65 63 65 6e 74 6c 79 20 63 68 61 6e 67 65   recently change
2401f 64 2c 20 73 6f 20 74 68 65 20 65 6e 74 72 79 20  d, so the entry 
24020 66 6f 72 20 74 68 69 73 20 74 61 62 6c 65 20 61  for this table a
24021 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 69 6e  lready exists in
24022 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 5f 6d  .** the sqlite_m
24023 61 73 74 65 72 20 74 61 62 6c 65 2e 20 20 57 65  aster table.  We
24024 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 6f 20   do not want to 
24025 63 72 65 61 74 65 20 69 74 20 61 67 61 69 6e 2e  create it again.
24026 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 53  .**.** If the pS
24027 65 6c 65 63 74 20 61 72 67 75 6d 65 6e 74 20 69  elect argument i
24028 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 74 20 6d  s not NULL, it m
24029 65 61 6e 73 20 74 68 61 74 20 74 68 69 73 20 72  eans that this r
2402a 6f 75 74 69 6e 65 0a 2a 2a 20 77 61 73 20 63 61  outine.** was ca
2402b 6c 6c 65 64 20 74 6f 20 63 72 65 61 74 65 20 61  lled to create a
2402c 20 74 61 62 6c 65 20 67 65 6e 65 72 61 74 65 64   table generated
2402d 20 66 72 6f 6d 20 61 20 0a 2a 2a 20 22 43 52 45   from a .** "CRE
2402e 41 54 45 20 54 41 42 4c 45 20 2e 2e 2e 20 41 53  ATE TABLE ... AS
2402f 20 53 45 4c 45 43 54 20 2e 2e 2e 22 20 73 74 61   SELECT ..." sta
24030 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 63 6f 6c  tement.  The col
24031 75 6d 6e 20 6e 61 6d 65 73 20 6f 66 0a 2a 2a 20  umn names of.** 
24032 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 77 69  the new table wi
24033 6c 6c 20 6d 61 74 63 68 20 74 68 65 20 72 65 73  ll match the res
24034 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 53  ult set of the S
24035 45 4c 45 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ELECT..*/.SQLITE
24036 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
24037 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 0a 20  lite3EndTable(. 
24038 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
24039 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
2403a 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  e context */.  T
2403b 6f 6b 65 6e 20 2a 70 43 6f 6e 73 2c 20 20 20 20  oken *pCons,    
2403c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 27 2c         /* The ',
2403d 27 20 74 6f 6b 65 6e 20 61 66 74 65 72 20 74 68  ' token after th
2403e 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 64 65  e last column de
2403f 66 6e 2e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  fn. */.  Token *
24040 70 45 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20  pEnd,           
24041 20 2f 2a 20 54 68 65 20 66 69 6e 61 6c 20 27 29   /* The final ')
24042 27 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 43  ' token in the C
24043 52 45 41 54 45 20 54 41 42 4c 45 20 2a 2f 0a 20  REATE TABLE */. 
24044 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
24045 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 6c 65           /* Sele
24046 63 74 20 66 72 6f 6d 20 61 20 22 43 52 45 41 54  ct from a "CREAT
24047 45 20 2e 2e 2e 20 41 53 20 53 45 4c 45 43 54 22  E ... AS SELECT"
24048 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a   */.){.  Table *
24049 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  p;.  sqlite3 *db
2404a 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2404b 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20 69 66 28   int iDb;..  if(
2404c 20 28 70 45 6e 64 3d 3d 30 20 26 26 20 70 53 65   (pEnd==0 && pSe
2404d 6c 65 63 74 3d 3d 30 29 20 7c 7c 20 64 62 2d 3e  lect==0) || db->
2404e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
2404f 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
24050 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e    p = pParse->pN
24051 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70  ewTable;.  if( p
24052 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20  ==0 ) return;.. 
24053 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 69 6e   assert( !db->in
24054 69 74 2e 62 75 73 79 20 7c 7c 20 21 70 53 65 6c  it.busy || !pSel
24055 65 63 74 20 29 3b 0a 0a 20 20 69 44 62 20 3d 20  ect );..  iDb = 
24056 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
24057 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70 53 63 68  ndex(db, p->pSch
24058 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  ema);..#ifndef S
24059 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b  QLITE_OMIT_CHECK
2405a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 6e 61  .  /* Resolve na
2405b 6d 65 73 20 69 6e 20 61 6c 6c 20 43 48 45 43 4b  mes in all CHECK
2405c 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 78 70 72   constraint expr
2405d 65 73 73 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20  essions..  */.  
2405e 69 66 28 20 70 2d 3e 70 43 68 65 63 6b 20 29 7b  if( p->pCheck ){
2405f 0a 20 20 20 20 53 72 63 4c 69 73 74 20 73 53 72  .    SrcList sSr
24060 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
24061 20 20 20 20 20 2f 2a 20 46 61 6b 65 20 53 72 63       /* Fake Src
24062 4c 69 73 74 20 66 6f 72 20 70 50 61 72 73 65 2d  List for pParse-
24063 3e 70 4e 65 77 54 61 62 6c 65 20 2a 2f 0a 20 20  >pNewTable */.  
24064 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e    NameContext sN
24065 43 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  C;              
24066 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78    /* Name contex
24067 74 20 66 6f 72 20 70 50 61 72 73 65 2d 3e 70 4e  t for pParse->pN
24068 65 77 54 61 62 6c 65 20 2a 2f 0a 0a 20 20 20 20  ewTable */..    
24069 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20  memset(&sNC, 0, 
2406a 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20  sizeof(sNC));.  
2406b 20 20 6d 65 6d 73 65 74 28 26 73 53 72 63 2c 20    memset(&sSrc, 
2406c 30 2c 20 73 69 7a 65 6f 66 28 73 53 72 63 29 29  0, sizeof(sSrc))
2406d 3b 0a 20 20 20 20 73 53 72 63 2e 6e 53 72 63 20  ;.    sSrc.nSrc 
2406e 3d 20 31 3b 0a 20 20 20 20 73 53 72 63 2e 61 5b  = 1;.    sSrc.a[
2406f 30 5d 2e 7a 4e 61 6d 65 20 3d 20 70 2d 3e 7a 4e  0].zName = p->zN
24070 61 6d 65 3b 0a 20 20 20 20 73 53 72 63 2e 61 5b  ame;.    sSrc.a[
24071 30 5d 2e 70 54 61 62 20 3d 20 70 3b 0a 20 20 20  0].pTab = p;.   
24072 20 73 53 72 63 2e 61 5b 30 5d 2e 69 43 75 72 73   sSrc.a[0].iCurs
24073 6f 72 20 3d 20 2d 31 3b 0a 20 20 20 20 73 4e 43  or = -1;.    sNC
24074 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
24075 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69  ;.    sNC.pSrcLi
24076 73 74 20 3d 20 26 73 53 72 63 3b 0a 20 20 20 20  st = &sSrc;.    
24077 73 4e 43 2e 69 73 43 68 65 63 6b 20 3d 20 31 3b  sNC.isCheck = 1;
24078 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
24079 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
2407a 28 26 73 4e 43 2c 20 70 2d 3e 70 43 68 65 63 6b  (&sNC, p->pCheck
2407b 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
2407c 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  n;.    }.  }.#en
2407d 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
2407e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43  SQLITE_OMIT_CHEC
2407f 4b 29 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74  K) */..  /* If t
24080 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  he db->init.busy
24081 20 69 73 20 31 20 69 74 20 6d 65 61 6e 73 20 77   is 1 it means w
24082 65 20 61 72 65 20 72 65 61 64 69 6e 67 20 74 68  e are reading th
24083 65 20 53 51 4c 20 6f 66 66 20 74 68 65 0a 20 20  e SQL off the.  
24084 2a 2a 20 22 73 71 6c 69 74 65 5f 6d 61 73 74 65  ** "sqlite_maste
24085 72 22 20 6f 72 20 22 73 71 6c 69 74 65 5f 74 65  r" or "sqlite_te
24086 6d 70 5f 6d 61 73 74 65 72 22 20 74 61 62 6c 65  mp_master" table
24087 20 6f 6e 20 74 68 65 20 64 69 73 6b 2e 0a 20 20   on the disk..  
24088 2a 2a 20 53 6f 20 64 6f 20 6e 6f 74 20 77 72 69  ** So do not wri
24089 74 65 20 74 6f 20 74 68 65 20 64 69 73 6b 20 61  te to the disk a
2408a 67 61 69 6e 2e 20 20 45 78 74 72 61 63 74 20 74  gain.  Extract t
2408b 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d  he root page num
2408c 62 65 72 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65  ber.  ** for the
2408d 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20   table from the 
2408e 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d  db->init.newTnum
2408f 20 66 69 65 6c 64 2e 20 20 28 54 68 65 20 70 61   field.  (The pa
24090 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 73  ge number.  ** s
24091 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20  hould have been 
24092 70 75 74 20 74 68 65 72 65 20 62 79 20 74 68 65  put there by the
24093 20 73 71 6c 69 74 65 4f 70 65 6e 43 62 20 72 6f   sqliteOpenCb ro
24094 75 74 69 6e 65 2e 29 0a 20 20 2a 2f 0a 20 20 69  utine.).  */.  i
24095 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  f( db->init.busy
24096 20 29 7b 0a 20 20 20 20 70 2d 3e 74 6e 75 6d 20   ){.    p->tnum 
24097 3d 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e  = db->init.newTn
24098 75 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  um;.  }..  /* If
24099 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e   not initializin
2409a 67 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61  g, then create a
2409b 20 72 65 63 6f 72 64 20 66 6f 72 20 74 68 65 20   record for the 
2409c 6e 65 77 20 74 61 62 6c 65 0a 20 20 2a 2a 20 69  new table.  ** i
2409d 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53  n the SQLITE_MAS
2409e 54 45 52 20 74 61 62 6c 65 20 6f 66 20 74 68 65  TER table of the
2409f 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a 0a   database..  **.
240a0 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20    ** If this is 
240a1 61 20 54 45 4d 50 4f 52 41 52 59 20 74 61 62 6c  a TEMPORARY tabl
240a2 65 2c 20 77 72 69 74 65 20 74 68 65 20 65 6e 74  e, write the ent
240a3 72 79 20 69 6e 74 6f 20 74 68 65 20 61 75 78 69  ry into the auxi
240a4 6c 69 61 72 79 0a 20 20 2a 2a 20 66 69 6c 65 20  liary.  ** file 
240a5 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 74 6f 20  instead of into 
240a6 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
240a7 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69  e file..  */.  i
240a8 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73  f( !db->init.bus
240a9 79 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a  y ){.    int n;.
240aa 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20      Vdbe *v;.   
240ab 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20 20   char *zType;   
240ac 20 2f 2a 20 22 76 69 65 77 22 20 6f 72 20 22 74   /* "view" or "t
240ad 61 62 6c 65 22 20 2a 2f 0a 20 20 20 20 63 68 61  able" */.    cha
240ae 72 20 2a 7a 54 79 70 65 32 3b 20 20 20 2f 2a 20  r *zType2;   /* 
240af 22 56 49 45 57 22 20 6f 72 20 22 54 41 42 4c 45  "VIEW" or "TABLE
240b0 22 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a  " */.    char *z
240b1 53 74 6d 74 3b 20 20 20 20 2f 2a 20 54 65 78 74  Stmt;    /* Text
240b2 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54   of the CREATE T
240b3 41 42 4c 45 20 6f 72 20 43 52 45 41 54 45 20 56  ABLE or CREATE V
240b4 49 45 57 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  IEW statement */
240b5 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65  ..    v = sqlite
240b6 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
240b7 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28  ;.    if( NEVER(
240b8 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a  v==0) ) return;.
240b9 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
240ba 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
240bb 73 65 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20  se, 0);..    /* 
240bc 0a 20 20 20 20 2a 2a 20 49 6e 69 74 69 61 6c 69  .    ** Initiali
240bd 7a 65 20 7a 54 79 70 65 20 66 6f 72 20 74 68 65  ze zType for the
240be 20 6e 65 77 20 76 69 65 77 20 6f 72 20 74 61 62   new view or tab
240bf 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  le..    */.    i
240c0 66 28 20 70 2d 3e 70 53 65 6c 65 63 74 3d 3d 30  f( p->pSelect==0
240c1 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 72   ){.      /* A r
240c2 65 67 75 6c 61 72 20 74 61 62 6c 65 20 2a 2f 0a  egular table */.
240c3 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 74        zType = "t
240c4 61 62 6c 65 22 3b 0a 20 20 20 20 20 20 7a 54 79  able";.      zTy
240c5 70 65 32 20 3d 20 22 54 41 42 4c 45 22 3b 0a 23  pe2 = "TABLE";.#
240c6 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
240c7 49 54 5f 56 49 45 57 0a 20 20 20 20 7d 65 6c 73  IT_VIEW.    }els
240c8 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 76 69  e{.      /* A vi
240c9 65 77 20 2a 2f 0a 20 20 20 20 20 20 7a 54 79 70  ew */.      zTyp
240ca 65 20 3d 20 22 76 69 65 77 22 3b 0a 20 20 20 20  e = "view";.    
240cb 20 20 7a 54 79 70 65 32 20 3d 20 22 56 49 45 57    zType2 = "VIEW
240cc 22 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a  ";.#endif.    }.
240cd 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20  .    /* If this 
240ce 69 73 20 61 20 43 52 45 41 54 45 20 54 41 42 4c  is a CREATE TABL
240cf 45 20 78 78 20 41 53 20 53 45 4c 45 43 54 20 2e  E xx AS SELECT .
240d0 2e 2e 2c 20 65 78 65 63 75 74 65 20 74 68 65 20  .., execute the 
240d1 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 73 74  SELECT.    ** st
240d2 61 74 65 6d 65 6e 74 20 74 6f 20 70 6f 70 75 6c  atement to popul
240d3 61 74 65 20 74 68 65 20 6e 65 77 20 74 61 62 6c  ate the new tabl
240d4 65 2e 20 54 68 65 20 72 6f 6f 74 2d 70 61 67 65  e. The root-page
240d5 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 0a   number for the.
240d6 20 20 20 20 2a 2a 20 6e 65 77 20 74 61 62 6c 65      ** new table
240d7 20 69 73 20 69 6e 20 72 65 67 69 73 74 65 72 20   is in register 
240d8 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2e  pParse->regRoot.
240d9 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f  .    **.    ** O
240da 6e 63 65 20 74 68 65 20 53 45 4c 45 43 54 20 68  nce the SELECT h
240db 61 73 20 62 65 65 6e 20 63 6f 64 65 64 20 62 79  as been coded by
240dc 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29   sqlite3Select()
240dd 2c 20 69 74 20 69 73 20 69 6e 20 61 0a 20 20 20  , it is in a.   
240de 20 2a 2a 20 73 75 69 74 61 62 6c 65 20 73 74 61   ** suitable sta
240df 74 65 20 74 6f 20 71 75 65 72 79 20 66 6f 72 20  te to query for 
240e0 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  the column names
240e1 20 61 6e 64 20 74 79 70 65 73 20 74 6f 20 62 65   and types to be
240e2 20 75 73 65 64 0a 20 20 20 20 2a 2a 20 62 79 20   used.    ** by 
240e3 74 68 65 20 6e 65 77 20 74 61 62 6c 65 2e 0a 20  the new table.. 
240e4 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 20 73     **.    ** A s
240e5 68 61 72 65 64 2d 63 61 63 68 65 20 77 72 69 74  hared-cache writ
240e6 65 2d 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 72 65  e-lock is not re
240e7 71 75 69 72 65 64 20 74 6f 20 77 72 69 74 65 20  quired to write 
240e8 74 6f 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  to the new table
240e9 2c 0a 20 20 20 20 2a 2a 20 61 73 20 61 20 73 63  ,.    ** as a sc
240ea 68 65 6d 61 2d 6c 6f 63 6b 20 6d 75 73 74 20 68  hema-lock must h
240eb 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e  ave already been
240ec 20 6f 62 74 61 69 6e 65 64 20 74 6f 20 63 72 65   obtained to cre
240ed 61 74 65 20 69 74 2e 20 53 69 6e 63 65 0a 20 20  ate it. Since.  
240ee 20 20 2a 2a 20 61 20 73 63 68 65 6d 61 2d 6c 6f    ** a schema-lo
240ef 63 6b 20 65 78 63 6c 75 64 65 73 20 61 6c 6c 20  ck excludes all 
240f0 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 75  other database u
240f1 73 65 72 73 2c 20 74 68 65 20 77 72 69 74 65 2d  sers, the write-
240f2 6c 6f 63 6b 20 77 6f 75 6c 64 0a 20 20 20 20 2a  lock would.    *
240f3 2a 20 62 65 20 72 65 64 75 6e 64 61 6e 74 2e 0a  * be redundant..
240f4 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
240f5 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20  Select ){.      
240f6 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b  SelectDest dest;
240f7 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 53  .      Table *pS
240f8 65 6c 54 61 62 3b 0a 0a 20 20 20 20 20 20 61 73  elTab;..      as
240f9 73 65 72 74 28 70 50 61 72 73 65 2d 3e 6e 54 61  sert(pParse->nTa
240fa 62 3d 3d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  b==1);.      sql
240fb 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
240fc 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20  , OP_OpenWrite, 
240fd 31 2c 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f  1, pParse->regRo
240fe 6f 74 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20  ot, iDb);.      
240ff 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
24100 65 50 35 28 76 2c 20 31 29 3b 0a 20 20 20 20 20  eP5(v, 1);.     
24101 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d 20   pParse->nTab = 
24102 32 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  2;.      sqlite3
24103 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26  SelectDestInit(&
24104 64 65 73 74 2c 20 53 52 54 5f 54 61 62 6c 65 2c  dest, SRT_Table,
24105 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   1);.      sqlit
24106 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
24107 20 70 53 65 6c 65 63 74 2c 20 26 64 65 73 74 29   pSelect, &dest)
24108 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
24109 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2410a 43 6c 6f 73 65 2c 20 31 29 3b 0a 20 20 20 20 20  Close, 1);.     
2410b 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
2410c 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  r==0 ){.        
2410d 70 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74 65  pSelTab = sqlite
2410e 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65  3ResultSetOfSele
2410f 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65  ct(pParse, pSele
24110 63 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ct);.        if(
24111 20 70 53 65 6c 54 61 62 3d 3d 30 20 29 20 72 65   pSelTab==0 ) re
24112 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 61 73  turn;.        as
24113 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 3d 3d 30  sert( p->aCol==0
24114 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e   );.        p->n
24115 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e 6e  Col = pSelTab->n
24116 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  Col;.        p->
24117 61 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e  aCol = pSelTab->
24118 61 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 53  aCol;.        pS
24119 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 30 3b  elTab->nCol = 0;
2411a 0a 20 20 20 20 20 20 20 20 70 53 65 6c 54 61 62  .        pSelTab
2411b 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20  ->aCol = 0;.    
2411c 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
2411d 65 54 61 62 6c 65 28 70 53 65 6c 54 61 62 29 3b  eTable(pSelTab);
2411e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
2411f 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74      /* Compute t
24120 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74  he complete text
24121 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 73   of the CREATE s
24122 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20  tatement */.    
24123 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20  if( pSelect ){. 
24124 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 63 72 65       zStmt = cre
24125 61 74 65 54 61 62 6c 65 53 74 6d 74 28 64 62 2c  ateTableStmt(db,
24126 20 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a   p);.    }else{.
24127 20 20 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28        n = (int)(
24128 70 45 6e 64 2d 3e 7a 20 2d 20 70 50 61 72 73 65  pEnd->z - pParse
24129 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 29 20  ->sNameToken.z) 
2412a 2b 20 31 3b 0a 20 20 20 20 20 20 7a 53 74 6d 74  + 1;.      zStmt
2412b 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
2412c 66 28 64 62 2c 20 0a 20 20 20 20 20 20 20 20 20  f(db, .         
2412d 20 22 43 52 45 41 54 45 20 25 73 20 25 2e 2a 73   "CREATE %s %.*s
2412e 22 2c 20 7a 54 79 70 65 32 2c 20 6e 2c 20 70 50  ", zType2, n, pP
2412f 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e  arse->sNameToken
24130 2e 7a 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  .z.      );.    
24131 7d 0a 0a 20 20 20 20 2f 2a 20 41 20 73 6c 6f 74  }..    /* A slot
24132 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20   for the record 
24133 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
24134 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 74 68   allocated in th
24135 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54 45  e .    ** SQLITE
24136 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 20 20  _MASTER table.  
24137 57 65 20 6a 75 73 74 20 6e 65 65 64 20 74 6f 20  We just need to 
24138 75 70 64 61 74 65 20 74 68 61 74 20 73 6c 6f 74  update that slot
24139 20 77 69 74 68 20 61 6c 6c 0a 20 20 20 20 2a 2a   with all.    **
2413a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   the information
2413b 20 77 65 27 76 65 20 63 6f 6c 6c 65 63 74 65 64   we've collected
2413c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
2413d 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
2413e 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 22 55  pParse,.      "U
2413f 50 44 41 54 45 20 25 51 2e 25 73 20 22 0a 20 20  PDATE %Q.%s ".  
24140 20 20 20 20 20 20 20 22 53 45 54 20 74 79 70 65         "SET type
24141 3d 27 25 73 27 2c 20 6e 61 6d 65 3d 25 51 2c 20  ='%s', name=%Q, 
24142 74 62 6c 5f 6e 61 6d 65 3d 25 51 2c 20 72 6f 6f  tbl_name=%Q, roo
24143 74 70 61 67 65 3d 23 25 64 2c 20 73 71 6c 3d 25  tpage=#%d, sql=%
24144 51 20 22 0a 20 20 20 20 20 20 20 22 57 48 45 52  Q ".       "WHER
24145 45 20 72 6f 77 69 64 3d 23 25 64 22 2c 0a 20 20  E rowid=#%d",.  
24146 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d      db->aDb[iDb]
24147 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54  .zName, SCHEMA_T
24148 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20 20 20  ABLE(iDb),.     
24149 20 7a 54 79 70 65 2c 0a 20 20 20 20 20 20 70 2d   zType,.      p-
2414a 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70 2d  >zName,.      p-
2414b 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70 50  >zName,.      pP
2414c 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2c 0a 20  arse->regRoot,. 
2414d 20 20 20 20 20 7a 53 74 6d 74 2c 0a 20 20 20 20       zStmt,.    
2414e 20 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 77    pParse->regRow
2414f 69 64 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 71  id.    );.    sq
24150 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
24151 7a 53 74 6d 74 29 3b 0a 20 20 20 20 73 71 6c 69  zStmt);.    sqli
24152 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28  te3ChangeCookie(
24153 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 0a 23  pParse, iDb);..#
24154 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
24155 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  IT_AUTOINCREMENT
24156 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f  .    /* Check to
24157 20 73 65 65 20 69 66 20 77 65 20 6e 65 65 64 20   see if we need 
24158 74 6f 20 63 72 65 61 74 65 20 61 6e 20 73 71 6c  to create an sql
24159 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62  ite_sequence tab
2415a 6c 65 20 66 6f 72 0a 20 20 20 20 2a 2a 20 6b 65  le for.    ** ke
2415b 65 70 69 6e 67 20 74 72 61 63 6b 20 6f 66 20 61  eping track of a
2415c 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 6b 65 79  utoincrement key
2415d 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  s..    */.    if
2415e 28 20 70 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  ( p->tabFlags & 
2415f 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74  TF_Autoincrement
24160 20 29 7b 0a 20 20 20 20 20 20 44 62 20 2a 70 44   ){.      Db *pD
24161 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62  b = &db->aDb[iDb
24162 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44 62  ];.      if( pDb
24163 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54  ->pSchema->pSeqT
24164 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ab==0 ){.       
24165 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
24166 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20  rse(pParse,.    
24167 20 20 20 20 20 20 22 43 52 45 41 54 45 20 54 41        "CREATE TA
24168 42 4c 45 20 25 51 2e 73 71 6c 69 74 65 5f 73 65  BLE %Q.sqlite_se
24169 71 75 65 6e 63 65 28 6e 61 6d 65 2c 73 65 71 29  quence(name,seq)
2416a 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70 44 62  ",.          pDb
2416b 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20 20 20  ->zName.        
2416c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
2416d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
2416e 52 65 70 61 72 73 65 20 65 76 65 72 79 74 68 69  Reparse everythi
2416f 6e 67 20 74 6f 20 75 70 64 61 74 65 20 6f 75 72  ng to update our
24170 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 20 73   internal data s
24171 74 72 75 63 74 75 72 65 73 20 2a 2f 0a 20 20 20  tructures */.   
24172 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24173 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53 63  p4(v, OP_ParseSc
24174 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20 30 2c  hema, iDb, 0, 0,
24175 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
24176 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 74 62 6c  MPrintf(db, "tbl
24177 5f 6e 61 6d 65 3d 27 25 71 27 22 2c 70 2d 3e 7a  _name='%q'",p->z
24178 4e 61 6d 65 29 2c 20 50 34 5f 44 59 4e 41 4d 49  Name), P4_DYNAMI
24179 43 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41  C);.  }...  /* A
2417a 64 64 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  dd the table to 
2417b 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65  the in-memory re
2417c 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
2417d 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20  the database..  
2417e 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69  */.  if( db->ini
2417f 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 54 61  t.busy ){.    Ta
24180 62 6c 65 20 2a 70 4f 6c 64 3b 0a 20 20 20 20 53  ble *pOld;.    S
24181 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d  chema *pSchema =
24182 20 70 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 20   p->pSchema;.   
24183 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 48   pOld = sqlite3H
24184 61 73 68 49 6e 73 65 72 74 28 26 70 53 63 68 65  ashInsert(&pSche
24185 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 2d 3e  ma->tblHash, p->
24186 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20  zName,.         
24187 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24188 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 6c 65      sqlite3Strle
24189 6e 33 30 28 70 2d 3e 7a 4e 61 6d 65 29 2c 70 29  n30(p->zName),p)
2418a 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 20 29  ;.    if( pOld )
2418b 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
2418c 70 3d 3d 70 4f 6c 64 20 29 3b 20 20 2f 2a 20 4d  p==pOld );  /* M
2418d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61 76 65 20  alloc must have 
2418e 66 61 69 6c 65 64 20 69 6e 73 69 64 65 20 48 61  failed inside Ha
2418f 73 68 49 6e 73 65 72 74 28 29 20 2a 2f 0a 20 20  shInsert() */.  
24190 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
24191 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
24192 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
24193 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61    pParse->pNewTa
24194 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d  ble = 0;.    db-
24195 3e 6e 54 61 62 6c 65 2b 2b 3b 0a 20 20 20 20 64  >nTable++;.    d
24196 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49  b->flags |= SQLI
24197 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73  TE_InternChanges
24198 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
24199 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42 4c  E_OMIT_ALTERTABL
2419a 45 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 70 53  E.    if( !p->pS
2419b 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 63  elect ){.      c
2419c 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2419d 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
2419e 29 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f  )pParse->sNameTo
2419f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20 20 69 6e 74  ken.z;.      int
241a0 20 6e 4e 61 6d 65 3b 0a 20 20 20 20 20 20 61 73   nName;.      as
241a1 73 65 72 74 28 20 21 70 53 65 6c 65 63 74 20 26  sert( !pSelect &
241a2 26 20 70 43 6f 6e 73 20 26 26 20 70 45 6e 64 20  & pCons && pEnd 
241a3 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f  );.      if( pCo
241a4 6e 73 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20  ns->z==0 ){.    
241a5 20 20 20 20 70 43 6f 6e 73 20 3d 20 70 45 6e 64      pCons = pEnd
241a6 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
241a7 6e 4e 61 6d 65 20 3d 20 28 69 6e 74 29 28 28 63  nName = (int)((c
241a8 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 43 6f 6e  onst char *)pCon
241a9 73 2d 3e 7a 20 2d 20 7a 4e 61 6d 65 29 3b 0a 20  s->z - zName);. 
241aa 20 20 20 20 20 70 2d 3e 61 64 64 43 6f 6c 4f 66       p->addColOf
241ab 66 73 65 74 20 3d 20 31 33 20 2b 20 73 71 6c 69  fset = 13 + sqli
241ac 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 7a  te3Utf8CharLen(z
241ad 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20  Name, nName);.  
241ae 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d    }.#endif.  }.}
241af 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
241b0 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a 0a 2a 2a  _OMIT_VIEW./*.**
241b1 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c   The parser call
241b2 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  s this routine i
241b3 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61 74  n order to creat
241b4 65 20 61 20 6e 65 77 20 56 49 45 57 0a 2a 2f 0a  e a new VIEW.*/.
241b5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
241b6 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61 74  oid sqlite3Creat
241b7 65 56 69 65 77 28 0a 20 20 50 61 72 73 65 20 2a  eView(.  Parse *
241b8 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 54  pParse,     /* T
241b9 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65  he parsing conte
241ba 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  xt */.  Token *p
241bb 42 65 67 69 6e 2c 20 20 20 20 20 2f 2a 20 54 68  Begin,     /* Th
241bc 65 20 43 52 45 41 54 45 20 74 6f 6b 65 6e 20 74  e CREATE token t
241bd 68 61 74 20 62 65 67 69 6e 73 20 74 68 65 20 73  hat begins the s
241be 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 54 6f  tatement */.  To
241bf 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 20  ken *pName1,    
241c0 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20 74 68   /* The token th
241c1 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d  at holds the nam
241c2 65 20 6f 66 20 74 68 65 20 76 69 65 77 20 2a 2f  e of the view */
241c3 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32  .  Token *pName2
241c4 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b  ,     /* The tok
241c5 65 6e 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68  en that holds th
241c6 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 76 69  e name of the vi
241c7 65 77 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  ew */.  Select *
241c8 70 53 65 6c 65 63 74 2c 20 20 20 2f 2a 20 41 20  pSelect,   /* A 
241c9 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
241ca 20 74 68 61 74 20 77 69 6c 6c 20 62 65 63 6f 6d   that will becom
241cb 65 20 74 68 65 20 6e 65 77 20 76 69 65 77 20 2a  e the new view *
241cc 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 2c 20  /.  int isTemp, 
241cd 20 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 66         /* TRUE f
241ce 6f 72 20 61 20 54 45 4d 50 4f 52 41 52 59 20 76  or a TEMPORARY v
241cf 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45  iew */.  int noE
241d0 72 72 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  rr          /* S
241d1 75 70 70 72 65 73 73 20 65 72 72 6f 72 20 6d 65  uppress error me
241d2 73 73 61 67 65 73 20 69 66 20 56 49 45 57 20 61  ssages if VIEW a
241d3 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f  lready exists */
241d4 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a  .){.  Table *p;.
241d5 20 20 69 6e 74 20 6e 3b 0a 20 20 63 6f 6e 73 74    int n;.  const
241d6 20 63 68 61 72 20 2a 7a 3b 0a 20 20 54 6f 6b 65   char *z;.  Toke
241d7 6e 20 73 45 6e 64 3b 0a 20 20 44 62 46 69 78 65  n sEnd;.  DbFixe
241d8 72 20 73 46 69 78 3b 0a 20 20 54 6f 6b 65 6e 20  r sFix;.  Token 
241d9 2a 70 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 69 44  *pName;.  int iD
241da 62 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  b;.  sqlite3 *db
241db 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a   = pParse->db;..
241dc 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 56    if( pParse->nV
241dd 61 72 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  ar>0 ){.    sqli
241de 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
241df 73 65 2c 20 22 70 61 72 61 6d 65 74 65 72 73 20  se, "parameters 
241e0 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20  are not allowed 
241e1 69 6e 20 76 69 65 77 73 22 29 3b 0a 20 20 20 20  in views");.    
241e2 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
241e3 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29  ete(db, pSelect)
241e4 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
241e5 7d 0a 20 20 73 71 6c 69 74 65 33 53 74 61 72 74  }.  sqlite3Start
241e6 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 4e  Table(pParse, pN
241e7 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 69 73  ame1, pName2, is
241e8 54 65 6d 70 2c 20 31 2c 20 30 2c 20 6e 6f 45 72  Temp, 1, 0, noEr
241e9 72 29 3b 0a 20 20 70 20 3d 20 70 50 61 72 73 65  r);.  p = pParse
241ea 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69  ->pNewTable;.  i
241eb 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 73  f( p==0 ){.    s
241ec 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
241ed 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b  te(db, pSelect);
241ee 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
241ef 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
241f0 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 3b 20 2f 2a  e->nErr==0 ); /*
241f1 20 49 66 20 73 71 6c 69 74 65 33 53 74 61 72 74   If sqlite3Start
241f2 54 61 62 6c 65 20 72 65 74 75 72 6e 20 6e 6f 6e  Table return non
241f3 2d 4e 55 4c 4c 20 74 68 65 6e 0a 20 20 20 20 20  -NULL then.     
241f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
241f5 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 72 65          ** there
241f6 20 63 6f 75 6c 64 20 6e 6f 74 20 68 61 76 65 20   could not have 
241f7 62 65 65 6e 20 61 6e 20 65 72 72 6f 72 20 2a 2f  been an error */
241f8 0a 20 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72  .  sqlite3TwoPar
241f9 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e  tName(pParse, pN
241fa 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70  ame1, pName2, &p
241fb 4e 61 6d 65 29 3b 0a 20 20 69 44 62 20 3d 20 73  Name);.  iDb = s
241fc 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
241fd 64 65 78 28 64 62 2c 20 70 2d 3e 70 53 63 68 65  dex(db, p->pSche
241fe 6d 61 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ma);.  if( sqlit
241ff 65 33 46 69 78 49 6e 69 74 28 26 73 46 69 78 2c  e3FixInit(&sFix,
24200 20 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22 76   pParse, iDb, "v
24201 69 65 77 22 2c 20 70 4e 61 6d 65 29 0a 20 20 20  iew", pName).   
24202 20 26 26 20 73 71 6c 69 74 65 33 46 69 78 53 65   && sqlite3FixSe
24203 6c 65 63 74 28 26 73 46 69 78 2c 20 70 53 65 6c  lect(&sFix, pSel
24204 65 63 74 29 0a 20 20 29 7b 0a 20 20 20 20 73 71  ect).  ){.    sq
24205 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
24206 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a  e(db, pSelect);.
24207 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
24208 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 20 63 6f 70  .  /* Make a cop
24209 79 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20  y of the entire 
2420a 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2420b 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68   that defines th
2420c 65 20 76 69 65 77 2e 0a 20 20 2a 2a 20 54 68 69  e view..  ** Thi
2420d 73 20 77 69 6c 6c 20 66 6f 72 63 65 20 61 6c 6c  s will force all
2420e 20 74 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e 2e   the Expr.token.
2420f 7a 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 64  z values to be d
24210 79 6e 61 6d 69 63 61 6c 6c 79 0a 20 20 2a 2a 20  ynamically.  ** 
24211 61 6c 6c 6f 63 61 74 65 64 20 72 61 74 68 65 72  allocated rather
24212 20 74 68 61 6e 20 70 6f 69 6e 74 20 74 6f 20 74   than point to t
24213 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20  he input string 
24214 2d 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68  - which means th
24215 61 74 0a 20 20 2a 2a 20 74 68 65 79 20 77 69 6c  at.  ** they wil
24216 6c 20 70 65 72 73 69 73 74 20 61 66 74 65 72 20  l persist after 
24217 74 68 65 20 63 75 72 72 65 6e 74 20 73 71 6c 69  the current sqli
24218 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c 6c 20  te3_exec() call 
24219 72 65 74 75 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20  returns..  */.  
2421a 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c  p->pSelect = sql
2421b 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62  ite3SelectDup(db
2421c 2c 20 70 53 65 6c 65 63 74 2c 20 45 58 50 52 44  , pSelect, EXPRD
2421d 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 73 71  UP_REDUCE);.  sq
2421e 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
2421f 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a  e(db, pSelect);.
24220 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
24221 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65  Failed ){.    re
24222 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20  turn;.  }.  if( 
24223 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29  !db->init.busy )
24224 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 69 65  {.    sqlite3Vie
24225 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
24226 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 7d 0a  pParse, p);.  }.
24227 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65  .  /* Locate the
24228 20 65 6e 64 20 6f 66 20 74 68 65 20 43 52 45 41   end of the CREA
24229 54 45 20 56 49 45 57 20 73 74 61 74 65 6d 65 6e  TE VIEW statemen
2422a 74 2e 20 20 4d 61 6b 65 20 73 45 6e 64 20 70 6f  t.  Make sEnd po
2422b 69 6e 74 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20  int to.  ** the 
2422c 65 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73 45 6e 64  end..  */.  sEnd
2422d 20 3d 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74   = pParse->sLast
2422e 54 6f 6b 65 6e 3b 0a 20 20 69 66 28 20 41 4c 57  Token;.  if( ALW
2422f 41 59 53 28 73 45 6e 64 2e 7a 5b 30 5d 21 3d 30  AYS(sEnd.z[0]!=0
24230 29 20 26 26 20 73 45 6e 64 2e 7a 5b 30 5d 21 3d  ) && sEnd.z[0]!=
24231 27 3b 27 20 29 7b 0a 20 20 20 20 73 45 6e 64 2e  ';' ){.    sEnd.
24232 7a 20 2b 3d 20 73 45 6e 64 2e 6e 3b 0a 20 20 7d  z += sEnd.n;.  }
24233 0a 20 20 73 45 6e 64 2e 6e 20 3d 20 30 3b 0a 20  .  sEnd.n = 0;. 
24234 20 6e 20 3d 20 28 69 6e 74 29 28 73 45 6e 64 2e   n = (int)(sEnd.
24235 7a 20 2d 20 70 42 65 67 69 6e 2d 3e 7a 29 3b 0a  z - pBegin->z);.
24236 20 20 7a 20 3d 20 70 42 65 67 69 6e 2d 3e 7a 3b    z = pBegin->z;
24237 0a 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53  .  while( ALWAYS
24238 28 6e 3e 30 29 20 26 26 20 73 71 6c 69 74 65 33  (n>0) && sqlite3
24239 49 73 73 70 61 63 65 28 7a 5b 6e 2d 31 5d 29 20  Isspace(z[n-1]) 
2423a 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 73 45 6e 64  ){ n--; }.  sEnd
2423b 2e 7a 20 3d 20 26 7a 5b 6e 2d 31 5d 3b 0a 20 20  .z = &z[n-1];.  
2423c 73 45 6e 64 2e 6e 20 3d 20 31 3b 0a 0a 20 20 2f  sEnd.n = 1;..  /
2423d 2a 20 55 73 65 20 73 71 6c 69 74 65 33 45 6e 64  * Use sqlite3End
2423e 54 61 62 6c 65 28 29 20 74 6f 20 61 64 64 20 74  Table() to add t
2423f 68 65 20 76 69 65 77 20 74 6f 20 74 68 65 20 53  he view to the S
24240 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62  QLITE_MASTER tab
24241 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45  le */.  sqlite3E
24242 6e 64 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  ndTable(pParse, 
24243 30 2c 20 26 73 45 6e 64 2c 20 30 29 3b 0a 20 20  0, &sEnd, 0);.  
24244 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66  return;.}.#endif
24245 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
24246 56 49 45 57 20 2a 2f 0a 0a 23 69 66 20 21 64 65  VIEW */..#if !de
24247 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
24248 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69  T_VIEW) || !defi
24249 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2424a 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a 2f 2a  VIRTUALTABLE)./*
2424b 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65 20 73 74  .** The Table st
2424c 72 75 63 74 75 72 65 20 70 54 61 62 6c 65 20 69  ructure pTable i
2424d 73 20 72 65 61 6c 6c 79 20 61 20 56 49 45 57 2e  s really a VIEW.
2424e 20 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 6e 61    Fill in the na
2424f 6d 65 73 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f  mes of.** the co
24250 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 76 69 65  lumns of the vie
24251 77 20 69 6e 20 74 68 65 20 70 54 61 62 6c 65 20  w in the pTable 
24252 73 74 72 75 63 74 75 72 65 2e 20 20 52 65 74 75  structure.  Retu
24253 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a  rn the number.**
24254 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 49 66 20   of errors.  If 
24255 61 6e 20 65 72 72 6f 72 20 69 73 20 73 65 65 6e  an error is seen
24256 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20   leave an error 
24257 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73  message in pPars
24258 65 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53  e->zErrMsg..*/.S
24259 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2425a 74 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74  t sqlite3ViewGet
2425b 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73  ColumnNames(Pars
2425c 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65  e *pParse, Table
2425d 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20 54 61 62   *pTable){.  Tab
2425e 6c 65 20 2a 70 53 65 6c 54 61 62 3b 20 20 20 2f  le *pSelTab;   /
2425f 2a 20 41 20 66 61 6b 65 20 74 61 62 6c 65 20 66  * A fake table f
24260 72 6f 6d 20 77 68 69 63 68 20 77 65 20 67 65 74  rom which we get
24261 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
24262 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65  */.  Select *pSe
24263 6c 3b 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f  l;     /* Copy o
24264 66 20 74 68 65 20 53 45 4c 45 43 54 20 74 68 61  f the SELECT tha
24265 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  t implements the
24266 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 6e   view */.  int n
24267 45 72 72 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  Err = 0;     /* 
24268 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  Number of errors
24269 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 2a 2f 0a   encountered */.
2426a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20    int n;        
2426b 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 69      /* Temporari
2426c 6c 79 20 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d  ly holds the num
2426d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20 61  ber of cursors a
2426e 73 73 69 67 6e 65 64 20 2a 2f 0a 20 20 73 71 6c  ssigned */.  sql
2426f 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
24270 65 2d 3e 64 62 3b 20 20 2f 2a 20 44 61 74 61 62  e->db;  /* Datab
24271 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66  ase connection f
24272 6f 72 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73  or malloc errors
24273 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 41 75 74   */.  int (*xAut
24274 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  h)(void*,int,con
24275 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
24276 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
24277 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 0a  ,const char*);..
24278 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65    assert( pTable
24279 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   );..#ifndef SQL
2427a 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2427b 54 41 42 4c 45 0a 20 20 69 66 28 20 73 71 6c 69  TABLE.  if( sqli
2427c 74 65 33 56 74 61 62 43 61 6c 6c 43 6f 6e 6e 65  te3VtabCallConne
2427d 63 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 6c  ct(pParse, pTabl
2427e 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  e) ){.    return
2427f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
24280 20 7d 0a 20 20 69 66 28 20 49 73 56 69 72 74 75   }.  if( IsVirtu
24281 61 6c 28 70 54 61 62 6c 65 29 20 29 20 72 65 74  al(pTable) ) ret
24282 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 23  urn 0;.#endif..#
24283 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
24284 49 54 5f 56 49 45 57 0a 20 20 2f 2a 20 41 20 70  IT_VIEW.  /* A p
24285 6f 73 69 74 69 76 65 20 6e 43 6f 6c 20 6d 65 61  ositive nCol mea
24286 6e 73 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 6e  ns the columns n
24287 61 6d 65 73 20 66 6f 72 20 74 68 69 73 20 76 69  ames for this vi
24288 65 77 20 61 72 65 0a 20 20 2a 2a 20 61 6c 72 65  ew are.  ** alre
24289 61 64 79 20 6b 6e 6f 77 6e 2e 0a 20 20 2a 2f 0a  ady known..  */.
2428a 20 20 69 66 28 20 70 54 61 62 6c 65 2d 3e 6e 43    if( pTable->nC
2428b 6f 6c 3e 30 20 29 20 72 65 74 75 72 6e 20 30 3b  ol>0 ) return 0;
2428c 0a 0a 20 20 2f 2a 20 41 20 6e 65 67 61 74 69 76  ..  /* A negativ
2428d 65 20 6e 43 6f 6c 20 69 73 20 61 20 73 70 65 63  e nCol is a spec
2428e 69 61 6c 20 6d 61 72 6b 65 72 20 6d 65 61 6e 69  ial marker meani
2428f 6e 67 20 74 68 61 74 20 77 65 20 61 72 65 20 63  ng that we are c
24290 75 72 72 65 6e 74 6c 79 0a 20 20 2a 2a 20 74 72  urrently.  ** tr
24291 79 69 6e 67 20 74 6f 20 63 6f 6d 70 75 74 65 20  ying to compute 
24292 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  the column names
24293 2e 20 20 49 66 20 77 65 20 65 6e 74 65 72 20 74  .  If we enter t
24294 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68  his routine with
24295 0a 20 20 2a 2a 20 61 20 6e 65 67 61 74 69 76 65  .  ** a negative
24296 20 6e 43 6f 6c 2c 20 69 74 20 6d 65 61 6e 73 20   nCol, it means 
24297 74 77 6f 20 6f 72 20 6d 6f 72 65 20 76 69 65 77  two or more view
24298 73 20 66 6f 72 6d 20 61 20 6c 6f 6f 70 2c 20 6c  s form a loop, l
24299 69 6b 65 20 74 68 69 73 3a 0a 20 20 2a 2a 0a 20  ike this:.  **. 
2429a 20 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 56   **     CREATE V
2429b 49 45 57 20 6f 6e 65 20 41 53 20 53 45 4c 45 43  IEW one AS SELEC
2429c 54 20 2a 20 46 52 4f 4d 20 74 77 6f 3b 0a 20 20  T * FROM two;.  
2429d 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 56 49  **     CREATE VI
2429e 45 57 20 74 77 6f 20 41 53 20 53 45 4c 45 43 54  EW two AS SELECT
2429f 20 2a 20 46 52 4f 4d 20 6f 6e 65 3b 0a 20 20 2a   * FROM one;.  *
242a0 2a 0a 20 20 2a 2a 20 41 63 74 75 61 6c 6c 79 2c  *.  ** Actually,
242a1 20 74 68 65 20 65 72 72 6f 72 20 61 62 6f 76 65   the error above
242a2 20 69 73 20 6e 6f 77 20 63 61 75 67 68 74 20 70   is now caught p
242a3 72 69 6f 72 20 74 6f 20 72 65 61 63 68 69 6e 67  rior to reaching
242a4 20 74 68 69 73 20 70 6f 69 6e 74 2e 0a 20 20 2a   this point..  *
242a5 2a 20 42 75 74 20 74 68 65 20 66 6f 6c 6c 6f 77  * But the follow
242a6 69 6e 67 20 74 65 73 74 20 69 73 20 73 74 69 6c  ing test is stil
242a7 6c 20 69 6d 70 6f 72 74 61 6e 74 20 61 73 20 69  l important as i
242a8 74 20 64 6f 65 73 20 63 6f 6d 65 20 75 70 0a 20  t does come up. 
242a9 20 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f   ** in the follo
242aa 77 69 6e 67 3a 0a 20 20 2a 2a 20 0a 20 20 2a 2a  wing:.  ** .  **
242ab 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c       CREATE TABL
242ac 45 20 6d 61 69 6e 2e 65 78 31 28 61 29 3b 0a 20  E main.ex1(a);. 
242ad 20 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 54   **     CREATE T
242ae 45 4d 50 20 56 49 45 57 20 65 78 31 20 41 53 20  EMP VIEW ex1 AS 
242af 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 65 78  SELECT a FROM ex
242b0 31 3b 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45  1;.  **     SELE
242b1 43 54 20 2a 20 46 52 4f 4d 20 74 65 6d 70 2e 65  CT * FROM temp.e
242b2 78 31 3b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  x1;.  */.  if( p
242b3 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3c 30 20 29 7b  Table->nCol<0 ){
242b4 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
242b5 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69  rMsg(pParse, "vi
242b6 65 77 20 25 73 20 69 73 20 63 69 72 63 75 6c 61  ew %s is circula
242b7 72 6c 79 20 64 65 66 69 6e 65 64 22 2c 20 70 54  rly defined", pT
242b8 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  able->zName);.  
242b9 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
242ba 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65    assert( pTable
242bb 2d 3e 6e 43 6f 6c 3e 3d 30 20 29 3b 0a 0a 20 20  ->nCol>=0 );..  
242bc 2f 2a 20 49 66 20 77 65 20 67 65 74 20 74 68 69  /* If we get thi
242bd 73 20 66 61 72 2c 20 69 74 20 6d 65 61 6e 73 20  s far, it means 
242be 77 65 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 75  we need to compu
242bf 74 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d  te the table nam
242c0 65 73 2e 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68  es..  ** Note th
242c1 61 74 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73  at the call to s
242c2 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f  qlite3ResultSetO
242c3 66 53 65 6c 65 63 74 28 29 20 77 69 6c 6c 20 65  fSelect() will e
242c4 78 70 61 6e 64 20 61 6e 79 0a 20 20 2a 2a 20 22  xpand any.  ** "
242c5 2a 22 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74  *" elements in t
242c6 68 65 20 72 65 73 75 6c 74 73 20 73 65 74 20 6f  he results set o
242c7 66 20 74 68 65 20 76 69 65 77 20 61 6e 64 20 77  f the view and w
242c8 69 6c 6c 20 61 73 73 69 67 6e 20 63 75 72 73 6f  ill assign curso
242c9 72 73 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 65  rs.  ** to the e
242ca 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 46  lements of the F
242cb 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 42 75 74  ROM clause.  But
242cc 20 77 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20   we do not want 
242cd 74 68 65 73 65 20 63 68 61 6e 67 65 73 0a 20 20  these changes.  
242ce 2a 2a 20 74 6f 20 62 65 20 70 65 72 6d 61 6e 65  ** to be permane
242cf 6e 74 2e 20 20 53 6f 20 74 68 65 20 63 6f 6d 70  nt.  So the comp
242d0 75 74 61 74 69 6f 6e 20 69 73 20 64 6f 6e 65 20  utation is done 
242d1 6f 6e 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  on a copy of the
242d2 20 53 45 4c 45 43 54 0a 20 20 2a 2a 20 73 74 61   SELECT.  ** sta
242d3 74 65 6d 65 6e 74 20 74 68 61 74 20 64 65 66 69  tement that defi
242d4 6e 65 73 20 74 68 65 20 76 69 65 77 2e 0a 20 20  nes the view..  
242d5 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54 61  */.  assert( pTa
242d6 62 6c 65 2d 3e 70 53 65 6c 65 63 74 20 29 3b 0a  ble->pSelect );.
242d7 20 20 70 53 65 6c 20 3d 20 73 71 6c 69 74 65 33    pSel = sqlite3
242d8 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 54  SelectDup(db, pT
242d9 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 2c 20 30  able->pSelect, 0
242da 29 3b 0a 20 20 69 66 28 20 70 53 65 6c 20 29 7b  );.  if( pSel ){
242db 0a 20 20 20 20 75 38 20 65 6e 61 62 6c 65 4c 6f  .    u8 enableLo
242dc 6f 6b 61 73 69 64 65 20 3d 20 64 62 2d 3e 6c 6f  okaside = db->lo
242dd 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
242de 3b 0a 20 20 20 20 6e 20 3d 20 70 50 61 72 73 65  ;.    n = pParse
242df 2d 3e 6e 54 61 62 3b 0a 20 20 20 20 73 71 6c 69  ->nTab;.    sqli
242e0 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e  te3SrcListAssign
242e1 43 75 72 73 6f 72 73 28 70 50 61 72 73 65 2c 20  Cursors(pParse, 
242e2 70 53 65 6c 2d 3e 70 53 72 63 29 3b 0a 20 20 20  pSel->pSrc);.   
242e3 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20   pTable->nCol = 
242e4 2d 31 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b  -1;.    db->look
242e5 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d  aside.bEnabled =
242e6 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   0;.#ifndef SQLI
242e7 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
242e8 41 54 49 4f 4e 0a 20 20 20 20 78 41 75 74 68 20  ATION.    xAuth 
242e9 3d 20 64 62 2d 3e 78 41 75 74 68 3b 0a 20 20 20  = db->xAuth;.   
242ea 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 30 3b 0a   db->xAuth = 0;.
242eb 20 20 20 20 70 53 65 6c 54 61 62 20 3d 20 73 71      pSelTab = sq
242ec 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66  lite3ResultSetOf
242ed 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
242ee 53 65 6c 29 3b 0a 20 20 20 20 64 62 2d 3e 78 41  Sel);.    db->xA
242ef 75 74 68 20 3d 20 78 41 75 74 68 3b 0a 23 65 6c  uth = xAuth;.#el
242f0 73 65 0a 20 20 20 20 70 53 65 6c 54 61 62 20 3d  se.    pSelTab =
242f1 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65   sqlite3ResultSe
242f2 74 4f 66 53 65 6c 65 63 74 28 70 50 61 72 73 65  tOfSelect(pParse
242f3 2c 20 70 53 65 6c 29 3b 0a 23 65 6e 64 69 66 0a  , pSel);.#endif.
242f4 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
242f5 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61  e.bEnabled = ena
242f6 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b 0a 20 20  bleLookaside;.  
242f7 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 3d    pParse->nTab =
242f8 20 6e 3b 0a 20 20 20 20 69 66 28 20 70 53 65 6c   n;.    if( pSel
242f9 54 61 62 20 29 7b 0a 20 20 20 20 20 20 61 73 73  Tab ){.      ass
242fa 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 61 43 6f  ert( pTable->aCo
242fb 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 54  l==0 );.      pT
242fc 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 70 53 65  able->nCol = pSe
242fd 6c 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20  lTab->nCol;.    
242fe 20 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 20 3d    pTable->aCol =
242ff 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a   pSelTab->aCol;.
24300 20 20 20 20 20 20 70 53 65 6c 54 61 62 2d 3e 6e        pSelTab->n
24301 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  Col = 0;.      p
24302 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 30  SelTab->aCol = 0
24303 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ;.      sqlite3D
24304 65 6c 65 74 65 54 61 62 6c 65 28 70 53 65 6c 54  eleteTable(pSelT
24305 61 62 29 3b 0a 20 20 20 20 20 20 70 54 61 62 6c  ab);.      pTabl
24306 65 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67  e->pSchema->flag
24307 73 20 7c 3d 20 44 42 5f 55 6e 72 65 73 65 74 56  s |= DB_UnresetV
24308 69 65 77 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  iews;.    }else{
24309 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e  .      pTable->n
2430a 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 6e  Col = 0;.      n
2430b 45 72 72 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20  Err++;.    }.   
2430c 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
2430d 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 29 3b 0a  lete(db, pSel);.
2430e 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 6e    } else {.    n
2430f 45 72 72 2b 2b 3b 0a 20 20 7d 0a 23 65 6e 64 69  Err++;.  }.#endi
24310 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
24311 5f 56 49 45 57 20 2a 2f 0a 20 20 72 65 74 75 72  _VIEW */.  retur
24312 6e 20 6e 45 72 72 3b 20 20 0a 7d 0a 23 65 6e 64  n nErr;  .}.#end
24313 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
24314 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
24315 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
24316 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
24317 54 41 42 4c 45 29 20 2a 2f 0a 0a 23 69 66 6e 64  TABLE) */..#ifnd
24318 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
24319 49 45 57 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  IEW./*.** Clear 
2431a 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  the column names
2431b 20 66 72 6f 6d 20 65 76 65 72 79 20 56 49 45 57   from every VIEW
2431c 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 64 78   in database idx
2431d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2431e 20 73 71 6c 69 74 65 56 69 65 77 52 65 73 65 74   sqliteViewReset
2431f 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  All(sqlite3 *db,
24320 20 69 6e 74 20 69 64 78 29 7b 0a 20 20 48 61 73   int idx){.  Has
24321 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 69 66 28 20  hElem *i;.  if( 
24322 21 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 64  !DbHasProperty(d
24323 62 2c 20 69 64 78 2c 20 44 42 5f 55 6e 72 65 73  b, idx, DB_Unres
24324 65 74 56 69 65 77 73 29 20 29 20 72 65 74 75 72  etViews) ) retur
24325 6e 3b 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74  n;.  for(i=sqlit
24326 65 48 61 73 68 46 69 72 73 74 28 26 64 62 2d 3e  eHashFirst(&db->
24327 61 44 62 5b 69 64 78 5d 2e 70 53 63 68 65 6d 61  aDb[idx].pSchema
24328 2d 3e 74 62 6c 48 61 73 68 29 3b 20 69 3b 69 3d  ->tblHash); i;i=
24329 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69  sqliteHashNext(i
2432a 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  )){.    Table *p
2432b 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68  Tab = sqliteHash
2432c 44 61 74 61 28 69 29 3b 0a 20 20 20 20 69 66 28  Data(i);.    if(
2432d 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29   pTab->pSelect )
2432e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 52 65  {.      sqliteRe
2432f 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70  setColumnNames(p
24330 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Tab);.    }.  }.
24331 20 20 44 62 43 6c 65 61 72 50 72 6f 70 65 72 74    DbClearPropert
24332 79 28 64 62 2c 20 69 64 78 2c 20 44 42 5f 55 6e  y(db, idx, DB_Un
24333 72 65 73 65 74 56 69 65 77 73 29 3b 0a 7d 0a 23  resetViews);.}.#
24334 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71  else.# define sq
24335 6c 69 74 65 56 69 65 77 52 65 73 65 74 41 6c 6c  liteViewResetAll
24336 28 41 2c 42 29 0a 23 65 6e 64 69 66 20 2f 2a 20  (A,B).#endif /* 
24337 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
24338 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20   */../*.** This 
24339 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
2433a 65 64 20 62 79 20 74 68 65 20 56 44 42 45 20 74  ed by the VDBE t
2433b 6f 20 61 64 6a 75 73 74 20 74 68 65 20 69 6e 74  o adjust the int
2433c 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a 2a 2a 20  ernal schema.** 
2433d 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20 77  used by SQLite w
2433e 68 65 6e 20 74 68 65 20 62 74 72 65 65 20 6c 61  hen the btree la
2433f 79 65 72 20 6d 6f 76 65 73 20 61 20 74 61 62 6c  yer moves a tabl
24340 65 20 72 6f 6f 74 20 70 61 67 65 2e 20 54 68 65  e root page. The
24341 0a 2a 2a 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66  .** root-page of
24342 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65   a table or inde
24343 78 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44  x in database iD
24344 62 20 68 61 73 20 63 68 61 6e 67 65 64 20 66 72  b has changed fr
24345 6f 6d 20 69 46 72 6f 6d 0a 2a 2a 20 74 6f 20 69  om iFrom.** to i
24346 54 6f 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74  To..**.** Ticket
24347 20 23 31 37 32 38 3a 20 20 54 68 65 20 73 79 6d   #1728:  The sym
24348 62 6f 6c 20 74 61 62 6c 65 20 6d 69 67 68 74 20  bol table might 
24349 73 74 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 69 6e  still contain in
2434a 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 6f 6e 20  formation.** on 
2434b 74 61 62 6c 65 73 20 61 6e 64 2f 6f 72 20 69 6e  tables and/or in
2434c 64 69 63 65 73 20 74 68 61 74 20 61 72 65 20 74  dices that are t
2434d 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20 62 65  he process of be
2434e 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20  ing deleted..** 
2434f 49 66 20 79 6f 75 20 61 72 65 20 75 6e 6c 75 63  If you are unluc
24350 6b 79 2c 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65  ky, one of those
24351 20 64 65 6c 65 74 65 64 20 69 6e 64 69 63 65 73   deleted indices
24352 20 6f 72 20 74 61 62 6c 65 73 20 6d 69 67 68 74   or tables might
24353 0a 2a 2a 20 68 61 76 65 20 74 68 65 20 73 61 6d  .** have the sam
24354 65 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d 62 65  e rootpage numbe
24355 72 20 61 73 20 74 68 65 20 72 65 61 6c 20 74 61  r as the real ta
24356 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 74 68 61  ble or index tha
24357 74 20 69 73 0a 2a 2a 20 62 65 69 6e 67 20 6d 6f  t is.** being mo
24358 76 65 64 2e 20 20 53 6f 20 77 65 20 63 61 6e 6e  ved.  So we cann
24359 6f 74 20 73 74 6f 70 20 73 65 61 72 63 68 69 6e  ot stop searchin
2435a 67 20 61 66 74 65 72 20 74 68 65 20 66 69 72 73  g after the firs
2435b 74 20 6d 61 74 63 68 20 0a 2a 2a 20 62 65 63 61  t match .** beca
2435c 75 73 65 20 74 68 65 20 66 69 72 73 74 20 6d 61  use the first ma
2435d 74 63 68 20 6d 69 67 68 74 20 62 65 20 66 6f 72  tch might be for
2435e 20 6f 6e 65 20 6f 66 20 74 68 65 20 64 65 6c 65   one of the dele
2435f 74 65 64 20 69 6e 64 69 63 65 73 0a 2a 2a 20 6f  ted indices.** o
24360 72 20 74 61 62 6c 65 73 20 61 6e 64 20 6e 6f 74  r tables and not
24361 20 74 68 65 20 74 61 62 6c 65 2f 69 6e 64 65 78   the table/index
24362 20 74 68 61 74 20 69 73 20 61 63 74 75 61 6c 6c   that is actuall
24363 79 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 0a 2a  y being moved..*
24364 2a 20 57 65 20 6d 75 73 74 20 63 6f 6e 74 69 6e  * We must contin
24365 75 65 20 6c 6f 6f 70 69 6e 67 20 75 6e 74 69 6c  ue looping until
24366 20 61 6c 6c 20 74 61 62 6c 65 73 20 61 6e 64 20   all tables and 
24367 69 6e 64 69 63 65 73 20 77 69 74 68 0a 2a 2a 20  indices with.** 
24368 72 6f 6f 74 70 61 67 65 3d 3d 69 46 72 6f 6d 20  rootpage==iFrom 
24369 68 61 76 65 20 62 65 65 6e 20 63 6f 6e 76 65 72  have been conver
2436a 74 65 64 20 74 6f 20 68 61 76 65 20 61 20 72 6f  ted to have a ro
2436b 6f 74 70 61 67 65 20 6f 66 20 69 54 6f 0a 2a 2a  otpage of iTo.**
2436c 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 65 20   in order to be 
2436d 63 65 72 74 61 69 6e 20 74 68 61 74 20 77 65 20  certain that we 
2436e 67 6f 74 20 74 68 65 20 72 69 67 68 74 20 6f 6e  got the right on
2436f 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  e..*/.#ifndef SQ
24370 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
24371 43 55 55 4d 0a 53 51 4c 49 54 45 5f 50 52 49 56  CUUM.SQLITE_PRIV
24372 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
24373 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 44 62  RootPageMoved(Db
24374 20 2a 70 44 62 2c 20 69 6e 74 20 69 46 72 6f 6d   *pDb, int iFrom
24375 2c 20 69 6e 74 20 69 54 6f 29 7b 0a 20 20 48 61  , int iTo){.  Ha
24376 73 68 45 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0a 20  shElem *pElem;. 
24377 20 48 61 73 68 20 2a 70 48 61 73 68 3b 0a 0a 20   Hash *pHash;.. 
24378 20 70 48 61 73 68 20 3d 20 26 70 44 62 2d 3e 70   pHash = &pDb->p
24379 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b  Schema->tblHash;
2437a 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c  .  for(pElem=sql
2437b 69 74 65 48 61 73 68 46 69 72 73 74 28 70 48 61  iteHashFirst(pHa
2437c 73 68 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65  sh); pElem; pEle
2437d 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  m=sqliteHashNext
2437e 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 54 61  (pElem)){.    Ta
2437f 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69  ble *pTab = sqli
24380 74 65 48 61 73 68 44 61 74 61 28 70 45 6c 65 6d  teHashData(pElem
24381 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d  );.    if( pTab-
24382 3e 74 6e 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a  >tnum==iFrom ){.
24383 20 20 20 20 20 20 70 54 61 62 2d 3e 74 6e 75 6d        pTab->tnum
24384 20 3d 20 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20   = iTo;.    }.  
24385 7d 0a 20 20 70 48 61 73 68 20 3d 20 26 70 44 62  }.  pHash = &pDb
24386 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61  ->pSchema->idxHa
24387 73 68 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d  sh;.  for(pElem=
24388 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
24389 70 48 61 73 68 29 3b 20 70 45 6c 65 6d 3b 20 70  pHash); pElem; p
2438a 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e  Elem=sqliteHashN
2438b 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20  ext(pElem)){.   
2438c 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 73   Index *pIdx = s
2438d 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 45  qliteHashData(pE
2438e 6c 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 49  lem);.    if( pI
2438f 64 78 2d 3e 74 6e 75 6d 3d 3d 69 46 72 6f 6d 20  dx->tnum==iFrom 
24390 29 7b 0a 20 20 20 20 20 20 70 49 64 78 2d 3e 74  ){.      pIdx->t
24391 6e 75 6d 20 3d 20 69 54 6f 3b 0a 20 20 20 20 7d  num = iTo;.    }
24392 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  .  }.}.#endif../
24393 2a 0a 2a 2a 20 57 72 69 74 65 20 63 6f 64 65 20  *.** Write code 
24394 74 6f 20 65 72 61 73 65 20 74 68 65 20 74 61 62  to erase the tab
24395 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67  le with root-pag
24396 65 20 69 54 61 62 6c 65 20 66 72 6f 6d 20 64 61  e iTable from da
24397 74 61 62 61 73 65 20 69 44 62 2e 0a 2a 2a 20 41  tabase iDb..** A
24398 6c 73 6f 20 77 72 69 74 65 20 63 6f 64 65 20 74  lso write code t
24399 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 73 71 6c  o modify the sql
2439a 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
2439b 20 61 6e 64 20 69 6e 74 65 72 6e 61 6c 20 73 63   and internal sc
2439c 68 65 6d 61 0a 2a 2a 20 69 66 20 61 20 72 6f 6f  hema.** if a roo
2439d 74 2d 70 61 67 65 20 6f 66 20 61 6e 6f 74 68 65  t-page of anothe
2439e 72 20 74 61 62 6c 65 20 69 73 20 6d 6f 76 65 64  r table is moved
2439f 20 62 79 20 74 68 65 20 62 74 72 65 65 2d 6c 61   by the btree-la
243a0 79 65 72 20 77 68 69 6c 73 74 0a 2a 2a 20 65 72  yer whilst.** er
243a1 61 73 69 6e 67 20 69 54 61 62 6c 65 20 28 74 68  asing iTable (th
243a2 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20 77 69  is can happen wi
243a3 74 68 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75  th an auto-vacuu
243a4 6d 20 64 61 74 61 62 61 73 65 29 2e 0a 2a 2f 20  m database)..*/ 
243a5 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 73  .static void des
243a6 74 72 6f 79 52 6f 6f 74 50 61 67 65 28 50 61 72  troyRootPage(Par
243a7 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
243a8 69 54 61 62 6c 65 2c 20 69 6e 74 20 69 44 62 29  iTable, int iDb)
243a9 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71  {.  Vdbe *v = sq
243aa 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
243ab 72 73 65 29 3b 0a 20 20 69 6e 74 20 72 31 20 3d  rse);.  int r1 =
243ac 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
243ad 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71  eg(pParse);.  sq
243ae 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
243af 76 2c 20 4f 50 5f 44 65 73 74 72 6f 79 2c 20 69  v, OP_Destroy, i
243b0 54 61 62 6c 65 2c 20 72 31 2c 20 69 44 62 29 3b  Table, r1, iDb);
243b1 0a 20 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f  .  sqlite3MayAbo
243b2 72 74 28 70 50 61 72 73 65 29 3b 0a 23 69 66 6e  rt(pParse);.#ifn
243b3 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
243b4 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 2f 2a 20  AUTOVACUUM.  /* 
243b5 4f 50 5f 44 65 73 74 72 6f 79 20 73 74 6f 72 65  OP_Destroy store
243b6 73 20 61 6e 20 69 6e 20 69 6e 74 65 67 65 72 20  s an in integer 
243b7 72 31 2e 20 49 66 20 74 68 69 73 20 69 6e 74 65  r1. If this inte
243b8 67 65 72 0a 20 20 2a 2a 20 69 73 20 6e 6f 6e 2d  ger.  ** is non-
243b9 7a 65 72 6f 2c 20 74 68 65 6e 20 69 74 20 69 73  zero, then it is
243ba 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e   the root page n
243bb 75 6d 62 65 72 20 6f 66 20 61 20 74 61 62 6c 65  umber of a table
243bc 20 6d 6f 76 65 64 20 74 6f 0a 20 20 2a 2a 20 6c   moved to.  ** l
243bd 6f 63 61 74 69 6f 6e 20 69 54 61 62 6c 65 2e 20  ocation iTable. 
243be 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  The following co
243bf 64 65 20 6d 6f 64 69 66 69 65 73 20 74 68 65 20  de modifies the 
243c0 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
243c1 62 6c 65 20 74 6f 0a 20 20 2a 2a 20 72 65 66 6c  ble to.  ** refl
243c2 65 63 74 20 74 68 69 73 2e 0a 20 20 2a 2a 0a 20  ect this..  **. 
243c3 20 2a 2a 20 54 68 65 20 22 23 4e 4e 4e 22 20 69   ** The "#NNN" i
243c4 6e 20 74 68 65 20 53 51 4c 20 69 73 20 61 20 73  n the SQL is a s
243c5 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20  pecial constant 
243c6 74 68 61 74 20 6d 65 61 6e 73 20 77 68 61 74 65  that means whate
243c7 76 65 72 20 76 61 6c 75 65 0a 20 20 2a 2a 20 69  ver value.  ** i
243c8 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 4e 4e  s in register NN
243c9 4e 2e 20 20 53 65 65 20 67 72 61 6d 6d 61 72 20  N.  See grammar 
243ca 72 75 6c 65 73 20 61 73 73 6f 63 69 61 74 65 64  rules associated
243cb 20 77 69 74 68 20 74 68 65 20 54 4b 5f 52 45 47   with the TK_REG
243cc 49 53 54 45 52 0a 20 20 2a 2a 20 74 6f 6b 65 6e  ISTER.  ** token
243cd 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
243ce 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 2a  information..  *
243cf 2f 0a 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65  /.  sqlite3Neste
243d0 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a  dParse(pParse, .
243d1 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51 2e       "UPDATE %Q.
243d2 25 73 20 53 45 54 20 72 6f 6f 74 70 61 67 65 3d  %s SET rootpage=
243d3 25 64 20 57 48 45 52 45 20 23 25 64 20 41 4e 44  %d WHERE #%d AND
243d4 20 72 6f 6f 74 70 61 67 65 3d 23 25 64 22 2c 0a   rootpage=#%d",.
243d5 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 2d       pParse->db-
243d6 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c  >aDb[iDb].zName,
243d7 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
243d8 62 29 2c 20 69 54 61 62 6c 65 2c 20 72 31 2c 20  b), iTable, r1, 
243d9 72 31 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71  r1);.#endif.  sq
243da 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
243db 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
243dc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  .}../*.** Write 
243dd 56 44 42 45 20 63 6f 64 65 20 74 6f 20 65 72 61  VDBE code to era
243de 73 65 20 74 61 62 6c 65 20 70 54 61 62 20 61 6e  se table pTab an
243df 64 20 61 6c 6c 20 61 73 73 6f 63 69 61 74 65 64  d all associated
243e0 20 69 6e 64 69 63 65 73 20 6f 6e 20 64 69 73 6b   indices on disk
243e1 2e 0a 2a 2a 20 43 6f 64 65 20 74 6f 20 75 70 64  ..** Code to upd
243e2 61 74 65 20 74 68 65 20 73 71 6c 69 74 65 5f 6d  ate the sqlite_m
243e3 61 73 74 65 72 20 74 61 62 6c 65 73 20 61 6e 64  aster tables and
243e4 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61   internal schema
243e5 20 64 65 66 69 6e 69 74 69 6f 6e 73 0a 2a 2a 20   definitions.** 
243e6 69 6e 20 63 61 73 65 20 61 20 72 6f 6f 74 2d 70  in case a root-p
243e7 61 67 65 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f  age belonging to
243e8 20 61 6e 6f 74 68 65 72 20 74 61 62 6c 65 20 69   another table i
243e9 73 20 6d 6f 76 65 64 20 62 79 20 74 68 65 20 62  s moved by the b
243ea 74 72 65 65 20 6c 61 79 65 72 0a 2a 2a 20 69 73  tree layer.** is
243eb 20 61 6c 73 6f 20 61 64 64 65 64 20 28 74 68 69   also added (thi
243ec 73 20 63 61 6e 20 68 61 70 70 65 6e 20 77 69 74  s can happen wit
243ed 68 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d  h an auto-vacuum
243ee 20 64 61 74 61 62 61 73 65 29 2e 0a 2a 2f 0a 73   database)..*/.s
243ef 74 61 74 69 63 20 76 6f 69 64 20 64 65 73 74 72  tatic void destr
243f0 6f 79 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70  oyTable(Parse *p
243f1 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54  Parse, Table *pT
243f2 61 62 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  ab){.#ifdef SQLI
243f3 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
243f4 55 4d 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  UM.  Index *pIdx
243f5 3b 0a 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71  ;.  int iDb = sq
243f6 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
243f7 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  ex(pParse->db, p
243f8 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  Tab->pSchema);. 
243f9 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67 65   destroyRootPage
243fa 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 74  (pParse, pTab->t
243fb 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 66 6f 72  num, iDb);.  for
243fc 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64  (pIdx=pTab->pInd
243fd 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
243fe 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
243ff 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67 65   destroyRootPage
24400 28 70 50 61 72 73 65 2c 20 70 49 64 78 2d 3e 74  (pParse, pIdx->t
24401 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 23  num, iDb);.  }.#
24402 65 6c 73 65 0a 20 20 2f 2a 20 49 66 20 74 68 65  else.  /* If the
24403 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 62 65   database may be
24404 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 63 61 70   auto-vacuum cap
24405 61 62 6c 65 20 28 69 66 20 53 51 4c 49 54 45 5f  able (if SQLITE_
24406 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
24407 20 20 2a 2a 20 69 73 20 6e 6f 74 20 64 65 66 69    ** is not defi
24408 6e 65 64 29 2c 20 74 68 65 6e 20 69 74 20 69 73  ned), then it is
24409 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 63 61   important to ca
2440a 6c 6c 20 4f 50 5f 44 65 73 74 72 6f 79 20 6f 6e  ll OP_Destroy on
2440b 20 74 68 65 0a 20 20 2a 2a 20 74 61 62 6c 65 20   the.  ** table 
2440c 61 6e 64 20 69 6e 64 65 78 20 72 6f 6f 74 2d 70  and index root-p
2440d 61 67 65 73 20 69 6e 20 6f 72 64 65 72 2c 20 73  ages in order, s
2440e 74 61 72 74 69 6e 67 20 77 69 74 68 20 74 68 65  tarting with the
2440f 20 6e 75 6d 65 72 69 63 61 6c 6c 79 20 0a 20 20   numerically .  
24410 2a 2a 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d  ** largest root-
24411 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 54 68 69  page number. Thi
24412 73 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 61  s guarantees tha
24413 74 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 72 6f  t none of the ro
24414 6f 74 2d 70 61 67 65 73 0a 20 20 2a 2a 20 74 6f  ot-pages.  ** to
24415 20 62 65 20 64 65 73 74 72 6f 79 65 64 20 69 73   be destroyed is
24416 20 72 65 6c 6f 63 61 74 65 64 20 62 79 20 61 6e   relocated by an
24417 20 65 61 72 6c 69 65 72 20 4f 50 5f 44 65 73 74   earlier OP_Dest
24418 72 6f 79 2e 20 69 2e 65 2e 20 69 66 20 74 68 65  roy. i.e. if the
24419 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20  .  ** following 
2441a 77 65 72 65 20 63 6f 64 65 64 3a 0a 20 20 2a 2a  were coded:.  **
2441b 0a 20 20 2a 2a 20 4f 50 5f 44 65 73 74 72 6f 79  .  ** OP_Destroy
2441c 20 34 20 30 0a 20 20 2a 2a 20 2e 2e 2e 0a 20 20   4 0.  ** ....  
2441d 2a 2a 20 4f 50 5f 44 65 73 74 72 6f 79 20 35 20  ** OP_Destroy 5 
2441e 30 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 6e 64 20  0.  **.  ** and 
2441f 72 6f 6f 74 20 70 61 67 65 20 35 20 68 61 70 70  root page 5 happ
24420 65 6e 65 64 20 74 6f 20 62 65 20 74 68 65 20 6c  ened to be the l
24421 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65  argest root-page
24422 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 0a 20   number in the. 
24423 20 2a 2a 20 64 61 74 61 62 61 73 65 2c 20 74 68   ** database, th
24424 65 6e 20 72 6f 6f 74 20 70 61 67 65 20 35 20 77  en root page 5 w
24425 6f 75 6c 64 20 62 65 20 6d 6f 76 65 64 20 74 6f  ould be moved to
24426 20 70 61 67 65 20 34 20 62 79 20 74 68 65 20 0a   page 4 by the .
24427 20 20 2a 2a 20 22 4f 50 5f 44 65 73 74 72 6f 79    ** "OP_Destroy
24428 20 34 20 30 22 20 6f 70 63 6f 64 65 2e 20 54 68   4 0" opcode. Th
24429 65 20 73 75 62 73 65 71 75 65 6e 74 20 22 4f 50  e subsequent "OP
2442a 5f 44 65 73 74 72 6f 79 20 35 20 30 22 20 77 6f  _Destroy 5 0" wo
2442b 75 6c 64 20 68 69 74 0a 20 20 2a 2a 20 61 20 66  uld hit.  ** a f
2442c 72 65 65 2d 6c 69 73 74 20 70 61 67 65 2e 0a 20  ree-list page.. 
2442d 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 20 3d   */.  int iTab =
2442e 20 70 54 61 62 2d 3e 74 6e 75 6d 3b 0a 20 20 69   pTab->tnum;.  i
2442f 6e 74 20 69 44 65 73 74 72 6f 79 65 64 20 3d 20  nt iDestroyed = 
24430 30 3b 0a 0a 20 20 77 68 69 6c 65 28 20 31 20 29  0;..  while( 1 )
24431 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64  {.    Index *pId
24432 78 3b 0a 20 20 20 20 69 6e 74 20 69 4c 61 72 67  x;.    int iLarg
24433 65 73 74 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66  est = 0;..    if
24434 28 20 69 44 65 73 74 72 6f 79 65 64 3d 3d 30 20  ( iDestroyed==0 
24435 7c 7c 20 69 54 61 62 3c 69 44 65 73 74 72 6f 79  || iTab<iDestroy
24436 65 64 20 29 7b 0a 20 20 20 20 20 20 69 4c 61 72  ed ){.      iLar
24437 67 65 73 74 20 3d 20 69 54 61 62 3b 0a 20 20 20  gest = iTab;.   
24438 20 7d 0a 20 20 20 20 66 6f 72 28 70 49 64 78 3d   }.    for(pIdx=
24439 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
2443a 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
2443b 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 6e 74  Next){.      int
2443c 20 69 49 64 78 20 3d 20 70 49 64 78 2d 3e 74 6e   iIdx = pIdx->tn
2443d 75 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  um;.      assert
2443e 28 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d  ( pIdx->pSchema=
2443f 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29  =pTab->pSchema )
24440 3b 0a 20 20 20 20 20 20 69 66 28 20 28 69 44 65  ;.      if( (iDe
24441 73 74 72 6f 79 65 64 3d 3d 30 20 7c 7c 20 28 69  stroyed==0 || (i
24442 49 64 78 3c 69 44 65 73 74 72 6f 79 65 64 29 29  Idx<iDestroyed))
24443 20 26 26 20 69 49 64 78 3e 69 4c 61 72 67 65 73   && iIdx>iLarges
24444 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4c 61  t ){.        iLa
24445 72 67 65 73 74 20 3d 20 69 49 64 78 3b 0a 20 20  rgest = iIdx;.  
24446 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
24447 69 66 28 20 69 4c 61 72 67 65 73 74 3d 3d 30 20  if( iLargest==0 
24448 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  ){.      return;
24449 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2444a 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69    int iDb = sqli
2444b 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
2444c 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61  (pParse->db, pTa
2444d 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20  b->pSchema);.   
2444e 20 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61     destroyRootPa
2444f 67 65 28 70 50 61 72 73 65 2c 20 69 4c 61 72 67  ge(pParse, iLarg
24450 65 73 74 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  est, iDb);.     
24451 20 69 44 65 73 74 72 6f 79 65 64 20 3d 20 69 4c   iDestroyed = iL
24452 61 72 67 65 73 74 3b 0a 20 20 20 20 7d 0a 20 20  argest;.    }.  
24453 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  }.#endif.}../*.*
24454 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
24455 73 20 63 61 6c 6c 65 64 20 74 6f 20 64 6f 20 74  s called to do t
24456 68 65 20 77 6f 72 6b 20 6f 66 20 61 20 44 52 4f  he work of a DRO
24457 50 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  P TABLE statemen
24458 74 2e 0a 2a 2a 20 70 4e 61 6d 65 20 69 73 20 74  t..** pName is t
24459 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
2445a 61 62 6c 65 20 74 6f 20 62 65 20 64 72 6f 70 70  able to be dropp
2445b 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
2445c 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2445d 65 33 44 72 6f 70 54 61 62 6c 65 28 50 61 72 73  e3DropTable(Pars
2445e 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69  e *pParse, SrcLi
2445f 73 74 20 2a 70 4e 61 6d 65 2c 20 69 6e 74 20 69  st *pName, int i
24460 73 56 69 65 77 2c 20 69 6e 74 20 6e 6f 45 72 72  sView, int noErr
24461 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  ){.  Table *pTab
24462 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73  ;.  Vdbe *v;.  s
24463 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
24464 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69  rse->db;.  int i
24465 44 62 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d  Db;..  if( db->m
24466 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
24467 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f     goto exit_dro
24468 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 61  p_table;.  }.  a
24469 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e  ssert( pParse->n
2446a 45 72 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  Err==0 );.  asse
2446b 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d  rt( pName->nSrc=
2446c 3d 31 20 29 3b 0a 20 20 70 54 61 62 20 3d 20 73  =1 );.  pTab = s
2446d 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c  qlite3LocateTabl
2446e 65 28 70 50 61 72 73 65 2c 20 69 73 56 69 65 77  e(pParse, isView
2446f 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
24470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
24471 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65  Name->a[0].zName
24472 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44  , pName->a[0].zD
24473 61 74 61 62 61 73 65 29 3b 0a 0a 20 20 69 66 28  atabase);..  if(
24474 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20   pTab==0 ){.    
24475 69 66 28 20 6e 6f 45 72 72 20 29 7b 0a 20 20 20  if( noErr ){.   
24476 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43     sqlite3ErrorC
24477 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20  lear(pParse);.  
24478 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 65 78 69    }.    goto exi
24479 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20  t_drop_table;.  
2447a 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  }.  iDb = sqlite
2447b 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
2447c 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
2447d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  );.  assert( iDb
2447e 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e  >=0 && iDb<db->n
2447f 44 62 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 70  Db );..  /* If p
24480 54 61 62 20 69 73 20 61 20 76 69 72 74 75 61 6c  Tab is a virtual
24481 20 74 61 62 6c 65 2c 20 63 61 6c 6c 20 56 69 65   table, call Vie
24482 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
24483 29 20 74 6f 20 65 6e 73 75 72 65 0a 20 20 2a 2a  ) to ensure.  **
24484 20 69 74 20 69 73 20 69 6e 69 74 69 61 6c 69 7a   it is initializ
24485 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 49  ed..  */.  if( I
24486 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 26  sVirtual(pTab) &
24487 26 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74  & sqlite3ViewGet
24488 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72  ColumnNames(pPar
24489 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20  se, pTab) ){.   
2448a 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f   goto exit_drop_
2448b 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 69 66 6e 64  table;.  }.#ifnd
2448c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
2448d 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b  UTHORIZATION.  {
2448e 0a 20 20 20 20 69 6e 74 20 63 6f 64 65 3b 0a 20  .    int code;. 
2448f 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
24490 54 61 62 20 3d 20 53 43 48 45 4d 41 5f 54 41 42  Tab = SCHEMA_TAB
24491 4c 45 28 69 44 62 29 3b 0a 20 20 20 20 63 6f 6e  LE(iDb);.    con
24492 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64  st char *zDb = d
24493 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
24494 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  e;.    const cha
24495 72 20 2a 7a 41 72 67 32 20 3d 20 30 3b 0a 20 20  r *zArg2 = 0;.  
24496 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
24497 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
24498 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a 54  QLITE_DELETE, zT
24499 61 62 2c 20 30 2c 20 7a 44 62 29 29 7b 0a 20 20  ab, 0, zDb)){.  
2449a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
2449b 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a  op_table;.    }.
2449c 20 20 20 20 69 66 28 20 69 73 56 69 65 77 20 29      if( isView )
2449d 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49  {.      if( !OMI
2449e 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d  T_TEMPDB && iDb=
2449f 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f  =1 ){.        co
244a0 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50  de = SQLITE_DROP
244a1 5f 54 45 4d 50 5f 56 49 45 57 3b 0a 20 20 20 20  _TEMP_VIEW;.    
244a2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
244a3 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44   code = SQLITE_D
244a4 52 4f 50 5f 56 49 45 57 3b 0a 20 20 20 20 20 20  ROP_VIEW;.      
244a5 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
244a6 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
244a7 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  LE.    }else if(
244a8 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
244a9 20 29 7b 0a 20 20 20 20 20 20 63 6f 64 65 20 3d   ){.      code =
244aa 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 41   SQLITE_DROP_VTA
244ab 42 4c 45 3b 0a 20 20 20 20 20 20 7a 41 72 67 32  BLE;.      zArg2
244ac 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 54 61   = sqlite3GetVTa
244ad 62 6c 65 28 64 62 2c 20 70 54 61 62 29 2d 3e 70  ble(db, pTab)->p
244ae 4d 6f 64 2d 3e 7a 4e 61 6d 65 3b 0a 23 65 6e 64  Mod->zName;.#end
244af 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  if.    }else{.  
244b0 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45      if( !OMIT_TE
244b1 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29  MPDB && iDb==1 )
244b2 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d  {.        code =
244b3 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d   SQLITE_DROP_TEM
244b4 50 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d  P_TABLE;.      }
244b5 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f  else{.        co
244b6 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50  de = SQLITE_DROP
244b7 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7d 0a  _TABLE;.      }.
244b8 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71      }.    if( sq
244b9 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
244ba 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 70 54 61  Parse, code, pTa
244bb 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 41 72 67 32 2c  b->zName, zArg2,
244bc 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67   zDb) ){.      g
244bd 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61  oto exit_drop_ta
244be 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ble;.    }.    i
244bf 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
244c0 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
244c1 54 45 5f 44 45 4c 45 54 45 2c 20 70 54 61 62 2d  TE_DELETE, pTab-
244c2 3e 7a 4e 61 6d 65 2c 20 30 2c 20 7a 44 62 29 20  >zName, 0, zDb) 
244c3 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78  ){.      goto ex
244c4 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20  it_drop_table;. 
244c5 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
244c6 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
244c7 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d  NICmp(pTab->zNam
244c8 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29  e, "sqlite_", 7)
244c9 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
244ca 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
244cb 65 2c 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79  e, "table %s may
244cc 20 6e 6f 74 20 62 65 20 64 72 6f 70 70 65 64 22   not be dropped"
244cd 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
244ce 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
244cf 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a 23  op_table;.  }..#
244d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
244d1 49 54 5f 56 49 45 57 0a 20 20 2f 2a 20 45 6e 73  IT_VIEW.  /* Ens
244d2 75 72 65 20 44 52 4f 50 20 54 41 42 4c 45 20 69  ure DROP TABLE i
244d3 73 20 6e 6f 74 20 75 73 65 64 20 6f 6e 20 61 20  s not used on a 
244d4 76 69 65 77 2c 20 61 6e 64 20 44 52 4f 50 20 56  view, and DROP V
244d5 49 45 57 20 69 73 20 6e 6f 74 20 75 73 65 64 0a  IEW is not used.
244d6 20 20 2a 2a 20 6f 6e 20 61 20 74 61 62 6c 65 2e    ** on a table.
244d7 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 56 69  .  */.  if( isVi
244d8 65 77 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c  ew && pTab->pSel
244d9 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  ect==0 ){.    sq
244da 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
244db 61 72 73 65 2c 20 22 75 73 65 20 44 52 4f 50 20  arse, "use DROP 
244dc 54 41 42 4c 45 20 74 6f 20 64 65 6c 65 74 65 20  TABLE to delete 
244dd 74 61 62 6c 65 20 25 73 22 2c 20 70 54 61 62 2d  table %s", pTab-
244de 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74  >zName);.    got
244df 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c  o exit_drop_tabl
244e0 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 69 73  e;.  }.  if( !is
244e1 56 69 65 77 20 26 26 20 70 54 61 62 2d 3e 70 53  View && pTab->pS
244e2 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c  elect ){.    sql
244e3 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
244e4 72 73 65 2c 20 22 75 73 65 20 44 52 4f 50 20 56  rse, "use DROP V
244e5 49 45 57 20 74 6f 20 64 65 6c 65 74 65 20 76 69  IEW to delete vi
244e6 65 77 20 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e  ew %s", pTab->zN
244e7 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ame);.    goto e
244e8 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a  xit_drop_table;.
244e9 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
244ea 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
244eb 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 74 61 62  o remove the tab
244ec 6c 65 20 66 72 6f 6d 20 74 68 65 20 6d 61 73 74  le from the mast
244ed 65 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f 6e  er table.  ** on
244ee 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 76 20   disk..  */.  v 
244ef 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
244f0 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
244f1 76 20 29 7b 0a 20 20 20 20 54 72 69 67 67 65 72  v ){.    Trigger
244f2 20 2a 70 54 72 69 67 67 65 72 3b 0a 20 20 20 20   *pTrigger;.    
244f3 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61  Db *pDb = &db->a
244f4 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20 73 71 6c  Db[iDb];.    sql
244f5 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
244f6 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
244f7 31 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65  1, iDb);..#ifnde
244f8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
244f9 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69  RTUALTABLE.    i
244fa 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
244fb 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  b) ){.      sqli
244fc 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c  te3VdbeAddOp0(v,
244fd 20 4f 50 5f 56 42 65 67 69 6e 29 3b 0a 20 20 20   OP_VBegin);.   
244fe 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71   }.#endif.    sq
244ff 6c 69 74 65 33 46 6b 44 72 6f 70 54 61 62 6c 65  lite3FkDropTable
24500 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 2c 20  (pParse, pName, 
24501 70 54 61 62 29 3b 0a 0a 20 20 20 20 2f 2a 20 44  pTab);..    /* D
24502 72 6f 70 20 61 6c 6c 20 74 72 69 67 67 65 72 73  rop all triggers
24503 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
24504 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
24505 20 64 72 6f 70 70 65 64 2e 20 43 6f 64 65 0a 20   dropped. Code. 
24506 20 20 20 2a 2a 20 69 73 20 67 65 6e 65 72 61 74     ** is generat
24507 65 64 20 74 6f 20 72 65 6d 6f 76 65 20 65 6e 74  ed to remove ent
24508 72 69 65 73 20 66 72 6f 6d 20 73 71 6c 69 74 65  ries from sqlite
24509 5f 6d 61 73 74 65 72 20 61 6e 64 2f 6f 72 0a 20  _master and/or. 
2450a 20 20 20 2a 2a 20 73 71 6c 69 74 65 5f 74 65 6d     ** sqlite_tem
2450b 70 5f 6d 61 73 74 65 72 20 69 66 20 72 65 71 75  p_master if requ
2450c 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ired..    */.   
2450d 20 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69   pTrigger = sqli
2450e 74 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 70  te3TriggerList(p
2450f 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20  Parse, pTab);.  
24510 20 20 77 68 69 6c 65 28 20 70 54 72 69 67 67 65    while( pTrigge
24511 72 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  r ){.      asser
24512 74 28 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63  t( pTrigger->pSc
24513 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68  hema==pTab->pSch
24514 65 6d 61 20 7c 7c 20 0a 20 20 20 20 20 20 20 20  ema || .        
24515 20 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68    pTrigger->pSch
24516 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e  ema==db->aDb[1].
24517 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20  pSchema );.     
24518 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67   sqlite3DropTrig
24519 67 65 72 50 74 72 28 70 50 61 72 73 65 2c 20 70  gerPtr(pParse, p
2451a 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 20 20  Trigger);.      
2451b 70 54 72 69 67 67 65 72 20 3d 20 70 54 72 69 67  pTrigger = pTrig
2451c 67 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  ger->pNext;.    
2451d 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
2451e 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45  E_OMIT_AUTOINCRE
2451f 4d 45 4e 54 0a 20 20 20 20 2f 2a 20 52 65 6d 6f  MENT.    /* Remo
24520 76 65 20 61 6e 79 20 65 6e 74 72 69 65 73 20 6f  ve any entries o
24521 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71  f the sqlite_seq
24522 75 65 6e 63 65 20 74 61 62 6c 65 20 61 73 73 6f  uence table asso
24523 63 69 61 74 65 64 20 77 69 74 68 0a 20 20 20 20  ciated with.    
24524 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  ** the table bei
24525 6e 67 20 64 72 6f 70 70 65 64 2e 20 54 68 69 73  ng dropped. This
24526 20 69 73 20 64 6f 6e 65 20 62 65 66 6f 72 65 20   is done before 
24527 74 68 65 20 74 61 62 6c 65 20 69 73 20 64 72 6f  the table is dro
24528 70 70 65 64 0a 20 20 20 20 2a 2a 20 61 74 20 74  pped.    ** at t
24529 68 65 20 62 74 72 65 65 20 6c 65 76 65 6c 2c 20  he btree level, 
2452a 69 6e 20 63 61 73 65 20 74 68 65 20 73 71 6c 69  in case the sqli
2452b 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c  te_sequence tabl
2452c 65 20 6e 65 65 64 73 20 74 6f 0a 20 20 20 20 2a  e needs to.    *
2452d 2a 20 6d 6f 76 65 20 61 73 20 61 20 72 65 73 75  * move as a resu
2452e 6c 74 20 6f 66 20 74 68 65 20 64 72 6f 70 20 28  lt of the drop (
2452f 63 61 6e 20 68 61 70 70 65 6e 20 69 6e 20 61 75  can happen in au
24530 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 29 2e  to-vacuum mode).
24531 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
24532 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26  pTab->tabFlags &
24533 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e   TF_Autoincremen
24534 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
24535 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50  e3NestedParse(pP
24536 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 22 44  arse,.        "D
24537 45 4c 45 54 45 20 46 52 4f 4d 20 25 73 2e 73 71  ELETE FROM %s.sq
24538 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 57 48  lite_sequence WH
24539 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a 20 20  ERE name=%Q",.  
2453a 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65        pDb->zName
2453b 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 0a 20 20  , pTab->zName.  
2453c 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 23 65 6e      );.    }.#en
2453d 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70  dif..    /* Drop
2453e 20 61 6c 6c 20 53 51 4c 49 54 45 5f 4d 41 53 54   all SQLITE_MAST
2453f 45 52 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64  ER table and ind
24540 65 78 20 65 6e 74 72 69 65 73 20 74 68 61 74 20  ex entries that 
24541 72 65 66 65 72 20 74 6f 20 74 68 65 0a 20 20 20  refer to the.   
24542 20 2a 2a 20 74 61 62 6c 65 2e 20 54 68 65 20 70   ** table. The p
24543 72 6f 67 72 61 6d 20 6e 61 6d 65 20 6c 6f 6f 70  rogram name loop
24544 73 20 74 68 72 6f 75 67 68 20 74 68 65 20 6d 61  s through the ma
24545 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20 64  ster table and d
24546 65 6c 65 74 65 73 0a 20 20 20 20 2a 2a 20 65 76  eletes.    ** ev
24547 65 72 79 20 72 6f 77 20 74 68 61 74 20 72 65 66  ery row that ref
24548 65 72 73 20 74 6f 20 61 20 74 61 62 6c 65 20 6f  ers to a table o
24549 66 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 20  f the same name 
2454a 61 73 20 74 68 65 20 6f 6e 65 20 62 65 69 6e 67  as the one being
2454b 0a 20 20 20 20 2a 2a 20 64 72 6f 70 70 65 64 2e  .    ** dropped.
2454c 20 54 72 69 67 67 65 72 73 20 61 72 65 20 68 61   Triggers are ha
2454d 6e 64 6c 65 64 20 73 65 70 65 72 61 74 65 6c 79  ndled seperately
2454e 20 62 65 63 61 75 73 65 20 61 20 74 72 69 67 67   because a trigg
2454f 65 72 20 63 61 6e 20 62 65 0a 20 20 20 20 2a 2a  er can be.    **
24550 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20   created in the 
24551 74 65 6d 70 20 64 61 74 61 62 61 73 65 20 74 68  temp database th
24552 61 74 20 72 65 66 65 72 73 20 74 6f 20 61 20 74  at refers to a t
24553 61 62 6c 65 20 69 6e 20 61 6e 6f 74 68 65 72 0a  able in another.
24554 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e      ** database.
24555 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
24556 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70  te3NestedParse(p
24557 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20  Parse, .        
24558 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e  "DELETE FROM %Q.
24559 25 73 20 57 48 45 52 45 20 74 62 6c 5f 6e 61 6d  %s WHERE tbl_nam
2455a 65 3d 25 51 20 61 6e 64 20 74 79 70 65 21 3d 27  e=%Q and type!='
2455b 74 72 69 67 67 65 72 27 22 2c 0a 20 20 20 20 20  trigger'",.     
2455c 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 53     pDb->zName, S
2455d 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29  CHEMA_TABLE(iDb)
2455e 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
2455f 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 61 6e 79  .    /* Drop any
24560 20 73 74 61 74 69 73 74 69 63 73 20 66 72 6f 6d   statistics from
24561 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
24562 31 20 74 61 62 6c 65 2c 20 69 66 20 69 74 20 65  1 table, if it e
24563 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 69 66 28  xists */.    if(
24564 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
24565 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 74  e(db, "sqlite_st
24566 61 74 31 22 2c 20 64 62 2d 3e 61 44 62 5b 69 44  at1", db->aDb[iD
24567 62 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20  b].zName) ){.   
24568 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64     sqlite3Nested
24569 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20  Parse(pParse,.  
2456a 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52        "DELETE FR
2456b 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61  OM %Q.sqlite_sta
2456c 74 31 20 57 48 45 52 45 20 74 62 6c 3d 25 51 22  t1 WHERE tbl=%Q"
2456d 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54  , pDb->zName, pT
2456e 61 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20  ab->zName.      
2456f 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  );.    }..    if
24570 28 20 21 69 73 56 69 65 77 20 26 26 20 21 49 73  ( !isView && !Is
24571 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
24572 0a 20 20 20 20 20 20 64 65 73 74 72 6f 79 54 61  .      destroyTa
24573 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  ble(pParse, pTab
24574 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
24575 20 52 65 6d 6f 76 65 20 74 68 65 20 74 61 62 6c   Remove the tabl
24576 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 53 51 4c  e entry from SQL
24577 69 74 65 27 73 20 69 6e 74 65 72 6e 61 6c 20 73  ite's internal s
24578 63 68 65 6d 61 20 61 6e 64 20 6d 6f 64 69 66 79  chema and modify
24579 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 63 68 65  .    ** the sche
2457a 6d 61 20 63 6f 6f 6b 69 65 2e 0a 20 20 20 20 2a  ma cookie..    *
2457b 2f 0a 20 20 20 20 69 66 28 20 49 73 56 69 72 74  /.    if( IsVirt
2457c 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
2457d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2457e 64 4f 70 34 28 76 2c 20 4f 50 5f 56 44 65 73 74  dOp4(v, OP_VDest
2457f 72 6f 79 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20  roy, iDb, 0, 0, 
24580 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b  pTab->zName, 0);
24581 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
24582 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
24583 4f 50 5f 44 72 6f 70 54 61 62 6c 65 2c 20 69 44  OP_DropTable, iD
24584 62 2c 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e 7a  b, 0, 0, pTab->z
24585 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 73 71  Name, 0);.    sq
24586 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69  lite3ChangeCooki
24587 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a  e(pParse, iDb);.
24588 20 20 7d 0a 20 20 73 71 6c 69 74 65 56 69 65 77    }.  sqliteView
24589 52 65 73 65 74 41 6c 6c 28 64 62 2c 20 69 44 62  ResetAll(db, iDb
2458a 29 3b 0a 0a 65 78 69 74 5f 64 72 6f 70 5f 74 61  );..exit_drop_ta
2458b 62 6c 65 3a 0a 20 20 73 71 6c 69 74 65 33 53 72  ble:.  sqlite3Sr
2458c 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  cListDelete(db, 
2458d 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pName);.}../*.**
2458e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2458f 20 63 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74   called to creat
24590 65 20 61 20 6e 65 77 20 66 6f 72 65 69 67 6e 20  e a new foreign 
24591 6b 65 79 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  key on the table
24592 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 75 6e  .** currently un
24593 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
24594 2e 20 20 70 46 72 6f 6d 43 6f 6c 20 64 65 74 65  .  pFromCol dete
24595 72 6d 69 6e 65 73 20 77 68 69 63 68 20 63 6f 6c  rmines which col
24596 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 63  umns.** in the c
24597 75 72 72 65 6e 74 20 74 61 62 6c 65 20 70 6f 69  urrent table poi
24598 6e 74 20 74 6f 20 74 68 65 20 66 6f 72 65 69 67  nt to the foreig
24599 6e 20 6b 65 79 2e 20 20 49 66 20 70 46 72 6f 6d  n key.  If pFrom
2459a 43 6f 6c 3d 3d 30 20 74 68 65 6e 0a 2a 2a 20 63  Col==0 then.** c
2459b 6f 6e 6e 65 63 74 20 74 68 65 20 6b 65 79 20 74  onnect the key t
2459c 6f 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d  o the last colum
2459d 6e 20 69 6e 73 65 72 74 65 64 2e 20 20 70 54 6f  n inserted.  pTo
2459e 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 0a   is the name of.
2459f 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 72 65 66  ** the table ref
245a0 65 72 72 65 64 20 74 6f 2e 20 20 70 54 6f 43 6f  erred to.  pToCo
245a1 6c 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 74  l is a list of t
245a2 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6f 74 68  ables in the oth
245a3 65 72 0a 2a 2a 20 70 54 6f 20 74 61 62 6c 65 20  er.** pTo table 
245a4 74 68 61 74 20 74 68 65 20 66 6f 72 65 69 67 6e  that the foreign
245a5 20 6b 65 79 20 70 6f 69 6e 74 73 20 74 6f 2e 20   key points to. 
245a6 20 66 6c 61 67 73 20 63 6f 6e 74 61 69 6e 73 20   flags contains 
245a7 61 6c 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  all.** informati
245a8 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 63 6f 6e  on about the con
245a9 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e  flict resolution
245aa 20 61 6c 67 6f 72 69 74 68 6d 73 20 73 70 65 63   algorithms spec
245ab 69 66 69 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20  ified.** in the 
245ac 4f 4e 20 44 45 4c 45 54 45 2c 20 4f 4e 20 55 50  ON DELETE, ON UP
245ad 44 41 54 45 20 61 6e 64 20 4f 4e 20 49 4e 53 45  DATE and ON INSE
245ae 52 54 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a  RT clauses..**.*
245af 2a 20 41 6e 20 46 4b 65 79 20 73 74 72 75 63 74  * An FKey struct
245b0 75 72 65 20 69 73 20 63 72 65 61 74 65 64 20 61  ure is created a
245b1 6e 64 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  nd added to the 
245b2 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 0a  table currently.
245b3 2a 2a 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  ** under constru
245b4 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 70 50 61  ction in the pPa
245b5 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 66  rse->pNewTable f
245b6 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ield..**.** The 
245b7 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20 73  foreign key is s
245b8 65 74 20 66 6f 72 20 49 4d 4d 45 44 49 41 54 45  et for IMMEDIATE
245b9 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20 41 20   processing.  A 
245ba 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 0a  subsequent call.
245bb 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 44 65 66  ** to sqlite3Def
245bc 65 72 46 6f 72 65 69 67 6e 4b 65 79 28 29 20 6d  erForeignKey() m
245bd 69 67 68 74 20 63 68 61 6e 67 65 20 74 68 69 73  ight change this
245be 20 74 6f 20 44 45 46 45 52 52 45 44 2e 0a 2a 2f   to DEFERRED..*/
245bf 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
245c0 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61  void sqlite3Crea
245c1 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 0a 20 20  teForeignKey(.  
245c2 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
245c3 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
245c4 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70  context */.  Exp
245c5 72 4c 69 73 74 20 2a 70 46 72 6f 6d 43 6f 6c 2c  rList *pFromCol,
245c6 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20    /* Columns in 
245c7 74 68 69 73 20 74 61 62 6c 65 20 74 68 61 74 20  this table that 
245c8 70 6f 69 6e 74 20 74 6f 20 6f 74 68 65 72 20 74  point to other t
245c9 61 62 6c 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  able */.  Token 
245ca 2a 70 54 6f 2c 20 20 20 20 20 20 20 20 20 20 2f  *pTo,          /
245cb 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 74  * Name of the ot
245cc 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 45  her table */.  E
245cd 78 70 72 4c 69 73 74 20 2a 70 54 6f 43 6f 6c 2c  xprList *pToCol,
245ce 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20 69      /* Columns i
245cf 6e 20 74 68 65 20 6f 74 68 65 72 20 74 61 62 6c  n the other tabl
245d0 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  e */.  int flags
245d1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
245d2 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69  onflict resoluti
245d3 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 2e 20 2a  on algorithms. *
245d4 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  /.){.  sqlite3 *
245d5 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
245d6 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
245d7 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
245d8 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 20 3d  .  FKey *pFKey =
245d9 20 30 3b 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78   0;.  FKey *pNex
245da 74 54 6f 3b 0a 20 20 54 61 62 6c 65 20 2a 70 20  tTo;.  Table *p 
245db 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  = pParse->pNewTa
245dc 62 6c 65 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65  ble;.  int nByte
245dd 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  ;.  int i;.  int
245de 20 6e 43 6f 6c 3b 0a 20 20 63 68 61 72 20 2a 7a   nCol;.  char *z
245df 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f  ;..  assert( pTo
245e0 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d  !=0 );.  if( p==
245e1 30 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f  0 || IN_DECLARE_
245e2 56 54 41 42 20 29 20 67 6f 74 6f 20 66 6b 5f 65  VTAB ) goto fk_e
245e3 6e 64 3b 0a 20 20 69 66 28 20 70 46 72 6f 6d 43  nd;.  if( pFromC
245e4 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ol==0 ){.    int
245e5 20 69 43 6f 6c 20 3d 20 70 2d 3e 6e 43 6f 6c 2d   iCol = p->nCol-
245e6 31 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52  1;.    if( NEVER
245e7 28 69 43 6f 6c 3c 30 29 20 29 20 67 6f 74 6f 20  (iCol<0) ) goto 
245e8 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 69 66 28 20  fk_end;.    if( 
245e9 70 54 6f 43 6f 6c 20 26 26 20 70 54 6f 43 6f 6c  pToCol && pToCol
245ea 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20 20  ->nExpr!=1 ){.  
245eb 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
245ec 4d 73 67 28 70 50 61 72 73 65 2c 20 22 66 6f 72  Msg(pParse, "for
245ed 65 69 67 6e 20 6b 65 79 20 6f 6e 20 25 73 22 0a  eign key on %s".
245ee 20 20 20 20 20 20 20 20 20 22 20 73 68 6f 75 6c           " shoul
245ef 64 20 72 65 66 65 72 65 6e 63 65 20 6f 6e 6c 79  d reference only
245f0 20 6f 6e 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74   one column of t
245f1 61 62 6c 65 20 25 54 22 2c 0a 20 20 20 20 20 20  able %T",.      
245f2 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d     p->aCol[iCol]
245f3 2e 7a 4e 61 6d 65 2c 20 70 54 6f 29 3b 0a 20 20  .zName, pTo);.  
245f4 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b      goto fk_end;
245f5 0a 20 20 20 20 7d 0a 20 20 20 20 6e 43 6f 6c 20  .    }.    nCol 
245f6 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  = 1;.  }else if(
245f7 20 70 54 6f 43 6f 6c 20 26 26 20 70 54 6f 43 6f   pToCol && pToCo
245f8 6c 2d 3e 6e 45 78 70 72 21 3d 70 46 72 6f 6d 43  l->nExpr!=pFromC
245f9 6f 6c 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20  ol->nExpr ){.   
245fa 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
245fb 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
245fc 20 22 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75   "number of colu
245fd 6d 6e 73 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b  mns in foreign k
245fe 65 79 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63  ey does not matc
245ff 68 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  h the number of 
24600 22 0a 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d  ".        "colum
24601 6e 73 20 69 6e 20 74 68 65 20 72 65 66 65 72 65  ns in the refere
24602 6e 63 65 64 20 74 61 62 6c 65 22 29 3b 0a 20 20  nced table");.  
24603 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20    goto fk_end;. 
24604 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 43 6f 6c   }else{.    nCol
24605 20 3d 20 70 46 72 6f 6d 43 6f 6c 2d 3e 6e 45 78   = pFromCol->nEx
24606 70 72 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20  pr;.  }.  nByte 
24607 3d 20 73 69 7a 65 6f 66 28 2a 70 46 4b 65 79 29  = sizeof(*pFKey)
24608 20 2b 20 28 6e 43 6f 6c 2d 31 29 2a 73 69 7a 65   + (nCol-1)*size
24609 6f 66 28 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30  of(pFKey->aCol[0
2460a 5d 29 20 2b 20 70 54 6f 2d 3e 6e 20 2b 20 31 3b  ]) + pTo->n + 1;
2460b 0a 20 20 69 66 28 20 70 54 6f 43 6f 6c 20 29 7b  .  if( pToCol ){
2460c 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
2460d 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70 72 3b 20 69  pToCol->nExpr; i
2460e 2b 2b 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65  ++){.      nByte
2460f 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65   += sqlite3Strle
24610 6e 33 30 28 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d  n30(pToCol->a[i]
24611 2e 7a 4e 61 6d 65 29 20 2b 20 31 3b 0a 20 20 20  .zName) + 1;.   
24612 20 7d 0a 20 20 7d 0a 20 20 70 46 4b 65 79 20 3d   }.  }.  pFKey =
24613 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
24614 5a 65 72 6f 28 64 62 2c 20 6e 42 79 74 65 20 29  Zero(db, nByte )
24615 3b 0a 20 20 69 66 28 20 70 46 4b 65 79 3d 3d 30  ;.  if( pFKey==0
24616 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f   ){.    goto fk_
24617 65 6e 64 3b 0a 20 20 7d 0a 20 20 70 46 4b 65 79  end;.  }.  pFKey
24618 2d 3e 70 46 72 6f 6d 20 3d 20 70 3b 0a 20 20 70  ->pFrom = p;.  p
24619 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 20  FKey->pNextFrom 
2461a 3d 20 70 2d 3e 70 46 4b 65 79 3b 0a 20 20 7a 20  = p->pFKey;.  z 
2461b 3d 20 28 63 68 61 72 2a 29 26 70 46 4b 65 79 2d  = (char*)&pFKey-
2461c 3e 61 43 6f 6c 5b 6e 43 6f 6c 5d 3b 0a 20 20 70  >aCol[nCol];.  p
2461d 46 4b 65 79 2d 3e 7a 54 6f 20 3d 20 7a 3b 0a 20  FKey->zTo = z;. 
2461e 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54 6f 2d 3e   memcpy(z, pTo->
2461f 7a 2c 20 70 54 6f 2d 3e 6e 29 3b 0a 20 20 7a 5b  z, pTo->n);.  z[
24620 70 54 6f 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 73  pTo->n] = 0;.  s
24621 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 7a 29  qlite3Dequote(z)
24622 3b 0a 20 20 7a 20 2b 3d 20 70 54 6f 2d 3e 6e 2b  ;.  z += pTo->n+
24623 31 3b 0a 20 20 70 46 4b 65 79 2d 3e 6e 43 6f 6c  1;.  pFKey->nCol
24624 20 3d 20 6e 43 6f 6c 3b 0a 20 20 69 66 28 20 70   = nCol;.  if( p
24625 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20  FromCol==0 ){.  
24626 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d    pFKey->aCol[0]
24627 2e 69 46 72 6f 6d 20 3d 20 70 2d 3e 6e 43 6f 6c  .iFrom = p->nCol
24628 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  -1;.  }else{.   
24629 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c   for(i=0; i<nCol
2462a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e  ; i++){.      in
2462b 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a  t j;.      for(j
2462c 3d 30 3b 20 6a 3c 70 2d 3e 6e 43 6f 6c 3b 20 6a  =0; j<p->nCol; j
2462d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
2462e 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
2462f 70 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65  p->aCol[j].zName
24630 2c 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61 5b 69 5d  , pFromCol->a[i]
24631 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20  .zName)==0 ){.  
24632 20 20 20 20 20 20 20 20 70 46 4b 65 79 2d 3e 61          pFKey->a
24633 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 20 3d 20 6a  Col[i].iFrom = j
24634 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
24635 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
24636 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6a     }.      if( j
24637 3e 3d 70 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20  >=p->nCol ){.   
24638 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
24639 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20  rMsg(pParse, .  
2463a 20 20 20 20 20 20 20 20 22 75 6e 6b 6e 6f 77 6e          "unknown
2463b 20 63 6f 6c 75 6d 6e 20 5c 22 25 73 5c 22 20 69   column \"%s\" i
2463c 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 65  n foreign key de
2463d 66 69 6e 69 74 69 6f 6e 22 2c 20 0a 20 20 20 20  finition", .    
2463e 20 20 20 20 20 20 70 46 72 6f 6d 43 6f 6c 2d 3e        pFromCol->
2463f 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20  a[i].zName);.   
24640 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64       goto fk_end
24641 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
24642 20 20 7d 0a 20 20 69 66 28 20 70 54 6f 43 6f 6c    }.  if( pToCol
24643 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
24644 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20   i<nCol; i++){. 
24645 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c       int n = sql
24646 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 54 6f  ite3Strlen30(pTo
24647 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29  Col->a[i].zName)
24648 3b 0a 20 20 20 20 20 20 70 46 4b 65 79 2d 3e 61  ;.      pFKey->a
24649 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 20 3d 20 7a 3b  Col[i].zCol = z;
2464a 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c  .      memcpy(z,
2464b 20 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e   pToCol->a[i].zN
2464c 61 6d 65 2c 20 6e 29 3b 0a 20 20 20 20 20 20 7a  ame, n);.      z
2464d 5b 6e 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7a  [n] = 0;.      z
2464e 20 2b 3d 20 6e 2b 31 3b 0a 20 20 20 20 7d 0a 20   += n+1;.    }. 
2464f 20 7d 0a 20 20 70 46 4b 65 79 2d 3e 69 73 44 65   }.  pFKey->isDe
24650 66 65 72 72 65 64 20 3d 20 30 3b 0a 20 20 70 46  ferred = 0;.  pF
24651 4b 65 79 2d 3e 61 41 63 74 69 6f 6e 5b 30 5d 20  Key->aAction[0] 
24652 3d 20 28 75 38 29 28 66 6c 61 67 73 20 26 20 30  = (u8)(flags & 0
24653 78 66 66 29 3b 20 20 20 20 20 20 20 20 20 20 20  xff);           
24654 20 2f 2a 20 4f 4e 20 44 45 4c 45 54 45 20 61 63   /* ON DELETE ac
24655 74 69 6f 6e 20 2a 2f 0a 20 20 70 46 4b 65 79 2d  tion */.  pFKey-
24656 3e 61 41 63 74 69 6f 6e 5b 31 5d 20 3d 20 28 75  >aAction[1] = (u
24657 38 29 28 28 66 6c 61 67 73 20 3e 3e 20 38 20 29  8)((flags >> 8 )
24658 20 26 20 30 78 66 66 29 3b 20 20 20 20 2f 2a 20   & 0xff);    /* 
24659 4f 4e 20 55 50 44 41 54 45 20 61 63 74 69 6f 6e  ON UPDATE action
2465a 20 2a 2f 0a 0a 20 20 70 4e 65 78 74 54 6f 20 3d   */..  pNextTo =
2465b 20 28 46 4b 65 79 20 2a 29 73 71 6c 69 74 65 33   (FKey *)sqlite3
2465c 48 61 73 68 49 6e 73 65 72 74 28 26 70 2d 3e 70  HashInsert(&p->p
2465d 53 63 68 65 6d 61 2d 3e 66 6b 65 79 48 61 73 68  Schema->fkeyHash
2465e 2c 20 0a 20 20 20 20 20 20 70 46 4b 65 79 2d 3e  , .      pFKey->
2465f 7a 54 6f 2c 20 73 71 6c 69 74 65 33 53 74 72 6c  zTo, sqlite3Strl
24660 65 6e 33 30 28 70 46 4b 65 79 2d 3e 7a 54 6f 29  en30(pFKey->zTo)
24661 2c 20 28 76 6f 69 64 20 2a 29 70 46 4b 65 79 0a  , (void *)pFKey.
24662 20 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 78 74    );.  if( pNext
24663 54 6f 3d 3d 70 46 4b 65 79 20 29 7b 0a 20 20 20  To==pFKey ){.   
24664 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
24665 64 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20  d = 1;.    goto 
24666 66 6b 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 69 66  fk_end;.  }.  if
24667 28 20 70 4e 65 78 74 54 6f 20 29 7b 0a 20 20 20  ( pNextTo ){.   
24668 20 61 73 73 65 72 74 28 20 70 4e 65 78 74 54 6f   assert( pNextTo
24669 2d 3e 70 50 72 65 76 54 6f 3d 3d 30 20 29 3b 0a  ->pPrevTo==0 );.
2466a 20 20 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74      pFKey->pNext
2466b 54 6f 20 3d 20 70 4e 65 78 74 54 6f 3b 0a 20 20  To = pNextTo;.  
2466c 20 20 70 4e 65 78 74 54 6f 2d 3e 70 50 72 65 76    pNextTo->pPrev
2466d 54 6f 20 3d 20 70 46 4b 65 79 3b 0a 20 20 7d 0a  To = pFKey;.  }.
2466e 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 66  .  /* Link the f
2466f 6f 72 65 69 67 6e 20 6b 65 79 20 74 6f 20 74 68  oreign key to th
24670 65 20 74 61 62 6c 65 20 61 73 20 74 68 65 20 6c  e table as the l
24671 61 73 74 20 73 74 65 70 2e 0a 20 20 2a 2f 0a 20  ast step..  */. 
24672 20 70 2d 3e 70 46 4b 65 79 20 3d 20 70 46 4b 65   p->pFKey = pFKe
24673 79 3b 0a 20 20 70 46 4b 65 79 20 3d 20 30 3b 0a  y;.  pFKey = 0;.
24674 0a 66 6b 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74  .fk_end:.  sqlit
24675 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 46 4b  e3DbFree(db, pFK
24676 65 79 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 21  ey);.#endif /* !
24677 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
24678 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29  MIT_FOREIGN_KEY)
24679 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70   */.  sqlite3Exp
2467a 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
2467b 70 46 72 6f 6d 43 6f 6c 29 3b 0a 20 20 73 71 6c  pFromCol);.  sql
2467c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
2467d 74 65 28 64 62 2c 20 70 54 6f 43 6f 6c 29 3b 0a  te(db, pToCol);.
2467e 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
2467f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
24680 77 68 65 6e 20 61 6e 20 49 4e 49 54 49 41 4c 4c  when an INITIALL
24681 59 20 49 4d 4d 45 44 49 41 54 45 20 6f 72 20 49  Y IMMEDIATE or I
24682 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45  NITIALLY DEFERRE
24683 44 0a 2a 2a 20 63 6c 61 75 73 65 20 69 73 20 73  D.** clause is s
24684 65 65 6e 20 61 73 20 70 61 72 74 20 6f 66 20 61  een as part of a
24685 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 66   foreign key def
24686 69 6e 69 74 69 6f 6e 2e 20 20 54 68 65 20 69 73  inition.  The is
24687 44 65 66 65 72 72 65 64 0a 2a 2a 20 70 61 72 61  Deferred.** para
24688 6d 65 74 65 72 20 69 73 20 31 20 66 6f 72 20 49  meter is 1 for I
24689 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52 45  NITIALLY DEFERRE
2468a 44 20 61 6e 64 20 30 20 66 6f 72 20 49 4e 49 54  D and 0 for INIT
2468b 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 2e  IALLY IMMEDIATE.
2468c 0a 2a 2a 20 54 68 65 20 62 65 68 61 76 69 6f 72  .** The behavior
2468d 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
2468e 65 6e 74 6c 79 20 63 72 65 61 74 65 64 20 66 6f  ently created fo
2468f 72 65 69 67 6e 20 6b 65 79 20 69 73 20 61 64 6a  reign key is adj
24690 75 73 74 65 64 0a 2a 2a 20 61 63 63 6f 72 64 69  usted.** accordi
24691 6e 67 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ngly..*/.SQLITE_
24692 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
24693 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e  ite3DeferForeign
24694 4b 65 79 28 50 61 72 73 65 20 2a 70 50 61 72 73  Key(Parse *pPars
24695 65 2c 20 69 6e 74 20 69 73 44 65 66 65 72 72 65  e, int isDeferre
24696 64 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  d){.#ifndef SQLI
24697 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  TE_OMIT_FOREIGN_
24698 4b 45 59 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  KEY.  Table *pTa
24699 62 3b 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79  b;.  FKey *pFKey
2469a 3b 0a 20 20 69 66 28 20 28 70 54 61 62 20 3d 20  ;.  if( (pTab = 
2469b 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
2469c 65 29 3d 3d 30 20 7c 7c 20 28 70 46 4b 65 79 20  e)==0 || (pFKey 
2469d 3d 20 70 54 61 62 2d 3e 70 46 4b 65 79 29 3d 3d  = pTab->pFKey)==
2469e 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73  0 ) return;.  as
2469f 73 65 72 74 28 20 69 73 44 65 66 65 72 72 65 64  sert( isDeferred
246a0 3d 3d 30 20 7c 7c 20 69 73 44 65 66 65 72 72 65  ==0 || isDeferre
246a1 64 3d 3d 31 20 29 3b 20 2f 2a 20 45 56 3a 20 52  d==1 ); /* EV: R
246a2 2d 33 30 33 32 33 2d 32 31 39 31 37 20 2a 2f 0a  -30323-21917 */.
246a3 20 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72    pFKey->isDefer
246a4 72 65 64 20 3d 20 28 75 38 29 69 73 44 65 66 65  red = (u8)isDefe
246a5 72 72 65 64 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a  rred;.#endif.}..
246a6 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
246a7 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 72  ode that will er
246a8 61 73 65 20 61 6e 64 20 72 65 66 69 6c 6c 20 69  ase and refill i
246a9 6e 64 65 78 20 2a 70 49 64 78 2e 20 20 54 68 69  ndex *pIdx.  Thi
246aa 73 20 69 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20  s is.** used to 
246ab 69 6e 69 74 69 61 6c 69 7a 65 20 61 20 6e 65 77  initialize a new
246ac 6c 79 20 63 72 65 61 74 65 64 20 69 6e 64 65 78  ly created index
246ad 20 6f 72 20 74 6f 20 72 65 63 6f 6d 70 75 74 65   or to recompute
246ae 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20   the.** content 
246af 6f 66 20 61 6e 20 69 6e 64 65 78 20 69 6e 20 72  of an index in r
246b0 65 73 70 6f 6e 73 65 20 74 6f 20 61 20 52 45 49  esponse to a REI
246b1 4e 44 45 58 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a  NDEX command..**
246b2 0a 2a 2a 20 69 66 20 6d 65 6d 52 6f 6f 74 50 61  .** if memRootPa
246b3 67 65 20 69 73 20 6e 6f 74 20 6e 65 67 61 74 69  ge is not negati
246b4 76 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  ve, it means tha
246b5 74 20 74 68 65 20 69 6e 64 65 78 20 69 73 20 6e  t the index is n
246b6 65 77 6c 79 0a 2a 2a 20 63 72 65 61 74 65 64 2e  ewly.** created.
246b7 20 20 54 68 65 20 72 65 67 69 73 74 65 72 20 73    The register s
246b8 70 65 63 69 66 69 65 64 20 62 79 20 6d 65 6d 52  pecified by memR
246b9 6f 6f 74 50 61 67 65 20 63 6f 6e 74 61 69 6e 73  ootPage contains
246ba 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67   the.** root pag
246bb 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
246bc 69 6e 64 65 78 2e 20 20 49 66 20 6d 65 6d 52 6f  index.  If memRo
246bd 6f 74 50 61 67 65 20 69 73 20 6e 65 67 61 74 69  otPage is negati
246be 76 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20  ve, then.** the 
246bf 69 6e 64 65 78 20 61 6c 72 65 61 64 79 20 65 78  index already ex
246c0 69 73 74 73 20 61 6e 64 20 6d 75 73 74 20 62 65  ists and must be
246c1 20 63 6c 65 61 72 65 64 20 62 65 66 6f 72 65 20   cleared before 
246c2 62 65 69 6e 67 20 72 65 66 69 6c 6c 65 64 20 61  being refilled a
246c3 6e 64 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70  nd.** the root p
246c4 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  age number of th
246c5 65 20 69 6e 64 65 78 20 69 73 20 74 61 6b 65 6e  e index is taken
246c6 20 66 72 6f 6d 20 70 49 6e 64 65 78 2d 3e 74 6e   from pIndex->tn
246c7 75 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  um..*/.static vo
246c8 69 64 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c  id sqlite3Refill
246c9 49 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 61  Index(Parse *pPa
246ca 72 73 65 2c 20 49 6e 64 65 78 20 2a 70 49 6e 64  rse, Index *pInd
246cb 65 78 2c 20 69 6e 74 20 6d 65 6d 52 6f 6f 74 50  ex, int memRootP
246cc 61 67 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  age){.  Table *p
246cd 54 61 62 20 3d 20 70 49 6e 64 65 78 2d 3e 70 54  Tab = pIndex->pT
246ce 61 62 6c 65 3b 20 20 2f 2a 20 54 68 65 20 74 61  able;  /* The ta
246cf 62 6c 65 20 74 68 61 74 20 69 73 20 69 6e 64 65  ble that is inde
246d0 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  xed */.  int iTa
246d1 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  b = pParse->nTab
246d2 2b 2b 3b 20 20 20 20 20 2f 2a 20 42 74 72 65 65  ++;     /* Btree
246d3 20 63 75 72 73 6f 72 20 75 73 65 64 20 66 6f 72   cursor used for
246d4 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 69   pTab */.  int i
246d5 49 64 78 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Idx = pParse->nT
246d6 61 62 2b 2b 3b 20 20 20 20 20 2f 2a 20 42 74 72  ab++;     /* Btr
246d7 65 65 20 63 75 72 73 6f 72 20 75 73 65 64 20 66  ee cursor used f
246d8 6f 72 20 70 49 6e 64 65 78 20 2a 2f 0a 20 20 69  or pIndex */.  i
246d9 6e 74 20 61 64 64 72 31 3b 20 20 20 20 20 20 20  nt addr1;       
246da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
246db 20 41 64 64 72 65 73 73 20 6f 66 20 74 6f 70 20   Address of top 
246dc 6f 66 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  of loop */.  int
246dd 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 20 20   tnum;          
246de 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
246df 6f 6f 74 20 70 61 67 65 20 6f 66 20 69 6e 64 65  oot page of inde
246e0 78 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20  x */.  Vdbe *v; 
246e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
246e2 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74        /* Generat
246e3 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69 73  e code into this
246e4 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
246e5 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70   */.  KeyInfo *p
246e6 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  Key;            
246e7 20 20 20 20 20 2f 2a 20 4b 65 79 49 6e 66 6f 20       /* KeyInfo 
246e8 66 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  for index */.  i
246e9 6e 74 20 72 65 67 49 64 78 4b 65 79 3b 20 20 20  nt regIdxKey;   
246ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
246eb 20 52 65 67 69 73 74 65 72 73 20 63 6f 6e 74 61   Registers conta
246ec 69 6e 69 6e 67 20 74 68 65 20 69 6e 64 65 78 20  ining the index 
246ed 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  key */.  int reg
246ee 52 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20 20  Record;         
246ef 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
246f0 74 65 72 20 68 6f 6c 64 69 6e 67 20 61 73 73 65  ter holding asse
246f1 6d 62 6c 69 65 64 20 69 6e 64 65 78 20 72 65 63  mblied index rec
246f2 6f 72 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ord */.  sqlite3
246f3 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
246f4 62 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64  b;      /* The d
246f5 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
246f6 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 20  on */.  int iDb 
246f7 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
246f8 6f 49 6e 64 65 78 28 64 62 2c 20 70 49 6e 64 65  oIndex(db, pInde
246f9 78 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 23 69  x->pSchema);..#i
246fa 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
246fb 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
246fc 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
246fd 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
246fe 51 4c 49 54 45 5f 52 45 49 4e 44 45 58 2c 20 70  QLITE_REINDEX, p
246ff 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 30 2c  Index->zName, 0,
24700 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69  .      db->aDb[i
24701 44 62 5d 2e 7a 4e 61 6d 65 20 29 20 29 7b 0a 20  Db].zName ) ){. 
24702 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23     return;.  }.#
24703 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 71 75  endif..  /* Requ
24704 69 72 65 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b  ire a write-lock
24705 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f   on the table to
24706 20 70 65 72 66 6f 72 6d 20 74 68 69 73 20 6f 70   perform this op
24707 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c  eration */.  sql
24708 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50  ite3TableLock(pP
24709 61 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62 2d  arse, iDb, pTab-
2470a 3e 74 6e 75 6d 2c 20 31 2c 20 70 54 61 62 2d 3e  >tnum, 1, pTab->
2470b 7a 4e 61 6d 65 29 3b 0a 0a 20 20 76 20 3d 20 73  zName);..  v = s
2470c 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
2470d 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d  arse);.  if( v==
2470e 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
2470f 28 20 6d 65 6d 52 6f 6f 74 50 61 67 65 3e 3d 30  ( memRootPage>=0
24710 20 29 7b 0a 20 20 20 20 74 6e 75 6d 20 3d 20 6d   ){.    tnum = m
24711 65 6d 52 6f 6f 74 50 61 67 65 3b 0a 20 20 7d 65  emRootPage;.  }e
24712 6c 73 65 7b 0a 20 20 20 20 74 6e 75 6d 20 3d 20  lse{.    tnum = 
24713 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 3b 0a 20 20  pIndex->tnum;.  
24714 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24715 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c  Op2(v, OP_Clear,
24716 20 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 7d   tnum, iDb);.  }
24717 0a 20 20 70 4b 65 79 20 3d 20 73 71 6c 69 74 65  .  pKey = sqlite
24718 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50  3IndexKeyinfo(pP
24719 61 72 73 65 2c 20 70 49 6e 64 65 78 29 3b 0a 20  arse, pIndex);. 
2471a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2471b 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  p4(v, OP_OpenWri
2471c 74 65 2c 20 69 49 64 78 2c 20 74 6e 75 6d 2c 20  te, iIdx, tnum, 
2471d 69 44 62 2c 20 0a 20 20 20 20 20 20 20 20 20 20  iDb, .          
2471e 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 20            (char 
2471f 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e  *)pKey, P4_KEYIN
24720 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 69  FO_HANDOFF);.  i
24721 66 28 20 6d 65 6d 52 6f 6f 74 50 61 67 65 3e 3d  f( memRootPage>=
24722 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
24723 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
24724 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  1);.  }.  sqlite
24725 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73  3OpenTable(pPars
24726 65 2c 20 69 54 61 62 2c 20 69 44 62 2c 20 70 54  e, iTab, iDb, pT
24727 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29  ab, OP_OpenRead)
24728 3b 0a 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69  ;.  addr1 = sqli
24729 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2472a 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 54 61 62   OP_Rewind, iTab
2472b 2c 20 30 29 3b 0a 20 20 72 65 67 52 65 63 6f 72  , 0);.  regRecor
2472c 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  d = sqlite3GetTe
2472d 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
2472e 20 72 65 67 49 64 78 4b 65 79 20 3d 20 73 71 6c   regIdxKey = sql
2472f 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65  ite3GenerateInde
24730 78 4b 65 79 28 70 50 61 72 73 65 2c 20 70 49 6e  xKey(pParse, pIn
24731 64 65 78 2c 20 69 54 61 62 2c 20 72 65 67 52 65  dex, iTab, regRe
24732 63 6f 72 64 2c 20 31 29 3b 0a 20 20 69 66 28 20  cord, 1);.  if( 
24733 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 21  pIndex->onError!
24734 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20 20 20  =OE_None ){.    
24735 63 6f 6e 73 74 20 69 6e 74 20 72 65 67 52 6f 77  const int regRow
24736 69 64 20 3d 20 72 65 67 49 64 78 4b 65 79 20 2b  id = regIdxKey +
24737 20 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e   pIndex->nColumn
24738 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20  ;.    const int 
24739 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j2 = sqlite3Vdbe
2473a 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 20 2b  CurrentAddr(v) +
2473b 20 32 3b 0a 20 20 20 20 76 6f 69 64 20 2a 20 63   2;.    void * c
2473c 6f 6e 73 74 20 70 52 65 67 4b 65 79 20 3d 20 53  onst pRegKey = S
2473d 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52  QLITE_INT_TO_PTR
2473e 28 72 65 67 49 64 78 4b 65 79 29 3b 0a 0a 20 20  (regIdxKey);..  
2473f 20 20 2f 2a 20 54 68 65 20 72 65 67 69 73 74 65    /* The registe
24740 72 73 20 61 63 63 65 73 73 65 64 20 62 79 20 74  rs accessed by t
24741 68 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 6f  he OP_IsUnique o
24742 70 63 6f 64 65 20 77 65 72 65 20 61 6c 6c 6f 63  pcode were alloc
24743 61 74 65 64 0a 20 20 20 20 2a 2a 20 75 73 69 6e  ated.    ** usin
24744 67 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  g sqlite3GetTemp
24745 52 61 6e 67 65 28 29 20 69 6e 73 69 64 65 20 6f  Range() inside o
24746 66 20 74 68 65 20 73 71 6c 69 74 65 33 47 65 6e  f the sqlite3Gen
24747 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 29 0a  erateIndexKey().
24748 20 20 20 20 2a 2a 20 63 61 6c 6c 20 61 62 6f 76      ** call abov
24749 65 2e 20 4a 75 73 74 20 62 65 66 6f 72 65 20 74  e. Just before t
2474a 68 61 74 20 66 75 6e 63 74 69 6f 6e 20 77 61 73  hat function was
2474b 20 66 72 65 65 64 20 74 68 65 79 20 77 65 72 65   freed they were
2474c 20 72 65 6c 65 61 73 65 64 0a 20 20 20 20 2a 2a   released.    **
2474d 20 28 6d 61 64 65 20 61 76 61 69 6c 61 62 6c 65   (made available
2474e 20 74 6f 20 74 68 65 20 63 6f 6d 70 69 6c 65 72   to the compiler
2474f 20 66 6f 72 20 72 65 75 73 65 29 20 75 73 69 6e   for reuse) usin
24750 67 20 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  g .    ** sqlite
24751 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67  3ReleaseTempRang
24752 65 28 29 2e 20 53 6f 20 69 6e 20 73 6f 6d 65 20  e(). So in some 
24753 77 61 79 73 20 68 61 76 69 6e 67 20 74 68 65 20  ways having the 
24754 4f 50 5f 49 73 55 6e 69 71 75 65 0a 20 20 20 20  OP_IsUnique.    
24755 2a 2a 20 6f 70 63 6f 64 65 20 75 73 65 20 74 68  ** opcode use th
24756 65 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20  e values stored 
24757 77 69 74 68 69 6e 20 73 65 65 6d 73 20 64 61 6e  within seems dan
24758 67 65 72 6f 75 73 2e 20 48 6f 77 65 76 65 72 2c  gerous. However,
24759 20 73 69 6e 63 65 0a 20 20 20 20 2a 2a 20 77 65   since.    ** we
2475a 20 63 61 6e 20 62 65 20 73 75 72 65 20 74 68 61   can be sure tha
2475b 74 20 6e 6f 20 6f 74 68 65 72 20 74 65 6d 70 20  t no other temp 
2475c 72 65 67 69 73 74 65 72 73 20 68 61 76 65 20 62  registers have b
2475d 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20  een allocated.  
2475e 20 20 2a 2a 20 73 69 6e 63 65 20 73 71 6c 69 74    ** since sqlit
2475f 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e  e3ReleaseTempRan
24760 67 65 28 29 20 77 61 73 20 63 61 6c 6c 65 64 2c  ge() was called,
24761 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 64   it is safe to d
24762 6f 20 73 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  o so..    */.   
24763 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24764 70 34 28 76 2c 20 4f 50 5f 49 73 55 6e 69 71 75  p4(v, OP_IsUniqu
24765 65 2c 20 69 49 64 78 2c 20 6a 32 2c 20 72 65 67  e, iIdx, j2, reg
24766 52 6f 77 69 64 2c 20 70 52 65 67 4b 65 79 2c 20  Rowid, pRegKey, 
24767 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20 73  P4_INT32);.    s
24768 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72  qlite3HaltConstr
24769 61 69 6e 74 28 0a 20 20 20 20 20 20 20 20 70 50  aint(.        pP
2476a 61 72 73 65 2c 20 4f 45 5f 41 62 6f 72 74 2c 20  arse, OE_Abort, 
2476b 22 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 73  "indexed columns
2476c 20 61 72 65 20 6e 6f 74 20 75 6e 69 71 75 65 22   are not unique"
2476d 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
2476e 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  }.  sqlite3VdbeA
2476f 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49  ddOp2(v, OP_IdxI
24770 6e 73 65 72 74 2c 20 69 49 64 78 2c 20 72 65 67  nsert, iIdx, reg
24771 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69 74  Record);.  sqlit
24772 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
24773 2c 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b  , OPFLAG_USESEEK
24774 52 45 53 55 4c 54 29 3b 0a 20 20 73 71 6c 69 74  RESULT);.  sqlit
24775 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
24776 28 70 50 61 72 73 65 2c 20 72 65 67 52 65 63 6f  (pParse, regReco
24777 72 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  rd);.  sqlite3Vd
24778 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
24779 65 78 74 2c 20 69 54 61 62 2c 20 61 64 64 72 31  ext, iTab, addr1
2477a 2b 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  +1);.  sqlite3Vd
2477b 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
2477c 64 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  dr1);.  sqlite3V
2477d 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2477e 43 6c 6f 73 65 2c 20 69 54 61 62 29 3b 0a 20 20  Close, iTab);.  
2477f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24780 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69  1(v, OP_Close, i
24781 49 64 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  Idx);.}../*.** C
24782 72 65 61 74 65 20 61 20 6e 65 77 20 69 6e 64 65  reate a new inde
24783 78 20 66 6f 72 20 61 6e 20 53 51 4c 20 74 61 62  x for an SQL tab
24784 6c 65 2e 20 20 70 4e 61 6d 65 31 2e 70 4e 61 6d  le.  pName1.pNam
24785 65 32 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  e2 is the name o
24786 66 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20  f the index .** 
24787 61 6e 64 20 70 54 62 6c 4c 69 73 74 20 69 73 20  and pTblList is 
24788 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
24789 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 74 6f  table that is to
2478a 20 62 65 20 69 6e 64 65 78 65 64 2e 20 20 42 6f   be indexed.  Bo
2478b 74 68 20 77 69 6c 6c 20 0a 2a 2a 20 62 65 20 4e  th will .** be N
2478c 55 4c 4c 20 66 6f 72 20 61 20 70 72 69 6d 61 72  ULL for a primar
2478d 79 20 6b 65 79 20 6f 72 20 61 6e 20 69 6e 64 65  y key or an inde
2478e 78 20 74 68 61 74 20 69 73 20 63 72 65 61 74 65  x that is create
2478f 64 20 74 6f 20 73 61 74 69 73 66 79 20 61 0a 2a  d to satisfy a.*
24790 2a 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61  * UNIQUE constra
24791 69 6e 74 2e 20 20 49 66 20 70 54 61 62 6c 65 20  int.  If pTable 
24792 61 6e 64 20 70 49 6e 64 65 78 20 61 72 65 20 4e  and pIndex are N
24793 55 4c 4c 2c 20 75 73 65 20 70 50 61 72 73 65 2d  ULL, use pParse-
24794 3e 70 4e 65 77 54 61 62 6c 65 0a 2a 2a 20 61 73  >pNewTable.** as
24795 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65   the table to be
24796 20 69 6e 64 65 78 65 64 2e 20 20 70 50 61 72 73   indexed.  pPars
24797 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 69 73 20  e->pNewTable is 
24798 61 20 74 61 62 6c 65 20 74 68 61 74 20 69 73 0a  a table that is.
24799 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69  ** currently bei
2479a 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62  ng constructed b
2479b 79 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  y a CREATE TABLE
2479c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a   statement..**.*
2479d 2a 20 70 4c 69 73 74 20 69 73 20 61 20 6c 69 73  * pList is a lis
2479e 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20  t of columns to 
2479f 62 65 20 69 6e 64 65 78 65 64 2e 20 20 70 4c 69  be indexed.  pLi
247a0 73 74 20 77 69 6c 6c 20 62 65 20 4e 55 4c 4c 20  st will be NULL 
247a1 69 66 20 74 68 69 73 0a 2a 2a 20 69 73 20 61 20  if this.** is a 
247a2 70 72 69 6d 61 72 79 20 6b 65 79 20 6f 72 20 75  primary key or u
247a3 6e 69 71 75 65 2d 63 6f 6e 73 74 72 61 69 6e 74  nique-constraint
247a4 20 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63   on the most rec
247a5 65 6e 74 20 63 6f 6c 75 6d 6e 20 61 64 64 65 64  ent column added
247a6 0a 2a 2a 20 74 6f 20 74 68 65 20 74 61 62 6c 65  .** to the table
247a7 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72   currently under
247a8 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20   construction.  
247a9 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e  .**.** If the in
247aa 64 65 78 20 69 73 20 63 72 65 61 74 65 64 20 73  dex is created s
247ab 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 72 65 74  uccessfully, ret
247ac 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
247ad 20 74 68 65 20 6e 65 77 20 49 6e 64 65 78 0a 2a   the new Index.*
247ae 2a 20 73 74 72 75 63 74 75 72 65 2e 20 54 68 69  * structure. Thi
247af 73 20 69 73 20 75 73 65 64 20 62 79 20 73 71 6c  s is used by sql
247b0 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65  ite3AddPrimaryKe
247b1 79 28 29 20 74 6f 20 6d 61 72 6b 20 74 68 65 20  y() to mark the 
247b2 69 6e 64 65 78 0a 2a 2a 20 61 73 20 74 68 65 20  index.** as the 
247b3 74 61 62 6c 65 73 20 70 72 69 6d 61 72 79 20 6b  tables primary k
247b4 65 79 20 28 49 6e 64 65 78 2e 61 75 74 6f 49 6e  ey (Index.autoIn
247b5 64 65 78 3d 3d 32 29 2e 0a 2a 2f 0a 53 51 4c 49  dex==2)..*/.SQLI
247b6 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78  TE_PRIVATE Index
247b7 20 2a 73 71 6c 69 74 65 33 43 72 65 61 74 65 49   *sqlite3CreateI
247b8 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70  ndex(.  Parse *p
247b9 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 41 6c  Parse,     /* Al
247ba 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  l information ab
247bb 6f 75 74 20 74 68 69 73 20 70 61 72 73 65 20 2a  out this parse *
247bc 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
247bd 31 2c 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20  1,     /* First 
247be 70 61 72 74 20 6f 66 20 69 6e 64 65 78 20 6e 61  part of index na
247bf 6d 65 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20  me. May be NULL 
247c0 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  */.  Token *pNam
247c1 65 32 2c 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e  e2,     /* Secon
247c2 64 20 70 61 72 74 20 6f 66 20 69 6e 64 65 78 20  d part of index 
247c3 6e 61 6d 65 2e 20 4d 61 79 20 62 65 20 4e 55 4c  name. May be NUL
247c4 4c 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  L */.  SrcList *
247c5 70 54 62 6c 4e 61 6d 65 2c 20 2f 2a 20 54 61 62  pTblName, /* Tab
247c6 6c 65 20 74 6f 20 69 6e 64 65 78 2e 20 55 73 65  le to index. Use
247c7 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
247c8 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20 45 78 70  le if 0 */.  Exp
247c9 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20  rList *pList,   
247ca 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20 63 6f 6c  /* A list of col
247cb 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 64 65 78  umns to be index
247cc 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72  ed */.  int onEr
247cd 72 6f 72 2c 20 20 20 20 20 20 20 2f 2a 20 4f 45  ror,       /* OE
247ce 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72  _Abort, OE_Ignor
247cf 65 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20 6f  e, OE_Replace, o
247d0 72 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20 54  r OE_None */.  T
247d1 6f 6b 65 6e 20 2a 70 53 74 61 72 74 2c 20 20 20  oken *pStart,   
247d2 20 20 2f 2a 20 54 68 65 20 43 52 45 41 54 45 20    /* The CREATE 
247d3 74 6f 6b 65 6e 20 74 68 61 74 20 62 65 67 69 6e  token that begin
247d4 73 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74  s this statement
247d5 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 45 6e   */.  Token *pEn
247d6 64 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  d,       /* The 
247d7 22 29 22 20 74 68 61 74 20 63 6c 6f 73 65 73 20  ")" that closes 
247d8 74 68 65 20 43 52 45 41 54 45 20 49 4e 44 45 58  the CREATE INDEX
247d9 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
247da 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 2c 20 20  int sortOrder,  
247db 20 20 20 2f 2a 20 53 6f 72 74 20 6f 72 64 65 72     /* Sort order
247dc 20 6f 66 20 70 72 69 6d 61 72 79 20 6b 65 79 20   of primary key 
247dd 77 68 65 6e 20 70 4c 69 73 74 3d 3d 4e 55 4c 4c  when pList==NULL
247de 20 2a 2f 0a 20 20 69 6e 74 20 69 66 4e 6f 74 45   */.  int ifNotE
247df 78 69 73 74 20 20 20 20 20 2f 2a 20 4f 6d 69 74  xist     /* Omit
247e0 20 65 72 72 6f 72 20 69 66 20 69 6e 64 65 78 20   error if index 
247e1 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 2a  already exists *
247e2 2f 0a 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 52  /.){.  Index *pR
247e3 65 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 50  et = 0;     /* P
247e4 6f 69 6e 74 65 72 20 74 6f 20 72 65 74 75 72 6e  ointer to return
247e5 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
247e6 62 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 61  b = 0;     /* Ta
247e7 62 6c 65 20 74 6f 20 62 65 20 69 6e 64 65 78 65  ble to be indexe
247e8 64 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49  d */.  Index *pI
247e9 6e 64 65 78 20 3d 20 30 3b 20 20 20 2f 2a 20 54  ndex = 0;   /* T
247ea 68 65 20 69 6e 64 65 78 20 74 6f 20 62 65 20 63  he index to be c
247eb 72 65 61 74 65 64 20 2a 2f 0a 20 20 63 68 61 72  reated */.  char
247ec 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20   *zName = 0;    
247ed 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
247ee 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e  index */.  int n
247ef 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
247f0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61  /* Number of cha
247f1 72 61 63 74 65 72 73 20 69 6e 20 7a 4e 61 6d 65  racters in zName
247f2 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a   */.  int i, j;.
247f3 20 20 54 6f 6b 65 6e 20 6e 75 6c 6c 49 64 3b 20    Token nullId; 
247f4 20 20 20 20 20 20 20 2f 2a 20 46 61 6b 65 20 74         /* Fake t
247f5 6f 6b 65 6e 20 66 6f 72 20 61 6e 20 65 6d 70 74  oken for an empt
247f6 79 20 49 44 20 6c 69 73 74 20 2a 2f 0a 20 20 44  y ID list */.  D
247f7 62 46 69 78 65 72 20 73 46 69 78 3b 20 20 20 20  bFixer sFix;    
247f8 20 20 20 20 2f 2a 20 46 6f 72 20 61 73 73 69 67      /* For assig
247f9 6e 69 6e 67 20 64 61 74 61 62 61 73 65 20 6e 61  ning database na
247fa 6d 65 73 20 74 6f 20 70 54 61 62 6c 65 20 2a 2f  mes to pTable */
247fb 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72  .  int sortOrder
247fc 4d 61 73 6b 3b 20 20 20 2f 2a 20 31 20 74 6f 20  Mask;   /* 1 to 
247fd 68 6f 6e 6f 72 20 44 45 53 43 20 69 6e 20 69 6e  honor DESC in in
247fe 64 65 78 2e 20 20 30 20 74 6f 20 69 67 6e 6f 72  dex.  0 to ignor
247ff 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  e. */.  sqlite3 
24800 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
24801 3b 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20 20  ;.  Db *pDb;    
24802 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
24803 73 70 65 63 69 66 69 63 20 74 61 62 6c 65 20 63  specific table c
24804 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 69 6e  ontaining the in
24805 64 65 78 65 64 20 64 61 74 61 62 61 73 65 20 2a  dexed database *
24806 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
24807 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
24808 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  x of the databas
24809 65 20 74 68 61 74 20 69 73 20 62 65 69 6e 67 20  e that is being 
2480a 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 54 6f 6b  written */.  Tok
2480b 65 6e 20 2a 70 4e 61 6d 65 20 3d 20 30 3b 20 20  en *pName = 0;  
2480c 20 20 2f 2a 20 55 6e 71 75 61 6c 69 66 69 65 64    /* Unqualified
2480d 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64   name of the ind
2480e 65 78 20 74 6f 20 63 72 65 61 74 65 20 2a 2f 0a  ex to create */.
2480f 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
24810 74 5f 69 74 65 6d 20 2a 70 4c 69 73 74 49 74 65  t_item *pListIte
24811 6d 3b 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e  m; /* For loopin
24812 67 20 6f 76 65 72 20 70 4c 69 73 74 20 2a 2f 0a  g over pList */.
24813 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 69 6e    int nCol;.  in
24814 74 20 6e 45 78 74 72 61 20 3d 20 30 3b 0a 20 20  t nExtra = 0;.  
24815 63 68 61 72 20 2a 7a 45 78 74 72 61 3b 0a 0a 20  char *zExtra;.. 
24816 20 61 73 73 65 72 74 28 20 70 53 74 61 72 74 3d   assert( pStart=
24817 3d 30 20 7c 7c 20 70 45 6e 64 21 3d 30 20 29 3b  =0 || pEnd!=0 );
24818 20 2f 2a 20 70 45 6e 64 20 6d 75 73 74 20 62 65   /* pEnd must be
24819 20 6e 6f 6e 2d 4e 55 4c 4c 20 69 66 20 70 53 74   non-NULL if pSt
2481a 61 72 74 20 69 73 20 2a 2f 0a 20 20 61 73 73 65  art is */.  asse
2481b 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  rt( pParse->nErr
2481c 3d 3d 30 20 29 3b 20 20 20 20 20 20 2f 2a 20 4e  ==0 );      /* N
2481d 65 76 65 72 20 63 61 6c 6c 65 64 20 77 69 74 68  ever called with
2481e 20 70 72 69 6f 72 20 65 72 72 6f 72 73 20 2a 2f   prior errors */
2481f 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
24820 63 46 61 69 6c 65 64 20 7c 7c 20 49 4e 5f 44 45  cFailed || IN_DE
24821 43 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a 20 20  CLARE_VTAB ){.  
24822 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
24823 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20  te_index;.  }.  
24824 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
24825 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
24826 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20  (pParse) ){.    
24827 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
24828 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 0a 20 20 2f  _index;.  }..  /
24829 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20  *.  ** Find the 
2482a 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 74 6f  table that is to
2482b 20 62 65 20 69 6e 64 65 78 65 64 2e 20 20 52 65   be indexed.  Re
2482c 74 75 72 6e 20 65 61 72 6c 79 20 69 66 20 6e 6f  turn early if no
2482d 74 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20  t found..  */.  
2482e 69 66 28 20 70 54 62 6c 4e 61 6d 65 21 3d 30 20  if( pTblName!=0 
2482f 29 7b 0a 0a 20 20 20 20 2f 2a 20 55 73 65 20 74  ){..    /* Use t
24830 68 65 20 74 77 6f 2d 70 61 72 74 20 69 6e 64 65  he two-part inde
24831 78 20 6e 61 6d 65 20 74 6f 20 64 65 74 65 72 6d  x name to determ
24832 69 6e 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ine the database
24833 20 0a 20 20 20 20 2a 2a 20 74 6f 20 73 65 61 72   .    ** to sear
24834 63 68 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  ch for the table
24835 2e 20 27 46 69 78 27 20 74 68 65 20 74 61 62 6c  . 'Fix' the tabl
24836 65 20 6e 61 6d 65 20 74 6f 20 74 68 69 73 20 64  e name to this d
24837 62 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20  b.    ** before 
24838 6c 6f 6f 6b 69 6e 67 20 75 70 20 74 68 65 20 74  looking up the t
24839 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  able..    */.   
2483a 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 31 20   assert( pName1 
2483b 26 26 20 70 4e 61 6d 65 32 20 29 3b 0a 20 20 20  && pName2 );.   
2483c 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77   iDb = sqlite3Tw
2483d 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65  oPartName(pParse
2483e 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32  , pName1, pName2
2483f 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69  , &pName);.    i
24840 66 28 20 69 44 62 3c 30 20 29 20 67 6f 74 6f 20  f( iDb<0 ) goto 
24841 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
24842 78 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  x;..#ifndef SQLI
24843 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20  TE_OMIT_TEMPDB. 
24844 20 20 20 2f 2a 20 49 66 20 74 68 65 20 69 6e 64     /* If the ind
24845 65 78 20 6e 61 6d 65 20 77 61 73 20 75 6e 71 75  ex name was unqu
24846 61 6c 69 66 69 65 64 2c 20 63 68 65 63 6b 20 69  alified, check i
24847 66 20 74 68 65 20 74 68 65 20 74 61 62 6c 65 0a  f the the table.
24848 20 20 20 20 2a 2a 20 69 73 20 61 20 74 65 6d 70      ** is a temp
24849 20 74 61 62 6c 65 2e 20 49 66 20 73 6f 2c 20 73   table. If so, s
2484a 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20  et the database 
2484b 74 6f 20 31 2e 20 44 6f 20 6e 6f 74 20 64 6f 20  to 1. Do not do 
2484c 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 66 20 69  this.    ** if i
2484d 6e 69 74 69 61 6c 69 73 69 6e 67 20 61 20 64 61  nitialising a da
2484e 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 0a 20  tabase schema.. 
2484f 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 64     */.    if( !d
24850 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a  b->init.busy ){.
24851 20 20 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c        pTab = sql
24852 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75  ite3SrcListLooku
24853 70 28 70 50 61 72 73 65 2c 20 70 54 62 6c 4e 61  p(pParse, pTblNa
24854 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  me);.      if( p
24855 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 26 26 20 70  Name2->n==0 && p
24856 54 61 62 20 26 26 20 70 54 61 62 2d 3e 70 53 63  Tab && pTab->pSc
24857 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d  hema==db->aDb[1]
24858 2e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20  .pSchema ){.    
24859 20 20 20 20 69 44 62 20 3d 20 31 3b 0a 20 20 20      iDb = 1;.   
2485a 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69     }.    }.#endi
2485b 66 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  f..    if( sqlit
2485c 65 33 46 69 78 49 6e 69 74 28 26 73 46 69 78 2c  e3FixInit(&sFix,
2485d 20 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22 69   pParse, iDb, "i
2485e 6e 64 65 78 22 2c 20 70 4e 61 6d 65 29 20 26 26  ndex", pName) &&
2485f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
24860 46 69 78 53 72 63 4c 69 73 74 28 26 73 46 69 78  FixSrcList(&sFix
24861 2c 20 70 54 62 6c 4e 61 6d 65 29 0a 20 20 20 20  , pTblName).    
24862 29 7b 0a 20 20 20 20 20 20 2f 2a 20 42 65 63 61  ){.      /* Beca
24863 75 73 65 20 74 68 65 20 70 61 72 73 65 72 20 63  use the parser c
24864 6f 6e 73 74 72 75 63 74 73 20 70 54 62 6c 4e 61  onstructs pTblNa
24865 6d 65 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65  me from a single
24866 20 69 64 65 6e 74 69 66 69 65 72 2c 0a 20 20 20   identifier,.   
24867 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 46 69 78     ** sqlite3Fix
24868 53 72 63 4c 69 73 74 20 63 61 6e 20 6e 65 76 65  SrcList can neve
24869 72 20 66 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 20  r fail. */.     
2486a 20 61 73 73 65 72 74 28 30 29 3b 0a 20 20 20 20   assert(0);.    
2486b 7d 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c  }.    pTab = sql
2486c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
2486d 70 50 61 72 73 65 2c 20 30 2c 20 70 54 62 6c 4e  pParse, 0, pTblN
2486e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c  ame->a[0].zName,
2486f 20 0a 20 20 20 20 20 20 20 20 70 54 62 6c 4e 61   .        pTblNa
24870 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61  me->a[0].zDataba
24871 73 65 29 3b 0a 20 20 20 20 69 66 28 20 21 70 54  se);.    if( !pT
24872 61 62 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  ab || db->malloc
24873 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 65 78  Failed ) goto ex
24874 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
24875 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d  .    assert( db-
24876 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d  >aDb[iDb].pSchem
24877 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  a==pTab->pSchema
24878 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   );.  }else{.   
24879 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 3d 3d   assert( pName==
2487a 30 20 29 3b 0a 20 20 20 20 70 54 61 62 20 3d 20  0 );.    pTab = 
2487b 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
2487c 65 3b 0a 20 20 20 20 69 66 28 20 21 70 54 61 62  e;.    if( !pTab
2487d 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65   ) goto exit_cre
2487e 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 69  ate_index;.    i
2487f 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
24880 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54  maToIndex(db, pT
24881 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
24882 7d 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61  }.  pDb = &db->a
24883 44 62 5b 69 44 62 5d 3b 0a 0a 20 20 61 73 73 65  Db[iDb];..  asse
24884 72 74 28 20 70 54 61 62 21 3d 30 20 29 3b 0a 20  rt( pTab!=0 );. 
24885 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
24886 3e 6e 45 72 72 3d 3d 30 20 29 3b 0a 20 20 69 66  >nErr==0 );.  if
24887 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  ( sqlite3StrNICm
24888 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22  p(pTab->zName, "
24889 73 71 6c 69 74 65 5f 22 2c 20 37 29 3d 3d 30 20  sqlite_", 7)==0 
2488a 0a 20 20 20 20 20 20 20 26 26 20 6d 65 6d 63 6d  .       && memcm
2488b 70 28 26 70 54 61 62 2d 3e 7a 4e 61 6d 65 5b 37  p(&pTab->zName[7
2488c 5d 2c 22 61 6c 74 65 72 74 61 62 5f 22 2c 39 29  ],"altertab_",9)
2488d 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  !=0 ){.    sqlit
2488e 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2488f 65 2c 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79  e, "table %s may
24890 20 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 64 22   not be indexed"
24891 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
24892 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72      goto exit_cr
24893 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a  eate_index;.  }.
24894 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
24895 4d 49 54 5f 56 49 45 57 0a 20 20 69 66 28 20 70  MIT_VIEW.  if( p
24896 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  Tab->pSelect ){.
24897 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
24898 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69 65  Msg(pParse, "vie
24899 77 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e  ws may not be in
2489a 64 65 78 65 64 22 29 3b 0a 20 20 20 20 67 6f 74  dexed");.    got
2489b 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e  o exit_create_in
2489c 64 65 78 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  dex;.  }.#endif.
2489d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2489e 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2489f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  .  if( IsVirtual
248a0 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71  (pTab) ){.    sq
248a1 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
248a2 61 72 73 65 2c 20 22 76 69 72 74 75 61 6c 20 74  arse, "virtual t
248a3 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74 20 62 65  ables may not be
248a4 20 69 6e 64 65 78 65 64 22 29 3b 0a 20 20 20 20   indexed");.    
248a5 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
248a6 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 23 65 6e 64  _index;.  }.#end
248a7 69 66 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69  if..  /*.  ** Fi
248a8 6e 64 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  nd the name of t
248a9 68 65 20 69 6e 64 65 78 2e 20 20 4d 61 6b 65 20  he index.  Make 
248aa 73 75 72 65 20 74 68 65 72 65 20 69 73 20 6e 6f  sure there is no
248ab 74 20 61 6c 72 65 61 64 79 20 61 6e 6f 74 68 65  t already anothe
248ac 72 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6f 72 20  r.  ** index or 
248ad 74 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 73  table with the s
248ae 61 6d 65 20 6e 61 6d 65 2e 20 20 0a 20 20 2a 2a  ame name.  .  **
248af 0a 20 20 2a 2a 20 45 78 63 65 70 74 69 6f 6e 3a  .  ** Exception:
248b0 20 20 49 66 20 77 65 20 61 72 65 20 72 65 61 64    If we are read
248b1 69 6e 67 20 74 68 65 20 6e 61 6d 65 73 20 6f 66  ing the names of
248b2 20 70 65 72 6d 61 6e 65 6e 74 20 69 6e 64 69 63   permanent indic
248b3 65 73 20 66 72 6f 6d 20 74 68 65 0a 20 20 2a 2a  es from the.  **
248b4 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
248b5 61 62 6c 65 20 28 62 65 63 61 75 73 65 20 73 6f  able (because so
248b6 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  me other process
248b7 20 63 68 61 6e 67 65 64 20 74 68 65 20 73 63 68   changed the sch
248b8 65 6d 61 29 20 61 6e 64 0a 20 20 2a 2a 20 6f 6e  ema) and.  ** on
248b9 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6e  e of the index n
248ba 61 6d 65 73 20 63 6f 6c 6c 69 64 65 73 20 77 69  ames collides wi
248bb 74 68 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61  th the name of a
248bc 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
248bd 20 6f 72 0a 20 20 2a 2a 20 69 6e 64 65 78 2c 20   or.  ** index, 
248be 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 63 6f 6e  then we will con
248bf 74 69 6e 75 65 20 74 6f 20 70 72 6f 63 65 73 73  tinue to process
248c0 20 74 68 69 73 20 69 6e 64 65 78 2e 0a 20 20 2a   this index..  *
248c1 2a 0a 20 20 2a 2a 20 49 66 20 70 4e 61 6d 65 3d  *.  ** If pName=
248c2 3d 30 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  =0 it means that
248c3 20 77 65 20 61 72 65 0a 20 20 2a 2a 20 64 65 61   we are.  ** dea
248c4 6c 69 6e 67 20 77 69 74 68 20 61 20 70 72 69 6d  ling with a prim
248c5 61 72 79 20 6b 65 79 20 6f 72 20 55 4e 49 51 55  ary key or UNIQU
248c6 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 20 57  E constraint.  W
248c7 65 20 68 61 76 65 20 74 6f 20 69 6e 76 65 6e 74  e have to invent
248c8 20 6f 75 72 0a 20 20 2a 2a 20 6f 77 6e 20 6e 61   our.  ** own na
248c9 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  me..  */.  if( p
248ca 4e 61 6d 65 20 29 7b 0a 20 20 20 20 7a 4e 61 6d  Name ){.    zNam
248cb 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  e = sqlite3NameF
248cc 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61  romToken(db, pNa
248cd 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 7a 4e 61  me);.    if( zNa
248ce 6d 65 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69  me==0 ) goto exi
248cf 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
248d0 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f      if( SQLITE_O
248d1 4b 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f  K!=sqlite3CheckO
248d2 62 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65  bjectName(pParse
248d3 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20  , zName) ){.    
248d4 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
248d5 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a  te_index;.    }.
248d6 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69      if( !db->ini
248d7 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20  t.busy ){.      
248d8 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54  if( sqlite3FindT
248d9 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  able(db, zName, 
248da 30 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  0)!=0 ){.       
248db 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
248dc 28 70 50 61 72 73 65 2c 20 22 74 68 65 72 65 20  (pParse, "there 
248dd 69 73 20 61 6c 72 65 61 64 79 20 61 20 74 61 62  is already a tab
248de 6c 65 20 6e 61 6d 65 64 20 25 73 22 2c 20 7a 4e  le named %s", zN
248df 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 67 6f  ame);.        go
248e0 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
248e1 6e 64 65 78 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ndex;.      }.  
248e2 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
248e3 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c  te3FindIndex(db,
248e4 20 7a 4e 61 6d 65 2c 20 70 44 62 2d 3e 7a 4e 61   zName, pDb->zNa
248e5 6d 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  me)!=0 ){.      
248e6 69 66 28 20 21 69 66 4e 6f 74 45 78 69 73 74 20  if( !ifNotExist 
248e7 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
248e8 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
248e9 65 2c 20 22 69 6e 64 65 78 20 25 73 20 61 6c 72  e, "index %s alr
248ea 65 61 64 79 20 65 78 69 73 74 73 22 2c 20 7a 4e  eady exists", zN
248eb 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ame);.      }.  
248ec 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72      goto exit_cr
248ed 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20  eate_index;.    
248ee 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  }.  }else{.    i
248ef 6e 74 20 6e 3b 0a 20 20 20 20 49 6e 64 65 78 20  nt n;.    Index 
248f0 2a 70 4c 6f 6f 70 3b 0a 20 20 20 20 66 6f 72 28  *pLoop;.    for(
248f1 70 4c 6f 6f 70 3d 70 54 61 62 2d 3e 70 49 6e 64  pLoop=pTab->pInd
248f2 65 78 2c 20 6e 3d 31 3b 20 70 4c 6f 6f 70 3b 20  ex, n=1; pLoop; 
248f3 70 4c 6f 6f 70 3d 70 4c 6f 6f 70 2d 3e 70 4e 65  pLoop=pLoop->pNe
248f4 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 7a  xt, n++){}.    z
248f5 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50  Name = sqlite3MP
248f6 72 69 6e 74 66 28 64 62 2c 20 22 73 71 6c 69 74  rintf(db, "sqlit
248f7 65 5f 61 75 74 6f 69 6e 64 65 78 5f 25 73 5f 25  e_autoindex_%s_%
248f8 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  d", pTab->zName,
248f9 20 6e 29 3b 0a 20 20 20 20 69 66 28 20 7a 4e 61   n);.    if( zNa
248fa 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67  me==0 ){.      g
248fb 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
248fc 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d  index;.    }.  }
248fd 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72  ..  /* Check for
248fe 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 74   authorization t
248ff 6f 20 63 72 65 61 74 65 20 61 6e 20 69 6e 64 65  o create an inde
24900 78 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  x..  */.#ifndef 
24901 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
24902 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20  ORIZATION.  {.  
24903 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
24904 62 20 3d 20 70 44 62 2d 3e 7a 4e 61 6d 65 3b 0a  b = pDb->zName;.
24905 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41      if( sqlite3A
24906 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
24907 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20   SQLITE_INSERT, 
24908 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62  SCHEMA_TABLE(iDb
24909 29 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20  ), 0, zDb) ){.  
2490a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72      goto exit_cr
2490b 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20  eate_index;.    
2490c 7d 0a 20 20 20 20 69 20 3d 20 53 51 4c 49 54 45  }.    i = SQLITE
2490d 5f 43 52 45 41 54 45 5f 49 4e 44 45 58 3b 0a 20  _CREATE_INDEX;. 
2490e 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d     if( !OMIT_TEM
2490f 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 20  PDB && iDb==1 ) 
24910 69 20 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54  i = SQLITE_CREAT
24911 45 5f 54 45 4d 50 5f 49 4e 44 45 58 3b 0a 20 20  E_TEMP_INDEX;.  
24912 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
24913 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 69  hCheck(pParse, i
24914 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a  , zName, pTab->z
24915 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20  Name, zDb) ){.  
24916 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72      goto exit_cr
24917 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20  eate_index;.    
24918 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  }.  }.#endif..  
24919 2f 2a 20 49 66 20 70 4c 69 73 74 3d 3d 30 2c 20  /* If pList==0, 
2491a 69 74 20 6d 65 61 6e 73 20 74 68 69 73 20 72 6f  it means this ro
2491b 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64  utine was called
2491c 20 74 6f 20 6d 61 6b 65 20 61 20 70 72 69 6d 61   to make a prima
2491d 72 79 0a 20 20 2a 2a 20 6b 65 79 20 6f 75 74 20  ry.  ** key out 
2491e 6f 66 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75  of the last colu
2491f 6d 6e 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  mn added to the 
24920 74 61 62 6c 65 20 75 6e 64 65 72 20 63 6f 6e 73  table under cons
24921 74 72 75 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 53  truction..  ** S
24922 6f 20 63 72 65 61 74 65 20 61 20 66 61 6b 65 20  o create a fake 
24923 6c 69 73 74 20 74 6f 20 73 69 6d 75 6c 61 74 65  list to simulate
24924 20 74 68 69 73 2e 0a 20 20 2a 2f 0a 20 20 69 66   this..  */.  if
24925 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20  ( pList==0 ){.  
24926 20 20 6e 75 6c 6c 49 64 2e 7a 20 3d 20 70 54 61    nullId.z = pTa
24927 62 2d 3e 61 43 6f 6c 5b 70 54 61 62 2d 3e 6e 43  b->aCol[pTab->nC
24928 6f 6c 2d 31 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  ol-1].zName;.   
24929 20 6e 75 6c 6c 49 64 2e 6e 20 3d 20 73 71 6c 69   nullId.n = sqli
2492a 74 65 33 53 74 72 6c 65 6e 33 30 28 28 63 68 61  te3Strlen30((cha
2492b 72 2a 29 6e 75 6c 6c 49 64 2e 7a 29 3b 0a 20 20  r*)nullId.z);.  
2492c 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65    pList = sqlite
2492d 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
2492e 70 50 61 72 73 65 2c 20 30 2c 20 30 29 3b 0a 20  pParse, 0, 0);. 
2492f 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20     if( pList==0 
24930 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61  ) goto exit_crea
24931 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 73 71  te_index;.    sq
24932 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74  lite3ExprListSet
24933 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4c 69  Name(pParse, pLi
24934 73 74 2c 20 26 6e 75 6c 6c 49 64 2c 20 30 29 3b  st, &nullId, 0);
24935 0a 20 20 20 20 70 4c 69 73 74 2d 3e 61 5b 30 5d  .    pList->a[0]
24936 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38  .sortOrder = (u8
24937 29 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 7d 0a  )sortOrder;.  }.
24938 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74  .  /* Figure out
24939 20 68 6f 77 20 6d 61 6e 79 20 62 79 74 65 73 20   how many bytes 
2493a 6f 66 20 73 70 61 63 65 20 61 72 65 20 72 65 71  of space are req
2493b 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 65  uired to store e
2493c 78 70 6c 69 63 69 74 6c 79 0a 20 20 2a 2a 20 73  xplicitly.  ** s
2493d 70 65 63 69 66 69 65 64 20 63 6f 6c 6c 61 74 69  pecified collati
2493e 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65  on sequence name
2493f 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  s..  */.  for(i=
24940 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70  0; i<pList->nExp
24941 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70  r; i++){.    Exp
24942 72 20 2a 70 45 78 70 72 20 3d 20 70 4c 69 73 74  r *pExpr = pList
24943 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[i].pExpr;.  
24944 20 20 69 66 28 20 70 45 78 70 72 20 29 7b 0a 20    if( pExpr ){. 
24945 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43       CollSeq *pC
24946 6f 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f  oll = pExpr->pCo
24947 6c 6c 3b 0a 20 20 20 20 20 20 2f 2a 20 45 69 74  ll;.      /* Eit
24948 68 65 72 20 70 43 6f 6c 6c 21 3d 30 20 6f 72 20  her pColl!=0 or 
24949 74 68 65 72 65 20 77 61 73 20 61 6e 20 4f 4f 4d  there was an OOM
2494a 20 66 61 69 6c 75 72 65 2e 20 20 42 75 74 20 69   failure.  But i
2494b 66 20 61 6e 20 4f 4f 4d 0a 20 20 20 20 20 20 2a  f an OOM.      *
2494c 2a 20 66 61 69 6c 75 72 65 20 77 65 20 68 61 76  * failure we hav
2494d 65 20 71 75 69 74 20 62 65 66 6f 72 65 20 72 65  e quit before re
2494e 61 63 68 69 6e 67 20 74 68 69 73 20 70 6f 69 6e  aching this poin
2494f 74 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  t. */.      if( 
24950 41 4c 57 41 59 53 28 70 43 6f 6c 6c 29 20 29 7b  ALWAYS(pColl) ){
24951 0a 20 20 20 20 20 20 20 20 6e 45 78 74 72 61 20  .        nExtra 
24952 2b 3d 20 28 31 20 2b 20 73 71 6c 69 74 65 33 53  += (1 + sqlite3S
24953 74 72 6c 65 6e 33 30 28 70 43 6f 6c 6c 2d 3e 7a  trlen30(pColl->z
24954 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20 7d 0a  Name));.      }.
24955 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
24956 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 74  .  ** Allocate t
24957 68 65 20 69 6e 64 65 78 20 73 74 72 75 63 74 75  he index structu
24958 72 65 2e 20 0a 20 20 2a 2f 0a 20 20 6e 4e 61 6d  re. .  */.  nNam
24959 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  e = sqlite3Strle
2495a 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 6e 43  n30(zName);.  nC
2495b 6f 6c 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70  ol = pList->nExp
2495c 72 3b 0a 20 20 70 49 6e 64 65 78 20 3d 20 73 71  r;.  pIndex = sq
2495d 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
2495e 6f 28 64 62 2c 20 0a 20 20 20 20 20 20 73 69 7a  o(db, .      siz
2495f 65 6f 66 28 49 6e 64 65 78 29 20 2b 20 20 20 20  eof(Index) +    
24960 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
24961 65 78 20 73 74 72 75 63 74 75 72 65 20 20 2a 2f  ex structure  */
24962 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 69 6e  .      sizeof(in
24963 74 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20  t)*nCol +       
24964 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 69 43      /* Index.aiC
24965 6f 6c 75 6d 6e 20 20 20 2a 2f 0a 20 20 20 20 20  olumn   */.     
24966 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e 43   sizeof(int)*(nC
24967 6f 6c 2b 31 29 20 2b 20 20 20 20 20 20 20 2f 2a  ol+1) +       /*
24968 20 49 6e 64 65 78 2e 61 69 52 6f 77 45 73 74 20   Index.aiRowEst 
24969 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 6f    */.      sizeo
2496a 66 28 63 68 61 72 20 2a 29 2a 6e 43 6f 6c 20 2b  f(char *)*nCol +
2496b 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2496c 2e 61 7a 43 6f 6c 6c 20 20 20 20 20 2a 2f 0a 20  .azColl     */. 
2496d 20 20 20 20 20 73 69 7a 65 6f 66 28 75 38 29 2a       sizeof(u8)*
2496e 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20 20 20 20  nCol +          
2496f 20 20 2f 2a 20 49 6e 64 65 78 2e 61 53 6f 72 74    /* Index.aSort
24970 4f 72 64 65 72 20 2a 2f 0a 20 20 20 20 20 20 6e  Order */.      n
24971 4e 61 6d 65 20 2b 20 31 20 2b 20 20 20 20 20 20  Name + 1 +      
24972 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
24973 6e 64 65 78 2e 7a 4e 61 6d 65 20 20 20 20 20 20  ndex.zName      
24974 2a 2f 0a 20 20 20 20 20 20 6e 45 78 74 72 61 20  */.      nExtra 
24975 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24976 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69        /* Collati
24977 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65  on sequence name
24978 73 20 2a 2f 0a 20 20 29 3b 0a 20 20 69 66 28 20  s */.  );.  if( 
24979 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2497a 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69   ){.    goto exi
2497b 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
2497c 20 20 7d 0a 20 20 70 49 6e 64 65 78 2d 3e 61 7a    }.  pIndex->az
2497d 43 6f 6c 6c 20 3d 20 28 63 68 61 72 2a 2a 29 28  Coll = (char**)(
2497e 26 70 49 6e 64 65 78 5b 31 5d 29 3b 0a 20 20 70  &pIndex[1]);.  p
2497f 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 20  Index->aiColumn 
24980 3d 20 28 69 6e 74 20 2a 29 28 26 70 49 6e 64 65  = (int *)(&pInde
24981 78 2d 3e 61 7a 43 6f 6c 6c 5b 6e 43 6f 6c 5d 29  x->azColl[nCol])
24982 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 61 69 52 6f  ;.  pIndex->aiRo
24983 77 45 73 74 20 3d 20 28 75 6e 73 69 67 6e 65 64  wEst = (unsigned
24984 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 69 43   *)(&pIndex->aiC
24985 6f 6c 75 6d 6e 5b 6e 43 6f 6c 5d 29 3b 0a 20 20  olumn[nCol]);.  
24986 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64  pIndex->aSortOrd
24987 65 72 20 3d 20 28 75 38 20 2a 29 28 26 70 49 6e  er = (u8 *)(&pIn
24988 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 5b 6e 43  dex->aiRowEst[nC
24989 6f 6c 2b 31 5d 29 3b 0a 20 20 70 49 6e 64 65 78  ol+1]);.  pIndex
2498a 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 20  ->zName = (char 
2498b 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 53 6f 72  *)(&pIndex->aSor
2498c 74 4f 72 64 65 72 5b 6e 43 6f 6c 5d 29 3b 0a 20  tOrder[nCol]);. 
2498d 20 7a 45 78 74 72 61 20 3d 20 28 63 68 61 72 20   zExtra = (char 
2498e 2a 29 28 26 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d  *)(&pIndex->zNam
2498f 65 5b 6e 4e 61 6d 65 2b 31 5d 29 3b 0a 20 20 6d  e[nName+1]);.  m
24990 65 6d 63 70 79 28 70 49 6e 64 65 78 2d 3e 7a 4e  emcpy(pIndex->zN
24991 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  ame, zName, nNam
24992 65 2b 31 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e  e+1);.  pIndex->
24993 70 54 61 62 6c 65 20 3d 20 70 54 61 62 3b 0a 20  pTable = pTab;. 
24994 20 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e   pIndex->nColumn
24995 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   = pList->nExpr;
24996 0a 20 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72  .  pIndex->onErr
24997 6f 72 20 3d 20 28 75 38 29 6f 6e 45 72 72 6f 72  or = (u8)onError
24998 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 61 75 74 6f  ;.  pIndex->auto
24999 49 6e 64 65 78 20 3d 20 28 75 38 29 28 70 4e 61  Index = (u8)(pNa
2499a 6d 65 3d 3d 30 29 3b 0a 20 20 70 49 6e 64 65 78  me==0);.  pIndex
2499b 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e  ->pSchema = db->
2499c 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61  aDb[iDb].pSchema
2499d 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f  ;..  /* Check to
2499e 20 73 65 65 20 69 66 20 77 65 20 73 68 6f 75 6c   see if we shoul
2499f 64 20 68 6f 6e 6f 72 20 44 45 53 43 20 72 65 71  d honor DESC req
249a0 75 65 73 74 73 20 6f 6e 20 69 6e 64 65 78 20 63  uests on index c
249a1 6f 6c 75 6d 6e 73 0a 20 20 2a 2f 0a 20 20 69 66  olumns.  */.  if
249a2 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  ( pDb->pSchema->
249a3 66 69 6c 65 5f 66 6f 72 6d 61 74 3e 3d 34 20 29  file_format>=4 )
249a4 7b 0a 20 20 20 20 73 6f 72 74 4f 72 64 65 72 4d  {.    sortOrderM
249a5 61 73 6b 20 3d 20 2d 31 3b 20 20 20 2f 2a 20 48  ask = -1;   /* H
249a6 6f 6e 6f 72 20 44 45 53 43 20 2a 2f 0a 20 20 7d  onor DESC */.  }
249a7 65 6c 73 65 7b 0a 20 20 20 20 73 6f 72 74 4f 72  else{.    sortOr
249a8 64 65 72 4d 61 73 6b 20 3d 20 30 3b 20 20 20 20  derMask = 0;    
249a9 2f 2a 20 49 67 6e 6f 72 65 20 44 45 53 43 20 2a  /* Ignore DESC *
249aa 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 63 61 6e  /.  }..  /* Scan
249ab 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68   the names of th
249ac 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65  e columns of the
249ad 20 74 61 62 6c 65 20 74 6f 20 62 65 20 69 6e 64   table to be ind
249ae 65 78 65 64 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f  exed and.  ** lo
249af 61 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e  ad the column in
249b0 64 69 63 65 73 20 69 6e 74 6f 20 74 68 65 20 49  dices into the I
249b1 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 2e 20  ndex structure. 
249b2 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f 72   Report an error
249b3 0a 20 20 2a 2a 20 69 66 20 61 6e 79 20 63 6f 6c  .  ** if any col
249b4 75 6d 6e 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64  umn is not found
249b5 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f  ..  **.  ** TODO
249b6 3a 20 20 41 64 64 20 61 20 74 65 73 74 20 74 6f  :  Add a test to
249b7 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
249b8 74 68 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 20  the same column 
249b9 69 73 20 6e 6f 74 20 6e 61 6d 65 64 0a 20 20 2a  is not named.  *
249ba 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  * more than once
249bb 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65   within the same
249bc 20 69 6e 64 65 78 2e 20 20 4f 6e 6c 79 20 74 68   index.  Only th
249bd 65 20 66 69 72 73 74 20 69 6e 73 74 61 6e 63 65  e first instance
249be 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6c   of.  ** the col
249bf 75 6d 6e 20 77 69 6c 6c 20 65 76 65 72 20 62 65  umn will ever be
249c0 20 75 73 65 64 20 62 79 20 74 68 65 20 6f 70 74   used by the opt
249c1 69 6d 69 7a 65 72 2e 20 20 4e 6f 74 65 20 74 68  imizer.  Note th
249c2 61 74 20 75 73 69 6e 67 20 74 68 65 0a 20 20 2a  at using the.  *
249c3 2a 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 20 6d 6f  * same column mo
249c4 72 65 20 74 68 61 6e 20 6f 6e 63 65 20 63 61 6e  re than once can
249c5 6e 6f 74 20 62 65 20 61 6e 20 65 72 72 6f 72 20  not be an error 
249c6 62 65 63 61 75 73 65 20 74 68 61 74 20 77 6f 75  because that wou
249c7 6c 64 20 0a 20 20 2a 2a 20 62 72 65 61 6b 20 62  ld .  ** break b
249c8 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69  ackwards compati
249c9 62 69 6c 69 74 79 20 2d 20 69 74 20 6e 65 65 64  bility - it need
249ca 73 20 74 6f 20 62 65 20 61 20 77 61 72 6e 69 6e  s to be a warnin
249cb 67 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  g..  */.  for(i=
249cc 30 2c 20 70 4c 69 73 74 49 74 65 6d 3d 70 4c 69  0, pListItem=pLi
249cd 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e  st->a; i<pList->
249ce 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 4c 69 73  nExpr; i++, pLis
249cf 74 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 63 6f  tItem++){.    co
249d0 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 4e 61  nst char *zColNa
249d1 6d 65 20 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e  me = pListItem->
249d2 7a 4e 61 6d 65 3b 0a 20 20 20 20 43 6f 6c 75 6d  zName;.    Colum
249d3 6e 20 2a 70 54 61 62 43 6f 6c 3b 0a 20 20 20 20  n *pTabCol;.    
249d4 69 6e 74 20 72 65 71 75 65 73 74 65 64 53 6f 72  int requestedSor
249d5 74 4f 72 64 65 72 3b 0a 20 20 20 20 63 68 61 72  tOrder;.    char
249d6 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20   *zColl;        
249d7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
249d8 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
249d9 20 6e 61 6d 65 20 2a 2f 0a 0a 20 20 20 20 66 6f   name */..    fo
249da 72 28 6a 3d 30 2c 20 70 54 61 62 43 6f 6c 3d 70  r(j=0, pTabCol=p
249db 54 61 62 2d 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61  Tab->aCol; j<pTa
249dc 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 54  b->nCol; j++, pT
249dd 61 62 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20  abCol++){.      
249de 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
249df 6d 70 28 7a 43 6f 6c 4e 61 6d 65 2c 20 70 54 61  mp(zColName, pTa
249e0 62 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20  bCol->zName)==0 
249e1 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20  ) break;.    }. 
249e2 20 20 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e     if( j>=pTab->
249e3 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 73 71  nCol ){.      sq
249e4 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
249e5 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20  arse, "table %s 
249e6 68 61 73 20 6e 6f 20 63 6f 6c 75 6d 6e 20 6e 61  has no column na
249e7 6d 65 64 20 25 73 22 2c 0a 20 20 20 20 20 20 20  med %s",.       
249e8 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43   pTab->zName, zC
249e9 6f 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 67  olName);.      g
249ea 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
249eb 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20  index;.    }.   
249ec 20 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d   pIndex->aiColum
249ed 6e 5b 69 5d 20 3d 20 6a 3b 0a 20 20 20 20 2f 2a  n[i] = j;.    /*
249ee 20 4a 75 73 74 69 66 69 63 61 74 69 6f 6e 20 6f   Justification o
249ef 66 20 74 68 65 20 41 4c 57 41 59 53 28 70 4c 69  f the ALWAYS(pLi
249f0 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70  stItem->pExpr->p
249f1 43 6f 6c 6c 29 3a 20 20 42 65 63 61 75 73 65 20  Coll):  Because 
249f2 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 77 61  of.    ** the wa
249f3 79 20 74 68 65 20 22 69 64 78 6c 69 73 74 22 20  y the "idxlist" 
249f4 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 20 69 73 20  non-terminal is 
249f5 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 74  constructed by t
249f6 68 65 20 70 61 72 73 65 72 2c 0a 20 20 20 20 2a  he parser,.    *
249f7 2a 20 69 66 20 70 4c 69 73 74 49 74 65 6d 2d 3e  * if pListItem->
249f8 70 45 78 70 72 20 69 73 20 6e 6f 74 20 6e 75 6c  pExpr is not nul
249f9 6c 20 74 68 65 6e 20 65 69 74 68 65 72 20 70 4c  l then either pL
249fa 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e  istItem->pExpr->
249fb 70 43 6f 6c 6c 0a 20 20 20 20 2a 2a 20 6d 75 73  pColl.    ** mus
249fc 74 20 65 78 69 73 74 20 6f 72 20 65 6c 73 65 20  t exist or else 
249fd 74 68 65 72 65 20 6d 75 73 74 20 68 61 76 65 20  there must have 
249fe 62 65 65 6e 20 61 6e 20 4f 4f 4d 20 65 72 72 6f  been an OOM erro
249ff 72 2e 20 20 42 75 74 20 69 66 20 74 68 65 72 65  r.  But if there
24a00 0a 20 20 20 20 2a 2a 20 77 61 73 20 61 6e 20 4f  .    ** was an O
24a01 4f 4d 20 65 72 72 6f 72 2c 20 77 65 20 77 6f 75  OM error, we wou
24a02 6c 64 20 6e 65 76 65 72 20 72 65 61 63 68 20 74  ld never reach t
24a03 68 69 73 20 70 6f 69 6e 74 2e 20 2a 2f 0a 20 20  his point. */.  
24a04 20 20 69 66 28 20 70 4c 69 73 74 49 74 65 6d 2d    if( pListItem-
24a05 3e 70 45 78 70 72 20 26 26 20 41 4c 57 41 59 53  >pExpr && ALWAYS
24a06 28 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70  (pListItem->pExp
24a07 72 2d 3e 70 43 6f 6c 6c 29 20 29 7b 0a 20 20 20  r->pColl) ){.   
24a08 20 20 20 69 6e 74 20 6e 43 6f 6c 6c 3b 0a 20 20     int nColl;.  
24a09 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 4c 69 73      zColl = pLis
24a0a 74 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43  tItem->pExpr->pC
24a0b 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20  oll->zName;.    
24a0c 20 20 6e 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    nColl = sqlite
24a0d 33 53 74 72 6c 65 6e 33 30 28 7a 43 6f 6c 6c 29  3Strlen30(zColl)
24a0e 20 2b 20 31 3b 0a 20 20 20 20 20 20 61 73 73 65   + 1;.      asse
24a0f 72 74 28 20 6e 45 78 74 72 61 3e 3d 6e 43 6f 6c  rt( nExtra>=nCol
24a10 6c 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70  l );.      memcp
24a11 79 28 7a 45 78 74 72 61 2c 20 7a 43 6f 6c 6c 2c  y(zExtra, zColl,
24a12 20 6e 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 7a   nColl);.      z
24a13 43 6f 6c 6c 20 3d 20 7a 45 78 74 72 61 3b 0a 20  Coll = zExtra;. 
24a14 20 20 20 20 20 7a 45 78 74 72 61 20 2b 3d 20 6e       zExtra += n
24a15 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 6e 45 78 74  Coll;.      nExt
24a16 72 61 20 2d 3d 20 6e 43 6f 6c 6c 3b 0a 20 20 20  ra -= nColl;.   
24a17 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 43   }else{.      zC
24a18 6f 6c 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c  oll = pTab->aCol
24a19 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20  [j].zColl;.     
24a1a 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29 7b 0a 20   if( !zColl ){. 
24a1b 20 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 64         zColl = d
24a1c 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 2d 3e 7a 4e  b->pDfltColl->zN
24a1d 61 6d 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ame;.      }.   
24a1e 20 7d 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e   }.    if( !db->
24a1f 69 6e 69 74 2e 62 75 73 79 20 26 26 20 21 73 71  init.busy && !sq
24a20 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53  lite3LocateCollS
24a21 65 71 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c  eq(pParse, zColl
24a22 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
24a23 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
24a24 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 6e  x;.    }.    pIn
24a25 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 20 3d  dex->azColl[i] =
24a26 20 7a 43 6f 6c 6c 3b 0a 20 20 20 20 72 65 71 75   zColl;.    requ
24a27 65 73 74 65 64 53 6f 72 74 4f 72 64 65 72 20 3d  estedSortOrder =
24a28 20 70 4c 69 73 74 49 74 65 6d 2d 3e 73 6f 72 74   pListItem->sort
24a29 4f 72 64 65 72 20 26 20 73 6f 72 74 4f 72 64 65  Order & sortOrde
24a2a 72 4d 61 73 6b 3b 0a 20 20 20 20 70 49 6e 64 65  rMask;.    pInde
24a2b 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  x->aSortOrder[i]
24a2c 20 3d 20 28 75 38 29 72 65 71 75 65 73 74 65 64   = (u8)requested
24a2d 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 7d 0a 20  SortOrder;.  }. 
24a2e 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52   sqlite3DefaultR
24a2f 6f 77 45 73 74 28 70 49 6e 64 65 78 29 3b 0a 0a  owEst(pIndex);..
24a30 20 20 69 66 28 20 70 54 61 62 3d 3d 70 50 61 72    if( pTab==pPar
24a31 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 29 7b  se->pNewTable ){
24a32 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 72 6f 75  .    /* This rou
24a33 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20 63 61  tine has been ca
24a34 6c 6c 65 64 20 74 6f 20 63 72 65 61 74 65 20 61  lled to create a
24a35 6e 20 61 75 74 6f 6d 61 74 69 63 20 69 6e 64 65  n automatic inde
24a36 78 20 61 73 20 61 0a 20 20 20 20 2a 2a 20 72 65  x as a.    ** re
24a37 73 75 6c 74 20 6f 66 20 61 20 50 52 49 4d 41 52  sult of a PRIMAR
24a38 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45 20  Y KEY or UNIQUE 
24a39 63 6c 61 75 73 65 20 6f 6e 20 61 20 63 6f 6c 75  clause on a colu
24a3a 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2c 20 6f  mn definition, o
24a3b 72 0a 20 20 20 20 2a 2a 20 61 20 50 52 49 4d 41  r.    ** a PRIMA
24a3c 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55 45  RY KEY or UNIQUE
24a3d 20 63 6c 61 75 73 65 20 66 6f 6c 6c 6f 77 69 6e   clause followin
24a3e 67 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 66  g the column def
24a3f 69 6e 69 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2a  initions..    **
24a40 20 69 2e 65 2e 20 6f 6e 65 20 6f 66 3a 0a 20 20   i.e. one of:.  
24a41 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 52 45 41    **.    ** CREA
24a42 54 45 20 54 41 42 4c 45 20 74 28 78 20 50 52 49  TE TABLE t(x PRI
24a43 4d 41 52 59 20 4b 45 59 2c 20 79 29 3b 0a 20 20  MARY KEY, y);.  
24a44 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c    ** CREATE TABL
24a45 45 20 74 28 78 2c 20 79 2c 20 55 4e 49 51 55 45  E t(x, y, UNIQUE
24a46 28 78 2c 20 79 29 29 3b 0a 20 20 20 20 2a 2a 0a  (x, y));.    **.
24a47 20 20 20 20 2a 2a 20 45 69 74 68 65 72 20 77 61      ** Either wa
24a48 79 2c 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20  y, check to see 
24a49 69 66 20 74 68 65 20 74 61 62 6c 65 20 61 6c 72  if the table alr
24a4a 65 61 64 79 20 68 61 73 20 73 75 63 68 20 61 6e  eady has such an
24a4b 20 69 6e 64 65 78 2e 20 49 66 0a 20 20 20 20 2a   index. If.    *
24a4c 2a 20 73 6f 2c 20 64 6f 6e 27 74 20 62 6f 74 68  * so, don't both
24a4d 65 72 20 63 72 65 61 74 69 6e 67 20 74 68 69 73  er creating this
24a4e 20 6f 6e 65 2e 20 54 68 69 73 20 6f 6e 6c 79 20   one. This only 
24a4f 61 70 70 6c 69 65 73 20 74 6f 0a 20 20 20 20 2a  applies to.    *
24a50 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  * automatically 
24a51 63 72 65 61 74 65 64 20 69 6e 64 69 63 65 73 2e  created indices.
24a52 20 55 73 65 72 73 20 63 61 6e 20 64 6f 20 61 73   Users can do as
24a53 20 74 68 65 79 20 77 69 73 68 20 77 69 74 68 0a   they wish with.
24a54 20 20 20 20 2a 2a 20 65 78 70 6c 69 63 69 74 20      ** explicit 
24a55 69 6e 64 69 63 65 73 2e 0a 20 20 20 20 2a 2a 0a  indices..    **.
24a56 20 20 20 20 2a 2a 20 54 77 6f 20 55 4e 49 51 55      ** Two UNIQU
24a57 45 20 6f 72 20 50 52 49 4d 41 52 59 20 4b 45 59  E or PRIMARY KEY
24a58 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65   constraints are
24a59 20 63 6f 6e 73 69 64 65 72 65 64 20 65 71 75 69   considered equi
24a5a 76 61 6c 65 6e 74 0a 20 20 20 20 2a 2a 20 28 61  valent.    ** (a
24a5b 6e 64 20 74 68 75 73 20 73 75 70 70 72 65 73 73  nd thus suppress
24a5c 69 6e 67 20 74 68 65 20 73 65 63 6f 6e 64 20 6f  ing the second o
24a5d 6e 65 29 20 65 76 65 6e 20 69 66 20 74 68 65 79  ne) even if they
24a5e 20 68 61 76 65 20 64 69 66 66 65 72 65 6e 74 0a   have different.
24a5f 20 20 20 20 2a 2a 20 73 6f 72 74 20 6f 72 64 65      ** sort orde
24a60 72 73 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  rs..    **.    *
24a61 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 64  * If there are d
24a62 69 66 66 65 72 65 6e 74 20 63 6f 6c 6c 61 74 69  ifferent collati
24a63 6e 67 20 73 65 71 75 65 6e 63 65 73 20 6f 72 20  ng sequences or 
24a64 69 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 6f  if the columns o
24a65 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 6f 6e  f.    ** the con
24a66 73 74 72 61 69 6e 74 20 6f 63 63 75 72 20 69 6e  straint occur in
24a67 20 64 69 66 66 65 72 65 6e 74 20 6f 72 64 65 72   different order
24a68 73 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 73  s, then the cons
24a69 74 72 61 69 6e 74 73 20 61 72 65 0a 20 20 20 20  traints are.    
24a6a 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64 20 64 69  ** considered di
24a6b 73 74 69 6e 63 74 20 61 6e 64 20 62 6f 74 68 20  stinct and both 
24a6c 72 65 73 75 6c 74 20 69 6e 20 73 65 70 61 72 61  result in separa
24a6d 74 65 20 69 6e 64 69 63 65 73 2e 0a 20 20 20 20  te indices..    
24a6e 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  */.    Index *pI
24a6f 64 78 3b 0a 20 20 20 20 66 6f 72 28 70 49 64 78  dx;.    for(pIdx
24a70 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
24a71 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
24a72 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 6e  pNext){.      in
24a73 74 20 6b 3b 0a 20 20 20 20 20 20 61 73 73 65 72  t k;.      asser
24a74 74 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72  t( pIdx->onError
24a75 21 3d 4f 45 5f 4e 6f 6e 65 20 29 3b 0a 20 20 20  !=OE_None );.   
24a76 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d     assert( pIdx-
24a77 3e 61 75 74 6f 49 6e 64 65 78 20 29 3b 0a 20 20  >autoIndex );.  
24a78 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 64      assert( pInd
24a79 65 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f  ex->onError!=OE_
24a7a 4e 6f 6e 65 20 29 3b 0a 0a 20 20 20 20 20 20 69  None );..      i
24a7b 66 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  f( pIdx->nColumn
24a7c 21 3d 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d  !=pIndex->nColum
24a7d 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  n ) continue;.  
24a7e 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70      for(k=0; k<p
24a7f 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b  Idx->nColumn; k+
24a80 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73  +){.        cons
24a81 74 20 63 68 61 72 20 2a 7a 31 3b 0a 20 20 20 20  t char *z1;.    
24a82 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
24a83 7a 32 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  z2;.        if( 
24a84 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6b  pIdx->aiColumn[k
24a85 5d 21 3d 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c  ]!=pIndex->aiCol
24a86 75 6d 6e 5b 6b 5d 20 29 20 62 72 65 61 6b 3b 0a  umn[k] ) break;.
24a87 20 20 20 20 20 20 20 20 7a 31 20 3d 20 70 49 64          z1 = pId
24a88 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20  x->azColl[k];.  
24a89 20 20 20 20 20 20 7a 32 20 3d 20 70 49 6e 64 65        z2 = pInde
24a8a 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20  x->azColl[k];.  
24a8b 20 20 20 20 20 20 69 66 28 20 7a 31 21 3d 7a 32        if( z1!=z2
24a8c 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
24a8d 6d 70 28 7a 31 2c 20 7a 32 29 20 29 20 62 72 65  mp(z1, z2) ) bre
24a8e 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
24a8f 20 20 69 66 28 20 6b 3d 3d 70 49 64 78 2d 3e 6e    if( k==pIdx->n
24a90 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20  Column ){.      
24a91 20 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72    if( pIdx->onEr
24a92 72 6f 72 21 3d 70 49 6e 64 65 78 2d 3e 6f 6e 45  ror!=pIndex->onE
24a93 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20  rror ){.        
24a94 20 20 2f 2a 20 54 68 69 73 20 63 6f 6e 73 74 72    /* This constr
24a95 61 69 6e 74 20 63 72 65 61 74 65 73 20 74 68 65  aint creates the
24a96 20 73 61 6d 65 20 69 6e 64 65 78 20 61 73 20 61   same index as a
24a97 20 70 72 65 76 69 6f 75 73 0a 20 20 20 20 20 20   previous.      
24a98 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e      ** constrain
24a99 74 20 73 70 65 63 69 66 69 65 64 20 73 6f 6d 65  t specified some
24a9a 77 68 65 72 65 20 69 6e 20 74 68 65 20 43 52 45  where in the CRE
24a9b 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
24a9c 65 6e 74 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  ent..          *
24a9d 2a 20 48 6f 77 65 76 65 72 20 74 68 65 20 4f 4e  * However the ON
24a9e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73 65   CONFLICT clause
24a9f 73 20 61 72 65 20 64 69 66 66 65 72 65 6e 74 2e  s are different.
24aa0 20 49 66 20 62 6f 74 68 20 74 68 69 73 20 0a 20   If both this . 
24aa1 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73           ** cons
24aa2 74 72 61 69 6e 74 20 61 6e 64 20 74 68 65 20 70  traint and the p
24aa3 72 65 76 69 6f 75 73 20 65 71 75 69 76 61 6c 65  revious equivale
24aa4 6e 74 20 63 6f 6e 73 74 72 61 69 6e 74 20 68 61  nt constraint ha
24aa5 76 65 20 65 78 70 6c 69 63 69 74 0a 20 20 20 20  ve explicit.    
24aa6 20 20 20 20 20 20 2a 2a 20 4f 4e 20 43 4f 4e 46        ** ON CONF
24aa7 4c 49 43 54 20 63 6c 61 75 73 65 73 20 74 68 69  LICT clauses thi
24aa8 73 20 69 73 20 61 6e 20 65 72 72 6f 72 2e 20 4f  s is an error. O
24aa9 74 68 65 72 77 69 73 65 2c 20 75 73 65 20 74 68  therwise, use th
24aaa 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 65  e.          ** e
24aab 78 70 6c 69 63 69 74 6c 79 20 73 70 65 63 69 66  xplicitly specif
24aac 69 65 64 20 62 65 68 61 76 69 6f 75 72 20 66 6f  ied behaviour fo
24aad 72 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 20  r the index..   
24aae 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
24aaf 20 20 20 20 69 66 28 20 21 28 70 49 64 78 2d 3e      if( !(pIdx->
24ab0 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61  onError==OE_Defa
24ab1 75 6c 74 20 7c 7c 20 70 49 6e 64 65 78 2d 3e 6f  ult || pIndex->o
24ab2 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75  nError==OE_Defau
24ab3 6c 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  lt) ){.         
24ab4 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
24ab5 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  sg(pParse, .    
24ab6 20 20 20 20 20 20 20 20 20 20 20 20 22 63 6f 6e              "con
24ab7 66 6c 69 63 74 69 6e 67 20 4f 4e 20 43 4f 4e 46  flicting ON CONF
24ab8 4c 49 43 54 20 63 6c 61 75 73 65 73 20 73 70 65  LICT clauses spe
24ab9 63 69 66 69 65 64 22 2c 20 30 29 3b 0a 20 20 20  cified", 0);.   
24aba 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
24abb 20 20 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e 45     if( pIdx->onE
24abc 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74  rror==OE_Default
24abd 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
24abe 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 20 3d 20  pIdx->onError = 
24abf 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3b  pIndex->onError;
24ac0 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
24ac1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 67       }.        g
24ac2 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
24ac3 69 6e 64 65 78 3b 0a 20 20 20 20 20 20 7d 0a 20  index;.      }. 
24ac4 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c     }.  }..  /* L
24ac5 69 6e 6b 20 74 68 65 20 6e 65 77 20 49 6e 64 65  ink the new Inde
24ac6 78 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 69  x structure to i
24ac7 74 73 20 74 61 62 6c 65 20 61 6e 64 20 74 6f 20  ts table and to 
24ac8 74 68 65 20 6f 74 68 65 72 0a 20 20 2a 2a 20 69  the other.  ** i
24ac9 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
24aca 65 20 73 74 72 75 63 74 75 72 65 73 2e 20 0a 20  e structures. . 
24acb 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e   */.  if( db->in
24acc 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 49  it.busy ){.    I
24acd 6e 64 65 78 20 2a 70 3b 0a 20 20 20 20 70 20 3d  ndex *p;.    p =
24ace 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65   sqlite3HashInse
24acf 72 74 28 26 70 49 6e 64 65 78 2d 3e 70 53 63 68  rt(&pIndex->pSch
24ad0 65 6d 61 2d 3e 69 64 78 48 61 73 68 2c 20 0a 20  ema->idxHash, . 
24ad1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24ad2 20 20 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d           pIndex-
24ad3 3e 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53  >zName, sqlite3S
24ad4 74 72 6c 65 6e 33 30 28 70 49 6e 64 65 78 2d 3e  trlen30(pIndex->
24ad5 7a 4e 61 6d 65 29 2c 0a 20 20 20 20 20 20 20 20  zName),.        
24ad6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24ad7 20 20 70 49 6e 64 65 78 29 3b 0a 20 20 20 20 69    pIndex);.    i
24ad8 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 61 73  f( p ){.      as
24ad9 73 65 72 74 28 20 70 3d 3d 70 49 6e 64 65 78 20  sert( p==pIndex 
24ada 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75  );  /* Malloc mu
24adb 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 2a  st have failed *
24adc 2f 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c  /.      db->mall
24add 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
24ade 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72      goto exit_cr
24adf 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20  eate_index;.    
24ae0 7d 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20  }.    db->flags 
24ae1 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  |= SQLITE_Intern
24ae2 43 68 61 6e 67 65 73 3b 0a 20 20 20 20 69 66 28  Changes;.    if(
24ae3 20 70 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a   pTblName!=0 ){.
24ae4 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 74 6e        pIndex->tn
24ae5 75 6d 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 6e 65  um = db->init.ne
24ae6 77 54 6e 75 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d  wTnum;.    }.  }
24ae7 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 62  ..  /* If the db
24ae8 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20 30  ->init.busy is 0
24ae9 20 74 68 65 6e 20 63 72 65 61 74 65 20 74 68 65   then create the
24aea 20 69 6e 64 65 78 20 6f 6e 20 64 69 73 6b 2e 20   index on disk. 
24aeb 20 54 68 69 73 0a 20 20 2a 2a 20 69 6e 76 6f 6c   This.  ** invol
24aec 76 65 73 20 77 72 69 74 69 6e 67 20 74 68 65 20  ves writing the 
24aed 69 6e 64 65 78 20 69 6e 74 6f 20 74 68 65 20 6d  index into the m
24aee 61 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20  aster table and 
24aef 66 69 6c 6c 69 6e 67 20 69 6e 20 74 68 65 0a 20  filling in the. 
24af0 20 2a 2a 20 69 6e 64 65 78 20 77 69 74 68 20 74   ** index with t
24af1 68 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65  he current table
24af2 20 63 6f 6e 74 65 6e 74 73 2e 0a 20 20 2a 2a 0a   contents..  **.
24af3 20 20 2a 2a 20 54 68 65 20 64 62 2d 3e 69 6e 69    ** The db->ini
24af4 74 2e 62 75 73 79 20 69 73 20 30 20 77 68 65 6e  t.busy is 0 when
24af5 20 74 68 65 20 75 73 65 72 20 66 69 72 73 74 20   the user first 
24af6 65 6e 74 65 72 73 20 61 20 43 52 45 41 54 45 20  enters a CREATE 
24af7 49 4e 44 45 58 20 0a 20 20 2a 2a 20 63 6f 6d 6d  INDEX .  ** comm
24af8 61 6e 64 2e 20 20 64 62 2d 3e 69 6e 69 74 2e 62  and.  db->init.b
24af9 75 73 79 20 69 73 20 31 20 77 68 65 6e 20 61 20  usy is 1 when a 
24afa 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e  database is open
24afb 65 64 20 61 6e 64 20 0a 20 20 2a 2a 20 43 52 45  ed and .  ** CRE
24afc 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 65 6d  ATE INDEX statem
24afd 65 6e 74 73 20 61 72 65 20 72 65 61 64 20 6f 75  ents are read ou
24afe 74 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20  t of the master 
24aff 74 61 62 6c 65 2e 20 20 49 6e 0a 20 20 2a 2a 20  table.  In.  ** 
24b00 74 68 65 20 6c 61 74 74 65 72 20 63 61 73 65 20  the latter case 
24b01 74 68 65 20 69 6e 64 65 78 20 61 6c 72 65 61 64  the index alread
24b02 79 20 65 78 69 73 74 73 20 6f 6e 20 64 69 73 6b  y exists on disk
24b03 2c 20 77 68 69 63 68 20 69 73 20 77 68 79 0a 20  , which is why. 
24b04 20 2a 2a 20 77 65 20 64 6f 6e 27 74 20 77 61 6e   ** we don't wan
24b05 74 20 74 6f 20 72 65 63 72 65 61 74 65 20 69 74  t to recreate it
24b06 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70  ..  **.  ** If p
24b07 54 62 6c 4e 61 6d 65 3d 3d 30 20 69 74 20 6d 65  TblName==0 it me
24b08 61 6e 73 20 74 68 69 73 20 69 6e 64 65 78 20 69  ans this index i
24b09 73 20 67 65 6e 65 72 61 74 65 64 20 61 73 20 61  s generated as a
24b0a 20 70 72 69 6d 61 72 79 20 6b 65 79 0a 20 20 2a   primary key.  *
24b0b 2a 20 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73  * or UNIQUE cons
24b0c 74 72 61 69 6e 74 20 6f 66 20 61 20 43 52 45 41  traint of a CREA
24b0d 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  TE TABLE stateme
24b0e 6e 74 2e 20 20 53 69 6e 63 65 20 74 68 65 20 74  nt.  Since the t
24b0f 61 62 6c 65 0a 20 20 2a 2a 20 68 61 73 20 6a 75  able.  ** has ju
24b10 73 74 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c  st been created,
24b11 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20   it contains no 
24b12 64 61 74 61 20 61 6e 64 20 74 68 65 20 69 6e 64  data and the ind
24b13 65 78 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  ex initializatio
24b14 6e 0a 20 20 2a 2a 20 73 74 65 70 20 63 61 6e 20  n.  ** step can 
24b15 62 65 20 73 6b 69 70 70 65 64 2e 0a 20 20 2a 2f  be skipped..  */
24b16 0a 20 20 65 6c 73 65 7b 20 2f 2a 20 69 66 28 20  .  else{ /* if( 
24b17 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30  db->init.busy==0
24b18 20 29 20 2a 2f 0a 20 20 20 20 56 64 62 65 20 2a   ) */.    Vdbe *
24b19 76 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 74  v;.    char *zSt
24b1a 6d 74 3b 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d  mt;.    int iMem
24b1b 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
24b1c 6d 3b 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c 69  m;..    v = sqli
24b1d 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
24b1e 65 29 3b 0a 20 20 20 20 69 66 28 20 76 3d 3d 30  e);.    if( v==0
24b1f 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65   ) goto exit_cre
24b20 61 74 65 5f 69 6e 64 65 78 3b 0a 0a 0a 20 20 20  ate_index;...   
24b21 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 72   /* Create the r
24b22 6f 6f 74 70 61 67 65 20 66 6f 72 20 74 68 65 20  ootpage for the 
24b23 69 6e 64 65 78 0a 20 20 20 20 2a 2f 0a 20 20 20  index.    */.   
24b24 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
24b25 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72  teOperation(pPar
24b26 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20  se, 1, iDb);.   
24b27 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24b28 70 32 28 76 2c 20 4f 50 5f 43 72 65 61 74 65 49  p2(v, OP_CreateI
24b29 6e 64 65 78 2c 20 69 44 62 2c 20 69 4d 65 6d 29  ndex, iDb, iMem)
24b2a 3b 0a 0a 20 20 20 20 2f 2a 20 47 61 74 68 65 72  ;..    /* Gather
24b2b 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65   the complete te
24b2c 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  xt of the CREATE
24b2d 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74   INDEX statement
24b2e 20 69 6e 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65   into.    ** the
24b2f 20 7a 53 74 6d 74 20 76 61 72 69 61 62 6c 65 0a   zStmt variable.
24b30 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
24b31 53 74 61 72 74 20 29 7b 0a 20 20 20 20 20 20 61  Start ){.      a
24b32 73 73 65 72 74 28 20 70 45 6e 64 21 3d 30 20 29  ssert( pEnd!=0 )
24b33 3b 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e 61 6d  ;.      /* A nam
24b34 65 64 20 69 6e 64 65 78 20 77 69 74 68 20 61 6e  ed index with an
24b35 20 65 78 70 6c 69 63 69 74 20 43 52 45 41 54 45   explicit CREATE
24b36 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74   INDEX statement
24b37 20 2a 2f 0a 20 20 20 20 20 20 7a 53 74 6d 74 20   */.      zStmt 
24b38 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
24b39 28 64 62 2c 20 22 43 52 45 41 54 45 25 73 20 49  (db, "CREATE%s I
24b3a 4e 44 45 58 20 25 2e 2a 73 22 2c 0a 20 20 20 20  NDEX %.*s",.    
24b3b 20 20 20 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f      onError==OE_
24b3c 4e 6f 6e 65 20 3f 20 22 22 20 3a 20 22 20 55 4e  None ? "" : " UN
24b3d 49 51 55 45 22 2c 0a 20 20 20 20 20 20 20 20 70  IQUE",.        p
24b3e 45 6e 64 2d 3e 7a 20 2d 20 70 4e 61 6d 65 2d 3e  End->z - pName->
24b3f 7a 20 2b 20 31 2c 0a 20 20 20 20 20 20 20 20 70  z + 1,.        p
24b40 4e 61 6d 65 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65  Name->z);.    }e
24b41 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6e  lse{.      /* An
24b42 20 61 75 74 6f 6d 61 74 69 63 20 69 6e 64 65 78   automatic index
24b43 20 63 72 65 61 74 65 64 20 62 79 20 61 20 50 52   created by a PR
24b44 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49  IMARY KEY or UNI
24b45 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a  QUE constraint *
24b46 2f 0a 20 20 20 20 20 20 2f 2a 20 7a 53 74 6d 74  /.      /* zStmt
24b47 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
24b48 66 28 22 22 29 3b 20 2a 2f 0a 20 20 20 20 20 20  f(""); */.      
24b49 7a 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 7d  zStmt = 0;.    }
24b4a 0a 0a 20 20 20 20 2f 2a 20 41 64 64 20 61 6e 20  ..    /* Add an 
24b4b 65 6e 74 72 79 20 69 6e 20 73 71 6c 69 74 65 5f  entry in sqlite_
24b4c 6d 61 73 74 65 72 20 66 6f 72 20 74 68 69 73 20  master for this 
24b4d 69 6e 64 65 78 0a 20 20 20 20 2a 2f 0a 20 20 20  index.    */.   
24b4e 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
24b4f 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20  rse(pParse, .   
24b50 20 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54       "INSERT INT
24b51 4f 20 25 51 2e 25 73 20 56 41 4c 55 45 53 28 27  O %Q.%s VALUES('
24b52 69 6e 64 65 78 27 2c 25 51 2c 25 51 2c 23 25 64  index',%Q,%Q,#%d
24b53 2c 25 51 29 3b 22 2c 0a 20 20 20 20 20 20 20 20  ,%Q);",.        
24b54 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
24b55 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  me, SCHEMA_TABLE
24b56 28 69 44 62 29 2c 0a 20 20 20 20 20 20 20 20 70  (iDb),.        p
24b57 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 0a 20 20  Index->zName,.  
24b58 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d        pTab->zNam
24b59 65 2c 0a 20 20 20 20 20 20 20 20 69 4d 65 6d 2c  e,.        iMem,
24b5a 0a 20 20 20 20 20 20 20 20 7a 53 74 6d 74 0a 20  .        zStmt. 
24b5b 20 20 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65     );.    sqlite
24b5c 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74 6d  3DbFree(db, zStm
24b5d 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6c 6c  t);..    /* Fill
24b5e 20 74 68 65 20 69 6e 64 65 78 20 77 69 74 68 20   the index with 
24b5f 64 61 74 61 20 61 6e 64 20 72 65 70 61 72 73 65  data and reparse
24b60 20 74 68 65 20 73 63 68 65 6d 61 2e 20 43 6f 64   the schema. Cod
24b61 65 20 61 6e 20 4f 50 5f 45 78 70 69 72 65 0a 20  e an OP_Expire. 
24b62 20 20 20 2a 2a 20 74 6f 20 69 6e 76 61 6c 69 64     ** to invalid
24b63 61 74 65 20 61 6c 6c 20 70 72 65 2d 63 6f 6d 70  ate all pre-comp
24b64 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e  iled statements.
24b65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
24b66 70 54 62 6c 4e 61 6d 65 20 29 7b 0a 20 20 20 20  pTblName ){.    
24b67 20 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c 49    sqlite3RefillI
24b68 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 49 6e  ndex(pParse, pIn
24b69 64 65 78 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20  dex, iMem);.    
24b6a 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43    sqlite3ChangeC
24b6b 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44  ookie(pParse, iD
24b6c 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
24b6d 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
24b6e 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69  P_ParseSchema, i
24b6f 44 62 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20  Db, 0, 0,.      
24b70 20 20 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74     sqlite3MPrint
24b71 66 28 64 62 2c 20 22 6e 61 6d 65 3d 27 25 71 27  f(db, "name='%q'
24b72 22 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65  ", pIndex->zName
24b73 29 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a  ), P4_DYNAMIC);.
24b74 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
24b75 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 45 78  eAddOp1(v, OP_Ex
24b76 70 69 72 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  pire, 0);.    }.
24b77 20 20 7d 0a 0a 20 20 2f 2a 20 57 68 65 6e 20 61    }..  /* When a
24b78 64 64 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 74  dding an index t
24b79 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e  o the list of in
24b7a 64 69 63 65 73 20 66 6f 72 20 61 20 74 61 62 6c  dices for a tabl
24b7b 65 2c 20 6d 61 6b 65 0a 20 20 2a 2a 20 73 75 72  e, make.  ** sur
24b7c 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6c 61  e all indices la
24b7d 62 65 6c 65 64 20 4f 45 5f 52 65 70 6c 61 63 65  beled OE_Replace
24b7e 20 63 6f 6d 65 20 61 66 74 65 72 20 61 6c 6c 20   come after all 
24b7f 74 68 6f 73 65 20 6c 61 62 65 6c 65 64 0a 20 20  those labeled.  
24b80 2a 2a 20 4f 45 5f 49 67 6e 6f 72 65 2e 20 20 54  ** OE_Ignore.  T
24b81 68 69 73 20 69 73 20 6e 65 63 65 73 73 61 72 79  his is necessary
24b82 20 66 6f 72 20 74 68 65 20 63 6f 72 72 65 63 74   for the correct
24b83 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63   constraint chec
24b84 6b 0a 20 20 2a 2a 20 70 72 6f 63 65 73 73 69 6e  k.  ** processin
24b85 67 20 28 69 6e 20 73 71 6c 69 74 65 33 47 65 6e  g (in sqlite3Gen
24b86 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43  erateConstraintC
24b87 68 65 63 6b 73 28 29 29 20 61 73 20 70 61 72 74  hecks()) as part
24b88 20 6f 66 0a 20 20 2a 2a 20 55 50 44 41 54 45 20   of.  ** UPDATE 
24b89 61 6e 64 20 49 4e 53 45 52 54 20 73 74 61 74 65  and INSERT state
24b8a 6d 65 6e 74 73 2e 20 20 0a 20 20 2a 2f 0a 20 20  ments.  .  */.  
24b8b 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  if( db->init.bus
24b8c 79 20 7c 7c 20 70 54 62 6c 4e 61 6d 65 3d 3d 30  y || pTblName==0
24b8d 20 29 7b 0a 20 20 20 20 69 66 28 20 6f 6e 45 72   ){.    if( onEr
24b8e 72 6f 72 21 3d 4f 45 5f 52 65 70 6c 61 63 65 20  ror!=OE_Replace 
24b8f 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d  || pTab->pIndex=
24b90 3d 30 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70  =0.         || p
24b91 54 61 62 2d 3e 70 49 6e 64 65 78 2d 3e 6f 6e 45  Tab->pIndex->onE
24b92 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65  rror==OE_Replace
24b93 29 7b 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d  ){.      pIndex-
24b94 3e 70 4e 65 78 74 20 3d 20 70 54 61 62 2d 3e 70  >pNext = pTab->p
24b95 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 70 54 61  Index;.      pTa
24b96 62 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 6e 64  b->pIndex = pInd
24b97 65 78 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ex;.    }else{. 
24b98 20 20 20 20 20 49 6e 64 65 78 20 2a 70 4f 74 68       Index *pOth
24b99 65 72 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65  er = pTab->pInde
24b9a 78 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  x;.      while( 
24b9b 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 20 26 26  pOther->pNext &&
24b9c 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 2d 3e   pOther->pNext->
24b9d 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 65 70 6c  onError!=OE_Repl
24b9e 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ace ){.        p
24b9f 4f 74 68 65 72 20 3d 20 70 4f 74 68 65 72 2d 3e  Other = pOther->
24ba0 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20  pNext;.      }. 
24ba1 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 4e 65       pIndex->pNe
24ba2 78 74 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65  xt = pOther->pNe
24ba3 78 74 3b 0a 20 20 20 20 20 20 70 4f 74 68 65 72  xt;.      pOther
24ba4 2d 3e 70 4e 65 78 74 20 3d 20 70 49 6e 64 65 78  ->pNext = pIndex
24ba5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 65 74  ;.    }.    pRet
24ba6 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20 20 20 70   = pIndex;.    p
24ba7 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 7d 0a 0a  Index = 0;.  }..
24ba8 20 20 2f 2a 20 43 6c 65 61 6e 20 75 70 20 62 65    /* Clean up be
24ba9 66 6f 72 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a  fore exiting */.
24baa 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
24bab 78 3a 0a 20 20 69 66 28 20 70 49 6e 64 65 78 20  x:.  if( pIndex 
24bac 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
24bad 72 65 65 28 70 49 6e 64 65 78 2d 3e 7a 43 6f 6c  ree(pIndex->zCol
24bae 41 66 66 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Aff);.    sqlite
24baf 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 6e 64  3DbFree(db, pInd
24bb0 65 78 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ex);.  }.  sqlit
24bb1 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
24bb2 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 73  (db, pList);.  s
24bb3 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c  qlite3SrcListDel
24bb4 65 74 65 28 64 62 2c 20 70 54 62 6c 4e 61 6d 65  ete(db, pTblName
24bb5 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
24bb6 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20  ee(db, zName);. 
24bb7 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a   return pRet;.}.
24bb8 0a 2f 2a 0a 2a 2a 20 46 69 6c 6c 20 74 68 65 20  ./*.** Fill the 
24bb9 49 6e 64 65 78 2e 61 69 52 6f 77 45 73 74 5b 5d  Index.aiRowEst[]
24bba 20 61 72 72 61 79 20 77 69 74 68 20 64 65 66 61   array with defa
24bbb 75 6c 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ult information 
24bbc 2d 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a  - information.**
24bbd 20 74 6f 20 62 65 20 75 73 65 64 20 77 68 65 6e   to be used when
24bbe 20 77 65 20 68 61 76 65 20 6e 6f 74 20 72 75 6e   we have not run
24bbf 20 74 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d   the ANALYZE com
24bc0 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 61 69 52 6f  mand..**.** aiRo
24bc1 77 45 73 74 5b 30 5d 20 69 73 20 73 75 70 70 6f  wEst[0] is suppo
24bc2 73 65 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68  se to contain th
24bc3 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  e number of elem
24bc4 65 6e 74 73 20 69 6e 20 74 68 65 20 69 6e 64 65  ents in the inde
24bc5 78 2e 0a 2a 2a 20 53 69 6e 63 65 20 77 65 20 64  x..** Since we d
24bc6 6f 20 6e 6f 74 20 6b 6e 6f 77 2c 20 67 75 65 73  o not know, gues
24bc7 73 20 31 20 6d 69 6c 6c 69 6f 6e 2e 20 20 61 69  s 1 million.  ai
24bc8 52 6f 77 45 73 74 5b 31 5d 20 69 73 20 61 6e 20  RowEst[1] is an 
24bc9 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 0a  estimate of the.
24bca 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  ** number of row
24bcb 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 74  s in the table t
24bcc 68 61 74 20 6d 61 74 63 68 20 61 6e 79 20 70 61  hat match any pa
24bcd 72 74 69 63 75 6c 61 72 20 76 61 6c 75 65 20 6f  rticular value o
24bce 66 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 63  f the.** first c
24bcf 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64  olumn of the ind
24bd0 65 78 2e 20 20 61 69 52 6f 77 45 73 74 5b 32 5d  ex.  aiRowEst[2]
24bd1 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20   is an estimate 
24bd2 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a  of the number.**
24bd3 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20 6d 61   of rows that ma
24bd4 74 63 68 20 61 6e 79 20 70 61 72 74 69 63 75 6c  tch any particul
24bd5 61 72 20 63 6f 6d 62 69 6e 69 61 74 69 6f 6e 20  ar combiniation 
24bd6 6f 66 20 74 68 65 20 66 69 72 73 74 20 32 20 63  of the first 2 c
24bd7 6f 6c 75 6d 6e 73 0a 2a 2a 20 6f 66 20 74 68 65  olumns.** of the
24bd8 20 69 6e 64 65 78 2e 20 20 41 6e 64 20 73 6f 20   index.  And so 
24bd9 66 6f 72 74 68 2e 20 20 49 74 20 6d 75 73 74 20  forth.  It must 
24bda 61 6c 77 61 79 73 20 62 65 20 74 68 65 20 63 61  always be the ca
24bdb 73 65 20 74 68 61 74 0a 2a 0a 2a 2a 20 20 20 20  se that.*.**    
24bdc 20 20 20 20 20 20 20 61 69 52 6f 77 45 73 74 5b         aiRowEst[
24bdd 4e 5d 3c 3d 61 69 52 6f 77 45 73 74 5b 4e 2d 31  N]<=aiRowEst[N-1
24bde 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61  ].**           a
24bdf 69 52 6f 77 45 73 74 5b 4e 5d 3e 3d 31 0a 2a 2a  iRowEst[N]>=1.**
24be0 0a 2a 2a 20 41 70 61 72 74 20 66 72 6f 6d 20 74  .** Apart from t
24be1 68 61 74 2c 20 77 65 20 68 61 76 65 20 6c 69 74  hat, we have lit
24be2 74 6c 65 20 74 6f 20 67 6f 20 6f 6e 20 62 65 73  tle to go on bes
24be3 69 64 65 73 20 69 6e 74 75 69 74 69 6f 6e 20 61  ides intuition a
24be4 73 20 74 6f 0a 2a 2a 20 68 6f 77 20 61 69 52 6f  s to.** how aiRo
24be5 77 45 73 74 5b 5d 20 73 68 6f 75 6c 64 20 62 65  wEst[] should be
24be6 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 54   initialized.  T
24be7 68 65 20 6e 75 6d 62 65 72 73 20 67 65 6e 65 72  he numbers gener
24be8 61 74 65 64 20 68 65 72 65 0a 2a 2a 20 61 72 65  ated here.** are
24be9 20 62 61 73 65 64 20 6f 6e 20 74 79 70 69 63 61   based on typica
24bea 6c 20 76 61 6c 75 65 73 20 66 6f 75 6e 64 20 69  l values found i
24beb 6e 20 61 63 74 75 61 6c 20 69 6e 64 69 63 65 73  n actual indices
24bec 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
24bed 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
24bee 44 65 66 61 75 6c 74 52 6f 77 45 73 74 28 49 6e  DefaultRowEst(In
24bef 64 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 75 6e  dex *pIdx){.  un
24bf0 73 69 67 6e 65 64 20 2a 61 20 3d 20 70 49 64 78  signed *a = pIdx
24bf1 2d 3e 61 69 52 6f 77 45 73 74 3b 0a 20 20 69 6e  ->aiRowEst;.  in
24bf2 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 61  t i;.  assert( a
24bf3 21 3d 30 20 29 3b 0a 20 20 61 5b 30 5d 20 3d 20  !=0 );.  a[0] = 
24bf4 31 30 30 30 30 30 30 3b 0a 20 20 66 6f 72 28 69  1000000;.  for(i
24bf5 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  =pIdx->nColumn; 
24bf6 69 3e 3d 35 3b 20 69 2d 2d 29 7b 0a 20 20 20 20  i>=5; i--){.    
24bf7 61 5b 69 5d 20 3d 20 35 3b 0a 20 20 7d 0a 20 20  a[i] = 5;.  }.  
24bf8 77 68 69 6c 65 28 20 69 3e 3d 31 20 29 7b 0a 20  while( i>=1 ){. 
24bf9 20 20 20 61 5b 69 5d 20 3d 20 31 31 20 2d 20 69     a[i] = 11 - i
24bfa 3b 0a 20 20 20 20 69 2d 2d 3b 0a 20 20 7d 0a 20  ;.    i--;.  }. 
24bfb 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72   if( pIdx->onErr
24bfc 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20  or!=OE_None ){. 
24bfd 20 20 20 61 5b 70 49 64 78 2d 3e 6e 43 6f 6c 75     a[pIdx->nColu
24bfe 6d 6e 5d 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a  mn] = 1;.  }.}..
24bff 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
24c00 6e 65 20 77 69 6c 6c 20 64 72 6f 70 20 61 6e 20  ne will drop an 
24c01 65 78 69 73 74 69 6e 67 20 6e 61 6d 65 64 20 69  existing named i
24c02 6e 64 65 78 2e 20 20 54 68 69 73 20 72 6f 75 74  ndex.  This rout
24c03 69 6e 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  ine.** implement
24c04 73 20 74 68 65 20 44 52 4f 50 20 49 4e 44 45 58  s the DROP INDEX
24c05 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53   statement..*/.S
24c06 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
24c07 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 49 6e  id sqlite3DropIn
24c08 64 65 78 28 50 61 72 73 65 20 2a 70 50 61 72 73  dex(Parse *pPars
24c09 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d  e, SrcList *pNam
24c0a 65 2c 20 69 6e 74 20 69 66 45 78 69 73 74 73 29  e, int ifExists)
24c0b 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  {.  Index *pInde
24c0c 78 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20  x;.  Vdbe *v;.  
24c0d 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
24c0e 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  arse->db;.  int 
24c0f 69 44 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  iDb;..  assert( 
24c10 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20  pParse->nErr==0 
24c11 29 3b 20 20 20 2f 2a 20 4e 65 76 65 72 20 63 61  );   /* Never ca
24c12 6c 6c 65 64 20 77 69 74 68 20 70 72 69 6f 72 20  lled with prior 
24c13 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 66 28 20  errors */.  if( 
24c14 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
24c15 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69   ){.    goto exi
24c16 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20  t_drop_index;.  
24c17 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d  }.  assert( pNam
24c18 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20  e->nSrc==1 );.  
24c19 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
24c1a 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
24c1b 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20  (pParse) ){.    
24c1c 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69  goto exit_drop_i
24c1d 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 70 49 6e 64  ndex;.  }.  pInd
24c1e 65 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ex = sqlite3Find
24c1f 49 6e 64 65 78 28 64 62 2c 20 70 4e 61 6d 65 2d  Index(db, pName-
24c20 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 4e 61  >a[0].zName, pNa
24c21 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61  me->a[0].zDataba
24c22 73 65 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65  se);.  if( pInde
24c23 78 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  x==0 ){.    if( 
24c24 21 69 66 45 78 69 73 74 73 20 29 7b 0a 20 20 20  !ifExists ){.   
24c25 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
24c26 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73  sg(pParse, "no s
24c27 75 63 68 20 69 6e 64 65 78 3a 20 25 53 22 2c 20  uch index: %S", 
24c28 70 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d  pName, 0);.    }
24c29 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 68 65  .    pParse->che
24c2a 63 6b 53 63 68 65 6d 61 20 3d 20 31 3b 0a 20 20  ckSchema = 1;.  
24c2b 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
24c2c 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 69 66  _index;.  }.  if
24c2d 28 20 70 49 6e 64 65 78 2d 3e 61 75 74 6f 49 6e  ( pIndex->autoIn
24c2e 64 65 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  dex ){.    sqlit
24c2f 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
24c30 65 2c 20 22 69 6e 64 65 78 20 61 73 73 6f 63 69  e, "index associ
24c31 61 74 65 64 20 77 69 74 68 20 55 4e 49 51 55 45  ated with UNIQUE
24c32 20 22 0a 20 20 20 20 20 20 22 6f 72 20 50 52 49   ".      "or PRI
24c33 4d 41 52 59 20 4b 45 59 20 63 6f 6e 73 74 72 61  MARY KEY constra
24c34 69 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20 64 72  int cannot be dr
24c35 6f 70 70 65 64 22 2c 20 30 29 3b 0a 20 20 20 20  opped", 0);.    
24c36 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69  goto exit_drop_i
24c37 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 69 44 62 20  ndex;.  }.  iDb 
24c38 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
24c39 6f 49 6e 64 65 78 28 64 62 2c 20 70 49 6e 64 65  oIndex(db, pInde
24c3a 78 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 23 69 66  x->pSchema);.#if
24c3b 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
24c3c 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20  _AUTHORIZATION. 
24c3d 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 65 20   {.    int code 
24c3e 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 49 4e  = SQLITE_DROP_IN
24c3f 44 45 58 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a  DEX;.    Table *
24c40 70 54 61 62 20 3d 20 70 49 6e 64 65 78 2d 3e 70  pTab = pIndex->p
24c41 54 61 62 6c 65 3b 0a 20 20 20 20 63 6f 6e 73 74  Table;.    const
24c42 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d   char *zDb = db-
24c43 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b  >aDb[iDb].zName;
24c44 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
24c45 2a 7a 54 61 62 20 3d 20 53 43 48 45 4d 41 5f 54  *zTab = SCHEMA_T
24c46 41 42 4c 45 28 69 44 62 29 3b 0a 20 20 20 20 69  ABLE(iDb);.    i
24c47 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
24c48 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
24c49 54 45 5f 44 45 4c 45 54 45 2c 20 7a 54 61 62 2c  TE_DELETE, zTab,
24c4a 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20   0, zDb) ){.    
24c4b 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
24c4c 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20  _index;.    }.  
24c4d 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50    if( !OMIT_TEMP
24c4e 44 42 20 26 26 20 69 44 62 20 29 20 63 6f 64 65  DB && iDb ) code
24c4f 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54   = SQLITE_DROP_T
24c50 45 4d 50 5f 49 4e 44 45 58 3b 0a 20 20 20 20 69  EMP_INDEX;.    i
24c51 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
24c52 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65  eck(pParse, code
24c53 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c  , pIndex->zName,
24c54 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 44   pTab->zName, zD
24c55 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  b) ){.      goto
24c56 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78   exit_drop_index
24c57 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
24c58 69 66 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74  if..  /* Generat
24c59 65 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65  e code to remove
24c5a 20 74 68 65 20 69 6e 64 65 78 20 61 6e 64 20 66   the index and f
24c5b 72 6f 6d 20 74 68 65 20 6d 61 73 74 65 72 20 74  rom the master t
24c5c 61 62 6c 65 20 2a 2f 0a 20 20 76 20 3d 20 73 71  able */.  v = sq
24c5d 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
24c5e 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b  rse);.  if( v ){
24c5f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69  .    sqlite3Begi
24c60 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
24c61 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62 29 3b  pParse, 1, iDb);
24c62 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74  .    sqlite3Nest
24c63 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
24c64 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46         "DELETE F
24c65 52 4f 4d 20 25 51 2e 25 73 20 57 48 45 52 45 20  ROM %Q.%s WHERE 
24c66 6e 61 6d 65 3d 25 51 22 2c 0a 20 20 20 20 20 20  name=%Q",.      
24c67 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
24c68 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  ame, SCHEMA_TABL
24c69 45 28 69 44 62 29 2c 0a 20 20 20 20 20 20 20 70  E(iDb),.       p
24c6a 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 0a 20 20 20  Index->zName.   
24c6b 20 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   );.    if( sqli
24c6c 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c  te3FindTable(db,
24c6d 20 22 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c   "sqlite_stat1",
24c6e 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
24c6f 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71  ame) ){.      sq
24c70 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
24c71 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
24c72 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51   "DELETE FROM %Q
24c73 2e 73 71 6c 69 74 65 5f 73 74 61 74 31 20 57 48  .sqlite_stat1 WH
24c74 45 52 45 20 69 64 78 3d 25 51 22 2c 0a 20 20 20  ERE idx=%Q",.   
24c75 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62       db->aDb[iDb
24c76 5d 2e 7a 4e 61 6d 65 2c 20 70 49 6e 64 65 78 2d  ].zName, pIndex-
24c77 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20 29 3b 0a  >zName.      );.
24c78 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
24c79 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50  3ChangeCookie(pP
24c7a 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20  arse, iDb);.    
24c7b 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67 65 28  destroyRootPage(
24c7c 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2d 3e  pParse, pIndex->
24c7d 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20 20 20  tnum, iDb);.    
24c7e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24c7f 34 28 76 2c 20 4f 50 5f 44 72 6f 70 49 6e 64 65  4(v, OP_DropInde
24c80 78 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 70 49  x, iDb, 0, 0, pI
24c81 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b  ndex->zName, 0);
24c82 0a 20 20 7d 0a 0a 65 78 69 74 5f 64 72 6f 70 5f  .  }..exit_drop_
24c83 69 6e 64 65 78 3a 0a 20 20 73 71 6c 69 74 65 33  index:.  sqlite3
24c84 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62  SrcListDelete(db
24c85 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a  , pName);.}../*.
24c86 2a 2a 20 70 41 72 72 61 79 20 69 73 20 61 20 70  ** pArray is a p
24c87 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72  ointer to an arr
24c88 61 79 20 6f 66 20 6f 62 6a 65 63 74 73 2e 20 20  ay of objects.  
24c89 45 61 63 68 20 6f 62 6a 65 63 74 20 69 6e 20 74  Each object in t
24c8a 68 65 0a 2a 2a 20 61 72 72 61 79 20 69 73 20 73  he.** array is s
24c8b 7a 45 6e 74 72 79 20 62 79 74 65 73 20 69 6e 20  zEntry bytes in 
24c8c 73 69 7a 65 2e 20 20 54 68 69 73 20 72 6f 75 74  size.  This rout
24c8d 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20  ine allocates a 
24c8e 6e 65 77 0a 2a 2a 20 6f 62 6a 65 63 74 20 6f 6e  new.** object on
24c8f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
24c90 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 2a 70 6e  array..**.** *pn
24c91 45 6e 74 72 79 20 69 73 20 74 68 65 20 6e 75 6d  Entry is the num
24c92 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61  ber of entries a
24c93 6c 72 65 61 64 79 20 69 6e 20 75 73 65 2e 20 20  lready in use.  
24c94 2a 70 6e 41 6c 6c 6f 63 20 69 73 0a 2a 2a 20 74  *pnAlloc is.** t
24c95 68 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c  he previously al
24c96 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20  located size of 
24c97 74 68 65 20 61 72 72 61 79 2e 20 20 69 6e 69 74  the array.  init
24c98 53 69 7a 65 20 69 73 20 74 68 65 0a 2a 2a 20 73  Size is the.** s
24c99 75 67 67 65 73 74 65 64 20 69 6e 69 74 69 61 6c  uggested initial
24c9a 20 61 72 72 61 79 20 73 69 7a 65 20 61 6c 6c 6f   array size allo
24c9b 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  cation..**.** Th
24c9c 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6e  e index of the n
24c9d 65 77 20 65 6e 74 72 79 20 69 73 20 72 65 74 75  ew entry is retu
24c9e 72 6e 65 64 20 69 6e 20 2a 70 49 64 78 2e 0a 2a  rned in *pIdx..*
24c9f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
24ca0 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  e returns a poin
24ca1 74 65 72 20 74 6f 20 74 68 65 20 61 72 72 61 79  ter to the array
24ca2 20 6f 66 20 6f 62 6a 65 63 74 73 2e 20 20 54 68   of objects.  Th
24ca3 69 73 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 74  is.** might be t
24ca4 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 70  he same as the p
24ca5 41 72 72 61 79 20 70 61 72 61 6d 65 74 65 72 20  Array parameter 
24ca6 6f 72 20 69 74 20 6d 69 67 68 74 20 62 65 20 61  or it might be a
24ca7 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 70 6f   different.** po
24ca8 69 6e 74 65 72 20 69 66 20 74 68 65 20 61 72 72  inter if the arr
24ca9 61 79 20 77 61 73 20 72 65 73 69 7a 65 64 2e 0a  ay was resized..
24caa 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
24cab 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 41  E void *sqlite3A
24cac 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20  rrayAllocate(.  
24cad 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
24cae 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20    /* Connection 
24caf 74 6f 20 6e 6f 74 69 66 79 20 6f 66 20 6d 61 6c  to notify of mal
24cb0 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 2a 2f 0a  loc failures */.
24cb1 20 20 76 6f 69 64 20 2a 70 41 72 72 61 79 2c 20    void *pArray, 
24cb2 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20      /* Array of 
24cb3 6f 62 6a 65 63 74 73 2e 20 20 4d 69 67 68 74 20  objects.  Might 
24cb4 62 65 20 72 65 61 6c 6c 6f 63 61 74 65 64 20 2a  be reallocated *
24cb5 2f 0a 20 20 69 6e 74 20 73 7a 45 6e 74 72 79 2c  /.  int szEntry,
24cb6 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
24cb7 20 65 61 63 68 20 6f 62 6a 65 63 74 20 69 6e 20   each object in 
24cb8 74 68 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 69  the array */.  i
24cb9 6e 74 20 69 6e 69 74 53 69 7a 65 2c 20 20 20 20  nt initSize,    
24cba 20 2f 2a 20 53 75 67 67 65 73 74 65 64 20 69 6e   /* Suggested in
24cbb 69 74 69 61 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e  itial allocation
24cbc 2c 20 69 6e 20 65 6c 65 6d 65 6e 74 73 20 2a 2f  , in elements */
24cbd 0a 20 20 69 6e 74 20 2a 70 6e 45 6e 74 72 79 2c  .  int *pnEntry,
24cbe 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
24cbf 66 20 6f 62 6a 65 63 74 73 20 63 75 72 72 65 6e  f objects curren
24cc0 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 20 20  tly in use */.  
24cc1 69 6e 74 20 2a 70 6e 41 6c 6c 6f 63 2c 20 20 20  int *pnAlloc,   
24cc2 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 69 7a    /* Current siz
24cc3 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74  e of the allocat
24cc4 69 6f 6e 2c 20 69 6e 20 65 6c 65 6d 65 6e 74 73  ion, in elements
24cc5 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 49 64 78 20   */.  int *pIdx 
24cc6 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
24cc7 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 61 20   the index of a 
24cc8 6e 65 77 20 73 6c 6f 74 20 68 65 72 65 20 2a 2f  new slot here */
24cc9 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20  .){.  char *z;. 
24cca 20 69 66 28 20 2a 70 6e 45 6e 74 72 79 20 3e 3d   if( *pnEntry >=
24ccb 20 2a 70 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20   *pnAlloc ){.   
24ccc 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 20   void *pNew;.   
24ccd 20 69 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 20 20   int newSize;.  
24cce 20 20 6e 65 77 53 69 7a 65 20 3d 20 28 2a 70 6e    newSize = (*pn
24ccf 41 6c 6c 6f 63 29 2a 32 20 2b 20 69 6e 69 74 53  Alloc)*2 + initS
24cd0 69 7a 65 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20  ize;.    pNew = 
24cd1 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
24cd2 28 64 62 2c 20 70 41 72 72 61 79 2c 20 6e 65 77  (db, pArray, new
24cd3 53 69 7a 65 2a 73 7a 45 6e 74 72 79 29 3b 0a 20  Size*szEntry);. 
24cd4 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29     if( pNew==0 )
24cd5 7b 0a 20 20 20 20 20 20 2a 70 49 64 78 20 3d 20  {.      *pIdx = 
24cd6 2d 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  -1;.      return
24cd7 20 70 41 72 72 61 79 3b 0a 20 20 20 20 7d 0a 20   pArray;.    }. 
24cd8 20 20 20 2a 70 6e 41 6c 6c 6f 63 20 3d 20 73 71     *pnAlloc = sq
24cd9 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a  lite3DbMallocSiz
24cda 65 28 64 62 2c 20 70 4e 65 77 29 2f 73 7a 45 6e  e(db, pNew)/szEn
24cdb 74 72 79 3b 0a 20 20 20 20 70 41 72 72 61 79 20  try;.    pArray 
24cdc 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 7a 20  = pNew;.  }.  z 
24cdd 3d 20 28 63 68 61 72 2a 29 70 41 72 72 61 79 3b  = (char*)pArray;
24cde 0a 20 20 6d 65 6d 73 65 74 28 26 7a 5b 2a 70 6e  .  memset(&z[*pn
24cdf 45 6e 74 72 79 20 2a 20 73 7a 45 6e 74 72 79 5d  Entry * szEntry]
24ce0 2c 20 30 2c 20 73 7a 45 6e 74 72 79 29 3b 0a 20  , 0, szEntry);. 
24ce1 20 2a 70 49 64 78 20 3d 20 2a 70 6e 45 6e 74 72   *pIdx = *pnEntr
24ce2 79 3b 0a 20 20 2b 2b 2a 70 6e 45 6e 74 72 79 3b  y;.  ++*pnEntry;
24ce3 0a 20 20 72 65 74 75 72 6e 20 70 41 72 72 61 79  .  return pArray
24ce4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e  ;.}../*.** Appen
24ce5 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20  d a new element 
24ce6 74 6f 20 74 68 65 20 67 69 76 65 6e 20 49 64 4c  to the given IdL
24ce7 69 73 74 2e 20 20 43 72 65 61 74 65 20 61 20 6e  ist.  Create a n
24ce8 65 77 20 49 64 4c 69 73 74 20 69 66 0a 2a 2a 20  ew IdList if.** 
24ce9 6e 65 65 64 20 62 65 2e 0a 2a 2a 0a 2a 2a 20 41  need be..**.** A
24cea 20 6e 65 77 20 49 64 4c 69 73 74 20 69 73 20 72   new IdList is r
24ceb 65 74 75 72 6e 65 64 2c 20 6f 72 20 4e 55 4c 4c  eturned, or NULL
24cec 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69   if malloc() fai
24ced 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ls..*/.SQLITE_PR
24cee 49 56 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71  IVATE IdList *sq
24cef 6c 69 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e  lite3IdListAppen
24cf0 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 49  d(sqlite3 *db, I
24cf1 64 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 54 6f  dList *pList, To
24cf2 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20  ken *pToken){.  
24cf3 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69  int i;.  if( pLi
24cf4 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69  st==0 ){.    pLi
24cf5 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  st = sqlite3DbMa
24cf6 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
24cf7 65 6f 66 28 49 64 4c 69 73 74 29 20 29 3b 0a 20  eof(IdList) );. 
24cf8 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20     if( pList==0 
24cf9 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
24cfa 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20  pList->nAlloc = 
24cfb 30 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 74 2d 3e  0;.  }.  pList->
24cfc 61 20 3d 20 73 71 6c 69 74 65 33 41 72 72 61 79  a = sqlite3Array
24cfd 41 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20 20 20  Allocate(.      
24cfe 64 62 2c 0a 20 20 20 20 20 20 70 4c 69 73 74 2d  db,.      pList-
24cff 3e 61 2c 0a 20 20 20 20 20 20 73 69 7a 65 6f 66  >a,.      sizeof
24d00 28 70 4c 69 73 74 2d 3e 61 5b 30 5d 29 2c 0a 20  (pList->a[0]),. 
24d01 20 20 20 20 20 35 2c 0a 20 20 20 20 20 20 26 70       5,.      &p
24d02 4c 69 73 74 2d 3e 6e 49 64 2c 0a 20 20 20 20 20  List->nId,.     
24d03 20 26 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2c   &pList->nAlloc,
24d04 0a 20 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20  .      &i.  );. 
24d05 20 69 66 28 20 69 3c 30 20 29 7b 0a 20 20 20 20   if( i<0 ){.    
24d06 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c  sqlite3IdListDel
24d07 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a  ete(db, pList);.
24d08 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
24d09 7d 0a 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  }.  pList->a[i].
24d0a 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e  zName = sqlite3N
24d0b 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
24d0c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 72 65 74 75   pToken);.  retu
24d0d 72 6e 20 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a  rn pList;.}../*.
24d0e 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 49 64 4c  ** Delete an IdL
24d0f 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ist..*/.SQLITE_P
24d10 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
24d11 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28  te3IdListDelete(
24d12 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 49 64 4c  sqlite3 *db, IdL
24d13 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69  ist *pList){.  i
24d14 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73  nt i;.  if( pLis
24d15 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  t==0 ) return;. 
24d16 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73   for(i=0; i<pLis
24d17 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20  t->nId; i++){.  
24d18 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
24d19 64 62 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  db, pList->a[i].
24d1a 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 73 71  zName);.  }.  sq
24d1b 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
24d1c 70 4c 69 73 74 2d 3e 61 29 3b 0a 20 20 73 71 6c  pList->a);.  sql
24d1d 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
24d1e 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  List);.}../*.** 
24d1f 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78  Return the index
24d20 20 69 6e 20 70 4c 69 73 74 20 6f 66 20 74 68 65   in pList of the
24d21 20 69 64 65 6e 74 69 66 69 65 72 20 6e 61 6d 65   identifier name
24d22 64 20 7a 49 64 2e 20 20 52 65 74 75 72 6e 20 2d  d zId.  Return -
24d23 31 0a 2a 2a 20 69 66 20 6e 6f 74 20 66 6f 75 6e  1.** if not foun
24d24 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
24d25 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
24d26 49 64 4c 69 73 74 49 6e 64 65 78 28 49 64 4c 69  IdListIndex(IdLi
24d27 73 74 20 2a 70 4c 69 73 74 2c 20 63 6f 6e 73 74  st *pList, const
24d28 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20   char *zName){. 
24d29 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 4c   int i;.  if( pL
24d2a 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ist==0 ) return 
24d2b 2d 31 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  -1;.  for(i=0; i
24d2c 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b  <pList->nId; i++
24d2d 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
24d2e 65 33 53 74 72 49 43 6d 70 28 70 4c 69 73 74 2d  e3StrICmp(pList-
24d2f 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e 61  >a[i].zName, zNa
24d30 6d 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  me)==0 ) return 
24d31 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  i;.  }.  return 
24d32 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70  -1;.}../*.** Exp
24d33 61 6e 64 20 74 68 65 20 73 70 61 63 65 20 61 6c  and the space al
24d34 6c 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20  located for the 
24d35 67 69 76 65 6e 20 53 72 63 4c 69 73 74 20 6f 62  given SrcList ob
24d36 6a 65 63 74 20 62 79 0a 2a 2a 20 63 72 65 61 74  ject by.** creat
24d37 69 6e 67 20 6e 45 78 74 72 61 20 6e 65 77 20 73  ing nExtra new s
24d38 6c 6f 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 61  lots beginning a
24d39 74 20 69 53 74 61 72 74 2e 20 20 69 53 74 61 72  t iStart.  iStar
24d3a 74 20 69 73 20 7a 65 72 6f 20 62 61 73 65 64 2e  t is zero based.
24d3b 0a 2a 2a 20 4e 65 77 20 73 6c 6f 74 73 20 61 72  .** New slots ar
24d3c 65 20 7a 65 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20  e zeroed..**.** 
24d3d 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73 75 70  For example, sup
24d3e 70 6f 73 65 20 61 20 53 72 63 4c 69 73 74 20 69  pose a SrcList i
24d3f 6e 69 74 69 61 6c 6c 79 20 63 6f 6e 74 61 69 6e  nitially contain
24d40 73 20 74 77 6f 20 65 6e 74 72 69 65 73 3a 20 41  s two entries: A
24d41 2c 42 2e 0a 2a 2a 20 54 6f 20 61 70 70 65 6e 64  ,B..** To append
24d42 20 33 20 6e 65 77 20 65 6e 74 72 69 65 73 20 6f   3 new entries o
24d43 6e 74 6f 20 74 68 65 20 65 6e 64 2c 20 64 6f 20  nto the end, do 
24d44 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 73  this:.**.**    s
24d45 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c  qlite3SrcListEnl
24d46 61 72 67 65 28 64 62 2c 20 70 53 72 63 6c 69 73  arge(db, pSrclis
24d47 74 2c 20 33 2c 20 32 29 3b 0a 2a 2a 0a 2a 2a 20  t, 3, 2);.**.** 
24d48 41 66 74 65 72 20 74 68 65 20 63 61 6c 6c 20 61  After the call a
24d49 62 6f 76 65 20 69 74 20 77 6f 75 6c 64 20 63 6f  bove it would co
24d4a 6e 74 61 69 6e 3a 20 20 41 2c 20 42 2c 20 6e 69  ntain:  A, B, ni
24d4b 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2e 0a 2a 2a 20  l, nil, nil..** 
24d4c 49 66 20 74 68 65 20 69 53 74 61 72 74 20 61 72  If the iStart ar
24d4d 67 75 6d 65 6e 74 20 68 61 64 20 62 65 65 6e 20  gument had been 
24d4e 31 20 69 6e 73 74 65 61 64 20 6f 66 20 32 2c 20  1 instead of 2, 
24d4f 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 0a  then the result.
24d50 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 62 65  ** would have be
24d51 65 6e 3a 20 20 41 2c 20 6e 69 6c 2c 20 6e 69 6c  en:  A, nil, nil
24d52 2c 20 6e 69 6c 2c 20 42 2e 20 20 54 6f 20 70 72  , nil, B.  To pr
24d53 65 70 65 6e 64 20 74 68 65 20 6e 65 77 20 73 6c  epend the new sl
24d54 6f 74 73 2c 0a 2a 2a 20 74 68 65 20 69 53 74 61  ots,.** the iSta
24d55 72 74 20 76 61 6c 75 65 20 77 6f 75 6c 64 20 62  rt value would b
24d56 65 20 30 2e 20 20 54 68 65 20 72 65 73 75 6c 74  e 0.  The result
24d57 20 74 68 65 6e 20 77 6f 75 6c 64 0a 2a 2a 20 62   then would.** b
24d58 65 3a 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c  e: nil, nil, nil
24d59 2c 20 41 2c 20 42 2e 0a 2a 2a 0a 2a 2a 20 49 66  , A, B..**.** If
24d5a 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
24d5b 74 69 6f 6e 20 66 61 69 6c 73 20 74 68 65 20 53  tion fails the S
24d5c 72 63 4c 69 73 74 20 69 73 20 75 6e 63 68 61 6e  rcList is unchan
24d5d 67 65 64 2e 20 20 54 68 65 0a 2a 2a 20 64 62 2d  ged.  The.** db-
24d5e 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c  >mallocFailed fl
24d5f 61 67 20 77 69 6c 6c 20 62 65 20 73 65 74 20 74  ag will be set t
24d60 6f 20 74 72 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54  o true..*/.SQLIT
24d61 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73  E_PRIVATE SrcLis
24d62 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73  t *sqlite3SrcLis
24d63 74 45 6e 6c 61 72 67 65 28 0a 20 20 73 71 6c 69  tEnlarge(.  sqli
24d64 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 2f  te3 *db,       /
24d65 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
24d66 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 69 66 79 20  ction to notify 
24d67 6f 66 20 4f 4f 4d 20 65 72 72 6f 72 73 20 2a 2f  of OOM errors */
24d68 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
24d69 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 53 72 63  ,     /* The Src
24d6a 4c 69 73 74 20 74 6f 20 62 65 20 65 6e 6c 61 72  List to be enlar
24d6b 67 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78  ged */.  int nEx
24d6c 74 72 61 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e  tra,        /* N
24d6d 75 6d 62 65 72 20 6f 66 20 6e 65 77 20 73 6c 6f  umber of new slo
24d6e 74 73 20 74 6f 20 61 64 64 20 74 6f 20 70 53 72  ts to add to pSr
24d6f 63 2d 3e 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  c->a[] */.  int 
24d70 69 53 74 61 72 74 20 20 20 20 20 20 20 20 20 2f  iStart         /
24d71 2a 20 49 6e 64 65 78 20 69 6e 20 70 53 72 63 2d  * Index in pSrc-
24d72 3e 61 5b 5d 20 6f 66 20 66 69 72 73 74 20 6e 65  >a[] of first ne
24d73 77 20 73 6c 6f 74 20 2a 2f 0a 29 7b 0a 20 20 69  w slot */.){.  i
24d74 6e 74 20 69 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69  nt i;..  /* Sani
24d75 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 63  ty checking on c
24d76 61 6c 6c 69 6e 67 20 70 61 72 61 6d 65 74 65 72  alling parameter
24d77 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69  s */.  assert( i
24d78 53 74 61 72 74 3e 3d 30 20 29 3b 0a 20 20 61 73  Start>=0 );.  as
24d79 73 65 72 74 28 20 6e 45 78 74 72 61 3e 3d 31 20  sert( nExtra>=1 
24d7a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 72  );.  assert( pSr
24d7b 63 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  c!=0 );.  assert
24d7c 28 20 69 53 74 61 72 74 3c 3d 70 53 72 63 2d 3e  ( iStart<=pSrc->
24d7d 6e 53 72 63 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c  nSrc );..  /* Al
24d7e 6c 6f 63 61 74 65 20 61 64 64 69 74 69 6f 6e 61  locate additiona
24d7f 6c 20 73 70 61 63 65 20 69 66 20 6e 65 65 64 65  l space if neede
24d80 64 20 2a 2f 0a 20 20 69 66 28 20 70 53 72 63 2d  d */.  if( pSrc-
24d81 3e 6e 53 72 63 2b 6e 45 78 74 72 61 3e 70 53 72  >nSrc+nExtra>pSr
24d82 63 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20  c->nAlloc ){.   
24d83 20 53 72 63 4c 69 73 74 20 2a 70 4e 65 77 3b 0a   SrcList *pNew;.
24d84 20 20 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d      int nAlloc =
24d85 20 70 53 72 63 2d 3e 6e 53 72 63 2b 6e 45 78 74   pSrc->nSrc+nExt
24d86 72 61 3b 0a 20 20 20 20 69 6e 74 20 6e 47 6f 74  ra;.    int nGot
24d87 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c  ;.    pNew = sql
24d88 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62  ite3DbRealloc(db
24d89 2c 20 70 53 72 63 2c 0a 20 20 20 20 20 20 20 20  , pSrc,.        
24d8a 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70         sizeof(*p
24d8b 53 72 63 29 20 2b 20 28 6e 41 6c 6c 6f 63 2d 31  Src) + (nAlloc-1
24d8c 29 2a 73 69 7a 65 6f 66 28 70 53 72 63 2d 3e 61  )*sizeof(pSrc->a
24d8d 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 28 20  [0]) );.    if( 
24d8e 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20  pNew==0 ){.     
24d8f 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c   assert( db->mal
24d90 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20  locFailed );.   
24d91 20 20 20 72 65 74 75 72 6e 20 70 53 72 63 3b 0a     return pSrc;.
24d92 20 20 20 20 7d 0a 20 20 20 20 70 53 72 63 20 3d      }.    pSrc =
24d93 20 70 4e 65 77 3b 0a 20 20 20 20 6e 47 6f 74 20   pNew;.    nGot 
24d94 3d 20 28 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  = (sqlite3DbMall
24d95 6f 63 53 69 7a 65 28 64 62 2c 20 70 4e 65 77 29  ocSize(db, pNew)
24d96 20 2d 20 73 69 7a 65 6f 66 28 2a 70 53 72 63 29   - sizeof(*pSrc)
24d97 29 2f 73 69 7a 65 6f 66 28 70 53 72 63 2d 3e 61  )/sizeof(pSrc->a
24d98 5b 30 5d 29 2b 31 3b 0a 20 20 20 20 70 53 72 63  [0])+1;.    pSrc
24d99 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 28 75 31 36 29  ->nAlloc = (u16)
24d9a 6e 47 6f 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  nGot;.  }..  /* 
24d9b 4d 6f 76 65 20 65 78 69 73 74 69 6e 67 20 73 6c  Move existing sl
24d9c 6f 74 73 20 74 68 61 74 20 63 6f 6d 65 20 61 66  ots that come af
24d9d 74 65 72 20 74 68 65 20 6e 65 77 6c 79 20 69 6e  ter the newly in
24d9e 73 65 72 74 65 64 20 73 6c 6f 74 73 0a 20 20 2a  serted slots.  *
24d9f 2a 20 6f 75 74 20 6f 66 20 74 68 65 20 77 61 79  * out of the way
24da0 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 70 53 72 63   */.  for(i=pSrc
24da1 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 3d 69 53 74  ->nSrc-1; i>=iSt
24da2 61 72 74 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 70  art; i--){.    p
24da3 53 72 63 2d 3e 61 5b 69 2b 6e 45 78 74 72 61 5d  Src->a[i+nExtra]
24da4 20 3d 20 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20   = pSrc->a[i];. 
24da5 20 7d 0a 20 20 70 53 72 63 2d 3e 6e 53 72 63 20   }.  pSrc->nSrc 
24da6 2b 3d 20 28 69 31 36 29 6e 45 78 74 72 61 3b 0a  += (i16)nExtra;.
24da7 0a 20 20 2f 2a 20 5a 65 72 6f 20 74 68 65 20 6e  .  /* Zero the n
24da8 65 77 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73  ewly allocated s
24da9 6c 6f 74 73 20 2a 2f 0a 20 20 6d 65 6d 73 65 74  lots */.  memset
24daa 28 26 70 53 72 63 2d 3e 61 5b 69 53 74 61 72 74  (&pSrc->a[iStart
24dab 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 53 72  ], 0, sizeof(pSr
24dac 63 2d 3e 61 5b 30 5d 29 2a 6e 45 78 74 72 61 29  c->a[0])*nExtra)
24dad 3b 0a 20 20 66 6f 72 28 69 3d 69 53 74 61 72 74  ;.  for(i=iStart
24dae 3b 20 69 3c 69 53 74 61 72 74 2b 6e 45 78 74 72  ; i<iStart+nExtr
24daf 61 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 53 72  a; i++){.    pSr
24db0 63 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 20  c->a[i].iCursor 
24db1 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  = -1;.  }..  /* 
24db2 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
24db3 20 74 6f 20 74 68 65 20 65 6e 6c 61 72 67 65 64   to the enlarged
24db4 20 53 72 63 4c 69 73 74 20 2a 2f 0a 20 20 72 65   SrcList */.  re
24db5 74 75 72 6e 20 70 53 72 63 3b 0a 7d 0a 0a 0a 2f  turn pSrc;.}.../
24db6 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 6e 65  *.** Append a ne
24db7 77 20 74 61 62 6c 65 20 6e 61 6d 65 20 74 6f 20  w table name to 
24db8 74 68 65 20 67 69 76 65 6e 20 53 72 63 4c 69 73  the given SrcLis
24db9 74 2e 20 20 43 72 65 61 74 65 20 61 20 6e 65 77  t.  Create a new
24dba 20 53 72 63 4c 69 73 74 20 69 66 0a 2a 2a 20 6e   SrcList if.** n
24dbb 65 65 64 20 62 65 2e 20 20 41 20 6e 65 77 20 65  eed be.  A new e
24dbc 6e 74 72 79 20 69 73 20 63 72 65 61 74 65 64 20  ntry is created 
24dbd 69 6e 20 74 68 65 20 53 72 63 4c 69 73 74 20 65  in the SrcList e
24dbe 76 65 6e 20 69 66 20 70 54 61 62 6c 65 20 69 73  ven if pTable is
24dbf 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 20 53   NULL..**.** A S
24dc0 72 63 4c 69 73 74 20 69 73 20 72 65 74 75 72 6e  rcList is return
24dc1 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74  ed, or NULL if t
24dc2 68 65 72 65 20 69 73 20 61 6e 20 4f 4f 4d 20 65  here is an OOM e
24dc3 72 72 6f 72 2e 20 20 54 68 65 20 72 65 74 75 72  rror.  The retur
24dc4 6e 65 64 0a 2a 2a 20 53 72 63 4c 69 73 74 20 6d  ned.** SrcList m
24dc5 69 67 68 74 20 62 65 20 74 68 65 20 73 61 6d 65  ight be the same
24dc6 20 61 73 20 74 68 65 20 53 72 63 4c 69 73 74 20   as the SrcList 
24dc7 74 68 61 74 20 77 61 73 20 69 6e 70 75 74 20 6f  that was input o
24dc8 72 20 69 74 20 6d 69 67 68 74 20 62 65 0a 2a 2a  r it might be.**
24dc9 20 61 20 6e 65 77 20 6f 6e 65 2e 20 20 49 66 20   a new one.  If 
24dca 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 64 6f 65  an OOM error doe
24dcb 73 20 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 74  s occurs, then t
24dcc 68 65 20 70 72 69 6f 72 20 76 61 6c 75 65 20 6f  he prior value o
24dcd 66 20 70 4c 69 73 74 0a 2a 2a 20 74 68 61 74 20  f pList.** that 
24dce 69 73 20 69 6e 70 75 74 20 74 6f 20 74 68 69 73  is input to this
24dcf 20 72 6f 75 74 69 6e 65 20 69 73 20 61 75 74 6f   routine is auto
24dd0 6d 61 74 69 63 61 6c 6c 79 20 66 72 65 65 64 2e  matically freed.
24dd1 0a 2a 2a 0a 2a 2a 20 49 66 20 70 44 61 74 61 62  .**.** If pDatab
24dd2 61 73 65 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 2c  ase is not null,
24dd3 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74   it means that t
24dd4 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20  he table has an 
24dd5 6f 70 74 69 6f 6e 61 6c 0a 2a 2a 20 64 61 74 61  optional.** data
24dd6 62 61 73 65 20 6e 61 6d 65 20 70 72 65 66 69 78  base name prefix
24dd7 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a 20 20 22  .  Like this:  "
24dd8 64 61 74 61 62 61 73 65 2e 74 61 62 6c 65 22 2e  database.table".
24dd9 20 20 54 68 65 20 70 44 61 74 61 62 61 73 65 0a    The pDatabase.
24dda 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  ** points to the
24ddb 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20   table name and 
24ddc 74 68 65 20 70 54 61 62 6c 65 20 70 6f 69 6e 74  the pTable point
24ddd 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  s to the databas
24dde 65 20 6e 61 6d 65 2e 0a 2a 2a 20 54 68 65 20 53  e name..** The S
24ddf 72 63 4c 69 73 74 2e 61 5b 5d 2e 7a 4e 61 6d 65  rcList.a[].zName
24de0 20 66 69 65 6c 64 20 69 73 20 66 69 6c 6c 65 64   field is filled
24de1 20 77 69 74 68 20 74 68 65 20 74 61 62 6c 65 20   with the table 
24de2 6e 61 6d 65 20 77 68 69 63 68 20 6d 69 67 68 74  name which might
24de3 0a 2a 2a 20 63 6f 6d 65 20 66 72 6f 6d 20 70 54  .** come from pT
24de4 61 62 6c 65 20 28 69 66 20 70 44 61 74 61 62 61  able (if pDataba
24de5 73 65 20 69 73 20 4e 55 4c 4c 29 20 6f 72 20 66  se is NULL) or f
24de6 72 6f 6d 20 70 44 61 74 61 62 61 73 65 2e 20 20  rom pDatabase.  
24de7 0a 2a 2a 20 53 72 63 4c 69 73 74 2e 61 5b 5d 2e  .** SrcList.a[].
24de8 7a 44 61 74 61 62 61 73 65 20 69 73 20 66 69 6c  zDatabase is fil
24de9 6c 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74  led with the dat
24dea 61 62 61 73 65 20 6e 61 6d 65 20 66 72 6f 6d 20  abase name from 
24deb 70 54 61 62 6c 65 2c 0a 2a 2a 20 6f 72 20 77 69  pTable,.** or wi
24dec 74 68 20 4e 55 4c 4c 20 69 66 20 6e 6f 20 64 61  th NULL if no da
24ded 74 61 62 61 73 65 20 69 73 20 73 70 65 63 69 66  tabase is specif
24dee 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74  ied..**.** In ot
24def 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 63 61  her words, if ca
24df0 6c 6c 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  ll like this:.**
24df1 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 71 6c 69  .**         sqli
24df2 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
24df3 28 44 2c 41 2c 42 2c 30 29 3b 0a 2a 2a 0a 2a 2a  (D,A,B,0);.**.**
24df4 20 54 68 65 6e 20 42 20 69 73 20 61 20 74 61 62   Then B is a tab
24df5 6c 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20  le name and the 
24df6 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73  database name is
24df7 20 75 6e 73 70 65 63 69 66 69 65 64 2e 20 20 49   unspecified.  I
24df8 66 20 63 61 6c 6c 65 64 0a 2a 2a 20 6c 69 6b 65  f called.** like
24df9 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
24dfa 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c       sqlite3SrcL
24dfb 69 73 74 41 70 70 65 6e 64 28 44 2c 41 2c 42 2c  istAppend(D,A,B,
24dfc 43 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 43  C);.**.** Then C
24dfd 20 69 73 20 74 68 65 20 74 61 62 6c 65 20 6e 61   is the table na
24dfe 6d 65 20 61 6e 64 20 42 20 69 73 20 74 68 65 20  me and B is the 
24dff 64 61 74 61 62 61 73 65 20 6e 61 6d 65 2e 20 20  database name.  
24e00 49 66 20 43 20 69 73 20 64 65 66 69 6e 65 64 0a  If C is defined.
24e01 2a 2a 20 74 68 65 6e 20 73 6f 20 69 73 20 42 2e  ** then so is B.
24e02 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
24e03 2c 20 77 65 20 6e 65 76 65 72 20 68 61 76 65 20  , we never have 
24e04 61 20 63 61 73 65 20 77 68 65 72 65 3a 0a 2a 2a  a case where:.**
24e05 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 71 6c 69  .**         sqli
24e06 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
24e07 28 44 2c 41 2c 30 2c 43 29 3b 0a 2a 2a 0a 2a 2a  (D,A,0,C);.**.**
24e08 20 42 6f 74 68 20 70 54 61 62 6c 65 20 61 6e 64   Both pTable and
24e09 20 70 44 61 74 61 62 61 73 65 20 61 72 65 20 61   pDatabase are a
24e0a 73 73 75 6d 65 64 20 74 6f 20 62 65 20 71 75 6f  ssumed to be quo
24e0b 74 65 64 2e 20 20 54 68 65 79 20 61 72 65 20 64  ted.  They are d
24e0c 65 71 75 6f 74 65 64 0a 2a 2a 20 62 65 66 6f 72  equoted.** befor
24e0d 65 20 62 65 69 6e 67 20 61 64 64 65 64 20 74 6f  e being added to
24e0e 20 74 68 65 20 53 72 63 4c 69 73 74 2e 0a 2a 2f   the SrcList..*/
24e0f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
24e10 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33  SrcList *sqlite3
24e11 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 0a 20  SrcListAppend(. 
24e12 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
24e13 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69       /* Connecti
24e14 6f 6e 20 74 6f 20 6e 6f 74 69 66 79 20 6f 66 20  on to notify of 
24e15 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20  malloc failures 
24e16 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 4c  */.  SrcList *pL
24e17 69 73 74 2c 20 20 20 20 20 2f 2a 20 41 70 70 65  ist,     /* Appe
24e18 6e 64 20 74 6f 20 74 68 69 73 20 53 72 63 4c 69  nd to this SrcLi
24e19 73 74 2e 20 4e 55 4c 4c 20 63 72 65 61 74 65 73  st. NULL creates
24e1a 20 61 20 6e 65 77 20 53 72 63 4c 69 73 74 20 2a   a new SrcList *
24e1b 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c  /.  Token *pTabl
24e1c 65 2c 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65  e,      /* Table
24e1d 20 74 6f 20 61 70 70 65 6e 64 20 2a 2f 0a 20 20   to append */.  
24e1e 54 6f 6b 65 6e 20 2a 70 44 61 74 61 62 61 73 65  Token *pDatabase
24e1f 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
24e20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  of the table */.
24e21 29 7b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  ){.  struct SrcL
24e22 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
24e23 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74 61  .  assert( pData
24e24 62 61 73 65 3d 3d 30 20 7c 7c 20 70 54 61 62 6c  base==0 || pTabl
24e25 65 21 3d 30 20 29 3b 20 20 2f 2a 20 43 61 6e 6e  e!=0 );  /* Cann
24e26 6f 74 20 68 61 76 65 20 43 20 77 69 74 68 6f 75  ot have C withou
24e27 74 20 42 20 2a 2f 0a 20 20 69 66 28 20 70 4c 69  t B */.  if( pLi
24e28 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69  st==0 ){.    pLi
24e29 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  st = sqlite3DbMa
24e2a 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
24e2b 65 6f 66 28 53 72 63 4c 69 73 74 29 20 29 3b 0a  eof(SrcList) );.
24e2c 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30      if( pList==0
24e2d 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
24e2e 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d   pList->nAlloc =
24e2f 20 31 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 74 20   1;.  }.  pList 
24e30 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  = sqlite3SrcList
24e31 45 6e 6c 61 72 67 65 28 64 62 2c 20 70 4c 69 73  Enlarge(db, pLis
24e32 74 2c 20 31 2c 20 70 4c 69 73 74 2d 3e 6e 53 72  t, 1, pList->nSr
24e33 63 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  c);.  if( db->ma
24e34 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
24e35 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
24e36 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74  Delete(db, pList
24e37 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
24e38 0a 20 20 7d 0a 20 20 70 49 74 65 6d 20 3d 20 26  .  }.  pItem = &
24e39 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e  pList->a[pList->
24e3a 6e 53 72 63 2d 31 5d 3b 0a 20 20 69 66 28 20 70  nSrc-1];.  if( p
24e3b 44 61 74 61 62 61 73 65 20 26 26 20 70 44 61 74  Database && pDat
24e3c 61 62 61 73 65 2d 3e 7a 3d 3d 30 20 29 7b 0a 20  abase->z==0 ){. 
24e3d 20 20 20 70 44 61 74 61 62 61 73 65 20 3d 20 30     pDatabase = 0
24e3e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 61 74  ;.  }.  if( pDat
24e3f 61 62 61 73 65 20 29 7b 0a 20 20 20 20 54 6f 6b  abase ){.    Tok
24e40 65 6e 20 2a 70 54 65 6d 70 20 3d 20 70 44 61 74  en *pTemp = pDat
24e41 61 62 61 73 65 3b 0a 20 20 20 20 70 44 61 74 61  abase;.    pData
24e42 62 61 73 65 20 3d 20 70 54 61 62 6c 65 3b 0a 20  base = pTable;. 
24e43 20 20 20 70 54 61 62 6c 65 20 3d 20 70 54 65 6d     pTable = pTem
24e44 70 3b 0a 20 20 7d 0a 20 20 70 49 74 65 6d 2d 3e  p;.  }.  pItem->
24e45 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e  zName = sqlite3N
24e46 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
24e47 20 70 54 61 62 6c 65 29 3b 0a 20 20 70 49 74 65   pTable);.  pIte
24e48 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 73  m->zDatabase = s
24e49 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
24e4a 6b 65 6e 28 64 62 2c 20 70 44 61 74 61 62 61 73  ken(db, pDatabas
24e4b 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4c 69  e);.  return pLi
24e4c 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73  st;.}../*.** Ass
24e4d 69 67 6e 20 56 64 62 65 43 75 72 73 6f 72 20 69  ign VdbeCursor i
24e4e 6e 64 65 78 20 6e 75 6d 62 65 72 73 20 74 6f 20  ndex numbers to 
24e4f 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 61 20  all tables in a 
24e50 53 72 63 4c 69 73 74 0a 2a 2f 0a 53 51 4c 49 54  SrcList.*/.SQLIT
24e51 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
24e52 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 73 73  qlite3SrcListAss
24e53 69 67 6e 43 75 72 73 6f 72 73 28 50 61 72 73 65  ignCursors(Parse
24e54 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73   *pParse, SrcLis
24e55 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74  t *pList){.  int
24e56 20 69 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63   i;.  struct Src
24e57 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
24e58 3b 0a 20 20 61 73 73 65 72 74 28 70 4c 69 73 74  ;.  assert(pList
24e59 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e   || pParse->db->
24e5a 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
24e5b 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20    if( pList ){. 
24e5c 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65     for(i=0, pIte
24e5d 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c  m=pList->a; i<pL
24e5e 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20  ist->nSrc; i++, 
24e5f 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
24e60 69 66 28 20 70 49 74 65 6d 2d 3e 69 43 75 72 73  if( pItem->iCurs
24e61 6f 72 3e 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20  or>=0 ) break;. 
24e62 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 75 72       pItem->iCur
24e63 73 6f 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  sor = pParse->nT
24e64 61 62 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20  ab++;.      if( 
24e65 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29  pItem->pSelect )
24e66 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
24e67 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75  3SrcListAssignCu
24e68 72 73 6f 72 73 28 70 50 61 72 73 65 2c 20 70 49  rsors(pParse, pI
24e69 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2d 3e 70 53  tem->pSelect->pS
24e6a 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rc);.      }.   
24e6b 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
24e6c 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65  Delete an entire
24e6d 20 53 72 63 4c 69 73 74 20 69 6e 63 6c 75 64 69   SrcList includi
24e6e 6e 67 20 61 6c 6c 20 69 74 73 20 73 75 62 73 74  ng all its subst
24e6f 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49  ructure..*/.SQLI
24e70 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
24e71 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
24e72 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62  lete(sqlite3 *db
24e73 2c 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74  , SrcList *pList
24e74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  ){.  int i;.  st
24e75 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
24e76 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20  m *pItem;.  if( 
24e77 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72  pList==0 ) retur
24e78 6e 3b 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70  n;.  for(pItem=p
24e79 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c  List->a, i=0; i<
24e7a 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  pList->nSrc; i++
24e7b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
24e7c 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
24e7d 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61  , pItem->zDataba
24e7e 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  se);.    sqlite3
24e7f 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d  DbFree(db, pItem
24e80 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71  ->zName);.    sq
24e81 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
24e82 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a  pItem->zAlias);.
24e83 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
24e84 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 49 6e  e(db, pItem->zIn
24e85 64 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  dex);.    sqlite
24e86 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 49 74  3DeleteTable(pIt
24e87 65 6d 2d 3e 70 54 61 62 29 3b 0a 20 20 20 20 73  em->pTab);.    s
24e88 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
24e89 74 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 53  te(db, pItem->pS
24e8a 65 6c 65 63 74 29 3b 0a 20 20 20 20 73 71 6c 69  elect);.    sqli
24e8b 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
24e8c 2c 20 70 49 74 65 6d 2d 3e 70 4f 6e 29 3b 0a 20  , pItem->pOn);. 
24e8d 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74     sqlite3IdList
24e8e 44 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65 6d  Delete(db, pItem
24e8f 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20 7d 0a 20  ->pUsing);.  }. 
24e90 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
24e91 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a  b, pList);.}../*
24e92 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
24e93 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
24e94 65 20 70 61 72 73 65 72 20 74 6f 20 61 64 64 20  e parser to add 
24e95 61 20 6e 65 77 20 74 65 72 6d 20 74 6f 20 74 68  a new term to th
24e96 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 61 20 67 72  e.** end of a gr
24e97 6f 77 69 6e 67 20 46 52 4f 4d 20 63 6c 61 75 73  owing FROM claus
24e98 65 2e 20 20 54 68 65 20 22 70 22 20 70 61 72 61  e.  The "p" para
24e99 6d 65 74 65 72 20 69 73 20 74 68 65 20 70 61 72  meter is the par
24e9a 74 20 6f 66 0a 2a 2a 20 74 68 65 20 46 52 4f 4d  t of.** the FROM
24e9b 20 63 6c 61 75 73 65 20 74 68 61 74 20 68 61 73   clause that has
24e9c 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f   already been co
24e9d 6e 73 74 72 75 63 74 65 64 2e 20 20 22 70 22 20  nstructed.  "p" 
24e9e 69 73 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 74 68  is NULL.** if th
24e9f 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20  is is the first 
24ea0 74 65 72 6d 20 6f 66 20 74 68 65 20 46 52 4f 4d  term of the FROM
24ea1 20 63 6c 61 75 73 65 2e 20 20 70 54 61 62 6c 65   clause.  pTable
24ea2 20 61 6e 64 20 70 44 61 74 61 62 61 73 65 0a 2a   and pDatabase.*
24ea3 2a 20 61 72 65 20 74 68 65 20 6e 61 6d 65 20 6f  * are the name o
24ea4 66 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20  f the table and 
24ea5 64 61 74 61 62 61 73 65 20 6e 61 6d 65 64 20 69  database named i
24ea6 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
24ea7 65 20 74 65 72 6d 2e 0a 2a 2a 20 70 44 61 74 61  e term..** pData
24ea8 62 61 73 65 20 69 73 20 4e 55 4c 4c 20 69 66 20  base is NULL if 
24ea9 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  the database nam
24eaa 65 20 71 75 61 6c 69 66 69 65 72 20 69 73 20 6d  e qualifier is m
24eab 69 73 73 69 6e 67 20 2d 20 74 68 65 0a 2a 2a 20  issing - the.** 
24eac 75 73 75 61 6c 20 63 61 73 65 2e 20 20 49 66 20  usual case.  If 
24ead 74 68 65 20 74 65 72 6d 20 68 61 73 20 61 20 61  the term has a a
24eae 6c 69 61 73 2c 20 74 68 65 6e 20 70 41 6c 69 61  lias, then pAlia
24eaf 73 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a  s points to the.
24eb0 2a 2a 20 61 6c 69 61 73 20 74 6f 6b 65 6e 2e 20  ** alias token. 
24eb1 20 49 66 20 74 68 65 20 74 65 72 6d 20 69 73 20   If the term is 
24eb2 61 20 73 75 62 71 75 65 72 79 2c 20 74 68 65 6e  a subquery, then
24eb3 20 70 53 75 62 71 75 65 72 79 20 69 73 20 74 68   pSubquery is th
24eb4 65 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74  e.** SELECT stat
24eb5 65 6d 65 6e 74 20 74 68 61 74 20 74 68 65 20 73  ement that the s
24eb6 75 62 71 75 65 72 79 20 65 6e 63 6f 64 65 73 2e  ubquery encodes.
24eb7 20 20 54 68 65 20 70 54 61 62 6c 65 20 61 6e 64    The pTable and
24eb8 0a 2a 2a 20 70 44 61 74 61 62 61 73 65 20 70 61  .** pDatabase pa
24eb9 72 61 6d 65 74 65 72 73 20 61 72 65 20 4e 55 4c  rameters are NUL
24eba 4c 20 66 6f 72 20 73 75 62 71 75 65 72 69 65 73  L for subqueries
24ebb 2e 20 20 54 68 65 20 70 4f 6e 20 61 6e 64 20 70  .  The pOn and p
24ebc 55 73 69 6e 67 0a 2a 2a 20 70 61 72 61 6d 65 74  Using.** paramet
24ebd 65 72 73 20 61 72 65 20 74 68 65 20 63 6f 6e 74  ers are the cont
24ebe 65 6e 74 20 6f 66 20 74 68 65 20 4f 4e 20 61 6e  ent of the ON an
24ebf 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 2e  d USING clauses.
24ec0 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  .**.** Return a 
24ec1 6e 65 77 20 53 72 63 4c 69 73 74 20 77 68 69 63  new SrcList whic
24ec2 68 20 65 6e 63 6f 64 65 73 20 69 73 20 74 68 65  h encodes is the
24ec3 20 46 52 4f 4d 20 77 69 74 68 20 74 68 65 20 6e   FROM with the n
24ec4 65 77 0a 2a 2a 20 74 65 72 6d 20 61 64 64 65 64  ew.** term added
24ec5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
24ec6 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c  ATE SrcList *sql
24ec7 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
24ec8 64 46 72 6f 6d 54 65 72 6d 28 0a 20 20 50 61 72  dFromTerm(.  Par
24ec9 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
24eca 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
24ecb 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63  context */.  Src
24ecc 4c 69 73 74 20 2a 70 2c 20 20 20 20 20 20 20 20  List *p,        
24ecd 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74       /* The left
24ece 20 70 61 72 74 20 6f 66 20 74 68 65 20 46 52 4f   part of the FRO
24ecf 4d 20 63 6c 61 75 73 65 20 61 6c 72 65 61 64 79  M clause already
24ed0 20 73 65 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e   seen */.  Token
24ed1 20 2a 70 54 61 62 6c 65 2c 20 20 20 20 20 20 20   *pTable,       
24ed2 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
24ed3 65 20 74 61 62 6c 65 20 74 6f 20 61 64 64 20 74  e table to add t
24ed4 6f 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  o the FROM claus
24ed5 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 44  e */.  Token *pD
24ed6 61 74 61 62 61 73 65 2c 20 20 20 20 20 20 20 2f  atabase,       /
24ed7 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  * Name of the da
24ed8 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
24ed9 67 20 70 54 61 62 6c 65 20 2a 2f 0a 20 20 54 6f  g pTable */.  To
24eda 6b 65 6e 20 2a 70 41 6c 69 61 73 2c 20 20 20 20  ken *pAlias,    
24edb 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67        /* The rig
24edc 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20  ht-hand side of 
24edd 74 68 65 20 41 53 20 73 75 62 65 78 70 72 65 73  the AS subexpres
24ede 73 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74  sion */.  Select
24edf 20 2a 70 53 75 62 71 75 65 72 79 2c 20 20 20 20   *pSubquery,    
24ee0 20 20 2f 2a 20 41 20 73 75 62 71 75 65 72 79 20    /* A subquery 
24ee1 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66  used in place of
24ee2 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f   a table name */
24ee3 0a 20 20 45 78 70 72 20 2a 70 4f 6e 2c 20 20 20  .  Expr *pOn,   
24ee4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
24ee5 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61  e ON clause of a
24ee6 20 6a 6f 69 6e 20 2a 2f 0a 20 20 49 64 4c 69 73   join */.  IdLis
24ee7 74 20 2a 70 55 73 69 6e 67 20 20 20 20 20 20 20  t *pUsing       
24ee8 20 20 20 2f 2a 20 54 68 65 20 55 53 49 4e 47 20     /* The USING 
24ee9 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e  clause of a join
24eea 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20   */.){.  struct 
24eeb 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
24eec 74 65 6d 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  tem;.  sqlite3 *
24eed 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
24eee 0a 20 20 69 66 28 20 21 70 20 26 26 20 28 70 4f  .  if( !p && (pO
24eef 6e 20 7c 7c 20 70 55 73 69 6e 67 29 20 29 7b 0a  n || pUsing) ){.
24ef0 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
24ef1 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 20 4a  Msg(pParse, "a J
24ef2 4f 49 4e 20 63 6c 61 75 73 65 20 69 73 20 72 65  OIN clause is re
24ef3 71 75 69 72 65 64 20 62 65 66 6f 72 65 20 25 73  quired before %s
24ef4 22 2c 20 0a 20 20 20 20 20 20 28 70 4f 6e 20 3f  ", .      (pOn ?
24ef5 20 22 4f 4e 22 20 3a 20 22 55 53 49 4e 47 22 29   "ON" : "USING")
24ef6 0a 20 20 20 20 29 3b 0a 20 20 20 20 67 6f 74 6f  .    );.    goto
24ef7 20 61 70 70 65 6e 64 5f 66 72 6f 6d 5f 65 72 72   append_from_err
24ef8 6f 72 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 73 71  or;.  }.  p = sq
24ef9 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
24efa 6e 64 28 64 62 2c 20 70 2c 20 70 54 61 62 6c 65  nd(db, p, pTable
24efb 2c 20 70 44 61 74 61 62 61 73 65 29 3b 0a 20 20  , pDatabase);.  
24efc 69 66 28 20 70 3d 3d 30 20 7c 7c 20 4e 45 56 45  if( p==0 || NEVE
24efd 52 28 70 2d 3e 6e 53 72 63 3d 3d 30 29 20 29 7b  R(p->nSrc==0) ){
24efe 0a 20 20 20 20 67 6f 74 6f 20 61 70 70 65 6e 64  .    goto append
24eff 5f 66 72 6f 6d 5f 65 72 72 6f 72 3b 0a 20 20 7d  _from_error;.  }
24f00 0a 20 20 70 49 74 65 6d 20 3d 20 26 70 2d 3e 61  .  pItem = &p->a
24f01 5b 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 61  [p->nSrc-1];.  a
24f02 73 73 65 72 74 28 20 70 41 6c 69 61 73 21 3d 30  ssert( pAlias!=0
24f03 20 29 3b 0a 20 20 69 66 28 20 70 41 6c 69 61 73   );.  if( pAlias
24f04 2d 3e 6e 20 29 7b 0a 20 20 20 20 70 49 74 65 6d  ->n ){.    pItem
24f05 2d 3e 7a 41 6c 69 61 73 20 3d 20 73 71 6c 69 74  ->zAlias = sqlit
24f06 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
24f07 64 62 2c 20 70 41 6c 69 61 73 29 3b 0a 20 20 7d  db, pAlias);.  }
24f08 0a 20 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63  .  pItem->pSelec
24f09 74 20 3d 20 70 53 75 62 71 75 65 72 79 3b 0a 20  t = pSubquery;. 
24f0a 20 70 49 74 65 6d 2d 3e 70 4f 6e 20 3d 20 70 4f   pItem->pOn = pO
24f0b 6e 3b 0a 20 20 70 49 74 65 6d 2d 3e 70 55 73 69  n;.  pItem->pUsi
24f0c 6e 67 20 3d 20 70 55 73 69 6e 67 3b 0a 20 20 72  ng = pUsing;.  r
24f0d 65 74 75 72 6e 20 70 3b 0a 0a 20 61 70 70 65 6e  eturn p;.. appen
24f0e 64 5f 66 72 6f 6d 5f 65 72 72 6f 72 3a 0a 20 20  d_from_error:.  
24f0f 61 73 73 65 72 74 28 20 70 3d 3d 30 20 29 3b 0a  assert( p==0 );.
24f10 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
24f11 65 74 65 28 64 62 2c 20 70 4f 6e 29 3b 0a 20 20  ete(db, pOn);.  
24f12 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c  sqlite3IdListDel
24f13 65 74 65 28 64 62 2c 20 70 55 73 69 6e 67 29 3b  ete(db, pUsing);
24f14 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
24f15 44 65 6c 65 74 65 28 64 62 2c 20 70 53 75 62 71  Delete(db, pSubq
24f16 75 65 72 79 29 3b 0a 20 20 72 65 74 75 72 6e 20  uery);.  return 
24f17 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20  0;.}../*.** Add 
24f18 61 6e 20 49 4e 44 45 58 45 44 20 42 59 20 6f 72  an INDEXED BY or
24f19 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c 61   NOT INDEXED cla
24f1a 75 73 65 20 74 6f 20 74 68 65 20 6d 6f 73 74 20  use to the most 
24f1b 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 0a  recently added .
24f1c 2a 2a 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68  ** element of th
24f1d 65 20 73 6f 75 72 63 65 2d 6c 69 73 74 20 70 61  e source-list pa
24f1e 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f  ssed as the seco
24f1f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a  nd argument..*/.
24f20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
24f21 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69  oid sqlite3SrcLi
24f22 73 74 49 6e 64 65 78 65 64 42 79 28 50 61 72 73  stIndexedBy(Pars
24f23 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69  e *pParse, SrcLi
24f24 73 74 20 2a 70 2c 20 54 6f 6b 65 6e 20 2a 70 49  st *p, Token *pI
24f25 6e 64 65 78 65 64 42 79 29 7b 0a 20 20 61 73 73  ndexedBy){.  ass
24f26 65 72 74 28 20 70 49 6e 64 65 78 65 64 42 79 21  ert( pIndexedBy!
24f27 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 20 26 26  =0 );.  if( p &&
24f28 20 41 4c 57 41 59 53 28 70 2d 3e 6e 53 72 63 3e   ALWAYS(p->nSrc>
24f29 30 29 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74  0) ){.    struct
24f2a 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
24f2b 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 70 2d 3e  Item = &p->a[p->
24f2c 6e 53 72 63 2d 31 5d 3b 0a 20 20 20 20 61 73 73  nSrc-1];.    ass
24f2d 65 72 74 28 20 70 49 74 65 6d 2d 3e 6e 6f 74 49  ert( pItem->notI
24f2e 6e 64 65 78 65 64 3d 3d 30 20 26 26 20 70 49 74  ndexed==0 && pIt
24f2f 65 6d 2d 3e 7a 49 6e 64 65 78 3d 3d 30 20 29 3b  em->zIndex==0 );
24f30 0a 20 20 20 20 69 66 28 20 70 49 6e 64 65 78 65  .    if( pIndexe
24f31 64 42 79 2d 3e 6e 3d 3d 31 20 26 26 20 21 70 49  dBy->n==1 && !pI
24f32 6e 64 65 78 65 64 42 79 2d 3e 7a 20 29 7b 0a 20  ndexedBy->z ){. 
24f33 20 20 20 20 20 2f 2a 20 41 20 22 4e 4f 54 20 49       /* A "NOT I
24f34 4e 44 45 58 45 44 22 20 63 6c 61 75 73 65 20 77  NDEXED" clause w
24f35 61 73 20 73 75 70 70 6c 69 65 64 2e 20 53 65 65  as supplied. See
24f36 20 70 61 72 73 65 2e 79 20 0a 20 20 20 20 20 20   parse.y .      
24f37 2a 2a 20 63 6f 6e 73 74 72 75 63 74 20 22 69 6e  ** construct "in
24f38 64 65 78 65 64 5f 6f 70 74 22 20 66 6f 72 20 64  dexed_opt" for d
24f39 65 74 61 69 6c 73 2e 20 2a 2f 0a 20 20 20 20 20  etails. */.     
24f3a 20 70 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78   pItem->notIndex
24f3b 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73  ed = 1;.    }els
24f3c 65 7b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e  e{.      pItem->
24f3d 7a 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33  zIndex = sqlite3
24f3e 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70 50  NameFromToken(pP
24f3f 61 72 73 65 2d 3e 64 62 2c 20 70 49 6e 64 65 78  arse->db, pIndex
24f40 65 64 42 79 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  edBy);.    }.  }
24f41 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 62  .}../*.** When b
24f42 75 69 6c 64 69 6e 67 20 75 70 20 61 20 46 52 4f  uilding up a FRO
24f43 4d 20 63 6c 61 75 73 65 20 69 6e 20 74 68 65 20  M clause in the 
24f44 70 61 72 73 65 72 2c 20 74 68 65 20 6a 6f 69 6e  parser, the join
24f45 20 6f 70 65 72 61 74 6f 72 0a 2a 2a 20 69 73 20   operator.** is 
24f46 69 6e 69 74 69 61 6c 6c 79 20 61 74 74 61 63 68  initially attach
24f47 65 64 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f  ed to the left o
24f48 70 65 72 61 6e 64 2e 20 20 42 75 74 20 74 68 65  perand.  But the
24f49 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 0a   code generator.
24f4a 2a 2a 20 65 78 70 65 63 74 73 20 74 68 65 20 6a  ** expects the j
24f4b 6f 69 6e 20 6f 70 65 72 61 74 6f 72 20 74 6f 20  oin operator to 
24f4c 62 65 20 6f 6e 20 74 68 65 20 72 69 67 68 74 20  be on the right 
24f4d 6f 70 65 72 61 6e 64 2e 20 20 54 68 69 73 20 72  operand.  This r
24f4e 6f 75 74 69 6e 65 0a 2a 2a 20 53 68 69 66 74 73  outine.** Shifts
24f4f 20 61 6c 6c 20 6a 6f 69 6e 20 6f 70 65 72 61 74   all join operat
24f50 6f 72 73 20 66 72 6f 6d 20 6c 65 66 74 20 74 6f  ors from left to
24f51 20 72 69 67 68 74 20 66 6f 72 20 61 6e 20 65 6e   right for an en
24f52 74 69 72 65 20 46 52 4f 4d 0a 2a 2a 20 63 6c 61  tire FROM.** cla
24f53 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70  use..**.** Examp
24f54 6c 65 3a 20 53 75 70 70 6f 73 65 20 74 68 65 20  le: Suppose the 
24f55 6a 6f 69 6e 20 69 73 20 6c 69 6b 65 20 74 68 69  join is like thi
24f56 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  s:.**.**        
24f57 20 20 20 41 20 6e 61 74 75 72 61 6c 20 63 72 6f     A natural cro
24f58 73 73 20 6a 6f 69 6e 20 42 0a 2a 2a 0a 2a 2a 20  ss join B.**.** 
24f59 54 68 65 20 6f 70 65 72 61 74 6f 72 20 69 73 20  The operator is 
24f5a 22 6e 61 74 75 72 61 6c 20 63 72 6f 73 73 20 6a  "natural cross j
24f5b 6f 69 6e 22 2e 20 20 54 68 65 20 41 20 61 6e 64  oin".  The A and
24f5c 20 42 20 6f 70 65 72 61 6e 64 73 20 61 72 65 20   B operands are 
24f5d 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 70 2d 3e  stored.** in p->
24f5e 61 5b 30 5d 20 61 6e 64 20 70 2d 3e 61 5b 31 5d  a[0] and p->a[1]
24f5f 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20  , respectively. 
24f60 20 54 68 65 20 70 61 72 73 65 72 20 69 6e 69 74   The parser init
24f61 69 61 6c 6c 79 20 73 74 6f 72 65 73 20 74 68 65  ially stores the
24f62 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 77 69 74  .** operator wit
24f63 68 20 41 2e 20 20 54 68 69 73 20 72 6f 75 74 69  h A.  This routi
24f64 6e 65 20 73 68 69 66 74 73 20 74 68 61 74 20 6f  ne shifts that o
24f65 70 65 72 61 74 6f 72 20 6f 76 65 72 20 74 6f 20  perator over to 
24f66 42 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  B..*/.SQLITE_PRI
24f67 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
24f68 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69  3SrcListShiftJoi
24f69 6e 54 79 70 65 28 53 72 63 4c 69 73 74 20 2a 70  nType(SrcList *p
24f6a 29 7b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d  ){.  if( p && p-
24f6b 3e 61 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  >a ){.    int i;
24f6c 0a 20 20 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 53  .    for(i=p->nS
24f6d 72 63 2d 31 3b 20 69 3e 30 3b 20 69 2d 2d 29 7b  rc-1; i>0; i--){
24f6e 0a 20 20 20 20 20 20 70 2d 3e 61 5b 69 5d 2e 6a  .      p->a[i].j
24f6f 6f 69 6e 74 79 70 65 20 3d 20 70 2d 3e 61 5b 69  ointype = p->a[i
24f70 2d 31 5d 2e 6a 6f 69 6e 74 79 70 65 3b 0a 20 20  -1].jointype;.  
24f71 20 20 7d 0a 20 20 20 20 70 2d 3e 61 5b 30 5d 2e    }.    p->a[0].
24f72 6a 6f 69 6e 74 79 70 65 20 3d 20 30 3b 0a 20 20  jointype = 0;.  
24f73 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e  }.}../*.** Begin
24f74 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a   a transaction.*
24f75 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
24f76 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67   void sqlite3Beg
24f77 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61  inTransaction(Pa
24f78 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
24f79 20 74 79 70 65 29 7b 0a 20 20 73 71 6c 69 74 65   type){.  sqlite
24f7a 33 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76  3 *db;.  Vdbe *v
24f7b 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73  ;.  int i;..  as
24f7c 73 65 72 74 28 20 70 50 61 72 73 65 21 3d 30 20  sert( pParse!=0 
24f7d 29 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  );.  db = pParse
24f7e 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20  ->db;.  assert( 
24f7f 64 62 21 3d 30 20 29 3b 0a 2f 2a 20 20 69 66 28  db!=0 );./*  if(
24f80 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3d   db->aDb[0].pBt=
24f81 3d 30 20 29 20 72 65 74 75 72 6e 3b 20 2a 2f 0a  =0 ) return; */.
24f82 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
24f83 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
24f84 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f  QLITE_TRANSACTIO
24f85 4e 2c 20 22 42 45 47 49 4e 22 2c 20 30 2c 20 30  N, "BEGIN", 0, 0
24f86 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ) ){.    return;
24f87 0a 20 20 7d 0a 20 20 76 20 3d 20 73 71 6c 69 74  .  }.  v = sqlit
24f88 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
24f89 29 3b 0a 20 20 69 66 28 20 21 76 20 29 20 72 65  );.  if( !v ) re
24f8a 74 75 72 6e 3b 0a 20 20 69 66 28 20 74 79 70 65  turn;.  if( type
24f8b 21 3d 54 4b 5f 44 45 46 45 52 52 45 44 20 29 7b  !=TK_DEFERRED ){
24f8c 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
24f8d 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
24f8e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
24f8f 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 54 72 61  AddOp2(v, OP_Tra
24f90 6e 73 61 63 74 69 6f 6e 2c 20 69 2c 20 28 74 79  nsaction, i, (ty
24f91 70 65 3d 3d 54 4b 5f 45 58 43 4c 55 53 49 56 45  pe==TK_EXCLUSIVE
24f92 29 2b 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  )+1);.      sqli
24f93 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65  te3VdbeUsesBtree
24f94 28 76 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20  (v, i);.    }.  
24f95 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  }.  sqlite3VdbeA
24f96 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 75 74 6f  ddOp2(v, OP_Auto
24f97 43 6f 6d 6d 69 74 2c 20 30 2c 20 30 29 3b 0a 7d  Commit, 0, 0);.}
24f98 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 61  ../*.** Commit a
24f99 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a   transaction.*/.
24f9a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
24f9b 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69  oid sqlite3Commi
24f9c 74 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72  tTransaction(Par
24f9d 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73  se *pParse){.  s
24f9e 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64  qlite3 *db;.  Vd
24f9f 62 65 20 2a 76 3b 0a 0a 20 20 61 73 73 65 72 74  be *v;..  assert
24fa0 28 20 70 50 61 72 73 65 21 3d 30 20 29 3b 0a 20  ( pParse!=0 );. 
24fa1 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
24fa2 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d  ;.  assert( db!=
24fa3 30 20 29 3b 0a 2f 2a 20 20 69 66 28 20 64 62 2d  0 );./*  if( db-
24fa4 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 29  >aDb[0].pBt==0 )
24fa5 20 72 65 74 75 72 6e 3b 20 2a 2f 0a 20 20 69 66   return; */.  if
24fa6 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  ( sqlite3AuthChe
24fa7 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ck(pParse, SQLIT
24fa8 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22  E_TRANSACTION, "
24fa9 43 4f 4d 4d 49 54 22 2c 20 30 2c 20 30 29 20 29  COMMIT", 0, 0) )
24faa 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
24fab 7d 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  }.  v = sqlite3G
24fac 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
24fad 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73    if( v ){.    s
24fae 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
24faf 28 76 2c 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69  (v, OP_AutoCommi
24fb0 74 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a  t, 1, 0);.  }.}.
24fb1 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20  ./*.** Rollback 
24fb2 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f  a transaction.*/
24fb3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
24fb4 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c  void sqlite3Roll
24fb5 62 61 63 6b 54 72 61 6e 73 61 63 74 69 6f 6e 28  backTransaction(
24fb6 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a  Parse *pParse){.
24fb7 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20    sqlite3 *db;. 
24fb8 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 61 73 73   Vdbe *v;..  ass
24fb9 65 72 74 28 20 70 50 61 72 73 65 21 3d 30 20 29  ert( pParse!=0 )
24fba 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ;.  db = pParse-
24fbb 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 64  >db;.  assert( d
24fbc 62 21 3d 30 20 29 3b 0a 2f 2a 20 20 69 66 28 20  b!=0 );./*  if( 
24fbd 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d  db->aDb[0].pBt==
24fbe 30 20 29 20 72 65 74 75 72 6e 3b 20 2a 2f 0a 20  0 ) return; */. 
24fbf 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68   if( sqlite3Auth
24fc0 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
24fc1 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e  LITE_TRANSACTION
24fc2 2c 20 22 52 4f 4c 4c 42 41 43 4b 22 2c 20 30 2c  , "ROLLBACK", 0,
24fc3 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72   0) ){.    retur
24fc4 6e 3b 0a 20 20 7d 0a 20 20 76 20 3d 20 73 71 6c  n;.  }.  v = sql
24fc5 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
24fc6 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a  se);.  if( v ){.
24fc7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24fc8 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 75 74 6f  ddOp2(v, OP_Auto
24fc9 43 6f 6d 6d 69 74 2c 20 31 2c 20 31 29 3b 0a 20  Commit, 1, 1);. 
24fca 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
24fcb 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
24fcc 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65  led by the parse
24fcd 72 20 77 68 65 6e 20 69 74 20 70 61 72 73 65 73  r when it parses
24fce 20 61 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 63 72   a command to cr
24fcf 65 61 74 65 2c 0a 2a 2a 20 72 65 6c 65 61 73 65  eate,.** release
24fd0 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 20   or rollback an 
24fd1 53 51 4c 20 73 61 76 65 70 6f 69 6e 74 2e 20 0a  SQL savepoint. .
24fd2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
24fd3 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 61  E void sqlite3Sa
24fd4 76 65 70 6f 69 6e 74 28 50 61 72 73 65 20 2a 70  vepoint(Parse *p
24fd5 50 61 72 73 65 2c 20 69 6e 74 20 6f 70 2c 20 54  Parse, int op, T
24fd6 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20  oken *pName){.  
24fd7 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 73 71  char *zName = sq
24fd8 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
24fd9 65 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  en(pParse->db, p
24fda 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4e 61  Name);.  if( zNa
24fdb 6d 65 20 29 7b 0a 20 20 20 20 56 64 62 65 20 2a  me ){.    Vdbe *
24fdc 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
24fdd 62 65 28 70 50 61 72 73 65 29 3b 0a 23 69 66 6e  be(pParse);.#ifn
24fde 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
24fdf 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
24fe0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
24fe1 68 61 72 20 2a 61 7a 5b 5d 20 3d 20 7b 20 22 42  har *az[] = { "B
24fe2 45 47 49 4e 22 2c 20 22 52 45 4c 45 41 53 45 22  EGIN", "RELEASE"
24fe3 2c 20 22 52 4f 4c 4c 42 41 43 4b 22 20 7d 3b 0a  , "ROLLBACK" };.
24fe4 20 20 20 20 61 73 73 65 72 74 28 20 21 53 41 56      assert( !SAV
24fe5 45 50 4f 49 4e 54 5f 42 45 47 49 4e 20 26 26 20  EPOINT_BEGIN && 
24fe6 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53  SAVEPOINT_RELEAS
24fe7 45 3d 3d 31 20 26 26 20 53 41 56 45 50 4f 49 4e  E==1 && SAVEPOIN
24fe8 54 5f 52 4f 4c 4c 42 41 43 4b 3d 3d 32 20 29 3b  T_ROLLBACK==2 );
24fe9 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20  .#endif.    if( 
24fea 21 76 20 7c 7c 20 73 71 6c 69 74 65 33 41 75 74  !v || sqlite3Aut
24feb 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
24fec 51 4c 49 54 45 5f 53 41 56 45 50 4f 49 4e 54 2c  QLITE_SAVEPOINT,
24fed 20 61 7a 5b 6f 70 5d 2c 20 7a 4e 61 6d 65 2c 20   az[op], zName, 
24fee 30 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  0) ){.      sqli
24fef 74 65 33 44 62 46 72 65 65 28 70 50 61 72 73 65  te3DbFree(pParse
24ff0 2d 3e 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  ->db, zName);.  
24ff1 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
24ff2 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
24ff3 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 61  eAddOp4(v, OP_Sa
24ff4 76 65 70 6f 69 6e 74 2c 20 6f 70 2c 20 30 2c 20  vepoint, op, 0, 
24ff5 30 2c 20 7a 4e 61 6d 65 2c 20 50 34 5f 44 59 4e  0, zName, P4_DYN
24ff6 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  AMIC);.  }.}../*
24ff7 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68  .** Make sure th
24ff8 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 20  e TEMP database 
24ff9 69 73 20 6f 70 65 6e 20 61 6e 64 20 61 76 61 69  is open and avai
24ffa 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 2e 20 20  lable for use.  
24ffb 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75  Return.** the nu
24ffc 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 20  mber of errors. 
24ffd 20 4c 65 61 76 65 20 61 6e 79 20 65 72 72 6f 72   Leave any error
24ffe 20 6d 65 73 73 61 67 65 73 20 69 6e 20 74 68 65   messages in the
24fff 20 70 50 61 72 73 65 20 73 74 72 75 63 74 75 72   pParse structur
25000 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
25001 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
25002 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65  OpenTempDatabase
25003 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b  (Parse *pParse){
25004 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
25005 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
25006 66 28 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42  f( db->aDb[1].pB
25007 74 3d 3d 30 20 26 26 20 21 70 50 61 72 73 65 2d  t==0 && !pParse-
25008 3e 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20  >explain ){.    
25009 69 6e 74 20 72 63 3b 0a 20 20 20 20 73 74 61 74  int rc;.    stat
2500a 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 66 6c 61  ic const int fla
2500b 67 73 20 3d 20 0a 20 20 20 20 20 20 20 20 20 20  gs = .          
2500c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
2500d 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20 20 20  WRITE |.        
2500e 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52    SQLITE_OPEN_CR
2500f 45 41 54 45 20 7c 0a 20 20 20 20 20 20 20 20 20  EATE |.         
25010 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43   SQLITE_OPEN_EXC
25011 4c 55 53 49 56 45 20 7c 0a 20 20 20 20 20 20 20  LUSIVE |.       
25012 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44     SQLITE_OPEN_D
25013 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a 20  ELETEONCLOSE |. 
25014 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
25015 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 0a 20  OPEN_TEMP_DB;.. 
25016 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
25017 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20  treeFactory(db, 
25018 30 2c 20 30 2c 20 53 51 4c 49 54 45 5f 44 45 46  0, 0, SQLITE_DEF
25019 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c  AULT_CACHE_SIZE,
2501a 20 66 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20   flags,.        
2501b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2501c 20 20 20 20 20 20 20 20 20 26 64 62 2d 3e 61 44           &db->aD
2501d 62 5b 31 5d 2e 70 42 74 29 3b 0a 20 20 20 20 69  b[1].pBt);.    i
2501e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2501f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
25020 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
25021 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65  , "unable to ope
25022 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20 64 61  n a temporary da
25023 74 61 62 61 73 65 20 22 0a 20 20 20 20 20 20 20  tabase ".       
25024 20 22 66 69 6c 65 20 66 6f 72 20 73 74 6f 72 69   "file for stori
25025 6e 67 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  ng temporary tab
25026 6c 65 73 22 29 3b 0a 20 20 20 20 20 20 70 50 61  les");.      pPa
25027 72 73 65 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20  rse->rc = rc;.  
25028 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
25029 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
2502a 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  (db->flags & SQL
2502b 49 54 45 5f 49 6e 54 72 61 6e 73 29 3d 3d 30 20  ITE_InTrans)==0 
2502c 7c 7c 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  || db->autoCommi
2502d 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  t );.    assert(
2502e 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68   db->aDb[1].pSch
2502f 65 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ema );.    sqlit
25030 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f  e3PagerJournalMo
25031 64 65 28 73 71 6c 69 74 65 33 42 74 72 65 65 50  de(sqlite3BtreeP
25032 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 31 5d 2e  ager(db->aDb[1].
25033 70 42 74 29 2c 0a 20 20 20 20 20 20 20 20 20 20  pBt),.          
25034 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25035 20 20 64 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61    db->dfltJourna
25036 6c 4d 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 72 65  lMode);.  }.  re
25037 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
25038 20 47 65 6e 65 72 61 74 65 20 56 44 42 45 20 63   Generate VDBE c
25039 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 76 65  ode that will ve
2503a 72 69 66 79 20 74 68 65 20 73 63 68 65 6d 61 20  rify the schema 
2503b 63 6f 6f 6b 69 65 20 61 6e 64 20 73 74 61 72 74  cookie and start
2503c 0a 2a 2a 20 61 20 72 65 61 64 2d 74 72 61 6e 73  .** a read-trans
2503d 61 63 74 69 6f 6e 20 66 6f 72 20 61 6c 6c 20 6e  action for all n
2503e 61 6d 65 64 20 64 61 74 61 62 61 73 65 20 66 69  amed database fi
2503f 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73  les..**.** It is
25040 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20   important that 
25041 61 6c 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  all schema cooki
25042 65 73 20 62 65 20 76 65 72 69 66 69 65 64 20 61  es be verified a
25043 6e 64 20 61 6c 6c 0a 2a 2a 20 72 65 61 64 20 74  nd all.** read t
25044 72 61 6e 73 61 63 74 69 6f 6e 73 20 62 65 20 73  ransactions be s
25045 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 6e  tarted before an
25046 79 74 68 69 6e 67 20 65 6c 73 65 20 68 61 70 70  ything else happ
25047 65 6e 73 20 69 6e 0a 2a 2a 20 74 68 65 20 56 44  ens in.** the VD
25048 42 45 20 70 72 6f 67 72 61 6d 2e 20 20 42 75 74  BE program.  But
25049 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   this routine ca
2504a 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 66 74 65  n be called afte
2504b 72 20 6d 75 63 68 20 6f 74 68 65 72 0a 2a 2a 20  r much other.** 
2504c 63 6f 64 65 20 68 61 73 20 62 65 65 6e 20 67 65  code has been ge
2504d 6e 65 72 61 74 65 64 2e 20 20 53 6f 20 68 65 72  nerated.  So her
2504e 65 20 69 73 20 77 68 61 74 20 77 65 20 64 6f 3a  e is what we do:
2504f 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
25050 20 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74 69   time this routi
25051 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 77 65  ne is called, we
25052 20 63 6f 64 65 20 61 6e 20 4f 50 5f 47 6f 74 6f   code an OP_Goto
25053 20 74 68 61 74 0a 2a 2a 20 77 69 6c 6c 20 6a 75   that.** will ju
25054 6d 70 20 74 6f 20 61 20 73 75 62 72 6f 75 74 69  mp to a subrouti
25055 6e 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  ne at the end of
25056 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 54   the program.  T
25057 68 65 6e 20 77 65 0a 2a 2a 20 72 65 63 6f 72 64  hen we.** record
25058 20 65 76 65 72 79 20 64 61 74 61 62 61 73 65 20   every database 
25059 74 68 61 74 20 6e 65 65 64 73 20 69 74 73 20 73  that needs its s
2505a 63 68 65 6d 61 20 76 65 72 69 66 69 65 64 20 69  chema verified i
2505b 6e 20 74 68 65 0a 2a 2a 20 70 50 61 72 73 65 2d  n the.** pParse-
2505c 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 66 69 65 6c  >cookieMask fiel
2505d 64 2e 20 20 4c 61 74 65 72 2c 20 61 66 74 65 72  d.  Later, after
2505e 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 20   all other code 
2505f 68 61 73 20 62 65 65 6e 0a 2a 2a 20 67 65 6e 65  has been.** gene
25060 72 61 74 65 64 2c 20 74 68 65 20 73 75 62 72 6f  rated, the subro
25061 75 74 69 6e 65 20 74 68 61 74 20 64 6f 65 73 20  utine that does 
25062 74 68 65 20 63 6f 6f 6b 69 65 20 76 65 72 69 66  the cookie verif
25063 69 63 61 74 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20  ications and.** 
25064 73 74 61 72 74 73 20 74 68 65 20 74 72 61 6e 73  starts the trans
25065 61 63 74 69 6f 6e 73 20 77 69 6c 6c 20 62 65 20  actions will be 
25066 63 6f 64 65 64 20 61 6e 64 20 74 68 65 20 4f 50  coded and the OP
25067 5f 47 6f 74 6f 20 50 32 20 76 61 6c 75 65 0a 2a  _Goto P2 value.*
25068 2a 20 77 69 6c 6c 20 62 65 20 6d 61 64 65 20 74  * will be made t
25069 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20  o point to that 
2506a 73 75 62 72 6f 75 74 69 6e 65 2e 20 20 54 68 65  subroutine.  The
2506b 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20 74   generation of t
2506c 68 65 0a 2a 2a 20 63 6f 6f 6b 69 65 20 76 65 72  he.** cookie ver
2506d 69 66 69 63 61 74 69 6f 6e 20 73 75 62 72 6f 75  ification subrou
2506e 74 69 6e 65 20 63 6f 64 65 20 68 61 70 70 65 6e  tine code happen
2506f 73 20 69 6e 20 73 71 6c 69 74 65 33 46 69 6e 69  s in sqlite3Fini
25070 73 68 43 6f 64 69 6e 67 28 29 2e 0a 2a 2a 0a 2a  shCoding()..**.*
25071 2a 20 49 66 20 69 44 62 3c 30 20 74 68 65 6e 20  * If iDb<0 then 
25072 63 6f 64 65 20 74 68 65 20 4f 50 5f 47 6f 74 6f  code the OP_Goto
25073 20 6f 6e 6c 79 20 2d 20 64 6f 6e 27 74 20 73 65   only - don't se
25074 74 20 66 6c 61 67 20 74 6f 20 76 65 72 69 66 79  t flag to verify
25075 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 6f   the.** schema o
25076 6e 20 61 6e 79 20 64 61 74 61 62 61 73 65 73 2e  n any databases.
25077 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73    This can be us
25078 65 64 20 74 6f 20 70 6f 73 69 74 69 6f 6e 20 74  ed to position t
25079 68 65 20 4f 50 5f 47 6f 74 6f 0a 2a 2a 20 65 61  he OP_Goto.** ea
2507a 72 6c 79 20 69 6e 20 74 68 65 20 63 6f 64 65 2c  rly in the code,
2507b 20 62 65 66 6f 72 65 20 77 65 20 6b 6e 6f 77 20   before we know 
2507c 69 66 20 61 6e 79 20 64 61 74 61 62 61 73 65 20  if any database 
2507d 74 61 62 6c 65 73 20 77 69 6c 6c 20 62 65 20 75  tables will be u
2507e 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  sed..*/.SQLITE_P
2507f 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
25080 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
25081 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72 73  ema(Parse *pPars
25082 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 50  e, int iDb){.  P
25083 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20  arse *pToplevel 
25084 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f  = sqlite3ParseTo
25085 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0a  plevel(pParse);.
25086 0a 20 20 69 66 28 20 70 54 6f 70 6c 65 76 65 6c  .  if( pToplevel
25087 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 3d 3d 30 20  ->cookieGoto==0 
25088 29 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d  ){.    Vdbe *v =
25089 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
2508a 70 54 6f 70 6c 65 76 65 6c 29 3b 0a 20 20 20 20  pToplevel);.    
2508b 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72  if( v==0 ) retur
2508c 6e 3b 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79  n;  /* This only
2508d 20 68 61 70 70 65 6e 73 20 69 66 20 74 68 65 72   happens if ther
2508e 65 20 77 61 73 20 61 20 70 72 69 6f 72 20 65 72  e was a prior er
2508f 72 6f 72 20 2a 2f 0a 20 20 20 20 70 54 6f 70 6c  ror */.    pTopl
25090 65 76 65 6c 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f  evel->cookieGoto
25091 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
25092 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
25093 20 30 2c 20 30 29 2b 31 3b 0a 20 20 7d 0a 20 20   0, 0)+1;.  }.  
25094 69 66 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20  if( iDb>=0 ){.  
25095 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
25096 70 54 6f 70 6c 65 76 65 6c 2d 3e 64 62 3b 0a 20  pToplevel->db;. 
25097 20 20 20 69 6e 74 20 6d 61 73 6b 3b 0a 0a 20 20     int mask;..  
25098 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 64 62    assert( iDb<db
25099 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 20 61 73 73  ->nDb );.    ass
2509a 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62  ert( db->aDb[iDb
2509b 5d 2e 70 42 74 21 3d 30 20 7c 7c 20 69 44 62 3d  ].pBt!=0 || iDb=
2509c 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =1 );.    assert
2509d 28 20 69 44 62 3c 53 51 4c 49 54 45 5f 4d 41 58  ( iDb<SQLITE_MAX
2509e 5f 41 54 54 41 43 48 45 44 2b 32 20 29 3b 0a 20  _ATTACHED+2 );. 
2509f 20 20 20 6d 61 73 6b 20 3d 20 31 3c 3c 69 44 62     mask = 1<<iDb
250a0 3b 0a 20 20 20 20 69 66 28 20 28 70 54 6f 70 6c  ;.    if( (pTopl
250a1 65 76 65 6c 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b  evel->cookieMask
250a2 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 7b 0a 20   & mask)==0 ){. 
250a3 20 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e       pToplevel->
250a4 63 6f 6f 6b 69 65 4d 61 73 6b 20 7c 3d 20 6d 61  cookieMask |= ma
250a5 73 6b 3b 0a 20 20 20 20 20 20 70 54 6f 70 6c 65  sk;.      pTople
250a6 76 65 6c 2d 3e 63 6f 6f 6b 69 65 56 61 6c 75 65  vel->cookieValue
250a7 5b 69 44 62 5d 20 3d 20 64 62 2d 3e 61 44 62 5b  [iDb] = db->aDb[
250a8 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 63  iDb].pSchema->sc
250a9 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b 0a 20 20 20  hema_cookie;.   
250aa 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d     if( !OMIT_TEM
250ab 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 7b  PDB && iDb==1 ){
250ac 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
250ad 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65  OpenTempDatabase
250ae 28 70 54 6f 70 6c 65 76 65 6c 29 3b 0a 20 20 20  (pToplevel);.   
250af 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d     }.    }.  }.}
250b0 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
250b1 20 56 44 42 45 20 63 6f 64 65 20 74 68 61 74 20   VDBE code that 
250b2 70 72 65 70 61 72 65 73 20 66 6f 72 20 64 6f 69  prepares for doi
250b3 6e 67 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 20  ng an operation 
250b4 74 68 61 74 0a 2a 2a 20 6d 69 67 68 74 20 63 68  that.** might ch
250b5 61 6e 67 65 20 74 68 65 20 64 61 74 61 62 61 73  ange the databas
250b6 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
250b7 75 74 69 6e 65 20 73 74 61 72 74 73 20 61 20 6e  utine starts a n
250b8 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  ew transaction i
250b9 66 20 77 65 20 61 72 65 20 6e 6f 74 20 61 6c 72  f we are not alr
250ba 65 61 64 79 20 77 69 74 68 69 6e 0a 2a 2a 20 61  eady within.** a
250bb 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49   transaction.  I
250bc 66 20 77 65 20 61 72 65 20 61 6c 72 65 61 64 79  f we are already
250bd 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61   within a transa
250be 63 74 69 6f 6e 2c 20 74 68 65 6e 20 61 20 63 68  ction, then a ch
250bf 65 63 6b 70 6f 69 6e 74 0a 2a 2a 20 69 73 20 73  eckpoint.** is s
250c0 65 74 20 69 66 20 74 68 65 20 73 65 74 53 74 61  et if the setSta
250c1 74 65 6d 65 6e 74 20 70 61 72 61 6d 65 74 65 72  tement parameter
250c2 20 69 73 20 74 72 75 65 2e 20 20 41 20 63 68 65   is true.  A che
250c3 63 6b 70 6f 69 6e 74 20 73 68 6f 75 6c 64 0a 2a  ckpoint should.*
250c4 2a 20 62 65 20 73 65 74 20 66 6f 72 20 6f 70 65  * be set for ope
250c5 72 61 74 69 6f 6e 73 20 74 68 61 74 20 6d 69 67  rations that mig
250c6 68 74 20 66 61 69 6c 20 28 64 75 65 20 74 6f 20  ht fail (due to 
250c7 61 20 63 6f 6e 73 74 72 61 69 6e 74 29 20 70 61  a constraint) pa
250c8 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 77 61 79  rt of.** the way
250c9 20 74 68 72 6f 75 67 68 20 61 6e 64 20 77 68 69   through and whi
250ca 63 68 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20  ch will need to 
250cb 75 6e 64 6f 20 73 6f 6d 65 20 77 72 69 74 65 73  undo some writes
250cc 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20   without having 
250cd 74 6f 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 74  to.** rollback t
250ce 68 65 20 77 68 6f 6c 65 20 74 72 61 6e 73 61 63  he whole transac
250cf 74 69 6f 6e 2e 20 20 46 6f 72 20 6f 70 65 72 61  tion.  For opera
250d0 74 69 6f 6e 73 20 77 68 65 72 65 20 61 6c 6c 20  tions where all 
250d1 63 6f 6e 73 74 72 61 69 6e 74 73 0a 2a 2a 20 63  constraints.** c
250d2 61 6e 20 62 65 20 63 68 65 63 6b 65 64 20 62 65  an be checked be
250d3 66 6f 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73  fore any changes
250d4 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65   are made to the
250d5 20 64 61 74 61 62 61 73 65 2c 20 69 74 20 69 73   database, it is
250d6 20 6e 65 76 65 72 0a 2a 2a 20 6e 65 63 65 73 73   never.** necess
250d7 61 72 79 20 74 6f 20 75 6e 64 6f 20 61 20 77 72  ary to undo a wr
250d8 69 74 65 20 61 6e 64 20 74 68 65 20 63 68 65 63  ite and the chec
250d9 6b 70 6f 69 6e 74 20 73 68 6f 75 6c 64 20 6e 6f  kpoint should no
250da 74 20 62 65 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c  t be set..*/.SQL
250db 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
250dc 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
250dd 74 65 4f 70 65 72 61 74 69 6f 6e 28 50 61 72 73  teOperation(Pars
250de 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 73  e *pParse, int s
250df 65 74 53 74 61 74 65 6d 65 6e 74 2c 20 69 6e 74  etStatement, int
250e0 20 69 44 62 29 7b 0a 20 20 50 61 72 73 65 20 2a   iDb){.  Parse *
250e1 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69  pToplevel = sqli
250e2 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c  te3ParseToplevel
250e3 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69  (pParse);.  sqli
250e4 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
250e5 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 29  ema(pParse, iDb)
250e6 3b 0a 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 77  ;.  pToplevel->w
250e7 72 69 74 65 4d 61 73 6b 20 7c 3d 20 31 3c 3c 69  riteMask |= 1<<i
250e8 44 62 3b 0a 20 20 70 54 6f 70 6c 65 76 65 6c 2d  Db;.  pToplevel-
250e9 3e 69 73 4d 75 6c 74 69 57 72 69 74 65 20 7c 3d  >isMultiWrite |=
250ea 20 73 65 74 53 74 61 74 65 6d 65 6e 74 3b 0a 7d   setStatement;.}
250eb 0a 0a 2f 2a 0a 2a 2a 20 49 6e 64 69 63 61 74 65  ../*.** Indicate
250ec 20 74 68 61 74 20 74 68 65 20 73 74 61 74 65 6d   that the statem
250ed 65 6e 74 20 63 75 72 72 65 6e 74 6c 79 20 75 6e  ent currently un
250ee 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
250ef 20 6d 69 67 68 74 20 77 72 69 74 65 0a 2a 2a 20   might write.** 
250f0 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 65 6e  more than one en
250f1 74 72 79 20 28 65 78 61 6d 70 6c 65 3a 20 64 65  try (example: de
250f2 6c 65 74 69 6e 67 20 6f 6e 65 20 72 6f 77 20 74  leting one row t
250f3 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 61 6e  hen inserting an
250f4 6f 74 68 65 72 2c 0a 2a 2a 20 69 6e 73 65 72 74  other,.** insert
250f5 69 6e 67 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77  ing multiple row
250f6 73 20 69 6e 20 61 20 74 61 62 6c 65 2c 20 6f 72  s in a table, or
250f7 20 69 6e 73 65 72 74 69 6e 67 20 61 20 72 6f 77   inserting a row
250f8 20 61 6e 64 20 69 6e 64 65 78 20 65 6e 74 72 69   and index entri
250f9 65 73 2e 29 0a 2a 2a 20 49 66 20 61 6e 20 61 62  es.).** If an ab
250fa 6f 72 74 20 6f 63 63 75 72 73 20 61 66 74 65 72  ort occurs after
250fb 20 73 6f 6d 65 20 6f 66 20 74 68 65 73 65 20 77   some of these w
250fc 72 69 74 65 73 20 68 61 76 65 20 63 6f 6d 70 6c  rites have compl
250fd 65 74 65 64 2c 20 74 68 65 6e 20 69 74 20 77 69  eted, then it wi
250fe 6c 6c 0a 2a 2a 20 62 65 20 6e 65 63 65 73 73 61  ll.** be necessa
250ff 72 79 20 74 6f 20 75 6e 64 6f 20 74 68 65 20 63  ry to undo the c
25100 6f 6d 70 6c 65 74 65 64 20 77 72 69 74 65 73 2e  ompleted writes.
25101 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
25102 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  TE void sqlite3M
25103 75 6c 74 69 57 72 69 74 65 28 50 61 72 73 65 20  ultiWrite(Parse 
25104 2a 70 50 61 72 73 65 29 7b 0a 20 20 50 61 72 73  *pParse){.  Pars
25105 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73  e *pToplevel = s
25106 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65  qlite3ParseTople
25107 76 65 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 70  vel(pParse);.  p
25108 54 6f 70 6c 65 76 65 6c 2d 3e 69 73 4d 75 6c 74  Toplevel->isMult
25109 69 57 72 69 74 65 20 3d 20 31 3b 0a 7d 0a 0a 2f  iWrite = 1;.}../
2510a 2a 20 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67  * .** The code g
2510b 65 6e 65 72 61 74 6f 72 20 63 61 6c 6c 73 20 74  enerator calls t
2510c 68 69 73 20 72 6f 75 74 69 6e 65 20 69 66 20 69  his routine if i
2510d 73 20 64 69 73 63 6f 76 65 72 73 20 74 68 61 74  s discovers that
2510e 20 69 74 20 69 73 0a 2a 2a 20 70 6f 73 73 69 62   it is.** possib
2510f 6c 65 20 74 6f 20 61 62 6f 72 74 20 61 20 73 74  le to abort a st
25110 61 74 65 6d 65 6e 74 20 70 72 69 6f 72 20 74 6f  atement prior to
25111 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 20 20 49 6e   completion.  In
25112 20 6f 72 64 65 72 20 74 6f 20 0a 2a 2a 20 70 65   order to .** pe
25113 72 66 6f 72 6d 20 74 68 69 73 20 61 62 6f 72 74  rform this abort
25114 20 77 69 74 68 6f 75 74 20 63 6f 72 72 75 70 74   without corrupt
25115 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65  ing the database
25116 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 6d 61 6b  , we need to mak
25117 65 0a 2a 2a 20 73 75 72 65 20 74 68 61 74 20 74  e.** sure that t
25118 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
25119 70 72 6f 74 65 63 74 65 64 20 62 79 20 61 20 73  protected by a s
2511a 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
2511b 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 65 63 68  tion..**.** Tech
2511c 6e 69 63 61 6c 6c 79 2c 20 77 65 20 6f 6e 6c 79  nically, we only
2511d 20 6e 65 65 64 20 74 6f 20 73 65 74 20 74 68 65   need to set the
2511e 20 6d 61 79 41 62 6f 72 74 20 66 6c 61 67 20 69   mayAbort flag i
2511f 66 20 74 68 65 0a 2a 2a 20 69 73 4d 75 6c 74 69  f the.** isMulti
25120 57 72 69 74 65 20 66 6c 61 67 20 77 61 73 20 70  Write flag was p
25121 72 65 76 69 6f 75 73 6c 79 20 73 65 74 2e 20 20  reviously set.  
25122 54 68 65 72 65 20 69 73 20 61 20 74 69 6d 65 20  There is a time 
25123 64 65 70 65 6e 64 65 6e 63 79 0a 2a 2a 20 73 75  dependency.** su
25124 63 68 20 74 68 61 74 20 74 68 65 20 61 62 6f 72  ch that the abor
25125 74 20 6d 75 73 74 20 6f 63 63 75 72 20 61 66 74  t must occur aft
25126 65 72 20 74 68 65 20 6d 75 6c 74 69 77 72 69 74  er the multiwrit
25127 65 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 0a 2a  e.  This makes.*
25128 2a 20 73 6f 6d 65 20 73 74 61 74 65 6d 65 6e 74  * some statement
25129 73 20 69 6e 76 6f 6c 76 69 6e 67 20 74 68 65 20  s involving the 
2512a 52 45 50 4c 41 43 45 20 63 6f 6e 66 6c 69 63 74  REPLACE conflict
2512b 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f   resolution algo
2512c 72 69 74 68 6d 0a 2a 2a 20 67 6f 20 61 20 6c 69  rithm.** go a li
2512d 74 74 6c 65 20 66 61 73 74 65 72 2e 20 20 42 75  ttle faster.  Bu
2512e 74 20 74 61 6b 69 6e 67 20 61 64 76 61 6e 74 61  t taking advanta
2512f 67 65 20 6f 66 20 74 68 69 73 20 74 69 6d 65 20  ge of this time 
25130 64 65 70 65 6e 64 65 6e 63 79 0a 2a 2a 20 6d 61  dependency.** ma
25131 6b 65 73 20 69 74 20 6d 6f 72 65 20 64 69 66 66  kes it more diff
25132 69 63 75 6c 74 20 74 6f 20 70 72 6f 76 65 20 74  icult to prove t
25133 68 61 74 20 74 68 65 20 63 6f 64 65 20 69 73 20  hat the code is 
25134 63 6f 72 72 65 63 74 20 28 69 6e 20 0a 2a 2a 20  correct (in .** 
25135 70 61 72 74 69 63 75 6c 61 72 2c 20 69 74 20 70  particular, it p
25136 72 65 76 65 6e 74 73 20 75 73 20 66 72 6f 6d 20  revents us from 
25137 77 72 69 74 69 6e 67 20 61 6e 20 65 66 66 65 63  writing an effec
25138 74 69 76 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  tive.** implemen
25139 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65  tation of sqlite
2513a 33 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28  3AssertMayAbort(
2513b 29 29 20 61 6e 64 20 73 6f 20 77 65 20 68 61 76  )) and so we hav
2513c 65 20 63 68 6f 73 65 6e 0a 2a 2a 20 74 6f 20 74  e chosen.** to t
2513d 61 6b 65 20 74 68 65 20 73 61 66 65 20 72 6f 75  ake the safe rou
2513e 74 65 20 61 6e 64 20 73 6b 69 70 20 74 68 65 20  te and skip the 
2513f 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 0a 2a 2f  optimization..*/
25140 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
25141 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 79 41  void sqlite3MayA
25142 62 6f 72 74 28 50 61 72 73 65 20 2a 70 50 61 72  bort(Parse *pPar
25143 73 65 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 54  se){.  Parse *pT
25144 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65  oplevel = sqlite
25145 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70  3ParseToplevel(p
25146 50 61 72 73 65 29 3b 0a 20 20 70 54 6f 70 6c 65  Parse);.  pTople
25147 76 65 6c 2d 3e 6d 61 79 41 62 6f 72 74 20 3d 20  vel->mayAbort = 
25148 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65  1;.}../*.** Code
25149 20 61 6e 20 4f 50 5f 48 61 6c 74 20 74 68 61 74   an OP_Halt that
2514a 20 63 61 75 73 65 73 20 74 68 65 20 76 64 62 65   causes the vdbe
2514b 20 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 53 51   to return an SQ
2514c 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 0a  LITE_CONSTRAINT.
2514d 2a 2a 20 65 72 72 6f 72 2e 20 54 68 65 20 6f 6e  ** error. The on
2514e 45 72 72 6f 72 20 70 61 72 61 6d 65 74 65 72 20  Error parameter 
2514f 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69 63 68  determines which
25150 20 28 69 66 20 61 6e 79 29 20 6f 66 20 74 68 65   (if any) of the
25151 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61 6e   statement.** an
25152 64 2f 6f 72 20 63 75 72 72 65 6e 74 20 74 72 61  d/or current tra
25153 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c  nsaction is roll
25154 65 64 20 62 61 63 6b 2e 0a 2a 2f 0a 53 51 4c 49  ed back..*/.SQLI
25155 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
25156 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74  sqlite3HaltConst
25157 72 61 69 6e 74 28 50 61 72 73 65 20 2a 70 50 61  raint(Parse *pPa
25158 72 73 65 2c 20 69 6e 74 20 6f 6e 45 72 72 6f 72  rse, int onError
25159 2c 20 63 68 61 72 20 2a 70 34 2c 20 69 6e 74 20  , char *p4, int 
2515a 70 34 74 79 70 65 29 7b 0a 20 20 56 64 62 65 20  p4type){.  Vdbe 
2515b 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  *v = sqlite3GetV
2515c 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
2515d 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 41  f( onError==OE_A
2515e 62 6f 72 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  bort ){.    sqli
2515f 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61 72  te3MayAbort(pPar
25160 73 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  se);.  }.  sqlit
25161 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
25162 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f  OP_Halt, SQLITE_
25163 43 4f 4e 53 54 52 41 49 4e 54 2c 20 6f 6e 45 72  CONSTRAINT, onEr
25164 72 6f 72 2c 20 30 2c 20 70 34 2c 20 70 34 74 79  ror, 0, p4, p4ty
25165 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  pe);.}../*.** Ch
25166 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 70 49  eck to see if pI
25167 6e 64 65 78 20 75 73 65 73 20 74 68 65 20 63 6f  ndex uses the co
25168 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
25169 20 70 43 6f 6c 6c 2e 20 20 52 65 74 75 72 6e 0a   pColl.  Return.
2516a 2a 2a 20 74 72 75 65 20 69 66 20 69 74 20 64 6f  ** true if it do
2516b 65 73 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20  es and false if 
2516c 69 74 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2f 0a  it does not..*/.
2516d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2516e 4d 49 54 5f 52 45 49 4e 44 45 58 0a 73 74 61 74  MIT_REINDEX.stat
2516f 69 63 20 69 6e 74 20 63 6f 6c 6c 61 74 69 6f 6e  ic int collation
25170 4d 61 74 63 68 28 63 6f 6e 73 74 20 63 68 61 72  Match(const char
25171 20 2a 7a 43 6f 6c 6c 2c 20 49 6e 64 65 78 20 2a   *zColl, Index *
25172 70 49 6e 64 65 78 29 7b 0a 20 20 69 6e 74 20 69  pIndex){.  int i
25173 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 43 6f 6c  ;.  assert( zCol
25174 6c 21 3d 30 20 29 3b 0a 20 20 66 6f 72 28 69 3d  l!=0 );.  for(i=
25175 30 3b 20 69 3c 70 49 6e 64 65 78 2d 3e 6e 43 6f  0; i<pIndex->nCo
25176 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  lumn; i++){.    
25177 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20  const char *z = 
25178 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 69  pIndex->azColl[i
25179 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a  ];.    assert( z
2517a 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 30  !=0 );.    if( 0
2517b 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ==sqlite3StrICmp
2517c 28 7a 2c 20 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20  (z, zColl) ){.  
2517d 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2517e 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
2517f 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a   0;.}.#endif../*
25180 0a 2a 2a 20 52 65 63 6f 6d 70 75 74 65 20 61 6c  .** Recompute al
25181 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54 61  l indices of pTa
25182 62 20 74 68 61 74 20 75 73 65 20 74 68 65 20 63  b that use the c
25183 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
25184 65 20 70 43 6f 6c 6c 2e 0a 2a 2a 20 49 66 20 70  e pColl..** If p
25185 43 6f 6c 6c 3d 3d 30 20 74 68 65 6e 20 72 65 63  Coll==0 then rec
25186 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e 64 69 63  ompute all indic
25187 65 73 20 6f 66 20 70 54 61 62 2e 0a 2a 2f 0a 23  es of pTab..*/.#
25188 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
25189 49 54 5f 52 45 49 4e 44 45 58 0a 73 74 61 74 69  IT_REINDEX.stati
2518a 63 20 76 6f 69 64 20 72 65 69 6e 64 65 78 54 61  c void reindexTa
2518b 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ble(Parse *pPars
2518c 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  e, Table *pTab, 
2518d 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43 6f 6c  char const *zCol
2518e 6c 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e  l){.  Index *pIn
2518f 64 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  dex;            
25190 20 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 61 73    /* An index as
25191 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 54  sociated with pT
25192 61 62 20 2a 2f 0a 0a 20 20 66 6f 72 28 70 49 6e  ab */..  for(pIn
25193 64 65 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  dex=pTab->pIndex
25194 3b 20 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78  ; pIndex; pIndex
25195 3d 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 29 7b  =pIndex->pNext){
25196 0a 20 20 20 20 69 66 28 20 7a 43 6f 6c 6c 3d 3d  .    if( zColl==
25197 30 20 7c 7c 20 63 6f 6c 6c 61 74 69 6f 6e 4d 61  0 || collationMa
25198 74 63 68 28 7a 43 6f 6c 6c 2c 20 70 49 6e 64 65  tch(zColl, pInde
25199 78 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  x) ){.      int 
2519a 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
2519b 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
2519c 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  e->db, pTab->pSc
2519d 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 73 71 6c  hema);.      sql
2519e 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
2519f 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
251a0 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73  0, iDb);.      s
251a1 71 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65  qlite3RefillInde
251a2 78 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78  x(pParse, pIndex
251a3 2c 20 2d 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  , -1);.    }.  }
251a4 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
251a5 20 52 65 63 6f 6d 70 75 74 65 20 61 6c 6c 20 69   Recompute all i
251a6 6e 64 69 63 65 73 20 6f 66 20 61 6c 6c 20 74 61  ndices of all ta
251a7 62 6c 65 73 20 69 6e 20 61 6c 6c 20 64 61 74 61  bles in all data
251a8 62 61 73 65 73 20 77 68 65 72 65 20 74 68 65 0a  bases where the.
251a9 2a 2a 20 69 6e 64 69 63 65 73 20 75 73 65 20 74  ** indices use t
251aa 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  he collating seq
251ab 75 65 6e 63 65 20 70 43 6f 6c 6c 2e 20 20 49 66  uence pColl.  If
251ac 20 70 43 6f 6c 6c 3d 3d 30 20 74 68 65 6e 20 72   pColl==0 then r
251ad 65 63 6f 6d 70 75 74 65 0a 2a 2a 20 61 6c 6c 20  ecompute.** all 
251ae 69 6e 64 69 63 65 73 20 65 76 65 72 79 77 68 65  indices everywhe
251af 72 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  re..*/.#ifndef S
251b0 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44  QLITE_OMIT_REIND
251b1 45 58 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  EX.static void r
251b2 65 69 6e 64 65 78 44 61 74 61 62 61 73 65 73 28  eindexDatabases(
251b3 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63  Parse *pParse, c
251b4 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43 6f 6c 6c  har const *zColl
251b5 29 7b 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20  ){.  Db *pDb;   
251b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
251b7 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 64 61 74   /* A single dat
251b8 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  abase */.  int i
251b9 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
251ba 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
251bb 74 61 62 61 73 65 20 69 6e 64 65 78 20 6e 75 6d  tabase index num
251bc 62 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ber */.  sqlite3
251bd 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
251be 62 3b 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61  b;   /* The data
251bf 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
251c0 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 6b  */.  HashElem *k
251c1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
251c2 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20   /* For looping 
251c3 6f 76 65 72 20 74 61 62 6c 65 73 20 69 6e 20 70  over tables in p
251c4 44 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  Db */.  Table *p
251c5 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  Tab;            
251c6 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 69      /* A table i
251c7 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a  n the database *
251c8 2f 0a 0a 20 20 66 6f 72 28 69 44 62 3d 30 2c 20  /..  for(iDb=0, 
251c9 70 44 62 3d 64 62 2d 3e 61 44 62 3b 20 69 44 62  pDb=db->aDb; iDb
251ca 3c 64 62 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b 2c  <db->nDb; iDb++,
251cb 20 70 44 62 2b 2b 29 7b 0a 20 20 20 20 61 73 73   pDb++){.    ass
251cc 65 72 74 28 20 70 44 62 21 3d 30 20 29 3b 0a 20  ert( pDb!=0 );. 
251cd 20 20 20 66 6f 72 28 6b 3d 73 71 6c 69 74 65 48     for(k=sqliteH
251ce 61 73 68 46 69 72 73 74 28 26 70 44 62 2d 3e 70  ashFirst(&pDb->p
251cf 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29  Schema->tblHash)
251d0 3b 20 20 6b 3b 20 6b 3d 73 71 6c 69 74 65 48 61  ;  k; k=sqliteHa
251d1 73 68 4e 65 78 74 28 6b 29 29 7b 0a 20 20 20 20  shNext(k)){.    
251d2 20 20 70 54 61 62 20 3d 20 28 54 61 62 6c 65 2a    pTab = (Table*
251d3 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28  )sqliteHashData(
251d4 6b 29 3b 0a 20 20 20 20 20 20 72 65 69 6e 64 65  k);.      reinde
251d5 78 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70  xTable(pParse, p
251d6 54 61 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20  Tab, zColl);.   
251d7 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a   }.  }.}.#endif.
251d8 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
251d9 63 6f 64 65 20 66 6f 72 20 74 68 65 20 52 45 49  code for the REI
251da 4e 44 45 58 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a  NDEX command..**
251db 0a 2a 2a 20 20 20 20 20 20 20 20 52 45 49 4e 44  .**        REIND
251dc 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20  EX              
251dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d                --
251de 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 52 45 49   1.**        REI
251df 4e 44 45 58 20 20 3c 63 6f 6c 6c 61 74 69 6f 6e  NDEX  <collation
251e0 3e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  >               
251e1 2d 2d 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 52  -- 2.**        R
251e2 45 49 4e 44 45 58 20 20 3f 3c 64 61 74 61 62 61  EINDEX  ?<databa
251e3 73 65 3e 2e 3f 3c 74 61 62 6c 65 6e 61 6d 65 3e  se>.?<tablename>
251e4 20 20 2d 2d 20 33 0a 2a 2a 20 20 20 20 20 20 20    -- 3.**       
251e5 20 52 45 49 4e 44 45 58 20 20 3f 3c 64 61 74 61   REINDEX  ?<data
251e6 62 61 73 65 3e 2e 3f 3c 69 6e 64 65 78 6e 61 6d  base>.?<indexnam
251e7 65 3e 20 20 2d 2d 20 34 0a 2a 2a 0a 2a 2a 20 46  e>  -- 4.**.** F
251e8 6f 72 6d 20 31 20 63 61 75 73 65 73 20 61 6c 6c  orm 1 causes all
251e9 20 69 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c 20   indices in all 
251ea 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
251eb 65 73 20 74 6f 20 62 65 20 72 65 62 75 69 6c 74  es to be rebuilt
251ec 2e 0a 2a 2a 20 46 6f 72 6d 20 32 20 72 65 62 75  ..** Form 2 rebu
251ed 69 6c 64 73 20 61 6c 6c 20 69 6e 64 69 63 65 73  ilds all indices
251ee 20 69 6e 20 61 6c 6c 20 64 61 74 61 62 61 73 65   in all database
251ef 73 20 74 68 61 74 20 75 73 65 20 74 68 65 20 6e  s that use the n
251f0 61 6d 65 64 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e  amed.** collatin
251f1 67 20 66 75 6e 63 74 69 6f 6e 2e 20 20 46 6f 72  g function.  For
251f2 6d 73 20 33 20 61 6e 64 20 34 20 72 65 62 75 69  ms 3 and 4 rebui
251f3 6c 64 20 74 68 65 20 6e 61 6d 65 64 20 69 6e 64  ld the named ind
251f4 65 78 20 6f 72 20 61 6c 6c 0a 2a 2a 20 69 6e 64  ex or all.** ind
251f5 69 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  ices associated 
251f6 77 69 74 68 20 74 68 65 20 6e 61 6d 65 64 20 74  with the named t
251f7 61 62 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  able..*/.#ifndef
251f8 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49   SQLITE_OMIT_REI
251f9 4e 44 45 58 0a 53 51 4c 49 54 45 5f 50 52 49 56  NDEX.SQLITE_PRIV
251fa 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
251fb 52 65 69 6e 64 65 78 28 50 61 72 73 65 20 2a 70  Reindex(Parse *p
251fc 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e  Parse, Token *pN
251fd 61 6d 65 31 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61  ame1, Token *pNa
251fe 6d 65 32 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20  me2){.  CollSeq 
251ff 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20  *pColl;         
25200 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67      /* Collating
25201 20 73 65 71 75 65 6e 63 65 20 74 6f 20 62 65 20   sequence to be 
25202 72 65 69 6e 64 65 78 65 64 2c 20 6f 72 20 4e 55  reindexed, or NU
25203 4c 4c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 3b  LL */.  char *z;
25204 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25205 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 61      /* Name of a
25206 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
25207 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
25208 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20 20  *zDb;           
25209 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
2520a 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 54 61  database */.  Ta
2520b 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20  ble *pTab;      
2520c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 74            /* A t
2520d 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61  able in the data
2520e 62 61 73 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20  base */.  Index 
2520f 2a 70 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20  *pIndex;        
25210 20 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e 64 65        /* An inde
25211 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  x associated wit
25212 68 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20  h pTab */.  int 
25213 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
25214 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
25215 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 6e 75  atabase index nu
25216 6d 62 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65  mber */.  sqlite
25217 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
25218 64 62 3b 20 20 20 2f 2a 20 54 68 65 20 64 61 74  db;   /* The dat
25219 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2521a 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4f 62   */.  Token *pOb
2521b 6a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  jName;          
2521c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
2521d 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
2521e 74 6f 20 62 65 20 72 65 69 6e 64 65 78 65 64 20  to be reindexed 
2521f 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68  */..  /* Read th
25220 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
25221 61 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  a. If an error o
25222 63 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20  ccurs, leave an 
25223 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20  error message.  
25224 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70  ** and code in p
25225 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e  Parse and return
25226 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28 20   NULL. */.  if( 
25227 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74  SQLITE_OK!=sqlit
25228 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
25229 72 73 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75  rse) ){.    retu
2522a 72 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70  rn;.  }..  if( p
2522b 4e 61 6d 65 31 3d 3d 30 20 29 7b 0a 20 20 20 20  Name1==0 ){.    
2522c 72 65 69 6e 64 65 78 44 61 74 61 62 61 73 65 73  reindexDatabases
2522d 28 70 50 61 72 73 65 2c 20 30 29 3b 0a 20 20 20  (pParse, 0);.   
2522e 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65   return;.  }else
2522f 20 69 66 28 20 4e 45 56 45 52 28 70 4e 61 6d 65   if( NEVER(pName
25230 32 3d 3d 30 29 20 7c 7c 20 70 4e 61 6d 65 32 2d  2==0) || pName2-
25231 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61  >z==0 ){.    cha
25232 72 20 2a 7a 43 6f 6c 6c 3b 0a 20 20 20 20 61 73  r *zColl;.    as
25233 73 65 72 74 28 20 70 4e 61 6d 65 31 2d 3e 7a 20  sert( pName1->z 
25234 29 3b 0a 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 73  );.    zColl = s
25235 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
25236 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ken(pParse->db, 
25237 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 69 66 28  pName1);.    if(
25238 20 21 7a 43 6f 6c 6c 20 29 20 72 65 74 75 72 6e   !zColl ) return
25239 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71  ;.    pColl = sq
2523a 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
2523b 28 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a 43  (db, ENC(db), zC
2523c 6f 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  oll, 0);.    if(
2523d 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20   pColl ){.      
2523e 72 65 69 6e 64 65 78 44 61 74 61 62 61 73 65 73  reindexDatabases
2523f 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b  (pParse, zColl);
25240 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
25241 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29 3b  Free(db, zColl);
25242 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
25243 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
25244 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c  DbFree(db, zColl
25245 29 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 73  );.  }.  iDb = s
25246 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d  qlite3TwoPartNam
25247 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31  e(pParse, pName1
25248 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4f 62 6a 4e  , pName2, &pObjN
25249 61 6d 65 29 3b 0a 20 20 69 66 28 20 69 44 62 3c  ame);.  if( iDb<
2524a 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a 20  0 ) return;.  z 
2524b 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
2524c 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4f 62 6a 4e  mToken(db, pObjN
2524d 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30  ame);.  if( z==0
2524e 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a 44 62   ) return;.  zDb
2524f 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
25250 7a 4e 61 6d 65 3b 0a 20 20 70 54 61 62 20 3d 20  zName;.  pTab = 
25251 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
25252 28 64 62 2c 20 7a 2c 20 7a 44 62 29 3b 0a 20 20  (db, z, zDb);.  
25253 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20  if( pTab ){.    
25254 72 65 69 6e 64 65 78 54 61 62 6c 65 28 70 50 61  reindexTable(pPa
25255 72 73 65 2c 20 70 54 61 62 2c 20 30 29 3b 0a 20  rse, pTab, 0);. 
25256 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
25257 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 72 65 74  (db, z);.    ret
25258 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 49 6e 64 65  urn;.  }.  pInde
25259 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49  x = sqlite3FindI
2525a 6e 64 65 78 28 64 62 2c 20 7a 2c 20 7a 44 62 29  ndex(db, z, zDb)
2525b 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
2525c 65 28 64 62 2c 20 7a 29 3b 0a 20 20 69 66 28 20  e(db, z);.  if( 
2525d 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73 71  pIndex ){.    sq
2525e 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
2525f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c  peration(pParse,
25260 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71   0, iDb);.    sq
25261 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78  lite3RefillIndex
25262 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78 2c  (pParse, pIndex,
25263 20 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 72 6e   -1);.    return
25264 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
25265 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
25266 22 75 6e 61 62 6c 65 20 74 6f 20 69 64 65 6e 74  "unable to ident
25267 69 66 79 20 74 68 65 20 6f 62 6a 65 63 74 20 74  ify the object t
25268 6f 20 62 65 20 72 65 69 6e 64 65 78 65 64 22 29  o be reindexed")
25269 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
2526a 2a 20 52 65 74 75 72 6e 20 61 20 64 79 6e 61 6d  * Return a dynam
2526b 69 63 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 4b  icly allocated K
2526c 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
2526d 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65   that can be use
2526e 64 0a 2a 2a 20 77 69 74 68 20 4f 50 5f 4f 70 65  d.** with OP_Ope
2526f 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e  nRead or OP_Open
25270 57 72 69 74 65 20 74 6f 20 61 63 63 65 73 73 20  Write to access 
25271 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 70  database index p
25272 49 64 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75  Idx..**.** If su
25273 63 63 65 73 73 66 75 6c 2c 20 61 20 70 6f 69 6e  ccessful, a poin
25274 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73  ter to the new s
25275 74 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75  tructure is retu
25276 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61  rned. In this ca
25277 73 65 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72  se.** the caller
25278 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
25279 66 6f 72 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69  for calling sqli
2527a 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 29 20  te3DbFree(db, ) 
2527b 6f 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  on the returned 
2527c 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 49 66 20  .** pointer. If 
2527d 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
2527e 28 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20 6f  (out of memory o
2527f 72 20 6d 69 73 73 69 6e 67 20 63 6f 6c 6c 61 74  r missing collat
25280 69 6f 6e 20 0a 2a 2a 20 73 65 71 75 65 6e 63 65  ion .** sequence
25281 29 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72  ), NULL is retur
25282 6e 65 64 20 61 6e 64 20 74 68 65 20 73 74 61 74  ned and the stat
25283 65 20 6f 66 20 70 50 61 72 73 65 20 75 70 64 61  e of pParse upda
25284 74 65 64 20 74 6f 20 72 65 66 6c 65 63 74 0a 2a  ted to reflect.*
25285 2a 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a  * the error..*/.
25286 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 4b  SQLITE_PRIVATE K
25287 65 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 49  eyInfo *sqlite3I
25288 6e 64 65 78 4b 65 79 69 6e 66 6f 28 50 61 72 73  ndexKeyinfo(Pars
25289 65 20 2a 70 50 61 72 73 65 2c 20 49 6e 64 65 78  e *pParse, Index
2528a 20 2a 70 49 64 78 29 7b 0a 20 20 69 6e 74 20 69   *pIdx){.  int i
2528b 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 20 3d 20 70  ;.  int nCol = p
2528c 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20  Idx->nColumn;.  
2528d 69 6e 74 20 6e 42 79 74 65 73 20 3d 20 73 69 7a  int nBytes = siz
2528e 65 6f 66 28 4b 65 79 49 6e 66 6f 29 20 2b 20 28  eof(KeyInfo) + (
2528f 6e 43 6f 6c 2d 31 29 2a 73 69 7a 65 6f 66 28 43  nCol-1)*sizeof(C
25290 6f 6c 6c 53 65 71 2a 29 20 2b 20 6e 43 6f 6c 3b  ollSeq*) + nCol;
25291 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
25292 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 4b   pParse->db;.  K
25293 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 28  eyInfo *pKey = (
25294 4b 65 79 49 6e 66 6f 20 2a 29 73 71 6c 69 74 65  KeyInfo *)sqlite
25295 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
25296 2c 20 6e 42 79 74 65 73 29 3b 0a 0a 20 20 69 66  , nBytes);..  if
25297 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 70 4b  ( pKey ){.    pK
25298 65 79 2d 3e 64 62 20 3d 20 70 50 61 72 73 65 2d  ey->db = pParse-
25299 3e 64 62 3b 0a 20 20 20 20 70 4b 65 79 2d 3e 61  >db;.    pKey->a
2529a 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 20  SortOrder = (u8 
2529b 2a 29 26 28 70 4b 65 79 2d 3e 61 43 6f 6c 6c 5b  *)&(pKey->aColl[
2529c 6e 43 6f 6c 5d 29 3b 0a 20 20 20 20 61 73 73 65  nCol]);.    asse
2529d 72 74 28 20 26 70 4b 65 79 2d 3e 61 53 6f 72 74  rt( &pKey->aSort
2529e 4f 72 64 65 72 5b 6e 43 6f 6c 5d 3d 3d 26 28 28  Order[nCol]==&((
2529f 28 75 38 20 2a 29 70 4b 65 79 29 5b 6e 42 79 74  (u8 *)pKey)[nByt
252a0 65 73 5d 29 20 29 3b 0a 20 20 20 20 66 6f 72 28  es]) );.    for(
252a1 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
252a2 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  ){.      char *z
252a3 43 6f 6c 6c 20 3d 20 70 49 64 78 2d 3e 61 7a 43  Coll = pIdx->azC
252a4 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 61 73  oll[i];.      as
252a5 73 65 72 74 28 20 7a 43 6f 6c 6c 20 29 3b 0a 20  sert( zColl );. 
252a6 20 20 20 20 20 70 4b 65 79 2d 3e 61 43 6f 6c 6c       pKey->aColl
252a7 5b 69 5d 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63  [i] = sqlite3Loc
252a8 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  ateCollSeq(pPars
252a9 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20  e, zColl);.     
252aa 20 70 4b 65 79 2d 3e 61 53 6f 72 74 4f 72 64 65   pKey->aSortOrde
252ab 72 5b 69 5d 20 3d 20 70 49 64 78 2d 3e 61 53 6f  r[i] = pIdx->aSo
252ac 72 74 4f 72 64 65 72 5b 69 5d 3b 0a 20 20 20 20  rtOrder[i];.    
252ad 7d 0a 20 20 20 20 70 4b 65 79 2d 3e 6e 46 69 65  }.    pKey->nFie
252ae 6c 64 20 3d 20 28 75 31 36 29 6e 43 6f 6c 3b 0a  ld = (u16)nCol;.
252af 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 72 73    }..  if( pPars
252b0 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20 73  e->nErr ){.    s
252b1 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
252b2 20 70 4b 65 79 29 3b 0a 20 20 20 20 70 4b 65 79   pKey);.    pKey
252b3 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
252b4 72 6e 20 70 4b 65 79 3b 0a 7d 0a 0a 2f 2a 2a 2a  rn pKey;.}../***
252b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
252b6 6f 66 20 62 75 69 6c 64 2e 63 20 2a 2a 2a 2a 2a  of build.c *****
252b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
252b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
252b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
252ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
252bb 6e 20 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b 2e  n file callback.
252bc 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
252bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
252be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
252bf 2a 20 32 30 30 35 20 4d 61 79 20 32 33 20 0a 2a  * 2005 May 23 .*
252c0 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
252c1 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
252c2 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
252c3 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
252c4 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
252c5 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
252c6 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
252c7 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
252c8 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
252c9 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
252ca 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
252cb 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
252cc 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
252cd 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
252ce 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
252cf 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
252d0 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
252d1 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
252d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
252d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
252d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
252d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
252d6 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
252d7 6e 74 61 69 6e 73 20 66 75 6e 63 74 69 6f 6e 73  ntains functions
252d8 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20   used to access 
252d9 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 68 61 73  the internal has
252da 68 20 74 61 62 6c 65 73 0a 2a 2a 20 6f 66 20 75  h tables.** of u
252db 73 65 72 20 64 65 66 69 6e 65 64 20 66 75 6e 63  ser defined func
252dc 74 69 6f 6e 73 20 61 6e 64 20 63 6f 6c 6c 61 74  tions and collat
252dd 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 2e 0a 2a  ion sequences..*
252de 2a 0a 2a 2a 20 24 49 64 3a 20 63 61 6c 6c 62 61  *.** $Id: callba
252df 63 6b 2e 63 2c 76 20 31 2e 34 32 20 32 30 30 39  ck.c,v 1.42 2009
252e0 2f 30 36 2f 31 37 20 30 30 3a 33 35 3a 33 31 20  /06/17 00:35:31 
252e1 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f  drh Exp $.*/.../
252e2 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20  *.** Invoke the 
252e3 27 63 6f 6c 6c 61 74 69 6f 6e 20 6e 65 65 64 65  'collation neede
252e4 64 27 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 72  d' callback to r
252e5 65 71 75 65 73 74 20 61 20 63 6f 6c 6c 61 74 69  equest a collati
252e6 6f 6e 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 69  on sequence.** i
252e7 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 65  n the encoding e
252e8 6e 63 20 6f 66 20 6e 61 6d 65 20 7a 4e 61 6d 65  nc of name zName
252e9 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a  , length nName..
252ea 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
252eb 61 6c 6c 43 6f 6c 6c 4e 65 65 64 65 64 28 73 71  allCollNeeded(sq
252ec 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 65  lite3 *db, int e
252ed 6e 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  nc, const char *
252ee 7a 4e 61 6d 65 29 7b 0a 20 20 61 73 73 65 72 74  zName){.  assert
252ef 28 20 21 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64  ( !db->xCollNeed
252f0 65 64 20 7c 7c 20 21 64 62 2d 3e 78 43 6f 6c 6c  ed || !db->xColl
252f1 4e 65 65 64 65 64 31 36 20 29 3b 0a 20 20 69 66  Needed16 );.  if
252f2 28 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65  ( db->xCollNeede
252f3 64 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  d ){.    char *z
252f4 45 78 74 65 72 6e 61 6c 20 3d 20 73 71 6c 69 74  External = sqlit
252f5 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a  e3DbStrDup(db, z
252f6 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21  Name);.    if( !
252f7 7a 45 78 74 65 72 6e 61 6c 20 29 20 72 65 74 75  zExternal ) retu
252f8 72 6e 3b 0a 20 20 20 20 64 62 2d 3e 78 43 6f 6c  rn;.    db->xCol
252f9 6c 4e 65 65 64 65 64 28 64 62 2d 3e 70 43 6f 6c  lNeeded(db->pCol
252fa 6c 4e 65 65 64 65 64 41 72 67 2c 20 64 62 2c 20  lNeededArg, db, 
252fb 65 6e 63 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b  enc, zExternal);
252fc 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
252fd 65 65 28 64 62 2c 20 7a 45 78 74 65 72 6e 61 6c  ee(db, zExternal
252fe 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  );.  }.#ifndef S
252ff 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
25300 0a 20 20 69 66 28 20 64 62 2d 3e 78 43 6f 6c 6c  .  if( db->xColl
25301 4e 65 65 64 65 64 31 36 20 29 7b 0a 20 20 20 20  Needed16 ){.    
25302 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 45 78 74  char const *zExt
25303 65 72 6e 61 6c 3b 0a 20 20 20 20 73 71 6c 69 74  ernal;.    sqlit
25304 65 33 5f 76 61 6c 75 65 20 2a 70 54 6d 70 20 3d  e3_value *pTmp =
25305 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77   sqlite3ValueNew
25306 28 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  (db);.    sqlite
25307 33 56 61 6c 75 65 53 65 74 53 74 72 28 70 54 6d  3ValueSetStr(pTm
25308 70 2c 20 2d 31 2c 20 7a 4e 61 6d 65 2c 20 53 51  p, -1, zName, SQ
25309 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
2530a 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7a  E_STATIC);.    z
2530b 45 78 74 65 72 6e 61 6c 20 3d 20 73 71 6c 69 74  External = sqlit
2530c 65 33 56 61 6c 75 65 54 65 78 74 28 70 54 6d 70  e3ValueText(pTmp
2530d 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  , SQLITE_UTF16NA
2530e 54 49 56 45 29 3b 0a 20 20 20 20 69 66 28 20 7a  TIVE);.    if( z
2530f 45 78 74 65 72 6e 61 6c 20 29 7b 0a 20 20 20 20  External ){.    
25310 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65    db->xCollNeede
25311 64 31 36 28 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65  d16(db->pCollNee
25312 64 65 64 41 72 67 2c 20 64 62 2c 20 28 69 6e 74  dedArg, db, (int
25313 29 45 4e 43 28 64 62 29 2c 20 7a 45 78 74 65 72  )ENC(db), zExter
25314 6e 61 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  nal);.    }.    
25315 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
25316 28 70 54 6d 70 29 3b 0a 20 20 7d 0a 23 65 6e 64  (pTmp);.  }.#end
25317 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  if.}../*.** This
25318 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
25319 65 64 20 69 66 20 74 68 65 20 63 6f 6c 6c 61 74  ed if the collat
2531a 69 6f 6e 20 66 61 63 74 6f 72 79 20 66 61 69 6c  ion factory fail
2531b 73 20 74 6f 20 64 65 6c 69 76 65 72 20 61 0a 2a  s to deliver a.*
2531c 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63  * collation func
2531d 74 69 6f 6e 20 69 6e 20 74 68 65 20 62 65 73 74  tion in the best
2531e 20 65 6e 63 6f 64 69 6e 67 20 62 75 74 20 74 68   encoding but th
2531f 65 72 65 20 6d 61 79 20 62 65 20 6f 74 68 65 72  ere may be other
25320 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f 66 20   versions.** of 
25321 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 66  this collation f
25322 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 6f 74 68  unction (for oth
25323 65 72 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  er text encoding
25324 73 29 20 61 76 61 69 6c 61 62 6c 65 2e 20 55 73  s) available. Us
25325 65 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 73  e one.** of thes
25326 65 20 69 6e 73 74 65 61 64 20 69 66 20 74 68 65  e instead if the
25327 79 20 65 78 69 73 74 2e 20 41 76 6f 69 64 20 61  y exist. Avoid a
25328 20 55 54 46 2d 38 20 3c 2d 3e 20 55 54 46 2d 31   UTF-8 <-> UTF-1
25329 36 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 66 0a  6 conversion if.
2532a 2a 2a 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a  ** possible..*/.
2532b 73 74 61 74 69 63 20 69 6e 74 20 73 79 6e 74 68  static int synth
2532c 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 20  CollSeq(sqlite3 
2532d 2a 64 62 2c 20 43 6f 6c 6c 53 65 71 20 2a 70 43  *db, CollSeq *pC
2532e 6f 6c 6c 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20  oll){.  CollSeq 
2532f 2a 70 43 6f 6c 6c 32 3b 0a 20 20 63 68 61 72 20  *pColl2;.  char 
25330 2a 7a 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  *z = pColl->zNam
25331 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  e;.  int i;.  st
25332 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 61 45  atic const u8 aE
25333 6e 63 5b 5d 20 3d 20 7b 20 53 51 4c 49 54 45 5f  nc[] = { SQLITE_
25334 55 54 46 31 36 42 45 2c 20 53 51 4c 49 54 45 5f  UTF16BE, SQLITE_
25335 55 54 46 31 36 4c 45 2c 20 53 51 4c 49 54 45 5f  UTF16LE, SQLITE_
25336 55 54 46 38 20 7d 3b 0a 20 20 66 6f 72 28 69 3d  UTF8 };.  for(i=
25337 30 3b 20 69 3c 33 3b 20 69 2b 2b 29 7b 0a 20 20  0; i<3; i++){.  
25338 20 20 70 43 6f 6c 6c 32 20 3d 20 73 71 6c 69 74    pColl2 = sqlit
25339 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62  e3FindCollSeq(db
2533a 2c 20 61 45 6e 63 5b 69 5d 2c 20 7a 2c 20 30 29  , aEnc[i], z, 0)
2533b 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 32  ;.    if( pColl2
2533c 2d 3e 78 43 6d 70 21 3d 30 20 29 7b 0a 20 20 20  ->xCmp!=0 ){.   
2533d 20 20 20 6d 65 6d 63 70 79 28 70 43 6f 6c 6c 2c     memcpy(pColl,
2533e 20 70 43 6f 6c 6c 32 2c 20 73 69 7a 65 6f 66 28   pColl2, sizeof(
2533f 43 6f 6c 6c 53 65 71 29 29 3b 0a 20 20 20 20 20  CollSeq));.     
25340 20 70 43 6f 6c 6c 2d 3e 78 44 65 6c 20 3d 20 30   pColl->xDel = 0
25341 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20  ;         /* Do 
25342 6e 6f 74 20 63 6f 70 79 20 74 68 65 20 64 65 73  not copy the des
25343 74 72 75 63 74 6f 72 20 2a 2f 0a 20 20 20 20 20  tructor */.     
25344 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
25345 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  K;.    }.  }.  r
25346 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
25347 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  OR;.}../*.** Thi
25348 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65  s function is re
25349 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 69 6e  sponsible for in
2534a 76 6f 6b 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61  voking the colla
2534b 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 63 61 6c  tion factory cal
2534c 6c 62 61 63 6b 0a 2a 2a 20 6f 72 20 73 75 62 73  lback.** or subs
2534d 74 69 74 75 74 69 6e 67 20 61 20 63 6f 6c 6c 61  tituting a colla
2534e 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6f 66  tion sequence of
2534f 20 61 20 64 69 66 66 65 72 65 6e 74 20 65 6e 63   a different enc
25350 6f 64 69 6e 67 20 77 68 65 6e 20 74 68 65 0a 2a  oding when the.*
25351 2a 20 72 65 71 75 65 73 74 65 64 20 63 6f 6c 6c  * requested coll
25352 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69  ation sequence i
25353 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20  s not available 
25354 69 6e 20 74 68 65 20 64 65 73 69 72 65 64 20 65  in the desired e
25355 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 20 0a 2a 2a 20  ncoding..** .** 
25356 49 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c  If it is not NUL
25357 4c 2c 20 74 68 65 6e 20 70 43 6f 6c 6c 20 6d 75  L, then pColl mu
25358 73 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  st point to the 
25359 64 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 20  database native 
2535a 65 6e 63 6f 64 69 6e 67 20 0a 2a 2a 20 63 6f 6c  encoding .** col
2535b 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
2535c 77 69 74 68 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c  with name zName,
2535d 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a   length nName..*
2535e 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20  *.** The return 
2535f 76 61 6c 75 65 20 69 73 20 65 69 74 68 65 72 20  value is either 
25360 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  the collation se
25361 71 75 65 6e 63 65 20 74 6f 20 62 65 20 75 73 65  quence to be use
25362 64 20 69 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a  d in database.**
25363 20 64 62 20 66 6f 72 20 63 6f 6c 6c 61 74 69 6f   db for collatio
25364 6e 20 74 79 70 65 20 6e 61 6d 65 20 7a 4e 61 6d  n type name zNam
25365 65 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65 2c  e, length nName,
25366 20 6f 72 20 4e 55 4c 4c 2c 20 69 66 20 6e 6f 20   or NULL, if no 
25367 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71  collation.** seq
25368 75 65 6e 63 65 20 63 61 6e 20 62 65 20 66 6f 75  uence can be fou
25369 6e 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  nd..**.** See al
2536a 73 6f 3a 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  so: sqlite3Locat
2536b 65 43 6f 6c 6c 53 65 71 28 29 2c 20 73 71 6c 69  eCollSeq(), sqli
2536c 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 29  te3FindCollSeq()
2536d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2536e 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69  TE CollSeq *sqli
2536f 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 0a 20  te3GetCollSeq(. 
25370 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 20 20   sqlite3* db,   
25371 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
25372 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
25373 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20  n */.  u8 enc,  
25374 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
25375 54 68 65 20 64 65 73 69 72 65 64 20 65 6e 63 6f  The desired enco
25376 64 69 6e 67 20 66 6f 72 20 74 68 65 20 63 6f 6c  ding for the col
25377 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
25378 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43  */.  CollSeq *pC
25379 6f 6c 6c 2c 20 20 20 20 20 20 20 2f 2a 20 43 6f  oll,       /* Co
2537a 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2537b 20 77 69 74 68 20 6e 61 74 69 76 65 20 65 6e 63   with native enc
2537c 6f 64 69 6e 67 2c 20 6f 72 20 4e 55 4c 4c 20 2a  oding, or NULL *
2537d 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
2537e 7a 4e 61 6d 65 20 20 20 20 20 2f 2a 20 43 6f 6c  zName     /* Col
2537f 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
25380 6e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 43 6f 6c  name */.){.  Col
25381 6c 53 65 71 20 2a 70 3b 0a 0a 20 20 70 20 3d 20  lSeq *p;..  p = 
25382 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 21 70 20  pColl;.  if( !p 
25383 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74  ){.    p = sqlit
25384 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62  e3FindCollSeq(db
25385 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c 20 30 29  , enc, zName, 0)
25386 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 20 7c  ;.  }.  if( !p |
25387 7c 20 21 70 2d 3e 78 43 6d 70 20 29 7b 0a 20 20  | !p->xCmp ){.  
25388 20 20 2f 2a 20 4e 6f 20 63 6f 6c 6c 61 74 69 6f    /* No collatio
25389 6e 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74 68  n sequence of th
2538a 69 73 20 74 79 70 65 20 66 6f 72 20 74 68 69 73  is type for this
2538b 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 72 65 67   encoding is reg
2538c 69 73 74 65 72 65 64 2e 0a 20 20 20 20 2a 2a 20  istered..    ** 
2538d 43 61 6c 6c 20 74 68 65 20 63 6f 6c 6c 61 74 69  Call the collati
2538e 6f 6e 20 66 61 63 74 6f 72 79 20 74 6f 20 73 65  on factory to se
2538f 65 20 69 66 20 69 74 20 63 61 6e 20 73 75 70 70  e if it can supp
25390 6c 79 20 75 73 20 77 69 74 68 20 6f 6e 65 2e 0a  ly us with one..
25391 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 6c 6c 43      */.    callC
25392 6f 6c 6c 4e 65 65 64 65 64 28 64 62 2c 20 65 6e  ollNeeded(db, en
25393 63 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70  c, zName);.    p
25394 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f   = sqlite3FindCo
25395 6c 6c 53 65 71 28 64 62 2c 20 65 6e 63 2c 20 7a  llSeq(db, enc, z
25396 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  Name, 0);.  }.  
25397 69 66 28 20 70 20 26 26 20 21 70 2d 3e 78 43 6d  if( p && !p->xCm
25398 70 20 26 26 20 73 79 6e 74 68 43 6f 6c 6c 53 65  p && synthCollSe
25399 71 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20  q(db, p) ){.    
2539a 70 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73  p = 0;.  }.  ass
2539b 65 72 74 28 20 21 70 20 7c 7c 20 70 2d 3e 78 43  ert( !p || p->xC
2539c 6d 70 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  mp );.  return p
2539d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
2539e 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
2539f 64 20 6f 6e 20 61 20 63 6f 6c 6c 61 74 69 6f 6e  d on a collation
253a0 20 73 65 71 75 65 6e 63 65 20 62 65 66 6f 72 65   sequence before
253a1 20 69 74 20 69 73 20 75 73 65 64 20 74 6f 0a 2a   it is used to.*
253a2 2a 20 63 68 65 63 6b 20 74 68 61 74 20 69 74 20  * check that it 
253a3 69 73 20 64 65 66 69 6e 65 64 2e 20 41 6e 20 75  is defined. An u
253a4 6e 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69  ndefined collati
253a5 6f 6e 20 73 65 71 75 65 6e 63 65 20 65 78 69 73  on sequence exis
253a6 74 73 20 77 68 65 6e 0a 2a 2a 20 61 20 64 61 74  ts when.** a dat
253a7 61 62 61 73 65 20 69 73 20 6c 6f 61 64 65 64 20  abase is loaded 
253a8 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 72 65  that contains re
253a9 66 65 72 65 6e 63 65 73 20 74 6f 20 63 6f 6c 6c  ferences to coll
253aa 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 0a  ation sequences.
253ab 2a 2a 20 74 68 61 74 20 68 61 76 65 20 6e 6f 74  ** that have not
253ac 20 62 65 65 6e 20 64 65 66 69 6e 65 64 20 62 79   been defined by
253ad 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
253ae 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 65 74 63 2e  collation() etc.
253af 0a 2a 2a 0a 2a 2a 20 49 66 20 72 65 71 75 69 72  .**.** If requir
253b0 65 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ed, this routine
253b1 20 63 61 6c 6c 73 20 74 68 65 20 27 63 6f 6c 6c   calls the 'coll
253b2 61 74 69 6f 6e 20 6e 65 65 64 65 64 27 20 63 61  ation needed' ca
253b3 6c 6c 62 61 63 6b 20 74 6f 0a 2a 2a 20 72 65 71  llback to.** req
253b4 75 65 73 74 20 61 20 64 65 66 69 6e 69 74 69 6f  uest a definitio
253b5 6e 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69  n of the collati
253b6 6e 67 20 73 65 71 75 65 6e 63 65 2e 20 49 66 20  ng sequence. If 
253b7 74 68 69 73 20 64 6f 65 73 6e 27 74 20 77 6f 72  this doesn't wor
253b8 6b 2c 20 0a 2a 2a 20 61 6e 20 65 71 75 69 76 61  k, .** an equiva
253b9 6c 65 6e 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73  lent collating s
253ba 65 71 75 65 6e 63 65 20 74 68 61 74 20 75 73 65  equence that use
253bb 73 20 61 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  s a text encodin
253bc 67 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66  g different.** f
253bd 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64 61 74  rom the main dat
253be 61 62 61 73 65 20 69 73 20 73 75 62 73 74 69 74  abase is substit
253bf 75 74 65 64 2c 20 69 66 20 6f 6e 65 20 69 73 20  uted, if one is 
253c0 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 53 51  available..*/.SQ
253c1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
253c2 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c   sqlite3CheckCol
253c3 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 72  lSeq(Parse *pPar
253c4 73 65 2c 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  se, CollSeq *pCo
253c5 6c 6c 29 7b 0a 20 20 69 66 28 20 70 43 6f 6c 6c  ll){.  if( pColl
253c6 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   ){.    const ch
253c7 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 43 6f 6c  ar *zName = pCol
253c8 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 73 71  l->zName;.    sq
253c9 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
253ca 73 65 2d 3e 64 62 3b 0a 20 20 20 20 43 6f 6c 6c  se->db;.    Coll
253cb 53 65 71 20 2a 70 20 3d 20 73 71 6c 69 74 65 33  Seq *p = sqlite3
253cc 47 65 74 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45  GetCollSeq(db, E
253cd 4e 43 28 64 62 29 2c 20 70 43 6f 6c 6c 2c 20 7a  NC(db), pColl, z
253ce 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21  Name);.    if( !
253cf 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  p ){.      sqlit
253d0 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
253d1 65 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 6c  e, "no such coll
253d2 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20  ation sequence: 
253d3 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  %s", zName);.   
253d4 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b     pParse->nErr+
253d5 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  +;.      return 
253d6 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
253d7 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
253d8 70 3d 3d 70 43 6f 6c 6c 20 29 3b 0a 20 20 7d 0a  p==pColl );.  }.
253d9 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
253da 4f 4b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4c  OK;.}..../*.** L
253db 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75 72 6e  ocate and return
253dc 20 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20 74   an entry from t
253dd 68 65 20 64 62 2e 61 43 6f 6c 6c 53 65 71 20 68  he db.aCollSeq h
253de 61 73 68 20 74 61 62 6c 65 2e 20 49 66 20 74 68  ash table. If th
253df 65 20 65 6e 74 72 79 0a 2a 2a 20 73 70 65 63 69  e entry.** speci
253e0 66 69 65 64 20 62 79 20 7a 4e 61 6d 65 20 61 6e  fied by zName an
253e1 64 20 6e 4e 61 6d 65 20 69 73 20 6e 6f 74 20 66  d nName is not f
253e2 6f 75 6e 64 20 61 6e 64 20 70 61 72 61 6d 65 74  ound and paramet
253e3 65 72 20 27 63 72 65 61 74 65 27 20 69 73 0a 2a  er 'create' is.*
253e4 2a 20 74 72 75 65 2c 20 74 68 65 6e 20 63 72 65  * true, then cre
253e5 61 74 65 20 61 20 6e 65 77 20 65 6e 74 72 79 2e  ate a new entry.
253e6 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72   Otherwise retur
253e7 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 45 61  n NULL..**.** Ea
253e8 63 68 20 70 6f 69 6e 74 65 72 20 73 74 6f 72 65  ch pointer store
253e9 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33  d in the sqlite3
253ea 2e 61 43 6f 6c 6c 53 65 71 20 68 61 73 68 20 74  .aCollSeq hash t
253eb 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e  able contains an
253ec 0a 2a 2a 20 61 72 72 61 79 20 6f 66 20 74 68 72  .** array of thr
253ed 65 65 20 43 6f 6c 6c 53 65 71 20 73 74 72 75 63  ee CollSeq struc
253ee 74 75 72 65 73 2e 20 54 68 65 20 66 69 72 73 74  tures. The first
253ef 20 69 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f   is the collatio
253f0 6e 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 70 72  n sequence.** pr
253f1 65 66 66 65 72 72 65 64 20 66 6f 72 20 55 54 46  efferred for UTF
253f2 2d 38 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 55  -8, the second U
253f3 54 46 2d 31 36 6c 65 2c 20 61 6e 64 20 74 68 65  TF-16le, and the
253f4 20 74 68 69 72 64 20 55 54 46 2d 31 36 62 65 2e   third UTF-16be.
253f5 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 64 20 69 6d  .**.** Stored im
253f6 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20  mediately after 
253f7 74 68 65 20 74 68 72 65 65 20 63 6f 6c 6c 61 74  the three collat
253f8 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 69 73  ion sequences is
253f9 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68   a copy of.** th
253fa 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
253fb 65 6e 63 65 20 6e 61 6d 65 2e 20 41 20 70 6f 69  ence name. A poi
253fc 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72  nter to this str
253fd 69 6e 67 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ing is stored in
253fe 0a 2a 2a 20 65 61 63 68 20 63 6f 6c 6c 61 74 69  .** each collati
253ff 6f 6e 20 73 65 71 75 65 6e 63 65 20 73 74 72 75  on sequence stru
25400 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  cture..*/.static
25401 20 43 6f 6c 6c 53 65 71 20 2a 66 69 6e 64 43 6f   CollSeq *findCo
25402 6c 6c 53 65 71 45 6e 74 72 79 28 0a 20 20 73 71  llSeqEntry(.  sq
25403 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
25404 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
25405 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
25406 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
25407 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  e,    /* Name of
25408 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   the collating s
25409 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20 69 6e 74  equence */.  int
2540a 20 63 72 65 61 74 65 20 20 20 20 20 20 20 20 20   create         
2540b 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 6e     /* Create a n
2540c 65 77 20 65 6e 74 72 79 20 69 66 20 74 72 75 65  ew entry if true
2540d 20 2a 2f 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71   */.){.  CollSeq
2540e 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 6e 74 20 6e   *pColl;.  int n
2540f 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74  Name = sqlite3St
25410 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20  rlen30(zName);. 
25411 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
25412 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 43  HashFind(&db->aC
25413 6f 6c 6c 53 65 71 2c 20 7a 4e 61 6d 65 2c 20 6e  ollSeq, zName, n
25414 4e 61 6d 65 29 3b 0a 0a 20 20 69 66 28 20 30 3d  Name);..  if( 0=
25415 3d 70 43 6f 6c 6c 20 26 26 20 63 72 65 61 74 65  =pColl && create
25416 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20   ){.    pColl = 
25417 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
25418 65 72 6f 28 64 62 2c 20 33 2a 73 69 7a 65 6f 66  ero(db, 3*sizeof
25419 28 2a 70 43 6f 6c 6c 29 20 2b 20 6e 4e 61 6d 65  (*pColl) + nName
2541a 20 2b 20 31 20 29 3b 0a 20 20 20 20 69 66 28 20   + 1 );.    if( 
2541b 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 43  pColl ){.      C
2541c 6f 6c 6c 53 65 71 20 2a 70 44 65 6c 20 3d 20 30  ollSeq *pDel = 0
2541d 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30 5d  ;.      pColl[0]
2541e 2e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29  .zName = (char*)
2541f 26 70 43 6f 6c 6c 5b 33 5d 3b 0a 20 20 20 20 20  &pColl[3];.     
25420 20 70 43 6f 6c 6c 5b 30 5d 2e 65 6e 63 20 3d 20   pColl[0].enc = 
25421 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20  SQLITE_UTF8;.   
25422 20 20 20 70 43 6f 6c 6c 5b 31 5d 2e 7a 4e 61 6d     pColl[1].zNam
25423 65 20 3d 20 28 63 68 61 72 2a 29 26 70 43 6f 6c  e = (char*)&pCol
25424 6c 5b 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c  l[3];.      pCol
25425 6c 5b 31 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54  l[1].enc = SQLIT
25426 45 5f 55 54 46 31 36 4c 45 3b 0a 20 20 20 20 20  E_UTF16LE;.     
25427 20 70 43 6f 6c 6c 5b 32 5d 2e 7a 4e 61 6d 65 20   pColl[2].zName 
25428 3d 20 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b  = (char*)&pColl[
25429 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b  3];.      pColl[
2542a 32 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  2].enc = SQLITE_
2542b 55 54 46 31 36 42 45 3b 0a 20 20 20 20 20 20 6d  UTF16BE;.      m
2542c 65 6d 63 70 79 28 70 43 6f 6c 6c 5b 30 5d 2e 7a  emcpy(pColl[0].z
2542d 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  Name, zName, nNa
2542e 6d 65 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c  me);.      pColl
2542f 5b 30 5d 2e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d  [0].zName[nName]
25430 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 44 65 6c   = 0;.      pDel
25431 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e   = sqlite3HashIn
25432 73 65 72 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53  sert(&db->aCollS
25433 65 71 2c 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61  eq, pColl[0].zNa
25434 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70 43 6f 6c 6c  me, nName, pColl
25435 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  );..      /* If 
25436 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75  a malloc() failu
25437 72 65 20 6f 63 63 75 72 72 65 64 20 69 6e 20 73  re occurred in s
25438 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74  qlite3HashInsert
25439 28 29 2c 20 69 74 20 77 69 6c 6c 20 0a 20 20 20  (), it will .   
2543a 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65     ** return the
2543b 20 70 43 6f 6c 6c 20 70 6f 69 6e 74 65 72 20 74   pColl pointer t
2543c 6f 20 62 65 20 64 65 6c 65 74 65 64 20 28 62 65  o be deleted (be
2543d 63 61 75 73 65 20 69 74 20 77 61 73 6e 27 74 20  cause it wasn't 
2543e 61 64 64 65 64 0a 20 20 20 20 20 20 2a 2a 20 74  added.      ** t
2543f 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  o the hash table
25440 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  )..      */.    
25441 20 20 61 73 73 65 72 74 28 20 70 44 65 6c 3d 3d    assert( pDel==
25442 30 20 7c 7c 20 70 44 65 6c 3d 3d 70 43 6f 6c 6c  0 || pDel==pColl
25443 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 44   );.      if( pD
25444 65 6c 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  el!=0 ){.       
25445 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
25446 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 73  d = 1;.        s
25447 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
25448 20 70 44 65 6c 29 3b 0a 20 20 20 20 20 20 20 20   pDel);.        
25449 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20  pColl = 0;.     
2544a 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
2544b 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a  eturn pColl;.}..
2544c 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20  /*.** Parameter 
2544d 7a 4e 61 6d 65 20 70 6f 69 6e 74 73 20 74 6f 20  zName points to 
2544e 61 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  a UTF-8 encoded 
2544f 73 74 72 69 6e 67 20 6e 4e 61 6d 65 20 62 79 74  string nName byt
25450 65 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 52 65 74 75  es long..** Retu
25451 72 6e 20 74 68 65 20 43 6f 6c 6c 53 65 71 2a 20  rn the CollSeq* 
25452 70 6f 69 6e 74 65 72 20 66 6f 72 20 74 68 65 20  pointer for the 
25453 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
25454 63 65 20 6e 61 6d 65 64 20 7a 4e 61 6d 65 0a 2a  ce named zName.*
25455 2a 20 66 6f 72 20 74 68 65 20 65 6e 63 6f 64 69  * for the encodi
25456 6e 67 20 27 65 6e 63 27 20 66 72 6f 6d 20 74 68  ng 'enc' from th
25457 65 20 64 61 74 61 62 61 73 65 20 27 64 62 27 2e  e database 'db'.
25458 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 6e  .**.** If the en
25459 74 72 79 20 73 70 65 63 69 66 69 65 64 20 69 73  try specified is
2545a 20 6e 6f 74 20 66 6f 75 6e 64 20 61 6e 64 20 27   not found and '
2545b 63 72 65 61 74 65 27 20 69 73 20 74 72 75 65 2c  create' is true,
2545c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 0a 2a   then create a.*
2545d 2a 20 6e 65 77 20 65 6e 74 72 79 2e 20 20 4f 74  * new entry.  Ot
2545e 68 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 4e  herwise return N
2545f 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 65 70  ULL..**.** A sep
25460 61 72 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 73  arate function s
25461 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c  qlite3LocateColl
25462 53 65 71 28 29 20 69 73 20 61 20 77 72 61 70 70  Seq() is a wrapp
25463 65 72 20 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 69  er around.** thi
25464 73 20 72 6f 75 74 69 6e 65 2e 20 20 73 71 6c 69  s routine.  sqli
25465 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71  te3LocateCollSeq
25466 28 29 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63  () invokes the c
25467 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79  ollation factory
25468 0a 2a 2a 20 69 66 20 6e 65 63 65 73 73 61 72 79  .** if necessary
25469 20 61 6e 64 20 67 65 6e 65 72 61 74 65 73 20 61   and generates a
2546a 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
2546b 69 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67  if the collating
2546c 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 63 61 6e   sequence.** can
2546d 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2a  not be found..**
2546e 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 73 71  .** See also: sq
2546f 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53  lite3LocateCollS
25470 65 71 28 29 2c 20 73 71 6c 69 74 65 33 47 65 74  eq(), sqlite3Get
25471 43 6f 6c 6c 53 65 71 28 29 0a 2a 2f 0a 53 51 4c  CollSeq().*/.SQL
25472 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c  ITE_PRIVATE Coll
25473 53 65 71 20 2a 73 71 6c 69 74 65 33 46 69 6e 64  Seq *sqlite3Find
25474 43 6f 6c 6c 53 65 71 28 0a 20 20 73 71 6c 69 74  CollSeq(.  sqlit
25475 65 33 20 2a 64 62 2c 0a 20 20 75 38 20 65 6e 63  e3 *db,.  u8 enc
25476 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
25477 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 63 72 65  zName,.  int cre
25478 61 74 65 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71  ate.){.  CollSeq
25479 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 7a   *pColl;.  if( z
2547a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 70 43 6f 6c  Name ){.    pCol
2547b 6c 20 3d 20 66 69 6e 64 43 6f 6c 6c 53 65 71 45  l = findCollSeqE
2547c 6e 74 72 79 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  ntry(db, zName, 
2547d 63 72 65 61 74 65 29 3b 0a 20 20 7d 65 6c 73 65  create);.  }else
2547e 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62  {.    pColl = db
2547f 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 7d  ->pDfltColl;.  }
25480 0a 20 20 61 73 73 65 72 74 28 20 53 51 4c 49 54  .  assert( SQLIT
25481 45 5f 55 54 46 38 3d 3d 31 20 26 26 20 53 51 4c  E_UTF8==1 && SQL
25482 49 54 45 5f 55 54 46 31 36 4c 45 3d 3d 32 20 26  ITE_UTF16LE==2 &
25483 26 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  & SQLITE_UTF16BE
25484 3d 3d 33 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==3 );.  assert(
25485 20 65 6e 63 3e 3d 53 51 4c 49 54 45 5f 55 54 46   enc>=SQLITE_UTF
25486 38 20 26 26 20 65 6e 63 3c 3d 53 51 4c 49 54 45  8 && enc<=SQLITE
25487 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66  _UTF16BE );.  if
25488 28 20 70 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c 20  ( pColl ) pColl 
25489 2b 3d 20 65 6e 63 2d 31 3b 0a 20 20 72 65 74 75  += enc-1;.  retu
2548a 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 20  rn pColl;.}../* 
2548b 44 75 72 69 6e 67 20 74 68 65 20 73 65 61 72 63  During the searc
2548c 68 20 66 6f 72 20 74 68 65 20 62 65 73 74 20 66  h for the best f
2548d 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69  unction definiti
2548e 6f 6e 2c 20 74 68 69 73 20 70 72 6f 63 65 64 75  on, this procedu
2548f 72 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20  re.** is called 
25490 74 6f 20 74 65 73 74 20 68 6f 77 20 77 65 6c 6c  to test how well
25491 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 61   the function pa
25492 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73  ssed as the firs
25493 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 6d 61  t argument.** ma
25494 74 63 68 65 73 20 74 68 65 20 72 65 71 75 65 73  tches the reques
25495 74 20 66 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e  t for a function
25496 20 77 69 74 68 20 6e 41 72 67 20 61 72 67 75 6d   with nArg argum
25497 65 6e 74 73 20 69 6e 20 61 20 73 79 73 74 65 6d  ents in a system
25498 0a 2a 2a 20 74 68 61 74 20 75 73 65 73 20 65 6e  .** that uses en
25499 63 6f 64 69 6e 67 20 65 6e 63 2e 20 54 68 65 20  coding enc. The 
2549a 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69  value returned i
2549b 6e 64 69 63 61 74 65 73 20 68 6f 77 20 77 65 6c  ndicates how wel
2549c 6c 20 74 68 65 0a 2a 2a 20 72 65 71 75 65 73 74  l the.** request
2549d 20 69 73 20 6d 61 74 63 68 65 64 2e 20 41 20 68   is matched. A h
2549e 69 67 68 65 72 20 76 61 6c 75 65 20 69 6e 64 69  igher value indi
2549f 63 61 74 65 73 20 61 20 62 65 74 74 65 72 20 6d  cates a better m
254a0 61 74 63 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  atch..**.** The 
254a1 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69  returned value i
254a2 73 20 61 6c 77 61 79 73 20 62 65 74 77 65 65 6e  s always between
254a3 20 30 20 61 6e 64 20 36 2c 20 61 73 20 66 6f 6c   0 and 6, as fol
254a4 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 30 3a 20 4e  lows:.**.** 0: N
254a5 6f 74 20 61 20 6d 61 74 63 68 2c 20 6f 72 20 69  ot a match, or i
254a6 66 20 6e 41 72 67 3c 30 20 61 6e 64 20 74 68 65  f nArg<0 and the
254a7 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 68 61 73   function is has
254a8 20 6e 6f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69   no implementati
254a9 6f 6e 2e 0a 2a 2a 20 31 3a 20 41 20 76 61 72 69  on..** 1: A vari
254aa 61 62 6c 65 20 61 72 67 75 6d 65 6e 74 73 20 66  able arguments f
254ab 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 70 72 65  unction that pre
254ac 66 65 72 73 20 55 54 46 2d 38 20 77 68 65 6e 20  fers UTF-8 when 
254ad 61 20 55 54 46 2d 31 36 0a 2a 2a 20 20 20 20 65  a UTF-16.**    e
254ae 6e 63 6f 64 69 6e 67 20 69 73 20 72 65 71 75 65  ncoding is reque
254af 73 74 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65  sted, or vice ve
254b0 72 73 61 2e 0a 2a 2a 20 32 3a 20 41 20 76 61 72  rsa..** 2: A var
254b1 69 61 62 6c 65 20 61 72 67 75 6d 65 6e 74 73 20  iable arguments 
254b2 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 75 73  function that us
254b3 65 73 20 55 54 46 2d 31 36 42 45 20 77 68 65 6e  es UTF-16BE when
254b4 20 55 54 46 2d 31 36 4c 45 20 69 73 0a 2a 2a 20   UTF-16LE is.** 
254b5 20 20 20 72 65 71 75 65 73 74 65 64 2c 20 6f 72     requested, or
254b6 20 76 69 63 65 20 76 65 72 73 61 2e 0a 2a 2a 20   vice versa..** 
254b7 33 3a 20 41 20 76 61 72 69 61 62 6c 65 20 61 72  3: A variable ar
254b8 67 75 6d 65 6e 74 73 20 66 75 6e 63 74 69 6f 6e  guments function
254b9 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20   using the same 
254ba 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a  text encoding..*
254bb 2a 20 34 3a 20 41 20 66 75 6e 63 74 69 6f 6e 20  * 4: A function 
254bc 77 69 74 68 20 74 68 65 20 65 78 61 63 74 20 6e  with the exact n
254bd 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
254be 74 73 20 72 65 71 75 65 73 74 65 64 20 74 68 61  ts requested tha
254bf 74 0a 2a 2a 20 20 20 20 70 72 65 66 65 72 73 20  t.**    prefers 
254c0 55 54 46 2d 38 20 77 68 65 6e 20 61 20 55 54 46  UTF-8 when a UTF
254c1 2d 31 36 20 65 6e 63 6f 64 69 6e 67 20 69 73 20  -16 encoding is 
254c2 72 65 71 75 65 73 74 65 64 2c 20 6f 72 20 76 69  requested, or vi
254c3 63 65 20 76 65 72 73 61 2e 0a 2a 2a 20 35 3a 20  ce versa..** 5: 
254c4 41 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  A function with 
254c5 74 68 65 20 65 78 61 63 74 20 6e 75 6d 62 65 72  the exact number
254c6 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 72 65   of arguments re
254c7 71 75 65 73 74 65 64 20 74 68 61 74 0a 2a 2a 20  quested that.** 
254c8 20 20 20 70 72 65 66 65 72 73 20 55 54 46 2d 31     prefers UTF-1
254c9 36 4c 45 20 77 68 65 6e 20 55 54 46 2d 31 36 42  6LE when UTF-16B
254ca 45 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20  E is requested, 
254cb 6f 72 20 76 69 63 65 20 76 65 72 73 61 2e 0a 2a  or vice versa..*
254cc 2a 20 36 3a 20 41 6e 20 65 78 61 63 74 20 6d 61  * 6: An exact ma
254cd 74 63 68 2e 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69  tch..**.*/.stati
254ce 63 20 69 6e 74 20 6d 61 74 63 68 51 75 61 6c 69  c int matchQuali
254cf 74 79 28 46 75 6e 63 44 65 66 20 2a 70 2c 20 69  ty(FuncDef *p, i
254d0 6e 74 20 6e 41 72 67 2c 20 75 38 20 65 6e 63 29  nt nArg, u8 enc)
254d1 7b 0a 20 20 69 6e 74 20 6d 61 74 63 68 20 3d 20  {.  int match = 
254d2 30 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 41 72 67  0;.  if( p->nArg
254d3 3d 3d 2d 31 20 7c 7c 20 70 2d 3e 6e 41 72 67 3d  ==-1 || p->nArg=
254d4 3d 6e 41 72 67 20 0a 20 20 20 7c 7c 20 28 6e 41  =nArg .   || (nA
254d5 72 67 3d 3d 2d 31 20 26 26 20 28 70 2d 3e 78 46  rg==-1 && (p->xF
254d6 75 6e 63 21 3d 30 20 7c 7c 20 70 2d 3e 78 53 74  unc!=0 || p->xSt
254d7 65 70 21 3d 30 29 29 0a 20 20 29 7b 0a 20 20 20  ep!=0)).  ){.   
254d8 20 6d 61 74 63 68 20 3d 20 31 3b 0a 20 20 20 20   match = 1;.    
254d9 69 66 28 20 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72  if( p->nArg==nAr
254da 67 20 7c 7c 20 6e 41 72 67 3d 3d 2d 31 20 29 7b  g || nArg==-1 ){
254db 0a 20 20 20 20 20 20 6d 61 74 63 68 20 3d 20 34  .      match = 4
254dc 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
254dd 65 6e 63 3d 3d 70 2d 3e 69 50 72 65 66 45 6e 63  enc==p->iPrefEnc
254de 20 29 7b 0a 20 20 20 20 20 20 6d 61 74 63 68 20   ){.      match 
254df 2b 3d 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20  += 2;.    }.    
254e0 65 6c 73 65 20 69 66 28 20 28 65 6e 63 3d 3d 53  else if( (enc==S
254e1 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 26 26  QLITE_UTF16LE &&
254e2 20 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d 53 51   p->iPrefEnc==SQ
254e3 4c 49 54 45 5f 55 54 46 31 36 42 45 29 20 7c 7c  LITE_UTF16BE) ||
254e4 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 28 65  .             (e
254e5 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  nc==SQLITE_UTF16
254e6 42 45 20 26 26 20 70 2d 3e 69 50 72 65 66 45 6e  BE && p->iPrefEn
254e7 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  c==SQLITE_UTF16L
254e8 45 29 20 29 7b 0a 20 20 20 20 20 20 6d 61 74 63  E) ){.      matc
254e9 68 20 2b 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  h += 1;.    }.  
254ea 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61 74 63 68  }.  return match
254eb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63  ;.}../*.** Searc
254ec 68 20 61 20 46 75 6e 63 44 65 66 48 61 73 68 20  h a FuncDefHash 
254ed 66 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 77  for a function w
254ee 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e 61  ith the given na
254ef 6d 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61  me.  Return.** a
254f0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
254f1 6d 61 74 63 68 69 6e 67 20 46 75 6e 63 44 65 66  matching FuncDef
254f2 20 69 66 20 66 6f 75 6e 64 2c 20 6f 72 20 30 20   if found, or 0 
254f3 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d  if there is no m
254f4 61 74 63 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  atch..*/.static 
254f5 46 75 6e 63 44 65 66 20 2a 66 75 6e 63 74 69 6f  FuncDef *functio
254f6 6e 53 65 61 72 63 68 28 0a 20 20 46 75 6e 63 44  nSearch(.  FuncD
254f7 65 66 48 61 73 68 20 2a 70 48 61 73 68 2c 20 20  efHash *pHash,  
254f8 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 74 6f  /* Hash table to
254f9 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 69 6e 74   search */.  int
254fa 20 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   h,             
254fb 20 20 2f 2a 20 48 61 73 68 20 6f 66 20 74 68 65    /* Hash of the
254fc 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74   name */.  const
254fd 20 63 68 61 72 20 2a 7a 46 75 6e 63 2c 20 20 20   char *zFunc,   
254fe 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 75 6e 63 74  /* Name of funct
254ff 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75  ion */.  int nFu
25500 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  nc            /*
25501 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
25502 20 69 6e 20 7a 46 75 6e 63 20 2a 2f 0a 29 7b 0a   in zFunc */.){.
25503 20 20 46 75 6e 63 44 65 66 20 2a 70 3b 0a 20 20    FuncDef *p;.  
25504 66 6f 72 28 70 3d 70 48 61 73 68 2d 3e 61 5b 68  for(p=pHash->a[h
25505 5d 3b 20 70 3b 20 70 3d 70 2d 3e 70 48 61 73 68  ]; p; p=p->pHash
25506 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
25507 65 33 53 74 72 4e 49 43 6d 70 28 70 2d 3e 7a 4e  e3StrNICmp(p->zN
25508 61 6d 65 2c 20 7a 46 75 6e 63 2c 20 6e 46 75 6e  ame, zFunc, nFun
25509 63 29 3d 3d 30 20 26 26 20 70 2d 3e 7a 4e 61 6d  c)==0 && p->zNam
2550a 65 5b 6e 46 75 6e 63 5d 3d 3d 30 20 29 7b 0a 20  e[nFunc]==0 ){. 
2550b 20 20 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20       return p;. 
2550c 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
2550d 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  n 0;.}../*.** In
2550e 73 65 72 74 20 61 20 6e 65 77 20 46 75 6e 63 44  sert a new FuncD
2550f 65 66 20 69 6e 74 6f 20 61 20 46 75 6e 63 44 65  ef into a FuncDe
25510 66 48 61 73 68 20 68 61 73 68 20 74 61 62 6c 65  fHash hash table
25511 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
25512 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
25513 46 75 6e 63 44 65 66 49 6e 73 65 72 74 28 0a 20  FuncDefInsert(. 
25514 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48   FuncDefHash *pH
25515 61 73 68 2c 20 20 2f 2a 20 54 68 65 20 68 61 73  ash,  /* The has
25516 68 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69  h table into whi
25517 63 68 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a  ch to insert */.
25518 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 20    FuncDef *pDef 
25519 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75         /* The fu
2551a 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f  nction definitio
2551b 6e 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 29  n to insert */.)
2551c 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 4f 74  {.  FuncDef *pOt
2551d 68 65 72 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65  her;.  int nName
2551e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
2551f 33 30 28 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b  30(pDef->zName);
25520 0a 20 20 75 38 20 63 31 20 3d 20 28 75 38 29 70  .  u8 c1 = (u8)p
25521 44 65 66 2d 3e 7a 4e 61 6d 65 5b 30 5d 3b 0a 20  Def->zName[0];. 
25522 20 69 6e 74 20 68 20 3d 20 28 73 71 6c 69 74 65   int h = (sqlite
25523 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 63 31  3UpperToLower[c1
25524 5d 20 2b 20 6e 4e 61 6d 65 29 20 25 20 41 72 72  ] + nName) % Arr
25525 61 79 53 69 7a 65 28 70 48 61 73 68 2d 3e 61 29  aySize(pHash->a)
25526 3b 0a 20 20 70 4f 74 68 65 72 20 3d 20 66 75 6e  ;.  pOther = fun
25527 63 74 69 6f 6e 53 65 61 72 63 68 28 70 48 61 73  ctionSearch(pHas
25528 68 2c 20 68 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d  h, h, pDef->zNam
25529 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 69 66 28  e, nName);.  if(
2552a 20 70 4f 74 68 65 72 20 29 7b 0a 20 20 20 20 61   pOther ){.    a
2552b 73 73 65 72 74 28 20 70 4f 74 68 65 72 21 3d 70  ssert( pOther!=p
2552c 44 65 66 20 26 26 20 70 4f 74 68 65 72 2d 3e 70  Def && pOther->p
2552d 4e 65 78 74 21 3d 70 44 65 66 20 29 3b 0a 20 20  Next!=pDef );.  
2552e 20 20 70 44 65 66 2d 3e 70 4e 65 78 74 20 3d 20    pDef->pNext = 
2552f 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20  pOther->pNext;. 
25530 20 20 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74     pOther->pNext
25531 20 3d 20 70 44 65 66 3b 0a 20 20 7d 65 6c 73 65   = pDef;.  }else
25532 7b 0a 20 20 20 20 70 44 65 66 2d 3e 70 4e 65 78  {.    pDef->pNex
25533 74 20 3d 20 30 3b 0a 20 20 20 20 70 44 65 66 2d  t = 0;.    pDef-
25534 3e 70 48 61 73 68 20 3d 20 70 48 61 73 68 2d 3e  >pHash = pHash->
25535 61 5b 68 5d 3b 0a 20 20 20 20 70 48 61 73 68 2d  a[h];.    pHash-
25536 3e 61 5b 68 5d 20 3d 20 70 44 65 66 3b 0a 20 20  >a[h] = pDef;.  
25537 7d 0a 7d 0a 20 20 0a 20 20 0a 0a 2f 2a 0a 2a 2a  }.}.  .  ../*.**
25538 20 4c 6f 63 61 74 65 20 61 20 75 73 65 72 20 66   Locate a user f
25539 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 61 20  unction given a 
2553a 6e 61 6d 65 2c 20 61 20 6e 75 6d 62 65 72 20 6f  name, a number o
2553b 66 20 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 20  f arguments and 
2553c 61 20 66 6c 61 67 0a 2a 2a 20 69 6e 64 69 63 61  a flag.** indica
2553d 74 69 6e 67 20 77 68 65 74 68 65 72 20 74 68 65  ting whether the
2553e 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 66 65 72   function prefer
2553f 73 20 55 54 46 2d 31 36 20 6f 76 65 72 20 55 54  s UTF-16 over UT
25540 46 2d 38 2e 20 20 52 65 74 75 72 6e 20 61 0a 2a  F-8.  Return a.*
25541 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * pointer to the
25542 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75   FuncDef structu
25543 72 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20  re that defines 
25544 74 68 61 74 20 66 75 6e 63 74 69 6f 6e 2c 20 6f  that function, o
25545 72 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c  r return.** NULL
25546 20 69 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   if the function
25547 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e   does not exist.
25548 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 72  .**.** If the cr
25549 65 61 74 65 46 6c 61 67 20 61 72 67 75 6d 65 6e  eateFlag argumen
2554a 74 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  t is true, then 
2554b 61 20 6e 65 77 20 28 62 6c 61 6e 6b 29 20 46 75  a new (blank) Fu
2554c 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75 63 74 75  ncDef.** structu
2554d 72 65 20 69 73 20 63 72 65 61 74 65 64 20 61 6e  re is created an
2554e 64 20 6c 69 6b 65 64 20 69 6e 74 6f 20 74 68 65  d liked into the
2554f 20 22 64 62 22 20 73 74 72 75 63 74 75 72 65 20   "db" structure 
25550 69 66 20 61 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68  if a.** no match
25551 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 72 65  ing function pre
25552 76 69 6f 75 73 6c 79 20 65 78 69 73 74 65 64 2e  viously existed.
25553 20 20 57 68 65 6e 20 63 72 65 61 74 65 46 6c 61    When createFla
25554 67 20 69 73 20 74 72 75 65 0a 2a 2a 20 61 6e 64  g is true.** and
25555 20 74 68 65 20 6e 41 72 67 20 70 61 72 61 6d 65   the nArg parame
25556 74 65 72 20 69 73 20 2d 31 2c 20 74 68 65 6e 20  ter is -1, then 
25557 6f 6e 6c 79 20 61 20 66 75 6e 63 74 69 6f 6e 20  only a function 
25558 74 68 61 74 20 61 63 63 65 70 74 73 0a 2a 2a 20  that accepts.** 
25559 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 61 72  any number of ar
2555a 67 75 6d 65 6e 74 73 20 77 69 6c 6c 20 62 65 20  guments will be 
2555b 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
2555c 49 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 73  If createFlag is
2555d 20 66 61 6c 73 65 20 61 6e 64 20 6e 41 72 67 20   false and nArg 
2555e 69 73 20 2d 31 2c 20 74 68 65 6e 20 74 68 65 20  is -1, then the 
2555f 66 69 72 73 74 20 76 61 6c 69 64 0a 2a 2a 20 66  first valid.** f
25560 75 6e 63 74 69 6f 6e 20 66 6f 75 6e 64 20 69 73  unction found is
25561 20 72 65 74 75 72 6e 65 64 2e 20 20 41 20 66 75   returned.  A fu
25562 6e 63 74 69 6f 6e 20 69 73 20 76 61 6c 69 64 20  nction is valid 
25563 69 66 20 65 69 74 68 65 72 20 78 46 75 6e 63 0a  if either xFunc.
25564 2a 2a 20 6f 72 20 78 53 74 65 70 20 69 73 20 6e  ** or xStep is n
25565 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 49  on-zero..**.** I
25566 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 20  f createFlag is 
25567 66 61 6c 73 65 2c 20 74 68 65 6e 20 61 20 66 75  false, then a fu
25568 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20  nction with the 
25569 72 65 71 75 69 72 65 64 20 6e 61 6d 65 20 61 6e  required name an
2556a 64 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 61  d.** number of a
2556b 72 67 75 6d 65 6e 74 73 20 6d 61 79 20 62 65 20  rguments may be 
2556c 72 65 74 75 72 6e 65 64 20 65 76 65 6e 20 69 66  returned even if
2556d 20 74 68 65 20 65 54 65 78 74 52 65 70 20 66 6c   the eTextRep fl
2556e 61 67 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d  ag does not.** m
2556f 61 74 63 68 20 74 68 61 74 20 72 65 71 75 65 73  atch that reques
25570 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
25571 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a  RIVATE FuncDef *
25572 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74  sqlite3FindFunct
25573 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ion(.  sqlite3 *
25574 64 62 2c 20 20 20 20 20 20 20 2f 2a 20 41 6e 20  db,       /* An 
25575 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f  open database */
25576 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
25577 4e 61 6d 65 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66  Name, /* Name of
25578 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20   the function.  
25579 4e 6f 74 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61  Not null-termina
2557a 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61  ted */.  int nNa
2557b 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  me,         /* N
2557c 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
2557d 65 72 73 20 69 6e 20 74 68 65 20 6e 61 6d 65 20  ers in the name 
2557e 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20  */.  int nArg,  
2557f 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
25580 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20  r of arguments. 
25581 20 2d 31 20 6d 65 61 6e 73 20 61 6e 79 20 6e 75   -1 means any nu
25582 6d 62 65 72 20 2a 2f 0a 20 20 75 38 20 65 6e 63  mber */.  u8 enc
25583 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
25584 50 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65  Preferred text e
25585 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74  ncoding */.  int
25586 20 63 72 65 61 74 65 46 6c 61 67 20 20 20 20 20   createFlag     
25587 2f 2a 20 43 72 65 61 74 65 20 6e 65 77 20 65 6e  /* Create new en
25588 74 72 79 20 69 66 20 74 72 75 65 20 61 6e 64 20  try if true and 
25589 64 6f 65 73 20 6e 6f 74 20 6f 74 68 65 72 77 69  does not otherwi
2558a 73 65 20 65 78 69 73 74 20 2a 2f 0a 29 7b 0a 20  se exist */.){. 
2558b 20 46 75 6e 63 44 65 66 20 2a 70 3b 20 20 20 20   FuncDef *p;    
2558c 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
2558d 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 46   variable */.  F
2558e 75 6e 63 44 65 66 20 2a 70 42 65 73 74 20 3d 20  uncDef *pBest = 
2558f 30 3b 20 2f 2a 20 42 65 73 74 20 6d 61 74 63 68  0; /* Best match
25590 20 66 6f 75 6e 64 20 73 6f 20 66 61 72 20 2a 2f   found so far */
25591 0a 20 20 69 6e 74 20 62 65 73 74 53 63 6f 72 65  .  int bestScore
25592 20 3d 20 30 3b 20 20 2f 2a 20 53 63 6f 72 65 20   = 0;  /* Score 
25593 6f 66 20 62 65 73 74 20 6d 61 74 63 68 20 2a 2f  of best match */
25594 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20  .  int h;       
25595 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 76         /* Hash v
25596 61 6c 75 65 20 2a 2f 0a 0a 0a 20 20 61 73 73 65  alue */...  asse
25597 72 74 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f  rt( enc==SQLITE_
25598 55 54 46 38 20 7c 7c 20 65 6e 63 3d 3d 53 51 4c  UTF8 || enc==SQL
25599 49 54 45 5f 55 54 46 31 36 4c 45 20 7c 7c 20 65  ITE_UTF16LE || e
2559a 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  nc==SQLITE_UTF16
2559b 42 45 20 29 3b 0a 20 20 68 20 3d 20 28 73 71 6c  BE );.  h = (sql
2559c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72  ite3UpperToLower
2559d 5b 28 75 38 29 7a 4e 61 6d 65 5b 30 5d 5d 20 2b  [(u8)zName[0]] +
2559e 20 6e 4e 61 6d 65 29 20 25 20 41 72 72 61 79 53   nName) % ArrayS
2559f 69 7a 65 28 64 62 2d 3e 61 46 75 6e 63 2e 61 29  ize(db->aFunc.a)
255a0 3b 0a 0a 20 20 2f 2a 20 46 69 72 73 74 20 73 65  ;..  /* First se
255a1 61 72 63 68 20 66 6f 72 20 61 20 6d 61 74 63 68  arch for a match
255a2 20 61 6d 6f 6e 67 73 74 20 74 68 65 20 61 70 70   amongst the app
255a3 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
255a4 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f   functions..  */
255a5 0a 20 20 70 20 3d 20 66 75 6e 63 74 69 6f 6e 53  .  p = functionS
255a6 65 61 72 63 68 28 26 64 62 2d 3e 61 46 75 6e 63  earch(&db->aFunc
255a7 2c 20 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  , h, zName, nNam
255a8 65 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29  e);.  while( p )
255a9 7b 0a 20 20 20 20 69 6e 74 20 73 63 6f 72 65 20  {.    int score 
255aa 3d 20 6d 61 74 63 68 51 75 61 6c 69 74 79 28 70  = matchQuality(p
255ab 2c 20 6e 41 72 67 2c 20 65 6e 63 29 3b 0a 20 20  , nArg, enc);.  
255ac 20 20 69 66 28 20 73 63 6f 72 65 3e 62 65 73 74    if( score>best
255ad 53 63 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 70  Score ){.      p
255ae 42 65 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20  Best = p;.      
255af 62 65 73 74 53 63 6f 72 65 20 3d 20 73 63 6f 72  bestScore = scor
255b0 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d  e;.    }.    p =
255b1 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a   p->pNext;.  }..
255b2 20 20 2f 2a 20 49 66 20 6e 6f 20 6d 61 74 63 68    /* If no match
255b3 20 69 73 20 66 6f 75 6e 64 2c 20 73 65 61 72 63   is found, searc
255b4 68 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 66  h the built-in f
255b5 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20  unctions..  **. 
255b6 20 2a 2a 20 45 78 63 65 70 74 2c 20 69 66 20 63   ** Except, if c
255b7 72 65 61 74 65 46 6c 61 67 20 69 73 20 74 72 75  reateFlag is tru
255b8 65 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  e, that means th
255b9 61 74 20 77 65 20 61 72 65 20 74 72 79 69 6e 67  at we are trying
255ba 20 74 6f 0a 20 20 2a 2a 20 69 6e 73 74 61 6c 6c   to.  ** install
255bb 20 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 2e   a new function.
255bc 20 20 57 68 61 74 65 76 65 72 20 46 75 6e 63 44    Whatever FuncD
255bd 65 66 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ef structure is 
255be 72 65 74 75 72 6e 65 64 20 77 69 6c 6c 0a 20 20  returned will.  
255bf 2a 2a 20 68 61 76 65 20 66 69 65 6c 64 73 20 6f  ** have fields o
255c0 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20  verwritten with 
255c1 6e 65 77 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  new information 
255c2 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20  appropriate for 
255c3 74 68 65 0a 20 20 2a 2a 20 6e 65 77 20 66 75 6e  the.  ** new fun
255c4 63 74 69 6f 6e 2e 20 20 42 75 74 20 74 68 65 20  ction.  But the 
255c5 46 75 6e 63 44 65 66 73 20 66 6f 72 20 62 75 69  FuncDefs for bui
255c6 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 20  lt-in functions 
255c7 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79 2e 0a 20  are read-only.. 
255c8 20 2a 2a 20 53 6f 20 77 65 20 6d 75 73 74 20 6e   ** So we must n
255c9 6f 74 20 73 65 61 72 63 68 20 66 6f 72 20 62 75  ot search for bu
255ca 69 6c 74 2d 69 6e 73 20 77 68 65 6e 20 63 72 65  ilt-ins when cre
255cb 61 74 69 6e 67 20 61 20 6e 65 77 20 66 75 6e 63  ating a new func
255cc 74 69 6f 6e 2e 0a 20 20 2a 2f 20 0a 20 20 69 66  tion..  */ .  if
255cd 28 20 21 63 72 65 61 74 65 46 6c 61 67 20 26 26  ( !createFlag &&
255ce 20 21 70 42 65 73 74 20 29 7b 0a 20 20 20 20 46   !pBest ){.    F
255cf 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61 73  uncDefHash *pHas
255d0 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63  h = &GLOBAL(Func
255d1 44 65 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33  DefHash, sqlite3
255d2 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29  GlobalFunctions)
255d3 3b 0a 20 20 20 20 70 20 3d 20 66 75 6e 63 74 69  ;.    p = functi
255d4 6f 6e 53 65 61 72 63 68 28 70 48 61 73 68 2c 20  onSearch(pHash, 
255d5 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  h, zName, nName)
255d6 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 20 29  ;.    while( p )
255d7 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 63 6f 72  {.      int scor
255d8 65 20 3d 20 6d 61 74 63 68 51 75 61 6c 69 74 79  e = matchQuality
255d9 28 70 2c 20 6e 41 72 67 2c 20 65 6e 63 29 3b 0a  (p, nArg, enc);.
255da 20 20 20 20 20 20 69 66 28 20 73 63 6f 72 65 3e        if( score>
255db 62 65 73 74 53 63 6f 72 65 20 29 7b 0a 20 20 20  bestScore ){.   
255dc 20 20 20 20 20 70 42 65 73 74 20 3d 20 70 3b 0a       pBest = p;.
255dd 20 20 20 20 20 20 20 20 62 65 73 74 53 63 6f 72          bestScor
255de 65 20 3d 20 73 63 6f 72 65 3b 0a 20 20 20 20 20  e = score;.     
255df 20 7d 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e   }.      p = p->
255e0 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  pNext;.    }.  }
255e1 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 72  ..  /* If the cr
255e2 65 61 74 65 46 6c 61 67 20 70 61 72 61 6d 65 74  eateFlag paramet
255e3 65 72 20 69 73 20 74 72 75 65 20 61 6e 64 20 74  er is true and t
255e4 68 65 20 73 65 61 72 63 68 20 64 69 64 20 6e 6f  he search did no
255e5 74 20 72 65 76 65 61 6c 20 61 6e 0a 20 20 2a 2a  t reveal an.  **
255e6 20 65 78 61 63 74 20 6d 61 74 63 68 20 66 6f 72   exact match for
255e7 20 74 68 65 20 6e 61 6d 65 2c 20 6e 75 6d 62 65   the name, numbe
255e8 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 61  r of arguments a
255e9 6e 64 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65  nd encoding, the
255ea 6e 20 61 64 64 20 61 0a 20 20 2a 2a 20 6e 65 77  n add a.  ** new
255eb 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20 68 61   entry to the ha
255ec 73 68 20 74 61 62 6c 65 20 61 6e 64 20 72 65 74  sh table and ret
255ed 75 72 6e 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69  urn it..  */.  i
255ee 66 28 20 63 72 65 61 74 65 46 6c 61 67 20 26 26  f( createFlag &&
255ef 20 28 62 65 73 74 53 63 6f 72 65 3c 36 20 7c 7c   (bestScore<6 ||
255f0 20 70 42 65 73 74 2d 3e 6e 41 72 67 21 3d 6e 41   pBest->nArg!=nA
255f1 72 67 29 20 26 26 20 0a 20 20 20 20 20 20 28 70  rg) && .      (p
255f2 42 65 73 74 20 3d 20 73 71 6c 69 74 65 33 44 62  Best = sqlite3Db
255f3 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
255f4 69 7a 65 6f 66 28 2a 70 42 65 73 74 29 2b 6e 4e  izeof(*pBest)+nN
255f5 61 6d 65 2b 31 29 29 21 3d 30 20 29 7b 0a 20 20  ame+1))!=0 ){.  
255f6 20 20 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 20 3d    pBest->zName =
255f7 20 28 63 68 61 72 20 2a 29 26 70 42 65 73 74 5b   (char *)&pBest[
255f8 31 5d 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 6e  1];.    pBest->n
255f9 41 72 67 20 3d 20 28 75 31 36 29 6e 41 72 67 3b  Arg = (u16)nArg;
255fa 0a 20 20 20 20 70 42 65 73 74 2d 3e 69 50 72 65  .    pBest->iPre
255fb 66 45 6e 63 20 3d 20 65 6e 63 3b 0a 20 20 20 20  fEnc = enc;.    
255fc 6d 65 6d 63 70 79 28 70 42 65 73 74 2d 3e 7a 4e  memcpy(pBest->zN
255fd 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  ame, zName, nNam
255fe 65 29 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 7a  e);.    pBest->z
255ff 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b  Name[nName] = 0;
25600 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63  .    sqlite3Func
25601 44 65 66 49 6e 73 65 72 74 28 26 64 62 2d 3e 61  DefInsert(&db->a
25602 46 75 6e 63 2c 20 70 42 65 73 74 29 3b 0a 20 20  Func, pBest);.  
25603 7d 0a 0a 20 20 69 66 28 20 70 42 65 73 74 20 26  }..  if( pBest &
25604 26 20 28 70 42 65 73 74 2d 3e 78 53 74 65 70 20  & (pBest->xStep 
25605 7c 7c 20 70 42 65 73 74 2d 3e 78 46 75 6e 63 20  || pBest->xFunc 
25606 7c 7c 20 63 72 65 61 74 65 46 6c 61 67 29 20 29  || createFlag) )
25607 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 42 65  {.    return pBe
25608 73 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  st;.  }.  return
25609 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65   0;.}../*.** Fre
2560a 65 20 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20  e all resources 
2560b 68 65 6c 64 20 62 79 20 74 68 65 20 73 63 68 65  held by the sche
2560c 6d 61 20 73 74 72 75 63 74 75 72 65 2e 20 54 68  ma structure. Th
2560d 65 20 76 6f 69 64 2a 20 61 72 67 75 6d 65 6e 74  e void* argument
2560e 20 70 6f 69 6e 74 73 0a 2a 2a 20 61 74 20 61 20   points.** at a 
2560f 53 63 68 65 6d 61 20 73 74 72 75 63 74 2e 20 54  Schema struct. T
25610 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65  his function doe
25611 73 20 6e 6f 74 20 63 61 6c 6c 20 73 71 6c 69 74  s not call sqlit
25612 65 33 44 62 46 72 65 65 28 64 62 2c 20 29 20 6f  e3DbFree(db, ) o
25613 6e 20 74 68 65 20 0a 2a 2a 20 70 6f 69 6e 74 65  n the .** pointe
25614 72 20 69 74 73 65 6c 66 2c 20 69 74 20 6a 75 73  r itself, it jus
25615 74 20 63 6c 65 61 6e 73 20 75 70 20 73 75 62 73  t cleans up subs
25616 69 64 75 61 72 79 20 72 65 73 6f 75 72 63 65 73  iduary resources
25617 20 28 69 2e 65 2e 20 74 68 65 20 63 6f 6e 74 65   (i.e. the conte
25618 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 73 63  nts.** of the sc
25619 68 65 6d 61 20 68 61 73 68 20 74 61 62 6c 65 73  hema hash tables
2561a 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 63 68  )..**.** The Sch
2561b 65 6d 61 2e 63 61 63 68 65 5f 73 69 7a 65 20 76  ema.cache_size v
2561c 61 72 69 61 62 6c 65 20 69 73 20 6e 6f 74 20 63  ariable is not c
2561d 6c 65 61 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  leared..*/.SQLIT
2561e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2561f 71 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65 65  qlite3SchemaFree
25620 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 48 61 73  (void *p){.  Has
25621 68 20 74 65 6d 70 31 3b 0a 20 20 48 61 73 68 20  h temp1;.  Hash 
25622 74 65 6d 70 32 3b 0a 20 20 48 61 73 68 45 6c 65  temp2;.  HashEle
25623 6d 20 2a 70 45 6c 65 6d 3b 0a 20 20 53 63 68 65  m *pElem;.  Sche
25624 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 28 53  ma *pSchema = (S
25625 63 68 65 6d 61 20 2a 29 70 3b 0a 0a 20 20 74 65  chema *)p;..  te
25626 6d 70 31 20 3d 20 70 53 63 68 65 6d 61 2d 3e 74  mp1 = pSchema->t
25627 62 6c 48 61 73 68 3b 0a 20 20 74 65 6d 70 32 20  blHash;.  temp2 
25628 3d 20 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48  = pSchema->trigH
25629 61 73 68 3b 0a 20 20 73 71 6c 69 74 65 33 48 61  ash;.  sqlite3Ha
2562a 73 68 49 6e 69 74 28 26 70 53 63 68 65 6d 61 2d  shInit(&pSchema-
2562b 3e 74 72 69 67 48 61 73 68 29 3b 0a 20 20 73 71  >trigHash);.  sq
2562c 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 26  lite3HashClear(&
2562d 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68  pSchema->idxHash
2562e 29 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73  );.  for(pElem=s
2562f 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26  qliteHashFirst(&
25630 74 65 6d 70 32 29 3b 20 70 45 6c 65 6d 3b 20 70  temp2); pElem; p
25631 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e  Elem=sqliteHashN
25632 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20  ext(pElem)){.   
25633 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72   sqlite3DeleteTr
25634 69 67 67 65 72 28 30 2c 20 28 54 72 69 67 67 65  igger(0, (Trigge
25635 72 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74  r*)sqliteHashDat
25636 61 28 70 45 6c 65 6d 29 29 3b 0a 20 20 7d 0a 20  a(pElem));.  }. 
25637 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61   sqlite3HashClea
25638 72 28 26 74 65 6d 70 32 29 3b 0a 20 20 73 71 6c  r(&temp2);.  sql
25639 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 53  ite3HashInit(&pS
2563a 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29 3b  chema->tblHash);
2563b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c  .  for(pElem=sql
2563c 69 74 65 48 61 73 68 46 69 72 73 74 28 26 74 65  iteHashFirst(&te
2563d 6d 70 31 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c  mp1); pElem; pEl
2563e 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78  em=sqliteHashNex
2563f 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 54  t(pElem)){.    T
25640 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c  able *pTab = sql
25641 69 74 65 48 61 73 68 44 61 74 61 28 70 45 6c 65  iteHashData(pEle
25642 6d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  m);.    assert( 
25643 70 54 61 62 2d 3e 64 62 4d 65 6d 3d 3d 30 20 29  pTab->dbMem==0 )
25644 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c  ;.    sqlite3Del
25645 65 74 65 54 61 62 6c 65 28 70 54 61 62 29 3b 0a  eteTable(pTab);.
25646 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 48 61 73    }.  sqlite3Has
25647 68 43 6c 65 61 72 28 26 74 65 6d 70 31 29 3b 0a  hClear(&temp1);.
25648 20 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65    sqlite3HashCle
25649 61 72 28 26 70 53 63 68 65 6d 61 2d 3e 66 6b 65  ar(&pSchema->fke
2564a 79 48 61 73 68 29 3b 0a 20 20 70 53 63 68 65 6d  yHash);.  pSchem
2564b 61 2d 3e 70 53 65 71 54 61 62 20 3d 20 30 3b 0a  a->pSeqTab = 0;.
2564c 20 20 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73    pSchema->flags
2564d 20 26 3d 20 7e 44 42 5f 53 63 68 65 6d 61 4c 6f   &= ~DB_SchemaLo
2564e 61 64 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  aded;.}../*.** F
2564f 69 6e 64 20 61 6e 64 20 72 65 74 75 72 6e 20 74  ind and return t
25650 68 65 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69  he schema associ
25651 61 74 65 64 20 77 69 74 68 20 61 20 42 54 72 65  ated with a BTre
25652 65 2e 20 20 43 72 65 61 74 65 0a 2a 2a 20 61 20  e.  Create.** a 
25653 6e 65 77 20 6f 6e 65 20 69 66 20 6e 65 63 65 73  new one if neces
25654 73 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  sary..*/.SQLITE_
25655 50 52 49 56 41 54 45 20 53 63 68 65 6d 61 20 2a  PRIVATE Schema *
25656 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74  sqlite3SchemaGet
25657 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 42 74  (sqlite3 *db, Bt
25658 72 65 65 20 2a 70 42 74 29 7b 0a 20 20 53 63 68  ree *pBt){.  Sch
25659 65 6d 61 20 2a 20 70 3b 0a 20 20 69 66 28 20 70  ema * p;.  if( p
2565a 42 74 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 53  Bt ){.    p = (S
2565b 63 68 65 6d 61 20 2a 29 73 71 6c 69 74 65 33 42  chema *)sqlite3B
2565c 74 72 65 65 53 63 68 65 6d 61 28 70 42 74 2c 20  treeSchema(pBt, 
2565d 73 69 7a 65 6f 66 28 53 63 68 65 6d 61 29 2c 20  sizeof(Schema), 
2565e 73 71 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65  sqlite3SchemaFre
2565f 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  e);.  }else{.   
25660 20 70 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 73   p = (Schema *)s
25661 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
25662 28 73 69 7a 65 6f 66 28 53 63 68 65 6d 61 29 29  (sizeof(Schema))
25663 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 20 29  ;.  }.  if( !p )
25664 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63  {.    db->malloc
25665 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65  Failed = 1;.  }e
25666 6c 73 65 20 69 66 20 28 20 30 3d 3d 70 2d 3e 66  lse if ( 0==p->f
25667 69 6c 65 5f 66 6f 72 6d 61 74 20 29 7b 0a 20 20  ile_format ){.  
25668 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69    sqlite3HashIni
25669 74 28 26 70 2d 3e 74 62 6c 48 61 73 68 29 3b 0a  t(&p->tblHash);.
2566a 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49      sqlite3HashI
2566b 6e 69 74 28 26 70 2d 3e 69 64 78 48 61 73 68 29  nit(&p->idxHash)
2566c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61 73  ;.    sqlite3Has
2566d 68 49 6e 69 74 28 26 70 2d 3e 74 72 69 67 48 61  hInit(&p->trigHa
2566e 73 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  sh);.    sqlite3
2566f 48 61 73 68 49 6e 69 74 28 26 70 2d 3e 66 6b 65  HashInit(&p->fke
25670 79 48 61 73 68 29 3b 0a 20 20 20 20 70 2d 3e 65  yHash);.    p->e
25671 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38  nc = SQLITE_UTF8
25672 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
25673 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
25674 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 63 61 6c 6c  **** End of call
25675 62 61 63 6b 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  back.c *********
25676 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25677 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25678 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
25679 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
2567a 64 65 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  delete.c *******
2567b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2567c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2567d 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
2567e 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
2567f 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
25680 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
25681 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
25682 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
25683 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
25684 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
25685 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
25686 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
25687 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
25688 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
25689 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
2568a 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
2568b 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
2568c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2568d 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
2568e 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
2568f 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
25690 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
25691 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25692 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25693 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25694 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
25695 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
25696 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65  s C code routine
25697 73 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65  s that are calle
25698 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 0a  d by the parser.
25699 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67  ** in order to g
2569a 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
2569b 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 73 74 61   DELETE FROM sta
2569c 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 24  tements..**.** $
2569d 49 64 3a 20 64 65 6c 65 74 65 2e 63 2c 76 20 31  Id: delete.c,v 1
2569e 2e 32 30 37 20 32 30 30 39 2f 30 38 2f 30 38 20  .207 2009/08/08 
2569f 31 38 3a 30 31 3a 30 38 20 64 72 68 20 45 78 70  18:01:08 drh Exp
256a0 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f   $.*/../*.** Loo
256a1 6b 20 75 70 20 65 76 65 72 79 20 74 61 62 6c 65  k up every table
256a2 20 74 68 61 74 20 69 73 20 6e 61 6d 65 64 20 69   that is named i
256a3 6e 20 70 53 72 63 2e 20 20 49 66 20 61 6e 79 20  n pSrc.  If any 
256a4 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 66 6f 75  table is not fou
256a5 6e 64 2c 0a 2a 2a 20 61 64 64 20 61 6e 20 65 72  nd,.** add an er
256a6 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70  ror message to p
256a7 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 61  Parse->zErrMsg a
256a8 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20  nd return NULL. 
256a9 20 49 66 20 61 6c 6c 20 74 61 62 6c 65 73 0a 2a   If all tables.*
256aa 2a 20 61 72 65 20 66 6f 75 6e 64 2c 20 72 65 74  * are found, ret
256ab 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
256ac 20 74 68 65 20 6c 61 73 74 20 74 61 62 6c 65 2e   the last table.
256ad 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
256ae 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65  TE Table *sqlite
256af 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 50  3SrcListLookup(P
256b0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72  arse *pParse, Sr
256b1 63 4c 69 73 74 20 2a 70 53 72 63 29 7b 0a 20 20  cList *pSrc){.  
256b2 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
256b3 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 53 72  tem *pItem = pSr
256b4 63 2d 3e 61 3b 0a 20 20 54 61 62 6c 65 20 2a 70  c->a;.  Table *p
256b5 54 61 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70  Tab;.  assert( p
256b6 49 74 65 6d 20 26 26 20 70 53 72 63 2d 3e 6e 53  Item && pSrc->nS
256b7 72 63 3d 3d 31 20 29 3b 0a 20 20 70 54 61 62 20  rc==1 );.  pTab 
256b8 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54  = sqlite3LocateT
256b9 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20  able(pParse, 0, 
256ba 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 70 49  pItem->zName, pI
256bb 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b  tem->zDatabase);
256bc 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  .  sqlite3Delete
256bd 54 61 62 6c 65 28 70 49 74 65 6d 2d 3e 70 54 61  Table(pItem->pTa
256be 62 29 3b 0a 20 20 70 49 74 65 6d 2d 3e 70 54 61  b);.  pItem->pTa
256bf 62 20 3d 20 70 54 61 62 3b 0a 20 20 69 66 28 20  b = pTab;.  if( 
256c0 70 54 61 62 20 29 7b 0a 20 20 20 20 70 54 61 62  pTab ){.    pTab
256c1 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 7d 0a 20 20  ->nRef++;.  }.  
256c2 69 66 28 20 73 71 6c 69 74 65 33 49 6e 64 65 78  if( sqlite3Index
256c3 65 64 42 79 4c 6f 6f 6b 75 70 28 70 50 61 72 73  edByLookup(pPars
256c4 65 2c 20 70 49 74 65 6d 29 20 29 7b 0a 20 20 20  e, pItem) ){.   
256c5 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 7d 0a 20   pTab = 0;.  }. 
256c6 20 72 65 74 75 72 6e 20 70 54 61 62 3b 0a 7d 0a   return pTab;.}.
256c7 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
256c8 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 67 69  make sure the gi
256c9 76 65 6e 20 74 61 62 6c 65 20 69 73 20 77 72 69  ven table is wri
256ca 74 61 62 6c 65 2e 20 20 49 66 20 69 74 20 69 73  table.  If it is
256cb 20 6e 6f 74 0a 2a 2a 20 77 72 69 74 61 62 6c 65   not.** writable
256cc 2c 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72  , generate an er
256cd 72 6f 72 20 6d 65 73 73 61 67 65 20 61 6e 64 20  ror message and 
256ce 72 65 74 75 72 6e 20 31 2e 20 20 49 66 20 69 74  return 1.  If it
256cf 20 69 73 0a 2a 2a 20 77 72 69 74 61 62 6c 65 20   is.** writable 
256d0 72 65 74 75 72 6e 20 30 3b 0a 2a 2f 0a 53 51 4c  return 0;.*/.SQL
256d1 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
256d2 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c  sqlite3IsReadOnl
256d3 79 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  y(Parse *pParse,
256d4 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 69 6e   Table *pTab, in
256d5 74 20 76 69 65 77 4f 6b 29 7b 0a 20 20 2f 2a 20  t viewOk){.  /* 
256d6 41 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 77  A table is not w
256d7 72 69 74 61 62 6c 65 20 75 6e 64 65 72 20 74 68  ritable under th
256d8 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 69 72 63  e following circ
256d9 75 6d 73 74 61 6e 63 65 73 3a 0a 20 20 2a 2a 0a  umstances:.  **.
256da 20 20 2a 2a 20 20 20 31 29 20 49 74 20 69 73 20    **   1) It is 
256db 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
256dc 61 6e 64 20 6e 6f 20 69 6d 70 6c 65 6d 65 6e 74  and no implement
256dd 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 78 55 70  ation of the xUp
256de 64 61 74 65 20 6d 65 74 68 6f 64 0a 20 20 2a 2a  date method.  **
256df 20 20 20 20 20 20 68 61 73 20 62 65 65 6e 20 70        has been p
256e0 72 6f 76 69 64 65 64 2c 20 6f 72 0a 20 20 2a 2a  rovided, or.  **
256e1 20 20 20 32 29 20 49 74 20 69 73 20 61 20 73 79     2) It is a sy
256e2 73 74 65 6d 20 74 61 62 6c 65 20 28 69 2e 65 2e  stem table (i.e.
256e3 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 29 2c   sqlite_master),
256e4 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 6e 6f   this call is no
256e5 74 0a 20 20 2a 2a 20 20 20 20 20 20 70 61 72 74  t.  **      part
256e6 20 6f 66 20 61 20 6e 65 73 74 65 64 20 70 61 72   of a nested par
256e7 73 65 20 61 6e 64 20 77 72 69 74 61 62 6c 65 5f  se and writable_
256e8 73 63 68 65 6d 61 20 70 72 61 67 6d 61 20 68 61  schema pragma ha
256e9 73 20 6e 6f 74 20 0a 20 20 2a 2a 20 20 20 20 20  s not .  **     
256ea 20 62 65 65 6e 20 73 70 65 63 69 66 69 65 64 2e   been specified.
256eb 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 65 69  .  **.  ** In ei
256ec 74 68 65 72 20 63 61 73 65 20 6c 65 61 76 65 20  ther case leave 
256ed 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
256ee 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72   in pParse and r
256ef 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a  eturn non-zero..
256f0 20 20 2a 2f 0a 20 20 69 66 28 20 28 20 49 73 56    */.  if( ( IsV
256f1 69 72 74 75 61 6c 28 70 54 61 62 29 20 0a 20 20  irtual(pTab) .  
256f2 20 20 20 26 26 20 73 71 6c 69 74 65 33 47 65 74     && sqlite3Get
256f3 56 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64  VTable(pParse->d
256f4 62 2c 20 70 54 61 62 29 2d 3e 70 4d 6f 64 2d 3e  b, pTab)->pMod->
256f5 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 61 74 65  pModule->xUpdate
256f6 3d 3d 30 20 29 0a 20 20 20 7c 7c 20 28 20 28 70  ==0 ).   || ( (p
256f7 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  Tab->tabFlags & 
256f8 54 46 5f 52 65 61 64 6f 6e 6c 79 29 21 3d 30 0a  TF_Readonly)!=0.
256f9 20 20 20 20 20 26 26 20 28 70 50 61 72 73 65 2d       && (pParse-
256fa 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  >db->flags & SQL
256fb 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 29  ITE_WriteSchema)
256fc 3d 3d 30 0a 20 20 20 20 20 26 26 20 70 50 61 72  ==0.     && pPar
256fd 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 0a  se->nested==0 ).
256fe 20 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33    ){.    sqlite3
256ff 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
25700 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20 6e   "table %s may n
25701 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64 22 2c  ot be modified",
25702 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pTab->zName);. 
25703 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
25704 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
25705 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69 66 28  _OMIT_VIEW.  if(
25706 20 21 76 69 65 77 4f 6b 20 26 26 20 70 54 61 62   !viewOk && pTab
25707 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ->pSelect ){.   
25708 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
25709 28 70 50 61 72 73 65 2c 22 63 61 6e 6e 6f 74 20  (pParse,"cannot 
2570a 6d 6f 64 69 66 79 20 25 73 20 62 65 63 61 75 73  modify %s becaus
2570b 65 20 69 74 20 69 73 20 61 20 76 69 65 77 22 2c  e it is a view",
2570c 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pTab->zName);.  
2570d 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
2570e 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
2570f 30 3b 0a 7d 0a 0a 0a 23 69 66 20 21 64 65 66 69  0;.}...#if !defi
25710 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
25711 56 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65  VIEW) && !define
25712 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
25713 49 47 47 45 52 29 0a 2f 2a 0a 2a 2a 20 45 76 61  IGGER)./*.** Eva
25714 6c 75 61 74 65 20 61 20 76 69 65 77 20 61 6e 64  luate a view and
25715 20 73 74 6f 72 65 20 69 74 73 20 72 65 73 75 6c   store its resul
25716 74 20 69 6e 20 61 6e 20 65 70 68 65 6d 65 72 61  t in an ephemera
25717 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 0a 2a 2a  l table.  The.**
25718 20 70 57 68 65 72 65 20 61 72 67 75 6d 65 6e 74   pWhere argument
25719 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20   is an optional 
2571a 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 68 61  WHERE clause tha
2571b 74 20 72 65 73 74 72 69 63 74 73 20 74 68 65 0a  t restricts the.
2571c 2a 2a 20 73 65 74 20 6f 66 20 72 6f 77 73 20 69  ** set of rows i
2571d 6e 20 74 68 65 20 76 69 65 77 20 74 68 61 74 20  n the view that 
2571e 61 72 65 20 74 6f 20 62 65 20 61 64 64 65 64 20  are to be added 
2571f 74 6f 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c  to the ephemeral
25720 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54   table..*/.SQLIT
25721 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
25722 71 6c 69 74 65 33 4d 61 74 65 72 69 61 6c 69 7a  qlite3Materializ
25723 65 56 69 65 77 28 0a 20 20 50 61 72 73 65 20 2a  eView(.  Parse *
25724 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a  pParse,       /*
25725 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
25726 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 56 69   */.  Table *pVi
25727 65 77 2c 20 20 20 20 20 20 20 20 2f 2a 20 56 69  ew,        /* Vi
25728 65 77 20 64 65 66 69 6e 69 74 69 6f 6e 20 2a 2f  ew definition */
25729 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c  .  Expr *pWhere,
2572a 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f          /* Optio
2572b 6e 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73 65  nal WHERE clause
2572c 20 74 6f 20 62 65 20 61 64 64 65 64 20 2a 2f 0a   to be added */.
2572d 20 20 69 6e 74 20 69 43 75 72 20 20 20 20 20 20    int iCur      
2572e 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
2572f 20 6e 75 6d 62 65 72 20 66 6f 72 20 65 70 68 65   number for ephe
25730 6d 65 72 69 61 6c 20 74 61 62 6c 65 20 2a 2f 0a  merial table */.
25731 29 7b 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20  ){.  SelectDest 
25732 64 65 73 74 3b 0a 20 20 53 65 6c 65 63 74 20 2a  dest;.  Select *
25733 70 44 75 70 3b 0a 20 20 73 71 6c 69 74 65 33 20  pDup;.  sqlite3 
25734 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
25735 3b 0a 0a 20 20 70 44 75 70 20 3d 20 73 71 6c 69  ;..  pDup = sqli
25736 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c  te3SelectDup(db,
25737 20 70 56 69 65 77 2d 3e 70 53 65 6c 65 63 74 2c   pView->pSelect,
25738 20 30 29 3b 0a 20 20 69 66 28 20 70 57 68 65 72   0);.  if( pWher
25739 65 20 29 7b 0a 20 20 20 20 53 72 63 4c 69 73 74  e ){.    SrcList
2573a 20 2a 70 46 72 6f 6d 3b 0a 20 20 20 20 0a 20 20   *pFrom;.    .  
2573b 20 20 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74    pWhere = sqlit
2573c 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 57  e3ExprDup(db, pW
2573d 68 65 72 65 2c 20 30 29 3b 0a 20 20 20 20 70 46  here, 0);.    pF
2573e 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33 53 72 63  rom = sqlite3Src
2573f 4c 69 73 74 41 70 70 65 6e 64 28 64 62 2c 20 30  ListAppend(db, 0
25740 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  , 0, 0);.    if(
25741 20 70 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20   pFrom ){.      
25742 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 6e  assert( pFrom->n
25743 53 72 63 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20  Src==1 );.      
25744 70 46 72 6f 6d 2d 3e 61 5b 30 5d 2e 7a 41 6c 69  pFrom->a[0].zAli
25745 61 73 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  as = sqlite3DbSt
25746 72 44 75 70 28 64 62 2c 20 70 56 69 65 77 2d 3e  rDup(db, pView->
25747 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 46  zName);.      pF
25748 72 6f 6d 2d 3e 61 5b 30 5d 2e 70 53 65 6c 65 63  rom->a[0].pSelec
25749 74 20 3d 20 70 44 75 70 3b 0a 20 20 20 20 20 20  t = pDup;.      
2574a 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 61  assert( pFrom->a
2574b 5b 30 5d 2e 70 4f 6e 3d 3d 30 20 29 3b 0a 20 20  [0].pOn==0 );.  
2574c 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72 6f      assert( pFro
2574d 6d 2d 3e 61 5b 30 5d 2e 70 55 73 69 6e 67 3d 3d  m->a[0].pUsing==
2574e 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  0 );.    }else{.
2574f 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
25750 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 44  ectDelete(db, pD
25751 75 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  up);.    }.    p
25752 44 75 70 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  Dup = sqlite3Sel
25753 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c 20 30  ectNew(pParse, 0
25754 2c 20 70 46 72 6f 6d 2c 20 70 57 68 65 72 65 2c  , pFrom, pWhere,
25755 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
25756 30 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  0);.  }.  sqlite
25757 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
25758 26 64 65 73 74 2c 20 53 52 54 5f 45 70 68 65 6d  &dest, SRT_Ephem
25759 54 61 62 2c 20 69 43 75 72 29 3b 0a 20 20 73 71  Tab, iCur);.  sq
2575a 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72  lite3Select(pPar
2575b 73 65 2c 20 70 44 75 70 2c 20 26 64 65 73 74 29  se, pDup, &dest)
2575c 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ;.  sqlite3Selec
2575d 74 44 65 6c 65 74 65 28 64 62 2c 20 70 44 75 70  tDelete(db, pDup
2575e 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21  );.}.#endif /* !
2575f 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
25760 4d 49 54 5f 56 49 45 57 29 20 26 26 20 21 64 65  MIT_VIEW) && !de
25761 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
25762 54 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a 23  T_TRIGGER) */..#
25763 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
25764 45 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f  E_ENABLE_UPDATE_
25765 44 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26  DELETE_LIMIT) &&
25766 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
25767 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a  _OMIT_SUBQUERY).
25768 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61  /*.** Generate a
25769 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  n expression tre
2576a 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  e to implement t
2576b 68 65 20 57 48 45 52 45 2c 20 4f 52 44 45 52 20  he WHERE, ORDER 
2576c 42 59 2c 0a 2a 2a 20 61 6e 64 20 4c 49 4d 49 54  BY,.** and LIMIT
2576d 2f 4f 46 46 53 45 54 20 70 6f 72 74 69 6f 6e 20  /OFFSET portion 
2576e 6f 66 20 44 45 4c 45 54 45 20 61 6e 64 20 55 50  of DELETE and UP
2576f 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 73 2e  DATE statements.
25770 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54  .**.**     DELET
25771 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f 77 78 79  E FROM table_wxy
25772 7a 20 57 48 45 52 45 20 61 3c 35 20 4f 52 44 45  z WHERE a<5 ORDE
25773 52 20 42 59 20 61 20 4c 49 4d 49 54 20 31 3b 0a  R BY a LIMIT 1;.
25774 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
25775 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 5f                \_
25776 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
25777 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20  _________/.**   
25778 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25779 20 20 20 20 20 20 20 20 20 20 20 20 70 4c 69 6d              pLim
2577a 69 74 57 68 65 72 65 20 28 70 49 6e 43 6c 61 75  itWhere (pInClau
2577b 73 65 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se).*/.SQLITE_PR
2577c 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
2577d 74 65 33 4c 69 6d 69 74 57 68 65 72 65 28 0a 20  te3LimitWhere(. 
2577e 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2577f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
25780 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74   The parser cont
25781 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ext */.  SrcList
25782 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20 20 20   *pSrc,         
25783 20 20 20 20 20 20 2f 2a 20 74 68 65 20 46 52 4f        /* the FRO
25784 4d 20 63 6c 61 75 73 65 20 2d 2d 20 77 68 69 63  M clause -- whic
25785 68 20 74 61 62 6c 65 73 20 74 6f 20 73 63 61 6e  h tables to scan
25786 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65   */.  Expr *pWhe
25787 72 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  re,             
25788 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
25789 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20  clause.  May be 
2578a 6e 75 6c 6c 20 2a 2f 0a 20 20 45 78 70 72 4c 69  null */.  ExprLi
2578b 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20  st *pOrderBy,   
2578c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52         /* The OR
2578d 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e 20 20  DER BY clause.  
2578e 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20  May be null */. 
2578f 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20 20   Expr *pLimit,  
25790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
25791 20 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73   The LIMIT claus
25792 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20  e.  May be null 
25793 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f 66 66 73  */.  Expr *pOffs
25794 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  et,             
25795 20 20 2f 2a 20 54 68 65 20 4f 46 46 53 45 54 20    /* The OFFSET 
25796 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20  clause.  May be 
25797 6e 75 6c 6c 20 2a 2f 0a 20 20 63 68 61 72 20 2a  null */.  char *
25798 7a 53 74 6d 74 54 79 70 65 20 20 20 20 20 20 20  zStmtType       
25799 20 20 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72         /* Either
2579a 20 44 45 4c 45 54 45 20 6f 72 20 55 50 44 41 54   DELETE or UPDAT
2579b 45 2e 20 20 46 6f 72 20 65 72 72 6f 72 20 6d 65  E.  For error me
2579c 73 73 61 67 65 73 2e 20 2a 2f 0a 29 7b 0a 20 20  ssages. */.){.  
2579d 45 78 70 72 20 2a 70 57 68 65 72 65 52 6f 77 69  Expr *pWhereRowi
2579e 64 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 2f 2a 20  d = NULL;    /* 
2579f 57 48 45 52 45 20 72 6f 77 69 64 20 2e 2e 20 2a  WHERE rowid .. *
257a0 2f 0a 20 20 45 78 70 72 20 2a 70 49 6e 43 6c 61  /.  Expr *pInCla
257a1 75 73 65 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20  use = NULL;     
257a2 20 2f 2a 20 57 48 45 52 45 20 72 6f 77 69 64 20   /* WHERE rowid 
257a3 49 4e 20 28 20 73 65 6c 65 63 74 20 29 20 2a 2f  IN ( select ) */
257a4 0a 20 20 45 78 70 72 20 2a 70 53 65 6c 65 63 74  .  Expr *pSelect
257a5 52 6f 77 69 64 20 3d 20 4e 55 4c 4c 3b 20 20 20  Rowid = NULL;   
257a6 2f 2a 20 53 45 4c 45 43 54 20 72 6f 77 69 64 20  /* SELECT rowid 
257a7 2e 2e 2e 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ... */.  ExprLis
257a8 74 20 2a 70 45 4c 69 73 74 20 3d 20 4e 55 4c 4c  t *pEList = NULL
257a9 3b 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73  ;     /* Express
257aa 69 6f 6e 20 6c 69 73 74 20 63 6f 6e 74 61 6e 69  ion list contani
257ab 6e 67 20 6f 6e 6c 79 20 70 53 65 6c 65 63 74 52  ng only pSelectR
257ac 6f 77 69 64 20 2a 2f 0a 20 20 53 72 63 4c 69 73  owid */.  SrcLis
257ad 74 20 2a 70 53 65 6c 65 63 74 53 72 63 20 3d 20  t *pSelectSrc = 
257ae 4e 55 4c 4c 3b 20 20 2f 2a 20 53 45 4c 45 43 54  NULL;  /* SELECT
257af 20 72 6f 77 69 64 20 46 52 4f 4d 20 78 20 2e 2e   rowid FROM x ..
257b0 2e 20 28 64 75 70 20 6f 66 20 70 53 72 63 29 20  . (dup of pSrc) 
257b1 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65  */.  Select *pSe
257b2 6c 65 63 74 20 3d 20 4e 55 4c 4c 3b 20 20 20 20  lect = NULL;    
257b3 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 53 45    /* Complete SE
257b4 4c 45 43 54 20 74 72 65 65 20 2a 2f 0a 0a 20 20  LECT tree */..  
257b5 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  /* Check that th
257b6 65 72 65 20 69 73 6e 27 74 20 61 6e 20 4f 52 44  ere isn't an ORD
257b7 45 52 20 42 59 20 77 69 74 68 6f 75 74 20 61 20  ER BY without a 
257b8 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 0a 20 20  LIMIT clause..  
257b9 2a 2f 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42  */.  if( pOrderB
257ba 79 20 26 26 20 28 70 4c 69 6d 69 74 20 3d 3d 20  y && (pLimit == 
257bb 30 29 20 29 20 7b 0a 20 20 20 20 73 71 6c 69 74  0) ) {.    sqlit
257bc 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
257bd 65 2c 20 22 4f 52 44 45 52 20 42 59 20 77 69 74  e, "ORDER BY wit
257be 68 6f 75 74 20 4c 49 4d 49 54 20 6f 6e 20 25 73  hout LIMIT on %s
257bf 22 2c 20 7a 53 74 6d 74 54 79 70 65 29 3b 0a 20  ", zStmtType);. 
257c0 20 20 20 70 50 61 72 73 65 2d 3e 70 61 72 73 65     pParse->parse
257c1 45 72 72 6f 72 20 3d 20 31 3b 0a 20 20 20 20 67  Error = 1;.    g
257c2 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f  oto limit_where_
257c3 63 6c 65 61 6e 75 70 5f 32 3b 0a 20 20 7d 0a 0a  cleanup_2;.  }..
257c4 20 20 2f 2a 20 57 65 20 6f 6e 6c 79 20 6e 65 65    /* We only nee
257c5 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20  d to generate a 
257c6 73 65 6c 65 63 74 20 65 78 70 72 65 73 73 69 6f  select expressio
257c7 6e 20 69 66 20 74 68 65 72 65 0a 20 20 2a 2a 20  n if there.  ** 
257c8 69 73 20 61 20 6c 69 6d 69 74 2f 6f 66 66 73 65  is a limit/offse
257c9 74 20 74 65 72 6d 20 74 6f 20 65 6e 66 6f 72 63  t term to enforc
257ca 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c  e..  */.  if( pL
257cb 69 6d 69 74 20 3d 3d 20 30 20 29 20 7b 0a 20 20  imit == 0 ) {.  
257cc 20 20 2f 2a 20 69 66 20 70 4c 69 6d 69 74 20 69    /* if pLimit i
257cd 73 20 6e 75 6c 6c 2c 20 70 4f 66 66 73 65 74 20  s null, pOffset 
257ce 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 6e  will always be n
257cf 75 6c 6c 20 61 73 20 77 65 6c 6c 2e 20 2a 2f 0a  ull as well. */.
257d0 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 66 66      assert( pOff
257d1 73 65 74 20 3d 3d 20 30 20 29 3b 0a 20 20 20 20  set == 0 );.    
257d2 72 65 74 75 72 6e 20 70 57 68 65 72 65 3b 0a 20  return pWhere;. 
257d3 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74   }..  /* Generat
257d4 65 20 61 20 73 65 6c 65 63 74 20 65 78 70 72 65  e a select expre
257d5 73 73 69 6f 6e 20 74 72 65 65 20 74 6f 20 65 6e  ssion tree to en
257d6 66 6f 72 63 65 20 74 68 65 20 6c 69 6d 69 74 2f  force the limit/
257d7 6f 66 66 73 65 74 20 0a 20 20 2a 2a 20 74 65 72  offset .  ** ter
257d8 6d 20 66 6f 72 20 74 68 65 20 44 45 4c 45 54 45  m for the DELETE
257d9 20 6f 72 20 55 50 44 41 54 45 20 73 74 61 74 65   or UPDATE state
257da 6d 65 6e 74 2e 20 20 46 6f 72 20 65 78 61 6d 70  ment.  For examp
257db 6c 65 3a 0a 20 20 2a 2a 20 20 20 44 45 4c 45 54  le:.  **   DELET
257dc 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f 61 20 57  E FROM table_a W
257dd 48 45 52 45 20 63 6f 6c 31 3d 31 20 4f 52 44 45  HERE col1=1 ORDE
257de 52 20 42 59 20 63 6f 6c 32 20 4c 49 4d 49 54 20  R BY col2 LIMIT 
257df 31 20 4f 46 46 53 45 54 20 31 0a 20 20 2a 2a 20  1 OFFSET 1.  ** 
257e0 62 65 63 6f 6d 65 73 3a 0a 20 20 2a 2a 20 20 20  becomes:.  **   
257e1 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c  DELETE FROM tabl
257e2 65 5f 61 20 57 48 45 52 45 20 72 6f 77 69 64 20  e_a WHERE rowid 
257e3 49 4e 20 28 20 0a 20 20 2a 2a 20 20 20 20 20 53  IN ( .  **     S
257e4 45 4c 45 43 54 20 72 6f 77 69 64 20 46 52 4f 4d  ELECT rowid FROM
257e5 20 74 61 62 6c 65 5f 61 20 57 48 45 52 45 20 63   table_a WHERE c
257e6 6f 6c 31 3d 31 20 4f 52 44 45 52 20 42 59 20 63  ol1=1 ORDER BY c
257e7 6f 6c 32 20 4c 49 4d 49 54 20 31 20 4f 46 46 53  ol2 LIMIT 1 OFFS
257e8 45 54 20 31 0a 20 20 2a 2a 20 20 20 29 3b 0a 20  ET 1.  **   );. 
257e9 20 2a 2f 0a 0a 20 20 70 53 65 6c 65 63 74 52 6f   */..  pSelectRo
257ea 77 69 64 20 3d 20 73 71 6c 69 74 65 33 50 45 78  wid = sqlite3PEx
257eb 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 52 4f  pr(pParse, TK_RO
257ec 57 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 69  W, 0, 0, 0);.  i
257ed 66 28 20 70 53 65 6c 65 63 74 52 6f 77 69 64 20  f( pSelectRowid 
257ee 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69  == 0 ) goto limi
257ef 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f  t_where_cleanup_
257f0 32 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 73 71  2;.  pEList = sq
257f1 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
257f2 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20 70  end(pParse, 0, p
257f3 53 65 6c 65 63 74 52 6f 77 69 64 29 3b 0a 20 20  SelectRowid);.  
257f4 69 66 28 20 70 45 4c 69 73 74 20 3d 3d 20 30 20  if( pEList == 0 
257f5 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65  ) goto limit_whe
257f6 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 0a 20  re_cleanup_2;.. 
257f7 20 2f 2a 20 64 75 70 6c 69 63 61 74 65 20 74 68   /* duplicate th
257f8 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 73  e FROM clause as
257f9 20 69 74 20 69 73 20 6e 65 65 64 65 64 20 62 79   it is needed by
257fa 20 62 6f 74 68 20 74 68 65 20 44 45 4c 45 54 45   both the DELETE
257fb 2f 55 50 44 41 54 45 20 74 72 65 65 0a 20 20 2a  /UPDATE tree.  *
257fc 2a 20 61 6e 64 20 74 68 65 20 53 45 4c 45 43 54  * and the SELECT
257fd 20 73 75 62 74 72 65 65 2e 20 2a 2f 0a 20 20 70   subtree. */.  p
257fe 53 65 6c 65 63 74 53 72 63 20 3d 20 73 71 6c 69  SelectSrc = sqli
257ff 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 70 50  te3SrcListDup(pP
25800 61 72 73 65 2d 3e 64 62 2c 20 70 53 72 63 2c 20  arse->db, pSrc, 
25801 30 29 3b 0a 20 20 69 66 28 20 70 53 65 6c 65 63  0);.  if( pSelec
25802 74 53 72 63 20 3d 3d 20 30 20 29 20 7b 0a 20 20  tSrc == 0 ) {.  
25803 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
25804 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
25805 64 62 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20  db, pEList);.   
25806 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65 72   goto limit_wher
25807 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 20 20 7d  e_cleanup_2;.  }
25808 0a 0a 20 20 2f 2a 20 67 65 6e 65 72 61 74 65 20  ..  /* generate 
25809 74 68 65 20 53 45 4c 45 43 54 20 65 78 70 72 65  the SELECT expre
2580a 73 73 69 6f 6e 20 74 72 65 65 2e 20 2a 2f 0a 20  ssion tree. */. 
2580b 20 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74   pSelect = sqlit
2580c 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72  e3SelectNew(pPar
2580d 73 65 2c 70 45 4c 69 73 74 2c 70 53 65 6c 65 63  se,pEList,pSelec
2580e 74 53 72 63 2c 70 57 68 65 72 65 2c 30 2c 30 2c  tSrc,pWhere,0,0,
2580f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
25810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4f                pO
25811 72 64 65 72 42 79 2c 30 2c 70 4c 69 6d 69 74 2c  rderBy,0,pLimit,
25812 70 4f 66 66 73 65 74 29 3b 0a 20 20 69 66 28 20  pOffset);.  if( 
25813 70 53 65 6c 65 63 74 20 3d 3d 20 30 20 29 20 72  pSelect == 0 ) r
25814 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 6e  eturn 0;..  /* n
25815 6f 77 20 67 65 6e 65 72 61 74 65 20 74 68 65 20  ow generate the 
25816 6e 65 77 20 57 48 45 52 45 20 72 6f 77 69 64 20  new WHERE rowid 
25817 49 4e 20 63 6c 61 75 73 65 20 66 6f 72 20 74 68  IN clause for th
25818 65 20 44 45 4c 45 54 45 2f 55 44 50 41 54 45 20  e DELETE/UDPATE 
25819 2a 2f 0a 20 20 70 57 68 65 72 65 52 6f 77 69 64  */.  pWhereRowid
2581a 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2581b 70 50 61 72 73 65 2c 20 54 4b 5f 52 4f 57 2c 20  pParse, TK_ROW, 
2581c 30 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20  0, 0, 0);.  if( 
2581d 70 57 68 65 72 65 52 6f 77 69 64 20 3d 3d 20 30  pWhereRowid == 0
2581e 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68   ) goto limit_wh
2581f 65 72 65 5f 63 6c 65 61 6e 75 70 5f 31 3b 0a 20  ere_cleanup_1;. 
25820 20 70 49 6e 43 6c 61 75 73 65 20 3d 20 73 71 6c   pInClause = sql
25821 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
25822 2c 20 54 4b 5f 49 4e 2c 20 70 57 68 65 72 65 52  , TK_IN, pWhereR
25823 6f 77 69 64 2c 20 30 2c 20 30 29 3b 0a 20 20 69  owid, 0, 0);.  i
25824 66 28 20 70 49 6e 43 6c 61 75 73 65 20 3d 3d 20  f( pInClause == 
25825 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77  0 ) goto limit_w
25826 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 31 3b 0a  here_cleanup_1;.
25827 0a 20 20 70 49 6e 43 6c 61 75 73 65 2d 3e 78 2e  .  pInClause->x.
25828 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63  pSelect = pSelec
25829 74 3b 0a 20 20 70 49 6e 43 6c 61 75 73 65 2d 3e  t;.  pInClause->
2582a 66 6c 61 67 73 20 7c 3d 20 45 50 5f 78 49 73 53  flags |= EP_xIsS
2582b 65 6c 65 63 74 3b 0a 20 20 73 71 6c 69 74 65 33  elect;.  sqlite3
2582c 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50  ExprSetHeight(pP
2582d 61 72 73 65 2c 20 70 49 6e 43 6c 61 75 73 65 29  arse, pInClause)
2582e 3b 0a 20 20 72 65 74 75 72 6e 20 70 49 6e 43 6c  ;.  return pInCl
2582f 61 75 73 65 3b 0a 0a 20 20 2f 2a 20 73 6f 6d 65  ause;..  /* some
25830 74 68 69 6e 67 20 77 65 6e 74 20 77 72 6f 6e 67  thing went wrong
25831 2e 20 63 6c 65 61 6e 20 75 70 20 61 6e 79 74 68  . clean up anyth
25832 69 6e 67 20 61 6c 6c 6f 63 61 74 65 64 2e 20 2a  ing allocated. *
25833 2f 0a 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c  /.limit_where_cl
25834 65 61 6e 75 70 5f 31 3a 0a 20 20 73 71 6c 69 74  eanup_1:.  sqlit
25835 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70  e3SelectDelete(p
25836 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 65 6c 65  Parse->db, pSele
25837 63 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b  ct);.  return 0;
25838 0a 0a 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c  ..limit_where_cl
25839 65 61 6e 75 70 5f 32 3a 0a 20 20 73 71 6c 69 74  eanup_2:.  sqlit
2583a 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 50 61  e3ExprDelete(pPa
2583b 72 73 65 2d 3e 64 62 2c 20 70 57 68 65 72 65 29  rse->db, pWhere)
2583c 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  ;.  sqlite3ExprL
2583d 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  istDelete(pParse
2583e 2d 3e 64 62 2c 20 70 4f 72 64 65 72 42 79 29 3b  ->db, pOrderBy);
2583f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
25840 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
25841 20 70 4c 69 6d 69 74 29 3b 0a 20 20 73 71 6c 69   pLimit);.  sqli
25842 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 50  te3ExprDelete(pP
25843 61 72 73 65 2d 3e 64 62 2c 20 70 4f 66 66 73 65  arse->db, pOffse
25844 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  t);.  return 0;.
25845 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69  }.#endif /* defi
25846 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
25847 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f  E_UPDATE_DELETE_
25848 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e  LIMIT) && !defin
25849 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
2584a 55 42 51 55 45 52 59 29 20 2a 2f 0a 0a 2f 2a 0a  UBQUERY) */../*.
2584b 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
2584c 20 66 6f 72 20 61 20 44 45 4c 45 54 45 20 46 52   for a DELETE FR
2584d 4f 4d 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  OM statement..**
2584e 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20 46  .**     DELETE F
2584f 52 4f 4d 20 74 61 62 6c 65 5f 77 78 79 7a 20 57  ROM table_wxyz W
25850 48 45 52 45 20 61 3c 35 20 41 4e 44 20 62 20 4e  HERE a<5 AND b N
25851 4f 54 20 4e 55 4c 4c 3b 0a 2a 2a 20 20 20 20 20  OT NULL;.**     
25852 20 20 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f              \___
25853 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 5c 5f 5f  _____/       \__
25854 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a  ______________/.
25855 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
25856 20 20 20 20 70 54 61 62 4c 69 73 74 20 20 20 20      pTabList    
25857 20 20 20 20 20 20 20 20 20 20 70 57 68 65 72 65            pWhere
25858 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
25859 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
2585a 65 6c 65 74 65 46 72 6f 6d 28 0a 20 20 50 61 72  eleteFrom(.  Par
2585b 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2585c 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
2585d 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  r context */.  S
2585e 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74  rcList *pTabList
2585f 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62  ,     /* The tab
25860 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 77 65  le from which we
25861 20 73 68 6f 75 6c 64 20 64 65 6c 65 74 65 20 74   should delete t
25862 68 69 6e 67 73 20 2a 2f 0a 20 20 45 78 70 72 20  hings */.  Expr 
25863 2a 70 57 68 65 72 65 20 20 20 20 20 20 20 20 20  *pWhere         
25864 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
25865 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e  lause.  May be n
25866 75 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65  ull */.){.  Vdbe
25867 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20   *v;            
25868 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
25869 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e  l database engin
2586a 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  e */.  Table *pT
2586b 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ab;           /*
2586c 20 54 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20   The table from 
2586d 77 68 69 63 68 20 72 65 63 6f 72 64 73 20 77 69  which records wi
2586e 6c 6c 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f  ll be deleted */
2586f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
25870 44 62 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  Db;       /* Nam
25871 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 68 6f  e of database ho
25872 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20  lding pTab */.  
25873 69 6e 74 20 65 6e 64 2c 20 61 64 64 72 20 3d 20  int end, addr = 
25874 30 3b 20 20 20 20 20 2f 2a 20 41 20 63 6f 75 70  0;     /* A coup
25875 6c 65 20 61 64 64 72 65 73 73 65 73 20 6f 66 20  le addresses of 
25876 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 2a  generated code *
25877 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
25878 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
25879 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
2587a 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66  WhereInfo *pWInf
2587b 6f 3b 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d  o;     /* Inform
2587c 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20  ation about the 
2587d 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
2587e 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20    Index *pIdx;  
2587f 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
25880 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e 64  looping over ind
25881 69 63 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c  ices of the tabl
25882 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b  e */.  int iCur;
25883 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
25884 20 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75 6d   VDBE Cursor num
25885 62 65 72 20 66 6f 72 20 70 54 61 62 20 2a 2f 0a  ber for pTab */.
25886 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
25887 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 69 6e           /* Main
25888 20 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74   database struct
25889 75 72 65 20 2a 2f 0a 20 20 41 75 74 68 43 6f 6e  ure */.  AuthCon
2588a 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 20 20  text sContext;  
2588b 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e  /* Authorization
2588c 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 4e 61   context */.  Na
2588d 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20  meContext sNC;  
2588e 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e       /* Name con
2588f 74 65 78 74 20 74 6f 20 72 65 73 6f 6c 76 65 20  text to resolve 
25890 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 2a  expressions in *
25891 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
25892 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
25893 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f  tabase number */
25894 0a 20 20 69 6e 74 20 6d 65 6d 43 6e 74 20 3d 20  .  int memCnt = 
25895 2d 31 3b 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d  -1;       /* Mem
25896 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 66 6f  ory cell used fo
25897 72 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 69 6e  r change countin
25898 67 20 2a 2f 0a 20 20 69 6e 74 20 72 63 61 75 74  g */.  int rcaut
25899 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  h;            /*
2589a 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   Value returned 
2589b 62 79 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  by authorization
2589c 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 23 69   callback */..#i
2589d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2589e 54 5f 54 52 49 47 47 45 52 0a 20 20 69 6e 74 20  T_TRIGGER.  int 
2589f 69 73 56 69 65 77 3b 20 20 20 20 20 20 20 20 20  isView;         
258a0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
258a1 20 69 66 20 61 74 74 65 6d 70 74 69 6e 67 20 74   if attempting t
258a2 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 61 20  o delete from a 
258a3 76 69 65 77 20 2a 2f 0a 20 20 54 72 69 67 67 65  view */.  Trigge
258a4 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 20  r *pTrigger;    
258a5 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f         /* List o
258a6 66 20 74 61 62 6c 65 20 74 72 69 67 67 65 72 73  f table triggers
258a7 2c 20 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f  , if required */
258a8 0a 23 65 6e 64 69 66 0a 0a 20 20 6d 65 6d 73 65  .#endif..  memse
258a9 74 28 26 73 43 6f 6e 74 65 78 74 2c 20 30 2c 20  t(&sContext, 0, 
258aa 73 69 7a 65 6f 66 28 73 43 6f 6e 74 65 78 74 29  sizeof(sContext)
258ab 29 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  );.  db = pParse
258ac 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 50 61 72  ->db;.  if( pPar
258ad 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
258ae 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
258af 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f      goto delete_
258b0 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  from_cleanup;.  
258b1 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  }.  assert( pTab
258b2 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b  List->nSrc==1 );
258b3 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68  ..  /* Locate th
258b4 65 20 74 61 62 6c 65 20 77 68 69 63 68 20 77 65  e table which we
258b5 20 77 61 6e 74 20 74 6f 20 64 65 6c 65 74 65 2e   want to delete.
258b6 20 20 54 68 69 73 20 74 61 62 6c 65 20 68 61 73    This table has
258b7 20 74 6f 20 62 65 0a 20 20 2a 2a 20 70 75 74 20   to be.  ** put 
258b8 69 6e 20 61 6e 20 53 72 63 4c 69 73 74 20 73 74  in an SrcList st
258b9 72 75 63 74 75 72 65 20 62 65 63 61 75 73 65 20  ructure because 
258ba 73 6f 6d 65 20 6f 66 20 74 68 65 20 73 75 62 72  some of the subr
258bb 6f 75 74 69 6e 65 73 20 77 65 0a 20 20 2a 2a 20  outines we.  ** 
258bc 77 69 6c 6c 20 62 65 20 63 61 6c 6c 69 6e 67 20  will be calling 
258bd 61 72 65 20 64 65 73 69 67 6e 65 64 20 74 6f 20  are designed to 
258be 77 6f 72 6b 20 77 69 74 68 20 6d 75 6c 74 69 70  work with multip
258bf 6c 65 20 74 61 62 6c 65 73 20 61 6e 64 20 65 78  le tables and ex
258c0 70 65 63 74 0a 20 20 2a 2a 20 61 6e 20 53 72 63  pect.  ** an Src
258c1 4c 69 73 74 2a 20 70 61 72 61 6d 65 74 65 72 20  List* parameter 
258c2 69 6e 73 74 65 61 64 20 6f 66 20 6a 75 73 74 20  instead of just 
258c3 61 20 54 61 62 6c 65 2a 20 70 61 72 61 6d 65 74  a Table* paramet
258c4 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 54 61 62 20  er..  */.  pTab 
258c5 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  = sqlite3SrcList
258c6 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70  Lookup(pParse, p
258c7 54 61 62 4c 69 73 74 29 3b 0a 20 20 69 66 28 20  TabList);.  if( 
258c8 70 54 61 62 3d 3d 30 20 29 20 20 67 6f 74 6f 20  pTab==0 )  goto 
258c9 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61  delete_from_clea
258ca 6e 75 70 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72  nup;..  /* Figur
258cb 65 20 6f 75 74 20 69 66 20 77 65 20 68 61 76 65  e out if we have
258cc 20 61 6e 79 20 74 72 69 67 67 65 72 73 20 61 6e   any triggers an
258cd 64 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62  d if the table b
258ce 65 69 6e 67 0a 20 20 2a 2a 20 64 65 6c 65 74 65  eing.  ** delete
258cf 64 20 66 72 6f 6d 20 69 73 20 61 20 76 69 65 77  d from is a view
258d0 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
258d1 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
258d2 52 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20 73  R.  pTrigger = s
258d3 71 6c 69 74 65 33 54 72 69 67 67 65 72 73 45 78  qlite3TriggersEx
258d4 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61 62  ist(pParse, pTab
258d5 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c 20  , TK_DELETE, 0, 
258d6 30 29 3b 0a 20 20 69 73 56 69 65 77 20 3d 20 70  0);.  isView = p
258d7 54 61 62 2d 3e 70 53 65 6c 65 63 74 21 3d 30 3b  Tab->pSelect!=0;
258d8 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
258d9 70 54 72 69 67 67 65 72 20 30 0a 23 20 64 65 66  pTrigger 0.# def
258da 69 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e  ine isView 0.#en
258db 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
258dc 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e  E_OMIT_VIEW.# un
258dd 64 65 66 20 69 73 56 69 65 77 0a 23 20 64 65 66  def isView.# def
258de 69 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e  ine isView 0.#en
258df 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 70 54 61  dif..  /* If pTa
258e0 62 20 69 73 20 72 65 61 6c 6c 79 20 61 20 76 69  b is really a vi
258e1 65 77 2c 20 6d 61 6b 65 20 73 75 72 65 20 69 74  ew, make sure it
258e2 20 68 61 73 20 62 65 65 6e 20 69 6e 69 74 69 61   has been initia
258e3 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  lized..  */.  if
258e4 28 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74  ( sqlite3ViewGet
258e5 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72  ColumnNames(pPar
258e6 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20  se, pTab) ){.   
258e7 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f   goto delete_fro
258e8 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  m_cleanup;.  }..
258e9 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 52    if( sqlite3IsR
258ea 65 61 64 4f 6e 6c 79 28 70 50 61 72 73 65 2c 20  eadOnly(pParse, 
258eb 70 54 61 62 2c 20 28 70 54 72 69 67 67 65 72 3f  pTab, (pTrigger?
258ec 31 3a 30 29 29 20 29 7b 0a 20 20 20 20 67 6f 74  1:0)) ){.    got
258ed 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c  o delete_from_cl
258ee 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 44 62  eanup;.  }.  iDb
258ef 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
258f0 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62  ToIndex(db, pTab
258f1 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73  ->pSchema);.  as
258f2 73 65 72 74 28 20 69 44 62 3c 64 62 2d 3e 6e 44  sert( iDb<db->nD
258f3 62 20 29 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d  b );.  zDb = db-
258f4 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b  >aDb[iDb].zName;
258f5 0a 20 20 72 63 61 75 74 68 20 3d 20 73 71 6c 69  .  rcauth = sqli
258f6 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
258f7 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45  rse, SQLITE_DELE
258f8 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  TE, pTab->zName,
258f9 20 30 2c 20 7a 44 62 29 3b 0a 20 20 61 73 73 65   0, zDb);.  asse
258fa 72 74 28 20 72 63 61 75 74 68 3d 3d 53 51 4c 49  rt( rcauth==SQLI
258fb 54 45 5f 4f 4b 20 7c 7c 20 72 63 61 75 74 68 3d  TE_OK || rcauth=
258fc 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 7c 7c 20  =SQLITE_DENY || 
258fd 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 49  rcauth==SQLITE_I
258fe 47 4e 4f 52 45 20 29 3b 0a 20 20 69 66 28 20 72  GNORE );.  if( r
258ff 63 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 44 45  cauth==SQLITE_DE
25900 4e 59 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64  NY ){.    goto d
25901 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e  elete_from_clean
25902 75 70 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  up;.  }.  assert
25903 28 21 69 73 56 69 65 77 20 7c 7c 20 70 54 72 69  (!isView || pTri
25904 67 67 65 72 29 3b 0a 0a 20 20 2f 2a 20 41 73 73  gger);..  /* Ass
25905 69 67 6e 20 20 63 75 72 73 6f 72 20 6e 75 6d 62  ign  cursor numb
25906 65 72 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20  er to the table 
25907 61 6e 64 20 61 6c 6c 20 69 74 73 20 69 6e 64 69  and all its indi
25908 63 65 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ces..  */.  asse
25909 72 74 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53  rt( pTabList->nS
2590a 72 63 3d 3d 31 20 29 3b 0a 20 20 69 43 75 72 20  rc==1 );.  iCur 
2590b 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d  = pTabList->a[0]
2590c 2e 69 43 75 72 73 6f 72 20 3d 20 70 50 61 72 73  .iCursor = pPars
2590d 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f 72  e->nTab++;.  for
2590e 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64  (pIdx=pTab->pInd
2590f 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
25910 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
25911 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
25912 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 61 72 74  .  }..  /* Start
25913 20 74 68 65 20 76 69 65 77 20 63 6f 6e 74 65 78   the view contex
25914 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 56  t.  */.  if( isV
25915 69 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  iew ){.    sqlit
25916 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73  e3AuthContextPus
25917 68 28 70 50 61 72 73 65 2c 20 26 73 43 6f 6e 74  h(pParse, &sCont
25918 65 78 74 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ext, pTab->zName
25919 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67  );.  }..  /* Beg
2591a 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  in generating co
2591b 64 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73  de..  */.  v = s
2591c 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
2591d 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d  arse);.  if( v==
2591e 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65  0 ){.    goto de
2591f 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75  lete_from_cleanu
25920 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  p;.  }.  if( pPa
25921 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29  rse->nested==0 )
25922 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e   sqlite3VdbeCoun
25923 74 43 68 61 6e 67 65 73 28 76 29 3b 0a 20 20 73  tChanges(v);.  s
25924 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
25925 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
25926 2c 20 31 2c 20 69 44 62 29 3b 0a 0a 20 20 2f 2a  , 1, iDb);..  /*
25927 20 49 66 20 77 65 20 61 72 65 20 74 72 79 69 6e   If we are tryin
25928 67 20 74 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d  g to delete from
25929 20 61 20 76 69 65 77 2c 20 72 65 61 6c 69 7a 65   a view, realize
2592a 20 74 68 61 74 20 76 69 65 77 20 69 6e 74 6f 0a   that view into.
2592b 20 20 2a 2a 20 61 20 65 70 68 65 6d 65 72 61 6c    ** a ephemeral
2592c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23 69 66   table..  */.#if
2592d 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2592e 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 20 21  _OMIT_VIEW) && !
2592f 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
25930 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20 69  MIT_TRIGGER).  i
25931 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20  f( isView ){.   
25932 20 73 71 6c 69 74 65 33 4d 61 74 65 72 69 61 6c   sqlite3Material
25933 69 7a 65 56 69 65 77 28 70 50 61 72 73 65 2c 20  izeView(pParse, 
25934 70 54 61 62 2c 20 70 57 68 65 72 65 2c 20 69 43  pTab, pWhere, iC
25935 75 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ur);.  }.#endif.
25936 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68  .  /* Resolve th
25937 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69  e column names i
25938 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
25939 73 65 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65  se..  */.  memse
2593a 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f  t(&sNC, 0, sizeo
2593b 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43 2e 70  f(sNC));.  sNC.p
2593c 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
2593d 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d    sNC.pSrcList =
2593e 20 70 54 61 62 4c 69 73 74 3b 0a 20 20 69 66 28   pTabList;.  if(
2593f 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
25940 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70  xprNames(&sNC, p
25941 57 68 65 72 65 29 20 29 7b 0a 20 20 20 20 67 6f  Where) ){.    go
25942 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63  to delete_from_c
25943 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f  leanup;.  }..  /
25944 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
25945 20 63 6f 75 6e 74 65 72 20 6f 66 20 74 68 65 20   counter of the 
25946 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 64  number of rows d
25947 65 6c 65 74 65 64 2c 20 69 66 0a 20 20 2a 2a 20  eleted, if.  ** 
25948 77 65 20 61 72 65 20 63 6f 75 6e 74 69 6e 67 20  we are counting 
25949 72 6f 77 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  rows..  */.  if(
2594a 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c   db->flags & SQL
2594b 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 7b  ITE_CountRows ){
2594c 0a 20 20 20 20 6d 65 6d 43 6e 74 20 3d 20 2b 2b  .    memCnt = ++
2594d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2594e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2594f 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
25950 72 2c 20 30 2c 20 6d 65 6d 43 6e 74 29 3b 0a 20  r, 0, memCnt);. 
25951 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
25952 54 45 5f 4f 4d 49 54 5f 54 52 55 4e 43 41 54 45  TE_OMIT_TRUNCATE
25953 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20  _OPTIMIZATION.  
25954 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a  /* Special case:
25955 20 41 20 44 45 4c 45 54 45 20 77 69 74 68 6f 75   A DELETE withou
25956 74 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65  t a WHERE clause
25957 20 64 65 6c 65 74 65 73 20 65 76 65 72 79 74 68   deletes everyth
25958 69 6e 67 2e 0a 20 20 2a 2a 20 49 74 20 69 73 20  ing..  ** It is 
25959 65 61 73 69 65 72 20 6a 75 73 74 20 74 6f 20 65  easier just to e
2595a 72 61 73 65 20 74 68 65 20 77 68 6f 6c 65 20 74  rase the whole t
2595b 61 62 6c 65 2e 20 50 72 69 6f 72 20 74 6f 20 76  able. Prior to v
2595c 65 72 73 69 6f 6e 20 33 2e 36 2e 35 2c 0a 20 20  ersion 3.6.5,.  
2595d 2a 2a 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61  ** this optimiza
2595e 74 69 6f 6e 20 63 61 75 73 65 64 20 74 68 65 20  tion caused the 
2595f 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e 74  row change count
25960 20 28 74 68 65 20 76 61 6c 75 65 20 72 65 74 75   (the value retu
25961 72 6e 65 64 20 62 79 20 0a 20 20 2a 2a 20 41 50  rned by .  ** AP
25962 49 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74  I function sqlit
25963 65 33 5f 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73  e3_count_changes
25964 29 20 74 6f 20 62 65 20 73 65 74 20 69 6e 63 6f  ) to be set inco
25965 72 72 65 63 74 6c 79 2e 20 20 2a 2f 0a 20 20 69  rrectly.  */.  i
25966 66 28 20 72 63 61 75 74 68 3d 3d 53 51 4c 49 54  f( rcauth==SQLIT
25967 45 5f 4f 4b 20 26 26 20 70 57 68 65 72 65 3d 3d  E_OK && pWhere==
25968 30 20 26 26 20 21 70 54 72 69 67 67 65 72 20 26  0 && !pTrigger &
25969 26 20 21 49 73 56 69 72 74 75 61 6c 28 70 54 61  & !IsVirtual(pTa
2596a 62 29 20 0a 20 20 20 26 26 20 30 3d 3d 73 71 6c  b) .   && 0==sql
2596b 69 74 65 33 46 6b 52 65 71 75 69 72 65 64 28 70  ite3FkRequired(p
2596c 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30 2c 20  Parse, pTab, 0, 
2596d 30 29 0a 20 20 29 7b 0a 20 20 20 20 61 73 73 65  0).  ){.    asse
2596e 72 74 28 20 21 69 73 56 69 65 77 20 29 3b 0a 20  rt( !isView );. 
2596f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25970 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6c 65 61 72  dOp4(v, OP_Clear
25971 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 44  , pTab->tnum, iD
25972 62 2c 20 6d 65 6d 43 6e 74 2c 0a 20 20 20 20 20  b, memCnt,.     
25973 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25974 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34   pTab->zName, P4
25975 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 66 6f  _STATIC);.    fo
25976 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
25977 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
25978 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
25979 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78      assert( pIdx
2597a 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d  ->pSchema==pTab-
2597b 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20  >pSchema );.    
2597c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2597d 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c  Op2(v, OP_Clear,
2597e 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62   pIdx->tnum, iDb
2597f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
25980 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
25981 45 5f 4f 4d 49 54 5f 54 52 55 4e 43 41 54 45 5f  E_OMIT_TRUNCATE_
25982 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a  OPTIMIZATION */.
25983 20 20 2f 2a 20 54 68 65 20 75 73 75 61 6c 20 63    /* The usual c
25984 61 73 65 3a 20 54 68 65 72 65 20 69 73 20 61 20  ase: There is a 
25985 57 48 45 52 45 20 63 6c 61 75 73 65 20 73 6f 20  WHERE clause so 
25986 77 65 20 68 61 76 65 20 74 6f 20 73 63 61 6e 20  we have to scan 
25987 74 68 72 6f 75 67 68 0a 20 20 2a 2a 20 74 68 65  through.  ** the
25988 20 74 61 62 6c 65 20 61 6e 64 20 70 69 63 6b 20   table and pick 
25989 77 68 69 63 68 20 72 65 63 6f 72 64 73 20 74 6f  which records to
2598a 20 64 65 6c 65 74 65 2e 0a 20 20 2a 2f 0a 20 20   delete..  */.  
2598b 7b 0a 20 20 20 20 69 6e 74 20 69 52 6f 77 53 65  {.    int iRowSe
2598c 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  t = ++pParse->nM
2598d 65 6d 3b 20 20 20 2f 2a 20 52 65 67 69 73 74 65  em;   /* Registe
2598e 72 20 66 6f 72 20 72 6f 77 73 65 74 20 6f 66 20  r for rowset of 
2598f 72 6f 77 73 20 74 6f 20 64 65 6c 65 74 65 20 2a  rows to delete *
25990 2f 0a 20 20 20 20 69 6e 74 20 69 52 6f 77 69 64  /.    int iRowid
25991 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
25992 6d 3b 20 20 20 20 2f 2a 20 55 73 65 64 20 66 6f  m;    /* Used fo
25993 72 20 73 74 6f 72 69 6e 67 20 72 6f 77 69 64 20  r storing rowid 
25994 76 61 6c 75 65 73 2e 20 2a 2f 0a 20 20 20 20 69  values. */.    i
25995 6e 74 20 72 65 67 52 6f 77 69 64 3b 20 20 20 20  nt regRowid;    
25996 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
25997 2a 20 41 63 74 75 61 6c 20 72 65 67 69 73 74 65  * Actual registe
25998 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72 6f 77  r containing row
25999 69 64 73 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 43  ids */..    /* C
2599a 6f 6c 6c 65 63 74 20 72 6f 77 69 64 73 20 6f 66  ollect rowids of
2599b 20 65 76 65 72 79 20 72 6f 77 20 74 6f 20 62 65   every row to be
2599c 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f   deleted..    */
2599d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2599e 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
2599f 6c 2c 20 30 2c 20 69 52 6f 77 53 65 74 29 3b 0a  l, 0, iRowSet);.
259a0 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c      pWInfo = sql
259a1 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70  ite3WhereBegin(p
259a2 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c  Parse, pTabList,
259a3 20 70 57 68 65 72 65 2c 30 2c 57 48 45 52 45 5f   pWhere,0,WHERE_
259a4 44 55 50 4c 49 43 41 54 45 53 5f 4f 4b 29 3b 0a  DUPLICATES_OK);.
259a5 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d      if( pWInfo==
259a6 30 20 29 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f  0 ) goto delete_
259a7 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  from_cleanup;.  
259a8 20 20 72 65 67 52 6f 77 69 64 20 3d 20 73 71 6c    regRowid = sql
259a9 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43  ite3ExprCodeGetC
259aa 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70 54  olumn(pParse, pT
259ab 61 62 2c 20 2d 31 2c 20 69 43 75 72 2c 20 69 52  ab, -1, iCur, iR
259ac 6f 77 69 64 2c 20 30 29 3b 0a 20 20 20 20 73 71  owid, 0);.    sq
259ad 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
259ae 76 2c 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 2c  v, OP_RowSetAdd,
259af 20 69 52 6f 77 53 65 74 2c 20 72 65 67 52 6f 77   iRowSet, regRow
259b0 69 64 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d  id);.    if( db-
259b1 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
259b2 43 6f 75 6e 74 52 6f 77 73 20 29 7b 0a 20 20 20  CountRows ){.   
259b3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
259b4 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d  dOp2(v, OP_AddIm
259b5 6d 2c 20 6d 65 6d 43 6e 74 2c 20 31 29 3b 0a 20  m, memCnt, 1);. 
259b6 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
259b7 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29  WhereEnd(pWInfo)
259b8 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 65  ;..    /* Delete
259b9 20 65 76 65 72 79 20 69 74 65 6d 20 77 68 6f 73   every item whos
259ba 65 20 6b 65 79 20 77 61 73 20 77 72 69 74 74 65  e key was writte
259bb 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20 64 75  n to the list du
259bc 72 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20  ring the.    ** 
259bd 64 61 74 61 62 61 73 65 20 73 63 61 6e 2e 20 20  database scan.  
259be 57 65 20 68 61 76 65 20 74 6f 20 64 65 6c 65 74  We have to delet
259bf 65 20 69 74 65 6d 73 20 61 66 74 65 72 20 74 68  e items after th
259c0 65 20 73 63 61 6e 20 69 73 20 63 6f 6d 70 6c 65  e scan is comple
259c1 74 65 0a 20 20 20 20 2a 2a 20 62 65 63 61 75 73  te.    ** becaus
259c2 65 20 64 65 6c 65 74 69 6e 67 20 61 6e 20 69 74  e deleting an it
259c3 65 6d 20 63 61 6e 20 63 68 61 6e 67 65 20 74 68  em can change th
259c4 65 20 73 63 61 6e 20 6f 72 64 65 72 2e 20 20 2a  e scan order.  *
259c5 2f 0a 20 20 20 20 65 6e 64 20 3d 20 73 71 6c 69  /.    end = sqli
259c6 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
259c7 28 76 29 3b 0a 0a 20 20 20 20 2f 2a 20 55 6e 6c  (v);..    /* Unl
259c8 65 73 73 20 74 68 69 73 20 69 73 20 61 20 76 69  ess this is a vi
259c9 65 77 2c 20 6f 70 65 6e 20 63 75 72 73 6f 72 73  ew, open cursors
259ca 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 77   for the table w
259cb 65 20 61 72 65 20 0a 20 20 20 20 2a 2a 20 64 65  e are .    ** de
259cc 6c 65 74 69 6e 67 20 66 72 6f 6d 20 61 6e 64 20  leting from and 
259cd 61 6c 6c 20 69 74 73 20 69 6e 64 69 63 65 73 2e  all its indices.
259ce 20 49 66 20 74 68 69 73 20 69 73 20 61 20 76 69   If this is a vi
259cf 65 77 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 20  ew, then the.   
259d0 20 2a 2a 20 6f 6e 6c 79 20 65 66 66 65 63 74 20   ** only effect 
259d1 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 68  this statement h
259d2 61 73 20 69 73 20 74 6f 20 66 69 72 65 20 74 68  as is to fire th
259d3 65 20 49 4e 53 54 45 41 44 20 4f 46 20 0a 20 20  e INSTEAD OF .  
259d4 20 20 2a 2a 20 74 72 69 67 67 65 72 73 2e 20 20    ** triggers.  
259d5 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 73 56 69  */.    if( !isVi
259d6 65 77 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ew ){.      sqli
259d7 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49  te3OpenTableAndI
259d8 6e 64 69 63 65 73 28 70 50 61 72 73 65 2c 20 70  ndices(pParse, p
259d9 54 61 62 2c 20 69 43 75 72 2c 20 4f 50 5f 4f 70  Tab, iCur, OP_Op
259da 65 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 7d 0a  enWrite);.    }.
259db 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69  .    addr = sqli
259dc 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
259dd 20 4f 50 5f 52 6f 77 53 65 74 52 65 61 64 2c 20   OP_RowSetRead, 
259de 69 52 6f 77 53 65 74 2c 20 65 6e 64 2c 20 69 52  iRowSet, end, iR
259df 6f 77 69 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 44  owid);..    /* D
259e0 65 6c 65 74 65 20 74 68 65 20 72 6f 77 20 2a 2f  elete the row */
259e1 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
259e2 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
259e3 45 0a 20 20 20 20 69 66 28 20 49 73 56 69 72 74  E.    if( IsVirt
259e4 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
259e5 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
259e6 56 54 61 62 20 3d 20 28 63 6f 6e 73 74 20 63 68  VTab = (const ch
259e7 61 72 20 2a 29 73 71 6c 69 74 65 33 47 65 74 56  ar *)sqlite3GetV
259e8 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29 3b  Table(db, pTab);
259e9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 74  .      sqlite3Vt
259ea 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 70  abMakeWritable(p
259eb 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20  Parse, pTab);.  
259ec 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
259ed 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 55 70 64  ddOp4(v, OP_VUpd
259ee 61 74 65 2c 20 30 2c 20 31 2c 20 69 52 6f 77 69  ate, 0, 1, iRowi
259ef 64 2c 20 70 56 54 61 62 2c 20 50 34 5f 56 54 41  d, pVTab, P4_VTA
259f0 42 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  B);.      sqlite
259f1 33 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73 65  3MayAbort(pParse
259f2 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e  );.    }else.#en
259f3 64 69 66 0a 20 20 20 20 7b 0a 20 20 20 20 20 20  dif.    {.      
259f4 69 6e 74 20 63 6f 75 6e 74 20 3d 20 28 70 50 61  int count = (pPa
259f5 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 29 3b  rse->nested==0);
259f6 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 63      /* True to c
259f7 6f 75 6e 74 20 63 68 61 6e 67 65 73 20 2a 2f 0a  ount changes */.
259f8 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e        sqlite3Gen
259f9 65 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28 70  erateRowDelete(p
259fa 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75  Parse, pTab, iCu
259fb 72 2c 20 69 52 6f 77 69 64 2c 20 63 6f 75 6e 74  r, iRowid, count
259fc 2c 20 70 54 72 69 67 67 65 72 2c 20 4f 45 5f 44  , pTrigger, OE_D
259fd 65 66 61 75 6c 74 29 3b 0a 20 20 20 20 7d 0a 0a  efault);.    }..
259fe 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68      /* End of th
259ff 65 20 64 65 6c 65 74 65 20 6c 6f 6f 70 20 2a 2f  e delete loop */
25a00 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
25a01 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
25a02 6f 2c 20 30 2c 20 61 64 64 72 29 3b 0a 20 20 20  o, 0, addr);.   
25a03 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
25a04 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 29  lveLabel(v, end)
25a05 3b 0a 0a 20 20 20 20 2f 2a 20 43 6c 6f 73 65 20  ;..    /* Close 
25a06 74 68 65 20 63 75 72 73 6f 72 73 20 6f 70 65 6e  the cursors open
25a07 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 61 6e   on the table an
25a08 64 20 69 74 73 20 69 6e 64 65 78 65 73 2e 20 2a  d its indexes. *
25a09 2f 0a 20 20 20 20 69 66 28 20 21 69 73 56 69 65  /.    if( !isVie
25a0a 77 20 26 26 20 21 49 73 56 69 72 74 75 61 6c 28  w && !IsVirtual(
25a0b 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 66  pTab) ){.      f
25a0c 6f 72 28 69 3d 31 2c 20 70 49 64 78 3d 70 54 61  or(i=1, pIdx=pTa
25a0d 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
25a0e 20 69 2b 2b 2c 20 70 49 64 78 3d 70 49 64 78 2d   i++, pIdx=pIdx-
25a0f 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20  >pNext){.       
25a10 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
25a11 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p2(v, OP_Close, 
25a12 69 43 75 72 20 2b 20 69 2c 20 70 49 64 78 2d 3e  iCur + i, pIdx->
25a13 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20 7d 0a 20  tnum);.      }. 
25a14 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25a15 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
25a16 73 65 2c 20 69 43 75 72 29 3b 0a 20 20 20 20 7d  se, iCur);.    }
25a17 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74  .  }..  /* Updat
25a18 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71  e the sqlite_seq
25a19 75 65 6e 63 65 20 74 61 62 6c 65 20 62 79 20 73  uence table by s
25a1a 74 6f 72 69 6e 67 20 74 68 65 20 63 6f 6e 74 65  toring the conte
25a1b 6e 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6d  nt of the.  ** m
25a1c 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 63 6f 75  aximum rowid cou
25a1d 6e 74 65 72 20 76 61 6c 75 65 73 20 72 65 63 6f  nter values reco
25a1e 72 64 65 64 20 77 68 69 6c 65 20 69 6e 73 65 72  rded while inser
25a1f 74 69 6e 67 20 69 6e 74 6f 0a 20 20 2a 2a 20 61  ting into.  ** a
25a20 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 74 61 62  utoincrement tab
25a21 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  les..  */.  if( 
25a22 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
25a23 30 20 26 26 20 70 50 61 72 73 65 2d 3e 70 54 72  0 && pParse->pTr
25a24 69 67 67 65 72 54 61 62 3d 3d 30 20 29 7b 0a 20  iggerTab==0 ){. 
25a25 20 20 20 73 71 6c 69 74 65 33 41 75 74 6f 69 6e     sqlite3Autoin
25a26 63 72 65 6d 65 6e 74 45 6e 64 28 70 50 61 72 73  crementEnd(pPars
25a27 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65  e);.  }..  /* Re
25a28 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
25a29 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77 65 72  of rows that wer
25a2a 65 20 64 65 6c 65 74 65 64 2e 20 49 66 20 74 68  e deleted. If th
25a2b 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 0a 20  is routine is . 
25a2c 20 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 20 63   ** generating c
25a2d 6f 64 65 20 62 65 63 61 75 73 65 20 6f 66 20 61  ode because of a
25a2e 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
25a2f 4e 65 73 74 65 64 50 61 72 73 65 28 29 2c 20 64  NestedParse(), d
25a30 6f 20 6e 6f 74 0a 20 20 2a 2a 20 69 6e 76 6f 6b  o not.  ** invok
25a31 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66  e the callback f
25a32 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20  unction..  */.  
25a33 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 26 53  if( (db->flags&S
25a34 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29  QLITE_CountRows)
25a35 20 26 26 20 21 70 50 61 72 73 65 2d 3e 6e 65 73   && !pParse->nes
25a36 74 65 64 20 26 26 20 21 70 50 61 72 73 65 2d 3e  ted && !pParse->
25a37 70 54 72 69 67 67 65 72 54 61 62 20 29 7b 0a 20  pTriggerTab ){. 
25a38 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25a39 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
25a3a 74 52 6f 77 2c 20 6d 65 6d 43 6e 74 2c 20 31 29  tRow, memCnt, 1)
25a3b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
25a3c 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31  eSetNumCols(v, 1
25a3d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
25a3e 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
25a3f 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
25a40 20 22 72 6f 77 73 20 64 65 6c 65 74 65 64 22 2c   "rows deleted",
25a41 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
25a42 0a 20 20 7d 0a 0a 64 65 6c 65 74 65 5f 66 72 6f  .  }..delete_fro
25a43 6d 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c  m_cleanup:.  sql
25a44 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
25a45 6f 70 28 26 73 43 6f 6e 74 65 78 74 29 3b 0a 20  op(&sContext);. 
25a46 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
25a47 65 6c 65 74 65 28 64 62 2c 20 70 54 61 62 4c 69  elete(db, pTabLi
25a48 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  st);.  sqlite3Ex
25a49 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 57 68  prDelete(db, pWh
25a4a 65 72 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a  ere);.  return;.
25a4b 7d 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 22  }./* Make sure "
25a4c 69 73 56 69 65 77 22 20 61 6e 64 20 6f 74 68 65  isView" and othe
25a4d 72 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 64  r macros defined
25a4e 20 61 62 6f 76 65 20 61 72 65 20 75 6e 64 65 66   above are undef
25a4f 69 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 0a  ined. Otherwise.
25a50 2a 2a 20 74 68 65 6c 79 20 6d 61 79 20 69 6e 74  ** thely may int
25a51 65 72 66 65 72 65 20 77 69 74 68 20 63 6f 6d 70  erfere with comp
25a52 69 6c 61 74 69 6f 6e 20 6f 66 20 6f 74 68 65 72  ilation of other
25a53 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68   functions in th
25a54 69 73 20 66 69 6c 65 0a 2a 2a 20 28 6f 72 20 69  is file.** (or i
25a55 6e 20 61 6e 6f 74 68 65 72 20 66 69 6c 65 2c 20  n another file, 
25a56 69 66 20 74 68 69 73 20 66 69 6c 65 20 62 65 63  if this file bec
25a57 6f 6d 65 73 20 70 61 72 74 20 6f 66 20 74 68 65  omes part of the
25a58 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 29 2e 20   amalgamation). 
25a59 20 2a 2f 0a 23 69 66 64 65 66 20 69 73 56 69 65   */.#ifdef isVie
25a5a 77 0a 20 23 75 6e 64 65 66 20 69 73 56 69 65 77  w. #undef isView
25a5b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 70  .#endif.#ifdef p
25a5c 54 72 69 67 67 65 72 0a 20 23 75 6e 64 65 66 20  Trigger. #undef 
25a5d 70 54 72 69 67 67 65 72 0a 23 65 6e 64 69 66 0a  pTrigger.#endif.
25a5e 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
25a5f 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 56 44  ine generates VD
25a60 42 45 20 63 6f 64 65 20 74 68 61 74 20 63 61 75  BE code that cau
25a61 73 65 73 20 61 20 73 69 6e 67 6c 65 20 72 6f 77  ses a single row
25a62 20 6f 66 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20   of a.** single 
25a63 74 61 62 6c 65 20 74 6f 20 62 65 20 64 65 6c 65  table to be dele
25a64 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56  ted..**.** The V
25a65 44 42 45 20 6d 75 73 74 20 62 65 20 69 6e 20 61  DBE must be in a
25a66 20 70 61 72 74 69 63 75 6c 61 72 20 73 74 61 74   particular stat
25a67 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74  e when this rout
25a68 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a  ine is called..*
25a69 2a 20 54 68 65 73 65 20 61 72 65 20 74 68 65 20  * These are the 
25a6a 72 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  requirements:.**
25a6b 0a 2a 2a 20 20 20 31 2e 20 20 41 20 72 65 61 64  .**   1.  A read
25a6c 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 70 6f  /write cursor po
25a6d 69 6e 74 69 6e 67 20 74 6f 20 70 54 61 62 2c 20  inting to pTab, 
25a6e 74 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69  the table contai
25a6f 6e 69 6e 67 20 74 68 65 20 72 6f 77 0a 2a 2a 20  ning the row.** 
25a70 20 20 20 20 20 20 74 6f 20 62 65 20 64 65 6c 65        to be dele
25a71 74 65 64 2c 20 6d 75 73 74 20 62 65 20 6f 70 65  ted, must be ope
25a72 6e 65 64 20 61 73 20 63 75 72 73 6f 72 20 6e 75  ned as cursor nu
25a73 6d 62 65 72 20 24 69 43 75 72 2e 0a 2a 2a 0a 2a  mber $iCur..**.*
25a74 2a 20 20 20 32 2e 20 20 52 65 61 64 2f 77 72 69  *   2.  Read/wri
25a75 74 65 20 63 75 72 73 6f 72 73 20 66 6f 72 20 61  te cursors for a
25a76 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54  ll indices of pT
25a77 61 62 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20  ab must be open 
25a78 61 73 0a 2a 2a 20 20 20 20 20 20 20 63 75 72 73  as.**       curs
25a79 6f 72 20 6e 75 6d 62 65 72 20 62 61 73 65 2b 69  or number base+i
25a7a 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 69 6e   for the i-th in
25a7b 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20  dex..**.**   3. 
25a7c 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62   The record numb
25a7d 65 72 20 6f 66 20 74 68 65 20 72 6f 77 20 74 6f  er of the row to
25a7e 20 62 65 20 64 65 6c 65 74 65 64 20 6d 75 73 74   be deleted must
25a7f 20 62 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a   be stored in.**
25a80 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 63 65         memory ce
25a81 6c 6c 20 69 52 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a  ll iRowid..**.**
25a82 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65   This routine ge
25a83 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74 6f 20  nerates code to 
25a84 72 65 6d 6f 76 65 20 62 6f 74 68 20 74 68 65 20  remove both the 
25a85 74 61 62 6c 65 20 72 65 63 6f 72 64 20 61 6e 64  table record and
25a86 20 61 6c 6c 20 0a 2a 2a 20 69 6e 64 65 78 20 65   all .** index e
25a87 6e 74 72 69 65 73 20 74 68 61 74 20 70 6f 69 6e  ntries that poin
25a88 74 20 74 6f 20 74 68 61 74 20 72 65 63 6f 72 64  t to that record
25a89 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
25a8a 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
25a8b 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65 74  GenerateRowDelet
25a8c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
25a8d 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69  se,     /* Parsi
25a8e 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
25a8f 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20  Table *pTab,    
25a90 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6e 74     /* Table cont
25a91 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 20 74  aining the row t
25a92 6f 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a  o be deleted */.
25a93 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20    int iCur,     
25a94 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e       /* Cursor n
25a95 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 61  umber for the ta
25a96 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52 6f  ble */.  int iRo
25a97 77 69 64 2c 20 20 20 20 20 20 20 20 2f 2a 20 4d  wid,        /* M
25a98 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74 20  emory cell that 
25a99 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f 77  contains the row
25a9a 69 64 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a  id to delete */.
25a9b 20 20 69 6e 74 20 63 6f 75 6e 74 2c 20 20 20 20    int count,    
25a9c 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a       /* If non-z
25a9d 65 72 6f 2c 20 69 6e 63 72 65 6d 65 6e 74 20 74  ero, increment t
25a9e 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f  he row change co
25a9f 75 6e 74 65 72 20 2a 2f 0a 20 20 54 72 69 67 67  unter */.  Trigg
25aa0 65 72 20 2a 70 54 72 69 67 67 65 72 2c 20 2f 2a  er *pTrigger, /*
25aa1 20 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72   List of trigger
25aa2 73 20 74 6f 20 28 70 6f 74 65 6e 74 69 61 6c 6c  s to (potentiall
25aa3 79 29 20 66 69 72 65 20 2a 2f 0a 20 20 69 6e 74  y) fire */.  int
25aa4 20 6f 6e 63 6f 6e 66 20 20 20 20 20 20 20 20 20   onconf         
25aa5 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e 20 43 4f  /* Default ON CO
25aa6 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 66 6f  NFLICT policy fo
25aa7 72 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 29 7b  r triggers */.){
25aa8 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
25aa9 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 20 20 20  rse->pVdbe;     
25aaa 20 20 20 2f 2a 20 56 64 62 65 20 2a 2f 0a 20 20     /* Vdbe */.  
25aab 69 6e 74 20 69 4f 6c 64 20 3d 20 30 3b 20 20 20  int iOld = 0;   
25aac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25aad 2f 2a 20 46 69 72 73 74 20 72 65 67 69 73 74 65  /* First registe
25aae 72 20 69 6e 20 4f 4c 44 2e 2a 20 61 72 72 61 79  r in OLD.* array
25aaf 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 61 62 65 6c   */.  int iLabel
25ab0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
25ab1 20 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20 72        /* Label r
25ab2 65 73 6f 6c 76 65 64 20 74 6f 20 65 6e 64 20 6f  esolved to end o
25ab3 66 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65  f generated code
25ab4 20 2a 2f 0a 0a 20 20 2f 2a 20 56 64 62 65 20 69   */..  /* Vdbe i
25ab5 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20  s guaranteed to 
25ab6 68 61 76 65 20 62 65 65 6e 20 61 6c 6c 6f 63 61  have been alloca
25ab7 74 65 64 20 62 79 20 74 68 69 73 20 73 74 61 67  ted by this stag
25ab8 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  e. */.  assert( 
25ab9 76 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 65 6b 20  v );..  /* Seek 
25aba 63 75 72 73 6f 72 20 69 43 75 72 20 74 6f 20 74  cursor iCur to t
25abb 68 65 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 65  he row to delete
25abc 2e 20 49 66 20 74 68 69 73 20 72 6f 77 20 6e 6f  . If this row no
25abd 20 6c 6f 6e 67 65 72 20 65 78 69 73 74 73 20 0a   longer exists .
25abe 20 20 2a 2a 20 28 74 68 69 73 20 63 61 6e 20 68    ** (this can h
25abf 61 70 70 65 6e 20 69 66 20 61 20 74 72 69 67 67  appen if a trigg
25ac0 65 72 20 70 72 6f 67 72 61 6d 20 68 61 73 20 61  er program has a
25ac1 6c 72 65 61 64 79 20 64 65 6c 65 74 65 64 20 69  lready deleted i
25ac2 74 29 2c 20 64 6f 0a 20 20 2a 2a 20 6e 6f 74 20  t), do.  ** not 
25ac3 61 74 74 65 6d 70 74 20 74 6f 20 64 65 6c 65 74  attempt to delet
25ac4 65 20 69 74 20 6f 72 20 66 69 72 65 20 61 6e 79  e it or fire any
25ac5 20 44 45 4c 45 54 45 20 74 72 69 67 67 65 72 73   DELETE triggers
25ac6 2e 20 20 2a 2f 0a 20 20 69 4c 61 62 65 6c 20 3d  .  */.  iLabel =
25ac7 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
25ac8 4c 61 62 65 6c 28 76 29 3b 0a 20 20 73 71 6c 69  Label(v);.  sqli
25ac9 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
25aca 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69   OP_NotExists, i
25acb 43 75 72 2c 20 69 4c 61 62 65 6c 2c 20 69 52 6f  Cur, iLabel, iRo
25acc 77 69 64 29 3b 0a 20 0a 20 20 2f 2a 20 49 66 20  wid);. .  /* If 
25acd 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 74 72  there are any tr
25ace 69 67 67 65 72 73 20 74 6f 20 66 69 72 65 2c 20  iggers to fire, 
25acf 61 6c 6c 6f 63 61 74 65 20 61 20 72 61 6e 67 65  allocate a range
25ad0 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 74 6f   of registers to
25ad1 0a 20 20 2a 2a 20 75 73 65 20 66 6f 72 20 74 68  .  ** use for th
25ad2 65 20 6f 6c 64 2e 2a 20 72 65 66 65 72 65 6e 63  e old.* referenc
25ad3 65 73 20 69 6e 20 74 68 65 20 74 72 69 67 67 65  es in the trigge
25ad4 72 73 2e 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  rs.  */.  if( sq
25ad5 6c 69 74 65 33 46 6b 52 65 71 75 69 72 65 64 28  lite3FkRequired(
25ad6 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30 2c  pParse, pTab, 0,
25ad7 20 30 29 20 7c 7c 20 70 54 72 69 67 67 65 72 20   0) || pTrigger 
25ad8 29 7b 0a 20 20 20 20 75 33 32 20 6d 61 73 6b 3b  ){.    u32 mask;
25ad9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25ada 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
25adb 4f 4c 44 2e 2a 20 63 6f 6c 75 6d 6e 73 20 69 6e  OLD.* columns in
25adc 20 75 73 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20   use */.    int 
25add 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  iCol;           
25ade 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
25adf 72 61 74 6f 72 20 75 73 65 64 20 77 68 69 6c 65  rator used while
25ae0 20 70 6f 70 75 6c 61 74 69 6e 67 20 4f 4c 44 2e   populating OLD.
25ae1 2a 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 54 4f 44  * */..    /* TOD
25ae2 4f 3a 20 43 6f 75 6c 64 20 75 73 65 20 74 65 6d  O: Could use tem
25ae3 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 73  porary registers
25ae4 20 68 65 72 65 2e 20 41 6c 73 6f 20 63 6f 75 6c   here. Also coul
25ae5 64 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 20  d attempt to.   
25ae6 20 2a 2a 20 61 76 6f 69 64 20 63 6f 70 79 69 6e   ** avoid copyin
25ae7 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  g the contents o
25ae8 66 20 74 68 65 20 72 6f 77 69 64 20 72 65 67 69  f the rowid regi
25ae9 73 74 65 72 2e 20 20 2a 2f 0a 20 20 20 20 6d 61  ster.  */.    ma
25aea 73 6b 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67  sk = sqlite3Trig
25aeb 67 65 72 4f 6c 64 6d 61 73 6b 28 70 50 61 72 73  gerOldmask(pPars
25aec 65 2c 20 70 54 72 69 67 67 65 72 2c 20 30 2c 20  e, pTrigger, 0, 
25aed 70 54 61 62 2c 20 6f 6e 63 6f 6e 66 29 3b 0a 20  pTab, onconf);. 
25aee 20 20 20 6d 61 73 6b 20 7c 3d 20 73 71 6c 69 74     mask |= sqlit
25aef 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 70 50 61 72  e3FkOldmask(pPar
25af0 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 69  se, pTab);.    i
25af1 4f 6c 64 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d  Old = pParse->nM
25af2 65 6d 2b 31 3b 0a 20 20 20 20 70 50 61 72 73 65  em+1;.    pParse
25af3 2d 3e 6e 4d 65 6d 20 2b 3d 20 28 31 20 2b 20 70  ->nMem += (1 + p
25af4 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 0a 20 20 20  Tab->nCol);..   
25af5 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65   /* Populate the
25af6 20 4f 4c 44 2e 2a 20 70 73 65 75 64 6f 2d 74 61   OLD.* pseudo-ta
25af7 62 6c 65 20 72 65 67 69 73 74 65 72 20 61 72 72  ble register arr
25af8 61 79 2e 20 54 68 65 73 65 20 76 61 6c 75 65 73  ay. These values
25af9 20 77 69 6c 6c 20 62 65 20 0a 20 20 20 20 2a 2a   will be .    **
25afa 20 75 73 65 64 20 62 79 20 61 6e 79 20 42 45 46   used by any BEF
25afb 4f 52 45 20 61 6e 64 20 41 46 54 45 52 20 74 72  ORE and AFTER tr
25afc 69 67 67 65 72 73 20 74 68 61 74 20 65 78 69 73  iggers that exis
25afd 74 2e 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  t.  */.    sqlit
25afe 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
25aff 4f 50 5f 43 6f 70 79 2c 20 69 52 6f 77 69 64 2c  OP_Copy, iRowid,
25b00 20 69 4f 6c 64 29 3b 0a 20 20 20 20 66 6f 72 28   iOld);.    for(
25b01 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61  iCol=0; iCol<pTa
25b02 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29  b->nCol; iCol++)
25b03 7b 0a 20 20 20 20 20 20 69 66 28 20 6d 61 73 6b  {.      if( mask
25b04 3d 3d 30 78 66 66 66 66 66 66 66 66 20 7c 7c 20  ==0xffffffff || 
25b05 6d 61 73 6b 26 28 31 3c 3c 69 43 6f 6c 29 20 29  mask&(1<<iCol) )
25b06 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 54  {.        int iT
25b07 61 72 67 65 74 20 3d 20 69 4f 6c 64 20 2b 20 69  arget = iOld + i
25b08 43 6f 6c 20 2b 20 31 3b 0a 20 20 20 20 20 20 20  Col + 1;.       
25b09 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
25b0a 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
25b0b 20 69 43 75 72 2c 20 69 43 6f 6c 2c 20 69 54 61   iCur, iCol, iTa
25b0c 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73  rget);.        s
25b0d 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61  qlite3ColumnDefa
25b0e 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 43 6f  ult(v, pTab, iCo
25b0f 6c 2c 20 69 54 61 72 67 65 74 29 3b 0a 20 20 20  l, iTarget);.   
25b10 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
25b11 2f 2a 20 49 6e 76 6f 6b 65 20 42 45 46 4f 52 45  /* Invoke BEFORE
25b12 20 44 45 4c 45 54 45 20 74 72 69 67 67 65 72 20   DELETE trigger 
25b13 70 72 6f 67 72 61 6d 73 2e 20 2a 2f 0a 20 20 20  programs. */.   
25b14 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54   sqlite3CodeRowT
25b15 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 70  rigger(pParse, p
25b16 54 72 69 67 67 65 72 2c 20 0a 20 20 20 20 20 20  Trigger, .      
25b17 20 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c 20    TK_DELETE, 0, 
25b18 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c 20  TRIGGER_BEFORE, 
25b19 70 54 61 62 2c 20 69 4f 6c 64 2c 20 6f 6e 63 6f  pTab, iOld, onco
25b1a 6e 66 2c 20 69 4c 61 62 65 6c 0a 20 20 20 20 29  nf, iLabel.    )
25b1b 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 65 6b 20 74  ;..    /* Seek t
25b1c 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65  he cursor to the
25b1d 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c 65 74   row to be delet
25b1e 65 64 20 61 67 61 69 6e 2e 20 49 74 20 6d 61 79  ed again. It may
25b1f 20 62 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20   be that.    ** 
25b20 74 68 65 20 42 45 46 4f 52 45 20 74 72 69 67 67  the BEFORE trigg
25b21 65 72 73 20 63 6f 64 65 64 20 61 62 6f 76 65 20  ers coded above 
25b22 68 61 76 65 20 61 6c 72 65 61 64 79 20 72 65 6d  have already rem
25b23 6f 76 65 64 20 74 68 65 20 72 6f 77 0a 20 20 20  oved the row.   
25b24 20 2a 2a 20 62 65 69 6e 67 20 64 65 6c 65 74 65   ** being delete
25b25 64 2e 20 44 6f 20 6e 6f 74 20 61 74 74 65 6d 70  d. Do not attemp
25b26 74 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20  t to delete the 
25b27 72 6f 77 20 61 20 73 65 63 6f 6e 64 20 74 69 6d  row a second tim
25b28 65 2c 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 64  e, and .    ** d
25b29 6f 20 6e 6f 74 20 66 69 72 65 20 41 46 54 45 52  o not fire AFTER
25b2a 20 74 72 69 67 67 65 72 73 2e 20 20 2a 2f 0a 20   triggers.  */. 
25b2b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25b2c 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78  dOp3(v, OP_NotEx
25b2d 69 73 74 73 2c 20 69 43 75 72 2c 20 69 4c 61 62  ists, iCur, iLab
25b2e 65 6c 2c 20 69 52 6f 77 69 64 29 3b 0a 0a 20 20  el, iRowid);..  
25b2f 20 20 2f 2a 20 44 6f 20 46 4b 20 70 72 6f 63 65    /* Do FK proce
25b30 73 73 69 6e 67 2e 20 54 68 69 73 20 63 61 6c 6c  ssing. This call
25b31 20 63 68 65 63 6b 73 20 74 68 61 74 20 61 6e 79   checks that any
25b32 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20   FK constraints 
25b33 74 68 61 74 0a 20 20 20 20 2a 2a 20 72 65 66 65  that.    ** refe
25b34 72 20 74 6f 20 74 68 69 73 20 74 61 62 6c 65 20  r to this table 
25b35 28 69 2e 65 2e 20 63 6f 6e 73 74 72 61 69 6e 74  (i.e. constraint
25b36 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 6f 74  s attached to ot
25b37 68 65 72 20 74 61 62 6c 65 73 29 20 0a 20 20 20  her tables) .   
25b38 20 2a 2a 20 61 72 65 20 6e 6f 74 20 76 69 6f 6c   ** are not viol
25b39 61 74 65 64 20 62 79 20 64 65 6c 65 74 69 6e 67  ated by deleting
25b3a 20 74 68 69 73 20 72 6f 77 2e 20 20 2a 2f 0a 20   this row.  */. 
25b3b 20 20 20 73 71 6c 69 74 65 33 46 6b 43 68 65 63     sqlite3FkChec
25b3c 6b 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  k(pParse, pTab, 
25b3d 69 4f 6c 64 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20  iOld, 0);.  }.. 
25b3e 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 69   /* Delete the i
25b3f 6e 64 65 78 20 61 6e 64 20 74 61 62 6c 65 20 65  ndex and table e
25b40 6e 74 72 69 65 73 2e 20 53 6b 69 70 20 74 68 69  ntries. Skip thi
25b41 73 20 73 74 65 70 20 69 66 20 70 54 61 62 20 69  s step if pTab i
25b42 73 20 72 65 61 6c 6c 79 0a 20 20 2a 2a 20 61 20  s really.  ** a 
25b43 76 69 65 77 20 28 69 6e 20 77 68 69 63 68 20 63  view (in which c
25b44 61 73 65 20 74 68 65 20 6f 6e 6c 79 20 65 66 66  ase the only eff
25b45 65 63 74 20 6f 66 20 74 68 65 20 44 45 4c 45 54  ect of the DELET
25b46 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 74  E statement is t
25b47 6f 0a 20 20 2a 2a 20 66 69 72 65 20 74 68 65 20  o.  ** fire the 
25b48 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67  INSTEAD OF trigg
25b49 65 72 73 29 2e 20 20 2a 2f 20 0a 20 20 69 66 28  ers).  */ .  if(
25b4a 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d   pTab->pSelect==
25b4b 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
25b4c 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78  GenerateRowIndex
25b4d 44 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20 70  Delete(pParse, p
25b4e 54 61 62 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20  Tab, iCur, 0);. 
25b4f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25b50 64 4f 70 32 28 76 2c 20 4f 50 5f 44 65 6c 65 74  dOp2(v, OP_Delet
25b51 65 2c 20 69 43 75 72 2c 20 28 63 6f 75 6e 74 3f  e, iCur, (count?
25b52 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 3a 30  OPFLAG_NCHANGE:0
25b53 29 29 3b 0a 20 20 20 20 69 66 28 20 63 6f 75 6e  ));.    if( coun
25b54 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
25b55 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
25b56 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  , -1, pTab->zNam
25b57 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  e, P4_STATIC);. 
25b58 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44     }.  }..  /* D
25b59 6f 20 61 6e 79 20 4f 4e 20 43 41 53 43 41 44 45  o any ON CASCADE
25b5a 2c 20 53 45 54 20 4e 55 4c 4c 20 6f 72 20 53 45  , SET NULL or SE
25b5b 54 20 44 45 46 41 55 4c 54 20 6f 70 65 72 61 74  T DEFAULT operat
25b5c 69 6f 6e 73 20 72 65 71 75 69 72 65 64 20 74 6f  ions required to
25b5d 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20 72 6f 77  .  ** handle row
25b5e 73 20 28 70 6f 73 73 69 62 6c 79 20 69 6e 20 6f  s (possibly in o
25b5f 74 68 65 72 20 74 61 62 6c 65 73 29 20 74 68 61  ther tables) tha
25b60 74 20 72 65 66 65 72 20 76 69 61 20 61 20 66 6f  t refer via a fo
25b61 72 65 69 67 6e 20 6b 65 79 0a 20 20 2a 2a 20 74  reign key.  ** t
25b62 6f 20 74 68 65 20 72 6f 77 20 6a 75 73 74 20 64  o the row just d
25b63 65 6c 65 74 65 64 2e 20 2a 2f 20 0a 20 20 73 71  eleted. */ .  sq
25b64 6c 69 74 65 33 46 6b 41 63 74 69 6f 6e 73 28 70  lite3FkActions(p
25b65 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30 2c 20  Parse, pTab, 0, 
25b66 69 4f 6c 64 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76  iOld);..  /* Inv
25b67 6f 6b 65 20 41 46 54 45 52 20 44 45 4c 45 54 45  oke AFTER DELETE
25b68 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
25b69 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 43  s. */.  sqlite3C
25b6a 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50  odeRowTrigger(pP
25b6b 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20  arse, pTrigger, 
25b6c 0a 20 20 20 20 20 20 54 4b 5f 44 45 4c 45 54 45  .      TK_DELETE
25b6d 2c 20 30 2c 20 54 52 49 47 47 45 52 5f 41 46 54  , 0, TRIGGER_AFT
25b6e 45 52 2c 20 70 54 61 62 2c 20 69 4f 6c 64 2c 20  ER, pTab, iOld, 
25b6f 6f 6e 63 6f 6e 66 2c 20 69 4c 61 62 65 6c 0a 20  onconf, iLabel. 
25b70 20 29 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68   );..  /* Jump h
25b71 65 72 65 20 69 66 20 74 68 65 20 72 6f 77 20 68  ere if the row h
25b72 61 64 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ad already been 
25b73 64 65 6c 65 74 65 64 20 62 65 66 6f 72 65 20 61  deleted before a
25b74 6e 79 20 42 45 46 4f 52 45 0a 20 20 2a 2a 20 74  ny BEFORE.  ** t
25b75 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 20  rigger programs 
25b76 77 65 72 65 20 69 6e 76 6f 6b 65 64 2e 20 4f 72  were invoked. Or
25b77 20 69 66 20 61 20 74 72 69 67 67 65 72 20 70 72   if a trigger pr
25b78 6f 67 72 61 6d 20 74 68 72 6f 77 73 20 61 20 0a  ogram throws a .
25b79 20 20 2a 2a 20 52 41 49 53 45 28 49 47 4e 4f 52    ** RAISE(IGNOR
25b7a 45 29 20 65 78 63 65 70 74 69 6f 6e 2e 20 20 2a  E) exception.  *
25b7b 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52  /.  sqlite3VdbeR
25b7c 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69  esolveLabel(v, i
25b7d 4c 61 62 65 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Label);.}../*.**
25b7e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65   This routine ge
25b7f 6e 65 72 61 74 65 73 20 56 44 42 45 20 63 6f 64  nerates VDBE cod
25b80 65 20 74 68 61 74 20 63 61 75 73 65 73 20 74 68  e that causes th
25b81 65 20 64 65 6c 65 74 69 6f 6e 20 6f 66 20 61 6c  e deletion of al
25b82 6c 0a 2a 2a 20 69 6e 64 65 78 20 65 6e 74 72 69  l.** index entri
25b83 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  es associated wi
25b84 74 68 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  th a single row 
25b85 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c  of a single tabl
25b86 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 44 42  e..**.** The VDB
25b87 45 20 6d 75 73 74 20 62 65 20 69 6e 20 61 20 70  E must be in a p
25b88 61 72 74 69 63 75 6c 61 72 20 73 74 61 74 65 20  articular state 
25b89 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  when this routin
25b8a 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20  e is called..** 
25b8b 54 68 65 73 65 20 61 72 65 20 74 68 65 20 72 65  These are the re
25b8c 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a  quirements:.**.*
25b8d 2a 20 20 20 31 2e 20 20 41 20 72 65 61 64 2f 77  *   1.  A read/w
25b8e 72 69 74 65 20 63 75 72 73 6f 72 20 70 6f 69 6e  rite cursor poin
25b8f 74 69 6e 67 20 74 6f 20 70 54 61 62 2c 20 74 68  ting to pTab, th
25b90 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69  e table containi
25b91 6e 67 20 74 68 65 20 72 6f 77 0a 2a 2a 20 20 20  ng the row.**   
25b92 20 20 20 20 74 6f 20 62 65 20 64 65 6c 65 74 65      to be delete
25b93 64 2c 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65  d, must be opene
25b94 64 20 61 73 20 63 75 72 73 6f 72 20 6e 75 6d 62  d as cursor numb
25b95 65 72 20 22 69 43 75 72 22 2e 0a 2a 2a 0a 2a 2a  er "iCur"..**.**
25b96 20 20 20 32 2e 20 20 52 65 61 64 2f 77 72 69 74     2.  Read/writ
25b97 65 20 63 75 72 73 6f 72 73 20 66 6f 72 20 61 6c  e cursors for al
25b98 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54 61  l indices of pTa
25b99 62 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 61  b must be open a
25b9a 73 0a 2a 2a 20 20 20 20 20 20 20 63 75 72 73 6f  s.**       curso
25b9b 72 20 6e 75 6d 62 65 72 20 69 43 75 72 2b 69 20  r number iCur+i 
25b9c 66 6f 72 20 74 68 65 20 69 2d 74 68 20 69 6e 64  for the i-th ind
25b9d 65 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 20  ex..**.**   3.  
25b9e 54 68 65 20 22 69 43 75 72 22 20 63 75 72 73 6f  The "iCur" curso
25b9f 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69  r must be pointi
25ba0 6e 67 20 74 6f 20 74 68 65 20 72 6f 77 20 74 68  ng to the row th
25ba1 61 74 20 69 73 20 74 6f 20 62 65 0a 2a 2a 20 20  at is to be.**  
25ba2 20 20 20 20 20 64 65 6c 65 74 65 64 2e 0a 2a 2f       deleted..*/
25ba3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
25ba4 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65  void sqlite3Gene
25ba5 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65  rateRowIndexDele
25ba6 74 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  te(.  Parse *pPa
25ba7 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73  rse,     /* Pars
25ba8 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e  ing and code gen
25ba9 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20  erating context 
25baa 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
25bab 2c 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65  ,       /* Table
25bac 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
25bad 72 6f 77 20 74 6f 20 62 65 20 64 65 6c 65 74 65  row to be delete
25bae 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c  d */.  int iCur,
25baf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
25bb0 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  sor number for t
25bb1 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  he table */.  in
25bb2 74 20 2a 61 52 65 67 49 64 78 20 20 20 20 20 20  t *aRegIdx      
25bb3 20 2f 2a 20 4f 6e 6c 79 20 64 65 6c 65 74 65 20   /* Only delete 
25bb4 69 66 20 61 52 65 67 49 64 78 21 3d 30 20 26 26  if aRegIdx!=0 &&
25bb5 20 61 52 65 67 49 64 78 5b 69 5d 3e 30 20 2a 2f   aRegIdx[i]>0 */
25bb6 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 49  .){.  int i;.  I
25bb7 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 69 6e  ndex *pIdx;.  in
25bb8 74 20 72 31 3b 0a 0a 20 20 66 6f 72 28 69 3d 31  t r1;..  for(i=1
25bb9 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  , pIdx=pTab->pIn
25bba 64 65 78 3b 20 70 49 64 78 3b 20 69 2b 2b 2c 20  dex; pIdx; i++, 
25bbb 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
25bbc 29 7b 0a 20 20 20 20 69 66 28 20 61 52 65 67 49  ){.    if( aRegI
25bbd 64 78 21 3d 30 20 26 26 20 61 52 65 67 49 64 78  dx!=0 && aRegIdx
25bbe 5b 69 2d 31 5d 3d 3d 30 20 29 20 63 6f 6e 74 69  [i-1]==0 ) conti
25bbf 6e 75 65 3b 0a 20 20 20 20 72 31 20 3d 20 73 71  nue;.    r1 = sq
25bc0 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64  lite3GenerateInd
25bc1 65 78 4b 65 79 28 70 50 61 72 73 65 2c 20 70 49  exKey(pParse, pI
25bc2 64 78 2c 20 69 43 75 72 2c 20 30 2c 20 30 29 3b  dx, iCur, 0, 0);
25bc3 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
25bc4 41 64 64 4f 70 33 28 70 50 61 72 73 65 2d 3e 70  AddOp3(pParse->p
25bc5 56 64 62 65 2c 20 4f 50 5f 49 64 78 44 65 6c 65  Vdbe, OP_IdxDele
25bc6 74 65 2c 20 69 43 75 72 2b 69 2c 20 72 31 2c 70  te, iCur+i, r1,p
25bc7 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b  Idx->nColumn+1);
25bc8 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  .  }.}../*.** Ge
25bc9 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
25bca 20 77 69 6c 6c 20 61 73 73 65 6d 62 6c 65 20 61   will assemble a
25bcb 6e 20 69 6e 64 65 78 20 6b 65 79 20 61 6e 64 20  n index key and 
25bcc 70 75 74 20 69 74 20 69 6e 20 72 65 67 69 73 74  put it in regist
25bcd 65 72 0a 2a 2a 20 72 65 67 4f 75 74 2e 20 20 54  er.** regOut.  T
25bce 68 65 20 6b 65 79 20 77 69 74 68 20 62 65 20 66  he key with be f
25bcf 6f 72 20 69 6e 64 65 78 20 70 49 64 78 20 77 68  or index pIdx wh
25bd0 69 63 68 20 69 73 20 61 6e 20 69 6e 64 65 78 20  ich is an index 
25bd1 6f 6e 20 70 54 61 62 2e 0a 2a 2a 20 69 43 75 72  on pTab..** iCur
25bd2 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66   is the index of
25bd3 20 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 20 6f   a cursor open o
25bd4 6e 20 74 68 65 20 70 54 61 62 20 74 61 62 6c 65  n the pTab table
25bd5 20 61 6e 64 20 70 6f 69 6e 74 69 6e 67 20 74 6f   and pointing to
25bd6 0a 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 74 68  .** the entry th
25bd7 61 74 20 6e 65 65 64 73 20 69 6e 64 65 78 69 6e  at needs indexin
25bd8 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  g..**.** Return 
25bd9 61 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65  a register numbe
25bda 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 66  r which is the f
25bdb 69 72 73 74 20 69 6e 20 61 20 62 6c 6f 63 6b 20  irst in a block 
25bdc 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20  of.** registers 
25bdd 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 65  that holds the e
25bde 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 69  lements of the i
25bdf 6e 64 65 78 20 6b 65 79 2e 20 20 54 68 65 0a 2a  ndex key.  The.*
25be0 2a 20 62 6c 6f 63 6b 20 6f 66 20 72 65 67 69 73  * block of regis
25be1 74 65 72 73 20 68 61 73 20 61 6c 72 65 61 64 79  ters has already
25be2 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65   been deallocate
25be3 64 20 62 79 20 74 68 65 20 74 69 6d 65 0a 2a 2a  d by the time.**
25be4 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
25be5 74 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  turns..*/.SQLITE
25be6 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
25be7 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65  ite3GenerateInde
25be8 78 4b 65 79 28 0a 20 20 50 61 72 73 65 20 2a 70  xKey(.  Parse *p
25be9 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61  Parse,     /* Pa
25bea 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
25beb 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 2c 20  .  Index *pIdx, 
25bec 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64        /* The ind
25bed 65 78 20 66 6f 72 20 77 68 69 63 68 20 74 6f 20  ex for which to 
25bee 67 65 6e 65 72 61 74 65 20 61 20 6b 65 79 20 2a  generate a key *
25bef 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20  /.  int iCur,   
25bf0 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
25bf1 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
25bf2 70 49 64 78 2d 3e 70 54 61 62 6c 65 20 74 61 62  pIdx->pTable tab
25bf3 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f  le */.  int regO
25bf4 75 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 72  ut,        /* Wr
25bf5 69 74 65 20 74 68 65 20 6e 65 77 20 69 6e 64 65  ite the new inde
25bf6 78 20 6b 65 79 20 74 6f 20 74 68 69 73 20 72 65  x key to this re
25bf7 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  gister */.  int 
25bf8 64 6f 4d 61 6b 65 52 65 63 20 20 20 20 20 20 2f  doMakeRec      /
25bf9 2a 20 52 75 6e 20 74 68 65 20 4f 50 5f 4d 61 6b  * Run the OP_Mak
25bfa 65 52 65 63 6f 72 64 20 69 6e 73 74 72 75 63 74  eRecord instruct
25bfb 69 6f 6e 20 69 66 20 74 72 75 65 20 2a 2f 0a 29  ion if true */.)
25bfc 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
25bfd 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
25bfe 6e 74 20 6a 3b 0a 20 20 54 61 62 6c 65 20 2a 70  nt j;.  Table *p
25bff 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 61 62  Tab = pIdx->pTab
25c00 6c 65 3b 0a 20 20 69 6e 74 20 72 65 67 42 61 73  le;.  int regBas
25c01 65 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 0a  e;.  int nCol;..
25c02 20 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e    nCol = pIdx->n
25c03 43 6f 6c 75 6d 6e 3b 0a 20 20 72 65 67 42 61 73  Column;.  regBas
25c04 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  e = sqlite3GetTe
25c05 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
25c06 6e 43 6f 6c 2b 31 29 3b 0a 20 20 73 71 6c 69 74  nCol+1);.  sqlit
25c07 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
25c08 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c 20  OP_Rowid, iCur, 
25c09 72 65 67 42 61 73 65 2b 6e 43 6f 6c 29 3b 0a 20  regBase+nCol);. 
25c0a 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 43 6f 6c   for(j=0; j<nCol
25c0b 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20  ; j++){.    int 
25c0c 69 64 78 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f  idx = pIdx->aiCo
25c0d 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 69 66 28  lumn[j];.    if(
25c0e 20 69 64 78 3d 3d 70 54 61 62 2d 3e 69 50 4b 65   idx==pTab->iPKe
25c0f 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  y ){.      sqlit
25c10 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
25c11 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 42 61 73  OP_SCopy, regBas
25c12 65 2b 6e 43 6f 6c 2c 20 72 65 67 42 61 73 65 2b  e+nCol, regBase+
25c13 6a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  j);.    }else{. 
25c14 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25c15 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
25c16 75 6d 6e 2c 20 69 43 75 72 2c 20 69 64 78 2c 20  umn, iCur, idx, 
25c17 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20 20 20  regBase+j);.    
25c18 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44    sqlite3ColumnD
25c19 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c 20  efault(v, pTab, 
25c1a 69 64 78 2c 20 2d 31 29 3b 0a 20 20 20 20 7d 0a  idx, -1);.    }.
25c1b 20 20 7d 0a 20 20 69 66 28 20 64 6f 4d 61 6b 65    }.  if( doMake
25c1c 52 65 63 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Rec ){.    sqlit
25c1d 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
25c1e 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
25c1f 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2b 31 2c 20  egBase, nCol+1, 
25c20 72 65 67 4f 75 74 29 3b 0a 20 20 20 20 73 71 6c  regOut);.    sql
25c21 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
25c22 28 76 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 49  (v, -1, sqlite3I
25c23 6e 64 65 78 41 66 66 69 6e 69 74 79 53 74 72 28  ndexAffinityStr(
25c24 76 2c 20 70 49 64 78 29 2c 20 30 29 3b 0a 20 20  v, pIdx), 0);.  
25c25 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
25c26 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
25c27 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73 65  (pParse, regBase
25c28 2c 20 6e 43 6f 6c 2b 31 29 3b 0a 20 20 7d 0a 20  , nCol+1);.  }. 
25c29 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
25c2a 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
25c2b 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2b 31   regBase, nCol+1
25c2c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 67 42  );.  return regB
25c2d 61 73 65 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a  ase;.}.../******
25c2e 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
25c2f 64 65 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  delete.c *******
25c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25c31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25c32 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
25c33 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
25c34 69 6c 65 20 66 75 6e 63 2e 63 20 2a 2a 2a 2a 2a  ile func.c *****
25c35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25c36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25c37 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
25c38 30 30 32 20 46 65 62 72 75 61 72 79 20 32 33 0a  002 February 23.
25c39 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
25c3a 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
25c3b 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
25c3c 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
25c3d 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
25c3e 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
25c3f 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
25c40 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
25c41 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
25c42 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
25c43 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
25c44 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
25c45 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
25c46 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
25c47 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
25c48 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
25c49 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
25c4a 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
25c4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25c4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25c4d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25c4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
25c4f 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
25c50 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74  ains the C funct
25c51 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d  ions that implem
25c52 65 6e 74 20 76 61 72 69 6f 75 73 20 53 51 4c 0a  ent various SQL.
25c53 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 6f 66 20  ** functions of 
25c54 53 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20  SQLite.  .**.** 
25c55 54 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e  There is only on
25c56 65 20 65 78 70 6f 72 74 65 64 20 73 79 6d 62 6f  e exported symbo
25c57 6c 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 2d  l in this file -
25c58 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a   the function.**
25c59 20 73 71 6c 69 74 65 52 65 67 69 73 74 65 72 42   sqliteRegisterB
25c5a 75 69 6c 64 69 6e 46 75 6e 63 74 69 6f 6e 73 28  uildinFunctions(
25c5b 29 20 66 6f 75 6e 64 20 61 74 20 74 68 65 20 62  ) found at the b
25c5c 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 66 69 6c  ottom of the fil
25c5d 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20  e..** All other 
25c5e 63 6f 64 65 20 68 61 73 20 66 69 6c 65 20 73 63  code has file sc
25c5f 6f 70 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52  ope..*/../*.** R
25c60 65 74 75 72 6e 20 74 68 65 20 63 6f 6c 6c 61 74  eturn the collat
25c61 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 61 73 73  ing function ass
25c62 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 66  ociated with a f
25c63 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  unction..*/.stat
25c64 69 63 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69  ic CollSeq *sqli
25c65 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65  te3GetFuncCollSe
25c66 71 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  q(sqlite3_contex
25c67 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 72  t *context){.  r
25c68 65 74 75 72 6e 20 63 6f 6e 74 65 78 74 2d 3e 70  eturn context->p
25c69 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  Coll;.}../*.** I
25c6a 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
25c6b 20 74 68 65 20 6e 6f 6e 2d 61 67 67 72 65 67 61   the non-aggrega
25c6c 74 65 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78  te min() and max
25c6d 28 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2f 0a  () functions.*/.
25c6e 73 74 61 74 69 63 20 76 6f 69 64 20 6d 69 6e 6d  static void minm
25c6f 61 78 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  axFunc(.  sqlite
25c70 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
25c71 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
25c72 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
25c73 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20  **argv.){.  int 
25c74 69 3b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 20 20  i;.  int mask;  
25c75 20 20 2f 2a 20 30 20 66 6f 72 20 6d 69 6e 28 29    /* 0 for min()
25c76 20 6f 72 20 30 78 66 66 66 66 66 66 66 66 20 66   or 0xffffffff f
25c77 6f 72 20 6d 61 78 28 29 20 2a 2f 0a 20 20 69 6e  or max() */.  in
25c78 74 20 69 42 65 73 74 3b 0a 20 20 43 6f 6c 6c 53  t iBest;.  CollS
25c79 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 61 73  eq *pColl;..  as
25c7a 73 65 72 74 28 20 61 72 67 63 3e 31 20 29 3b 0a  sert( argc>1 );.
25c7b 20 20 6d 61 73 6b 20 3d 20 73 71 6c 69 74 65 33    mask = sqlite3
25c7c 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65  _user_data(conte
25c7d 78 74 29 3d 3d 30 20 3f 20 30 20 3a 20 2d 31 3b  xt)==0 ? 0 : -1;
25c7e 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  .  pColl = sqlit
25c7f 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71  e3GetFuncCollSeq
25c80 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 61 73 73  (context);.  ass
25c81 65 72 74 28 20 70 43 6f 6c 6c 20 29 3b 0a 20 20  ert( pColl );.  
25c82 61 73 73 65 72 74 28 20 6d 61 73 6b 3d 3d 2d 31  assert( mask==-1
25c83 20 7c 7c 20 6d 61 73 6b 3d 3d 30 20 29 3b 0a 20   || mask==0 );. 
25c84 20 69 42 65 73 74 20 3d 20 30 3b 0a 20 20 69 66   iBest = 0;.  if
25c85 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
25c86 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53  type(argv[0])==S
25c87 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74  QLITE_NULL ) ret
25c88 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20  urn;.  for(i=1; 
25c89 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20  i<argc; i++){.  
25c8a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61    if( sqlite3_va
25c8b 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 69 5d  lue_type(argv[i]
25c8c 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  )==SQLITE_NULL )
25c8d 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 66 28   return;.    if(
25c8e 20 28 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70   (sqlite3MemComp
25c8f 61 72 65 28 61 72 67 76 5b 69 42 65 73 74 5d 2c  are(argv[iBest],
25c90 20 61 72 67 76 5b 69 5d 2c 20 70 43 6f 6c 6c 29   argv[i], pColl)
25c91 5e 6d 61 73 6b 29 3e 3d 30 20 29 7b 0a 20 20 20  ^mask)>=0 ){.   
25c92 20 20 20 74 65 73 74 63 61 73 65 28 20 6d 61 73     testcase( mas
25c93 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 42  k==0 );.      iB
25c94 65 73 74 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20  est = i;.    }. 
25c95 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73   }.  sqlite3_res
25c96 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78  ult_value(contex
25c97 74 2c 20 61 72 67 76 5b 69 42 65 73 74 5d 29 3b  t, argv[iBest]);
25c98 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
25c99 20 74 68 65 20 74 79 70 65 20 6f 66 20 74 68 65   the type of the
25c9a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74   argument..*/.st
25c9b 61 74 69 63 20 76 6f 69 64 20 74 79 70 65 6f 66  atic void typeof
25c9c 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
25c9d 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
25c9e 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c  ,.  int NotUsed,
25c9f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
25ca0 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e   **argv.){.  con
25ca1 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 30 3b 0a  st char *z = 0;.
25ca2 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
25ca3 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 73  ER(NotUsed);.  s
25ca4 77 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76  witch( sqlite3_v
25ca5 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30  alue_type(argv[0
25ca6 5d 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53  ]) ){.    case S
25ca7 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7a  QLITE_INTEGER: z
25ca8 20 3d 20 22 69 6e 74 65 67 65 72 22 3b 20 62 72   = "integer"; br
25ca9 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51  eak;.    case SQ
25caa 4c 49 54 45 5f 54 45 58 54 3a 20 20 20 20 7a 20  LITE_TEXT:    z 
25cab 3d 20 22 74 65 78 74 22 3b 20 20 20 20 62 72 65  = "text";    bre
25cac 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c  ak;.    case SQL
25cad 49 54 45 5f 46 4c 4f 41 54 3a 20 20 20 7a 20 3d  ITE_FLOAT:   z =
25cae 20 22 72 65 61 6c 22 3b 20 20 20 20 62 72 65 61   "real";    brea
25caf 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  k;.    case SQLI
25cb0 54 45 5f 42 4c 4f 42 3a 20 20 20 20 7a 20 3d 20  TE_BLOB:    z = 
25cb1 22 62 6c 6f 62 22 3b 20 20 20 20 62 72 65 61 6b  "blob";    break
25cb2 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20  ;.    default:  
25cb3 20 20 20 20 20 20 20 20 20 20 20 7a 20 3d 20 22             z = "
25cb4 6e 75 6c 6c 22 3b 20 20 20 20 62 72 65 61 6b 3b  null";    break;
25cb5 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72  .  }.  sqlite3_r
25cb6 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
25cb7 78 74 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54  xt, z, -1, SQLIT
25cb8 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 0a 2f  E_STATIC);.}.../
25cb9 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
25cba 69 6f 6e 20 6f 66 20 74 68 65 20 6c 65 6e 67 74  ion of the lengt
25cbb 68 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a  h() function.*/.
25cbc 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 6e 67  static void leng
25cbd 74 68 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  thFunc(.  sqlite
25cbe 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
25cbf 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
25cc0 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
25cc1 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20  **argv.){.  int 
25cc2 6c 65 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  len;..  assert( 
25cc3 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55  argc==1 );.  UNU
25cc4 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72  SED_PARAMETER(ar
25cc5 67 63 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73  gc);.  switch( s
25cc6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
25cc7 65 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20  e(argv[0]) ){.  
25cc8 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c    case SQLITE_BL
25cc9 4f 42 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c  OB:.    case SQL
25cca 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20  ITE_INTEGER:.   
25ccb 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f   case SQLITE_FLO
25ccc 41 54 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  AT: {.      sqli
25ccd 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63  te3_result_int(c
25cce 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f  ontext, sqlite3_
25ccf 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
25cd0 5b 30 5d 29 29 3b 0a 20 20 20 20 20 20 62 72 65  [0]));.      bre
25cd1 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
25cd2 73 65 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20  se SQLITE_TEXT: 
25cd3 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75 6e  {.      const un
25cd4 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 20 3d  signed char *z =
25cd5 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
25cd6 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
25cd7 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72      if( z==0 ) r
25cd8 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 6c 65 6e  eturn;.      len
25cd9 20 3d 20 30 3b 0a 20 20 20 20 20 20 77 68 69 6c   = 0;.      whil
25cda 65 28 20 2a 7a 20 29 7b 0a 20 20 20 20 20 20 20  e( *z ){.       
25cdb 20 6c 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20   len++;.        
25cdc 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38  SQLITE_SKIP_UTF8
25cdd 28 7a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  (z);.      }.   
25cde 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
25cdf 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 6c  t_int(context, l
25ce0 65 6e 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  en);.      break
25ce1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
25ce2 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  ult: {.      sql
25ce3 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
25ce4 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20  (context);.     
25ce5 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
25ce6 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  }.}../*.** Imple
25ce7 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
25ce8 20 61 62 73 28 29 20 66 75 6e 63 74 69 6f 6e 0a   abs() function.
25ce9 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
25cea 62 73 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63  bsFunc(sqlite3_c
25ceb 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
25cec 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
25ced 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29  e3_value **argv)
25cee 7b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  {.  assert( argc
25cef 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f  ==1 );.  UNUSED_
25cf0 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b  PARAMETER(argc);
25cf1 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c 69 74  .  switch( sqlit
25cf2 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
25cf3 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63 61  gv[0]) ){.    ca
25cf4 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  se SQLITE_INTEGE
25cf5 52 3a 20 7b 0a 20 20 20 20 20 20 69 36 34 20 69  R: {.      i64 i
25cf6 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  Val = sqlite3_va
25cf7 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30  lue_int64(argv[0
25cf8 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 56  ]);.      if( iV
25cf9 61 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  al<0 ){.        
25cfa 69 66 28 20 28 69 56 61 6c 3c 3c 31 29 3d 3d 30  if( (iVal<<1)==0
25cfb 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
25cfc 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
25cfd 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 69 6e 74  or(context, "int
25cfe 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 22 2c 20  eger overflow", 
25cff 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  -1);.          r
25d00 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d  eturn;.        }
25d01 0a 20 20 20 20 20 20 20 20 69 56 61 6c 20 3d 20  .        iVal = 
25d02 2d 69 56 61 6c 3b 0a 20 20 20 20 20 20 7d 20 0a  -iVal;.      } .
25d03 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
25d04 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65  sult_int64(conte
25d05 78 74 2c 20 69 56 61 6c 29 3b 0a 20 20 20 20 20  xt, iVal);.     
25d06 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
25d07 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55    case SQLITE_NU
25d08 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  LL: {.      sqli
25d09 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28  te3_result_null(
25d0a 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  context);.      
25d0b 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
25d0c 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
25d0d 20 20 64 6f 75 62 6c 65 20 72 56 61 6c 20 3d 20    double rVal = 
25d0e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f  sqlite3_value_do
25d0f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20  uble(argv[0]);. 
25d10 20 20 20 20 20 69 66 28 20 72 56 61 6c 3c 30 20       if( rVal<0 
25d11 29 20 72 56 61 6c 20 3d 20 2d 72 56 61 6c 3b 0a  ) rVal = -rVal;.
25d12 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
25d13 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74  sult_double(cont
25d14 65 78 74 2c 20 72 56 61 6c 29 3b 0a 20 20 20 20  ext, rVal);.    
25d15 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
25d16 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c   }.}../*.** Impl
25d17 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
25d18 65 20 73 75 62 73 74 72 28 29 20 66 75 6e 63 74  e substr() funct
25d19 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 73 74  ion..**.** subst
25d1a 72 28 78 2c 70 31 2c 70 32 29 20 20 72 65 74 75  r(x,p1,p2)  retu
25d1b 72 6e 73 20 70 32 20 63 68 61 72 61 63 74 65 72  rns p2 character
25d1c 73 20 6f 66 20 78 5b 5d 20 62 65 67 69 6e 6e 69  s of x[] beginni
25d1d 6e 67 20 77 69 74 68 20 70 31 2e 0a 2a 2a 20 70  ng with p1..** p
25d1e 31 20 69 73 20 31 2d 69 6e 64 65 78 65 64 2e 20  1 is 1-indexed. 
25d1f 20 53 6f 20 73 75 62 73 74 72 28 78 2c 31 2c 31   So substr(x,1,1
25d20 29 20 72 65 74 75 72 6e 73 20 74 68 65 20 66 69  ) returns the fi
25d21 72 73 74 20 63 68 61 72 61 63 74 65 72 0a 2a 2a  rst character.**
25d22 20 6f 66 20 78 2e 20 20 49 66 20 78 20 69 73 20   of x.  If x is 
25d23 74 65 78 74 2c 20 74 68 65 6e 20 77 65 20 61 63  text, then we ac
25d24 74 75 61 6c 6c 79 20 63 6f 75 6e 74 20 55 54 46  tually count UTF
25d25 2d 38 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a  -8 characters..*
25d26 2a 20 49 66 20 78 20 69 73 20 61 20 62 6c 6f 62  * If x is a blob
25d27 2c 20 74 68 65 6e 20 77 65 20 63 6f 75 6e 74 20  , then we count 
25d28 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  bytes..**.** If 
25d29 70 31 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20  p1 is negative, 
25d2a 74 68 65 6e 20 77 65 20 62 65 67 69 6e 20 61 62  then we begin ab
25d2b 73 28 70 31 29 20 66 72 6f 6d 20 74 68 65 20 65  s(p1) from the e
25d2c 6e 64 20 6f 66 20 78 5b 5d 2e 0a 2a 2f 0a 73 74  nd of x[]..*/.st
25d2d 61 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 72  atic void substr
25d2e 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
25d2f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
25d30 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
25d31 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
25d32 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  argv.){.  const 
25d33 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
25d34 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ;.  const unsign
25d35 65 64 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69  ed char *z2;.  i
25d36 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 70 30  nt len;.  int p0
25d37 74 79 70 65 3b 0a 20 20 69 36 34 20 70 31 2c 20  type;.  i64 p1, 
25d38 70 32 3b 0a 20 20 69 6e 74 20 6e 65 67 50 32 20  p2;.  int negP2 
25d39 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  = 0;..  assert( 
25d3a 61 72 67 63 3d 3d 33 20 7c 7c 20 61 72 67 63 3d  argc==3 || argc=
25d3b 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  =2 );.  if( sqli
25d3c 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
25d3d 72 67 76 5b 31 5d 29 3d 3d 53 51 4c 49 54 45 5f  rgv[1])==SQLITE_
25d3e 4e 55 4c 4c 0a 20 20 20 7c 7c 20 28 61 72 67 63  NULL.   || (argc
25d3f 3d 3d 33 20 26 26 20 73 71 6c 69 74 65 33 5f 76  ==3 && sqlite3_v
25d40 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 32  alue_type(argv[2
25d41 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 29  ])==SQLITE_NULL)
25d42 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  .  ){.    return
25d43 3b 0a 20 20 7d 0a 20 20 70 30 74 79 70 65 20 3d  ;.  }.  p0type =
25d44 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
25d45 79 70 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ype(argv[0]);.  
25d46 69 66 28 20 70 30 74 79 70 65 3d 3d 53 51 4c 49  if( p0type==SQLI
25d47 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 6c  TE_BLOB ){.    l
25d48 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  en = sqlite3_val
25d49 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
25d4a 29 3b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  );.    z = sqlit
25d4b 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72  e3_value_blob(ar
25d4c 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 20  gv[0]);.    if( 
25d4d 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  z==0 ) return;. 
25d4e 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3d 3d     assert( len==
25d4f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
25d50 74 65 73 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a  tes(argv[0]) );.
25d51 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d    }else{.    z =
25d52 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
25d53 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
25d54 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74    if( z==0 ) ret
25d55 75 72 6e 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 30  urn;.    len = 0
25d56 3b 0a 20 20 20 20 66 6f 72 28 7a 32 3d 7a 3b 20  ;.    for(z2=z; 
25d57 2a 7a 32 3b 20 6c 65 6e 2b 2b 29 7b 0a 20 20 20  *z2; len++){.   
25d58 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55     SQLITE_SKIP_U
25d59 54 46 38 28 7a 32 29 3b 0a 20 20 20 20 7d 0a 20  TF8(z2);.    }. 
25d5a 20 7d 0a 20 20 70 31 20 3d 20 73 71 6c 69 74 65   }.  p1 = sqlite
25d5b 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76  3_value_int(argv
25d5c 5b 31 5d 29 3b 0a 20 20 69 66 28 20 61 72 67 63  [1]);.  if( argc
25d5d 3d 3d 33 20 29 7b 0a 20 20 20 20 70 32 20 3d 20  ==3 ){.    p2 = 
25d5e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
25d5f 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20  t(argv[2]);.    
25d60 69 66 28 20 70 32 3c 30 20 29 7b 0a 20 20 20 20  if( p2<0 ){.    
25d61 20 20 70 32 20 3d 20 2d 70 32 3b 0a 20 20 20 20    p2 = -p2;.    
25d62 20 20 6e 65 67 50 32 20 3d 20 31 3b 0a 20 20 20    negP2 = 1;.   
25d63 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
25d64 70 32 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  p2 = sqlite3_con
25d65 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
25d66 6f 6e 74 65 78 74 29 2d 3e 61 4c 69 6d 69 74 5b  ontext)->aLimit[
25d67 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
25d68 47 54 48 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20  GTH];.  }.  if( 
25d69 70 31 3c 30 20 29 7b 0a 20 20 20 20 70 31 20 2b  p1<0 ){.    p1 +
25d6a 3d 20 6c 65 6e 3b 0a 20 20 20 20 69 66 28 20 70  = len;.    if( p
25d6b 31 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 32 20  1<0 ){.      p2 
25d6c 2b 3d 20 70 31 3b 0a 20 20 20 20 20 20 69 66 28  += p1;.      if(
25d6d 20 70 32 3c 30 20 29 20 70 32 20 3d 20 30 3b 0a   p2<0 ) p2 = 0;.
25d6e 20 20 20 20 20 20 70 31 20 3d 20 30 3b 0a 20 20        p1 = 0;.  
25d6f 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
25d70 70 31 3e 30 20 29 7b 0a 20 20 20 20 70 31 2d 2d  p1>0 ){.    p1--
25d71 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 32  ;.  }else if( p2
25d72 3e 30 20 29 7b 0a 20 20 20 20 70 32 2d 2d 3b 0a  >0 ){.    p2--;.
25d73 20 20 7d 0a 20 20 69 66 28 20 6e 65 67 50 32 20    }.  if( negP2 
25d74 29 7b 0a 20 20 20 20 70 31 20 2d 3d 20 70 32 3b  ){.    p1 -= p2;
25d75 0a 20 20 20 20 69 66 28 20 70 31 3c 30 20 29 7b  .    if( p1<0 ){
25d76 0a 20 20 20 20 20 20 70 32 20 2b 3d 20 70 31 3b  .      p2 += p1;
25d77 0a 20 20 20 20 20 20 70 31 20 3d 20 30 3b 0a 20  .      p1 = 0;. 
25d78 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72     }.  }.  asser
25d79 74 28 20 70 31 3e 3d 30 20 26 26 20 70 32 3e 3d  t( p1>=0 && p2>=
25d7a 30 20 29 3b 0a 20 20 69 66 28 20 70 31 2b 70 32  0 );.  if( p1+p2
25d7b 3e 6c 65 6e 20 29 7b 0a 20 20 20 20 70 32 20 3d  >len ){.    p2 =
25d7c 20 6c 65 6e 2d 70 31 3b 0a 20 20 20 20 69 66 28   len-p1;.    if(
25d7d 20 70 32 3c 30 20 29 20 70 32 20 3d 20 30 3b 0a   p2<0 ) p2 = 0;.
25d7e 20 20 7d 0a 20 20 69 66 28 20 70 30 74 79 70 65    }.  if( p0type
25d7f 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b  !=SQLITE_BLOB ){
25d80 0a 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 20 26  .    while( *z &
25d81 26 20 70 31 20 29 7b 0a 20 20 20 20 20 20 53 51  & p1 ){.      SQ
25d82 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a  LITE_SKIP_UTF8(z
25d83 29 3b 0a 20 20 20 20 20 20 70 31 2d 2d 3b 0a 20  );.      p1--;. 
25d84 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 7a 32 3d     }.    for(z2=
25d85 7a 3b 20 2a 7a 32 20 26 26 20 70 32 3b 20 70 32  z; *z2 && p2; p2
25d86 2d 2d 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 54  --){.      SQLIT
25d87 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 32 29 3b  E_SKIP_UTF8(z2);
25d88 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
25d89 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
25d8a 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a  ontext, (char*)z
25d8b 2c 20 28 69 6e 74 29 28 7a 32 2d 7a 29 2c 20 53  , (int)(z2-z), S
25d8c 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
25d8d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
25d8e 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
25d8f 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61  ob(context, (cha
25d90 72 2a 29 26 7a 5b 70 31 5d 2c 20 28 69 6e 74 29  r*)&z[p1], (int)
25d91 70 32 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  p2, SQLITE_TRANS
25d92 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  IENT);.  }.}../*
25d93 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
25d94 6f 6e 20 6f 66 20 74 68 65 20 72 6f 75 6e 64 28  on of the round(
25d95 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 23 69  ) function.*/.#i
25d96 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
25d97 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
25d98 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f 75  .static void rou
25d99 6e 64 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63  ndFunc(sqlite3_c
25d9a 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
25d9b 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
25d9c 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29  e3_value **argv)
25d9d 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20  {.  int n = 0;. 
25d9e 20 64 6f 75 62 6c 65 20 72 3b 0a 20 20 63 68 61   double r;.  cha
25d9f 72 20 2a 7a 42 75 66 3b 0a 20 20 61 73 73 65 72  r *zBuf;.  asser
25da0 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72  t( argc==1 || ar
25da1 67 63 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 61  gc==2 );.  if( a
25da2 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 69 66  rgc==2 ){.    if
25da3 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73  ( SQLITE_NULL==s
25da4 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
25da5 65 28 61 72 67 76 5b 31 5d 29 20 29 20 72 65 74  e(argv[1]) ) ret
25da6 75 72 6e 3b 0a 20 20 20 20 6e 20 3d 20 73 71 6c  urn;.    n = sql
25da7 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61  ite3_value_int(a
25da8 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28  rgv[1]);.    if(
25da9 20 6e 3e 33 30 20 29 20 6e 20 3d 20 33 30 3b 0a   n>30 ) n = 30;.
25daa 20 20 20 20 69 66 28 20 6e 3c 30 20 29 20 6e 20      if( n<0 ) n 
25dab 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  = 0;.  }.  if( s
25dac 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
25dad 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49  e(argv[0])==SQLI
25dae 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e  TE_NULL ) return
25daf 3b 0a 20 20 72 20 3d 20 73 71 6c 69 74 65 33 5f  ;.  r = sqlite3_
25db0 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67  value_double(arg
25db1 76 5b 30 5d 29 3b 0a 20 20 7a 42 75 66 20 3d 20  v[0]);.  zBuf = 
25db2 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
25db3 22 25 2e 2a 66 22 2c 6e 2c 72 29 3b 0a 20 20 69  "%.*f",n,r);.  i
25db4 66 28 20 7a 42 75 66 3d 3d 30 20 29 7b 0a 20 20  f( zBuf==0 ){.  
25db5 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
25db6 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e  _error_nomem(con
25db7 74 65 78 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  text);.  }else{.
25db8 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28      sqlite3AtoF(
25db9 7a 42 75 66 2c 20 26 72 29 3b 0a 20 20 20 20 73  zBuf, &r);.    s
25dba 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 42 75 66  qlite3_free(zBuf
25dbb 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
25dbc 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e  esult_double(con
25dbd 74 65 78 74 2c 20 72 29 3b 0a 20 20 7d 0a 7d 0a  text, r);.  }.}.
25dbe 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6c  #endif../*.** Al
25dbf 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 62 79 74  locate nByte byt
25dc0 65 73 20 6f 66 20 73 70 61 63 65 20 75 73 69 6e  es of space usin
25dc1 67 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  g sqlite3_malloc
25dc2 28 29 2e 20 49 66 20 74 68 65 0a 2a 2a 20 61 6c  (). If the.** al
25dc3 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20  location fails, 
25dc4 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 72 65 73  call sqlite3_res
25dc5 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28  ult_error_nomem(
25dc6 29 20 74 6f 20 6e 6f 74 69 66 79 0a 2a 2a 20 74  ) to notify.** t
25dc7 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
25dc8 6c 65 20 74 68 61 74 20 6d 61 6c 6c 6f 63 28 29  le that malloc()
25dc9 20 68 61 73 20 66 61 69 6c 65 64 20 61 6e 64 20   has failed and 
25dca 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20  return NULL..** 
25dcb 49 66 20 6e 42 79 74 65 20 69 73 20 6c 61 72 67  If nByte is larg
25dcc 65 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78 69  er than the maxi
25dcd 6d 75 6d 20 73 74 72 69 6e 67 20 6f 72 20 62 6c  mum string or bl
25dce 6f 62 20 6c 65 6e 67 74 68 2c 20 74 68 65 6e 0a  ob length, then.
25dcf 2a 2a 20 72 61 69 73 65 20 61 6e 20 53 51 4c 49  ** raise an SQLI
25dd0 54 45 5f 54 4f 4f 42 49 47 20 65 78 63 65 70 74  TE_TOOBIG except
25dd1 69 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e 20 4e  ion and return N
25dd2 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ULL..*/.static v
25dd3 6f 69 64 20 2a 63 6f 6e 74 65 78 74 4d 61 6c 6c  oid *contextMall
25dd4 6f 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  oc(sqlite3_conte
25dd5 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 36 34  xt *context, i64
25dd6 20 6e 42 79 74 65 29 7b 0a 20 20 63 68 61 72 20   nByte){.  char 
25dd7 2a 7a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  *z;.  sqlite3 *d
25dd8 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
25dd9 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
25dda 6e 74 65 78 74 29 3b 0a 20 20 61 73 73 65 72 74  ntext);.  assert
25ddb 28 20 6e 42 79 74 65 3e 30 20 29 3b 0a 20 20 74  ( nByte>0 );.  t
25ddc 65 73 74 63 61 73 65 28 20 6e 42 79 74 65 3d 3d  estcase( nByte==
25ddd 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
25dde 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20  E_LIMIT_LENGTH] 
25ddf 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e  );.  testcase( n
25de0 42 79 74 65 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74  Byte==db->aLimit
25de1 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  [SQLITE_LIMIT_LE
25de2 4e 47 54 48 5d 2b 31 20 29 3b 0a 20 20 69 66 28  NGTH]+1 );.  if(
25de3 20 6e 42 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69   nByte>db->aLimi
25de4 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
25de5 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 73 71  ENGTH] ){.    sq
25de6 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
25de7 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78  or_toobig(contex
25de8 74 29 3b 0a 20 20 20 20 7a 20 3d 20 30 3b 0a 20  t);.    z = 0;. 
25de9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20   }else{.    z = 
25dea 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 28 69  sqlite3Malloc((i
25deb 6e 74 29 6e 42 79 74 65 29 3b 0a 20 20 20 20 69  nt)nByte);.    i
25dec 66 28 20 21 7a 20 29 7b 0a 20 20 20 20 20 20 73  f( !z ){.      s
25ded 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
25dee 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78  ror_nomem(contex
25def 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  t);.    }.  }.  
25df0 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a  return z;.}../*.
25df1 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
25df2 6e 20 6f 66 20 74 68 65 20 75 70 70 65 72 28 29  n of the upper()
25df3 20 61 6e 64 20 6c 6f 77 65 72 28 29 20 53 51 4c   and lower() SQL
25df4 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73   functions..*/.s
25df5 74 61 74 69 63 20 76 6f 69 64 20 75 70 70 65 72  tatic void upper
25df6 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e  Func(sqlite3_con
25df7 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69  text *context, i
25df8 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
25df9 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a  _value **argv){.
25dfa 20 20 63 68 61 72 20 2a 7a 31 3b 0a 20 20 63 6f    char *z1;.  co
25dfb 6e 73 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20  nst char *z2;.  
25dfc 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 55 4e 55 53  int i, n;.  UNUS
25dfd 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67  ED_PARAMETER(arg
25dfe 63 29 3b 0a 20 20 7a 32 20 3d 20 28 63 68 61 72  c);.  z2 = (char
25dff 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
25e00 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  text(argv[0]);. 
25e01 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   n = sqlite3_val
25e02 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
25e03 29 3b 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74  );.  /* Verify t
25e04 68 61 74 20 74 68 65 20 63 61 6c 6c 20 74 6f 20  hat the call to 
25e05 5f 62 79 74 65 73 28 29 20 64 6f 65 73 20 6e 6f  _bytes() does no
25e06 74 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65  t invalidate the
25e07 20 5f 74 65 78 74 28 29 20 70 6f 69 6e 74 65 72   _text() pointer
25e08 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7a 32   */.  assert( z2
25e09 3d 3d 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33  ==(char*)sqlite3
25e0a 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
25e0b 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a 32  [0]) );.  if( z2
25e0c 20 29 7b 0a 20 20 20 20 7a 31 20 3d 20 63 6f 6e   ){.    z1 = con
25e0d 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65  textMalloc(conte
25e0e 78 74 2c 20 28 28 69 36 34 29 6e 29 2b 31 29 3b  xt, ((i64)n)+1);
25e0f 0a 20 20 20 20 69 66 28 20 7a 31 20 29 7b 0a 20  .    if( z1 ){. 
25e10 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 31 2c 20       memcpy(z1, 
25e11 7a 32 2c 20 6e 2b 31 29 3b 0a 20 20 20 20 20 20  z2, n+1);.      
25e12 66 6f 72 28 69 3d 30 3b 20 7a 31 5b 69 5d 3b 20  for(i=0; z1[i]; 
25e13 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a 31  i++){.        z1
25e14 5b 69 5d 20 3d 20 28 63 68 61 72 29 73 71 6c 69  [i] = (char)sqli
25e15 74 65 33 54 6f 75 70 70 65 72 28 7a 31 5b 69 5d  te3Toupper(z1[i]
25e16 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
25e17 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
25e18 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 31  text(context, z1
25e19 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 5f 66 72  , -1, sqlite3_fr
25e1a 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  ee);.    }.  }.}
25e1b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 77  .static void low
25e1c 65 72 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63  erFunc(sqlite3_c
25e1d 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
25e1e 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
25e1f 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29  e3_value **argv)
25e20 7b 0a 20 20 75 38 20 2a 7a 31 3b 0a 20 20 63 6f  {.  u8 *z1;.  co
25e21 6e 73 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20  nst char *z2;.  
25e22 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 55 4e 55 53  int i, n;.  UNUS
25e23 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67  ED_PARAMETER(arg
25e24 63 29 3b 0a 20 20 7a 32 20 3d 20 28 63 68 61 72  c);.  z2 = (char
25e25 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
25e26 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  text(argv[0]);. 
25e27 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   n = sqlite3_val
25e28 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
25e29 29 3b 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74  );.  /* Verify t
25e2a 68 61 74 20 74 68 65 20 63 61 6c 6c 20 74 6f 20  hat the call to 
25e2b 5f 62 79 74 65 73 28 29 20 64 6f 65 73 20 6e 6f  _bytes() does no
25e2c 74 20 69 6e 76 61 6c 69 64 61 74 65 20 74 68 65  t invalidate the
25e2d 20 5f 74 65 78 74 28 29 20 70 6f 69 6e 74 65 72   _text() pointer
25e2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7a 32   */.  assert( z2
25e2f 3d 3d 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33  ==(char*)sqlite3
25e30 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
25e31 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a 32  [0]) );.  if( z2
25e32 20 29 7b 0a 20 20 20 20 7a 31 20 3d 20 63 6f 6e   ){.    z1 = con
25e33 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65  textMalloc(conte
25e34 78 74 2c 20 28 28 69 36 34 29 6e 29 2b 31 29 3b  xt, ((i64)n)+1);
25e35 0a 20 20 20 20 69 66 28 20 7a 31 20 29 7b 0a 20  .    if( z1 ){. 
25e36 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 31 2c 20       memcpy(z1, 
25e37 7a 32 2c 20 6e 2b 31 29 3b 0a 20 20 20 20 20 20  z2, n+1);.      
25e38 66 6f 72 28 69 3d 30 3b 20 7a 31 5b 69 5d 3b 20  for(i=0; z1[i]; 
25e39 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 7a 31  i++){.        z1
25e3a 5b 69 5d 20 3d 20 73 71 6c 69 74 65 33 54 6f 6c  [i] = sqlite3Tol
25e3b 6f 77 65 72 28 7a 31 5b 69 5d 29 3b 0a 20 20 20  ower(z1[i]);.   
25e3c 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
25e3d 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
25e3e 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 20 2a 29  ontext, (char *)
25e3f 7a 31 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 5f  z1, -1, sqlite3_
25e40 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  free);.    }.  }
25e41 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
25e42 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
25e43 49 46 4e 55 4c 4c 28 29 2c 20 4e 56 4c 28 29 2c  IFNULL(), NVL(),
25e44 20 61 6e 64 20 43 4f 41 4c 45 53 43 45 28 29 20   and COALESCE() 
25e45 66 75 6e 63 74 69 6f 6e 73 2e 20 20 0a 2a 2a 20  functions.  .** 
25e46 41 6c 6c 20 74 68 72 65 65 20 64 6f 20 74 68 65  All three do the
25e47 20 73 61 6d 65 20 74 68 69 6e 67 2e 20 20 54 68   same thing.  Th
25e48 65 79 20 72 65 74 75 72 6e 20 74 68 65 20 66 69  ey return the fi
25e49 72 73 74 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20  rst non-NULL.** 
25e4a 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61  argument..*/.sta
25e4b 74 69 63 20 76 6f 69 64 20 69 66 6e 75 6c 6c 46  tic void ifnullF
25e4c 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
25e4d 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
25e4e 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
25e4f 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
25e50 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  rgv.){.  int i;.
25e51 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 61 72 67    for(i=0; i<arg
25e52 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  c; i++){.    if(
25e53 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 21 3d 73 71   SQLITE_NULL!=sq
25e54 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
25e55 28 61 72 67 76 5b 69 5d 29 20 29 7b 0a 20 20 20  (argv[i]) ){.   
25e56 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
25e57 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74 2c  t_value(context,
25e58 20 61 72 67 76 5b 69 5d 29 3b 0a 20 20 20 20 20   argv[i]);.     
25e59 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
25e5a 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  }.}../*.** Imple
25e5b 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 72 61 6e  mentation of ran
25e5c 64 6f 6d 28 29 2e 20 20 52 65 74 75 72 6e 20 61  dom().  Return a
25e5d 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67 65 72 2e   random integer.
25e5e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69    .*/.static voi
25e5f 64 20 72 61 6e 64 6f 6d 46 75 6e 63 28 0a 20 20  d randomFunc(.  
25e60 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
25e61 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
25e62 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74  NotUsed,.  sqlit
25e63 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73  e3_value **NotUs
25e64 65 64 32 0a 29 7b 0a 20 20 73 71 6c 69 74 65 5f  ed2.){.  sqlite_
25e65 69 6e 74 36 34 20 72 3b 0a 20 20 55 4e 55 53 45  int64 r;.  UNUSE
25e66 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74  D_PARAMETER2(Not
25e67 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b  Used, NotUsed2);
25e68 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f  .  sqlite3_rando
25e69 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 72 29 2c  mness(sizeof(r),
25e6a 20 26 72 29 3b 0a 20 20 69 66 28 20 72 3c 30 20   &r);.  if( r<0 
25e6b 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 6e 65 65  ){.    /* We nee
25e6c 64 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 72  d to prevent a r
25e6d 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 6f 66 20  andom number of 
25e6e 30 78 38 30 30 30 30 30 30 30 30 30 30 30 30 30  0x80000000000000
25e6f 30 30 20 0a 20 20 20 20 2a 2a 20 28 6f 72 20 2d  00 .    ** (or -
25e70 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35  9223372036854775
25e71 38 30 38 29 20 73 69 6e 63 65 20 77 68 65 6e 20  808) since when 
25e72 79 6f 75 20 64 6f 20 61 62 73 28 29 20 6f 66 20  you do abs() of 
25e73 74 68 61 74 0a 20 20 20 20 2a 2a 20 6e 75 6d 62  that.    ** numb
25e74 65 72 20 6f 66 20 79 6f 75 20 67 65 74 20 74 68  er of you get th
25e75 65 20 73 61 6d 65 20 76 61 6c 75 65 20 62 61 63  e same value bac
25e76 6b 20 61 67 61 69 6e 2e 20 20 54 6f 20 64 6f 20  k again.  To do 
25e77 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 6e 20 61  this.    ** in a
25e78 20 77 61 79 20 74 68 61 74 20 69 73 20 74 65 73   way that is tes
25e79 74 61 62 6c 65 2c 20 6d 61 73 6b 20 74 68 65 20  table, mask the 
25e7a 73 69 67 6e 20 62 69 74 20 6f 66 66 20 6f 66 20  sign bit off of 
25e7b 6e 65 67 61 74 69 76 65 0a 20 20 20 20 2a 2a 20  negative.    ** 
25e7c 76 61 6c 75 65 73 2c 20 72 65 73 75 6c 74 69 6e  values, resultin
25e7d 67 20 69 6e 20 61 20 70 6f 73 69 74 69 76 65 20  g in a positive 
25e7e 76 61 6c 75 65 2e 20 20 54 68 65 6e 20 74 61 6b  value.  Then tak
25e7f 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 32 73  e the .    ** 2s
25e80 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 6f 66 20 74   complement of t
25e81 68 61 74 20 70 6f 73 69 74 69 76 65 20 76 61 6c  hat positive val
25e82 75 65 2e 20 20 54 68 65 20 65 6e 64 20 72 65 73  ue.  The end res
25e83 75 6c 74 20 63 61 6e 0a 20 20 20 20 2a 2a 20 74  ult can.    ** t
25e84 68 65 72 65 66 6f 72 65 20 62 65 20 6e 6f 20 6c  herefore be no l
25e85 65 73 73 20 74 68 61 6e 20 2d 39 32 32 33 33 37  ess than -922337
25e86 32 30 33 36 38 35 34 37 37 35 38 30 37 2e 0a 20  2036854775807.. 
25e87 20 20 20 2a 2f 0a 20 20 20 20 72 20 3d 20 2d 28     */.    r = -(
25e88 72 20 5e 20 28 28 28 73 71 6c 69 74 65 33 5f 69  r ^ (((sqlite3_i
25e89 6e 74 36 34 29 31 29 3c 3c 36 33 29 29 3b 0a 20  nt64)1)<<63));. 
25e8a 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73   }.  sqlite3_res
25e8b 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78  ult_int64(contex
25e8c 74 2c 20 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  t, r);.}../*.** 
25e8d 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
25e8e 66 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 4e 29 2e  f randomblob(N).
25e8f 20 20 52 65 74 75 72 6e 20 61 20 72 61 6e 64 6f    Return a rando
25e90 6d 20 62 6c 6f 62 0a 2a 2a 20 74 68 61 74 20 69  m blob.** that i
25e91 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e 67 2e 0a  s N bytes long..
25e92 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
25e93 61 6e 64 6f 6d 42 6c 6f 62 28 0a 20 20 73 71 6c  andomBlob(.  sql
25e94 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
25e95 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
25e96 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
25e97 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69  ue **argv.){.  i
25e98 6e 74 20 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64  nt n;.  unsigned
25e99 20 63 68 61 72 20 2a 70 3b 0a 20 20 61 73 73 65   char *p;.  asse
25e9a 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20  rt( argc==1 );. 
25e9b 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
25e9c 52 28 61 72 67 63 29 3b 0a 20 20 6e 20 3d 20 73  R(argc);.  n = s
25e9d 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
25e9e 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28  (argv[0]);.  if(
25e9f 20 6e 3c 31 20 29 7b 0a 20 20 20 20 6e 20 3d 20   n<1 ){.    n = 
25ea0 31 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 63 6f 6e  1;.  }.  p = con
25ea1 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65  textMalloc(conte
25ea2 78 74 2c 20 6e 29 3b 0a 20 20 69 66 28 20 70 20  xt, n);.  if( p 
25ea3 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
25ea4 61 6e 64 6f 6d 6e 65 73 73 28 6e 2c 20 70 29 3b  andomness(n, p);
25ea5 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
25ea6 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 78 74  ult_blob(context
25ea7 2c 20 28 63 68 61 72 2a 29 70 2c 20 6e 2c 20 73  , (char*)p, n, s
25ea8 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20  qlite3_free);.  
25ea9 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  }.}../*.** Imple
25eaa 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
25eab 20 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77   last_insert_row
25eac 69 64 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f  id() SQL functio
25ead 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e 0a 2a  n.  The return.*
25eae 2a 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 73  * value is the s
25eaf 61 6d 65 20 61 73 20 74 68 65 20 73 71 6c 69 74  ame as the sqlit
25eb0 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
25eb1 6f 77 69 64 28 29 20 41 50 49 20 66 75 6e 63 74  owid() API funct
25eb2 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
25eb3 6f 69 64 20 6c 61 73 74 5f 69 6e 73 65 72 74 5f  oid last_insert_
25eb4 72 6f 77 69 64 28 0a 20 20 73 71 6c 69 74 65 33  rowid(.  sqlite3
25eb5 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
25eb6 74 2c 20 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  t, .  int NotUse
25eb7 64 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  d, .  sqlite3_va
25eb8 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29  lue **NotUsed2.)
25eb9 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
25eba 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  = sqlite3_contex
25ebb 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
25ebc 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  ext);.  UNUSED_P
25ebd 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
25ebe 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20  d, NotUsed2);.  
25ebf 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
25ec0 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 73 71  nt64(context, sq
25ec1 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
25ec2 74 5f 72 6f 77 69 64 28 64 62 29 29 3b 0a 7d 0a  t_rowid(db));.}.
25ec3 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
25ec4 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 68 61  ation of the cha
25ec5 6e 67 65 73 28 29 20 53 51 4c 20 66 75 6e 63 74  nges() SQL funct
25ec6 69 6f 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e  ion.  The return
25ec7 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a 2a 2a   value is the.**
25ec8 20 73 61 6d 65 20 61 73 20 74 68 65 20 73 71 6c   same as the sql
25ec9 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 41  ite3_changes() A
25eca 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  PI function..*/.
25ecb 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 61 6e  static void chan
25ecc 67 65 73 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  ges(.  sqlite3_c
25ecd 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
25ece 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a  .  int NotUsed,.
25ecf 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
25ed0 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20  **NotUsed2.){.  
25ed1 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
25ed2 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
25ed3 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
25ed4 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
25ed5 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e  ETER2(NotUsed, N
25ed6 6f 74 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69  otUsed2);.  sqli
25ed7 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63  te3_result_int(c
25ed8 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f  ontext, sqlite3_
25ed9 63 68 61 6e 67 65 73 28 64 62 29 29 3b 0a 7d 0a  changes(db));.}.
25eda 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
25edb 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 74 6f 74  ation of the tot
25edc 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 53 51 4c  al_changes() SQL
25edd 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20   function.  The 
25ede 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a  return value is.
25edf 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  ** the same as t
25ee0 68 65 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c  he sqlite3_total
25ee1 5f 63 68 61 6e 67 65 73 28 29 20 41 50 49 20 66  _changes() API f
25ee2 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  unction..*/.stat
25ee3 69 63 20 76 6f 69 64 20 74 6f 74 61 6c 5f 63 68  ic void total_ch
25ee4 61 6e 67 65 73 28 0a 20 20 73 71 6c 69 74 65 33  anges(.  sqlite3
25ee5 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
25ee6 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  t,.  int NotUsed
25ee7 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
25ee8 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a  e **NotUsed2.){.
25ee9 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
25eea 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
25eeb 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78  db_handle(contex
25eec 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  t);.  UNUSED_PAR
25eed 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c  AMETER2(NotUsed,
25eee 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 73 71   NotUsed2);.  sq
25eef 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
25ef0 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65  (context, sqlite
25ef1 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28  3_total_changes(
25ef2 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  db));.}../*.** A
25ef3 20 73 74 72 75 63 74 75 72 65 20 64 65 66 69 6e   structure defin
25ef4 69 6e 67 20 68 6f 77 20 74 6f 20 64 6f 20 47 4c  ing how to do GL
25ef5 4f 42 2d 73 74 79 6c 65 20 63 6f 6d 70 61 72 69  OB-style compari
25ef6 73 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  sons..*/.struct 
25ef7 63 6f 6d 70 61 72 65 49 6e 66 6f 20 7b 0a 20 20  compareInfo {.  
25ef8 75 38 20 6d 61 74 63 68 41 6c 6c 3b 0a 20 20 75  u8 matchAll;.  u
25ef9 38 20 6d 61 74 63 68 4f 6e 65 3b 0a 20 20 75 38  8 matchOne;.  u8
25efa 20 6d 61 74 63 68 53 65 74 3b 0a 20 20 75 38 20   matchSet;.  u8 
25efb 6e 6f 43 61 73 65 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a  noCase;.};../*.*
25efc 2a 20 46 6f 72 20 4c 49 4b 45 20 61 6e 64 20 47  * For LIKE and G
25efd 4c 4f 42 20 6d 61 74 63 68 69 6e 67 20 6f 6e 20  LOB matching on 
25efe 45 42 43 44 49 43 20 6d 61 63 68 69 6e 65 73 2c  EBCDIC machines,
25eff 20 61 73 73 75 6d 65 20 74 68 61 74 20 65 76 65   assume that eve
25f00 72 79 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 20  ry.** character 
25f01 69 73 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 62  is exactly one b
25f02 79 74 65 20 69 6e 20 73 69 7a 65 2e 20 20 41 6c  yte in size.  Al
25f03 73 6f 2c 20 61 6c 6c 20 63 68 61 72 61 63 74 65  so, all characte
25f04 72 73 20 61 72 65 0a 2a 2a 20 61 62 6c 65 20 74  rs are.** able t
25f05 6f 20 70 61 72 74 69 63 69 70 61 74 65 20 69 6e  o participate in
25f06 20 75 70 70 65 72 2d 63 61 73 65 2d 74 6f 2d 6c   upper-case-to-l
25f07 6f 77 65 72 2d 63 61 73 65 20 6d 61 70 70 69 6e  ower-case mappin
25f08 67 73 20 69 6e 20 45 42 43 44 49 43 0a 2a 2a 20  gs in EBCDIC.** 
25f09 77 68 65 72 65 61 73 20 6f 6e 6c 79 20 63 68 61  whereas only cha
25f0a 72 61 63 74 65 72 73 20 6c 65 73 73 20 74 68 61  racters less tha
25f0b 6e 20 30 78 38 30 20 64 6f 20 69 6e 20 41 53 43  n 0x80 do in ASC
25f0c 49 49 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  II..*/.#if defin
25f0d 65 64 28 53 51 4c 49 54 45 5f 45 42 43 44 49 43  ed(SQLITE_EBCDIC
25f0e 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
25f0f 65 33 55 74 66 38 52 65 61 64 28 41 2c 43 29 20  e3Utf8Read(A,C) 
25f10 20 20 20 28 2a 28 41 2b 2b 29 29 0a 23 20 64 65     (*(A++)).# de
25f11 66 69 6e 65 20 47 6c 6f 67 55 70 70 65 72 54 6f  fine GlogUpperTo
25f12 4c 6f 77 65 72 28 41 29 20 20 20 20 20 41 20 3d  Lower(A)     A =
25f13 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c   sqlite3UpperToL
25f14 6f 77 65 72 5b 41 5d 0a 23 65 6c 73 65 0a 23 20  ower[A].#else.# 
25f15 64 65 66 69 6e 65 20 47 6c 6f 67 55 70 70 65 72  define GlogUpper
25f16 54 6f 4c 6f 77 65 72 28 41 29 20 20 20 20 20 69  ToLower(A)     i
25f17 66 28 20 41 3c 30 78 38 30 20 29 7b 20 41 20 3d  f( A<0x80 ){ A =
25f18 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c   sqlite3UpperToL
25f19 6f 77 65 72 5b 41 5d 3b 20 7d 0a 23 65 6e 64 69  ower[A]; }.#endi
25f1a 66 0a 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  f..static const 
25f1b 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e  struct compareIn
25f1c 66 6f 20 67 6c 6f 62 49 6e 66 6f 20 3d 20 7b 20  fo globInfo = { 
25f1d 27 2a 27 2c 20 27 3f 27 2c 20 27 5b 27 2c 20 30  '*', '?', '[', 0
25f1e 20 7d 3b 0a 2f 2a 20 54 68 65 20 63 6f 72 72 65   };./* The corre
25f1f 63 74 20 53 51 4c 2d 39 32 20 62 65 68 61 76 69  ct SQL-92 behavi
25f20 6f 72 20 69 73 20 66 6f 72 20 74 68 65 20 4c 49  or is for the LI
25f21 4b 45 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 69  KE operator to i
25f22 67 6e 6f 72 65 0a 2a 2a 20 63 61 73 65 2e 20 20  gnore.** case.  
25f23 54 68 75 73 20 20 27 61 27 20 4c 49 4b 45 20 27  Thus  'a' LIKE '
25f24 41 27 20 77 6f 75 6c 64 20 62 65 20 74 72 75 65  A' would be true
25f25 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  . */.static cons
25f26 74 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65  t struct compare
25f27 49 6e 66 6f 20 6c 69 6b 65 49 6e 66 6f 4e 6f 72  Info likeInfoNor
25f28 6d 20 3d 20 7b 20 27 25 27 2c 20 27 5f 27 2c 20  m = { '%', '_', 
25f29 20 20 30 2c 20 31 20 7d 3b 0a 2f 2a 20 49 66 20    0, 1 };./* If 
25f2a 53 51 4c 49 54 45 5f 43 41 53 45 5f 53 45 4e 53  SQLITE_CASE_SENS
25f2b 49 54 49 56 45 5f 4c 49 4b 45 20 69 73 20 64 65  ITIVE_LIKE is de
25f2c 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20  fined, then the 
25f2d 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 0a 2a 2a  LIKE operator.**
25f2e 20 69 73 20 63 61 73 65 20 73 65 6e 73 69 74 69   is case sensiti
25f2f 76 65 20 63 61 75 73 69 6e 67 20 27 61 27 20 4c  ve causing 'a' L
25f30 49 4b 45 20 27 41 27 20 74 6f 20 62 65 20 66 61  IKE 'A' to be fa
25f31 6c 73 65 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f  lse */.static co
25f32 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70 61  nst struct compa
25f33 72 65 49 6e 66 6f 20 6c 69 6b 65 49 6e 66 6f 41  reInfo likeInfoA
25f34 6c 74 20 3d 20 7b 20 27 25 27 2c 20 27 5f 27 2c  lt = { '%', '_',
25f35 20 20 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 0a 2a     0, 0 };../*.*
25f36 2a 20 43 6f 6d 70 61 72 65 20 74 77 6f 20 55 54  * Compare two UT
25f37 46 2d 38 20 73 74 72 69 6e 67 73 20 66 6f 72 20  F-8 strings for 
25f38 65 71 75 61 6c 69 74 79 20 77 68 65 72 65 20 74  equality where t
25f39 68 65 20 66 69 72 73 74 20 73 74 72 69 6e 67 20  he first string 
25f3a 63 61 6e 0a 2a 2a 20 70 6f 74 65 6e 74 69 61 6c  can.** potential
25f3b 6c 79 20 62 65 20 61 20 22 67 6c 6f 62 22 20 65  ly be a "glob" e
25f3c 78 70 72 65 73 73 69 6f 6e 2e 20 20 52 65 74 75  xpression.  Retu
25f3d 72 6e 20 74 72 75 65 20 28 31 29 20 69 66 20 74  rn true (1) if t
25f3e 68 65 79 0a 2a 2a 20 61 72 65 20 74 68 65 20 73  hey.** are the s
25f3f 61 6d 65 20 61 6e 64 20 66 61 6c 73 65 20 28 30  ame and false (0
25f40 29 20 69 66 20 74 68 65 79 20 61 72 65 20 64 69  ) if they are di
25f41 66 66 65 72 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 47  fferent..**.** G
25f42 6c 6f 62 62 69 6e 67 20 72 75 6c 65 73 3a 0a 2a  lobbing rules:.*
25f43 2a 0a 2a 2a 20 20 20 20 20 20 27 2a 27 20 20 20  *.**      '*'   
25f44 20 20 20 20 4d 61 74 63 68 65 73 20 61 6e 79 20      Matches any 
25f45 73 65 71 75 65 6e 63 65 20 6f 66 20 7a 65 72 6f  sequence of zero
25f46 20 6f 72 20 6d 6f 72 65 20 63 68 61 72 61 63 74   or more charact
25f47 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ers..**.**      
25f48 27 3f 27 20 20 20 20 20 20 20 4d 61 74 63 68 65  '?'       Matche
25f49 73 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 63 68  s exactly one ch
25f4a 61 72 61 63 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 20  aracter..**.**  
25f4b 20 20 20 5b 2e 2e 2e 5d 20 20 20 20 20 20 4d 61     [...]      Ma
25f4c 74 63 68 65 73 20 6f 6e 65 20 63 68 61 72 61 63  tches one charac
25f4d 74 65 72 20 66 72 6f 6d 20 74 68 65 20 65 6e 63  ter from the enc
25f4e 6c 6f 73 65 64 20 6c 69 73 74 20 6f 66 0a 2a 2a  losed list of.**
25f4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25f50 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a  characters..**.*
25f51 2a 20 20 20 20 20 5b 5e 2e 2e 2e 5d 20 20 20 20  *     [^...]    
25f52 20 4d 61 74 63 68 65 73 20 6f 6e 65 20 63 68 61   Matches one cha
25f53 72 61 63 74 65 72 20 6e 6f 74 20 69 6e 20 74 68  racter not in th
25f54 65 20 65 6e 63 6c 6f 73 65 64 20 6c 69 73 74 2e  e enclosed list.
25f55 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 68 65 20  .**.** With the 
25f56 5b 2e 2e 2e 5d 20 61 6e 64 20 5b 5e 2e 2e 2e 5d  [...] and [^...]
25f57 20 6d 61 74 63 68 69 6e 67 2c 20 61 20 27 5d 27   matching, a ']'
25f58 20 63 68 61 72 61 63 74 65 72 20 63 61 6e 20 62   character can b
25f59 65 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 69 6e  e included.** in
25f5a 20 74 68 65 20 6c 69 73 74 20 62 79 20 6d 61 6b   the list by mak
25f5b 69 6e 67 20 69 74 20 74 68 65 20 66 69 72 73 74  ing it the first
25f5c 20 63 68 61 72 61 63 74 65 72 20 61 66 74 65 72   character after
25f5d 20 27 5b 27 20 6f 72 20 27 5e 27 2e 20 20 41 0a   '[' or '^'.  A.
25f5e 2a 2a 20 72 61 6e 67 65 20 6f 66 20 63 68 61 72  ** range of char
25f5f 61 63 74 65 72 73 20 63 61 6e 20 62 65 20 73 70  acters can be sp
25f60 65 63 69 66 69 65 64 20 75 73 69 6e 67 20 27 2d  ecified using '-
25f61 27 2e 20 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20  '.  Example:.** 
25f62 22 5b 61 2d 7a 5d 22 20 6d 61 74 63 68 65 73 20  "[a-z]" matches 
25f63 61 6e 79 20 73 69 6e 67 6c 65 20 6c 6f 77 65 72  any single lower
25f64 2d 63 61 73 65 20 6c 65 74 74 65 72 2e 20 20 54  -case letter.  T
25f65 6f 20 6d 61 74 63 68 20 61 20 27 2d 27 2c 20 6d  o match a '-', m
25f66 61 6b 65 0a 2a 2a 20 69 74 20 74 68 65 20 6c 61  ake.** it the la
25f67 73 74 20 63 68 61 72 61 63 74 65 72 20 69 6e 20  st character in 
25f68 74 68 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  the list..**.** 
25f69 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
25f6a 75 73 75 61 6c 6c 79 20 71 75 69 63 6b 2c 20 62  usually quick, b
25f6b 75 74 20 63 61 6e 20 62 65 20 4e 2a 2a 32 20 69  ut can be N**2 i
25f6c 6e 20 74 68 65 20 77 6f 72 73 74 20 63 61 73 65  n the worst case
25f6d 2e 0a 2a 2a 0a 2a 2a 20 48 69 6e 74 73 3a 20 74  ..**.** Hints: t
25f6e 6f 20 6d 61 74 63 68 20 27 2a 27 20 6f 72 20 27  o match '*' or '
25f6f 3f 27 2c 20 70 75 74 20 74 68 65 6d 20 69 6e 20  ?', put them in 
25f70 22 5b 5d 22 2e 20 20 4c 69 6b 65 20 74 68 69 73  "[]".  Like this
25f71 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
25f72 61 62 63 5b 2a 5d 78 79 7a 20 20 20 20 20 20 20  abc[*]xyz       
25f73 20 4d 61 74 63 68 65 73 20 22 61 62 63 2a 78 79   Matches "abc*xy
25f74 7a 22 20 6f 6e 6c 79 0a 2a 2f 0a 73 74 61 74 69  z" only.*/.stati
25f75 63 20 69 6e 74 20 70 61 74 74 65 72 6e 43 6f 6d  c int patternCom
25f76 70 61 72 65 28 0a 20 20 63 6f 6e 73 74 20 75 38  pare(.  const u8
25f77 20 2a 7a 50 61 74 74 65 72 6e 2c 20 20 20 20 20   *zPattern,     
25f78 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
25f79 67 6c 6f 62 20 70 61 74 74 65 72 6e 20 2a 2f 0a  glob pattern */.
25f7a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 53 74 72    const u8 *zStr
25f7b 69 6e 67 2c 20 20 20 20 20 20 20 20 20 20 20 20  ing,            
25f7c 20 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e 67     /* The string
25f7d 20 74 6f 20 63 6f 6d 70 61 72 65 20 61 67 61 69   to compare agai
25f7e 6e 73 74 20 74 68 65 20 67 6c 6f 62 20 2a 2f 0a  nst the glob */.
25f7f 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 63    const struct c
25f80 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49 6e 66  ompareInfo *pInf
25f81 6f 2c 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f  o, /* Informatio
25f82 6e 20 61 62 6f 75 74 20 68 6f 77 20 74 6f 20 64  n about how to d
25f83 6f 20 74 68 65 20 63 6f 6d 70 61 72 65 20 2a 2f  o the compare */
25f84 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 65 73 63  .  const int esc
25f85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25f86 20 20 20 20 2f 2a 20 54 68 65 20 65 73 63 61 70      /* The escap
25f87 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 29  e character */.)
25f88 7b 0a 20 20 69 6e 74 20 63 2c 20 63 32 3b 0a 20  {.  int c, c2;. 
25f89 20 69 6e 74 20 69 6e 76 65 72 74 3b 0a 20 20 69   int invert;.  i
25f8a 6e 74 20 73 65 65 6e 3b 0a 20 20 75 38 20 6d 61  nt seen;.  u8 ma
25f8b 74 63 68 4f 6e 65 20 3d 20 70 49 6e 66 6f 2d 3e  tchOne = pInfo->
25f8c 6d 61 74 63 68 4f 6e 65 3b 0a 20 20 75 38 20 6d  matchOne;.  u8 m
25f8d 61 74 63 68 41 6c 6c 20 3d 20 70 49 6e 66 6f 2d  atchAll = pInfo-
25f8e 3e 6d 61 74 63 68 41 6c 6c 3b 0a 20 20 75 38 20  >matchAll;.  u8 
25f8f 6d 61 74 63 68 53 65 74 20 3d 20 70 49 6e 66 6f  matchSet = pInfo
25f90 2d 3e 6d 61 74 63 68 53 65 74 3b 0a 20 20 75 38  ->matchSet;.  u8
25f91 20 6e 6f 43 61 73 65 20 3d 20 70 49 6e 66 6f 2d   noCase = pInfo-
25f92 3e 6e 6f 43 61 73 65 3b 20 0a 20 20 69 6e 74 20  >noCase; .  int 
25f93 70 72 65 76 45 73 63 61 70 65 20 3d 20 30 3b 20  prevEscape = 0; 
25f94 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
25f95 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 61 72  he previous char
25f96 61 63 74 65 72 20 77 61 73 20 27 65 73 63 61 70  acter was 'escap
25f97 65 27 20 2a 2f 0a 0a 20 20 77 68 69 6c 65 28 20  e' */..  while( 
25f98 28 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38  (c = sqlite3Utf8
25f99 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 26 7a  Read(zPattern,&z
25f9a 50 61 74 74 65 72 6e 29 29 21 3d 30 20 29 7b 0a  Pattern))!=0 ){.
25f9b 20 20 20 20 69 66 28 20 21 70 72 65 76 45 73 63      if( !prevEsc
25f9c 61 70 65 20 26 26 20 63 3d 3d 6d 61 74 63 68 41  ape && c==matchA
25f9d 6c 6c 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c  ll ){.      whil
25f9e 65 28 20 28 63 3d 73 71 6c 69 74 65 33 55 74 66  e( (c=sqlite3Utf
25f9f 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 26  8Read(zPattern,&
25fa0 7a 50 61 74 74 65 72 6e 29 29 20 3d 3d 20 6d 61  zPattern)) == ma
25fa1 74 63 68 41 6c 6c 0a 20 20 20 20 20 20 20 20 20  tchAll.         
25fa2 20 20 20 20 20 20 7c 7c 20 63 20 3d 3d 20 6d 61        || c == ma
25fa3 74 63 68 4f 6e 65 20 29 7b 0a 20 20 20 20 20 20  tchOne ){.      
25fa4 20 20 69 66 28 20 63 3d 3d 6d 61 74 63 68 4f 6e    if( c==matchOn
25fa5 65 20 26 26 20 73 71 6c 69 74 65 33 55 74 66 38  e && sqlite3Utf8
25fa6 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a  Read(zString, &z
25fa7 53 74 72 69 6e 67 29 3d 3d 30 20 29 7b 0a 20 20  String)==0 ){.  
25fa8 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
25fa9 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
25faa 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3d    }.      if( c=
25fab 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  =0 ){.        re
25fac 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 65  turn 1;.      }e
25fad 6c 73 65 20 69 66 28 20 63 3d 3d 65 73 63 20 29  lse if( c==esc )
25fae 7b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 73 71  {.        c = sq
25faf 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50  lite3Utf8Read(zP
25fb0 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 74 65 72  attern, &zPatter
25fb1 6e 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  n);.        if( 
25fb2 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  c==0 ){.        
25fb3 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
25fb4 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
25fb5 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 68 53 65  e if( c==matchSe
25fb6 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  t ){.        ass
25fb7 65 72 74 28 20 65 73 63 3d 3d 30 20 29 3b 20 20  ert( esc==0 );  
25fb8 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69         /* This i
25fb9 73 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49 4b 45  s GLOB, not LIKE
25fba 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65   */.        asse
25fbb 72 74 28 20 6d 61 74 63 68 53 65 74 3c 30 78 38  rt( matchSet<0x8
25fbc 30 20 29 3b 20 20 2f 2a 20 27 5b 27 20 69 73 20  0 );  /* '[' is 
25fbd 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 68  a single-byte ch
25fbe 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 20 20 20  aracter */.     
25fbf 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 74 72 69     while( *zStri
25fc0 6e 67 20 26 26 20 70 61 74 74 65 72 6e 43 6f 6d  ng && patternCom
25fc1 70 61 72 65 28 26 7a 50 61 74 74 65 72 6e 5b 2d  pare(&zPattern[-
25fc2 31 5d 2c 7a 53 74 72 69 6e 67 2c 70 49 6e 66 6f  1],zString,pInfo
25fc3 2c 65 73 63 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ,esc)==0 ){.    
25fc4 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49        SQLITE_SKI
25fc5 50 5f 55 54 46 38 28 7a 53 74 72 69 6e 67 29 3b  P_UTF8(zString);
25fc6 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
25fc7 20 20 20 72 65 74 75 72 6e 20 2a 7a 53 74 72 69     return *zStri
25fc8 6e 67 21 3d 30 3b 0a 20 20 20 20 20 20 7d 0a 20  ng!=0;.      }. 
25fc9 20 20 20 20 20 77 68 69 6c 65 28 20 28 63 32 20       while( (c2 
25fca 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
25fcb 64 28 7a 53 74 72 69 6e 67 2c 26 7a 53 74 72 69  d(zString,&zStri
25fcc 6e 67 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ng))!=0 ){.     
25fcd 20 20 20 69 66 28 20 6e 6f 43 61 73 65 20 29 7b     if( noCase ){
25fce 0a 20 20 20 20 20 20 20 20 20 20 47 6c 6f 67 55  .          GlogU
25fcf 70 70 65 72 54 6f 4c 6f 77 65 72 28 63 32 29 3b  pperToLower(c2);
25fd0 0a 20 20 20 20 20 20 20 20 20 20 47 6c 6f 67 55  .          GlogU
25fd1 70 70 65 72 54 6f 4c 6f 77 65 72 28 63 29 3b 0a  pperToLower(c);.
25fd2 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28            while(
25fd3 20 63 32 20 21 3d 20 30 20 26 26 20 63 32 20 21   c2 != 0 && c2 !
25fd4 3d 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20  = c ){.         
25fd5 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55     c2 = sqlite3U
25fd6 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c  tf8Read(zString,
25fd7 20 26 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20   &zString);.    
25fd8 20 20 20 20 20 20 20 20 47 6c 6f 67 55 70 70 65          GlogUppe
25fd9 72 54 6f 4c 6f 77 65 72 28 63 32 29 3b 0a 20 20  rToLower(c2);.  
25fda 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
25fdb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
25fdc 20 20 20 77 68 69 6c 65 28 20 63 32 20 21 3d 20     while( c2 != 
25fdd 30 20 26 26 20 63 32 20 21 3d 20 63 20 29 7b 0a  0 && c2 != c ){.
25fde 20 20 20 20 20 20 20 20 20 20 20 20 63 32 20 3d              c2 =
25fdf 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
25fe0 28 7a 53 74 72 69 6e 67 2c 20 26 7a 53 74 72 69  (zString, &zStri
25fe1 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ng);.          }
25fe2 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
25fe3 20 20 20 69 66 28 20 63 32 3d 3d 30 20 29 20 72     if( c2==0 ) r
25fe4 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20  eturn 0;.       
25fe5 20 69 66 28 20 70 61 74 74 65 72 6e 43 6f 6d 70   if( patternComp
25fe6 61 72 65 28 7a 50 61 74 74 65 72 6e 2c 7a 53 74  are(zPattern,zSt
25fe7 72 69 6e 67 2c 70 49 6e 66 6f 2c 65 73 63 29 20  ring,pInfo,esc) 
25fe8 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
25fe9 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e    }.      return
25fea 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   0;.    }else if
25feb 28 20 21 70 72 65 76 45 73 63 61 70 65 20 26 26  ( !prevEscape &&
25fec 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20 29 7b 0a   c==matchOne ){.
25fed 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
25fee 33 55 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e  3Utf8Read(zStrin
25fef 67 2c 20 26 7a 53 74 72 69 6e 67 29 3d 3d 30 20  g, &zString)==0 
25ff0 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
25ff1 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
25ff2 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61   }else if( c==ma
25ff3 74 63 68 53 65 74 20 29 7b 0a 20 20 20 20 20 20  tchSet ){.      
25ff4 69 6e 74 20 70 72 69 6f 72 5f 63 20 3d 20 30 3b  int prior_c = 0;
25ff5 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65  .      assert( e
25ff6 73 63 3d 3d 30 20 29 3b 20 20 20 20 2f 2a 20 54  sc==0 );    /* T
25ff7 68 69 73 20 6f 6e 6c 79 20 6f 63 63 75 72 73 20  his only occurs 
25ff8 66 6f 72 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49  for GLOB, not LI
25ff9 4b 45 20 2a 2f 0a 20 20 20 20 20 20 73 65 65 6e  KE */.      seen
25ffa 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 76 65   = 0;.      inve
25ffb 72 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 20  rt = 0;.      c 
25ffc 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
25ffd 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a 53 74 72  d(zString, &zStr
25ffe 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ing);.      if( 
25fff 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  c==0 ) return 0;
26000 0a 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69  .      c2 = sqli
26001 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74  te3Utf8Read(zPat
26002 74 65 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29  tern, &zPattern)
26003 3b 0a 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d  ;.      if( c2==
26004 27 5e 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69  '^' ){.        i
26005 6e 76 65 72 74 20 3d 20 31 3b 0a 20 20 20 20 20  nvert = 1;.     
26006 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55     c2 = sqlite3U
26007 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e  tf8Read(zPattern
26008 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20  , &zPattern);.  
26009 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
2600a 63 32 3d 3d 27 5d 27 20 29 7b 0a 20 20 20 20 20  c2==']' ){.     
2600b 20 20 20 69 66 28 20 63 3d 3d 27 5d 27 20 29 20     if( c==']' ) 
2600c 73 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20  seen = 1;.      
2600d 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74    c2 = sqlite3Ut
2600e 66 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c  f8Read(zPattern,
2600f 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20 20   &zPattern);.   
26010 20 20 20 7d 0a 20 20 20 20 20 20 77 68 69 6c 65     }.      while
26011 28 20 63 32 20 26 26 20 63 32 21 3d 27 5d 27 20  ( c2 && c2!=']' 
26012 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  ){.        if( c
26013 32 3d 3d 27 2d 27 20 26 26 20 7a 50 61 74 74 65  2=='-' && zPatte
26014 72 6e 5b 30 5d 21 3d 27 5d 27 20 26 26 20 7a 50  rn[0]!=']' && zP
26015 61 74 74 65 72 6e 5b 30 5d 21 3d 30 20 26 26 20  attern[0]!=0 && 
26016 70 72 69 6f 72 5f 63 3e 30 20 29 7b 0a 20 20 20  prior_c>0 ){.   
26017 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69         c2 = sqli
26018 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74  te3Utf8Read(zPat
26019 74 65 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29  tern, &zPattern)
2601a 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
2601b 63 3e 3d 70 72 69 6f 72 5f 63 20 26 26 20 63 3c  c>=prior_c && c<
2601c 3d 63 32 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a  =c2 ) seen = 1;.
2601d 20 20 20 20 20 20 20 20 20 20 70 72 69 6f 72 5f            prior_
2601e 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  c = 0;.        }
2601f 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
26020 69 66 28 20 63 3d 3d 63 32 20 29 7b 0a 20 20 20  if( c==c2 ){.   
26021 20 20 20 20 20 20 20 20 20 73 65 65 6e 20 3d 20           seen = 
26022 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  1;.          }. 
26023 20 20 20 20 20 20 20 20 20 70 72 69 6f 72 5f 63           prior_c
26024 20 3d 20 63 32 3b 0a 20 20 20 20 20 20 20 20 7d   = c2;.        }
26025 0a 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71  .        c2 = sq
26026 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50  lite3Utf8Read(zP
26027 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 74 65 72  attern, &zPatter
26028 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  n);.      }.    
26029 20 20 69 66 28 20 63 32 3d 3d 30 20 7c 7c 20 28    if( c2==0 || (
2602a 73 65 65 6e 20 5e 20 69 6e 76 65 72 74 29 3d 3d  seen ^ invert)==
2602b 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  0 ){.        ret
2602c 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 0;.      }. 
2602d 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65 73 63     }else if( esc
2602e 3d 3d 63 20 26 26 20 21 70 72 65 76 45 73 63 61  ==c && !prevEsca
2602f 70 65 20 29 7b 0a 20 20 20 20 20 20 70 72 65 76  pe ){.      prev
26030 45 73 63 61 70 65 20 3d 20 31 3b 0a 20 20 20 20  Escape = 1;.    
26031 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 32 20  }else{.      c2 
26032 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
26033 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a 53 74 72  d(zString, &zStr
26034 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ing);.      if( 
26035 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20 20 20 20  noCase ){.      
26036 20 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77    GlogUpperToLow
26037 65 72 28 63 29 3b 0a 20 20 20 20 20 20 20 20 47  er(c);.        G
26038 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28  logUpperToLower(
26039 63 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  c2);.      }.   
2603a 20 20 20 69 66 28 20 63 21 3d 63 32 20 29 7b 0a     if( c!=c2 ){.
2603b 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
2603c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2603d 70 72 65 76 45 73 63 61 70 65 20 3d 20 30 3b 0a  prevEscape = 0;.
2603e 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
2603f 72 6e 20 2a 7a 53 74 72 69 6e 67 3d 3d 30 3b 0a  rn *zString==0;.
26040 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74  }../*.** Count t
26041 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d  he number of tim
26042 65 73 20 74 68 61 74 20 74 68 65 20 4c 49 4b 45  es that the LIKE
26043 20 6f 70 65 72 61 74 6f 72 20 28 6f 72 20 47 4c   operator (or GL
26044 4f 42 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 6a  OB which is.** j
26045 75 73 74 20 61 20 76 61 72 69 61 74 69 6f 6e 20  ust a variation 
26046 6f 66 20 4c 49 4b 45 29 20 67 65 74 73 20 63 61  of LIKE) gets ca
26047 6c 6c 65 64 2e 20 20 54 68 69 73 20 69 73 20 75  lled.  This is u
26048 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a  sed for testing.
26049 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64  ** only..*/.#ifd
2604a 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
2604b 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2604c 6c 69 74 65 33 5f 6c 69 6b 65 5f 63 6f 75 6e 74  lite3_like_count
2604d 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f   = 0;.#endif.../
2604e 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
2604f 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 6b 65 28  ion of the like(
26050 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20  ) SQL function. 
26051 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
26052 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 65  mplements.** the
26053 20 62 75 69 6c 64 2d 69 6e 20 4c 49 4b 45 20 6f   build-in LIKE o
26054 70 65 72 61 74 6f 72 2e 20 20 54 68 65 20 66 69  perator.  The fi
26055 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
26056 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
26057 74 68 65 0a 2a 2a 20 70 61 74 74 65 72 6e 20 61  the.** pattern a
26058 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  nd the second ar
26059 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 73 74  gument is the st
2605a 72 69 6e 67 2e 20 20 53 6f 2c 20 74 68 65 20 53  ring.  So, the S
2605b 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a  QL statements:.*
2605c 2a 0a 2a 2a 20 20 20 20 20 20 20 41 20 4c 49 4b  *.**       A LIK
2605d 45 20 42 0a 2a 2a 0a 2a 2a 20 69 73 20 69 6d 70  E B.**.** is imp
2605e 6c 65 6d 65 6e 74 65 64 20 61 73 20 6c 69 6b 65  lemented as like
2605f 28 42 2c 41 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  (B,A)..**.** Thi
26060 73 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20  s same function 
26061 28 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e  (with a differen
26062 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 73 74  t compareInfo st
26063 72 75 63 74 75 72 65 29 20 63 6f 6d 70 75 74 65  ructure) compute
26064 73 0a 2a 2a 20 74 68 65 20 47 4c 4f 42 20 6f 70  s.** the GLOB op
26065 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69  erator..*/.stati
26066 63 20 76 6f 69 64 20 6c 69 6b 65 46 75 6e 63 28  c void likeFunc(
26067 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
26068 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20  xt *context, .  
26069 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73 71 6c  int argc, .  sql
2606a 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
2606b 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  v.){.  const uns
2606c 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 41 2c 20  igned char *zA, 
2606d 2a 7a 42 3b 0a 20 20 69 6e 74 20 65 73 63 61 70  *zB;.  int escap
2606e 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 50 61  e = 0;.  int nPa
2606f 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  t;.  sqlite3 *db
26070 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
26071 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
26072 74 65 78 74 29 3b 0a 0a 20 20 7a 42 20 3d 20 73  text);..  zB = s
26073 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
26074 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7a 41  t(argv[0]);.  zA
26075 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
26076 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a  _text(argv[1]);.
26077 0a 20 20 2f 2a 20 4c 69 6d 69 74 20 74 68 65 20  .  /* Limit the 
26078 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 4c 49  length of the LI
26079 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61 74 74 65  KE or GLOB patte
2607a 72 6e 20 74 6f 20 61 76 6f 69 64 20 70 72 6f 62  rn to avoid prob
2607b 6c 65 6d 73 0a 20 20 2a 2a 20 6f 66 20 64 65 65  lems.  ** of dee
2607c 70 20 72 65 63 75 72 73 69 6f 6e 20 61 6e 64 20  p recursion and 
2607d 4e 2a 4e 20 62 65 68 61 76 69 6f 72 20 69 6e 20  N*N behavior in 
2607e 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 29  patternCompare()
2607f 2e 0a 20 20 2a 2f 0a 20 20 6e 50 61 74 20 3d 20  ..  */.  nPat = 
26080 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
26081 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  tes(argv[0]);.  
26082 74 65 73 74 63 61 73 65 28 20 6e 50 61 74 3d 3d  testcase( nPat==
26083 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
26084 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54  E_LIMIT_LIKE_PAT
26085 54 45 52 4e 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a  TERN_LENGTH] );.
26086 20 20 74 65 73 74 63 61 73 65 28 20 6e 50 61 74    testcase( nPat
26087 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ==db->aLimit[SQL
26088 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50  ITE_LIMIT_LIKE_P
26089 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 5d 2b 31  ATTERN_LENGTH]+1
2608a 20 29 3b 0a 20 20 69 66 28 20 6e 50 61 74 20 3e   );.  if( nPat >
2608b 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49   db->aLimit[SQLI
2608c 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41  TE_LIMIT_LIKE_PA
2608d 54 54 45 52 4e 5f 4c 45 4e 47 54 48 5d 20 29 7b  TTERN_LENGTH] ){
2608e 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
2608f 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78  ult_error(contex
26090 74 2c 20 22 4c 49 4b 45 20 6f 72 20 47 4c 4f 42  t, "LIKE or GLOB
26091 20 70 61 74 74 65 72 6e 20 74 6f 6f 20 63 6f 6d   pattern too com
26092 70 6c 65 78 22 2c 20 2d 31 29 3b 0a 20 20 20 20  plex", -1);.    
26093 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73  return;.  }.  as
26094 73 65 72 74 28 20 7a 42 3d 3d 73 71 6c 69 74 65  sert( zB==sqlite
26095 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
26096 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 45 6e 63  v[0]) );  /* Enc
26097 6f 64 69 6e 67 20 64 69 64 20 6e 6f 74 20 63 68  oding did not ch
26098 61 6e 67 65 20 2a 2f 0a 0a 20 20 69 66 28 20 61  ange */..  if( a
26099 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20 2f 2a  rgc==3 ){.    /*
2609a 20 54 68 65 20 65 73 63 61 70 65 20 63 68 61 72   The escape char
2609b 61 63 74 65 72 20 73 74 72 69 6e 67 20 6d 75 73  acter string mus
2609c 74 20 63 6f 6e 73 69 73 74 20 6f 66 20 61 20 73  t consist of a s
2609d 69 6e 67 6c 65 20 55 54 46 2d 38 20 63 68 61 72  ingle UTF-8 char
2609e 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20 4f 74  acter..    ** Ot
2609f 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 20  herwise, return 
260a0 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20 2a 2f  an error..    */
260a1 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  .    const unsig
260a2 6e 65 64 20 63 68 61 72 20 2a 7a 45 73 63 20 3d  ned char *zEsc =
260a3 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
260a4 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20  ext(argv[2]);.  
260a5 20 20 69 66 28 20 7a 45 73 63 3d 3d 30 20 29 20    if( zEsc==0 ) 
260a6 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20  return;.    if( 
260a7 73 71 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c  sqlite3Utf8CharL
260a8 65 6e 28 28 63 68 61 72 2a 29 7a 45 73 63 2c 20  en((char*)zEsc, 
260a9 2d 31 29 21 3d 31 20 29 7b 0a 20 20 20 20 20 20  -1)!=1 ){.      
260aa 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
260ab 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 0a 20  rror(context, . 
260ac 20 20 20 20 20 20 20 20 20 22 45 53 43 41 50 45           "ESCAPE
260ad 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75 73 74   expression must
260ae 20 62 65 20 61 20 73 69 6e 67 6c 65 20 63 68 61   be a single cha
260af 72 61 63 74 65 72 22 2c 20 2d 31 29 3b 0a 20 20  racter", -1);.  
260b0 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
260b1 7d 0a 20 20 20 20 65 73 63 61 70 65 20 3d 20 73  }.    escape = s
260b2 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
260b3 45 73 63 2c 20 26 7a 45 73 63 29 3b 0a 20 20 7d  Esc, &zEsc);.  }
260b4 0a 20 20 69 66 28 20 7a 41 20 26 26 20 7a 42 20  .  if( zA && zB 
260b5 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 63 6f  ){.    struct co
260b6 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49 6e 66 6f  mpareInfo *pInfo
260b7 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f   = sqlite3_user_
260b8 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 23  data(context);.#
260b9 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
260ba 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 69  T.    sqlite3_li
260bb 6b 65 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64  ke_count++;.#end
260bc 69 66 0a 20 20 20 20 0a 20 20 20 20 73 71 6c 69  if.    .    sqli
260bd 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63  te3_result_int(c
260be 6f 6e 74 65 78 74 2c 20 70 61 74 74 65 72 6e 43  ontext, patternC
260bf 6f 6d 70 61 72 65 28 7a 42 2c 20 7a 41 2c 20 70  ompare(zB, zA, p
260c0 49 6e 66 6f 2c 20 65 73 63 61 70 65 29 29 3b 0a  Info, escape));.
260c1 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70    }.}../*.** Imp
260c2 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
260c3 68 65 20 4e 55 4c 4c 49 46 28 78 2c 79 29 20 66  he NULLIF(x,y) f
260c4 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65  unction.  The re
260c5 73 75 6c 74 20 69 73 20 74 68 65 20 66 69 72 73  sult is the firs
260c6 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 66  t.** argument if
260c7 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73 20 61   the arguments a
260c8 72 65 20 64 69 66 66 65 72 65 6e 74 2e 20 20 54  re different.  T
260c9 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c  he result is NUL
260ca 4c 20 69 66 20 74 68 65 0a 2a 2a 20 61 72 67 75  L if the.** argu
260cb 6d 65 6e 74 73 20 61 72 65 20 65 71 75 61 6c 20  ments are equal 
260cc 74 6f 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a  to each other..*
260cd 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 75  /.static void nu
260ce 6c 6c 69 66 46 75 6e 63 28 0a 20 20 73 71 6c 69  llifFunc(.  sqli
260cf 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
260d0 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55  text,.  int NotU
260d1 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  sed,.  sqlite3_v
260d2 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
260d3 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20   CollSeq *pColl 
260d4 3d 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63  = sqlite3GetFunc
260d5 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65 78 74 29  CollSeq(context)
260d6 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
260d7 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
260d8 20 69 66 28 20 73 71 6c 69 74 65 33 4d 65 6d 43   if( sqlite3MemC
260d9 6f 6d 70 61 72 65 28 61 72 67 76 5b 30 5d 2c 20  ompare(argv[0], 
260da 61 72 67 76 5b 31 5d 2c 20 70 43 6f 6c 6c 29 21  argv[1], pColl)!
260db 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
260dc 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63  3_result_value(c
260dd 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 30 5d 29  ontext, argv[0])
260de 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ;.  }.}../*.** I
260df 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
260e0 20 74 68 65 20 73 71 6c 69 74 65 5f 76 65 72 73   the sqlite_vers
260e1 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20  ion() function. 
260e2 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 74   The result is t
260e3 68 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66  he version.** of
260e4 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
260e5 61 72 79 20 74 68 61 74 20 69 73 20 72 75 6e 6e  ary that is runn
260e6 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ing..*/.static v
260e7 6f 69 64 20 76 65 72 73 69 6f 6e 46 75 6e 63 28  oid versionFunc(
260e8 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
260e9 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
260ea 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71  nt NotUsed,.  sq
260eb 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f  lite3_value **No
260ec 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53  tUsed2.){.  UNUS
260ed 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f  ED_PARAMETER2(No
260ee 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29  tUsed, NotUsed2)
260ef 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
260f0 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
260f1 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e   sqlite3_version
260f2 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41  , -1, SQLITE_STA
260f3 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  TIC);.}../*.** I
260f4 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
260f5 20 74 68 65 20 73 71 6c 69 74 65 5f 73 6f 75 72   the sqlite_sour
260f6 63 65 5f 69 64 28 29 20 66 75 6e 63 74 69 6f 6e  ce_id() function
260f7 2e 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20  . The result is 
260f8 61 20 73 74 72 69 6e 67 0a 2a 2a 20 74 68 61 74  a string.** that
260f9 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20   identifies the 
260fa 70 61 72 74 69 63 75 6c 61 72 20 76 65 72 73 69  particular versi
260fb 6f 6e 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65  on of the source
260fc 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 62 75   code used to bu
260fd 69 6c 64 0a 2a 2a 20 53 51 4c 69 74 65 2e 0a 2a  ild.** SQLite..*
260fe 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6f  /.static void so
260ff 75 72 63 65 69 64 46 75 6e 63 28 0a 20 20 73 71  urceidFunc(.  sq
26100 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
26101 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f  ontext,.  int No
26102 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33  tUsed,.  sqlite3
26103 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64  _value **NotUsed
26104 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  2.){.  UNUSED_PA
26105 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64  RAMETER2(NotUsed
26106 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 73  , NotUsed2);.  s
26107 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
26108 78 74 28 63 6f 6e 74 65 78 74 2c 20 53 51 4c 49  xt(context, SQLI
26109 54 45 5f 53 4f 55 52 43 45 5f 49 44 2c 20 2d 31  TE_SOURCE_ID, -1
2610a 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
2610b 3b 0a 7d 0a 0a 2f 2a 20 41 72 72 61 79 20 66 6f  ;.}../* Array fo
2610c 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f  r converting fro
2610d 6d 20 68 61 6c 66 2d 62 79 74 65 73 20 28 6e 79  m half-bytes (ny
2610e 62 62 6c 65 73 29 20 69 6e 74 6f 20 41 53 43 49  bbles) into ASCI
2610f 49 20 68 65 78 0a 2a 2a 20 64 69 67 69 74 73 2e  I hex.** digits.
26110 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74   */.static const
26111 20 63 68 61 72 20 68 65 78 64 69 67 69 74 73 5b   char hexdigits[
26112 5d 20 3d 20 7b 0a 20 20 27 30 27 2c 20 27 31 27  ] = {.  '0', '1'
26113 2c 20 27 32 27 2c 20 27 33 27 2c 20 27 34 27 2c  , '2', '3', '4',
26114 20 27 35 27 2c 20 27 36 27 2c 20 27 37 27 2c 0a   '5', '6', '7',.
26115 20 20 27 38 27 2c 20 27 39 27 2c 20 27 41 27 2c    '8', '9', 'A',
26116 20 27 42 27 2c 20 27 43 27 2c 20 27 44 27 2c 20   'B', 'C', 'D', 
26117 27 45 27 2c 20 27 46 27 20 0a 7d 3b 0a 0a 2f 2a  'E', 'F' .};../*
26118 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
26119 20 2d 20 54 68 69 73 20 69 73 20 6e 6f 74 20 61   - This is not a
2611a 6e 20 6f 66 66 69 63 69 61 6c 20 66 75 6e 63 74  n official funct
2611b 69 6f 6e 2e 20 20 54 68 65 20 69 6e 74 65 72 66  ion.  The interf
2611c 61 63 65 20 6d 61 79 0a 2a 2a 20 63 68 61 6e 67  ace may.** chang
2611d 65 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f  e.  This functio
2611e 6e 20 6d 61 79 20 64 69 73 61 70 70 65 61 72 2e  n may disappear.
2611f 20 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 20 63    Do not write c
26120 6f 64 65 20 74 68 61 74 20 64 65 70 65 6e 64 73  ode that depends
26121 0a 2a 2a 20 6f 6e 20 74 68 69 73 20 66 75 6e 63  .** on this func
26122 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c  tion..**.** Impl
26123 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
26124 65 20 51 55 4f 54 45 28 29 20 66 75 6e 63 74 69  e QUOTE() functi
26125 6f 6e 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69  on.  This functi
26126 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c  on takes a singl
26127 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20 20  e.** argument.  
26128 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  If the argument 
26129 69 73 20 6e 75 6d 65 72 69 63 2c 20 74 68 65 20  is numeric, the 
2612a 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
2612b 74 68 65 20 73 61 6d 65 20 61 73 0a 2a 2a 20 74  the same as.** t
2612c 68 65 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66  he argument.  If
2612d 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73   the argument is
2612e 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 74 75 72   NULL, the retur
2612f 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 73  n value is the s
26130 74 72 69 6e 67 0a 2a 2a 20 22 4e 55 4c 4c 22 2e  tring.** "NULL".
26131 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65    Otherwise, the
26132 20 61 72 67 75 6d 65 6e 74 20 69 73 20 65 6e 63   argument is enc
26133 6c 6f 73 65 64 20 69 6e 20 73 69 6e 67 6c 65 20  losed in single 
26134 71 75 6f 74 65 73 20 77 69 74 68 0a 2a 2a 20 73  quotes with.** s
26135 69 6e 67 6c 65 2d 71 75 6f 74 65 20 65 73 63 61  ingle-quote esca
26136 70 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  pes..*/.static v
26137 6f 69 64 20 71 75 6f 74 65 46 75 6e 63 28 73 71  oid quoteFunc(sq
26138 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
26139 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
2613a 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
2613b 2a 2a 61 72 67 76 29 7b 0a 20 20 61 73 73 65 72  **argv){.  asser
2613c 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20  t( argc==1 );.  
2613d 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
2613e 28 61 72 67 63 29 3b 0a 20 20 73 77 69 74 63 68  (argc);.  switch
2613f 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
26140 74 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29 7b  type(argv[0]) ){
26141 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
26142 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 61  _INTEGER:.    ca
26143 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a  se SQLITE_FLOAT:
26144 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
26145 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f  _result_value(co
26146 6e 74 65 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b  ntext, argv[0]);
26147 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
26148 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
26149 49 54 45 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 20  ITE_BLOB: {.    
2614a 20 20 63 68 61 72 20 2a 7a 54 65 78 74 20 3d 20    char *zText = 
2614b 30 3b 0a 20 20 20 20 20 20 63 68 61 72 20 63 6f  0;.      char co
2614c 6e 73 74 20 2a 7a 42 6c 6f 62 20 3d 20 73 71 6c  nst *zBlob = sql
2614d 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
2614e 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20  argv[0]);.      
2614f 69 6e 74 20 6e 42 6c 6f 62 20 3d 20 73 71 6c 69  int nBlob = sqli
26150 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
26151 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20  argv[0]);.      
26152 61 73 73 65 72 74 28 20 7a 42 6c 6f 62 3d 3d 73  assert( zBlob==s
26153 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
26154 62 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 2f 2a  b(argv[0]) ); /*
26155 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61   No encoding cha
26156 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20 7a 54 65  nge */.      zTe
26157 78 74 20 3d 20 28 63 68 61 72 20 2a 29 63 6f 6e  xt = (char *)con
26158 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65  textMalloc(conte
26159 78 74 2c 20 28 32 2a 28 69 36 34 29 6e 42 6c 6f  xt, (2*(i64)nBlo
2615a 62 29 2b 34 29 3b 20 0a 20 20 20 20 20 20 69 66  b)+4); .      if
2615b 28 20 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 20  ( zText ){.     
2615c 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
2615d 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 42 6c    for(i=0; i<nBl
2615e 6f 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ob; i++){.      
2615f 20 20 20 20 7a 54 65 78 74 5b 28 69 2a 32 29 2b      zText[(i*2)+
26160 32 5d 20 3d 20 68 65 78 64 69 67 69 74 73 5b 28  2] = hexdigits[(
26161 7a 42 6c 6f 62 5b 69 5d 3e 3e 34 29 26 30 78 30  zBlob[i]>>4)&0x0
26162 46 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 54  F];.          zT
26163 65 78 74 5b 28 69 2a 32 29 2b 33 5d 20 3d 20 68  ext[(i*2)+3] = h
26164 65 78 64 69 67 69 74 73 5b 28 7a 42 6c 6f 62 5b  exdigits[(zBlob[
26165 69 5d 29 26 30 78 30 46 5d 3b 0a 20 20 20 20 20  i])&0x0F];.     
26166 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7a 54 65     }.        zTe
26167 78 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b 32 5d 20  xt[(nBlob*2)+2] 
26168 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20  = '\'';.        
26169 7a 54 65 78 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b  zText[(nBlob*2)+
2616a 33 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20  3] = '\0';.     
2616b 20 20 20 7a 54 65 78 74 5b 30 5d 20 3d 20 27 58     zText[0] = 'X
2616c 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74  ';.        zText
2616d 5b 31 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20  [1] = '\'';.    
2616e 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
2616f 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
26170 20 7a 54 65 78 74 2c 20 2d 31 2c 20 53 51 4c 49   zText, -1, SQLI
26171 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
26172 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
26173 72 65 65 28 7a 54 65 78 74 29 3b 0a 20 20 20 20  ree(zText);.    
26174 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
26175 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
26176 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20  SQLITE_TEXT: {. 
26177 20 20 20 20 20 69 6e 74 20 69 2c 6a 3b 0a 20 20       int i,j;.  
26178 20 20 20 20 75 36 34 20 6e 3b 0a 20 20 20 20 20      u64 n;.     
26179 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
2617a 63 68 61 72 20 2a 7a 41 72 67 20 3d 20 73 71 6c  char *zArg = sql
2617b 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
2617c 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20  argv[0]);.      
2617d 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 20 20 20 20  char *z;..      
2617e 69 66 28 20 7a 41 72 67 3d 3d 30 20 29 20 72 65  if( zArg==0 ) re
2617f 74 75 72 6e 3b 0a 20 20 20 20 20 20 66 6f 72 28  turn;.      for(
26180 69 3d 30 2c 20 6e 3d 30 3b 20 7a 41 72 67 5b 69  i=0, n=0; zArg[i
26181 5d 3b 20 69 2b 2b 29 7b 20 69 66 28 20 7a 41 72  ]; i++){ if( zAr
26182 67 5b 69 5d 3d 3d 27 5c 27 27 20 29 20 6e 2b 2b  g[i]=='\'' ) n++
26183 3b 20 7d 0a 20 20 20 20 20 20 7a 20 3d 20 63 6f  ; }.      z = co
26184 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74  ntextMalloc(cont
26185 65 78 74 2c 20 28 28 69 36 34 29 69 29 2b 28 28  ext, ((i64)i)+((
26186 69 36 34 29 6e 29 2b 33 29 3b 0a 20 20 20 20 20  i64)n)+3);.     
26187 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20   if( z ){.      
26188 20 20 7a 5b 30 5d 20 3d 20 27 5c 27 27 3b 0a 20    z[0] = '\'';. 
26189 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20         for(i=0, 
2618a 6a 3d 31 3b 20 7a 41 72 67 5b 69 5d 3b 20 69 2b  j=1; zArg[i]; i+
2618b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 5b  +){.          z[
2618c 6a 2b 2b 5d 20 3d 20 7a 41 72 67 5b 69 5d 3b 0a  j++] = zArg[i];.
2618d 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 41            if( zA
2618e 72 67 5b 69 5d 3d 3d 27 5c 27 27 20 29 7b 0a 20  rg[i]=='\'' ){. 
2618f 20 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b             z[j++
26190 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20  ] = '\'';.      
26191 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
26192 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d          z[j++] =
26193 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 7a   '\'';.        z
26194 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  [j] = 0;.       
26195 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
26196 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c  text(context, z,
26197 20 6a 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65   j, sqlite3_free
26198 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
26199 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2619a 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
2619b 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
2619c 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
2619d 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e  gv[0])==SQLITE_N
2619e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ULL );.      sql
2619f 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
261a0 28 63 6f 6e 74 65 78 74 2c 20 22 4e 55 4c 4c 22  (context, "NULL"
261a1 2c 20 34 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  , 4, SQLITE_STAT
261a2 49 43 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  IC);.      break
261a3 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
261a4 2a 0a 2a 2a 20 54 68 65 20 68 65 78 28 29 20 66  *.** The hex() f
261a5 75 6e 63 74 69 6f 6e 2e 20 20 49 6e 74 65 72 70  unction.  Interp
261a6 72 65 74 20 74 68 65 20 61 72 67 75 6d 65 6e 74  ret the argument
261a7 20 61 73 20 61 20 62 6c 6f 62 2e 20 20 52 65 74   as a blob.  Ret
261a8 75 72 6e 0a 2a 2a 20 61 20 68 65 78 61 64 65 63  urn.** a hexadec
261a9 69 6d 61 6c 20 72 65 6e 64 65 72 69 6e 67 20 61  imal rendering a
261aa 73 20 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 74 69  s text..*/.stati
261ab 63 20 76 6f 69 64 20 68 65 78 46 75 6e 63 28 0a  c void hexFunc(.
261ac 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
261ad 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
261ae 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
261af 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
261b0 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20  {.  int i, n;.  
261b1 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
261b2 68 61 72 20 2a 70 42 6c 6f 62 3b 0a 20 20 63 68  har *pBlob;.  ch
261b3 61 72 20 2a 7a 48 65 78 2c 20 2a 7a 3b 0a 20 20  ar *zHex, *z;.  
261b4 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20  assert( argc==1 
261b5 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
261b6 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 70  METER(argc);.  p
261b7 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f 76  Blob = sqlite3_v
261b8 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30  alue_blob(argv[0
261b9 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65  ]);.  n = sqlite
261ba 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
261bb 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73 65 72 74  gv[0]);.  assert
261bc 28 20 70 42 6c 6f 62 3d 3d 73 71 6c 69 74 65 33  ( pBlob==sqlite3
261bd 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76  _value_blob(argv
261be 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f 20 65  [0]) );  /* No e
261bf 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 20 2a  ncoding change *
261c0 2f 0a 20 20 7a 20 3d 20 7a 48 65 78 20 3d 20 63  /.  z = zHex = c
261c1 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e  ontextMalloc(con
261c2 74 65 78 74 2c 20 28 28 69 36 34 29 6e 29 2a 32  text, ((i64)n)*2
261c3 20 2b 20 31 29 3b 0a 20 20 69 66 28 20 7a 48 65   + 1);.  if( zHe
261c4 78 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  x ){.    for(i=0
261c5 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 42 6c 6f  ; i<n; i++, pBlo
261c6 62 2b 2b 29 7b 0a 20 20 20 20 20 20 75 6e 73 69  b++){.      unsi
261c7 67 6e 65 64 20 63 68 61 72 20 63 20 3d 20 2a 70  gned char c = *p
261c8 42 6c 6f 62 3b 0a 20 20 20 20 20 20 2a 28 7a 2b  Blob;.      *(z+
261c9 2b 29 20 3d 20 68 65 78 64 69 67 69 74 73 5b 28  +) = hexdigits[(
261ca 63 3e 3e 34 29 26 30 78 66 5d 3b 0a 20 20 20 20  c>>4)&0xf];.    
261cb 20 20 2a 28 7a 2b 2b 29 20 3d 20 68 65 78 64 69    *(z++) = hexdi
261cc 67 69 74 73 5b 63 26 30 78 66 5d 3b 0a 20 20 20  gits[c&0xf];.   
261cd 20 7d 0a 20 20 20 20 2a 7a 20 3d 20 30 3b 0a 20   }.    *z = 0;. 
261ce 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
261cf 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
261d0 7a 48 65 78 2c 20 6e 2a 32 2c 20 73 71 6c 69 74  zHex, n*2, sqlit
261d1 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a  e3_free);.  }.}.
261d2 0a 2f 2a 0a 2a 2a 20 54 68 65 20 7a 65 72 6f 62  ./*.** The zerob
261d3 6c 6f 62 28 4e 29 20 66 75 6e 63 74 69 6f 6e 20  lob(N) function 
261d4 72 65 74 75 72 6e 73 20 61 20 7a 65 72 6f 2d 66  returns a zero-f
261d5 69 6c 6c 65 64 20 62 6c 6f 62 20 6f 66 20 73 69  illed blob of si
261d6 7a 65 20 4e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73  ze N bytes..*/.s
261d7 74 61 74 69 63 20 76 6f 69 64 20 7a 65 72 6f 62  tatic void zerob
261d8 6c 6f 62 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  lobFunc(.  sqlit
261d9 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
261da 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  ext,.  int argc,
261db 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
261dc 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 36 34   **argv.){.  i64
261dd 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   n;.  sqlite3 *d
261de 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
261df 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
261e0 6e 74 65 78 74 29 3b 0a 20 20 61 73 73 65 72 74  ntext);.  assert
261e1 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55  ( argc==1 );.  U
261e2 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
261e3 61 72 67 63 29 3b 0a 20 20 6e 20 3d 20 73 71 6c  argc);.  n = sql
261e4 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
261e5 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 74 65 73  (argv[0]);.  tes
261e6 74 63 61 73 65 28 20 6e 3d 3d 64 62 2d 3e 61 4c  tcase( n==db->aL
261e7 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
261e8 54 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 74  T_LENGTH] );.  t
261e9 65 73 74 63 61 73 65 28 20 6e 3d 3d 64 62 2d 3e  estcase( n==db->
261ea 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
261eb 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b  MIT_LENGTH]+1 );
261ec 0a 20 20 69 66 28 20 6e 3e 64 62 2d 3e 61 4c 69  .  if( n>db->aLi
261ed 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
261ee 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20  _LENGTH] ){.    
261ef 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
261f0 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74  rror_toobig(cont
261f1 65 78 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ext);.  }else{. 
261f2 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
261f3 74 5f 7a 65 72 6f 62 6c 6f 62 28 63 6f 6e 74 65  t_zeroblob(conte
261f4 78 74 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20 7d  xt, (int)n);.  }
261f5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65  .}../*.** The re
261f6 70 6c 61 63 65 28 29 20 66 75 6e 63 74 69 6f 6e  place() function
261f7 2e 20 20 54 68 72 65 65 20 61 72 67 75 6d 65 6e  .  Three argumen
261f8 74 73 20 61 72 65 20 61 6c 6c 20 73 74 72 69 6e  ts are all strin
261f9 67 73 3a 20 63 61 6c 6c 0a 2a 2a 20 74 68 65 6d  gs: call.** them
261fa 20 41 2c 20 42 2c 20 61 6e 64 20 43 2e 20 54 68   A, B, and C. Th
261fb 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c 73 6f  e result is also
261fc 20 61 20 73 74 72 69 6e 67 20 77 68 69 63 68 20   a string which 
261fd 69 73 20 64 65 72 69 76 65 64 0a 2a 2a 20 66 72  is derived.** fr
261fe 6f 6d 20 41 20 62 79 20 72 65 70 6c 61 63 69 6e  om A by replacin
261ff 67 20 65 76 65 72 79 20 6f 63 63 75 72 61 6e 63  g every occuranc
26200 65 20 6f 66 20 42 20 77 69 74 68 20 43 2e 20 20  e of B with C.  
26201 54 68 65 20 6d 61 74 63 68 0a 2a 2a 20 6d 75 73  The match.** mus
26202 74 20 62 65 20 65 78 61 63 74 2e 20 20 43 6f 6c  t be exact.  Col
26203 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73  lating sequences
26204 20 61 72 65 20 6e 6f 74 20 75 73 65 64 2e 0a 2a   are not used..*
26205 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
26206 70 6c 61 63 65 46 75 6e 63 28 0a 20 20 73 71 6c  placeFunc(.  sql
26207 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
26208 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
26209 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
2620a 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63  ue **argv.){.  c
2620b 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
2620c 61 72 20 2a 7a 53 74 72 3b 20 20 20 20 20 20 20  ar *zStr;       
2620d 20 2f 2a 20 54 68 65 20 69 6e 70 75 74 20 73 74   /* The input st
2620e 72 69 6e 67 20 41 20 2a 2f 0a 20 20 63 6f 6e 73  ring A */.  cons
2620f 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
26210 2a 7a 50 61 74 74 65 72 6e 3b 20 20 20 20 2f 2a  *zPattern;    /*
26211 20 54 68 65 20 70 61 74 74 65 72 6e 20 73 74 72   The pattern str
26212 69 6e 67 20 42 20 2a 2f 0a 20 20 63 6f 6e 73 74  ing B */.  const
26213 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
26214 7a 52 65 70 3b 20 20 20 20 20 20 20 20 2f 2a 20  zRep;        /* 
26215 54 68 65 20 72 65 70 6c 61 63 65 6d 65 6e 74 20  The replacement 
26216 73 74 72 69 6e 67 20 43 20 2a 2f 0a 20 20 75 6e  string C */.  un
26217 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f 75  signed char *zOu
26218 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
26219 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 20 2a 2f  /* The output */
2621a 0a 20 20 69 6e 74 20 6e 53 74 72 3b 20 20 20 20  .  int nStr;    
2621b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
2621c 69 7a 65 20 6f 66 20 7a 53 74 72 20 2a 2f 0a 20  ize of zStr */. 
2621d 20 69 6e 74 20 6e 50 61 74 74 65 72 6e 3b 20 20   int nPattern;  
2621e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
2621f 65 20 6f 66 20 7a 50 61 74 74 65 72 6e 20 2a 2f  e of zPattern */
26220 0a 20 20 69 6e 74 20 6e 52 65 70 3b 20 20 20 20  .  int nRep;    
26221 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
26222 69 7a 65 20 6f 66 20 7a 52 65 70 20 2a 2f 0a 20  ize of zRep */. 
26223 20 69 36 34 20 6e 4f 75 74 3b 20 20 20 20 20 20   i64 nOut;      
26224 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
26225 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 7a 4f 75  imum size of zOu
26226 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f 6f 70 4c  t */.  int loopL
26227 69 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20 20  imit;           
26228 2f 2a 20 4c 61 73 74 20 7a 53 74 72 5b 5d 20 74  /* Last zStr[] t
26229 68 61 74 20 6d 69 67 68 74 20 6d 61 74 63 68 20  hat might match 
2622a 7a 50 61 74 74 65 72 6e 5b 5d 20 2a 2f 0a 20 20  zPattern[] */.  
2622b 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20  int i, j;       
2622c 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
2622d 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 0a 20 20   counters */..  
2622e 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 33 20  assert( argc==3 
2622f 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
26230 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 7a  METER(argc);.  z
26231 53 74 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  Str = sqlite3_va
26232 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
26233 29 3b 0a 20 20 69 66 28 20 7a 53 74 72 3d 3d 30  );.  if( zStr==0
26234 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6e 53 74   ) return;.  nSt
26235 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  r = sqlite3_valu
26236 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29  e_bytes(argv[0])
26237 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 53 74 72  ;.  assert( zStr
26238 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ==sqlite3_value_
26239 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b  text(argv[0]) );
2623a 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67    /* No encoding
2623b 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 7a 50 61   change */.  zPa
2623c 74 74 65 72 6e 20 3d 20 73 71 6c 69 74 65 33 5f  ttern = sqlite3_
2623d 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
2623e 31 5d 29 3b 0a 20 20 69 66 28 20 7a 50 61 74 74  1]);.  if( zPatt
2623f 65 72 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73  ern==0 ){.    as
26240 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 76 61  sert( sqlite3_va
26241 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31 5d  lue_type(argv[1]
26242 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 0a 20  )==SQLITE_NULL. 
26243 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71             || sq
26244 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
26245 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
26246 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
26247 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
26248 7d 0a 20 20 69 66 28 20 7a 50 61 74 74 65 72 6e  }.  if( zPattern
26249 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73  [0]==0 ){.    as
2624a 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 76 61  sert( sqlite3_va
2624b 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31 5d  lue_type(argv[1]
2624c 29 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  )!=SQLITE_NULL )
2624d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
2624e 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65  sult_value(conte
2624f 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20 20  xt, argv[0]);.  
26250 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
26251 6e 50 61 74 74 65 72 6e 20 3d 20 73 71 6c 69 74  nPattern = sqlit
26252 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
26253 72 67 76 5b 31 5d 29 3b 0a 20 20 61 73 73 65 72  rgv[1]);.  asser
26254 74 28 20 7a 50 61 74 74 65 72 6e 3d 3d 73 71 6c  t( zPattern==sql
26255 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
26256 61 72 67 76 5b 31 5d 29 20 29 3b 20 20 2f 2a 20  argv[1]) );  /* 
26257 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e  No encoding chan
26258 67 65 20 2a 2f 0a 20 20 7a 52 65 70 20 3d 20 73  ge */.  zRep = s
26259 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2625a 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 69 66  t(argv[2]);.  if
2625b 28 20 7a 52 65 70 3d 3d 30 20 29 20 72 65 74 75  ( zRep==0 ) retu
2625c 72 6e 3b 0a 20 20 6e 52 65 70 20 3d 20 73 71 6c  rn;.  nRep = sql
2625d 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
2625e 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 61 73 73  (argv[2]);.  ass
2625f 65 72 74 28 20 7a 52 65 70 3d 3d 73 71 6c 69 74  ert( zRep==sqlit
26260 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
26261 67 76 5b 32 5d 29 20 29 3b 0a 20 20 6e 4f 75 74  gv[2]) );.  nOut
26262 20 3d 20 6e 53 74 72 20 2b 20 31 3b 0a 20 20 61   = nStr + 1;.  a
26263 73 73 65 72 74 28 20 6e 4f 75 74 3c 53 51 4c 49  ssert( nOut<SQLI
26264 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b  TE_MAX_LENGTH );
26265 0a 20 20 7a 4f 75 74 20 3d 20 63 6f 6e 74 65 78  .  zOut = contex
26266 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c  tMalloc(context,
26267 20 28 69 36 34 29 6e 4f 75 74 29 3b 0a 20 20 69   (i64)nOut);.  i
26268 66 28 20 7a 4f 75 74 3d 3d 30 20 29 7b 0a 20 20  f( zOut==0 ){.  
26269 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
2626a 6c 6f 6f 70 4c 69 6d 69 74 20 3d 20 6e 53 74 72  loopLimit = nStr
2626b 20 2d 20 6e 50 61 74 74 65 72 6e 3b 20 20 0a 20   - nPattern;  . 
2626c 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c 3d 6c   for(i=j=0; i<=l
2626d 6f 6f 70 4c 69 6d 69 74 3b 20 69 2b 2b 29 7b 0a  oopLimit; i++){.
2626e 20 20 20 20 69 66 28 20 7a 53 74 72 5b 69 5d 21      if( zStr[i]!
2626f 3d 7a 50 61 74 74 65 72 6e 5b 30 5d 20 7c 7c 20  =zPattern[0] || 
26270 6d 65 6d 63 6d 70 28 26 7a 53 74 72 5b 69 5d 2c  memcmp(&zStr[i],
26271 20 7a 50 61 74 74 65 72 6e 2c 20 6e 50 61 74 74   zPattern, nPatt
26272 65 72 6e 29 20 29 7b 0a 20 20 20 20 20 20 7a 4f  ern) ){.      zO
26273 75 74 5b 6a 2b 2b 5d 20 3d 20 7a 53 74 72 5b 69  ut[j++] = zStr[i
26274 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  ];.    }else{.  
26275 20 20 20 20 75 38 20 2a 7a 4f 6c 64 3b 0a 20 20      u8 *zOld;.  
26276 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
26277 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  = sqlite3_contex
26278 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
26279 65 78 74 29 3b 0a 20 20 20 20 20 20 6e 4f 75 74  ext);.      nOut
2627a 20 2b 3d 20 6e 52 65 70 20 2d 20 6e 50 61 74 74   += nRep - nPatt
2627b 65 72 6e 3b 0a 20 20 20 20 20 20 74 65 73 74 63  ern;.      testc
2627c 61 73 65 28 20 6e 4f 75 74 2d 31 3d 3d 64 62 2d  ase( nOut-1==db-
2627d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
2627e 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a  IMIT_LENGTH] );.
2627f 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
26280 6e 4f 75 74 2d 32 3d 3d 64 62 2d 3e 61 4c 69 6d  nOut-2==db->aLim
26281 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
26282 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 20 20 20  LENGTH] );.     
26283 20 69 66 28 20 6e 4f 75 74 2d 31 3e 64 62 2d 3e   if( nOut-1>db->
26284 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
26285 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20  MIT_LENGTH] ){. 
26286 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72         sqlite3_r
26287 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62  esult_error_toob
26288 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  ig(context);.   
26289 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
2628a 65 65 28 64 62 2c 20 7a 4f 75 74 29 3b 0a 20 20  ee(db, zOut);.  
2628b 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
2628c 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 4f 6c 64      }.      zOld
2628d 20 3d 20 7a 4f 75 74 3b 0a 20 20 20 20 20 20 7a   = zOut;.      z
2628e 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 72 65  Out = sqlite3_re
2628f 61 6c 6c 6f 63 28 7a 4f 75 74 2c 20 28 69 6e 74  alloc(zOut, (int
26290 29 6e 4f 75 74 29 3b 0a 20 20 20 20 20 20 69 66  )nOut);.      if
26291 28 20 7a 4f 75 74 3d 3d 30 20 29 7b 0a 20 20 20  ( zOut==0 ){.   
26292 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
26293 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28  ult_error_nomem(
26294 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  context);.      
26295 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
26296 64 62 2c 20 7a 4f 6c 64 29 3b 0a 20 20 20 20 20  db, zOld);.     
26297 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20     return;.     
26298 20 7d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28   }.      memcpy(
26299 26 7a 4f 75 74 5b 6a 5d 2c 20 7a 52 65 70 2c 20  &zOut[j], zRep, 
2629a 6e 52 65 70 29 3b 0a 20 20 20 20 20 20 6a 20 2b  nRep);.      j +
2629b 3d 20 6e 52 65 70 3b 0a 20 20 20 20 20 20 69 20  = nRep;.      i 
2629c 2b 3d 20 6e 50 61 74 74 65 72 6e 2d 31 3b 0a 20  += nPattern-1;. 
2629d 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72     }.  }.  asser
2629e 74 28 20 6a 2b 6e 53 74 72 2d 69 2b 31 3d 3d 6e  t( j+nStr-i+1==n
2629f 4f 75 74 20 29 3b 0a 20 20 6d 65 6d 63 70 79 28  Out );.  memcpy(
262a0 26 7a 4f 75 74 5b 6a 5d 2c 20 26 7a 53 74 72 5b  &zOut[j], &zStr[
262a1 69 5d 2c 20 6e 53 74 72 2d 69 29 3b 0a 20 20 6a  i], nStr-i);.  j
262a2 20 2b 3d 20 6e 53 74 72 20 2d 20 69 3b 0a 20 20   += nStr - i;.  
262a3 61 73 73 65 72 74 28 20 6a 3c 3d 6e 4f 75 74 20  assert( j<=nOut 
262a4 29 3b 0a 20 20 7a 4f 75 74 5b 6a 5d 20 3d 20 30  );.  zOut[j] = 0
262a5 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
262a6 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
262a7 20 28 63 68 61 72 2a 29 7a 4f 75 74 2c 20 6a 2c   (char*)zOut, j,
262a8 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
262a9 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
262aa 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 54  ntation of the T
262ab 52 49 4d 28 29 2c 20 4c 54 52 49 4d 28 29 2c 20  RIM(), LTRIM(), 
262ac 61 6e 64 20 52 54 52 49 4d 28 29 20 66 75 6e 63  and RTRIM() func
262ad 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 75 73  tions..** The us
262ae 65 72 64 61 74 61 20 69 73 20 30 78 31 20 66 6f  erdata is 0x1 fo
262af 72 20 6c 65 66 74 20 74 72 69 6d 2c 20 30 78 32  r left trim, 0x2
262b0 20 66 6f 72 20 72 69 67 68 74 20 74 72 69 6d 2c   for right trim,
262b1 20 30 78 33 20 66 6f 72 20 62 6f 74 68 2e 0a 2a   0x3 for both..*
262b2 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 72  /.static void tr
262b3 69 6d 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  imFunc(.  sqlite
262b4 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
262b5 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
262b6 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
262b7 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73  **argv.){.  cons
262b8 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
262b9 2a 7a 49 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a  *zIn;         /*
262ba 20 49 6e 70 75 74 20 73 74 72 69 6e 67 20 2a 2f   Input string */
262bb 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
262bc 64 20 63 68 61 72 20 2a 7a 43 68 61 72 53 65 74  d char *zCharSet
262bd 3b 20 20 20 20 2f 2a 20 53 65 74 20 6f 66 20 63  ;    /* Set of c
262be 68 61 72 61 63 74 65 72 73 20 74 6f 20 74 72 69  haracters to tri
262bf 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 3b 20  m */.  int nIn; 
262c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
262c1 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
262c2 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 69  er of bytes in i
262c3 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  nput */.  int fl
262c4 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
262c5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31              /* 1
262c6 3a 20 74 72 69 6d 6c 65 66 74 20 20 32 3a 20 74  : trimleft  2: t
262c7 72 69 6d 72 69 67 68 74 20 20 33 3a 20 74 72 69  rimright  3: tri
262c8 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  m */.  int i;   
262c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
262ca 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
262cb 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e   counter */.  un
262cc 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4c 65  signed char *aLe
262cd 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  n = 0;          
262ce 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 65 61 63  /* Length of eac
262cf 68 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 7a  h character in z
262d0 43 68 61 72 53 65 74 20 2a 2f 0a 20 20 75 6e 73  CharSet */.  uns
262d1 69 67 6e 65 64 20 63 68 61 72 20 2a 2a 61 7a 43  igned char **azC
262d2 68 61 72 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  har = 0;       /
262d3 2a 20 49 6e 64 69 76 69 64 75 61 6c 20 63 68 61  * Individual cha
262d4 72 61 63 74 65 72 73 20 69 6e 20 7a 43 68 61 72  racters in zChar
262d5 53 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68  Set */.  int nCh
262d6 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ar;             
262d7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
262d8 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
262d9 72 73 20 69 6e 20 7a 43 68 61 72 53 65 74 20 2a  rs in zCharSet *
262da 2f 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  /..  if( sqlite3
262db 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
262dc 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c  [0])==SQLITE_NUL
262dd 4c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  L ){.    return;
262de 0a 20 20 7d 0a 20 20 7a 49 6e 20 3d 20 73 71 6c  .  }.  zIn = sql
262df 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
262e0 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20  argv[0]);.  if( 
262e1 7a 49 6e 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  zIn==0 ) return;
262e2 0a 20 20 6e 49 6e 20 3d 20 73 71 6c 69 74 65 33  .  nIn = sqlite3
262e3 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
262e4 76 5b 30 5d 29 3b 0a 20 20 61 73 73 65 72 74 28  v[0]);.  assert(
262e5 20 7a 49 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61   zIn==sqlite3_va
262e6 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
262e7 29 20 29 3b 0a 20 20 69 66 28 20 61 72 67 63 3d  ) );.  if( argc=
262e8 3d 31 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63  =1 ){.    static
262e9 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
262ea 63 68 61 72 20 6c 65 6e 4f 6e 65 5b 5d 20 3d 20  char lenOne[] = 
262eb 7b 20 31 20 7d 3b 0a 20 20 20 20 73 74 61 74 69  { 1 };.    stati
262ec 63 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  c unsigned char 
262ed 2a 20 63 6f 6e 73 74 20 61 7a 4f 6e 65 5b 5d 20  * const azOne[] 
262ee 3d 20 7b 20 28 75 38 2a 29 22 20 22 20 7d 3b 0a  = { (u8*)" " };.
262ef 20 20 20 20 6e 43 68 61 72 20 3d 20 31 3b 0a 20      nChar = 1;. 
262f0 20 20 20 61 4c 65 6e 20 3d 20 28 75 38 2a 29 6c     aLen = (u8*)l
262f1 65 6e 4f 6e 65 3b 0a 20 20 20 20 61 7a 43 68 61  enOne;.    azCha
262f2 72 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  r = (unsigned ch
262f3 61 72 20 2a 2a 29 61 7a 4f 6e 65 3b 0a 20 20 20  ar **)azOne;.   
262f4 20 7a 43 68 61 72 53 65 74 20 3d 20 30 3b 0a 20   zCharSet = 0;. 
262f5 20 7d 65 6c 73 65 20 69 66 28 20 28 7a 43 68 61   }else if( (zCha
262f6 72 53 65 74 20 3d 20 73 71 6c 69 74 65 33 5f 76  rSet = sqlite3_v
262f7 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31  alue_text(argv[1
262f8 5d 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ]))==0 ){.    re
262f9 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  turn;.  }else{. 
262fa 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65     const unsigne
262fb 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 66  d char *z;.    f
262fc 6f 72 28 7a 3d 7a 43 68 61 72 53 65 74 2c 20 6e  or(z=zCharSet, n
262fd 43 68 61 72 3d 30 3b 20 2a 7a 3b 20 6e 43 68 61  Char=0; *z; nCha
262fe 72 2b 2b 29 7b 0a 20 20 20 20 20 20 53 51 4c 49  r++){.      SQLI
262ff 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b  TE_SKIP_UTF8(z);
26300 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e  .    }.    if( n
26301 43 68 61 72 3e 30 20 29 7b 0a 20 20 20 20 20 20  Char>0 ){.      
26302 61 7a 43 68 61 72 20 3d 20 63 6f 6e 74 65 78 74  azChar = context
26303 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20  Malloc(context, 
26304 28 28 69 36 34 29 6e 43 68 61 72 29 2a 28 73 69  ((i64)nChar)*(si
26305 7a 65 6f 66 28 63 68 61 72 2a 29 2b 31 29 29 3b  zeof(char*)+1));
26306 0a 20 20 20 20 20 20 69 66 28 20 61 7a 43 68 61  .      if( azCha
26307 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  r==0 ){.        
26308 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
26309 20 20 20 20 20 20 61 4c 65 6e 20 3d 20 28 75 6e        aLen = (un
2630a 73 69 67 6e 65 64 20 63 68 61 72 2a 29 26 61 7a  signed char*)&az
2630b 43 68 61 72 5b 6e 43 68 61 72 5d 3b 0a 20 20 20  Char[nChar];.   
2630c 20 20 20 66 6f 72 28 7a 3d 7a 43 68 61 72 53 65     for(z=zCharSe
2630d 74 2c 20 6e 43 68 61 72 3d 30 3b 20 2a 7a 3b 20  t, nChar=0; *z; 
2630e 6e 43 68 61 72 2b 2b 29 7b 0a 20 20 20 20 20 20  nChar++){.      
2630f 20 20 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d 20    azChar[nChar] 
26310 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
26311 20 2a 29 7a 3b 0a 20 20 20 20 20 20 20 20 53 51   *)z;.        SQ
26312 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a  LITE_SKIP_UTF8(z
26313 29 3b 0a 20 20 20 20 20 20 20 20 61 4c 65 6e 5b  );.        aLen[
26314 6e 43 68 61 72 5d 20 3d 20 28 75 38 29 28 7a 20  nChar] = (u8)(z 
26315 2d 20 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d 29  - azChar[nChar])
26316 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
26317 20 20 7d 0a 20 20 69 66 28 20 6e 43 68 61 72 3e    }.  if( nChar>
26318 30 20 29 7b 0a 20 20 20 20 66 6c 61 67 73 20 3d  0 ){.    flags =
26319 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49   SQLITE_PTR_TO_I
2631a 4e 54 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  NT(sqlite3_user_
2631b 64 61 74 61 28 63 6f 6e 74 65 78 74 29 29 3b 0a  data(context));.
2631c 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20      if( flags & 
2631d 31 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65  1 ){.      while
2631e 28 20 6e 49 6e 3e 30 20 29 7b 0a 20 20 20 20 20  ( nIn>0 ){.     
2631f 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a     int len = 0;.
26320 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
26321 20 69 3c 6e 43 68 61 72 3b 20 69 2b 2b 29 7b 0a   i<nChar; i++){.
26322 20 20 20 20 20 20 20 20 20 20 6c 65 6e 20 3d 20            len = 
26323 61 4c 65 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20  aLen[i];.       
26324 20 20 20 69 66 28 20 6c 65 6e 3c 3d 6e 49 6e 20     if( len<=nIn 
26325 26 26 20 6d 65 6d 63 6d 70 28 7a 49 6e 2c 20 61  && memcmp(zIn, a
26326 7a 43 68 61 72 5b 69 5d 2c 20 6c 65 6e 29 3d 3d  zChar[i], len)==
26327 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  0 ) break;.     
26328 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
26329 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 65 61   i>=nChar ) brea
2632a 6b 3b 0a 20 20 20 20 20 20 20 20 7a 49 6e 20 2b  k;.        zIn +
2632b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 6e  = len;.        n
2632c 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20 20  In -= len;.     
2632d 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
2632e 20 66 6c 61 67 73 20 26 20 32 20 29 7b 0a 20 20   flags & 2 ){.  
2632f 20 20 20 20 77 68 69 6c 65 28 20 6e 49 6e 3e 30      while( nIn>0
26330 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
26331 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  len = 0;.       
26332 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 68 61   for(i=0; i<nCha
26333 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  r; i++){.       
26334 20 20 20 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69 5d     len = aLen[i]
26335 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
26336 6c 65 6e 3c 3d 6e 49 6e 20 26 26 20 6d 65 6d 63  len<=nIn && memc
26337 6d 70 28 26 7a 49 6e 5b 6e 49 6e 2d 6c 65 6e 5d  mp(&zIn[nIn-len]
26338 2c 61 7a 43 68 61 72 5b 69 5d 2c 6c 65 6e 29 3d  ,azChar[i],len)=
26339 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
2633a 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
2633b 28 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 65  ( i>=nChar ) bre
2633c 61 6b 3b 0a 20 20 20 20 20 20 20 20 6e 49 6e 20  ak;.        nIn 
2633d 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 7d 0a  -= len;.      }.
2633e 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 7a 43      }.    if( zC
2633f 68 61 72 53 65 74 20 29 7b 0a 20 20 20 20 20 20  harSet ){.      
26340 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 43  sqlite3_free(azC
26341 68 61 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  har);.    }.  }.
26342 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
26343 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 28  _text(context, (
26344 63 68 61 72 2a 29 7a 49 6e 2c 20 6e 49 6e 2c 20  char*)zIn, nIn, 
26345 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
26346 29 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51  );.}...#ifdef SQ
26347 4c 49 54 45 5f 53 4f 55 4e 44 45 58 0a 2f 2a 0a  LITE_SOUNDEX./*.
26348 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 73  ** Compute the s
26349 6f 75 6e 64 65 78 20 65 6e 63 6f 64 69 6e 67 20  oundex encoding 
2634a 6f 66 20 61 20 77 6f 72 64 2e 0a 2a 2f 0a 73 74  of a word..*/.st
2634b 61 74 69 63 20 76 6f 69 64 20 73 6f 75 6e 64 65  atic void sounde
2634c 78 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  xFunc(.  sqlite3
2634d 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
2634e 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
2634f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
26350 2a 61 72 67 76 0a 29 7b 0a 20 20 63 68 61 72 20  *argv.){.  char 
26351 7a 52 65 73 75 6c 74 5b 38 5d 3b 0a 20 20 63 6f  zResult[8];.  co
26352 6e 73 74 20 75 38 20 2a 7a 49 6e 3b 0a 20 20 69  nst u8 *zIn;.  i
26353 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74 61 74 69  nt i, j;.  stati
26354 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  c const unsigned
26355 20 63 68 61 72 20 69 43 6f 64 65 5b 5d 20 3d 20   char iCode[] = 
26356 7b 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30  {.    0, 0, 0, 0
26357 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
26358 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
26359 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20  0, 0,.    0, 0, 
2635a 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2635b 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
2635c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c   0, 0, 0,.    0,
2635d 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2635e 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2635f 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20  , 0, 0, 0, 0,.  
26360 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c    0, 0, 0, 0, 0,
26361 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
26362 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
26363 2c 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 32  ,.    0, 0, 1, 2
26364 2c 20 33 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c  , 3, 0, 1, 2, 0,
26365 20 30 2c 20 32 2c 20 32 2c 20 34 2c 20 35 2c 20   0, 2, 2, 4, 5, 
26366 35 2c 20 30 2c 0a 20 20 20 20 31 2c 20 32 2c 20  5, 0,.    1, 2, 
26367 36 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 30  6, 2, 3, 0, 1, 0
26368 2c 20 32 2c 20 30 2c 20 32 2c 20 30 2c 20 30 2c  , 2, 0, 2, 0, 0,
26369 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c   0, 0, 0,.    0,
2636a 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20 30 2c 20   0, 1, 2, 3, 0, 
2636b 31 2c 20 32 2c 20 30 2c 20 30 2c 20 32 2c 20 32  1, 2, 0, 0, 2, 2
2636c 2c 20 34 2c 20 35 2c 20 35 2c 20 30 2c 0a 20 20  , 4, 5, 5, 0,.  
2636d 20 20 31 2c 20 32 2c 20 36 2c 20 32 2c 20 33 2c    1, 2, 6, 2, 3,
2636e 20 30 2c 20 31 2c 20 30 2c 20 32 2c 20 30 2c 20   0, 1, 0, 2, 0, 
2636f 32 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  2, 0, 0, 0, 0, 0
26370 2c 0a 20 20 7d 3b 0a 20 20 61 73 73 65 72 74 28  ,.  };.  assert(
26371 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 7a 49   argc==1 );.  zI
26372 6e 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 33  n = (u8*)sqlite3
26373 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
26374 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a 49 6e 3d  [0]);.  if( zIn=
26375 3d 30 20 29 20 7a 49 6e 20 3d 20 28 75 38 2a 29  =0 ) zIn = (u8*)
26376 22 22 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 7a  "";.  for(i=0; z
26377 49 6e 5b 69 5d 20 26 26 20 21 73 71 6c 69 74 65  In[i] && !sqlite
26378 33 49 73 61 6c 70 68 61 28 7a 49 6e 5b 69 5d 29  3Isalpha(zIn[i])
26379 3b 20 69 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 7a  ; i++){}.  if( z
2637a 49 6e 5b 69 5d 20 29 7b 0a 20 20 20 20 75 38 20  In[i] ){.    u8 
2637b 70 72 65 76 63 6f 64 65 20 3d 20 69 43 6f 64 65  prevcode = iCode
2637c 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20  [zIn[i]&0x7f];. 
2637d 20 20 20 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20     zResult[0] = 
2637e 73 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 7a  sqlite3Toupper(z
2637f 49 6e 5b 69 5d 29 3b 0a 20 20 20 20 66 6f 72 28  In[i]);.    for(
26380 6a 3d 31 3b 20 6a 3c 34 20 26 26 20 7a 49 6e 5b  j=1; j<4 && zIn[
26381 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  i]; i++){.      
26382 69 6e 74 20 63 6f 64 65 20 3d 20 69 43 6f 64 65  int code = iCode
26383 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20  [zIn[i]&0x7f];. 
26384 20 20 20 20 20 69 66 28 20 63 6f 64 65 3e 30 20       if( code>0 
26385 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  ){.        if( c
26386 6f 64 65 21 3d 70 72 65 76 63 6f 64 65 20 29 7b  ode!=prevcode ){
26387 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 76 63  .          prevc
26388 6f 64 65 20 3d 20 63 6f 64 65 3b 0a 20 20 20 20  ode = code;.    
26389 20 20 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b        zResult[j+
2638a 2b 5d 20 3d 20 63 6f 64 65 20 2b 20 27 30 27 3b  +] = code + '0';
2638b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2638c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2638d 70 72 65 76 63 6f 64 65 20 3d 20 30 3b 0a 20 20  prevcode = 0;.  
2638e 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2638f 77 68 69 6c 65 28 20 6a 3c 34 20 29 7b 0a 20 20  while( j<4 ){.  
26390 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b 5d      zResult[j++]
26391 20 3d 20 27 30 27 3b 0a 20 20 20 20 7d 0a 20 20   = '0';.    }.  
26392 20 20 7a 52 65 73 75 6c 74 5b 6a 5d 20 3d 20 30    zResult[j] = 0
26393 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
26394 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
26395 74 2c 20 7a 52 65 73 75 6c 74 2c 20 34 2c 20 53  t, zResult, 4, S
26396 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
26397 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
26398 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
26399 78 74 28 63 6f 6e 74 65 78 74 2c 20 22 3f 30 30  xt(context, "?00
2639a 30 22 2c 20 34 2c 20 53 51 4c 49 54 45 5f 53 54  0", 4, SQLITE_ST
2639b 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  ATIC);.  }.}.#en
2639c 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
2639d 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  ITE_OMIT_LOAD_EX
2639e 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 41 20  TENSION./*.** A 
2639f 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 6c 6f  function that lo
263a0 61 64 73 20 61 20 73 68 61 72 65 64 2d 6c 69 62  ads a shared-lib
263a1 72 61 72 79 20 65 78 74 65 6e 73 69 6f 6e 20 74  rary extension t
263a2 68 65 6e 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c  hen returns NULL
263a3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
263a4 20 6c 6f 61 64 45 78 74 28 73 71 6c 69 74 65 33   loadExt(sqlite3
263a5 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
263a6 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
263a7 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
263a8 76 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  v){.  const char
263a9 20 2a 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74   *zFile = (const
263aa 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f   char *)sqlite3_
263ab 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
263ac 30 5d 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  0]);.  const cha
263ad 72 20 2a 7a 50 72 6f 63 3b 0a 20 20 73 71 6c 69  r *zProc;.  sqli
263ae 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65  te3 *db = sqlite
263af 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
263b0 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  dle(context);.  
263b1 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20  char *zErrMsg = 
263b2 30 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d  0;..  if( argc==
263b3 32 20 29 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d  2 ){.    zProc =
263b4 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
263b5 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
263b6 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 7d 65  t(argv[1]);.  }e
263b7 6c 73 65 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d  lse{.    zProc =
263b8 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 46   0;.  }.  if( zF
263b9 69 6c 65 20 26 26 20 73 71 6c 69 74 65 33 5f 6c  ile && sqlite3_l
263ba 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 64 62  oad_extension(db
263bb 2c 20 7a 46 69 6c 65 2c 20 7a 50 72 6f 63 2c 20  , zFile, zProc, 
263bc 26 7a 45 72 72 4d 73 67 29 20 29 7b 0a 20 20 20  &zErrMsg) ){.   
263bd 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
263be 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a  error(context, z
263bf 45 72 72 4d 73 67 2c 20 2d 31 29 3b 0a 20 20 20  ErrMsg, -1);.   
263c0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45   sqlite3_free(zE
263c1 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 23 65  rrMsg);.  }.}.#e
263c2 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  ndif.../*.** An 
263c3 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
263c4 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
263c5 75 72 65 20 68 6f 6c 64 73 20 74 68 65 20 63 6f  ure holds the co
263c6 6e 74 65 78 74 20 6f 66 20 61 0a 2a 2a 20 73 75  ntext of a.** su
263c7 6d 28 29 20 6f 72 20 61 76 67 28 29 20 61 67 67  m() or avg() agg
263c8 72 65 67 61 74 65 20 63 6f 6d 70 75 74 61 74 69  regate computati
263c9 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  on..*/.typedef s
263ca 74 72 75 63 74 20 53 75 6d 43 74 78 20 53 75 6d  truct SumCtx Sum
263cb 43 74 78 3b 0a 73 74 72 75 63 74 20 53 75 6d 43  Ctx;.struct SumC
263cc 74 78 20 7b 0a 20 20 64 6f 75 62 6c 65 20 72 53  tx {.  double rS
263cd 75 6d 3b 20 20 20 20 20 20 2f 2a 20 46 6c 6f 61  um;      /* Floa
263ce 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 6d 20 2a  ting point sum *
263cf 2f 0a 20 20 69 36 34 20 69 53 75 6d 3b 20 20 20  /.  i64 iSum;   
263d0 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72        /* Integer
263d1 20 73 75 6d 20 2a 2f 20 20 20 0a 20 20 69 36 34   sum */   .  i64
263d2 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 2f   cnt;          /
263d3 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  * Number of elem
263d4 65 6e 74 73 20 73 75 6d 6d 65 64 20 2a 2f 0a 20  ents summed */. 
263d5 20 75 38 20 6f 76 65 72 66 6c 6f 77 3b 20 20 20   u8 overflow;   
263d6 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e     /* True if in
263d7 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 20 73  teger overflow s
263d8 65 65 6e 20 2a 2f 0a 20 20 75 38 20 61 70 70 72  een */.  u8 appr
263d9 6f 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ox;        /* Tr
263da 75 65 20 69 66 20 6e 6f 6e 2d 69 6e 74 65 67 65  ue if non-intege
263db 72 20 76 61 6c 75 65 20 77 61 73 20 69 6e 70 75  r value was inpu
263dc 74 20 74 6f 20 74 68 65 20 73 75 6d 20 2a 2f 0a  t to the sum */.
263dd 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e  };../*.** Routin
263de 65 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75  es used to compu
263df 74 65 20 74 68 65 20 73 75 6d 2c 20 61 76 65 72  te the sum, aver
263e0 61 67 65 2c 20 61 6e 64 20 74 6f 74 61 6c 2e 0a  age, and total..
263e1 2a 2a 0a 2a 2a 20 54 68 65 20 53 55 4d 28 29 20  **.** The SUM() 
263e2 66 75 6e 63 74 69 6f 6e 20 66 6f 6c 6c 6f 77 73  function follows
263e3 20 74 68 65 20 28 62 72 6f 6b 65 6e 29 20 53 51   the (broken) SQ
263e4 4c 20 73 74 61 6e 64 61 72 64 20 77 68 69 63 68  L standard which
263e5 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 69   means.** that i
263e6 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69  t returns NULL i
263e7 66 20 69 74 20 73 75 6d 73 20 6f 76 65 72 20 6e  f it sums over n
263e8 6f 20 69 6e 70 75 74 73 2e 20 20 54 4f 54 41 4c  o inputs.  TOTAL
263e9 20 72 65 74 75 72 6e 73 0a 2a 2a 20 30 2e 30 20   returns.** 0.0 
263ea 69 6e 20 74 68 61 74 20 63 61 73 65 2e 20 20 49  in that case.  I
263eb 6e 20 61 64 64 69 74 69 6f 6e 2c 20 54 4f 54 41  n addition, TOTA
263ec 4c 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73  L always returns
263ed 20 61 20 66 6c 6f 61 74 20 77 68 65 72 65 0a 2a   a float where.*
263ee 2a 20 53 55 4d 20 6d 69 67 68 74 20 72 65 74 75  * SUM might retu
263ef 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 69 66  rn an integer if
263f0 20 69 74 20 6e 65 76 65 72 20 65 6e 63 6f 75 6e   it never encoun
263f1 74 65 72 73 20 61 20 66 6c 6f 61 74 69 6e 67 20  ters a floating 
263f2 70 6f 69 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20  point.** value. 
263f3 20 54 4f 54 41 4c 20 6e 65 76 65 72 20 66 61 69   TOTAL never fai
263f4 6c 73 2c 20 62 75 74 20 53 55 4d 20 6d 69 67 68  ls, but SUM migh
263f5 74 20 74 68 72 6f 75 67 68 20 61 6e 20 65 78 63  t through an exc
263f6 65 70 74 69 6f 6e 20 69 66 0a 2a 2a 20 69 74 20  eption if.** it 
263f7 6f 76 65 72 66 6c 6f 77 73 20 61 6e 20 69 6e 74  overflows an int
263f8 65 67 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eger..*/.static 
263f9 76 6f 69 64 20 73 75 6d 53 74 65 70 28 73 71 6c  void sumStep(sql
263fa 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
263fb 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c  ntext, int argc,
263fc 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
263fd 2a 61 72 67 76 29 7b 0a 20 20 53 75 6d 43 74 78  *argv){.  SumCtx
263fe 20 2a 70 3b 0a 20 20 69 6e 74 20 74 79 70 65 3b   *p;.  int type;
263ff 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
26400 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  =1 );.  UNUSED_P
26401 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a  ARAMETER(argc);.
26402 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67    p = sqlite3_ag
26403 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28  gregate_context(
26404 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28  context, sizeof(
26405 2a 70 29 29 3b 0a 20 20 74 79 70 65 20 3d 20 73  *p));.  type = s
26406 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d  qlite3_value_num
26407 65 72 69 63 5f 74 79 70 65 28 61 72 67 76 5b 30  eric_type(argv[0
26408 5d 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 74  ]);.  if( p && t
26409 79 70 65 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  ype!=SQLITE_NULL
2640a 20 29 7b 0a 20 20 20 20 70 2d 3e 63 6e 74 2b 2b   ){.    p->cnt++
2640b 3b 0a 20 20 20 20 69 66 28 20 74 79 70 65 3d 3d  ;.    if( type==
2640c 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 29  SQLITE_INTEGER )
2640d 7b 0a 20 20 20 20 20 20 69 36 34 20 76 20 3d 20  {.      i64 v = 
2640e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
2640f 74 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  t64(argv[0]);.  
26410 20 20 20 20 70 2d 3e 72 53 75 6d 20 2b 3d 20 76      p->rSum += v
26411 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 2d 3e  ;.      if( (p->
26412 61 70 70 72 6f 78 7c 70 2d 3e 6f 76 65 72 66 6c  approx|p->overfl
26413 6f 77 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ow)==0 ){.      
26414 20 20 69 36 34 20 69 4e 65 77 53 75 6d 20 3d 20    i64 iNewSum = 
26415 70 2d 3e 69 53 75 6d 20 2b 20 76 3b 0a 20 20 20  p->iSum + v;.   
26416 20 20 20 20 20 69 6e 74 20 73 31 20 3d 20 28 69       int s1 = (i
26417 6e 74 29 28 70 2d 3e 69 53 75 6d 20 3e 3e 20 28  nt)(p->iSum >> (
26418 73 69 7a 65 6f 66 28 69 36 34 29 2a 38 2d 31 29  sizeof(i64)*8-1)
26419 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 73  );.        int s
2641a 32 20 3d 20 28 69 6e 74 29 28 76 20 20 20 20 20  2 = (int)(v     
2641b 20 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36 34    >> (sizeof(i64
2641c 29 2a 38 2d 31 29 29 3b 0a 20 20 20 20 20 20 20  )*8-1));.       
2641d 20 69 6e 74 20 73 33 20 3d 20 28 69 6e 74 29 28   int s3 = (int)(
2641e 69 4e 65 77 53 75 6d 20 3e 3e 20 28 73 69 7a 65  iNewSum >> (size
2641f 6f 66 28 69 36 34 29 2a 38 2d 31 29 29 3b 0a 20  of(i64)*8-1));. 
26420 20 20 20 20 20 20 20 70 2d 3e 6f 76 65 72 66 6c         p->overfl
26421 6f 77 20 3d 20 28 28 73 31 26 73 32 26 7e 73 33  ow = ((s1&s2&~s3
26422 29 20 7c 20 28 7e 73 31 26 7e 73 32 26 73 33 29  ) | (~s1&~s2&s3)
26423 29 3f 31 3a 30 3b 0a 20 20 20 20 20 20 20 20 70  )?1:0;.        p
26424 2d 3e 69 53 75 6d 20 3d 20 69 4e 65 77 53 75 6d  ->iSum = iNewSum
26425 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
26426 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d 3e 72 53  lse{.      p->rS
26427 75 6d 20 2b 3d 20 73 71 6c 69 74 65 33 5f 76 61  um += sqlite3_va
26428 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b  lue_double(argv[
26429 30 5d 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 70  0]);.      p->ap
2642a 70 72 6f 78 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  prox = 1;.    }.
2642b 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69    }.}.static voi
2642c 64 20 73 75 6d 46 69 6e 61 6c 69 7a 65 28 73 71  d sumFinalize(sq
2642d 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
2642e 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d 43 74  ontext){.  SumCt
2642f 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69  x *p;.  p = sqli
26430 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
26431 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30  ntext(context, 0
26432 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d  );.  if( p && p-
26433 3e 63 6e 74 3e 30 20 29 7b 0a 20 20 20 20 69 66  >cnt>0 ){.    if
26434 28 20 70 2d 3e 6f 76 65 72 66 6c 6f 77 20 29 7b  ( p->overflow ){
26435 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
26436 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74  esult_error(cont
26437 65 78 74 2c 22 69 6e 74 65 67 65 72 20 6f 76 65  ext,"integer ove
26438 72 66 6c 6f 77 22 2c 2d 31 29 3b 0a 20 20 20 20  rflow",-1);.    
26439 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 61 70 70  }else if( p->app
2643a 72 6f 78 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  rox ){.      sql
2643b 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
2643c 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 2d 3e 72  le(context, p->r
2643d 53 75 6d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Sum);.    }else{
2643e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
2643f 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74  esult_int64(cont
26440 65 78 74 2c 20 70 2d 3e 69 53 75 6d 29 3b 0a 20  ext, p->iSum);. 
26441 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69     }.  }.}.stati
26442 63 20 76 6f 69 64 20 61 76 67 46 69 6e 61 6c 69  c void avgFinali
26443 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ze(sqlite3_conte
26444 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20  xt *context){.  
26445 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d  SumCtx *p;.  p =
26446 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61   sqlite3_aggrega
26447 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65  te_context(conte
26448 78 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 20  xt, 0);.  if( p 
26449 26 26 20 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a 20  && p->cnt>0 ){. 
2644a 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
2644b 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74  t_double(context
2644c 2c 20 70 2d 3e 72 53 75 6d 2f 28 64 6f 75 62 6c  , p->rSum/(doubl
2644d 65 29 70 2d 3e 63 6e 74 29 3b 0a 20 20 7d 0a 7d  e)p->cnt);.  }.}
2644e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 6f 74  .static void tot
2644f 61 6c 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74  alFinalize(sqlit
26450 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
26451 65 78 74 29 7b 0a 20 20 53 75 6d 43 74 78 20 2a  ext){.  SumCtx *
26452 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  p;.  p = sqlite3
26453 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
26454 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b 0a  xt(context, 0);.
26455 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49    /* (double)0 I
26456 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45  n case of SQLITE
26457 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
26458 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 73 71 6c  OINT... */.  sql
26459 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
2645a 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 20 3f 20  le(context, p ? 
2645b 70 2d 3e 72 53 75 6d 20 3a 20 28 64 6f 75 62 6c  p->rSum : (doubl
2645c 65 29 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  e)0);.}../*.** T
2645d 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
2645e 75 63 74 75 72 65 20 6b 65 65 70 73 20 74 72 61  ucture keeps tra
2645f 63 6b 20 6f 66 20 73 74 61 74 65 20 69 6e 66 6f  ck of state info
26460 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 0a  rmation for the.
26461 2a 2a 20 63 6f 75 6e 74 28 29 20 61 67 67 72 65  ** count() aggre
26462 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  gate function..*
26463 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
26464 20 43 6f 75 6e 74 43 74 78 20 43 6f 75 6e 74 43   CountCtx CountC
26465 74 78 3b 0a 73 74 72 75 63 74 20 43 6f 75 6e 74  tx;.struct Count
26466 43 74 78 20 7b 0a 20 20 69 36 34 20 6e 3b 0a 7d  Ctx {.  i64 n;.}
26467 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65  ;../*.** Routine
26468 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  s to implement t
26469 68 65 20 63 6f 75 6e 74 28 29 20 61 67 67 72 65  he count() aggre
2646a 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  gate function..*
2646b 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  /.static void co
2646c 75 6e 74 53 74 65 70 28 73 71 6c 69 74 65 33 5f  untStep(sqlite3_
2646d 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
2646e 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
2646f 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
26470 29 7b 0a 20 20 43 6f 75 6e 74 43 74 78 20 2a 70  ){.  CountCtx *p
26471 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f  ;.  p = sqlite3_
26472 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
26473 74 28 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f  t(context, sizeo
26474 66 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 28 61  f(*p));.  if( (a
26475 72 67 63 3d 3d 30 20 7c 7c 20 53 51 4c 49 54 45  rgc==0 || SQLITE
26476 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 33 5f 76  _NULL!=sqlite3_v
26477 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30  alue_type(argv[0
26478 5d 29 29 20 26 26 20 70 20 29 7b 0a 20 20 20 20  ])) && p ){.    
26479 70 2d 3e 6e 2b 2b 3b 0a 20 20 7d 0a 0a 23 69 66  p->n++;.  }..#if
2647a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2647b 5f 44 45 50 52 45 43 41 54 45 44 0a 20 20 2f 2a  _DEPRECATED.  /*
2647c 20 54 68 65 20 73 71 6c 69 74 65 33 5f 61 67 67   The sqlite3_agg
2647d 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 29 20 66  regate_count() f
2647e 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 70 72 65  unction is depre
2647f 63 61 74 65 64 2e 20 20 42 75 74 20 6a 75 73 74  cated.  But just
26480 20 74 6f 20 6d 61 6b 65 0a 20 20 2a 2a 20 73 75   to make.  ** su
26481 72 65 20 69 74 20 73 74 69 6c 6c 20 6f 70 65 72  re it still oper
26482 61 74 65 73 20 63 6f 72 72 65 63 74 6c 79 2c 20  ates correctly, 
26483 76 65 72 69 66 79 20 74 68 61 74 20 69 74 73 20  verify that its 
26484 63 6f 75 6e 74 20 61 67 72 65 65 73 20 77 69 74  count agrees wit
26485 68 20 6f 75 72 20 0a 20 20 2a 2a 20 69 6e 74 65  h our .  ** inte
26486 72 6e 61 6c 20 63 6f 75 6e 74 20 77 68 65 6e 20  rnal count when 
26487 75 73 69 6e 67 20 63 6f 75 6e 74 28 2a 29 20 61  using count(*) a
26488 6e 64 20 77 68 65 6e 20 74 68 65 20 74 6f 74 61  nd when the tota
26489 6c 20 63 6f 75 6e 74 20 63 61 6e 20 62 65 0a 20  l count can be. 
2648a 20 2a 2a 20 65 78 70 72 65 73 73 65 64 20 61 73   ** expressed as
2648b 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65   a 32-bit intege
2648c 72 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  r. */.  assert( 
2648d 61 72 67 63 3d 3d 31 20 7c 7c 20 70 3d 3d 30 20  argc==1 || p==0 
2648e 7c 7c 20 70 2d 3e 6e 3e 30 78 37 66 66 66 66 66  || p->n>0x7fffff
2648f 66 66 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20  ff.          || 
26490 70 2d 3e 6e 3d 3d 73 71 6c 69 74 65 33 5f 61 67  p->n==sqlite3_ag
26491 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 63 6f  gregate_count(co
26492 6e 74 65 78 74 29 20 29 3b 0a 23 65 6e 64 69 66  ntext) );.#endif
26493 0a 7d 20 20 20 0a 73 74 61 74 69 63 20 76 6f 69  .}   .static voi
26494 64 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 28  d countFinalize(
26495 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
26496 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 43 6f 75  *context){.  Cou
26497 6e 74 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20  ntCtx *p;.  p = 
26498 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
26499 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78  e_context(contex
2649a 74 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  t, 0);.  sqlite3
2649b 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f  _result_int64(co
2649c 6e 74 65 78 74 2c 20 70 20 3f 20 70 2d 3e 6e 20  ntext, p ? p->n 
2649d 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  : 0);.}../*.** R
2649e 6f 75 74 69 6e 65 73 20 74 6f 20 69 6d 70 6c 65  outines to imple
2649f 6d 65 6e 74 20 6d 69 6e 28 29 20 61 6e 64 20 6d  ment min() and m
264a0 61 78 28 29 20 61 67 67 72 65 67 61 74 65 20 66  ax() aggregate f
264a1 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61  unctions..*/.sta
264a2 74 69 63 20 76 6f 69 64 20 6d 69 6e 6d 61 78 53  tic void minmaxS
264a3 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  tep(.  sqlite3_c
264a4 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
264a5 20 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c   .  int NotUsed,
264a6 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   .  sqlite3_valu
264a7 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 4d 65  e **argv.){.  Me
264a8 6d 20 2a 70 41 72 67 20 20 3d 20 28 4d 65 6d 20  m *pArg  = (Mem 
264a9 2a 29 61 72 67 76 5b 30 5d 3b 0a 20 20 4d 65 6d  *)argv[0];.  Mem
264aa 20 2a 70 42 65 73 74 3b 0a 20 20 55 4e 55 53 45   *pBest;.  UNUSE
264ab 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
264ac 73 65 64 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c  sed);..  if( sql
264ad 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
264ae 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45  argv[0])==SQLITE
264af 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a  _NULL ) return;.
264b0 20 20 70 42 65 73 74 20 3d 20 28 4d 65 6d 20 2a    pBest = (Mem *
264b1 29 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61  )sqlite3_aggrega
264b2 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65  te_context(conte
264b3 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 42 65 73  xt, sizeof(*pBes
264b4 74 29 29 3b 0a 20 20 69 66 28 20 21 70 42 65 73  t));.  if( !pBes
264b5 74 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69  t ) return;..  i
264b6 66 28 20 70 42 65 73 74 2d 3e 66 6c 61 67 73 20  f( pBest->flags 
264b7 29 7b 0a 20 20 20 20 69 6e 74 20 6d 61 78 3b 0a  ){.    int max;.
264b8 20 20 20 20 69 6e 74 20 63 6d 70 3b 0a 20 20 20      int cmp;.   
264b9 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20   CollSeq *pColl 
264ba 3d 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63  = sqlite3GetFunc
264bb 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65 78 74 29  CollSeq(context)
264bc 3b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 73 74  ;.    /* This st
264bd 65 70 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  ep function is u
264be 73 65 64 20 66 6f 72 20 62 6f 74 68 20 74 68 65  sed for both the
264bf 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28 29   min() and max()
264c0 20 61 67 67 72 65 67 61 74 65 73 2c 0a 20 20 20   aggregates,.   
264c1 20 2a 2a 20 74 68 65 20 6f 6e 6c 79 20 64 69 66   ** the only dif
264c2 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20  ference between 
264c3 74 68 65 20 74 77 6f 20 62 65 69 6e 67 20 74 68  the two being th
264c4 61 74 20 74 68 65 20 73 65 6e 73 65 20 6f 66 20  at the sense of 
264c5 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 61  the.    ** compa
264c6 72 69 73 6f 6e 20 69 73 20 69 6e 76 65 72 74 65  rison is inverte
264c7 64 2e 20 46 6f 72 20 74 68 65 20 6d 61 78 28 29  d. For the max()
264c8 20 61 67 67 72 65 67 61 74 65 2c 20 74 68 65 0a   aggregate, the.
264c9 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 75      ** sqlite3_u
264ca 73 65 72 5f 64 61 74 61 28 29 20 66 75 6e 63 74  ser_data() funct
264cb 69 6f 6e 20 72 65 74 75 72 6e 73 20 28 76 6f 69  ion returns (voi
264cc 64 20 2a 29 2d 31 2e 20 46 6f 72 20 6d 69 6e 28  d *)-1. For min(
264cd 29 20 69 74 0a 20 20 20 20 2a 2a 20 72 65 74 75  ) it.    ** retu
264ce 72 6e 73 20 28 76 6f 69 64 20 2a 29 64 62 2c 20  rns (void *)db, 
264cf 77 68 65 72 65 20 64 62 20 69 73 20 74 68 65 20  where db is the 
264d0 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62 61 73  sqlite3* databas
264d1 65 20 70 6f 69 6e 74 65 72 2e 0a 20 20 20 20 2a  e pointer..    *
264d2 2a 20 54 68 65 72 65 66 6f 72 65 20 74 68 65 20  * Therefore the 
264d3 6e 65 78 74 20 73 74 61 74 65 6d 65 6e 74 20 73  next statement s
264d4 65 74 73 20 76 61 72 69 61 62 6c 65 20 27 6d 61  ets variable 'ma
264d5 78 27 20 74 6f 20 31 20 66 6f 72 20 74 68 65 20  x' to 1 for the 
264d6 6d 61 78 28 29 0a 20 20 20 20 2a 2a 20 61 67 67  max().    ** agg
264d7 72 65 67 61 74 65 2c 20 6f 72 20 30 20 66 6f 72  regate, or 0 for
264d8 20 6d 69 6e 28 29 2e 0a 20 20 20 20 2a 2f 0a 20   min()..    */. 
264d9 20 20 20 6d 61 78 20 3d 20 73 71 6c 69 74 65 33     max = sqlite3
264da 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65  _user_data(conte
264db 78 74 29 21 3d 30 3b 0a 20 20 20 20 63 6d 70 20  xt)!=0;.    cmp 
264dc 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70  = sqlite3MemComp
264dd 61 72 65 28 70 42 65 73 74 2c 20 70 41 72 67 2c  are(pBest, pArg,
264de 20 70 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28   pColl);.    if(
264df 20 28 6d 61 78 20 26 26 20 63 6d 70 3c 30 29 20   (max && cmp<0) 
264e0 7c 7c 20 28 21 6d 61 78 20 26 26 20 63 6d 70 3e  || (!max && cmp>
264e1 30 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  0) ){.      sqli
264e2 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 70  te3VdbeMemCopy(p
264e3 42 65 73 74 2c 20 70 41 72 67 29 3b 0a 20 20 20  Best, pArg);.   
264e4 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
264e5 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f  sqlite3VdbeMemCo
264e6 70 79 28 70 42 65 73 74 2c 20 70 41 72 67 29 3b  py(pBest, pArg);
264e7 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f  .  }.}.static vo
264e8 69 64 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a  id minMaxFinaliz
264e9 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  e(sqlite3_contex
264ea 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 73  t *context){.  s
264eb 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 52  qlite3_value *pR
264ec 65 73 3b 0a 20 20 70 52 65 73 20 3d 20 28 73 71  es;.  pRes = (sq
264ed 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 29 73 71  lite3_value *)sq
264ee 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
264ef 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  context(context,
264f0 20 30 29 3b 0a 20 20 69 66 28 20 70 52 65 73 20   0);.  if( pRes 
264f1 29 7b 0a 20 20 20 20 69 66 28 20 41 4c 57 41 59  ){.    if( ALWAY
264f2 53 28 70 52 65 73 2d 3e 66 6c 61 67 73 29 20 29  S(pRes->flags) )
264f3 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
264f4 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e  result_value(con
264f5 74 65 78 74 2c 20 70 52 65 73 29 3b 0a 20 20 20  text, pRes);.   
264f6 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
264f7 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 52 65  beMemRelease(pRe
264f8 73 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  s);.  }.}../*.**
264f9 20 67 72 6f 75 70 5f 63 6f 6e 63 61 74 28 45 58   group_concat(EX
264fa 50 52 2c 20 3f 53 45 50 41 52 41 54 4f 52 3f 29  PR, ?SEPARATOR?)
264fb 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
264fc 67 72 6f 75 70 43 6f 6e 63 61 74 53 74 65 70 28  groupConcatStep(
264fd 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
264fe 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
264ff 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
26500 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
26501 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
26502 2a 7a 56 61 6c 3b 0a 20 20 53 74 72 41 63 63 75  *zVal;.  StrAccu
26503 6d 20 2a 70 41 63 63 75 6d 3b 0a 20 20 63 6f 6e  m *pAccum;.  con
26504 73 74 20 63 68 61 72 20 2a 7a 53 65 70 3b 0a 20  st char *zSep;. 
26505 20 69 6e 74 20 6e 56 61 6c 2c 20 6e 53 65 70 3b   int nVal, nSep;
26506 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
26507 3d 31 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b  =1 || argc==2 );
26508 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76  .  if( sqlite3_v
26509 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30  alue_type(argv[0
2650a 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  ])==SQLITE_NULL 
2650b 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 41 63 63  ) return;.  pAcc
2650c 75 6d 20 3d 20 28 53 74 72 41 63 63 75 6d 2a 29  um = (StrAccum*)
2650d 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
2650e 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78  e_context(contex
2650f 74 2c 20 73 69 7a 65 6f 66 28 2a 70 41 63 63 75  t, sizeof(*pAccu
26510 6d 29 29 3b 0a 0a 20 20 69 66 28 20 70 41 63 63  m));..  if( pAcc
26511 75 6d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  um ){.    sqlite
26512 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  3 *db = sqlite3_
26513 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
26514 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  e(context);.    
26515 69 6e 74 20 66 69 72 73 74 54 65 72 6d 20 3d 20  int firstTerm = 
26516 70 41 63 63 75 6d 2d 3e 75 73 65 4d 61 6c 6c 6f  pAccum->useMallo
26517 63 3d 3d 30 3b 0a 20 20 20 20 70 41 63 63 75 6d  c==0;.    pAccum
26518 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 31 3b  ->useMalloc = 1;
26519 0a 20 20 20 20 70 41 63 63 75 6d 2d 3e 6d 78 41  .    pAccum->mxA
2651a 6c 6c 6f 63 20 3d 20 64 62 2d 3e 61 4c 69 6d 69  lloc = db->aLimi
2651b 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
2651c 45 4e 47 54 48 5d 3b 0a 20 20 20 20 69 66 28 20  ENGTH];.    if( 
2651d 21 66 69 72 73 74 54 65 72 6d 20 29 7b 0a 20 20  !firstTerm ){.  
2651e 20 20 20 20 69 66 28 20 61 72 67 63 3d 3d 32 20      if( argc==2 
2651f 29 7b 0a 20 20 20 20 20 20 20 20 7a 53 65 70 20  ){.        zSep 
26520 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33  = (char*)sqlite3
26521 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
26522 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20 20 6e 53  [1]);.        nS
26523 65 70 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  ep = sqlite3_val
26524 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 31 5d  ue_bytes(argv[1]
26525 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
26526 20 20 20 20 20 20 20 20 7a 53 65 70 20 3d 20 22          zSep = "
26527 2c 22 3b 0a 20 20 20 20 20 20 20 20 6e 53 65 70  ,";.        nSep
26528 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
26529 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
2652a 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d  cumAppend(pAccum
2652b 2c 20 7a 53 65 70 2c 20 6e 53 65 70 29 3b 0a 20  , zSep, nSep);. 
2652c 20 20 20 7d 0a 20 20 20 20 7a 56 61 6c 20 3d 20     }.    zVal = 
2652d 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76  (char*)sqlite3_v
2652e 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
2652f 5d 29 3b 0a 20 20 20 20 6e 56 61 6c 20 3d 20 73  ]);.    nVal = s
26530 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
26531 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  es(argv[0]);.   
26532 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
26533 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a  Append(pAccum, z
26534 56 61 6c 2c 20 6e 56 61 6c 29 3b 0a 20 20 7d 0a  Val, nVal);.  }.
26535 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67 72  }.static void gr
26536 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c 69 7a  oupConcatFinaliz
26537 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  e(sqlite3_contex
26538 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53  t *context){.  S
26539 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 3b  trAccum *pAccum;
2653a 0a 20 20 70 41 63 63 75 6d 20 3d 20 73 71 6c 69  .  pAccum = sqli
2653b 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
2653c 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30  ntext(context, 0
2653d 29 3b 0a 20 20 69 66 28 20 70 41 63 63 75 6d 20  );.  if( pAccum 
2653e 29 7b 0a 20 20 20 20 69 66 28 20 70 41 63 63 75  ){.    if( pAccu
2653f 6d 2d 3e 74 6f 6f 42 69 67 20 29 7b 0a 20 20 20  m->tooBig ){.   
26540 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
26541 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63  t_error_toobig(c
26542 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 65 6c  ontext);.    }el
26543 73 65 20 69 66 28 20 70 41 63 63 75 6d 2d 3e 6d  se if( pAccum->m
26544 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
26545 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
26546 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28  ult_error_nomem(
26547 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 65  context);.    }e
26548 6c 73 65 7b 20 20 20 20 0a 20 20 20 20 20 20 73  lse{    .      s
26549 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
2654a 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69  xt(context, sqli
2654b 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73  te3StrAccumFinis
2654c 68 28 70 41 63 63 75 6d 29 2c 20 2d 31 2c 20 0a  h(pAccum), -1, .
2654d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2654e 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2654f 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20  3_free);.    }. 
26550 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
26551 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74   function regist
26552 65 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 65 20  ered all of the 
26553 61 62 6f 76 65 20 43 20 66 75 6e 63 74 69 6f 6e  above C function
26554 73 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63  s as SQL.** func
26555 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 68 6f  tions.  This sho
26556 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79 20  uld be the only 
26557 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20  routine in this 
26558 66 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 78 74  file with.** ext
26559 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a  ernal linkage..*
2655a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2655b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67   void sqlite3Reg
2655c 69 73 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63  isterBuiltinFunc
2655d 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64  tions(sqlite3 *d
2655e 62 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  b){.#ifndef SQLI
2655f 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42  TE_OMIT_ALTERTAB
26560 4c 45 0a 20 20 73 71 6c 69 74 65 33 41 6c 74 65  LE.  sqlite3Alte
26561 72 46 75 6e 63 74 69 6f 6e 73 28 64 62 29 3b 0a  rFunctions(db);.
26562 23 65 6e 64 69 66 0a 20 20 69 66 28 20 21 64 62  #endif.  if( !db
26563 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
26564 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73  {.    int rc = s
26565 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f  qlite3_overload_
26566 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 4d 41  function(db, "MA
26567 54 43 48 22 2c 20 32 29 3b 0a 20 20 20 20 61 73  TCH", 2);.    as
26568 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
26569 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51  _NOMEM || rc==SQ
2656a 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69  LITE_OK );.    i
2656b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
2656c 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  MEM ){.      db-
2656d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
2656e 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  1;.    }.  }.}..
2656f 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 4c 49  /*.** Set the LI
26570 4b 45 4f 50 54 20 66 6c 61 67 20 6f 6e 20 74 68  KEOPT flag on th
26571 65 20 32 2d 61 72 67 75 6d 65 6e 74 20 66 75 6e  e 2-argument fun
26572 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 67  ction with the g
26573 69 76 65 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a 73 74  iven name..*/.st
26574 61 74 69 63 20 76 6f 69 64 20 73 65 74 4c 69 6b  atic void setLik
26575 65 4f 70 74 46 6c 61 67 28 73 71 6c 69 74 65 33  eOptFlag(sqlite3
26576 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *db, const char
26577 20 2a 7a 4e 61 6d 65 2c 20 75 38 20 66 6c 61 67   *zName, u8 flag
26578 56 61 6c 29 7b 0a 20 20 46 75 6e 63 44 65 66 20  Val){.  FuncDef 
26579 2a 70 44 65 66 3b 0a 20 20 70 44 65 66 20 3d 20  *pDef;.  pDef = 
2657a 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74  sqlite3FindFunct
2657b 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 73  ion(db, zName, s
2657c 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
2657d 4e 61 6d 65 29 2c 0a 20 20 20 20 20 20 20 20 20  Name),.         
2657e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2657f 20 20 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54      2, SQLITE_UT
26580 46 38 2c 20 30 29 3b 0a 20 20 69 66 28 20 41 4c  F8, 0);.  if( AL
26581 57 41 59 53 28 70 44 65 66 29 20 29 7b 0a 20 20  WAYS(pDef) ){.  
26582 20 20 70 44 65 66 2d 3e 66 6c 61 67 73 20 3d 20    pDef->flags = 
26583 66 6c 61 67 56 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a  flagVal;.  }.}..
26584 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 74  /*.** Register t
26585 68 65 20 62 75 69 6c 74 2d 69 6e 20 4c 49 4b 45  he built-in LIKE
26586 20 61 6e 64 20 47 4c 4f 42 20 66 75 6e 63 74 69   and GLOB functi
26587 6f 6e 73 2e 20 20 54 68 65 20 63 61 73 65 53 65  ons.  The caseSe
26588 6e 73 69 74 69 76 65 0a 2a 2a 20 70 61 72 61 6d  nsitive.** param
26589 65 74 65 72 20 64 65 74 65 72 6d 69 6e 65 73 20  eter determines 
2658a 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
2658b 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72  he LIKE operator
2658c 20 69 73 20 63 61 73 65 0a 2a 2a 20 73 65 6e 73   is case.** sens
2658d 69 74 69 76 65 2e 20 20 47 4c 4f 42 20 69 73 20  itive.  GLOB is 
2658e 61 6c 77 61 79 73 20 63 61 73 65 20 73 65 6e 73  always case sens
2658f 69 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  itive..*/.SQLITE
26590 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
26591 6c 69 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b  lite3RegisterLik
26592 65 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74  eFunctions(sqlit
26593 65 33 20 2a 64 62 2c 20 69 6e 74 20 63 61 73 65  e3 *db, int case
26594 53 65 6e 73 69 74 69 76 65 29 7b 0a 20 20 73 74  Sensitive){.  st
26595 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f  ruct compareInfo
26596 20 2a 70 49 6e 66 6f 3b 0a 20 20 69 66 28 20 63   *pInfo;.  if( c
26597 61 73 65 53 65 6e 73 69 74 69 76 65 20 29 7b 0a  aseSensitive ){.
26598 20 20 20 20 70 49 6e 66 6f 20 3d 20 28 73 74 72      pInfo = (str
26599 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 2a  uct compareInfo*
2659a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 3b 0a 20  )&likeInfoAlt;. 
2659b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 66   }else{.    pInf
2659c 6f 20 3d 20 28 73 74 72 75 63 74 20 63 6f 6d 70  o = (struct comp
2659d 61 72 65 49 6e 66 6f 2a 29 26 6c 69 6b 65 49 6e  areInfo*)&likeIn
2659e 66 6f 4e 6f 72 6d 3b 0a 20 20 7d 0a 20 20 73 71  foNorm;.  }.  sq
2659f 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28  lite3CreateFunc(
265a0 64 62 2c 20 22 6c 69 6b 65 22 2c 20 32 2c 20 53  db, "like", 2, S
265a1 51 4c 49 54 45 5f 41 4e 59 2c 20 70 49 6e 66 6f  QLITE_ANY, pInfo
265a2 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30  , likeFunc, 0, 0
265a3 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61  );.  sqlite3Crea
265a4 74 65 46 75 6e 63 28 64 62 2c 20 22 6c 69 6b 65  teFunc(db, "like
265a5 22 2c 20 33 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ", 3, SQLITE_ANY
265a6 2c 20 70 49 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e  , pInfo, likeFun
265a7 63 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69  c, 0, 0);.  sqli
265a8 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62  te3CreateFunc(db
265a9 2c 20 22 67 6c 6f 62 22 2c 20 32 2c 20 53 51 4c  , "glob", 2, SQL
265aa 49 54 45 5f 41 4e 59 2c 20 0a 20 20 20 20 20 20  ITE_ANY, .      
265ab 28 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49  (struct compareI
265ac 6e 66 6f 2a 29 26 67 6c 6f 62 49 6e 66 6f 2c 20  nfo*)&globInfo, 
265ad 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 30 29 3b 0a  likeFunc, 0,0);.
265ae 20 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61 67    setLikeOptFlag
265af 28 64 62 2c 20 22 67 6c 6f 62 22 2c 20 53 51 4c  (db, "glob", SQL
265b0 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c 20  ITE_FUNC_LIKE | 
265b1 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45  SQLITE_FUNC_CASE
265b2 29 3b 0a 20 20 73 65 74 4c 69 6b 65 4f 70 74 46  );.  setLikeOptF
265b3 6c 61 67 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20  lag(db, "like", 
265b4 0a 20 20 20 20 20 20 63 61 73 65 53 65 6e 73 69  .      caseSensi
265b5 74 69 76 65 20 3f 20 28 53 51 4c 49 54 45 5f 46  tive ? (SQLITE_F
265b6 55 4e 43 5f 4c 49 4b 45 20 7c 20 53 51 4c 49 54  UNC_LIKE | SQLIT
265b7 45 5f 46 55 4e 43 5f 43 41 53 45 29 20 3a 20 53  E_FUNC_CASE) : S
265b8 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29  QLITE_FUNC_LIKE)
265b9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72  ;.}../*.** pExpr
265ba 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 78   points to an ex
265bb 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20 69  pression which i
265bc 6d 70 6c 65 6d 65 6e 74 73 20 61 20 66 75 6e 63  mplements a func
265bd 74 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 69 74 20  tion.  If.** it 
265be 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 74  is appropriate t
265bf 6f 20 61 70 70 6c 79 20 74 68 65 20 4c 49 4b 45  o apply the LIKE
265c0 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 74 6f   optimization to
265c1 20 74 68 61 74 20 66 75 6e 63 74 69 6f 6e 0a 2a   that function.*
265c2 2a 20 74 68 65 6e 20 73 65 74 20 61 57 63 5b 30  * then set aWc[0
265c3 5d 20 74 68 72 6f 75 67 68 20 61 57 63 5b 32 5d  ] through aWc[2]
265c4 20 74 6f 20 74 68 65 20 77 69 6c 64 63 61 72 64   to the wildcard
265c5 20 63 68 61 72 61 63 74 65 72 73 20 61 6e 64 0a   characters and.
265c6 2a 2a 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20  ** return TRUE. 
265c7 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   If the function
265c8 20 69 73 20 6e 6f 74 20 61 20 4c 49 4b 45 2d 73   is not a LIKE-s
265c9 74 79 6c 65 20 66 75 6e 63 74 69 6f 6e 20 74 68  tyle function th
265ca 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20 46 41 4c  en.** return FAL
265cb 53 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  SE..*/.SQLITE_PR
265cc 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
265cd 33 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28  3IsLikeFunction(
265ce 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70  sqlite3 *db, Exp
265cf 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 2a 70  r *pExpr, int *p
265d0 49 73 4e 6f 63 61 73 65 2c 20 63 68 61 72 20 2a  IsNocase, char *
265d1 61 57 63 29 7b 0a 20 20 46 75 6e 63 44 65 66 20  aWc){.  FuncDef 
265d2 2a 70 44 65 66 3b 0a 20 20 69 66 28 20 70 45 78  *pDef;.  if( pEx
265d3 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46 55 4e 43 54  pr->op!=TK_FUNCT
265d4 49 4f 4e 20 0a 20 20 20 7c 7c 20 21 70 45 78 70  ION .   || !pExp
265d5 72 2d 3e 78 2e 70 4c 69 73 74 20 0a 20 20 20 7c  r->x.pList .   |
265d6 7c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  | pExpr->x.pList
265d7 2d 3e 6e 45 78 70 72 21 3d 32 0a 20 20 29 7b 0a  ->nExpr!=2.  ){.
265d8 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
265d9 7d 0a 20 20 61 73 73 65 72 74 28 20 21 45 78 70  }.  assert( !Exp
265da 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
265db 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
265dc 29 20 29 3b 0a 20 20 70 44 65 66 20 3d 20 73 71  ) );.  pDef = sq
265dd 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f  lite3FindFunctio
265de 6e 28 64 62 2c 20 70 45 78 70 72 2d 3e 75 2e 7a  n(db, pExpr->u.z
265df 54 6f 6b 65 6e 2c 20 0a 20 20 20 20 20 20 20 20  Token, .        
265e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
265e1 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 6c       sqlite3Strl
265e2 65 6e 33 30 28 70 45 78 70 72 2d 3e 75 2e 7a 54  en30(pExpr->u.zT
265e3 6f 6b 65 6e 29 2c 0a 20 20 20 20 20 20 20 20 20  oken),.         
265e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
265e5 20 20 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54      2, SQLITE_UT
265e6 46 38 2c 20 30 29 3b 0a 20 20 69 66 28 20 4e 45  F8, 0);.  if( NE
265e7 56 45 52 28 70 44 65 66 3d 3d 30 29 20 7c 7c 20  VER(pDef==0) || 
265e8 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53  (pDef->flags & S
265e9 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29  QLITE_FUNC_LIKE)
265ea 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
265eb 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  n 0;.  }..  /* T
265ec 68 65 20 6d 65 6d 63 70 79 28 29 20 73 74 61 74  he memcpy() stat
265ed 65 6d 65 6e 74 20 61 73 73 75 6d 65 73 20 74 68  ement assumes th
265ee 61 74 20 74 68 65 20 77 69 6c 64 63 61 72 64 20  at the wildcard 
265ef 63 68 61 72 61 63 74 65 72 73 20 61 72 65 0a 20  characters are. 
265f0 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 68   ** the first th
265f1 72 65 65 20 73 74 61 74 65 6d 65 6e 74 73 20 69  ree statements i
265f2 6e 20 74 68 65 20 63 6f 6d 70 61 72 65 49 6e 66  n the compareInf
265f3 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  o structure.  Th
265f4 65 0a 20 20 2a 2a 20 61 73 73 65 72 74 73 28 29  e.  ** asserts()
265f5 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 76 65 72   that follow ver
265f6 69 66 79 20 74 68 61 74 20 61 73 73 75 6d 70 74  ify that assumpt
265f7 69 6f 6e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 63 70  ion.  */.  memcp
265f8 79 28 61 57 63 2c 20 70 44 65 66 2d 3e 70 55 73  y(aWc, pDef->pUs
265f9 65 72 44 61 74 61 2c 20 33 29 3b 0a 20 20 61 73  erData, 3);.  as
265fa 73 65 72 74 28 20 28 63 68 61 72 2a 29 26 6c 69  sert( (char*)&li
265fb 6b 65 49 6e 66 6f 41 6c 74 20 3d 3d 20 28 63 68  keInfoAlt == (ch
265fc 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74  ar*)&likeInfoAlt
265fd 2e 6d 61 74 63 68 41 6c 6c 20 29 3b 0a 20 20 61  .matchAll );.  a
265fe 73 73 65 72 74 28 20 26 28 28 63 68 61 72 2a 29  ssert( &((char*)
265ff 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 29 5b 31 5d  &likeInfoAlt)[1]
26600 20 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b 65   == (char*)&like
26601 49 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 4f 6e 65  InfoAlt.matchOne
26602 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 26 28   );.  assert( &(
26603 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f  (char*)&likeInfo
26604 41 6c 74 29 5b 32 5d 20 3d 3d 20 28 63 68 61 72  Alt)[2] == (char
26605 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d  *)&likeInfoAlt.m
26606 61 74 63 68 53 65 74 20 29 3b 0a 20 20 2a 70 49  atchSet );.  *pI
26607 73 4e 6f 63 61 73 65 20 3d 20 28 70 44 65 66 2d  sNocase = (pDef-
26608 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
26609 46 55 4e 43 5f 43 41 53 45 29 3d 3d 30 3b 0a 20  FUNC_CASE)==0;. 
2660a 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
2660b 0a 2a 2a 20 41 6c 6c 20 61 6c 6c 20 6f 66 20 74  .** All all of t
2660c 68 65 20 46 75 6e 63 44 65 66 20 73 74 72 75 63  he FuncDef struc
2660d 74 75 72 65 73 20 69 6e 20 74 68 65 20 61 42 75  tures in the aBu
2660e 69 6c 74 69 6e 46 75 6e 63 5b 5d 20 61 72 72 61  iltinFunc[] arra
2660f 79 20 61 62 6f 76 65 0a 2a 2a 20 74 6f 20 74 68  y above.** to th
26610 65 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f  e global functio
26611 6e 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54  n hash table.  T
26612 68 69 73 20 6f 63 63 75 72 73 20 61 74 20 73 74  his occurs at st
26613 61 72 74 2d 74 69 6d 65 20 28 61 73 0a 2a 2a 20  art-time (as.** 
26614 61 20 63 6f 6e 73 65 71 75 65 6e 63 65 20 6f 66  a consequence of
26615 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33   calling sqlite3
26616 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 29 2e 0a  _initialize())..
26617 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 69 73  **.** After this
26618 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 0a 2a 2f   routine runs.*/
26619 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2661a 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69  void sqlite3Regi
2661b 73 74 65 72 47 6c 6f 62 61 6c 46 75 6e 63 74 69  sterGlobalFuncti
2661c 6f 6e 73 28 76 6f 69 64 29 7b 0a 20 20 2f 2a 0a  ons(void){.  /*.
2661d 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    ** The followi
2661e 6e 67 20 61 72 72 61 79 20 68 6f 6c 64 73 20 46  ng array holds F
2661f 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 65  uncDef structure
26620 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65  s for all of the
26621 20 66 75 6e 63 74 69 6f 6e 73 0a 20 20 2a 2a 20   functions.  ** 
26622 64 65 66 69 6e 65 64 20 69 6e 20 74 68 69 73 20  defined in this 
26623 66 69 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  file..  **.  ** 
26624 54 68 65 20 61 72 72 61 79 20 63 61 6e 6e 6f 74  The array cannot
26625 20 62 65 20 63 6f 6e 73 74 61 6e 74 20 73 69 6e   be constant sin
26626 63 65 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d  ce changes are m
26627 61 64 65 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20  ade to the.  ** 
26628 46 75 6e 63 44 65 66 2e 70 48 61 73 68 20 65 6c  FuncDef.pHash el
26629 65 6d 65 6e 74 73 20 61 74 20 73 74 61 72 74 2d  ements at start-
2662a 74 69 6d 65 2e 20 20 54 68 65 20 65 6c 65 6d 65  time.  The eleme
2662b 6e 74 73 20 6f 66 20 74 68 69 73 20 61 72 72 61  nts of this arra
2662c 79 0a 20 20 2a 2a 20 61 72 65 20 72 65 61 64 2d  y.  ** are read-
2662d 6f 6e 6c 79 20 61 66 74 65 72 20 69 6e 69 74 69  only after initi
2662e 61 6c 69 7a 61 74 69 6f 6e 20 69 73 20 63 6f 6d  alization is com
2662f 70 6c 65 74 65 2e 0a 20 20 2a 2f 0a 20 20 73 74  plete..  */.  st
26630 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20  atic SQLITE_WSD 
26631 46 75 6e 63 44 65 66 20 61 42 75 69 6c 74 69 6e  FuncDef aBuiltin
26632 46 75 6e 63 5b 5d 20 3d 20 7b 0a 20 20 20 20 46  Func[] = {.    F
26633 55 4e 43 54 49 4f 4e 28 6c 74 72 69 6d 2c 20 20  UNCTION(ltrim,  
26634 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 31              1, 1
26635 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20  , 0, trimFunc   
26636 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
26637 43 54 49 4f 4e 28 6c 74 72 69 6d 2c 20 20 20 20  CTION(ltrim,    
26638 20 20 20 20 20 20 20 20 20 20 32 2c 20 31 2c 20            2, 1, 
26639 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20  0, trimFunc     
2663a 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
2663b 49 4f 4e 28 72 74 72 69 6d 2c 20 20 20 20 20 20  ION(rtrim,      
2663c 20 20 20 20 20 20 20 20 31 2c 20 32 2c 20 30 2c          1, 2, 0,
2663d 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20   trimFunc       
2663e 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
2663f 4e 28 72 74 72 69 6d 2c 20 20 20 20 20 20 20 20  N(rtrim,        
26640 20 20 20 20 20 20 32 2c 20 32 2c 20 30 2c 20 74        2, 2, 0, t
26641 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20  rimFunc         
26642 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
26643 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20  trim,           
26644 20 20 20 20 31 2c 20 33 2c 20 30 2c 20 74 72 69      1, 3, 0, tri
26645 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c  mFunc         ),
26646 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74 72  .    FUNCTION(tr
26647 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  im,             
26648 20 20 32 2c 20 33 2c 20 30 2c 20 74 72 69 6d 46    2, 3, 0, trimF
26649 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20  unc         ),. 
2664a 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 69 6e 2c     FUNCTION(min,
2664b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d                 -
2664c 31 2c 20 30 2c 20 31 2c 20 6d 69 6e 6d 61 78 46  1, 0, 1, minmaxF
2664d 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20  unc       ),.   
2664e 20 46 55 4e 43 54 49 4f 4e 28 6d 69 6e 2c 20 20   FUNCTION(min,  
2664f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c                0,
26650 20 30 2c 20 31 2c 20 30 20 20 20 20 20 20 20 20   0, 1, 0        
26651 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 41          ),.    A
26652 47 47 52 45 47 41 54 45 28 6d 69 6e 2c 20 20 20  GGREGATE(min,   
26653 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30              1, 0
26654 2c 20 31 2c 20 6d 69 6e 6d 61 78 53 74 65 70 2c  , 1, minmaxStep,
26655 20 20 20 20 20 20 6d 69 6e 4d 61 78 46 69 6e 61        minMaxFina
26656 6c 69 7a 65 20 29 2c 0a 20 20 20 20 46 55 4e 43  lize ),.    FUNC
26657 54 49 4f 4e 28 6d 61 78 2c 20 20 20 20 20 20 20  TION(max,       
26658 20 20 20 20 20 20 20 20 2d 31 2c 20 31 2c 20 31          -1, 1, 1
26659 2c 20 6d 69 6e 6d 61 78 46 75 6e 63 20 20 20 20  , minmaxFunc    
2665a 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
2665b 4f 4e 28 6d 61 78 2c 20 20 20 20 20 20 20 20 20  ON(max,         
2665c 20 20 20 20 20 20 20 30 2c 20 31 2c 20 31 2c 20         0, 1, 1, 
2665d 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
2665e 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54   ),.    AGGREGAT
2665f 45 28 6d 61 78 2c 20 20 20 20 20 20 20 20 20 20  E(max,          
26660 20 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 6d 69       1, 1, 1, mi
26661 6e 6d 61 78 53 74 65 70 2c 20 20 20 20 20 20 6d  nmaxStep,      m
26662 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 20 29 2c  inMaxFinalize ),
26663 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74 79  .    FUNCTION(ty
26664 70 65 6f 66 2c 20 20 20 20 20 20 20 20 20 20 20  peof,           
26665 20 20 31 2c 20 30 2c 20 30 2c 20 74 79 70 65 6f    1, 0, 0, typeo
26666 66 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20  fFunc       ),. 
26667 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 65 6e 67     FUNCTION(leng
26668 74 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  th,             
26669 31 2c 20 30 2c 20 30 2c 20 6c 65 6e 67 74 68 46  1, 0, 0, lengthF
2666a 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20  unc       ),.   
2666b 20 46 55 4e 43 54 49 4f 4e 28 73 75 62 73 74 72   FUNCTION(substr
2666c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c  ,             2,
2666d 20 30 2c 20 30 2c 20 73 75 62 73 74 72 46 75 6e   0, 0, substrFun
2666e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46  c       ),.    F
2666f 55 4e 43 54 49 4f 4e 28 73 75 62 73 74 72 2c 20  UNCTION(substr, 
26670 20 20 20 20 20 20 20 20 20 20 20 20 33 2c 20 30              3, 0
26671 2c 20 30 2c 20 73 75 62 73 74 72 46 75 6e 63 20  , 0, substrFunc 
26672 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
26673 43 54 49 4f 4e 28 61 62 73 2c 20 20 20 20 20 20  CTION(abs,      
26674 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20            1, 0, 
26675 30 2c 20 61 62 73 46 75 6e 63 20 20 20 20 20 20  0, absFunc      
26676 20 20 20 20 29 2c 0a 23 69 66 6e 64 65 66 20 53      ),.#ifndef S
26677 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
26678 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 46 55  ING_POINT.    FU
26679 4e 43 54 49 4f 4e 28 72 6f 75 6e 64 2c 20 20 20  NCTION(round,   
2667a 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c             1, 0,
2667b 20 30 2c 20 72 6f 75 6e 64 46 75 6e 63 20 20 20   0, roundFunc   
2667c 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
2667d 54 49 4f 4e 28 72 6f 75 6e 64 2c 20 20 20 20 20  TION(round,     
2667e 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20 30           2, 0, 0
2667f 2c 20 72 6f 75 6e 64 46 75 6e 63 20 20 20 20 20  , roundFunc     
26680 20 20 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20 20     ),.#endif.   
26681 20 46 55 4e 43 54 49 4f 4e 28 75 70 70 65 72 2c   FUNCTION(upper,
26682 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c                1,
26683 20 30 2c 20 30 2c 20 75 70 70 65 72 46 75 6e 63   0, 0, upperFunc
26684 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46          ),.    F
26685 55 4e 43 54 49 4f 4e 28 6c 6f 77 65 72 2c 20 20  UNCTION(lower,  
26686 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30              1, 0
26687 2c 20 30 2c 20 6c 6f 77 65 72 46 75 6e 63 20 20  , 0, lowerFunc  
26688 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
26689 43 54 49 4f 4e 28 63 6f 61 6c 65 73 63 65 2c 20  CTION(coalesce, 
2668a 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20            1, 0, 
2668b 30 2c 20 30 20 20 20 20 20 20 20 20 20 20 20 20  0, 0            
2668c 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
2668d 49 4f 4e 28 63 6f 61 6c 65 73 63 65 2c 20 20 20  ION(coalesce,   
2668e 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c         -1, 0, 0,
2668f 20 69 66 6e 75 6c 6c 46 75 6e 63 20 20 20 20 20   ifnullFunc     
26690 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
26691 4e 28 63 6f 61 6c 65 73 63 65 2c 20 20 20 20 20  N(coalesce,     
26692 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30        0, 0, 0, 0
26693 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26694 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
26695 68 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  hex,            
26696 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 68 65 78      1, 0, 0, hex
26697 46 75 6e 63 20 20 20 20 20 20 20 20 20 20 29 2c  Func          ),
26698 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 69 66  .    FUNCTION(if
26699 6e 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20  null,           
2669a 20 20 32 2c 20 30 2c 20 31 2c 20 69 66 6e 75 6c    2, 0, 1, ifnul
2669b 6c 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20  lFunc       ),. 
2669c 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 61 6e 64     FUNCTION(rand
2669d 6f 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  om,             
2669e 30 2c 20 30 2c 20 30 2c 20 72 61 6e 64 6f 6d 46  0, 0, 0, randomF
2669f 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20  unc       ),.   
266a0 20 46 55 4e 43 54 49 4f 4e 28 72 61 6e 64 6f 6d   FUNCTION(random
266a1 62 6c 6f 62 2c 20 20 20 20 20 20 20 20 20 31 2c  blob,         1,
266a2 20 30 2c 20 30 2c 20 72 61 6e 64 6f 6d 42 6c 6f   0, 0, randomBlo
266a3 62 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46  b       ),.    F
266a4 55 4e 43 54 49 4f 4e 28 6e 75 6c 6c 69 66 2c 20  UNCTION(nullif, 
266a5 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 30              2, 0
266a6 2c 20 31 2c 20 6e 75 6c 6c 69 66 46 75 6e 63 20  , 1, nullifFunc 
266a7 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
266a8 43 54 49 4f 4e 28 73 71 6c 69 74 65 5f 76 65 72  CTION(sqlite_ver
266a9 73 69 6f 6e 2c 20 20 20 20 20 30 2c 20 30 2c 20  sion,     0, 0, 
266aa 30 2c 20 76 65 72 73 69 6f 6e 46 75 6e 63 20 20  0, versionFunc  
266ab 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
266ac 49 4f 4e 28 73 71 6c 69 74 65 5f 73 6f 75 72 63  ION(sqlite_sourc
266ad 65 5f 69 64 2c 20 20 20 30 2c 20 30 2c 20 30 2c  e_id,   0, 0, 0,
266ae 20 73 6f 75 72 63 65 69 64 46 75 6e 63 20 20 20   sourceidFunc   
266af 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
266b0 4e 28 71 75 6f 74 65 2c 20 20 20 20 20 20 20 20  N(quote,        
266b1 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 71        1, 0, 0, q
266b2 75 6f 74 65 46 75 6e 63 20 20 20 20 20 20 20 20  uoteFunc        
266b3 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
266b4 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
266b5 64 2c 20 20 30 2c 20 30 2c 20 30 2c 20 6c 61 73  d,  0, 0, 0, las
266b6 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 29 2c  t_insert_rowid),
266b7 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 68  .    FUNCTION(ch
266b8 61 6e 67 65 73 2c 20 20 20 20 20 20 20 20 20 20  anges,          
266b9 20 20 30 2c 20 30 2c 20 30 2c 20 63 68 61 6e 67    0, 0, 0, chang
266ba 65 73 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20  es          ),. 
266bb 20 20 20 46 55 4e 43 54 49 4f 4e 28 74 6f 74 61     FUNCTION(tota
266bc 6c 5f 63 68 61 6e 67 65 73 2c 20 20 20 20 20 20  l_changes,      
266bd 30 2c 20 30 2c 20 30 2c 20 74 6f 74 61 6c 5f 63  0, 0, 0, total_c
266be 68 61 6e 67 65 73 20 20 20 20 29 2c 0a 20 20 20  hanges    ),.   
266bf 20 46 55 4e 43 54 49 4f 4e 28 72 65 70 6c 61 63   FUNCTION(replac
266c0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 33 2c  e,            3,
266c1 20 30 2c 20 30 2c 20 72 65 70 6c 61 63 65 46 75   0, 0, replaceFu
266c2 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46  nc      ),.    F
266c3 55 4e 43 54 49 4f 4e 28 7a 65 72 6f 62 6c 6f 62  UNCTION(zeroblob
266c4 2c 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30  ,           1, 0
266c5 2c 20 30 2c 20 7a 65 72 6f 62 6c 6f 62 46 75 6e  , 0, zeroblobFun
266c6 63 20 20 20 20 20 29 2c 0a 20 20 23 69 66 64 65  c     ),.  #ifde
266c7 66 20 53 51 4c 49 54 45 5f 53 4f 55 4e 44 45 58  f SQLITE_SOUNDEX
266c8 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 6f  .    FUNCTION(so
266c9 75 6e 64 65 78 2c 20 20 20 20 20 20 20 20 20 20  undex,          
266ca 20 20 31 2c 20 30 2c 20 30 2c 20 73 6f 75 6e 64    1, 0, 0, sound
266cb 65 78 46 75 6e 63 20 20 20 20 20 20 29 2c 0a 20  exFunc      ),. 
266cc 20 23 65 6e 64 69 66 0a 20 20 23 69 66 6e 64 65   #endif.  #ifnde
266cd 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
266ce 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 20 20 20  AD_EXTENSION.   
266cf 20 46 55 4e 43 54 49 4f 4e 28 6c 6f 61 64 5f 65   FUNCTION(load_e
266d0 78 74 65 6e 73 69 6f 6e 2c 20 20 20 20 20 31 2c  xtension,     1,
266d1 20 30 2c 20 30 2c 20 6c 6f 61 64 45 78 74 20 20   0, 0, loadExt  
266d2 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46          ),.    F
266d3 55 4e 43 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74  UNCTION(load_ext
266d4 65 6e 73 69 6f 6e 2c 20 20 20 20 20 32 2c 20 30  ension,     2, 0
266d5 2c 20 30 2c 20 6c 6f 61 64 45 78 74 20 20 20 20  , 0, loadExt    
266d6 20 20 20 20 20 20 29 2c 0a 20 20 23 65 6e 64 69        ),.  #endi
266d7 66 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 28  f.    AGGREGATE(
266d8 73 75 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  sum,            
266d9 20 20 20 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53     1, 0, 0, sumS
266da 74 65 70 2c 20 20 20 20 20 20 20 20 20 73 75 6d  tep,         sum
266db 46 69 6e 61 6c 69 7a 65 20 20 20 20 29 2c 0a 20  Finalize    ),. 
266dc 20 20 20 41 47 47 52 45 47 41 54 45 28 74 6f 74     AGGREGATE(tot
266dd 61 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  al,             
266de 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 70  1, 0, 0, sumStep
266df 2c 20 20 20 20 20 20 20 20 20 74 6f 74 61 6c 46  ,         totalF
266e0 69 6e 61 6c 69 7a 65 20 20 20 20 29 2c 0a 20 20  inalize    ),.  
266e1 20 20 41 47 47 52 45 47 41 54 45 28 61 76 67 2c    AGGREGATE(avg,
266e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
266e3 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c  , 0, 0, sumStep,
266e4 20 20 20 20 20 20 20 20 20 61 76 67 46 69 6e 61           avgFina
266e5 6c 69 7a 65 20 20 20 20 29 2c 0a 20 2f 2a 20 41  lize    ),. /* A
266e6 47 47 52 45 47 41 54 45 28 63 6f 75 6e 74 2c 20  GGREGATE(count, 
266e7 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30              0, 0
266e8 2c 20 30 2c 20 63 6f 75 6e 74 53 74 65 70 2c 20  , 0, countStep, 
266e9 20 20 20 20 20 20 63 6f 75 6e 74 46 69 6e 61 6c        countFinal
266ea 69 7a 65 20 20 29 2c 20 2a 2f 0a 20 20 20 20 7b  ize  ), */.    {
266eb 30 2c 53 51 4c 49 54 45 5f 55 54 46 38 2c 53 51  0,SQLITE_UTF8,SQ
266ec 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 55 4e 54 2c  LITE_FUNC_COUNT,
266ed 30 2c 30 2c 30 2c 63 6f 75 6e 74 53 74 65 70 2c  0,0,0,countStep,
266ee 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 2c 22 63  countFinalize,"c
266ef 6f 75 6e 74 22 2c 30 7d 2c 0a 20 20 20 20 41 47  ount",0},.    AG
266f0 47 52 45 47 41 54 45 28 63 6f 75 6e 74 2c 20 20  GREGATE(count,  
266f1 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c             1, 0,
266f2 20 30 2c 20 63 6f 75 6e 74 53 74 65 70 2c 20 20   0, countStep,  
266f3 20 20 20 20 20 63 6f 75 6e 74 46 69 6e 61 6c 69       countFinali
266f4 7a 65 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45  ze  ),.    AGGRE
266f5 47 41 54 45 28 67 72 6f 75 70 5f 63 6f 6e 63 61  GATE(group_conca
266f6 74 2c 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c  t,      1, 0, 0,
266f7 20 67 72 6f 75 70 43 6f 6e 63 61 74 53 74 65 70   groupConcatStep
266f8 2c 20 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e  , groupConcatFin
266f9 61 6c 69 7a 65 29 2c 0a 20 20 20 20 41 47 47 52  alize),.    AGGR
266fa 45 47 41 54 45 28 67 72 6f 75 70 5f 63 6f 6e 63  EGATE(group_conc
266fb 61 74 2c 20 20 20 20 20 20 32 2c 20 30 2c 20 30  at,      2, 0, 0
266fc 2c 20 67 72 6f 75 70 43 6f 6e 63 61 74 53 74 65  , groupConcatSte
266fd 70 2c 20 67 72 6f 75 70 43 6f 6e 63 61 74 46 69  p, groupConcatFi
266fe 6e 61 6c 69 7a 65 29 2c 0a 20 20 0a 20 20 20 20  nalize),.  .    
266ff 4c 49 4b 45 46 55 4e 43 28 67 6c 6f 62 2c 20 32  LIKEFUNC(glob, 2
26700 2c 20 26 67 6c 6f 62 49 6e 66 6f 2c 20 53 51 4c  , &globInfo, SQL
26701 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51  ITE_FUNC_LIKE|SQ
26702 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c  LITE_FUNC_CASE),
26703 0a 20 20 23 69 66 64 65 66 20 53 51 4c 49 54 45  .  #ifdef SQLITE
26704 5f 43 41 53 45 5f 53 45 4e 53 49 54 49 56 45 5f  _CASE_SENSITIVE_
26705 4c 49 4b 45 0a 20 20 20 20 4c 49 4b 45 46 55 4e  LIKE.    LIKEFUN
26706 43 28 6c 69 6b 65 2c 20 32 2c 20 26 6c 69 6b 65  C(like, 2, &like
26707 49 6e 66 6f 41 6c 74 2c 20 53 51 4c 49 54 45 5f  InfoAlt, SQLITE_
26708 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 45  FUNC_LIKE|SQLITE
26709 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0a 20 20 20  _FUNC_CASE),.   
2670a 20 4c 49 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20   LIKEFUNC(like, 
2670b 33 2c 20 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2c  3, &likeInfoAlt,
2670c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b   SQLITE_FUNC_LIK
2670d 45 7c 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41  E|SQLITE_FUNC_CA
2670e 53 45 29 2c 0a 20 20 23 65 6c 73 65 0a 20 20 20  SE),.  #else.   
2670f 20 4c 49 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20   LIKEFUNC(like, 
26710 32 2c 20 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d  2, &likeInfoNorm
26711 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49  , SQLITE_FUNC_LI
26712 4b 45 29 2c 0a 20 20 20 20 4c 49 4b 45 46 55 4e  KE),.    LIKEFUN
26713 43 28 6c 69 6b 65 2c 20 33 2c 20 26 6c 69 6b 65  C(like, 3, &like
26714 49 6e 66 6f 4e 6f 72 6d 2c 20 53 51 4c 49 54 45  InfoNorm, SQLITE
26715 5f 46 55 4e 43 5f 4c 49 4b 45 29 2c 0a 20 20 23  _FUNC_LIKE),.  #
26716 65 6e 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 69 6e  endif.  };..  in
26717 74 20 69 3b 0a 20 20 46 75 6e 63 44 65 66 48 61  t i;.  FuncDefHa
26718 73 68 20 2a 70 48 61 73 68 20 3d 20 26 47 4c 4f  sh *pHash = &GLO
26719 42 41 4c 28 46 75 6e 63 44 65 66 48 61 73 68 2c  BAL(FuncDefHash,
2671a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75   sqlite3GlobalFu
2671b 6e 63 74 69 6f 6e 73 29 3b 0a 20 20 46 75 6e 63  nctions);.  Func
2671c 44 65 66 20 2a 61 46 75 6e 63 20 3d 20 28 46 75  Def *aFunc = (Fu
2671d 6e 63 44 65 66 2a 29 26 47 4c 4f 42 41 4c 28 46  ncDef*)&GLOBAL(F
2671e 75 6e 63 44 65 66 2c 20 61 42 75 69 6c 74 69 6e  uncDef, aBuiltin
2671f 46 75 6e 63 29 3b 0a 0a 20 20 66 6f 72 28 69 3d  Func);..  for(i=
26720 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 61  0; i<ArraySize(a
26721 42 75 69 6c 74 69 6e 46 75 6e 63 29 3b 20 69 2b  BuiltinFunc); i+
26722 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46  +){.    sqlite3F
26723 75 6e 63 44 65 66 49 6e 73 65 72 74 28 70 48 61  uncDefInsert(pHa
26724 73 68 2c 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a  sh, &aFunc[i]);.
26725 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 67    }.  sqlite3Reg
26726 69 73 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e  isterDateTimeFun
26727 63 74 69 6f 6e 73 28 29 3b 0a 7d 0a 0a 2f 2a 2a  ctions();.}../**
26728 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
26729 20 6f 66 20 66 75 6e 63 2e 63 20 2a 2a 2a 2a 2a   of func.c *****
2672a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2672b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2672c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
2672d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
2672e 69 6e 20 66 69 6c 65 20 66 6b 65 79 2e 63 20 2a  in file fkey.c *
2672f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26731 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
26732 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
26733 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
26734 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
26735 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
26736 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
26737 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
26738 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
26739 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
2673a 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
2673b 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
2673c 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
2673d 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
2673e 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
2673f 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
26740 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
26741 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
26742 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
26743 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
26744 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26745 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26746 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26747 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
26748 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
26749 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 62  ains code used b
2674a 79 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 74  y the compiler t
2674b 6f 20 61 64 64 20 66 6f 72 65 69 67 6e 20 6b 65  o add foreign ke
2674c 79 0a 2a 2a 20 73 75 70 70 6f 72 74 20 74 6f 20  y.** support to 
2674d 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61  compiled SQL sta
2674e 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 0a 23 69 66  tements..*/..#if
2674f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
26750 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 23 69 66  _FOREIGN_KEY.#if
26751 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
26752 5f 54 52 49 47 47 45 52 0a 0a 2f 2a 0a 2a 2a 20  _TRIGGER../*.** 
26753 44 65 66 65 72 72 65 64 20 61 6e 64 20 49 6d 6d  Deferred and Imm
26754 65 64 69 61 74 65 20 46 4b 73 0a 2a 2a 20 2d 2d  ediate FKs.** --
26755 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26756 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 46  --------.**.** F
26757 6f 72 65 69 67 6e 20 6b 65 79 73 20 69 6e 20 53  oreign keys in S
26758 51 4c 69 74 65 20 63 6f 6d 65 20 69 6e 20 74 77  QLite come in tw
26759 6f 20 66 6c 61 76 6f 75 72 73 3a 20 64 65 66 65  o flavours: defe
2675a 72 72 65 64 20 61 6e 64 20 69 6d 6d 65 64 69 61  rred and immedia
2675b 74 65 2e 0a 2a 2a 20 49 66 20 61 6e 20 69 6d 6d  te..** If an imm
2675c 65 64 69 61 74 65 20 66 6f 72 65 69 67 6e 20 6b  ediate foreign k
2675d 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73  ey constraint is
2675e 20 76 69 6f 6c 61 74 65 64 2c 20 53 51 4c 49 54   violated, SQLIT
2675f 45 5f 43 4f 4e 53 54 52 41 49 4e 54 0a 2a 2a 20  E_CONSTRAINT.** 
26760 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
26761 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74  the current stat
26762 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
26763 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 49  n rolled back. I
26764 66 20 61 20 0a 2a 2a 20 64 65 66 65 72 72 65 64  f a .** deferred
26765 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e   foreign key con
26766 73 74 72 61 69 6e 74 20 69 73 20 76 69 6f 6c 61  straint is viola
26767 74 65 64 2c 20 6e 6f 20 61 63 74 69 6f 6e 20 69  ted, no action i
26768 73 20 74 61 6b 65 6e 20 0a 2a 2a 20 69 6d 6d 65  s taken .** imme
26769 64 69 61 74 65 6c 79 2e 20 48 6f 77 65 76 65 72  diately. However
2676a 20 69 66 20 74 68 65 20 61 70 70 6c 69 63 61 74   if the applicat
2676b 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ion attempts to 
2676c 63 6f 6d 6d 69 74 20 74 68 65 20 0a 2a 2a 20 74  commit the .** t
2676d 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 66 6f 72  ransaction befor
2676e 65 20 66 69 78 69 6e 67 20 74 68 65 20 63 6f 6e  e fixing the con
2676f 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f  straint violatio
26770 6e 2c 20 74 68 65 20 61 74 74 65 6d 70 74 20 66  n, the attempt f
26771 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 44 65 66 65  ails..**.** Defe
26772 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 73  rred constraints
26773 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64   are implemented
26774 20 75 73 69 6e 67 20 61 20 73 69 6d 70 6c 65 20   using a simple 
26775 63 6f 75 6e 74 65 72 20 61 73 73 6f 63 69 61 74  counter associat
26776 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 64  ed.** with the d
26777 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20  atabase handle. 
26778 54 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20 73  The counter is s
26779 65 74 20 74 6f 20 7a 65 72 6f 20 65 61 63 68 20  et to zero each 
2677a 74 69 6d 65 20 61 20 0a 2a 2a 20 64 61 74 61 62  time a .** datab
2677b 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ase transaction 
2677c 69 73 20 6f 70 65 6e 65 64 2e 20 45 61 63 68 20  is opened. Each 
2677d 74 69 6d 65 20 61 20 73 74 61 74 65 6d 65 6e 74  time a statement
2677e 20 69 73 20 65 78 65 63 75 74 65 64 20 0a 2a 2a   is executed .**
2677f 20 74 68 61 74 20 63 61 75 73 65 73 20 61 20 66   that causes a f
26780 6f 72 65 69 67 6e 20 6b 65 79 20 76 69 6f 6c 61  oreign key viola
26781 74 69 6f 6e 2c 20 74 68 65 20 63 6f 75 6e 74 65  tion, the counte
26782 72 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  r is incremented
26783 2e 20 45 61 63 68 0a 2a 2a 20 74 69 6d 65 20 61  . Each.** time a
26784 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 65 78   statement is ex
26785 65 63 75 74 65 64 20 74 68 61 74 20 72 65 6d 6f  ecuted that remo
26786 76 65 73 20 61 6e 20 65 78 69 73 74 69 6e 67 20  ves an existing 
26787 76 69 6f 6c 61 74 69 6f 6e 20 66 72 6f 6d 0a 2a  violation from.*
26788 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  * the database, 
26789 74 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20 64  the counter is d
2678a 65 63 72 65 6d 65 6e 74 65 64 2e 20 57 68 65 6e  ecremented. When
2678b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
2678c 20 69 73 0a 2a 2a 20 63 6f 6d 6d 69 74 74 65 64   is.** committed
2678d 2c 20 74 68 65 20 63 6f 6d 6d 69 74 20 66 61 69  , the commit fai
2678e 6c 73 20 69 66 20 74 68 65 20 63 75 72 72 65 6e  ls if the curren
2678f 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63  t value of the c
26790 6f 75 6e 74 65 72 20 69 73 0a 2a 2a 20 67 72 65  ounter is.** gre
26791 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2e 20  ater than zero. 
26792 54 68 69 73 20 73 63 68 65 6d 65 20 68 61 73 20  This scheme has 
26793 74 77 6f 20 62 69 67 20 64 72 61 77 62 61 63 6b  two big drawback
26794 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 57 68 65  s:.**.**   * Whe
26795 6e 20 61 20 63 6f 6d 6d 69 74 20 66 61 69 6c 73  n a commit fails
26796 20 64 75 65 20 74 6f 20 61 20 64 65 66 65 72 72   due to a deferr
26797 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  ed foreign key c
26798 6f 6e 73 74 72 61 69 6e 74 2c 20 0a 2a 2a 20 20  onstraint, .**  
26799 20 20 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77     there is no w
2679a 61 79 20 74 6f 20 74 65 6c 6c 20 77 68 69 63 68  ay to tell which
2679b 20 66 6f 72 65 69 67 6e 20 63 6f 6e 73 74 72 61   foreign constra
2679c 69 6e 74 20 69 73 20 6e 6f 74 20 73 61 74 69 73  int is not satis
2679d 66 69 65 64 2c 0a 2a 2a 20 20 20 20 20 6f 72 20  fied,.**     or 
2679e 77 68 69 63 68 20 72 6f 77 20 69 74 20 69 73 20  which row it is 
2679f 6e 6f 74 20 73 61 74 69 73 66 69 65 64 20 66 6f  not satisfied fo
267a0 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20  r..**.**   * If 
267a1 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
267a2 74 61 69 6e 73 20 66 6f 72 65 69 67 6e 20 6b 65  tains foreign ke
267a3 79 20 76 69 6f 6c 61 74 69 6f 6e 73 20 77 68 65  y violations whe
267a4 6e 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 74 72  n the .**     tr
267a5 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65  ansaction is ope
267a6 6e 65 64 2c 20 74 68 69 73 20 6d 61 79 20 63 61  ned, this may ca
267a7 75 73 65 20 74 68 65 20 6d 65 63 68 61 6e 69 73  use the mechanis
267a8 6d 20 74 6f 20 6d 61 6c 66 75 6e 63 74 69 6f 6e  m to malfunction
267a9 2e 0a 2a 2a 0a 2a 2a 20 44 65 73 70 69 74 65 20  ..**.** Despite 
267aa 74 68 65 73 65 20 70 72 6f 62 6c 65 6d 73 2c 20  these problems, 
267ab 74 68 69 73 20 61 70 70 72 6f 61 63 68 20 69 73  this approach is
267ac 20 61 64 6f 70 74 65 64 20 61 73 20 69 74 20 73   adopted as it s
267ad 65 65 6d 73 20 73 69 6d 70 6c 65 72 0a 2a 2a 20  eems simpler.** 
267ae 74 68 61 6e 20 74 68 65 20 61 6c 74 65 72 6e 61  than the alterna
267af 74 69 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 53  tives..**.** INS
267b0 45 52 54 20 6f 70 65 72 61 74 69 6f 6e 73 3a 0a  ERT operations:.
267b1 2a 2a 0a 2a 2a 20 20 20 49 2e 31 29 20 46 6f 72  **.**   I.1) For
267b2 20 65 61 63 68 20 46 4b 20 66 6f 72 20 77 68 69   each FK for whi
267b3 63 68 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  ch the table is 
267b4 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 2c  the child table,
267b5 20 73 65 61 72 63 68 0a 2a 2a 20 20 20 20 20 20   search.**      
267b6 20 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62    the parent tab
267b7 6c 65 20 66 6f 72 20 61 20 6d 61 74 63 68 2e 20  le for a match. 
267b8 49 66 20 6e 6f 6e 65 20 69 73 20 66 6f 75 6e 64  If none is found
267b9 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 0a 2a   increment the.*
267ba 2a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 72 61  *        constra
267bb 69 6e 74 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 0a  int counter..**.
267bc 2a 2a 20 20 20 49 2e 32 29 20 46 6f 72 20 65 61  **   I.2) For ea
267bd 63 68 20 46 4b 20 66 6f 72 20 77 68 69 63 68 20  ch FK for which 
267be 74 68 65 20 74 61 62 6c 65 20 69 73 20 74 68 65  the table is the
267bf 20 70 61 72 65 6e 74 20 74 61 62 6c 65 2c 20 0a   parent table, .
267c0 2a 2a 20 20 20 20 20 20 20 20 73 65 61 72 63 68  **        search
267c1 20 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65   the child table
267c2 20 66 6f 72 20 72 6f 77 73 20 74 68 61 74 20 63   for rows that c
267c3 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65  orrespond to the
267c4 20 6e 65 77 0a 2a 2a 20 20 20 20 20 20 20 20 72   new.**        r
267c5 6f 77 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74  ow in the parent
267c6 20 74 61 62 6c 65 2e 20 44 65 63 72 65 6d 65 6e   table. Decremen
267c7 74 20 74 68 65 20 63 6f 75 6e 74 65 72 20 66 6f  t the counter fo
267c8 72 20 65 61 63 68 20 72 6f 77 0a 2a 2a 20 20 20  r each row.**   
267c9 20 20 20 20 20 66 6f 75 6e 64 20 28 61 73 20 74       found (as t
267ca 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73  he constraint is
267cb 20 6e 6f 77 20 73 61 74 69 73 66 69 65 64 29 2e   now satisfied).
267cc 0a 2a 2a 0a 2a 2a 20 44 45 4c 45 54 45 20 6f 70  .**.** DELETE op
267cd 65 72 61 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20  erations:.**.** 
267ce 20 20 44 2e 31 29 20 46 6f 72 20 65 61 63 68 20    D.1) For each 
267cf 46 4b 20 66 6f 72 20 77 68 69 63 68 20 74 68 65  FK for which the
267d0 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 63 68   table is the ch
267d1 69 6c 64 20 74 61 62 6c 65 2c 20 0a 2a 2a 20 20  ild table, .**  
267d2 20 20 20 20 20 20 73 65 61 72 63 68 20 74 68 65        search the
267d3 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 66 6f   parent table fo
267d4 72 20 61 20 72 6f 77 20 74 68 61 74 20 63 6f 72  r a row that cor
267d5 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20  responds to the 
267d6 0a 2a 2a 20 20 20 20 20 20 20 20 64 65 6c 65 74  .**        delet
267d7 65 64 20 72 6f 77 20 69 6e 20 74 68 65 20 63 68  ed row in the ch
267d8 69 6c 64 20 74 61 62 6c 65 2e 20 49 66 20 73 75  ild table. If su
267d9 63 68 20 61 20 72 6f 77 20 69 73 20 6e 6f 74 20  ch a row is not 
267da 66 6f 75 6e 64 2c 20 0a 2a 2a 20 20 20 20 20 20  found, .**      
267db 20 20 64 65 63 72 65 6d 65 6e 74 20 74 68 65 20    decrement the 
267dc 63 6f 75 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 20  counter..**.**  
267dd 20 44 2e 32 29 20 46 6f 72 20 65 61 63 68 20 46   D.2) For each F
267de 4b 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20  K for which the 
267df 74 61 62 6c 65 20 69 73 20 74 68 65 20 70 61 72  table is the par
267e0 65 6e 74 20 74 61 62 6c 65 2c 20 73 65 61 72 63  ent table, searc
267e1 68 20 0a 2a 2a 20 20 20 20 20 20 20 20 74 68 65  h .**        the
267e2 20 63 68 69 6c 64 20 74 61 62 6c 65 20 66 6f 72   child table for
267e3 20 72 6f 77 73 20 74 68 61 74 20 63 6f 72 72 65   rows that corre
267e4 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 64 65 6c  spond to the del
267e5 65 74 65 64 20 72 6f 77 20 0a 2a 2a 20 20 20 20  eted row .**    
267e6 20 20 20 20 69 6e 20 74 68 65 20 70 61 72 65 6e      in the paren
267e7 74 20 74 61 62 6c 65 2e 20 46 6f 72 20 65 61 63  t table. For eac
267e8 68 20 66 6f 75 6e 64 20 69 6e 63 72 65 6d 65 6e  h found incremen
267e9 74 20 74 68 65 20 63 6f 75 6e 74 65 72 2e 0a 2a  t the counter..*
267ea 2a 0a 2a 2a 20 55 50 44 41 54 45 20 6f 70 65 72  *.** UPDATE oper
267eb 61 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ations:.**.**   
267ec 41 6e 20 55 50 44 41 54 45 20 63 6f 6d 6d 61 6e  An UPDATE comman
267ed 64 20 72 65 71 75 69 72 65 73 20 74 68 61 74 20  d requires that 
267ee 61 6c 6c 20 34 20 73 74 65 70 73 20 61 62 6f 76  all 4 steps abov
267ef 65 20 61 72 65 20 74 61 6b 65 6e 2c 20 62 75 74  e are taken, but
267f0 20 6f 6e 6c 79 0a 2a 2a 20 20 20 66 6f 72 20 46   only.**   for F
267f1 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 66 6f  K constraints fo
267f2 72 20 77 68 69 63 68 20 74 68 65 20 61 66 66 65  r which the affe
267f3 63 74 65 64 20 63 6f 6c 75 6d 6e 73 20 61 72 65  cted columns are
267f4 20 61 63 74 75 61 6c 6c 79 20 0a 2a 2a 20 20 20   actually .**   
267f5 6d 6f 64 69 66 69 65 64 20 28 76 61 6c 75 65 73  modified (values
267f6 20 6d 75 73 74 20 62 65 20 63 6f 6d 70 61 72 65   must be compare
267f7 64 20 61 74 20 72 75 6e 74 69 6d 65 29 2e 0a 2a  d at runtime)..*
267f8 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 49  *.** Note that I
267f9 2e 31 20 61 6e 64 20 44 2e 31 20 61 72 65 20 76  .1 and D.1 are v
267fa 65 72 79 20 73 69 6d 69 6c 61 72 20 6f 70 65 72  ery similar oper
267fb 61 74 69 6f 6e 73 2c 20 61 73 20 61 72 65 20 49  ations, as are I
267fc 2e 32 20 61 6e 64 20 44 2e 32 2e 0a 2a 2a 20 54  .2 and D.2..** T
267fd 68 69 73 20 73 69 6d 70 6c 69 66 69 65 73 20 74  his simplifies t
267fe 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
267ff 6e 20 61 20 62 69 74 2e 0a 2a 2a 0a 2a 2a 20 46  n a bit..**.** F
26800 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20  or the purposes 
26801 6f 66 20 69 6d 6d 65 64 69 61 74 65 20 46 4b 20  of immediate FK 
26802 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 74 68 65  constraints, the
26803 20 4f 52 20 52 45 50 4c 41 43 45 20 63 6f 6e 66   OR REPLACE conf
26804 6c 69 63 74 0a 2a 2a 20 72 65 73 6f 6c 75 74 69  lict.** resoluti
26805 6f 6e 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  on is considered
26806 20 74 6f 20 64 65 6c 65 74 65 20 72 6f 77 73 20   to delete rows 
26807 62 65 66 6f 72 65 20 74 68 65 20 6e 65 77 20 72  before the new r
26808 6f 77 20 69 73 20 69 6e 73 65 72 74 65 64 2e 0a  ow is inserted..
26809 2a 2a 20 49 66 20 61 20 64 65 6c 65 74 65 20 63  ** If a delete c
2680a 61 75 73 65 64 20 62 79 20 4f 52 20 52 45 50 4c  aused by OR REPL
2680b 41 43 45 20 76 69 6f 6c 61 74 65 73 20 61 6e 20  ACE violates an 
2680c 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 61  FK constraint, a
2680d 6e 20 65 78 63 65 70 74 69 6f 6e 0a 2a 2a 20 69  n exception.** i
2680e 73 20 74 68 72 6f 77 6e 2c 20 65 76 65 6e 20 69  s thrown, even i
2680f 66 20 74 68 65 20 46 4b 20 63 6f 6e 73 74 72 61  f the FK constra
26810 69 6e 74 20 77 6f 75 6c 64 20 62 65 20 73 61 74  int would be sat
26811 69 73 66 69 65 64 20 61 66 74 65 72 20 74 68 65  isfied after the
26812 20 6e 65 77 20 0a 2a 2a 20 72 6f 77 20 69 73 20   new .** row is 
26813 69 6e 73 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  inserted..**.** 
26814 49 6d 6d 65 64 69 61 74 65 20 63 6f 6e 73 74 72  Immediate constr
26815 61 69 6e 74 73 20 61 72 65 20 75 73 75 61 6c 6c  aints are usuall
26816 79 20 68 61 6e 64 6c 65 64 20 73 69 6d 69 6c 61  y handled simila
26817 72 6c 79 2e 20 54 68 65 20 6f 6e 6c 79 20 64 69  rly. The only di
26818 66 66 65 72 65 6e 63 65 20 0a 2a 2a 20 69 73 20  fference .** is 
26819 74 68 61 74 20 74 68 65 20 63 6f 75 6e 74 65 72  that the counter
2681a 20 75 73 65 64 20 69 73 20 73 74 6f 72 65 64 20   used is stored 
2681b 61 73 20 70 61 72 74 20 6f 66 20 65 61 63 68 20  as part of each 
2681c 69 6e 64 69 76 69 64 75 61 6c 20 73 74 61 74 65  individual state
2681d 6d 65 6e 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 28  ment.** object (
2681e 73 74 72 75 63 74 20 56 64 62 65 29 2e 20 49 66  struct Vdbe). If
2681f 2c 20 61 66 74 65 72 20 74 68 65 20 73 74 61 74  , after the stat
26820 65 6d 65 6e 74 20 68 61 73 20 72 75 6e 2c 20 69  ement has run, i
26821 74 73 20 69 6d 6d 65 64 69 61 74 65 0a 2a 2a 20  ts immediate.** 
26822 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74  constraint count
26823 65 72 20 69 73 20 67 72 65 61 74 65 72 20 74 68  er is greater th
26824 61 6e 20 7a 65 72 6f 2c 20 69 74 20 72 65 74 75  an zero, it retu
26825 72 6e 73 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54  rns SQLITE_CONST
26826 52 41 49 4e 54 0a 2a 2a 20 61 6e 64 20 74 68 65  RAINT.** and the
26827 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
26828 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64  action is rolled
26829 20 62 61 63 6b 2e 20 41 6e 20 65 78 63 65 70 74   back. An except
2682a 69 6f 6e 20 69 73 20 61 6e 20 49 4e 53 45 52 54  ion is an INSERT
2682b 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 68  .** statement th
2682c 61 74 20 69 6e 73 65 72 74 73 20 61 20 73 69 6e  at inserts a sin
2682d 67 6c 65 20 72 6f 77 20 6f 6e 6c 79 20 28 6e 6f  gle row only (no
2682e 20 74 72 69 67 67 65 72 73 29 2e 20 49 6e 20 74   triggers). In t
2682f 68 69 73 20 63 61 73 65 2c 0a 2a 2a 20 69 6e 73  his case,.** ins
26830 74 65 61 64 20 6f 66 20 75 73 69 6e 67 20 61 20  tead of using a 
26831 63 6f 75 6e 74 65 72 2c 20 61 6e 20 65 78 63 65  counter, an exce
26832 70 74 69 6f 6e 20 69 73 20 74 68 72 6f 77 6e 20  ption is thrown 
26833 69 6d 6d 65 64 69 61 74 65 6c 79 20 69 66 20 74  immediately if t
26834 68 65 0a 2a 2a 20 49 4e 53 45 52 54 20 76 69 6f  he.** INSERT vio
26835 6c 61 74 65 73 20 61 20 66 6f 72 65 69 67 6e 20  lates a foreign 
26836 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20  key constraint. 
26837 54 68 69 73 20 69 73 20 6e 65 63 65 73 73 61 72  This is necessar
26838 79 20 61 73 20 73 75 63 68 0a 2a 2a 20 61 6e 20  y as such.** an 
26839 49 4e 53 45 52 54 20 64 6f 65 73 20 6e 6f 74 20  INSERT does not 
2683a 6f 70 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74  open a statement
2683b 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a   transaction..**
2683c 0a 2a 2a 20 54 4f 44 4f 3a 20 48 6f 77 20 73 68  .** TODO: How sh
2683d 6f 75 6c 64 20 64 72 6f 70 70 69 6e 67 20 61 20  ould dropping a 
2683e 74 61 62 6c 65 20 62 65 20 68 61 6e 64 6c 65 64  table be handled
2683f 3f 20 48 6f 77 20 73 68 6f 75 6c 64 20 72 65 6e  ? How should ren
26840 61 6d 69 6e 67 20 61 20 0a 2a 2a 20 74 61 62 6c  aming a .** tabl
26841 65 20 62 65 20 68 61 6e 64 6c 65 64 3f 0a 2a 2a  e be handled?.**
26842 0a 2a 2a 0a 2a 2a 20 51 75 65 72 79 20 41 50 49  .**.** Query API
26843 20 4e 6f 74 65 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d   Notes.** ------
26844 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20  ---------.**.** 
26845 42 65 66 6f 72 65 20 63 6f 64 69 6e 67 20 61 6e  Before coding an
26846 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54   UPDATE or DELET
26847 45 20 72 6f 77 20 6f 70 65 72 61 74 69 6f 6e 2c  E row operation,
26848 20 74 68 65 20 63 6f 64 65 2d 67 65 6e 65 72 61   the code-genera
26849 74 6f 72 0a 2a 2a 20 66 6f 72 20 74 68 6f 73 65  tor.** for those
2684a 20 74 77 6f 20 6f 70 65 72 61 74 69 6f 6e 73 20   two operations 
2684b 6e 65 65 64 73 20 74 6f 20 6b 6e 6f 77 20 77 68  needs to know wh
2684c 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
2684d 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 72 65   operation.** re
2684e 71 75 69 72 65 73 20 61 6e 79 20 46 4b 20 70 72  quires any FK pr
2684f 6f 63 65 73 73 69 6e 67 20 61 6e 64 2c 20 69 66  ocessing and, if
26850 20 73 6f 2c 20 77 68 69 63 68 20 63 6f 6c 75 6d   so, which colum
26851 6e 73 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e  ns of the origin
26852 61 6c 0a 2a 2a 20 72 6f 77 20 61 72 65 20 72 65  al.** row are re
26853 71 75 69 72 65 64 20 62 79 20 74 68 65 20 46 4b  quired by the FK
26854 20 70 72 6f 63 65 73 73 69 6e 67 20 56 44 42 45   processing VDBE
26855 20 63 6f 64 65 20 28 69 2e 65 2e 20 69 66 20 46   code (i.e. if F
26856 4b 73 20 77 65 72 65 0a 2a 2a 20 69 6d 70 6c 65  Ks were.** imple
26857 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 74 72 69  mented using tri
26858 67 67 65 72 73 2c 20 77 68 69 63 68 20 6f 66 20  ggers, which of 
26859 74 68 65 20 6f 6c 64 2e 2a 20 63 6f 6c 75 6d 6e  the old.* column
2685a 73 20 77 6f 75 6c 64 20 62 65 20 0a 2a 2a 20 61  s would be .** a
2685b 63 63 65 73 73 65 64 29 2e 20 4e 6f 20 69 6e 66  ccessed). No inf
2685c 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72 65 71 75  ormation is requ
2685d 69 72 65 64 20 62 79 20 74 68 65 20 63 6f 64 65  ired by the code
2685e 2d 67 65 6e 65 72 61 74 6f 72 20 62 65 66 6f 72  -generator befor
2685f 65 0a 2a 2a 20 63 6f 64 69 6e 67 20 61 6e 20 49  e.** coding an I
26860 4e 53 45 52 54 20 6f 70 65 72 61 74 69 6f 6e 2e  NSERT operation.
26861 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 75   The functions u
26862 73 65 64 20 62 79 20 74 68 65 20 55 50 44 41 54  sed by the UPDAT
26863 45 2f 44 45 4c 45 54 45 0a 2a 2a 20 67 65 6e 65  E/DELETE.** gene
26864 72 61 74 69 6f 6e 20 63 6f 64 65 20 74 6f 20 71  ration code to q
26865 75 65 72 79 20 66 6f 72 20 74 68 69 73 20 69 6e  uery for this in
26866 66 6f 72 6d 61 74 69 6f 6e 20 61 72 65 3a 0a 2a  formation are:.*
26867 2a 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 46 6b  *.**   sqlite3Fk
26868 52 65 71 75 69 72 65 64 28 29 20 2d 20 54 65 73  Required() - Tes
26869 74 20 74 6f 20 73 65 65 20 69 66 20 46 4b 20 70  t to see if FK p
2686a 72 6f 63 65 73 73 69 6e 67 20 69 73 20 72 65 71  rocessing is req
2686b 75 69 72 65 64 2e 0a 2a 2a 20 20 20 73 71 6c 69  uired..**   sqli
2686c 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 29 20 20  te3FkOldmask()  
2686d 2d 20 51 75 65 72 79 20 66 6f 72 20 74 68 65 20  - Query for the 
2686e 73 65 74 20 6f 66 20 72 65 71 75 69 72 65 64 20  set of required 
2686f 6f 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73 2e 0a 2a  old.* columns..*
26870 2a 0a 2a 2a 0a 2a 2a 20 45 78 74 65 72 6e 61 6c  *.**.** External
26871 6c 79 20 61 63 63 65 73 73 69 62 6c 65 20 6d 6f  ly accessible mo
26872 64 75 6c 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a  dule functions.*
26873 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  * --------------
26874 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26875 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20  --------.**.**  
26876 20 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b 28   sqlite3FkCheck(
26877 29 20 20 20 20 2d 20 43 68 65 63 6b 20 66 6f 72  )    - Check for
26878 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 69 6f   foreign key vio
26879 6c 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 73 71  lations..**   sq
2687a 6c 69 74 65 33 46 6b 41 63 74 69 6f 6e 73 28 29  lite3FkActions()
2687b 20 20 2d 20 43 6f 64 65 20 74 72 69 67 67 65 72    - Code trigger
2687c 73 20 66 6f 72 20 4f 4e 20 55 50 44 41 54 45 2f  s for ON UPDATE/
2687d 4f 4e 20 44 45 4c 45 54 45 20 61 63 74 69 6f 6e  ON DELETE action
2687e 73 2e 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 46  s..**   sqlite3F
2687f 6b 44 65 6c 65 74 65 28 29 20 20 20 2d 20 44 65  kDelete()   - De
26880 6c 65 74 65 20 61 6e 20 46 4b 65 79 20 73 74 72  lete an FKey str
26881 75 63 74 75 72 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  ucture..*/../*.*
26882 2a 20 56 44 42 45 20 43 61 6c 6c 69 6e 67 20 43  * VDBE Calling C
26883 6f 6e 76 65 6e 74 69 6f 6e 0a 2a 2a 20 2d 2d 2d  onvention.** ---
26884 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26885 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70  ----.**.** Examp
26886 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 46 6f 72 20  le:.**.**   For 
26887 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 49 4e  the following IN
26888 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 3a 0a  SERT statement:.
26889 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45  **.**     CREATE
2688a 20 54 41 42 4c 45 20 74 31 28 61 2c 20 62 20 49   TABLE t1(a, b I
2688b 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
2688c 45 59 2c 20 63 29 3b 0a 2a 2a 20 20 20 20 20 49  EY, c);.**     I
2688d 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41  NSERT INTO t1 VA
2688e 4c 55 45 53 28 31 2c 20 32 2c 20 33 2e 31 29 3b  LUES(1, 2, 3.1);
2688f 0a 2a 2a 0a 2a 2a 20 20 20 52 65 67 69 73 74 65  .**.**   Registe
26890 72 20 28 78 29 3a 20 20 20 20 20 20 20 20 32 20  r (x):        2 
26891 20 20 20 28 74 79 70 65 20 69 6e 74 65 67 65 72     (type integer
26892 29 0a 2a 2a 20 20 20 52 65 67 69 73 74 65 72 20  ).**   Register 
26893 28 78 2b 31 29 3a 20 20 20 20 20 20 31 20 20 20  (x+1):      1   
26894 20 28 74 79 70 65 20 69 6e 74 65 67 65 72 29 0a   (type integer).
26895 2a 2a 20 20 20 52 65 67 69 73 74 65 72 20 28 78  **   Register (x
26896 2b 32 29 3a 20 20 20 20 20 20 4e 55 4c 4c 20 28  +2):      NULL (
26897 74 79 70 65 20 4e 55 4c 4c 29 0a 2a 2a 20 20 20  type NULL).**   
26898 52 65 67 69 73 74 65 72 20 28 78 2b 33 29 3a 20  Register (x+3): 
26899 20 20 20 20 20 33 2e 31 20 20 28 74 79 70 65 20       3.1  (type 
2689a 72 65 61 6c 29 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  real).*/../*.** 
2689b 41 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  A foreign key co
2689c 6e 73 74 72 61 69 6e 74 20 72 65 71 75 69 72 65  nstraint require
2689d 73 20 74 68 61 74 20 74 68 65 20 6b 65 79 20 63  s that the key c
2689e 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 70 61  olumns in the pa
2689f 72 65 6e 74 0a 2a 2a 20 74 61 62 6c 65 20 61 72  rent.** table ar
268a0 65 20 63 6f 6c 6c 65 63 74 69 76 65 6c 79 20 73  e collectively s
268a1 75 62 6a 65 63 74 20 74 6f 20 61 20 55 4e 49 51  ubject to a UNIQ
268a2 55 45 20 6f 72 20 50 52 49 4d 41 52 59 20 4b 45  UE or PRIMARY KE
268a3 59 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 2a 2a  Y constraint..**
268a4 20 47 69 76 65 6e 20 74 68 61 74 20 70 50 61 72   Given that pPar
268a5 65 6e 74 20 69 73 20 74 68 65 20 70 61 72 65 6e  ent is the paren
268a6 74 20 74 61 62 6c 65 20 66 6f 72 20 66 6f 72 65  t table for fore
268a7 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
268a8 6e 74 20 70 46 4b 65 79 2c 20 0a 2a 2a 20 73 65  nt pFKey, .** se
268a9 61 72 63 68 20 74 68 65 20 73 63 68 65 6d 61 20  arch the schema 
268aa 61 20 75 6e 69 71 75 65 20 69 6e 64 65 78 20 6f  a unique index o
268ab 6e 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79  n the parent key
268ac 20 63 6f 6c 75 6d 6e 73 2e 20 0a 2a 2a 0a 2a 2a   columns. .**.**
268ad 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
268ae 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64  zero is returned
268af 2e 20 49 66 20 74 68 65 20 70 61 72 65 6e 74 20  . If the parent 
268b0 6b 65 79 20 69 73 20 61 6e 20 49 4e 54 45 47 45  key is an INTEGE
268b1 52 20 50 52 49 4d 41 52 59 20 0a 2a 2a 20 4b 45  R PRIMARY .** KE
268b2 59 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 6f  Y column, then o
268b3 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 2a  utput variable *
268b4 70 70 49 64 78 20 69 73 20 73 65 74 20 74 6f 20  ppIdx is set to 
268b5 4e 55 4c 4c 2e 20 4f 74 68 65 72 77 69 73 65 2c  NULL. Otherwise,
268b6 20 2a 70 70 49 64 78 20 0a 2a 2a 20 69 73 20 73   *ppIdx .** is s
268b7 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  et to point to t
268b8 68 65 20 75 6e 69 71 75 65 20 69 6e 64 65 78 2e  he unique index.
268b9 20 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20   .** .** If the 
268ba 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6e 73 69  parent key consi
268bb 73 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20  sts of a single 
268bc 63 6f 6c 75 6d 6e 20 28 74 68 65 20 66 6f 72 65  column (the fore
268bd 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
268be 6e 74 0a 2a 2a 20 69 73 20 6e 6f 74 20 61 20 63  nt.** is not a c
268bf 6f 6d 70 6f 73 69 74 65 20 66 6f 72 65 69 67 6e  omposite foreign
268c0 20 6b 65 79 29 2c 20 6f 75 74 70 75 74 20 76 61   key), output va
268c1 72 69 61 62 6c 65 20 2a 70 61 69 43 6f 6c 20 69  riable *paiCol i
268c2 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a  s set to NULL..*
268c3 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20  * Otherwise, it 
268c4 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  is set to point 
268c5 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20  to an allocated 
268c6 61 72 72 61 79 20 6f 66 20 73 69 7a 65 20 4e 2c  array of size N,
268c7 20 77 68 65 72 65 0a 2a 2a 20 4e 20 69 73 20 74   where.** N is t
268c8 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
268c9 75 6d 6e 73 20 69 6e 20 74 68 65 20 70 61 72 65  umns in the pare
268ca 6e 74 20 6b 65 79 2e 20 54 68 65 20 66 69 72 73  nt key. The firs
268cb 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  t element of the
268cc 0a 2a 2a 20 61 72 72 61 79 20 69 73 20 74 68 65  .** array is the
268cd 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63 68   index of the ch
268ce 69 6c 64 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e  ild table column
268cf 20 74 68 61 74 20 69 73 20 6d 61 70 70 65 64 20   that is mapped 
268d0 62 79 20 74 68 65 20 46 4b 0a 2a 2a 20 63 6f 6e  by the FK.** con
268d1 73 74 72 61 69 6e 74 20 74 6f 20 74 68 65 20 70  straint to the p
268d2 61 72 65 6e 74 20 74 61 62 6c 65 20 63 6f 6c 75  arent table colu
268d3 6d 6e 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  mn stored in the
268d4 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d   left-most colum
268d5 6e 0a 2a 2a 20 6f 66 20 69 6e 64 65 78 20 2a 70  n.** of index *p
268d6 70 49 64 78 2e 20 54 68 65 20 73 65 63 6f 6e 64  pIdx. The second
268d7 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
268d8 61 72 72 61 79 20 69 73 20 74 68 65 20 69 6e 64  array is the ind
268d9 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 63 68 69  ex of the.** chi
268da 6c 64 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20  ld table column 
268db 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73  that corresponds
268dc 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 6c   to the second l
268dd 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20  eft-most column 
268de 6f 66 0a 2a 2a 20 2a 70 70 49 64 78 2c 20 61 6e  of.** *ppIdx, an
268df 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49  d so on..**.** I
268e0 66 20 74 68 65 20 72 65 71 75 69 72 65 64 20 69  f the required i
268e1 6e 64 65 78 20 63 61 6e 6e 6f 74 20 62 65 20 66  ndex cannot be f
268e2 6f 75 6e 64 2c 20 65 69 74 68 65 72 20 62 65 63  ound, either bec
268e3 61 75 73 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29  ause:.**.**   1)
268e4 20 54 68 65 20 6e 61 6d 65 64 20 70 61 72 65 6e   The named paren
268e5 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 64 6f  t key columns do
268e6 20 6e 6f 74 20 65 78 69 73 74 2c 20 6f 72 0a 2a   not exist, or.*
268e7 2a 0a 2a 2a 20 20 20 32 29 20 54 68 65 20 6e 61  *.**   2) The na
268e8 6d 65 64 20 70 61 72 65 6e 74 20 6b 65 79 20 63  med parent key c
268e9 6f 6c 75 6d 6e 73 20 64 6f 20 65 78 69 73 74 2c  olumns do exist,
268ea 20 62 75 74 20 61 72 65 20 6e 6f 74 20 73 75 62   but are not sub
268eb 6a 65 63 74 20 74 6f 20 61 0a 2a 2a 20 20 20 20  ject to a.**    
268ec 20 20 55 4e 49 51 55 45 20 6f 72 20 50 52 49 4d    UNIQUE or PRIM
268ed 41 52 59 20 4b 45 59 20 63 6f 6e 73 74 72 61 69  ARY KEY constrai
268ee 6e 74 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 33  nt, or.**.**   3
268ef 29 20 4e 6f 20 70 61 72 65 6e 74 20 6b 65 79 20  ) No parent key 
268f0 63 6f 6c 75 6d 6e 73 20 77 65 72 65 20 70 72 6f  columns were pro
268f1 76 69 64 65 64 20 65 78 70 6c 69 63 69 74 6c 79  vided explicitly
268f2 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a   as part of the.
268f3 2a 2a 20 20 20 20 20 20 66 6f 72 65 69 67 6e 20  **      foreign 
268f4 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 2c 20  key definition, 
268f5 61 6e 64 20 74 68 65 20 70 61 72 65 6e 74 20 74  and the parent t
268f6 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20 68 61  able does not ha
268f7 76 65 20 61 0a 2a 2a 20 20 20 20 20 20 50 52 49  ve a.**      PRI
268f8 4d 41 52 59 20 4b 45 59 2c 20 6f 72 0a 2a 2a 0a  MARY KEY, or.**.
268f9 2a 2a 20 20 20 34 29 20 4e 6f 20 70 61 72 65 6e  **   4) No paren
268fa 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 77 65  t key columns we
268fb 72 65 20 70 72 6f 76 69 64 65 64 20 65 78 70 6c  re provided expl
268fc 69 63 69 74 6c 79 20 61 73 20 70 61 72 74 20 6f  icitly as part o
268fd 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 66 6f  f the.**      fo
268fe 72 65 69 67 6e 20 6b 65 79 20 64 65 66 69 6e 69  reign key defini
268ff 74 69 6f 6e 2c 20 61 6e 64 20 74 68 65 20 50 52  tion, and the PR
26900 49 4d 41 52 59 20 4b 45 59 20 6f 66 20 74 68 65  IMARY KEY of the
26901 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 0a 2a   parent table .*
26902 2a 20 20 20 20 20 20 63 6f 6e 73 69 73 74 73 20  *      consists 
26903 6f 66 20 61 20 61 20 64 69 66 66 65 72 65 6e 74  of a a different
26904 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
26905 6e 73 20 74 6f 20 74 68 65 20 63 68 69 6c 64 20  ns to the child 
26906 6b 65 79 20 69 6e 20 0a 2a 2a 20 20 20 20 20 20  key in .**      
26907 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 2e  the child table.
26908 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 6e 6f 6e 2d  .**.** then non-
26909 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64  zero is returned
2690a 2c 20 61 6e 64 20 61 20 22 66 6f 72 65 69 67 6e  , and a "foreign
2690b 20 6b 65 79 20 6d 69 73 6d 61 74 63 68 22 20 65   key mismatch" e
2690c 72 72 6f 72 20 6c 6f 61 64 65 64 0a 2a 2a 20 69  rror loaded.** i
2690d 6e 74 6f 20 70 50 61 72 73 65 2e 20 49 66 20 61  nto pParse. If a
2690e 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75  n OOM error occu
2690f 72 73 2c 20 6e 6f 6e 2d 7a 65 72 6f 20 69 73 20  rs, non-zero is 
26910 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65  returned and the
26911 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  .** pParse->db->
26912 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61  mallocFailed fla
26913 67 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 73 74 61  g is set..*/.sta
26914 74 69 63 20 69 6e 74 20 6c 6f 63 61 74 65 46 6b  tic int locateFk
26915 65 79 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65  eyIndex(.  Parse
26916 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
26917 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
26918 72 73 65 20 63 6f 6e 74 65 78 74 20 74 6f 20 73  rse context to s
26919 74 6f 72 65 20 61 6e 79 20 65 72 72 6f 72 20 69  tore any error i
2691a 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 50  n */.  Table *pP
2691b 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20  arent,          
2691c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74         /* Parent
2691d 20 74 61 62 6c 65 20 6f 66 20 46 4b 20 63 6f 6e   table of FK con
2691e 73 74 72 61 69 6e 74 20 70 46 4b 65 79 20 2a 2f  straint pFKey */
2691f 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 2c 20  .  FKey *pFKey, 
26920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26921 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b 65     /* Foreign ke
26922 79 20 74 6f 20 66 69 6e 64 20 69 6e 64 65 78 20  y to find index 
26923 66 6f 72 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a  for */.  Index *
26924 2a 70 70 49 64 78 2c 20 20 20 20 20 20 20 20 20  *ppIdx,         
26925 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
26926 20 55 6e 69 71 75 65 20 69 6e 64 65 78 20 6f 6e   Unique index on
26927 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 2a 2f   parent table */
26928 0a 20 20 69 6e 74 20 2a 2a 70 61 69 43 6f 6c 20  .  int **paiCol 
26929 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2692a 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 61 70 20 6f     /* OUT: Map o
2692b 66 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 20  f index columns 
2692c 69 6e 20 70 46 4b 65 79 20 2a 2f 0a 29 7b 0a 20  in pFKey */.){. 
2692d 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 30   Index *pIdx = 0
2692e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2692f 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f       /* Value to
26930 20 72 65 74 75 72 6e 20 76 69 61 20 2a 70 70 49   return via *ppI
26931 64 78 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 43  dx */.  int *aiC
26932 6f 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ol = 0;         
26933 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
26934 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 76  alue to return v
26935 69 61 20 2a 70 61 69 43 6f 6c 20 2a 2f 0a 20 20  ia *paiCol */.  
26936 69 6e 74 20 6e 43 6f 6c 20 3d 20 70 46 4b 65 79  int nCol = pFKey
26937 2d 3e 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20  ->nCol;         
26938 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
26939 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 61 72 65   columns in pare
2693a 6e 74 20 6b 65 79 20 2a 2f 0a 20 20 63 68 61 72  nt key */.  char
2693b 20 2a 7a 4b 65 79 20 3d 20 70 46 4b 65 79 2d 3e   *zKey = pFKey->
2693c 61 43 6f 6c 5b 30 5d 2e 7a 43 6f 6c 3b 20 20 20  aCol[0].zCol;   
2693d 2f 2a 20 4e 61 6d 65 20 6f 66 20 6c 65 66 74 2d  /* Name of left-
2693e 6d 6f 73 74 20 70 61 72 65 6e 74 20 6b 65 79 20  most parent key 
2693f 63 6f 6c 75 6d 6e 20 2a 2f 0a 0a 20 20 2f 2a 20  column */..  /* 
26940 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65  The caller is re
26941 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 7a 65  sponsible for ze
26942 72 6f 69 6e 67 20 6f 75 74 70 75 74 20 70 61 72  roing output par
26943 61 6d 65 74 65 72 73 2e 20 2a 2f 0a 20 20 61 73  ameters. */.  as
26944 73 65 72 74 28 20 70 70 49 64 78 20 26 26 20 2a  sert( ppIdx && *
26945 70 70 49 64 78 3d 3d 30 20 29 3b 0a 20 20 61 73  ppIdx==0 );.  as
26946 73 65 72 74 28 20 21 70 61 69 43 6f 6c 20 7c 7c  sert( !paiCol ||
26947 20 2a 70 61 69 43 6f 6c 3d 3d 30 20 29 3b 0a 20   *paiCol==0 );. 
26948 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 20   assert( pParse 
26949 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73  );..  /* If this
2694a 20 69 73 20 61 20 6e 6f 6e 2d 63 6f 6d 70 6f 73   is a non-compos
2694b 69 74 65 20 28 73 69 6e 67 6c 65 20 63 6f 6c 75  ite (single colu
2694c 6d 6e 29 20 66 6f 72 65 69 67 6e 20 6b 65 79 2c  mn) foreign key,
2694d 20 63 68 65 63 6b 20 69 66 20 69 74 20 0a 20 20   check if it .  
2694e 2a 2a 20 6d 61 70 73 20 74 6f 20 74 68 65 20 49  ** maps to the I
2694f 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
26950 45 59 20 6f 66 20 74 61 62 6c 65 20 70 50 61 72  EY of table pPar
26951 65 6e 74 2e 20 49 66 20 73 6f 2c 20 6c 65 61 76  ent. If so, leav
26952 65 20 2a 70 70 49 64 78 20 0a 20 20 2a 2a 20 61  e *ppIdx .  ** a
26953 6e 64 20 2a 70 61 69 43 6f 6c 20 73 65 74 20 74  nd *paiCol set t
26954 6f 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72  o zero and retur
26955 6e 20 65 61 72 6c 79 2e 20 0a 20 20 2a 2a 0a 20  n early. .  **. 
26956 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66   ** Otherwise, f
26957 6f 72 20 61 20 63 6f 6d 70 6f 73 69 74 65 20 66  or a composite f
26958 6f 72 65 69 67 6e 20 6b 65 79 20 28 6d 6f 72 65  oreign key (more
26959 20 74 68 61 6e 20 6f 6e 65 20 63 6f 6c 75 6d 6e   than one column
2695a 29 2c 20 61 6c 6c 6f 63 61 74 65 0a 20 20 2a 2a  ), allocate.  **
2695b 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 61   space for the a
2695c 69 43 6f 6c 20 61 72 72 61 79 20 28 72 65 74 75  iCol array (retu
2695d 72 6e 65 64 20 76 69 61 20 6f 75 74 70 75 74 20  rned via output 
2695e 70 61 72 61 6d 65 74 65 72 20 2a 70 61 69 43 6f  parameter *paiCo
2695f 6c 29 2e 0a 20 20 2a 2a 20 4e 6f 6e 2d 63 6f 6d  l)..  ** Non-com
26960 70 6f 73 69 74 65 20 66 6f 72 65 69 67 6e 20 6b  posite foreign k
26961 65 79 73 20 64 6f 20 6e 6f 74 20 72 65 71 75 69  eys do not requi
26962 72 65 20 74 68 65 20 61 69 43 6f 6c 20 61 72 72  re the aiCol arr
26963 61 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e  ay..  */.  if( n
26964 43 6f 6c 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a  Col==1 ){.    /*
26965 20 54 68 65 20 46 4b 20 6d 61 70 73 20 74 6f 20   The FK maps to 
26966 74 68 65 20 49 50 4b 20 69 66 20 61 6e 79 20 6f  the IPK if any o
26967 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
26968 61 72 65 20 74 72 75 65 3a 0a 20 20 20 20 2a 2a  are true:.    **
26969 0a 20 20 20 20 2a 2a 20 20 20 31 29 20 54 68 65  .    **   1) The
2696a 72 65 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52  re is an INTEGER
2696b 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c   PRIMARY KEY col
2696c 75 6d 6e 20 61 6e 64 20 74 68 65 20 46 4b 20 69  umn and the FK i
2696d 73 20 69 6d 70 6c 69 63 69 74 6c 79 20 0a 20 20  s implicitly .  
2696e 20 20 2a 2a 20 20 20 20 20 20 6d 61 70 70 65 64    **      mapped
2696f 20 74 6f 20 74 68 65 20 70 72 69 6d 61 72 79 20   to the primary 
26970 6b 65 79 20 6f 66 20 74 61 62 6c 65 20 70 50 61  key of table pPa
26971 72 65 6e 74 2c 20 6f 72 0a 20 20 20 20 2a 2a 20  rent, or.    ** 
26972 20 20 32 29 20 54 68 65 20 46 4b 20 69 73 20 65    2) The FK is e
26973 78 70 6c 69 63 69 74 6c 79 20 6d 61 70 70 65 64  xplicitly mapped
26974 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 64 65 63   to a column dec
26975 6c 61 72 65 64 20 61 73 20 49 4e 54 45 47 45 52  lared as INTEGER
26976 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 50 52 49  .    **      PRI
26977 4d 41 52 59 20 4b 45 59 2e 0a 20 20 20 20 2a 2f  MARY KEY..    */
26978 0a 20 20 20 20 69 66 28 20 70 50 61 72 65 6e 74  .    if( pParent
26979 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 7b 0a 20 20  ->iPKey>=0 ){.  
2697a 20 20 20 20 69 66 28 20 21 7a 4b 65 79 20 29 20      if( !zKey ) 
2697b 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
2697c 69 66 28 20 21 73 71 6c 69 74 65 33 53 74 72 49  if( !sqlite3StrI
2697d 43 6d 70 28 70 50 61 72 65 6e 74 2d 3e 61 43 6f  Cmp(pParent->aCo
2697e 6c 5b 70 50 61 72 65 6e 74 2d 3e 69 50 4b 65 79  l[pParent->iPKey
2697f 5d 2e 7a 4e 61 6d 65 2c 20 7a 4b 65 79 29 20 29  ].zName, zKey) )
26980 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
26981 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 61 69  .  }else if( pai
26982 43 6f 6c 20 29 7b 0a 20 20 20 20 61 73 73 65 72  Col ){.    asser
26983 74 28 20 6e 43 6f 6c 3e 31 20 29 3b 0a 20 20 20  t( nCol>1 );.   
26984 20 61 69 43 6f 6c 20 3d 20 28 69 6e 74 20 2a 29   aiCol = (int *)
26985 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
26986 61 77 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 6e  aw(pParse->db, n
26987 43 6f 6c 2a 73 69 7a 65 6f 66 28 69 6e 74 29 29  Col*sizeof(int))
26988 3b 0a 20 20 20 20 69 66 28 20 21 61 69 43 6f 6c  ;.    if( !aiCol
26989 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20   ) return 1;.   
2698a 20 2a 70 61 69 43 6f 6c 20 3d 20 61 69 43 6f 6c   *paiCol = aiCol
2698b 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 70 49 64  ;.  }..  for(pId
2698c 78 3d 70 50 61 72 65 6e 74 2d 3e 70 49 6e 64 65  x=pParent->pInde
2698d 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49  x; pIdx; pIdx=pI
2698e 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  dx->pNext){.    
2698f 69 66 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  if( pIdx->nColum
26990 6e 3d 3d 6e 43 6f 6c 20 26 26 20 70 49 64 78 2d  n==nCol && pIdx-
26991 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e  >onError!=OE_Non
26992 65 20 29 7b 20 0a 20 20 20 20 20 20 2f 2a 20 70  e ){ .      /* p
26993 49 64 78 20 69 73 20 61 20 55 4e 49 51 55 45 20  Idx is a UNIQUE 
26994 69 6e 64 65 78 20 28 6f 72 20 61 20 50 52 49 4d  index (or a PRIM
26995 41 52 59 20 4b 45 59 29 20 61 6e 64 20 68 61 73  ARY KEY) and has
26996 20 74 68 65 20 72 69 67 68 74 20 6e 75 6d 62 65   the right numbe
26997 72 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 63 6f  r.      ** of co
26998 6c 75 6d 6e 73 2e 20 49 66 20 65 61 63 68 20 69  lumns. If each i
26999 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 63 6f  ndexed column co
2699a 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 61 20 66  rresponds to a f
2699b 6f 72 65 69 67 6e 20 6b 65 79 0a 20 20 20 20 20  oreign key.     
2699c 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 66 20 70 46   ** column of pF
2699d 4b 65 79 2c 20 74 68 65 6e 20 74 68 69 73 20 69  Key, then this i
2699e 6e 64 65 78 20 69 73 20 61 20 77 69 6e 6e 65 72  ndex is a winner
2699f 2e 20 20 2a 2f 0a 0a 20 20 20 20 20 20 69 66 28  .  */..      if(
269a0 20 7a 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20   zKey==0 ){.    
269a1 20 20 20 20 2f 2a 20 49 66 20 7a 4b 65 79 20 69      /* If zKey i
269a2 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 69  s NULL, then thi
269a3 73 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73  s foreign key is
269a4 20 69 6d 70 6c 69 63 69 74 6c 79 20 6d 61 70 70   implicitly mapp
269a5 65 64 20 74 6f 20 0a 20 20 20 20 20 20 20 20 2a  ed to .        *
269a6 2a 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45  * the PRIMARY KE
269a7 59 20 6f 66 20 74 61 62 6c 65 20 70 50 61 72 65  Y of table pPare
269a8 6e 74 2e 20 54 68 65 20 50 52 49 4d 41 52 59 20  nt. The PRIMARY 
269a9 4b 45 59 20 69 6e 64 65 78 20 6d 61 79 20 62 65  KEY index may be
269aa 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 64 65   .        ** ide
269ab 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 74  ntified by the t
269ac 65 73 74 20 28 49 6e 64 65 78 2e 61 75 74 6f 49  est (Index.autoI
269ad 6e 64 65 78 3d 3d 32 29 2e 20 20 2a 2f 0a 20 20  ndex==2).  */.  
269ae 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e        if( pIdx->
269af 61 75 74 6f 49 6e 64 65 78 3d 3d 32 20 29 7b 0a  autoIndex==2 ){.
269b0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61 69            if( ai
269b1 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Col ){.         
269b2 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
269b3 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
269b4 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 20 61 69 43 6f  <nCol; i++) aiCo
269b5 6c 5b 69 5d 20 3d 20 70 46 4b 65 79 2d 3e 61 43  l[i] = pFKey->aC
269b6 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 3b 0a 20 20 20  ol[i].iFrom;.   
269b7 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
269b8 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
269b9 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
269ba 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 7a  .        /* If z
269bb 4b 65 79 20 69 73 20 6e 6f 6e 2d 4e 55 4c 4c 2c  Key is non-NULL,
269bc 20 74 68 65 6e 20 74 68 69 73 20 66 6f 72 65 69   then this forei
269bd 67 6e 20 6b 65 79 20 77 61 73 20 64 65 63 6c 61  gn key was decla
269be 72 65 64 20 74 6f 0a 20 20 20 20 20 20 20 20 2a  red to.        *
269bf 2a 20 6d 61 70 20 74 6f 20 61 6e 20 65 78 70 6c  * map to an expl
269c0 69 63 69 74 20 6c 69 73 74 20 6f 66 20 63 6f 6c  icit list of col
269c1 75 6d 6e 73 20 69 6e 20 74 61 62 6c 65 20 70 50  umns in table pP
269c2 61 72 65 6e 74 2e 20 43 68 65 63 6b 20 69 66 20  arent. Check if 
269c3 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  this.        ** 
269c4 69 6e 64 65 78 20 6d 61 74 63 68 65 73 20 74 68  index matches th
269c5 6f 73 65 20 63 6f 6c 75 6d 6e 73 2e 20 41 6c 73  ose columns. Als
269c6 6f 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74 68  o, check that th
269c7 65 20 69 6e 64 65 78 20 75 73 65 73 0a 20 20 20  e index uses.   
269c8 20 20 20 20 20 2a 2a 20 74 68 65 20 64 65 66 61       ** the defa
269c9 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  ult collation se
269ca 71 75 65 6e 63 65 73 20 66 6f 72 20 65 61 63 68  quences for each
269cb 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 20 20   column. */.    
269cc 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20      int i, j;.  
269cd 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
269ce 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nCol; i++){.   
269cf 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20         int iCol 
269d0 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  = pIdx->aiColumn
269d1 5b 69 5d 3b 20 20 20 20 20 2f 2a 20 49 6e 64 65  [i];     /* Inde
269d2 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 70  x of column in p
269d3 61 72 65 6e 74 20 74 62 6c 20 2a 2f 0a 20 20 20  arent tbl */.   
269d4 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 44 66         char *zDf
269d5 6c 74 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20  ltColl;         
269d6 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 2e           /* Def.
269d7 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 6f 72 20 63   collation for c
269d8 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 20  olumn */.       
269d9 20 20 20 63 68 61 72 20 2a 7a 49 64 78 43 6f 6c     char *zIdxCol
269da 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
269db 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
269dc 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 2a  indexed column *
269dd 2f 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  /..          /* 
269de 49 66 20 74 68 65 20 69 6e 64 65 78 20 75 73 65  If the index use
269df 73 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  s a collation se
269e0 71 75 65 6e 63 65 20 74 68 61 74 20 69 73 20 64  quence that is d
269e1 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a 20 20  ifferent from.  
269e2 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64          ** the d
269e3 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e  efault collation
269e4 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68   sequence for th
269e5 65 20 63 6f 6c 75 6d 6e 2c 20 74 68 69 73 20 69  e column, this i
269e6 6e 64 65 78 20 69 73 0a 20 20 20 20 20 20 20 20  ndex is.        
269e7 20 20 2a 2a 20 75 6e 75 73 61 62 6c 65 2e 20 42    ** unusable. B
269e8 61 69 6c 20 6f 75 74 20 65 61 72 6c 79 20 69 6e  ail out early in
269e9 20 74 68 69 73 20 63 61 73 65 2e 20 20 2a 2f 0a   this case.  */.
269ea 20 20 20 20 20 20 20 20 20 20 7a 44 66 6c 74 43            zDfltC
269eb 6f 6c 6c 20 3d 20 70 50 61 72 65 6e 74 2d 3e 61  oll = pParent->a
269ec 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 43 6f 6c 6c 3b  Col[iCol].zColl;
269ed 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21  .          if( !
269ee 7a 44 66 6c 74 43 6f 6c 6c 20 29 7b 0a 20 20 20  zDfltColl ){.   
269ef 20 20 20 20 20 20 20 20 20 7a 44 66 6c 74 43 6f           zDfltCo
269f0 6c 6c 20 3d 20 22 42 49 4e 41 52 59 22 3b 0a 20  ll = "BINARY";. 
269f1 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
269f2 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
269f3 53 74 72 49 43 6d 70 28 70 49 64 78 2d 3e 61 7a  StrICmp(pIdx->az
269f4 43 6f 6c 6c 5b 69 5d 2c 20 7a 44 66 6c 74 43 6f  Coll[i], zDfltCo
269f5 6c 6c 29 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20  ll) ) break;..  
269f6 20 20 20 20 20 20 20 20 7a 49 64 78 43 6f 6c 20          zIdxCol 
269f7 3d 20 70 50 61 72 65 6e 74 2d 3e 61 43 6f 6c 5b  = pParent->aCol[
269f8 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  iCol].zName;.   
269f9 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20         for(j=0; 
269fa 6a 3c 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20  j<nCol; j++){.  
269fb 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71            if( sq
269fc 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 46 4b  lite3StrICmp(pFK
269fd 65 79 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c  ey->aCol[j].zCol
269fe 2c 20 7a 49 64 78 43 6f 6c 29 3d 3d 30 20 29 7b  , zIdxCol)==0 ){
269ff 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  .              i
26a00 66 28 20 61 69 43 6f 6c 20 29 20 61 69 43 6f 6c  f( aiCol ) aiCol
26a01 5b 69 5d 20 3d 20 70 46 4b 65 79 2d 3e 61 43 6f  [i] = pFKey->aCo
26a02 6c 5b 6a 5d 2e 69 46 72 6f 6d 3b 0a 20 20 20 20  l[j].iFrom;.    
26a03 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
26a04 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
26a05 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
26a06 20 20 20 20 20 69 66 28 20 6a 3d 3d 6e 43 6f 6c       if( j==nCol
26a07 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
26a08 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
26a09 69 3d 3d 6e 43 6f 6c 20 29 20 62 72 65 61 6b 3b  i==nCol ) break;
26a0a 20 20 20 20 20 20 2f 2a 20 70 49 64 78 20 69 73        /* pIdx is
26a0b 20 75 73 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20   usable */.     
26a0c 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
26a0d 69 66 28 20 21 70 49 64 78 20 29 7b 0a 20 20 20  if( !pIdx ){.   
26a0e 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e 64 69   if( !pParse->di
26a0f 73 61 62 6c 65 54 72 69 67 67 65 72 73 20 29 7b  sableTriggers ){
26a10 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
26a11 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
26a12 66 6f 72 65 69 67 6e 20 6b 65 79 20 6d 69 73 6d  foreign key mism
26a13 61 74 63 68 22 29 3b 0a 20 20 20 20 7d 0a 20 20  atch");.    }.  
26a14 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
26a15 70 50 61 72 73 65 2d 3e 64 62 2c 20 61 69 43 6f  pParse->db, aiCo
26a16 6c 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  l);.    return 1
26a17 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 49 64 78 20  ;.  }..  *ppIdx 
26a18 3d 20 70 49 64 78 3b 0a 20 20 72 65 74 75 72 6e  = pIdx;.  return
26a19 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69   0;.}../*.** Thi
26a1a 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
26a1b 6c 6c 65 64 20 77 68 65 6e 20 61 20 72 6f 77 20  lled when a row 
26a1c 69 73 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  is inserted into
26a1d 20 6f 72 20 64 65 6c 65 74 65 64 20 66 72 6f 6d   or deleted from
26a1e 20 74 68 65 20 0a 2a 2a 20 63 68 69 6c 64 20 74   the .** child t
26a1f 61 62 6c 65 20 6f 66 20 66 6f 72 65 69 67 6e 20  able of foreign 
26a20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 70  key constraint p
26a21 46 4b 65 79 2e 20 49 66 20 61 6e 20 53 51 4c 20  FKey. If an SQL 
26a22 55 50 44 41 54 45 20 69 73 20 65 78 65 63 75 74  UPDATE is execut
26a23 65 64 20 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 68  ed .** on the ch
26a24 69 6c 64 20 74 61 62 6c 65 20 6f 66 20 70 46 4b  ild table of pFK
26a25 65 79 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ey, this functio
26a26 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 77 69  n is invoked twi
26a27 63 65 20 66 6f 72 20 65 61 63 68 20 72 6f 77 0a  ce for each row.
26a28 2a 2a 20 61 66 66 65 63 74 65 64 20 2d 20 6f 6e  ** affected - on
26a29 63 65 20 74 6f 20 22 64 65 6c 65 74 65 22 20 74  ce to "delete" t
26a2a 68 65 20 6f 6c 64 20 72 6f 77 2c 20 61 6e 64 20  he old row, and 
26a2b 74 68 65 6e 20 61 67 61 69 6e 20 74 6f 20 22 69  then again to "i
26a2c 6e 73 65 72 74 22 20 74 68 65 0a 2a 2a 20 6e 65  nsert" the.** ne
26a2d 77 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 45 61 63  w row..**.** Eac
26a2e 68 20 74 69 6d 65 20 69 74 20 69 73 20 63 61 6c  h time it is cal
26a2f 6c 65 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69  led, this functi
26a30 6f 6e 20 67 65 6e 65 72 61 74 65 73 20 56 44 42  on generates VDB
26a31 45 20 63 6f 64 65 20 74 6f 20 6c 6f 63 61 74 65  E code to locate
26a32 20 74 68 65 0a 2a 2a 20 72 6f 77 20 69 6e 20 74   the.** row in t
26a33 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20  he parent table 
26a34 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73  that corresponds
26a35 20 74 6f 20 74 68 65 20 72 6f 77 20 62 65 69 6e   to the row bein
26a36 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  g inserted into 
26a37 0a 2a 2a 20 6f 72 20 64 65 6c 65 74 65 64 20 66  .** or deleted f
26a38 72 6f 6d 20 74 68 65 20 63 68 69 6c 64 20 74 61  rom the child ta
26a39 62 6c 65 2e 20 49 66 20 74 68 65 20 70 61 72 65  ble. If the pare
26a3a 6e 74 20 72 6f 77 20 63 61 6e 20 62 65 20 66 6f  nt row can be fo
26a3b 75 6e 64 2c 20 6e 6f 20 0a 2a 2a 20 73 70 65 63  und, no .** spec
26a3c 69 61 6c 20 61 63 74 69 6f 6e 20 69 73 20 74 61  ial action is ta
26a3d 6b 65 6e 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ken. Otherwise, 
26a3e 69 66 20 74 68 65 20 70 61 72 65 6e 74 20 72 6f  if the parent ro
26a3f 77 20 63 61 6e 20 2a 6e 6f 74 2a 20 62 65 0a 2a  w can *not* be.*
26a40 2a 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 70  * found in the p
26a41 61 72 65 6e 74 20 74 61 62 6c 65 3a 0a 2a 2a 0a  arent table:.**.
26a42 2a 2a 20 20 20 4f 70 65 72 61 74 69 6f 6e 20 7c  **   Operation |
26a43 20 46 4b 20 74 79 70 65 20 20 20 7c 20 41 63 74   FK type   | Act
26a44 69 6f 6e 20 74 61 6b 65 6e 0a 2a 2a 20 20 20 2d  ion taken.**   -
26a45 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26a46 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26a47 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26a48 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26a49 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 49  ---------.**   I
26a4a 4e 53 45 52 54 20 20 20 20 20 20 69 6d 6d 65 64  NSERT      immed
26a4b 69 61 74 65 20 20 20 49 6e 63 72 65 6d 65 6e 74  iate   Increment
26a4c 20 74 68 65 20 22 69 6d 6d 65 64 69 61 74 65 20   the "immediate 
26a4d 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74  constraint count
26a4e 65 72 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 45 4c  er"..**.**   DEL
26a4f 45 54 45 20 20 20 20 20 20 69 6d 6d 65 64 69 61  ETE      immedia
26a50 74 65 20 20 20 44 65 63 72 65 6d 65 6e 74 20 74  te   Decrement t
26a51 68 65 20 22 69 6d 6d 65 64 69 61 74 65 20 63 6f  he "immediate co
26a52 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 65 72  nstraint counter
26a53 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 53 45 52  "..**.**   INSER
26a54 54 20 20 20 20 20 20 64 65 66 65 72 72 65 64 20  T      deferred 
26a55 20 20 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65     Increment the
26a56 20 22 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74   "deferred const
26a57 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a  raint counter"..
26a58 2a 2a 0a 2a 2a 20 20 20 44 45 4c 45 54 45 20 20  **.**   DELETE  
26a59 20 20 20 20 64 65 66 65 72 72 65 64 20 20 20 20      deferred    
26a5a 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 22 64  Decrement the "d
26a5b 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69  eferred constrai
26a5c 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a 2a 2a 0a  nt counter"..**.
26a5d 2a 2a 20 54 68 65 73 65 20 6f 70 65 72 61 74 69  ** These operati
26a5e 6f 6e 73 20 61 72 65 20 69 64 65 6e 74 69 66 69  ons are identifi
26a5f 65 64 20 69 6e 20 74 68 65 20 63 6f 6d 6d 65 6e  ed in the commen
26a60 74 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20  t at the top of 
26a61 74 68 69 73 20 66 69 6c 65 20 0a 2a 2a 20 28 66  this file .** (f
26a62 6b 65 79 2e 63 29 20 61 73 20 22 49 2e 31 22 20  key.c) as "I.1" 
26a63 61 6e 64 20 22 44 2e 31 22 2e 0a 2a 2f 0a 73 74  and "D.1"..*/.st
26a64 61 74 69 63 20 76 6f 69 64 20 66 6b 4c 6f 6f 6b  atic void fkLook
26a65 75 70 50 61 72 65 6e 74 28 0a 20 20 50 61 72 73  upParent(.  Pars
26a66 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
26a67 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65    /* Parse conte
26a68 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c  xt */.  int iDb,
26a69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
26a6a 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61 62 61   Index of databa
26a6b 73 65 20 68 6f 75 73 69 6e 67 20 70 54 61 62 20  se housing pTab 
26a6c 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
26a6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  ,          /* Pa
26a6e 72 65 6e 74 20 74 61 62 6c 65 20 6f 66 20 46 4b  rent table of FK
26a6f 20 70 46 4b 65 79 20 2a 2f 0a 20 20 49 6e 64 65   pFKey */.  Inde
26a70 78 20 2a 70 49 64 78 2c 20 20 20 20 20 20 20 20  x *pIdx,        
26a71 20 20 2f 2a 20 55 6e 69 71 75 65 20 69 6e 64 65    /* Unique inde
26a72 78 20 6f 6e 20 70 61 72 65 6e 74 20 6b 65 79 20  x on parent key 
26a73 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 54 61 62 20  columns in pTab 
26a74 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79  */.  FKey *pFKey
26a75 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f  ,          /* Fo
26a76 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
26a77 61 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  aint */.  int *a
26a78 69 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20  iCol,           
26a79 2f 2a 20 4d 61 70 20 66 72 6f 6d 20 70 61 72 65  /* Map from pare
26a7a 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 74  nt key columns t
26a7b 6f 20 63 68 69 6c 64 20 74 61 62 6c 65 20 63 6f  o child table co
26a7c 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 72  lumns */.  int r
26a7d 65 67 44 61 74 61 2c 20 20 20 20 20 20 20 20 20  egData,         
26a7e 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61   /* Address of a
26a7f 72 72 61 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20  rray containing 
26a80 63 68 69 6c 64 20 74 61 62 6c 65 20 72 6f 77 20  child table row 
26a81 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 63 72 2c 20  */.  int nIncr, 
26a82 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
26a83 63 72 65 6d 65 6e 74 20 63 6f 6e 73 74 72 61 69  crement constrai
26a84 6e 74 20 63 6f 75 6e 74 65 72 20 62 79 20 74 68  nt counter by th
26a85 69 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 67  is */.  int isIg
26a86 6e 6f 72 65 20 20 20 20 20 20 20 20 20 20 2f 2a  nore          /*
26a87 20 49 66 20 74 72 75 65 2c 20 70 72 65 74 65 6e   If true, preten
26a88 64 20 70 54 61 62 20 63 6f 6e 74 61 69 6e 73 20  d pTab contains 
26a89 61 6c 6c 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20  all NULL values 
26a8a 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20  */.){.  int i;  
26a8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26a8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26a8d 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61    /* Iterator va
26a8e 72 69 61 62 6c 65 20 2a 2f 0a 20 20 56 64 62 65  riable */.  Vdbe
26a8f 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
26a90 56 64 62 65 28 70 50 61 72 73 65 29 3b 20 20 20  Vdbe(pParse);   
26a91 20 20 20 20 20 20 2f 2a 20 56 64 62 65 20 74 6f        /* Vdbe to
26a92 20 61 64 64 20 63 6f 64 65 20 74 6f 20 2a 2f 0a   add code to */.
26a93 20 20 69 6e 74 20 69 43 75 72 20 3d 20 70 50 61    int iCur = pPa
26a94 72 73 65 2d 3e 6e 54 61 62 20 2d 20 31 3b 20 20  rse->nTab - 1;  
26a95 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
26a96 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 74 6f 20  ursor number to 
26a97 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 6b  use */.  int iOk
26a98 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
26a99 6b 65 4c 61 62 65 6c 28 76 29 3b 20 20 20 20 20  keLabel(v);     
26a9a 20 20 20 2f 2a 20 6a 75 6d 70 20 68 65 72 65 20     /* jump here 
26a9b 69 66 20 70 61 72 65 6e 74 20 6b 65 79 20 66 6f  if parent key fo
26a9c 75 6e 64 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20  und */..  /* If 
26a9d 6e 49 6e 63 72 20 69 73 20 6c 65 73 73 20 74 68  nIncr is less th
26a9e 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 63 68  an zero, then ch
26a9f 65 63 6b 20 61 74 20 72 75 6e 74 69 6d 65 20 69  eck at runtime i
26aa0 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 0a  f there are any.
26aa1 20 20 2a 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67    ** outstanding
26aa2 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 74 6f 20   constraints to 
26aa3 72 65 73 6f 6c 76 65 2e 20 49 66 20 74 68 65 72  resolve. If ther
26aa4 65 20 61 72 65 20 6e 6f 74 2c 20 74 68 65 72 65  e are not, there
26aa5 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 2a 2a   is no need.  **
26aa6 20 74 6f 20 63 68 65 63 6b 20 69 66 20 64 65 6c   to check if del
26aa7 65 74 69 6e 67 20 74 68 69 73 20 72 6f 77 20 72  eting this row r
26aa8 65 73 6f 6c 76 65 73 20 61 6e 79 20 6f 75 74 73  esolves any outs
26aa9 74 61 6e 64 69 6e 67 20 76 69 6f 6c 61 74 69 6f  tanding violatio
26aaa 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 68  ns..  **.  ** Ch
26aab 65 63 6b 20 69 66 20 61 6e 79 20 6f 66 20 74 68  eck if any of th
26aac 65 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 69 6e  e key columns in
26aad 20 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65   the child table
26aae 20 72 6f 77 20 61 72 65 20 4e 55 4c 4c 2e 20 49   row are NULL. I
26aaf 66 20 0a 20 20 2a 2a 20 61 6e 79 20 61 72 65 2c  f .  ** any are,
26ab0 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 72   then the constr
26ab1 61 69 6e 74 20 69 73 20 63 6f 6e 73 69 64 65 72  aint is consider
26ab2 65 64 20 73 61 74 69 73 66 69 65 64 2e 20 4e 6f  ed satisfied. No
26ab3 20 6e 65 65 64 20 74 6f 20 0a 20 20 2a 2a 20 73   need to .  ** s
26ab4 65 61 72 63 68 20 66 6f 72 20 61 20 6d 61 74 63  earch for a matc
26ab5 68 69 6e 67 20 72 6f 77 20 69 6e 20 74 68 65 20  hing row in the 
26ab6 70 61 72 65 6e 74 20 74 61 62 6c 65 2e 20 20 2a  parent table.  *
26ab7 2f 0a 20 20 69 66 28 20 6e 49 6e 63 72 3c 30 20  /.  if( nIncr<0 
26ab8 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
26ab9 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46  beAddOp2(v, OP_F
26aba 6b 49 66 5a 65 72 6f 2c 20 70 46 4b 65 79 2d 3e  kIfZero, pFKey->
26abb 69 73 44 65 66 65 72 72 65 64 2c 20 69 4f 6b 29  isDeferred, iOk)
26abc 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b  ;.  }.  for(i=0;
26abd 20 69 3c 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 20   i<pFKey->nCol; 
26abe 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 69 52  i++){.    int iR
26abf 65 67 20 3d 20 61 69 43 6f 6c 5b 69 5d 20 2b 20  eg = aiCol[i] + 
26ac0 72 65 67 44 61 74 61 20 2b 20 31 3b 0a 20 20 20  regData + 1;.   
26ac1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26ac2 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c  p2(v, OP_IsNull,
26ac3 20 69 52 65 67 2c 20 69 4f 6b 29 3b 0a 20 20 7d   iReg, iOk);.  }
26ac4 0a 0a 20 20 69 66 28 20 69 73 49 67 6e 6f 72 65  ..  if( isIgnore
26ac5 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ==0 ){.    if( p
26ac6 49 64 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Idx==0 ){.      
26ac7 2f 2a 20 49 66 20 70 49 64 78 20 69 73 20 4e 55  /* If pIdx is NU
26ac8 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 70 61 72  LL, then the par
26ac9 65 6e 74 20 6b 65 79 20 69 73 20 74 68 65 20 49  ent key is the I
26aca 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
26acb 45 59 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75  EY.      ** colu
26acc 6d 6e 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74  mn of the parent
26acd 20 74 61 62 6c 65 20 28 74 61 62 6c 65 20 70 54   table (table pT
26ace 61 62 29 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69  ab).  */.      i
26acf 6e 74 20 69 4d 75 73 74 42 65 49 6e 74 3b 20 20  nt iMustBeInt;  
26ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26ad1 41 64 64 72 65 73 73 20 6f 66 20 4d 75 73 74 42  Address of MustB
26ad2 65 49 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  eInt instruction
26ad3 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 65   */.      int re
26ad4 67 54 65 6d 70 20 3d 20 73 71 6c 69 74 65 33 47  gTemp = sqlite3G
26ad5 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
26ad6 29 3b 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 49  );.  .      /* I
26ad7 6e 76 6f 6b 65 20 4d 75 73 74 42 65 49 6e 74 20  nvoke MustBeInt 
26ad8 74 6f 20 63 6f 65 72 63 65 20 74 68 65 20 63 68  to coerce the ch
26ad9 69 6c 64 20 6b 65 79 20 76 61 6c 75 65 20 74 6f  ild key value to
26ada 20 61 6e 20 69 6e 74 65 67 65 72 20 28 69 2e 65   an integer (i.e
26adb 2e 20 0a 20 20 20 20 20 20 2a 2a 20 61 70 70 6c  . .      ** appl
26adc 79 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f  y the affinity o
26add 66 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79  f the parent key
26ade 29 2e 20 49 66 20 74 68 69 73 20 66 61 69 6c 73  ). If this fails
26adf 2c 20 74 68 65 6e 20 74 68 65 72 65 0a 20 20 20  , then there.   
26ae0 20 20 20 2a 2a 20 69 73 20 6e 6f 20 6d 61 74 63     ** is no matc
26ae1 68 69 6e 67 20 70 61 72 65 6e 74 20 6b 65 79 2e  hing parent key.
26ae2 20 42 65 66 6f 72 65 20 75 73 69 6e 67 20 4d 75   Before using Mu
26ae3 73 74 42 65 49 6e 74 2c 20 6d 61 6b 65 20 61 20  stBeInt, make a 
26ae4 63 6f 70 79 20 6f 66 0a 20 20 20 20 20 20 2a 2a  copy of.      **
26ae5 20 74 68 65 20 76 61 6c 75 65 2e 20 4f 74 68 65   the value. Othe
26ae6 72 77 69 73 65 2c 20 74 68 65 20 76 61 6c 75 65  rwise, the value
26ae7 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74   inserted into t
26ae8 68 65 20 63 68 69 6c 64 20 6b 65 79 20 63 6f 6c  he child key col
26ae9 75 6d 6e 0a 20 20 20 20 20 20 2a 2a 20 77 69 6c  umn.      ** wil
26aea 6c 20 68 61 76 65 20 49 4e 54 45 47 45 52 20 61  l have INTEGER a
26aeb 66 66 69 6e 69 74 79 20 61 70 70 6c 69 65 64 20  ffinity applied 
26aec 74 6f 20 69 74 2c 20 77 68 69 63 68 20 6d 61 79  to it, which may
26aed 20 6e 6f 74 20 62 65 20 63 6f 72 72 65 63 74 2e   not be correct.
26aee 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74    */.      sqlit
26aef 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
26af0 4f 50 5f 53 43 6f 70 79 2c 20 61 69 43 6f 6c 5b  OP_SCopy, aiCol[
26af1 30 5d 2b 31 2b 72 65 67 44 61 74 61 2c 20 72 65  0]+1+regData, re
26af2 67 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 69 4d  gTemp);.      iM
26af3 75 73 74 42 65 49 6e 74 20 3d 20 73 71 6c 69 74  ustBeInt = sqlit
26af4 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
26af5 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72 65  OP_MustBeInt, re
26af6 67 54 65 6d 70 2c 20 30 29 3b 0a 20 20 0a 20 20  gTemp, 0);.  .  
26af7 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61      /* If the pa
26af8 72 65 6e 74 20 74 61 62 6c 65 20 69 73 20 74 68  rent table is th
26af9 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 63 68  e same as the ch
26afa 69 6c 64 20 74 61 62 6c 65 2c 20 61 6e 64 20 77  ild table, and w
26afb 65 20 61 72 65 20 61 62 6f 75 74 0a 20 20 20 20  e are about.    
26afc 20 20 2a 2a 20 74 6f 20 69 6e 63 72 65 6d 65 6e    ** to incremen
26afd 74 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  t the constraint
26afe 2d 63 6f 75 6e 74 65 72 20 28 69 2e 65 2e 20 74  -counter (i.e. t
26aff 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54  his is an INSERT
26b00 20 6f 70 65 72 61 74 69 6f 6e 29 2c 0a 20 20 20   operation),.   
26b01 20 20 20 2a 2a 20 74 68 65 6e 20 63 68 65 63 6b     ** then check
26b02 20 69 66 20 74 68 65 20 72 6f 77 20 62 65 69 6e   if the row bein
26b03 67 20 69 6e 73 65 72 74 65 64 20 6d 61 74 63 68  g inserted match
26b04 65 73 20 69 74 73 65 6c 66 2e 20 49 66 20 73 6f  es itself. If so
26b05 2c 20 64 6f 20 6e 6f 74 0a 20 20 20 20 20 20 2a  , do not.      *
26b06 2a 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20  * increment the 
26b07 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e 74  constraint-count
26b08 65 72 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66  er.  */.      if
26b09 28 20 70 54 61 62 3d 3d 70 46 4b 65 79 2d 3e 70  ( pTab==pFKey->p
26b0a 46 72 6f 6d 20 26 26 20 6e 49 6e 63 72 3d 3d 31  From && nIncr==1
26b0b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
26b0c 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
26b0d 20 4f 50 5f 45 71 2c 20 72 65 67 44 61 74 61 2c   OP_Eq, regData,
26b0e 20 69 4f 6b 2c 20 72 65 67 54 65 6d 70 29 3b 0a   iOk, regTemp);.
26b0f 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20        }.  .     
26b10 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c   sqlite3OpenTabl
26b11 65 28 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20  e(pParse, iCur, 
26b12 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70  iDb, pTab, OP_Op
26b13 65 6e 52 65 61 64 29 3b 0a 20 20 20 20 20 20 73  enRead);.      s
26b14 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
26b15 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  (v, OP_NotExists
26b16 2c 20 69 43 75 72 2c 20 30 2c 20 72 65 67 54 65  , iCur, 0, regTe
26b17 6d 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  mp);.      sqlit
26b18 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
26b19 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 4f 6b 29  OP_Goto, 0, iOk)
26b1a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
26b1b 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 73  dbeJumpHere(v, s
26b1c 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
26b1d 74 41 64 64 72 28 76 29 2d 32 29 3b 0a 20 20 20  tAddr(v)-2);.   
26b1e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
26b1f 6d 70 48 65 72 65 28 76 2c 20 69 4d 75 73 74 42  mpHere(v, iMustB
26b20 65 49 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  eInt);.      sql
26b21 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
26b22 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 54 65  eg(pParse, regTe
26b23 6d 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  mp);.    }else{.
26b24 20 20 20 20 20 20 69 6e 74 20 6e 43 6f 6c 20 3d        int nCol =
26b25 20 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 0a 20 20   pFKey->nCol;.  
26b26 20 20 20 20 69 6e 74 20 72 65 67 54 65 6d 70 20      int regTemp 
26b27 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
26b28 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 43  Range(pParse, nC
26b29 6f 6c 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 72  ol);.      int r
26b2a 65 67 52 65 63 20 3d 20 73 71 6c 69 74 65 33 47  egRec = sqlite3G
26b2b 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
26b2c 29 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f  );.      KeyInfo
26b2d 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33   *pKey = sqlite3
26b2e 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61  IndexKeyinfo(pPa
26b2f 72 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 0a 20  rse, pIdx);.  . 
26b30 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26b31 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65  AddOp3(v, OP_Ope
26b32 6e 52 65 61 64 2c 20 69 43 75 72 2c 20 70 49 64  nRead, iCur, pId
26b33 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20  x->tnum, iDb);. 
26b34 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26b35 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20  ChangeP4(v, -1, 
26b36 28 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f  (char*)pKey, P4_
26b37 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
26b38 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
26b39 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20   i<nCol; i++){. 
26b3a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
26b3b 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
26b3c 43 6f 70 79 2c 20 61 69 43 6f 6c 5b 69 5d 2b 31  Copy, aiCol[i]+1
26b3d 2b 72 65 67 44 61 74 61 2c 20 72 65 67 54 65 6d  +regData, regTem
26b3e 70 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  p+i);.      }.  
26b3f 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
26b40 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 69 73   parent table is
26b41 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
26b42 20 63 68 69 6c 64 20 74 61 62 6c 65 2c 20 61 6e   child table, an
26b43 64 20 77 65 20 61 72 65 20 61 62 6f 75 74 0a 20  d we are about. 
26b44 20 20 20 20 20 2a 2a 20 74 6f 20 69 6e 63 72 65       ** to incre
26b45 6d 65 6e 74 20 74 68 65 20 63 6f 6e 73 74 72 61  ment the constra
26b46 69 6e 74 2d 63 6f 75 6e 74 65 72 20 28 69 2e 65  int-counter (i.e
26b47 2e 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53  . this is an INS
26b48 45 52 54 20 6f 70 65 72 61 74 69 6f 6e 29 2c 0a  ERT operation),.
26b49 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 68        ** then ch
26b4a 65 63 6b 20 69 66 20 74 68 65 20 72 6f 77 20 62  eck if the row b
26b4b 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 6d 61  eing inserted ma
26b4c 74 63 68 65 73 20 69 74 73 65 6c 66 2e 20 49 66  tches itself. If
26b4d 20 73 6f 2c 20 64 6f 20 6e 6f 74 0a 20 20 20 20   so, do not.    
26b4e 20 20 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 20 74    ** increment t
26b4f 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f  he constraint-co
26b50 75 6e 74 65 72 2e 20 20 2a 2f 0a 20 20 20 20 20  unter.  */.     
26b51 20 69 66 28 20 70 54 61 62 3d 3d 70 46 4b 65 79   if( pTab==pFKey
26b52 2d 3e 70 46 72 6f 6d 20 26 26 20 6e 49 6e 63 72  ->pFrom && nIncr
26b53 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ==1 ){.        i
26b54 6e 74 20 69 4a 75 6d 70 20 3d 20 73 71 6c 69 74  nt iJump = sqlit
26b55 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
26b56 72 28 76 29 20 2b 20 6e 43 6f 6c 20 2b 20 31 3b  r(v) + nCol + 1;
26b57 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30  .        for(i=0
26b58 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ; i<nCol; i++){.
26b59 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 43            int iC
26b5a 68 69 6c 64 20 3d 20 61 69 43 6f 6c 5b 69 5d 2b  hild = aiCol[i]+
26b5b 31 2b 72 65 67 44 61 74 61 3b 0a 20 20 20 20 20  1+regData;.     
26b5c 20 20 20 20 20 69 6e 74 20 69 50 61 72 65 6e 74       int iParent
26b5d 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d   = pIdx->aiColum
26b5e 6e 5b 69 5d 2b 31 2b 72 65 67 44 61 74 61 3b 0a  n[i]+1+regData;.
26b5f 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
26b60 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
26b61 50 5f 4e 65 2c 20 69 43 68 69 6c 64 2c 20 69 4a  P_Ne, iChild, iJ
26b62 75 6d 70 2c 20 69 50 61 72 65 6e 74 29 3b 0a 20  ump, iParent);. 
26b63 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
26b64 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26b65 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
26b66 2c 20 69 4f 6b 29 3b 0a 20 20 20 20 20 20 7d 0a  , iOk);.      }.
26b67 20 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33    .      sqlite3
26b68 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
26b69 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
26b6a 54 65 6d 70 2c 20 6e 43 6f 6c 2c 20 72 65 67 52  Temp, nCol, regR
26b6b 65 63 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ec);.      sqlit
26b6c 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
26b6d 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 49 6e 64  , -1, sqlite3Ind
26b6e 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c  exAffinityStr(v,
26b6f 20 70 49 64 78 29 2c 20 30 29 3b 0a 20 20 20 20   pIdx), 0);.    
26b70 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26b71 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c  Op3(v, OP_Found,
26b72 20 69 43 75 72 2c 20 69 4f 6b 2c 20 72 65 67 52   iCur, iOk, regR
26b73 65 63 29 3b 0a 20 20 0a 20 20 20 20 20 20 73 71  ec);.  .      sq
26b74 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
26b75 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 52  Reg(pParse, regR
26b76 65 63 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ec);.      sqlit
26b77 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e  e3ReleaseTempRan
26b78 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 54 65  ge(pParse, regTe
26b79 6d 70 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20 20 7d  mp, nCol);.    }
26b7a 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 46 4b  .  }..  if( !pFK
26b7b 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64 20 26  ey->isDeferred &
26b7c 26 20 21 70 50 61 72 73 65 2d 3e 70 54 6f 70 6c  & !pParse->pTopl
26b7d 65 76 65 6c 20 26 26 20 21 70 50 61 72 73 65 2d  evel && !pParse-
26b7e 3e 69 73 4d 75 6c 74 69 57 72 69 74 65 20 29 7b  >isMultiWrite ){
26b7f 0a 20 20 20 20 2f 2a 20 53 70 65 63 69 61 6c 20  .    /* Special 
26b80 63 61 73 65 3a 20 49 66 20 74 68 69 73 20 69 73  case: If this is
26b81 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65   an INSERT state
26b82 6d 65 6e 74 20 74 68 61 74 20 77 69 6c 6c 20 69  ment that will i
26b83 6e 73 65 72 74 20 65 78 61 63 74 6c 79 0a 20 20  nsert exactly.  
26b84 20 20 2a 2a 20 6f 6e 65 20 72 6f 77 20 69 6e 74    ** one row int
26b85 6f 20 74 68 65 20 74 61 62 6c 65 2c 20 72 61 69  o the table, rai
26b86 73 65 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20  se a constraint 
26b87 69 6d 6d 65 64 69 61 74 65 6c 79 20 69 6e 73 74  immediately inst
26b88 65 61 64 20 6f 66 0a 20 20 20 20 2a 2a 20 69 6e  ead of.    ** in
26b89 63 72 65 6d 65 6e 74 69 6e 67 20 61 20 63 6f 75  crementing a cou
26b8a 6e 74 65 72 2e 20 54 68 69 73 20 69 73 20 6e 65  nter. This is ne
26b8b 63 65 73 73 61 72 79 20 61 73 20 74 68 65 20 56  cessary as the V
26b8c 4d 20 63 6f 64 65 20 69 73 20 62 65 69 6e 67 0a  M code is being.
26b8d 20 20 20 20 2a 2a 20 67 65 6e 65 72 61 74 65 64      ** generated
26b8e 20 66 6f 72 20 77 69 6c 6c 20 6e 6f 74 20 6f 70   for will not op
26b8f 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74  en a statement t
26b90 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 2a 2f 0a  ransaction.  */.
26b91 20 20 20 20 61 73 73 65 72 74 28 20 6e 49 6e 63      assert( nInc
26b92 72 3d 3d 31 20 29 3b 0a 20 20 20 20 73 71 6c 69  r==1 );.    sqli
26b93 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e  te3HaltConstrain
26b94 74 28 0a 20 20 20 20 20 20 20 20 70 50 61 72 73  t(.        pPars
26b95 65 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 22 66 6f  e, OE_Abort, "fo
26b96 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
26b97 61 69 6e 74 20 66 61 69 6c 65 64 22 2c 20 50 34  aint failed", P4
26b98 5f 53 54 41 54 49 43 0a 20 20 20 20 29 3b 0a 20  _STATIC.    );. 
26b99 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20   }else{.    if( 
26b9a 6e 49 6e 63 72 3e 30 20 26 26 20 70 46 4b 65 79  nIncr>0 && pFKey
26b9b 2d 3e 69 73 44 65 66 65 72 72 65 64 3d 3d 30 20  ->isDeferred==0 
26b9c 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
26b9d 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50  ParseToplevel(pP
26b9e 61 72 73 65 29 2d 3e 6d 61 79 41 62 6f 72 74 20  arse)->mayAbort 
26b9f 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  = 1;.    }.    s
26ba0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
26ba1 28 76 2c 20 4f 50 5f 46 6b 43 6f 75 6e 74 65 72  (v, OP_FkCounter
26ba2 2c 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72  , pFKey->isDefer
26ba3 72 65 64 2c 20 6e 49 6e 63 72 29 3b 0a 20 20 7d  red, nIncr);.  }
26ba4 0a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52  ..  sqlite3VdbeR
26ba5 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69  esolveLabel(v, i
26ba6 4f 6b 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  Ok);.  sqlite3Vd
26ba7 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43  beAddOp1(v, OP_C
26ba8 6c 6f 73 65 2c 20 69 43 75 72 29 3b 0a 7d 0a 0a  lose, iCur);.}..
26ba9 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
26baa 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  ion is called to
26bab 20 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20 65   generate code e
26bac 78 65 63 75 74 65 64 20 77 68 65 6e 20 61 20 72  xecuted when a r
26bad 6f 77 20 69 73 20 64 65 6c 65 74 65 64 0a 2a 2a  ow is deleted.**
26bae 20 66 72 6f 6d 20 74 68 65 20 70 61 72 65 6e 74   from the parent
26baf 20 74 61 62 6c 65 20 6f 66 20 66 6f 72 65 69 67   table of foreig
26bb0 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
26bb1 20 70 46 4b 65 79 20 61 6e 64 2c 20 69 66 20 70   pFKey and, if p
26bb2 46 4b 65 79 20 69 73 20 0a 2a 2a 20 64 65 66 65  FKey is .** defe
26bb3 72 72 65 64 2c 20 77 68 65 6e 20 61 20 72 6f 77  rred, when a row
26bb4 20 69 73 20 69 6e 73 65 72 74 65 64 20 69 6e 74   is inserted int
26bb5 6f 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65  o the same table
26bb6 2e 20 57 68 65 6e 20 67 65 6e 65 72 61 74 69 6e  . When generatin
26bb7 67 0a 2a 2a 20 63 6f 64 65 20 66 6f 72 20 61 6e  g.** code for an
26bb8 20 53 51 4c 20 55 50 44 41 54 45 20 6f 70 65 72   SQL UPDATE oper
26bb9 61 74 69 6f 6e 2c 20 74 68 69 73 20 66 75 6e 63  ation, this func
26bba 74 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c  tion may be call
26bbb 65 64 20 74 77 69 63 65 20 2d 0a 2a 2a 20 6f 6e  ed twice -.** on
26bbc 63 65 20 74 6f 20 22 64 65 6c 65 74 65 22 20 74  ce to "delete" t
26bbd 68 65 20 6f 6c 64 20 72 6f 77 20 61 6e 64 20 6f  he old row and o
26bbe 6e 63 65 20 74 6f 20 22 69 6e 73 65 72 74 22 20  nce to "insert" 
26bbf 74 68 65 20 6e 65 77 20 72 6f 77 2e 0a 2a 2a 0a  the new row..**.
26bc0 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65  ** The code gene
26bc1 72 61 74 65 64 20 62 79 20 74 68 69 73 20 66 75  rated by this fu
26bc2 6e 63 74 69 6f 6e 20 73 63 61 6e 73 20 74 68 72  nction scans thr
26bc3 6f 75 67 68 20 74 68 65 20 72 6f 77 73 20 69 6e  ough the rows in
26bc4 20 74 68 65 20 63 68 69 6c 64 0a 2a 2a 20 74 61   the child.** ta
26bc5 62 6c 65 20 74 68 61 74 20 63 6f 72 72 65 73 70  ble that corresp
26bc6 6f 6e 64 20 74 6f 20 74 68 65 20 70 61 72 65 6e  ond to the paren
26bc7 74 20 74 61 62 6c 65 20 72 6f 77 20 62 65 69 6e  t table row bein
26bc8 67 20 64 65 6c 65 74 65 64 20 6f 72 20 69 6e 73  g deleted or ins
26bc9 65 72 74 65 64 2e 0a 2a 2a 20 46 6f 72 20 65 61  erted..** For ea
26bca 63 68 20 63 68 69 6c 64 20 72 6f 77 20 66 6f 75  ch child row fou
26bcb 6e 64 2c 20 6f 6e 65 20 6f 66 20 74 68 65 20 66  nd, one of the f
26bcc 6f 6c 6c 6f 77 69 6e 67 20 61 63 74 69 6f 6e 73  ollowing actions
26bcd 20 69 73 20 74 61 6b 65 6e 3a 0a 2a 2a 0a 2a 2a   is taken:.**.**
26bce 20 20 20 4f 70 65 72 61 74 69 6f 6e 20 7c 20 46     Operation | F
26bcf 4b 20 74 79 70 65 20 20 20 7c 20 41 63 74 69 6f  K type   | Actio
26bd0 6e 20 74 61 6b 65 6e 0a 2a 2a 20 20 20 2d 2d 2d  n taken.**   ---
26bd1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26bd2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26bd3 2d 2d 2d 2d 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 0a 2a 2a 20 20 20 44 45 4c  -------.**   DEL
26bd6 45 54 45 20 20 20 20 20 20 69 6d 6d 65 64 69 61  ETE      immedia
26bd7 74 65 20 20 20 49 6e 63 72 65 6d 65 6e 74 20 74  te   Increment t
26bd8 68 65 20 22 69 6d 6d 65 64 69 61 74 65 20 63 6f  he "immediate co
26bd9 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 65 72  nstraint counter
26bda 22 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  "..**           
26bdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26bdc 4f 72 2c 20 69 66 20 74 68 65 20 4f 4e 20 28 55  Or, if the ON (U
26bdd 50 44 41 54 45 7c 44 45 4c 45 54 45 29 20 61 63  PDATE|DELETE) ac
26bde 74 69 6f 6e 20 69 73 20 52 45 53 54 52 49 43 54  tion is RESTRICT
26bdf 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ,.**            
26be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
26be1 68 72 6f 77 20 61 20 22 66 6f 72 65 69 67 6e 20  hrow a "foreign 
26be2 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66  key constraint f
26be3 61 69 6c 65 64 22 20 65 78 63 65 70 74 69 6f 6e  ailed" exception
26be4 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 53 45 52 54  ..**.**   INSERT
26be5 20 20 20 20 20 20 69 6d 6d 65 64 69 61 74 65 20        immediate 
26be6 20 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20    Decrement the 
26be7 22 69 6d 6d 65 64 69 61 74 65 20 63 6f 6e 73 74  "immediate const
26be8 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a  raint counter"..
26be9 2a 2a 0a 2a 2a 20 20 20 44 45 4c 45 54 45 20 20  **.**   DELETE  
26bea 20 20 20 20 64 65 66 65 72 72 65 64 20 20 20 20      deferred    
26beb 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 22 64  Increment the "d
26bec 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69  eferred constrai
26bed 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a 2a 2a 20  nt counter"..** 
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 20 20 20 4f 72 2c 20 69 66            Or, if
26bf0 20 74 68 65 20 4f 4e 20 28 55 50 44 41 54 45 7c   the ON (UPDATE|
26bf1 44 45 4c 45 54 45 29 20 61 63 74 69 6f 6e 20 69  DELETE) action i
26bf2 73 20 52 45 53 54 52 49 43 54 2c 0a 2a 2a 20 20  s RESTRICT,.**  
26bf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26bf4 20 20 20 20 20 20 20 20 20 74 68 72 6f 77 20 61           throw a
26bf5 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f   "foreign key co
26bf6 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 22  nstraint failed"
26bf7 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a   exception..**.*
26bf8 2a 20 20 20 49 4e 53 45 52 54 20 20 20 20 20 20  *   INSERT      
26bf9 64 65 66 65 72 72 65 64 20 20 20 20 44 65 63 72  deferred    Decr
26bfa 65 6d 65 6e 74 20 74 68 65 20 22 64 65 66 65 72  ement the "defer
26bfb 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 20 63  red constraint c
26bfc 6f 75 6e 74 65 72 22 2e 0a 2a 2a 0a 2a 2a 20 54  ounter"..**.** T
26bfd 68 65 73 65 20 6f 70 65 72 61 74 69 6f 6e 73 20  hese operations 
26bfe 61 72 65 20 69 64 65 6e 74 69 66 69 65 64 20 69  are identified i
26bff 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 61 74  n the comment at
26c00 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 69 73   the top of this
26c01 20 66 69 6c 65 20 0a 2a 2a 20 28 66 6b 65 79 2e   file .** (fkey.
26c02 63 29 20 61 73 20 22 49 2e 32 22 20 61 6e 64 20  c) as "I.2" and 
26c03 22 44 2e 32 22 2e 0a 2a 2f 0a 73 74 61 74 69 63  "D.2"..*/.static
26c04 20 76 6f 69 64 20 66 6b 53 63 61 6e 43 68 69 6c   void fkScanChil
26c05 64 72 65 6e 28 0a 20 20 50 61 72 73 65 20 2a 70  dren(.  Parse *p
26c06 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
26c07 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
26c08 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72   context */.  Sr
26c09 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20  cList *pSrc,    
26c0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
26c0b 20 53 72 63 4c 69 73 74 20 63 6f 6e 74 61 69 6e   SrcList contain
26c0c 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 74 6f  ing the table to
26c0d 20 73 63 61 6e 20 2a 2f 0a 20 20 54 61 62 6c 65   scan */.  Table
26c0e 20 2a 70 54 61 62 2c 0a 20 20 49 6e 64 65 78 20   *pTab,.  Index 
26c0f 2a 70 49 64 78 2c 20 20 20 20 20 20 20 20 20 20  *pIdx,          
26c10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
26c11 65 69 67 6e 20 6b 65 79 20 69 6e 64 65 78 20 2a  eign key index *
26c12 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 2c  /.  FKey *pFKey,
26c13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c14 20 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b      /* Foreign k
26c15 65 79 20 72 65 6c 61 74 69 6f 6e 73 68 69 70 20  ey relationship 
26c16 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 43 6f 6c 2c  */.  int *aiCol,
26c17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c18 20 20 20 20 20 2f 2a 20 4d 61 70 20 66 72 6f 6d       /* Map from
26c19 20 70 49 64 78 20 63 6f 6c 73 20 74 6f 20 63 68   pIdx cols to ch
26c1a 69 6c 64 20 74 61 62 6c 65 20 63 6f 6c 73 20 2a  ild table cols *
26c1b 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61 2c  /.  int regData,
26c1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c1d 20 20 20 20 2f 2a 20 52 65 66 65 72 65 6e 63 65      /* Reference
26c1e 64 20 74 61 62 6c 65 20 64 61 74 61 20 73 74 61  d table data sta
26c1f 72 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e  rts here */.  in
26c20 74 20 6e 49 6e 63 72 20 20 20 20 20 20 20 20 20  t nIncr         
26c21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
26c22 20 41 6d 6f 75 6e 74 20 74 6f 20 69 6e 63 72 65   Amount to incre
26c23 6d 65 6e 74 20 64 65 66 65 72 72 65 64 20 63 6f  ment deferred co
26c24 75 6e 74 65 72 20 62 79 20 2a 2f 0a 29 7b 0a 20  unter by */.){. 
26c25 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
26c26 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20  Parse->db;      
26c27 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
26c28 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20  dle */.  int i; 
26c29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c2a 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
26c2b 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f  ator variable */
26c2c 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 20  .  Expr *pWhere 
26c2d 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
26c2e 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c 61 75     /* WHERE clau
26c2f 73 65 20 74 6f 20 73 63 61 6e 20 77 69 74 68 20  se to scan with 
26c30 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  */.  NameContext
26c31 20 73 4e 61 6d 65 43 6f 6e 74 65 78 74 3b 20 20   sNameContext;  
26c32 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
26c33 75 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 20  used to resolve 
26c34 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
26c35 20 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49    WhereInfo *pWI
26c36 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  nfo;            
26c37 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 75 73 65    /* Context use
26c38 64 20 62 79 20 73 71 6c 69 74 65 33 57 68 65 72  d by sqlite3Wher
26c39 65 58 58 58 28 29 20 2a 2f 0a 20 20 69 6e 74 20  eXXX() */.  int 
26c3a 69 46 6b 49 66 5a 65 72 6f 20 3d 20 30 3b 20 20  iFkIfZero = 0;  
26c3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
26c3c 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 46 6b 49  ddress of OP_FkI
26c3d 66 5a 65 72 6f 20 2a 2f 0a 20 20 56 64 62 65 20  fZero */.  Vdbe 
26c3e 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  *v = sqlite3GetV
26c3f 64 62 65 28 70 50 61 72 73 65 29 3b 0a 0a 20 20  dbe(pParse);..  
26c40 61 73 73 65 72 74 28 20 21 70 49 64 78 20 7c 7c  assert( !pIdx ||
26c41 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 3d 3d 70   pIdx->pTable==p
26c42 54 61 62 20 29 3b 0a 0a 20 20 69 66 28 20 6e 49  Tab );..  if( nI
26c43 6e 63 72 3c 30 20 29 7b 0a 20 20 20 20 69 46 6b  ncr<0 ){.    iFk
26c44 49 66 5a 65 72 6f 20 3d 20 73 71 6c 69 74 65 33  IfZero = sqlite3
26c45 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
26c46 5f 46 6b 49 66 5a 65 72 6f 2c 20 70 46 4b 65 79  _FkIfZero, pFKey
26c47 2d 3e 69 73 44 65 66 65 72 72 65 64 2c 20 30 29  ->isDeferred, 0)
26c48 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61  ;.  }..  /* Crea
26c49 74 65 20 61 6e 20 45 78 70 72 20 6f 62 6a 65 63  te an Expr objec
26c4a 74 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 61  t representing a
26c4b 6e 20 53 51 4c 20 65 78 70 72 65 73 73 69 6f 6e  n SQL expression
26c4c 20 6c 69 6b 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a   like:.  **.  **
26c4d 20 20 20 3c 70 61 72 65 6e 74 2d 6b 65 79 31 3e     <parent-key1>
26c4e 20 3d 20 3c 63 68 69 6c 64 2d 6b 65 79 31 3e 20   = <child-key1> 
26c4f 41 4e 44 20 3c 70 61 72 65 6e 74 2d 6b 65 79 32  AND <parent-key2
26c50 3e 20 3d 20 3c 63 68 69 6c 64 2d 6b 65 79 32 3e  > = <child-key2>
26c51 20 2e 2e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54   ....  **.  ** T
26c52 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
26c53 75 65 6e 63 65 20 75 73 65 64 20 66 6f 72 20 74  uence used for t
26c54 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 73 68  he comparison sh
26c55 6f 75 6c 64 20 62 65 20 74 68 61 74 20 6f 66 0a  ould be that of.
26c56 20 20 2a 2a 20 74 68 65 20 70 61 72 65 6e 74 20    ** the parent 
26c57 6b 65 79 20 63 6f 6c 75 6d 6e 73 2e 20 54 68 65  key columns. The
26c58 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65   affinity of the
26c59 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75   parent key colu
26c5a 6d 6e 20 73 68 6f 75 6c 64 0a 20 20 2a 2a 20 62  mn should.  ** b
26c5b 65 20 61 70 70 6c 69 65 64 20 74 6f 20 65 61 63  e applied to eac
26c5c 68 20 63 68 69 6c 64 20 6b 65 79 20 76 61 6c 75  h child key valu
26c5d 65 20 62 65 66 6f 72 65 20 74 68 65 20 63 6f 6d  e before the com
26c5e 70 61 72 69 73 6f 6e 20 74 61 6b 65 73 20 70 6c  parison takes pl
26c5f 61 63 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  ace..  */.  for(
26c60 69 3d 30 3b 20 69 3c 70 46 4b 65 79 2d 3e 6e 43  i=0; i<pFKey->nC
26c61 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78  ol; i++){.    Ex
26c62 70 72 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 20  pr *pLeft;      
26c63 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
26c64 61 6c 75 65 20 66 72 6f 6d 20 70 61 72 65 6e 74  alue from parent
26c65 20 74 61 62 6c 65 20 72 6f 77 20 2a 2f 0a 20 20   table row */.  
26c66 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 3b 20    Expr *pRight; 
26c67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c68 2f 2a 20 43 6f 6c 75 6d 6e 20 72 65 66 20 74 6f  /* Column ref to
26c69 20 63 68 69 6c 64 20 74 61 62 6c 65 20 2a 2f 0a   child table */.
26c6a 20 20 20 20 45 78 70 72 20 2a 70 45 71 3b 20 20      Expr *pEq;  
26c6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26c6c 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20    /* Expression 
26c6d 28 70 4c 65 66 74 20 3d 20 70 52 69 67 68 74 29  (pLeft = pRight)
26c6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c   */.    int iCol
26c6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
26c70 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
26c71 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 63 68 69 6c  f column in chil
26c72 64 20 74 61 62 6c 65 20 2a 2f 20 0a 20 20 20 20  d table */ .    
26c73 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c  const char *zCol
26c74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
26c75 20 4e 61 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e 20   Name of column 
26c76 69 6e 20 63 68 69 6c 64 20 74 61 62 6c 65 20 2a  in child table *
26c77 2f 0a 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 73  /..    pLeft = s
26c78 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20 54  qlite3Expr(db, T
26c79 4b 5f 52 45 47 49 53 54 45 52 2c 20 30 29 3b 0a  K_REGISTER, 0);.
26c7a 20 20 20 20 69 66 28 20 70 4c 65 66 74 20 29 7b      if( pLeft ){
26c7b 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 68  .      /* Set th
26c7c 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
26c7d 65 6e 63 65 20 61 6e 64 20 61 66 66 69 6e 69 74  ence and affinit
26c7e 79 20 6f 66 20 74 68 65 20 4c 48 53 20 6f 66 20  y of the LHS of 
26c7f 65 61 63 68 20 54 4b 5f 45 51 0a 20 20 20 20 20  each TK_EQ.     
26c80 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 74   ** expression t
26c81 6f 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79  o the parent key
26c82 20 63 6f 6c 75 6d 6e 20 64 65 66 61 75 6c 74 73   column defaults
26c83 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  .  */.      if( 
26c84 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20  pIdx ){.        
26c85 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20  Column *pCol;.  
26c86 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 70 49 64        iCol = pId
26c87 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a  x->aiColumn[i];.
26c88 20 20 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 26          pCol = &
26c89 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 61 43  pIdx->pTable->aC
26c8a 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 20 20  ol[iCol];.      
26c8b 20 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65 20    pLeft->iTable 
26c8c 3d 20 72 65 67 44 61 74 61 2b 69 43 6f 6c 2b 31  = regData+iCol+1
26c8d 3b 0a 20 20 20 20 20 20 20 20 70 4c 65 66 74 2d  ;.        pLeft-
26c8e 3e 61 66 66 69 6e 69 74 79 20 3d 20 70 43 6f 6c  >affinity = pCol
26c8f 2d 3e 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20  ->affinity;.    
26c90 20 20 20 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c      pLeft->pColl
26c91 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65   = sqlite3Locate
26c92 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
26c93 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 29 3b 0a 20 20  pCol->zColl);.  
26c94 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
26c95 20 20 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65     pLeft->iTable
26c96 20 3d 20 72 65 67 44 61 74 61 3b 0a 20 20 20 20   = regData;.    
26c97 20 20 20 20 70 4c 65 66 74 2d 3e 61 66 66 69 6e      pLeft->affin
26c98 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  ity = SQLITE_AFF
26c99 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20  _INTEGER;.      
26c9a 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 43 6f 6c  }.    }.    iCol
26c9b 20 3d 20 61 69 43 6f 6c 20 3f 20 61 69 43 6f 6c   = aiCol ? aiCol
26c9c 5b 69 5d 20 3a 20 70 46 4b 65 79 2d 3e 61 43 6f  [i] : pFKey->aCo
26c9d 6c 5b 30 5d 2e 69 46 72 6f 6d 3b 0a 20 20 20 20  l[0].iFrom;.    
26c9e 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 30 20  assert( iCol>=0 
26c9f 29 3b 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 70 46  );.    zCol = pF
26ca0 4b 65 79 2d 3e 70 46 72 6f 6d 2d 3e 61 43 6f 6c  Key->pFrom->aCol
26ca1 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  [iCol].zName;.  
26ca2 20 20 70 52 69 67 68 74 20 3d 20 73 71 6c 69 74    pRight = sqlit
26ca3 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 44  e3Expr(db, TK_ID
26ca4 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 70 45 71  , zCol);.    pEq
26ca5 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
26ca6 70 50 61 72 73 65 2c 20 54 4b 5f 45 51 2c 20 70  pParse, TK_EQ, p
26ca7 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 30 29  Left, pRight, 0)
26ca8 3b 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20 73  ;.    pWhere = s
26ca9 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 64 62  qlite3ExprAnd(db
26caa 2c 20 70 57 68 65 72 65 2c 20 70 45 71 29 3b 0a  , pWhere, pEq);.
26cab 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
26cac 20 63 68 69 6c 64 20 74 61 62 6c 65 20 69 73 20   child table is 
26cad 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20  the same as the 
26cae 70 61 72 65 6e 74 20 74 61 62 6c 65 2c 20 61 6e  parent table, an
26caf 64 20 74 68 69 73 20 73 63 61 6e 0a 20 20 2a 2a  d this scan.  **
26cb0 20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63 65   is taking place
26cb1 20 61 73 20 70 61 72 74 20 6f 66 20 61 20 44 45   as part of a DE
26cb2 4c 45 54 45 20 6f 70 65 72 61 74 69 6f 6e 20 28  LETE operation (
26cb3 6f 70 65 72 61 74 69 6f 6e 20 44 2e 32 29 2c 20  operation D.2), 
26cb4 6f 6d 69 74 20 74 68 65 0a 20 20 2a 2a 20 72 6f  omit the.  ** ro
26cb5 77 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 20  w being deleted 
26cb6 66 72 6f 6d 20 74 68 65 20 73 63 61 6e 20 62 79  from the scan by
26cb7 20 61 64 64 69 6e 67 20 28 24 72 6f 77 69 64 20   adding ($rowid 
26cb8 21 3d 20 72 6f 77 69 64 29 20 74 6f 20 74 68 65  != rowid) to the
26cb9 20 57 48 45 52 45 20 0a 20 20 2a 2a 20 63 6c 61   WHERE .  ** cla
26cba 75 73 65 2c 20 77 68 65 72 65 20 24 72 6f 77 69  use, where $rowi
26cbb 64 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f  d is the rowid o
26cbc 66 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20  f the row being 
26cbd 64 65 6c 65 74 65 64 2e 20 20 2a 2f 0a 20 20 69  deleted.  */.  i
26cbe 66 28 20 70 54 61 62 3d 3d 70 46 4b 65 79 2d 3e  f( pTab==pFKey->
26cbf 70 46 72 6f 6d 20 26 26 20 6e 49 6e 63 72 3e 30  pFrom && nIncr>0
26cc0 20 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45   ){.    Expr *pE
26cc1 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  q;              
26cc2 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73        /* Express
26cc3 69 6f 6e 20 28 70 4c 65 66 74 20 3d 20 70 52 69  ion (pLeft = pRi
26cc4 67 68 74 29 20 2a 2f 0a 20 20 20 20 45 78 70 72  ght) */.    Expr
26cc5 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 20 20 20   *pLeft;        
26cc6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c            /* Val
26cc7 75 65 20 66 72 6f 6d 20 70 61 72 65 6e 74 20 74  ue from parent t
26cc8 61 62 6c 65 20 72 6f 77 20 2a 2f 0a 20 20 20 20  able row */.    
26cc9 45 78 70 72 20 2a 70 52 69 67 68 74 3b 20 20 20  Expr *pRight;   
26cca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
26ccb 20 43 6f 6c 75 6d 6e 20 72 65 66 20 74 6f 20 63   Column ref to c
26ccc 68 69 6c 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hild table */.  
26ccd 20 20 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65    pLeft = sqlite
26cce 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 52 45 47  3Expr(db, TK_REG
26ccf 49 53 54 45 52 2c 20 30 29 3b 0a 20 20 20 20 70  ISTER, 0);.    p
26cd0 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45  Right = sqlite3E
26cd1 78 70 72 28 64 62 2c 20 54 4b 5f 43 4f 4c 55 4d  xpr(db, TK_COLUM
26cd2 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70  N, 0);.    if( p
26cd3 4c 65 66 74 20 26 26 20 70 52 69 67 68 74 20 29  Left && pRight )
26cd4 7b 0a 20 20 20 20 20 20 70 4c 65 66 74 2d 3e 69  {.      pLeft->i
26cd5 54 61 62 6c 65 20 3d 20 72 65 67 44 61 74 61 3b  Table = regData;
26cd6 0a 20 20 20 20 20 20 70 4c 65 66 74 2d 3e 61 66  .      pLeft->af
26cd7 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f  finity = SQLITE_
26cd8 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  AFF_INTEGER;.   
26cd9 20 20 20 70 52 69 67 68 74 2d 3e 69 54 61 62 6c     pRight->iTabl
26cda 65 20 3d 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 69  e = pSrc->a[0].i
26cdb 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 70 52  Cursor;.      pR
26cdc 69 67 68 74 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20  ight->iColumn = 
26cdd 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45  -1;.    }.    pE
26cde 71 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  q = sqlite3PExpr
26cdf 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 45 2c 20  (pParse, TK_NE, 
26ce0 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 30  pLeft, pRight, 0
26ce1 29 3b 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20  );.    pWhere = 
26ce2 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 64  sqlite3ExprAnd(d
26ce3 62 2c 20 70 57 68 65 72 65 2c 20 70 45 71 29 3b  b, pWhere, pEq);
26ce4 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c  .  }..  /* Resol
26ce5 76 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  ve the reference
26ce6 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63  s in the WHERE c
26ce7 6c 61 75 73 65 2e 20 2a 2f 0a 20 20 6d 65 6d 73  lause. */.  mems
26ce8 65 74 28 26 73 4e 61 6d 65 43 6f 6e 74 65 78 74  et(&sNameContext
26ce9 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4e 61 6d 65  , 0, sizeof(Name
26cea 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20 73 4e 61  Context));.  sNa
26ceb 6d 65 43 6f 6e 74 65 78 74 2e 70 53 72 63 4c 69  meContext.pSrcLi
26cec 73 74 20 3d 20 70 53 72 63 3b 0a 20 20 73 4e 61  st = pSrc;.  sNa
26ced 6d 65 43 6f 6e 74 65 78 74 2e 70 50 61 72 73 65  meContext.pParse
26cee 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 71 6c   = pParse;.  sql
26cef 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
26cf0 61 6d 65 73 28 26 73 4e 61 6d 65 43 6f 6e 74 65  ames(&sNameConte
26cf1 78 74 2c 20 70 57 68 65 72 65 29 3b 0a 0a 20 20  xt, pWhere);..  
26cf2 2f 2a 20 43 72 65 61 74 65 20 56 44 42 45 20 74  /* Create VDBE t
26cf3 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74  o loop through t
26cf4 68 65 20 65 6e 74 72 69 65 73 20 69 6e 20 70 53  he entries in pS
26cf5 72 63 20 74 68 61 74 20 6d 61 74 63 68 20 74 68  rc that match th
26cf6 65 20 57 48 45 52 45 0a 20 20 2a 2a 20 63 6c 61  e WHERE.  ** cla
26cf7 75 73 65 2e 20 49 66 20 74 68 65 20 63 6f 6e 73  use. If the cons
26cf8 74 72 61 69 6e 74 20 69 73 20 6e 6f 74 20 64 65  traint is not de
26cf9 66 65 72 72 65 64 2c 20 74 68 72 6f 77 20 61 6e  ferred, throw an
26cfa 20 65 78 63 65 70 74 69 6f 6e 20 66 6f 72 0a 20   exception for. 
26cfb 20 2a 2a 20 65 61 63 68 20 72 6f 77 20 66 6f 75   ** each row fou
26cfc 6e 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66  nd. Otherwise, f
26cfd 6f 72 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73  or deferred cons
26cfe 74 72 61 69 6e 74 73 2c 20 69 6e 63 72 65 6d 65  traints, increme
26cff 6e 74 20 74 68 65 0a 20 20 2a 2a 20 64 65 66 65  nt the.  ** defe
26d00 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 20  rred constraint 
26d01 63 6f 75 6e 74 65 72 20 62 79 20 6e 49 6e 63 72  counter by nIncr
26d02 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 73 65   for each row se
26d03 6c 65 63 74 65 64 2e 20 20 2a 2f 0a 20 20 70 57  lected.  */.  pW
26d04 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57 68  Info = sqlite3Wh
26d05 65 72 65 42 65 67 69 6e 28 70 50 61 72 73 65 2c  ereBegin(pParse,
26d06 20 70 53 72 63 2c 20 70 57 68 65 72 65 2c 20 30   pSrc, pWhere, 0
26d07 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 49 6e 63  , 0);.  if( nInc
26d08 72 3e 30 20 26 26 20 70 46 4b 65 79 2d 3e 69 73  r>0 && pFKey->is
26d09 44 65 66 65 72 72 65 64 3d 3d 30 20 29 7b 0a 20  Deferred==0 ){. 
26d0a 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 54     sqlite3ParseT
26d0b 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29 2d  oplevel(pParse)-
26d0c 3e 6d 61 79 41 62 6f 72 74 20 3d 20 31 3b 0a 20  >mayAbort = 1;. 
26d0d 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65   }.  sqlite3Vdbe
26d0e 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46 6b 43  AddOp2(v, OP_FkC
26d0f 6f 75 6e 74 65 72 2c 20 70 46 4b 65 79 2d 3e 69  ounter, pFKey->i
26d10 73 44 65 66 65 72 72 65 64 2c 20 6e 49 6e 63 72  sDeferred, nIncr
26d11 29 3b 0a 20 20 69 66 28 20 70 57 49 6e 66 6f 20  );.  if( pWInfo 
26d12 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 57 68  ){.    sqlite3Wh
26d13 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a  ereEnd(pWInfo);.
26d14 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20    }..  /* Clean 
26d15 75 70 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  up the WHERE cla
26d16 75 73 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20  use constructed 
26d17 61 62 6f 76 65 2e 20 2a 2f 0a 20 20 73 71 6c 69  above. */.  sqli
26d18 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
26d19 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 69 66 28  , pWhere);.  if(
26d1a 20 69 46 6b 49 66 5a 65 72 6f 20 29 7b 0a 20 20   iFkIfZero ){.  
26d1b 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
26d1c 70 48 65 72 65 28 76 2c 20 69 46 6b 49 66 5a 65  pHere(v, iFkIfZe
26d1d 72 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ro);.  }.}../*.*
26d1e 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
26d1f 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
26d20 72 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66  r to the head of
26d21 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f   a linked list o
26d22 66 20 46 4b 0a 2a 2a 20 63 6f 6e 73 74 72 61 69  f FK.** constrai
26d23 6e 74 73 20 66 6f 72 20 77 68 69 63 68 20 74 61  nts for which ta
26d24 62 6c 65 20 70 54 61 62 20 69 73 20 74 68 65 20  ble pTab is the 
26d25 70 61 72 65 6e 74 20 74 61 62 6c 65 2e 20 46 6f  parent table. Fo
26d26 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 67 69  r example,.** gi
26d27 76 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ven the followin
26d28 67 20 73 63 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20  g schema:.**.** 
26d29 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74    CREATE TABLE t
26d2a 31 28 61 20 50 52 49 4d 41 52 59 20 4b 45 59 29  1(a PRIMARY KEY)
26d2b 3b 0a 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41  ;.**   CREATE TA
26d2c 42 4c 45 20 74 32 28 62 20 52 45 46 45 52 45 4e  BLE t2(b REFEREN
26d2d 43 45 53 20 74 31 28 61 29 3b 0a 2a 2a 0a 2a 2a  CES t1(a);.**.**
26d2e 20 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 75   Calling this fu
26d2f 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 61 62 6c  nction with tabl
26d30 65 20 22 74 31 22 20 61 73 20 61 6e 20 61 72 67  e "t1" as an arg
26d31 75 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 61 20  ument returns a 
26d32 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68  pointer.** to th
26d33 65 20 46 4b 65 79 20 73 74 72 75 63 74 75 72 65  e FKey structure
26d34 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68   representing th
26d35 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  e foreign key co
26d36 6e 73 74 72 61 69 6e 74 20 6f 6e 20 74 61 62 6c  nstraint on tabl
26d37 65 0a 2a 2a 20 22 74 32 22 2e 20 43 61 6c 6c 69  e.** "t2". Calli
26d38 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ng this function
26d39 20 77 69 74 68 20 22 74 32 22 20 61 73 20 74 68   with "t2" as th
26d3a 65 20 61 72 67 75 6d 65 6e 74 20 77 6f 75 6c 64  e argument would
26d3b 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 4e 55 4c   return a.** NUL
26d3c 4c 20 70 6f 69 6e 74 65 72 20 28 61 73 20 74 68  L pointer (as th
26d3d 65 72 65 20 61 72 65 20 6e 6f 20 46 4b 20 63 6f  ere are no FK co
26d3e 6e 73 74 72 61 69 6e 74 73 20 66 6f 72 20 77 68  nstraints for wh
26d3f 69 63 68 20 74 32 20 69 73 20 74 68 65 20 70 61  ich t2 is the pa
26d40 72 65 6e 74 0a 2a 2a 20 74 61 62 6c 65 29 2e 0a  rent.** table)..
26d41 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
26d42 45 20 46 4b 65 79 20 2a 73 71 6c 69 74 65 33 46  E FKey *sqlite3F
26d43 6b 52 65 66 65 72 65 6e 63 65 73 28 54 61 62 6c  kReferences(Tabl
26d44 65 20 2a 70 54 61 62 29 7b 0a 20 20 69 6e 74 20  e *pTab){.  int 
26d45 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53  nName = sqlite3S
26d46 74 72 6c 65 6e 33 30 28 70 54 61 62 2d 3e 7a 4e  trlen30(pTab->zN
26d47 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 28  ame);.  return (
26d48 46 4b 65 79 20 2a 29 73 71 6c 69 74 65 33 48 61  FKey *)sqlite3Ha
26d49 73 68 46 69 6e 64 28 26 70 54 61 62 2d 3e 70 53  shFind(&pTab->pS
26d4a 63 68 65 6d 61 2d 3e 66 6b 65 79 48 61 73 68 2c  chema->fkeyHash,
26d4b 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 6e 4e   pTab->zName, nN
26d4c 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ame);.}../*.** T
26d4d 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
26d4e 6e 74 20 69 73 20 61 20 54 72 69 67 67 65 72 20  nt is a Trigger 
26d4f 73 74 72 75 63 74 75 72 65 20 61 6c 6c 6f 63 61  structure alloca
26d50 74 65 64 20 62 79 20 74 68 65 20 0a 2a 2a 20 66  ted by the .** f
26d51 6b 41 63 74 69 6f 6e 54 72 69 67 67 65 72 28 29  kActionTrigger()
26d52 20 72 6f 75 74 69 6e 65 2e 20 54 68 69 73 20 66   routine. This f
26d53 75 6e 63 74 69 6f 6e 20 64 65 6c 65 74 65 73 20  unction deletes 
26d54 74 68 65 20 54 72 69 67 67 65 72 20 73 74 72 75  the Trigger stru
26d55 63 74 75 72 65 0a 2a 2a 20 61 6e 64 20 61 6c 6c  cture.** and all
26d56 20 6f 66 20 69 74 73 20 73 75 62 2d 63 6f 6d 70   of its sub-comp
26d57 6f 6e 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  onents..**.** Th
26d58 65 20 54 72 69 67 67 65 72 20 73 74 72 75 63 74  e Trigger struct
26d59 75 72 65 20 6f 72 20 61 6e 79 20 6f 66 20 69 74  ure or any of it
26d5a 73 20 73 75 62 2d 63 6f 6d 70 6f 6e 65 6e 74 73  s sub-components
26d5b 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63 61 74 65   may be allocate
26d5c 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 6c 6f  d from.** the lo
26d5d 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 62  okaside buffer b
26d5e 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 64 61 74 61  elonging to data
26d5f 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62 4d 65  base handle dbMe
26d60 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  m..*/.static voi
26d61 64 20 66 6b 54 72 69 67 67 65 72 44 65 6c 65 74  d fkTriggerDelet
26d62 65 28 73 71 6c 69 74 65 33 20 2a 64 62 4d 65 6d  e(sqlite3 *dbMem
26d63 2c 20 54 72 69 67 67 65 72 20 2a 70 29 7b 0a 20  , Trigger *p){. 
26d64 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 54 72   if( p ){.    Tr
26d65 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65 70  iggerStep *pStep
26d66 20 3d 20 70 2d 3e 73 74 65 70 5f 6c 69 73 74 3b   = p->step_list;
26d67 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
26d68 44 65 6c 65 74 65 28 64 62 4d 65 6d 2c 20 70 53  Delete(dbMem, pS
26d69 74 65 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20  tep->pWhere);.  
26d6a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
26d6b 74 44 65 6c 65 74 65 28 64 62 4d 65 6d 2c 20 70  tDelete(dbMem, p
26d6c 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 29  Step->pExprList)
26d6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c  ;.    sqlite3Sel
26d6e 65 63 74 44 65 6c 65 74 65 28 64 62 4d 65 6d 2c  ectDelete(dbMem,
26d6f 20 70 53 74 65 70 2d 3e 70 53 65 6c 65 63 74 29   pStep->pSelect)
26d70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
26d71 72 44 65 6c 65 74 65 28 64 62 4d 65 6d 2c 20 70  rDelete(dbMem, p
26d72 2d 3e 70 57 68 65 6e 29 3b 0a 20 20 20 20 73 71  ->pWhen);.    sq
26d73 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 4d 65  lite3DbFree(dbMe
26d74 6d 2c 20 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  m, p);.  }.}../*
26d75 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
26d76 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 67  n is called to g
26d77 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
26d78 74 20 72 75 6e 73 20 77 68 65 6e 20 74 61 62 6c  t runs when tabl
26d79 65 20 70 54 61 62 20 69 73 0a 2a 2a 20 62 65 69  e pTab is.** bei
26d7a 6e 67 20 64 72 6f 70 70 65 64 20 66 72 6f 6d 20  ng dropped from 
26d7b 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 54 68  the database. Th
26d7c 65 20 53 72 63 4c 69 73 74 20 70 61 73 73 65 64  e SrcList passed
26d7d 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
26d7e 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68  rgument.** to th
26d7f 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74  is function cont
26d80 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 20 65 6e  ains a single en
26d81 74 72 79 20 67 75 61 72 61 6e 74 65 65 64 20 74  try guaranteed t
26d82 6f 20 72 65 73 6f 6c 76 65 20 74 6f 0a 2a 2a 20  o resolve to.** 
26d83 74 61 62 6c 65 20 70 54 61 62 2e 0a 2a 2a 0a 2a  table pTab..**.*
26d84 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 6e 6f 20 63  * Normally, no c
26d85 6f 64 65 20 69 73 20 72 65 71 75 69 72 65 64 2e  ode is required.
26d86 20 48 6f 77 65 76 65 72 2c 20 69 66 20 65 69 74   However, if eit
26d87 68 65 72 0a 2a 2a 0a 2a 2a 20 20 20 28 61 29 20  her.**.**   (a) 
26d88 54 68 65 20 74 61 62 6c 65 20 69 73 20 74 68 65  The table is the
26d89 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 6f 66   parent table of
26d8a 20 61 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74   a FK constraint
26d8b 2c 20 6f 72 0a 2a 2a 20 20 20 28 62 29 20 54 68  , or.**   (b) Th
26d8c 65 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 63  e table is the c
26d8d 68 69 6c 64 20 74 61 62 6c 65 20 6f 66 20 61 20  hild table of a 
26d8e 64 65 66 65 72 72 65 64 20 46 4b 20 63 6f 6e 73  deferred FK cons
26d8f 74 72 61 69 6e 74 20 61 6e 64 20 69 74 20 69 73  traint and it is
26d90 0a 2a 2a 20 20 20 20 20 20 20 64 65 74 65 72 6d  .**       determ
26d91 69 6e 65 64 20 61 74 20 72 75 6e 74 69 6d 65 20  ined at runtime 
26d92 74 68 61 74 20 74 68 65 72 65 20 61 72 65 20 6f  that there are o
26d93 75 74 73 74 61 6e 64 69 6e 67 20 64 65 66 65 72  utstanding defer
26d94 72 65 64 20 46 4b 20 0a 2a 2a 20 20 20 20 20 20  red FK .**      
26d95 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c   constraint viol
26d96 61 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 64 61  ations in the da
26d97 74 61 62 61 73 65 2c 0a 2a 2a 0a 2a 2a 20 74 68  tabase,.**.** th
26d98 65 6e 20 74 68 65 20 65 71 75 69 76 61 6c 65 6e  en the equivalen
26d99 74 20 6f 66 20 22 44 45 4c 45 54 45 20 46 52 4f  t of "DELETE FRO
26d9a 4d 20 3c 74 62 6c 3e 22 20 69 73 20 65 78 65 63  M <tbl>" is exec
26d9b 75 74 65 64 20 62 65 66 6f 72 65 20 64 72 6f 70  uted before drop
26d9c 70 69 6e 67 0a 2a 2a 20 74 68 65 20 74 61 62 6c  ping.** the tabl
26d9d 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62  e from the datab
26d9e 61 73 65 2e 20 54 72 69 67 67 65 72 73 20 61 72  ase. Triggers ar
26d9f 65 20 64 69 73 61 62 6c 65 64 20 77 68 69 6c 65  e disabled while
26da0 20 72 75 6e 6e 69 6e 67 20 74 68 69 73 0a 2a 2a   running this.**
26da1 20 44 45 4c 45 54 45 2c 20 62 75 74 20 66 6f 72   DELETE, but for
26da2 65 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 73  eign key actions
26da3 20 61 72 65 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c   are not..*/.SQL
26da4 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
26da5 20 73 71 6c 69 74 65 33 46 6b 44 72 6f 70 54 61   sqlite3FkDropTa
26da6 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ble(Parse *pPars
26da7 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d  e, SrcList *pNam
26da8 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b  e, Table *pTab){
26da9 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
26daa 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
26dab 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 26 53 51  f( (db->flags&SQ
26dac 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73  LITE_ForeignKeys
26dad 29 20 26 26 20 21 49 73 56 69 72 74 75 61 6c 28  ) && !IsVirtual(
26dae 70 54 61 62 29 20 26 26 20 21 70 54 61 62 2d 3e  pTab) && !pTab->
26daf 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 69  pSelect ){.    i
26db0 6e 74 20 69 53 6b 69 70 20 3d 20 30 3b 0a 20 20  nt iSkip = 0;.  
26db1 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69    Vdbe *v = sqli
26db2 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
26db3 65 29 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28  e);..    assert(
26db4 20 76 20 29 3b 20 20 20 20 20 20 20 20 20 20 20   v );           
26db5 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 68         /* VDBE h
26db6 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
26db7 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 20  allocated */.   
26db8 20 69 66 28 20 73 71 6c 69 74 65 33 46 6b 52 65   if( sqlite3FkRe
26db9 66 65 72 65 6e 63 65 73 28 70 54 61 62 29 3d 3d  ferences(pTab)==
26dba 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65  0 ){.      /* Se
26dbb 61 72 63 68 20 66 6f 72 20 61 20 64 65 66 65 72  arch for a defer
26dbc 72 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  red foreign key 
26dbd 63 6f 6e 73 74 72 61 69 6e 74 20 66 6f 72 20 77  constraint for w
26dbe 68 69 63 68 20 74 68 69 73 20 74 61 62 6c 65 0a  hich this table.
26dbf 20 20 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20        ** is the 
26dc0 63 68 69 6c 64 20 74 61 62 6c 65 2e 20 49 66 20  child table. If 
26dc1 6f 6e 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f  one cannot be fo
26dc2 75 6e 64 2c 20 72 65 74 75 72 6e 20 77 69 74 68  und, return with
26dc3 6f 75 74 20 0a 20 20 20 20 20 20 2a 2a 20 67 65  out .      ** ge
26dc4 6e 65 72 61 74 69 6e 67 20 61 6e 79 20 56 44 42  nerating any VDB
26dc5 45 20 63 6f 64 65 2e 20 49 66 20 6f 6e 65 20 63  E code. If one c
26dc6 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20 74 68 65  an be found, the
26dc7 6e 20 6a 75 6d 70 20 6f 76 65 72 0a 20 20 20 20  n jump over.    
26dc8 20 20 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20    ** the entire 
26dc9 44 45 4c 45 54 45 20 69 66 20 74 68 65 72 65 20  DELETE if there 
26dca 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69  are no outstandi
26dcb 6e 67 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73  ng deferred cons
26dcc 74 72 61 69 6e 74 73 0a 20 20 20 20 20 20 2a 2a  traints.      **
26dcd 20 77 68 65 6e 20 74 68 69 73 20 73 74 61 74 65   when this state
26dce 6d 65 6e 74 20 69 73 20 72 75 6e 2e 20 20 2a 2f  ment is run.  */
26dcf 0a 20 20 20 20 20 20 46 4b 65 79 20 2a 70 3b 0a  .      FKey *p;.
26dd0 20 20 20 20 20 20 66 6f 72 28 70 3d 70 54 61 62        for(p=pTab
26dd1 2d 3e 70 46 4b 65 79 3b 20 70 3b 20 70 3d 70 2d  ->pFKey; p; p=p-
26dd2 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20 20  >pNextFrom){.   
26dd3 20 20 20 20 20 69 66 28 20 70 2d 3e 69 73 44 65       if( p->isDe
26dd4 66 65 72 72 65 64 20 29 20 62 72 65 61 6b 3b 0a  ferred ) break;.
26dd5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
26dd6 28 20 21 70 20 29 20 72 65 74 75 72 6e 3b 0a 20  ( !p ) return;. 
26dd7 20 20 20 20 20 69 53 6b 69 70 20 3d 20 73 71 6c       iSkip = sql
26dd8 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
26dd9 6c 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  l(v);.      sqli
26dda 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
26ddb 20 4f 50 5f 46 6b 49 66 5a 65 72 6f 2c 20 31 2c   OP_FkIfZero, 1,
26ddc 20 69 53 6b 69 70 29 3b 0a 20 20 20 20 7d 0a 0a   iSkip);.    }..
26ddd 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61      pParse->disa
26dde 62 6c 65 54 72 69 67 67 65 72 73 20 3d 20 31 3b  bleTriggers = 1;
26ddf 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65  .    sqlite3Dele
26de0 74 65 46 72 6f 6d 28 70 50 61 72 73 65 2c 20 73  teFrom(pParse, s
26de1 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70  qlite3SrcListDup
26de2 28 64 62 2c 20 70 4e 61 6d 65 2c 20 30 29 2c 20  (db, pName, 0), 
26de3 30 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  0);.    pParse->
26de4 64 69 73 61 62 6c 65 54 72 69 67 67 65 72 73 20  disableTriggers 
26de5 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  = 0;..    /* If 
26de6 74 68 65 20 44 45 4c 45 54 45 20 68 61 73 20 67  the DELETE has g
26de7 65 6e 65 72 61 74 65 64 20 69 6d 6d 65 64 69 61  enerated immedia
26de8 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  te foreign key c
26de9 6f 6e 73 74 72 61 69 6e 74 20 0a 20 20 20 20 2a  onstraint .    *
26dea 2a 20 76 69 6f 6c 61 74 69 6f 6e 73 2c 20 68 61  * violations, ha
26deb 6c 74 20 74 68 65 20 56 44 42 45 20 61 6e 64 20  lt the VDBE and 
26dec 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  return an error 
26ded 61 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 62  at this point, b
26dee 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20 61 6e 79  efore.    ** any
26def 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 74   modifications t
26df0 6f 20 74 68 65 20 73 63 68 65 6d 61 20 61 72 65  o the schema are
26df1 20 6d 61 64 65 2e 20 54 68 69 73 20 69 73 20 62   made. This is b
26df2 65 63 61 75 73 65 20 73 74 61 74 65 6d 65 6e 74  ecause statement
26df3 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74  .    ** transact
26df4 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61 62 6c  ions are not abl
26df5 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 73 63  e to rollback sc
26df6 68 65 6d 61 20 63 68 61 6e 67 65 73 2e 20 20 2a  hema changes.  *
26df7 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
26df8 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46 6b  eAddOp2(v, OP_Fk
26df9 49 66 5a 65 72 6f 2c 20 30 2c 20 73 71 6c 69 74  IfZero, 0, sqlit
26dfa 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
26dfb 72 28 76 29 2b 32 29 3b 0a 20 20 20 20 73 71 6c  r(v)+2);.    sql
26dfc 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69  ite3HaltConstrai
26dfd 6e 74 28 0a 20 20 20 20 20 20 20 20 70 50 61 72  nt(.        pPar
26dfe 73 65 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 22 66  se, OE_Abort, "f
26dff 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
26e00 72 61 69 6e 74 20 66 61 69 6c 65 64 22 2c 20 50  raint failed", P
26e01 34 5f 53 54 41 54 49 43 0a 20 20 20 20 29 3b 0a  4_STATIC.    );.
26e02 0a 20 20 20 20 69 66 28 20 69 53 6b 69 70 20 29  .    if( iSkip )
26e03 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
26e04 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
26e05 76 2c 20 69 53 6b 69 70 29 3b 0a 20 20 20 20 7d  v, iSkip);.    }
26e06 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
26e07 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
26e08 61 6c 6c 65 64 20 77 68 65 6e 20 69 6e 73 65 72  alled when inser
26e09 74 69 6e 67 2c 20 64 65 6c 65 74 69 6e 67 20 6f  ting, deleting o
26e0a 72 20 75 70 64 61 74 69 6e 67 20 61 20 72 6f 77  r updating a row
26e0b 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61   of.** table pTa
26e0c 62 20 74 6f 20 67 65 6e 65 72 61 74 65 20 56 44  b to generate VD
26e0d 42 45 20 63 6f 64 65 20 74 6f 20 70 65 72 66 6f  BE code to perfo
26e0e 72 6d 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  rm foreign key c
26e0f 6f 6e 73 74 72 61 69 6e 74 20 0a 2a 2a 20 70 72  onstraint .** pr
26e10 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 74 68 65  ocessing for the
26e11 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a   operation..**.*
26e12 2a 20 46 6f 72 20 61 20 44 45 4c 45 54 45 20 6f  * For a DELETE o
26e13 70 65 72 61 74 69 6f 6e 2c 20 70 61 72 61 6d 65  peration, parame
26e14 74 65 72 20 72 65 67 4f 6c 64 20 69 73 20 70 61  ter regOld is pa
26e15 73 73 65 64 20 74 68 65 20 69 6e 64 65 78 20 6f  ssed the index o
26e16 66 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 72  f the.** first r
26e17 65 67 69 73 74 65 72 20 69 6e 20 61 6e 20 61 72  egister in an ar
26e18 72 61 79 20 6f 66 20 28 70 54 61 62 2d 3e 6e 43  ray of (pTab->nC
26e19 6f 6c 2b 31 29 20 72 65 67 69 73 74 65 72 73 20  ol+1) registers 
26e1a 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 0a 2a  containing the.*
26e1b 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 72  * rowid of the r
26e1c 6f 77 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64  ow being deleted
26e1d 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 65 61  , followed by ea
26e1e 63 68 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  ch of the column
26e1f 20 76 61 6c 75 65 73 0a 2a 2a 20 6f 66 20 74 68   values.** of th
26e20 65 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65  e row being dele
26e21 74 65 64 2c 20 66 72 6f 6d 20 6c 65 66 74 20 74  ted, from left t
26e22 6f 20 72 69 67 68 74 2e 20 50 61 72 61 6d 65 74  o right. Paramet
26e23 65 72 20 72 65 67 4e 65 77 20 69 73 20 70 61 73  er regNew is pas
26e24 73 65 64 0a 2a 2a 20 7a 65 72 6f 20 69 6e 20 74  sed.** zero in t
26e25 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  his case..**.** 
26e26 46 6f 72 20 61 6e 20 49 4e 53 45 52 54 20 6f 70  For an INSERT op
26e27 65 72 61 74 69 6f 6e 2c 20 72 65 67 4f 6c 64 20  eration, regOld 
26e28 69 73 20 70 61 73 73 65 64 20 7a 65 72 6f 20 61  is passed zero a
26e29 6e 64 20 72 65 67 4e 65 77 20 69 73 20 70 61 73  nd regNew is pas
26e2a 73 65 64 20 74 68 65 0a 2a 2a 20 66 69 72 73 74  sed the.** first
26e2b 20 72 65 67 69 73 74 65 72 20 6f 66 20 61 6e 20   register of an 
26e2c 61 72 72 61 79 20 6f 66 20 28 70 54 61 62 2d 3e  array of (pTab->
26e2d 6e 43 6f 6c 2b 31 29 20 72 65 67 69 73 74 65 72  nCol+1) register
26e2e 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  s containing the
26e2f 20 6e 65 77 0a 2a 2a 20 72 6f 77 20 64 61 74 61   new.** row data
26e30 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 6e 20 55  ..**.** For an U
26e31 50 44 41 54 45 20 6f 70 65 72 61 74 69 6f 6e 2c  PDATE operation,
26e32 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
26e33 73 20 63 61 6c 6c 65 64 20 74 77 69 63 65 2e 20  s called twice. 
26e34 4f 6e 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74  Once before.** t
26e35 68 65 20 6f 72 69 67 69 6e 61 6c 20 72 65 63 6f  he original reco
26e36 72 64 20 69 73 20 64 65 6c 65 74 65 64 20 66 72  rd is deleted fr
26e37 6f 6d 20 74 68 65 20 74 61 62 6c 65 20 75 73 69  om the table usi
26e38 6e 67 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 63  ng the calling c
26e39 6f 6e 76 65 6e 74 69 6f 6e 0a 2a 2a 20 64 65 73  onvention.** des
26e3a 63 72 69 62 65 64 20 66 6f 72 20 44 45 4c 45 54  cribed for DELET
26e3b 45 2e 20 54 68 65 6e 20 61 67 61 69 6e 20 61 66  E. Then again af
26e3c 74 65 72 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  ter the original
26e3d 20 72 65 63 6f 72 64 20 69 73 20 64 65 6c 65 74   record is delet
26e3e 65 64 0a 2a 2a 20 62 75 74 20 62 65 66 6f 72 65  ed.** but before
26e3f 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20   the new record 
26e40 69 73 20 69 6e 73 65 72 74 65 64 20 75 73 69 6e  is inserted usin
26e41 67 20 74 68 65 20 49 4e 53 45 52 54 20 63 6f 6e  g the INSERT con
26e42 76 65 6e 74 69 6f 6e 2e 20 0a 2a 2f 0a 53 51 4c  vention. .*/.SQL
26e43 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
26e44 20 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b 28   sqlite3FkCheck(
26e45 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
26e46 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
26e47 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74     /* Parse cont
26e48 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
26e49 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 20  pTab,           
26e4a 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 20           /* Row 
26e4b 69 73 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64  is being deleted
26e4c 20 66 72 6f 6d 20 74 68 69 73 20 74 61 62 6c 65   from this table
26e4d 20 2a 2f 20 0a 20 20 69 6e 74 20 72 65 67 4f 6c   */ .  int regOl
26e4e 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
26e4f 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f         /* Previo
26e50 75 73 20 72 6f 77 20 64 61 74 61 20 69 73 20 73  us row data is s
26e51 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20  tored here */.  
26e52 69 6e 74 20 72 65 67 4e 65 77 20 20 20 20 20 20  int regNew      
26e53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26e54 2f 2a 20 4e 65 77 20 72 6f 77 20 64 61 74 61 20  /* New row data 
26e55 69 73 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a  is stored here *
26e56 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  /.){.  sqlite3 *
26e57 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
26e58 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
26e59 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 56  se handle */.  V
26e5a 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
26e5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26e5c 2a 20 56 4d 20 74 6f 20 77 72 69 74 65 20 63 6f  * VM to write co
26e5d 64 65 20 74 6f 20 2a 2f 0a 20 20 46 4b 65 79 20  de to */.  FKey 
26e5e 2a 70 46 4b 65 79 3b 20 20 20 20 20 20 20 20 20  *pFKey;         
26e5f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
26e60 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 74 68  ed to iterate th
26e61 72 6f 75 67 68 20 46 4b 73 20 2a 2f 0a 20 20 69  rough FKs */.  i
26e62 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
26e63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26e64 2a 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61 62  * Index of datab
26e65 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70  ase containing p
26e66 54 61 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  Tab */.  const c
26e67 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20  har *zDb;       
26e68 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
26e69 20 6f 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e   of database con
26e6a 74 61 69 6e 69 6e 67 20 70 54 61 62 20 2a 2f 0a  taining pTab */.
26e6b 20 20 69 6e 74 20 69 73 49 67 6e 6f 72 65 45 72    int isIgnoreEr
26e6c 72 6f 72 73 20 3d 20 70 50 61 72 73 65 2d 3e 64  rors = pParse->d
26e6d 69 73 61 62 6c 65 54 72 69 67 67 65 72 73 3b 0a  isableTriggers;.
26e6e 0a 20 20 2f 2a 20 45 78 61 63 74 6c 79 20 6f 6e  .  /* Exactly on
26e6f 65 20 6f 66 20 72 65 67 4f 6c 64 20 61 6e 64 20  e of regOld and 
26e70 72 65 67 4e 65 77 20 73 68 6f 75 6c 64 20 62 65  regNew should be
26e71 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 2a 2f 0a 20 20   non-zero. */.  
26e72 61 73 73 65 72 74 28 20 28 72 65 67 4f 6c 64 3d  assert( (regOld=
26e73 3d 30 29 21 3d 28 72 65 67 4e 65 77 3d 3d 30 29  =0)!=(regNew==0)
26e74 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 66 6f 72   );..  /* If for
26e75 65 69 67 6e 2d 6b 65 79 73 20 61 72 65 20 64 69  eign-keys are di
26e76 73 61 62 6c 65 64 2c 20 74 68 69 73 20 66 75 6e  sabled, this fun
26e77 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
26e78 2e 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e  . */.  if( (db->
26e79 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 6f 72  flags&SQLITE_For
26e7a 65 69 67 6e 4b 65 79 73 29 3d 3d 30 20 29 20 72  eignKeys)==0 ) r
26e7b 65 74 75 72 6e 3b 0a 0a 20 20 76 20 3d 20 73 71  eturn;..  v = sq
26e7c 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
26e7d 72 73 65 29 3b 0a 20 20 69 44 62 20 3d 20 73 71  rse);.  iDb = sq
26e7e 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
26e7f 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  ex(db, pTab->pSc
26e80 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20 3d 20 64  hema);.  zDb = d
26e81 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
26e82 65 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20 74 68  e;..  /* Loop th
26e83 72 6f 75 67 68 20 61 6c 6c 20 74 68 65 20 66 6f  rough all the fo
26e84 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
26e85 61 69 6e 74 73 20 66 6f 72 20 77 68 69 63 68 20  aints for which 
26e86 70 54 61 62 20 69 73 20 74 68 65 0a 20 20 2a 2a  pTab is the.  **
26e87 20 63 68 69 6c 64 20 74 61 62 6c 65 20 28 74 68   child table (th
26e88 65 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 65  e table that the
26e89 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 66   foreign key def
26e8a 69 6e 69 74 69 6f 6e 20 69 73 20 70 61 72 74 20  inition is part 
26e8b 6f 66 29 2e 20 20 2a 2f 0a 20 20 66 6f 72 28 70  of).  */.  for(p
26e8c 46 4b 65 79 3d 70 54 61 62 2d 3e 70 46 4b 65 79  FKey=pTab->pFKey
26e8d 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70  ; pFKey; pFKey=p
26e8e 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 29  FKey->pNextFrom)
26e8f 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 6f  {.    Table *pTo
26e90 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
26e91 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 74 61      /* Parent ta
26e92 62 6c 65 20 6f 66 20 66 6f 72 65 69 67 6e 20 6b  ble of foreign k
26e93 65 79 20 70 46 4b 65 79 20 2a 2f 0a 20 20 20 20  ey pFKey */.    
26e94 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 30 3b  Index *pIdx = 0;
26e95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
26e96 20 49 6e 64 65 78 20 6f 6e 20 6b 65 79 20 63 6f   Index on key co
26e97 6c 75 6d 6e 73 20 69 6e 20 70 54 6f 20 2a 2f 0a  lumns in pTo */.
26e98 20 20 20 20 69 6e 74 20 2a 61 69 46 72 65 65 20      int *aiFree 
26e99 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 2a 61 69  = 0;.    int *ai
26e9a 43 6f 6c 3b 0a 20 20 20 20 69 6e 74 20 69 43 6f  Col;.    int iCo
26e9b 6c 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  l;.    int i;.  
26e9c 20 20 69 6e 74 20 69 73 49 67 6e 6f 72 65 20 3d    int isIgnore =
26e9d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64   0;..    /* Find
26e9e 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
26e9f 65 20 6f 66 20 74 68 69 73 20 66 6f 72 65 69 67  e of this foreig
26ea0 6e 20 6b 65 79 2e 20 41 6c 73 6f 20 66 69 6e 64  n key. Also find
26ea1 20 61 20 75 6e 69 71 75 65 20 69 6e 64 65 78 20   a unique index 
26ea2 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 70  .    ** on the p
26ea3 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e  arent key column
26ea4 73 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20  s in the parent 
26ea5 74 61 62 6c 65 2e 20 49 66 20 65 69 74 68 65 72  table. If either
26ea6 20 6f 66 20 74 68 65 73 65 20 0a 20 20 20 20 2a   of these .    *
26ea7 2a 20 73 63 68 65 6d 61 20 69 74 65 6d 73 20 63  * schema items c
26ea8 61 6e 6e 6f 74 20 62 65 20 6c 6f 63 61 74 65 64  annot be located
26ea9 2c 20 73 65 74 20 61 6e 20 65 72 72 6f 72 20 69  , set an error i
26eaa 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 74  n pParse and ret
26eab 75 72 6e 20 0a 20 20 20 20 2a 2a 20 65 61 72 6c  urn .    ** earl
26eac 79 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  y.  */.    if( p
26ead 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 54 72  Parse->disableTr
26eae 69 67 67 65 72 73 20 29 7b 0a 20 20 20 20 20 20  iggers ){.      
26eaf 70 54 6f 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  pTo = sqlite3Fin
26eb0 64 54 61 62 6c 65 28 64 62 2c 20 70 46 4b 65 79  dTable(db, pFKey
26eb1 2d 3e 7a 54 6f 2c 20 7a 44 62 29 3b 0a 20 20 20  ->zTo, zDb);.   
26eb2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 54   }else{.      pT
26eb3 6f 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  o = sqlite3Locat
26eb4 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30  eTable(pParse, 0
26eb5 2c 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 7a 44  , pFKey->zTo, zD
26eb6 62 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  b);.    }.    if
26eb7 28 20 21 70 54 6f 20 7c 7c 20 6c 6f 63 61 74 65  ( !pTo || locate
26eb8 46 6b 65 79 49 6e 64 65 78 28 70 50 61 72 73 65  FkeyIndex(pParse
26eb9 2c 20 70 54 6f 2c 20 70 46 4b 65 79 2c 20 26 70  , pTo, pFKey, &p
26eba 49 64 78 2c 20 26 61 69 46 72 65 65 29 20 29 7b  Idx, &aiFree) ){
26ebb 0a 20 20 20 20 20 20 69 66 28 20 21 69 73 49 67  .      if( !isIg
26ebc 6e 6f 72 65 45 72 72 6f 72 73 20 7c 7c 20 64 62  noreErrors || db
26ebd 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
26ebe 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 63   return;.      c
26ebf 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20  ontinue;.    }. 
26ec0 20 20 20 61 73 73 65 72 74 28 20 70 46 4b 65 79     assert( pFKey
26ec1 2d 3e 6e 43 6f 6c 3d 3d 31 20 7c 7c 20 28 61 69  ->nCol==1 || (ai
26ec2 46 72 65 65 20 26 26 20 70 49 64 78 29 20 29 3b  Free && pIdx) );
26ec3 0a 0a 20 20 20 20 69 66 28 20 61 69 46 72 65 65  ..    if( aiFree
26ec4 20 29 7b 0a 20 20 20 20 20 20 61 69 43 6f 6c 20   ){.      aiCol 
26ec5 3d 20 61 69 46 72 65 65 3b 0a 20 20 20 20 7d 65  = aiFree;.    }e
26ec6 6c 73 65 7b 0a 20 20 20 20 20 20 69 43 6f 6c 20  lse{.      iCol 
26ec7 3d 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d  = pFKey->aCol[0]
26ec8 2e 69 46 72 6f 6d 3b 0a 20 20 20 20 20 20 61 69  .iFrom;.      ai
26ec9 43 6f 6c 20 3d 20 26 69 43 6f 6c 3b 0a 20 20 20  Col = &iCol;.   
26eca 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20   }.    for(i=0; 
26ecb 69 3c 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 69  i<pFKey->nCol; i
26ecc 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61  ++){.      if( a
26ecd 69 43 6f 6c 5b 69 5d 3d 3d 70 54 61 62 2d 3e 69  iCol[i]==pTab->i
26ece 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20  PKey ){.        
26ecf 61 69 43 6f 6c 5b 69 5d 20 3d 20 2d 31 3b 0a 20  aiCol[i] = -1;. 
26ed0 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53       }.#ifndef S
26ed1 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
26ed2 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 20 20 2f  RIZATION.      /
26ed3 2a 20 52 65 71 75 65 73 74 20 70 65 72 6d 69 73  * Request permis
26ed4 73 69 6f 6e 20 74 6f 20 72 65 61 64 20 74 68 65  sion to read the
26ed5 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75   parent key colu
26ed6 6d 6e 73 2e 20 49 66 20 74 68 65 20 0a 20 20 20  mns. If the .   
26ed7 20 20 20 2a 2a 20 61 75 74 68 6f 72 69 7a 61 74     ** authorizat
26ed8 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  ion callback ret
26ed9 75 72 6e 73 20 53 51 4c 49 54 45 5f 49 47 4e 4f  urns SQLITE_IGNO
26eda 52 45 2c 20 62 65 68 61 76 65 20 61 73 20 69 66  RE, behave as if
26edb 20 61 6e 79 0a 20 20 20 20 20 20 2a 2a 20 76 61   any.      ** va
26edc 6c 75 65 73 20 72 65 61 64 20 66 72 6f 6d 20 74  lues read from t
26edd 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20  he parent table 
26ede 61 72 65 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 20  are NULL. */.   
26edf 20 20 20 69 66 28 20 64 62 2d 3e 78 41 75 74 68     if( db->xAuth
26ee0 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
26ee1 72 63 61 75 74 68 3b 0a 20 20 20 20 20 20 20 20  rcauth;.        
26ee2 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 70 54 6f  char *zCol = pTo
26ee3 2d 3e 61 43 6f 6c 5b 70 49 64 78 20 3f 20 70 49  ->aCol[pIdx ? pI
26ee4 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20  dx->aiColumn[i] 
26ee5 3a 20 70 54 6f 2d 3e 69 50 4b 65 79 5d 2e 7a 4e  : pTo->iPKey].zN
26ee6 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 72 63 61  ame;.        rca
26ee7 75 74 68 20 3d 20 73 71 6c 69 74 65 33 41 75 74  uth = sqlite3Aut
26ee8 68 52 65 61 64 43 6f 6c 28 70 50 61 72 73 65 2c  hReadCol(pParse,
26ee9 20 70 54 6f 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f   pTo->zName, zCo
26eea 6c 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20  l, iDb);.       
26eeb 20 69 73 49 67 6e 6f 72 65 20 3d 20 28 72 63 61   isIgnore = (rca
26eec 75 74 68 3d 3d 53 51 4c 49 54 45 5f 49 47 4e 4f  uth==SQLITE_IGNO
26eed 52 45 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  RE);.      }.#en
26eee 64 69 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  dif.    }..    /
26eef 2a 20 54 61 6b 65 20 61 20 73 68 61 72 65 64 2d  * Take a shared-
26ef0 63 61 63 68 65 20 61 64 76 69 73 6f 72 79 20 72  cache advisory r
26ef1 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ead-lock on the 
26ef2 70 61 72 65 6e 74 20 74 61 62 6c 65 2e 20 41 6c  parent table. Al
26ef3 6c 6f 63 61 74 65 20 0a 20 20 20 20 2a 2a 20 61  locate .    ** a
26ef4 20 63 75 72 73 6f 72 20 74 6f 20 75 73 65 20 74   cursor to use t
26ef5 6f 20 73 65 61 72 63 68 20 74 68 65 20 75 6e 69  o search the uni
26ef6 71 75 65 20 69 6e 64 65 78 20 6f 6e 20 74 68 65  que index on the
26ef7 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75   parent key colu
26ef8 6d 6e 73 20 0a 20 20 20 20 2a 2a 20 69 6e 20 74  mns .    ** in t
26ef9 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 2e  he parent table.
26efa 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
26efb 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65  TableLock(pParse
26efc 2c 20 69 44 62 2c 20 70 54 6f 2d 3e 74 6e 75 6d  , iDb, pTo->tnum
26efd 2c 20 30 2c 20 70 54 6f 2d 3e 7a 4e 61 6d 65 29  , 0, pTo->zName)
26efe 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54  ;.    pParse->nT
26eff 61 62 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 72  ab++;..    if( r
26f00 65 67 4f 6c 64 21 3d 30 20 29 7b 0a 20 20 20 20  egOld!=0 ){.    
26f01 20 20 2f 2a 20 41 20 72 6f 77 20 69 73 20 62 65    /* A row is be
26f02 69 6e 67 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d  ing removed from
26f03 20 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65   the child table
26f04 2e 20 53 65 61 72 63 68 20 66 6f 72 20 74 68 65  . Search for the
26f05 20 70 61 72 65 6e 74 2e 0a 20 20 20 20 20 20 2a   parent..      *
26f06 2a 20 49 66 20 74 68 65 20 70 61 72 65 6e 74 20  * If the parent 
26f07 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20  does not exist, 
26f08 72 65 6d 6f 76 69 6e 67 20 74 68 65 20 63 68 69  removing the chi
26f09 6c 64 20 72 6f 77 20 72 65 73 6f 6c 76 65 73 20  ld row resolves 
26f0a 61 6e 20 0a 20 20 20 20 20 20 2a 2a 20 6f 75 74  an .      ** out
26f0b 73 74 61 6e 64 69 6e 67 20 66 6f 72 65 69 67 6e  standing foreign
26f0c 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
26f0d 76 69 6f 6c 61 74 69 6f 6e 2e 20 2a 2f 0a 20 20  violation. */.  
26f0e 20 20 20 20 66 6b 4c 6f 6f 6b 75 70 50 61 72 65      fkLookupPare
26f0f 6e 74 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20  nt(pParse, iDb, 
26f10 70 54 6f 2c 20 70 49 64 78 2c 20 70 46 4b 65 79  pTo, pIdx, pFKey
26f11 2c 20 61 69 43 6f 6c 2c 20 72 65 67 4f 6c 64 2c  , aiCol, regOld,
26f12 20 2d 31 2c 69 73 49 67 6e 6f 72 65 29 3b 0a 20   -1,isIgnore);. 
26f13 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 65 67     }.    if( reg
26f14 4e 65 77 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  New!=0 ){.      
26f15 2f 2a 20 41 20 72 6f 77 20 69 73 20 62 65 69 6e  /* A row is bein
26f16 67 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 63  g added to the c
26f17 68 69 6c 64 20 74 61 62 6c 65 2e 20 49 66 20 61  hild table. If a
26f18 20 70 61 72 65 6e 74 20 72 6f 77 20 63 61 6e 6e   parent row cann
26f19 6f 74 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 66  ot.      ** be f
26f1a 6f 75 6e 64 2c 20 61 64 64 69 6e 67 20 74 68 65  ound, adding the
26f1b 20 63 68 69 6c 64 20 72 6f 77 20 68 61 73 20 76   child row has v
26f1c 69 6f 6c 61 74 65 64 20 74 68 65 20 46 4b 20 63  iolated the FK c
26f1d 6f 6e 73 74 72 61 69 6e 74 2e 20 2a 2f 20 0a 20  onstraint. */ . 
26f1e 20 20 20 20 20 66 6b 4c 6f 6f 6b 75 70 50 61 72       fkLookupPar
26f1f 65 6e 74 28 70 50 61 72 73 65 2c 20 69 44 62 2c  ent(pParse, iDb,
26f20 20 70 54 6f 2c 20 70 49 64 78 2c 20 70 46 4b 65   pTo, pIdx, pFKe
26f21 79 2c 20 61 69 43 6f 6c 2c 20 72 65 67 4e 65 77  y, aiCol, regNew
26f22 2c 20 2b 31 2c 69 73 49 67 6e 6f 72 65 29 3b 0a  , +1,isIgnore);.
26f23 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
26f24 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 69 46  e3DbFree(db, aiF
26f25 72 65 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ree);.  }..  /* 
26f26 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 61 6c 6c  Loop through all
26f27 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79   the foreign key
26f28 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 74 68 61   constraints tha
26f29 74 20 72 65 66 65 72 20 74 6f 20 74 68 69 73 20  t refer to this 
26f2a 74 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70  table */.  for(p
26f2b 46 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 46 6b  FKey = sqlite3Fk
26f2c 52 65 66 65 72 65 6e 63 65 73 28 70 54 61 62 29  References(pTab)
26f2d 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70  ; pFKey; pFKey=p
26f2e 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 29 7b 0a  FKey->pNextTo){.
26f2f 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20      Index *pIdx 
26f30 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
26f31 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79    /* Foreign key
26f32 20 69 6e 64 65 78 20 66 6f 72 20 70 46 4b 65 79   index for pFKey
26f33 20 2a 2f 0a 20 20 20 20 53 72 63 4c 69 73 74 20   */.    SrcList 
26f34 2a 70 53 72 63 3b 0a 20 20 20 20 69 6e 74 20 2a  *pSrc;.    int *
26f35 61 69 43 6f 6c 20 3d 20 30 3b 0a 0a 20 20 20 20  aiCol = 0;..    
26f36 69 66 28 20 21 70 46 4b 65 79 2d 3e 69 73 44 65  if( !pFKey->isDe
26f37 66 65 72 72 65 64 20 26 26 20 21 70 50 61 72 73  ferred && !pPars
26f38 65 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 26 26 20  e->pToplevel && 
26f39 21 70 50 61 72 73 65 2d 3e 69 73 4d 75 6c 74 69  !pParse->isMulti
26f3a 57 72 69 74 65 20 29 7b 0a 20 20 20 20 20 20 61  Write ){.      a
26f3b 73 73 65 72 74 28 20 72 65 67 4f 6c 64 3d 3d 30  ssert( regOld==0
26f3c 20 26 26 20 72 65 67 4e 65 77 21 3d 30 20 29 3b   && regNew!=0 );
26f3d 0a 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74  .      /* Insert
26f3e 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 72 6f 77  ing a single row
26f3f 20 69 6e 74 6f 20 61 20 70 61 72 65 6e 74 20 74   into a parent t
26f40 61 62 6c 65 20 63 61 6e 6e 6f 74 20 63 61 75 73  able cannot caus
26f41 65 20 61 6e 20 69 6d 6d 65 64 69 61 74 65 0a 20  e an immediate. 
26f42 20 20 20 20 20 2a 2a 20 66 6f 72 65 69 67 6e 20       ** foreign 
26f43 6b 65 79 20 76 69 6f 6c 61 74 69 6f 6e 2e 20 53  key violation. S
26f44 6f 20 64 6f 20 6e 6f 74 68 69 6e 67 20 69 6e 20  o do nothing in 
26f45 74 68 69 73 20 63 61 73 65 2e 20 20 2a 2f 0a 20  this case.  */. 
26f46 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
26f47 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 6c 6f     }..    if( lo
26f48 63 61 74 65 46 6b 65 79 49 6e 64 65 78 28 70 50  cateFkeyIndex(pP
26f49 61 72 73 65 2c 20 70 54 61 62 2c 20 70 46 4b 65  arse, pTab, pFKe
26f4a 79 2c 20 26 70 49 64 78 2c 20 26 61 69 43 6f 6c  y, &pIdx, &aiCol
26f4b 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21  ) ){.      if( !
26f4c 69 73 49 67 6e 6f 72 65 45 72 72 6f 72 73 20 7c  isIgnoreErrors |
26f4d 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
26f4e 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20  ed ) return;.   
26f4f 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
26f50 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 61   }.    assert( a
26f51 69 43 6f 6c 20 7c 7c 20 70 46 4b 65 79 2d 3e 6e  iCol || pFKey->n
26f52 43 6f 6c 3d 3d 31 20 29 3b 0a 0a 20 20 20 20 2f  Col==1 );..    /
26f53 2a 20 43 72 65 61 74 65 20 61 20 53 72 63 4c 69  * Create a SrcLi
26f54 73 74 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  st structure con
26f55 74 61 69 6e 69 6e 67 20 61 20 73 69 6e 67 6c 65  taining a single
26f56 20 74 61 62 6c 65 20 28 74 68 65 20 74 61 62 6c   table (the tabl
26f57 65 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 6f  e .    ** the fo
26f58 72 65 69 67 6e 20 6b 65 79 20 74 68 61 74 20 72  reign key that r
26f59 65 66 65 72 73 20 74 6f 20 74 68 69 73 20 74 61  efers to this ta
26f5a 62 6c 65 20 69 73 20 61 74 74 61 63 68 65 64 20  ble is attached 
26f5b 74 6f 29 2e 20 54 68 69 73 0a 20 20 20 20 2a 2a  to). This.    **
26f5c 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72   is required for
26f5d 20 74 68 65 20 73 71 6c 69 74 65 33 57 68 65 72   the sqlite3Wher
26f5e 65 58 58 58 28 29 20 69 6e 74 65 72 66 61 63 65  eXXX() interface
26f5f 2e 20 20 2a 2f 0a 20 20 20 20 70 53 72 63 20 3d  .  */.    pSrc =
26f60 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
26f61 70 70 65 6e 64 28 64 62 2c 20 30 2c 20 30 2c 20  ppend(db, 0, 0, 
26f62 30 29 3b 0a 20 20 20 20 69 66 28 20 70 53 72 63  0);.    if( pSrc
26f63 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74   ){.      struct
26f64 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
26f65 49 74 65 6d 20 3d 20 70 53 72 63 2d 3e 61 3b 0a  Item = pSrc->a;.
26f66 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 54 61        pItem->pTa
26f67 62 20 3d 20 70 46 4b 65 79 2d 3e 70 46 72 6f 6d  b = pFKey->pFrom
26f68 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 7a  ;.      pItem->z
26f69 4e 61 6d 65 20 3d 20 70 46 4b 65 79 2d 3e 70 46  Name = pFKey->pF
26f6a 72 6f 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20  rom->zName;.    
26f6b 20 20 70 49 74 65 6d 2d 3e 70 54 61 62 2d 3e 6e    pItem->pTab->n
26f6c 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 70 49 74  Ref++;.      pIt
26f6d 65 6d 2d 3e 69 43 75 72 73 6f 72 20 3d 20 70 50  em->iCursor = pP
26f6e 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
26f6f 0a 20 20 20 20 20 20 69 66 28 20 72 65 67 4e 65  .      if( regNe
26f70 77 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  w!=0 ){.        
26f71 66 6b 53 63 61 6e 43 68 69 6c 64 72 65 6e 28 70  fkScanChildren(p
26f72 50 61 72 73 65 2c 20 70 53 72 63 2c 20 70 54 61  Parse, pSrc, pTa
26f73 62 2c 20 70 49 64 78 2c 20 70 46 4b 65 79 2c 20  b, pIdx, pFKey, 
26f74 61 69 43 6f 6c 2c 20 72 65 67 4e 65 77 2c 20 2d  aiCol, regNew, -
26f75 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  1);.      }.    
26f76 20 20 69 66 28 20 72 65 67 4f 6c 64 21 3d 30 20    if( regOld!=0 
26f77 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  ){.        /* If
26f78 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 54   there is a REST
26f79 52 49 43 54 20 61 63 74 69 6f 6e 20 63 6f 6e 66  RICT action conf
26f7a 69 67 75 72 65 64 20 66 6f 72 20 74 68 65 20 63  igured for the c
26f7b 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e  urrent operation
26f7c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74  .        ** on t
26f7d 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20  he parent table 
26f7e 6f 66 20 74 68 69 73 20 46 4b 2c 20 74 68 65 6e  of this FK, then
26f7f 20 74 68 72 6f 77 20 61 6e 20 65 78 63 65 70 74   throw an except
26f80 69 6f 6e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  ion .        ** 
26f81 69 6d 6d 65 64 69 61 74 65 6c 79 20 69 66 20 74  immediately if t
26f82 68 65 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74  he FK constraint
26f83 20 69 73 20 76 69 6f 6c 61 74 65 64 2c 20 65 76   is violated, ev
26f84 65 6e 20 69 66 20 74 68 69 73 20 69 73 20 61 0a  en if this is a.
26f85 20 20 20 20 20 20 20 20 2a 2a 20 64 65 66 65 72          ** defer
26f86 72 65 64 20 74 72 69 67 67 65 72 2e 20 54 68 61  red trigger. Tha
26f87 74 27 73 20 77 68 61 74 20 52 45 53 54 52 49 43  t's what RESTRIC
26f88 54 20 6d 65 61 6e 73 2e 20 54 6f 20 64 65 66 65  T means. To defe
26f89 72 20 63 68 65 63 6b 69 6e 67 0a 20 20 20 20 20  r checking.     
26f8a 20 20 20 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72     ** the constr
26f8b 61 69 6e 74 2c 20 74 68 65 20 46 4b 20 73 68 6f  aint, the FK sho
26f8c 75 6c 64 20 73 70 65 63 69 66 79 20 4e 4f 20 41  uld specify NO A
26f8d 43 54 49 4f 4e 20 28 72 65 70 72 65 73 65 6e 74  CTION (represent
26f8e 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 75 73  ed.        ** us
26f8f 69 6e 67 20 4f 45 5f 4e 6f 6e 65 29 2e 20 4e 4f  ing OE_None). NO
26f90 20 41 43 54 49 4f 4e 20 69 73 20 74 68 65 20 64   ACTION is the d
26f91 65 66 61 75 6c 74 2e 20 20 2a 2f 0a 20 20 20 20  efault.  */.    
26f92 20 20 20 20 66 6b 53 63 61 6e 43 68 69 6c 64 72      fkScanChildr
26f93 65 6e 28 70 50 61 72 73 65 2c 20 70 53 72 63 2c  en(pParse, pSrc,
26f94 20 70 54 61 62 2c 20 70 49 64 78 2c 20 70 46 4b   pTab, pIdx, pFK
26f95 65 79 2c 20 61 69 43 6f 6c 2c 20 72 65 67 4f 6c  ey, aiCol, regOl
26f96 64 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  d, 1);.      }. 
26f97 20 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d       pItem->zNam
26f98 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c  e = 0;.      sql
26f99 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
26f9a 65 28 64 62 2c 20 70 53 72 63 29 3b 0a 20 20 20  e(db, pSrc);.   
26f9b 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62   }.    sqlite3Db
26f9c 46 72 65 65 28 64 62 2c 20 61 69 43 6f 6c 29 3b  Free(db, aiCol);
26f9d 0a 20 20 7d 0a 7d 0a 0a 23 64 65 66 69 6e 65 20  .  }.}..#define 
26f9e 43 4f 4c 55 4d 4e 5f 4d 41 53 4b 28 78 29 20 28  COLUMN_MASK(x) (
26f9f 28 28 78 29 3e 33 31 29 20 3f 20 30 78 66 66 66  ((x)>31) ? 0xfff
26fa0 66 66 66 66 66 20 3a 20 28 28 75 33 32 29 31 3c  fffff : ((u32)1<
26fa1 3c 28 78 29 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68  <(x)))../*.** Th
26fa2 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
26fa3 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 67 65 6e  alled before gen
26fa4 65 72 61 74 69 6e 67 20 63 6f 64 65 20 74 6f 20  erating code to 
26fa5 75 70 64 61 74 65 20 6f 72 20 64 65 6c 65 74 65  update or delete
26fa6 20 61 20 0a 2a 2a 20 72 6f 77 20 63 6f 6e 74 61   a .** row conta
26fa7 69 6e 65 64 20 69 6e 20 74 61 62 6c 65 20 70 54  ined in table pT
26fa8 61 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ab..*/.SQLITE_PR
26fa9 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65  IVATE u32 sqlite
26faa 33 46 6b 4f 6c 64 6d 61 73 6b 28 0a 20 20 50 61  3FkOldmask(.  Pa
26fab 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
26fac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
26fad 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a   Parse context *
26fae 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20  /.  Table *pTab 
26faf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26fb0 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69      /* Table bei
26fb1 6e 67 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0a 29  ng modified */.)
26fb2 7b 0a 20 20 75 33 32 20 6d 61 73 6b 20 3d 20 30  {.  u32 mask = 0
26fb3 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
26fb4 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45  db->flags&SQLITE
26fb5 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20 29 7b 0a  _ForeignKeys ){.
26fb6 20 20 20 20 46 4b 65 79 20 2a 70 3b 0a 20 20 20      FKey *p;.   
26fb7 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28   int i;.    for(
26fb8 70 3d 70 54 61 62 2d 3e 70 46 4b 65 79 3b 20 70  p=pTab->pFKey; p
26fb9 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 46 72 6f 6d  ; p=p->pNextFrom
26fba 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  ){.      for(i=0
26fbb 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  ; i<p->nCol; i++
26fbc 29 20 6d 61 73 6b 20 7c 3d 20 43 4f 4c 55 4d 4e  ) mask |= COLUMN
26fbd 5f 4d 41 53 4b 28 70 2d 3e 61 43 6f 6c 5b 69 5d  _MASK(p->aCol[i]
26fbe 2e 69 46 72 6f 6d 29 3b 0a 20 20 20 20 7d 0a 20  .iFrom);.    }. 
26fbf 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 33     for(p=sqlite3
26fc0 46 6b 52 65 66 65 72 65 6e 63 65 73 28 70 54 61  FkReferences(pTa
26fc1 62 29 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78  b); p; p=p->pNex
26fc2 74 54 6f 29 7b 0a 20 20 20 20 20 20 49 6e 64 65  tTo){.      Inde
26fc3 78 20 2a 70 49 64 78 20 3d 20 30 3b 0a 20 20 20  x *pIdx = 0;.   
26fc4 20 20 20 6c 6f 63 61 74 65 46 6b 65 79 49 6e 64     locateFkeyInd
26fc5 65 78 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ex(pParse, pTab,
26fc6 20 70 2c 20 26 70 49 64 78 2c 20 30 29 3b 0a 20   p, &pIdx, 0);. 
26fc7 20 20 20 20 20 69 66 28 20 70 49 64 78 20 29 7b       if( pIdx ){
26fc8 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30  .        for(i=0
26fc9 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  ; i<pIdx->nColum
26fca 6e 3b 20 69 2b 2b 29 20 6d 61 73 6b 20 7c 3d 20  n; i++) mask |= 
26fcb 43 4f 4c 55 4d 4e 5f 4d 41 53 4b 28 70 49 64 78  COLUMN_MASK(pIdx
26fcc 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a  ->aiColumn[i]);.
26fcd 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
26fce 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b  }.  return mask;
26fcf 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
26fd0 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
26fd1 64 20 62 65 66 6f 72 65 20 67 65 6e 65 72 61 74  d before generat
26fd2 69 6e 67 20 63 6f 64 65 20 74 6f 20 75 70 64 61  ing code to upda
26fd3 74 65 20 6f 72 20 64 65 6c 65 74 65 20 61 20 0a  te or delete a .
26fd4 2a 2a 20 72 6f 77 20 63 6f 6e 74 61 69 6e 65 64  ** row contained
26fd5 20 69 6e 20 74 61 62 6c 65 20 70 54 61 62 2e 20   in table pTab. 
26fd6 49 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  If the operation
26fd7 20 69 73 20 61 20 44 45 4c 45 54 45 2c 20 74 68   is a DELETE, th
26fd8 65 6e 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  en.** parameter 
26fd9 61 43 68 61 6e 67 65 20 69 73 20 70 61 73 73 65  aChange is passe
26fda 64 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 2e 20  d a NULL value. 
26fdb 46 6f 72 20 61 6e 20 55 50 44 41 54 45 2c 20 61  For an UPDATE, a
26fdc 43 68 61 6e 67 65 20 70 6f 69 6e 74 73 0a 2a 2a  Change points.**
26fdd 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20   to an array of 
26fde 73 69 7a 65 20 4e 2c 20 77 68 65 72 65 20 4e 20  size N, where N 
26fdf 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
26fe0 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 61 62 6c   columns in tabl
26fe1 65 20 70 54 61 62 2e 0a 2a 2a 20 49 66 20 74 68  e pTab..** If th
26fe2 65 20 69 27 74 68 20 63 6f 6c 75 6d 6e 20 69 73  e i'th column is
26fe3 20 6e 6f 74 20 6d 6f 64 69 66 69 65 64 20 62 79   not modified by
26fe4 20 74 68 65 20 55 50 44 41 54 45 2c 20 74 68 65   the UPDATE, the
26fe5 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  n the correspond
26fe6 69 6e 67 20 0a 2a 2a 20 65 6e 74 72 79 20 69 6e  ing .** entry in
26fe7 20 74 68 65 20 61 43 68 61 6e 67 65 5b 5d 20 61   the aChange[] a
26fe8 72 72 61 79 20 69 73 20 73 65 74 20 74 6f 20 2d  rray is set to -
26fe9 31 2e 20 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e  1. If the column
26fea 20 69 73 20 6d 6f 64 69 66 69 65 64 2c 0a 2a 2a   is modified,.**
26feb 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 30 20   the value is 0 
26fec 6f 72 20 67 72 65 61 74 65 72 2e 20 50 61 72 61  or greater. Para
26fed 6d 65 74 65 72 20 63 68 6e 67 52 6f 77 69 64 20  meter chngRowid 
26fee 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 69  is set to true i
26fef 66 20 74 68 65 0a 2a 2a 20 55 50 44 41 54 45 20  f the.** UPDATE 
26ff0 73 74 61 74 65 6d 65 6e 74 20 6d 6f 64 69 66 69  statement modifi
26ff1 65 73 20 74 68 65 20 72 6f 77 69 64 20 66 69 65  es the rowid fie
26ff2 6c 64 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  lds of the table
26ff3 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 66  ..**.** If any f
26ff4 6f 72 65 69 67 6e 20 6b 65 79 20 70 72 6f 63 65  oreign key proce
26ff5 73 73 69 6e 67 20 77 69 6c 6c 20 62 65 20 72 65  ssing will be re
26ff6 71 75 69 72 65 64 2c 20 74 68 69 73 20 66 75 6e  quired, this fun
26ff7 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 0a 2a 2a  ction returns.**
26ff8 20 74 72 75 65 2e 20 49 66 20 74 68 65 72 65 20   true. If there 
26ff9 69 73 20 6e 6f 20 66 6f 72 65 69 67 6e 20 6b 65  is no foreign ke
26ffa 79 20 72 65 6c 61 74 65 64 20 70 72 6f 63 65 73  y related proces
26ffb 73 69 6e 67 2c 20 74 68 69 73 20 66 75 6e 63 74  sing, this funct
26ffc 69 6f 6e 20 0a 2a 2a 20 72 65 74 75 72 6e 73 20  ion .** returns 
26ffd 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  false..*/.SQLITE
26ffe 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
26fff 69 74 65 33 46 6b 52 65 71 75 69 72 65 64 28 0a  ite3FkRequired(.
27000 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
27001 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27002 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65    /* Parse conte
27003 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  xt */.  Table *p
27004 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  Tab,            
27005 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
27006 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 20   being modified 
27007 2a 2f 0a 20 20 69 6e 74 20 2a 61 43 68 61 6e 67  */.  int *aChang
27008 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
27009 20 20 20 20 20 2f 2a 20 4e 6f 6e 2d 4e 55 4c 4c       /* Non-NULL
2700a 20 66 6f 72 20 55 50 44 41 54 45 20 6f 70 65 72   for UPDATE oper
2700b 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  ations */.  int 
2700c 63 68 6e 67 52 6f 77 69 64 20 20 20 20 20 20 20  chngRowid       
2700d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2700e 72 75 65 20 66 6f 72 20 55 50 44 41 54 45 20 74  rue for UPDATE t
2700f 68 61 74 20 61 66 66 65 63 74 73 20 72 6f 77 69  hat affects rowi
27010 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70 50  d */.){.  if( pP
27011 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 26  arse->db->flags&
27012 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65  SQLITE_ForeignKe
27013 79 73 20 29 7b 0a 20 20 20 20 69 66 28 20 21 61  ys ){.    if( !a
27014 43 68 61 6e 67 65 20 29 7b 0a 20 20 20 20 20 20  Change ){.      
27015 2f 2a 20 41 20 44 45 4c 45 54 45 20 6f 70 65 72  /* A DELETE oper
27016 61 74 69 6f 6e 2e 20 46 6f 72 65 69 67 6e 20 6b  ation. Foreign k
27017 65 79 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73  ey processing is
27018 20 72 65 71 75 69 72 65 64 20 69 66 20 74 68 65   required if the
27019 20 0a 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65   .      ** table
2701a 20 69 6e 20 71 75 65 73 74 69 6f 6e 20 69 73 20   in question is 
2701b 65 69 74 68 65 72 20 74 68 65 20 63 68 69 6c 64  either the child
2701c 20 6f 72 20 70 61 72 65 6e 74 20 74 61 62 6c 65   or parent table
2701d 20 66 6f 72 20 61 6e 79 20 0a 20 20 20 20 20 20   for any .      
2701e 2a 2a 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  ** foreign key c
2701f 6f 6e 73 74 72 61 69 6e 74 2e 20 20 2a 2f 0a 20  onstraint.  */. 
27020 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c       return (sql
27021 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73  ite3FkReferences
27022 28 70 54 61 62 29 20 7c 7c 20 70 54 61 62 2d 3e  (pTab) || pTab->
27023 70 46 4b 65 79 29 3b 0a 20 20 20 20 7d 65 6c 73  pFKey);.    }els
27024 65 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73  e{.      /* This
27025 20 69 73 20 61 6e 20 55 50 44 41 54 45 2e 20 46   is an UPDATE. F
27026 6f 72 65 69 67 6e 20 6b 65 79 20 70 72 6f 63 65  oreign key proce
27027 73 73 69 6e 67 20 69 73 20 6f 6e 6c 79 20 72 65  ssing is only re
27028 71 75 69 72 65 64 20 69 66 20 74 68 65 0a 20 20  quired if the.  
27029 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f 6e      ** operation
2702a 20 6d 6f 64 69 66 69 65 73 20 6f 6e 65 20 6f 72   modifies one or
2702b 20 6d 6f 72 65 20 63 68 69 6c 64 20 6f 72 20 70   more child or p
2702c 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e  arent key column
2702d 73 2e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  s. */.      int 
2702e 69 3b 0a 20 20 20 20 20 20 46 4b 65 79 20 2a 70  i;.      FKey *p
2702f 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63  ;..      /* Chec
27030 6b 20 69 66 20 61 6e 79 20 63 68 69 6c 64 20 6b  k if any child k
27031 65 79 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 62  ey columns are b
27032 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e 20 2a  eing modified. *
27033 2f 0a 20 20 20 20 20 20 66 6f 72 28 70 3d 70 54  /.      for(p=pT
27034 61 62 2d 3e 70 46 4b 65 79 3b 20 70 3b 20 70 3d  ab->pFKey; p; p=
27035 70 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20  p->pNextFrom){. 
27036 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20         for(i=0; 
27037 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  i<p->nCol; i++){
27038 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69  .          int i
27039 43 68 69 6c 64 4b 65 79 20 3d 20 70 2d 3e 61 43  ChildKey = p->aC
2703a 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 3b 0a 20 20 20  ol[i].iFrom;.   
2703b 20 20 20 20 20 20 20 69 66 28 20 61 43 68 61 6e         if( aChan
2703c 67 65 5b 69 43 68 69 6c 64 4b 65 79 5d 3e 3d 30  ge[iChildKey]>=0
2703d 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20   ) return 1;.   
2703e 20 20 20 20 20 20 20 69 66 28 20 69 43 68 69 6c         if( iChil
2703f 64 4b 65 79 3d 3d 70 54 61 62 2d 3e 69 50 4b 65  dKey==pTab->iPKe
27040 79 20 26 26 20 63 68 6e 67 52 6f 77 69 64 20 29  y && chngRowid )
27041 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
27042 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20     }.      }..  
27043 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20      /* Check if 
27044 61 6e 79 20 70 61 72 65 6e 74 20 6b 65 79 20 63  any parent key c
27045 6f 6c 75 6d 6e 73 20 61 72 65 20 62 65 69 6e 67  olumns are being
27046 20 6d 6f 64 69 66 69 65 64 2e 20 2a 2f 0a 20 20   modified. */.  
27047 20 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65      for(p=sqlite
27048 33 46 6b 52 65 66 65 72 65 6e 63 65 73 28 70 54  3FkReferences(pT
27049 61 62 29 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65  ab); p; p=p->pNe
2704a 78 74 54 6f 29 7b 0a 20 20 20 20 20 20 20 20 66  xtTo){.        f
2704b 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f  or(i=0; i<p->nCo
2704c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  l; i++){.       
2704d 20 20 20 63 68 61 72 20 2a 7a 4b 65 79 20 3d 20     char *zKey = 
2704e 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 3b  p->aCol[i].zCol;
2704f 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69  .          int i
27050 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20 66  Key;.          f
27051 6f 72 28 69 4b 65 79 3d 30 3b 20 69 4b 65 79 3c  or(iKey=0; iKey<
27052 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 4b 65 79  pTab->nCol; iKey
27053 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
27054 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20   Column *pCol = 
27055 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 4b 65 79  &pTab->aCol[iKey
27056 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ];.            i
27057 66 28 20 28 7a 4b 65 79 20 3f 20 21 73 71 6c 69  f( (zKey ? !sqli
27058 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 2d  te3StrICmp(pCol-
27059 3e 7a 4e 61 6d 65 2c 20 7a 4b 65 79 29 20 3a 20  >zName, zKey) : 
2705a 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65 79 29  pCol->isPrimKey)
2705b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2705c 20 20 69 66 28 20 61 43 68 61 6e 67 65 5b 69 4b    if( aChange[iK
2705d 65 79 5d 3e 3d 30 20 29 20 72 65 74 75 72 6e 20  ey]>=0 ) return 
2705e 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1;.             
2705f 20 69 66 28 20 69 4b 65 79 3d 3d 70 54 61 62 2d   if( iKey==pTab-
27060 3e 69 50 4b 65 79 20 26 26 20 63 68 6e 67 52 6f  >iPKey && chngRo
27061 77 69 64 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  wid ) return 1;.
27062 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
27063 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
27064 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
27065 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
27066 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
27067 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
27068 65 64 20 77 68 65 6e 20 61 6e 20 55 50 44 41 54  ed when an UPDAT
27069 45 20 6f 72 20 44 45 4c 45 54 45 20 6f 70 65 72  E or DELETE oper
2706a 61 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 0a  ation is being .
2706b 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 74  ** compiled on t
2706c 61 62 6c 65 20 70 54 61 62 2c 20 77 68 69 63 68  able pTab, which
2706d 20 69 73 20 74 68 65 20 70 61 72 65 6e 74 20 74   is the parent t
2706e 61 62 6c 65 20 6f 66 20 66 6f 72 65 69 67 6e 2d  able of foreign-
2706f 6b 65 79 20 70 46 4b 65 79 2e 0a 2a 2a 20 49 66  key pFKey..** If
27070 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70 65   the current ope
27071 72 61 74 69 6f 6e 20 69 73 20 61 6e 20 55 50 44  ration is an UPD
27072 41 54 45 2c 20 74 68 65 6e 20 74 68 65 20 70 43  ATE, then the pC
27073 68 61 6e 67 65 73 20 70 61 72 61 6d 65 74 65 72  hanges parameter
27074 20 69 73 0a 2a 2a 20 70 61 73 73 65 64 20 61 20   is.** passed a 
27075 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6c  pointer to the l
27076 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 62  ist of columns b
27077 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e 20 49  eing modified. I
27078 66 20 69 74 20 69 73 20 61 0a 2a 2a 20 44 45 4c  f it is a.** DEL
27079 45 54 45 2c 20 70 43 68 61 6e 67 65 73 20 69 73  ETE, pChanges is
2707a 20 70 61 73 73 65 64 20 61 20 4e 55 4c 4c 20 70   passed a NULL p
2707b 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 74  ointer..**.** It
2707c 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
2707d 65 72 20 74 6f 20 61 20 54 72 69 67 67 65 72 20  er to a Trigger 
2707e 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
2707f 6e 69 6e 67 20 61 20 74 72 69 67 67 65 72 0a 2a  ning a trigger.*
27080 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20  * equivalent to 
27081 74 68 65 20 4f 4e 20 55 50 44 41 54 45 20 6f 72  the ON UPDATE or
27082 20 4f 4e 20 44 45 4c 45 54 45 20 61 63 74 69 6f   ON DELETE actio
27083 6e 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70  n specified by p
27084 46 4b 65 79 2e 0a 2a 2a 20 49 66 20 74 68 65 20  FKey..** If the 
27085 61 63 74 69 6f 6e 20 69 73 20 22 4e 4f 20 41 43  action is "NO AC
27086 54 49 4f 4e 22 20 6f 72 20 22 52 45 53 54 52 49  TION" or "RESTRI
27087 43 54 22 2c 20 74 68 65 6e 20 61 20 4e 55 4c 4c  CT", then a NULL
27088 20 70 6f 69 6e 74 65 72 20 69 73 0a 2a 2a 20 72   pointer is.** r
27089 65 74 75 72 6e 65 64 20 28 74 68 65 73 65 20 61  eturned (these a
2708a 63 74 69 6f 6e 73 20 72 65 71 75 69 72 65 20 6e  ctions require n
2708b 6f 20 73 70 65 63 69 61 6c 20 68 61 6e 64 6c 69  o special handli
2708c 6e 67 20 62 79 20 74 68 65 20 74 72 69 67 67 65  ng by the trigge
2708d 72 73 0a 2a 2a 20 73 75 62 2d 73 79 73 74 65 6d  rs.** sub-system
2708e 2c 20 63 6f 64 65 20 66 6f 72 20 74 68 65 6d 20  , code for them 
2708f 69 73 20 63 72 65 61 74 65 64 20 62 79 20 66 6b  is created by fk
27090 53 63 61 6e 43 68 69 6c 64 72 65 6e 28 29 29 2e  ScanChildren()).
27091 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70  .**.** For examp
27092 6c 65 2c 20 69 66 20 70 46 4b 65 79 20 69 73 20  le, if pFKey is 
27093 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  the foreign key 
27094 61 6e 64 20 70 54 61 62 20 69 73 20 74 61 62 6c  and pTab is tabl
27095 65 20 22 70 22 20 69 6e 20 0a 2a 2a 20 74 68 65  e "p" in .** the
27096 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 63 68 65 6d   following schem
27097 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 43 52 45 41 54  a:.**.**   CREAT
27098 45 20 54 41 42 4c 45 20 70 28 70 6b 20 50 52 49  E TABLE p(pk PRI
27099 4d 41 52 59 20 4b 45 59 29 3b 0a 2a 2a 20 20 20  MARY KEY);.**   
2709a 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 28 63  CREATE TABLE c(c
2709b 6b 20 52 45 46 45 52 45 4e 43 45 53 20 70 20 4f  k REFERENCES p O
2709c 4e 20 44 45 4c 45 54 45 20 43 41 53 43 41 44 45  N DELETE CASCADE
2709d 29 3b 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 74 68  );.**.** then th
2709e 65 20 72 65 74 75 72 6e 65 64 20 74 72 69 67 67  e returned trigg
2709f 65 72 20 73 74 72 75 63 74 75 72 65 20 69 73 20  er structure is 
270a0 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a 0a 2a  equivalent to:.*
270a1 2a 0a 2a 2a 20 20 20 43 52 45 41 54 45 20 54 52  *.**   CREATE TR
270a2 49 47 47 45 52 20 2e 2e 2e 20 44 45 4c 45 54 45  IGGER ... DELETE
270a3 20 4f 4e 20 70 20 42 45 47 49 4e 0a 2a 2a 20 20   ON p BEGIN.**  
270a4 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 63     DELETE FROM c
270a5 20 57 48 45 52 45 20 63 6b 20 3d 20 6f 6c 64 2e   WHERE ck = old.
270a6 70 6b 3b 0a 2a 2a 20 20 20 45 4e 44 3b 0a 2a 2a  pk;.**   END;.**
270a7 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64  .** The returned
270a8 20 70 6f 69 6e 74 65 72 20 69 73 20 63 61 63 68   pointer is cach
270a9 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68  ed as part of th
270aa 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 62  e foreign key ob
270ab 6a 65 63 74 2e 20 49 74 0a 2a 2a 20 69 73 20 65  ject. It.** is e
270ac 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 20  ventually freed 
270ad 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 72  along with the r
270ae 65 73 74 20 6f 66 20 74 68 65 20 66 6f 72 65 69  est of the forei
270af 67 6e 20 6b 65 79 20 6f 62 6a 65 63 74 20 62 79  gn key object by
270b0 20 0a 2a 2a 20 73 71 6c 69 74 65 33 46 6b 44 65   .** sqlite3FkDe
270b1 6c 65 74 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  lete()..*/.stati
270b2 63 20 54 72 69 67 67 65 72 20 2a 66 6b 41 63 74  c Trigger *fkAct
270b3 69 6f 6e 54 72 69 67 67 65 72 28 0a 20 20 50 61  ionTrigger(.  Pa
270b4 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
270b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
270b6 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a   Parse context *
270b7 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
270b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
270b9 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69      /* Table bei
270ba 6e 67 20 75 70 64 61 74 65 64 20 6f 72 20 64 65  ng updated or de
270bb 6c 65 74 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20  leted from */.  
270bc 46 4b 65 79 20 2a 70 46 4b 65 79 2c 20 20 20 20  FKey *pFKey,    
270bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
270be 2f 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79 20 74  /* Foreign key t
270bf 6f 20 67 65 74 20 61 63 74 69 6f 6e 20 66 6f 72  o get action for
270c0 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
270c1 70 43 68 61 6e 67 65 73 20 20 20 20 20 20 20 20  pChanges        
270c2 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 65 2d        /* Change-
270c3 6c 69 73 74 20 66 6f 72 20 55 50 44 41 54 45 2c  list for UPDATE,
270c4 20 4e 55 4c 4c 20 66 6f 72 20 44 45 4c 45 54 45   NULL for DELETE
270c5 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
270c6 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
270c7 62 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61  b;       /* Data
270c8 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  base handle */. 
270c9 20 69 6e 74 20 61 63 74 69 6f 6e 3b 20 20 20 20   int action;    
270ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
270cb 20 2f 2a 20 4f 6e 65 20 6f 66 20 4f 45 5f 4e 6f   /* One of OE_No
270cc 6e 65 2c 20 4f 45 5f 43 61 73 63 61 64 65 20 65  ne, OE_Cascade e
270cd 74 63 2e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  tc. */.  Trigger
270ce 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 20 20   *pTrigger;     
270cf 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 69 67           /* Trig
270d0 67 65 72 20 64 65 66 69 6e 69 74 69 6f 6e 20 74  ger definition t
270d1 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e  o return */.  in
270d2 74 20 69 41 63 74 69 6f 6e 20 3d 20 28 70 43 68  t iAction = (pCh
270d3 61 6e 67 65 73 21 3d 30 29 3b 20 20 20 20 2f 2a  anges!=0);    /*
270d4 20 31 20 66 6f 72 20 55 50 44 41 54 45 2c 20 30   1 for UPDATE, 0
270d5 20 66 6f 72 20 44 45 4c 45 54 45 20 2a 2f 0a 0a   for DELETE */..
270d6 20 20 61 63 74 69 6f 6e 20 3d 20 70 46 4b 65 79    action = pFKey
270d7 2d 3e 61 41 63 74 69 6f 6e 5b 69 41 63 74 69 6f  ->aAction[iActio
270d8 6e 5d 3b 0a 20 20 70 54 72 69 67 67 65 72 20 3d  n];.  pTrigger =
270d9 20 70 46 4b 65 79 2d 3e 61 70 54 72 69 67 67 65   pFKey->apTrigge
270da 72 5b 69 41 63 74 69 6f 6e 5d 3b 0a 0a 20 20 69  r[iAction];..  i
270db 66 28 20 61 63 74 69 6f 6e 21 3d 4f 45 5f 4e 6f  f( action!=OE_No
270dc 6e 65 20 26 26 20 21 70 54 72 69 67 67 65 72 20  ne && !pTrigger 
270dd 29 7b 0a 20 20 20 20 75 38 20 65 6e 61 62 6c 65  ){.    u8 enable
270de 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20  Lookaside;      
270df 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20       /* Copy of 
270e0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45  db->lookaside.bE
270e1 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 20 20 63 68  nabled */.    ch
270e2 61 72 20 63 6f 6e 73 74 20 2a 7a 46 72 6f 6d 3b  ar const *zFrom;
270e3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
270e4 61 6d 65 20 6f 66 20 63 68 69 6c 64 20 74 61 62  ame of child tab
270e5 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 46  le */.    int nF
270e6 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  rom;            
270e7 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
270e8 68 20 69 6e 20 62 79 74 65 73 20 6f 66 20 7a 46  h in bytes of zF
270e9 72 6f 6d 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78  rom */.    Index
270ea 20 2a 70 49 64 78 20 3d 20 30 3b 20 20 20 20 20   *pIdx = 0;     
270eb 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65           /* Pare
270ec 6e 74 20 6b 65 79 20 69 6e 64 65 78 20 66 6f 72  nt key index for
270ed 20 74 68 69 73 20 46 4b 20 2a 2f 0a 20 20 20 20   this FK */.    
270ee 69 6e 74 20 2a 61 69 43 6f 6c 20 3d 20 30 3b 20  int *aiCol = 0; 
270ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
270f0 20 63 68 69 6c 64 20 74 61 62 6c 65 20 63 6f 6c   child table col
270f1 73 20 2d 3e 20 70 61 72 65 6e 74 20 6b 65 79 20  s -> parent key 
270f2 63 6f 6c 73 20 2a 2f 0a 20 20 20 20 54 72 69 67  cols */.    Trig
270f3 67 65 72 53 74 65 70 20 2a 70 53 74 65 70 20 3d  gerStep *pStep =
270f4 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69   0;        /* Fi
270f5 72 73 74 20 28 6f 6e 6c 79 29 20 73 74 65 70 20  rst (only) step 
270f6 6f 66 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  of trigger progr
270f7 61 6d 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a  am */.    Expr *
270f8 70 57 68 65 72 65 20 3d 20 30 3b 20 20 20 20 20  pWhere = 0;     
270f9 20 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45          /* WHERE
270fa 20 63 6c 61 75 73 65 20 6f 66 20 74 72 69 67 67   clause of trigg
270fb 65 72 20 73 74 65 70 20 2a 2f 0a 20 20 20 20 45  er step */.    E
270fc 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d  xprList *pList =
270fd 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20   0;          /* 
270fe 43 68 61 6e 67 65 73 20 6c 69 73 74 20 69 66 20  Changes list if 
270ff 4f 4e 20 55 50 44 41 54 45 20 43 41 53 43 41 44  ON UPDATE CASCAD
27100 45 20 2a 2f 0a 20 20 20 20 53 65 6c 65 63 74 20  E */.    Select 
27101 2a 70 53 65 6c 65 63 74 20 3d 20 30 3b 20 20 20  *pSelect = 0;   
27102 20 20 20 20 20 20 20 2f 2a 20 49 66 20 52 45 53         /* If RES
27103 54 52 49 43 54 2c 20 22 53 45 4c 45 43 54 20 52  TRICT, "SELECT R
27104 41 49 53 45 28 2e 2e 2e 29 22 20 2a 2f 0a 20 20  AISE(...)" */.  
27105 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
27106 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27107 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
27108 61 62 6c 65 20 2a 2f 0a 20 20 20 20 45 78 70 72  able */.    Expr
27109 20 2a 70 57 68 65 6e 20 3d 20 30 3b 20 20 20 20   *pWhen = 0;    
2710a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 48 45            /* WHE
2710b 4e 20 63 6c 61 75 73 65 20 66 6f 72 20 74 68 65  N clause for the
2710c 20 74 72 69 67 67 65 72 20 2a 2f 0a 0a 20 20 20   trigger */..   
2710d 20 69 66 28 20 6c 6f 63 61 74 65 46 6b 65 79 49   if( locateFkeyI
2710e 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 54 61  ndex(pParse, pTa
2710f 62 2c 20 70 46 4b 65 79 2c 20 26 70 49 64 78 2c  b, pFKey, &pIdx,
27110 20 26 61 69 43 6f 6c 29 20 29 20 72 65 74 75 72   &aiCol) ) retur
27111 6e 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28  n 0;.    assert(
27112 20 61 69 43 6f 6c 20 7c 7c 20 70 46 4b 65 79 2d   aiCol || pFKey-
27113 3e 6e 43 6f 6c 3d 3d 31 20 29 3b 0a 0a 20 20 20  >nCol==1 );..   
27114 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46 4b 65   for(i=0; i<pFKe
27115 79 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20  y->nCol; i++){. 
27116 20 20 20 20 20 54 6f 6b 65 6e 20 74 4f 6c 64 20       Token tOld 
27117 3d 20 7b 20 22 6f 6c 64 22 2c 20 33 20 7d 3b 20  = { "old", 3 }; 
27118 20 2f 2a 20 4c 69 74 65 72 61 6c 20 22 6f 6c 64   /* Literal "old
27119 22 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 20  " token */.     
2711a 20 54 6f 6b 65 6e 20 74 4e 65 77 20 3d 20 7b 20   Token tNew = { 
2711b 22 6e 65 77 22 2c 20 33 20 7d 3b 20 20 2f 2a 20  "new", 3 };  /* 
2711c 4c 69 74 65 72 61 6c 20 22 6e 65 77 22 20 74 6f  Literal "new" to
2711d 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 54 6f 6b  ken */.      Tok
2711e 65 6e 20 74 46 72 6f 6d 43 6f 6c 3b 20 20 20 20  en tFromCol;    
2711f 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
27120 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 63 68   of column in ch
27121 69 6c 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  ild table */.   
27122 20 20 20 54 6f 6b 65 6e 20 74 54 6f 43 6f 6c 3b     Token tToCol;
27123 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
27124 2a 20 4e 61 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e  * Name of column
27125 20 69 6e 20 70 61 72 65 6e 74 20 74 61 62 6c 65   in parent table
27126 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 46   */.      int iF
27127 72 6f 6d 43 6f 6c 3b 20 20 20 20 20 20 20 20 20  romCol;         
27128 20 20 20 20 20 20 2f 2a 20 49 64 78 20 6f 66 20        /* Idx of 
27129 63 6f 6c 75 6d 6e 20 69 6e 20 63 68 69 6c 64 20  column in child 
2712a 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 45  table */.      E
2712b 78 70 72 20 2a 70 45 71 3b 20 20 20 20 20 20 20  xpr *pEq;       
2712c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 46             /* tF
2712d 72 6f 6d 43 6f 6c 20 3d 20 4f 4c 44 2e 74 54 6f  romCol = OLD.tTo
2712e 43 6f 6c 20 2a 2f 0a 0a 20 20 20 20 20 20 69 46  Col */..      iF
2712f 72 6f 6d 43 6f 6c 20 3d 20 61 69 43 6f 6c 20 3f  romCol = aiCol ?
27130 20 61 69 43 6f 6c 5b 69 5d 20 3a 20 70 46 4b 65   aiCol[i] : pFKe
27131 79 2d 3e 61 43 6f 6c 5b 30 5d 2e 69 46 72 6f 6d  y->aCol[0].iFrom
27132 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
27133 69 46 72 6f 6d 43 6f 6c 3e 3d 30 20 29 3b 0a 20  iFromCol>=0 );. 
27134 20 20 20 20 20 74 54 6f 43 6f 6c 2e 7a 20 3d 20       tToCol.z = 
27135 70 49 64 78 20 3f 20 70 54 61 62 2d 3e 61 43 6f  pIdx ? pTab->aCo
27136 6c 5b 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  l[pIdx->aiColumn
27137 5b 69 5d 5d 2e 7a 4e 61 6d 65 20 3a 20 22 6f 69  [i]].zName : "oi
27138 64 22 3b 0a 20 20 20 20 20 20 74 46 72 6f 6d 43  d";.      tFromC
27139 6f 6c 2e 7a 20 3d 20 70 46 4b 65 79 2d 3e 70 46  ol.z = pFKey->pF
2713a 72 6f 6d 2d 3e 61 43 6f 6c 5b 69 46 72 6f 6d 43  rom->aCol[iFromC
2713b 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20 20 20 20  ol].zName;..    
2713c 20 20 74 54 6f 43 6f 6c 2e 6e 20 3d 20 73 71 6c    tToCol.n = sql
2713d 69 74 65 33 53 74 72 6c 65 6e 33 30 28 74 54 6f  ite3Strlen30(tTo
2713e 43 6f 6c 2e 7a 29 3b 0a 20 20 20 20 20 20 74 46  Col.z);.      tF
2713f 72 6f 6d 43 6f 6c 2e 6e 20 3d 20 73 71 6c 69 74  romCol.n = sqlit
27140 65 33 53 74 72 6c 65 6e 33 30 28 74 46 72 6f 6d  e3Strlen30(tFrom
27141 43 6f 6c 2e 7a 29 3b 0a 0a 20 20 20 20 20 20 2f  Col.z);..      /
27142 2a 20 43 72 65 61 74 65 20 74 68 65 20 65 78 70  * Create the exp
27143 72 65 73 73 69 6f 6e 20 22 4f 4c 44 2e 7a 54 6f  ression "OLD.zTo
27144 43 6f 6c 20 3d 20 7a 46 72 6f 6d 43 6f 6c 22 2e  Col = zFromCol".
27145 20 49 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74   It is important
27146 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 74  .      ** that t
27147 68 65 20 22 4f 4c 44 2e 7a 54 6f 43 6f 6c 22 20  he "OLD.zToCol" 
27148 74 65 72 6d 20 69 73 20 6f 6e 20 74 68 65 20 4c  term is on the L
27149 48 53 20 6f 66 20 74 68 65 20 3d 20 6f 70 65 72  HS of the = oper
2714a 61 74 6f 72 2c 20 73 6f 0a 20 20 20 20 20 20 2a  ator, so.      *
2714b 2a 20 74 68 61 74 20 74 68 65 20 61 66 66 69 6e  * that the affin
2714c 69 74 79 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f  ity and collatio
2714d 6e 20 73 65 71 75 65 6e 63 65 20 61 73 73 6f 63  n sequence assoc
2714e 69 61 74 65 64 20 77 69 74 68 20 74 68 65 0a 20  iated with the. 
2714f 20 20 20 20 20 2a 2a 20 70 61 72 65 6e 74 20 74       ** parent t
27150 61 62 6c 65 20 61 72 65 20 75 73 65 64 20 66 6f  able are used fo
27151 72 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  r the comparison
27152 2e 20 2a 2f 0a 20 20 20 20 20 20 70 45 71 20 3d  . */.      pEq =
27153 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
27154 61 72 73 65 2c 20 54 4b 5f 45 51 2c 0a 20 20 20  arse, TK_EQ,.   
27155 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 45         sqlite3PE
27156 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44  xpr(pParse, TK_D
27157 4f 54 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  OT, .           
27158 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
27159 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20  arse, TK_ID, 0, 
2715a 30 2c 20 26 74 4f 6c 64 29 2c 0a 20 20 20 20 20  0, &tOld),.     
2715b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 45         sqlite3PE
2715c 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
2715d 44 2c 20 30 2c 20 30 2c 20 26 74 54 6f 43 6f 6c  D, 0, 0, &tToCol
2715e 29 0a 20 20 20 20 20 20 20 20 20 20 2c 20 30 29  ).          , 0)
2715f 2c 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ,.          sqli
27160 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
27161 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74   TK_ID, 0, 0, &t
27162 46 72 6f 6d 43 6f 6c 29 0a 20 20 20 20 20 20 2c  FromCol).      ,
27163 20 30 29 3b 0a 20 20 20 20 20 20 70 57 68 65 72   0);.      pWher
27164 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  e = sqlite3ExprA
27165 6e 64 28 64 62 2c 20 70 57 68 65 72 65 2c 20 70  nd(db, pWhere, p
27166 45 71 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 46  Eq);..      /* F
27167 6f 72 20 4f 4e 20 55 50 44 41 54 45 2c 20 63 6f  or ON UPDATE, co
27168 6e 73 74 72 75 63 74 20 74 68 65 20 6e 65 78 74  nstruct the next
27169 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45   term of the WHE
2716a 4e 20 63 6c 61 75 73 65 2e 0a 20 20 20 20 20 20  N clause..      
2716b 2a 2a 20 54 68 65 20 66 69 6e 61 6c 20 57 48 45  ** The final WHE
2716c 4e 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 62 65  N clause will be
2716d 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20 20 20   like this:.    
2716e 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20    **.      **   
2716f 20 57 48 45 4e 20 4e 4f 54 28 6f 6c 64 2e 63 6f   WHEN NOT(old.co
27170 6c 31 20 49 53 20 6e 65 77 2e 63 6f 6c 31 20 41  l1 IS new.col1 A
27171 4e 44 20 2e 2e 2e 20 41 4e 44 20 6f 6c 64 2e 63  ND ... AND old.c
27172 6f 6c 4e 20 49 53 20 6e 65 77 2e 63 6f 6c 4e 29  olN IS new.colN)
27173 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
27174 69 66 28 20 70 43 68 61 6e 67 65 73 20 29 7b 0a  if( pChanges ){.
27175 20 20 20 20 20 20 20 20 70 45 71 20 3d 20 73 71          pEq = sq
27176 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
27177 65 2c 20 54 4b 5f 49 53 2c 0a 20 20 20 20 20 20  e, TK_IS,.      
27178 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 45 78        sqlite3PEx
27179 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f  pr(pParse, TK_DO
2717a 54 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  T, .            
2717b 20 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70    sqlite3PExpr(p
2717c 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c  Parse, TK_ID, 0,
2717d 20 30 2c 20 26 74 4f 6c 64 29 2c 0a 20 20 20 20   0, &tOld),.    
2717e 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2717f 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
27180 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74 54 6f  K_ID, 0, 0, &tTo
27181 43 6f 6c 29 2c 0a 20 20 20 20 20 20 20 20 20 20  Col),.          
27182 20 20 20 20 30 29 2c 0a 20 20 20 20 20 20 20 20      0),.        
27183 20 20 20 20 73 71 6c 69 74 65 33 50 45 78 70 72      sqlite3PExpr
27184 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c  (pParse, TK_DOT,
27185 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
27186 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
27187 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
27188 2c 20 26 74 4e 65 77 29 2c 0a 20 20 20 20 20 20  , &tNew),.      
27189 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
2718a 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
2718b 49 44 2c 20 30 2c 20 30 2c 20 26 74 54 6f 43 6f  ID, 0, 0, &tToCo
2718c 6c 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  l),.            
2718d 20 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20    0),.          
2718e 20 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 57    0);.        pW
2718f 68 65 6e 20 3d 20 73 71 6c 69 74 65 33 45 78 70  hen = sqlite3Exp
27190 72 41 6e 64 28 64 62 2c 20 70 57 68 65 6e 2c 20  rAnd(db, pWhen, 
27191 70 45 71 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  pEq);.      }.  
27192 0a 20 20 20 20 20 20 69 66 28 20 61 63 74 69 6f  .      if( actio
27193 6e 21 3d 4f 45 5f 52 65 73 74 72 69 63 74 20 26  n!=OE_Restrict &
27194 26 20 28 61 63 74 69 6f 6e 21 3d 4f 45 5f 43 61  & (action!=OE_Ca
27195 73 63 61 64 65 20 7c 7c 20 70 43 68 61 6e 67 65  scade || pChange
27196 73 29 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78  s) ){.        Ex
27197 70 72 20 2a 70 4e 65 77 3b 0a 20 20 20 20 20 20  pr *pNew;.      
27198 20 20 69 66 28 20 61 63 74 69 6f 6e 3d 3d 4f 45    if( action==OE
27199 5f 43 61 73 63 61 64 65 20 29 7b 0a 20 20 20 20  _Cascade ){.    
2719a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c        pNew = sql
2719b 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2719c 2c 20 54 4b 5f 44 4f 54 2c 20 0a 20 20 20 20 20  , TK_DOT, .     
2719d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 45         sqlite3PE
2719e 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
2719f 44 2c 20 30 2c 20 30 2c 20 26 74 4e 65 77 29 2c  D, 0, 0, &tNew),
271a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
271a1 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
271a2 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
271a3 74 54 6f 43 6f 6c 29 0a 20 20 20 20 20 20 20 20  tToCol).        
271a4 20 20 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20    , 0);.        
271a5 7d 65 6c 73 65 20 69 66 28 20 61 63 74 69 6f 6e  }else if( action
271a6 3d 3d 4f 45 5f 53 65 74 44 66 6c 74 20 29 7b 0a  ==OE_SetDflt ){.
271a7 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 2a            Expr *
271a8 70 44 66 6c 74 20 3d 20 70 46 4b 65 79 2d 3e 70  pDflt = pFKey->p
271a9 46 72 6f 6d 2d 3e 61 43 6f 6c 5b 69 46 72 6f 6d  From->aCol[iFrom
271aa 43 6f 6c 5d 2e 70 44 66 6c 74 3b 0a 20 20 20 20  Col].pDflt;.    
271ab 20 20 20 20 20 20 69 66 28 20 70 44 66 6c 74 20        if( pDflt 
271ac 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ){.            p
271ad 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70  New = sqlite3Exp
271ae 72 44 75 70 28 64 62 2c 20 70 44 66 6c 74 2c 20  rDup(db, pDflt, 
271af 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  0);.          }e
271b0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
271b1 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 50   pNew = sqlite3P
271b2 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
271b3 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  NULL, 0, 0, 0);.
271b4 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
271b5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
271b6 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69       pNew = sqli
271b7 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
271b8 20 54 4b 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20   TK_NULL, 0, 0, 
271b9 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  0);.        }.  
271ba 20 20 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71        pList = sq
271bb 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
271bc 65 6e 64 28 70 50 61 72 73 65 2c 20 70 4c 69 73  end(pParse, pLis
271bd 74 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 20 20  t, pNew);.      
271be 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
271bf 74 53 65 74 4e 61 6d 65 28 70 50 61 72 73 65 2c  tSetName(pParse,
271c0 20 70 4c 69 73 74 2c 20 26 74 46 72 6f 6d 43 6f   pList, &tFromCo
271c1 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  l, 0);.      }. 
271c2 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
271c3 44 62 46 72 65 65 28 64 62 2c 20 61 69 43 6f 6c  DbFree(db, aiCol
271c4 29 3b 0a 0a 20 20 20 20 7a 46 72 6f 6d 20 3d 20  );..    zFrom = 
271c5 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 2d 3e 7a 4e  pFKey->pFrom->zN
271c6 61 6d 65 3b 0a 20 20 20 20 6e 46 72 6f 6d 20 3d  ame;.    nFrom =
271c7 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
271c8 28 7a 46 72 6f 6d 29 3b 0a 0a 20 20 20 20 69 66  (zFrom);..    if
271c9 28 20 61 63 74 69 6f 6e 3d 3d 4f 45 5f 52 65 73  ( action==OE_Res
271ca 74 72 69 63 74 20 29 7b 0a 20 20 20 20 20 20 54  trict ){.      T
271cb 6f 6b 65 6e 20 74 46 72 6f 6d 3b 0a 20 20 20 20  oken tFrom;.    
271cc 20 20 45 78 70 72 20 2a 70 52 61 69 73 65 3b 20    Expr *pRaise; 
271cd 0a 0a 20 20 20 20 20 20 74 46 72 6f 6d 2e 7a 20  ..      tFrom.z 
271ce 3d 20 7a 46 72 6f 6d 3b 0a 20 20 20 20 20 20 74  = zFrom;.      t
271cf 46 72 6f 6d 2e 6e 20 3d 20 6e 46 72 6f 6d 3b 0a  From.n = nFrom;.
271d0 20 20 20 20 20 20 70 52 61 69 73 65 20 3d 20 73        pRaise = s
271d1 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20 54  qlite3Expr(db, T
271d2 4b 5f 52 41 49 53 45 2c 20 22 66 6f 72 65 69 67  K_RAISE, "foreig
271d3 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
271d4 20 66 61 69 6c 65 64 22 29 3b 0a 20 20 20 20 20   failed");.     
271d5 20 69 66 28 20 70 52 61 69 73 65 20 29 7b 0a 20   if( pRaise ){. 
271d6 20 20 20 20 20 20 20 70 52 61 69 73 65 2d 3e 61         pRaise->a
271d7 66 66 69 6e 69 74 79 20 3d 20 4f 45 5f 41 62 6f  ffinity = OE_Abo
271d8 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rt;.      }.    
271d9 20 20 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69    pSelect = sqli
271da 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61  te3SelectNew(pPa
271db 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20  rse, .          
271dc 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
271dd 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c  ppend(pParse, 0,
271de 20 70 52 61 69 73 65 29 2c 0a 20 20 20 20 20 20   pRaise),.      
271df 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69      sqlite3SrcLi
271e0 73 74 41 70 70 65 6e 64 28 64 62 2c 20 30 2c 20  stAppend(db, 0, 
271e1 26 74 46 72 6f 6d 2c 20 30 29 2c 0a 20 20 20 20  &tFrom, 0),.    
271e2 20 20 20 20 20 20 70 57 68 65 72 65 2c 0a 20 20        pWhere,.  
271e3 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c          0, 0, 0,
271e4 20 30 2c 20 30 2c 20 30 0a 20 20 20 20 20 20 29   0, 0, 0.      )
271e5 3b 0a 20 20 20 20 20 20 70 57 68 65 72 65 20 3d  ;.      pWhere =
271e6 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f   0;.    }..    /
271e7 2a 20 49 6e 20 74 68 65 20 63 75 72 72 65 6e 74  * In the current
271e8 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c   implementation,
271e9 20 70 54 61 62 2d 3e 64 62 4d 65 6d 3d 3d 30 20   pTab->dbMem==0 
271ea 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73 20 65  for all tables e
271eb 78 63 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72  xcept.    ** for
271ec 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
271ed 73 20 75 73 65 64 20 74 6f 20 64 65 73 63 72 69  s used to descri
271ee 62 65 20 73 75 62 71 75 65 72 69 65 73 2e 20 20  be subqueries.  
271ef 41 6e 64 20 74 65 6d 70 6f 72 61 72 79 0a 20 20  And temporary.  
271f0 20 20 2a 2a 20 74 61 62 6c 65 73 20 64 6f 20 6e    ** tables do n
271f1 6f 74 20 68 61 76 65 20 66 6f 72 65 69 67 6e 20  ot have foreign 
271f2 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e  key constraints.
271f3 20 20 48 65 6e 63 65 2c 20 70 54 61 62 2d 3e 64    Hence, pTab->d
271f4 62 4d 65 6d 0a 20 20 20 20 2a 2a 20 73 68 6f 75  bMem.    ** shou
271f5 6c 64 20 61 6c 77 61 79 73 20 62 65 20 30 20 74  ld always be 0 t
271f6 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  here..    */.   
271f7 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65   enableLookaside
271f8 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65   = db->lookaside
271f9 2e 62 45 6e 61 62 6c 65 64 3b 0a 20 20 20 20 64  .bEnabled;.    d
271fa 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e  b->lookaside.bEn
271fb 61 62 6c 65 64 20 3d 20 30 3b 0a 0a 20 20 20 20  abled = 0;..    
271fc 70 54 72 69 67 67 65 72 20 3d 20 28 54 72 69 67  pTrigger = (Trig
271fd 67 65 72 20 2a 29 73 71 6c 69 74 65 33 44 62 4d  ger *)sqlite3DbM
271fe 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 0a 20  allocZero(db, . 
271ff 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 54 72         sizeof(Tr
27200 69 67 67 65 72 29 20 2b 20 20 20 20 20 20 20 20  igger) +        
27201 20 2f 2a 20 73 74 72 75 63 74 20 54 72 69 67 67   /* struct Trigg
27202 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 69  er */.        si
27203 7a 65 6f 66 28 54 72 69 67 67 65 72 53 74 65 70  zeof(TriggerStep
27204 29 20 2b 20 20 20 20 20 2f 2a 20 53 69 6e 67 6c  ) +     /* Singl
27205 65 20 73 74 65 70 20 69 6e 20 74 72 69 67 67 65  e step in trigge
27206 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 20  r program */.   
27207 20 20 20 20 20 6e 46 72 6f 6d 20 2b 20 31 20 20       nFrom + 1  
27208 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
27209 2a 20 53 70 61 63 65 20 66 6f 72 20 70 53 74 65  * Space for pSte
2720a 70 2d 3e 74 61 72 67 65 74 2e 7a 20 2a 2f 0a 20  p->target.z */. 
2720b 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 70 54     );.    if( pT
2720c 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 20 20  rigger ){.      
2720d 70 53 74 65 70 20 3d 20 70 54 72 69 67 67 65 72  pStep = pTrigger
2720e 2d 3e 73 74 65 70 5f 6c 69 73 74 20 3d 20 28 54  ->step_list = (T
2720f 72 69 67 67 65 72 53 74 65 70 20 2a 29 26 70 54  riggerStep *)&pT
27210 72 69 67 67 65 72 5b 31 5d 3b 0a 20 20 20 20 20  rigger[1];.     
27211 20 70 53 74 65 70 2d 3e 74 61 72 67 65 74 2e 7a   pStep->target.z
27212 20 3d 20 28 63 68 61 72 20 2a 29 26 70 53 74 65   = (char *)&pSte
27213 70 5b 31 5d 3b 0a 20 20 20 20 20 20 70 53 74 65  p[1];.      pSte
27214 70 2d 3e 74 61 72 67 65 74 2e 6e 20 3d 20 6e 46  p->target.n = nF
27215 72 6f 6d 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70  rom;.      memcp
27216 79 28 28 63 68 61 72 20 2a 29 70 53 74 65 70 2d  y((char *)pStep-
27217 3e 74 61 72 67 65 74 2e 7a 2c 20 7a 46 72 6f 6d  >target.z, zFrom
27218 2c 20 6e 46 72 6f 6d 29 3b 0a 20 20 0a 20 20 20  , nFrom);.  .   
27219 20 20 20 70 53 74 65 70 2d 3e 70 57 68 65 72 65     pStep->pWhere
2721a 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
2721b 70 28 64 62 2c 20 70 57 68 65 72 65 2c 20 45 58  p(db, pWhere, EX
2721c 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20  PRDUP_REDUCE);. 
2721d 20 20 20 20 20 70 53 74 65 70 2d 3e 70 45 78 70       pStep->pExp
2721e 72 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45  rList = sqlite3E
2721f 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70  xprListDup(db, p
27220 4c 69 73 74 2c 20 45 58 50 52 44 55 50 5f 52 45  List, EXPRDUP_RE
27221 44 55 43 45 29 3b 0a 20 20 20 20 20 20 70 53 74  DUCE);.      pSt
27222 65 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71  ep->pSelect = sq
27223 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64  lite3SelectDup(d
27224 62 2c 20 70 53 65 6c 65 63 74 2c 20 45 58 50 52  b, pSelect, EXPR
27225 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20  DUP_REDUCE);.   
27226 20 20 20 69 66 28 20 70 57 68 65 6e 20 29 7b 0a     if( pWhen ){.
27227 20 20 20 20 20 20 20 20 70 57 68 65 6e 20 3d 20          pWhen = 
27228 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
27229 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 70 57 68  rse, TK_NOT, pWh
2722a 65 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20  en, 0, 0);.     
2722b 20 20 20 70 54 72 69 67 67 65 72 2d 3e 70 57 68     pTrigger->pWh
2722c 65 6e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  en = sqlite3Expr
2722d 44 75 70 28 64 62 2c 20 70 57 68 65 6e 2c 20 45  Dup(db, pWhen, E
2722e 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a  XPRDUP_REDUCE);.
2722f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
27230 20 20 20 2f 2a 20 52 65 2d 65 6e 61 62 6c 65 20     /* Re-enable 
27231 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75  the lookaside bu
27232 66 66 65 72 2c 20 69 66 20 69 74 20 77 61 73 20  ffer, if it was 
27233 64 69 73 61 62 6c 65 64 20 65 61 72 6c 69 65 72  disabled earlier
27234 2e 20 2a 2f 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f  . */.    db->loo
27235 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20  kaside.bEnabled 
27236 3d 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64  = enableLookasid
27237 65 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 45  e;..    sqlite3E
27238 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 57  xprDelete(db, pW
27239 68 65 72 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  here);.    sqlit
2723a 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
2723b 20 70 57 68 65 6e 29 3b 0a 20 20 20 20 73 71 6c   pWhen);.    sql
2723c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
2723d 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20  te(db, pList);. 
2723e 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
2723f 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65  Delete(db, pSele
27240 63 74 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d  ct);.    if( db-
27241 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 31  >mallocFailed==1
27242 20 29 7b 0a 20 20 20 20 20 20 66 6b 54 72 69 67   ){.      fkTrig
27243 67 65 72 44 65 6c 65 74 65 28 64 62 2c 20 70 54  gerDelete(db, pT
27244 72 69 67 67 65 72 29 3b 0a 20 20 20 20 20 20 72  rigger);.      r
27245 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 0a  eturn 0;.    }..
27246 20 20 20 20 73 77 69 74 63 68 28 20 61 63 74 69      switch( acti
27247 6f 6e 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  on ){.      case
27248 20 4f 45 5f 52 65 73 74 72 69 63 74 3a 0a 20 20   OE_Restrict:.  
27249 20 20 20 20 20 20 70 53 74 65 70 2d 3e 6f 70 20        pStep->op 
2724a 3d 20 54 4b 5f 53 45 4c 45 43 54 3b 20 0a 20 20  = TK_SELECT; .  
2724b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2724c 20 20 20 63 61 73 65 20 4f 45 5f 43 61 73 63 61     case OE_Casca
2724d 64 65 3a 20 0a 20 20 20 20 20 20 20 20 69 66 28  de: .        if(
2724e 20 21 70 43 68 61 6e 67 65 73 20 29 7b 20 0a 20   !pChanges ){ . 
2724f 20 20 20 20 20 20 20 20 20 70 53 74 65 70 2d 3e           pStep->
27250 6f 70 20 3d 20 54 4b 5f 44 45 4c 45 54 45 3b 20  op = TK_DELETE; 
27251 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
27252 3b 20 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  ; .        }.   
27253 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20     default:.    
27254 20 20 20 20 70 53 74 65 70 2d 3e 6f 70 20 3d 20      pStep->op = 
27255 54 4b 5f 55 50 44 41 54 45 3b 0a 20 20 20 20 7d  TK_UPDATE;.    }
27256 0a 20 20 20 20 70 53 74 65 70 2d 3e 70 54 72 69  .    pStep->pTri
27257 67 20 3d 20 70 54 72 69 67 67 65 72 3b 0a 20 20  g = pTrigger;.  
27258 20 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68    pTrigger->pSch
27259 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68  ema = pTab->pSch
2725a 65 6d 61 3b 0a 20 20 20 20 70 54 72 69 67 67 65  ema;.    pTrigge
2725b 72 2d 3e 70 54 61 62 53 63 68 65 6d 61 20 3d 20  r->pTabSchema = 
2725c 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20  pTab->pSchema;. 
2725d 20 20 20 70 46 4b 65 79 2d 3e 61 70 54 72 69 67     pFKey->apTrig
2725e 67 65 72 5b 69 41 63 74 69 6f 6e 5d 20 3d 20 70  ger[iAction] = p
2725f 54 72 69 67 67 65 72 3b 0a 20 20 20 20 70 54 72  Trigger;.    pTr
27260 69 67 67 65 72 2d 3e 6f 70 20 3d 20 28 70 43 68  igger->op = (pCh
27261 61 6e 67 65 73 20 3f 20 54 4b 5f 55 50 44 41 54  anges ? TK_UPDAT
27262 45 20 3a 20 54 4b 5f 44 45 4c 45 54 45 29 3b 0a  E : TK_DELETE);.
27263 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 54    }..  return pT
27264 72 69 67 67 65 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rigger;.}../*.**
27265 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
27266 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 64 65  s called when de
27267 6c 65 74 69 6e 67 20 6f 72 20 75 70 64 61 74 69  leting or updati
27268 6e 67 20 61 20 72 6f 77 20 74 6f 20 69 6d 70 6c  ng a row to impl
27269 65 6d 65 6e 74 0a 2a 2a 20 61 6e 79 20 72 65 71  ement.** any req
2726a 75 69 72 65 64 20 43 41 53 43 41 44 45 2c 20 53  uired CASCADE, S
2726b 45 54 20 4e 55 4c 4c 20 6f 72 20 53 45 54 20 44  ET NULL or SET D
2726c 45 46 41 55 4c 54 20 61 63 74 69 6f 6e 73 2e 0a  EFAULT actions..
2726d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2726e 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 6b  E void sqlite3Fk
2726f 41 63 74 69 6f 6e 73 28 0a 20 20 50 61 72 73 65  Actions(.  Parse
27270 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
27271 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
27272 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  rse context */. 
27273 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
27274 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27275 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20   /* Table being 
27276 75 70 64 61 74 65 64 20 6f 72 20 64 65 6c 65 74  updated or delet
27277 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 45 78 70  ed from */.  Exp
27278 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73 2c  rList *pChanges,
27279 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2727a 43 68 61 6e 67 65 2d 6c 69 73 74 20 66 6f 72 20  Change-list for 
2727b 55 50 44 41 54 45 2c 20 4e 55 4c 4c 20 66 6f 72  UPDATE, NULL for
2727c 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20 69 6e 74   DELETE */.  int
2727d 20 72 65 67 4f 6c 64 20 20 20 20 20 20 20 20 20   regOld         
2727e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2727f 41 64 64 72 65 73 73 20 6f 66 20 61 72 72 61 79  Address of array
27280 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6f 6c 64 20   containing old 
27281 72 6f 77 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 49  row */.){.  /* I
27282 66 20 66 6f 72 65 69 67 6e 2d 6b 65 79 20 73 75  f foreign-key su
27283 70 70 6f 72 74 20 69 73 20 65 6e 61 62 6c 65 64  pport is enabled
27284 2c 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  , iterate throug
27285 68 20 61 6c 6c 20 46 4b 73 20 74 68 61 74 20 0a  h all FKs that .
27286 20 20 2a 2a 20 72 65 66 65 72 20 74 6f 20 74 61    ** refer to ta
27287 62 6c 65 20 70 54 61 62 2e 20 49 66 20 74 68 65  ble pTab. If the
27288 72 65 20 69 73 20 61 6e 20 61 63 74 69 6f 6e 20  re is an action 
27289 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
2728a 74 68 65 20 46 4b 20 0a 20 20 2a 2a 20 66 6f 72  the FK .  ** for
2728b 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20   this operation 
2728c 28 65 69 74 68 65 72 20 75 70 64 61 74 65 20 6f  (either update o
2728d 72 20 64 65 6c 65 74 65 29 2c 20 69 6e 76 6f 6b  r delete), invok
2728e 65 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64  e the associated
2728f 20 0a 20 20 2a 2a 20 74 72 69 67 67 65 72 20 73   .  ** trigger s
27290 75 62 2d 70 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a  ub-program.  */.
27291 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62    if( pParse->db
27292 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46  ->flags&SQLITE_F
27293 6f 72 65 69 67 6e 4b 65 79 73 20 29 7b 0a 20 20  oreignKeys ){.  
27294 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20 20    FKey *pFKey;  
27295 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27296 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
27297 61 62 6c 65 20 2a 2f 0a 20 20 20 20 66 6f 72 28  able */.    for(
27298 70 46 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 46  pFKey = sqlite3F
27299 6b 52 65 66 65 72 65 6e 63 65 73 28 70 54 61 62  kReferences(pTab
2729a 29 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65 79 3d  ); pFKey; pFKey=
2729b 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 29 7b  pFKey->pNextTo){
2729c 0a 20 20 20 20 20 20 54 72 69 67 67 65 72 20 2a  .      Trigger *
2729d 70 41 63 74 69 6f 6e 20 3d 20 66 6b 41 63 74 69  pAction = fkActi
2729e 6f 6e 54 72 69 67 67 65 72 28 70 50 61 72 73 65  onTrigger(pParse
2729f 2c 20 70 54 61 62 2c 20 70 46 4b 65 79 2c 20 70  , pTab, pFKey, p
272a0 43 68 61 6e 67 65 73 29 3b 0a 20 20 20 20 20 20  Changes);.      
272a1 69 66 28 20 70 41 63 74 69 6f 6e 20 29 7b 0a 20  if( pAction ){. 
272a2 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f         sqlite3Co
272a3 64 65 52 6f 77 54 72 69 67 67 65 72 44 69 72 65  deRowTriggerDire
272a4 63 74 28 70 50 61 72 73 65 2c 20 70 41 63 74 69  ct(pParse, pActi
272a5 6f 6e 2c 20 70 54 61 62 2c 20 72 65 67 4f 6c 64  on, pTab, regOld
272a6 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 30 29 3b 0a  , OE_Abort, 0);.
272a7 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
272a8 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 69  }.}..#endif /* i
272a9 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
272aa 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a 0a 2f 2a  T_TRIGGER */../*
272ab 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20 6d 65 6d  .** Free all mem
272ac 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77  ory associated w
272ad 69 74 68 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  ith foreign key 
272ae 64 65 66 69 6e 69 74 69 6f 6e 73 20 61 74 74 61  definitions atta
272af 63 68 65 64 20 74 6f 0a 2a 2a 20 74 61 62 6c 65  ched to.** table
272b0 20 70 54 61 62 2e 20 52 65 6d 6f 76 65 20 74 68   pTab. Remove th
272b1 65 20 64 65 6c 65 74 65 64 20 66 6f 72 65 69 67  e deleted foreig
272b2 6e 20 6b 65 79 73 20 66 72 6f 6d 20 74 68 65 20  n keys from the 
272b3 53 63 68 65 6d 61 2e 66 6b 65 79 48 61 73 68 0a  Schema.fkeyHash.
272b4 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a  ** hash table..*
272b5 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
272b6 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 6b 44   void sqlite3FkD
272b7 65 6c 65 74 65 28 54 61 62 6c 65 20 2a 70 54 61  elete(Table *pTa
272b8 62 29 7b 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65  b){.  FKey *pFKe
272b9 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
272ba 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
272bb 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20  r variable */.  
272bc 46 4b 65 79 20 2a 70 4e 65 78 74 3b 20 20 20 20  FKey *pNext;    
272bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
272be 2f 2a 20 43 6f 70 79 20 6f 66 20 70 46 4b 65 79  /* Copy of pFKey
272bf 2d 3e 70 4e 65 78 74 46 72 6f 6d 20 2a 2f 0a 0a  ->pNextFrom */..
272c0 20 20 66 6f 72 28 70 46 4b 65 79 3d 70 54 61 62    for(pFKey=pTab
272c1 2d 3e 70 46 4b 65 79 3b 20 70 46 4b 65 79 3b 20  ->pFKey; pFKey; 
272c2 70 46 4b 65 79 3d 70 4e 65 78 74 29 7b 0a 0a 20  pFKey=pNext){.. 
272c3 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65     /* Remove the
272c4 20 46 4b 20 66 72 6f 6d 20 74 68 65 20 66 6b 65   FK from the fke
272c5 79 48 61 73 68 20 68 61 73 68 20 74 61 62 6c 65  yHash hash table
272c6 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 46 4b  . */.    if( pFK
272c7 65 79 2d 3e 70 50 72 65 76 54 6f 20 29 7b 0a 20  ey->pPrevTo ){. 
272c8 20 20 20 20 20 70 46 4b 65 79 2d 3e 70 50 72 65       pFKey->pPre
272c9 76 54 6f 2d 3e 70 4e 65 78 74 54 6f 20 3d 20 70  vTo->pNextTo = p
272ca 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 3b 0a 20  FKey->pNextTo;. 
272cb 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
272cc 76 6f 69 64 20 2a 64 61 74 61 20 3d 20 28 76 6f  void *data = (vo
272cd 69 64 20 2a 29 70 46 4b 65 79 2d 3e 70 4e 65 78  id *)pFKey->pNex
272ce 74 54 6f 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74  tTo;.      const
272cf 20 63 68 61 72 20 2a 7a 20 3d 20 28 64 61 74 61   char *z = (data
272d0 20 3f 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54   ? pFKey->pNextT
272d1 6f 2d 3e 7a 54 6f 20 3a 20 70 46 4b 65 79 2d 3e  o->zTo : pFKey->
272d2 7a 54 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  zTo);.      sqli
272d3 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70  te3HashInsert(&p
272d4 54 61 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 6b  Tab->pSchema->fk
272d5 65 79 48 61 73 68 2c 20 7a 2c 20 73 71 6c 69 74  eyHash, z, sqlit
272d6 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 2c 20 64  e3Strlen30(z), d
272d7 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ata);.    }.    
272d8 69 66 28 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74  if( pFKey->pNext
272d9 54 6f 20 29 7b 0a 20 20 20 20 20 20 70 46 4b 65  To ){.      pFKe
272da 79 2d 3e 70 4e 65 78 74 54 6f 2d 3e 70 50 72 65  y->pNextTo->pPre
272db 76 54 6f 20 3d 20 70 46 4b 65 79 2d 3e 70 50 72  vTo = pFKey->pPr
272dc 65 76 54 6f 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  evTo;.    }..   
272dd 20 2f 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 74   /* Delete any t
272de 72 69 67 67 65 72 73 20 63 72 65 61 74 65 64 20  riggers created 
272df 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 63 74  to implement act
272e0 69 6f 6e 73 20 66 6f 72 20 74 68 69 73 20 46 4b  ions for this FK
272e1 2e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  . */.#ifndef SQL
272e2 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
272e3 0a 20 20 20 20 66 6b 54 72 69 67 67 65 72 44 65  .    fkTriggerDe
272e4 6c 65 74 65 28 70 54 61 62 2d 3e 64 62 4d 65 6d  lete(pTab->dbMem
272e5 2c 20 70 46 4b 65 79 2d 3e 61 70 54 72 69 67 67  , pFKey->apTrigg
272e6 65 72 5b 30 5d 29 3b 0a 20 20 20 20 66 6b 54 72  er[0]);.    fkTr
272e7 69 67 67 65 72 44 65 6c 65 74 65 28 70 54 61 62  iggerDelete(pTab
272e8 2d 3e 64 62 4d 65 6d 2c 20 70 46 4b 65 79 2d 3e  ->dbMem, pFKey->
272e9 61 70 54 72 69 67 67 65 72 5b 31 5d 29 3b 0a 23  apTrigger[1]);.#
272ea 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 45 56  endif..    /* EV
272eb 3a 20 52 2d 33 30 33 32 33 2d 32 31 39 31 37 20  : R-30323-21917 
272ec 45 61 63 68 20 66 6f 72 65 69 67 6e 20 6b 65 79  Each foreign key
272ed 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 6e 20 53   constraint in S
272ee 51 4c 69 74 65 20 69 73 0a 20 20 20 20 2a 2a 20  QLite is.    ** 
272ef 63 6c 61 73 73 69 66 69 65 64 20 61 73 20 65 69  classified as ei
272f0 74 68 65 72 20 69 6d 6d 65 64 69 61 74 65 20 6f  ther immediate o
272f1 72 20 64 65 66 65 72 72 65 64 2e 0a 20 20 20 20  r deferred..    
272f2 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  */.    assert( p
272f3 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64  FKey->isDeferred
272f4 3d 3d 30 20 7c 7c 20 70 46 4b 65 79 2d 3e 69 73  ==0 || pFKey->is
272f5 44 65 66 65 72 72 65 64 3d 3d 31 20 29 3b 0a 0a  Deferred==1 );..
272f6 20 20 20 20 70 4e 65 78 74 20 3d 20 70 46 4b 65      pNext = pFKe
272f7 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 3b 0a 20 20  y->pNextFrom;.  
272f8 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
272f9 70 54 61 62 2d 3e 64 62 4d 65 6d 2c 20 70 46 4b  pTab->dbMem, pFK
272fa 65 79 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  ey);.  }.}.#endi
272fb 66 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49  f /* ifndef SQLI
272fc 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  TE_OMIT_FOREIGN_
272fd 4b 45 59 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  KEY */../*******
272fe 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66  ******* End of f
272ff 6b 65 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  key.c **********
27300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27301 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27302 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
27303 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
27304 6c 65 20 69 6e 73 65 72 74 2e 63 20 2a 2a 2a 2a  le insert.c ****
27305 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27306 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27307 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
27308 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
27309 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
2730a 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
2730b 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
2730c 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
2730d 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
2730e 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
2730f 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
27310 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
27311 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
27312 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
27313 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
27314 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
27315 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
27316 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
27317 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
27318 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
27319 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
2731a 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
2731b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2731c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2731d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2731e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
2731f 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
27320 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74  ains C code rout
27321 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 63 61  ines that are ca
27322 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
27323 65 72 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20  er.** to handle 
27324 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74  INSERT statement
27325 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  s in SQLite..**.
27326 2a 2a 20 24 49 64 3a 20 69 6e 73 65 72 74 2e 63  ** $Id: insert.c
27327 2c 76 20 31 2e 32 37 30 20 32 30 30 39 2f 30 37  ,v 1.270 2009/07
27328 2f 32 34 20 31 37 3a 35 38 3a 35 33 20 64 61 6e  /24 17:58:53 dan
27329 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a  ielk1977 Exp $.*
2732a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  /../*.** Generat
2732b 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c  e code that will
2732c 20 6f 70 65 6e 20 61 20 74 61 62 6c 65 20 66 6f   open a table fo
2732d 72 20 72 65 61 64 69 6e 67 2e 0a 2a 2f 0a 53 51  r reading..*/.SQ
2732e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2732f 64 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62  d sqlite3OpenTab
27330 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 2c 20  le(.  Parse *p, 
27331 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74        /* Generat
27332 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69 73  e code into this
27333 20 56 44 42 45 20 2a 2f 0a 20 20 69 6e 74 20 69   VDBE */.  int i
27334 43 75 72 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  Cur,       /* Th
27335 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  e cursor number 
27336 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  of the table */.
27337 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20    int iDb,      
27338 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
27339 65 20 69 6e 64 65 78 20 69 6e 20 73 71 6c 69 74  e index in sqlit
2733a 65 33 2e 61 44 62 5b 5d 20 2a 2f 0a 20 20 54 61  e3.aDb[] */.  Ta
2733b 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 2f 2a  ble *pTab,    /*
2733c 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65   The table to be
2733d 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74   opened */.  int
2733e 20 6f 70 63 6f 64 65 20 20 20 20 20 20 2f 2a 20   opcode      /* 
2733f 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f 72 20 4f  OP_OpenRead or O
27340 50 5f 4f 70 65 6e 57 72 69 74 65 20 2a 2f 0a 29  P_OpenWrite */.)
27341 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69  {.  Vdbe *v;.  i
27342 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
27343 62 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 76  b) ) return;.  v
27344 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
27345 65 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20  e(p);.  assert( 
27346 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57  opcode==OP_OpenW
27347 72 69 74 65 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d  rite || opcode==
27348 4f 50 5f 4f 70 65 6e 52 65 61 64 20 29 3b 0a 20  OP_OpenRead );. 
27349 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63   sqlite3TableLoc
2734a 6b 28 70 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e  k(p, iDb, pTab->
2734b 74 6e 75 6d 2c 20 28 6f 70 63 6f 64 65 3d 3d 4f  tnum, (opcode==O
2734c 50 5f 4f 70 65 6e 57 72 69 74 65 29 3f 31 3a 30  P_OpenWrite)?1:0
2734d 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
2734e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2734f 4f 70 33 28 76 2c 20 6f 70 63 6f 64 65 2c 20 69  Op3(v, opcode, i
27350 43 75 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c  Cur, pTab->tnum,
27351 20 69 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 33   iDb);.  sqlite3
27352 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
27353 2d 31 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54  -1, SQLITE_INT_T
27354 4f 5f 50 54 52 28 70 54 61 62 2d 3e 6e 43 6f 6c  O_PTR(pTab->nCol
27355 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20  ), P4_INT32);.  
27356 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
27357 22 25 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  "%s", pTab->zNam
27358 65 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  e));.}../*.** Re
27359 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
2735a 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66  o the column aff
2735b 69 6e 69 74 79 20 73 74 72 69 6e 67 20 61 73 73  inity string ass
2735c 6f 63 69 61 74 65 64 20 77 69 74 68 20 69 6e 64  ociated with ind
2735d 65 78 0a 2a 2a 20 70 49 64 78 2e 20 41 20 63 6f  ex.** pIdx. A co
2735e 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74  lumn affinity st
2735f 72 69 6e 67 20 68 61 73 20 6f 6e 65 20 63 68 61  ring has one cha
27360 72 61 63 74 65 72 20 66 6f 72 20 65 61 63 68 20  racter for each 
27361 63 6f 6c 75 6d 6e 20 69 6e 20 0a 2a 2a 20 74 68  column in .** th
27362 65 20 74 61 62 6c 65 2c 20 61 63 63 6f 72 64 69  e table, accordi
27363 6e 67 20 74 6f 20 74 68 65 20 61 66 66 69 6e 69  ng to the affini
27364 74 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  ty of the column
27365 3a 0a 2a 2a 0a 2a 2a 20 20 43 68 61 72 61 63 74  :.**.**  Charact
27366 65 72 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 61  er      Column a
27367 66 66 69 6e 69 74 79 0a 2a 2a 20 20 2d 2d 2d 2d  ffinity.**  ----
27368 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
27369 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 27  ----------.**  '
2736a 61 27 20 20 20 20 20 20 20 20 20 20 20 20 54 45  a'            TE
2736b 58 54 0a 2a 2a 20 20 27 62 27 20 20 20 20 20 20  XT.**  'b'      
2736c 20 20 20 20 20 20 4e 4f 4e 45 0a 2a 2a 20 20 27        NONE.**  '
2736d 63 27 20 20 20 20 20 20 20 20 20 20 20 20 4e 55  c'            NU
2736e 4d 45 52 49 43 0a 2a 2a 20 20 27 64 27 20 20 20  MERIC.**  'd'   
2736f 20 20 20 20 20 20 20 20 20 49 4e 54 45 47 45 52           INTEGER
27370 0a 2a 2a 20 20 27 65 27 20 20 20 20 20 20 20 20  .**  'e'        
27371 20 20 20 20 52 45 41 4c 0a 2a 2a 0a 2a 2a 20 41      REAL.**.** A
27372 6e 20 65 78 74 72 61 20 27 62 27 20 69 73 20 61  n extra 'b' is a
27373 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 65  ppended to the e
27374 6e 64 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67  nd of the string
27375 20 74 6f 20 63 6f 76 65 72 20 74 68 65 0a 2a 2a   to cover the.**
27376 20 72 6f 77 69 64 20 74 68 61 74 20 61 70 70 65   rowid that appe
27377 61 72 73 20 61 73 20 74 68 65 20 6c 61 73 74 20  ars as the last 
27378 63 6f 6c 75 6d 6e 20 69 6e 20 65 76 65 72 79 20  column in every 
27379 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 4d 65 6d  index..**.** Mem
2737a 6f 72 79 20 66 6f 72 20 74 68 65 20 62 75 66 66  ory for the buff
2737b 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  er containing th
2737c 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 61  e column index a
2737d 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 0a 2a  ffinity string.*
2737e 2a 20 69 73 20 6d 61 6e 61 67 65 64 20 61 6c 6f  * is managed alo
2737f 6e 67 20 77 69 74 68 20 74 68 65 20 72 65 73 74  ng with the rest
27380 20 6f 66 20 74 68 65 20 49 6e 64 65 78 20 73 74   of the Index st
27381 72 75 63 74 75 72 65 2e 20 49 74 20 77 69 6c 6c  ructure. It will
27382 20 62 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20   be.** released 
27383 77 68 65 6e 20 73 71 6c 69 74 65 33 44 65 6c 65  when sqlite3Dele
27384 74 65 49 6e 64 65 78 28 29 20 69 73 20 63 61 6c  teIndex() is cal
27385 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  led..*/.SQLITE_P
27386 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
27387 72 20 2a 73 71 6c 69 74 65 33 49 6e 64 65 78 41  r *sqlite3IndexA
27388 66 66 69 6e 69 74 79 53 74 72 28 56 64 62 65 20  ffinityStr(Vdbe 
27389 2a 76 2c 20 49 6e 64 65 78 20 2a 70 49 64 78 29  *v, Index *pIdx)
2738a 7b 0a 20 20 69 66 28 20 21 70 49 64 78 2d 3e 7a  {.  if( !pIdx->z
2738b 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20 2f 2a  ColAff ){.    /*
2738c 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   The first time 
2738d 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74  a column affinit
2738e 79 20 73 74 72 69 6e 67 20 66 6f 72 20 61 20 70  y string for a p
2738f 61 72 74 69 63 75 6c 61 72 20 69 6e 64 65 78 20  articular index 
27390 69 73 0a 20 20 20 20 2a 2a 20 72 65 71 75 69 72  is.    ** requir
27391 65 64 2c 20 69 74 20 69 73 20 61 6c 6c 6f 63 61  ed, it is alloca
27392 74 65 64 20 61 6e 64 20 70 6f 70 75 6c 61 74 65  ted and populate
27393 64 20 68 65 72 65 2e 20 49 74 20 69 73 20 74 68  d here. It is th
27394 65 6e 20 73 74 6f 72 65 64 20 61 73 0a 20 20 20  en stored as.   
27395 20 2a 2a 20 61 20 6d 65 6d 62 65 72 20 6f 66 20   ** a member of 
27396 74 68 65 20 49 6e 64 65 78 20 73 74 72 75 63 74  the Index struct
27397 75 72 65 20 66 6f 72 20 73 75 62 73 65 71 75 65  ure for subseque
27398 6e 74 20 75 73 65 2e 0a 20 20 20 20 2a 2a 0a 20  nt use..    **. 
27399 20 20 20 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e     ** The column
2739a 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67   affinity string
2739b 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79   will eventually
2739c 20 62 65 20 64 65 6c 65 74 65 64 20 62 79 0a 20   be deleted by. 
2739d 20 20 20 2a 2a 20 73 71 6c 69 74 65 44 65 6c 65     ** sqliteDele
2739e 74 65 49 6e 64 65 78 28 29 20 77 68 65 6e 20 74  teIndex() when t
2739f 68 65 20 49 6e 64 65 78 20 73 74 72 75 63 74 75  he Index structu
273a0 72 65 20 69 74 73 65 6c 66 20 69 73 20 63 6c 65  re itself is cle
273a1 61 6e 65 64 0a 20 20 20 20 2a 2a 20 75 70 2e 0a  aned.    ** up..
273a2 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e      */.    int n
273a3 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61  ;.    Table *pTa
273a4 62 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65  b = pIdx->pTable
273a5 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64  ;.    sqlite3 *d
273a6 62 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 44  b = sqlite3VdbeD
273a7 62 28 76 29 3b 0a 20 20 20 20 70 49 64 78 2d 3e  b(v);.    pIdx->
273a8 7a 43 6f 6c 41 66 66 20 3d 20 28 63 68 61 72 20  zColAff = (char 
273a9 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  *)sqlite3Malloc(
273aa 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 29  pIdx->nColumn+2)
273ab 3b 0a 20 20 20 20 69 66 28 20 21 70 49 64 78 2d  ;.    if( !pIdx-
273ac 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20  >zColAff ){.    
273ad 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
273ae 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65  ed = 1;.      re
273af 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
273b0 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 70 49 64    for(n=0; n<pId
273b1 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6e 2b 2b 29  x->nColumn; n++)
273b2 7b 0a 20 20 20 20 20 20 70 49 64 78 2d 3e 7a 43  {.      pIdx->zC
273b3 6f 6c 41 66 66 5b 6e 5d 20 3d 20 70 54 61 62 2d  olAff[n] = pTab-
273b4 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69 43 6f  >aCol[pIdx->aiCo
273b5 6c 75 6d 6e 5b 6e 5d 5d 2e 61 66 66 69 6e 69 74  lumn[n]].affinit
273b6 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 64  y;.    }.    pId
273b7 78 2d 3e 7a 43 6f 6c 41 66 66 5b 6e 2b 2b 5d 20  x->zColAff[n++] 
273b8 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  = SQLITE_AFF_NON
273b9 45 3b 0a 20 20 20 20 70 49 64 78 2d 3e 7a 43 6f  E;.    pIdx->zCo
273ba 6c 41 66 66 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d  lAff[n] = 0;.  }
273bb 0a 20 0a 20 20 72 65 74 75 72 6e 20 70 49 64 78  . .  return pIdx
273bc 2d 3e 7a 43 6f 6c 41 66 66 3b 0a 7d 0a 0a 2f 2a  ->zColAff;.}../*
273bd 0a 2a 2a 20 53 65 74 20 50 34 20 6f 66 20 74 68  .** Set P4 of th
273be 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
273bf 69 6e 73 65 72 74 65 64 20 6f 70 63 6f 64 65 20  inserted opcode 
273c0 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69  to a column affi
273c1 6e 69 74 79 0a 2a 2a 20 73 74 72 69 6e 67 20 66  nity.** string f
273c2 6f 72 20 74 61 62 6c 65 20 70 54 61 62 2e 20 41  or table pTab. A
273c3 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79   column affinity
273c4 20 73 74 72 69 6e 67 20 68 61 73 20 6f 6e 65 20   string has one 
273c5 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 66 6f 72  character.** for
273c6 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 69 6e 64   each column ind
273c7 65 78 65 64 20 62 79 20 74 68 65 20 69 6e 64 65  exed by the inde
273c8 78 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20  x, according to 
273c9 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20  the affinity of 
273ca 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 3a 0a 2a  the.** column:.*
273cb 2a 0a 2a 2a 20 20 43 68 61 72 61 63 74 65 72 20  *.**  Character 
273cc 20 20 20 20 20 43 6f 6c 75 6d 6e 20 61 66 66 69       Column affi
273cd 6e 69 74 79 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d  nity.**  -------
273ce 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
273cf 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 27 61 27 20  -------.**  'a' 
273d0 20 20 20 20 20 20 20 20 20 20 20 54 45 58 54 0a             TEXT.
273d1 2a 2a 20 20 27 62 27 20 20 20 20 20 20 20 20 20  **  'b'         
273d2 20 20 20 4e 4f 4e 45 0a 2a 2a 20 20 27 63 27 20     NONE.**  'c' 
273d3 20 20 20 20 20 20 20 20 20 20 20 4e 55 4d 45 52             NUMER
273d4 49 43 0a 2a 2a 20 20 27 64 27 20 20 20 20 20 20  IC.**  'd'      
273d5 20 20 20 20 20 20 49 4e 54 45 47 45 52 0a 2a 2a        INTEGER.**
273d6 20 20 27 65 27 20 20 20 20 20 20 20 20 20 20 20    'e'           
273d7 20 52 45 41 4c 0a 2a 2f 0a 53 51 4c 49 54 45 5f   REAL.*/.SQLITE_
273d8 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
273d9 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74  ite3TableAffinit
273da 79 53 74 72 28 56 64 62 65 20 2a 76 2c 20 54 61  yStr(Vdbe *v, Ta
273db 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 2f 2a  ble *pTab){.  /*
273dc 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   The first time 
273dd 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74  a column affinit
273de 79 20 73 74 72 69 6e 67 20 66 6f 72 20 61 20 70  y string for a p
273df 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 0a  articular table.
273e0 20 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64    ** is required
273e1 2c 20 69 74 20 69 73 20 61 6c 6c 6f 63 61 74 65  , it is allocate
273e2 64 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 64 20  d and populated 
273e3 68 65 72 65 2e 20 49 74 20 69 73 20 74 68 65 6e  here. It is then
273e4 20 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 61 73   .  ** stored as
273e5 20 61 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65   a member of the
273e6 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65   Table structure
273e7 20 66 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20   for subsequent 
273e8 75 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  use..  **.  ** T
273e9 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69  he column affini
273ea 74 79 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 65  ty string will e
273eb 76 65 6e 74 75 61 6c 6c 79 20 62 65 20 64 65 6c  ventually be del
273ec 65 74 65 64 20 62 79 0a 20 20 2a 2a 20 73 71 6c  eted by.  ** sql
273ed 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
273ee 29 20 77 68 65 6e 20 74 68 65 20 54 61 62 6c 65  ) when the Table
273ef 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c   structure itsel
273f0 66 20 69 73 20 63 6c 65 61 6e 65 64 20 75 70 2e  f is cleaned up.
273f1 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 54 61  .  */.  if( !pTa
273f2 62 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20  b->zColAff ){.  
273f3 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b    char *zColAff;
273f4 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
273f5 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
273f6 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 3b 0a  lite3VdbeDb(v);.
273f7 0a 20 20 20 20 7a 43 6f 6c 41 66 66 20 3d 20 28  .    zColAff = (
273f8 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 4d 61  char *)sqlite3Ma
273f9 6c 6c 6f 63 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b  lloc(pTab->nCol+
273fa 31 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 43 6f  1);.    if( !zCo
273fb 6c 41 66 66 20 29 7b 0a 20 20 20 20 20 20 64 62  lAff ){.      db
273fc 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
273fd 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   1;.      return
273fe 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 66 6f 72  ;.    }..    for
273ff 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43  (i=0; i<pTab->nC
27400 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
27401 7a 43 6f 6c 41 66 66 5b 69 5d 20 3d 20 70 54 61  zColAff[i] = pTa
27402 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e  b->aCol[i].affin
27403 69 74 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a  ity;.    }.    z
27404 43 6f 6c 41 66 66 5b 70 54 61 62 2d 3e 6e 43 6f  ColAff[pTab->nCo
27405 6c 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 20 20  l] = '\0';..    
27406 70 54 61 62 2d 3e 7a 43 6f 6c 41 66 66 20 3d 20  pTab->zColAff = 
27407 7a 43 6f 6c 41 66 66 3b 0a 20 20 7d 0a 0a 20 20  zColAff;.  }..  
27408 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
27409 65 50 34 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d  eP4(v, -1, pTab-
2740a 3e 7a 43 6f 6c 41 66 66 2c 20 30 29 3b 0a 7d 0a  >zColAff, 0);.}.
2740b 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f  ./*.** Return no
2740c 6e 2d 7a 65 72 6f 20 69 66 20 74 68 65 20 74 61  n-zero if the ta
2740d 62 6c 65 20 70 54 61 62 20 69 6e 20 64 61 74 61  ble pTab in data
2740e 62 61 73 65 20 69 44 62 20 6f 72 20 61 6e 79 20  base iDb or any 
2740f 6f 66 20 69 74 73 20 69 6e 64 69 63 65 73 0a 2a  of its indices.*
27410 2a 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e  * have been open
27411 65 64 20 61 74 20 61 6e 79 20 70 6f 69 6e 74 20  ed at any point 
27412 69 6e 20 74 68 65 20 56 44 42 45 20 70 72 6f 67  in the VDBE prog
27413 72 61 6d 20 62 65 67 69 6e 6e 69 6e 67 20 61 74  ram beginning at
27414 20 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 53 74   location.** iSt
27415 61 72 74 41 64 64 72 20 74 68 72 6f 75 67 68 74  artAddr throught
27416 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
27417 70 72 6f 67 72 61 6d 2e 20 20 54 68 69 73 20 69  program.  This i
27418 73 20 75 73 65 64 20 74 6f 20 73 65 65 20 69 66  s used to see if
27419 20 0a 2a 2a 20 61 20 73 74 61 74 65 6d 65 6e 74   .** a statement
2741a 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 20 22 49   of the form  "I
2741b 4e 53 45 52 54 20 49 4e 54 4f 20 3c 69 44 62 2c  NSERT INTO <iDb,
2741c 20 70 54 61 62 3e 20 53 45 4c 45 43 54 20 2e 2e   pTab> SELECT ..
2741d 2e 22 20 63 61 6e 20 0a 2a 2a 20 72 75 6e 20 77  ." can .** run w
2741e 69 74 68 6f 75 74 20 75 73 69 6e 67 20 74 65 6d  ithout using tem
2741f 70 6f 72 61 72 79 20 74 61 62 6c 65 20 66 6f 72  porary table for
27420 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20   the results of 
27421 74 68 65 20 53 45 4c 45 43 54 2e 20 0a 2a 2f 0a  the SELECT. .*/.
27422 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 73  static int reads
27423 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 2c 20  Table(Parse *p, 
27424 69 6e 74 20 69 53 74 61 72 74 41 64 64 72 2c 20  int iStartAddr, 
27425 69 6e 74 20 69 44 62 2c 20 54 61 62 6c 65 20 2a  int iDb, Table *
27426 70 54 61 62 29 7b 0a 20 20 56 64 62 65 20 2a 76  pTab){.  Vdbe *v
27427 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
27428 65 28 70 29 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  e(p);.  int i;. 
27429 20 69 6e 74 20 69 45 6e 64 20 3d 20 73 71 6c 69   int iEnd = sqli
2742a 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
2742b 64 72 28 76 29 3b 0a 23 69 66 6e 64 65 66 20 53  dr(v);.#ifndef S
2742c 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
2742d 41 4c 54 41 42 4c 45 0a 20 20 56 54 61 62 6c 65  ALTABLE.  VTable
2742e 20 2a 70 56 54 61 62 20 3d 20 49 73 56 69 72 74   *pVTab = IsVirt
2742f 75 61 6c 28 70 54 61 62 29 20 3f 20 73 71 6c 69  ual(pTab) ? sqli
27430 74 65 33 47 65 74 56 54 61 62 6c 65 28 70 2d 3e  te3GetVTable(p->
27431 64 62 2c 20 70 54 61 62 29 20 3a 20 30 3b 0a 23  db, pTab) : 0;.#
27432 65 6e 64 69 66 0a 0a 20 20 66 6f 72 28 69 3d 69  endif..  for(i=i
27433 53 74 61 72 74 41 64 64 72 3b 20 69 3c 69 45 6e  StartAddr; i<iEn
27434 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 56 64 62  d; i++){.    Vdb
27435 65 4f 70 20 2a 70 4f 70 20 3d 20 73 71 6c 69 74  eOp *pOp = sqlit
27436 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 69  e3VdbeGetOp(v, i
27437 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
27438 4f 70 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  Op!=0 );.    if(
27439 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
2743a 5f 4f 70 65 6e 52 65 61 64 20 26 26 20 70 4f 70  _OpenRead && pOp
2743b 2d 3e 70 33 3d 3d 69 44 62 20 29 7b 0a 20 20 20  ->p3==iDb ){.   
2743c 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78     Index *pIndex
2743d 3b 0a 20 20 20 20 20 20 69 6e 74 20 74 6e 75 6d  ;.      int tnum
2743e 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 20 20   = pOp->p2;.    
2743f 20 20 69 66 28 20 74 6e 75 6d 3d 3d 70 54 61 62    if( tnum==pTab
27440 2d 3e 74 6e 75 6d 20 29 7b 0a 20 20 20 20 20 20  ->tnum ){.      
27441 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
27442 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 70 49    }.      for(pI
27443 6e 64 65 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  ndex=pTab->pInde
27444 78 3b 20 70 49 6e 64 65 78 3b 20 70 49 6e 64 65  x; pIndex; pInde
27445 78 3d 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 29  x=pIndex->pNext)
27446 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 74 6e  {.        if( tn
27447 75 6d 3d 3d 70 49 6e 64 65 78 2d 3e 74 6e 75 6d  um==pIndex->tnum
27448 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
27449 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20  turn 1;.        
2744a 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
2744b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2744c 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2744d 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70  .    if( pOp->op
2744e 63 6f 64 65 3d 3d 4f 50 5f 56 4f 70 65 6e 20 26  code==OP_VOpen &
2744f 26 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3d  & pOp->p4.pVtab=
27450 3d 70 56 54 61 62 20 29 7b 0a 20 20 20 20 20 20  =pVTab ){.      
27451 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e  assert( pOp->p4.
27452 70 56 74 61 62 21 3d 30 20 29 3b 0a 20 20 20 20  pVtab!=0 );.    
27453 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
27454 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42 20 29  4type==P4_VTAB )
27455 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ;.      return 1
27456 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
27457 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
27458 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
27459 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d  _OMIT_AUTOINCREM
2745a 45 4e 54 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65  ENT./*.** Locate
2745b 20 6f 72 20 63 72 65 61 74 65 20 61 6e 20 41 75   or create an Au
2745c 74 6f 69 6e 63 49 6e 66 6f 20 73 74 72 75 63 74  toincInfo struct
2745d 75 72 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  ure associated w
2745e 69 74 68 20 74 61 62 6c 65 20 70 54 61 62 0a 2a  ith table pTab.*
2745f 2a 20 77 68 69 63 68 20 69 73 20 69 6e 20 64 61  * which is in da
27460 74 61 62 61 73 65 20 69 44 62 2e 20 20 52 65 74  tabase iDb.  Ret
27461 75 72 6e 20 74 68 65 20 72 65 67 69 73 74 65 72  urn the register
27462 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
27463 72 65 67 69 73 74 65 72 0a 2a 2a 20 74 68 61 74  register.** that
27464 20 68 6f 6c 64 73 20 74 68 65 20 6d 61 78 69 6d   holds the maxim
27465 75 6d 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20  um rowid..**.** 
27466 54 68 65 72 65 20 69 73 20 61 74 20 6d 6f 73 74  There is at most
27467 20 6f 6e 65 20 41 75 74 6f 69 6e 63 49 6e 66 6f   one AutoincInfo
27468 20 73 74 72 75 63 74 75 72 65 20 70 65 72 20 74   structure per t
27469 61 62 6c 65 20 65 76 65 6e 20 69 66 20 74 68 65  able even if the
2746a 0a 2a 2a 20 73 61 6d 65 20 74 61 62 6c 65 20 69  .** same table i
2746b 73 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 65  s autoincremente
2746c 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73  d multiple times
2746d 20 64 75 65 20 74 6f 20 69 6e 73 65 72 74 73 20   due to inserts 
2746e 77 69 74 68 69 6e 0a 2a 2a 20 74 72 69 67 67 65  within.** trigge
2746f 72 73 2e 20 20 41 20 6e 65 77 20 41 75 74 6f 69  rs.  A new Autoi
27470 6e 63 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  ncInfo structure
27471 20 69 73 20 63 72 65 61 74 65 64 20 69 66 20 74   is created if t
27472 68 69 73 20 69 73 20 74 68 65 0a 2a 2a 20 66 69  his is the.** fi
27473 72 73 74 20 75 73 65 20 6f 66 20 74 61 62 6c 65  rst use of table
27474 20 70 54 61 62 2e 20 20 4f 6e 20 32 6e 64 20 61   pTab.  On 2nd a
27475 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 75 73  nd subsequent us
27476 65 73 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  es, the original
27477 0a 2a 2a 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20  .** AutoincInfo 
27478 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65  structure is use
27479 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 65 65 20 6d  d..**.** Three m
2747a 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20  emory locations 
2747b 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 3a 0a 2a  are allocated:.*
2747c 2a 0a 2a 2a 20 20 20 28 31 29 20 20 52 65 67 69  *.**   (1)  Regi
2747d 73 74 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65  ster to hold the
2747e 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 70 54 61   name of the pTa
2747f 62 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 28 32  b table..**   (2
27480 29 20 20 52 65 67 69 73 74 65 72 20 74 6f 20 68  )  Register to h
27481 6f 6c 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  old the maximum 
27482 52 4f 57 49 44 20 6f 66 20 70 54 61 62 2e 0a 2a  ROWID of pTab..*
27483 2a 20 20 20 28 33 29 20 20 52 65 67 69 73 74 65  *   (3)  Registe
27484 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 6f  r to hold the ro
27485 77 69 64 20 69 6e 20 73 71 6c 69 74 65 5f 73 65  wid in sqlite_se
27486 71 75 65 6e 63 65 20 6f 66 20 70 54 61 62 0a 2a  quence of pTab.*
27487 2a 0a 2a 2a 20 54 68 65 20 32 6e 64 20 72 65 67  *.** The 2nd reg
27488 69 73 74 65 72 20 69 73 20 74 68 65 20 6f 6e 65  ister is the one
27489 20 74 68 61 74 20 69 73 20 72 65 74 75 72 6e 65   that is returne
2748a 64 2e 20 20 54 68 61 74 20 69 73 20 61 6c 6c 20  d.  That is all 
2748b 74 68 65 0a 2a 2a 20 69 6e 73 65 72 74 20 72 6f  the.** insert ro
2748c 75 74 69 6e 65 20 6e 65 65 64 73 20 74 6f 20 6b  utine needs to k
2748d 6e 6f 77 20 61 62 6f 75 74 2e 0a 2a 2f 0a 73 74  now about..*/.st
2748e 61 74 69 63 20 69 6e 74 20 61 75 74 6f 49 6e 63  atic int autoInc
2748f 42 65 67 69 6e 28 0a 20 20 50 61 72 73 65 20 2a  Begin(.  Parse *
27490 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20  pParse,      /* 
27491 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
27492 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20  */.  int iDb,   
27493 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
27494 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  x of the databas
27495 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a  e holding pTab *
27496 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20  /.  Table *pTab 
27497 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
27498 61 62 6c 65 20 77 65 20 61 72 65 20 77 72 69 74  able we are writ
27499 69 6e 67 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 69  ing to */.){.  i
2749a 6e 74 20 6d 65 6d 49 64 20 3d 20 30 3b 20 20 20  nt memId = 0;   
2749b 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68     /* Register h
2749c 6f 6c 64 69 6e 67 20 6d 61 78 69 6d 75 6d 20 72  olding maximum r
2749d 6f 77 69 64 20 2a 2f 0a 20 20 69 66 28 20 70 54  owid */.  if( pT
2749e 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  ab->tabFlags & T
2749f 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20  F_Autoincrement 
274a0 29 7b 0a 20 20 20 20 50 61 72 73 65 20 2a 70 54  ){.    Parse *pT
274a1 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65  oplevel = sqlite
274a2 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70  3ParseToplevel(p
274a3 50 61 72 73 65 29 3b 0a 20 20 20 20 41 75 74 6f  Parse);.    Auto
274a4 69 6e 63 49 6e 66 6f 20 2a 70 49 6e 66 6f 3b 0a  incInfo *pInfo;.
274a5 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20 70 54 6f  .    pInfo = pTo
274a6 70 6c 65 76 65 6c 2d 3e 70 41 69 6e 63 3b 0a 20  plevel->pAinc;. 
274a7 20 20 20 77 68 69 6c 65 28 20 70 49 6e 66 6f 20     while( pInfo 
274a8 26 26 20 70 49 6e 66 6f 2d 3e 70 54 61 62 21 3d  && pInfo->pTab!=
274a9 70 54 61 62 20 29 7b 20 70 49 6e 66 6f 20 3d 20  pTab ){ pInfo = 
274aa 70 49 6e 66 6f 2d 3e 70 4e 65 78 74 3b 20 7d 0a  pInfo->pNext; }.
274ab 20 20 20 20 69 66 28 20 70 49 6e 66 6f 3d 3d 30      if( pInfo==0
274ac 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f 20   ){.      pInfo 
274ad 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
274ae 63 52 61 77 28 70 50 61 72 73 65 2d 3e 64 62 2c  cRaw(pParse->db,
274af 20 73 69 7a 65 6f 66 28 2a 70 49 6e 66 6f 29 29   sizeof(*pInfo))
274b0 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 6e 66  ;.      if( pInf
274b1 6f 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  o==0 ) return 0;
274b2 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 70 4e  .      pInfo->pN
274b3 65 78 74 20 3d 20 70 54 6f 70 6c 65 76 65 6c 2d  ext = pToplevel-
274b4 3e 70 41 69 6e 63 3b 0a 20 20 20 20 20 20 70 54  >pAinc;.      pT
274b5 6f 70 6c 65 76 65 6c 2d 3e 70 41 69 6e 63 20 3d  oplevel->pAinc =
274b6 20 70 49 6e 66 6f 3b 0a 20 20 20 20 20 20 70 49   pInfo;.      pI
274b7 6e 66 6f 2d 3e 70 54 61 62 20 3d 20 70 54 61 62  nfo->pTab = pTab
274b8 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 69  ;.      pInfo->i
274b9 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 20 20  Db = iDb;.      
274ba 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 4d 65 6d 2b  pToplevel->nMem+
274bb 2b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  +;              
274bc 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
274bd 74 6f 20 68 6f 6c 64 20 6e 61 6d 65 20 6f 66 20  to hold name of 
274be 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 70  table */.      p
274bf 49 6e 66 6f 2d 3e 72 65 67 43 74 72 20 3d 20 2b  Info->regCtr = +
274c0 2b 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 4d 65 6d  +pToplevel->nMem
274c1 3b 20 20 2f 2a 20 4d 61 78 20 72 6f 77 69 64 20  ;  /* Max rowid 
274c2 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20  register */.    
274c3 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 4d 65    pToplevel->nMe
274c4 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20 20 20 20  m++;            
274c5 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 69        /* Rowid i
274c6 6e 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  n sqlite_sequenc
274c7 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 6d  e */.    }.    m
274c8 65 6d 49 64 20 3d 20 70 49 6e 66 6f 2d 3e 72 65  emId = pInfo->re
274c9 67 43 74 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75  gCtr;.  }.  retu
274ca 72 6e 20 6d 65 6d 49 64 3b 0a 7d 0a 0a 2f 2a 0a  rn memId;.}../*.
274cb 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
274cc 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74  generates code t
274cd 68 61 74 20 77 69 6c 6c 20 69 6e 69 74 69 61 6c  hat will initial
274ce 69 7a 65 20 61 6c 6c 20 6f 66 20 74 68 65 0a 2a  ize all of the.*
274cf 2a 20 72 65 67 69 73 74 65 72 20 75 73 65 64 20  * register used 
274d0 62 79 20 74 68 65 20 61 75 74 6f 69 6e 63 72 65  by the autoincre
274d1 6d 65 6e 74 20 74 72 61 63 6b 65 72 2e 20 20 0a  ment tracker.  .
274d2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
274d3 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75  E void sqlite3Au
274d4 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65 67 69 6e  toincrementBegin
274d5 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b  (Parse *pParse){
274d6 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a  .  AutoincInfo *
274d7 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  p;            /*
274d8 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   Information abo
274d9 75 74 20 61 6e 20 41 55 54 4f 49 4e 43 52 45 4d  ut an AUTOINCREM
274da 45 4e 54 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ENT */.  sqlite3
274db 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
274dc 62 3b 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62  b;  /* The datab
274dd 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
274de 2f 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20 20  /.  Db *pDb;    
274df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
274e0 2a 20 44 61 74 61 62 61 73 65 20 6f 6e 6c 79 20  * Database only 
274e1 61 75 74 6f 69 6e 63 20 74 61 62 6c 65 20 2a 2f  autoinc table */
274e2 0a 20 20 69 6e 74 20 6d 65 6d 49 64 3b 20 20 20  .  int memId;   
274e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
274e4 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   Register holdin
274e5 67 20 6d 61 78 20 72 6f 77 69 64 20 2a 2f 0a 20  g max rowid */. 
274e6 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20 20 20   int addr;      
274e7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
274e8 20 56 44 42 45 20 61 64 64 72 65 73 73 20 2a 2f   VDBE address */
274e9 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
274ea 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 20 2f 2a  rse->pVdbe;   /*
274eb 20 56 44 42 45 20 75 6e 64 65 72 20 63 6f 6e 73   VDBE under cons
274ec 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f  truction */..  /
274ed 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
274ee 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 64  s never called d
274ef 75 72 69 6e 67 20 74 72 69 67 67 65 72 2d 67 65  uring trigger-ge
274f0 6e 65 72 61 74 69 6f 6e 2e 20 20 49 74 20 69 73  neration.  It is
274f1 0a 20 20 2a 2a 20 6f 6e 6c 79 20 63 61 6c 6c 65  .  ** only calle
274f2 64 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 2d 6c  d from the top-l
274f3 65 76 65 6c 20 2a 2f 0a 20 20 61 73 73 65 72 74  evel */.  assert
274f4 28 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67  ( pParse->pTrigg
274f5 65 72 54 61 62 3d 3d 30 20 29 3b 0a 20 20 61 73  erTab==0 );.  as
274f6 73 65 72 74 28 20 70 50 61 72 73 65 3d 3d 73 71  sert( pParse==sq
274f7 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76  lite3ParseToplev
274f8 65 6c 28 70 50 61 72 73 65 29 20 29 3b 0a 0a 20  el(pParse) );.. 
274f9 20 61 73 73 65 72 74 28 20 76 20 29 3b 20 20 20   assert( v );   
274fa 2f 2a 20 57 65 20 66 61 69 6c 65 64 20 6c 6f 6e  /* We failed lon
274fb 67 20 61 67 6f 20 69 66 20 74 68 69 73 20 69 73  g ago if this is
274fc 20 6e 6f 74 20 73 6f 20 2a 2f 0a 20 20 66 6f 72   not so */.  for
274fd 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 41 69  (p = pParse->pAi
274fe 6e 63 3b 20 70 3b 20 70 20 3d 20 70 2d 3e 70 4e  nc; p; p = p->pN
274ff 65 78 74 29 7b 0a 20 20 20 20 70 44 62 20 3d 20  ext){.    pDb = 
27500 26 64 62 2d 3e 61 44 62 5b 70 2d 3e 69 44 62 5d  &db->aDb[p->iDb]
27501 3b 0a 20 20 20 20 6d 65 6d 49 64 20 3d 20 70 2d  ;.    memId = p-
27502 3e 72 65 67 43 74 72 3b 0a 20 20 20 20 73 71 6c  >regCtr;.    sql
27503 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50  ite3OpenTable(pP
27504 61 72 73 65 2c 20 30 2c 20 70 2d 3e 69 44 62 2c  arse, 0, p->iDb,
27505 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 70   pDb->pSchema->p
27506 53 65 71 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52  SeqTab, OP_OpenR
27507 65 61 64 29 3b 0a 20 20 20 20 61 64 64 72 20 3d  ead);.    addr =
27508 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
27509 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20  entAddr(v);.    
2750a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2750b 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
2750c 20 30 2c 20 6d 65 6d 49 64 2d 31 2c 20 30 2c 20   0, memId-1, 0, 
2750d 70 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  p->pTab->zName, 
2750e 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
2750f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
27510 52 65 77 69 6e 64 2c 20 30 2c 20 61 64 64 72 2b  Rewind, 0, addr+
27511 39 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  9);.    sqlite3V
27512 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
27513 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c 20 6d 65  Column, 0, 0, me
27514 6d 49 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  mId);.    sqlite
27515 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
27516 50 5f 4e 65 2c 20 6d 65 6d 49 64 2d 31 2c 20 61  P_Ne, memId-1, a
27517 64 64 72 2b 37 2c 20 6d 65 6d 49 64 29 3b 0a 20  ddr+7, memId);. 
27518 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
27519 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45  angeP5(v, SQLITE
2751a 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20  _JUMPIFNULL);.  
2751b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2751c 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c  Op2(v, OP_Rowid,
2751d 20 30 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a 20 20   0, memId+1);.  
2751e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2751f 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
27520 2c 20 30 2c 20 31 2c 20 6d 65 6d 49 64 29 3b 0a  , 0, 1, memId);.
27521 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27522 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
27523 2c 20 30 2c 20 61 64 64 72 2b 39 29 3b 0a 20 20  , 0, addr+9);.  
27524 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27525 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
27526 30 2c 20 61 64 64 72 2b 32 29 3b 0a 20 20 20 20  0, addr+2);.    
27527 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27528 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
27529 20 30 2c 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20   0, memId);.    
2752a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2752b 30 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 29 3b 0a  0(v, OP_Close);.
2752c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 70 64    }.}../*.** Upd
2752d 61 74 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  ate the maximum 
2752e 72 6f 77 69 64 20 66 6f 72 20 61 6e 20 61 75 74  rowid for an aut
2752f 6f 69 6e 63 72 65 6d 65 6e 74 20 63 61 6c 63 75  oincrement calcu
27530 6c 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  lation..**.** Th
27531 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c  is routine shoul
27532 64 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e  d be called when
27533 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   the top of the 
27534 73 74 61 63 6b 20 68 6f 6c 64 73 20 61 0a 2a 2a  stack holds a.**
27535 20 6e 65 77 20 72 6f 77 69 64 20 74 68 61 74 20   new rowid that 
27536 69 73 20 61 62 6f 75 74 20 74 6f 20 62 65 20 69  is about to be i
27537 6e 73 65 72 74 65 64 2e 20 20 49 66 20 74 68 61  nserted.  If tha
27538 74 20 6e 65 77 20 72 6f 77 69 64 20 69 73 0a 2a  t new rowid is.*
27539 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  * larger than th
2753a 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20  e maximum rowid 
2753b 69 6e 20 74 68 65 20 6d 65 6d 49 64 20 6d 65 6d  in the memId mem
2753c 6f 72 79 20 63 65 6c 6c 2c 20 74 68 65 6e 20 74  ory cell, then t
2753d 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 65 6c  he.** memory cel
2753e 6c 20 69 73 20 75 70 64 61 74 65 64 2e 20 20 54  l is updated.  T
2753f 68 65 20 73 74 61 63 6b 20 69 73 20 75 6e 63 68  he stack is unch
27540 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  anged..*/.static
27541 20 76 6f 69 64 20 61 75 74 6f 49 6e 63 53 74 65   void autoIncSte
27542 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  p(Parse *pParse,
27543 20 69 6e 74 20 6d 65 6d 49 64 2c 20 69 6e 74 20   int memId, int 
27544 72 65 67 52 6f 77 69 64 29 7b 0a 20 20 69 66 28  regRowid){.  if(
27545 20 6d 65 6d 49 64 3e 30 20 29 7b 0a 20 20 20 20   memId>0 ){.    
27546 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27547 32 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c  2(pParse->pVdbe,
27548 20 4f 50 5f 4d 65 6d 4d 61 78 2c 20 6d 65 6d 49   OP_MemMax, memI
27549 64 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  d, regRowid);.  
2754a 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
2754b 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65  routine generate
2754c 73 20 74 68 65 20 63 6f 64 65 20 6e 65 65 64 65  s the code neede
2754d 64 20 74 6f 20 77 72 69 74 65 20 61 75 74 6f 69  d to write autoi
2754e 6e 63 72 65 6d 65 6e 74 0a 2a 2a 20 6d 61 78 69  ncrement.** maxi
2754f 6d 75 6d 20 72 6f 77 69 64 20 76 61 6c 75 65 73  mum rowid values
27550 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 73   back into the s
27551 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 72  qlite_sequence r
27552 65 67 69 73 74 65 72 2e 0a 2a 2a 20 45 76 65 72  egister..** Ever
27553 79 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  y statement that
27554 20 6d 69 67 68 74 20 64 6f 20 61 6e 20 49 4e 53   might do an INS
27555 45 52 54 20 69 6e 74 6f 20 61 6e 20 61 75 74 6f  ERT into an auto
27556 69 6e 63 72 65 6d 65 6e 74 0a 2a 2a 20 74 61 62  increment.** tab
27557 6c 65 20 28 65 69 74 68 65 72 20 64 69 72 65 63  le (either direc
27558 74 6c 79 20 6f 72 20 74 68 72 6f 75 67 68 20 74  tly or through t
27559 72 69 67 67 65 72 73 29 20 6e 65 65 64 73 20 74  riggers) needs t
2755a 6f 20 63 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 72  o call this.** r
2755b 6f 75 74 69 6e 65 20 6a 75 73 74 20 62 65 66 6f  outine just befo
2755c 72 65 20 74 68 65 20 22 65 78 69 74 22 20 63 6f  re the "exit" co
2755d 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  de..*/.SQLITE_PR
2755e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2755f 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45  e3AutoincrementE
27560 6e 64 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  nd(Parse *pParse
27561 29 7b 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f  ){.  AutoincInfo
27562 20 2a 70 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d   *p;.  Vdbe *v =
27563 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
27564 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
27565 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 61  pParse->db;..  a
27566 73 73 65 72 74 28 20 76 20 29 3b 0a 20 20 66 6f  ssert( v );.  fo
27567 72 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 41  r(p = pParse->pA
27568 69 6e 63 3b 20 70 3b 20 70 20 3d 20 70 2d 3e 70  inc; p; p = p->p
27569 4e 65 78 74 29 7b 0a 20 20 20 20 44 62 20 2a 70  Next){.    Db *p
2756a 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 70 2d  Db = &db->aDb[p-
2756b 3e 69 44 62 5d 3b 0a 20 20 20 20 69 6e 74 20 6a  >iDb];.    int j
2756c 31 2c 20 6a 32 2c 20 6a 33 2c 20 6a 34 2c 20 6a  1, j2, j3, j4, j
2756d 35 3b 0a 20 20 20 20 69 6e 74 20 69 52 65 63 3b  5;.    int iRec;
2756e 0a 20 20 20 20 69 6e 74 20 6d 65 6d 49 64 20 3d  .    int memId =
2756f 20 70 2d 3e 72 65 67 43 74 72 3b 0a 0a 20 20 20   p->regCtr;..   
27570 20 69 52 65 63 20 3d 20 73 71 6c 69 74 65 33 47   iRec = sqlite3G
27571 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
27572 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 70  );.    sqlite3Op
27573 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  enTable(pParse, 
27574 30 2c 20 70 2d 3e 69 44 62 2c 20 70 44 62 2d 3e  0, p->iDb, pDb->
27575 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 61 62  pSchema->pSeqTab
27576 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b  , OP_OpenWrite);
27577 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65  .    j1 = sqlite
27578 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
27579 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 6d 65 6d 49 64  P_NotNull, memId
2757a 2b 31 29 3b 0a 20 20 20 20 6a 32 20 3d 20 73 71  +1);.    j2 = sq
2757b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
2757c 76 2c 20 4f 50 5f 52 65 77 69 6e 64 29 3b 0a 20  v, OP_Rewind);. 
2757d 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56     j3 = sqlite3V
2757e 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2757f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c 20 69 52  Column, 0, 0, iR
27580 65 63 29 3b 0a 20 20 20 20 6a 34 20 3d 20 73 71  ec);.    j4 = sq
27581 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
27582 76 2c 20 4f 50 5f 45 71 2c 20 6d 65 6d 49 64 2d  v, OP_Eq, memId-
27583 31 2c 20 30 2c 20 69 52 65 63 29 3b 0a 20 20 20  1, 0, iRec);.   
27584 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27585 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 30  p2(v, OP_Next, 0
27586 2c 20 6a 33 29 3b 0a 20 20 20 20 73 71 6c 69 74  , j3);.    sqlit
27587 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
27588 2c 20 6a 32 29 3b 0a 20 20 20 20 73 71 6c 69 74  , j2);.    sqlit
27589 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2758a 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 30 2c 20  OP_NewRowid, 0, 
2758b 6d 65 6d 49 64 2b 31 29 3b 0a 20 20 20 20 6a 35  memId+1);.    j5
2758c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2758d 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29  dOp0(v, OP_Goto)
2758e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2758f 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 34 29  eJumpHere(v, j4)
27590 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
27591 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f  eAddOp2(v, OP_Ro
27592 77 69 64 2c 20 30 2c 20 6d 65 6d 49 64 2b 31 29  wid, 0, memId+1)
27593 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
27594 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29  eJumpHere(v, j1)
27595 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
27596 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 35 29  eJumpHere(v, j5)
27597 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
27598 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61  eAddOp3(v, OP_Ma
27599 6b 65 52 65 63 6f 72 64 2c 20 6d 65 6d 49 64 2d  keRecord, memId-
2759a 31 2c 20 32 2c 20 69 52 65 63 29 3b 0a 20 20 20  1, 2, iRec);.   
2759b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2759c 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c  p3(v, OP_Insert,
2759d 20 30 2c 20 69 52 65 63 2c 20 6d 65 6d 49 64 2b   0, iRec, memId+
2759e 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
2759f 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f  dbeChangeP5(v, O
275a0 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20  PFLAG_APPEND);. 
275a1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
275a2 64 4f 70 30 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp0(v, OP_Close
275a3 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  );.    sqlite3Re
275a4 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
275a5 72 73 65 2c 20 69 52 65 63 29 3b 0a 20 20 7d 0a  rse, iRec);.  }.
275a6 7d 0a 23 65 6c 73 65 0a 2f 2a 0a 2a 2a 20 49 66  }.#else./*.** If
275a7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
275a8 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73 20 64 65  OINCREMENT is de
275a9 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20  fined, then the 
275aa 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73 0a 2a  three routines.*
275ab 2a 20 61 62 6f 76 65 20 61 72 65 20 61 6c 6c 20  * above are all 
275ac 6e 6f 2d 6f 70 73 0a 2a 2f 0a 23 20 64 65 66 69  no-ops.*/.# defi
275ad 6e 65 20 61 75 74 6f 49 6e 63 42 65 67 69 6e 28  ne autoIncBegin(
275ae 41 2c 42 2c 43 29 20 28 30 29 0a 23 20 64 65 66  A,B,C) (0).# def
275af 69 6e 65 20 61 75 74 6f 49 6e 63 53 74 65 70 28  ine autoIncStep(
275b0 41 2c 42 2c 43 29 0a 23 65 6e 64 69 66 20 2f 2a  A,B,C).#endif /*
275b1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
275b2 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a 0a  OINCREMENT */...
275b3 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61  /* Forward decla
275b4 72 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63  ration */.static
275b5 20 69 6e 74 20 78 66 65 72 4f 70 74 69 6d 69 7a   int xferOptimiz
275b6 61 74 69 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a  ation(.  Parse *
275b7 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f  pParse,        /
275b8 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74  * Parser context
275b9 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 44 65   */.  Table *pDe
275ba 73 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  st,         /* T
275bb 68 65 20 74 61 62 6c 65 20 77 65 20 61 72 65 20  he table we are 
275bc 69 6e 73 65 72 74 69 6e 67 20 69 6e 74 6f 20 2a  inserting into *
275bd 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
275be 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 53  ect,      /* A S
275bf 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
275c0 74 6f 20 75 73 65 20 61 73 20 74 68 65 20 64 61  to use as the da
275c1 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20 69  ta source */.  i
275c2 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20 20  nt onError,     
275c3 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 68       /* How to h
275c4 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74  andle constraint
275c5 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74   errors */.  int
275c6 20 69 44 62 44 65 73 74 20 20 20 20 20 20 20 20   iDbDest        
275c7 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
275c8 73 65 20 6f 66 20 70 44 65 73 74 20 2a 2f 0a 29  se of pDest */.)
275c9 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  ;../*.** This ro
275ca 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 20 74 6f  utine is call to
275cb 20 68 61 6e 64 6c 65 20 53 51 4c 20 6f 66 20 74   handle SQL of t
275cc 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72  he following for
275cd 6d 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 69 6e 73  ms:.**.**    ins
275ce 65 72 74 20 69 6e 74 6f 20 54 41 42 4c 45 20 28  ert into TABLE (
275cf 49 44 4c 49 53 54 29 20 76 61 6c 75 65 73 28 45  IDLIST) values(E
275d0 58 50 52 4c 49 53 54 29 0a 2a 2a 20 20 20 20 69  XPRLIST).**    i
275d1 6e 73 65 72 74 20 69 6e 74 6f 20 54 41 42 4c 45  nsert into TABLE
275d2 20 28 49 44 4c 49 53 54 29 20 73 65 6c 65 63 74   (IDLIST) select
275d3 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 44 4c 49 53  .**.** The IDLIS
275d4 54 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20  T following the 
275d5 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 61 6c  table name is al
275d6 77 61 79 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 20  ways optional.  
275d7 49 66 20 6f 6d 69 74 74 65 64 2c 0a 2a 2a 20 74  If omitted,.** t
275d8 68 65 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c  hen a list of al
275d9 6c 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 74 68  l columns for th
275da 65 20 74 61 62 6c 65 20 69 73 20 73 75 62 73 74  e table is subst
275db 69 74 75 74 65 64 2e 20 20 54 68 65 20 49 44 4c  ituted.  The IDL
275dc 49 53 54 0a 2a 2a 20 61 70 70 65 61 72 73 20 69  IST.** appears i
275dd 6e 20 74 68 65 20 70 43 6f 6c 75 6d 6e 20 70 61  n the pColumn pa
275de 72 61 6d 65 74 65 72 2e 20 20 70 43 6f 6c 75 6d  rameter.  pColum
275df 6e 20 69 73 20 4e 55 4c 4c 20 69 66 20 49 44 4c  n is NULL if IDL
275e0 49 53 54 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a  IST is omitted..
275e1 2a 2a 0a 2a 2a 20 54 68 65 20 70 4c 69 73 74 20  **.** The pList 
275e2 70 61 72 61 6d 65 74 65 72 20 68 6f 6c 64 73 20  parameter holds 
275e3 45 58 50 52 4c 49 53 54 20 69 6e 20 74 68 65 20  EXPRLIST in the 
275e4 66 69 72 73 74 20 66 6f 72 6d 20 6f 66 20 74 68  first form of th
275e5 65 20 49 4e 53 45 52 54 0a 2a 2a 20 73 74 61 74  e INSERT.** stat
275e6 65 6d 65 6e 74 20 61 62 6f 76 65 2c 20 61 6e 64  ement above, and
275e7 20 70 53 65 6c 65 63 74 20 69 73 20 4e 55 4c 4c   pSelect is NULL
275e8 2e 20 20 46 6f 72 20 74 68 65 20 73 65 63 6f 6e  .  For the secon
275e9 64 20 66 6f 72 6d 2c 20 70 4c 69 73 74 20 69 73  d form, pList is
275ea 0a 2a 2a 20 4e 55 4c 4c 20 61 6e 64 20 70 53 65  .** NULL and pSe
275eb 6c 65 63 74 20 69 73 20 61 20 70 6f 69 6e 74 65  lect is a pointe
275ec 72 20 74 6f 20 74 68 65 20 73 65 6c 65 63 74 20  r to the select 
275ed 73 74 61 74 65 6d 65 6e 74 20 75 73 65 64 20 74  statement used t
275ee 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 64 61  o generate.** da
275ef 74 61 20 66 6f 72 20 74 68 65 20 69 6e 73 65 72  ta for the inser
275f0 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  t..**.** The cod
275f1 65 20 67 65 6e 65 72 61 74 65 64 20 66 6f 6c 6c  e generated foll
275f2 6f 77 73 20 6f 6e 65 20 6f 66 20 66 6f 75 72 20  ows one of four 
275f3 74 65 6d 70 6c 61 74 65 73 2e 20 20 46 6f 72 20  templates.  For 
275f4 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 73 65 6c 65  a simple.** sele
275f5 63 74 20 77 69 74 68 20 64 61 74 61 20 63 6f 6d  ct with data com
275f6 69 6e 67 20 66 72 6f 6d 20 61 20 56 41 4c 55 45  ing from a VALUE
275f7 53 20 63 6c 61 75 73 65 2c 20 74 68 65 20 63 6f  S clause, the co
275f8 64 65 20 65 78 65 63 75 74 65 73 0a 2a 2a 20 6f  de executes.** o
275f9 6e 63 65 20 73 74 72 61 69 67 68 74 20 64 6f 77  nce straight dow
275fa 6e 20 74 68 72 6f 75 67 68 2e 20 20 50 73 65 75  n through.  Pseu
275fb 64 6f 2d 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20  do-code follows 
275fc 28 77 65 20 63 61 6c 6c 20 74 68 69 73 0a 2a 2a  (we call this.**
275fd 20 74 68 65 20 22 31 73 74 20 74 65 6d 70 6c 61   the "1st templa
275fe 74 65 22 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  te"):.**.**     
275ff 20 20 20 20 6f 70 65 6e 20 77 72 69 74 65 20 63      open write c
27600 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e  ursor to <table>
27601 20 61 6e 64 20 69 74 73 20 69 6e 64 69 63 65 73   and its indices
27602 0a 2a 2a 20 20 20 20 20 20 20 20 20 70 75 74 73  .**         puts
27603 20 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20 65   VALUES clause e
27604 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 74 6f 20  xpressions onto 
27605 74 68 65 20 73 74 61 63 6b 0a 2a 2a 20 20 20 20  the stack.**    
27606 20 20 20 20 20 77 72 69 74 65 20 74 68 65 20 72       write the r
27607 65 73 75 6c 74 69 6e 67 20 72 65 63 6f 72 64 20  esulting record 
27608 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20  into <table>.** 
27609 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70 0a          cleanup.
2760a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 72 65 65 20  **.** The three 
2760b 72 65 6d 61 69 6e 69 6e 67 20 74 65 6d 70 6c 61  remaining templa
2760c 74 65 73 20 61 73 73 75 6d 65 20 74 68 65 20 73  tes assume the s
2760d 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 66 20 74  tatement is of t
2760e 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20  he form.**.**   
2760f 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c 74 61 62  INSERT INTO <tab
27610 6c 65 3e 20 53 45 4c 45 43 54 20 2e 2e 2e 0a 2a  le> SELECT ....*
27611 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 45 4c 45  *.** If the SELE
27612 43 54 20 63 6c 61 75 73 65 20 69 73 20 6f 66 20  CT clause is of 
27613 74 68 65 20 72 65 73 74 72 69 63 74 65 64 20 66  the restricted f
27614 6f 72 6d 20 22 53 45 4c 45 43 54 20 2a 20 46 52  orm "SELECT * FR
27615 4f 4d 20 3c 74 61 62 6c 65 32 3e 22 20 2d 0a 2a  OM <table2>" -.*
27616 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  * in other words
27617 20 69 66 20 74 68 65 20 53 45 4c 45 43 54 20 70   if the SELECT p
27618 75 6c 6c 73 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73  ulls all columns
27619 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 74   from a single t
2761a 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 72  able.** and ther
2761b 65 20 69 73 20 6e 6f 20 57 48 45 52 45 20 6f 72  e is no WHERE or
2761c 20 4c 49 4d 49 54 20 6f 72 20 47 52 4f 55 50 20   LIMIT or GROUP 
2761d 42 59 20 6f 72 20 4f 52 44 45 52 20 42 59 20 63  BY or ORDER BY c
2761e 6c 61 75 73 65 73 2c 20 61 6e 64 0a 2a 2a 20 69  lauses, and.** i
2761f 66 20 3c 74 61 62 6c 65 32 3e 20 61 6e 64 20 3c  f <table2> and <
27620 74 61 62 6c 65 31 3e 20 61 72 65 20 64 69 73 74  table1> are dist
27621 69 6e 63 74 20 74 61 62 6c 65 73 20 62 75 74 20  inct tables but 
27622 68 61 76 65 20 69 64 65 6e 74 69 63 61 6c 0a 2a  have identical.*
27623 2a 20 73 63 68 65 6d 61 73 2c 20 69 6e 63 6c 75  * schemas, inclu
27624 64 69 6e 67 20 61 6c 6c 20 74 68 65 20 73 61 6d  ding all the sam
27625 65 20 69 6e 64 69 63 65 73 2c 20 74 68 65 6e 20  e indices, then 
27626 61 20 73 70 65 63 69 61 6c 20 6f 70 74 69 6d 69  a special optimi
27627 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76  zation.** is inv
27628 6f 6b 65 64 20 74 68 61 74 20 63 6f 70 69 65 73  oked that copies
27629 20 72 61 77 20 72 65 63 6f 72 64 73 20 66 72 6f   raw records fro
2762a 6d 20 3c 74 61 62 6c 65 32 3e 20 6f 76 65 72 20  m <table2> over 
2762b 74 6f 20 3c 74 61 62 6c 65 31 3e 2e 0a 2a 2a 20  to <table1>..** 
2762c 53 65 65 20 74 68 65 20 78 66 65 72 4f 70 74 69  See the xferOpti
2762d 6d 69 7a 61 74 69 6f 6e 28 29 20 66 75 6e 63 74  mization() funct
2762e 69 6f 6e 20 66 6f 72 20 74 68 65 20 69 6d 70 6c  ion for the impl
2762f 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
27630 69 73 0a 2a 2a 20 74 65 6d 70 6c 61 74 65 2e 20  is.** template. 
27631 20 54 68 69 73 20 69 73 20 74 68 65 20 32 6e 64   This is the 2nd
27632 20 74 65 6d 70 6c 61 74 65 2e 0a 2a 2a 0a 2a 2a   template..**.**
27633 20 20 20 20 20 20 20 20 20 6f 70 65 6e 20 61 20           open a 
27634 77 72 69 74 65 20 63 75 72 73 6f 72 20 74 6f 20  write cursor to 
27635 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 20  <table>.**      
27636 20 20 20 6f 70 65 6e 20 72 65 61 64 20 63 75 72     open read cur
27637 73 6f 72 20 6f 6e 20 3c 74 61 62 6c 65 32 3e 0a  sor on <table2>.
27638 2a 2a 20 20 20 20 20 20 20 20 20 74 72 61 6e 73  **         trans
27639 66 65 72 20 61 6c 6c 20 72 65 63 6f 72 64 73 20  fer all records 
2763a 69 6e 20 3c 74 61 62 6c 65 32 3e 20 6f 76 65 72  in <table2> over
2763b 20 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20   to <table>.**  
2763c 20 20 20 20 20 20 20 63 6c 6f 73 65 20 63 75 72         close cur
2763d 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  sors.**         
2763e 66 6f 72 65 61 63 68 20 69 6e 64 65 78 20 6f 6e  foreach index on
2763f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20   <table>.**     
27640 20 20 20 20 20 20 6f 70 65 6e 20 61 20 77 72 69        open a wri
27641 74 65 20 63 75 72 73 6f 72 20 6f 6e 20 74 68 65  te cursor on the
27642 20 3c 74 61 62 6c 65 3e 20 69 6e 64 65 78 0a 2a   <table> index.*
27643 2a 20 20 20 20 20 20 20 20 20 20 20 6f 70 65 6e  *           open
27644 20 61 20 72 65 61 64 20 63 75 72 73 6f 72 20 6f   a read cursor o
27645 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  n the correspond
27646 69 6e 67 20 3c 74 61 62 6c 65 32 3e 20 69 6e 64  ing <table2> ind
27647 65 78 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ex.**           
27648 74 72 61 6e 73 66 65 72 20 61 6c 6c 20 72 65 63  transfer all rec
27649 6f 72 64 73 20 66 72 6f 6d 20 74 68 65 20 72 65  ords from the re
2764a 61 64 20 74 6f 20 74 68 65 20 77 72 69 74 65 20  ad to the write 
2764b 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20  cursors.**      
2764c 20 20 20 20 20 63 6c 6f 73 65 20 63 75 72 73 6f       close curso
2764d 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 65 6e  rs.**         en
2764e 64 20 66 6f 72 65 61 63 68 0a 2a 2a 0a 2a 2a 20  d foreach.**.** 
2764f 54 68 65 20 33 72 64 20 74 65 6d 70 6c 61 74 65  The 3rd template
27650 20 69 73 20 66 6f 72 20 77 68 65 6e 20 74 68 65   is for when the
27651 20 73 65 63 6f 6e 64 20 74 65 6d 70 6c 61 74 65   second template
27652 20 64 6f 65 73 20 6e 6f 74 20 61 70 70 6c 79 0a   does not apply.
27653 2a 2a 20 61 6e 64 20 74 68 65 20 53 45 4c 45 43  ** and the SELEC
27654 54 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f  T clause does no
27655 74 20 72 65 61 64 20 66 72 6f 6d 20 3c 74 61 62  t read from <tab
27656 6c 65 3e 20 61 74 20 61 6e 79 20 74 69 6d 65 2e  le> at any time.
27657 0a 2a 2a 20 54 68 65 20 67 65 6e 65 72 61 74 65  .** The generate
27658 64 20 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 74  d code follows t
27659 68 69 73 20 74 65 6d 70 6c 61 74 65 3a 0a 2a 2a  his template:.**
2765a 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20  .**         EOF 
2765b 3c 2d 20 30 0a 2a 2a 20 20 20 20 20 20 20 20 20  <- 0.**         
2765c 58 20 3c 2d 20 41 0a 2a 2a 20 20 20 20 20 20 20  X <- A.**       
2765d 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20 20 20 20    goto B.**     
2765e 20 41 3a 20 73 65 74 75 70 20 66 6f 72 20 74 68   A: setup for th
2765f 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20  e SELECT.**     
27660 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68      loop over th
27661 65 20 72 6f 77 73 20 69 6e 20 74 68 65 20 53 45  e rows in the SE
27662 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20  LECT.**         
27663 20 20 6c 6f 61 64 20 76 61 6c 75 65 73 20 69 6e    load values in
27664 74 6f 20 72 65 67 69 73 74 65 72 73 20 52 2e 2e  to registers R..
27665 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  R+n.**          
27666 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20   yield X.**     
27667 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a 2a 20      end loop.** 
27668 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70 20          cleanup 
27669 61 66 74 65 72 20 74 68 65 20 53 45 4c 45 43 54  after the SELECT
2766a 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20  .**         EOF 
2766b 3c 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 20  <- 1.**         
2766c 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20  yield X.**      
2766d 20 20 20 67 6f 74 6f 20 41 0a 2a 2a 20 20 20 20     goto A.**    
2766e 20 20 42 3a 20 6f 70 65 6e 20 77 72 69 74 65 20    B: open write 
2766f 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65  cursor to <table
27670 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69 63 65  > and its indice
27671 73 0a 2a 2a 20 20 20 20 20 20 43 3a 20 79 69 65  s.**      C: yie
27672 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20  ld X.**         
27673 69 66 20 45 4f 46 20 67 6f 74 6f 20 44 0a 2a 2a  if EOF goto D.**
27674 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 20           insert 
27675 74 68 65 20 73 65 6c 65 63 74 20 72 65 73 75 6c  the select resul
27676 74 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 20 66  t into <table> f
27677 72 6f 6d 20 52 2e 2e 52 2b 6e 0a 2a 2a 20 20 20  rom R..R+n.**   
27678 20 20 20 20 20 20 67 6f 74 6f 20 43 0a 2a 2a 20        goto C.** 
27679 20 20 20 20 20 44 3a 20 63 6c 65 61 6e 75 70 0a       D: cleanup.
2767a 2a 2a 0a 2a 2a 20 54 68 65 20 34 74 68 20 74 65  **.** The 4th te
2767b 6d 70 6c 61 74 65 20 69 73 20 75 73 65 64 20 69  mplate is used i
2767c 66 20 74 68 65 20 69 6e 73 65 72 74 20 73 74 61  f the insert sta
2767d 74 65 6d 65 6e 74 20 74 61 6b 65 73 20 69 74 73  tement takes its
2767e 0a 2a 2a 20 76 61 6c 75 65 73 20 66 72 6f 6d 20  .** values from 
2767f 61 20 53 45 4c 45 43 54 20 62 75 74 20 74 68 65  a SELECT but the
27680 20 64 61 74 61 20 69 73 20 62 65 69 6e 67 20 69   data is being i
27681 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 61 20 74  nserted into a t
27682 61 62 6c 65 0a 2a 2a 20 74 68 61 74 20 69 73 20  able.** that is 
27683 61 6c 73 6f 20 72 65 61 64 20 61 73 20 70 61 72  also read as par
27684 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e  t of the SELECT.
27685 20 20 49 6e 20 74 68 65 20 74 68 69 72 64 20 66    In the third f
27686 6f 72 6d 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20  orm,.** we have 
27687 74 6f 20 75 73 65 20 61 20 69 6e 74 65 72 6d 65  to use a interme
27688 64 69 61 74 65 20 74 61 62 6c 65 20 74 6f 20 73  diate table to s
27689 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73  tore the results
2768a 20 6f 66 0a 2a 2a 20 74 68 65 20 73 65 6c 65 63   of.** the selec
2768b 74 2e 20 20 54 68 65 20 74 65 6d 70 6c 61 74 65  t.  The template
2768c 20 69 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a   is like this:.*
2768d 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46  *.**         EOF
2768e 20 3c 2d 20 30 0a 2a 2a 20 20 20 20 20 20 20 20   <- 0.**        
2768f 20 58 20 3c 2d 20 41 0a 2a 2a 20 20 20 20 20 20   X <- A.**      
27690 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20 20 20     goto B.**    
27691 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72 20 74    A: setup for t
27692 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20  he SELECT.**    
27693 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74       loop over t
27694 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  he tables in the
27695 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20   SELECT.**      
27696 20 20 20 20 20 6c 6f 61 64 20 76 61 6c 75 65 20       load value 
27697 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 52 2e  into register R.
27698 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  .R+n.**         
27699 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20    yield X.**    
2769a 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a 2a       end loop.**
2769b 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70           cleanup
2769c 20 61 66 74 65 72 20 74 68 65 20 53 45 4c 45 43   after the SELEC
2769d 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46  T.**         EOF
2769e 20 3c 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20 20   <- 1.**        
2769f 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20   yield X.**     
276a0 20 20 20 20 68 61 6c 74 2d 65 72 72 6f 72 0a 2a      halt-error.*
276a1 2a 20 20 20 20 20 20 42 3a 20 6f 70 65 6e 20 74  *      B: open t
276a2 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20  emp table.**    
276a3 20 20 4c 3a 20 79 69 65 6c 64 20 58 0a 2a 2a 20    L: yield X.** 
276a4 20 20 20 20 20 20 20 20 69 66 20 45 4f 46 20 67          if EOF g
276a5 6f 74 6f 20 4d 0a 2a 2a 20 20 20 20 20 20 20 20  oto M.**        
276a6 20 69 6e 73 65 72 74 20 72 6f 77 20 66 72 6f 6d   insert row from
276a7 20 52 2e 2e 52 2b 6e 20 69 6e 74 6f 20 74 65 6d   R..R+n into tem
276a8 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20  p table.**      
276a9 20 20 20 67 6f 74 6f 20 4c 0a 2a 2a 20 20 20 20     goto L.**    
276aa 20 20 4d 3a 20 6f 70 65 6e 20 77 72 69 74 65 20    M: open write 
276ab 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65  cursor to <table
276ac 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69 63 65  > and its indice
276ad 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 72 65 77  s.**         rew
276ae 69 6e 64 20 74 65 6d 70 20 74 61 62 6c 65 0a 2a  ind temp table.*
276af 2a 20 20 20 20 20 20 43 3a 20 6c 6f 6f 70 20 6f  *      C: loop o
276b0 76 65 72 20 72 6f 77 73 20 6f 66 20 69 6e 74 65  ver rows of inte
276b1 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65 0a 2a  rmediate table.*
276b2 2a 20 20 20 20 20 20 20 20 20 20 20 74 72 61 6e  *           tran
276b3 73 66 65 72 20 76 61 6c 75 65 73 20 66 6f 72 6d  sfer values form
276b4 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61   intermediate ta
276b5 62 6c 65 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e  ble into <table>
276b6 0a 2a 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20  .**         end 
276b7 6c 6f 6f 70 0a 2a 2a 20 20 20 20 20 20 44 3a 20  loop.**      D: 
276b8 63 6c 65 61 6e 75 70 0a 2a 2f 0a 53 51 4c 49 54  cleanup.*/.SQLIT
276b9 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
276ba 71 6c 69 74 65 33 49 6e 73 65 72 74 28 0a 20 20  qlite3Insert(.  
276bb 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
276bc 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20        /* Parser 
276bd 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63  context */.  Src
276be 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20  List *pTabList, 
276bf 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 61     /* Name of ta
276c0 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77  ble into which w
276c1 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20  e are inserting 
276c2 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
276c3 4c 69 73 74 2c 20 20 20 20 20 20 2f 2a 20 4c 69  List,      /* Li
276c4 73 74 20 6f 66 20 76 61 6c 75 65 73 20 74 6f 20  st of values to 
276c5 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20  be inserted */. 
276c6 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
276c7 2c 20 20 20 20 20 20 2f 2a 20 41 20 53 45 4c 45  ,      /* A SELE
276c8 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  CT statement to 
276c9 75 73 65 20 61 73 20 74 68 65 20 64 61 74 61 20  use as the data 
276ca 73 6f 75 72 63 65 20 2a 2f 0a 20 20 49 64 4c 69  source */.  IdLi
276cb 73 74 20 2a 70 43 6f 6c 75 6d 6e 2c 20 20 20 20  st *pColumn,    
276cc 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65    /* Column name
276cd 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  s corresponding 
276ce 74 6f 20 49 44 4c 49 53 54 2e 20 2a 2f 0a 20 20  to IDLIST. */.  
276cf 69 6e 74 20 6f 6e 45 72 72 6f 72 20 20 20 20 20  int onError     
276d0 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20        /* How to 
276d1 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e  handle constrain
276d2 74 20 65 72 72 6f 72 73 20 2a 2f 0a 29 7b 0a 20  t errors */.){. 
276d3 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
276d4 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61         /* The ma
276d5 69 6e 20 64 61 74 61 62 61 73 65 20 73 74 72 75  in database stru
276d6 63 74 75 72 65 20 2a 2f 0a 20 20 54 61 62 6c 65  cture */.  Table
276d7 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
276d8 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f   /* The table to
276d9 20 69 6e 73 65 72 74 20 69 6e 74 6f 2e 20 20 61   insert into.  a
276da 6b 61 20 54 41 42 4c 45 20 2a 2f 0a 20 20 63 68  ka TABLE */.  ch
276db 61 72 20 2a 7a 54 61 62 3b 20 20 20 20 20 20 20  ar *zTab;       
276dc 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
276dd 68 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68  he table into wh
276de 69 63 68 20 77 65 20 61 72 65 20 69 6e 73 65 72  ich we are inser
276df 74 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ting */.  const 
276e0 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20  char *zDb;      
276e1 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64  /* Name of the d
276e2 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20  atabase holding 
276e3 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20  this table */.  
276e4 69 6e 74 20 69 2c 20 6a 2c 20 69 64 78 3b 20 20  int i, j, idx;  
276e5 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
276e6 75 6e 74 65 72 73 20 2a 2f 0a 20 20 56 64 62 65  unters */.  Vdbe
276e7 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20   *v;            
276e8 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
276e9 64 65 20 69 6e 74 6f 20 74 68 69 73 20 76 69 72  de into this vir
276ea 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a  tual machine */.
276eb 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20    Index *pIdx;  
276ec 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c          /* For l
276ed 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e 64 69  ooping over indi
276ee 63 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ces of the table
276ef 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d   */.  int nColum
276f0 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  n;          /* N
276f1 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
276f2 20 69 6e 20 74 68 65 20 64 61 74 61 20 2a 2f 0a   in the data */.
276f3 20 20 69 6e 74 20 6e 48 69 64 64 65 6e 20 3d 20    int nHidden = 
276f4 30 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  0;      /* Numbe
276f5 72 20 6f 66 20 68 69 64 64 65 6e 20 63 6f 6c 75  r of hidden colu
276f6 6d 6e 73 20 69 66 20 54 41 42 4c 45 20 69 73 20  mns if TABLE is 
276f7 76 69 72 74 75 61 6c 20 2a 2f 0a 20 20 69 6e 74  virtual */.  int
276f8 20 62 61 73 65 43 75 72 20 3d 20 30 3b 20 20 20   baseCur = 0;   
276f9 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72 73 6f     /* VDBE Curso
276fa 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 70 54 61  r number for pTa
276fb 62 20 2a 2f 0a 20 20 69 6e 74 20 6b 65 79 43 6f  b */.  int keyCo
276fc 6c 75 6d 6e 20 3d 20 2d 31 3b 20 20 20 2f 2a 20  lumn = -1;   /* 
276fd 43 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 74  Column that is t
276fe 68 65 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  he INTEGER PRIMA
276ff 52 59 20 4b 45 59 20 2a 2f 0a 20 20 69 6e 74 20  RY KEY */.  int 
27700 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20 20 20 20 20  endOfLoop;      
27701 20 20 2f 2a 20 4c 61 62 65 6c 20 66 6f 72 20 74    /* Label for t
27702 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e  he end of the in
27703 73 65 72 74 69 6f 6e 20 6c 6f 6f 70 20 2a 2f 0a  sertion loop */.
27704 20 20 69 6e 74 20 75 73 65 54 65 6d 70 54 61 62    int useTempTab
27705 6c 65 20 3d 20 30 3b 20 2f 2a 20 53 74 6f 72 65  le = 0; /* Store
27706 20 53 45 4c 45 43 54 20 72 65 73 75 6c 74 73 20   SELECT results 
27707 69 6e 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  in intermediate 
27708 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 73  table */.  int s
27709 72 63 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20  rcTab = 0;      
2770a 20 2f 2a 20 44 61 74 61 20 63 6f 6d 65 73 20 66   /* Data comes f
2770b 72 6f 6d 20 74 68 69 73 20 74 65 6d 70 6f 72 61  rom this tempora
2770c 72 79 20 63 75 72 73 6f 72 20 69 66 20 3e 3d 30  ry cursor if >=0
2770d 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 49 6e   */.  int addrIn
2770e 73 54 6f 70 20 3d 20 30 3b 20 20 20 2f 2a 20 4a  sTop = 0;   /* J
2770f 75 6d 70 20 74 6f 20 6c 61 62 65 6c 20 22 44 22  ump to label "D"
27710 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 6f   */.  int addrCo
27711 6e 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54  nt = 0;     /* T
27712 6f 70 20 6f 66 20 69 6e 73 65 72 74 20 6c 6f 6f  op of insert loo
27713 70 2e 20 4c 61 62 65 6c 20 22 43 22 20 69 6e 20  p. Label "C" in 
27714 74 65 6d 70 6c 61 74 65 73 20 33 20 61 6e 64 20  templates 3 and 
27715 34 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 53  4 */.  int addrS
27716 65 6c 65 63 74 20 3d 20 30 3b 20 20 20 2f 2a 20  elect = 0;   /* 
27717 41 64 64 72 65 73 73 20 6f 66 20 63 6f 72 6f 75  Address of corou
27718 74 69 6e 65 20 74 68 61 74 20 69 6d 70 6c 65 6d  tine that implem
27719 65 6e 74 73 20 74 68 65 20 53 45 4c 45 43 54 20  ents the SELECT 
2771a 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20  */.  SelectDest 
2771b 64 65 73 74 3b 20 20 20 20 20 20 2f 2a 20 44 65  dest;      /* De
2771c 73 74 69 6e 61 74 69 6f 6e 20 66 6f 72 20 53 45  stination for SE
2771d 4c 45 43 54 20 6f 6e 20 72 68 73 20 6f 66 20 49  LECT on rhs of I
2771e 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e 74 20 69  NSERT */.  int i
2771f 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
27720 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61 74   /* Index of dat
27721 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 54 41  abase holding TA
27722 42 4c 45 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62  BLE */.  Db *pDb
27723 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
27724 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
27725 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 20  ontaining table 
27726 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 69  being inserted i
27727 6e 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 61 70 70  nto */.  int app
27728 65 6e 64 46 6c 61 67 20 3d 20 30 3b 20 20 20 2f  endFlag = 0;   /
27729 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 69 6e  * True if the in
2772a 73 65 72 74 20 69 73 20 6c 69 6b 65 6c 79 20 74  sert is likely t
2772b 6f 20 62 65 20 61 6e 20 61 70 70 65 6e 64 20 2a  o be an append *
2772c 2f 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72  /..  /* Register
2772d 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a   allocations */.
2772e 20 20 69 6e 74 20 72 65 67 46 72 6f 6d 53 65 6c    int regFromSel
2772f 65 63 74 20 3d 20 30 3b 2f 2a 20 42 61 73 65 20  ect = 0;/* Base 
27730 72 65 67 69 73 74 65 72 20 66 6f 72 20 64 61 74  register for dat
27731 61 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 53 45  a coming from SE
27732 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 72 65  LECT */.  int re
27733 67 41 75 74 6f 69 6e 63 20 3d 20 30 3b 20 20 20  gAutoinc = 0;   
27734 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64  /* Register hold
27735 69 6e 67 20 74 68 65 20 41 55 54 4f 49 4e 43 52  ing the AUTOINCR
27736 45 4d 45 4e 54 20 63 6f 75 6e 74 65 72 20 2a 2f  EMENT counter */
27737 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 43 6f 75  .  int regRowCou
27738 6e 74 20 3d 20 30 3b 20 20 2f 2a 20 4d 65 6d 6f  nt = 0;  /* Memo
27739 72 79 20 63 65 6c 6c 20 75 73 65 64 20 66 6f 72  ry cell used for
2773a 20 74 68 65 20 72 6f 77 20 63 6f 75 6e 74 65 72   the row counter
2773b 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49 6e 73   */.  int regIns
2773c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42  ;           /* B
2773d 6c 6f 63 6b 20 6f 66 20 72 65 67 73 20 68 6f 6c  lock of regs hol
2773e 64 69 6e 67 20 72 6f 77 69 64 2b 64 61 74 61 20  ding rowid+data 
2773f 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 2a  being inserted *
27740 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64  /.  int regRowid
27741 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 72 65 67  ;         /* reg
27742 69 73 74 65 72 73 20 68 6f 6c 64 69 6e 67 20 69  isters holding i
27743 6e 73 65 72 74 20 72 6f 77 69 64 20 2a 2f 0a 20  nsert rowid */. 
27744 20 69 6e 74 20 72 65 67 44 61 74 61 3b 20 20 20   int regData;   
27745 20 20 20 20 20 20 20 2f 2a 20 72 65 67 69 73 74         /* regist
27746 65 72 20 68 6f 6c 64 69 6e 67 20 66 69 72 73 74  er holding first
27747 20 63 6f 6c 75 6d 6e 20 74 6f 20 69 6e 73 65 72   column to inser
27748 74 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 65  t */.  int regRe
27749 63 6f 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20  cord;        /* 
2774a 48 6f 6c 64 73 20 74 68 65 20 61 73 73 65 6d 62  Holds the assemb
2774b 6c 69 65 64 20 72 6f 77 20 72 65 63 6f 72 64 20  lied row record 
2774c 2a 2f 0a 20 20 69 6e 74 20 72 65 67 45 6f 66 20  */.  int regEof 
2774d 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 52 65  = 0;       /* Re
2774e 67 69 73 74 65 72 20 72 65 63 6f 72 64 69 6e 67  gister recording
2774f 20 65 6e 64 20 6f 66 20 53 45 4c 45 43 54 20 64   end of SELECT d
27750 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52  ata */.  int *aR
27751 65 67 49 64 78 20 3d 20 30 3b 20 20 20 20 20 2f  egIdx = 0;     /
27752 2a 20 4f 6e 65 20 72 65 67 69 73 74 65 72 20 61  * One register a
27753 6c 6c 6f 63 61 74 65 64 20 74 6f 20 65 61 63 68  llocated to each
27754 20 69 6e 64 65 78 20 2a 2f 0a 0a 23 69 66 6e 64   index */..#ifnd
27755 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
27756 52 49 47 47 45 52 0a 20 20 69 6e 74 20 69 73 56  RIGGER.  int isV
27757 69 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20  iew;            
27758 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
27759 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 69 6e  attempting to in
2775a 73 65 72 74 20 69 6e 74 6f 20 61 20 76 69 65 77  sert into a view
2775b 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70   */.  Trigger *p
2775c 54 72 69 67 67 65 72 3b 20 20 20 20 20 20 20 20  Trigger;        
2775d 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69    /* List of tri
2775e 67 67 65 72 73 20 6f 6e 20 70 54 61 62 2c 20 69  ggers on pTab, i
2775f 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20  f required */.  
27760 69 6e 74 20 74 6d 61 73 6b 3b 20 20 20 20 20 20  int tmask;      
27761 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
27762 61 73 6b 20 6f 66 20 74 72 69 67 67 65 72 20 74  ask of trigger t
27763 69 6d 65 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a  imes */.#endif..
27764 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
27765 62 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 65 73  b;.  memset(&des
27766 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 65 73  t, 0, sizeof(des
27767 74 29 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73  t));.  if( pPars
27768 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d  e->nErr || db->m
27769 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
2776a 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63     goto insert_c
2776b 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f  leanup;.  }..  /
2776c 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62  * Locate the tab
2776d 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65  le into which we
2776e 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 69   will be inserti
2776f 6e 67 20 6e 65 77 20 69 6e 66 6f 72 6d 61 74 69  ng new informati
27770 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  on..  */.  asser
27771 74 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72  t( pTabList->nSr
27772 63 3d 3d 31 20 29 3b 0a 20 20 7a 54 61 62 20 3d  c==1 );.  zTab =
27773 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e   pTabList->a[0].
27774 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 4e 45 56  zName;.  if( NEV
27775 45 52 28 7a 54 61 62 3d 3d 30 29 20 29 20 67 6f  ER(zTab==0) ) go
27776 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
27777 70 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69  p;.  pTab = sqli
27778 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70  te3SrcListLookup
27779 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73  (pParse, pTabLis
2777a 74 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d  t);.  if( pTab==
2777b 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e  0 ){.    goto in
2777c 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
2777d 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  }.  iDb = sqlite
2777e 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
2777f 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
27780 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  );.  assert( iDb
27781 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 70 44  <db->nDb );.  pD
27782 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62  b = &db->aDb[iDb
27783 5d 3b 0a 20 20 7a 44 62 20 3d 20 70 44 62 2d 3e  ];.  zDb = pDb->
27784 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 73 71 6c  zName;.  if( sql
27785 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
27786 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53  arse, SQLITE_INS
27787 45 52 54 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ERT, pTab->zName
27788 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20  , 0, zDb) ){.   
27789 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65   goto insert_cle
2778a 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  anup;.  }..  /* 
2778b 46 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65  Figure out if we
2778c 20 68 61 76 65 20 61 6e 79 20 74 72 69 67 67 65   have any trigge
2778d 72 73 20 61 6e 64 20 69 66 20 74 68 65 20 74 61  rs and if the ta
2778e 62 6c 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 69  ble being.  ** i
2778f 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 69 73 20  nserted into is 
27790 61 20 76 69 65 77 0a 20 20 2a 2f 0a 23 69 66 6e  a view.  */.#ifn
27791 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
27792 54 52 49 47 47 45 52 0a 20 20 70 54 72 69 67 67  TRIGGER.  pTrigg
27793 65 72 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67  er = sqlite3Trig
27794 67 65 72 73 45 78 69 73 74 28 70 50 61 72 73 65  gersExist(pParse
27795 2c 20 70 54 61 62 2c 20 54 4b 5f 49 4e 53 45 52  , pTab, TK_INSER
27796 54 2c 20 30 2c 20 26 74 6d 61 73 6b 29 3b 0a 20  T, 0, &tmask);. 
27797 20 69 73 56 69 65 77 20 3d 20 70 54 61 62 2d 3e   isView = pTab->
27798 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23 65 6c 73  pSelect!=0;.#els
27799 65 0a 23 20 64 65 66 69 6e 65 20 70 54 72 69 67  e.# define pTrig
2779a 67 65 72 20 30 0a 23 20 64 65 66 69 6e 65 20 74  ger 0.# define t
2779b 6d 61 73 6b 20 30 0a 23 20 64 65 66 69 6e 65 20  mask 0.# define 
2779c 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a  isView 0.#endif.
2779d 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
2779e 49 54 5f 56 49 45 57 0a 23 20 75 6e 64 65 66 20  IT_VIEW.# undef 
2779f 69 73 56 69 65 77 0a 23 20 64 65 66 69 6e 65 20  isView.# define 
277a0 69 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a  isView 0.#endif.
277a1 20 20 61 73 73 65 72 74 28 20 28 70 54 72 69 67    assert( (pTrig
277a2 67 65 72 20 26 26 20 74 6d 61 73 6b 29 20 7c 7c  ger && tmask) ||
277a3 20 28 70 54 72 69 67 67 65 72 3d 3d 30 20 26 26   (pTrigger==0 &&
277a4 20 74 6d 61 73 6b 3d 3d 30 29 20 29 3b 0a 0a 20   tmask==0) );.. 
277a5 20 2f 2a 20 49 66 20 70 54 61 62 20 69 73 20 72   /* If pTab is r
277a6 65 61 6c 6c 79 20 61 20 76 69 65 77 2c 20 6d 61  eally a view, ma
277a7 6b 65 20 73 75 72 65 20 69 74 20 68 61 73 20 62  ke sure it has b
277a8 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e  een initialized.
277a9 0a 20 20 2a 2a 20 56 69 65 77 47 65 74 43 6f 6c  .  ** ViewGetCol
277aa 75 6d 6e 4e 61 6d 65 73 28 29 20 69 73 20 61 20  umnNames() is a 
277ab 6e 6f 2d 6f 70 20 69 66 20 70 54 61 62 20 69 73  no-op if pTab is
277ac 20 6e 6f 74 20 61 20 76 69 65 77 20 28 6f 72 20   not a view (or 
277ad 76 69 72 74 75 61 6c 20 0a 20 20 2a 2a 20 6d 6f  virtual .  ** mo
277ae 64 75 6c 65 20 74 61 62 6c 65 29 2e 0a 20 20 2a  dule table)..  *
277af 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56  /.  if( sqlite3V
277b0 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65  iewGetColumnName
277b1 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 20  s(pParse, pTab) 
277b2 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65  ){.    goto inse
277b3 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  rt_cleanup;.  }.
277b4 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74 68 61  .  /* Ensure tha
277b5 74 3a 0a 20 20 2a 20 20 28 61 29 20 74 68 65 20  t:.  *  (a) the 
277b6 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 72 65 61  table is not rea
277b7 64 2d 6f 6e 6c 79 2c 20 0a 20 20 2a 20 20 28 62  d-only, .  *  (b
277b8 29 20 74 68 61 74 20 69 66 20 69 74 20 69 73 20  ) that if it is 
277b9 61 20 76 69 65 77 20 74 68 65 6e 20 4f 4e 20 49  a view then ON I
277ba 4e 53 45 52 54 20 74 72 69 67 67 65 72 73 20 65  NSERT triggers e
277bb 78 69 73 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20  xist.  */.  if( 
277bc 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c  sqlite3IsReadOnl
277bd 79 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  y(pParse, pTab, 
277be 74 6d 61 73 6b 29 20 29 7b 0a 20 20 20 20 67 6f  tmask) ){.    go
277bf 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
277c0 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c  p;.  }..  /* All
277c1 6f 63 61 74 65 20 61 20 56 44 42 45 0a 20 20 2a  ocate a VDBE.  *
277c2 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  /.  v = sqlite3G
277c3 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
277c4 20 20 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74    if( v==0 ) got
277c5 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70  o insert_cleanup
277c6 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
277c7 6e 65 73 74 65 64 3d 3d 30 20 29 20 73 71 6c 69  nested==0 ) sqli
277c8 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e  te3VdbeCountChan
277c9 67 65 73 28 76 29 3b 0a 20 20 73 71 6c 69 74 65  ges(v);.  sqlite
277ca 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
277cb 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 53 65  tion(pParse, pSe
277cc 6c 65 63 74 20 7c 7c 20 70 54 72 69 67 67 65 72  lect || pTrigger
277cd 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66  , iDb);..#ifndef
277ce 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 45   SQLITE_OMIT_XFE
277cf 52 5f 4f 50 54 0a 20 20 2f 2a 20 49 66 20 74 68  R_OPT.  /* If th
277d0 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f  e statement is o
277d1 66 20 74 68 65 20 66 6f 72 6d 0a 20 20 2a 2a 0a  f the form.  **.
277d2 20 20 2a 2a 20 20 20 20 20 20 20 49 4e 53 45 52    **       INSER
277d3 54 20 49 4e 54 4f 20 3c 74 61 62 6c 65 31 3e 20  T INTO <table1> 
277d4 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 3c 74  SELECT * FROM <t
277d5 61 62 6c 65 32 3e 3b 0a 20 20 2a 2a 0a 20 20 2a  able2>;.  **.  *
277d6 2a 20 54 68 65 6e 20 73 70 65 63 69 61 6c 20 6f  * Then special o
277d7 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 63 61 6e  ptimizations can
277d8 20 62 65 20 61 70 70 6c 69 65 64 20 74 68 61 74   be applied that
277d9 20 6d 61 6b 65 20 74 68 65 20 74 72 61 6e 73 66   make the transf
277da 65 72 0a 20 20 2a 2a 20 76 65 72 79 20 66 61 73  er.  ** very fas
277db 74 20 61 6e 64 20 77 68 69 63 68 20 72 65 64 75  t and which redu
277dc 63 65 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e  ce fragmentation
277dd 20 6f 66 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a   of indices..  *
277de 2a 0a 20 20 2a 2a 20 54 68 69 73 20 69 73 20 74  *.  ** This is t
277df 68 65 20 32 6e 64 20 74 65 6d 70 6c 61 74 65 2e  he 2nd template.
277e0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c  .  */.  if( pCol
277e1 75 6d 6e 3d 3d 30 20 26 26 20 78 66 65 72 4f 70  umn==0 && xferOp
277e2 74 69 6d 69 7a 61 74 69 6f 6e 28 70 50 61 72 73  timization(pPars
277e3 65 2c 20 70 54 61 62 2c 20 70 53 65 6c 65 63 74  e, pTab, pSelect
277e4 2c 20 6f 6e 45 72 72 6f 72 2c 20 69 44 62 29 20  , onError, iDb) 
277e5 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  ){.    assert( !
277e6 70 54 72 69 67 67 65 72 20 29 3b 0a 20 20 20 20  pTrigger );.    
277e7 61 73 73 65 72 74 28 20 70 4c 69 73 74 3d 3d 30  assert( pList==0
277e8 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73   );.    goto ins
277e9 65 72 74 5f 65 6e 64 3b 0a 20 20 7d 0a 23 65 6e  ert_end;.  }.#en
277ea 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
277eb 49 54 5f 58 46 45 52 5f 4f 50 54 20 2a 2f 0a 0a  IT_XFER_OPT */..
277ec 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
277ed 61 6e 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  an AUTOINCREMENT
277ee 20 74 61 62 6c 65 2c 20 6c 6f 6f 6b 20 75 70 20   table, look up 
277ef 74 68 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d  the sequence num
277f0 62 65 72 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20  ber in the.  ** 
277f1 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20  sqlite_sequence 
277f2 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 20  table and store 
277f3 69 74 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c  it in memory cel
277f4 6c 20 72 65 67 41 75 74 6f 69 6e 63 2e 0a 20 20  l regAutoinc..  
277f5 2a 2f 0a 20 20 72 65 67 41 75 74 6f 69 6e 63 20  */.  regAutoinc 
277f6 3d 20 61 75 74 6f 49 6e 63 42 65 67 69 6e 28 70  = autoIncBegin(p
277f7 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62  Parse, iDb, pTab
277f8 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20  );..  /* Figure 
277f9 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 63 6f 6c  out how many col
277fa 75 6d 6e 73 20 6f 66 20 64 61 74 61 20 61 72 65  umns of data are
277fb 20 73 75 70 70 6c 69 65 64 2e 20 20 49 66 20 74   supplied.  If t
277fc 68 65 20 64 61 74 61 0a 20 20 2a 2a 20 69 73 20  he data.  ** is 
277fd 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 61 20 53 45  coming from a SE
277fe 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2c 20  LECT statement, 
277ff 74 68 65 6e 20 67 65 6e 65 72 61 74 65 20 61 20  then generate a 
27800 63 6f 2d 72 6f 75 74 69 6e 65 20 74 68 61 74 0a  co-routine that.
27801 20 20 2a 2a 20 70 72 6f 64 75 63 65 73 20 61 20    ** produces a 
27802 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74 68  single row of th
27803 65 20 53 45 4c 45 43 54 20 6f 6e 20 65 61 63 68  e SELECT on each
27804 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20 20 54 68   invocation.  Th
27805 65 0a 20 20 2a 2a 20 63 6f 2d 72 6f 75 74 69 6e  e.  ** co-routin
27806 65 20 69 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20  e is the common 
27807 68 65 61 64 65 72 20 74 6f 20 74 68 65 20 33 72  header to the 3r
27808 64 20 61 6e 64 20 34 74 68 20 74 65 6d 70 6c 61  d and 4th templa
27809 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  tes..  */.  if( 
2780a 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 2f  pSelect ){.    /
2780b 2a 20 44 61 74 61 20 69 73 20 63 6f 6d 69 6e 67  * Data is coming
2780c 20 66 72 6f 6d 20 61 20 53 45 4c 45 43 54 2e 20   from a SELECT. 
2780d 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
2780e 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 61 74  o implement that
2780f 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 61   SELECT.    ** a
27810 73 20 61 20 63 6f 2d 72 6f 75 74 69 6e 65 2e 20  s a co-routine. 
27811 20 54 68 65 20 63 6f 64 65 20 69 73 20 63 6f 6d   The code is com
27812 6d 6f 6e 20 74 6f 20 62 6f 74 68 20 74 68 65 20  mon to both the 
27813 33 72 64 20 61 6e 64 20 34 74 68 0a 20 20 20 20  3rd and 4th.    
27814 2a 2a 20 74 65 6d 70 6c 61 74 65 73 3a 0a 20 20  ** templates:.  
27815 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
27816 20 20 20 20 45 4f 46 20 3c 2d 20 30 0a 20 20 20      EOF <- 0.   
27817 20 2a 2a 20 20 20 20 20 20 20 20 20 58 20 3c 2d   **         X <-
27818 20 41 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20   A.    **       
27819 20 20 67 6f 74 6f 20 42 0a 20 20 20 20 2a 2a 20    goto B.    ** 
2781a 20 20 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f       A: setup fo
2781b 72 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20  r the SELECT.   
2781c 20 2a 2a 20 20 20 20 20 20 20 20 20 6c 6f 6f 70   **         loop
2781d 20 6f 76 65 72 20 74 68 65 20 74 61 62 6c 65 73   over the tables
2781e 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 0a 20   in the SELECT. 
2781f 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
27820 6c 6f 61 64 20 76 61 6c 75 65 20 69 6e 74 6f 20  load value into 
27821 72 65 67 69 73 74 65 72 20 52 2e 2e 52 2b 6e 0a  register R..R+n.
27822 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
27823 20 79 69 65 6c 64 20 58 0a 20 20 20 20 2a 2a 20   yield X.    ** 
27824 20 20 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70          end loop
27825 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
27826 63 6c 65 61 6e 75 70 20 61 66 74 65 72 20 74 68  cleanup after th
27827 65 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20  e SELECT.    ** 
27828 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 31          EOF <- 1
27829 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2782a 79 69 65 6c 64 20 58 0a 20 20 20 20 2a 2a 20 20  yield X.    **  
2782b 20 20 20 20 20 20 20 68 61 6c 74 2d 65 72 72 6f         halt-erro
2782c 72 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  r.    **.    ** 
2782d 4f 6e 20 65 61 63 68 20 69 6e 76 6f 63 61 74 69  On each invocati
2782e 6f 6e 20 6f 66 20 74 68 65 20 63 6f 2d 72 6f 75  on of the co-rou
2782f 74 69 6e 65 2c 20 69 74 20 70 75 74 73 20 61 20  tine, it puts a 
27830 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74 68  single row of th
27831 65 0a 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 20  e.    ** SELECT 
27832 72 65 73 75 6c 74 20 69 6e 74 6f 20 72 65 67 69  result into regi
27833 73 74 65 72 73 20 64 65 73 74 2e 69 4d 65 6d 2e  sters dest.iMem.
27834 2e 2e 64 65 73 74 2e 69 4d 65 6d 2b 64 65 73 74  ..dest.iMem+dest
27835 2e 6e 4d 65 6d 2d 31 2e 0a 20 20 20 20 2a 2a 20  .nMem-1..    ** 
27836 28 54 68 65 73 65 20 6f 75 74 70 75 74 20 72 65  (These output re
27837 67 69 73 74 65 72 73 20 61 72 65 20 61 6c 6c 6f  gisters are allo
27838 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33  cated by sqlite3
27839 53 65 6c 65 63 74 28 29 2e 29 20 20 57 68 65 6e  Select().)  When
2783a 0a 20 20 20 20 2a 2a 20 74 68 65 20 53 45 4c 45  .    ** the SELE
2783b 43 54 20 63 6f 6d 70 6c 65 74 65 73 2c 20 69 74  CT completes, it
2783c 20 73 65 74 73 20 74 68 65 20 45 4f 46 20 66 6c   sets the EOF fl
2783d 61 67 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67  ag stored in reg
2783e 45 6f 66 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  Eof..    */.    
2783f 69 6e 74 20 72 63 2c 20 6a 31 3b 0a 0a 20 20 20  int rc, j1;..   
27840 20 72 65 67 45 6f 66 20 3d 20 2b 2b 70 50 61 72   regEof = ++pPar
27841 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71  se->nMem;.    sq
27842 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27843 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
27844 2c 20 72 65 67 45 6f 66 29 3b 20 20 20 20 20 20  , regEof);      
27845 2f 2a 20 45 4f 46 20 3c 2d 20 30 20 2a 2f 0a 20  /* EOF <- 0 */. 
27846 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
27847 76 2c 20 22 53 45 4c 45 43 54 20 65 6f 66 20 66  v, "SELECT eof f
27848 6c 61 67 22 29 29 3b 0a 20 20 20 20 73 71 6c 69  lag"));.    sqli
27849 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69  te3SelectDestIni
2784a 74 28 26 64 65 73 74 2c 20 53 52 54 5f 43 6f 72  t(&dest, SRT_Cor
2784b 6f 75 74 69 6e 65 2c 20 2b 2b 70 50 61 72 73 65  outine, ++pParse
2784c 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 61 64 64  ->nMem);.    add
2784d 72 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65  rSelect = sqlite
2784e 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
2784f 28 76 29 2b 32 3b 0a 20 20 20 20 73 71 6c 69 74  (v)+2;.    sqlit
27850 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
27851 4f 50 5f 49 6e 74 65 67 65 72 2c 20 61 64 64 72  OP_Integer, addr
27852 53 65 6c 65 63 74 2d 31 2c 20 64 65 73 74 2e 69  Select-1, dest.i
27853 50 61 72 6d 29 3b 0a 20 20 20 20 6a 31 20 3d 20  Parm);.    j1 = 
27854 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27855 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
27856 20 30 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d   0);.    VdbeCom
27857 6d 65 6e 74 28 28 76 2c 20 22 4a 75 6d 70 20 6f  ment((v, "Jump o
27858 76 65 72 20 53 45 4c 45 43 54 20 63 6f 72 6f 75  ver SELECT corou
27859 74 69 6e 65 22 29 29 3b 0a 0a 20 20 20 20 2f 2a  tine"));..    /*
2785a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 65 78 70   Resolve the exp
2785b 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20  ressions in the 
2785c 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2785d 20 61 6e 64 20 65 78 65 63 75 74 65 20 69 74 2e   and execute it.
2785e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   */.    rc = sql
2785f 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73  ite3Select(pPars
27860 65 2c 20 70 53 65 6c 65 63 74 2c 20 26 64 65 73  e, pSelect, &des
27861 74 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  t);.    assert( 
27862 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20  pParse->nErr==0 
27863 7c 7c 20 72 63 20 29 3b 0a 20 20 20 20 69 66 28  || rc );.    if(
27864 20 72 63 20 7c 7c 20 4e 45 56 45 52 28 70 50 61   rc || NEVER(pPa
27865 72 73 65 2d 3e 6e 45 72 72 29 20 7c 7c 20 64 62  rse->nErr) || db
27866 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
27867 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 73  {.      goto ins
27868 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  ert_cleanup;.   
27869 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
2786a 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2786b 6e 74 65 67 65 72 2c 20 31 2c 20 72 65 67 45 6f  nteger, 1, regEo
2786c 66 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 45  f);         /* E
2786d 4f 46 20 3c 2d 20 31 20 2a 2f 0a 20 20 20 20 73  OF <- 1 */.    s
2786e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2786f 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 64 65  (v, OP_Yield, de
27870 73 74 2e 69 50 61 72 6d 29 3b 20 20 20 2f 2a 20  st.iParm);   /* 
27871 79 69 65 6c 64 20 58 20 2a 2f 0a 20 20 20 20 73  yield X */.    s
27872 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
27873 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c  (v, OP_Halt, SQL
27874 49 54 45 5f 49 4e 54 45 52 4e 41 4c 2c 20 4f 45  ITE_INTERNAL, OE
27875 5f 41 62 6f 72 74 29 3b 0a 20 20 20 20 56 64 62  _Abort);.    Vdb
27876 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 45 6e  eComment((v, "En
27877 64 20 6f 66 20 53 45 4c 45 43 54 20 63 6f 72 6f  d of SELECT coro
27878 75 74 69 6e 65 22 29 29 3b 0a 20 20 20 20 73 71  utine"));.    sq
27879 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
2787a 65 28 76 2c 20 6a 31 29 3b 20 20 20 20 20 20 20  e(v, j1);       
2787b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2787c 20 20 20 2f 2a 20 6c 61 62 65 6c 20 42 3a 20 2a     /* label B: *
2787d 2f 0a 0a 20 20 20 20 72 65 67 46 72 6f 6d 53 65  /..    regFromSe
2787e 6c 65 63 74 20 3d 20 64 65 73 74 2e 69 4d 65 6d  lect = dest.iMem
2787f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 53  ;.    assert( pS
27880 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 20 29 3b  elect->pEList );
27881 0a 20 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70  .    nColumn = p
27882 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e  Select->pEList->
27883 6e 45 78 70 72 3b 0a 20 20 20 20 61 73 73 65 72  nExpr;.    asser
27884 74 28 20 64 65 73 74 2e 6e 4d 65 6d 3d 3d 6e 43  t( dest.nMem==nC
27885 6f 6c 75 6d 6e 20 29 3b 0a 0a 20 20 20 20 2f 2a  olumn );..    /*
27886 20 53 65 74 20 75 73 65 54 65 6d 70 54 61 62 6c   Set useTempTabl
27887 65 20 74 6f 20 54 52 55 45 20 69 66 20 74 68 65  e to TRUE if the
27888 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 53   result of the S
27889 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 0a  ELECT statement.
2788a 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65      ** should be
2788b 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20   written into a 
2788c 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
2788d 28 74 65 6d 70 6c 61 74 65 20 34 29 2e 20 20 53  (template 4).  S
2788e 65 74 20 74 6f 0a 20 20 20 20 2a 2a 20 46 41 4c  et to.    ** FAL
2788f 53 45 20 69 66 20 65 61 63 68 2a 20 72 6f 77 20  SE if each* row 
27890 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 63 61  of the SELECT ca
27891 6e 20 62 65 20 77 72 69 74 74 65 6e 20 64 69 72  n be written dir
27892 65 63 74 6c 79 20 69 6e 74 6f 0a 20 20 20 20 2a  ectly into.    *
27893 2a 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  * the destinatio
27894 6e 20 74 61 62 6c 65 20 28 74 65 6d 70 6c 61 74  n table (templat
27895 65 20 33 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  e 3)..    **.   
27896 20 2a 2a 20 41 20 74 65 6d 70 20 74 61 62 6c 65   ** A temp table
27897 20 6d 75 73 74 20 62 65 20 75 73 65 64 20 69 66   must be used if
27898 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
27899 20 75 70 64 61 74 65 64 20 69 73 20 61 6c 73 6f   updated is also
2789a 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 6f 66 20 74   one.    ** of t
2789b 68 65 20 74 61 62 6c 65 73 20 62 65 69 6e 67 20  he tables being 
2789c 72 65 61 64 20 62 79 20 74 68 65 20 53 45 4c 45  read by the SELE
2789d 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41  CT statement.  A
2789e 6c 73 6f 20 75 73 65 20 61 20 0a 20 20 20 20 2a  lso use a .    *
2789f 2a 20 74 65 6d 70 20 74 61 62 6c 65 20 69 6e 20  * temp table in 
278a0 74 68 65 20 63 61 73 65 20 6f 66 20 72 6f 77 20  the case of row 
278a1 74 72 69 67 67 65 72 73 2e 0a 20 20 20 20 2a 2f  triggers..    */
278a2 0a 20 20 20 20 69 66 28 20 70 54 72 69 67 67 65  .    if( pTrigge
278a3 72 20 7c 7c 20 72 65 61 64 73 54 61 62 6c 65 28  r || readsTable(
278a4 70 50 61 72 73 65 2c 20 61 64 64 72 53 65 6c 65  pParse, addrSele
278a5 63 74 2c 20 69 44 62 2c 20 70 54 61 62 29 20 29  ct, iDb, pTab) )
278a6 7b 0a 20 20 20 20 20 20 75 73 65 54 65 6d 70 54  {.      useTempT
278a7 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  able = 1;.    }.
278a8 0a 20 20 20 20 69 66 28 20 75 73 65 54 65 6d 70  .    if( useTemp
278a9 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 2f  Table ){.      /
278aa 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 6f 72  * Invoke the cor
278ab 6f 75 74 69 6e 65 20 74 6f 20 65 78 74 72 61 63  outine to extrac
278ac 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72  t information fr
278ad 6f 6d 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20  om the SELECT.  
278ae 20 20 20 20 2a 2a 20 61 6e 64 20 61 64 64 20 69      ** and add i
278af 74 20 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74  t to a transient
278b0 20 74 61 62 6c 65 20 73 72 63 54 61 62 2e 20 20   table srcTab.  
278b1 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74  The code generat
278b2 65 64 0a 20 20 20 20 20 20 2a 2a 20 68 65 72 65  ed.      ** here
278b3 20 69 73 20 66 72 6f 6d 20 74 68 65 20 34 74 68   is from the 4th
278b4 20 74 65 6d 70 6c 61 74 65 3a 0a 20 20 20 20 20   template:.     
278b5 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20   **.      **    
278b6 20 20 42 3a 20 6f 70 65 6e 20 74 65 6d 70 20 74    B: open temp t
278b7 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 20 20  able.      **   
278b8 20 20 20 4c 3a 20 79 69 65 6c 64 20 58 0a 20 20     L: yield X.  
278b9 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 69      **         i
278ba 66 20 45 4f 46 20 67 6f 74 6f 20 4d 0a 20 20 20  f EOF goto M.   
278bb 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 69 6e     **         in
278bc 73 65 72 74 20 72 6f 77 20 66 72 6f 6d 20 52 2e  sert row from R.
278bd 2e 52 2b 6e 20 69 6e 74 6f 20 74 65 6d 70 20 74  .R+n into temp t
278be 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 20 20  able.      **   
278bf 20 20 20 20 20 20 67 6f 74 6f 20 4c 0a 20 20 20        goto L.   
278c0 20 20 20 2a 2a 20 20 20 20 20 20 4d 3a 20 2e 2e     **      M: ..
278c1 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
278c2 20 69 6e 74 20 72 65 67 52 65 63 3b 20 20 20 20   int regRec;    
278c3 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
278c4 72 20 74 6f 20 68 6f 6c 64 20 70 61 63 6b 65 64  r to hold packed
278c5 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20   record */.     
278c6 20 69 6e 74 20 72 65 67 54 65 6d 70 52 6f 77 69   int regTempRowi
278c7 64 3b 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65  d;    /* Registe
278c8 72 20 74 6f 20 68 6f 6c 64 20 74 65 6d 70 20 74  r to hold temp t
278c9 61 62 6c 65 20 52 4f 57 49 44 20 2a 2f 0a 20 20  able ROWID */.  
278ca 20 20 20 20 69 6e 74 20 61 64 64 72 54 6f 70 3b      int addrTop;
278cb 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62 65           /* Labe
278cc 6c 20 22 4c 22 20 2a 2f 0a 20 20 20 20 20 20 69  l "L" */.      i
278cd 6e 74 20 61 64 64 72 49 66 3b 20 20 20 20 20 20  nt addrIf;      
278ce 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
278cf 66 20 6a 75 6d 70 20 74 6f 20 4d 20 2a 2f 0a 0a  f jump to M */..
278d0 20 20 20 20 20 20 73 72 63 54 61 62 20 3d 20 70        srcTab = p
278d1 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
278d2 20 20 20 20 20 72 65 67 52 65 63 20 3d 20 73 71       regRec = sq
278d3 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
278d4 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 72  pParse);.      r
278d5 65 67 54 65 6d 70 52 6f 77 69 64 20 3d 20 73 71  egTempRowid = sq
278d6 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
278d7 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73  pParse);.      s
278d8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
278d9 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d  (v, OP_OpenEphem
278da 65 72 61 6c 2c 20 73 72 63 54 61 62 2c 20 6e 43  eral, srcTab, nC
278db 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 61 64  olumn);.      ad
278dc 64 72 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56  drTop = sqlite3V
278dd 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
278de 59 69 65 6c 64 2c 20 64 65 73 74 2e 69 50 61 72  Yield, dest.iPar
278df 6d 29 3b 0a 20 20 20 20 20 20 61 64 64 72 49 66  m);.      addrIf
278e0 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
278e1 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 72  dOp1(v, OP_If, r
278e2 65 67 45 6f 66 29 3b 0a 20 20 20 20 20 20 73 71  egEof);.      sq
278e3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
278e4 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
278e5 2c 20 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 2c  , regFromSelect,
278e6 20 6e 43 6f 6c 75 6d 6e 2c 20 72 65 67 52 65 63   nColumn, regRec
278e7 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
278e8 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
278e9 5f 4e 65 77 52 6f 77 69 64 2c 20 73 72 63 54 61  _NewRowid, srcTa
278ea 62 2c 20 72 65 67 54 65 6d 70 52 6f 77 69 64 29  b, regTempRowid)
278eb 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
278ec 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
278ed 49 6e 73 65 72 74 2c 20 73 72 63 54 61 62 2c 20  Insert, srcTab, 
278ee 72 65 67 52 65 63 2c 20 72 65 67 54 65 6d 70 52  regRec, regTempR
278ef 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c  owid);.      sql
278f0 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
278f1 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64  , OP_Goto, 0, ad
278f2 64 72 54 6f 70 29 3b 0a 20 20 20 20 20 20 73 71  drTop);.      sq
278f3 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
278f4 65 28 76 2c 20 61 64 64 72 49 66 29 3b 0a 20 20  e(v, addrIf);.  
278f5 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
278f6 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
278f7 2c 20 72 65 67 52 65 63 29 3b 0a 20 20 20 20 20  , regRec);.     
278f8 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
278f9 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
278fa 65 67 54 65 6d 70 52 6f 77 69 64 29 3b 0a 20 20  egTempRowid);.  
278fb 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
278fc 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20   /* This is the 
278fd 63 61 73 65 20 69 66 20 74 68 65 20 64 61 74 61  case if the data
278fe 20 66 6f 72 20 74 68 65 20 49 4e 53 45 52 54 20   for the INSERT 
278ff 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 61  is coming from a
27900 20 56 41 4c 55 45 53 0a 20 20 20 20 2a 2a 20 63   VALUES.    ** c
27901 6c 61 75 73 65 0a 20 20 20 20 2a 2f 0a 20 20 20  lause.    */.   
27902 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43   NameContext sNC
27903 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e  ;.    memset(&sN
27904 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43  C, 0, sizeof(sNC
27905 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72  ));.    sNC.pPar
27906 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20  se = pParse;.   
27907 20 73 72 63 54 61 62 20 3d 20 2d 31 3b 0a 20 20   srcTab = -1;.  
27908 20 20 61 73 73 65 72 74 28 20 75 73 65 54 65 6d    assert( useTem
27909 70 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20  pTable==0 );.   
2790a 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c 69 73 74   nColumn = pList
2790b 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20   ? pList->nExpr 
2790c 3a 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  : 0;.    for(i=0
2790d 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b  ; i<nColumn; i++
2790e 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ){.      if( sql
2790f 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
27910 61 6d 65 73 28 26 73 4e 43 2c 20 70 4c 69 73 74  ames(&sNC, pList
27911 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b  ->a[i].pExpr) ){
27912 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 69 6e  .        goto in
27913 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
27914 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
27915 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  .  /* Make sure 
27916 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  the number of co
27917 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 73 6f 75  lumns in the sou
27918 72 63 65 20 64 61 74 61 20 6d 61 74 63 68 65 73  rce data matches
27919 20 74 68 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a   the number.  **
2791a 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62   of columns to b
2791b 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  e inserted into 
2791c 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  the table..  */.
2791d 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
2791e 70 54 61 62 29 20 29 7b 0a 20 20 20 20 66 6f 72  pTab) ){.    for
2791f 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43  (i=0; i<pTab->nC
27920 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
27921 6e 48 69 64 64 65 6e 20 2b 3d 20 28 49 73 48 69  nHidden += (IsHi
27922 64 64 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62  ddenColumn(&pTab
27923 2d 3e 61 43 6f 6c 5b 69 5d 29 20 3f 20 31 20 3a  ->aCol[i]) ? 1 :
27924 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   0);.    }.  }. 
27925 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20   if( pColumn==0 
27926 26 26 20 6e 43 6f 6c 75 6d 6e 20 26 26 20 6e 43  && nColumn && nC
27927 6f 6c 75 6d 6e 21 3d 28 70 54 61 62 2d 3e 6e 43  olumn!=(pTab->nC
27928 6f 6c 2d 6e 48 69 64 64 65 6e 29 20 29 7b 0a 20  ol-nHidden) ){. 
27929 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2792a 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  sg(pParse, .    
2792b 20 20 20 22 74 61 62 6c 65 20 25 53 20 68 61 73     "table %S has
2792c 20 25 64 20 63 6f 6c 75 6d 6e 73 20 62 75 74 20   %d columns but 
2792d 25 64 20 76 61 6c 75 65 73 20 77 65 72 65 20 73  %d values were s
2792e 75 70 70 6c 69 65 64 22 2c 0a 20 20 20 20 20 20  upplied",.      
2792f 20 70 54 61 62 4c 69 73 74 2c 20 30 2c 20 70 54   pTabList, 0, pT
27930 61 62 2d 3e 6e 43 6f 6c 2d 6e 48 69 64 64 65 6e  ab->nCol-nHidden
27931 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  , nColumn);.    
27932 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61  goto insert_clea
27933 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  nup;.  }.  if( p
27934 43 6f 6c 75 6d 6e 21 3d 30 20 26 26 20 6e 43 6f  Column!=0 && nCo
27935 6c 75 6d 6e 21 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e  lumn!=pColumn->n
27936 49 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  Id ){.    sqlite
27937 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
27938 2c 20 22 25 64 20 76 61 6c 75 65 73 20 66 6f 72  , "%d values for
27939 20 25 64 20 63 6f 6c 75 6d 6e 73 22 2c 20 6e 43   %d columns", nC
2793a 6f 6c 75 6d 6e 2c 20 70 43 6f 6c 75 6d 6e 2d 3e  olumn, pColumn->
2793b 6e 49 64 29 3b 0a 20 20 20 20 67 6f 74 6f 20 69  nId);.    goto i
2793c 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20  nsert_cleanup;. 
2793d 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
2793e 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74  INSERT statement
2793f 20 69 6e 63 6c 75 64 65 64 20 61 6e 20 49 44 4c   included an IDL
27940 49 53 54 20 74 65 72 6d 2c 20 74 68 65 6e 20 6d  IST term, then m
27941 61 6b 65 20 73 75 72 65 0a 20 20 2a 2a 20 61 6c  ake sure.  ** al
27942 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68  l elements of th
27943 65 20 49 44 4c 49 53 54 20 72 65 61 6c 6c 79 20  e IDLIST really 
27944 61 72 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74  are columns of t
27945 68 65 20 74 61 62 6c 65 20 61 6e 64 20 0a 20 20  he table and .  
27946 2a 2a 20 72 65 6d 65 6d 62 65 72 20 74 68 65 20  ** remember the 
27947 63 6f 6c 75 6d 6e 20 69 6e 64 69 63 65 73 2e 0a  column indices..
27948 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65    **.  ** If the
27949 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 49 4e   table has an IN
2794a 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
2794b 59 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 74 68 61  Y column and tha
2794c 74 20 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 69 73  t column.  ** is
2794d 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20 49 44   named in the ID
2794e 4c 49 53 54 2c 20 74 68 65 6e 20 72 65 63 6f 72  LIST, then recor
2794f 64 20 69 6e 20 74 68 65 20 6b 65 79 43 6f 6c 75  d in the keyColu
27950 6d 6e 20 76 61 72 69 61 62 6c 65 0a 20 20 2a 2a  mn variable.  **
27951 20 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f 20   the index into 
27952 49 44 4c 49 53 54 20 6f 66 20 74 68 65 20 70 72  IDLIST of the pr
27953 69 6d 61 72 79 20 6b 65 79 20 63 6f 6c 75 6d 6e  imary key column
27954 2e 20 20 6b 65 79 43 6f 6c 75 6d 6e 20 69 73 0a  .  keyColumn is.
27955 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 6f    ** the index o
27956 66 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65  f the primary ke
27957 79 20 61 73 20 69 74 20 61 70 70 65 61 72 73 20  y as it appears 
27958 69 6e 20 49 44 4c 49 53 54 2c 20 6e 6f 74 20 61  in IDLIST, not a
27959 73 0a 20 20 2a 2a 20 69 73 20 61 70 70 65 61 72  s.  ** is appear
2795a 73 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61  s in the origina
2795b 6c 20 74 61 62 6c 65 2e 20 20 28 54 68 65 20 69  l table.  (The i
2795c 6e 64 65 78 20 6f 66 20 74 68 65 20 70 72 69 6d  ndex of the prim
2795d 61 72 79 0a 20 20 2a 2a 20 6b 65 79 20 69 6e 20  ary.  ** key in 
2795e 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 61 62  the original tab
2795f 6c 65 20 69 73 20 70 54 61 62 2d 3e 69 50 4b 65  le is pTab->iPKe
27960 79 2e 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  y.).  */.  if( p
27961 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 66 6f  Column ){.    fo
27962 72 28 69 3d 30 3b 20 69 3c 70 43 6f 6c 75 6d 6e  r(i=0; i<pColumn
27963 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nId; i++){.   
27964 20 20 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d     pColumn->a[i]
27965 2e 69 64 78 20 3d 20 2d 31 3b 0a 20 20 20 20 7d  .idx = -1;.    }
27966 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
27967 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 69 2b  pColumn->nId; i+
27968 2b 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d  +){.      for(j=
27969 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  0; j<pTab->nCol;
2796a 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69   j++){.        i
2796b 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
2796c 70 28 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e  p(pColumn->a[i].
2796d 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43 6f  zName, pTab->aCo
2796e 6c 5b 6a 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29  l[j].zName)==0 )
2796f 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c  {.          pCol
27970 75 6d 6e 2d 3e 61 5b 69 5d 2e 69 64 78 20 3d 20  umn->a[i].idx = 
27971 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  j;.          if(
27972 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20   j==pTab->iPKey 
27973 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6b  ){.            k
27974 65 79 43 6f 6c 75 6d 6e 20 3d 20 69 3b 0a 20 20  eyColumn = i;.  
27975 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
27976 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
27977 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
27978 20 20 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e     if( j>=pTab->
27979 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  nCol ){.        
2797a 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f 77  if( sqlite3IsRow
2797b 69 64 28 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d  id(pColumn->a[i]
2797c 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20  .zName) ){.     
2797d 20 20 20 20 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d       keyColumn =
2797e 20 69 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73   i;.        }els
2797f 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  e{.          sql
27980 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
27981 72 73 65 2c 20 22 74 61 62 6c 65 20 25 53 20 68  rse, "table %S h
27982 61 73 20 6e 6f 20 63 6f 6c 75 6d 6e 20 6e 61 6d  as no column nam
27983 65 64 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20  ed %s",.        
27984 20 20 20 20 20 20 70 54 61 62 4c 69 73 74 2c 20        pTabList, 
27985 30 2c 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d  0, pColumn->a[i]
27986 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20  .zName);.       
27987 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b     pParse->nErr+
27988 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  +;.          got
27989 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70  o insert_cleanup
2798a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2798b 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
2798c 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
2798d 6e 6f 20 49 44 4c 49 53 54 20 74 65 72 6d 20 62  no IDLIST term b
2798e 75 74 20 74 68 65 20 74 61 62 6c 65 20 68 61 73  ut the table has
2798f 20 61 6e 20 69 6e 74 65 67 65 72 20 70 72 69 6d   an integer prim
27990 61 72 79 0a 20 20 2a 2a 20 6b 65 79 2c 20 74 68  ary.  ** key, th
27991 65 20 73 65 74 20 74 68 65 20 6b 65 79 43 6f 6c  e set the keyCol
27992 75 6d 6e 20 76 61 72 69 61 62 6c 65 20 74 6f 20  umn variable to 
27993 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20  the primary key 
27994 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 0a 20 20 2a  column index.  *
27995 2a 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61  * in the origina
27996 6c 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69  l table definiti
27997 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  on..  */.  if( p
27998 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 6e 43 6f  Column==0 && nCo
27999 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20 6b 65  lumn>0 ){.    ke
2799a 79 43 6f 6c 75 6d 6e 20 3d 20 70 54 61 62 2d 3e  yColumn = pTab->
2799b 69 50 4b 65 79 3b 0a 20 20 7d 0a 20 20 20 20 0a  iPKey;.  }.    .
2799c 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
2799d 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 72 6f 77  the count of row
2799e 73 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64  s to be inserted
2799f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  .  */.  if( db->
279a0 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43  flags & SQLITE_C
279a1 6f 75 6e 74 52 6f 77 73 20 29 7b 0a 20 20 20 20  ountRows ){.    
279a2 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20 2b 2b  regRowCount = ++
279a3 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
279a4 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
279a5 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
279a6 72 2c 20 30 2c 20 72 65 67 52 6f 77 43 6f 75 6e  r, 0, regRowCoun
279a7 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  t);.  }..  /* If
279a8 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20 76   this is not a v
279a9 69 65 77 2c 20 6f 70 65 6e 20 74 68 65 20 74 61  iew, open the ta
279aa 62 6c 65 20 61 6e 64 20 61 6e 64 20 61 6c 6c 20  ble and and all 
279ab 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 66 28  indices */.  if(
279ac 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20   !isView ){.    
279ad 69 6e 74 20 6e 49 64 78 3b 0a 0a 20 20 20 20 62  int nIdx;..    b
279ae 61 73 65 43 75 72 20 3d 20 70 50 61 72 73 65 2d  aseCur = pParse-
279af 3e 6e 54 61 62 3b 0a 20 20 20 20 6e 49 64 78 20  >nTab;.    nIdx 
279b0 3d 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62  = sqlite3OpenTab
279b1 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 70 50 61  leAndIndices(pPa
279b2 72 73 65 2c 20 70 54 61 62 2c 20 62 61 73 65 43  rse, pTab, baseC
279b3 75 72 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65  ur, OP_OpenWrite
279b4 29 3b 0a 20 20 20 20 61 52 65 67 49 64 78 20 3d  );.    aRegIdx =
279b5 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
279b6 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69  Raw(db, sizeof(i
279b7 6e 74 29 2a 28 6e 49 64 78 2b 31 29 29 3b 0a 20  nt)*(nIdx+1));. 
279b8 20 20 20 69 66 28 20 61 52 65 67 49 64 78 3d 3d     if( aRegIdx==
279b9 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  0 ){.      goto 
279ba 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
279bb 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d      }.    for(i=
279bc 30 3b 20 69 3c 6e 49 64 78 3b 20 69 2b 2b 29 7b  0; i<nIdx; i++){
279bd 0a 20 20 20 20 20 20 61 52 65 67 49 64 78 5b 69  .      aRegIdx[i
279be 5d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  ] = ++pParse->nM
279bf 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  em;.    }.  }.. 
279c0 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20   /* This is the 
279c1 74 6f 70 20 6f 66 20 74 68 65 20 6d 61 69 6e 20  top of the main 
279c2 69 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f 70 20 2a  insertion loop *
279c3 2f 0a 20 20 69 66 28 20 75 73 65 54 65 6d 70 54  /.  if( useTempT
279c4 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54  able ){.    /* T
279c5 68 69 73 20 62 6c 6f 63 6b 20 63 6f 64 65 73 20  his block codes 
279c6 74 68 65 20 74 6f 70 20 6f 66 20 6c 6f 6f 70 20  the top of loop 
279c7 6f 6e 6c 79 2e 20 20 54 68 65 20 63 6f 6d 70 6c  only.  The compl
279c8 65 74 65 20 6c 6f 6f 70 20 69 73 20 74 68 65 0a  ete loop is the.
279c9 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67      ** following
279ca 20 70 73 65 75 64 6f 63 6f 64 65 20 28 74 65 6d   pseudocode (tem
279cb 70 6c 61 74 65 20 34 29 3a 0a 20 20 20 20 2a 2a  plate 4):.    **
279cc 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
279cd 72 65 77 69 6e 64 20 74 65 6d 70 20 74 61 62 6c  rewind temp tabl
279ce 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 43 3a  e.    **      C:
279cf 20 6c 6f 6f 70 20 6f 76 65 72 20 72 6f 77 73 20   loop over rows 
279d0 6f 66 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  of intermediate 
279d1 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 20 20 20  table.    **    
279d2 20 20 20 20 20 20 20 74 72 61 6e 73 66 65 72 20         transfer 
279d3 76 61 6c 75 65 73 20 66 6f 72 6d 20 69 6e 74 65  values form inte
279d4 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65 20 69  rmediate table i
279d5 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 20 20 20 20  nto <table>.    
279d6 2a 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20 6c  **         end l
279d7 6f 6f 70 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  oop.    **      
279d8 44 3a 20 2e 2e 2e 0a 20 20 20 20 2a 2f 0a 20 20  D: ....    */.  
279d9 20 20 61 64 64 72 49 6e 73 54 6f 70 20 3d 20 73    addrInsTop = s
279da 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
279db 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 73  (v, OP_Rewind, s
279dc 72 63 54 61 62 29 3b 0a 20 20 20 20 61 64 64 72  rcTab);.    addr
279dd 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64  Cont = sqlite3Vd
279de 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
279df 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 53  ;.  }else if( pS
279e0 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 2f 2a 20  elect ){.    /* 
279e1 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 64 65 73  This block codes
279e2 20 74 68 65 20 74 6f 70 20 6f 66 20 6c 6f 6f 70   the top of loop
279e3 20 6f 6e 6c 79 2e 20 20 54 68 65 20 63 6f 6d 70   only.  The comp
279e4 6c 65 74 65 20 6c 6f 6f 70 20 69 73 20 74 68 65  lete loop is the
279e5 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e  .    ** followin
279e6 67 20 70 73 65 75 64 6f 63 6f 64 65 20 28 74 65  g pseudocode (te
279e7 6d 70 6c 61 74 65 20 33 29 3a 0a 20 20 20 20 2a  mplate 3):.    *
279e8 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 43 3a  *.    **      C:
279e9 20 79 69 65 6c 64 20 58 0a 20 20 20 20 2a 2a 20   yield X.    ** 
279ea 20 20 20 20 20 20 20 20 69 66 20 45 4f 46 20 67          if EOF g
279eb 6f 74 6f 20 44 0a 20 20 20 20 2a 2a 20 20 20 20  oto D.    **    
279ec 20 20 20 20 20 69 6e 73 65 72 74 20 74 68 65 20       insert the 
279ed 73 65 6c 65 63 74 20 72 65 73 75 6c 74 20 69 6e  select result in
279ee 74 6f 20 3c 74 61 62 6c 65 3e 20 66 72 6f 6d 20  to <table> from 
279ef 52 2e 2e 52 2b 6e 0a 20 20 20 20 2a 2a 20 20 20  R..R+n.    **   
279f0 20 20 20 20 20 20 67 6f 74 6f 20 43 0a 20 20 20        goto C.   
279f1 20 2a 2a 20 20 20 20 20 20 44 3a 20 2e 2e 2e 0a   **      D: ....
279f2 20 20 20 20 2a 2f 0a 20 20 20 20 61 64 64 72 43      */.    addrC
279f3 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ont = sqlite3Vdb
279f4 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69  eAddOp1(v, OP_Yi
279f5 65 6c 64 2c 20 64 65 73 74 2e 69 50 61 72 6d 29  eld, dest.iParm)
279f6 3b 0a 20 20 20 20 61 64 64 72 49 6e 73 54 6f 70  ;.    addrInsTop
279f7 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
279f8 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 72  dOp1(v, OP_If, r
279f9 65 67 45 6f 66 29 3b 0a 20 20 7d 0a 0a 20 20 2f  egEof);.  }..  /
279fa 2a 20 41 6c 6c 6f 63 61 74 65 20 72 65 67 69 73  * Allocate regis
279fb 74 65 72 73 20 66 6f 72 20 68 6f 6c 64 69 6e 67  ters for holding
279fc 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68   the rowid of th
279fd 65 20 6e 65 77 20 72 6f 77 2c 0a 20 20 2a 2a 20  e new row,.  ** 
279fe 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  the content of t
279ff 68 65 20 6e 65 77 20 72 6f 77 2c 20 61 6e 64 20  he new row, and 
27a00 74 68 65 20 61 73 73 65 6d 62 6c 69 65 64 20 72  the assemblied r
27a01 6f 77 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2f 0a  ow record..  */.
27a02 20 20 72 65 67 52 65 63 6f 72 64 20 3d 20 2b 2b    regRecord = ++
27a03 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
27a04 72 65 67 52 6f 77 69 64 20 3d 20 72 65 67 49 6e  regRowid = regIn
27a05 73 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  s = pParse->nMem
27a06 2b 31 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d  +1;.  pParse->nM
27a07 65 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c  em += pTab->nCol
27a08 20 2b 20 31 3b 0a 20 20 69 66 28 20 49 73 56 69   + 1;.  if( IsVi
27a09 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
27a0a 20 20 20 72 65 67 52 6f 77 69 64 2b 2b 3b 0a 20     regRowid++;. 
27a0b 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b     pParse->nMem+
27a0c 2b 3b 0a 20 20 7d 0a 20 20 72 65 67 44 61 74 61  +;.  }.  regData
27a0d 20 3d 20 72 65 67 52 6f 77 69 64 2b 31 3b 0a 0a   = regRowid+1;..
27a0e 20 20 2f 2a 20 52 75 6e 20 74 68 65 20 42 45 46    /* Run the BEF
27a0f 4f 52 45 20 61 6e 64 20 49 4e 53 54 45 41 44 20  ORE and INSTEAD 
27a10 4f 46 20 74 72 69 67 67 65 72 73 2c 20 69 66 20  OF triggers, if 
27a11 74 68 65 72 65 20 61 72 65 20 61 6e 79 0a 20 20  there are any.  
27a12 2a 2f 0a 20 20 65 6e 64 4f 66 4c 6f 6f 70 20 3d  */.  endOfLoop =
27a13 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
27a14 4c 61 62 65 6c 28 76 29 3b 0a 20 20 69 66 28 20  Label(v);.  if( 
27a15 74 6d 61 73 6b 20 26 20 54 52 49 47 47 45 52 5f  tmask & TRIGGER_
27a16 42 45 46 4f 52 45 20 29 7b 0a 20 20 20 20 69 6e  BEFORE ){.    in
27a17 74 20 72 65 67 43 6f 6c 73 20 3d 20 73 71 6c 69  t regCols = sqli
27a18 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28  te3GetTempRange(
27a19 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 6e 43  pParse, pTab->nC
27a1a 6f 6c 2b 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 62  ol+1);..    /* b
27a1b 75 69 6c 64 20 74 68 65 20 4e 45 57 2e 2a 20 72  uild the NEW.* r
27a1c 65 66 65 72 65 6e 63 65 20 72 6f 77 2e 20 20 4e  eference row.  N
27a1d 6f 74 65 20 74 68 61 74 20 69 66 20 74 68 65 72  ote that if ther
27a1e 65 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52 0a  e is an INTEGER.
27a1f 20 20 20 20 2a 2a 20 50 52 49 4d 41 52 59 20 4b      ** PRIMARY K
27a20 45 59 20 69 6e 74 6f 20 77 68 69 63 68 20 61 20  EY into which a 
27a21 4e 55 4c 4c 20 69 73 20 62 65 69 6e 67 20 69 6e  NULL is being in
27a22 73 65 72 74 65 64 2c 20 74 68 61 74 20 4e 55 4c  serted, that NUL
27a23 4c 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 2a 2a  L will be.    **
27a24 20 74 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f   translated into
27a25 20 61 20 75 6e 69 71 75 65 20 49 44 20 66 6f 72   a unique ID for
27a26 20 74 68 65 20 72 6f 77 2e 20 20 42 75 74 20 6f   the row.  But o
27a27 6e 20 61 20 42 45 46 4f 52 45 20 74 72 69 67 67  n a BEFORE trigg
27a28 65 72 2c 0a 20 20 20 20 2a 2a 20 77 65 20 64 6f  er,.    ** we do
27a29 20 6e 6f 74 20 6b 6e 6f 77 20 77 68 61 74 20 74   not know what t
27a2a 68 65 20 75 6e 69 71 75 65 20 49 44 20 77 69 6c  he unique ID wil
27a2b 6c 20 62 65 20 28 62 65 63 61 75 73 65 20 74 68  l be (because th
27a2c 65 20 69 6e 73 65 72 74 20 68 61 73 0a 20 20 20  e insert has.   
27a2d 20 2a 2a 20 6e 6f 74 20 68 61 70 70 65 6e 65 64   ** not happened
27a2e 20 79 65 74 29 20 73 6f 20 77 65 20 73 75 62 73   yet) so we subs
27a2f 74 69 74 75 74 65 20 61 20 72 6f 77 69 64 20 6f  titute a rowid o
27a30 66 20 2d 31 0a 20 20 20 20 2a 2f 0a 20 20 20 20  f -1.    */.    
27a31 69 66 28 20 6b 65 79 43 6f 6c 75 6d 6e 3c 30 20  if( keyColumn<0 
27a32 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
27a33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27a34 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 72 65  _Integer, -1, re
27a35 67 43 6f 6c 73 29 3b 0a 20 20 20 20 7d 65 6c 73  gCols);.    }els
27a36 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 31 3b  e{.      int j1;
27a37 0a 20 20 20 20 20 20 69 66 28 20 75 73 65 54 65  .      if( useTe
27a38 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  mpTable ){.     
27a39 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27a3a 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
27a3b 6e 2c 20 73 72 63 54 61 62 2c 20 6b 65 79 43 6f  n, srcTab, keyCo
27a3c 6c 75 6d 6e 2c 20 72 65 67 43 6f 6c 73 29 3b 0a  lumn, regCols);.
27a3d 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
27a3e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65       assert( pSe
27a3f 6c 65 63 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 4f  lect==0 );  /* O
27a40 74 68 65 72 77 69 73 65 20 75 73 65 54 65 6d 70  therwise useTemp
27a41 54 61 62 6c 65 20 69 73 20 74 72 75 65 20 2a 2f  Table is true */
27a42 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
27a43 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
27a44 20 70 4c 69 73 74 2d 3e 61 5b 6b 65 79 43 6f 6c   pList->a[keyCol
27a45 75 6d 6e 5d 2e 70 45 78 70 72 2c 20 72 65 67 43  umn].pExpr, regC
27a46 6f 6c 73 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ols);.      }.  
27a47 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33      j1 = sqlite3
27a48 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
27a49 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 43 6f 6c  _NotNull, regCol
27a4a 73 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  s);.      sqlite
27a4b 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
27a4c 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 72  P_Integer, -1, r
27a4d 65 67 43 6f 6c 73 29 3b 0a 20 20 20 20 20 20 73  egCols);.      s
27a4e 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
27a4f 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 20  re(v, j1);.     
27a50 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27a51 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49  p1(v, OP_MustBeI
27a52 6e 74 2c 20 72 65 67 43 6f 6c 73 29 3b 0a 20 20  nt, regCols);.  
27a53 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 61 6e 6e    }..    /* Cann
27a54 6f 74 20 68 61 76 65 20 74 72 69 67 67 65 72 73  ot have triggers
27a55 20 6f 6e 20 61 20 76 69 72 74 75 61 6c 20 74 61   on a virtual ta
27a56 62 6c 65 2e 20 49 66 20 69 74 20 77 65 72 65 20  ble. If it were 
27a57 70 6f 73 73 69 62 6c 65 2c 0a 20 20 20 20 2a 2a  possible,.    **
27a58 20 74 68 69 73 20 62 6c 6f 63 6b 20 77 6f 75 6c   this block woul
27a59 64 20 68 61 76 65 20 74 6f 20 61 63 63 6f 75 6e  d have to accoun
27a5a 74 20 66 6f 72 20 68 69 64 64 65 6e 20 63 6f 6c  t for hidden col
27a5b 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  umn..    */.    
27a5c 61 73 73 65 72 74 28 20 21 49 73 56 69 72 74 75  assert( !IsVirtu
27a5d 61 6c 28 70 54 61 62 29 20 29 3b 0a 0a 20 20 20  al(pTab) );..   
27a5e 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 6e   /* Create the n
27a5f 65 77 20 63 6f 6c 75 6d 6e 20 64 61 74 61 0a 20  ew column data. 
27a60 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d     */.    for(i=
27a61 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  0; i<pTab->nCol;
27a62 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
27a63 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20   pColumn==0 ){. 
27a64 20 20 20 20 20 20 20 6a 20 3d 20 69 3b 0a 20 20         j = i;.  
27a65 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
27a66 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 43     for(j=0; j<pC
27a67 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 6a 2b 2b 29  olumn->nId; j++)
27a68 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
27a69 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d 2e 69 64  pColumn->a[j].id
27a6a 78 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20 20  x==i ) break;.  
27a6b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
27a6c 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d        if( pColum
27a6d 6e 20 26 26 20 6a 3e 3d 70 43 6f 6c 75 6d 6e 2d  n && j>=pColumn-
27a6e 3e 6e 49 64 20 29 7b 0a 20 20 20 20 20 20 20 20  >nId ){.        
27a6f 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
27a70 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 61 43  pParse, pTab->aC
27a71 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c 20 72 65 67  ol[i].pDflt, reg
27a72 43 6f 6c 73 2b 69 2b 31 29 3b 0a 20 20 20 20 20  Cols+i+1);.     
27a73 20 7d 65 6c 73 65 20 69 66 28 20 75 73 65 54 65   }else if( useTe
27a74 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  mpTable ){.     
27a75 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27a76 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
27a77 6e 2c 20 73 72 63 54 61 62 2c 20 6a 2c 20 72 65  n, srcTab, j, re
27a78 67 43 6f 6c 73 2b 69 2b 31 29 3b 20 0a 20 20 20  gCols+i+1); .   
27a79 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
27a7a 20 20 61 73 73 65 72 74 28 20 70 53 65 6c 65 63    assert( pSelec
27a7b 74 3d 3d 30 20 29 3b 20 2f 2a 20 4f 74 68 65 72  t==0 ); /* Other
27a7c 77 69 73 65 20 75 73 65 54 65 6d 70 54 61 62 6c  wise useTempTabl
27a7d 65 20 69 73 20 74 72 75 65 20 2a 2f 0a 20 20 20  e is true */.   
27a7e 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
27a7f 43 6f 64 65 41 6e 64 43 61 63 68 65 28 70 50 61  CodeAndCache(pPa
27a80 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d  rse, pList->a[j]
27a81 2e 70 45 78 70 72 2c 20 72 65 67 43 6f 6c 73 2b  .pExpr, regCols+
27a82 69 2b 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  i+1);.      }.  
27a83 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
27a84 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54  his is an INSERT
27a85 20 6f 6e 20 61 20 76 69 65 77 20 77 69 74 68 20   on a view with 
27a86 61 6e 20 49 4e 53 54 45 41 44 20 4f 46 20 49 4e  an INSTEAD OF IN
27a87 53 45 52 54 20 74 72 69 67 67 65 72 2c 0a 20 20  SERT trigger,.  
27a88 20 20 2a 2a 20 64 6f 20 6e 6f 74 20 61 74 74 65    ** do not atte
27a89 6d 70 74 20 61 6e 79 20 63 6f 6e 76 65 72 73 69  mpt any conversi
27a8a 6f 6e 73 20 62 65 66 6f 72 65 20 61 73 73 65 6d  ons before assem
27a8b 62 6c 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64  bling the record
27a8c 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73  ..    ** If this
27a8d 20 69 73 20 61 20 72 65 61 6c 20 74 61 62 6c 65   is a real table
27a8e 2c 20 61 74 74 65 6d 70 74 20 63 6f 6e 76 65 72  , attempt conver
27a8f 73 69 6f 6e 73 20 61 73 20 72 65 71 75 69 72 65  sions as require
27a90 64 20 62 79 20 74 68 65 0a 20 20 20 20 2a 2a 20  d by the.    ** 
27a91 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 61 66 66  table column aff
27a92 69 6e 69 74 69 65 73 2e 0a 20 20 20 20 2a 2f 0a  inities..    */.
27a93 20 20 20 20 69 66 28 20 21 69 73 56 69 65 77 20      if( !isView 
27a94 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
27a95 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27a96 5f 41 66 66 69 6e 69 74 79 2c 20 72 65 67 43 6f  _Affinity, regCo
27a97 6c 73 2b 31 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c  ls+1, pTab->nCol
27a98 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
27a99 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74 72  TableAffinityStr
27a9a 28 76 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 7d  (v, pTab);.    }
27a9b 0a 0a 20 20 20 20 2f 2a 20 46 69 72 65 20 42 45  ..    /* Fire BE
27a9c 46 4f 52 45 20 6f 72 20 49 4e 53 54 45 41 44 20  FORE or INSTEAD 
27a9d 4f 46 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20  OF triggers */. 
27a9e 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f     sqlite3CodeRo
27a9f 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c  wTrigger(pParse,
27aa0 20 70 54 72 69 67 67 65 72 2c 20 54 4b 5f 49 4e   pTrigger, TK_IN
27aa1 53 45 52 54 2c 20 30 2c 20 54 52 49 47 47 45 52  SERT, 0, TRIGGER
27aa2 5f 42 45 46 4f 52 45 2c 20 0a 20 20 20 20 20 20  _BEFORE, .      
27aa3 20 20 70 54 61 62 2c 20 72 65 67 43 6f 6c 73 2d    pTab, regCols-
27aa4 70 54 61 62 2d 3e 6e 43 6f 6c 2d 31 2c 20 6f 6e  pTab->nCol-1, on
27aa5 45 72 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70  Error, endOfLoop
27aa6 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 52  );..    sqlite3R
27aa7 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28  eleaseTempRange(
27aa8 70 50 61 72 73 65 2c 20 72 65 67 43 6f 6c 73 2c  pParse, regCols,
27aa9 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 3b 0a   pTab->nCol+1);.
27aaa 20 20 7d 0a 0a 20 20 2f 2a 20 50 75 73 68 20 74    }..  /* Push t
27aab 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  he record number
27aac 20 66 6f 72 20 74 68 65 20 6e 65 77 20 65 6e 74   for the new ent
27aad 72 79 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63  ry onto the stac
27aae 6b 2e 20 20 54 68 65 0a 20 20 2a 2a 20 72 65 63  k.  The.  ** rec
27aaf 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20 61 20  ord number is a 
27ab0 72 61 6e 64 6f 6d 6c 79 20 67 65 6e 65 72 61 74  randomly generat
27ab1 65 20 69 6e 74 65 67 65 72 20 63 72 65 61 74 65  e integer create
27ab2 64 20 62 79 20 4e 65 77 52 6f 77 69 64 0a 20 20  d by NewRowid.  
27ab3 2a 2a 20 65 78 63 65 70 74 20 77 68 65 6e 20 74  ** except when t
27ab4 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20  he table has an 
27ab5 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
27ab6 4b 45 59 20 63 6f 6c 75 6d 6e 2c 20 69 6e 20 77  KEY column, in w
27ab7 68 69 63 68 0a 20 20 2a 2a 20 63 61 73 65 20 74  hich.  ** case t
27ab8 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  he record number
27ab9 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
27aba 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 20 0a 20 20  that column. .  
27abb 2a 2f 0a 20 20 69 66 28 20 21 69 73 56 69 65 77  */.  if( !isView
27abc 20 29 7b 0a 20 20 20 20 69 66 28 20 49 73 56 69   ){.    if( IsVi
27abd 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
27abe 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 20       /* The row 
27abf 74 68 61 74 20 74 68 65 20 56 55 70 64 61 74 65  that the VUpdate
27ac0 20 6f 70 63 6f 64 65 20 77 69 6c 6c 20 64 65 6c   opcode will del
27ac1 65 74 65 3a 20 6e 6f 6e 65 20 2a 2f 0a 20 20 20  ete: none */.   
27ac2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27ac3 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
27ac4 20 30 2c 20 72 65 67 49 6e 73 29 3b 0a 20 20 20   0, regIns);.   
27ac5 20 7d 0a 20 20 20 20 69 66 28 20 6b 65 79 43 6f   }.    if( keyCo
27ac6 6c 75 6d 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20  lumn>=0 ){.     
27ac7 20 69 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c   if( useTempTabl
27ac8 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  e ){.        sql
27ac9 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
27aca 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63  , OP_Column, src
27acb 54 61 62 2c 20 6b 65 79 43 6f 6c 75 6d 6e 2c 20  Tab, keyColumn, 
27acc 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  regRowid);.     
27acd 20 7d 65 6c 73 65 20 69 66 28 20 70 53 65 6c 65   }else if( pSele
27ace 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ct ){.        sq
27acf 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27ad0 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67  v, OP_SCopy, reg
27ad1 46 72 6f 6d 53 65 6c 65 63 74 2b 6b 65 79 43 6f  FromSelect+keyCo
27ad2 6c 75 6d 6e 2c 20 72 65 67 52 6f 77 69 64 29 3b  lumn, regRowid);
27ad3 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
27ad4 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f        VdbeOp *pO
27ad5 70 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  p;.        sqlit
27ad6 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
27ad7 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6b 65 79 43  e, pList->a[keyC
27ad8 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 2c 20 72 65  olumn].pExpr, re
27ad9 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 20  gRowid);.       
27ada 20 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64   pOp = sqlite3Vd
27adb 62 65 47 65 74 4f 70 28 76 2c 20 2d 31 29 3b 0a  beGetOp(v, -1);.
27adc 20 20 20 20 20 20 20 20 69 66 28 20 41 4c 57 41          if( ALWA
27add 59 53 28 70 4f 70 29 20 26 26 20 70 4f 70 2d 3e  YS(pOp) && pOp->
27ade 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 75 6c 6c 20  opcode==OP_Null 
27adf 26 26 20 21 49 73 56 69 72 74 75 61 6c 28 70 54  && !IsVirtual(pT
27ae0 61 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ab) ){.         
27ae1 20 61 70 70 65 6e 64 46 6c 61 67 20 3d 20 31 3b   appendFlag = 1;
27ae2 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e  .          pOp->
27ae3 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 65 77 52  opcode = OP_NewR
27ae4 6f 77 69 64 3b 0a 20 20 20 20 20 20 20 20 20 20  owid;.          
27ae5 70 4f 70 2d 3e 70 31 20 3d 20 62 61 73 65 43 75  pOp->p1 = baseCu
27ae6 72 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70  r;.          pOp
27ae7 2d 3e 70 32 20 3d 20 72 65 67 52 6f 77 69 64 3b  ->p2 = regRowid;
27ae8 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e  .          pOp->
27ae9 70 33 20 3d 20 72 65 67 41 75 74 6f 69 6e 63 3b  p3 = regAutoinc;
27aea 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
27aeb 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74   }.      /* If t
27aec 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 65  he PRIMARY KEY e
27aed 78 70 72 65 73 73 69 6f 6e 20 69 73 20 4e 55 4c  xpression is NUL
27aee 4c 2c 20 74 68 65 6e 20 75 73 65 20 4f 50 5f 4e  L, then use OP_N
27aef 65 77 52 6f 77 69 64 0a 20 20 20 20 20 20 2a 2a  ewRowid.      **
27af0 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 75   to generate a u
27af1 6e 69 71 75 65 20 70 72 69 6d 61 72 79 20 6b 65  nique primary ke
27af2 79 20 76 61 6c 75 65 2e 0a 20 20 20 20 20 20 2a  y value..      *
27af3 2f 0a 20 20 20 20 20 20 69 66 28 20 21 61 70 70  /.      if( !app
27af4 65 6e 64 46 6c 61 67 20 29 7b 0a 20 20 20 20 20  endFlag ){.     
27af5 20 20 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20 20     int j1;.     
27af6 20 20 20 69 66 28 20 21 49 73 56 69 72 74 75 61     if( !IsVirtua
27af7 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20  l(pTab) ){.     
27af8 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65       j1 = sqlite
27af9 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
27afa 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 52 6f  P_NotNull, regRo
27afb 77 69 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20  wid);.          
27afc 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27afd 33 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64  3(v, OP_NewRowid
27afe 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67 52 6f  , baseCur, regRo
27aff 77 69 64 2c 20 72 65 67 41 75 74 6f 69 6e 63 29  wid, regAutoinc)
27b00 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
27b01 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
27b02 76 2c 20 6a 31 29 3b 0a 20 20 20 20 20 20 20 20  v, j1);.        
27b03 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
27b04 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j1 = sqlite3Vdb
27b05 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
27b06 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
27b07 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
27b08 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 52 6f  OP_IsNull, regRo
27b09 77 69 64 2c 20 6a 31 2b 32 29 3b 0a 20 20 20 20  wid, j1+2);.    
27b0a 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71      }.        sq
27b0b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
27b0c 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c  v, OP_MustBeInt,
27b0d 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
27b0e 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66    }.    }else if
27b0f 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
27b10 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
27b11 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
27b12 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 52  OP_Null, 0, regR
27b13 6f 77 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65  owid);.    }else
27b14 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
27b15 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
27b16 4e 65 77 52 6f 77 69 64 2c 20 62 61 73 65 43 75  NewRowid, baseCu
27b17 72 2c 20 72 65 67 52 6f 77 69 64 2c 20 72 65 67  r, regRowid, reg
27b18 41 75 74 6f 69 6e 63 29 3b 0a 20 20 20 20 20 20  Autoinc);.      
27b19 61 70 70 65 6e 64 46 6c 61 67 20 3d 20 31 3b 0a  appendFlag = 1;.
27b1a 20 20 20 20 7d 0a 20 20 20 20 61 75 74 6f 49 6e      }.    autoIn
27b1b 63 53 74 65 70 28 70 50 61 72 73 65 2c 20 72 65  cStep(pParse, re
27b1c 67 41 75 74 6f 69 6e 63 2c 20 72 65 67 52 6f 77  gAutoinc, regRow
27b1d 69 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 50 75 73  id);..    /* Pus
27b1e 68 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b  h onto the stack
27b1f 2c 20 64 61 74 61 20 66 6f 72 20 61 6c 6c 20 63  , data for all c
27b20 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 6e 65  olumns of the ne
27b21 77 20 65 6e 74 72 79 2c 20 62 65 67 69 6e 6e 69  w entry, beginni
27b22 6e 67 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 74  ng.    ** with t
27b23 68 65 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 2e  he first column.
27b24 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 48 69 64  .    */.    nHid
27b25 64 65 6e 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72  den = 0;.    for
27b26 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43  (i=0; i<pTab->nC
27b27 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
27b28 69 6e 74 20 69 52 65 67 53 74 6f 72 65 20 3d 20  int iRegStore = 
27b29 72 65 67 52 6f 77 69 64 2b 31 2b 69 3b 0a 20 20  regRowid+1+i;.  
27b2a 20 20 20 20 69 66 28 20 69 3d 3d 70 54 61 62 2d      if( i==pTab-
27b2b 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  >iPKey ){.      
27b2c 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f    /* The value o
27b2d 66 20 74 68 65 20 49 4e 54 45 47 45 52 20 50 52  f the INTEGER PR
27b2e 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e  IMARY KEY column
27b2f 20 69 73 20 61 6c 77 61 79 73 20 61 20 4e 55 4c   is always a NUL
27b30 4c 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 57 68  L..        ** Wh
27b31 65 6e 65 76 65 72 20 74 68 69 73 20 63 6f 6c 75  enever this colu
27b32 6d 6e 20 69 73 20 72 65 61 64 2c 20 74 68 65 20  mn is read, the 
27b33 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 77 69  record number wi
27b34 6c 6c 20 62 65 20 73 75 62 73 74 69 74 75 74 65  ll be substitute
27b35 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20  d.        ** in 
27b36 69 74 73 20 70 6c 61 63 65 2e 20 20 53 6f 20 77  its place.  So w
27b37 69 6c 6c 20 66 69 6c 6c 20 74 68 69 73 20 63 6f  ill fill this co
27b38 6c 75 6d 6e 20 77 69 74 68 20 61 20 4e 55 4c 4c  lumn with a NULL
27b39 20 74 6f 20 61 76 6f 69 64 0a 20 20 20 20 20 20   to avoid.      
27b3a 20 20 2a 2a 20 74 61 6b 69 6e 67 20 75 70 20 64    ** taking up d
27b3b 61 74 61 20 73 70 61 63 65 20 77 69 74 68 20 69  ata space with i
27b3c 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20  nformation that 
27b3d 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 75 73  will never be us
27b3e 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 73  ed. */.        s
27b3f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
27b40 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
27b41 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20 20 20  iRegStore);.    
27b42 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
27b43 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
27b44 70 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20  pColumn==0 ){.  
27b45 20 20 20 20 20 20 69 66 28 20 49 73 48 69 64 64        if( IsHidd
27b46 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62 2d 3e  enColumn(&pTab->
27b47 61 43 6f 6c 5b 69 5d 29 20 29 7b 0a 20 20 20 20  aCol[i]) ){.    
27b48 20 20 20 20 20 20 61 73 73 65 72 74 28 20 49 73        assert( Is
27b49 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 3b  Virtual(pTab) );
27b4a 0a 20 20 20 20 20 20 20 20 20 20 6a 20 3d 20 2d  .          j = -
27b4b 31 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 48 69  1;.          nHi
27b4c 64 64 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20  dden++;.        
27b4d 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
27b4e 20 6a 20 3d 20 69 20 2d 20 6e 48 69 64 64 65 6e   j = i - nHidden
27b4f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
27b50 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
27b51 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 43 6f 6c   for(j=0; j<pCol
27b52 75 6d 6e 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a  umn->nId; j++){.
27b53 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43            if( pC
27b54 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d 2e 69 64 78 3d  olumn->a[j].idx=
27b55 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =i ) break;.    
27b56 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
27b57 20 20 20 20 69 66 28 20 6a 3c 30 20 7c 7c 20 6e      if( j<0 || n
27b58 43 6f 6c 75 6d 6e 3d 3d 30 20 7c 7c 20 28 70 43  Column==0 || (pC
27b59 6f 6c 75 6d 6e 20 26 26 20 6a 3e 3d 70 43 6f 6c  olumn && j>=pCol
27b5a 75 6d 6e 2d 3e 6e 49 64 29 20 29 7b 0a 20 20 20  umn->nId) ){.   
27b5b 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
27b5c 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 54 61  Code(pParse, pTa
27b5d 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74  b->aCol[i].pDflt
27b5e 2c 20 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20  , iRegStore);.  
27b5f 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 73      }else if( us
27b60 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20  eTempTable ){.  
27b61 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27b62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
27b63 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6a 2c  lumn, srcTab, j,
27b64 20 69 52 65 67 53 74 6f 72 65 29 3b 20 0a 20 20   iRegStore); .  
27b65 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 53      }else if( pS
27b66 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20  elect ){.       
27b67 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27b68 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20  p2(v, OP_SCopy, 
27b69 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 2b 6a 2c  regFromSelect+j,
27b6a 20 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20 20   iRegStore);.   
27b6b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
27b6c 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
27b6d 65 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d  e(pParse, pList-
27b6e 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 69 52 65  >a[j].pExpr, iRe
27b6f 67 53 74 6f 72 65 29 3b 0a 20 20 20 20 20 20 7d  gStore);.      }
27b70 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 47  .    }..    /* G
27b71 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
27b72 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74  check constraint
27b73 73 20 61 6e 64 20 67 65 6e 65 72 61 74 65 20 69  s and generate i
27b74 6e 64 65 78 20 6b 65 79 73 20 61 6e 64 0a 20 20  ndex keys and.  
27b75 20 20 2a 2a 20 64 6f 20 74 68 65 20 69 6e 73 65    ** do the inse
27b76 72 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 23 69  rtion..    */.#i
27b77 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
27b78 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
27b79 20 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c     if( IsVirtual
27b7a 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
27b7b 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 56 54 61  const char *pVTa
27b7c 62 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  b = (const char 
27b7d 2a 29 73 71 6c 69 74 65 33 47 65 74 56 54 61 62  *)sqlite3GetVTab
27b7e 6c 65 28 64 62 2c 20 70 54 61 62 29 3b 0a 20 20  le(db, pTab);.  
27b7f 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 4d      sqlite3VtabM
27b80 61 6b 65 57 72 69 74 61 62 6c 65 28 70 50 61 72  akeWritable(pPar
27b81 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20  se, pTab);.     
27b82 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27b83 70 34 28 76 2c 20 4f 50 5f 56 55 70 64 61 74 65  p4(v, OP_VUpdate
27b84 2c 20 31 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b  , 1, pTab->nCol+
27b85 32 2c 20 72 65 67 49 6e 73 2c 20 70 56 54 61 62  2, regIns, pVTab
27b86 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 20 20  , P4_VTAB);.    
27b87 20 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72    sqlite3MayAbor
27b88 74 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 7d  t(pParse);.    }
27b89 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20  else.#endif.    
27b8a 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 73 52 65  {.      int isRe
27b8b 70 6c 61 63 65 3b 20 20 20 20 2f 2a 20 53 65 74  place;    /* Set
27b8c 20 74 6f 20 74 72 75 65 20 69 66 20 63 6f 6e 73   to true if cons
27b8d 74 72 61 69 6e 74 73 20 6d 61 79 20 63 61 75 73  traints may caus
27b8e 65 20 61 20 72 65 70 6c 61 63 65 20 2a 2f 0a 20  e a replace */. 
27b8f 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65       sqlite3Gene
27b90 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68  rateConstraintCh
27b91 65 63 6b 73 28 70 50 61 72 73 65 2c 20 70 54 61  ecks(pParse, pTa
27b92 62 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67 49  b, baseCur, regI
27b93 6e 73 2c 20 61 52 65 67 49 64 78 2c 0a 20 20 20  ns, aRegIdx,.   
27b94 20 20 20 20 20 20 20 6b 65 79 43 6f 6c 75 6d 6e         keyColumn
27b95 3e 3d 30 2c 20 30 2c 20 6f 6e 45 72 72 6f 72 2c  >=0, 0, onError,
27b96 20 65 6e 64 4f 66 4c 6f 6f 70 2c 20 26 69 73 52   endOfLoop, &isR
27b97 65 70 6c 61 63 65 0a 20 20 20 20 20 20 29 3b 0a  eplace.      );.
27b98 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 6b 43        sqlite3FkC
27b99 68 65 63 6b 28 70 50 61 72 73 65 2c 20 70 54 61  heck(pParse, pTa
27b9a 62 2c 20 30 2c 20 72 65 67 49 6e 73 29 3b 0a 20  b, 0, regIns);. 
27b9b 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6d 70       sqlite3Comp
27b9c 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 0a 20  leteInsertion(. 
27b9d 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2c           pParse,
27b9e 20 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20   pTab, baseCur, 
27b9f 72 65 67 49 6e 73 2c 20 61 52 65 67 49 64 78 2c  regIns, aRegIdx,
27ba0 20 30 2c 20 61 70 70 65 6e 64 46 6c 61 67 2c 20   0, appendFlag, 
27ba1 69 73 52 65 70 6c 61 63 65 3d 3d 30 0a 20 20 20  isReplace==0.   
27ba2 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a     );.    }.  }.
27ba3 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65  .  /* Update the
27ba4 20 63 6f 75 6e 74 20 6f 66 20 72 6f 77 73 20 74   count of rows t
27ba5 68 61 74 20 61 72 65 20 69 6e 73 65 72 74 65 64  hat are inserted
27ba6 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d  .  */.  if( (db-
27ba7 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
27ba8 43 6f 75 6e 74 52 6f 77 73 29 21 3d 30 20 29 7b  CountRows)!=0 ){
27ba9 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27baa 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64  AddOp2(v, OP_Add
27bab 49 6d 6d 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74  Imm, regRowCount
27bac 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  , 1);.  }..  if(
27bad 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20   pTrigger ){.   
27bae 20 2f 2a 20 43 6f 64 65 20 41 46 54 45 52 20 74   /* Code AFTER t
27baf 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 20 20 73  riggers */.    s
27bb0 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69  qlite3CodeRowTri
27bb1 67 67 65 72 28 70 50 61 72 73 65 2c 20 70 54 72  gger(pParse, pTr
27bb2 69 67 67 65 72 2c 20 54 4b 5f 49 4e 53 45 52 54  igger, TK_INSERT
27bb3 2c 20 30 2c 20 54 52 49 47 47 45 52 5f 41 46 54  , 0, TRIGGER_AFT
27bb4 45 52 2c 20 0a 20 20 20 20 20 20 20 20 70 54 61  ER, .        pTa
27bb5 62 2c 20 72 65 67 44 61 74 61 2d 32 2d 70 54 61  b, regData-2-pTa
27bb6 62 2d 3e 6e 43 6f 6c 2c 20 6f 6e 45 72 72 6f 72  b->nCol, onError
27bb7 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20  , endOfLoop);.  
27bb8 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 62 6f 74 74  }..  /* The bott
27bb9 6f 6d 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 69  om of the main i
27bba 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f 70 2c 20 69  nsertion loop, i
27bbb 66 20 74 68 65 20 64 61 74 61 20 73 6f 75 72 63  f the data sourc
27bbc 65 0a 20 20 2a 2a 20 69 73 20 61 20 53 45 4c 45  e.  ** is a SELE
27bbd 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20  CT statement..  
27bbe 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
27bbf 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
27bc0 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 69 66  endOfLoop);.  if
27bc1 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29  ( useTempTable )
27bc2 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
27bc3 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
27bc4 78 74 2c 20 73 72 63 54 61 62 2c 20 61 64 64 72  xt, srcTab, addr
27bc5 43 6f 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  Cont);.    sqlit
27bc6 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
27bc7 2c 20 61 64 64 72 49 6e 73 54 6f 70 29 3b 0a 20  , addrInsTop);. 
27bc8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27bc9 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp1(v, OP_Close
27bca 2c 20 73 72 63 54 61 62 29 3b 0a 20 20 7d 65 6c  , srcTab);.  }el
27bcb 73 65 20 69 66 28 20 70 53 65 6c 65 63 74 20 29  se if( pSelect )
27bcc 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
27bcd 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
27bce 74 6f 2c 20 30 2c 20 61 64 64 72 43 6f 6e 74 29  to, 0, addrCont)
27bcf 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
27bd0 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
27bd1 72 49 6e 73 54 6f 70 29 3b 0a 20 20 7d 0a 0a 20  rInsTop);.  }.. 
27bd2 20 69 66 28 20 21 49 73 56 69 72 74 75 61 6c 28   if( !IsVirtual(
27bd3 70 54 61 62 29 20 26 26 20 21 69 73 56 69 65 77  pTab) && !isView
27bd4 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6c 6f 73 65   ){.    /* Close
27bd5 20 61 6c 6c 20 74 61 62 6c 65 73 20 6f 70 65 6e   all tables open
27bd6 65 64 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  ed */.    sqlite
27bd7 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
27bd8 50 5f 43 6c 6f 73 65 2c 20 62 61 73 65 43 75 72  P_Close, baseCur
27bd9 29 3b 0a 20 20 20 20 66 6f 72 28 69 64 78 3d 31  );.    for(idx=1
27bda 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  , pIdx=pTab->pIn
27bdb 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
27bdc 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 64 78  pIdx->pNext, idx
27bdd 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
27bde 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
27bdf 4f 50 5f 43 6c 6f 73 65 2c 20 69 64 78 2b 62 61  OP_Close, idx+ba
27be0 73 65 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20  seCur);.    }.  
27be1 7d 0a 0a 69 6e 73 65 72 74 5f 65 6e 64 3a 0a 20  }..insert_end:. 
27be2 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 73   /* Update the s
27be3 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74  qlite_sequence t
27be4 61 62 6c 65 20 62 79 20 73 74 6f 72 69 6e 67 20  able by storing 
27be5 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  the content of t
27be6 68 65 0a 20 20 2a 2a 20 6d 61 78 69 6d 75 6d 20  he.  ** maximum 
27be7 72 6f 77 69 64 20 63 6f 75 6e 74 65 72 20 76 61  rowid counter va
27be8 6c 75 65 73 20 72 65 63 6f 72 64 65 64 20 77 68  lues recorded wh
27be9 69 6c 65 20 69 6e 73 65 72 74 69 6e 67 20 69 6e  ile inserting in
27bea 74 6f 0a 20 20 2a 2a 20 61 75 74 6f 69 6e 63 72  to.  ** autoincr
27beb 65 6d 65 6e 74 20 74 61 62 6c 65 73 2e 0a 20 20  ement tables..  
27bec 2a 2f 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  */.  if( pParse-
27bed 3e 6e 65 73 74 65 64 3d 3d 30 20 26 26 20 70 50  >nested==0 && pP
27bee 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61  arse->pTriggerTa
27bef 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  b==0 ){.    sqli
27bf0 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74  te3Autoincrement
27bf1 45 6e 64 28 70 50 61 72 73 65 29 3b 0a 20 20 7d  End(pParse);.  }
27bf2 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 74 75  ..  /*.  ** Retu
27bf3 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
27bf4 20 72 6f 77 73 20 69 6e 73 65 72 74 65 64 2e 20   rows inserted. 
27bf5 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  If this routine 
27bf6 69 73 20 0a 20 20 2a 2a 20 67 65 6e 65 72 61 74  is .  ** generat
27bf7 69 6e 67 20 63 6f 64 65 20 62 65 63 61 75 73 65  ing code because
27bf8 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f 20 73 71   of a call to sq
27bf9 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
27bfa 28 29 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20  (), do not.  ** 
27bfb 69 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62  invoke the callb
27bfc 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20  ack function..  
27bfd 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c  */.  if( (db->fl
27bfe 61 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74  ags&SQLITE_Count
27bff 52 6f 77 73 29 20 26 26 20 21 70 50 61 72 73 65  Rows) && !pParse
27c00 2d 3e 6e 65 73 74 65 64 20 26 26 20 21 70 50 61  ->nested && !pPa
27c01 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62  rse->pTriggerTab
27c02 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
27c03 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
27c04 52 65 73 75 6c 74 52 6f 77 2c 20 72 65 67 52 6f  ResultRow, regRo
27c05 77 43 6f 75 6e 74 2c 20 31 29 3b 0a 20 20 20 20  wCount, 1);.    
27c06 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
27c07 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20  mCols(v, 1);.   
27c08 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
27c09 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
27c0a 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 72 6f 77 73  NAME_NAME, "rows
27c0b 20 69 6e 73 65 72 74 65 64 22 2c 20 53 51 4c 49   inserted", SQLI
27c0c 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a  TE_STATIC);.  }.
27c0d 0a 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3a  .insert_cleanup:
27c0e 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
27c0f 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61 62  tDelete(db, pTab
27c10 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  List);.  sqlite3
27c11 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64  ExprListDelete(d
27c12 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 73 71 6c  b, pList);.  sql
27c13 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
27c14 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20  (db, pSelect);. 
27c15 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65   sqlite3IdListDe
27c16 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c 75 6d 6e  lete(db, pColumn
27c17 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
27c18 65 65 28 64 62 2c 20 61 52 65 67 49 64 78 29 3b  ee(db, aRegIdx);
27c19 0a 7d 0a 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65  .}../* Make sure
27c1a 20 22 69 73 56 69 65 77 22 20 61 6e 64 20 6f 74   "isView" and ot
27c1b 68 65 72 20 6d 61 63 72 6f 73 20 64 65 66 69 6e  her macros defin
27c1c 65 64 20 61 62 6f 76 65 20 61 72 65 20 75 6e 64  ed above are und
27c1d 65 66 69 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  efined. Otherwis
27c1e 65 0a 2a 2a 20 74 68 65 6c 79 20 6d 61 79 20 69  e.** thely may i
27c1f 6e 74 65 72 66 65 72 65 20 77 69 74 68 20 63 6f  nterfere with co
27c20 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20 6f 74 68  mpilation of oth
27c21 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20  er functions in 
27c22 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 28 6f 72  this file.** (or
27c23 20 69 6e 20 61 6e 6f 74 68 65 72 20 66 69 6c 65   in another file
27c24 2c 20 69 66 20 74 68 69 73 20 66 69 6c 65 20 62  , if this file b
27c25 65 63 6f 6d 65 73 20 70 61 72 74 20 6f 66 20 74  ecomes part of t
27c26 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 29  he amalgamation)
27c27 2e 20 20 2a 2f 0a 23 69 66 64 65 66 20 69 73 56  .  */.#ifdef isV
27c28 69 65 77 0a 20 23 75 6e 64 65 66 20 69 73 56 69  iew. #undef isVi
27c29 65 77 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  ew.#endif.#ifdef
27c2a 20 70 54 72 69 67 67 65 72 0a 20 23 75 6e 64 65   pTrigger. #unde
27c2b 66 20 70 54 72 69 67 67 65 72 0a 23 65 6e 64 69  f pTrigger.#endi
27c2c 66 0a 23 69 66 64 65 66 20 74 6d 61 73 6b 0a 20  f.#ifdef tmask. 
27c2d 23 75 6e 64 65 66 20 74 6d 61 73 6b 0a 23 65 6e  #undef tmask.#en
27c2e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  dif.../*.** Gene
27c2f 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64 6f 20  rate code to do 
27c30 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63 6b  constraint check
27c31 73 20 70 72 69 6f 72 20 74 6f 20 61 6e 20 49 4e  s prior to an IN
27c32 53 45 52 54 20 6f 72 20 61 6e 20 55 50 44 41 54  SERT or an UPDAT
27c33 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70  E..**.** The inp
27c34 75 74 20 69 73 20 61 20 72 61 6e 67 65 20 6f 66  ut is a range of
27c35 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72 65 67   consecutive reg
27c36 69 73 74 65 72 73 20 61 73 20 66 6f 6c 6c 6f 77  isters as follow
27c37 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 31 2e 20 20  s:.**.**    1.  
27c38 54 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65  The rowid of the
27c39 20 72 6f 77 20 61 66 74 65 72 20 74 68 65 20 75   row after the u
27c3a 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  pdate..**.**    
27c3b 32 2e 20 20 54 68 65 20 64 61 74 61 20 69 6e 20  2.  The data in 
27c3c 74 68 65 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e  the first column
27c3d 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20 61 66   of the entry af
27c3e 74 65 72 20 74 68 65 20 75 70 64 61 74 65 2e 0a  ter the update..
27c3f 2a 2a 0a 2a 2a 20 20 20 20 69 2e 20 20 44 61 74  **.**    i.  Dat
27c40 61 20 66 72 6f 6d 20 6d 69 64 64 6c 65 20 63 6f  a from middle co
27c41 6c 75 6d 6e 73 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 20  lumns....**.**  
27c42 20 20 4e 2e 20 20 54 68 65 20 64 61 74 61 20 69    N.  The data i
27c43 6e 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d  n the last colum
27c44 6e 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20 61  n of the entry a
27c45 66 74 65 72 20 74 68 65 20 75 70 64 61 74 65 2e  fter the update.
27c46 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 52 6f  .**.** The regRo
27c47 77 69 64 20 70 61 72 61 6d 65 74 65 72 20 69 73  wid parameter is
27c48 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
27c49 65 20 72 65 67 69 73 74 65 72 20 63 6f 6e 74 61  e register conta
27c4a 69 6e 69 6e 67 20 28 31 29 2e 0a 2a 2a 0a 2a 2a  ining (1)..**.**
27c4b 20 49 66 20 69 73 55 70 64 61 74 65 20 69 73 20   If isUpdate is 
27c4c 74 72 75 65 20 61 6e 64 20 72 6f 77 69 64 43 68  true and rowidCh
27c4d 6e 67 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  ng is non-zero, 
27c4e 74 68 65 6e 20 72 6f 77 69 64 43 68 6e 67 20 63  then rowidChng c
27c4f 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 61  ontains.** the a
27c50 64 64 72 65 73 73 20 6f 66 20 61 20 72 65 67 69  ddress of a regi
27c51 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ster containing 
27c52 74 68 65 20 72 6f 77 69 64 20 62 65 66 6f 72 65  the rowid before
27c53 20 74 68 65 20 75 70 64 61 74 65 20 74 61 6b 65   the update take
27c54 73 0a 2a 2a 20 70 6c 61 63 65 2e 20 69 73 55 70  s.** place. isUp
27c55 64 61 74 65 20 69 73 20 74 72 75 65 20 66 6f 72  date is true for
27c56 20 55 50 44 41 54 45 73 20 61 6e 64 20 66 61 6c   UPDATEs and fal
27c57 73 65 20 66 6f 72 20 49 4e 53 45 52 54 73 2e 20  se for INSERTs. 
27c58 49 66 20 69 73 55 70 64 61 74 65 0a 2a 2a 20 69  If isUpdate.** i
27c59 73 20 66 61 6c 73 65 2c 20 69 6e 64 69 63 61 74  s false, indicat
27c5a 69 6e 67 20 61 6e 20 49 4e 53 45 52 54 20 73 74  ing an INSERT st
27c5b 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 61 20  atement, then a 
27c5c 6e 6f 6e 2d 7a 65 72 6f 20 72 6f 77 69 64 43 68  non-zero rowidCh
27c5d 6e 67 20 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73  ng .** indicates
27c5e 20 74 68 61 74 20 74 68 65 20 72 6f 77 69 64 20   that the rowid 
27c5f 77 61 73 20 65 78 70 6c 69 63 69 74 6c 79 20 73  was explicitly s
27c60 70 65 63 69 66 69 65 64 20 61 73 20 70 61 72 74  pecified as part
27c61 20 6f 66 20 74 68 65 0a 2a 2a 20 49 4e 53 45 52   of the.** INSER
27c62 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 49 66 20  T statement. If 
27c63 72 6f 77 69 64 43 68 6e 67 20 69 73 20 66 61 6c  rowidChng is fal
27c64 73 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  se, it means tha
27c65 74 20 20 74 68 65 20 72 6f 77 69 64 20 69 73 0a  t  the rowid is.
27c66 2a 2a 20 63 6f 6d 70 75 74 65 64 20 61 75 74 6f  ** computed auto
27c67 6d 61 74 69 63 61 6c 6c 79 20 69 6e 20 61 6e 20  matically in an 
27c68 69 6e 73 65 72 74 20 6f 72 20 74 68 61 74 20 74  insert or that t
27c69 68 65 20 72 6f 77 69 64 20 76 61 6c 75 65 20 69  he rowid value i
27c6a 73 20 6e 6f 74 20 0a 2a 2a 20 6d 6f 64 69 66 69  s not .** modifi
27c6b 65 64 20 62 79 20 61 6e 20 75 70 64 61 74 65 2e  ed by an update.
27c6c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  .**.** The code 
27c6d 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 69  generated by thi
27c6e 73 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 20  s routine store 
27c6f 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72 69 65  new index entrie
27c70 73 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74  s into.** regist
27c71 65 72 73 20 69 64 65 6e 74 69 66 69 65 64 20 62  ers identified b
27c72 79 20 61 52 65 67 49 64 78 5b 5d 2e 20 20 4e 6f  y aRegIdx[].  No
27c73 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20   index entry is 
27c74 63 72 65 61 74 65 64 20 66 6f 72 0a 2a 2a 20 69  created for.** i
27c75 6e 64 69 63 65 73 20 77 68 65 72 65 20 61 52 65  ndices where aRe
27c76 67 49 64 78 5b 69 5d 3d 3d 30 2e 20 20 54 68 65  gIdx[i]==0.  The
27c77 20 6f 72 64 65 72 20 6f 66 20 69 6e 64 69 63 65   order of indice
27c78 73 20 69 6e 20 61 52 65 67 49 64 78 5b 5d 20 69  s in aRegIdx[] i
27c79 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73  s.** the same as
27c7a 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 69 6e   the order of in
27c7b 64 69 63 65 73 20 6f 6e 20 74 68 65 20 6c 69 6e  dices on the lin
27c7c 6b 65 64 20 6c 69 73 74 20 6f 66 20 69 6e 64 69  ked list of indi
27c7d 63 65 73 0a 2a 2a 20 61 74 74 61 63 68 65 64 20  ces.** attached 
27c7e 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a  to the table..**
27c7f 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
27c80 20 61 6c 73 6f 20 67 65 6e 65 72 61 74 65 73 20   also generates 
27c81 63 6f 64 65 20 74 6f 20 63 68 65 63 6b 20 63 6f  code to check co
27c82 6e 73 74 72 61 69 6e 74 73 2e 20 20 4e 4f 54 20  nstraints.  NOT 
27c83 4e 55 4c 4c 2c 0a 2a 2a 20 43 48 45 43 4b 2c 20  NULL,.** CHECK, 
27c84 61 6e 64 20 55 4e 49 51 55 45 20 63 6f 6e 73 74  and UNIQUE const
27c85 72 61 69 6e 74 73 20 61 72 65 20 61 6c 6c 20 63  raints are all c
27c86 68 65 63 6b 65 64 2e 20 20 49 66 20 61 20 63 6f  hecked.  If a co
27c87 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 73 2c 0a  nstraint fails,.
27c88 2a 2a 20 74 68 65 6e 20 74 68 65 20 61 70 70 72  ** then the appr
27c89 6f 70 72 69 61 74 65 20 61 63 74 69 6f 6e 20 69  opriate action i
27c8a 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 20 54 68  s performed.  Th
27c8b 65 72 65 20 61 72 65 20 66 69 76 65 20 70 6f 73  ere are five pos
27c8c 73 69 62 6c 65 0a 2a 2a 20 61 63 74 69 6f 6e 73  sible.** actions
27c8d 3a 20 52 4f 4c 4c 42 41 43 4b 2c 20 41 42 4f 52  : ROLLBACK, ABOR
27c8e 54 2c 20 46 41 49 4c 2c 20 52 45 50 4c 41 43 45  T, FAIL, REPLACE
27c8f 2c 20 61 6e 64 20 49 47 4e 4f 52 45 2e 0a 2a 2a  , and IGNORE..**
27c90 0a 2a 2a 20 20 43 6f 6e 73 74 72 61 69 6e 74 20  .**  Constraint 
27c91 74 79 70 65 20 20 41 63 74 69 6f 6e 20 20 20 20  type  Action    
27c92 20 20 20 57 68 61 74 20 48 61 70 70 65 6e 73 0a     What Happens.
27c93 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **  ------------
27c94 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20  ---  ---------- 
27c95 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
27c96 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
27c97 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 61  ----------.**  a
27c98 6e 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ny              
27c99 52 4f 4c 4c 42 41 43 4b 20 20 20 20 20 54 68 65  ROLLBACK     The
27c9a 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
27c9b 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62  tion is rolled b
27c9c 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  ack and.**      
27c9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27c9e 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
27c9f 33 5f 65 78 65 63 28 29 20 72 65 74 75 72 6e 73  3_exec() returns
27ca0 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 77 69 74   immediately wit
27ca1 68 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  h a.**          
27ca2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27ca3 20 20 20 20 20 20 72 65 74 75 72 6e 20 63 6f 64        return cod
27ca4 65 20 6f 66 20 53 51 4c 49 54 45 5f 43 4f 4e 53  e of SQLITE_CONS
27ca5 54 52 41 49 4e 54 2e 0a 2a 2a 0a 2a 2a 20 20 61  TRAINT..**.**  a
27ca6 6e 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ny              
27ca7 41 42 4f 52 54 20 20 20 20 20 20 20 20 42 61 63  ABORT        Bac
27ca8 6b 20 6f 75 74 20 63 68 61 6e 67 65 73 20 66 72  k out changes fr
27ca9 6f 6d 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  om the current c
27caa 6f 6d 6d 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20  ommand.**       
27cab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27cac 20 20 20 20 20 20 20 20 20 6f 6e 6c 79 20 28 64           only (d
27cad 6f 20 6e 6f 74 20 64 6f 20 61 20 63 6f 6d 70 6c  o not do a compl
27cae 65 74 65 20 72 6f 6c 6c 62 61 63 6b 29 20 74 68  ete rollback) th
27caf 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  en.**           
27cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27cb1 20 20 20 20 20 63 61 75 73 65 20 73 71 6c 69 74       cause sqlit
27cb2 65 33 5f 65 78 65 63 28 29 20 74 6f 20 72 65 74  e3_exec() to ret
27cb3 75 72 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a  urn immediately.
27cb4 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
27cb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27cb6 20 20 77 69 74 68 20 53 51 4c 49 54 45 5f 43 4f    with SQLITE_CO
27cb7 4e 53 54 52 41 49 4e 54 2e 0a 2a 2a 0a 2a 2a 20  NSTRAINT..**.** 
27cb8 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20 20   any            
27cb9 20 20 46 41 49 4c 20 20 20 20 20 20 20 20 20 53    FAIL         S
27cba 71 6c 69 74 65 5f 65 78 65 63 28 29 20 72 65 74  qlite_exec() ret
27cbb 75 72 6e 73 20 69 6d 6d 65 64 69 61 74 65 6c 79  urns immediately
27cbc 20 77 69 74 68 20 61 0a 2a 2a 20 20 20 20 20 20   with a.**      
27cbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27cbe 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
27cbf 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f   code of SQLITE_
27cc0 43 4f 4e 53 54 52 41 49 4e 54 2e 20 20 54 68 65  CONSTRAINT.  The
27cc1 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
27cc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27cc3 20 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69     transaction i
27cc4 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63  s not rolled bac
27cc5 6b 20 61 6e 64 20 61 6e 79 0a 2a 2a 20 20 20 20  k and any.**    
27cc6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27cc7 20 20 20 20 20 20 20 20 20 20 20 20 70 72 69 6f              prio
27cc8 72 20 63 68 61 6e 67 65 73 20 61 72 65 20 72 65  r changes are re
27cc9 74 61 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 61  tained..**.**  a
27cca 6e 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ny              
27ccb 49 47 4e 4f 52 45 20 20 20 20 20 20 20 54 68 65  IGNORE       The
27ccc 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 61   record number a
27ccd 6e 64 20 64 61 74 61 20 69 73 20 70 6f 70 70 65  nd data is poppe
27cce 64 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 20 20 20  d from.**       
27ccf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27cd0 20 20 20 20 20 20 20 20 20 74 68 65 20 73 74 61           the sta
27cd1 63 6b 20 61 6e 64 20 74 68 65 72 65 20 69 73 20  ck and there is 
27cd2 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 6a 75 6d  an immediate jum
27cd3 70 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  p.**            
27cd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27cd5 20 20 20 20 74 6f 20 6c 61 62 65 6c 20 69 67 6e      to label ign
27cd6 6f 72 65 44 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 20  oreDest..**.**  
27cd7 4e 4f 54 20 4e 55 4c 4c 20 20 20 20 20 20 20 20  NOT NULL        
27cd8 20 52 45 50 4c 41 43 45 20 20 20 20 20 20 54 68   REPLACE      Th
27cd9 65 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20  e NULL value is 
27cda 72 65 70 6c 61 63 65 20 62 79 20 74 68 65 20 64  replace by the d
27cdb 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20  efault.**       
27cdc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27cdd 20 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 66           value f
27cde 6f 72 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 20  or that column. 
27cdf 20 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   If the default 
27ce0 76 61 6c 75 65 0a 2a 2a 20 20 20 20 20 20 20 20  value.**        
27ce1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27ce2 20 20 20 20 20 20 20 20 69 73 20 4e 55 4c 4c 2c          is NULL,
27ce3 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 74   the action is t
27ce4 68 65 20 73 61 6d 65 20 61 73 20 41 42 4f 52 54  he same as ABORT
27ce5 2e 0a 2a 2a 0a 2a 2a 20 20 55 4e 49 51 55 45 20  ..**.**  UNIQUE 
27ce6 20 20 20 20 20 20 20 20 20 20 52 45 50 4c 41 43            REPLAC
27ce7 45 20 20 20 20 20 20 54 68 65 20 6f 74 68 65 72  E      The other
27ce8 20 72 6f 77 20 74 68 61 74 20 63 6f 6e 66 6c 69   row that confli
27ce9 63 74 73 20 77 69 74 68 20 74 68 65 20 72 6f 77  cts with the row
27cea 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
27ceb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27cec 20 20 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65     being inserte
27ced 64 20 69 73 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a  d is removed..**
27cee 0a 2a 2a 20 20 43 48 45 43 4b 20 20 20 20 20 20  .**  CHECK      
27cef 20 20 20 20 20 20 52 45 50 4c 41 43 45 20 20 20        REPLACE   
27cf0 20 20 20 49 6c 6c 65 67 61 6c 2e 20 20 54 68 65     Illegal.  The
27cf1 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 20 65   results in an e
27cf2 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  xception..**.** 
27cf3 57 68 69 63 68 20 61 63 74 69 6f 6e 20 74 6f 20  Which action to 
27cf4 74 61 6b 65 20 69 73 20 64 65 74 65 72 6d 69 6e  take is determin
27cf5 65 64 20 62 79 20 74 68 65 20 6f 76 65 72 72 69  ed by the overri
27cf6 64 65 45 72 72 6f 72 20 70 61 72 61 6d 65 74 65  deError paramete
27cf7 72 2e 0a 2a 2a 20 4f 72 20 69 66 20 6f 76 65 72  r..** Or if over
27cf8 72 69 64 65 45 72 72 6f 72 3d 3d 4f 45 5f 44 65  rideError==OE_De
27cf9 66 61 75 6c 74 2c 20 74 68 65 6e 20 74 68 65 20  fault, then the 
27cfa 70 50 61 72 73 65 2d 3e 6f 6e 45 72 72 6f 72 20  pParse->onError 
27cfb 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69 73 20  parameter.** is 
27cfc 75 73 65 64 2e 20 20 4f 72 20 69 66 20 70 50 61  used.  Or if pPa
27cfd 72 73 65 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45  rse->onError==OE
27cfe 5f 44 65 66 61 75 6c 74 20 74 68 65 6e 20 74 68  _Default then th
27cff 65 20 6f 6e 45 72 72 6f 72 20 76 61 6c 75 65 0a  e onError value.
27d00 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 6e 73 74  ** for the const
27d01 72 61 69 6e 74 20 69 73 20 75 73 65 64 2e 0a 2a  raint is used..*
27d02 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67  *.** The calling
27d03 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6f 70   routine must op
27d04 65 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65 20  en a read/write 
27d05 63 75 72 73 6f 72 20 66 6f 72 20 70 54 61 62 20  cursor for pTab 
27d06 77 69 74 68 0a 2a 2a 20 63 75 72 73 6f 72 20 6e  with.** cursor n
27d07 75 6d 62 65 72 20 22 62 61 73 65 43 75 72 22 2e  umber "baseCur".
27d08 20 20 41 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66    All indices of
27d09 20 70 54 61 62 20 6d 75 73 74 20 61 6c 73 6f 20   pTab must also 
27d0a 68 61 76 65 20 6f 70 65 6e 0a 2a 2a 20 72 65 61  have open.** rea
27d0b 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 73 20  d/write cursors 
27d0c 77 69 74 68 20 63 75 72 73 6f 72 20 6e 75 6d 62  with cursor numb
27d0d 65 72 20 62 61 73 65 43 75 72 2b 69 20 66 6f 72  er baseCur+i for
27d0e 20 74 68 65 20 69 2d 74 68 20 63 75 72 73 6f 72   the i-th cursor
27d0f 2e 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 66 20  ..** Except, if 
27d10 74 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 73 73  there is no poss
27d11 69 62 69 6c 69 74 79 20 6f 66 20 61 20 52 45 50  ibility of a REP
27d12 4c 41 43 45 20 61 63 74 69 6f 6e 20 74 68 65 6e  LACE action then
27d13 0a 2a 2a 20 63 75 72 73 6f 72 73 20 64 6f 20 6e  .** cursors do n
27d14 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6f 70  ot need to be op
27d15 65 6e 20 66 6f 72 20 69 6e 64 69 63 65 73 20 77  en for indices w
27d16 68 65 72 65 20 61 52 65 67 49 64 78 5b 69 5d 3d  here aRegIdx[i]=
27d17 3d 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  =0..*/.SQLITE_PR
27d18 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
27d19 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72  e3GenerateConstr
27d1a 61 69 6e 74 43 68 65 63 6b 73 28 0a 20 20 50 61  aintChecks(.  Pa
27d1b 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
27d1c 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
27d1d 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62  context */.  Tab
27d1e 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20  le *pTab,       
27d1f 20 2f 2a 20 74 68 65 20 74 61 62 6c 65 20 69 6e   /* the table in
27d20 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 65 20  to which we are 
27d21 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20 69  inserting */.  i
27d22 6e 74 20 62 61 73 65 43 75 72 2c 20 20 20 20 20  nt baseCur,     
27d23 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61     /* Index of a
27d24 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73   read/write curs
27d25 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 70  or pointing at p
27d26 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  Tab */.  int reg
27d27 52 6f 77 69 64 2c 20 20 20 20 20 20 20 2f 2a 20  Rowid,       /* 
27d28 49 6e 64 65 78 20 6f 66 20 74 68 65 20 72 61 6e  Index of the ran
27d29 67 65 20 6f 66 20 69 6e 70 75 74 20 72 65 67 69  ge of input regi
27d2a 73 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 2a  sters */.  int *
27d2b 61 52 65 67 49 64 78 2c 20 20 20 20 20 20 20 2f  aRegIdx,       /
27d2c 2a 20 52 65 67 69 73 74 65 72 20 75 73 65 64 20  * Register used 
27d2d 62 79 20 65 61 63 68 20 69 6e 64 65 78 2e 20 20  by each index.  
27d2e 30 20 66 6f 72 20 75 6e 75 73 65 64 20 69 6e 64  0 for unused ind
27d2f 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 6f  ices */.  int ro
27d30 77 69 64 43 68 6e 67 2c 20 20 20 20 20 20 2f 2a  widChng,      /*
27d31 20 54 72 75 65 20 69 66 20 74 68 65 20 72 6f 77   True if the row
27d32 69 64 20 6d 69 67 68 74 20 63 6f 6c 6c 69 64 65  id might collide
27d33 20 77 69 74 68 20 65 78 69 73 74 69 6e 67 20 65   with existing e
27d34 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 73  ntry */.  int is
27d35 55 70 64 61 74 65 2c 20 20 20 20 20 20 20 2f 2a  Update,       /*
27d36 20 54 72 75 65 20 66 6f 72 20 55 50 44 41 54 45   True for UPDATE
27d37 2c 20 46 61 6c 73 65 20 66 6f 72 20 49 4e 53 45  , False for INSE
27d38 52 54 20 2a 2f 0a 20 20 69 6e 74 20 6f 76 65 72  RT */.  int over
27d39 72 69 64 65 45 72 72 6f 72 2c 20 20 2f 2a 20 4f  rideError,  /* O
27d3a 76 65 72 72 69 64 65 20 6f 6e 45 72 72 6f 72 20  verride onError 
27d3b 74 6f 20 74 68 69 73 20 69 66 20 6e 6f 74 20 4f  to this if not O
27d3c 45 5f 44 65 66 61 75 6c 74 20 2a 2f 0a 20 20 69  E_Default */.  i
27d3d 6e 74 20 69 67 6e 6f 72 65 44 65 73 74 2c 20 20  nt ignoreDest,  
27d3e 20 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68     /* Jump to th
27d3f 69 73 20 6c 61 62 65 6c 20 6f 6e 20 61 6e 20 4f  is label on an O
27d40 45 5f 49 67 6e 6f 72 65 20 72 65 73 6f 6c 75 74  E_Ignore resolut
27d41 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 62  ion */.  int *pb
27d42 4d 61 79 52 65 70 6c 61 63 65 20 20 20 2f 2a 20  MayReplace   /* 
27d43 4f 55 54 3a 20 53 65 74 20 74 6f 20 74 72 75 65  OUT: Set to true
27d44 20 69 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 6d   if constraint m
27d45 61 79 20 63 61 75 73 65 20 61 20 72 65 70 6c 61  ay cause a repla
27d46 63 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  ce */.){.  int i
27d47 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
27d48 2a 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * loop counter *
27d49 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  /.  Vdbe *v;    
27d4a 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20          /* VDBE 
27d4b 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 74 69 6f  under construtio
27d4c 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b  n */.  int nCol;
27d4d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
27d4e 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
27d4f 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72  */.  int onError
27d50 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66  ;        /* Conf
27d51 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20  lict resolution 
27d52 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20 69 6e  strategy */.  in
27d53 74 20 6a 31 3b 20 20 20 20 20 20 20 20 20 20 20  t j1;           
27d54 20 20 2f 2a 20 41 64 64 72 65 73 73 73 20 6f 66    /* Addresss of
27d55 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f   jump instructio
27d56 6e 20 2a 2f 0a 20 20 69 6e 74 20 6a 32 20 3d 20  n */.  int j2 = 
27d57 30 2c 20 6a 33 3b 20 20 20 20 20 2f 2a 20 41 64  0, j3;     /* Ad
27d58 64 72 65 73 73 65 73 20 6f 66 20 6a 75 6d 70 20  dresses of jump 
27d59 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a  instructions */.
27d5a 20 20 69 6e 74 20 72 65 67 44 61 74 61 3b 20 20    int regData;  
27d5b 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
27d5c 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 72  r containing fir
27d5d 73 74 20 64 61 74 61 20 63 6f 6c 75 6d 6e 20 2a  st data column *
27d5e 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20  /.  int iCur;   
27d5f 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
27d60 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 2a   cursor number *
27d61 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b  /.  Index *pIdx;
27d62 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
27d63 74 65 72 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68  ter to one of th
27d64 65 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69  e indices */.  i
27d65 6e 74 20 73 65 65 6e 52 65 70 6c 61 63 65 20 3d  nt seenReplace =
27d66 20 30 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 52   0; /* True if R
27d67 45 50 4c 41 43 45 20 69 73 20 75 73 65 64 20 74  EPLACE is used t
27d68 6f 20 72 65 73 6f 6c 76 65 20 49 4e 54 20 50 4b  o resolve INT PK
27d69 20 63 6f 6e 66 6c 69 63 74 20 2a 2f 0a 20 20 69   conflict */.  i
27d6a 6e 74 20 72 65 67 4f 6c 64 52 6f 77 69 64 20 3d  nt regOldRowid =
27d6b 20 28 72 6f 77 69 64 43 68 6e 67 20 26 26 20 69   (rowidChng && i
27d6c 73 55 70 64 61 74 65 29 20 3f 20 72 6f 77 69 64  sUpdate) ? rowid
27d6d 43 68 6e 67 20 3a 20 72 65 67 52 6f 77 69 64 3b  Chng : regRowid;
27d6e 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ..  v = sqlite3G
27d6f 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
27d70 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29    assert( v!=0 )
27d71 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  ;.  assert( pTab
27d72 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20  ->pSelect==0 ); 
27d73 20 2f 2a 20 54 68 69 73 20 74 61 62 6c 65 20 69   /* This table i
27d74 73 20 6e 6f 74 20 61 20 56 49 45 57 20 2a 2f 0a  s not a VIEW */.
27d75 20 20 6e 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e    nCol = pTab->n
27d76 43 6f 6c 3b 0a 20 20 72 65 67 44 61 74 61 20 3d  Col;.  regData =
27d77 20 72 65 67 52 6f 77 69 64 20 2b 20 31 3b 0a 0a   regRowid + 1;..
27d78 20 20 2f 2a 20 54 65 73 74 20 61 6c 6c 20 4e 4f    /* Test all NO
27d79 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e  T NULL constrain
27d7a 74 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  ts..  */.  for(i
27d7b 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
27d7c 7b 0a 20 20 20 20 69 66 28 20 69 3d 3d 70 54 61  {.    if( i==pTa
27d7d 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20  b->iPKey ){.    
27d7e 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
27d7f 7d 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20  }.    onError = 
27d80 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f  pTab->aCol[i].no
27d81 74 4e 75 6c 6c 3b 0a 20 20 20 20 69 66 28 20 6f  tNull;.    if( o
27d82 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20  nError==OE_None 
27d83 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
27d84 69 66 28 20 6f 76 65 72 72 69 64 65 45 72 72 6f  if( overrideErro
27d85 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b  r!=OE_Default ){
27d86 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d  .      onError =
27d87 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 3b 0a   overrideError;.
27d88 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 6e      }else if( on
27d89 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c  Error==OE_Defaul
27d8a 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72  t ){.      onErr
27d8b 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20  or = OE_Abort;. 
27d8c 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6f 6e 45     }.    if( onE
27d8d 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65  rror==OE_Replace
27d8e 20 26 26 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69   && pTab->aCol[i
27d8f 5d 2e 70 44 66 6c 74 3d 3d 30 20 29 7b 0a 20 20  ].pDflt==0 ){.  
27d90 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45      onError = OE
27d91 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20  _Abort;.    }.  
27d92 20 20 61 73 73 65 72 74 28 20 6f 6e 45 72 72 6f    assert( onErro
27d93 72 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 7c  r==OE_Rollback |
27d94 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 41 62  | onError==OE_Ab
27d95 6f 72 74 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d  ort || onError==
27d96 4f 45 5f 46 61 69 6c 0a 20 20 20 20 20 20 20 20  OE_Fail.        
27d97 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 49  || onError==OE_I
27d98 67 6e 6f 72 65 20 7c 7c 20 6f 6e 45 72 72 6f 72  gnore || onError
27d99 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 3b 0a  ==OE_Replace );.
27d9a 20 20 20 20 73 77 69 74 63 68 28 20 6f 6e 45 72      switch( onEr
27d9b 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 63 61 73  ror ){.      cas
27d9c 65 20 4f 45 5f 41 62 6f 72 74 3a 0a 20 20 20 20  e OE_Abort:.    
27d9d 20 20 20 20 73 71 6c 69 74 65 33 4d 61 79 41 62      sqlite3MayAb
27d9e 6f 72 74 28 70 50 61 72 73 65 29 3b 0a 20 20 20  ort(pParse);.   
27d9f 20 20 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62     case OE_Rollb
27da0 61 63 6b 3a 0a 20 20 20 20 20 20 63 61 73 65 20  ack:.      case 
27da1 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20 20 20 20 20  OE_Fail: {.     
27da2 20 20 20 63 68 61 72 20 2a 7a 4d 73 67 3b 0a 20     char *zMsg;. 
27da3 20 20 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69         j1 = sqli
27da4 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
27da5 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 2c 0a   OP_HaltIfNull,.
27da6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27da7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27da8 20 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41    SQLITE_CONSTRA
27da9 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c 20 72 65  INT, onError, re
27daa 67 44 61 74 61 2b 69 29 3b 0a 20 20 20 20 20 20  gData+i);.      
27dab 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33    zMsg = sqlite3
27dac 4d 50 72 69 6e 74 66 28 70 50 61 72 73 65 2d 3e  MPrintf(pParse->
27dad 64 62 2c 20 22 25 73 2e 25 73 20 6d 61 79 20 6e  db, "%s.%s may n
27dae 6f 74 20 62 65 20 4e 55 4c 4c 22 2c 0a 20 20 20  ot be NULL",.   
27daf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27db0 20 20 20 20 20 20 20 20 20 20 20 70 54 61 62 2d             pTab-
27db1 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43  >zName, pTab->aC
27db2 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20  ol[i].zName);.  
27db3 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27db4 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
27db5 20 7a 4d 73 67 2c 20 50 34 5f 44 59 4e 41 4d 49   zMsg, P4_DYNAMI
27db6 43 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  C);.        brea
27db7 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
27db8 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65 3a   case OE_Ignore:
27db9 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   {.        sqlit
27dba 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
27dbb 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 44 61  OP_IsNull, regDa
27dbc 74 61 2b 69 2c 20 69 67 6e 6f 72 65 44 65 73 74  ta+i, ignoreDest
27dbd 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
27dbe 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
27dbf 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
27dc0 20 20 20 61 73 73 65 72 74 28 20 6f 6e 45 72 72     assert( onErr
27dc1 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29  or==OE_Replace )
27dc2 3b 0a 20 20 20 20 20 20 20 20 6a 31 20 3d 20 73  ;.        j1 = s
27dc3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
27dc4 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20  (v, OP_NotNull, 
27dc5 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20 20 20  regData+i);.    
27dc6 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
27dc7 6f 64 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  ode(pParse, pTab
27dc8 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c  ->aCol[i].pDflt,
27dc9 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20 20   regData+i);.   
27dca 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
27dcb 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b  JumpHere(v, j1);
27dcc 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
27dcd 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
27dce 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20 61 6c 6c  }..  /* Test all
27dcf 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e   CHECK constrain
27dd0 74 73 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  ts.  */.#ifndef 
27dd1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43  SQLITE_OMIT_CHEC
27dd2 4b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 43  K.  if( pTab->pC
27dd3 68 65 63 6b 20 26 26 20 28 70 50 61 72 73 65 2d  heck && (pParse-
27dd4 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  >db->flags & SQL
27dd5 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73  ITE_IgnoreChecks
27dd6 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  )==0 ){.    int 
27dd7 61 6c 6c 4f 6b 20 3d 20 73 71 6c 69 74 65 33 56  allOk = sqlite3V
27dd8 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
27dd9 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6b 42  .    pParse->ckB
27dda 61 73 65 20 3d 20 72 65 67 44 61 74 61 3b 0a 20  ase = regData;. 
27ddb 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
27ddc 54 72 75 65 28 70 50 61 72 73 65 2c 20 70 54 61  True(pParse, pTa
27ddd 62 2d 3e 70 43 68 65 63 6b 2c 20 61 6c 6c 4f 6b  b->pCheck, allOk
27dde 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  , SQLITE_JUMPIFN
27ddf 55 4c 4c 29 3b 0a 20 20 20 20 6f 6e 45 72 72 6f  ULL);.    onErro
27de0 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72 72 6f  r = overrideErro
27de1 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20 3f 20  r!=OE_Default ? 
27de2 6f 76 65 72 72 69 64 65 45 72 72 6f 72 20 3a 20  overrideError : 
27de3 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66  OE_Abort;.    if
27de4 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 49 67  ( onError==OE_Ig
27de5 6e 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 73 71  nore ){.      sq
27de6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27de7 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69  v, OP_Goto, 0, i
27de8 67 6e 6f 72 65 44 65 73 74 29 3b 0a 20 20 20 20  gnoreDest);.    
27de9 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
27dea 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69  ite3HaltConstrai
27deb 6e 74 28 70 50 61 72 73 65 2c 20 6f 6e 45 72 72  nt(pParse, onErr
27dec 6f 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d  or, 0, 0);.    }
27ded 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27dee 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
27def 61 6c 6c 4f 6b 29 3b 0a 20 20 7d 0a 23 65 6e 64  allOk);.  }.#end
27df0 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
27df1 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b  QLITE_OMIT_CHECK
27df2 29 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 77 65  ) */..  /* If we
27df3 20 68 61 76 65 20 61 6e 20 49 4e 54 45 47 45 52   have an INTEGER
27df4 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 6d 61   PRIMARY KEY, ma
27df5 6b 65 20 73 75 72 65 20 74 68 65 20 70 72 69 6d  ke sure the prim
27df6 61 72 79 20 6b 65 79 0a 20 20 2a 2a 20 6f 66 20  ary key.  ** of 
27df7 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 64  the new record d
27df8 6f 65 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73  oes not previous
27df9 6c 79 20 65 78 69 73 74 2e 20 20 45 78 63 65 70  ly exist.  Excep
27dfa 74 2c 20 69 66 20 74 68 69 73 0a 20 20 2a 2a 20  t, if this.  ** 
27dfb 69 73 20 61 6e 20 55 50 44 41 54 45 20 61 6e 64  is an UPDATE and
27dfc 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79   the primary key
27dfd 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 69 6e 67   is not changing
27dfe 2c 20 74 68 61 74 20 69 73 20 4f 4b 2e 0a 20 20  , that is OK..  
27dff 2a 2f 0a 20 20 69 66 28 20 72 6f 77 69 64 43 68  */.  if( rowidCh
27e00 6e 67 20 29 7b 0a 20 20 20 20 6f 6e 45 72 72 6f  ng ){.    onErro
27e01 72 20 3d 20 70 54 61 62 2d 3e 6b 65 79 43 6f 6e  r = pTab->keyCon
27e02 66 3b 0a 20 20 20 20 69 66 28 20 6f 76 65 72 72  f;.    if( overr
27e03 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66  ideError!=OE_Def
27e04 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e  ault ){.      on
27e05 45 72 72 6f 72 20 3d 20 6f 76 65 72 72 69 64 65  Error = override
27e06 45 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65  Error;.    }else
27e07 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45   if( onError==OE
27e08 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20  _Default ){.    
27e09 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41    onError = OE_A
27e0a 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  bort;.    }.    
27e0b 0a 20 20 20 20 69 66 28 20 69 73 55 70 64 61 74  .    if( isUpdat
27e0c 65 20 29 7b 0a 20 20 20 20 20 20 6a 32 20 3d 20  e ){.      j2 = 
27e0d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27e0e 33 28 76 2c 20 4f 50 5f 45 71 2c 20 72 65 67 52  3(v, OP_Eq, regR
27e0f 6f 77 69 64 2c 20 30 2c 20 72 6f 77 69 64 43 68  owid, 0, rowidCh
27e10 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6a  ng);.    }.    j
27e11 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  3 = sqlite3VdbeA
27e12 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45  ddOp3(v, OP_NotE
27e13 78 69 73 74 73 2c 20 62 61 73 65 43 75 72 2c 20  xists, baseCur, 
27e14 30 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  0, regRowid);.  
27e15 20 20 73 77 69 74 63 68 28 20 6f 6e 45 72 72 6f    switch( onErro
27e16 72 20 29 7b 0a 20 20 20 20 20 20 64 65 66 61 75  r ){.      defau
27e17 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 6f 6e  lt: {.        on
27e18 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74  Error = OE_Abort
27e19 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c  ;.        /* Fal
27e1a 6c 20 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20  l thru into the 
27e1b 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20 20 20  next case */.   
27e1c 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20     }.      case 
27e1d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 0a 20 20 20  OE_Rollback:.   
27e1e 20 20 20 63 61 73 65 20 4f 45 5f 41 62 6f 72 74     case OE_Abort
27e1f 3a 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f  :.      case OE_
27e20 46 61 69 6c 3a 20 7b 0a 20 20 20 20 20 20 20 20  Fail: {.        
27e21 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74  sqlite3HaltConst
27e22 72 61 69 6e 74 28 0a 20 20 20 20 20 20 20 20 20  raint(.         
27e23 20 70 50 61 72 73 65 2c 20 6f 6e 45 72 72 6f 72   pParse, onError
27e24 2c 20 22 50 52 49 4d 41 52 59 20 4b 45 59 20 6d  , "PRIMARY KEY m
27e25 75 73 74 20 62 65 20 75 6e 69 71 75 65 22 2c 20  ust be unique", 
27e26 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
27e27 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
27e28 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45   }.      case OE
27e29 5f 52 65 70 6c 61 63 65 3a 20 7b 0a 20 20 20 20  _Replace: {.    
27e2a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
27e2b 61 72 65 20 44 45 4c 45 54 45 20 74 72 69 67 67  are DELETE trigg
27e2c 65 72 73 20 6f 6e 20 74 68 69 73 20 74 61 62 6c  ers on this tabl
27e2d 65 20 61 6e 64 20 74 68 65 0a 20 20 20 20 20 20  e and the.      
27e2e 20 20 2a 2a 20 72 65 63 75 72 73 69 76 65 2d 74    ** recursive-t
27e2f 72 69 67 67 65 72 73 20 66 6c 61 67 20 69 73 20  riggers flag is 
27e30 73 65 74 2c 20 63 61 6c 6c 20 47 65 6e 65 72 61  set, call Genera
27e31 74 65 52 6f 77 44 65 6c 65 74 65 28 29 20 74 6f  teRowDelete() to
27e32 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 6d 6f  .        ** remo
27e33 76 65 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 69  ve the conflicti
27e34 6e 67 20 72 6f 77 20 66 72 6f 6d 20 74 68 65 20  ng row from the 
27e35 74 68 65 20 74 61 62 6c 65 2e 20 54 68 69 73 20  the table. This 
27e36 77 69 6c 6c 20 66 69 72 65 0a 20 20 20 20 20 20  will fire.      
27e37 20 20 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72    ** the trigger
27e38 73 20 61 6e 64 20 72 65 6d 6f 76 65 20 62 6f 74  s and remove bot
27e39 68 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20  h the table and 
27e3a 69 6e 64 65 78 20 62 2d 74 72 65 65 20 65 6e 74  index b-tree ent
27e3b 72 69 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2a  ries..        **
27e3c 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 74 68 65  .        ** Othe
27e3d 72 77 69 73 65 2c 20 69 66 20 74 68 65 72 65 20  rwise, if there 
27e3e 61 72 65 20 6e 6f 20 74 72 69 67 67 65 72 73 20  are no triggers 
27e3f 6f 72 20 74 68 65 20 72 65 63 75 72 73 69 76 65  or the recursive
27e40 2d 74 72 69 67 67 65 72 73 0a 20 20 20 20 20 20  -triggers.      
27e41 20 20 2a 2a 20 66 6c 61 67 20 69 73 20 6e 6f 74    ** flag is not
27e42 20 73 65 74 2c 20 63 61 6c 6c 20 47 65 6e 65 72   set, call Gener
27e43 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74  ateRowIndexDelet
27e44 65 28 29 2e 20 54 68 69 73 20 72 65 6d 6f 76 65  e(). This remove
27e45 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65  s.        ** the
27e46 20 69 6e 64 65 78 20 62 2d 74 72 65 65 20 65 6e   index b-tree en
27e47 74 72 69 65 73 20 6f 6e 6c 79 2e 20 54 68 65 20  tries only. The 
27e48 74 61 62 6c 65 20 62 2d 74 72 65 65 20 65 6e 74  table b-tree ent
27e49 72 79 20 77 69 6c 6c 20 62 65 20 0a 20 20 20 20  ry will be .    
27e4a 20 20 20 20 2a 2a 20 72 65 70 6c 61 63 65 64 20      ** replaced 
27e4b 62 79 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79  by the new entry
27e4c 20 77 68 65 6e 20 69 74 20 69 73 20 69 6e 73 65   when it is inse
27e4d 72 74 65 64 2e 20 20 2a 2f 0a 20 20 20 20 20 20  rted.  */.      
27e4e 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
27e4f 67 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ger = 0;.       
27e50 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d   if( pParse->db-
27e51 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65  >flags&SQLITE_Re
27e52 63 54 72 69 67 67 65 72 73 20 29 7b 0a 20 20 20  cTriggers ){.   
27e53 20 20 20 20 20 20 20 70 54 72 69 67 67 65 72 20         pTrigger 
27e54 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  = sqlite3Trigger
27e55 73 45 78 69 73 74 28 70 50 61 72 73 65 2c 20 70  sExist(pParse, p
27e56 54 61 62 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20  Tab, TK_DELETE, 
27e57 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d  0, 0);.        }
27e58 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
27e59 4d 75 6c 74 69 57 72 69 74 65 28 70 50 61 72 73  MultiWrite(pPars
27e5a 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  e);.        if( 
27e5b 70 54 72 69 67 67 65 72 20 7c 7c 20 73 71 6c 69  pTrigger || sqli
27e5c 74 65 33 46 6b 52 65 71 75 69 72 65 64 28 70 50  te3FkRequired(pP
27e5d 61 72 73 65 2c 20 70 54 61 62 2c 20 30 2c 20 30  arse, pTab, 0, 0
27e5e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  ) ){.          s
27e5f 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f  qlite3GenerateRo
27e60 77 44 65 6c 65 74 65 28 0a 20 20 20 20 20 20 20  wDelete(.       
27e61 20 20 20 20 20 20 20 70 50 61 72 73 65 2c 20 70         pParse, p
27e62 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 72 65  Tab, baseCur, re
27e63 67 52 6f 77 69 64 2c 20 30 2c 20 70 54 72 69 67  gRowid, 0, pTrig
27e64 67 65 72 2c 20 4f 45 5f 52 65 70 6c 61 63 65 0a  ger, OE_Replace.
27e65 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20            );.   
27e66 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
27e67 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e        sqlite3Gen
27e68 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c  erateRowIndexDel
27e69 65 74 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  ete(pParse, pTab
27e6a 2c 20 62 61 73 65 43 75 72 2c 20 30 29 3b 0a 20  , baseCur, 0);. 
27e6b 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
27e6c 20 73 65 65 6e 52 65 70 6c 61 63 65 20 3d 20 31   seenReplace = 1
27e6d 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
27e6e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
27e6f 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b  ase OE_Ignore: {
27e70 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
27e71 20 73 65 65 6e 52 65 70 6c 61 63 65 3d 3d 30 20   seenReplace==0 
27e72 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
27e73 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
27e74 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f  OP_Goto, 0, igno
27e75 72 65 44 65 73 74 29 3b 0a 20 20 20 20 20 20 20  reDest);.       
27e76 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
27e77 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
27e78 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
27e79 20 6a 33 29 3b 0a 20 20 20 20 69 66 28 20 69 73   j3);.    if( is
27e7a 55 70 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20  Update ){.      
27e7b 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
27e7c 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20 20 20 20  ere(v, j2);.    
27e7d 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74  }.  }..  /* Test
27e7e 20 61 6c 6c 20 55 4e 49 51 55 45 20 63 6f 6e 73   all UNIQUE cons
27e7f 74 72 61 69 6e 74 73 20 62 79 20 63 72 65 61 74  traints by creat
27e80 69 6e 67 20 65 6e 74 72 69 65 73 20 66 6f 72 20  ing entries for 
27e81 65 61 63 68 20 55 4e 49 51 55 45 0a 20 20 2a 2a  each UNIQUE.  **
27e82 20 69 6e 64 65 78 20 61 6e 64 20 6d 61 6b 69 6e   index and makin
27e83 67 20 73 75 72 65 20 74 68 61 74 20 64 75 70 6c  g sure that dupl
27e84 69 63 61 74 65 20 65 6e 74 72 69 65 73 20 64 6f  icate entries do
27e85 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69   not already exi
27e86 73 74 2e 0a 20 20 2a 2a 20 41 64 64 20 74 68 65  st..  ** Add the
27e87 20 6e 65 77 20 72 65 63 6f 72 64 73 20 74 6f 20   new records to 
27e88 74 68 65 20 69 6e 64 69 63 65 73 20 61 73 20 77  the indices as w
27e89 65 20 67 6f 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  e go..  */.  for
27e8a 28 69 43 75 72 3d 30 2c 20 70 49 64 78 3d 70 54  (iCur=0, pIdx=pT
27e8b 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78  ab->pIndex; pIdx
27e8c 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65  ; pIdx=pIdx->pNe
27e8d 78 74 2c 20 69 43 75 72 2b 2b 29 7b 0a 20 20 20  xt, iCur++){.   
27e8e 20 69 6e 74 20 72 65 67 49 64 78 3b 0a 20 20 20   int regIdx;.   
27e8f 20 69 6e 74 20 72 65 67 52 3b 0a 0a 20 20 20 20   int regR;..    
27e90 69 66 28 20 61 52 65 67 49 64 78 5b 69 43 75 72  if( aRegIdx[iCur
27e91 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  ]==0 ) continue;
27e92 20 20 2f 2a 20 53 6b 69 70 20 75 6e 75 73 65 64    /* Skip unused
27e93 20 69 6e 64 69 63 65 73 20 2a 2f 0a 0a 20 20 20   indices */..   
27e94 20 2f 2a 20 43 72 65 61 74 65 20 61 20 6b 65 79   /* Create a key
27e95 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74   for accessing t
27e96 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 2a  he index entry *
27e97 2f 0a 20 20 20 20 72 65 67 49 64 78 20 3d 20 73  /.    regIdx = s
27e98 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e  qlite3GetTempRan
27e99 67 65 28 70 50 61 72 73 65 2c 20 70 49 64 78 2d  ge(pParse, pIdx-
27e9a 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 20 20 20  >nColumn+1);.   
27e9b 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78   for(i=0; i<pIdx
27e9c 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  ->nColumn; i++){
27e9d 0a 20 20 20 20 20 20 69 6e 74 20 69 64 78 20 3d  .      int idx =
27e9e 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b   pIdx->aiColumn[
27e9f 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 69 64  i];.      if( id
27ea0 78 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29  x==pTab->iPKey )
27ea1 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
27ea2 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
27ea3 50 5f 53 43 6f 70 79 2c 20 72 65 67 52 6f 77 69  P_SCopy, regRowi
27ea4 64 2c 20 72 65 67 49 64 78 2b 69 29 3b 0a 20 20  d, regIdx+i);.  
27ea5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
27ea6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27ea7 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79  dOp2(v, OP_SCopy
27ea8 2c 20 72 65 67 44 61 74 61 2b 69 64 78 2c 20 72  , regData+idx, r
27ea9 65 67 49 64 78 2b 69 29 3b 0a 20 20 20 20 20 20  egIdx+i);.      
27eaa 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
27eab 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27eac 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 52 6f   OP_SCopy, regRo
27ead 77 69 64 2c 20 72 65 67 49 64 78 2b 69 29 3b 0a  wid, regIdx+i);.
27eae 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27eaf 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp3(v, OP_Make
27eb0 52 65 63 6f 72 64 2c 20 72 65 67 49 64 78 2c 20  Record, regIdx, 
27eb1 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 2c  pIdx->nColumn+1,
27eb2 20 61 52 65 67 49 64 78 5b 69 43 75 72 5d 29 3b   aRegIdx[iCur]);
27eb3 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27eb4 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20  ChangeP4(v, -1, 
27eb5 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69  sqlite3IndexAffi
27eb6 6e 69 74 79 53 74 72 28 76 2c 20 70 49 64 78 29  nityStr(v, pIdx)
27eb7 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
27eb8 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69  3ExprCacheAffini
27eb9 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c  tyChange(pParse,
27eba 20 72 65 67 49 64 78 2c 20 70 49 64 78 2d 3e 6e   regIdx, pIdx->n
27ebb 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 0a 20 20 20 20  Column+1);..    
27ebc 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77 68 61 74  /* Find out what
27ebd 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 20   action to take 
27ebe 69 6e 20 63 61 73 65 20 74 68 65 72 65 20 69 73  in case there is
27ebf 20 61 6e 20 69 6e 64 65 78 69 6e 67 20 63 6f 6e   an indexing con
27ec0 66 6c 69 63 74 20 2a 2f 0a 20 20 20 20 6f 6e 45  flict */.    onE
27ec1 72 72 6f 72 20 3d 20 70 49 64 78 2d 3e 6f 6e 45  rror = pIdx->onE
27ec2 72 72 6f 72 3b 0a 20 20 20 20 69 66 28 20 6f 6e  rror;.    if( on
27ec3 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 29  Error==OE_None )
27ec4 7b 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  { .      sqlite3
27ec5 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65  ReleaseTempRange
27ec6 28 70 50 61 72 73 65 2c 20 72 65 67 49 64 78 2c  (pParse, regIdx,
27ec7 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31   pIdx->nColumn+1
27ec8 29 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75  );.      continu
27ec9 65 3b 20 20 2f 2a 20 70 49 64 78 20 69 73 20 6e  e;  /* pIdx is n
27eca 6f 74 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65  ot a UNIQUE inde
27ecb 78 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69  x */.    }.    i
27ecc 66 28 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72  f( overrideError
27ecd 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a  !=OE_Default ){.
27ece 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20        onError = 
27ecf 6f 76 65 72 72 69 64 65 45 72 72 6f 72 3b 0a 20  overrideError;. 
27ed0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 6e 45     }else if( onE
27ed1 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74  rror==OE_Default
27ed2 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f   ){.      onErro
27ed3 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20  r = OE_Abort;.  
27ed4 20 20 7d 0a 20 20 20 20 69 66 28 20 73 65 65 6e    }.    if( seen
27ed5 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 20  Replace ){.     
27ed6 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45   if( onError==OE
27ed7 5f 49 67 6e 6f 72 65 20 29 20 6f 6e 45 72 72 6f  _Ignore ) onErro
27ed8 72 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b 0a  r = OE_Replace;.
27ed9 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 6f        else if( o
27eda 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c 20  nError==OE_Fail 
27edb 29 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41  ) onError = OE_A
27edc 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  bort;.    }.    
27edd 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f  .    /* Check to
27ede 20 73 65 65 20 69 66 20 74 68 65 20 6e 65 77 20   see if the new 
27edf 69 6e 64 65 78 20 65 6e 74 72 79 20 77 69 6c 6c  index entry will
27ee0 20 62 65 20 75 6e 69 71 75 65 20 2a 2f 0a 20 20   be unique */.  
27ee1 20 20 72 65 67 52 20 3d 20 73 71 6c 69 74 65 33    regR = sqlite3
27ee2 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
27ee3 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  e);.    sqlite3V
27ee4 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
27ee5 53 43 6f 70 79 2c 20 72 65 67 4f 6c 64 52 6f 77  SCopy, regOldRow
27ee6 69 64 2c 20 72 65 67 52 29 3b 0a 20 20 20 20 6a  id, regR);.    j
27ee7 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  3 = sqlite3VdbeA
27ee8 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 73 55 6e  ddOp4(v, OP_IsUn
27ee9 69 71 75 65 2c 20 62 61 73 65 43 75 72 2b 69 43  ique, baseCur+iC
27eea 75 72 2b 31 2c 20 30 2c 0a 20 20 20 20 20 20 20  ur+1, 0,.       
27eeb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27eec 20 20 20 20 72 65 67 52 2c 20 53 51 4c 49 54 45      regR, SQLITE
27eed 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 72 65 67 49  _INT_TO_PTR(regI
27eee 64 78 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  dx),.           
27eef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27ef0 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20 73  P4_INT32);.    s
27ef1 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
27ef2 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  pRange(pParse, r
27ef3 65 67 49 64 78 2c 20 70 49 64 78 2d 3e 6e 43 6f  egIdx, pIdx->nCo
27ef4 6c 75 6d 6e 2b 31 29 3b 0a 0a 20 20 20 20 2f 2a  lumn+1);..    /*
27ef5 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
27ef6 68 61 74 20 65 78 65 63 75 74 65 73 20 69 66 20  hat executes if 
27ef7 74 68 65 20 6e 65 77 20 69 6e 64 65 78 20 65 6e  the new index en
27ef8 74 72 79 20 69 73 20 6e 6f 74 20 75 6e 69 71 75  try is not uniqu
27ef9 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  e */.    assert(
27efa 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 6f 6c   onError==OE_Rol
27efb 6c 62 61 63 6b 20 7c 7c 20 6f 6e 45 72 72 6f 72  lback || onError
27efc 3d 3d 4f 45 5f 41 62 6f 72 74 20 7c 7c 20 6f 6e  ==OE_Abort || on
27efd 45 72 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c 0a 20  Error==OE_Fail. 
27efe 20 20 20 20 20 20 20 7c 7c 20 6f 6e 45 72 72 6f         || onErro
27eff 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 7c 7c 20  r==OE_Ignore || 
27f00 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c  onError==OE_Repl
27f01 61 63 65 20 29 3b 0a 20 20 20 20 73 77 69 74 63  ace );.    switc
27f02 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20  h( onError ){.  
27f03 20 20 20 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c      case OE_Roll
27f04 62 61 63 6b 3a 0a 20 20 20 20 20 20 63 61 73 65  back:.      case
27f05 20 4f 45 5f 41 62 6f 72 74 3a 0a 20 20 20 20 20   OE_Abort:.     
27f06 20 63 61 73 65 20 4f 45 5f 46 61 69 6c 3a 20 7b   case OE_Fail: {
27f07 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a  .        int j;.
27f08 20 20 20 20 20 20 20 20 53 74 72 41 63 63 75 6d          StrAccum
27f09 20 65 72 72 4d 73 67 3b 0a 20 20 20 20 20 20 20   errMsg;.       
27f0a 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 65   const char *zSe
27f0b 70 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  p;.        char 
27f0c 2a 7a 45 72 72 3b 0a 0a 20 20 20 20 20 20 20 20  *zErr;..        
27f0d 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49  sqlite3StrAccumI
27f0e 6e 69 74 28 26 65 72 72 4d 73 67 2c 20 30 2c 20  nit(&errMsg, 0, 
27f0f 30 2c 20 32 30 30 29 3b 0a 20 20 20 20 20 20 20  0, 200);.       
27f10 20 65 72 72 4d 73 67 2e 64 62 20 3d 20 70 50 61   errMsg.db = pPa
27f11 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 20  rse->db;.       
27f12 20 7a 53 65 70 20 3d 20 70 49 64 78 2d 3e 6e 43   zSep = pIdx->nC
27f13 6f 6c 75 6d 6e 3e 31 20 3f 20 22 63 6f 6c 75 6d  olumn>1 ? "colum
27f14 6e 73 20 22 20 3a 20 22 63 6f 6c 75 6d 6e 20 22  ns " : "column "
27f15 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  ;.        for(j=
27f16 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75  0; j<pIdx->nColu
27f17 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; j++){.      
27f18 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d      char *zCol =
27f19 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 49 64 78   pTab->aCol[pIdx
27f1a 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 5d 2e 7a  ->aiColumn[j]].z
27f1b 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20  Name;.          
27f1c 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41  sqlite3StrAccumA
27f1d 70 70 65 6e 64 28 26 65 72 72 4d 73 67 2c 20 7a  ppend(&errMsg, z
27f1e 53 65 70 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  Sep, -1);.      
27f1f 20 20 20 20 7a 53 65 70 20 3d 20 22 2c 20 22 3b      zSep = ", ";
27f20 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
27f21 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
27f22 28 26 65 72 72 4d 73 67 2c 20 7a 43 6f 6c 2c 20  (&errMsg, zCol, 
27f23 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  -1);.        }. 
27f24 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
27f25 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26 65 72  rAccumAppend(&er
27f26 72 4d 73 67 2c 0a 20 20 20 20 20 20 20 20 20 20  rMsg,.          
27f27 20 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e    pIdx->nColumn>
27f28 31 20 3f 20 22 20 61 72 65 20 6e 6f 74 20 75 6e  1 ? " are not un
27f29 69 71 75 65 22 20 3a 20 22 20 69 73 20 6e 6f 74  ique" : " is not
27f2a 20 75 6e 69 71 75 65 22 2c 20 2d 31 29 3b 0a 20   unique", -1);. 
27f2b 20 20 20 20 20 20 20 7a 45 72 72 20 3d 20 73 71         zErr = sq
27f2c 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e  lite3StrAccumFin
27f2d 69 73 68 28 26 65 72 72 4d 73 67 29 3b 0a 20 20  ish(&errMsg);.  
27f2e 20 20 20 20 20 20 73 71 6c 69 74 65 33 48 61 6c        sqlite3Hal
27f2f 74 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 72  tConstraint(pPar
27f30 73 65 2c 20 6f 6e 45 72 72 6f 72 2c 20 7a 45 72  se, onError, zEr
27f31 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  r, 0);.        s
27f32 71 6c 69 74 65 33 44 62 46 72 65 65 28 65 72 72  qlite3DbFree(err
27f33 4d 73 67 2e 64 62 2c 20 7a 45 72 72 29 3b 0a 20  Msg.db, zErr);. 
27f34 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
27f35 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
27f36 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20 20   OE_Ignore: {.  
27f37 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 65        assert( se
27f38 65 6e 52 65 70 6c 61 63 65 3d 3d 30 20 29 3b 0a  enReplace==0 );.
27f39 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
27f3a 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
27f3b 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65 44  Goto, 0, ignoreD
27f3c 65 73 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72  est);.        br
27f3d 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
27f3e 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
27f3f 20 20 20 20 20 20 54 72 69 67 67 65 72 20 2a 70        Trigger *p
27f40 54 72 69 67 67 65 72 20 3d 20 30 3b 0a 20 20 20  Trigger = 0;.   
27f41 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e 45       assert( onE
27f42 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65  rror==OE_Replace
27f43 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   );.        sqli
27f44 74 65 33 4d 75 6c 74 69 57 72 69 74 65 28 70 50  te3MultiWrite(pP
27f45 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 69  arse);.        i
27f46 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66  f( pParse->db->f
27f47 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 63 54  lags&SQLITE_RecT
27f48 72 69 67 67 65 72 73 20 29 7b 0a 20 20 20 20 20  riggers ){.     
27f49 20 20 20 20 20 70 54 72 69 67 67 65 72 20 3d 20       pTrigger = 
27f4a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 73 45  sqlite3TriggersE
27f4b 78 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61  xist(pParse, pTa
27f4c 62 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c  b, TK_DELETE, 0,
27f4d 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   0);.        }. 
27f4e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 65         sqlite3Ge
27f4f 6e 65 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28  nerateRowDelete(
27f50 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61  .            pPa
27f51 72 73 65 2c 20 70 54 61 62 2c 20 62 61 73 65 43  rse, pTab, baseC
27f52 75 72 2c 20 72 65 67 52 2c 20 30 2c 20 70 54 72  ur, regR, 0, pTr
27f53 69 67 67 65 72 2c 20 4f 45 5f 52 65 70 6c 61 63  igger, OE_Replac
27f54 65 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20  e.        );.   
27f55 20 20 20 20 20 73 65 65 6e 52 65 70 6c 61 63 65       seenReplace
27f56 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72   = 1;.        br
27f57 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
27f58 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
27f59 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 33  beJumpHere(v, j3
27f5a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  );.    sqlite3Re
27f5b 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
27f5c 72 73 65 2c 20 72 65 67 52 29 3b 0a 20 20 7d 0a  rse, regR);.  }.
27f5d 20 20 0a 20 20 69 66 28 20 70 62 4d 61 79 52 65    .  if( pbMayRe
27f5e 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 2a 70 62  place ){.    *pb
27f5f 4d 61 79 52 65 70 6c 61 63 65 20 3d 20 73 65 65  MayReplace = see
27f60 6e 52 65 70 6c 61 63 65 3b 0a 20 20 7d 0a 7d 0a  nReplace;.  }.}.
27f61 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
27f62 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f  ine generates co
27f63 64 65 20 74 6f 20 66 69 6e 69 73 68 20 74 68 65  de to finish the
27f64 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41 54   INSERT or UPDAT
27f65 45 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 74  E operation.** t
27f66 68 61 74 20 77 61 73 20 73 74 61 72 74 65 64 20  hat was started 
27f67 62 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20  by a prior call 
27f68 74 6f 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61  to sqlite3Genera
27f69 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63  teConstraintChec
27f6a 6b 73 2e 0a 2a 2a 20 41 20 63 6f 6e 73 65 63 75  ks..** A consecu
27f6b 74 69 76 65 20 72 61 6e 67 65 20 6f 66 20 72 65  tive range of re
27f6c 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e 67  gisters starting
27f6d 20 61 74 20 72 65 67 52 6f 77 69 64 20 63 6f 6e   at regRowid con
27f6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 72 6f 77  tains the.** row
27f6f 69 64 20 61 6e 64 20 74 68 65 20 63 6f 6e 74 65  id and the conte
27f70 6e 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  nt to be inserte
27f71 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 67  d..**.** The arg
27f72 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 72  uments to this r
27f73 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65  outine should be
27f74 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
27f75 20 66 69 72 73 74 20 73 69 78 0a 2a 2a 20 61 72   first six.** ar
27f76 67 75 6d 65 6e 74 73 20 74 6f 20 73 71 6c 69 74  guments to sqlit
27f77 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72  e3GenerateConstr
27f78 61 69 6e 74 43 68 65 63 6b 73 2e 0a 2a 2f 0a 53  aintChecks..*/.S
27f79 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
27f7a 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65  id sqlite3Comple
27f7b 74 65 49 6e 73 65 72 74 69 6f 6e 28 0a 20 20 50  teInsertion(.  P
27f7c 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
27f7d 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
27f7e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61   context */.  Ta
27f7f 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20  ble *pTab,      
27f80 20 20 2f 2a 20 74 68 65 20 74 61 62 6c 65 20 69    /* the table i
27f81 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 65  nto which we are
27f82 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20   inserting */.  
27f83 69 6e 74 20 62 61 73 65 43 75 72 2c 20 20 20 20  int baseCur,    
27f84 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
27f85 61 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72  a read/write cur
27f86 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  sor pointing at 
27f87 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65  pTab */.  int re
27f88 67 52 6f 77 69 64 2c 20 20 20 20 20 20 20 2f 2a  gRowid,       /*
27f89 20 52 61 6e 67 65 20 6f 66 20 63 6f 6e 74 65 6e   Range of conten
27f8a 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67  t */.  int *aReg
27f8b 49 64 78 2c 20 20 20 20 20 20 20 2f 2a 20 52 65  Idx,       /* Re
27f8c 67 69 73 74 65 72 20 75 73 65 64 20 62 79 20 65  gister used by e
27f8d 61 63 68 20 69 6e 64 65 78 2e 20 20 30 20 66 6f  ach index.  0 fo
27f8e 72 20 75 6e 75 73 65 64 20 69 6e 64 69 63 65 73  r unused indices
27f8f 20 2a 2f 0a 20 20 69 6e 74 20 69 73 55 70 64 61   */.  int isUpda
27f90 74 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 72 75  te,       /* Tru
27f91 65 20 66 6f 72 20 55 50 44 41 54 45 2c 20 46 61  e for UPDATE, Fa
27f92 6c 73 65 20 66 6f 72 20 49 4e 53 45 52 54 20 2a  lse for INSERT *
27f93 2f 0a 20 20 69 6e 74 20 61 70 70 65 6e 64 42 69  /.  int appendBi
27f94 61 73 2c 20 20 20 20 20 2f 2a 20 54 72 75 65 20  as,     /* True 
27f95 69 66 20 74 68 69 73 20 69 73 20 6c 69 6b 65 6c  if this is likel
27f96 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e  y to be an appen
27f97 64 20 2a 2f 0a 20 20 69 6e 74 20 75 73 65 53 65  d */.  int useSe
27f98 65 6b 52 65 73 75 6c 74 20 20 20 2f 2a 20 54 72  ekResult   /* Tr
27f99 75 65 20 74 6f 20 73 65 74 20 74 68 65 20 55 53  ue to set the US
27f9a 45 53 45 45 4b 52 45 53 55 4c 54 20 66 6c 61 67  ESEEKRESULT flag
27f9b 20 6f 6e 20 4f 50 5f 5b 49 64 78 5d 49 6e 73 65   on OP_[Idx]Inse
27f9c 72 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  rt */.){.  int i
27f9d 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69  ;.  Vdbe *v;.  i
27f9e 6e 74 20 6e 49 64 78 3b 0a 20 20 49 6e 64 65 78  nt nIdx;.  Index
27f9f 20 2a 70 49 64 78 3b 0a 20 20 75 38 20 70 69 6b   *pIdx;.  u8 pik
27fa0 5f 66 6c 61 67 73 3b 0a 20 20 69 6e 74 20 72 65  _flags;.  int re
27fa1 67 44 61 74 61 3b 0a 20 20 69 6e 74 20 72 65 67  gData;.  int reg
27fa2 52 65 63 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69  Rec;..  v = sqli
27fa3 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
27fa4 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21  e);.  assert( v!
27fa5 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
27fa6 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30  pTab->pSelect==0
27fa7 20 29 3b 20 20 2f 2a 20 54 68 69 73 20 74 61 62   );  /* This tab
27fa8 6c 65 20 69 73 20 6e 6f 74 20 61 20 56 49 45 57  le is not a VIEW
27fa9 20 2a 2f 0a 20 20 66 6f 72 28 6e 49 64 78 3d 30   */.  for(nIdx=0
27faa 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  , pIdx=pTab->pIn
27fab 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
27fac 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6e 49 64  pIdx->pNext, nId
27fad 78 2b 2b 29 7b 7d 0a 20 20 66 6f 72 28 69 3d 6e  x++){}.  for(i=n
27fae 49 64 78 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d  Idx-1; i>=0; i--
27faf 29 7b 0a 20 20 20 20 69 66 28 20 61 52 65 67 49  ){.    if( aRegI
27fb0 64 78 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69  dx[i]==0 ) conti
27fb1 6e 75 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  nue;.    sqlite3
27fb2 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27fb3 5f 49 64 78 49 6e 73 65 72 74 2c 20 62 61 73 65  _IdxInsert, base
27fb4 43 75 72 2b 69 2b 31 2c 20 61 52 65 67 49 64 78  Cur+i+1, aRegIdx
27fb5 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 75 73  [i]);.    if( us
27fb6 65 53 65 65 6b 52 65 73 75 6c 74 20 29 7b 0a 20  eSeekResult ){. 
27fb7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
27fb8 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c  ChangeP5(v, OPFL
27fb9 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54  AG_USESEEKRESULT
27fba 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
27fbb 65 67 44 61 74 61 20 3d 20 72 65 67 52 6f 77 69  egData = regRowi
27fbc 64 20 2b 20 31 3b 0a 20 20 72 65 67 52 65 63 20  d + 1;.  regRec 
27fbd 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
27fbe 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73  Reg(pParse);.  s
27fbf 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
27fc0 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  (v, OP_MakeRecor
27fc1 64 2c 20 72 65 67 44 61 74 61 2c 20 70 54 61 62  d, regData, pTab
27fc2 2d 3e 6e 43 6f 6c 2c 20 72 65 67 52 65 63 29 3b  ->nCol, regRec);
27fc3 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 41  .  sqlite3TableA
27fc4 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70 54  ffinityStr(v, pT
27fc5 61 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  ab);.  sqlite3Ex
27fc6 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
27fc7 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65  hange(pParse, re
27fc8 67 44 61 74 61 2c 20 70 54 61 62 2d 3e 6e 43 6f  gData, pTab->nCo
27fc9 6c 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  l);.  if( pParse
27fca 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20 20 20  ->nested ){.    
27fcb 70 69 6b 5f 66 6c 61 67 73 20 3d 20 30 3b 0a 20  pik_flags = 0;. 
27fcc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 69 6b 5f   }else{.    pik_
27fcd 66 6c 61 67 73 20 3d 20 4f 50 46 4c 41 47 5f 4e  flags = OPFLAG_N
27fce 43 48 41 4e 47 45 3b 0a 20 20 20 20 70 69 6b 5f  CHANGE;.    pik_
27fcf 66 6c 61 67 73 20 7c 3d 20 28 69 73 55 70 64 61  flags |= (isUpda
27fd0 74 65 3f 4f 50 46 4c 41 47 5f 49 53 55 50 44 41  te?OPFLAG_ISUPDA
27fd1 54 45 3a 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f  TE:OPFLAG_LASTRO
27fd2 57 49 44 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  WID);.  }.  if( 
27fd3 61 70 70 65 6e 64 42 69 61 73 20 29 7b 0a 20 20  appendBias ){.  
27fd4 20 20 70 69 6b 5f 66 6c 61 67 73 20 7c 3d 20 4f    pik_flags |= O
27fd5 50 46 4c 41 47 5f 41 50 50 45 4e 44 3b 0a 20 20  PFLAG_APPEND;.  
27fd6 7d 0a 20 20 69 66 28 20 75 73 65 53 65 65 6b 52  }.  if( useSeekR
27fd7 65 73 75 6c 74 20 29 7b 0a 20 20 20 20 70 69 6b  esult ){.    pik
27fd8 5f 66 6c 61 67 73 20 7c 3d 20 4f 50 46 4c 41 47  _flags |= OPFLAG
27fd9 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 3b 0a  _USESEEKRESULT;.
27fda 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
27fdb 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e  eAddOp3(v, OP_In
27fdc 73 65 72 74 2c 20 62 61 73 65 43 75 72 2c 20 72  sert, baseCur, r
27fdd 65 67 52 65 63 2c 20 72 65 67 52 6f 77 69 64 29  egRec, regRowid)
27fde 3b 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d  ;.  if( !pParse-
27fdf 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20 20 20 73  >nested ){.    s
27fe0 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
27fe1 50 34 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e  P4(v, -1, pTab->
27fe2 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43  zName, P4_STATIC
27fe3 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
27fe4 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
27fe5 70 69 6b 5f 66 6c 61 67 73 29 3b 0a 7d 0a 0a 2f  pik_flags);.}../
27fe6 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
27fe7 64 65 20 74 68 61 74 20 77 69 6c 6c 20 6f 70 65  de that will ope
27fe8 6e 20 63 75 72 73 6f 72 73 20 66 6f 72 20 61 20  n cursors for a 
27fe9 74 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 61 6c  table and for al
27fea 6c 0a 2a 2a 20 69 6e 64 69 63 65 73 20 6f 66 20  l.** indices of 
27feb 74 68 61 74 20 74 61 62 6c 65 2e 20 20 54 68 65  that table.  The
27fec 20 22 62 61 73 65 43 75 72 22 20 70 61 72 61 6d   "baseCur" param
27fed 65 74 65 72 20 69 73 20 74 68 65 20 63 75 72 73  eter is the curs
27fee 6f 72 20 6e 75 6d 62 65 72 20 75 73 65 64 0a 2a  or number used.*
27fef 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e  * for the table.
27ff0 20 20 49 6e 64 69 63 65 73 20 61 72 65 20 6f 70    Indices are op
27ff1 65 6e 65 64 20 6f 6e 20 73 75 62 73 65 71 75 65  ened on subseque
27ff2 6e 74 20 63 75 72 73 6f 72 73 2e 0a 2a 2a 0a 2a  nt cursors..**.*
27ff3 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
27ff4 62 65 72 20 6f 66 20 69 6e 64 69 63 65 73 20 6f  ber of indices o
27ff5 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a  n the table..*/.
27ff6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
27ff7 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61  nt sqlite3OpenTa
27ff8 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 0a 20  bleAndIndices(. 
27ff9 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
27ffa 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
27ffb 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20  text */.  Table 
27ffc 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a 20 54 61  *pTab,     /* Ta
27ffd 62 6c 65 20 74 6f 20 62 65 20 6f 70 65 6e 65 64  ble to be opened
27ffe 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65 43 75   */.  int baseCu
27fff 72 2c 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72  r,     /* Cursor
28000 20 6e 75 6d 62 65 72 20 61 73 73 69 67 6e 65 64   number assigned
28001 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 2a 2f   to the table */
28002 0a 20 20 69 6e 74 20 6f 70 20 20 20 20 20 20 20  .  int op       
28003 20 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 6e 52 65      /* OP_OpenRe
28004 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e 57 72 69  ad or OP_OpenWri
28005 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  te */.){.  int i
28006 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 49  ;.  int iDb;.  I
28007 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 56 64  ndex *pIdx;.  Vd
28008 62 65 20 2a 76 3b 0a 0a 20 20 69 66 28 20 49 73  be *v;..  if( Is
28009 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 20  Virtual(pTab) ) 
2800a 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 44 62 20  return 0;.  iDb 
2800b 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
2800c 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
2800d 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
2800e 29 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  );.  v = sqlite3
2800f 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
28010 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20  .  assert( v!=0 
28011 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e  );.  sqlite3Open
28012 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 62 61  Table(pParse, ba
28013 73 65 43 75 72 2c 20 69 44 62 2c 20 70 54 61 62  seCur, iDb, pTab
28014 2c 20 6f 70 29 3b 0a 20 20 66 6f 72 28 69 3d 31  , op);.  for(i=1
28015 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  , pIdx=pTab->pIn
28016 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
28017 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b  pIdx->pNext, i++
28018 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a  ){.    KeyInfo *
28019 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e  pKey = sqlite3In
2801a 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73  dexKeyinfo(pPars
2801b 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 61 73  e, pIdx);.    as
2801c 73 65 72 74 28 20 70 49 64 78 2d 3e 70 53 63 68  sert( pIdx->pSch
2801d 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65  ema==pTab->pSche
2801e 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ma );.    sqlite
2801f 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 6f  3VdbeAddOp4(v, o
28020 70 2c 20 69 2b 62 61 73 65 43 75 72 2c 20 70 49  p, i+baseCur, pI
28021 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20  dx->tnum, iDb,. 
28022 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28023 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79       (char*)pKey
28024 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  , P4_KEYINFO_HAN
28025 44 4f 46 46 29 3b 0a 20 20 20 20 56 64 62 65 43  DOFF);.    VdbeC
28026 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c  omment((v, "%s",
28027 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a   pIdx->zName));.
28028 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65    }.  if( pParse
28029 2d 3e 6e 54 61 62 3c 62 61 73 65 43 75 72 2b 69  ->nTab<baseCur+i
2802a 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   ){.    pParse->
2802b 6e 54 61 62 20 3d 20 62 61 73 65 43 75 72 2b 69  nTab = baseCur+i
2802c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69  ;.  }.  return i
2802d 2d 31 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53  -1;.}...#ifdef S
2802e 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a  QLITE_TEST./*.**
2802f 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67   The following g
28030 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69  lobal variable i
28031 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68  s incremented wh
28032 65 6e 65 76 65 72 20 74 68 65 0a 2a 2a 20 74 72  enever the.** tr
28033 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74  ansfer optimizat
28034 69 6f 6e 20 69 73 20 75 73 65 64 2e 20 20 54 68  ion is used.  Th
28035 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 74  is is used for t
28036 65 73 74 69 6e 67 0a 2a 2a 20 70 75 72 70 6f 73  esting.** purpos
28037 65 73 20 6f 6e 6c 79 20 2d 20 74 6f 20 6d 61 6b  es only - to mak
28038 65 20 73 75 72 65 20 74 68 65 20 74 72 61 6e 73  e sure the trans
28039 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  fer optimization
2803a 20 72 65 61 6c 6c 79 0a 2a 2a 20 69 73 20 68 61   really.** is ha
2803b 70 70 65 6e 69 6e 67 20 77 68 65 6e 20 69 74 20  ppening when it 
2803c 69 73 20 73 75 70 70 6f 73 65 20 74 6f 2e 0a 2a  is suppose to..*
2803d 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2803e 20 73 71 6c 69 74 65 33 5f 78 66 65 72 6f 70 74   sqlite3_xferopt
2803f 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 20 2f  _count;.#endif /
28040 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f  * SQLITE_TEST */
28041 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
28042 45 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 0a  E_OMIT_XFER_OPT.
28043 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 63  /*.** Check to c
28044 6f 6c 6c 61 74 69 6f 6e 20 6e 61 6d 65 73 20 74  ollation names t
28045 6f 20 73 65 65 20 69 66 20 74 68 65 79 20 61 72  o see if they ar
28046 65 20 63 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a 2f  e compatible..*/
28047 0a 73 74 61 74 69 63 20 69 6e 74 20 78 66 65 72  .static int xfer
28048 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c 6c 61 74  CompatibleCollat
28049 69 6f 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ion(const char *
2804a 7a 31 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  z1, const char *
2804b 7a 32 29 7b 0a 20 20 69 66 28 20 7a 31 3d 3d 30  z2){.  if( z1==0
2804c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 7a   ){.    return z
2804d 32 3d 3d 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20  2==0;.  }.  if( 
2804e 7a 32 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  z2==0 ){.    ret
2804f 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  urn 0;.  }.  ret
28050 75 72 6e 20 73 71 6c 69 74 65 33 53 74 72 49 43  urn sqlite3StrIC
28051 6d 70 28 7a 31 2c 20 7a 32 29 3d 3d 30 3b 0a 7d  mp(z1, z2)==0;.}
28052 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  .../*.** Check t
28053 6f 20 73 65 65 20 69 66 20 69 6e 64 65 78 20 70  o see if index p
28054 53 72 63 20 69 73 20 63 6f 6d 70 61 74 69 62 6c  Src is compatibl
28055 65 20 61 73 20 61 20 73 6f 75 72 63 65 20 6f 66  e as a source of
28056 20 64 61 74 61 0a 2a 2a 20 66 6f 72 20 69 6e 64   data.** for ind
28057 65 78 20 70 44 65 73 74 20 69 6e 20 61 6e 20 69  ex pDest in an i
28058 6e 73 65 72 74 20 74 72 61 6e 73 66 65 72 20 6f  nsert transfer o
28059 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68  ptimization.  Th
2805a 65 20 72 75 6c 65 73 0a 2a 2a 20 66 6f 72 20 61  e rules.** for a
2805b 20 63 6f 6d 70 61 74 69 62 6c 65 20 69 6e 64 65   compatible inde
2805c 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 20  x:.**.**    *   
2805d 54 68 65 20 69 6e 64 65 78 20 69 73 20 6f 76 65  The index is ove
2805e 72 20 74 68 65 20 73 61 6d 65 20 73 65 74 20 6f  r the same set o
2805f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 20 20 20  f columns.**    
28060 2a 20 20 20 54 68 65 20 73 61 6d 65 20 44 45 53  *   The same DES
28061 43 20 61 6e 64 20 41 53 43 20 6d 61 72 6b 69 6e  C and ASC markin
28062 67 73 20 6f 63 63 75 72 73 20 6f 6e 20 61 6c 6c  gs occurs on all
28063 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 20 20 20 2a   columns.**    *
28064 20 20 20 54 68 65 20 73 61 6d 65 20 6f 6e 45 72     The same onEr
28065 72 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 20 28  ror processing (
28066 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e  OE_Abort, OE_Ign
28067 6f 72 65 2c 20 65 74 63 29 0a 2a 2a 20 20 20 20  ore, etc).**    
28068 2a 20 20 20 54 68 65 20 73 61 6d 65 20 63 6f 6c  *   The same col
28069 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
2806a 6f 6e 20 65 61 63 68 20 63 6f 6c 75 6d 6e 0a 2a  on each column.*
2806b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 78 66 65  /.static int xfe
2806c 72 43 6f 6d 70 61 74 69 62 6c 65 49 6e 64 65 78  rCompatibleIndex
2806d 28 49 6e 64 65 78 20 2a 70 44 65 73 74 2c 20 49  (Index *pDest, I
2806e 6e 64 65 78 20 2a 70 53 72 63 29 7b 0a 20 20 69  ndex *pSrc){.  i
2806f 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20  nt i;.  assert( 
28070 70 44 65 73 74 20 26 26 20 70 53 72 63 20 29 3b  pDest && pSrc );
28071 0a 20 20 61 73 73 65 72 74 28 20 70 44 65 73 74  .  assert( pDest
28072 2d 3e 70 54 61 62 6c 65 21 3d 70 53 72 63 2d 3e  ->pTable!=pSrc->
28073 70 54 61 62 6c 65 20 29 3b 0a 20 20 69 66 28 20  pTable );.  if( 
28074 70 44 65 73 74 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d  pDest->nColumn!=
28075 70 53 72 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b  pSrc->nColumn ){
28076 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
28077 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 6e 75   /* Different nu
28078 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
28079 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 65  */.  }.  if( pDe
2807a 73 74 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70 53 72  st->onError!=pSr
2807b 63 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20  c->onError ){.  
2807c 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
2807d 20 44 69 66 66 65 72 65 6e 74 20 63 6f 6e 66 6c   Different confl
2807e 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 73  ict resolution s
2807f 74 72 61 74 65 67 69 65 73 20 2a 2f 0a 20 20 7d  trategies */.  }
28080 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53  .  for(i=0; i<pS
28081 72 63 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b  rc->nColumn; i++
28082 29 7b 0a 20 20 20 20 69 66 28 20 70 53 72 63 2d  ){.    if( pSrc-
28083 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 21 3d 70 44  >aiColumn[i]!=pD
28084 65 73 74 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d  est->aiColumn[i]
28085 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
28086 20 30 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65   0;   /* Differe
28087 6e 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 64 65 78  nt columns index
28088 65 64 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20  ed */.    }.    
28089 69 66 28 20 70 53 72 63 2d 3e 61 53 6f 72 74 4f  if( pSrc->aSortO
2808a 72 64 65 72 5b 69 5d 21 3d 70 44 65 73 74 2d 3e  rder[i]!=pDest->
2808b 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 29 7b  aSortOrder[i] ){
2808c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
2808d 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20     /* Different 
2808e 73 6f 72 74 20 6f 72 64 65 72 73 20 2a 2f 0a 20  sort orders */. 
2808f 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 78 66     }.    if( !xf
28090 65 72 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c 6c  erCompatibleColl
28091 61 74 69 6f 6e 28 70 53 72 63 2d 3e 61 7a 43 6f  ation(pSrc->azCo
28092 6c 6c 5b 69 5d 2c 70 44 65 73 74 2d 3e 61 7a 43  ll[i],pDest->azC
28093 6f 6c 6c 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20  oll[i]) ){.     
28094 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
28095 44 69 66 66 65 72 65 6e 74 20 63 6f 6c 6c 61 74  Different collat
28096 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 2a 2f  ing sequences */
28097 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
28098 20 49 66 20 6e 6f 20 74 65 73 74 20 61 62 6f 76   If no test abov
28099 65 20 66 61 69 6c 73 20 74 68 65 6e 20 74 68 65  e fails then the
2809a 20 69 6e 64 69 63 65 73 20 6d 75 73 74 20 62 65   indices must be
2809b 20 63 6f 6d 70 61 74 69 62 6c 65 20 2a 2f 0a 20   compatible */. 
2809c 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
2809d 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 68 65 20  .** Attempt the 
2809e 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a  transfer optimiz
2809f 61 74 69 6f 6e 20 6f 6e 20 49 4e 53 45 52 54 73  ation on INSERTs
280a0 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a   of the form.**.
280a1 2a 2a 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e  **     INSERT IN
280a2 54 4f 20 74 61 62 31 20 53 45 4c 45 43 54 20 2a  TO tab1 SELECT *
280a3 20 46 52 4f 4d 20 74 61 62 32 3b 0a 2a 2a 0a 2a   FROM tab2;.**.*
280a4 2a 20 54 68 69 73 20 6f 70 74 69 6d 69 7a 61 74  * This optimizat
280a5 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 61 74 74 65  ion is only atte
280a6 6d 70 74 65 64 20 69 66 0a 2a 2a 0a 2a 2a 20 20  mpted if.**.**  
280a7 20 20 28 31 29 20 20 74 61 62 31 20 61 6e 64 20    (1)  tab1 and 
280a8 74 61 62 32 20 68 61 76 65 20 69 64 65 6e 74 69  tab2 have identi
280a9 63 61 6c 20 73 63 68 65 6d 61 73 20 69 6e 63 6c  cal schemas incl
280aa 75 64 69 6e 67 20 61 6c 6c 20 74 68 65 0a 2a 2a  uding all the.**
280ab 20 20 20 20 20 20 20 20 20 73 61 6d 65 20 69 6e           same in
280ac 64 69 63 65 73 20 61 6e 64 20 63 6f 6e 73 74 72  dices and constr
280ad 61 69 6e 74 73 0a 2a 2a 0a 2a 2a 20 20 20 20 28  aints.**.**    (
280ae 32 29 20 20 74 61 62 31 20 61 6e 64 20 74 61 62  2)  tab1 and tab
280af 32 20 61 72 65 20 64 69 66 66 65 72 65 6e 74 20  2 are different 
280b0 74 61 62 6c 65 73 0a 2a 2a 0a 2a 2a 20 20 20 20  tables.**.**    
280b1 28 33 29 20 20 54 68 65 72 65 20 6d 75 73 74 20  (3)  There must 
280b2 62 65 20 6e 6f 20 74 72 69 67 67 65 72 73 20 6f  be no triggers o
280b3 6e 20 74 61 62 31 0a 2a 2a 0a 2a 2a 20 20 20 20  n tab1.**.**    
280b4 28 34 29 20 20 54 68 65 20 72 65 73 75 6c 74 20  (4)  The result 
280b5 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43  set of the SELEC
280b6 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 22  T statement is "
280b7 2a 22 0a 2a 2a 0a 2a 2a 20 20 20 20 28 35 29 20  *".**.**    (5) 
280b8 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   The SELECT stat
280b9 65 6d 65 6e 74 20 68 61 73 20 6e 6f 20 57 48 45  ement has no WHE
280ba 52 45 2c 20 48 41 56 49 4e 47 2c 20 4f 52 44 45  RE, HAVING, ORDE
280bb 52 20 42 59 2c 20 47 52 4f 55 50 20 42 59 2c 0a  R BY, GROUP BY,.
280bc 2a 2a 20 20 20 20 20 20 20 20 20 6f 72 20 4c 49  **         or LI
280bd 4d 49 54 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a  MIT clause..**.*
280be 2a 20 20 20 20 28 36 29 20 20 54 68 65 20 53 45  *    (6)  The SE
280bf 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69  LECT statement i
280c0 73 20 61 20 73 69 6d 70 6c 65 20 28 6e 6f 74 20  s a simple (not 
280c1 61 20 63 6f 6d 70 6f 75 6e 64 29 20 73 65 6c 65  a compound) sele
280c2 63 74 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20  ct that.**      
280c3 20 20 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c 79     contains only
280c4 20 74 61 62 32 20 69 6e 20 69 74 73 20 46 52 4f   tab2 in its FRO
280c5 4d 20 63 6c 61 75 73 65 0a 2a 2a 0a 2a 2a 20 54  M clause.**.** T
280c6 68 69 73 20 6d 65 74 68 6f 64 20 66 6f 72 20 69  his method for i
280c7 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 20  mplementing the 
280c8 49 4e 53 45 52 54 20 74 72 61 6e 73 66 65 72 73  INSERT transfers
280c9 20 72 61 77 20 72 65 63 6f 72 64 73 20 66 72 6f   raw records fro
280ca 6d 0a 2a 2a 20 74 61 62 32 20 6f 76 65 72 20 74  m.** tab2 over t
280cb 6f 20 74 61 62 31 2e 20 20 54 68 65 20 63 6f 6c  o tab1.  The col
280cc 75 6d 6e 73 20 61 72 65 20 6e 6f 74 20 64 65 63  umns are not dec
280cd 6f 64 65 64 2e 20 20 52 61 77 20 72 65 63 6f 72  oded.  Raw recor
280ce 64 73 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 69  ds from.** the i
280cf 6e 64 69 63 65 73 20 6f 66 20 74 61 62 32 20 61  ndices of tab2 a
280d0 72 65 20 74 72 61 6e 73 66 65 72 65 64 20 74 6f  re transfered to
280d1 20 74 61 62 31 20 61 73 20 77 65 6c 6c 2e 20 20   tab1 as well.  
280d2 49 6e 20 73 6f 20 64 6f 69 6e 67 2c 0a 2a 2a 20  In so doing,.** 
280d3 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 74 61  the resulting ta
280d4 62 31 20 68 61 73 20 6d 75 63 68 20 6c 65 73 73  b1 has much less
280d5 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 2e 0a   fragmentation..
280d6 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
280d7 6e 65 20 72 65 74 75 72 6e 73 20 54 52 55 45 20  ne returns TRUE 
280d8 69 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74  if the optimizat
280d9 69 6f 6e 20 69 73 20 61 74 74 65 6d 70 74 65 64  ion is attempted
280da 2e 20 20 49 66 20 61 6e 79 0a 2a 2a 20 6f 66 20  .  If any.** of 
280db 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61  the conditions a
280dc 62 6f 76 65 20 66 61 69 6c 20 73 6f 20 74 68 61  bove fail so tha
280dd 74 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69  t the optimizati
280de 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 0a 2a 2a  on should not.**
280df 20 62 65 20 61 74 74 65 6d 70 74 65 64 2c 20 74   be attempted, t
280e0 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
280e1 20 72 65 74 75 72 6e 73 20 46 41 4c 53 45 2e 0a   returns FALSE..
280e2 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 78 66  */.static int xf
280e3 65 72 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 28 0a  erOptimization(.
280e4 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
280e5 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
280e6 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  r context */.  T
280e7 61 62 6c 65 20 2a 70 44 65 73 74 2c 20 20 20 20  able *pDest,    
280e8 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
280e9 65 20 77 65 20 61 72 65 20 69 6e 73 65 72 74 69  e we are inserti
280ea 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20 53 65 6c  ng into */.  Sel
280eb 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20  ect *pSelect,   
280ec 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73     /* A SELECT s
280ed 74 61 74 65 6d 65 6e 74 20 74 6f 20 75 73 65 20  tatement to use 
280ee 61 73 20 74 68 65 20 64 61 74 61 20 73 6f 75 72  as the data sour
280ef 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72  ce */.  int onEr
280f0 72 6f 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ror,          /*
280f1 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20 63   How to handle c
280f2 6f 6e 73 74 72 61 69 6e 74 20 65 72 72 6f 72 73  onstraint errors
280f3 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 44 65 73   */.  int iDbDes
280f4 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  t           /* T
280f5 68 65 20 64 61 74 61 62 61 73 65 20 6f 66 20 70  he database of p
280f6 44 65 73 74 20 2a 2f 0a 29 7b 0a 20 20 45 78 70  Dest */.){.  Exp
280f7 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20  rList *pEList;  
280f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
280f9 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   The result set 
280fa 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 2a 2f  of the SELECT */
280fb 0a 20 20 54 61 62 6c 65 20 2a 70 53 72 63 3b 20  .  Table *pSrc; 
280fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
280fd 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
280fe 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
280ff 75 73 65 20 6f 66 20 53 45 4c 45 43 54 20 2a 2f  use of SELECT */
28100 0a 20 20 49 6e 64 65 78 20 2a 70 53 72 63 49 64  .  Index *pSrcId
28101 78 2c 20 2a 70 44 65 73 74 49 64 78 3b 20 20 20  x, *pDestIdx;   
28102 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 61 6e      /* Source an
28103 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 6e  d destination in
28104 64 69 63 65 73 20 2a 2f 0a 20 20 73 74 72 75 63  dices */.  struc
28105 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
28106 70 49 74 65 6d 3b 20 20 20 20 20 20 2f 2a 20 41  pItem;      /* A
28107 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 70 53 65  n element of pSe
28108 6c 65 63 74 2d 3e 70 53 72 63 20 2a 2f 0a 20 20  lect->pSrc */.  
28109 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
2810a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2810b 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
2810c 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 53 72 63   */.  int iDbSrc
2810d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2810e 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
2810f 74 61 62 61 73 65 20 6f 66 20 70 53 72 63 20 2a  tabase of pSrc *
28110 2f 0a 20 20 69 6e 74 20 69 53 72 63 2c 20 69 44  /.  int iSrc, iD
28111 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  est;            
28112 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 73 20       /* Cursors 
28113 66 72 6f 6d 20 73 6f 75 72 63 65 20 61 6e 64 20  from source and 
28114 64 65 73 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20  destination */. 
28115 20 69 6e 74 20 61 64 64 72 31 2c 20 61 64 64 72   int addr1, addr
28116 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  2;              
28117 20 20 2f 2a 20 4c 6f 6f 70 20 61 64 64 72 65 73    /* Loop addres
28118 73 65 73 20 2a 2f 0a 20 20 69 6e 74 20 65 6d 70  ses */.  int emp
28119 74 79 44 65 73 74 54 65 73 74 3b 20 20 20 20 20  tyDestTest;     
2811a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64            /* Add
2811b 72 65 73 73 20 6f 66 20 74 65 73 74 20 66 6f 72  ress of test for
2811c 20 65 6d 70 74 79 20 70 44 65 73 74 20 2a 2f 0a   empty pDest */.
2811d 20 20 69 6e 74 20 65 6d 70 74 79 53 72 63 54 65    int emptySrcTe
2811e 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  st;             
2811f 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
28120 20 74 65 73 74 20 66 6f 72 20 65 6d 70 74 79 20   test for empty 
28121 70 53 72 63 20 2a 2f 0a 20 20 56 64 62 65 20 2a  pSrc */.  Vdbe *
28122 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
28123 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
28124 65 20 56 44 42 45 20 77 65 20 61 72 65 20 62 75  e VDBE we are bu
28125 69 6c 64 69 6e 67 20 2a 2f 0a 20 20 4b 65 79 49  ilding */.  KeyI
28126 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 20 20 20 20  nfo *pKey;      
28127 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28128 4b 65 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  Key information 
28129 66 6f 72 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a  for an index */.
2812a 20 20 69 6e 74 20 72 65 67 41 75 74 6f 69 6e 63    int regAutoinc
2812b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2812c 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 72 65 67     /* Memory reg
2812d 69 73 74 65 72 20 75 73 65 64 20 62 79 20 41 55  ister used by AU
2812e 54 4f 49 4e 43 20 2a 2f 0a 20 20 69 6e 74 20 64  TOINC */.  int d
2812f 65 73 74 48 61 73 55 6e 69 71 75 65 49 64 78 20  estHasUniqueIdx 
28130 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  = 0;        /* T
28131 72 75 65 20 69 66 20 70 44 65 73 74 20 68 61 73  rue if pDest has
28132 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78 20   a UNIQUE index 
28133 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61  */.  int regData
28134 2c 20 72 65 67 52 6f 77 69 64 3b 20 20 20 20 20  , regRowid;     
28135 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
28136 72 73 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 20  rs holding data 
28137 61 6e 64 20 72 6f 77 69 64 20 2a 2f 0a 0a 20 20  and rowid */..  
28138 69 66 28 20 70 53 65 6c 65 63 74 3d 3d 30 20 29  if( pSelect==0 )
28139 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
2813a 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 6f 66 20    /* Must be of 
2813b 74 68 65 20 66 6f 72 6d 20 20 49 4e 53 45 52 54  the form  INSERT
2813c 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 54   INTO ... SELECT
2813d 20 2e 2e 2e 20 2a 2f 0a 20 20 7d 0a 20 20 69 66   ... */.  }.  if
2813e 28 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  ( sqlite3Trigger
2813f 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 44 65  List(pParse, pDe
28140 73 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  st) ){.    retur
28141 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 31 20 6d  n 0;   /* tab1 m
28142 75 73 74 20 6e 6f 74 20 68 61 76 65 20 74 72 69  ust not have tri
28143 67 67 65 72 73 20 2a 2f 0a 20 20 7d 0a 23 69 66  ggers */.  }.#if
28144 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
28145 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
28146 69 66 28 20 70 44 65 73 74 2d 3e 74 61 62 46 6c  if( pDest->tabFl
28147 61 67 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c  ags & TF_Virtual
28148 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
28149 3b 20 20 20 2f 2a 20 74 61 62 31 20 6d 75 73 74  ;   /* tab1 must
2814a 20 6e 6f 74 20 62 65 20 61 20 76 69 72 74 75 61   not be a virtua
2814b 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7d 0a 23  l table */.  }.#
2814c 65 6e 64 69 66 0a 20 20 69 66 28 20 6f 6e 45 72  endif.  if( onEr
2814d 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20  ror==OE_Default 
2814e 29 7b 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d  ){.    onError =
2814f 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 20   OE_Abort;.  }. 
28150 20 69 66 28 20 6f 6e 45 72 72 6f 72 21 3d 4f 45   if( onError!=OE
28151 5f 41 62 6f 72 74 20 26 26 20 6f 6e 45 72 72 6f  _Abort && onErro
28152 72 21 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 29  r!=OE_Rollback )
28153 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
28154 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 64 6f 20 4f    /* Cannot do O
28155 52 20 52 45 50 4c 41 43 45 20 6f 72 20 4f 52 20  R REPLACE or OR 
28156 49 47 4e 4f 52 45 20 6f 72 20 4f 52 20 46 41 49  IGNORE or OR FAI
28157 4c 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65 72  L */.  }.  asser
28158 74 28 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 29  t(pSelect->pSrc)
28159 3b 20 20 20 2f 2a 20 61 6c 6c 6f 63 61 74 65 64  ;   /* allocated
2815a 20 65 76 65 6e 20 69 66 20 74 68 65 72 65 20 69   even if there i
2815b 73 20 6e 6f 20 46 52 4f 4d 20 63 6c 61 75 73 65  s no FROM clause
2815c 20 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c 65 63   */.  if( pSelec
2815d 74 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31  t->pSrc->nSrc!=1
2815e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2815f 3b 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75  ;   /* FROM clau
28160 73 65 20 6d 75 73 74 20 68 61 76 65 20 65 78 61  se must have exa
28161 63 74 6c 79 20 6f 6e 65 20 74 65 72 6d 20 2a 2f  ctly one term */
28162 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65  .  }.  if( pSele
28163 63 74 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70  ct->pSrc->a[0].p
28164 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 72 65  Select ){.    re
28165 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 46 52 4f  turn 0;   /* FRO
28166 4d 20 63 6c 61 75 73 65 20 63 61 6e 6e 6f 74 20  M clause cannot 
28167 63 6f 6e 74 61 69 6e 20 61 20 73 75 62 71 75 65  contain a subque
28168 72 79 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  ry */.  }.  if( 
28169 70 53 65 6c 65 63 74 2d 3e 70 57 68 65 72 65 20  pSelect->pWhere 
2816a 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
2816b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79     /* SELECT may
2816c 20 6e 6f 74 20 68 61 76 65 20 61 20 57 48 45 52   not have a WHER
2816d 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a  E clause */.  }.
2816e 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70    if( pSelect->p
2816f 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 72  OrderBy ){.    r
28170 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45  eturn 0;   /* SE
28171 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 68 61 76  LECT may not hav
28172 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c  e an ORDER BY cl
28173 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 2f 2a  ause */.  }.  /*
28174 20 44 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20   Do not need to 
28175 74 65 73 74 20 66 6f 72 20 61 20 48 41 56 49 4e  test for a HAVIN
28176 47 20 63 6c 61 75 73 65 2e 20 20 49 66 20 48 41  G clause.  If HA
28177 56 49 4e 47 20 69 73 20 70 72 65 73 65 6e 74 20  VING is present 
28178 62 75 74 0a 20 20 2a 2a 20 74 68 65 72 65 20 69  but.  ** there i
28179 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 2c 20 77  s no ORDER BY, w
2817a 65 20 77 69 6c 6c 20 67 65 74 20 61 6e 20 65 72  e will get an er
2817b 72 6f 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70 53  ror. */.  if( pS
2817c 65 6c 65 63 74 2d 3e 70 47 72 6f 75 70 42 79 20  elect->pGroupBy 
2817d 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
2817e 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79     /* SELECT may
2817f 20 6e 6f 74 20 68 61 76 65 20 61 20 47 52 4f 55   not have a GROU
28180 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20  P BY clause */. 
28181 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74   }.  if( pSelect
28182 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20  ->pLimit ){.    
28183 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53  return 0;   /* S
28184 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 68 61  ELECT may not ha
28185 76 65 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73  ve a LIMIT claus
28186 65 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65 72  e */.  }.  asser
28187 74 28 20 70 53 65 6c 65 63 74 2d 3e 70 4f 66 66  t( pSelect->pOff
28188 73 65 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 4d 75  set==0 );  /* Mu
28189 73 74 20 62 65 20 73 6f 20 69 66 20 70 4c 69 6d  st be so if pLim
2818a 69 74 3d 3d 30 20 2a 2f 0a 20 20 69 66 28 20 70  it==0 */.  if( p
2818b 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 20 29  Select->pPrior )
2818c 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
2818d 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79 20    /* SELECT may 
2818e 6e 6f 74 20 62 65 20 61 20 63 6f 6d 70 6f 75 6e  not be a compoun
2818f 64 20 71 75 65 72 79 20 2a 2f 0a 20 20 7d 0a 20  d query */.  }. 
28190 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 73 65   if( pSelect->se
28191 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74  lFlags & SF_Dist
28192 69 6e 63 74 20 29 7b 0a 20 20 20 20 72 65 74 75  inct ){.    retu
28193 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43  rn 0;   /* SELEC
28194 54 20 6d 61 79 20 6e 6f 74 20 62 65 20 44 49 53  T may not be DIS
28195 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 0a 20 20 70  TINCT */.  }.  p
28196 45 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d  EList = pSelect-
28197 3e 70 45 4c 69 73 74 3b 0a 20 20 61 73 73 65 72  >pEList;.  asser
28198 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a  t( pEList!=0 );.
28199 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 6e 45    if( pEList->nE
2819a 78 70 72 21 3d 31 20 29 7b 0a 20 20 20 20 72 65  xpr!=1 ){.    re
2819b 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54 68 65  turn 0;   /* The
2819c 20 72 65 73 75 6c 74 20 73 65 74 20 6d 75 73 74   result set must
2819d 20 68 61 76 65 20 65 78 61 63 74 6c 79 20 6f 6e   have exactly on
2819e 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 7d 0a  e column */.  }.
2819f 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74    assert( pEList
281a0 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 20 29 3b 0a  ->a[0].pExpr );.
281a1 20 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b    if( pEList->a[
281a2 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  0].pExpr->op!=TK
281a3 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 72 65 74 75  _ALL ){.    retu
281a4 72 6e 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 72  rn 0;   /* The r
281a5 65 73 75 6c 74 20 73 65 74 20 6d 75 73 74 20 62  esult set must b
281a6 65 20 74 68 65 20 73 70 65 63 69 61 6c 20 6f 70  e the special op
281a7 65 72 61 74 6f 72 20 22 2a 22 20 2a 2f 0a 20 20  erator "*" */.  
281a8 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20  }..  /* At this 
281a9 70 6f 69 6e 74 20 77 65 20 68 61 76 65 20 65 73  point we have es
281aa 74 61 62 6c 69 73 68 65 64 20 74 68 61 74 20 74  tablished that t
281ab 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
281ac 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 72 72  of the.  ** corr
281ad 65 63 74 20 73 79 6e 74 61 63 74 69 63 20 66 6f  ect syntactic fo
281ae 72 6d 20 74 6f 20 70 61 72 74 69 63 69 70 61 74  rm to participat
281af 65 20 69 6e 20 74 68 69 73 20 6f 70 74 69 6d 69  e in this optimi
281b0 7a 61 74 69 6f 6e 2e 20 20 4e 6f 77 0a 20 20 2a  zation.  Now.  *
281b1 2a 20 77 65 20 68 61 76 65 20 74 6f 20 63 68 65  * we have to che
281b2 63 6b 20 74 68 65 20 73 65 6d 61 6e 74 69 63 73  ck the semantics
281b3 2e 0a 20 20 2a 2f 0a 20 20 70 49 74 65 6d 20 3d  ..  */.  pItem =
281b4 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 2d 3e   pSelect->pSrc->
281b5 61 3b 0a 20 20 70 53 72 63 20 3d 20 73 71 6c 69  a;.  pSrc = sqli
281b6 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70  te3LocateTable(p
281b7 50 61 72 73 65 2c 20 30 2c 20 70 49 74 65 6d 2d  Parse, 0, pItem-
281b8 3e 7a 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e 7a  >zName, pItem->z
281b9 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28  Database);.  if(
281ba 20 70 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20   pSrc==0 ){.    
281bb 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 46  return 0;   /* F
281bc 52 4f 4d 20 63 6c 61 75 73 65 20 64 6f 65 73 20  ROM clause does 
281bd 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 72 65  not contain a re
281be 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7d 0a  al table */.  }.
281bf 20 20 69 66 28 20 70 53 72 63 3d 3d 70 44 65 73    if( pSrc==pDes
281c0 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
281c1 30 3b 20 20 20 2f 2a 20 74 61 62 31 20 61 6e 64  0;   /* tab1 and
281c2 20 74 61 62 32 20 6d 61 79 20 6e 6f 74 20 62 65   tab2 may not be
281c3 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 20   the same table 
281c4 2a 2f 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  */.  }.#ifndef S
281c5 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
281c6 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 70 53  ALTABLE.  if( pS
281c7 72 63 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  rc->tabFlags & T
281c8 46 5f 56 69 72 74 75 61 6c 20 29 7b 0a 20 20 20  F_Virtual ){.   
281c9 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
281ca 74 61 62 32 20 6d 75 73 74 20 6e 6f 74 20 62 65  tab2 must not be
281cb 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
281cc 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20   */.  }.#endif. 
281cd 20 69 66 28 20 70 53 72 63 2d 3e 70 53 65 6c 65   if( pSrc->pSele
281ce 63 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ct ){.    return
281cf 20 30 3b 20 20 20 2f 2a 20 74 61 62 32 20 6d 61   0;   /* tab2 ma
281d0 79 20 6e 6f 74 20 62 65 20 61 20 76 69 65 77 20  y not be a view 
281d1 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 65  */.  }.  if( pDe
281d2 73 74 2d 3e 6e 43 6f 6c 21 3d 70 53 72 63 2d 3e  st->nCol!=pSrc->
281d3 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 72 65 74 75  nCol ){.    retu
281d4 72 6e 20 30 3b 20 20 20 2f 2a 20 4e 75 6d 62 65  rn 0;   /* Numbe
281d5 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6d 75 73  r of columns mus
281d6 74 20 62 65 20 74 68 65 20 73 61 6d 65 20 69 6e  t be the same in
281d7 20 74 61 62 31 20 61 6e 64 20 74 61 62 32 20 2a   tab1 and tab2 *
281d8 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 65 73  /.  }.  if( pDes
281d9 74 2d 3e 69 50 4b 65 79 21 3d 70 53 72 63 2d 3e  t->iPKey!=pSrc->
281da 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 72 65 74  iPKey ){.    ret
281db 75 72 6e 20 30 3b 20 20 20 2f 2a 20 42 6f 74 68  urn 0;   /* Both
281dc 20 74 61 62 6c 65 73 20 6d 75 73 74 20 68 61 76   tables must hav
281dd 65 20 74 68 65 20 73 61 6d 65 20 49 4e 54 45 47  e the same INTEG
281de 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a  ER PRIMARY KEY *
281df 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b  /.  }.  for(i=0;
281e0 20 69 3c 70 44 65 73 74 2d 3e 6e 43 6f 6c 3b 20   i<pDest->nCol; 
281e1 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 44  i++){.    if( pD
281e2 65 73 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 61 66 66  est->aCol[i].aff
281e3 69 6e 69 74 79 21 3d 70 53 72 63 2d 3e 61 43 6f  inity!=pSrc->aCo
281e4 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 20 29 7b  l[i].affinity ){
281e5 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
281e6 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20      /* Affinity 
281e7 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 6d 65  must be the same
281e8 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20   on all columns 
281e9 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  */.    }.    if(
281ea 20 21 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65   !xferCompatible
281eb 43 6f 6c 6c 61 74 69 6f 6e 28 70 44 65 73 74 2d  Collation(pDest-
281ec 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 2c 20  >aCol[i].zColl, 
281ed 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43  pSrc->aCol[i].zC
281ee 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 72 65  oll) ){.      re
281ef 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20 43 6f  turn 0;    /* Co
281f0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
281f1 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 6d   must be the sam
281f2 65 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73  e on all columns
281f3 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   */.    }.    if
281f4 28 20 70 44 65 73 74 2d 3e 61 43 6f 6c 5b 69 5d  ( pDest->aCol[i]
281f5 2e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70 53 72  .notNull && !pSr
281f6 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75  c->aCol[i].notNu
281f7 6c 6c 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ll ){.      retu
281f8 72 6e 20 30 3b 20 20 20 20 2f 2a 20 74 61 62 32  rn 0;    /* tab2
281f9 20 6d 75 73 74 20 62 65 20 4e 4f 54 20 4e 55 4c   must be NOT NUL
281fa 4c 20 69 66 20 74 61 62 31 20 69 73 20 2a 2f 0a  L if tab1 is */.
281fb 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28      }.  }.  for(
281fc 70 44 65 73 74 49 64 78 3d 70 44 65 73 74 2d 3e  pDestIdx=pDest->
281fd 70 49 6e 64 65 78 3b 20 70 44 65 73 74 49 64 78  pIndex; pDestIdx
281fe 3b 20 70 44 65 73 74 49 64 78 3d 70 44 65 73 74  ; pDestIdx=pDest
281ff 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
28200 20 69 66 28 20 70 44 65 73 74 49 64 78 2d 3e 6f   if( pDestIdx->o
28201 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20  nError!=OE_None 
28202 29 7b 0a 20 20 20 20 20 20 64 65 73 74 48 61 73  ){.      destHas
28203 55 6e 69 71 75 65 49 64 78 20 3d 20 31 3b 0a 20  UniqueIdx = 1;. 
28204 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 53 72     }.    for(pSr
28205 63 49 64 78 3d 70 53 72 63 2d 3e 70 49 6e 64 65  cIdx=pSrc->pInde
28206 78 3b 20 70 53 72 63 49 64 78 3b 20 70 53 72 63  x; pSrcIdx; pSrc
28207 49 64 78 3d 70 53 72 63 49 64 78 2d 3e 70 4e 65  Idx=pSrcIdx->pNe
28208 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20 78  xt){.      if( x
28209 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 49 6e 64  ferCompatibleInd
2820a 65 78 28 70 44 65 73 74 49 64 78 2c 20 70 53 72  ex(pDestIdx, pSr
2820b 63 49 64 78 29 20 29 20 62 72 65 61 6b 3b 0a 20  cIdx) ) break;. 
2820c 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 53 72     }.    if( pSr
2820d 63 49 64 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20  cIdx==0 ){.     
2820e 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a   return 0;    /*
2820f 20 70 44 65 73 74 49 64 78 20 68 61 73 20 6e 6f   pDestIdx has no
28210 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 69   corresponding i
28211 6e 64 65 78 20 69 6e 20 70 53 72 63 20 2a 2f 0a  ndex in pSrc */.
28212 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 6e 64 65      }.  }.#ifnde
28213 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48  f SQLITE_OMIT_CH
28214 45 43 4b 0a 20 20 69 66 28 20 70 44 65 73 74 2d  ECK.  if( pDest-
28215 3e 70 43 68 65 63 6b 20 26 26 20 21 73 71 6c 69  >pCheck && !sqli
28216 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70  te3ExprCompare(p
28217 53 72 63 2d 3e 70 43 68 65 63 6b 2c 20 70 44 65  Src->pCheck, pDe
28218 73 74 2d 3e 70 43 68 65 63 6b 29 20 29 7b 0a 20  st->pCheck) ){. 
28219 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
2821a 2a 20 54 61 62 6c 65 73 20 68 61 76 65 20 64 69  * Tables have di
2821b 66 66 65 72 65 6e 74 20 43 48 45 43 4b 20 63 6f  fferent CHECK co
2821c 6e 73 74 72 61 69 6e 74 73 2e 20 20 54 69 63 6b  nstraints.  Tick
2821d 65 74 20 23 32 32 35 32 20 2a 2f 0a 20 20 7d 0a  et #2252 */.  }.
2821e 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20  #endif..  /* If 
2821f 77 65 20 67 65 74 20 74 68 69 73 20 66 61 72 2c  we get this far,
28220 20 69 74 20 6d 65 61 6e 73 20 65 69 74 68 65 72   it means either
28221 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a  :.  **.  **    *
28222 20 20 20 57 65 20 63 61 6e 20 61 6c 77 61 79 73     We can always
28223 20 64 6f 20 74 68 65 20 74 72 61 6e 73 66 65 72   do the transfer
28224 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f   if the table co
28225 6e 74 61 69 6e 73 20 61 6e 0a 20 20 2a 2a 20 20  ntains an.  **  
28226 20 20 20 20 20 20 61 6e 20 69 6e 74 65 67 65 72        an integer
28227 20 70 72 69 6d 61 72 79 20 6b 65 79 0a 20 20 2a   primary key.  *
28228 2a 0a 20 20 2a 2a 20 20 20 20 2a 20 20 20 57 65  *.  **    *   We
28229 20 63 61 6e 20 63 6f 6e 64 69 74 69 6f 6e 61 6c   can conditional
2822a 6c 79 20 64 6f 20 74 68 65 20 74 72 61 6e 73 66  ly do the transf
2822b 65 72 20 69 66 20 74 68 65 20 64 65 73 74 69 6e  er if the destin
2822c 61 74 69 6f 6e 0a 20 20 2a 2a 20 20 20 20 20 20  ation.  **      
2822d 20 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79    table is empty
2822e 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51  ..  */.#ifdef SQ
2822f 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71 6c 69  LITE_TEST.  sqli
28230 74 65 33 5f 78 66 65 72 6f 70 74 5f 63 6f 75 6e  te3_xferopt_coun
28231 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 69 44  t++;.#endif.  iD
28232 62 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53 63  bSrc = sqlite3Sc
28233 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
28234 73 65 2d 3e 64 62 2c 20 70 53 72 63 2d 3e 70 53  se->db, pSrc->pS
28235 63 68 65 6d 61 29 3b 0a 20 20 76 20 3d 20 73 71  chema);.  v = sq
28236 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
28237 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 43  rse);.  sqlite3C
28238 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28  odeVerifySchema(
28239 70 50 61 72 73 65 2c 20 69 44 62 53 72 63 29 3b  pParse, iDbSrc);
2823a 0a 20 20 69 53 72 63 20 3d 20 70 50 61 72 73 65  .  iSrc = pParse
2823b 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 69 44 65 73  ->nTab++;.  iDes
2823c 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  t = pParse->nTab
2823d 2b 2b 3b 0a 20 20 72 65 67 41 75 74 6f 69 6e 63  ++;.  regAutoinc
2823e 20 3d 20 61 75 74 6f 49 6e 63 42 65 67 69 6e 28   = autoIncBegin(
2823f 70 50 61 72 73 65 2c 20 69 44 62 44 65 73 74 2c  pParse, iDbDest,
28240 20 70 44 65 73 74 29 3b 0a 20 20 73 71 6c 69 74   pDest);.  sqlit
28241 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72  e3OpenTable(pPar
28242 73 65 2c 20 69 44 65 73 74 2c 20 69 44 62 44 65  se, iDest, iDbDe
28243 73 74 2c 20 70 44 65 73 74 2c 20 4f 50 5f 4f 70  st, pDest, OP_Op
28244 65 6e 57 72 69 74 65 29 3b 0a 20 20 69 66 28 20  enWrite);.  if( 
28245 28 70 44 65 73 74 2d 3e 69 50 4b 65 79 3c 30 20  (pDest->iPKey<0 
28246 26 26 20 70 44 65 73 74 2d 3e 70 49 6e 64 65 78  && pDest->pIndex
28247 21 3d 30 29 20 7c 7c 20 64 65 73 74 48 61 73 55  !=0) || destHasU
28248 6e 69 71 75 65 49 64 78 20 29 7b 0a 20 20 20 20  niqueIdx ){.    
28249 2f 2a 20 49 66 20 74 61 62 6c 65 73 20 64 6f 20  /* If tables do 
2824a 6e 6f 74 20 68 61 76 65 20 61 6e 20 49 4e 54 45  not have an INTE
2824b 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20  GER PRIMARY KEY 
2824c 61 6e 64 20 74 68 65 72 65 0a 20 20 20 20 2a 2a  and there.    **
2824d 20 61 72 65 20 69 6e 64 69 63 65 73 20 74 6f 20   are indices to 
2824e 62 65 20 63 6f 70 69 65 64 20 61 6e 64 20 74 68  be copied and th
2824f 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73  e destination is
28250 20 6e 6f 74 20 65 6d 70 74 79 2c 0a 20 20 20 20   not empty,.    
28251 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 64 69  ** we have to di
28252 73 61 6c 6c 6f 77 20 74 68 65 20 74 72 61 6e 73  sallow the trans
28253 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  fer optimization
28254 20 62 65 63 61 75 73 65 20 74 68 65 0a 20 20 20   because the.   
28255 20 2a 2a 20 74 68 65 20 72 6f 77 69 64 73 20 6d   ** the rowids m
28256 69 67 68 74 20 63 68 61 6e 67 65 20 77 68 69 63  ight change whic
28257 68 20 77 69 6c 6c 20 6d 65 73 73 20 75 70 20 69  h will mess up i
28258 6e 64 65 78 69 6e 67 2e 0a 20 20 20 20 2a 2a 0a  ndexing..    **.
28259 20 20 20 20 2a 2a 20 4f 72 20 69 66 20 74 68 65      ** Or if the
2825a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 68 61 73   destination has
2825b 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78 20   a UNIQUE index 
2825c 61 6e 64 20 69 73 20 6e 6f 74 20 65 6d 70 74 79  and is not empty
2825d 2c 0a 20 20 20 20 2a 2a 20 77 65 20 61 6c 73 6f  ,.    ** we also
2825e 20 64 69 73 61 6c 6c 6f 77 20 74 68 65 20 74 72   disallow the tr
2825f 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74  ansfer optimizat
28260 69 6f 6e 20 62 65 63 61 75 73 65 20 77 65 20 63  ion because we c
28261 61 6e 6e 6f 74 0a 20 20 20 20 2a 2a 20 69 6e 73  annot.    ** ins
28262 75 72 65 20 74 68 61 74 20 61 6c 6c 20 65 6e 74  ure that all ent
28263 72 69 65 73 20 69 6e 20 74 68 65 20 75 6e 69 6f  ries in the unio
28264 6e 20 6f 66 20 44 45 53 54 20 61 6e 64 20 53 52  n of DEST and SR
28265 43 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 2a 2a  C will be.    **
28266 20 75 6e 69 71 75 65 2e 0a 20 20 20 20 2a 2f 0a   unique..    */.
28267 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69      addr1 = sqli
28268 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
28269 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 44 65 73   OP_Rewind, iDes
2826a 74 2c 20 30 29 3b 0a 20 20 20 20 65 6d 70 74 79  t, 0);.    empty
2826b 44 65 73 74 54 65 73 74 20 3d 20 73 71 6c 69 74  DestTest = sqlit
2826c 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2826d 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 3b 0a  OP_Goto, 0, 0);.
2826e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
2826f 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 31  umpHere(v, addr1
28270 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
28271 65 6d 70 74 79 44 65 73 74 54 65 73 74 20 3d 20  emptyDestTest = 
28272 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  0;.  }.  sqlite3
28273 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65  OpenTable(pParse
28274 2c 20 69 53 72 63 2c 20 69 44 62 53 72 63 2c 20  , iSrc, iDbSrc, 
28275 70 53 72 63 2c 20 4f 50 5f 4f 70 65 6e 52 65 61  pSrc, OP_OpenRea
28276 64 29 3b 0a 20 20 65 6d 70 74 79 53 72 63 54 65  d);.  emptySrcTe
28277 73 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  st = sqlite3Vdbe
28278 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77  AddOp2(v, OP_Rew
28279 69 6e 64 2c 20 69 53 72 63 2c 20 30 29 3b 0a 20  ind, iSrc, 0);. 
2827a 20 72 65 67 44 61 74 61 20 3d 20 73 71 6c 69 74   regData = sqlit
2827b 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2827c 72 73 65 29 3b 0a 20 20 72 65 67 52 6f 77 69 64  rse);.  regRowid
2827d 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2827e 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
2827f 69 66 28 20 70 44 65 73 74 2d 3e 69 50 4b 65 79  if( pDest->iPKey
28280 3e 3d 30 20 29 7b 0a 20 20 20 20 61 64 64 72 31  >=0 ){.    addr1
28281 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
28282 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64  dOp2(v, OP_Rowid
28283 2c 20 69 53 72 63 2c 20 72 65 67 52 6f 77 69 64  , iSrc, regRowid
28284 29 3b 0a 20 20 20 20 61 64 64 72 32 20 3d 20 73  );.    addr2 = s
28285 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
28286 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  (v, OP_NotExists
28287 2c 20 69 44 65 73 74 2c 20 30 2c 20 72 65 67 52  , iDest, 0, regR
28288 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  owid);.    sqlit
28289 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74  e3HaltConstraint
2828a 28 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65  (.        pParse
2828b 2c 20 6f 6e 45 72 72 6f 72 2c 20 22 50 52 49 4d  , onError, "PRIM
2828c 41 52 59 20 4b 45 59 20 6d 75 73 74 20 62 65 20  ARY KEY must be 
2828d 75 6e 69 71 75 65 22 2c 20 50 34 5f 53 54 41 54  unique", P4_STAT
2828e 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  IC);.    sqlite3
2828f 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
28290 61 64 64 72 32 29 3b 0a 20 20 20 20 61 75 74 6f  addr2);.    auto
28291 49 6e 63 53 74 65 70 28 70 50 61 72 73 65 2c 20  IncStep(pParse, 
28292 72 65 67 41 75 74 6f 69 6e 63 2c 20 72 65 67 52  regAutoinc, regR
28293 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73 65 20 69  owid);.  }else i
28294 66 28 20 70 44 65 73 74 2d 3e 70 49 6e 64 65 78  f( pDest->pIndex
28295 3d 3d 30 20 29 7b 0a 20 20 20 20 61 64 64 72 31  ==0 ){.    addr1
28296 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
28297 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f  dOp2(v, OP_NewRo
28298 77 69 64 2c 20 69 44 65 73 74 2c 20 72 65 67 52  wid, iDest, regR
28299 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  owid);.  }else{.
2829a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69      addr1 = sqli
2829b 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2829c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 53 72 63 2c   OP_Rowid, iSrc,
2829d 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
2829e 61 73 73 65 72 74 28 20 28 70 44 65 73 74 2d 3e  assert( (pDest->
2829f 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75  tabFlags & TF_Au
282a0 74 6f 69 6e 63 72 65 6d 65 6e 74 29 3d 3d 30 20  toincrement)==0 
282a1 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
282a2 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
282a3 5f 52 6f 77 44 61 74 61 2c 20 69 53 72 63 2c 20  _RowData, iSrc, 
282a4 72 65 67 44 61 74 61 29 3b 0a 20 20 73 71 6c 69  regData);.  sqli
282a5 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
282a6 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69 44 65 73   OP_Insert, iDes
282a7 74 2c 20 72 65 67 44 61 74 61 2c 20 72 65 67 52  t, regData, regR
282a8 6f 77 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33  owid);.  sqlite3
282a9 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
282aa 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 7c 4f  OPFLAG_NCHANGE|O
282ab 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 7c  PFLAG_LASTROWID|
282ac 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a  OPFLAG_APPEND);.
282ad 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
282ae 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 44 65  ngeP4(v, -1, pDe
282af 73 74 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20  st->zName, 0);. 
282b0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
282b1 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69  p2(v, OP_Next, i
282b2 53 72 63 2c 20 61 64 64 72 31 29 3b 0a 20 20 66  Src, addr1);.  f
282b3 6f 72 28 70 44 65 73 74 49 64 78 3d 70 44 65 73  or(pDestIdx=pDes
282b4 74 2d 3e 70 49 6e 64 65 78 3b 20 70 44 65 73 74  t->pIndex; pDest
282b5 49 64 78 3b 20 70 44 65 73 74 49 64 78 3d 70 44  Idx; pDestIdx=pD
282b6 65 73 74 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  estIdx->pNext){.
282b7 20 20 20 20 66 6f 72 28 70 53 72 63 49 64 78 3d      for(pSrcIdx=
282b8 70 53 72 63 2d 3e 70 49 6e 64 65 78 3b 20 41 4c  pSrc->pIndex; AL
282b9 57 41 59 53 28 70 53 72 63 49 64 78 29 3b 20 70  WAYS(pSrcIdx); p
282ba 53 72 63 49 64 78 3d 70 53 72 63 49 64 78 2d 3e  SrcIdx=pSrcIdx->
282bb 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66  pNext){.      if
282bc 28 20 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65  ( xferCompatible
282bd 49 6e 64 65 78 28 70 44 65 73 74 49 64 78 2c 20  Index(pDestIdx, 
282be 70 53 72 63 49 64 78 29 20 29 20 62 72 65 61 6b  pSrcIdx) ) break
282bf 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
282c0 72 74 28 20 70 53 72 63 49 64 78 20 29 3b 0a 20  rt( pSrcIdx );. 
282c1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
282c2 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp2(v, OP_Close
282c3 2c 20 69 53 72 63 2c 20 30 29 3b 0a 20 20 20 20  , iSrc, 0);.    
282c4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
282c5 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69  2(v, OP_Close, i
282c6 44 65 73 74 2c 20 30 29 3b 0a 20 20 20 20 70 4b  Dest, 0);.    pK
282c7 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65  ey = sqlite3Inde
282c8 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
282c9 20 70 53 72 63 49 64 78 29 3b 0a 20 20 20 20 73   pSrcIdx);.    s
282ca 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
282cb 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c  (v, OP_OpenRead,
282cc 20 69 53 72 63 2c 20 70 53 72 63 49 64 78 2d 3e   iSrc, pSrcIdx->
282cd 74 6e 75 6d 2c 20 69 44 62 53 72 63 2c 0a 20 20  tnum, iDbSrc,.  
282ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
282cf 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c      (char*)pKey,
282d0 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
282d1 4f 46 46 29 3b 0a 20 20 20 20 56 64 62 65 43 6f  OFF);.    VdbeCo
282d2 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20  mment((v, "%s", 
282d3 70 53 72 63 49 64 78 2d 3e 7a 4e 61 6d 65 29 29  pSrcIdx->zName))
282d4 3b 0a 20 20 20 20 70 4b 65 79 20 3d 20 73 71 6c  ;.    pKey = sql
282d5 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f  ite3IndexKeyinfo
282d6 28 70 50 61 72 73 65 2c 20 70 44 65 73 74 49 64  (pParse, pDestId
282d7 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  x);.    sqlite3V
282d8 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
282d9 4f 70 65 6e 57 72 69 74 65 2c 20 69 44 65 73 74  OpenWrite, iDest
282da 2c 20 70 44 65 73 74 49 64 78 2d 3e 74 6e 75 6d  , pDestIdx->tnum
282db 2c 20 69 44 62 44 65 73 74 2c 0a 20 20 20 20 20  , iDbDest,.     
282dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
282dd 20 28 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34   (char*)pKey, P4
282de 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
282df 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  );.    VdbeComme
282e0 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70 44 65  nt((v, "%s", pDe
282e1 73 74 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a  stIdx->zName));.
282e2 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69      addr1 = sqli
282e3 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
282e4 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 53 72 63   OP_Rewind, iSrc
282e5 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
282e6 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
282e7 50 5f 52 6f 77 4b 65 79 2c 20 69 53 72 63 2c 20  P_RowKey, iSrc, 
282e8 72 65 67 44 61 74 61 29 3b 0a 20 20 20 20 73 71  regData);.    sq
282e9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
282ea 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c  v, OP_IdxInsert,
282eb 20 69 44 65 73 74 2c 20 72 65 67 44 61 74 61 2c   iDest, regData,
282ec 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   1);.    sqlite3
282ed 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
282ee 5f 4e 65 78 74 2c 20 69 53 72 63 2c 20 61 64 64  _Next, iSrc, add
282ef 72 31 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  r1+1);.    sqlit
282f0 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
282f1 2c 20 61 64 64 72 31 29 3b 0a 20 20 7d 0a 20 20  , addr1);.  }.  
282f2 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
282f3 65 72 65 28 76 2c 20 65 6d 70 74 79 53 72 63 54  ere(v, emptySrcT
282f4 65 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 52  est);.  sqlite3R
282f5 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
282f6 61 72 73 65 2c 20 72 65 67 52 6f 77 69 64 29 3b  arse, regRowid);
282f7 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
282f8 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
282f9 20 72 65 67 44 61 74 61 29 3b 0a 20 20 73 71 6c   regData);.  sql
282fa 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
282fb 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 53 72 63  , OP_Close, iSrc
282fc 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  , 0);.  sqlite3V
282fd 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
282fe 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20 30 29  Close, iDest, 0)
282ff 3b 0a 20 20 69 66 28 20 65 6d 70 74 79 44 65 73  ;.  if( emptyDes
28300 74 54 65 73 74 20 29 7b 0a 20 20 20 20 73 71 6c  tTest ){.    sql
28301 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
28302 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54  , OP_Halt, SQLIT
28303 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 73 71  E_OK, 0);.    sq
28304 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
28305 65 28 76 2c 20 65 6d 70 74 79 44 65 73 74 54 65  e(v, emptyDestTe
28306 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  st);.    sqlite3
28307 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
28308 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20 30  _Close, iDest, 0
28309 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
2830a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
2830b 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65  turn 1;.  }.}.#e
2830c 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2830d 4d 49 54 5f 58 46 45 52 5f 4f 50 54 20 2a 2f 0a  MIT_XFER_OPT */.
2830e 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2830f 20 45 6e 64 20 6f 66 20 69 6e 73 65 72 74 2e 63   End of insert.c
28310 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
28311 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28312 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
28313 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
28314 20 42 65 67 69 6e 20 66 69 6c 65 20 6c 65 67 61   Begin file lega
28315 63 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  cy.c ***********
28316 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28317 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
28318 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
28319 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
2831a 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
2831b 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
2831c 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
2831d 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
2831e 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
2831f 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
28320 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
28321 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
28322 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
28323 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
28324 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
28325 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
28326 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
28327 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
28328 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
28329 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
2832a 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
2832b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2832c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2832d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2832e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2832f 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d 61 69 6e 20  *******.** Main 
28330 66 69 6c 65 20 66 6f 72 20 74 68 65 20 53 51 4c  file for the SQL
28331 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68  ite library.  Th
28332 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68  e routines in th
28333 69 73 20 66 69 6c 65 0a 2a 2a 20 69 6d 70 6c 65  is file.** imple
28334 6d 65 6e 74 20 74 68 65 20 70 72 6f 67 72 61 6d  ment the program
28335 6d 65 72 20 69 6e 74 65 72 66 61 63 65 20 74 6f  mer interface to
28336 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20 52   the library.  R
28337 6f 75 74 69 6e 65 73 20 69 6e 0a 2a 2a 20 6f 74  outines in.** ot
28338 68 65 72 20 66 69 6c 65 73 20 61 72 65 20 66 6f  her files are fo
28339 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 62  r internal use b
2833a 79 20 53 51 4c 69 74 65 20 61 6e 64 20 73 68 6f  y SQLite and sho
2833b 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 61 63  uld not be.** ac
2833c 63 65 73 73 65 64 20 62 79 20 75 73 65 72 73 20  cessed by users 
2833d 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a  of the library..
2833e 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6c 65 67 61 63  **.** $Id: legac
2833f 79 2e 63 2c 76 20 31 2e 33 35 20 32 30 30 39 2f  y.c,v 1.35 2009/
28340 30 38 2f 30 37 20 31 36 3a 35 36 3a 30 30 20 64  08/07 16:56:00 d
28341 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
28342 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63  .*/.../*.** Exec
28343 75 74 65 20 53 51 4c 20 63 6f 64 65 2e 20 20 52  ute SQL code.  R
28344 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65  eturn one of the
28345 20 53 51 4c 49 54 45 5f 20 73 75 63 63 65 73 73   SQLITE_ success
28346 2f 66 61 69 6c 75 72 65 0a 2a 2a 20 63 6f 64 65  /failure.** code
28347 73 2e 20 20 41 6c 73 6f 20 77 72 69 74 65 20 61  s.  Also write a
28348 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
28349 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  into memory obta
2834a 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c  ined from.** mal
2834b 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b 65 20 2a  loc() and make *
2834c 70 7a 45 72 72 4d 73 67 20 70 6f 69 6e 74 20 74  pzErrMsg point t
2834d 6f 20 74 68 61 74 20 6d 65 73 73 61 67 65 2e 0a  o that message..
2834e 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c  **.** If the SQL
2834f 20 69 73 20 61 20 71 75 65 72 79 2c 20 74 68 65   is a query, the
28350 6e 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69  n for each row i
28351 6e 20 74 68 65 20 71 75 65 72 79 20 72 65 73 75  n the query resu
28352 6c 74 0a 2a 2a 20 74 68 65 20 78 43 61 6c 6c 62  lt.** the xCallb
28353 61 63 6b 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  ack() function i
28354 73 20 63 61 6c 6c 65 64 2e 20 20 70 41 72 67 20  s called.  pArg 
28355 62 65 63 6f 6d 65 73 20 74 68 65 20 66 69 72 73  becomes the firs
28356 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f  t.** argument to
28357 20 78 43 61 6c 6c 62 61 63 6b 28 29 2e 20 20 49   xCallback().  I
28358 66 20 78 43 61 6c 6c 62 61 63 6b 3d 4e 55 4c 4c  f xCallback=NULL
28359 20 74 68 65 6e 20 6e 6f 20 63 61 6c 6c 62 61 63   then no callbac
2835a 6b 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64 2c  k.** is invoked,
2835b 20 65 76 65 6e 20 66 6f 72 20 71 75 65 72 69 65   even for querie
2835c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
2835d 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 65   int sqlite3_exe
2835e 63 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  c(.  sqlite3 *db
2835f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
28360 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
28361 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 53 51   on which the SQ
28362 4c 20 65 78 65 63 75 74 65 73 20 2a 2f 0a 20 20  L executes */.  
28363 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
28364 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ,           /* T
28365 68 65 20 53 51 4c 20 74 6f 20 62 65 20 65 78 65  he SQL to be exe
28366 63 75 74 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74  cuted */.  sqlit
28367 65 33 5f 63 61 6c 6c 62 61 63 6b 20 78 43 61 6c  e3_callback xCal
28368 6c 62 61 63 6b 2c 20 2f 2a 20 49 6e 76 6f 6b 65  lback, /* Invoke
28369 20 74 68 69 73 20 63 61 6c 6c 62 61 63 6b 20 72   this callback r
2836a 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 76 6f 69 64  outine */.  void
2836b 20 2a 70 41 72 67 2c 20 20 20 20 20 20 20 20 20   *pArg,         
2836c 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
2836d 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 43 61   argument to xCa
2836e 6c 6c 62 61 63 6b 28 29 20 2a 2f 0a 20 20 63 68  llback() */.  ch
2836f 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20  ar **pzErrMsg   
28370 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
28371 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  te error message
28372 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  s here */.){.  i
28373 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
28374 4b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  K;         /* Re
28375 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 63  turn code */.  c
28376 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74  onst char *zLeft
28377 6f 76 65 72 3b 20 20 20 20 20 20 2f 2a 20 54 61  over;      /* Ta
28378 69 6c 20 6f 66 20 75 6e 70 72 6f 63 65 73 73 65  il of unprocesse
28379 64 20 53 51 4c 20 2a 2f 0a 20 20 73 71 6c 69 74  d SQL */.  sqlit
2837a 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 20 3d  e3_stmt *pStmt =
2837b 20 30 3b 20 20 20 20 2f 2a 20 54 68 65 20 63 75   0;    /* The cu
2837c 72 72 65 6e 74 20 53 51 4c 20 73 74 61 74 65 6d  rrent SQL statem
2837d 65 6e 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ent */.  char **
2837e 61 7a 43 6f 6c 73 20 3d 20 30 3b 20 20 20 20 20  azCols = 0;     
2837f 20 20 20 20 20 2f 2a 20 4e 61 6d 65 73 20 6f 66       /* Names of
28380 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20   result columns 
28381 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 74 72 79 20  */.  int nRetry 
28382 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
28383 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65   /* Number of re
28384 74 72 79 20 61 74 74 65 6d 70 74 73 20 2a 2f 0a  try attempts */.
28385 20 20 69 6e 74 20 63 61 6c 6c 62 61 63 6b 49 73    int callbackIs
28386 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  Init;         /*
28387 20 54 72 75 65 20 69 66 20 63 61 6c 6c 62 61 63   True if callbac
28388 6b 20 64 61 74 61 20 69 73 20 69 6e 69 74 69 61  k data is initia
28389 6c 69 7a 65 64 20 2a 2f 0a 0a 20 20 69 66 28 20  lized */..  if( 
2838a 7a 53 71 6c 3d 3d 30 20 29 20 7a 53 71 6c 20 3d  zSql==0 ) zSql =
2838b 20 22 22 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f   "";..  sqlite3_
2838c 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
2838d 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65  mutex);.  sqlite
2838e 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
2838f 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 77 68 69 6c  E_OK, 0);.  whil
28390 65 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  e( (rc==SQLITE_O
28391 4b 20 7c 7c 20 28 72 63 3d 3d 53 51 4c 49 54 45  K || (rc==SQLITE
28392 5f 53 43 48 45 4d 41 20 26 26 20 28 2b 2b 6e 52  _SCHEMA && (++nR
28393 65 74 72 79 29 3c 32 29 29 20 26 26 20 7a 53 71  etry)<2)) && zSq
28394 6c 5b 30 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20  l[0] ){.    int 
28395 6e 43 6f 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a  nCol;.    char *
28396 2a 61 7a 56 61 6c 73 20 3d 20 30 3b 0a 0a 20 20  *azVals = 0;..  
28397 20 20 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20    pStmt = 0;.   
28398 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72   rc = sqlite3_pr
28399 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20  epare(db, zSql, 
2839a 2d 31 2c 20 26 70 53 74 6d 74 2c 20 26 7a 4c 65  -1, &pStmt, &zLe
2839b 66 74 6f 76 65 72 29 3b 0a 20 20 20 20 61 73 73  ftover);.    ass
2839c 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
2839d 4f 4b 20 7c 7c 20 70 53 74 6d 74 3d 3d 30 20 29  OK || pStmt==0 )
2839e 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
2839f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
283a0 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d   continue;.    }
283a1 0a 20 20 20 20 69 66 28 20 21 70 53 74 6d 74 20  .    if( !pStmt 
283a2 29 7b 0a 20 20 20 20 20 20 2f 2a 20 74 68 69 73  ){.      /* this
283a3 20 68 61 70 70 65 6e 73 20 66 6f 72 20 61 20 63   happens for a c
283a4 6f 6d 6d 65 6e 74 20 6f 72 20 77 68 69 74 65 2d  omment or white-
283a5 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 7a  space */.      z
283a6 53 71 6c 20 3d 20 7a 4c 65 66 74 6f 76 65 72 3b  Sql = zLeftover;
283a7 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b  .      continue;
283a8 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 6c 6c  .    }..    call
283a9 62 61 63 6b 49 73 49 6e 69 74 20 3d 20 30 3b 0a  backIsInit = 0;.
283aa 20 20 20 20 6e 43 6f 6c 20 3d 20 73 71 6c 69 74      nCol = sqlit
283ab 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28  e3_column_count(
283ac 70 53 74 6d 74 29 3b 0a 0a 20 20 20 20 77 68 69  pStmt);..    whi
283ad 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 20 20 69  le( 1 ){.      i
283ae 6e 74 20 69 3b 0a 20 20 20 20 20 20 72 63 20 3d  nt i;.      rc =
283af 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53   sqlite3_step(pS
283b0 74 6d 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  tmt);..      /* 
283b1 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62  Invoke the callb
283b2 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 69 66 20  ack function if 
283b3 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 20 20  required */.    
283b4 20 20 69 66 28 20 78 43 61 6c 6c 62 61 63 6b 20    if( xCallback 
283b5 26 26 20 28 53 51 4c 49 54 45 5f 52 4f 57 3d 3d  && (SQLITE_ROW==
283b6 72 63 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20  rc || .         
283b7 20 28 53 51 4c 49 54 45 5f 44 4f 4e 45 3d 3d 72   (SQLITE_DONE==r
283b8 63 20 26 26 20 21 63 61 6c 6c 62 61 63 6b 49 73  c && !callbackIs
283b9 49 6e 69 74 0a 20 20 20 20 20 20 20 20 20 20 20  Init.           
283ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
283bb 26 26 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  && db->flags&SQL
283bc 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b  ITE_NullCallback
283bd 29 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  )) ){.        if
283be 28 20 21 63 61 6c 6c 62 61 63 6b 49 73 49 6e 69  ( !callbackIsIni
283bf 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  t ){.          a
283c0 7a 43 6f 6c 73 20 3d 20 73 71 6c 69 74 65 33 44  zCols = sqlite3D
283c1 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
283c2 32 2a 6e 43 6f 6c 2a 73 69 7a 65 6f 66 28 63 6f  2*nCol*sizeof(co
283c3 6e 73 74 20 63 68 61 72 2a 29 20 2b 20 31 29 3b  nst char*) + 1);
283c4 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61  .          if( a
283c5 7a 43 6f 6c 73 3d 3d 30 20 29 7b 0a 20 20 20 20  zCols==0 ){.    
283c6 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 65          goto exe
283c7 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 20  c_out;.         
283c8 20 7d 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72   }.          for
283c9 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
283ca 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  +){.            
283cb 61 7a 43 6f 6c 73 5b 69 5d 20 3d 20 28 63 68 61  azCols[i] = (cha
283cc 72 20 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75  r *)sqlite3_colu
283cd 6d 6e 5f 6e 61 6d 65 28 70 53 74 6d 74 2c 20 69  mn_name(pStmt, i
283ce 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f  );.            /
283cf 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74  * sqlite3VdbeSet
283d0 43 6f 6c 4e 61 6d 65 28 29 20 69 6e 73 74 61 6c  ColName() instal
283d1 6c 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  ls column names 
283d2 61 73 20 55 54 46 38 0a 20 20 20 20 20 20 20 20  as UTF8.        
283d3 20 20 20 20 2a 2a 20 73 74 72 69 6e 67 73 20 73      ** strings s
283d4 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61  o there is no wa
283d5 79 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f  y for sqlite3_co
283d6 6c 75 6d 6e 5f 6e 61 6d 65 28 29 20 74 6f 20 66  lumn_name() to f
283d7 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ail. */.        
283d8 20 20 20 20 61 73 73 65 72 74 28 20 61 7a 43 6f      assert( azCo
283d9 6c 73 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 20 20  ls[i]!=0 );.    
283da 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
283db 20 20 63 61 6c 6c 62 61 63 6b 49 73 49 6e 69 74    callbackIsInit
283dc 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 1;.        }.
283dd 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
283de 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
283df 20 20 20 20 20 20 20 20 61 7a 56 61 6c 73 20 3d          azVals =
283e0 20 26 61 7a 43 6f 6c 73 5b 6e 43 6f 6c 5d 3b 0a   &azCols[nCol];.
283e1 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d            for(i=
283e2 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
283e3 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 7a 56  .            azV
283e4 61 6c 73 5b 69 5d 20 3d 20 28 63 68 61 72 20 2a  als[i] = (char *
283e5 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  )sqlite3_column_
283e6 74 65 78 74 28 70 53 74 6d 74 2c 20 69 29 3b 0a  text(pStmt, i);.
283e7 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
283e8 21 61 7a 56 61 6c 73 5b 69 5d 20 26 26 20 73 71  !azVals[i] && sq
283e9 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
283ea 65 28 70 53 74 6d 74 2c 20 69 29 21 3d 53 51 4c  e(pStmt, i)!=SQL
283eb 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  ITE_NULL ){.    
283ec 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61            db->ma
283ed 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
283ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67 6f                go
283ef 74 6f 20 65 78 65 63 5f 6f 75 74 3b 0a 20 20 20  to exec_out;.   
283f0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
283f1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
283f2 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 43 61  .        if( xCa
283f3 6c 6c 62 61 63 6b 28 70 41 72 67 2c 20 6e 43 6f  llback(pArg, nCo
283f4 6c 2c 20 61 7a 56 61 6c 73 2c 20 61 7a 43 6f 6c  l, azVals, azCol
283f5 73 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  s) ){.          
283f6 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52  rc = SQLITE_ABOR
283f7 54 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  T;.          sql
283f8 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65  ite3VdbeFinalize
283f9 28 28 56 64 62 65 20 2a 29 70 53 74 6d 74 29 3b  ((Vdbe *)pStmt);
283fa 0a 20 20 20 20 20 20 20 20 20 20 70 53 74 6d 74  .          pStmt
283fb 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
283fc 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
283fd 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 2c 20 30   SQLITE_ABORT, 0
283fe 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  );.          got
283ff 6f 20 65 78 65 63 5f 6f 75 74 3b 0a 20 20 20 20  o exec_out;.    
28400 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20      }.      }.. 
28401 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
28402 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
28403 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
28404 64 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64 62  dbeFinalize((Vdb
28405 65 20 2a 29 70 53 74 6d 74 29 3b 0a 20 20 20 20  e *)pStmt);.    
28406 20 20 20 20 70 53 74 6d 74 20 3d 20 30 3b 0a 20      pStmt = 0;. 
28407 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
28408 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 7b 0a  QLITE_SCHEMA ){.
28409 20 20 20 20 20 20 20 20 20 20 6e 52 65 74 72 79            nRetry
2840a 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
2840b 7a 53 71 6c 20 3d 20 7a 4c 65 66 74 6f 76 65 72  zSql = zLeftover
2840c 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c  ;.          whil
2840d 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63  e( sqlite3Isspac
2840e 65 28 7a 53 71 6c 5b 30 5d 29 20 29 20 7a 53 71  e(zSql[0]) ) zSq
2840f 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  l++;.        }. 
28410 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
28411 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
28412 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
28413 62 2c 20 61 7a 43 6f 6c 73 29 3b 0a 20 20 20 20  b, azCols);.    
28414 61 7a 43 6f 6c 73 20 3d 20 30 3b 0a 20 20 7d 0a  azCols = 0;.  }.
28415 0a 65 78 65 63 5f 6f 75 74 3a 0a 20 20 69 66 28  .exec_out:.  if(
28416 20 70 53 74 6d 74 20 29 20 73 71 6c 69 74 65 33   pStmt ) sqlite3
28417 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64  VdbeFinalize((Vd
28418 62 65 20 2a 29 70 53 74 6d 74 29 3b 0a 20 20 73  be *)pStmt);.  s
28419 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2841a 20 61 7a 43 6f 6c 73 29 3b 0a 0a 20 20 72 63 20   azCols);..  rc 
2841b 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
2841c 28 64 62 2c 20 72 63 29 3b 0a 20 20 69 66 28 20  (db, rc);.  if( 
2841d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc!=SQLITE_OK &&
2841e 20 41 4c 57 41 59 53 28 72 63 3d 3d 73 71 6c 69   ALWAYS(rc==sqli
2841f 74 65 33 5f 65 72 72 63 6f 64 65 28 64 62 29 29  te3_errcode(db))
28420 20 26 26 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a   && pzErrMsg ){.
28421 20 20 20 20 69 6e 74 20 6e 45 72 72 4d 73 67 20      int nErrMsg 
28422 3d 20 31 20 2b 20 73 71 6c 69 74 65 33 53 74 72  = 1 + sqlite3Str
28423 6c 65 6e 33 30 28 73 71 6c 69 74 65 33 5f 65 72  len30(sqlite3_er
28424 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 20 20 2a  rmsg(db));.    *
28425 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74  pzErrMsg = sqlit
28426 65 33 4d 61 6c 6c 6f 63 28 6e 45 72 72 4d 73 67  e3Malloc(nErrMsg
28427 29 3b 0a 20 20 20 20 69 66 28 20 2a 70 7a 45 72  );.    if( *pzEr
28428 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 6d 65  rMsg ){.      me
28429 6d 63 70 79 28 2a 70 7a 45 72 72 4d 73 67 2c 20  mcpy(*pzErrMsg, 
2842a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64  sqlite3_errmsg(d
2842b 62 29 2c 20 6e 45 72 72 4d 73 67 29 3b 0a 20 20  b), nErrMsg);.  
2842c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
2842d 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
2842e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
2842f 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
28430 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 20 7d  NOMEM, 0);.    }
28431 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 7a 45  .  }else if( pzE
28432 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 2a 70 7a  rrMsg ){.    *pz
28433 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a  ErrMsg = 0;.  }.
28434 0a 20 20 61 73 73 65 72 74 28 20 28 72 63 26 64  .  assert( (rc&d
28435 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20  b->errMask)==rc 
28436 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
28437 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
28438 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
28439 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
2843a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6c 65 67 61  **** End of lega
2843b 63 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  cy.c ***********
2843c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2843d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2843e 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2843f 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
28440 6c 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a  loadext.c ******
28441 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28442 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28443 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20  ***/./*.** 2006 
28444 4a 75 6e 65 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65  June 7.**.** The
28445 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
28446 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
28447 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
28448 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
28449 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
2844a 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
2844b 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
2844c 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
2844d 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
2844e 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
2844f 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
28450 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
28451 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
28452 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
28453 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
28454 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
28455 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
28456 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28457 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28458 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28459 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2845a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
2845b 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
2845c 20 75 73 65 64 20 74 6f 20 64 79 6e 61 6d 69 63   used to dynamic
2845d 61 6c 6c 79 20 6c 6f 61 64 20 65 78 74 65 6e 73  ally load extens
2845e 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a 20 74 68 65  ions into.** the
2845f 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e   SQLite library.
28460 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6c 6f 61 64  .**.** $Id: load
28461 65 78 74 2e 63 2c 76 20 31 2e 36 30 20 32 30 30  ext.c,v 1.60 200
28462 39 2f 30 36 2f 30 33 20 30 31 3a 32 34 3a 35 34  9/06/03 01:24:54
28463 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 23   drh Exp $.*/..#
28464 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f  ifndef SQLITE_CO
28465 52 45 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c  RE.  #define SQL
28466 49 54 45 5f 43 4f 52 45 20 31 20 20 2f 2a 20 44  ITE_CORE 1  /* D
28467 69 73 61 62 6c 65 20 74 68 65 20 41 50 49 20 72  isable the API r
28468 65 64 65 66 69 6e 69 74 69 6f 6e 20 69 6e 20 73  edefinition in s
28469 71 6c 69 74 65 33 65 78 74 2e 68 20 2a 2f 0a 23  qlite3ext.h */.#
2846a 65 6e 64 69 66 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  endif./*********
2846b 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71  ***** Include sq
2846c 6c 69 74 65 33 65 78 74 2e 68 20 69 6e 20 74 68  lite3ext.h in th
2846d 65 20 6d 69 64 64 6c 65 20 6f 66 20 6c 6f 61 64  e middle of load
2846e 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ext.c **********
2846f 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
28470 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
28471 20 73 71 6c 69 74 65 33 65 78 74 2e 68 20 2a 2a   sqlite3ext.h **
28472 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28473 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28474 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36  ****/./*.** 2006
28475 20 4a 75 6e 65 20 37 0a 2a 2a 0a 2a 2a 20 54 68   June 7.**.** Th
28476 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
28477 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
28478 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
28479 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
2847a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
2847b 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
2847c 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
2847d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
2847e 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
2847f 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
28480 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
28481 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
28482 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
28483 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
28484 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
28485 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
28486 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
28487 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28488 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28489 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2848a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2848b 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68  ******.** This h
2848c 65 61 64 65 72 20 66 69 6c 65 20 64 65 66 69 6e  eader file defin
2848d 65 73 20 74 68 65 20 53 51 4c 69 74 65 20 69 6e  es the SQLite in
2848e 74 65 72 66 61 63 65 20 66 6f 72 20 75 73 65 20  terface for use 
2848f 62 79 0a 2a 2a 20 73 68 61 72 65 64 20 6c 69 62  by.** shared lib
28490 72 61 72 69 65 73 20 74 68 61 74 20 77 61 6e 74  raries that want
28491 20 74 6f 20 62 65 20 69 6d 70 6f 72 74 65 64 20   to be imported 
28492 61 73 20 65 78 74 65 6e 73 69 6f 6e 73 20 69 6e  as extensions in
28493 74 6f 0a 2a 2a 20 61 6e 20 53 51 4c 69 74 65 20  to.** an SQLite 
28494 69 6e 73 74 61 6e 63 65 2e 20 20 53 68 61 72 65  instance.  Share
28495 64 20 6c 69 62 72 61 72 69 65 73 20 74 68 61 74  d libraries that
28496 20 69 6e 74 65 6e 64 20 74 6f 20 62 65 20 6c 6f   intend to be lo
28497 61 64 65 64 0a 2a 2a 20 61 73 20 65 78 74 65 6e  aded.** as exten
28498 73 69 6f 6e 73 20 62 79 20 53 51 4c 69 74 65 20  sions by SQLite 
28499 73 68 6f 75 6c 64 20 23 69 6e 63 6c 75 64 65 20  should #include 
2849a 74 68 69 73 20 66 69 6c 65 20 69 6e 73 74 65 61  this file instea
2849b 64 20 6f 66 20 0a 2a 2a 20 73 71 6c 69 74 65 33  d of .** sqlite3
2849c 2e 68 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24  .h..**.** @(#) $
2849d 49 64 3a 20 73 71 6c 69 74 65 33 65 78 74 2e 68  Id: sqlite3ext.h
2849e 2c 76 20 31 2e 32 35 20 32 30 30 38 2f 31 30 2f  ,v 1.25 2008/10/
2849f 31 32 20 30 30 3a 32 37 3a 35 34 20 73 68 61 6e  12 00:27:54 shan
284a0 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64  e Exp $.*/.#ifnd
284a1 65 66 20 5f 53 51 4c 49 54 45 33 45 58 54 5f 48  ef _SQLITE3EXT_H
284a2 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54  _.#define _SQLIT
284a3 45 33 45 58 54 5f 48 5f 0a 0a 74 79 70 65 64 65  E3EXT_H_..typede
284a4 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
284a5 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 73 71  _api_routines sq
284a6 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e  lite3_api_routin
284a7 65 73 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  es;../*.** The f
284a8 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
284a9 72 65 20 68 6f 6c 64 73 20 70 6f 69 6e 74 65 72  re holds pointer
284aa 73 20 74 6f 20 61 6c 6c 20 6f 66 20 74 68 65 20  s to all of the 
284ab 53 51 4c 69 74 65 20 41 50 49 0a 2a 2a 20 72 6f  SQLite API.** ro
284ac 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 57 41  utines..**.** WA
284ad 52 4e 49 4e 47 3a 20 20 49 6e 20 6f 72 64 65 72  RNING:  In order
284ae 20 74 6f 20 6d 61 69 6e 74 61 69 6e 20 62 61 63   to maintain bac
284af 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69  kwards compatibi
284b0 6c 69 74 79 2c 20 61 64 64 20 6e 65 77 0a 2a 2a  lity, add new.**
284b1 20 69 6e 74 65 72 66 61 63 65 73 20 74 6f 20 74   interfaces to t
284b2 68 65 20 65 6e 64 20 6f 66 20 74 68 69 73 20 73  he end of this s
284b3 74 72 75 63 74 75 72 65 20 6f 6e 6c 79 2e 20 20  tructure only.  
284b4 49 66 20 79 6f 75 20 69 6e 73 65 72 74 20 6e 65  If you insert ne
284b5 77 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20  w.** interfaces 
284b6 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
284b7 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2c   this structure,
284b8 20 74 68 65 6e 20 6f 6c 64 65 72 20 64 69 66 66   then older diff
284b9 65 72 65 6e 74 0a 2a 2a 20 76 65 72 73 69 6f 6e  erent.** version
284ba 73 20 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c  s of SQLite will
284bb 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20   not be able to 
284bc 6c 6f 61 64 20 65 61 63 68 20 6f 74 68 65 72 73  load each others
284bd 27 20 73 68 61 72 65 64 0a 2a 2a 20 6c 69 62 72  ' shared.** libr
284be 61 72 69 65 73 21 0a 2a 2f 0a 73 74 72 75 63 74  aries!.*/.struct
284bf 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75   sqlite3_api_rou
284c0 74 69 6e 65 73 20 7b 0a 20 20 76 6f 69 64 20 2a  tines {.  void *
284c1 20 28 2a 61 67 67 72 65 67 61 74 65 5f 63 6f 6e   (*aggregate_con
284c2 74 65 78 74 29 28 73 71 6c 69 74 65 33 5f 63 6f  text)(sqlite3_co
284c3 6e 74 65 78 74 2a 2c 69 6e 74 20 6e 42 79 74 65  ntext*,int nByte
284c4 73 29 3b 0a 20 20 69 6e 74 20 20 28 2a 61 67 67  s);.  int  (*agg
284c5 72 65 67 61 74 65 5f 63 6f 75 6e 74 29 28 73 71  regate_count)(sq
284c6 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b  lite3_context*);
284c7 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 62  .  int  (*bind_b
284c8 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  lob)(sqlite3_stm
284c9 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  t*,int,const voi
284ca 64 2a 2c 69 6e 74 20 6e 2c 76 6f 69 64 28 2a 29  d*,int n,void(*)
284cb 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20  (void*));.  int 
284cc 20 28 2a 62 69 6e 64 5f 64 6f 75 62 6c 65 29 28   (*bind_double)(
284cd 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
284ce 74 2c 64 6f 75 62 6c 65 29 3b 0a 20 20 69 6e 74  t,double);.  int
284cf 20 20 28 2a 62 69 6e 64 5f 69 6e 74 29 28 73 71    (*bind_int)(sq
284d0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c  lite3_stmt*,int,
284d1 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62  int);.  int  (*b
284d2 69 6e 64 5f 69 6e 74 36 34 29 28 73 71 6c 69 74  ind_int64)(sqlit
284d3 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 73 71 6c  e3_stmt*,int,sql
284d4 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20 20 69 6e  ite_int64);.  in
284d5 74 20 20 28 2a 62 69 6e 64 5f 6e 75 6c 6c 29 28  t  (*bind_null)(
284d6 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
284d7 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e  t);.  int  (*bin
284d8 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e  d_parameter_coun
284d9 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  t)(sqlite3_stmt*
284da 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64  );.  int  (*bind
284db 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78  _parameter_index
284dc 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
284dd 63 6f 6e 73 74 20 63 68 61 72 2a 7a 4e 61 6d 65  const char*zName
284de 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
284df 2a 20 28 2a 62 69 6e 64 5f 70 61 72 61 6d 65 74  * (*bind_paramet
284e0 65 72 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65 33  er_name)(sqlite3
284e1 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 69  _stmt*,int);.  i
284e2 6e 74 20 20 28 2a 62 69 6e 64 5f 74 65 78 74 29  nt  (*bind_text)
284e3 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
284e4 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69  nt,const char*,i
284e5 6e 74 20 6e 2c 76 6f 69 64 28 2a 29 28 76 6f 69  nt n,void(*)(voi
284e6 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62  d*));.  int  (*b
284e7 69 6e 64 5f 74 65 78 74 31 36 29 28 73 71 6c 69  ind_text16)(sqli
284e8 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f  te3_stmt*,int,co
284e9 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f  nst void*,int,vo
284ea 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20  id(*)(void*));. 
284eb 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 76 61 6c   int  (*bind_val
284ec 75 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ue)(sqlite3_stmt
284ed 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 73 71 6c 69  *,int,const sqli
284ee 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69  te3_value*);.  i
284ef 6e 74 20 20 28 2a 62 75 73 79 5f 68 61 6e 64 6c  nt  (*busy_handl
284f0 65 72 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74  er)(sqlite3*,int
284f1 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 76  (*)(void*,int),v
284f2 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  oid*);.  int  (*
284f3 62 75 73 79 5f 74 69 6d 65 6f 75 74 29 28 73 71  busy_timeout)(sq
284f4 6c 69 74 65 33 2a 2c 69 6e 74 20 6d 73 29 3b 0a  lite3*,int ms);.
284f5 20 20 69 6e 74 20 20 28 2a 63 68 61 6e 67 65 73    int  (*changes
284f6 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69  )(sqlite3*);.  i
284f7 6e 74 20 20 28 2a 63 6c 6f 73 65 29 28 73 71 6c  nt  (*close)(sql
284f8 69 74 65 33 2a 29 3b 0a 20 20 69 6e 74 20 20 28  ite3*);.  int  (
284f9 2a 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  *collation_neede
284fa 64 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64  d)(sqlite3*,void
284fb 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c  *,void(*)(void*,
284fc 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65  sqlite3*,int eTe
284fd 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 61 72  xtRep,const char
284fe 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f  *));.  int  (*co
284ff 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36  llation_needed16
28500 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 2a  )(sqlite3*,void*
28501 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73  ,void(*)(void*,s
28502 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78  qlite3*,int eTex
28503 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  tRep,const void*
28504 29 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  ));.  const void
28505 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 62 6c 6f 62   * (*column_blob
28506 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
28507 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74  int iCol);.  int
28508 20 20 28 2a 63 6f 6c 75 6d 6e 5f 62 79 74 65 73    (*column_bytes
28509 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
2850a 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74  int iCol);.  int
2850b 20 20 28 2a 63 6f 6c 75 6d 6e 5f 62 79 74 65 73    (*column_bytes
2850c 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  16)(sqlite3_stmt
2850d 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69  *,int iCol);.  i
2850e 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 63 6f 75  nt  (*column_cou
2850f 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  nt)(sqlite3_stmt
28510 2a 70 53 74 6d 74 29 3b 0a 20 20 63 6f 6e 73 74  *pStmt);.  const
28511 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e   char * (*column
28512 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 29 28  _database_name)(
28513 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
28514 74 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  t);.  const void
28515 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 61 74 61   * (*column_data
28516 62 61 73 65 5f 6e 61 6d 65 31 36 29 28 73 71 6c  base_name16)(sql
28517 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
28518 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20  .  const char * 
28519 28 2a 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70  (*column_decltyp
2851a 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  e)(sqlite3_stmt*
2851b 2c 69 6e 74 20 69 29 3b 0a 20 20 63 6f 6e 73 74  ,int i);.  const
2851c 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e   void * (*column
2851d 5f 64 65 63 6c 74 79 70 65 31 36 29 28 73 71 6c  _decltype16)(sql
2851e 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
2851f 0a 20 20 64 6f 75 62 6c 65 20 20 28 2a 63 6f 6c  .  double  (*col
28520 75 6d 6e 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69  umn_double)(sqli
28521 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43  te3_stmt*,int iC
28522 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f  ol);.  int  (*co
28523 6c 75 6d 6e 5f 69 6e 74 29 28 73 71 6c 69 74 65  lumn_int)(sqlite
28524 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c  3_stmt*,int iCol
28525 29 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  );.  sqlite_int6
28526 34 20 20 28 2a 63 6f 6c 75 6d 6e 5f 69 6e 74 36  4  (*column_int6
28527 34 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  4)(sqlite3_stmt*
28528 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 63 6f  ,int iCol);.  co
28529 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c  nst char * (*col
2852a 75 6d 6e 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65  umn_name)(sqlite
2852b 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20  3_stmt*,int);.  
2852c 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63  const void * (*c
2852d 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 29 28 73 71  olumn_name16)(sq
2852e 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29  lite3_stmt*,int)
2852f 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
28530 20 28 2a 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e   (*column_origin
28531 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 73  _name)(sqlite3_s
28532 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e  tmt*,int);.  con
28533 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75  st void * (*colu
28534 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36  mn_origin_name16
28535 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
28536 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68  int);.  const ch
28537 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 61  ar * (*column_ta
28538 62 6c 65 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65  ble_name)(sqlite
28539 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20  3_stmt*,int);.  
2853a 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63  const void * (*c
2853b 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
2853c 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  16)(sqlite3_stmt
2853d 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20  *,int);.  const 
2853e 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 20  unsigned char * 
2853f 28 2a 63 6f 6c 75 6d 6e 5f 74 65 78 74 29 28 73  (*column_text)(s
28540 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
28541 20 69 43 6f 6c 29 3b 0a 20 20 63 6f 6e 73 74 20   iCol);.  const 
28542 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f  void * (*column_
28543 74 65 78 74 31 36 29 28 73 71 6c 69 74 65 33 5f  text16)(sqlite3_
28544 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b  stmt*,int iCol);
28545 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e  .  int  (*column
28546 5f 74 79 70 65 29 28 73 71 6c 69 74 65 33 5f 73  _type)(sqlite3_s
28547 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a  tmt*,int iCol);.
28548 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a    sqlite3_value*
28549 20 28 2a 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 29   (*column_value)
2854a 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
2854b 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 76 6f 69 64  nt iCol);.  void
2854c 20 2a 20 28 2a 63 6f 6d 6d 69 74 5f 68 6f 6f 6b   * (*commit_hook
2854d 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 28 2a  )(sqlite3*,int(*
2854e 29 28 76 6f 69 64 2a 29 2c 76 6f 69 64 2a 29 3b  )(void*),void*);
2854f 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6d 70 6c 65  .  int  (*comple
28550 74 65 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 73  te)(const char*s
28551 71 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f  ql);.  int  (*co
28552 6d 70 6c 65 74 65 31 36 29 28 63 6f 6e 73 74 20  mplete16)(const 
28553 76 6f 69 64 2a 73 71 6c 29 3b 0a 20 20 69 6e 74  void*sql);.  int
28554 20 20 28 2a 63 72 65 61 74 65 5f 63 6f 6c 6c 61    (*create_colla
28555 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a 2c 63  tion)(sqlite3*,c
28556 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76  onst char*,int,v
28557 6f 69 64 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64  oid*,int(*)(void
28558 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
28559 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
2855a 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 72  *));.  int  (*cr
2855b 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36  eate_collation16
2855c 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  )(sqlite3*,const
2855d 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 2a   void*,int,void*
2855e 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e  ,int(*)(void*,in
2855f 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  t,const void*,in
28560 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 29 3b  t,const void*));
28561 0a 20 20 69 6e 74 20 20 28 2a 63 72 65 61 74 65  .  int  (*create
28562 5f 66 75 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74  _function)(sqlit
28563 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
28564 69 6e 74 2c 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f  int,int,void*,vo
28565 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69  id (*xFunc)(sqli
28566 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
28567 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a  ,sqlite3_value**
28568 29 2c 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28  ),void (*xStep)(
28569 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
2856a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
2856b 75 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 46 69  ue**),void (*xFi
2856c 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  nal)(sqlite3_con
2856d 74 65 78 74 2a 29 29 3b 0a 20 20 69 6e 74 20 20  text*));.  int  
2856e 28 2a 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  (*create_functio
2856f 6e 31 36 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  n16)(sqlite3*,co
28570 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e  nst void*,int,in
28571 74 2c 76 6f 69 64 2a 2c 76 6f 69 64 20 28 2a 78  t,void*,void (*x
28572 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f  Func)(sqlite3_co
28573 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
28574 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f 69 64  e3_value**),void
28575 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65   (*xStep)(sqlite
28576 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
28577 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c  qlite3_value**),
28578 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73  void (*xFinal)(s
28579 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
2857a 29 3b 0a 20 20 69 6e 74 20 28 2a 63 72 65 61 74  );.  int (*creat
2857b 65 5f 6d 6f 64 75 6c 65 29 28 73 71 6c 69 74 65  e_module)(sqlite
2857c 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  3*,const char*,c
2857d 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
2857e 75 6c 65 2a 2c 76 6f 69 64 2a 29 3b 0a 20 20 69  ule*,void*);.  i
2857f 6e 74 20 20 28 2a 64 61 74 61 5f 63 6f 75 6e 74  nt  (*data_count
28580 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 70  )(sqlite3_stmt*p
28581 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  Stmt);.  sqlite3
28582 20 2a 20 28 2a 64 62 5f 68 61 6e 64 6c 65 29 28   * (*db_handle)(
28583 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a  sqlite3_stmt*);.
28584 20 20 69 6e 74 20 28 2a 64 65 63 6c 61 72 65 5f    int (*declare_
28585 76 74 61 62 29 28 73 71 6c 69 74 65 33 2a 2c 63  vtab)(sqlite3*,c
28586 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 69  onst char*);.  i
28587 6e 74 20 20 28 2a 65 6e 61 62 6c 65 5f 73 68 61  nt  (*enable_sha
28588 72 65 64 5f 63 61 63 68 65 29 28 69 6e 74 29 3b  red_cache)(int);
28589 0a 20 20 69 6e 74 20 20 28 2a 65 72 72 63 6f 64  .  int  (*errcod
2858a 65 29 28 73 71 6c 69 74 65 33 2a 64 62 29 3b 0a  e)(sqlite3*db);.
2858b 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28    const char * (
2858c 2a 65 72 72 6d 73 67 29 28 73 71 6c 69 74 65 33  *errmsg)(sqlite3
2858d 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  *);.  const void
2858e 20 2a 20 28 2a 65 72 72 6d 73 67 31 36 29 28 73   * (*errmsg16)(s
2858f 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69 6e 74 20  qlite3*);.  int 
28590 20 28 2a 65 78 65 63 29 28 73 71 6c 69 74 65 33   (*exec)(sqlite3
28591 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71  *,const char*,sq
28592 6c 69 74 65 33 5f 63 61 6c 6c 62 61 63 6b 2c 76  lite3_callback,v
28593 6f 69 64 2a 2c 63 68 61 72 2a 2a 29 3b 0a 20 20  oid*,char**);.  
28594 69 6e 74 20 20 28 2a 65 78 70 69 72 65 64 29 28  int  (*expired)(
28595 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a  sqlite3_stmt*);.
28596 20 20 69 6e 74 20 20 28 2a 66 69 6e 61 6c 69 7a    int  (*finaliz
28597 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  e)(sqlite3_stmt*
28598 70 53 74 6d 74 29 3b 0a 20 20 76 6f 69 64 20 20  pStmt);.  void  
28599 28 2a 66 72 65 65 29 28 76 6f 69 64 2a 29 3b 0a  (*free)(void*);.
2859a 20 20 76 6f 69 64 20 20 28 2a 66 72 65 65 5f 74    void  (*free_t
2859b 61 62 6c 65 29 28 63 68 61 72 2a 2a 72 65 73 75  able)(char**resu
2859c 6c 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 67 65  lt);.  int  (*ge
2859d 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 29 28 73 71  t_autocommit)(sq
2859e 6c 69 74 65 33 2a 29 3b 0a 20 20 76 6f 69 64 20  lite3*);.  void 
2859f 2a 20 28 2a 67 65 74 5f 61 75 78 64 61 74 61 29  * (*get_auxdata)
285a0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
285a1 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28  *,int);.  int  (
285a2 2a 67 65 74 5f 74 61 62 6c 65 29 28 73 71 6c 69  *get_table)(sqli
285a3 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
285a4 2c 63 68 61 72 2a 2a 2a 2c 69 6e 74 2a 2c 69 6e  ,char***,int*,in
285a5 74 2a 2c 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e  t*,char**);.  in
285a6 74 20 20 28 2a 67 6c 6f 62 61 6c 5f 72 65 63 6f  t  (*global_reco
285a7 76 65 72 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f  ver)(void);.  vo
285a8 69 64 20 20 28 2a 69 6e 74 65 72 72 75 70 74 78  id  (*interruptx
285a9 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 73  )(sqlite3*);.  s
285aa 71 6c 69 74 65 5f 69 6e 74 36 34 20 20 28 2a 6c  qlite_int64  (*l
285ab 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64  ast_insert_rowid
285ac 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 63  )(sqlite3*);.  c
285ad 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 6c 69  onst char * (*li
285ae 62 76 65 72 73 69 6f 6e 29 28 76 6f 69 64 29 3b  bversion)(void);
285af 0a 20 20 69 6e 74 20 20 28 2a 6c 69 62 76 65 72  .  int  (*libver
285b0 73 69 6f 6e 5f 6e 75 6d 62 65 72 29 28 76 6f 69  sion_number)(voi
285b1 64 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 6d 61  d);.  void *(*ma
285b2 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20 63 68  lloc)(int);.  ch
285b3 61 72 20 2a 20 28 2a 6d 70 72 69 6e 74 66 29 28  ar * (*mprintf)(
285b4 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29  const char*,...)
285b5 3b 0a 20 20 69 6e 74 20 20 28 2a 6f 70 65 6e 29  ;.  int  (*open)
285b6 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c  (const char*,sql
285b7 69 74 65 33 2a 2a 29 3b 0a 20 20 69 6e 74 20 20  ite3**);.  int  
285b8 28 2a 6f 70 65 6e 31 36 29 28 63 6f 6e 73 74 20  (*open16)(const 
285b9 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2a 29  void*,sqlite3**)
285ba 3b 0a 20 20 69 6e 74 20 20 28 2a 70 72 65 70 61  ;.  int  (*prepa
285bb 72 65 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  re)(sqlite3*,con
285bc 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 73 71 6c  st char*,int,sql
285bd 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73  ite3_stmt**,cons
285be 74 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74  t char**);.  int
285bf 20 20 28 2a 70 72 65 70 61 72 65 31 36 29 28 73    (*prepare16)(s
285c0 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76 6f  qlite3*,const vo
285c1 69 64 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  id*,int,sqlite3_
285c2 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 76 6f 69  stmt**,const voi
285c3 64 2a 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28  d**);.  void * (
285c4 2a 70 72 6f 66 69 6c 65 29 28 73 71 6c 69 74 65  *profile)(sqlite
285c5 33 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  3*,void(*)(void*
285c6 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c  ,const char*,sql
285c7 69 74 65 5f 75 69 6e 74 36 34 29 2c 76 6f 69 64  ite_uint64),void
285c8 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 70 72  *);.  void  (*pr
285c9 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 29 28  ogress_handler)(
285ca 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 69 6e 74  sqlite3*,int,int
285cb 28 2a 29 28 76 6f 69 64 2a 29 2c 76 6f 69 64 2a  (*)(void*),void*
285cc 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 72 65 61  );.  void *(*rea
285cd 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c 69 6e 74 29  lloc)(void*,int)
285ce 3b 0a 20 20 69 6e 74 20 20 28 2a 72 65 73 65 74  ;.  int  (*reset
285cf 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 70  )(sqlite3_stmt*p
285d0 53 74 6d 74 29 3b 0a 20 20 76 6f 69 64 20 20 28  Stmt);.  void  (
285d1 2a 72 65 73 75 6c 74 5f 62 6c 6f 62 29 28 73 71  *result_blob)(sq
285d2 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63  lite3_context*,c
285d3 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76  onst void*,int,v
285d4 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
285d5 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
285d6 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69 74 65 33  _double)(sqlite3
285d7 5f 63 6f 6e 74 65 78 74 2a 2c 64 6f 75 62 6c 65  _context*,double
285d8 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  );.  void  (*res
285d9 75 6c 74 5f 65 72 72 6f 72 29 28 73 71 6c 69 74  ult_error)(sqlit
285da 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73  e3_context*,cons
285db 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 20 20  t char*,int);.  
285dc 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 65  void  (*result_e
285dd 72 72 6f 72 31 36 29 28 73 71 6c 69 74 65 33 5f  rror16)(sqlite3_
285de 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76  context*,const v
285df 6f 69 64 2a 2c 69 6e 74 29 3b 0a 20 20 76 6f 69  oid*,int);.  voi
285e0 64 20 20 28 2a 72 65 73 75 6c 74 5f 69 6e 74 29  d  (*result_int)
285e1 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
285e2 2a 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 20  *,int);.  void  
285e3 28 2a 72 65 73 75 6c 74 5f 69 6e 74 36 34 29 28  (*result_int64)(
285e4 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
285e5 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a  ,sqlite_int64);.
285e6 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74    void  (*result
285e7 5f 6e 75 6c 6c 29 28 73 71 6c 69 74 65 33 5f 63  _null)(sqlite3_c
285e8 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 76 6f 69 64  ontext*);.  void
285e9 20 20 28 2a 72 65 73 75 6c 74 5f 74 65 78 74 29    (*result_text)
285ea 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
285eb 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e  *,const char*,in
285ec 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  t,void(*)(void*)
285ed 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  );.  void  (*res
285ee 75 6c 74 5f 74 65 78 74 31 36 29 28 73 71 6c 69  ult_text16)(sqli
285ef 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e  te3_context*,con
285f0 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69  st void*,int,voi
285f1 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20  d(*)(void*));.  
285f2 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74  void  (*result_t
285f3 65 78 74 31 36 62 65 29 28 73 71 6c 69 74 65 33  ext16be)(sqlite3
285f4 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20  _context*,const 
285f5 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a  void*,int,void(*
285f6 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69  )(void*));.  voi
285f7 64 20 20 28 2a 72 65 73 75 6c 74 5f 74 65 78 74  d  (*result_text
285f8 31 36 6c 65 29 28 73 71 6c 69 74 65 33 5f 63 6f  16le)(sqlite3_co
285f9 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69  ntext*,const voi
285fa 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76  d*,int,void(*)(v
285fb 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20  oid*));.  void  
285fc 28 2a 72 65 73 75 6c 74 5f 76 61 6c 75 65 29 28  (*result_value)(
285fd 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
285fe 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  ,sqlite3_value*)
285ff 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 72 6f 6c  ;.  void * (*rol
28600 6c 62 61 63 6b 5f 68 6f 6f 6b 29 28 73 71 6c 69  lback_hook)(sqli
28601 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69  te3*,void(*)(voi
28602 64 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e  d*),void*);.  in
28603 74 20 20 28 2a 73 65 74 5f 61 75 74 68 6f 72 69  t  (*set_authori
28604 7a 65 72 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e  zer)(sqlite3*,in
28605 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  t(*)(void*,int,c
28606 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
28607 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
28608 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c  r*,const char*),
28609 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 20  void*);.  void  
2860a 28 2a 73 65 74 5f 61 75 78 64 61 74 61 29 28 73  (*set_auxdata)(s
2860b 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
2860c 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69 64 20 28  int,void*,void (
2860d 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 63 68  *)(void*));.  ch
2860e 61 72 20 2a 20 28 2a 73 6e 70 72 69 6e 74 66 29  ar * (*snprintf)
2860f 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f 6e 73 74  (int,char*,const
28610 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 20 20 69   char*,...);.  i
28611 6e 74 20 20 28 2a 73 74 65 70 29 28 73 71 6c 69  nt  (*step)(sqli
28612 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 69 6e  te3_stmt*);.  in
28613 74 20 20 28 2a 74 61 62 6c 65 5f 63 6f 6c 75 6d  t  (*table_colum
28614 6e 5f 6d 65 74 61 64 61 74 61 29 28 73 71 6c 69  n_metadata)(sqli
28615 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
28616 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
28617 73 74 20 63 68 61 72 2a 2c 63 68 61 72 20 63 6f  st char*,char co
28618 6e 73 74 2a 2a 2c 63 68 61 72 20 63 6f 6e 73 74  nst**,char const
28619 2a 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74  **,int*,int*,int
2861a 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 74 68  *);.  void  (*th
2861b 72 65 61 64 5f 63 6c 65 61 6e 75 70 29 28 76 6f  read_cleanup)(vo
2861c 69 64 29 3b 0a 20 20 69 6e 74 20 20 28 2a 74 6f  id);.  int  (*to
2861d 74 61 6c 5f 63 68 61 6e 67 65 73 29 28 73 71 6c  tal_changes)(sql
2861e 69 74 65 33 2a 29 3b 0a 20 20 76 6f 69 64 20 2a  ite3*);.  void *
2861f 20 28 2a 74 72 61 63 65 29 28 73 71 6c 69 74 65   (*trace)(sqlite
28620 33 2a 2c 76 6f 69 64 28 2a 78 54 72 61 63 65 29  3*,void(*xTrace)
28621 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61  (void*,const cha
28622 72 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e  r*),void*);.  in
28623 74 20 20 28 2a 74 72 61 6e 73 66 65 72 5f 62 69  t  (*transfer_bi
28624 6e 64 69 6e 67 73 29 28 73 71 6c 69 74 65 33 5f  ndings)(sqlite3_
28625 73 74 6d 74 2a 2c 73 71 6c 69 74 65 33 5f 73 74  stmt*,sqlite3_st
28626 6d 74 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28  mt*);.  void * (
28627 2a 75 70 64 61 74 65 5f 68 6f 6f 6b 29 28 73 71  *update_hook)(sq
28628 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76  lite3*,void(*)(v
28629 6f 69 64 2a 2c 69 6e 74 20 2c 63 68 61 72 20 63  oid*,int ,char c
2862a 6f 6e 73 74 2a 2c 63 68 61 72 20 63 6f 6e 73 74  onst*,char const
2862b 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 2c  *,sqlite_int64),
2862c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a  void*);.  void *
2862d 20 28 2a 75 73 65 72 5f 64 61 74 61 29 28 73 71   (*user_data)(sq
2862e 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b  lite3_context*);
2862f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20  .  const void * 
28630 28 2a 76 61 6c 75 65 5f 62 6c 6f 62 29 28 73 71  (*value_blob)(sq
28631 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
28632 20 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f 62 79   int  (*value_by
28633 74 65 73 29 28 73 71 6c 69 74 65 33 5f 76 61 6c  tes)(sqlite3_val
28634 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76  ue*);.  int  (*v
28635 61 6c 75 65 5f 62 79 74 65 73 31 36 29 28 73 71  alue_bytes16)(sq
28636 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
28637 20 64 6f 75 62 6c 65 20 20 28 2a 76 61 6c 75 65   double  (*value
28638 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69 74 65 33  _double)(sqlite3
28639 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20  _value*);.  int 
2863a 20 28 2a 76 61 6c 75 65 5f 69 6e 74 29 28 73 71   (*value_int)(sq
2863b 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
2863c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 20 28   sqlite_int64  (
2863d 2a 76 61 6c 75 65 5f 69 6e 74 36 34 29 28 73 71  *value_int64)(sq
2863e 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
2863f 20 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f 6e 75   int  (*value_nu
28640 6d 65 72 69 63 5f 74 79 70 65 29 28 73 71 6c 69  meric_type)(sqli
28641 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 63  te3_value*);.  c
28642 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
28643 61 72 20 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78  ar * (*value_tex
28644 74 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  t)(sqlite3_value
28645 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  *);.  const void
28646 20 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78 74 31   * (*value_text1
28647 36 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  6)(sqlite3_value
28648 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  *);.  const void
28649 20 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78 74 31   * (*value_text1
2864a 36 62 65 29 28 73 71 6c 69 74 65 33 5f 76 61 6c  6be)(sqlite3_val
2864b 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  ue*);.  const vo
2864c 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78  id * (*value_tex
2864d 74 31 36 6c 65 29 28 73 71 6c 69 74 65 33 5f 76  t16le)(sqlite3_v
2864e 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28  alue*);.  int  (
2864f 2a 76 61 6c 75 65 5f 74 79 70 65 29 28 73 71 6c  *value_type)(sql
28650 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20  ite3_value*);.  
28651 63 68 61 72 20 2a 28 2a 76 6d 70 72 69 6e 74 66  char *(*vmprintf
28652 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 76 61  )(const char*,va
28653 5f 6c 69 73 74 29 3b 0a 20 20 2f 2a 20 41 64 64  _list);.  /* Add
28654 65 64 20 3f 3f 3f 20 2a 2f 0a 20 20 69 6e 74 20  ed ??? */.  int 
28655 28 2a 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  (*overload_funct
28656 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a 2c 20 63  ion)(sqlite3*, c
28657 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63  onst char *zFunc
28658 4e 61 6d 65 2c 20 69 6e 74 20 6e 41 72 67 29 3b  Name, int nArg);
28659 0a 20 20 2f 2a 20 41 64 64 65 64 20 62 79 20 33  .  /* Added by 3
2865a 2e 33 2e 31 33 20 2a 2f 0a 20 20 69 6e 74 20 28  .3.13 */.  int (
2865b 2a 70 72 65 70 61 72 65 5f 76 32 29 28 73 71 6c  *prepare_v2)(sql
2865c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
2865d 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 73 74  *,int,sqlite3_st
2865e 6d 74 2a 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  mt**,const char*
2865f 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 70 72 65 70  *);.  int (*prep
28660 61 72 65 31 36 5f 76 32 29 28 73 71 6c 69 74 65  are16_v2)(sqlite
28661 33 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  3*,const void*,i
28662 6e 74 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  nt,sqlite3_stmt*
28663 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2a 29 3b  *,const void**);
28664 0a 20 20 69 6e 74 20 28 2a 63 6c 65 61 72 5f 62  .  int (*clear_b
28665 69 6e 64 69 6e 67 73 29 28 73 71 6c 69 74 65 33  indings)(sqlite3
28666 5f 73 74 6d 74 2a 29 3b 0a 20 20 2f 2a 20 41 64  _stmt*);.  /* Ad
28667 64 65 64 20 62 79 20 33 2e 34 2e 31 20 2a 2f 0a  ded by 3.4.1 */.
28668 20 20 69 6e 74 20 28 2a 63 72 65 61 74 65 5f 6d    int (*create_m
28669 6f 64 75 6c 65 5f 76 32 29 28 73 71 6c 69 74 65  odule_v2)(sqlite
2866a 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  3*,const char*,c
2866b 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
2866c 75 6c 65 2a 2c 76 6f 69 64 2a 2c 76 6f 69 64 20  ule*,void*,void 
2866d 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64  (*xDestroy)(void
2866e 20 2a 29 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64   *));.  /* Added
2866f 20 62 79 20 33 2e 35 2e 30 20 2a 2f 0a 20 20 69   by 3.5.0 */.  i
28670 6e 74 20 28 2a 62 69 6e 64 5f 7a 65 72 6f 62 6c  nt (*bind_zerobl
28671 6f 62 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ob)(sqlite3_stmt
28672 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e  *,int,int);.  in
28673 74 20 28 2a 62 6c 6f 62 5f 62 79 74 65 73 29 28  t (*blob_bytes)(
28674 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 29 3b 0a  sqlite3_blob*);.
28675 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 63 6c 6f    int (*blob_clo
28676 73 65 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  se)(sqlite3_blob
28677 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62  *);.  int (*blob
28678 5f 6f 70 65 6e 29 28 73 71 6c 69 74 65 33 2a 2c  _open)(sqlite3*,
28679 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
2867a 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
2867b 61 72 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36  ar*,sqlite3_int6
2867c 34 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 62 6c  4,int,sqlite3_bl
2867d 6f 62 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 62  ob**);.  int (*b
2867e 6c 6f 62 5f 72 65 61 64 29 28 73 71 6c 69 74 65  lob_read)(sqlite
2867f 33 5f 62 6c 6f 62 2a 2c 76 6f 69 64 2a 2c 69 6e  3_blob*,void*,in
28680 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a  t,int);.  int (*
28681 62 6c 6f 62 5f 77 72 69 74 65 29 28 73 71 6c 69  blob_write)(sqli
28682 74 65 33 5f 62 6c 6f 62 2a 2c 63 6f 6e 73 74 20  te3_blob*,const 
28683 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a  void*,int,int);.
28684 20 20 69 6e 74 20 28 2a 63 72 65 61 74 65 5f 63    int (*create_c
28685 6f 6c 6c 61 74 69 6f 6e 5f 76 32 29 28 73 71 6c  ollation_v2)(sql
28686 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
28687 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 69 6e 74 28  *,int,void*,int(
28688 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  *)(void*,int,con
28689 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  st void*,int,con
2868a 73 74 20 76 6f 69 64 2a 29 2c 76 6f 69 64 28 2a  st void*),void(*
2868b 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74  )(void*));.  int
2868c 20 28 2a 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 29   (*file_control)
2868d 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
2868e 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29  char*,int,void*)
2868f 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ;.  sqlite3_int6
28690 34 20 28 2a 6d 65 6d 6f 72 79 5f 68 69 67 68 77  4 (*memory_highw
28691 61 74 65 72 29 28 69 6e 74 29 3b 0a 20 20 73 71  ater)(int);.  sq
28692 6c 69 74 65 33 5f 69 6e 74 36 34 20 28 2a 6d 65  lite3_int64 (*me
28693 6d 6f 72 79 5f 75 73 65 64 29 28 76 6f 69 64 29  mory_used)(void)
28694 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
28695 78 20 2a 28 2a 6d 75 74 65 78 5f 61 6c 6c 6f 63  x *(*mutex_alloc
28696 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28  )(int);.  void (
28697 2a 6d 75 74 65 78 5f 65 6e 74 65 72 29 28 73 71  *mutex_enter)(sq
28698 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 20  lite3_mutex*);. 
28699 20 76 6f 69 64 20 28 2a 6d 75 74 65 78 5f 66 72   void (*mutex_fr
2869a 65 65 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  ee)(sqlite3_mute
2869b 78 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 6d 75  x*);.  void (*mu
2869c 74 65 78 5f 6c 65 61 76 65 29 28 73 71 6c 69 74  tex_leave)(sqlit
2869d 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 20 20 69 6e  e3_mutex*);.  in
2869e 74 20 28 2a 6d 75 74 65 78 5f 74 72 79 29 28 73  t (*mutex_try)(s
2869f 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a  qlite3_mutex*);.
286a0 20 20 69 6e 74 20 28 2a 6f 70 65 6e 5f 76 32 29    int (*open_v2)
286a1 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c  (const char*,sql
286a2 69 74 65 33 2a 2a 2c 69 6e 74 2c 63 6f 6e 73 74  ite3**,int,const
286a3 20 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74 20 28   char*);.  int (
286a4 2a 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 29  *release_memory)
286a5 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a  (int);.  void (*
286a6 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
286a7 65 6d 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  em)(sqlite3_cont
286a8 65 78 74 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a  ext*);.  void (*
286a9 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f  result_error_too
286aa 62 69 67 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  big)(sqlite3_con
286ab 74 65 78 74 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  text*);.  int (*
286ac 73 6c 65 65 70 29 28 69 6e 74 29 3b 0a 20 20 76  sleep)(int);.  v
286ad 6f 69 64 20 28 2a 73 6f 66 74 5f 68 65 61 70 5f  oid (*soft_heap_
286ae 6c 69 6d 69 74 29 28 69 6e 74 29 3b 0a 20 20 73  limit)(int);.  s
286af 71 6c 69 74 65 33 5f 76 66 73 20 2a 28 2a 76 66  qlite3_vfs *(*vf
286b0 73 5f 66 69 6e 64 29 28 63 6f 6e 73 74 20 63 68  s_find)(const ch
286b1 61 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 76 66  ar*);.  int (*vf
286b2 73 5f 72 65 67 69 73 74 65 72 29 28 73 71 6c 69  s_register)(sqli
286b3 74 65 33 5f 76 66 73 2a 2c 69 6e 74 29 3b 0a 20  te3_vfs*,int);. 
286b4 20 69 6e 74 20 28 2a 76 66 73 5f 75 6e 72 65 67   int (*vfs_unreg
286b5 69 73 74 65 72 29 28 73 71 6c 69 74 65 33 5f 76  ister)(sqlite3_v
286b6 66 73 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 74  fs*);.  int (*xt
286b7 68 72 65 61 64 73 61 66 65 29 28 76 6f 69 64 29  hreadsafe)(void)
286b8 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65 73 75 6c  ;.  void (*resul
286b9 74 5f 7a 65 72 6f 62 6c 6f 62 29 28 73 71 6c 69  t_zeroblob)(sqli
286ba 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
286bb 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65 73 75  );.  void (*resu
286bc 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 29 28 73  lt_error_code)(s
286bd 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
286be 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 74 65  int);.  int (*te
286bf 73 74 5f 63 6f 6e 74 72 6f 6c 29 28 69 6e 74 2c  st_control)(int,
286c0 20 2e 2e 2e 29 3b 0a 20 20 76 6f 69 64 20 28 2a   ...);.  void (*
286c1 72 61 6e 64 6f 6d 6e 65 73 73 29 28 69 6e 74 2c  randomness)(int,
286c2 76 6f 69 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65  void*);.  sqlite
286c3 33 20 2a 28 2a 63 6f 6e 74 65 78 74 5f 64 62 5f  3 *(*context_db_
286c4 68 61 6e 64 6c 65 29 28 73 71 6c 69 74 65 33 5f  handle)(sqlite3_
286c5 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 69 6e 74  context*);.  int
286c6 20 28 2a 65 78 74 65 6e 64 65 64 5f 72 65 73 75   (*extended_resu
286c7 6c 74 5f 63 6f 64 65 73 29 28 73 71 6c 69 74 65  lt_codes)(sqlite
286c8 33 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28  3*,int);.  int (
286c9 2a 6c 69 6d 69 74 29 28 73 71 6c 69 74 65 33 2a  *limit)(sqlite3*
286ca 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 73 71 6c  ,int,int);.  sql
286cb 69 74 65 33 5f 73 74 6d 74 20 2a 28 2a 6e 65 78  ite3_stmt *(*nex
286cc 74 5f 73 74 6d 74 29 28 73 71 6c 69 74 65 33 2a  t_stmt)(sqlite3*
286cd 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b  ,sqlite3_stmt*);
286ce 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 28  .  const char *(
286cf 2a 73 71 6c 29 28 73 71 6c 69 74 65 33 5f 73 74  *sql)(sqlite3_st
286d0 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 73 74  mt*);.  int (*st
286d1 61 74 75 73 29 28 69 6e 74 2c 69 6e 74 2a 2c 69  atus)(int,int*,i
286d2 6e 74 2a 2c 69 6e 74 29 3b 0a 7d 3b 0a 0a 2f 2a  nt*,int);.};../*
286d3 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
286d4 67 20 6d 61 63 72 6f 73 20 72 65 64 65 66 69 6e  g macros redefin
286d5 65 20 74 68 65 20 41 50 49 20 72 6f 75 74 69 6e  e the API routin
286d6 65 73 20 73 6f 20 74 68 61 74 20 74 68 65 79 20  es so that they 
286d7 61 72 65 0a 2a 2a 20 72 65 64 69 72 65 63 74 65  are.** redirecte
286d8 64 20 74 68 72 6f 75 67 68 74 20 74 68 65 20 67  d throught the g
286d9 6c 6f 62 61 6c 20 73 71 6c 69 74 65 33 5f 61 70  lobal sqlite3_ap
286da 69 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  i structure..**.
286db 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
286dc 69 6c 65 20 69 73 20 61 6c 73 6f 20 75 73 65 64  ile is also used
286dd 20 62 79 20 74 68 65 20 6c 6f 61 64 65 78 74 2e   by the loadext.
286de 63 20 73 6f 75 72 63 65 20 66 69 6c 65 0a 2a 2a  c source file.**
286df 20 28 70 61 72 74 20 6f 66 20 74 68 65 20 6d 61   (part of the ma
286e0 69 6e 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72  in SQLite librar
286e1 79 20 2d 20 6e 6f 74 20 61 6e 20 65 78 74 65 6e  y - not an exten
286e2 73 69 6f 6e 29 20 73 6f 20 74 68 61 74 0a 2a 2a  sion) so that.**
286e3 20 69 74 20 63 61 6e 20 67 65 74 20 61 63 63 65   it can get acce
286e4 73 73 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65  ss to the sqlite
286e5 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 73  3_api_routines s
286e6 74 72 75 63 74 75 72 65 0a 2a 2a 20 64 65 66 69  tructure.** defi
286e7 6e 69 74 69 6f 6e 2e 20 20 42 75 74 20 74 68 65  nition.  But the
286e8 20 6d 61 69 6e 20 6c 69 62 72 61 72 79 20 64 6f   main library do
286e9 65 73 20 6e 6f 74 20 77 61 6e 74 20 74 6f 20 72  es not want to r
286ea 65 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 41  edefine.** the A
286eb 50 49 2e 20 20 53 6f 20 74 68 65 20 72 65 64 65  PI.  So the rede
286ec 66 69 6e 69 74 69 6f 6e 20 6d 61 63 72 6f 73 20  finition macros 
286ed 61 72 65 20 6f 6e 6c 79 20 76 61 6c 69 64 20 69  are only valid i
286ee 66 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f  f the.** SQLITE_
286ef 43 4f 52 45 20 6d 61 63 72 6f 73 20 69 73 20 75  CORE macros is u
286f0 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66  ndefined..*/.#if
286f1 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45  ndef SQLITE_CORE
286f2 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
286f3 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
286f4 78 74 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  xt      sqlite3_
286f5 61 70 69 2d 3e 61 67 67 72 65 67 61 74 65 5f 63  api->aggregate_c
286f6 6f 6e 74 65 78 74 0a 23 69 66 6e 64 65 66 20 53  ontext.#ifndef S
286f7 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45  QLITE_OMIT_DEPRE
286f8 43 41 54 45 44 0a 23 64 65 66 69 6e 65 20 73 71  CATED.#define sq
286f9 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
286fa 63 6f 75 6e 74 20 20 20 20 20 20 20 20 73 71 6c  count        sql
286fb 69 74 65 33 5f 61 70 69 2d 3e 61 67 67 72 65 67  ite3_api->aggreg
286fc 61 74 65 5f 63 6f 75 6e 74 0a 23 65 6e 64 69 66  ate_count.#endif
286fd 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
286fe 5f 62 69 6e 64 5f 62 6c 6f 62 20 20 20 20 20 20  _bind_blob      
286ff 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28700 61 70 69 2d 3e 62 69 6e 64 5f 62 6c 6f 62 0a 23  api->bind_blob.#
28701 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
28702 69 6e 64 5f 64 6f 75 62 6c 65 20 20 20 20 20 20  ind_double      
28703 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28704 69 2d 3e 62 69 6e 64 5f 64 6f 75 62 6c 65 0a 23  i->bind_double.#
28705 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
28706 69 6e 64 5f 69 6e 74 20 20 20 20 20 20 20 20 20  ind_int         
28707 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28708 69 2d 3e 62 69 6e 64 5f 69 6e 74 0a 23 64 65 66  i->bind_int.#def
28709 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
2870a 5f 69 6e 74 36 34 20 20 20 20 20 20 20 20 20 20  _int64          
2870b 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
2870c 62 69 6e 64 5f 69 6e 74 36 34 0a 23 64 65 66 69  bind_int64.#defi
2870d 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
2870e 6e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20  null            
2870f 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
28710 69 6e 64 5f 6e 75 6c 6c 0a 23 64 65 66 69 6e 65  ind_null.#define
28711 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61   sqlite3_bind_pa
28712 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 20 20 20  rameter_count   
28713 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e  sqlite3_api->bin
28714 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e  d_parameter_coun
28715 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  t.#define sqlite
28716 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  3_bind_parameter
28717 5f 69 6e 64 65 78 20 20 20 73 71 6c 69 74 65 33  _index   sqlite3
28718 5f 61 70 69 2d 3e 62 69 6e 64 5f 70 61 72 61 6d  _api->bind_param
28719 65 74 65 72 5f 69 6e 64 65 78 0a 23 64 65 66 69  eter_index.#defi
2871a 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
2871b 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 20 20  parameter_name  
2871c 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
2871d 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61  ind_parameter_na
2871e 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  me.#define sqlit
2871f 65 33 5f 62 69 6e 64 5f 74 65 78 74 20 20 20 20  e3_bind_text    
28720 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28721 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 74 65 78 74  3_api->bind_text
28722 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28723 5f 62 69 6e 64 5f 74 65 78 74 31 36 20 20 20 20  _bind_text16    
28724 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28725 61 70 69 2d 3e 62 69 6e 64 5f 74 65 78 74 31 36  api->bind_text16
28726 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28727 5f 62 69 6e 64 5f 76 61 6c 75 65 20 20 20 20 20  _bind_value     
28728 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28729 61 70 69 2d 3e 62 69 6e 64 5f 76 61 6c 75 65 0a  api->bind_value.
2872a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2872b 62 75 73 79 5f 68 61 6e 64 6c 65 72 20 20 20 20  busy_handler    
2872c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
2872d 70 69 2d 3e 62 75 73 79 5f 68 61 6e 64 6c 65 72  pi->busy_handler
2872e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
2872f 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 20 20 20  _busy_timeout   
28730 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28731 61 70 69 2d 3e 62 75 73 79 5f 74 69 6d 65 6f 75  api->busy_timeou
28732 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  t.#define sqlite
28733 33 5f 63 68 61 6e 67 65 73 20 20 20 20 20 20 20  3_changes       
28734 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28735 5f 61 70 69 2d 3e 63 68 61 6e 67 65 73 0a 23 64  _api->changes.#d
28736 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6c  efine sqlite3_cl
28737 6f 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ose             
28738 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28739 2d 3e 63 6c 6f 73 65 0a 23 64 65 66 69 6e 65 20  ->close.#define 
2873a 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f  sqlite3_collatio
2873b 6e 5f 6e 65 65 64 65 64 20 20 20 20 20 20 20 73  n_needed       s
2873c 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 6c  qlite3_api->coll
2873d 61 74 69 6f 6e 5f 6e 65 65 64 65 64 0a 23 64 65  ation_needed.#de
2873e 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
2873f 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 20  lation_needed16 
28740 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28741 3e 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  >collation_neede
28742 64 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  d16.#define sqli
28743 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 20  te3_column_blob 
28744 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28745 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 62  e3_api->column_b
28746 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  lob.#define sqli
28747 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
28748 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28749 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 62  e3_api->column_b
2874a 79 74 65 73 0a 23 64 65 66 69 6e 65 20 73 71 6c  ytes.#define sql
2874b 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
2874c 73 31 36 20 20 20 20 20 20 20 20 20 73 71 6c 69  s16         sqli
2874d 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
2874e 62 79 74 65 73 31 36 0a 23 64 65 66 69 6e 65 20  bytes16.#define 
2874f 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63  sqlite3_column_c
28750 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 73  ount           s
28751 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
28752 6d 6e 5f 63 6f 75 6e 74 0a 23 64 65 66 69 6e 65  mn_count.#define
28753 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
28754 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 20 20 20  database_name   
28755 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c  sqlite3_api->col
28756 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
28757 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
28758 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73  3_column_databas
28759 65 5f 6e 61 6d 65 31 36 20 73 71 6c 69 74 65 33  e_name16 sqlite3
2875a 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 61 74  _api->column_dat
2875b 61 62 61 73 65 5f 6e 61 6d 65 31 36 0a 23 64 65  abase_name16.#de
2875c 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
2875d 75 6d 6e 5f 64 65 63 6c 74 79 70 65 20 20 20 20  umn_decltype    
2875e 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
2875f 3e 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65  >column_decltype
28760 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28761 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65  _column_decltype
28762 31 36 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  16      sqlite3_
28763 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 65 63 6c  api->column_decl
28764 74 79 70 65 31 36 0a 23 64 65 66 69 6e 65 20 73  type16.#define s
28765 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f  qlite3_column_do
28766 75 62 6c 65 20 20 20 20 20 20 20 20 20 20 73 71  uble          sq
28767 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
28768 6e 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69 6e 65  n_double.#define
28769 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
2876a 69 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20  int             
2876b 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c  sqlite3_api->col
2876c 75 6d 6e 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20  umn_int.#define 
2876d 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
2876e 6e 74 36 34 20 20 20 20 20 20 20 20 20 20 20 73  nt64           s
2876f 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
28770 6d 6e 5f 69 6e 74 36 34 0a 23 64 65 66 69 6e 65  mn_int64.#define
28771 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
28772 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20  name            
28773 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c  sqlite3_api->col
28774 75 6d 6e 5f 6e 61 6d 65 0a 23 64 65 66 69 6e 65  umn_name.#define
28775 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
28776 6e 61 6d 65 31 36 20 20 20 20 20 20 20 20 20 20  name16          
28777 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c  sqlite3_api->col
28778 75 6d 6e 5f 6e 61 6d 65 31 36 0a 23 64 65 66 69  umn_name16.#defi
28779 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
2877a 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 20 20 20  n_origin_name   
2877b 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
2877c 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d  olumn_origin_nam
2877d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
2877e 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f  3_column_origin_
2877f 6e 61 6d 65 31 36 20 20 20 73 71 6c 69 74 65 33  name16   sqlite3
28780 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6f 72 69  _api->column_ori
28781 67 69 6e 5f 6e 61 6d 65 31 36 0a 23 64 65 66 69  gin_name16.#defi
28782 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
28783 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 20 20 20  n_table_name    
28784 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
28785 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
28786 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28787 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  _column_table_na
28788 6d 65 31 36 20 20 20 20 73 71 6c 69 74 65 33 5f  me16    sqlite3_
28789 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74 61 62 6c  api->column_tabl
2878a 65 5f 6e 61 6d 65 31 36 0a 23 64 65 66 69 6e 65  e_name16.#define
2878b 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
2878c 74 65 78 74 20 20 20 20 20 20 20 20 20 20 20 20  text            
2878d 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c  sqlite3_api->col
2878e 75 6d 6e 5f 74 65 78 74 0a 23 64 65 66 69 6e 65  umn_text.#define
2878f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
28790 74 65 78 74 31 36 20 20 20 20 20 20 20 20 20 20  text16          
28791 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c  sqlite3_api->col
28792 75 6d 6e 5f 74 65 78 74 31 36 0a 23 64 65 66 69  umn_text16.#defi
28793 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
28794 6e 5f 74 79 70 65 20 20 20 20 20 20 20 20 20 20  n_type          
28795 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
28796 6f 6c 75 6d 6e 5f 74 79 70 65 0a 23 64 65 66 69  olumn_type.#defi
28797 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
28798 6e 5f 76 61 6c 75 65 20 20 20 20 20 20 20 20 20  n_value         
28799 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
2879a 6f 6c 75 6d 6e 5f 76 61 6c 75 65 0a 23 64 65 66  olumn_value.#def
2879b 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d  ine sqlite3_comm
2879c 69 74 5f 68 6f 6f 6b 20 20 20 20 20 20 20 20 20  it_hook         
2879d 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
2879e 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 0a 23 64 65 66  commit_hook.#def
2879f 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  ine sqlite3_comp
287a0 6c 65 74 65 20 20 20 20 20 20 20 20 20 20 20 20  lete            
287a1 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
287a2 63 6f 6d 70 6c 65 74 65 0a 23 64 65 66 69 6e 65  complete.#define
287a3 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
287a4 65 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20  e16             
287a5 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d  sqlite3_api->com
287a6 70 6c 65 74 65 31 36 0a 23 64 65 66 69 6e 65 20  plete16.#define 
287a7 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
287a8 6f 6c 6c 61 74 69 6f 6e 20 20 20 20 20 20 20 73  ollation       s
287a9 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61  qlite3_api->crea
287aa 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 0a 23 64 65  te_collation.#de
287ab 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65  fine sqlite3_cre
287ac 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 20  ate_collation16 
287ad 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
287ae 3e 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  >create_collatio
287af 6e 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  n16.#define sqli
287b0 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
287b1 69 6f 6e 20 20 20 20 20 20 20 20 73 71 6c 69 74  ion        sqlit
287b2 65 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 66  e3_api->create_f
287b3 75 6e 63 74 69 6f 6e 0a 23 64 65 66 69 6e 65 20  unction.#define 
287b4 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
287b5 75 6e 63 74 69 6f 6e 31 36 20 20 20 20 20 20 73  unction16      s
287b6 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61  qlite3_api->crea
287b7 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 0a 23 64  te_function16.#d
287b8 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72  efine sqlite3_cr
287b9 65 61 74 65 5f 6d 6f 64 75 6c 65 20 20 20 20 20  eate_module     
287ba 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
287bb 2d 3e 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 0a  ->create_module.
287bc 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
287bd 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32  create_module_v2
287be 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
287bf 70 69 2d 3e 63 72 65 61 74 65 5f 6d 6f 64 75 6c  pi->create_modul
287c0 65 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c  e_v2.#define sql
287c1 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 20  ite3_data_count 
287c2 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
287c3 74 65 33 5f 61 70 69 2d 3e 64 61 74 61 5f 63 6f  te3_api->data_co
287c4 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  unt.#define sqli
287c5 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 20 20 20  te3_db_handle   
287c6 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
287c7 65 33 5f 61 70 69 2d 3e 64 62 5f 68 61 6e 64 6c  e3_api->db_handl
287c8 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
287c9 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 20 20  3_declare_vtab  
287ca 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
287cb 5f 61 70 69 2d 3e 64 65 63 6c 61 72 65 5f 76 74  _api->declare_vt
287cc 61 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ab.#define sqlit
287cd 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64  e3_enable_shared
287ce 5f 63 61 63 68 65 20 20 20 20 73 71 6c 69 74 65  _cache    sqlite
287cf 33 5f 61 70 69 2d 3e 65 6e 61 62 6c 65 5f 73 68  3_api->enable_sh
287d0 61 72 65 64 5f 63 61 63 68 65 0a 23 64 65 66 69  ared_cache.#defi
287d1 6e 65 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  ne sqlite3_errco
287d2 64 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  de              
287d3 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65    sqlite3_api->e
287d4 72 72 63 6f 64 65 0a 23 64 65 66 69 6e 65 20 73  rrcode.#define s
287d5 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 20 20 20  qlite3_errmsg   
287d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
287d7 6c 69 74 65 33 5f 61 70 69 2d 3e 65 72 72 6d 73  lite3_api->errms
287d8 67 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  g.#define sqlite
287d9 33 5f 65 72 72 6d 73 67 31 36 20 20 20 20 20 20  3_errmsg16      
287da 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
287db 5f 61 70 69 2d 3e 65 72 72 6d 73 67 31 36 0a 23  _api->errmsg16.#
287dc 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65  define sqlite3_e
287dd 78 65 63 20 20 20 20 20 20 20 20 20 20 20 20 20  xec             
287de 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
287df 69 2d 3e 65 78 65 63 0a 23 69 66 6e 64 65 66 20  i->exec.#ifndef 
287e0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52  SQLITE_OMIT_DEPR
287e1 45 43 41 54 45 44 0a 23 64 65 66 69 6e 65 20 73  ECATED.#define s
287e2 71 6c 69 74 65 33 5f 65 78 70 69 72 65 64 20 20  qlite3_expired  
287e3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
287e4 6c 69 74 65 33 5f 61 70 69 2d 3e 65 78 70 69 72  lite3_api->expir
287e5 65 64 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e  ed.#endif.#defin
287e6 65 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  e sqlite3_finali
287e7 7a 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ze              
287e8 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 66 69   sqlite3_api->fi
287e9 6e 61 6c 69 7a 65 0a 23 64 65 66 69 6e 65 20 73  nalize.#define s
287ea 71 6c 69 74 65 33 5f 66 72 65 65 20 20 20 20 20  qlite3_free     
287eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
287ec 6c 69 74 65 33 5f 61 70 69 2d 3e 66 72 65 65 0a  lite3_api->free.
287ed 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
287ee 66 72 65 65 5f 74 61 62 6c 65 20 20 20 20 20 20  free_table      
287ef 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
287f0 70 69 2d 3e 66 72 65 65 5f 74 61 62 6c 65 0a 23  pi->free_table.#
287f1 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67  define sqlite3_g
287f2 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 20 20 20  et_autocommit   
287f3 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
287f4 69 2d 3e 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69  i->get_autocommi
287f5 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  t.#define sqlite
287f6 33 5f 67 65 74 5f 61 75 78 64 61 74 61 20 20 20  3_get_auxdata   
287f7 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
287f8 5f 61 70 69 2d 3e 67 65 74 5f 61 75 78 64 61 74  _api->get_auxdat
287f9 61 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  a.#define sqlite
287fa 33 5f 67 65 74 5f 74 61 62 6c 65 20 20 20 20 20  3_get_table     
287fb 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
287fc 5f 61 70 69 2d 3e 67 65 74 5f 74 61 62 6c 65 0a  _api->get_table.
287fd 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
287fe 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 23  MIT_DEPRECATED.#
287ff 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67  define sqlite3_g
28800 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 20 20 20  lobal_recover   
28801 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28802 69 2d 3e 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65  i->global_recove
28803 72 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65  r.#endif.#define
28804 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
28805 70 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  pt              
28806 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 69 6e 74  sqlite3_api->int
28807 65 72 72 75 70 74 78 0a 23 64 65 66 69 6e 65 20  erruptx.#define 
28808 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73  sqlite3_last_ins
28809 65 72 74 5f 72 6f 77 69 64 20 20 20 20 20 20 73  ert_rowid      s
2880a 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 61 73 74  qlite3_api->last
2880b 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 0a 23 64  _insert_rowid.#d
2880c 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c 69  efine sqlite3_li
2880d 62 76 65 72 73 69 6f 6e 20 20 20 20 20 20 20 20  bversion        
2880e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2880f 2d 3e 6c 69 62 76 65 72 73 69 6f 6e 0a 23 64 65  ->libversion.#de
28810 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c 69 62  fine sqlite3_lib
28811 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 20 20  version_number  
28812 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28813 3e 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62  >libversion_numb
28814 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  er.#define sqlit
28815 65 33 5f 6d 61 6c 6c 6f 63 20 20 20 20 20 20 20  e3_malloc       
28816 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28817 33 5f 61 70 69 2d 3e 6d 61 6c 6c 6f 63 0a 23 64  3_api->malloc.#d
28818 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 70  efine sqlite3_mp
28819 72 69 6e 74 66 20 20 20 20 20 20 20 20 20 20 20  rintf           
2881a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2881b 2d 3e 6d 70 72 69 6e 74 66 0a 23 64 65 66 69 6e  ->mprintf.#defin
2881c 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 20 20  e sqlite3_open  
2881d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2881e 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 70   sqlite3_api->op
2881f 65 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  en.#define sqlit
28820 65 33 5f 6f 70 65 6e 31 36 20 20 20 20 20 20 20  e3_open16       
28821 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28822 33 5f 61 70 69 2d 3e 6f 70 65 6e 31 36 0a 23 64  3_api->open16.#d
28823 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72  efine sqlite3_pr
28824 65 70 61 72 65 20 20 20 20 20 20 20 20 20 20 20  epare           
28825 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28826 2d 3e 70 72 65 70 61 72 65 0a 23 64 65 66 69 6e  ->prepare.#defin
28827 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  e sqlite3_prepar
28828 65 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20  e16             
28829 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72   sqlite3_api->pr
2882a 65 70 61 72 65 31 36 0a 23 64 65 66 69 6e 65 20  epare16.#define 
2882b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
2882c 76 32 20 20 20 20 20 20 20 20 20 20 20 20 20 73  v2             s
2882d 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70  qlite3_api->prep
2882e 61 72 65 5f 76 32 0a 23 64 65 66 69 6e 65 20 73  are_v2.#define s
2882f 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
28830 5f 76 32 20 20 20 20 20 20 20 20 20 20 20 73 71  _v2           sq
28831 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61  lite3_api->prepa
28832 72 65 31 36 5f 76 32 0a 23 64 65 66 69 6e 65 20  re16_v2.#define 
28833 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 20  sqlite3_profile 
28834 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
28835 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 6f 66  qlite3_api->prof
28836 69 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ile.#define sqli
28837 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e  te3_progress_han
28838 64 6c 65 72 20 20 20 20 20 20 20 73 71 6c 69 74  dler       sqlit
28839 65 33 5f 61 70 69 2d 3e 70 72 6f 67 72 65 73 73  e3_api->progress
2883a 5f 68 61 6e 64 6c 65 72 0a 23 64 65 66 69 6e 65  _handler.#define
2883b 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
2883c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2883d 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 61  sqlite3_api->rea
2883e 6c 6c 6f 63 0a 23 64 65 66 69 6e 65 20 73 71 6c  lloc.#define sql
2883f 69 74 65 33 5f 72 65 73 65 74 20 20 20 20 20 20  ite3_reset      
28840 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28841 74 65 33 5f 61 70 69 2d 3e 72 65 73 65 74 0a 23  te3_api->reset.#
28842 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
28843 65 73 75 6c 74 5f 62 6c 6f 62 20 20 20 20 20 20  esult_blob      
28844 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28845 69 2d 3e 72 65 73 75 6c 74 5f 62 6c 6f 62 0a 23  i->result_blob.#
28846 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
28847 65 73 75 6c 74 5f 64 6f 75 62 6c 65 20 20 20 20  esult_double    
28848 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28849 69 2d 3e 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65  i->result_double
2884a 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
2884b 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 20 20 20  _result_error   
2884c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2884d 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f  api->result_erro
2884e 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  r.#define sqlite
2884f 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36  3_result_error16
28850 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28851 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72  _api->result_err
28852 6f 72 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c  or16.#define sql
28853 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
28854 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28855 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f  te3_api->result_
28856 69 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  int.#define sqli
28857 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34  te3_result_int64
28858 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28859 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 69  e3_api->result_i
2885a 6e 74 36 34 0a 23 64 65 66 69 6e 65 20 73 71 6c  nt64.#define sql
2885b 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
2885c 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2885d 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f  te3_api->result_
2885e 6e 75 6c 6c 0a 23 64 65 66 69 6e 65 20 73 71 6c  null.#define sql
2885f 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
28860 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28861 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f  te3_api->result_
28862 74 65 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c  text.#define sql
28863 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
28864 31 36 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  16          sqli
28865 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f  te3_api->result_
28866 74 65 78 74 31 36 0a 23 64 65 66 69 6e 65 20 73  text16.#define s
28867 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
28868 78 74 31 36 62 65 20 20 20 20 20 20 20 20 73 71  xt16be        sq
28869 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
2886a 74 5f 74 65 78 74 31 36 62 65 0a 23 64 65 66 69  t_text16be.#defi
2886b 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
2886c 74 5f 74 65 78 74 31 36 6c 65 20 20 20 20 20 20  t_text16le      
2886d 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
2886e 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 0a 23  esult_text16le.#
2886f 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
28870 65 73 75 6c 74 5f 76 61 6c 75 65 20 20 20 20 20  esult_value     
28871 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28872 69 2d 3e 72 65 73 75 6c 74 5f 76 61 6c 75 65 0a  i->result_value.
28873 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28874 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 20 20 20  rollback_hook   
28875 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28876 70 69 2d 3e 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f  pi->rollback_hoo
28877 6b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  k.#define sqlite
28878 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
28879 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2887a 5f 61 70 69 2d 3e 73 65 74 5f 61 75 74 68 6f 72  _api->set_author
2887b 69 7a 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c  izer.#define sql
2887c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
2887d 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2887e 74 65 33 5f 61 70 69 2d 3e 73 65 74 5f 61 75 78  te3_api->set_aux
2887f 64 61 74 61 0a 23 64 65 66 69 6e 65 20 73 71 6c  data.#define sql
28880 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 20 20 20  ite3_snprintf   
28881 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28882 74 65 33 5f 61 70 69 2d 3e 73 6e 70 72 69 6e 74  te3_api->snprint
28883 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  f.#define sqlite
28884 33 5f 73 74 65 70 20 20 20 20 20 20 20 20 20 20  3_step          
28885 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28886 5f 61 70 69 2d 3e 73 74 65 70 0a 23 64 65 66 69  _api->step.#defi
28887 6e 65 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65  ne sqlite3_table
28888 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61  _column_metadata
28889 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74    sqlite3_api->t
2888a 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61  able_column_meta
2888b 64 61 74 61 0a 23 64 65 66 69 6e 65 20 73 71 6c  data.#define sql
2888c 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61  ite3_thread_clea
2888d 6e 75 70 20 20 20 20 20 20 20 20 20 73 71 6c 69  nup         sqli
2888e 74 65 33 5f 61 70 69 2d 3e 74 68 72 65 61 64 5f  te3_api->thread_
2888f 63 6c 65 61 6e 75 70 0a 23 64 65 66 69 6e 65 20  cleanup.#define 
28890 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68  sqlite3_total_ch
28891 61 6e 67 65 73 20 20 20 20 20 20 20 20 20 20 73  anges          s
28892 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 6f 74 61  qlite3_api->tota
28893 6c 5f 63 68 61 6e 67 65 73 0a 23 64 65 66 69 6e  l_changes.#defin
28894 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 20  e sqlite3_trace 
28895 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28896 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 72   sqlite3_api->tr
28897 61 63 65 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ace.#ifndef SQLI
28898 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54  TE_OMIT_DEPRECAT
28899 45 44 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ED.#define sqlit
2889a 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64  e3_transfer_bind
2889b 69 6e 67 73 20 20 20 20 20 20 73 71 6c 69 74 65  ings      sqlite
2889c 33 5f 61 70 69 2d 3e 74 72 61 6e 73 66 65 72 5f  3_api->transfer_
2889d 62 69 6e 64 69 6e 67 73 0a 23 65 6e 64 69 66 0a  bindings.#endif.
2889e 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2889f 75 70 64 61 74 65 5f 68 6f 6f 6b 20 20 20 20 20  update_hook     
288a0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
288a1 70 69 2d 3e 75 70 64 61 74 65 5f 68 6f 6f 6b 0a  pi->update_hook.
288a2 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
288a3 75 73 65 72 5f 64 61 74 61 20 20 20 20 20 20 20  user_data       
288a4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
288a5 70 69 2d 3e 75 73 65 72 5f 64 61 74 61 0a 23 64  pi->user_data.#d
288a6 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61  efine sqlite3_va
288a7 6c 75 65 5f 62 6c 6f 62 20 20 20 20 20 20 20 20  lue_blob        
288a8 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
288a9 2d 3e 76 61 6c 75 65 5f 62 6c 6f 62 0a 23 64 65  ->value_blob.#de
288aa 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
288ab 75 65 5f 62 79 74 65 73 20 20 20 20 20 20 20 20  ue_bytes        
288ac 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
288ad 3e 76 61 6c 75 65 5f 62 79 74 65 73 0a 23 64 65  >value_bytes.#de
288ae 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
288af 75 65 5f 62 79 74 65 73 31 36 20 20 20 20 20 20  ue_bytes16      
288b0 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
288b1 3e 76 61 6c 75 65 5f 62 79 74 65 73 31 36 0a 23  >value_bytes16.#
288b2 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
288b3 61 6c 75 65 5f 64 6f 75 62 6c 65 20 20 20 20 20  alue_double     
288b4 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
288b5 69 2d 3e 76 61 6c 75 65 5f 64 6f 75 62 6c 65 0a  i->value_double.
288b6 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
288b7 76 61 6c 75 65 5f 69 6e 74 20 20 20 20 20 20 20  value_int       
288b8 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
288b9 70 69 2d 3e 76 61 6c 75 65 5f 69 6e 74 0a 23 64  pi->value_int.#d
288ba 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61  efine sqlite3_va
288bb 6c 75 65 5f 69 6e 74 36 34 20 20 20 20 20 20 20  lue_int64       
288bc 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
288bd 2d 3e 76 61 6c 75 65 5f 69 6e 74 36 34 0a 23 64  ->value_int64.#d
288be 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61  efine sqlite3_va
288bf 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65  lue_numeric_type
288c0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
288c1 2d 3e 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f  ->value_numeric_
288c2 74 79 70 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  type.#define sql
288c3 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 20  ite3_value_text 
288c4 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
288c5 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74  te3_api->value_t
288c6 65 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ext.#define sqli
288c7 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
288c8 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
288c9 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74 65  e3_api->value_te
288ca 78 74 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c  xt16.#define sql
288cb 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
288cc 36 62 65 20 20 20 20 20 20 20 20 20 73 71 6c 69  6be         sqli
288cd 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74  te3_api->value_t
288ce 65 78 74 31 36 62 65 0a 23 64 65 66 69 6e 65 20  ext16be.#define 
288cf 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
288d0 78 74 31 36 6c 65 20 20 20 20 20 20 20 20 20 73  xt16le         s
288d1 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
288d2 65 5f 74 65 78 74 31 36 6c 65 0a 23 64 65 66 69  e_text16le.#defi
288d3 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ne sqlite3_value
288d4 5f 74 79 70 65 20 20 20 20 20 20 20 20 20 20 20  _type           
288d5 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
288d6 61 6c 75 65 5f 74 79 70 65 0a 23 64 65 66 69 6e  alue_type.#defin
288d7 65 20 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e  e sqlite3_vmprin
288d8 74 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20  tf              
288d9 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 6d   sqlite3_api->vm
288da 70 72 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 73  printf.#define s
288db 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f  qlite3_overload_
288dc 66 75 6e 63 74 69 6f 6e 20 20 20 20 20 20 73 71  function      sq
288dd 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 76 65 72 6c  lite3_api->overl
288de 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 0a 23 64 65  oad_function.#de
288df 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65  fine sqlite3_pre
288e0 70 61 72 65 5f 76 32 20 20 20 20 20 20 20 20 20  pare_v2         
288e1 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
288e2 3e 70 72 65 70 61 72 65 5f 76 32 0a 23 64 65 66  >prepare_v2.#def
288e3 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ine sqlite3_prep
288e4 61 72 65 31 36 5f 76 32 20 20 20 20 20 20 20 20  are16_v2        
288e5 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
288e6 70 72 65 70 61 72 65 31 36 5f 76 32 0a 23 64 65  prepare16_v2.#de
288e7 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6c 65  fine sqlite3_cle
288e8 61 72 5f 62 69 6e 64 69 6e 67 73 20 20 20 20 20  ar_bindings     
288e9 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
288ea 3e 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 0a  >clear_bindings.
288eb 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
288ec 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 20 20 20  bind_zeroblob   
288ed 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
288ee 70 69 2d 3e 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f  pi->bind_zeroblo
288ef 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  b.#define sqlite
288f0 33 5f 62 6c 6f 62 5f 62 79 74 65 73 20 20 20 20  3_blob_bytes    
288f1 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
288f2 5f 61 70 69 2d 3e 62 6c 6f 62 5f 62 79 74 65 73  _api->blob_bytes
288f3 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
288f4 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 20 20 20 20  _blob_close     
288f5 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
288f6 61 70 69 2d 3e 62 6c 6f 62 5f 63 6c 6f 73 65 0a  api->blob_close.
288f7 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
288f8 62 6c 6f 62 5f 6f 70 65 6e 20 20 20 20 20 20 20  blob_open       
288f9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
288fa 70 69 2d 3e 62 6c 6f 62 5f 6f 70 65 6e 0a 23 64  pi->blob_open.#d
288fb 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c  efine sqlite3_bl
288fc 6f 62 5f 72 65 61 64 20 20 20 20 20 20 20 20 20  ob_read         
288fd 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
288fe 2d 3e 62 6c 6f 62 5f 72 65 61 64 0a 23 64 65 66  ->blob_read.#def
288ff 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ine sqlite3_blob
28900 5f 77 72 69 74 65 20 20 20 20 20 20 20 20 20 20  _write          
28901 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28902 62 6c 6f 62 5f 77 72 69 74 65 0a 23 64 65 66 69  blob_write.#defi
28903 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
28904 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 20 20  e_collation_v2  
28905 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
28906 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  reate_collation_
28907 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  v2.#define sqlit
28908 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 20  e3_file_control 
28909 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2890a 33 5f 61 70 69 2d 3e 66 69 6c 65 5f 63 6f 6e 74  3_api->file_cont
2890b 72 6f 6c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  rol.#define sqli
2890c 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77  te3_memory_highw
2890d 61 74 65 72 20 20 20 20 20 20 20 73 71 6c 69 74  ater       sqlit
2890e 65 33 5f 61 70 69 2d 3e 6d 65 6d 6f 72 79 5f 68  e3_api->memory_h
2890f 69 67 68 77 61 74 65 72 0a 23 64 65 66 69 6e 65  ighwater.#define
28910 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f   sqlite3_memory_
28911 75 73 65 64 20 20 20 20 20 20 20 20 20 20 20 20  used            
28912 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 65 6d  sqlite3_api->mem
28913 6f 72 79 5f 75 73 65 64 0a 23 64 65 66 69 6e 65  ory_used.#define
28914 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
28915 6c 6c 6f 63 20 20 20 20 20 20 20 20 20 20 20 20  lloc            
28916 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75 74  sqlite3_api->mut
28917 65 78 5f 61 6c 6c 6f 63 0a 23 64 65 66 69 6e 65  ex_alloc.#define
28918 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
28919 6e 74 65 72 20 20 20 20 20 20 20 20 20 20 20 20  nter            
2891a 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75 74  sqlite3_api->mut
2891b 65 78 5f 65 6e 74 65 72 0a 23 64 65 66 69 6e 65  ex_enter.#define
2891c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66   sqlite3_mutex_f
2891d 72 65 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ree             
2891e 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75 74  sqlite3_api->mut
2891f 65 78 5f 66 72 65 65 0a 23 64 65 66 69 6e 65 20  ex_free.#define 
28920 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
28921 61 76 65 20 20 20 20 20 20 20 20 20 20 20 20 73  ave            s
28922 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75 74 65  qlite3_api->mute
28923 78 5f 6c 65 61 76 65 0a 23 64 65 66 69 6e 65 20  x_leave.#define 
28924 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
28925 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  y              s
28926 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75 74 65  qlite3_api->mute
28927 78 5f 74 72 79 0a 23 64 65 66 69 6e 65 20 73 71  x_try.#define sq
28928 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 20 20 20  lite3_open_v2   
28929 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2892a 69 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e 5f 76  ite3_api->open_v
2892b 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  2.#define sqlite
2892c 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
2892d 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2892e 5f 61 70 69 2d 3e 72 65 6c 65 61 73 65 5f 6d 65  _api->release_me
2892f 6d 6f 72 79 0a 23 64 65 66 69 6e 65 20 73 71 6c  mory.#define sql
28930 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
28931 72 5f 6e 6f 6d 65 6d 20 20 20 20 20 73 71 6c 69  r_nomem     sqli
28932 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f  te3_api->result_
28933 65 72 72 6f 72 5f 6e 6f 6d 65 6d 0a 23 64 65 66  error_nomem.#def
28934 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ine sqlite3_resu
28935 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 20  lt_error_toobig 
28936 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28937 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f  result_error_too
28938 62 69 67 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  big.#define sqli
28939 74 65 33 5f 73 6c 65 65 70 20 20 20 20 20 20 20  te3_sleep       
2893a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2893b 65 33 5f 61 70 69 2d 3e 73 6c 65 65 70 0a 23 64  e3_api->sleep.#d
2893c 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 6f  efine sqlite3_so
2893d 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 20 20 20  ft_heap_limit   
2893e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2893f 2d 3e 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69  ->soft_heap_limi
28940 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  t.#define sqlite
28941 33 5f 76 66 73 5f 66 69 6e 64 20 20 20 20 20 20  3_vfs_find      
28942 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28943 5f 61 70 69 2d 3e 76 66 73 5f 66 69 6e 64 0a 23  _api->vfs_find.#
28944 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
28945 66 73 5f 72 65 67 69 73 74 65 72 20 20 20 20 20  fs_register     
28946 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28947 69 2d 3e 76 66 73 5f 72 65 67 69 73 74 65 72 0a  i->vfs_register.
28948 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28949 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 20 20  vfs_unregister  
2894a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
2894b 70 69 2d 3e 76 66 73 5f 75 6e 72 65 67 69 73 74  pi->vfs_unregist
2894c 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  er.#define sqlit
2894d 65 33 5f 74 68 72 65 61 64 73 61 66 65 20 20 20  e3_threadsafe   
2894e 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2894f 33 5f 61 70 69 2d 3e 78 74 68 72 65 61 64 73 61  3_api->xthreadsa
28950 66 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  fe.#define sqlit
28951 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c  e3_result_zerobl
28952 6f 62 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ob        sqlite
28953 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 7a 65  3_api->result_ze
28954 72 6f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73  roblob.#define s
28955 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
28956 72 6f 72 5f 63 6f 64 65 20 20 20 20 20 20 73 71  ror_code      sq
28957 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
28958 74 5f 65 72 72 6f 72 5f 63 6f 64 65 0a 23 64 65  t_error_code.#de
28959 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 65 73  fine sqlite3_tes
2895a 74 5f 63 6f 6e 74 72 6f 6c 20 20 20 20 20 20 20  t_control       
2895b 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
2895c 3e 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 0a 23 64  >test_control.#d
2895d 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 61  efine sqlite3_ra
2895e 6e 64 6f 6d 6e 65 73 73 20 20 20 20 20 20 20 20  ndomness        
2895f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28960 2d 3e 72 61 6e 64 6f 6d 6e 65 73 73 0a 23 64 65  ->randomness.#de
28961 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e  fine sqlite3_con
28962 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 20 20  text_db_handle  
28963 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28964 3e 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  >context_db_hand
28965 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
28966 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75  e3_extended_resu
28967 6c 74 5f 63 6f 64 65 73 20 20 73 71 6c 69 74 65  lt_codes  sqlite
28968 33 5f 61 70 69 2d 3e 65 78 74 65 6e 64 65 64 5f  3_api->extended_
28969 72 65 73 75 6c 74 5f 63 6f 64 65 73 0a 23 64 65  result_codes.#de
2896a 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c 69 6d  fine sqlite3_lim
2896b 69 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  it              
2896c 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
2896d 3e 6c 69 6d 69 74 0a 23 64 65 66 69 6e 65 20 73  >limit.#define s
2896e 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74  qlite3_next_stmt
2896f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
28970 6c 69 74 65 33 5f 61 70 69 2d 3e 6e 65 78 74 5f  lite3_api->next_
28971 73 74 6d 74 0a 23 64 65 66 69 6e 65 20 73 71 6c  stmt.#define sql
28972 69 74 65 33 5f 73 71 6c 20 20 20 20 20 20 20 20  ite3_sql        
28973 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28974 74 65 33 5f 61 70 69 2d 3e 73 71 6c 0a 23 64 65  te3_api->sql.#de
28975 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 74 61  fine sqlite3_sta
28976 74 75 73 20 20 20 20 20 20 20 20 20 20 20 20 20  tus             
28977 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28978 3e 73 74 61 74 75 73 0a 23 65 6e 64 69 66 20 2f  >status.#endif /
28979 2a 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 2a 2f  * SQLITE_CORE */
2897a 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
2897b 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31  _EXTENSION_INIT1
2897c 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74       const sqlit
2897d 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20  e3_api_routines 
2897e 2a 73 71 6c 69 74 65 33 5f 61 70 69 20 3d 20 30  *sqlite3_api = 0
2897f 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ;.#define SQLITE
28980 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 32  _EXTENSION_INIT2
28981 28 76 29 20 20 73 71 6c 69 74 65 33 5f 61 70 69  (v)  sqlite3_api
28982 20 3d 20 76 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a   = v;..#endif /*
28983 20 5f 53 51 4c 49 54 45 33 45 58 54 5f 48 5f 20   _SQLITE3EXT_H_ 
28984 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
28985 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74  *** End of sqlit
28986 65 33 65 78 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  e3ext.h ********
28987 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28988 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28989 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
2898a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
2898b 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
2898c 20 69 6e 20 6c 6f 61 64 65 78 74 2e 63 20 2a 2a   in loadext.c **
2898d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2898e 2a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  **/..#ifndef SQL
2898f 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  ITE_OMIT_LOAD_EX
28990 54 45 4e 53 49 4f 4e 0a 0a 2f 2a 0a 2a 2a 20 53  TENSION../*.** S
28991 6f 6d 65 20 41 50 49 20 72 6f 75 74 69 6e 65 73  ome API routines
28992 20 61 72 65 20 6f 6d 69 74 74 65 64 20 77 68 65   are omitted whe
28993 6e 20 76 61 72 69 6f 75 73 20 66 65 61 74 75 72  n various featur
28994 65 73 20 61 72 65 0a 2a 2a 20 65 78 63 6c 75 64  es are.** exclud
28995 65 64 20 66 72 6f 6d 20 61 20 62 75 69 6c 64 20  ed from a build 
28996 6f 66 20 53 51 4c 69 74 65 2e 20 20 53 75 62 73  of SQLite.  Subs
28997 74 69 74 75 74 65 20 61 20 4e 55 4c 4c 20 70 6f  titute a NULL po
28998 69 6e 74 65 72 0a 2a 2a 20 66 6f 72 20 61 6e 79  inter.** for any
28999 20 6d 69 73 73 69 6e 67 20 41 50 49 73 2e 0a 2a   missing APIs..*
2899a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
2899b 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d  _ENABLE_COLUMN_M
2899c 45 54 41 44 41 54 41 0a 23 20 64 65 66 69 6e 65  ETADATA.# define
2899d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
2899e 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 20 20 20  database_name   
2899f 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
289a0 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61  e3_column_databa
289a1 73 65 5f 6e 61 6d 65 31 36 20 30 0a 23 20 64 65  se_name16 0.# de
289a2 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
289a3 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 20  umn_table_name  
289a4 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
289a5 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61  qlite3_column_ta
289a6 62 6c 65 5f 6e 61 6d 65 31 36 20 20 20 20 30 0a  ble_name16    0.
289a7 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
289a8 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  _column_origin_n
289a9 61 6d 65 20 20 20 20 20 30 0a 23 20 64 65 66 69  ame     0.# defi
289aa 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
289ab 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 20  n_origin_name16 
289ac 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
289ad 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d  ite3_table_colum
289ae 6e 5f 6d 65 74 61 64 61 74 61 20 20 30 0a 23 65  n_metadata  0.#e
289af 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
289b0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
289b1 5a 41 54 49 4f 4e 0a 23 20 64 65 66 69 6e 65 20  ZATION.# define 
289b2 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68  sqlite3_set_auth
289b3 6f 72 69 7a 65 72 20 20 20 20 20 20 20 20 20 30  orizer         0
289b4 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
289b5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
289b6 36 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  6.# define sqlit
289b7 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 20 20  e3_bind_text16  
289b8 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65            0.# de
289b9 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
289ba 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 20  lation_needed16 
289bb 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
289bc 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65  qlite3_column_de
289bd 63 6c 74 79 70 65 31 36 20 20 20 20 20 20 30 0a  cltype16      0.
289be 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
289bf 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 20 20  _column_name16  
289c0 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69          0.# defi
289c1 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
289c2 6e 5f 74 65 78 74 31 36 20 20 20 20 20 20 20 20  n_text16        
289c3 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
289c4 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 20  ite3_complete16 
289c5 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20              0.# 
289c6 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
289c7 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31  reate_collation1
289c8 36 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65  6     0.# define
289c9 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
289ca 66 75 6e 63 74 69 6f 6e 31 36 20 20 20 20 20 20  function16      
289cb 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
289cc 65 33 5f 65 72 72 6d 73 67 31 36 20 20 20 20 20  e3_errmsg16     
289cd 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65            0.# de
289ce 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f 70 65  fine sqlite3_ope
289cf 6e 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20  n16             
289d0 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
289d1 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
289d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a                0.
289d3 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
289d4 5f 70 72 65 70 61 72 65 31 36 5f 76 32 20 20 20  _prepare16_v2   
289d5 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69          0.# defi
289d6 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
289d7 74 5f 65 72 72 6f 72 31 36 20 20 20 20 20 20 20  t_error16       
289d8 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
289d9 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
289da 31 36 20 20 20 20 20 20 20 20 20 20 30 0a 23 20  16          0.# 
289db 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
289dc 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 20 20  esult_text16be  
289dd 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65        0.# define
289de 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
289df 74 65 78 74 31 36 6c 65 20 20 20 20 20 20 20 20  text16le        
289e0 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
289e1 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 20  e3_value_text16 
289e2 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65            0.# de
289e3 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
289e4 75 65 5f 74 65 78 74 31 36 62 65 20 20 20 20 20  ue_text16be     
289e5 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
289e6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
289e7 74 31 36 6c 65 20 20 20 20 20 20 20 20 20 30 0a  t16le         0.
289e8 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
289e9 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  _column_database
289ea 5f 6e 61 6d 65 31 36 20 30 0a 23 20 64 65 66 69  _name16 0.# defi
289eb 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
289ec 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 20 20  n_table_name16  
289ed 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c    0.# define sql
289ee 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
289ef 69 6e 5f 6e 61 6d 65 31 36 20 20 20 30 0a 23 65  in_name16   0.#e
289f0 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
289f1 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54  ITE_OMIT_COMPLET
289f2 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  E.# define sqlit
289f3 65 33 5f 63 6f 6d 70 6c 65 74 65 20 30 0a 23 20  e3_complete 0.# 
289f4 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
289f5 6f 6d 70 6c 65 74 65 31 36 20 30 0a 23 65 6e 64  omplete16 0.#end
289f6 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
289f7 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f  E_OMIT_PROGRESS_
289f8 43 41 4c 4c 42 41 43 4b 0a 23 20 64 65 66 69 6e  CALLBACK.# defin
289f9 65 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65  e sqlite3_progre
289fa 73 73 5f 68 61 6e 64 6c 65 72 20 30 0a 23 65 6e  ss_handler 0.#en
289fb 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
289fc 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
289fd 41 42 4c 45 0a 23 20 64 65 66 69 6e 65 20 73 71  ABLE.# define sq
289fe 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
289ff 75 6c 65 20 30 0a 23 20 64 65 66 69 6e 65 20 73  ule 0.# define s
28a00 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
28a01 64 75 6c 65 5f 76 32 20 30 0a 23 20 64 65 66 69  dule_v2 0.# defi
28a02 6e 65 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61  ne sqlite3_decla
28a03 72 65 5f 76 74 61 62 20 30 0a 23 65 6e 64 69 66  re_vtab 0.#endif
28a04 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
28a05 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
28a06 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  E.# define sqlit
28a07 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64  e3_enable_shared
28a08 5f 63 61 63 68 65 20 30 0a 23 65 6e 64 69 66 0a  _cache 0.#endif.
28a09 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
28a0a 4d 49 54 5f 54 52 41 43 45 0a 23 20 64 65 66 69  MIT_TRACE.# defi
28a0b 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69  ne sqlite3_profi
28a0c 6c 65 20 20 20 20 20 20 20 30 0a 23 20 64 65 66  le       0.# def
28a0d 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63  ine sqlite3_trac
28a0e 65 20 20 20 20 20 20 20 20 20 30 0a 23 65 6e 64  e         0.#end
28a0f 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
28a10 45 5f 4f 4d 49 54 5f 47 45 54 5f 54 41 42 4c 45  E_OMIT_GET_TABLE
28a11 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
28a12 33 5f 66 72 65 65 5f 74 61 62 6c 65 20 20 20 20  3_free_table    
28a13 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  0.# define sqlit
28a14 65 33 5f 67 65 74 5f 74 61 62 6c 65 20 20 20 20  e3_get_table    
28a15 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65   0.#endif..#ifde
28a16 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
28a17 43 52 42 4c 4f 42 0a 23 64 65 66 69 6e 65 20 73  CRBLOB.#define s
28a18 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f  qlite3_bind_zero
28a19 62 6c 6f 62 20 20 30 0a 23 64 65 66 69 6e 65 20  blob  0.#define 
28a1a 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74  sqlite3_blob_byt
28a1b 65 73 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65  es     0.#define
28a1c 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c   sqlite3_blob_cl
28a1d 6f 73 65 20 20 20 20 20 30 0a 23 64 65 66 69 6e  ose     0.#defin
28a1e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f  e sqlite3_blob_o
28a1f 70 65 6e 20 20 20 20 20 20 30 0a 23 64 65 66 69  pen      0.#defi
28a20 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  ne sqlite3_blob_
28a21 72 65 61 64 20 20 20 20 20 20 30 0a 23 64 65 66  read      0.#def
28a22 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ine sqlite3_blob
28a23 5f 77 72 69 74 65 20 20 20 20 20 30 0a 23 65 6e  _write     0.#en
28a24 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  dif../*.** The f
28a25 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
28a26 72 65 20 63 6f 6e 74 61 69 6e 73 20 70 6f 69 6e  re contains poin
28a27 74 65 72 73 20 74 6f 20 61 6c 6c 20 53 51 4c 69  ters to all SQLi
28a28 74 65 20 41 50 49 20 72 6f 75 74 69 6e 65 73 2e  te API routines.
28a29 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  .** A pointer to
28a2a 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
28a2b 69 73 20 70 61 73 73 65 64 20 69 6e 74 6f 20 65  is passed into e
28a2c 78 74 65 6e 73 69 6f 6e 73 20 77 68 65 6e 20 74  xtensions when t
28a2d 68 65 79 20 61 72 65 0a 2a 2a 20 6c 6f 61 64 65  hey are.** loade
28a2e 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 65 78  d so that the ex
28a2f 74 65 6e 73 69 6f 6e 20 63 61 6e 20 6d 61 6b 65  tension can make
28a30 20 63 61 6c 6c 73 20 62 61 63 6b 20 69 6e 74 6f   calls back into
28a31 20 74 68 65 20 53 51 4c 69 74 65 0a 2a 2a 20 6c   the SQLite.** l
28a32 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68  ibrary..**.** Wh
28a33 65 6e 20 61 64 64 69 6e 67 20 6e 65 77 20 41 50  en adding new AP
28a34 49 73 2c 20 61 64 64 20 74 68 65 6d 20 74 6f 20  Is, add them to 
28a35 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68  the bottom of th
28a36 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  is structure.** 
28a37 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72 65 73  in order to pres
28a38 65 72 76 65 20 62 61 63 6b 77 61 72 64 73 20 63  erve backwards c
28a39 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a  ompatibility..**
28a3a 0a 2a 2a 20 45 78 74 65 6e 73 69 6f 6e 73 20 74  .** Extensions t
28a3b 68 61 74 20 75 73 65 20 6e 65 77 65 72 20 41 50  hat use newer AP
28a3c 49 73 20 73 68 6f 75 6c 64 20 66 69 72 73 74 20  Is should first 
28a3d 63 61 6c 6c 20 74 68 65 0a 2a 2a 20 73 71 6c 69  call the.** sqli
28a3e 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e  te3_libversion_n
28a3f 75 6d 62 65 72 28 29 20 74 6f 20 6d 61 6b 65 20  umber() to make 
28a40 73 75 72 65 20 74 68 61 74 20 74 68 65 20 41 50  sure that the AP
28a41 49 20 74 68 65 79 0a 2a 2a 20 69 6e 74 65 6e 64  I they.** intend
28a42 20 74 6f 20 75 73 65 20 69 73 20 73 75 70 70 6f   to use is suppo
28a43 72 74 65 64 20 62 79 20 74 68 65 20 6c 69 62 72  rted by the libr
28a44 61 72 79 2e 20 20 45 78 74 65 6e 73 69 6f 6e 73  ary.  Extensions
28a45 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 6c 73 6f 20   should.** also 
28a46 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75  check to make su
28a47 72 65 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e  re that the poin
28a48 74 65 72 20 74 6f 20 74 68 65 20 66 75 6e 63 74  ter to the funct
28a49 69 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 4e 55  ion is.** not NU
28a4a 4c 4c 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e  LL before callin
28a4b 67 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  g it..*/.static 
28a4c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70  const sqlite3_ap
28a4d 69 5f 72 6f 75 74 69 6e 65 73 20 73 71 6c 69 74  i_routines sqlit
28a4e 65 33 41 70 69 73 20 3d 20 7b 0a 20 20 73 71 6c  e3Apis = {.  sql
28a4f 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
28a50 6f 6e 74 65 78 74 2c 0a 23 69 66 6e 64 65 66 20  ontext,.#ifndef 
28a51 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52  SQLITE_OMIT_DEPR
28a52 45 43 41 54 45 44 0a 20 20 73 71 6c 69 74 65 33  ECATED.  sqlite3
28a53 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74  _aggregate_count
28a54 2c 0a 23 65 6c 73 65 0a 20 20 30 2c 0a 23 65 6e  ,.#else.  0,.#en
28a55 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 62 69  dif.  sqlite3_bi
28a56 6e 64 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74  nd_blob,.  sqlit
28a57 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 2c 0a  e3_bind_double,.
28a58 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
28a59 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69  nt,.  sqlite3_bi
28a5a 6e 64 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69  nd_int64,.  sqli
28a5b 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 2c 0a 20  te3_bind_null,. 
28a5c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61   sqlite3_bind_pa
28a5d 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 2c 0a 20  rameter_count,. 
28a5e 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61   sqlite3_bind_pa
28a5f 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 2c 0a 20  rameter_index,. 
28a60 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61   sqlite3_bind_pa
28a61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 2c 0a 20 20  rameter_name,.  
28a62 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
28a63 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  t,.  sqlite3_bin
28a64 64 5f 74 65 78 74 31 36 2c 0a 20 20 73 71 6c 69  d_text16,.  sqli
28a65 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 2c 0a  te3_bind_value,.
28a66 20 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68    sqlite3_busy_h
28a67 61 6e 64 6c 65 72 2c 0a 20 20 73 71 6c 69 74 65  andler,.  sqlite
28a68 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 2c 0a  3_busy_timeout,.
28a69 20 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65    sqlite3_change
28a6a 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6c 6f  s,.  sqlite3_clo
28a6b 73 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  se,.  sqlite3_co
28a6c 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 2c 0a  llation_needed,.
28a6d 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74    sqlite3_collat
28a6e 69 6f 6e 5f 6e 65 65 64 65 64 31 36 2c 0a 20 20  ion_needed16,.  
28a6f 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
28a70 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  lob,.  sqlite3_c
28a71 6f 6c 75 6d 6e 5f 62 79 74 65 73 2c 0a 20 20 73  olumn_bytes,.  s
28a72 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
28a73 74 65 73 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  tes16,.  sqlite3
28a74 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 2c 0a 20  _column_count,. 
28a75 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
28a76 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 2c 0a 20  database_name,. 
28a77 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
28a78 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 2c  database_name16,
28a79 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
28a7a 6e 5f 64 65 63 6c 74 79 70 65 2c 0a 20 20 73 71  n_decltype,.  sq
28a7b 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63  lite3_column_dec
28a7c 6c 74 79 70 65 31 36 2c 0a 20 20 73 71 6c 69 74  ltype16,.  sqlit
28a7d 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65  e3_column_double
28a7e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
28a7f 6d 6e 5f 69 6e 74 2c 0a 20 20 73 71 6c 69 74 65  mn_int,.  sqlite
28a80 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 2c 0a  3_column_int64,.
28a81 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
28a82 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33  _name,.  sqlite3
28a83 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 2c 0a  _column_name16,.
28a84 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
28a85 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 2c 0a 20 20  _origin_name,.  
28a86 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f  sqlite3_column_o
28a87 72 69 67 69 6e 5f 6e 61 6d 65 31 36 2c 0a 20 20  rigin_name16,.  
28a88 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
28a89 61 62 6c 65 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c  able_name,.  sql
28a8a 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c  ite3_column_tabl
28a8b 65 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71 6c 69  e_name16,.  sqli
28a8c 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 2c  te3_column_text,
28a8d 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
28a8e 6e 5f 74 65 78 74 31 36 2c 0a 20 20 73 71 6c 69  n_text16,.  sqli
28a8f 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 2c  te3_column_type,
28a90 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
28a91 6e 5f 76 61 6c 75 65 2c 0a 20 20 73 71 6c 69 74  n_value,.  sqlit
28a92 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 2c 0a  e3_commit_hook,.
28a93 20 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65    sqlite3_comple
28a94 74 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  te,.  sqlite3_co
28a95 6d 70 6c 65 74 65 31 36 2c 0a 20 20 73 71 6c 69  mplete16,.  sqli
28a96 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
28a97 74 69 6f 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f  tion,.  sqlite3_
28a98 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
28a99 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 72  16,.  sqlite3_cr
28a9a 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 2c 0a 20  eate_function,. 
28a9b 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
28a9c 66 75 6e 63 74 69 6f 6e 31 36 2c 0a 20 20 73 71  function16,.  sq
28a9d 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
28a9e 75 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 64  ule,.  sqlite3_d
28a9f 61 74 61 5f 63 6f 75 6e 74 2c 0a 20 20 73 71 6c  ata_count,.  sql
28aa0 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 2c 0a  ite3_db_handle,.
28aa1 20 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72    sqlite3_declar
28aa2 65 5f 76 74 61 62 2c 0a 20 20 73 71 6c 69 74 65  e_vtab,.  sqlite
28aa3 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f  3_enable_shared_
28aa4 63 61 63 68 65 2c 0a 20 20 73 71 6c 69 74 65 33  cache,.  sqlite3
28aa5 5f 65 72 72 63 6f 64 65 2c 0a 20 20 73 71 6c 69  _errcode,.  sqli
28aa6 74 65 33 5f 65 72 72 6d 73 67 2c 0a 20 20 73 71  te3_errmsg,.  sq
28aa7 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 2c 0a  lite3_errmsg16,.
28aa8 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63 2c 0a    sqlite3_exec,.
28aa9 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
28aaa 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 20  MIT_DEPRECATED. 
28aab 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 64   sqlite3_expired
28aac 2c 0a 23 65 6c 73 65 0a 20 20 30 2c 0a 23 65 6e  ,.#else.  0,.#en
28aad 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 66 69  dif.  sqlite3_fi
28aae 6e 61 6c 69 7a 65 2c 0a 20 20 73 71 6c 69 74 65  nalize,.  sqlite
28aaf 33 5f 66 72 65 65 2c 0a 20 20 73 71 6c 69 74 65  3_free,.  sqlite
28ab0 33 5f 66 72 65 65 5f 74 61 62 6c 65 2c 0a 20 20  3_free_table,.  
28ab1 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f  sqlite3_get_auto
28ab2 63 6f 6d 6d 69 74 2c 0a 20 20 73 71 6c 69 74 65  commit,.  sqlite
28ab3 33 5f 67 65 74 5f 61 75 78 64 61 74 61 2c 0a 20  3_get_auxdata,. 
28ab4 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62   sqlite3_get_tab
28ab5 6c 65 2c 0a 20 20 30 2c 20 20 20 20 20 2f 2a 20  le,.  0,     /* 
28ab6 57 61 73 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62  Was sqlite3_glob
28ab7 61 6c 5f 72 65 63 6f 76 65 72 28 29 2c 20 62 75  al_recover(), bu
28ab8 74 20 74 68 61 74 20 66 75 6e 63 74 69 6f 6e 20  t that function 
28ab9 69 73 20 64 65 70 72 65 63 61 74 65 64 20 2a 2f  is deprecated */
28aba 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  .  sqlite3_inter
28abb 72 75 70 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  rupt,.  sqlite3_
28abc 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
28abd 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69 62  d,.  sqlite3_lib
28abe 76 65 72 73 69 6f 6e 2c 0a 20 20 73 71 6c 69 74  version,.  sqlit
28abf 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75  e3_libversion_nu
28ac0 6d 62 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f  mber,.  sqlite3_
28ac1 6d 61 6c 6c 6f 63 2c 0a 20 20 73 71 6c 69 74 65  malloc,.  sqlite
28ac2 33 5f 6d 70 72 69 6e 74 66 2c 0a 20 20 73 71 6c  3_mprintf,.  sql
28ac3 69 74 65 33 5f 6f 70 65 6e 2c 0a 20 20 73 71 6c  ite3_open,.  sql
28ac4 69 74 65 33 5f 6f 70 65 6e 31 36 2c 0a 20 20 73  ite3_open16,.  s
28ac5 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 2c 0a  qlite3_prepare,.
28ac6 20 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72    sqlite3_prepar
28ac7 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70  e16,.  sqlite3_p
28ac8 72 6f 66 69 6c 65 2c 0a 20 20 73 71 6c 69 74 65  rofile,.  sqlite
28ac9 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c  3_progress_handl
28aca 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  er,.  sqlite3_re
28acb 61 6c 6c 6f 63 2c 0a 20 20 73 71 6c 69 74 65 33  alloc,.  sqlite3
28acc 5f 72 65 73 65 74 2c 0a 20 20 73 71 6c 69 74 65  _reset,.  sqlite
28acd 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 2c 0a 20  3_result_blob,. 
28ace 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
28acf 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c 69 74 65  double,.  sqlite
28ad0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 2c 0a  3_result_error,.
28ad1 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
28ad2 5f 65 72 72 6f 72 31 36 2c 0a 20 20 73 71 6c 69  _error16,.  sqli
28ad3 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 2c 0a  te3_result_int,.
28ad4 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
28ad5 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69 74 65  _int64,.  sqlite
28ad6 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 2c 0a 20  3_result_null,. 
28ad7 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
28ad8 74 65 78 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  text,.  sqlite3_
28ad9 72 65 73 75 6c 74 5f 74 65 78 74 31 36 2c 0a 20  result_text16,. 
28ada 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
28adb 74 65 78 74 31 36 62 65 2c 0a 20 20 73 71 6c 69  text16be,.  sqli
28adc 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
28add 36 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  6le,.  sqlite3_r
28ade 65 73 75 6c 74 5f 76 61 6c 75 65 2c 0a 20 20 73  esult_value,.  s
28adf 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f  qlite3_rollback_
28ae0 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74 65 33 5f  hook,.  sqlite3_
28ae1 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 2c 0a  set_authorizer,.
28ae2 20 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75    sqlite3_set_au
28ae3 78 64 61 74 61 2c 0a 20 20 73 71 6c 69 74 65 33  xdata,.  sqlite3
28ae4 5f 73 6e 70 72 69 6e 74 66 2c 0a 20 20 73 71 6c  _snprintf,.  sql
28ae5 69 74 65 33 5f 73 74 65 70 2c 0a 20 20 73 71 6c  ite3_step,.  sql
28ae6 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d  ite3_table_colum
28ae7 6e 5f 6d 65 74 61 64 61 74 61 2c 0a 23 69 66 6e  n_metadata,.#ifn
28ae8 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
28ae9 44 45 50 52 45 43 41 54 45 44 0a 20 20 73 71 6c  DEPRECATED.  sql
28aea 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61  ite3_thread_clea
28aeb 6e 75 70 2c 0a 23 65 6c 73 65 0a 20 20 30 2c 0a  nup,.#else.  0,.
28aec 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33  #endif.  sqlite3
28aed 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 2c 0a  _total_changes,.
28aee 20 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 2c    sqlite3_trace,
28aef 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
28af0 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a  OMIT_DEPRECATED.
28af1 20 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 66    sqlite3_transf
28af2 65 72 5f 62 69 6e 64 69 6e 67 73 2c 0a 23 65 6c  er_bindings,.#el
28af3 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69 66 0a 20  se.  0,.#endif. 
28af4 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f   sqlite3_update_
28af5 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74 65 33 5f  hook,.  sqlite3_
28af6 75 73 65 72 5f 64 61 74 61 2c 0a 20 20 73 71 6c  user_data,.  sql
28af7 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 2c  ite3_value_blob,
28af8 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
28af9 5f 62 79 74 65 73 2c 0a 20 20 73 71 6c 69 74 65  _bytes,.  sqlite
28afa 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 2c  3_value_bytes16,
28afb 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
28afc 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c 69 74  _double,.  sqlit
28afd 65 33 5f 76 61 6c 75 65 5f 69 6e 74 2c 0a 20 20  e3_value_int,.  
28afe 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
28aff 74 36 34 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  t64,.  sqlite3_v
28b00 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70  alue_numeric_typ
28b01 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  e,.  sqlite3_val
28b02 75 65 5f 74 65 78 74 2c 0a 20 20 73 71 6c 69 74  ue_text,.  sqlit
28b03 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c  e3_value_text16,
28b04 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
28b05 5f 74 65 78 74 31 36 62 65 2c 0a 20 20 73 71 6c  _text16be,.  sql
28b06 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
28b07 36 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  6le,.  sqlite3_v
28b08 61 6c 75 65 5f 74 79 70 65 2c 0a 20 20 73 71 6c  alue_type,.  sql
28b09 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 2c 0a 20  ite3_vmprintf,. 
28b0a 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6f 72 69   /*.  ** The ori
28b0b 67 69 6e 61 6c 20 41 50 49 20 73 65 74 20 65 6e  ginal API set en
28b0c 64 73 20 68 65 72 65 2e 20 20 41 6c 6c 20 65 78  ds here.  All ex
28b0d 74 65 6e 73 69 6f 6e 73 20 63 61 6e 20 63 61 6c  tensions can cal
28b0e 6c 20 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68  l any.  ** of th
28b0f 65 20 41 50 49 73 20 61 62 6f 76 65 20 70 72 6f  e APIs above pro
28b10 76 69 64 65 64 20 74 68 61 74 20 74 68 65 20 70  vided that the p
28b11 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55  ointer is not NU
28b12 4c 4c 2e 20 20 42 75 74 0a 20 20 2a 2a 20 62 65  LL.  But.  ** be
28b13 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 41 50 49  fore calling API
28b14 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 2c 20 65  s that follow, e
28b15 78 74 65 6e 73 69 6f 6e 20 73 68 6f 75 6c 64 20  xtension should 
28b16 63 68 65 63 6b 20 74 68 65 0a 20 20 2a 2a 20 73  check the.  ** s
28b17 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
28b18 6e 5f 6e 75 6d 62 65 72 28 29 20 74 6f 20 6d 61  n_number() to ma
28b19 6b 65 20 73 75 72 65 20 74 68 65 79 20 61 72 65  ke sure they are
28b1a 20 64 65 61 6c 69 6e 67 20 77 69 74 68 0a 20 20   dealing with.  
28b1b 2a 2a 20 61 20 6c 69 62 72 61 72 79 20 74 68 61  ** a library tha
28b1c 74 20 69 73 20 6e 65 77 20 65 6e 6f 75 67 68 20  t is new enough 
28b1d 74 6f 20 73 75 70 70 6f 72 74 20 74 68 61 74 20  to support that 
28b1e 41 50 49 2e 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  API..  *********
28b1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28b21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28b22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28b23 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  .  */.  sqlite3_
28b24 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f  overload_functio
28b25 6e 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64  n,..  /*.  ** Ad
28b26 64 65 64 20 61 66 74 65 72 20 33 2e 33 2e 31 33  ded after 3.3.13
28b27 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  .  */.  sqlite3_
28b28 70 72 65 70 61 72 65 5f 76 32 2c 0a 20 20 73 71  prepare_v2,.  sq
28b29 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f  lite3_prepare16_
28b2a 76 32 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6c  v2,.  sqlite3_cl
28b2b 65 61 72 5f 62 69 6e 64 69 6e 67 73 2c 0a 0a 20  ear_bindings,.. 
28b2c 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20 66   /*.  ** Added f
28b2d 6f 72 20 33 2e 34 2e 31 0a 20 20 2a 2f 0a 20 20  or 3.4.1.  */.  
28b2e 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
28b2f 6f 64 75 6c 65 5f 76 32 2c 0a 0a 20 20 2f 2a 0a  odule_v2,..  /*.
28b30 20 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33    ** Added for 3
28b31 2e 35 2e 30 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  .5.0.  */.  sqli
28b32 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f  te3_bind_zeroblo
28b33 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f  b,.  sqlite3_blo
28b34 62 5f 62 79 74 65 73 2c 0a 20 20 73 71 6c 69 74  b_bytes,.  sqlit
28b35 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 2c 0a 20  e3_blob_close,. 
28b36 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70   sqlite3_blob_op
28b37 65 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c  en,.  sqlite3_bl
28b38 6f 62 5f 72 65 61 64 2c 0a 20 20 73 71 6c 69 74  ob_read,.  sqlit
28b39 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 2c 0a 20  e3_blob_write,. 
28b3a 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
28b3b 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 2c 0a 20 20  collation_v2,.  
28b3c 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e  sqlite3_file_con
28b3d 74 72 6f 6c 2c 0a 20 20 73 71 6c 69 74 65 33 5f  trol,.  sqlite3_
28b3e 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72  memory_highwater
28b3f 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  ,.  sqlite3_memo
28b40 72 79 5f 75 73 65 64 2c 0a 23 69 66 64 65 66 20  ry_used,.#ifdef 
28b41 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49  SQLITE_MUTEX_OMI
28b42 54 0a 20 20 30 2c 20 0a 20 20 30 2c 20 0a 20 20  T.  0, .  0, .  
28b43 30 2c 0a 20 20 30 2c 0a 20 20 30 2c 0a 23 65 6c  0,.  0,.  0,.#el
28b44 73 65 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  se.  sqlite3_mut
28b45 65 78 5f 61 6c 6c 6f 63 2c 0a 20 20 73 71 6c 69  ex_alloc,.  sqli
28b46 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 2c  te3_mutex_enter,
28b47 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
28b48 5f 66 72 65 65 2c 0a 20 20 73 71 6c 69 74 65 33  _free,.  sqlite3
28b49 5f 6d 75 74 65 78 5f 6c 65 61 76 65 2c 0a 20 20  _mutex_leave,.  
28b4a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
28b4b 79 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69  y,.#endif.  sqli
28b4c 74 65 33 5f 6f 70 65 6e 5f 76 32 2c 0a 20 20 73  te3_open_v2,.  s
28b4d 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
28b4e 65 6d 6f 72 79 2c 0a 20 20 73 71 6c 69 74 65 33  emory,.  sqlite3
28b4f 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f  _result_error_no
28b50 6d 65 6d 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  mem,.  sqlite3_r
28b51 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62  esult_error_toob
28b52 69 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 6c  ig,.  sqlite3_sl
28b53 65 65 70 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73  eep,.  sqlite3_s
28b54 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 2c 0a  oft_heap_limit,.
28b55 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69    sqlite3_vfs_fi
28b56 6e 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  nd,.  sqlite3_vf
28b57 73 5f 72 65 67 69 73 74 65 72 2c 0a 20 20 73 71  s_register,.  sq
28b58 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69  lite3_vfs_unregi
28b59 73 74 65 72 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  ster,..  /*.  **
28b5a 20 41 64 64 65 64 20 66 6f 72 20 33 2e 35 2e 38   Added for 3.5.8
28b5b 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  .  */.  sqlite3_
28b5c 74 68 72 65 61 64 73 61 66 65 2c 0a 20 20 73 71  threadsafe,.  sq
28b5d 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72  lite3_result_zer
28b5e 6f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33  oblob,.  sqlite3
28b5f 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f  _result_error_co
28b60 64 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 74 65  de,.  sqlite3_te
28b61 73 74 5f 63 6f 6e 74 72 6f 6c 2c 0a 20 20 73 71  st_control,.  sq
28b62 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73  lite3_randomness
28b63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ,.  sqlite3_cont
28b64 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 2c 0a 0a  ext_db_handle,..
28b65 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20    /*.  ** Added 
28b66 66 6f 72 20 33 2e 36 2e 30 0a 20 20 2a 2f 0a 20  for 3.6.0.  */. 
28b67 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65   sqlite3_extende
28b68 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 2c 0a  d_result_codes,.
28b69 20 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 2c    sqlite3_limit,
28b6a 0a 20 20 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f  .  sqlite3_next_
28b6b 73 74 6d 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  stmt,.  sqlite3_
28b6c 73 71 6c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73  sql,.  sqlite3_s
28b6d 74 61 74 75 73 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  tatus,.};../*.**
28b6e 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64   Attempt to load
28b6f 20 61 6e 20 53 51 4c 69 74 65 20 65 78 74 65 6e   an SQLite exten
28b70 73 69 6f 6e 20 6c 69 62 72 61 72 79 20 63 6f 6e  sion library con
28b71 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66 69  tained in the fi
28b72 6c 65 0a 2a 2a 20 7a 46 69 6c 65 2e 20 20 54 68  le.** zFile.  Th
28b73 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 69 73  e entry point is
28b74 20 7a 50 72 6f 63 2e 20 20 7a 50 72 6f 63 20 6d   zProc.  zProc m
28b75 61 79 20 62 65 20 30 20 69 6e 20 77 68 69 63 68  ay be 0 in which
28b76 20 63 61 73 65 20 61 0a 2a 2a 20 64 65 66 61 75   case a.** defau
28b77 6c 74 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 6e  lt entry point n
28b78 61 6d 65 20 28 73 71 6c 69 74 65 33 5f 65 78 74  ame (sqlite3_ext
28b79 65 6e 73 69 6f 6e 5f 69 6e 69 74 29 20 69 73 20  ension_init) is 
28b7a 75 73 65 64 2e 20 20 55 73 65 0a 2a 2a 20 6f 66  used.  Use.** of
28b7b 20 74 68 65 20 64 65 66 61 75 6c 74 20 6e 61 6d   the default nam
28b7c 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64  e is recommended
28b7d 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
28b7e 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
28b7f 65 73 73 20 61 6e 64 20 53 51 4c 49 54 45 5f 45  ess and SQLITE_E
28b80 52 52 4f 52 20 69 66 20 73 6f 6d 65 74 68 69 6e  RROR if somethin
28b81 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a  g goes wrong..**
28b82 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20  .** If an error 
28b83 6f 63 63 75 72 73 20 61 6e 64 20 70 7a 45 72 72  occurs and pzErr
28b84 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c 20 74 68  Msg is not 0, th
28b85 65 6e 20 66 69 6c 6c 20 2a 70 7a 45 72 72 4d 73  en fill *pzErrMs
28b86 67 20 77 69 74 68 20 0a 2a 2a 20 65 72 72 6f 72  g with .** error
28b87 20 6d 65 73 73 61 67 65 20 74 65 78 74 2e 20 20   message text.  
28b88 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  The calling func
28b89 74 69 6f 6e 20 73 68 6f 75 6c 64 20 66 72 65 65  tion should free
28b8a 20 74 68 69 73 20 6d 65 6d 6f 72 79 0a 2a 2a 20   this memory.** 
28b8b 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74  by calling sqlit
28b8c 65 33 44 62 46 72 65 65 28 64 62 2c 20 29 2e 0a  e3DbFree(db, )..
28b8d 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  */.static int sq
28b8e 6c 69 74 65 33 4c 6f 61 64 45 78 74 65 6e 73 69  lite3LoadExtensi
28b8f 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  on(.  sqlite3 *d
28b90 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  b,          /* L
28b91 6f 61 64 20 74 68 65 20 65 78 74 65 6e 73 69 6f  oad the extensio
28b92 6e 20 69 6e 74 6f 20 74 68 69 73 20 64 61 74 61  n into this data
28b93 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
28b94 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
28b95 2a 7a 46 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61  *zFile,    /* Na
28b96 6d 65 20 6f 66 20 74 68 65 20 73 68 61 72 65 64  me of the shared
28b97 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e   library contain
28b98 69 6e 67 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f  ing extension */
28b99 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
28b9a 50 72 6f 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72  Proc,    /* Entr
28b9b 79 20 70 6f 69 6e 74 2e 20 20 55 73 65 20 22 73  y point.  Use "s
28b9c 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e  qlite3_extension
28b9d 5f 69 6e 69 74 22 20 69 66 20 30 20 2a 2f 0a 20  _init" if 0 */. 
28b9e 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
28b9f 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20 65 72         /* Put er
28ba0 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65 72 65  ror message here
28ba1 20 69 66 20 6e 6f 74 20 30 20 2a 2f 0a 29 7b 0a   if not 0 */.){.
28ba2 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
28ba3 56 66 73 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a  Vfs = db->pVfs;.
28ba4 20 20 76 6f 69 64 20 2a 68 61 6e 64 6c 65 3b 0a    void *handle;.
28ba5 20 20 69 6e 74 20 28 2a 78 49 6e 69 74 29 28 73    int (*xInit)(s
28ba6 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63  qlite3*,char**,c
28ba7 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69  onst sqlite3_api
28ba8 5f 72 6f 75 74 69 6e 65 73 2a 29 3b 0a 20 20 63  _routines*);.  c
28ba9 68 61 72 20 2a 7a 45 72 72 6d 73 67 20 3d 20 30  har *zErrmsg = 0
28baa 3b 0a 20 20 76 6f 69 64 20 2a 2a 61 48 61 6e 64  ;.  void **aHand
28bab 6c 65 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20  le;.  const int 
28bac 6e 4d 73 67 20 3d 20 33 30 30 3b 0a 0a 20 20 69  nMsg = 300;..  i
28bad 66 28 20 70 7a 45 72 72 4d 73 67 20 29 20 2a 70  f( pzErrMsg ) *p
28bae 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 0a 20 20  zErrMsg = 0;..  
28baf 2f 2a 20 54 69 63 6b 65 74 20 23 31 38 36 33 2e  /* Ticket #1863.
28bb0 20 20 54 6f 20 61 76 6f 69 64 20 61 20 63 72 65    To avoid a cre
28bb1 61 74 69 6e 67 20 73 65 63 75 72 69 74 79 20 70  ating security p
28bb2 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 6c 64 65  roblems for olde
28bb3 72 0a 20 20 2a 2a 20 61 70 70 6c 69 63 61 74 69  r.  ** applicati
28bb4 6f 6e 73 20 74 68 61 74 20 72 65 6c 69 6e 6b 20  ons that relink 
28bb5 61 67 61 69 6e 73 74 20 6e 65 77 65 72 20 76 65  against newer ve
28bb6 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65  rsions of SQLite
28bb7 2c 20 74 68 65 0a 20 20 2a 2a 20 61 62 69 6c 69  , the.  ** abili
28bb8 74 79 20 74 6f 20 72 75 6e 20 6c 6f 61 64 5f 65  ty to run load_e
28bb9 78 74 65 6e 73 69 6f 6e 20 69 73 20 74 75 72 6e  xtension is turn
28bba 65 64 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c  ed off by defaul
28bbb 74 2e 20 20 4f 6e 65 0a 20 20 2a 2a 20 6d 75 73  t.  One.  ** mus
28bbc 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 65  t call sqlite3_e
28bbd 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e  nable_load_exten
28bbe 73 69 6f 6e 28 29 20 74 6f 20 74 75 72 6e 20 6f  sion() to turn o
28bbf 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 20 20 2a 2a  n extension.  **
28bc0 20 6c 6f 61 64 69 6e 67 2e 20 20 4f 74 68 65 72   loading.  Other
28bc1 77 69 73 65 20 79 6f 75 20 67 65 74 20 74 68 65  wise you get the
28bc2 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72 72 6f 72   following error
28bc3 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62  ..  */.  if( (db
28bc4 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
28bc5 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 29 3d  _LoadExtension)=
28bc6 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 7a  =0 ){.    if( pz
28bc7 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20  ErrMsg ){.      
28bc8 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69  *pzErrMsg = sqli
28bc9 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 6e 6f 74  te3_mprintf("not
28bca 20 61 75 74 68 6f 72 69 7a 65 64 22 29 3b 0a 20   authorized");. 
28bcb 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
28bcc 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
28bcd 7d 0a 0a 20 20 69 66 28 20 7a 50 72 6f 63 3d 3d  }..  if( zProc==
28bce 30 20 29 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d  0 ){.    zProc =
28bcf 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73   "sqlite3_extens
28bd0 69 6f 6e 5f 69 6e 69 74 22 3b 0a 20 20 7d 0a 0a  ion_init";.  }..
28bd1 20 20 68 61 6e 64 6c 65 20 3d 20 73 71 6c 69 74    handle = sqlit
28bd2 65 33 4f 73 44 6c 4f 70 65 6e 28 70 56 66 73 2c  e3OsDlOpen(pVfs,
28bd3 20 7a 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 68   zFile);.  if( h
28bd4 61 6e 64 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20  andle==0 ){.    
28bd5 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a  if( pzErrMsg ){.
28bd6 20 20 20 20 20 20 7a 45 72 72 6d 73 67 20 3d 20        zErrmsg = 
28bd7 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f  sqlite3StackAllo
28bd8 63 5a 65 72 6f 28 64 62 2c 20 6e 4d 73 67 29 3b  cZero(db, nMsg);
28bd9 0a 20 20 20 20 20 20 69 66 28 20 7a 45 72 72 6d  .      if( zErrm
28bda 73 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  sg ){.        sq
28bdb 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
28bdc 4d 73 67 2c 20 7a 45 72 72 6d 73 67 2c 20 0a 20  Msg, zErrmsg, . 
28bdd 20 20 20 20 20 20 20 20 20 20 20 22 75 6e 61 62             "unab
28bde 6c 65 20 74 6f 20 6f 70 65 6e 20 73 68 61 72 65  le to open share
28bdf 64 20 6c 69 62 72 61 72 79 20 5b 25 73 5d 22 2c  d library [%s]",
28be0 20 7a 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 20   zFile);.       
28be1 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f   sqlite3OsDlErro
28be2 72 28 70 56 66 73 2c 20 6e 4d 73 67 2d 31 2c 20  r(pVfs, nMsg-1, 
28be3 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20  zErrmsg);.      
28be4 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71    *pzErrMsg = sq
28be5 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c  lite3DbStrDup(0,
28be6 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 20   zErrmsg);.     
28be7 20 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46     sqlite3StackF
28be8 72 65 65 28 64 62 2c 20 7a 45 72 72 6d 73 67 29  ree(db, zErrmsg)
28be9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
28bea 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
28beb 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 78  E_ERROR;.  }.  x
28bec 49 6e 69 74 20 3d 20 28 69 6e 74 28 2a 29 28 73  Init = (int(*)(s
28bed 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63  qlite3*,char**,c
28bee 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69  onst sqlite3_api
28bef 5f 72 6f 75 74 69 6e 65 73 2a 29 29 0a 20 20 20  _routines*)).   
28bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28bf1 73 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 70  sqlite3OsDlSym(p
28bf2 56 66 73 2c 20 68 61 6e 64 6c 65 2c 20 7a 50 72  Vfs, handle, zPr
28bf3 6f 63 29 3b 0a 20 20 69 66 28 20 78 49 6e 69 74  oc);.  if( xInit
28bf4 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ==0 ){.    if( p
28bf5 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20  zErrMsg ){.     
28bf6 20 7a 45 72 72 6d 73 67 20 3d 20 73 71 6c 69 74   zErrmsg = sqlit
28bf7 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f  e3StackAllocZero
28bf8 28 64 62 2c 20 6e 4d 73 67 29 3b 0a 20 20 20 20  (db, nMsg);.    
28bf9 20 20 69 66 28 20 7a 45 72 72 6d 73 67 20 29 7b    if( zErrmsg ){
28bfa 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
28bfb 5f 73 6e 70 72 69 6e 74 66 28 6e 4d 73 67 2c 20  _snprintf(nMsg, 
28bfc 7a 45 72 72 6d 73 67 2c 0a 20 20 20 20 20 20 20  zErrmsg,.       
28bfd 20 20 20 20 20 22 6e 6f 20 65 6e 74 72 79 20 70       "no entry p
28bfe 6f 69 6e 74 20 5b 25 73 5d 20 69 6e 20 73 68 61  oint [%s] in sha
28bff 72 65 64 20 6c 69 62 72 61 72 79 20 5b 25 73 5d  red library [%s]
28c00 22 2c 20 7a 50 72 6f 63 2c 7a 46 69 6c 65 29 3b  ", zProc,zFile);
28c01 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
28c02 4f 73 44 6c 45 72 72 6f 72 28 70 56 66 73 2c 20  OsDlError(pVfs, 
28c03 6e 4d 73 67 2d 31 2c 20 7a 45 72 72 6d 73 67 29  nMsg-1, zErrmsg)
28c04 3b 0a 20 20 20 20 20 20 20 20 2a 70 7a 45 72 72  ;.        *pzErr
28c05 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53  Msg = sqlite3DbS
28c06 74 72 44 75 70 28 30 2c 20 7a 45 72 72 6d 73 67  trDup(0, zErrmsg
28c07 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
28c08 65 33 53 74 61 63 6b 46 72 65 65 28 64 62 2c 20  e3StackFree(db, 
28c09 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20  zErrmsg);.      
28c0a 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  }.      sqlite3O
28c0b 73 44 6c 43 6c 6f 73 65 28 70 56 66 73 2c 20 68  sDlClose(pVfs, h
28c0c 61 6e 64 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20  andle);.    }.  
28c0d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
28c0e 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 20 69  ERROR;.  }else i
28c0f 66 28 20 78 49 6e 69 74 28 64 62 2c 20 26 7a 45  f( xInit(db, &zE
28c10 72 72 6d 73 67 2c 20 26 73 71 6c 69 74 65 33 41  rrmsg, &sqlite3A
28c11 70 69 73 29 20 29 7b 0a 20 20 20 20 69 66 28 20  pis) ){.    if( 
28c12 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20  pzErrMsg ){.    
28c13 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71    *pzErrMsg = sq
28c14 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 65  lite3_mprintf("e
28c15 72 72 6f 72 20 64 75 72 69 6e 67 20 69 6e 69 74  rror during init
28c16 69 61 6c 69 7a 61 74 69 6f 6e 3a 20 25 73 22 2c  ialization: %s",
28c17 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 7d   zErrmsg);.    }
28c18 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
28c19 65 28 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20  e(zErrmsg);.    
28c1a 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65  sqlite3OsDlClose
28c1b 28 70 56 66 73 2c 20 68 61 6e 64 6c 65 29 3b 0a  (pVfs, handle);.
28c1c 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
28c1d 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20  E_ERROR;.  }..  
28c1e 2f 2a 20 41 70 70 65 6e 64 20 74 68 65 20 6e 65  /* Append the ne
28c1f 77 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  w shared library
28c20 20 68 61 6e 64 6c 65 20 74 6f 20 74 68 65 20 64   handle to the d
28c21 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 20 61 72  b->aExtension ar
28c22 72 61 79 2e 20 2a 2f 0a 20 20 61 48 61 6e 64 6c  ray. */.  aHandl
28c23 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  e = sqlite3DbMal
28c24 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
28c25 6f 66 28 68 61 6e 64 6c 65 29 2a 28 64 62 2d 3e  of(handle)*(db->
28c26 6e 45 78 74 65 6e 73 69 6f 6e 2b 31 29 29 3b 0a  nExtension+1));.
28c27 20 20 69 66 28 20 61 48 61 6e 64 6c 65 3d 3d 30    if( aHandle==0
28c28 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
28c29 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
28c2a 0a 20 20 69 66 28 20 64 62 2d 3e 6e 45 78 74 65  .  if( db->nExte
28c2b 6e 73 69 6f 6e 3e 30 20 29 7b 0a 20 20 20 20 6d  nsion>0 ){.    m
28c2c 65 6d 63 70 79 28 61 48 61 6e 64 6c 65 2c 20 64  emcpy(aHandle, d
28c2d 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 2c 20 73  b->aExtension, s
28c2e 69 7a 65 6f 66 28 68 61 6e 64 6c 65 29 2a 64 62  izeof(handle)*db
28c2f 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 29 3b 0a 20  ->nExtension);. 
28c30 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72   }.  sqlite3DbFr
28c31 65 65 28 64 62 2c 20 64 62 2d 3e 61 45 78 74 65  ee(db, db->aExte
28c32 6e 73 69 6f 6e 29 3b 0a 20 20 64 62 2d 3e 61 45  nsion);.  db->aE
28c33 78 74 65 6e 73 69 6f 6e 20 3d 20 61 48 61 6e 64  xtension = aHand
28c34 6c 65 3b 0a 0a 20 20 64 62 2d 3e 61 45 78 74 65  le;..  db->aExte
28c35 6e 73 69 6f 6e 5b 64 62 2d 3e 6e 45 78 74 65 6e  nsion[db->nExten
28c36 73 69 6f 6e 2b 2b 5d 20 3d 20 68 61 6e 64 6c 65  sion++] = handle
28c37 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
28c38 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  E_OK;.}.SQLITE_A
28c39 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c  PI int sqlite3_l
28c3a 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 0a 20  oad_extension(. 
28c3b 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
28c3c 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74         /* Load t
28c3d 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 74  he extension int
28c3e 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  o this database 
28c3f 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
28c40 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
28c41 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  e,    /* Name of
28c42 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
28c43 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 65  ary containing e
28c44 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f  xtension */.  co
28c45 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 6f 63 2c  nst char *zProc,
28c46 20 20 20 20 2f 2a 20 45 6e 74 72 79 20 70 6f 69      /* Entry poi
28c47 6e 74 2e 20 20 55 73 65 20 22 73 71 6c 69 74 65  nt.  Use "sqlite
28c48 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74  3_extension_init
28c49 22 20 69 66 20 30 20 2a 2f 0a 20 20 63 68 61 72  " if 0 */.  char
28c4a 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20   **pzErrMsg     
28c4b 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72 20 6d    /* Put error m
28c4c 65 73 73 61 67 65 20 68 65 72 65 20 69 66 20 6e  essage here if n
28c4d 6f 74 20 30 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ot 0 */.){.  int
28c4e 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d   rc;.  sqlite3_m
28c4f 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
28c50 75 74 65 78 29 3b 0a 20 20 72 63 20 3d 20 73 71  utex);.  rc = sq
28c51 6c 69 74 65 33 4c 6f 61 64 45 78 74 65 6e 73 69  lite3LoadExtensi
28c52 6f 6e 28 64 62 2c 20 7a 46 69 6c 65 2c 20 7a 50  on(db, zFile, zP
28c53 72 6f 63 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a  roc, pzErrMsg);.
28c54 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
28c55 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20  iExit(db, rc);. 
28c56 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
28c57 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
28c58 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
28c59 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73  ./*.** Call this
28c5a 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 74 68   routine when th
28c5b 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
28c5c 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 69 6e 67  ction is closing
28c5d 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20   in order.** to 
28c5e 63 6c 65 61 6e 20 75 70 20 6c 6f 61 64 65 64 20  clean up loaded 
28c5f 65 78 74 65 6e 73 69 6f 6e 73 0a 2a 2f 0a 53 51  extensions.*/.SQ
28c60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
28c61 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78  d sqlite3CloseEx
28c62 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33  tensions(sqlite3
28c63 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   *db){.  int i;.
28c64 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
28c65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
28c66 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72  >mutex) );.  for
28c67 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 45 78 74  (i=0; i<db->nExt
28c68 65 6e 73 69 6f 6e 3b 20 69 2b 2b 29 7b 0a 20 20  ension; i++){.  
28c69 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f    sqlite3OsDlClo
28c6a 73 65 28 64 62 2d 3e 70 56 66 73 2c 20 64 62 2d  se(db->pVfs, db-
28c6b 3e 61 45 78 74 65 6e 73 69 6f 6e 5b 69 5d 29 3b  >aExtension[i]);
28c6c 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62  .  }.  sqlite3Db
28c6d 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 45 78  Free(db, db->aEx
28c6e 74 65 6e 73 69 6f 6e 29 3b 0a 7d 0a 0a 2f 2a 0a  tension);.}../*.
28c6f 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 69 73  ** Enable or dis
28c70 61 62 6c 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c  able extension l
28c71 6f 61 64 69 6e 67 2e 20 20 45 78 74 65 6e 73 69  oading.  Extensi
28c72 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73 20 64 69  on loading is di
28c73 73 61 62 6c 65 64 20 62 79 0a 2a 2a 20 64 65 66  sabled by.** def
28c74 61 75 6c 74 20 73 6f 20 61 73 20 6e 6f 74 20 74  ault so as not t
28c75 6f 20 6f 70 65 6e 20 73 65 63 75 72 69 74 79 20  o open security 
28c76 68 6f 6c 65 73 20 69 6e 20 6f 6c 64 65 72 20 61  holes in older a
28c77 70 70 6c 69 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a  pplications..*/.
28c78 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
28c79 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f  qlite3_enable_lo
28c7a 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 73 71 6c  ad_extension(sql
28c7b 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 6e  ite3 *db, int on
28c7c 6f 66 66 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  off){.  sqlite3_
28c7d 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
28c7e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6f 6e  mutex);.  if( on
28c7f 6f 66 66 20 29 7b 0a 20 20 20 20 64 62 2d 3e 66  off ){.    db->f
28c80 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4c  lags |= SQLITE_L
28c81 6f 61 64 45 78 74 65 6e 73 69 6f 6e 3b 0a 20 20  oadExtension;.  
28c82 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e 66  }else{.    db->f
28c83 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f  lags &= ~SQLITE_
28c84 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 3b 0a 20  LoadExtension;. 
28c85 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
28c86 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
28c87 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ex);.  return SQ
28c88 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64  LITE_OK;.}..#end
28c89 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
28c8a 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  T_LOAD_EXTENSION
28c8b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61   */../*.** The a
28c8c 75 74 6f 2d 65 78 74 65 6e 73 69 6f 6e 20 63 6f  uto-extension co
28c8d 64 65 20 61 64 64 65 64 20 72 65 67 61 72 64 6c  de added regardl
28c8e 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f  ess of whether o
28c8f 72 20 6e 6f 74 20 65 78 74 65 6e 73 69 6f 6e 0a  r not extension.
28c90 2a 2a 20 6c 6f 61 64 69 6e 67 20 69 73 20 73 75  ** loading is su
28c91 70 70 6f 72 74 65 64 2e 20 20 57 65 20 6e 65 65  pported.  We nee
28c92 64 20 61 20 64 75 6d 6d 79 20 73 71 6c 69 74 65  d a dummy sqlite
28c93 33 41 70 69 73 20 70 6f 69 6e 74 65 72 20 66 6f  3Apis pointer fo
28c94 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64 65 20 69  r that.** code i
28c95 66 20 72 65 67 75 6c 61 72 20 65 78 74 65 6e 73  f regular extens
28c96 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73 20 6e  ion loading is n
28c97 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 54  ot available.  T
28c98 68 69 73 20 69 73 20 74 68 61 74 0a 2a 2a 20 64  his is that.** d
28c99 75 6d 6d 79 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f  ummy pointer..*/
28c9a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
28c9b 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
28c9c 4f 4e 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  ON.static const 
28c9d 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
28c9e 69 6e 65 73 20 73 71 6c 69 74 65 33 41 70 69 73  ines sqlite3Apis
28c9f 20 3d 20 7b 20 30 20 7d 3b 0a 23 65 6e 64 69 66   = { 0 };.#endif
28ca0 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  .../*.** The fol
28ca1 6c 6f 77 69 6e 67 20 6f 62 6a 65 63 74 20 68 6f  lowing object ho
28ca2 6c 64 73 20 74 68 65 20 6c 69 73 74 20 6f 66 20  lds the list of 
28ca3 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 6c 6f  automatically lo
28ca4 61 64 65 64 0a 2a 2a 20 65 78 74 65 6e 73 69 6f  aded.** extensio
28ca5 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6c  ns..**.** This l
28ca6 69 73 74 20 69 73 20 73 68 61 72 65 64 20 61 63  ist is shared ac
28ca7 72 6f 73 73 20 74 68 72 65 61 64 73 2e 20 20 54  ross threads.  T
28ca8 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  he SQLITE_MUTEX_
28ca9 53 54 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a  STATIC_MASTER.**
28caa 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68   mutex must be h
28cab 65 6c 64 20 77 68 69 6c 65 20 61 63 63 65 73 73  eld while access
28cac 69 6e 67 20 74 68 69 73 20 6c 69 73 74 2e 0a 2a  ing this list..*
28cad 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
28cae 20 73 71 6c 69 74 65 33 41 75 74 6f 45 78 74 4c   sqlite3AutoExtL
28caf 69 73 74 20 73 71 6c 69 74 65 33 41 75 74 6f 45  ist sqlite3AutoE
28cb0 78 74 4c 69 73 74 3b 0a 73 74 61 74 69 63 20 53  xtList;.static S
28cb1 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74  QLITE_WSD struct
28cb2 20 73 71 6c 69 74 65 33 41 75 74 6f 45 78 74 4c   sqlite3AutoExtL
28cb3 69 73 74 20 7b 0a 20 20 69 6e 74 20 6e 45 78 74  ist {.  int nExt
28cb4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
28cb5 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
28cb6 69 65 73 20 69 6e 20 61 45 78 74 5b 5d 20 2a 2f  ies in aExt[] */
28cb7 20 20 20 20 20 20 20 20 20 20 0a 20 20 76 6f 69            .  voi
28cb8 64 20 28 2a 2a 61 45 78 74 29 28 76 6f 69 64 29  d (**aExt)(void)
28cb9 3b 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20  ;   /* Pointers 
28cba 74 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e  to the extension
28cbb 20 69 6e 69 74 20 66 75 6e 63 74 69 6f 6e 73 20   init functions 
28cbc 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 41 75 74 6f  */.} sqlite3Auto
28cbd 65 78 74 20 3d 20 7b 20 30 2c 20 30 20 7d 3b 0a  ext = { 0, 0 };.
28cbe 0a 2f 2a 20 54 68 65 20 22 77 73 64 41 75 74 6f  ./* The "wsdAuto
28cbf 65 78 74 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20  ext" macro will 
28cc0 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 61  resolve to the a
28cc1 75 74 6f 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20  utoextension.** 
28cc2 73 74 61 74 65 20 76 65 63 74 6f 72 2e 20 20 49  state vector.  I
28cc3 66 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69  f writable stati
28cc4 63 20 64 61 74 61 20 69 73 20 75 6e 73 75 70 70  c data is unsupp
28cc5 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74 61 72  orted on the tar
28cc6 67 65 74 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20  get,.** we have 
28cc7 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73 74  to locate the st
28cc8 61 74 65 20 76 65 63 74 6f 72 20 61 74 20 72 75  ate vector at ru
28cc9 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68 65 20  n-time.  In the 
28cca 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63  more common.** c
28ccb 61 73 65 20 77 68 65 72 65 20 77 72 69 74 61 62  ase where writab
28ccc 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69  le static data i
28ccd 73 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64  s supported, wsd
28cce 53 74 61 74 20 63 61 6e 20 72 65 66 65 72 20 64  Stat can refer d
28ccf 69 72 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68  irectly.** to th
28cd0 65 20 22 73 71 6c 69 74 65 33 41 75 74 6f 65 78  e "sqlite3Autoex
28cd1 74 22 20 73 74 61 74 65 20 76 65 63 74 6f 72 20  t" state vector 
28cd2 64 65 63 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a  declared above..
28cd3 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
28cd4 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 64 65 66 69  _OMIT_WSD.# defi
28cd5 6e 65 20 77 73 64 41 75 74 6f 65 78 74 49 6e 69  ne wsdAutoextIni
28cd6 74 20 5c 0a 20 20 73 71 6c 69 74 65 33 41 75 74  t \.  sqlite3Aut
28cd7 6f 45 78 74 4c 69 73 74 20 2a 78 20 3d 20 26 47  oExtList *x = &G
28cd8 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33 41 75 74  LOBAL(sqlite3Aut
28cd9 6f 45 78 74 4c 69 73 74 2c 73 71 6c 69 74 65 33  oExtList,sqlite3
28cda 41 75 74 6f 65 78 74 29 0a 23 20 64 65 66 69 6e  Autoext).# defin
28cdb 65 20 77 73 64 41 75 74 6f 65 78 74 20 78 5b 30  e wsdAutoext x[0
28cdc 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ].#else.# define
28cdd 20 77 73 64 41 75 74 6f 65 78 74 49 6e 69 74 0a   wsdAutoextInit.
28cde 23 20 64 65 66 69 6e 65 20 77 73 64 41 75 74 6f  # define wsdAuto
28cdf 65 78 74 20 73 71 6c 69 74 65 33 41 75 74 6f 65  ext sqlite3Autoe
28ce0 78 74 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  xt.#endif.../*.*
28ce1 2a 20 52 65 67 69 73 74 65 72 20 61 20 73 74 61  * Register a sta
28ce2 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 65 64 20 65  tically linked e
28ce3 78 74 65 6e 73 69 6f 6e 20 74 68 61 74 20 69 73  xtension that is
28ce4 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a   automatically.*
28ce5 2a 20 6c 6f 61 64 65 64 20 62 79 20 65 76 65 72  * loaded by ever
28ce6 79 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 63  y new database c
28ce7 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  onnection..*/.SQ
28ce8 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
28ce9 69 74 65 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73  ite3_auto_extens
28cea 69 6f 6e 28 76 6f 69 64 20 28 2a 78 49 6e 69 74  ion(void (*xInit
28ceb 29 28 76 6f 69 64 29 29 7b 0a 20 20 69 6e 74 20  )(void)){.  int 
28cec 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
28ced 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
28cee 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 72  MIT_AUTOINIT.  r
28cef 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  c = sqlite3_init
28cf0 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20  ialize();.  if( 
28cf1 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  rc ){.    return
28cf2 20 72 63 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e   rc;.  }else.#en
28cf3 64 69 66 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20  dif.  {.    int 
28cf4 69 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  i;.#if SQLITE_TH
28cf5 52 45 41 44 53 41 46 45 0a 20 20 20 20 73 71 6c  READSAFE.    sql
28cf6 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
28cf7 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78  x = sqlite3Mutex
28cf8 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
28cf9 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
28cfa 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 77 73  );.#endif.    ws
28cfb 64 41 75 74 6f 65 78 74 49 6e 69 74 3b 0a 20 20  dAutoextInit;.  
28cfc 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
28cfd 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20  enter(mutex);.  
28cfe 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 77 73 64    for(i=0; i<wsd
28cff 41 75 74 6f 65 78 74 2e 6e 45 78 74 3b 20 69 2b  Autoext.nExt; i+
28d00 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 77 73  +){.      if( ws
28d01 64 41 75 74 6f 65 78 74 2e 61 45 78 74 5b 69 5d  dAutoext.aExt[i]
28d02 3d 3d 78 49 6e 69 74 20 29 20 62 72 65 61 6b 3b  ==xInit ) break;
28d03 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
28d04 3d 3d 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78  ==wsdAutoext.nEx
28d05 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e  t ){.      int n
28d06 42 79 74 65 20 3d 20 28 77 73 64 41 75 74 6f 65  Byte = (wsdAutoe
28d07 78 74 2e 6e 45 78 74 2b 31 29 2a 73 69 7a 65 6f  xt.nExt+1)*sizeo
28d08 66 28 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78  f(wsdAutoext.aEx
28d09 74 5b 30 5d 29 3b 0a 20 20 20 20 20 20 76 6f 69  t[0]);.      voi
28d0a 64 20 28 2a 2a 61 4e 65 77 29 28 76 6f 69 64 29  d (**aNew)(void)
28d0b 3b 0a 20 20 20 20 20 20 61 4e 65 77 20 3d 20 73  ;.      aNew = s
28d0c 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 77  qlite3_realloc(w
28d0d 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74 2c 20  sdAutoext.aExt, 
28d0e 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 69 66  nByte);.      if
28d0f 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20  ( aNew==0 ){.   
28d10 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
28d11 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65  _NOMEM;.      }e
28d12 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 77 73 64  lse{.        wsd
28d13 41 75 74 6f 65 78 74 2e 61 45 78 74 20 3d 20 61  Autoext.aExt = a
28d14 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 77 73 64  New;.        wsd
28d15 41 75 74 6f 65 78 74 2e 61 45 78 74 5b 77 73 64  Autoext.aExt[wsd
28d16 41 75 74 6f 65 78 74 2e 6e 45 78 74 5d 20 3d 20  Autoext.nExt] = 
28d17 78 49 6e 69 74 3b 0a 20 20 20 20 20 20 20 20 77  xInit;.        w
28d18 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 2b 2b  sdAutoext.nExt++
28d19 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
28d1a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
28d1b 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a  x_leave(mutex);.
28d1c 20 20 20 20 61 73 73 65 72 74 28 20 28 72 63 26      assert( (rc&
28d1d 30 78 66 66 29 3d 3d 72 63 20 29 3b 0a 20 20 20  0xff)==rc );.   
28d1e 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
28d1f 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 74  }../*.** Reset t
28d20 68 65 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74  he automatic ext
28d21 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6d  ension loading m
28d22 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a 53 51 4c  echanism..*/.SQL
28d23 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
28d24 69 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f  ite3_reset_auto_
28d25 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 29 7b  extension(void){
28d26 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
28d27 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20  OMIT_AUTOINIT.  
28d28 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  if( sqlite3_init
28d29 69 61 6c 69 7a 65 28 29 3d 3d 53 51 4c 49 54 45  ialize()==SQLITE
28d2a 5f 4f 4b 20 29 0a 23 65 6e 64 69 66 0a 20 20 7b  _OK ).#endif.  {
28d2b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
28d2c 41 44 53 41 46 45 0a 20 20 20 20 73 71 6c 69 74  ADSAFE.    sqlit
28d2d 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20  e3_mutex *mutex 
28d2e 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  = sqlite3MutexAl
28d2f 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
28d30 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b  _STATIC_MASTER);
28d31 0a 23 65 6e 64 69 66 0a 20 20 20 20 77 73 64 41  .#endif.    wsdA
28d32 75 74 6f 65 78 74 49 6e 69 74 3b 0a 20 20 20 20  utoextInit;.    
28d33 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
28d34 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 20 20  ter(mutex);.    
28d35 73 71 6c 69 74 65 33 5f 66 72 65 65 28 77 73 64  sqlite3_free(wsd
28d36 41 75 74 6f 65 78 74 2e 61 45 78 74 29 3b 0a 20  Autoext.aExt);. 
28d37 20 20 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45     wsdAutoext.aE
28d38 78 74 20 3d 20 30 3b 0a 20 20 20 20 77 73 64 41  xt = 0;.    wsdA
28d39 75 74 6f 65 78 74 2e 6e 45 78 74 20 3d 20 30 3b  utoext.nExt = 0;
28d3a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
28d3b 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b  ex_leave(mutex);
28d3c 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  .  }.}../*.** Lo
28d3d 61 64 20 61 6c 6c 20 61 75 74 6f 6d 61 74 69 63  ad all automatic
28d3e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a   extensions..**.
28d3f 2a 2a 20 49 66 20 61 6e 79 74 68 69 6e 67 20 67  ** If anything g
28d40 6f 65 73 20 77 72 6f 6e 67 2c 20 73 65 74 20 61  oes wrong, set a
28d41 6e 20 65 72 72 6f 72 20 69 6e 20 74 68 65 20 64  n error in the d
28d42 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
28d43 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
28d44 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
28d45 65 33 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73  e3AutoLoadExtens
28d46 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62  ions(sqlite3 *db
28d47 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
28d48 74 20 67 6f 20 3d 20 31 3b 0a 20 20 69 6e 74 20  t go = 1;.  int 
28d49 28 2a 78 49 6e 69 74 29 28 73 71 6c 69 74 65 33  (*xInit)(sqlite3
28d4a 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73 74 20 73  *,char**,const s
28d4b 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69  qlite3_api_routi
28d4c 6e 65 73 2a 29 3b 0a 0a 20 20 77 73 64 41 75 74  nes*);..  wsdAut
28d4d 6f 65 78 74 49 6e 69 74 3b 0a 20 20 69 66 28 20  oextInit;.  if( 
28d4e 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 3d  wsdAutoext.nExt=
28d4f 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 6d  =0 ){.    /* Com
28d50 6d 6f 6e 20 63 61 73 65 3a 20 65 61 72 6c 79 20  mon case: early 
28d51 6f 75 74 20 77 69 74 68 6f 75 74 20 65 76 65 72  out without ever
28d52 79 20 68 61 76 69 6e 67 20 74 6f 20 61 63 71 75  y having to acqu
28d53 69 72 65 20 61 20 6d 75 74 65 78 20 2a 2f 0a 20  ire a mutex */. 
28d54 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
28d55 20 66 6f 72 28 69 3d 30 3b 20 67 6f 3b 20 69 2b   for(i=0; go; i+
28d56 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45  +){.    char *zE
28d57 72 72 6d 73 67 3b 0a 23 69 66 20 53 51 4c 49 54  rrmsg;.#if SQLIT
28d58 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 20  E_THREADSAFE.   
28d59 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
28d5a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d  mutex = sqlite3M
28d5b 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
28d5c 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
28d5d 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20  STER);.#endif.  
28d5e 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
28d5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20  enter(mutex);.  
28d60 20 20 69 66 28 20 69 3e 3d 77 73 64 41 75 74 6f    if( i>=wsdAuto
28d61 65 78 74 2e 6e 45 78 74 20 29 7b 0a 20 20 20 20  ext.nExt ){.    
28d62 20 20 78 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20    xInit = 0;.   
28d63 20 20 20 67 6f 20 3d 20 30 3b 0a 20 20 20 20 7d     go = 0;.    }
28d64 65 6c 73 65 7b 0a 20 20 20 20 20 20 78 49 6e 69  else{.      xIni
28d65 74 20 3d 20 28 69 6e 74 28 2a 29 28 73 71 6c 69  t = (int(*)(sqli
28d66 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73  te3*,char**,cons
28d67 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f  t sqlite3_api_ro
28d68 75 74 69 6e 65 73 2a 29 29 0a 20 20 20 20 20 20  utines*)).      
28d69 20 20 20 20 20 20 20 20 77 73 64 41 75 74 6f 65          wsdAutoe
28d6a 78 74 2e 61 45 78 74 5b 69 5d 3b 0a 20 20 20 20  xt.aExt[i];.    
28d6b 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  }.    sqlite3_mu
28d6c 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29  tex_leave(mutex)
28d6d 3b 0a 20 20 20 20 7a 45 72 72 6d 73 67 20 3d 20  ;.    zErrmsg = 
28d6e 30 3b 0a 20 20 20 20 69 66 28 20 78 49 6e 69 74  0;.    if( xInit
28d6f 20 26 26 20 78 49 6e 69 74 28 64 62 2c 20 26 7a   && xInit(db, &z
28d70 45 72 72 6d 73 67 2c 20 26 73 71 6c 69 74 65 33  Errmsg, &sqlite3
28d71 41 70 69 73 29 20 29 7b 0a 20 20 20 20 20 20 73  Apis) ){.      s
28d72 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
28d73 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 0a 20 20  SQLITE_ERROR,.  
28d74 20 20 20 20 20 20 20 20 20 20 22 61 75 74 6f 6d            "autom
28d75 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 20 6c  atic extension l
28d76 6f 61 64 69 6e 67 20 66 61 69 6c 65 64 3a 20 25  oading failed: %
28d77 73 22 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20  s", zErrmsg);.  
28d78 20 20 20 20 67 6f 20 3d 20 30 3b 0a 20 20 20 20      go = 0;.    
28d79 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  }.    sqlite3_fr
28d7a 65 65 28 7a 45 72 72 6d 73 67 29 3b 0a 20 20 7d  ee(zErrmsg);.  }
28d7b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
28d7c 2a 2a 2a 20 45 6e 64 20 6f 66 20 6c 6f 61 64 65  *** End of loade
28d7d 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  xt.c ***********
28d7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28d7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28d80 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
28d81 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70  *** Begin file p
28d82 72 61 67 6d 61 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  ragma.c ********
28d83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28d84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28d85 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 41  **/./*.** 2003 A
28d86 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65  pril 6.**.** The
28d87 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
28d88 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
28d89 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
28d8a 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
28d8b 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
28d8c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
28d8d 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
28d8e 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
28d8f 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
28d90 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
28d91 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
28d92 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
28d93 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
28d94 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
28d95 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
28d96 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
28d97 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
28d98 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28d99 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28d9a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28d9b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28d9c 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
28d9d 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
28d9e 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
28d9f 6e 74 20 74 68 65 20 50 52 41 47 4d 41 20 63 6f  nt the PRAGMA co
28da0 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  mmand..**.** $Id
28da1 3a 20 70 72 61 67 6d 61 2e 63 2c 76 20 31 2e 32  : pragma.c,v 1.2
28da2 31 34 20 32 30 30 39 2f 30 37 2f 30 32 20 30 37  14 2009/07/02 07
28da3 3a 34 37 3a 33 33 20 64 61 6e 69 65 6c 6b 31 39  :47:33 danielk19
28da4 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20  77 Exp $.*/../* 
28da5 49 67 6e 6f 72 65 20 74 68 69 73 20 77 68 6f 6c  Ignore this whol
28da6 65 20 66 69 6c 65 20 69 66 20 70 72 61 67 6d 61  e file if pragma
28da7 73 20 61 72 65 20 64 69 73 61 62 6c 65 64 0a 2a  s are disabled.*
28da8 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
28da9 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 41 47 4d  QLITE_OMIT_PRAGM
28daa 41 29 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70  A)../*.** Interp
28dab 72 65 74 20 74 68 65 20 67 69 76 65 6e 20 73 74  ret the given st
28dac 72 69 6e 67 20 61 73 20 61 20 73 61 66 65 74 79  ring as a safety
28dad 20 6c 65 76 65 6c 2e 20 20 52 65 74 75 72 6e 20   level.  Return 
28dae 30 20 66 6f 72 20 4f 46 46 2c 0a 2a 2a 20 31 20  0 for OFF,.** 1 
28daf 66 6f 72 20 4f 4e 20 6f 72 20 4e 4f 52 4d 41 4c  for ON or NORMAL
28db0 20 61 6e 64 20 32 20 66 6f 72 20 46 55 4c 4c 2e   and 2 for FULL.
28db1 20 20 52 65 74 75 72 6e 20 31 20 66 6f 72 20 61    Return 1 for a
28db2 6e 20 65 6d 70 74 79 20 6f 72 20 0a 2a 2a 20 75  n empty or .** u
28db3 6e 72 65 63 6f 67 6e 69 7a 65 64 20 73 74 72 69  nrecognized stri
28db4 6e 67 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a  ng argument..**.
28db5 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
28db6 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64   values returned
28db7 20 61 72 65 20 6f 6e 65 20 6c 65 73 73 20 74 68   are one less th
28db8 61 74 20 74 68 65 20 76 61 6c 75 65 73 20 74 68  at the values th
28db9 61 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20  at.** should be 
28dba 70 61 73 73 65 64 20 69 6e 74 6f 20 73 71 6c 69  passed into sqli
28dbb 74 65 33 42 74 72 65 65 53 65 74 53 61 66 65 74  te3BtreeSetSafet
28dbc 79 4c 65 76 65 6c 28 29 2e 20 20 54 68 65 20 69  yLevel().  The i
28dbd 73 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20 73 75 70  s done.** to sup
28dbe 70 6f 72 74 20 6c 65 67 61 63 79 20 53 51 4c 20  port legacy SQL 
28dbf 63 6f 64 65 2e 20 20 54 68 65 20 73 61 66 65 74  code.  The safet
28dc0 79 20 6c 65 76 65 6c 20 75 73 65 64 20 74 6f 20  y level used to 
28dc1 62 65 20 62 6f 6f 6c 65 61 6e 0a 2a 2a 20 61 6e  be boolean.** an
28dc2 64 20 6f 6c 64 65 72 20 73 63 72 69 70 74 73 20  d older scripts 
28dc3 6d 61 79 20 68 61 76 65 20 75 73 65 64 20 6e 75  may have used nu
28dc4 6d 62 65 72 73 20 30 20 66 6f 72 20 4f 46 46 20  mbers 0 for OFF 
28dc5 61 6e 64 20 31 20 66 6f 72 20 4f 4e 2e 0a 2a 2f  and 1 for ON..*/
28dc6 0a 73 74 61 74 69 63 20 75 38 20 67 65 74 53 61  .static u8 getSa
28dc7 66 65 74 79 4c 65 76 65 6c 28 63 6f 6e 73 74 20  fetyLevel(const 
28dc8 63 68 61 72 20 2a 7a 29 7b 0a 20 20 20 20 20 20  char *z){.      
28dc9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28dca 20 20 20 20 20 20 20 2f 2a 20 31 32 33 34 35 36         /* 123456
28dcb 37 38 39 20 31 32 33 34 35 36 37 38 39 20 2a 2f  789 123456789 */
28dcc 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
28dcd 63 68 61 72 20 7a 54 65 78 74 5b 5d 20 3d 20 22  char zText[] = "
28dce 6f 6e 6f 66 66 61 6c 73 65 79 65 73 74 72 75 65  onoffalseyestrue
28dcf 66 75 6c 6c 22 3b 0a 20 20 73 74 61 74 69 63 20  full";.  static 
28dd0 63 6f 6e 73 74 20 75 38 20 69 4f 66 66 73 65 74  const u8 iOffset
28dd1 5b 5d 20 3d 20 7b 30 2c 20 31 2c 20 32 2c 20 34  [] = {0, 1, 2, 4
28dd2 2c 20 39 2c 20 31 32 2c 20 31 36 7d 3b 0a 20 20  , 9, 12, 16};.  
28dd3 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20  static const u8 
28dd4 69 4c 65 6e 67 74 68 5b 5d 20 3d 20 7b 32 2c 20  iLength[] = {2, 
28dd5 32 2c 20 33 2c 20 35 2c 20 33 2c 20 34 2c 20 34  2, 3, 5, 3, 4, 4
28dd6 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  };.  static cons
28dd7 74 20 75 38 20 69 56 61 6c 75 65 5b 5d 20 3d 20  t u8 iValue[] = 
28dd8 20 7b 31 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c   {1, 0, 0, 0, 1,
28dd9 20 31 2c 20 32 7d 3b 0a 20 20 69 6e 74 20 69 2c   1, 2};.  int i,
28dda 20 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65   n;.  if( sqlite
28ddb 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 0a  3Isdigit(*z) ){.
28ddc 20 20 20 20 72 65 74 75 72 6e 20 28 75 38 29 61      return (u8)a
28ddd 74 6f 69 28 7a 29 3b 0a 20 20 7d 0a 20 20 6e 20  toi(z);.  }.  n 
28dde 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
28ddf 30 28 7a 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  0(z);.  for(i=0;
28de0 20 69 3c 41 72 72 61 79 53 69 7a 65 28 69 4c 65   i<ArraySize(iLe
28de1 6e 67 74 68 29 3b 20 69 2b 2b 29 7b 0a 20 20 20  ngth); i++){.   
28de2 20 69 66 28 20 69 4c 65 6e 67 74 68 5b 69 5d 3d   if( iLength[i]=
28de3 3d 6e 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  =n && sqlite3Str
28de4 4e 49 43 6d 70 28 26 7a 54 65 78 74 5b 69 4f 66  NICmp(&zText[iOf
28de5 66 73 65 74 5b 69 5d 5d 2c 7a 2c 6e 29 3d 3d 30  fset[i]],z,n)==0
28de6 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
28de7 20 69 56 61 6c 75 65 5b 69 5d 3b 0a 20 20 20 20   iValue[i];.    
28de8 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31  }.  }.  return 1
28de9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72  ;.}../*.** Inter
28dea 70 72 65 74 20 74 68 65 20 67 69 76 65 6e 20 73  pret the given s
28deb 74 72 69 6e 67 20 61 73 20 61 20 62 6f 6f 6c 65  tring as a boole
28dec 61 6e 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61  an value..*/.sta
28ded 74 69 63 20 75 38 20 67 65 74 42 6f 6f 6c 65 61  tic u8 getBoolea
28dee 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  n(const char *z)
28def 7b 0a 20 20 72 65 74 75 72 6e 20 67 65 74 53 61  {.  return getSa
28df0 66 65 74 79 4c 65 76 65 6c 28 7a 29 26 31 3b 0a  fetyLevel(z)&1;.
28df1 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72  }../*.** Interpr
28df2 65 74 20 74 68 65 20 67 69 76 65 6e 20 73 74 72  et the given str
28df3 69 6e 67 20 61 73 20 61 20 6c 6f 63 6b 69 6e 67  ing as a locking
28df4 20 6d 6f 64 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a   mode value..*/.
28df5 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 4c 6f  static int getLo
28df6 63 6b 69 6e 67 4d 6f 64 65 28 63 6f 6e 73 74 20  ckingMode(const 
28df7 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66 28 20  char *z){.  if( 
28df8 7a 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d  z ){.    if( 0==
28df9 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
28dfa 2c 20 22 65 78 63 6c 75 73 69 76 65 22 29 20 29  , "exclusive") )
28dfb 20 72 65 74 75 72 6e 20 50 41 47 45 52 5f 4c 4f   return PAGER_LO
28dfc 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53  CKINGMODE_EXCLUS
28dfd 49 56 45 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d  IVE;.    if( 0==
28dfe 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
28dff 2c 20 22 6e 6f 72 6d 61 6c 22 29 20 29 20 72 65  , "normal") ) re
28e00 74 75 72 6e 20 50 41 47 45 52 5f 4c 4f 43 4b 49  turn PAGER_LOCKI
28e01 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3b 0a 20  NGMODE_NORMAL;. 
28e02 20 7d 0a 20 20 72 65 74 75 72 6e 20 50 41 47 45   }.  return PAGE
28e03 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55  R_LOCKINGMODE_QU
28e04 45 52 59 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  ERY;.}..#ifndef 
28e05 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
28e06 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 6e 74  VACUUM./*.** Int
28e07 65 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e  erpret the given
28e08 20 73 74 72 69 6e 67 20 61 73 20 61 6e 20 61 75   string as an au
28e09 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 76  to-vacuum mode v
28e0a 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  alue..**.** The 
28e0b 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 69 6e 67  following string
28e0c 73 2c 20 22 6e 6f 6e 65 22 2c 20 22 66 75 6c 6c  s, "none", "full
28e0d 22 20 61 6e 64 20 22 69 6e 63 72 65 6d 65 6e 74  " and "increment
28e0e 61 6c 22 20 61 72 65 20 0a 2a 2a 20 61 63 63 65  al" are .** acce
28e0f 70 74 61 62 6c 65 2c 20 61 73 20 61 72 65 20 74  ptable, as are t
28e10 68 65 69 72 20 6e 75 6d 65 72 69 63 20 65 71 75  heir numeric equ
28e11 69 76 61 6c 65 6e 74 73 3a 20 30 2c 20 31 20 61  ivalents: 0, 1 a
28e12 6e 64 20 32 20 72 65 73 70 65 63 74 69 76 65 6c  nd 2 respectivel
28e13 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  y..*/.static int
28e14 20 67 65 74 41 75 74 6f 56 61 63 75 75 6d 28 63   getAutoVacuum(c
28e15 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
28e16 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 30 3d   int i;.  if( 0=
28e17 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28  =sqlite3StrICmp(
28e18 7a 2c 20 22 6e 6f 6e 65 22 29 20 29 20 72 65 74  z, "none") ) ret
28e19 75 72 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41  urn BTREE_AUTOVA
28e1a 43 55 55 4d 5f 4e 4f 4e 45 3b 0a 20 20 69 66 28  CUUM_NONE;.  if(
28e1b 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43   0==sqlite3StrIC
28e1c 6d 70 28 7a 2c 20 22 66 75 6c 6c 22 29 20 29 20  mp(z, "full") ) 
28e1d 72 65 74 75 72 6e 20 42 54 52 45 45 5f 41 55 54  return BTREE_AUT
28e1e 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 3b 0a 20 20  OVACUUM_FULL;.  
28e1f 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74  if( 0==sqlite3St
28e20 72 49 43 6d 70 28 7a 2c 20 22 69 6e 63 72 65 6d  rICmp(z, "increm
28e21 65 6e 74 61 6c 22 29 20 29 20 72 65 74 75 72 6e  ental") ) return
28e22 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55   BTREE_AUTOVACUU
28e23 4d 5f 49 4e 43 52 3b 0a 20 20 69 20 3d 20 61 74  M_INCR;.  i = at
28e24 6f 69 28 7a 29 3b 0a 20 20 72 65 74 75 72 6e 20  oi(z);.  return 
28e25 28 75 38 29 28 28 69 3e 3d 30 26 26 69 3c 3d 32  (u8)((i>=0&&i<=2
28e26 29 3f 69 3a 30 29 3b 0a 7d 0a 23 65 6e 64 69 66  )?i:0);.}.#endif
28e27 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54   /* ifndef SQLIT
28e28 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
28e29 4d 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  M */..#ifndef SQ
28e2a 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
28e2b 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 6e  PRAGMAS./*.** In
28e2c 74 65 72 70 72 65 74 20 74 68 65 20 67 69 76 65  terpret the give
28e2d 6e 20 73 74 72 69 6e 67 20 61 73 20 61 20 74 65  n string as a te
28e2e 6d 70 20 64 62 20 6c 6f 63 61 74 69 6f 6e 2e 20  mp db location. 
28e2f 52 65 74 75 72 6e 20 31 20 66 6f 72 20 66 69 6c  Return 1 for fil
28e30 65 0a 2a 2a 20 62 61 63 6b 65 64 20 74 65 6d 70  e.** backed temp
28e31 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 73 2c  orary databases,
28e32 20 32 20 66 6f 72 20 74 68 65 20 52 65 64 2d 42   2 for the Red-B
28e33 6c 61 63 6b 20 74 72 65 65 20 69 6e 20 6d 65 6d  lack tree in mem
28e34 6f 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ory database.** 
28e35 61 6e 64 20 30 20 74 6f 20 75 73 65 20 74 68 65  and 0 to use the
28e36 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 64 65   compile-time de
28e37 66 61 75 6c 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  fault..*/.static
28e38 20 69 6e 74 20 67 65 74 54 65 6d 70 53 74 6f 72   int getTempStor
28e39 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  e(const char *z)
28e3a 7b 0a 20 20 69 66 28 20 7a 5b 30 5d 3e 3d 27 30  {.  if( z[0]>='0
28e3b 27 20 26 26 20 7a 5b 30 5d 3c 3d 27 32 27 20 29  ' && z[0]<='2' )
28e3c 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 5b 30  {.    return z[0
28e3d 5d 20 2d 20 27 30 27 3b 0a 20 20 7d 65 6c 73 65  ] - '0';.  }else
28e3e 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
28e3f 43 6d 70 28 7a 2c 20 22 66 69 6c 65 22 29 3d 3d  Cmp(z, "file")==
28e40 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
28e41 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  1;.  }else if( s
28e42 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c  qlite3StrICmp(z,
28e43 20 22 6d 65 6d 6f 72 79 22 29 3d 3d 30 20 29 7b   "memory")==0 ){
28e44 0a 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20  .    return 2;. 
28e45 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
28e46 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  rn 0;.  }.}.#end
28e47 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 50 41 47  if /* SQLITE_PAG
28e48 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23  ER_PRAGMAS */..#
28e49 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
28e4a 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53  IT_PAGER_PRAGMAS
28e4b 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74  ./*.** Invalidat
28e4c 65 20 74 65 6d 70 20 73 74 6f 72 61 67 65 2c 20  e temp storage, 
28e4d 65 69 74 68 65 72 20 77 68 65 6e 20 74 68 65 20  either when the 
28e4e 74 65 6d 70 20 73 74 6f 72 61 67 65 20 69 73 20  temp storage is 
28e4f 63 68 61 6e 67 65 64 0a 2a 2a 20 66 72 6f 6d 20  changed.** from 
28e50 64 65 66 61 75 6c 74 2c 20 6f 72 20 77 68 65 6e  default, or when
28e51 20 27 66 69 6c 65 27 20 61 6e 64 20 74 68 65 20   'file' and the 
28e52 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63  temp_store_direc
28e53 74 6f 72 79 20 68 61 73 20 63 68 61 6e 67 65 64  tory has changed
28e54 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
28e55 6e 76 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f  nvalidateTempSto
28e56 72 61 67 65 28 50 61 72 73 65 20 2a 70 50 61 72  rage(Parse *pPar
28e57 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  se){.  sqlite3 *
28e58 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
28e59 0a 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 31  .  if( db->aDb[1
28e5a 5d 2e 70 42 74 21 3d 30 20 29 7b 0a 20 20 20 20  ].pBt!=0 ){.    
28e5b 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d  if( !db->autoCom
28e5c 6d 69 74 20 7c 7c 20 73 71 6c 69 74 65 33 42 74  mit || sqlite3Bt
28e5d 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73  reeIsInReadTrans
28e5e 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29  (db->aDb[1].pBt)
28e5f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
28e60 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
28e61 2c 20 22 74 65 6d 70 6f 72 61 72 79 20 73 74 6f  , "temporary sto
28e62 72 61 67 65 20 63 61 6e 6e 6f 74 20 62 65 20 63  rage cannot be c
28e63 68 61 6e 67 65 64 20 22 0a 20 20 20 20 20 20 20  hanged ".       
28e64 20 22 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 20   "from within a 
28e65 74 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20  transaction");. 
28e66 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
28e67 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a  TE_ERROR;.    }.
28e68 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
28e69 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62 5b 31 5d  Close(db->aDb[1]
28e6a 2e 70 42 74 29 3b 0a 20 20 20 20 64 62 2d 3e 61  .pBt);.    db->a
28e6b 44 62 5b 31 5d 2e 70 42 74 20 3d 20 30 3b 0a 20  Db[1].pBt = 0;. 
28e6c 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49     sqlite3ResetI
28e6d 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62  nternalSchema(db
28e6e 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , 0);.  }.  retu
28e6f 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
28e70 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
28e71 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a  _PAGER_PRAGMAS *
28e72 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
28e73 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
28e74 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  GMAS./*.** If th
28e75 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 20  e TEMP database 
28e76 69 73 20 6f 70 65 6e 2c 20 63 6c 6f 73 65 20 69  is open, close i
28e77 74 20 61 6e 64 20 6d 61 72 6b 20 74 68 65 20 64  t and mark the d
28e78 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 0a 2a  atabase schema.*
28e79 2a 20 61 73 20 6e 65 65 64 69 6e 67 20 72 65 6c  * as needing rel
28e7a 6f 61 64 69 6e 67 2e 20 20 54 68 69 73 20 6d 75  oading.  This mu
28e7b 73 74 20 62 65 20 64 6f 6e 65 20 77 68 65 6e 20  st be done when 
28e7c 75 73 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45  using the SQLITE
28e7d 5f 54 45 4d 50 5f 53 54 4f 52 45 0a 2a 2a 20 6f  _TEMP_STORE.** o
28e7e 72 20 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 53  r DEFAULT_TEMP_S
28e7f 54 4f 52 45 20 70 72 61 67 6d 61 73 2e 0a 2a 2f  TORE pragmas..*/
28e80 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 61 6e  .static int chan
28e81 67 65 54 65 6d 70 53 74 6f 72 61 67 65 28 50 61  geTempStorage(Pa
28e82 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e  rse *pParse, con
28e83 73 74 20 63 68 61 72 20 2a 7a 53 74 6f 72 61 67  st char *zStorag
28e84 65 54 79 70 65 29 7b 0a 20 20 69 6e 74 20 74 73  eType){.  int ts
28e85 20 3d 20 67 65 74 54 65 6d 70 53 74 6f 72 65 28   = getTempStore(
28e86 7a 53 74 6f 72 61 67 65 54 79 70 65 29 3b 0a 20  zStorageType);. 
28e87 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
28e88 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
28e89 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3d   db->temp_store=
28e8a 3d 74 73 20 29 20 72 65 74 75 72 6e 20 53 51 4c  =ts ) return SQL
28e8b 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 6e  ITE_OK;.  if( in
28e8c 76 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72  validateTempStor
28e8d 61 67 65 28 20 70 50 61 72 73 65 20 29 20 21 3d  age( pParse ) !=
28e8e 20 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20   SQLITE_OK ){.  
28e8f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
28e90 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 64 62 2d  ERROR;.  }.  db-
28e91 3e 74 65 6d 70 5f 73 74 6f 72 65 20 3d 20 28 75  >temp_store = (u
28e92 38 29 74 73 3b 0a 20 20 72 65 74 75 72 6e 20 53  8)ts;.  return S
28e93 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64  QLITE_OK;.}.#end
28e94 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 50 41 47  if /* SQLITE_PAG
28e95 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 2f  ER_PRAGMAS */../
28e96 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
28e97 64 65 20 74 6f 20 72 65 74 75 72 6e 20 61 20 73  de to return a s
28e98 69 6e 67 6c 65 20 69 6e 74 65 67 65 72 20 76 61  ingle integer va
28e99 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  lue..*/.static v
28e9a 6f 69 64 20 72 65 74 75 72 6e 53 69 6e 67 6c 65  oid returnSingle
28e9b 49 6e 74 28 50 61 72 73 65 20 2a 70 50 61 72 73  Int(Parse *pPars
28e9c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  e, const char *z
28e9d 4c 61 62 65 6c 2c 20 69 36 34 20 76 61 6c 75 65  Label, i64 value
28e9e 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73  ){.  Vdbe *v = s
28e9f 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
28ea0 61 72 73 65 29 3b 0a 20 20 69 6e 74 20 6d 65 6d  arse);.  int mem
28ea1 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
28ea2 6d 3b 0a 20 20 69 36 34 20 2a 70 49 36 34 20 3d  m;.  i64 *pI64 =
28ea3 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
28ea4 52 61 77 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  Raw(pParse->db, 
28ea5 73 69 7a 65 6f 66 28 76 61 6c 75 65 29 29 3b 0a  sizeof(value));.
28ea6 20 20 69 66 28 20 70 49 36 34 20 29 7b 0a 20 20    if( pI64 ){.  
28ea7 20 20 6d 65 6d 63 70 79 28 70 49 36 34 2c 20 26    memcpy(pI64, &
28ea8 76 61 6c 75 65 2c 20 73 69 7a 65 6f 66 28 76 61  value, sizeof(va
28ea9 6c 75 65 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  lue));.  }.  sql
28eaa 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
28eab 2c 20 4f 50 5f 49 6e 74 36 34 2c 20 30 2c 20 6d  , OP_Int64, 0, m
28eac 65 6d 2c 20 30 2c 20 28 63 68 61 72 2a 29 70 49  em, 0, (char*)pI
28ead 36 34 2c 20 50 34 5f 49 4e 54 36 34 29 3b 0a 20  64, P4_INT64);. 
28eae 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
28eaf 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20  umCols(v, 1);.  
28eb0 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
28eb1 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
28eb2 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4c 61 62 65 6c  AME_NAME, zLabel
28eb3 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
28eb4 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
28eb5 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
28eb6 6c 74 52 6f 77 2c 20 6d 65 6d 2c 20 31 29 3b 0a  ltRow, mem, 1);.
28eb7 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
28eb8 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47  E_OMIT_FLAG_PRAG
28eb9 4d 41 53 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  MAS./*.** Check 
28eba 74 6f 20 73 65 65 20 69 66 20 7a 52 69 67 68 74  to see if zRight
28ebb 20 61 6e 64 20 7a 4c 65 66 74 20 72 65 66 65 72   and zLeft refer
28ebc 20 74 6f 20 61 20 70 72 61 67 6d 61 20 74 68 61   to a pragma tha
28ebd 74 20 71 75 65 72 69 65 73 0a 2a 2a 20 6f 72 20  t queries.** or 
28ebe 63 68 61 6e 67 65 73 20 6f 6e 65 20 6f 66 20 74  changes one of t
28ebf 68 65 20 66 6c 61 67 73 20 69 6e 20 64 62 2d 3e  he flags in db->
28ec0 66 6c 61 67 73 2e 20 20 52 65 74 75 72 6e 20 31  flags.  Return 1
28ec1 20 69 66 20 73 6f 20 61 6e 64 20 30 20 69 66 20   if so and 0 if 
28ec2 6e 6f 74 2e 0a 2a 2a 20 41 6c 73 6f 2c 20 69 6d  not..** Also, im
28ec3 70 6c 65 6d 65 6e 74 20 74 68 65 20 70 72 61 67  plement the prag
28ec4 6d 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ma..*/.static in
28ec5 74 20 66 6c 61 67 50 72 61 67 6d 61 28 50 61 72  t flagPragma(Par
28ec6 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73  se *pParse, cons
28ec7 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63  t char *zLeft, c
28ec8 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 69 67 68  onst char *zRigh
28ec9 74 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  t){.  static con
28eca 73 74 20 73 74 72 75 63 74 20 73 50 72 61 67 6d  st struct sPragm
28ecb 61 54 79 70 65 20 7b 0a 20 20 20 20 63 6f 6e 73  aType {.    cons
28ecc 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20  t char *zName;  
28ecd 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 70  /* Name of the p
28ece 72 61 67 6d 61 20 2a 2f 0a 20 20 20 20 69 6e 74  ragma */.    int
28ecf 20 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20   mask;          
28ed0 20 2f 2a 20 4d 61 73 6b 20 66 6f 72 20 74 68 65   /* Mask for the
28ed1 20 64 62 2d 3e 66 6c 61 67 73 20 76 61 6c 75 65   db->flags value
28ed2 20 2a 2f 0a 20 20 7d 20 61 50 72 61 67 6d 61 5b   */.  } aPragma[
28ed3 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 66 75 6c  ] = {.    { "ful
28ed4 6c 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 73 22 2c  l_column_names",
28ed5 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 46          SQLITE_F
28ed6 75 6c 6c 43 6f 6c 4e 61 6d 65 73 20 20 7d 2c 0a  ullColNames  },.
28ed7 20 20 20 20 7b 20 22 73 68 6f 72 74 5f 63 6f 6c      { "short_col
28ed8 75 6d 6e 5f 6e 61 6d 65 73 22 2c 20 20 20 20 20  umn_names",     
28ed9 20 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f    SQLITE_ShortCo
28eda 6c 4e 61 6d 65 73 20 7d 2c 0a 20 20 20 20 7b 20  lNames },.    { 
28edb 22 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 22 2c  "count_changes",
28edc 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
28edd 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20 20  TE_CountRows    
28ede 20 7d 2c 0a 20 20 20 20 7b 20 22 65 6d 70 74 79   },.    { "empty
28edf 5f 72 65 73 75 6c 74 5f 63 61 6c 6c 62 61 63 6b  _result_callback
28ee0 73 22 2c 20 20 20 53 51 4c 49 54 45 5f 4e 75 6c  s",   SQLITE_Nul
28ee1 6c 43 61 6c 6c 62 61 63 6b 20 20 7d 2c 0a 20 20  lCallback  },.  
28ee2 20 20 7b 20 22 6c 65 67 61 63 79 5f 66 69 6c 65    { "legacy_file
28ee3 5f 66 6f 72 6d 61 74 22 2c 20 20 20 20 20 20 20  _format",       
28ee4 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c  SQLITE_LegacyFil
28ee5 65 46 6d 74 20 7d 2c 0a 20 20 20 20 7b 20 22 66  eFmt },.    { "f
28ee6 75 6c 6c 66 73 79 6e 63 22 2c 20 20 20 20 20 20  ullfsync",      
28ee7 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
28ee8 5f 46 75 6c 6c 46 53 79 6e 63 20 20 20 20 20 7d  _FullFSync     }
28ee9 2c 0a 20 20 20 20 7b 20 22 72 65 76 65 72 73 65  ,.    { "reverse
28eea 5f 75 6e 6f 72 64 65 72 65 64 5f 73 65 6c 65 63  _unordered_selec
28eeb 74 73 22 2c 20 53 51 4c 49 54 45 5f 52 65 76 65  ts", SQLITE_Reve
28eec 72 73 65 4f 72 64 65 72 20 20 7d 2c 0a 23 69 66  rseOrder  },.#if
28eed 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
28eee 0a 20 20 20 20 7b 20 22 73 71 6c 5f 74 72 61 63  .    { "sql_trac
28eef 65 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  e",             
28ef0 20 20 20 53 51 4c 49 54 45 5f 53 71 6c 54 72 61     SQLITE_SqlTra
28ef1 63 65 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b  ce      },.    {
28ef2 20 22 76 64 62 65 5f 6c 69 73 74 69 6e 67 22 2c   "vdbe_listing",
28ef3 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
28ef4 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 20  ITE_VdbeListing 
28ef5 20 20 7d 2c 0a 20 20 20 20 7b 20 22 76 64 62 65    },.    { "vdbe
28ef6 5f 74 72 61 63 65 22 2c 20 20 20 20 20 20 20 20  _trace",        
28ef7 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 56 64         SQLITE_Vd
28ef8 62 65 54 72 61 63 65 20 20 20 20 20 7d 2c 0a 23  beTrace     },.#
28ef9 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51  endif.#ifndef SQ
28efa 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a  LITE_OMIT_CHECK.
28efb 20 20 20 20 7b 20 22 69 67 6e 6f 72 65 5f 63 68      { "ignore_ch
28efc 65 63 6b 5f 63 6f 6e 73 74 72 61 69 6e 74 73 22  eck_constraints"
28efd 2c 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43  , SQLITE_IgnoreC
28efe 68 65 63 6b 73 20 20 7d 2c 0a 23 65 6e 64 69 66  hecks  },.#endif
28eff 0a 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  .    /* The foll
28f00 6f 77 69 6e 67 20 69 73 20 56 45 52 59 20 65 78  owing is VERY ex
28f01 70 65 72 69 6d 65 6e 74 61 6c 20 2a 2f 0a 20 20  perimental */.  
28f02 20 20 7b 20 22 77 72 69 74 61 62 6c 65 5f 73 63    { "writable_sc
28f03 68 65 6d 61 22 2c 20 20 20 20 20 20 20 20 20 20  hema",          
28f04 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65  SQLITE_WriteSche
28f05 6d 61 7c 53 51 4c 49 54 45 5f 52 65 63 6f 76 65  ma|SQLITE_Recove
28f06 72 79 4d 6f 64 65 20 7d 2c 0a 20 20 20 20 7b 20  ryMode },.    { 
28f07 22 6f 6d 69 74 5f 72 65 61 64 6c 6f 63 6b 22 2c  "omit_readlock",
28f08 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
28f09 54 45 5f 4e 6f 52 65 61 64 6c 6f 63 6b 20 20 20  TE_NoReadlock   
28f0a 20 7d 2c 0a 0a 20 20 20 20 2f 2a 20 54 4f 44 4f   },..    /* TODO
28f0b 3a 20 4d 61 79 62 65 20 69 74 20 73 68 6f 75 6c  : Maybe it shoul
28f0c 64 6e 27 74 20 62 65 20 70 6f 73 73 69 62 6c 65  dn't be possible
28f0d 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 52   to change the R
28f0e 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 0a 20  eadUncommitted. 
28f0f 20 20 20 2a 2a 20 66 6c 61 67 20 69 66 20 74 68     ** flag if th
28f10 65 72 65 20 61 72 65 20 61 6e 79 20 61 63 74 69  ere are any acti
28f11 76 65 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 2a  ve statements. *
28f12 2f 0a 20 20 20 20 7b 20 22 72 65 61 64 5f 75 6e  /.    { "read_un
28f13 63 6f 6d 6d 69 74 74 65 64 22 2c 20 20 20 20 20  committed",     
28f14 20 20 20 20 53 51 4c 49 54 45 5f 52 65 61 64 55      SQLITE_ReadU
28f15 6e 63 6f 6d 6d 69 74 74 65 64 20 7d 2c 0a 20 20  ncommitted },.  
28f16 20 20 7b 20 22 72 65 63 75 72 73 69 76 65 5f 74    { "recursive_t
28f17 72 69 67 67 65 72 73 22 2c 20 20 20 20 20 20 20  riggers",       
28f18 53 51 4c 49 54 45 5f 52 65 63 54 72 69 67 67 65  SQLITE_RecTrigge
28f19 72 73 20 7d 2c 0a 0a 20 20 20 20 2f 2a 20 54 68  rs },..    /* Th
28f1a 69 73 20 66 6c 61 67 20 6d 61 79 20 6f 6e 6c 79  is flag may only
28f1b 20 62 65 20 73 65 74 20 69 66 20 62 6f 74 68 20   be set if both 
28f1c 66 6f 72 65 69 67 6e 2d 6b 65 79 20 61 6e 64 20  foreign-key and 
28f1d 74 72 69 67 67 65 72 20 73 75 70 70 6f 72 74 0a  trigger support.
28f1e 20 20 20 20 2a 2a 20 61 72 65 20 70 72 65 73 65      ** are prese
28f1f 6e 74 20 69 6e 20 74 68 65 20 62 75 69 6c 64 2e  nt in the build.
28f20 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65    */.#if !define
28f21 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  d(SQLITE_OMIT_FO
28f22 52 45 49 47 4e 5f 4b 45 59 29 20 26 26 20 21 64  REIGN_KEY) && !d
28f23 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
28f24 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20 20 20  IT_TRIGGER).    
28f25 7b 20 22 66 6f 72 65 69 67 6e 5f 6b 65 79 73 22  { "foreign_keys"
28f26 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51  ,             SQ
28f27 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73  LITE_ForeignKeys
28f28 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a   },.#endif.  };.
28f29 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74    int i;.  const
28f2a 20 73 74 72 75 63 74 20 73 50 72 61 67 6d 61 54   struct sPragmaT
28f2b 79 70 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d  ype *p;.  for(i=
28f2c 30 2c 20 70 3d 61 50 72 61 67 6d 61 3b 20 69 3c  0, p=aPragma; i<
28f2d 41 72 72 61 79 53 69 7a 65 28 61 50 72 61 67 6d  ArraySize(aPragm
28f2e 61 29 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20  a); i++, p++){. 
28f2f 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
28f30 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 70 2d 3e  rICmp(zLeft, p->
28f31 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20  zName)==0 ){.   
28f32 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d     sqlite3 *db =
28f33 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20   pParse->db;.   
28f34 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20     Vdbe *v;.    
28f35 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
28f36 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
28f37 20 20 20 20 61 73 73 65 72 74 28 20 76 21 3d 30      assert( v!=0
28f38 20 29 3b 20 20 2f 2a 20 41 6c 72 65 61 64 79 20   );  /* Already 
28f39 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c  allocated by sql
28f3a 69 74 65 33 50 72 61 67 6d 61 28 29 20 2a 2f 0a  ite3Pragma() */.
28f3b 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53        if( ALWAYS
28f3c 28 76 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69  (v) ){.        i
28f3d 66 28 20 7a 52 69 67 68 74 3d 3d 30 20 29 7b 0a  f( zRight==0 ){.
28f3e 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
28f3f 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65  SingleInt(pParse
28f40 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 28 64 62 2d  , p->zName, (db-
28f41 3e 66 6c 61 67 73 20 26 20 70 2d 3e 6d 61 73 6b  >flags & p->mask
28f42 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  )!=0 );.        
28f43 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
28f44 20 69 6e 74 20 6d 61 73 6b 20 3d 20 70 2d 3e 6d   int mask = p->m
28f45 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ask;          /*
28f46 20 4d 61 73 6b 20 6f 66 20 62 69 74 73 20 74 6f   Mask of bits to
28f47 20 73 65 74 20 6f 72 20 63 6c 65 61 72 2e 20 2a   set or clear. *
28f48 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  /.          if( 
28f49 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d  db->autoCommit==
28f4a 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
28f4b 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79 20   /* Foreign key 
28f4c 73 75 70 70 6f 72 74 20 6d 61 79 20 6e 6f 74 20  support may not 
28f4d 62 65 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 69  be enabled or di
28f4e 73 61 62 6c 65 64 20 77 68 69 6c 65 20 6e 6f 74  sabled while not
28f4f 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
28f50 69 6e 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 6d  in auto-commit m
28f51 6f 64 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20  ode.  */.       
28f52 20 20 20 20 20 6d 61 73 6b 20 26 3d 20 7e 28 53       mask &= ~(S
28f53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79  QLITE_ForeignKey
28f54 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  s);.          }.
28f55 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 67  .          if( g
28f56 65 74 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74  etBoolean(zRight
28f57 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
28f58 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 6d 61   db->flags |= ma
28f59 73 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  sk;.          }e
28f5a 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
28f5b 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 6d   db->flags &= ~m
28f5c 61 73 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  ask;.          }
28f5d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  ..          /* M
28f5e 61 6e 79 20 6f 66 20 74 68 65 20 66 6c 61 67 2d  any of the flag-
28f5f 70 72 61 67 6d 61 73 20 6d 6f 64 69 66 79 20 74  pragmas modify t
28f60 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65  he code generate
28f61 64 20 62 79 20 74 68 65 20 53 51 4c 20 0a 20 20  d by the SQL .  
28f62 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70 69          ** compi
28f63 6c 65 72 20 28 65 67 2e 20 63 6f 75 6e 74 5f 63  ler (eg. count_c
28f64 68 61 6e 67 65 73 29 2e 20 53 6f 20 61 64 64 20  hanges). So add 
28f65 61 6e 20 6f 70 63 6f 64 65 20 74 6f 20 65 78 70  an opcode to exp
28f66 69 72 65 20 61 6c 6c 0a 20 20 20 20 20 20 20 20  ire all.        
28f67 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 53 51    ** compiled SQ
28f68 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61 66 74  L statements aft
28f69 65 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 20 70  er modifying a p
28f6a 72 61 67 6d 61 20 76 61 6c 75 65 2e 0a 20 20 20  ragma value..   
28f6b 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
28f6c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28f6d 64 64 4f 70 32 28 76 2c 20 4f 50 5f 45 78 70 69  ddOp2(v, OP_Expi
28f6e 72 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20  re, 0, 0);.     
28f6f 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20     }.      }..  
28f70 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
28f71 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
28f72 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20   0;.}.#endif /* 
28f73 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47  SQLITE_OMIT_FLAG
28f74 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 2f 2a 0a  _PRAGMAS */../*.
28f75 2a 2a 20 52 65 74 75 72 6e 20 61 20 68 75 6d 61  ** Return a huma
28f76 6e 2d 72 65 61 64 61 62 6c 65 20 6e 61 6d 65 20  n-readable name 
28f77 66 6f 72 20 61 20 63 6f 6e 73 74 72 61 69 6e 74  for a constraint
28f78 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61 63 74 69   resolution acti
28f79 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  on..*/.#ifndef S
28f7a 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
28f7b 47 4e 5f 4b 45 59 0a 73 74 61 74 69 63 20 63 6f  GN_KEY.static co
28f7c 6e 73 74 20 63 68 61 72 20 2a 61 63 74 69 6f 6e  nst char *action
28f7d 4e 61 6d 65 28 75 38 20 61 63 74 69 6f 6e 29 7b  Name(u8 action){
28f7e 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
28f7f 4e 61 6d 65 3b 0a 20 20 73 77 69 74 63 68 28 20  Name;.  switch( 
28f80 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 63 61  action ){.    ca
28f81 73 65 20 4f 45 5f 53 65 74 4e 75 6c 6c 3a 20 20  se OE_SetNull:  
28f82 7a 4e 61 6d 65 20 3d 20 22 53 45 54 20 4e 55 4c  zName = "SET NUL
28f83 4c 22 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b  L";        break
28f84 3b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 53 65  ;.    case OE_Se
28f85 74 44 66 6c 74 3a 20 20 7a 4e 61 6d 65 20 3d 20  tDflt:  zName = 
28f86 22 53 45 54 20 44 45 46 41 55 4c 54 22 3b 20 20  "SET DEFAULT";  
28f87 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
28f88 73 65 20 4f 45 5f 43 61 73 63 61 64 65 3a 20 20  se OE_Cascade:  
28f89 7a 4e 61 6d 65 20 3d 20 22 43 41 53 43 41 44 45  zName = "CASCADE
28f8a 22 3b 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  ";         break
28f8b 3b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 52 65  ;.    case OE_Re
28f8c 73 74 72 69 63 74 3a 20 7a 4e 61 6d 65 20 3d 20  strict: zName = 
28f8d 22 52 45 53 54 52 49 43 54 22 3b 20 20 20 20 20  "RESTRICT";     
28f8e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65     break;.    de
28f8f 66 61 75 6c 74 3a 20 20 20 20 20 20 20 20 20 20  fault:          
28f90 7a 4e 61 6d 65 20 3d 20 22 4e 4f 20 41 43 54 49  zName = "NO ACTI
28f91 4f 4e 22 3b 20 20 0a 20 20 20 20 20 20 20 20 20  ON";  .         
28f92 20 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73               ass
28f93 65 72 74 28 20 61 63 74 69 6f 6e 3d 3d 4f 45 5f  ert( action==OE_
28f94 4e 6f 6e 65 20 29 3b 20 62 72 65 61 6b 3b 0a 20  None ); break;. 
28f95 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 61 6d   }.  return zNam
28f96 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  e;.}.#endif../*.
28f97 2a 2a 20 50 72 6f 63 65 73 73 20 61 20 70 72 61  ** Process a pra
28f98 67 6d 61 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  gma statement.  
28f99 0a 2a 2a 0a 2a 2a 20 50 72 61 67 6d 61 73 20 61  .**.** Pragmas a
28f9a 72 65 20 6f 66 20 74 68 69 73 20 66 6f 72 6d 3a  re of this form:
28f9b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 50 52 41 47  .**.**      PRAG
28f9c 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 69 64  MA [database.]id
28f9d 20 5b 3d 20 76 61 6c 75 65 5d 0a 2a 2a 0a 2a 2a   [= value].**.**
28f9e 20 54 68 65 20 69 64 65 6e 74 69 66 69 65 72 20   The identifier 
28f9f 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20 61 20  might also be a 
28fa0 73 74 72 69 6e 67 2e 20 20 54 68 65 20 76 61 6c  string.  The val
28fa1 75 65 20 69 73 20 61 20 73 74 72 69 6e 67 2c 20  ue is a string, 
28fa2 61 6e 64 0a 2a 2a 20 69 64 65 6e 74 69 66 69 65  and.** identifie
28fa3 72 2c 20 6f 72 20 61 20 6e 75 6d 62 65 72 2e 20  r, or a number. 
28fa4 20 49 66 20 6d 69 6e 75 73 46 6c 61 67 20 69 73   If minusFlag is
28fa5 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20   true, then the 
28fa6 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 20 6e 75  value is.** a nu
28fa7 6d 62 65 72 20 74 68 61 74 20 77 61 73 20 70 72  mber that was pr
28fa8 65 63 65 64 65 64 20 62 79 20 61 20 6d 69 6e 75  eceded by a minu
28fa9 73 20 73 69 67 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  s sign..**.** If
28faa 20 74 68 65 20 6c 65 66 74 20 73 69 64 65 20 69   the left side i
28fab 73 20 22 64 61 74 61 62 61 73 65 2e 69 64 22 20  s "database.id" 
28fac 74 68 65 6e 20 70 49 64 31 20 69 73 20 74 68 65  then pId1 is the
28fad 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a   database name.*
28fae 2a 20 61 6e 64 20 70 49 64 32 20 69 73 20 74 68  * and pId2 is th
28faf 65 20 69 64 2e 20 20 49 66 20 74 68 65 20 6c 65  e id.  If the le
28fb0 66 74 20 73 69 64 65 20 69 73 20 6a 75 73 74 20  ft side is just 
28fb1 22 69 64 22 20 74 68 65 6e 20 70 49 64 31 20 69  "id" then pId1 i
28fb2 73 20 74 68 65 0a 2a 2a 20 69 64 20 61 6e 64 20  s the.** id and 
28fb3 70 49 64 32 20 69 73 20 61 6e 79 20 65 6d 70 74  pId2 is any empt
28fb4 79 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51 4c  y string..*/.SQL
28fb5 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
28fb6 20 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28 0a   sqlite3Pragma(.
28fb7 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
28fb8 20 0a 20 20 54 6f 6b 65 6e 20 2a 70 49 64 31 2c   .  Token *pId1,
28fb9 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
28fba 20 70 61 72 74 20 6f 66 20 5b 64 61 74 61 62 61   part of [databa
28fbb 73 65 2e 5d 69 64 20 66 69 65 6c 64 20 2a 2f 0a  se.]id field */.
28fbc 20 20 54 6f 6b 65 6e 20 2a 70 49 64 32 2c 20 20    Token *pId2,  
28fbd 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20        /* Second 
28fbe 70 61 72 74 20 6f 66 20 5b 64 61 74 61 62 61 73  part of [databas
28fbf 65 2e 5d 69 64 20 66 69 65 6c 64 2c 20 6f 72 20  e.]id field, or 
28fc0 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  NULL */.  Token 
28fc1 2a 70 56 61 6c 75 65 2c 20 20 20 20 20 20 2f 2a  *pValue,      /*
28fc2 20 54 6f 6b 65 6e 20 66 6f 72 20 3c 76 61 6c 75   Token for <valu
28fc3 65 3e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20  e>, or NULL */. 
28fc4 20 69 6e 74 20 6d 69 6e 75 73 46 6c 61 67 20 20   int minusFlag  
28fc5 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
28fc6 61 20 27 2d 27 20 73 69 67 6e 20 70 72 65 63 65  a '-' sign prece
28fc7 64 65 64 20 3c 76 61 6c 75 65 3e 20 2a 2f 0a 29  ded <value> */.)
28fc8 7b 0a 20 20 63 68 61 72 20 2a 7a 4c 65 66 74 20  {.  char *zLeft 
28fc9 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  = 0;       /* Nu
28fca 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46  l-terminated UTF
28fcb 2d 38 20 73 74 72 69 6e 67 20 3c 69 64 3e 20 2a  -8 string <id> *
28fcc 2f 0a 20 20 63 68 61 72 20 2a 7a 52 69 67 68 74  /.  char *zRight
28fcd 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 75   = 0;      /* Nu
28fce 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46  l-terminated UTF
28fcf 2d 38 20 73 74 72 69 6e 67 20 3c 76 61 6c 75 65  -8 string <value
28fd0 3e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20  >, or NULL */.  
28fd1 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20  const char *zDb 
28fd2 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 64 61  = 0;   /* The da
28fd3 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20  tabase name */. 
28fd4 20 54 6f 6b 65 6e 20 2a 70 49 64 3b 20 20 20 20   Token *pId;    
28fd5 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
28fd6 65 72 20 74 6f 20 3c 69 64 3e 20 74 6f 6b 65 6e  er to <id> token
28fd7 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
28fd8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28fd9 44 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 66  Database index f
28fda 6f 72 20 3c 64 61 74 61 62 61 73 65 3e 20 2a 2f  or <database> */
28fdb 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
28fdc 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44   pParse->db;.  D
28fdd 62 20 2a 70 44 62 3b 0a 20 20 56 64 62 65 20 2a  b *pDb;.  Vdbe *
28fde 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
28fdf 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  e = sqlite3VdbeC
28fe0 72 65 61 74 65 28 64 62 29 3b 0a 20 20 69 66 28  reate(db);.  if(
28fe1 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   v==0 ) return;.
28fe2 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d    pParse->nMem =
28fe3 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 72 70   2;..  /* Interp
28fe4 72 65 74 20 74 68 65 20 5b 64 61 74 61 62 61 73  ret the [databas
28fe5 65 2e 5d 20 70 61 72 74 20 6f 66 20 74 68 65 20  e.] part of the 
28fe6 70 72 61 67 6d 61 20 73 74 61 74 65 6d 65 6e 74  pragma statement
28fe7 2e 20 69 44 62 20 69 73 20 74 68 65 0a 20 20 2a  . iDb is the.  *
28fe8 2a 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64  * index of the d
28fe9 61 74 61 62 61 73 65 20 74 68 69 73 20 70 72 61  atabase this pra
28fea 67 6d 61 20 69 73 20 62 65 69 6e 67 20 61 70 70  gma is being app
28feb 6c 69 65 64 20 74 6f 20 69 6e 20 64 62 2e 61 44  lied to in db.aD
28fec 62 5b 5d 2e 20 2a 2f 0a 20 20 69 44 62 20 3d 20  b[]. */.  iDb = 
28fed 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61  sqlite3TwoPartNa
28fee 6d 65 28 70 50 61 72 73 65 2c 20 70 49 64 31 2c  me(pParse, pId1,
28fef 20 70 49 64 32 2c 20 26 70 49 64 29 3b 0a 20 20   pId2, &pId);.  
28ff0 69 66 28 20 69 44 62 3c 30 20 29 20 72 65 74 75  if( iDb<0 ) retu
28ff1 72 6e 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d  rn;.  pDb = &db-
28ff2 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a 20 20 2f 2a  >aDb[iDb];..  /*
28ff3 20 49 66 20 74 68 65 20 74 65 6d 70 20 64 61 74   If the temp dat
28ff4 61 62 61 73 65 20 68 61 73 20 62 65 65 6e 20 65  abase has been e
28ff5 78 70 6c 69 63 69 74 6c 79 20 6e 61 6d 65 64 20  xplicitly named 
28ff6 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 0a  as part of the .
28ff7 20 20 2a 2a 20 70 72 61 67 6d 61 2c 20 6d 61 6b    ** pragma, mak
28ff8 65 20 73 75 72 65 20 69 74 20 69 73 20 6f 70 65  e sure it is ope
28ff9 6e 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69  n. .  */.  if( i
28ffa 44 62 3d 3d 31 20 26 26 20 73 71 6c 69 74 65 33  Db==1 && sqlite3
28ffb 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65  OpenTempDatabase
28ffc 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20  (pParse) ){.    
28ffd 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 7a  return;.  }..  z
28ffe 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 4e 61  Left = sqlite3Na
28fff 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
29000 70 49 64 29 3b 0a 20 20 69 66 28 20 21 7a 4c 65  pId);.  if( !zLe
29001 66 74 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  ft ) return;.  i
29002 66 28 20 6d 69 6e 75 73 46 6c 61 67 20 29 7b 0a  f( minusFlag ){.
29003 20 20 20 20 7a 52 69 67 68 74 20 3d 20 73 71 6c      zRight = sql
29004 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
29005 22 2d 25 54 22 2c 20 70 56 61 6c 75 65 29 3b 0a  "-%T", pValue);.
29006 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 52 69    }else{.    zRi
29007 67 68 74 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  ght = sqlite3Nam
29008 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
29009 56 61 6c 75 65 29 3b 0a 20 20 7d 0a 0a 20 20 61  Value);.  }..  a
2900a 73 73 65 72 74 28 20 70 49 64 32 20 29 3b 0a 20  ssert( pId2 );. 
2900b 20 7a 44 62 20 3d 20 70 49 64 32 2d 3e 6e 3e 30   zDb = pId2->n>0
2900c 20 3f 20 70 44 62 2d 3e 7a 4e 61 6d 65 20 3a 20   ? pDb->zName : 
2900d 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  0;.  if( sqlite3
2900e 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
2900f 2c 20 53 51 4c 49 54 45 5f 50 52 41 47 4d 41 2c  , SQLITE_PRAGMA,
29010 20 7a 4c 65 66 74 2c 20 7a 52 69 67 68 74 2c 20   zLeft, zRight, 
29011 7a 44 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  zDb) ){.    goto
29012 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 7d   pragma_out;.  }
29013 0a 20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  . .#ifndef SQLIT
29014 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
29015 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20  GMAS.  /*.  **  
29016 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
29017 2e 5d 64 65 66 61 75 6c 74 5f 63 61 63 68 65 5f  .]default_cache_
29018 73 69 7a 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d  size.  **  PRAGM
29019 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 64 65 66  A [database.]def
2901a 61 75 6c 74 5f 63 61 63 68 65 5f 73 69 7a 65 3d  ault_cache_size=
2901b 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  N.  **.  ** The 
2901c 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72  first form repor
2901d 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 70  ts the current p
2901e 65 72 73 69 73 74 65 6e 74 20 73 65 74 74 69 6e  ersistent settin
2901f 67 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 70  g for the.  ** p
29020 61 67 65 20 63 61 63 68 65 20 73 69 7a 65 2e 20  age cache size. 
29021 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   The value retur
29022 6e 65 64 20 69 73 20 74 68 65 20 6d 61 78 69 6d  ned is the maxim
29023 75 6d 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 2a  um number of.  *
29024 2a 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 70  * pages in the p
29025 61 67 65 20 63 61 63 68 65 2e 20 20 54 68 65 20  age cache.  The 
29026 73 65 63 6f 6e 64 20 66 6f 72 6d 20 73 65 74 73  second form sets
29027 20 62 6f 74 68 20 74 68 65 20 63 75 72 72 65 6e   both the curren
29028 74 0a 20 20 2a 2a 20 70 61 67 65 20 63 61 63 68  t.  ** page cach
29029 65 20 73 69 7a 65 20 76 61 6c 75 65 20 61 6e 64  e size value and
2902a 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20   the persistent 
2902b 70 61 67 65 20 63 61 63 68 65 20 73 69 7a 65 20  page cache size 
2902c 76 61 6c 75 65 0a 20 20 2a 2a 20 73 74 6f 72 65  value.  ** store
2902d 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  d in the databas
2902e 65 20 66 69 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a  e file..  **.  *
2902f 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 63 61  * The default ca
29030 63 68 65 20 73 69 7a 65 20 69 73 20 73 74 6f 72  che size is stor
29031 65 64 20 69 6e 20 6d 65 74 61 2d 76 61 6c 75 65  ed in meta-value
29032 20 32 20 6f 66 20 70 61 67 65 20 31 20 6f 66 20   2 of page 1 of 
29033 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  the.  ** databas
29034 65 20 66 69 6c 65 2e 20 20 54 68 65 20 63 61 63  e file.  The cac
29035 68 65 20 73 69 7a 65 20 69 73 20 61 63 74 75 61  he size is actua
29036 6c 6c 79 20 74 68 65 20 61 62 73 6f 6c 75 74 65  lly the absolute
29037 20 76 61 6c 75 65 20 6f 66 0a 20 20 2a 2a 20 74   value of.  ** t
29038 68 69 73 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74  his memory locat
29039 69 6f 6e 2e 20 20 54 68 65 20 73 69 67 6e 20 6f  ion.  The sign o
2903a 66 20 6d 65 74 61 2d 76 61 6c 75 65 20 32 20 64  f meta-value 2 d
2903b 65 74 65 72 6d 69 6e 65 73 20 74 68 65 0a 20 20  etermines the.  
2903c 2a 2a 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 73  ** synchronous s
2903d 65 74 74 69 6e 67 2e 20 20 41 20 6e 65 67 61 74  etting.  A negat
2903e 69 76 65 20 76 61 6c 75 65 20 6d 65 61 6e 73 20  ive value means 
2903f 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f  synchronous is o
29040 66 66 0a 20 20 2a 2a 20 61 6e 64 20 61 20 70 6f  ff.  ** and a po
29041 73 69 74 69 76 65 20 76 61 6c 75 65 20 6d 65 61  sitive value mea
29042 6e 73 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69  ns synchronous i
29043 73 20 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  s on..  */.  if(
29044 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
29045 7a 4c 65 66 74 2c 22 64 65 66 61 75 6c 74 5f 63  zLeft,"default_c
29046 61 63 68 65 5f 73 69 7a 65 22 29 3d 3d 30 20 29  ache_size")==0 )
29047 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  {.    static con
29048 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 67 65  st VdbeOpList ge
29049 74 43 61 63 68 65 53 69 7a 65 5b 5d 20 3d 20 7b  tCacheSize[] = {
2904a 0a 20 20 20 20 20 20 7b 20 4f 50 5f 54 72 61 6e  .      { OP_Tran
2904b 73 61 63 74 69 6f 6e 2c 20 30 2c 20 30 2c 20 20  saction, 0, 0,  
2904c 20 20 20 20 20 20 30 7d 2c 20 20 20 20 20 20 20        0},       
2904d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2904e 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20    /* 0 */.      
2904f 7b 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c  { OP_ReadCookie,
29050 20 20 30 2c 20 31 2c 20 20 20 20 20 20 20 20 42    0, 1,        B
29051 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43 41 43  TREE_DEFAULT_CAC
29052 48 45 5f 53 49 5a 45 7d 2c 20 20 2f 2a 20 31 20  HE_SIZE},  /* 1 
29053 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49 66  */.      { OP_If
29054 50 6f 73 2c 20 20 20 20 20 20 20 31 2c 20 37 2c  Pos,       1, 7,
29055 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20          0},.    
29056 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20    { OP_Integer, 
29057 20 20 20 20 30 2c 20 32 2c 20 20 20 20 20 20 20      0, 2,       
29058 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f   0},.      { OP_
29059 53 75 62 74 72 61 63 74 2c 20 20 20 20 31 2c 20  Subtract,    1, 
2905a 32 2c 20 20 20 20 20 20 20 20 31 7d 2c 0a 20 20  2,        1},.  
2905b 20 20 20 20 7b 20 4f 50 5f 49 66 50 6f 73 2c 20      { OP_IfPos, 
2905c 20 20 20 20 20 20 31 2c 20 37 2c 20 20 20 20 20        1, 7,     
2905d 20 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f     0},.      { O
2905e 50 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20 30  P_Integer,     0
2905f 2c 20 31 2c 20 20 20 20 20 20 20 20 30 7d 2c 20  , 1,        0}, 
29060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29061 20 20 20 20 20 20 20 20 2f 2a 20 36 20 2a 2f 0a          /* 6 */.
29062 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c        { OP_Resul
29063 74 52 6f 77 2c 20 20 20 31 2c 20 31 2c 20 20 20  tRow,   1, 1,   
29064 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 7d 3b 0a       0},.    };.
29065 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20      int addr;.  
29066 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61    if( sqlite3Rea
29067 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
29068 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75  ) goto pragma_ou
29069 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  t;.    sqlite3Vd
2906a 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69  beUsesBtree(v, i
2906b 44 62 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 52  Db);.    if( !zR
2906c 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 73 71  ight ){.      sq
2906d 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
2906e 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 20  ols(v, 1);.     
2906f 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
29070 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
29071 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 61 63 68  NAME_NAME, "cach
29072 65 5f 73 69 7a 65 22 2c 20 53 51 4c 49 54 45 5f  e_size", SQLITE_
29073 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 70  STATIC);.      p
29074 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 32  Parse->nMem += 2
29075 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73  ;.      addr = s
29076 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
29077 69 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65  ist(v, ArraySize
29078 28 67 65 74 43 61 63 68 65 53 69 7a 65 29 2c 20  (getCacheSize), 
29079 67 65 74 43 61 63 68 65 53 69 7a 65 29 3b 0a 20  getCacheSize);. 
2907a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2907b 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72  ChangeP1(v, addr
2907c 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71  , iDb);.      sq
2907d 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2907e 31 28 76 2c 20 61 64 64 72 2b 31 2c 20 69 44 62  1(v, addr+1, iDb
2907f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29080 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
29081 61 64 64 72 2b 36 2c 20 53 51 4c 49 54 45 5f 44  addr+6, SQLITE_D
29082 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a  EFAULT_CACHE_SIZ
29083 45 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  E);.    }else{. 
29084 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20       int size = 
29085 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20 20  atoi(zRight);.  
29086 20 20 20 20 69 66 28 20 73 69 7a 65 3c 30 20 29      if( size<0 )
29087 20 73 69 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20   size = -size;. 
29088 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69       sqlite3Begi
29089 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
2908a 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b  pParse, 0, iDb);
2908b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2908c 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2908d 6e 74 65 67 65 72 2c 20 73 69 7a 65 2c 20 31 29  nteger, size, 1)
2908e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2908f 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
29090 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62 2c  ReadCookie, iDb,
29091 20 32 2c 20 42 54 52 45 45 5f 44 45 46 41 55 4c   2, BTREE_DEFAUL
29092 54 5f 43 41 43 48 45 5f 53 49 5a 45 29 3b 0a 20  T_CACHE_SIZE);. 
29093 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
29094 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
29095 20 4f 50 5f 49 66 50 6f 73 2c 20 32 2c 20 30 29   OP_IfPos, 2, 0)
29096 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
29097 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29098 49 6e 74 65 67 65 72 2c 20 2d 73 69 7a 65 2c 20  Integer, -size, 
29099 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
2909a 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2909b 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 73 71   addr);.      sq
2909c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2909d 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c  v, OP_SetCookie,
2909e 20 69 44 62 2c 20 42 54 52 45 45 5f 44 45 46 41   iDb, BTREE_DEFA
2909f 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20  ULT_CACHE_SIZE, 
290a0 31 29 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e 70  1);.      pDb->p
290a1 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69  Schema->cache_si
290a2 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20  ze = size;.     
290a3 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
290a4 43 61 63 68 65 53 69 7a 65 28 70 44 62 2d 3e 70  CacheSize(pDb->p
290a5 42 74 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61  Bt, pDb->pSchema
290a6 2d 3e 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 20  ->cache_size);. 
290a7 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20     }.  }else..  
290a8 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  /*.  **  PRAGMA 
290a9 5b 64 61 74 61 62 61 73 65 2e 5d 70 61 67 65 5f  [database.]page_
290aa 73 69 7a 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d  size.  **  PRAGM
290ab 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 70 61 67  A [database.]pag
290ac 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a 0a 20 20  e_size=N.  **.  
290ad 2a 2a 20 54 68 65 20 66 69 72 73 74 20 66 6f 72  ** The first for
290ae 6d 20 72 65 70 6f 72 74 73 20 74 68 65 20 63 75  m reports the cu
290af 72 72 65 6e 74 20 73 65 74 74 69 6e 67 20 66 6f  rrent setting fo
290b0 72 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62  r the.  ** datab
290b1 61 73 65 20 70 61 67 65 20 73 69 7a 65 20 69 6e  ase page size in
290b2 20 62 79 74 65 73 2e 20 20 54 68 65 20 73 65 63   bytes.  The sec
290b3 6f 6e 64 20 66 6f 72 6d 20 73 65 74 73 20 74 68  ond form sets th
290b4 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20  e.  ** database 
290b5 70 61 67 65 20 73 69 7a 65 20 76 61 6c 75 65 2e  page size value.
290b6 20 20 54 68 65 20 76 61 6c 75 65 20 63 61 6e 20    The value can 
290b7 6f 6e 6c 79 20 62 65 20 73 65 74 20 69 66 0a 20  only be set if. 
290b8 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65   ** the database
290b9 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65   has not yet bee
290ba 6e 20 63 72 65 61 74 65 64 2e 0a 20 20 2a 2f 0a  n created..  */.
290bb 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
290bc 49 43 6d 70 28 7a 4c 65 66 74 2c 22 70 61 67 65  ICmp(zLeft,"page
290bd 5f 73 69 7a 65 22 29 3d 3d 30 20 29 7b 0a 20 20  _size")==0 ){.  
290be 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 70    Btree *pBt = p
290bf 44 62 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73 73  Db->pBt;.    ass
290c0 65 72 74 28 20 70 42 74 21 3d 30 20 29 3b 0a 20  ert( pBt!=0 );. 
290c1 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29     if( !zRight )
290c2 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65  {.      int size
290c3 20 3d 20 41 4c 57 41 59 53 28 70 42 74 29 20 3f   = ALWAYS(pBt) ?
290c4 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
290c5 50 61 67 65 53 69 7a 65 28 70 42 74 29 20 3a 20  PageSize(pBt) : 
290c6 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 53  0;.      returnS
290c7 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c  ingleInt(pParse,
290c8 20 22 70 61 67 65 5f 73 69 7a 65 22 2c 20 73 69   "page_size", si
290c9 7a 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ze);.    }else{.
290ca 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20        /* Malloc 
290cb 6d 61 79 20 66 61 69 6c 20 77 68 65 6e 20 73 65  may fail when se
290cc 74 74 69 6e 67 20 74 68 65 20 70 61 67 65 2d 73  tting the page-s
290cd 69 7a 65 2c 20 61 73 20 74 68 65 72 65 20 69 73  ize, as there is
290ce 20 61 6e 20 69 6e 74 65 72 6e 61 6c 0a 20 20 20   an internal.   
290cf 20 20 20 2a 2a 20 62 75 66 66 65 72 20 74 68 61     ** buffer tha
290d0 74 20 74 68 65 20 70 61 67 65 72 20 6d 6f 64 75  t the pager modu
290d1 6c 65 20 72 65 73 69 7a 65 73 20 75 73 69 6e 67  le resizes using
290d2 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
290d3 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ()..      */.   
290d4 20 20 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73     db->nextPages
290d5 69 7a 65 20 3d 20 61 74 6f 69 28 7a 52 69 67 68  ize = atoi(zRigh
290d6 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51  t);.      if( SQ
290d7 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73 71 6c 69  LITE_NOMEM==sqli
290d8 74 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53  te3BtreeSetPageS
290d9 69 7a 65 28 70 42 74 2c 20 64 62 2d 3e 6e 65 78  ize(pBt, db->nex
290da 74 50 61 67 65 73 69 7a 65 2c 20 2d 31 2c 20 30  tPagesize, -1, 0
290db 29 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d  ) ){.        db-
290dc 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
290dd 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
290de 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20  .  }else..  /*. 
290df 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
290e0 61 62 61 73 65 2e 5d 6d 61 78 5f 70 61 67 65 5f  abase.]max_page_
290e1 63 6f 75 6e 74 0a 20 20 2a 2a 20 20 50 52 41 47  count.  **  PRAG
290e2 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6d 61  MA [database.]ma
290e3 78 5f 70 61 67 65 5f 63 6f 75 6e 74 3d 4e 0a 20  x_page_count=N. 
290e4 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69 72   **.  ** The fir
290e5 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74 73 20  st form reports 
290e6 74 68 65 20 63 75 72 72 65 6e 74 20 73 65 74 74  the current sett
290e7 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a  ing for the.  **
290e8 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
290e9 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
290ea 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
290eb 54 68 65 20 0a 20 20 2a 2a 20 73 65 63 6f 6e 64  The .  ** second
290ec 20 66 6f 72 6d 20 61 74 74 65 6d 70 74 73 20 74   form attempts t
290ed 6f 20 63 68 61 6e 67 65 20 74 68 69 73 20 73 65  o change this se
290ee 74 74 69 6e 67 2e 20 20 42 6f 74 68 0a 20 20 2a  tting.  Both.  *
290ef 2a 20 66 6f 72 6d 73 20 72 65 74 75 72 6e 20 74  * forms return t
290f0 68 65 20 63 75 72 72 65 6e 74 20 73 65 74 74 69  he current setti
290f1 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ng..  */.  if( s
290f2 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
290f3 65 66 74 2c 22 6d 61 78 5f 70 61 67 65 5f 63 6f  eft,"max_page_co
290f4 75 6e 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  unt")==0 ){.    
290f5 42 74 72 65 65 20 2a 70 42 74 20 3d 20 70 44 62  Btree *pBt = pDb
290f6 2d 3e 70 42 74 3b 0a 20 20 20 20 69 6e 74 20 6e  ->pBt;.    int n
290f7 65 77 4d 61 78 20 3d 20 30 3b 0a 20 20 20 20 61  ewMax = 0;.    a
290f8 73 73 65 72 74 28 20 70 42 74 21 3d 30 20 29 3b  ssert( pBt!=0 );
290f9 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 20  .    if( zRight 
290fa 29 7b 0a 20 20 20 20 20 20 6e 65 77 4d 61 78 20  ){.      newMax 
290fb 3d 20 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a  = atoi(zRight);.
290fc 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 41 4c      }.    if( AL
290fd 57 41 59 53 28 70 42 74 29 20 29 7b 0a 20 20 20  WAYS(pBt) ){.   
290fe 20 20 20 6e 65 77 4d 61 78 20 3d 20 73 71 6c 69     newMax = sqli
290ff 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 65 43  te3BtreeMaxPageC
29100 6f 75 6e 74 28 70 42 74 2c 20 6e 65 77 4d 61 78  ount(pBt, newMax
29101 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  );.    }.    ret
29102 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61  urnSingleInt(pPa
29103 72 73 65 2c 20 22 6d 61 78 5f 70 61 67 65 5f 63  rse, "max_page_c
29104 6f 75 6e 74 22 2c 20 6e 65 77 4d 61 78 29 3b 0a  ount", newMax);.
29105 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20    }else..  /*.  
29106 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
29107 62 61 73 65 2e 5d 70 61 67 65 5f 63 6f 75 6e 74  base.]page_count
29108 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72  .  **.  ** Retur
29109 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
2910a 70 61 67 65 73 20 69 6e 20 74 68 65 20 73 70 65  pages in the spe
2910b 63 69 66 69 65 64 20 64 61 74 61 62 61 73 65 2e  cified database.
2910c 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
2910d 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
2910e 2c 22 70 61 67 65 5f 63 6f 75 6e 74 22 29 3d 3d  ,"page_count")==
2910f 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 52 65  0 ){.    int iRe
29110 67 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  g;.    if( sqlit
29111 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61  e3ReadSchema(pPa
29112 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67  rse) ) goto prag
29113 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 73 71 6c 69  ma_out;.    sqli
29114 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68  te3CodeVerifySch
29115 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 29  ema(pParse, iDb)
29116 3b 0a 20 20 20 20 69 52 65 67 20 3d 20 2b 2b 70  ;.    iReg = ++p
29117 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
29118 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29119 70 32 28 76 2c 20 4f 50 5f 50 61 67 65 63 6f 75  p2(v, OP_Pagecou
2911a 6e 74 2c 20 69 44 62 2c 20 69 52 65 67 29 3b 0a  nt, iDb, iReg);.
2911b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2911c 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
2911d 6c 74 52 6f 77 2c 20 69 52 65 67 2c 20 31 29 3b  ltRow, iReg, 1);
2911e 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2911f 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
29120 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
29121 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
29122 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
29123 22 70 61 67 65 5f 63 6f 75 6e 74 22 2c 20 53 51  "page_count", SQ
29124 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
29125 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  }else..  /*.  **
29126 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
29127 73 65 2e 5d 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65  se.]locking_mode
29128 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
29129 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 69 6e 67  atabase.]locking
2912a 5f 6d 6f 64 65 20 3d 20 28 6e 6f 72 6d 61 6c 7c  _mode = (normal|
2912b 65 78 63 6c 75 73 69 76 65 29 0a 20 20 2a 2f 0a  exclusive).  */.
2912c 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
2912d 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6c 6f 63 6b  ICmp(zLeft,"lock
2912e 69 6e 67 5f 6d 6f 64 65 22 29 3d 3d 30 20 29 7b  ing_mode")==0 ){
2912f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
29130 2a 7a 52 65 74 20 3d 20 22 6e 6f 72 6d 61 6c 22  *zRet = "normal"
29131 3b 0a 20 20 20 20 69 6e 74 20 65 4d 6f 64 65 20  ;.    int eMode 
29132 3d 20 67 65 74 4c 6f 63 6b 69 6e 67 4d 6f 64 65  = getLockingMode
29133 28 7a 52 69 67 68 74 29 3b 0a 0a 20 20 20 20 69  (zRight);..    i
29134 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20 26 26  f( pId2->n==0 &&
29135 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f   eMode==PAGER_LO
29136 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 20  CKINGMODE_QUERY 
29137 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 69 6d 70  ){.      /* Simp
29138 6c 65 20 22 50 52 41 47 4d 41 20 6c 6f 63 6b 69  le "PRAGMA locki
29139 6e 67 5f 6d 6f 64 65 3b 22 20 73 74 61 74 65 6d  ng_mode;" statem
2913a 65 6e 74 2e 20 54 68 69 73 20 69 73 20 61 20 71  ent. This is a q
2913b 75 65 72 79 20 66 6f 72 0a 20 20 20 20 20 20 2a  uery for.      *
2913c 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 65  * the current de
2913d 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 20 6d 6f  fault locking mo
2913e 64 65 20 28 77 68 69 63 68 20 6d 61 79 20 62 65  de (which may be
2913f 20 64 69 66 66 65 72 65 6e 74 20 74 6f 0a 20 20   different to.  
29140 20 20 20 20 2a 2a 20 74 68 65 20 6c 6f 63 6b 69      ** the locki
29141 6e 67 2d 6d 6f 64 65 20 6f 66 20 74 68 65 20 6d  ng-mode of the m
29142 61 69 6e 20 64 61 74 61 62 61 73 65 29 2e 0a 20  ain database).. 
29143 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 65 4d       */.      eM
29144 6f 64 65 20 3d 20 64 62 2d 3e 64 66 6c 74 4c 6f  ode = db->dfltLo
29145 63 6b 4d 6f 64 65 3b 0a 20 20 20 20 7d 65 6c 73  ckMode;.    }els
29146 65 7b 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a  e{.      Pager *
29147 70 50 61 67 65 72 3b 0a 20 20 20 20 20 20 69 66  pPager;.      if
29148 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20 29 7b 0a  ( pId2->n==0 ){.
29149 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
2914a 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 6e  indicates that n
2914b 6f 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  o database name 
2914c 77 61 73 20 73 70 65 63 69 66 69 65 64 20 61 73  was specified as
2914d 20 70 61 72 74 0a 20 20 20 20 20 20 20 20 2a 2a   part.        **
2914e 20 6f 66 20 74 68 65 20 50 52 41 47 4d 41 20 63   of the PRAGMA c
2914f 6f 6d 6d 61 6e 64 2e 20 49 6e 20 74 68 69 73 20  ommand. In this 
29150 63 61 73 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67  case the locking
29151 2d 6d 6f 64 65 20 6d 75 73 74 20 62 65 0a 20 20  -mode must be.  
29152 20 20 20 20 20 20 2a 2a 20 73 65 74 20 6f 6e 20        ** set on 
29153 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74  all attached dat
29154 61 62 61 73 65 73 2c 20 61 73 20 77 65 6c 6c 20  abases, as well 
29155 61 73 20 74 68 65 20 6d 61 69 6e 20 64 62 20 66  as the main db f
29156 69 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a  ile..        **.
29157 20 20 20 20 20 20 20 20 2a 2a 20 41 6c 73 6f 2c          ** Also,
29158 20 74 68 65 20 73 71 6c 69 74 65 33 2e 64 66 6c   the sqlite3.dfl
29159 74 4c 6f 63 6b 4d 6f 64 65 20 76 61 72 69 61 62  tLockMode variab
2915a 6c 65 20 69 73 20 73 65 74 20 73 6f 20 74 68 61  le is set so tha
2915b 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79  t.        ** any
2915c 20 73 75 62 73 65 71 75 65 6e 74 6c 79 20 61 74   subsequently at
2915d 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
2915e 20 61 6c 73 6f 20 75 73 65 20 74 68 65 20 73 70   also use the sp
2915f 65 63 69 66 69 65 64 0a 20 20 20 20 20 20 20 20  ecified.        
29160 2a 2a 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 2e  ** locking mode.
29161 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
29162 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20      int ii;.    
29163 20 20 20 20 61 73 73 65 72 74 28 70 44 62 3d 3d      assert(pDb==
29164 26 64 62 2d 3e 61 44 62 5b 30 5d 29 3b 0a 20 20  &db->aDb[0]);.  
29165 20 20 20 20 20 20 66 6f 72 28 69 69 3d 32 3b 20        for(ii=2; 
29166 69 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 69 2b 2b  ii<db->nDb; ii++
29167 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61  ){.          pPa
29168 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72  ger = sqlite3Btr
29169 65 65 50 61 67 65 72 28 64 62 2d 3e 61 44 62 5b  eePager(db->aDb[
2916a 69 69 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20  ii].pBt);.      
2916b 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
2916c 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67  LockingMode(pPag
2916d 65 72 2c 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20  er, eMode);.    
2916e 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 62      }.        db
2916f 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 20 3d  ->dfltLockMode =
29170 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 20 20   (u8)eMode;.    
29171 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72    }.      pPager
29172 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
29173 61 67 65 72 28 70 44 62 2d 3e 70 42 74 29 3b 0a  ager(pDb->pBt);.
29174 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20 73 71        eMode = sq
29175 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e  lite3PagerLockin
29176 67 4d 6f 64 65 28 70 50 61 67 65 72 2c 20 65 4d  gMode(pPager, eM
29177 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ode);.    }..   
29178 20 61 73 73 65 72 74 28 65 4d 6f 64 65 3d 3d 50   assert(eMode==P
29179 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
2917a 5f 4e 4f 52 4d 41 4c 7c 7c 65 4d 6f 64 65 3d 3d  _NORMAL||eMode==
2917b 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
2917c 45 5f 45 58 43 4c 55 53 49 56 45 29 3b 0a 20 20  E_EXCLUSIVE);.  
2917d 20 20 69 66 28 20 65 4d 6f 64 65 3d 3d 50 41 47    if( eMode==PAG
2917e 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45  ER_LOCKINGMODE_E
2917f 58 43 4c 55 53 49 56 45 20 29 7b 0a 20 20 20 20  XCLUSIVE ){.    
29180 20 20 7a 52 65 74 20 3d 20 22 65 78 63 6c 75 73    zRet = "exclus
29181 69 76 65 22 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ive";.    }.    
29182 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
29183 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20  mCols(v, 1);.   
29184 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
29185 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
29186 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6c 6f 63 6b  NAME_NAME, "lock
29187 69 6e 67 5f 6d 6f 64 65 22 2c 20 53 51 4c 49 54  ing_mode", SQLIT
29188 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73  E_STATIC);.    s
29189 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2918a 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
2918b 30 2c 20 31 2c 20 30 2c 20 7a 52 65 74 2c 20 30  0, 1, 0, zRet, 0
2918c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2918d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2918e 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b  esultRow, 1, 1);
2918f 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20  .  }else..  /*. 
29190 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
29191 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f 6d  abase.]journal_m
29192 6f 64 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41  ode.  **  PRAGMA
29193 20 5b 64 61 74 61 62 61 73 65 2e 5d 6a 6f 75 72   [database.]jour
29194 6e 61 6c 5f 6d 6f 64 65 20 3d 20 28 64 65 6c 65  nal_mode = (dele
29195 74 65 7c 70 65 72 73 69 73 74 7c 6f 66 66 7c 74  te|persist|off|t
29196 72 75 6e 63 61 74 65 7c 6d 65 6d 6f 72 79 29 0a  runcate|memory).
29197 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
29198 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
29199 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 22 29 3d  "journal_mode")=
2919a 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 4d  =0 ){.    int eM
2919b 6f 64 65 3b 0a 20 20 20 20 73 74 61 74 69 63 20  ode;.    static 
2919c 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 61 7a 4d  char * const azM
2919d 6f 64 65 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20 20  odeName[] = {.  
2919e 20 20 20 20 22 64 65 6c 65 74 65 22 2c 20 22 70      "delete", "p
2919f 65 72 73 69 73 74 22 2c 20 22 6f 66 66 22 2c 20  ersist", "off", 
291a0 22 74 72 75 6e 63 61 74 65 22 2c 20 22 6d 65 6d  "truncate", "mem
291a1 6f 72 79 22 0a 20 20 20 20 7d 3b 0a 0a 20 20 20  ory".    };..   
291a2 20 69 66 28 20 7a 52 69 67 68 74 3d 3d 30 20 29   if( zRight==0 )
291a3 7b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20  {.      eMode = 
291a4 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
291a5 45 5f 51 55 45 52 59 3b 0a 20 20 20 20 7d 65 6c  E_QUERY;.    }el
291a6 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20  se{.      int n 
291a7 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
291a8 30 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20  0(zRight);.     
291a9 20 65 4d 6f 64 65 20 3d 20 73 69 7a 65 6f 66 28   eMode = sizeof(
291aa 61 7a 4d 6f 64 65 4e 61 6d 65 29 2f 73 69 7a 65  azModeName)/size
291ab 6f 66 28 61 7a 4d 6f 64 65 4e 61 6d 65 5b 30 5d  of(azModeName[0]
291ac 29 20 2d 20 31 3b 0a 20 20 20 20 20 20 77 68 69  ) - 1;.      whi
291ad 6c 65 28 20 65 4d 6f 64 65 3e 3d 30 20 26 26 20  le( eMode>=0 && 
291ae 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
291af 7a 52 69 67 68 74 2c 20 61 7a 4d 6f 64 65 4e 61  zRight, azModeNa
291b0 6d 65 5b 65 4d 6f 64 65 5d 2c 20 6e 29 21 3d 30  me[eMode], n)!=0
291b1 20 29 7b 0a 20 20 20 20 20 20 20 20 65 4d 6f 64   ){.        eMod
291b2 65 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  e--;.      }.   
291b3 20 7d 0a 20 20 20 20 69 66 28 20 70 49 64 32 2d   }.    if( pId2-
291b4 3e 6e 3d 3d 30 20 26 26 20 65 4d 6f 64 65 3d 3d  >n==0 && eMode==
291b5 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
291b6 45 5f 51 55 45 52 59 20 29 7b 0a 20 20 20 20 20  E_QUERY ){.     
291b7 20 2f 2a 20 53 69 6d 70 6c 65 20 22 50 52 41 47   /* Simple "PRAG
291b8 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3b  MA journal_mode;
291b9 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 69  " statement. Thi
291ba 73 20 69 73 20 61 20 71 75 65 72 79 20 66 6f 72  s is a query for
291bb 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 75  .      ** the cu
291bc 72 72 65 6e 74 20 64 65 66 61 75 6c 74 20 6a 6f  rrent default jo
291bd 75 72 6e 61 6c 20 6d 6f 64 65 20 28 77 68 69 63  urnal mode (whic
291be 68 20 6d 61 79 20 62 65 20 64 69 66 66 65 72 65  h may be differe
291bf 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74  nt to.      ** t
291c0 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20  he journal-mode 
291c1 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  of the main data
291c2 62 61 73 65 29 2e 0a 20 20 20 20 20 20 2a 2f 0a  base)..      */.
291c3 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20 64 62        eMode = db
291c4 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64  ->dfltJournalMod
291c5 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  e;.    }else{.  
291c6 20 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65      Pager *pPage
291c7 72 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 64  r;.      if( pId
291c8 32 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20  2->n==0 ){.     
291c9 20 20 20 2f 2a 20 54 68 69 73 20 69 6e 64 69 63     /* This indic
291ca 61 74 65 73 20 74 68 61 74 20 6e 6f 20 64 61 74  ates that no dat
291cb 61 62 61 73 65 20 6e 61 6d 65 20 77 61 73 20 73  abase name was s
291cc 70 65 63 69 66 69 65 64 20 61 73 20 70 61 72 74  pecified as part
291cd 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74  .        ** of t
291ce 68 65 20 50 52 41 47 4d 41 20 63 6f 6d 6d 61 6e  he PRAGMA comman
291cf 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  d. In this case 
291d0 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65  the journal-mode
291d1 20 6d 75 73 74 20 62 65 0a 20 20 20 20 20 20 20   must be.       
291d2 20 2a 2a 20 73 65 74 20 6f 6e 20 61 6c 6c 20 61   ** set on all a
291d3 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
291d4 73 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 74 68  s, as well as th
291d5 65 20 6d 61 69 6e 20 64 62 20 66 69 6c 65 2e 0a  e main db file..
291d6 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
291d7 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 74 68 65 20     ** Also, the 
291d8 73 71 6c 69 74 65 33 2e 64 66 6c 74 4a 6f 75 72  sqlite3.dfltJour
291d9 6e 61 6c 4d 6f 64 65 20 76 61 72 69 61 62 6c 65  nalMode variable
291da 20 69 73 20 73 65 74 20 73 6f 20 74 68 61 74 0a   is set so that.
291db 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 20 73          ** any s
291dc 75 62 73 65 71 75 65 6e 74 6c 79 20 61 74 74 61  ubsequently atta
291dd 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20 61  ched databases a
291de 6c 73 6f 20 75 73 65 20 74 68 65 20 73 70 65 63  lso use the spec
291df 69 66 69 65 64 0a 20 20 20 20 20 20 20 20 2a 2a  ified.        **
291e0 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 2e 0a 20   journal mode.. 
291e1 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
291e2 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20    int ii;.      
291e3 20 20 61 73 73 65 72 74 28 70 44 62 3d 3d 26 64    assert(pDb==&d
291e4 62 2d 3e 61 44 62 5b 30 5d 29 3b 0a 20 20 20 20  b->aDb[0]);.    
291e5 20 20 20 20 66 6f 72 28 69 69 3d 31 3b 20 69 69      for(ii=1; ii
291e6 3c 64 62 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b  <db->nDb; ii++){
291e7 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 64  .          if( d
291e8 62 2d 3e 61 44 62 5b 69 69 5d 2e 70 42 74 20 29  b->aDb[ii].pBt )
291e9 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50  {.            pP
291ea 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74  ager = sqlite3Bt
291eb 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61 44 62  reePager(db->aDb
291ec 5b 69 69 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20  [ii].pBt);.     
291ed 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
291ee 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 70  gerJournalMode(p
291ef 50 61 67 65 72 2c 20 65 4d 6f 64 65 29 3b 0a 20  Pager, eMode);. 
291f0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
291f1 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 62 2d     }.        db-
291f2 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65  >dfltJournalMode
291f3 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20   = (u8)eMode;.  
291f4 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67      }.      pPag
291f5 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
291f6 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42 74 29  ePager(pDb->pBt)
291f7 3b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20  ;.      eMode = 
291f8 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72  sqlite3PagerJour
291f9 6e 61 6c 4d 6f 64 65 28 70 50 61 67 65 72 2c 20  nalMode(pPager, 
291fa 65 4d 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20  eMode);.    }.  
291fb 20 20 61 73 73 65 72 74 28 20 65 4d 6f 64 65 3d    assert( eMode=
291fc 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
291fd 44 45 5f 44 45 4c 45 54 45 0a 20 20 20 20 20 20  DE_DELETE.      
291fe 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65          || eMode
291ff 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
29200 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a 20 20 20  ODE_TRUNCATE.   
29201 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d             || eM
29202 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
29203 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20  ALMODE_PERSIST. 
29204 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
29205 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  eMode==PAGER_JOU
29206 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a 20 20 20  RNALMODE_OFF.   
29207 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d             || eM
29208 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
29209 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b  ALMODE_MEMORY );
2920a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2920b 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
2920c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2920d 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
2920e 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
2920f 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 22 2c 20  "journal_mode", 
29210 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
29211 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29212 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
29213 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 0a 20  ng8, 0, 1, 0, . 
29214 20 20 20 20 20 20 20 20 20 20 61 7a 4d 6f 64 65            azMode
29215 4e 61 6d 65 5b 65 4d 6f 64 65 5d 2c 20 50 34 5f  Name[eMode], P4_
29216 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c  STATIC);.    sql
29217 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
29218 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  , OP_ResultRow, 
29219 31 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a  1, 1);.  }else..
2921a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d    /*.  **  PRAGM
2921b 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6a 6f 75  A [database.]jou
2921c 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 0a  rnal_size_limit.
2921d 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
2921e 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f  tabase.]journal_
2921f 73 69 7a 65 5f 6c 69 6d 69 74 3d 4e 0a 20 20 2a  size_limit=N.  *
29220 2a 0a 20 20 2a 2a 20 47 65 74 20 6f 72 20 73 65  *.  ** Get or se
29221 74 20 74 68 65 20 73 69 7a 65 20 6c 69 6d 69 74  t the size limit
29222 20 6f 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75   on rollback jou
29223 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 20 20 2a 2f  rnal files..  */
29224 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
29225 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6a 6f 75  rICmp(zLeft,"jou
29226 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 22  rnal_size_limit"
29227 29 3d 3d 30 20 29 7b 0a 20 20 20 20 50 61 67 65  )==0 ){.    Page
29228 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c 69  r *pPager = sqli
29229 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 44  te3BtreePager(pD
2922a 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 69 36 34  b->pBt);.    i64
2922b 20 69 4c 69 6d 69 74 20 3d 20 2d 32 3b 0a 20 20   iLimit = -2;.  
2922c 20 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0a    if( zRight ){.
2922d 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f        sqlite3Ato
2922e 69 36 34 28 7a 52 69 67 68 74 2c 20 26 69 4c 69  i64(zRight, &iLi
2922f 6d 69 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  mit);.      if( 
29230 69 4c 69 6d 69 74 3c 2d 31 20 29 20 69 4c 69 6d  iLimit<-1 ) iLim
29231 69 74 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20  it = -1;.    }. 
29232 20 20 20 69 4c 69 6d 69 74 20 3d 20 73 71 6c 69     iLimit = sqli
29233 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 53  te3PagerJournalS
29234 69 7a 65 4c 69 6d 69 74 28 70 50 61 67 65 72 2c  izeLimit(pPager,
29235 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 72 65   iLimit);.    re
29236 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50  turnSingleInt(pP
29237 61 72 73 65 2c 20 22 6a 6f 75 72 6e 61 6c 5f 73  arse, "journal_s
29238 69 7a 65 5f 6c 69 6d 69 74 22 2c 20 69 4c 69 6d  ize_limit", iLim
29239 69 74 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a 23 65  it);.  }else..#e
2923a 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2923b 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
2923c 53 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  S */..  /*.  ** 
2923d 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
2923e 65 2e 5d 61 75 74 6f 5f 76 61 63 75 75 6d 0a 20  e.]auto_vacuum. 
2923f 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
29240 61 62 61 73 65 2e 5d 61 75 74 6f 5f 76 61 63 75  abase.]auto_vacu
29241 75 6d 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 47  um=N.  **.  ** G
29242 65 74 20 6f 72 20 73 65 74 20 74 68 65 20 76 61  et or set the va
29243 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  lue of the datab
29244 61 73 65 20 27 61 75 74 6f 2d 76 61 63 75 75 6d  ase 'auto-vacuum
29245 27 20 70 61 72 61 6d 65 74 65 72 2e 0a 20 20 2a  ' parameter..  *
29246 2a 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 6f  * The value is o
29247 6e 65 20 6f 66 3a 20 20 30 20 4e 4f 4e 45 20 31  ne of:  0 NONE 1
29248 20 46 55 4c 4c 20 32 20 49 4e 43 52 45 4d 45 4e   FULL 2 INCREMEN
29249 54 41 4c 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66  TAL.  */.#ifndef
2924a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
2924b 4f 56 41 43 55 55 4d 0a 20 20 69 66 28 20 73 71  OVACUUM.  if( sq
2924c 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
2924d 66 74 2c 22 61 75 74 6f 5f 76 61 63 75 75 6d 22  ft,"auto_vacuum"
2924e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 42 74 72 65  )==0 ){.    Btre
2924f 65 20 2a 70 42 74 20 3d 20 70 44 62 2d 3e 70 42  e *pBt = pDb->pB
29250 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  t;.    assert( p
29251 42 74 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  Bt!=0 );.    if(
29252 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65   sqlite3ReadSche
29253 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20  ma(pParse) ){.  
29254 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f      goto pragma_
29255 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  out;.    }.    i
29256 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20  f( !zRight ){.  
29257 20 20 20 20 69 6e 74 20 61 75 74 6f 5f 76 61 63      int auto_vac
29258 75 75 6d 3b 0a 20 20 20 20 20 20 69 66 28 20 41  uum;.      if( A
29259 4c 57 41 59 53 28 70 42 74 29 20 29 7b 0a 20 20  LWAYS(pBt) ){.  
2925a 20 20 20 20 20 20 20 61 75 74 6f 5f 76 61 63 75         auto_vacu
2925b 75 6d 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  um = sqlite3Btre
2925c 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70  eGetAutoVacuum(p
2925d 42 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  Bt);.      }else
2925e 7b 0a 20 20 20 20 20 20 20 20 20 61 75 74 6f 5f  {.         auto_
2925f 76 61 63 75 75 6d 20 3d 20 53 51 4c 49 54 45 5f  vacuum = SQLITE_
29260 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43 55  DEFAULT_AUTOVACU
29261 55 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  UM;.      }.    
29262 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e    returnSingleIn
29263 74 28 70 50 61 72 73 65 2c 20 22 61 75 74 6f 5f  t(pParse, "auto_
29264 76 61 63 75 75 6d 22 2c 20 61 75 74 6f 5f 76 61  vacuum", auto_va
29265 63 75 75 6d 29 3b 0a 20 20 20 20 7d 65 6c 73 65  cuum);.    }else
29266 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 41 75 74  {.      int eAut
29267 6f 20 3d 20 67 65 74 41 75 74 6f 56 61 63 75 75  o = getAutoVacuu
29268 6d 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20  m(zRight);.     
29269 20 61 73 73 65 72 74 28 20 65 41 75 74 6f 3e 3d   assert( eAuto>=
2926a 30 20 26 26 20 65 41 75 74 6f 3c 3d 32 20 29 3b  0 && eAuto<=2 );
2926b 0a 20 20 20 20 20 20 64 62 2d 3e 6e 65 78 74 41  .      db->nextA
2926c 75 74 6f 76 61 63 20 3d 20 28 75 38 29 65 41 75  utovac = (u8)eAu
2926d 74 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 41 4c  to;.      if( AL
2926e 57 41 59 53 28 65 41 75 74 6f 3e 3d 30 29 20 29  WAYS(eAuto>=0) )
2926f 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6c  {.        /* Cal
29270 6c 20 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28  l SetAutoVacuum(
29271 29 20 74 6f 20 73 65 74 20 69 6e 69 74 69 61 6c  ) to set initial
29272 69 7a 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  ize the internal
29273 20 61 75 74 6f 20 61 6e 64 0a 20 20 20 20 20 20   auto and.      
29274 20 20 2a 2a 20 69 6e 63 72 2d 76 61 63 75 75 6d    ** incr-vacuum
29275 20 66 6c 61 67 73 2e 20 54 68 69 73 20 69 73 20   flags. This is 
29276 72 65 71 75 69 72 65 64 20 69 6e 20 63 61 73 65  required in case
29277 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e   this connection
29278 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 72 65 61  .        ** crea
29279 74 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65  tes the database
2927a 20 66 69 6c 65 2e 20 49 74 20 69 73 20 69 6d 70   file. It is imp
2927b 6f 72 74 61 6e 74 20 74 68 61 74 20 69 74 20 69  ortant that it i
2927c 73 20 63 72 65 61 74 65 64 0a 20 20 20 20 20 20  s created.      
2927d 20 20 2a 2a 20 61 73 20 61 6e 20 61 75 74 6f 2d    ** as an auto-
2927e 76 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20 64  vacuum capable d
2927f 62 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  b..        */.  
29280 20 20 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73        int rc = s
29281 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75  qlite3BtreeSetAu
29282 74 6f 56 61 63 75 75 6d 28 70 42 74 2c 20 65 41  toVacuum(pBt, eA
29283 75 74 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66  uto);.        if
29284 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
29285 26 26 20 28 65 41 75 74 6f 3d 3d 31 20 7c 7c 20  && (eAuto==1 || 
29286 65 41 75 74 6f 3d 3d 32 29 20 29 7b 0a 20 20 20  eAuto==2) ){.   
29287 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 73         /* When s
29288 65 74 74 69 6e 67 20 74 68 65 20 61 75 74 6f 5f  etting the auto_
29289 76 61 63 75 75 6d 20 6d 6f 64 65 20 74 6f 20 65  vacuum mode to e
2928a 69 74 68 65 72 20 22 66 75 6c 6c 22 20 6f 72 20  ither "full" or 
2928b 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 22 69  .          ** "i
2928c 6e 63 72 65 6d 65 6e 74 61 6c 22 2c 20 77 72 69  ncremental", wri
2928d 74 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  te the value of 
2928e 6d 65 74 61 5b 36 5d 20 69 6e 20 74 68 65 20 64  meta[6] in the d
2928f 61 74 61 62 61 73 65 0a 20 20 20 20 20 20 20 20  atabase.        
29290 20 20 2a 2a 20 66 69 6c 65 2e 20 42 65 66 6f 72    ** file. Befor
29291 65 20 77 72 69 74 69 6e 67 20 74 6f 20 6d 65 74  e writing to met
29292 61 5b 36 5d 2c 20 63 68 65 63 6b 20 74 68 61 74  a[6], check that
29293 20 6d 65 74 61 5b 33 5d 20 69 6e 64 69 63 61 74   meta[3] indicat
29294 65 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  es.          ** 
29295 74 68 61 74 20 74 68 69 73 20 72 65 61 6c 6c 79  that this really
29296 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75   is an auto-vacu
29297 75 6d 20 63 61 70 61 62 6c 65 20 64 61 74 61 62  um capable datab
29298 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  ase..          *
29299 2f 0a 20 20 20 20 20 20 20 20 20 20 73 74 61 74  /.          stat
2929a 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c  ic const VdbeOpL
2929b 69 73 74 20 73 65 74 4d 65 74 61 36 5b 5d 20 3d  ist setMeta6[] =
2929c 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b   {.            {
2929d 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c   OP_Transaction,
2929e 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 31      0,         1
2929f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
292a0 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f    0},    /* 0 */
292a1 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
292a2 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 20 20  P_ReadCookie,   
292a3 20 20 30 2c 20 20 20 20 20 20 20 20 20 31 2c 20    0,         1, 
292a4 20 20 20 20 20 20 20 20 42 54 52 45 45 5f 4c 41          BTREE_LA
292a5 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 7d  RGEST_ROOT_PAGE}
292a6 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  ,.            { 
292a7 4f 50 5f 49 66 2c 20 20 20 20 20 20 20 20 20 20  OP_If,          
292a8 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 30 2c     1,         0,
292a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
292aa 20 30 7d 2c 20 20 20 20 2f 2a 20 32 20 2a 2f 0a   0},    /* 2 */.
292ab 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
292ac 5f 48 61 6c 74 2c 20 20 20 20 20 20 20 20 20 20  _Halt,          
292ad 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 4f 45 5f 41   SQLITE_OK, OE_A
292ae 62 6f 72 74 2c 20 20 20 20 20 20 20 20 20 20 30  bort,          0
292af 7d 2c 20 20 20 20 2f 2a 20 33 20 2a 2f 0a 20 20  },    /* 3 */.  
292b0 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49            { OP_I
292b1 6e 74 65 67 65 72 2c 20 20 20 20 20 20 20 20 30  nteger,        0
292b2 2c 20 20 20 20 20 20 20 20 20 31 2c 20 20 20 20  ,         1,    
292b3 20 20 20 20 20 20 20 20 20 20 20 20 20 30 7d 2c               0},
292b4 20 20 20 20 2f 2a 20 34 20 2a 2f 0a 20 20 20 20      /* 4 */.    
292b5 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 65 74          { OP_Set
292b6 43 6f 6f 6b 69 65 2c 20 20 20 20 20 20 30 2c 20  Cookie,      0, 
292b7 20 20 20 20 20 20 20 20 42 54 52 45 45 5f 49 4e          BTREE_IN
292b8 43 52 5f 56 41 43 55 55 4d 2c 20 31 7d 2c 20 20  CR_VACUUM, 1},  
292b9 20 20 2f 2a 20 35 20 2a 2f 0a 20 20 20 20 20 20    /* 5 */.      
292ba 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 20 20 20      };.         
292bb 20 69 6e 74 20 69 41 64 64 72 3b 0a 20 20 20 20   int iAddr;.    
292bc 20 20 20 20 20 20 69 41 64 64 72 20 3d 20 73 71        iAddr = sq
292bd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
292be 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28  st(v, ArraySize(
292bf 73 65 74 4d 65 74 61 36 29 2c 20 73 65 74 4d 65  setMeta6), setMe
292c0 74 61 36 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ta6);.          
292c1 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
292c2 65 50 31 28 76 2c 20 69 41 64 64 72 2c 20 69 44  eP1(v, iAddr, iD
292c3 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  b);.          sq
292c4 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
292c5 31 28 76 2c 20 69 41 64 64 72 2b 31 2c 20 69 44  1(v, iAddr+1, iD
292c6 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  b);.          sq
292c7 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
292c8 32 28 76 2c 20 69 41 64 64 72 2b 32 2c 20 69 41  2(v, iAddr+2, iA
292c9 64 64 72 2b 34 29 3b 0a 20 20 20 20 20 20 20 20  ddr+4);.        
292ca 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
292cb 6e 67 65 50 31 28 76 2c 20 69 41 64 64 72 2b 34  ngeP1(v, iAddr+4
292cc 2c 20 65 41 75 74 6f 2d 31 29 3b 0a 20 20 20 20  , eAuto-1);.    
292cd 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
292ce 65 43 68 61 6e 67 65 50 31 28 76 2c 20 69 41 64  eChangeP1(v, iAd
292cf 64 72 2b 35 2c 20 69 44 62 29 3b 0a 20 20 20 20  dr+5, iDb);.    
292d0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
292d1 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44  eUsesBtree(v, iD
292d2 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  b);.        }.  
292d3 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
292d4 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a  lse.#endif..  /*
292d5 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
292d6 61 74 61 62 61 73 65 2e 5d 69 6e 63 72 65 6d 65  atabase.]increme
292d7 6e 74 61 6c 5f 76 61 63 75 75 6d 28 4e 29 0a 20  ntal_vacuum(N). 
292d8 20 2a 2a 0a 20 20 2a 2a 20 44 6f 20 4e 20 73 74   **.  ** Do N st
292d9 65 70 73 20 6f 66 20 69 6e 63 72 65 6d 65 6e 74  eps of increment
292da 61 6c 20 76 61 63 75 75 6d 69 6e 67 20 6f 6e 20  al vacuuming on 
292db 61 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f  a database..  */
292dc 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
292dd 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
292de 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
292df 49 43 6d 70 28 7a 4c 65 66 74 2c 22 69 6e 63 72  ICmp(zLeft,"incr
292e0 65 6d 65 6e 74 61 6c 5f 76 61 63 75 75 6d 22 29  emental_vacuum")
292e1 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  ==0 ){.    int i
292e2 4c 69 6d 69 74 2c 20 61 64 64 72 3b 0a 20 20 20  Limit, addr;.   
292e3 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64   if( sqlite3Read
292e4 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
292e5 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 72 61  {.      goto pra
292e6 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  gma_out;.    }. 
292e7 20 20 20 69 66 28 20 7a 52 69 67 68 74 3d 3d 30     if( zRight==0
292e8 20 7c 7c 20 21 73 71 6c 69 74 65 33 47 65 74 49   || !sqlite3GetI
292e9 6e 74 33 32 28 7a 52 69 67 68 74 2c 20 26 69 4c  nt32(zRight, &iL
292ea 69 6d 69 74 29 20 7c 7c 20 69 4c 69 6d 69 74 3c  imit) || iLimit<
292eb 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 4c 69 6d  =0 ){.      iLim
292ec 69 74 20 3d 20 30 78 37 66 66 66 66 66 66 66 3b  it = 0x7fffffff;
292ed 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
292ee 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
292ef 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c  ation(pParse, 0,
292f0 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74   iDb);.    sqlit
292f1 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
292f2 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 4c 69 6d  OP_Integer, iLim
292f3 69 74 2c 20 31 29 3b 0a 20 20 20 20 61 64 64 72  it, 1);.    addr
292f4 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
292f5 64 4f 70 31 28 76 2c 20 4f 50 5f 49 6e 63 72 56  dOp1(v, OP_IncrV
292f6 61 63 75 75 6d 2c 20 69 44 62 29 3b 0a 20 20 20  acuum, iDb);.   
292f7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
292f8 70 31 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p1(v, OP_ResultR
292f9 6f 77 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  ow, 1);.    sqli
292fa 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
292fb 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 31 2c 20 2d   OP_AddImm, 1, -
292fc 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
292fd 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
292fe 49 66 50 6f 73 2c 20 31 2c 20 61 64 64 72 29 3b  IfPos, 1, addr);
292ff 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
29300 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
29301 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69  );.  }else.#endi
29302 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
29303 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
29304 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20  GMAS.  /*.  **  
29305 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
29306 2e 5d 63 61 63 68 65 5f 73 69 7a 65 0a 20 20 2a  .]cache_size.  *
29307 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62  *  PRAGMA [datab
29308 61 73 65 2e 5d 63 61 63 68 65 5f 73 69 7a 65 3d  ase.]cache_size=
29309 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  N.  **.  ** The 
2930a 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72  first form repor
2930b 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c  ts the current l
2930c 6f 63 61 6c 20 73 65 74 74 69 6e 67 20 66 6f 72  ocal setting for
2930d 20 74 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 63   the.  ** page c
2930e 61 63 68 65 20 73 69 7a 65 2e 20 20 54 68 65 20  ache size.  The 
2930f 6c 6f 63 61 6c 20 73 65 74 74 69 6e 67 20 63 61  local setting ca
29310 6e 20 62 65 20 64 69 66 66 65 72 65 6e 74 20 66  n be different f
29311 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 70 65 72  rom.  ** the per
29312 73 69 73 74 65 6e 74 20 63 61 63 68 65 20 73 69  sistent cache si
29313 7a 65 20 76 61 6c 75 65 20 74 68 61 74 20 69 73  ze value that is
29314 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64   stored in the d
29315 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c  atabase.  ** fil
29316 65 20 69 74 73 65 6c 66 2e 20 20 54 68 65 20 76  e itself.  The v
29317 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73  alue returned is
29318 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   the maximum num
29319 62 65 72 20 6f 66 0a 20 20 2a 2a 20 70 61 67 65  ber of.  ** page
2931a 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61  s in the page ca
2931b 63 68 65 2e 20 20 54 68 65 20 73 65 63 6f 6e 64  che.  The second
2931c 20 66 6f 72 6d 20 73 65 74 73 20 74 68 65 20 6c   form sets the l
2931d 6f 63 61 6c 0a 20 20 2a 2a 20 70 61 67 65 20 63  ocal.  ** page c
2931e 61 63 68 65 20 73 69 7a 65 20 76 61 6c 75 65 2e  ache size value.
2931f 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 63 68    It does not ch
29320 61 6e 67 65 20 74 68 65 20 70 65 72 73 69 73 74  ange the persist
29321 65 6e 74 0a 20 20 2a 2a 20 63 61 63 68 65 20 73  ent.  ** cache s
29322 69 7a 65 20 73 74 6f 72 65 64 20 6f 6e 20 74 68  ize stored on th
29323 65 20 64 69 73 6b 20 73 6f 20 74 68 65 20 63 61  e disk so the ca
29324 63 68 65 20 73 69 7a 65 20 77 69 6c 6c 20 72 65  che size will re
29325 76 65 72 74 0a 20 20 2a 2a 20 74 6f 20 69 74 73  vert.  ** to its
29326 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 77   default value w
29327 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
29328 20 69 73 20 63 6c 6f 73 65 64 20 61 6e 64 20 72   is closed and r
29329 65 6f 70 65 6e 65 64 2e 0a 20 20 2a 2a 20 4e 20  eopened..  ** N 
2932a 73 68 6f 75 6c 64 20 62 65 20 61 20 70 6f 73 69  should be a posi
2932b 74 69 76 65 20 69 6e 74 65 67 65 72 2e 0a 20 20  tive integer..  
2932c 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
2932d 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 63  StrICmp(zLeft,"c
2932e 61 63 68 65 5f 73 69 7a 65 22 29 3d 3d 30 20 29  ache_size")==0 )
2932f 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
29330 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
29331 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d  se) ) goto pragm
29332 61 5f 6f 75 74 3b 0a 20 20 20 20 69 66 28 20 21  a_out;.    if( !
29333 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  zRight ){.      
29334 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28  returnSingleInt(
29335 70 50 61 72 73 65 2c 20 22 63 61 63 68 65 5f 73  pParse, "cache_s
29336 69 7a 65 22 2c 20 70 44 62 2d 3e 70 53 63 68 65  ize", pDb->pSche
29337 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 29 3b  ma->cache_size);
29338 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
29339 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 61 74 6f    int size = ato
2933a 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20  i(zRight);.     
2933b 20 69 66 28 20 73 69 7a 65 3c 30 20 29 20 73 69   if( size<0 ) si
2933c 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20 20 20 20  ze = -size;.    
2933d 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e    pDb->pSchema->
2933e 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 73 69 7a  cache_size = siz
2933f 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e;.      sqlite3
29340 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a  BtreeSetCacheSiz
29341 65 28 70 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d  e(pDb->pBt, pDb-
29342 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f  >pSchema->cache_
29343 73 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  size);.    }.  }
29344 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  else..  /*.  ** 
29345 20 20 50 52 41 47 4d 41 20 74 65 6d 70 5f 73 74    PRAGMA temp_st
29346 6f 72 65 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d  ore.  **   PRAGM
29347 41 20 74 65 6d 70 5f 73 74 6f 72 65 20 3d 20 22  A temp_store = "
29348 64 65 66 61 75 6c 74 22 7c 22 6d 65 6d 6f 72 79  default"|"memory
29349 22 7c 22 66 69 6c 65 22 0a 20 20 2a 2a 0a 20 20  "|"file".  **.  
2934a 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20 73 65 74  ** Return or set
2934b 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65   the local value
2934c 20 6f 66 20 74 68 65 20 74 65 6d 70 5f 73 74 6f   of the temp_sto
2934d 72 65 20 66 6c 61 67 2e 20 20 43 68 61 6e 67 69  re flag.  Changi
2934e 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6c 6f 63 61  ng.  ** the loca
2934f 6c 20 76 61 6c 75 65 20 64 6f 65 73 20 6e 6f 74  l value does not
29350 20 6d 61 6b 65 20 63 68 61 6e 67 65 73 20 74 6f   make changes to
29351 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65 20 61   the disk file a
29352 6e 64 20 74 68 65 20 64 65 66 61 75 6c 74 0a 20  nd the default. 
29353 20 2a 2a 20 76 61 6c 75 65 20 77 69 6c 6c 20 62   ** value will b
29354 65 20 72 65 73 74 6f 72 65 64 20 74 68 65 20 6e  e restored the n
29355 65 78 74 20 74 69 6d 65 20 74 68 65 20 64 61 74  ext time the dat
29356 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 2e  abase is opened.
29357 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20  .  **.  ** Note 
29358 74 68 61 74 20 69 74 20 69 73 20 70 6f 73 73 69  that it is possi
29359 62 6c 65 20 66 6f 72 20 74 68 65 20 6c 69 62 72  ble for the libr
2935a 61 72 79 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  ary compile-time
2935b 20 6f 70 74 69 6f 6e 73 20 74 6f 0a 20 20 2a 2a   options to.  **
2935c 20 6f 76 65 72 72 69 64 65 20 74 68 69 73 20 73   override this s
2935d 65 74 74 69 6e 67 0a 20 20 2a 2f 0a 20 20 69 66  etting.  */.  if
2935e 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
2935f 28 7a 4c 65 66 74 2c 20 22 74 65 6d 70 5f 73 74  (zLeft, "temp_st
29360 6f 72 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ore")==0 ){.    
29361 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a 20  if( !zRight ){. 
29362 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c       returnSingl
29363 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22 74 65  eInt(pParse, "te
29364 6d 70 5f 73 74 6f 72 65 22 2c 20 64 62 2d 3e 74  mp_store", db->t
29365 65 6d 70 5f 73 74 6f 72 65 29 3b 0a 20 20 20 20  emp_store);.    
29366 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 68 61  }else{.      cha
29367 6e 67 65 54 65 6d 70 53 74 6f 72 61 67 65 28 70  ngeTempStorage(p
29368 50 61 72 73 65 2c 20 7a 52 69 67 68 74 29 3b 0a  Parse, zRight);.
29369 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20      }.  }else.. 
2936a 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d   /*.  **   PRAGM
2936b 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72  A temp_store_dir
2936c 65 63 74 6f 72 79 0a 20 20 2a 2a 20 20 20 50 52  ectory.  **   PR
2936d 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f  AGMA temp_store_
2936e 64 69 72 65 63 74 6f 72 79 20 3d 20 22 22 7c 22  directory = ""|"
2936f 64 69 72 65 63 74 6f 72 79 5f 6e 61 6d 65 22 0a  directory_name".
29370 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e    **.  ** Return
29371 20 6f 72 20 73 65 74 20 74 68 65 20 6c 6f 63 61   or set the loca
29372 6c 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 74  l value of the t
29373 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74  emp_store_direct
29374 6f 72 79 20 66 6c 61 67 2e 20 20 43 68 61 6e 67  ory flag.  Chang
29375 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 76 61 6c  ing.  ** the val
29376 75 65 20 73 65 74 73 20 61 20 73 70 65 63 69 66  ue sets a specif
29377 69 63 20 64 69 72 65 63 74 6f 72 79 20 74 6f 20  ic directory to 
29378 62 65 20 75 73 65 64 20 66 6f 72 20 74 65 6d 70  be used for temp
29379 6f 72 61 72 79 20 66 69 6c 65 73 2e 0a 20 20 2a  orary files..  *
2937a 2a 20 53 65 74 74 69 6e 67 20 74 6f 20 61 20 6e  * Setting to a n
2937b 75 6c 6c 20 73 74 72 69 6e 67 20 72 65 76 65 72  ull string rever
2937c 74 73 20 74 6f 20 74 68 65 20 64 65 66 61 75 6c  ts to the defaul
2937d 74 20 74 65 6d 70 6f 72 61 72 79 20 64 69 72 65  t temporary dire
2937e 63 74 6f 72 79 20 73 65 61 72 63 68 2e 0a 20 20  ctory search..  
2937f 2a 2a 20 49 66 20 74 65 6d 70 6f 72 61 72 79 20  ** If temporary 
29380 64 69 72 65 63 74 6f 72 79 20 69 73 20 63 68 61  directory is cha
29381 6e 67 65 64 2c 20 74 68 65 6e 20 69 6e 76 61 6c  nged, then inval
29382 69 64 61 74 65 54 65 6d 70 53 74 6f 72 61 67 65  idateTempStorage
29383 2e 0a 20 20 2a 2a 0a 20 20 2a 2f 0a 20 20 69 66  ..  **.  */.  if
29384 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
29385 28 7a 4c 65 66 74 2c 20 22 74 65 6d 70 5f 73 74  (zLeft, "temp_st
29386 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 22 29 3d  ore_directory")=
29387 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 7a  =0 ){.    if( !z
29388 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 69  Right ){.      i
29389 66 28 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f  f( sqlite3_temp_
2938a 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20  directory ){.   
2938b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2938c 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
2938d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2938e 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
2938f 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 0, COLNAME_NA
29390 4d 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  ME, .           
29391 20 22 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72   "temp_store_dir
29392 65 63 74 6f 72 79 22 2c 20 53 51 4c 49 54 45 5f  ectory", SQLITE_
29393 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
29394 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29395 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
29396 2c 20 30 2c 20 31 2c 20 30 2c 20 73 71 6c 69 74  , 0, 1, 0, sqlit
29397 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72  e3_temp_director
29398 79 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  y, 0);.        s
29399 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2939a 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
2939b 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d  , 1, 1);.      }
2939c 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 23 69 66 6e  .    }else{.#ifn
2939d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2939e 57 53 44 0a 20 20 20 20 20 20 69 66 28 20 7a 52  WSD.      if( zR
2939f 69 67 68 74 5b 30 5d 20 29 7b 0a 20 20 20 20 20  ight[0] ){.     
293a0 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20     int rc;.     
293a1 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20     int res;.    
293a2 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
293a3 4f 73 41 63 63 65 73 73 28 64 62 2d 3e 70 56 66  OsAccess(db->pVf
293a4 73 2c 20 7a 52 69 67 68 74 2c 20 53 51 4c 49 54  s, zRight, SQLIT
293a5 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49  E_ACCESS_READWRI
293a6 54 45 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20  TE, &res);.     
293a7 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
293a8 45 5f 4f 4b 20 7c 7c 20 72 65 73 3d 3d 30 20 29  E_OK || res==0 )
293a9 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
293aa 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
293ab 73 65 2c 20 22 6e 6f 74 20 61 20 77 72 69 74 61  se, "not a writa
293ac 62 6c 65 20 64 69 72 65 63 74 6f 72 79 22 29 3b  ble directory");
293ad 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
293ae 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20  pragma_out;.    
293af 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
293b0 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 54      if( SQLITE_T
293b1 45 4d 50 5f 53 54 4f 52 45 3d 3d 30 0a 20 20 20  EMP_STORE==0.   
293b2 20 20 20 20 7c 7c 20 28 53 51 4c 49 54 45 5f 54      || (SQLITE_T
293b3 45 4d 50 5f 53 54 4f 52 45 3d 3d 31 20 26 26 20  EMP_STORE==1 && 
293b4 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3c 3d  db->temp_store<=
293b5 31 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 53 51  1).       || (SQ
293b6 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d  LITE_TEMP_STORE=
293b7 3d 32 20 26 26 20 64 62 2d 3e 74 65 6d 70 5f 73  =2 && db->temp_s
293b8 74 6f 72 65 3d 3d 31 29 0a 20 20 20 20 20 20 29  tore==1).      )
293b9 7b 0a 20 20 20 20 20 20 20 20 69 6e 76 61 6c 69  {.        invali
293ba 64 61 74 65 54 65 6d 70 53 74 6f 72 61 67 65 28  dateTempStorage(
293bb 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 7d  pParse);.      }
293bc 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
293bd 72 65 65 28 73 71 6c 69 74 65 33 5f 74 65 6d 70  ree(sqlite3_temp
293be 5f 64 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 20  _directory);.   
293bf 20 20 20 69 66 28 20 7a 52 69 67 68 74 5b 30 5d     if( zRight[0]
293c0 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
293c1 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f  te3_temp_directo
293c2 72 79 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  ry = sqlite3DbSt
293c3 72 44 75 70 28 30 2c 20 7a 52 69 67 68 74 29 3b  rDup(0, zRight);
293c4 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
293c5 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 74 65        sqlite3_te
293c6 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 3d 20 30  mp_directory = 0
293c7 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  ;.      }.#endif
293c8 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
293c9 57 53 44 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d  WSD */.    }.  }
293ca 65 6c 73 65 0a 0a 23 69 66 20 21 64 65 66 69 6e  else..#if !defin
293cb 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
293cc 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 29 0a  _LOCKING_STYLE).
293cd 23 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 5f  #  if defined(__
293ce 41 50 50 4c 45 5f 5f 29 0a 23 20 20 20 20 64 65  APPLE__).#    de
293cf 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42  fine SQLITE_ENAB
293d0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
293d1 20 31 0a 23 20 20 65 6c 73 65 0a 23 20 20 20 20   1.#  else.#    
293d2 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e  define SQLITE_EN
293d3 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
293d4 4c 45 20 30 0a 23 20 20 65 6e 64 69 66 0a 23 65  LE 0.#  endif.#e
293d5 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
293d6 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
293d7 54 59 4c 45 0a 20 20 2f 2a 0a 20 20 20 2a 2a 20  TYLE.  /*.   ** 
293d8 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
293d9 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66  se.]lock_proxy_f
293da 69 6c 65 0a 20 20 20 2a 2a 20 20 20 50 52 41 47  ile.   **   PRAG
293db 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f  MA [database.]lo
293dc 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 20 3d 20  ck_proxy_file = 
293dd 22 3a 61 75 74 6f 3a 22 7c 22 6c 6f 63 6b 5f 66  ":auto:"|"lock_f
293de 69 6c 65 5f 70 61 74 68 22 0a 20 20 20 2a 2a 0a  ile_path".   **.
293df 20 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20     ** Return or 
293e0 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66  set the value of
293e1 20 74 68 65 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f   the lock_proxy_
293e2 66 69 6c 65 20 66 6c 61 67 2e 20 20 43 68 61 6e  file flag.  Chan
293e3 67 69 6e 67 0a 20 20 20 2a 2a 20 74 68 65 20 76  ging.   ** the v
293e4 61 6c 75 65 20 73 65 74 73 20 61 20 73 70 65 63  alue sets a spec
293e5 69 66 69 63 20 66 69 6c 65 20 74 6f 20 62 65 20  ific file to be 
293e6 75 73 65 64 20 66 6f 72 20 64 61 74 61 62 61 73  used for databas
293e7 65 20 61 63 63 65 73 73 20 6c 6f 63 6b 73 2e 0a  e access locks..
293e8 20 20 20 2a 2a 0a 20 20 20 2a 2f 0a 20 20 69 66     **.   */.  if
293e9 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
293ea 28 7a 4c 65 66 74 2c 20 22 6c 6f 63 6b 5f 70 72  (zLeft, "lock_pr
293eb 6f 78 79 5f 66 69 6c 65 22 29 3d 3d 30 20 29 7b  oxy_file")==0 ){
293ec 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74  .    if( !zRight
293ed 20 29 7b 0a 20 20 20 20 20 20 50 61 67 65 72 20   ){.      Pager 
293ee 2a 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65  *pPager = sqlite
293ef 33 42 74 72 65 65 50 61 67 65 72 28 70 44 62 2d  3BtreePager(pDb-
293f0 3e 70 42 74 29 3b 0a 20 20 20 20 20 20 63 68 61  >pBt);.      cha
293f1 72 20 2a 70 72 6f 78 79 5f 66 69 6c 65 5f 70 61  r *proxy_file_pa
293f2 74 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20  th = NULL;.     
293f3 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
293f4 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 50 61  File = sqlite3Pa
293f5 67 65 72 46 69 6c 65 28 70 50 61 67 65 72 29 3b  gerFile(pPager);
293f6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  .      sqlite3Os
293f7 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 70 46 69 6c  FileControl(pFil
293f8 65 2c 20 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f  e, SQLITE_GET_LO
293f9 43 4b 50 52 4f 58 59 46 49 4c 45 2c 20 0a 20 20  CKPROXYFILE, .  
293fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
293fb 20 20 20 20 20 20 20 20 20 26 70 72 6f 78 79 5f           &proxy_
293fc 66 69 6c 65 5f 70 61 74 68 29 3b 0a 20 20 20 20  file_path);.    
293fd 20 20 0a 20 20 20 20 20 20 69 66 28 20 70 72 6f    .      if( pro
293fe 78 79 5f 66 69 6c 65 5f 70 61 74 68 20 29 7b 0a  xy_file_path ){.
293ff 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29400 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
29401 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   1);.        sql
29402 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
29403 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
29404 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20 20 20 20  _NAME, .        
29405 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29406 20 20 20 20 20 20 22 6c 6f 63 6b 5f 70 72 6f 78        "lock_prox
29407 79 5f 66 69 6c 65 22 2c 20 53 51 4c 49 54 45 5f  y_file", SQLITE_
29408 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
29409 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2940a 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
2940b 2c 20 30 2c 20 31 2c 20 30 2c 20 70 72 6f 78 79  , 0, 1, 0, proxy
2940c 5f 66 69 6c 65 5f 70 61 74 68 2c 20 30 29 3b 0a  _file_path, 0);.
2940d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2940e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2940f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29  ResultRow, 1, 1)
29410 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
29411 6c 73 65 7b 0a 20 20 20 20 20 20 50 61 67 65 72  lse{.      Pager
29412 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74   *pPager = sqlit
29413 65 33 42 74 72 65 65 50 61 67 65 72 28 70 44 62  e3BtreePager(pDb
29414 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20 20 73 71  ->pBt);.      sq
29415 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c  lite3_file *pFil
29416 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  e = sqlite3Pager
29417 46 69 6c 65 28 70 50 61 67 65 72 29 3b 0a 20 20  File(pPager);.  
29418 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
29419 20 20 20 69 66 28 20 7a 52 69 67 68 74 5b 30 5d     if( zRight[0]
2941a 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73 3d   ){.        res=
2941b 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e  sqlite3OsFileCon
2941c 74 72 6f 6c 28 70 46 69 6c 65 2c 20 53 51 4c 49  trol(pFile, SQLI
2941d 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59  TE_SET_LOCKPROXY
2941e 46 49 4c 45 2c 20 0a 20 20 20 20 20 20 20 20 20  FILE, .         
2941f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29420 20 20 20 20 20 20 20 20 20 20 20 20 7a 52 69 67              zRig
29421 68 74 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73  ht);.      } els
29422 65 20 7b 0a 20 20 20 20 20 20 20 20 72 65 73 3d  e {.        res=
29423 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e  sqlite3OsFileCon
29424 74 72 6f 6c 28 70 46 69 6c 65 2c 20 53 51 4c 49  trol(pFile, SQLI
29425 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59  TE_SET_LOCKPROXY
29426 46 49 4c 45 2c 20 0a 20 20 20 20 20 20 20 20 20  FILE, .         
29427 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29428 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c              NULL
29429 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2942a 20 69 66 28 20 72 65 73 21 3d 53 51 4c 49 54 45   if( res!=SQLITE
2942b 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73  _OK ){.        s
2942c 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2942d 50 61 72 73 65 2c 20 22 66 61 69 6c 65 64 20 74  Parse, "failed t
2942e 6f 20 73 65 74 20 6c 6f 63 6b 20 70 72 6f 78 79  o set lock proxy
2942f 20 66 69 6c 65 22 29 3b 0a 20 20 20 20 20 20 20   file");.       
29430 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74   goto pragma_out
29431 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
29432 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f    }else.#endif /
29433 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
29434 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f  LOCKING_STYLE */
29435 20 20 20 20 20 20 0a 20 20 20 20 0a 20 20 2f 2a        .    .  /*
29436 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b  .  **   PRAGMA [
29437 64 61 74 61 62 61 73 65 2e 5d 73 79 6e 63 68 72  database.]synchr
29438 6f 6e 6f 75 73 0a 20 20 2a 2a 20 20 20 50 52 41  onous.  **   PRA
29439 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 73  GMA [database.]s
2943a 79 6e 63 68 72 6f 6e 6f 75 73 3d 4f 46 46 7c 4f  ynchronous=OFF|O
2943b 4e 7c 4e 4f 52 4d 41 4c 7c 46 55 4c 4c 0a 20 20  N|NORMAL|FULL.  
2943c 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f  **.  ** Return o
2943d 72 20 73 65 74 20 74 68 65 20 6c 6f 63 61 6c 20  r set the local 
2943e 76 61 6c 75 65 20 6f 66 20 74 68 65 20 73 79 6e  value of the syn
2943f 63 68 72 6f 6e 6f 75 73 20 66 6c 61 67 2e 20 20  chronous flag.  
29440 43 68 61 6e 67 69 6e 67 0a 20 20 2a 2a 20 74 68  Changing.  ** th
29441 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 20 64 6f  e local value do
29442 65 73 20 6e 6f 74 20 6d 61 6b 65 20 63 68 61 6e  es not make chan
29443 67 65 73 20 74 6f 20 74 68 65 20 64 69 73 6b 20  ges to the disk 
29444 66 69 6c 65 20 61 6e 64 20 74 68 65 0a 20 20 2a  file and the.  *
29445 2a 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20  * default value 
29446 77 69 6c 6c 20 62 65 20 72 65 73 74 6f 72 65 64  will be restored
29447 20 74 68 65 20 6e 65 78 74 20 74 69 6d 65 20 74   the next time t
29448 68 65 20 64 61 74 61 62 61 73 65 20 69 73 0a 20  he database is. 
29449 20 2a 2a 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f   ** opened..  */
2944a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
2944b 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 73 79 6e  rICmp(zLeft,"syn
2944c 63 68 72 6f 6e 6f 75 73 22 29 3d 3d 30 20 29 7b  chronous")==0 ){
2944d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
2944e 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
2944f 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61  e) ) goto pragma
29450 5f 6f 75 74 3b 0a 20 20 20 20 69 66 28 20 21 7a  _out;.    if( !z
29451 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 72  Right ){.      r
29452 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70  eturnSingleInt(p
29453 50 61 72 73 65 2c 20 22 73 79 6e 63 68 72 6f 6e  Parse, "synchron
29454 6f 75 73 22 2c 20 70 44 62 2d 3e 73 61 66 65 74  ous", pDb->safet
29455 79 5f 6c 65 76 65 6c 2d 31 29 3b 0a 20 20 20 20  y_level-1);.    
29456 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28  }else{.      if(
29457 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74   !db->autoCommit
29458 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
29459 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2945a 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  se, .           
2945b 20 22 53 61 66 65 74 79 20 6c 65 76 65 6c 20 6d   "Safety level m
2945c 61 79 20 6e 6f 74 20 62 65 20 63 68 61 6e 67 65  ay not be change
2945d 64 20 69 6e 73 69 64 65 20 61 20 74 72 61 6e 73  d inside a trans
2945e 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20 20 20  action");.      
2945f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
29460 44 62 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65 6c  Db->safety_level
29461 20 3d 20 67 65 74 53 61 66 65 74 79 4c 65 76 65   = getSafetyLeve
29462 6c 28 7a 52 69 67 68 74 29 2b 31 3b 0a 20 20 20  l(zRight)+1;.   
29463 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c     }.    }.  }el
29464 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  se.#endif /* SQL
29465 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50  ITE_OMIT_PAGER_P
29466 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64  RAGMAS */..#ifnd
29467 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
29468 4c 41 47 5f 50 52 41 47 4d 41 53 0a 20 20 69 66  LAG_PRAGMAS.  if
29469 28 20 66 6c 61 67 50 72 61 67 6d 61 28 70 50 61  ( flagPragma(pPa
2946a 72 73 65 2c 20 7a 4c 65 66 74 2c 20 7a 52 69 67  rse, zLeft, zRig
2946b 68 74 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ht) ){.    /* Th
2946c 65 20 66 6c 61 67 50 72 61 67 6d 61 28 29 20 73  e flagPragma() s
2946d 75 62 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20 67  ubroutine also g
2946e 65 6e 65 72 61 74 65 73 20 61 6e 79 20 6e 65 63  enerates any nec
2946f 65 73 73 61 72 79 20 63 6f 64 65 0a 20 20 20 20  essary code.    
29470 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 74 68  ** there is noth
29471 69 6e 67 20 6d 6f 72 65 20 74 6f 20 64 6f 20 68  ing more to do h
29472 65 72 65 20 2a 2f 0a 20 20 7d 65 6c 73 65 0a 23  ere */.  }else.#
29473 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
29474 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d 41  OMIT_FLAG_PRAGMA
29475 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  S */..#ifndef SQ
29476 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41  LITE_OMIT_SCHEMA
29477 5f 50 52 41 47 4d 41 53 0a 20 20 2f 2a 0a 20 20  _PRAGMAS.  /*.  
29478 2a 2a 20 20 20 50 52 41 47 4d 41 20 74 61 62 6c  **   PRAGMA tabl
29479 65 5f 69 6e 66 6f 28 3c 74 61 62 6c 65 3e 29 0a  e_info(<table>).
2947a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e    **.  ** Return
2947b 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 66 6f   a single row fo
2947c 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f 66  r each column of
2947d 20 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65   the named table
2947e 2e 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66  . The columns of
2947f 0a 20 20 2a 2a 20 74 68 65 20 72 65 74 75 72 6e  .  ** the return
29480 65 64 20 64 61 74 61 20 73 65 74 20 61 72 65 3a  ed data set are:
29481 0a 20 20 2a 2a 0a 20 20 2a 2a 20 63 69 64 3a 20  .  **.  ** cid: 
29482 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 69 64         Column id
29483 20 28 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d 20   (numbered from 
29484 6c 65 66 74 20 74 6f 20 72 69 67 68 74 2c 20 73  left to right, s
29485 74 61 72 74 69 6e 67 20 61 74 20 30 29 0a 20 20  tarting at 0).  
29486 2a 2a 20 6e 61 6d 65 3a 20 20 20 20 20 20 20 43  ** name:       C
29487 6f 6c 75 6d 6e 20 6e 61 6d 65 0a 20 20 2a 2a 20  olumn name.  ** 
29488 74 79 70 65 3a 20 20 20 20 20 20 20 43 6f 6c 75  type:       Colu
29489 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74  mn declaration t
2948a 79 70 65 2e 0a 20 20 2a 2a 20 6e 6f 74 6e 75 6c  ype..  ** notnul
2948b 6c 3a 20 20 20 20 54 72 75 65 20 69 66 20 27 4e  l:    True if 'N
2948c 4f 54 20 4e 55 4c 4c 27 20 69 73 20 70 61 72 74  OT NULL' is part
2948d 20 6f 66 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61   of column decla
2948e 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 64 66 6c 74  ration.  ** dflt
2948f 5f 76 61 6c 75 65 3a 20 54 68 65 20 64 65 66 61  _value: The defa
29490 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68  ult value for th
29491 65 20 63 6f 6c 75 6d 6e 2c 20 69 66 20 61 6e 79  e column, if any
29492 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
29493 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
29494 74 2c 20 22 74 61 62 6c 65 5f 69 6e 66 6f 22 29  t, "table_info")
29495 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b  ==0 && zRight ){
29496 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
29497 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
29498 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
29499 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d  se) ) goto pragm
2949a 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54 61 62 20  a_out;.    pTab 
2949b 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  = sqlite3FindTab
2949c 6c 65 28 64 62 2c 20 7a 52 69 67 68 74 2c 20 7a  le(db, zRight, z
2949d 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61  Db);.    if( pTa
2949e 62 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  b ){.      int i
2949f 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 48 69 64  ;.      int nHid
294a0 64 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 43  den = 0;.      C
294a1 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 20  olumn *pCol;.   
294a2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
294a3 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 36 29 3b 0a  tNumCols(v, 6);.
294a4 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d        pParse->nM
294a5 65 6d 20 3d 20 36 3b 0a 20 20 20 20 20 20 73 71  em = 6;.      sq
294a6 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
294a7 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
294a8 45 5f 4e 41 4d 45 2c 20 22 63 69 64 22 2c 20 53  E_NAME, "cid", S
294a9 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
294aa 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
294ab 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c  SetColName(v, 1,
294ac 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
294ad 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53 54  name", SQLITE_ST
294ae 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
294af 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
294b0 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45  me(v, 2, COLNAME
294b1 5f 4e 41 4d 45 2c 20 22 74 79 70 65 22 2c 20 53  _NAME, "type", S
294b2 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
294b3 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
294b4 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 33 2c  SetColName(v, 3,
294b5 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
294b6 6e 6f 74 6e 75 6c 6c 22 2c 20 53 51 4c 49 54 45  notnull", SQLITE
294b7 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
294b8 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
294b9 6c 4e 61 6d 65 28 76 2c 20 34 2c 20 43 4f 4c 4e  lName(v, 4, COLN
294ba 41 4d 45 5f 4e 41 4d 45 2c 20 22 64 66 6c 74 5f  AME_NAME, "dflt_
294bb 76 61 6c 75 65 22 2c 20 53 51 4c 49 54 45 5f 53  value", SQLITE_S
294bc 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71  TATIC);.      sq
294bd 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
294be 61 6d 65 28 76 2c 20 35 2c 20 43 4f 4c 4e 41 4d  ame(v, 5, COLNAM
294bf 45 5f 4e 41 4d 45 2c 20 22 70 6b 22 2c 20 53 51  E_NAME, "pk", SQ
294c0 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
294c1 20 20 20 20 73 71 6c 69 74 65 33 56 69 65 77 47      sqlite3ViewG
294c2 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50  etColumnNames(pP
294c3 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20  arse, pTab);.   
294c4 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 43 6f 6c     for(i=0, pCol
294c5 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b 20 69 3c 70  =pTab->aCol; i<p
294c6 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20  Tab->nCol; i++, 
294c7 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20  pCol++){.       
294c8 20 69 66 28 20 49 73 48 69 64 64 65 6e 43 6f 6c   if( IsHiddenCol
294c9 75 6d 6e 28 70 43 6f 6c 29 20 29 7b 0a 20 20 20  umn(pCol) ){.   
294ca 20 20 20 20 20 20 20 6e 48 69 64 64 65 6e 2b 2b         nHidden++
294cb 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74  ;.          cont
294cc 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  inue;.        }.
294cd 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
294ce 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
294cf 49 6e 74 65 67 65 72 2c 20 69 2d 6e 48 69 64 64  Integer, i-nHidd
294d0 65 6e 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20  en, 1);.        
294d1 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
294d2 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
294d3 20 30 2c 20 32 2c 20 30 2c 20 70 43 6f 6c 2d 3e   0, 2, 0, pCol->
294d4 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20  zName, 0);.     
294d5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
294d6 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
294d7 67 38 2c 20 30 2c 20 33 2c 20 30 2c 0a 20 20 20  g8, 0, 3, 0,.   
294d8 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a 54          pCol->zT
294d9 79 70 65 20 3f 20 70 43 6f 6c 2d 3e 7a 54 79 70  ype ? pCol->zTyp
294da 65 20 3a 20 22 22 2c 20 30 29 3b 0a 20 20 20 20  e : "", 0);.    
294db 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
294dc 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
294dd 67 65 72 2c 20 28 70 43 6f 6c 2d 3e 6e 6f 74 4e  ger, (pCol->notN
294de 75 6c 6c 20 3f 20 31 20 3a 20 30 29 2c 20 34 29  ull ? 1 : 0), 4)
294df 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 43  ;.        if( pC
294e0 6f 6c 2d 3e 7a 44 66 6c 74 20 29 7b 0a 20 20 20  ol->zDflt ){.   
294e1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
294e2 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
294e3 74 72 69 6e 67 38 2c 20 30 2c 20 35 2c 20 30 2c  tring8, 0, 5, 0,
294e4 20 28 63 68 61 72 2a 29 70 43 6f 6c 2d 3e 7a 44   (char*)pCol->zD
294e5 66 6c 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  flt, 0);.       
294e6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
294e7 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
294e8 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
294e9 30 2c 20 35 29 3b 0a 20 20 20 20 20 20 20 20 7d  0, 5);.        }
294ea 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
294eb 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
294ec 5f 49 6e 74 65 67 65 72 2c 20 70 43 6f 6c 2d 3e  _Integer, pCol->
294ed 69 73 50 72 69 6d 4b 65 79 2c 20 36 29 3b 0a 20  isPrimKey, 6);. 
294ee 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
294ef 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
294f0 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 36 29 3b  esultRow, 1, 6);
294f1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
294f2 20 7d 65 6c 73 65 0a 0a 20 20 69 66 28 20 73 71   }else..  if( sq
294f3 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
294f4 66 74 2c 20 22 69 6e 64 65 78 5f 69 6e 66 6f 22  ft, "index_info"
294f5 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29  )==0 && zRight )
294f6 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64  {.    Index *pId
294f7 78 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  x;.    Table *pT
294f8 61 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  ab;.    if( sqli
294f9 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
294fa 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61  arse) ) goto pra
294fb 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 49 64  gma_out;.    pId
294fc 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49  x = sqlite3FindI
294fd 6e 64 65 78 28 64 62 2c 20 7a 52 69 67 68 74 2c  ndex(db, zRight,
294fe 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70   zDb);.    if( p
294ff 49 64 78 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  Idx ){.      int
29500 20 69 3b 0a 20 20 20 20 20 20 70 54 61 62 20 3d   i;.      pTab =
29501 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 3b 0a 20   pIdx->pTable;. 
29502 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29503 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 33 29  SetNumCols(v, 3)
29504 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
29505 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20 20 20  nMem = 3;.      
29506 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
29507 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
29508 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 6e 6f  AME_NAME, "seqno
29509 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
2950a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2950b 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
2950c 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 1, COLNAME_NAM
2950d 45 2c 20 22 63 69 64 22 2c 20 53 51 4c 49 54 45  E, "cid", SQLITE
2950e 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
2950f 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
29510 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e  lName(v, 2, COLN
29511 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22  AME_NAME, "name"
29512 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
29513 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
29514 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   i<pIdx->nColumn
29515 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
29516 69 6e 74 20 63 6e 75 6d 20 3d 20 70 49 64 78 2d  int cnum = pIdx-
29517 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20  >aiColumn[i];.  
29518 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29519 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
2951a 74 65 67 65 72 2c 20 69 2c 20 31 29 3b 0a 20 20  teger, i, 1);.  
2951b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2951c 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
2951d 74 65 67 65 72 2c 20 63 6e 75 6d 2c 20 32 29 3b  teger, cnum, 2);
2951e 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2951f 20 70 54 61 62 2d 3e 6e 43 6f 6c 3e 63 6e 75 6d   pTab->nCol>cnum
29520 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   );.        sqli
29521 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
29522 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
29523 33 2c 20 30 2c 20 70 54 61 62 2d 3e 61 43 6f 6c  3, 0, pTab->aCol
29524 5b 63 6e 75 6d 5d 2e 7a 4e 61 6d 65 2c 20 30 29  [cnum].zName, 0)
29525 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
29526 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29527 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
29528 33 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  3);.      }.    
29529 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66 28  }.  }else..  if(
2952a 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
2952b 7a 4c 65 66 74 2c 20 22 69 6e 64 65 78 5f 6c 69  zLeft, "index_li
2952c 73 74 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68  st")==0 && zRigh
2952d 74 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a  t ){.    Index *
2952e 70 49 64 78 3b 0a 20 20 20 20 54 61 62 6c 65 20  pIdx;.    Table 
2952f 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20 73  *pTab;.    if( s
29530 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
29531 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20  (pParse) ) goto 
29532 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20  pragma_out;.    
29533 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69  pTab = sqlite3Fi
29534 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 52 69 67  ndTable(db, zRig
29535 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66  ht, zDb);.    if
29536 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20  ( pTab ){.      
29537 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
29538 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  be(pParse);.    
29539 20 20 70 49 64 78 20 3d 20 70 54 61 62 2d 3e 70    pIdx = pTab->p
2953a 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 69 66 28  Index;.      if(
2953b 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 20   pIdx ){.       
2953c 20 69 6e 74 20 69 20 3d 20 30 3b 20 0a 20 20 20   int i = 0; .   
2953d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2953e 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 33 29  SetNumCols(v, 3)
2953f 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65  ;.        pParse
29540 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20  ->nMem = 3;.    
29541 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
29542 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
29543 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73  COLNAME_NAME, "s
29544 65 71 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  eq", SQLITE_STAT
29545 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  IC);.        sql
29546 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
29547 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45  me(v, 1, COLNAME
29548 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22 2c 20 53  _NAME, "name", S
29549 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
2954a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2954b 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
2954c 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  2, COLNAME_NAME,
2954d 20 22 75 6e 69 71 75 65 22 2c 20 53 51 4c 49 54   "unique", SQLIT
2954e 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
2954f 20 20 20 77 68 69 6c 65 28 70 49 64 78 29 7b 0a     while(pIdx){.
29550 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
29551 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29552 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 31 29  P_Integer, i, 1)
29553 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
29554 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
29555 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
29556 32 2c 20 30 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d  2, 0, pIdx->zNam
29557 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  e, 0);.         
29558 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29559 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
2955a 2c 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21  , pIdx->onError!
2955b 3d 4f 45 5f 4e 6f 6e 65 2c 20 33 29 3b 0a 20 20  =OE_None, 3);.  
2955c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2955d 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2955e 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 33 29  ResultRow, 1, 3)
2955f 3b 0a 20 20 20 20 20 20 20 20 20 20 2b 2b 69 3b  ;.          ++i;
29560 0a 20 20 20 20 20 20 20 20 20 20 70 49 64 78 20  .          pIdx 
29561 3d 20 70 49 64 78 2d 3e 70 4e 65 78 74 3b 0a 20  = pIdx->pNext;. 
29562 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
29563 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a  .    }.  }else..
29564 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
29565 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 64 61 74  ICmp(zLeft, "dat
29566 61 62 61 73 65 5f 6c 69 73 74 22 29 3d 3d 30 20  abase_list")==0 
29567 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  ){.    int i;.  
29568 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61    if( sqlite3Rea
29569 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
2956a 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75  ) goto pragma_ou
2956b 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  t;.    sqlite3Vd
2956c 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
2956d 33 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  3);.    pParse->
2956e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20 73 71  nMem = 3;.    sq
2956f 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
29570 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
29571 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c 20 53  E_NAME, "seq", S
29572 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
29573 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
29574 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43  tColName(v, 1, C
29575 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61  OLNAME_NAME, "na
29576 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  me", SQLITE_STAT
29577 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  IC);.    sqlite3
29578 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
29579 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 2, COLNAME_NAM
2957a 45 2c 20 22 66 69 6c 65 22 2c 20 53 51 4c 49 54  E, "file", SQLIT
2957b 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 66  E_STATIC);.    f
2957c 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
2957d 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  b; i++){.      i
2957e 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42  f( db->aDb[i].pB
2957f 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  t==0 ) continue;
29580 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64  .      assert( d
29581 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 21  b->aDb[i].zName!
29582 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =0 );.      sqli
29583 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
29584 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20   OP_Integer, i, 
29585 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
29586 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
29587 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 32 2c  P_String8, 0, 2,
29588 20 30 2c 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a   0, db->aDb[i].z
29589 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Name, 0);.      
2958a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2958b 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
2958c 20 30 2c 20 33 2c 20 30 2c 0a 20 20 20 20 20 20   0, 3, 0,.      
2958d 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
2958e 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 64 62 2d  eGetFilename(db-
2958f 3e 61 44 62 5b 69 5d 2e 70 42 74 29 2c 20 30 29  >aDb[i].pBt), 0)
29590 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
29591 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29592 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 33 29  ResultRow, 1, 3)
29593 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  ;.    }.  }else.
29594 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
29595 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 63 6f  rICmp(zLeft, "co
29596 6c 6c 61 74 69 6f 6e 5f 6c 69 73 74 22 29 3d 3d  llation_list")==
29597 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d  0 ){.    int i =
29598 20 30 3b 0a 20 20 20 20 48 61 73 68 45 6c 65 6d   0;.    HashElem
29599 20 2a 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   *p;.    sqlite3
2959a 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
2959b 2c 20 32 29 3b 0a 20 20 20 20 70 50 61 72 73 65  , 2);.    pParse
2959c 2d 3e 6e 4d 65 6d 20 3d 20 32 3b 0a 20 20 20 20  ->nMem = 2;.    
2959d 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
2959e 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
2959f 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c  AME_NAME, "seq",
295a0 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
295a1 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
295a2 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c  SetColName(v, 1,
295a3 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
295a4 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53 54  name", SQLITE_ST
295a5 41 54 49 43 29 3b 0a 20 20 20 20 66 6f 72 28 70  ATIC);.    for(p
295a6 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
295a7 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b  (&db->aCollSeq);
295a8 20 70 3b 20 70 3d 73 71 6c 69 74 65 48 61 73 68   p; p=sqliteHash
295a9 4e 65 78 74 28 70 29 29 7b 0a 20 20 20 20 20 20  Next(p)){.      
295aa 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d  CollSeq *pColl =
295ab 20 28 43 6f 6c 6c 53 65 71 20 2a 29 73 71 6c 69   (CollSeq *)sqli
295ac 74 65 48 61 73 68 44 61 74 61 28 70 29 3b 0a 20  teHashData(p);. 
295ad 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
295ae 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
295af 65 67 65 72 2c 20 69 2b 2b 2c 20 31 29 3b 0a 20  eger, i++, 1);. 
295b0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
295b1 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
295b2 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30 2c 20 70  ing8, 0, 2, 0, p
295b3 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b  Coll->zName, 0);
295b4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
295b5 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
295b6 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 32 29 3b  esultRow, 1, 2);
295b7 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23  .    }.  }else.#
295b8 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
295b9 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 50 52 41 47  OMIT_SCHEMA_PRAG
295ba 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  MAS */..#ifndef 
295bb 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45  SQLITE_OMIT_FORE
295bc 49 47 4e 5f 4b 45 59 0a 20 20 69 66 28 20 73 71  IGN_KEY.  if( sq
295bd 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
295be 66 74 2c 20 22 66 6f 72 65 69 67 6e 5f 6b 65 79  ft, "foreign_key
295bf 5f 6c 69 73 74 22 29 3d 3d 30 20 26 26 20 7a 52  _list")==0 && zR
295c0 69 67 68 74 20 29 7b 0a 20 20 20 20 46 4b 65 79  ight ){.    FKey
295c1 20 2a 70 46 4b 3b 0a 20 20 20 20 54 61 62 6c 65   *pFK;.    Table
295c2 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20   *pTab;.    if( 
295c3 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
295c4 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f  a(pParse) ) goto
295c5 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20   pragma_out;.   
295c6 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46   pTab = sqlite3F
295c7 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 52 69  indTable(db, zRi
295c8 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69  ght, zDb);.    i
295c9 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20  f( pTab ){.     
295ca 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
295cb 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20  dbe(pParse);.   
295cc 20 20 20 70 46 4b 20 3d 20 70 54 61 62 2d 3e 70     pFK = pTab->p
295cd 46 4b 65 79 3b 0a 20 20 20 20 20 20 69 66 28 20  FKey;.      if( 
295ce 70 46 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69  pFK ){.        i
295cf 6e 74 20 69 20 3d 20 30 3b 20 0a 20 20 20 20 20  nt i = 0; .     
295d0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
295d1 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 38 29 3b 0a  tNumCols(v, 8);.
295d2 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
295d3 6e 4d 65 6d 20 3d 20 38 3b 0a 20 20 20 20 20 20  nMem = 8;.      
295d4 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
295d5 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
295d6 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 69 64 22  LNAME_NAME, "id"
295d7 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
295d8 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
295d9 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
295da 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 1, COLNAME_NA
295db 4d 45 2c 20 22 73 65 71 22 2c 20 53 51 4c 49 54  ME, "seq", SQLIT
295dc 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
295dd 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
295de 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43  tColName(v, 2, C
295df 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 74 61  OLNAME_NAME, "ta
295e0 62 6c 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ble", SQLITE_STA
295e1 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71  TIC);.        sq
295e2 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
295e3 61 6d 65 28 76 2c 20 33 2c 20 43 4f 4c 4e 41 4d  ame(v, 3, COLNAM
295e4 45 5f 4e 41 4d 45 2c 20 22 66 72 6f 6d 22 2c 20  E_NAME, "from", 
295e5 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
295e6 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
295e7 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
295e8 20 34 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   4, COLNAME_NAME
295e9 2c 20 22 74 6f 22 2c 20 53 51 4c 49 54 45 5f 53  , "to", SQLITE_S
295ea 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
295eb 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
295ec 6c 4e 61 6d 65 28 76 2c 20 35 2c 20 43 4f 4c 4e  lName(v, 5, COLN
295ed 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 6e 5f 75 70  AME_NAME, "on_up
295ee 64 61 74 65 22 2c 20 53 51 4c 49 54 45 5f 53 54  date", SQLITE_ST
295ef 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73  ATIC);.        s
295f0 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
295f1 4e 61 6d 65 28 76 2c 20 36 2c 20 43 4f 4c 4e 41  Name(v, 6, COLNA
295f2 4d 45 5f 4e 41 4d 45 2c 20 22 6f 6e 5f 64 65 6c  ME_NAME, "on_del
295f3 65 74 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ete", SQLITE_STA
295f4 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71  TIC);.        sq
295f5 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
295f6 61 6d 65 28 76 2c 20 37 2c 20 43 4f 4c 4e 41 4d  ame(v, 7, COLNAM
295f7 45 5f 4e 41 4d 45 2c 20 22 6d 61 74 63 68 22 2c  E_NAME, "match",
295f8 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
295f9 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 70  .        while(p
295fa 46 4b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  FK){.          i
295fb 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20  nt j;.          
295fc 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46 4b 2d 3e  for(j=0; j<pFK->
295fd 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  nCol; j++){.    
295fe 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43          char *zC
295ff 6f 6c 20 3d 20 70 46 4b 2d 3e 61 43 6f 6c 5b 6a  ol = pFK->aCol[j
29600 5d 2e 7a 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20  ].zCol;.        
29601 20 20 20 20 63 68 61 72 20 2a 7a 4f 6e 44 65 6c      char *zOnDel
29602 65 74 65 20 3d 20 28 63 68 61 72 20 2a 29 61 63  ete = (char *)ac
29603 74 69 6f 6e 4e 61 6d 65 28 70 46 4b 2d 3e 61 41  tionName(pFK->aA
29604 63 74 69 6f 6e 5b 30 5d 29 3b 0a 20 20 20 20 20  ction[0]);.     
29605 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4f 6e         char *zOn
29606 55 70 64 61 74 65 20 3d 20 28 63 68 61 72 20 2a  Update = (char *
29607 29 61 63 74 69 6f 6e 4e 61 6d 65 28 70 46 4b 2d  )actionName(pFK-
29608 3e 61 41 63 74 69 6f 6e 5b 31 5d 29 3b 0a 20 20  >aAction[1]);.  
29609 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2960a 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2960b 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 31 29  P_Integer, i, 1)
2960c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ;.            sq
2960d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2960e 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6a  v, OP_Integer, j
2960f 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 2);.          
29610 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29611 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
29612 38 2c 20 30 2c 20 33 2c 20 30 2c 20 70 46 4b 2d  8, 0, 3, 0, pFK-
29613 3e 7a 54 6f 2c 20 30 29 3b 0a 20 20 20 20 20 20  >zTo, 0);.      
29614 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29615 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
29616 72 69 6e 67 38 2c 20 30 2c 20 34 2c 20 30 2c 0a  ring8, 0, 4, 0,.
29617 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29618 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54                pT
29619 61 62 2d 3e 61 43 6f 6c 5b 70 46 4b 2d 3e 61 43  ab->aCol[pFK->aC
2961a 6f 6c 5b 6a 5d 2e 69 46 72 6f 6d 5d 2e 7a 4e 61  ol[j].iFrom].zNa
2961b 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  me, 0);.        
2961c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2961d 64 64 4f 70 34 28 76 2c 20 7a 43 6f 6c 20 3f 20  ddOp4(v, zCol ? 
2961e 4f 50 5f 53 74 72 69 6e 67 38 20 3a 20 4f 50 5f  OP_String8 : OP_
2961f 4e 75 6c 6c 2c 20 30 2c 20 35 2c 20 30 2c 20 7a  Null, 0, 5, 0, z
29620 43 6f 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  Col, 0);.       
29621 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29622 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
29623 69 6e 67 38 2c 20 30 2c 20 36 2c 20 30 2c 20 7a  ing8, 0, 6, 0, z
29624 4f 6e 55 70 64 61 74 65 2c 20 30 29 3b 0a 20 20  OnUpdate, 0);.  
29625 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
29626 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
29627 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 37 2c  P_String8, 0, 7,
29628 20 30 2c 20 7a 4f 6e 44 65 6c 65 74 65 2c 20 30   0, zOnDelete, 0
29629 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
2962a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2962b 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
2962c 30 2c 20 38 2c 20 30 2c 20 22 4e 4f 4e 45 22 2c  0, 8, 0, "NONE",
2962d 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   0);.           
2962e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2962f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
29630 6f 77 2c 20 31 2c 20 38 29 3b 0a 20 20 20 20 20  ow, 1, 8);.     
29631 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
29632 20 2b 2b 69 3b 0a 20 20 20 20 20 20 20 20 20 20   ++i;.          
29633 70 46 4b 20 3d 20 70 46 4b 2d 3e 70 4e 65 78 74  pFK = pFK->pNext
29634 46 72 6f 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a  From;.        }.
29635 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
29636 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20  }else.#endif /* 
29637 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
29638 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
29639 29 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44  ) */..#ifndef ND
2963a 45 42 55 47 0a 20 20 69 66 28 20 73 71 6c 69 74  EBUG.  if( sqlit
2963b 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
2963c 20 22 70 61 72 73 65 72 5f 74 72 61 63 65 22 29   "parser_trace")
2963d 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 7a  ==0 ){.    if( z
2963e 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 69  Right ){.      i
2963f 66 28 20 67 65 74 42 6f 6f 6c 65 61 6e 28 7a 52  f( getBoolean(zR
29640 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 20  ight) ){.       
29641 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 72   sqlite3ParserTr
29642 61 63 65 28 73 74 64 65 72 72 2c 20 22 70 61 72  ace(stderr, "par
29643 73 65 72 3a 20 22 29 3b 0a 20 20 20 20 20 20 7d  ser: ");.      }
29644 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
29645 6c 69 74 65 33 50 61 72 73 65 72 54 72 61 63 65  lite3ParserTrace
29646 28 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  (0, 0);.      }.
29647 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65      }.  }else.#e
29648 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 69 6e 73  ndif..  /* Reins
29649 74 61 6c 6c 20 74 68 65 20 4c 49 4b 45 20 61 6e  tall the LIKE an
2964a 64 20 47 4c 4f 42 20 66 75 6e 63 74 69 6f 6e 73  d GLOB functions
2964b 2e 20 20 54 68 65 20 76 61 72 69 61 6e 74 20 6f  .  The variant o
2964c 66 20 4c 49 4b 45 0a 20 20 2a 2a 20 75 73 65 64  f LIKE.  ** used
2964d 20 77 69 6c 6c 20 62 65 20 63 61 73 65 20 73 65   will be case se
2964e 6e 73 69 74 69 76 65 20 6f 72 20 6e 6f 74 20 64  nsitive or not d
2964f 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20  epending on the 
29650 52 48 53 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  RHS..  */.  if( 
29651 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
29652 4c 65 66 74 2c 20 22 63 61 73 65 5f 73 65 6e 73  Left, "case_sens
29653 69 74 69 76 65 5f 6c 69 6b 65 22 29 3d 3d 30 20  itive_like")==0 
29654 29 7b 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68  ){.    if( zRigh
29655 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
29656 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65 46 75  e3RegisterLikeFu
29657 6e 63 74 69 6f 6e 73 28 64 62 2c 20 67 65 74 42  nctions(db, getB
29658 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74 29 29 3b  oolean(zRight));
29659 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a  .    }.  }else..
2965a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 49  #ifndef SQLITE_I
2965b 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 5f 45  NTEGRITY_CHECK_E
2965c 52 52 4f 52 5f 4d 41 58 0a 23 20 64 65 66 69 6e  RROR_MAX.# defin
2965d 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 52 49  e SQLITE_INTEGRI
2965e 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f 52 5f 4d  TY_CHECK_ERROR_M
2965f 41 58 20 31 30 30 0a 23 65 6e 64 69 66 0a 0a 23  AX 100.#endif..#
29660 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
29661 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  IT_INTEGRITY_CHE
29662 43 4b 0a 20 20 2f 2a 20 50 72 61 67 6d 61 20 22  CK.  /* Pragma "
29663 71 75 69 63 6b 5f 63 68 65 63 6b 22 20 69 73 20  quick_check" is 
29664 61 6e 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20  an experimental 
29665 72 65 64 75 63 65 64 20 76 65 72 73 69 6f 6e 20  reduced version 
29666 6f 66 20 0a 20 20 2a 2a 20 69 6e 74 65 67 72 69  of .  ** integri
29667 74 79 5f 63 68 65 63 6b 20 64 65 73 69 67 6e 65  ty_check designe
29668 64 20 74 6f 20 64 65 74 65 63 74 20 6d 6f 73 74  d to detect most
29669 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70   database corrup
2966a 74 69 6f 6e 0a 20 20 2a 2a 20 77 69 74 68 6f 75  tion.  ** withou
2966b 74 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 6f 76  t most of the ov
2966c 65 72 68 65 61 64 20 6f 66 20 61 20 66 75 6c 6c  erhead of a full
2966d 20 69 6e 74 65 67 72 69 74 79 2d 63 68 65 63 6b   integrity-check
2966e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
2966f 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
29670 74 2c 20 22 69 6e 74 65 67 72 69 74 79 5f 63 68  t, "integrity_ch
29671 65 63 6b 22 29 3d 3d 30 0a 20 20 20 7c 7c 20 73  eck")==0.   || s
29672 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
29673 65 66 74 2c 20 22 71 75 69 63 6b 5f 63 68 65 63  eft, "quick_chec
29674 6b 22 29 3d 3d 30 20 0a 20 20 29 7b 0a 20 20 20  k")==0 .  ){.   
29675 20 69 6e 74 20 69 2c 20 6a 2c 20 61 64 64 72 2c   int i, j, addr,
29676 20 6d 78 45 72 72 3b 0a 0a 20 20 20 20 2f 2a 20   mxErr;..    /* 
29677 43 6f 64 65 20 74 68 61 74 20 61 70 70 65 61 72  Code that appear
29678 73 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20  s at the end of 
29679 74 68 65 20 69 6e 74 65 67 72 69 74 79 20 63 68  the integrity ch
2967a 65 63 6b 2e 20 20 49 66 20 6e 6f 20 65 72 72 6f  eck.  If no erro
2967b 72 0a 20 20 20 20 2a 2a 20 6d 65 73 73 61 67 65  r.    ** message
2967c 73 20 68 61 76 65 20 62 65 65 6e 20 67 65 6e 65  s have been gene
2967d 72 61 74 65 64 2c 20 6f 75 74 70 75 74 20 4f 4b  rated, output OK
2967e 2e 20 20 4f 74 68 65 72 77 69 73 65 20 6f 75 74  .  Otherwise out
2967f 70 75 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 65  put the.    ** e
29680 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20 20  rror message.   
29681 20 2a 2f 0a 20 20 20 20 73 74 61 74 69 63 20 63   */.    static c
29682 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20  onst VdbeOpList 
29683 65 6e 64 43 6f 64 65 5b 5d 20 3d 20 7b 0a 20 20  endCode[] = {.  
29684 20 20 20 20 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c      { OP_AddImm,
29685 20 20 20 20 20 20 31 2c 20 30 2c 20 20 20 20 20        1, 0,     
29686 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a     0},    /* 0 *
29687 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49 66 4e  /.      { OP_IfN
29688 65 67 2c 20 20 20 20 20 20 20 31 2c 20 30 2c 20  eg,       1, 0, 
29689 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a         0},    /*
2968a 20 31 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50   1 */.      { OP
2968b 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20 30 2c  _String8,     0,
2968c 20 33 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20   3,        0},  
2968d 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20    /* 2 */.      
2968e 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  { OP_ResultRow, 
2968f 20 20 33 2c 20 31 2c 20 20 20 20 20 20 20 20 30    3, 1,        0
29690 7d 2c 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20 69  },.    };..    i
29691 6e 74 20 69 73 51 75 69 63 6b 20 3d 20 28 7a 4c  nt isQuick = (zL
29692 65 66 74 5b 30 5d 3d 3d 27 71 27 29 3b 0a 0a 20  eft[0]=='q');.. 
29693 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65     /* Initialize
29694 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61   the VDBE progra
29695 6d 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c  m */.    if( sql
29696 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
29697 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72  Parse) ) goto pr
29698 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 50  agma_out;.    pP
29699 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 36 3b 0a  arse->nMem = 6;.
2969a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2969b 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
2969c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2969d 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
2969e 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
2969f 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 22  integrity_check"
296a0 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
296a1 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68  ;..    /* Set th
296a2 65 20 6d 61 78 69 6d 75 6d 20 65 72 72 6f 72 20  e maximum error 
296a3 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 6d 78 45  count */.    mxE
296a4 72 72 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45  rr = SQLITE_INTE
296a5 47 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f  GRITY_CHECK_ERRO
296a6 52 5f 4d 41 58 3b 0a 20 20 20 20 69 66 28 20 7a  R_MAX;.    if( z
296a7 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 6d  Right ){.      m
296a8 78 45 72 72 20 3d 20 61 74 6f 69 28 7a 52 69 67  xErr = atoi(zRig
296a9 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d  ht);.      if( m
296aa 78 45 72 72 3c 3d 30 20 29 7b 0a 20 20 20 20 20  xErr<=0 ){.     
296ab 20 20 20 6d 78 45 72 72 20 3d 20 53 51 4c 49 54     mxErr = SQLIT
296ac 45 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  E_INTEGRITY_CHEC
296ad 4b 5f 45 52 52 4f 52 5f 4d 41 58 3b 0a 20 20 20  K_ERROR_MAX;.   
296ae 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
296af 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
296b0 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
296b1 6d 78 45 72 72 2c 20 31 29 3b 20 20 2f 2a 20 72  mxErr, 1);  /* r
296b2 65 67 5b 31 5d 20 68 6f 6c 64 73 20 65 72 72 6f  eg[1] holds erro
296b3 72 73 20 6c 65 66 74 20 2a 2f 0a 0a 20 20 20 20  rs left */..    
296b4 2f 2a 20 44 6f 20 61 6e 20 69 6e 74 65 67 72 69  /* Do an integri
296b5 74 79 20 63 68 65 63 6b 20 6f 6e 20 65 61 63 68  ty check on each
296b6 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
296b7 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  /.    for(i=0; i
296b8 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
296b9 20 20 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a        HashElem *
296ba 78 3b 0a 20 20 20 20 20 20 48 61 73 68 20 2a 70  x;.      Hash *p
296bb 54 62 6c 73 3b 0a 20 20 20 20 20 20 69 6e 74 20  Tbls;.      int 
296bc 63 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20  cnt = 0;..      
296bd 69 66 28 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20  if( OMIT_TEMPDB 
296be 26 26 20 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e  && i==1 ) contin
296bf 75 65 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74  ue;..      sqlit
296c0 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65  e3CodeVerifySche
296c1 6d 61 28 70 50 61 72 73 65 2c 20 69 29 3b 0a 20  ma(pParse, i);. 
296c2 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
296c3 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
296c4 20 4f 50 5f 49 66 50 6f 73 2c 20 31 29 3b 20 2f   OP_IfPos, 1); /
296c5 2a 20 48 61 6c 74 20 69 66 20 6f 75 74 20 6f 66  * Halt if out of
296c6 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20 20 20   errors */.     
296c7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
296c8 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 30  p2(v, OP_Halt, 0
296c9 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
296ca 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
296cb 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20 20 20 20  v, addr);..     
296cc 20 2f 2a 20 44 6f 20 61 6e 20 69 6e 74 65 67 72   /* Do an integr
296cd 69 74 79 20 63 68 65 63 6b 20 6f 66 20 74 68 65  ity check of the
296ce 20 42 2d 54 72 65 65 0a 20 20 20 20 20 20 2a 2a   B-Tree.      **
296cf 0a 20 20 20 20 20 20 2a 2a 20 42 65 67 69 6e 20  .      ** Begin 
296d0 62 79 20 66 69 6c 6c 69 6e 67 20 72 65 67 69 73  by filling regis
296d1 74 65 72 73 20 32 2c 20 33 2c 20 2e 2e 2e 20 77  ters 2, 3, ... w
296d2 69 74 68 20 74 68 65 20 72 6f 6f 74 20 70 61 67  ith the root pag
296d3 65 73 20 6e 75 6d 62 65 72 73 0a 20 20 20 20 20  es numbers.     
296d4 20 2a 2a 20 66 6f 72 20 61 6c 6c 20 74 61 62 6c   ** for all tabl
296d5 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73 20 69  es and indices i
296d6 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  n the database..
296d7 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70        */.      p
296d8 54 62 6c 73 20 3d 20 26 64 62 2d 3e 61 44 62 5b  Tbls = &db->aDb[
296d9 69 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48  i].pSchema->tblH
296da 61 73 68 3b 0a 20 20 20 20 20 20 66 6f 72 28 78  ash;.      for(x
296db 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
296dc 28 70 54 62 6c 73 29 3b 20 78 3b 20 78 3d 73 71  (pTbls); x; x=sq
296dd 6c 69 74 65 48 61 73 68 4e 65 78 74 28 78 29 29  liteHashNext(x))
296de 7b 0a 20 20 20 20 20 20 20 20 54 61 62 6c 65 20  {.        Table 
296df 2a 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61  *pTab = sqliteHa
296e0 73 68 44 61 74 61 28 78 29 3b 0a 20 20 20 20 20  shData(x);.     
296e1 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a     Index *pIdx;.
296e2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
296e3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
296e4 49 6e 74 65 67 65 72 2c 20 70 54 61 62 2d 3e 74  Integer, pTab->t
296e5 6e 75 6d 2c 20 32 2b 63 6e 74 29 3b 0a 20 20 20  num, 2+cnt);.   
296e6 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20       cnt++;.    
296e7 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61      for(pIdx=pTa
296e8 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
296e9 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
296ea 74 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71  t){.          sq
296eb 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
296ec 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70  v, OP_Integer, p
296ed 49 64 78 2d 3e 74 6e 75 6d 2c 20 32 2b 63 6e 74  Idx->tnum, 2+cnt
296ee 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74  );.          cnt
296ef 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ++;.        }.  
296f0 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
296f1 4d 61 6b 65 20 73 75 72 65 20 73 75 66 66 69 63  Make sure suffic
296f2 69 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 72  ient number of r
296f3 65 67 69 73 74 65 72 73 20 68 61 76 65 20 62 65  egisters have be
296f4 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a  en allocated */.
296f5 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73 65        if( pParse
296f6 2d 3e 6e 4d 65 6d 20 3c 20 63 6e 74 2b 34 20 29  ->nMem < cnt+4 )
296f7 7b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65  {.        pParse
296f8 2d 3e 6e 4d 65 6d 20 3d 20 63 6e 74 2b 34 3b 0a  ->nMem = cnt+4;.
296f9 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
296fa 2a 20 44 6f 20 74 68 65 20 62 2d 74 72 65 65 20  * Do the b-tree 
296fb 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 73  integrity checks
296fc 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
296fd 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
296fe 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 2c 20 32  P_IntegrityCk, 2
296ff 2c 20 63 6e 74 2c 20 31 29 3b 0a 20 20 20 20 20  , cnt, 1);.     
29700 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
29701 67 65 50 35 28 76 2c 20 28 75 38 29 69 29 3b 0a  geP5(v, (u8)i);.
29702 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
29703 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
29704 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 32 29 3b  , OP_IsNull, 2);
29705 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29706 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
29707 74 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c  tring8, 0, 3, 0,
29708 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  .         sqlite
29709 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 2a 2a  3MPrintf(db, "**
2970a 2a 20 69 6e 20 64 61 74 61 62 61 73 65 20 25 73  * in database %s
2970b 20 2a 2a 2a 5c 6e 22 2c 20 64 62 2d 3e 61 44 62   ***\n", db->aDb
2970c 5b 69 5d 2e 7a 4e 61 6d 65 29 2c 0a 20 20 20 20  [i].zName),.    
2970d 20 20 20 20 20 50 34 5f 44 59 4e 41 4d 49 43 29       P4_DYNAMIC)
2970e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2970f 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
29710 4d 6f 76 65 2c 20 32 2c 20 34 2c 20 31 29 3b 0a  Move, 2, 4, 1);.
29711 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29712 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
29713 6e 63 61 74 2c 20 34 2c 20 33 2c 20 32 29 3b 0a  ncat, 4, 3, 2);.
29714 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29715 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
29716 73 75 6c 74 52 6f 77 2c 20 32 2c 20 31 29 3b 0a  sultRow, 2, 1);.
29717 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29718 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
29719 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 61  r);..      /* Ma
2971a 6b 65 20 73 75 72 65 20 61 6c 6c 20 74 68 65 20  ke sure all the 
2971b 69 6e 64 69 63 65 73 20 61 72 65 20 63 6f 6e 73  indices are cons
2971c 74 72 75 63 74 65 64 20 63 6f 72 72 65 63 74 6c  tructed correctl
2971d 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  y..      */.    
2971e 20 20 66 6f 72 28 78 3d 73 71 6c 69 74 65 48 61    for(x=sqliteHa
2971f 73 68 46 69 72 73 74 28 70 54 62 6c 73 29 3b 20  shFirst(pTbls); 
29720 78 20 26 26 20 21 69 73 51 75 69 63 6b 3b 20 78  x && !isQuick; x
29721 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28  =sqliteHashNext(
29722 78 29 29 7b 0a 20 20 20 20 20 20 20 20 54 61 62  x)){.        Tab
29723 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69 74  le *pTab = sqlit
29724 65 48 61 73 68 44 61 74 61 28 78 29 3b 0a 20 20  eHashData(x);.  
29725 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64        Index *pId
29726 78 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6c  x;.        int l
29727 6f 6f 70 54 6f 70 3b 0a 0a 20 20 20 20 20 20 20  oopTop;..       
29728 20 69 66 28 20 70 54 61 62 2d 3e 70 49 6e 64 65   if( pTab->pInde
29729 78 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  x==0 ) continue;
2972a 0a 20 20 20 20 20 20 20 20 61 64 64 72 20 3d 20  .        addr = 
2972b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2972c 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31  1(v, OP_IfPos, 1
2972d 29 3b 20 20 2f 2a 20 53 74 6f 70 20 69 66 20 6f  );  /* Stop if o
2972e 75 74 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0a  ut of errors */.
2972f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29730 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29731 48 61 6c 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Halt, 0, 0);.   
29732 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29733 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
29734 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
29735 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e  e3OpenTableAndIn
29736 64 69 63 65 73 28 70 50 61 72 73 65 2c 20 70 54  dices(pParse, pT
29737 61 62 2c 20 31 2c 20 4f 50 5f 4f 70 65 6e 52 65  ab, 1, OP_OpenRe
29738 61 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ad);.        sql
29739 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2973a 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
2973b 20 32 29 3b 20 20 2f 2a 20 72 65 67 28 32 29 20   2);  /* reg(2) 
2973c 77 69 6c 6c 20 63 6f 75 6e 74 20 65 6e 74 72 69  will count entri
2973d 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 6c 6f  es */.        lo
2973e 6f 70 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56  opTop = sqlite3V
2973f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29740 52 65 77 69 6e 64 2c 20 31 2c 20 30 29 3b 0a 20  Rewind, 1, 0);. 
29741 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29742 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
29743 64 64 49 6d 6d 2c 20 32 2c 20 31 29 3b 20 20 20  ddImm, 2, 1);   
29744 2f 2a 20 69 6e 63 72 65 6d 65 6e 74 20 65 6e 74  /* increment ent
29745 72 79 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20  ry count */.    
29746 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64      for(j=0, pId
29747 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
29748 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
29749 3e 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20  >pNext, j++){.  
2974a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 6d 70 32          int jmp2
2974b 3b 0a 20 20 20 20 20 20 20 20 20 20 73 74 61 74  ;.          stat
2974c 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c  ic const VdbeOpL
2974d 69 73 74 20 69 64 78 45 72 72 5b 5d 20 3d 20 7b  ist idxErr[] = {
2974e 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
2974f 50 5f 41 64 64 49 6d 6d 2c 20 20 20 20 20 20 31  P_AddImm,      1
29750 2c 20 2d 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20  , -1,  0},.     
29751 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69         { OP_Stri
29752 6e 67 38 2c 20 20 20 20 20 30 2c 20 20 33 2c 20  ng8,     0,  3, 
29753 20 30 7d 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0a   0},    /* 1 */.
29754 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
29755 5f 52 6f 77 69 64 2c 20 20 20 20 20 20 20 31 2c  _Rowid,       1,
29756 20 20 34 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20    4,  0},.      
29757 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e        { OP_Strin
29758 67 38 2c 20 20 20 20 20 30 2c 20 20 35 2c 20 20  g8,     0,  5,  
29759 30 7d 2c 20 20 20 20 2f 2a 20 33 20 2a 2f 0a 20  0},    /* 3 */. 
2975a 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
2975b 53 74 72 69 6e 67 38 2c 20 20 20 20 20 30 2c 20  String8,     0, 
2975c 20 36 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 34   6,  0},    /* 4
2975d 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
2975e 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20  { OP_Concat,    
2975f 20 20 34 2c 20 20 33 2c 20 20 33 7d 2c 0a 20 20    4,  3,  3},.  
29760 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 43            { OP_C
29761 6f 6e 63 61 74 2c 20 20 20 20 20 20 35 2c 20 20  oncat,      5,  
29762 33 2c 20 20 33 7d 2c 0a 20 20 20 20 20 20 20 20  3,  3},.        
29763 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c      { OP_Concat,
29764 20 20 20 20 20 20 36 2c 20 20 33 2c 20 20 33 7d        6,  3,  3}
29765 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  ,.            { 
29766 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 20 20  OP_ResultRow,   
29767 33 2c 20 20 31 2c 20 20 30 7d 2c 0a 20 20 20 20  3,  1,  0},.    
29768 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 66 50          { OP_IfP
29769 6f 73 2c 20 20 20 20 20 20 20 31 2c 20 20 30 2c  os,       1,  0,
2976a 20 20 30 7d 2c 20 20 20 20 2f 2a 20 39 20 2a 2f    0},    /* 9 */
2976b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
2976c 50 5f 48 61 6c 74 2c 20 20 20 20 20 20 20 20 30  P_Halt,        0
2976d 2c 20 20 30 2c 20 20 30 7d 2c 0a 20 20 20 20 20  ,  0,  0},.     
2976e 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 20 20       };.        
2976f 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74    sqlite3Generat
29770 65 49 6e 64 65 78 4b 65 79 28 70 50 61 72 73 65  eIndexKey(pParse
29771 2c 20 70 49 64 78 2c 20 31 2c 20 33 2c 20 31 29  , pIdx, 1, 3, 1)
29772 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 6d 70 32  ;.          jmp2
29773 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
29774 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e 64  dOp3(v, OP_Found
29775 2c 20 6a 2b 32 2c 20 30 2c 20 33 29 3b 0a 20 20  , j+2, 0, 3);.  
29776 20 20 20 20 20 20 20 20 61 64 64 72 20 3d 20 73          addr = s
29777 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c  qlite3VdbeAddOpL
29778 69 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65  ist(v, ArraySize
29779 28 69 64 78 45 72 72 29 2c 20 69 64 78 45 72 72  (idxErr), idxErr
2977a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
2977b 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
2977c 28 76 2c 20 61 64 64 72 2b 31 2c 20 22 72 6f 77  (v, addr+1, "row
2977d 69 64 20 22 2c 20 50 34 5f 53 54 41 54 49 43 29  id ", P4_STATIC)
2977e 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
2977f 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
29780 76 2c 20 61 64 64 72 2b 33 2c 20 22 20 6d 69 73  v, addr+3, " mis
29781 73 69 6e 67 20 66 72 6f 6d 20 69 6e 64 65 78 20  sing from index 
29782 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  ", P4_STATIC);. 
29783 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
29784 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
29785 61 64 64 72 2b 34 2c 20 70 49 64 78 2d 3e 7a 4e  addr+4, pIdx->zN
29786 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  ame, P4_STATIC);
29787 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
29788 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
29789 2c 20 61 64 64 72 2b 39 29 3b 0a 20 20 20 20 20  , addr+9);.     
2978a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2978b 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 6d 70 32  JumpHere(v, jmp2
2978c 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
2978d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2978e 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
2978f 74 2c 20 31 2c 20 6c 6f 6f 70 54 6f 70 2b 31 29  t, 1, loopTop+1)
29790 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
29791 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
29792 20 6c 6f 6f 70 54 6f 70 29 3b 0a 20 20 20 20 20   loopTop);.     
29793 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64 78     for(j=0, pIdx
29794 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
29795 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
29796 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20 20  pNext, j++){.   
29797 20 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f         static co
29798 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 63  nst VdbeOpList c
29799 6e 74 49 64 78 5b 5d 20 3d 20 7b 0a 20 20 20 20  ntIdx[] = {.    
2979a 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e           { OP_In
2979b 74 65 67 65 72 2c 20 20 20 20 20 20 30 2c 20 20  teger,      0,  
2979c 33 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20  3,  0},.        
2979d 20 20 20 20 20 7b 20 4f 50 5f 52 65 77 69 6e 64       { OP_Rewind
2979e 2c 20 20 20 20 20 20 20 30 2c 20 20 30 2c 20 20  ,       0,  0,  
2979f 30 7d 2c 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20  0},  /* 1 */.   
297a0 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 41            { OP_A
297a1 64 64 49 6d 6d 2c 20 20 20 20 20 20 20 33 2c 20  ddImm,       3, 
297a2 20 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20   1,  0},.       
297a3 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65 78 74 2c        { OP_Next,
297a4 20 20 20 20 20 20 20 20 20 30 2c 20 20 30 2c 20           0,  0, 
297a5 20 30 7d 2c 20 20 2f 2a 20 33 20 2a 2f 0a 20 20   0},  /* 3 */.  
297a6 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
297a7 45 71 2c 20 20 20 20 20 20 20 20 20 20 20 32 2c  Eq,           2,
297a8 20 20 30 2c 20 20 33 7d 2c 20 20 2f 2a 20 34 20    0,  3},  /* 4 
297a9 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
297aa 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20 20  { OP_AddImm,    
297ab 20 20 20 31 2c 20 2d 31 2c 20 20 30 7d 2c 0a 20     1, -1,  0},. 
297ac 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
297ad 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20 20 30  _String8,      0
297ae 2c 20 20 32 2c 20 20 30 7d 2c 20 20 2f 2a 20 36  ,  2,  0},  /* 6
297af 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
297b0 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20   { OP_String8,  
297b1 20 20 20 20 30 2c 20 20 33 2c 20 20 30 7d 2c 20      0,  3,  0}, 
297b2 20 2f 2a 20 37 20 2a 2f 0a 20 20 20 20 20 20 20   /* 7 */.       
297b3 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61        { OP_Conca
297b4 74 2c 20 20 20 20 20 20 20 33 2c 20 20 32 2c 20  t,       3,  2, 
297b5 20 32 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20   2},.           
297b6 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77    { OP_ResultRow
297b7 2c 20 20 20 20 32 2c 20 20 31 2c 20 20 30 7d 2c  ,    2,  1,  0},
297b8 0a 20 20 20 20 20 20 20 20 20 20 7d 3b 0a 20 20  .          };.  
297b9 20 20 20 20 20 20 20 20 61 64 64 72 20 3d 20 73          addr = s
297ba 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
297bb 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31 29  (v, OP_IfPos, 1)
297bc 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
297bd 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
297be 20 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30 29 3b   OP_Halt, 0, 0);
297bf 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
297c0 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
297c1 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 20  , addr);.       
297c2 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
297c3 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76  3VdbeAddOpList(v
297c4 2c 20 41 72 72 61 79 53 69 7a 65 28 63 6e 74 49  , ArraySize(cntI
297c5 64 78 29 2c 20 63 6e 74 49 64 78 29 3b 0a 20 20  dx), cntIdx);.  
297c6 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
297c7 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61  dbeChangeP1(v, a
297c8 64 64 72 2b 31 2c 20 6a 2b 32 29 3b 0a 20 20 20  ddr+1, j+2);.   
297c9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
297ca 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61 64  beChangeP2(v, ad
297cb 64 72 2b 31 2c 20 61 64 64 72 2b 34 29 3b 0a 20  dr+1, addr+4);. 
297cc 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
297cd 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
297ce 61 64 64 72 2b 33 2c 20 6a 2b 32 29 3b 0a 20 20  addr+3, j+2);.  
297cf 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
297d0 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61  dbeChangeP2(v, a
297d1 64 64 72 2b 33 2c 20 61 64 64 72 2b 32 29 3b 0a  ddr+3, addr+2);.
297d2 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
297d3 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
297d4 20 61 64 64 72 2b 34 29 3b 0a 20 20 20 20 20 20   addr+4);.      
297d5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
297d6 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2b  hangeP4(v, addr+
297d7 36 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  6, .            
297d8 20 20 20 20 20 20 20 20 20 22 77 72 6f 6e 67 20           "wrong 
297d9 23 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  # of entries in 
297da 69 6e 64 65 78 20 22 2c 20 50 34 5f 53 54 41 54  index ", P4_STAT
297db 49 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  IC);.          s
297dc 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
297dd 50 34 28 76 2c 20 61 64 64 72 2b 37 2c 20 70 49  P4(v, addr+7, pI
297de 64 78 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54  dx->zName, P4_ST
297df 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 7d  ATIC);.        }
297e0 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20 7d 0a  .      } .    }.
297e1 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
297e2 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28  e3VdbeAddOpList(
297e3 76 2c 20 41 72 72 61 79 53 69 7a 65 28 65 6e 64  v, ArraySize(end
297e4 43 6f 64 65 29 2c 20 65 6e 64 43 6f 64 65 29 3b  Code), endCode);
297e5 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
297e6 43 68 61 6e 67 65 50 32 28 76 2c 20 61 64 64 72  ChangeP2(v, addr
297e7 2c 20 2d 6d 78 45 72 72 29 3b 0a 20 20 20 20 73  , -mxErr);.    s
297e8 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
297e9 72 65 28 76 2c 20 61 64 64 72 2b 31 29 3b 0a 20  re(v, addr+1);. 
297ea 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
297eb 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2b 32  angeP4(v, addr+2
297ec 2c 20 22 6f 6b 22 2c 20 50 34 5f 53 54 41 54 49  , "ok", P4_STATI
297ed 43 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64  C);.  }else.#end
297ee 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
297ef 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  T_INTEGRITY_CHEC
297f0 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  K */..#ifndef SQ
297f1 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
297f2 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47    /*.  **   PRAG
297f3 4d 41 20 65 6e 63 6f 64 69 6e 67 0a 20 20 2a 2a  MA encoding.  **
297f4 20 20 20 50 52 41 47 4d 41 20 65 6e 63 6f 64 69     PRAGMA encodi
297f5 6e 67 20 3d 20 22 75 74 66 2d 38 22 7c 22 75 74  ng = "utf-8"|"ut
297f6 66 2d 31 36 22 7c 22 75 74 66 2d 31 36 6c 65 22  f-16"|"utf-16le"
297f7 7c 22 75 74 66 2d 31 36 62 65 22 0a 20 20 2a 2a  |"utf-16be".  **
297f8 0a 20 20 2a 2a 20 49 6e 20 69 74 73 20 66 69 72  .  ** In its fir
297f9 73 74 20 66 6f 72 6d 2c 20 74 68 69 73 20 70 72  st form, this pr
297fa 61 67 6d 61 20 72 65 74 75 72 6e 73 20 74 68 65  agma returns the
297fb 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65   encoding of the
297fc 20 6d 61 69 6e 0a 20 20 2a 2a 20 64 61 74 61 62   main.  ** datab
297fd 61 73 65 2e 20 49 66 20 74 68 65 20 64 61 74 61  ase. If the data
297fe 62 61 73 65 20 69 73 20 6e 6f 74 20 69 6e 69 74  base is not init
297ff 69 61 6c 69 7a 65 64 2c 20 69 74 20 69 73 20 69  ialized, it is i
29800 6e 69 74 69 61 6c 69 7a 65 64 20 6e 6f 77 2e 0a  nitialized now..
29801 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 65    **.  ** The se
29802 63 6f 6e 64 20 66 6f 72 6d 20 6f 66 20 74 68 69  cond form of thi
29803 73 20 70 72 61 67 6d 61 20 69 73 20 61 20 6e 6f  s pragma is a no
29804 2d 6f 70 20 69 66 20 74 68 65 20 6d 61 69 6e 20  -op if the main 
29805 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20  database file.  
29806 2a 2a 20 68 61 73 20 6e 6f 74 20 61 6c 72 65 61  ** has not alrea
29807 64 79 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69  dy been initiali
29808 7a 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73  zed. In this cas
29809 65 20 69 74 20 73 65 74 73 20 74 68 65 20 64 65  e it sets the de
2980a 66 61 75 6c 74 0a 20 20 2a 2a 20 65 6e 63 6f 64  fault.  ** encod
2980b 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 62 65  ing that will be
2980c 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 6d 61   used for the ma
2980d 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
2980e 20 69 66 20 61 20 6e 65 77 20 66 69 6c 65 0a 20   if a new file. 
2980f 20 2a 2a 20 69 73 20 63 72 65 61 74 65 64 2e 20   ** is created. 
29810 49 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6d  If an existing m
29811 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c  ain database fil
29812 65 20 69 73 20 6f 70 65 6e 65 64 2c 20 74 68 65  e is opened, the
29813 6e 20 74 68 65 0a 20 20 2a 2a 20 64 65 66 61 75  n the.  ** defau
29814 6c 74 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  lt text encoding
29815 20 66 6f 72 20 74 68 65 20 65 78 69 73 74 69 6e   for the existin
29816 67 20 64 61 74 61 62 61 73 65 20 69 73 20 75 73  g database is us
29817 65 64 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 49  ed..  ** .  ** I
29818 6e 20 61 6c 6c 20 63 61 73 65 73 20 6e 65 77 20  n all cases new 
29819 64 61 74 61 62 61 73 65 73 20 63 72 65 61 74 65  databases create
2981a 64 20 75 73 69 6e 67 20 74 68 65 20 41 54 54 41  d using the ATTA
2981b 43 48 20 63 6f 6d 6d 61 6e 64 20 61 72 65 0a 20  CH command are. 
2981c 20 2a 2a 20 63 72 65 61 74 65 64 20 74 6f 20 75   ** created to u
2981d 73 65 20 74 68 65 20 73 61 6d 65 20 64 65 66 61  se the same defa
2981e 75 6c 74 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  ult text encodin
2981f 67 20 61 73 20 74 68 65 20 6d 61 69 6e 20 64 61  g as the main da
29820 74 61 62 61 73 65 2e 20 49 66 0a 20 20 2a 2a 20  tabase. If.  ** 
29821 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
29822 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 69  e has not been i
29823 6e 69 74 69 61 6c 69 7a 65 64 20 61 6e 64 2f 6f  nitialized and/o
29824 72 20 63 72 65 61 74 65 64 20 77 68 65 6e 20 41  r created when A
29825 54 54 41 43 48 0a 20 20 2a 2a 20 69 73 20 65 78  TTACH.  ** is ex
29826 65 63 75 74 65 64 2c 20 74 68 69 73 20 69 73 20  ecuted, this is 
29827 64 6f 6e 65 20 62 65 66 6f 72 65 20 74 68 65 20  done before the 
29828 41 54 54 41 43 48 20 6f 70 65 72 61 74 69 6f 6e  ATTACH operation
29829 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 74  ..  **.  ** In t
2982a 68 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 74  he second form t
2982b 68 69 73 20 70 72 61 67 6d 61 20 73 65 74 73 20  his pragma sets 
2982c 74 68 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  the text encodin
2982d 67 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e 0a  g to be used in.
2982e 20 20 2a 2a 20 6e 65 77 20 64 61 74 61 62 61 73    ** new databas
2982f 65 20 66 69 6c 65 73 20 63 72 65 61 74 65 64 20  e files created 
29830 75 73 69 6e 67 20 74 68 69 73 20 64 61 74 61 62  using this datab
29831 61 73 65 20 68 61 6e 64 6c 65 2e 20 49 74 20 69  ase handle. It i
29832 73 20 6f 6e 6c 79 0a 20 20 2a 2a 20 75 73 65 66  s only.  ** usef
29833 75 6c 20 69 66 20 69 6e 76 6f 6b 65 64 20 69 6d  ul if invoked im
29834 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20  mediately after 
29835 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
29836 65 20 69 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  e i.  */.  if( s
29837 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
29838 65 66 74 2c 20 22 65 6e 63 6f 64 69 6e 67 22 29  eft, "encoding")
29839 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 61 74 69  ==0 ){.    stati
2983a 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 45  c const struct E
2983b 6e 63 4e 61 6d 65 20 7b 0a 20 20 20 20 20 20 63  ncName {.      c
2983c 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 20 20  har *zName;.    
2983d 20 20 75 38 20 65 6e 63 3b 0a 20 20 20 20 7d 20    u8 enc;.    } 
2983e 65 6e 63 6e 61 6d 65 73 5b 5d 20 3d 20 7b 0a 20  encnames[] = {. 
2983f 20 20 20 20 20 7b 20 22 55 54 46 38 22 2c 20 20       { "UTF8",  
29840 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 20 20     SQLITE_UTF8  
29841 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 20 20 7b        },.      {
29842 20 22 55 54 46 2d 38 22 2c 20 20 20 20 53 51 4c   "UTF-8",    SQL
29843 49 54 45 5f 55 54 46 38 20 20 20 20 20 20 20 20  ITE_UTF8        
29844 7d 2c 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 65  },  /* Must be e
29845 6c 65 6d 65 6e 74 20 5b 31 5d 20 2a 2f 0a 20 20  lement [1] */.  
29846 20 20 20 20 7b 20 22 55 54 46 2d 31 36 6c 65 22      { "UTF-16le"
29847 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  , SQLITE_UTF16LE
29848 20 20 20 20 20 7d 2c 20 20 2f 2a 20 4d 75 73 74       },  /* Must
29849 20 62 65 20 65 6c 65 6d 65 6e 74 20 5b 32 5d 20   be element [2] 
2984a 2a 2f 0a 20 20 20 20 20 20 7b 20 22 55 54 46 2d  */.      { "UTF-
2984b 31 36 62 65 22 2c 20 53 51 4c 49 54 45 5f 55 54  16be", SQLITE_UT
2984c 46 31 36 42 45 20 20 20 20 20 7d 2c 20 20 2f 2a  F16BE     },  /*
2984d 20 4d 75 73 74 20 62 65 20 65 6c 65 6d 65 6e 74   Must be element
2984e 20 5b 33 5d 20 2a 2f 0a 20 20 20 20 20 20 7b 20   [3] */.      { 
2984f 22 55 54 46 31 36 6c 65 22 2c 20 20 53 51 4c 49  "UTF16le",  SQLI
29850 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20 20 7d  TE_UTF16LE     }
29851 2c 0a 20 20 20 20 20 20 7b 20 22 55 54 46 31 36  ,.      { "UTF16
29852 62 65 22 2c 20 20 53 51 4c 49 54 45 5f 55 54 46  be",  SQLITE_UTF
29853 31 36 42 45 20 20 20 20 20 7d 2c 0a 20 20 20 20  16BE     },.    
29854 20 20 7b 20 22 55 54 46 2d 31 36 22 2c 20 20 20    { "UTF-16",   
29855 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
29856 20 20 20 7d 2c 20 2f 2a 20 53 51 4c 49 54 45 5f     }, /* SQLITE_
29857 55 54 46 31 36 4e 41 54 49 56 45 20 2a 2f 0a 20  UTF16NATIVE */. 
29858 20 20 20 20 20 7b 20 22 55 54 46 31 36 22 2c 20       { "UTF16", 
29859 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20     0            
2985a 20 20 20 20 20 20 7d 2c 20 2f 2a 20 53 51 4c 49        }, /* SQLI
2985b 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20 2a  TE_UTF16NATIVE *
2985c 2f 0a 20 20 20 20 20 20 7b 20 30 2c 20 30 20 7d  /.      { 0, 0 }
2985d 0a 20 20 20 20 7d 3b 0a 20 20 20 20 63 6f 6e 73  .    };.    cons
2985e 74 20 73 74 72 75 63 74 20 45 6e 63 4e 61 6d 65  t struct EncName
2985f 20 2a 70 45 6e 63 3b 0a 20 20 20 20 69 66 28 20   *pEnc;.    if( 
29860 21 7a 52 69 67 68 74 20 29 7b 20 20 20 20 2f 2a  !zRight ){    /*
29861 20 22 50 52 41 47 4d 41 20 65 6e 63 6f 64 69 6e   "PRAGMA encodin
29862 67 22 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  g" */.      if( 
29863 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
29864 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f  a(pParse) ) goto
29865 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20   pragma_out;.   
29866 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
29867 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a  tNumCols(v, 1);.
29868 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29869 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
2986a 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
2986b 22 65 6e 63 6f 64 69 6e 67 22 2c 20 53 51 4c 49  "encoding", SQLI
2986c 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
2986d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2986e 4f 70 32 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op2(v, OP_String
2986f 38 2c 20 30 2c 20 31 29 3b 0a 20 20 20 20 20 20  8, 0, 1);.      
29870 61 73 73 65 72 74 28 20 65 6e 63 6e 61 6d 65 73  assert( encnames
29871 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d 2e 65 6e  [SQLITE_UTF8].en
29872 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29  c==SQLITE_UTF8 )
29873 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
29874 65 6e 63 6e 61 6d 65 73 5b 53 51 4c 49 54 45 5f  encnames[SQLITE_
29875 55 54 46 31 36 4c 45 5d 2e 65 6e 63 3d 3d 53 51  UTF16LE].enc==SQ
29876 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 29 3b 0a  LITE_UTF16LE );.
29877 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 6e        assert( en
29878 63 6e 61 6d 65 73 5b 53 51 4c 49 54 45 5f 55 54  cnames[SQLITE_UT
29879 46 31 36 42 45 5d 2e 65 6e 63 3d 3d 53 51 4c 49  F16BE].enc==SQLI
2987a 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20 20  TE_UTF16BE );.  
2987b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2987c 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 65  hangeP4(v, -1, e
2987d 6e 63 6e 61 6d 65 73 5b 45 4e 43 28 70 50 61 72  ncnames[ENC(pPar
2987e 73 65 2d 3e 64 62 29 5d 2e 7a 4e 61 6d 65 2c 20  se->db)].zName, 
2987f 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
29880 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29881 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
29882 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20  Row, 1, 1);.    
29883 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20  }else{          
29884 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
29885 20 22 50 52 41 47 4d 41 20 65 6e 63 6f 64 69 6e   "PRAGMA encodin
29886 67 20 3d 20 58 58 58 22 20 2a 2f 0a 20 20 20 20  g = XXX" */.    
29887 20 20 2f 2a 20 4f 6e 6c 79 20 63 68 61 6e 67 65    /* Only change
29888 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 73 71   the value of sq
29889 6c 69 74 65 2e 65 6e 63 20 69 66 20 74 68 65 20  lite.enc if the 
2988a 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
2988b 69 73 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20  is not.      ** 
2988c 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 49 66 20  initialized. If 
2988d 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
2988e 65 20 65 78 69 73 74 73 2c 20 74 68 65 20 6e 65  e exists, the ne
2988f 77 20 73 71 6c 69 74 65 2e 65 6e 63 20 76 61 6c  w sqlite.enc val
29890 75 65 0a 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c  ue.      ** will
29891 20 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20   be overwritten 
29892 77 68 65 6e 20 74 68 65 20 73 63 68 65 6d 61 20  when the schema 
29893 69 73 20 6e 65 78 74 20 6c 6f 61 64 65 64 2e 20  is next loaded. 
29894 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 0a 20  If it does not. 
29895 20 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20       ** already 
29896 65 78 69 73 74 73 2c 20 69 74 20 77 69 6c 6c 20  exists, it will 
29897 62 65 20 63 72 65 61 74 65 64 20 74 6f 20 75 73  be created to us
29898 65 20 74 68 65 20 6e 65 77 20 65 6e 63 6f 64 69  e the new encodi
29899 6e 67 20 76 61 6c 75 65 2e 0a 20 20 20 20 20 20  ng value..      
2989a 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 0a 20 20  */.      if( .  
2989b 20 20 20 20 20 20 21 28 44 62 48 61 73 50 72 6f        !(DbHasPro
2989c 70 65 72 74 79 28 64 62 2c 20 30 2c 20 44 42 5f  perty(db, 0, DB_
2989d 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 29 20 7c  SchemaLoaded)) |
2989e 7c 20 0a 20 20 20 20 20 20 20 20 44 62 48 61 73  | .        DbHas
2989f 50 72 6f 70 65 72 74 79 28 64 62 2c 20 30 2c 20  Property(db, 0, 
298a0 44 42 5f 45 6d 70 74 79 29 20 0a 20 20 20 20 20  DB_Empty) .     
298a1 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28   ){.        for(
298a2 70 45 6e 63 3d 26 65 6e 63 6e 61 6d 65 73 5b 30  pEnc=&encnames[0
298a3 5d 3b 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65 3b 20  ]; pEnc->zName; 
298a4 70 45 6e 63 2b 2b 29 7b 0a 20 20 20 20 20 20 20  pEnc++){.       
298a5 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65     if( 0==sqlite
298a6 33 53 74 72 49 43 6d 70 28 7a 52 69 67 68 74 2c  3StrICmp(zRight,
298a7 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65 29 20 29 7b   pEnc->zName) ){
298a8 0a 20 20 20 20 20 20 20 20 20 20 20 20 45 4e 43  .            ENC
298a9 28 70 50 61 72 73 65 2d 3e 64 62 29 20 3d 20 70  (pParse->db) = p
298aa 45 6e 63 2d 3e 65 6e 63 20 3f 20 70 45 6e 63 2d  Enc->enc ? pEnc-
298ab 3e 65 6e 63 20 3a 20 53 51 4c 49 54 45 5f 55 54  >enc : SQLITE_UT
298ac 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 20 20 20  F16NATIVE;.     
298ad 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
298ae 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
298af 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
298b0 21 70 45 6e 63 2d 3e 7a 4e 61 6d 65 20 29 7b 0a  !pEnc->zName ){.
298b1 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
298b2 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
298b3 2c 20 22 75 6e 73 75 70 70 6f 72 74 65 64 20 65  , "unsupported e
298b4 6e 63 6f 64 69 6e 67 3a 20 25 73 22 2c 20 7a 52  ncoding: %s", zR
298b5 69 67 68 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  ight);.        }
298b6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
298b7 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a   }else.#endif /*
298b8 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
298b9 31 36 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  16 */..#ifndef S
298ba 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d  QLITE_OMIT_SCHEM
298bb 41 5f 56 45 52 53 49 4f 4e 5f 50 52 41 47 4d 41  A_VERSION_PRAGMA
298bc 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52  S.  /*.  **   PR
298bd 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
298be 73 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e 0a 20  schema_version. 
298bf 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61   **   PRAGMA [da
298c0 74 61 62 61 73 65 2e 5d 73 63 68 65 6d 61 5f 76  tabase.]schema_v
298c1 65 72 73 69 6f 6e 20 3d 20 3c 69 6e 74 65 67 65  ersion = <intege
298c2 72 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 50  r>.  **.  **   P
298c3 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
298c4 5d 75 73 65 72 5f 76 65 72 73 69 6f 6e 0a 20 20  ]user_version.  
298c5 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74  **   PRAGMA [dat
298c6 61 62 61 73 65 2e 5d 75 73 65 72 5f 76 65 72 73  abase.]user_vers
298c7 69 6f 6e 20 3d 20 3c 69 6e 74 65 67 65 72 3e 0a  ion = <integer>.
298c8 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 70 72    **.  ** The pr
298c9 61 67 6d 61 27 73 20 73 63 68 65 6d 61 5f 76 65  agma's schema_ve
298ca 72 73 69 6f 6e 20 61 6e 64 20 75 73 65 72 5f 76  rsion and user_v
298cb 65 72 73 69 6f 6e 20 61 72 65 20 75 73 65 64 20  ersion are used 
298cc 74 6f 20 73 65 74 20 6f 72 20 67 65 74 0a 20 20  to set or get.  
298cd 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
298ce 74 68 65 20 73 63 68 65 6d 61 2d 76 65 72 73 69  the schema-versi
298cf 6f 6e 20 61 6e 64 20 75 73 65 72 2d 76 65 72 73  on and user-vers
298d0 69 6f 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c  ion, respectivel
298d1 79 2e 20 42 6f 74 68 0a 20 20 2a 2a 20 74 68 65  y. Both.  ** the
298d2 20 73 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20   schema-version 
298d3 61 6e 64 20 74 68 65 20 75 73 65 72 2d 76 65 72  and the user-ver
298d4 73 69 6f 6e 20 61 72 65 20 33 32 2d 62 69 74 20  sion are 32-bit 
298d5 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73 0a  signed integers.
298d6 20 20 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 74    ** stored in t
298d7 68 65 20 64 61 74 61 62 61 73 65 20 68 65 61 64  he database head
298d8 65 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  er..  **.  ** Th
298d9 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20  e schema-cookie 
298da 69 73 20 75 73 75 61 6c 6c 79 20 6f 6e 6c 79 20  is usually only 
298db 6d 61 6e 69 70 75 6c 61 74 65 64 20 69 6e 74 65  manipulated inte
298dc 72 6e 61 6c 6c 79 20 62 79 20 53 51 4c 69 74 65  rnally by SQLite
298dd 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20 69 6e 63  . It.  ** is inc
298de 72 65 6d 65 6e 74 65 64 20 62 79 20 53 51 4c 69  remented by SQLi
298df 74 65 20 77 68 65 6e 65 76 65 72 20 74 68 65 20  te whenever the 
298e0 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
298e1 69 73 20 6d 6f 64 69 66 69 65 64 20 28 62 79 0a  is modified (by.
298e2 20 20 2a 2a 20 63 72 65 61 74 69 6e 67 20 6f 72    ** creating or
298e3 20 64 72 6f 70 70 69 6e 67 20 61 20 74 61 62 6c   dropping a tabl
298e4 65 20 6f 72 20 69 6e 64 65 78 29 2e 20 54 68 65  e or index). The
298e5 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20   schema version 
298e6 69 73 20 75 73 65 64 20 62 79 0a 20 20 2a 2a 20  is used by.  ** 
298e7 53 51 4c 69 74 65 20 65 61 63 68 20 74 69 6d 65  SQLite each time
298e8 20 61 20 71 75 65 72 79 20 69 73 20 65 78 65 63   a query is exec
298e9 75 74 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74  uted to ensure t
298ea 68 61 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  hat the internal
298eb 20 63 61 63 68 65 0a 20 20 2a 2a 20 6f 66 20 74   cache.  ** of t
298ec 68 65 20 73 63 68 65 6d 61 20 75 73 65 64 20 77  he schema used w
298ed 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68  hen compiling th
298ee 65 20 53 51 4c 20 71 75 65 72 79 20 6d 61 74 63  e SQL query matc
298ef 68 65 73 20 74 68 65 20 73 63 68 65 6d 61 20 6f  hes the schema o
298f0 66 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62  f.  ** the datab
298f1 61 73 65 20 61 67 61 69 6e 73 74 20 77 68 69 63  ase against whic
298f2 68 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 71  h the compiled q
298f3 75 65 72 79 20 69 73 20 61 63 74 75 61 6c 6c 79  uery is actually
298f4 20 65 78 65 63 75 74 65 64 2e 0a 20 20 2a 2a 20   executed..  ** 
298f5 53 75 62 76 65 72 74 69 6e 67 20 74 68 69 73 20  Subverting this 
298f6 6d 65 63 68 61 6e 69 73 6d 20 62 79 20 75 73 69  mechanism by usi
298f7 6e 67 20 22 50 52 41 47 4d 41 20 73 63 68 65 6d  ng "PRAGMA schem
298f8 61 5f 76 65 72 73 69 6f 6e 22 20 74 6f 20 6d 6f  a_version" to mo
298f9 64 69 66 79 0a 20 20 2a 2a 20 74 68 65 20 73 63  dify.  ** the sc
298fa 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20 69 73 20  hema-version is 
298fb 70 6f 74 65 6e 74 69 61 6c 6c 79 20 64 61 6e 67  potentially dang
298fc 65 72 6f 75 73 20 61 6e 64 20 6d 61 79 20 6c 65  erous and may le
298fd 61 64 20 74 6f 20 70 72 6f 67 72 61 6d 0a 20 20  ad to program.  
298fe 2a 2a 20 63 72 61 73 68 65 73 20 6f 72 20 64 61  ** crashes or da
298ff 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f  tabase corruptio
29900 6e 2e 20 55 73 65 20 77 69 74 68 20 63 61 75 74  n. Use with caut
29901 69 6f 6e 21 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  ion!.  **.  ** T
29902 68 65 20 75 73 65 72 2d 76 65 72 73 69 6f 6e 20  he user-version 
29903 69 73 20 6e 6f 74 20 75 73 65 64 20 69 6e 74 65  is not used inte
29904 72 6e 61 6c 6c 79 20 62 79 20 53 51 4c 69 74 65  rnally by SQLite
29905 2e 20 49 74 20 6d 61 79 20 62 65 20 75 73 65 64  . It may be used
29906 20 62 79 0a 20 20 2a 2a 20 61 70 70 6c 69 63 61   by.  ** applica
29907 74 69 6f 6e 73 20 66 6f 72 20 61 6e 79 20 70 75  tions for any pu
29908 72 70 6f 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  rpose..  */.  if
29909 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
2990a 28 7a 4c 65 66 74 2c 20 22 73 63 68 65 6d 61 5f  (zLeft, "schema_
2990b 76 65 72 73 69 6f 6e 22 29 3d 3d 30 20 0a 20 20  version")==0 .  
2990c 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72 49 43   || sqlite3StrIC
2990d 6d 70 28 7a 4c 65 66 74 2c 20 22 75 73 65 72 5f  mp(zLeft, "user_
2990e 76 65 72 73 69 6f 6e 22 29 3d 3d 30 20 0a 20 20  version")==0 .  
2990f 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72 49 43   || sqlite3StrIC
29910 6d 70 28 7a 4c 65 66 74 2c 20 22 66 72 65 65 6c  mp(zLeft, "freel
29911 69 73 74 5f 63 6f 75 6e 74 22 29 3d 3d 30 20 0a  ist_count")==0 .
29912 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 43 6f    ){.    int iCo
29913 6f 6b 69 65 3b 20 20 20 2f 2a 20 43 6f 6f 6b 69  okie;   /* Cooki
29914 65 20 69 6e 64 65 78 2e 20 31 20 66 6f 72 20 73  e index. 1 for s
29915 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 2c 20 36 20  chema-cookie, 6 
29916 66 6f 72 20 75 73 65 72 2d 63 6f 6f 6b 69 65 2e  for user-cookie.
29917 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
29918 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20  dbeUsesBtree(v, 
29919 69 44 62 29 3b 0a 20 20 20 20 73 77 69 74 63 68  iDb);.    switch
2991a 28 20 7a 4c 65 66 74 5b 30 5d 20 29 7b 0a 20 20  ( zLeft[0] ){.  
2991b 20 20 20 20 63 61 73 65 20 27 66 27 3a 20 63 61      case 'f': ca
2991c 73 65 20 27 46 27 3a 0a 20 20 20 20 20 20 20 20  se 'F':.        
2991d 69 43 6f 6f 6b 69 65 20 3d 20 42 54 52 45 45 5f  iCookie = BTREE_
2991e 46 52 45 45 5f 50 41 47 45 5f 43 4f 55 4e 54 3b  FREE_PAGE_COUNT;
2991f 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
29920 20 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 20        case 's': 
29921 63 61 73 65 20 27 53 27 3a 0a 20 20 20 20 20 20  case 'S':.      
29922 20 20 69 43 6f 6f 6b 69 65 20 3d 20 42 54 52 45    iCookie = BTRE
29923 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e  E_SCHEMA_VERSION
29924 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
29925 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a  .      default:.
29926 20 20 20 20 20 20 20 20 69 43 6f 6f 6b 69 65 20          iCookie 
29927 3d 20 42 54 52 45 45 5f 55 53 45 52 5f 56 45 52  = BTREE_USER_VER
29928 53 49 4f 4e 3b 0a 20 20 20 20 20 20 20 20 62 72  SION;.        br
29929 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
2992a 69 66 28 20 7a 52 69 67 68 74 20 26 26 20 69 43  if( zRight && iC
2992b 6f 6f 6b 69 65 21 3d 42 54 52 45 45 5f 46 52 45  ookie!=BTREE_FRE
2992c 45 5f 50 41 47 45 5f 43 4f 55 4e 54 20 29 7b 0a  E_PAGE_COUNT ){.
2992d 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
2992e 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f 6f  he specified coo
2992f 6b 69 65 20 76 61 6c 75 65 20 2a 2f 0a 20 20 20  kie value */.   
29930 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
29931 56 64 62 65 4f 70 4c 69 73 74 20 73 65 74 43 6f  VdbeOpList setCo
29932 6f 6b 69 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  okie[] = {.     
29933 20 20 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63 74     { OP_Transact
29934 69 6f 6e 2c 20 20 20 20 30 2c 20 20 31 2c 20 20  ion,    0,  1,  
29935 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20  0},    /* 0 */. 
29936 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65         { OP_Inte
29937 67 65 72 2c 20 20 20 20 20 20 20 20 30 2c 20 20  ger,        0,  
29938 31 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31 20  1,  0},    /* 1 
29939 2a 2f 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f  */.        { OP_
2993a 53 65 74 43 6f 6f 6b 69 65 2c 20 20 20 20 20 20  SetCookie,      
2993b 30 2c 20 20 30 2c 20 20 31 7d 2c 20 20 20 20 2f  0,  0,  1},    /
2993c 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20 7d 3b 0a  * 2 */.      };.
2993d 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 20 3d        int addr =
2993e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2993f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53 69  pList(v, ArraySi
29940 7a 65 28 73 65 74 43 6f 6f 6b 69 65 29 2c 20 73  ze(setCookie), s
29941 65 74 43 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 20  etCookie);.     
29942 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
29943 67 65 50 31 28 76 2c 20 61 64 64 72 2c 20 69 44  geP1(v, addr, iD
29944 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
29945 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
29946 20 61 64 64 72 2b 31 2c 20 61 74 6f 69 28 7a 52   addr+1, atoi(zR
29947 69 67 68 74 29 29 3b 0a 20 20 20 20 20 20 73 71  ight));.      sq
29948 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
29949 31 28 76 2c 20 61 64 64 72 2b 32 2c 20 69 44 62  1(v, addr+2, iDb
2994a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2994b 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20  VdbeChangeP2(v, 
2994c 61 64 64 72 2b 32 2c 20 69 43 6f 6f 6b 69 65 29  addr+2, iCookie)
2994d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2994e 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 73     /* Read the s
2994f 70 65 63 69 66 69 65 64 20 63 6f 6f 6b 69 65 20  pecified cookie 
29950 76 61 6c 75 65 20 2a 2f 0a 20 20 20 20 20 20 73  value */.      s
29951 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65  tatic const Vdbe
29952 4f 70 4c 69 73 74 20 72 65 61 64 43 6f 6f 6b 69  OpList readCooki
29953 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20  e[] = {.        
29954 7b 20 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e  { OP_Transaction
29955 2c 20 20 20 20 20 30 2c 20 20 30 2c 20 20 30 7d  ,     0,  0,  0}
29956 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20  ,    /* 0 */.   
29957 20 20 20 20 20 7b 20 4f 50 5f 52 65 61 64 43 6f       { OP_ReadCo
29958 6f 6b 69 65 2c 20 20 20 20 20 20 30 2c 20 20 31  okie,      0,  1
29959 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31 20 2a  ,  0},    /* 1 *
2995a 2f 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 52  /.        { OP_R
2995b 65 73 75 6c 74 52 6f 77 2c 20 20 20 20 20 20 20  esultRow,       
2995c 31 2c 20 20 31 2c 20 20 30 7d 0a 20 20 20 20 20  1,  1,  0}.     
2995d 20 7d 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 64   };.      int ad
2995e 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
2995f 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72  AddOpList(v, Arr
29960 61 79 53 69 7a 65 28 72 65 61 64 43 6f 6f 6b 69  aySize(readCooki
29961 65 29 2c 20 72 65 61 64 43 6f 6f 6b 69 65 29 3b  e), readCookie);
29962 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29963 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64  beChangeP1(v, ad
29964 64 72 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20  dr, iDb);.      
29965 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
29966 65 50 31 28 76 2c 20 61 64 64 72 2b 31 2c 20 69  eP1(v, addr+1, i
29967 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Db);.      sqlit
29968 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76  e3VdbeChangeP3(v
29969 2c 20 61 64 64 72 2b 31 2c 20 69 43 6f 6f 6b 69  , addr+1, iCooki
2996a 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
2996b 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
2996c 76 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  v, 1);.      sql
2996d 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
2996e 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
2996f 5f 4e 41 4d 45 2c 20 7a 4c 65 66 74 2c 20 53 51  _NAME, zLeft, SQ
29970 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
29971 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23  .    }.  }else.#
29972 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
29973 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 56 45 52 53  OMIT_SCHEMA_VERS
29974 49 4f 4e 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a  ION_PRAGMAS */..
29975 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
29976 54 45 5f 44 45 42 55 47 29 20 7c 7c 20 64 65 66  TE_DEBUG) || def
29977 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
29978 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 70 6f  ).  /*.  ** Repo
29979 72 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  rt the current s
2997a 74 61 74 65 20 6f 66 20 66 69 6c 65 20 6c 6f 67  tate of file log
2997b 73 20 66 6f 72 20 61 6c 6c 20 64 61 74 61 62 61  s for all databa
2997c 73 65 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ses.  */.  if( s
2997d 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
2997e 65 66 74 2c 20 22 6c 6f 63 6b 5f 73 74 61 74 75  eft, "lock_statu
2997f 73 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74  s")==0 ){.    st
29980 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
29981 2a 63 6f 6e 73 74 20 61 7a 4c 6f 63 6b 4e 61 6d  *const azLockNam
29982 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 22 75  e[] = {.      "u
29983 6e 6c 6f 63 6b 65 64 22 2c 20 22 73 68 61 72 65  nlocked", "share
29984 64 22 2c 20 22 72 65 73 65 72 76 65 64 22 2c 20  d", "reserved", 
29985 22 70 65 6e 64 69 6e 67 22 2c 20 22 65 78 63 6c  "pending", "excl
29986 75 73 69 76 65 22 0a 20 20 20 20 7d 3b 0a 20 20  usive".    };.  
29987 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c    int i;.    sql
29988 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
29989 6c 73 28 76 2c 20 32 29 3b 0a 20 20 20 20 70 50  ls(v, 2);.    pP
2998a 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 32 3b 0a  arse->nMem = 2;.
2998b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2998c 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
2998d 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 64  COLNAME_NAME, "d
2998e 61 74 61 62 61 73 65 22 2c 20 53 51 4c 49 54 45  atabase", SQLITE
2998f 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71  _STATIC);.    sq
29990 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
29991 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d  ame(v, 1, COLNAM
29992 45 5f 4e 41 4d 45 2c 20 22 73 74 61 74 75 73 22  E_NAME, "status"
29993 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
29994 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
29995 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
29996 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74        Btree *pBt
29997 3b 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a 70  ;.      Pager *p
29998 50 61 67 65 72 3b 0a 20 20 20 20 20 20 63 6f 6e  Pager;.      con
29999 73 74 20 63 68 61 72 20 2a 7a 53 74 61 74 65 20  st char *zState 
2999a 3d 20 22 75 6e 6b 6e 6f 77 6e 22 3b 0a 20 20 20  = "unknown";.   
2999b 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
2999c 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a  if( db->aDb[i].z
2999d 4e 61 6d 65 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  Name==0 ) contin
2999e 75 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ue;.      sqlite
2999f 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
299a0 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 31 2c  P_String8, 0, 1,
299a1 20 30 2c 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a   0, db->aDb[i].z
299a2 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29  Name, P4_STATIC)
299a3 3b 0a 20 20 20 20 20 20 70 42 74 20 3d 20 64 62  ;.      pBt = db
299a4 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20  ->aDb[i].pBt;.  
299a5 20 20 20 20 69 66 28 20 70 42 74 3d 3d 30 20 7c      if( pBt==0 |
299a6 7c 20 28 70 50 61 67 65 72 20 3d 20 73 71 6c 69  | (pPager = sqli
299a7 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 42  te3BtreePager(pB
299a8 74 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  t))==0 ){.      
299a9 20 20 7a 53 74 61 74 65 20 3d 20 22 63 6c 6f 73    zState = "clos
299aa 65 64 22 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ed";.      }else
299ab 20 69 66 28 20 73 71 6c 69 74 65 33 5f 66 69 6c   if( sqlite3_fil
299ac 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20 69 20  e_control(db, i 
299ad 3f 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61  ? db->aDb[i].zNa
299ae 6d 65 20 3a 20 30 2c 20 0a 20 20 20 20 20 20 20  me : 0, .       
299af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
299b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
299b1 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53  LITE_FCNTL_LOCKS
299b2 54 41 54 45 2c 20 26 6a 29 3d 3d 53 51 4c 49 54  TATE, &j)==SQLIT
299b3 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
299b4 20 7a 53 74 61 74 65 20 3d 20 61 7a 4c 6f 63 6b   zState = azLock
299b5 4e 61 6d 65 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d  Name[j];.      }
299b6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
299b7 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
299b8 74 72 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30 2c  tring8, 0, 2, 0,
299b9 20 7a 53 74 61 74 65 2c 20 50 34 5f 53 54 41 54   zState, P4_STAT
299ba 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
299bb 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
299bc 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c  OP_ResultRow, 1,
299bd 20 32 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65   2);.    }..  }e
299be 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20  lse.#endif..#if 
299bf 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
299c0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
299c1 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 6b 65  rICmp(zLeft, "ke
299c2 79 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74  y")==0 && zRight
299c3 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
299c4 6b 65 79 28 64 62 2c 20 7a 52 69 67 68 74 2c 20  key(db, zRight, 
299c5 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
299c6 7a 52 69 67 68 74 29 29 3b 0a 20 20 7d 65 6c 73  zRight));.  }els
299c7 65 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  e.  if( sqlite3S
299c8 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 72  trICmp(zLeft, "r
299c9 65 6b 65 79 22 29 3d 3d 30 20 26 26 20 7a 52 69  ekey")==0 && zRi
299ca 67 68 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ght ){.    sqlit
299cb 65 33 5f 72 65 6b 65 79 28 64 62 2c 20 7a 52 69  e3_rekey(db, zRi
299cc 67 68 74 2c 20 73 71 6c 69 74 65 33 53 74 72 6c  ght, sqlite3Strl
299cd 65 6e 33 30 28 7a 52 69 67 68 74 29 29 3b 0a 20  en30(zRight));. 
299ce 20 7d 65 6c 73 65 0a 20 20 69 66 28 20 7a 52 69   }else.  if( zRi
299cf 67 68 74 20 26 26 20 28 73 71 6c 69 74 65 33 53  ght && (sqlite3S
299d0 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 68  trICmp(zLeft, "h
299d1 65 78 6b 65 79 22 29 3d 3d 30 20 7c 7c 0a 20 20  exkey")==0 ||.  
299d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
299d3 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
299d4 65 66 74 2c 20 22 68 65 78 72 65 6b 65 79 22 29  eft, "hexrekey")
299d5 3d 3d 30 29 20 29 7b 0a 20 20 20 20 69 6e 74 20  ==0) ){.    int 
299d6 69 2c 20 68 31 2c 20 68 32 3b 0a 20 20 20 20 63  i, h1, h2;.    c
299d7 68 61 72 20 7a 4b 65 79 5b 34 30 5d 3b 0a 20 20  har zKey[40];.  
299d8 20 20 66 6f 72 28 69 3d 30 3b 20 28 68 31 20 3d    for(i=0; (h1 =
299d9 20 7a 52 69 67 68 74 5b 69 5d 29 21 3d 30 20 26   zRight[i])!=0 &
299da 26 20 28 68 32 20 3d 20 7a 52 69 67 68 74 5b 69  & (h2 = zRight[i
299db 2b 31 5d 29 21 3d 30 3b 20 69 2b 3d 32 29 7b 0a  +1])!=0; i+=2){.
299dc 20 20 20 20 20 20 68 31 20 2b 3d 20 39 2a 28 31        h1 += 9*(1
299dd 26 28 68 31 3e 3e 36 29 29 3b 0a 20 20 20 20 20  &(h1>>6));.     
299de 20 68 32 20 2b 3d 20 39 2a 28 31 26 28 68 32 3e   h2 += 9*(1&(h2>
299df 3e 36 29 29 3b 0a 20 20 20 20 20 20 7a 4b 65 79  >6));.      zKey
299e0 5b 69 2f 32 5d 20 3d 20 28 68 32 20 26 20 30 78  [i/2] = (h2 & 0x
299e1 30 66 29 20 7c 20 28 28 68 31 20 26 20 30 78 66  0f) | ((h1 & 0xf
299e2 29 3c 3c 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20  )<<4);.    }.   
299e3 20 69 66 28 20 28 7a 4c 65 66 74 5b 33 5d 20 26   if( (zLeft[3] &
299e4 20 30 78 66 29 3d 3d 30 78 62 20 29 7b 0a 20 20   0xf)==0xb ){.  
299e5 20 20 20 20 73 71 6c 69 74 65 33 5f 6b 65 79 28      sqlite3_key(
299e6 64 62 2c 20 7a 4b 65 79 2c 20 69 2f 32 29 3b 0a  db, zKey, i/2);.
299e7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
299e8 20 73 71 6c 69 74 65 33 5f 72 65 6b 65 79 28 64   sqlite3_rekey(d
299e9 62 2c 20 7a 4b 65 79 2c 20 69 2f 32 29 3b 0a 20  b, zKey, i/2);. 
299ea 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e     }.  }else.#en
299eb 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 48  dif.#if SQLITE_H
299ec 41 53 5f 43 4f 44 45 43 20 7c 7c 20 64 65 66 69  AS_CODEC || defi
299ed 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
299ee 45 5f 43 45 52 4f 44 29 0a 20 20 69 66 28 20 73  E_CEROD).  if( s
299ef 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
299f0 65 66 74 2c 20 22 61 63 74 69 76 61 74 65 5f 65  eft, "activate_e
299f1 78 74 65 6e 73 69 6f 6e 73 22 29 3d 3d 30 20 29  xtensions")==0 )
299f2 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 48 41 53  {.#if SQLITE_HAS
299f3 5f 43 4f 44 45 43 0a 20 20 20 20 69 66 28 20 73  _CODEC.    if( s
299f4 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a  qlite3StrNICmp(z
299f5 52 69 67 68 74 2c 20 22 73 65 65 2d 22 2c 20 34  Right, "see-", 4
299f6 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 65 78  )==0 ){.      ex
299f7 74 65 72 6e 20 76 6f 69 64 20 73 71 6c 69 74 65  tern void sqlite
299f8 33 5f 61 63 74 69 76 61 74 65 5f 73 65 65 28 63  3_activate_see(c
299f9 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 20  onst char*);.   
299fa 20 20 20 73 71 6c 69 74 65 33 5f 61 63 74 69 76     sqlite3_activ
299fb 61 74 65 5f 73 65 65 28 26 7a 52 69 67 68 74 5b  ate_see(&zRight[
299fc 34 5d 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  4]);.    }.#endi
299fd 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
299fe 45 4e 41 42 4c 45 5f 43 45 52 4f 44 0a 20 20 20  ENABLE_CEROD.   
299ff 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e   if( sqlite3StrN
29a00 49 43 6d 70 28 7a 52 69 67 68 74 2c 20 22 63 65  ICmp(zRight, "ce
29a01 72 6f 64 2d 22 2c 20 36 29 3d 3d 30 20 29 7b 0a  rod-", 6)==0 ){.
29a02 20 20 20 20 20 20 65 78 74 65 72 6e 20 76 6f 69        extern voi
29a03 64 20 73 71 6c 69 74 65 33 5f 61 63 74 69 76 61  d sqlite3_activa
29a04 74 65 5f 63 65 72 6f 64 28 63 6f 6e 73 74 20 63  te_cerod(const c
29a05 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 73 71 6c  har*);.      sql
29a06 69 74 65 33 5f 61 63 74 69 76 61 74 65 5f 63 65  ite3_activate_ce
29a07 72 6f 64 28 26 7a 52 69 67 68 74 5b 36 5d 29 3b  rod(&zRight[6]);
29a08 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
29a09 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 20 0a  }else.#endif.. .
29a0a 20 20 7b 2f 2a 20 45 6d 70 74 79 20 45 4c 53 45    {/* Empty ELSE
29a0b 20 63 6c 61 75 73 65 20 2a 2f 7d 0a 0a 20 20 2f   clause */}..  /
29a0c 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 45 78 70  * Code an OP_Exp
29a0d 69 72 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f  ire at the end o
29a0e 66 20 65 61 63 68 20 50 52 41 47 4d 41 20 70 72  f each PRAGMA pr
29a0f 6f 67 72 61 6d 20 74 6f 20 63 61 75 73 65 0a 20  ogram to cause. 
29a10 20 2a 2a 20 74 68 65 20 56 44 42 45 20 69 6d 70   ** the VDBE imp
29a11 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 70 72  lementing the pr
29a12 61 67 6d 61 20 74 6f 20 65 78 70 69 72 65 2e 20  agma to expire. 
29a13 4d 6f 73 74 20 28 61 6c 6c 3f 29 20 70 72 61 67  Most (all?) prag
29a14 6d 61 73 0a 20 20 2a 2a 20 61 72 65 20 6f 6e 6c  mas.  ** are onl
29a15 79 20 76 61 6c 69 64 20 66 6f 72 20 61 20 73 69  y valid for a si
29a16 6e 67 6c 65 20 65 78 65 63 75 74 69 6f 6e 2e 0a  ngle execution..
29a17 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
29a18 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 45  beAddOp2(v, OP_E
29a19 78 70 69 72 65 2c 20 31 2c 20 30 29 3b 0a 0a 20  xpire, 1, 0);.. 
29a1a 20 2f 2a 0a 20 20 2a 2a 20 52 65 73 65 74 20 74   /*.  ** Reset t
29a1b 68 65 20 73 61 66 65 74 79 20 6c 65 76 65 6c 2c  he safety level,
29a1c 20 69 6e 20 63 61 73 65 20 74 68 65 20 66 75 6c   in case the ful
29a1d 6c 66 73 79 6e 63 20 66 6c 61 67 20 6f 72 20 73  lfsync flag or s
29a1e 79 6e 63 68 72 6f 6e 6f 75 73 0a 20 20 2a 2a 20  ynchronous.  ** 
29a1f 73 65 74 74 69 6e 67 20 63 68 61 6e 67 65 64 2e  setting changed.
29a20 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
29a21 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
29a22 50 52 41 47 4d 41 53 0a 20 20 69 66 28 20 64 62  PRAGMAS.  if( db
29a23 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a  ->autoCommit ){.
29a24 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
29a25 53 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 70  SetSafetyLevel(p
29a26 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e 73 61  Db->pBt, pDb->sa
29a27 66 65 74 79 5f 6c 65 76 65 6c 2c 0a 20 20 20 20  fety_level,.    
29a28 20 20 20 20 20 20 20 20 20 20 20 28 64 62 2d 3e             (db->
29a29 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 75 6c  flags&SQLITE_Ful
29a2a 6c 46 53 79 6e 63 29 21 3d 30 29 3b 0a 20 20 7d  lFSync)!=0);.  }
29a2b 0a 23 65 6e 64 69 66 0a 70 72 61 67 6d 61 5f 6f  .#endif.pragma_o
29a2c 75 74 3a 0a 20 20 73 71 6c 69 74 65 33 44 62 46  ut:.  sqlite3DbF
29a2d 72 65 65 28 64 62 2c 20 7a 4c 65 66 74 29 3b 0a  ree(db, zLeft);.
29a2e 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
29a2f 64 62 2c 20 7a 52 69 67 68 74 29 3b 0a 7d 0a 0a  db, zRight);.}..
29a30 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
29a31 5f 4f 4d 49 54 5f 50 52 41 47 4d 41 20 2a 2f 0a  _OMIT_PRAGMA */.
29a32 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
29a33 20 45 6e 64 20 6f 66 20 70 72 61 67 6d 61 2e 63   End of pragma.c
29a34 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
29a35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29a36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
29a37 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
29a38 20 42 65 67 69 6e 20 66 69 6c 65 20 70 72 65 70   Begin file prep
29a39 61 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  are.c **********
29a3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29a3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
29a3c 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4d 61 79 20  ./*.** 2005 May 
29a3d 32 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  25.**.** The aut
29a3e 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
29a3f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
29a40 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
29a41 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
29a42 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
29a43 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
29a44 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
29a45 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
29a46 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
29a47 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
29a48 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
29a49 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
29a4a 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
29a4b 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
29a4c 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
29a4d 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
29a4e 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
29a4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29a51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29a52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29a53 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
29a54 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d 70 6c  ontains the impl
29a55 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
29a56 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  e sqlite3_prepar
29a57 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  e().** interface
29a58 2c 20 61 6e 64 20 72 6f 75 74 69 6e 65 73 20 74  , and routines t
29a59 68 61 74 20 63 6f 6e 74 72 69 62 75 74 65 20 74  hat contribute t
29a5a 6f 20 6c 6f 61 64 69 6e 67 20 74 68 65 20 64 61  o loading the da
29a5b 74 61 62 61 73 65 20 73 63 68 65 6d 61 0a 2a 2a  tabase schema.**
29a5c 20 66 72 6f 6d 20 64 69 73 6b 2e 0a 2a 2a 0a 2a   from disk..**.*
29a5d 2a 20 24 49 64 3a 20 70 72 65 70 61 72 65 2e 63  * $Id: prepare.c
29a5e 2c 76 20 31 2e 31 33 31 20 32 30 30 39 2f 30 38  ,v 1.131 2009/08
29a5f 2f 30 36 20 31 37 3a 34 33 3a 33 31 20 64 72 68  /06 17:43:31 drh
29a60 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
29a61 20 46 69 6c 6c 20 74 68 65 20 49 6e 69 74 44 61   Fill the InitDa
29a62 74 61 20 73 74 72 75 63 74 75 72 65 20 77 69 74  ta structure wit
29a63 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  h an error messa
29a64 67 65 20 74 68 61 74 20 69 6e 64 69 63 61 74 65  ge that indicate
29a65 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 64 61  s.** that the da
29a66 74 61 62 61 73 65 20 69 73 20 63 6f 72 72 75 70  tabase is corrup
29a67 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
29a68 64 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61 28  d corruptSchema(
29a69 0a 20 20 49 6e 69 74 44 61 74 61 20 2a 70 44 61  .  InitData *pDa
29a6a 74 61 2c 20 20 20 20 20 2f 2a 20 49 6e 69 74 69  ta,     /* Initi
29a6b 61 6c 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78  alization contex
29a6c 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  t */.  const cha
29a6d 72 20 2a 7a 4f 62 6a 2c 20 20 20 20 2f 2a 20 4f  r *zObj,    /* O
29a6e 62 6a 65 63 74 20 62 65 69 6e 67 20 70 61 72 73  bject being pars
29a6f 65 64 20 61 74 20 74 68 65 20 70 6f 69 6e 74 20  ed at the point 
29a70 6f 66 20 65 72 72 6f 72 20 2a 2f 0a 20 20 63 6f  of error */.  co
29a71 6e 73 74 20 63 68 61 72 20 2a 7a 45 78 74 72 61  nst char *zExtra
29a72 20 20 20 2f 2a 20 45 72 72 6f 72 20 69 6e 66 6f     /* Error info
29a73 72 6d 61 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20  rmation */.){.  
29a74 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 44  sqlite3 *db = pD
29a75 61 74 61 2d 3e 64 62 3b 0a 20 20 69 66 28 20 21  ata->db;.  if( !
29a76 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
29a77 20 26 26 20 28 64 62 2d 3e 66 6c 61 67 73 20 26   && (db->flags &
29a78 20 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79   SQLITE_Recovery
29a79 4d 6f 64 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Mode)==0 ){.    
29a7a 69 66 28 20 7a 4f 62 6a 3d 3d 30 20 29 20 7a 4f  if( zObj==0 ) zO
29a7b 62 6a 20 3d 20 22 3f 22 3b 0a 20 20 20 20 73 71  bj = "?";.    sq
29a7c 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 70  lite3SetString(p
29a7d 44 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67 2c 20  Data->pzErrMsg, 
29a7e 64 62 2c 0a 20 20 20 20 20 20 22 6d 61 6c 66 6f  db,.      "malfo
29a7f 72 6d 65 64 20 64 61 74 61 62 61 73 65 20 73 63  rmed database sc
29a80 68 65 6d 61 20 28 25 73 29 22 2c 20 7a 4f 62 6a  hema (%s)", zObj
29a81 29 3b 0a 20 20 20 20 69 66 28 20 7a 45 78 74 72  );.    if( zExtr
29a82 61 20 29 7b 0a 20 20 20 20 20 20 2a 70 44 61 74  a ){.      *pDat
29a83 61 2d 3e 70 7a 45 72 72 4d 73 67 20 3d 20 73 71  a->pzErrMsg = sq
29a84 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62  lite3MAppendf(db
29a85 2c 20 2a 70 44 61 74 61 2d 3e 70 7a 45 72 72 4d  , *pData->pzErrM
29a86 73 67 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  sg, .           
29a87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29a88 20 20 20 20 20 20 22 25 73 20 2d 20 25 73 22 2c        "%s - %s",
29a89 20 2a 70 44 61 74 61 2d 3e 70 7a 45 72 72 4d 73   *pData->pzErrMs
29a8a 67 2c 20 7a 45 78 74 72 61 29 3b 0a 20 20 20 20  g, zExtra);.    
29a8b 7d 0a 20 20 7d 0a 20 20 70 44 61 74 61 2d 3e 72  }.  }.  pData->r
29a8c 63 20 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  c = db->mallocFa
29a8d 69 6c 65 64 20 3f 20 53 51 4c 49 54 45 5f 4e 4f  iled ? SQLITE_NO
29a8e 4d 45 4d 20 3a 20 53 51 4c 49 54 45 5f 43 4f 52  MEM : SQLITE_COR
29a8f 52 55 50 54 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  RUPT;.}../*.** T
29a90 68 69 73 20 69 73 20 74 68 65 20 63 61 6c 6c 62  his is the callb
29a91 61 63 6b 20 72 6f 75 74 69 6e 65 20 66 6f 72 20  ack routine for 
29a92 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 69 6e  the code that in
29a93 69 74 69 61 6c 69 7a 65 73 20 74 68 65 0a 2a 2a  itializes the.**
29a94 20 64 61 74 61 62 61 73 65 2e 20 20 53 65 65 20   database.  See 
29a95 73 71 6c 69 74 65 33 49 6e 69 74 28 29 20 62 65  sqlite3Init() be
29a96 6c 6f 77 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  low for addition
29a97 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
29a98 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
29a99 69 73 20 61 6c 73 6f 20 63 61 6c 6c 65 64 20 66  is also called f
29a9a 72 6f 6d 20 74 68 65 20 4f 50 5f 50 61 72 73 65  rom the OP_Parse
29a9b 53 63 68 65 6d 61 20 6f 70 63 6f 64 65 20 6f 66  Schema opcode of
29a9c 20 74 68 65 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a   the VDBE..**.**
29a9d 20 45 61 63 68 20 63 61 6c 6c 62 61 63 6b 20 63   Each callback c
29a9e 6f 6e 74 61 69 6e 73 20 74 68 65 20 66 6f 6c 6c  ontains the foll
29a9f 6f 77 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f  owing informatio
29aa0 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 72 67  n:.**.**     arg
29aa1 76 5b 30 5d 20 3d 20 6e 61 6d 65 20 6f 66 20 74  v[0] = name of t
29aa2 68 69 6e 67 20 62 65 69 6e 67 20 63 72 65 61 74  hing being creat
29aa3 65 64 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b 31  ed.**     argv[1
29aa4 5d 20 3d 20 72 6f 6f 74 20 70 61 67 65 20 6e 75  ] = root page nu
29aa5 6d 62 65 72 20 66 6f 72 20 74 61 62 6c 65 20 6f  mber for table o
29aa6 72 20 69 6e 64 65 78 2e 20 30 20 66 6f 72 20 74  r index. 0 for t
29aa7 72 69 67 67 65 72 20 6f 72 20 76 69 65 77 2e 0a  rigger or view..
29aa8 2a 2a 20 20 20 20 20 61 72 67 76 5b 32 5d 20 3d  **     argv[2] =
29aa9 20 53 51 4c 20 74 65 78 74 20 66 6f 72 20 74 68   SQL text for th
29aaa 65 20 43 52 45 41 54 45 20 73 74 61 74 65 6d 65  e CREATE stateme
29aab 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45  nt..**.*/.SQLITE
29aac 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
29aad 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b  ite3InitCallback
29aae 28 76 6f 69 64 20 2a 70 49 6e 69 74 2c 20 69 6e  (void *pInit, in
29aaf 74 20 61 72 67 63 2c 20 63 68 61 72 20 2a 2a 61  t argc, char **a
29ab0 72 67 76 2c 20 63 68 61 72 20 2a 2a 4e 6f 74 55  rgv, char **NotU
29ab1 73 65 64 29 7b 0a 20 20 49 6e 69 74 44 61 74 61  sed){.  InitData
29ab2 20 2a 70 44 61 74 61 20 3d 20 28 49 6e 69 74 44   *pData = (InitD
29ab3 61 74 61 2a 29 70 49 6e 69 74 3b 0a 20 20 73 71  ata*)pInit;.  sq
29ab4 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 44 61 74  lite3 *db = pDat
29ab5 61 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62  a->db;.  int iDb
29ab6 20 3d 20 70 44 61 74 61 2d 3e 69 44 62 3b 0a 0a   = pData->iDb;..
29ab7 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d    assert( argc==
29ab8 33 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  3 );.  UNUSED_PA
29ab9 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64  RAMETER2(NotUsed
29aba 2c 20 61 72 67 63 29 3b 0a 20 20 61 73 73 65 72  , argc);.  asser
29abb 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
29abc 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
29abd 20 29 3b 0a 20 20 44 62 43 6c 65 61 72 50 72 6f   );.  DbClearPro
29abe 70 65 72 74 79 28 64 62 2c 20 69 44 62 2c 20 44  perty(db, iDb, D
29abf 42 5f 45 6d 70 74 79 29 3b 0a 20 20 69 66 28 20  B_Empty);.  if( 
29ac0 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
29ac1 20 29 7b 0a 20 20 20 20 63 6f 72 72 75 70 74 53   ){.    corruptS
29ac2 63 68 65 6d 61 28 70 44 61 74 61 2c 20 61 72 67  chema(pData, arg
29ac3 76 5b 30 5d 2c 20 30 29 3b 0a 20 20 20 20 72 65  v[0], 0);.    re
29ac4 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 61  turn 1;.  }..  a
29ac5 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26  ssert( iDb>=0 &&
29ac6 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a   iDb<db->nDb );.
29ac7 20 20 69 66 28 20 61 72 67 76 3d 3d 30 20 29 20    if( argv==0 ) 
29ac8 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 4d  return 0;   /* M
29ac9 69 67 68 74 20 68 61 70 70 65 6e 20 69 66 20 45  ight happen if E
29aca 4d 50 54 59 5f 52 45 53 55 4c 54 5f 43 41 4c 4c  MPTY_RESULT_CALL
29acb 42 41 43 4b 53 20 61 72 65 20 6f 6e 20 2a 2f 0a  BACKS are on */.
29acc 20 20 69 66 28 20 61 72 67 76 5b 31 5d 3d 3d 30    if( argv[1]==0
29acd 20 29 7b 0a 20 20 20 20 63 6f 72 72 75 70 74 53   ){.    corruptS
29ace 63 68 65 6d 61 28 70 44 61 74 61 2c 20 61 72 67  chema(pData, arg
29acf 76 5b 30 5d 2c 20 30 29 3b 0a 20 20 7d 65 6c 73  v[0], 0);.  }els
29ad0 65 20 69 66 28 20 61 72 67 76 5b 32 5d 20 26 26  e if( argv[2] &&
29ad1 20 61 72 67 76 5b 32 5d 5b 30 5d 20 29 7b 0a 20   argv[2][0] ){. 
29ad2 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20 70     /* Call the p
29ad3 61 72 73 65 72 20 74 6f 20 70 72 6f 63 65 73 73  arser to process
29ad4 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 2c   a CREATE TABLE,
29ad5 20 49 4e 44 45 58 20 6f 72 20 56 49 45 57 2e 0a   INDEX or VIEW..
29ad6 20 20 20 20 2a 2a 20 42 75 74 20 62 65 63 61 75      ** But becau
29ad7 73 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  se db->init.busy
29ad8 20 69 73 20 73 65 74 20 74 6f 20 31 2c 20 6e 6f   is set to 1, no
29ad9 20 56 44 42 45 20 63 6f 64 65 20 69 73 20 67 65   VDBE code is ge
29ada 6e 65 72 61 74 65 64 0a 20 20 20 20 2a 2a 20 6f  nerated.    ** o
29adb 72 20 65 78 65 63 75 74 65 64 2e 20 20 41 6c 6c  r executed.  All
29adc 20 74 68 65 20 70 61 72 73 65 72 20 64 6f 65 73   the parser does
29add 20 69 73 20 62 75 69 6c 64 20 74 68 65 20 69 6e   is build the in
29ade 74 65 72 6e 61 6c 20 64 61 74 61 0a 20 20 20 20  ternal data.    
29adf 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 74 68  ** structures th
29ae0 61 74 20 64 65 73 63 72 69 62 65 20 74 68 65 20  at describe the 
29ae1 74 61 62 6c 65 2c 20 69 6e 64 65 78 2c 20 6f 72  table, index, or
29ae2 20 76 69 65 77 2e 0a 20 20 20 20 2a 2f 0a 20 20   view..    */.  
29ae3 20 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20    char *zErr;.  
29ae4 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 61 73    int rc;.    as
29ae5 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62  sert( db->init.b
29ae6 75 73 79 20 29 3b 0a 20 20 20 20 64 62 2d 3e 69  usy );.    db->i
29ae7 6e 69 74 2e 69 44 62 20 3d 20 69 44 62 3b 0a 20  nit.iDb = iDb;. 
29ae8 20 20 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54     db->init.newT
29ae9 6e 75 6d 20 3d 20 61 74 6f 69 28 61 72 67 76 5b  num = atoi(argv[
29aea 31 5d 29 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69  1]);.    db->ini
29aeb 74 2e 6f 72 70 68 61 6e 54 72 69 67 67 65 72 20  t.orphanTrigger 
29aec 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  = 0;.    rc = sq
29aed 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 61  lite3_exec(db, a
29aee 72 67 76 5b 32 5d 2c 20 30 2c 20 30 2c 20 26 7a  rgv[2], 0, 0, &z
29aef 45 72 72 29 3b 0a 20 20 20 20 64 62 2d 3e 69 6e  Err);.    db->in
29af0 69 74 2e 69 44 62 20 3d 20 30 3b 0a 20 20 20 20  it.iDb = 0;.    
29af1 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
29af2 54 45 5f 4f 4b 20 7c 7c 20 7a 45 72 72 3d 3d 30  TE_OK || zErr==0
29af3 20 29 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49   );.    if( SQLI
29af4 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20  TE_OK!=rc ){.   
29af5 20 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e     if( db->init.
29af6 6f 72 70 68 61 6e 54 72 69 67 67 65 72 20 29 7b  orphanTrigger ){
29af7 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
29af8 20 69 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20 20   iDb==1 );.     
29af9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
29afa 70 44 61 74 61 2d 3e 72 63 20 3d 20 72 63 3b 0a  pData->rc = rc;.
29afb 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
29afc 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a  SQLITE_NOMEM ){.
29afd 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61            db->ma
29afe 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
29aff 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
29b00 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4e 54  ( rc!=SQLITE_INT
29b01 45 52 52 55 50 54 20 26 26 20 72 63 21 3d 53 51  ERRUPT && rc!=SQ
29b02 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29 7b 0a 20  LITE_LOCKED ){. 
29b03 20 20 20 20 20 20 20 20 20 63 6f 72 72 75 70 74           corrupt
29b04 53 63 68 65 6d 61 28 70 44 61 74 61 2c 20 61 72  Schema(pData, ar
29b05 67 76 5b 30 5d 2c 20 7a 45 72 72 29 3b 0a 20 20  gv[0], zErr);.  
29b06 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
29b07 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
29b08 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20  ree(db, zErr);. 
29b09 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
29b0a 20 61 72 67 76 5b 30 5d 3d 3d 30 20 29 7b 0a 20   argv[0]==0 ){. 
29b0b 20 20 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61     corruptSchema
29b0c 28 70 44 61 74 61 2c 20 30 2c 20 30 29 3b 0a 20  (pData, 0, 0);. 
29b0d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49   }else{.    /* I
29b0e 66 20 74 68 65 20 53 51 4c 20 63 6f 6c 75 6d 6e  f the SQL column
29b0f 20 69 73 20 62 6c 61 6e 6b 20 69 74 20 6d 65 61   is blank it mea
29b10 6e 73 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e  ns this is an in
29b11 64 65 78 20 74 68 61 74 0a 20 20 20 20 2a 2a 20  dex that.    ** 
29b12 77 61 73 20 63 72 65 61 74 65 64 20 74 6f 20 62  was created to b
29b13 65 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45  e the PRIMARY KE
29b14 59 20 6f 72 20 74 6f 20 66 75 6c 66 69 6c 6c 20  Y or to fulfill 
29b15 61 20 55 4e 49 51 55 45 0a 20 20 20 20 2a 2a 20  a UNIQUE.    ** 
29b16 63 6f 6e 73 74 72 61 69 6e 74 20 66 6f 72 20 61  constraint for a
29b17 20 43 52 45 41 54 45 20 54 41 42 4c 45 2e 20 20   CREATE TABLE.  
29b18 54 68 65 20 69 6e 64 65 78 20 73 68 6f 75 6c 64  The index should
29b19 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 20 20   have already.  
29b1a 20 20 2a 2a 20 62 65 65 6e 20 63 72 65 61 74 65    ** been create
29b1b 64 20 77 68 65 6e 20 77 65 20 70 72 6f 63 65 73  d when we proces
29b1c 73 65 64 20 74 68 65 20 43 52 45 41 54 45 20 54  sed the CREATE T
29b1d 41 42 4c 45 2e 20 20 41 6c 6c 20 77 65 20 68 61  ABLE.  All we ha
29b1e 76 65 0a 20 20 20 20 2a 2a 20 74 6f 20 64 6f 20  ve.    ** to do 
29b1f 68 65 72 65 20 69 73 20 72 65 63 6f 72 64 20 74  here is record t
29b20 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d  he root page num
29b21 62 65 72 20 66 6f 72 20 74 68 61 74 20 69 6e 64  ber for that ind
29b22 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49  ex..    */.    I
29b23 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20  ndex *pIndex;.  
29b24 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74    pIndex = sqlit
29b25 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20  e3FindIndex(db, 
29b26 61 72 67 76 5b 30 5d 2c 20 64 62 2d 3e 61 44 62  argv[0], db->aDb
29b27 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20  [iDb].zName);.  
29b28 20 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20    if( pIndex==0 
29b29 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73  ){.      /* This
29b2a 20 63 61 6e 20 6f 63 63 75 72 20 69 66 20 74 68   can occur if th
29b2b 65 72 65 20 65 78 69 73 74 73 20 61 6e 20 69 6e  ere exists an in
29b2c 64 65 78 20 6f 6e 20 61 20 54 45 4d 50 20 74 61  dex on a TEMP ta
29b2d 62 6c 65 20 77 68 69 63 68 0a 20 20 20 20 20 20  ble which.      
29b2e 2a 2a 20 68 61 73 20 74 68 65 20 73 61 6d 65 20  ** has the same 
29b2f 6e 61 6d 65 20 61 73 20 61 6e 6f 74 68 65 72 20  name as another 
29b30 69 6e 64 65 78 20 6f 6e 20 61 20 70 65 72 6d 61  index on a perma
29b31 6e 65 6e 74 20 69 6e 64 65 78 2e 20 20 53 69 6e  nent index.  Sin
29b32 63 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  ce.      ** the 
29b33 70 65 72 6d 61 6e 65 6e 74 20 74 61 62 6c 65 20  permanent table 
29b34 69 73 20 68 69 64 64 65 6e 20 62 79 20 74 68 65  is hidden by the
29b35 20 54 45 4d 50 20 74 61 62 6c 65 2c 20 77 65 20   TEMP table, we 
29b36 63 61 6e 20 61 6c 73 6f 0a 20 20 20 20 20 20 2a  can also.      *
29b37 2a 20 73 61 66 65 6c 79 20 69 67 6e 6f 72 65 20  * safely ignore 
29b38 74 68 65 20 69 6e 64 65 78 20 6f 6e 20 74 68 65  the index on the
29b39 20 70 65 72 6d 61 6e 65 6e 74 20 74 61 62 6c 65   permanent table
29b3a 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
29b3b 20 2f 2a 20 44 6f 20 4e 6f 74 68 69 6e 67 20 2a   /* Do Nothing *
29b3c 2f 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  /;.    }else if(
29b3d 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32   sqlite3GetInt32
29b3e 28 61 72 67 76 5b 31 5d 2c 20 26 70 49 6e 64 65  (argv[1], &pInde
29b3f 78 2d 3e 74 6e 75 6d 29 3d 3d 30 20 29 7b 0a 20  x->tnum)==0 ){. 
29b40 20 20 20 20 20 63 6f 72 72 75 70 74 53 63 68 65       corruptSche
29b41 6d 61 28 70 44 61 74 61 2c 20 61 72 67 76 5b 30  ma(pData, argv[0
29b42 5d 2c 20 22 69 6e 76 61 6c 69 64 20 72 6f 6f 74  ], "invalid root
29b43 70 61 67 65 22 29 3b 0a 20 20 20 20 7d 0a 20 20  page");.    }.  
29b44 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
29b45 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74  ./*.** Attempt t
29b46 6f 20 72 65 61 64 20 74 68 65 20 64 61 74 61 62  o read the datab
29b47 61 73 65 20 73 63 68 65 6d 61 20 61 6e 64 20 69  ase schema and i
29b48 6e 69 74 69 61 6c 69 7a 65 20 69 6e 74 65 72 6e  nitialize intern
29b49 61 6c 0a 2a 2a 20 64 61 74 61 20 73 74 72 75 63  al.** data struc
29b4a 74 75 72 65 73 20 66 6f 72 20 61 20 73 69 6e 67  tures for a sing
29b4b 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  le database file
29b4c 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20  .  The index of 
29b4d 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
29b4e 66 69 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79  file is given by
29b4f 20 69 44 62 2e 20 20 69 44 62 3d 3d 30 20 69 73   iDb.  iDb==0 is
29b50 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 6d 61   used for the ma
29b51 69 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  in.** database. 
29b52 20 69 44 62 3d 3d 31 20 73 68 6f 75 6c 64 20 6e   iDb==1 should n
29b53 65 76 65 72 20 62 65 20 75 73 65 64 2e 20 20 69  ever be used.  i
29b54 44 62 3e 3d 32 20 69 73 20 75 73 65 64 20 66 6f  Db>=2 is used fo
29b55 72 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64  r.** auxiliary d
29b56 61 74 61 62 61 73 65 73 2e 20 20 52 65 74 75 72  atabases.  Retur
29b57 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c  n one of the SQL
29b58 49 54 45 5f 20 65 72 72 6f 72 20 63 6f 64 65 73  ITE_ error codes
29b59 20 74 6f 0a 2a 2a 20 69 6e 64 69 63 61 74 65 20   to.** indicate 
29b5a 73 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c 75  success or failu
29b5b 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  re..*/.static in
29b5c 74 20 73 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65  t sqlite3InitOne
29b5d 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
29b5e 74 20 69 44 62 2c 20 63 68 61 72 20 2a 2a 70 7a  t iDb, char **pz
29b5f 45 72 72 4d 73 67 29 7b 0a 20 20 69 6e 74 20 72  ErrMsg){.  int r
29b60 63 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  c;.  int i;.  in
29b61 74 20 73 69 7a 65 3b 0a 20 20 54 61 62 6c 65 20  t size;.  Table 
29b62 2a 70 54 61 62 3b 0a 20 20 44 62 20 2a 70 44 62  *pTab;.  Db *pDb
29b63 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ;.  char const *
29b64 61 7a 41 72 67 5b 34 5d 3b 0a 20 20 69 6e 74 20  azArg[4];.  int 
29b65 6d 65 74 61 5b 35 5d 3b 0a 20 20 49 6e 69 74 44  meta[5];.  InitD
29b66 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 20 20  ata initData;.  
29b67 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 73  char const *zMas
29b68 74 65 72 53 63 68 65 6d 61 3b 0a 20 20 63 68 61  terSchema;.  cha
29b69 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 73 74 65 72  r const *zMaster
29b6a 4e 61 6d 65 20 3d 20 53 43 48 45 4d 41 5f 54 41  Name = SCHEMA_TA
29b6b 42 4c 45 28 69 44 62 29 3b 0a 20 20 69 6e 74 20  BLE(iDb);.  int 
29b6c 6f 70 65 6e 65 64 54 72 61 6e 73 61 63 74 69 6f  openedTransactio
29b6d 6e 20 3d 20 30 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  n = 0;..  /*.  *
29b6e 2a 20 54 68 65 20 6d 61 73 74 65 72 20 64 61 74  * The master dat
29b6f 61 62 61 73 65 20 74 61 62 6c 65 20 68 61 73 20  abase table has 
29b70 61 20 73 74 72 75 63 74 75 72 65 20 6c 69 6b 65  a structure like
29b71 20 74 68 69 73 0a 20 20 2a 2f 0a 20 20 73 74 61   this.  */.  sta
29b72 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 6d  tic const char m
29b73 61 73 74 65 72 5f 73 63 68 65 6d 61 5b 5d 20 3d  aster_schema[] =
29b74 20 0a 20 20 20 20 20 22 43 52 45 41 54 45 20 54   .     "CREATE T
29b75 41 42 4c 45 20 73 71 6c 69 74 65 5f 6d 61 73 74  ABLE sqlite_mast
29b76 65 72 28 5c 6e 22 0a 20 20 20 20 20 22 20 20 74  er(\n".     "  t
29b77 79 70 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20  ype text,\n".   
29b78 20 20 22 20 20 6e 61 6d 65 20 74 65 78 74 2c 5c    "  name text,\
29b79 6e 22 0a 20 20 20 20 20 22 20 20 74 62 6c 5f 6e  n".     "  tbl_n
29b7a 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20  ame text,\n".   
29b7b 20 20 22 20 20 72 6f 6f 74 70 61 67 65 20 69 6e    "  rootpage in
29b7c 74 65 67 65 72 2c 5c 6e 22 0a 20 20 20 20 20 22  teger,\n".     "
29b7d 20 20 73 71 6c 20 74 65 78 74 5c 6e 22 0a 20 20    sql text\n".  
29b7e 20 20 20 22 29 22 0a 20 20 3b 0a 23 69 66 6e 64     ")".  ;.#ifnd
29b7f 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
29b80 45 4d 50 44 42 0a 20 20 73 74 61 74 69 63 20 63  EMPDB.  static c
29b81 6f 6e 73 74 20 63 68 61 72 20 74 65 6d 70 5f 6d  onst char temp_m
29b82 61 73 74 65 72 5f 73 63 68 65 6d 61 5b 5d 20 3d  aster_schema[] =
29b83 20 0a 20 20 20 20 20 22 43 52 45 41 54 45 20 54   .     "CREATE T
29b84 45 4d 50 20 54 41 42 4c 45 20 73 71 6c 69 74 65  EMP TABLE sqlite
29b85 5f 74 65 6d 70 5f 6d 61 73 74 65 72 28 5c 6e 22  _temp_master(\n"
29b86 0a 20 20 20 20 20 22 20 20 74 79 70 65 20 74 65  .     "  type te
29b87 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 6e  xt,\n".     "  n
29b88 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20  ame text,\n".   
29b89 20 20 22 20 20 74 62 6c 5f 6e 61 6d 65 20 74 65    "  tbl_name te
29b8a 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 72  xt,\n".     "  r
29b8b 6f 6f 74 70 61 67 65 20 69 6e 74 65 67 65 72 2c  ootpage integer,
29b8c 5c 6e 22 0a 20 20 20 20 20 22 20 20 73 71 6c 20  \n".     "  sql 
29b8d 74 65 78 74 5c 6e 22 0a 20 20 20 20 20 22 29 22  text\n".     ")"
29b8e 0a 20 20 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65  .  ;.#else.  #de
29b8f 66 69 6e 65 20 74 65 6d 70 5f 6d 61 73 74 65 72  fine temp_master
29b90 5f 73 63 68 65 6d 61 20 30 0a 23 65 6e 64 69 66  _schema 0.#endif
29b91 0a 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  ..  assert( iDb>
29b92 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44  =0 && iDb<db->nD
29b93 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64  b );.  assert( d
29b94 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
29b95 65 6d 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ema );.  assert(
29b96 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
29b97 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
29b98 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3d  ;.  assert( iDb=
29b99 3d 31 20 7c 7c 20 73 71 6c 69 74 65 33 42 74 72  =1 || sqlite3Btr
29b9a 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d  eeHoldsMutex(db-
29b9b 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 29 20 29  >aDb[iDb].pBt) )
29b9c 3b 0a 0a 20 20 2f 2a 20 7a 4d 61 73 74 65 72 53  ;..  /* zMasterS
29b9d 63 68 65 6d 61 20 61 6e 64 20 7a 49 6e 69 74 53  chema and zInitS
29b9e 63 72 69 70 74 20 61 72 65 20 73 65 74 20 74 6f  cript are set to
29b9f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 6d 61   point at the ma
29ba0 73 74 65 72 20 73 63 68 65 6d 61 0a 20 20 2a 2a  ster schema.  **
29ba1 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 73 61 74   and initialisat
29ba2 69 6f 6e 20 73 63 72 69 70 74 20 61 70 70 72 6f  ion script appro
29ba3 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 20 64  priate for the d
29ba4 61 74 61 62 61 73 65 20 62 65 69 6e 67 0a 20 20  atabase being.  
29ba5 2a 2a 20 69 6e 69 74 69 61 6c 69 73 65 64 2e 20  ** initialised. 
29ba6 7a 4d 61 73 74 65 72 4e 61 6d 65 20 69 73 20 74  zMasterName is t
29ba7 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6d  he name of the m
29ba8 61 73 74 65 72 20 74 61 62 6c 65 2e 0a 20 20 2a  aster table..  *
29ba9 2f 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45  /.  if( !OMIT_TE
29baa 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29  MPDB && iDb==1 )
29bab 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72 53 63 68  {.    zMasterSch
29bac 65 6d 61 20 3d 20 74 65 6d 70 5f 6d 61 73 74 65  ema = temp_maste
29bad 72 5f 73 63 68 65 6d 61 3b 0a 20 20 7d 65 6c 73  r_schema;.  }els
29bae 65 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72 53 63  e{.    zMasterSc
29baf 68 65 6d 61 20 3d 20 6d 61 73 74 65 72 5f 73 63  hema = master_sc
29bb0 68 65 6d 61 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73  hema;.  }.  zMas
29bb1 74 65 72 4e 61 6d 65 20 3d 20 53 43 48 45 4d 41  terName = SCHEMA
29bb2 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 0a 20 20  _TABLE(iDb);..  
29bb3 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 74 68 65  /* Construct the
29bb4 20 73 63 68 65 6d 61 20 74 61 62 6c 65 73 2e 20   schema tables. 
29bb5 20 2a 2f 0a 20 20 61 7a 41 72 67 5b 30 5d 20 3d   */.  azArg[0] =
29bb6 20 7a 4d 61 73 74 65 72 4e 61 6d 65 3b 0a 20 20   zMasterName;.  
29bb7 61 7a 41 72 67 5b 31 5d 20 3d 20 22 31 22 3b 0a  azArg[1] = "1";.
29bb8 20 20 61 7a 41 72 67 5b 32 5d 20 3d 20 7a 4d 61    azArg[2] = zMa
29bb9 73 74 65 72 53 63 68 65 6d 61 3b 0a 20 20 61 7a  sterSchema;.  az
29bba 41 72 67 5b 33 5d 20 3d 20 30 3b 0a 20 20 69 6e  Arg[3] = 0;.  in
29bbb 69 74 44 61 74 61 2e 64 62 20 3d 20 64 62 3b 0a  itData.db = db;.
29bbc 20 20 69 6e 69 74 44 61 74 61 2e 69 44 62 20 3d    initData.iDb =
29bbd 20 69 44 62 3b 0a 20 20 69 6e 69 74 44 61 74 61   iDb;.  initData
29bbe 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  .rc = SQLITE_OK;
29bbf 0a 20 20 69 6e 69 74 44 61 74 61 2e 70 7a 45 72  .  initData.pzEr
29bc0 72 4d 73 67 20 3d 20 70 7a 45 72 72 4d 73 67 3b  rMsg = pzErrMsg;
29bc1 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33  .  (void)sqlite3
29bc2 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20  SafetyOff(db);. 
29bc3 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c   sqlite3InitCall
29bc4 62 61 63 6b 28 26 69 6e 69 74 44 61 74 61 2c 20  back(&initData, 
29bc5 33 2c 20 28 63 68 61 72 20 2a 2a 29 61 7a 41 72  3, (char **)azAr
29bc6 67 2c 20 30 29 3b 0a 20 20 28 76 6f 69 64 29 73  g, 0);.  (void)s
29bc7 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
29bc8 62 29 3b 0a 20 20 69 66 28 20 69 6e 69 74 44 61  b);.  if( initDa
29bc9 74 61 2e 72 63 20 29 7b 0a 20 20 20 20 72 63 20  ta.rc ){.    rc 
29bca 3d 20 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20  = initData.rc;. 
29bcb 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f 75     goto error_ou
29bcc 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d 20  t;.  }.  pTab = 
29bcd 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
29bce 28 64 62 2c 20 7a 4d 61 73 74 65 72 4e 61 6d 65  (db, zMasterName
29bcf 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  , db->aDb[iDb].z
29bd0 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 41 4c 57  Name);.  if( ALW
29bd1 41 59 53 28 70 54 61 62 29 20 29 7b 0a 20 20 20  AYS(pTab) ){.   
29bd2 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20   pTab->tabFlags 
29bd3 7c 3d 20 54 46 5f 52 65 61 64 6f 6e 6c 79 3b 0a  |= TF_Readonly;.
29bd4 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65    }..  /* Create
29bd5 20 61 20 63 75 72 73 6f 72 20 74 6f 20 68 6f 6c   a cursor to hol
29bd6 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f  d the database o
29bd7 70 65 6e 0a 20 20 2a 2f 0a 20 20 70 44 62 20 3d  pen.  */.  pDb =
29bd8 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a   &db->aDb[iDb];.
29bd9 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d    if( pDb->pBt==
29bda 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 4f 4d  0 ){.    if( !OM
29bdb 49 54 5f 54 45 4d 50 44 42 20 26 26 20 41 4c 57  IT_TEMPDB && ALW
29bdc 41 59 53 28 69 44 62 3d 3d 31 29 20 29 7b 0a 20  AYS(iDb==1) ){. 
29bdd 20 20 20 20 20 44 62 53 65 74 50 72 6f 70 65 72       DbSetProper
29bde 74 79 28 64 62 2c 20 31 2c 20 44 42 5f 53 63 68  ty(db, 1, DB_Sch
29bdf 65 6d 61 4c 6f 61 64 65 64 29 3b 0a 20 20 20 20  emaLoaded);.    
29be0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  }.    return SQL
29be1 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f  ITE_OK;.  }..  /
29be2 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  * If there is no
29be3 74 20 61 6c 72 65 61 64 79 20 61 20 72 65 61 64  t already a read
29be4 2d 6f 6e 6c 79 20 28 6f 72 20 72 65 61 64 2d 77  -only (or read-w
29be5 72 69 74 65 29 20 74 72 61 6e 73 61 63 74 69 6f  rite) transactio
29be6 6e 20 6f 70 65 6e 65 64 0a 20 20 2a 2a 20 6f 6e  n opened.  ** on
29be7 20 74 68 65 20 62 2d 74 72 65 65 20 64 61 74 61   the b-tree data
29be8 62 61 73 65 2c 20 6f 70 65 6e 20 6f 6e 65 20 6e  base, open one n
29be9 6f 77 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63  ow. If a transac
29bea 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 2c 20  tion is opened, 
29beb 69 74 20 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65  it .  ** will be
29bec 20 63 6c 6f 73 65 64 20 62 65 66 6f 72 65 20 74   closed before t
29bed 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
29bee 75 72 6e 73 2e 20 20 2a 2f 0a 20 20 73 71 6c 69  urns.  */.  sqli
29bef 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 44  te3BtreeEnter(pD
29bf0 62 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 20 21  b->pBt);.  if( !
29bf1 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e  sqlite3BtreeIsIn
29bf2 52 65 61 64 54 72 61 6e 73 28 70 44 62 2d 3e 70  ReadTrans(pDb->p
29bf3 42 74 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  Bt) ){.    rc = 
29bf4 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69  sqlite3BtreeBegi
29bf5 6e 54 72 61 6e 73 28 70 44 62 2d 3e 70 42 74 2c  nTrans(pDb->pBt,
29bf6 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21   0);.    if( rc!
29bf7 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
29bf8 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
29bf9 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 64  ring(pzErrMsg, d
29bfa 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33  b, "%s", sqlite3
29bfb 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20 20  ErrStr(rc));.   
29bfc 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f     goto initone_
29bfd 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 20 20 7d  error_out;.    }
29bfe 0a 20 20 20 20 6f 70 65 6e 65 64 54 72 61 6e 73  .    openedTrans
29bff 61 63 74 69 6f 6e 20 3d 20 31 3b 0a 20 20 7d 0a  action = 1;.  }.
29c00 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20 64 61  .  /* Get the da
29c01 74 61 62 61 73 65 20 6d 65 74 61 20 69 6e 66 6f  tabase meta info
29c02 72 6d 61 74 69 6f 6e 2e 0a 20 20 2a 2a 0a 20 20  rmation..  **.  
29c03 2a 2a 20 4d 65 74 61 20 76 61 6c 75 65 73 20 61  ** Meta values a
29c04 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20  re as follows:. 
29c05 20 2a 2a 20 20 20 20 6d 65 74 61 5b 30 5d 20 20   **    meta[0]  
29c06 20 53 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 20   Schema cookie. 
29c07 20 43 68 61 6e 67 65 73 20 77 69 74 68 20 65 61   Changes with ea
29c08 63 68 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65  ch schema change
29c09 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 31  ..  **    meta[1
29c0a 5d 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20  ]   File format 
29c0b 6f 66 20 73 63 68 65 6d 61 20 6c 61 79 65 72 2e  of schema layer.
29c0c 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 32 5d  .  **    meta[2]
29c0d 20 20 20 53 69 7a 65 20 6f 66 20 74 68 65 20 70     Size of the p
29c0e 61 67 65 20 63 61 63 68 65 2e 0a 20 20 2a 2a 20  age cache..  ** 
29c0f 20 20 20 6d 65 74 61 5b 33 5d 20 20 20 4c 61 72     meta[3]   Lar
29c10 67 65 73 74 20 72 6f 6f 74 70 61 67 65 20 28 61  gest rootpage (a
29c11 75 74 6f 2f 69 6e 63 72 5f 76 61 63 75 75 6d 20  uto/incr_vacuum 
29c12 6d 6f 64 65 29 0a 20 20 2a 2a 20 20 20 20 6d 65  mode).  **    me
29c13 74 61 5b 34 5d 20 20 20 44 62 20 74 65 78 74 20  ta[4]   Db text 
29c14 65 6e 63 6f 64 69 6e 67 2e 20 31 3a 55 54 46 2d  encoding. 1:UTF-
29c15 38 20 32 3a 55 54 46 2d 31 36 4c 45 20 33 3a 55  8 2:UTF-16LE 3:U
29c16 54 46 2d 31 36 42 45 0a 20 20 2a 2a 20 20 20 20  TF-16BE.  **    
29c17 6d 65 74 61 5b 35 5d 20 20 20 55 73 65 72 20 76  meta[5]   User v
29c18 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 20 20 20 6d  ersion.  **    m
29c19 65 74 61 5b 36 5d 20 20 20 49 6e 63 72 65 6d 65  eta[6]   Increme
29c1a 6e 74 61 6c 20 76 61 63 75 75 6d 20 6d 6f 64 65  ntal vacuum mode
29c1b 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 37 5d  .  **    meta[7]
29c1c 20 20 20 75 6e 75 73 65 64 0a 20 20 2a 2a 20 20     unused.  **  
29c1d 20 20 6d 65 74 61 5b 38 5d 20 20 20 75 6e 75 73    meta[8]   unus
29c1e 65 64 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b  ed.  **    meta[
29c1f 39 5d 20 20 20 75 6e 75 73 65 64 0a 20 20 2a 2a  9]   unused.  **
29c20 0a 20 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 65 20  .  ** Note: The 
29c21 23 64 65 66 69 6e 65 64 20 53 51 4c 49 54 45 5f  #defined SQLITE_
29c22 55 54 46 2a 20 73 79 6d 62 6f 6c 73 20 69 6e 20  UTF* symbols in 
29c23 73 71 6c 69 74 65 49 6e 74 2e 68 20 63 6f 72 72  sqliteInt.h corr
29c24 65 73 70 6f 6e 64 20 74 6f 0a 20 20 2a 2a 20 74  espond to.  ** t
29c25 68 65 20 70 6f 73 73 69 62 6c 65 20 76 61 6c 75  he possible valu
29c26 65 73 20 6f 66 20 6d 65 74 61 5b 34 5d 2e 0a 20  es of meta[4].. 
29c27 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   */.  for(i=0; i
29c28 3c 41 72 72 61 79 53 69 7a 65 28 6d 65 74 61 29  <ArraySize(meta)
29c29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  ; i++){.    sqli
29c2a 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28  te3BtreeGetMeta(
29c2b 70 44 62 2d 3e 70 42 74 2c 20 69 2b 31 2c 20 28  pDb->pBt, i+1, (
29c2c 75 33 32 20 2a 29 26 6d 65 74 61 5b 69 5d 29 3b  u32 *)&meta[i]);
29c2d 0a 20 20 7d 0a 20 20 70 44 62 2d 3e 70 53 63 68  .  }.  pDb->pSch
29c2e 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b  ema->schema_cook
29c2f 69 65 20 3d 20 6d 65 74 61 5b 42 54 52 45 45 5f  ie = meta[BTREE_
29c30 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2d 31  SCHEMA_VERSION-1
29c31 5d 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f 70 65 6e  ];..  /* If open
29c32 69 6e 67 20 61 20 6e 6f 6e 2d 65 6d 70 74 79 20  ing a non-empty 
29c33 64 61 74 61 62 61 73 65 2c 20 63 68 65 63 6b 20  database, check 
29c34 74 68 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  the text encodin
29c35 67 2e 20 46 6f 72 20 74 68 65 0a 20 20 2a 2a 20  g. For the.  ** 
29c36 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20 73  main database, s
29c37 65 74 20 73 71 6c 69 74 65 33 2e 65 6e 63 20 74  et sqlite3.enc t
29c38 6f 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f  o the encoding o
29c39 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  f the main datab
29c3a 61 73 65 2e 0a 20 20 2a 2a 20 46 6f 72 20 61 6e  ase..  ** For an
29c3b 20 61 74 74 61 63 68 65 64 20 64 62 2c 20 69 74   attached db, it
29c3c 20 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20   is an error if 
29c3d 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20  the encoding is 
29c3e 6e 6f 74 20 74 68 65 20 73 61 6d 65 0a 20 20 2a  not the same.  *
29c3f 2a 20 61 73 20 73 71 6c 69 74 65 33 2e 65 6e 63  * as sqlite3.enc
29c40 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65 74  ..  */.  if( met
29c41 61 5b 42 54 52 45 45 5f 54 45 58 54 5f 45 4e 43  a[BTREE_TEXT_ENC
29c42 4f 44 49 4e 47 2d 31 5d 20 29 7b 20 20 2f 2a 20  ODING-1] ){  /* 
29c43 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f  text encoding */
29c44 0a 20 20 20 20 69 66 28 20 69 44 62 3d 3d 30 20  .    if( iDb==0 
29c45 29 7b 0a 20 20 20 20 20 20 75 38 20 65 6e 63 6f  ){.      u8 enco
29c46 64 69 6e 67 3b 0a 20 20 20 20 20 20 2f 2a 20 49  ding;.      /* I
29c47 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 6d 61  f opening the ma
29c48 69 6e 20 64 61 74 61 62 61 73 65 2c 20 73 65 74  in database, set
29c49 20 45 4e 43 28 64 62 29 2e 20 2a 2f 0a 20 20 20   ENC(db). */.   
29c4a 20 20 20 65 6e 63 6f 64 69 6e 67 20 3d 20 28 75     encoding = (u
29c4b 38 29 6d 65 74 61 5b 42 54 52 45 45 5f 54 45 58  8)meta[BTREE_TEX
29c4c 54 5f 45 4e 43 4f 44 49 4e 47 2d 31 5d 20 26 20  T_ENCODING-1] & 
29c4d 33 3b 0a 20 20 20 20 20 20 69 66 28 20 65 6e 63  3;.      if( enc
29c4e 6f 64 69 6e 67 3d 3d 30 20 29 20 65 6e 63 6f 64  oding==0 ) encod
29c4f 69 6e 67 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  ing = SQLITE_UTF
29c50 38 3b 0a 20 20 20 20 20 20 45 4e 43 28 64 62 29  8;.      ENC(db)
29c51 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 20   = encoding;.   
29c52 20 20 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c     db->pDfltColl
29c53 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f   = sqlite3FindCo
29c54 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c 49 54 45  llSeq(db, SQLITE
29c55 5f 55 54 46 38 2c 20 22 42 49 4e 41 52 59 22 2c  _UTF8, "BINARY",
29c56 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a   0);.    }else{.
29c57 20 20 20 20 20 20 2f 2a 20 49 66 20 6f 70 65 6e        /* If open
29c58 69 6e 67 20 61 6e 20 61 74 74 61 63 68 65 64 20  ing an attached 
29c59 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 65 6e  database, the en
29c5a 63 6f 64 69 6e 67 20 6d 75 63 68 20 6d 61 74 63  coding much matc
29c5b 68 20 45 4e 43 28 64 62 29 20 2a 2f 0a 20 20 20  h ENC(db) */.   
29c5c 20 20 20 69 66 28 20 6d 65 74 61 5b 42 54 52 45     if( meta[BTRE
29c5d 45 5f 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47 2d  E_TEXT_ENCODING-
29c5e 31 5d 21 3d 45 4e 43 28 64 62 29 20 29 7b 0a 20  1]!=ENC(db) ){. 
29c5f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65         sqlite3Se
29c60 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67  tString(pzErrMsg
29c61 2c 20 64 62 2c 20 22 61 74 74 61 63 68 65 64 20  , db, "attached 
29c62 64 61 74 61 62 61 73 65 73 20 6d 75 73 74 20 75  databases must u
29c63 73 65 20 74 68 65 20 73 61 6d 65 22 0a 20 20 20  se the same".   
29c64 20 20 20 20 20 20 20 20 20 22 20 74 65 78 74 20           " text 
29c65 65 6e 63 6f 64 69 6e 67 20 61 73 20 6d 61 69 6e  encoding as main
29c66 20 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20 20   database");.   
29c67 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
29c68 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20  _ERROR;.        
29c69 67 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f 65 72 72  goto initone_err
29c6a 6f 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a  or_out;.      }.
29c6b 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
29c6c 20 20 20 44 62 53 65 74 50 72 6f 70 65 72 74 79     DbSetProperty
29c6d 28 64 62 2c 20 69 44 62 2c 20 44 42 5f 45 6d 70  (db, iDb, DB_Emp
29c6e 74 79 29 3b 0a 20 20 7d 0a 20 20 70 44 62 2d 3e  ty);.  }.  pDb->
29c6f 70 53 63 68 65 6d 61 2d 3e 65 6e 63 20 3d 20 45  pSchema->enc = E
29c70 4e 43 28 64 62 29 3b 0a 0a 20 20 69 66 28 20 70  NC(db);..  if( p
29c71 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63  Db->pSchema->cac
29c72 68 65 5f 73 69 7a 65 3d 3d 30 20 29 7b 0a 20 20  he_size==0 ){.  
29c73 20 20 73 69 7a 65 20 3d 20 6d 65 74 61 5b 42 54    size = meta[BT
29c74 52 45 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48  REE_DEFAULT_CACH
29c75 45 5f 53 49 5a 45 2d 31 5d 3b 0a 20 20 20 20 69  E_SIZE-1];.    i
29c76 66 28 20 73 69 7a 65 3d 3d 30 20 29 7b 20 73 69  f( size==0 ){ si
29c77 7a 65 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41  ze = SQLITE_DEFA
29c78 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 3b 20  ULT_CACHE_SIZE; 
29c79 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 3c 30  }.    if( size<0
29c7a 20 29 20 73 69 7a 65 20 3d 20 2d 73 69 7a 65 3b   ) size = -size;
29c7b 0a 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d  .    pDb->pSchem
29c7c 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 20 3d 20  a->cache_size = 
29c7d 73 69 7a 65 3b 0a 20 20 20 20 73 71 6c 69 74 65  size;.    sqlite
29c7e 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69  3BtreeSetCacheSi
29c7f 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20 70 44 62  ze(pDb->pBt, pDb
29c80 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65  ->pSchema->cache
29c81 5f 73 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f  _size);.  }..  /
29c82 2a 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d  *.  ** file_form
29c83 61 74 3d 3d 31 20 20 20 20 56 65 72 73 69 6f 6e  at==1    Version
29c84 20 33 2e 30 2e 30 2e 0a 20 20 2a 2a 20 66 69 6c   3.0.0..  ** fil
29c85 65 5f 66 6f 72 6d 61 74 3d 3d 32 20 20 20 20 56  e_format==2    V
29c86 65 72 73 69 6f 6e 20 33 2e 31 2e 33 2e 20 20 2f  ersion 3.1.3.  /
29c87 2f 20 41 4c 54 45 52 20 54 41 42 4c 45 20 41 44  / ALTER TABLE AD
29c88 44 20 43 4f 4c 55 4d 4e 0a 20 20 2a 2a 20 66 69  D COLUMN.  ** fi
29c89 6c 65 5f 66 6f 72 6d 61 74 3d 3d 33 20 20 20 20  le_format==3    
29c8a 56 65 72 73 69 6f 6e 20 33 2e 31 2e 34 2e 20 20  Version 3.1.4.  
29c8b 2f 2f 20 64 69 74 74 6f 20 62 75 74 20 77 69 74  // ditto but wit
29c8c 68 20 6e 6f 6e 2d 4e 55 4c 4c 20 64 65 66 61 75  h non-NULL defau
29c8d 6c 74 73 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f  lts.  ** file_fo
29c8e 72 6d 61 74 3d 3d 34 20 20 20 20 56 65 72 73 69  rmat==4    Versi
29c8f 6f 6e 20 33 2e 33 2e 30 2e 20 20 2f 2f 20 44 45  on 3.3.0.  // DE
29c90 53 43 20 69 6e 64 69 63 65 73 2e 20 20 42 6f 6f  SC indices.  Boo
29c91 6c 65 61 6e 20 63 6f 6e 73 74 61 6e 74 73 0a 20  lean constants. 
29c92 20 2a 2f 0a 20 20 70 44 62 2d 3e 70 53 63 68 65   */.  pDb->pSche
29c93 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20  ma->file_format 
29c94 3d 20 28 75 38 29 6d 65 74 61 5b 42 54 52 45 45  = (u8)meta[BTREE
29c95 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 2d 31 5d 3b  _FILE_FORMAT-1];
29c96 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68  .  if( pDb->pSch
29c97 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74  ema->file_format
29c98 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44 62 2d 3e  ==0 ){.    pDb->
29c99 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f  pSchema->file_fo
29c9a 72 6d 61 74 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  rmat = 1;.  }.  
29c9b 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61  if( pDb->pSchema
29c9c 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3e 53 51  ->file_format>SQ
29c9d 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f  LITE_MAX_FILE_FO
29c9e 52 4d 41 54 20 29 7b 0a 20 20 20 20 73 71 6c 69  RMAT ){.    sqli
29c9f 74 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45  te3SetString(pzE
29ca0 72 72 4d 73 67 2c 20 64 62 2c 20 22 75 6e 73 75  rrMsg, db, "unsu
29ca1 70 70 6f 72 74 65 64 20 66 69 6c 65 20 66 6f 72  pported file for
29ca2 6d 61 74 22 29 3b 0a 20 20 20 20 72 63 20 3d 20  mat");.    rc = 
29ca3 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
29ca4 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f 65    goto initone_e
29ca5 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20  rror_out;.  }.. 
29ca6 20 2f 2a 20 54 69 63 6b 65 74 20 23 32 38 30 34   /* Ticket #2804
29ca7 3a 20 20 57 68 65 6e 20 77 65 20 6f 70 65 6e 20  :  When we open 
29ca8 61 20 64 61 74 61 62 61 73 65 20 69 6e 20 74 68  a database in th
29ca9 65 20 6e 65 77 65 72 20 66 69 6c 65 20 66 6f 72  e newer file for
29caa 6d 61 74 2c 0a 20 20 2a 2a 20 63 6c 65 61 72 20  mat,.  ** clear 
29cab 74 68 65 20 6c 65 67 61 63 79 5f 66 69 6c 65 5f  the legacy_file_
29cac 66 6f 72 6d 61 74 20 70 72 61 67 6d 61 20 66 6c  format pragma fl
29cad 61 67 20 73 6f 20 74 68 61 74 20 61 20 56 41 43  ag so that a VAC
29cae 55 55 4d 20 77 69 6c 6c 0a 20 20 2a 2a 20 6e 6f  UUM will.  ** no
29caf 74 20 64 6f 77 6e 67 72 61 64 65 20 74 68 65 20  t downgrade the 
29cb0 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 68 75  database and thu
29cb1 73 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 79  s invalidate any
29cb2 20 64 65 73 63 65 6e 64 69 6e 67 0a 20 20 2a 2a   descending.  **
29cb3 20 69 6e 64 69 63 65 73 20 74 68 61 74 20 74 68   indices that th
29cb4 65 20 75 73 65 72 20 6d 69 67 68 74 20 68 61 76  e user might hav
29cb5 65 20 63 72 65 61 74 65 64 2e 0a 20 20 2a 2f 0a  e created..  */.
29cb6 20 20 69 66 28 20 69 44 62 3d 3d 30 20 26 26 20    if( iDb==0 && 
29cb7 6d 65 74 61 5b 42 54 52 45 45 5f 46 49 4c 45 5f  meta[BTREE_FILE_
29cb8 46 4f 52 4d 41 54 2d 31 5d 3e 3d 34 20 29 7b 0a  FORMAT-1]>=4 ){.
29cb9 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d      db->flags &=
29cba 20 7e 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46   ~SQLITE_LegacyF
29cbb 69 6c 65 46 6d 74 3b 0a 20 20 7d 0a 0a 20 20 2f  ileFmt;.  }..  /
29cbc 2a 20 52 65 61 64 20 74 68 65 20 73 63 68 65 6d  * Read the schem
29cbd 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75  a information ou
29cbe 74 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20  t of the schema 
29cbf 74 61 62 6c 65 73 0a 20 20 2a 2f 0a 20 20 61 73  tables.  */.  as
29cc0 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62  sert( db->init.b
29cc1 75 73 79 20 29 3b 0a 20 20 7b 0a 20 20 20 20 63  usy );.  {.    c
29cc2 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 20 20 7a  har *zSql;.    z
29cc3 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Sql = sqlite3MPr
29cc4 69 6e 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20  intf(db, .      
29cc5 20 20 22 53 45 4c 45 43 54 20 6e 61 6d 65 2c 20    "SELECT name, 
29cc6 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c 20 46 52  rootpage, sql FR
29cc7 4f 4d 20 27 25 71 27 2e 25 73 22 2c 0a 20 20 20  OM '%q'.%s",.   
29cc8 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62       db->aDb[iDb
29cc9 5d 2e 7a 4e 61 6d 65 2c 20 7a 4d 61 73 74 65 72  ].zName, zMaster
29cca 4e 61 6d 65 29 3b 0a 20 20 20 20 28 76 6f 69 64  Name);.    (void
29ccb 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66  )sqlite3SafetyOf
29ccc 66 28 64 62 29 3b 0a 23 69 66 6e 64 65 66 20 53  f(db);.#ifndef S
29ccd 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
29cce 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 7b 0a 20  RIZATION.    {. 
29ccf 20 20 20 20 20 69 6e 74 20 28 2a 78 41 75 74 68       int (*xAuth
29cd0 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
29cd1 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
29cd2 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
29cd3 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20  const char*);.  
29cd4 20 20 20 20 78 41 75 74 68 20 3d 20 64 62 2d 3e      xAuth = db->
29cd5 78 41 75 74 68 3b 0a 20 20 20 20 20 20 64 62 2d  xAuth;.      db-
29cd6 3e 78 41 75 74 68 20 3d 20 30 3b 0a 23 65 6e 64  >xAuth = 0;.#end
29cd7 69 66 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  if.      rc = sq
29cd8 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a  lite3_exec(db, z
29cd9 53 71 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69 74  Sql, sqlite3Init
29cda 43 61 6c 6c 62 61 63 6b 2c 20 26 69 6e 69 74 44  Callback, &initD
29cdb 61 74 61 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66  ata, 0);.#ifndef
29cdc 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
29cdd 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 20  HORIZATION.     
29cde 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 78 41 75   db->xAuth = xAu
29cdf 74 68 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  th;.    }.#endif
29ce0 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
29ce1 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 69 6e  ITE_OK ) rc = in
29ce2 69 74 44 61 74 61 2e 72 63 3b 0a 20 20 20 20 28  itData.rc;.    (
29ce3 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
29ce4 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 73 71  tyOn(db);.    sq
29ce5 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
29ce6 7a 53 71 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53  zSql);.#ifndef S
29ce7 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59  QLITE_OMIT_ANALY
29ce8 5a 45 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  ZE.    if( rc==S
29ce9 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
29cea 20 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73 69    sqlite3Analysi
29ceb 73 4c 6f 61 64 28 64 62 2c 20 69 44 62 29 3b 0a  sLoad(db, iDb);.
29cec 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
29ced 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
29cee 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72  cFailed ){.    r
29cef 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
29cf0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73  ;.    sqlite3Res
29cf1 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
29cf2 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69  (db, 0);.  }.  i
29cf3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
29cf4 20 7c 7c 20 28 64 62 2d 3e 66 6c 61 67 73 26 53   || (db->flags&S
29cf5 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f  QLITE_RecoveryMo
29cf6 64 65 29 29 7b 0a 20 20 20 20 2f 2a 20 42 6c 61  de)){.    /* Bla
29cf7 63 6b 20 6d 61 67 69 63 3a 20 49 66 20 74 68 65  ck magic: If the
29cf8 20 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79   SQLITE_Recovery
29cf9 4d 6f 64 65 20 66 6c 61 67 20 69 73 20 73 65 74  Mode flag is set
29cfa 2c 20 74 68 65 6e 20 63 6f 6e 73 69 64 65 72 0a  , then consider.
29cfb 20 20 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d      ** the schem
29cfc 61 20 6c 6f 61 64 65 64 2c 20 65 76 65 6e 20 69  a loaded, even i
29cfd 66 20 65 72 72 6f 72 73 20 6f 63 63 75 72 72 65  f errors occurre
29cfe 64 2e 20 49 6e 20 74 68 69 73 20 73 69 74 75 61  d. In this situa
29cff 74 69 6f 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a  tion the .    **
29d00 20 63 75 72 72 65 6e 74 20 73 71 6c 69 74 65 33   current sqlite3
29d01 5f 70 72 65 70 61 72 65 28 29 20 6f 70 65 72 61  _prepare() opera
29d02 74 69 6f 6e 20 77 69 6c 6c 20 66 61 69 6c 2c 20  tion will fail, 
29d03 62 75 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  but the followin
29d04 67 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 77 69 6c  g one.    ** wil
29d05 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6d  l attempt to com
29d06 70 69 6c 65 20 74 68 65 20 73 75 70 70 6c 69 65  pile the supplie
29d07 64 20 73 74 61 74 65 6d 65 6e 74 20 61 67 61 69  d statement agai
29d08 6e 73 74 20 77 68 61 74 65 76 65 72 20 73 75 62  nst whatever sub
29d09 73 65 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68  set.    ** of th
29d0a 65 20 73 63 68 65 6d 61 20 77 61 73 20 6c 6f 61  e schema was loa
29d0b 64 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 65  ded before the e
29d0c 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2e 20 54  rror occurred. T
29d0d 68 65 20 70 72 69 6d 61 72 79 0a 20 20 20 20 2a  he primary.    *
29d0e 2a 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69  * purpose of thi
29d0f 73 20 69 73 20 74 6f 20 61 6c 6c 6f 77 20 61 63  s is to allow ac
29d10 63 65 73 73 20 74 6f 20 74 68 65 20 73 71 6c 69  cess to the sqli
29d11 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 0a  te_master table.
29d12 20 20 20 20 2a 2a 20 65 76 65 6e 20 77 68 65 6e      ** even when
29d13 20 69 74 73 20 63 6f 6e 74 65 6e 74 73 20 68 61   its contents ha
29d14 76 65 20 62 65 65 6e 20 63 6f 72 72 75 70 74 65  ve been corrupte
29d15 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 44 62  d..    */.    Db
29d16 53 65 74 50 72 6f 70 65 72 74 79 28 64 62 2c 20  SetProperty(db, 
29d17 69 44 62 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f  iDb, DB_SchemaLo
29d18 61 64 65 64 29 3b 0a 20 20 20 20 72 63 20 3d 20  aded);.    rc = 
29d19 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
29d1a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 66    /* Jump here f
29d1b 6f 72 20 61 6e 20 65 72 72 6f 72 20 74 68 61 74  or an error that
29d1c 20 6f 63 63 75 72 73 20 61 66 74 65 72 20 73 75   occurs after su
29d1d 63 63 65 73 73 66 75 6c 6c 79 20 61 6c 6c 6f 63  ccessfully alloc
29d1e 61 74 69 6e 67 0a 20 20 2a 2a 20 63 75 72 4d 61  ating.  ** curMa
29d1f 69 6e 20 61 6e 64 20 63 61 6c 6c 69 6e 67 20 73  in and calling s
29d20 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
29d21 28 29 2e 20 46 6f 72 20 61 6e 20 65 72 72 6f 72  (). For an error
29d22 20 74 68 61 74 20 6f 63 63 75 72 73 0a 20 20 2a   that occurs.  *
29d23 2a 20 62 65 66 6f 72 65 20 74 68 61 74 20 70 6f  * before that po
29d24 69 6e 74 2c 20 6a 75 6d 70 20 74 6f 20 65 72 72  int, jump to err
29d25 6f 72 5f 6f 75 74 2e 0a 20 20 2a 2f 0a 69 6e 69  or_out..  */.ini
29d26 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75 74 3a 0a  tone_error_out:.
29d27 20 20 69 66 28 20 6f 70 65 6e 65 64 54 72 61 6e    if( openedTran
29d28 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 73  saction ){.    s
29d29 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
29d2a 74 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 7d  t(pDb->pBt);.  }
29d2b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
29d2c 65 61 76 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a  eave(pDb->pBt);.
29d2d 0a 65 72 72 6f 72 5f 6f 75 74 3a 0a 20 20 69 66  .error_out:.  if
29d2e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
29d2f 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45  EM || rc==SQLITE
29d30 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a  _IOERR_NOMEM ){.
29d31 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
29d32 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  iled = 1;.  }.  
29d33 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
29d34 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
29d35 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ll database file
29d36 73 20 2d 20 74 68 65 20 6d 61 69 6e 20 64 61 74  s - the main dat
29d37 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 20  abase file, the 
29d38 66 69 6c 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20  file.** used to 
29d39 73 74 6f 72 65 20 74 65 6d 70 6f 72 61 72 79 20  store temporary 
29d3a 74 61 62 6c 65 73 2c 20 61 6e 64 20 61 6e 79 20  tables, and any 
29d3b 61 64 64 69 74 69 6f 6e 61 6c 20 64 61 74 61 62  additional datab
29d3c 61 73 65 20 66 69 6c 65 73 0a 2a 2a 20 63 72 65  ase files.** cre
29d3d 61 74 65 64 20 75 73 69 6e 67 20 41 54 54 41 43  ated using ATTAC
29d3e 48 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 52  H statements.  R
29d3f 65 74 75 72 6e 20 61 20 73 75 63 63 65 73 73 20  eturn a success 
29d40 63 6f 64 65 2e 20 20 49 66 20 61 6e 0a 2a 2a 20  code.  If an.** 
29d41 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 77 72  error occurs, wr
29d42 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ite an error mes
29d43 73 61 67 65 20 69 6e 74 6f 20 2a 70 7a 45 72 72  sage into *pzErr
29d44 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72  Msg..**.** After
29d45 20 61 20 64 61 74 61 62 61 73 65 20 69 73 20 69   a database is i
29d46 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68 65 20  nitialized, the 
29d47 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 20  DB_SchemaLoaded 
29d48 62 69 74 20 69 73 20 73 65 74 0a 2a 2a 20 62 69  bit is set.** bi
29d49 74 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20  t is set in the 
29d4a 66 6c 61 67 73 20 66 69 65 6c 64 20 6f 66 20 74  flags field of t
29d4b 68 65 20 44 62 20 73 74 72 75 63 74 75 72 65 2e  he Db structure.
29d4c 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65   If the database
29d4d 0a 2a 2a 20 66 69 6c 65 20 77 61 73 20 6f 66 20  .** file was of 
29d4e 7a 65 72 6f 2d 6c 65 6e 67 74 68 2c 20 74 68 65  zero-length, the
29d4f 6e 20 74 68 65 20 44 42 5f 45 6d 70 74 79 20 66  n the DB_Empty f
29d50 6c 61 67 20 69 73 20 61 6c 73 6f 20 73 65 74 2e  lag is also set.
29d51 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
29d52 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e  TE int sqlite3In
29d53 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  it(sqlite3 *db, 
29d54 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 29  char **pzErrMsg)
29d55 7b 0a 20 20 69 6e 74 20 69 2c 20 72 63 3b 0a 20  {.  int i, rc;. 
29d56 20 69 6e 74 20 63 6f 6d 6d 69 74 5f 69 6e 74 65   int commit_inte
29d57 72 6e 61 6c 20 3d 20 21 28 64 62 2d 3e 66 6c 61  rnal = !(db->fla
29d58 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  gs&SQLITE_Intern
29d59 43 68 61 6e 67 65 73 29 3b 0a 20 20 0a 20 20 61  Changes);.  .  a
29d5a 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
29d5b 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
29d5c 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 53  tex) );.  rc = S
29d5d 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 64 62 2d 3e  QLITE_OK;.  db->
29d5e 69 6e 69 74 2e 62 75 73 79 20 3d 20 31 3b 0a 20  init.busy = 1;. 
29d5f 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51   for(i=0; rc==SQ
29d60 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d  LITE_OK && i<db-
29d61 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
29d62 69 66 28 20 44 62 48 61 73 50 72 6f 70 65 72 74  if( DbHasPropert
29d63 79 28 64 62 2c 20 69 2c 20 44 42 5f 53 63 68 65  y(db, i, DB_Sche
29d64 6d 61 4c 6f 61 64 65 64 29 20 7c 7c 20 69 3d 3d  maLoaded) || i==
29d65 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  1 ) continue;.  
29d66 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49 6e    rc = sqlite3In
29d67 69 74 4f 6e 65 28 64 62 2c 20 69 2c 20 70 7a 45  itOne(db, i, pzE
29d68 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66 28 20  rrMsg);.    if( 
29d69 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  rc ){.      sqli
29d6a 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c  te3ResetInternal
29d6b 53 63 68 65 6d 61 28 64 62 2c 20 69 29 3b 0a 20  Schema(db, i);. 
29d6c 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f     }.  }..  /* O
29d6d 6e 63 65 20 61 6c 6c 20 74 68 65 20 6f 74 68 65  nce all the othe
29d6e 72 20 64 61 74 61 62 61 73 65 73 20 68 61 76 65  r databases have
29d6f 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 73 65   been initialise
29d70 64 2c 20 6c 6f 61 64 20 74 68 65 20 73 63 68 65  d, load the sche
29d71 6d 61 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20  ma.  ** for the 
29d72 54 45 4d 50 20 64 61 74 61 62 61 73 65 2e 20 54  TEMP database. T
29d73 68 69 73 20 69 73 20 6c 6f 61 64 65 64 20 6c 61  his is loaded la
29d74 73 74 2c 20 61 73 20 74 68 65 20 54 45 4d 50 20  st, as the TEMP 
29d75 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 73 63  database.  ** sc
29d76 68 65 6d 61 20 6d 61 79 20 63 6f 6e 74 61 69 6e  hema may contain
29d77 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 6f   references to o
29d78 62 6a 65 63 74 73 20 69 6e 20 6f 74 68 65 72 20  bjects in other 
29d79 64 61 74 61 62 61 73 65 73 2e 0a 20 20 2a 2f 0a  databases..  */.
29d7a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
29d7b 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20 69 66 28  MIT_TEMPDB.  if(
29d7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
29d7d 26 20 41 4c 57 41 59 53 28 64 62 2d 3e 6e 44 62  & ALWAYS(db->nDb
29d7e 3e 31 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  >1).            
29d7f 20 20 20 20 20 20 20 20 26 26 20 21 44 62 48 61          && !DbHa
29d80 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 31 2c  sProperty(db, 1,
29d81 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64   DB_SchemaLoaded
29d82 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  ) ){.    rc = sq
29d83 6c 69 74 65 33 49 6e 69 74 4f 6e 65 28 64 62 2c  lite3InitOne(db,
29d84 20 31 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a 20   1, pzErrMsg);. 
29d85 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
29d86 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49     sqlite3ResetI
29d87 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62  nternalSchema(db
29d88 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  , 1);.    }.  }.
29d89 23 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 69 6e  #endif..  db->in
29d8a 69 74 2e 62 75 73 79 20 3d 20 30 3b 0a 20 20 69  it.busy = 0;.  i
29d8b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
29d8c 20 26 26 20 63 6f 6d 6d 69 74 5f 69 6e 74 65 72   && commit_inter
29d8d 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  nal ){.    sqlit
29d8e 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c  e3CommitInternal
29d8f 43 68 61 6e 67 65 73 28 64 62 29 3b 0a 20 20 7d  Changes(db);.  }
29d90 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a  ..  return rc; .
29d91 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
29d92 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
29d93 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65   if the database
29d94 20 73 63 68 65 6d 61 20 69 73 20 61 6c 72 65 61   schema is alrea
29d95 64 79 20 69 6e 69 74 69 61 6c 69 73 65 64 2e 0a  dy initialised..
29d96 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68  ** Otherwise, th
29d97 65 20 73 63 68 65 6d 61 20 69 73 20 6c 6f 61 64  e schema is load
29d98 65 64 2e 20 41 6e 20 65 72 72 6f 72 20 63 6f 64  ed. An error cod
29d99 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  e is returned..*
29d9a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
29d9b 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 61 64   int sqlite3Read
29d9c 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70 50  Schema(Parse *pP
29d9d 61 72 73 65 29 7b 0a 20 20 69 6e 74 20 72 63 20  arse){.  int rc 
29d9e 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73  = SQLITE_OK;.  s
29d9f 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
29da0 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72  rse->db;.  asser
29da1 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
29da2 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
29da3 20 29 3b 0a 20 20 69 66 28 20 21 64 62 2d 3e 69   );.  if( !db->i
29da4 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20  nit.busy ){.    
29da5 72 63 20 3d 20 73 71 6c 69 74 65 33 49 6e 69 74  rc = sqlite3Init
29da6 28 64 62 2c 20 26 70 50 61 72 73 65 2d 3e 7a 45  (db, &pParse->zE
29da7 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 20 20 69 66  rrMsg);.  }.  if
29da8 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
29da9 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72  ){.    pParse->r
29daa 63 20 3d 20 72 63 3b 0a 20 20 20 20 70 50 61 72  c = rc;.    pPar
29dab 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a  se->nErr++;.  }.
29dac 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
29dad 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 73 63 68  ./*.** Check sch
29dae 65 6d 61 20 63 6f 6f 6b 69 65 73 20 69 6e 20 61  ema cookies in a
29daf 6c 6c 20 64 61 74 61 62 61 73 65 73 2e 20 20 49  ll databases.  I
29db0 66 20 61 6e 79 20 63 6f 6f 6b 69 65 20 69 73 20  f any cookie is 
29db1 6f 75 74 0a 2a 2a 20 6f 66 20 64 61 74 65 20 73  out.** of date s
29db2 65 74 20 70 50 61 72 73 65 2d 3e 72 63 20 74 6f  et pParse->rc to
29db3 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 2e 20   SQLITE_SCHEMA. 
29db4 20 49 66 20 61 6c 6c 20 73 63 68 65 6d 61 20 63   If all schema c
29db5 6f 6f 6b 69 65 73 0a 2a 2a 20 6d 61 6b 65 20 6e  ookies.** make n
29db6 6f 20 63 68 61 6e 67 65 73 20 74 6f 20 70 50 61  o changes to pPa
29db7 72 73 65 2d 3e 72 63 2e 0a 2a 2f 0a 73 74 61 74  rse->rc..*/.stat
29db8 69 63 20 76 6f 69 64 20 73 63 68 65 6d 61 49 73  ic void schemaIs
29db9 56 61 6c 69 64 28 50 61 72 73 65 20 2a 70 50 61  Valid(Parse *pPa
29dba 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  rse){.  sqlite3 
29dbb 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
29dbc 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 69  ;.  int iDb;.  i
29dbd 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 63 6f 6f  nt rc;.  int coo
29dbe 6b 69 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  kie;..  assert( 
29dbf 70 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68  pParse->checkSch
29dc0 65 6d 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ema );.  assert(
29dc1 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
29dc2 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
29dc3 3b 0a 20 20 66 6f 72 28 69 44 62 3d 30 3b 20 69  ;.  for(iDb=0; i
29dc4 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 69 44 62 2b  Db<db->nDb; iDb+
29dc5 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 65 6e  +){.    int open
29dc6 65 64 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20  edTransaction = 
29dc7 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  0;         /* Tr
29dc8 75 65 20 69 66 20 61 20 74 72 61 6e 73 61 63 74  ue if a transact
29dc9 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 20 2a 2f  ion is opened */
29dca 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20  .    Btree *pBt 
29dcb 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  = db->aDb[iDb].p
29dcc 42 74 3b 20 20 20 20 20 2f 2a 20 42 74 72 65 65  Bt;     /* Btree
29dcd 20 64 61 74 61 62 61 73 65 20 74 6f 20 72 65 61   database to rea
29dce 64 20 63 6f 6f 6b 69 65 20 66 72 6f 6d 20 2a 2f  d cookie from */
29dcf 0a 20 20 20 20 69 66 28 20 70 42 74 3d 3d 30 20  .    if( pBt==0 
29dd0 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20  ) continue;..   
29dd1 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
29dd2 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 20 72 65  not already a re
29dd3 61 64 2d 6f 6e 6c 79 20 28 6f 72 20 72 65 61 64  ad-only (or read
29dd4 2d 77 72 69 74 65 29 20 74 72 61 6e 73 61 63 74  -write) transact
29dd5 69 6f 6e 20 6f 70 65 6e 65 64 0a 20 20 20 20 2a  ion opened.    *
29dd6 2a 20 6f 6e 20 74 68 65 20 62 2d 74 72 65 65 20  * on the b-tree 
29dd7 64 61 74 61 62 61 73 65 2c 20 6f 70 65 6e 20 6f  database, open o
29dd8 6e 65 20 6e 6f 77 2e 20 49 66 20 61 20 74 72 61  ne now. If a tra
29dd9 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e  nsaction is open
29dda 65 64 2c 20 69 74 20 0a 20 20 20 20 2a 2a 20 77  ed, it .    ** w
29ddb 69 6c 6c 20 62 65 20 63 6c 6f 73 65 64 20 69 6d  ill be closed im
29ddc 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20  mediately after 
29ddd 72 65 61 64 69 6e 67 20 74 68 65 20 6d 65 74 61  reading the meta
29dde 2d 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 69  -value. */.    i
29ddf 66 28 20 21 73 71 6c 69 74 65 33 42 74 72 65 65  f( !sqlite3Btree
29de0 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 70 42  IsInReadTrans(pB
29de1 74 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  t) ){.      rc =
29de2 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67   sqlite3BtreeBeg
29de3 69 6e 54 72 61 6e 73 28 70 42 74 2c 20 30 29 3b  inTrans(pBt, 0);
29de4 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
29de5 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72  QLITE_NOMEM || r
29de6 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  c==SQLITE_IOERR_
29de7 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 20  NOMEM ){.       
29de8 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
29de9 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  d = 1;.      }. 
29dea 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
29deb 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 3b  ITE_OK ) return;
29dec 0a 20 20 20 20 20 20 6f 70 65 6e 65 64 54 72 61  .      openedTra
29ded 6e 73 61 63 74 69 6f 6e 20 3d 20 31 3b 0a 20 20  nsaction = 1;.  
29dee 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64    }..    /* Read
29def 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b   the schema cook
29df0 69 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ie from the data
29df1 62 61 73 65 2e 20 49 66 20 69 74 20 64 6f 65 73  base. If it does
29df2 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20 0a   not match the .
29df3 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 73 74 6f      ** value sto
29df4 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74  red as part of t
29df5 68 65 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d  he in the in-mem
29df6 6f 72 79 20 73 63 68 65 6d 61 20 72 65 70 72 65  ory schema repre
29df7 73 65 6e 74 61 74 69 6f 6e 2c 0a 20 20 20 20 2a  sentation,.    *
29df8 2a 20 73 65 74 20 50 61 72 73 65 2e 72 63 20 74  * set Parse.rc t
29df9 6f 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 2e  o SQLITE_SCHEMA.
29dfa 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42   */.    sqlite3B
29dfb 74 72 65 65 47 65 74 4d 65 74 61 28 70 42 74 2c  treeGetMeta(pBt,
29dfc 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45   BTREE_SCHEMA_VE
29dfd 52 53 49 4f 4e 2c 20 28 75 33 32 20 2a 29 26 63  RSION, (u32 *)&c
29dfe 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 69 66 28 20  ookie);.    if( 
29dff 63 6f 6f 6b 69 65 21 3d 64 62 2d 3e 61 44 62 5b  cookie!=db->aDb[
29e00 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 63  iDb].pSchema->sc
29e01 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 29 7b 0a 20  hema_cookie ){. 
29e02 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20       pParse->rc 
29e03 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b  = SQLITE_SCHEMA;
29e04 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
29e05 6c 6f 73 65 20 74 68 65 20 74 72 61 6e 73 61 63  lose the transac
29e06 74 69 6f 6e 2c 20 69 66 20 6f 6e 65 20 77 61 73  tion, if one was
29e07 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 20 20 20   opened. */.    
29e08 69 66 28 20 6f 70 65 6e 65 64 54 72 61 6e 73 61  if( openedTransa
29e09 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 73  ction ){.      s
29e0a 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
29e0b 74 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20 20  t(pBt);.    }.  
29e0c 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  }.}../*.** Conve
29e0d 72 74 20 61 20 73 63 68 65 6d 61 20 70 6f 69 6e  rt a schema poin
29e0e 74 65 72 20 69 6e 74 6f 20 74 68 65 20 69 44 62  ter into the iDb
29e0f 20 69 6e 64 65 78 20 74 68 61 74 20 69 6e 64 69   index that indi
29e10 63 61 74 65 73 0a 2a 2a 20 77 68 69 63 68 20 64  cates.** which d
29e11 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20  atabase file in 
29e12 64 62 2d 3e 61 44 62 5b 5d 20 74 68 65 20 73 63  db->aDb[] the sc
29e13 68 65 6d 61 20 72 65 66 65 72 73 20 74 6f 2e 0a  hema refers to..
29e14 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 61 6d  **.** If the sam
29e15 65 20 64 61 74 61 62 61 73 65 20 69 73 20 61 74  e database is at
29e16 74 61 63 68 65 64 20 6d 6f 72 65 20 74 68 61 6e  tached more than
29e17 20 6f 6e 63 65 2c 20 74 68 65 20 66 69 72 73 74   once, the first
29e18 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 64 61 74  .** attached dat
29e19 61 62 61 73 65 20 69 73 20 72 65 74 75 72 6e 65  abase is returne
29e1a 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
29e1b 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
29e1c 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 73 71  SchemaToIndex(sq
29e1d 6c 69 74 65 33 20 2a 64 62 2c 20 53 63 68 65 6d  lite3 *db, Schem
29e1e 61 20 2a 70 53 63 68 65 6d 61 29 7b 0a 20 20 69  a *pSchema){.  i
29e1f 6e 74 20 69 20 3d 20 2d 31 30 30 30 30 30 30 3b  nt i = -1000000;
29e20 0a 0a 20 20 2f 2a 20 49 66 20 70 53 63 68 65 6d  ..  /* If pSchem
29e21 61 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20  a is NULL, then 
29e22 72 65 74 75 72 6e 20 2d 31 30 30 30 30 30 30 2e  return -1000000.
29e23 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 68   This happens wh
29e24 65 6e 20 63 6f 64 65 20 69 6e 20 0a 20 20 2a 2a  en code in .  **
29e25 20 65 78 70 72 2e 63 20 69 73 20 74 72 79 69 6e   expr.c is tryin
29e26 67 20 74 6f 20 72 65 73 6f 6c 76 65 20 61 20 72  g to resolve a r
29e27 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 74 72  eference to a tr
29e28 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20 28 69  ansient table (i
29e29 2e 65 2e 20 6f 6e 65 0a 20 20 2a 2a 20 63 72 65  .e. one.  ** cre
29e2a 61 74 65 64 20 62 79 20 61 20 73 75 62 2d 73 65  ated by a sub-se
29e2b 6c 65 63 74 29 2e 20 49 6e 20 74 68 69 73 20 63  lect). In this c
29e2c 61 73 65 20 74 68 65 20 72 65 74 75 72 6e 20 76  ase the return v
29e2d 61 6c 75 65 20 6f 66 20 74 68 69 73 20 0a 20 20  alue of this .  
29e2e 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75  ** function shou
29e2f 6c 64 20 6e 65 76 65 72 20 62 65 20 75 73 65 64  ld never be used
29e30 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 72  ..  **.  ** We r
29e31 65 74 75 72 6e 20 2d 31 30 30 30 30 30 30 20 69  eturn -1000000 i
29e32 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6d 6f  nstead of the mo
29e33 72 65 20 75 73 75 61 6c 20 2d 31 20 73 69 6d 70  re usual -1 simp
29e34 6c 79 20 62 65 63 61 75 73 65 20 75 73 69 6e 67  ly because using
29e35 0a 20 20 2a 2a 20 2d 31 30 30 30 30 30 30 20 61  .  ** -1000000 a
29e36 73 20 74 68 65 20 69 6e 63 6f 72 72 65 63 74 20  s the incorrect 
29e37 69 6e 64 65 78 20 69 6e 74 6f 20 64 62 2d 3e 61  index into db->a
29e38 44 62 5b 5d 20 69 73 20 6d 75 63 68 20 0a 20 20  Db[] is much .  
29e39 2a 2a 20 6d 6f 72 65 20 6c 69 6b 65 6c 79 20 74  ** more likely t
29e3a 6f 20 63 61 75 73 65 20 61 20 73 65 67 66 61 75  o cause a segfau
29e3b 6c 74 20 74 68 61 6e 20 2d 31 20 28 6f 66 20 63  lt than -1 (of c
29e3c 6f 75 72 73 65 20 74 68 65 72 65 20 61 72 65 20  ourse there are 
29e3d 61 73 73 65 72 74 28 29 0a 20 20 2a 2a 20 73 74  assert().  ** st
29e3e 61 74 65 6d 65 6e 74 73 20 74 6f 6f 2c 20 62 75  atements too, bu
29e3f 74 20 69 74 20 6e 65 76 65 72 20 68 75 72 74 73  t it never hurts
29e40 20 74 6f 20 70 6c 61 79 20 74 68 65 20 6f 64 64   to play the odd
29e41 73 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  s)..  */.  asser
29e42 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
29e43 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
29e44 20 29 3b 0a 20 20 69 66 28 20 70 53 63 68 65 6d   );.  if( pSchem
29e45 61 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  a ){.    for(i=0
29e46 3b 20 41 4c 57 41 59 53 28 69 3c 64 62 2d 3e 6e  ; ALWAYS(i<db->n
29e47 44 62 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Db); i++){.     
29e48 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e   if( db->aDb[i].
29e49 70 53 63 68 65 6d 61 3d 3d 70 53 63 68 65 6d 61  pSchema==pSchema
29e4a 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61   ){.        brea
29e4b 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
29e4c 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d  .    assert( i>=
29e4d 30 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 20 29  0 && i<db->nDb )
29e4e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69  ;.  }.  return i
29e4f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 69  ;.}../*.** Compi
29e50 6c 65 20 74 68 65 20 55 54 46 2d 38 20 65 6e 63  le the UTF-8 enc
29e51 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  oded SQL stateme
29e52 6e 74 20 7a 53 71 6c 20 69 6e 74 6f 20 61 20 73  nt zSql into a s
29e53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e  tatement handle.
29e54 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
29e55 71 6c 69 74 65 33 50 72 65 70 61 72 65 28 0a 20  qlite3Prepare(. 
29e56 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
29e57 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
29e58 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a  tabase handle. *
29e59 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
29e5a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a  zSql,         /*
29e5b 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53   UTF-8 encoded S
29e5c 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f  QL statement. */
29e5d 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20  .  int nBytes,  
29e5e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
29e5f 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69  Length of zSql i
29e60 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 69 6e  n bytes. */.  in
29e61 74 20 73 61 76 65 53 71 6c 46 6c 61 67 2c 20 20  t saveSqlFlag,  
29e62 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
29e63 74 6f 20 63 6f 70 79 20 53 51 4c 20 74 65 78 74  to copy SQL text
29e64 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65   into the sqlite
29e65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20 73 71 6c 69  3_stmt */.  sqli
29e66 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d  te3_stmt **ppStm
29e67 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20  t,    /* OUT: A 
29e68 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
29e69 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
29e6a 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  t */.  const cha
29e6b 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 20  r **pzTail      
29e6c 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20   /* OUT: End of 
29e6d 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a 2f  parsed string */
29e6e 0a 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61  .){.  Parse *pPa
29e6f 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  rse;            
29e70 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
29e71 78 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45  xt */.  char *zE
29e72 72 72 4d 73 67 20 3d 20 30 3b 20 20 20 20 20 20  rrMsg = 0;      
29e73 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 61    /* Error messa
29e74 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d  ge */.  int rc =
29e75 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
29e76 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65    /* Result code
29e77 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20   */.  int i;    
29e78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29e79 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
29e7a 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  */..  /* Allocat
29e7b 65 20 74 68 65 20 70 61 72 73 69 6e 67 20 63 6f  e the parsing co
29e7c 6e 74 65 78 74 20 2a 2f 0a 20 20 70 50 61 72 73  ntext */.  pPars
29e7d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b  e = sqlite3Stack
29e7e 41 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  AllocZero(db, si
29e7f 7a 65 6f 66 28 2a 70 50 61 72 73 65 29 29 3b 0a  zeof(*pParse));.
29e80 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30 20    if( pParse==0 
29e81 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
29e82 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f  TE_NOMEM;.    go
29e83 74 6f 20 65 6e 64 5f 70 72 65 70 61 72 65 3b 0a  to end_prepare;.
29e84 20 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c 69 74    }..  if( sqlit
29e85 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29  e3SafetyOn(db) )
29e86 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
29e87 45 5f 4d 49 53 55 53 45 3b 0a 20 20 20 20 67 6f  E_MISUSE;.    go
29e88 74 6f 20 65 6e 64 5f 70 72 65 70 61 72 65 3b 0a  to end_prepare;.
29e89 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 70    }.  assert( pp
29e8a 53 74 6d 74 20 26 26 20 2a 70 70 53 74 6d 74 3d  Stmt && *ppStmt=
29e8b 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
29e8c 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
29e8d 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  d );.  assert( s
29e8e 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
29e8f 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
29e90 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 76  .  /* Check to v
29e91 65 72 69 66 79 20 74 68 61 74 20 69 74 20 69 73  erify that it is
29e92 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 67 65 74   possible to get
29e93 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20   a read lock on 
29e94 61 6c 6c 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  all.  ** databas
29e95 65 20 73 63 68 65 6d 61 73 2e 20 20 54 68 65 20  e schemas.  The 
29e96 69 6e 61 62 69 6c 69 74 79 20 74 6f 20 67 65 74  inability to get
29e97 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 69 6e 64   a read lock ind
29e98 69 63 61 74 65 73 20 74 68 61 74 0a 20 20 2a 2a  icates that.  **
29e99 20 73 6f 6d 65 20 6f 74 68 65 72 20 64 61 74 61   some other data
29e9a 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
29e9b 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 77 72 69  is holding a wri
29e9c 74 65 2d 6c 6f 63 6b 2c 20 77 68 69 63 68 20 69  te-lock, which i
29e9d 6e 0a 20 20 2a 2a 20 74 75 72 6e 20 6d 65 61 6e  n.  ** turn mean
29e9e 73 20 74 68 61 74 20 74 68 65 20 6f 74 68 65 72  s that the other
29e9f 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20   connection has 
29ea0 6d 61 64 65 20 75 6e 63 6f 6d 6d 69 74 74 65 64  made uncommitted
29ea1 20 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 74 6f   changes.  ** to
29ea2 20 74 68 65 20 73 63 68 65 6d 61 2e 0a 20 20 2a   the schema..  *
29ea3 2a 0a 20 20 2a 2a 20 57 65 72 65 20 77 65 20 74  *.  ** Were we t
29ea4 6f 20 70 72 6f 63 65 65 64 20 61 6e 64 20 70 72  o proceed and pr
29ea5 65 70 61 72 65 20 74 68 65 20 73 74 61 74 65 6d  epare the statem
29ea6 65 6e 74 20 61 67 61 69 6e 73 74 20 74 68 65 20  ent against the 
29ea7 75 6e 63 6f 6d 6d 69 74 74 65 64 0a 20 20 2a 2a  uncommitted.  **
29ea8 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 20   schema changes 
29ea9 61 6e 64 20 69 66 20 74 68 6f 73 65 20 73 63 68  and if those sch
29eaa 65 6d 61 20 63 68 61 6e 67 65 73 20 61 72 65 20  ema changes are 
29eab 73 75 62 73 65 71 75 65 6e 74 6c 79 20 72 6f 6c  subsequently rol
29eac 6c 65 64 0a 20 20 2a 2a 20 62 61 63 6b 20 61 6e  led.  ** back an
29ead 64 20 64 69 66 66 65 72 65 6e 74 20 63 68 61 6e  d different chan
29eae 67 65 73 20 61 72 65 20 6d 61 64 65 20 69 6e 20  ges are made in 
29eaf 74 68 65 69 72 20 70 6c 61 63 65 2c 20 74 68 65  their place, the
29eb0 6e 20 77 68 65 6e 20 74 68 69 73 0a 20 20 2a 2a  n when this.  **
29eb1 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
29eb2 65 6e 74 20 67 6f 65 73 20 74 6f 20 72 75 6e 20  ent goes to run 
29eb3 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  the schema cooki
29eb4 65 20 77 6f 75 6c 64 20 66 61 69 6c 20 74 6f 20  e would fail to 
29eb5 64 65 74 65 63 74 0a 20 20 2a 2a 20 74 68 65 20  detect.  ** the 
29eb6 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2e 20 20  schema change.  
29eb7 44 69 73 61 73 74 65 72 20 77 6f 75 6c 64 20 66  Disaster would f
29eb8 6f 6c 6c 6f 77 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  ollow..  **.  **
29eb9 20 54 68 69 73 20 74 68 72 65 61 64 20 69 73 20   This thread is 
29eba 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69 6e  currently holdin
29ebb 67 20 6d 75 74 65 78 65 73 20 6f 6e 20 61 6c 6c  g mutexes on all
29ebc 20 42 74 72 65 65 73 20 28 62 65 63 61 75 73 65   Btrees (because
29ebd 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c  .  ** of the sql
29ebe 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c  ite3BtreeEnterAl
29ebf 6c 28 29 20 69 6e 20 73 71 6c 69 74 65 33 4c 6f  l() in sqlite3Lo
29ec0 63 6b 41 6e 64 50 72 65 70 61 72 65 28 29 29 20  ckAndPrepare()) 
29ec1 73 6f 20 69 74 0a 20 20 2a 2a 20 69 73 20 6e 6f  so it.  ** is no
29ec2 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 61  t possible for a
29ec3 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 74 6f  nother thread to
29ec4 20 73 74 61 72 74 20 61 20 6e 65 77 20 73 63 68   start a new sch
29ec5 65 6d 61 20 63 68 61 6e 67 65 0a 20 20 2a 2a 20  ema change.  ** 
29ec6 77 68 69 6c 65 20 74 68 69 73 20 72 6f 75 74 69  while this routi
29ec7 6e 65 20 69 73 20 72 75 6e 6e 69 6e 67 2e 20 20  ne is running.  
29ec8 48 65 6e 63 65 2c 20 77 65 20 64 6f 20 6e 6f 74  Hence, we do not
29ec9 20 6e 65 65 64 20 74 6f 20 68 6f 6c 64 20 0a 20   need to hold . 
29eca 20 2a 2a 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65   ** locks on the
29ecb 20 73 63 68 65 6d 61 2c 20 77 65 20 6a 75 73 74   schema, we just
29ecc 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 73 75   need to make su
29ecd 72 65 20 6e 6f 62 6f 64 79 20 65 6c 73 65 20 69  re nobody else i
29ece 73 20 0a 20 20 2a 2a 20 68 6f 6c 64 69 6e 67 20  s .  ** holding 
29ecf 74 68 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  them..  **.  ** 
29ed0 4e 6f 74 65 20 74 68 61 74 20 73 65 74 74 69 6e  Note that settin
29ed1 67 20 52 45 41 44 5f 55 4e 43 4f 4d 4d 49 54 54  g READ_UNCOMMITT
29ed2 45 44 20 6f 76 65 72 72 69 64 65 73 20 6d 6f 73  ED overrides mos
29ed3 74 20 6c 6f 63 6b 20 64 65 74 65 63 74 69 6f 6e  t lock detection
29ed4 2c 0a 20 20 2a 2a 20 62 75 74 20 69 74 20 64 6f  ,.  ** but it do
29ed5 65 73 20 2a 6e 6f 74 2a 20 6f 76 65 72 72 69 64  es *not* overrid
29ed6 65 20 73 63 68 65 6d 61 20 6c 6f 63 6b 20 64 65  e schema lock de
29ed7 74 65 63 74 69 6f 6e 2c 20 73 6f 20 74 68 69 73  tection, so this
29ed8 20 61 6c 6c 20 73 74 69 6c 6c 0a 20 20 2a 2a 20   all still.  ** 
29ed9 77 6f 72 6b 73 20 65 76 65 6e 20 69 66 20 52 45  works even if RE
29eda 41 44 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 20 69  AD_UNCOMMITTED i
29edb 73 20 73 65 74 2e 0a 20 20 2a 2f 0a 20 20 66 6f  s set..  */.  fo
29edc 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62  r(i=0; i<db->nDb
29edd 3b 20 69 2b 2b 29 20 7b 0a 20 20 20 20 42 74 72  ; i++) {.    Btr
29ede 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44  ee *pBt = db->aD
29edf 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66  b[i].pBt;.    if
29ee0 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 61  ( pBt ){.      a
29ee1 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
29ee2 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 42  reeHoldsMutex(pB
29ee3 74 29 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  t) );.      rc =
29ee4 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68   sqlite3BtreeSch
29ee5 65 6d 61 4c 6f 63 6b 65 64 28 70 42 74 29 3b 0a  emaLocked(pBt);.
29ee6 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
29ee7 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
29ee8 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44  ar *zDb = db->aD
29ee9 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  b[i].zName;.    
29eea 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
29eeb 28 64 62 2c 20 72 63 2c 20 22 64 61 74 61 62 61  (db, rc, "databa
29eec 73 65 20 73 63 68 65 6d 61 20 69 73 20 6c 6f 63  se schema is loc
29eed 6b 65 64 3a 20 25 73 22 2c 20 7a 44 62 29 3b 0a  ked: %s", zDb);.
29eee 20 20 20 20 20 20 20 20 28 76 6f 69 64 29 73 71          (void)sq
29eef 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
29ef0 62 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74  b);.        test
29ef1 63 61 73 65 28 20 64 62 2d 3e 66 6c 61 67 73 20  case( db->flags 
29ef2 26 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63  & SQLITE_ReadUnc
29ef3 6f 6d 6d 69 74 74 65 64 20 29 3b 0a 20 20 20 20  ommitted );.    
29ef4 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 72 65      goto end_pre
29ef5 70 61 72 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  pare;.      }.  
29ef6 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74    }.  }..  sqlit
29ef7 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74  e3VtabUnlockList
29ef8 28 64 62 29 3b 0a 0a 20 20 70 50 61 72 73 65 2d  (db);..  pParse-
29ef9 3e 64 62 20 3d 20 64 62 3b 0a 20 20 69 66 28 20  >db = db;.  if( 
29efa 6e 42 79 74 65 73 3e 3d 30 20 26 26 20 28 6e 42  nBytes>=0 && (nB
29efb 79 74 65 73 3d 3d 30 20 7c 7c 20 7a 53 71 6c 5b  ytes==0 || zSql[
29efc 6e 42 79 74 65 73 2d 31 5d 21 3d 30 29 20 29 7b  nBytes-1]!=0) ){
29efd 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 71 6c 43  .    char *zSqlC
29efe 6f 70 79 3b 0a 20 20 20 20 69 6e 74 20 6d 78 4c  opy;.    int mxL
29eff 65 6e 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b  en = db->aLimit[
29f00 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c  SQLITE_LIMIT_SQL
29f01 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 20 20 74 65  _LENGTH];.    te
29f02 73 74 63 61 73 65 28 20 6e 42 79 74 65 73 3d 3d  stcase( nBytes==
29f03 6d 78 4c 65 6e 20 29 3b 0a 20 20 20 20 74 65 73  mxLen );.    tes
29f04 74 63 61 73 65 28 20 6e 42 79 74 65 73 3d 3d 6d  tcase( nBytes==m
29f05 78 4c 65 6e 2b 31 20 29 3b 0a 20 20 20 20 69 66  xLen+1 );.    if
29f06 28 20 6e 42 79 74 65 73 3e 6d 78 4c 65 6e 20 29  ( nBytes>mxLen )
29f07 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
29f08 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
29f09 54 4f 4f 42 49 47 2c 20 22 73 74 61 74 65 6d 65  TOOBIG, "stateme
29f0a 6e 74 20 74 6f 6f 20 6c 6f 6e 67 22 29 3b 0a 20  nt too long");. 
29f0b 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74       (void)sqlit
29f0c 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
29f0d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
29f0e 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 53  te3ApiExit(db, S
29f0f 51 4c 49 54 45 5f 54 4f 4f 42 49 47 29 3b 0a 20  QLITE_TOOBIG);. 
29f10 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 72       goto end_pr
29f11 65 70 61 72 65 3b 0a 20 20 20 20 7d 0a 20 20 20  epare;.    }.   
29f12 20 7a 53 71 6c 43 6f 70 79 20 3d 20 73 71 6c 69   zSqlCopy = sqli
29f13 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c  te3DbStrNDup(db,
29f14 20 7a 53 71 6c 2c 20 6e 42 79 74 65 73 29 3b 0a   zSql, nBytes);.
29f15 20 20 20 20 69 66 28 20 7a 53 71 6c 43 6f 70 79      if( zSqlCopy
29f16 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
29f17 33 52 75 6e 50 61 72 73 65 72 28 70 50 61 72 73  3RunParser(pPars
29f18 65 2c 20 7a 53 71 6c 43 6f 70 79 2c 20 26 7a 45  e, zSqlCopy, &zE
29f19 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 73 71  rrMsg);.      sq
29f1a 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
29f1b 7a 53 71 6c 43 6f 70 79 29 3b 0a 20 20 20 20 20  zSqlCopy);.     
29f1c 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d   pParse->zTail =
29f1d 20 26 7a 53 71 6c 5b 70 50 61 72 73 65 2d 3e 7a   &zSql[pParse->z
29f1e 54 61 69 6c 2d 7a 53 71 6c 43 6f 70 79 5d 3b 0a  Tail-zSqlCopy];.
29f1f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
29f20 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d   pParse->zTail =
29f21 20 26 7a 53 71 6c 5b 6e 42 79 74 65 73 5d 3b 0a   &zSql[nBytes];.
29f22 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
29f23 20 20 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72     sqlite3RunPar
29f24 73 65 72 28 70 50 61 72 73 65 2c 20 7a 53 71 6c  ser(pParse, zSql
29f25 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d  , &zErrMsg);.  }
29f26 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  ..  if( db->mall
29f27 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
29f28 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c  pParse->rc = SQL
29f29 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
29f2a 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63 3d   if( pParse->rc=
29f2b 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 70  =SQLITE_DONE ) p
29f2c 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49  Parse->rc = SQLI
29f2d 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 50 61  TE_OK;.  if( pPa
29f2e 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61  rse->checkSchema
29f2f 20 29 7b 0a 20 20 20 20 73 63 68 65 6d 61 49 73   ){.    schemaIs
29f30 56 61 6c 69 64 28 70 50 61 72 73 65 29 3b 0a 20  Valid(pParse);. 
29f31 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d   }.  if( pParse-
29f32 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45  >rc==SQLITE_SCHE
29f33 4d 41 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  MA ){.    sqlite
29f34 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
29f35 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d  hema(db, 0);.  }
29f36 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
29f37 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70  cFailed ){.    p
29f38 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49  Parse->rc = SQLI
29f39 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
29f3a 69 66 28 20 70 7a 54 61 69 6c 20 29 7b 0a 20 20  if( pzTail ){.  
29f3b 20 20 2a 70 7a 54 61 69 6c 20 3d 20 70 50 61 72    *pzTail = pPar
29f3c 73 65 2d 3e 7a 54 61 69 6c 3b 0a 20 20 7d 0a 20  se->zTail;.  }. 
29f3d 20 72 63 20 3d 20 70 50 61 72 73 65 2d 3e 72 63   rc = pParse->rc
29f3e 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
29f3f 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20  E_OMIT_EXPLAIN. 
29f40 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
29f41 4f 4b 20 26 26 20 70 50 61 72 73 65 2d 3e 70 56  OK && pParse->pV
29f42 64 62 65 20 26 26 20 70 50 61 72 73 65 2d 3e 65  dbe && pParse->e
29f43 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 73 74  xplain ){.    st
29f44 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
29f45 2a 20 63 6f 6e 73 74 20 61 7a 43 6f 6c 4e 61 6d  * const azColNam
29f46 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 22  e[] = {.       "
29f47 61 64 64 72 22 2c 20 22 6f 70 63 6f 64 65 22 2c  addr", "opcode",
29f48 20 22 70 31 22 2c 20 22 70 32 22 2c 20 22 70 33   "p1", "p2", "p3
29f49 22 2c 20 22 70 34 22 2c 20 22 70 35 22 2c 20 22  ", "p4", "p5", "
29f4a 63 6f 6d 6d 65 6e 74 22 2c 0a 20 20 20 20 20 20  comment",.      
29f4b 20 22 6f 72 64 65 72 22 2c 20 22 66 72 6f 6d 22   "order", "from"
29f4c 2c 20 22 64 65 74 61 69 6c 22 0a 20 20 20 20 7d  , "detail".    }
29f4d 3b 0a 20 20 20 20 69 6e 74 20 69 46 69 72 73 74  ;.    int iFirst
29f4e 2c 20 6d 78 3b 0a 20 20 20 20 69 66 28 20 70 50  , mx;.    if( pP
29f4f 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32  arse->explain==2
29f50 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
29f51 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
29f52 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 33  pParse->pVdbe, 3
29f53 29 3b 0a 20 20 20 20 20 20 69 46 69 72 73 74 20  );.      iFirst 
29f54 3d 20 38 3b 0a 20 20 20 20 20 20 6d 78 20 3d 20  = 8;.      mx = 
29f55 31 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  11;.    }else{. 
29f56 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29f57 53 65 74 4e 75 6d 43 6f 6c 73 28 70 50 61 72 73  SetNumCols(pPars
29f58 65 2d 3e 70 56 64 62 65 2c 20 38 29 3b 0a 20 20  e->pVdbe, 8);.  
29f59 20 20 20 20 69 46 69 72 73 74 20 3d 20 30 3b 0a      iFirst = 0;.
29f5a 20 20 20 20 20 20 6d 78 20 3d 20 38 3b 0a 20 20        mx = 8;.  
29f5b 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 69 46    }.    for(i=iF
29f5c 69 72 73 74 3b 20 69 3c 6d 78 3b 20 69 2b 2b 29  irst; i<mx; i++)
29f5d 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
29f5e 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 70 50  dbeSetColName(pP
29f5f 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 69 2d 69  arse->pVdbe, i-i
29f60 46 69 72 73 74 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  First, COLNAME_N
29f61 41 4d 45 2c 0a 20 20 20 20 20 20 20 20 20 20 20  AME,.           
29f62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29f63 20 61 7a 43 6f 6c 4e 61 6d 65 5b 69 5d 2c 20 53   azColName[i], S
29f64 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
29f65 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
29f66 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
29f67 66 65 74 79 4f 66 66 28 64 62 29 20 29 7b 0a 20  fetyOff(db) ){. 
29f68 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d     rc = SQLITE_M
29f69 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 20 20 61 73  ISUSE;.  }..  as
29f6a 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62  sert( db->init.b
29f6b 75 73 79 3d 3d 30 20 7c 7c 20 73 61 76 65 53 71  usy==0 || saveSq
29f6c 6c 46 6c 61 67 3d 3d 30 20 29 3b 0a 20 20 69 66  lFlag==0 );.  if
29f6d 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d  ( db->init.busy=
29f6e 3d 30 20 29 7b 0a 20 20 20 20 56 64 62 65 20 2a  =0 ){.    Vdbe *
29f6f 70 56 64 62 65 20 3d 20 70 50 61 72 73 65 2d 3e  pVdbe = pParse->
29f70 70 56 64 62 65 3b 0a 20 20 20 20 73 71 6c 69 74  pVdbe;.    sqlit
29f71 65 33 56 64 62 65 53 65 74 53 71 6c 28 70 56 64  e3VdbeSetSql(pVd
29f72 62 65 2c 20 7a 53 71 6c 2c 20 28 69 6e 74 29 28  be, zSql, (int)(
29f73 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 2d 7a 53  pParse->zTail-zS
29f74 71 6c 29 2c 20 73 61 76 65 53 71 6c 46 6c 61 67  ql), saveSqlFlag
29f75 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  );.  }.  if( pPa
29f76 72 73 65 2d 3e 70 56 64 62 65 20 26 26 20 28 72  rse->pVdbe && (r
29f77 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c!=SQLITE_OK || 
29f78 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
29f79 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
29f7a 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 70 50 61  VdbeFinalize(pPa
29f7b 72 73 65 2d 3e 70 56 64 62 65 29 3b 0a 20 20 20  rse->pVdbe);.   
29f7c 20 61 73 73 65 72 74 28 21 28 2a 70 70 53 74 6d   assert(!(*ppStm
29f7d 74 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  t));.  }else{.  
29f7e 20 20 2a 70 70 53 74 6d 74 20 3d 20 28 73 71 6c    *ppStmt = (sql
29f7f 69 74 65 33 5f 73 74 6d 74 2a 29 70 50 61 72 73  ite3_stmt*)pPars
29f80 65 2d 3e 70 56 64 62 65 3b 0a 20 20 7d 0a 0a 20  e->pVdbe;.  }.. 
29f81 20 69 66 28 20 7a 45 72 72 4d 73 67 20 29 7b 0a   if( zErrMsg ){.
29f82 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
29f83 28 64 62 2c 20 72 63 2c 20 22 25 73 22 2c 20 7a  (db, rc, "%s", z
29f84 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 73 71 6c  ErrMsg);.    sql
29f85 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
29f86 45 72 72 4d 73 67 29 3b 0a 20 20 7d 65 6c 73 65  ErrMsg);.  }else
29f87 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
29f88 6f 72 28 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20  or(db, rc, 0);. 
29f89 20 7d 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20   }..  /* Delete 
29f8a 61 6e 79 20 54 72 69 67 67 65 72 50 72 67 20 73  any TriggerPrg s
29f8b 74 72 75 63 74 75 72 65 73 20 61 6c 6c 6f 63 61  tructures alloca
29f8c 74 65 64 20 77 68 69 6c 65 20 70 61 72 73 69 6e  ted while parsin
29f8d 67 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74  g this statement
29f8e 2e 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 70 50  . */.  while( pP
29f8f 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 50 72  arse->pTriggerPr
29f90 67 20 29 7b 0a 20 20 20 20 54 72 69 67 67 65 72  g ){.    Trigger
29f91 50 72 67 20 2a 70 54 20 3d 20 70 50 61 72 73 65  Prg *pT = pParse
29f92 2d 3e 70 54 72 69 67 67 65 72 50 72 67 3b 0a 20  ->pTriggerPrg;. 
29f93 20 20 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67     pParse->pTrig
29f94 67 65 72 50 72 67 20 3d 20 70 54 2d 3e 70 4e 65  gerPrg = pT->pNe
29f95 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  xt;.    sqlite3V
29f96 64 62 65 50 72 6f 67 72 61 6d 44 65 6c 65 74 65  dbeProgramDelete
29f97 28 64 62 2c 20 70 54 2d 3e 70 50 72 6f 67 72 61  (db, pT->pProgra
29f98 6d 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  m, 0);.    sqlit
29f99 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 29  e3DbFree(db, pT)
29f9a 3b 0a 20 20 7d 0a 0a 65 6e 64 5f 70 72 65 70 61  ;.  }..end_prepa
29f9b 72 65 3a 0a 0a 20 20 73 71 6c 69 74 65 33 53 74  re:..  sqlite3St
29f9c 61 63 6b 46 72 65 65 28 64 62 2c 20 70 50 61 72  ackFree(db, pPar
29f9d 73 65 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  se);.  rc = sqli
29f9e 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
29f9f 63 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 72  c);.  assert( (r
29fa0 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d  c&db->errMask)==
29fa1 72 63 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  rc );.  return r
29fa2 63 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  c;.}.static int 
29fa3 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72  sqlite3LockAndPr
29fa4 65 70 61 72 65 28 0a 20 20 73 71 6c 69 74 65 33  epare(.  sqlite3
29fa5 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
29fa6 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68     /* Database h
29fa7 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73  andle. */.  cons
29fa8 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20  t char *zSql,   
29fa9 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65        /* UTF-8 e
29faa 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65  ncoded SQL state
29fab 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e  ment. */.  int n
29fac 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20  Bytes,          
29fad 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
29fae 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e  f zSql in bytes.
29faf 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 53 71   */.  int saveSq
29fb0 6c 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20  lFlag,          
29fb1 2f 2a 20 54 72 75 65 20 74 6f 20 63 6f 70 79 20  /* True to copy 
29fb2 53 51 4c 20 74 65 78 74 20 69 6e 74 6f 20 74 68  SQL text into th
29fb3 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e sqlite3_stmt *
29fb4 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
29fb5 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a   **ppStmt,    /*
29fb6 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20   OUT: A pointer 
29fb7 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20  to the prepared 
29fb8 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63  statement */.  c
29fb9 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61  onst char **pzTa
29fba 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  il       /* OUT:
29fbb 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73   End of parsed s
29fbc 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e  tring */.){.  in
29fbd 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  t rc;.  assert( 
29fbe 70 70 53 74 6d 74 21 3d 30 20 29 3b 0a 20 20 2a  ppStmt!=0 );.  *
29fbf 70 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 69 66  ppStmt = 0;.  if
29fc0 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79  ( !sqlite3Safety
29fc1 43 68 65 63 6b 4f 6b 28 64 62 29 20 29 7b 0a 20  CheckOk(db) ){. 
29fc2 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
29fc3 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73  _MISUSE;.  }.  s
29fc4 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
29fc5 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
29fc6 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
29fc7 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 72 63 20  erAll(db);.  rc 
29fc8 3d 20 73 71 6c 69 74 65 33 50 72 65 70 61 72 65  = sqlite3Prepare
29fc9 28 64 62 2c 20 7a 53 71 6c 2c 20 6e 42 79 74 65  (db, zSql, nByte
29fca 73 2c 20 73 61 76 65 53 71 6c 46 6c 61 67 2c 20  s, saveSqlFlag, 
29fcb 70 70 53 74 6d 74 2c 20 70 7a 54 61 69 6c 29 3b  ppStmt, pzTail);
29fcc 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
29fcd 45 5f 53 43 48 45 4d 41 20 29 7b 0a 20 20 20 20  E_SCHEMA ){.    
29fce 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
29fcf 28 2a 70 70 53 74 6d 74 29 3b 0a 20 20 20 20 72  (*ppStmt);.    r
29fd0 63 20 3d 20 73 71 6c 69 74 65 33 50 72 65 70 61  c = sqlite3Prepa
29fd1 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 6e 42 79  re(db, zSql, nBy
29fd2 74 65 73 2c 20 73 61 76 65 53 71 6c 46 6c 61 67  tes, saveSqlFlag
29fd3 2c 20 70 70 53 74 6d 74 2c 20 70 7a 54 61 69 6c  , ppStmt, pzTail
29fd4 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
29fd5 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62  BtreeLeaveAll(db
29fd6 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
29fd7 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
29fd8 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
29fd9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 72 75 6e  ;.}../*.** Rerun
29fda 20 74 68 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e   the compilation
29fdb 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20   of a statement 
29fdc 61 66 74 65 72 20 61 20 73 63 68 65 6d 61 20 63  after a schema c
29fdd 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  hange..**.** If 
29fde 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  the statement is
29fdf 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65   successfully re
29fe0 63 6f 6d 70 69 6c 65 64 2c 20 72 65 74 75 72 6e  compiled, return
29fe1 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65   SQLITE_OK. Othe
29fe2 72 77 69 73 65 2c 0a 2a 2a 20 69 66 20 74 68 65  rwise,.** if the
29fe3 20 73 74 61 74 65 6d 65 6e 74 20 63 61 6e 6e 6f   statement canno
29fe4 74 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20  t be recompiled 
29fe5 62 65 63 61 75 73 65 20 61 6e 6f 74 68 65 72 20  because another 
29fe6 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 0a 2a  connection has.*
29fe7 2a 20 6c 6f 63 6b 65 64 20 74 68 65 20 73 71 6c  * locked the sql
29fe8 69 74 65 33 5f 6d 61 73 74 65 72 20 74 61 62 6c  ite3_master tabl
29fe9 65 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  e, return SQLITE
29fea 5f 4c 4f 43 4b 45 44 2e 20 49 66 20 61 6e 79 20  _LOCKED. If any 
29feb 6f 74 68 65 72 20 65 72 72 6f 72 0a 2a 2a 20 6f  other error.** o
29fec 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 53 51  ccurs, return SQ
29fed 4c 49 54 45 5f 53 43 48 45 4d 41 2e 0a 2a 2f 0a  LITE_SCHEMA..*/.
29fee 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
29fef 6e 74 20 73 71 6c 69 74 65 33 52 65 70 72 65 70  nt sqlite3Reprep
29ff0 61 72 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  are(Vdbe *p){.  
29ff1 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
29ff2 33 5f 73 74 6d 74 20 2a 70 4e 65 77 3b 0a 20 20  3_stmt *pNew;.  
29ff3 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
29ff4 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ;.  sqlite3 *db;
29ff5 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
29ff6 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73  te3_mutex_held(s
29ff7 71 6c 69 74 65 33 56 64 62 65 44 62 28 70 29 2d  qlite3VdbeDb(p)-
29ff8 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 7a 53 71  >mutex) );.  zSq
29ff9 6c 20 3d 20 73 71 6c 69 74 65 33 5f 73 71 6c 28  l = sqlite3_sql(
29ffa 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29  (sqlite3_stmt *)
29ffb 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 53  p);.  assert( zS
29ffc 71 6c 21 3d 30 20 29 3b 20 20 2f 2a 20 52 65 70  ql!=0 );  /* Rep
29ffd 72 65 70 61 72 65 20 6f 6e 6c 79 20 63 61 6c 6c  repare only call
29ffe 65 64 20 66 6f 72 20 70 72 65 70 61 72 65 5f 76  ed for prepare_v
29fff 32 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 2a  2() statements *
2a000 2f 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33  /.  db = sqlite3
2a001 56 64 62 65 44 62 28 70 29 3b 0a 20 20 61 73 73  VdbeDb(p);.  ass
2a002 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
2a003 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
2a004 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  x) );.  rc = sql
2a005 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61  ite3LockAndPrepa
2a006 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  re(db, zSql, -1,
2a007 20 30 2c 20 26 70 4e 65 77 2c 20 30 29 3b 0a 20   0, &pNew, 0);. 
2a008 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 69   if( rc ){.    i
2a009 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
2a00a 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  MEM ){.      db-
2a00b 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
2a00c 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  1;.    }.    ass
2a00d 65 72 74 28 20 70 4e 65 77 3d 3d 30 20 29 3b 0a  ert( pNew==0 );.
2a00e 20 20 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d      return (rc==
2a00f 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 29 20 3f  SQLITE_LOCKED) ?
2a010 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 3a   SQLITE_LOCKED :
2a011 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a   SQLITE_SCHEMA;.
2a012 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
2a013 65 72 74 28 20 70 4e 65 77 21 3d 30 20 29 3b 0a  ert( pNew!=0 );.
2a014 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
2a015 65 53 77 61 70 28 28 56 64 62 65 2a 29 70 4e 65  eSwap((Vdbe*)pNe
2a016 77 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  w, p);.  sqlite3
2a017 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73  TransferBindings
2a018 28 70 4e 65 77 2c 20 28 73 71 6c 69 74 65 33 5f  (pNew, (sqlite3_
2a019 73 74 6d 74 2a 29 70 29 3b 0a 20 20 73 71 6c 69  stmt*)p);.  sqli
2a01a 74 65 33 56 64 62 65 52 65 73 65 74 53 74 65 70  te3VdbeResetStep
2a01b 52 65 73 75 6c 74 28 28 56 64 62 65 2a 29 70 4e  Result((Vdbe*)pN
2a01c 65 77 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ew);.  sqlite3Vd
2a01d 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64 62 65  beFinalize((Vdbe
2a01e 2a 29 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72  *)pNew);.  retur
2a01f 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
2a020 0a 2f 2a 0a 2a 2a 20 54 77 6f 20 76 65 72 73 69  ./*.** Two versi
2a021 6f 6e 73 20 6f 66 20 74 68 65 20 6f 66 66 69 63  ons of the offic
2a022 69 61 6c 20 41 50 49 2e 20 20 4c 65 67 61 63 79  ial API.  Legacy
2a023 20 61 6e 64 20 6e 65 77 20 75 73 65 2e 20 20 49   and new use.  I
2a024 6e 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a 20  n the legacy.** 
2a025 76 65 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69  version, the ori
2a026 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 69  ginal SQL text i
2a027 73 20 6e 6f 74 20 73 61 76 65 64 20 69 6e 20 74  s not saved in t
2a028 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74  he prepared stat
2a029 65 6d 65 6e 74 0a 2a 2a 20 61 6e 64 20 73 6f 20  ement.** and so 
2a02a 69 66 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e  if a schema chan
2a02b 67 65 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54  ge occurs, SQLIT
2a02c 45 5f 53 43 48 45 4d 41 20 69 73 20 72 65 74 75  E_SCHEMA is retu
2a02d 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74  rned by.** sqlit
2a02e 65 33 5f 73 74 65 70 28 29 2e 20 20 49 6e 20 74  e3_step().  In t
2a02f 68 65 20 6e 65 77 20 76 65 72 73 69 6f 6e 2c 20  he new version, 
2a030 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c  the original SQL
2a031 20 74 65 78 74 20 69 73 20 72 65 74 61 69 6e 65   text is retaine
2a032 64 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 74 61  d.** and the sta
2a033 74 65 6d 65 6e 74 20 69 73 20 61 75 74 6f 6d 61  tement is automa
2a034 74 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69 6c  tically recompil
2a035 65 64 20 69 66 20 61 6e 20 73 63 68 65 6d 61 20  ed if an schema 
2a036 63 68 61 6e 67 65 0a 2a 2a 20 6f 63 63 75 72 73  change.** occurs
2a037 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2a038 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70  int sqlite3_prep
2a039 61 72 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  are(.  sqlite3 *
2a03a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
2a03b 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
2a03c 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  dle. */.  const 
2a03d 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20  char *zSql,     
2a03e 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63      /* UTF-8 enc
2a03f 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  oded SQL stateme
2a040 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  nt. */.  int nBy
2a041 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  tes,            
2a042 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
2a043 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a  zSql in bytes. *
2a044 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
2a045 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a   **ppStmt,    /*
2a046 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20   OUT: A pointer 
2a047 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20  to the prepared 
2a048 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63  statement */.  c
2a049 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61  onst char **pzTa
2a04a 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  il       /* OUT:
2a04b 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73   End of parsed s
2a04c 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e  tring */.){.  in
2a04d 74 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c  t rc;.  rc = sql
2a04e 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61  ite3LockAndPrepa
2a04f 72 65 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65  re(db,zSql,nByte
2a050 73 2c 30 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69  s,0,ppStmt,pzTai
2a051 6c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  l);.  assert( rc
2a052 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70  ==SQLITE_OK || p
2a053 70 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53  pStmt==0 || *ppS
2a054 74 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45  tmt==0 );  /* VE
2a055 52 49 46 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a  RIFY: F13021 */.
2a056 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53    return rc;.}.S
2a057 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2a058 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
2a059 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
2a05a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a05b 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
2a05c 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  . */.  const cha
2a05d 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20  r *zSql,        
2a05e 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65   /* UTF-8 encode
2a05f 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  d SQL statement.
2a060 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73   */.  int nBytes
2a061 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2a062 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71  /* Length of zSq
2a063 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20  l in bytes. */. 
2a064 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
2a065 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55  ppStmt,    /* OU
2a066 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  T: A pointer to 
2a067 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61  the prepared sta
2a068 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73  tement */.  cons
2a069 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20  t char **pzTail 
2a06a 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e        /* OUT: En
2a06b 64 20 6f 66 20 70 61 72 73 65 64 20 73 74 72 69  d of parsed stri
2a06c 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  ng */.){.  int r
2a06d 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  c;.  rc = sqlite
2a06e 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28  3LockAndPrepare(
2a06f 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 31  db,zSql,nBytes,1
2a070 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b  ,ppStmt,pzTail);
2a071 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
2a072 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 70 53 74  QLITE_OK || ppSt
2a073 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53 74 6d 74  mt==0 || *ppStmt
2a074 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45 52 49 46  ==0 );  /* VERIF
2a075 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a 20 20 72  Y: F13021 */.  r
2a076 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 69  eturn rc;.}...#i
2a077 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2a078 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 43 6f  T_UTF16./*.** Co
2a079 6d 70 69 6c 65 20 74 68 65 20 55 54 46 2d 31 36  mpile the UTF-16
2a07a 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61   encoded SQL sta
2a07b 74 65 6d 65 6e 74 20 7a 53 71 6c 20 69 6e 74 6f  tement zSql into
2a07c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e   a statement han
2a07d 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  dle..*/.static i
2a07e 6e 74 20 73 71 6c 69 74 65 33 50 72 65 70 61 72  nt sqlite3Prepar
2a07f 65 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  e16(.  sqlite3 *
2a080 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
2a081 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
2a082 64 6c 65 2e 20 2a 2f 20 0a 20 20 63 6f 6e 73 74  dle. */ .  const
2a083 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20   void *zSql,    
2a084 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e       /* UTF-8 en
2a085 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d  coded SQL statem
2a086 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  ent. */.  int nB
2a087 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20  ytes,           
2a088 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
2a089 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20   zSql in bytes. 
2a08a 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 53 71 6c  */.  int saveSql
2a08b 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20 2f  Flag,          /
2a08c 2a 20 54 72 75 65 20 74 6f 20 73 61 76 65 20 53  * True to save S
2a08d 51 4c 20 74 65 78 74 20 69 6e 74 6f 20 74 68 65  QL text into the
2a08e 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2f   sqlite3_stmt */
2a08f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2a090 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20  **ppStmt,    /* 
2a091 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74  OUT: A pointer t
2a092 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73  o the prepared s
2a093 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f  tatement */.  co
2a094 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69  nst void **pzTai
2a095 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20  l       /* OUT: 
2a096 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73 74  End of parsed st
2a097 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20  ring */.){.  /* 
2a098 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 75  This function cu
2a099 72 72 65 6e 74 6c 79 20 77 6f 72 6b 73 20 62 79  rrently works by
2a09a 20 66 69 72 73 74 20 74 72 61 6e 73 66 6f 72 6d   first transform
2a09b 69 6e 67 20 74 68 65 20 55 54 46 2d 31 36 0a 20  ing the UTF-16. 
2a09c 20 2a 2a 20 65 6e 63 6f 64 65 64 20 73 74 72 69   ** encoded stri
2a09d 6e 67 20 74 6f 20 55 54 46 2d 38 2c 20 74 68 65  ng to UTF-8, the
2a09e 6e 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74  n invoking sqlit
2a09f 65 33 5f 70 72 65 70 61 72 65 28 29 2e 20 54 68  e3_prepare(). Th
2a0a0 65 0a 20 20 2a 2a 20 74 72 69 63 6b 79 20 62 69  e.  ** tricky bi
2a0a1 74 20 69 73 20 66 69 67 75 72 69 6e 67 20 6f 75  t is figuring ou
2a0a2 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f  t the pointer to
2a0a3 20 72 65 74 75 72 6e 20 69 6e 20 2a 70 7a 54 61   return in *pzTa
2a0a4 69 6c 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20  il..  */.  char 
2a0a5 2a 7a 53 71 6c 38 3b 0a 20 20 63 6f 6e 73 74 20  *zSql8;.  const 
2a0a6 63 68 61 72 20 2a 7a 54 61 69 6c 38 20 3d 20 30  char *zTail8 = 0
2a0a7 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
2a0a8 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72  ITE_OK;..  asser
2a0a9 74 28 20 70 70 53 74 6d 74 20 29 3b 0a 20 20 2a  t( ppStmt );.  *
2a0aa 70 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 69 66  ppStmt = 0;.  if
2a0ab 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79  ( !sqlite3Safety
2a0ac 43 68 65 63 6b 4f 6b 28 64 62 29 20 29 7b 0a 20  CheckOk(db) ){. 
2a0ad 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2a0ae 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73  _MISUSE;.  }.  s
2a0af 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
2a0b0 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
2a0b1 20 7a 53 71 6c 38 20 3d 20 73 71 6c 69 74 65 33   zSql8 = sqlite3
2a0b2 55 74 66 31 36 74 6f 38 28 64 62 2c 20 7a 53 71  Utf16to8(db, zSq
2a0b3 6c 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 69 66  l, nBytes);.  if
2a0b4 28 20 7a 53 71 6c 38 20 29 7b 0a 20 20 20 20 72  ( zSql8 ){.    r
2a0b5 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41  c = sqlite3LockA
2a0b6 6e 64 50 72 65 70 61 72 65 28 64 62 2c 20 7a 53  ndPrepare(db, zS
2a0b7 71 6c 38 2c 20 2d 31 2c 20 73 61 76 65 53 71 6c  ql8, -1, saveSql
2a0b8 46 6c 61 67 2c 20 70 70 53 74 6d 74 2c 20 26 7a  Flag, ppStmt, &z
2a0b9 54 61 69 6c 38 29 3b 0a 20 20 7d 0a 0a 20 20 69  Tail8);.  }..  i
2a0ba 66 28 20 7a 54 61 69 6c 38 20 26 26 20 70 7a 54  f( zTail8 && pzT
2a0bb 61 69 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  ail ){.    /* If
2a0bc 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
2a0bd 20 72 65 74 75 72 6e 73 20 61 20 74 61 69 6c 20   returns a tail 
2a0be 70 6f 69 6e 74 65 72 2c 20 77 65 20 63 61 6c 63  pointer, we calc
2a0bf 75 6c 61 74 65 20 74 68 65 0a 20 20 20 20 2a 2a  ulate the.    **
2a0c0 20 65 71 75 69 76 61 6c 65 6e 74 20 70 6f 69 6e   equivalent poin
2a0c1 74 65 72 20 69 6e 74 6f 20 74 68 65 20 55 54 46  ter into the UTF
2a0c2 2d 31 36 20 73 74 72 69 6e 67 20 62 79 20 63 6f  -16 string by co
2a0c3 75 6e 74 69 6e 67 20 74 68 65 20 75 6e 69 63 6f  unting the unico
2a0c4 64 65 0a 20 20 20 20 2a 2a 20 63 68 61 72 61 63  de.    ** charac
2a0c5 74 65 72 73 20 62 65 74 77 65 65 6e 20 7a 53 71  ters between zSq
2a0c6 6c 38 20 61 6e 64 20 7a 54 61 69 6c 38 2c 20 61  l8 and zTail8, a
2a0c7 6e 64 20 74 68 65 6e 20 72 65 74 75 72 6e 69 6e  nd then returnin
2a0c8 67 20 61 20 70 6f 69 6e 74 65 72 0a 20 20 20 20  g a pointer.    
2a0c9 2a 2a 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62  ** the same numb
2a0ca 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  er of characters
2a0cb 20 69 6e 74 6f 20 74 68 65 20 55 54 46 2d 31 36   into the UTF-16
2a0cc 20 73 74 72 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a   string..    */.
2a0cd 20 20 20 20 69 6e 74 20 63 68 61 72 73 5f 70 61      int chars_pa
2a0ce 72 73 65 64 20 3d 20 73 71 6c 69 74 65 33 55 74  rsed = sqlite3Ut
2a0cf 66 38 43 68 61 72 4c 65 6e 28 7a 53 71 6c 38 2c  f8CharLen(zSql8,
2a0d0 20 28 69 6e 74 29 28 7a 54 61 69 6c 38 2d 7a 53   (int)(zTail8-zS
2a0d1 71 6c 38 29 29 3b 0a 20 20 20 20 2a 70 7a 54 61  ql8));.    *pzTa
2a0d2 69 6c 20 3d 20 28 75 38 20 2a 29 7a 53 71 6c 20  il = (u8 *)zSql 
2a0d3 2b 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79  + sqlite3Utf16By
2a0d4 74 65 4c 65 6e 28 7a 53 71 6c 2c 20 63 68 61 72  teLen(zSql, char
2a0d5 73 5f 70 61 72 73 65 64 29 3b 0a 20 20 7d 0a 20  s_parsed);.  }. 
2a0d6 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2a0d7 62 2c 20 7a 53 71 6c 38 29 3b 20 0a 20 20 72 63  b, zSql8); .  rc
2a0d8 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
2a0d9 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c  t(db, rc);.  sql
2a0da 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
2a0db 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
2a0dc 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
2a0dd 2a 2a 20 54 77 6f 20 76 65 72 73 69 6f 6e 73 20  ** Two versions 
2a0de 6f 66 20 74 68 65 20 6f 66 66 69 63 69 61 6c 20  of the official 
2a0df 41 50 49 2e 20 20 4c 65 67 61 63 79 20 61 6e 64  API.  Legacy and
2a0e0 20 6e 65 77 20 75 73 65 2e 20 20 49 6e 20 74 68   new use.  In th
2a0e1 65 20 6c 65 67 61 63 79 0a 2a 2a 20 76 65 72 73  e legacy.** vers
2a0e2 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e 61  ion, the origina
2a0e3 6c 20 53 51 4c 20 74 65 78 74 20 69 73 20 6e 6f  l SQL text is no
2a0e4 74 20 73 61 76 65 64 20 69 6e 20 74 68 65 20 70  t saved in the p
2a0e5 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2a0e6 74 0a 2a 2a 20 61 6e 64 20 73 6f 20 69 66 20 61  t.** and so if a
2a0e7 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 20 6f   schema change o
2a0e8 63 63 75 72 73 2c 20 53 51 4c 49 54 45 5f 53 43  ccurs, SQLITE_SC
2a0e9 48 45 4d 41 20 69 73 20 72 65 74 75 72 6e 65 64  HEMA is returned
2a0ea 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73   by.** sqlite3_s
2a0eb 74 65 70 28 29 2e 20 20 49 6e 20 74 68 65 20 6e  tep().  In the n
2a0ec 65 77 20 76 65 72 73 69 6f 6e 2c 20 74 68 65 20  ew version, the 
2a0ed 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78  original SQL tex
2a0ee 74 20 69 73 20 72 65 74 61 69 6e 65 64 0a 2a 2a  t is retained.**
2a0ef 20 61 6e 64 20 74 68 65 20 73 74 61 74 65 6d 65   and the stateme
2a0f0 6e 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61  nt is automatica
2a0f1 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 20 69  lly recompiled i
2a0f2 66 20 61 6e 20 73 63 68 65 6d 61 20 63 68 61 6e  f an schema chan
2a0f3 67 65 0a 2a 2a 20 6f 63 63 75 72 73 2e 0a 2a 2f  ge.** occurs..*/
2a0f4 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2a0f5 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
2a0f6 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  6(.  sqlite3 *db
2a0f7 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
2a0f8 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c  * Database handl
2a0f9 65 2e 20 2a 2f 20 0a 20 20 63 6f 6e 73 74 20 76  e. */ .  const v
2a0fa 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20  oid *zSql,      
2a0fb 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f     /* UTF-8 enco
2a0fc 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ded SQL statemen
2a0fd 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  t. */.  int nByt
2a0fe 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  es,             
2a0ff 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a    /* Length of z
2a100 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f  Sql in bytes. */
2a101 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2a102 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20  **ppStmt,    /* 
2a103 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74  OUT: A pointer t
2a104 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73  o the prepared s
2a105 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f  tatement */.  co
2a106 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69  nst void **pzTai
2a107 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20  l       /* OUT: 
2a108 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73 74  End of parsed st
2a109 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ring */.){.  int
2a10a 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69   rc;.  rc = sqli
2a10b 74 65 33 50 72 65 70 61 72 65 31 36 28 64 62 2c  te3Prepare16(db,
2a10c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 30 2c 70 70  zSql,nBytes,0,pp
2a10d 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a 20 20  Stmt,pzTail);.  
2a10e 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
2a10f 54 45 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74 3d  TE_OK || ppStmt=
2a110 3d 30 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d 30  =0 || *ppStmt==0
2a111 20 29 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a 20   );  /* VERIFY: 
2a112 46 31 33 30 32 31 20 2a 2f 0a 20 20 72 65 74 75  F13021 */.  retu
2a113 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f  rn rc;.}.SQLITE_
2a114 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2a115 70 72 65 70 61 72 65 31 36 5f 76 32 28 0a 20 20  prepare16_v2(.  
2a116 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
2a117 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2a118 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f  abase handle. */
2a119 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a   .  const void *
2a11a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a  zSql,         /*
2a11b 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53   UTF-8 encoded S
2a11c 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f  QL statement. */
2a11d 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20  .  int nBytes,  
2a11e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a11f 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69  Length of zSql i
2a120 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71  n bytes. */.  sq
2a121 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53  lite3_stmt **ppS
2a122 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20  tmt,    /* OUT: 
2a123 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
2a124 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
2a125 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  ent */.  const v
2a126 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  oid **pzTail    
2a127 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f     /* OUT: End o
2a128 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20  f parsed string 
2a129 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
2a12a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 72    rc = sqlite3Pr
2a12b 65 70 61 72 65 31 36 28 64 62 2c 7a 53 71 6c 2c  epare16(db,zSql,
2a12c 6e 42 79 74 65 73 2c 31 2c 70 70 53 74 6d 74 2c  nBytes,1,ppStmt,
2a12d 70 7a 54 61 69 6c 29 3b 0a 20 20 61 73 73 65 72  pzTail);.  asser
2a12e 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
2a12f 20 7c 7c 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c   || ppStmt==0 ||
2a130 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20   *ppStmt==0 );  
2a131 2f 2a 20 56 45 52 49 46 59 3a 20 46 31 33 30 32  /* VERIFY: F1302
2a132 31 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63  1 */.  return rc
2a133 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
2a134 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
2a135 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
2a136 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72 65 70  **** End of prep
2a137 61 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  are.c **********
2a138 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a139 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a13a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2a13b 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
2a13c 73 65 6c 65 63 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  select.c *******
2a13d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a13e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a13f 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
2a140 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
2a141 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
2a142 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
2a143 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
2a144 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
2a145 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
2a146 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
2a147 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
2a148 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
2a149 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
2a14a 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2a14b 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
2a14c 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
2a14d 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
2a14e 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2a14f 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
2a150 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
2a151 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
2a152 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
2a153 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a154 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a155 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a156 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
2a157 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
2a158 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65  s C code routine
2a159 73 20 74 68 61 74 20 61 72 65 20 63 61 6c 6c 65  s that are calle
2a15a 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 0a  d by the parser.
2a15b 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 53 45 4c  ** to handle SEL
2a15c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69  ECT statements i
2a15d 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  n SQLite..**.** 
2a15e 24 49 64 3a 20 73 65 6c 65 63 74 2e 63 2c 76 20  $Id: select.c,v 
2a15f 31 2e 35 32 36 20 32 30 30 39 2f 30 38 2f 30 31  1.526 2009/08/01
2a160 20 31 35 3a 30 39 3a 35 38 20 64 72 68 20 45 78   15:09:58 drh Ex
2a161 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 44  p $.*/.../*.** D
2a162 65 6c 65 74 65 20 61 6c 6c 20 74 68 65 20 63 6f  elete all the co
2a163 6e 74 65 6e 74 20 6f 66 20 61 20 53 65 6c 65 63  ntent of a Selec
2a164 74 20 73 74 72 75 63 74 75 72 65 20 62 75 74 20  t structure but 
2a165 64 6f 20 6e 6f 74 20 64 65 61 6c 6c 6f 63 61 74  do not deallocat
2a166 65 0a 2a 2a 20 74 68 65 20 73 65 6c 65 63 74 20  e.** the select 
2a167 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66  structure itself
2a168 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2a169 20 63 6c 65 61 72 53 65 6c 65 63 74 28 73 71 6c   clearSelect(sql
2a16a 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74  ite3 *db, Select
2a16b 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 45   *p){.  sqlite3E
2a16c 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
2a16d 2c 20 70 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20  , p->pEList);.  
2a16e 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
2a16f 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 53 72 63  lete(db, p->pSrc
2a170 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2a171 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 57  Delete(db, p->pW
2a172 68 65 72 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  here);.  sqlite3
2a173 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64  ExprListDelete(d
2a174 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 29 3b  b, p->pGroupBy);
2a175 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
2a176 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 48 61 76  lete(db, p->pHav
2a177 69 6e 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ing);.  sqlite3E
2a178 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
2a179 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a  , p->pOrderBy);.
2a17a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2a17b 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 50 72  elete(db, p->pPr
2a17c 69 6f 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ior);.  sqlite3E
2a17d 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d  xprDelete(db, p-
2a17e 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 73 71 6c 69  >pLimit);.  sqli
2a17f 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
2a180 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 3b 0a 7d  , p->pOffset);.}
2a181 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
2a182 7a 65 20 61 20 53 65 6c 65 63 74 44 65 73 74 20  ze a SelectDest 
2a183 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51  structure..*/.SQ
2a184 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2a185 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  d sqlite3SelectD
2a186 65 73 74 49 6e 69 74 28 53 65 6c 65 63 74 44 65  estInit(SelectDe
2a187 73 74 20 2a 70 44 65 73 74 2c 20 69 6e 74 20 65  st *pDest, int e
2a188 44 65 73 74 2c 20 69 6e 74 20 69 50 61 72 6d 29  Dest, int iParm)
2a189 7b 0a 20 20 70 44 65 73 74 2d 3e 65 44 65 73 74  {.  pDest->eDest
2a18a 20 3d 20 28 75 38 29 65 44 65 73 74 3b 0a 20 20   = (u8)eDest;.  
2a18b 70 44 65 73 74 2d 3e 69 50 61 72 6d 20 3d 20 69  pDest->iParm = i
2a18c 50 61 72 6d 3b 0a 20 20 70 44 65 73 74 2d 3e 61  Parm;.  pDest->a
2a18d 66 66 69 6e 69 74 79 20 3d 20 30 3b 0a 20 20 70  ffinity = 0;.  p
2a18e 44 65 73 74 2d 3e 69 4d 65 6d 20 3d 20 30 3b 0a  Dest->iMem = 0;.
2a18f 20 20 70 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20    pDest->nMem = 
2a190 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  0;.}.../*.** All
2a191 6f 63 61 74 65 20 61 20 6e 65 77 20 53 65 6c 65  ocate a new Sele
2a192 63 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64  ct structure and
2a193 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   return a pointe
2a194 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20 73 74 72  r to that.** str
2a195 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54  ucture..*/.SQLIT
2a196 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63 74  E_PRIVATE Select
2a197 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e   *sqlite3SelectN
2a198 65 77 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ew(.  Parse *pPa
2a199 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
2a19a 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2a19b 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
2a19c 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 77 68 69  List,     /* whi
2a19d 63 68 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 69 6e  ch columns to in
2a19e 63 6c 75 64 65 20 69 6e 20 74 68 65 20 72 65 73  clude in the res
2a19f 75 6c 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ult */.  SrcList
2a1a0 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20 20 2f   *pSrc,        /
2a1a1 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  * the FROM claus
2a1a2 65 20 2d 2d 20 77 68 69 63 68 20 74 61 62 6c 65  e -- which table
2a1a3 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 20 45  s to scan */.  E
2a1a4 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20  xpr *pWhere,    
2a1a5 20 20 20 20 20 2f 2a 20 74 68 65 20 57 48 45 52       /* the WHER
2a1a6 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78  E clause */.  Ex
2a1a7 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79  prList *pGroupBy
2a1a8 2c 20 20 20 2f 2a 20 74 68 65 20 47 52 4f 55 50  ,   /* the GROUP
2a1a9 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
2a1aa 45 78 70 72 20 2a 70 48 61 76 69 6e 67 2c 20 20  Expr *pHaving,  
2a1ab 20 20 20 20 20 20 2f 2a 20 74 68 65 20 48 41 56        /* the HAV
2a1ac 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ING clause */.  
2a1ad 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
2a1ae 42 79 2c 20 20 20 2f 2a 20 74 68 65 20 4f 52 44  By,   /* the ORD
2a1af 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  ER BY clause */.
2a1b0 20 20 69 6e 74 20 69 73 44 69 73 74 69 6e 63 74    int isDistinct
2a1b1 2c 20 20 20 20 20 20 20 2f 2a 20 74 72 75 65 20  ,       /* true 
2a1b2 69 66 20 74 68 65 20 44 49 53 54 49 4e 43 54 20  if the DISTINCT 
2a1b3 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65  keyword is prese
2a1b4 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c  nt */.  Expr *pL
2a1b5 69 6d 69 74 2c 20 20 20 20 20 20 20 20 20 2f 2a  imit,         /*
2a1b6 20 4c 49 4d 49 54 20 76 61 6c 75 65 2e 20 20 4e   LIMIT value.  N
2a1b7 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73  ULL means not us
2a1b8 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f  ed */.  Expr *pO
2a1b9 66 66 73 65 74 20 20 20 20 20 20 20 20 20 2f 2a  ffset         /*
2a1ba 20 4f 46 46 53 45 54 20 76 61 6c 75 65 2e 20 20   OFFSET value.  
2a1bb 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 20 6f 66  NULL means no of
2a1bc 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 53 65 6c  fset */.){.  Sel
2a1bd 65 63 74 20 2a 70 4e 65 77 3b 0a 20 20 53 65 6c  ect *pNew;.  Sel
2a1be 65 63 74 20 73 74 61 6e 64 69 6e 3b 0a 20 20 73  ect standin;.  s
2a1bf 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2a1c0 72 73 65 2d 3e 64 62 3b 0a 20 20 70 4e 65 77 20  rse->db;.  pNew 
2a1c1 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2a1c2 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
2a1c3 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 61 73 73  (*pNew) );.  ass
2a1c4 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ert( db->mallocF
2a1c5 61 69 6c 65 64 20 7c 7c 20 21 70 4f 66 66 73 65  ailed || !pOffse
2a1c6 74 20 7c 7c 20 70 4c 69 6d 69 74 20 29 3b 20 2f  t || pLimit ); /
2a1c7 2a 20 4f 46 46 53 45 54 20 69 6d 70 6c 69 65 73  * OFFSET implies
2a1c8 20 4c 49 4d 49 54 20 2a 2f 0a 20 20 69 66 28 20   LIMIT */.  if( 
2a1c9 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 70  pNew==0 ){.    p
2a1ca 4e 65 77 20 3d 20 26 73 74 61 6e 64 69 6e 3b 0a  New = &standin;.
2a1cb 20 20 20 20 6d 65 6d 73 65 74 28 70 4e 65 77 2c      memset(pNew,
2a1cc 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77   0, sizeof(*pNew
2a1cd 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 45  ));.  }.  if( pE
2a1ce 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70  List==0 ){.    p
2a1cf 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45  EList = sqlite3E
2a1d0 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
2a1d1 61 72 73 65 2c 20 30 2c 20 73 71 6c 69 74 65 33  arse, 0, sqlite3
2a1d2 45 78 70 72 28 64 62 2c 54 4b 5f 41 4c 4c 2c 30  Expr(db,TK_ALL,0
2a1d3 29 29 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e  ));.  }.  pNew->
2a1d4 70 45 4c 69 73 74 20 3d 20 70 45 4c 69 73 74 3b  pEList = pEList;
2a1d5 0a 20 20 70 4e 65 77 2d 3e 70 53 72 63 20 3d 20  .  pNew->pSrc = 
2a1d6 70 53 72 63 3b 0a 20 20 70 4e 65 77 2d 3e 70 57  pSrc;.  pNew->pW
2a1d7 68 65 72 65 20 3d 20 70 57 68 65 72 65 3b 0a 20  here = pWhere;. 
2a1d8 20 70 4e 65 77 2d 3e 70 47 72 6f 75 70 42 79 20   pNew->pGroupBy 
2a1d9 3d 20 70 47 72 6f 75 70 42 79 3b 0a 20 20 70 4e  = pGroupBy;.  pN
2a1da 65 77 2d 3e 70 48 61 76 69 6e 67 20 3d 20 70 48  ew->pHaving = pH
2a1db 61 76 69 6e 67 3b 0a 20 20 70 4e 65 77 2d 3e 70  aving;.  pNew->p
2a1dc 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72  OrderBy = pOrder
2a1dd 42 79 3b 0a 20 20 70 4e 65 77 2d 3e 73 65 6c 46  By;.  pNew->selF
2a1de 6c 61 67 73 20 3d 20 69 73 44 69 73 74 69 6e 63  lags = isDistinc
2a1df 74 20 3f 20 53 46 5f 44 69 73 74 69 6e 63 74 20  t ? SF_Distinct 
2a1e0 3a 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6f 70 20  : 0;.  pNew->op 
2a1e1 3d 20 54 4b 5f 53 45 4c 45 43 54 3b 0a 20 20 70  = TK_SELECT;.  p
2a1e2 4e 65 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 4c  New->pLimit = pL
2a1e3 69 6d 69 74 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f  imit;.  pNew->pO
2a1e4 66 66 73 65 74 20 3d 20 70 4f 66 66 73 65 74 3b  ffset = pOffset;
2a1e5 0a 20 20 61 73 73 65 72 74 28 20 70 4f 66 66 73  .  assert( pOffs
2a1e6 65 74 3d 3d 30 20 7c 7c 20 70 4c 69 6d 69 74 21  et==0 || pLimit!
2a1e7 3d 30 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 61 64  =0 );.  pNew->ad
2a1e8 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20  drOpenEphm[0] = 
2a1e9 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72  -1;.  pNew->addr
2a1ea 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d 31  OpenEphm[1] = -1
2a1eb 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70  ;.  pNew->addrOp
2a1ec 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a  enEphm[2] = -1;.
2a1ed 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
2a1ee 46 61 69 6c 65 64 20 29 20 7b 0a 20 20 20 20 63  Failed ) {.    c
2a1ef 6c 65 61 72 53 65 6c 65 63 74 28 64 62 2c 20 70  learSelect(db, p
2a1f0 4e 65 77 29 3b 0a 20 20 20 20 69 66 28 20 70 4e  New);.    if( pN
2a1f1 65 77 21 3d 26 73 74 61 6e 64 69 6e 20 29 20 73  ew!=&standin ) s
2a1f2 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2a1f3 20 70 4e 65 77 29 3b 0a 20 20 20 20 70 4e 65 77   pNew);.    pNew
2a1f4 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
2a1f5 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pNew;.}../*.*
2a1f6 2a 20 44 65 6c 65 74 65 20 74 68 65 20 67 69 76  * Delete the giv
2a1f7 65 6e 20 53 65 6c 65 63 74 20 73 74 72 75 63 74  en Select struct
2a1f8 75 72 65 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69  ure and all of i
2a1f9 74 73 20 73 75 62 73 74 72 75 63 74 75 72 65 73  ts substructures
2a1fa 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2a1fb 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2a1fc 53 65 6c 65 63 74 44 65 6c 65 74 65 28 73 71 6c  SelectDelete(sql
2a1fd 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74  ite3 *db, Select
2a1fe 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
2a1ff 0a 20 20 20 20 63 6c 65 61 72 53 65 6c 65 63 74  .    clearSelect
2a200 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c  (db, p);.    sql
2a201 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2a202 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2a203 47 69 76 65 6e 20 31 20 74 6f 20 33 20 69 64 65  Given 1 to 3 ide
2a204 6e 74 69 66 69 65 72 73 20 70 72 65 63 65 65 64  ntifiers preceed
2a205 69 6e 67 20 74 68 65 20 4a 4f 49 4e 20 6b 65 79  ing the JOIN key
2a206 77 6f 72 64 2c 20 64 65 74 65 72 6d 69 6e 65 20  word, determine 
2a207 74 68 65 0a 2a 2a 20 74 79 70 65 20 6f 66 20 6a  the.** type of j
2a208 6f 69 6e 2e 20 20 52 65 74 75 72 6e 20 61 6e 20  oin.  Return an 
2a209 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74  integer constant
2a20a 20 74 68 61 74 20 65 78 70 72 65 73 73 65 73 20   that expresses 
2a20b 74 68 61 74 20 74 79 70 65 0a 2a 2a 20 69 6e 20  that type.** in 
2a20c 74 65 72 6d 73 20 6f 66 20 74 68 65 20 66 6f 6c  terms of the fol
2a20d 6c 6f 77 69 6e 67 20 62 69 74 20 76 61 6c 75 65  lowing bit value
2a20e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4a 54 5f  s:.**.**     JT_
2a20f 49 4e 4e 45 52 0a 2a 2a 20 20 20 20 20 4a 54 5f  INNER.**     JT_
2a210 43 52 4f 53 53 0a 2a 2a 20 20 20 20 20 4a 54 5f  CROSS.**     JT_
2a211 4f 55 54 45 52 0a 2a 2a 20 20 20 20 20 4a 54 5f  OUTER.**     JT_
2a212 4e 41 54 55 52 41 4c 0a 2a 2a 20 20 20 20 20 4a  NATURAL.**     J
2a213 54 5f 4c 45 46 54 0a 2a 2a 20 20 20 20 20 4a 54  T_LEFT.**     JT
2a214 5f 52 49 47 48 54 0a 2a 2a 0a 2a 2a 20 41 20 66  _RIGHT.**.** A f
2a215 75 6c 6c 20 6f 75 74 65 72 20 6a 6f 69 6e 20 69  ull outer join i
2a216 73 20 74 68 65 20 63 6f 6d 62 69 6e 61 74 69 6f  s the combinatio
2a217 6e 20 6f 66 20 4a 54 5f 4c 45 46 54 20 61 6e 64  n of JT_LEFT and
2a218 20 4a 54 5f 52 49 47 48 54 2e 0a 2a 2a 0a 2a 2a   JT_RIGHT..**.**
2a219 20 49 66 20 61 6e 20 69 6c 6c 65 67 61 6c 20 6f   If an illegal o
2a21a 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6a 6f  r unsupported jo
2a21b 69 6e 20 74 79 70 65 20 69 73 20 73 65 65 6e 2c  in type is seen,
2a21c 20 74 68 65 6e 20 73 74 69 6c 6c 20 72 65 74 75   then still retu
2a21d 72 6e 0a 2a 2a 20 61 20 6a 6f 69 6e 20 74 79 70  rn.** a join typ
2a21e 65 2c 20 62 75 74 20 70 75 74 20 61 6e 20 65 72  e, but put an er
2a21f 72 6f 72 20 69 6e 20 74 68 65 20 70 50 61 72 73  ror in the pPars
2a220 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  e structure..*/.
2a221 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2a222 6e 74 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79  nt sqlite3JoinTy
2a223 70 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  pe(Parse *pParse
2a224 2c 20 54 6f 6b 65 6e 20 2a 70 41 2c 20 54 6f 6b  , Token *pA, Tok
2a225 65 6e 20 2a 70 42 2c 20 54 6f 6b 65 6e 20 2a 70  en *pB, Token *p
2a226 43 29 7b 0a 20 20 69 6e 74 20 6a 6f 69 6e 74 79  C){.  int jointy
2a227 70 65 20 3d 20 30 3b 0a 20 20 54 6f 6b 65 6e 20  pe = 0;.  Token 
2a228 2a 61 70 41 6c 6c 5b 33 5d 3b 0a 20 20 54 6f 6b  *apAll[3];.  Tok
2a229 65 6e 20 2a 70 3b 0a 20 20 20 20 20 20 20 20 20  en *p;.         
2a22a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a22b 20 20 20 20 2f 2a 20 20 20 30 31 32 33 34 35 36      /*   0123456
2a22c 37 38 39 20 31 32 33 34 35 36 37 38 39 20 31 32  789 123456789 12
2a22d 33 34 35 36 37 38 39 20 31 32 33 20 2a 2f 0a 20  3456789 123 */. 
2a22e 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
2a22f 61 72 20 7a 4b 65 79 54 65 78 74 5b 5d 20 3d 20  ar zKeyText[] = 
2a230 22 6e 61 74 75 72 61 6c 65 66 74 6f 75 74 65 72  "naturaleftouter
2a231 69 67 68 74 66 75 6c 6c 69 6e 6e 65 72 63 72 6f  ightfullinnercro
2a232 73 73 22 3b 0a 20 20 73 74 61 74 69 63 20 63 6f  ss";.  static co
2a233 6e 73 74 20 73 74 72 75 63 74 20 7b 0a 20 20 20  nst struct {.   
2a234 20 75 38 20 69 3b 20 20 20 20 20 20 20 20 2f 2a   u8 i;        /*
2a235 20 42 65 67 69 6e 6e 69 6e 67 20 6f 66 20 6b 65   Beginning of ke
2a236 79 77 6f 72 64 20 74 65 78 74 20 69 6e 20 7a 4b  yword text in zK
2a237 65 79 54 65 78 74 5b 5d 20 2a 2f 0a 20 20 20 20  eyText[] */.    
2a238 75 38 20 6e 43 68 61 72 3b 20 20 20 20 2f 2a 20  u8 nChar;    /* 
2a239 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 6b 65  Length of the ke
2a23a 79 77 6f 72 64 20 69 6e 20 63 68 61 72 61 63 74  yword in charact
2a23b 65 72 73 20 2a 2f 0a 20 20 20 20 75 38 20 63 6f  ers */.    u8 co
2a23c 64 65 3b 20 20 20 20 20 2f 2a 20 4a 6f 69 6e 20  de;     /* Join 
2a23d 74 79 70 65 20 6d 61 73 6b 20 2a 2f 0a 20 20 7d  type mask */.  }
2a23e 20 61 4b 65 79 77 6f 72 64 5b 5d 20 3d 20 7b 0a   aKeyword[] = {.
2a23f 20 20 20 20 2f 2a 20 6e 61 74 75 72 61 6c 20 2a      /* natural *
2a240 2f 20 7b 20 30 2c 20 20 37 2c 20 4a 54 5f 4e 41  / { 0,  7, JT_NA
2a241 54 55 52 41 4c 20 20 20 20 20 20 20 20 20 20 20  TURAL           
2a242 20 20 20 20 20 7d 2c 0a 20 20 20 20 2f 2a 20 6c       },.    /* l
2a243 65 66 74 20 20 20 20 2a 2f 20 7b 20 36 2c 20 20  eft    */ { 6,  
2a244 34 2c 20 4a 54 5f 4c 45 46 54 7c 4a 54 5f 4f 55  4, JT_LEFT|JT_OU
2a245 54 45 52 20 20 20 20 20 20 20 20 20 20 7d 2c 0a  TER          },.
2a246 20 20 20 20 2f 2a 20 6f 75 74 65 72 20 20 20 2a      /* outer   *
2a247 2f 20 7b 20 31 30 2c 20 35 2c 20 4a 54 5f 4f 55  / { 10, 5, JT_OU
2a248 54 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20  TER             
2a249 20 20 20 20 20 7d 2c 0a 20 20 20 20 2f 2a 20 72       },.    /* r
2a24a 69 67 68 74 20 20 20 2a 2f 20 7b 20 31 34 2c 20  ight   */ { 14, 
2a24b 35 2c 20 4a 54 5f 52 49 47 48 54 7c 4a 54 5f 4f  5, JT_RIGHT|JT_O
2a24c 55 54 45 52 20 20 20 20 20 20 20 20 20 7d 2c 0a  UTER         },.
2a24d 20 20 20 20 2f 2a 20 66 75 6c 6c 20 20 20 20 2a      /* full    *
2a24e 2f 20 7b 20 31 39 2c 20 34 2c 20 4a 54 5f 4c 45  / { 19, 4, JT_LE
2a24f 46 54 7c 4a 54 5f 52 49 47 48 54 7c 4a 54 5f 4f  FT|JT_RIGHT|JT_O
2a250 55 54 45 52 20 7d 2c 0a 20 20 20 20 2f 2a 20 69  UTER },.    /* i
2a251 6e 6e 65 72 20 20 20 2a 2f 20 7b 20 32 33 2c 20  nner   */ { 23, 
2a252 35 2c 20 4a 54 5f 49 4e 4e 45 52 20 20 20 20 20  5, JT_INNER     
2a253 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 2c 0a               },.
2a254 20 20 20 20 2f 2a 20 63 72 6f 73 73 20 20 20 2a      /* cross   *
2a255 2f 20 7b 20 32 38 2c 20 35 2c 20 4a 54 5f 49 4e  / { 28, 5, JT_IN
2a256 4e 45 52 7c 4a 54 5f 43 52 4f 53 53 20 20 20 20  NER|JT_CROSS    
2a257 20 20 20 20 20 7d 2c 0a 20 20 7d 3b 0a 20 20 69       },.  };.  i
2a258 6e 74 20 69 2c 20 6a 3b 0a 20 20 61 70 41 6c 6c  nt i, j;.  apAll
2a259 5b 30 5d 20 3d 20 70 41 3b 0a 20 20 61 70 41 6c  [0] = pA;.  apAl
2a25a 6c 5b 31 5d 20 3d 20 70 42 3b 0a 20 20 61 70 41  l[1] = pB;.  apA
2a25b 6c 6c 5b 32 5d 20 3d 20 70 43 3b 0a 20 20 66 6f  ll[2] = pC;.  fo
2a25c 72 28 69 3d 30 3b 20 69 3c 33 20 26 26 20 61 70  r(i=0; i<3 && ap
2a25d 41 6c 6c 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20  All[i]; i++){.  
2a25e 20 20 70 20 3d 20 61 70 41 6c 6c 5b 69 5d 3b 0a    p = apAll[i];.
2a25f 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 41      for(j=0; j<A
2a260 72 72 61 79 53 69 7a 65 28 61 4b 65 79 77 6f 72  rraySize(aKeywor
2a261 64 29 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  d); j++){.      
2a262 69 66 28 20 70 2d 3e 6e 3d 3d 61 4b 65 79 77 6f  if( p->n==aKeywo
2a263 72 64 5b 6a 5d 2e 6e 43 68 61 72 20 0a 20 20 20  rd[j].nChar .   
2a264 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65         && sqlite
2a265 33 53 74 72 4e 49 43 6d 70 28 28 63 68 61 72 2a  3StrNICmp((char*
2a266 29 70 2d 3e 7a 2c 20 26 7a 4b 65 79 54 65 78 74  )p->z, &zKeyText
2a267 5b 61 4b 65 79 77 6f 72 64 5b 6a 5d 2e 69 5d 2c  [aKeyword[j].i],
2a268 20 70 2d 3e 6e 29 3d 3d 30 20 29 7b 0a 20 20 20   p->n)==0 ){.   
2a269 20 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 7c 3d       jointype |=
2a26a 20 61 4b 65 79 77 6f 72 64 5b 6a 5d 2e 63 6f 64   aKeyword[j].cod
2a26b 65 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  e;.        break
2a26c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2a26d 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 3d      testcase( j=
2a26e 3d 30 20 7c 7c 20 6a 3d 3d 31 20 7c 7c 20 6a 3d  =0 || j==1 || j=
2a26f 3d 32 20 7c 7c 20 6a 3d 3d 33 20 7c 7c 20 6a 3d  =2 || j==3 || j=
2a270 3d 34 20 7c 7c 20 6a 3d 3d 35 20 7c 7c 20 6a 3d  =4 || j==5 || j=
2a271 3d 36 20 29 3b 0a 20 20 20 20 69 66 28 20 6a 3e  =6 );.    if( j>
2a272 3d 41 72 72 61 79 53 69 7a 65 28 61 4b 65 79 77  =ArraySize(aKeyw
2a273 6f 72 64 29 20 29 7b 0a 20 20 20 20 20 20 6a 6f  ord) ){.      jo
2a274 69 6e 74 79 70 65 20 7c 3d 20 4a 54 5f 45 52 52  intype |= JT_ERR
2a275 4f 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  OR;.      break;
2a276 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
2a277 0a 20 20 20 20 20 28 6a 6f 69 6e 74 79 70 65 20  .     (jointype 
2a278 26 20 28 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f 4f  & (JT_INNER|JT_O
2a279 55 54 45 52 29 29 3d 3d 28 4a 54 5f 49 4e 4e 45  UTER))==(JT_INNE
2a27a 52 7c 4a 54 5f 4f 55 54 45 52 29 20 7c 7c 0a 20  R|JT_OUTER) ||. 
2a27b 20 20 20 20 28 6a 6f 69 6e 74 79 70 65 20 26 20      (jointype & 
2a27c 4a 54 5f 45 52 52 4f 52 29 21 3d 30 0a 20 20 29  JT_ERROR)!=0.  )
2a27d 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
2a27e 20 2a 7a 53 70 20 3d 20 22 20 22 3b 0a 20 20 20   *zSp = " ";.   
2a27f 20 61 73 73 65 72 74 28 20 70 42 21 3d 30 20 29   assert( pB!=0 )
2a280 3b 0a 20 20 20 20 69 66 28 20 70 43 3d 3d 30 20  ;.    if( pC==0 
2a281 29 7b 20 7a 53 70 2b 2b 3b 20 7d 0a 20 20 20 20  ){ zSp++; }.    
2a282 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2a283 70 50 61 72 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e  pParse, "unknown
2a284 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20   or unsupported 
2a285 6a 6f 69 6e 20 74 79 70 65 3a 20 22 0a 20 20 20  join type: ".   
2a286 20 20 20 20 22 25 54 20 25 54 25 73 25 54 22 2c      "%T %T%s%T",
2a287 20 70 41 2c 20 70 42 2c 20 7a 53 70 2c 20 70 43   pA, pB, zSp, pC
2a288 29 3b 0a 20 20 20 20 6a 6f 69 6e 74 79 70 65 20  );.    jointype 
2a289 3d 20 4a 54 5f 49 4e 4e 45 52 3b 0a 20 20 7d 65  = JT_INNER;.  }e
2a28a 6c 73 65 20 69 66 28 20 28 6a 6f 69 6e 74 79 70  lse if( (jointyp
2a28b 65 20 26 20 4a 54 5f 4f 55 54 45 52 29 21 3d 30  e & JT_OUTER)!=0
2a28c 20 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 6a   .         && (j
2a28d 6f 69 6e 74 79 70 65 20 26 20 28 4a 54 5f 4c 45  ointype & (JT_LE
2a28e 46 54 7c 4a 54 5f 52 49 47 48 54 29 29 21 3d 4a  FT|JT_RIGHT))!=J
2a28f 54 5f 4c 45 46 54 20 29 7b 0a 20 20 20 20 73 71  T_LEFT ){.    sq
2a290 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2a291 61 72 73 65 2c 20 0a 20 20 20 20 20 20 22 52 49  arse, .      "RI
2a292 47 48 54 20 61 6e 64 20 46 55 4c 4c 20 4f 55 54  GHT and FULL OUT
2a293 45 52 20 4a 4f 49 4e 73 20 61 72 65 20 6e 6f 74  ER JOINs are not
2a294 20 63 75 72 72 65 6e 74 6c 79 20 73 75 70 70 6f   currently suppo
2a295 72 74 65 64 22 29 3b 0a 20 20 20 20 6a 6f 69 6e  rted");.    join
2a296 74 79 70 65 20 3d 20 4a 54 5f 49 4e 4e 45 52 3b  type = JT_INNER;
2a297 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6a 6f  .  }.  return jo
2a298 69 6e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  intype;.}../*.**
2a299 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65   Return the inde
2a29a 78 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 6e  x of a column in
2a29b 20 61 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72   a table.  Retur
2a29c 6e 20 2d 31 20 69 66 20 74 68 65 20 63 6f 6c 75  n -1 if the colu
2a29d 6d 6e 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 6e  mn.** is not con
2a29e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 74 61  tained in the ta
2a29f 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ble..*/.static i
2a2a0 6e 74 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 54  nt columnIndex(T
2a2a1 61 62 6c 65 20 2a 70 54 61 62 2c 20 63 6f 6e 73  able *pTab, cons
2a2a2 74 20 63 68 61 72 20 2a 7a 43 6f 6c 29 7b 0a 20  t char *zCol){. 
2a2a3 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
2a2a4 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  0; i<pTab->nCol;
2a2a5 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73   i++){.    if( s
2a2a6 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 54  qlite3StrICmp(pT
2a2a7 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d  ab->aCol[i].zNam
2a2a8 65 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 20 72 65  e, zCol)==0 ) re
2a2a9 74 75 72 6e 20 69 3b 0a 20 20 7d 0a 20 20 72 65  turn i;.  }.  re
2a2aa 74 75 72 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn -1;.}../*.*
2a2ab 2a 20 43 72 65 61 74 65 20 61 6e 20 65 78 70 72  * Create an expr
2a2ac 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20  ession node for 
2a2ad 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20 77 69  an identifier wi
2a2ae 74 68 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 7a  th the name of z
2a2af 4e 61 6d 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  Name.*/.SQLITE_P
2a2b0 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
2a2b1 69 74 65 33 43 72 65 61 74 65 49 64 45 78 70 72  ite3CreateIdExpr
2a2b2 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2a2b3 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
2a2b4 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  e){.  return sql
2a2b5 69 74 65 33 45 78 70 72 28 70 50 61 72 73 65 2d  ite3Expr(pParse-
2a2b6 3e 64 62 2c 20 54 4b 5f 49 44 2c 20 7a 4e 61 6d  >db, TK_ID, zNam
2a2b7 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  e);.}../*.** Add
2a2b8 20 61 20 74 65 72 6d 20 74 6f 20 74 68 65 20 57   a term to the W
2a2b9 48 45 52 45 20 65 78 70 72 65 73 73 69 6f 6e 20  HERE expression 
2a2ba 69 6e 20 2a 70 70 45 78 70 72 20 74 68 61 74 20  in *ppExpr that 
2a2bb 72 65 71 75 69 72 65 73 20 74 68 65 0a 2a 2a 20  requires the.** 
2a2bc 7a 43 6f 6c 20 63 6f 6c 75 6d 6e 20 74 6f 20 62  zCol column to b
2a2bd 65 20 65 71 75 61 6c 20 69 6e 20 74 68 65 20 74  e equal in the t
2a2be 77 6f 20 74 61 62 6c 65 73 20 70 54 61 62 31 20  wo tables pTab1 
2a2bf 61 6e 64 20 70 54 61 62 32 2e 0a 2a 2f 0a 73 74  and pTab2..*/.st
2a2c0 61 74 69 63 20 76 6f 69 64 20 61 64 64 57 68 65  atic void addWhe
2a2c1 72 65 54 65 72 6d 28 0a 20 20 50 61 72 73 65 20  reTerm(.  Parse 
2a2c2 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2a2c3 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
2a2c4 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  ntext */.  const
2a2c5 20 63 68 61 72 20 2a 7a 43 6f 6c 2c 20 20 20 20   char *zCol,    
2a2c6 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
2a2c7 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63  he column */.  c
2a2c8 6f 6e 73 74 20 54 61 62 6c 65 20 2a 70 54 61 62  onst Table *pTab
2a2c9 31 2c 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74  1,      /* First
2a2ca 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73   table */.  cons
2a2cb 74 20 63 68 61 72 20 2a 7a 41 6c 69 61 73 31 2c  t char *zAlias1,
2a2cc 20 20 20 20 20 2f 2a 20 41 6c 69 61 73 20 66 6f       /* Alias fo
2a2cd 72 20 66 69 72 73 74 20 74 61 62 6c 65 2e 20 20  r first table.  
2a2ce 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20  May be NULL */. 
2a2cf 20 63 6f 6e 73 74 20 54 61 62 6c 65 20 2a 70 54   const Table *pT
2a2d0 61 62 32 2c 20 20 20 20 20 20 2f 2a 20 53 65 63  ab2,      /* Sec
2a2d1 6f 6e 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63  ond table */.  c
2a2d2 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 6c 69 61  onst char *zAlia
2a2d3 73 32 2c 20 20 20 20 20 2f 2a 20 41 6c 69 61 73  s2,     /* Alias
2a2d4 20 66 6f 72 20 73 65 63 6f 6e 64 20 74 61 62 6c   for second tabl
2a2d5 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20  e.  May be NULL 
2a2d6 2a 2f 0a 20 20 69 6e 74 20 69 52 69 67 68 74 4a  */.  int iRightJ
2a2d7 6f 69 6e 54 61 62 6c 65 2c 20 20 20 20 20 2f 2a  oinTable,     /*
2a2d8 20 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72   VDBE cursor for
2a2d9 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65   the right table
2a2da 20 2a 2f 0a 20 20 45 78 70 72 20 2a 2a 70 70 45   */.  Expr **ppE
2a2db 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f  xpr,           /
2a2dc 2a 20 41 64 64 20 74 68 65 20 65 71 75 61 6c 69  * Add the equali
2a2dd 74 79 20 74 65 72 6d 20 74 6f 20 74 68 69 73 20  ty term to this 
2a2de 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
2a2df 69 6e 74 20 69 73 4f 75 74 65 72 4a 6f 69 6e 20  int isOuterJoin 
2a2e0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
2a2e1 20 69 66 20 64 65 61 6c 69 6e 67 20 77 69 74 68   if dealing with
2a2e2 20 61 6e 20 4f 55 54 45 52 20 6a 6f 69 6e 20 2a   an OUTER join *
2a2e3 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 45 31  /.){.  Expr *pE1
2a2e4 61 2c 20 2a 70 45 31 62 2c 20 2a 70 45 31 63 3b  a, *pE1b, *pE1c;
2a2e5 0a 20 20 45 78 70 72 20 2a 70 45 32 61 2c 20 2a  .  Expr *pE2a, *
2a2e6 70 45 32 62 2c 20 2a 70 45 32 63 3b 0a 20 20 45  pE2b, *pE2c;.  E
2a2e7 78 70 72 20 2a 70 45 3b 0a 0a 20 20 70 45 31 61  xpr *pE;..  pE1a
2a2e8 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 65   = sqlite3Create
2a2e9 49 64 45 78 70 72 28 70 50 61 72 73 65 2c 20 7a  IdExpr(pParse, z
2a2ea 43 6f 6c 29 3b 0a 20 20 70 45 32 61 20 3d 20 73  Col);.  pE2a = s
2a2eb 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 45 78  qlite3CreateIdEx
2a2ec 70 72 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 29  pr(pParse, zCol)
2a2ed 3b 0a 20 20 69 66 28 20 7a 41 6c 69 61 73 31 3d  ;.  if( zAlias1=
2a2ee 3d 30 20 29 7b 0a 20 20 20 20 7a 41 6c 69 61 73  =0 ){.    zAlias
2a2ef 31 20 3d 20 70 54 61 62 31 2d 3e 7a 4e 61 6d 65  1 = pTab1->zName
2a2f0 3b 0a 20 20 7d 0a 20 20 70 45 31 62 20 3d 20 73  ;.  }.  pE1b = s
2a2f1 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 45 78  qlite3CreateIdEx
2a2f2 70 72 28 70 50 61 72 73 65 2c 20 7a 41 6c 69 61  pr(pParse, zAlia
2a2f3 73 31 29 3b 0a 20 20 69 66 28 20 7a 41 6c 69 61  s1);.  if( zAlia
2a2f4 73 32 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 41 6c  s2==0 ){.    zAl
2a2f5 69 61 73 32 20 3d 20 70 54 61 62 32 2d 3e 7a 4e  ias2 = pTab2->zN
2a2f6 61 6d 65 3b 0a 20 20 7d 0a 20 20 70 45 32 62 20  ame;.  }.  pE2b 
2a2f7 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 49  = sqlite3CreateI
2a2f8 64 45 78 70 72 28 70 50 61 72 73 65 2c 20 7a 41  dExpr(pParse, zA
2a2f9 6c 69 61 73 32 29 3b 0a 20 20 70 45 31 63 20 3d  lias2);.  pE1c =
2a2fa 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2a2fb 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 70 45  arse, TK_DOT, pE
2a2fc 31 62 2c 20 70 45 31 61 2c 20 30 29 3b 0a 20 20  1b, pE1a, 0);.  
2a2fd 70 45 32 63 20 3d 20 73 71 6c 69 74 65 33 50 45  pE2c = sqlite3PE
2a2fe 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44  xpr(pParse, TK_D
2a2ff 4f 54 2c 20 70 45 32 62 2c 20 70 45 32 61 2c 20  OT, pE2b, pE2a, 
2a300 30 29 3b 0a 20 20 70 45 20 3d 20 73 71 6c 69 74  0);.  pE = sqlit
2a301 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2a302 54 4b 5f 45 51 2c 20 70 45 31 63 2c 20 70 45 32  TK_EQ, pE1c, pE2
2a303 63 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 45 20  c, 0);.  if( pE 
2a304 26 26 20 69 73 4f 75 74 65 72 4a 6f 69 6e 20 29  && isOuterJoin )
2a305 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72 6f  {.    ExprSetPro
2a306 70 65 72 74 79 28 70 45 2c 20 45 50 5f 46 72 6f  perty(pE, EP_Fro
2a307 6d 4a 6f 69 6e 29 3b 0a 20 20 20 20 61 73 73 65  mJoin);.    asse
2a308 72 74 28 20 21 45 78 70 72 48 61 73 41 6e 79 50  rt( !ExprHasAnyP
2a309 72 6f 70 65 72 74 79 28 70 45 2c 20 45 50 5f 54  roperty(pE, EP_T
2a30a 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75  okenOnly|EP_Redu
2a30b 63 65 64 29 20 29 3b 0a 20 20 20 20 45 78 70 72  ced) );.    Expr
2a30c 53 65 74 49 72 72 65 64 75 63 69 62 6c 65 28 70  SetIrreducible(p
2a30d 45 29 3b 0a 20 20 20 20 70 45 2d 3e 69 52 69 67  E);.    pE->iRig
2a30e 68 74 4a 6f 69 6e 54 61 62 6c 65 20 3d 20 28 69  htJoinTable = (i
2a30f 31 36 29 69 52 69 67 68 74 4a 6f 69 6e 54 61 62  16)iRightJoinTab
2a310 6c 65 3b 0a 20 20 7d 0a 20 20 2a 70 70 45 78 70  le;.  }.  *ppExp
2a311 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  r = sqlite3ExprA
2a312 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 2a 70  nd(pParse->db,*p
2a313 70 45 78 70 72 2c 20 70 45 29 3b 0a 7d 0a 0a 2f  pExpr, pE);.}../
2a314 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 50 5f  *.** Set the EP_
2a315 46 72 6f 6d 4a 6f 69 6e 20 70 72 6f 70 65 72 74  FromJoin propert
2a316 79 20 6f 6e 20 61 6c 6c 20 74 65 72 6d 73 20 6f  y on all terms o
2a317 66 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 72  f the given expr
2a318 65 73 73 69 6f 6e 2e 0a 2a 2a 20 41 6e 64 20 73  ession..** And s
2a319 65 74 20 74 68 65 20 45 78 70 72 2e 69 52 69 67  et the Expr.iRig
2a31a 68 74 4a 6f 69 6e 54 61 62 6c 65 20 74 6f 20 69  htJoinTable to i
2a31b 54 61 62 6c 65 20 66 6f 72 20 65 76 65 72 79 20  Table for every 
2a31c 74 65 72 6d 20 69 6e 20 74 68 65 0a 2a 2a 20 65  term in the.** e
2a31d 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  xpression..**.**
2a31e 20 54 68 65 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e   The EP_FromJoin
2a31f 20 70 72 6f 70 65 72 74 79 20 69 73 20 75 73 65   property is use
2a320 64 20 6f 6e 20 74 65 72 6d 73 20 6f 66 20 61 6e  d on terms of an
2a321 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 74   expression to t
2a322 65 6c 6c 0a 2a 2a 20 74 68 65 20 4c 45 46 54 20  ell.** the LEFT 
2a323 4f 55 54 45 52 20 4a 4f 49 4e 20 70 72 6f 63 65  OUTER JOIN proce
2a324 73 73 69 6e 67 20 6c 6f 67 69 63 20 74 68 61 74  ssing logic that
2a325 20 74 68 69 73 20 74 65 72 6d 20 69 73 20 70 61   this term is pa
2a326 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 6a 6f 69  rt of the.** joi
2a327 6e 20 72 65 73 74 72 69 63 74 69 6f 6e 20 73 70  n restriction sp
2a328 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 4f  ecified in the O
2a329 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73  N or USING claus
2a32a 65 20 61 6e 64 20 6e 6f 74 20 61 20 70 61 72 74  e and not a part
2a32b 0a 2a 2a 20 6f 66 20 74 68 65 20 6d 6f 72 65 20  .** of the more 
2a32c 67 65 6e 65 72 61 6c 20 57 48 45 52 45 20 63 6c  general WHERE cl
2a32d 61 75 73 65 2e 20 20 54 68 65 73 65 20 74 65 72  ause.  These ter
2a32e 6d 73 20 61 72 65 20 6d 6f 76 65 64 20 6f 76 65  ms are moved ove
2a32f 72 20 74 6f 20 74 68 65 0a 2a 2a 20 57 48 45 52  r to the.** WHER
2a330 45 20 63 6c 61 75 73 65 20 64 75 72 69 6e 67 20  E clause during 
2a331 6a 6f 69 6e 20 70 72 6f 63 65 73 73 69 6e 67 20  join processing 
2a332 62 75 74 20 77 65 20 6e 65 65 64 20 74 6f 20 72  but we need to r
2a333 65 6d 65 6d 62 65 72 20 74 68 61 74 20 74 68 65  emember that the
2a334 79 0a 2a 2a 20 6f 72 69 67 69 6e 61 74 65 64 20  y.** originated 
2a335 69 6e 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49  in the ON or USI
2a336 4e 47 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a  NG clause..**.**
2a337 20 54 68 65 20 45 78 70 72 2e 69 52 69 67 68 74   The Expr.iRight
2a338 4a 6f 69 6e 54 61 62 6c 65 20 74 65 6c 6c 73 20  JoinTable tells 
2a339 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2a33a 20 70 72 6f 63 65 73 73 69 6e 67 20 74 68 61 74   processing that
2a33b 20 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73 69   the.** expressi
2a33c 6f 6e 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 61  on depends on ta
2a33d 62 6c 65 20 69 52 69 67 68 74 4a 6f 69 6e 54 61  ble iRightJoinTa
2a33e 62 6c 65 20 65 76 65 6e 20 69 66 20 74 68 61 74  ble even if that
2a33f 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 0a 2a 2a   table is not.**
2a340 20 65 78 70 6c 69 63 69 74 6c 79 20 6d 65 6e 74   explicitly ment
2a341 69 6f 6e 65 64 20 69 6e 20 74 68 65 20 65 78 70  ioned in the exp
2a342 72 65 73 73 69 6f 6e 2e 20 20 54 68 61 74 20 69  ression.  That i
2a343 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 6e 65  nformation is ne
2a344 65 64 65 64 0a 2a 2a 20 66 6f 72 20 63 61 73 65  eded.** for case
2a345 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a  s like this:.**.
2a346 2a 2a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46  **    SELECT * F
2a347 52 4f 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e  ROM t1 LEFT JOIN
2a348 20 74 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 62   t2 ON t1.a=t2.b
2a349 20 41 4e 44 20 74 31 2e 78 3d 35 0a 2a 2a 0a 2a   AND t1.x=5.**.*
2a34a 2a 20 54 68 65 20 77 68 65 72 65 20 63 6c 61 75  * The where clau
2a34b 73 65 20 6e 65 65 64 73 20 74 6f 20 64 65 66 65  se needs to defe
2a34c 72 20 74 68 65 20 68 61 6e 64 6c 69 6e 67 20 6f  r the handling o
2a34d 66 20 74 68 65 20 74 31 2e 78 3d 35 0a 2a 2a 20  f the t1.x=5.** 
2a34e 74 65 72 6d 20 75 6e 74 69 6c 20 61 66 74 65 72  term until after
2a34f 20 74 68 65 20 74 32 20 6c 6f 6f 70 20 6f 66 20   the t2 loop of 
2a350 74 68 65 20 6a 6f 69 6e 2e 20 20 49 6e 20 74 68  the join.  In th
2a351 61 74 20 77 61 79 2c 20 61 0a 2a 2a 20 4e 55 4c  at way, a.** NUL
2a352 4c 20 74 32 20 72 6f 77 20 77 69 6c 6c 20 62 65  L t2 row will be
2a353 20 69 6e 73 65 72 74 65 64 20 77 68 65 6e 65 76   inserted whenev
2a354 65 72 20 74 31 2e 78 21 3d 35 2e 20 20 49 66 20  er t1.x!=5.  If 
2a355 77 65 20 64 6f 20 6e 6f 74 0a 2a 2a 20 64 65 66  we do not.** def
2a356 65 72 20 74 68 65 20 68 61 6e 64 6c 69 6e 67 20  er the handling 
2a357 6f 66 20 74 31 2e 78 3d 35 2c 20 69 74 20 77 69  of t1.x=5, it wi
2a358 6c 6c 20 62 65 20 70 72 6f 63 65 73 73 65 64 20  ll be processed 
2a359 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20 61  immediately.** a
2a35a 66 74 65 72 20 74 68 65 20 74 31 20 6c 6f 6f 70  fter the t1 loop
2a35b 20 61 6e 64 20 72 6f 77 73 20 77 69 74 68 20 74   and rows with t
2a35c 31 2e 78 21 3d 35 20 77 69 6c 6c 20 6e 65 76 65  1.x!=5 will neve
2a35d 72 20 61 70 70 65 61 72 20 69 6e 0a 2a 2a 20 74  r appear in.** t
2a35e 68 65 20 6f 75 74 70 75 74 2c 20 77 68 69 63 68  he output, which
2a35f 20 69 73 20 69 6e 63 6f 72 72 65 63 74 2e 0a 2a   is incorrect..*
2a360 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65  /.static void se
2a361 74 4a 6f 69 6e 45 78 70 72 28 45 78 70 72 20 2a  tJoinExpr(Expr *
2a362 70 2c 20 69 6e 74 20 69 54 61 62 6c 65 29 7b 0a  p, int iTable){.
2a363 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20    while( p ){.  
2a364 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74    ExprSetPropert
2a365 79 28 70 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e  y(p, EP_FromJoin
2a366 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  );.    assert( !
2a367 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72  ExprHasAnyProper
2a368 74 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e  ty(p, EP_TokenOn
2a369 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29  ly|EP_Reduced) )
2a36a 3b 0a 20 20 20 20 45 78 70 72 53 65 74 49 72 72  ;.    ExprSetIrr
2a36b 65 64 75 63 69 62 6c 65 28 70 29 3b 0a 20 20 20  educible(p);.   
2a36c 20 70 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61   p->iRightJoinTa
2a36d 62 6c 65 20 3d 20 28 69 31 36 29 69 54 61 62 6c  ble = (i16)iTabl
2a36e 65 3b 0a 20 20 20 20 73 65 74 4a 6f 69 6e 45 78  e;.    setJoinEx
2a36f 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20 69 54 61  pr(p->pLeft, iTa
2a370 62 6c 65 29 3b 0a 20 20 20 20 70 20 3d 20 70 2d  ble);.    p = p-
2a371 3e 70 52 69 67 68 74 3b 0a 20 20 7d 20 0a 7d 0a  >pRight;.  } .}.
2a372 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
2a373 69 6e 65 20 70 72 6f 63 65 73 73 65 73 20 74 68  ine processes th
2a374 65 20 6a 6f 69 6e 20 69 6e 66 6f 72 6d 61 74 69  e join informati
2a375 6f 6e 20 66 6f 72 20 61 20 53 45 4c 45 43 54 20  on for a SELECT 
2a376 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 4f 4e  statement..** ON
2a377 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73   and USING claus
2a378 65 73 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64  es are converted
2a379 20 69 6e 74 6f 20 65 78 74 72 61 20 74 65 72 6d   into extra term
2a37a 73 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  s of the WHERE c
2a37b 6c 61 75 73 65 2e 0a 2a 2a 20 4e 41 54 55 52 41  lause..** NATURA
2a37c 4c 20 6a 6f 69 6e 73 20 61 6c 73 6f 20 63 72 65  L joins also cre
2a37d 61 74 65 20 65 78 74 72 61 20 57 48 45 52 45 20  ate extra WHERE 
2a37e 63 6c 61 75 73 65 20 74 65 72 6d 73 2e 0a 2a 2a  clause terms..**
2a37f 0a 2a 2a 20 54 68 65 20 74 65 72 6d 73 20 6f 66  .** The terms of
2a380 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61   a FROM clause a
2a381 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20  re contained in 
2a382 74 68 65 20 53 65 6c 65 63 74 2e 70 53 72 63 20  the Select.pSrc 
2a383 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68  structure..** Th
2a384 65 20 6c 65 66 74 20 6d 6f 73 74 20 74 61 62 6c  e left most tabl
2a385 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20 65  e is the first e
2a386 6e 74 72 79 20 69 6e 20 53 65 6c 65 63 74 2e 70  ntry in Select.p
2a387 53 72 63 2e 20 20 54 68 65 20 72 69 67 68 74 2d  Src.  The right-
2a388 6d 6f 73 74 0a 2a 2a 20 74 61 62 6c 65 20 69 73  most.** table is
2a389 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 2e   the last entry.
2a38a 20 20 54 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61    The join opera
2a38b 74 6f 72 20 69 73 20 68 65 6c 64 20 69 6e 20 74  tor is held in t
2a38c 68 65 20 65 6e 74 72 79 20 74 6f 0a 2a 2a 20 74  he entry to.** t
2a38d 68 65 20 6c 65 66 74 2e 20 20 54 68 75 73 20 65  he left.  Thus e
2a38e 6e 74 72 79 20 30 20 63 6f 6e 74 61 69 6e 73 20  ntry 0 contains 
2a38f 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f  the join operato
2a390 72 20 66 6f 72 20 74 68 65 20 6a 6f 69 6e 20 62  r for the join b
2a391 65 74 77 65 65 6e 0a 2a 2a 20 65 6e 74 72 69 65  etween.** entrie
2a392 73 20 30 20 61 6e 64 20 31 2e 20 20 41 6e 79 20  s 0 and 1.  Any 
2a393 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75  ON or USING clau
2a394 73 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ses associated w
2a395 69 74 68 20 74 68 65 20 6a 6f 69 6e 20 61 72 65  ith the join are
2a396 0a 2a 2a 20 61 6c 73 6f 20 61 74 74 61 63 68 65  .** also attache
2a397 64 20 74 6f 20 74 68 65 20 6c 65 66 74 20 65 6e  d to the left en
2a398 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  try..**.** This 
2a399 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
2a39a 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72  the number of er
2a39b 72 6f 72 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  rors encountered
2a39c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2a39d 73 71 6c 69 74 65 50 72 6f 63 65 73 73 4a 6f 69  sqliteProcessJoi
2a39e 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  n(Parse *pParse,
2a39f 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53   Select *p){.  S
2a3a0 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20  rcList *pSrc;   
2a3a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2a3a2 2a 20 41 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20  * All tables in 
2a3a3 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2a3a4 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20  */.  int i, j;  
2a3a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a3a6 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
2a3a7 6e 74 65 72 73 20 2a 2f 0a 20 20 73 74 72 75 63  nters */.  struc
2a3a8 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
2a3a9 70 4c 65 66 74 3b 20 20 20 20 20 2f 2a 20 4c 65  pLeft;     /* Le
2a3aa 66 74 20 74 61 62 6c 65 20 62 65 69 6e 67 20 6a  ft table being j
2a3ab 6f 69 6e 65 64 20 2a 2f 0a 20 20 73 74 72 75 63  oined */.  struc
2a3ac 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
2a3ad 70 52 69 67 68 74 3b 20 20 20 20 2f 2a 20 52 69  pRight;    /* Ri
2a3ae 67 68 74 20 74 61 62 6c 65 20 62 65 69 6e 67 20  ght table being 
2a3af 6a 6f 69 6e 65 64 20 2a 2f 0a 0a 20 20 70 53 72  joined */..  pSr
2a3b0 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 70  c = p->pSrc;.  p
2a3b1 4c 65 66 74 20 3d 20 26 70 53 72 63 2d 3e 61 5b  Left = &pSrc->a[
2a3b2 30 5d 3b 0a 20 20 70 52 69 67 68 74 20 3d 20 26  0];.  pRight = &
2a3b3 70 4c 65 66 74 5b 31 5d 3b 0a 20 20 66 6f 72 28  pLeft[1];.  for(
2a3b4 69 3d 30 3b 20 69 3c 70 53 72 63 2d 3e 6e 53 72  i=0; i<pSrc->nSr
2a3b5 63 2d 31 3b 20 69 2b 2b 2c 20 70 52 69 67 68 74  c-1; i++, pRight
2a3b6 2b 2b 2c 20 70 4c 65 66 74 2b 2b 29 7b 0a 20 20  ++, pLeft++){.  
2a3b7 20 20 54 61 62 6c 65 20 2a 70 4c 65 66 74 54 61    Table *pLeftTa
2a3b8 62 20 3d 20 70 4c 65 66 74 2d 3e 70 54 61 62 3b  b = pLeft->pTab;
2a3b9 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 52 69 67  .    Table *pRig
2a3ba 68 74 54 61 62 20 3d 20 70 52 69 67 68 74 2d 3e  htTab = pRight->
2a3bb 70 54 61 62 3b 0a 20 20 20 20 69 6e 74 20 69 73  pTab;.    int is
2a3bc 4f 75 74 65 72 3b 0a 0a 20 20 20 20 69 66 28 20  Outer;..    if( 
2a3bd 4e 45 56 45 52 28 70 4c 65 66 74 54 61 62 3d 3d  NEVER(pLeftTab==
2a3be 30 20 7c 7c 20 70 52 69 67 68 74 54 61 62 3d 3d  0 || pRightTab==
2a3bf 30 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  0) ) continue;. 
2a3c0 20 20 20 69 73 4f 75 74 65 72 20 3d 20 28 70 52     isOuter = (pR
2a3c1 69 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 65 20 26  ight->jointype &
2a3c2 20 4a 54 5f 4f 55 54 45 52 29 21 3d 30 3b 0a 0a   JT_OUTER)!=0;..
2a3c3 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 20      /* When the 
2a3c4 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f 72 64 20  NATURAL keyword 
2a3c5 69 73 20 70 72 65 73 65 6e 74 2c 20 61 64 64 20  is present, add 
2a3c6 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72  WHERE clause ter
2a3c7 6d 73 20 66 6f 72 0a 20 20 20 20 2a 2a 20 65 76  ms for.    ** ev
2a3c8 65 72 79 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20  ery column that 
2a3c9 74 68 65 20 74 77 6f 20 74 61 62 6c 65 73 20 68  the two tables h
2a3ca 61 76 65 20 69 6e 20 63 6f 6d 6d 6f 6e 2e 0a 20  ave in common.. 
2a3cb 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 52     */.    if( pR
2a3cc 69 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 65 20 26  ight->jointype &
2a3cd 20 4a 54 5f 4e 41 54 55 52 41 4c 20 29 7b 0a 20   JT_NATURAL ){. 
2a3ce 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d       if( pRight-
2a3cf 3e 70 4f 6e 20 7c 7c 20 70 52 69 67 68 74 2d 3e  >pOn || pRight->
2a3d0 70 55 73 69 6e 67 20 29 7b 0a 20 20 20 20 20 20  pUsing ){.      
2a3d1 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2a3d2 67 28 70 50 61 72 73 65 2c 20 22 61 20 4e 41 54  g(pParse, "a NAT
2a3d3 55 52 41 4c 20 6a 6f 69 6e 20 6d 61 79 20 6e 6f  URAL join may no
2a3d4 74 20 68 61 76 65 20 22 0a 20 20 20 20 20 20 20  t have ".       
2a3d5 20 20 20 20 22 61 6e 20 4f 4e 20 6f 72 20 55 53      "an ON or US
2a3d6 49 4e 47 20 63 6c 61 75 73 65 22 2c 20 30 29 3b  ING clause", 0);
2a3d7 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
2a3d8 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
2a3d9 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4c 65 66   for(j=0; j<pLef
2a3da 74 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29  tTab->nCol; j++)
2a3db 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  {.        char *
2a3dc 7a 4e 61 6d 65 20 3d 20 70 4c 65 66 74 54 61 62  zName = pLeftTab
2a3dd 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b  ->aCol[j].zName;
2a3de 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6f 6c  .        if( col
2a3df 75 6d 6e 49 6e 64 65 78 28 70 52 69 67 68 74 54  umnIndex(pRightT
2a3e0 61 62 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29 7b  ab, zName)>=0 ){
2a3e1 0a 20 20 20 20 20 20 20 20 20 20 61 64 64 57 68  .          addWh
2a3e2 65 72 65 54 65 72 6d 28 70 50 61 72 73 65 2c 20  ereTerm(pParse, 
2a3e3 7a 4e 61 6d 65 2c 20 70 4c 65 66 74 54 61 62 2c  zName, pLeftTab,
2a3e4 20 70 4c 65 66 74 2d 3e 7a 41 6c 69 61 73 2c 20   pLeft->zAlias, 
2a3e5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2a3e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
2a3e7 52 69 67 68 74 54 61 62 2c 20 70 52 69 67 68 74  RightTab, pRight
2a3e8 2d 3e 7a 41 6c 69 61 73 2c 0a 20 20 20 20 20 20  ->zAlias,.      
2a3e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a3ea 20 20 20 20 20 20 20 20 70 52 69 67 68 74 2d 3e          pRight->
2a3eb 69 43 75 72 73 6f 72 2c 20 26 70 2d 3e 70 57 68  iCursor, &p->pWh
2a3ec 65 72 65 2c 20 69 73 4f 75 74 65 72 29 3b 0a 20  ere, isOuter);. 
2a3ed 20 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 20           .      
2a3ee 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
2a3ef 7d 0a 0a 20 20 20 20 2f 2a 20 44 69 73 61 6c 6c  }..    /* Disall
2a3f0 6f 77 20 62 6f 74 68 20 4f 4e 20 61 6e 64 20 55  ow both ON and U
2a3f1 53 49 4e 47 20 63 6c 61 75 73 65 73 20 69 6e 20  SING clauses in 
2a3f2 74 68 65 20 73 61 6d 65 20 6a 6f 69 6e 0a 20 20  the same join.  
2a3f3 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 52 69    */.    if( pRi
2a3f4 67 68 74 2d 3e 70 4f 6e 20 26 26 20 70 52 69 67  ght->pOn && pRig
2a3f5 68 74 2d 3e 70 55 73 69 6e 67 20 29 7b 0a 20 20  ht->pUsing ){.  
2a3f6 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2a3f7 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e  Msg(pParse, "can
2a3f8 6e 6f 74 20 68 61 76 65 20 62 6f 74 68 20 4f 4e  not have both ON
2a3f9 20 61 6e 64 20 55 53 49 4e 47 20 22 0a 20 20 20   and USING ".   
2a3fa 20 20 20 20 20 22 63 6c 61 75 73 65 73 20 69 6e       "clauses in
2a3fb 20 74 68 65 20 73 61 6d 65 20 6a 6f 69 6e 22 29   the same join")
2a3fc 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ;.      return 1
2a3fd 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
2a3fe 41 64 64 20 74 68 65 20 4f 4e 20 63 6c 61 75 73  Add the ON claus
2a3ff 65 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  e to the end of 
2a400 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2a401 2c 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 0a 20  , connected by. 
2a402 20 20 20 2a 2a 20 61 6e 20 41 4e 44 20 6f 70 65     ** an AND ope
2a403 72 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20  rator..    */.  
2a404 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 70 4f    if( pRight->pO
2a405 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69  n ){.      if( i
2a406 73 4f 75 74 65 72 20 29 20 73 65 74 4a 6f 69 6e  sOuter ) setJoin
2a407 45 78 70 72 28 70 52 69 67 68 74 2d 3e 70 4f 6e  Expr(pRight->pOn
2a408 2c 20 70 52 69 67 68 74 2d 3e 69 43 75 72 73 6f  , pRight->iCurso
2a409 72 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 57 68  r);.      p->pWh
2a40a 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ere = sqlite3Exp
2a40b 72 41 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c  rAnd(pParse->db,
2a40c 20 70 2d 3e 70 57 68 65 72 65 2c 20 70 52 69 67   p->pWhere, pRig
2a40d 68 74 2d 3e 70 4f 6e 29 3b 0a 20 20 20 20 20 20  ht->pOn);.      
2a40e 70 52 69 67 68 74 2d 3e 70 4f 6e 20 3d 20 30 3b  pRight->pOn = 0;
2a40f 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
2a410 72 65 61 74 65 20 65 78 74 72 61 20 74 65 72 6d  reate extra term
2a411 73 20 6f 6e 20 74 68 65 20 57 48 45 52 45 20 63  s on the WHERE c
2a412 6c 61 75 73 65 20 66 6f 72 20 65 61 63 68 20 63  lause for each c
2a413 6f 6c 75 6d 6e 20 6e 61 6d 65 64 0a 20 20 20 20  olumn named.    
2a414 2a 2a 20 69 6e 20 74 68 65 20 55 53 49 4e 47 20  ** in the USING 
2a415 63 6c 61 75 73 65 2e 20 20 45 78 61 6d 70 6c 65  clause.  Example
2a416 3a 20 49 66 20 74 68 65 20 74 77 6f 20 74 61 62  : If the two tab
2a417 6c 65 73 20 74 6f 20 62 65 20 6a 6f 69 6e 65 64  les to be joined
2a418 20 61 72 65 20 0a 20 20 20 20 2a 2a 20 41 20 61   are .    ** A a
2a419 6e 64 20 42 20 61 6e 64 20 74 68 65 20 55 53 49  nd B and the USI
2a41a 4e 47 20 63 6c 61 75 73 65 20 6e 61 6d 65 73 20  NG clause names 
2a41b 58 2c 20 59 2c 20 61 6e 64 20 5a 2c 20 74 68 65  X, Y, and Z, the
2a41c 6e 20 61 64 64 20 74 68 69 73 0a 20 20 20 20 2a  n add this.    *
2a41d 2a 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 63  * to the WHERE c
2a41e 6c 61 75 73 65 3a 20 20 20 20 41 2e 58 3d 42 2e  lause:    A.X=B.
2a41f 58 20 41 4e 44 20 41 2e 59 3d 42 2e 59 20 41 4e  X AND A.Y=B.Y AN
2a420 44 20 41 2e 5a 3d 42 2e 5a 0a 20 20 20 20 2a 2a  D A.Z=B.Z.    **
2a421 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f 72   Report an error
2a422 20 69 66 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6d   if any column m
2a423 65 6e 74 69 6f 6e 65 64 20 69 6e 20 74 68 65 20  entioned in the 
2a424 55 53 49 4e 47 20 63 6c 61 75 73 65 20 69 73 0a  USING clause is.
2a425 20 20 20 20 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61      ** not conta
2a426 69 6e 65 64 20 69 6e 20 62 6f 74 68 20 74 61 62  ined in both tab
2a427 6c 65 73 20 74 6f 20 62 65 20 6a 6f 69 6e 65 64  les to be joined
2a428 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
2a429 20 70 52 69 67 68 74 2d 3e 70 55 73 69 6e 67 20   pRight->pUsing 
2a42a 29 7b 0a 20 20 20 20 20 20 49 64 4c 69 73 74 20  ){.      IdList 
2a42b 2a 70 4c 69 73 74 20 3d 20 70 52 69 67 68 74 2d  *pList = pRight-
2a42c 3e 70 55 73 69 6e 67 3b 0a 20 20 20 20 20 20 66  >pUsing;.      f
2a42d 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4c 69 73 74 2d  or(j=0; j<pList-
2a42e 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  >nId; j++){.    
2a42f 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20      char *zName 
2a430 3d 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 7a 4e  = pList->a[j].zN
2a431 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ame;.        if(
2a432 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 4c 65   columnIndex(pLe
2a433 66 74 54 61 62 2c 20 7a 4e 61 6d 65 29 3c 30 20  ftTab, zName)<0 
2a434 7c 7c 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70  || columnIndex(p
2a435 52 69 67 68 74 54 61 62 2c 20 7a 4e 61 6d 65 29  RightTab, zName)
2a436 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  <0 ){.          
2a437 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2a438 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20  pParse, "cannot 
2a439 6a 6f 69 6e 20 75 73 69 6e 67 20 63 6f 6c 75 6d  join using colum
2a43a 6e 20 25 73 20 2d 20 63 6f 6c 75 6d 6e 20 22 0a  n %s - column ".
2a43b 20 20 20 20 20 20 20 20 20 20 20 20 22 6e 6f 74              "not
2a43c 20 70 72 65 73 65 6e 74 20 69 6e 20 62 6f 74 68   present in both
2a43d 20 74 61 62 6c 65 73 22 2c 20 7a 4e 61 6d 65 29   tables", zName)
2a43e 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
2a43f 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a  rn 1;.        }.
2a440 20 20 20 20 20 20 20 20 61 64 64 57 68 65 72 65          addWhere
2a441 54 65 72 6d 28 70 50 61 72 73 65 2c 20 7a 4e 61  Term(pParse, zNa
2a442 6d 65 2c 20 70 4c 65 66 74 54 61 62 2c 20 70 4c  me, pLeftTab, pL
2a443 65 66 74 2d 3e 7a 41 6c 69 61 73 2c 20 0a 20 20  eft->zAlias, .  
2a444 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a445 20 20 20 20 20 20 20 20 20 20 70 52 69 67 68 74            pRight
2a446 54 61 62 2c 20 70 52 69 67 68 74 2d 3e 7a 41 6c  Tab, pRight->zAl
2a447 69 61 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ias,.           
2a448 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a449 20 70 52 69 67 68 74 2d 3e 69 43 75 72 73 6f 72   pRight->iCursor
2a44a 2c 20 26 70 2d 3e 70 57 68 65 72 65 2c 20 69 73  , &p->pWhere, is
2a44b 4f 75 74 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a  Outer);.      }.
2a44c 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
2a44d 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  rn 0;.}../*.** I
2a44e 6e 73 65 72 74 20 63 6f 64 65 20 69 6e 74 6f 20  nsert code into 
2a44f 22 76 22 20 74 68 61 74 20 77 69 6c 6c 20 70 75  "v" that will pu
2a450 73 68 20 74 68 65 20 72 65 63 6f 72 64 20 6f 6e  sh the record on
2a451 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 0a   the top of the.
2a452 2a 2a 20 73 74 61 63 6b 20 69 6e 74 6f 20 74 68  ** stack into th
2a453 65 20 73 6f 72 74 65 72 2e 0a 2a 2f 0a 73 74 61  e sorter..*/.sta
2a454 74 69 63 20 76 6f 69 64 20 70 75 73 68 4f 6e 74  tic void pushOnt
2a455 6f 53 6f 72 74 65 72 28 0a 20 20 50 61 72 73 65  oSorter(.  Parse
2a456 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2a457 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
2a458 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ext */.  ExprLis
2a459 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20  t *pOrderBy,    
2a45a 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  /* The ORDER BY 
2a45b 63 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65  clause */.  Sele
2a45c 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20  ct *pSelect,    
2a45d 20 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20     /* The whole 
2a45e 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2a45f 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74   */.  int regDat
2a460 61 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  a            /* 
2a461 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67  Register holding
2a462 20 64 61 74 61 20 74 6f 20 62 65 20 73 6f 72 74   data to be sort
2a463 65 64 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20  ed */.){.  Vdbe 
2a464 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
2a465 62 65 3b 0a 20 20 69 6e 74 20 6e 45 78 70 72 20  be;.  int nExpr 
2a466 3d 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  = pOrderBy->nExp
2a467 72 3b 0a 20 20 69 6e 74 20 72 65 67 42 61 73 65  r;.  int regBase
2a468 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2a469 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e  pRange(pParse, n
2a46a 45 78 70 72 2b 32 29 3b 0a 20 20 69 6e 74 20 72  Expr+2);.  int r
2a46b 65 67 52 65 63 6f 72 64 20 3d 20 73 71 6c 69 74  egRecord = sqlit
2a46c 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2a46d 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  rse);.  sqlite3E
2a46e 78 70 72 43 61 63 68 65 43 6c 65 61 72 28 70 50  xprCacheClear(pP
2a46f 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  arse);.  sqlite3
2a470 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74  ExprCodeExprList
2a471 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42  (pParse, pOrderB
2a472 79 2c 20 72 65 67 42 61 73 65 2c 20 30 29 3b 0a  y, regBase, 0);.
2a473 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2a474 4f 70 32 28 76 2c 20 4f 50 5f 53 65 71 75 65 6e  Op2(v, OP_Sequen
2a475 63 65 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45  ce, pOrderBy->iE
2a476 43 75 72 73 6f 72 2c 20 72 65 67 42 61 73 65 2b  Cursor, regBase+
2a477 6e 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65  nExpr);.  sqlite
2a478 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 50  3ExprCodeMove(pP
2a479 61 72 73 65 2c 20 72 65 67 44 61 74 61 2c 20 72  arse, regData, r
2a47a 65 67 42 61 73 65 2b 6e 45 78 70 72 2b 31 2c 20  egBase+nExpr+1, 
2a47b 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  1);.  sqlite3Vdb
2a47c 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61  eAddOp3(v, OP_Ma
2a47d 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 42 61 73  keRecord, regBas
2a47e 65 2c 20 6e 45 78 70 72 20 2b 20 32 2c 20 72 65  e, nExpr + 2, re
2a47f 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69  gRecord);.  sqli
2a480 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a481 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70   OP_IdxInsert, p
2a482 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f  OrderBy->iECurso
2a483 72 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20  r, regRecord);. 
2a484 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
2a485 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
2a486 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c  egRecord);.  sql
2a487 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
2a488 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67  ange(pParse, reg
2a489 42 61 73 65 2c 20 6e 45 78 70 72 2b 32 29 3b 0a  Base, nExpr+2);.
2a48a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 69    if( pSelect->i
2a48b 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 69 6e 74  Limit ){.    int
2a48c 20 61 64 64 72 31 2c 20 61 64 64 72 32 3b 0a 20   addr1, addr2;. 
2a48d 20 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b 0a 20     int iLimit;. 
2a48e 20 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e     if( pSelect->
2a48f 69 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20  iOffset ){.     
2a490 20 69 4c 69 6d 69 74 20 3d 20 70 53 65 6c 65 63   iLimit = pSelec
2a491 74 2d 3e 69 4f 66 66 73 65 74 2b 31 3b 0a 20 20  t->iOffset+1;.  
2a492 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
2a493 4c 69 6d 69 74 20 3d 20 70 53 65 6c 65 63 74 2d  Limit = pSelect-
2a494 3e 69 4c 69 6d 69 74 3b 0a 20 20 20 20 7d 0a 20  >iLimit;.    }. 
2a495 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74     addr1 = sqlit
2a496 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2a497 4f 50 5f 49 66 5a 65 72 6f 2c 20 69 4c 69 6d 69  OP_IfZero, iLimi
2a498 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  t);.    sqlite3V
2a499 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2a49a 41 64 64 49 6d 6d 2c 20 69 4c 69 6d 69 74 2c 20  AddImm, iLimit, 
2a49b 2d 31 29 3b 0a 20 20 20 20 61 64 64 72 32 20 3d  -1);.    addr2 =
2a49c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a49d 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a  p0(v, OP_Goto);.
2a49e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
2a49f 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 31  umpHere(v, addr1
2a4a0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2a4a1 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4c  beAddOp1(v, OP_L
2a4a2 61 73 74 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69  ast, pOrderBy->i
2a4a3 45 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73 71  ECursor);.    sq
2a4a4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2a4a5 76 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20 70 4f  v, OP_Delete, pO
2a4a6 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72  rderBy->iECursor
2a4a7 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2a4a8 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
2a4a9 64 72 32 29 3b 0a 20 20 20 20 70 53 65 6c 65 63  dr2);.    pSelec
2a4aa 74 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20  t->iLimit = 0;. 
2a4ab 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20   }.}../*.** Add 
2a4ac 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  code to implemen
2a4ad 74 20 74 68 65 20 4f 46 46 53 45 54 0a 2a 2f 0a  t the OFFSET.*/.
2a4ae 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65  static void code
2a4af 4f 66 66 73 65 74 28 0a 20 20 56 64 62 65 20 2a  Offset(.  Vdbe *
2a4b0 76 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47  v,          /* G
2a4b1 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74  enerate code int
2a4b2 6f 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 53  o this VM */.  S
2a4b3 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20  elect *p,       
2a4b4 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73   /* The SELECT s
2a4b5 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63  tatement being c
2a4b6 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43  oded */.  int iC
2a4b7 6f 6e 74 69 6e 75 65 20 20 20 20 20 2f 2a 20 4a  ontinue     /* J
2a4b8 75 6d 70 20 68 65 72 65 20 74 6f 20 73 6b 69 70  ump here to skip
2a4b9 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 65 63   the current rec
2a4ba 6f 72 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20  ord */.){.  if( 
2a4bb 70 2d 3e 69 4f 66 66 73 65 74 20 26 26 20 69 43  p->iOffset && iC
2a4bc 6f 6e 74 69 6e 75 65 21 3d 30 20 29 7b 0a 20 20  ontinue!=0 ){.  
2a4bd 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20    int addr;.    
2a4be 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a4bf 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20  2(v, OP_AddImm, 
2a4c0 70 2d 3e 69 4f 66 66 73 65 74 2c 20 2d 31 29 3b  p->iOffset, -1);
2a4c1 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69  .    addr = sqli
2a4c2 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2a4c3 20 4f 50 5f 49 66 4e 65 67 2c 20 70 2d 3e 69 4f   OP_IfNeg, p->iO
2a4c4 66 66 73 65 74 29 3b 0a 20 20 20 20 73 71 6c 69  ffset);.    sqli
2a4c5 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a4c6 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 43 6f   OP_Goto, 0, iCo
2a4c7 6e 74 69 6e 75 65 29 3b 0a 20 20 20 20 56 64 62  ntinue);.    Vdb
2a4c8 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 73 6b  eComment((v, "sk
2a4c9 69 70 20 4f 46 46 53 45 54 20 72 65 63 6f 72 64  ip OFFSET record
2a4ca 73 22 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  s"));.    sqlite
2a4cb 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2a4cc 20 61 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f   addr);.  }.}../
2a4cd 2a 0a 2a 2a 20 41 64 64 20 63 6f 64 65 20 74 68  *.** Add code th
2a4ce 61 74 20 77 69 6c 6c 20 63 68 65 63 6b 20 74 6f  at will check to
2a4cf 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 4e   make sure the N
2a4d0 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72 74   registers start
2a4d1 69 6e 67 20 61 74 20 69 4d 65 6d 0a 2a 2a 20 66  ing at iMem.** f
2a4d2 6f 72 6d 20 61 20 64 69 73 74 69 6e 63 74 20 65  orm a distinct e
2a4d3 6e 74 72 79 2e 20 20 69 54 61 62 20 69 73 20 61  ntry.  iTab is a
2a4d4 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 74   sorting index t
2a4d5 68 61 74 20 68 6f 6c 64 73 20 70 72 65 76 69 6f  hat holds previo
2a4d6 75 73 6c 79 0a 2a 2a 20 73 65 65 6e 20 63 6f 6d  usly.** seen com
2a4d7 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 74 68 65  binations of the
2a4d8 20 4e 20 76 61 6c 75 65 73 2e 20 20 41 20 6e 65   N values.  A ne
2a4d9 77 20 65 6e 74 72 79 20 69 73 20 6d 61 64 65 20  w entry is made 
2a4da 69 6e 20 69 54 61 62 0a 2a 2a 20 69 66 20 74 68  in iTab.** if th
2a4db 65 20 63 75 72 72 65 6e 74 20 4e 20 76 61 6c 75  e current N valu
2a4dc 65 73 20 61 72 65 20 6e 65 77 2e 0a 2a 2a 0a 2a  es are new..**.*
2a4dd 2a 20 41 20 6a 75 6d 70 20 74 6f 20 61 64 64 72  * A jump to addr
2a4de 52 65 70 65 61 74 20 69 73 20 6d 61 64 65 20 61  Repeat is made a
2a4df 6e 64 20 74 68 65 20 4e 2b 31 20 76 61 6c 75 65  nd the N+1 value
2a4e0 73 20 61 72 65 20 70 6f 70 70 65 64 20 66 72 6f  s are popped fro
2a4e1 6d 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b 20 69  m the.** stack i
2a4e2 66 20 74 68 65 20 74 6f 70 20 4e 20 65 6c 65 6d  f the top N elem
2a4e3 65 6e 74 73 20 61 72 65 20 6e 6f 74 20 64 69 73  ents are not dis
2a4e4 74 69 6e 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  tinct..*/.static
2a4e5 20 76 6f 69 64 20 63 6f 64 65 44 69 73 74 69 6e   void codeDistin
2a4e6 63 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ct(.  Parse *pPa
2a4e7 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73  rse,     /* Pars
2a4e8 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e  ing and code gen
2a4e9 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20  erating context 
2a4ea 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 2c 20 20  */.  int iTab,  
2a4eb 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 6f 72          /* A sor
2a4ec 74 69 6e 67 20 69 6e 64 65 78 20 75 73 65 64 20  ting index used 
2a4ed 74 6f 20 74 65 73 74 20 66 6f 72 20 64 69 73 74  to test for dist
2a4ee 69 6e 63 74 6e 65 73 73 20 2a 2f 0a 20 20 69 6e  inctness */.  in
2a4ef 74 20 61 64 64 72 52 65 70 65 61 74 2c 20 20 20  t addrRepeat,   
2a4f0 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65   /* Jump to here
2a4f1 20 69 66 20 6e 6f 74 20 64 69 73 74 69 6e 63 74   if not distinct
2a4f2 20 2a 2f 0a 20 20 69 6e 74 20 4e 2c 20 20 20 20   */.  int N,    
2a4f3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2a4f4 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 2a  er of elements *
2a4f5 2f 0a 20 20 69 6e 74 20 69 4d 65 6d 20 20 20 20  /.  int iMem    
2a4f6 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
2a4f7 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20  element */.){.  
2a4f8 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 72  Vdbe *v;.  int r
2a4f9 31 3b 0a 0a 20 20 76 20 3d 20 70 50 61 72 73 65  1;..  v = pParse
2a4fa 2d 3e 70 56 64 62 65 3b 0a 20 20 72 31 20 3d 20  ->pVdbe;.  r1 = 
2a4fb 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
2a4fc 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c  g(pParse);.  sql
2a4fd 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2a4fe 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
2a4ff 20 69 4d 65 6d 2c 20 4e 2c 20 72 31 29 3b 0a 20   iMem, N, r1);. 
2a500 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a501 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20  p3(v, OP_Found, 
2a502 69 54 61 62 2c 20 61 64 64 72 52 65 70 65 61 74  iTab, addrRepeat
2a503 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33  , r1);.  sqlite3
2a504 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2a505 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 54 61 62  _IdxInsert, iTab
2a506 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33  , r1);.  sqlite3
2a507 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
2a508 50 61 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f  Parse, r1);.}../
2a509 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e  *.** Generate an
2a50a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77   error message w
2a50b 68 65 6e 20 61 20 53 45 4c 45 43 54 20 69 73 20  hen a SELECT is 
2a50c 75 73 65 64 20 77 69 74 68 69 6e 20 61 20 73 75  used within a su
2a50d 62 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 28  bexpression.** (
2a50e 65 78 61 6d 70 6c 65 3a 20 20 22 61 20 49 4e 20  example:  "a IN 
2a50f 28 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74  (SELECT * FROM t
2a510 61 62 6c 65 29 22 29 20 62 75 74 20 69 74 20 68  able)") but it h
2a511 61 73 20 6d 6f 72 65 20 74 68 61 6e 20 31 20 72  as more than 1 r
2a512 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e  esult.** column.
2a513 20 20 57 65 20 64 6f 20 74 68 69 73 20 69 6e 20    We do this in 
2a514 61 20 73 75 62 72 6f 75 74 69 6e 65 20 62 65 63  a subroutine bec
2a515 61 75 73 65 20 74 68 65 20 65 72 72 6f 72 20 6f  ause the error o
2a516 63 63 75 72 73 20 69 6e 20 6d 75 6c 74 69 70 6c  ccurs in multipl
2a517 65 0a 2a 2a 20 70 6c 61 63 65 73 2e 0a 2a 2f 0a  e.** places..*/.
2a518 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b  static int check
2a519 46 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65  ForMultiColumnSe
2a51a 6c 65 63 74 45 72 72 6f 72 28 0a 20 20 50 61 72  lectError(.  Par
2a51b 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2a51c 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65    /* Parse conte
2a51d 78 74 2e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44  xt. */.  SelectD
2a51e 65 73 74 20 2a 70 44 65 73 74 2c 20 20 20 2f 2a  est *pDest,   /*
2a51f 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 6f 66 20   Destination of 
2a520 53 45 4c 45 43 54 20 72 65 73 75 6c 74 73 20 2a  SELECT results *
2a521 2f 0a 20 20 69 6e 74 20 6e 45 78 70 72 20 20 20  /.  int nExpr   
2a522 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2a523 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c  er of result col
2a524 75 6d 6e 73 20 72 65 74 75 72 6e 65 64 20 62 79  umns returned by
2a525 20 53 45 4c 45 43 54 20 2a 2f 0a 29 7b 0a 20 20   SELECT */.){.  
2a526 69 6e 74 20 65 44 65 73 74 20 3d 20 70 44 65 73  int eDest = pDes
2a527 74 2d 3e 65 44 65 73 74 3b 0a 20 20 69 66 28 20  t->eDest;.  if( 
2a528 6e 45 78 70 72 3e 31 20 26 26 20 28 65 44 65 73  nExpr>1 && (eDes
2a529 74 3d 3d 53 52 54 5f 4d 65 6d 20 7c 7c 20 65 44  t==SRT_Mem || eD
2a52a 65 73 74 3d 3d 53 52 54 5f 53 65 74 29 20 29 7b  est==SRT_Set) ){
2a52b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2a52c 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6f 6e  rMsg(pParse, "on
2a52d 6c 79 20 61 20 73 69 6e 67 6c 65 20 72 65 73 75  ly a single resu
2a52e 6c 74 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20 22  lt allowed for "
2a52f 0a 20 20 20 20 20 20 20 22 61 20 53 45 4c 45 43  .       "a SELEC
2a530 54 20 74 68 61 74 20 69 73 20 70 61 72 74 20 6f  T that is part o
2a531 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 22  f an expression"
2a532 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  );.    return 1;
2a533 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
2a534 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f  turn 0;.  }.}../
2a535 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2a536 65 20 67 65 6e 65 72 61 74 65 73 20 74 68 65 20  e generates the 
2a537 63 6f 64 65 20 66 6f 72 20 74 68 65 20 69 6e 73  code for the ins
2a538 69 64 65 20 6f 66 20 74 68 65 20 69 6e 6e 65 72  ide of the inner
2a539 20 6c 6f 6f 70 0a 2a 2a 20 6f 66 20 61 20 53 45   loop.** of a SE
2a53a 4c 45 43 54 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73  LECT..**.** If s
2a53b 72 63 54 61 62 20 61 6e 64 20 6e 43 6f 6c 75 6d  rcTab and nColum
2a53c 6e 20 61 72 65 20 62 6f 74 68 20 7a 65 72 6f 2c  n are both zero,
2a53d 20 74 68 65 6e 20 74 68 65 20 70 45 4c 69 73 74   then the pEList
2a53e 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20   expressions.** 
2a53f 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 69 6e  are evaluated in
2a540 20 6f 72 64 65 72 20 74 6f 20 67 65 74 20 74 68   order to get th
2a541 65 20 64 61 74 61 20 66 6f 72 20 74 68 69 73 20  e data for this 
2a542 72 6f 77 2e 20 20 49 66 20 6e 43 6f 6c 75 6d 6e  row.  If nColumn
2a543 3e 30 0a 2a 2a 20 74 68 65 6e 20 64 61 74 61 20  >0.** then data 
2a544 69 73 20 70 75 6c 6c 65 64 20 66 72 6f 6d 20 73  is pulled from s
2a545 72 63 54 61 62 20 61 6e 64 20 70 45 4c 69 73 74  rcTab and pEList
2a546 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 74 6f   is used only to
2a547 20 67 65 74 20 74 68 65 0a 2a 2a 20 64 61 74 61   get the.** data
2a548 74 79 70 65 73 20 66 6f 72 20 65 61 63 68 20 63  types for each c
2a549 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  olumn..*/.static
2a54a 20 76 6f 69 64 20 73 65 6c 65 63 74 49 6e 6e 65   void selectInne
2a54b 72 4c 6f 6f 70 28 0a 20 20 50 61 72 73 65 20 2a  rLoop(.  Parse *
2a54c 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2a54d 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
2a54e 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65  ontext */.  Sele
2a54f 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  ct *p,          
2a550 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 6c      /* The compl
2a551 65 74 65 20 73 65 6c 65 63 74 20 73 74 61 74 65  ete select state
2a552 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64  ment being coded
2a553 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
2a554 70 45 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a  pEList,       /*
2a555 20 4c 69 73 74 20 6f 66 20 76 61 6c 75 65 73 20   List of values 
2a556 62 65 69 6e 67 20 65 78 74 72 61 63 74 65 64 20  being extracted 
2a557 2a 2f 0a 20 20 69 6e 74 20 73 72 63 54 61 62 2c  */.  int srcTab,
2a558 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a559 50 75 6c 6c 20 64 61 74 61 20 66 72 6f 6d 20 74  Pull data from t
2a55a 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  his table */.  i
2a55b 6e 74 20 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20 20  nt nColumn,     
2a55c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
2a55d 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
2a55e 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20  he source table 
2a55f 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2a560 4f 72 64 65 72 42 79 2c 20 20 20 20 20 2f 2a 20  OrderBy,     /* 
2a561 49 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73 6f 72  If not NULL, sor
2a562 74 20 72 65 73 75 6c 74 73 20 75 73 69 6e 67 20  t results using 
2a563 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20 69 6e  this key */.  in
2a564 74 20 64 69 73 74 69 6e 63 74 2c 20 20 20 20 20  t distinct,     
2a565 20 20 20 20 20 20 2f 2a 20 49 66 20 3e 3d 30 2c        /* If >=0,
2a566 20 6d 61 6b 65 20 73 75 72 65 20 72 65 73 75 6c   make sure resul
2a567 74 73 20 61 72 65 20 64 69 73 74 69 6e 63 74 20  ts are distinct 
2a568 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20  */.  SelectDest 
2a569 2a 70 44 65 73 74 2c 20 20 20 20 20 20 2f 2a 20  *pDest,      /* 
2a56a 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20 6f  How to dispose o
2a56b 66 20 74 68 65 20 72 65 73 75 6c 74 73 20 2a 2f  f the results */
2a56c 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65  .  int iContinue
2a56d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75  ,          /* Ju
2a56e 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69  mp here to conti
2a56f 6e 75 65 20 77 69 74 68 20 6e 65 78 74 20 72 6f  nue with next ro
2a570 77 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65 61  w */.  int iBrea
2a571 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  k              /
2a572 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62  * Jump here to b
2a573 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20  reak out of the 
2a574 69 6e 6e 65 72 20 6c 6f 6f 70 20 2a 2f 0a 29 7b  inner loop */.){
2a575 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
2a576 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e  rse->pVdbe;.  in
2a577 74 20 69 3b 0a 20 20 69 6e 74 20 68 61 73 44 69  t i;.  int hasDi
2a578 73 74 69 6e 63 74 3b 20 20 20 20 20 20 20 20 2f  stinct;        /
2a579 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 44 49  * True if the DI
2a57a 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69  STINCT keyword i
2a57b 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69  s present */.  i
2a57c 6e 74 20 72 65 67 52 65 73 75 6c 74 3b 20 20 20  nt regResult;   
2a57d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
2a57e 61 72 74 20 6f 66 20 6d 65 6d 6f 72 79 20 68 6f  art of memory ho
2a57f 6c 64 69 6e 67 20 72 65 73 75 6c 74 20 73 65 74  lding result set
2a580 20 2a 2f 0a 20 20 69 6e 74 20 65 44 65 73 74 20   */.  int eDest 
2a581 3d 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3b 20  = pDest->eDest; 
2a582 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73 70    /* How to disp
2a583 6f 73 65 20 6f 66 20 72 65 73 75 6c 74 73 20 2a  ose of results *
2a584 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 20 3d 20  /.  int iParm = 
2a585 70 44 65 73 74 2d 3e 69 50 61 72 6d 3b 20 20 20  pDest->iParm;   
2a586 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e  /* First argumen
2a587 74 20 74 6f 20 64 69 73 70 6f 73 61 6c 20 6d 65  t to disposal me
2a588 74 68 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  thod */.  int nR
2a589 65 73 75 6c 74 43 6f 6c 3b 20 20 20 20 20 20 20  esultCol;       
2a58a 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2a58b 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  of result column
2a58c 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  s */..  assert( 
2a58d 76 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52  v );.  if( NEVER
2a58e 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b  (v==0) ) return;
2a58f 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73  .  assert( pELis
2a590 74 21 3d 30 20 29 3b 0a 20 20 68 61 73 44 69 73  t!=0 );.  hasDis
2a591 74 69 6e 63 74 20 3d 20 64 69 73 74 69 6e 63 74  tinct = distinct
2a592 3e 3d 30 3b 0a 20 20 69 66 28 20 70 4f 72 64 65  >=0;.  if( pOrde
2a593 72 42 79 3d 3d 30 20 26 26 20 21 68 61 73 44 69  rBy==0 && !hasDi
2a594 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 63 6f  stinct ){.    co
2a595 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2c 20 69  deOffset(v, p, i
2a596 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 7d 0a 0a  Continue);.  }..
2a597 20 20 2f 2a 20 50 75 6c 6c 20 74 68 65 20 72 65    /* Pull the re
2a598 71 75 65 73 74 65 64 20 63 6f 6c 75 6d 6e 73 2e  quested columns.
2a599 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 43 6f 6c  .  */.  if( nCol
2a59a 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20 6e 52 65  umn>0 ){.    nRe
2a59b 73 75 6c 74 43 6f 6c 20 3d 20 6e 43 6f 6c 75 6d  sultCol = nColum
2a59c 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  n;.  }else{.    
2a59d 6e 52 65 73 75 6c 74 43 6f 6c 20 3d 20 70 45 4c  nResultCol = pEL
2a59e 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 7d 0a  ist->nExpr;.  }.
2a59f 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69 4d 65    if( pDest->iMe
2a5a0 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44 65 73  m==0 ){.    pDes
2a5a1 74 2d 3e 69 4d 65 6d 20 3d 20 70 50 61 72 73 65  t->iMem = pParse
2a5a2 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 20 20 70 44  ->nMem+1;.    pD
2a5a3 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20 6e 52 65 73  est->nMem = nRes
2a5a4 75 6c 74 43 6f 6c 3b 0a 20 20 20 20 70 50 61 72  ultCol;.    pPar
2a5a5 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65 73  se->nMem += nRes
2a5a6 75 6c 74 43 6f 6c 3b 0a 20 20 7d 65 6c 73 65 7b  ultCol;.  }else{
2a5a7 20 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 44   .    assert( pD
2a5a8 65 73 74 2d 3e 6e 4d 65 6d 3d 3d 6e 52 65 73 75  est->nMem==nResu
2a5a9 6c 74 43 6f 6c 20 29 3b 0a 20 20 7d 0a 20 20 72  ltCol );.  }.  r
2a5aa 65 67 52 65 73 75 6c 74 20 3d 20 70 44 65 73 74  egResult = pDest
2a5ab 2d 3e 69 4d 65 6d 3b 0a 20 20 69 66 28 20 6e 43  ->iMem;.  if( nC
2a5ac 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20 66  olumn>0 ){.    f
2a5ad 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d  or(i=0; i<nColum
2a5ae 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  n; i++){.      s
2a5af 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2a5b0 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73  (v, OP_Column, s
2a5b1 72 63 54 61 62 2c 20 69 2c 20 72 65 67 52 65 73  rcTab, i, regRes
2a5b2 75 6c 74 2b 69 29 3b 0a 20 20 20 20 7d 0a 20 20  ult+i);.    }.  
2a5b3 7d 65 6c 73 65 20 69 66 28 20 65 44 65 73 74 21  }else if( eDest!
2a5b4 3d 53 52 54 5f 45 78 69 73 74 73 20 29 7b 0a 20  =SRT_Exists ){. 
2a5b5 20 20 20 2f 2a 20 49 66 20 74 68 65 20 64 65 73     /* If the des
2a5b6 74 69 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20 45  tination is an E
2a5b7 58 49 53 54 53 28 2e 2e 2e 29 20 65 78 70 72 65  XISTS(...) expre
2a5b8 73 73 69 6f 6e 2c 20 74 68 65 20 61 63 74 75 61  ssion, the actua
2a5b9 6c 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 20  l.    ** values 
2a5ba 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20  returned by the 
2a5bb 53 45 4c 45 43 54 20 61 72 65 20 6e 6f 74 20 72  SELECT are not r
2a5bc 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a  equired..    */.
2a5bd 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2a5be 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73 65  acheClear(pParse
2a5bf 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
2a5c0 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 70  prCodeExprList(p
2a5c1 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 72  Parse, pEList, r
2a5c2 65 67 52 65 73 75 6c 74 2c 20 65 44 65 73 74 3d  egResult, eDest=
2a5c3 3d 53 52 54 5f 4f 75 74 70 75 74 29 3b 0a 20 20  =SRT_Output);.  
2a5c4 7d 0a 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 52  }.  nColumn = nR
2a5c5 65 73 75 6c 74 43 6f 6c 3b 0a 0a 20 20 2f 2a 20  esultCol;..  /* 
2a5c6 49 66 20 74 68 65 20 44 49 53 54 49 4e 43 54 20  If the DISTINCT 
2a5c7 6b 65 79 77 6f 72 64 20 77 61 73 20 70 72 65 73  keyword was pres
2a5c8 65 6e 74 20 6f 6e 20 74 68 65 20 53 45 4c 45 43  ent on the SELEC
2a5c9 54 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a  T statement.  **
2a5ca 20 61 6e 64 20 74 68 69 73 20 72 6f 77 20 68 61   and this row ha
2a5cb 73 20 62 65 65 6e 20 73 65 65 6e 20 62 65 66 6f  s been seen befo
2a5cc 72 65 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20  re, then do not 
2a5cd 6d 61 6b 65 20 74 68 69 73 20 72 6f 77 0a 20 20  make this row.  
2a5ce 2a 2a 20 70 61 72 74 20 6f 66 20 74 68 65 20 72  ** part of the r
2a5cf 65 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  esult..  */.  if
2a5d0 28 20 68 61 73 44 69 73 74 69 6e 63 74 20 29 7b  ( hasDistinct ){
2a5d1 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c  .    assert( pEL
2a5d2 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 61 73  ist!=0 );.    as
2a5d3 73 65 72 74 28 20 70 45 4c 69 73 74 2d 3e 6e 45  sert( pEList->nE
2a5d4 78 70 72 3d 3d 6e 43 6f 6c 75 6d 6e 20 29 3b 0a  xpr==nColumn );.
2a5d5 20 20 20 20 63 6f 64 65 44 69 73 74 69 6e 63 74      codeDistinct
2a5d6 28 70 50 61 72 73 65 2c 20 64 69 73 74 69 6e 63  (pParse, distinc
2a5d7 74 2c 20 69 43 6f 6e 74 69 6e 75 65 2c 20 6e 43  t, iContinue, nC
2a5d8 6f 6c 75 6d 6e 2c 20 72 65 67 52 65 73 75 6c 74  olumn, regResult
2a5d9 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 72 64 65  );.    if( pOrde
2a5da 72 42 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  rBy==0 ){.      
2a5db 63 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2c  codeOffset(v, p,
2a5dc 20 69 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 20   iContinue);.   
2a5dd 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 63 68   }.  }..  if( ch
2a5de 65 63 6b 46 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d  eckForMultiColum
2a5df 6e 53 65 6c 65 63 74 45 72 72 6f 72 28 70 50 61  nSelectError(pPa
2a5e0 72 73 65 2c 20 70 44 65 73 74 2c 20 70 45 4c 69  rse, pDest, pELi
2a5e1 73 74 2d 3e 6e 45 78 70 72 29 20 29 7b 0a 20 20  st->nExpr) ){.  
2a5e2 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
2a5e3 20 73 77 69 74 63 68 28 20 65 44 65 73 74 20 29   switch( eDest )
2a5e4 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73  {.    /* In this
2a5e5 20 6d 6f 64 65 2c 20 77 72 69 74 65 20 65 61 63   mode, write eac
2a5e6 68 20 71 75 65 72 79 20 72 65 73 75 6c 74 20 74  h query result t
2a5e7 6f 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65  o the key of the
2a5e8 20 74 65 6d 70 6f 72 61 72 79 0a 20 20 20 20 2a   temporary.    *
2a5e9 2a 20 74 61 62 6c 65 20 69 50 61 72 6d 2e 0a 20  * table iParm.. 
2a5ea 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51     */.#ifndef SQ
2a5eb 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55  LITE_OMIT_COMPOU
2a5ec 4e 44 5f 53 45 4c 45 43 54 0a 20 20 20 20 63 61  ND_SELECT.    ca
2a5ed 73 65 20 53 52 54 5f 55 6e 69 6f 6e 3a 20 7b 0a  se SRT_Union: {.
2a5ee 20 20 20 20 20 20 69 6e 74 20 72 31 3b 0a 20 20        int r1;.  
2a5ef 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
2a5f0 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
2a5f1 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
2a5f2 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2a5f3 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65  P_MakeRecord, re
2a5f4 67 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e  gResult, nColumn
2a5f5 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  , r1);.      sql
2a5f6 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2a5f7 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20  , OP_IdxInsert, 
2a5f8 69 50 61 72 6d 2c 20 72 31 29 3b 0a 20 20 20 20  iParm, r1);.    
2a5f9 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2a5fa 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
2a5fb 72 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  r1);.      break
2a5fc 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
2a5fd 43 6f 6e 73 74 72 75 63 74 20 61 20 72 65 63 6f  Construct a reco
2a5fe 72 64 20 66 72 6f 6d 20 74 68 65 20 71 75 65 72  rd from the quer
2a5ff 79 20 72 65 73 75 6c 74 2c 20 62 75 74 20 69 6e  y result, but in
2a600 73 74 65 61 64 20 6f 66 0a 20 20 20 20 2a 2a 20  stead of.    ** 
2a601 73 61 76 69 6e 67 20 74 68 61 74 20 72 65 63 6f  saving that reco
2a602 72 64 2c 20 75 73 65 20 69 74 20 61 73 20 61 20  rd, use it as a 
2a603 6b 65 79 20 74 6f 20 64 65 6c 65 74 65 20 65 6c  key to delete el
2a604 65 6d 65 6e 74 73 20 66 72 6f 6d 0a 20 20 20 20  ements from.    
2a605 2a 2a 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  ** the temporary
2a606 20 74 61 62 6c 65 20 69 50 61 72 6d 2e 0a 20 20   table iParm..  
2a607 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52    */.    case SR
2a608 54 5f 45 78 63 65 70 74 3a 20 7b 0a 20 20 20 20  T_Except: {.    
2a609 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2a60a 4f 70 33 28 76 2c 20 4f 50 5f 49 64 78 44 65 6c  Op3(v, OP_IdxDel
2a60b 65 74 65 2c 20 69 50 61 72 6d 2c 20 72 65 67 52  ete, iParm, regR
2a60c 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b  esult, nColumn);
2a60d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2a60e 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
2a60f 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 72 65 73  /* Store the res
2a610 75 6c 74 20 61 73 20 64 61 74 61 20 75 73 69 6e  ult as data usin
2a611 67 20 61 20 75 6e 69 71 75 65 20 6b 65 79 2e 0a  g a unique key..
2a612 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
2a613 53 52 54 5f 54 61 62 6c 65 3a 0a 20 20 20 20 63  SRT_Table:.    c
2a614 61 73 65 20 53 52 54 5f 45 70 68 65 6d 54 61 62  ase SRT_EphemTab
2a615 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 31  : {.      int r1
2a616 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2a617 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
2a618 20 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44      testcase( eD
2a619 65 73 74 3d 3d 53 52 54 5f 54 61 62 6c 65 20 29  est==SRT_Table )
2a61a 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
2a61b 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68  ( eDest==SRT_Eph
2a61c 65 6d 54 61 62 20 29 3b 0a 20 20 20 20 20 20 73  emTab );.      s
2a61d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2a61e 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  (v, OP_MakeRecor
2a61f 64 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e 43  d, regResult, nC
2a620 6f 6c 75 6d 6e 2c 20 72 31 29 3b 0a 20 20 20 20  olumn, r1);.    
2a621 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29    if( pOrderBy )
2a622 7b 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e  {.        pushOn
2a623 74 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c  toSorter(pParse,
2a624 20 70 4f 72 64 65 72 42 79 2c 20 70 2c 20 72 31   pOrderBy, p, r1
2a625 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
2a626 20 20 20 20 20 20 20 20 69 6e 74 20 72 32 20 3d          int r2 =
2a627 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2a628 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
2a629 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a62a 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52  ddOp2(v, OP_NewR
2a62b 6f 77 69 64 2c 20 69 50 61 72 6d 2c 20 72 32 29  owid, iParm, r2)
2a62c 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2a62d 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2a62e 50 5f 49 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c  P_Insert, iParm,
2a62f 20 72 31 2c 20 72 32 29 3b 0a 20 20 20 20 20 20   r1, r2);.      
2a630 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
2a631 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f  ngeP5(v, OPFLAG_
2a632 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 20 20 20  APPEND);.       
2a633 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
2a634 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
2a635 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  2);.      }.    
2a636 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2a637 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
2a638 72 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  r1);.      break
2a639 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66  ;.    }..#ifndef
2a63a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
2a63b 51 55 45 52 59 0a 20 20 20 20 2f 2a 20 49 66 20  QUERY.    /* If 
2a63c 77 65 20 61 72 65 20 63 72 65 61 74 69 6e 67 20  we are creating 
2a63d 61 20 73 65 74 20 66 6f 72 20 61 6e 20 22 65 78  a set for an "ex
2a63e 70 72 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e  pr IN (SELECT ..
2a63f 2e 29 22 20 63 6f 6e 73 74 72 75 63 74 2c 0a 20  .)" construct,. 
2a640 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 72 65     ** then there
2a641 20 73 68 6f 75 6c 64 20 62 65 20 61 20 73 69 6e   should be a sin
2a642 67 6c 65 20 69 74 65 6d 20 6f 6e 20 74 68 65 20  gle item on the 
2a643 73 74 61 63 6b 2e 20 20 57 72 69 74 65 20 74 68  stack.  Write th
2a644 69 73 0a 20 20 20 20 2a 2a 20 69 74 65 6d 20 69  is.    ** item i
2a645 6e 74 6f 20 74 68 65 20 73 65 74 20 74 61 62 6c  nto the set tabl
2a646 65 20 77 69 74 68 20 62 6f 67 75 73 20 64 61 74  e with bogus dat
2a647 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  a..    */.    ca
2a648 73 65 20 53 52 54 5f 53 65 74 3a 20 7b 0a 20 20  se SRT_Set: {.  
2a649 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c      assert( nCol
2a64a 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20  umn==1 );.      
2a64b 70 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 73 71  p->affinity = sq
2a64c 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69  lite3CompareAffi
2a64d 6e 69 74 79 28 70 45 4c 69 73 74 2d 3e 61 5b 30  nity(pEList->a[0
2a64e 5d 2e 70 45 78 70 72 2c 20 70 44 65 73 74 2d 3e  ].pExpr, pDest->
2a64f 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 20 20 20  affinity);.     
2a650 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b   if( pOrderBy ){
2a651 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 74 20 66  .        /* At f
2a652 69 72 73 74 20 67 6c 61 6e 63 65 20 79 6f 75 20  irst glance you 
2a653 77 6f 75 6c 64 20 74 68 69 6e 6b 20 77 65 20 63  would think we c
2a654 6f 75 6c 64 20 6f 70 74 69 6d 69 7a 65 20 6f 75  ould optimize ou
2a655 74 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  t the.        **
2a656 20 4f 52 44 45 52 20 42 59 20 69 6e 20 74 68 69   ORDER BY in thi
2a657 73 20 63 61 73 65 20 73 69 6e 63 65 20 74 68 65  s case since the
2a658 20 6f 72 64 65 72 20 6f 66 20 65 6e 74 72 69 65   order of entrie
2a659 73 20 69 6e 20 74 68 65 20 73 65 74 0a 20 20 20  s in the set.   
2a65a 20 20 20 20 20 2a 2a 20 64 6f 65 73 20 6e 6f 74       ** does not
2a65b 20 6d 61 74 74 65 72 2e 20 20 42 75 74 20 74 68   matter.  But th
2a65c 65 72 65 20 6d 69 67 68 74 20 62 65 20 61 20 4c  ere might be a L
2a65d 49 4d 49 54 20 63 6c 61 75 73 65 2c 20 69 6e 20  IMIT clause, in 
2a65e 77 68 69 63 68 0a 20 20 20 20 20 20 20 20 2a 2a  which.        **
2a65f 20 63 61 73 65 20 74 68 65 20 6f 72 64 65 72 20   case the order 
2a660 64 6f 65 73 20 6d 61 74 74 65 72 20 2a 2f 0a 20  does matter */. 
2a661 20 20 20 20 20 20 20 70 75 73 68 4f 6e 74 6f 53         pushOntoS
2a662 6f 72 74 65 72 28 70 50 61 72 73 65 2c 20 70 4f  orter(pParse, pO
2a663 72 64 65 72 42 79 2c 20 70 2c 20 72 65 67 52 65  rderBy, p, regRe
2a664 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c  sult);.      }el
2a665 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  se{.        int 
2a666 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
2a667 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
2a668 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2a669 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2a66a 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 52  MakeRecord, regR
2a66b 65 73 75 6c 74 2c 20 31 2c 20 72 31 2c 20 26 70  esult, 1, r1, &p
2a66c 2d 3e 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a  ->affinity, 1);.
2a66d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2a66e 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79  xprCacheAffinity
2a66f 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  Change(pParse, r
2a670 65 67 52 65 73 75 6c 74 2c 20 31 29 3b 0a 20 20  egResult, 1);.  
2a671 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2a672 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64  eAddOp2(v, OP_Id
2a673 78 49 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c 20  xInsert, iParm, 
2a674 72 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  r1);.        sql
2a675 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
2a676 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a  eg(pParse, r1);.
2a677 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
2a678 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
2a679 2f 2a 20 49 66 20 61 6e 79 20 72 6f 77 20 65 78  /* If any row ex
2a67a 69 73 74 20 69 6e 20 74 68 65 20 72 65 73 75 6c  ist in the resul
2a67b 74 20 73 65 74 2c 20 72 65 63 6f 72 64 20 74 68  t set, record th
2a67c 61 74 20 66 61 63 74 20 61 6e 64 20 61 62 6f 72  at fact and abor
2a67d 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  t..    */.    ca
2a67e 73 65 20 53 52 54 5f 45 78 69 73 74 73 3a 20 7b  se SRT_Exists: {
2a67f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2a680 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2a681 6e 74 65 67 65 72 2c 20 31 2c 20 69 50 61 72 6d  nteger, 1, iParm
2a682 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  );.      /* The 
2a683 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 77 69 6c  LIMIT clause wil
2a684 6c 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20  l terminate the 
2a685 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20  loop for us */. 
2a686 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2a687 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69  }..    /* If thi
2a688 73 20 69 73 20 61 20 73 63 61 6c 61 72 20 73 65  s is a scalar se
2a689 6c 65 63 74 20 74 68 61 74 20 69 73 20 70 61 72  lect that is par
2a68a 74 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69  t of an expressi
2a68b 6f 6e 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20  on, then.    ** 
2a68c 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
2a68d 73 20 69 6e 20 74 68 65 20 61 70 70 72 6f 70 72  s in the appropr
2a68e 69 61 74 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  iate memory cell
2a68f 20 61 6e 64 20 62 72 65 61 6b 20 6f 75 74 0a 20   and break out. 
2a690 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 63 61     ** of the sca
2a691 6e 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20  n loop..    */. 
2a692 20 20 20 63 61 73 65 20 53 52 54 5f 4d 65 6d 3a     case SRT_Mem:
2a693 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
2a694 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20   nColumn==1 );. 
2a695 20 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42       if( pOrderB
2a696 79 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75 73  y ){.        pus
2a697 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70 50 61 72  hOntoSorter(pPar
2a698 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 2c  se, pOrderBy, p,
2a699 20 72 65 67 52 65 73 75 6c 74 29 3b 0a 20 20 20   regResult);.   
2a69a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2a69b 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
2a69c 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 72 65  eMove(pParse, re
2a69d 67 52 65 73 75 6c 74 2c 20 69 50 61 72 6d 2c 20  gResult, iParm, 
2a69e 31 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54  1);.        /* T
2a69f 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20  he LIMIT clause 
2a6a0 77 69 6c 6c 20 6a 75 6d 70 20 6f 75 74 20 6f 66  will jump out of
2a6a1 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73   the loop for us
2a6a2 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   */.      }.    
2a6a3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
2a6a4 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66  endif /* #ifndef
2a6a5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
2a6a6 51 55 45 52 59 20 2a 2f 0a 0a 20 20 20 20 2f 2a  QUERY */..    /*
2a6a7 20 53 65 6e 64 20 74 68 65 20 64 61 74 61 20 74   Send the data t
2a6a8 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66  o the callback f
2a6a9 75 6e 63 74 69 6f 6e 20 6f 72 20 74 6f 20 61 20  unction or to a 
2a6aa 73 75 62 72 6f 75 74 69 6e 65 2e 20 20 49 6e 20  subroutine.  In 
2a6ab 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 73 65 20  the.    ** case 
2a6ac 6f 66 20 61 20 73 75 62 72 6f 75 74 69 6e 65 2c  of a subroutine,
2a6ad 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65 20   the subroutine 
2a6ae 69 74 73 65 6c 66 20 69 73 20 72 65 73 70 6f 6e  itself is respon
2a6af 73 69 62 6c 65 20 66 6f 72 0a 20 20 20 20 2a 2a  sible for.    **
2a6b0 20 70 6f 70 70 69 6e 67 20 74 68 65 20 64 61 74   popping the dat
2a6b1 61 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63 6b  a from the stack
2a6b2 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
2a6b3 65 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 3a  e SRT_Coroutine:
2a6b4 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 4f 75  .    case SRT_Ou
2a6b5 74 70 75 74 3a 20 7b 0a 20 20 20 20 20 20 74 65  tput: {.      te
2a6b6 73 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d 53  stcase( eDest==S
2a6b7 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29 3b 0a  RT_Coroutine );.
2a6b8 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2a6b9 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75  eDest==SRT_Outpu
2a6ba 74 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  t );.      if( p
2a6bb 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20  OrderBy ){.     
2a6bc 20 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69     int r1 = sqli
2a6bd 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
2a6be 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73  arse);.        s
2a6bf 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2a6c0 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  (v, OP_MakeRecor
2a6c1 64 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e 43  d, regResult, nC
2a6c2 6f 6c 75 6d 6e 2c 20 72 31 29 3b 0a 20 20 20 20  olumn, r1);.    
2a6c3 20 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74      pushOntoSort
2a6c4 65 72 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65  er(pParse, pOrde
2a6c5 72 42 79 2c 20 70 2c 20 72 31 29 3b 0a 20 20 20  rBy, p, r1);.   
2a6c6 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
2a6c7 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
2a6c8 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 7d 65  e, r1);.      }e
2a6c9 6c 73 65 20 69 66 28 20 65 44 65 73 74 3d 3d 53  lse if( eDest==S
2a6ca 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29 7b 0a  RT_Coroutine ){.
2a6cb 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2a6cc 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2a6cd 59 69 65 6c 64 2c 20 70 44 65 73 74 2d 3e 69 50  Yield, pDest->iP
2a6ce 61 72 6d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  arm);.      }els
2a6cf 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
2a6d0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2a6d1 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 72 65  OP_ResultRow, re
2a6d2 67 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e  gResult, nColumn
2a6d3 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2a6d4 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e  e3ExprCacheAffin
2a6d5 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65  ityChange(pParse
2a6d6 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e 43 6f  , regResult, nCo
2a6d7 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20  lumn);.      }. 
2a6d8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2a6d9 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  }..#if !defined(
2a6da 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
2a6db 47 45 52 29 0a 20 20 20 20 2f 2a 20 44 69 73 63  GER).    /* Disc
2a6dc 61 72 64 20 74 68 65 20 72 65 73 75 6c 74 73 2e  ard the results.
2a6dd 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 66    This is used f
2a6de 6f 72 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  or SELECT statem
2a6df 65 6e 74 73 20 69 6e 73 69 64 65 0a 20 20 20 20  ents inside.    
2a6e0 2a 2a 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61  ** the body of a
2a6e1 20 54 52 49 47 47 45 52 2e 20 20 54 68 65 20 70   TRIGGER.  The p
2a6e2 75 72 70 6f 73 65 20 6f 66 20 73 75 63 68 20 73  urpose of such s
2a6e3 65 6c 65 63 74 73 20 69 73 20 74 6f 20 63 61 6c  elects is to cal
2a6e4 6c 0a 20 20 20 20 2a 2a 20 75 73 65 72 2d 64 65  l.    ** user-de
2a6e5 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20  fined functions 
2a6e6 74 68 61 74 20 68 61 76 65 20 73 69 64 65 20 65  that have side e
2a6e7 66 66 65 63 74 73 2e 20 20 57 65 20 64 6f 20 6e  ffects.  We do n
2a6e8 6f 74 20 63 61 72 65 0a 20 20 20 20 2a 2a 20 61  ot care.    ** a
2a6e9 62 6f 75 74 20 74 68 65 20 61 63 74 75 61 6c 20  bout the actual 
2a6ea 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 73  results of the s
2a6eb 65 6c 65 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  elect..    */.  
2a6ec 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
2a6ed 20 20 20 61 73 73 65 72 74 28 20 65 44 65 73 74     assert( eDest
2a6ee 3d 3d 53 52 54 5f 44 69 73 63 61 72 64 20 29 3b  ==SRT_Discard );
2a6ef 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2a6f0 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a    }.#endif.  }..
2a6f1 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65    /* Jump to the
2a6f2 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70   end of the loop
2a6f3 20 69 66 20 74 68 65 20 4c 49 4d 49 54 20 69 73   if the LIMIT is
2a6f4 20 72 65 61 63 68 65 64 2e 0a 20 20 2a 2f 0a 20   reached..  */. 
2a6f5 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 29   if( p->iLimit )
2a6f6 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f  {.    assert( pO
2a6f7 72 64 65 72 42 79 3d 3d 30 20 29 3b 20 20 2f 2a  rderBy==0 );  /*
2a6f8 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 20   If there is an 
2a6f9 4f 52 44 45 52 20 42 59 2c 20 74 68 65 20 63 61  ORDER BY, the ca
2a6fa 6c 6c 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20  ll to.          
2a6fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a6fc 20 20 2a 2a 20 70 75 73 68 4f 6e 74 6f 53 6f 72    ** pushOntoSor
2a6fd 74 65 72 28 29 20 77 6f 75 6c 64 20 68 61 76 65  ter() would have
2a6fe 20 63 6c 65 61 72 65 64 20 70 2d 3e 69 4c 69 6d   cleared p->iLim
2a6ff 69 74 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  it */.    sqlite
2a700 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a701 50 5f 41 64 64 49 6d 6d 2c 20 70 2d 3e 69 4c 69  P_AddImm, p->iLi
2a702 6d 69 74 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71  mit, -1);.    sq
2a703 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2a704 76 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 70 2d  v, OP_IfZero, p-
2a705 3e 69 4c 69 6d 69 74 2c 20 69 42 72 65 61 6b 29  >iLimit, iBreak)
2a706 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  ;.  }.}../*.** G
2a707 69 76 65 6e 20 61 6e 20 65 78 70 72 65 73 73 69  iven an expressi
2a708 6f 6e 20 6c 69 73 74 2c 20 67 65 6e 65 72 61 74  on list, generat
2a709 65 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  e a KeyInfo stru
2a70a 63 74 75 72 65 20 74 68 61 74 20 72 65 63 6f 72  cture that recor
2a70b 64 73 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 74  ds.** the collat
2a70c 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72  ing sequence for
2a70d 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e   each expression
2a70e 20 69 6e 20 74 68 61 74 20 65 78 70 72 65 73 73   in that express
2a70f 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  ion list..**.** 
2a710 49 66 20 74 68 65 20 45 78 70 72 4c 69 73 74 20  If the ExprList 
2a711 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f  is an ORDER BY o
2a712 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  r GROUP BY claus
2a713 65 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  e then the resul
2a714 74 69 6e 67 0a 2a 2a 20 4b 65 79 49 6e 66 6f 20  ting.** KeyInfo 
2a715 73 74 72 75 63 74 75 72 65 20 69 73 20 61 70 70  structure is app
2a716 72 6f 70 72 69 61 74 65 20 66 6f 72 20 69 6e 69  ropriate for ini
2a717 74 69 61 6c 69 7a 69 6e 67 20 61 20 76 69 72 74  tializing a virt
2a718 75 61 6c 20 69 6e 64 65 78 20 74 6f 0a 2a 2a 20  ual index to.** 
2a719 69 6d 70 6c 65 6d 65 6e 74 20 74 68 61 74 20 63  implement that c
2a71a 6c 61 75 73 65 2e 20 20 49 66 20 74 68 65 20 45  lause.  If the E
2a71b 78 70 72 4c 69 73 74 20 69 73 20 74 68 65 20 72  xprList is the r
2a71c 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53  esult set of a S
2a71d 45 4c 45 43 54 0a 2a 2a 20 74 68 65 6e 20 74 68  ELECT.** then th
2a71e 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  e KeyInfo struct
2a71f 75 72 65 20 69 73 20 61 70 70 72 6f 70 72 69 61  ure is appropria
2a720 74 65 20 66 6f 72 20 69 6e 69 74 69 61 6c 69 7a  te for initializ
2a721 69 6e 67 20 61 20 76 69 72 74 75 61 6c 0a 2a 2a  ing a virtual.**
2a722 20 69 6e 64 65 78 20 74 6f 20 69 6d 70 6c 65 6d   index to implem
2a723 65 6e 74 20 61 20 44 49 53 54 49 4e 43 54 20 74  ent a DISTINCT t
2a724 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65  est..**.** Space
2a725 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 4b 65 79   to hold the Key
2a726 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69  Info structure i
2a727 73 20 6f 62 74 61 69 6e 20 66 72 6f 6d 20 6d 61  s obtain from ma
2a728 6c 6c 6f 63 2e 20 20 54 68 65 20 63 61 6c 6c 69  lloc.  The calli
2a729 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69  ng.** function i
2a72a 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f  s responsible fo
2a72b 72 20 73 65 65 69 6e 67 20 74 68 61 74 20 74 68  r seeing that th
2a72c 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
2a72d 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 66 72  eventually.** fr
2a72e 65 65 64 2e 20 20 41 64 64 20 74 68 65 20 4b 65  eed.  Add the Ke
2a72f 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
2a730 74 6f 20 74 68 65 20 50 34 20 66 69 65 6c 64 20  to the P4 field 
2a731 6f 66 20 61 6e 20 6f 70 63 6f 64 65 20 75 73 69  of an opcode usi
2a732 6e 67 0a 2a 2a 20 50 34 5f 4b 45 59 49 4e 46 4f  ng.** P4_KEYINFO
2a733 5f 48 41 4e 44 4f 46 46 20 69 73 20 74 68 65 20  _HANDOFF is the 
2a734 75 73 75 61 6c 20 77 61 79 20 6f 66 20 64 65 61  usual way of dea
2a735 6c 69 6e 67 20 77 69 74 68 20 74 68 69 73 2e 0a  ling with this..
2a736 2a 2f 0a 73 74 61 74 69 63 20 4b 65 79 49 6e 66  */.static KeyInf
2a737 6f 20 2a 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78  o *keyInfoFromEx
2a738 70 72 4c 69 73 74 28 50 61 72 73 65 20 2a 70 50  prList(Parse *pP
2a739 61 72 73 65 2c 20 45 78 70 72 4c 69 73 74 20 2a  arse, ExprList *
2a73a 70 4c 69 73 74 29 7b 0a 20 20 73 71 6c 69 74 65  pList){.  sqlite
2a73b 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2a73c 64 62 3b 0a 20 20 69 6e 74 20 6e 45 78 70 72 3b  db;.  int nExpr;
2a73d 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 49 6e 66  .  KeyInfo *pInf
2a73e 6f 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72  o;.  struct Expr
2a73f 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
2a740 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 6e 45  ;.  int i;..  nE
2a741 78 70 72 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78  xpr = pList->nEx
2a742 70 72 3b 0a 20 20 70 49 6e 66 6f 20 3d 20 73 71  pr;.  pInfo = sq
2a743 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
2a744 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 49  o(db, sizeof(*pI
2a745 6e 66 6f 29 20 2b 20 6e 45 78 70 72 2a 28 73 69  nfo) + nExpr*(si
2a746 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 2b 31  zeof(CollSeq*)+1
2a747 29 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 66 6f  ) );.  if( pInfo
2a748 20 29 7b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 61   ){.    pInfo->a
2a749 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 2a  SortOrder = (u8*
2a74a 29 26 70 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e  )&pInfo->aColl[n
2a74b 45 78 70 72 5d 3b 0a 20 20 20 20 70 49 6e 66 6f  Expr];.    pInfo
2a74c 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29  ->nField = (u16)
2a74d 6e 45 78 70 72 3b 0a 20 20 20 20 70 49 6e 66 6f  nExpr;.    pInfo
2a74e 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b  ->enc = ENC(db);
2a74f 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 64 62 20 3d  .    pInfo->db =
2a750 20 64 62 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30   db;.    for(i=0
2a751 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  , pItem=pList->a
2a752 3b 20 69 3c 6e 45 78 70 72 3b 20 69 2b 2b 2c 20  ; i<nExpr; i++, 
2a753 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
2a754 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a  CollSeq *pColl;.
2a755 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71        pColl = sq
2a756 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
2a757 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e  (pParse, pItem->
2a758 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 69 66  pExpr);.      if
2a759 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  ( !pColl ){.    
2a75a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e      pColl = db->
2a75b 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20 20  pDfltColl;.     
2a75c 20 7d 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e   }.      pInfo->
2a75d 61 43 6f 6c 6c 5b 69 5d 20 3d 20 70 43 6f 6c 6c  aColl[i] = pColl
2a75e 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61  ;.      pInfo->a
2a75f 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 70  SortOrder[i] = p
2a760 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b  Item->sortOrder;
2a761 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
2a762 75 72 6e 20 70 49 6e 66 6f 3b 0a 7d 0a 0a 0a 2f  urn pInfo;.}.../
2a763 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 6e 65  *.** If the inne
2a764 72 20 6c 6f 6f 70 20 77 61 73 20 67 65 6e 65 72  r loop was gener
2a765 61 74 65 64 20 75 73 69 6e 67 20 61 20 6e 6f 6e  ated using a non
2a766 2d 6e 75 6c 6c 20 70 4f 72 64 65 72 42 79 20 61  -null pOrderBy a
2a767 72 67 75 6d 65 6e 74 2c 0a 2a 2a 20 74 68 65 6e  rgument,.** then
2a768 20 74 68 65 20 72 65 73 75 6c 74 73 20 77 65 72   the results wer
2a769 65 20 70 6c 61 63 65 64 20 69 6e 20 61 20 73 6f  e placed in a so
2a76a 72 74 65 72 2e 20 20 41 66 74 65 72 20 74 68 65  rter.  After the
2a76b 20 6c 6f 6f 70 20 69 73 20 74 65 72 6d 69 6e 61   loop is termina
2a76c 74 65 64 0a 2a 2a 20 77 65 20 6e 65 65 64 20 74  ted.** we need t
2a76d 6f 20 72 75 6e 20 74 68 65 20 73 6f 72 74 65 72  o run the sorter
2a76e 20 61 6e 64 20 6f 75 74 70 75 74 20 74 68 65 20   and output the 
2a76f 72 65 73 75 6c 74 73 2e 20 20 54 68 65 20 66 6f  results.  The fo
2a770 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 72 6f 75 74 69  llowing.** routi
2a771 6e 65 20 67 65 6e 65 72 61 74 65 73 20 74 68 65  ne generates the
2a772 20 63 6f 64 65 20 6e 65 65 64 65 64 20 74 6f 20   code needed to 
2a773 64 6f 20 74 68 61 74 2e 0a 2a 2f 0a 73 74 61 74  do that..*/.stat
2a774 69 63 20 76 6f 69 64 20 67 65 6e 65 72 61 74 65  ic void generate
2a775 53 6f 72 74 54 61 69 6c 28 0a 20 20 50 61 72 73  SortTail(.  Pars
2a776 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a  e *pParse,    /*
2a777 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
2a778 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c   */.  Select *p,
2a779 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53          /* The S
2a77a 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
2a77b 2a 2f 0a 20 20 56 64 62 65 20 2a 76 2c 20 20 20  */.  Vdbe *v,   
2a77c 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61         /* Genera
2a77d 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69  te code into thi
2a77e 73 20 56 44 42 45 20 2a 2f 0a 20 20 69 6e 74 20  s VDBE */.  int 
2a77f 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 2f 2a  nColumn,      /*
2a780 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
2a781 6e 73 20 6f 66 20 64 61 74 61 20 2a 2f 0a 20 20  ns of data */.  
2a782 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73  SelectDest *pDes
2a783 74 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 73  t /* Write the s
2a784 6f 72 74 65 64 20 72 65 73 75 6c 74 73 20 68 65  orted results he
2a785 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61  re */.){.  int a
2a786 64 64 72 42 72 65 61 6b 20 3d 20 73 71 6c 69 74  ddrBreak = sqlit
2a787 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
2a788 76 29 3b 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20  v);     /* Jump 
2a789 68 65 72 65 20 74 6f 20 65 78 69 74 20 6c 6f 6f  here to exit loo
2a78a 70 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43  p */.  int addrC
2a78b 6f 6e 74 69 6e 75 65 20 3d 20 73 71 6c 69 74 65  ontinue = sqlite
2a78c 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
2a78d 29 3b 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65  );  /* Jump here
2a78e 20 66 6f 72 20 6e 65 78 74 20 63 79 63 6c 65 20   for next cycle 
2a78f 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20  */.  int addr;. 
2a790 20 69 6e 74 20 69 54 61 62 3b 0a 20 20 69 6e 74   int iTab;.  int
2a791 20 70 73 65 75 64 6f 54 61 62 20 3d 20 30 3b 0a   pseudoTab = 0;.
2a792 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64    ExprList *pOrd
2a793 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72  erBy = p->pOrder
2a794 42 79 3b 0a 0a 20 20 69 6e 74 20 65 44 65 73 74  By;..  int eDest
2a795 20 3d 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3b   = pDest->eDest;
2a796 0a 20 20 69 6e 74 20 69 50 61 72 6d 20 3d 20 70  .  int iParm = p
2a797 44 65 73 74 2d 3e 69 50 61 72 6d 3b 0a 0a 20 20  Dest->iParm;..  
2a798 69 6e 74 20 72 65 67 52 6f 77 3b 0a 20 20 69 6e  int regRow;.  in
2a799 74 20 72 65 67 52 6f 77 69 64 3b 0a 0a 20 20 69  t regRowid;..  i
2a79a 54 61 62 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e  Tab = pOrderBy->
2a79b 69 45 43 75 72 73 6f 72 3b 0a 20 20 72 65 67 52  iECursor;.  regR
2a79c 6f 77 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  ow = sqlite3GetT
2a79d 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
2a79e 20 20 69 66 28 20 65 44 65 73 74 3d 3d 53 52 54    if( eDest==SRT
2a79f 5f 4f 75 74 70 75 74 20 7c 7c 20 65 44 65 73 74  _Output || eDest
2a7a0 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20  ==SRT_Coroutine 
2a7a1 29 7b 0a 20 20 20 20 70 73 65 75 64 6f 54 61 62  ){.    pseudoTab
2a7a2 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
2a7a3 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  +;.    sqlite3Vd
2a7a4 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f  beAddOp3(v, OP_O
2a7a5 70 65 6e 50 73 65 75 64 6f 2c 20 70 73 65 75 64  penPseudo, pseud
2a7a6 6f 54 61 62 2c 20 72 65 67 52 6f 77 2c 20 6e 43  oTab, regRow, nC
2a7a7 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 72 65 67 52  olumn);.    regR
2a7a8 6f 77 69 64 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  owid = 0;.  }els
2a7a9 65 7b 0a 20 20 20 20 72 65 67 52 6f 77 69 64 20  e{.    regRowid 
2a7aa 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
2a7ab 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 7d  Reg(pParse);.  }
2a7ac 0a 20 20 61 64 64 72 20 3d 20 31 20 2b 20 73 71  .  addr = 1 + sq
2a7ad 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2a7ae 76 2c 20 4f 50 5f 53 6f 72 74 2c 20 69 54 61 62  v, OP_Sort, iTab
2a7af 2c 20 61 64 64 72 42 72 65 61 6b 29 3b 0a 20 20  , addrBreak);.  
2a7b0 63 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2c  codeOffset(v, p,
2a7b1 20 61 64 64 72 43 6f 6e 74 69 6e 75 65 29 3b 0a   addrContinue);.
2a7b2 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2a7b3 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
2a7b4 2c 20 69 54 61 62 2c 20 70 4f 72 64 65 72 42 79  , iTab, pOrderBy
2a7b5 2d 3e 6e 45 78 70 72 20 2b 20 31 2c 20 72 65 67  ->nExpr + 1, reg
2a7b6 52 6f 77 29 3b 0a 20 20 73 77 69 74 63 68 28 20  Row);.  switch( 
2a7b7 65 44 65 73 74 20 29 7b 0a 20 20 20 20 63 61 73  eDest ){.    cas
2a7b8 65 20 53 52 54 5f 54 61 62 6c 65 3a 0a 20 20 20  e SRT_Table:.   
2a7b9 20 63 61 73 65 20 53 52 54 5f 45 70 68 65 6d 54   case SRT_EphemT
2a7ba 61 62 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  ab: {.      test
2a7bb 63 61 73 65 28 20 65 44 65 73 74 3d 3d 53 52 54  case( eDest==SRT
2a7bc 5f 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 20 20  _Table );.      
2a7bd 74 65 73 74 63 61 73 65 28 20 65 44 65 73 74 3d  testcase( eDest=
2a7be 3d 53 52 54 5f 45 70 68 65 6d 54 61 62 20 29 3b  =SRT_EphemTab );
2a7bf 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2a7c0 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
2a7c1 65 77 52 6f 77 69 64 2c 20 69 50 61 72 6d 2c 20  ewRowid, iParm, 
2a7c2 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  regRowid);.     
2a7c3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a7c4 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c  p3(v, OP_Insert,
2a7c5 20 69 50 61 72 6d 2c 20 72 65 67 52 6f 77 2c 20   iParm, regRow, 
2a7c6 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  regRowid);.     
2a7c7 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
2a7c8 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41  geP5(v, OPFLAG_A
2a7c9 50 50 45 4e 44 29 3b 0a 20 20 20 20 20 20 62 72  PPEND);.      br
2a7ca 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  eak;.    }.#ifnd
2a7cb 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
2a7cc 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73 65  UBQUERY.    case
2a7cd 20 53 52 54 5f 53 65 74 3a 20 7b 0a 20 20 20 20   SRT_Set: {.    
2a7ce 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d    assert( nColum
2a7cf 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71  n==1 );.      sq
2a7d0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2a7d1 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
2a7d2 2c 20 72 65 67 52 6f 77 2c 20 31 2c 20 72 65 67  , regRow, 1, reg
2a7d3 52 6f 77 69 64 2c 20 26 70 2d 3e 61 66 66 69 6e  Rowid, &p->affin
2a7d4 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20 73  ity, 1);.      s
2a7d5 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
2a7d6 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
2a7d7 61 72 73 65 2c 20 72 65 67 52 6f 77 2c 20 31 29  arse, regRow, 1)
2a7d8 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2a7d9 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2a7da 49 64 78 49 6e 73 65 72 74 2c 20 69 50 61 72 6d  IdxInsert, iParm
2a7db 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
2a7dc 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2a7dd 20 20 20 20 63 61 73 65 20 53 52 54 5f 4d 65 6d      case SRT_Mem
2a7de 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
2a7df 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a  ( nColumn==1 );.
2a7e0 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2a7e1 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72 73 65  rCodeMove(pParse
2a7e2 2c 20 72 65 67 52 6f 77 2c 20 69 50 61 72 6d 2c  , regRow, iParm,
2a7e3 20 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68   1);.      /* Th
2a7e4 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 77  e LIMIT clause w
2a7e5 69 6c 6c 20 74 65 72 6d 69 6e 61 74 65 20 74 68  ill terminate th
2a7e6 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f  e loop for us */
2a7e7 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2a7e8 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 64    }.#endif.    d
2a7e9 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
2a7ea 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 61 73 73  int i;.      ass
2a7eb 65 72 74 28 20 65 44 65 73 74 3d 3d 53 52 54 5f  ert( eDest==SRT_
2a7ec 4f 75 74 70 75 74 20 7c 7c 20 65 44 65 73 74 3d  Output || eDest=
2a7ed 3d 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29  =SRT_Coroutine )
2a7ee 3b 20 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  ; .      testcas
2a7ef 65 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75  e( eDest==SRT_Ou
2a7f0 74 70 75 74 20 29 3b 0a 20 20 20 20 20 20 74 65  tput );.      te
2a7f1 73 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d 53  stcase( eDest==S
2a7f2 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29 3b 0a  RT_Coroutine );.
2a7f3 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
2a7f4 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a  <nColumn; i++){.
2a7f5 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2a7f6 72 65 67 52 6f 77 21 3d 70 44 65 73 74 2d 3e 69  regRow!=pDest->i
2a7f7 4d 65 6d 2b 69 20 29 3b 0a 20 20 20 20 20 20 20  Mem+i );.       
2a7f8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a7f9 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
2a7fa 20 70 73 65 75 64 6f 54 61 62 2c 20 69 2c 20 70   pseudoTab, i, p
2a7fb 44 65 73 74 2d 3e 69 4d 65 6d 2b 69 29 3b 0a 20  Dest->iMem+i);. 
2a7fc 20 20 20 20 20 20 20 69 66 28 20 69 3d 3d 30 20         if( i==0 
2a7fd 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
2a7fe 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35  ite3VdbeChangeP5
2a7ff 28 76 2c 20 4f 50 46 4c 41 47 5f 43 4c 45 41 52  (v, OPFLAG_CLEAR
2a800 43 41 43 48 45 29 3b 0a 20 20 20 20 20 20 20 20  CACHE);.        
2a801 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
2a802 69 66 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f  if( eDest==SRT_O
2a803 75 74 70 75 74 20 29 7b 0a 20 20 20 20 20 20 20  utput ){.       
2a804 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a805 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
2a806 6f 77 2c 20 70 44 65 73 74 2d 3e 69 4d 65 6d 2c  ow, pDest->iMem,
2a807 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20   nColumn);.     
2a808 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
2a809 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
2a80a 65 28 70 50 61 72 73 65 2c 20 70 44 65 73 74 2d  e(pParse, pDest-
2a80b 3e 69 4d 65 6d 2c 20 6e 43 6f 6c 75 6d 6e 29 3b  >iMem, nColumn);
2a80c 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2a80d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2a80e 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69  eAddOp1(v, OP_Yi
2a80f 65 6c 64 2c 20 70 44 65 73 74 2d 3e 69 50 61 72  eld, pDest->iPar
2a810 6d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  m);.      }.    
2a811 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
2a812 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65   }.  sqlite3Rele
2a813 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
2a814 65 2c 20 72 65 67 52 6f 77 29 3b 0a 20 20 73 71  e, regRow);.  sq
2a815 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2a816 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 52  Reg(pParse, regR
2a817 6f 77 69 64 29 3b 0a 0a 20 20 2f 2a 20 4c 49 4d  owid);..  /* LIM
2a818 49 54 20 68 61 73 20 62 65 65 6e 20 69 6d 70 6c  IT has been impl
2a819 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65 20 70  emented by the p
2a81a 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 29 20  ushOntoSorter() 
2a81b 72 6f 75 74 69 6e 65 2e 0a 20 20 2a 2f 0a 20 20  routine..  */.  
2a81c 61 73 73 65 72 74 28 20 70 2d 3e 69 4c 69 6d 69  assert( p->iLimi
2a81d 74 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 54 68  t==0 );..  /* Th
2a81e 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20  e bottom of the 
2a81f 6c 6f 6f 70 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  loop.  */.  sqli
2a820 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
2a821 62 65 6c 28 76 2c 20 61 64 64 72 43 6f 6e 74 69  bel(v, addrConti
2a822 6e 75 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  nue);.  sqlite3V
2a823 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2a824 4e 65 78 74 2c 20 69 54 61 62 2c 20 61 64 64 72  Next, iTab, addr
2a825 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2a826 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
2a827 61 64 64 72 42 72 65 61 6b 29 3b 0a 20 20 69 66  addrBreak);.  if
2a828 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74  ( eDest==SRT_Out
2a829 70 75 74 20 7c 7c 20 65 44 65 73 74 3d 3d 53 52  put || eDest==SR
2a82a 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29 7b 0a 20  T_Coroutine ){. 
2a82b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2a82c 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp2(v, OP_Close
2a82d 2c 20 70 73 65 75 64 6f 54 61 62 2c 20 30 29 3b  , pseudoTab, 0);
2a82e 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
2a82f 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
2a830 6f 20 61 20 73 74 72 69 6e 67 20 63 6f 6e 74 61  o a string conta
2a831 69 6e 69 6e 67 20 74 68 65 20 27 64 65 63 6c 61  ining the 'decla
2a832 72 61 74 69 6f 6e 20 74 79 70 65 27 20 6f 66 20  ration type' of 
2a833 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  the.** expressio
2a834 6e 20 70 45 78 70 72 2e 20 54 68 65 20 73 74 72  n pExpr. The str
2a835 69 6e 67 20 6d 61 79 20 62 65 20 74 72 65 61 74  ing may be treat
2a836 65 64 20 61 73 20 73 74 61 74 69 63 20 62 79 20  ed as static by 
2a837 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a  the caller..**.*
2a838 2a 20 54 68 65 20 64 65 63 6c 61 72 61 74 69 6f  * The declaratio
2a839 6e 20 74 79 70 65 20 69 73 20 74 68 65 20 65 78  n type is the ex
2a83a 61 63 74 20 64 61 74 61 74 79 70 65 20 64 65 66  act datatype def
2a83b 69 6e 69 74 69 6f 6e 20 65 78 74 72 61 63 74 65  inition extracte
2a83c 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 6f 72  d from the.** or
2a83d 69 67 69 6e 61 6c 20 43 52 45 41 54 45 20 54 41  iginal CREATE TA
2a83e 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 66  BLE statement if
2a83f 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
2a840 69 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 54 68 65  is a column. The
2a841 0a 2a 2a 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  .** declaration 
2a842 74 79 70 65 20 66 6f 72 20 61 20 52 4f 57 49 44  type for a ROWID
2a843 20 66 69 65 6c 64 20 69 73 20 49 4e 54 45 47 45   field is INTEGE
2a844 52 2e 20 45 78 61 63 74 6c 79 20 77 68 65 6e 20  R. Exactly when 
2a845 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  an expression.**
2a846 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61   is considered a
2a847 20 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 63   column can be c
2a848 6f 6d 70 6c 65 78 20 69 6e 20 74 68 65 20 70 72  omplex in the pr
2a849 65 73 65 6e 63 65 20 6f 66 20 73 75 62 71 75 65  esence of subque
2a84a 72 69 65 73 2e 20 54 68 65 0a 2a 2a 20 72 65 73  ries. The.** res
2a84b 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69  ult-set expressi
2a84c 6f 6e 20 69 6e 20 61 6c 6c 20 6f 66 20 74 68 65  on in all of the
2a84d 20 66 6f 6c 6c 6f 77 69 6e 67 20 53 45 4c 45 43   following SELEC
2a84e 54 20 73 74 61 74 65 6d 65 6e 74 73 20 69 73 20  T statements is 
2a84f 0a 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64 20 61  .** considered a
2a850 20 63 6f 6c 75 6d 6e 20 62 79 20 74 68 69 73 20   column by this 
2a851 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
2a852 20 20 53 45 4c 45 43 54 20 63 6f 6c 20 46 52 4f    SELECT col FRO
2a853 4d 20 74 62 6c 3b 0a 2a 2a 20 20 20 53 45 4c 45  M tbl;.**   SELE
2a854 43 54 20 28 53 45 4c 45 43 54 20 63 6f 6c 20 46  CT (SELECT col F
2a855 52 4f 4d 20 74 62 6c 3b 0a 2a 2a 20 20 20 53 45  ROM tbl;.**   SE
2a856 4c 45 43 54 20 28 53 45 4c 45 43 54 20 63 6f 6c  LECT (SELECT col
2a857 20 46 52 4f 4d 20 74 62 6c 29 3b 0a 2a 2a 20 20   FROM tbl);.**  
2a858 20 53 45 4c 45 43 54 20 61 62 63 20 46 52 4f 4d   SELECT abc FROM
2a859 20 28 53 45 4c 45 43 54 20 63 6f 6c 20 41 53 20   (SELECT col AS 
2a85a 61 62 63 20 46 52 4f 4d 20 74 62 6c 29 3b 0a 2a  abc FROM tbl);.*
2a85b 2a 20 0a 2a 2a 20 54 68 65 20 64 65 63 6c 61 72  * .** The declar
2a85c 61 74 69 6f 6e 20 74 79 70 65 20 66 6f 72 20 61  ation type for a
2a85d 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 74  ny expression ot
2a85e 68 65 72 20 74 68 61 6e 20 61 20 63 6f 6c 75 6d  her than a colum
2a85f 6e 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74  n is NULL..*/.st
2a860 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
2a861 2a 63 6f 6c 75 6d 6e 54 79 70 65 28 0a 20 20 4e  *columnType(.  N
2a862 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c  ameContext *pNC,
2a863 20 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c   .  Expr *pExpr,
2a864 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
2a865 70 7a 4f 72 69 67 69 6e 44 62 2c 0a 20 20 63 6f  pzOriginDb,.  co
2a866 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 4f 72 69  nst char **pzOri
2a867 67 69 6e 54 61 62 2c 0a 20 20 63 6f 6e 73 74 20  ginTab,.  const 
2a868 63 68 61 72 20 2a 2a 70 7a 4f 72 69 67 69 6e 43  char **pzOriginC
2a869 6f 6c 0a 29 7b 0a 20 20 63 68 61 72 20 63 6f 6e  ol.){.  char con
2a86a 73 74 20 2a 7a 54 79 70 65 20 3d 20 30 3b 0a 20  st *zType = 0;. 
2a86b 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4f 72   char const *zOr
2a86c 69 67 69 6e 44 62 20 3d 20 30 3b 0a 20 20 63 68  iginDb = 0;.  ch
2a86d 61 72 20 63 6f 6e 73 74 20 2a 7a 4f 72 69 67 69  ar const *zOrigi
2a86e 6e 54 61 62 20 3d 20 30 3b 0a 20 20 63 68 61 72  nTab = 0;.  char
2a86f 20 63 6f 6e 73 74 20 2a 7a 4f 72 69 67 69 6e 43   const *zOriginC
2a870 6f 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6a 3b  ol = 0;.  int j;
2a871 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 45 78  .  if( NEVER(pEx
2a872 70 72 3d 3d 30 29 20 7c 7c 20 70 4e 43 2d 3e 70  pr==0) || pNC->p
2a873 53 72 63 4c 69 73 74 3d 3d 30 20 29 20 72 65 74  SrcList==0 ) ret
2a874 75 72 6e 20 30 3b 0a 0a 20 20 73 77 69 74 63 68  urn 0;..  switch
2a875 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20  ( pExpr->op ){. 
2a876 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43     case TK_AGG_C
2a877 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65 20  OLUMN:.    case 
2a878 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20  TK_COLUMN: {.   
2a879 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
2a87a 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e  sion is a column
2a87b 2e 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62  . Locate the tab
2a87c 6c 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73  le the column is
2a87d 20 62 65 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20   being.      ** 
2a87e 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 69  extracted from i
2a87f 6e 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 70 53  n NameContext.pS
2a880 72 63 4c 69 73 74 2e 20 54 68 69 73 20 74 61 62  rcList. This tab
2a881 6c 65 20 6d 61 79 20 62 65 20 72 65 61 6c 0a 20  le may be real. 
2a882 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65       ** database
2a883 20 74 61 62 6c 65 20 6f 72 20 61 20 73 75 62 71   table or a subq
2a884 75 65 72 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  uery..      */. 
2a885 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62       Table *pTab
2a886 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
2a887 20 2f 2a 20 54 61 62 6c 65 20 73 74 72 75 63 74   /* Table struct
2a888 75 72 65 20 63 6f 6c 75 6d 6e 20 69 73 20 65 78  ure column is ex
2a889 74 72 61 63 74 65 64 20 66 72 6f 6d 20 2a 2f 0a  tracted from */.
2a88a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53        Select *pS
2a88b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
2a88c 20 20 2f 2a 20 53 65 6c 65 63 74 20 74 68 65 20    /* Select the 
2a88d 63 6f 6c 75 6d 6e 20 69 73 20 65 78 74 72 61 63  column is extrac
2a88e 74 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20  ted from */.    
2a88f 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 45 78    int iCol = pEx
2a890 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 20 20 2f 2a  pr->iColumn;  /*
2a891 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e   Index of column
2a892 20 69 6e 20 70 54 61 62 20 2a 2f 0a 20 20 20 20   in pTab */.    
2a893 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70    testcase( pExp
2a894 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f  r->op==TK_AGG_CO
2a895 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74 65  LUMN );.      te
2a896 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
2a897 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a  p==TK_COLUMN );.
2a898 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 4e 43        while( pNC
2a899 20 26 26 20 21 70 54 61 62 20 29 7b 0a 20 20 20   && !pTab ){.   
2a89a 20 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 54       SrcList *pT
2a89b 61 62 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53  abList = pNC->pS
2a89c 72 63 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20  rcList;.        
2a89d 66 6f 72 28 6a 3d 30 3b 6a 3c 70 54 61 62 4c 69  for(j=0;j<pTabLi
2a89e 73 74 2d 3e 6e 53 72 63 20 26 26 20 70 54 61 62  st->nSrc && pTab
2a89f 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 69 43 75 72 73  List->a[j].iCurs
2a8a0 6f 72 21 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c  or!=pExpr->iTabl
2a8a1 65 3b 6a 2b 2b 29 3b 0a 20 20 20 20 20 20 20 20  e;j++);.        
2a8a2 69 66 28 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e  if( j<pTabList->
2a8a3 6e 53 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  nSrc ){.        
2a8a4 20 20 70 54 61 62 20 3d 20 70 54 61 62 4c 69 73    pTab = pTabLis
2a8a5 74 2d 3e 61 5b 6a 5d 2e 70 54 61 62 3b 0a 20 20  t->a[j].pTab;.  
2a8a6 20 20 20 20 20 20 20 20 70 53 20 3d 20 70 54 61          pS = pTa
2a8a7 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 53 65 6c  bList->a[j].pSel
2a8a8 65 63 74 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  ect;.        }el
2a8a9 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e  se{.          pN
2a8aa 43 20 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a  C = pNC->pNext;.
2a8ab 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2a8ac 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 70 54 61  }..      if( pTa
2a8ad 62 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  b==0 ){.        
2a8ae 2f 2a 20 41 74 20 6f 6e 65 20 74 69 6d 65 2c 20  /* At one time, 
2a8af 63 6f 64 65 20 73 75 63 68 20 61 73 20 22 53 45  code such as "SE
2a8b0 4c 45 43 54 20 6e 65 77 2e 78 22 20 77 69 74 68  LECT new.x" with
2a8b1 69 6e 20 61 20 74 72 69 67 67 65 72 20 77 6f 75  in a trigger wou
2a8b2 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 61  ld.        ** ca
2a8b3 75 73 65 20 74 68 69 73 20 63 6f 6e 64 69 74 69  use this conditi
2a8b4 6f 6e 20 74 6f 20 72 75 6e 2e 20 20 53 69 6e 63  on to run.  Sinc
2a8b5 65 20 74 68 65 6e 2c 20 77 65 20 68 61 76 65 20  e then, we have 
2a8b6 72 65 73 74 72 75 63 74 75 72 65 64 20 68 6f 77  restructured how
2a8b7 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 69 67  .        ** trig
2a8b8 67 65 72 20 63 6f 64 65 20 69 73 20 67 65 6e 65  ger code is gene
2a8b9 72 61 74 65 64 20 61 6e 64 20 73 6f 20 74 68 69  rated and so thi
2a8ba 73 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 20 6e  s condition is n
2a8bb 6f 20 6c 6f 6e 67 65 72 20 0a 20 20 20 20 20 20  o longer .      
2a8bc 20 20 2a 2a 20 70 6f 73 73 69 62 6c 65 2e 20 48    ** possible. H
2a8bd 6f 77 65 76 65 72 2c 20 69 74 20 63 61 6e 20 73  owever, it can s
2a8be 74 69 6c 6c 20 62 65 20 74 72 75 65 20 66 6f 72  till be true for
2a8bf 20 73 74 61 74 65 6d 65 6e 74 73 20 6c 69 6b 65   statements like
2a8c0 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20  .        ** the 
2a8c1 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 20 20 20 20 20  following:.     
2a8c2 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
2a8c3 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
2a8c4 74 31 28 63 6f 6c 20 49 4e 54 45 47 45 52 29 3b  t1(col INTEGER);
2a8c5 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 53 45  .        **   SE
2a8c6 4c 45 43 54 20 28 53 45 4c 45 43 54 20 74 31 2e  LECT (SELECT t1.
2a8c7 63 6f 6c 29 20 46 52 4f 4d 20 46 52 4f 4d 20 74  col) FROM FROM t
2a8c8 31 3b 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  1;.        **.  
2a8c9 20 20 20 20 20 20 2a 2a 20 77 68 65 6e 20 63 6f        ** when co
2a8ca 6c 75 6d 6e 54 79 70 65 28 29 20 69 73 20 63 61  lumnType() is ca
2a8cb 6c 6c 65 64 20 6f 6e 20 74 68 65 20 65 78 70 72  lled on the expr
2a8cc 65 73 73 69 6f 6e 20 22 74 31 2e 63 6f 6c 22 20  ession "t1.col" 
2a8cd 69 6e 20 74 68 65 20 0a 20 20 20 20 20 20 20 20  in the .        
2a8ce 2a 2a 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 49  ** sub-select. I
2a8cf 6e 20 74 68 69 73 20 63 61 73 65 2c 20 73 65 74  n this case, set
2a8d0 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 79 70 65   the column type
2a8d1 20 74 6f 20 4e 55 4c 4c 2c 20 65 76 65 6e 0a 20   to NULL, even. 
2a8d2 20 20 20 20 20 20 20 2a 2a 20 74 68 6f 75 67 68         ** though
2a8d3 20 69 74 20 73 68 6f 75 6c 64 20 72 65 61 6c 6c   it should reall
2a8d4 79 20 62 65 20 22 49 4e 54 45 47 45 52 22 2e 0a  y be "INTEGER"..
2a8d5 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
2a8d6 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f     ** This is no
2a8d7 74 20 61 20 70 72 6f 62 6c 65 6d 2c 20 61 73 20  t a problem, as 
2a8d8 74 68 65 20 63 6f 6c 75 6d 6e 20 74 79 70 65 20  the column type 
2a8d9 6f 66 20 22 74 31 2e 63 6f 6c 22 20 69 73 20 6e  of "t1.col" is n
2a8da 65 76 65 72 0a 20 20 20 20 20 20 20 20 2a 2a 20  ever.        ** 
2a8db 75 73 65 64 2e 20 57 68 65 6e 20 63 6f 6c 75 6d  used. When colum
2a8dc 6e 54 79 70 65 28 29 20 69 73 20 63 61 6c 6c 65  nType() is calle
2a8dd 64 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73  d on the express
2a8de 69 6f 6e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  ion .        ** 
2a8df 22 28 53 45 4c 45 43 54 20 74 31 2e 63 6f 6c 29  "(SELECT t1.col)
2a8e0 22 2c 20 74 68 65 20 63 6f 72 72 65 63 74 20 74  ", the correct t
2a8e1 79 70 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  ype is returned 
2a8e2 28 73 65 65 20 74 68 65 20 54 4b 5f 53 45 4c 45  (see the TK_SELE
2a8e3 43 54 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 72  CT.        ** br
2a8e4 61 6e 63 68 20 62 65 6c 6f 77 2e 20 20 2a 2f 0a  anch below.  */.
2a8e5 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2a8e6 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73       }..      as
2a8e7 73 65 72 74 28 20 70 54 61 62 20 26 26 20 70 45  sert( pTab && pE
2a8e8 78 70 72 2d 3e 70 54 61 62 3d 3d 70 54 61 62 20  xpr->pTab==pTab 
2a8e9 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 53 20  );.      if( pS 
2a8ea 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
2a8eb 65 20 22 74 61 62 6c 65 22 20 69 73 20 61 63 74  e "table" is act
2a8ec 75 61 6c 6c 79 20 61 20 73 75 62 2d 73 65 6c 65  ually a sub-sele
2a8ed 63 74 20 6f 72 20 61 20 76 69 65 77 20 69 6e 20  ct or a view in 
2a8ee 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a  the FROM clause.
2a8ef 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68          ** of th
2a8f0 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
2a8f1 6e 74 2e 20 52 65 74 75 72 6e 20 74 68 65 20 64  nt. Return the d
2a8f2 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20  eclaration type 
2a8f3 61 6e 64 20 6f 72 69 67 69 6e 0a 20 20 20 20 20  and origin.     
2a8f4 20 20 20 2a 2a 20 64 61 74 61 20 66 6f 72 20 74     ** data for t
2a8f5 68 65 20 72 65 73 75 6c 74 2d 73 65 74 20 63 6f  he result-set co
2a8f6 6c 75 6d 6e 20 6f 66 20 74 68 65 20 73 75 62 2d  lumn of the sub-
2a8f7 73 65 6c 65 63 74 2e 0a 20 20 20 20 20 20 20 20  select..        
2a8f8 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 41  */.        if( A
2a8f9 4c 57 41 59 53 28 69 43 6f 6c 3e 3d 30 20 26 26  LWAYS(iCol>=0 &&
2a8fa 20 69 43 6f 6c 3c 70 53 2d 3e 70 45 4c 69 73 74   iCol<pS->pEList
2a8fb 2d 3e 6e 45 78 70 72 29 20 29 7b 0a 20 20 20 20  ->nExpr) ){.    
2a8fc 20 20 20 20 20 20 2f 2a 20 49 66 20 69 43 6f 6c        /* If iCol
2a8fd 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65   is less than ze
2a8fe 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 65 78 70  ro, then the exp
2a8ff 72 65 73 73 69 6f 6e 20 72 65 71 75 65 73 74 73  ression requests
2a900 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a   the.          *
2a901 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 73  * rowid of the s
2a902 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 76 69 65  ub-select or vie
2a903 77 2e 20 54 68 69 73 20 65 78 70 72 65 73 73 69  w. This expressi
2a904 6f 6e 20 69 73 20 6c 65 67 61 6c 20 28 73 65 65  on is legal (see
2a905 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74   .          ** t
2a906 65 73 74 20 63 61 73 65 20 6d 69 73 63 32 2e 32  est case misc2.2
2a907 2e 32 29 20 2d 20 69 74 20 61 6c 77 61 79 73 20  .2) - it always 
2a908 65 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55 4c  evaluates to NUL
2a909 4c 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L..          */.
2a90a 20 20 20 20 20 20 20 20 20 20 4e 61 6d 65 43 6f            NameCo
2a90b 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20 20 20  ntext sNC;.     
2a90c 20 20 20 20 20 45 78 70 72 20 2a 70 20 3d 20 70       Expr *p = p
2a90d 53 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f  S->pEList->a[iCo
2a90e 6c 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  l].pExpr;.      
2a90f 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74      sNC.pSrcList
2a910 20 3d 20 70 53 2d 3e 70 53 72 63 3b 0a 20 20 20   = pS->pSrc;.   
2a911 20 20 20 20 20 20 20 73 4e 43 2e 70 4e 65 78 74         sNC.pNext
2a912 20 3d 20 70 4e 43 3b 0a 20 20 20 20 20 20 20 20   = pNC;.        
2a913 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70    sNC.pParse = p
2a914 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20  NC->pParse;.    
2a915 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 63 6f        zType = co
2a916 6c 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70  lumnType(&sNC, p
2a917 2c 20 26 7a 4f 72 69 67 69 6e 44 62 2c 20 26 7a  , &zOriginDb, &z
2a918 4f 72 69 67 69 6e 54 61 62 2c 20 26 7a 4f 72 69  OriginTab, &zOri
2a919 67 69 6e 43 6f 6c 29 3b 20 0a 20 20 20 20 20 20  ginCol); .      
2a91a 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20    }.      }else 
2a91b 69 66 28 20 41 4c 57 41 59 53 28 70 54 61 62 2d  if( ALWAYS(pTab-
2a91c 3e 70 53 63 68 65 6d 61 29 20 29 7b 0a 20 20 20  >pSchema) ){.   
2a91d 20 20 20 20 20 2f 2a 20 41 20 72 65 61 6c 20 74       /* A real t
2a91e 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  able */.        
2a91f 61 73 73 65 72 74 28 20 21 70 53 20 29 3b 0a 20  assert( !pS );. 
2a920 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c         if( iCol<
2a921 30 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d  0 ) iCol = pTab-
2a922 3e 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 20 20  >iPKey;.        
2a923 61 73 73 65 72 74 28 20 69 43 6f 6c 3d 3d 2d 31  assert( iCol==-1
2a924 20 7c 7c 20 28 69 43 6f 6c 3e 3d 30 20 26 26 20   || (iCol>=0 && 
2a925 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 29  iCol<pTab->nCol)
2a926 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
2a927 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20  iCol<0 ){.      
2a928 20 20 20 20 7a 54 79 70 65 20 3d 20 22 49 4e 54      zType = "INT
2a929 45 47 45 52 22 3b 0a 20 20 20 20 20 20 20 20 20  EGER";.         
2a92a 20 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 22 72   zOriginCol = "r
2a92b 6f 77 69 64 22 3b 0a 20 20 20 20 20 20 20 20 7d  owid";.        }
2a92c 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
2a92d 7a 54 79 70 65 20 3d 20 70 54 61 62 2d 3e 61 43  zType = pTab->aC
2a92e 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 70 65 3b 0a  ol[iCol].zType;.
2a92f 20 20 20 20 20 20 20 20 20 20 7a 4f 72 69 67 69            zOrigi
2a930 6e 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f  nCol = pTab->aCo
2a931 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20  l[iCol].zName;. 
2a932 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2a933 20 7a 4f 72 69 67 69 6e 54 61 62 20 3d 20 70 54   zOriginTab = pT
2a934 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  ab->zName;.     
2a935 20 20 20 69 66 28 20 70 4e 43 2d 3e 70 50 61 72     if( pNC->pPar
2a936 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  se ){.          
2a937 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65  int iDb = sqlite
2a938 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
2a939 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2c 20  NC->pParse->db, 
2a93a 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
2a93b 20 20 20 20 20 20 20 20 20 20 7a 4f 72 69 67 69            zOrigi
2a93c 6e 44 62 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73  nDb = pNC->pPars
2a93d 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  e->db->aDb[iDb].
2a93e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 7d  zName;.        }
2a93f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
2a940 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e  reak;.    }.#ifn
2a941 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2a942 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73  SUBQUERY.    cas
2a943 65 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a 20  e TK_SELECT: {. 
2a944 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72       /* The expr
2a945 65 73 73 69 6f 6e 20 69 73 20 61 20 73 75 62 2d  ession is a sub-
2a946 73 65 6c 65 63 74 2e 20 52 65 74 75 72 6e 20 74  select. Return t
2a947 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74  he declaration t
2a948 79 70 65 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a  ype and.      **
2a949 20 6f 72 69 67 69 6e 20 69 6e 66 6f 20 66 6f 72   origin info for
2a94a 20 74 68 65 20 73 69 6e 67 6c 65 20 63 6f 6c 75   the single colu
2a94b 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  mn in the result
2a94c 20 73 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45   set of the SELE
2a94d 43 54 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74  CT.      ** stat
2a94e 65 6d 65 6e 74 2e 0a 20 20 20 20 20 20 2a 2f 0a  ement..      */.
2a94f 20 20 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78        NameContex
2a950 74 20 73 4e 43 3b 0a 20 20 20 20 20 20 53 65 6c  t sNC;.      Sel
2a951 65 63 74 20 2a 70 53 20 3d 20 70 45 78 70 72 2d  ect *pS = pExpr-
2a952 3e 78 2e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20  >x.pSelect;.    
2a953 20 20 45 78 70 72 20 2a 70 20 3d 20 70 53 2d 3e    Expr *p = pS->
2a954 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
2a955 70 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  pr;.      assert
2a956 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
2a957 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  y(pExpr, EP_xIsS
2a958 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20  elect) );.      
2a959 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70  sNC.pSrcList = p
2a95a 53 2d 3e 70 53 72 63 3b 0a 20 20 20 20 20 20 73  S->pSrc;.      s
2a95b 4e 43 2e 70 4e 65 78 74 20 3d 20 70 4e 43 3b 0a  NC.pNext = pNC;.
2a95c 20 20 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65        sNC.pParse
2a95d 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a   = pNC->pParse;.
2a95e 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 63 6f        zType = co
2a95f 6c 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70  lumnType(&sNC, p
2a960 2c 20 26 7a 4f 72 69 67 69 6e 44 62 2c 20 26 7a  , &zOriginDb, &z
2a961 4f 72 69 67 69 6e 54 61 62 2c 20 26 7a 4f 72 69  OriginTab, &zOri
2a962 67 69 6e 43 6f 6c 29 3b 20 0a 20 20 20 20 20 20  ginCol); .      
2a963 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
2a964 64 69 66 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28  dif.  }.  .  if(
2a965 20 70 7a 4f 72 69 67 69 6e 44 62 20 29 7b 0a 20   pzOriginDb ){. 
2a966 20 20 20 61 73 73 65 72 74 28 20 70 7a 4f 72 69     assert( pzOri
2a967 67 69 6e 54 61 62 20 26 26 20 70 7a 4f 72 69 67  ginTab && pzOrig
2a968 69 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 2a 70 7a  inCol );.    *pz
2a969 4f 72 69 67 69 6e 44 62 20 3d 20 7a 4f 72 69 67  OriginDb = zOrig
2a96a 69 6e 44 62 3b 0a 20 20 20 20 2a 70 7a 4f 72 69  inDb;.    *pzOri
2a96b 67 69 6e 54 61 62 20 3d 20 7a 4f 72 69 67 69 6e  ginTab = zOrigin
2a96c 54 61 62 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 67  Tab;.    *pzOrig
2a96d 69 6e 43 6f 6c 20 3d 20 7a 4f 72 69 67 69 6e 43  inCol = zOriginC
2a96e 6f 6c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ol;.  }.  return
2a96f 20 7a 54 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   zType;.}../*.**
2a970 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
2a971 68 61 74 20 77 69 6c 6c 20 74 65 6c 6c 20 74 68  hat will tell th
2a972 65 20 56 44 42 45 20 74 68 65 20 64 65 63 6c 61  e VDBE the decla
2a973 72 61 74 69 6f 6e 20 74 79 70 65 73 20 6f 66 20  ration types of 
2a974 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68  columns.** in th
2a975 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2f  e result set..*/
2a976 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67 65 6e  .static void gen
2a977 65 72 61 74 65 43 6f 6c 75 6d 6e 54 79 70 65 73  erateColumnTypes
2a978 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2a979 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65  e,      /* Parse
2a97a 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  r context */.  S
2a97b 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74  rcList *pTabList
2a97c 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61  ,  /* List of ta
2a97d 62 6c 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69  bles */.  ExprLi
2a97e 73 74 20 2a 70 45 4c 69 73 74 20 20 20 20 2f 2a  st *pEList    /*
2a97f 20 45 78 70 72 65 73 73 69 6f 6e 73 20 64 65 66   Expressions def
2a980 69 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74  ining the result
2a981 20 73 65 74 20 2a 2f 0a 29 7b 0a 23 69 66 6e 64   set */.){.#ifnd
2a982 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
2a983 45 43 4c 54 59 50 45 0a 20 20 56 64 62 65 20 2a  ECLTYPE.  Vdbe *
2a984 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2a985 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4e 61  e;.  int i;.  Na
2a986 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20  meContext sNC;. 
2a987 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20   sNC.pSrcList = 
2a988 70 54 61 62 4c 69 73 74 3b 0a 20 20 73 4e 43 2e  pTabList;.  sNC.
2a989 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
2a98a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45  .  for(i=0; i<pE
2a98b 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
2a98c 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 20 3d  ){.    Expr *p =
2a98d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45   pEList->a[i].pE
2a98e 78 70 72 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  xpr;.    const c
2a98f 68 61 72 20 2a 7a 54 79 70 65 3b 0a 23 69 66 64  har *zType;.#ifd
2a990 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
2a991 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41  _COLUMN_METADATA
2a992 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
2a993 2a 7a 4f 72 69 67 44 62 20 3d 20 30 3b 0a 20 20  *zOrigDb = 0;.  
2a994 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f    const char *zO
2a995 72 69 67 54 61 62 20 3d 20 30 3b 0a 20 20 20 20  rigTab = 0;.    
2a996 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 72 69  const char *zOri
2a997 67 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 7a 54  gCol = 0;.    zT
2a998 79 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65  ype = columnType
2a999 28 26 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 67  (&sNC, p, &zOrig
2a99a 44 62 2c 20 26 7a 4f 72 69 67 54 61 62 2c 20 26  Db, &zOrigTab, &
2a99b 7a 4f 72 69 67 43 6f 6c 29 3b 0a 0a 20 20 20 20  zOrigCol);..    
2a99c 2f 2a 20 54 68 65 20 76 64 62 65 20 6d 75 73 74  /* The vdbe must
2a99d 20 6d 61 6b 65 20 69 74 73 20 6f 77 6e 20 63 6f   make its own co
2a99e 70 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  py of the column
2a99f 2d 74 79 70 65 20 61 6e 64 20 6f 74 68 65 72 20  -type and other 
2a9a0 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 73  .    ** column s
2a9a1 70 65 63 69 66 69 63 20 73 74 72 69 6e 67 73 2c  pecific strings,
2a9a2 20 69 6e 20 63 61 73 65 20 74 68 65 20 73 63 68   in case the sch
2a9a3 65 6d 61 20 69 73 20 72 65 73 65 74 20 62 65 66  ema is reset bef
2a9a4 6f 72 65 20 74 68 69 73 0a 20 20 20 20 2a 2a 20  ore this.    ** 
2a9a5 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  virtual machine 
2a9a6 69 73 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20  is deleted..    
2a9a7 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
2a9a8 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
2a9a9 69 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42  i, COLNAME_DATAB
2a9aa 41 53 45 2c 20 7a 4f 72 69 67 44 62 2c 20 53 51  ASE, zOrigDb, SQ
2a9ab 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
2a9ac 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2a9ad 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c  SetColName(v, i,
2a9ae 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 2c 20   COLNAME_TABLE, 
2a9af 7a 4f 72 69 67 54 61 62 2c 20 53 51 4c 49 54 45  zOrigTab, SQLITE
2a9b0 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20  _TRANSIENT);.   
2a9b1 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
2a9b2 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c  olName(v, i, COL
2a9b3 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 2c 20 7a 4f 72  NAME_COLUMN, zOr
2a9b4 69 67 43 6f 6c 2c 20 53 51 4c 49 54 45 5f 54 52  igCol, SQLITE_TR
2a9b5 41 4e 53 49 45 4e 54 29 3b 0a 23 65 6c 73 65 0a  ANSIENT);.#else.
2a9b6 20 20 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c 75      zType = colu
2a9b7 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20  mnType(&sNC, p, 
2a9b8 30 2c 20 30 2c 20 30 29 3b 0a 23 65 6e 64 69 66  0, 0, 0);.#endif
2a9b9 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2a9ba 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c  SetColName(v, i,
2a9bb 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50   COLNAME_DECLTYP
2a9bc 45 2c 20 7a 54 79 70 65 2c 20 53 51 4c 49 54 45  E, zType, SQLITE
2a9bd 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d  _TRANSIENT);.  }
2a9be 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2a9bf 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20  E_OMIT_DECLTYPE 
2a9c0 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  */.}../*.** Gene
2a9c1 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77  rate code that w
2a9c2 69 6c 6c 20 74 65 6c 6c 20 74 68 65 20 56 44 42  ill tell the VDB
2a9c3 45 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 63  E the names of c
2a9c4 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68 65  olumns.** in the
2a9c5 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54 68   result set.  Th
2a9c6 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  is information i
2a9c7 73 20 75 73 65 64 20 74 6f 20 70 72 6f 76 69 64  s used to provid
2a9c8 65 20 74 68 65 0a 2a 2a 20 61 7a 43 6f 6c 5b 5d  e the.** azCol[]
2a9c9 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 63   values in the c
2a9ca 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74  allback..*/.stat
2a9cb 69 63 20 76 6f 69 64 20 67 65 6e 65 72 61 74 65  ic void generate
2a9cc 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 0a 20 20 50  ColumnNames(.  P
2a9cd 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2a9ce 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e     /* Parser con
2a9cf 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73  text */.  SrcLis
2a9d0 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 2f 2a  t *pTabList,  /*
2a9d1 20 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20   List of tables 
2a9d2 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2a9d3 45 4c 69 73 74 20 20 20 20 2f 2a 20 45 78 70 72  EList    /* Expr
2a9d4 65 73 73 69 6f 6e 73 20 64 65 66 69 6e 69 6e 67  essions defining
2a9d5 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
2a9d6 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20  */.){.  Vdbe *v 
2a9d7 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
2a9d8 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73  .  int i, j;.  s
2a9d9 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2a9da 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 66  rse->db;.  int f
2a9db 75 6c 6c 4e 61 6d 65 73 2c 20 73 68 6f 72 74 4e  ullNames, shortN
2a9dc 61 6d 65 73 3b 0a 0a 23 69 66 6e 64 65 66 20 53  ames;..#ifndef S
2a9dd 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41  QLITE_OMIT_EXPLA
2a9de 49 4e 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  IN.  /* If this 
2a9df 69 73 20 61 6e 20 45 58 50 4c 41 49 4e 2c 20 73  is an EXPLAIN, s
2a9e0 6b 69 70 20 74 68 69 73 20 73 74 65 70 20 2a 2f  kip this step */
2a9e1 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 65  .  if( pParse->e
2a9e2 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 72 65  xplain ){.    re
2a9e3 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  turn;.  }.#endif
2a9e4 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ..  if( pParse->
2a9e5 63 6f 6c 4e 61 6d 65 73 53 65 74 20 7c 7c 20 4e  colNamesSet || N
2a9e6 45 56 45 52 28 76 3d 3d 30 29 20 7c 7c 20 64 62  EVER(v==0) || db
2a9e7 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2a9e8 20 72 65 74 75 72 6e 3b 0a 20 20 70 50 61 72 73   return;.  pPars
2a9e9 65 2d 3e 63 6f 6c 4e 61 6d 65 73 53 65 74 20 3d  e->colNamesSet =
2a9ea 20 31 3b 0a 20 20 66 75 6c 6c 4e 61 6d 65 73 20   1;.  fullNames 
2a9eb 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  = (db->flags & S
2a9ec 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d  QLITE_FullColNam
2a9ed 65 73 29 21 3d 30 3b 0a 20 20 73 68 6f 72 74 4e  es)!=0;.  shortN
2a9ee 61 6d 65 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67  ames = (db->flag
2a9ef 73 20 26 20 53 51 4c 49 54 45 5f 53 68 6f 72 74  s & SQLITE_Short
2a9f0 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 3b 0a 20 20  ColNames)!=0;.  
2a9f1 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
2a9f2 6d 43 6f 6c 73 28 76 2c 20 70 45 4c 69 73 74 2d  mCols(v, pEList-
2a9f3 3e 6e 45 78 70 72 29 3b 0a 20 20 66 6f 72 28 69  >nExpr);.  for(i
2a9f4 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45  =0; i<pEList->nE
2a9f5 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45  xpr; i++){.    E
2a9f6 78 70 72 20 2a 70 3b 0a 20 20 20 20 70 20 3d 20  xpr *p;.    p = 
2a9f7 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78  pEList->a[i].pEx
2a9f8 70 72 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45  pr;.    if( NEVE
2a9f9 52 28 70 3d 3d 30 29 20 29 20 63 6f 6e 74 69 6e  R(p==0) ) contin
2a9fa 75 65 3b 0a 20 20 20 20 69 66 28 20 70 45 4c 69  ue;.    if( pELi
2a9fb 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20 29  st->a[i].zName )
2a9fc 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e  {.      char *zN
2a9fd 61 6d 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b  ame = pEList->a[
2a9fe 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  i].zName;.      
2a9ff 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
2aa00 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e  lName(v, i, COLN
2aa01 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65 2c  AME_NAME, zName,
2aa02 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
2aa03 54 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  T);.    }else if
2aa04 28 20 28 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c  ( (p->op==TK_COL
2aa05 55 4d 4e 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 54 4b  UMN || p->op==TK
2aa06 5f 41 47 47 5f 43 4f 4c 55 4d 4e 29 20 26 26 20  _AGG_COLUMN) && 
2aa07 70 54 61 62 4c 69 73 74 20 29 7b 0a 20 20 20 20  pTabList ){.    
2aa08 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
2aa09 20 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 3b       char *zCol;
2aa0a 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20  .      int iCol 
2aa0b 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20  = p->iColumn;.  
2aa0c 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 41 4c 57      for(j=0; ALW
2aa0d 41 59 53 28 6a 3c 70 54 61 62 4c 69 73 74 2d 3e  AYS(j<pTabList->
2aa0e 6e 53 72 63 29 3b 20 6a 2b 2b 29 7b 0a 20 20 20  nSrc); j++){.   
2aa0f 20 20 20 20 20 69 66 28 20 70 54 61 62 4c 69 73       if( pTabLis
2aa10 74 2d 3e 61 5b 6a 5d 2e 69 43 75 72 73 6f 72 3d  t->a[j].iCursor=
2aa11 3d 70 2d 3e 69 54 61 62 6c 65 20 29 20 62 72 65  =p->iTable ) bre
2aa12 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2aa13 20 20 61 73 73 65 72 74 28 20 6a 3c 70 54 61 62    assert( j<pTab
2aa14 4c 69 73 74 2d 3e 6e 53 72 63 20 29 3b 0a 20 20  List->nSrc );.  
2aa15 20 20 20 20 70 54 61 62 20 3d 20 70 54 61 62 4c      pTab = pTabL
2aa16 69 73 74 2d 3e 61 5b 6a 5d 2e 70 54 61 62 3b 0a  ist->a[j].pTab;.
2aa17 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30        if( iCol<0
2aa18 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e   ) iCol = pTab->
2aa19 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 61 73 73  iPKey;.      ass
2aa1a 65 72 74 28 20 69 43 6f 6c 3d 3d 2d 31 20 7c 7c  ert( iCol==-1 ||
2aa1b 20 28 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f   (iCol>=0 && iCo
2aa1c 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 29 20 29 3b  l<pTab->nCol) );
2aa1d 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c  .      if( iCol<
2aa1e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 43 6f  0 ){.        zCo
2aa1f 6c 20 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20 20  l = "rowid";.   
2aa20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2aa21 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61    zCol = pTab->a
2aa22 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b  Col[iCol].zName;
2aa23 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
2aa24 66 28 20 21 73 68 6f 72 74 4e 61 6d 65 73 20 26  f( !shortNames &
2aa25 26 20 21 66 75 6c 6c 4e 61 6d 65 73 20 29 7b 0a  & !fullNames ){.
2aa26 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2aa27 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
2aa28 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   i, COLNAME_NAME
2aa29 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  , .            s
2aa2a 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
2aa2b 62 2c 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e  b, pEList->a[i].
2aa2c 7a 53 70 61 6e 29 2c 20 53 51 4c 49 54 45 5f 44  zSpan), SQLITE_D
2aa2d 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 7d  YNAMIC);.      }
2aa2e 65 6c 73 65 20 69 66 28 20 66 75 6c 6c 4e 61 6d  else if( fullNam
2aa2f 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  es ){.        ch
2aa30 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20  ar *zName = 0;. 
2aa31 20 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73         zName = s
2aa32 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
2aa33 2c 20 22 25 73 2e 25 73 22 2c 20 70 54 61 62 2d  , "%s.%s", pTab-
2aa34 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3b 0a 20  >zName, zCol);. 
2aa35 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2aa36 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
2aa37 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  i, COLNAME_NAME,
2aa38 20 7a 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 44   zName, SQLITE_D
2aa39 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 7d  YNAMIC);.      }
2aa3a 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
2aa3b 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
2aa3c 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d  ame(v, i, COLNAM
2aa3d 45 5f 4e 41 4d 45 2c 20 7a 43 6f 6c 2c 20 53 51  E_NAME, zCol, SQ
2aa3e 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
2aa3f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
2aa40 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
2aa41 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
2aa42 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, i, COLNAME_NA
2aa43 4d 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 73  ME, .          s
2aa44 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
2aa45 62 2c 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e  b, pEList->a[i].
2aa46 7a 53 70 61 6e 29 2c 20 53 51 4c 49 54 45 5f 44  zSpan), SQLITE_D
2aa47 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d 0a 20  YNAMIC);.    }. 
2aa48 20 7d 0a 20 20 67 65 6e 65 72 61 74 65 43 6f 6c   }.  generateCol
2aa49 75 6d 6e 54 79 70 65 73 28 70 50 61 72 73 65 2c  umnTypes(pParse,
2aa4a 20 70 54 61 62 4c 69 73 74 2c 20 70 45 4c 69 73   pTabList, pELis
2aa4b 74 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  t);.}..#ifndef S
2aa4c 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f  QLITE_OMIT_COMPO
2aa4d 55 4e 44 5f 53 45 4c 45 43 54 0a 2f 2a 0a 2a 2a  UND_SELECT./*.**
2aa4e 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e   Name of the con
2aa4f 6e 65 63 74 69 6f 6e 20 6f 70 65 72 61 74 6f 72  nection operator
2aa50 2c 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72  , used for error
2aa51 20 6d 65 73 73 61 67 65 73 2e 0a 2a 2f 0a 73 74   messages..*/.st
2aa52 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
2aa53 2a 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 69 6e  *selectOpName(in
2aa54 74 20 69 64 29 7b 0a 20 20 63 68 61 72 20 2a 7a  t id){.  char *z
2aa55 3b 0a 20 20 73 77 69 74 63 68 28 20 69 64 20 29  ;.  switch( id )
2aa56 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4c  {.    case TK_AL
2aa57 4c 3a 20 20 20 20 20 20 20 7a 20 3d 20 22 55 4e  L:       z = "UN
2aa58 49 4f 4e 20 41 4c 4c 22 3b 20 20 20 62 72 65 61  ION ALL";   brea
2aa59 6b 3b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49  k;.    case TK_I
2aa5a 4e 54 45 52 53 45 43 54 3a 20 7a 20 3d 20 22 49  NTERSECT: z = "I
2aa5b 4e 54 45 52 53 45 43 54 22 3b 20 20 20 62 72 65  NTERSECT";   bre
2aa5c 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  ak;.    case TK_
2aa5d 45 58 43 45 50 54 3a 20 20 20 20 7a 20 3d 20 22  EXCEPT:    z = "
2aa5e 45 58 43 45 50 54 22 3b 20 20 20 20 20 20 62 72  EXCEPT";      br
2aa5f 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74  eak;.    default
2aa60 3a 20 20 20 20 20 20 20 20 20 20 20 7a 20 3d 20  :           z = 
2aa61 22 55 4e 49 4f 4e 22 3b 20 20 20 20 20 20 20 62  "UNION";       b
2aa62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75  reak;.  }.  retu
2aa63 72 6e 20 7a 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  rn z;.}.#endif /
2aa64 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f  * SQLITE_OMIT_CO
2aa65 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 2a 2f  MPOUND_SELECT */
2aa66 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20  ../*.** Given a 
2aa67 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  an expression li
2aa68 73 74 20 28 77 68 69 63 68 20 69 73 20 72 65 61  st (which is rea
2aa69 6c 6c 79 20 74 68 65 20 6c 69 73 74 20 6f 66 20  lly the list of 
2aa6a 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 74  expressions.** t
2aa6b 68 61 74 20 66 6f 72 6d 20 74 68 65 20 72 65 73  hat form the res
2aa6c 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45 4c  ult set of a SEL
2aa6d 45 43 54 20 73 74 61 74 65 6d 65 6e 74 29 20 63  ECT statement) c
2aa6e 6f 6d 70 75 74 65 20 61 70 70 72 6f 70 72 69 61  ompute appropria
2aa6f 74 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 61 6d  te.** column nam
2aa70 65 73 20 66 6f 72 20 61 20 74 61 62 6c 65 20 74  es for a table t
2aa71 68 61 74 20 77 6f 75 6c 64 20 68 6f 6c 64 20 74  hat would hold t
2aa72 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  he expression li
2aa73 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 63 6f  st..**.** All co
2aa74 6c 75 6d 6e 20 6e 61 6d 65 73 20 77 69 6c 6c 20  lumn names will 
2aa75 62 65 20 75 6e 69 71 75 65 2e 0a 2a 2a 0a 2a 2a  be unique..**.**
2aa76 20 4f 6e 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e   Only the column
2aa77 20 6e 61 6d 65 73 20 61 72 65 20 63 6f 6d 70 75   names are compu
2aa78 74 65 64 2e 20 20 43 6f 6c 75 6d 6e 2e 7a 54 79  ted.  Column.zTy
2aa79 70 65 2c 20 43 6f 6c 75 6d 6e 2e 7a 43 6f 6c 6c  pe, Column.zColl
2aa7a 2c 0a 2a 2a 20 61 6e 64 20 6f 74 68 65 72 20 66  ,.** and other f
2aa7b 69 65 6c 64 73 20 6f 66 20 43 6f 6c 75 6d 6e 20  ields of Column 
2aa7c 61 72 65 20 7a 65 72 6f 65 64 2e 0a 2a 2a 0a 2a  are zeroed..**.*
2aa7d 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * Return SQLITE_
2aa7e 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20  OK on success.  
2aa7f 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  If a memory allo
2aa80 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63  cation error occ
2aa81 75 72 73 2c 0a 2a 2a 20 73 74 6f 72 65 20 4e 55  urs,.** store NU
2aa82 4c 4c 20 69 6e 20 2a 70 61 43 6f 6c 20 61 6e 64  LL in *paCol and
2aa83 20 30 20 69 6e 20 2a 70 6e 43 6f 6c 20 61 6e 64   0 in *pnCol and
2aa84 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
2aa85 4f 4d 45 4d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  OMEM..*/.static 
2aa86 69 6e 74 20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e  int selectColumn
2aa87 73 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 0a 20  sFromExprList(. 
2aa88 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2aa89 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
2aa8a 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2aa8b 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
2aa8c 74 2c 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72  t,       /* Expr
2aa8d 20 6c 69 73 74 20 66 72 6f 6d 20 77 68 69 63 68   list from which
2aa8e 20 74 6f 20 64 65 72 69 76 65 20 63 6f 6c 75 6d   to derive colum
2aa8f 6e 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 69 6e 74  n names */.  int
2aa90 20 2a 70 6e 43 6f 6c 2c 20 20 20 20 20 20 20 20   *pnCol,        
2aa91 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68       /* Write th
2aa92 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
2aa93 6d 6e 73 20 68 65 72 65 20 2a 2f 0a 20 20 43 6f  mns here */.  Co
2aa94 6c 75 6d 6e 20 2a 2a 70 61 43 6f 6c 20 20 20 20  lumn **paCol    
2aa95 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
2aa96 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 6c 69  he new column li
2aa97 73 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  st here */.){.  
2aa98 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2aa99 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 44  arse->db;   /* D
2aa9a 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2aa9b 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a  on */.  int i, j
2aa9c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2aa9d 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2aa9e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e  ters */.  int cn
2aa9f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
2aaa0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 61        /* Index a
2aaa1 64 64 65 64 20 74 6f 20 6d 61 6b 65 20 74 68 65  dded to make the
2aaa2 20 6e 61 6d 65 20 75 6e 69 71 75 65 20 2a 2f 0a   name unique */.
2aaa3 20 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 2c 20    Column *aCol, 
2aaa4 2a 70 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a  *pCol;        /*
2aaa5 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
2aaa6 72 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73  r result columns
2aaa7 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20   */.  int nCol; 
2aaa8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2aaa9 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
2aaaa 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65  olumns in the re
2aaab 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 45 78  sult set */.  Ex
2aaac 70 72 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20  pr *p;          
2aaad 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
2aaae 72 65 73 73 69 6f 6e 20 66 6f 72 20 61 20 73 69  ression for a si
2aaaf 6e 67 6c 65 20 72 65 73 75 6c 74 20 63 6f 6c 75  ngle result colu
2aab0 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e  mn */.  char *zN
2aab1 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
2aab2 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61      /* Column na
2aab3 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d  me */.  int nNam
2aab4 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
2aab5 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6e      /* Size of n
2aab6 61 6d 65 20 69 6e 20 7a 4e 61 6d 65 5b 5d 20 2a  ame in zName[] *
2aab7 2f 0a 0a 20 20 2a 70 6e 43 6f 6c 20 3d 20 6e 43  /..  *pnCol = nC
2aab8 6f 6c 20 3d 20 70 45 4c 69 73 74 2d 3e 6e 45 78  ol = pEList->nEx
2aab9 70 72 3b 0a 20 20 61 43 6f 6c 20 3d 20 2a 70 61  pr;.  aCol = *pa
2aaba 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  Col = sqlite3DbM
2aabb 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
2aabc 7a 65 6f 66 28 61 43 6f 6c 5b 30 5d 29 2a 6e 43  zeof(aCol[0])*nC
2aabd 6f 6c 29 3b 0a 20 20 69 66 28 20 61 43 6f 6c 3d  ol);.  if( aCol=
2aabe 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  =0 ) return SQLI
2aabf 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 66 6f 72 28  TE_NOMEM;.  for(
2aac0 69 3d 30 2c 20 70 43 6f 6c 3d 61 43 6f 6c 3b 20  i=0, pCol=aCol; 
2aac1 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f  i<nCol; i++, pCo
2aac2 6c 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 47 65 74  l++){.    /* Get
2aac3 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20   an appropriate 
2aac4 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 63 6f 6c  name for the col
2aac5 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  umn.    */.    p
2aac6 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e   = pEList->a[i].
2aac7 70 45 78 70 72 3b 0a 20 20 20 20 61 73 73 65 72  pExpr;.    asser
2aac8 74 28 20 70 2d 3e 70 52 69 67 68 74 3d 3d 30 20  t( p->pRight==0 
2aac9 7c 7c 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  || ExprHasProper
2aaca 74 79 28 70 2d 3e 70 52 69 67 68 74 2c 20 45 50  ty(p->pRight, EP
2aacb 5f 49 6e 74 56 61 6c 75 65 29 0a 20 20 20 20 20  _IntValue).     
2aacc 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e            || p->
2aacd 70 52 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e  pRight->u.zToken
2aace 3d 3d 30 20 7c 7c 20 70 2d 3e 70 52 69 67 68 74  ==0 || p->pRight
2aacf 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 21 3d 30  ->u.zToken[0]!=0
2aad0 20 29 3b 0a 20 20 20 20 69 66 28 20 28 7a 4e 61   );.    if( (zNa
2aad1 6d 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69  me = pEList->a[i
2aad2 5d 2e 7a 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20  ].zName)!=0 ){. 
2aad3 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63       /* If the c
2aad4 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 61  olumn contains a
2aad5 6e 20 22 41 53 20 3c 6e 61 6d 65 3e 22 20 70 68  n "AS <name>" ph
2aad6 72 61 73 65 2c 20 75 73 65 20 3c 6e 61 6d 65 3e  rase, use <name>
2aad7 20 61 73 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a   as the name */.
2aad8 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71        zName = sq
2aad9 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
2aada 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65  , zName);.    }e
2aadb 6c 73 65 7b 0a 20 20 20 20 20 20 45 78 70 72 20  lse{.      Expr 
2aadc 2a 70 43 6f 6c 45 78 70 72 20 3d 20 70 3b 20 20  *pColExpr = p;  
2aadd 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f  /* The expressio
2aade 6e 20 74 68 61 74 20 69 73 20 74 68 65 20 72 65  n that is the re
2aadf 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  sult column name
2aae0 20 2a 2f 0a 20 20 20 20 20 20 54 61 62 6c 65 20   */.      Table 
2aae1 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 2f  *pTab;         /
2aae2 2a 20 54 61 62 6c 65 20 61 73 73 6f 63 69 61 74  * Table associat
2aae3 65 64 20 77 69 74 68 20 74 68 69 73 20 65 78 70  ed with this exp
2aae4 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  ression */.     
2aae5 20 77 68 69 6c 65 28 20 70 43 6f 6c 45 78 70 72   while( pColExpr
2aae6 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29 20 70  ->op==TK_DOT ) p
2aae7 43 6f 6c 45 78 70 72 20 3d 20 70 43 6f 6c 45 78  ColExpr = pColEx
2aae8 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20  pr->pRight;.    
2aae9 20 20 69 66 28 20 70 43 6f 6c 45 78 70 72 2d 3e    if( pColExpr->
2aaea 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26  op==TK_COLUMN &&
2aaeb 20 41 4c 57 41 59 53 28 70 43 6f 6c 45 78 70 72   ALWAYS(pColExpr
2aaec 2d 3e 70 54 61 62 21 3d 30 29 20 29 7b 0a 20 20  ->pTab!=0) ){.  
2aaed 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 63 6f 6c        /* For col
2aaee 75 6d 6e 73 20 75 73 65 20 74 68 65 20 63 6f 6c  umns use the col
2aaef 75 6d 6e 20 6e 61 6d 65 20 6e 61 6d 65 20 2a 2f  umn name name */
2aaf0 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f  .        int iCo
2aaf1 6c 20 3d 20 70 43 6f 6c 45 78 70 72 2d 3e 69 43  l = pColExpr->iC
2aaf2 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 70  olumn;.        p
2aaf3 54 61 62 20 3d 20 70 43 6f 6c 45 78 70 72 2d 3e  Tab = pColExpr->
2aaf4 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 69 66  pTab;.        if
2aaf5 28 20 69 43 6f 6c 3c 30 20 29 20 69 43 6f 6c 20  ( iCol<0 ) iCol 
2aaf6 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20  = pTab->iPKey;. 
2aaf7 20 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73         zName = s
2aaf8 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
2aaf9 2c 20 22 25 73 22 2c 0a 20 20 20 20 20 20 20 20  , "%s",.        
2aafa 20 20 20 20 20 20 20 20 20 69 43 6f 6c 3e 3d 30           iCol>=0
2aafb 20 3f 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43   ? pTab->aCol[iC
2aafc 6f 6c 5d 2e 7a 4e 61 6d 65 20 3a 20 22 72 6f 77  ol].zName : "row
2aafd 69 64 22 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  id");.      }els
2aafe 65 20 69 66 28 20 70 43 6f 6c 45 78 70 72 2d 3e  e if( pColExpr->
2aaff 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 20  op==TK_ID ){.   
2ab00 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
2ab01 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 43  prHasProperty(pC
2ab02 6f 6c 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61  olExpr, EP_IntVa
2ab03 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20  lue) );.        
2ab04 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d  zName = sqlite3M
2ab05 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 22 2c  Printf(db, "%s",
2ab06 20 70 43 6f 6c 45 78 70 72 2d 3e 75 2e 7a 54 6f   pColExpr->u.zTo
2ab07 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ken);.      }els
2ab08 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 55 73  e{.        /* Us
2ab09 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74  e the original t
2ab0a 65 78 74 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  ext of the colum
2ab0b 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 61 73 20  n expression as 
2ab0c 69 74 73 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20  its name */.    
2ab0d 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69      zName = sqli
2ab0e 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
2ab0f 25 73 22 2c 20 70 45 4c 69 73 74 2d 3e 61 5b 69  %s", pEList->a[i
2ab10 5d 2e 7a 53 70 61 6e 29 3b 0a 20 20 20 20 20 20  ].zSpan);.      
2ab11 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
2ab12 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2ab13 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2ab14 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d  3DbFree(db, zNam
2ab15 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  e);.      break;
2ab16 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4d  .    }..    /* M
2ab17 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 6f 6c  ake sure the col
2ab18 75 6d 6e 20 6e 61 6d 65 20 69 73 20 75 6e 69 71  umn name is uniq
2ab19 75 65 2e 20 20 49 66 20 74 68 65 20 6e 61 6d 65  ue.  If the name
2ab1a 20 69 73 20 6e 6f 74 20 75 6e 69 71 75 65 2c 0a   is not unique,.
2ab1b 20 20 20 20 2a 2a 20 61 70 70 65 6e 64 20 61 20      ** append a 
2ab1c 69 6e 74 65 67 65 72 20 74 6f 20 74 68 65 20 6e  integer to the n
2ab1d 61 6d 65 20 73 6f 20 74 68 61 74 20 69 74 20 62  ame so that it b
2ab1e 65 63 6f 6d 65 73 20 75 6e 69 71 75 65 2e 0a 20  ecomes unique.. 
2ab1f 20 20 20 2a 2f 0a 20 20 20 20 6e 4e 61 6d 65 20     */.    nName 
2ab20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
2ab21 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 66 6f  0(zName);.    fo
2ab22 72 28 6a 3d 63 6e 74 3d 30 3b 20 6a 3c 69 3b 20  r(j=cnt=0; j<i; 
2ab23 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  j++){.      if( 
2ab24 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 61  sqlite3StrICmp(a
2ab25 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e  Col[j].zName, zN
2ab26 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ame)==0 ){.     
2ab27 20 20 20 63 68 61 72 20 2a 7a 4e 65 77 4e 61 6d     char *zNewNam
2ab28 65 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d 65  e;.        zName
2ab29 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b 0a 20 20 20  [nName] = 0;.   
2ab2a 20 20 20 20 20 7a 4e 65 77 4e 61 6d 65 20 3d 20       zNewName = 
2ab2b 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
2ab2c 62 2c 20 22 25 73 3a 25 64 22 2c 20 7a 4e 61 6d  b, "%s:%d", zNam
2ab2d 65 2c 20 2b 2b 63 6e 74 29 3b 0a 20 20 20 20 20  e, ++cnt);.     
2ab2e 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2ab2f 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  (db, zName);.   
2ab30 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 7a 4e 65       zName = zNe
2ab31 77 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 6a  wName;.        j
2ab32 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 69   = -1;.        i
2ab33 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 62 72  f( zName==0 ) br
2ab34 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2ab35 20 7d 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61   }.    pCol->zNa
2ab36 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 7d 0a  me = zName;.  }.
2ab37 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
2ab38 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 66 6f  Failed ){.    fo
2ab39 72 28 6a 3d 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29  r(j=0; j<i; j++)
2ab3a 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
2ab3b 62 46 72 65 65 28 64 62 2c 20 61 43 6f 6c 5b 6a  bFree(db, aCol[j
2ab3c 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a  ].zName);.    }.
2ab3d 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2ab3e 65 28 64 62 2c 20 61 43 6f 6c 29 3b 0a 20 20 20  e(db, aCol);.   
2ab3f 20 2a 70 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20   *paCol = 0;.   
2ab40 20 2a 70 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20   *pnCol = 0;.   
2ab41 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
2ab42 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  OMEM;.  }.  retu
2ab43 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
2ab44 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 79 70 65 20  ./*.** Add type 
2ab45 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 6e  and collation in
2ab46 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 61 20 63  formation to a c
2ab47 6f 6c 75 6d 6e 20 6c 69 73 74 20 62 61 73 65 64  olumn list based
2ab48 20 6f 6e 0a 2a 2a 20 61 20 53 45 4c 45 43 54 20   on.** a SELECT 
2ab49 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 0a 2a  statement..** .*
2ab4a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73  * The column lis
2ab4b 74 20 70 72 65 73 75 6d 61 62 6c 79 20 63 61 6d  t presumably cam
2ab4c 65 20 66 72 6f 6d 20 73 65 6c 65 63 74 43 6f 6c  e from selectCol
2ab4d 75 6d 6e 4e 61 6d 65 73 46 72 6f 6d 45 78 70 72  umnNamesFromExpr
2ab4e 4c 69 73 74 28 29 2e 0a 2a 2a 20 54 68 65 20 63  List()..** The c
2ab4f 6f 6c 75 6d 6e 20 6c 69 73 74 20 68 61 73 20 6f  olumn list has o
2ab50 6e 6c 79 20 6e 61 6d 65 73 2c 20 6e 6f 74 20 74  nly names, not t
2ab51 79 70 65 73 20 6f 72 20 63 6f 6c 6c 61 74 69 6f  ypes or collatio
2ab52 6e 73 2e 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75  ns.  This.** rou
2ab53 74 69 6e 65 20 67 6f 65 73 20 74 68 72 6f 75 67  tine goes throug
2ab54 68 20 61 6e 64 20 61 64 64 73 20 74 68 65 20 74  h and adds the t
2ab55 79 70 65 73 20 61 6e 64 20 63 6f 6c 6c 61 74 69  ypes and collati
2ab56 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ons..**.** This 
2ab57 72 6f 75 74 69 6e 65 20 72 65 71 75 69 72 65 73  routine requires
2ab58 20 74 68 61 74 20 61 6c 6c 20 69 64 65 6e 74 69   that all identi
2ab59 66 69 65 72 73 20 69 6e 20 74 68 65 20 53 45 4c  fiers in the SEL
2ab5a 45 43 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  ECT.** statement
2ab5b 20 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a 2f   be resolved..*/
2ab5c 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 6c  .static void sel
2ab5d 65 63 74 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65  ectAddColumnType
2ab5e 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20  AndCollation(.  
2ab5f 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2ab60 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
2ab61 20 63 6f 6e 74 65 78 74 73 20 2a 2f 0a 20 20 69   contexts */.  i
2ab62 6e 74 20 6e 43 6f 6c 2c 20 20 20 20 20 20 20 20  nt nCol,        
2ab63 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2ab64 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 43  f columns */.  C
2ab65 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 2c 20 20 20 20  olumn *aCol,    
2ab66 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
2ab67 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 53 65 6c  columns */.  Sel
2ab68 65 63 74 20 2a 70 53 65 6c 65 63 74 20 20 20 20  ect *pSelect    
2ab69 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 75 73 65     /* SELECT use
2ab6a 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74  d to determine t
2ab6b 79 70 65 73 20 61 6e 64 20 63 6f 6c 6c 61 74 69  ypes and collati
2ab6c 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  ons */.){.  sqli
2ab6d 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2ab6e 2d 3e 64 62 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74  ->db;.  NameCont
2ab6f 65 78 74 20 73 4e 43 3b 0a 20 20 43 6f 6c 75 6d  ext sNC;.  Colum
2ab70 6e 20 2a 70 43 6f 6c 3b 0a 20 20 43 6f 6c 6c 53  n *pCol;.  CollS
2ab71 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 6e 74  eq *pColl;.  int
2ab72 20 69 3b 0a 20 20 45 78 70 72 20 2a 70 3b 0a 20   i;.  Expr *p;. 
2ab73 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
2ab74 5f 69 74 65 6d 20 2a 61 3b 0a 0a 20 20 61 73 73  _item *a;..  ass
2ab75 65 72 74 28 20 70 53 65 6c 65 63 74 21 3d 30 20  ert( pSelect!=0 
2ab76 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 53  );.  assert( (pS
2ab77 65 6c 65 63 74 2d 3e 73 65 6c 46 6c 61 67 73 20  elect->selFlags 
2ab78 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 29 21 3d  & SF_Resolved)!=
2ab79 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
2ab7a 43 6f 6c 3d 3d 70 53 65 6c 65 63 74 2d 3e 70 45  Col==pSelect->pE
2ab7b 4c 69 73 74 2d 3e 6e 45 78 70 72 20 7c 7c 20 64  List->nExpr || d
2ab7c 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2ab7d 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  );.  if( db->mal
2ab7e 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75  locFailed ) retu
2ab7f 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e  rn;.  memset(&sN
2ab80 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43  C, 0, sizeof(sNC
2ab81 29 29 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c 69  ));.  sNC.pSrcLi
2ab82 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53  st = pSelect->pS
2ab83 72 63 3b 0a 20 20 61 20 3d 20 70 53 65 6c 65 63  rc;.  a = pSelec
2ab84 74 2d 3e 70 45 4c 69 73 74 2d 3e 61 3b 0a 20 20  t->pEList->a;.  
2ab85 66 6f 72 28 69 3d 30 2c 20 70 43 6f 6c 3d 61 43  for(i=0, pCol=aC
2ab86 6f 6c 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 2c  ol; i<nCol; i++,
2ab87 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 70 20   pCol++){.    p 
2ab88 3d 20 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  = a[i].pExpr;.  
2ab89 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20    pCol->zType = 
2ab8a 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
2ab8b 64 62 2c 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26  db, columnType(&
2ab8c 73 4e 43 2c 20 70 2c 20 30 2c 20 30 2c 20 30 29  sNC, p, 0, 0, 0)
2ab8d 29 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 61 66 66  );.    pCol->aff
2ab8e 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33 45  inity = sqlite3E
2ab8f 78 70 72 41 66 66 69 6e 69 74 79 28 70 29 3b 0a  xprAffinity(p);.
2ab90 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e 61 66      if( pCol->af
2ab91 66 69 6e 69 74 79 3d 3d 30 20 29 20 70 43 6f 6c  finity==0 ) pCol
2ab92 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c  ->affinity = SQL
2ab93 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20  ITE_AFF_NONE;.  
2ab94 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
2ab95 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61  3ExprCollSeq(pPa
2ab96 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 69 66 28  rse, p);.    if(
2ab97 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20   pColl ){.      
2ab98 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 20 3d 20 73 71  pCol->zColl = sq
2ab99 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
2ab9a 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b  , pColl->zName);
2ab9b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
2ab9c 0a 2a 2a 20 47 69 76 65 6e 20 61 20 53 45 4c 45  .** Given a SELE
2ab9d 43 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 67 65  CT statement, ge
2ab9e 6e 65 72 61 74 65 20 61 20 54 61 62 6c 65 20 73  nerate a Table s
2ab9f 74 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65  tructure that de
2aba0 73 63 72 69 62 65 73 0a 2a 2a 20 74 68 65 20 72  scribes.** the r
2aba1 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 61  esult set of tha
2aba2 74 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a 53 51 4c  t SELECT..*/.SQL
2aba3 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c  ITE_PRIVATE Tabl
2aba4 65 20 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74  e *sqlite3Result
2aba5 53 65 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73  SetOfSelect(Pars
2aba6 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63  e *pParse, Selec
2aba7 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20 54  t *pSelect){.  T
2aba8 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 73 71  able *pTab;.  sq
2aba9 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2abaa 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 73 61  se->db;.  int sa
2abab 76 65 64 46 6c 61 67 73 3b 0a 0a 20 20 73 61 76  vedFlags;..  sav
2abac 65 64 46 6c 61 67 73 20 3d 20 64 62 2d 3e 66 6c  edFlags = db->fl
2abad 61 67 73 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73  ags;.  db->flags
2abae 20 26 3d 20 7e 53 51 4c 49 54 45 5f 46 75 6c 6c   &= ~SQLITE_Full
2abaf 43 6f 6c 4e 61 6d 65 73 3b 0a 20 20 64 62 2d 3e  ColNames;.  db->
2abb0 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f  flags |= SQLITE_
2abb1 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 3b 0a 20  ShortColNames;. 
2abb2 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72   sqlite3SelectPr
2abb3 65 70 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65  ep(pParse, pSele
2abb4 63 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 50  ct, 0);.  if( pP
2abb5 61 72 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74  arse->nErr ) ret
2abb6 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20  urn 0;.  while( 
2abb7 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 20  pSelect->pPrior 
2abb8 29 20 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c  ) pSelect = pSel
2abb9 65 63 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 64  ect->pPrior;.  d
2abba 62 2d 3e 66 6c 61 67 73 20 3d 20 73 61 76 65 64  b->flags = saved
2abbb 46 6c 61 67 73 3b 0a 20 20 70 54 61 62 20 3d 20  Flags;.  pTab = 
2abbc 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
2abbd 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54  ero(db, sizeof(T
2abbe 61 62 6c 65 29 20 29 3b 0a 20 20 69 66 28 20 70  able) );.  if( p
2abbf 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  Tab==0 ){.    re
2abc0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 2f 2a  turn 0;.  }.  /*
2abc1 20 54 68 65 20 73 71 6c 69 74 65 33 52 65 73 75   The sqlite3Resu
2abc2 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 29 20  ltSetOfSelect() 
2abc3 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 6e 20 63  is only used n c
2abc4 6f 6e 74 65 78 74 73 20 77 68 65 72 65 20 6c 6f  ontexts where lo
2abc5 6f 6b 61 73 69 64 65 0a 20 20 2a 2a 20 69 73 20  okaside.  ** is 
2abc6 64 69 73 61 62 6c 65 64 2c 20 73 6f 20 77 65 20  disabled, so we 
2abc7 6d 69 67 68 74 20 61 73 20 77 65 6c 6c 20 68 61  might as well ha
2abc8 72 64 2d 63 6f 64 65 20 70 54 61 62 2d 3e 64 62  rd-code pTab->db
2abc9 4d 65 6d 20 74 6f 20 4e 55 4c 4c 2e 20 2a 2f 0a  Mem to NULL. */.
2abca 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6c 6f    assert( db->lo
2abcb 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
2abcc 3d 3d 30 20 29 3b 0a 20 20 70 54 61 62 2d 3e 64  ==0 );.  pTab->d
2abcd 62 4d 65 6d 20 3d 20 30 3b 0a 20 20 70 54 61 62  bMem = 0;.  pTab
2abce 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 54  ->nRef = 1;.  pT
2abcf 61 62 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20  ab->zName = 0;. 
2abd0 20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 73 46 72   selectColumnsFr
2abd1 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72 73  omExprList(pPars
2abd2 65 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69  e, pSelect->pELi
2abd3 73 74 2c 20 26 70 54 61 62 2d 3e 6e 43 6f 6c 2c  st, &pTab->nCol,
2abd4 20 26 70 54 61 62 2d 3e 61 43 6f 6c 29 3b 0a 20   &pTab->aCol);. 
2abd5 20 73 65 6c 65 63 74 41 64 64 43 6f 6c 75 6d 6e   selectAddColumn
2abd6 54 79 70 65 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e  TypeAndCollation
2abd7 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 6e  (pParse, pTab->n
2abd8 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 2c  Col, pTab->aCol,
2abd9 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 70 54 61   pSelect);.  pTa
2abda 62 2d 3e 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20  b->iPKey = -1;. 
2abdb 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
2abdc 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c  ailed ){.    sql
2abdd 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
2abde 70 54 61 62 29 3b 0a 20 20 20 20 72 65 74 75 72  pTab);.    retur
2abdf 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 0;.  }.  retur
2abe0 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pTab;.}../*.**
2abe1 20 47 65 74 20 61 20 56 44 42 45 20 66 6f 72 20   Get a VDBE for 
2abe2 74 68 65 20 67 69 76 65 6e 20 70 61 72 73 65 72  the given parser
2abe3 20 63 6f 6e 74 65 78 74 2e 20 20 43 72 65 61 74   context.  Creat
2abe4 65 20 61 20 6e 65 77 20 6f 6e 65 20 69 66 20 6e  e a new one if n
2abe5 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 49 66 20  ecessary..** If 
2abe6 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
2abe7 20 72 65 74 75 72 6e 20 4e 55 4c 4c 20 61 6e 64   return NULL and
2abe8 20 6c 65 61 76 65 20 61 20 6d 65 73 73 61 67 65   leave a message
2abe9 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53   in pParse..*/.S
2abea 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64  QLITE_PRIVATE Vd
2abeb 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56 64  be *sqlite3GetVd
2abec 62 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  be(Parse *pParse
2abed 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
2abee 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
2abef 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20  if( v==0 ){.    
2abf0 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2abf1 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  e = sqlite3VdbeC
2abf2 72 65 61 74 65 28 70 50 61 72 73 65 2d 3e 64 62  reate(pParse->db
2abf3 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
2abf4 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 20  E_OMIT_TRACE.   
2abf5 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 20 20   if( v ){.      
2abf6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2abf7 30 28 76 2c 20 4f 50 5f 54 72 61 63 65 29 3b 0a  0(v, OP_Trace);.
2abf8 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
2abf9 0a 20 20 72 65 74 75 72 6e 20 76 3b 0a 7d 0a 0a  .  return v;.}..
2abfa 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74  ./*.** Compute t
2abfb 68 65 20 69 4c 69 6d 69 74 20 61 6e 64 20 69 4f  he iLimit and iO
2abfc 66 66 73 65 74 20 66 69 65 6c 64 73 20 6f 66 20  ffset fields of 
2abfd 74 68 65 20 53 45 4c 45 43 54 20 62 61 73 65 64  the SELECT based
2abfe 20 6f 6e 20 74 68 65 0a 2a 2a 20 70 4c 69 6d 69   on the.** pLimi
2abff 74 20 61 6e 64 20 70 4f 66 66 73 65 74 20 65 78  t and pOffset ex
2ac00 70 72 65 73 73 69 6f 6e 73 2e 20 20 70 4c 69 6d  pressions.  pLim
2ac01 69 74 20 61 6e 64 20 70 4f 66 66 73 65 74 20 68  it and pOffset h
2ac02 6f 6c 64 20 74 68 65 20 65 78 70 72 65 73 73 69  old the expressi
2ac03 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 61 70 70 65  ons.** that appe
2ac04 61 72 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e  ar in the origin
2ac05 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  al SQL statement
2ac06 20 61 66 74 65 72 20 74 68 65 20 4c 49 4d 49 54   after the LIMIT
2ac07 20 61 6e 64 20 4f 46 46 53 45 54 0a 2a 2a 20 6b   and OFFSET.** k
2ac08 65 79 77 6f 72 64 73 2e 20 20 4f 72 20 4e 55 4c  eywords.  Or NUL
2ac09 4c 20 69 66 20 74 68 6f 73 65 20 6b 65 79 77 6f  L if those keywo
2ac0a 72 64 73 20 61 72 65 20 6f 6d 69 74 74 65 64 2e  rds are omitted.
2ac0b 20 69 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66 66   iLimit and iOff
2ac0c 73 65 74 20 0a 2a 2a 20 61 72 65 20 74 68 65 20  set .** are the 
2ac0d 69 6e 74 65 67 65 72 20 6d 65 6d 6f 72 79 20 72  integer memory r
2ac0e 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 73 20  egister numbers 
2ac0f 66 6f 72 20 63 6f 75 6e 74 65 72 73 20 75 73 65  for counters use
2ac10 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 0a 2a 2a  d to compute .**
2ac11 20 74 68 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f   the limit and o
2ac12 66 66 73 65 74 2e 20 20 49 66 20 74 68 65 72 65  ffset.  If there
2ac13 20 69 73 20 6e 6f 20 6c 69 6d 69 74 20 61 6e 64   is no limit and
2ac14 2f 6f 72 20 6f 66 66 73 65 74 2c 20 74 68 65 6e  /or offset, then
2ac15 20 0a 2a 2a 20 69 4c 69 6d 69 74 20 61 6e 64 20   .** iLimit and 
2ac16 69 4f 66 66 73 65 74 20 61 72 65 20 6e 65 67 61  iOffset are nega
2ac17 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tive..**.** This
2ac18 20 72 6f 75 74 69 6e 65 20 63 68 61 6e 67 65 73   routine changes
2ac19 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 69   the values of i
2ac1a 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66 66 73 65  Limit and iOffse
2ac1b 74 20 6f 6e 6c 79 20 69 66 0a 2a 2a 20 61 20 6c  t only if.** a l
2ac1c 69 6d 69 74 20 6f 72 20 6f 66 66 73 65 74 20 69  imit or offset i
2ac1d 73 20 64 65 66 69 6e 65 64 20 62 79 20 70 4c 69  s defined by pLi
2ac1e 6d 69 74 20 61 6e 64 20 70 4f 66 66 73 65 74 2e  mit and pOffset.
2ac1f 20 20 69 4c 69 6d 69 74 20 61 6e 64 0a 2a 2a 20    iLimit and.** 
2ac20 69 4f 66 66 73 65 74 20 73 68 6f 75 6c 64 20 68  iOffset should h
2ac21 61 76 65 20 62 65 65 6e 20 70 72 65 73 65 74 20  ave been preset 
2ac22 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65 20 64  to appropriate d
2ac23 65 66 61 75 6c 74 20 76 61 6c 75 65 73 0a 2a 2a  efault values.**
2ac24 20 28 75 73 75 61 6c 6c 79 20 62 75 74 20 6e 6f   (usually but no
2ac25 74 20 61 6c 77 61 79 73 20 2d 31 29 20 70 72 69  t always -1) pri
2ac26 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68  or to calling th
2ac27 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 20 4f  is routine..** O
2ac28 6e 6c 79 20 69 66 20 70 4c 69 6d 69 74 21 3d 30  nly if pLimit!=0
2ac29 20 6f 72 20 70 4f 66 66 73 65 74 21 3d 30 20 64   or pOffset!=0 d
2ac2a 6f 20 74 68 65 20 6c 69 6d 69 74 20 72 65 67 69  o the limit regi
2ac2b 73 74 65 72 73 20 67 65 74 0a 2a 2a 20 72 65 64  sters get.** red
2ac2c 65 66 69 6e 65 64 2e 20 20 54 68 65 20 55 4e 49  efined.  The UNI
2ac2d 4f 4e 20 41 4c 4c 20 6f 70 65 72 61 74 6f 72 20  ON ALL operator 
2ac2e 75 73 65 73 20 74 68 69 73 20 70 72 6f 70 65 72  uses this proper
2ac2f 74 79 20 74 6f 20 66 6f 72 63 65 0a 2a 2a 20 74  ty to force.** t
2ac30 68 65 20 72 65 75 73 65 20 6f 66 20 74 68 65 20  he reuse of the 
2ac31 73 61 6d 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f  same limit and o
2ac32 66 66 73 65 74 20 72 65 67 69 73 74 65 72 73 20  ffset registers 
2ac33 61 63 72 6f 73 73 20 6d 75 6c 74 69 70 6c 65 0a  across multiple.
2ac34 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  ** SELECT statem
2ac35 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ents..*/.static 
2ac36 76 6f 69 64 20 63 6f 6d 70 75 74 65 4c 69 6d 69  void computeLimi
2ac37 74 52 65 67 69 73 74 65 72 73 28 50 61 72 73 65  tRegisters(Parse
2ac38 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74   *pParse, Select
2ac39 20 2a 70 2c 20 69 6e 74 20 69 42 72 65 61 6b 29   *p, int iBreak)
2ac3a 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 30 3b  {.  Vdbe *v = 0;
2ac3b 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 20 3d 20  .  int iLimit = 
2ac3c 30 3b 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74  0;.  int iOffset
2ac3d 3b 0a 20 20 69 6e 74 20 61 64 64 72 31 3b 0a 20  ;.  int addr1;. 
2ac3e 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 29   if( p->iLimit )
2ac3f 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 0a   return;..  /* .
2ac40 20 20 2a 2a 20 22 4c 49 4d 49 54 20 2d 31 22 20    ** "LIMIT -1" 
2ac41 61 6c 77 61 79 73 20 73 68 6f 77 73 20 61 6c 6c  always shows all
2ac42 20 72 6f 77 73 2e 20 20 54 68 65 72 65 20 69 73   rows.  There is
2ac43 20 73 6f 6d 65 0a 20 20 2a 2a 20 63 6f 6e 74 72   some.  ** contr
2ac44 61 76 65 72 73 79 20 61 62 6f 75 74 20 77 68 61  aversy about wha
2ac45 74 20 74 68 65 20 63 6f 72 72 65 63 74 20 62 65  t the correct be
2ac46 68 61 76 69 6f 72 20 73 68 6f 75 6c 64 20 62 65  havior should be
2ac47 2e 0a 20 20 2a 2a 20 54 68 65 20 63 75 72 72 65  ..  ** The curre
2ac48 6e 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  nt implementatio
2ac49 6e 20 69 6e 74 65 72 70 72 65 74 73 20 22 4c 49  n interprets "LI
2ac4a 4d 49 54 20 30 22 20 74 6f 20 6d 65 61 6e 0a 20  MIT 0" to mean. 
2ac4b 20 2a 2a 20 6e 6f 20 72 6f 77 73 2e 0a 20 20 2a   ** no rows..  *
2ac4c 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43  /.  sqlite3ExprC
2ac4d 61 63 68 65 43 6c 65 61 72 28 70 50 61 72 73 65  acheClear(pParse
2ac4e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
2ac4f 70 4f 66 66 73 65 74 3d 3d 30 20 7c 7c 20 70 2d  pOffset==0 || p-
2ac50 3e 70 4c 69 6d 69 74 21 3d 30 20 29 3b 0a 20 20  >pLimit!=0 );.  
2ac51 69 66 28 20 70 2d 3e 70 4c 69 6d 69 74 20 29 7b  if( p->pLimit ){
2ac52 0a 20 20 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d  .    p->iLimit =
2ac53 20 69 4c 69 6d 69 74 20 3d 20 2b 2b 70 50 61 72   iLimit = ++pPar
2ac54 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 76 20  se->nMem;.    v 
2ac55 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
2ac56 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66  (pParse);.    if
2ac57 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20  ( NEVER(v==0) ) 
2ac58 72 65 74 75 72 6e 3b 20 20 2f 2a 20 56 44 42 45  return;  /* VDBE
2ac59 20 73 68 6f 75 6c 64 20 68 61 76 65 20 61 6c 72   should have alr
2ac5a 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61  eady been alloca
2ac5b 74 65 64 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  ted */.    sqlit
2ac5c 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
2ac5d 65 2c 20 70 2d 3e 70 4c 69 6d 69 74 2c 20 69 4c  e, p->pLimit, iL
2ac5e 69 6d 69 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  imit);.    sqlit
2ac5f 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2ac60 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 69 4c  OP_MustBeInt, iL
2ac61 69 6d 69 74 29 3b 0a 20 20 20 20 56 64 62 65 43  imit);.    VdbeC
2ac62 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4c 49 4d 49  omment((v, "LIMI
2ac63 54 20 63 6f 75 6e 74 65 72 22 29 29 3b 0a 20 20  T counter"));.  
2ac64 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2ac65 4f 70 32 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f  Op2(v, OP_IfZero
2ac66 2c 20 69 4c 69 6d 69 74 2c 20 69 42 72 65 61 6b  , iLimit, iBreak
2ac67 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4f  );.    if( p->pO
2ac68 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20 70  ffset ){.      p
2ac69 2d 3e 69 4f 66 66 73 65 74 20 3d 20 69 4f 66 66  ->iOffset = iOff
2ac6a 73 65 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  set = ++pParse->
2ac6b 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 70 50 61 72  nMem;.      pPar
2ac6c 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 20 20 20 2f 2a  se->nMem++;   /*
2ac6d 20 41 6c 6c 6f 63 61 74 65 20 61 6e 20 65 78 74   Allocate an ext
2ac6e 72 61 20 72 65 67 69 73 74 65 72 20 66 6f 72 20  ra register for 
2ac6f 6c 69 6d 69 74 2b 6f 66 66 73 65 74 20 2a 2f 0a  limit+offset */.
2ac70 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2ac71 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 2d  rCode(pParse, p-
2ac72 3e 70 4f 66 66 73 65 74 2c 20 69 4f 66 66 73 65  >pOffset, iOffse
2ac73 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
2ac74 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2ac75 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 69 4f 66  P_MustBeInt, iOf
2ac76 66 73 65 74 29 3b 0a 20 20 20 20 20 20 56 64 62  fset);.      Vdb
2ac77 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4f 46  eComment((v, "OF
2ac78 46 53 45 54 20 63 6f 75 6e 74 65 72 22 29 29 3b  FSET counter"));
2ac79 0a 20 20 20 20 20 20 61 64 64 72 31 20 3d 20 73  .      addr1 = s
2ac7a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2ac7b 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69 4f  (v, OP_IfPos, iO
2ac7c 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 73 71  ffset);.      sq
2ac7d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2ac7e 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
2ac7f 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20  , iOffset);.    
2ac80 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2ac81 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b  pHere(v, addr1);
2ac82 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2ac83 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41  beAddOp3(v, OP_A
2ac84 64 64 2c 20 69 4c 69 6d 69 74 2c 20 69 4f 66 66  dd, iLimit, iOff
2ac85 73 65 74 2c 20 69 4f 66 66 73 65 74 2b 31 29 3b  set, iOffset+1);
2ac86 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
2ac87 6e 74 28 28 76 2c 20 22 4c 49 4d 49 54 2b 4f 46  nt((v, "LIMIT+OF
2ac88 46 53 45 54 22 29 29 3b 0a 20 20 20 20 20 20 61  FSET"));.      a
2ac89 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64  ddr1 = sqlite3Vd
2ac8a 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
2ac8b 66 50 6f 73 2c 20 69 4c 69 6d 69 74 29 3b 0a 20  fPos, iLimit);. 
2ac8c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2ac8d 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2ac8e 65 67 65 72 2c 20 2d 31 2c 20 69 4f 66 66 73 65  eger, -1, iOffse
2ac8f 74 2b 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  t+1);.      sqli
2ac90 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2ac91 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 20 20 7d  v, addr1);.    }
2ac92 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  .  }.}..#ifndef 
2ac93 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50  SQLITE_OMIT_COMP
2ac94 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 2f 2a 0a 2a  OUND_SELECT./*.*
2ac95 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61 70 70  * Return the app
2ac96 72 6f 70 72 69 61 74 65 20 63 6f 6c 6c 61 74 69  ropriate collati
2ac97 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20  ng sequence for 
2ac98 74 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75  the iCol-th colu
2ac99 6d 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 72 65 73  mn of.** the res
2ac9a 75 6c 74 20 73 65 74 20 66 6f 72 20 74 68 65 20  ult set for the 
2ac9b 63 6f 6d 70 6f 75 6e 64 2d 73 65 6c 65 63 74 20  compound-select 
2ac9c 73 74 61 74 65 6d 65 6e 74 20 22 70 22 2e 20 20  statement "p".  
2ac9d 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 0a 2a  Return NULL if.*
2ac9e 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 68 61 73  * the column has
2ac9f 20 6e 6f 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c   no default coll
2aca0 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 0a  ating sequence..
2aca1 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c 6c 61 74  **.** The collat
2aca2 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72  ing sequence for
2aca3 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65   the compound se
2aca4 6c 65 63 74 20 69 73 20 74 61 6b 65 6e 20 66 72  lect is taken fr
2aca5 6f 6d 20 74 68 65 0a 2a 2a 20 6c 65 66 74 2d 6d  om the.** left-m
2aca6 6f 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ost term of the 
2aca7 73 65 6c 65 63 74 20 74 68 61 74 20 68 61 73 20  select that has 
2aca8 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  a collating sequ
2aca9 65 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ence..*/.static 
2acaa 43 6f 6c 6c 53 65 71 20 2a 6d 75 6c 74 69 53 65  CollSeq *multiSe
2acab 6c 65 63 74 43 6f 6c 6c 53 65 71 28 50 61 72 73  lectCollSeq(Pars
2acac 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63  e *pParse, Selec
2acad 74 20 2a 70 2c 20 69 6e 74 20 69 43 6f 6c 29 7b  t *p, int iCol){
2acae 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 52 65 74  .  CollSeq *pRet
2acaf 3b 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f  ;.  if( p->pPrio
2acb0 72 20 29 7b 0a 20 20 20 20 70 52 65 74 20 3d 20  r ){.    pRet = 
2acb1 6d 75 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53  multiSelectCollS
2acb2 65 71 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 50  eq(pParse, p->pP
2acb3 72 69 6f 72 2c 20 69 43 6f 6c 29 3b 0a 20 20 7d  rior, iCol);.  }
2acb4 65 6c 73 65 7b 0a 20 20 20 20 70 52 65 74 20 3d  else{.    pRet =
2acb5 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74   0;.  }.  assert
2acb6 28 20 69 43 6f 6c 3e 3d 30 20 29 3b 0a 20 20 69  ( iCol>=0 );.  i
2acb7 66 28 20 70 52 65 74 3d 3d 30 20 26 26 20 69 43  f( pRet==0 && iC
2acb8 6f 6c 3c 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  ol<p->pEList->nE
2acb9 78 70 72 20 29 7b 0a 20 20 20 20 70 52 65 74 20  xpr ){.    pRet 
2acba 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c  = sqlite3ExprCol
2acbb 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 2d 3e  lSeq(pParse, p->
2acbc 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e  pEList->a[iCol].
2acbd 70 45 78 70 72 29 3b 0a 20 20 7d 0a 20 20 72 65  pExpr);.  }.  re
2acbe 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 23 65 6e  turn pRet;.}.#en
2acbf 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2acc0 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  IT_COMPOUND_SELE
2acc1 43 54 20 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72  CT */../* Forwar
2acc2 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73  d reference */.s
2acc3 74 61 74 69 63 20 69 6e 74 20 6d 75 6c 74 69 53  tatic int multiS
2acc4 65 6c 65 63 74 4f 72 64 65 72 42 79 28 0a 20 20  electOrderBy(.  
2acc5 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2acc6 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
2acc7 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65   context */.  Se
2acc8 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20  lect *p,        
2acc9 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74      /* The right
2acca 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 73  -most of SELECTs
2accb 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a   to be coded */.
2accc 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44    SelectDest *pD
2accd 65 73 74 20 20 20 20 20 2f 2a 20 57 68 61 74 20  est     /* What 
2acce 74 6f 20 64 6f 20 77 69 74 68 20 71 75 65 72 79  to do with query
2accf 20 72 65 73 75 6c 74 73 20 2a 2f 0a 29 3b 0a 0a   results */.);..
2acd0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2acd1 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  OMIT_COMPOUND_SE
2acd2 4c 45 43 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  LECT./*.** This 
2acd3 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
2acd4 64 20 74 6f 20 70 72 6f 63 65 73 73 20 61 20 63  d to process a c
2acd5 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79 20 66 6f  ompound query fo
2acd6 72 6d 20 66 72 6f 6d 0a 2a 2a 20 74 77 6f 20 6f  rm from.** two o
2acd7 72 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65 20  r more separate 
2acd8 71 75 65 72 69 65 73 20 75 73 69 6e 67 20 55 4e  queries using UN
2acd9 49 4f 4e 2c 20 55 4e 49 4f 4e 20 41 4c 4c 2c 20  ION, UNION ALL, 
2acda 45 58 43 45 50 54 2c 20 6f 72 0a 2a 2a 20 49 4e  EXCEPT, or.** IN
2acdb 54 45 52 53 45 43 54 0a 2a 2a 0a 2a 2a 20 22 70  TERSECT.**.** "p
2acdc 22 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  " points to the 
2acdd 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20 74 68  right-most of th
2acde 65 20 74 77 6f 20 71 75 65 72 69 65 73 2e 20 20  e two queries.  
2acdf 74 68 65 20 71 75 65 72 79 20 6f 6e 20 74 68 65  the query on the
2ace0 0a 2a 2a 20 6c 65 66 74 20 69 73 20 70 2d 3e 70  .** left is p->p
2ace1 50 72 69 6f 72 2e 20 20 54 68 65 20 6c 65 66 74  Prior.  The left
2ace2 20 71 75 65 72 79 20 63 6f 75 6c 64 20 61 6c 73   query could als
2ace3 6f 20 62 65 20 61 20 63 6f 6d 70 6f 75 6e 64 20  o be a compound 
2ace4 71 75 65 72 79 0a 2a 2a 20 69 6e 20 77 68 69 63  query.** in whic
2ace5 68 20 63 61 73 65 20 74 68 69 73 20 72 6f 75 74  h case this rout
2ace6 69 6e 65 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c  ine will be call
2ace7 65 64 20 72 65 63 75 72 73 69 76 65 6c 79 2e 20  ed recursively. 
2ace8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c  .**.** The resul
2ace9 74 73 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 20  ts of the total 
2acea 71 75 65 72 79 20 61 72 65 20 74 6f 20 62 65 20  query are to be 
2aceb 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 64  written into a d
2acec 65 73 74 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6f 66  estination.** of
2aced 20 74 79 70 65 20 65 44 65 73 74 20 77 69 74 68   type eDest with
2acee 20 70 61 72 61 6d 65 74 65 72 20 69 50 61 72 6d   parameter iParm
2acef 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 20  ..**.** Example 
2acf0 31 3a 20 20 43 6f 6e 73 69 64 65 72 20 61 20 74  1:  Consider a t
2acf1 68 72 65 65 2d 77 61 79 20 63 6f 6d 70 6f 75 6e  hree-way compoun
2acf2 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  d SQL statement.
2acf3 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43  .**.**     SELEC
2acf4 54 20 61 20 46 52 4f 4d 20 74 31 20 55 4e 49 4f  T a FROM t1 UNIO
2acf5 4e 20 53 45 4c 45 43 54 20 62 20 46 52 4f 4d 20  N SELECT b FROM 
2acf6 74 32 20 55 4e 49 4f 4e 20 53 45 4c 45 43 54 20  t2 UNION SELECT 
2acf7 63 20 46 52 4f 4d 20 74 33 0a 2a 2a 0a 2a 2a 20  c FROM t3.**.** 
2acf8 54 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 69  This statement i
2acf9 73 20 70 61 72 73 65 64 20 75 70 20 61 73 20 66  s parsed up as f
2acfa 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
2acfb 20 20 53 45 4c 45 43 54 20 63 20 46 52 4f 4d 20    SELECT c FROM 
2acfc 74 33 0a 2a 2a 20 20 20 20 20 20 7c 0a 2a 2a 20  t3.**      |.** 
2acfd 20 20 20 20 20 60 2d 2d 2d 2d 2d 3e 20 20 53 45       `----->  SE
2acfe 4c 45 43 54 20 62 20 46 52 4f 4d 20 74 32 0a 2a  LECT b FROM t2.*
2acff 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2ad00 20 7c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   |.**           
2ad01 20 20 20 20 20 60 2d 2d 2d 2d 2d 2d 3e 20 20 53       `------>  S
2ad02 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 74 31 0a  ELECT a FROM t1.
2ad03 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 72 6f 77 73  **.** The arrows
2ad04 20 69 6e 20 74 68 65 20 64 69 61 67 72 61 6d 20   in the diagram 
2ad05 61 62 6f 76 65 20 72 65 70 72 65 73 65 6e 74 20  above represent 
2ad06 74 68 65 20 53 65 6c 65 63 74 2e 70 50 72 69 6f  the Select.pPrio
2ad07 72 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 53 6f  r pointer..** So
2ad08 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   if this routine
2ad09 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68 20   is called with 
2ad0a 70 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 74  p equal to the t
2ad0b 33 20 71 75 65 72 79 2c 20 74 68 65 6e 0a 2a 2a  3 query, then.**
2ad0c 20 70 50 72 69 6f 72 20 77 69 6c 6c 20 62 65 20   pPrior will be 
2ad0d 74 68 65 20 74 32 20 71 75 65 72 79 2e 20 20 70  the t2 query.  p
2ad0e 2d 3e 6f 70 20 77 69 6c 6c 20 62 65 20 54 4b 5f  ->op will be TK_
2ad0f 55 4e 49 4f 4e 20 69 6e 20 74 68 69 73 20 63 61  UNION in this ca
2ad10 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 69 63 65  se..**.** Notice
2ad11 20 74 68 61 74 20 62 65 63 61 75 73 65 20 6f 66   that because of
2ad12 20 74 68 65 20 77 61 79 20 53 51 4c 69 74 65 20   the way SQLite 
2ad13 70 61 72 73 65 73 20 63 6f 6d 70 6f 75 6e 64 20  parses compound 
2ad14 53 45 4c 45 43 54 73 2c 20 74 68 65 0a 2a 2a 20  SELECTs, the.** 
2ad15 69 6e 64 69 76 69 64 75 61 6c 20 73 65 6c 65 63  individual selec
2ad16 74 73 20 61 6c 77 61 79 73 20 67 72 6f 75 70 20  ts always group 
2ad17 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67  from left to rig
2ad18 68 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ht..*/.static in
2ad19 74 20 6d 75 6c 74 69 53 65 6c 65 63 74 28 0a 20  t multiSelect(. 
2ad1a 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2ad1b 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
2ad1c 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  g context */.  S
2ad1d 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20  elect *p,       
2ad1e 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68       /* The righ
2ad1f 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43 54  t-most of SELECT
2ad20 73 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f  s to be coded */
2ad21 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70  .  SelectDest *p
2ad22 44 65 73 74 20 20 20 20 20 2f 2a 20 57 68 61 74  Dest     /* What
2ad23 20 74 6f 20 64 6f 20 77 69 74 68 20 71 75 65 72   to do with quer
2ad24 79 20 72 65 73 75 6c 74 73 20 2a 2f 0a 29 7b 0a  y results */.){.
2ad25 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
2ad26 45 5f 4f 4b 3b 20 20 20 2f 2a 20 53 75 63 63 65  E_OK;   /* Succe
2ad27 73 73 20 63 6f 64 65 20 66 72 6f 6d 20 61 20 73  ss code from a s
2ad28 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 53  ubroutine */.  S
2ad29 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 3b 20 20  elect *pPrior;  
2ad2a 20 20 20 20 20 2f 2a 20 41 6e 6f 74 68 65 72 20       /* Another 
2ad2b 53 45 4c 45 43 54 20 69 6d 6d 65 64 69 61 74 65  SELECT immediate
2ad2c 6c 79 20 74 6f 20 6f 75 72 20 6c 65 66 74 20 2a  ly to our left *
2ad2d 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  /.  Vdbe *v;    
2ad2e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e            /* Gen
2ad2f 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 74 68  erate code to th
2ad30 69 73 20 56 44 42 45 20 2a 2f 0a 20 20 53 65 6c  is VDBE */.  Sel
2ad31 65 63 74 44 65 73 74 20 64 65 73 74 3b 20 20 20  ectDest dest;   
2ad32 20 20 20 2f 2a 20 41 6c 74 65 72 6e 61 74 69 76     /* Alternativ
2ad33 65 20 64 61 74 61 20 64 65 73 74 69 6e 61 74 69  e data destinati
2ad34 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  on */.  Select *
2ad35 70 44 65 6c 65 74 65 20 3d 20 30 3b 20 20 2f 2a  pDelete = 0;  /*
2ad36 20 43 68 61 69 6e 20 6f 66 20 73 69 6d 70 6c 65   Chain of simple
2ad37 20 73 65 6c 65 63 74 73 20 74 6f 20 64 65 6c 65   selects to dele
2ad38 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  te */.  sqlite3 
2ad39 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  *db;          /*
2ad3a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
2ad3b 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 4d 61  tion */..  /* Ma
2ad3c 6b 65 20 73 75 72 65 20 74 68 65 72 65 20 69 73  ke sure there is
2ad3d 20 6e 6f 20 4f 52 44 45 52 20 42 59 20 6f 72 20   no ORDER BY or 
2ad3e 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 6f 6e 20  LIMIT clause on 
2ad3f 70 72 69 6f 72 20 53 45 4c 45 43 54 73 2e 20 20  prior SELECTs.  
2ad40 4f 6e 6c 79 0a 20 20 2a 2a 20 74 68 65 20 6c 61  Only.  ** the la
2ad41 73 74 20 28 72 69 67 68 74 2d 6d 6f 73 74 29 20  st (right-most) 
2ad42 53 45 4c 45 43 54 20 69 6e 20 74 68 65 20 73 65  SELECT in the se
2ad43 72 69 65 73 20 6d 61 79 20 68 61 76 65 20 61 6e  ries may have an
2ad44 20 4f 52 44 45 52 20 42 59 20 6f 72 20 4c 49 4d   ORDER BY or LIM
2ad45 49 54 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  IT..  */.  asser
2ad46 74 28 20 70 20 26 26 20 70 2d 3e 70 50 72 69 6f  t( p && p->pPrio
2ad47 72 20 29 3b 20 20 2f 2a 20 43 61 6c 6c 69 6e 67  r );  /* Calling
2ad48 20 66 75 6e 63 74 69 6f 6e 20 67 75 61 72 61 6e   function guaran
2ad49 74 65 65 73 20 74 68 69 73 20 6d 75 63 68 20 2a  tees this much *
2ad4a 2f 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  /.  db = pParse-
2ad4b 3e 64 62 3b 0a 20 20 70 50 72 69 6f 72 20 3d 20  >db;.  pPrior = 
2ad4c 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 61 73 73  p->pPrior;.  ass
2ad4d 65 72 74 28 20 70 50 72 69 6f 72 2d 3e 70 52 69  ert( pPrior->pRi
2ad4e 67 68 74 6d 6f 73 74 21 3d 70 50 72 69 6f 72 20  ghtmost!=pPrior 
2ad4f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72  );.  assert( pPr
2ad50 69 6f 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74 3d  ior->pRightmost=
2ad51 3d 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 29  =p->pRightmost )
2ad52 3b 0a 20 20 64 65 73 74 20 3d 20 2a 70 44 65 73  ;.  dest = *pDes
2ad53 74 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72 2d  t;.  if( pPrior-
2ad54 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20  >pOrderBy ){.   
2ad55 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2ad56 28 70 50 61 72 73 65 2c 22 4f 52 44 45 52 20 42  (pParse,"ORDER B
2ad57 59 20 63 6c 61 75 73 65 20 73 68 6f 75 6c 64 20  Y clause should 
2ad58 63 6f 6d 65 20 61 66 74 65 72 20 25 73 20 6e 6f  come after %s no
2ad59 74 20 62 65 66 6f 72 65 22 2c 0a 20 20 20 20 20  t before",.     
2ad5a 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70 2d   selectOpName(p-
2ad5b 3e 6f 70 29 29 3b 0a 20 20 20 20 72 63 20 3d 20  >op));.    rc = 
2ad5c 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74  1;.    goto mult
2ad5d 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  i_select_end;.  
2ad5e 7d 0a 20 20 69 66 28 20 70 50 72 69 6f 72 2d 3e  }.  if( pPrior->
2ad5f 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 73 71  pLimit ){.    sq
2ad60 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2ad61 61 72 73 65 2c 22 4c 49 4d 49 54 20 63 6c 61 75  arse,"LIMIT clau
2ad62 73 65 20 73 68 6f 75 6c 64 20 63 6f 6d 65 20 61  se should come a
2ad63 66 74 65 72 20 25 73 20 6e 6f 74 20 62 65 66 6f  fter %s not befo
2ad64 72 65 22 2c 0a 20 20 20 20 20 20 73 65 6c 65 63  re",.      selec
2ad65 74 4f 70 4e 61 6d 65 28 70 2d 3e 6f 70 29 29 3b  tOpName(p->op));
2ad66 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20  .    rc = 1;.   
2ad67 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65   goto multi_sele
2ad68 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 20 20 76  ct_end;.  }..  v
2ad69 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
2ad6a 65 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73  e(pParse);.  ass
2ad6b 65 72 74 28 20 76 21 3d 30 20 29 3b 20 20 2f 2a  ert( v!=0 );  /*
2ad6c 20 54 68 65 20 56 44 42 45 20 61 6c 72 65 61 64   The VDBE alread
2ad6d 79 20 63 72 65 61 74 65 64 20 62 79 20 63 61 6c  y created by cal
2ad6e 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  ling function */
2ad6f 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68  ..  /* Create th
2ad70 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 74 65  e destination te
2ad71 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 69 66  mporary table if
2ad72 20 6e 65 63 65 73 73 61 72 79 0a 20 20 2a 2f 0a   necessary.  */.
2ad73 20 20 69 66 28 20 64 65 73 74 2e 65 44 65 73 74    if( dest.eDest
2ad74 3d 3d 53 52 54 5f 45 70 68 65 6d 54 61 62 20 29  ==SRT_EphemTab )
2ad75 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  {.    assert( p-
2ad76 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 73  >pEList );.    s
2ad77 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2ad78 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d  (v, OP_OpenEphem
2ad79 65 72 61 6c 2c 20 64 65 73 74 2e 69 50 61 72 6d  eral, dest.iParm
2ad7a 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78  , p->pEList->nEx
2ad7b 70 72 29 3b 0a 20 20 20 20 64 65 73 74 2e 65 44  pr);.    dest.eD
2ad7c 65 73 74 20 3d 20 53 52 54 5f 54 61 62 6c 65 3b  est = SRT_Table;
2ad7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  .  }..  /* Make 
2ad7e 73 75 72 65 20 61 6c 6c 20 53 45 4c 45 43 54 73  sure all SELECTs
2ad7f 20 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e   in the statemen
2ad80 74 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20  t have the same 
2ad81 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e  number of elemen
2ad82 74 73 0a 20 20 2a 2a 20 69 6e 20 74 68 65 69 72  ts.  ** in their
2ad83 20 72 65 73 75 6c 74 20 73 65 74 73 2e 0a 20 20   result sets..  
2ad84 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  */.  assert( p->
2ad85 70 45 4c 69 73 74 20 26 26 20 70 50 72 69 6f 72  pEList && pPrior
2ad86 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 69 66  ->pEList );.  if
2ad87 28 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78  ( p->pEList->nEx
2ad88 70 72 21 3d 70 50 72 69 6f 72 2d 3e 70 45 4c 69  pr!=pPrior->pELi
2ad89 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20  st->nExpr ){.   
2ad8a 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2ad8b 28 70 50 61 72 73 65 2c 20 22 53 45 4c 45 43 54  (pParse, "SELECT
2ad8c 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 61 6e  s to the left an
2ad8d 64 20 72 69 67 68 74 20 6f 66 20 25 73 22 0a 20  d right of %s". 
2ad8e 20 20 20 20 20 22 20 64 6f 20 6e 6f 74 20 68 61       " do not ha
2ad8f 76 65 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62  ve the same numb
2ad90 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c  er of result col
2ad91 75 6d 6e 73 22 2c 20 73 65 6c 65 63 74 4f 70 4e  umns", selectOpN
2ad92 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a 20 20 20  ame(p->op));.   
2ad93 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74   rc = 1;.    got
2ad94 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65  o multi_select_e
2ad95 6e 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f  nd;.  }..  /* Co
2ad96 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 73 20 74  mpound SELECTs t
2ad97 68 61 74 20 68 61 76 65 20 61 6e 20 4f 52 44 45  hat have an ORDE
2ad98 52 20 42 59 20 63 6c 61 75 73 65 20 61 72 65 20  R BY clause are 
2ad99 68 61 6e 64 6c 65 64 20 73 65 70 61 72 61 74 65  handled separate
2ad9a 6c 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ly..  */.  if( p
2ad9b 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ->pOrderBy ){.  
2ad9c 20 20 72 65 74 75 72 6e 20 6d 75 6c 74 69 53 65    return multiSe
2ad9d 6c 65 63 74 4f 72 64 65 72 42 79 28 70 50 61 72  lectOrderBy(pPar
2ad9e 73 65 2c 20 70 2c 20 70 44 65 73 74 29 3b 0a 20  se, p, pDest);. 
2ad9f 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74   }..  /* Generat
2ada0 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 6c  e code for the l
2ada1 65 66 74 20 61 6e 64 20 72 69 67 68 74 20 53 45  eft and right SE
2ada2 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e  LECT statements.
2ada3 0a 20 20 2a 2f 0a 20 20 73 77 69 74 63 68 28 20  .  */.  switch( 
2ada4 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73  p->op ){.    cas
2ada5 65 20 54 4b 5f 41 4c 4c 3a 20 7b 0a 20 20 20 20  e TK_ALL: {.    
2ada6 20 20 69 6e 74 20 61 64 64 72 20 3d 20 30 3b 0a    int addr = 0;.
2ada7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
2ada8 50 72 69 6f 72 2d 3e 70 4c 69 6d 69 74 20 29 3b  Prior->pLimit );
2ada9 0a 20 20 20 20 20 20 70 50 72 69 6f 72 2d 3e 70  .      pPrior->p
2adaa 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69  Limit = p->pLimi
2adab 74 3b 0a 20 20 20 20 20 20 70 50 72 69 6f 72 2d  t;.      pPrior-
2adac 3e 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f  >pOffset = p->pO
2adad 66 66 73 65 74 3b 0a 20 20 20 20 20 20 72 63 20  ffset;.      rc 
2adae 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  = sqlite3Select(
2adaf 70 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c 20  pParse, pPrior, 
2adb0 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20 70 2d  &dest);.      p-
2adb1 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20  >pLimit = 0;.   
2adb2 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20     p->pOffset = 
2adb3 30 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20  0;.      if( rc 
2adb4 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  ){.        goto 
2adb5 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64  multi_select_end
2adb6 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2adb7 70 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20  p->pPrior = 0;. 
2adb8 20 20 20 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d       p->iLimit =
2adb9 20 70 50 72 69 6f 72 2d 3e 69 4c 69 6d 69 74 3b   pPrior->iLimit;
2adba 0a 20 20 20 20 20 20 70 2d 3e 69 4f 66 66 73 65  .      p->iOffse
2adbb 74 20 3d 20 70 50 72 69 6f 72 2d 3e 69 4f 66 66  t = pPrior->iOff
2adbc 73 65 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70  set;.      if( p
2adbd 2d 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20  ->iLimit ){.    
2adbe 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
2adbf 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2adc0 4f 50 5f 49 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c  OP_IfZero, p->iL
2adc1 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 20 20 56  imit);.        V
2adc2 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
2adc3 4a 75 6d 70 20 61 68 65 61 64 20 69 66 20 4c 49  Jump ahead if LI
2adc4 4d 49 54 20 72 65 61 63 68 65 64 22 29 29 3b 0a  MIT reached"));.
2adc5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
2adc6 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
2adc7 28 70 50 61 72 73 65 2c 20 70 2c 20 26 64 65 73  (pParse, p, &des
2adc8 74 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  t);.      testca
2adc9 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  se( rc!=SQLITE_O
2adca 4b 20 29 3b 0a 20 20 20 20 20 20 70 44 65 6c 65  K );.      pDele
2adcb 74 65 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a  te = p->pPrior;.
2adcc 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20        p->pPrior 
2adcd 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20  = pPrior;.      
2adce 69 66 28 20 61 64 64 72 20 29 7b 0a 20 20 20 20  if( addr ){.    
2adcf 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
2add0 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29  umpHere(v, addr)
2add1 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2add2 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
2add3 20 63 61 73 65 20 54 4b 5f 45 58 43 45 50 54 3a   case TK_EXCEPT:
2add4 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 4e 49  .    case TK_UNI
2add5 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  ON: {.      int 
2add6 75 6e 69 6f 6e 54 61 62 3b 20 20 20 20 2f 2a 20  unionTab;    /* 
2add7 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66  Cursor number of
2add8 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74   the temporary t
2add9 61 62 6c 65 20 68 6f 6c 64 69 6e 67 20 72 65 73  able holding res
2adda 75 6c 74 20 2a 2f 0a 20 20 20 20 20 20 75 38 20  ult */.      u8 
2addb 6f 70 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a  op = 0;       /*
2addc 20 4f 6e 65 20 6f 66 20 74 68 65 20 53 52 54 5f   One of the SRT_
2addd 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 6f 20 61   operations to a
2adde 70 70 6c 79 20 74 6f 20 73 65 6c 66 20 2a 2f 0a  pply to self */.
2addf 20 20 20 20 20 20 69 6e 74 20 70 72 69 6f 72 4f        int priorO
2ade0 70 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 53 52  p;     /* The SR
2ade1 54 5f 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20  T_ operation to 
2ade2 61 70 70 6c 79 20 74 6f 20 70 72 69 6f 72 20 73  apply to prior s
2ade3 65 6c 65 63 74 73 20 2a 2f 0a 20 20 20 20 20 20  elects */.      
2ade4 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20 2a 70  Expr *pLimit, *p
2ade5 4f 66 66 73 65 74 3b 20 2f 2a 20 53 61 76 65 64  Offset; /* Saved
2ade6 20 76 61 6c 75 65 73 20 6f 66 20 70 2d 3e 6e 4c   values of p->nL
2ade7 69 6d 69 74 20 61 6e 64 20 70 2d 3e 6e 4f 66 66  imit and p->nOff
2ade8 73 65 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  set */.      int
2ade9 20 61 64 64 72 3b 0a 20 20 20 20 20 20 53 65 6c   addr;.      Sel
2adea 65 63 74 44 65 73 74 20 75 6e 69 6f 6e 64 65 73  ectDest uniondes
2adeb 74 3b 0a 0a 20 20 20 20 20 20 74 65 73 74 63 61  t;..      testca
2adec 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 45 58  se( p->op==TK_EX
2aded 43 45 50 54 20 29 3b 0a 20 20 20 20 20 20 74 65  CEPT );.      te
2adee 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54  stcase( p->op==T
2adef 4b 5f 55 4e 49 4f 4e 20 29 3b 0a 20 20 20 20 20  K_UNION );.     
2adf0 20 70 72 69 6f 72 4f 70 20 3d 20 53 52 54 5f 55   priorOp = SRT_U
2adf1 6e 69 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28 20  nion;.      if( 
2adf2 64 65 73 74 2e 65 44 65 73 74 3d 3d 70 72 69 6f  dest.eDest==prio
2adf3 72 4f 70 20 26 26 20 41 4c 57 41 59 53 28 21 70  rOp && ALWAYS(!p
2adf4 2d 3e 70 4c 69 6d 69 74 20 26 26 21 70 2d 3e 70  ->pLimit &&!p->p
2adf5 4f 66 66 73 65 74 29 20 29 7b 0a 20 20 20 20 20  Offset) ){.     
2adf6 20 20 20 2f 2a 20 57 65 20 63 61 6e 20 72 65 75     /* We can reu
2adf7 73 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74  se a temporary t
2adf8 61 62 6c 65 20 67 65 6e 65 72 61 74 65 64 20 62  able generated b
2adf9 79 20 61 20 53 45 4c 45 43 54 20 74 6f 20 6f 75  y a SELECT to ou
2adfa 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 69 67  r.        ** rig
2adfb 68 74 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ht..        */. 
2adfc 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
2adfd 2d 3e 70 52 69 67 68 74 6d 6f 73 74 21 3d 70 20  ->pRightmost!=p 
2adfe 29 3b 20 20 2f 2a 20 43 61 6e 20 6f 6e 6c 79 20  );  /* Can only 
2adff 68 61 70 70 65 6e 20 66 6f 72 20 6c 65 66 74 77  happen for leftw
2ae00 61 72 64 20 65 6c 65 6d 65 6e 74 73 0a 20 20 20  ard elements.   
2ae01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ae03 20 20 2a 2a 20 6f 66 20 61 20 33 2d 77 61 79 20    ** of a 3-way 
2ae04 6f 72 20 6d 6f 72 65 20 63 6f 6d 70 6f 75 6e 64  or more compound
2ae05 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65   */.        asse
2ae06 72 74 28 20 70 2d 3e 70 4c 69 6d 69 74 3d 3d 30  rt( p->pLimit==0
2ae07 20 29 3b 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20   );      /* Not 
2ae08 61 6c 6c 6f 77 65 64 20 6f 6e 20 6c 65 66 74 77  allowed on leftw
2ae09 61 72 64 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a  ard elements */.
2ae0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2ae0b 70 2d 3e 70 4f 66 66 73 65 74 3d 3d 30 20 29 3b  p->pOffset==0 );
2ae0c 20 20 20 20 20 2f 2a 20 4e 6f 74 20 61 6c 6c 6f       /* Not allo
2ae0d 77 65 64 20 6f 6e 20 6c 65 66 74 77 61 72 64 20  wed on leftward 
2ae0e 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20  elements */.    
2ae0f 20 20 20 20 75 6e 69 6f 6e 54 61 62 20 3d 20 64      unionTab = d
2ae10 65 73 74 2e 69 50 61 72 6d 3b 0a 20 20 20 20 20  est.iParm;.     
2ae11 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2ae12 2f 2a 20 57 65 20 77 69 6c 6c 20 6e 65 65 64 20  /* We will need 
2ae13 74 6f 20 63 72 65 61 74 65 20 6f 75 72 20 6f 77  to create our ow
2ae14 6e 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  n temporary tabl
2ae15 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 20 20  e to hold the.  
2ae16 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 72 6d 65        ** interme
2ae17 64 69 61 74 65 20 72 65 73 75 6c 74 73 2e 0a 20  diate results.. 
2ae18 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
2ae19 20 20 75 6e 69 6f 6e 54 61 62 20 3d 20 70 50 61    unionTab = pPa
2ae1a 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
2ae1b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
2ae1c 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b 0a 20  pOrderBy==0 );. 
2ae1d 20 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71         addr = sq
2ae1e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2ae1f 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  v, OP_OpenEpheme
2ae20 72 61 6c 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 30  ral, unionTab, 0
2ae21 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
2ae22 74 28 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70  t( p->addrOpenEp
2ae23 68 6d 5b 30 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20  hm[0] == -1 );. 
2ae24 20 20 20 20 20 20 20 70 2d 3e 61 64 64 72 4f 70         p->addrOp
2ae25 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 61 64 64 72  enEphm[0] = addr
2ae26 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 52 69  ;.        p->pRi
2ae27 67 68 74 6d 6f 73 74 2d 3e 73 65 6c 46 6c 61 67  ghtmost->selFlag
2ae28 73 20 7c 3d 20 53 46 5f 55 73 65 73 45 70 68 65  s |= SF_UsesEphe
2ae29 6d 65 72 61 6c 3b 0a 20 20 20 20 20 20 20 20 61  meral;.        a
2ae2a 73 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74  ssert( p->pEList
2ae2b 20 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20   );.      }..   
2ae2c 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 53     /* Code the S
2ae2d 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73  ELECT statements
2ae2e 20 74 6f 20 6f 75 72 20 6c 65 66 74 0a 20 20 20   to our left.   
2ae2f 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65     */.      asse
2ae30 72 74 28 20 21 70 50 72 69 6f 72 2d 3e 70 4f 72  rt( !pPrior->pOr
2ae31 64 65 72 42 79 20 29 3b 0a 20 20 20 20 20 20 73  derBy );.      s
2ae32 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74  qlite3SelectDest
2ae33 49 6e 69 74 28 26 75 6e 69 6f 6e 64 65 73 74 2c  Init(&uniondest,
2ae34 20 70 72 69 6f 72 4f 70 2c 20 75 6e 69 6f 6e 54   priorOp, unionT
2ae35 61 62 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  ab);.      rc = 
2ae36 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
2ae37 61 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 26 75  arse, pPrior, &u
2ae38 6e 69 6f 6e 64 65 73 74 29 3b 0a 20 20 20 20 20  niondest);.     
2ae39 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
2ae3a 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65     goto multi_se
2ae3b 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20  lect_end;.      
2ae3c 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64 65  }..      /* Code
2ae3d 20 74 68 65 20 63 75 72 72 65 6e 74 20 53 45 4c   the current SEL
2ae3e 45 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 20 20  ECT statement.  
2ae3f 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
2ae40 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 43 45 50   p->op==TK_EXCEP
2ae41 54 20 29 7b 0a 20 20 20 20 20 20 20 20 6f 70 20  T ){.        op 
2ae42 3d 20 53 52 54 5f 45 78 63 65 70 74 3b 0a 20 20  = SRT_Except;.  
2ae43 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2ae44 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 70     assert( p->op
2ae45 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 29 3b 0a 20 20  ==TK_UNION );.  
2ae46 20 20 20 20 20 20 6f 70 20 3d 20 53 52 54 5f 55        op = SRT_U
2ae47 6e 69 6f 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20  nion;.      }.  
2ae48 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20      p->pPrior = 
2ae49 30 3b 0a 20 20 20 20 20 20 70 4c 69 6d 69 74 20  0;.      pLimit 
2ae4a 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20  = p->pLimit;.   
2ae4b 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 30     p->pLimit = 0
2ae4c 3b 0a 20 20 20 20 20 20 70 4f 66 66 73 65 74 20  ;.      pOffset 
2ae4d 3d 20 70 2d 3e 70 4f 66 66 73 65 74 3b 0a 20 20  = p->pOffset;.  
2ae4e 20 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d      p->pOffset =
2ae4f 20 30 3b 0a 20 20 20 20 20 20 75 6e 69 6f 6e 64   0;.      uniond
2ae50 65 73 74 2e 65 44 65 73 74 20 3d 20 6f 70 3b 0a  est.eDest = op;.
2ae51 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
2ae52 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
2ae53 20 70 2c 20 26 75 6e 69 6f 6e 64 65 73 74 29 3b   p, &uniondest);
2ae54 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2ae55 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2ae56 3b 0a 20 20 20 20 20 20 2f 2a 20 51 75 65 72 79  ;.      /* Query
2ae57 20 66 6c 61 74 74 65 6e 69 6e 67 20 69 6e 20 73   flattening in s
2ae58 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 20 6d  qlite3Select() m
2ae59 69 67 68 74 20 72 65 66 69 6c 6c 20 70 2d 3e 70  ight refill p->p
2ae5a 4f 72 64 65 72 42 79 2e 0a 20 20 20 20 20 20 2a  OrderBy..      *
2ae5b 2a 20 42 65 20 73 75 72 65 20 74 6f 20 64 65 6c  * Be sure to del
2ae5c 65 74 65 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c  ete p->pOrderBy,
2ae5d 20 74 68 65 72 65 66 6f 72 65 2c 20 74 6f 20 61   therefore, to a
2ae5e 76 6f 69 64 20 61 20 6d 65 6d 6f 72 79 20 6c 65  void a memory le
2ae5f 61 6b 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  ak. */.      sql
2ae60 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
2ae61 74 65 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72  te(db, p->pOrder
2ae62 42 79 29 3b 0a 20 20 20 20 20 20 70 44 65 6c 65  By);.      pDele
2ae63 74 65 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a  te = p->pPrior;.
2ae64 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20        p->pPrior 
2ae65 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20  = pPrior;.      
2ae66 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 30 3b  p->pOrderBy = 0;
2ae67 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2ae68 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e  prDelete(db, p->
2ae69 70 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 70  pLimit);.      p
2ae6a 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 4c 69 6d 69  ->pLimit = pLimi
2ae6b 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66 66  t;.      p->pOff
2ae6c 73 65 74 20 3d 20 70 4f 66 66 73 65 74 3b 0a 20  set = pOffset;. 
2ae6d 20 20 20 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d       p->iLimit =
2ae6e 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4f 66   0;.      p->iOf
2ae6f 66 73 65 74 20 3d 20 30 3b 0a 0a 20 20 20 20 20  fset = 0;..     
2ae70 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20   /* Convert the 
2ae71 64 61 74 61 20 69 6e 20 74 68 65 20 74 65 6d 70  data in the temp
2ae72 6f 72 61 72 79 20 74 61 62 6c 65 20 69 6e 74 6f  orary table into
2ae73 20 77 68 61 74 65 76 65 72 20 66 6f 72 6d 0a 20   whatever form. 
2ae74 20 20 20 20 20 2a 2a 20 69 74 20 69 73 20 74 68       ** it is th
2ae75 61 74 20 77 65 20 63 75 72 72 65 6e 74 6c 79 20  at we currently 
2ae76 6e 65 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  need..      */. 
2ae77 20 20 20 20 20 61 73 73 65 72 74 28 20 75 6e 69       assert( uni
2ae78 6f 6e 54 61 62 3d 3d 64 65 73 74 2e 69 50 61 72  onTab==dest.iPar
2ae79 6d 20 7c 7c 20 64 65 73 74 2e 65 44 65 73 74 21  m || dest.eDest!
2ae7a 3d 70 72 69 6f 72 4f 70 20 29 3b 0a 20 20 20 20  =priorOp );.    
2ae7b 20 20 69 66 28 20 64 65 73 74 2e 65 44 65 73 74    if( dest.eDest
2ae7c 21 3d 70 72 69 6f 72 4f 70 20 29 7b 0a 20 20 20  !=priorOp ){.   
2ae7d 20 20 20 20 20 69 6e 74 20 69 43 6f 6e 74 2c 20       int iCont, 
2ae7e 69 42 72 65 61 6b 2c 20 69 53 74 61 72 74 3b 0a  iBreak, iStart;.
2ae7f 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2ae80 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 20  p->pEList );.   
2ae81 20 20 20 20 20 69 66 28 20 64 65 73 74 2e 65 44       if( dest.eD
2ae82 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20  est==SRT_Output 
2ae83 29 7b 0a 20 20 20 20 20 20 20 20 20 20 53 65 6c  ){.          Sel
2ae84 65 63 74 20 2a 70 46 69 72 73 74 20 3d 20 70 3b  ect *pFirst = p;
2ae85 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65  .          while
2ae86 28 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72  ( pFirst->pPrior
2ae87 20 29 20 70 46 69 72 73 74 20 3d 20 70 46 69 72   ) pFirst = pFir
2ae88 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20  st->pPrior;.    
2ae89 20 20 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f        generateCo
2ae8a 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65  lumnNames(pParse
2ae8b 2c 20 30 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c  , 0, pFirst->pEL
2ae8c 69 73 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ist);.        }.
2ae8d 20 20 20 20 20 20 20 20 69 42 72 65 61 6b 20 3d          iBreak =
2ae8e 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
2ae8f 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20  Label(v);.      
2ae90 20 20 69 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65    iCont = sqlite
2ae91 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
2ae92 29 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75  );.        compu
2ae93 74 65 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73  teLimitRegisters
2ae94 28 70 50 61 72 73 65 2c 20 70 2c 20 69 42 72 65  (pParse, p, iBre
2ae95 61 6b 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ak);.        sql
2ae96 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2ae97 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 75 6e 69  , OP_Rewind, uni
2ae98 6f 6e 54 61 62 2c 20 69 42 72 65 61 6b 29 3b 0a  onTab, iBreak);.
2ae99 20 20 20 20 20 20 20 20 69 53 74 61 72 74 20 3d          iStart =
2ae9a 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
2ae9b 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20  entAddr(v);.    
2ae9c 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c      selectInnerL
2ae9d 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70  oop(pParse, p, p
2ae9e 2d 3e 70 45 4c 69 73 74 2c 20 75 6e 69 6f 6e 54  ->pEList, unionT
2ae9f 61 62 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e  ab, p->pEList->n
2aea0 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20 20 20  Expr,.          
2aea1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c                0,
2aea2 20 2d 31 2c 20 26 64 65 73 74 2c 20 69 43 6f 6e   -1, &dest, iCon
2aea3 74 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20  t, iBreak);.    
2aea4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
2aea5 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69  esolveLabel(v, i
2aea6 43 6f 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 73  Cont);.        s
2aea7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2aea8 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 75 6e 69  (v, OP_Next, uni
2aea9 6f 6e 54 61 62 2c 20 69 53 74 61 72 74 29 3b 0a  onTab, iStart);.
2aeaa 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2aeab 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
2aeac 76 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20  v, iBreak);.    
2aead 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2aeae 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp2(v, OP_Clos
2aeaf 65 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 30 29 3b  e, unionTab, 0);
2aeb0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
2aeb1 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2aeb2 64 65 66 61 75 6c 74 3a 20 61 73 73 65 72 74 28  default: assert(
2aeb3 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52   p->op==TK_INTER
2aeb4 53 45 43 54 20 29 3b 20 7b 0a 20 20 20 20 20 20  SECT ); {.      
2aeb5 69 6e 74 20 74 61 62 31 2c 20 74 61 62 32 3b 0a  int tab1, tab2;.
2aeb6 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6e 74 2c        int iCont,
2aeb7 20 69 42 72 65 61 6b 2c 20 69 53 74 61 72 74 3b   iBreak, iStart;
2aeb8 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 69  .      Expr *pLi
2aeb9 6d 69 74 2c 20 2a 70 4f 66 66 73 65 74 3b 0a 20  mit, *pOffset;. 
2aeba 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20       int addr;. 
2aebb 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20       SelectDest 
2aebc 69 6e 74 65 72 73 65 63 74 64 65 73 74 3b 0a 20  intersectdest;. 
2aebd 20 20 20 20 20 69 6e 74 20 72 31 3b 0a 0a 20 20       int r1;..  
2aebe 20 20 20 20 2f 2a 20 49 4e 54 45 52 53 45 43 54      /* INTERSECT
2aebf 20 69 73 20 64 69 66 66 65 72 65 6e 74 20 66 72   is different fr
2aec0 6f 6d 20 74 68 65 20 6f 74 68 65 72 73 20 73 69  om the others si
2aec1 6e 63 65 20 69 74 20 72 65 71 75 69 72 65 73 0a  nce it requires.
2aec2 20 20 20 20 20 20 2a 2a 20 74 77 6f 20 74 65 6d        ** two tem
2aec3 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20  porary tables.  
2aec4 48 65 6e 63 65 20 69 74 20 68 61 73 20 69 74 73  Hence it has its
2aec5 20 6f 77 6e 20 63 61 73 65 2e 20 20 42 65 67 69   own case.  Begi
2aec6 6e 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 61 6c  n.      ** by al
2aec7 6c 6f 63 61 74 69 6e 67 20 74 68 65 20 74 61 62  locating the tab
2aec8 6c 65 73 20 77 65 20 77 69 6c 6c 20 6e 65 65 64  les we will need
2aec9 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
2aeca 20 74 61 62 31 20 3d 20 70 50 61 72 73 65 2d 3e   tab1 = pParse->
2aecb 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 74 61  nTab++;.      ta
2aecc 62 32 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  b2 = pParse->nTa
2aecd 62 2b 2b 3b 0a 20 20 20 20 20 20 61 73 73 65 72  b++;.      asser
2aece 74 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 3d 3d  t( p->pOrderBy==
2aecf 30 20 29 3b 0a 0a 20 20 20 20 20 20 61 64 64 72  0 );..      addr
2aed0 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2aed1 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45  dOp2(v, OP_OpenE
2aed2 70 68 65 6d 65 72 61 6c 2c 20 74 61 62 31 2c 20  phemeral, tab1, 
2aed3 30 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  0);.      assert
2aed4 28 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  ( p->addrOpenEph
2aed5 6d 5b 30 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20  m[0] == -1 );.  
2aed6 20 20 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45      p->addrOpenE
2aed7 70 68 6d 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20  phm[0] = addr;. 
2aed8 20 20 20 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f       p->pRightmo
2aed9 73 74 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20  st->selFlags |= 
2aeda 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c  SF_UsesEphemeral
2aedb 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2aedc 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 0a 20 20  p->pEList );..  
2aedd 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20      /* Code the 
2aede 53 45 4c 45 43 54 73 20 74 6f 20 6f 75 72 20 6c  SELECTs to our l
2aedf 65 66 74 20 69 6e 74 6f 20 74 65 6d 70 6f 72 61  eft into tempora
2aee0 72 79 20 74 61 62 6c 65 20 22 74 61 62 31 22 2e  ry table "tab1".
2aee1 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2aee2 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73  sqlite3SelectDes
2aee3 74 49 6e 69 74 28 26 69 6e 74 65 72 73 65 63 74  tInit(&intersect
2aee4 64 65 73 74 2c 20 53 52 54 5f 55 6e 69 6f 6e 2c  dest, SRT_Union,
2aee5 20 74 61 62 31 29 3b 0a 20 20 20 20 20 20 72 63   tab1);.      rc
2aee6 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
2aee7 28 70 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c  (pParse, pPrior,
2aee8 20 26 69 6e 74 65 72 73 65 63 74 64 65 73 74 29   &intersectdest)
2aee9 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
2aeea 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d  {.        goto m
2aeeb 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b  ulti_select_end;
2aeec 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
2aeed 2f 2a 20 43 6f 64 65 20 74 68 65 20 63 75 72 72  /* Code the curr
2aeee 65 6e 74 20 53 45 4c 45 43 54 20 69 6e 74 6f 20  ent SELECT into 
2aeef 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
2aef0 22 74 61 62 32 22 0a 20 20 20 20 20 20 2a 2f 0a  "tab2".      */.
2aef1 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
2aef2 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2aef3 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
2aef4 61 6c 2c 20 74 61 62 32 2c 20 30 29 3b 0a 20 20  al, tab2, 0);.  
2aef5 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61      assert( p->a
2aef6 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d  ddrOpenEphm[1] =
2aef7 3d 20 2d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d  = -1 );.      p-
2aef8 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d  >addrOpenEphm[1]
2aef9 20 3d 20 61 64 64 72 3b 0a 20 20 20 20 20 20 70   = addr;.      p
2aefa 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20  ->pPrior = 0;.  
2aefb 20 20 20 20 70 4c 69 6d 69 74 20 3d 20 70 2d 3e      pLimit = p->
2aefc 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d  pLimit;.      p-
2aefd 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20  >pLimit = 0;.   
2aefe 20 20 20 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e     pOffset = p->
2aeff 70 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70  pOffset;.      p
2af00 2d 3e 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20  ->pOffset = 0;. 
2af01 20 20 20 20 20 69 6e 74 65 72 73 65 63 74 64 65       intersectde
2af02 73 74 2e 69 50 61 72 6d 20 3d 20 74 61 62 32 3b  st.iParm = tab2;
2af03 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
2af04 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
2af05 2c 20 70 2c 20 26 69 6e 74 65 72 73 65 63 74 64  , p, &intersectd
2af06 65 73 74 29 3b 0a 20 20 20 20 20 20 74 65 73 74  est);.      test
2af07 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45  case( rc!=SQLITE
2af08 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 70 44 65  _OK );.      pDe
2af09 6c 65 74 65 20 3d 20 70 2d 3e 70 50 72 69 6f 72  lete = p->pPrior
2af0a 3b 0a 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f  ;.      p->pPrio
2af0b 72 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20  r = pPrior;.    
2af0c 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
2af0d 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69  ete(db, p->pLimi
2af0e 74 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69  t);.      p->pLi
2af0f 6d 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20  mit = pLimit;.  
2af10 20 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d      p->pOffset =
2af11 20 70 4f 66 66 73 65 74 3b 0a 0a 20 20 20 20 20   pOffset;..     
2af12 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
2af13 65 20 74 6f 20 74 61 6b 65 20 74 68 65 20 69 6e  e to take the in
2af14 74 65 72 73 65 63 74 69 6f 6e 20 6f 66 20 74 68  tersection of th
2af15 65 20 74 77 6f 20 74 65 6d 70 6f 72 61 72 79 0a  e two temporary.
2af16 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 73 2e        ** tables.
2af17 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2af18 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73  assert( p->pELis
2af19 74 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 64  t );.      if( d
2af1a 65 73 74 2e 65 44 65 73 74 3d 3d 53 52 54 5f 4f  est.eDest==SRT_O
2af1b 75 74 70 75 74 20 29 7b 0a 20 20 20 20 20 20 20  utput ){.       
2af1c 20 53 65 6c 65 63 74 20 2a 70 46 69 72 73 74 20   Select *pFirst 
2af1d 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 77 68 69  = p;.        whi
2af1e 6c 65 28 20 70 46 69 72 73 74 2d 3e 70 50 72 69  le( pFirst->pPri
2af1f 6f 72 20 29 20 70 46 69 72 73 74 20 3d 20 70 46  or ) pFirst = pF
2af20 69 72 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20  irst->pPrior;.  
2af21 20 20 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f        generateCo
2af22 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65  lumnNames(pParse
2af23 2c 20 30 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c  , 0, pFirst->pEL
2af24 69 73 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ist);.      }.  
2af25 20 20 20 20 69 42 72 65 61 6b 20 3d 20 73 71 6c      iBreak = sql
2af26 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
2af27 6c 28 76 29 3b 0a 20 20 20 20 20 20 69 43 6f 6e  l(v);.      iCon
2af28 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  t = sqlite3VdbeM
2af29 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
2af2a 20 20 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52     computeLimitR
2af2b 65 67 69 73 74 65 72 73 28 70 50 61 72 73 65 2c  egisters(pParse,
2af2c 20 70 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20   p, iBreak);.   
2af2d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2af2e 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
2af2f 64 2c 20 74 61 62 31 2c 20 69 42 72 65 61 6b 29  d, tab1, iBreak)
2af30 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c  ;.      r1 = sql
2af31 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
2af32 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 69 53  Parse);.      iS
2af33 74 61 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64  tart = sqlite3Vd
2af34 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2af35 6f 77 4b 65 79 2c 20 74 61 62 31 2c 20 72 31 29  owKey, tab1, r1)
2af36 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2af37 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2af38 4e 6f 74 46 6f 75 6e 64 2c 20 74 61 62 32 2c 20  NotFound, tab2, 
2af39 69 43 6f 6e 74 2c 20 72 31 29 3b 0a 20 20 20 20  iCont, r1);.    
2af3a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2af3b 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
2af3c 72 31 29 3b 0a 20 20 20 20 20 20 73 65 6c 65 63  r1);.      selec
2af3d 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73  tInnerLoop(pPars
2af3e 65 2c 20 70 2c 20 70 2d 3e 70 45 4c 69 73 74 2c  e, p, p->pEList,
2af3f 20 74 61 62 31 2c 20 70 2d 3e 70 45 4c 69 73 74   tab1, p->pEList
2af40 2d 3e 6e 45 78 70 72 2c 0a 20 20 20 20 20 20 20  ->nExpr,.       
2af41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
2af42 2c 20 2d 31 2c 20 26 64 65 73 74 2c 20 69 43 6f  , -1, &dest, iCo
2af43 6e 74 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20  nt, iBreak);.   
2af44 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
2af45 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 43  solveLabel(v, iC
2af46 6f 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ont);.      sqli
2af47 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2af48 20 4f 50 5f 4e 65 78 74 2c 20 74 61 62 31 2c 20   OP_Next, tab1, 
2af49 69 53 74 61 72 74 29 3b 0a 20 20 20 20 20 20 73  iStart);.      s
2af4a 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
2af4b 65 4c 61 62 65 6c 28 76 2c 20 69 42 72 65 61 6b  eLabel(v, iBreak
2af4c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2af4d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2af4e 5f 43 6c 6f 73 65 2c 20 74 61 62 32 2c 20 30 29  _Close, tab2, 0)
2af4f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2af50 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2af51 43 6c 6f 73 65 2c 20 74 61 62 31 2c 20 30 29 3b  Close, tab1, 0);
2af52 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2af53 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f    }.  }..  /* Co
2af54 6d 70 75 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20  mpute collating 
2af55 73 65 71 75 65 6e 63 65 73 20 75 73 65 64 20 62  sequences used b
2af56 79 20 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72  y .  ** temporar
2af57 79 20 74 61 62 6c 65 73 20 6e 65 65 64 65 64 20  y tables needed 
2af58 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
2af59 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74   compound select
2af5a 2e 0a 20 20 2a 2a 20 41 74 74 61 63 68 20 74 68  ..  ** Attach th
2af5b 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  e KeyInfo struct
2af5c 75 72 65 20 74 6f 20 61 6c 6c 20 74 65 6d 70 6f  ure to all tempo
2af5d 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 20 20 2a  rary tables..  *
2af5e 2a 0a 20 20 2a 2a 20 54 68 69 73 20 73 65 63 74  *.  ** This sect
2af5f 69 6f 6e 20 69 73 20 72 75 6e 20 62 79 20 74 68  ion is run by th
2af60 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 53 45 4c  e right-most SEL
2af61 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e  ECT statement on
2af62 6c 79 2e 0a 20 20 2a 2a 20 53 45 4c 45 43 54 20  ly..  ** SELECT 
2af63 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 74 68  statements to th
2af64 65 20 6c 65 66 74 20 61 6c 77 61 79 73 20 73 6b  e left always sk
2af65 69 70 20 74 68 69 73 20 70 61 72 74 2e 20 20 54  ip this part.  T
2af66 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20  he right-most.  
2af67 2a 2a 20 53 45 4c 45 43 54 20 6d 69 67 68 74 20  ** SELECT might 
2af68 61 6c 73 6f 20 73 6b 69 70 20 74 68 69 73 20 70  also skip this p
2af69 61 72 74 20 69 66 20 69 74 20 68 61 73 20 6e 6f  art if it has no
2af6a 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2af6b 20 61 6e 64 0a 20 20 2a 2a 20 6e 6f 20 74 65 6d   and.  ** no tem
2af6c 70 20 74 61 62 6c 65 73 20 61 72 65 20 72 65 71  p tables are req
2af6d 75 69 72 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  uired..  */.  if
2af6e 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20  ( p->selFlags & 
2af6f 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c  SF_UsesEphemeral
2af70 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 20 20   ){.    int i;  
2af71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2af72 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
2af73 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 4b 65 79  unter */.    Key
2af74 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20  Info *pKeyInfo; 
2af75 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
2af76 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2af77 20 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 20   for the result 
2af78 73 65 74 20 2a 2f 0a 20 20 20 20 53 65 6c 65 63  set */.    Selec
2af79 74 20 2a 70 4c 6f 6f 70 3b 20 20 20 20 20 20 20  t *pLoop;       
2af7a 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
2af7b 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20  looping through 
2af7c 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2af7d 73 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 65 71  s */.    CollSeq
2af7e 20 2a 2a 61 70 43 6f 6c 6c 3b 20 20 20 20 20 20   **apColl;      
2af7f 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f         /* For lo
2af80 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20 70 4b  oping through pK
2af81 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 5d 20  eyInfo->aColl[] 
2af82 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b  */.    int nCol;
2af83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2af84 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2af85 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 72 65 73  f columns in res
2af86 75 6c 74 20 73 65 74 20 2a 2f 0a 0a 20 20 20 20  ult set */..    
2af87 61 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67 68  assert( p->pRigh
2af88 74 6d 6f 73 74 3d 3d 70 20 29 3b 0a 20 20 20 20  tmost==p );.    
2af89 6e 43 6f 6c 20 3d 20 70 2d 3e 70 45 4c 69 73 74  nCol = p->pEList
2af8a 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 70 4b 65  ->nExpr;.    pKe
2af8b 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44  yInfo = sqlite3D
2af8c 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 0a  bMallocZero(db,.
2af8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2af8e 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70         sizeof(*p
2af8f 4b 65 79 49 6e 66 6f 29 2b 6e 43 6f 6c 2a 28 73  KeyInfo)+nCol*(s
2af90 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 20  izeof(CollSeq*) 
2af91 2b 20 31 29 29 3b 0a 20 20 20 20 69 66 28 20 21  + 1));.    if( !
2af92 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20  pKeyInfo ){.    
2af93 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
2af94 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  MEM;.      goto 
2af95 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64  multi_select_end
2af96 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 4b 65  ;.    }..    pKe
2af97 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43  yInfo->enc = ENC
2af98 28 64 62 29 3b 0a 20 20 20 20 70 4b 65 79 49 6e  (db);.    pKeyIn
2af99 66 6f 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31  fo->nField = (u1
2af9a 36 29 6e 43 6f 6c 3b 0a 0a 20 20 20 20 66 6f 72  6)nCol;..    for
2af9b 28 69 3d 30 2c 20 61 70 43 6f 6c 6c 3d 70 4b 65  (i=0, apColl=pKe
2af9c 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 3b 20 69 3c  yInfo->aColl; i<
2af9d 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 61 70 43 6f 6c  nCol; i++, apCol
2af9e 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 2a 61 70 43  l++){.      *apC
2af9f 6f 6c 6c 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63  oll = multiSelec
2afa0 74 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  tCollSeq(pParse,
2afa1 20 70 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 66   p, i);.      if
2afa2 28 20 30 3d 3d 2a 61 70 43 6f 6c 6c 20 29 7b 0a  ( 0==*apColl ){.
2afa3 20 20 20 20 20 20 20 20 2a 61 70 43 6f 6c 6c 20          *apColl 
2afa4 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b  = db->pDfltColl;
2afa5 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
2afa6 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 3b      for(pLoop=p;
2afa7 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c   pLoop; pLoop=pL
2afa8 6f 6f 70 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20  oop->pPrior){.  
2afa9 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32      for(i=0; i<2
2afaa 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
2afab 69 6e 74 20 61 64 64 72 20 3d 20 70 4c 6f 6f 70  int addr = pLoop
2afac 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 69  ->addrOpenEphm[i
2afad 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61  ];.        if( a
2afae 64 64 72 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  ddr<0 ){.       
2afaf 20 20 20 2f 2a 20 49 66 20 5b 30 5d 20 69 73 20     /* If [0] is 
2afb0 75 6e 75 73 65 64 20 74 68 65 6e 20 5b 31 5d 20  unused then [1] 
2afb1 69 73 20 61 6c 73 6f 20 75 6e 75 73 65 64 2e 20  is also unused. 
2afb2 20 53 6f 20 77 65 20 63 61 6e 0a 20 20 20 20 20   So we can.     
2afb3 20 20 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 73       ** always s
2afb4 61 66 65 6c 79 20 61 62 6f 72 74 20 61 73 20 73  afely abort as s
2afb5 6f 6f 6e 20 61 73 20 74 68 65 20 66 69 72 73 74  oon as the first
2afb6 20 75 6e 75 73 65 64 20 73 6c 6f 74 20 69 73 20   unused slot is 
2afb7 66 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 20 20 20  found */.       
2afb8 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 6f 70     assert( pLoop
2afb9 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31  ->addrOpenEphm[1
2afba 5d 3c 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20  ]<0 );.         
2afbb 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
2afbc 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
2afbd 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c  3VdbeChangeP2(v,
2afbe 20 61 64 64 72 2c 20 6e 43 6f 6c 29 3b 0a 20 20   addr, nCol);.  
2afbf 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2afc0 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64  eChangeP4(v, add
2afc1 72 2c 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e  r, (char*)pKeyIn
2afc2 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b  fo, P4_KEYINFO);
2afc3 0a 20 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e  .        pLoop->
2afc4 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 69 5d 20  addrOpenEphm[i] 
2afc5 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  = -1;.      }.  
2afc6 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44    }.    sqlite3D
2afc7 62 46 72 65 65 28 64 62 2c 20 70 4b 65 79 49 6e  bFree(db, pKeyIn
2afc8 66 6f 29 3b 0a 20 20 7d 0a 0a 6d 75 6c 74 69 5f  fo);.  }..multi_
2afc9 73 65 6c 65 63 74 5f 65 6e 64 3a 0a 20 20 70 44  select_end:.  pD
2afca 65 73 74 2d 3e 69 4d 65 6d 20 3d 20 64 65 73 74  est->iMem = dest
2afcb 2e 69 4d 65 6d 3b 0a 20 20 70 44 65 73 74 2d 3e  .iMem;.  pDest->
2afcc 6e 4d 65 6d 20 3d 20 64 65 73 74 2e 6e 4d 65 6d  nMem = dest.nMem
2afcd 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ;.  sqlite3Selec
2afce 74 44 65 6c 65 74 65 28 64 62 2c 20 70 44 65 6c  tDelete(db, pDel
2afcf 65 74 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ete);.  return r
2afd0 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  c;.}.#endif /* S
2afd1 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f  QLITE_OMIT_COMPO
2afd2 55 4e 44 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a 2f  UND_SELECT */../
2afd3 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 6f 75 74  *.** Code an out
2afd4 70 75 74 20 73 75 62 72 6f 75 74 69 6e 65 20 66  put subroutine f
2afd5 6f 72 20 61 20 63 6f 72 6f 75 74 69 6e 65 20 69  or a coroutine i
2afd6 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
2afd7 20 61 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61   a.** SELECT sta
2afd8 74 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tment..**.** The
2afd9 20 64 61 74 61 20 74 6f 20 62 65 20 6f 75 74 70   data to be outp
2afda 75 74 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 20  ut is contained 
2afdb 69 6e 20 70 49 6e 2d 3e 69 4d 65 6d 2e 20 20 54  in pIn->iMem.  T
2afdc 68 65 72 65 20 61 72 65 0a 2a 2a 20 70 49 6e 2d  here are.** pIn-
2afdd 3e 6e 4d 65 6d 20 63 6f 6c 75 6d 6e 73 20 74 6f  >nMem columns to
2afde 20 62 65 20 6f 75 74 70 75 74 2e 20 20 70 44 65   be output.  pDe
2afdf 73 74 20 69 73 20 77 68 65 72 65 20 74 68 65 20  st is where the 
2afe0 6f 75 74 70 75 74 20 73 68 6f 75 6c 64 0a 2a 2a  output should.**
2afe1 20 62 65 20 73 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20   be sent..**.** 
2afe2 72 65 67 52 65 74 75 72 6e 20 69 73 20 74 68 65  regReturn is the
2afe3 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72   number of the r
2afe4 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20  egister holding 
2afe5 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65 0a 2a  the subroutine.*
2afe6 2a 20 72 65 74 75 72 6e 20 61 64 64 72 65 73 73  * return address
2afe7 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 65 67 50 72  ..**.** If regPr
2afe8 65 76 3e 30 20 74 68 65 6e 20 69 74 20 69 73 20  ev>0 then it is 
2afe9 61 20 74 68 65 20 66 69 72 73 74 20 72 65 67 69  a the first regi
2afea 73 74 65 72 20 69 6e 20 61 20 76 65 63 74 6f 72  ster in a vector
2afeb 20 74 68 61 74 0a 2a 2a 20 72 65 63 6f 72 64 73   that.** records
2afec 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 6f 75   the previous ou
2afed 74 70 75 74 2e 20 20 6d 65 6d 5b 72 65 67 50 72  tput.  mem[regPr
2afee 65 76 5d 20 69 73 20 61 20 66 6c 61 67 20 74 68  ev] is a flag th
2afef 61 74 20 69 73 20 66 61 6c 73 65 0a 2a 2a 20 69  at is false.** i
2aff0 66 20 74 68 65 72 65 20 68 61 73 20 62 65 65 6e  f there has been
2aff1 20 6e 6f 20 70 72 65 76 69 6f 75 73 20 6f 75 74   no previous out
2aff2 70 75 74 2e 20 20 49 66 20 72 65 67 50 72 65 76  put.  If regPrev
2aff3 3e 30 20 74 68 65 6e 20 63 6f 64 65 20 69 73 0a  >0 then code is.
2aff4 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20  ** generated to 
2aff5 73 75 70 70 72 65 73 73 20 64 75 70 6c 69 63 61  suppress duplica
2aff6 74 65 73 2e 20 20 70 4b 65 79 49 6e 66 6f 20 69  tes.  pKeyInfo i
2aff7 73 20 75 73 65 64 20 66 6f 72 20 63 6f 6d 70 61  s used for compa
2aff8 72 69 6e 67 0a 2a 2a 20 6b 65 79 73 2e 0a 2a 2a  ring.** keys..**
2aff9 0a 2a 2a 20 49 66 20 74 68 65 20 4c 49 4d 49 54  .** If the LIMIT
2affa 20 66 6f 75 6e 64 20 69 6e 20 70 2d 3e 69 4c 69   found in p->iLi
2affb 6d 69 74 20 69 73 20 72 65 61 63 68 65 64 2c 20  mit is reached, 
2affc 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79  jump immediately
2affd 20 74 6f 0a 2a 2a 20 69 42 72 65 61 6b 2e 0a 2a   to.** iBreak..*
2affe 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 6e  /.static int gen
2afff 65 72 61 74 65 4f 75 74 70 75 74 53 75 62 72 6f  erateOutputSubro
2b000 75 74 69 6e 65 28 0a 20 20 50 61 72 73 65 20 2a  utine(.  Parse *
2b001 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2b002 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
2b003 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ext */.  Select 
2b004 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
2b005 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73   /* The SELECT s
2b006 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65  tatement */.  Se
2b007 6c 65 63 74 44 65 73 74 20 2a 70 49 6e 2c 20 20  lectDest *pIn,  
2b008 20 20 20 20 20 20 2f 2a 20 43 6f 72 6f 75 74 69        /* Corouti
2b009 6e 65 20 73 75 70 70 6c 79 69 6e 67 20 64 61 74  ne supplying dat
2b00a 61 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73  a */.  SelectDes
2b00b 74 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20 2f  t *pDest,      /
2b00c 2a 20 57 68 65 72 65 20 74 6f 20 73 65 6e 64 20  * Where to send 
2b00d 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e  the data */.  in
2b00e 74 20 72 65 67 52 65 74 75 72 6e 2c 20 20 20 20  t regReturn,    
2b00f 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74        /* The ret
2b010 75 72 6e 20 61 64 64 72 65 73 73 20 72 65 67 69  urn address regi
2b011 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ster */.  int re
2b012 67 50 72 65 76 2c 20 20 20 20 20 20 20 20 20 20  gPrev,          
2b013 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 72 65    /* Previous re
2b014 73 75 6c 74 20 72 65 67 69 73 74 65 72 2e 20 20  sult register.  
2b015 4e 6f 20 75 6e 69 71 75 65 6e 65 73 73 20 69 66  No uniqueness if
2b016 20 30 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20   0 */.  KeyInfo 
2b017 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20  *pKeyInfo,      
2b018 2f 2a 20 46 6f 72 20 63 6f 6d 70 61 72 69 6e 67  /* For comparing
2b019 20 77 69 74 68 20 70 72 65 76 69 6f 75 73 20 65   with previous e
2b01a 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20 70 34  ntry */.  int p4
2b01b 74 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20  type,           
2b01c 20 20 2f 2a 20 54 68 65 20 70 34 20 74 79 70 65    /* The p4 type
2b01d 20 66 6f 72 20 70 4b 65 79 49 6e 66 6f 20 2a 2f   for pKeyInfo */
2b01e 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 20 20 20  .  int iBreak   
2b01f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75             /* Ju
2b020 6d 70 20 68 65 72 65 20 69 66 20 77 65 20 68 69  mp here if we hi
2b021 74 20 74 68 65 20 4c 49 4d 49 54 20 2a 2f 0a 29  t the LIMIT */.)
2b022 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
2b023 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
2b024 6e 74 20 69 43 6f 6e 74 69 6e 75 65 3b 0a 20 20  nt iContinue;.  
2b025 69 6e 74 20 61 64 64 72 3b 0a 0a 20 20 61 64 64  int addr;..  add
2b026 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  r = sqlite3VdbeC
2b027 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20  urrentAddr(v);. 
2b028 20 69 43 6f 6e 74 69 6e 75 65 20 3d 20 73 71 6c   iContinue = sql
2b029 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
2b02a 6c 28 76 29 3b 0a 0a 20 20 2f 2a 20 53 75 70 70  l(v);..  /* Supp
2b02b 72 65 73 73 20 64 75 70 6c 69 63 61 74 65 73 20  ress duplicates 
2b02c 66 6f 72 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50  for UNION, EXCEP
2b02d 54 2c 20 61 6e 64 20 49 4e 54 45 52 53 45 43 54  T, and INTERSECT
2b02e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 67   .  */.  if( reg
2b02f 50 72 65 76 20 29 7b 0a 20 20 20 20 69 6e 74 20  Prev ){.    int 
2b030 6a 31 2c 20 6a 32 3b 0a 20 20 20 20 6a 31 20 3d  j1, j2;.    j1 =
2b031 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b032 70 31 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20  p1(v, OP_IfNot, 
2b033 72 65 67 50 72 65 76 29 3b 0a 20 20 20 20 6a 32  regPrev);.    j2
2b034 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2b035 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6d 70 61  dOp4(v, OP_Compa
2b036 72 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 72  re, pIn->iMem, r
2b037 65 67 50 72 65 76 2b 31 2c 20 70 49 6e 2d 3e 6e  egPrev+1, pIn->n
2b038 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Mem,.           
2b039 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b03a 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e     (char*)pKeyIn
2b03b 66 6f 2c 20 70 34 74 79 70 65 29 3b 0a 20 20 20  fo, p4type);.   
2b03c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b03d 70 33 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 6a  p3(v, OP_Jump, j
2b03e 32 2b 32 2c 20 69 43 6f 6e 74 69 6e 75 65 2c 20  2+2, iContinue, 
2b03f 6a 32 2b 32 29 3b 0a 20 20 20 20 73 71 6c 69 74  j2+2);.    sqlit
2b040 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2b041 2c 20 6a 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  , j1);.    sqlit
2b042 65 33 45 78 70 72 43 6f 64 65 43 6f 70 79 28 70  e3ExprCodeCopy(p
2b043 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d  Parse, pIn->iMem
2b044 2c 20 72 65 67 50 72 65 76 2b 31 2c 20 70 49 6e  , regPrev+1, pIn
2b045 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 73 71 6c  ->nMem);.    sql
2b046 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b047 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c  , OP_Integer, 1,
2b048 20 72 65 67 50 72 65 76 29 3b 0a 20 20 7d 0a 20   regPrev);.  }. 
2b049 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d   if( pParse->db-
2b04a 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
2b04b 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20  return 0;..  /* 
2b04c 53 75 70 70 72 65 73 73 20 74 68 65 20 74 68 65  Suppress the the
2b04d 20 66 69 72 73 74 20 4f 46 46 53 45 54 20 65 6e   first OFFSET en
2b04e 74 72 69 65 73 20 69 66 20 74 68 65 72 65 20 69  tries if there i
2b04f 73 20 61 6e 20 4f 46 46 53 45 54 20 63 6c 61 75  s an OFFSET clau
2b050 73 65 0a 20 20 2a 2f 0a 20 20 63 6f 64 65 4f 66  se.  */.  codeOf
2b051 66 73 65 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74  fset(v, p, iCont
2b052 69 6e 75 65 29 3b 0a 0a 20 20 73 77 69 74 63 68  inue);..  switch
2b053 28 20 70 44 65 73 74 2d 3e 65 44 65 73 74 20 29  ( pDest->eDest )
2b054 7b 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74  {.    /* Store t
2b055 68 65 20 72 65 73 75 6c 74 20 61 73 20 64 61 74  he result as dat
2b056 61 20 75 73 69 6e 67 20 61 20 75 6e 69 71 75 65  a using a unique
2b057 20 6b 65 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20   key..    */.   
2b058 20 63 61 73 65 20 53 52 54 5f 54 61 62 6c 65 3a   case SRT_Table:
2b059 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 70  .    case SRT_Ep
2b05a 68 65 6d 54 61 62 3a 20 7b 0a 20 20 20 20 20 20  hemTab: {.      
2b05b 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33  int r1 = sqlite3
2b05c 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
2b05d 65 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 32  e);.      int r2
2b05e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2b05f 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
2b060 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 44      testcase( pD
2b061 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f  est->eDest==SRT_
2b062 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 20 20 74  Table );.      t
2b063 65 73 74 63 61 73 65 28 20 70 44 65 73 74 2d 3e  estcase( pDest->
2b064 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68 65 6d  eDest==SRT_Ephem
2b065 54 61 62 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  Tab );.      sql
2b066 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2b067 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
2b068 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 70 49 6e 2d   pIn->iMem, pIn-
2b069 3e 6e 4d 65 6d 2c 20 72 31 29 3b 0a 20 20 20 20  >nMem, r1);.    
2b06a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b06b 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77  Op2(v, OP_NewRow
2b06c 69 64 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  id, pDest->iParm
2b06d 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 73 71 6c  , r2);.      sql
2b06e 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2b06f 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 70 44 65  , OP_Insert, pDe
2b070 73 74 2d 3e 69 50 61 72 6d 2c 20 72 31 2c 20 72  st->iParm, r1, r
2b071 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  2);.      sqlite
2b072 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
2b073 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b   OPFLAG_APPEND);
2b074 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
2b075 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2b076 72 73 65 2c 20 72 32 29 3b 0a 20 20 20 20 20 20  rse, r2);.      
2b077 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2b078 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
2b079 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
2b07a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53      }..#ifndef S
2b07b 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
2b07c 45 52 59 0a 20 20 20 20 2f 2a 20 49 66 20 77 65  ERY.    /* If we
2b07d 20 61 72 65 20 63 72 65 61 74 69 6e 67 20 61 20   are creating a 
2b07e 73 65 74 20 66 6f 72 20 61 6e 20 22 65 78 70 72  set for an "expr
2b07f 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29   IN (SELECT ...)
2b080 22 20 63 6f 6e 73 74 72 75 63 74 2c 0a 20 20 20  " construct,.   
2b081 20 2a 2a 20 74 68 65 6e 20 74 68 65 72 65 20 73   ** then there s
2b082 68 6f 75 6c 64 20 62 65 20 61 20 73 69 6e 67 6c  hould be a singl
2b083 65 20 69 74 65 6d 20 6f 6e 20 74 68 65 20 73 74  e item on the st
2b084 61 63 6b 2e 20 20 57 72 69 74 65 20 74 68 69 73  ack.  Write this
2b085 0a 20 20 20 20 2a 2a 20 69 74 65 6d 20 69 6e 74  .    ** item int
2b086 6f 20 74 68 65 20 73 65 74 20 74 61 62 6c 65 20  o the set table 
2b087 77 69 74 68 20 62 6f 67 75 73 20 64 61 74 61 2e  with bogus data.
2b088 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
2b089 20 53 52 54 5f 53 65 74 3a 20 7b 0a 20 20 20 20   SRT_Set: {.    
2b08a 20 20 69 6e 74 20 72 31 3b 0a 20 20 20 20 20 20    int r1;.      
2b08b 61 73 73 65 72 74 28 20 70 49 6e 2d 3e 6e 4d 65  assert( pIn->nMe
2b08c 6d 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d  m==1 );.      p-
2b08d 3e 61 66 66 69 6e 69 74 79 20 3d 20 0a 20 20 20  >affinity = .   
2b08e 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6d        sqlite3Com
2b08f 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 2d 3e  pareAffinity(p->
2b090 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
2b091 70 72 2c 20 70 44 65 73 74 2d 3e 61 66 66 69 6e  pr, pDest->affin
2b092 69 74 79 29 3b 0a 20 20 20 20 20 20 72 31 20 3d  ity);.      r1 =
2b093 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2b094 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
2b095 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b096 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op4(v, OP_MakeRe
2b097 63 6f 72 64 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c  cord, pIn->iMem,
2b098 20 31 2c 20 72 31 2c 20 26 70 2d 3e 61 66 66 69   1, r1, &p->affi
2b099 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20  nity, 1);.      
2b09a 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
2b09b 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
2b09c 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d  Parse, pIn->iMem
2b09d 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
2b09e 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b09f 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70   OP_IdxInsert, p
2b0a0 44 65 73 74 2d 3e 69 50 61 72 6d 2c 20 72 31 29  Dest->iParm, r1)
2b0a1 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
2b0a2 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
2b0a3 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20  arse, r1);.     
2b0a4 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23   break;.    }..#
2b0a5 69 66 20 30 20 20 2f 2a 20 4e 65 76 65 72 20 6f  if 0  /* Never o
2b0a6 63 63 75 72 73 20 6f 6e 20 61 6e 20 4f 52 44 45  ccurs on an ORDE
2b0a7 52 20 42 59 20 71 75 65 72 79 20 2a 2f 0a 20 20  R BY query */.  
2b0a8 20 20 2f 2a 20 49 66 20 61 6e 79 20 72 6f 77 20    /* If any row 
2b0a9 65 78 69 73 74 20 69 6e 20 74 68 65 20 72 65 73  exist in the res
2b0aa 75 6c 74 20 73 65 74 2c 20 72 65 63 6f 72 64 20  ult set, record 
2b0ab 74 68 61 74 20 66 61 63 74 20 61 6e 64 20 61 62  that fact and ab
2b0ac 6f 72 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ort..    */.    
2b0ad 63 61 73 65 20 53 52 54 5f 45 78 69 73 74 73 3a  case SRT_Exists:
2b0ae 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
2b0af 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b0b0 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 70 44 65  _Integer, 1, pDe
2b0b1 73 74 2d 3e 69 50 61 72 6d 29 3b 0a 20 20 20 20  st->iParm);.    
2b0b2 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63    /* The LIMIT c
2b0b3 6c 61 75 73 65 20 77 69 6c 6c 20 74 65 72 6d 69  lause will termi
2b0b4 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 20 66 6f  nate the loop fo
2b0b5 72 20 75 73 20 2a 2f 0a 20 20 20 20 20 20 62 72  r us */.      br
2b0b6 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
2b0b7 66 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69  f..    /* If thi
2b0b8 73 20 69 73 20 61 20 73 63 61 6c 61 72 20 73 65  s is a scalar se
2b0b9 6c 65 63 74 20 74 68 61 74 20 69 73 20 70 61 72  lect that is par
2b0ba 74 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69  t of an expressi
2b0bb 6f 6e 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20  on, then.    ** 
2b0bc 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
2b0bd 73 20 69 6e 20 74 68 65 20 61 70 70 72 6f 70 72  s in the appropr
2b0be 69 61 74 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  iate memory cell
2b0bf 20 61 6e 64 20 62 72 65 61 6b 20 6f 75 74 0a 20   and break out. 
2b0c0 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 63 61     ** of the sca
2b0c1 6e 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20  n loop..    */. 
2b0c2 20 20 20 63 61 73 65 20 53 52 54 5f 4d 65 6d 3a     case SRT_Mem:
2b0c3 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
2b0c4 20 70 49 6e 2d 3e 6e 4d 65 6d 3d 3d 31 20 29 3b   pIn->nMem==1 );
2b0c5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2b0c6 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72 73  prCodeMove(pPars
2b0c7 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 70 44  e, pIn->iMem, pD
2b0c8 65 73 74 2d 3e 69 50 61 72 6d 2c 20 31 29 3b 0a  est->iParm, 1);.
2b0c9 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d        /* The LIM
2b0ca 49 54 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 6a  IT clause will j
2b0cb 75 6d 70 20 6f 75 74 20 6f 66 20 74 68 65 20 6c  ump out of the l
2b0cc 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20  oop for us */.  
2b0cd 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2b0ce 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64  .#endif /* #ifnd
2b0cf 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
2b0d0 55 42 51 55 45 52 59 20 2a 2f 0a 0a 20 20 20 20  UBQUERY */..    
2b0d1 2f 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20 61  /* The results a
2b0d2 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 73  re stored in a s
2b0d3 65 71 75 65 6e 63 65 20 6f 66 20 72 65 67 69 73  equence of regis
2b0d4 74 65 72 73 0a 20 20 20 20 2a 2a 20 73 74 61 72  ters.    ** star
2b0d5 74 69 6e 67 20 61 74 20 70 44 65 73 74 2d 3e 69  ting at pDest->i
2b0d6 4d 65 6d 2e 20 20 54 68 65 6e 20 74 68 65 20 63  Mem.  Then the c
2b0d7 6f 2d 72 6f 75 74 69 6e 65 20 79 69 65 6c 64 73  o-routine yields
2b0d8 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
2b0d9 65 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 3a  e SRT_Coroutine:
2b0da 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 44 65   {.      if( pDe
2b0db 73 74 2d 3e 69 4d 65 6d 3d 3d 30 20 29 7b 0a 20  st->iMem==0 ){. 
2b0dc 20 20 20 20 20 20 20 70 44 65 73 74 2d 3e 69 4d         pDest->iM
2b0dd 65 6d 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  em = sqlite3GetT
2b0de 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
2b0df 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20   pIn->nMem);.   
2b0e0 20 20 20 20 20 70 44 65 73 74 2d 3e 6e 4d 65 6d       pDest->nMem
2b0e1 20 3d 20 70 49 6e 2d 3e 6e 4d 65 6d 3b 0a 20 20   = pIn->nMem;.  
2b0e2 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
2b0e3 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28  te3ExprCodeMove(
2b0e4 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65  pParse, pIn->iMe
2b0e5 6d 2c 20 70 44 65 73 74 2d 3e 69 4d 65 6d 2c 20  m, pDest->iMem, 
2b0e6 70 44 65 73 74 2d 3e 6e 4d 65 6d 29 3b 0a 20 20  pDest->nMem);.  
2b0e7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b0e8 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c  ddOp1(v, OP_Yiel
2b0e9 64 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29  d, pDest->iParm)
2b0ea 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2b0eb 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
2b0ec 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61 62 6f 76  none of the abov
2b0ed 65 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  e, then the resu
2b0ee 6c 74 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d  lt destination m
2b0ef 75 73 74 20 62 65 0a 20 20 20 20 2a 2a 20 53 52  ust be.    ** SR
2b0f0 54 5f 4f 75 74 70 75 74 2e 20 20 54 68 69 73 20  T_Output.  This 
2b0f1 72 6f 75 74 69 6e 65 20 69 73 20 6e 65 76 65 72  routine is never
2b0f2 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 6e 79   called with any
2b0f3 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 64 65   other.    ** de
2b0f4 73 74 69 6e 61 74 69 6f 6e 20 6f 74 68 65 72 20  stination other 
2b0f5 74 68 61 6e 20 74 68 65 20 6f 6e 65 73 20 68 61  than the ones ha
2b0f6 6e 64 6c 65 64 20 61 62 6f 76 65 20 6f 72 20 53  ndled above or S
2b0f7 52 54 5f 4f 75 74 70 75 74 2e 0a 20 20 20 20 2a  RT_Output..    *
2b0f8 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 20 53 52 54  *.    ** For SRT
2b0f9 5f 4f 75 74 70 75 74 2c 20 72 65 73 75 6c 74 73  _Output, results
2b0fa 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61   are stored in a
2b0fb 20 73 65 71 75 65 6e 63 65 20 6f 66 20 72 65 67   sequence of reg
2b0fc 69 73 74 65 72 73 2e 20 20 0a 20 20 20 20 2a 2a  isters.  .    **
2b0fd 20 54 68 65 6e 20 74 68 65 20 4f 50 5f 52 65 73   Then the OP_Res
2b0fe 75 6c 74 52 6f 77 20 6f 70 63 6f 64 65 20 69 73  ultRow opcode is
2b0ff 20 75 73 65 64 20 74 6f 20 63 61 75 73 65 20 73   used to cause s
2b100 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 74 6f  qlite3_step() to
2b101 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 74  .    ** return t
2b102 68 65 20 6e 65 78 74 20 72 6f 77 20 6f 66 20 72  he next row of r
2b103 65 73 75 6c 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  esult..    */.  
2b104 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
2b105 20 20 20 61 73 73 65 72 74 28 20 70 44 65 73 74     assert( pDest
2b106 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74  ->eDest==SRT_Out
2b107 70 75 74 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  put );.      sql
2b108 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b109 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  , OP_ResultRow, 
2b10a 70 49 6e 2d 3e 69 4d 65 6d 2c 20 70 49 6e 2d 3e  pIn->iMem, pIn->
2b10b 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c  nMem);.      sql
2b10c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
2b10d 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72  inityChange(pPar
2b10e 73 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 70  se, pIn->iMem, p
2b10f 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 20  In->nMem);.     
2b110 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2b111 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20  }..  /* Jump to 
2b112 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c  the end of the l
2b113 6f 6f 70 20 69 66 20 74 68 65 20 4c 49 4d 49 54  oop if the LIMIT
2b114 20 69 73 20 72 65 61 63 68 65 64 2e 0a 20 20 2a   is reached..  *
2b115 2f 0a 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69  /.  if( p->iLimi
2b116 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
2b117 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b118 5f 41 64 64 49 6d 6d 2c 20 70 2d 3e 69 4c 69 6d  _AddImm, p->iLim
2b119 69 74 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c  it, -1);.    sql
2b11a 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b11b 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 70 2d 3e  , OP_IfZero, p->
2b11c 69 4c 69 6d 69 74 2c 20 69 42 72 65 61 6b 29 3b  iLimit, iBreak);
2b11d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  .  }..  /* Gener
2b11e 61 74 65 20 74 68 65 20 73 75 62 72 6f 75 74 69  ate the subrouti
2b11f 6e 65 20 72 65 74 75 72 6e 0a 20 20 2a 2f 0a 20  ne return.  */. 
2b120 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
2b121 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 43 6f 6e  lveLabel(v, iCon
2b122 74 69 6e 75 65 29 3b 0a 20 20 73 71 6c 69 74 65  tinue);.  sqlite
2b123 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2b124 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 52 65 74  P_Return, regRet
2b125 75 72 6e 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  urn);..  return 
2b126 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  addr;.}../*.** A
2b127 6c 74 65 72 6e 61 74 69 76 65 20 63 6f 6d 70 6f  lternative compo
2b128 75 6e 64 20 73 65 6c 65 63 74 20 63 6f 64 65 20  und select code 
2b129 67 65 6e 65 72 61 74 6f 72 20 66 6f 72 20 63 61  generator for ca
2b12a 73 65 73 20 77 68 65 6e 20 74 68 65 72 65 0a 2a  ses when there.*
2b12b 2a 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59  * is an ORDER BY
2b12c 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 57   clause..**.** W
2b12d 65 20 61 73 73 75 6d 65 20 61 20 71 75 65 72 79  e assume a query
2b12e 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
2b12f 67 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20  g form:.**.**   
2b130 20 20 20 3c 73 65 6c 65 63 74 41 3e 20 20 3c 6f     <selectA>  <o
2b131 70 65 72 61 74 6f 72 3e 20 20 3c 73 65 6c 65 63  perator>  <selec
2b132 74 42 3e 20 20 4f 52 44 45 52 20 42 59 20 3c 6f  tB>  ORDER BY <o
2b133 72 64 65 72 62 79 6c 69 73 74 3e 0a 2a 2a 0a 2a  rderbylist>.**.*
2b134 2a 20 3c 6f 70 65 72 61 74 6f 72 3e 20 69 73 20  * <operator> is 
2b135 6f 6e 65 20 6f 66 20 55 4e 49 4f 4e 20 41 4c 4c  one of UNION ALL
2b136 2c 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54 2c  , UNION, EXCEPT,
2b137 20 6f 72 20 49 4e 54 45 52 53 45 43 54 2e 20 20   or INTERSECT.  
2b138 54 68 65 20 69 64 65 61 0a 2a 2a 20 69 73 20 74  The idea.** is t
2b139 6f 20 63 6f 64 65 20 62 6f 74 68 20 3c 73 65 6c  o code both <sel
2b13a 65 63 74 41 3e 20 61 6e 64 20 3c 73 65 6c 65 63  ectA> and <selec
2b13b 74 42 3e 20 77 69 74 68 20 74 68 65 20 4f 52 44  tB> with the ORD
2b13c 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 73 0a  ER BY clause as.
2b13d 2a 2a 20 63 6f 2d 72 6f 75 74 69 6e 65 73 2e 20  ** co-routines. 
2b13e 20 54 68 65 6e 20 72 75 6e 20 74 68 65 20 63 6f   Then run the co
2b13f 2d 72 6f 75 74 69 6e 65 73 20 69 6e 20 70 61 72  -routines in par
2b140 61 6c 6c 65 6c 20 61 6e 64 20 6d 65 72 67 65 20  allel and merge 
2b141 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 69  the results.** i
2b142 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74 2e 20  nto the output. 
2b143 20 49 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f 20   In addition to 
2b144 74 68 65 20 74 77 6f 20 63 6f 72 6f 75 74 69 6e  the two coroutin
2b145 65 73 20 28 63 61 6c 6c 65 64 20 73 65 6c 65 63  es (called selec
2b146 74 41 20 61 6e 64 0a 2a 2a 20 73 65 6c 65 63 74  tA and.** select
2b147 42 29 20 74 68 65 72 65 20 61 72 65 20 37 20 73  B) there are 7 s
2b148 75 62 72 6f 75 74 69 6e 65 73 3a 0a 2a 2a 0a 2a  ubroutines:.**.*
2b149 2a 20 20 20 20 6f 75 74 41 3a 20 20 20 20 4d 6f  *    outA:    Mo
2b14a 76 65 20 74 68 65 20 6f 75 74 70 75 74 20 6f 66  ve the output of
2b14b 20 74 68 65 20 73 65 6c 65 63 74 41 20 63 6f 72   the selectA cor
2b14c 6f 75 74 69 6e 65 20 69 6e 74 6f 20 74 68 65 20  outine into the 
2b14d 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20  output.**       
2b14e 20 20 20 20 20 20 6f 66 20 74 68 65 20 63 6f 6d        of the com
2b14f 70 6f 75 6e 64 20 71 75 65 72 79 2e 0a 2a 2a 0a  pound query..**.
2b150 2a 2a 20 20 20 20 6f 75 74 42 3a 20 20 20 20 4d  **    outB:    M
2b151 6f 76 65 20 74 68 65 20 6f 75 74 70 75 74 20 6f  ove the output o
2b152 66 20 74 68 65 20 73 65 6c 65 63 74 42 20 63 6f  f the selectB co
2b153 72 6f 75 74 69 6e 65 20 69 6e 74 6f 20 74 68 65  routine into the
2b154 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 20 20   output.**      
2b155 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 63 6f         of the co
2b156 6d 70 6f 75 6e 64 20 71 75 65 72 79 2e 20 20 28  mpound query.  (
2b157 4f 6e 6c 79 20 67 65 6e 65 72 61 74 65 64 20 66  Only generated f
2b158 6f 72 20 55 4e 49 4f 4e 20 61 6e 64 0a 2a 2a 20  or UNION and.** 
2b159 20 20 20 20 20 20 20 20 20 20 20 20 55 4e 49 4f              UNIO
2b15a 4e 20 41 4c 4c 2e 20 20 45 58 43 45 50 54 20 61  N ALL.  EXCEPT a
2b15b 6e 64 20 49 4e 53 45 52 54 53 45 43 54 20 6e 65  nd INSERTSECT ne
2b15c 76 65 72 20 6f 75 74 70 75 74 20 61 20 72 6f 77  ver output a row
2b15d 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20   that.**        
2b15e 20 20 20 20 20 61 70 70 65 61 72 73 20 6f 6e 6c       appears onl
2b15f 79 20 69 6e 20 42 2e 29 0a 2a 2a 0a 2a 2a 20 20  y in B.).**.**  
2b160 20 20 41 6c 74 42 3a 20 20 20 20 43 61 6c 6c 65    AltB:    Calle
2b161 64 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20  d when there is 
2b162 64 61 74 61 20 66 72 6f 6d 20 62 6f 74 68 20 63  data from both c
2b163 6f 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 41 3c  oroutines and A<
2b164 42 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 41 65 71 42  B..**.**    AeqB
2b165 3a 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65 6e  :    Called when
2b166 20 74 68 65 72 65 20 69 73 20 64 61 74 61 20 66   there is data f
2b167 72 6f 6d 20 62 6f 74 68 20 63 6f 72 6f 75 74 69  rom both corouti
2b168 6e 65 73 20 61 6e 64 20 41 3d 3d 42 2e 0a 2a 2a  nes and A==B..**
2b169 0a 2a 2a 20 20 20 20 41 67 74 42 3a 20 20 20 20  .**    AgtB:    
2b16a 43 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 72  Called when ther
2b16b 65 20 69 73 20 64 61 74 61 20 66 72 6f 6d 20 62  e is data from b
2b16c 6f 74 68 20 63 6f 72 6f 75 74 69 6e 65 73 20 61  oth coroutines a
2b16d 6e 64 20 41 3e 42 2e 0a 2a 2a 0a 2a 2a 20 20 20  nd A>B..**.**   
2b16e 20 45 6f 66 41 3a 20 20 20 20 43 61 6c 6c 65 64   EofA:    Called
2b16f 20 77 68 65 6e 20 64 61 74 61 20 69 73 20 65 78   when data is ex
2b170 68 61 75 73 74 65 64 20 66 72 6f 6d 20 73 65 6c  hausted from sel
2b171 65 63 74 41 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 45  ectA..**.**    E
2b172 6f 66 42 3a 20 20 20 20 43 61 6c 6c 65 64 20 77  ofB:    Called w
2b173 68 65 6e 20 64 61 74 61 20 69 73 20 65 78 68 61  hen data is exha
2b174 75 73 74 65 64 20 66 72 6f 6d 20 73 65 6c 65 63  usted from selec
2b175 74 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d  tB..**.** The im
2b176 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
2b177 74 68 65 20 6c 61 74 74 65 72 20 66 69 76 65 20  the latter five 
2b178 73 75 62 72 6f 75 74 69 6e 65 73 20 64 65 70 65  subroutines depe
2b179 6e 64 20 6f 6e 20 77 68 69 63 68 20 0a 2a 2a 20  nd on which .** 
2b17a 3c 6f 70 65 72 61 74 6f 72 3e 20 69 73 20 75 73  <operator> is us
2b17b 65 64 3a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 20 20 20  ed:.**.**.**    
2b17c 20 20 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 41           UNION A
2b17d 4c 4c 20 20 20 20 20 20 20 20 20 55 4e 49 4f 4e  LL         UNION
2b17e 20 20 20 20 20 20 20 20 20 20 20 20 45 58 43 45              EXCE
2b17f 50 54 20 20 20 20 20 20 20 20 20 20 49 4e 54 45  PT          INTE
2b180 52 53 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20  RSECT.**        
2b181 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20    ------------- 
2b182 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
2b183 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  --  ------------
2b184 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  --  ------------
2b185 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 41 6c 74 42 3a  -----.**   AltB:
2b186 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20     outA, nextA  
2b187 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20      outA, nextA 
2b188 20 20 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74        outA, next
2b189 41 20 20 20 20 20 20 20 20 20 6e 65 78 74 41 0a  A         nextA.
2b18a 2a 2a 0a 2a 2a 20 20 20 41 65 71 42 3a 20 20 20  **.**   AeqB:   
2b18b 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20  outA, nextA     
2b18c 20 20 20 20 6e 65 78 74 41 20 20 20 20 20 20 20      nextA       
2b18d 20 20 20 20 20 20 6e 65 78 74 41 20 20 20 20 20        nextA     
2b18e 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 0a      outA, nextA.
2b18f 2a 2a 0a 2a 2a 20 20 20 41 67 74 42 3a 20 20 20  **.**   AgtB:   
2b190 6f 75 74 42 2c 20 6e 65 78 74 42 20 20 20 20 20  outB, nextB     
2b191 20 6f 75 74 42 2c 20 6e 65 78 74 42 20 20 20 20   outB, nextB    
2b192 20 20 20 20 20 20 6e 65 78 74 42 20 20 20 20 20        nextB     
2b193 20 20 20 20 20 20 20 6e 65 78 74 42 0a 2a 2a 0a         nextB.**.
2b194 2a 2a 20 20 20 45 6f 66 41 3a 20 20 20 6f 75 74  **   EofA:   out
2b195 42 2c 20 6e 65 78 74 42 20 20 20 20 20 20 6f 75  B, nextB      ou
2b196 74 42 2c 20 6e 65 78 74 42 20 20 20 20 20 20 20  tB, nextB       
2b197 20 20 20 68 61 6c 74 20 20 20 20 20 20 20 20 20     halt         
2b198 20 20 20 20 68 61 6c 74 0a 2a 2a 0a 2a 2a 20 20      halt.**.**  
2b199 20 45 6f 66 42 3a 20 20 20 6f 75 74 41 2c 20 6e   EofB:   outA, n
2b19a 65 78 74 41 20 20 20 20 20 20 6f 75 74 41 2c 20  extA      outA, 
2b19b 6e 65 78 74 41 20 20 20 20 20 20 20 6f 75 74 41  nextA       outA
2b19c 2c 20 6e 65 78 74 41 20 20 20 20 20 20 20 20 20  , nextA         
2b19d 68 61 6c 74 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  halt.**.** In th
2b19e 65 20 41 6c 74 42 2c 20 41 65 71 42 2c 20 61 6e  e AltB, AeqB, an
2b19f 64 20 41 67 74 42 20 73 75 62 72 6f 75 74 69 6e  d AgtB subroutin
2b1a0 65 73 2c 20 61 6e 20 45 4f 46 20 6f 6e 20 41 20  es, an EOF on A 
2b1a1 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 78 74 41 0a  following nextA.
2b1a2 2a 2a 20 63 61 75 73 65 73 20 61 6e 20 69 6d 6d  ** causes an imm
2b1a3 65 64 69 61 74 65 20 6a 75 6d 70 20 74 6f 20 45  ediate jump to E
2b1a4 6f 66 41 20 61 6e 64 20 61 6e 20 45 4f 46 20 6f  ofA and an EOF o
2b1a5 6e 20 42 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65  n B following ne
2b1a6 78 74 42 20 63 61 75 73 65 73 0a 2a 2a 20 61 6e  xtB causes.** an
2b1a7 20 69 6d 6d 65 64 69 61 74 65 20 6a 75 6d 70 20   immediate jump 
2b1a8 74 6f 20 45 6f 66 42 2e 20 20 57 69 74 68 69 6e  to EofB.  Within
2b1a9 20 45 6f 66 41 20 61 6e 64 20 45 6f 66 42 2c 20   EofA and EofB, 
2b1aa 61 6e 64 20 45 4f 46 20 6f 6e 20 65 6e 74 72 79  and EOF on entry
2b1ab 20 6f 72 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67   or.** following
2b1ac 20 6e 65 78 74 58 20 63 61 75 73 65 73 20 61 20   nextX causes a 
2b1ad 6a 75 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20  jump to the end 
2b1ae 6f 66 20 74 68 65 20 73 65 6c 65 63 74 20 70 72  of the select pr
2b1af 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  ocessing..**.** 
2b1b0 44 75 70 6c 69 63 61 74 65 20 72 65 6d 6f 76 61  Duplicate remova
2b1b1 6c 20 69 6e 20 74 68 65 20 55 4e 49 4f 4e 2c 20  l in the UNION, 
2b1b2 45 58 43 45 50 54 2c 20 61 6e 64 20 49 4e 54 45  EXCEPT, and INTE
2b1b3 52 53 45 43 54 20 63 61 73 65 73 20 69 73 20 68  RSECT cases is h
2b1b4 61 6e 64 6c 65 64 0a 2a 2a 20 77 69 74 68 69 6e  andled.** within
2b1b5 20 74 68 65 20 6f 75 74 70 75 74 20 73 75 62 72   the output subr
2b1b6 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 72 65 67  outine.  The reg
2b1b7 50 72 65 76 20 72 65 67 69 73 74 65 72 20 73 65  Prev register se
2b1b8 74 20 68 6f 6c 64 73 20 74 68 65 20 70 72 65 76  t holds the prev
2b1b9 69 6f 75 73 6c 79 0a 2a 2a 20 6f 75 74 70 75 74  iously.** output
2b1ba 20 76 61 6c 75 65 2e 20 20 41 20 63 6f 6d 70 61   value.  A compa
2b1bb 72 69 73 6f 6e 20 69 73 20 6d 61 64 65 20 61 67  rison is made ag
2b1bc 61 69 6e 73 74 20 74 68 69 73 20 76 61 6c 75 65  ainst this value
2b1bd 20 61 6e 64 20 74 68 65 20 6f 75 74 70 75 74 0a   and the output.
2b1be 2a 2a 20 69 73 20 73 6b 69 70 70 65 64 20 69 66  ** is skipped if
2b1bf 20 74 68 65 20 6e 65 78 74 20 72 65 73 75 6c 74   the next result
2b1c0 73 20 77 6f 75 6c 64 20 62 65 20 74 68 65 20 73  s would be the s
2b1c1 61 6d 65 20 61 73 20 74 68 65 20 70 72 65 76 69  ame as the previ
2b1c2 6f 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ous..**.** The i
2b1c3 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 70 6c  mplementation pl
2b1c4 61 6e 20 69 73 20 74 6f 20 69 6d 70 6c 65 6d 65  an is to impleme
2b1c5 6e 74 20 74 68 65 20 74 77 6f 20 63 6f 72 6f 75  nt the two corou
2b1c6 74 69 6e 65 73 20 61 6e 64 20 73 65 76 65 6e 0a  tines and seven.
2b1c7 2a 2a 20 73 75 62 72 6f 75 74 69 6e 65 73 20 66  ** subroutines f
2b1c8 69 72 73 74 2c 20 74 68 65 6e 20 70 75 74 20 74  irst, then put t
2b1c9 68 65 20 63 6f 6e 74 72 6f 6c 20 6c 6f 67 69 63  he control logic
2b1ca 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d 2e 20   at the bottom. 
2b1cb 20 4c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   Like this:.**.*
2b1cc 2a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  *          goto 
2b1cd 49 6e 69 74 0a 2a 2a 20 20 20 20 20 63 6f 41 3a  Init.**     coA:
2b1ce 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20 6c   coroutine for l
2b1cf 65 66 74 20 71 75 65 72 79 20 28 41 29 0a 2a 2a  eft query (A).**
2b1d0 20 20 20 20 20 63 6f 42 3a 20 63 6f 72 6f 75 74       coB: corout
2b1d1 69 6e 65 20 66 6f 72 20 72 69 67 68 74 20 71 75  ine for right qu
2b1d2 65 72 79 20 28 42 29 0a 2a 2a 20 20 20 20 6f 75  ery (B).**    ou
2b1d3 74 41 3a 20 6f 75 74 70 75 74 20 6f 6e 65 20 72  tA: output one r
2b1d4 6f 77 20 6f 66 20 41 0a 2a 2a 20 20 20 20 6f 75  ow of A.**    ou
2b1d5 74 42 3a 20 6f 75 74 70 75 74 20 6f 6e 65 20 72  tB: output one r
2b1d6 6f 77 20 6f 66 20 42 20 28 55 4e 49 4f 4e 20 61  ow of B (UNION a
2b1d7 6e 64 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 6e 6c  nd UNION ALL onl
2b1d8 79 29 0a 2a 2a 20 20 20 20 45 6f 66 41 3a 20 2e  y).**    EofA: .
2b1d9 2e 2e 0a 2a 2a 20 20 20 20 45 6f 66 42 3a 20 2e  ...**    EofB: .
2b1da 2e 2e 0a 2a 2a 20 20 20 20 41 6c 74 42 3a 20 2e  ...**    AltB: .
2b1db 2e 2e 0a 2a 2a 20 20 20 20 41 65 71 42 3a 20 2e  ...**    AeqB: .
2b1dc 2e 2e 0a 2a 2a 20 20 20 20 41 67 74 42 3a 20 2e  ...**    AgtB: .
2b1dd 2e 2e 0a 2a 2a 20 20 20 20 49 6e 69 74 3a 20 69  ...**    Init: i
2b1de 6e 69 74 69 61 6c 69 7a 65 20 63 6f 72 6f 75 74  nitialize corout
2b1df 69 6e 65 20 72 65 67 69 73 74 65 72 73 0a 2a 2a  ine registers.**
2b1e0 20 20 20 20 20 20 20 20 20 20 79 69 65 6c 64 20            yield 
2b1e1 63 6f 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  coA.**          
2b1e2 69 66 20 65 6f 66 28 41 29 20 67 6f 74 6f 20 45  if eof(A) goto E
2b1e3 6f 66 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ofA.**          
2b1e4 79 69 65 6c 64 20 63 6f 42 0a 2a 2a 20 20 20 20  yield coB.**    
2b1e5 20 20 20 20 20 20 69 66 20 65 6f 66 28 42 29 20        if eof(B) 
2b1e6 67 6f 74 6f 20 45 6f 66 42 0a 2a 2a 20 20 20 20  goto EofB.**    
2b1e7 43 6d 70 72 3a 20 43 6f 6d 70 61 72 65 20 41 2c  Cmpr: Compare A,
2b1e8 20 42 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 4a   B.**          J
2b1e9 75 6d 70 20 41 6c 74 42 2c 20 41 65 71 42 2c 20  ump AltB, AeqB, 
2b1ea 41 67 74 42 0a 2a 2a 20 20 20 20 20 45 6e 64 3a  AgtB.**     End:
2b1eb 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 63 61   ....**.** We ca
2b1ec 6c 6c 20 41 6c 74 42 2c 20 41 65 71 42 2c 20 41  ll AltB, AeqB, A
2b1ed 67 74 42 2c 20 45 6f 66 41 2c 20 61 6e 64 20 45  gtB, EofA, and E
2b1ee 6f 66 42 20 22 73 75 62 72 6f 75 74 69 6e 65 73  ofB "subroutines
2b1ef 22 20 62 75 74 20 74 68 65 79 20 61 72 65 20 6e  " but they are n
2b1f0 6f 74 0a 2a 2a 20 61 63 74 75 61 6c 6c 79 20 63  ot.** actually c
2b1f1 61 6c 6c 65 64 20 75 73 69 6e 67 20 47 6f 73 75  alled using Gosu
2b1f2 62 20 61 6e 64 20 74 68 65 79 20 64 6f 20 6e 6f  b and they do no
2b1f3 74 20 52 65 74 75 72 6e 2e 20 20 45 6f 66 41 20  t Return.  EofA 
2b1f4 61 6e 64 20 45 6f 66 42 20 6c 6f 6f 70 0a 2a 2a  and EofB loop.**
2b1f5 20 75 6e 74 69 6c 20 61 6c 6c 20 64 61 74 61 20   until all data 
2b1f6 69 73 20 65 78 68 61 75 73 74 65 64 20 74 68 65  is exhausted the
2b1f7 6e 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 22 65  n jump to the "e
2b1f8 6e 64 22 20 6c 61 62 65 2e 20 20 41 6c 74 42 2c  nd" labe.  AltB,
2b1f9 20 41 65 71 42 2c 0a 2a 2a 20 61 6e 64 20 41 67   AeqB,.** and Ag
2b1fa 74 42 20 6a 75 6d 70 20 74 6f 20 65 69 74 68 65  tB jump to eithe
2b1fb 72 20 4c 32 20 6f 72 20 74 6f 20 6f 6e 65 20 6f  r L2 or to one o
2b1fc 66 20 45 6f 66 41 20 6f 72 20 45 6f 66 42 2e 0a  f EofA or EofB..
2b1fd 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
2b1fe 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f  E_OMIT_COMPOUND_
2b1ff 53 45 4c 45 43 54 0a 73 74 61 74 69 63 20 69 6e  SELECT.static in
2b200 74 20 6d 75 6c 74 69 53 65 6c 65 63 74 4f 72 64  t multiSelectOrd
2b201 65 72 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70  erBy(.  Parse *p
2b202 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a  Parse,        /*
2b203 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
2b204 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c   */.  Select *p,
2b205 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2b206 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66  he right-most of
2b207 20 53 45 4c 45 43 54 73 20 74 6f 20 62 65 20 63   SELECTs to be c
2b208 6f 64 65 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74  oded */.  Select
2b209 44 65 73 74 20 2a 70 44 65 73 74 20 20 20 20 20  Dest *pDest     
2b20a 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69  /* What to do wi
2b20b 74 68 20 71 75 65 72 79 20 72 65 73 75 6c 74 73  th query results
2b20c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20   */.){.  int i, 
2b20d 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  j;             /
2b20e 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
2b20f 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 72  */.  Select *pPr
2b210 69 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e  ior;       /* An
2b211 6f 74 68 65 72 20 53 45 4c 45 43 54 20 69 6d 6d  other SELECT imm
2b212 65 64 69 61 74 65 6c 79 20 74 6f 20 6f 75 72 20  ediately to our 
2b213 6c 65 66 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a  left */.  Vdbe *
2b214 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
2b215 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
2b216 20 74 6f 20 74 68 69 73 20 56 44 42 45 20 2a 2f   to this VDBE */
2b217 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65  .  SelectDest de
2b218 73 74 41 3b 20 20 20 20 20 2f 2a 20 44 65 73 74  stA;     /* Dest
2b219 69 6e 61 74 69 6f 6e 20 66 6f 72 20 63 6f 72 6f  ination for coro
2b21a 75 74 69 6e 65 20 41 20 2a 2f 0a 20 20 53 65 6c  utine A */.  Sel
2b21b 65 63 74 44 65 73 74 20 64 65 73 74 42 3b 20 20  ectDest destB;  
2b21c 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f     /* Destinatio
2b21d 6e 20 66 6f 72 20 63 6f 72 6f 75 74 69 6e 65 20  n for coroutine 
2b21e 42 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41 64  B */.  int regAd
2b21f 64 72 41 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  drA;         /* 
2b220 41 64 64 72 65 73 73 20 72 65 67 69 73 74 65 72  Address register
2b221 20 66 6f 72 20 73 65 6c 65 63 74 2d 41 20 63 6f   for select-A co
2b222 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  routine */.  int
2b223 20 72 65 67 45 6f 66 41 3b 20 20 20 20 20 20 20   regEofA;       
2b224 20 20 20 2f 2a 20 46 6c 61 67 20 74 6f 20 69 6e     /* Flag to in
2b225 64 69 63 61 74 65 20 77 68 65 6e 20 73 65 6c 65  dicate when sele
2b226 63 74 2d 41 20 69 73 20 63 6f 6d 70 6c 65 74 65  ct-A is complete
2b227 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41 64 64   */.  int regAdd
2b228 72 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41  rB;         /* A
2b229 64 64 72 65 73 73 20 72 65 67 69 73 74 65 72 20  ddress register 
2b22a 66 6f 72 20 73 65 6c 65 63 74 2d 42 20 63 6f 72  for select-B cor
2b22b 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  outine */.  int 
2b22c 72 65 67 45 6f 66 42 3b 20 20 20 20 20 20 20 20  regEofB;        
2b22d 20 20 2f 2a 20 46 6c 61 67 20 74 6f 20 69 6e 64    /* Flag to ind
2b22e 69 63 61 74 65 20 77 68 65 6e 20 73 65 6c 65 63  icate when selec
2b22f 74 2d 42 20 69 73 20 63 6f 6d 70 6c 65 74 65 20  t-B is complete 
2b230 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 53 65 6c  */.  int addrSel
2b231 65 63 74 41 3b 20 20 20 20 20 20 2f 2a 20 41 64  ectA;      /* Ad
2b232 64 72 65 73 73 20 6f 66 20 74 68 65 20 73 65 6c  dress of the sel
2b233 65 63 74 2d 41 20 63 6f 72 6f 75 74 69 6e 65 20  ect-A coroutine 
2b234 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 53 65 6c  */.  int addrSel
2b235 65 63 74 42 3b 20 20 20 20 20 20 2f 2a 20 41 64  ectB;      /* Ad
2b236 64 72 65 73 73 20 6f 66 20 74 68 65 20 73 65 6c  dress of the sel
2b237 65 63 74 2d 42 20 63 6f 72 6f 75 74 69 6e 65 20  ect-B coroutine 
2b238 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f 75 74 41  */.  int regOutA
2b239 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64  ;          /* Ad
2b23a 64 72 65 73 73 20 72 65 67 69 73 74 65 72 20 66  dress register f
2b23b 6f 72 20 74 68 65 20 6f 75 74 70 75 74 2d 41 20  or the output-A 
2b23c 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20  subroutine */.  
2b23d 69 6e 74 20 72 65 67 4f 75 74 42 3b 20 20 20 20  int regOutB;    
2b23e 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
2b23f 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 74 68   register for th
2b240 65 20 6f 75 74 70 75 74 2d 42 20 73 75 62 72 6f  e output-B subro
2b241 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61  utine */.  int a
2b242 64 64 72 4f 75 74 41 3b 20 20 20 20 20 20 20 20  ddrOutA;        
2b243 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74   /* Address of t
2b244 68 65 20 6f 75 74 70 75 74 2d 41 20 73 75 62 72  he output-A subr
2b245 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  outine */.  int 
2b246 61 64 64 72 4f 75 74 42 20 3d 20 30 3b 20 20 20  addrOutB = 0;   
2b247 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
2b248 74 68 65 20 6f 75 74 70 75 74 2d 42 20 73 75 62  the output-B sub
2b249 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  routine */.  int
2b24a 20 61 64 64 72 45 6f 66 41 3b 20 20 20 20 20 20   addrEofA;      
2b24b 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
2b24c 20 74 68 65 20 73 65 6c 65 63 74 2d 41 2d 65 78   the select-A-ex
2b24d 68 61 75 73 74 65 64 20 73 75 62 72 6f 75 74 69  hausted subrouti
2b24e 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ne */.  int addr
2b24f 45 6f 66 42 3b 20 20 20 20 20 20 20 20 20 2f 2a  EofB;         /*
2b250 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   Address of the 
2b251 73 65 6c 65 63 74 2d 42 2d 65 78 68 61 75 73 74  select-B-exhaust
2b252 65 64 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f  ed subroutine */
2b253 0a 20 20 69 6e 74 20 61 64 64 72 41 6c 74 42 3b  .  int addrAltB;
2b254 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
2b255 65 73 73 20 6f 66 20 74 68 65 20 41 3c 42 20 73  ess of the A<B s
2b256 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69  ubroutine */.  i
2b257 6e 74 20 61 64 64 72 41 65 71 42 3b 20 20 20 20  nt addrAeqB;    
2b258 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
2b259 6f 66 20 74 68 65 20 41 3d 3d 42 20 73 75 62 72  of the A==B subr
2b25a 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  outine */.  int 
2b25b 61 64 64 72 41 67 74 42 3b 20 20 20 20 20 20 20  addrAgtB;       
2b25c 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
2b25d 74 68 65 20 41 3e 42 20 73 75 62 72 6f 75 74 69  the A>B subrouti
2b25e 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4c  ne */.  int regL
2b25f 69 6d 69 74 41 3b 20 20 20 20 20 20 20 20 2f 2a  imitA;        /*
2b260 20 4c 69 6d 69 74 20 72 65 67 69 73 74 65 72 20   Limit register 
2b261 66 6f 72 20 73 65 6c 65 63 74 2d 41 20 2a 2f 0a  for select-A */.
2b262 20 20 69 6e 74 20 72 65 67 4c 69 6d 69 74 42 3b    int regLimitB;
2b263 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6d 69 74          /* Limit
2b264 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 73 65   register for se
2b265 6c 65 63 74 2d 41 20 2a 2f 0a 20 20 69 6e 74 20  lect-A */.  int 
2b266 72 65 67 50 72 65 76 3b 20 20 20 20 20 20 20 20  regPrev;        
2b267 20 20 2f 2a 20 41 20 72 61 6e 67 65 20 6f 66 20    /* A range of 
2b268 72 65 67 69 73 74 65 72 73 20 74 6f 20 68 6f 6c  registers to hol
2b269 64 20 70 72 65 76 69 6f 75 73 20 6f 75 74 70 75  d previous outpu
2b26a 74 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 64  t */.  int saved
2b26b 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 2f 2a 20  Limit;       /* 
2b26c 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20 70  Saved value of p
2b26d 2d 3e 69 4c 69 6d 69 74 20 2a 2f 0a 20 20 69 6e  ->iLimit */.  in
2b26e 74 20 73 61 76 65 64 4f 66 66 73 65 74 3b 20 20  t savedOffset;  
2b26f 20 20 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c      /* Saved val
2b270 75 65 20 6f 66 20 70 2d 3e 69 4f 66 66 73 65 74  ue of p->iOffset
2b271 20 2a 2f 0a 20 20 69 6e 74 20 6c 61 62 65 6c 43   */.  int labelC
2b272 6d 70 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c  mpr;        /* L
2b273 61 62 65 6c 20 66 6f 72 20 74 68 65 20 73 74 61  abel for the sta
2b274 72 74 20 6f 66 20 74 68 65 20 6d 65 72 67 65 20  rt of the merge 
2b275 61 6c 67 6f 72 69 74 68 6d 20 2a 2f 0a 20 20 69  algorithm */.  i
2b276 6e 74 20 6c 61 62 65 6c 45 6e 64 3b 20 20 20 20  nt labelEnd;    
2b277 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20 66 6f       /* Label fo
2b278 72 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  r the end of the
2b279 20 6f 76 65 72 61 6c 6c 20 53 45 4c 45 43 54 20   overall SELECT 
2b27a 73 74 6d 74 20 2a 2f 0a 20 20 69 6e 74 20 6a 31  stmt */.  int j1
2b27b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2b27c 2f 2a 20 4a 75 6d 70 20 69 6e 73 74 72 75 63 74  /* Jump instruct
2b27d 69 6f 6e 73 20 74 68 61 74 20 67 65 74 20 72 65  ions that get re
2b27e 74 61 72 67 65 74 74 65 64 20 2a 2f 0a 20 20 69  targetted */.  i
2b27f 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20  nt op;          
2b280 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54       /* One of T
2b281 4b 5f 41 4c 4c 2c 20 54 4b 5f 55 4e 49 4f 4e 2c  K_ALL, TK_UNION,
2b282 20 54 4b 5f 45 58 43 45 50 54 2c 20 54 4b 5f 49   TK_EXCEPT, TK_I
2b283 4e 54 45 52 53 45 43 54 20 2a 2f 0a 20 20 4b 65  NTERSECT */.  Ke
2b284 79 49 6e 66 6f 20 2a 70 4b 65 79 44 75 70 20 3d  yInfo *pKeyDup =
2b285 20 30 3b 20 2f 2a 20 43 6f 6d 70 61 72 69 73 6f   0; /* Compariso
2b286 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  n information fo
2b287 72 20 64 75 70 6c 69 63 61 74 65 20 72 65 6d 6f  r duplicate remo
2b288 76 61 6c 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f  val */.  KeyInfo
2b289 20 2a 70 4b 65 79 4d 65 72 67 65 3b 20 20 20 2f   *pKeyMerge;   /
2b28a 2a 20 43 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 66  * Comparison inf
2b28b 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 6d 65 72  ormation for mer
2b28c 67 69 6e 67 20 72 6f 77 73 20 2a 2f 0a 20 20 73  ging rows */.  s
2b28d 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
2b28e 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
2b28f 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
2b290 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
2b291 72 42 79 3b 20 20 20 2f 2a 20 54 68 65 20 4f 52  rBy;   /* The OR
2b292 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
2b293 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b  .  int nOrderBy;
2b294 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2b295 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 74  er of terms in t
2b296 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2b297 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 50 65  se */.  int *aPe
2b298 72 6d 75 74 65 3b 20 20 20 20 20 20 20 20 2f 2a  rmute;        /*
2b299 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 4f 52   Mapping from OR
2b29a 44 45 52 20 42 59 20 74 65 72 6d 73 20 74 6f 20  DER BY terms to 
2b29b 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d  result set colum
2b29c 6e 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  ns */..  assert(
2b29d 20 70 2d 3e 70 4f 72 64 65 72 42 79 21 3d 30 20   p->pOrderBy!=0 
2b29e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65  );.  assert( pKe
2b29f 79 44 75 70 3d 3d 30 20 29 3b 20 2f 2a 20 22 4d  yDup==0 ); /* "M
2b2a0 61 6e 61 67 65 64 22 20 63 6f 64 65 20 6e 65 65  anaged" code nee
2b2a1 64 73 20 74 68 69 73 2e 20 20 54 69 63 6b 65 74  ds this.  Ticket
2b2a2 20 23 33 33 38 32 2e 20 2a 2f 0a 20 20 64 62 20   #3382. */.  db 
2b2a3 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2b2a4 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2b2a5 65 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d  e;.  assert( v!=
2b2a6 30 20 29 3b 20 20 20 20 20 20 20 2f 2a 20 41 6c  0 );       /* Al
2b2a7 72 65 61 64 79 20 74 68 72 6f 77 6e 20 74 68 65  ready thrown the
2b2a8 20 65 72 72 6f 72 20 69 66 20 56 44 42 45 20 61   error if VDBE a
2b2a9 6c 6c 6f 63 20 66 61 69 6c 65 64 20 2a 2f 0a 20  lloc failed */. 
2b2aa 20 6c 61 62 65 6c 45 6e 64 20 3d 20 73 71 6c 69   labelEnd = sqli
2b2ab 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
2b2ac 28 76 29 3b 0a 20 20 6c 61 62 65 6c 43 6d 70 72  (v);.  labelCmpr
2b2ad 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
2b2ae 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 0a 20 20  keLabel(v);...  
2b2af 2f 2a 20 50 61 74 63 68 20 75 70 20 74 68 65 20  /* Patch up the 
2b2b0 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 0a  ORDER BY clause.
2b2b1 20 20 2a 2f 0a 20 20 6f 70 20 3d 20 70 2d 3e 6f    */.  op = p->o
2b2b2 70 3b 20 20 0a 20 20 70 50 72 69 6f 72 20 3d 20  p;  .  pPrior = 
2b2b3 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 61 73 73  p->pPrior;.  ass
2b2b4 65 72 74 28 20 70 50 72 69 6f 72 2d 3e 70 4f 72  ert( pPrior->pOr
2b2b5 64 65 72 42 79 3d 3d 30 20 29 3b 0a 20 20 70 4f  derBy==0 );.  pO
2b2b6 72 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64  rderBy = p->pOrd
2b2b7 65 72 42 79 3b 0a 20 20 61 73 73 65 72 74 28 20  erBy;.  assert( 
2b2b8 70 4f 72 64 65 72 42 79 20 29 3b 0a 20 20 6e 4f  pOrderBy );.  nO
2b2b9 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42  rderBy = pOrderB
2b2ba 79 2d 3e 6e 45 78 70 72 3b 0a 0a 20 20 2f 2a 20  y->nExpr;..  /* 
2b2bb 46 6f 72 20 6f 70 65 72 61 74 6f 72 73 20 6f 74  For operators ot
2b2bc 68 65 72 20 74 68 61 6e 20 55 4e 49 4f 4e 20 41  her than UNION A
2b2bd 4c 4c 20 77 65 20 68 61 76 65 20 74 6f 20 6d 61  LL we have to ma
2b2be 6b 65 20 73 75 72 65 20 74 68 61 74 0a 20 20 2a  ke sure that.  *
2b2bf 2a 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  * the ORDER BY c
2b2c0 6c 61 75 73 65 20 63 6f 76 65 72 73 20 65 76 65  lause covers eve
2b2c1 72 79 20 74 65 72 6d 20 6f 66 20 74 68 65 20 72  ry term of the r
2b2c2 65 73 75 6c 74 20 73 65 74 2e 20 20 41 64 64 0a  esult set.  Add.
2b2c3 20 20 2a 2a 20 74 65 72 6d 73 20 74 6f 20 74 68    ** terms to th
2b2c4 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
2b2c5 65 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a  e as necessary..
2b2c6 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 21 3d 54    */.  if( op!=T
2b2c7 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 66 6f 72  K_ALL ){.    for
2b2c8 28 69 3d 31 3b 20 64 62 2d 3e 6d 61 6c 6c 6f 63  (i=1; db->malloc
2b2c9 46 61 69 6c 65 64 3d 3d 30 20 26 26 20 69 3c 3d  Failed==0 && i<=
2b2ca 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  p->pEList->nExpr
2b2cb 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74  ; i++){.      st
2b2cc 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
2b2cd 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 20  em *pItem;.     
2b2ce 20 66 6f 72 28 6a 3d 30 2c 20 70 49 74 65 6d 3d   for(j=0, pItem=
2b2cf 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 6a 3c 6e  pOrderBy->a; j<n
2b2d0 4f 72 64 65 72 42 79 3b 20 6a 2b 2b 2c 20 70 49  OrderBy; j++, pI
2b2d1 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  tem++){.        
2b2d2 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 69  assert( pItem->i
2b2d3 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20 20  Col>0 );.       
2b2d4 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43 6f 6c   if( pItem->iCol
2b2d5 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ==i ) break;.   
2b2d6 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6a     }.      if( j
2b2d7 3d 3d 6e 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ==nOrderBy ){.  
2b2d8 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77        Expr *pNew
2b2d9 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64   = sqlite3Expr(d
2b2da 62 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30  b, TK_INTEGER, 0
2b2db 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
2b2dc 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  New==0 ) return 
2b2dd 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
2b2de 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67        pNew->flag
2b2df 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65  s |= EP_IntValue
2b2e0 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e  ;.        pNew->
2b2e1 75 2e 69 56 61 6c 75 65 20 3d 20 69 3b 0a 20 20  u.iValue = i;.  
2b2e2 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d        pOrderBy =
2b2e3 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2b2e4 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70  Append(pParse, p
2b2e5 4f 72 64 65 72 42 79 2c 20 70 4e 65 77 29 3b 0a  OrderBy, pNew);.
2b2e6 20 20 20 20 20 20 20 20 70 4f 72 64 65 72 42 79          pOrderBy
2b2e7 2d 3e 61 5b 6e 4f 72 64 65 72 42 79 2b 2b 5d 2e  ->a[nOrderBy++].
2b2e8 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 3b 0a 20  iCol = (u16)i;. 
2b2e9 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
2b2ea 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74  ..  /* Compute t
2b2eb 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 70 65  he comparison pe
2b2ec 72 6d 75 74 61 74 69 6f 6e 20 61 6e 64 20 6b 65  rmutation and ke
2b2ed 79 69 6e 66 6f 20 74 68 61 74 20 69 73 20 75 73  yinfo that is us
2b2ee 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 74 68 65  ed with.  ** the
2b2ef 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 75 73 65   permutation use
2b2f0 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69  d to determine i
2b2f1 66 20 74 68 65 20 6e 65 78 74 0a 20 20 2a 2a 20  f the next.  ** 
2b2f2 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 73 20 63  row of results c
2b2f3 6f 6d 65 73 20 66 72 6f 6d 20 73 65 6c 65 63 74  omes from select
2b2f4 41 20 6f 72 20 73 65 6c 65 63 74 42 2e 20 20 41  A or selectB.  A
2b2f5 6c 73 6f 20 61 64 64 20 65 78 70 6c 69 63 69 74  lso add explicit
2b2f6 0a 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 73  .  ** collations
2b2f7 20 74 6f 20 74 68 65 20 4f 52 44 45 52 20 42 59   to the ORDER BY
2b2f8 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 73 6f   clause terms so
2b2f9 20 74 68 61 74 20 77 68 65 6e 20 74 68 65 20 73   that when the s
2b2fa 75 62 71 75 65 72 69 65 73 0a 20 20 2a 2a 20 74  ubqueries.  ** t
2b2fb 6f 20 74 68 65 20 72 69 67 68 74 20 61 6e 64 20  o the right and 
2b2fc 74 68 65 20 6c 65 66 74 20 61 72 65 20 65 76 61  the left are eva
2b2fd 6c 75 61 74 65 64 2c 20 74 68 65 79 20 75 73 65  luated, they use
2b2fe 20 74 68 65 20 63 6f 72 72 65 63 74 0a 20 20 2a   the correct.  *
2b2ff 2a 20 63 6f 6c 6c 61 74 69 6f 6e 2e 0a 20 20 2a  * collation..  *
2b300 2f 0a 20 20 61 50 65 72 6d 75 74 65 20 3d 20 73  /.  aPermute = s
2b301 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
2b302 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74  w(db, sizeof(int
2b303 29 2a 6e 4f 72 64 65 72 42 79 29 3b 0a 20 20 69  )*nOrderBy);.  i
2b304 66 28 20 61 50 65 72 6d 75 74 65 20 29 7b 0a 20  f( aPermute ){. 
2b305 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
2b306 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
2b307 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74      for(i=0, pIt
2b308 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20  em=pOrderBy->a; 
2b309 69 3c 6e 4f 72 64 65 72 42 79 3b 20 69 2b 2b 2c  i<nOrderBy; i++,
2b30a 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
2b30b 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e   assert( pItem->
2b30c 69 43 6f 6c 3e 30 20 20 26 26 20 70 49 74 65 6d  iCol>0  && pItem
2b30d 2d 3e 69 43 6f 6c 3c 3d 70 2d 3e 70 45 4c 69 73  ->iCol<=p->pELis
2b30e 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 20 20  t->nExpr );.    
2b30f 20 20 61 50 65 72 6d 75 74 65 5b 69 5d 20 3d 20    aPermute[i] = 
2b310 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 2d 20 31 3b  pItem->iCol - 1;
2b311 0a 20 20 20 20 7d 0a 20 20 20 20 70 4b 65 79 4d  .    }.    pKeyM
2b312 65 72 67 65 20 3d 0a 20 20 20 20 20 20 73 71 6c  erge =.      sql
2b313 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
2b314 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79  db, sizeof(*pKey
2b315 4d 65 72 67 65 29 2b 6e 4f 72 64 65 72 42 79 2a  Merge)+nOrderBy*
2b316 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a  (sizeof(CollSeq*
2b317 29 2b 31 29 29 3b 0a 20 20 20 20 69 66 28 20 70  )+1));.    if( p
2b318 4b 65 79 4d 65 72 67 65 20 29 7b 0a 20 20 20 20  KeyMerge ){.    
2b319 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 61 53 6f    pKeyMerge->aSo
2b31a 72 74 4f 72 64 65 72 20 3d 20 28 75 38 2a 29 26  rtOrder = (u8*)&
2b31b 70 4b 65 79 4d 65 72 67 65 2d 3e 61 43 6f 6c 6c  pKeyMerge->aColl
2b31c 5b 6e 4f 72 64 65 72 42 79 5d 3b 0a 20 20 20 20  [nOrderBy];.    
2b31d 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 6e 46 69    pKeyMerge->nFi
2b31e 65 6c 64 20 3d 20 28 75 31 36 29 6e 4f 72 64 65  eld = (u16)nOrde
2b31f 72 42 79 3b 0a 20 20 20 20 20 20 70 4b 65 79 4d  rBy;.      pKeyM
2b320 65 72 67 65 2d 3e 65 6e 63 20 3d 20 45 4e 43 28  erge->enc = ENC(
2b321 64 62 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  db);.      for(i
2b322 3d 30 3b 20 69 3c 6e 4f 72 64 65 72 42 79 3b 20  =0; i<nOrderBy; 
2b323 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 43 6f  i++){.        Co
2b324 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20  llSeq *pColl;.  
2b325 20 20 20 20 20 20 45 78 70 72 20 2a 70 54 65 72        Expr *pTer
2b326 6d 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b  m = pOrderBy->a[
2b327 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  i].pExpr;.      
2b328 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 66 6c 61    if( pTerm->fla
2b329 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61  gs & EP_ExpColla
2b32a 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  te ){.          
2b32b 70 43 6f 6c 6c 20 3d 20 70 54 65 72 6d 2d 3e 70  pColl = pTerm->p
2b32c 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 7d 65  Coll;.        }e
2b32d 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70  lse{.          p
2b32e 43 6f 6c 6c 20 3d 20 6d 75 6c 74 69 53 65 6c 65  Coll = multiSele
2b32f 63 74 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  ctCollSeq(pParse
2b330 2c 20 70 2c 20 61 50 65 72 6d 75 74 65 5b 69 5d  , p, aPermute[i]
2b331 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 54 65  );.          pTe
2b332 72 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  rm->flags |= EP_
2b333 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20  ExpCollate;.    
2b334 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 70 43 6f        pTerm->pCo
2b335 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20  ll = pColl;.    
2b336 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4b      }.        pK
2b337 65 79 4d 65 72 67 65 2d 3e 61 43 6f 6c 6c 5b 69  eyMerge->aColl[i
2b338 5d 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  ] = pColl;.     
2b339 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 61 53     pKeyMerge->aS
2b33a 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 70 4f  ortOrder[i] = pO
2b33b 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 73 6f 72  rderBy->a[i].sor
2b33c 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 7d 0a  tOrder;.      }.
2b33d 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
2b33e 20 20 20 70 4b 65 79 4d 65 72 67 65 20 3d 20 30     pKeyMerge = 0
2b33f 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 74  ;.  }..  /* Reat
2b340 74 61 63 68 20 74 68 65 20 4f 52 44 45 52 20 42  tach the ORDER B
2b341 59 20 63 6c 61 75 73 65 20 74 6f 20 74 68 65 20  Y clause to the 
2b342 71 75 65 72 79 2e 0a 20 20 2a 2f 0a 20 20 70 2d  query..  */.  p-
2b343 3e 70 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64  >pOrderBy = pOrd
2b344 65 72 42 79 3b 0a 20 20 70 50 72 69 6f 72 2d 3e  erBy;.  pPrior->
2b345 70 4f 72 64 65 72 42 79 20 3d 20 73 71 6c 69 74  pOrderBy = sqlit
2b346 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 70 50  e3ExprListDup(pP
2b347 61 72 73 65 2d 3e 64 62 2c 20 70 4f 72 64 65 72  arse->db, pOrder
2b348 42 79 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 41 6c  By, 0);..  /* Al
2b349 6c 6f 63 61 74 65 20 61 20 72 61 6e 67 65 20 6f  locate a range o
2b34a 66 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69  f temporary regi
2b34b 73 74 65 72 73 20 61 6e 64 20 74 68 65 20 4b 65  sters and the Ke
2b34c 79 49 6e 66 6f 20 6e 65 65 64 65 64 0a 20 20 2a  yInfo needed.  *
2b34d 2a 20 66 6f 72 20 74 68 65 20 6c 6f 67 69 63 20  * for the logic 
2b34e 74 68 61 74 20 72 65 6d 6f 76 65 73 20 64 75 70  that removes dup
2b34f 6c 69 63 61 74 65 20 72 65 73 75 6c 74 20 72 6f  licate result ro
2b350 77 73 20 77 68 65 6e 20 74 68 65 0a 20 20 2a 2a  ws when the.  **
2b351 20 6f 70 65 72 61 74 6f 72 20 69 73 20 55 4e 49   operator is UNI
2b352 4f 4e 2c 20 45 58 43 45 50 54 2c 20 6f 72 20 49  ON, EXCEPT, or I
2b353 4e 54 45 52 53 45 43 54 20 28 62 75 74 20 6e 6f  NTERSECT (but no
2b354 74 20 55 4e 49 4f 4e 20 41 4c 4c 29 2e 0a 20 20  t UNION ALL)..  
2b355 2a 2f 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  */.  if( op==TK_
2b356 41 4c 4c 20 29 7b 0a 20 20 20 20 72 65 67 50 72  ALL ){.    regPr
2b357 65 76 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  ev = 0;.  }else{
2b358 0a 20 20 20 20 69 6e 74 20 6e 45 78 70 72 20 3d  .    int nExpr =
2b359 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70   p->pEList->nExp
2b35a 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  r;.    assert( n
2b35b 4f 72 64 65 72 42 79 3e 3d 6e 45 78 70 72 20 7c  OrderBy>=nExpr |
2b35c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
2b35d 65 64 20 29 3b 0a 20 20 20 20 72 65 67 50 72 65  ed );.    regPre
2b35e 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  v = sqlite3GetTe
2b35f 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
2b360 6e 45 78 70 72 2b 31 29 3b 0a 20 20 20 20 73 71  nExpr+1);.    sq
2b361 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b362 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
2b363 2c 20 72 65 67 50 72 65 76 29 3b 0a 20 20 20 20  , regPrev);.    
2b364 70 4b 65 79 44 75 70 20 3d 20 73 71 6c 69 74 65  pKeyDup = sqlite
2b365 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
2b366 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2b367 20 20 20 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79      sizeof(*pKey
2b368 44 75 70 29 20 2b 20 6e 45 78 70 72 2a 28 73 69  Dup) + nExpr*(si
2b369 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 2b 31  zeof(CollSeq*)+1
2b36a 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4b 65  ) );.    if( pKe
2b36b 79 44 75 70 20 29 7b 0a 20 20 20 20 20 20 70 4b  yDup ){.      pK
2b36c 65 79 44 75 70 2d 3e 61 53 6f 72 74 4f 72 64 65  eyDup->aSortOrde
2b36d 72 20 3d 20 28 75 38 2a 29 26 70 4b 65 79 44 75  r = (u8*)&pKeyDu
2b36e 70 2d 3e 61 43 6f 6c 6c 5b 6e 45 78 70 72 5d 3b  p->aColl[nExpr];
2b36f 0a 20 20 20 20 20 20 70 4b 65 79 44 75 70 2d 3e  .      pKeyDup->
2b370 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29 6e 45  nField = (u16)nE
2b371 78 70 72 3b 0a 20 20 20 20 20 20 70 4b 65 79 44  xpr;.      pKeyD
2b372 75 70 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62  up->enc = ENC(db
2b373 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  );.      for(i=0
2b374 3b 20 69 3c 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  ; i<nExpr; i++){
2b375 0a 20 20 20 20 20 20 20 20 70 4b 65 79 44 75 70  .        pKeyDup
2b376 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20 6d 75 6c  ->aColl[i] = mul
2b377 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28  tiSelectCollSeq(
2b378 70 50 61 72 73 65 2c 20 70 2c 20 69 29 3b 0a 20  pParse, p, i);. 
2b379 20 20 20 20 20 20 20 70 4b 65 79 44 75 70 2d 3e         pKeyDup->
2b37a 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20  aSortOrder[i] = 
2b37b 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
2b37c 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 53 65 70 61  .  }. .  /* Sepa
2b37d 72 61 74 65 20 74 68 65 20 6c 65 66 74 20 61 6e  rate the left an
2b37e 64 20 74 68 65 20 72 69 67 68 74 20 71 75 65 72  d the right quer
2b37f 79 20 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74 68  y from one anoth
2b380 65 72 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 50 72  er.  */.  p->pPr
2b381 69 6f 72 20 3d 20 30 3b 0a 20 20 70 50 72 69 6f  ior = 0;.  pPrio
2b382 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20  r->pRightmost = 
2b383 30 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 73 6f  0;.  sqlite3Reso
2b384 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28  lveOrderGroupBy(
2b385 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 4f  pParse, p, p->pO
2b386 72 64 65 72 42 79 2c 20 22 4f 52 44 45 52 22 29  rderBy, "ORDER")
2b387 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72 2d 3e  ;.  if( pPrior->
2b388 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20  pPrior==0 ){.   
2b389 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f   sqlite3ResolveO
2b38a 72 64 65 72 47 72 6f 75 70 42 79 28 70 50 61 72  rderGroupBy(pPar
2b38b 73 65 2c 20 70 50 72 69 6f 72 2c 20 70 50 72 69  se, pPrior, pPri
2b38c 6f 72 2d 3e 70 4f 72 64 65 72 42 79 2c 20 22 4f  or->pOrderBy, "O
2b38d 52 44 45 52 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f  RDER");.  }..  /
2b38e 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 6c 69  * Compute the li
2b38f 6d 69 74 20 72 65 67 69 73 74 65 72 73 20 2a 2f  mit registers */
2b390 0a 20 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52  .  computeLimitR
2b391 65 67 69 73 74 65 72 73 28 70 50 61 72 73 65 2c  egisters(pParse,
2b392 20 70 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20   p, labelEnd);. 
2b393 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 26   if( p->iLimit &
2b394 26 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 7b 0a  & op==TK_ALL ){.
2b395 20 20 20 20 72 65 67 4c 69 6d 69 74 41 20 3d 20      regLimitA = 
2b396 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2b397 20 20 20 20 72 65 67 4c 69 6d 69 74 42 20 3d 20      regLimitB = 
2b398 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2b399 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b39a 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79  ddOp2(v, OP_Copy
2b39b 2c 20 70 2d 3e 69 4f 66 66 73 65 74 20 3f 20 70  , p->iOffset ? p
2b39c 2d 3e 69 4f 66 66 73 65 74 2b 31 20 3a 20 70 2d  ->iOffset+1 : p-
2b39d 3e 69 4c 69 6d 69 74 2c 0a 20 20 20 20 20 20 20  >iLimit,.       
2b39e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b39f 20 20 20 20 20 20 20 20 20 20 20 72 65 67 4c 69             regLi
2b3a0 6d 69 74 41 29 3b 0a 20 20 20 20 73 71 6c 69 74  mitA);.    sqlit
2b3a1 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b3a2 4f 50 5f 43 6f 70 79 2c 20 72 65 67 4c 69 6d 69  OP_Copy, regLimi
2b3a3 74 41 2c 20 72 65 67 4c 69 6d 69 74 42 29 3b 0a  tA, regLimitB);.
2b3a4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 67    }else{.    reg
2b3a5 4c 69 6d 69 74 41 20 3d 20 72 65 67 4c 69 6d 69  LimitA = regLimi
2b3a6 74 42 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71  tB = 0;.  }.  sq
2b3a7 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
2b3a8 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a  db, p->pLimit);.
2b3a9 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b    p->pLimit = 0;
2b3aa 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
2b3ab 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f 66 66  lete(db, p->pOff
2b3ac 73 65 74 29 3b 0a 20 20 70 2d 3e 70 4f 66 66 73  set);.  p->pOffs
2b3ad 65 74 20 3d 20 30 3b 0a 0a 20 20 72 65 67 41 64  et = 0;..  regAd
2b3ae 64 72 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  drA = ++pParse->
2b3af 6e 4d 65 6d 3b 0a 20 20 72 65 67 45 6f 66 41 20  nMem;.  regEofA 
2b3b0 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2b3b1 3b 0a 20 20 72 65 67 41 64 64 72 42 20 3d 20 2b  ;.  regAddrB = +
2b3b2 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
2b3b3 20 72 65 67 45 6f 66 42 20 3d 20 2b 2b 70 50 61   regEofB = ++pPa
2b3b4 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67  rse->nMem;.  reg
2b3b5 4f 75 74 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d  OutA = ++pParse-
2b3b6 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 4f 75 74 42  >nMem;.  regOutB
2b3b7 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
2b3b8 6d 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  m;.  sqlite3Sele
2b3b9 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74  ctDestInit(&dest
2b3ba 41 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65  A, SRT_Coroutine
2b3bb 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 73  , regAddrA);.  s
2b3bc 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74  qlite3SelectDest
2b3bd 49 6e 69 74 28 26 64 65 73 74 42 2c 20 53 52 54  Init(&destB, SRT
2b3be 5f 43 6f 72 6f 75 74 69 6e 65 2c 20 72 65 67 41  _Coroutine, regA
2b3bf 64 64 72 42 29 3b 0a 0a 20 20 2f 2a 20 4a 75 6d  ddrB);..  /* Jum
2b3c0 70 20 70 61 73 74 20 74 68 65 20 76 61 72 69 6f  p past the vario
2b3c1 75 73 20 73 75 62 72 6f 75 74 69 6e 65 73 20 61  us subroutines a
2b3c2 6e 64 20 63 6f 72 6f 75 74 69 6e 65 73 20 74 6f  nd coroutines to
2b3c3 20 74 68 65 20 6d 61 69 6e 0a 20 20 2a 2a 20 6d   the main.  ** m
2b3c4 65 72 67 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20  erge loop.  */. 
2b3c5 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j1 = sqlite3Vdb
2b3c6 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f  eAddOp0(v, OP_Go
2b3c7 74 6f 29 3b 0a 20 20 61 64 64 72 53 65 6c 65 63  to);.  addrSelec
2b3c8 74 41 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  tA = sqlite3Vdbe
2b3c9 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
2b3ca 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
2b3cb 61 20 63 6f 72 6f 75 74 69 6e 65 20 74 6f 20 65  a coroutine to e
2b3cc 76 61 6c 75 61 74 65 20 74 68 65 20 53 45 4c 45  valuate the SELE
2b3cd 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  CT statement to 
2b3ce 74 68 65 0a 20 20 2a 2a 20 6c 65 66 74 20 6f 66  the.  ** left of
2b3cf 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 6f 70   the compound op
2b3d0 65 72 61 74 6f 72 20 2d 20 74 68 65 20 22 41 22  erator - the "A"
2b3d1 20 73 65 6c 65 63 74 2e 0a 20 20 2a 2f 0a 20 20   select..  */.  
2b3d2 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28  VdbeNoopComment(
2b3d3 28 76 2c 20 22 42 65 67 69 6e 20 63 6f 72 6f 75  (v, "Begin corou
2b3d4 74 69 6e 65 20 66 6f 72 20 6c 65 66 74 20 53 45  tine for left SE
2b3d5 4c 45 43 54 22 29 29 3b 0a 20 20 70 50 72 69 6f  LECT"));.  pPrio
2b3d6 72 2d 3e 69 4c 69 6d 69 74 20 3d 20 72 65 67 4c  r->iLimit = regL
2b3d7 69 6d 69 74 41 3b 0a 20 20 73 71 6c 69 74 65 33  imitA;.  sqlite3
2b3d8 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
2b3d9 50 72 69 6f 72 2c 20 26 64 65 73 74 41 29 3b 0a  Prior, &destA);.
2b3da 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b3db 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2b3dc 72 2c 20 31 2c 20 72 65 67 45 6f 66 41 29 3b 0a  r, 1, regEofA);.
2b3dd 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b3de 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
2b3df 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 56 64   regAddrA);.  Vd
2b3e0 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76  beNoopComment((v
2b3e1 2c 20 22 45 6e 64 20 63 6f 72 6f 75 74 69 6e 65  , "End coroutine
2b3e2 20 66 6f 72 20 6c 65 66 74 20 53 45 4c 45 43 54   for left SELECT
2b3e3 22 29 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  "));..  /* Gener
2b3e4 61 74 65 20 61 20 63 6f 72 6f 75 74 69 6e 65 20  ate a coroutine 
2b3e5 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65 20  to evaluate the 
2b3e6 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2b3e7 20 6f 6e 20 0a 20 20 2a 2a 20 74 68 65 20 72 69   on .  ** the ri
2b3e8 67 68 74 20 2d 20 74 68 65 20 22 42 22 20 73 65  ght - the "B" se
2b3e9 6c 65 63 74 0a 20 20 2a 2f 0a 20 20 61 64 64 72  lect.  */.  addr
2b3ea 53 65 6c 65 63 74 42 20 3d 20 73 71 6c 69 74 65  SelectB = sqlite
2b3eb 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
2b3ec 28 76 29 3b 0a 20 20 56 64 62 65 4e 6f 6f 70 43  (v);.  VdbeNoopC
2b3ed 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 42 65 67 69  omment((v, "Begi
2b3ee 6e 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20  n coroutine for 
2b3ef 72 69 67 68 74 20 53 45 4c 45 43 54 22 29 29 3b  right SELECT"));
2b3f0 0a 20 20 73 61 76 65 64 4c 69 6d 69 74 20 3d 20  .  savedLimit = 
2b3f1 70 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 73 61 76  p->iLimit;.  sav
2b3f2 65 64 4f 66 66 73 65 74 20 3d 20 70 2d 3e 69 4f  edOffset = p->iO
2b3f3 66 66 73 65 74 3b 0a 20 20 70 2d 3e 69 4c 69 6d  ffset;.  p->iLim
2b3f4 69 74 20 3d 20 72 65 67 4c 69 6d 69 74 42 3b 0a  it = regLimitB;.
2b3f5 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30    p->iOffset = 0
2b3f6 3b 20 20 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  ;  .  sqlite3Sel
2b3f7 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20 26  ect(pParse, p, &
2b3f8 64 65 73 74 42 29 3b 0a 20 20 70 2d 3e 69 4c 69  destB);.  p->iLi
2b3f9 6d 69 74 20 3d 20 73 61 76 65 64 4c 69 6d 69 74  mit = savedLimit
2b3fa 3b 0a 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d  ;.  p->iOffset =
2b3fb 20 73 61 76 65 64 4f 66 66 73 65 74 3b 0a 20 20   savedOffset;.  
2b3fc 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b3fd 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2b3fe 20 31 2c 20 72 65 67 45 6f 66 42 29 3b 0a 20 20   1, regEofB);.  
2b3ff 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b400 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72  1(v, OP_Yield, r
2b401 65 67 41 64 64 72 42 29 3b 0a 20 20 56 64 62 65  egAddrB);.  Vdbe
2b402 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  NoopComment((v, 
2b403 22 45 6e 64 20 63 6f 72 6f 75 74 69 6e 65 20 66  "End coroutine f
2b404 6f 72 20 72 69 67 68 74 20 53 45 4c 45 43 54 22  or right SELECT"
2b405 29 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61  ));..  /* Genera
2b406 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20  te a subroutine 
2b407 74 68 61 74 20 6f 75 74 70 75 74 73 20 74 68 65  that outputs the
2b408 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20   current row of 
2b409 74 68 65 20 41 0a 20 20 2a 2a 20 73 65 6c 65 63  the A.  ** selec
2b40a 74 20 61 73 20 74 68 65 20 6e 65 78 74 20 6f 75  t as the next ou
2b40b 74 70 75 74 20 72 6f 77 20 6f 66 20 74 68 65 20  tput row of the 
2b40c 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2e  compound select.
2b40d 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70  .  */.  VdbeNoop
2b40e 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4f 75 74  Comment((v, "Out
2b40f 70 75 74 20 72 6f 75 74 69 6e 65 20 66 6f 72 20  put routine for 
2b410 41 22 29 29 3b 0a 20 20 61 64 64 72 4f 75 74 41  A"));.  addrOutA
2b411 20 3d 20 67 65 6e 65 72 61 74 65 4f 75 74 70 75   = generateOutpu
2b412 74 53 75 62 72 6f 75 74 69 6e 65 28 70 50 61 72  tSubroutine(pPar
2b413 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  se,.            
2b414 20 20 20 20 20 70 2c 20 26 64 65 73 74 41 2c 20       p, &destA, 
2b415 70 44 65 73 74 2c 20 72 65 67 4f 75 74 41 2c 0a  pDest, regOutA,.
2b416 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b417 20 72 65 67 50 72 65 76 2c 20 70 4b 65 79 44 75   regPrev, pKeyDu
2b418 70 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  p, P4_KEYINFO_HA
2b419 4e 44 4f 46 46 2c 20 6c 61 62 65 6c 45 6e 64 29  NDOFF, labelEnd)
2b41a 3b 0a 20 20 0a 20 20 2f 2a 20 47 65 6e 65 72 61  ;.  .  /* Genera
2b41b 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20  te a subroutine 
2b41c 74 68 61 74 20 6f 75 74 70 75 74 73 20 74 68 65  that outputs the
2b41d 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20   current row of 
2b41e 74 68 65 20 42 0a 20 20 2a 2a 20 73 65 6c 65 63  the B.  ** selec
2b41f 74 20 61 73 20 74 68 65 20 6e 65 78 74 20 6f 75  t as the next ou
2b420 74 70 75 74 20 72 6f 77 20 6f 66 20 74 68 65 20  tput row of the 
2b421 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2e  compound select.
2b422 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 3d 3d  .  */.  if( op==
2b423 54 4b 5f 41 4c 4c 20 7c 7c 20 6f 70 3d 3d 54 4b  TK_ALL || op==TK
2b424 5f 55 4e 49 4f 4e 20 29 7b 0a 20 20 20 20 56 64  _UNION ){.    Vd
2b425 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76  beNoopComment((v
2b426 2c 20 22 4f 75 74 70 75 74 20 72 6f 75 74 69 6e  , "Output routin
2b427 65 20 66 6f 72 20 42 22 29 29 3b 0a 20 20 20 20  e for B"));.    
2b428 61 64 64 72 4f 75 74 42 20 3d 20 67 65 6e 65 72  addrOutB = gener
2b429 61 74 65 4f 75 74 70 75 74 53 75 62 72 6f 75 74  ateOutputSubrout
2b42a 69 6e 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20  ine(pParse,.    
2b42b 20 20 20 20 20 20 20 20 20 20 20 20 20 70 2c 20               p, 
2b42c 26 64 65 73 74 42 2c 20 70 44 65 73 74 2c 20 72  &destB, pDest, r
2b42d 65 67 4f 75 74 42 2c 0a 20 20 20 20 20 20 20 20  egOutB,.        
2b42e 20 20 20 20 20 20 20 20 20 72 65 67 50 72 65 76           regPrev
2b42f 2c 20 70 4b 65 79 44 75 70 2c 20 50 34 5f 4b 45  , pKeyDup, P4_KE
2b430 59 49 4e 46 4f 5f 53 54 41 54 49 43 2c 20 6c 61  YINFO_STATIC, la
2b431 62 65 6c 45 6e 64 29 3b 0a 20 20 7d 0a 0a 20 20  belEnd);.  }..  
2b432 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73 75  /* Generate a su
2b433 62 72 6f 75 74 69 6e 65 20 74 6f 20 72 75 6e 20  broutine to run 
2b434 77 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73  when the results
2b435 20 66 72 6f 6d 20 73 65 6c 65 63 74 20 41 0a 20   from select A. 
2b436 20 2a 2a 20 61 72 65 20 65 78 68 61 75 73 74 65   ** are exhauste
2b437 64 20 61 6e 64 20 6f 6e 6c 79 20 64 61 74 61 20  d and only data 
2b438 69 6e 20 73 65 6c 65 63 74 20 42 20 72 65 6d 61  in select B rema
2b439 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65  ins..  */.  Vdbe
2b43a 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  NoopComment((v, 
2b43b 22 65 6f 66 2d 41 20 73 75 62 72 6f 75 74 69 6e  "eof-A subroutin
2b43c 65 22 29 29 3b 0a 20 20 69 66 28 20 6f 70 3d 3d  e"));.  if( op==
2b43d 54 4b 5f 45 58 43 45 50 54 20 7c 7c 20 6f 70 3d  TK_EXCEPT || op=
2b43e 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 29 7b  =TK_INTERSECT ){
2b43f 0a 20 20 20 20 61 64 64 72 45 6f 66 41 20 3d 20  .    addrEofA = 
2b440 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b441 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
2b442 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 7d 65   labelEnd);.  }e
2b443 6c 73 65 7b 20 20 0a 20 20 20 20 61 64 64 72 45  lse{  .    addrE
2b444 6f 66 41 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ofA = sqlite3Vdb
2b445 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66  eAddOp2(v, OP_If
2b446 2c 20 72 65 67 45 6f 66 42 2c 20 6c 61 62 65 6c  , regEofB, label
2b447 45 6e 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  End);.    sqlite
2b448 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2b449 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 42  P_Gosub, regOutB
2b44a 2c 20 61 64 64 72 4f 75 74 42 29 3b 0a 20 20 20  , addrOutB);.   
2b44b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b44c 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
2b44d 72 65 67 41 64 64 72 42 29 3b 0a 20 20 20 20 73  regAddrB);.    s
2b44e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b44f 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
2b450 61 64 64 72 45 6f 66 41 29 3b 0a 20 20 7d 0a 0a  addrEofA);.  }..
2b451 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20    /* Generate a 
2b452 73 75 62 72 6f 75 74 69 6e 65 20 74 6f 20 72 75  subroutine to ru
2b453 6e 20 77 68 65 6e 20 74 68 65 20 72 65 73 75 6c  n when the resul
2b454 74 73 20 66 72 6f 6d 20 73 65 6c 65 63 74 20 42  ts from select B
2b455 0a 20 20 2a 2a 20 61 72 65 20 65 78 68 61 75 73  .  ** are exhaus
2b456 74 65 64 20 61 6e 64 20 6f 6e 6c 79 20 64 61 74  ted and only dat
2b457 61 20 69 6e 20 73 65 6c 65 63 74 20 41 20 72 65  a in select A re
2b458 6d 61 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  mains..  */.  if
2b459 28 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45  ( op==TK_INTERSE
2b45a 43 54 20 29 7b 0a 20 20 20 20 61 64 64 72 45 6f  CT ){.    addrEo
2b45b 66 42 20 3d 20 61 64 64 72 45 6f 66 41 3b 0a 20  fB = addrEofA;. 
2b45c 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 56 64   }else{  .    Vd
2b45d 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76  beNoopComment((v
2b45e 2c 20 22 65 6f 66 2d 42 20 73 75 62 72 6f 75 74  , "eof-B subrout
2b45f 69 6e 65 22 29 29 3b 0a 20 20 20 20 61 64 64 72  ine"));.    addr
2b460 45 6f 66 42 20 3d 20 73 71 6c 69 74 65 33 56 64  EofB = sqlite3Vd
2b461 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2b462 66 2c 20 72 65 67 45 6f 66 41 2c 20 6c 61 62 65  f, regEofA, labe
2b463 6c 45 6e 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  lEnd);.    sqlit
2b464 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b465 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74  OP_Gosub, regOut
2b466 41 2c 20 61 64 64 72 4f 75 74 41 29 3b 0a 20 20  A, addrOutA);.  
2b467 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b468 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
2b469 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 20 20   regAddrA);.    
2b46a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b46b 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
2b46c 20 61 64 64 72 45 6f 66 42 29 3b 0a 20 20 7d 0a   addrEofB);.  }.
2b46d 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63  .  /* Generate c
2b46e 6f 64 65 20 74 6f 20 68 61 6e 64 6c 65 20 74 68  ode to handle th
2b46f 65 20 63 61 73 65 20 6f 66 20 41 3c 42 0a 20 20  e case of A<B.  
2b470 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d  */.  VdbeNoopCom
2b471 6d 65 6e 74 28 28 76 2c 20 22 41 2d 6c 74 2d 42  ment((v, "A-lt-B
2b472 20 73 75 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a   subroutine"));.
2b473 20 20 61 64 64 72 41 6c 74 42 20 3d 20 73 71 6c    addrAltB = sql
2b474 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b475 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f  , OP_Gosub, regO
2b476 75 74 41 2c 20 61 64 64 72 4f 75 74 41 29 3b 0a  utA, addrOutA);.
2b477 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b478 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
2b479 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 73 71   regAddrA);.  sq
2b47a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b47b 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66  v, OP_If, regEof
2b47c 41 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a 20 20  A, addrEofA);.  
2b47d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b47e 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
2b47f 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 0a 20 20   labelCmpr);..  
2b480 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
2b481 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 20 63   to handle the c
2b482 61 73 65 20 6f 66 20 41 3d 3d 42 0a 20 20 2a 2f  ase of A==B.  */
2b483 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c  .  if( op==TK_AL
2b484 4c 20 29 7b 0a 20 20 20 20 61 64 64 72 41 65 71  L ){.    addrAeq
2b485 42 20 3d 20 61 64 64 72 41 6c 74 42 3b 0a 20 20  B = addrAltB;.  
2b486 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b  }else if( op==TK
2b487 5f 49 4e 54 45 52 53 45 43 54 20 29 7b 0a 20 20  _INTERSECT ){.  
2b488 20 20 61 64 64 72 41 65 71 42 20 3d 20 61 64 64    addrAeqB = add
2b489 72 41 6c 74 42 3b 0a 20 20 20 20 61 64 64 72 41  rAltB;.    addrA
2b48a 6c 74 42 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ltB++;.  }else{.
2b48b 20 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d      VdbeNoopComm
2b48c 65 6e 74 28 28 76 2c 20 22 41 2d 65 71 2d 42 20  ent((v, "A-eq-B 
2b48d 73 75 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20  subroutine"));. 
2b48e 20 20 20 61 64 64 72 41 65 71 42 20 3d 0a 20 20     addrAeqB =.  
2b48f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b490 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
2b491 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 20 20   regAddrA);.    
2b492 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b493 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45  2(v, OP_If, regE
2b494 6f 66 41 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a  ofA, addrEofA);.
2b495 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b496 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
2b497 2c 20 30 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b  , 0, labelCmpr);
2b498 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  .  }..  /* Gener
2b499 61 74 65 20 63 6f 64 65 20 74 6f 20 68 61 6e 64  ate code to hand
2b49a 6c 65 20 74 68 65 20 63 61 73 65 20 6f 66 20 41  le the case of A
2b49b 3e 42 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f  >B.  */.  VdbeNo
2b49c 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 41  opComment((v, "A
2b49d 2d 67 74 2d 42 20 73 75 62 72 6f 75 74 69 6e 65  -gt-B subroutine
2b49e 22 29 29 3b 0a 20 20 61 64 64 72 41 67 74 42 20  "));.  addrAgtB 
2b49f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
2b4a0 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 69  rentAddr(v);.  i
2b4a1 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 7c 7c  f( op==TK_ALL ||
2b4a2 20 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 29 7b   op==TK_UNION ){
2b4a3 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2b4a4 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73  AddOp2(v, OP_Gos
2b4a5 75 62 2c 20 72 65 67 4f 75 74 42 2c 20 61 64 64  ub, regOutB, add
2b4a6 72 4f 75 74 42 29 3b 0a 20 20 7d 0a 20 20 73 71  rOutB);.  }.  sq
2b4a7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2b4a8 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67  v, OP_Yield, reg
2b4a9 41 64 64 72 42 29 3b 0a 20 20 73 71 6c 69 74 65  AddrB);.  sqlite
2b4aa 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2b4ab 50 5f 49 66 2c 20 72 65 67 45 6f 66 42 2c 20 61  P_If, regEofB, a
2b4ac 64 64 72 45 6f 66 42 29 3b 0a 20 20 73 71 6c 69  ddrEofB);.  sqli
2b4ad 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b4ae 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 6c 61 62   OP_Goto, 0, lab
2b4af 65 6c 43 6d 70 72 29 3b 0a 0a 20 20 2f 2a 20 54  elCmpr);..  /* T
2b4b0 68 69 73 20 63 6f 64 65 20 72 75 6e 73 20 6f 6e  his code runs on
2b4b1 63 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65  ce to initialize
2b4b2 20 65 76 65 72 79 74 68 69 6e 67 2e 0a 20 20 2a   everything..  *
2b4b3 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a  /.  sqlite3VdbeJ
2b4b4 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a  umpHere(v, j1);.
2b4b5 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b4b6 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2b4b7 72 2c 20 30 2c 20 72 65 67 45 6f 66 41 29 3b 0a  r, 0, regEofA);.
2b4b8 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b4b9 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2b4ba 72 2c 20 30 2c 20 72 65 67 45 6f 66 42 29 3b 0a  r, 0, regEofB);.
2b4bb 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b4bc 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c  Op2(v, OP_Gosub,
2b4bd 20 72 65 67 41 64 64 72 41 2c 20 61 64 64 72 53   regAddrA, addrS
2b4be 65 6c 65 63 74 41 29 3b 0a 20 20 73 71 6c 69 74  electA);.  sqlit
2b4bf 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b4c0 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 41 64 64  OP_Gosub, regAdd
2b4c1 72 42 2c 20 61 64 64 72 53 65 6c 65 63 74 42 29  rB, addrSelectB)
2b4c2 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2b4c3 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp2(v, OP_If, 
2b4c4 72 65 67 45 6f 66 41 2c 20 61 64 64 72 45 6f 66  regEofA, addrEof
2b4c5 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  A);.  sqlite3Vdb
2b4c6 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66  eAddOp2(v, OP_If
2b4c7 2c 20 72 65 67 45 6f 66 42 2c 20 61 64 64 72 45  , regEofB, addrE
2b4c8 6f 66 42 29 3b 0a 0a 20 20 2f 2a 20 49 6d 70 6c  ofB);..  /* Impl
2b4c9 65 6d 65 6e 74 20 74 68 65 20 6d 61 69 6e 20 6d  ement the main m
2b4ca 65 72 67 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20  erge loop.  */. 
2b4cb 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
2b4cc 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6c 61 62 65  lveLabel(v, labe
2b4cd 6c 43 6d 70 72 29 3b 0a 20 20 73 71 6c 69 74 65  lCmpr);.  sqlite
2b4ce 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
2b4cf 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 2c 20 30  P_Permutation, 0
2b4d0 2c 20 30 2c 20 30 2c 20 28 63 68 61 72 2a 29 61  , 0, 0, (char*)a
2b4d1 50 65 72 6d 75 74 65 2c 20 50 34 5f 49 4e 54 41  Permute, P4_INTA
2b4d2 52 52 41 59 29 3b 0a 20 20 73 71 6c 69 74 65 33  RRAY);.  sqlite3
2b4d3 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2b4d4 5f 43 6f 6d 70 61 72 65 2c 20 64 65 73 74 41 2e  _Compare, destA.
2b4d5 69 4d 65 6d 2c 20 64 65 73 74 42 2e 69 4d 65 6d  iMem, destB.iMem
2b4d6 2c 20 6e 4f 72 64 65 72 42 79 2c 0a 20 20 20 20  , nOrderBy,.    
2b4d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b4d8 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79       (char*)pKey
2b4d9 4d 65 72 67 65 2c 20 50 34 5f 4b 45 59 49 4e 46  Merge, P4_KEYINF
2b4da 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 73 71  O_HANDOFF);.  sq
2b4db 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2b4dc 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 61 64 64 72  v, OP_Jump, addr
2b4dd 41 6c 74 42 2c 20 61 64 64 72 41 65 71 42 2c 20  AltB, addrAeqB, 
2b4de 61 64 64 72 41 67 74 42 29 3b 0a 0a 20 20 2f 2a  addrAgtB);..  /*
2b4df 20 52 65 6c 65 61 73 65 20 74 65 6d 70 6f 72 61   Release tempora
2b4e0 72 79 20 72 65 67 69 73 74 65 72 73 0a 20 20 2a  ry registers.  *
2b4e1 2f 0a 20 20 69 66 28 20 72 65 67 50 72 65 76 20  /.  if( regPrev 
2b4e2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  ){.    sqlite3Re
2b4e3 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70  leaseTempRange(p
2b4e4 50 61 72 73 65 2c 20 72 65 67 50 72 65 76 2c 20  Parse, regPrev, 
2b4e5 6e 4f 72 64 65 72 42 79 2b 31 29 3b 0a 20 20 7d  nOrderBy+1);.  }
2b4e6 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74  ..  /* Jump to t
2b4e7 68 65 20 74 68 69 73 20 70 6f 69 6e 74 20 69 6e  he this point in
2b4e8 20 6f 72 64 65 72 20 74 6f 20 74 65 72 6d 69 6e   order to termin
2b4e9 61 74 65 20 74 68 65 20 71 75 65 72 79 2e 0a 20  ate the query.. 
2b4ea 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
2b4eb 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2b4ec 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 0a 20 20 2f   labelEnd);..  /
2b4ed 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72  * Set the number
2b4ee 20 6f 66 20 6f 75 74 70 75 74 20 63 6f 6c 75 6d   of output colum
2b4ef 6e 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44  ns.  */.  if( pD
2b4f0 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f  est->eDest==SRT_
2b4f1 4f 75 74 70 75 74 20 29 7b 0a 20 20 20 20 53 65  Output ){.    Se
2b4f2 6c 65 63 74 20 2a 70 46 69 72 73 74 20 3d 20 70  lect *pFirst = p
2b4f3 50 72 69 6f 72 3b 0a 20 20 20 20 77 68 69 6c 65  Prior;.    while
2b4f4 28 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72  ( pFirst->pPrior
2b4f5 20 29 20 70 46 69 72 73 74 20 3d 20 70 46 69 72   ) pFirst = pFir
2b4f6 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20  st->pPrior;.    
2b4f7 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61  generateColumnNa
2b4f8 6d 65 73 28 70 50 61 72 73 65 2c 20 30 2c 20 70  mes(pParse, 0, p
2b4f9 46 69 72 73 74 2d 3e 70 45 4c 69 73 74 29 3b 0a  First->pEList);.
2b4fa 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 73 73 65    }..  /* Reasse
2b4fb 6d 62 6c 79 20 74 68 65 20 63 6f 6d 70 6f 75 6e  mbly the compoun
2b4fc 64 20 71 75 65 72 79 20 73 6f 20 74 68 61 74 20  d query so that 
2b4fd 69 74 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64  it will be freed
2b4fe 20 63 6f 72 72 65 63 74 6c 79 0a 20 20 2a 2a 20   correctly.  ** 
2b4ff 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66  by the calling f
2b500 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28  unction */.  if(
2b501 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20   p->pPrior ){.  
2b502 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2b503 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 50 72  elete(db, p->pPr
2b504 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70  ior);.  }.  p->p
2b505 50 72 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a  Prior = pPrior;.
2b506 0a 20 20 2f 2a 2a 2a 20 54 42 44 3a 20 20 49 6e  .  /*** TBD:  In
2b507 73 65 72 74 20 73 75 62 72 6f 75 74 69 6e 65 20  sert subroutine 
2b508 63 61 6c 6c 73 20 74 6f 20 63 6c 6f 73 65 20 63  calls to close c
2b509 75 72 73 6f 72 73 20 6f 6e 20 69 6e 63 6f 6d 70  ursors on incomp
2b50a 6c 65 74 65 0a 20 20 2a 2a 2a 2a 20 73 75 62 71  lete.  **** subq
2b50b 75 65 72 69 65 73 20 2a 2a 2a 2a 2f 0a 20 20 72  ueries ****/.  r
2b50c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2b50d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21  .}.#endif..#if !
2b50e 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2b50f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c  MIT_SUBQUERY) ||
2b510 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2b511 5f 4f 4d 49 54 5f 56 49 45 57 29 0a 2f 2a 20 46  _OMIT_VIEW)./* F
2b512 6f 72 77 61 72 64 20 44 65 63 6c 61 72 61 74 69  orward Declarati
2b513 6f 6e 73 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ons */.static vo
2b514 69 64 20 73 75 62 73 74 45 78 70 72 4c 69 73 74  id substExprList
2b515 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 4c  (sqlite3*, ExprL
2b516 69 73 74 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c  ist*, int, ExprL
2b517 69 73 74 2a 29 3b 0a 73 74 61 74 69 63 20 76 6f  ist*);.static vo
2b518 69 64 20 73 75 62 73 74 53 65 6c 65 63 74 28 73  id substSelect(s
2b519 71 6c 69 74 65 33 2a 2c 20 53 65 6c 65 63 74 20  qlite3*, Select 
2b51a 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74  *, int, ExprList
2b51b 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 63 61 6e   *);../*.** Scan
2b51c 20 74 68 72 6f 75 67 68 20 74 68 65 20 65 78 70   through the exp
2b51d 72 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 20  ression pExpr.  
2b51e 52 65 70 6c 61 63 65 20 65 76 65 72 79 20 72 65  Replace every re
2b51f 66 65 72 65 6e 63 65 20 74 6f 0a 2a 2a 20 61 20  ference to.** a 
2b520 63 6f 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20  column in table 
2b521 6e 75 6d 62 65 72 20 69 54 61 62 6c 65 20 77 69  number iTable wi
2b522 74 68 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  th a copy of the
2b523 20 69 43 6f 6c 75 6d 6e 2d 74 68 0a 2a 2a 20 65   iColumn-th.** e
2b524 6e 74 72 79 20 69 6e 20 70 45 4c 69 73 74 2e 20  ntry in pEList. 
2b525 20 28 42 75 74 20 6c 65 61 76 65 20 72 65 66 65   (But leave refe
2b526 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 52 4f  rences to the RO
2b527 57 49 44 20 63 6f 6c 75 6d 6e 20 0a 2a 2a 20 75  WID column .** u
2b528 6e 63 68 61 6e 67 65 64 2e 29 0a 2a 2a 0a 2a 2a  nchanged.).**.**
2b529 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2b52a 20 70 61 72 74 20 6f 66 20 74 68 65 20 66 6c 61   part of the fla
2b52b 74 74 65 6e 69 6e 67 20 70 72 6f 63 65 64 75 72  ttening procedur
2b52c 65 2e 20 20 41 20 73 75 62 71 75 65 72 79 0a 2a  e.  A subquery.*
2b52d 2a 20 77 68 6f 73 65 20 72 65 73 75 6c 74 20 73  * whose result s
2b52e 65 74 20 69 73 20 64 65 66 69 6e 65 64 20 62 79  et is defined by
2b52f 20 70 45 4c 69 73 74 20 61 70 70 65 61 72 73 20   pEList appears 
2b530 61 73 20 65 6e 74 72 79 20 69 6e 20 74 68 65 0a  as entry in the.
2b531 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  ** FROM clause o
2b532 66 20 61 20 53 45 4c 45 43 54 20 73 75 63 68 20  f a SELECT such 
2b533 74 68 61 74 20 74 68 65 20 56 44 42 45 20 63 75  that the VDBE cu
2b534 72 73 6f 72 20 61 73 73 69 67 6e 65 64 20 74 6f  rsor assigned to
2b535 20 74 68 61 74 0a 2a 2a 20 46 4f 52 4d 20 63 6c   that.** FORM cl
2b536 61 75 73 65 20 65 6e 74 72 79 20 69 73 20 69 54  ause entry is iT
2b537 61 62 6c 65 2e 20 20 54 68 69 73 20 72 6f 75 74  able.  This rout
2b538 69 6e 65 20 6d 61 6b 65 20 74 68 65 20 6e 65 63  ine make the nec
2b539 65 73 73 61 72 79 20 0a 2a 2a 20 63 68 61 6e 67  essary .** chang
2b53a 65 73 20 74 6f 20 70 45 78 70 72 20 73 6f 20 74  es to pExpr so t
2b53b 68 61 74 20 69 74 20 72 65 66 65 72 73 20 64 69  hat it refers di
2b53c 72 65 63 74 6c 79 20 74 6f 20 74 68 65 20 73 6f  rectly to the so
2b53d 75 72 63 65 20 74 61 62 6c 65 0a 2a 2a 20 6f 66  urce table.** of
2b53e 20 74 68 65 20 73 75 62 71 75 65 72 79 20 72 61   the subquery ra
2b53f 74 68 65 72 20 74 68 65 20 72 65 73 75 6c 74 20  ther the result 
2b540 73 65 74 20 6f 66 20 74 68 65 20 73 75 62 71 75  set of the subqu
2b541 65 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 45  ery..*/.static E
2b542 78 70 72 20 2a 73 75 62 73 74 45 78 70 72 28 0a  xpr *substExpr(.
2b543 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
2b544 20 20 20 20 20 20 2f 2a 20 52 65 70 6f 72 74 20        /* Report 
2b545 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 74 6f  malloc errors to
2b546 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e   this connection
2b547 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70   */.  Expr *pExp
2b548 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70  r,        /* Exp
2b549 72 20 69 6e 20 77 68 69 63 68 20 73 75 62 73 74  r in which subst
2b54a 69 74 75 74 69 6f 6e 20 6f 63 63 75 72 73 20 2a  itution occurs *
2b54b 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20  /.  int iTable, 
2b54c 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
2b54d 20 74 6f 20 62 65 20 73 75 62 73 74 69 74 75 74   to be substitut
2b54e 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ed */.  ExprList
2b54f 20 2a 70 45 4c 69 73 74 20 20 20 20 2f 2a 20 53   *pEList    /* S
2b550 75 62 73 74 69 74 75 74 65 20 65 78 70 72 65 73  ubstitute expres
2b551 73 69 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 69 66  sions */.){.  if
2b552 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74  ( pExpr==0 ) ret
2b553 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 45 78  urn 0;.  if( pEx
2b554 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d  pr->op==TK_COLUM
2b555 4e 20 26 26 20 70 45 78 70 72 2d 3e 69 54 61 62  N && pExpr->iTab
2b556 6c 65 3d 3d 69 54 61 62 6c 65 20 29 7b 0a 20 20  le==iTable ){.  
2b557 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f    if( pExpr->iCo
2b558 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20  lumn<0 ){.      
2b559 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e  pExpr->op = TK_N
2b55a 55 4c 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ULL;.    }else{.
2b55b 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77        Expr *pNew
2b55c 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2b55d 70 45 4c 69 73 74 21 3d 30 20 26 26 20 70 45 78  pEList!=0 && pEx
2b55e 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 70 45 4c 69  pr->iColumn<pELi
2b55f 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 20  st->nExpr );.   
2b560 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
2b561 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20 70 45  ->pLeft==0 && pE
2b562 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29  xpr->pRight==0 )
2b563 3b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73  ;.      pNew = s
2b564 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
2b565 2c 20 70 45 4c 69 73 74 2d 3e 61 5b 70 45 78 70  , pEList->a[pExp
2b566 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70  r->iColumn].pExp
2b567 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  r, 0);.      if(
2b568 20 70 4e 65 77 20 26 26 20 70 45 78 70 72 2d 3e   pNew && pExpr->
2b569 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20  pColl ){.       
2b56a 20 70 4e 65 77 2d 3e 70 43 6f 6c 6c 20 3d 20 70   pNew->pColl = p
2b56b 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20  Expr->pColl;.   
2b56c 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
2b56d 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
2b56e 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 70   pExpr);.      p
2b56f 45 78 70 72 20 3d 20 70 4e 65 77 3b 0a 20 20 20  Expr = pNew;.   
2b570 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
2b571 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 3d 20 73  pExpr->pLeft = s
2b572 75 62 73 74 45 78 70 72 28 64 62 2c 20 70 45 78  ubstExpr(db, pEx
2b573 70 72 2d 3e 70 4c 65 66 74 2c 20 69 54 61 62 6c  pr->pLeft, iTabl
2b574 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  e, pEList);.    
2b575 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 3d 20  pExpr->pRight = 
2b576 73 75 62 73 74 45 78 70 72 28 64 62 2c 20 70 45  substExpr(db, pE
2b577 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 69 54 61  xpr->pRight, iTa
2b578 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
2b579 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
2b57a 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
2b57b 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  xIsSelect) ){.  
2b57c 20 20 20 20 73 75 62 73 74 53 65 6c 65 63 74 28      substSelect(
2b57d 64 62 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65  db, pExpr->x.pSe
2b57e 6c 65 63 74 2c 20 69 54 61 62 6c 65 2c 20 70 45  lect, iTable, pE
2b57f 4c 69 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  List);.    }else
2b580 7b 0a 20 20 20 20 20 20 73 75 62 73 74 45 78 70  {.      substExp
2b581 72 4c 69 73 74 28 64 62 2c 20 70 45 78 70 72 2d  rList(db, pExpr-
2b582 3e 78 2e 70 4c 69 73 74 2c 20 69 54 61 62 6c 65  >x.pList, iTable
2b583 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 7d  , pEList);.    }
2b584 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 45  .  }.  return pE
2b585 78 70 72 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  xpr;.}.static vo
2b586 69 64 20 73 75 62 73 74 45 78 70 72 4c 69 73 74  id substExprList
2b587 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
2b588 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 70 6f           /* Repo
2b589 72 74 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73  rt malloc errors
2b58a 20 68 65 72 65 20 2a 2f 0a 20 20 45 78 70 72 4c   here */.  ExprL
2b58b 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20  ist *pList,     
2b58c 2f 2a 20 4c 69 73 74 20 74 6f 20 73 63 61 6e 20  /* List to scan 
2b58d 61 6e 64 20 69 6e 20 77 68 69 63 68 20 74 6f 20  and in which to 
2b58e 6d 61 6b 65 20 73 75 62 73 74 69 74 75 74 65 73  make substitutes
2b58f 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65   */.  int iTable
2b590 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61  ,          /* Ta
2b591 62 6c 65 20 74 6f 20 62 65 20 73 75 62 73 74 69  ble to be substi
2b592 74 75 74 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c  tuted */.  ExprL
2b593 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20 20 20  ist *pEList     
2b594 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 76 61  /* Substitute va
2b595 6c 75 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  lues */.){.  int
2b596 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d   i;.  if( pList=
2b597 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66  =0 ) return;.  f
2b598 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d  or(i=0; i<pList-
2b599 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  >nExpr; i++){.  
2b59a 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45    pList->a[i].pE
2b59b 78 70 72 20 3d 20 73 75 62 73 74 45 78 70 72 28  xpr = substExpr(
2b59c 64 62 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  db, pList->a[i].
2b59d 70 45 78 70 72 2c 20 69 54 61 62 6c 65 2c 20 70  pExpr, iTable, p
2b59e 45 4c 69 73 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74  EList);.  }.}.st
2b59f 61 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 53  atic void substS
2b5a0 65 6c 65 63 74 28 0a 20 20 73 71 6c 69 74 65 33  elect(.  sqlite3
2b5a1 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a   *db,         /*
2b5a2 20 52 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65   Report malloc e
2b5a3 72 72 6f 72 73 20 68 65 72 65 20 2a 2f 0a 20 20  rrors here */.  
2b5a4 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20  Select *p,      
2b5a5 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 73       /* SELECT s
2b5a6 74 61 74 65 6d 65 6e 74 20 69 6e 20 77 68 69 63  tatement in whic
2b5a7 68 20 74 6f 20 6d 61 6b 65 20 73 75 62 73 74 69  h to make substi
2b5a8 74 75 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74  tutions */.  int
2b5a9 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20   iTable,        
2b5aa 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62 65    /* Table to be
2b5ab 20 72 65 70 6c 61 63 65 64 20 2a 2f 0a 20 20 45   replaced */.  E
2b5ac 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20  xprList *pEList 
2b5ad 20 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74      /* Substitut
2b5ae 65 20 76 61 6c 75 65 73 20 2a 2f 0a 29 7b 0a 20  e values */.){. 
2b5af 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a   SrcList *pSrc;.
2b5b0 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
2b5b1 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
2b5b2 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 70 20  int i;.  if( !p 
2b5b3 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 75 62 73  ) return;.  subs
2b5b4 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d  tExprList(db, p-
2b5b5 3e 70 45 4c 69 73 74 2c 20 69 54 61 62 6c 65 2c  >pEList, iTable,
2b5b6 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75 62 73   pEList);.  subs
2b5b7 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d  tExprList(db, p-
2b5b8 3e 70 47 72 6f 75 70 42 79 2c 20 69 54 61 62 6c  >pGroupBy, iTabl
2b5b9 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75  e, pEList);.  su
2b5ba 62 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20  bstExprList(db, 
2b5bb 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 69 54 61  p->pOrderBy, iTa
2b5bc 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
2b5bd 70 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 75 62  p->pHaving = sub
2b5be 73 74 45 78 70 72 28 64 62 2c 20 70 2d 3e 70 48  stExpr(db, p->pH
2b5bf 61 76 69 6e 67 2c 20 69 54 61 62 6c 65 2c 20 70  aving, iTable, p
2b5c0 45 4c 69 73 74 29 3b 0a 20 20 70 2d 3e 70 57 68  EList);.  p->pWh
2b5c1 65 72 65 20 3d 20 73 75 62 73 74 45 78 70 72 28  ere = substExpr(
2b5c2 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 2c 20 69  db, p->pWhere, i
2b5c3 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a  Table, pEList);.
2b5c4 20 20 73 75 62 73 74 53 65 6c 65 63 74 28 64 62    substSelect(db
2b5c5 2c 20 70 2d 3e 70 50 72 69 6f 72 2c 20 69 54 61  , p->pPrior, iTa
2b5c6 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
2b5c7 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a  pSrc = p->pSrc;.
2b5c8 20 20 61 73 73 65 72 74 28 20 70 53 72 63 20 29    assert( pSrc )
2b5c9 3b 20 20 2f 2a 20 45 76 65 6e 20 66 6f 72 20 28  ;  /* Even for (
2b5ca 53 45 4c 45 43 54 20 31 29 20 77 65 20 68 61 76  SELECT 1) we hav
2b5cb 65 3a 20 70 53 72 63 21 3d 30 20 62 75 74 20 70  e: pSrc!=0 but p
2b5cc 53 72 63 2d 3e 6e 53 72 63 3d 3d 30 20 2a 2f 0a  Src->nSrc==0 */.
2b5cd 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 53 72    if( ALWAYS(pSr
2b5ce 63 29 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  c) ){.    for(i=
2b5cf 70 53 72 63 2d 3e 6e 53 72 63 2c 20 70 49 74 65  pSrc->nSrc, pIte
2b5d0 6d 3d 70 53 72 63 2d 3e 61 3b 20 69 3e 30 3b 20  m=pSrc->a; i>0; 
2b5d1 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i--, pItem++){. 
2b5d2 20 20 20 20 20 73 75 62 73 74 53 65 6c 65 63 74       substSelect
2b5d3 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c  (db, pItem->pSel
2b5d4 65 63 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c  ect, iTable, pEL
2b5d5 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ist);.    }.  }.
2b5d6 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  }.#endif /* !def
2b5d7 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
2b5d8 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21 64  _SUBQUERY) || !d
2b5d9 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
2b5da 49 54 5f 56 49 45 57 29 20 2a 2f 0a 0a 23 69 66  IT_VIEW) */..#if
2b5db 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2b5dc 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20  _OMIT_SUBQUERY) 
2b5dd 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
2b5de 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 0a 2f 2a  TE_OMIT_VIEW)./*
2b5df 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2b5e0 20 61 74 74 65 6d 70 74 73 20 74 6f 20 66 6c 61   attempts to fla
2b5e1 74 74 65 6e 20 73 75 62 71 75 65 72 69 65 73 20  tten subqueries 
2b5e2 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 70 65 65  in order to spee
2b5e3 64 0a 2a 2a 20 65 78 65 63 75 74 69 6f 6e 2e 20  d.** execution. 
2b5e4 20 49 74 20 72 65 74 75 72 6e 73 20 31 20 69 66   It returns 1 if
2b5e5 20 69 74 20 6d 61 6b 65 73 20 63 68 61 6e 67 65   it makes change
2b5e6 73 20 61 6e 64 20 30 20 69 66 20 6e 6f 20 66 6c  s and 0 if no fl
2b5e7 61 74 74 65 6e 69 6e 67 0a 2a 2a 20 6f 63 63 75  attening.** occu
2b5e8 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 75 6e 64  rs..**.** To und
2b5e9 65 72 73 74 61 6e 64 20 74 68 65 20 63 6f 6e 63  erstand the conc
2b5ea 65 70 74 20 6f 66 20 66 6c 61 74 74 65 6e 69 6e  ept of flattenin
2b5eb 67 2c 20 63 6f 6e 73 69 64 65 72 20 74 68 65 20  g, consider the 
2b5ec 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 71 75 65  following.** que
2b5ed 72 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45  ry:.**.**     SE
2b5ee 4c 45 43 54 20 61 20 46 52 4f 4d 20 28 53 45 4c  LECT a FROM (SEL
2b5ef 45 43 54 20 78 2b 79 20 41 53 20 61 20 46 52 4f  ECT x+y AS a FRO
2b5f0 4d 20 74 31 20 57 48 45 52 45 20 7a 3c 31 30 30  M t1 WHERE z<100
2b5f1 29 20 57 48 45 52 45 20 61 3e 35 0a 2a 2a 0a 2a  ) WHERE a>5.**.*
2b5f2 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 77 61  * The default wa
2b5f3 79 20 6f 66 20 69 6d 70 6c 65 6d 65 6e 74 69 6e  y of implementin
2b5f4 67 20 74 68 69 73 20 71 75 65 72 79 20 69 73 20  g this query is 
2b5f5 74 6f 20 65 78 65 63 75 74 65 20 74 68 65 0a 2a  to execute the.*
2b5f6 2a 20 73 75 62 71 75 65 72 79 20 66 69 72 73 74  * subquery first
2b5f7 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72   and store the r
2b5f8 65 73 75 6c 74 73 20 69 6e 20 61 20 74 65 6d 70  esults in a temp
2b5f9 6f 72 61 72 79 20 74 61 62 6c 65 2c 20 74 68 65  orary table, the
2b5fa 6e 0a 2a 2a 20 72 75 6e 20 74 68 65 20 6f 75 74  n.** run the out
2b5fb 65 72 20 71 75 65 72 79 20 6f 6e 20 74 68 61 74  er query on that
2b5fc 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
2b5fd 2e 20 20 54 68 69 73 20 72 65 71 75 69 72 65 73  .  This requires
2b5fe 20 74 77 6f 0a 2a 2a 20 70 61 73 73 65 73 20 6f   two.** passes o
2b5ff 76 65 72 20 74 68 65 20 64 61 74 61 2e 20 20 46  ver the data.  F
2b600 75 72 74 68 65 72 6d 6f 72 65 2c 20 62 65 63 61  urthermore, beca
2b601 75 73 65 20 74 68 65 20 74 65 6d 70 6f 72 61 72  use the temporar
2b602 79 20 74 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e  y table.** has n
2b603 6f 20 69 6e 64 69 63 65 73 2c 20 74 68 65 20 57  o indices, the W
2b604 48 45 52 45 20 63 6c 61 75 73 65 20 6f 6e 20 74  HERE clause on t
2b605 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 63  he outer query c
2b606 61 6e 6e 6f 74 20 62 65 0a 2a 2a 20 6f 70 74 69  annot be.** opti
2b607 6d 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  mized..**.** Thi
2b608 73 20 72 6f 75 74 69 6e 65 20 61 74 74 65 6d 70  s routine attemp
2b609 74 73 20 74 6f 20 72 65 77 72 69 74 65 20 71 75  ts to rewrite qu
2b60a 65 72 69 65 73 20 73 75 63 68 20 61 73 20 74 68  eries such as th
2b60b 65 20 61 62 6f 76 65 20 69 6e 74 6f 0a 2a 2a 20  e above into.** 
2b60c 61 20 73 69 6e 67 6c 65 20 66 6c 61 74 20 73 65  a single flat se
2b60d 6c 65 63 74 2c 20 6c 69 6b 65 20 74 68 69 73 3a  lect, like this:
2b60e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43  .**.**     SELEC
2b60f 54 20 78 2b 79 20 41 53 20 61 20 46 52 4f 4d 20  T x+y AS a FROM 
2b610 74 31 20 57 48 45 52 45 20 7a 3c 31 30 30 20 41  t1 WHERE z<100 A
2b611 4e 44 20 61 3e 35 0a 2a 2a 0a 2a 2a 20 54 68 65  ND a>5.**.** The
2b612 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20   code generated 
2b613 66 6f 72 20 74 68 69 73 20 73 69 6d 70 69 66 69  for this simpifi
2b614 63 61 74 69 6f 6e 20 67 69 76 65 73 20 74 68 65  cation gives the
2b615 20 73 61 6d 65 20 72 65 73 75 6c 74 0a 2a 2a 20   same result.** 
2b616 62 75 74 20 6f 6e 6c 79 20 68 61 73 20 74 6f 20  but only has to 
2b617 73 63 61 6e 20 74 68 65 20 64 61 74 61 20 6f 6e  scan the data on
2b618 63 65 2e 20 20 41 6e 64 20 62 65 63 61 75 73 65  ce.  And because
2b619 20 69 6e 64 69 63 65 73 20 6d 69 67 68 74 20 0a   indices might .
2b61a 2a 2a 20 65 78 69 73 74 20 6f 6e 20 74 68 65 20  ** exist on the 
2b61b 74 61 62 6c 65 20 74 31 2c 20 61 20 63 6f 6d 70  table t1, a comp
2b61c 6c 65 74 65 20 73 63 61 6e 20 6f 66 20 74 68 65  lete scan of the
2b61d 20 64 61 74 61 20 6d 69 67 68 74 20 62 65 0a 2a   data might be.*
2b61e 2a 20 61 76 6f 69 64 65 64 2e 0a 2a 2a 0a 2a 2a  * avoided..**.**
2b61f 20 46 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 6f   Flattening is o
2b620 6e 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69 66  nly attempted if
2b621 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c   all of the foll
2b622 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0a  owing are true:.
2b623 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 20 54 68 65  **.**   (1)  The
2b624 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 74 68   subquery and th
2b625 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f  e outer query do
2b626 20 6e 6f 74 20 62 6f 74 68 20 75 73 65 20 61 67   not both use ag
2b627 67 72 65 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20  gregates..**.** 
2b628 20 20 28 32 29 20 20 54 68 65 20 73 75 62 71 75    (2)  The subqu
2b629 65 72 79 20 69 73 20 6e 6f 74 20 61 6e 20 61 67  ery is not an ag
2b62a 67 72 65 67 61 74 65 20 6f 72 20 74 68 65 20 6f  gregate or the o
2b62b 75 74 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f  uter query is no
2b62c 74 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20  t a join..**.** 
2b62d 20 20 28 33 29 20 20 54 68 65 20 73 75 62 71 75    (3)  The subqu
2b62e 65 72 79 20 69 73 20 6e 6f 74 20 74 68 65 20 72  ery is not the r
2b62f 69 67 68 74 20 6f 70 65 72 61 6e 64 20 6f 66 20  ight operand of 
2b630 61 20 6c 65 66 74 20 6f 75 74 65 72 20 6a 6f 69  a left outer joi
2b631 6e 0a 2a 2a 20 20 20 20 20 20 20 20 28 4f 72 69  n.**        (Ori
2b632 67 69 6e 61 6c 6c 79 20 74 69 63 6b 65 74 20 23  ginally ticket #
2b633 33 30 36 2e 20 20 53 74 72 65 6e 67 68 74 65 6e  306.  Strenghten
2b634 65 64 20 62 79 20 74 69 63 6b 65 74 20 23 33 33  ed by ticket #33
2b635 30 30 29 0a 2a 2a 0a 2a 2a 20 20 20 28 34 29 20  00).**.**   (4) 
2b636 20 54 68 65 20 73 75 62 71 75 65 72 79 20 69 73   The subquery is
2b637 20 6e 6f 74 20 44 49 53 54 49 4e 43 54 20 6f 72   not DISTINCT or
2b638 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
2b639 20 69 73 20 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a   is not a join..
2b63a 2a 2a 0a 2a 2a 20 20 20 28 35 29 20 20 54 68 65  **.**   (5)  The
2b63b 20 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74   subquery is not
2b63c 20 44 49 53 54 49 4e 43 54 20 6f 72 20 74 68 65   DISTINCT or the
2b63d 20 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 65   outer query doe
2b63e 73 20 6e 6f 74 20 75 73 65 0a 2a 2a 20 20 20 20  s not use.**    
2b63f 20 20 20 20 61 67 67 72 65 67 61 74 65 73 2e 0a      aggregates..
2b640 2a 2a 0a 2a 2a 20 20 20 28 36 29 20 20 54 68 65  **.**   (6)  The
2b641 20 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e   subquery does n
2b642 6f 74 20 75 73 65 20 61 67 67 72 65 67 61 74 65  ot use aggregate
2b643 73 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71  s or the outer q
2b644 75 65 72 79 20 69 73 20 6e 6f 74 0a 2a 2a 20 20  uery is not.**  
2b645 20 20 20 20 20 20 44 49 53 54 49 4e 43 54 2e 0a        DISTINCT..
2b646 2a 2a 0a 2a 2a 20 20 20 28 37 29 20 20 54 68 65  **.**   (7)  The
2b647 20 73 75 62 71 75 65 72 79 20 68 61 73 20 61 20   subquery has a 
2b648 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a  FROM clause..**.
2b649 2a 2a 20 20 20 28 38 29 20 20 54 68 65 20 73 75  **   (8)  The su
2b64a 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20  bquery does not 
2b64b 75 73 65 20 4c 49 4d 49 54 20 6f 72 20 74 68 65  use LIMIT or the
2b64c 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20   outer query is 
2b64d 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a  not a join..**.*
2b64e 2a 20 20 20 28 39 29 20 20 54 68 65 20 73 75 62  *   (9)  The sub
2b64f 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75  query does not u
2b650 73 65 20 4c 49 4d 49 54 20 6f 72 20 74 68 65 20  se LIMIT or the 
2b651 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 65 73  outer query does
2b652 20 6e 6f 74 20 75 73 65 0a 2a 2a 20 20 20 20 20   not use.**     
2b653 20 20 20 61 67 67 72 65 67 61 74 65 73 2e 0a 2a     aggregates..*
2b654 2a 0a 2a 2a 20 20 28 31 30 29 20 20 54 68 65 20  *.**  (10)  The 
2b655 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f  subquery does no
2b656 74 20 75 73 65 20 61 67 67 72 65 67 61 74 65 73  t use aggregates
2b657 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75   or the outer qu
2b658 65 72 79 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20  ery does not.** 
2b659 20 20 20 20 20 20 20 75 73 65 20 4c 49 4d 49 54         use LIMIT
2b65a 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 31 29 20 20 54  ..**.**  (11)  T
2b65b 68 65 20 73 75 62 71 75 65 72 79 20 61 6e 64 20  he subquery and 
2b65c 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
2b65d 64 6f 20 6e 6f 74 20 62 6f 74 68 20 68 61 76 65  do not both have
2b65e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2b65f 73 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 32 29 20 20  s..**.**  (12)  
2b660 4e 6f 74 20 69 6d 70 6c 65 6d 65 6e 74 65 64 2e  Not implemented.
2b661 20 20 53 75 62 73 75 6d 65 64 20 69 6e 74 6f 20    Subsumed into 
2b662 72 65 73 74 72 69 63 74 69 6f 6e 20 28 33 29 2e  restriction (3).
2b663 20 20 57 61 73 20 70 72 65 76 69 6f 75 73 6c 79    Was previously
2b664 0a 2a 2a 20 20 20 20 20 20 20 20 61 20 73 65 70  .**        a sep
2b665 61 72 61 74 65 20 72 65 73 74 72 69 63 74 69 6f  arate restrictio
2b666 6e 20 64 65 72 69 76 69 6e 67 20 66 72 6f 6d 20  n deriving from 
2b667 74 69 63 6b 65 74 20 23 33 35 30 2e 0a 2a 2a 0a  ticket #350..**.
2b668 2a 2a 20 20 28 31 33 29 20 20 54 68 65 20 73 75  **  (13)  The su
2b669 62 71 75 65 72 79 20 61 6e 64 20 6f 75 74 65 72  bquery and outer
2b66a 20 71 75 65 72 79 20 64 6f 20 6e 6f 74 20 62 6f   query do not bo
2b66b 74 68 20 75 73 65 20 4c 49 4d 49 54 0a 2a 2a 0a  th use LIMIT.**.
2b66c 2a 2a 20 20 28 31 34 29 20 20 54 68 65 20 73 75  **  (14)  The su
2b66d 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20  bquery does not 
2b66e 75 73 65 20 4f 46 46 53 45 54 0a 2a 2a 0a 2a 2a  use OFFSET.**.**
2b66f 20 20 28 31 35 29 20 20 54 68 65 20 6f 75 74 65    (15)  The oute
2b670 72 20 71 75 65 72 79 20 69 73 20 6e 6f 74 20 70  r query is not p
2b671 61 72 74 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e  art of a compoun
2b672 64 20 73 65 6c 65 63 74 20 6f 72 20 74 68 65 0a  d select or the.
2b673 2a 2a 20 20 20 20 20 20 20 20 73 75 62 71 75 65  **        subque
2b674 72 79 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65  ry does not have
2b675 20 62 6f 74 68 20 61 6e 20 4f 52 44 45 52 20 42   both an ORDER B
2b676 59 20 61 6e 64 20 61 20 4c 49 4d 49 54 20 63 6c  Y and a LIMIT cl
2b677 61 75 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20  ause..**        
2b678 28 53 65 65 20 74 69 63 6b 65 74 20 23 32 33 33  (See ticket #233
2b679 39 29 0a 2a 2a 0a 2a 2a 20 20 28 31 36 29 20 20  9).**.**  (16)  
2b67a 54 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  The outer query 
2b67b 69 73 20 6e 6f 74 20 61 6e 20 61 67 67 72 65 67  is not an aggreg
2b67c 61 74 65 20 6f 72 20 74 68 65 20 73 75 62 71 75  ate or the subqu
2b67d 65 72 79 20 64 6f 65 73 0a 2a 2a 20 20 20 20 20  ery does.**     
2b67e 20 20 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 4f     not contain O
2b67f 52 44 45 52 20 42 59 2e 20 20 28 54 69 63 6b 65  RDER BY.  (Ticke
2b680 74 20 23 32 39 34 32 29 20 20 54 68 69 73 20 75  t #2942)  This u
2b681 73 65 64 20 74 6f 20 6e 6f 74 20 6d 61 74 74 65  sed to not matte
2b682 72 0a 2a 2a 20 20 20 20 20 20 20 20 75 6e 74 69  r.**        unti
2b683 6c 20 77 65 20 69 6e 74 72 6f 64 75 63 65 64 20  l we introduced 
2b684 74 68 65 20 67 72 6f 75 70 5f 63 6f 6e 63 61 74  the group_concat
2b685 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 0a 2a  () function.  .*
2b686 2a 0a 2a 2a 20 20 28 31 37 29 20 20 54 68 65 20  *.**  (17)  The 
2b687 73 75 62 2d 71 75 65 72 79 20 69 73 20 6e 6f 74  sub-query is not
2b688 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65   a compound sele
2b689 63 74 2c 20 6f 72 20 69 74 20 69 73 20 61 20 55  ct, or it is a U
2b68a 4e 49 4f 4e 20 41 4c 4c 20 0a 2a 2a 20 20 20 20  NION ALL .**    
2b68b 20 20 20 20 63 6f 6d 70 6f 75 6e 64 20 63 6c 61      compound cla
2b68c 75 73 65 20 6d 61 64 65 20 75 70 20 65 6e 74 69  use made up enti
2b68d 72 65 6c 79 20 6f 66 20 6e 6f 6e 2d 61 67 67 72  rely of non-aggr
2b68e 65 67 61 74 65 20 71 75 65 72 69 65 73 2c 20 61  egate queries, a
2b68f 6e 64 20 0a 2a 2a 20 20 20 20 20 20 20 20 74 68  nd .**        th
2b690 65 20 70 61 72 65 6e 74 20 71 75 65 72 79 3a 0a  e parent query:.
2b691 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2a  **.**          *
2b692 20 69 73 20 6e 6f 74 20 69 74 73 65 6c 66 20 70   is not itself p
2b693 61 72 74 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e  art of a compoun
2b694 64 20 73 65 6c 65 63 74 2c 0a 2a 2a 20 20 20 20  d select,.**    
2b695 20 20 20 20 20 20 2a 20 69 73 20 6e 6f 74 20 61        * is not a
2b696 6e 20 61 67 67 72 65 67 61 74 65 20 6f 72 20 44  n aggregate or D
2b697 49 53 54 49 4e 43 54 20 71 75 65 72 79 2c 20 61  ISTINCT query, a
2b698 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2a  nd.**          *
2b699 20 68 61 73 20 6e 6f 20 6f 74 68 65 72 20 74 61   has no other ta
2b69a 62 6c 65 73 20 6f 72 20 73 75 62 2d 73 65 6c 65  bles or sub-sele
2b69b 63 74 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  cts in the FROM 
2b69c 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  clause..**.**   
2b69d 20 20 20 20 20 54 68 65 20 70 61 72 65 6e 74 20       The parent 
2b69e 61 6e 64 20 73 75 62 2d 71 75 65 72 79 20 6d 61  and sub-query ma
2b69f 79 20 63 6f 6e 74 61 69 6e 20 57 48 45 52 45 20  y contain WHERE 
2b6a0 63 6c 61 75 73 65 73 2e 20 53 75 62 6a 65 63 74  clauses. Subject
2b6a1 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 72 75   to.**        ru
2b6a2 6c 65 73 20 28 31 31 29 2c 20 28 31 33 29 20 61  les (11), (13) a
2b6a3 6e 64 20 28 31 34 29 2c 20 74 68 65 79 20 6d 61  nd (14), they ma
2b6a4 79 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 20 4f  y also contain O
2b6a5 52 44 45 52 20 42 59 2c 0a 2a 2a 20 20 20 20 20  RDER BY,.**     
2b6a6 20 20 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46     LIMIT and OFF
2b6a7 53 45 54 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a  SET clauses..**.
2b6a8 2a 2a 20 20 28 31 38 29 20 20 49 66 20 74 68 65  **  (18)  If the
2b6a9 20 73 75 62 2d 71 75 65 72 79 20 69 73 20 61 20   sub-query is a 
2b6aa 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2c  compound select,
2b6ab 20 74 68 65 6e 20 61 6c 6c 20 74 65 72 6d 73 20   then all terms 
2b6ac 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  of the.**       
2b6ad 20 4f 52 44 45 52 20 62 79 20 63 6c 61 75 73 65   ORDER by clause
2b6ae 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20 6d   of the parent m
2b6af 75 73 74 20 62 65 20 73 69 6d 70 6c 65 20 72 65  ust be simple re
2b6b0 66 65 72 65 6e 63 65 73 20 74 6f 20 0a 2a 2a 20  ferences to .** 
2b6b1 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 73 20 6f         columns o
2b6b2 66 20 74 68 65 20 73 75 62 2d 71 75 65 72 79 2e  f the sub-query.
2b6b3 0a 2a 2a 0a 2a 2a 20 20 28 31 39 29 20 20 54 68  .**.**  (19)  Th
2b6b4 65 20 73 75 62 71 75 65 72 79 20 64 6f 65 73 20  e subquery does 
2b6b5 6e 6f 74 20 75 73 65 20 4c 49 4d 49 54 20 6f 72  not use LIMIT or
2b6b6 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
2b6b7 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 20 20 20   does not.**    
2b6b8 20 20 20 20 68 61 76 65 20 61 20 57 48 45 52 45      have a WHERE
2b6b9 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20   clause..**.**  
2b6ba 28 32 30 29 20 20 49 66 20 74 68 65 20 73 75 62  (20)  If the sub
2b6bb 2d 71 75 65 72 79 20 69 73 20 61 20 63 6f 6d 70  -query is a comp
2b6bc 6f 75 6e 64 20 73 65 6c 65 63 74 2c 20 74 68 65  ound select, the
2b6bd 6e 20 69 74 20 6d 75 73 74 20 6e 6f 74 20 75 73  n it must not us
2b6be 65 0a 2a 2a 20 20 20 20 20 20 20 20 61 6e 20 4f  e.**        an O
2b6bf 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e 20  RDER BY clause. 
2b6c0 20 54 69 63 6b 65 74 20 23 33 37 37 33 2e 20 20   Ticket #3773.  
2b6c1 57 65 20 63 6f 75 6c 64 20 72 65 6c 61 78 20 74  We could relax t
2b6c2 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a  his constraint.*
2b6c3 2a 20 20 20 20 20 20 20 20 73 6f 6d 65 77 68 61  *        somewha
2b6c4 74 20 62 79 20 73 61 79 69 6e 67 20 74 68 61 74  t by saying that
2b6c5 20 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68   the terms of th
2b6c6 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
2b6c7 65 20 6d 75 73 74 0a 2a 2a 20 20 20 20 20 20 20  e must.**       
2b6c8 20 61 70 70 65 61 72 20 61 73 20 75 6e 6d 6f 64   appear as unmod
2b6c9 69 66 69 65 64 20 72 65 73 75 6c 74 20 63 6f 6c  ified result col
2b6ca 75 6d 6e 73 20 69 6e 20 74 68 65 20 6f 75 74 65  umns in the oute
2b6cb 72 20 71 75 65 72 79 2e 20 20 42 75 74 0a 2a 2a  r query.  But.**
2b6cc 20 20 20 20 20 20 20 20 68 61 76 65 20 6f 74 68          have oth
2b6cd 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73  er optimizations
2b6ce 20 69 6e 20 6d 69 6e 64 20 74 6f 20 64 65 61 6c   in mind to deal
2b6cf 20 77 69 74 68 20 74 68 61 74 20 63 61 73 65 2e   with that case.
2b6d0 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 72  .**.** In this r
2b6d1 6f 75 74 69 6e 65 2c 20 74 68 65 20 22 70 22 20  outine, the "p" 
2b6d2 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 70  parameter is a p
2b6d3 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 75  ointer to the ou
2b6d4 74 65 72 20 71 75 65 72 79 2e 0a 2a 2a 20 54 68  ter query..** Th
2b6d5 65 20 73 75 62 71 75 65 72 79 20 69 73 20 70 2d  e subquery is p-
2b6d6 3e 70 53 72 63 2d 3e 61 5b 69 46 72 6f 6d 5d 2e  >pSrc->a[iFrom].
2b6d7 20 20 69 73 41 67 67 20 69 73 20 74 72 75 65 20    isAgg is true 
2b6d8 69 66 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  if the outer que
2b6d9 72 79 0a 2a 2a 20 75 73 65 73 20 61 67 67 72 65  ry.** uses aggre
2b6da 67 61 74 65 73 20 61 6e 64 20 73 75 62 71 75 65  gates and subque
2b6db 72 79 49 73 41 67 67 20 69 73 20 74 72 75 65 20  ryIsAgg is true 
2b6dc 69 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20  if the subquery 
2b6dd 75 73 65 73 20 61 67 67 72 65 67 61 74 65 73 2e  uses aggregates.
2b6de 0a 2a 2a 0a 2a 2a 20 49 66 20 66 6c 61 74 74 65  .**.** If flatte
2b6df 6e 69 6e 67 20 69 73 20 6e 6f 74 20 61 74 74 65  ning is not atte
2b6e0 6d 70 74 65 64 2c 20 74 68 69 73 20 72 6f 75 74  mpted, this rout
2b6e1 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 61  ine is a no-op a
2b6e2 6e 64 20 72 65 74 75 72 6e 73 20 30 2e 0a 2a 2a  nd returns 0..**
2b6e3 20 49 66 20 66 6c 61 74 74 65 6e 69 6e 67 20 69   If flattening i
2b6e4 73 20 61 74 74 65 6d 70 74 65 64 20 74 68 69 73  s attempted this
2b6e5 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
2b6e6 20 31 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66   1..**.** All of
2b6e7 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
2b6e8 61 6e 61 6c 79 73 69 73 20 6d 75 73 74 20 6f 63  analysis must oc
2b6e9 63 75 72 20 6f 6e 20 62 6f 74 68 20 74 68 65 20  cur on both the 
2b6ea 6f 75 74 65 72 20 71 75 65 72 79 20 61 6e 64 0a  outer query and.
2b6eb 2a 2a 20 74 68 65 20 73 75 62 71 75 65 72 79 20  ** the subquery 
2b6ec 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74  before this rout
2b6ed 69 6e 65 20 72 75 6e 73 2e 0a 2a 2f 0a 73 74 61  ine runs..*/.sta
2b6ee 74 69 63 20 69 6e 74 20 66 6c 61 74 74 65 6e 53  tic int flattenS
2b6ef 75 62 71 75 65 72 79 28 0a 20 20 50 61 72 73 65  ubquery(.  Parse
2b6f0 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2b6f1 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
2b6f2 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  xt */.  Select *
2b6f3 70 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p,           /* 
2b6f4 54 68 65 20 70 61 72 65 6e 74 20 6f 72 20 6f 75  The parent or ou
2b6f5 74 65 72 20 53 45 4c 45 43 54 20 73 74 61 74 65  ter SELECT state
2b6f6 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 69 46  ment */.  int iF
2b6f7 72 6f 6d 2c 20 20 20 20 20 20 20 20 20 20 20 2f  rom,           /
2b6f8 2a 20 49 6e 64 65 78 20 69 6e 20 70 2d 3e 70 53  * Index in p->pS
2b6f9 72 63 2d 3e 61 5b 5d 20 6f 66 20 74 68 65 20 69  rc->a[] of the i
2b6fa 6e 6e 65 72 20 73 75 62 71 75 65 72 79 20 2a 2f  nner subquery */
2b6fb 0a 20 20 69 6e 74 20 69 73 41 67 67 2c 20 20 20  .  int isAgg,   
2b6fc 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
2b6fd 69 66 20 6f 75 74 65 72 20 53 45 4c 45 43 54 20  if outer SELECT 
2b6fe 75 73 65 73 20 61 67 67 72 65 67 61 74 65 20 66  uses aggregate f
2b6ff 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  unctions */.  in
2b700 74 20 73 75 62 71 75 65 72 79 49 73 41 67 67 20  t subqueryIsAgg 
2b701 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
2b702 65 20 73 75 62 71 75 65 72 79 20 75 73 65 73 20  e subquery uses 
2b703 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
2b704 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73  ons */.){.  cons
2b705 74 20 63 68 61 72 20 2a 7a 53 61 76 65 64 41 75  t char *zSavedAu
2b706 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 72  thContext = pPar
2b707 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74  se->zAuthContext
2b708 3b 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 61 72  ;.  Select *pPar
2b709 65 6e 74 3b 0a 20 20 53 65 6c 65 63 74 20 2a 70  ent;.  Select *p
2b70a 53 75 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  Sub;       /* Th
2b70b 65 20 69 6e 6e 65 72 20 71 75 65 72 79 20 6f 72  e inner query or
2b70c 20 22 73 75 62 71 75 65 72 79 22 20 2a 2f 0a 20   "subquery" */. 
2b70d 20 53 65 6c 65 63 74 20 2a 70 53 75 62 31 3b 20   Select *pSub1; 
2b70e 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
2b70f 74 6f 20 74 68 65 20 72 69 67 68 74 6d 6f 73 74  to the rightmost
2b710 20 73 65 6c 65 63 74 20 69 6e 20 73 75 62 2d 71   select in sub-q
2b711 75 65 72 79 20 2a 2f 0a 20 20 53 72 63 4c 69 73  uery */.  SrcLis
2b712 74 20 2a 70 53 72 63 3b 20 20 20 20 20 20 2f 2a  t *pSrc;      /*
2b713 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   The FROM clause
2b714 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20 71 75   of the outer qu
2b715 65 72 79 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ery */.  SrcList
2b716 20 2a 70 53 75 62 53 72 63 3b 20 20 20 2f 2a 20   *pSubSrc;   /* 
2b717 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  The FROM clause 
2b718 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20  of the subquery 
2b719 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2b71a 4c 69 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 20  List;    /* The 
2b71b 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68  result set of th
2b71c 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 2a 2f  e outer query */
2b71d 0a 20 20 69 6e 74 20 69 50 61 72 65 6e 74 3b 20  .  int iParent; 
2b71e 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 63         /* VDBE c
2b71f 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20  ursor number of 
2b720 74 68 65 20 70 53 75 62 20 72 65 73 75 6c 74 20  the pSub result 
2b721 73 65 74 20 74 65 6d 70 20 74 61 62 6c 65 20 2a  set temp table *
2b722 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
2b723 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
2b724 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 70  counter */.  Exp
2b725 72 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20  r *pWhere;      
2b726 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b727 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
2b728 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  e */.  struct Sr
2b729 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 75 62  cList_item *pSub
2b72a 69 74 65 6d 3b 20 20 20 2f 2a 20 54 68 65 20 73  item;   /* The s
2b72b 75 62 71 75 65 72 79 20 2a 2f 0a 20 20 73 71 6c  ubquery */.  sql
2b72c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2b72d 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 43 68 65  e->db;..  /* Che
2b72e 63 6b 20 74 6f 20 73 65 65 20 69 66 20 66 6c 61  ck to see if fla
2b72f 74 74 65 6e 69 6e 67 20 69 73 20 70 65 72 6d 69  ttening is permi
2b730 74 74 65 64 2e 20 20 52 65 74 75 72 6e 20 30 20  tted.  Return 0 
2b731 69 66 20 6e 6f 74 2e 0a 20 20 2a 2f 0a 20 20 61  if not..  */.  a
2b732 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
2b733 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 69   assert( p->pPri
2b734 6f 72 3d 3d 30 20 29 3b 20 20 2f 2a 20 55 6e 61  or==0 );  /* Una
2b735 62 6c 65 20 74 6f 20 66 6c 61 74 74 65 6e 20 63  ble to flatten c
2b736 6f 6d 70 6f 75 6e 64 20 71 75 65 72 69 65 73 20  ompound queries 
2b737 2a 2f 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70  */.  pSrc = p->p
2b738 53 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70  Src;.  assert( p
2b739 53 72 63 20 26 26 20 69 46 72 6f 6d 3e 3d 30 20  Src && iFrom>=0 
2b73a 26 26 20 69 46 72 6f 6d 3c 70 53 72 63 2d 3e 6e  && iFrom<pSrc->n
2b73b 53 72 63 20 29 3b 0a 20 20 70 53 75 62 69 74 65  Src );.  pSubite
2b73c 6d 20 3d 20 26 70 53 72 63 2d 3e 61 5b 69 46 72  m = &pSrc->a[iFr
2b73d 6f 6d 5d 3b 0a 20 20 69 50 61 72 65 6e 74 20 3d  om];.  iParent =
2b73e 20 70 53 75 62 69 74 65 6d 2d 3e 69 43 75 72 73   pSubitem->iCurs
2b73f 6f 72 3b 0a 20 20 70 53 75 62 20 3d 20 70 53 75  or;.  pSub = pSu
2b740 62 69 74 65 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a  bitem->pSelect;.
2b741 20 20 61 73 73 65 72 74 28 20 70 53 75 62 21 3d    assert( pSub!=
2b742 30 20 29 3b 0a 20 20 69 66 28 20 69 73 41 67 67  0 );.  if( isAgg
2b743 20 26 26 20 73 75 62 71 75 65 72 79 49 73 41 67   && subqueryIsAg
2b744 67 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20  g ) return 0;   
2b745 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b746 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 29   Restriction (1)
2b747 20 20 2a 2f 0a 20 20 69 66 28 20 73 75 62 71 75    */.  if( subqu
2b748 65 72 79 49 73 41 67 67 20 26 26 20 70 53 72 63  eryIsAgg && pSrc
2b749 2d 3e 6e 53 72 63 3e 31 20 29 20 72 65 74 75 72  ->nSrc>1 ) retur
2b74a 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  n 0;          /*
2b74b 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 32 29   Restriction (2)
2b74c 20 20 2a 2f 0a 20 20 70 53 75 62 53 72 63 20 3d    */.  pSubSrc =
2b74d 20 70 53 75 62 2d 3e 70 53 72 63 3b 0a 20 20 61   pSub->pSrc;.  a
2b74e 73 73 65 72 74 28 20 70 53 75 62 53 72 63 20 29  ssert( pSubSrc )
2b74f 3b 0a 20 20 2f 2a 20 50 72 69 6f 72 20 74 6f 20  ;.  /* Prior to 
2b750 76 65 72 73 69 6f 6e 20 33 2e 31 2e 32 2c 20 77  version 3.1.2, w
2b751 68 65 6e 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46  hen LIMIT and OF
2b752 46 53 45 54 20 68 61 64 20 74 6f 20 62 65 20 73  FSET had to be s
2b753 69 6d 70 6c 65 20 63 6f 6e 73 74 61 6e 74 73 2c  imple constants,
2b754 0a 20 20 2a 2a 20 6e 6f 74 20 61 72 62 69 74 72  .  ** not arbitr
2b755 61 72 79 20 65 78 70 72 65 73 73 73 69 6f 6e 73  ary expresssions
2b756 2c 20 77 65 20 61 6c 6c 6f 77 65 64 20 73 6f 6d  , we allowed som
2b757 65 20 63 6f 6d 62 69 6e 69 6e 67 20 6f 66 20 4c  e combining of L
2b758 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 54 0a  IMIT and OFFSET.
2b759 20 20 2a 2a 20 62 65 63 61 75 73 65 20 74 68 65    ** because the
2b75a 79 20 63 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75  y could be compu
2b75b 74 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  ted at compile-t
2b75c 69 6d 65 2e 20 20 42 75 74 20 77 68 65 6e 20 4c  ime.  But when L
2b75d 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 54 0a  IMIT and OFFSET.
2b75e 20 20 2a 2a 20 62 65 63 61 6d 65 20 61 72 62 69    ** became arbi
2b75f 74 72 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e  trary expression
2b760 73 2c 20 77 65 20 77 65 72 65 20 66 6f 72 63 65  s, we were force
2b761 64 20 74 6f 20 61 64 64 20 72 65 73 74 72 69 63  d to add restric
2b762 74 69 6f 6e 73 20 28 31 33 29 0a 20 20 2a 2a 20  tions (13).  ** 
2b763 61 6e 64 20 28 31 34 29 2e 20 2a 2f 0a 20 20 69  and (14). */.  i
2b764 66 28 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20  f( pSub->pLimit 
2b765 26 26 20 70 2d 3e 70 4c 69 6d 69 74 20 29 20 72  && p->pLimit ) r
2b766 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
2b767 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63        /* Restric
2b768 74 69 6f 6e 20 28 31 33 29 20 2a 2f 0a 20 20 69  tion (13) */.  i
2b769 66 28 20 70 53 75 62 2d 3e 70 4f 66 66 73 65 74  f( pSub->pOffset
2b76a 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
2b76b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b76c 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63        /* Restric
2b76d 74 69 6f 6e 20 28 31 34 29 20 2a 2f 0a 20 20 69  tion (14) */.  i
2b76e 66 28 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74  f( p->pRightmost
2b76f 20 26 26 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74   && pSub->pLimit
2b770 20 26 26 20 70 53 75 62 2d 3e 70 4f 72 64 65 72   && pSub->pOrder
2b771 42 79 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  By ){.    return
2b772 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
2b773 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b774 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2b775 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31  * Restriction (1
2b776 35 29 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  5) */.  }.  if( 
2b777 70 53 75 62 53 72 63 2d 3e 6e 53 72 63 3d 3d 30  pSubSrc->nSrc==0
2b778 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
2b779 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b77a 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f     /* Restrictio
2b77b 6e 20 28 37 29 20 20 2a 2f 0a 20 20 69 66 28 20  n (7)  */.  if( 
2b77c 28 28 70 53 75 62 2d 3e 73 65 6c 46 6c 61 67 73  ((pSub->selFlags
2b77d 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 29 21   & SF_Distinct)!
2b77e 3d 30 20 7c 7c 20 70 53 75 62 2d 3e 70 4c 69 6d  =0 || pSub->pLim
2b77f 69 74 29 20 0a 20 20 20 20 20 20 20 20 20 26 26  it) .         &&
2b780 20 28 70 53 72 63 2d 3e 6e 53 72 63 3e 31 20 7c   (pSrc->nSrc>1 |
2b781 7c 20 69 73 41 67 67 29 20 29 7b 20 20 20 20 20  | isAgg) ){     
2b782 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74       /* Restrict
2b783 69 6f 6e 73 20 28 34 29 28 35 29 28 38 29 28 39  ions (4)(5)(8)(9
2b784 29 20 2a 2f 0a 20 20 20 20 20 72 65 74 75 72 6e  ) */.     return
2b785 20 30 3b 20 20 20 20 20 20 20 0a 20 20 7d 0a 20   0;       .  }. 
2b786 20 69 66 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67   if( (p->selFlag
2b787 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 29  s & SF_Distinct)
2b788 21 3d 30 20 26 26 20 73 75 62 71 75 65 72 79 49  !=0 && subqueryI
2b789 73 41 67 67 20 29 7b 0a 20 20 20 20 20 72 65 74  sAgg ){.     ret
2b78a 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 2f  urn 0;         /
2b78b 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 36  * Restriction (6
2b78c 29 20 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  )  */.  }.  if( 
2b78d 70 2d 3e 70 4f 72 64 65 72 42 79 20 26 26 20 70  p->pOrderBy && p
2b78e 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b  Sub->pOrderBy ){
2b78f 0a 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  .     return 0; 
2b790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b791 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b792 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
2b793 74 72 69 63 74 69 6f 6e 20 28 31 31 29 20 2a 2f  triction (11) */
2b794 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 41 67 67  .  }.  if( isAgg
2b795 20 26 26 20 70 53 75 62 2d 3e 70 4f 72 64 65 72   && pSub->pOrder
2b796 42 79 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20  By ) return 0;  
2b797 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b798 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 36   Restriction (16
2b799 29 20 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d  ) */.  if( pSub-
2b79a 3e 70 4c 69 6d 69 74 20 26 26 20 70 2d 3e 70 57  >pLimit && p->pW
2b79b 68 65 72 65 20 29 20 72 65 74 75 72 6e 20 30 3b  here ) return 0;
2b79c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b79d 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 39   Restriction (19
2b79e 29 20 2a 2f 0a 0a 20 20 2f 2a 20 4f 42 53 4f 4c  ) */..  /* OBSOL
2b79f 45 54 45 20 43 4f 4d 4d 45 4e 54 20 31 3a 0a 20  ETE COMMENT 1:. 
2b7a0 20 2a 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   ** Restriction 
2b7a1 33 3a 20 20 49 66 20 74 68 65 20 73 75 62 71 75  3:  If the subqu
2b7a2 65 72 79 20 69 73 20 61 20 6a 6f 69 6e 2c 20 6d  ery is a join, m
2b7a3 61 6b 65 20 73 75 72 65 20 74 68 65 20 73 75 62  ake sure the sub
2b7a4 71 75 65 72 79 20 69 73 20 0a 20 20 2a 2a 20 6e  query is .  ** n
2b7a5 6f 74 20 75 73 65 64 20 61 73 20 74 68 65 20 72  ot used as the r
2b7a6 69 67 68 74 20 6f 70 65 72 61 6e 64 20 6f 66 20  ight operand of 
2b7a7 61 6e 20 6f 75 74 65 72 20 6a 6f 69 6e 2e 20 20  an outer join.  
2b7a8 45 78 61 6d 70 6c 65 73 20 6f 66 20 77 68 79 20  Examples of why 
2b7a9 74 68 69 73 0a 20 20 2a 2a 20 69 73 20 6e 6f 74  this.  ** is not
2b7aa 20 61 6c 6c 6f 77 65 64 3a 0a 20 20 2a 2a 0a 20   allowed:.  **. 
2b7ab 20 2a 2a 20 20 20 20 20 20 20 20 20 74 31 20 4c   **         t1 L
2b7ac 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 28  EFT OUTER JOIN (
2b7ad 74 32 20 4a 4f 49 4e 20 74 33 29 0a 20 20 2a 2a  t2 JOIN t3).  **
2b7ae 0a 20 20 2a 2a 20 49 66 20 77 65 20 66 6c 61 74  .  ** If we flat
2b7af 74 65 6e 20 74 68 65 20 61 62 6f 76 65 2c 20 77  ten the above, w
2b7b0 65 20 77 6f 75 6c 64 20 67 65 74 0a 20 20 2a 2a  e would get.  **
2b7b1 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 28 74  .  **         (t
2b7b2 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49  1 LEFT OUTER JOI
2b7b3 4e 20 74 32 29 20 4a 4f 49 4e 20 74 33 0a 20 20  N t2) JOIN t3.  
2b7b4 2a 2a 0a 20 20 2a 2a 20 77 68 69 63 68 20 69 73  **.  ** which is
2b7b5 20 6e 6f 74 20 61 74 20 61 6c 6c 20 74 68 65 20   not at all the 
2b7b6 73 61 6d 65 20 74 68 69 6e 67 2e 0a 20 20 2a 2a  same thing..  **
2b7b7 0a 20 20 2a 2a 20 4f 42 53 4f 4c 45 54 45 20 43  .  ** OBSOLETE C
2b7b8 4f 4d 4d 45 4e 54 20 32 3a 0a 20 20 2a 2a 20 52  OMMENT 2:.  ** R
2b7b9 65 73 74 72 69 63 74 69 6f 6e 20 31 32 3a 20 20  estriction 12:  
2b7ba 49 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20  If the subquery 
2b7bb 69 73 20 74 68 65 20 72 69 67 68 74 20 6f 70 65  is the right ope
2b7bc 72 61 6e 64 20 6f 66 20 61 20 6c 65 66 74 20 6f  rand of a left o
2b7bd 75 74 65 72 0a 20 20 2a 2a 20 6a 6f 69 6e 2c 20  uter.  ** join, 
2b7be 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 73 75  make sure the su
2b7bf 62 71 75 65 72 79 20 68 61 73 20 6e 6f 20 57 48  bquery has no WH
2b7c0 45 52 45 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a  ERE clause..  **
2b7c1 20 41 6e 20 65 78 61 6d 70 6c 65 73 20 6f 66 20   An examples of 
2b7c2 77 68 79 20 74 68 69 73 20 69 73 20 6e 6f 74 20  why this is not 
2b7c3 61 6c 6c 6f 77 65 64 3a 0a 20 20 2a 2a 0a 20 20  allowed:.  **.  
2b7c4 2a 2a 20 20 20 20 20 20 20 20 20 74 31 20 4c 45  **         t1 LE
2b7c5 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 28 53  FT OUTER JOIN (S
2b7c6 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 32 20  ELECT * FROM t2 
2b7c7 57 48 45 52 45 20 74 32 2e 78 3e 30 29 0a 20 20  WHERE t2.x>0).  
2b7c8 2a 2a 0a 20 20 2a 2a 20 49 66 20 77 65 20 66 6c  **.  ** If we fl
2b7c9 61 74 74 65 6e 20 74 68 65 20 61 62 6f 76 65 2c  atten the above,
2b7ca 20 77 65 20 77 6f 75 6c 64 20 67 65 74 0a 20 20   we would get.  
2b7cb 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20  **.  **         
2b7cc 28 74 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a  (t1 LEFT OUTER J
2b7cd 4f 49 4e 20 74 32 29 20 57 48 45 52 45 20 74 32  OIN t2) WHERE t2
2b7ce 2e 78 3e 30 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42  .x>0.  **.  ** B
2b7cf 75 74 20 74 68 65 20 74 32 2e 78 3e 30 20 74 65  ut the t2.x>0 te
2b7d0 73 74 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 66  st will always f
2b7d1 61 69 6c 20 6f 6e 20 61 20 4e 55 4c 4c 20 72 6f  ail on a NULL ro
2b7d2 77 20 6f 66 20 74 32 2c 20 77 68 69 63 68 0a 20  w of t2, which. 
2b7d3 20 2a 2a 20 65 66 66 65 63 74 69 76 65 6c 79 20   ** effectively 
2b7d4 63 6f 6e 76 65 72 74 73 20 74 68 65 20 4f 55 54  converts the OUT
2b7d5 45 52 20 4a 4f 49 4e 20 69 6e 74 6f 20 61 6e 20  ER JOIN into an 
2b7d6 49 4e 4e 45 52 20 4a 4f 49 4e 2e 0a 20 20 2a 2a  INNER JOIN..  **
2b7d7 0a 20 20 2a 2a 20 54 48 49 53 20 4f 56 45 52 52  .  ** THIS OVERR
2b7d8 49 44 45 53 20 4f 42 53 4f 4c 45 54 45 20 43 4f  IDES OBSOLETE CO
2b7d9 4d 4d 45 4e 54 53 20 31 20 41 4e 44 20 32 20 41  MMENTS 1 AND 2 A
2b7da 42 4f 56 45 3a 0a 20 20 2a 2a 20 54 69 63 6b 65  BOVE:.  ** Ticke
2b7db 74 20 23 33 33 30 30 20 73 68 6f 77 73 20 74 68  t #3300 shows th
2b7dc 61 74 20 66 6c 61 74 74 65 6e 69 6e 67 20 74 68  at flattening th
2b7dd 65 20 72 69 67 68 74 20 74 65 72 6d 20 6f 66 20  e right term of 
2b7de 61 20 4c 45 46 54 20 4a 4f 49 4e 0a 20 20 2a 2a  a LEFT JOIN.  **
2b7df 20 69 73 20 66 72 61 75 67 68 74 20 77 69 74 68   is fraught with
2b7e0 20 64 61 6e 67 65 72 2e 20 20 42 65 73 74 20 74   danger.  Best t
2b7e1 6f 20 61 76 6f 69 64 20 74 68 65 20 77 68 6f 6c  o avoid the whol
2b7e2 65 20 74 68 69 6e 67 2e 20 20 49 66 20 74 68 65  e thing.  If the
2b7e3 0a 20 20 2a 2a 20 73 75 62 71 75 65 72 79 20 69  .  ** subquery i
2b7e4 73 20 74 68 65 20 72 69 67 68 74 20 74 65 72 6d  s the right term
2b7e5 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 2c   of a LEFT JOIN,
2b7e6 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 66 6c 61   then do not fla
2b7e7 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  tten..  */.  if(
2b7e8 20 28 70 53 75 62 69 74 65 6d 2d 3e 6a 6f 69 6e   (pSubitem->join
2b7e9 74 79 70 65 20 26 20 4a 54 5f 4f 55 54 45 52 29  type & JT_OUTER)
2b7ea 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  !=0 ){.    retur
2b7eb 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52  n 0;.  }..  /* R
2b7ec 65 73 74 72 69 63 74 69 6f 6e 20 31 37 3a 20 49  estriction 17: I
2b7ed 66 20 74 68 65 20 73 75 62 2d 71 75 65 72 79 20  f the sub-query 
2b7ee 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45  is a compound SE
2b7ef 4c 45 43 54 2c 20 74 68 65 6e 20 69 74 20 6d 75  LECT, then it mu
2b7f0 73 74 0a 20 20 2a 2a 20 75 73 65 20 6f 6e 6c 79  st.  ** use only
2b7f1 20 74 68 65 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f   the UNION ALL o
2b7f2 70 65 72 61 74 6f 72 2e 20 41 6e 64 20 6e 6f 6e  perator. And non
2b7f3 65 20 6f 66 20 74 68 65 20 73 69 6d 70 6c 65 20  e of the simple 
2b7f4 73 65 6c 65 63 74 20 71 75 65 72 69 65 73 0a 20  select queries. 
2b7f5 20 2a 2a 20 74 68 61 74 20 6d 61 6b 65 20 75 70   ** that make up
2b7f6 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 53 45   the compound SE
2b7f7 4c 45 43 54 20 61 72 65 20 61 6c 6c 6f 77 65 64  LECT are allowed
2b7f8 20 74 6f 20 62 65 20 61 67 67 72 65 67 61 74 65   to be aggregate
2b7f9 20 6f 72 20 64 69 73 74 69 6e 63 74 0a 20 20 2a   or distinct.  *
2b7fa 2a 20 71 75 65 72 69 65 73 2e 0a 20 20 2a 2f 0a  * queries..  */.
2b7fb 20 20 69 66 28 20 70 53 75 62 2d 3e 70 50 72 69    if( pSub->pPri
2b7fc 6f 72 20 29 7b 0a 20 20 20 20 69 66 28 20 70 53  or ){.    if( pS
2b7fd 75 62 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a  ub->pOrderBy ){.
2b7fe 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20        return 0; 
2b7ff 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   /* Restriction 
2b800 32 30 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20  20 */.    }.    
2b801 69 66 28 20 69 73 41 67 67 20 7c 7c 20 28 70 2d  if( isAgg || (p-
2b802 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44  >selFlags & SF_D
2b803 69 73 74 69 6e 63 74 29 21 3d 30 20 7c 7c 20 70  istinct)!=0 || p
2b804 53 72 63 2d 3e 6e 53 72 63 21 3d 31 20 29 7b 0a  Src->nSrc!=1 ){.
2b805 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
2b806 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 53      }.    for(pS
2b807 75 62 31 3d 70 53 75 62 3b 20 70 53 75 62 31 3b  ub1=pSub; pSub1;
2b808 20 70 53 75 62 31 3d 70 53 75 62 31 2d 3e 70 50   pSub1=pSub1->pP
2b809 72 69 6f 72 29 7b 0a 20 20 20 20 20 20 74 65 73  rior){.      tes
2b80a 74 63 61 73 65 28 20 28 70 53 75 62 31 2d 3e 73  tcase( (pSub1->s
2b80b 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69  elFlags & (SF_Di
2b80c 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67  stinct|SF_Aggreg
2b80d 61 74 65 29 29 3d 3d 53 46 5f 44 69 73 74 69 6e  ate))==SF_Distin
2b80e 63 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ct );.      test
2b80f 63 61 73 65 28 20 28 70 53 75 62 31 2d 3e 73 65  case( (pSub1->se
2b810 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73  lFlags & (SF_Dis
2b811 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61  tinct|SF_Aggrega
2b812 74 65 29 29 3d 3d 53 46 5f 41 67 67 72 65 67 61  te))==SF_Aggrega
2b813 74 65 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  te );.      if( 
2b814 28 70 53 75 62 31 2d 3e 73 65 6c 46 6c 61 67 73  (pSub1->selFlags
2b815 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c   & (SF_Distinct|
2b816 53 46 5f 41 67 67 72 65 67 61 74 65 29 29 21 3d  SF_Aggregate))!=
2b817 30 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 53 75  0.       || (pSu
2b818 62 31 2d 3e 70 50 72 69 6f 72 20 26 26 20 70 53  b1->pPrior && pS
2b819 75 62 31 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 29  ub1->op!=TK_ALL)
2b81a 20 0a 20 20 20 20 20 20 20 7c 7c 20 4e 45 56 45   .       || NEVE
2b81b 52 28 70 53 75 62 31 2d 3e 70 53 72 63 3d 3d 30  R(pSub1->pSrc==0
2b81c 29 20 7c 7c 20 70 53 75 62 31 2d 3e 70 53 72 63  ) || pSub1->pSrc
2b81d 2d 3e 6e 53 72 63 21 3d 31 0a 20 20 20 20 20 20  ->nSrc!=1.      
2b81e 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
2b81f 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
2b820 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 73 74 72   }..    /* Restr
2b821 69 63 74 69 6f 6e 20 31 38 2e 20 2a 2f 0a 20 20  iction 18. */.  
2b822 20 20 69 66 28 20 70 2d 3e 70 4f 72 64 65 72 42    if( p->pOrderB
2b823 79 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  y ){.      int i
2b824 69 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d  i;.      for(ii=
2b825 30 3b 20 69 69 3c 70 2d 3e 70 4f 72 64 65 72 42  0; ii<p->pOrderB
2b826 79 2d 3e 6e 45 78 70 72 3b 20 69 69 2b 2b 29 7b  y->nExpr; ii++){
2b827 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e  .        if( p->
2b828 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 69 5d 2e  pOrderBy->a[ii].
2b829 69 43 6f 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e  iCol==0 ) return
2b82a 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
2b82b 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 2a 2a 2a 2a 20  }.  }..  /***** 
2b82c 49 66 20 77 65 20 72 65 61 63 68 20 74 68 69 73  If we reach this
2b82d 20 70 6f 69 6e 74 2c 20 66 6c 61 74 74 65 6e 69   point, flatteni
2b82e 6e 67 20 69 73 20 70 65 72 6d 69 74 74 65 64 2e  ng is permitted.
2b82f 20 2a 2a 2a 2a 2a 2f 0a 0a 20 20 2f 2a 20 41 75   *****/..  /* Au
2b830 74 68 6f 72 69 7a 65 20 74 68 65 20 73 75 62 71  thorize the subq
2b831 75 65 72 79 20 2a 2f 0a 20 20 70 50 61 72 73 65  uery */.  pParse
2b832 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d  ->zAuthContext =
2b833 20 70 53 75 62 69 74 65 6d 2d 3e 7a 4e 61 6d 65   pSubitem->zName
2b834 3b 0a 20 20 73 71 6c 69 74 65 33 41 75 74 68 43  ;.  sqlite3AuthC
2b835 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
2b836 49 54 45 5f 53 45 4c 45 43 54 2c 20 30 2c 20 30  ITE_SELECT, 0, 0
2b837 2c 20 30 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  , 0);.  pParse->
2b838 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a  zAuthContext = z
2b839 53 61 76 65 64 41 75 74 68 43 6f 6e 74 65 78 74  SavedAuthContext
2b83a 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73  ;..  /* If the s
2b83b 75 62 2d 71 75 65 72 79 20 69 73 20 61 20 63 6f  ub-query is a co
2b83c 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 74  mpound SELECT st
2b83d 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 28 62  atement, then (b
2b83e 79 20 72 65 73 74 72 69 63 74 69 6f 6e 73 0a 20  y restrictions. 
2b83f 20 2a 2a 20 31 37 20 61 6e 64 20 31 38 20 61 62   ** 17 and 18 ab
2b840 6f 76 65 29 20 69 74 20 6d 75 73 74 20 62 65 20  ove) it must be 
2b841 61 20 55 4e 49 4f 4e 20 41 4c 4c 20 61 6e 64 20  a UNION ALL and 
2b842 74 68 65 20 70 61 72 65 6e 74 20 71 75 65 72 79  the parent query
2b843 20 6d 75 73 74 20 0a 20 20 2a 2a 20 62 65 20 6f   must .  ** be o
2b844 66 20 74 68 65 20 66 6f 72 6d 3a 0a 20 20 2a 2a  f the form:.  **
2b845 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  .  **     SELECT
2b846 20 3c 65 78 70 72 2d 6c 69 73 74 3e 20 46 52 4f   <expr-list> FRO
2b847 4d 20 28 3c 73 75 62 2d 71 75 65 72 79 3e 29 20  M (<sub-query>) 
2b848 3c 77 68 65 72 65 2d 63 6c 61 75 73 65 3e 20 0a  <where-clause> .
2b849 20 20 2a 2a 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77    **.  ** follow
2b84a 65 64 20 62 79 20 61 6e 79 20 4f 52 44 45 52 20  ed by any ORDER 
2b84b 42 59 2c 20 4c 49 4d 49 54 20 61 6e 64 2f 6f 72  BY, LIMIT and/or
2b84c 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73 2e   OFFSET clauses.
2b84d 20 54 68 69 73 20 62 6c 6f 63 6b 0a 20 20 2a 2a   This block.  **
2b84e 20 63 72 65 61 74 65 73 20 4e 2d 31 20 63 6f 70   creates N-1 cop
2b84f 69 65 73 20 6f 66 20 74 68 65 20 70 61 72 65 6e  ies of the paren
2b850 74 20 71 75 65 72 79 20 77 69 74 68 6f 75 74 20  t query without 
2b851 61 6e 79 20 4f 52 44 45 52 20 42 59 2c 20 4c 49  any ORDER BY, LI
2b852 4d 49 54 20 6f 72 20 0a 20 20 2a 2a 20 4f 46 46  MIT or .  ** OFF
2b853 53 45 54 20 63 6c 61 75 73 65 73 20 61 6e 64 20  SET clauses and 
2b854 6a 6f 69 6e 73 20 74 68 65 6d 20 74 6f 20 74 68  joins them to th
2b855 65 20 6c 65 66 74 2d 68 61 6e 64 2d 73 69 64 65  e left-hand-side
2b856 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   of the original
2b857 0a 20 20 2a 2a 20 75 73 69 6e 67 20 55 4e 49 4f  .  ** using UNIO
2b858 4e 20 41 4c 4c 20 6f 70 65 72 61 74 6f 72 73 2e  N ALL operators.
2b859 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 4e 20   In this case N 
2b85a 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
2b85b 20 73 69 6d 70 6c 65 0a 20 20 2a 2a 20 73 65 6c   simple.  ** sel
2b85c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 73 20 69  ect statements i
2b85d 6e 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73  n the compound s
2b85e 75 62 2d 71 75 65 72 79 2e 0a 20 20 2a 2a 0a 20  ub-query..  **. 
2b85f 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 20 20 2a   ** Example:.  *
2b860 2a 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43  *.  **     SELEC
2b861 54 20 61 2b 31 20 46 52 4f 4d 20 28 0a 20 20 2a  T a+1 FROM (.  *
2b862 2a 20 20 20 20 20 20 20 20 53 45 4c 45 43 54 20  *        SELECT 
2b863 78 20 46 52 4f 4d 20 74 61 62 0a 20 20 2a 2a 20  x FROM tab.  ** 
2b864 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c         UNION ALL
2b865 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 53 45 4c  .  **        SEL
2b866 45 43 54 20 79 20 46 52 4f 4d 20 74 61 62 0a 20  ECT y FROM tab. 
2b867 20 2a 2a 20 20 20 20 20 20 20 20 55 4e 49 4f 4e   **        UNION
2b868 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 20 20   ALL.  **       
2b869 20 53 45 4c 45 43 54 20 61 62 73 28 7a 2a 32 29   SELECT abs(z*2)
2b86a 20 46 52 4f 4d 20 74 61 62 32 0a 20 20 2a 2a 20   FROM tab2.  ** 
2b86b 20 20 20 20 29 20 57 48 45 52 45 20 61 21 3d 35      ) WHERE a!=5
2b86c 20 4f 52 44 45 52 20 42 59 20 31 0a 20 20 2a 2a   ORDER BY 1.  **
2b86d 0a 20 20 2a 2a 20 54 72 61 6e 73 66 6f 72 6d 65  .  ** Transforme
2b86e 64 20 69 6e 74 6f 3a 0a 20 20 2a 2a 0a 20 20 2a  d into:.  **.  *
2b86f 2a 20 20 20 20 20 53 45 4c 45 43 54 20 78 2b 31  *     SELECT x+1
2b870 20 46 52 4f 4d 20 74 61 62 20 57 48 45 52 45 20   FROM tab WHERE 
2b871 78 2b 31 21 3d 35 0a 20 20 2a 2a 20 20 20 20 20  x+1!=5.  **     
2b872 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20  UNION ALL.  **  
2b873 20 20 20 53 45 4c 45 43 54 20 79 2b 31 20 46 52     SELECT y+1 FR
2b874 4f 4d 20 74 61 62 20 57 48 45 52 45 20 79 2b 31  OM tab WHERE y+1
2b875 21 3d 35 0a 20 20 2a 2a 20 20 20 20 20 55 4e 49  !=5.  **     UNI
2b876 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20  ON ALL.  **     
2b877 53 45 4c 45 43 54 20 61 62 73 28 7a 2a 32 29 2b  SELECT abs(z*2)+
2b878 31 20 46 52 4f 4d 20 74 61 62 32 20 57 48 45 52  1 FROM tab2 WHER
2b879 45 20 61 62 73 28 7a 2a 32 29 2b 31 21 3d 35 0a  E abs(z*2)+1!=5.
2b87a 20 20 2a 2a 20 20 20 20 20 4f 52 44 45 52 20 42    **     ORDER B
2b87b 59 20 31 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65  Y 1.  **.  ** We
2b87c 20 63 61 6c 6c 20 74 68 69 73 20 74 68 65 20 22   call this the "
2b87d 63 6f 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65 72  compound-subquer
2b87e 79 20 66 6c 61 74 74 65 6e 69 6e 67 22 2e 0a 20  y flattening".. 
2b87f 20 2a 2f 0a 20 20 66 6f 72 28 70 53 75 62 3d 70   */.  for(pSub=p
2b880 53 75 62 2d 3e 70 50 72 69 6f 72 3b 20 70 53 75  Sub->pPrior; pSu
2b881 62 3b 20 70 53 75 62 3d 70 53 75 62 2d 3e 70 50  b; pSub=pSub->pP
2b882 72 69 6f 72 29 7b 0a 20 20 20 20 53 65 6c 65 63  rior){.    Selec
2b883 74 20 2a 70 4e 65 77 3b 0a 20 20 20 20 45 78 70  t *pNew;.    Exp
2b884 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 20  rList *pOrderBy 
2b885 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20  = p->pOrderBy;. 
2b886 20 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 20     Expr *pLimit 
2b887 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20  = p->pLimit;.   
2b888 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 20   Select *pPrior 
2b889 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20  = p->pPrior;.   
2b88a 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 30   p->pOrderBy = 0
2b88b 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20  ;.    p->pSrc = 
2b88c 30 3b 0a 20 20 20 20 70 2d 3e 70 50 72 69 6f 72  0;.    p->pPrior
2b88d 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 4c 69   = 0;.    p->pLi
2b88e 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 70 4e 65  mit = 0;.    pNe
2b88f 77 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63  w = sqlite3Selec
2b890 74 44 75 70 28 64 62 2c 20 70 2c 20 30 29 3b 0a  tDup(db, p, 0);.
2b891 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20      p->pLimit = 
2b892 70 4c 69 6d 69 74 3b 0a 20 20 20 20 70 2d 3e 70  pLimit;.    p->p
2b893 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72  OrderBy = pOrder
2b894 42 79 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 20  By;.    p->pSrc 
2b895 3d 20 70 53 72 63 3b 0a 20 20 20 20 70 2d 3e 6f  = pSrc;.    p->o
2b896 70 20 3d 20 54 4b 5f 41 4c 4c 3b 0a 20 20 20 20  p = TK_ALL;.    
2b897 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20  p->pRightmost = 
2b898 30 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d  0;.    if( pNew=
2b899 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4e 65 77  =0 ){.      pNew
2b89a 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 7d   = pPrior;.    }
2b89b 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4e 65 77  else{.      pNew
2b89c 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69 6f  ->pPrior = pPrio
2b89d 72 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70  r;.      pNew->p
2b89e 52 69 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a 20  Rightmost = 0;. 
2b89f 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70 50 72 69     }.    p->pPri
2b8a0 6f 72 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 69  or = pNew;.    i
2b8a1 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
2b8a2 6c 65 64 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  led ) return 1;.
2b8a3 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20    }..  /* Begin 
2b8a4 66 6c 61 74 74 65 6e 69 6e 67 20 74 68 65 20 69  flattening the i
2b8a5 46 72 6f 6d 2d 74 68 20 65 6e 74 72 79 20 6f 66  From-th entry of
2b8a6 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2b8a7 20 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 6f 75   .  ** in the ou
2b8a8 74 65 72 20 71 75 65 72 79 2e 0a 20 20 2a 2f 0a  ter query..  */.
2b8a9 20 20 70 53 75 62 20 3d 20 70 53 75 62 31 20 3d    pSub = pSub1 =
2b8aa 20 70 53 75 62 69 74 65 6d 2d 3e 70 53 65 6c 65   pSubitem->pSele
2b8ab 63 74 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65  ct;..  /* Delete
2b8ac 20 74 68 65 20 74 72 61 6e 73 69 65 6e 74 20 74   the transient t
2b8ad 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 61  able structure a
2b8ae 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
2b8af 68 65 0a 20 20 2a 2a 20 73 75 62 71 75 65 72 79  he.  ** subquery
2b8b0 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 44  .  */.  sqlite3D
2b8b1 62 46 72 65 65 28 64 62 2c 20 70 53 75 62 69 74  bFree(db, pSubit
2b8b2 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a  em->zDatabase);.
2b8b3 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2b8b4 64 62 2c 20 70 53 75 62 69 74 65 6d 2d 3e 7a 4e  db, pSubitem->zN
2b8b5 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ame);.  sqlite3D
2b8b6 62 46 72 65 65 28 64 62 2c 20 70 53 75 62 69 74  bFree(db, pSubit
2b8b7 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 70  em->zAlias);.  p
2b8b8 53 75 62 69 74 65 6d 2d 3e 7a 44 61 74 61 62 61  Subitem->zDataba
2b8b9 73 65 20 3d 20 30 3b 0a 20 20 70 53 75 62 69 74  se = 0;.  pSubit
2b8ba 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20  em->zName = 0;. 
2b8bb 20 70 53 75 62 69 74 65 6d 2d 3e 7a 41 6c 69 61   pSubitem->zAlia
2b8bc 73 20 3d 20 30 3b 0a 20 20 70 53 75 62 69 74 65  s = 0;.  pSubite
2b8bd 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 30 3b 0a  m->pSelect = 0;.
2b8be 0a 20 20 2f 2a 20 44 65 66 65 72 20 64 65 6c 65  .  /* Defer dele
2b8bf 74 69 6e 67 20 74 68 65 20 54 61 62 6c 65 20 6f  ting the Table o
2b8c0 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65 64  bject associated
2b8c1 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 73   with the.  ** s
2b8c2 75 62 71 75 65 72 79 20 75 6e 74 69 6c 20 63 6f  ubquery until co
2b8c3 64 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 69 73  de generation is
2b8c4 0a 20 20 2a 2a 20 63 6f 6d 70 6c 65 74 65 2c 20  .  ** complete, 
2b8c5 73 69 6e 63 65 20 74 68 65 72 65 20 6d 61 79 20  since there may 
2b8c6 73 74 69 6c 6c 20 65 78 69 73 74 20 45 78 70 72  still exist Expr
2b8c7 2e 70 54 61 62 20 65 6e 74 72 69 65 73 20 74 68  .pTab entries th
2b8c8 61 74 0a 20 20 2a 2a 20 72 65 66 65 72 20 74 6f  at.  ** refer to
2b8c9 20 74 68 65 20 73 75 62 71 75 65 72 79 20 65 76   the subquery ev
2b8ca 65 6e 20 61 66 74 65 72 20 66 6c 61 74 74 65 6e  en after flatten
2b8cb 69 6e 67 2e 20 20 54 69 63 6b 65 74 20 23 33 33  ing.  Ticket #33
2b8cc 34 36 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 70 53  46..  **.  ** pS
2b8cd 75 62 69 74 65 6d 2d 3e 70 54 61 62 20 69 73 20  ubitem->pTab is 
2b8ce 61 6c 77 61 79 73 20 6e 6f 6e 2d 4e 55 4c 4c 20  always non-NULL 
2b8cf 62 79 20 74 65 73 74 20 72 65 73 74 72 69 63 74  by test restrict
2b8d0 69 6f 6e 73 20 61 6e 64 20 74 65 73 74 73 20 61  ions and tests a
2b8d1 62 6f 76 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  bove..  */.  if(
2b8d2 20 41 4c 57 41 59 53 28 70 53 75 62 69 74 65 6d   ALWAYS(pSubitem
2b8d3 2d 3e 70 54 61 62 21 3d 30 29 20 29 7b 0a 20 20  ->pTab!=0) ){.  
2b8d4 20 20 54 61 62 6c 65 20 2a 70 54 61 62 54 6f 44    Table *pTabToD
2b8d5 65 6c 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 70  el = pSubitem->p
2b8d6 54 61 62 3b 0a 20 20 20 20 69 66 28 20 70 54 61  Tab;.    if( pTa
2b8d7 62 54 6f 44 65 6c 2d 3e 6e 52 65 66 3d 3d 31 20  bToDel->nRef==1 
2b8d8 29 7b 0a 20 20 20 20 20 20 50 61 72 73 65 20 2a  ){.      Parse *
2b8d9 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69  pToplevel = sqli
2b8da 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c  te3ParseToplevel
2b8db 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
2b8dc 70 54 61 62 54 6f 44 65 6c 2d 3e 70 4e 65 78 74  pTabToDel->pNext
2b8dd 5a 6f 6d 62 69 65 20 3d 20 70 54 6f 70 6c 65 76  Zombie = pToplev
2b8de 65 6c 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 3b 0a  el->pZombieTab;.
2b8df 20 20 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d        pToplevel-
2b8e0 3e 70 5a 6f 6d 62 69 65 54 61 62 20 3d 20 70 54  >pZombieTab = pT
2b8e1 61 62 54 6f 44 65 6c 3b 0a 20 20 20 20 7d 65 6c  abToDel;.    }el
2b8e2 73 65 7b 0a 20 20 20 20 20 20 70 54 61 62 54 6f  se{.      pTabTo
2b8e3 44 65 6c 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20  Del->nRef--;.   
2b8e4 20 7d 0a 20 20 20 20 70 53 75 62 69 74 65 6d 2d   }.    pSubitem-
2b8e5 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20 7d 0a 0a  >pTab = 0;.  }..
2b8e6 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
2b8e7 6e 67 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 63  ng loop runs onc
2b8e8 65 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d 20  e for each term 
2b8e9 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 2d 73 75  in a compound-su
2b8ea 62 71 75 65 72 79 0a 20 20 2a 2a 20 66 6c 61 74  bquery.  ** flat
2b8eb 74 65 6e 69 6e 67 20 28 61 73 20 64 65 73 63 72  tening (as descr
2b8ec 69 62 65 64 20 61 62 6f 76 65 29 2e 20 20 49 66  ibed above).  If
2b8ed 20 77 65 20 61 72 65 20 64 6f 69 6e 67 20 61 20   we are doing a 
2b8ee 64 69 66 66 65 72 65 6e 74 20 6b 69 6e 64 0a 20  different kind. 
2b8ef 20 2a 2a 20 6f 66 20 66 6c 61 74 74 65 6e 69 6e   ** of flattenin
2b8f0 67 20 2d 20 61 20 66 6c 61 74 74 65 6e 69 6e 67  g - a flattening
2b8f1 20 6f 74 68 65 72 20 74 68 61 6e 20 61 20 63 6f   other than a co
2b8f2 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65 72 79 20  mpound-subquery 
2b8f3 66 6c 61 74 74 65 6e 69 6e 67 20 2d 0a 20 20 2a  flattening -.  *
2b8f4 2a 20 74 68 65 6e 20 74 68 69 73 20 6c 6f 6f 70  * then this loop
2b8f5 20 6f 6e 6c 79 20 72 75 6e 73 20 6f 6e 63 65 2e   only runs once.
2b8f6 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
2b8f7 6c 6f 6f 70 20 6d 6f 76 65 73 20 61 6c 6c 20 6f  loop moves all o
2b8f8 66 20 74 68 65 20 46 52 4f 4d 20 65 6c 65 6d 65  f the FROM eleme
2b8f9 6e 74 73 20 6f 66 20 74 68 65 20 73 75 62 71 75  nts of the subqu
2b8fa 65 72 79 20 69 6e 74 6f 20 74 68 65 0a 20 20 2a  ery into the.  *
2b8fb 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  * the FROM claus
2b8fc 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20 71  e of the outer q
2b8fd 75 65 72 79 2e 20 20 42 65 66 6f 72 65 20 64 6f  uery.  Before do
2b8fe 69 6e 67 20 74 68 69 73 2c 20 72 65 6d 65 6d 62  ing this, rememb
2b8ff 65 72 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 73  er.  ** the curs
2b900 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  or number for th
2b901 65 20 6f 72 69 67 69 6e 61 6c 20 6f 75 74 65 72  e original outer
2b902 20 71 75 65 72 79 20 46 52 4f 4d 20 65 6c 65 6d   query FROM elem
2b903 65 6e 74 20 69 6e 0a 20 20 2a 2a 20 69 50 61 72  ent in.  ** iPar
2b904 65 6e 74 2e 20 20 54 68 65 20 69 50 61 72 65 6e  ent.  The iParen
2b905 74 20 63 75 72 73 6f 72 20 77 69 6c 6c 20 6e 65  t cursor will ne
2b906 76 65 72 20 62 65 20 75 73 65 64 2e 20 20 53 75  ver be used.  Su
2b907 62 73 65 71 75 65 6e 74 20 63 6f 64 65 0a 20 20  bsequent code.  
2b908 2a 2a 20 77 69 6c 6c 20 73 63 61 6e 20 65 78 70  ** will scan exp
2b909 72 65 73 73 69 6f 6e 73 20 6c 6f 6f 6b 69 6e 67  ressions looking
2b90a 20 66 6f 72 20 69 50 61 72 65 6e 74 20 72 65 66   for iParent ref
2b90b 65 72 65 6e 63 65 73 20 61 6e 64 20 72 65 70 6c  erences and repl
2b90c 61 63 65 0a 20 20 2a 2a 20 74 68 6f 73 65 20 72  ace.  ** those r
2b90d 65 66 65 72 65 6e 63 65 73 20 77 69 74 68 20 65  eferences with e
2b90e 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20  xpressions that 
2b90f 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 73  resolve to the s
2b910 75 62 71 75 65 72 79 20 46 52 4f 4d 0a 20 20 2a  ubquery FROM.  *
2b911 2a 20 65 6c 65 6d 65 6e 74 73 20 77 65 20 61 72  * elements we ar
2b912 65 20 6e 6f 77 20 63 6f 70 79 69 6e 67 20 69 6e  e now copying in
2b913 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 50 61  ..  */.  for(pPa
2b914 72 65 6e 74 3d 70 3b 20 70 50 61 72 65 6e 74 3b  rent=p; pParent;
2b915 20 70 50 61 72 65 6e 74 3d 70 50 61 72 65 6e 74   pParent=pParent
2b916 2d 3e 70 50 72 69 6f 72 2c 20 70 53 75 62 3d 70  ->pPrior, pSub=p
2b917 53 75 62 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20  Sub->pPrior){.  
2b918 20 20 69 6e 74 20 6e 53 75 62 53 72 63 3b 0a 20    int nSubSrc;. 
2b919 20 20 20 75 38 20 6a 6f 69 6e 74 79 70 65 20 3d     u8 jointype =
2b91a 20 30 3b 0a 20 20 20 20 70 53 75 62 53 72 63 20   0;.    pSubSrc 
2b91b 3d 20 70 53 75 62 2d 3e 70 53 72 63 3b 20 20 20  = pSub->pSrc;   
2b91c 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65    /* FROM clause
2b91d 20 6f 66 20 73 75 62 71 75 65 72 79 20 2a 2f 0a   of subquery */.
2b91e 20 20 20 20 6e 53 75 62 53 72 63 20 3d 20 70 53      nSubSrc = pS
2b91f 75 62 53 72 63 2d 3e 6e 53 72 63 3b 20 20 2f 2a  ubSrc->nSrc;  /*
2b920 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73   Number of terms
2b921 20 69 6e 20 73 75 62 71 75 65 72 79 20 46 52 4f   in subquery FRO
2b922 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20  M clause */.    
2b923 70 53 72 63 20 3d 20 70 50 61 72 65 6e 74 2d 3e  pSrc = pParent->
2b924 70 53 72 63 3b 20 20 20 20 20 2f 2a 20 46 52 4f  pSrc;     /* FRO
2b925 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20  M clause of the 
2b926 6f 75 74 65 72 20 71 75 65 72 79 20 2a 2f 0a 0a  outer query */..
2b927 20 20 20 20 69 66 28 20 70 53 72 63 20 29 7b 0a      if( pSrc ){.
2b928 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
2b929 61 72 65 6e 74 3d 3d 70 20 29 3b 20 20 2f 2a 20  arent==p );  /* 
2b92a 46 69 72 73 74 20 74 69 6d 65 20 74 68 72 6f 75  First time throu
2b92b 67 68 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  gh the loop */. 
2b92c 20 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 3d 20       jointype = 
2b92d 70 53 75 62 69 74 65 6d 2d 3e 6a 6f 69 6e 74 79  pSubitem->jointy
2b92e 70 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  pe;.    }else{. 
2b92f 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
2b930 72 65 6e 74 21 3d 70 20 29 3b 20 20 2f 2a 20 32  rent!=p );  /* 2
2b931 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e  nd and subsequen
2b932 74 20 74 69 6d 65 73 20 74 68 72 6f 75 67 68 20  t times through 
2b933 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20  the loop */.    
2b934 20 20 70 53 72 63 20 3d 20 70 50 61 72 65 6e 74    pSrc = pParent
2b935 2d 3e 70 53 72 63 20 3d 20 73 71 6c 69 74 65 33  ->pSrc = sqlite3
2b936 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 64 62  SrcListAppend(db
2b937 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
2b938 20 20 69 66 28 20 70 53 72 63 3d 3d 30 20 29 7b    if( pSrc==0 ){
2b939 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2b93a 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2b93b 64 20 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  d );.        bre
2b93c 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2b93d 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 75  }..    /* The su
2b93e 62 71 75 65 72 79 20 75 73 65 73 20 61 20 73 69  bquery uses a si
2b93f 6e 67 6c 65 20 73 6c 6f 74 20 6f 66 20 74 68 65  ngle slot of the
2b940 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
2b941 74 68 65 20 6f 75 74 65 72 0a 20 20 20 20 2a 2a  the outer.    **
2b942 20 71 75 65 72 79 2e 20 20 49 66 20 74 68 65 20   query.  If the 
2b943 73 75 62 71 75 65 72 79 20 68 61 73 20 6d 6f 72  subquery has mor
2b944 65 20 74 68 61 6e 20 6f 6e 65 20 65 6c 65 6d 65  e than one eleme
2b945 6e 74 20 69 6e 20 69 74 73 20 46 52 4f 4d 20 63  nt in its FROM c
2b946 6c 61 75 73 65 2c 0a 20 20 20 20 2a 2a 20 74 68  lause,.    ** th
2b947 65 6e 20 65 78 70 61 6e 64 20 74 68 65 20 6f 75  en expand the ou
2b948 74 65 72 20 71 75 65 72 79 20 74 6f 20 6d 61 6b  ter query to mak
2b949 65 20 73 70 61 63 65 20 66 6f 72 20 69 74 20 74  e space for it t
2b94a 6f 20 68 6f 6c 64 20 61 6c 6c 20 65 6c 65 6d 65  o hold all eleme
2b94b 6e 74 73 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68  nts.    ** of th
2b94c 65 20 73 75 62 71 75 65 72 79 2e 0a 20 20 20 20  e subquery..    
2b94d 2a 2a 0a 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c  **.    ** Exampl
2b94e 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  e:.    **.    **
2b94f 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f      SELECT * FRO
2b950 4d 20 74 61 62 41 2c 20 28 53 45 4c 45 43 54 20  M tabA, (SELECT 
2b951 2a 20 46 52 4f 4d 20 73 75 62 31 2c 20 73 75 62  * FROM sub1, sub
2b952 32 29 2c 20 74 61 62 42 3b 0a 20 20 20 20 2a 2a  2), tabB;.    **
2b953 0a 20 20 20 20 2a 2a 20 54 68 65 20 6f 75 74 65  .    ** The oute
2b954 72 20 71 75 65 72 79 20 68 61 73 20 33 20 73 6c  r query has 3 sl
2b955 6f 74 73 20 69 6e 20 69 74 73 20 46 52 4f 4d 20  ots in its FROM 
2b956 63 6c 61 75 73 65 2e 20 20 4f 6e 65 20 73 6c 6f  clause.  One slo
2b957 74 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20  t of the.    ** 
2b958 6f 75 74 65 72 20 71 75 65 72 79 20 28 74 68 65  outer query (the
2b959 20 6d 69 64 64 6c 65 20 73 6c 6f 74 29 20 69 73   middle slot) is
2b95a 20 75 73 65 64 20 62 79 20 74 68 65 20 73 75 62   used by the sub
2b95b 71 75 65 72 79 2e 20 20 54 68 65 20 6e 65 78 74  query.  The next
2b95c 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 6f 66  .    ** block of
2b95d 20 63 6f 64 65 20 77 69 6c 6c 20 65 78 70 61 6e   code will expan
2b95e 64 20 74 68 65 20 6f 75 74 20 71 75 65 72 79 20  d the out query 
2b95f 74 6f 20 34 20 73 6c 6f 74 73 2e 20 20 54 68 65  to 4 slots.  The
2b960 20 6d 69 64 64 6c 65 0a 20 20 20 20 2a 2a 20 73   middle.    ** s
2b961 6c 6f 74 20 69 73 20 65 78 70 61 6e 64 65 64 20  lot is expanded 
2b962 74 6f 20 74 77 6f 20 73 6c 6f 74 73 20 69 6e 20  to two slots in 
2b963 6f 72 64 65 72 20 74 6f 20 6d 61 6b 65 20 73 70  order to make sp
2b964 61 63 65 20 66 6f 72 20 74 68 65 0a 20 20 20 20  ace for the.    
2b965 2a 2a 20 74 77 6f 20 65 6c 65 6d 65 6e 74 73 20  ** two elements 
2b966 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
2b967 73 65 20 6f 66 20 74 68 65 20 73 75 62 71 75 65  se of the subque
2b968 72 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ry..    */.    i
2b969 66 28 20 6e 53 75 62 53 72 63 3e 31 20 29 7b 0a  f( nSubSrc>1 ){.
2b96a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70        pParent->p
2b96b 53 72 63 20 3d 20 70 53 72 63 20 3d 20 73 71 6c  Src = pSrc = sql
2b96c 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72  ite3SrcListEnlar
2b96d 67 65 28 64 62 2c 20 70 53 72 63 2c 20 6e 53 75  ge(db, pSrc, nSu
2b96e 62 53 72 63 2d 31 2c 69 46 72 6f 6d 2b 31 29 3b  bSrc-1,iFrom+1);
2b96f 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6d  .      if( db->m
2b970 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
2b971 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2b972 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
2b973 20 2f 2a 20 54 72 61 6e 73 66 65 72 20 74 68 65   /* Transfer the
2b974 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72   FROM clause ter
2b975 6d 73 20 66 72 6f 6d 20 74 68 65 20 73 75 62 71  ms from the subq
2b976 75 65 72 79 20 69 6e 74 6f 20 74 68 65 0a 20 20  uery into the.  
2b977 20 20 2a 2a 20 6f 75 74 65 72 20 71 75 65 72 79    ** outer query
2b978 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  ..    */.    for
2b979 28 69 3d 30 3b 20 69 3c 6e 53 75 62 53 72 63 3b  (i=0; i<nSubSrc;
2b97a 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c   i++){.      sql
2b97b 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
2b97c 28 64 62 2c 20 70 53 72 63 2d 3e 61 5b 69 2b 69  (db, pSrc->a[i+i
2b97d 46 72 6f 6d 5d 2e 70 55 73 69 6e 67 29 3b 0a 20  From].pUsing);. 
2b97e 20 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 2b 69       pSrc->a[i+i
2b97f 46 72 6f 6d 5d 20 3d 20 70 53 75 62 53 72 63 2d  From] = pSubSrc-
2b980 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 6d 65 6d  >a[i];.      mem
2b981 73 65 74 28 26 70 53 75 62 53 72 63 2d 3e 61 5b  set(&pSubSrc->a[
2b982 69 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 53  i], 0, sizeof(pS
2b983 75 62 53 72 63 2d 3e 61 5b 69 5d 29 29 3b 0a 20  ubSrc->a[i]));. 
2b984 20 20 20 7d 0a 20 20 20 20 70 53 72 63 2d 3e 61     }.    pSrc->a
2b985 5b 69 46 72 6f 6d 5d 2e 6a 6f 69 6e 74 79 70 65  [iFrom].jointype
2b986 20 3d 20 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 0a   = jointype;.  .
2b987 20 20 20 20 2f 2a 20 4e 6f 77 20 62 65 67 69 6e      /* Now begin
2b988 20 73 75 62 73 74 69 74 75 74 69 6e 67 20 73 75   substituting su
2b989 62 71 75 65 72 79 20 72 65 73 75 6c 74 20 73 65  bquery result se
2b98a 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 66 6f  t expressions fo
2b98b 72 20 0a 20 20 20 20 2a 2a 20 72 65 66 65 72 65  r .    ** refere
2b98c 6e 63 65 73 20 74 6f 20 74 68 65 20 69 50 61 72  nces to the iPar
2b98d 65 6e 74 20 69 6e 20 74 68 65 20 6f 75 74 65 72  ent in the outer
2b98e 20 71 75 65 72 79 2e 0a 20 20 20 20 2a 2a 20 0a   query..    ** .
2b98f 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a      ** Example:.
2b990 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2b991 53 45 4c 45 43 54 20 61 2b 35 2c 20 62 2a 31 30  SELECT a+5, b*10
2b992 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20 78 2a   FROM (SELECT x*
2b993 33 20 41 53 20 61 2c 20 79 2b 31 30 20 41 53 20  3 AS a, y+10 AS 
2b994 62 20 46 52 4f 4d 20 74 31 29 20 57 48 45 52 45  b FROM t1) WHERE
2b995 20 61 3e 62 3b 0a 20 20 20 20 2a 2a 20 20 20 5c   a>b;.    **   \
2b996 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b997 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f       \__________
2b998 5f 5f 5f 20 73 75 62 71 75 65 72 79 20 5f 5f 5f  ___ subquery ___
2b999 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 20  _______/        
2b99a 20 20 2f 0a 20 20 20 20 2a 2a 20 20 20 20 5c 5f    /.    **    \_
2b99b 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
2b99c 5f 5f 5f 5f 20 6f 75 74 65 72 20 71 75 65 72 79  ____ outer query
2b99d 20 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f   _______________
2b99e 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f  _______________/
2b99f 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57  .    **.    ** W
2b9a0 65 20 6c 6f 6f 6b 20 61 74 20 65 76 65 72 79 20  e look at every 
2b9a1 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68  expression in th
2b9a2 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 61 6e  e outer query an
2b9a3 64 20 65 76 65 72 79 20 70 6c 61 63 65 20 77 65  d every place we
2b9a4 20 73 65 65 0a 20 20 20 20 2a 2a 20 22 61 22 20   see.    ** "a" 
2b9a5 77 65 20 73 75 62 73 74 69 74 75 74 65 20 22 78  we substitute "x
2b9a6 2a 33 22 20 61 6e 64 20 65 76 65 72 79 20 70 6c  *3" and every pl
2b9a7 61 63 65 20 77 65 20 73 65 65 20 22 62 22 20 77  ace we see "b" w
2b9a8 65 20 73 75 62 73 74 69 74 75 74 65 20 22 79 2b  e substitute "y+
2b9a9 31 30 22 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  10"..    */.    
2b9aa 70 4c 69 73 74 20 3d 20 70 50 61 72 65 6e 74 2d  pList = pParent-
2b9ab 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 66 6f 72  >pEList;.    for
2b9ac 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  (i=0; i<pList->n
2b9ad 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
2b9ae 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 61 5b 69    if( pList->a[i
2b9af 5d 2e 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20  ].zName==0 ){.  
2b9b0 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
2b9b1 20 2a 7a 53 70 61 6e 20 3d 20 70 4c 69 73 74 2d   *zSpan = pList-
2b9b2 3e 61 5b 69 5d 2e 7a 53 70 61 6e 3b 0a 20 20 20  >a[i].zSpan;.   
2b9b3 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28       if( ALWAYS(
2b9b4 7a 53 70 61 6e 29 20 29 7b 0a 20 20 20 20 20 20  zSpan) ){.      
2b9b5 20 20 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e      pList->a[i].
2b9b6 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44  zName = sqlite3D
2b9b7 62 53 74 72 44 75 70 28 64 62 2c 20 7a 53 70 61  bStrDup(db, zSpa
2b9b8 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  n);.        }.  
2b9b9 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2b9ba 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64 62  substExprList(db
2b9bb 2c 20 70 50 61 72 65 6e 74 2d 3e 70 45 4c 69 73  , pParent->pELis
2b9bc 74 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75 62  t, iParent, pSub
2b9bd 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 69  ->pEList);.    i
2b9be 66 28 20 69 73 41 67 67 20 29 7b 0a 20 20 20 20  f( isAgg ){.    
2b9bf 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28    substExprList(
2b9c0 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 47 72  db, pParent->pGr
2b9c1 6f 75 70 42 79 2c 20 69 50 61 72 65 6e 74 2c 20  oupBy, iParent, 
2b9c2 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20  pSub->pEList);. 
2b9c3 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 48       pParent->pH
2b9c4 61 76 69 6e 67 20 3d 20 73 75 62 73 74 45 78 70  aving = substExp
2b9c5 72 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70  r(db, pParent->p
2b9c6 48 61 76 69 6e 67 2c 20 69 50 61 72 65 6e 74 2c  Having, iParent,
2b9c7 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a   pSub->pEList);.
2b9c8 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 53      }.    if( pS
2b9c9 75 62 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a  ub->pOrderBy ){.
2b9ca 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
2b9cb 61 72 65 6e 74 2d 3e 70 4f 72 64 65 72 42 79 3d  arent->pOrderBy=
2b9cc 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72  =0 );.      pPar
2b9cd 65 6e 74 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20  ent->pOrderBy = 
2b9ce 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 3b 0a  pSub->pOrderBy;.
2b9cf 20 20 20 20 20 20 70 53 75 62 2d 3e 70 4f 72 64        pSub->pOrd
2b9d0 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 7d 65  erBy = 0;.    }e
2b9d1 6c 73 65 20 69 66 28 20 70 50 61 72 65 6e 74 2d  lse if( pParent-
2b9d2 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20  >pOrderBy ){.   
2b9d3 20 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74     substExprList
2b9d4 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 4f  (db, pParent->pO
2b9d5 72 64 65 72 42 79 2c 20 69 50 61 72 65 6e 74 2c  rderBy, iParent,
2b9d6 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a   pSub->pEList);.
2b9d7 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 53      }.    if( pS
2b9d8 75 62 2d 3e 70 57 68 65 72 65 20 29 7b 0a 20 20  ub->pWhere ){.  
2b9d9 20 20 20 20 70 57 68 65 72 65 20 3d 20 73 71 6c      pWhere = sql
2b9da 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
2b9db 70 53 75 62 2d 3e 70 57 68 65 72 65 2c 20 30 29  pSub->pWhere, 0)
2b9dc 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2b9dd 20 20 20 70 57 68 65 72 65 20 3d 20 30 3b 0a 20     pWhere = 0;. 
2b9de 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 75 62     }.    if( sub
2b9df 71 75 65 72 79 49 73 41 67 67 20 29 7b 0a 20 20  queryIsAgg ){.  
2b9e0 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72      assert( pPar
2b9e1 65 6e 74 2d 3e 70 48 61 76 69 6e 67 3d 3d 30 20  ent->pHaving==0 
2b9e2 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74  );.      pParent
2b9e3 2d 3e 70 48 61 76 69 6e 67 20 3d 20 70 50 61 72  ->pHaving = pPar
2b9e4 65 6e 74 2d 3e 70 57 68 65 72 65 3b 0a 20 20 20  ent->pWhere;.   
2b9e5 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65     pParent->pWhe
2b9e6 72 65 20 3d 20 70 57 68 65 72 65 3b 0a 20 20 20  re = pWhere;.   
2b9e7 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76     pParent->pHav
2b9e8 69 6e 67 20 3d 20 73 75 62 73 74 45 78 70 72 28  ing = substExpr(
2b9e9 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 48 61  db, pParent->pHa
2b9ea 76 69 6e 67 2c 20 69 50 61 72 65 6e 74 2c 20 70  ving, iParent, p
2b9eb 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20  Sub->pEList);.  
2b9ec 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 48 61      pParent->pHa
2b9ed 76 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 45 78  ving = sqlite3Ex
2b9ee 70 72 41 6e 64 28 64 62 2c 20 70 50 61 72 65 6e  prAnd(db, pParen
2b9ef 74 2d 3e 70 48 61 76 69 6e 67 2c 20 0a 20 20 20  t->pHaving, .   
2b9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b9f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
2b9f2 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
2b9f3 2c 20 70 53 75 62 2d 3e 70 48 61 76 69 6e 67 2c  , pSub->pHaving,
2b9f4 20 30 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65   0));.      asse
2b9f5 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 70 47 72  rt( pParent->pGr
2b9f6 6f 75 70 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20  oupBy==0 );.    
2b9f7 20 20 70 50 61 72 65 6e 74 2d 3e 70 47 72 6f 75    pParent->pGrou
2b9f8 70 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70  pBy = sqlite3Exp
2b9f9 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 53 75  rListDup(db, pSu
2b9fa 62 2d 3e 70 47 72 6f 75 70 42 79 2c 20 30 29 3b  b->pGroupBy, 0);
2b9fb 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2b9fc 20 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72    pParent->pWher
2b9fd 65 20 3d 20 73 75 62 73 74 45 78 70 72 28 64 62  e = substExpr(db
2b9fe 2c 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72  , pParent->pWher
2b9ff 65 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75 62  e, iParent, pSub
2ba00 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20  ->pEList);.     
2ba01 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65   pParent->pWhere
2ba02 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e   = sqlite3ExprAn
2ba03 64 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70  d(db, pParent->p
2ba04 57 68 65 72 65 2c 20 70 57 68 65 72 65 29 3b 0a  Where, pWhere);.
2ba05 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20      }.  .    /* 
2ba06 54 68 65 20 66 6c 61 74 74 65 6e 65 64 20 71 75  The flattened qu
2ba07 65 72 79 20 69 73 20 64 69 73 74 69 6e 63 74 20  ery is distinct 
2ba08 69 66 20 65 69 74 68 65 72 20 74 68 65 20 69 6e  if either the in
2ba09 6e 65 72 20 6f 72 20 74 68 65 0a 20 20 20 20 2a  ner or the.    *
2ba0a 2a 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73  * outer query is
2ba0b 20 64 69 73 74 69 6e 63 74 2e 20 0a 20 20 20 20   distinct. .    
2ba0c 2a 2f 0a 20 20 20 20 70 50 61 72 65 6e 74 2d 3e  */.    pParent->
2ba0d 73 65 6c 46 6c 61 67 73 20 7c 3d 20 70 53 75 62  selFlags |= pSub
2ba0e 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f  ->selFlags & SF_
2ba0f 44 69 73 74 69 6e 63 74 3b 0a 20 20 0a 20 20 20  Distinct;.  .   
2ba10 20 2f 2a 0a 20 20 20 20 2a 2a 20 53 45 4c 45 43   /*.    ** SELEC
2ba11 54 20 2e 2e 2e 20 46 52 4f 4d 20 28 53 45 4c 45  T ... FROM (SELE
2ba12 43 54 20 2e 2e 2e 20 4c 49 4d 49 54 20 61 20 4f  CT ... LIMIT a O
2ba13 46 46 53 45 54 20 62 29 20 4c 49 4d 49 54 20 78  FFSET b) LIMIT x
2ba14 20 4f 46 46 53 45 54 20 79 3b 0a 20 20 20 20 2a   OFFSET y;.    *
2ba15 2a 0a 20 20 20 20 2a 2a 20 4f 6e 65 20 69 73 20  *.    ** One is 
2ba16 74 65 6d 70 74 65 64 20 74 6f 20 74 72 79 20 74  tempted to try t
2ba17 6f 20 61 64 64 20 61 20 61 6e 64 20 62 20 74 6f  o add a and b to
2ba18 20 63 6f 6d 62 69 6e 65 20 74 68 65 20 6c 69 6d   combine the lim
2ba19 69 74 73 2e 20 20 42 75 74 20 74 68 69 73 0a 20  its.  But this. 
2ba1a 20 20 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 77     ** does not w
2ba1b 6f 72 6b 20 69 66 20 65 69 74 68 65 72 20 6c 69  ork if either li
2ba1c 6d 69 74 20 69 73 20 6e 65 67 61 74 69 76 65 2e  mit is negative.
2ba1d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
2ba1e 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a  pSub->pLimit ){.
2ba1f 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70        pParent->p
2ba20 4c 69 6d 69 74 20 3d 20 70 53 75 62 2d 3e 70 4c  Limit = pSub->pL
2ba21 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 53 75 62  imit;.      pSub
2ba22 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20  ->pLimit = 0;.  
2ba23 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69    }.  }..  /* Fi
2ba24 6e 69 61 6c 6c 79 2c 20 64 65 6c 65 74 65 20 77  nially, delete w
2ba25 68 61 74 20 69 73 20 6c 65 66 74 20 6f 66 20 74  hat is left of t
2ba26 68 65 20 73 75 62 71 75 65 72 79 20 61 6e 64 20  he subquery and 
2ba27 72 65 74 75 72 6e 0a 20 20 2a 2a 20 73 75 63 63  return.  ** succ
2ba28 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ess..  */.  sqli
2ba29 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
2ba2a 64 62 2c 20 70 53 75 62 31 29 3b 0a 0a 20 20 72  db, pSub1);..  r
2ba2b 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69  eturn 1;.}.#endi
2ba2c 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
2ba2d 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
2ba2e 52 59 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28  RY) || !defined(
2ba2f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
2ba30 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c  ) */../*.** Anal
2ba31 79 7a 65 20 74 68 65 20 53 45 4c 45 43 54 20 73  yze the SELECT s
2ba32 74 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20  tatement passed 
2ba33 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74  as an argument t
2ba34 6f 20 73 65 65 20 69 66 20 69 74 0a 2a 2a 20 69  o see if it.** i
2ba35 73 20 61 20 6d 69 6e 28 29 20 6f 72 20 6d 61 78  s a min() or max
2ba36 28 29 20 71 75 65 72 79 2e 20 52 65 74 75 72 6e  () query. Return
2ba37 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d   WHERE_ORDERBY_M
2ba38 49 4e 20 6f 72 20 57 48 45 52 45 5f 4f 52 44 45  IN or WHERE_ORDE
2ba39 52 42 59 5f 4d 41 58 20 69 66 20 0a 2a 2a 20 69  RBY_MAX if .** i
2ba3a 74 20 69 73 2c 20 6f 72 20 30 20 6f 74 68 65 72  t is, or 0 other
2ba3b 77 69 73 65 2e 20 41 74 20 70 72 65 73 65 6e 74  wise. At present
2ba3c 2c 20 61 20 71 75 65 72 79 20 69 73 20 63 6f 6e  , a query is con
2ba3d 73 69 64 65 72 65 64 20 74 6f 20 62 65 0a 2a 2a  sidered to be.**
2ba3e 20 61 20 6d 69 6e 28 29 2f 6d 61 78 28 29 20 71   a min()/max() q
2ba3f 75 65 72 79 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20  uery if:.**.**  
2ba40 20 31 2e 20 54 68 65 72 65 20 69 73 20 61 20 73   1. There is a s
2ba41 69 6e 67 6c 65 20 6f 62 6a 65 63 74 20 69 6e 20  ingle object in 
2ba42 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e  the FROM clause.
2ba43 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 54 68 65 72  .**.**   2. Ther
2ba44 65 20 69 73 20 61 20 73 69 6e 67 6c 65 20 65 78  e is a single ex
2ba45 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20  pression in the 
2ba46 72 65 73 75 6c 74 20 73 65 74 2c 20 61 6e 64 20  result set, and 
2ba47 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 20 65 69  it is.**      ei
2ba48 74 68 65 72 20 6d 69 6e 28 78 29 20 6f 72 20 6d  ther min(x) or m
2ba49 61 78 28 78 29 2c 20 77 68 65 72 65 20 78 20 69  ax(x), where x i
2ba4a 73 20 61 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72  s a column refer
2ba4b 65 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ence..*/.static 
2ba4c 75 38 20 6d 69 6e 4d 61 78 51 75 65 72 79 28 53  u8 minMaxQuery(S
2ba4d 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 45 78 70  elect *p){.  Exp
2ba4e 72 20 2a 70 45 78 70 72 3b 0a 20 20 45 78 70 72  r *pExpr;.  Expr
2ba4f 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 3d 20 70  List *pEList = p
2ba50 2d 3e 70 45 4c 69 73 74 3b 0a 0a 20 20 69 66 28  ->pEList;..  if(
2ba51 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d   pEList->nExpr!=
2ba52 31 20 29 20 72 65 74 75 72 6e 20 57 48 45 52 45  1 ) return WHERE
2ba53 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b  _ORDERBY_NORMAL;
2ba54 0a 20 20 70 45 78 70 72 20 3d 20 70 45 4c 69 73  .  pExpr = pELis
2ba55 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20  t->a[0].pExpr;. 
2ba56 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d   if( pExpr->op!=
2ba57 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20  TK_AGG_FUNCTION 
2ba58 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66  ) return 0;.  if
2ba59 28 20 4e 45 56 45 52 28 45 78 70 72 48 61 73 50  ( NEVER(ExprHasP
2ba5a 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
2ba5b 50 5f 78 49 73 53 65 6c 65 63 74 29 29 20 29 20  P_xIsSelect)) ) 
2ba5c 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 45 4c 69  return 0;.  pELi
2ba5d 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c  st = pExpr->x.pL
2ba5e 69 73 74 3b 0a 20 20 69 66 28 20 70 45 4c 69 73  ist;.  if( pELis
2ba5f 74 3d 3d 30 20 7c 7c 20 70 45 4c 69 73 74 2d 3e  t==0 || pEList->
2ba60 6e 45 78 70 72 21 3d 31 20 29 20 72 65 74 75 72  nExpr!=1 ) retur
2ba61 6e 20 30 3b 0a 20 20 69 66 28 20 70 45 4c 69 73  n 0;.  if( pELis
2ba62 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f  t->a[0].pExpr->o
2ba63 70 21 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  p!=TK_AGG_COLUMN
2ba64 20 29 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f   ) return WHERE_
2ba65 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b 0a  ORDERBY_NORMAL;.
2ba66 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
2ba67 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
2ba68 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29  , EP_IntValue) )
2ba69 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ;.  if( sqlite3S
2ba6a 74 72 49 43 6d 70 28 70 45 78 70 72 2d 3e 75 2e  trICmp(pExpr->u.
2ba6b 7a 54 6f 6b 65 6e 2c 22 6d 69 6e 22 29 3d 3d 30  zToken,"min")==0
2ba6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57   ){.    return W
2ba6d 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e  HERE_ORDERBY_MIN
2ba6e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71  ;.  }else if( sq
2ba6f 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 45 78  lite3StrICmp(pEx
2ba70 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 22 6d 61  pr->u.zToken,"ma
2ba71 78 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  x")==0 ){.    re
2ba72 74 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52  turn WHERE_ORDER
2ba73 42 59 5f 4d 41 58 3b 0a 20 20 7d 0a 20 20 72 65  BY_MAX;.  }.  re
2ba74 74 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52  turn WHERE_ORDER
2ba75 42 59 5f 4e 4f 52 4d 41 4c 3b 0a 7d 0a 0a 2f 2a  BY_NORMAL;.}../*
2ba76 0a 2a 2a 20 54 68 65 20 73 65 6c 65 63 74 20 73  .** The select s
2ba77 74 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20  tatement passed 
2ba78 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  as the first arg
2ba79 75 6d 65 6e 74 20 69 73 20 61 6e 20 61 67 67 72  ument is an aggr
2ba7a 65 67 61 74 65 20 71 75 65 72 79 2e 0a 2a 2a 20  egate query..** 
2ba7b 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 6d 65  The second argme
2ba7c 6e 74 20 69 73 20 74 68 65 20 61 73 73 6f 63 69  nt is the associ
2ba7d 61 74 65 64 20 61 67 67 72 65 67 61 74 65 2d 69  ated aggregate-i
2ba7e 6e 66 6f 20 6f 62 6a 65 63 74 2e 20 54 68 69 73  nfo object. This
2ba7f 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 65   .** function te
2ba80 73 74 73 20 69 66 20 74 68 65 20 53 45 4c 45 43  sts if the SELEC
2ba81 54 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  T is of the form
2ba82 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54  :.**.**   SELECT
2ba83 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 3c   count(*) FROM <
2ba84 74 62 6c 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65  tbl>.**.** where
2ba85 20 74 61 62 6c 65 20 69 73 20 61 20 64 61 74 61   table is a data
2ba86 62 61 73 65 20 74 61 62 6c 65 2c 20 6e 6f 74 20  base table, not 
2ba87 61 20 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20  a sub-select or 
2ba88 76 69 65 77 2e 20 49 66 20 74 68 65 20 71 75 65  view. If the que
2ba89 72 79 0a 2a 2a 20 64 6f 65 73 20 6d 61 74 63 68  ry.** does match
2ba8a 20 74 68 69 73 20 70 61 74 74 65 72 6e 2c 20 74   this pattern, t
2ba8b 68 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  hen a pointer to
2ba8c 20 74 68 65 20 54 61 62 6c 65 20 6f 62 6a 65 63   the Table objec
2ba8d 74 20 72 65 70 72 65 73 65 6e 74 69 6e 67 0a 2a  t representing.*
2ba8e 2a 20 3c 74 62 6c 3e 20 69 73 20 72 65 74 75 72  * <tbl> is retur
2ba8f 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ned. Otherwise, 
2ba90 30 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  0 is returned..*
2ba91 2f 0a 73 74 61 74 69 63 20 54 61 62 6c 65 20 2a  /.static Table *
2ba92 69 73 53 69 6d 70 6c 65 43 6f 75 6e 74 28 53 65  isSimpleCount(Se
2ba93 6c 65 63 74 20 2a 70 2c 20 41 67 67 49 6e 66 6f  lect *p, AggInfo
2ba94 20 2a 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20 54   *pAggInfo){.  T
2ba95 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 45 78  able *pTab;.  Ex
2ba96 70 72 20 2a 70 45 78 70 72 3b 0a 0a 20 20 61 73  pr *pExpr;..  as
2ba97 73 65 72 74 28 20 21 70 2d 3e 70 47 72 6f 75 70  sert( !p->pGroup
2ba98 42 79 20 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e  By );..  if( p->
2ba99 70 57 68 65 72 65 20 7c 7c 20 70 2d 3e 70 45 4c  pWhere || p->pEL
2ba9a 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 0a 20  ist->nExpr!=1 . 
2ba9b 20 20 7c 7c 20 70 2d 3e 70 53 72 63 2d 3e 6e 53    || p->pSrc->nS
2ba9c 72 63 21 3d 31 20 7c 7c 20 70 2d 3e 70 53 72 63  rc!=1 || p->pSrc
2ba9d 2d 3e 61 5b 30 5d 2e 70 53 65 6c 65 63 74 0a 20  ->a[0].pSelect. 
2ba9e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2ba9f 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d 20 70  ;.  }.  pTab = p
2baa0 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61  ->pSrc->a[0].pTa
2baa1 62 3b 0a 20 20 70 45 78 70 72 20 3d 20 70 2d 3e  b;.  pExpr = p->
2baa2 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
2baa3 70 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54  pr;.  assert( pT
2baa4 61 62 20 26 26 20 21 70 54 61 62 2d 3e 70 53 65  ab && !pTab->pSe
2baa5 6c 65 63 74 20 26 26 20 70 45 78 70 72 20 29 3b  lect && pExpr );
2baa6 0a 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61  ..  if( IsVirtua
2baa7 6c 28 70 54 61 62 29 20 29 20 72 65 74 75 72 6e  l(pTab) ) return
2baa8 20 30 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d   0;.  if( pExpr-
2baa9 3e 6f 70 21 3d 54 4b 5f 41 47 47 5f 46 55 4e 43  >op!=TK_AGG_FUNC
2baaa 54 49 4f 4e 20 29 20 72 65 74 75 72 6e 20 30 3b  TION ) return 0;
2baab 0a 20 20 69 66 28 20 28 70 41 67 67 49 6e 66 6f  .  if( (pAggInfo
2baac 2d 3e 61 46 75 6e 63 5b 30 5d 2e 70 46 75 6e 63  ->aFunc[0].pFunc
2baad 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46  ->flags&SQLITE_F
2baae 55 4e 43 5f 43 4f 55 4e 54 29 3d 3d 30 20 29 20  UNC_COUNT)==0 ) 
2baaf 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
2bab0 70 45 78 70 72 2d 3e 66 6c 61 67 73 26 45 50 5f  pExpr->flags&EP_
2bab1 44 69 73 74 69 6e 63 74 20 29 20 72 65 74 75 72  Distinct ) retur
2bab2 6e 20 30 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70  n 0;..  return p
2bab3 54 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  Tab;.}../*.** If
2bab4 20 74 68 65 20 73 6f 75 72 63 65 2d 6c 69 73 74   the source-list
2bab5 20 69 74 65 6d 20 70 61 73 73 65 64 20 61 73 20   item passed as 
2bab6 61 6e 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20  an argument was 
2bab7 61 75 67 6d 65 6e 74 65 64 20 77 69 74 68 20 61  augmented with a
2bab8 6e 0a 2a 2a 20 49 4e 44 45 58 45 44 20 42 59 20  n.** INDEXED BY 
2bab9 63 6c 61 75 73 65 2c 20 74 68 65 6e 20 74 72 79  clause, then try
2baba 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73   to locate the s
2babb 70 65 63 69 66 69 65 64 20 69 6e 64 65 78 2e 20  pecified index. 
2babc 49 66 20 74 68 65 72 65 0a 2a 2a 20 77 61 73 20  If there.** was 
2babd 73 75 63 68 20 61 20 63 6c 61 75 73 65 20 61 6e  such a clause an
2babe 64 20 74 68 65 20 6e 61 6d 65 64 20 69 6e 64 65  d the named inde
2babf 78 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e  x cannot be foun
2bac0 64 2c 20 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51  d, return .** SQ
2bac1 4c 49 54 45 5f 45 52 52 4f 52 20 61 6e 64 20 6c  LITE_ERROR and l
2bac2 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 69 6e  eave an error in
2bac3 20 70 50 61 72 73 65 2e 20 4f 74 68 65 72 77 69   pParse. Otherwi
2bac4 73 65 2c 20 70 6f 70 75 6c 61 74 65 20 0a 2a 2a  se, populate .**
2bac5 20 70 46 72 6f 6d 2d 3e 70 49 6e 64 65 78 20 61   pFrom->pIndex a
2bac6 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  nd return SQLITE
2bac7 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  _OK..*/.SQLITE_P
2bac8 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2bac9 65 33 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75  e3IndexedByLooku
2baca 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  p(Parse *pParse,
2bacb 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
2bacc 69 74 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20  item *pFrom){.  
2bacd 69 66 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 20  if( pFrom->pTab 
2bace 26 26 20 70 46 72 6f 6d 2d 3e 7a 49 6e 64 65 78  && pFrom->zIndex
2bacf 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70   ){.    Table *p
2bad0 54 61 62 20 3d 20 70 46 72 6f 6d 2d 3e 70 54 61  Tab = pFrom->pTa
2bad1 62 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 49 6e  b;.    char *zIn
2bad2 64 65 78 20 3d 20 70 46 72 6f 6d 2d 3e 7a 49 6e  dex = pFrom->zIn
2bad3 64 65 78 3b 0a 20 20 20 20 49 6e 64 65 78 20 2a  dex;.    Index *
2bad4 70 49 64 78 3b 0a 20 20 20 20 66 6f 72 28 70 49  pIdx;.    for(pI
2bad5 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
2bad6 20 0a 20 20 20 20 20 20 20 20 70 49 64 78 20 26   .        pIdx &
2bad7 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  & sqlite3StrICmp
2bad8 28 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 7a 49  (pIdx->zName, zI
2bad9 6e 64 65 78 29 3b 20 0a 20 20 20 20 20 20 20 20  ndex); .        
2bada 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
2badb 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20  .    );.    if( 
2badc 21 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 73  !pIdx ){.      s
2badd 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2bade 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20  Parse, "no such 
2badf 69 6e 64 65 78 3a 20 25 73 22 2c 20 7a 49 6e 64  index: %s", zInd
2bae0 65 78 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 65  ex, 0);.      re
2bae1 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
2bae2 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 46 72  R;.    }.    pFr
2bae3 6f 6d 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 64  om->pIndex = pId
2bae4 78 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  x;.  }.  return 
2bae5 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
2bae6 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2bae7 20 69 73 20 61 20 57 61 6c 6b 65 72 20 63 61 6c   is a Walker cal
2bae8 6c 62 61 63 6b 20 66 6f 72 20 22 65 78 70 61 6e  lback for "expan
2bae9 64 69 6e 67 22 20 61 20 53 45 4c 45 43 54 20 73  ding" a SELECT s
2baea 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 22 45 78  tatement..** "Ex
2baeb 70 61 6e 64 69 6e 67 22 20 6d 65 61 6e 73 20 74  panding" means t
2baec 6f 20 64 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  o do the followi
2baed 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29  ng:.**.**    (1)
2baee 20 20 4d 61 6b 65 20 73 75 72 65 20 56 44 42 45    Make sure VDBE
2baef 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20   cursor numbers 
2baf0 68 61 76 65 20 62 65 65 6e 20 61 73 73 69 67 6e  have been assign
2baf1 65 64 20 74 6f 20 65 76 65 72 79 0a 2a 2a 20 20  ed to every.**  
2baf2 20 20 20 20 20 20 20 65 6c 65 6d 65 6e 74 20 6f         element o
2baf3 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  f the FROM claus
2baf4 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20  e..**.**    (2) 
2baf5 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 70 54 61   Fill in the pTa
2baf6 62 4c 69 73 74 2d 3e 61 5b 5d 2e 70 54 61 62 20  bList->a[].pTab 
2baf7 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 53 72  fields in the Sr
2baf8 63 4c 69 73 74 20 74 68 61 74 20 0a 2a 2a 20 20  cList that .**  
2baf9 20 20 20 20 20 20 20 64 65 66 69 6e 65 73 20 46         defines F
2bafa 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 57 68 65  ROM clause.  Whe
2bafb 6e 20 76 69 65 77 73 20 61 70 70 65 61 72 20 69  n views appear i
2bafc 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
2bafd 65 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 66 69  e,.**         fi
2bafe 6c 6c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d  ll pTabList->a[]
2baff 2e 70 53 65 6c 65 63 74 20 77 69 74 68 20 61 20  .pSelect with a 
2bb00 63 6f 70 79 20 6f 66 20 74 68 65 20 53 45 4c 45  copy of the SELE
2bb01 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  CT statement.** 
2bb02 20 20 20 20 20 20 20 20 74 68 61 74 20 69 6d 70          that imp
2bb03 6c 65 6d 65 6e 74 73 20 74 68 65 20 76 69 65 77  lements the view
2bb04 2e 20 20 41 20 63 6f 70 79 20 69 73 20 6d 61 64  .  A copy is mad
2bb05 65 20 6f 66 20 74 68 65 20 76 69 65 77 27 73 20  e of the view's 
2bb06 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20  SELECT.**       
2bb07 20 20 73 74 61 74 65 6d 65 6e 74 20 73 6f 20 74    statement so t
2bb08 68 61 74 20 77 65 20 63 61 6e 20 66 72 65 65 6c  hat we can freel
2bb09 79 20 6d 6f 64 69 66 79 20 6f 72 20 64 65 6c 65  y modify or dele
2bb0a 74 65 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e  te that statemen
2bb0b 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 77 69 74  t.**         wit
2bb0c 68 6f 75 74 20 77 6f 72 72 79 69 6e 67 20 61 62  hout worrying ab
2bb0d 6f 75 74 20 6d 65 73 73 69 6e 67 20 75 70 20 74  out messing up t
2bb0e 68 65 20 70 72 65 73 69 73 74 65 6e 74 20 72 65  he presistent re
2bb0f 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20  presentation.** 
2bb10 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 76          of the v
2bb11 69 65 77 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 33  iew..**.**    (3
2bb12 29 20 20 41 64 64 20 74 65 72 6d 73 20 74 6f 20  )  Add terms to 
2bb13 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2bb14 20 74 6f 20 61 63 63 6f 6d 6f 64 61 74 65 20 74   to accomodate t
2bb15 68 65 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f  he NATURAL keywo
2bb16 72 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 6e  rd.**         on
2bb17 20 6a 6f 69 6e 73 20 61 6e 64 20 74 68 65 20 4f   joins and the O
2bb18 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75  N and USING clau
2bb19 73 65 20 6f 66 20 6a 6f 69 6e 73 2e 0a 2a 2a 0a  se of joins..**.
2bb1a 2a 2a 20 20 20 20 28 34 29 20 20 53 63 61 6e 20  **    (4)  Scan 
2bb1b 74 68 65 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75  the list of colu
2bb1c 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c  mns in the resul
2bb1d 74 20 73 65 74 20 28 70 45 4c 69 73 74 29 20 6c  t set (pEList) l
2bb1e 6f 6f 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20  ooking.**       
2bb1f 20 20 66 6f 72 20 69 6e 73 74 61 6e 63 65 73 20    for instances 
2bb20 6f 66 20 74 68 65 20 22 2a 22 20 6f 70 65 72 61  of the "*" opera
2bb21 74 6f 72 20 6f 72 20 74 68 65 20 54 41 42 4c 45  tor or the TABLE
2bb22 2e 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20  .* operator..** 
2bb23 20 20 20 20 20 20 20 20 49 66 20 66 6f 75 6e 64          If found
2bb24 2c 20 65 78 70 61 6e 64 20 65 61 63 68 20 22 2a  , expand each "*
2bb25 22 20 74 6f 20 62 65 20 65 76 65 72 79 20 63 6f  " to be every co
2bb26 6c 75 6d 6e 20 69 6e 20 65 76 65 72 79 20 74 61  lumn in every ta
2bb27 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 61  ble.**         a
2bb28 6e 64 20 54 41 42 4c 45 2e 2a 20 74 6f 20 62 65  nd TABLE.* to be
2bb29 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20 69 6e   every column in
2bb2a 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2f 0a 73 74   TABLE..**.*/.st
2bb2b 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63 74 45  atic int selectE
2bb2c 78 70 61 6e 64 65 72 28 57 61 6c 6b 65 72 20 2a  xpander(Walker *
2bb2d 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20  pWalker, Select 
2bb2e 2a 70 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50  *p){.  Parse *pP
2bb2f 61 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e  arse = pWalker->
2bb30 70 50 61 72 73 65 3b 0a 20 20 69 6e 74 20 69 2c  pParse;.  int i,
2bb31 20 6a 2c 20 6b 3b 0a 20 20 53 72 63 4c 69 73 74   j, k;.  SrcList
2bb32 20 2a 70 54 61 62 4c 69 73 74 3b 0a 20 20 45 78   *pTabList;.  Ex
2bb33 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a  prList *pEList;.
2bb34 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
2bb35 5f 69 74 65 6d 20 2a 70 46 72 6f 6d 3b 0a 20 20  _item *pFrom;.  
2bb36 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2bb37 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28  arse->db;..  if(
2bb38 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2bb39 64 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  d  ){.    return
2bb3a 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a   WRC_Abort;.  }.
2bb3b 20 20 69 66 28 20 4e 45 56 45 52 28 70 2d 3e 70    if( NEVER(p->p
2bb3c 53 72 63 3d 3d 30 29 20 7c 7c 20 28 70 2d 3e 73  Src==0) || (p->s
2bb3d 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 45 78 70  elFlags & SF_Exp
2bb3e 61 6e 64 65 64 29 21 3d 30 20 29 7b 0a 20 20 20  anded)!=0 ){.   
2bb3f 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e   return WRC_Prun
2bb40 65 3b 0a 20 20 7d 0a 20 20 70 2d 3e 73 65 6c 46  e;.  }.  p->selF
2bb41 6c 61 67 73 20 7c 3d 20 53 46 5f 45 78 70 61 6e  lags |= SF_Expan
2bb42 64 65 64 3b 0a 20 20 70 54 61 62 4c 69 73 74 20  ded;.  pTabList 
2bb43 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 70 45 4c  = p->pSrc;.  pEL
2bb44 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b  ist = p->pEList;
2bb45 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
2bb46 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20   cursor numbers 
2bb47 68 61 76 65 20 62 65 65 6e 20 61 73 73 69 67 6e  have been assign
2bb48 65 64 20 74 6f 20 61 6c 6c 20 65 6e 74 72 69 65  ed to all entrie
2bb49 73 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20 46 52  s in.  ** the FR
2bb4a 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  OM clause of the
2bb4b 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2bb4c 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  t..  */.  sqlite
2bb4d 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75  3SrcListAssignCu
2bb4e 72 73 6f 72 73 28 70 50 61 72 73 65 2c 20 70 54  rsors(pParse, pT
2bb4f 61 62 4c 69 73 74 29 3b 0a 0a 20 20 2f 2a 20 4c  abList);..  /* L
2bb50 6f 6f 6b 20 75 70 20 65 76 65 72 79 20 74 61 62  ook up every tab
2bb51 6c 65 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20  le named in the 
2bb52 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74  FROM clause of t
2bb53 68 65 20 73 65 6c 65 63 74 2e 20 20 49 66 0a 20  he select.  If. 
2bb54 20 2a 2a 20 61 6e 20 65 6e 74 72 79 20 6f 66 20   ** an entry of 
2bb55 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2bb56 69 73 20 61 20 73 75 62 71 75 65 72 79 20 69 6e  is a subquery in
2bb57 73 74 65 61 64 20 6f 66 20 61 20 74 61 62 6c 65  stead of a table
2bb58 20 6f 72 20 76 69 65 77 2c 0a 20 20 2a 2a 20 74   or view,.  ** t
2bb59 68 65 6e 20 63 72 65 61 74 65 20 61 20 74 72 61  hen create a tra
2bb5a 6e 73 69 65 6e 74 20 74 61 62 6c 65 20 73 74 72  nsient table str
2bb5b 75 63 74 75 72 65 20 74 6f 20 64 65 73 63 72 69  ucture to descri
2bb5c 62 65 20 74 68 65 20 73 75 62 71 75 65 72 79 2e  be the subquery.
2bb5d 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c  .  */.  for(i=0,
2bb5e 20 70 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74 2d   pFrom=pTabList-
2bb5f 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e  >a; i<pTabList->
2bb60 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d  nSrc; i++, pFrom
2bb61 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a  ++){.    Table *
2bb62 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20 70 46  pTab;.    if( pF
2bb63 72 6f 6d 2d 3e 70 54 61 62 21 3d 30 20 29 7b 0a  rom->pTab!=0 ){.
2bb64 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 74        /* This st
2bb65 61 74 65 6d 65 6e 74 20 68 61 73 20 61 6c 72 65  atement has alre
2bb66 61 64 79 20 62 65 65 6e 20 70 72 65 70 61 72 65  ady been prepare
2bb67 64 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20  d.  There is no 
2bb68 6e 65 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f  need.      ** to
2bb69 20 67 6f 20 66 75 72 74 68 65 72 2e 20 2a 2f 0a   go further. */.
2bb6a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3d        assert( i=
2bb6b 3d 30 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75  =0 );.      retu
2bb6c 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20  rn WRC_Prune;.  
2bb6d 20 20 7d 0a 20 20 20 20 69 66 28 20 70 46 72 6f    }.    if( pFro
2bb6e 6d 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a 23  m->zName==0 ){.#
2bb6f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2bb70 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20  IT_SUBQUERY.    
2bb71 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 20 3d    Select *pSel =
2bb72 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 3b   pFrom->pSelect;
2bb73 0a 20 20 20 20 20 20 2f 2a 20 41 20 73 75 62 2d  .      /* A sub-
2bb74 71 75 65 72 79 20 69 6e 20 74 68 65 20 46 52 4f  query in the FRO
2bb75 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45  M clause of a SE
2bb76 4c 45 43 54 20 2a 2f 0a 20 20 20 20 20 20 61 73  LECT */.      as
2bb77 73 65 72 74 28 20 70 53 65 6c 21 3d 30 20 29 3b  sert( pSel!=0 );
2bb78 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2bb79 46 72 6f 6d 2d 3e 70 54 61 62 3d 3d 30 20 29 3b  From->pTab==0 );
2bb7a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 61  .      sqlite3Wa
2bb7b 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72  lkSelect(pWalker
2bb7c 2c 20 70 53 65 6c 29 3b 0a 20 20 20 20 20 20 70  , pSel);.      p
2bb7d 46 72 6f 6d 2d 3e 70 54 61 62 20 3d 20 70 54 61  From->pTab = pTa
2bb7e 62 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  b = sqlite3DbMal
2bb7f 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
2bb80 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20 20 20 20  of(Table));.    
2bb81 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20    if( pTab==0 ) 
2bb82 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
2bb83 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 64 62  ;.      pTab->db
2bb84 4d 65 6d 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73  Mem = db->lookas
2bb85 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3f 20 64  ide.bEnabled ? d
2bb86 62 20 3a 20 30 3b 0a 20 20 20 20 20 20 70 54 61  b : 0;.      pTa
2bb87 62 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20  b->nRef = 1;.   
2bb88 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20 3d     pTab->zName =
2bb89 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
2bb8a 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 75 62 71  db, "sqlite_subq
2bb8b 75 65 72 79 5f 25 70 5f 22 2c 20 28 76 6f 69 64  uery_%p_", (void
2bb8c 2a 29 70 54 61 62 29 3b 0a 20 20 20 20 20 20 77  *)pTab);.      w
2bb8d 68 69 6c 65 28 20 70 53 65 6c 2d 3e 70 50 72 69  hile( pSel->pPri
2bb8e 6f 72 20 29 7b 20 70 53 65 6c 20 3d 20 70 53 65  or ){ pSel = pSe
2bb8f 6c 2d 3e 70 50 72 69 6f 72 3b 20 7d 0a 20 20 20  l->pPrior; }.   
2bb90 20 20 20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 73     selectColumns
2bb91 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61  FromExprList(pPa
2bb92 72 73 65 2c 20 70 53 65 6c 2d 3e 70 45 4c 69 73  rse, pSel->pELis
2bb93 74 2c 20 26 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20  t, &pTab->nCol, 
2bb94 26 70 54 61 62 2d 3e 61 43 6f 6c 29 3b 0a 20 20  &pTab->aCol);.  
2bb95 20 20 20 20 70 54 61 62 2d 3e 69 50 4b 65 79 20      pTab->iPKey 
2bb96 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 54 61 62  = -1;.      pTab
2bb97 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46  ->tabFlags |= TF
2bb98 5f 45 70 68 65 6d 65 72 61 6c 3b 0a 23 65 6e 64  _Ephemeral;.#end
2bb99 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  if.    }else{.  
2bb9a 20 20 20 20 2f 2a 20 41 6e 20 6f 72 64 69 6e 61      /* An ordina
2bb9b 72 79 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77  ry table or view
2bb9c 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 46 52 4f   name in the FRO
2bb9d 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20  M clause */.    
2bb9e 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d    assert( pFrom-
2bb9f 3e 70 54 61 62 3d 3d 30 20 29 3b 0a 20 20 20 20  >pTab==0 );.    
2bba0 20 20 70 46 72 6f 6d 2d 3e 70 54 61 62 20 3d 20    pFrom->pTab = 
2bba1 70 54 61 62 20 3d 20 0a 20 20 20 20 20 20 20 20  pTab = .        
2bba2 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62  sqlite3LocateTab
2bba3 6c 65 28 70 50 61 72 73 65 2c 30 2c 70 46 72 6f  le(pParse,0,pFro
2bba4 6d 2d 3e 7a 4e 61 6d 65 2c 70 46 72 6f 6d 2d 3e  m->zName,pFrom->
2bba5 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20  zDatabase);.    
2bba6 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20    if( pTab==0 ) 
2bba7 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
2bba8 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 6e 52  ;.      pTab->nR
2bba9 65 66 2b 2b 3b 0a 23 69 66 20 21 64 65 66 69 6e  ef++;.#if !defin
2bbaa 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
2bbab 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64  IEW) || !defined
2bbac 20 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49   (SQLITE_OMIT_VI
2bbad 52 54 55 41 4c 54 41 42 4c 45 29 0a 20 20 20 20  RTUALTABLE).    
2bbae 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c    if( pTab->pSel
2bbaf 65 63 74 20 7c 7c 20 49 73 56 69 72 74 75 61 6c  ect || IsVirtual
2bbb0 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
2bbb1 20 20 2f 2a 20 57 65 20 72 65 61 63 68 20 68 65    /* We reach he
2bbb2 72 65 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20  re if the named 
2bbb3 74 61 62 6c 65 20 69 73 20 61 20 72 65 61 6c 6c  table is a reall
2bbb4 79 20 61 20 76 69 65 77 20 2a 2f 0a 20 20 20 20  y a view */.    
2bbb5 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56      if( sqlite3V
2bbb6 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65  iewGetColumnName
2bbb7 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 20  s(pParse, pTab) 
2bbb8 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
2bbb9 72 74 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  rt;.        asse
2bbba 72 74 28 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65  rt( pFrom->pSele
2bbbb 63 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  ct==0 );.       
2bbbc 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 20   pFrom->pSelect 
2bbbd 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  = sqlite3SelectD
2bbbe 75 70 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 65  up(db, pTab->pSe
2bbbf 6c 65 63 74 2c 20 30 29 3b 0a 20 20 20 20 20 20  lect, 0);.      
2bbc0 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c    sqlite3WalkSel
2bbc1 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 46 72  ect(pWalker, pFr
2bbc2 6f 6d 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20  om->pSelect);.  
2bbc3 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
2bbc4 20 7d 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 61 74   }..    /* Locat
2bbc5 65 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65  e the index name
2bbc6 64 20 62 79 20 74 68 65 20 49 4e 44 45 58 45 44  d by the INDEXED
2bbc7 20 42 59 20 63 6c 61 75 73 65 2c 20 69 66 20 61   BY clause, if a
2bbc8 6e 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 73  ny. */.    if( s
2bbc9 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79 4c  qlite3IndexedByL
2bbca 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 46  ookup(pParse, pF
2bbcb 72 6f 6d 29 20 29 7b 0a 20 20 20 20 20 20 72 65  rom) ){.      re
2bbcc 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
2bbcd 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
2bbce 50 72 6f 63 65 73 73 20 4e 41 54 55 52 41 4c 20  Process NATURAL 
2bbcf 6b 65 79 77 6f 72 64 73 2c 20 61 6e 64 20 4f 4e  keywords, and ON
2bbd0 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73   and USING claus
2bbd1 65 73 20 6f 66 20 6a 6f 69 6e 73 2e 0a 20 20 2a  es of joins..  *
2bbd2 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  /.  if( db->mall
2bbd3 6f 63 46 61 69 6c 65 64 20 7c 7c 20 73 71 6c 69  ocFailed || sqli
2bbd4 74 65 50 72 6f 63 65 73 73 4a 6f 69 6e 28 70 50  teProcessJoin(pP
2bbd5 61 72 73 65 2c 20 70 29 20 29 7b 0a 20 20 20 20  arse, p) ){.    
2bbd6 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
2bbd7 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20  ;.  }..  /* For 
2bbd8 65 76 65 72 79 20 22 2a 22 20 74 68 61 74 20 6f  every "*" that o
2bbd9 63 63 75 72 73 20 69 6e 20 74 68 65 20 63 6f 6c  ccurs in the col
2bbda 75 6d 6e 20 6c 69 73 74 2c 20 69 6e 73 65 72 74  umn list, insert
2bbdb 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 0a 20 20   the names of.  
2bbdc 2a 2a 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69  ** all columns i
2bbdd 6e 20 61 6c 6c 20 74 61 62 6c 65 73 2e 20 20 41  n all tables.  A
2bbde 6e 64 20 66 6f 72 20 65 76 65 72 79 20 54 41 42  nd for every TAB
2bbdf 4c 45 2e 2a 20 69 6e 73 65 72 74 20 74 68 65 20  LE.* insert the 
2bbe0 6e 61 6d 65 73 0a 20 20 2a 2a 20 6f 66 20 61 6c  names.  ** of al
2bbe1 6c 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 54 41 42  l columns in TAB
2bbe2 4c 45 2e 20 20 54 68 65 20 70 61 72 73 65 72 20  LE.  The parser 
2bbe3 69 6e 73 65 72 74 65 64 20 61 20 73 70 65 63 69  inserted a speci
2bbe4 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20  al expression.  
2bbe5 2a 2a 20 77 69 74 68 20 74 68 65 20 54 4b 5f 41  ** with the TK_A
2bbe6 4c 4c 20 6f 70 65 72 61 74 6f 72 20 66 6f 72 20  LL operator for 
2bbe7 65 61 63 68 20 22 2a 22 20 74 68 61 74 20 69 74  each "*" that it
2bbe8 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 63 6f   found in the co
2bbe9 6c 75 6d 6e 20 6c 69 73 74 2e 0a 20 20 2a 2a 20  lumn list..  ** 
2bbea 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  The following co
2bbeb 64 65 20 6a 75 73 74 20 68 61 73 20 74 6f 20 6c  de just has to l
2bbec 6f 63 61 74 65 20 74 68 65 20 54 4b 5f 41 4c 4c  ocate the TK_ALL
2bbed 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 6e 64   expressions and
2bbee 20 65 78 70 61 6e 64 0a 20 20 2a 2a 20 65 61 63   expand.  ** eac
2bbef 68 20 6f 6e 65 20 74 6f 20 74 68 65 20 6c 69 73  h one to the lis
2bbf0 74 20 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73  t of all columns
2bbf1 20 69 6e 20 61 6c 6c 20 74 61 62 6c 65 73 2e 0a   in all tables..
2bbf2 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69    **.  ** The fi
2bbf3 72 73 74 20 6c 6f 6f 70 20 6a 75 73 74 20 63 68  rst loop just ch
2bbf4 65 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 74  ecks to see if t
2bbf5 68 65 72 65 20 61 72 65 20 61 6e 79 20 22 2a 22  here are any "*"
2bbf6 20 6f 70 65 72 61 74 6f 72 73 0a 20 20 2a 2a 20   operators.  ** 
2bbf7 74 68 61 74 20 6e 65 65 64 20 65 78 70 61 6e 64  that need expand
2bbf8 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  ing..  */.  for(
2bbf9 6b 3d 30 3b 20 6b 3c 70 45 4c 69 73 74 2d 3e 6e  k=0; k<pEList->n
2bbfa 45 78 70 72 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20  Expr; k++){.    
2bbfb 45 78 70 72 20 2a 70 45 20 3d 20 70 45 4c 69 73  Expr *pE = pELis
2bbfc 74 2d 3e 61 5b 6b 5d 2e 70 45 78 70 72 3b 0a 20  t->a[k].pExpr;. 
2bbfd 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54     if( pE->op==T
2bbfe 4b 5f 41 4c 4c 20 29 20 62 72 65 61 6b 3b 0a 20  K_ALL ) break;. 
2bbff 20 20 20 61 73 73 65 72 74 28 20 70 45 2d 3e 6f     assert( pE->o
2bc00 70 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20 70 45 2d  p!=TK_DOT || pE-
2bc01 3e 70 52 69 67 68 74 21 3d 30 20 29 3b 0a 20 20  >pRight!=0 );.  
2bc02 20 20 61 73 73 65 72 74 28 20 70 45 2d 3e 6f 70    assert( pE->op
2bc03 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20 28 70 45 2d  !=TK_DOT || (pE-
2bc04 3e 70 4c 65 66 74 21 3d 30 20 26 26 20 70 45 2d  >pLeft!=0 && pE-
2bc05 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49  >pLeft->op==TK_I
2bc06 44 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 45  D) );.    if( pE
2bc07 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 26 26 20  ->op==TK_DOT && 
2bc08 70 45 2d 3e 70 52 69 67 68 74 2d 3e 6f 70 3d 3d  pE->pRight->op==
2bc09 54 4b 5f 41 4c 4c 20 29 20 62 72 65 61 6b 3b 0a  TK_ALL ) break;.
2bc0a 20 20 7d 0a 20 20 69 66 28 20 6b 3c 70 45 4c 69    }.  if( k<pELi
2bc0b 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20  st->nExpr ){.   
2bc0c 20 2f 2a 0a 20 20 20 20 2a 2a 20 49 66 20 77 65   /*.    ** If we
2bc0d 20 67 65 74 20 68 65 72 65 20 69 74 20 6d 65 61   get here it mea
2bc0e 6e 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  ns the result se
2bc0f 74 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f  t contains one o
2bc10 72 20 6d 6f 72 65 20 22 2a 22 0a 20 20 20 20 2a  r more "*".    *
2bc11 2a 20 6f 70 65 72 61 74 6f 72 73 20 74 68 61 74  * operators that
2bc12 20 6e 65 65 64 20 74 6f 20 62 65 20 65 78 70 61   need to be expa
2bc13 6e 64 65 64 2e 20 20 4c 6f 6f 70 20 74 68 72 6f  nded.  Loop thro
2bc14 75 67 68 20 65 61 63 68 20 65 78 70 72 65 73 73  ugh each express
2bc15 69 6f 6e 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68  ion.    ** in th
2bc16 65 20 72 65 73 75 6c 74 20 73 65 74 20 61 6e 64  e result set and
2bc17 20 65 78 70 61 6e 64 20 74 68 65 6d 20 6f 6e 65   expand them one
2bc18 20 62 79 20 6f 6e 65 2e 0a 20 20 20 20 2a 2f 0a   by one..    */.
2bc19 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
2bc1a 69 73 74 5f 69 74 65 6d 20 2a 61 20 3d 20 70 45  ist_item *a = pE
2bc1b 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 45 78 70  List->a;.    Exp
2bc1c 72 4c 69 73 74 20 2a 70 4e 65 77 20 3d 20 30 3b  rList *pNew = 0;
2bc1d 0a 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d  .    int flags =
2bc1e 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61   pParse->db->fla
2bc1f 67 73 3b 0a 20 20 20 20 69 6e 74 20 6c 6f 6e 67  gs;.    int long
2bc20 4e 61 6d 65 73 20 3d 20 28 66 6c 61 67 73 20 26  Names = (flags &
2bc21 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e   SQLITE_FullColN
2bc22 61 6d 65 73 29 21 3d 30 0a 20 20 20 20 20 20 20  ames)!=0.       
2bc23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
2bc24 26 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54  & (flags & SQLIT
2bc25 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 29  E_ShortColNames)
2bc26 3d 3d 30 3b 0a 0a 20 20 20 20 66 6f 72 28 6b 3d  ==0;..    for(k=
2bc27 30 3b 20 6b 3c 70 45 4c 69 73 74 2d 3e 6e 45 78  0; k<pEList->nEx
2bc28 70 72 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; k++){.      
2bc29 45 78 70 72 20 2a 70 45 20 3d 20 61 5b 6b 5d 2e  Expr *pE = a[k].
2bc2a 70 45 78 70 72 3b 0a 20 20 20 20 20 20 61 73 73  pExpr;.      ass
2bc2b 65 72 74 28 20 70 45 2d 3e 6f 70 21 3d 54 4b 5f  ert( pE->op!=TK_
2bc2c 44 4f 54 20 7c 7c 20 70 45 2d 3e 70 52 69 67 68  DOT || pE->pRigh
2bc2d 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66  t!=0 );.      if
2bc2e 28 20 70 45 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c  ( pE->op!=TK_ALL
2bc2f 20 26 26 20 28 70 45 2d 3e 6f 70 21 3d 54 4b 5f   && (pE->op!=TK_
2bc30 44 4f 54 20 7c 7c 20 70 45 2d 3e 70 52 69 67 68  DOT || pE->pRigh
2bc31 74 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 29 20 29  t->op!=TK_ALL) )
2bc32 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69  {.        /* Thi
2bc33 73 20 70 61 72 74 69 63 75 6c 61 72 20 65 78 70  s particular exp
2bc34 72 65 73 73 69 6f 6e 20 64 6f 65 73 20 6e 6f 74  ression does not
2bc35 20 6e 65 65 64 20 74 6f 20 62 65 20 65 78 70 61   need to be expa
2bc36 6e 64 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f  nded..        */
2bc37 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20  .        pNew = 
2bc38 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2bc39 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70 4e  ppend(pParse, pN
2bc3a 65 77 2c 20 61 5b 6b 5d 2e 70 45 78 70 72 29 3b  ew, a[k].pExpr);
2bc3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e 65  .        if( pNe
2bc3c 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  w ){.          p
2bc3d 4e 65 77 2d 3e 61 5b 70 4e 65 77 2d 3e 6e 45 78  New->a[pNew->nEx
2bc3e 70 72 2d 31 5d 2e 7a 4e 61 6d 65 20 3d 20 61 5b  pr-1].zName = a[
2bc3f 6b 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  k].zName;.      
2bc40 20 20 20 20 70 4e 65 77 2d 3e 61 5b 70 4e 65 77      pNew->a[pNew
2bc41 2d 3e 6e 45 78 70 72 2d 31 5d 2e 7a 53 70 61 6e  ->nExpr-1].zSpan
2bc42 20 3d 20 61 5b 6b 5d 2e 7a 53 70 61 6e 3b 0a 20   = a[k].zSpan;. 
2bc43 20 20 20 20 20 20 20 20 20 61 5b 6b 5d 2e 7a 4e           a[k].zN
2bc44 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ame = 0;.       
2bc45 20 20 20 61 5b 6b 5d 2e 7a 53 70 61 6e 20 3d 20     a[k].zSpan = 
2bc46 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
2bc47 20 20 20 20 20 61 5b 6b 5d 2e 70 45 78 70 72 20       a[k].pExpr 
2bc48 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 0;.      }else
2bc49 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69  {.        /* Thi
2bc4a 73 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  s expression is 
2bc4b 61 20 22 2a 22 20 6f 72 20 61 20 22 54 41 42 4c  a "*" or a "TABL
2bc4c 45 2e 2a 22 20 61 6e 64 20 6e 65 65 64 73 20 74  E.*" and needs t
2bc4d 6f 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  o be.        ** 
2bc4e 65 78 70 61 6e 64 65 64 2e 20 2a 2f 0a 20 20 20  expanded. */.   
2bc4f 20 20 20 20 20 69 6e 74 20 74 61 62 6c 65 53 65       int tableSe
2bc50 65 6e 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20  en = 0;      /* 
2bc51 53 65 74 20 74 6f 20 31 20 77 68 65 6e 20 54 41  Set to 1 when TA
2bc52 42 4c 45 20 6d 61 74 63 68 65 73 20 2a 2f 0a 20  BLE matches */. 
2bc53 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 4e         char *zTN
2bc54 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
2bc55 2f 2a 20 74 65 78 74 20 6f 66 20 6e 61 6d 65 20  /* text of name 
2bc56 6f 66 20 54 41 42 4c 45 20 2a 2f 0a 20 20 20 20  of TABLE */.    
2bc57 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d      if( pE->op==
2bc58 54 4b 5f 44 4f 54 20 29 7b 0a 20 20 20 20 20 20  TK_DOT ){.      
2bc59 20 20 20 20 61 73 73 65 72 74 28 20 70 45 2d 3e      assert( pE->
2bc5a 70 4c 65 66 74 21 3d 30 20 29 3b 0a 20 20 20 20  pLeft!=0 );.    
2bc5b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45        assert( !E
2bc5c 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
2bc5d 45 2d 3e 70 4c 65 66 74 2c 20 45 50 5f 49 6e 74  E->pLeft, EP_Int
2bc5e 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20  Value) );.      
2bc5f 20 20 20 20 7a 54 4e 61 6d 65 20 3d 20 70 45 2d      zTName = pE-
2bc60 3e 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e  >pLeft->u.zToken
2bc61 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
2bc62 0a 20 20 20 20 20 20 20 20 20 20 7a 54 4e 61 6d  .          zTNam
2bc63 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  e = 0;.        }
2bc64 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30  .        for(i=0
2bc65 2c 20 70 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74  , pFrom=pTabList
2bc66 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d  ->a; i<pTabList-
2bc67 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f  >nSrc; i++, pFro
2bc68 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  m++){.          
2bc69 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 46  Table *pTab = pF
2bc6a 72 6f 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20  rom->pTab;.     
2bc6b 20 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e       char *zTabN
2bc6c 61 6d 65 20 3d 20 70 46 72 6f 6d 2d 3e 7a 41 6c  ame = pFrom->zAl
2bc6d 69 61 73 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ias;.          i
2bc6e 66 28 20 7a 54 61 62 4e 61 6d 65 3d 3d 30 20 29  f( zTabName==0 )
2bc6f 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a 54  {.            zT
2bc70 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a  abName = pTab->z
2bc71 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20  Name;.          
2bc72 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  }.          if( 
2bc73 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2bc74 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
2bc75 20 20 20 20 69 66 28 20 7a 54 4e 61 6d 65 20 26      if( zTName &
2bc76 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  & sqlite3StrICmp
2bc77 28 7a 54 4e 61 6d 65 2c 20 7a 54 61 62 4e 61 6d  (zTName, zTabNam
2bc78 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  e)!=0 ){.       
2bc79 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
2bc7a 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2bc7b 20 20 20 20 20 74 61 62 6c 65 53 65 65 6e 20 3d       tableSeen =
2bc7c 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f   1;.          fo
2bc7d 72 28 6a 3d 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e  r(j=0; j<pTab->n
2bc7e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  Col; j++){.     
2bc7f 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78         Expr *pEx
2bc80 70 72 2c 20 2a 70 52 69 67 68 74 3b 0a 20 20 20  pr, *pRight;.   
2bc81 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
2bc82 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f  Name = pTab->aCo
2bc83 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  l[j].zName;.    
2bc84 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43          char *zC
2bc85 6f 6c 6e 61 6d 65 3b 20 20 2f 2a 20 54 68 65 20  olname;  /* The 
2bc86 63 6f 6d 70 75 74 65 64 20 63 6f 6c 75 6d 6e 20  computed column 
2bc87 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  name */.        
2bc88 20 20 20 20 63 68 61 72 20 2a 7a 54 6f 46 72 65      char *zToFre
2bc89 65 3b 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 65 64  e;   /* Malloced
2bc8a 20 73 74 72 69 6e 67 20 74 68 61 74 20 6e 65 65   string that nee
2bc8b 64 73 20 74 6f 20 62 65 20 66 72 65 65 64 20 2a  ds to be freed *
2bc8c 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 54 6f  /.            To
2bc8d 6b 65 6e 20 73 43 6f 6c 6e 61 6d 65 3b 20 20 2f  ken sColname;  /
2bc8e 2a 20 43 6f 6d 70 75 74 65 64 20 63 6f 6c 75 6d  * Computed colum
2bc8f 6e 20 6e 61 6d 65 20 61 73 20 61 20 74 6f 6b 65  n name as a toke
2bc90 6e 20 2a 2f 0a 0a 20 20 20 20 20 20 20 20 20 20  n */..          
2bc91 20 20 2f 2a 20 49 66 20 61 20 63 6f 6c 75 6d 6e    /* If a column
2bc92 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 27 68   is marked as 'h
2bc93 69 64 64 65 6e 27 20 28 63 75 72 72 65 6e 74 6c  idden' (currentl
2bc94 79 20 6f 6e 6c 79 20 70 6f 73 73 69 62 6c 65 0a  y only possible.
2bc95 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66              ** f
2bc96 6f 72 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  or virtual table
2bc97 73 29 2c 20 64 6f 20 6e 6f 74 20 69 6e 63 6c 75  s), do not inclu
2bc98 64 65 20 69 74 20 69 6e 20 74 68 65 20 65 78 70  de it in the exp
2bc99 61 6e 64 65 64 0a 20 20 20 20 20 20 20 20 20 20  anded.          
2bc9a 20 20 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74 20    ** result-set 
2bc9b 6c 69 73 74 2e 0a 20 20 20 20 20 20 20 20 20 20  list..          
2bc9c 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20    */.           
2bc9d 20 69 66 28 20 49 73 48 69 64 64 65 6e 43 6f 6c   if( IsHiddenCol
2bc9e 75 6d 6e 28 26 70 54 61 62 2d 3e 61 43 6f 6c 5b  umn(&pTab->aCol[
2bc9f 6a 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  j]) ){.         
2bca0 20 20 20 20 20 61 73 73 65 72 74 28 49 73 56 69       assert(IsVi
2bca1 72 74 75 61 6c 28 70 54 61 62 29 29 3b 0a 20 20  rtual(pTab));.  
2bca2 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74              cont
2bca3 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20  inue;.          
2bca4 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 20    }..           
2bca5 20 69 66 28 20 69 3e 30 20 26 26 20 7a 54 4e 61   if( i>0 && zTNa
2bca6 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  me==0 ){.       
2bca7 20 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72         struct Sr
2bca8 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c 65 66  cList_item *pLef
2bca9 74 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61  t = &pTabList->a
2bcaa 5b 69 2d 31 5d 3b 0a 20 20 20 20 20 20 20 20 20  [i-1];.         
2bcab 20 20 20 20 20 69 66 28 20 28 70 4c 65 66 74 5b       if( (pLeft[
2bcac 31 5d 2e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54  1].jointype & JT
2bcad 5f 4e 41 54 55 52 41 4c 29 21 3d 30 20 26 26 0a  _NATURAL)!=0 &&.
2bcae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bcaf 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 49 6e          columnIn
2bcb0 64 65 78 28 70 4c 65 66 74 2d 3e 70 54 61 62 2c  dex(pLeft->pTab,
2bcb1 20 7a 4e 61 6d 65 29 3e 3d 30 20 29 7b 0a 20 20   zName)>=0 ){.  
2bcb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2bcb3 20 49 6e 20 61 20 4e 41 54 55 52 41 4c 20 6a 6f   In a NATURAL jo
2bcb4 69 6e 2c 20 6f 6d 69 74 20 74 68 65 20 6a 6f 69  in, omit the joi
2bcb5 6e 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 74  n columns from t
2bcb6 68 65 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  he .            
2bcb7 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 6f 6e 20      ** table on 
2bcb8 74 68 65 20 72 69 67 68 74 20 2a 2f 0a 20 20 20  the right */.   
2bcb9 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
2bcba 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20  tinue;.         
2bcbb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
2bcbc 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
2bcbd 49 64 4c 69 73 74 49 6e 64 65 78 28 70 4c 65 66  IdListIndex(pLef
2bcbe 74 5b 31 5d 2e 70 55 73 69 6e 67 2c 20 7a 4e 61  t[1].pUsing, zNa
2bcbf 6d 65 29 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  me)>=0 ){.      
2bcc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 20            /* In 
2bcc1 61 20 6a 6f 69 6e 20 77 69 74 68 20 61 20 55 53  a join with a US
2bcc2 49 4e 47 20 63 6c 61 75 73 65 2c 20 6f 6d 69 74  ING clause, omit
2bcc3 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 0a   columns in the.
2bcc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bcc5 2a 2a 20 75 73 69 6e 67 20 63 6c 61 75 73 65 20  ** using clause 
2bcc6 66 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 20 6f  from the table o
2bcc7 6e 20 74 68 65 20 72 69 67 68 74 2e 20 2a 2f 0a  n the right. */.
2bcc8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bcc9 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
2bcca 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2bccb 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2bccc 20 20 20 20 70 52 69 67 68 74 20 3d 20 73 71 6c      pRight = sql
2bccd 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f  ite3Expr(db, TK_
2bcce 49 44 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ID, zName);.    
2bccf 20 20 20 20 20 20 20 20 7a 43 6f 6c 6e 61 6d 65          zColname
2bcd0 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20   = zName;.      
2bcd1 20 20 20 20 20 20 7a 54 6f 46 72 65 65 20 3d 20        zToFree = 
2bcd2 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  0;.            i
2bcd3 66 28 20 6c 6f 6e 67 4e 61 6d 65 73 20 7c 7c 20  f( longNames || 
2bcd4 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3e 31  pTabList->nSrc>1
2bcd5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2bcd6 20 20 45 78 70 72 20 2a 70 4c 65 66 74 3b 0a 20    Expr *pLeft;. 
2bcd7 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4c 65               pLe
2bcd8 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ft = sqlite3Expr
2bcd9 28 64 62 2c 20 54 4b 5f 49 44 2c 20 7a 54 61 62  (db, TK_ID, zTab
2bcda 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Name);.         
2bcdb 20 20 20 20 20 70 45 78 70 72 20 3d 20 73 71 6c       pExpr = sql
2bcdc 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2bcdd 2c 20 54 4b 5f 44 4f 54 2c 20 70 4c 65 66 74 2c  , TK_DOT, pLeft,
2bcde 20 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20 20   pRight, 0);.   
2bcdf 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6c             if( l
2bce0 6f 6e 67 4e 61 6d 65 73 20 29 7b 0a 20 20 20 20  ongNames ){.    
2bce1 20 20 20 20 20 20 20 20 20 20 20 20 7a 43 6f 6c              zCol
2bce2 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50  name = sqlite3MP
2bce3 72 69 6e 74 66 28 64 62 2c 20 22 25 73 2e 25 73  rintf(db, "%s.%s
2bce4 22 2c 20 7a 54 61 62 4e 61 6d 65 2c 20 7a 4e 61  ", zTabName, zNa
2bce5 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  me);.           
2bce6 20 20 20 20 20 7a 54 6f 46 72 65 65 20 3d 20 7a       zToFree = z
2bce7 43 6f 6c 6e 61 6d 65 3b 0a 20 20 20 20 20 20 20  Colname;.       
2bce8 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2bce9 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2bcea 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 20            pExpr 
2bceb 3d 20 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20  = pRight;.      
2bcec 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2bced 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74      pNew = sqlit
2bcee 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
2bcef 28 70 50 61 72 73 65 2c 20 70 4e 65 77 2c 20 70  (pParse, pNew, p
2bcf0 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 20  Expr);.         
2bcf1 20 20 20 73 43 6f 6c 6e 61 6d 65 2e 7a 20 3d 20     sColname.z = 
2bcf2 7a 43 6f 6c 6e 61 6d 65 3b 0a 20 20 20 20 20 20  zColname;.      
2bcf3 20 20 20 20 20 20 73 43 6f 6c 6e 61 6d 65 2e 6e        sColname.n
2bcf4 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
2bcf5 33 30 28 7a 43 6f 6c 6e 61 6d 65 29 3b 0a 20 20  30(zColname);.  
2bcf6 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2bcf7 33 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65  3ExprListSetName
2bcf8 28 70 50 61 72 73 65 2c 20 70 4e 65 77 2c 20 26  (pParse, pNew, &
2bcf9 73 43 6f 6c 6e 61 6d 65 2c 20 30 29 3b 0a 20 20  sColname, 0);.  
2bcfa 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2bcfb 33 44 62 46 72 65 65 28 64 62 2c 20 7a 54 6f 46  3DbFree(db, zToF
2bcfc 72 65 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ree);.          
2bcfd 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
2bcfe 20 20 20 20 69 66 28 20 21 74 61 62 6c 65 53 65      if( !tableSe
2bcff 65 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  en ){.          
2bd00 69 66 28 20 7a 54 4e 61 6d 65 20 29 7b 0a 20 20  if( zTName ){.  
2bd01 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2bd02 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2bd03 2c 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65  , "no such table
2bd04 3a 20 25 73 22 2c 20 7a 54 4e 61 6d 65 29 3b 0a  : %s", zTName);.
2bd05 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
2bd06 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
2bd07 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2bd08 72 73 65 2c 20 22 6e 6f 20 74 61 62 6c 65 73 20  rse, "no tables 
2bd09 73 70 65 63 69 66 69 65 64 22 29 3b 0a 20 20 20  specified");.   
2bd0a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2bd0b 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
2bd0c 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2bd0d 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2bd0e 45 4c 69 73 74 29 3b 0a 20 20 20 20 70 2d 3e 70  EList);.    p->p
2bd0f 45 4c 69 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20  EList = pNew;.  
2bd10 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  }.#if SQLITE_MAX
2bd11 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 2d  _COLUMN.  if( p-
2bd12 3e 70 45 4c 69 73 74 20 26 26 20 70 2d 3e 70 45  >pEList && p->pE
2bd13 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e  List->nExpr>db->
2bd14 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
2bd15 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20  MIT_COLUMN] ){. 
2bd16 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2bd17 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20  sg(pParse, "too 
2bd18 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  many columns in 
2bd19 72 65 73 75 6c 74 20 73 65 74 22 29 3b 0a 20 20  result set");.  
2bd1a 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72  }.#endif.  retur
2bd1b 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a  n WRC_Continue;.
2bd1c 7d 0a 0a 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20 72  }../*.** No-op r
2bd1d 6f 75 74 69 6e 65 20 66 6f 72 20 74 68 65 20 70  outine for the p
2bd1e 61 72 73 65 2d 74 72 65 65 20 77 61 6c 6b 65 72  arse-tree walker
2bd1f 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69  ..**.** When thi
2bd20 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65  s routine is the
2bd21 20 57 61 6c 6b 65 72 2e 78 45 78 70 72 43 61 6c   Walker.xExprCal
2bd22 6c 62 61 63 6b 20 74 68 65 6e 20 65 78 70 72 65  lback then expre
2bd23 73 73 69 6f 6e 20 74 72 65 65 73 0a 2a 2a 20 61  ssion trees.** a
2bd24 72 65 20 77 61 6c 6b 65 64 20 77 69 74 68 6f 75  re walked withou
2bd25 74 20 61 6e 79 20 61 63 74 69 6f 6e 73 20 62 65  t any actions be
2bd26 69 6e 67 20 74 61 6b 65 6e 20 61 74 20 65 61 63  ing taken at eac
2bd27 68 20 6e 6f 64 65 2e 20 20 50 72 65 73 75 6d 61  h node.  Presuma
2bd28 62 6c 79 2c 0a 2a 2a 20 77 68 65 6e 20 74 68 69  bly,.** when thi
2bd29 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
2bd2a 64 20 66 6f 72 20 57 61 6c 6b 65 72 2e 78 45 78  d for Walker.xEx
2bd2b 70 72 43 61 6c 6c 62 61 63 6b 20 74 68 65 6e 20  prCallback then 
2bd2c 0a 2a 2a 20 57 61 6c 6b 65 72 2e 78 53 65 6c 65  .** Walker.xSele
2bd2d 63 74 43 61 6c 6c 62 61 63 6b 20 69 73 20 73 65  ctCallback is se
2bd2e 74 20 74 6f 20 64 6f 20 73 6f 6d 65 74 68 69 6e  t to do somethin
2bd2f 67 20 75 73 65 66 75 6c 20 66 6f 72 20 65 76 65  g useful for eve
2bd30 72 79 20 0a 2a 2a 20 73 75 62 71 75 65 72 79 20  ry .** subquery 
2bd31 69 6e 20 74 68 65 20 70 61 72 73 65 72 20 74 72  in the parser tr
2bd32 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ee..*/.static in
2bd33 74 20 65 78 70 72 57 61 6c 6b 4e 6f 6f 70 28 57  t exprWalkNoop(W
2bd34 61 6c 6b 65 72 20 2a 4e 6f 74 55 73 65 64 2c 20  alker *NotUsed, 
2bd35 45 78 70 72 20 2a 4e 6f 74 55 73 65 64 32 29 7b  Expr *NotUsed2){
2bd36 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
2bd37 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
2bd38 74 55 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72  tUsed2);.  retur
2bd39 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a  n WRC_Continue;.
2bd3a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
2bd3b 75 74 69 6e 65 20 22 65 78 70 61 6e 64 73 22 20  utine "expands" 
2bd3c 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
2bd3d 6e 74 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74  nt and all of it
2bd3e 73 20 73 75 62 71 75 65 72 69 65 73 2e 0a 2a 2a  s subqueries..**
2bd3f 20 46 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   For additional 
2bd40 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 77  information on w
2bd41 68 61 74 20 69 74 20 6d 65 61 6e 73 20 74 6f 20  hat it means to 
2bd42 22 65 78 70 61 6e 64 22 20 61 20 53 45 4c 45 43  "expand" a SELEC
2bd43 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2c 20  T.** statement, 
2bd44 73 65 65 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20  see the comment 
2bd45 6f 6e 20 74 68 65 20 73 65 6c 65 63 74 45 78 70  on the selectExp
2bd46 61 6e 64 20 77 6f 72 6b 65 72 20 63 61 6c 6c 62  and worker callb
2bd47 61 63 6b 20 61 62 6f 76 65 2e 0a 2a 2a 0a 2a 2a  ack above..**.**
2bd48 20 45 78 70 61 6e 64 69 6e 67 20 61 20 53 45 4c   Expanding a SEL
2bd49 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73  ECT statement is
2bd4a 20 74 68 65 20 66 69 72 73 74 20 73 74 65 70 20   the first step 
2bd4b 69 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 0a  in processing a.
2bd4c 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  ** SELECT statem
2bd4d 65 6e 74 2e 20 20 54 68 65 20 53 45 4c 45 43 54  ent.  The SELECT
2bd4e 20 73 74 61 74 65 6d 65 6e 74 20 6d 75 73 74 20   statement must 
2bd4f 62 65 20 65 78 70 61 6e 64 65 64 20 62 65 66 6f  be expanded befo
2bd50 72 65 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f 6c  re.** name resol
2bd51 75 74 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d  ution is perform
2bd52 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79  ed..**.** If any
2bd53 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
2bd54 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  , an error messa
2bd55 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  ge is written in
2bd56 74 6f 20 70 50 61 72 73 65 2e 0a 2a 2a 20 54 68  to pParse..** Th
2bd57 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
2bd58 6f 6e 20 63 61 6e 20 64 65 74 65 63 74 20 74 68  on can detect th
2bd59 65 20 70 72 6f 62 6c 65 6d 20 62 79 20 6c 6f 6f  e problem by loo
2bd5a 6b 69 6e 67 20 61 74 20 70 50 61 72 73 65 2d 3e  king at pParse->
2bd5b 6e 45 72 72 0a 2a 2a 20 61 6e 64 2f 6f 72 20 70  nErr.** and/or p
2bd5c 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
2bd5d 63 46 61 69 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74  cFailed..*/.stat
2bd5e 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  ic void sqlite3S
2bd5f 65 6c 65 63 74 45 78 70 61 6e 64 28 50 61 72 73  electExpand(Pars
2bd60 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63  e *pParse, Selec
2bd61 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20 57  t *pSelect){.  W
2bd62 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 78 53 65  alker w;.  w.xSe
2bd63 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 73  lectCallback = s
2bd64 65 6c 65 63 74 45 78 70 61 6e 64 65 72 3b 0a 20  electExpander;. 
2bd65 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b   w.xExprCallback
2bd66 20 3d 20 65 78 70 72 57 61 6c 6b 4e 6f 6f 70 3b   = exprWalkNoop;
2bd67 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50  .  w.pParse = pP
2bd68 61 72 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 57  arse;.  sqlite3W
2bd69 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c 20 70 53  alkSelect(&w, pS
2bd6a 65 6c 65 63 74 29 3b 0a 7d 0a 0a 0a 23 69 66 6e  elect);.}...#ifn
2bd6b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2bd6c 53 55 42 51 55 45 52 59 0a 2f 2a 0a 2a 2a 20 54  SUBQUERY./*.** T
2bd6d 68 69 73 20 69 73 20 61 20 57 61 6c 6b 65 72 2e  his is a Walker.
2bd6e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20  xSelectCallback 
2bd6f 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 74 68 65  callback for the
2bd70 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 54 79   sqlite3SelectTy
2bd71 70 65 49 6e 66 6f 28 29 0a 2a 2a 20 69 6e 74 65  peInfo().** inte
2bd72 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  rface..**.** For
2bd73 20 65 61 63 68 20 46 52 4f 4d 2d 63 6c 61 75 73   each FROM-claus
2bd74 65 20 73 75 62 71 75 65 72 79 2c 20 61 64 64 20  e subquery, add 
2bd75 43 6f 6c 75 6d 6e 2e 7a 54 79 70 65 20 61 6e 64  Column.zType and
2bd76 20 43 6f 6c 75 6d 6e 2e 7a 43 6f 6c 6c 0a 2a 2a   Column.zColl.**
2bd77 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20   information to 
2bd78 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  the Table struct
2bd79 75 72 65 20 74 68 61 74 20 72 65 70 72 65 73 65  ure that represe
2bd7a 6e 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 73  nts the result s
2bd7b 65 74 0a 2a 2a 20 6f 66 20 74 68 61 74 20 73 75  et.** of that su
2bd7c 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  bquery..**.** Th
2bd7d 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
2bd7e 65 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74  e that represent
2bd7f 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  s the result set
2bd80 20 77 61 73 20 63 6f 6e 73 74 72 75 63 74 65 64   was constructed
2bd81 0a 2a 2a 20 62 79 20 73 65 6c 65 63 74 45 78 70  .** by selectExp
2bd82 61 6e 64 65 72 28 29 20 62 75 74 20 74 68 65 20  ander() but the 
2bd83 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61 74 69  type and collati
2bd84 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77  on information w
2bd85 61 73 20 6f 6d 69 74 74 65 64 0a 2a 2a 20 61 74  as omitted.** at
2bd86 20 74 68 61 74 20 70 6f 69 6e 74 20 62 65 63 61   that point beca
2bd87 75 73 65 20 69 64 65 6e 74 69 66 69 65 72 73 20  use identifiers 
2bd88 68 61 64 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  had not yet been
2bd89 20 72 65 73 6f 6c 76 65 64 2e 20 20 54 68 69 73   resolved.  This
2bd8a 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 63  .** routine is c
2bd8b 61 6c 6c 65 64 20 61 66 74 65 72 20 69 64 65 6e  alled after iden
2bd8c 74 69 66 69 65 72 20 72 65 73 6f 6c 75 74 69 6f  tifier resolutio
2bd8d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
2bd8e 20 73 65 6c 65 63 74 41 64 64 53 75 62 71 75 65   selectAddSubque
2bd8f 72 79 54 79 70 65 49 6e 66 6f 28 57 61 6c 6b 65  ryTypeInfo(Walke
2bd90 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65  r *pWalker, Sele
2bd91 63 74 20 2a 70 29 7b 0a 20 20 50 61 72 73 65 20  ct *p){.  Parse 
2bd92 2a 70 50 61 72 73 65 3b 0a 20 20 69 6e 74 20 69  *pParse;.  int i
2bd93 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61  ;.  SrcList *pTa
2bd94 62 4c 69 73 74 3b 0a 20 20 73 74 72 75 63 74 20  bList;.  struct 
2bd95 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 46  SrcList_item *pF
2bd96 72 6f 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  rom;..  assert( 
2bd97 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  p->selFlags & SF
2bd98 5f 52 65 73 6f 6c 76 65 64 20 29 3b 0a 20 20 61  _Resolved );.  a
2bd99 73 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c  ssert( (p->selFl
2bd9a 61 67 73 20 26 20 53 46 5f 48 61 73 54 79 70 65  ags & SF_HasType
2bd9b 49 6e 66 6f 29 3d 3d 30 20 29 3b 0a 20 20 70 2d  Info)==0 );.  p-
2bd9c 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f  >selFlags |= SF_
2bd9d 48 61 73 54 79 70 65 49 6e 66 6f 3b 0a 20 20 70  HasTypeInfo;.  p
2bd9e 50 61 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d  Parse = pWalker-
2bd9f 3e 70 50 61 72 73 65 3b 0a 20 20 70 54 61 62 4c  >pParse;.  pTabL
2bda0 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20  ist = p->pSrc;. 
2bda1 20 66 6f 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d   for(i=0, pFrom=
2bda2 70 54 61 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70  pTabList->a; i<p
2bda3 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  TabList->nSrc; i
2bda4 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20 20  ++, pFrom++){.  
2bda5 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
2bda6 70 46 72 6f 6d 2d 3e 70 54 61 62 3b 0a 20 20 20  pFrom->pTab;.   
2bda7 20 69 66 28 20 41 4c 57 41 59 53 28 70 54 61 62   if( ALWAYS(pTab
2bda8 21 3d 30 29 20 26 26 20 28 70 54 61 62 2d 3e 74  !=0) && (pTab->t
2bda9 61 62 46 6c 61 67 73 20 26 20 54 46 5f 45 70 68  abFlags & TF_Eph
2bdaa 65 6d 65 72 61 6c 29 21 3d 30 20 29 7b 0a 20 20  emeral)!=0 ){.  
2bdab 20 20 20 20 2f 2a 20 41 20 73 75 62 2d 71 75 65      /* A sub-que
2bdac 72 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  ry in the FROM c
2bdad 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43  lause of a SELEC
2bdae 54 20 2a 2f 0a 20 20 20 20 20 20 53 65 6c 65 63  T */.      Selec
2bdaf 74 20 2a 70 53 65 6c 20 3d 20 70 46 72 6f 6d 2d  t *pSel = pFrom-
2bdb0 3e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 20  >pSelect;.      
2bdb1 61 73 73 65 72 74 28 20 70 53 65 6c 20 29 3b 0a  assert( pSel );.
2bdb2 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 65        while( pSe
2bdb3 6c 2d 3e 70 50 72 69 6f 72 20 29 20 70 53 65 6c  l->pPrior ) pSel
2bdb4 20 3d 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72 3b   = pSel->pPrior;
2bdb5 0a 20 20 20 20 20 20 73 65 6c 65 63 74 41 64 64  .      selectAdd
2bdb6 43 6f 6c 75 6d 6e 54 79 70 65 41 6e 64 43 6f 6c  ColumnTypeAndCol
2bdb7 6c 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70  lation(pParse, p
2bdb8 54 61 62 2d 3e 6e 43 6f 6c 2c 20 70 54 61 62 2d  Tab->nCol, pTab-
2bdb9 3e 61 43 6f 6c 2c 20 70 53 65 6c 29 3b 0a 20 20  >aCol, pSel);.  
2bdba 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
2bdbb 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d   WRC_Continue;.}
2bdbc 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
2bdbd 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 64 64  This routine add
2bdbe 73 20 64 61 74 61 74 79 70 65 20 61 6e 64 20 63  s datatype and c
2bdbf 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
2bdc0 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f  e information to
2bdc1 0a 2a 2a 20 74 68 65 20 54 61 62 6c 65 20 73 74  .** the Table st
2bdc2 72 75 63 74 75 72 65 73 20 6f 66 20 61 6c 6c 20  ructures of all 
2bdc3 46 52 4f 4d 2d 63 6c 61 75 73 65 20 73 75 62 71  FROM-clause subq
2bdc4 75 65 72 69 65 73 20 69 6e 20 61 0a 2a 2a 20 53  ueries in a.** S
2bdc5 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
2bdc6 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 69 73 20  .**.** Use this 
2bdc7 72 6f 75 74 69 6e 65 20 61 66 74 65 72 20 6e 61  routine after na
2bdc8 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 2e 0a 2a  me resolution..*
2bdc9 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
2bdca 6c 69 74 65 33 53 65 6c 65 63 74 41 64 64 54 79  lite3SelectAddTy
2bdcb 70 65 49 6e 66 6f 28 50 61 72 73 65 20 2a 70 50  peInfo(Parse *pP
2bdcc 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 53  arse, Select *pS
2bdcd 65 6c 65 63 74 29 7b 0a 23 69 66 6e 64 65 66 20  elect){.#ifndef 
2bdce 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
2bdcf 55 45 52 59 0a 20 20 57 61 6c 6b 65 72 20 77 3b  UERY.  Walker w;
2bdd0 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c  .  w.xSelectCall
2bdd1 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 41 64 64  back = selectAdd
2bdd2 53 75 62 71 75 65 72 79 54 79 70 65 49 6e 66 6f  SubqueryTypeInfo
2bdd3 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62  ;.  w.xExprCallb
2bdd4 61 63 6b 20 3d 20 65 78 70 72 57 61 6c 6b 4e 6f  ack = exprWalkNo
2bdd5 6f 70 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d  op;.  w.pParse =
2bdd6 20 70 50 61 72 73 65 3b 0a 20 20 73 71 6c 69 74   pParse;.  sqlit
2bdd7 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c  e3WalkSelect(&w,
2bdd8 20 70 53 65 6c 65 63 74 29 3b 0a 23 65 6e 64 69   pSelect);.#endi
2bdd9 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  f.}.../*.** This
2bdda 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 6f 66   routine sets of
2bddb 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
2bddc 65 6e 74 20 66 6f 72 20 70 72 6f 63 65 73 73 69  ent for processi
2bddd 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 66 6f 6c 6c  ng.  The.** foll
2bdde 6f 77 69 6e 67 20 69 73 20 61 63 63 6f 6d 70 6c  owing is accompl
2bddf 69 73 68 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ished:.**.**    
2bde0 20 2a 20 20 56 44 42 45 20 43 75 72 73 6f 72 20   *  VDBE Cursor 
2bde1 6e 75 6d 62 65 72 73 20 61 72 65 20 61 73 73 69  numbers are assi
2bde2 67 6e 65 64 20 74 6f 20 61 6c 6c 20 46 52 4f 4d  gned to all FROM
2bde3 2d 63 6c 61 75 73 65 20 74 65 72 6d 73 2e 0a 2a  -clause terms..*
2bde4 2a 20 20 20 20 20 2a 20 20 45 70 68 65 6d 65 72  *     *  Ephemer
2bde5 61 6c 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 73  al Table objects
2bde6 20 61 72 65 20 63 72 65 61 74 65 64 20 66 6f 72   are created for
2bde7 20 61 6c 6c 20 46 52 4f 4d 2d 63 6c 61 75 73 65   all FROM-clause
2bde8 20 73 75 62 71 75 65 72 69 65 73 2e 0a 2a 2a 20   subqueries..** 
2bde9 20 20 20 20 2a 20 20 4f 4e 20 61 6e 64 20 55 53      *  ON and US
2bdea 49 4e 47 20 63 6c 61 75 73 65 73 20 61 72 65 20  ING clauses are 
2bdeb 73 68 69 66 74 65 64 20 69 6e 74 6f 20 57 48 45  shifted into WHE
2bdec 52 45 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a  RE statements.**
2bded 20 20 20 20 20 2a 20 20 57 69 6c 64 63 61 72 64       *  Wildcard
2bdee 73 20 22 2a 22 20 61 6e 64 20 22 54 41 42 4c 45  s "*" and "TABLE
2bdef 2e 2a 22 20 69 6e 20 72 65 73 75 6c 74 20 73 65  .*" in result se
2bdf0 74 73 20 61 72 65 20 65 78 70 61 6e 64 65 64 2e  ts are expanded.
2bdf1 0a 2a 2a 20 20 20 20 20 2a 20 20 49 64 65 6e 74  .**     *  Ident
2bdf2 69 66 69 65 72 73 20 69 6e 20 65 78 70 72 65 73  ifiers in expres
2bdf3 73 69 6f 6e 20 61 72 65 20 6d 61 74 63 68 65 64  sion are matched
2bdf4 20 74 6f 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a   to tables..**.*
2bdf5 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
2bdf6 63 74 73 20 72 65 63 75 72 73 69 76 65 6c 79 20  cts recursively 
2bdf7 6f 6e 20 61 6c 6c 20 73 75 62 71 75 65 72 69 65  on all subquerie
2bdf8 73 20 77 69 74 68 69 6e 20 74 68 65 20 53 45 4c  s within the SEL
2bdf9 45 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ECT..*/.SQLITE_P
2bdfa 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2bdfb 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 0a 20  te3SelectPrep(. 
2bdfc 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2bdfd 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2bdfe 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
2bdff 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20  .  Select *p,   
2be00 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2be01 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2be02 74 20 62 65 69 6e 67 20 63 6f 64 65 64 2e 20 2a  t being coded. *
2be03 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  /.  NameContext 
2be04 2a 70 4f 75 74 65 72 4e 43 20 20 2f 2a 20 4e 61  *pOuterNC  /* Na
2be05 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 63  me context for c
2be06 6f 6e 74 61 69 6e 65 72 20 2a 2f 0a 29 7b 0a 20  ontainer */.){. 
2be07 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
2be08 69 66 28 20 4e 45 56 45 52 28 70 3d 3d 30 29 20  if( NEVER(p==0) 
2be09 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d  ) return;.  db =
2be0a 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
2be0b 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  f( p->selFlags &
2be0c 20 53 46 5f 48 61 73 54 79 70 65 49 6e 66 6f 20   SF_HasTypeInfo 
2be0d 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69  ) return;.  sqli
2be0e 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28  te3SelectExpand(
2be0f 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 69 66  pParse, p);.  if
2be10 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  ( pParse->nErr |
2be11 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
2be12 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73  ed ) return;.  s
2be13 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c  qlite3ResolveSel
2be14 65 63 74 4e 61 6d 65 73 28 70 50 61 72 73 65 2c  ectNames(pParse,
2be15 20 70 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20   p, pOuterNC);. 
2be16 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
2be17 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
2be18 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a  ailed ) return;.
2be19 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 41    sqlite3SelectA
2be1a 64 64 54 79 70 65 49 6e 66 6f 28 70 50 61 72 73  ddTypeInfo(pPars
2be1b 65 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  e, p);.}../*.** 
2be1c 52 65 73 65 74 20 74 68 65 20 61 67 67 72 65 67  Reset the aggreg
2be1d 61 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 2e  ate accumulator.
2be1e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 67 67 72 65  .**.** The aggre
2be1f 67 61 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72  gate accumulator
2be20 20 69 73 20 61 20 73 65 74 20 6f 66 20 6d 65 6d   is a set of mem
2be21 6f 72 79 20 63 65 6c 6c 73 20 74 68 61 74 20 68  ory cells that h
2be22 6f 6c 64 0a 2a 2a 20 69 6e 74 65 72 6d 65 64 69  old.** intermedi
2be23 61 74 65 20 72 65 73 75 6c 74 73 20 77 68 69 6c  ate results whil
2be24 65 20 63 61 6c 63 75 6c 61 74 69 6e 67 20 61 6e  e calculating an
2be25 20 61 67 67 72 65 67 61 74 65 2e 20 20 54 68 69   aggregate.  Thi
2be26 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 73 69 6d  s.** routine sim
2be27 70 6c 79 20 73 74 6f 72 65 73 20 4e 55 4c 4c 73  ply stores NULLs
2be28 20 69 6e 20 61 6c 6c 20 6f 66 20 74 68 6f 73 65   in all of those
2be29 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 2e 0a 2a   memory cells..*
2be2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
2be2b 73 65 74 41 63 63 75 6d 75 6c 61 74 6f 72 28 50  setAccumulator(P
2be2c 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 41 67  arse *pParse, Ag
2be2d 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 29  gInfo *pAggInfo)
2be2e 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
2be2f 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
2be30 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 41  nt i;.  struct A
2be31 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 46 75  ggInfo_func *pFu
2be32 6e 63 3b 0a 20 20 69 66 28 20 70 41 67 67 49 6e  nc;.  if( pAggIn
2be33 66 6f 2d 3e 6e 46 75 6e 63 2b 70 41 67 67 49 6e  fo->nFunc+pAggIn
2be34 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 30 20 29  fo->nColumn==0 )
2be35 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
2be36 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  }.  for(i=0; i<p
2be37 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e  AggInfo->nColumn
2be38 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  ; i++){.    sqli
2be39 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2be3a 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 70 41 67   OP_Null, 0, pAg
2be3b 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 69 5d 2e 69  gInfo->aCol[i].i
2be3c 4d 65 6d 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  Mem);.  }.  for(
2be3d 70 46 75 6e 63 3d 70 41 67 67 49 6e 66 6f 2d 3e  pFunc=pAggInfo->
2be3e 61 46 75 6e 63 2c 20 69 3d 30 3b 20 69 3c 70 41  aFunc, i=0; i<pA
2be3f 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69  ggInfo->nFunc; i
2be40 2b 2b 2c 20 70 46 75 6e 63 2b 2b 29 7b 0a 20 20  ++, pFunc++){.  
2be41 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2be42 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
2be43 30 2c 20 70 46 75 6e 63 2d 3e 69 4d 65 6d 29 3b  0, pFunc->iMem);
2be44 0a 20 20 20 20 69 66 28 20 70 46 75 6e 63 2d 3e  .    if( pFunc->
2be45 69 44 69 73 74 69 6e 63 74 3e 3d 30 20 29 7b 0a  iDistinct>=0 ){.
2be46 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d        Expr *pE =
2be47 20 70 46 75 6e 63 2d 3e 70 45 78 70 72 3b 0a 20   pFunc->pExpr;. 
2be48 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
2be49 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
2be4a 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
2be4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 2d  );.      if( pE-
2be4c 3e 78 2e 70 4c 69 73 74 3d 3d 30 20 7c 7c 20 70  >x.pList==0 || p
2be4d 45 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70  E->x.pList->nExp
2be4e 72 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  r!=1 ){.        
2be4f 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2be50 70 50 61 72 73 65 2c 20 22 44 49 53 54 49 4e 43  pParse, "DISTINC
2be51 54 20 61 67 67 72 65 67 61 74 65 73 20 6d 75 73  T aggregates mus
2be52 74 20 68 61 76 65 20 65 78 61 63 74 6c 79 20 6f  t have exactly o
2be53 6e 65 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  ne ".           
2be54 22 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 20  "argument");.   
2be55 20 20 20 20 20 70 46 75 6e 63 2d 3e 69 44 69 73       pFunc->iDis
2be56 74 69 6e 63 74 20 3d 20 2d 31 3b 0a 20 20 20 20  tinct = -1;.    
2be57 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2be58 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
2be59 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46 72 6f 6d  fo = keyInfoFrom
2be5a 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c  ExprList(pParse,
2be5b 20 70 45 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20   pE->x.pList);. 
2be5c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2be5d 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
2be5e 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70 46  penEphemeral, pF
2be5f 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63 74 2c 20  unc->iDistinct, 
2be60 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  0, 0,.          
2be61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be62 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c  (char*)pKeyInfo,
2be63 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
2be64 4f 46 46 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  OFF);.      }.  
2be65 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
2be66 20 49 6e 76 6f 6b 65 20 74 68 65 20 4f 50 5f 41   Invoke the OP_A
2be67 67 67 46 69 6e 61 6c 69 7a 65 20 6f 70 63 6f 64  ggFinalize opcod
2be68 65 20 66 6f 72 20 65 76 65 72 79 20 61 67 67 72  e for every aggr
2be69 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 0a 2a  egate function.*
2be6a 2a 20 69 6e 20 74 68 65 20 41 67 67 49 6e 66 6f  * in the AggInfo
2be6b 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
2be6c 74 61 74 69 63 20 76 6f 69 64 20 66 69 6e 61 6c  tatic void final
2be6d 69 7a 65 41 67 67 46 75 6e 63 74 69 6f 6e 73 28  izeAggFunctions(
2be6e 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 41  Parse *pParse, A
2be6f 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f  ggInfo *pAggInfo
2be70 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
2be71 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
2be72 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
2be73 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 46  AggInfo_func *pF
2be74 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 3d  ;.  for(i=0, pF=
2be75 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b  pAggInfo->aFunc;
2be76 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75   i<pAggInfo->nFu
2be77 6e 63 3b 20 69 2b 2b 2c 20 70 46 2b 2b 29 7b 0a  nc; i++, pF++){.
2be78 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c      ExprList *pL
2be79 69 73 74 20 3d 20 70 46 2d 3e 70 45 78 70 72 2d  ist = pF->pExpr-
2be7a 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 61 73  >x.pList;.    as
2be7b 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72  sert( !ExprHasPr
2be7c 6f 70 65 72 74 79 28 70 46 2d 3e 70 45 78 70 72  operty(pF->pExpr
2be7d 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
2be7e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2be7f 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 41  beAddOp4(v, OP_A
2be80 67 67 46 69 6e 61 6c 2c 20 70 46 2d 3e 69 4d 65  ggFinal, pF->iMe
2be81 6d 2c 20 70 4c 69 73 74 20 3f 20 70 4c 69 73 74  m, pList ? pList
2be82 2d 3e 6e 45 78 70 72 20 3a 20 30 2c 20 30 2c 0a  ->nExpr : 0, 0,.
2be83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be84 20 20 20 20 20 20 28 76 6f 69 64 2a 29 70 46 2d        (void*)pF-
2be85 3e 70 46 75 6e 63 2c 20 50 34 5f 46 55 4e 43 44  >pFunc, P4_FUNCD
2be86 45 46 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  EF);.  }.}../*.*
2be87 2a 20 55 70 64 61 74 65 20 74 68 65 20 61 63 63  * Update the acc
2be88 75 6d 75 6c 61 74 6f 72 20 6d 65 6d 6f 72 79 20  umulator memory 
2be89 63 65 6c 6c 73 20 66 6f 72 20 61 6e 20 61 67 67  cells for an agg
2be8a 72 65 67 61 74 65 20 62 61 73 65 64 20 6f 6e 0a  regate based on.
2be8b 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  ** the current c
2be8c 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e 0a  ursor position..
2be8d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75  */.static void u
2be8e 70 64 61 74 65 41 63 63 75 6d 75 6c 61 74 6f 72  pdateAccumulator
2be8f 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2be90 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66  AggInfo *pAggInf
2be91 6f 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  o){.  Vdbe *v = 
2be92 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
2be93 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74   int i;.  struct
2be94 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70   AggInfo_func *p
2be95 46 3b 0a 20 20 73 74 72 75 63 74 20 41 67 67 49  F;.  struct AggI
2be96 6e 66 6f 5f 63 6f 6c 20 2a 70 43 3b 0a 0a 20 20  nfo_col *pC;..  
2be97 70 41 67 67 49 6e 66 6f 2d 3e 64 69 72 65 63 74  pAggInfo->direct
2be98 4d 6f 64 65 20 3d 20 31 3b 0a 20 20 73 71 6c 69  Mode = 1;.  sqli
2be99 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 61  te3ExprCacheClea
2be9a 72 28 70 50 61 72 73 65 29 3b 0a 20 20 66 6f 72  r(pParse);.  for
2be9b 28 69 3d 30 2c 20 70 46 3d 70 41 67 67 49 6e 66  (i=0, pF=pAggInf
2be9c 6f 2d 3e 61 46 75 6e 63 3b 20 69 3c 70 41 67 67  o->aFunc; i<pAgg
2be9d 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b  Info->nFunc; i++
2be9e 2c 20 70 46 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  , pF++){.    int
2be9f 20 6e 41 72 67 3b 0a 20 20 20 20 69 6e 74 20 61   nArg;.    int a
2bea0 64 64 72 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20  ddrNext = 0;.   
2bea1 20 69 6e 74 20 72 65 67 41 67 67 3b 0a 20 20 20   int regAgg;.   
2bea2 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
2bea3 20 3d 20 70 46 2d 3e 70 45 78 70 72 2d 3e 78 2e   = pF->pExpr->x.
2bea4 70 4c 69 73 74 3b 0a 20 20 20 20 61 73 73 65 72  pList;.    asser
2bea5 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  t( !ExprHasPrope
2bea6 72 74 79 28 70 46 2d 3e 70 45 78 70 72 2c 20 45  rty(pF->pExpr, E
2bea7 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a  P_xIsSelect) );.
2bea8 20 20 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b      if( pList ){
2bea9 0a 20 20 20 20 20 20 6e 41 72 67 20 3d 20 70 4c  .      nArg = pL
2beaa 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20  ist->nExpr;.    
2beab 20 20 72 65 67 41 67 67 20 3d 20 73 71 6c 69 74    regAgg = sqlit
2beac 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70  e3GetTempRange(p
2bead 50 61 72 73 65 2c 20 6e 41 72 67 29 3b 0a 20 20  Parse, nArg);.  
2beae 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2beaf 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 72  odeExprList(pPar
2beb0 73 65 2c 20 70 4c 69 73 74 2c 20 72 65 67 41 67  se, pList, regAg
2beb1 67 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65  g, 0);.    }else
2beb2 7b 0a 20 20 20 20 20 20 6e 41 72 67 20 3d 20 30  {.      nArg = 0
2beb3 3b 0a 20 20 20 20 20 20 72 65 67 41 67 67 20 3d  ;.      regAgg =
2beb4 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   0;.    }.    if
2beb5 28 20 70 46 2d 3e 69 44 69 73 74 69 6e 63 74 3e  ( pF->iDistinct>
2beb6 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 64 64 72  =0 ){.      addr
2beb7 4e 65 78 74 20 3d 20 73 71 6c 69 74 65 33 56 64  Next = sqlite3Vd
2beb8 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
2beb9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 41        assert( nA
2beba 72 67 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 63  rg==1 );.      c
2bebb 6f 64 65 44 69 73 74 69 6e 63 74 28 70 50 61 72  odeDistinct(pPar
2bebc 73 65 2c 20 70 46 2d 3e 69 44 69 73 74 69 6e 63  se, pF->iDistinc
2bebd 74 2c 20 61 64 64 72 4e 65 78 74 2c 20 31 2c 20  t, addrNext, 1, 
2bebe 72 65 67 41 67 67 29 3b 0a 20 20 20 20 7d 0a 20  regAgg);.    }. 
2bebf 20 20 20 69 66 28 20 70 46 2d 3e 70 46 75 6e 63     if( pF->pFunc
2bec0 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
2bec1 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29  _FUNC_NEEDCOLL )
2bec2 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20  {.      CollSeq 
2bec3 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20  *pColl = 0;.    
2bec4 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
2bec5 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
2bec6 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
2bec7 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21    assert( pList!
2bec8 3d 30 20 29 3b 20 20 2f 2a 20 70 4c 69 73 74 21  =0 );  /* pList!
2bec9 3d 30 20 69 66 20 70 46 2d 3e 70 46 75 6e 63 20  =0 if pF->pFunc 
2beca 68 61 73 20 4e 45 45 44 43 4f 4c 4c 20 2a 2f 0a  has NEEDCOLL */.
2becb 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70        for(j=0, p
2becc 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 21  Item=pList->a; !
2becd 70 43 6f 6c 6c 20 26 26 20 6a 3c 6e 41 72 67 3b  pColl && j<nArg;
2bece 20 6a 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   j++, pItem++){.
2becf 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20          pColl = 
2bed0 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53  sqlite3ExprCollS
2bed1 65 71 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d  eq(pParse, pItem
2bed2 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  ->pExpr);.      
2bed3 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70 43 6f  }.      if( !pCo
2bed4 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43  ll ){.        pC
2bed5 6f 6c 6c 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  oll = pParse->db
2bed6 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20  ->pDfltColl;.   
2bed7 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
2bed8 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2bed9 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20 30  OP_CollSeq, 0, 0
2beda 2c 20 30 2c 20 28 63 68 61 72 20 2a 29 70 43 6f  , 0, (char *)pCo
2bedb 6c 6c 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b  ll, P4_COLLSEQ);
2bedc 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2bedd 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2bede 4f 50 5f 41 67 67 53 74 65 70 2c 20 30 2c 20 72  OP_AggStep, 0, r
2bedf 65 67 41 67 67 2c 20 70 46 2d 3e 69 4d 65 6d 2c  egAgg, pF->iMem,
2bee0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2bee1 20 20 20 20 20 20 20 28 76 6f 69 64 2a 29 70 46         (void*)pF
2bee2 2d 3e 70 46 75 6e 63 2c 20 50 34 5f 46 55 4e 43  ->pFunc, P4_FUNC
2bee3 44 45 46 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  DEF);.    sqlite
2bee4 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
2bee5 20 28 75 38 29 6e 41 72 67 29 3b 0a 20 20 20 20   (u8)nArg);.    
2bee6 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2bee7 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
2bee8 72 65 67 41 67 67 2c 20 6e 41 72 67 29 3b 0a 20  regAgg, nArg);. 
2bee9 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
2beea 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
2beeb 65 28 70 50 61 72 73 65 2c 20 72 65 67 41 67 67  e(pParse, regAgg
2beec 2c 20 6e 41 72 67 29 3b 0a 20 20 20 20 69 66 28  , nArg);.    if(
2beed 20 61 64 64 72 4e 65 78 74 20 29 7b 0a 20 20 20   addrNext ){.   
2beee 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
2beef 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64  solveLabel(v, ad
2bef0 64 72 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 73  drNext);.      s
2bef1 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43  qlite3ExprCacheC
2bef2 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20  lear(pParse);.  
2bef3 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d    }.  }.  for(i=
2bef4 30 2c 20 70 43 3d 70 41 67 67 49 6e 66 6f 2d 3e  0, pC=pAggInfo->
2bef5 61 43 6f 6c 3b 20 69 3c 70 41 67 67 49 6e 66 6f  aCol; i<pAggInfo
2bef6 2d 3e 6e 41 63 63 75 6d 75 6c 61 74 6f 72 3b 20  ->nAccumulator; 
2bef7 69 2b 2b 2c 20 70 43 2b 2b 29 7b 0a 20 20 20 20  i++, pC++){.    
2bef8 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
2bef9 70 50 61 72 73 65 2c 20 70 43 2d 3e 70 45 78 70  pParse, pC->pExp
2befa 72 2c 20 70 43 2d 3e 69 4d 65 6d 29 3b 0a 20 20  r, pC->iMem);.  
2befb 7d 0a 20 20 70 41 67 67 49 6e 66 6f 2d 3e 64 69  }.  pAggInfo->di
2befc 72 65 63 74 4d 6f 64 65 20 3d 20 30 3b 0a 20 20  rectMode = 0;.  
2befd 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
2befe 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 7d  Clear(pParse);.}
2beff 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
2bf00 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 53 45   code for the SE
2bf01 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 67  LECT statement g
2bf02 69 76 65 6e 20 69 6e 20 74 68 65 20 70 20 61 72  iven in the p ar
2bf03 67 75 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20  gument.  .**.** 
2bf04 54 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20  The results are 
2bf05 64 69 73 74 72 69 62 75 74 65 64 20 69 6e 20 76  distributed in v
2bf06 61 72 69 6f 75 73 20 77 61 79 73 20 64 65 70 65  arious ways depe
2bf07 6e 64 69 6e 67 20 6f 6e 20 74 68 65 0a 2a 2a 20  nding on the.** 
2bf08 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
2bf09 53 65 6c 65 63 74 44 65 73 74 20 73 74 72 75 63  SelectDest struc
2bf0a 74 75 72 65 20 70 6f 69 6e 74 65 64 20 74 6f 20  ture pointed to 
2bf0b 62 79 20 61 72 67 75 6d 65 6e 74 20 70 44 65 73  by argument pDes
2bf0c 74 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  t.** as follows:
2bf0d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 70 44 65 73 74  .**.**     pDest
2bf0e 2d 3e 65 44 65 73 74 20 20 20 20 52 65 73 75 6c  ->eDest    Resul
2bf0f 74 0a 2a 2a 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d  t.**     -------
2bf10 2d 2d 2d 2d 2d 20 20 20 20 2d 2d 2d 2d 2d 2d 2d  -----    -------
2bf11 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2bf12 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2bf13 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 53 52 54 5f  ----.**     SRT_
2bf14 4f 75 74 70 75 74 20 20 20 20 20 20 47 65 6e 65  Output      Gene
2bf15 72 61 74 65 20 61 20 72 6f 77 20 6f 66 20 6f 75  rate a row of ou
2bf16 74 70 75 74 20 28 75 73 69 6e 67 20 74 68 65 20  tput (using the 
2bf17 4f 50 5f 52 65 73 75 6c 74 52 6f 77 0a 2a 2a 20  OP_ResultRow.** 
2bf18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bf19 20 20 20 20 6f 70 63 6f 64 65 29 20 66 6f 72 20      opcode) for 
2bf1a 65 61 63 68 20 72 6f 77 20 69 6e 20 74 68 65 20  each row in the 
2bf1b 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2a 0a 2a  result set..**.*
2bf1c 2a 20 20 20 20 20 53 52 54 5f 4d 65 6d 20 20 20  *     SRT_Mem   
2bf1d 20 20 20 20 20 20 4f 6e 6c 79 20 76 61 6c 69 64        Only valid
2bf1e 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 69   if the result i
2bf1f 73 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d  s a single colum
2bf20 6e 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  n..**           
2bf21 20 20 20 20 20 20 20 20 20 20 53 74 6f 72 65 20            Store 
2bf22 74 68 65 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e  the first column
2bf23 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 72 65   of the first re
2bf24 73 75 6c 74 20 72 6f 77 0a 2a 2a 20 20 20 20 20  sult row.**     
2bf25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bf26 69 6e 20 72 65 67 69 73 74 65 72 20 70 44 65 73  in register pDes
2bf27 74 2d 3e 69 50 61 72 6d 20 74 68 65 6e 20 61 62  t->iParm then ab
2bf28 61 6e 64 6f 6e 20 74 68 65 20 72 65 73 74 0a 2a  andon the rest.*
2bf29 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2bf2a 20 20 20 20 20 20 6f 66 20 74 68 65 20 71 75 65        of the que
2bf2b 72 79 2e 20 20 54 68 69 73 20 64 65 73 74 69 6e  ry.  This destin
2bf2c 61 74 69 6f 6e 20 69 6d 70 6c 69 65 73 20 22 4c  ation implies "L
2bf2d 49 4d 49 54 20 31 22 2e 0a 2a 2a 0a 2a 2a 20 20  IMIT 1"..**.**  
2bf2e 20 20 20 53 52 54 5f 53 65 74 20 20 20 20 20 20     SRT_Set      
2bf2f 20 20 20 54 68 65 20 72 65 73 75 6c 74 20 6d 75     The result mu
2bf30 73 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20 63  st be a single c
2bf31 6f 6c 75 6d 6e 2e 20 20 53 74 6f 72 65 20 65 61  olumn.  Store ea
2bf32 63 68 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ch.**           
2bf33 20 20 20 20 20 20 20 20 20 20 72 6f 77 20 6f 66            row of
2bf34 20 72 65 73 75 6c 74 20 61 73 20 74 68 65 20 6b   result as the k
2bf35 65 79 20 69 6e 20 74 61 62 6c 65 20 70 44 65 73  ey in table pDes
2bf36 74 2d 3e 69 50 61 72 6d 2e 20 0a 2a 2a 20 20 20  t->iParm. .**   
2bf37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bf38 20 20 41 70 70 6c 79 20 74 68 65 20 61 66 66 69    Apply the affi
2bf39 6e 69 74 79 20 70 44 65 73 74 2d 3e 61 66 66 69  nity pDest->affi
2bf3a 6e 69 74 79 20 62 65 66 6f 72 65 20 73 74 6f 72  nity before stor
2bf3b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ing.**          
2bf3c 20 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c             resul
2bf3d 74 73 2e 20 20 55 73 65 64 20 74 6f 20 69 6d 70  ts.  Used to imp
2bf3e 6c 65 6d 65 6e 74 20 22 49 4e 20 28 53 45 4c 45  lement "IN (SELE
2bf3f 43 54 20 2e 2e 2e 29 22 2e 0a 2a 2a 0a 2a 2a 20  CT ...)"..**.** 
2bf40 20 20 20 20 53 52 54 5f 55 6e 69 6f 6e 20 20 20      SRT_Union   
2bf41 20 20 20 20 53 74 6f 72 65 20 72 65 73 75 6c 74      Store result
2bf42 73 20 61 73 20 61 20 6b 65 79 20 69 6e 20 61 20  s as a key in a 
2bf43 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
2bf44 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a  pDest->iParm..**
2bf45 0a 2a 2a 20 20 20 20 20 53 52 54 5f 45 78 63 65  .**     SRT_Exce
2bf46 70 74 20 20 20 20 20 20 52 65 6d 6f 76 65 20 72  pt      Remove r
2bf47 65 73 75 6c 74 73 20 66 72 6f 6d 20 74 68 65 20  esults from the 
2bf48 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
2bf49 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a  pDest->iParm..**
2bf4a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 54 61 62 6c  .**     SRT_Tabl
2bf4b 65 20 20 20 20 20 20 20 53 74 6f 72 65 20 72 65  e       Store re
2bf4c 73 75 6c 74 73 20 69 6e 20 74 65 6d 70 6f 72 61  sults in tempora
2bf4d 72 79 20 74 61 62 6c 65 20 70 44 65 73 74 2d 3e  ry table pDest->
2bf4e 69 50 61 72 6d 2e 0a 2a 2a 20 20 20 20 20 20 20  iParm..**       
2bf4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68                Th
2bf50 69 73 20 69 73 20 6c 69 6b 65 20 53 52 54 5f 45  is is like SRT_E
2bf51 70 68 65 6d 54 61 62 20 65 78 63 65 70 74 20 74  phemTab except t
2bf52 68 61 74 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a  hat the table.**
2bf53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bf54 20 20 20 20 20 69 73 20 61 73 73 75 6d 65 64 20       is assumed 
2bf55 74 6f 20 61 6c 72 65 61 64 79 20 62 65 20 6f 70  to already be op
2bf56 65 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52  en..**.**     SR
2bf57 54 5f 45 70 68 65 6d 54 61 62 20 20 20 20 43 72  T_EphemTab    Cr
2bf58 65 61 74 65 20 61 6e 20 74 65 6d 70 6f 72 61 72  eate an temporar
2bf59 79 20 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69  y table pDest->i
2bf5a 50 61 72 6d 20 61 6e 64 20 73 74 6f 72 65 0a 2a  Parm and store.*
2bf5b 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2bf5c 20 20 20 20 20 20 74 68 65 20 72 65 73 75 6c 74        the result
2bf5d 20 74 68 65 72 65 2e 20 54 68 65 20 63 75 72 73   there. The curs
2bf5e 6f 72 20 69 73 20 6c 65 66 74 20 6f 70 65 6e 20  or is left open 
2bf5f 61 66 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20  after.**        
2bf60 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74               ret
2bf61 75 72 6e 69 6e 67 2e 20 20 54 68 69 73 20 69 73  urning.  This is
2bf62 20 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c 65 20   like SRT_Table 
2bf63 65 78 63 65 70 74 20 74 68 61 74 0a 2a 2a 20 20  except that.**  
2bf64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bf65 20 20 20 74 68 69 73 20 64 65 73 74 69 6e 61 74     this destinat
2bf66 69 6f 6e 20 75 73 65 73 20 4f 50 5f 4f 70 65 6e  ion uses OP_Open
2bf67 45 70 68 65 6d 65 72 61 6c 20 74 6f 20 63 72 65  Ephemeral to cre
2bf68 61 74 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ate.**          
2bf69 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 74             the t
2bf6a 61 62 6c 65 20 66 69 72 73 74 2e 0a 2a 2a 0a 2a  able first..**.*
2bf6b 2a 20 20 20 20 20 53 52 54 5f 43 6f 72 6f 75 74  *     SRT_Corout
2bf6c 69 6e 65 20 20 20 47 65 6e 65 72 61 74 65 20 61  ine   Generate a
2bf6d 20 63 6f 2d 72 6f 75 74 69 6e 65 20 74 68 61 74   co-routine that
2bf6e 20 72 65 74 75 72 6e 73 20 61 20 6e 65 77 20 72   returns a new r
2bf6f 6f 77 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20  ow of.**        
2bf70 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 73               res
2bf71 75 6c 74 73 20 65 61 63 68 20 74 69 6d 65 20 69  ults each time i
2bf72 74 20 69 73 20 69 6e 76 6f 6b 65 64 2e 20 20 54  t is invoked.  T
2bf73 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 0a 2a  he entry point.*
2bf74 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2bf75 20 20 20 20 20 20 6f 66 20 74 68 65 20 63 6f 2d        of the co-
2bf76 72 6f 75 74 69 6e 65 20 69 73 20 73 74 6f 72 65  routine is store
2bf77 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 44  d in register pD
2bf78 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a 0a 2a  est->iParm..**.*
2bf79 2a 20 20 20 20 20 53 52 54 5f 45 78 69 73 74 73  *     SRT_Exists
2bf7a 20 20 20 20 20 20 53 74 6f 72 65 20 61 20 31 20        Store a 1 
2bf7b 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 70  in memory cell p
2bf7c 44 65 73 74 2d 3e 69 50 61 72 6d 20 69 66 20 74  Dest->iParm if t
2bf7d 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 20 20 20  he result.**    
2bf7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bf7f 20 73 65 74 20 69 73 20 6e 6f 74 20 65 6d 70 74   set is not empt
2bf80 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54  y..**.**     SRT
2bf81 5f 44 69 73 63 61 72 64 20 20 20 20 20 54 68 72  _Discard     Thr
2bf82 6f 77 20 74 68 65 20 72 65 73 75 6c 74 73 20 61  ow the results a
2bf83 77 61 79 2e 20 20 54 68 69 73 20 69 73 20 75 73  way.  This is us
2bf84 65 64 20 62 79 20 53 45 4c 45 43 54 0a 2a 2a 20  ed by SELECT.** 
2bf85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bf86 20 20 20 20 73 74 61 74 65 6d 65 6e 74 73 20 77      statements w
2bf87 69 74 68 69 6e 20 74 72 69 67 67 65 72 73 20 77  ithin triggers w
2bf88 68 6f 73 65 20 6f 6e 6c 79 20 70 75 72 70 6f 73  hose only purpos
2bf89 65 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  e is.**         
2bf8a 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20              the 
2bf8b 73 69 64 65 2d 65 66 66 65 63 74 73 20 6f 66 20  side-effects of 
2bf8c 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  functions..**.**
2bf8d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
2bf8e 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
2bf8f 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 49 66 20   of errors.  If 
2bf90 61 6e 79 20 65 72 72 6f 72 73 20 61 72 65 0a 2a  any errors are.*
2bf91 2a 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74  * encountered, t
2bf92 68 65 6e 20 61 6e 20 61 70 70 72 6f 70 72 69 61  hen an appropria
2bf93 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  te error message
2bf94 20 69 73 20 6c 65 66 74 20 69 6e 0a 2a 2a 20 70   is left in.** p
2bf95 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2e 0a  Parse->zErrMsg..
2bf96 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2bf97 6e 65 20 64 6f 65 73 20 4e 4f 54 20 66 72 65 65  ne does NOT free
2bf98 20 74 68 65 20 53 65 6c 65 63 74 20 73 74 72 75   the Select stru
2bf99 63 74 75 72 65 20 70 61 73 73 65 64 20 69 6e 2e  cture passed in.
2bf9a 20 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67    The.** calling
2bf9b 20 66 75 6e 63 74 69 6f 6e 20 6e 65 65 64 73 20   function needs 
2bf9c 74 6f 20 64 6f 20 74 68 61 74 2e 0a 2a 2f 0a 53  to do that..*/.S
2bf9d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2bf9e 74 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  t sqlite3Select(
2bf9f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2bfa0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
2bfa1 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   parser context 
2bfa2 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20  */.  Select *p, 
2bfa3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2bfa4 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
2bfa5 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64 2e  ent being coded.
2bfa6 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74   */.  SelectDest
2bfa7 20 2a 70 44 65 73 74 20 20 20 20 20 20 2f 2a 20   *pDest      /* 
2bfa8 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20  What to do with 
2bfa9 74 68 65 20 71 75 65 72 79 20 72 65 73 75 6c 74  the query result
2bfaa 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c  s */.){.  int i,
2bfab 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   j;             
2bfac 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
2bfad 73 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f  s */.  WhereInfo
2bfae 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a   *pWInfo;     /*
2bfaf 20 52 65 74 75 72 6e 20 66 72 6f 6d 20 73 71 6c   Return from sql
2bfb0 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
2bfb1 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20   */.  Vdbe *v;  
2bfb2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2bfb3 54 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  The virtual mach
2bfb4 69 6e 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  ine under constr
2bfb5 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  uction */.  int 
2bfb6 69 73 41 67 67 3b 20 20 20 20 20 20 20 20 20 20  isAgg;          
2bfb7 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 73     /* True for s
2bfb8 65 6c 65 63 74 20 6c 69 73 74 73 20 6c 69 6b 65  elect lists like
2bfb9 20 22 63 6f 75 6e 74 28 2a 29 22 20 2a 2f 0a 20   "count(*)" */. 
2bfba 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
2bfbb 74 3b 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20  t;      /* List 
2bfbc 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 65 78  of columns to ex
2bfbd 74 72 61 63 74 2e 20 2a 2f 0a 20 20 53 72 63 4c  tract. */.  SrcL
2bfbe 69 73 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20  ist *pTabList;  
2bfbf 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61     /* List of ta
2bfc0 62 6c 65 73 20 74 6f 20 73 65 6c 65 63 74 20 66  bles to select f
2bfc1 72 6f 6d 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  rom */.  Expr *p
2bfc2 57 68 65 72 65 3b 20 20 20 20 20 20 20 20 20 20  Where;          
2bfc3 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
2bfc4 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c  use.  May be NUL
2bfc5 4c 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  L */.  ExprList 
2bfc6 2a 70 4f 72 64 65 72 42 79 3b 20 20 20 20 2f 2a  *pOrderBy;    /*
2bfc7 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   The ORDER BY cl
2bfc8 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55  ause.  May be NU
2bfc9 4c 4c 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  LL */.  ExprList
2bfca 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 2f   *pGroupBy;    /
2bfcb 2a 20 54 68 65 20 47 52 4f 55 50 20 42 59 20 63  * The GROUP BY c
2bfcc 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e  lause.  May be N
2bfcd 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ULL */.  Expr *p
2bfce 48 61 76 69 6e 67 3b 20 20 20 20 20 20 20 20 20  Having;         
2bfcf 2f 2a 20 54 68 65 20 48 41 56 49 4e 47 20 63 6c  /* The HAVING cl
2bfd0 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55  ause.  May be NU
2bfd1 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 73 44 69  LL */.  int isDi
2bfd2 73 74 69 6e 63 74 3b 20 20 20 20 20 20 20 20 2f  stinct;        /
2bfd3 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 44 49  * True if the DI
2bfd4 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69  STINCT keyword i
2bfd5 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69  s present */.  i
2bfd6 6e 74 20 64 69 73 74 69 6e 63 74 3b 20 20 20 20  nt distinct;    
2bfd7 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74        /* Table t
2bfd8 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20 64 69  o use for the di
2bfd9 73 74 69 6e 63 74 20 73 65 74 20 2a 2f 0a 20 20  stinct set */.  
2bfda 69 6e 74 20 72 63 20 3d 20 31 3b 20 20 20 20 20  int rc = 1;     
2bfdb 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
2bfdc 74 6f 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74  to return from t
2bfdd 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  his function */.
2bfde 20 20 69 6e 74 20 61 64 64 72 53 6f 72 74 49 6e    int addrSortIn
2bfdf 64 65 78 3b 20 20 20 20 20 2f 2a 20 41 64 64 72  dex;     /* Addr
2bfe0 65 73 73 20 6f 66 20 61 6e 20 4f 50 5f 4f 70 65  ess of an OP_Ope
2bfe1 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72  nEphemeral instr
2bfe2 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 41 67 67 49  uction */.  AggI
2bfe3 6e 66 6f 20 73 41 67 67 49 6e 66 6f 3b 20 20 20  nfo sAggInfo;   
2bfe4 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f     /* Informatio
2bfe5 6e 20 75 73 65 64 20 62 79 20 61 67 67 72 65 67  n used by aggreg
2bfe6 61 74 65 20 71 75 65 72 69 65 73 20 2a 2f 0a 20  ate queries */. 
2bfe7 20 69 6e 74 20 69 45 6e 64 3b 20 20 20 20 20 20   int iEnd;      
2bfe8 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
2bfe9 73 73 20 6f 66 20 74 68 65 20 65 6e 64 20 6f 66  ss of the end of
2bfea 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20   the query */.  
2bfeb 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
2bfec 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
2bfed 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2bfee 6e 20 2a 2f 0a 0a 20 20 64 62 20 3d 20 70 50 61  n */..  db = pPa
2bfef 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70  rse->db;.  if( p
2bff0 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  ==0 || db->mallo
2bff1 63 46 61 69 6c 65 64 20 7c 7c 20 70 50 61 72 73  cFailed || pPars
2bff2 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20 72  e->nErr ){.    r
2bff3 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69  eturn 1;.  }.  i
2bff4 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
2bff5 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
2bff6 54 45 5f 53 45 4c 45 43 54 2c 20 30 2c 20 30 2c  TE_SELECT, 0, 0,
2bff7 20 30 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a   0) ) return 1;.
2bff8 20 20 6d 65 6d 73 65 74 28 26 73 41 67 67 49 6e    memset(&sAggIn
2bff9 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 41  fo, 0, sizeof(sA
2bffa 67 67 49 6e 66 6f 29 29 3b 0a 0a 20 20 69 66 28  ggInfo));..  if(
2bffb 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72 62   IgnorableOrderb
2bffc 79 28 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20  y(pDest) ){.    
2bffd 61 73 73 65 72 74 28 70 44 65 73 74 2d 3e 65 44  assert(pDest->eD
2bffe 65 73 74 3d 3d 53 52 54 5f 45 78 69 73 74 73 20  est==SRT_Exists 
2bfff 7c 7c 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d  || pDest->eDest=
2c000 3d 53 52 54 5f 55 6e 69 6f 6e 20 7c 7c 20 0a 20  =SRT_Union || . 
2c001 20 20 20 20 20 20 20 20 20 20 70 44 65 73 74 2d            pDest-
2c002 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45 78 63 65  >eDest==SRT_Exce
2c003 70 74 20 7c 7c 20 70 44 65 73 74 2d 3e 65 44 65  pt || pDest->eDe
2c004 73 74 3d 3d 53 52 54 5f 44 69 73 63 61 72 64 29  st==SRT_Discard)
2c005 3b 0a 20 20 20 20 2f 2a 20 49 66 20 4f 52 44 45  ;.    /* If ORDE
2c006 52 20 42 59 20 6d 61 6b 65 73 20 6e 6f 20 64 69  R BY makes no di
2c007 66 66 65 72 65 6e 63 65 20 69 6e 20 74 68 65 20  fference in the 
2c008 6f 75 74 70 75 74 20 74 68 65 6e 20 6e 65 69 74  output then neit
2c009 68 65 72 20 64 6f 65 73 0a 20 20 20 20 2a 2a 20  her does.    ** 
2c00a 44 49 53 54 49 4e 43 54 20 73 6f 20 69 74 20 63  DISTINCT so it c
2c00b 61 6e 20 62 65 20 72 65 6d 6f 76 65 64 20 74 6f  an be removed to
2c00c 6f 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  o. */.    sqlite
2c00d 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
2c00e 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 29  db, p->pOrderBy)
2c00f 3b 0a 20 20 20 20 70 2d 3e 70 4f 72 64 65 72 42  ;.    p->pOrderB
2c010 79 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 73 65  y = 0;.    p->se
2c011 6c 46 6c 61 67 73 20 26 3d 20 7e 53 46 5f 44 69  lFlags &= ~SF_Di
2c012 73 74 69 6e 63 74 3b 0a 20 20 7d 0a 20 20 73 71  stinct;.  }.  sq
2c013 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28  lite3SelectPrep(
2c014 70 50 61 72 73 65 2c 20 70 2c 20 30 29 3b 0a 20  pParse, p, 0);. 
2c015 20 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e 70   pOrderBy = p->p
2c016 4f 72 64 65 72 42 79 3b 0a 20 20 70 54 61 62 4c  OrderBy;.  pTabL
2c017 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20  ist = p->pSrc;. 
2c018 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c   pEList = p->pEL
2c019 69 73 74 3b 0a 20 20 69 66 28 20 70 50 61 72 73  ist;.  if( pPars
2c01a 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d  e->nErr || db->m
2c01b 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
2c01c 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65     goto select_e
2c01d 6e 64 3b 0a 20 20 7d 0a 20 20 69 73 41 67 67 20  nd;.  }.  isAgg 
2c01e 3d 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  = (p->selFlags &
2c01f 20 53 46 5f 41 67 67 72 65 67 61 74 65 29 21 3d   SF_Aggregate)!=
2c020 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c  0;.  assert( pEL
2c021 69 73 74 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20  ist!=0 );..  /* 
2c022 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67  Begin generating
2c023 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 76 20   code..  */.  v 
2c024 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
2c025 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
2c026 76 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c 65  v==0 ) goto sele
2c027 63 74 5f 65 6e 64 3b 0a 0a 20 20 2f 2a 20 47 65  ct_end;..  /* Ge
2c028 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
2c029 61 6c 6c 20 73 75 62 2d 71 75 65 72 69 65 73 20  all sub-queries 
2c02a 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
2c02b 73 65 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65 66  se.  */.#if !def
2c02c 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
2c02d 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21 64  _SUBQUERY) || !d
2c02e 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
2c02f 49 54 5f 56 49 45 57 29 0a 20 20 66 6f 72 28 69  IT_VIEW).  for(i
2c030 3d 30 3b 20 21 70 2d 3e 70 50 72 69 6f 72 20 26  =0; !p->pPrior &
2c031 26 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  & i<pTabList->nS
2c032 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74  rc; i++){.    st
2c033 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
2c034 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 54 61 62  m *pItem = &pTab
2c035 4c 69 73 74 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20  List->a[i];.    
2c036 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b  SelectDest dest;
2c037 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 75  .    Select *pSu
2c038 62 20 3d 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65  b = pItem->pSele
2c039 63 74 3b 0a 20 20 20 20 69 6e 74 20 69 73 41 67  ct;.    int isAg
2c03a 67 53 75 62 3b 0a 0a 20 20 20 20 69 66 28 20 70  gSub;..    if( p
2c03b 53 75 62 3d 3d 30 20 7c 7c 20 70 49 74 65 6d 2d  Sub==0 || pItem-
2c03c 3e 69 73 50 6f 70 75 6c 61 74 65 64 20 29 20 63  >isPopulated ) c
2c03d 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20 2f 2a  ontinue;..    /*
2c03e 20 49 6e 63 72 65 6d 65 6e 74 20 50 61 72 73 65   Increment Parse
2c03f 2e 6e 48 65 69 67 68 74 20 62 79 20 74 68 65 20  .nHeight by the 
2c040 68 65 69 67 68 74 20 6f 66 20 74 68 65 20 6c 61  height of the la
2c041 72 67 65 73 74 20 65 78 70 72 65 73 73 69 6f 6e  rgest expression
2c042 0a 20 20 20 20 2a 2a 20 74 72 65 65 20 72 65 66  .    ** tree ref
2c043 65 72 65 64 20 74 6f 20 62 79 20 74 68 69 73 2c  ered to by this,
2c044 20 74 68 65 20 70 61 72 65 6e 74 20 73 65 6c 65   the parent sele
2c045 63 74 2e 20 54 68 65 20 63 68 69 6c 64 20 73 65  ct. The child se
2c046 6c 65 63 74 0a 20 20 20 20 2a 2a 20 6d 61 79 20  lect.    ** may 
2c047 63 6f 6e 74 61 69 6e 20 65 78 70 72 65 73 73 69  contain expressi
2c048 6f 6e 20 74 72 65 65 73 20 6f 66 20 61 74 20 6d  on trees of at m
2c049 6f 73 74 0a 20 20 20 20 2a 2a 20 28 53 51 4c 49  ost.    ** (SQLI
2c04a 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54  TE_MAX_EXPR_DEPT
2c04b 48 2d 50 61 72 73 65 2e 6e 48 65 69 67 68 74 29  H-Parse.nHeight)
2c04c 20 68 65 69 67 68 74 2e 20 54 68 69 73 20 69 73   height. This is
2c04d 20 61 20 62 69 74 0a 20 20 20 20 2a 2a 20 6d 6f   a bit.    ** mo
2c04e 72 65 20 63 6f 6e 73 65 72 76 61 74 69 76 65 20  re conservative 
2c04f 74 68 61 6e 20 6e 65 63 65 73 73 61 72 79 2c 20  than necessary, 
2c050 62 75 74 20 6d 75 63 68 20 65 61 73 69 65 72 20  but much easier 
2c051 74 68 61 6e 20 65 6e 66 6f 72 63 69 6e 67 0a 20  than enforcing. 
2c052 20 20 20 2a 2a 20 61 6e 20 65 78 61 63 74 20 6c     ** an exact l
2c053 69 6d 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  imit..    */.   
2c054 20 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74   pParse->nHeight
2c055 20 2b 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63   += sqlite3Selec
2c056 74 45 78 70 72 48 65 69 67 68 74 28 70 29 3b 0a  tExprHeight(p);.
2c057 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f  .    /* Check to
2c058 20 73 65 65 20 69 66 20 74 68 65 20 73 75 62 71   see if the subq
2c059 75 65 72 79 20 63 61 6e 20 62 65 20 61 62 73 6f  uery can be abso
2c05a 72 62 65 64 20 69 6e 74 6f 20 74 68 65 20 70 61  rbed into the pa
2c05b 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 73 41  rent. */.    isA
2c05c 67 67 53 75 62 20 3d 20 28 70 53 75 62 2d 3e 73  ggSub = (pSub->s
2c05d 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 41 67 67  elFlags & SF_Agg
2c05e 72 65 67 61 74 65 29 21 3d 30 3b 0a 20 20 20 20  regate)!=0;.    
2c05f 69 66 28 20 66 6c 61 74 74 65 6e 53 75 62 71 75  if( flattenSubqu
2c060 65 72 79 28 70 50 61 72 73 65 2c 20 70 2c 20 69  ery(pParse, p, i
2c061 2c 20 69 73 41 67 67 2c 20 69 73 41 67 67 53 75  , isAgg, isAggSu
2c062 62 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  b) ){.      if( 
2c063 69 73 41 67 67 53 75 62 20 29 7b 0a 20 20 20 20  isAggSub ){.    
2c064 20 20 20 20 69 73 41 67 67 20 3d 20 31 3b 0a 20      isAgg = 1;. 
2c065 20 20 20 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61         p->selFla
2c066 67 73 20 7c 3d 20 53 46 5f 41 67 67 72 65 67 61  gs |= SF_Aggrega
2c067 74 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  te;.      }.    
2c068 20 20 69 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 65    i = -1;.    }e
2c069 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
2c06a 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
2c06b 28 26 64 65 73 74 2c 20 53 52 54 5f 45 70 68 65  (&dest, SRT_Ephe
2c06c 6d 54 61 62 2c 20 70 49 74 65 6d 2d 3e 69 43 75  mTab, pItem->iCu
2c06d 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 61 73 73  rsor);.      ass
2c06e 65 72 74 28 20 70 49 74 65 6d 2d 3e 69 73 50 6f  ert( pItem->isPo
2c06f 70 75 6c 61 74 65 64 3d 3d 30 20 29 3b 0a 20 20  pulated==0 );.  
2c070 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
2c071 74 28 70 50 61 72 73 65 2c 20 70 53 75 62 2c 20  t(pParse, pSub, 
2c072 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20 70 49  &dest);.      pI
2c073 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64  tem->isPopulated
2c074 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 1;.    }.    
2c075 69 66 28 20 2f 2a 70 50 61 72 73 65 2d 3e 6e 45  if( /*pParse->nE
2c076 72 72 20 7c 7c 2a 2f 20 64 62 2d 3e 6d 61 6c 6c  rr ||*/ db->mall
2c077 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
2c078 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e    goto select_en
2c079 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61  d;.    }.    pPa
2c07a 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2d 3d 20  rse->nHeight -= 
2c07b 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70  sqlite3SelectExp
2c07c 72 48 65 69 67 68 74 28 70 29 3b 0a 20 20 20 20  rHeight(p);.    
2c07d 70 54 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53  pTabList = p->pS
2c07e 72 63 3b 0a 20 20 20 20 69 66 28 20 21 49 67 6e  rc;.    if( !Ign
2c07f 6f 72 61 62 6c 65 4f 72 64 65 72 62 79 28 70 44  orableOrderby(pD
2c080 65 73 74 29 20 29 7b 0a 20 20 20 20 20 20 70 4f  est) ){.      pO
2c081 72 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64  rderBy = p->pOrd
2c082 65 72 42 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  erBy;.    }.  }.
2c083 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45    pEList = p->pE
2c084 4c 69 73 74 3b 0a 23 65 6e 64 69 66 0a 20 20 70  List;.#endif.  p
2c085 57 68 65 72 65 20 3d 20 70 2d 3e 70 57 68 65 72  Where = p->pWher
2c086 65 3b 0a 20 20 70 47 72 6f 75 70 42 79 20 3d 20  e;.  pGroupBy = 
2c087 70 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 70  p->pGroupBy;.  p
2c088 48 61 76 69 6e 67 20 3d 20 70 2d 3e 70 48 61 76  Having = p->pHav
2c089 69 6e 67 3b 0a 20 20 69 73 44 69 73 74 69 6e 63  ing;.  isDistinc
2c08a 74 20 3d 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73  t = (p->selFlags
2c08b 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 29 21   & SF_Distinct)!
2c08c 3d 30 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  =0;..#ifndef SQL
2c08d 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e  ITE_OMIT_COMPOUN
2c08e 44 5f 53 45 4c 45 43 54 0a 20 20 2f 2a 20 49 66  D_SELECT.  /* If
2c08f 20 74 68 65 72 65 20 69 73 20 61 72 65 20 61 20   there is are a 
2c090 73 65 71 75 65 6e 63 65 20 6f 66 20 71 75 65 72  sequence of quer
2c091 69 65 73 2c 20 64 6f 20 74 68 65 20 65 61 72 6c  ies, do the earl
2c092 69 65 72 20 6f 6e 65 73 20 66 69 72 73 74 2e 0a  ier ones first..
2c093 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 50    */.  if( p->pP
2c094 72 69 6f 72 20 29 7b 0a 20 20 20 20 69 66 28 20  rior ){.    if( 
2c095 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 3d 3d 30  p->pRightmost==0
2c096 20 29 7b 0a 20 20 20 20 20 20 53 65 6c 65 63 74   ){.      Select
2c097 20 2a 70 4c 6f 6f 70 2c 20 2a 70 52 69 67 68 74   *pLoop, *pRight
2c098 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 74 20   = 0;.      int 
2c099 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  cnt = 0;.      i
2c09a 6e 74 20 6d 78 53 65 6c 65 63 74 3b 0a 20 20 20  nt mxSelect;.   
2c09b 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 3b 20     for(pLoop=p; 
2c09c 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f  pLoop; pLoop=pLo
2c09d 6f 70 2d 3e 70 50 72 69 6f 72 2c 20 63 6e 74 2b  op->pPrior, cnt+
2c09e 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 6f 6f  +){.        pLoo
2c09f 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20  p->pRightmost = 
2c0a0 70 3b 0a 20 20 20 20 20 20 20 20 70 4c 6f 6f 70  p;.        pLoop
2c0a1 2d 3e 70 4e 65 78 74 20 3d 20 70 52 69 67 68 74  ->pNext = pRight
2c0a2 3b 0a 20 20 20 20 20 20 20 20 70 52 69 67 68 74  ;.        pRight
2c0a3 20 3d 20 70 4c 6f 6f 70 3b 0a 20 20 20 20 20 20   = pLoop;.      
2c0a4 7d 0a 20 20 20 20 20 20 6d 78 53 65 6c 65 63 74  }.      mxSelect
2c0a5 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51   = db->aLimit[SQ
2c0a6 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f  LITE_LIMIT_COMPO
2c0a7 55 4e 44 5f 53 45 4c 45 43 54 5d 3b 0a 20 20 20  UND_SELECT];.   
2c0a8 20 20 20 69 66 28 20 6d 78 53 65 6c 65 63 74 20     if( mxSelect 
2c0a9 26 26 20 63 6e 74 3e 6d 78 53 65 6c 65 63 74 20  && cnt>mxSelect 
2c0aa 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2c0ab 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2c0ac 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72  e, "too many ter
2c0ad 6d 73 20 69 6e 20 63 6f 6d 70 6f 75 6e 64 20 53  ms in compound S
2c0ae 45 4c 45 43 54 22 29 3b 0a 20 20 20 20 20 20 20  ELECT");.       
2c0af 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
2c0b0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74   }.    }.    ret
2c0b1 75 72 6e 20 6d 75 6c 74 69 53 65 6c 65 63 74 28  urn multiSelect(
2c0b2 70 50 61 72 73 65 2c 20 70 2c 20 70 44 65 73 74  pParse, p, pDest
2c0b3 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  );.  }.#endif.. 
2c0b4 20 2f 2a 20 49 66 20 77 72 69 74 69 6e 67 20 74   /* If writing t
2c0b5 6f 20 6d 65 6d 6f 72 79 20 6f 72 20 67 65 6e 65  o memory or gene
2c0b6 72 61 74 69 6e 67 20 61 20 73 65 74 0a 20 20 2a  rating a set.  *
2c0b7 2a 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20  * only a single 
2c0b8 63 6f 6c 75 6d 6e 20 6d 61 79 20 62 65 20 6f 75  column may be ou
2c0b9 74 70 75 74 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64  tput..  */.#ifnd
2c0ba 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
2c0bb 55 42 51 55 45 52 59 0a 20 20 69 66 28 20 63 68  UBQUERY.  if( ch
2c0bc 65 63 6b 46 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d  eckForMultiColum
2c0bd 6e 53 65 6c 65 63 74 45 72 72 6f 72 28 70 50 61  nSelectError(pPa
2c0be 72 73 65 2c 20 70 44 65 73 74 2c 20 70 45 4c 69  rse, pDest, pELi
2c0bf 73 74 2d 3e 6e 45 78 70 72 29 20 29 7b 0a 20 20  st->nExpr) ){.  
2c0c0 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e    goto select_en
2c0c1 64 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  d;.  }.#endif.. 
2c0c2 20 2f 2a 20 49 66 20 70 6f 73 73 69 62 6c 65 2c   /* If possible,
2c0c3 20 72 65 77 72 69 74 65 20 74 68 65 20 71 75 65   rewrite the que
2c0c4 72 79 20 74 6f 20 75 73 65 20 47 52 4f 55 50 20  ry to use GROUP 
2c0c5 42 59 20 69 6e 73 74 65 61 64 20 6f 66 20 44 49  BY instead of DI
2c0c6 53 54 49 4e 43 54 2e 0a 20 20 2a 2a 20 47 52 4f  STINCT..  ** GRO
2c0c7 55 50 20 42 59 20 6d 69 67 68 74 20 75 73 65 20  UP BY might use 
2c0c8 61 6e 20 69 6e 64 65 78 2c 20 44 49 53 54 49 4e  an index, DISTIN
2c0c9 43 54 20 6e 65 76 65 72 20 64 6f 65 73 2e 0a 20  CT never does.. 
2c0ca 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d   */.  assert( p-
2c0cb 3e 70 47 72 6f 75 70 42 79 3d 3d 30 20 7c 7c 20  >pGroupBy==0 || 
2c0cc 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53  (p->selFlags & S
2c0cd 46 5f 41 67 67 72 65 67 61 74 65 29 21 3d 30 20  F_Aggregate)!=0 
2c0ce 29 3b 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c  );.  if( (p->sel
2c0cf 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74  Flags & (SF_Dist
2c0d0 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74  inct|SF_Aggregat
2c0d1 65 29 29 3d 3d 53 46 5f 44 69 73 74 69 6e 63 74  e))==SF_Distinct
2c0d2 20 29 7b 0a 20 20 20 20 70 2d 3e 70 47 72 6f 75   ){.    p->pGrou
2c0d3 70 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70  pBy = sqlite3Exp
2c0d4 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e  rListDup(db, p->
2c0d5 70 45 4c 69 73 74 2c 20 30 29 3b 0a 20 20 20 20  pEList, 0);.    
2c0d6 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47  pGroupBy = p->pG
2c0d7 72 6f 75 70 42 79 3b 0a 20 20 20 20 70 2d 3e 73  roupBy;.    p->s
2c0d8 65 6c 46 6c 61 67 73 20 26 3d 20 7e 53 46 5f 44  elFlags &= ~SF_D
2c0d9 69 73 74 69 6e 63 74 3b 0a 20 20 20 20 69 73 44  istinct;.    isD
2c0da 69 73 74 69 6e 63 74 20 3d 20 30 3b 0a 20 20 7d  istinct = 0;.  }
2c0db 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
2c0dc 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63  is an ORDER BY c
2c0dd 6c 61 75 73 65 2c 20 74 68 65 6e 20 74 68 69 73  lause, then this
2c0de 20 73 6f 72 74 69 6e 67 0a 20 20 2a 2a 20 69 6e   sorting.  ** in
2c0df 64 65 78 20 6d 69 67 68 74 20 65 6e 64 20 75 70  dex might end up
2c0e0 20 62 65 69 6e 67 20 75 6e 75 73 65 64 20 69 66   being unused if
2c0e1 20 74 68 65 20 64 61 74 61 20 63 61 6e 20 62 65   the data can be
2c0e2 20 0a 20 20 2a 2a 20 65 78 74 72 61 63 74 65 64   .  ** extracted
2c0e3 20 69 6e 20 70 72 65 2d 73 6f 72 74 65 64 20 6f   in pre-sorted o
2c0e4 72 64 65 72 2e 20 20 49 66 20 74 68 61 74 20 69  rder.  If that i
2c0e5 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e  s the case, then
2c0e6 20 74 68 65 0a 20 20 2a 2a 20 4f 50 5f 4f 70 65   the.  ** OP_Ope
2c0e7 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72  nEphemeral instr
2c0e8 75 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63  uction will be c
2c0e9 68 61 6e 67 65 64 20 74 6f 20 61 6e 20 4f 50 5f  hanged to an OP_
2c0ea 4e 6f 6f 70 20 6f 6e 63 65 0a 20 20 2a 2a 20 77  Noop once.  ** w
2c0eb 65 20 66 69 67 75 72 65 20 6f 75 74 20 74 68 61  e figure out tha
2c0ec 74 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e  t the sorting in
2c0ed 64 65 78 20 69 73 20 6e 6f 74 20 6e 65 65 64 65  dex is not neede
2c0ee 64 2e 20 20 54 68 65 20 61 64 64 72 53 6f 72 74  d.  The addrSort
2c0ef 49 6e 64 65 78 0a 20 20 2a 2a 20 76 61 72 69 61  Index.  ** varia
2c0f0 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 66  ble is used to f
2c0f1 61 63 69 6c 69 74 61 74 65 20 74 68 61 74 20 63  acilitate that c
2c0f2 68 61 6e 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  hange..  */.  if
2c0f3 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ( pOrderBy ){.  
2c0f4 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
2c0f5 6e 66 6f 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66  nfo;.    pKeyInf
2c0f6 6f 20 3d 20 6b 65 79 49 6e 66 6f 46 72 6f 6d 45  o = keyInfoFromE
2c0f7 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20  xprList(pParse, 
2c0f8 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20 20 70  pOrderBy);.    p
2c0f9 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f  OrderBy->iECurso
2c0fa 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  r = pParse->nTab
2c0fb 2b 2b 3b 0a 20 20 20 20 70 2d 3e 61 64 64 72 4f  ++;.    p->addrO
2c0fc 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 61 64 64  penEphm[2] = add
2c0fd 72 53 6f 72 74 49 6e 64 65 78 20 3d 0a 20 20 20  rSortIndex =.   
2c0fe 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c0ff 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 45  dOp4(v, OP_OpenE
2c100 70 68 65 6d 65 72 61 6c 2c 0a 20 20 20 20 20 20  phemeral,.      
2c101 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c102 20 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e 69       pOrderBy->i
2c103 45 43 75 72 73 6f 72 2c 20 70 4f 72 64 65 72 42  ECursor, pOrderB
2c104 79 2d 3e 6e 45 78 70 72 2b 32 2c 20 30 2c 0a 20  y->nExpr+2, 0,. 
2c105 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c106 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a            (char*
2c107 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45  )pKeyInfo, P4_KE
2c108 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a  YINFO_HANDOFF);.
2c109 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 64 64    }else{.    add
2c10a 72 53 6f 72 74 49 6e 64 65 78 20 3d 20 2d 31 3b  rSortIndex = -1;
2c10b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
2c10c 65 20 6f 75 74 70 75 74 20 69 73 20 64 65 73 74  e output is dest
2c10d 69 6e 65 64 20 66 6f 72 20 61 20 74 65 6d 70 6f  ined for a tempo
2c10e 72 61 72 79 20 74 61 62 6c 65 2c 20 6f 70 65 6e  rary table, open
2c10f 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 20 20 2a   that table..  *
2c110 2f 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 65  /.  if( pDest->e
2c111 44 65 73 74 3d 3d 53 52 54 5f 45 70 68 65 6d 54  Dest==SRT_EphemT
2c112 61 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ab ){.    sqlite
2c113 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2c114 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c  P_OpenEphemeral,
2c115 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2c 20 70   pDest->iParm, p
2c116 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20  EList->nExpr);. 
2c117 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65   }..  /* Set the
2c118 20 6c 69 6d 69 74 65 72 2e 0a 20 20 2a 2f 0a 20   limiter..  */. 
2c119 20 69 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 56   iEnd = sqlite3V
2c11a 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
2c11b 0a 20 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52  .  computeLimitR
2c11c 65 67 69 73 74 65 72 73 28 70 50 61 72 73 65 2c  egisters(pParse,
2c11d 20 70 2c 20 69 45 6e 64 29 3b 0a 0a 20 20 2f 2a   p, iEnd);..  /*
2c11e 20 4f 70 65 6e 20 61 20 76 69 72 74 75 61 6c 20   Open a virtual 
2c11f 69 6e 64 65 78 20 74 6f 20 75 73 65 20 66 6f 72  index to use for
2c120 20 74 68 65 20 64 69 73 74 69 6e 63 74 20 73 65   the distinct se
2c121 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73  t..  */.  if( is
2c122 44 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20  Distinct ){.    
2c123 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
2c124 6f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  o;.    assert( i
2c125 73 41 67 67 20 7c 7c 20 70 47 72 6f 75 70 42 79  sAgg || pGroupBy
2c126 20 29 3b 0a 20 20 20 20 64 69 73 74 69 6e 63 74   );.    distinct
2c127 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
2c128 2b 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20  +;.    pKeyInfo 
2c129 3d 20 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70  = keyInfoFromExp
2c12a 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 2d  rList(pParse, p-
2c12b 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 73 71  >pEList);.    sq
2c12c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2c12d 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  v, OP_OpenEpheme
2c12e 72 61 6c 2c 20 64 69 73 74 69 6e 63 74 2c 20 30  ral, distinct, 0
2c12f 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
2c130 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68               (ch
2c131 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34  ar*)pKeyInfo, P4
2c132 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
2c133 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
2c134 64 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b 0a 20  distinct = -1;. 
2c135 20 7d 0a 0a 20 20 2f 2a 20 41 67 67 72 65 67 61   }..  /* Aggrega
2c136 74 65 20 61 6e 64 20 6e 6f 6e 2d 61 67 67 72 65  te and non-aggre
2c137 67 61 74 65 20 71 75 65 72 69 65 73 20 61 72 65  gate queries are
2c138 20 68 61 6e 64 6c 65 64 20 64 69 66 66 65 72 65   handled differe
2c139 6e 74 6c 79 20 2a 2f 0a 20 20 69 66 28 20 21 69  ntly */.  if( !i
2c13a 73 41 67 67 20 26 26 20 70 47 72 6f 75 70 42 79  sAgg && pGroupBy
2c13b 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ==0 ){.    /* Th
2c13c 69 73 20 63 61 73 65 20 69 73 20 66 6f 72 20 6e  is case is for n
2c13d 6f 6e 2d 61 67 67 72 65 67 61 74 65 20 71 75 65  on-aggregate que
2c13e 72 69 65 73 0a 20 20 20 20 2a 2a 20 42 65 67 69  ries.    ** Begi
2c13f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  n the database s
2c140 63 61 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  can.    */.    p
2c141 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57  WInfo = sqlite3W
2c142 68 65 72 65 42 65 67 69 6e 28 70 50 61 72 73 65  hereBegin(pParse
2c143 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 57 68 65  , pTabList, pWhe
2c144 72 65 2c 20 26 70 4f 72 64 65 72 42 79 2c 20 30  re, &pOrderBy, 0
2c145 29 3b 0a 20 20 20 20 69 66 28 20 70 57 49 6e 66  );.    if( pWInf
2c146 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c 65  o==0 ) goto sele
2c147 63 74 5f 65 6e 64 3b 0a 0a 20 20 20 20 2f 2a 20  ct_end;..    /* 
2c148 49 66 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78  If sorting index
2c149 20 74 68 61 74 20 77 61 73 20 63 72 65 61 74 65   that was create
2c14a 64 20 62 79 20 61 20 70 72 69 6f 72 20 4f 50 5f  d by a prior OP_
2c14b 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 0a 20  OpenEphemeral . 
2c14c 20 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f     ** instructio
2c14d 6e 20 65 6e 64 65 64 20 75 70 20 6e 6f 74 20 62  n ended up not b
2c14e 65 69 6e 67 20 6e 65 65 64 65 64 2c 20 74 68 65  eing needed, the
2c14f 6e 20 63 68 61 6e 67 65 20 74 68 65 20 4f 50 5f  n change the OP_
2c150 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 0a 20 20  OpenEphemeral.  
2c151 20 20 2a 2a 20 69 6e 74 6f 20 61 6e 20 4f 50 5f    ** into an OP_
2c152 4e 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  Noop..    */.   
2c153 20 69 66 28 20 61 64 64 72 53 6f 72 74 49 6e 64   if( addrSortInd
2c154 65 78 3e 3d 30 20 26 26 20 70 4f 72 64 65 72 42  ex>=0 && pOrderB
2c155 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  y==0 ){.      sq
2c156 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54  lite3VdbeChangeT
2c157 6f 4e 6f 6f 70 28 76 2c 20 61 64 64 72 53 6f 72  oNoop(v, addrSor
2c158 74 49 6e 64 65 78 2c 20 31 29 3b 0a 20 20 20 20  tIndex, 1);.    
2c159 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68    p->addrOpenEph
2c15a 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20 20 20 7d  m[2] = -1;.    }
2c15b 0a 0a 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65  ..    /* Use the
2c15c 20 73 74 61 6e 64 61 72 64 20 69 6e 6e 65 72 20   standard inner 
2c15d 6c 6f 6f 70 0a 20 20 20 20 2a 2f 0a 20 20 20 20  loop.    */.    
2c15e 61 73 73 65 72 74 28 21 69 73 44 69 73 74 69 6e  assert(!isDistin
2c15f 63 74 29 3b 0a 20 20 20 20 73 65 6c 65 63 74 49  ct);.    selectI
2c160 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c  nnerLoop(pParse,
2c161 20 70 2c 20 70 45 4c 69 73 74 2c 20 30 2c 20 30   p, pEList, 0, 0
2c162 2c 20 70 4f 72 64 65 72 42 79 2c 20 2d 31 2c 20  , pOrderBy, -1, 
2c163 70 44 65 73 74 2c 0a 20 20 20 20 20 20 20 20 20  pDest,.         
2c164 20 20 20 20 20 20 20 20 20 20 20 70 57 49 6e 66             pWInf
2c165 6f 2d 3e 69 43 6f 6e 74 69 6e 75 65 2c 20 70 57  o->iContinue, pW
2c166 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 29 3b 0a 0a  Info->iBreak);..
2c167 20 20 20 20 2f 2a 20 45 6e 64 20 74 68 65 20 64      /* End the d
2c168 61 74 61 62 61 73 65 20 73 63 61 6e 20 6c 6f 6f  atabase scan loo
2c169 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  p..    */.    sq
2c16a 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 57  lite3WhereEnd(pW
2c16b 49 6e 66 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Info);.  }else{.
2c16c 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74      /* This is t
2c16d 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f  he processing fo
2c16e 72 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72  r aggregate quer
2c16f 69 65 73 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43  ies */.    NameC
2c170 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 2f  ontext sNC;    /
2c171 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66  * Name context f
2c172 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 20 61 67  or processing ag
2c173 67 72 65 67 61 74 65 20 69 6e 66 6f 72 6d 61 74  gregate informat
2c174 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ion */.    int i
2c175 41 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f  AMem;          /
2c176 2a 20 46 69 72 73 74 20 4d 65 6d 20 61 64 64 72  * First Mem addr
2c177 65 73 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 20  ess for storing 
2c178 63 75 72 72 65 6e 74 20 47 52 4f 55 50 20 42 59  current GROUP BY
2c179 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 42 4d 65   */.    int iBMe
2c17a 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  m;          /* F
2c17b 69 72 73 74 20 4d 65 6d 20 61 64 64 72 65 73 73  irst Mem address
2c17c 20 66 6f 72 20 70 72 65 76 69 6f 75 73 20 47 52   for previous GR
2c17d 4f 55 50 20 42 59 20 2a 2f 0a 20 20 20 20 69 6e  OUP BY */.    in
2c17e 74 20 69 55 73 65 46 6c 61 67 3b 20 20 20 20 20  t iUseFlag;     
2c17f 20 20 2f 2a 20 4d 65 6d 20 61 64 64 72 65 73 73    /* Mem address
2c180 20 68 6f 6c 64 69 6e 67 20 66 6c 61 67 20 69 6e   holding flag in
2c181 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 61 74  dicating that at
2c182 20 6c 65 61 73 74 0a 20 20 20 20 20 20 20 20 20   least.         
2c183 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
2c184 2a 20 6f 6e 65 20 72 6f 77 20 6f 66 20 74 68 65  * one row of the
2c185 20 69 6e 70 75 74 20 74 6f 20 74 68 65 20 61 67   input to the ag
2c186 67 72 65 67 61 74 6f 72 20 68 61 73 20 62 65 65  gregator has bee
2c187 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n.              
2c188 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f            ** pro
2c189 63 65 73 73 65 64 20 2a 2f 0a 20 20 20 20 69 6e  cessed */.    in
2c18a 74 20 69 41 62 6f 72 74 46 6c 61 67 3b 20 20 20  t iAbortFlag;   
2c18b 20 20 2f 2a 20 4d 65 6d 20 61 64 64 72 65 73 73    /* Mem address
2c18c 20 77 68 69 63 68 20 63 61 75 73 65 73 20 71 75   which causes qu
2c18d 65 72 79 20 61 62 6f 72 74 20 69 66 20 70 6f 73  ery abort if pos
2c18e 69 74 69 76 65 20 2a 2f 0a 20 20 20 20 69 6e 74  itive */.    int
2c18f 20 67 72 6f 75 70 42 79 53 6f 72 74 3b 20 20 20   groupBySort;   
2c190 20 2f 2a 20 52 6f 77 73 20 63 6f 6d 65 20 66 72   /* Rows come fr
2c191 6f 6d 20 73 6f 75 72 63 65 20 69 6e 20 47 52 4f  om source in GRO
2c192 55 50 20 42 59 20 6f 72 64 65 72 20 2a 2f 0a 20  UP BY order */. 
2c193 20 20 20 69 6e 74 20 61 64 64 72 45 6e 64 3b 20     int addrEnd; 
2c194 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66         /* End of
2c195 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20   processing for 
2c196 74 68 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 0a  this SELECT */..
2c197 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 61 6e      /* Remove an
2c198 79 20 61 6e 64 20 61 6c 6c 20 61 6c 69 61 73 65  y and all aliase
2c199 73 20 62 65 74 77 65 65 6e 20 74 68 65 20 72 65  s between the re
2c19a 73 75 6c 74 20 73 65 74 20 61 6e 64 20 74 68 65  sult set and the
2c19b 0a 20 20 20 20 2a 2a 20 47 52 4f 55 50 20 42 59  .    ** GROUP BY
2c19c 20 63 6c 61 75 73 65 2e 0a 20 20 20 20 2a 2f 0a   clause..    */.
2c19d 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42 79      if( pGroupBy
2c19e 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 3b   ){.      int k;
2c19f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c1a0 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
2c1a1 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20  counter */.     
2c1a2 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
2c1a3 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20 2f  _item *pItem;  /
2c1a4 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76  * For looping ov
2c1a5 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  er expression in
2c1a6 20 61 20 6c 69 73 74 20 2a 2f 0a 0a 20 20 20 20   a list */..    
2c1a7 20 20 66 6f 72 28 6b 3d 70 2d 3e 70 45 4c 69 73    for(k=p->pELis
2c1a8 74 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d 3d  t->nExpr, pItem=
2c1a9 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 3b 20 6b 3e  p->pEList->a; k>
2c1aa 30 3b 20 6b 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29  0; k--, pItem++)
2c1ab 7b 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d  {.        pItem-
2c1ac 3e 69 41 6c 69 61 73 20 3d 20 30 3b 0a 20 20 20  >iAlias = 0;.   
2c1ad 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 6b     }.      for(k
2c1ae 3d 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72  =pGroupBy->nExpr
2c1af 2c 20 70 49 74 65 6d 3d 70 47 72 6f 75 70 42 79  , pItem=pGroupBy
2c1b0 2d 3e 61 3b 20 6b 3e 30 3b 20 6b 2d 2d 2c 20 70  ->a; k>0; k--, p
2c1b1 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Item++){.       
2c1b2 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d   pItem->iAlias =
2c1b3 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
2c1b4 7d 0a 0a 20 0a 20 20 20 20 2f 2a 20 43 72 65 61  }.. .    /* Crea
2c1b5 74 65 20 61 20 6c 61 62 65 6c 20 74 6f 20 6a 75  te a label to ju
2c1b6 6d 70 20 74 6f 20 77 68 65 6e 20 77 65 20 77 61  mp to when we wa
2c1b7 6e 74 20 74 6f 20 61 62 6f 72 74 20 74 68 65 20  nt to abort the 
2c1b8 71 75 65 72 79 20 2a 2f 0a 20 20 20 20 61 64 64  query */.    add
2c1b9 72 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 56 64  rEnd = sqlite3Vd
2c1ba 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
2c1bb 0a 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20  .    /* Convert 
2c1bc 54 4b 5f 43 4f 4c 55 4d 4e 20 6e 6f 64 65 73 20  TK_COLUMN nodes 
2c1bd 69 6e 74 6f 20 54 4b 5f 41 47 47 5f 43 4f 4c 55  into TK_AGG_COLU
2c1be 4d 4e 20 61 6e 64 20 6d 61 6b 65 20 65 6e 74 72  MN and make entr
2c1bf 69 65 73 20 69 6e 0a 20 20 20 20 2a 2a 20 73 41  ies in.    ** sA
2c1c0 67 67 49 6e 66 6f 20 66 6f 72 20 61 6c 6c 20 54  ggInfo for all T
2c1c1 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 6e  K_AGG_FUNCTION n
2c1c2 6f 64 65 73 20 69 6e 20 65 78 70 72 65 73 73 69  odes in expressi
2c1c3 6f 6e 73 20 6f 66 20 74 68 65 0a 20 20 20 20 2a  ons of the.    *
2c1c4 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  * SELECT stateme
2c1c5 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d  nt..    */.    m
2c1c6 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73  emset(&sNC, 0, s
2c1c7 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 20  izeof(sNC));.   
2c1c8 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50   sNC.pParse = pP
2c1c9 61 72 73 65 3b 0a 20 20 20 20 73 4e 43 2e 70 53  arse;.    sNC.pS
2c1ca 72 63 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73  rcList = pTabLis
2c1cb 74 3b 0a 20 20 20 20 73 4e 43 2e 70 41 67 67 49  t;.    sNC.pAggI
2c1cc 6e 66 6f 20 3d 20 26 73 41 67 67 49 6e 66 6f 3b  nfo = &sAggInfo;
2c1cd 0a 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 6e 53  .    sAggInfo.nS
2c1ce 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 20 3d 20 70  ortingColumn = p
2c1cf 47 72 6f 75 70 42 79 20 3f 20 70 47 72 6f 75 70  GroupBy ? pGroup
2c1d0 42 79 2d 3e 6e 45 78 70 72 2b 31 20 3a 20 30 3b  By->nExpr+1 : 0;
2c1d1 0a 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 70 47  .    sAggInfo.pG
2c1d2 72 6f 75 70 42 79 20 3d 20 70 47 72 6f 75 70 42  roupBy = pGroupB
2c1d3 79 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  y;.    sqlite3Ex
2c1d4 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74  prAnalyzeAggList
2c1d5 28 26 73 4e 43 2c 20 70 45 4c 69 73 74 29 3b 0a  (&sNC, pEList);.
2c1d6 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 41      sqlite3ExprA
2c1d7 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 26 73  nalyzeAggList(&s
2c1d8 4e 43 2c 20 70 4f 72 64 65 72 42 79 29 3b 0a 20  NC, pOrderBy);. 
2c1d9 20 20 20 69 66 28 20 70 48 61 76 69 6e 67 20 29     if( pHaving )
2c1da 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
2c1db 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67  xprAnalyzeAggreg
2c1dc 61 74 65 73 28 26 73 4e 43 2c 20 70 48 61 76 69  ates(&sNC, pHavi
2c1dd 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ng);.    }.    s
2c1de 41 67 67 49 6e 66 6f 2e 6e 41 63 63 75 6d 75 6c  AggInfo.nAccumul
2c1df 61 74 6f 72 20 3d 20 73 41 67 67 49 6e 66 6f 2e  ator = sAggInfo.
2c1e0 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 66 6f 72  nColumn;.    for
2c1e1 28 69 3d 30 3b 20 69 3c 73 41 67 67 49 6e 66 6f  (i=0; i<sAggInfo
2c1e2 2e 6e 46 75 6e 63 3b 20 69 2b 2b 29 7b 0a 20 20  .nFunc; i++){.  
2c1e3 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
2c1e4 72 48 61 73 50 72 6f 70 65 72 74 79 28 73 41 67  rHasProperty(sAg
2c1e5 67 49 6e 66 6f 2e 61 46 75 6e 63 5b 69 5d 2e 70  gInfo.aFunc[i].p
2c1e6 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65  Expr, EP_xIsSele
2c1e7 63 74 29 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ct) );.      sql
2c1e8 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41  ite3ExprAnalyzeA
2c1e9 67 67 4c 69 73 74 28 26 73 4e 43 2c 20 73 41 67  ggList(&sNC, sAg
2c1ea 67 49 6e 66 6f 2e 61 46 75 6e 63 5b 69 5d 2e 70  gInfo.aFunc[i].p
2c1eb 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a  Expr->x.pList);.
2c1ec 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62      }.    if( db
2c1ed 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2c1ee 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64   goto select_end
2c1ef 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65 73  ;..    /* Proces
2c1f0 73 69 6e 67 20 66 6f 72 20 61 67 67 72 65 67 61  sing for aggrega
2c1f1 74 65 73 20 77 69 74 68 20 47 52 4f 55 50 20 42  tes with GROUP B
2c1f2 59 20 69 73 20 76 65 72 79 20 64 69 66 66 65 72  Y is very differ
2c1f3 65 6e 74 20 61 6e 64 0a 20 20 20 20 2a 2a 20 6d  ent and.    ** m
2c1f4 75 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 78  uch more complex
2c1f5 20 74 68 61 6e 20 61 67 67 72 65 67 61 74 65 73   than aggregates
2c1f6 20 77 69 74 68 6f 75 74 20 61 20 47 52 4f 55 50   without a GROUP
2c1f7 20 42 59 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20   BY..    */.    
2c1f8 69 66 28 20 70 47 72 6f 75 70 42 79 20 29 7b 0a  if( pGroupBy ){.
2c1f9 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70        KeyInfo *p
2c1fa 4b 65 79 49 6e 66 6f 3b 20 20 2f 2a 20 4b 65 79  KeyInfo;  /* Key
2c1fb 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ing information 
2c1fc 66 6f 72 20 74 68 65 20 67 72 6f 75 70 20 62 79  for the group by
2c1fd 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20   clause */.     
2c1fe 20 69 6e 74 20 6a 31 3b 20 20 20 20 20 20 20 20   int j1;        
2c1ff 20 20 20 20 20 2f 2a 20 41 2d 76 73 2d 42 20 63       /* A-vs-B c
2c200 6f 6d 70 61 72 69 73 69 6f 6e 20 6a 75 6d 70 20  omparision jump 
2c201 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64  */.      int add
2c202 72 4f 75 74 70 75 74 52 6f 77 3b 20 20 2f 2a 20  rOutputRow;  /* 
2c203 53 74 61 72 74 20 6f 66 20 73 75 62 72 6f 75 74  Start of subrout
2c204 69 6e 65 20 74 68 61 74 20 6f 75 74 70 75 74 73  ine that outputs
2c205 20 61 20 72 65 73 75 6c 74 20 72 6f 77 20 2a 2f   a result row */
2c206 0a 20 20 20 20 20 20 69 6e 74 20 72 65 67 4f 75  .      int regOu
2c207 74 70 75 74 52 6f 77 3b 20 20 20 2f 2a 20 52 65  tputRow;   /* Re
2c208 74 75 72 6e 20 61 64 64 72 65 73 73 20 72 65 67  turn address reg
2c209 69 73 74 65 72 20 66 6f 72 20 6f 75 74 70 75 74  ister for output
2c20a 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20   subroutine */. 
2c20b 20 20 20 20 20 69 6e 74 20 61 64 64 72 53 65 74       int addrSet
2c20c 41 62 6f 72 74 3b 20 20 20 2f 2a 20 53 65 74 20  Abort;   /* Set 
2c20d 74 68 65 20 61 62 6f 72 74 20 66 6c 61 67 20 61  the abort flag a
2c20e 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 20  nd return */.   
2c20f 20 20 20 69 6e 74 20 61 64 64 72 54 6f 70 4f 66     int addrTopOf
2c210 4c 6f 6f 70 3b 20 20 2f 2a 20 54 6f 70 20 6f 66  Loop;  /* Top of
2c211 20 74 68 65 20 69 6e 70 75 74 20 6c 6f 6f 70 20   the input loop 
2c212 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64  */.      int add
2c213 72 53 6f 72 74 69 6e 67 49 64 78 3b 20 2f 2a 20  rSortingIdx; /* 
2c214 54 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d  The OP_OpenEphem
2c215 65 72 61 6c 20 66 6f 72 20 74 68 65 20 73 6f 72  eral for the sor
2c216 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20  ting index */.  
2c217 20 20 20 20 69 6e 74 20 61 64 64 72 52 65 73 65      int addrRese
2c218 74 3b 20 20 20 20 20 20 2f 2a 20 53 75 62 72 6f  t;      /* Subro
2c219 75 74 69 6e 65 20 66 6f 72 20 72 65 73 65 74 74  utine for resett
2c21a 69 6e 67 20 74 68 65 20 61 63 63 75 6d 75 6c 61  ing the accumula
2c21b 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  tor */.      int
2c21c 20 72 65 67 52 65 73 65 74 3b 20 20 20 20 20 20   regReset;      
2c21d 20 2f 2a 20 52 65 74 75 72 6e 20 61 64 64 72 65   /* Return addre
2c21e 73 73 20 72 65 67 69 73 74 65 72 20 66 6f 72 20  ss register for 
2c21f 72 65 73 65 74 20 73 75 62 72 6f 75 74 69 6e 65  reset subroutine
2c220 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66   */..      /* If
2c221 20 74 68 65 72 65 20 69 73 20 61 20 47 52 4f 55   there is a GROU
2c222 50 20 42 59 20 63 6c 61 75 73 65 20 77 65 20 6d  P BY clause we m
2c223 69 67 68 74 20 6e 65 65 64 20 61 20 73 6f 72 74  ight need a sort
2c224 69 6e 67 20 69 6e 64 65 78 20 74 6f 0a 20 20 20  ing index to.   
2c225 20 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20     ** implement 
2c226 69 74 2e 20 20 41 6c 6c 6f 63 61 74 65 20 74 68  it.  Allocate th
2c227 61 74 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78  at sorting index
2c228 20 6e 6f 77 2e 20 20 49 66 20 69 74 20 74 75 72   now.  If it tur
2c229 6e 73 20 6f 75 74 0a 20 20 20 20 20 20 2a 2a 20  ns out.      ** 
2c22a 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 20 6e  that we do not n
2c22b 65 65 64 20 69 74 20 61 66 74 65 72 20 61 6c 6c  eed it after all
2c22c 2c 20 74 68 65 20 4f 70 65 6e 45 70 68 65 6d 65  , the OpenEpheme
2c22d 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a  ral instruction.
2c22e 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65        ** will be
2c22f 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20   converted into 
2c230 61 20 4e 6f 6f 70 2e 20 20 0a 20 20 20 20 20 20  a Noop.  .      
2c231 2a 2f 0a 20 20 20 20 20 20 73 41 67 67 49 6e 66  */.      sAggInf
2c232 6f 2e 73 6f 72 74 69 6e 67 49 64 78 20 3d 20 70  o.sortingIdx = p
2c233 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
2c234 20 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20       pKeyInfo = 
2c235 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c  keyInfoFromExprL
2c236 69 73 74 28 70 50 61 72 73 65 2c 20 70 47 72 6f  ist(pParse, pGro
2c237 75 70 42 79 29 3b 0a 20 20 20 20 20 20 61 64 64  upBy);.      add
2c238 72 53 6f 72 74 69 6e 67 49 64 78 20 3d 20 73 71  rSortingIdx = sq
2c239 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2c23a 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  v, OP_OpenEpheme
2c23b 72 61 6c 2c 20 0a 20 20 20 20 20 20 20 20 20 20  ral, .          
2c23c 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67  sAggInfo.sorting
2c23d 49 64 78 2c 20 73 41 67 67 49 6e 66 6f 2e 6e 53  Idx, sAggInfo.nS
2c23e 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 2c 20 0a 20  ortingColumn, . 
2c23f 20 20 20 20 20 20 20 20 20 30 2c 20 28 63 68 61           0, (cha
2c240 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34 5f  r*)pKeyInfo, P4_
2c241 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
2c242 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 6e 69 74  ;..      /* Init
2c243 69 61 6c 69 7a 65 20 6d 65 6d 6f 72 79 20 6c 6f  ialize memory lo
2c244 63 61 74 69 6f 6e 73 20 75 73 65 64 20 62 79 20  cations used by 
2c245 47 52 4f 55 50 20 42 59 20 61 67 67 72 65 67 61  GROUP BY aggrega
2c246 74 65 20 70 72 6f 63 65 73 73 69 6e 67 0a 20 20  te processing.  
2c247 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 55 73      */.      iUs
2c248 65 46 6c 61 67 20 3d 20 2b 2b 70 50 61 72 73 65  eFlag = ++pParse
2c249 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 69 41  ->nMem;.      iA
2c24a 62 6f 72 74 46 6c 61 67 20 3d 20 2b 2b 70 50 61  bortFlag = ++pPa
2c24b 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20  rse->nMem;.     
2c24c 20 72 65 67 4f 75 74 70 75 74 52 6f 77 20 3d 20   regOutputRow = 
2c24d 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2c24e 20 20 20 20 20 20 61 64 64 72 4f 75 74 70 75 74        addrOutput
2c24f 52 6f 77 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Row = sqlite3Vdb
2c250 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
2c251 20 20 20 20 20 72 65 67 52 65 73 65 74 20 3d 20       regReset = 
2c252 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2c253 20 20 20 20 20 20 61 64 64 72 52 65 73 65 74 20        addrReset 
2c254 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
2c255 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
2c256 20 69 41 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d   iAMem = pParse-
2c257 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 20 20 20  >nMem + 1;.     
2c258 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d   pParse->nMem +=
2c259 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72   pGroupBy->nExpr
2c25a 3b 0a 20 20 20 20 20 20 69 42 4d 65 6d 20 3d 20  ;.      iBMem = 
2c25b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31  pParse->nMem + 1
2c25c 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
2c25d 6e 4d 65 6d 20 2b 3d 20 70 47 72 6f 75 70 42 79  nMem += pGroupBy
2c25e 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 73  ->nExpr;.      s
2c25f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2c260 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2c261 30 2c 20 69 41 62 6f 72 74 46 6c 61 67 29 3b 0a  0, iAbortFlag);.
2c262 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
2c263 74 28 28 76 2c 20 22 63 6c 65 61 72 20 61 62 6f  t((v, "clear abo
2c264 72 74 20 66 6c 61 67 22 29 29 3b 0a 20 20 20 20  rt flag"));.    
2c265 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c266 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2c267 72 2c 20 30 2c 20 69 55 73 65 46 6c 61 67 29 3b  r, 0, iUseFlag);
2c268 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
2c269 6e 74 28 28 76 2c 20 22 69 6e 64 69 63 61 74 65  nt((v, "indicate
2c26a 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 65 6d 70   accumulator emp
2c26b 74 79 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  ty"));..      /*
2c26c 20 42 65 67 69 6e 20 61 20 6c 6f 6f 70 20 74 68   Begin a loop th
2c26d 61 74 20 77 69 6c 6c 20 65 78 74 72 61 63 74 20  at will extract 
2c26e 61 6c 6c 20 73 6f 75 72 63 65 20 72 6f 77 73 20  all source rows 
2c26f 69 6e 20 47 52 4f 55 50 20 42 59 20 6f 72 64 65  in GROUP BY orde
2c270 72 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73  r..      ** This
2c271 20 6d 69 67 68 74 20 69 6e 76 6f 6c 76 65 20 74   might involve t
2c272 77 6f 20 73 65 70 61 72 61 74 65 20 6c 6f 6f 70  wo separate loop
2c273 73 20 77 69 74 68 20 61 6e 20 4f 50 5f 53 6f 72  s with an OP_Sor
2c274 74 20 69 6e 20 62 65 74 77 65 65 6e 2c 20 6f 72  t in between, or
2c275 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 6d 69 67  .      ** it mig
2c276 68 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20 6c  ht be a single l
2c277 6f 6f 70 20 74 68 61 74 20 75 73 65 73 20 61 6e  oop that uses an
2c278 20 69 6e 64 65 78 20 74 6f 20 65 78 74 72 61 63   index to extrac
2c279 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 20 20  t information.  
2c27a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 69      ** in the ri
2c27b 67 68 74 20 6f 72 64 65 72 20 74 6f 20 62 65 67  ght order to beg
2c27c 69 6e 20 77 69 74 68 2e 0a 20 20 20 20 20 20 2a  in with..      *
2c27d 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  /.      sqlite3V
2c27e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c27f 47 6f 73 75 62 2c 20 72 65 67 52 65 73 65 74 2c  Gosub, regReset,
2c280 20 61 64 64 72 52 65 73 65 74 29 3b 0a 20 20 20   addrReset);.   
2c281 20 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69     pWInfo = sqli
2c282 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70 50  te3WhereBegin(pP
2c283 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20  arse, pTabList, 
2c284 70 57 68 65 72 65 2c 20 26 70 47 72 6f 75 70 42  pWhere, &pGroupB
2c285 79 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  y, 0);.      if(
2c286 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74   pWInfo==0 ) got
2c287 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  o select_end;.  
2c288 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42 79      if( pGroupBy
2c289 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ==0 ){.        /
2c28a 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a 65 72 20  * The optimizer 
2c28b 69 73 20 61 62 6c 65 20 74 6f 20 64 65 6c 69 76  is able to deliv
2c28c 65 72 20 72 6f 77 73 20 69 6e 20 67 72 6f 75 70  er rows in group
2c28d 20 62 79 20 6f 72 64 65 72 20 73 6f 0a 20 20 20   by order so.   
2c28e 20 20 20 20 20 2a 2a 20 77 65 20 64 6f 20 6e 6f       ** we do no
2c28f 74 20 68 61 76 65 20 74 6f 20 73 6f 72 74 2e 20  t have to sort. 
2c290 20 54 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65   The OP_OpenEphe
2c291 6d 65 72 61 6c 20 74 61 62 6c 65 20 77 69 6c 6c  meral table will
2c292 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 63   be.        ** c
2c293 61 6e 63 65 6c 6c 65 64 20 6c 61 74 65 72 20 62  ancelled later b
2c294 65 63 61 75 73 65 20 77 65 20 73 74 69 6c 6c 20  ecause we still 
2c295 6e 65 65 64 20 74 6f 20 75 73 65 20 74 68 65 20  need to use the 
2c296 70 4b 65 79 49 6e 66 6f 0a 20 20 20 20 20 20 20  pKeyInfo.       
2c297 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 47 72 6f   */.        pGro
2c298 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70  upBy = p->pGroup
2c299 42 79 3b 0a 20 20 20 20 20 20 20 20 67 72 6f 75  By;.        grou
2c29a 70 42 79 53 6f 72 74 20 3d 20 30 3b 0a 20 20 20  pBySort = 0;.   
2c29b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2c29c 20 20 2f 2a 20 52 6f 77 73 20 61 72 65 20 63 6f    /* Rows are co
2c29d 6d 69 6e 67 20 6f 75 74 20 69 6e 20 75 6e 64 65  ming out in unde
2c29e 74 65 72 6d 69 6e 65 64 20 6f 72 64 65 72 2e 20  termined order. 
2c29f 20 57 65 20 68 61 76 65 20 74 6f 20 70 75 73 68   We have to push
2c2a0 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 61 63 68  .        ** each
2c2a1 20 72 6f 77 20 69 6e 74 6f 20 61 20 73 6f 72 74   row into a sort
2c2a2 69 6e 67 20 69 6e 64 65 78 2c 20 74 65 72 6d 69  ing index, termi
2c2a3 6e 61 74 65 20 74 68 65 20 66 69 72 73 74 20 6c  nate the first l
2c2a4 6f 6f 70 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20  oop,.        ** 
2c2a5 74 68 65 6e 20 6c 6f 6f 70 20 6f 76 65 72 20 74  then loop over t
2c2a6 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78  he sorting index
2c2a7 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 74   in order to get
2c2a8 20 74 68 65 20 6f 75 74 70 75 74 0a 20 20 20 20   the output.    
2c2a9 20 20 20 20 2a 2a 20 69 6e 20 73 6f 72 74 65 64      ** in sorted
2c2aa 20 6f 72 64 65 72 0a 20 20 20 20 20 20 20 20 2a   order.        *
2c2ab 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 65  /.        int re
2c2ac 67 42 61 73 65 3b 0a 20 20 20 20 20 20 20 20 69  gBase;.        i
2c2ad 6e 74 20 72 65 67 52 65 63 6f 72 64 3b 0a 20 20  nt regRecord;.  
2c2ae 20 20 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a        int nCol;.
2c2af 20 20 20 20 20 20 20 20 69 6e 74 20 6e 47 72 6f          int nGro
2c2b0 75 70 42 79 3b 0a 0a 20 20 20 20 20 20 20 20 67  upBy;..        g
2c2b1 72 6f 75 70 42 79 53 6f 72 74 20 3d 20 31 3b 0a  roupBySort = 1;.
2c2b2 20 20 20 20 20 20 20 20 6e 47 72 6f 75 70 42 79          nGroupBy
2c2b3 20 3d 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78   = pGroupBy->nEx
2c2b4 70 72 3b 0a 20 20 20 20 20 20 20 20 6e 43 6f 6c  pr;.        nCol
2c2b5 20 3d 20 6e 47 72 6f 75 70 42 79 20 2b 20 31 3b   = nGroupBy + 1;
2c2b6 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 6e 47 72  .        j = nGr
2c2b7 6f 75 70 42 79 2b 31 3b 0a 20 20 20 20 20 20 20  oupBy+1;.       
2c2b8 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 41 67 67   for(i=0; i<sAgg
2c2b9 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  Info.nColumn; i+
2c2ba 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
2c2bb 28 20 73 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b  ( sAggInfo.aCol[
2c2bc 69 5d 2e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e  i].iSorterColumn
2c2bd 3e 3d 6a 20 29 7b 0a 20 20 20 20 20 20 20 20 20  >=j ){.         
2c2be 20 20 20 6e 43 6f 6c 2b 2b 3b 0a 20 20 20 20 20     nCol++;.     
2c2bf 20 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20         j++;.    
2c2c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2c2c1 7d 0a 20 20 20 20 20 20 20 20 72 65 67 42 61 73  }.        regBas
2c2c2 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  e = sqlite3GetTe
2c2c3 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
2c2c4 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 73  nCol);.        s
2c2c5 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43  qlite3ExprCacheC
2c2c6 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20  lear(pParse);.  
2c2c7 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2c2c8 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50  rCodeExprList(pP
2c2c9 61 72 73 65 2c 20 70 47 72 6f 75 70 42 79 2c 20  arse, pGroupBy, 
2c2ca 72 65 67 42 61 73 65 2c 20 30 29 3b 0a 20 20 20  regBase, 0);.   
2c2cb 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2c2cc 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 71  AddOp2(v, OP_Seq
2c2cd 75 65 6e 63 65 2c 20 73 41 67 67 49 6e 66 6f 2e  uence, sAggInfo.
2c2ce 73 6f 72 74 69 6e 67 49 64 78 2c 72 65 67 42 61  sortingIdx,regBa
2c2cf 73 65 2b 6e 47 72 6f 75 70 42 79 29 3b 0a 20 20  se+nGroupBy);.  
2c2d0 20 20 20 20 20 20 6a 20 3d 20 6e 47 72 6f 75 70        j = nGroup
2c2d1 42 79 2b 31 3b 0a 20 20 20 20 20 20 20 20 66 6f  By+1;.        fo
2c2d2 72 28 69 3d 30 3b 20 69 3c 73 41 67 67 49 6e 66  r(i=0; i<sAggInf
2c2d3 6f 2e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  o.nColumn; i++){
2c2d4 0a 20 20 20 20 20 20 20 20 20 20 73 74 72 75 63  .          struc
2c2d5 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70  t AggInfo_col *p
2c2d6 43 6f 6c 20 3d 20 26 73 41 67 67 49 6e 66 6f 2e  Col = &sAggInfo.
2c2d7 61 43 6f 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 20  aCol[i];.       
2c2d8 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e 69 53 6f     if( pCol->iSo
2c2d9 72 74 65 72 43 6f 6c 75 6d 6e 3e 3d 6a 20 29 7b  rterColumn>=j ){
2c2da 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74  .            int
2c2db 20 72 31 20 3d 20 6a 20 2b 20 72 65 67 42 61 73   r1 = j + regBas
2c2dc 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  e;.            i
2c2dd 6e 74 20 72 32 3b 0a 0a 20 20 20 20 20 20 20 20  nt r2;..        
2c2de 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33      r2 = sqlite3
2c2df 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d  ExprCodeGetColum
2c2e0 6e 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  n(pParse, .     
2c2e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c2e2 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e            pCol->
2c2e3 70 54 61 62 2c 20 70 43 6f 6c 2d 3e 69 43 6f 6c  pTab, pCol->iCol
2c2e4 75 6d 6e 2c 20 70 43 6f 6c 2d 3e 69 54 61 62 6c  umn, pCol->iTabl
2c2e5 65 2c 20 72 31 2c 20 30 29 3b 0a 20 20 20 20 20  e, r1, 0);.     
2c2e6 20 20 20 20 20 20 20 69 66 28 20 72 31 21 3d 72         if( r1!=r
2c2e7 32 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  2 ){.           
2c2e8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c2e9 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79  dOp2(v, OP_SCopy
2c2ea 2c 20 72 32 2c 20 72 31 29 3b 0a 20 20 20 20 20  , r2, r1);.     
2c2eb 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2c2ec 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20       j++;.      
2c2ed 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
2c2ee 20 20 20 20 20 20 20 20 72 65 67 52 65 63 6f 72          regRecor
2c2ef 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  d = sqlite3GetTe
2c2f0 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
2c2f1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2c2f2 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
2c2f3 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 42 61  akeRecord, regBa
2c2f4 73 65 2c 20 6e 43 6f 6c 2c 20 72 65 67 52 65 63  se, nCol, regRec
2c2f5 6f 72 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ord);.        sq
2c2f6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2c2f7 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c  v, OP_IdxInsert,
2c2f8 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e   sAggInfo.sortin
2c2f9 67 49 64 78 2c 20 72 65 67 52 65 63 6f 72 64 29  gIdx, regRecord)
2c2fa 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2c2fb 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
2c2fc 70 50 61 72 73 65 2c 20 72 65 67 52 65 63 6f 72  pParse, regRecor
2c2fd 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  d);.        sqli
2c2fe 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
2c2ff 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 42  nge(pParse, regB
2c300 61 73 65 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20 20  ase, nCol);.    
2c301 20 20 20 20 73 71 6c 69 74 65 33 57 68 65 72 65      sqlite3Where
2c302 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 20 20 20  End(pWInfo);.   
2c303 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2c304 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 6f 72  AddOp2(v, OP_Sor
2c305 74 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74  t, sAggInfo.sort
2c306 69 6e 67 49 64 78 2c 20 61 64 64 72 45 6e 64 29  ingIdx, addrEnd)
2c307 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 43 6f  ;.        VdbeCo
2c308 6d 6d 65 6e 74 28 28 76 2c 20 22 47 52 4f 55 50  mment((v, "GROUP
2c309 20 42 59 20 73 6f 72 74 22 29 29 3b 0a 20 20 20   BY sort"));.   
2c30a 20 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 75 73       sAggInfo.us
2c30b 65 53 6f 72 74 69 6e 67 49 64 78 20 3d 20 31 3b  eSortingIdx = 1;
2c30c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2c30d 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28 70  ExprCacheClear(p
2c30e 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a  Parse);.      }.
2c30f 0a 20 20 20 20 20 20 2f 2a 20 45 76 61 6c 75 61  .      /* Evalua
2c310 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 47  te the current G
2c311 52 4f 55 50 20 42 59 20 74 65 72 6d 73 20 61 6e  ROUP BY terms an
2c312 64 20 73 74 6f 72 65 20 69 6e 20 62 30 2c 20 62  d store in b0, b
2c313 31 2c 20 62 32 2e 2e 2e 0a 20 20 20 20 20 20 2a  1, b2....      *
2c314 2a 20 28 62 30 20 69 73 20 6d 65 6d 6f 72 79 20  * (b0 is memory 
2c315 6c 6f 63 61 74 69 6f 6e 20 69 42 4d 65 6d 2b 30  location iBMem+0
2c316 2c 20 62 31 20 69 73 20 69 42 4d 65 6d 2b 31 2c  , b1 is iBMem+1,
2c317 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 0a 20   and so forth). 
2c318 20 20 20 20 20 2a 2a 20 54 68 65 6e 20 63 6f 6d       ** Then com
2c319 70 61 72 65 20 74 68 65 20 63 75 72 72 65 6e 74  pare the current
2c31a 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d 73 20   GROUP BY terms 
2c31b 61 67 61 69 6e 73 74 20 74 68 65 20 47 52 4f 55  against the GROU
2c31c 50 20 42 59 20 74 65 72 6d 73 0a 20 20 20 20 20  P BY terms.     
2c31d 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 70 72 65   ** from the pre
2c31e 76 69 6f 75 73 20 72 6f 77 20 63 75 72 72 65 6e  vious row curren
2c31f 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 61 30  tly stored in a0
2c320 2c 20 61 31 2c 20 61 32 2e 2e 2e 0a 20 20 20 20  , a1, a2....    
2c321 20 20 2a 2f 0a 20 20 20 20 20 20 61 64 64 72 54    */.      addrT
2c322 6f 70 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74  opOfLoop = sqlit
2c323 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
2c324 72 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  r(v);.      sqli
2c325 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 61  te3ExprCacheClea
2c326 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  r(pParse);.     
2c327 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 47 72 6f   for(j=0; j<pGro
2c328 75 70 42 79 2d 3e 6e 45 78 70 72 3b 20 6a 2b 2b  upBy->nExpr; j++
2c329 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 67  ){.        if( g
2c32a 72 6f 75 70 42 79 53 6f 72 74 20 29 7b 0a 20 20  roupBySort ){.  
2c32b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2c32c 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2c32d 43 6f 6c 75 6d 6e 2c 20 73 41 67 67 49 6e 66 6f  Column, sAggInfo
2c32e 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20 6a 2c 20  .sortingIdx, j, 
2c32f 69 42 4d 65 6d 2b 6a 29 3b 0a 20 20 20 20 20 20  iBMem+j);.      
2c330 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2c331 20 20 20 73 41 67 67 49 6e 66 6f 2e 64 69 72 65     sAggInfo.dire
2c332 63 74 4d 6f 64 65 20 3d 20 31 3b 0a 20 20 20 20  ctMode = 1;.    
2c333 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2c334 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 47  rCode(pParse, pG
2c335 72 6f 75 70 42 79 2d 3e 61 5b 6a 5d 2e 70 45 78  roupBy->a[j].pEx
2c336 70 72 2c 20 69 42 4d 65 6d 2b 6a 29 3b 0a 20 20  pr, iBMem+j);.  
2c337 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2c338 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c339 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f  eAddOp4(v, OP_Co
2c33a 6d 70 61 72 65 2c 20 69 41 4d 65 6d 2c 20 69 42  mpare, iAMem, iB
2c33b 4d 65 6d 2c 20 70 47 72 6f 75 70 42 79 2d 3e 6e  Mem, pGroupBy->n
2c33c 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20 20 20  Expr,.          
2c33d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c33e 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c  (char*)pKeyInfo,
2c33f 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20   P4_KEYINFO);.  
2c340 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33      j1 = sqlite3
2c341 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
2c342 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  v);.      sqlite
2c343 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2c344 50 5f 4a 75 6d 70 2c 20 6a 31 2b 31 2c 20 30 2c  P_Jump, j1+1, 0,
2c345 20 6a 31 2b 31 29 3b 0a 0a 20 20 20 20 20 20 2f   j1+1);..      /
2c346 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2c347 74 68 61 74 20 72 75 6e 73 20 77 68 65 6e 65 76  that runs whenev
2c348 65 72 20 74 68 65 20 47 52 4f 55 50 20 42 59 20  er the GROUP BY 
2c349 63 68 61 6e 67 65 73 2e 0a 20 20 20 20 20 20 2a  changes..      *
2c34a 2a 20 43 68 61 6e 67 65 73 20 69 6e 20 74 68 65  * Changes in the
2c34b 20 47 52 4f 55 50 20 42 59 20 61 72 65 20 64 65   GROUP BY are de
2c34c 74 65 63 74 65 64 20 62 79 20 74 68 65 20 70 72  tected by the pr
2c34d 65 76 69 6f 75 73 20 63 6f 64 65 0a 20 20 20 20  evious code.    
2c34e 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20 49 66 20    ** block.  If 
2c34f 74 68 65 72 65 20 77 65 72 65 20 6e 6f 20 63 68  there were no ch
2c350 61 6e 67 65 73 2c 20 74 68 69 73 20 62 6c 6f 63  anges, this bloc
2c351 6b 20 69 73 20 73 6b 69 70 70 65 64 2e 0a 20 20  k is skipped..  
2c352 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
2c353 54 68 69 73 20 63 6f 64 65 20 63 6f 70 69 65 73  This code copies
2c354 20 63 75 72 72 65 6e 74 20 67 72 6f 75 70 20 62   current group b
2c355 79 20 74 65 72 6d 73 20 69 6e 20 62 30 2c 62 31  y terms in b0,b1
2c356 2c 62 32 2c 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a  ,b2,....      **
2c357 20 6f 76 65 72 20 74 6f 20 61 30 2c 61 31 2c 61   over to a0,a1,a
2c358 32 2e 20 20 49 74 20 74 68 65 6e 20 63 61 6c 6c  2.  It then call
2c359 73 20 74 68 65 20 6f 75 74 70 75 74 20 73 75 62  s the output sub
2c35a 72 6f 75 74 69 6e 65 0a 20 20 20 20 20 20 2a 2a  routine.      **
2c35b 20 61 6e 64 20 72 65 73 65 74 73 20 74 68 65 20   and resets the 
2c35c 61 67 67 72 65 67 61 74 65 20 61 63 63 75 6d 75  aggregate accumu
2c35d 6c 61 74 6f 72 20 72 65 67 69 73 74 65 72 73 20  lator registers 
2c35e 69 6e 20 70 72 65 70 61 72 61 74 69 6f 6e 0a 20  in preparation. 
2c35f 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 20       ** for the 
2c360 6e 65 78 74 20 47 52 4f 55 50 20 42 59 20 62 61  next GROUP BY ba
2c361 74 63 68 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  tch..      */.  
2c362 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2c363 6f 64 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20  odeMove(pParse, 
2c364 69 42 4d 65 6d 2c 20 69 41 4d 65 6d 2c 20 70 47  iBMem, iAMem, pG
2c365 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 29 3b 0a  roupBy->nExpr);.
2c366 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c367 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2c368 73 75 62 2c 20 72 65 67 4f 75 74 70 75 74 52 6f  sub, regOutputRo
2c369 77 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77  w, addrOutputRow
2c36a 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
2c36b 6d 65 6e 74 28 28 76 2c 20 22 6f 75 74 70 75 74  ment((v, "output
2c36c 20 6f 6e 65 20 72 6f 77 22 29 29 3b 0a 20 20 20   one row"));.   
2c36d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c36e 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 50 6f 73  dOp2(v, OP_IfPos
2c36f 2c 20 69 41 62 6f 72 74 46 6c 61 67 2c 20 61 64  , iAbortFlag, ad
2c370 64 72 45 6e 64 29 3b 0a 20 20 20 20 20 20 56 64  drEnd);.      Vd
2c371 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 63  beComment((v, "c
2c372 68 65 63 6b 20 61 62 6f 72 74 20 66 6c 61 67 22  heck abort flag"
2c373 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ));.      sqlite
2c374 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2c375 50 5f 47 6f 73 75 62 2c 20 72 65 67 52 65 73 65  P_Gosub, regRese
2c376 74 2c 20 61 64 64 72 52 65 73 65 74 29 3b 0a 20  t, addrReset);. 
2c377 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
2c378 28 28 76 2c 20 22 72 65 73 65 74 20 61 63 63 75  ((v, "reset accu
2c379 6d 75 6c 61 74 6f 72 22 29 29 3b 0a 0a 20 20 20  mulator"));..   
2c37a 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65     /* Update the
2c37b 20 61 67 67 72 65 67 61 74 65 20 61 63 63 75 6d   aggregate accum
2c37c 75 6c 61 74 6f 72 73 20 62 61 73 65 64 20 6f 6e  ulators based on
2c37d 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a   the content of.
2c37e 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 75 72        ** the cur
2c37f 72 65 6e 74 20 72 6f 77 0a 20 20 20 20 20 20 2a  rent row.      *
2c380 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  /.      sqlite3V
2c381 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
2c382 31 29 3b 0a 20 20 20 20 20 20 75 70 64 61 74 65  1);.      update
2c383 41 63 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72  Accumulator(pPar
2c384 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a  se, &sAggInfo);.
2c385 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c386 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
2c387 74 65 67 65 72 2c 20 31 2c 20 69 55 73 65 46 6c  teger, 1, iUseFl
2c388 61 67 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43  ag);.      VdbeC
2c389 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 69 6e 64 69  omment((v, "indi
2c38a 63 61 74 65 20 64 61 74 61 20 69 6e 20 61 63 63  cate data in acc
2c38b 75 6d 75 6c 61 74 6f 72 22 29 29 3b 0a 0a 20 20  umulator"));..  
2c38c 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68      /* End of th
2c38d 65 20 6c 6f 6f 70 0a 20 20 20 20 20 20 2a 2f 0a  e loop.      */.
2c38e 20 20 20 20 20 20 69 66 28 20 67 72 6f 75 70 42        if( groupB
2c38f 79 53 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 20  ySort ){.       
2c390 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c391 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 73  p2(v, OP_Next, s
2c392 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49  AggInfo.sortingI
2c393 64 78 2c 20 61 64 64 72 54 6f 70 4f 66 4c 6f 6f  dx, addrTopOfLoo
2c394 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  p);.      }else{
2c395 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2c396 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29  WhereEnd(pWInfo)
2c397 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2c398 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f  3VdbeChangeToNoo
2c399 70 28 76 2c 20 61 64 64 72 53 6f 72 74 69 6e 67  p(v, addrSorting
2c39a 49 64 78 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d  Idx, 1);.      }
2c39b 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75  ..      /* Outpu
2c39c 74 20 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 20  t the final row 
2c39d 6f 66 20 72 65 73 75 6c 74 0a 20 20 20 20 20 20  of result.      
2c39e 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
2c39f 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2c3a0 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 70 75  _Gosub, regOutpu
2c3a1 74 52 6f 77 2c 20 61 64 64 72 4f 75 74 70 75 74  tRow, addrOutput
2c3a2 52 6f 77 29 3b 0a 20 20 20 20 20 20 56 64 62 65  Row);.      Vdbe
2c3a3 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 6f 75 74  Comment((v, "out
2c3a4 70 75 74 20 66 69 6e 61 6c 20 72 6f 77 22 29 29  put final row"))
2c3a5 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70  ;..      /* Jump
2c3a6 20 6f 76 65 72 20 74 68 65 20 73 75 62 72 6f 75   over the subrou
2c3a7 74 69 6e 65 73 0a 20 20 20 20 20 20 2a 2f 0a 20  tines.      */. 
2c3a8 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2c3a9 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
2c3aa 6f 2c 20 30 2c 20 61 64 64 72 45 6e 64 29 3b 0a  o, 0, addrEnd);.
2c3ab 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61  .      /* Genera
2c3ac 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20  te a subroutine 
2c3ad 74 68 61 74 20 6f 75 74 70 75 74 73 20 61 20 73  that outputs a s
2c3ae 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74 68 65  ingle row of the
2c3af 20 72 65 73 75 6c 74 0a 20 20 20 20 20 20 2a 2a   result.      **
2c3b0 20 73 65 74 2e 20 20 54 68 69 73 20 73 75 62 72   set.  This subr
2c3b1 6f 75 74 69 6e 65 20 66 69 72 73 74 20 6c 6f 6f  outine first loo
2c3b2 6b 73 20 61 74 20 74 68 65 20 69 55 73 65 46 6c  ks at the iUseFl
2c3b3 61 67 2e 20 20 49 66 20 69 55 73 65 46 6c 61 67  ag.  If iUseFlag
2c3b4 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 6c 65 73  .      ** is les
2c3b5 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  s than or equal 
2c3b6 74 6f 20 7a 65 72 6f 2c 20 74 68 65 20 73 75 62  to zero, the sub
2c3b7 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
2c3b8 6f 70 2e 20 20 49 66 0a 20 20 20 20 20 20 2a 2a  op.  If.      **
2c3b9 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20   the processing 
2c3ba 63 61 6c 6c 73 20 66 6f 72 20 74 68 65 20 71 75  calls for the qu
2c3bb 65 72 79 20 74 6f 20 61 62 6f 72 74 2c 20 74 68  ery to abort, th
2c3bc 69 73 20 73 75 62 72 6f 75 74 69 6e 65 0a 20 20  is subroutine.  
2c3bd 20 20 20 20 2a 2a 20 69 6e 63 72 65 6d 65 6e 74      ** increment
2c3be 73 20 74 68 65 20 69 41 62 6f 72 74 46 6c 61 67  s the iAbortFlag
2c3bf 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   memory location
2c3c0 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
2c3c1 67 20 69 6e 0a 20 20 20 20 20 20 2a 2a 20 6f 72  g in.      ** or
2c3c2 64 65 72 20 74 6f 20 73 69 67 6e 61 6c 20 74 68  der to signal th
2c3c3 65 20 63 61 6c 6c 65 72 20 74 6f 20 61 62 6f 72  e caller to abor
2c3c4 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  t..      */.    
2c3c5 20 20 61 64 64 72 53 65 74 41 62 6f 72 74 20 3d    addrSetAbort =
2c3c6 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
2c3c7 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20  entAddr(v);.    
2c3c8 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c3c9 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2c3ca 72 2c 20 31 2c 20 69 41 62 6f 72 74 46 6c 61 67  r, 1, iAbortFlag
2c3cb 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
2c3cc 6d 65 6e 74 28 28 76 2c 20 22 73 65 74 20 61 62  ment((v, "set ab
2c3cd 6f 72 74 20 66 6c 61 67 22 29 29 3b 0a 20 20 20  ort flag"));.   
2c3ce 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c3cf 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72  dOp1(v, OP_Retur
2c3d0 6e 2c 20 72 65 67 4f 75 74 70 75 74 52 6f 77 29  n, regOutputRow)
2c3d1 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2c3d2 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
2c3d3 76 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77  v, addrOutputRow
2c3d4 29 3b 0a 20 20 20 20 20 20 61 64 64 72 4f 75 74  );.      addrOut
2c3d5 70 75 74 52 6f 77 20 3d 20 73 71 6c 69 74 65 33  putRow = sqlite3
2c3d6 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
2c3d7 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  v);.      sqlite
2c3d8 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2c3d9 50 5f 49 66 50 6f 73 2c 20 69 55 73 65 46 6c 61  P_IfPos, iUseFla
2c3da 67 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77  g, addrOutputRow
2c3db 2b 32 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43  +2);.      VdbeC
2c3dc 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 47 72 6f 75  omment((v, "Grou
2c3dd 70 62 79 20 72 65 73 75 6c 74 20 67 65 6e 65 72  pby result gener
2c3de 61 74 6f 72 20 65 6e 74 72 79 20 70 6f 69 6e 74  ator entry point
2c3df 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  "));.      sqlit
2c3e0 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2c3e1 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 4f 75  OP_Return, regOu
2c3e2 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20  tputRow);.      
2c3e3 66 69 6e 61 6c 69 7a 65 41 67 67 46 75 6e 63 74  finalizeAggFunct
2c3e4 69 6f 6e 73 28 70 50 61 72 73 65 2c 20 26 73 41  ions(pParse, &sA
2c3e5 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 73  ggInfo);.      s
2c3e6 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
2c3e7 65 28 70 50 61 72 73 65 2c 20 70 48 61 76 69 6e  e(pParse, pHavin
2c3e8 67 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77  g, addrOutputRow
2c3e9 2b 31 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  +1, SQLITE_JUMPI
2c3ea 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73 65  FNULL);.      se
2c3eb 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50  lectInnerLoop(pP
2c3ec 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45 4c 69  arse, p, p->pELi
2c3ed 73 74 2c 20 30 2c 20 30 2c 20 70 4f 72 64 65 72  st, 0, 0, pOrder
2c3ee 42 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  By,.            
2c3ef 20 20 20 20 20 20 20 20 20 20 64 69 73 74 69 6e            distin
2c3f0 63 74 2c 20 70 44 65 73 74 2c 0a 20 20 20 20 20  ct, pDest,.     
2c3f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c3f2 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 2b 31   addrOutputRow+1
2c3f3 2c 20 61 64 64 72 53 65 74 41 62 6f 72 74 29 3b  , addrSetAbort);
2c3f4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2c3f5 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52  beAddOp1(v, OP_R
2c3f6 65 74 75 72 6e 2c 20 72 65 67 4f 75 74 70 75 74  eturn, regOutput
2c3f7 52 6f 77 29 3b 0a 20 20 20 20 20 20 56 64 62 65  Row);.      Vdbe
2c3f8 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6e 64  Comment((v, "end
2c3f9 20 67 72 6f 75 70 62 79 20 72 65 73 75 6c 74 20   groupby result 
2c3fa 67 65 6e 65 72 61 74 6f 72 22 29 29 3b 0a 0a 20  generator"));.. 
2c3fb 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
2c3fc 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68   a subroutine th
2c3fd 61 74 20 77 69 6c 6c 20 72 65 73 65 74 20 74 68  at will reset th
2c3fe 65 20 67 72 6f 75 70 2d 62 79 20 61 63 63 75 6d  e group-by accum
2c3ff 75 6c 61 74 6f 72 0a 20 20 20 20 20 20 2a 2f 0a  ulator.      */.
2c400 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c401 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2c402 20 61 64 64 72 52 65 73 65 74 29 3b 0a 20 20 20   addrReset);.   
2c403 20 20 20 72 65 73 65 74 41 63 63 75 6d 75 6c 61     resetAccumula
2c404 74 6f 72 28 70 50 61 72 73 65 2c 20 26 73 41 67  tor(pParse, &sAg
2c405 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 73 71  gInfo);.      sq
2c406 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2c407 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65  v, OP_Return, re
2c408 67 52 65 73 65 74 29 3b 0a 20 20 20 20 20 0a 20  gReset);.     . 
2c409 20 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20 70 47     } /* endif pG
2c40a 72 6f 75 70 42 79 2e 20 20 42 65 67 69 6e 20 61  roupBy.  Begin a
2c40b 67 67 72 65 67 61 74 65 20 71 75 65 72 69 65 73  ggregate queries
2c40c 20 77 69 74 68 6f 75 74 20 47 52 4f 55 50 20 42   without GROUP B
2c40d 59 3a 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20 7b  Y: */.    else {
2c40e 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20  .      ExprList 
2c40f 2a 70 44 65 6c 20 3d 20 30 3b 0a 23 69 66 6e 64  *pDel = 0;.#ifnd
2c410 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
2c411 54 52 45 45 43 4f 55 4e 54 0a 20 20 20 20 20 20  TREECOUNT.      
2c412 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20  Table *pTab;.   
2c413 20 20 20 69 66 28 20 28 70 54 61 62 20 3d 20 69     if( (pTab = i
2c414 73 53 69 6d 70 6c 65 43 6f 75 6e 74 28 70 2c 20  sSimpleCount(p, 
2c415 26 73 41 67 67 49 6e 66 6f 29 29 21 3d 30 20 29  &sAggInfo))!=0 )
2c416 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  {.        /* If 
2c417 69 73 53 69 6d 70 6c 65 43 6f 75 6e 74 28 29 20  isSimpleCount() 
2c418 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
2c419 72 20 74 6f 20 61 20 54 61 62 6c 65 20 73 74 72  r to a Table str
2c41a 75 63 74 75 72 65 2c 20 74 68 65 6e 0a 20 20 20  ucture, then.   
2c41b 20 20 20 20 20 2a 2a 20 74 68 65 20 53 51 4c 20       ** the SQL 
2c41c 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 66 20  statement is of 
2c41d 74 68 65 20 66 6f 72 6d 3a 0a 20 20 20 20 20 20  the form:.      
2c41e 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
2c41f 20 20 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a    SELECT count(*
2c420 29 20 46 52 4f 4d 20 3c 74 62 6c 3e 0a 20 20 20  ) FROM <tbl>.   
2c421 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
2c422 2a 2a 20 77 68 65 72 65 20 74 68 65 20 54 61 62  ** where the Tab
2c423 6c 65 20 73 74 72 75 63 74 75 72 65 20 72 65 74  le structure ret
2c424 75 72 6e 65 64 20 72 65 70 72 65 73 65 6e 74 73  urned represents
2c425 20 74 61 62 6c 65 20 3c 74 62 6c 3e 2e 0a 20 20   table <tbl>..  
2c426 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
2c427 20 2a 2a 20 54 68 69 73 20 73 74 61 74 65 6d 65   ** This stateme
2c428 6e 74 20 69 73 20 73 6f 20 63 6f 6d 6d 6f 6e 20  nt is so common 
2c429 74 68 61 74 20 69 74 20 69 73 20 6f 70 74 69 6d  that it is optim
2c42a 69 7a 65 64 20 73 70 65 63 69 61 6c 6c 79 2e 20  ized specially. 
2c42b 54 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f  The.        ** O
2c42c 50 5f 43 6f 75 6e 74 20 69 6e 73 74 72 75 63 74  P_Count instruct
2c42d 69 6f 6e 20 69 73 20 65 78 65 63 75 74 65 64 20  ion is executed 
2c42e 65 69 74 68 65 72 20 6f 6e 20 74 68 65 20 69 6e  either on the in
2c42f 74 6b 65 79 20 74 61 62 6c 65 20 74 68 61 74 0a  tkey table that.
2c430 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61          ** conta
2c431 69 6e 73 20 74 68 65 20 64 61 74 61 20 66 6f 72  ins the data for
2c432 20 74 61 62 6c 65 20 3c 74 62 6c 3e 20 6f 72 20   table <tbl> or 
2c433 6f 6e 20 6f 6e 65 20 6f 66 20 69 74 73 20 69 6e  on one of its in
2c434 64 65 78 65 73 2e 20 49 74 0a 20 20 20 20 20 20  dexes. It.      
2c435 20 20 2a 2a 20 69 73 20 62 65 74 74 65 72 20 74    ** is better t
2c436 6f 20 65 78 65 63 75 74 65 20 74 68 65 20 6f 70  o execute the op
2c437 20 6f 6e 20 61 6e 20 69 6e 64 65 78 2c 20 61 73   on an index, as
2c438 20 69 6e 64 65 78 65 73 20 61 72 65 20 61 6c 6d   indexes are alm
2c439 6f 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  ost.        ** a
2c43a 6c 77 61 79 73 20 73 70 72 65 61 64 20 61 63 72  lways spread acr
2c43b 6f 73 73 20 6c 65 73 73 20 70 61 67 65 73 20 74  oss less pages t
2c43c 68 61 6e 20 74 68 65 69 72 20 63 6f 72 72 65 73  han their corres
2c43d 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65 73 2e 0a  ponding tables..
2c43e 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
2c43f 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 44 62     const int iDb
2c440 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
2c441 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
2c442 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
2c443 61 29 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73  a);.        cons
2c444 74 20 69 6e 74 20 69 43 73 72 20 3d 20 70 50 61  t int iCsr = pPa
2c445 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20 20 20 20  rse->nTab++;    
2c446 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f 20 73 63   /* Cursor to sc
2c447 61 6e 20 62 2d 74 72 65 65 20 2a 2f 0a 20 20 20  an b-tree */.   
2c448 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78       Index *pIdx
2c449 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2c44a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
2c44b 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a  rator variable *
2c44c 2f 0a 20 20 20 20 20 20 20 20 4b 65 79 49 6e 66  /.        KeyInf
2c44d 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 30 3b  o *pKeyInfo = 0;
2c44e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2c44f 2a 20 4b 65 79 69 6e 66 6f 20 66 6f 72 20 73 63  * Keyinfo for sc
2c450 61 6e 6e 65 64 20 69 6e 64 65 78 20 2a 2f 0a 20  anned index */. 
2c451 20 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 42         Index *pB
2c452 65 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  est = 0;        
2c453 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
2c454 65 73 74 20 69 6e 64 65 78 20 66 6f 75 6e 64 20  est index found 
2c455 73 6f 20 66 61 72 20 2a 2f 0a 20 20 20 20 20 20  so far */.      
2c456 20 20 69 6e 74 20 69 52 6f 6f 74 20 3d 20 70 54    int iRoot = pT
2c457 61 62 2d 3e 74 6e 75 6d 3b 20 20 20 20 20 20 20  ab->tnum;       
2c458 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70         /* Root p
2c459 61 67 65 20 6f 66 20 73 63 61 6e 6e 65 64 20 62  age of scanned b
2c45a 2d 74 72 65 65 20 2a 2f 0a 0a 20 20 20 20 20 20  -tree */..      
2c45b 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72    sqlite3CodeVer
2c45c 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65  ifySchema(pParse
2c45d 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20  , iDb);.        
2c45e 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
2c45f 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54  (pParse, iDb, pT
2c460 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 61  ab->tnum, 0, pTa
2c461 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 20  b->zName);..    
2c462 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f      /* Search fo
2c463 72 20 74 68 65 20 69 6e 64 65 78 20 74 68 61 74  r the index that
2c464 20 68 61 73 20 74 68 65 20 6c 65 61 73 74 20 61   has the least a
2c465 6d 6f 75 6e 74 20 6f 66 20 63 6f 6c 75 6d 6e 73  mount of columns
2c466 2e 20 49 66 0a 20 20 20 20 20 20 20 20 2a 2a 20  . If.        ** 
2c467 74 68 65 72 65 20 69 73 20 73 75 63 68 20 61 6e  there is such an
2c468 20 69 6e 64 65 78 2c 20 61 6e 64 20 69 74 20 68   index, and it h
2c469 61 73 20 6c 65 73 73 20 63 6f 6c 75 6d 6e 73 20  as less columns 
2c46a 74 68 61 6e 20 74 68 65 20 74 61 62 6c 65 0a 20  than the table. 
2c46b 20 20 20 20 20 20 20 2a 2a 20 64 6f 65 73 2c 20         ** does, 
2c46c 74 68 65 6e 20 77 65 20 63 61 6e 20 61 73 73 75  then we can assu
2c46d 6d 65 20 74 68 61 74 20 69 74 20 63 6f 6e 73 75  me that it consu
2c46e 6d 65 73 20 6c 65 73 73 20 73 70 61 63 65 20 6f  mes less space o
2c46f 6e 20 64 69 73 6b 20 61 6e 64 0a 20 20 20 20 20  n disk and.     
2c470 20 20 20 2a 2a 20 77 69 6c 6c 20 74 68 65 72 65     ** will there
2c471 66 6f 72 65 20 62 65 20 63 68 65 61 70 65 72 20  fore be cheaper 
2c472 74 6f 20 73 63 61 6e 20 74 6f 20 64 65 74 65 72  to scan to deter
2c473 6d 69 6e 65 20 74 68 65 20 71 75 65 72 79 20 72  mine the query r
2c474 65 73 75 6c 74 2e 0a 20 20 20 20 20 20 20 20 2a  esult..        *
2c475 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 73  * In this case s
2c476 65 74 20 69 52 6f 6f 74 20 74 6f 20 74 68 65 20  et iRoot to the 
2c477 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72  root page number
2c478 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 62 2d   of the index b-
2c479 74 72 65 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  tree.        ** 
2c47a 61 6e 64 20 70 4b 65 79 49 6e 66 6f 20 74 6f 20  and pKeyInfo to 
2c47b 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  the KeyInfo stru
2c47c 63 74 75 72 65 20 72 65 71 75 69 72 65 64 20 74  cture required t
2c47d 6f 20 6e 61 76 69 67 61 74 65 20 74 68 65 0a 20  o navigate the. 
2c47e 20 20 20 20 20 20 20 2a 2a 20 69 6e 64 65 78 2e         ** index.
2c47f 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
2c480 20 20 20 20 2a 2a 20 49 6e 20 70 72 61 63 74 69      ** In practi
2c481 63 65 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73  ce the KeyInfo s
2c482 74 72 75 63 74 75 72 65 20 77 69 6c 6c 20 6e 6f  tructure will no
2c483 74 20 62 65 20 75 73 65 64 2e 20 49 74 20 69 73  t be used. It is
2c484 20 6f 6e 6c 79 20 0a 20 20 20 20 20 20 20 20 2a   only .        *
2c485 2a 20 70 61 73 73 65 64 20 74 6f 20 6b 65 65 70  * passed to keep
2c486 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 68 61 70   OP_OpenRead hap
2c487 70 79 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  py..        */. 
2c488 20 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d         for(pIdx=
2c489 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
2c48a 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
2c48b 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 20  Next){.         
2c48c 20 69 66 28 20 21 70 42 65 73 74 20 7c 7c 20 70   if( !pBest || p
2c48d 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3c 70 42 65  Idx->nColumn<pBe
2c48e 73 74 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20  st->nColumn ){. 
2c48f 20 20 20 20 20 20 20 20 20 20 20 70 42 65 73 74             pBest
2c490 20 3d 20 70 49 64 78 3b 0a 20 20 20 20 20 20 20   = pIdx;.       
2c491 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
2c492 20 20 20 20 20 20 20 69 66 28 20 70 42 65 73 74         if( pBest
2c493 20 26 26 20 70 42 65 73 74 2d 3e 6e 43 6f 6c 75   && pBest->nColu
2c494 6d 6e 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b  mn<pTab->nCol ){
2c495 0a 20 20 20 20 20 20 20 20 20 20 69 52 6f 6f 74  .          iRoot
2c496 20 3d 20 70 42 65 73 74 2d 3e 74 6e 75 6d 3b 0a   = pBest->tnum;.
2c497 20 20 20 20 20 20 20 20 20 20 70 4b 65 79 49 6e            pKeyIn
2c498 66 6f 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65  fo = sqlite3Inde
2c499 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
2c49a 20 70 42 65 73 74 29 3b 0a 20 20 20 20 20 20 20   pBest);.       
2c49b 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f   }..        /* O
2c49c 70 65 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20  pen a read-only 
2c49d 63 75 72 73 6f 72 2c 20 65 78 65 63 75 74 65 20  cursor, execute 
2c49e 74 68 65 20 4f 50 5f 43 6f 75 6e 74 2c 20 63 6c  the OP_Count, cl
2c49f 6f 73 65 20 74 68 65 20 63 75 72 73 6f 72 2e 20  ose the cursor. 
2c4a0 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  */.        sqlit
2c4a1 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2c4a2 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 43 73  OP_OpenRead, iCs
2c4a3 72 2c 20 69 52 6f 6f 74 2c 20 69 44 62 29 3b 0a  r, iRoot, iDb);.
2c4a4 20 20 20 20 20 20 20 20 69 66 28 20 70 4b 65 79          if( pKey
2c4a5 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 20 20  Info ){.        
2c4a6 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
2c4a7 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 68  ngeP4(v, -1, (ch
2c4a8 61 72 20 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50  ar *)pKeyInfo, P
2c4a9 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
2c4aa 46 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  F);.        }.  
2c4ab 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c4ac 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f  eAddOp2(v, OP_Co
2c4ad 75 6e 74 2c 20 69 43 73 72 2c 20 73 41 67 67 49  unt, iCsr, sAggI
2c4ae 6e 66 6f 2e 61 46 75 6e 63 5b 30 5d 2e 69 4d 65  nfo.aFunc[0].iMe
2c4af 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  m);.        sqli
2c4b0 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2c4b1 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 73 72 29   OP_Close, iCsr)
2c4b2 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65  ;.      }else.#e
2c4b3 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2c4b4 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 20 2a  MIT_BTREECOUNT *
2c4b5 2f 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20  /.      {.      
2c4b6 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68    /* Check if th
2c4b7 65 20 71 75 65 72 79 20 69 73 20 6f 66 20 6f 6e  e query is of on
2c4b8 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
2c4b9 6e 67 20 66 6f 72 6d 73 3a 0a 20 20 20 20 20 20  ng forms:.      
2c4ba 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
2c4bb 20 20 53 45 4c 45 43 54 20 6d 69 6e 28 78 29 20    SELECT min(x) 
2c4bc 46 52 4f 4d 20 2e 2e 2e 0a 20 20 20 20 20 20 20  FROM ....       
2c4bd 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 6d 61 78   **   SELECT max
2c4be 28 78 29 20 46 52 4f 4d 20 2e 2e 2e 0a 20 20 20  (x) FROM ....   
2c4bf 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
2c4c0 2a 2a 20 49 66 20 69 74 20 69 73 2c 20 74 68 65  ** If it is, the
2c4c1 6e 20 61 73 6b 20 74 68 65 20 63 6f 64 65 20 69  n ask the code i
2c4c2 6e 20 77 68 65 72 65 2e 63 20 74 6f 20 61 74 74  n where.c to att
2c4c3 65 6d 70 74 20 74 6f 20 73 6f 72 74 20 72 65 73  empt to sort res
2c4c4 75 6c 74 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  ults.        ** 
2c4c5 61 73 20 69 66 20 74 68 65 72 65 20 77 61 73 20  as if there was 
2c4c6 61 6e 20 22 4f 52 44 45 52 20 4f 4e 20 78 22 20  an "ORDER ON x" 
2c4c7 6f 72 20 22 4f 52 44 45 52 20 4f 4e 20 78 20 44  or "ORDER ON x D
2c4c8 45 53 43 22 20 63 6c 61 75 73 65 2e 20 0a 20 20  ESC" clause. .  
2c4c9 20 20 20 20 20 20 2a 2a 20 49 66 20 77 68 65 72        ** If wher
2c4ca 65 2e 63 20 69 73 20 61 62 6c 65 20 74 6f 20 70  e.c is able to p
2c4cb 72 6f 64 75 63 65 20 72 65 73 75 6c 74 73 20 73  roduce results s
2c4cc 6f 72 74 65 64 20 69 6e 20 74 68 69 73 20 6f 72  orted in this or
2c4cd 64 65 72 2c 20 74 68 65 6e 0a 20 20 20 20 20 20  der, then.      
2c4ce 20 20 2a 2a 20 61 64 64 20 76 64 62 65 20 63 6f    ** add vdbe co
2c4cf 64 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20  de to break out 
2c4d0 6f 66 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e  of the processin
2c4d1 67 20 6c 6f 6f 70 20 61 66 74 65 72 20 74 68 65  g loop after the
2c4d2 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 69 72   .        ** fir
2c4d3 73 74 20 69 74 65 72 61 74 69 6f 6e 20 28 73 69  st iteration (si
2c4d4 6e 63 65 20 74 68 65 20 66 69 72 73 74 20 69 74  nce the first it
2c4d5 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c  eration of the l
2c4d6 6f 6f 70 20 69 73 20 0a 20 20 20 20 20 20 20 20  oop is .        
2c4d7 2a 2a 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  ** guaranteed to
2c4d8 20 6f 70 65 72 61 74 65 20 6f 6e 20 74 68 65 20   operate on the 
2c4d9 72 6f 77 20 77 69 74 68 20 74 68 65 20 6d 69 6e  row with the min
2c4da 69 6d 75 6d 20 6f 72 20 6d 61 78 69 6d 75 6d 20  imum or maximum 
2c4db 0a 20 20 20 20 20 20 20 20 2a 2a 20 76 61 6c 75  .        ** valu
2c4dc 65 20 6f 66 20 78 2c 20 74 68 65 20 6f 6e 6c 79  e of x, the only
2c4dd 20 72 6f 77 20 72 65 71 75 69 72 65 64 29 2e 0a   row required)..
2c4de 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
2c4df 20 20 20 2a 2a 20 41 20 73 70 65 63 69 61 6c 20     ** A special 
2c4e0 66 6c 61 67 20 6d 75 73 74 20 62 65 20 70 61 73  flag must be pas
2c4e1 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 57 68  sed to sqlite3Wh
2c4e2 65 72 65 42 65 67 69 6e 28 29 20 74 6f 20 73 6c  ereBegin() to sl
2c4e3 69 67 68 74 6c 79 0a 20 20 20 20 20 20 20 20 2a  ightly.        *
2c4e4 2a 20 6d 6f 64 69 66 79 20 62 65 68 61 76 69 6f  * modify behavio
2c4e5 75 72 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20  ur as follows:. 
2c4e6 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
2c4e7 20 20 2a 2a 20 20 20 2b 20 49 66 20 74 68 65 20    **   + If the 
2c4e8 71 75 65 72 79 20 69 73 20 61 20 22 53 45 4c 45  query is a "SELE
2c4e9 43 54 20 6d 69 6e 28 78 29 22 2c 20 74 68 65 6e  CT min(x)", then
2c4ea 20 74 68 65 20 6c 6f 6f 70 20 63 6f 64 65 64 20   the loop coded 
2c4eb 62 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  by.        **   
2c4ec 20 20 77 68 65 72 65 2e 63 20 73 68 6f 75 6c 64    where.c should
2c4ed 20 6e 6f 74 20 69 74 65 72 61 74 65 20 6f 76 65   not iterate ove
2c4ee 72 20 61 6e 79 20 76 61 6c 75 65 73 20 77 69 74  r any values wit
2c4ef 68 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 0a 20  h a NULL value. 
2c4f0 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 66 6f         **     fo
2c4f1 72 20 78 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a  r x..        **.
2c4f2 20 20 20 20 20 20 20 20 2a 2a 20 20 20 2b 20 54          **   + T
2c4f3 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 63 6f 64  he optimizer cod
2c4f4 65 20 69 6e 20 77 68 65 72 65 2e 63 20 28 74 68  e in where.c (th
2c4f5 65 20 74 68 69 6e 67 20 74 68 61 74 20 64 65 63  e thing that dec
2c4f6 69 64 65 73 20 77 68 69 63 68 0a 20 20 20 20 20  ides which.     
2c4f7 20 20 20 2a 2a 20 20 20 20 20 69 6e 64 65 78 20     **     index 
2c4f8 6f 72 20 69 6e 64 69 63 65 73 20 74 6f 20 75 73  or indices to us
2c4f9 65 29 20 73 68 6f 75 6c 64 20 70 6c 61 63 65 20  e) should place 
2c4fa 61 20 64 69 66 66 65 72 65 6e 74 20 70 72 69 6f  a different prio
2c4fb 72 69 74 79 20 6f 6e 20 0a 20 20 20 20 20 20 20  rity on .       
2c4fc 20 2a 2a 20 20 20 20 20 73 61 74 69 73 66 79 69   **     satisfyi
2c4fd 6e 67 20 74 68 65 20 27 4f 52 44 45 52 20 42 59  ng the 'ORDER BY
2c4fe 27 20 63 6c 61 75 73 65 20 74 68 61 6e 20 69 74  ' clause than it
2c4ff 20 64 6f 65 73 20 69 6e 20 6f 74 68 65 72 20 63   does in other c
2c500 61 73 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2a  ases..        **
2c501 20 20 20 20 20 52 65 66 65 72 20 74 6f 20 63 6f       Refer to co
2c502 64 65 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 20  de and comments 
2c503 69 6e 20 77 68 65 72 65 2e 63 20 66 6f 72 20 64  in where.c for d
2c504 65 74 61 69 6c 73 2e 0a 20 20 20 20 20 20 20 20  etails..        
2c505 2a 2f 0a 20 20 20 20 20 20 20 20 45 78 70 72 4c  */.        ExprL
2c506 69 73 74 20 2a 70 4d 69 6e 4d 61 78 20 3d 20 30  ist *pMinMax = 0
2c507 3b 0a 20 20 20 20 20 20 20 20 75 38 20 66 6c 61  ;.        u8 fla
2c508 67 20 3d 20 6d 69 6e 4d 61 78 51 75 65 72 79 28  g = minMaxQuery(
2c509 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  p);.        if( 
2c50a 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20  flag ){.        
2c50b 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
2c50c 61 73 50 72 6f 70 65 72 74 79 28 70 2d 3e 70 45  asProperty(p->pE
2c50d 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
2c50e 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
2c50f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4d 69  );.          pMi
2c510 6e 4d 61 78 20 3d 20 73 71 6c 69 74 65 33 45 78  nMax = sqlite3Ex
2c511 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d  prListDup(db, p-
2c512 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  >pEList->a[0].pE
2c513 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2c 30 29 3b  xpr->x.pList,0);
2c514 0a 20 20 20 20 20 20 20 20 20 20 70 44 65 6c 20  .          pDel 
2c515 3d 20 70 4d 69 6e 4d 61 78 3b 0a 20 20 20 20 20  = pMinMax;.     
2c516 20 20 20 20 20 69 66 28 20 70 4d 69 6e 4d 61 78       if( pMinMax
2c517 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46   && !db->mallocF
2c518 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20  ailed ){.       
2c519 20 20 20 20 20 70 4d 69 6e 4d 61 78 2d 3e 61 5b       pMinMax->a[
2c51a 30 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 66  0].sortOrder = f
2c51b 6c 61 67 21 3d 57 48 45 52 45 5f 4f 52 44 45 52  lag!=WHERE_ORDER
2c51c 42 59 5f 4d 49 4e 20 3f 31 3a 30 3b 0a 20 20 20  BY_MIN ?1:0;.   
2c51d 20 20 20 20 20 20 20 20 20 70 4d 69 6e 4d 61 78           pMinMax
2c51e 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70  ->a[0].pExpr->op
2c51f 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20   = TK_COLUMN;.  
2c520 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2c521 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 20 20 2f    }.  .        /
2c522 2a 20 54 68 69 73 20 63 61 73 65 20 72 75 6e 73  * This case runs
2c523 20 69 66 20 74 68 65 20 61 67 67 72 65 67 61 74   if the aggregat
2c524 65 20 68 61 73 20 6e 6f 20 47 52 4f 55 50 20 42  e has no GROUP B
2c525 59 20 63 6c 61 75 73 65 2e 20 20 54 68 65 0a 20  Y clause.  The. 
2c526 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 73         ** proces
2c527 73 69 6e 67 20 69 73 20 6d 75 63 68 20 73 69 6d  sing is much sim
2c528 70 6c 65 72 20 73 69 6e 63 65 20 74 68 65 72 65  pler since there
2c529 20 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c   is only a singl
2c52a 65 20 72 6f 77 0a 20 20 20 20 20 20 20 20 2a 2a  e row.        **
2c52b 20 6f 66 20 6f 75 74 70 75 74 2e 0a 20 20 20 20   of output..    
2c52c 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72      */.        r
2c52d 65 73 65 74 41 63 63 75 6d 75 6c 61 74 6f 72 28  esetAccumulator(
2c52e 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66  pParse, &sAggInf
2c52f 6f 29 3b 0a 20 20 20 20 20 20 20 20 70 57 49 6e  o);.        pWIn
2c530 66 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72  fo = sqlite3Wher
2c531 65 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70  eBegin(pParse, p
2c532 54 61 62 4c 69 73 74 2c 20 70 57 68 65 72 65 2c  TabList, pWhere,
2c533 20 26 70 4d 69 6e 4d 61 78 2c 20 66 6c 61 67 29   &pMinMax, flag)
2c534 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 57  ;.        if( pW
2c535 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Info==0 ){.     
2c536 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2c537 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2c538 44 65 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Del);.          
2c539 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b  goto select_end;
2c53a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2c53b 20 20 20 75 70 64 61 74 65 41 63 63 75 6d 75 6c     updateAccumul
2c53c 61 74 6f 72 28 70 50 61 72 73 65 2c 20 26 73 41  ator(pParse, &sA
2c53d 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20  ggInfo);.       
2c53e 20 69 66 28 20 21 70 4d 69 6e 4d 61 78 20 26 26   if( !pMinMax &&
2c53f 20 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20   flag ){.       
2c540 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c541 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
2c542 20 30 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65   0, pWInfo->iBre
2c543 61 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 56  ak);.          V
2c544 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
2c545 25 73 28 29 20 62 79 20 69 6e 64 65 78 22 2c 0a  %s() by index",.
2c546 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c547 28 66 6c 61 67 3d 3d 57 48 45 52 45 5f 4f 52 44  (flag==WHERE_ORD
2c548 45 52 42 59 5f 4d 49 4e 3f 22 6d 69 6e 22 3a 22  ERBY_MIN?"min":"
2c549 6d 61 78 22 29 29 29 3b 0a 20 20 20 20 20 20 20  max")));.       
2c54a 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   }.        sqlit
2c54b 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66  e3WhereEnd(pWInf
2c54c 6f 29 3b 0a 20 20 20 20 20 20 20 20 66 69 6e 61  o);.        fina
2c54d 6c 69 7a 65 41 67 67 46 75 6e 63 74 69 6f 6e 73  lizeAggFunctions
2c54e 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e  (pParse, &sAggIn
2c54f 66 6f 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  fo);.      }..  
2c550 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d 20 30      pOrderBy = 0
2c551 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
2c552 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73  xprIfFalse(pPars
2c553 65 2c 20 70 48 61 76 69 6e 67 2c 20 61 64 64 72  e, pHaving, addr
2c554 45 6e 64 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50  End, SQLITE_JUMP
2c555 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73  IFNULL);.      s
2c556 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70  electInnerLoop(p
2c557 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45 4c  Parse, p, p->pEL
2c558 69 73 74 2c 20 30 2c 20 30 2c 20 30 2c 20 2d 31  ist, 0, 0, 0, -1
2c559 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
2c55a 20 20 20 20 20 20 20 20 20 70 44 65 73 74 2c 20           pDest, 
2c55b 61 64 64 72 45 6e 64 2c 20 61 64 64 72 45 6e 64  addrEnd, addrEnd
2c55c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2c55d 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64  ExprListDelete(d
2c55e 62 2c 20 70 44 65 6c 29 3b 0a 20 20 20 20 7d 0a  b, pDel);.    }.
2c55f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
2c560 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61  esolveLabel(v, a
2c561 64 64 72 45 6e 64 29 3b 0a 20 20 20 20 0a 20 20  ddrEnd);.    .  
2c562 7d 20 2f 2a 20 65 6e 64 69 66 20 61 67 67 72 65  } /* endif aggre
2c563 67 61 74 65 20 71 75 65 72 79 20 2a 2f 0a 0a 20  gate query */.. 
2c564 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
2c565 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  an ORDER BY clau
2c566 73 65 2c 20 74 68 65 6e 20 77 65 20 6e 65 65 64  se, then we need
2c567 20 74 6f 20 73 6f 72 74 20 74 68 65 20 72 65 73   to sort the res
2c568 75 6c 74 73 0a 20 20 2a 2a 20 61 6e 64 20 73 65  ults.  ** and se
2c569 6e 64 20 74 68 65 6d 20 74 6f 20 74 68 65 20 63  nd them to the c
2c56a 61 6c 6c 62 61 63 6b 20 6f 6e 65 20 62 79 20 6f  allback one by o
2c56b 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ne..  */.  if( p
2c56c 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 67  OrderBy ){.    g
2c56d 65 6e 65 72 61 74 65 53 6f 72 74 54 61 69 6c 28  enerateSortTail(
2c56e 70 50 61 72 73 65 2c 20 70 2c 20 76 2c 20 70 45  pParse, p, v, pE
2c56f 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70 44 65  List->nExpr, pDe
2c570 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a  st);.  }..  /* J
2c571 75 6d 70 20 68 65 72 65 20 74 6f 20 73 6b 69 70  ump here to skip
2c572 20 74 68 69 73 20 71 75 65 72 79 0a 20 20 2a 2f   this query.  */
2c573 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  .  sqlite3VdbeRe
2c574 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 45  solveLabel(v, iE
2c575 6e 64 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 53  nd);..  /* The S
2c576 45 4c 45 43 54 20 77 61 73 20 73 75 63 63 65 73  ELECT was succes
2c577 73 66 75 6c 6c 79 20 63 6f 64 65 64 2e 20 20 20  sfully coded.   
2c578 53 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 63  Set the return c
2c579 6f 64 65 20 74 6f 20 30 0a 20 20 2a 2a 20 74 6f  ode to 0.  ** to
2c57a 20 69 6e 64 69 63 61 74 65 20 6e 6f 20 65 72 72   indicate no err
2c57b 6f 72 73 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d  ors..  */.  rc =
2c57c 20 30 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 74 72 6f   0;..  /* Contro
2c57d 6c 20 6a 75 6d 70 73 20 74 6f 20 68 65 72 65 20  l jumps to here 
2c57e 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 65  if an error is e
2c57f 6e 63 6f 75 6e 74 65 72 65 64 20 61 62 6f 76 65  ncountered above
2c580 2c 20 6f 72 20 75 70 6f 6e 0a 20 20 2a 2a 20 73  , or upon.  ** s
2c581 75 63 63 65 73 73 66 75 6c 20 63 6f 64 69 6e 67  uccessful coding
2c582 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e 0a   of the SELECT..
2c583 20 20 2a 2f 0a 73 65 6c 65 63 74 5f 65 6e 64 3a    */.select_end:
2c584 0a 0a 20 20 2f 2a 20 49 64 65 6e 74 69 66 79 20  ..  /* Identify 
2c585 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 66 20  column names if 
2c586 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 53  results of the S
2c587 45 4c 45 43 54 20 61 72 65 20 74 6f 20 62 65 20  ELECT are to be 
2c588 6f 75 74 70 75 74 2e 0a 20 20 2a 2f 0a 20 20 69  output..  */.  i
2c589 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
2c58a 20 26 26 20 70 44 65 73 74 2d 3e 65 44 65 73 74   && pDest->eDest
2c58b 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a  ==SRT_Output ){.
2c58c 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c 75      generateColu
2c58d 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20  mnNames(pParse, 
2c58e 70 54 61 62 4c 69 73 74 2c 20 70 45 4c 69 73 74  pTabList, pEList
2c58f 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  );.  }..  sqlite
2c590 33 44 62 46 72 65 65 28 64 62 2c 20 73 41 67 67  3DbFree(db, sAgg
2c591 49 6e 66 6f 2e 61 43 6f 6c 29 3b 0a 20 20 73 71  Info.aCol);.  sq
2c592 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2c593 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 29 3b  sAggInfo.aFunc);
2c594 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2c595 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
2c596 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a  ITE_DEBUG)./*.**
2c597 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c598 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c599 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c59a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c59b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
2c59c 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
2c59d 6f 64 65 20 69 73 20 75 73 65 64 20 66 6f 72 20  ode is used for 
2c59e 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75  testing and debu
2c59f 67 67 69 6e 67 20 6f 6e 6c 79 2e 20 20 54 68 65  gging only.  The
2c5a0 20 63 6f 64 65 0a 2a 2a 20 74 68 61 74 20 66 6f   code.** that fo
2c5a1 6c 6c 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 61  llows does not a
2c5a2 70 70 65 61 72 20 69 6e 20 6e 6f 72 6d 61 6c 20  ppear in normal 
2c5a3 62 75 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  builds..**.** Th
2c5a4 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
2c5a5 20 75 73 65 64 20 74 6f 20 70 72 69 6e 74 20 6f   used to print o
2c5a6 75 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ut the content o
2c5a7 66 20 61 6c 6c 20 6f 72 20 70 61 72 74 20 6f 66  f all or part of
2c5a8 20 61 20 0a 2a 2a 20 70 61 72 73 65 20 73 74 72   a .** parse str
2c5a9 75 63 74 75 72 65 73 20 73 75 63 68 20 61 73 20  uctures such as 
2c5aa 53 65 6c 65 63 74 20 6f 72 20 45 78 70 72 2e 20  Select or Expr. 
2c5ab 20 53 75 63 68 20 70 72 69 6e 74 6f 75 74 73 20   Such printouts 
2c5ac 61 72 65 20 75 73 65 66 75 6c 0a 2a 2a 20 66 6f  are useful.** fo
2c5ad 72 20 68 65 6c 70 69 6e 67 20 74 6f 20 75 6e 64  r helping to und
2c5ae 65 72 73 74 61 6e 64 20 77 68 61 74 20 69 73 20  erstand what is 
2c5af 68 61 70 70 65 6e 69 6e 67 20 69 6e 73 69 64 65  happening inside
2c5b0 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61   the code genera
2c5b1 74 6f 72 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68  tor.** during th
2c5b2 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 63  e execution of c
2c5b3 6f 6d 70 6c 65 78 20 53 45 4c 45 43 54 20 73 74  omplex SELECT st
2c5b4 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  atements..**.** 
2c5b5 54 68 65 73 65 20 72 6f 75 74 69 6e 65 20 61 72  These routine ar
2c5b6 65 20 6e 6f 74 20 63 61 6c 6c 65 64 20 61 6e 79  e not called any
2c5b7 77 68 65 72 65 20 66 72 6f 6d 20 77 69 74 68 69  where from withi
2c5b8 6e 20 74 68 65 20 6e 6f 72 6d 61 6c 0a 2a 2a 20  n the normal.** 
2c5b9 63 6f 64 65 20 62 61 73 65 2e 20 20 54 68 65 6e  code base.  Then
2c5ba 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f   are intended to
2c5bb 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   be called from 
2c5bc 77 69 74 68 69 6e 20 74 68 65 20 64 65 62 75 67  within the debug
2c5bd 67 65 72 0a 2a 2a 20 6f 72 20 66 72 6f 6d 20 74  ger.** or from t
2c5be 65 6d 70 6f 72 61 72 79 20 22 70 72 69 6e 74 66  emporary "printf
2c5bf 22 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 73  " statements ins
2c5c0 65 72 74 65 64 20 66 6f 72 20 64 65 62 75 67 67  erted for debugg
2c5c1 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ing..*/.SQLITE_P
2c5c2 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2c5c3 74 65 33 50 72 69 6e 74 45 78 70 72 28 45 78 70  te3PrintExpr(Exp
2c5c4 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 45 78  r *p){.  if( !Ex
2c5c5 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c  prHasProperty(p,
2c5c6 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 26 26   EP_IntValue) &&
2c5c7 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 29 7b 0a   p->u.zToken ){.
2c5c8 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
2c5c9 50 72 69 6e 74 66 28 22 28 25 73 22 2c 20 70 2d  Printf("(%s", p-
2c5ca 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 7d 65  >u.zToken);.  }e
2c5cb 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
2c5cc 44 65 62 75 67 50 72 69 6e 74 66 28 22 28 25 64  DebugPrintf("(%d
2c5cd 22 2c 20 70 2d 3e 6f 70 29 3b 0a 20 20 7d 0a 20  ", p->op);.  }. 
2c5ce 20 69 66 28 20 70 2d 3e 70 4c 65 66 74 20 29 7b   if( p->pLeft ){
2c5cf 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75  .    sqlite3Debu
2c5d0 67 50 72 69 6e 74 66 28 22 20 22 29 3b 0a 20 20  gPrintf(" ");.  
2c5d1 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78    sqlite3PrintEx
2c5d2 70 72 28 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20  pr(p->pLeft);.  
2c5d3 7d 0a 20 20 69 66 28 20 70 2d 3e 70 52 69 67 68  }.  if( p->pRigh
2c5d4 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
2c5d5 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 22 29  DebugPrintf(" ")
2c5d6 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72 69  ;.    sqlite3Pri
2c5d7 6e 74 45 78 70 72 28 70 2d 3e 70 52 69 67 68 74  ntExpr(p->pRight
2c5d8 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
2c5d9 44 65 62 75 67 50 72 69 6e 74 66 28 22 29 22 29  DebugPrintf(")")
2c5da 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
2c5db 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
2c5dc 72 69 6e 74 45 78 70 72 4c 69 73 74 28 45 78 70  rintExprList(Exp
2c5dd 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20  rList *pList){. 
2c5de 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
2c5df 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70  0; i<pList->nExp
2c5e0 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  r; i++){.    sql
2c5e1 69 74 65 33 50 72 69 6e 74 45 78 70 72 28 70 4c  ite3PrintExpr(pL
2c5e2 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29  ist->a[i].pExpr)
2c5e3 3b 0a 20 20 20 20 69 66 28 20 69 3c 70 4c 69 73  ;.    if( i<pLis
2c5e4 74 2d 3e 6e 45 78 70 72 2d 31 20 29 7b 0a 20 20  t->nExpr-1 ){.  
2c5e5 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
2c5e6 50 72 69 6e 74 66 28 22 2c 20 22 29 3b 0a 20 20  Printf(", ");.  
2c5e7 20 20 7d 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45    }.  }.}.SQLITE
2c5e8 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2c5e9 6c 69 74 65 33 50 72 69 6e 74 53 65 6c 65 63 74  lite3PrintSelect
2c5ea 28 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20  (Select *p, int 
2c5eb 69 6e 64 65 6e 74 29 7b 0a 20 20 73 71 6c 69 74  indent){.  sqlit
2c5ec 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25  e3DebugPrintf("%
2c5ed 2a 73 53 45 4c 45 43 54 28 25 70 29 20 22 2c 20  *sSELECT(%p) ", 
2c5ee 69 6e 64 65 6e 74 2c 20 22 22 2c 20 70 29 3b 0a  indent, "", p);.
2c5ef 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78    sqlite3PrintEx
2c5f0 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73 74  prList(p->pEList
2c5f1 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75  );.  sqlite3Debu
2c5f2 67 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20  gPrintf("\n");. 
2c5f3 20 69 66 28 20 70 2d 3e 70 53 72 63 20 29 7b 0a   if( p->pSrc ){.
2c5f4 20 20 20 20 63 68 61 72 20 2a 7a 50 72 65 66 69      char *zPrefi
2c5f5 78 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  x;.    int i;.  
2c5f6 20 20 7a 50 72 65 66 69 78 20 3d 20 22 46 52 4f    zPrefix = "FRO
2c5f7 4d 22 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  M";.    for(i=0;
2c5f8 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63   i<p->pSrc->nSrc
2c5f9 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74  ; i++){.      st
2c5fa 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
2c5fb 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e 70  m *pItem = &p->p
2c5fc 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20  Src->a[i];.     
2c5fd 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2c5fe 6e 74 66 28 22 25 2a 73 20 22 2c 20 69 6e 64 65  ntf("%*s ", inde
2c5ff 6e 74 2b 36 2c 20 7a 50 72 65 66 69 78 29 3b 0a  nt+6, zPrefix);.
2c600 20 20 20 20 20 20 7a 50 72 65 66 69 78 20 3d 20        zPrefix = 
2c601 22 22 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  "";.      if( pI
2c602 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  tem->pSelect ){.
2c603 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
2c604 65 62 75 67 50 72 69 6e 74 66 28 22 28 5c 6e 22  ebugPrintf("(\n"
2c605 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2c606 65 33 50 72 69 6e 74 53 65 6c 65 63 74 28 70 49  e3PrintSelect(pI
2c607 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 69 6e  tem->pSelect, in
2c608 64 65 6e 74 2b 31 30 29 3b 0a 20 20 20 20 20 20  dent+10);.      
2c609 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
2c60a 69 6e 74 66 28 22 25 2a 73 29 22 2c 20 69 6e 64  intf("%*s)", ind
2c60b 65 6e 74 2b 38 2c 20 22 22 29 3b 0a 20 20 20 20  ent+8, "");.    
2c60c 20 20 7d 65 6c 73 65 20 69 66 28 20 70 49 74 65    }else if( pIte
2c60d 6d 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20  m->zName ){.    
2c60e 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
2c60f 50 72 69 6e 74 66 28 22 25 73 22 2c 20 70 49 74  Printf("%s", pIt
2c610 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  em->zName);.    
2c611 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 49    }.      if( pI
2c612 74 65 6d 2d 3e 70 54 61 62 20 29 7b 0a 20 20 20  tem->pTab ){.   
2c613 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75       sqlite3Debu
2c614 67 50 72 69 6e 74 66 28 22 28 74 61 62 6c 65 3a  gPrintf("(table:
2c615 20 25 73 29 22 2c 20 70 49 74 65 6d 2d 3e 70 54   %s)", pItem->pT
2c616 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab->zName);.    
2c617 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 49    }.      if( pI
2c618 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20  tem->zAlias ){. 
2c619 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65         sqlite3De
2c61a 62 75 67 50 72 69 6e 74 66 28 22 20 41 53 20 25  bugPrintf(" AS %
2c61b 73 22 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61  s", pItem->zAlia
2c61c 73 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  s);.      }.    
2c61d 20 20 69 66 28 20 69 3c 70 2d 3e 70 53 72 63 2d    if( i<p->pSrc-
2c61e 3e 6e 53 72 63 2d 31 20 29 7b 0a 20 20 20 20 20  >nSrc-1 ){.     
2c61f 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
2c620 72 69 6e 74 66 28 22 2c 22 29 3b 0a 20 20 20 20  rintf(",");.    
2c621 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
2c622 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e  3DebugPrintf("\n
2c623 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ");.    }.  }.  
2c624 69 66 28 20 70 2d 3e 70 57 68 65 72 65 20 29 7b  if( p->pWhere ){
2c625 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75  .    sqlite3Debu
2c626 67 50 72 69 6e 74 66 28 22 25 2a 73 20 57 48 45  gPrintf("%*s WHE
2c627 52 45 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22  RE ", indent, ""
2c628 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72  );.    sqlite3Pr
2c629 69 6e 74 45 78 70 72 28 70 2d 3e 70 57 68 65 72  intExpr(p->pWher
2c62a 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  e);.    sqlite3D
2c62b 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22 29  ebugPrintf("\n")
2c62c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70  ;.  }.  if( p->p
2c62d 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 73  GroupBy ){.    s
2c62e 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2c62f 66 28 22 25 2a 73 20 47 52 4f 55 50 20 42 59 20  f("%*s GROUP BY 
2c630 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22 29 3b 0a  ", indent, "");.
2c631 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74      sqlite3Print
2c632 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 47 72 6f  ExprList(p->pGro
2c633 75 70 42 79 29 3b 0a 20 20 20 20 73 71 6c 69 74  upBy);.    sqlit
2c634 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c  e3DebugPrintf("\
2c635 6e 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  n");.  }.  if( p
2c636 2d 3e 70 48 61 76 69 6e 67 20 29 7b 0a 20 20 20  ->pHaving ){.   
2c637 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2c638 6e 74 66 28 22 25 2a 73 20 48 41 56 49 4e 47 20  ntf("%*s HAVING 
2c639 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22 29 3b 0a  ", indent, "");.
2c63a 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74      sqlite3Print
2c63b 45 78 70 72 28 70 2d 3e 70 48 61 76 69 6e 67 29  Expr(p->pHaving)
2c63c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62  ;.    sqlite3Deb
2c63d 75 67 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a  ugPrintf("\n");.
2c63e 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4f 72    }.  if( p->pOr
2c63f 64 65 72 42 79 20 29 7b 0a 20 20 20 20 73 71 6c  derBy ){.    sql
2c640 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
2c641 22 25 2a 73 20 4f 52 44 45 52 20 42 59 20 22 2c  "%*s ORDER BY ",
2c642 20 69 6e 64 65 6e 74 2c 20 22 22 29 3b 0a 20 20   indent, "");.  
2c643 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78    sqlite3PrintEx
2c644 70 72 4c 69 73 74 28 70 2d 3e 70 4f 72 64 65 72  prList(p->pOrder
2c645 42 79 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  By);.    sqlite3
2c646 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22  DebugPrintf("\n"
2c647 29 3b 0a 20 20 7d 0a 7d 0a 2f 2a 20 45 6e 64 20  );.  }.}./* End 
2c648 6f 66 20 74 68 65 20 73 74 72 75 63 74 75 72 65  of the structure
2c649 20 64 65 62 75 67 20 70 72 69 6e 74 69 6e 67 20   debug printing 
2c64a 63 6f 64 65 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  code.***********
2c64b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c64c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c64d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c64e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c64f 2a 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65  **/.#endif /* de
2c650 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53  fined(SQLITE_TES
2c651 54 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  T) || defined(SQ
2c652 4c 49 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a 0a  LITE_DEBUG) */..
2c653 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2c654 45 6e 64 20 6f 66 20 73 65 6c 65 63 74 2e 63 20  End of select.c 
2c655 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c656 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c657 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2c658 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2c659 42 65 67 69 6e 20 66 69 6c 65 20 74 61 62 6c 65  Begin file table
2c65a 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2c65b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c65c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2c65d 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
2c65e 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
2c65f 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
2c660 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
2c661 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
2c662 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
2c663 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
2c664 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
2c665 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
2c666 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
2c667 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
2c668 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
2c669 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
2c66a 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
2c66b 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
2c66c 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
2c66d 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
2c66e 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
2c66f 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
2c670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c671 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c672 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c673 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c674 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
2c675 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ile contains the
2c676 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62   sqlite3_get_tab
2c677 6c 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  le() and sqlite3
2c678 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 0a 2a 2a  _free_table().**
2c679 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69   interface routi
2c67a 6e 65 73 2e 20 20 54 68 65 73 65 20 61 72 65 20  nes.  These are 
2c67b 6a 75 73 74 20 77 72 61 70 70 65 72 73 20 61 72  just wrappers ar
2c67c 6f 75 6e 64 20 74 68 65 20 6d 61 69 6e 0a 2a 2a  ound the main.**
2c67d 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69   interface routi
2c67e 6e 65 20 6f 66 20 73 71 6c 69 74 65 33 5f 65 78  ne of sqlite3_ex
2c67f 65 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  ec()..**.** Thes
2c680 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 69  e routines are i
2c681 6e 20 61 20 73 65 70 61 72 61 74 65 20 66 69 6c  n a separate fil
2c682 65 73 20 73 6f 20 74 68 61 74 20 74 68 65 79 20  es so that they 
2c683 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6c 69 6e 6b  will not be link
2c684 65 64 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 72  ed.** if they ar
2c685 65 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2a 0a 2a  e not used..**.*
2c686 2a 20 24 49 64 3a 20 74 61 62 6c 65 2e 63 2c 76  * $Id: table.c,v
2c687 20 31 2e 34 30 20 32 30 30 39 2f 30 34 2f 31 30   1.40 2009/04/10
2c688 20 31 34 3a 32 38 3a 30 30 20 64 72 68 20 45 78   14:28:00 drh Ex
2c689 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  p $.*/..#ifndef 
2c68a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54 5f  SQLITE_OMIT_GET_
2c68b 54 41 42 4c 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  TABLE../*.** Thi
2c68c 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75  s structure is u
2c68d 73 65 64 20 74 6f 20 70 61 73 73 20 64 61 74 61  sed to pass data
2c68e 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 67 65   from sqlite3_ge
2c68f 74 5f 74 61 62 6c 65 28 29 20 74 68 72 6f 75 67  t_table() throug
2c690 68 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c  h.** to the call
2c691 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 69 73  back function is
2c692 20 75 73 65 73 20 74 6f 20 62 75 69 6c 64 20 74   uses to build t
2c693 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a 74 79  he result..*/.ty
2c694 70 65 64 65 66 20 73 74 72 75 63 74 20 54 61 62  pedef struct Tab
2c695 52 65 73 75 6c 74 20 7b 0a 20 20 63 68 61 72 20  Result {.  char 
2c696 2a 2a 61 7a 52 65 73 75 6c 74 3b 20 20 20 2f 2a  **azResult;   /*
2c697 20 41 63 63 75 6d 75 6c 61 74 65 64 20 6f 75 74   Accumulated out
2c698 70 75 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  put */.  char *z
2c699 45 72 72 4d 73 67 3b 20 20 20 20 20 2f 2a 20 45  ErrMsg;     /* E
2c69a 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78  rror message tex
2c69b 74 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f  t, if an error o
2c69c 63 63 75 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  ccurs */.  int n
2c69d 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 2f 2a  Alloc;        /*
2c69e 20 53 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64   Slots allocated
2c69f 20 66 6f 72 20 61 7a 52 65 73 75 6c 74 5b 5d 20   for azResult[] 
2c6a0 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f 77 3b 20 20  */.  int nRow;  
2c6a1 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2c6a2 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65  r of rows in the
2c6a3 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 69 6e 74   result */.  int
2c6a4 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20   nColumn;       
2c6a5 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  /* Number of col
2c6a6 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75  umns in the resu
2c6a7 6c 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74  lt */.  int nDat
2c6a8 61 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6c  a;         /* Sl
2c6a9 6f 74 73 20 75 73 65 64 20 69 6e 20 61 7a 52 65  ots used in azRe
2c6aa 73 75 6c 74 5b 5d 2e 20 20 28 6e 52 6f 77 2b 31  sult[].  (nRow+1
2c6ab 29 2a 6e 43 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69  )*nColumn */.  i
2c6ac 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
2c6ad 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
2c6ae 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 65 78   from sqlite3_ex
2c6af 65 63 28 29 20 2a 2f 0a 7d 20 54 61 62 52 65 73  ec() */.} TabRes
2c6b0 75 6c 74 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ult;../*.** This
2c6b1 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
2c6b2 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ed once for each
2c6b3 20 72 6f 77 20 69 6e 20 74 68 65 20 72 65 73 75   row in the resu
2c6b4 6c 74 20 74 61 62 6c 65 2e 20 20 49 74 73 20 6a  lt table.  Its j
2c6b5 6f 62 0a 2a 2a 20 69 73 20 74 6f 20 66 69 6c 6c  ob.** is to fill
2c6b6 20 69 6e 20 74 68 65 20 54 61 62 52 65 73 75 6c   in the TabResul
2c6b7 74 20 73 74 72 75 63 74 75 72 65 20 61 70 70 72  t structure appr
2c6b8 6f 70 72 69 61 74 65 6c 79 2c 20 61 6c 6c 6f 63  opriately, alloc
2c6b9 61 74 69 6e 67 20 6e 65 77 0a 2a 2a 20 6d 65 6d  ating new.** mem
2c6ba 6f 72 79 20 61 73 20 6e 65 63 65 73 73 61 72 79  ory as necessary
2c6bb 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2c6bc 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
2c6bd 65 5f 63 62 28 76 6f 69 64 20 2a 70 41 72 67 2c  e_cb(void *pArg,
2c6be 20 69 6e 74 20 6e 43 6f 6c 2c 20 63 68 61 72 20   int nCol, char 
2c6bf 2a 2a 61 72 67 76 2c 20 63 68 61 72 20 2a 2a 63  **argv, char **c
2c6c0 6f 6c 76 29 7b 0a 20 20 54 61 62 52 65 73 75 6c  olv){.  TabResul
2c6c1 74 20 2a 70 20 3d 20 28 54 61 62 52 65 73 75 6c  t *p = (TabResul
2c6c2 74 2a 29 70 41 72 67 3b 20 20 2f 2a 20 52 65 73  t*)pArg;  /* Res
2c6c3 75 6c 74 20 61 63 63 75 6d 75 6c 61 74 6f 72 20  ult accumulator 
2c6c4 2a 2f 0a 20 20 69 6e 74 20 6e 65 65 64 3b 20 20  */.  int need;  
2c6c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c6c6 20 20 20 20 20 20 20 2f 2a 20 53 6c 6f 74 73 20         /* Slots 
2c6c7 6e 65 65 64 65 64 20 69 6e 20 70 2d 3e 61 7a 52  needed in p->azR
2c6c8 65 73 75 6c 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74  esult[] */.  int
2c6c9 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
2c6ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2c6cb 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
2c6cc 2f 0a 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20  /.  char *z;    
2c6cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c6ce 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c        /* A singl
2c6cf 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 72 65 73 75  e column of resu
2c6d0 6c 74 20 2a 2f 0a 0a 20 20 2f 2a 20 4d 61 6b 65  lt */..  /* Make
2c6d1 20 73 75 72 65 20 74 68 65 72 65 20 69 73 20 65   sure there is e
2c6d2 6e 6f 75 67 68 20 73 70 61 63 65 20 69 6e 20 70  nough space in p
2c6d3 2d 3e 61 7a 52 65 73 75 6c 74 20 74 6f 20 68 6f  ->azResult to ho
2c6d4 6c 64 20 65 76 65 72 79 74 68 69 6e 67 0a 20 20  ld everything.  
2c6d5 2a 2a 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65  ** we need to re
2c6d6 6d 65 6d 62 65 72 20 66 72 6f 6d 20 74 68 69 73  member from this
2c6d7 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74   invocation of t
2c6d8 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 20 20 2a  he callback..  *
2c6d9 2f 0a 20 20 69 66 28 20 70 2d 3e 6e 52 6f 77 3d  /.  if( p->nRow=
2c6da 3d 30 20 26 26 20 61 72 67 76 21 3d 30 20 29 7b  =0 && argv!=0 ){
2c6db 0a 20 20 20 20 6e 65 65 64 20 3d 20 6e 43 6f 6c  .    need = nCol
2c6dc 2a 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  *2;.  }else{.   
2c6dd 20 6e 65 65 64 20 3d 20 6e 43 6f 6c 3b 0a 20 20   need = nCol;.  
2c6de 7d 0a 20 20 69 66 28 20 70 2d 3e 6e 44 61 74 61  }.  if( p->nData
2c6df 20 2b 20 6e 65 65 64 20 3e 20 70 2d 3e 6e 41 6c   + need > p->nAl
2c6e0 6c 6f 63 20 29 7b 0a 20 20 20 20 63 68 61 72 20  loc ){.    char 
2c6e1 2a 2a 61 7a 4e 65 77 3b 0a 20 20 20 20 70 2d 3e  **azNew;.    p->
2c6e2 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 41 6c 6c  nAlloc = p->nAll
2c6e3 6f 63 2a 32 20 2b 20 6e 65 65 64 3b 0a 20 20 20  oc*2 + need;.   
2c6e4 20 61 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33   azNew = sqlite3
2c6e5 5f 72 65 61 6c 6c 6f 63 28 20 70 2d 3e 61 7a 52  _realloc( p->azR
2c6e6 65 73 75 6c 74 2c 20 73 69 7a 65 6f 66 28 63 68  esult, sizeof(ch
2c6e7 61 72 2a 29 2a 70 2d 3e 6e 41 6c 6c 6f 63 20 29  ar*)*p->nAlloc )
2c6e8 3b 0a 20 20 20 20 69 66 28 20 61 7a 4e 65 77 3d  ;.    if( azNew=
2c6e9 3d 30 20 29 20 67 6f 74 6f 20 6d 61 6c 6c 6f 63  =0 ) goto malloc
2c6ea 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 70 2d 3e  _failed;.    p->
2c6eb 61 7a 52 65 73 75 6c 74 20 3d 20 61 7a 4e 65 77  azResult = azNew
2c6ec 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
2c6ed 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74  his is the first
2c6ee 20 72 6f 77 2c 20 74 68 65 6e 20 67 65 6e 65 72   row, then gener
2c6ef 61 74 65 20 61 6e 20 65 78 74 72 61 20 72 6f 77  ate an extra row
2c6f0 20 63 6f 6e 74 61 69 6e 69 6e 67 0a 20 20 2a 2a   containing.  **
2c6f1 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c   the names of al
2c6f2 6c 20 63 6f 6c 75 6d 6e 73 2e 0a 20 20 2a 2f 0a  l columns..  */.
2c6f3 20 20 69 66 28 20 70 2d 3e 6e 52 6f 77 3d 3d 30    if( p->nRow==0
2c6f4 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 43 6f 6c 75   ){.    p->nColu
2c6f5 6d 6e 20 3d 20 6e 43 6f 6c 3b 0a 20 20 20 20 66  mn = nCol;.    f
2c6f6 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20  or(i=0; i<nCol; 
2c6f7 69 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 20 3d 20  i++){.      z = 
2c6f8 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
2c6f9 22 25 73 22 2c 20 63 6f 6c 76 5b 69 5d 29 3b 0a  "%s", colv[i]);.
2c6fa 20 20 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29        if( z==0 )
2c6fb 20 67 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69   goto malloc_fai
2c6fc 6c 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e 61 7a  led;.      p->az
2c6fd 52 65 73 75 6c 74 5b 70 2d 3e 6e 44 61 74 61 2b  Result[p->nData+
2c6fe 2b 5d 20 3d 20 7a 3b 0a 20 20 20 20 7d 0a 20 20  +] = z;.    }.  
2c6ff 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6e 43 6f  }else if( p->nCo
2c700 6c 75 6d 6e 21 3d 6e 43 6f 6c 20 29 7b 0a 20 20  lumn!=nCol ){.  
2c701 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
2c702 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  ->zErrMsg);.    
2c703 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c  p->zErrMsg = sql
2c704 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20  ite3_mprintf(.  
2c705 20 20 20 20 20 22 73 71 6c 69 74 65 33 5f 67 65       "sqlite3_ge
2c706 74 5f 74 61 62 6c 65 28 29 20 63 61 6c 6c 65 64  t_table() called
2c707 20 77 69 74 68 20 74 77 6f 20 6f 72 20 6d 6f 72   with two or mor
2c708 65 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 71  e incompatible q
2c709 75 65 72 69 65 73 22 0a 20 20 20 20 29 3b 0a 20  ueries".    );. 
2c70a 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54     p->rc = SQLIT
2c70b 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 72 65 74  E_ERROR;.    ret
2c70c 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  urn 1;.  }..  /*
2c70d 20 43 6f 70 79 20 6f 76 65 72 20 74 68 65 20 72   Copy over the r
2c70e 6f 77 20 64 61 74 61 0a 20 20 2a 2f 0a 20 20 69  ow data.  */.  i
2c70f 66 28 20 61 72 67 76 21 3d 30 20 29 7b 0a 20 20  f( argv!=0 ){.  
2c710 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
2c711 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  l; i++){.      i
2c712 66 28 20 61 72 67 76 5b 69 5d 3d 3d 30 20 29 7b  f( argv[i]==0 ){
2c713 0a 20 20 20 20 20 20 20 20 7a 20 3d 20 30 3b 0a  .        z = 0;.
2c714 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2c715 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c       int n = sql
2c716 69 74 65 33 53 74 72 6c 65 6e 33 30 28 61 72 67  ite3Strlen30(arg
2c717 76 5b 69 5d 29 2b 31 3b 0a 20 20 20 20 20 20 20  v[i])+1;.       
2c718 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c   z = sqlite3_mal
2c719 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 20 20 20 20  loc( n );.      
2c71a 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 67 6f 74    if( z==0 ) got
2c71b 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b  o malloc_failed;
2c71c 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
2c71d 7a 2c 20 61 72 67 76 5b 69 5d 2c 20 6e 29 3b 0a  z, argv[i], n);.
2c71e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d        }.      p-
2c71f 3e 61 7a 52 65 73 75 6c 74 5b 70 2d 3e 6e 44 61  >azResult[p->nDa
2c720 74 61 2b 2b 5d 20 3d 20 7a 3b 0a 20 20 20 20 7d  ta++] = z;.    }
2c721 0a 20 20 20 20 70 2d 3e 6e 52 6f 77 2b 2b 3b 0a  .    p->nRow++;.
2c722 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
2c723 0a 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3a 0a  .malloc_failed:.
2c724 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45    p->rc = SQLITE
2c725 5f 4e 4f 4d 45 4d 3b 0a 20 20 72 65 74 75 72 6e  _NOMEM;.  return
2c726 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65   1;.}../*.** Que
2c727 72 79 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  ry the database.
2c728 20 20 42 75 74 20 69 6e 73 74 65 61 64 20 6f 66    But instead of
2c729 20 69 6e 76 6f 6b 69 6e 67 20 61 20 63 61 6c 6c   invoking a call
2c72a 62 61 63 6b 20 66 6f 72 20 65 61 63 68 20 72 6f  back for each ro
2c72b 77 2c 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 66  w,.** malloc() f
2c72c 6f 72 20 73 70 61 63 65 20 74 6f 20 68 6f 6c 64  or space to hold
2c72d 20 74 68 65 20 72 65 73 75 6c 74 20 61 6e 64 20   the result and 
2c72e 72 65 74 75 72 6e 20 74 68 65 20 65 6e 74 69 72  return the entir
2c72f 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 61 74 20  e results.** at 
2c730 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f  the conclusion o
2c731 66 20 74 68 65 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a  f the call..**.*
2c732 2a 20 54 68 65 20 72 65 73 75 6c 74 20 74 68 61  * The result tha
2c733 74 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20  t is written to 
2c734 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 20 69 73 20  ***pazResult is 
2c735 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f  held in memory o
2c736 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20  btained.** from 
2c737 6d 61 6c 6c 6f 63 28 29 2e 20 20 42 75 74 20 74  malloc().  But t
2c738 68 65 20 63 61 6c 6c 65 72 20 63 61 6e 6e 6f 74  he caller cannot
2c739 20 66 72 65 65 20 74 68 69 73 20 6d 65 6d 6f 72   free this memor
2c73a 79 20 64 69 72 65 63 74 6c 79 2e 20 20 0a 2a 2a  y directly.  .**
2c73b 20 49 6e 73 74 65 61 64 2c 20 74 68 65 20 65 6e   Instead, the en
2c73c 74 69 72 65 20 74 61 62 6c 65 20 73 68 6f 75 6c  tire table shoul
2c73d 64 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 73  d be passed to s
2c73e 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c  qlite3_free_tabl
2c73f 65 28 29 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20  e() when.** the 
2c740 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 72  calling procedur
2c741 65 20 69 73 20 66 69 6e 69 73 68 65 64 20 75 73  e is finished us
2c742 69 6e 67 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54  ing it..*/.SQLIT
2c743 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2c744 33 5f 67 65 74 5f 74 61 62 6c 65 28 0a 20 20 73  3_get_table(.  s
2c745 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
2c746 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2c747 65 20 64 61 74 61 62 61 73 65 20 6f 6e 20 77 68  e database on wh
2c748 69 63 68 20 74 68 65 20 53 51 4c 20 65 78 65 63  ich the SQL exec
2c749 75 74 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  utes */.  const 
2c74a 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20  char *zSql,     
2c74b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 51 4c        /* The SQL
2c74c 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 20   to be executed 
2c74d 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 2a 70 61 7a  */.  char ***paz
2c74e 52 65 73 75 6c 74 2c 20 20 20 20 20 20 20 20 20  Result,         
2c74f 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72 65   /* Write the re
2c750 73 75 6c 74 20 74 61 62 6c 65 20 68 65 72 65 20  sult table here 
2c751 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 6f 77 2c  */.  int *pnRow,
2c752 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c753 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75   /* Write the nu
2c754 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20  mber of rows in 
2c755 74 68 65 20 72 65 73 75 6c 74 20 68 65 72 65 20  the result here 
2c756 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 75  */.  int *pnColu
2c757 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  mn,             
2c758 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75   /* Write the nu
2c759 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
2c75a 6f 66 20 72 65 73 75 6c 74 20 68 65 72 65 20 2a  of result here *
2c75b 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  /.  char **pzErr
2c75c 4d 73 67 20 20 20 20 20 20 20 20 20 20 20 20 20  Msg             
2c75d 2f 2a 20 57 72 69 74 65 20 65 72 72 6f 72 20 6d  /* Write error m
2c75e 65 73 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a  essages here */.
2c75f 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 54  ){.  int rc;.  T
2c760 61 62 52 65 73 75 6c 74 20 72 65 73 3b 0a 0a 20  abResult res;.. 
2c761 20 2a 70 61 7a 52 65 73 75 6c 74 20 3d 20 30 3b   *pazResult = 0;
2c762 0a 20 20 69 66 28 20 70 6e 43 6f 6c 75 6d 6e 20  .  if( pnColumn 
2c763 29 20 2a 70 6e 43 6f 6c 75 6d 6e 20 3d 20 30 3b  ) *pnColumn = 0;
2c764 0a 20 20 69 66 28 20 70 6e 52 6f 77 20 29 20 2a  .  if( pnRow ) *
2c765 70 6e 52 6f 77 20 3d 20 30 3b 0a 20 20 69 66 28  pnRow = 0;.  if(
2c766 20 70 7a 45 72 72 4d 73 67 20 29 20 2a 70 7a 45   pzErrMsg ) *pzE
2c767 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 72 65 73  rrMsg = 0;.  res
2c768 2e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  .zErrMsg = 0;.  
2c769 72 65 73 2e 6e 52 6f 77 20 3d 20 30 3b 0a 20 20  res.nRow = 0;.  
2c76a 72 65 73 2e 6e 43 6f 6c 75 6d 6e 20 3d 20 30 3b  res.nColumn = 0;
2c76b 0a 20 20 72 65 73 2e 6e 44 61 74 61 20 3d 20 31  .  res.nData = 1
2c76c 3b 0a 20 20 72 65 73 2e 6e 41 6c 6c 6f 63 20 3d  ;.  res.nAlloc =
2c76d 20 32 30 3b 0a 20 20 72 65 73 2e 72 63 20 3d 20   20;.  res.rc = 
2c76e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 72 65 73  SQLITE_OK;.  res
2c76f 2e 61 7a 52 65 73 75 6c 74 20 3d 20 73 71 6c 69  .azResult = sqli
2c770 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
2c771 66 28 63 68 61 72 2a 29 2a 72 65 73 2e 6e 41 6c  f(char*)*res.nAl
2c772 6c 6f 63 20 29 3b 0a 20 20 69 66 28 20 72 65 73  loc );.  if( res
2c773 2e 61 7a 52 65 73 75 6c 74 3d 3d 30 20 29 7b 0a  .azResult==0 ){.
2c774 20 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65       db->errCode
2c775 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
2c776 0a 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  .     return SQL
2c777 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
2c778 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 30 5d   res.azResult[0]
2c779 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c   = 0;.  rc = sql
2c77a 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53  ite3_exec(db, zS
2c77b 71 6c 2c 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  ql, sqlite3_get_
2c77c 74 61 62 6c 65 5f 63 62 2c 20 26 72 65 73 2c 20  table_cb, &res, 
2c77d 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 61 73 73  pzErrMsg);.  ass
2c77e 65 72 74 28 20 73 69 7a 65 6f 66 28 72 65 73 2e  ert( sizeof(res.
2c77f 61 7a 52 65 73 75 6c 74 5b 30 5d 29 3e 3d 20 73  azResult[0])>= s
2c780 69 7a 65 6f 66 28 72 65 73 2e 6e 44 61 74 61 29  izeof(res.nData)
2c781 20 29 3b 0a 20 20 72 65 73 2e 61 7a 52 65 73 75   );.  res.azResu
2c782 6c 74 5b 30 5d 20 3d 20 53 51 4c 49 54 45 5f 49  lt[0] = SQLITE_I
2c783 4e 54 5f 54 4f 5f 50 54 52 28 72 65 73 2e 6e 44  NT_TO_PTR(res.nD
2c784 61 74 61 29 3b 0a 20 20 69 66 28 20 28 72 63 26  ata);.  if( (rc&
2c785 30 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f 41 42  0xff)==SQLITE_AB
2c786 4f 52 54 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ORT ){.    sqlit
2c787 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 26 72  e3_free_table(&r
2c788 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 29 3b  es.azResult[1]);
2c789 0a 20 20 20 20 69 66 28 20 72 65 73 2e 7a 45 72  .    if( res.zEr
2c78a 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 69 66  rMsg ){.      if
2c78b 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20  ( pzErrMsg ){.  
2c78c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
2c78d 65 65 28 2a 70 7a 45 72 72 4d 73 67 29 3b 0a 20  ee(*pzErrMsg);. 
2c78e 20 20 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67         *pzErrMsg
2c78f 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
2c790 74 66 28 22 25 73 22 2c 72 65 73 2e 7a 45 72 72  tf("%s",res.zErr
2c791 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Msg);.      }.  
2c792 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
2c793 28 72 65 73 2e 7a 45 72 72 4d 73 67 29 3b 0a 20  (res.zErrMsg);. 
2c794 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 65 72 72     }.    db->err
2c795 43 6f 64 65 20 3d 20 72 65 73 2e 72 63 3b 20 20  Code = res.rc;  
2c796 2f 2a 20 41 73 73 75 6d 65 20 33 32 2d 62 69 74  /* Assume 32-bit
2c797 20 61 73 73 69 67 6e 6d 65 6e 74 20 69 73 20 61   assignment is a
2c798 74 6f 6d 69 63 20 2a 2f 0a 20 20 20 20 72 65 74  tomic */.    ret
2c799 75 72 6e 20 72 65 73 2e 72 63 3b 0a 20 20 7d 0a  urn res.rc;.  }.
2c79a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 72    sqlite3_free(r
2c79b 65 73 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 69  es.zErrMsg);.  i
2c79c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2c79d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
2c79e 66 72 65 65 5f 74 61 62 6c 65 28 26 72 65 73 2e  free_table(&res.
2c79f 61 7a 52 65 73 75 6c 74 5b 31 5d 29 3b 0a 20 20  azResult[1]);.  
2c7a0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
2c7a1 0a 20 20 69 66 28 20 72 65 73 2e 6e 41 6c 6c 6f  .  if( res.nAllo
2c7a2 63 3e 72 65 73 2e 6e 44 61 74 61 20 29 7b 0a 20  c>res.nData ){. 
2c7a3 20 20 20 63 68 61 72 20 2a 2a 61 7a 4e 65 77 3b     char **azNew;
2c7a4 0a 20 20 20 20 61 7a 4e 65 77 20 3d 20 73 71 6c  .    azNew = sql
2c7a5 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 20 72 65  ite3_realloc( re
2c7a6 73 2e 61 7a 52 65 73 75 6c 74 2c 20 73 69 7a 65  s.azResult, size
2c7a7 6f 66 28 63 68 61 72 2a 29 2a 72 65 73 2e 6e 44  of(char*)*res.nD
2c7a8 61 74 61 20 29 3b 0a 20 20 20 20 69 66 28 20 61  ata );.    if( a
2c7a9 7a 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20  zNew==0 ){.     
2c7aa 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61   sqlite3_free_ta
2c7ab 62 6c 65 28 26 72 65 73 2e 61 7a 52 65 73 75 6c  ble(&res.azResul
2c7ac 74 5b 31 5d 29 3b 0a 20 20 20 20 20 20 64 62 2d  t[1]);.      db-
2c7ad 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54  >errCode = SQLIT
2c7ae 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 72  E_NOMEM;.      r
2c7af 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
2c7b0 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  EM;.    }.    re
2c7b1 73 2e 61 7a 52 65 73 75 6c 74 20 3d 20 61 7a 4e  s.azResult = azN
2c7b2 65 77 3b 0a 20 20 7d 0a 20 20 2a 70 61 7a 52 65  ew;.  }.  *pazRe
2c7b3 73 75 6c 74 20 3d 20 26 72 65 73 2e 61 7a 52 65  sult = &res.azRe
2c7b4 73 75 6c 74 5b 31 5d 3b 0a 20 20 69 66 28 20 70  sult[1];.  if( p
2c7b5 6e 43 6f 6c 75 6d 6e 20 29 20 2a 70 6e 43 6f 6c  nColumn ) *pnCol
2c7b6 75 6d 6e 20 3d 20 72 65 73 2e 6e 43 6f 6c 75 6d  umn = res.nColum
2c7b7 6e 3b 0a 20 20 69 66 28 20 70 6e 52 6f 77 20 29  n;.  if( pnRow )
2c7b8 20 2a 70 6e 52 6f 77 20 3d 20 72 65 73 2e 6e 52   *pnRow = res.nR
2c7b9 6f 77 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ow;.  return rc;
2c7ba 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
2c7bb 6f 75 74 69 6e 65 20 66 72 65 65 73 20 74 68 65  outine frees the
2c7bc 20 73 70 61 63 65 20 74 68 65 20 73 71 6c 69 74   space the sqlit
2c7bd 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 6d  e3_get_table() m
2c7be 61 6c 6c 6f 63 65 64 2e 0a 2a 2f 0a 53 51 4c 49  alloced..*/.SQLI
2c7bf 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
2c7c0 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 0a  te3_free_table(.
2c7c1 20 20 63 68 61 72 20 2a 2a 61 7a 52 65 73 75 6c    char **azResul
2c7c2 74 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t            /* 
2c7c3 52 65 73 75 6c 74 20 72 65 74 75 72 6e 65 64 20  Result returned 
2c7c4 66 72 6f 6d 20 66 72 6f 6d 20 73 71 6c 69 74 65  from from sqlite
2c7c5 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 2a 2f  3_get_table() */
2c7c6 0a 29 7b 0a 20 20 69 66 28 20 61 7a 52 65 73 75  .){.  if( azResu
2c7c7 6c 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c  lt ){.    int i,
2c7c8 20 6e 3b 0a 20 20 20 20 61 7a 52 65 73 75 6c 74   n;.    azResult
2c7c9 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  --;.    assert( 
2c7ca 61 7a 52 65 73 75 6c 74 21 3d 30 20 29 3b 0a 20  azResult!=0 );. 
2c7cb 20 20 20 6e 20 3d 20 53 51 4c 49 54 45 5f 50 54     n = SQLITE_PT
2c7cc 52 5f 54 4f 5f 49 4e 54 28 61 7a 52 65 73 75 6c  R_TO_INT(azResul
2c7cd 74 5b 30 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69  t[0]);.    for(i
2c7ce 3d 31 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 20 69  =1; i<n; i++){ i
2c7cf 66 28 20 61 7a 52 65 73 75 6c 74 5b 69 5d 20 29  f( azResult[i] )
2c7d0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 7a   sqlite3_free(az
2c7d1 52 65 73 75 6c 74 5b 69 5d 29 3b 20 7d 0a 20 20  Result[i]); }.  
2c7d2 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61    sqlite3_free(a
2c7d3 7a 52 65 73 75 6c 74 29 3b 0a 20 20 7d 0a 7d 0a  zResult);.  }.}.
2c7d4 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2c7d5 45 5f 4f 4d 49 54 5f 47 45 54 5f 54 41 42 4c 45  E_OMIT_GET_TABLE
2c7d6 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
2c7d7 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 61 62 6c  **** End of tabl
2c7d8 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
2c7d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c7da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c7db 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2c7dc 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
2c7dd 74 72 69 67 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a  trigger.c ******
2c7de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c7df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c7e0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 0a 2a 2a 20 54 68  ***/./*.**.** Th
2c7e1 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
2c7e2 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
2c7e3 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
2c7e4 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
2c7e5 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
2c7e6 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
2c7e7 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
2c7e8 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
2c7e9 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
2c7ea 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
2c7eb 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
2c7ec 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
2c7ed 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
2c7ee 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
2c7ef 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
2c7f0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
2c7f1 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
2c7f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c7f6 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20  ******.**.**.** 
2c7f7 24 49 64 3a 20 74 72 69 67 67 65 72 2e 63 2c 76  $Id: trigger.c,v
2c7f8 20 31 2e 31 34 33 20 32 30 30 39 2f 30 38 2f 31   1.143 2009/08/1
2c7f9 30 20 30 33 3a 35 37 3a 35 38 20 73 68 61 6e 65  0 03:57:58 shane
2c7fa 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64   Exp $.*/..#ifnd
2c7fb 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
2c7fc 52 49 47 47 45 52 0a 2f 2a 0a 2a 2a 20 44 65 6c  RIGGER./*.** Del
2c7fd 65 74 65 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73  ete a linked lis
2c7fe 74 20 6f 66 20 54 72 69 67 67 65 72 53 74 65 70  t of TriggerStep
2c7ff 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a   structures..*/.
2c800 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2c801 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74  oid sqlite3Delet
2c802 65 54 72 69 67 67 65 72 53 74 65 70 28 73 71 6c  eTriggerStep(sql
2c803 69 74 65 33 20 2a 64 62 2c 20 54 72 69 67 67 65  ite3 *db, Trigge
2c804 72 53 74 65 70 20 2a 70 54 72 69 67 67 65 72 53  rStep *pTriggerS
2c805 74 65 70 29 7b 0a 20 20 77 68 69 6c 65 28 20 70  tep){.  while( p
2c806 54 72 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20  TriggerStep ){. 
2c807 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a     TriggerStep *
2c808 20 70 54 6d 70 20 3d 20 70 54 72 69 67 67 65 72   pTmp = pTrigger
2c809 53 74 65 70 3b 0a 20 20 20 20 70 54 72 69 67 67  Step;.    pTrigg
2c80a 65 72 53 74 65 70 20 3d 20 70 54 72 69 67 67 65  erStep = pTrigge
2c80b 72 53 74 65 70 2d 3e 70 4e 65 78 74 3b 0a 0a 20  rStep->pNext;.. 
2c80c 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
2c80d 6c 65 74 65 28 64 62 2c 20 70 54 6d 70 2d 3e 70  lete(db, pTmp->p
2c80e 57 68 65 72 65 29 3b 0a 20 20 20 20 73 71 6c 69  Where);.    sqli
2c80f 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
2c810 65 28 64 62 2c 20 70 54 6d 70 2d 3e 70 45 78 70  e(db, pTmp->pExp
2c811 72 4c 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69  rList);.    sqli
2c812 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
2c813 64 62 2c 20 70 54 6d 70 2d 3e 70 53 65 6c 65 63  db, pTmp->pSelec
2c814 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49  t);.    sqlite3I
2c815 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  dListDelete(db, 
2c816 70 54 6d 70 2d 3e 70 49 64 4c 69 73 74 29 3b 0a  pTmp->pIdList);.
2c817 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2c818 65 65 28 64 62 2c 20 70 54 6d 70 29 3b 0a 20 20  ee(db, pTmp);.  
2c819 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e  }.}../*.** Given
2c81a 20 74 61 62 6c 65 20 70 54 61 62 2c 20 72 65 74   table pTab, ret
2c81b 75 72 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c  urn a list of al
2c81c 6c 20 74 68 65 20 74 72 69 67 67 65 72 73 20 61  l the triggers a
2c81d 74 74 61 63 68 65 64 20 74 6f 20 0a 2a 2a 20 74  ttached to .** t
2c81e 68 65 20 74 61 62 6c 65 2e 20 54 68 65 20 6c 69  he table. The li
2c81f 73 74 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 20  st is connected 
2c820 62 79 20 54 72 69 67 67 65 72 2e 70 4e 65 78 74  by Trigger.pNext
2c821 20 70 6f 69 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a   pointers..**.**
2c822 20 41 6c 6c 20 6f 66 20 74 68 65 20 74 72 69 67   All of the trig
2c823 67 65 72 73 20 6f 6e 20 70 54 61 62 20 74 68 61  gers on pTab tha
2c824 74 20 61 72 65 20 69 6e 20 74 68 65 20 73 61 6d  t are in the sam
2c825 65 20 64 61 74 61 62 61 73 65 20 61 73 20 70 54  e database as pT
2c826 61 62 0a 2a 2a 20 61 72 65 20 61 6c 72 65 61 64  ab.** are alread
2c827 79 20 61 74 74 61 63 68 65 64 20 74 6f 20 70 54  y attached to pT
2c828 61 62 2d 3e 70 54 72 69 67 67 65 72 2e 20 20 42  ab->pTrigger.  B
2c829 75 74 20 74 68 65 72 65 20 6d 69 67 68 74 20 62  ut there might b
2c82a 65 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20  e additional.** 
2c82b 74 72 69 67 67 65 72 73 20 6f 6e 20 70 54 61 62  triggers on pTab
2c82c 20 69 6e 20 74 68 65 20 54 45 4d 50 20 73 63 68   in the TEMP sch
2c82d 65 6d 61 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ema.  This routi
2c82e 6e 65 20 70 72 65 70 65 6e 64 73 20 61 6c 6c 0a  ne prepends all.
2c82f 2a 2a 20 54 45 4d 50 20 74 72 69 67 67 65 72 73  ** TEMP triggers
2c830 20 6f 6e 20 70 54 61 62 20 74 6f 20 74 68 65 20   on pTab to the 
2c831 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65  beginning of the
2c832 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 20   pTab->pTrigger 
2c833 6c 69 73 74 0a 2a 2a 20 61 6e 64 20 72 65 74 75  list.** and retu
2c834 72 6e 73 20 74 68 65 20 63 6f 6d 62 69 6e 65 64  rns the combined
2c835 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20   list..**.** To 
2c836 73 74 61 74 65 20 69 74 20 61 6e 6f 74 68 65 72  state it another
2c837 20 77 61 79 3a 20 20 54 68 69 73 20 72 6f 75 74   way:  This rout
2c838 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 6c 69  ine returns a li
2c839 73 74 20 6f 66 20 61 6c 6c 20 74 72 69 67 67 65  st of all trigge
2c83a 72 73 0a 2a 2a 20 74 68 61 74 20 66 69 72 65 20  rs.** that fire 
2c83b 6f 66 66 20 6f 66 20 70 54 61 62 2e 20 20 54 68  off of pTab.  Th
2c83c 65 20 6c 69 73 74 20 77 69 6c 6c 20 69 6e 63 6c  e list will incl
2c83d 75 64 65 20 61 6e 79 20 54 45 4d 50 20 74 72 69  ude any TEMP tri
2c83e 67 67 65 72 73 20 6f 6e 0a 2a 2a 20 70 54 61 62  ggers on.** pTab
2c83f 20 61 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20   as well as the 
2c840 74 72 69 67 67 65 72 73 20 6c 69 73 65 64 20 69  triggers lised i
2c841 6e 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72  n pTab->pTrigger
2c842 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2c843 41 54 45 20 54 72 69 67 67 65 72 20 2a 73 71 6c  ATE Trigger *sql
2c844 69 74 65 33 54 72 69 67 67 65 72 4c 69 73 74 28  ite3TriggerList(
2c845 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
2c846 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 53  able *pTab){.  S
2c847 63 68 65 6d 61 20 2a 20 63 6f 6e 73 74 20 70 54  chema * const pT
2c848 6d 70 53 63 68 65 6d 61 20 3d 20 70 50 61 72 73  mpSchema = pPars
2c849 65 2d 3e 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53  e->db->aDb[1].pS
2c84a 63 68 65 6d 61 3b 0a 20 20 54 72 69 67 67 65 72  chema;.  Trigger
2c84b 20 2a 70 4c 69 73 74 20 3d 20 30 3b 20 20 20 20   *pList = 0;    
2c84c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2c84d 20 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72   List of trigger
2c84e 73 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a  s to return */..
2c84f 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 69    if( pParse->di
2c850 73 61 62 6c 65 54 72 69 67 67 65 72 73 20 29 7b  sableTriggers ){
2c851 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
2c852 20 7d 0a 0a 20 20 69 66 28 20 70 54 6d 70 53 63   }..  if( pTmpSc
2c853 68 65 6d 61 21 3d 70 54 61 62 2d 3e 70 53 63 68  hema!=pTab->pSch
2c854 65 6d 61 20 29 7b 0a 20 20 20 20 48 61 73 68 45  ema ){.    HashE
2c855 6c 65 6d 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28  lem *p;.    for(
2c856 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  p=sqliteHashFirs
2c857 74 28 26 70 54 6d 70 53 63 68 65 6d 61 2d 3e 74  t(&pTmpSchema->t
2c858 72 69 67 48 61 73 68 29 3b 20 70 3b 20 70 3d 73  rigHash); p; p=s
2c859 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 29  qliteHashNext(p)
2c85a 29 7b 0a 20 20 20 20 20 20 54 72 69 67 67 65 72  ){.      Trigger
2c85b 20 2a 70 54 72 69 67 20 3d 20 28 54 72 69 67 67   *pTrig = (Trigg
2c85c 65 72 20 2a 29 73 71 6c 69 74 65 48 61 73 68 44  er *)sqliteHashD
2c85d 61 74 61 28 70 29 3b 0a 20 20 20 20 20 20 69 66  ata(p);.      if
2c85e 28 20 70 54 72 69 67 2d 3e 70 54 61 62 53 63 68  ( pTrig->pTabSch
2c85f 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65  ema==pTab->pSche
2c860 6d 61 0a 20 20 20 20 20 20 20 26 26 20 30 3d 3d  ma.       && 0==
2c861 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
2c862 54 72 69 67 2d 3e 74 61 62 6c 65 2c 20 70 54 61  Trig->table, pTa
2c863 62 2d 3e 7a 4e 61 6d 65 29 20 0a 20 20 20 20 20  b->zName) .     
2c864 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 72 69   ){.        pTri
2c865 67 2d 3e 70 4e 65 78 74 20 3d 20 28 70 4c 69 73  g->pNext = (pLis
2c866 74 20 3f 20 70 4c 69 73 74 20 3a 20 70 54 61 62  t ? pList : pTab
2c867 2d 3e 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20  ->pTrigger);.   
2c868 20 20 20 20 20 70 4c 69 73 74 20 3d 20 70 54 72       pList = pTr
2c869 69 67 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ig;.      }.    
2c86a 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
2c86b 28 70 4c 69 73 74 20 3f 20 70 4c 69 73 74 20 3a  (pList ? pList :
2c86c 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 29   pTab->pTrigger)
2c86d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
2c86e 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  is called by the
2c86f 20 70 61 72 73 65 72 20 77 68 65 6e 20 69 74 20   parser when it 
2c870 73 65 65 73 20 61 20 43 52 45 41 54 45 20 54 52  sees a CREATE TR
2c871 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74 0a  IGGER statement.
2c872 2a 2a 20 75 70 20 74 6f 20 74 68 65 20 70 6f 69  ** up to the poi
2c873 6e 74 20 6f 66 20 74 68 65 20 42 45 47 49 4e 20  nt of the BEGIN 
2c874 62 65 66 6f 72 65 20 74 68 65 20 74 72 69 67 67  before the trigg
2c875 65 72 20 61 63 74 69 6f 6e 73 2e 20 20 41 20 54  er actions.  A T
2c876 72 69 67 67 65 72 0a 2a 2a 20 73 74 72 75 63 74  rigger.** struct
2c877 75 72 65 20 69 73 20 67 65 6e 65 72 61 74 65 64  ure is generated
2c878 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 69 6e   based on the in
2c879 66 6f 72 6d 61 74 69 6f 6e 20 61 76 61 69 6c 61  formation availa
2c87a 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 64 0a 2a  ble and stored.*
2c87b 2a 20 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65  * in pParse->pNe
2c87c 77 54 72 69 67 67 65 72 2e 20 20 41 66 74 65 72  wTrigger.  After
2c87d 20 74 68 65 20 74 72 69 67 67 65 72 20 61 63 74   the trigger act
2c87e 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20 70  ions have been p
2c87f 61 72 73 65 64 2c 20 74 68 65 0a 2a 2a 20 73 71  arsed, the.** sq
2c880 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67 67  lite3FinishTrigg
2c881 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73  er() function is
2c882 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 6c   called to compl
2c883 65 74 65 20 74 68 65 20 74 72 69 67 67 65 72 0a  ete the trigger.
2c884 2a 2a 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20  ** construction 
2c885 70 72 6f 63 65 73 73 2e 0a 2a 2f 0a 53 51 4c 49  process..*/.SQLI
2c886 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2c887 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 69 67  sqlite3BeginTrig
2c888 67 65 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ger(.  Parse *pP
2c889 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 54 68  arse,      /* Th
2c88a 65 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20  e parse context 
2c88b 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54 52  of the CREATE TR
2c88c 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74 20  IGGER statement 
2c88d 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  */.  Token *pNam
2c88e 65 31 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  e1,      /* The 
2c88f 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69 67  name of the trig
2c890 67 65 72 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ger */.  Token *
2c891 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 2f 2a 20  pName2,      /* 
2c892 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
2c893 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 69 6e 74  trigger */.  int
2c894 20 74 72 5f 74 6d 2c 20 20 20 20 20 20 20 20 20   tr_tm,         
2c895 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 42 45   /* One of TK_BE
2c896 46 4f 52 45 2c 20 54 4b 5f 41 46 54 45 52 2c 20  FORE, TK_AFTER, 
2c897 54 4b 5f 49 4e 53 54 45 41 44 20 2a 2f 0a 20 20  TK_INSTEAD */.  
2c898 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20  int op,         
2c899 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b      /* One of TK
2c89a 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 55 50 44 41  _INSERT, TK_UPDA
2c89b 54 45 2c 20 54 4b 5f 44 45 4c 45 54 45 20 2a 2f  TE, TK_DELETE */
2c89c 0a 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c 75  .  IdList *pColu
2c89d 6d 6e 73 2c 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e  mns,   /* column
2c89e 20 6c 69 73 74 20 69 66 20 74 68 69 73 20 69 73   list if this is
2c89f 20 61 6e 20 55 50 44 41 54 45 20 4f 46 20 74 72   an UPDATE OF tr
2c8a0 69 67 67 65 72 20 2a 2f 0a 20 20 53 72 63 4c 69  igger */.  SrcLi
2c8a1 73 74 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 2f  st *pTableName,/
2c8a2 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  * The name of th
2c8a3 65 20 74 61 62 6c 65 2f 76 69 65 77 20 74 68 65  e table/view the
2c8a4 20 74 72 69 67 67 65 72 20 61 70 70 6c 69 65 73   trigger applies
2c8a5 20 74 6f 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70   to */.  Expr *p
2c8a6 57 68 65 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20  When,        /* 
2c8a7 57 48 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a 20  WHEN clause */. 
2c8a8 20 69 6e 74 20 69 73 54 65 6d 70 2c 20 20 20 20   int isTemp,    
2c8a9 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
2c8aa 74 68 65 20 54 45 4d 50 4f 52 41 52 59 20 6b 65  the TEMPORARY ke
2c8ab 79 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74  yword is present
2c8ac 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20   */.  int noErr 
2c8ad 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 70            /* Sup
2c8ae 70 72 65 73 73 20 65 72 72 6f 72 73 20 69 66 20  press errors if 
2c8af 74 68 65 20 74 72 69 67 67 65 72 20 61 6c 72 65  the trigger alre
2c8b0 61 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b  ady exists */.){
2c8b1 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
2c8b2 67 67 65 72 20 3d 20 30 3b 20 20 2f 2a 20 54 68  gger = 0;  /* Th
2c8b3 65 20 6e 65 77 20 74 72 69 67 67 65 72 20 2a 2f  e new trigger */
2c8b4 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20  .  Table *pTab; 
2c8b5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
2c8b6 62 6c 65 20 74 68 61 74 20 74 68 65 20 74 72 69  ble that the tri
2c8b7 67 67 65 72 20 66 69 72 65 73 20 6f 66 66 20 6f  gger fires off o
2c8b8 66 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61  f */.  char *zNa
2c8b9 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  me = 0;        /
2c8ba 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 72  * Name of the tr
2c8bb 69 67 67 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74  igger */.  sqlit
2c8bc 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2c8bd 3e 64 62 3b 20 20 2f 2a 20 54 68 65 20 64 61 74  >db;  /* The dat
2c8be 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2c8bf 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
2c8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2c8c1 20 54 68 65 20 64 61 74 61 62 61 73 65 20 74 6f   The database to
2c8c2 20 73 74 6f 72 65 20 74 68 65 20 74 72 69 67 67   store the trigg
2c8c3 65 72 20 69 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e  er in */.  Token
2c8c4 20 2a 70 4e 61 6d 65 3b 20 20 20 20 20 20 20 20   *pName;        
2c8c5 20 20 20 2f 2a 20 54 68 65 20 75 6e 71 75 61 6c     /* The unqual
2c8c6 69 66 69 65 64 20 64 62 20 6e 61 6d 65 20 2a 2f  ified db name */
2c8c7 0a 20 20 44 62 46 69 78 65 72 20 73 46 69 78 3b  .  DbFixer sFix;
2c8c8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
2c8c9 61 74 65 20 76 65 63 74 6f 72 20 66 6f 72 20 74  ate vector for t
2c8ca 68 65 20 44 42 20 66 69 78 65 72 20 2a 2f 0a 20  he DB fixer */. 
2c8cb 20 69 6e 74 20 69 54 61 62 44 62 3b 20 20 20 20   int iTabDb;    
2c8cc 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
2c8cd 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  x of the databas
2c8ce 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a  e holding pTab *
2c8cf 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e 61  /..  assert( pNa
2c8d0 6d 65 31 21 3d 30 20 29 3b 20 20 20 2f 2a 20 70  me1!=0 );   /* p
2c8d1 4e 61 6d 65 31 2d 3e 7a 20 6d 69 67 68 74 20 62  Name1->z might b
2c8d2 65 20 4e 55 4c 4c 2c 20 62 75 74 20 6e 6f 74 20  e NULL, but not 
2c8d3 70 4e 61 6d 65 31 20 69 74 73 65 6c 66 20 2a 2f  pName1 itself */
2c8d4 0a 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65  .  assert( pName
2c8d5 32 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  2!=0 );.  assert
2c8d6 28 20 6f 70 3d 3d 54 4b 5f 49 4e 53 45 52 54 20  ( op==TK_INSERT 
2c8d7 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 50 44 41 54 45  || op==TK_UPDATE
2c8d8 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 44 45 4c 45 54   || op==TK_DELET
2c8d9 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  E );.  assert( o
2c8da 70 3e 30 20 26 26 20 6f 70 3c 30 78 66 66 20 29  p>0 && op<0xff )
2c8db 3b 0a 20 20 69 66 28 20 69 73 54 65 6d 70 20 29  ;.  if( isTemp )
2c8dc 7b 0a 20 20 20 20 2f 2a 20 49 66 20 54 45 4d 50  {.    /* If TEMP
2c8dd 20 77 61 73 20 73 70 65 63 69 66 69 65 64 2c 20   was specified, 
2c8de 74 68 65 6e 20 74 68 65 20 74 72 69 67 67 65 72  then the trigger
2c8df 20 6e 61 6d 65 20 6d 61 79 20 6e 6f 74 20 62 65   name may not be
2c8e0 20 71 75 61 6c 69 66 69 65 64 2e 20 2a 2f 0a 20   qualified. */. 
2c8e1 20 20 20 69 66 28 20 70 4e 61 6d 65 32 2d 3e 6e     if( pName2->n
2c8e2 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  >0 ){.      sqli
2c8e3 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2c8e4 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79 20 74  se, "temporary t
2c8e5 72 69 67 67 65 72 20 6d 61 79 20 6e 6f 74 20 68  rigger may not h
2c8e6 61 76 65 20 71 75 61 6c 69 66 69 65 64 20 6e 61  ave qualified na
2c8e7 6d 65 22 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  me");.      goto
2c8e8 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70   trigger_cleanup
2c8e9 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 44 62 20  ;.    }.    iDb 
2c8ea 3d 20 31 3b 0a 20 20 20 20 70 4e 61 6d 65 20 3d  = 1;.    pName =
2c8eb 20 70 4e 61 6d 65 31 3b 0a 20 20 7d 65 6c 73 65   pName1;.  }else
2c8ec 7b 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20  {.    /* Figure 
2c8ed 6f 75 74 20 74 68 65 20 64 62 20 74 68 61 74 20  out the db that 
2c8ee 74 68 65 20 74 68 65 20 74 72 69 67 67 65 72 20  the the trigger 
2c8ef 77 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64 20  will be created 
2c8f0 69 6e 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20  in */.    iDb = 
2c8f1 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61  sqlite3TwoPartNa
2c8f2 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65  me(pParse, pName
2c8f3 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d  1, pName2, &pNam
2c8f4 65 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3c  e);.    if( iDb<
2c8f5 30 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  0 ){.      goto 
2c8f6 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
2c8f7 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
2c8f8 20 49 66 20 74 68 65 20 74 72 69 67 67 65 72 20   If the trigger 
2c8f9 6e 61 6d 65 20 77 61 73 20 75 6e 71 75 61 6c 69  name was unquali
2c8fa 66 69 65 64 2c 20 61 6e 64 20 74 68 65 20 74 61  fied, and the ta
2c8fb 62 6c 65 20 69 73 20 61 20 74 65 6d 70 20 74 61  ble is a temp ta
2c8fc 62 6c 65 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 73  ble,.  ** then s
2c8fd 65 74 20 69 44 62 20 74 6f 20 31 20 74 6f 20 63  et iDb to 1 to c
2c8fe 72 65 61 74 65 20 74 68 65 20 74 72 69 67 67 65  reate the trigge
2c8ff 72 20 69 6e 20 74 68 65 20 74 65 6d 70 6f 72 61  r in the tempora
2c900 72 79 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a  ry database..  *
2c901 2a 20 49 66 20 73 71 6c 69 74 65 33 53 72 63 4c  * If sqlite3SrcL
2c902 69 73 74 4c 6f 6f 6b 75 70 28 29 20 72 65 74 75  istLookup() retu
2c903 72 6e 73 20 30 2c 20 69 6e 64 69 63 61 74 69 6e  rns 0, indicatin
2c904 67 20 74 68 65 20 74 61 62 6c 65 20 64 6f 65 73  g the table does
2c905 20 6e 6f 74 0a 20 20 2a 2a 20 65 78 69 73 74 2c   not.  ** exist,
2c906 20 74 68 65 20 65 72 72 6f 72 20 69 73 20 63 61   the error is ca
2c907 75 67 68 74 20 62 79 20 74 68 65 20 62 6c 6f 63  ught by the bloc
2c908 6b 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20 20  k below..  */.  
2c909 69 66 28 20 21 70 54 61 62 6c 65 4e 61 6d 65 20  if( !pTableName 
2c90a 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
2c90b 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  led ){.    goto 
2c90c 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
2c90d 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d 20 73 71  .  }.  pTab = sq
2c90e 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b  lite3SrcListLook
2c90f 75 70 28 70 50 61 72 73 65 2c 20 70 54 61 62 6c  up(pParse, pTabl
2c910 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 4e  eName);.  if( pN
2c911 61 6d 65 32 2d 3e 6e 3d 3d 30 20 26 26 20 70 54  ame2->n==0 && pT
2c912 61 62 20 26 26 20 70 54 61 62 2d 3e 70 53 63 68  ab && pTab->pSch
2c913 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e  ema==db->aDb[1].
2c914 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 69  pSchema ){.    i
2c915 44 62 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f  Db = 1;.  }..  /
2c916 2a 20 45 6e 73 75 72 65 20 74 68 65 20 74 61 62  * Ensure the tab
2c917 6c 65 20 6e 61 6d 65 20 6d 61 74 63 68 65 73 20  le name matches 
2c918 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 61 6e  database name an
2c919 64 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65  d that the table
2c91a 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 69 66 28   exists */.  if(
2c91b 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2c91c 64 20 29 20 67 6f 74 6f 20 74 72 69 67 67 65 72  d ) goto trigger
2c91d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 61 73 73 65  _cleanup;.  asse
2c91e 72 74 28 20 70 54 61 62 6c 65 4e 61 6d 65 2d 3e  rt( pTableName->
2c91f 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 69 66 28  nSrc==1 );.  if(
2c920 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28   sqlite3FixInit(
2c921 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c 20 69  &sFix, pParse, i
2c922 44 62 2c 20 22 74 72 69 67 67 65 72 22 2c 20 70  Db, "trigger", p
2c923 4e 61 6d 65 29 20 26 26 20 0a 20 20 20 20 20 20  Name) && .      
2c924 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73  sqlite3FixSrcLis
2c925 74 28 26 73 46 69 78 2c 20 70 54 61 62 6c 65 4e  t(&sFix, pTableN
2c926 61 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  ame) ){.    goto
2c927 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70   trigger_cleanup
2c928 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d 20 73  ;.  }.  pTab = s
2c929 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f  qlite3SrcListLoo
2c92a 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54 61 62  kup(pParse, pTab
2c92b 6c 65 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21  leName);.  if( !
2c92c 70 54 61 62 20 29 7b 0a 20 20 20 20 2f 2a 20 54  pTab ){.    /* T
2c92d 68 65 20 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f  he table does no
2c92e 74 20 65 78 69 73 74 2e 20 2a 2f 0a 20 20 20 20  t exist. */.    
2c92f 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62  if( db->init.iDb
2c930 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ==1 ){.      /* 
2c931 54 69 63 6b 65 74 20 23 33 38 31 30 2e 0a 20 20  Ticket #3810..  
2c932 20 20 20 20 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c      ** Normally,
2c933 20 77 68 65 6e 65 76 65 72 20 61 20 74 61 62 6c   whenever a tabl
2c934 65 20 69 73 20 64 72 6f 70 70 65 64 2c 20 61 6c  e is dropped, al
2c935 6c 20 61 73 73 6f 63 69 61 74 65 64 20 74 72 69  l associated tri
2c936 67 67 65 72 73 20 61 72 65 0a 20 20 20 20 20 20  ggers are.      
2c937 2a 2a 20 64 72 6f 70 70 65 64 20 74 6f 6f 2e 20  ** dropped too. 
2c938 20 42 75 74 20 69 66 20 61 20 54 45 4d 50 20 74   But if a TEMP t
2c939 72 69 67 67 65 72 20 69 73 20 63 72 65 61 74 65  rigger is create
2c93a 64 20 6f 6e 20 61 20 6e 6f 6e 2d 54 45 4d 50 20  d on a non-TEMP 
2c93b 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61  table.      ** a
2c93c 6e 64 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  nd the table is 
2c93d 64 72 6f 70 70 65 64 20 62 79 20 61 20 64 69 66  dropped by a dif
2c93e 66 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 20  ferent database 
2c93f 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65 0a  connection, the.
2c940 20 20 20 20 20 20 2a 2a 20 74 72 69 67 67 65 72        ** trigger
2c941 20 69 73 20 6e 6f 74 20 76 69 73 69 62 6c 65 20   is not visible 
2c942 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
2c943 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20  connection that 
2c944 64 6f 65 73 20 74 68 65 0a 20 20 20 20 20 20 2a  does the.      *
2c945 2a 20 64 72 6f 70 20 73 6f 20 74 68 65 20 74 72  * drop so the tr
2c946 69 67 67 65 72 20 63 61 6e 6e 6f 74 20 62 65 20  igger cannot be 
2c947 64 72 6f 70 70 65 64 2e 20 20 54 68 69 73 20 72  dropped.  This r
2c948 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a 20 20 20  esults in an.   
2c949 20 20 20 2a 2a 20 22 6f 72 70 68 61 6e 65 64 20     ** "orphaned 
2c94a 74 72 69 67 67 65 72 22 20 2d 20 61 20 74 72 69  trigger" - a tri
2c94b 67 67 65 72 20 77 68 6f 73 65 20 61 73 73 6f 63  gger whose assoc
2c94c 69 61 74 65 64 20 74 61 62 6c 65 20 69 73 20 6d  iated table is m
2c94d 69 73 73 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f  issing..      */
2c94e 0a 20 20 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e  .      db->init.
2c94f 6f 72 70 68 61 6e 54 72 69 67 67 65 72 20 3d 20  orphanTrigger = 
2c950 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74  1;.    }.    got
2c951 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
2c952 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 49 73 56  p;.  }.  if( IsV
2c953 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
2c954 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2c955 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e  Msg(pParse, "can
2c956 6e 6f 74 20 63 72 65 61 74 65 20 74 72 69 67 67  not create trigg
2c957 65 72 73 20 6f 6e 20 76 69 72 74 75 61 6c 20 74  ers on virtual t
2c958 61 62 6c 65 73 22 29 3b 0a 20 20 20 20 67 6f 74  ables");.    got
2c959 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
2c95a 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65  p;.  }..  /* Che
2c95b 63 6b 20 74 68 61 74 20 74 68 65 20 74 72 69 67  ck that the trig
2c95c 67 65 72 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20  ger name is not 
2c95d 72 65 73 65 72 76 65 64 20 61 6e 64 20 74 68 61  reserved and tha
2c95e 74 20 6e 6f 20 74 72 69 67 67 65 72 20 6f 66 20  t no trigger of 
2c95f 74 68 65 0a 20 20 2a 2a 20 73 70 65 63 69 66 69  the.  ** specifi
2c960 65 64 20 6e 61 6d 65 20 65 78 69 73 74 73 20 2a  ed name exists *
2c961 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69  /.  zName = sqli
2c962 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
2c963 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69  (db, pName);.  i
2c964 66 28 20 21 7a 4e 61 6d 65 20 7c 7c 20 53 51 4c  f( !zName || SQL
2c965 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43  ITE_OK!=sqlite3C
2c966 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70  heckObjectName(p
2c967 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b  Parse, zName) ){
2c968 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65  .    goto trigge
2c969 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  r_cleanup;.  }. 
2c96a 20 69 66 28 20 73 71 6c 69 74 65 33 48 61 73 68   if( sqlite3Hash
2c96b 46 69 6e 64 28 26 28 64 62 2d 3e 61 44 62 5b 69  Find(&(db->aDb[i
2c96c 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69  Db].pSchema->tri
2c96d 67 48 61 73 68 29 2c 0a 20 20 20 20 20 20 20 20  gHash),.        
2c96e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 4e                zN
2c96f 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c  ame, sqlite3Strl
2c970 65 6e 33 30 28 7a 4e 61 6d 65 29 29 20 29 7b 0a  en30(zName)) ){.
2c971 20 20 20 20 69 66 28 20 21 6e 6f 45 72 72 20 29      if( !noErr )
2c972 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
2c973 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2c974 22 74 72 69 67 67 65 72 20 25 54 20 61 6c 72 65  "trigger %T alre
2c975 61 64 79 20 65 78 69 73 74 73 22 2c 20 70 4e 61  ady exists", pNa
2c976 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67  me);.    }.    g
2c977 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61  oto trigger_clea
2c978 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44  nup;.  }..  /* D
2c979 6f 20 6e 6f 74 20 63 72 65 61 74 65 20 61 20 74  o not create a t
2c97a 72 69 67 67 65 72 20 6f 6e 20 61 20 73 79 73 74  rigger on a syst
2c97b 65 6d 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66  em table */.  if
2c97c 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  ( sqlite3StrNICm
2c97d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22  p(pTab->zName, "
2c97e 73 71 6c 69 74 65 5f 22 2c 20 37 29 3d 3d 30 20  sqlite_", 7)==0 
2c97f 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2c980 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2c981 63 61 6e 6e 6f 74 20 63 72 65 61 74 65 20 74 72  cannot create tr
2c982 69 67 67 65 72 20 6f 6e 20 73 79 73 74 65 6d 20  igger on system 
2c983 74 61 62 6c 65 22 29 3b 0a 20 20 20 20 70 50 61  table");.    pPa
2c984 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20  rse->nErr++;.   
2c985 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c   goto trigger_cl
2c986 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  eanup;.  }..  /*
2c987 20 49 4e 53 54 45 41 44 20 6f 66 20 74 72 69 67   INSTEAD of trig
2c988 67 65 72 73 20 61 72 65 20 6f 6e 6c 79 20 66 6f  gers are only fo
2c989 72 20 76 69 65 77 73 20 61 6e 64 20 76 69 65 77  r views and view
2c98a 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 20 49  s only support I
2c98b 4e 53 54 45 41 44 0a 20 20 2a 2a 20 6f 66 20 74  NSTEAD.  ** of t
2c98c 72 69 67 67 65 72 73 2e 0a 20 20 2a 2f 0a 20 20  riggers..  */.  
2c98d 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63  if( pTab->pSelec
2c98e 74 20 26 26 20 74 72 5f 74 6d 21 3d 54 4b 5f 49  t && tr_tm!=TK_I
2c98f 4e 53 54 45 41 44 20 29 7b 0a 20 20 20 20 73 71  NSTEAD ){.    sq
2c990 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2c991 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72  arse, "cannot cr
2c992 65 61 74 65 20 25 73 20 74 72 69 67 67 65 72 20  eate %s trigger 
2c993 6f 6e 20 76 69 65 77 3a 20 25 53 22 2c 20 0a 20  on view: %S", . 
2c994 20 20 20 20 20 20 20 28 74 72 5f 74 6d 20 3d 3d         (tr_tm ==
2c995 20 54 4b 5f 42 45 46 4f 52 45 29 3f 22 42 45 46   TK_BEFORE)?"BEF
2c996 4f 52 45 22 3a 22 41 46 54 45 52 22 2c 20 70 54  ORE":"AFTER", pT
2c997 61 62 6c 65 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  ableName, 0);.  
2c998 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63    goto trigger_c
2c999 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66  leanup;.  }.  if
2c99a 28 20 21 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  ( !pTab->pSelect
2c99b 20 26 26 20 74 72 5f 74 6d 3d 3d 54 4b 5f 49 4e   && tr_tm==TK_IN
2c99c 53 54 45 41 44 20 29 7b 0a 20 20 20 20 73 71 6c  STEAD ){.    sql
2c99d 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2c99e 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65  rse, "cannot cre
2c99f 61 74 65 20 49 4e 53 54 45 41 44 20 4f 46 22 0a  ate INSTEAD OF".
2c9a0 20 20 20 20 20 20 20 20 22 20 74 72 69 67 67 65          " trigge
2c9a1 72 20 6f 6e 20 74 61 62 6c 65 3a 20 25 53 22 2c  r on table: %S",
2c9a2 20 70 54 61 62 6c 65 4e 61 6d 65 2c 20 30 29 3b   pTableName, 0);
2c9a3 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65  .    goto trigge
2c9a4 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  r_cleanup;.  }. 
2c9a5 20 69 54 61 62 44 62 20 3d 20 73 71 6c 69 74 65   iTabDb = sqlite
2c9a6 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
2c9a7 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
2c9a8 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
2c9a9 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
2c9aa 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e  ATION.  {.    in
2c9ab 74 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f  t code = SQLITE_
2c9ac 43 52 45 41 54 45 5f 54 52 49 47 47 45 52 3b 0a  CREATE_TRIGGER;.
2c9ad 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2c9ae 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 54  zDb = db->aDb[iT
2c9af 61 62 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  abDb].zName;.   
2c9b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
2c9b1 54 72 69 67 20 3d 20 69 73 54 65 6d 70 20 3f 20  Trig = isTemp ? 
2c9b2 64 62 2d 3e 61 44 62 5b 31 5d 2e 7a 4e 61 6d 65  db->aDb[1].zName
2c9b3 20 3a 20 7a 44 62 3b 0a 20 20 20 20 69 66 28 20   : zDb;.    if( 
2c9b4 69 54 61 62 44 62 3d 3d 31 20 7c 7c 20 69 73 54  iTabDb==1 || isT
2c9b5 65 6d 70 20 29 20 63 6f 64 65 20 3d 20 53 51 4c  emp ) code = SQL
2c9b6 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f  ITE_CREATE_TEMP_
2c9b7 54 52 49 47 47 45 52 3b 0a 20 20 20 20 69 66 28  TRIGGER;.    if(
2c9b8 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
2c9b9 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20  k(pParse, code, 
2c9ba 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61  zName, pTab->zNa
2c9bb 6d 65 2c 20 7a 44 62 54 72 69 67 29 20 29 7b 0a  me, zDbTrig) ){.
2c9bc 20 20 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67        goto trigg
2c9bd 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  er_cleanup;.    
2c9be 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  }.    if( sqlite
2c9bf 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
2c9c0 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54  e, SQLITE_INSERT
2c9c1 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
2c9c2 54 61 62 44 62 29 2c 30 2c 7a 44 62 29 29 7b 0a  TabDb),0,zDb)){.
2c9c3 20 20 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67        goto trigg
2c9c4 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  er_cleanup;.    
2c9c5 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  }.  }.#endif..  
2c9c6 2f 2a 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72  /* INSTEAD OF tr
2c9c7 69 67 67 65 72 73 20 63 61 6e 20 6f 6e 6c 79 20  iggers can only 
2c9c8 61 70 70 65 61 72 20 6f 6e 20 76 69 65 77 73 20  appear on views 
2c9c9 61 6e 64 20 42 45 46 4f 52 45 20 74 72 69 67 67  and BEFORE trigg
2c9ca 65 72 73 0a 20 20 2a 2a 20 63 61 6e 6e 6f 74 20  ers.  ** cannot 
2c9cb 61 70 70 65 61 72 20 6f 6e 20 76 69 65 77 73 2e  appear on views.
2c9cc 20 20 53 6f 20 77 65 20 6d 69 67 68 74 20 61 73    So we might as
2c9cd 20 77 65 6c 6c 20 74 72 61 6e 73 6c 61 74 65 20   well translate 
2c9ce 65 76 65 72 79 0a 20 20 2a 2a 20 49 4e 53 54 45  every.  ** INSTE
2c9cf 41 44 20 4f 46 20 74 72 69 67 67 65 72 20 69 6e  AD OF trigger in
2c9d0 74 6f 20 61 20 42 45 46 4f 52 45 20 74 72 69 67  to a BEFORE trig
2c9d1 67 65 72 2e 20 20 49 74 20 73 69 6d 70 6c 69 66  ger.  It simplif
2c9d2 69 65 73 20 63 6f 64 65 0a 20 20 2a 2a 20 65 6c  ies code.  ** el
2c9d3 73 65 77 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20  sewhere..  */.  
2c9d4 69 66 20 28 74 72 5f 74 6d 20 3d 3d 20 54 4b 5f  if (tr_tm == TK_
2c9d5 49 4e 53 54 45 41 44 29 7b 0a 20 20 20 20 74 72  INSTEAD){.    tr
2c9d6 5f 74 6d 20 3d 20 54 4b 5f 42 45 46 4f 52 45 3b  _tm = TK_BEFORE;
2c9d7 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 75 69 6c 64  .  }..  /* Build
2c9d8 20 74 68 65 20 54 72 69 67 67 65 72 20 6f 62 6a   the Trigger obj
2c9d9 65 63 74 20 2a 2f 0a 20 20 70 54 72 69 67 67 65  ect */.  pTrigge
2c9da 72 20 3d 20 28 54 72 69 67 67 65 72 2a 29 73 71  r = (Trigger*)sq
2c9db 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
2c9dc 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 72 69  o(db, sizeof(Tri
2c9dd 67 67 65 72 29 29 3b 0a 20 20 69 66 28 20 70 54  gger));.  if( pT
2c9de 72 69 67 67 65 72 3d 3d 30 20 29 20 67 6f 74 6f  rigger==0 ) goto
2c9df 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70   trigger_cleanup
2c9e0 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e  ;.  pTrigger->zN
2c9e1 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 7a  ame = zName;.  z
2c9e2 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 70 54 72 69  Name = 0;.  pTri
2c9e3 67 67 65 72 2d 3e 74 61 62 6c 65 20 3d 20 73 71  gger->table = sq
2c9e4 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
2c9e5 2c 20 70 54 61 62 6c 65 4e 61 6d 65 2d 3e 61 5b  , pTableName->a[
2c9e6 30 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 70 54 72  0].zName);.  pTr
2c9e7 69 67 67 65 72 2d 3e 70 53 63 68 65 6d 61 20 3d  igger->pSchema =
2c9e8 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53   db->aDb[iDb].pS
2c9e9 63 68 65 6d 61 3b 0a 20 20 70 54 72 69 67 67 65  chema;.  pTrigge
2c9ea 72 2d 3e 70 54 61 62 53 63 68 65 6d 61 20 3d 20  r->pTabSchema = 
2c9eb 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20  pTab->pSchema;. 
2c9ec 20 70 54 72 69 67 67 65 72 2d 3e 6f 70 20 3d 20   pTrigger->op = 
2c9ed 28 75 38 29 6f 70 3b 0a 20 20 70 54 72 69 67 67  (u8)op;.  pTrigg
2c9ee 65 72 2d 3e 74 72 5f 74 6d 20 3d 20 74 72 5f 74  er->tr_tm = tr_t
2c9ef 6d 3d 3d 54 4b 5f 42 45 46 4f 52 45 20 3f 20 54  m==TK_BEFORE ? T
2c9f0 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 3a 20  RIGGER_BEFORE : 
2c9f1 54 52 49 47 47 45 52 5f 41 46 54 45 52 3b 0a 20  TRIGGER_AFTER;. 
2c9f2 20 70 54 72 69 67 67 65 72 2d 3e 70 57 68 65 6e   pTrigger->pWhen
2c9f3 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
2c9f4 70 28 64 62 2c 20 70 57 68 65 6e 2c 20 45 58 50  p(db, pWhen, EXP
2c9f5 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20  RDUP_REDUCE);.  
2c9f6 70 54 72 69 67 67 65 72 2d 3e 70 43 6f 6c 75 6d  pTrigger->pColum
2c9f7 6e 73 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69  ns = sqlite3IdLi
2c9f8 73 74 44 75 70 28 64 62 2c 20 70 43 6f 6c 75 6d  stDup(db, pColum
2c9f9 6e 73 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ns);.  assert( p
2c9fa 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
2c9fb 65 72 3d 3d 30 20 29 3b 0a 20 20 70 50 61 72 73  er==0 );.  pPars
2c9fc 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 20 3d  e->pNewTrigger =
2c9fd 20 70 54 72 69 67 67 65 72 3b 0a 0a 74 72 69 67   pTrigger;..trig
2c9fe 67 65 72 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73  ger_cleanup:.  s
2c9ff 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2ca00 20 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74   zName);.  sqlit
2ca01 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28  e3SrcListDelete(
2ca02 64 62 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b  db, pTableName);
2ca03 0a 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74  .  sqlite3IdList
2ca04 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c 75  Delete(db, pColu
2ca05 6d 6e 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  mns);.  sqlite3E
2ca06 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 57  xprDelete(db, pW
2ca07 68 65 6e 29 3b 0a 20 20 69 66 28 20 21 70 50 61  hen);.  if( !pPa
2ca08 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72  rse->pNewTrigger
2ca09 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
2ca0a 65 6c 65 74 65 54 72 69 67 67 65 72 28 64 62 2c  eleteTrigger(db,
2ca0b 20 70 54 72 69 67 67 65 72 29 3b 0a 20 20 7d 65   pTrigger);.  }e
2ca0c 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
2ca0d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69   pParse->pNewTri
2ca0e 67 67 65 72 3d 3d 70 54 72 69 67 67 65 72 20 29  gger==pTrigger )
2ca0f 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
2ca10 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
2ca11 61 6c 6c 65 64 20 61 66 74 65 72 20 61 6c 6c 20  alled after all 
2ca12 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 20 61  of the trigger a
2ca13 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e  ctions have been
2ca14 20 70 61 72 73 65 64 0a 2a 2a 20 69 6e 20 6f 72   parsed.** in or
2ca15 64 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20  der to complete 
2ca16 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20 62  the process of b
2ca17 75 69 6c 64 69 6e 67 20 74 68 65 20 74 72 69 67  uilding the trig
2ca18 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ger..*/.SQLITE_P
2ca19 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2ca1a 74 65 33 46 69 6e 69 73 68 54 72 69 67 67 65 72  te3FinishTrigger
2ca1b 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2ca1c 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  e,          /* P
2ca1d 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
2ca1e 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  .  TriggerStep *
2ca1f 70 53 74 65 70 4c 69 73 74 2c 20 2f 2a 20 54 68  pStepList, /* Th
2ca20 65 20 74 72 69 67 67 65 72 65 64 20 70 72 6f 67  e triggered prog
2ca21 72 61 6d 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ram */.  Token *
2ca22 70 41 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20  pAll            
2ca23 20 2f 2a 20 54 6f 6b 65 6e 20 74 68 61 74 20 64   /* Token that d
2ca24 65 73 63 72 69 62 65 73 20 74 68 65 20 63 6f 6d  escribes the com
2ca25 70 6c 65 74 65 20 43 52 45 41 54 45 20 54 52 49  plete CREATE TRI
2ca26 47 47 45 52 20 2a 2f 0a 29 7b 0a 20 20 54 72 69  GGER */.){.  Tri
2ca27 67 67 65 72 20 2a 70 54 72 69 67 20 3d 20 70 50  gger *pTrig = pP
2ca28 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65  arse->pNewTrigge
2ca29 72 3b 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72  r;    /* Trigger
2ca2a 20 62 65 69 6e 67 20 66 69 6e 69 73 68 65 64 20   being finished 
2ca2b 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  */.  char *zName
2ca2c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2ca2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2ca2e 20 4e 61 6d 65 20 6f 66 20 74 72 69 67 67 65 72   Name of trigger
2ca2f 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
2ca30 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
2ca31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2ca32 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 2a  * The database *
2ca33 2f 0a 20 20 44 62 46 69 78 65 72 20 73 46 69 78  /.  DbFixer sFix
2ca34 3b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  ;.  int iDb;    
2ca35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ca36 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2ca37 44 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  Database contain
2ca38 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 72 20  ing the trigger 
2ca39 2a 2f 0a 20 20 54 6f 6b 65 6e 20 6e 61 6d 65 54  */.  Token nameT
2ca3a 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20  oken;           
2ca3b 2f 2a 20 54 72 69 67 67 65 72 20 6e 61 6d 65 20  /* Trigger name 
2ca3c 66 6f 72 20 65 72 72 6f 72 20 72 65 70 6f 72 74  for error report
2ca3d 69 6e 67 20 2a 2f 0a 0a 20 20 70 54 72 69 67 20  ing */..  pTrig 
2ca3e 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72  = pParse->pNewTr
2ca3f 69 67 67 65 72 3b 0a 20 20 70 50 61 72 73 65 2d  igger;.  pParse-
2ca40 3e 70 4e 65 77 54 72 69 67 67 65 72 20 3d 20 30  >pNewTrigger = 0
2ca41 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 50  ;.  if( NEVER(pP
2ca42 61 72 73 65 2d 3e 6e 45 72 72 29 20 7c 7c 20 21  arse->nErr) || !
2ca43 70 54 72 69 67 20 29 20 67 6f 74 6f 20 74 72 69  pTrig ) goto tri
2ca44 67 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e  ggerfinish_clean
2ca45 75 70 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 70 54  up;.  zName = pT
2ca46 72 69 67 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 69 44  rig->zName;.  iD
2ca47 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
2ca48 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
2ca49 3e 64 62 2c 20 70 54 72 69 67 2d 3e 70 53 63 68  >db, pTrig->pSch
2ca4a 65 6d 61 29 3b 0a 20 20 70 54 72 69 67 2d 3e 73  ema);.  pTrig->s
2ca4b 74 65 70 5f 6c 69 73 74 20 3d 20 70 53 74 65 70  tep_list = pStep
2ca4c 4c 69 73 74 3b 0a 20 20 77 68 69 6c 65 28 20 70  List;.  while( p
2ca4d 53 74 65 70 4c 69 73 74 20 29 7b 0a 20 20 20 20  StepList ){.    
2ca4e 70 53 74 65 70 4c 69 73 74 2d 3e 70 54 72 69 67  pStepList->pTrig
2ca4f 20 3d 20 70 54 72 69 67 3b 0a 20 20 20 20 70 53   = pTrig;.    pS
2ca50 74 65 70 4c 69 73 74 20 3d 20 70 53 74 65 70 4c  tepList = pStepL
2ca51 69 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a  ist->pNext;.  }.
2ca52 20 20 6e 61 6d 65 54 6f 6b 65 6e 2e 7a 20 3d 20    nameToken.z = 
2ca53 70 54 72 69 67 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  pTrig->zName;.  
2ca54 6e 61 6d 65 54 6f 6b 65 6e 2e 6e 20 3d 20 73 71  nameToken.n = sq
2ca55 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 6e 61  lite3Strlen30(na
2ca56 6d 65 54 6f 6b 65 6e 2e 7a 29 3b 0a 20 20 69 66  meToken.z);.  if
2ca57 28 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74  ( sqlite3FixInit
2ca58 28 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c 20  (&sFix, pParse, 
2ca59 69 44 62 2c 20 22 74 72 69 67 67 65 72 22 2c 20  iDb, "trigger", 
2ca5a 26 6e 61 6d 65 54 6f 6b 65 6e 29 20 0a 20 20 20  &nameToken) .   
2ca5b 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65         && sqlite
2ca5c 33 46 69 78 54 72 69 67 67 65 72 53 74 65 70 28  3FixTriggerStep(
2ca5d 26 73 46 69 78 2c 20 70 54 72 69 67 2d 3e 73 74  &sFix, pTrig->st
2ca5e 65 70 5f 6c 69 73 74 29 20 29 7b 0a 20 20 20 20  ep_list) ){.    
2ca5f 67 6f 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69  goto triggerfini
2ca60 73 68 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  sh_cleanup;.  }.
2ca61 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 72 65 20  .  /* if we are 
2ca62 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67  not initializing
2ca63 2c 20 61 6e 64 20 74 68 69 73 20 74 72 69 67 67  , and this trigg
2ca64 65 72 20 69 73 20 6e 6f 74 20 6f 6e 20 61 20 54  er is not on a T
2ca65 45 4d 50 20 74 61 62 6c 65 2c 20 0a 20 20 2a 2a  EMP table, .  **
2ca66 20 62 75 69 6c 64 20 74 68 65 20 73 71 6c 69 74   build the sqlit
2ca67 65 5f 6d 61 73 74 65 72 20 65 6e 74 72 79 0a 20  e_master entry. 
2ca68 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e 69   */.  if( !db->i
2ca69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20  nit.busy ){.    
2ca6a 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 63 68 61  Vdbe *v;.    cha
2ca6b 72 20 2a 7a 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61  r *z;..    /* Ma
2ca6c 6b 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74  ke an entry in t
2ca6d 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
2ca6e 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 76 20   table */.    v 
2ca6f 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
2ca70 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66  (pParse);.    if
2ca71 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 74 72  ( v==0 ) goto tr
2ca72 69 67 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61  iggerfinish_clea
2ca73 6e 75 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  nup;.    sqlite3
2ca74 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74  BeginWriteOperat
2ca75 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69  ion(pParse, 0, i
2ca76 44 62 29 3b 0a 20 20 20 20 7a 20 3d 20 73 71 6c  Db);.    z = sql
2ca77 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62  ite3DbStrNDup(db
2ca78 2c 20 28 63 68 61 72 2a 29 70 41 6c 6c 2d 3e 7a  , (char*)pAll->z
2ca79 2c 20 70 41 6c 6c 2d 3e 6e 29 3b 0a 20 20 20 20  , pAll->n);.    
2ca7a 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
2ca7b 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20  se(pParse,.     
2ca7c 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 25    "INSERT INTO %
2ca7d 51 2e 25 73 20 56 41 4c 55 45 53 28 27 74 72 69  Q.%s VALUES('tri
2ca7e 67 67 65 72 27 2c 25 51 2c 25 51 2c 30 2c 27 43  gger',%Q,%Q,0,'C
2ca7f 52 45 41 54 45 20 54 52 49 47 47 45 52 20 25 71  REATE TRIGGER %q
2ca80 27 29 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e  ')",.       db->
2ca81 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20  aDb[iDb].zName, 
2ca82 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62  SCHEMA_TABLE(iDb
2ca83 29 2c 20 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20  ), zName,.      
2ca84 20 70 54 72 69 67 2d 3e 74 61 62 6c 65 2c 20 7a   pTrig->table, z
2ca85 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
2ca86 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20  Free(db, z);.   
2ca87 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f   sqlite3ChangeCo
2ca88 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62  okie(pParse, iDb
2ca89 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2ca8a 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50  beAddOp4(v, OP_P
2ca8b 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62 2c  arseSchema, iDb,
2ca8c 20 30 2c 20 30 2c 20 73 71 6c 69 74 65 33 4d 50   0, 0, sqlite3MP
2ca8d 72 69 6e 74 66 28 0a 20 20 20 20 20 20 20 20 64  rintf(.        d
2ca8e 62 2c 20 22 74 79 70 65 3d 27 74 72 69 67 67 65  b, "type='trigge
2ca8f 72 27 20 41 4e 44 20 6e 61 6d 65 3d 27 25 71 27  r' AND name='%q'
2ca90 22 2c 20 7a 4e 61 6d 65 29 2c 20 50 34 5f 44 59  ", zName), P4_DY
2ca91 4e 41 4d 49 43 0a 20 20 20 20 29 3b 0a 20 20 7d  NAMIC.    );.  }
2ca92 0a 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74  ..  if( db->init
2ca93 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 54 72 69  .busy ){.    Tri
2ca94 67 67 65 72 20 2a 70 4c 69 6e 6b 20 3d 20 70 54  gger *pLink = pT
2ca95 72 69 67 3b 0a 20 20 20 20 48 61 73 68 20 2a 70  rig;.    Hash *p
2ca96 48 61 73 68 20 3d 20 26 64 62 2d 3e 61 44 62 5b  Hash = &db->aDb[
2ca97 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72  iDb].pSchema->tr
2ca98 69 67 48 61 73 68 3b 0a 20 20 20 20 70 54 72 69  igHash;.    pTri
2ca99 67 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49  g = sqlite3HashI
2ca9a 6e 73 65 72 74 28 70 48 61 73 68 2c 20 7a 4e 61  nsert(pHash, zNa
2ca9b 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65  me, sqlite3Strle
2ca9c 6e 33 30 28 7a 4e 61 6d 65 29 2c 20 70 54 72 69  n30(zName), pTri
2ca9d 67 29 3b 0a 20 20 20 20 69 66 28 20 70 54 72 69  g);.    if( pTri
2ca9e 67 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d  g ){.      db->m
2ca9f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
2caa0 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
2caa1 4c 69 6e 6b 2d 3e 70 53 63 68 65 6d 61 3d 3d 70  Link->pSchema==p
2caa2 4c 69 6e 6b 2d 3e 70 54 61 62 53 63 68 65 6d 61  Link->pTabSchema
2caa3 20 29 7b 0a 20 20 20 20 20 20 54 61 62 6c 65 20   ){.      Table 
2caa4 2a 70 54 61 62 3b 0a 20 20 20 20 20 20 69 6e 74  *pTab;.      int
2caa5 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
2caa6 65 6e 33 30 28 70 4c 69 6e 6b 2d 3e 74 61 62 6c  en30(pLink->tabl
2caa7 65 29 3b 0a 20 20 20 20 20 20 70 54 61 62 20 3d  e);.      pTab =
2caa8 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64   sqlite3HashFind
2caa9 28 26 70 4c 69 6e 6b 2d 3e 70 54 61 62 53 63 68  (&pLink->pTabSch
2caaa 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 4c  ema->tblHash, pL
2caab 69 6e 6b 2d 3e 74 61 62 6c 65 2c 20 6e 29 3b 0a  ink->table, n);.
2caac 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
2caad 61 62 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  ab!=0 );.      p
2caae 4c 69 6e 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 54  Link->pNext = pT
2caaf 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 0a 20 20  ab->pTrigger;.  
2cab0 20 20 20 20 70 54 61 62 2d 3e 70 54 72 69 67 67      pTab->pTrigg
2cab1 65 72 20 3d 20 70 4c 69 6e 6b 3b 0a 20 20 20 20  er = pLink;.    
2cab2 7d 0a 20 20 7d 0a 0a 74 72 69 67 67 65 72 66 69  }.  }..triggerfi
2cab3 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3a 0a 20 20  nish_cleanup:.  
2cab4 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
2cab5 67 67 65 72 28 64 62 2c 20 70 54 72 69 67 29 3b  gger(db, pTrig);
2cab6 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61 72  .  assert( !pPar
2cab7 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 20  se->pNewTrigger 
2cab8 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65  );.  sqlite3Dele
2cab9 74 65 54 72 69 67 67 65 72 53 74 65 70 28 64 62  teTriggerStep(db
2caba 2c 20 70 53 74 65 70 4c 69 73 74 29 3b 0a 7d 0a  , pStepList);.}.
2cabb 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 53 45  ./*.** Turn a SE
2cabc 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 28  LECT statement (
2cabd 74 68 61 74 20 74 68 65 20 70 53 65 6c 65 63 74  that the pSelect
2cabe 20 70 61 72 61 6d 65 74 65 72 20 70 6f 69 6e 74   parameter point
2cabf 73 20 74 6f 29 20 69 6e 74 6f 0a 2a 2a 20 61 20  s to) into.** a 
2cac0 74 72 69 67 67 65 72 20 73 74 65 70 2e 20 20 52  trigger step.  R
2cac1 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
2cac2 74 6f 20 61 20 54 72 69 67 67 65 72 53 74 65 70  to a TriggerStep
2cac3 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a   structure..**.*
2cac4 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c  * The parser cal
2cac5 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
2cac6 77 68 65 6e 20 69 74 20 66 69 6e 64 73 20 61 20  when it finds a 
2cac7 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2cac8 20 69 6e 0a 2a 2a 20 62 6f 64 79 20 6f 66 20 61   in.** body of a
2cac9 20 54 52 49 47 47 45 52 2e 20 20 0a 2a 2f 0a 53   TRIGGER.  .*/.S
2caca 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 72  QLITE_PRIVATE Tr
2cacb 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74  iggerStep *sqlit
2cacc 65 33 54 72 69 67 67 65 72 53 65 6c 65 63 74 53  e3TriggerSelectS
2cacd 74 65 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  tep(sqlite3 *db,
2cace 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
2cacf 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70  ){.  TriggerStep
2cad0 20 2a 70 54 72 69 67 67 65 72 53 74 65 70 20 3d   *pTriggerStep =
2cad1 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
2cad2 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
2cad3 54 72 69 67 67 65 72 53 74 65 70 29 29 3b 0a 20  TriggerStep));. 
2cad4 20 69 66 28 20 70 54 72 69 67 67 65 72 53 74 65   if( pTriggerSte
2cad5 70 3d 3d 30 20 29 20 7b 0a 20 20 20 20 73 71 6c  p==0 ) {.    sql
2cad6 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
2cad7 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20  (db, pSelect);. 
2cad8 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
2cad9 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d  .  pTriggerStep-
2cada 3e 6f 70 20 3d 20 54 4b 5f 53 45 4c 45 43 54 3b  >op = TK_SELECT;
2cadb 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d  .  pTriggerStep-
2cadc 3e 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65  >pSelect = pSele
2cadd 63 74 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74  ct;.  pTriggerSt
2cade 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20 4f 45 5f  ep->orconf = OE_
2cadf 44 65 66 61 75 6c 74 3b 0a 20 20 72 65 74 75 72  Default;.  retur
2cae0 6e 20 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a  n pTriggerStep;.
2cae1 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  }../*.** Allocat
2cae2 65 20 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20  e space to hold 
2cae3 61 20 6e 65 77 20 74 72 69 67 67 65 72 20 73 74  a new trigger st
2cae4 65 70 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 74  ep.  The allocat
2cae5 65 64 20 73 70 61 63 65 0a 2a 2a 20 68 6f 6c 64  ed space.** hold
2cae6 73 20 62 6f 74 68 20 74 68 65 20 54 72 69 67 67  s both the Trigg
2cae7 65 72 53 74 65 70 20 6f 62 6a 65 63 74 20 61 6e  erStep object an
2cae8 64 20 74 68 65 20 54 72 69 67 67 65 72 53 74 65  d the TriggerSte
2cae9 70 2e 74 61 72 67 65 74 2e 7a 20 73 74 72 69 6e  p.target.z strin
2caea 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 4f  g..**.** If an O
2caeb 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  OM error occurs,
2caec 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
2caed 64 20 61 6e 64 20 64 62 2d 3e 6d 61 6c 6c 6f 63  d and db->malloc
2caee 46 61 69 6c 65 64 20 69 73 20 73 65 74 2e 0a 2a  Failed is set..*
2caef 2f 0a 73 74 61 74 69 63 20 54 72 69 67 67 65 72  /.static Trigger
2caf0 53 74 65 70 20 2a 74 72 69 67 67 65 72 53 74 65  Step *triggerSte
2caf1 70 41 6c 6c 6f 63 61 74 65 28 0a 20 20 73 71 6c  pAllocate(.  sql
2caf2 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
2caf3 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
2caf4 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
2caf5 2a 2f 0a 20 20 75 38 20 6f 70 2c 20 20 20 20 20  */.  u8 op,     
2caf6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2caf7 20 2f 2a 20 54 72 69 67 67 65 72 20 6f 70 63 6f   /* Trigger opco
2caf8 64 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  de */.  Token *p
2caf9 4e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20  Name            
2cafa 20 20 20 20 2f 2a 20 54 68 65 20 74 61 72 67 65      /* The targe
2cafb 74 20 6e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 54  t name */.){.  T
2cafc 72 69 67 67 65 72 53 74 65 70 20 2a 70 54 72 69  riggerStep *pTri
2cafd 67 67 65 72 53 74 65 70 3b 0a 0a 20 20 70 54 72  ggerStep;..  pTr
2cafe 69 67 67 65 72 53 74 65 70 20 3d 20 73 71 6c 69  iggerStep = sqli
2caff 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
2cb00 64 62 2c 20 73 69 7a 65 6f 66 28 54 72 69 67 67  db, sizeof(Trigg
2cb01 65 72 53 74 65 70 29 20 2b 20 70 4e 61 6d 65 2d  erStep) + pName-
2cb02 3e 6e 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67  >n);.  if( pTrig
2cb03 67 65 72 53 74 65 70 20 29 7b 0a 20 20 20 20 63  gerStep ){.    c
2cb04 68 61 72 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29  har *z = (char*)
2cb05 26 70 54 72 69 67 67 65 72 53 74 65 70 5b 31 5d  &pTriggerStep[1]
2cb06 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20  ;.    memcpy(z, 
2cb07 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d  pName->z, pName-
2cb08 3e 6e 29 3b 0a 20 20 20 20 70 54 72 69 67 67 65  >n);.    pTrigge
2cb09 72 53 74 65 70 2d 3e 74 61 72 67 65 74 2e 7a 20  rStep->target.z 
2cb0a 3d 20 7a 3b 0a 20 20 20 20 70 54 72 69 67 67 65  = z;.    pTrigge
2cb0b 72 53 74 65 70 2d 3e 74 61 72 67 65 74 2e 6e 20  rStep->target.n 
2cb0c 3d 20 70 4e 61 6d 65 2d 3e 6e 3b 0a 20 20 20 20  = pName->n;.    
2cb0d 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 70  pTriggerStep->op
2cb0e 20 3d 20 6f 70 3b 0a 20 20 7d 0a 20 20 72 65 74   = op;.  }.  ret
2cb0f 75 72 6e 20 70 54 72 69 67 67 65 72 53 74 65 70  urn pTriggerStep
2cb10 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 75 69 6c 64  ;.}../*.** Build
2cb11 20 61 20 74 72 69 67 67 65 72 20 73 74 65 70 20   a trigger step 
2cb12 6f 75 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 54  out of an INSERT
2cb13 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 52 65 74   statement.  Ret
2cb14 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a  urn a pointer.**
2cb15 20 74 6f 20 74 68 65 20 6e 65 77 20 74 72 69 67   to the new trig
2cb16 67 65 72 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20  ger step..**.** 
2cb17 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73  The parser calls
2cb18 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68   this routine wh
2cb19 65 6e 20 69 74 20 73 65 65 73 20 61 6e 20 49 4e  en it sees an IN
2cb1a 53 45 52 54 20 69 6e 73 69 64 65 20 74 68 65 0a  SERT inside the.
2cb1b 2a 2a 20 62 6f 64 79 20 6f 66 20 61 20 74 72 69  ** body of a tri
2cb1c 67 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  gger..*/.SQLITE_
2cb1d 50 52 49 56 41 54 45 20 54 72 69 67 67 65 72 53  PRIVATE TriggerS
2cb1e 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67  tep *sqlite3Trig
2cb1f 67 65 72 49 6e 73 65 72 74 53 74 65 70 28 0a 20  gerInsertStep(. 
2cb20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
2cb21 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
2cb22 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
2cb23 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62  */.  Token *pTab
2cb24 6c 65 4e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65  leName,  /* Name
2cb25 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e   of the table in
2cb26 74 6f 20 77 68 69 63 68 20 77 65 20 69 6e 73 65  to which we inse
2cb27 72 74 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a  rt */.  IdList *
2cb28 70 43 6f 6c 75 6d 6e 2c 20 20 20 20 2f 2a 20 4c  pColumn,    /* L
2cb29 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ist of columns i
2cb2a 6e 20 70 54 61 62 6c 65 4e 61 6d 65 20 74 6f 20  n pTableName to 
2cb2b 69 6e 73 65 72 74 20 69 6e 74 6f 20 2a 2f 0a 20  insert into */. 
2cb2c 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
2cb2d 74 2c 20 20 20 2f 2a 20 54 68 65 20 56 41 4c 55  t,   /* The VALU
2cb2e 45 20 63 6c 61 75 73 65 3a 20 61 20 6c 69 73 74  E clause: a list
2cb2f 20 6f 66 20 76 61 6c 75 65 73 20 74 6f 20 62 65   of values to be
2cb30 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 53   inserted */.  S
2cb31 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20  elect *pSelect, 
2cb32 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73     /* A SELECT s
2cb33 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 73 75  tatement that su
2cb34 70 70 6c 69 65 73 20 76 61 6c 75 65 73 20 2a 2f  pplies values */
2cb35 0a 20 20 75 38 20 6f 72 63 6f 6e 66 20 20 20 20  .  u8 orconf    
2cb36 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f         /* The co
2cb37 6e 66 6c 69 63 74 20 61 6c 67 6f 72 69 74 68 6d  nflict algorithm
2cb38 20 28 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 52   (OE_Abort, OE_R
2cb39 65 70 6c 61 63 65 2c 20 65 74 63 2e 29 20 2a 2f  eplace, etc.) */
2cb3a 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65  .){.  TriggerSte
2cb3b 70 20 2a 70 54 72 69 67 67 65 72 53 74 65 70 3b  p *pTriggerStep;
2cb3c 0a 0a 20 20 61 73 73 65 72 74 28 70 45 4c 69 73  ..  assert(pELis
2cb3d 74 20 3d 3d 20 30 20 7c 7c 20 70 53 65 6c 65 63  t == 0 || pSelec
2cb3e 74 20 3d 3d 20 30 29 3b 0a 20 20 61 73 73 65 72  t == 0);.  asser
2cb3f 74 28 70 45 4c 69 73 74 20 21 3d 20 30 20 7c 7c  t(pEList != 0 ||
2cb40 20 70 53 65 6c 65 63 74 20 21 3d 20 30 20 7c 7c   pSelect != 0 ||
2cb41 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2cb42 64 29 3b 0a 0a 20 20 70 54 72 69 67 67 65 72 53  d);..  pTriggerS
2cb43 74 65 70 20 3d 20 74 72 69 67 67 65 72 53 74 65  tep = triggerSte
2cb44 70 41 6c 6c 6f 63 61 74 65 28 64 62 2c 20 54 4b  pAllocate(db, TK
2cb45 5f 49 4e 53 45 52 54 2c 20 70 54 61 62 6c 65 4e  _INSERT, pTableN
2cb46 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 54 72 69  ame);.  if( pTri
2cb47 67 67 65 72 53 74 65 70 20 29 7b 0a 20 20 20 20  ggerStep ){.    
2cb48 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 53  pTriggerStep->pS
2cb49 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53  elect = sqlite3S
2cb4a 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 53 65  electDup(db, pSe
2cb4b 6c 65 63 74 2c 20 45 58 50 52 44 55 50 5f 52 45  lect, EXPRDUP_RE
2cb4c 44 55 43 45 29 3b 0a 20 20 20 20 70 54 72 69 67  DUCE);.    pTrig
2cb4d 67 65 72 53 74 65 70 2d 3e 70 49 64 4c 69 73 74  gerStep->pIdList
2cb4e 20 3d 20 70 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20   = pColumn;.    
2cb4f 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70 45  pTriggerStep->pE
2cb50 78 70 72 4c 69 73 74 20 3d 20 73 71 6c 69 74 65  xprList = sqlite
2cb51 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
2cb52 20 70 45 4c 69 73 74 2c 20 45 58 50 52 44 55 50   pEList, EXPRDUP
2cb53 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 20 70 54  _REDUCE);.    pT
2cb54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72 63 6f  riggerStep->orco
2cb55 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20 20 7d  nf = orconf;.  }
2cb56 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
2cb57 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62  3IdListDelete(db
2cb58 2c 20 70 43 6f 6c 75 6d 6e 29 3b 0a 20 20 7d 0a  , pColumn);.  }.
2cb59 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
2cb5a 74 44 65 6c 65 74 65 28 64 62 2c 20 70 45 4c 69  tDelete(db, pELi
2cb5b 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  st);.  sqlite3Se
2cb5c 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
2cb5d 53 65 6c 65 63 74 29 3b 0a 0a 20 20 72 65 74 75  Select);..  retu
2cb5e 72 6e 20 70 54 72 69 67 67 65 72 53 74 65 70 3b  rn pTriggerStep;
2cb5f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72  .}../*.** Constr
2cb60 75 63 74 20 61 20 74 72 69 67 67 65 72 20 73 74  uct a trigger st
2cb61 65 70 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  ep that implemen
2cb62 74 73 20 61 6e 20 55 50 44 41 54 45 20 73 74 61  ts an UPDATE sta
2cb63 74 65 6d 65 6e 74 20 61 6e 64 20 72 65 74 75 72  tement and retur
2cb64 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  n.** a pointer t
2cb65 6f 20 74 68 61 74 20 74 72 69 67 67 65 72 20 73  o that trigger s
2cb66 74 65 70 2e 20 20 54 68 65 20 70 61 72 73 65 72  tep.  The parser
2cb67 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
2cb68 69 6e 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 73  ine when it.** s
2cb69 65 65 73 20 61 6e 20 55 50 44 41 54 45 20 73 74  ees an UPDATE st
2cb6a 61 74 65 6d 65 6e 74 20 69 6e 73 69 64 65 20 74  atement inside t
2cb6b 68 65 20 62 6f 64 79 20 6f 66 20 61 20 43 52 45  he body of a CRE
2cb6c 41 54 45 20 54 52 49 47 47 45 52 2e 0a 2a 2f 0a  ATE TRIGGER..*/.
2cb6d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54  SQLITE_PRIVATE T
2cb6e 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69  riggerStep *sqli
2cb6f 74 65 33 54 72 69 67 67 65 72 55 70 64 61 74 65  te3TriggerUpdate
2cb70 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33 20  Step(.  sqlite3 
2cb71 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  *db,         /* 
2cb72 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
2cb73 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b  nection */.  Tok
2cb74 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 20  en *pTableName, 
2cb75 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
2cb76 20 74 61 62 6c 65 20 74 6f 20 62 65 20 75 70 64   table to be upd
2cb77 61 74 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69  ated */.  ExprLi
2cb78 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 2f  st *pEList,    /
2cb79 2a 20 54 68 65 20 53 45 54 20 63 6c 61 75 73 65  * The SET clause
2cb7a 3a 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e  : list of column
2cb7b 20 61 6e 64 20 6e 65 77 20 76 61 6c 75 65 73 20   and new values 
2cb7c 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72  */.  Expr *pWher
2cb7d 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  e,        /* The
2cb7e 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
2cb7f 0a 20 20 75 38 20 6f 72 63 6f 6e 66 20 20 20 20  .  u8 orconf    
2cb80 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63          /* The c
2cb81 6f 6e 66 6c 69 63 74 20 61 6c 67 6f 72 69 74 68  onflict algorith
2cb82 6d 2e 20 28 4f 45 5f 41 62 6f 72 74 2c 20 4f 45  m. (OE_Abort, OE
2cb83 5f 49 67 6e 6f 72 65 2c 20 65 74 63 29 20 2a 2f  _Ignore, etc) */
2cb84 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65  .){.  TriggerSte
2cb85 70 20 2a 70 54 72 69 67 67 65 72 53 74 65 70 3b  p *pTriggerStep;
2cb86 0a 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70  ..  pTriggerStep
2cb87 20 3d 20 74 72 69 67 67 65 72 53 74 65 70 41 6c   = triggerStepAl
2cb88 6c 6f 63 61 74 65 28 64 62 2c 20 54 4b 5f 55 50  locate(db, TK_UP
2cb89 44 41 54 45 2c 20 70 54 61 62 6c 65 4e 61 6d 65  DATE, pTableName
2cb8a 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65  );.  if( pTrigge
2cb8b 72 53 74 65 70 20 29 7b 0a 20 20 20 20 70 54 72  rStep ){.    pTr
2cb8c 69 67 67 65 72 53 74 65 70 2d 3e 70 45 78 70 72  iggerStep->pExpr
2cb8d 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
2cb8e 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 45  prListDup(db, pE
2cb8f 4c 69 73 74 2c 20 45 58 50 52 44 55 50 5f 52 45  List, EXPRDUP_RE
2cb90 44 55 43 45 29 3b 0a 20 20 20 20 70 54 72 69 67  DUCE);.    pTrig
2cb91 67 65 72 53 74 65 70 2d 3e 70 57 68 65 72 65 20  gerStep->pWhere 
2cb92 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
2cb93 28 64 62 2c 20 70 57 68 65 72 65 2c 20 45 58 50  (db, pWhere, EXP
2cb94 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20  RDUP_REDUCE);.  
2cb95 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e    pTriggerStep->
2cb96 6f 72 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b  orconf = orconf;
2cb97 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78  .  }.  sqlite3Ex
2cb98 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
2cb99 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69   pEList);.  sqli
2cb9a 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
2cb9b 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 72 65 74  , pWhere);.  ret
2cb9c 75 72 6e 20 70 54 72 69 67 67 65 72 53 74 65 70  urn pTriggerStep
2cb9d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74  ;.}../*.** Const
2cb9e 72 75 63 74 20 61 20 74 72 69 67 67 65 72 20 73  ruct a trigger s
2cb9f 74 65 70 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  tep that impleme
2cba0 6e 74 73 20 61 20 44 45 4c 45 54 45 20 73 74 61  nts a DELETE sta
2cba1 74 65 6d 65 6e 74 20 61 6e 64 20 72 65 74 75 72  tement and retur
2cba2 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  n.** a pointer t
2cba3 6f 20 74 68 61 74 20 74 72 69 67 67 65 72 20 73  o that trigger s
2cba4 74 65 70 2e 20 20 54 68 65 20 70 61 72 73 65 72  tep.  The parser
2cba5 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
2cba6 69 6e 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 73  ine when it.** s
2cba7 65 65 73 20 61 20 44 45 4c 45 54 45 20 73 74 61  ees a DELETE sta
2cba8 74 65 6d 65 6e 74 20 69 6e 73 69 64 65 20 74 68  tement inside th
2cba9 65 20 62 6f 64 79 20 6f 66 20 61 20 43 52 45 41  e body of a CREA
2cbaa 54 45 20 54 52 49 47 47 45 52 2e 0a 2a 2f 0a 53  TE TRIGGER..*/.S
2cbab 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 72  QLITE_PRIVATE Tr
2cbac 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74  iggerStep *sqlit
2cbad 65 33 54 72 69 67 67 65 72 44 65 6c 65 74 65 53  e3TriggerDeleteS
2cbae 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  tep(.  sqlite3 *
2cbaf 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  db,            /
2cbb0 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
2cbb1 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ction */.  Token
2cbb2 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20   *pTableName,   
2cbb3 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
2cbb4 66 72 6f 6d 20 77 68 69 63 68 20 72 6f 77 73 20  from which rows 
2cbb5 61 72 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20  are deleted */. 
2cbb6 20 45 78 70 72 20 2a 70 57 68 65 72 65 20 20 20   Expr *pWhere   
2cbb7 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2cbb8 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
2cbb9 29 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70  ){.  TriggerStep
2cbba 20 2a 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a   *pTriggerStep;.
2cbbb 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70 20  .  pTriggerStep 
2cbbc 3d 20 74 72 69 67 67 65 72 53 74 65 70 41 6c 6c  = triggerStepAll
2cbbd 6f 63 61 74 65 28 64 62 2c 20 54 4b 5f 44 45 4c  ocate(db, TK_DEL
2cbbe 45 54 45 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29  ETE, pTableName)
2cbbf 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72  ;.  if( pTrigger
2cbc0 53 74 65 70 20 29 7b 0a 20 20 20 20 70 54 72 69  Step ){.    pTri
2cbc1 67 67 65 72 53 74 65 70 2d 3e 70 57 68 65 72 65  ggerStep->pWhere
2cbc2 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
2cbc3 70 28 64 62 2c 20 70 57 68 65 72 65 2c 20 45 58  p(db, pWhere, EX
2cbc4 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20  PRDUP_REDUCE);. 
2cbc5 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d     pTriggerStep-
2cbc6 3e 6f 72 63 6f 6e 66 20 3d 20 4f 45 5f 44 65 66  >orconf = OE_Def
2cbc7 61 75 6c 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  ault;.  }.  sqli
2cbc8 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
2cbc9 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 72 65 74  , pWhere);.  ret
2cbca 75 72 6e 20 70 54 72 69 67 67 65 72 53 74 65 70  urn pTriggerStep
2cbcb 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 63 75  ;.}../* .** Recu
2cbcc 72 73 69 76 65 6c 79 20 64 65 6c 65 74 65 20 61  rsively delete a
2cbcd 20 54 72 69 67 67 65 72 20 73 74 72 75 63 74 75   Trigger structu
2cbce 72 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  re.*/.SQLITE_PRI
2cbcf 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2cbd0 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 73  3DeleteTrigger(s
2cbd1 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 72 69 67  qlite3 *db, Trig
2cbd2 67 65 72 20 2a 70 54 72 69 67 67 65 72 29 7b 0a  ger *pTrigger){.
2cbd3 20 20 69 66 28 20 70 54 72 69 67 67 65 72 3d 3d    if( pTrigger==
2cbd4 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71  0 ) return;.  sq
2cbd5 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67  lite3DeleteTrigg
2cbd6 65 72 53 74 65 70 28 64 62 2c 20 70 54 72 69 67  erStep(db, pTrig
2cbd7 67 65 72 2d 3e 73 74 65 70 5f 6c 69 73 74 29 3b  ger->step_list);
2cbd8 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2cbd9 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 7a  (db, pTrigger->z
2cbda 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Name);.  sqlite3
2cbdb 44 62 46 72 65 65 28 64 62 2c 20 70 54 72 69 67  DbFree(db, pTrig
2cbdc 67 65 72 2d 3e 74 61 62 6c 65 29 3b 0a 20 20 73  ger->table);.  s
2cbdd 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2cbde 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 70  (db, pTrigger->p
2cbdf 57 68 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33  When);.  sqlite3
2cbe0 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  IdListDelete(db,
2cbe1 20 70 54 72 69 67 67 65 72 2d 3e 70 43 6f 6c 75   pTrigger->pColu
2cbe2 6d 6e 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  mns);.  sqlite3D
2cbe3 62 46 72 65 65 28 64 62 2c 20 70 54 72 69 67 67  bFree(db, pTrigg
2cbe4 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  er);.}../*.** Th
2cbe5 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
2cbe6 61 6c 6c 65 64 20 74 6f 20 64 72 6f 70 20 61 20  alled to drop a 
2cbe7 74 72 69 67 67 65 72 20 66 72 6f 6d 20 74 68 65  trigger from the
2cbe8 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
2cbe9 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61  . .**.** This ma
2cbea 79 20 62 65 20 63 61 6c 6c 65 64 20 64 69 72 65  y be called dire
2cbeb 63 74 6c 79 20 66 72 6f 6d 20 74 68 65 20 70 61  ctly from the pa
2cbec 72 73 65 72 20 61 6e 64 20 74 68 65 72 65 66 6f  rser and therefo
2cbed 72 65 20 69 64 65 6e 74 69 66 69 65 73 0a 2a 2a  re identifies.**
2cbee 20 74 68 65 20 74 72 69 67 67 65 72 20 62 79 20   the trigger by 
2cbef 6e 61 6d 65 2e 20 20 54 68 65 20 73 71 6c 69 74  name.  The sqlit
2cbf0 65 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72  e3DropTriggerPtr
2cbf1 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20  () routine does 
2cbf2 74 68 65 0a 2a 2a 20 73 61 6d 65 20 6a 6f 62 20  the.** same job 
2cbf3 61 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  as this routine 
2cbf4 65 78 63 65 70 74 20 69 74 20 74 61 6b 65 73 20  except it takes 
2cbf5 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
2cbf6 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 6e 73 74   trigger.** inst
2cbf7 65 61 64 20 6f 66 20 74 68 65 20 74 72 69 67 67  ead of the trigg
2cbf8 65 72 20 6e 61 6d 65 2e 0a 2a 2a 2f 0a 53 51 4c  er name..**/.SQL
2cbf9 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2cbfa 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67   sqlite3DropTrig
2cbfb 67 65 72 28 50 61 72 73 65 20 2a 70 50 61 72 73  ger(Parse *pPars
2cbfc 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d  e, SrcList *pNam
2cbfd 65 2c 20 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20  e, int noErr){. 
2cbfe 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67   Trigger *pTrigg
2cbff 65 72 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b  er = 0;.  int i;
2cc00 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2cc01 44 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  Db;.  const char
2cc02 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e   *zName;.  int n
2cc03 4e 61 6d 65 3b 0a 20 20 73 71 6c 69 74 65 33 20  Name;.  sqlite3 
2cc04 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2cc05 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  ;..  if( db->mal
2cc06 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f  locFailed ) goto
2cc07 20 64 72 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c   drop_trigger_cl
2cc08 65 61 6e 75 70 3b 0a 20 20 69 66 28 20 53 51 4c  eanup;.  if( SQL
2cc09 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52  ITE_OK!=sqlite3R
2cc0a 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
2cc0b 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 72  ) ){.    goto dr
2cc0c 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61 6e  op_trigger_clean
2cc0d 75 70 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  up;.  }..  asser
2cc0e 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d  t( pName->nSrc==
2cc0f 31 20 29 3b 0a 20 20 7a 44 62 20 3d 20 70 4e 61  1 );.  zDb = pNa
2cc10 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61  me->a[0].zDataba
2cc11 73 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 70 4e  se;.  zName = pN
2cc12 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 3b  ame->a[0].zName;
2cc13 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74  .  nName = sqlit
2cc14 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65  e3Strlen30(zName
2cc15 29 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d 49 54 5f  );.  for(i=OMIT_
2cc16 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e 6e 44  TEMPDB; i<db->nD
2cc17 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  b; i++){.    int
2cc18 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69 5e 31   j = (i<2) ? i^1
2cc19 20 3a 20 69 3b 20 20 2f 2a 20 53 65 61 72 63 68   : i;  /* Search
2cc1a 20 54 45 4d 50 20 62 65 66 6f 72 65 20 4d 41 49   TEMP before MAI
2cc1b 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a 44 62  N */.    if( zDb
2cc1c 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
2cc1d 6d 70 28 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e  mp(db->aDb[j].zN
2cc1e 61 6d 65 2c 20 7a 44 62 29 20 29 20 63 6f 6e 74  ame, zDb) ) cont
2cc1f 69 6e 75 65 3b 0a 20 20 20 20 70 54 72 69 67 67  inue;.    pTrigg
2cc20 65 72 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68  er = sqlite3Hash
2cc21 46 69 6e 64 28 26 28 64 62 2d 3e 61 44 62 5b 6a  Find(&(db->aDb[j
2cc22 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48  ].pSchema->trigH
2cc23 61 73 68 29 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  ash), zName, nNa
2cc24 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70 54 72  me);.    if( pTr
2cc25 69 67 67 65 72 20 29 20 62 72 65 61 6b 3b 0a 20  igger ) break;. 
2cc26 20 7d 0a 20 20 69 66 28 20 21 70 54 72 69 67 67   }.  if( !pTrigg
2cc27 65 72 20 29 7b 0a 20 20 20 20 69 66 28 20 21 6e  er ){.    if( !n
2cc28 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20 73 71  oErr ){.      sq
2cc29 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2cc2a 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 74  arse, "no such t
2cc2b 72 69 67 67 65 72 3a 20 25 53 22 2c 20 70 4e 61  rigger: %S", pNa
2cc2c 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  me, 0);.    }.  
2cc2d 20 20 67 6f 74 6f 20 64 72 6f 70 5f 74 72 69 67    goto drop_trig
2cc2e 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ger_cleanup;.  }
2cc2f 0a 20 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72  .  sqlite3DropTr
2cc30 69 67 67 65 72 50 74 72 28 70 50 61 72 73 65 2c  iggerPtr(pParse,
2cc31 20 70 54 72 69 67 67 65 72 29 3b 0a 0a 64 72 6f   pTrigger);..dro
2cc32 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  p_trigger_cleanu
2cc33 70 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c  p:.  sqlite3SrcL
2cc34 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4e  istDelete(db, pN
2cc35 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ame);.}../*.** R
2cc36 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
2cc37 74 6f 20 74 68 65 20 54 61 62 6c 65 20 73 74 72  to the Table str
2cc38 75 63 74 75 72 65 20 66 6f 72 20 74 68 65 20 74  ucture for the t
2cc39 61 62 6c 65 20 74 68 61 74 20 61 20 74 72 69 67  able that a trig
2cc3a 67 65 72 0a 2a 2a 20 69 73 20 73 65 74 20 6f 6e  ger.** is set on
2cc3b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54 61 62 6c  ..*/.static Tabl
2cc3c 65 20 2a 74 61 62 6c 65 4f 66 54 72 69 67 67 65  e *tableOfTrigge
2cc3d 72 28 54 72 69 67 67 65 72 20 2a 70 54 72 69 67  r(Trigger *pTrig
2cc3e 67 65 72 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20  ger){.  int n = 
2cc3f 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2cc40 70 54 72 69 67 67 65 72 2d 3e 74 61 62 6c 65 29  pTrigger->table)
2cc41 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ;.  return sqlit
2cc42 65 33 48 61 73 68 46 69 6e 64 28 26 70 54 72 69  e3HashFind(&pTri
2cc43 67 67 65 72 2d 3e 70 54 61 62 53 63 68 65 6d 61  gger->pTabSchema
2cc44 2d 3e 74 62 6c 48 61 73 68 2c 20 70 54 72 69 67  ->tblHash, pTrig
2cc45 67 65 72 2d 3e 74 61 62 6c 65 2c 20 6e 29 3b 0a  ger->table, n);.
2cc46 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 61  }.../*.** Drop a
2cc47 20 74 72 69 67 67 65 72 20 67 69 76 65 6e 20 61   trigger given a
2cc48 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74   pointer to that
2cc49 20 74 72 69 67 67 65 72 2e 20 0a 2a 2f 0a 53 51   trigger. .*/.SQ
2cc4a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2cc4b 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69  d sqlite3DropTri
2cc4c 67 67 65 72 50 74 72 28 50 61 72 73 65 20 2a 70  ggerPtr(Parse *p
2cc4d 50 61 72 73 65 2c 20 54 72 69 67 67 65 72 20 2a  Parse, Trigger *
2cc4e 70 54 72 69 67 67 65 72 29 7b 0a 20 20 54 61 62  pTrigger){.  Tab
2cc4f 6c 65 20 20 20 2a 70 54 61 62 6c 65 3b 0a 20 20  le   *pTable;.  
2cc50 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74  Vdbe *v;.  sqlit
2cc51 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2cc52 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a  >db;.  int iDb;.
2cc53 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
2cc54 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
2cc55 61 72 73 65 2d 3e 64 62 2c 20 70 54 72 69 67 67  arse->db, pTrigg
2cc56 65 72 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  er->pSchema);.  
2cc57 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26  assert( iDb>=0 &
2cc58 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b  & iDb<db->nDb );
2cc59 0a 20 20 70 54 61 62 6c 65 20 3d 20 74 61 62 6c  .  pTable = tabl
2cc5a 65 4f 66 54 72 69 67 67 65 72 28 70 54 72 69 67  eOfTrigger(pTrig
2cc5b 67 65 72 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ger);.  assert( 
2cc5c 70 54 61 62 6c 65 20 29 3b 0a 20 20 61 73 73 65  pTable );.  asse
2cc5d 72 74 28 20 70 54 61 62 6c 65 2d 3e 70 53 63 68  rt( pTable->pSch
2cc5e 65 6d 61 3d 3d 70 54 72 69 67 67 65 72 2d 3e 70  ema==pTrigger->p
2cc5f 53 63 68 65 6d 61 20 7c 7c 20 69 44 62 3d 3d 31  Schema || iDb==1
2cc60 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   );.#ifndef SQLI
2cc61 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
2cc62 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e  ATION.  {.    in
2cc63 74 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f  t code = SQLITE_
2cc64 44 52 4f 50 5f 54 52 49 47 47 45 52 3b 0a 20 20  DROP_TRIGGER;.  
2cc65 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
2cc66 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  b = db->aDb[iDb]
2cc67 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73  .zName;.    cons
2cc68 74 20 63 68 61 72 20 2a 7a 54 61 62 20 3d 20 53  t char *zTab = S
2cc69 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29  CHEMA_TABLE(iDb)
2cc6a 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3d 3d 31  ;.    if( iDb==1
2cc6b 20 29 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45   ) code = SQLITE
2cc6c 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47  _DROP_TEMP_TRIGG
2cc6d 45 52 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  ER;.    if( sqli
2cc6e 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
2cc6f 72 73 65 2c 20 63 6f 64 65 2c 20 70 54 72 69 67  rse, code, pTrig
2cc70 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62  ger->zName, pTab
2cc71 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20  le->zName, zDb) 
2cc72 7c 7c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ||.      sqlite3
2cc73 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
2cc74 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c  , SQLITE_DELETE,
2cc75 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62 29 20 29   zTab, 0, zDb) )
2cc76 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  {.      return;.
2cc77 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
2cc78 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
2cc79 63 6f 64 65 20 74 6f 20 64 65 73 74 72 6f 79 20  code to destroy 
2cc7a 74 68 65 20 64 61 74 61 62 61 73 65 20 72 65 63  the database rec
2cc7b 6f 72 64 20 6f 66 20 74 68 65 20 74 72 69 67 67  ord of the trigg
2cc7c 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  er..  */.  asser
2cc7d 74 28 20 70 54 61 62 6c 65 21 3d 30 20 29 3b 0a  t( pTable!=0 );.
2cc7e 20 20 69 66 28 20 28 76 20 3d 20 73 71 6c 69 74    if( (v = sqlit
2cc7f 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
2cc80 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ))!=0 ){.    int
2cc81 20 62 61 73 65 3b 0a 20 20 20 20 73 74 61 74 69   base;.    stati
2cc82 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69  c const VdbeOpLi
2cc83 73 74 20 64 72 6f 70 54 72 69 67 67 65 72 5b 5d  st dropTrigger[]
2cc84 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20 4f 50 5f   = {.      { OP_
2cc85 52 65 77 69 6e 64 2c 20 20 20 20 20 30 2c 20 41  Rewind,     0, A
2cc86 44 44 52 28 39 29 2c 20 20 30 7d 2c 0a 20 20 20  DDR(9),  0},.   
2cc87 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c     { OP_String8,
2cc88 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20 20 20      0, 1,       
2cc89 20 30 7d 2c 20 2f 2a 20 31 20 2a 2f 0a 20 20 20   0}, /* 1 */.   
2cc8a 20 20 20 7b 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20     { OP_Column, 
2cc8b 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20 20 20      0, 1,       
2cc8c 20 32 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f   2},.      { OP_
2cc8d 4e 65 2c 20 20 20 20 20 20 20 20 20 32 2c 20 41  Ne,         2, A
2cc8e 44 44 52 28 38 29 2c 20 20 31 7d 2c 0a 20 20 20  DDR(8),  1},.   
2cc8f 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c     { OP_String8,
2cc90 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20 20 20      0, 1,       
2cc91 20 30 7d 2c 20 2f 2a 20 34 3a 20 22 74 72 69 67   0}, /* 4: "trig
2cc92 67 65 72 22 20 2a 2f 0a 20 20 20 20 20 20 7b 20  ger" */.      { 
2cc93 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 30  OP_Column,     0
2cc94 2c 20 30 2c 20 20 20 20 20 20 20 20 32 7d 2c 0a  , 0,        2},.
2cc95 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65 2c 20 20        { OP_Ne,  
2cc96 20 20 20 20 20 20 20 32 2c 20 41 44 44 52 28 38         2, ADDR(8
2cc97 29 2c 20 20 31 7d 2c 0a 20 20 20 20 20 20 7b 20  ),  1},.      { 
2cc98 4f 50 5f 44 65 6c 65 74 65 2c 20 20 20 20 20 30  OP_Delete,     0
2cc99 2c 20 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a  , 0,        0},.
2cc9a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65 78 74 2c        { OP_Next,
2cc9b 20 20 20 20 20 20 20 30 2c 20 41 44 44 52 28 31         0, ADDR(1
2cc9c 29 2c 20 20 30 7d 2c 20 2f 2a 20 38 20 2a 2f 0a  ),  0}, /* 8 */.
2cc9d 20 20 20 20 7d 3b 0a 0a 20 20 20 20 73 71 6c 69      };..    sqli
2cc9e 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
2cc9f 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30  ration(pParse, 0
2cca0 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69  , iDb);.    sqli
2cca1 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62  te3OpenMasterTab
2cca2 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  le(pParse, iDb);
2cca3 0a 20 20 20 20 62 61 73 65 20 3d 20 73 71 6c 69  .    base = sqli
2cca4 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
2cca5 28 76 2c 20 20 41 72 72 61 79 53 69 7a 65 28 64  (v,  ArraySize(d
2cca6 72 6f 70 54 72 69 67 67 65 72 29 2c 20 64 72 6f  ropTrigger), dro
2cca7 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 73  pTrigger);.    s
2cca8 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2cca9 50 34 28 76 2c 20 62 61 73 65 2b 31 2c 20 70 54  P4(v, base+1, pT
2ccaa 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20 30  rigger->zName, 0
2ccab 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2ccac 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 62 61  beChangeP4(v, ba
2ccad 73 65 2b 34 2c 20 22 74 72 69 67 67 65 72 22 2c  se+4, "trigger",
2ccae 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
2ccaf 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f   sqlite3ChangeCo
2ccb0 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62  okie(pParse, iDb
2ccb1 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2ccb2 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
2ccb3 6c 6f 73 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20  lose, 0, 0);.   
2ccb4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2ccb5 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72 69  p4(v, OP_DropTri
2ccb6 67 67 65 72 2c 20 69 44 62 2c 20 30 2c 20 30 2c  gger, iDb, 0, 0,
2ccb7 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65   pTrigger->zName
2ccb8 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 50  , 0);.    if( pP
2ccb9 61 72 73 65 2d 3e 6e 4d 65 6d 3c 33 20 29 7b 0a  arse->nMem<3 ){.
2ccba 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d        pParse->nM
2ccbb 65 6d 20 3d 20 33 3b 0a 20 20 20 20 7d 0a 20 20  em = 3;.    }.  
2ccbc 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76  }.}../*.** Remov
2ccbd 65 20 61 20 74 72 69 67 67 65 72 20 66 72 6f 6d  e a trigger from
2ccbe 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 73   the hash tables
2ccbf 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 2a 20   of the sqlite* 
2ccc0 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49  pointer..*/.SQLI
2ccc1 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2ccc2 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64  sqlite3UnlinkAnd
2ccc3 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71  DeleteTrigger(sq
2ccc4 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69  lite3 *db, int i
2ccc5 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  Db, const char *
2ccc6 7a 4e 61 6d 65 29 7b 0a 20 20 48 61 73 68 20 2a  zName){.  Hash *
2ccc7 70 48 61 73 68 20 3d 20 26 28 64 62 2d 3e 61 44  pHash = &(db->aD
2ccc8 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e  b[iDb].pSchema->
2ccc9 74 72 69 67 48 61 73 68 29 3b 0a 20 20 54 72 69  trigHash);.  Tri
2ccca 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 0a  gger *pTrigger;.
2cccb 20 20 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c    pTrigger = sql
2cccc 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 70  ite3HashInsert(p
2cccd 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 73 71 6c  Hash, zName, sql
2ccce 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61  ite3Strlen30(zNa
2cccf 6d 65 29 2c 20 30 29 3b 0a 20 20 69 66 28 20 41  me), 0);.  if( A
2ccd0 4c 57 41 59 53 28 70 54 72 69 67 67 65 72 29 20  LWAYS(pTrigger) 
2ccd1 29 7b 0a 20 20 20 20 69 66 28 20 70 54 72 69 67  ){.    if( pTrig
2ccd2 67 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54  ger->pSchema==pT
2ccd3 72 69 67 67 65 72 2d 3e 70 54 61 62 53 63 68 65  rigger->pTabSche
2ccd4 6d 61 20 29 7b 0a 20 20 20 20 20 20 54 61 62 6c  ma ){.      Tabl
2ccd5 65 20 2a 70 54 61 62 20 3d 20 74 61 62 6c 65 4f  e *pTab = tableO
2ccd6 66 54 72 69 67 67 65 72 28 70 54 72 69 67 67 65  fTrigger(pTrigge
2ccd7 72 29 3b 0a 20 20 20 20 20 20 54 72 69 67 67 65  r);.      Trigge
2ccd8 72 20 2a 2a 70 70 3b 0a 20 20 20 20 20 20 66 6f  r **pp;.      fo
2ccd9 72 28 70 70 3d 26 70 54 61 62 2d 3e 70 54 72 69  r(pp=&pTab->pTri
2ccda 67 67 65 72 3b 20 2a 70 70 21 3d 70 54 72 69 67  gger; *pp!=pTrig
2ccdb 67 65 72 3b 20 70 70 3d 26 28 28 2a 70 70 29 2d  ger; pp=&((*pp)-
2ccdc 3e 70 4e 65 78 74 29 29 3b 0a 20 20 20 20 20 20  >pNext));.      
2ccdd 2a 70 70 20 3d 20 28 2a 70 70 29 2d 3e 70 4e 65  *pp = (*pp)->pNe
2ccde 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  xt;.    }.    sq
2ccdf 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67  lite3DeleteTrigg
2cce0 65 72 28 64 62 2c 20 70 54 72 69 67 67 65 72 29  er(db, pTrigger)
2cce1 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20  ;.    db->flags 
2cce2 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  |= SQLITE_Intern
2cce3 43 68 61 6e 67 65 73 3b 0a 20 20 7d 0a 7d 0a 0a  Changes;.  }.}..
2cce4 2f 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 69 73 20  /*.** pEList is 
2cce5 74 68 65 20 53 45 54 20 63 6c 61 75 73 65 20 6f  the SET clause o
2cce6 66 20 61 6e 20 55 50 44 41 54 45 20 73 74 61 74  f an UPDATE stat
2cce7 65 6d 65 6e 74 2e 20 20 45 61 63 68 20 65 6e 74  ement.  Each ent
2cce8 72 79 0a 2a 2a 20 69 6e 20 70 45 4c 69 73 74 20  ry.** in pEList 
2cce9 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74  is of the format
2ccea 20 3c 69 64 3e 3d 3c 65 78 70 72 3e 2e 20 20 49   <id>=<expr>.  I
2cceb 66 20 61 6e 79 20 6f 66 20 74 68 65 20 65 6e 74  f any of the ent
2ccec 72 69 65 73 0a 2a 2a 20 69 6e 20 70 45 4c 69 73  ries.** in pELis
2cced 74 20 68 61 76 65 20 61 6e 20 3c 69 64 3e 20 77  t have an <id> w
2ccee 68 69 63 68 20 6d 61 74 63 68 65 73 20 61 6e 20  hich matches an 
2ccef 69 64 65 6e 74 69 66 69 65 72 20 69 6e 20 70 49  identifier in pI
2ccf0 64 4c 69 73 74 2c 0a 2a 2a 20 74 68 65 6e 20 72  dList,.** then r
2ccf1 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49 66 20  eturn TRUE.  If 
2ccf2 70 49 64 4c 69 73 74 3d 3d 4e 55 4c 4c 2c 20 74  pIdList==NULL, t
2ccf3 68 65 6e 20 69 74 20 69 73 20 63 6f 6e 73 69 64  hen it is consid
2ccf4 65 72 65 64 20 61 0a 2a 2a 20 77 69 6c 64 63 61  ered a.** wildca
2ccf5 72 64 20 74 68 61 74 20 6d 61 74 63 68 65 73 20  rd that matches 
2ccf6 61 6e 79 74 68 69 6e 67 2e 20 20 4c 69 6b 65 77  anything.  Likew
2ccf7 69 73 65 20 69 66 20 70 45 4c 69 73 74 3d 3d 4e  ise if pEList==N
2ccf8 55 4c 4c 20 74 68 65 6e 0a 2a 2a 20 69 74 20 6d  ULL then.** it m
2ccf9 61 74 63 68 65 73 20 61 6e 79 74 68 69 6e 67 20  atches anything 
2ccfa 73 6f 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e  so always return
2ccfb 20 74 72 75 65 2e 20 20 52 65 74 75 72 6e 20 66   true.  Return f
2ccfc 61 6c 73 65 20 6f 6e 6c 79 0a 2a 2a 20 69 66 20  alse only.** if 
2ccfd 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63  there is no matc
2ccfe 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  h..*/.static int
2ccff 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76 65 72   checkColumnOver
2cd00 6c 61 70 28 49 64 4c 69 73 74 20 2a 70 49 64 4c  lap(IdList *pIdL
2cd01 69 73 74 2c 20 45 78 70 72 4c 69 73 74 20 2a 70  ist, ExprList *p
2cd02 45 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 65 3b  EList){.  int e;
2cd03 0a 20 20 69 66 28 20 70 49 64 4c 69 73 74 3d 3d  .  if( pIdList==
2cd04 30 20 7c 7c 20 4e 45 56 45 52 28 70 45 4c 69 73  0 || NEVER(pELis
2cd05 74 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20 31  t==0) ) return 1
2cd06 3b 0a 20 20 66 6f 72 28 65 3d 30 3b 20 65 3c 70  ;.  for(e=0; e<p
2cd07 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 65 2b  EList->nExpr; e+
2cd08 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  +){.    if( sqli
2cd09 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28 70  te3IdListIndex(p
2cd0a 49 64 4c 69 73 74 2c 20 70 45 4c 69 73 74 2d 3e  IdList, pEList->
2cd0b 61 5b 65 5d 2e 7a 4e 61 6d 65 29 3e 3d 30 20 29  a[e].zName)>=0 )
2cd0c 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
2cd0d 20 72 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a 2f   return 0; .}../
2cd0e 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 6c 69  *.** Return a li
2cd0f 73 74 20 6f 66 20 61 6c 6c 20 74 72 69 67 67 65  st of all trigge
2cd10 72 73 20 6f 6e 20 74 61 62 6c 65 20 70 54 61 62  rs on table pTab
2cd11 20 69 66 20 74 68 65 72 65 20 65 78 69 73 74 73   if there exists
2cd12 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 6f 6e 65   at least.** one
2cd13 20 74 72 69 67 67 65 72 20 74 68 61 74 20 6d 75   trigger that mu
2cd14 73 74 20 62 65 20 66 69 72 65 64 20 77 68 65 6e  st be fired when
2cd15 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66   an operation of
2cd16 20 74 79 70 65 20 27 6f 70 27 20 69 73 20 0a 2a   type 'op' is .*
2cd17 2a 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 74  * performed on t
2cd18 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 2c 20 69  he table, and, i
2cd19 66 20 74 68 61 74 20 6f 70 65 72 61 74 69 6f 6e  f that operation
2cd1a 20 69 73 20 61 6e 20 55 50 44 41 54 45 2c 20 69   is an UPDATE, i
2cd1b 66 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20 6f 6e  f at.** least on
2cd1c 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73  e of the columns
2cd1d 20 69 6e 20 70 43 68 61 6e 67 65 73 20 69 73 20   in pChanges is 
2cd1e 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e 0a  being modified..
2cd1f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2cd20 45 20 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74  E Trigger *sqlit
2cd21 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28  e3TriggersExist(
2cd22 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2cd23 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  ,          /* Pa
2cd24 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  rse context */. 
2cd25 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
2cd26 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2cd27 74 61 62 6c 65 20 74 68 65 20 63 6f 6e 74 61 69  table the contai
2cd28 6e 73 20 74 68 65 20 74 72 69 67 67 65 72 73 20  ns the triggers 
2cd29 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20  */.  int op,    
2cd2a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2cd2b 6f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54 45  one of TK_DELETE
2cd2c 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f  , TK_INSERT, TK_
2cd2d 55 50 44 41 54 45 20 2a 2f 0a 20 20 45 78 70 72  UPDATE */.  Expr
2cd2e 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73 2c 20  List *pChanges, 
2cd2f 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20 74      /* Columns t
2cd30 68 61 74 20 63 68 61 6e 67 65 20 69 6e 20 61 6e  hat change in an
2cd31 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
2cd32 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4d 61 73  t */.  int *pMas
2cd33 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  k              /
2cd34 2a 20 4f 55 54 3a 20 4d 61 73 6b 20 6f 66 20 54  * OUT: Mask of T
2cd35 52 49 47 47 45 52 5f 42 45 46 4f 52 45 7c 54 52  RIGGER_BEFORE|TR
2cd36 49 47 47 45 52 5f 41 46 54 45 52 20 2a 2f 0a 29  IGGER_AFTER */.)
2cd37 7b 0a 20 20 69 6e 74 20 6d 61 73 6b 20 3d 20 30  {.  int mask = 0
2cd38 3b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 4c 69  ;.  Trigger *pLi
2cd39 73 74 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67  st = sqlite3Trig
2cd3a 67 65 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20  gerList(pParse, 
2cd3b 70 54 61 62 29 3b 0a 20 20 54 72 69 67 67 65 72  pTab);.  Trigger
2cd3c 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 70   *p;.  assert( p
2cd3d 4c 69 73 74 3d 3d 30 20 7c 7c 20 49 73 56 69 72  List==0 || IsVir
2cd3e 74 75 61 6c 28 70 54 61 62 29 3d 3d 30 20 29 3b  tual(pTab)==0 );
2cd3f 0a 20 20 66 6f 72 28 70 3d 70 4c 69 73 74 3b 20  .  for(p=pList; 
2cd40 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a  p; p=p->pNext){.
2cd41 20 20 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 6f      if( p->op==o
2cd42 70 20 26 26 20 63 68 65 63 6b 43 6f 6c 75 6d 6e  p && checkColumn
2cd43 4f 76 65 72 6c 61 70 28 70 2d 3e 70 43 6f 6c 75  Overlap(p->pColu
2cd44 6d 6e 73 2c 20 70 43 68 61 6e 67 65 73 29 20 29  mns, pChanges) )
2cd45 7b 0a 20 20 20 20 20 20 6d 61 73 6b 20 7c 3d 20  {.      mask |= 
2cd46 70 2d 3e 74 72 5f 74 6d 3b 0a 20 20 20 20 7d 0a  p->tr_tm;.    }.
2cd47 20 20 7d 0a 20 20 69 66 28 20 70 4d 61 73 6b 20    }.  if( pMask 
2cd48 29 7b 0a 20 20 20 20 2a 70 4d 61 73 6b 20 3d 20  ){.    *pMask = 
2cd49 6d 61 73 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75  mask;.  }.  retu
2cd4a 72 6e 20 28 6d 61 73 6b 20 3f 20 70 4c 69 73 74  rn (mask ? pList
2cd4b 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   : 0);.}../*.** 
2cd4c 43 6f 6e 76 65 72 74 20 74 68 65 20 70 53 74 65  Convert the pSte
2cd4d 70 2d 3e 74 61 72 67 65 74 20 74 6f 6b 65 6e 20  p->target token 
2cd4e 69 6e 74 6f 20 61 20 53 72 63 4c 69 73 74 20 61  into a SrcList a
2cd4f 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e  nd return a poin
2cd50 74 65 72 0a 2a 2a 20 74 6f 20 74 68 61 74 20 53  ter.** to that S
2cd51 72 63 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  rcList..**.** Th
2cd52 69 73 20 72 6f 75 74 69 6e 65 20 61 64 64 73 20  is routine adds 
2cd53 61 20 73 70 65 63 69 66 69 63 20 64 61 74 61 62  a specific datab
2cd54 61 73 65 20 6e 61 6d 65 2c 20 69 66 20 6e 65 65  ase name, if nee
2cd55 64 65 64 2c 20 74 6f 20 74 68 65 20 74 61 72 67  ded, to the targ
2cd56 65 74 20 77 68 65 6e 0a 2a 2a 20 66 6f 72 6d 69  et when.** formi
2cd57 6e 67 20 74 68 65 20 53 72 63 4c 69 73 74 2e 20  ng the SrcList. 
2cd58 20 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61   This prevents a
2cd59 20 74 72 69 67 67 65 72 20 69 6e 20 6f 6e 65 20   trigger in one 
2cd5a 64 61 74 61 62 61 73 65 20 66 72 6f 6d 0a 2a 2a  database from.**
2cd5b 20 72 65 66 65 72 72 69 6e 67 20 74 6f 20 61 20   referring to a 
2cd5c 74 61 72 67 65 74 20 69 6e 20 61 6e 6f 74 68 65  target in anothe
2cd5d 72 20 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20  r database.  An 
2cd5e 65 78 63 65 70 74 69 6f 6e 20 69 73 20 77 68 65  exception is whe
2cd5f 6e 20 74 68 65 0a 2a 2a 20 74 72 69 67 67 65 72  n the.** trigger
2cd60 20 69 73 20 69 6e 20 54 45 4d 50 20 69 6e 20 77   is in TEMP in w
2cd61 68 69 63 68 20 63 61 73 65 20 69 74 20 63 61 6e  hich case it can
2cd62 20 72 65 66 65 72 20 74 6f 20 61 6e 79 20 6f 74   refer to any ot
2cd63 68 65 72 20 64 61 74 61 62 61 73 65 20 69 74 0a  her database it.
2cd64 2a 2a 20 77 61 6e 74 73 2e 0a 2a 2f 0a 73 74 61  ** wants..*/.sta
2cd65 74 69 63 20 53 72 63 4c 69 73 74 20 2a 74 61 72  tic SrcList *tar
2cd66 67 65 74 53 72 63 4c 69 73 74 28 0a 20 20 50 61  getSrcList(.  Pa
2cd67 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2cd68 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e     /* The parsin
2cd69 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  g context */.  T
2cd6a 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65  riggerStep *pSte
2cd6b 70 20 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67  p   /* The trigg
2cd6c 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  er containing th
2cd6d 65 20 74 61 72 67 65 74 20 74 6f 6b 65 6e 20 2a  e target token *
2cd6e 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20  /.){.  int iDb; 
2cd6f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2cd70 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
2cd71 62 61 73 65 20 74 6f 20 75 73 65 20 2a 2f 0a 20  base to use */. 
2cd72 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20   SrcList *pSrc; 
2cd73 20 20 20 20 20 20 2f 2a 20 53 72 63 4c 69 73 74        /* SrcList
2cd74 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20   to be returned 
2cd75 2a 2f 0a 0a 20 20 70 53 72 63 20 3d 20 73 71 6c  */..  pSrc = sql
2cd76 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
2cd77 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 30 2c  d(pParse->db, 0,
2cd78 20 26 70 53 74 65 70 2d 3e 74 61 72 67 65 74 2c   &pStep->target,
2cd79 20 30 29 3b 0a 20 20 69 66 28 20 70 53 72 63 20   0);.  if( pSrc 
2cd7a 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
2cd7b 53 72 63 2d 3e 6e 53 72 63 3e 30 20 29 3b 0a 20  Src->nSrc>0 );. 
2cd7c 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63 2d     assert( pSrc-
2cd7d 3e 61 21 3d 30 20 29 3b 0a 20 20 20 20 69 44 62  >a!=0 );.    iDb
2cd7e 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
2cd7f 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
2cd80 64 62 2c 20 70 53 74 65 70 2d 3e 70 54 72 69 67  db, pStep->pTrig
2cd81 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20  ->pSchema);.    
2cd82 69 66 28 20 69 44 62 3d 3d 30 20 7c 7c 20 69 44  if( iDb==0 || iD
2cd83 62 3e 3d 32 20 29 7b 0a 20 20 20 20 20 20 73 71  b>=2 ){.      sq
2cd84 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2cd85 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 61 73  se->db;.      as
2cd86 73 65 72 74 28 20 69 44 62 3c 70 50 61 72 73 65  sert( iDb<pParse
2cd87 2d 3e 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20  ->db->nDb );.   
2cd88 20 20 20 70 53 72 63 2d 3e 61 5b 70 53 72 63 2d     pSrc->a[pSrc-
2cd89 3e 6e 53 72 63 2d 31 5d 2e 7a 44 61 74 61 62 61  >nSrc-1].zDataba
2cd8a 73 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  se = sqlite3DbSt
2cd8b 72 44 75 70 28 64 62 2c 20 64 62 2d 3e 61 44 62  rDup(db, db->aDb
2cd8c 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20  [iDb].zName);.  
2cd8d 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
2cd8e 20 70 53 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pSrc;.}../*.** 
2cd8f 47 65 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f  Generate VDBE co
2cd90 64 65 20 66 6f 72 20 74 68 65 20 73 74 61 74 65  de for the state
2cd91 6d 65 6e 74 73 20 69 6e 73 69 64 65 20 74 68 65  ments inside the
2cd92 20 62 6f 64 79 20 6f 66 20 61 20 73 69 6e 67 6c   body of a singl
2cd93 65 20 0a 2a 2a 20 74 72 69 67 67 65 72 2e 0a 2a  e .** trigger..*
2cd94 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64  /.static int cod
2cd95 65 54 72 69 67 67 65 72 50 72 6f 67 72 61 6d 28  eTriggerProgram(
2cd96 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2cd97 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
2cd98 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
2cd99 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53  xt */.  TriggerS
2cd9a 74 65 70 20 2a 70 53 74 65 70 4c 69 73 74 2c 20  tep *pStepList, 
2cd9b 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 73 74 61    /* List of sta
2cd9c 74 65 6d 65 6e 74 73 20 69 6e 73 69 64 65 20 74  tements inside t
2cd9d 68 65 20 74 72 69 67 67 65 72 20 62 6f 64 79 20  he trigger body 
2cd9e 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 20  */.  int orconf 
2cd9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2cda0 2a 20 43 6f 6e 66 6c 69 63 74 20 61 6c 67 6f 72  * Conflict algor
2cda1 69 74 68 6d 2e 20 28 4f 45 5f 41 62 6f 72 74 2c  ithm. (OE_Abort,
2cda2 20 65 74 63 29 20 2a 2f 20 20 0a 29 7b 0a 20 20   etc) */  .){.  
2cda3 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74  TriggerStep *pSt
2cda4 65 70 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  ep;.  Vdbe *v = 
2cda5 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
2cda6 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2cda7 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 61 73  Parse->db;..  as
2cda8 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 54  sert( pParse->pT
2cda9 72 69 67 67 65 72 54 61 62 20 26 26 20 70 50 61  riggerTab && pPa
2cdaa 72 73 65 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 29  rse->pToplevel )
2cdab 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 74 65  ;.  assert( pSte
2cdac 70 4c 69 73 74 20 29 3b 0a 20 20 61 73 73 65 72  pList );.  asser
2cdad 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 66 6f 72  t( v!=0 );.  for
2cdae 28 70 53 74 65 70 3d 70 53 74 65 70 4c 69 73 74  (pStep=pStepList
2cdaf 3b 20 70 53 74 65 70 3b 20 70 53 74 65 70 3d 70  ; pStep; pStep=p
2cdb0 53 74 65 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  Step->pNext){.  
2cdb1 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
2cdb2 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20  the ON CONFLICT 
2cdb3 70 6f 6c 69 63 79 20 74 68 61 74 20 77 69 6c 6c  policy that will
2cdb4 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 69   be used for thi
2cdb5 73 20 73 74 65 70 0a 20 20 20 20 2a 2a 20 6f 66  s step.    ** of
2cdb6 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f   the trigger pro
2cdb7 67 72 61 6d 2e 20 49 66 20 74 68 65 20 73 74 61  gram. If the sta
2cdb8 74 65 6d 65 6e 74 20 74 68 61 74 20 63 61 75 73  tement that caus
2cdb9 65 64 20 74 68 69 73 20 74 72 69 67 67 65 72 0a  ed this trigger.
2cdba 20 20 20 20 2a 2a 20 74 6f 20 66 69 72 65 20 68      ** to fire h
2cdbb 61 64 20 61 6e 20 65 78 70 6c 69 63 69 74 20 4f  ad an explicit O
2cdbc 4e 20 43 4f 4e 46 4c 49 43 54 2c 20 74 68 65 6e  N CONFLICT, then
2cdbd 20 75 73 65 20 69 74 2e 20 4f 74 68 65 72 77 69   use it. Otherwi
2cdbe 73 65 2c 20 75 73 65 0a 20 20 20 20 2a 2a 20 74  se, use.    ** t
2cdbf 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70  he ON CONFLICT p
2cdc0 6f 6c 69 63 79 20 74 68 61 74 20 77 61 73 20 73  olicy that was s
2cdc1 70 65 63 69 66 69 65 64 20 61 73 20 70 61 72 74  pecified as part
2cdc2 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 0a   of the trigger.
2cdc3 20 20 20 20 2a 2a 20 73 74 65 70 20 73 74 61 74      ** step stat
2cdc4 65 6d 65 6e 74 2e 20 45 78 61 6d 70 6c 65 3a 0a  ement. Example:.
2cdc5 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2cdc6 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20 41  CREATE TRIGGER A
2cdc7 46 54 45 52 20 49 4e 53 45 52 54 20 4f 4e 20 74  FTER INSERT ON t
2cdc8 31 20 42 45 47 49 4e 3b 0a 20 20 20 20 2a 2a 20  1 BEGIN;.    ** 
2cdc9 20 20 20 20 49 4e 53 45 52 54 20 4f 52 20 52 45      INSERT OR RE
2cdca 50 4c 41 43 45 20 49 4e 54 4f 20 74 32 20 56 41  PLACE INTO t2 VA
2cdcb 4c 55 45 53 28 6e 65 77 2e 61 2c 20 6e 65 77 2e  LUES(new.a, new.
2cdcc 62 29 3b 0a 20 20 20 20 2a 2a 20 20 20 45 4e 44  b);.    **   END
2cdcd 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ;.    **.    ** 
2cdce 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
2cdcf 20 2e 2e 2e 20 3b 20 20 20 20 20 20 20 20 20 20   ... ;          
2cdd0 20 20 2d 2d 20 69 6e 73 65 72 74 20 69 6e 74 6f    -- insert into
2cdd1 20 74 32 20 75 73 65 73 20 52 45 50 4c 41 43 45   t2 uses REPLACE
2cdd2 20 70 6f 6c 69 63 79 0a 20 20 20 20 2a 2a 20 20   policy.    **  
2cdd3 20 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52   INSERT OR IGNOR
2cdd4 45 20 49 4e 54 4f 20 74 31 20 2e 2e 2e 20 3b 20  E INTO t1 ... ; 
2cdd5 20 2d 2d 20 69 6e 73 65 72 74 20 69 6e 74 6f 20   -- insert into 
2cdd6 74 32 20 75 73 65 73 20 49 47 4e 4f 52 45 20 70  t2 uses IGNORE p
2cdd7 6f 6c 69 63 79 0a 20 20 20 20 2a 2f 0a 20 20 20  olicy.    */.   
2cdd8 20 70 50 61 72 73 65 2d 3e 65 4f 72 63 6f 6e 66   pParse->eOrconf
2cdd9 20 3d 20 28 6f 72 63 6f 6e 66 3d 3d 4f 45 5f 44   = (orconf==OE_D
2cdda 65 66 61 75 6c 74 29 3f 70 53 74 65 70 2d 3e 6f  efault)?pStep->o
2cddb 72 63 6f 6e 66 3a 28 75 38 29 6f 72 63 6f 6e 66  rconf:(u8)orconf
2cddc 3b 0a 0a 20 20 20 20 73 77 69 74 63 68 28 20 70  ;..    switch( p
2cddd 53 74 65 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20  Step->op ){.    
2cdde 20 20 63 61 73 65 20 54 4b 5f 55 50 44 41 54 45    case TK_UPDATE
2cddf 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  : {.        sqli
2cde0 74 65 33 55 70 64 61 74 65 28 70 50 61 72 73 65  te3Update(pParse
2cde1 2c 20 0a 20 20 20 20 20 20 20 20 20 20 74 61 72  , .          tar
2cde2 67 65 74 53 72 63 4c 69 73 74 28 70 50 61 72 73  getSrcList(pPars
2cde3 65 2c 20 70 53 74 65 70 29 2c 0a 20 20 20 20 20  e, pStep),.     
2cde4 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2cde5 4c 69 73 74 44 75 70 28 64 62 2c 20 70 53 74 65  ListDup(db, pSte
2cde6 70 2d 3e 70 45 78 70 72 4c 69 73 74 2c 20 30 29  p->pExprList, 0)
2cde7 2c 20 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  , .          sql
2cde8 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
2cde9 70 53 74 65 70 2d 3e 70 57 68 65 72 65 2c 20 30  pStep->pWhere, 0
2cdea 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20 70 50  ), .          pP
2cdeb 61 72 73 65 2d 3e 65 4f 72 63 6f 6e 66 0a 20 20  arse->eOrconf.  
2cdec 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20        );.       
2cded 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
2cdee 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e        case TK_IN
2cdef 53 45 52 54 3a 20 7b 0a 20 20 20 20 20 20 20 20  SERT: {.        
2cdf0 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 70 50  sqlite3Insert(pP
2cdf1 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20  arse, .         
2cdf2 20 74 61 72 67 65 74 53 72 63 4c 69 73 74 28 70   targetSrcList(p
2cdf3 50 61 72 73 65 2c 20 70 53 74 65 70 29 2c 0a 20  Parse, pStep),. 
2cdf4 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2cdf5 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20  ExprListDup(db, 
2cdf6 70 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74  pStep->pExprList
2cdf7 2c 20 30 29 2c 20 0a 20 20 20 20 20 20 20 20 20  , 0), .         
2cdf8 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
2cdf9 70 28 64 62 2c 20 70 53 74 65 70 2d 3e 70 53 65  p(db, pStep->pSe
2cdfa 6c 65 63 74 2c 20 30 29 2c 20 0a 20 20 20 20 20  lect, 0), .     
2cdfb 20 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69       sqlite3IdLi
2cdfc 73 74 44 75 70 28 64 62 2c 20 70 53 74 65 70 2d  stDup(db, pStep-
2cdfd 3e 70 49 64 4c 69 73 74 29 2c 20 0a 20 20 20 20  >pIdList), .    
2cdfe 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 65 4f        pParse->eO
2cdff 72 63 6f 6e 66 0a 20 20 20 20 20 20 20 20 29 3b  rconf.        );
2ce00 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2ce01 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
2ce02 73 65 20 54 4b 5f 44 45 4c 45 54 45 3a 20 7b 0a  se TK_DELETE: {.
2ce03 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
2ce04 65 6c 65 74 65 46 72 6f 6d 28 70 50 61 72 73 65  eleteFrom(pParse
2ce05 2c 20 0a 20 20 20 20 20 20 20 20 20 20 74 61 72  , .          tar
2ce06 67 65 74 53 72 63 4c 69 73 74 28 70 50 61 72 73  getSrcList(pPars
2ce07 65 2c 20 70 53 74 65 70 29 2c 0a 20 20 20 20 20  e, pStep),.     
2ce08 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2ce09 44 75 70 28 64 62 2c 20 70 53 74 65 70 2d 3e 70  Dup(db, pStep->p
2ce0a 57 68 65 72 65 2c 20 30 29 0a 20 20 20 20 20 20  Where, 0).      
2ce0b 20 20 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65    );.        bre
2ce0c 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2ce0d 20 20 64 65 66 61 75 6c 74 3a 20 61 73 73 65 72    default: asser
2ce0e 74 28 20 70 53 74 65 70 2d 3e 6f 70 3d 3d 54 4b  t( pStep->op==TK
2ce0f 5f 53 45 4c 45 43 54 20 29 3b 20 7b 0a 20 20 20  _SELECT ); {.   
2ce10 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74 20       SelectDest 
2ce11 73 44 65 73 74 3b 0a 20 20 20 20 20 20 20 20 53  sDest;.        S
2ce12 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 3d  elect *pSelect =
2ce13 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
2ce14 70 28 64 62 2c 20 70 53 74 65 70 2d 3e 70 53 65  p(db, pStep->pSe
2ce15 6c 65 63 74 2c 20 30 29 3b 0a 20 20 20 20 20 20  lect, 0);.      
2ce16 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2ce17 65 73 74 49 6e 69 74 28 26 73 44 65 73 74 2c 20  estInit(&sDest, 
2ce18 53 52 54 5f 44 69 73 63 61 72 64 2c 20 30 29 3b  SRT_Discard, 0);
2ce19 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2ce1a 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
2ce1b 53 65 6c 65 63 74 2c 20 26 73 44 65 73 74 29 3b  Select, &sDest);
2ce1c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2ce1d 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
2ce1e 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20   pSelect);.     
2ce1f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2ce20 7d 0a 20 20 20 20 7d 20 0a 20 20 20 20 69 66 28  }.    } .    if(
2ce21 20 70 53 74 65 70 2d 3e 6f 70 21 3d 54 4b 5f 53   pStep->op!=TK_S
2ce22 45 4c 45 43 54 20 29 7b 0a 20 20 20 20 20 20 73  ELECT ){.      s
2ce23 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30  qlite3VdbeAddOp0
2ce24 28 76 2c 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e  (v, OP_ResetCoun
2ce25 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  t);.    }.  }.. 
2ce26 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 23 69   return 0;.}..#i
2ce27 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
2ce28 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  G./*.** This fun
2ce29 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f  ction is used to
2ce2a 20 61 64 64 20 56 64 62 65 43 6f 6d 6d 65 6e 74   add VdbeComment
2ce2b 28 29 20 61 6e 6e 6f 74 61 74 69 6f 6e 73 20 74  () annotations t
2ce2c 6f 20 61 20 56 44 42 45 0a 2a 2a 20 70 72 6f 67  o a VDBE.** prog
2ce2d 72 61 6d 2e 20 49 74 20 69 73 20 6e 6f 74 20 75  ram. It is not u
2ce2e 73 65 64 20 69 6e 20 70 72 6f 64 75 63 74 69 6f  sed in productio
2ce2f 6e 20 63 6f 64 65 2c 20 6f 6e 6c 79 20 66 6f 72  n code, only for
2ce30 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2f 0a 73   debugging..*/.s
2ce31 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
2ce32 20 2a 6f 6e 45 72 72 6f 72 54 65 78 74 28 69 6e   *onErrorText(in
2ce33 74 20 6f 6e 45 72 72 6f 72 29 7b 0a 20 20 73 77  t onError){.  sw
2ce34 69 74 63 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b  itch( onError ){
2ce35 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 41 62 6f  .    case OE_Abo
2ce36 72 74 3a 20 20 20 20 72 65 74 75 72 6e 20 22 61  rt:    return "a
2ce37 62 6f 72 74 22 3b 0a 20 20 20 20 63 61 73 65 20  bort";.    case 
2ce38 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 20 72 65 74  OE_Rollback: ret
2ce39 75 72 6e 20 22 72 6f 6c 6c 62 61 63 6b 22 3b 0a  urn "rollback";.
2ce3a 20 20 20 20 63 61 73 65 20 4f 45 5f 46 61 69 6c      case OE_Fail
2ce3b 3a 20 20 20 20 20 72 65 74 75 72 6e 20 22 66 61  :     return "fa
2ce3c 69 6c 22 3b 0a 20 20 20 20 63 61 73 65 20 4f 45  il";.    case OE
2ce3d 5f 52 65 70 6c 61 63 65 3a 20 20 72 65 74 75 72  _Replace:  retur
2ce3e 6e 20 22 72 65 70 6c 61 63 65 22 3b 0a 20 20 20  n "replace";.   
2ce3f 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65 3a   case OE_Ignore:
2ce40 20 20 20 72 65 74 75 72 6e 20 22 69 67 6e 6f 72     return "ignor
2ce41 65 22 3b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f  e";.    case OE_
2ce42 44 65 66 61 75 6c 74 3a 20 20 72 65 74 75 72 6e  Default:  return
2ce43 20 22 64 65 66 61 75 6c 74 22 3b 0a 20 20 7d 0a   "default";.  }.
2ce44 20 20 72 65 74 75 72 6e 20 22 6e 2f 61 22 3b 0a    return "n/a";.
2ce45 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
2ce46 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 73 74  Parse context st
2ce47 72 75 63 74 75 72 65 20 70 46 72 6f 6d 20 68 61  ructure pFrom ha
2ce48 73 20 6a 75 73 74 20 62 65 65 6e 20 75 73 65 64  s just been used
2ce49 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 75 62   to create a sub
2ce4a 2d 76 64 62 65 0a 2a 2a 20 28 74 72 69 67 67 65  -vdbe.** (trigge
2ce4b 72 20 70 72 6f 67 72 61 6d 29 2e 20 49 66 20 61  r program). If a
2ce4c 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75  n error has occu
2ce4d 72 72 65 64 2c 20 74 72 61 6e 73 66 65 72 20 65  rred, transfer e
2ce4e 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  rror information
2ce4f 0a 2a 2a 20 66 72 6f 6d 20 70 46 72 6f 6d 20 74  .** from pFrom t
2ce50 6f 20 70 54 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63  o pTo..*/.static
2ce51 20 76 6f 69 64 20 74 72 61 6e 73 66 65 72 50 61   void transferPa
2ce52 72 73 65 45 72 72 6f 72 28 50 61 72 73 65 20 2a  rseError(Parse *
2ce53 70 54 6f 2c 20 50 61 72 73 65 20 2a 70 46 72 6f  pTo, Parse *pFro
2ce54 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 46  m){.  assert( pF
2ce55 72 6f 6d 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20  rom->zErrMsg==0 
2ce56 7c 7c 20 70 46 72 6f 6d 2d 3e 6e 45 72 72 20 29  || pFrom->nErr )
2ce57 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 2d  ;.  assert( pTo-
2ce58 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 7c 7c 20 70  >zErrMsg==0 || p
2ce59 54 6f 2d 3e 6e 45 72 72 20 29 3b 0a 20 20 69 66  To->nErr );.  if
2ce5a 28 20 70 54 6f 2d 3e 6e 45 72 72 3d 3d 30 20 29  ( pTo->nErr==0 )
2ce5b 7b 0a 20 20 20 20 70 54 6f 2d 3e 7a 45 72 72 4d  {.    pTo->zErrM
2ce5c 73 67 20 3d 20 70 46 72 6f 6d 2d 3e 7a 45 72 72  sg = pFrom->zErr
2ce5d 4d 73 67 3b 0a 20 20 20 20 70 54 6f 2d 3e 6e 45  Msg;.    pTo->nE
2ce5e 72 72 20 3d 20 70 46 72 6f 6d 2d 3e 6e 45 72 72  rr = pFrom->nErr
2ce5f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
2ce60 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 46 72  qlite3DbFree(pFr
2ce61 6f 6d 2d 3e 64 62 2c 20 70 46 72 6f 6d 2d 3e 7a  om->db, pFrom->z
2ce62 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 0a  ErrMsg);.  }.}..
2ce63 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 6e 64  /*.** Create and
2ce64 20 70 6f 70 75 6c 61 74 65 20 61 20 6e 65 77 20   populate a new 
2ce65 54 72 69 67 67 65 72 50 72 67 20 6f 62 6a 65 63  TriggerPrg objec
2ce66 74 20 77 69 74 68 20 61 20 73 75 62 2d 70 72 6f  t with a sub-pro
2ce67 67 72 61 6d 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65  gram .** impleme
2ce68 6e 74 69 6e 67 20 74 72 69 67 67 65 72 20 70 54  nting trigger pT
2ce69 72 69 67 67 65 72 20 77 69 74 68 20 4f 4e 20 43  rigger with ON C
2ce6a 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 6f  ONFLICT policy o
2ce6b 72 63 6f 6e 66 2e 0a 2a 2f 0a 73 74 61 74 69 63  rconf..*/.static
2ce6c 20 54 72 69 67 67 65 72 50 72 67 20 2a 63 6f 64   TriggerPrg *cod
2ce6d 65 52 6f 77 54 72 69 67 67 65 72 28 0a 20 20 50  eRowTrigger(.  P
2ce6e 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2ce6f 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 70      /* Current p
2ce70 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  arse context */.
2ce71 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
2ce72 67 65 72 2c 20 20 20 2f 2a 20 54 72 69 67 67 65  ger,   /* Trigge
2ce73 72 20 74 6f 20 63 6f 64 65 20 2a 2f 0a 20 20 54  r to code */.  T
2ce74 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
2ce75 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
2ce76 20 70 54 72 69 67 67 65 72 20 69 73 20 61 74 74   pTrigger is att
2ce77 61 63 68 65 64 20 74 6f 20 2a 2f 0a 20 20 69 6e  ached to */.  in
2ce78 74 20 6f 72 63 6f 6e 66 20 20 20 20 20 20 20 20  t orconf        
2ce79 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43     /* ON CONFLIC
2ce7a 54 20 70 6f 6c 69 63 79 20 74 6f 20 63 6f 64 65  T policy to code
2ce7b 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
2ce7c 20 77 69 74 68 20 2a 2f 0a 29 7b 0a 20 20 50 61   with */.){.  Pa
2ce7d 72 73 65 20 2a 70 54 6f 70 20 3d 20 73 71 6c 69  rse *pTop = sqli
2ce7e 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c  te3ParseToplevel
2ce7f 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69  (pParse);.  sqli
2ce80 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2ce81 2d 3e 64 62 3b 20 20 20 2f 2a 20 44 61 74 61 62  ->db;   /* Datab
2ce82 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ase handle */.  
2ce83 54 72 69 67 67 65 72 50 72 67 20 2a 70 50 72 67  TriggerPrg *pPrg
2ce84 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56  ;           /* V
2ce85 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a  alue to return *
2ce86 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 6e 20  /.  Expr *pWhen 
2ce87 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
2ce88 2f 2a 20 44 75 70 6c 69 63 61 74 65 20 6f 66 20  /* Duplicate of 
2ce89 74 72 69 67 67 65 72 20 57 48 45 4e 20 65 78 70  trigger WHEN exp
2ce8a 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 56 64 62  ression */.  Vdb
2ce8b 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20  e *v;           
2ce8c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70           /* Temp
2ce8d 6f 72 61 72 79 20 56 4d 20 2a 2f 0a 20 20 4e 61  orary VM */.  Na
2ce8e 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20  meContext sNC;  
2ce8f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
2ce90 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 73 75  e context for su
2ce91 62 2d 76 64 62 65 20 2a 2f 0a 20 20 53 75 62 50  b-vdbe */.  SubP
2ce92 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d  rogram *pProgram
2ce93 20 3d 20 30 3b 20 20 20 2f 2a 20 53 75 62 2d 76   = 0;   /* Sub-v
2ce94 64 62 65 20 66 6f 72 20 74 72 69 67 67 65 72 20  dbe for trigger 
2ce95 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 50 61 72  program */.  Par
2ce96 73 65 20 2a 70 53 75 62 50 61 72 73 65 3b 20 20  se *pSubParse;  
2ce97 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
2ce98 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 73 75  e context for su
2ce99 62 2d 76 64 62 65 20 2a 2f 0a 20 20 69 6e 74 20  b-vdbe */.  int 
2ce9a 69 45 6e 64 54 72 69 67 67 65 72 20 3d 20 30 3b  iEndTrigger = 0;
2ce9b 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c          /* Label
2ce9c 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66 20 57   to jump to if W
2ce9d 48 45 4e 20 69 73 20 66 61 6c 73 65 20 2a 2f 0a  HEN is false */.
2ce9e 0a 20 20 61 73 73 65 72 74 28 20 70 54 72 69 67  .  assert( pTrig
2ce9f 67 65 72 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 7c 7c  ger->zName==0 ||
2cea0 20 70 54 61 62 3d 3d 74 61 62 6c 65 4f 66 54 72   pTab==tableOfTr
2cea1 69 67 67 65 72 28 70 54 72 69 67 67 65 72 29 20  igger(pTrigger) 
2cea2 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  );..  /* Allocat
2cea3 65 20 74 68 65 20 54 72 69 67 67 65 72 50 72 67  e the TriggerPrg
2cea4 20 61 6e 64 20 53 75 62 50 72 6f 67 72 61 6d 20   and SubProgram 
2cea5 6f 62 6a 65 63 74 73 2e 20 54 6f 20 65 6e 73 75  objects. To ensu
2cea6 72 65 20 74 68 61 74 20 74 68 65 79 0a 20 20 2a  re that they.  *
2cea7 2a 20 61 72 65 20 66 72 65 65 64 20 69 66 20 61  * are freed if a
2cea8 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
2cea9 6c 69 6e 6b 20 74 68 65 6d 20 69 6e 74 6f 20 74  link them into t
2ceaa 68 65 20 50 61 72 73 65 2e 70 54 72 69 67 67 65  he Parse.pTrigge
2ceab 72 50 72 67 20 0a 20 20 2a 2a 20 6c 69 73 74 20  rPrg .  ** list 
2ceac 6f 66 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c  of the top-level
2cead 20 50 61 72 73 65 20 6f 62 6a 65 63 74 20 73 6f   Parse object so
2ceae 6f 6e 65 72 20 72 61 74 68 65 72 20 74 68 61 6e  oner rather than
2ceaf 20 6c 61 74 65 72 2e 20 20 2a 2f 0a 20 20 70 50   later.  */.  pP
2ceb0 72 67 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  rg = sqlite3DbMa
2ceb1 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
2ceb2 65 6f 66 28 54 72 69 67 67 65 72 50 72 67 29 29  eof(TriggerPrg))
2ceb3 3b 0a 20 20 69 66 28 20 21 70 50 72 67 20 29 20  ;.  if( !pPrg ) 
2ceb4 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 50 72 67  return 0;.  pPrg
2ceb5 2d 3e 70 4e 65 78 74 20 3d 20 70 54 6f 70 2d 3e  ->pNext = pTop->
2ceb6 70 54 72 69 67 67 65 72 50 72 67 3b 0a 20 20 70  pTriggerPrg;.  p
2ceb7 54 6f 70 2d 3e 70 54 72 69 67 67 65 72 50 72 67  Top->pTriggerPrg
2ceb8 20 3d 20 70 50 72 67 3b 0a 20 20 70 50 72 67 2d   = pPrg;.  pPrg-
2ceb9 3e 70 50 72 6f 67 72 61 6d 20 3d 20 70 50 72 6f  >pProgram = pPro
2ceba 67 72 61 6d 20 3d 20 73 71 6c 69 74 65 33 44 62  gram = sqlite3Db
2cebb 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
2cebc 69 7a 65 6f 66 28 53 75 62 50 72 6f 67 72 61 6d  izeof(SubProgram
2cebd 29 29 3b 0a 20 20 69 66 28 20 21 70 50 72 6f 67  ));.  if( !pProg
2cebe 72 61 6d 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ram ) return 0;.
2cebf 20 20 70 50 72 6f 67 72 61 6d 2d 3e 6e 52 65 66    pProgram->nRef
2cec0 20 3d 20 31 3b 0a 20 20 70 50 72 67 2d 3e 70 54   = 1;.  pPrg->pT
2cec1 72 69 67 67 65 72 20 3d 20 70 54 72 69 67 67 65  rigger = pTrigge
2cec2 72 3b 0a 20 20 70 50 72 67 2d 3e 6f 72 63 6f 6e  r;.  pPrg->orcon
2cec3 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20 20 70 50  f = orconf;.  pP
2cec4 72 67 2d 3e 6f 6c 64 6d 61 73 6b 20 3d 20 30 78  rg->oldmask = 0x
2cec5 66 66 66 66 66 66 66 66 3b 0a 0a 20 20 2f 2a 20  ffffffff;..  /* 
2cec6 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 70 6f 70  Allocate and pop
2cec7 75 6c 61 74 65 20 61 20 6e 65 77 20 50 61 72 73  ulate a new Pars
2cec8 65 20 63 6f 6e 74 65 78 74 20 74 6f 20 75 73 65  e context to use
2cec9 20 66 6f 72 20 63 6f 64 69 6e 67 20 74 68 65 20   for coding the 
2ceca 0a 20 20 2a 2a 20 74 72 69 67 67 65 72 20 73 75  .  ** trigger su
2cecb 62 2d 70 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20  b-program.  */. 
2cecc 20 70 53 75 62 50 61 72 73 65 20 3d 20 73 71 6c   pSubParse = sql
2cecd 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65  ite3StackAllocZe
2cece 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 50 61  ro(db, sizeof(Pa
2cecf 72 73 65 29 29 3b 0a 20 20 69 66 28 20 21 70 53  rse));.  if( !pS
2ced0 75 62 50 61 72 73 65 20 29 20 72 65 74 75 72 6e  ubParse ) return
2ced1 20 30 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e   0;.  memset(&sN
2ced2 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43  C, 0, sizeof(sNC
2ced3 29 29 3b 0a 20 20 73 4e 43 2e 70 50 61 72 73 65  ));.  sNC.pParse
2ced4 20 3d 20 70 53 75 62 50 61 72 73 65 3b 0a 20 20   = pSubParse;.  
2ced5 70 53 75 62 50 61 72 73 65 2d 3e 64 62 20 3d 20  pSubParse->db = 
2ced6 64 62 3b 0a 20 20 70 53 75 62 50 61 72 73 65 2d  db;.  pSubParse-
2ced7 3e 70 54 72 69 67 67 65 72 54 61 62 20 3d 20 70  >pTriggerTab = p
2ced8 54 61 62 3b 0a 20 20 70 53 75 62 50 61 72 73 65  Tab;.  pSubParse
2ced9 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 3d 20 70 54  ->pToplevel = pT
2ceda 6f 70 3b 0a 20 20 70 53 75 62 50 61 72 73 65 2d  op;.  pSubParse-
2cedb 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20  >zAuthContext = 
2cedc 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 3b  pTrigger->zName;
2cedd 0a 20 20 70 53 75 62 50 61 72 73 65 2d 3e 65 54  .  pSubParse->eT
2cede 72 69 67 67 65 72 4f 70 20 3d 20 70 54 72 69 67  riggerOp = pTrig
2cedf 67 65 72 2d 3e 6f 70 3b 0a 0a 20 20 76 20 3d 20  ger->op;..  v = 
2cee0 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2cee1 53 75 62 50 61 72 73 65 29 3b 0a 20 20 69 66 28  SubParse);.  if(
2cee2 20 76 20 29 7b 0a 20 20 20 20 56 64 62 65 43 6f   v ){.    VdbeCo
2cee3 6d 6d 65 6e 74 28 28 76 2c 20 22 53 74 61 72 74  mment((v, "Start
2cee4 3a 20 25 73 2e 25 73 20 28 25 73 20 25 73 25 73  : %s.%s (%s %s%s
2cee5 25 73 20 4f 4e 20 25 73 29 22 2c 20 0a 20 20 20  %s ON %s)", .   
2cee6 20 20 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61     pTrigger->zNa
2cee7 6d 65 2c 20 6f 6e 45 72 72 6f 72 54 65 78 74 28  me, onErrorText(
2cee8 6f 72 63 6f 6e 66 29 2c 0a 20 20 20 20 20 20 28  orconf),.      (
2cee9 70 54 72 69 67 67 65 72 2d 3e 74 72 5f 74 6d 3d  pTrigger->tr_tm=
2ceea 3d 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20  =TRIGGER_BEFORE 
2ceeb 3f 20 22 42 45 46 4f 52 45 22 20 3a 20 22 41 46  ? "BEFORE" : "AF
2ceec 54 45 52 22 29 2c 0a 20 20 20 20 20 20 20 20 28  TER"),.        (
2ceed 70 54 72 69 67 67 65 72 2d 3e 6f 70 3d 3d 54 4b  pTrigger->op==TK
2ceee 5f 55 50 44 41 54 45 20 3f 20 22 55 50 44 41 54  _UPDATE ? "UPDAT
2ceef 45 22 20 3a 20 22 22 29 2c 0a 20 20 20 20 20 20  E" : ""),.      
2cef0 20 20 28 70 54 72 69 67 67 65 72 2d 3e 6f 70 3d    (pTrigger->op=
2cef1 3d 54 4b 5f 49 4e 53 45 52 54 20 3f 20 22 49 4e  =TK_INSERT ? "IN
2cef2 53 45 52 54 22 20 3a 20 22 22 29 2c 0a 20 20 20  SERT" : ""),.   
2cef3 20 20 20 20 20 28 70 54 72 69 67 67 65 72 2d 3e       (pTrigger->
2cef4 6f 70 3d 3d 54 4b 5f 44 45 4c 45 54 45 20 3f 20  op==TK_DELETE ? 
2cef5 22 44 45 4c 45 54 45 22 20 3a 20 22 22 29 2c 0a  "DELETE" : ""),.
2cef6 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d        pTab->zNam
2cef7 65 0a 20 20 20 20 29 29 3b 0a 23 69 66 6e 64 65  e.    ));.#ifnde
2cef8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
2cef9 41 43 45 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ACE.    sqlite3V
2cefa 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
2cefb 31 2c 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1, .      sqlite
2cefc 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 2d 2d  3MPrintf(db, "--
2cefd 20 54 52 49 47 47 45 52 20 25 73 22 2c 20 70 54   TRIGGER %s", pT
2cefe 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 29 2c 20  rigger->zName), 
2ceff 50 34 5f 44 59 4e 41 4d 49 43 0a 20 20 20 20 29  P4_DYNAMIC.    )
2cf00 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a  ;.#endif..    /*
2cf01 20 49 66 20 6f 6e 65 20 77 61 73 20 73 70 65 63   If one was spec
2cf02 69 66 69 65 64 2c 20 63 6f 64 65 20 74 68 65 20  ified, code the 
2cf03 57 48 45 4e 20 63 6c 61 75 73 65 2e 20 49 66 20  WHEN clause. If 
2cf04 69 74 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20  it evaluates to 
2cf05 66 61 6c 73 65 0a 20 20 20 20 2a 2a 20 28 6f 72  false.    ** (or
2cf06 20 4e 55 4c 4c 29 20 74 68 65 20 73 75 62 2d 76   NULL) the sub-v
2cf07 64 62 65 20 69 73 20 69 6d 6d 65 64 69 61 74 65  dbe is immediate
2cf08 6c 79 20 68 61 6c 74 65 64 20 62 79 20 6a 75 6d  ly halted by jum
2cf09 70 69 6e 67 20 74 6f 20 74 68 65 20 0a 20 20 20  ping to the .   
2cf0a 20 2a 2a 20 4f 50 5f 48 61 6c 74 20 69 6e 73 65   ** OP_Halt inse
2cf0b 72 74 65 64 20 61 74 20 74 68 65 20 65 6e 64 20  rted at the end 
2cf0c 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20  of the program. 
2cf0d 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 72 69   */.    if( pTri
2cf0e 67 67 65 72 2d 3e 70 57 68 65 6e 20 29 7b 0a 20  gger->pWhen ){. 
2cf0f 20 20 20 20 20 70 57 68 65 6e 20 3d 20 73 71 6c       pWhen = sql
2cf10 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
2cf11 70 54 72 69 67 67 65 72 2d 3e 70 57 68 65 6e 2c  pTrigger->pWhen,
2cf12 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53   0);.      if( S
2cf13 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65  QLITE_OK==sqlite
2cf14 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
2cf15 73 28 26 73 4e 43 2c 20 70 57 68 65 6e 29 20 0a  s(&sNC, pWhen) .
2cf16 20 20 20 20 20 20 20 26 26 20 64 62 2d 3e 6d 61         && db->ma
2cf17 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 0a 20  llocFailed==0 . 
2cf18 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
2cf19 69 45 6e 64 54 72 69 67 67 65 72 20 3d 20 73 71  iEndTrigger = sq
2cf1a 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
2cf1b 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20 73  el(v);.        s
2cf1c 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
2cf1d 65 28 70 53 75 62 50 61 72 73 65 2c 20 70 57 68  e(pSubParse, pWh
2cf1e 65 6e 2c 20 69 45 6e 64 54 72 69 67 67 65 72 2c  en, iEndTrigger,
2cf1f 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
2cf20 4c 4c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  LL);.      }.   
2cf21 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
2cf22 6c 65 74 65 28 64 62 2c 20 70 57 68 65 6e 29 3b  lete(db, pWhen);
2cf23 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
2cf24 6f 64 65 20 74 68 65 20 74 72 69 67 67 65 72 20  ode the trigger 
2cf25 70 72 6f 67 72 61 6d 20 69 6e 74 6f 20 74 68 65  program into the
2cf26 20 73 75 62 2d 76 64 62 65 2e 20 2a 2f 0a 20 20   sub-vdbe. */.  
2cf27 20 20 63 6f 64 65 54 72 69 67 67 65 72 50 72 6f    codeTriggerPro
2cf28 67 72 61 6d 28 70 53 75 62 50 61 72 73 65 2c 20  gram(pSubParse, 
2cf29 70 54 72 69 67 67 65 72 2d 3e 73 74 65 70 5f 6c  pTrigger->step_l
2cf2a 69 73 74 2c 20 6f 72 63 6f 6e 66 29 3b 0a 0a 20  ist, orconf);.. 
2cf2b 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20     /* Insert an 
2cf2c 4f 50 5f 48 61 6c 74 20 61 74 20 74 68 65 20 65  OP_Halt at the e
2cf2d 6e 64 20 6f 66 20 74 68 65 20 73 75 62 2d 70 72  nd of the sub-pr
2cf2e 6f 67 72 61 6d 2e 20 2a 2f 0a 20 20 20 20 69 66  ogram. */.    if
2cf2f 28 20 69 45 6e 64 54 72 69 67 67 65 72 20 29 7b  ( iEndTrigger ){
2cf30 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2cf31 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
2cf32 2c 20 69 45 6e 64 54 72 69 67 67 65 72 29 3b 0a  , iEndTrigger);.
2cf33 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2cf34 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f  3VdbeAddOp0(v, O
2cf35 50 5f 48 61 6c 74 29 3b 0a 20 20 20 20 56 64 62  P_Halt);.    Vdb
2cf36 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 45 6e  eComment((v, "En
2cf37 64 3a 20 25 73 2e 25 73 22 2c 20 70 54 72 69 67  d: %s.%s", pTrig
2cf38 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20 6f 6e 45 72  ger->zName, onEr
2cf39 72 6f 72 54 65 78 74 28 6f 72 63 6f 6e 66 29 29  rorText(orconf))
2cf3a 29 3b 0a 0a 20 20 20 20 74 72 61 6e 73 66 65 72  );..    transfer
2cf3b 50 61 72 73 65 45 72 72 6f 72 28 70 50 61 72 73  ParseError(pPars
2cf3c 65 2c 20 70 53 75 62 50 61 72 73 65 29 3b 0a 20  e, pSubParse);. 
2cf3d 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f     if( db->mallo
2cf3e 63 46 61 69 6c 65 64 3d 3d 30 20 29 7b 0a 20 20  cFailed==0 ){.  
2cf3f 20 20 20 20 70 50 72 6f 67 72 61 6d 2d 3e 61 4f      pProgram->aO
2cf40 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 54  p = sqlite3VdbeT
2cf41 61 6b 65 4f 70 41 72 72 61 79 28 76 2c 20 26 70  akeOpArray(v, &p
2cf42 50 72 6f 67 72 61 6d 2d 3e 6e 4f 70 2c 20 26 70  Program->nOp, &p
2cf43 54 6f 70 2d 3e 6e 4d 61 78 41 72 67 29 3b 0a 20  Top->nMaxArg);. 
2cf44 20 20 20 7d 0a 20 20 20 20 70 50 72 6f 67 72 61     }.    pProgra
2cf45 6d 2d 3e 6e 4d 65 6d 20 3d 20 70 53 75 62 50 61  m->nMem = pSubPa
2cf46 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 70  rse->nMem;.    p
2cf47 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 20 3d 20  Program->nCsr = 
2cf48 70 53 75 62 50 61 72 73 65 2d 3e 6e 54 61 62 3b  pSubParse->nTab;
2cf49 0a 20 20 20 20 70 50 72 6f 67 72 61 6d 2d 3e 74  .    pProgram->t
2cf4a 6f 6b 65 6e 20 3d 20 28 76 6f 69 64 20 2a 29 70  oken = (void *)p
2cf4b 54 72 69 67 67 65 72 3b 0a 20 20 20 20 70 50 72  Trigger;.    pPr
2cf4c 67 2d 3e 6f 6c 64 6d 61 73 6b 20 3d 20 70 53 75  g->oldmask = pSu
2cf4d 62 50 61 72 73 65 2d 3e 6f 6c 64 6d 61 73 6b 3b  bParse->oldmask;
2cf4e 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2cf4f 44 65 6c 65 74 65 28 76 29 3b 0a 20 20 7d 0a 0a  Delete(v);.  }..
2cf50 20 20 61 73 73 65 72 74 28 20 21 70 53 75 62 50    assert( !pSubP
2cf51 61 72 73 65 2d 3e 70 41 69 6e 63 20 20 20 20 20  arse->pAinc     
2cf52 20 20 26 26 20 21 70 53 75 62 50 61 72 73 65 2d    && !pSubParse-
2cf53 3e 70 5a 6f 6d 62 69 65 54 61 62 20 29 3b 0a 20  >pZombieTab );. 
2cf54 20 61 73 73 65 72 74 28 20 21 70 53 75 62 50 61   assert( !pSubPa
2cf55 72 73 65 2d 3e 70 54 72 69 67 67 65 72 50 72 67  rse->pTriggerPrg
2cf56 20 26 26 20 21 70 53 75 62 50 61 72 73 65 2d 3e   && !pSubParse->
2cf57 6e 4d 61 78 41 72 67 20 29 3b 0a 20 20 73 71 6c  nMaxArg );.  sql
2cf58 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 64 62  ite3StackFree(db
2cf59 2c 20 70 53 75 62 50 61 72 73 65 29 3b 0a 0a 20  , pSubParse);.. 
2cf5a 20 72 65 74 75 72 6e 20 70 50 72 67 3b 0a 7d 0a   return pPrg;.}.
2cf5b 20 20 20 20 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72      ./*.** Retur
2cf5c 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  n a pointer to a
2cf5d 20 54 72 69 67 67 65 72 50 72 67 20 6f 62 6a 65   TriggerPrg obje
2cf5e 63 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ct containing th
2cf5f 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 66 6f  e sub-program fo
2cf60 72 0a 2a 2a 20 74 72 69 67 67 65 72 20 70 54 72  r.** trigger pTr
2cf61 69 67 67 65 72 20 77 69 74 68 20 64 65 66 61 75  igger with defau
2cf62 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61  lt ON CONFLICT a
2cf63 6c 67 6f 72 69 74 68 6d 20 6f 72 63 6f 6e 66 2e  lgorithm orconf.
2cf64 20 49 66 20 6e 6f 20 73 75 63 68 0a 2a 2a 20 54   If no such.** T
2cf65 72 69 67 67 65 72 50 72 67 20 6f 62 6a 65 63 74  riggerPrg object
2cf66 20 65 78 69 73 74 73 2c 20 61 20 6e 65 77 20 6f   exists, a new o
2cf67 62 6a 65 63 74 20 69 73 20 61 6c 6c 6f 63 61 74  bject is allocat
2cf68 65 64 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 64  ed and populated
2cf69 20 62 65 66 6f 72 65 0a 2a 2a 20 62 65 69 6e 67   before.** being
2cf6a 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74   returned..*/.st
2cf6b 61 74 69 63 20 54 72 69 67 67 65 72 50 72 67 20  atic TriggerPrg 
2cf6c 2a 67 65 74 52 6f 77 54 72 69 67 67 65 72 28 0a  *getRowTrigger(.
2cf6d 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2cf6e 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
2cf6f 74 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20  t parse context 
2cf70 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54  */.  Trigger *pT
2cf71 72 69 67 67 65 72 2c 20 20 20 2f 2a 20 54 72 69  rigger,   /* Tri
2cf72 67 67 65 72 20 74 6f 20 63 6f 64 65 20 2a 2f 0a  gger to code */.
2cf73 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
2cf74 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
2cf75 62 6c 65 20 74 72 69 67 67 65 72 20 70 54 72 69  ble trigger pTri
2cf76 67 67 65 72 20 69 73 20 61 74 74 61 63 68 65 64  gger is attached
2cf77 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63   to */.  int orc
2cf78 6f 6e 66 20 20 20 20 20 20 20 20 20 20 20 2f 2a  onf           /*
2cf79 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67   ON CONFLICT alg
2cf7a 6f 72 69 74 68 6d 2e 20 2a 2f 0a 29 7b 0a 20 20  orithm. */.){.  
2cf7b 50 61 72 73 65 20 2a 70 52 6f 6f 74 20 3d 20 73  Parse *pRoot = s
2cf7c 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65  qlite3ParseTople
2cf7d 76 65 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 54  vel(pParse);.  T
2cf7e 72 69 67 67 65 72 50 72 67 20 2a 70 50 72 67 3b  riggerPrg *pPrg;
2cf7f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54 72 69  ..  assert( pTri
2cf80 67 67 65 72 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 7c  gger->zName==0 |
2cf81 7c 20 70 54 61 62 3d 3d 74 61 62 6c 65 4f 66 54  | pTab==tableOfT
2cf82 72 69 67 67 65 72 28 70 54 72 69 67 67 65 72 29  rigger(pTrigger)
2cf83 20 29 3b 0a 0a 20 20 2f 2a 20 49 74 20 6d 61 79   );..  /* It may
2cf84 20 62 65 20 74 68 61 74 20 74 68 69 73 20 74 72   be that this tr
2cf85 69 67 67 65 72 20 68 61 73 20 61 6c 72 65 61 64  igger has alread
2cf86 79 20 62 65 65 6e 20 63 6f 64 65 64 20 28 6f 72  y been coded (or
2cf87 20 69 73 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20   is in the.  ** 
2cf88 70 72 6f 63 65 73 73 20 6f 66 20 62 65 69 6e 67  process of being
2cf89 20 63 6f 64 65 64 29 2e 20 49 66 20 74 68 69 73   coded). If this
2cf8a 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68   is the case, th
2cf8b 65 6e 20 61 6e 20 65 6e 74 72 79 20 77 69 74 68  en an entry with
2cf8c 0a 20 20 2a 2a 20 61 20 6d 61 74 63 68 69 6e 67  .  ** a matching
2cf8d 20 54 72 69 67 67 65 72 50 72 67 2e 70 54 72 69   TriggerPrg.pTri
2cf8e 67 67 65 72 20 66 69 65 6c 64 20 77 69 6c 6c 20  gger field will 
2cf8f 62 65 20 70 72 65 73 65 6e 74 20 73 6f 6d 65 77  be present somew
2cf90 68 65 72 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65  here.  ** in the
2cf91 20 50 61 72 73 65 2e 70 54 72 69 67 67 65 72 50   Parse.pTriggerP
2cf92 72 67 20 6c 69 73 74 2e 20 53 65 61 72 63 68 20  rg list. Search 
2cf93 66 6f 72 20 73 75 63 68 20 61 6e 20 65 6e 74 72  for such an entr
2cf94 79 2e 20 20 2a 2f 0a 20 20 66 6f 72 28 70 50 72  y.  */.  for(pPr
2cf95 67 3d 70 52 6f 6f 74 2d 3e 70 54 72 69 67 67 65  g=pRoot->pTrigge
2cf96 72 50 72 67 3b 20 0a 20 20 20 20 20 20 70 50 72  rPrg; .      pPr
2cf97 67 20 26 26 20 28 70 50 72 67 2d 3e 70 54 72 69  g && (pPrg->pTri
2cf98 67 67 65 72 21 3d 70 54 72 69 67 67 65 72 20 7c  gger!=pTrigger |
2cf99 7c 20 70 50 72 67 2d 3e 6f 72 63 6f 6e 66 21 3d  | pPrg->orconf!=
2cf9a 6f 72 63 6f 6e 66 29 3b 20 0a 20 20 20 20 20 20  orconf); .      
2cf9b 70 50 72 67 3d 70 50 72 67 2d 3e 70 4e 65 78 74  pPrg=pPrg->pNext
2cf9c 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61  .  );..  /* If a
2cf9d 6e 20 65 78 69 73 74 69 6e 67 20 54 72 69 67 67  n existing Trigg
2cf9e 65 72 50 72 67 20 63 6f 75 6c 64 20 6e 6f 74 20  erPrg could not 
2cf9f 62 65 20 6c 6f 63 61 74 65 64 2c 20 63 72 65 61  be located, crea
2cfa0 74 65 20 61 20 6e 65 77 20 6f 6e 65 2e 20 2a 2f  te a new one. */
2cfa1 0a 20 20 69 66 28 20 21 70 50 72 67 20 29 7b 0a  .  if( !pPrg ){.
2cfa2 20 20 20 20 70 50 72 67 20 3d 20 63 6f 64 65 52      pPrg = codeR
2cfa3 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65  owTrigger(pParse
2cfa4 2c 20 70 54 72 69 67 67 65 72 2c 20 70 54 61 62  , pTrigger, pTab
2cfa5 2c 20 6f 72 63 6f 6e 66 29 3b 0a 20 20 7d 0a 0a  , orconf);.  }..
2cfa6 20 20 72 65 74 75 72 6e 20 70 50 72 67 3b 0a 7d    return pPrg;.}
2cfa7 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
2cfa8 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 74 72   code for the tr
2cfa9 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 61 73  igger program as
2cfaa 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 72  sociated with tr
2cfab 69 67 67 65 72 20 70 20 6f 6e 20 0a 2a 2a 20 74  igger p on .** t
2cfac 61 62 6c 65 20 70 54 61 62 2e 20 54 68 65 20 72  able pTab. The r
2cfad 65 67 2c 20 6f 72 63 6f 6e 66 20 61 6e 64 20 69  eg, orconf and i
2cfae 67 6e 6f 72 65 4a 75 6d 70 20 70 61 72 61 6d 65  gnoreJump parame
2cfaf 74 65 72 73 20 70 61 73 73 65 64 20 74 6f 20 74  ters passed to t
2cfb0 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  his.** function 
2cfb1 61 72 65 20 74 68 65 20 73 61 6d 65 20 61 73 20  are the same as 
2cfb2 74 68 6f 73 65 20 64 65 73 63 72 69 62 65 64 20  those described 
2cfb3 69 6e 20 74 68 65 20 68 65 61 64 65 72 20 66 75  in the header fu
2cfb4 6e 63 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 73 71  nction for.** sq
2cfb5 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67  lite3CodeRowTrig
2cfb6 67 65 72 28 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ger().*/.SQLITE_
2cfb7 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2cfb8 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67  ite3CodeRowTrigg
2cfb9 65 72 44 69 72 65 63 74 28 0a 20 20 50 61 72 73  erDirect(.  Pars
2cfba 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2cfbb 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78   /* Parse contex
2cfbc 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a  t */.  Trigger *
2cfbd 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  p,          /* T
2cfbe 72 69 67 67 65 72 20 74 6f 20 63 6f 64 65 20 2a  rigger to code *
2cfbf 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
2cfc0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2cfc1 74 61 62 6c 65 20 74 6f 20 63 6f 64 65 20 74 72  table to code tr
2cfc2 69 67 67 65 72 73 20 66 72 6f 6d 20 2a 2f 0a 20  iggers from */. 
2cfc3 20 69 6e 74 20 72 65 67 2c 20 20 20 20 20 20 20   int reg,       
2cfc4 20 20 20 20 20 20 2f 2a 20 52 65 67 20 61 72 72        /* Reg arr
2cfc5 61 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 4f 4c  ay containing OL
2cfc6 44 2e 2a 20 61 6e 64 20 4e 45 57 2e 2a 20 76 61  D.* and NEW.* va
2cfc7 6c 75 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 72  lues */.  int or
2cfc8 63 6f 6e 66 2c 20 20 20 20 20 20 20 20 20 20 2f  conf,          /
2cfc9 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f  * ON CONFLICT po
2cfca 6c 69 63 79 20 2a 2f 0a 20 20 69 6e 74 20 69 67  licy */.  int ig
2cfcb 6e 6f 72 65 4a 75 6d 70 20 20 20 20 20 20 20 2f  noreJump       /
2cfcc 2a 20 49 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f  * Instruction to
2cfcd 20 6a 75 6d 70 20 74 6f 20 66 6f 72 20 52 41 49   jump to for RAI
2cfce 53 45 28 49 47 4e 4f 52 45 29 20 2a 2f 0a 29 7b  SE(IGNORE) */.){
2cfcf 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c  .  Vdbe *v = sql
2cfd0 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
2cfd1 73 65 29 3b 20 2f 2a 20 4d 61 69 6e 20 56 4d 20  se); /* Main VM 
2cfd2 2a 2f 0a 20 20 54 72 69 67 67 65 72 50 72 67 20  */.  TriggerPrg 
2cfd3 2a 70 50 72 67 3b 0a 20 20 70 50 72 67 20 3d 20  *pPrg;.  pPrg = 
2cfd4 67 65 74 52 6f 77 54 72 69 67 67 65 72 28 70 50  getRowTrigger(pP
2cfd5 61 72 73 65 2c 20 70 2c 20 70 54 61 62 2c 20 6f  arse, p, pTab, o
2cfd6 72 63 6f 6e 66 29 3b 0a 20 20 61 73 73 65 72 74  rconf);.  assert
2cfd7 28 20 70 50 72 67 20 7c 7c 20 70 50 61 72 73 65  ( pPrg || pParse
2cfd8 2d 3e 6e 45 72 72 20 7c 7c 20 70 50 61 72 73 65  ->nErr || pParse
2cfd9 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
2cfda 65 64 20 29 3b 0a 0a 20 20 2f 2a 20 43 6f 64 65  ed );..  /* Code
2cfdb 20 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20   the OP_Program 
2cfdc 6f 70 63 6f 64 65 20 69 6e 20 74 68 65 20 70 61  opcode in the pa
2cfdd 72 65 6e 74 20 56 44 42 45 2e 20 50 34 20 6f 66  rent VDBE. P4 of
2cfde 20 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20   the OP_Program 
2cfdf 0a 20 20 2a 2a 20 69 73 20 61 20 70 6f 69 6e 74  .  ** is a point
2cfe0 65 72 20 74 6f 20 74 68 65 20 73 75 62 2d 76 64  er to the sub-vd
2cfe1 62 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  be containing th
2cfe2 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  e trigger progra
2cfe3 6d 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 72  m.  */.  if( pPr
2cfe4 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  g ){.    sqlite3
2cfe5 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2cfe6 5f 50 72 6f 67 72 61 6d 2c 20 72 65 67 2c 20 69  _Program, reg, i
2cfe7 67 6e 6f 72 65 4a 75 6d 70 2c 20 2b 2b 70 50 61  gnoreJump, ++pPa
2cfe8 72 73 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20  rse->nMem);.    
2cfe9 70 50 72 67 2d 3e 70 50 72 6f 67 72 61 6d 2d 3e  pPrg->pProgram->
2cfea 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 73 71 6c 69  nRef++;.    sqli
2cfeb 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
2cfec 76 2c 20 2d 31 2c 20 28 63 6f 6e 73 74 20 63 68  v, -1, (const ch
2cfed 61 72 20 2a 29 70 50 72 67 2d 3e 70 50 72 6f 67  ar *)pPrg->pProg
2cfee 72 61 6d 2c 20 50 34 5f 53 55 42 50 52 4f 47 52  ram, P4_SUBPROGR
2cfef 41 4d 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d  AM);.    VdbeCom
2cff0 6d 65 6e 74 28 0a 20 20 20 20 20 20 20 20 28 76  ment(.        (v
2cff1 2c 20 22 43 61 6c 6c 3a 20 25 73 2e 25 73 22 2c  , "Call: %s.%s",
2cff2 20 28 70 2d 3e 7a 4e 61 6d 65 3f 70 2d 3e 7a 4e   (p->zName?p->zN
2cff3 61 6d 65 3a 22 66 6b 65 79 22 29 2c 20 6f 6e 45  ame:"fkey"), onE
2cff4 72 72 6f 72 54 65 78 74 28 6f 72 63 6f 6e 66 29  rrorText(orconf)
2cff5 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20  ));..    /* Set 
2cff6 74 68 65 20 50 35 20 6f 70 65 72 61 6e 64 20 6f  the P5 operand o
2cff7 66 20 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d  f the OP_Program
2cff8 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20   instruction to 
2cff9 6e 6f 6e 2d 7a 65 72 6f 20 69 66 0a 20 20 20 20  non-zero if.    
2cffa 2a 2a 20 72 65 63 75 72 73 69 76 65 20 69 6e 76  ** recursive inv
2cffb 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20  ocation of this 
2cffc 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20  trigger program 
2cffd 69 73 20 64 69 73 61 6c 6c 6f 77 65 64 2e 20 52  is disallowed. R
2cffe 65 63 75 72 73 69 76 65 0a 20 20 20 20 2a 2a 20  ecursive.    ** 
2cfff 69 6e 76 6f 63 61 74 69 6f 6e 20 69 73 20 64 69  invocation is di
2d000 73 61 6c 6c 6f 77 65 64 20 69 66 20 28 61 29 20  sallowed if (a) 
2d001 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20  the sub-program 
2d002 69 73 20 72 65 61 6c 6c 79 20 61 20 74 72 69 67  is really a trig
2d003 67 65 72 2c 0a 20 20 20 20 2a 2a 20 6e 6f 74 20  ger,.    ** not 
2d004 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63  a foreign key ac
2d005 74 69 6f 6e 2c 20 61 6e 64 20 28 62 29 20 74 68  tion, and (b) th
2d006 65 20 66 6c 61 67 20 74 6f 20 65 6e 61 62 6c 65  e flag to enable
2d007 20 72 65 63 75 72 73 69 76 65 20 74 72 69 67 67   recursive trigg
2d008 65 72 73 0a 20 20 20 20 2a 2a 20 69 73 20 63 6c  ers.    ** is cl
2d009 65 61 72 2e 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ear.  */.    sql
2d00a 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35  ite3VdbeChangeP5
2d00b 28 76 2c 20 70 2d 3e 7a 4e 61 6d 65 20 26 26 20  (v, p->zName && 
2d00c 21 28 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c  !(pParse->db->fl
2d00d 61 67 73 26 53 51 4c 49 54 45 5f 52 65 63 54 72  ags&SQLITE_RecTr
2d00e 69 67 67 65 72 73 29 29 3b 0a 20 20 7d 0a 7d 0a  iggers));.  }.}.
2d00f 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 63  ./*.** This is c
2d010 61 6c 6c 65 64 20 74 6f 20 63 6f 64 65 20 74 68  alled to code th
2d011 65 20 72 65 71 75 69 72 65 64 20 46 4f 52 20 45  e required FOR E
2d012 41 43 48 20 52 4f 57 20 74 72 69 67 67 65 72 73  ACH ROW triggers
2d013 20 66 6f 72 20 61 6e 20 6f 70 65 72 61 74 69 6f   for an operatio
2d014 6e 0a 2a 2a 20 6f 6e 20 74 61 62 6c 65 20 70 54  n.** on table pT
2d015 61 62 2e 20 54 68 65 20 6f 70 65 72 61 74 69 6f  ab. The operatio
2d016 6e 20 74 6f 20 63 6f 64 65 20 74 72 69 67 67 65  n to code trigge
2d017 72 73 20 66 6f 72 20 28 49 4e 53 45 52 54 2c 20  rs for (INSERT, 
2d018 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45  UPDATE or DELETE
2d019 29 0a 2a 2a 20 69 73 20 67 69 76 65 6e 20 62 79  ).** is given by
2d01a 20 74 68 65 20 6f 70 20 70 61 72 61 6d 61 74 65   the op paramate
2d01b 72 2e 20 54 68 65 20 74 72 5f 74 6d 20 70 61 72  r. The tr_tm par
2d01c 61 6d 65 74 65 72 20 64 65 74 65 72 6d 69 6e 65  ameter determine
2d01d 73 20 77 68 65 74 68 65 72 20 74 68 65 0a 2a 2a  s whether the.**
2d01e 20 42 45 46 4f 52 45 20 6f 72 20 41 46 54 45 52   BEFORE or AFTER
2d01f 20 74 72 69 67 67 65 72 73 20 61 72 65 20 63 6f   triggers are co
2d020 64 65 64 2e 20 49 66 20 74 68 65 20 6f 70 65 72  ded. If the oper
2d021 61 74 69 6f 6e 20 69 73 20 61 6e 20 55 50 44 41  ation is an UPDA
2d022 54 45 2c 20 74 68 65 6e 0a 2a 2a 20 70 61 72 61  TE, then.** para
2d023 6d 65 74 65 72 20 70 43 68 61 6e 67 65 73 20 69  meter pChanges i
2d024 73 20 70 61 73 73 65 64 20 74 68 65 20 6c 69 73  s passed the lis
2d025 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 62 65 69  t of columns bei
2d026 6e 67 20 6d 6f 64 69 66 69 65 64 2e 0a 2a 2a 0a  ng modified..**.
2d027 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  ** If there are 
2d028 6e 6f 20 74 72 69 67 67 65 72 73 20 74 68 61 74  no triggers that
2d029 20 66 69 72 65 20 61 74 20 74 68 65 20 73 70 65   fire at the spe
2d02a 63 69 66 69 65 64 20 74 69 6d 65 20 66 6f 72 20  cified time for 
2d02b 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  the specified.**
2d02c 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20 70 54   operation on pT
2d02d 61 62 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ab, this functio
2d02e 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  n is a no-op..**
2d02f 0a 2a 2a 20 54 68 65 20 72 65 67 20 61 72 67 75  .** The reg argu
2d030 6d 65 6e 74 20 69 73 20 74 68 65 20 61 64 64 72  ment is the addr
2d031 65 73 73 20 6f 66 20 74 68 65 20 66 69 72 73 74  ess of the first
2d032 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20   in an array of 
2d033 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20 74 68  registers .** th
2d034 61 74 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 76  at contain the v
2d035 61 6c 75 65 73 20 73 75 62 73 74 69 74 75 74 65  alues substitute
2d036 64 20 66 6f 72 20 74 68 65 20 6e 65 77 2e 2a 20  d for the new.* 
2d037 61 6e 64 20 6f 6c 64 2e 2a 20 72 65 66 65 72 65  and old.* refere
2d038 6e 63 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 74  nces.** in the t
2d039 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 20  rigger program. 
2d03a 49 66 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62  If N is the numb
2d03b 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
2d03c 20 74 61 62 6c 65 20 70 54 61 62 0a 2a 2a 20 28   table pTab.** (
2d03d 61 20 63 6f 70 79 20 6f 66 20 70 54 61 62 2d 3e  a copy of pTab->
2d03e 6e 43 6f 6c 29 2c 20 74 68 65 6e 20 72 65 67 69  nCol), then regi
2d03f 73 74 65 72 73 20 61 72 65 20 70 6f 70 75 6c 61  sters are popula
2d040 74 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  ted as follows:.
2d041 2a 2a 0a 2a 2a 20 20 20 52 65 67 69 73 74 65 72  **.**   Register
2d042 20 20 20 20 20 20 20 43 6f 6e 74 61 69 6e 73 0a         Contains.
2d043 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **   -----------
2d044 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d045 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d046 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
2d047 20 72 65 67 2b 30 20 20 20 20 20 20 20 20 20 20   reg+0          
2d048 4f 4c 44 2e 72 6f 77 69 64 0a 2a 2a 20 20 20 72  OLD.rowid.**   r
2d049 65 67 2b 31 20 20 20 20 20 20 20 20 20 20 4f 4c  eg+1          OL
2d04a 44 2e 2a 20 76 61 6c 75 65 20 6f 66 20 6c 65 66  D.* value of lef
2d04b 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66  t-most column of
2d04c 20 70 54 61 62 0a 2a 2a 20 20 20 2e 2e 2e 20 20   pTab.**   ...  
2d04d 20 20 20 20 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a            ....**
2d04e 20 20 20 72 65 67 2b 4e 20 20 20 20 20 20 20 20     reg+N        
2d04f 20 20 4f 4c 44 2e 2a 20 76 61 6c 75 65 20 6f 66    OLD.* value of
2d050 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 6f 6c 75   right-most colu
2d051 6d 6e 20 6f 66 20 70 54 61 62 0a 2a 2a 20 20 20  mn of pTab.**   
2d052 72 65 67 2b 4e 2b 31 20 20 20 20 20 20 20 20 4e  reg+N+1        N
2d053 45 57 2e 72 6f 77 69 64 0a 2a 2a 20 20 20 72 65  EW.rowid.**   re
2d054 67 2b 4e 2b 32 20 20 20 20 20 20 20 20 4f 4c 44  g+N+2        OLD
2d055 2e 2a 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74  .* value of left
2d056 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20  -most column of 
2d057 70 54 61 62 0a 2a 2a 20 20 20 2e 2e 2e 20 20 20  pTab.**   ...   
2d058 20 20 20 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20           ....** 
2d059 20 20 72 65 67 2b 4e 2b 4e 2b 31 20 20 20 20 20    reg+N+N+1     
2d05a 20 4e 45 57 2e 2a 20 76 61 6c 75 65 20 6f 66 20   NEW.* value of 
2d05b 72 69 67 68 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d  right-most colum
2d05c 6e 20 6f 66 20 70 54 61 62 0a 2a 2a 0a 2a 2a 20  n of pTab.**.** 
2d05d 46 6f 72 20 4f 4e 20 44 45 4c 45 54 45 20 74 72  For ON DELETE tr
2d05e 69 67 67 65 72 73 2c 20 74 68 65 20 72 65 67 69  iggers, the regi
2d05f 73 74 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67  sters containing
2d060 20 74 68 65 20 4e 45 57 2e 2a 20 76 61 6c 75 65   the NEW.* value
2d061 73 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20  s will.** never 
2d062 62 65 20 61 63 63 65 73 73 65 64 20 62 79 20 74  be accessed by t
2d063 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  he trigger progr
2d064 61 6d 2c 20 73 6f 20 74 68 65 79 20 61 72 65 20  am, so they are 
2d065 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64 20 6f 72  not allocated or
2d066 20 0a 2a 2a 20 70 6f 70 75 6c 61 74 65 64 20 62   .** populated b
2d067 79 20 74 68 65 20 63 61 6c 6c 65 72 20 28 74 68  y the caller (th
2d068 65 72 65 20 69 73 20 6e 6f 20 64 61 74 61 20 74  ere is no data t
2d069 6f 20 70 6f 70 75 6c 61 74 65 20 74 68 65 6d 20  o populate them 
2d06a 77 69 74 68 20 61 6e 79 77 61 79 29 2e 20 0a 2a  with anyway). .*
2d06b 2a 20 53 69 6d 69 6c 61 72 6c 79 2c 20 66 6f 72  * Similarly, for
2d06c 20 4f 4e 20 49 4e 53 45 52 54 20 74 72 69 67 67   ON INSERT trigg
2d06d 65 72 73 20 74 68 65 20 76 61 6c 75 65 73 20 73  ers the values s
2d06e 74 6f 72 65 64 20 69 6e 20 74 68 65 20 4f 4c 44  tored in the OLD
2d06f 2e 2a 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20  .* registers.** 
2d070 61 72 65 20 6e 65 76 65 72 20 61 63 63 65 73 73  are never access
2d071 65 64 2c 20 61 6e 64 20 73 6f 20 61 72 65 20 6e  ed, and so are n
2d072 6f 74 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  ot allocated by 
2d073 74 68 65 20 63 61 6c 6c 65 72 2e 20 53 6f 2c 20  the caller. So, 
2d074 66 6f 72 20 61 6e 0a 2a 2a 20 4f 4e 20 49 4e 53  for an.** ON INS
2d075 45 52 54 20 74 72 69 67 67 65 72 2c 20 74 68 65  ERT trigger, the
2d076 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 74 6f   value passed to
2d077 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   this function a
2d078 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 67 0a  s parameter reg.
2d079 2a 2a 20 69 73 20 6e 6f 74 20 61 20 72 65 61 64  ** is not a read
2d07a 61 62 6c 65 20 72 65 67 69 73 74 65 72 2c 20 61  able register, a
2d07b 6c 74 68 6f 75 67 68 20 72 65 67 69 73 74 65 72  lthough register
2d07c 73 20 28 72 65 67 2b 4e 29 20 74 68 72 6f 75 67  s (reg+N) throug
2d07d 68 20 0a 2a 2a 20 28 72 65 67 2b 4e 2b 4e 2b 31  h .** (reg+N+N+1
2d07e 29 20 61 72 65 2e 0a 2a 2a 0a 2a 2a 20 50 61 72  ) are..**.** Par
2d07f 61 6d 65 74 65 72 20 6f 72 63 6f 6e 66 20 69 73  ameter orconf is
2d080 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f 6e   the default con
2d081 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e  flict resolution
2d082 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 74   algorithm for t
2d083 68 65 0a 2a 2a 20 74 72 69 67 67 65 72 20 70 72  he.** trigger pr
2d084 6f 67 72 61 6d 20 74 6f 20 75 73 65 20 28 52 45  ogram to use (RE
2d085 50 4c 41 43 45 2c 20 49 47 4e 4f 52 45 20 65 74  PLACE, IGNORE et
2d086 63 2e 29 2e 20 50 61 72 61 6d 65 74 65 72 20 69  c.). Parameter i
2d087 67 6e 6f 72 65 4a 75 6d 70 0a 2a 2a 20 69 73 20  gnoreJump.** is 
2d088 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  the instruction 
2d089 74 68 61 74 20 63 6f 6e 74 72 6f 6c 20 73 68 6f  that control sho
2d08a 75 6c 64 20 6a 75 6d 70 20 74 6f 20 69 66 20 61  uld jump to if a
2d08b 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
2d08c 0a 2a 2a 20 72 61 69 73 65 73 20 61 6e 20 49 47  .** raises an IG
2d08d 4e 4f 52 45 20 65 78 63 65 70 74 69 6f 6e 2e 0a  NORE exception..
2d08e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2d08f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
2d090 64 65 52 6f 77 54 72 69 67 67 65 72 28 0a 20 20  deRowTrigger(.  
2d091 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2d092 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f       /* Parse co
2d093 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67  ntext */.  Trigg
2d094 65 72 20 2a 70 54 72 69 67 67 65 72 2c 20 20 20  er *pTrigger,   
2d095 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67 67  /* List of trigg
2d096 65 72 73 20 6f 6e 20 74 61 62 6c 65 20 70 54 61  ers on table pTa
2d097 62 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20  b */.  int op,  
2d098 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
2d099 6e 65 20 6f 66 20 54 4b 5f 55 50 44 41 54 45 2c  ne of TK_UPDATE,
2d09a 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 44   TK_INSERT, TK_D
2d09b 45 4c 45 54 45 20 2a 2f 0a 20 20 45 78 70 72 4c  ELETE */.  ExprL
2d09c 69 73 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20  ist *pChanges,  
2d09d 2f 2a 20 43 68 61 6e 67 65 73 20 6c 69 73 74 20  /* Changes list 
2d09e 66 6f 72 20 61 6e 79 20 55 50 44 41 54 45 20 4f  for any UPDATE O
2d09f 46 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20  F triggers */.  
2d0a0 69 6e 74 20 74 72 5f 74 6d 2c 20 20 20 20 20 20  int tr_tm,      
2d0a1 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54       /* One of T
2d0a2 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c 20 54  RIGGER_BEFORE, T
2d0a3 52 49 47 47 45 52 5f 41 46 54 45 52 20 2a 2f 0a  RIGGER_AFTER */.
2d0a4 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
2d0a5 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
2d0a6 62 6c 65 20 74 6f 20 63 6f 64 65 20 74 72 69 67  ble to code trig
2d0a7 67 65 72 73 20 66 72 6f 6d 20 2a 2f 0a 20 20 69  gers from */.  i
2d0a8 6e 74 20 72 65 67 2c 20 20 20 20 20 20 20 20 20  nt reg,         
2d0a9 20 20 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74      /* The first
2d0aa 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20   in an array of 
2d0ab 72 65 67 69 73 74 65 72 73 20 28 73 65 65 20 61  registers (see a
2d0ac 62 6f 76 65 29 20 2a 2f 0a 20 20 69 6e 74 20 6f  bove) */.  int o
2d0ad 72 63 6f 6e 66 2c 20 20 20 20 20 20 20 20 20 20  rconf,          
2d0ae 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70  /* ON CONFLICT p
2d0af 6f 6c 69 63 79 20 2a 2f 0a 20 20 69 6e 74 20 69  olicy */.  int i
2d0b0 67 6e 6f 72 65 4a 75 6d 70 20 20 20 20 20 20 20  gnoreJump       
2d0b1 2f 2a 20 49 6e 73 74 72 75 63 74 69 6f 6e 20 74  /* Instruction t
2d0b2 6f 20 6a 75 6d 70 20 74 6f 20 66 6f 72 20 52 41  o jump to for RA
2d0b3 49 53 45 28 49 47 4e 4f 52 45 29 20 2a 2f 0a 29  ISE(IGNORE) */.)
2d0b4 7b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 3b 20  {.  Trigger *p; 
2d0b5 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64           /* Used
2d0b6 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f   to iterate thro
2d0b7 75 67 68 20 70 54 72 69 67 67 65 72 20 6c 69 73  ugh pTrigger lis
2d0b8 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  t */..  assert( 
2d0b9 6f 70 3d 3d 54 4b 5f 55 50 44 41 54 45 20 7c 7c  op==TK_UPDATE ||
2d0ba 20 6f 70 3d 3d 54 4b 5f 49 4e 53 45 52 54 20 7c   op==TK_INSERT |
2d0bb 7c 20 6f 70 3d 3d 54 4b 5f 44 45 4c 45 54 45 20  | op==TK_DELETE 
2d0bc 29 3b 0a 20 20 61 73 73 65 72 74 28 20 74 72 5f  );.  assert( tr_
2d0bd 74 6d 3d 3d 54 52 49 47 47 45 52 5f 42 45 46 4f  tm==TRIGGER_BEFO
2d0be 52 45 20 7c 7c 20 74 72 5f 74 6d 3d 3d 54 52 49  RE || tr_tm==TRI
2d0bf 47 47 45 52 5f 41 46 54 45 52 20 29 3b 0a 20 20  GGER_AFTER );.  
2d0c0 61 73 73 65 72 74 28 20 28 6f 70 3d 3d 54 4b 5f  assert( (op==TK_
2d0c1 55 50 44 41 54 45 29 3d 3d 28 70 43 68 61 6e 67  UPDATE)==(pChang
2d0c2 65 73 21 3d 30 29 20 29 3b 0a 0a 20 20 66 6f 72  es!=0) );..  for
2d0c3 28 70 3d 70 54 72 69 67 67 65 72 3b 20 70 3b 20  (p=pTrigger; p; 
2d0c4 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 0a 20 20  p=p->pNext){..  
2d0c5 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 65 63    /* Sanity chec
2d0c6 6b 69 6e 67 3a 20 20 54 68 65 20 73 63 68 65 6d  king:  The schem
2d0c7 61 20 66 6f 72 20 74 68 65 20 74 72 69 67 67 65  a for the trigge
2d0c8 72 20 61 6e 64 20 66 6f 72 20 74 68 65 20 74 61  r and for the ta
2d0c9 62 6c 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 61  ble are.    ** a
2d0ca 6c 77 61 79 73 20 64 65 66 69 6e 65 64 2e 20 20  lways defined.  
2d0cb 54 68 65 20 74 72 69 67 67 65 72 20 6d 75 73 74  The trigger must
2d0cc 20 62 65 20 69 6e 20 74 68 65 20 73 61 6d 65 20   be in the same 
2d0cd 73 63 68 65 6d 61 20 61 73 20 74 68 65 20 74 61  schema as the ta
2d0ce 62 6c 65 0a 20 20 20 20 2a 2a 20 6f 72 20 65 6c  ble.    ** or el
2d0cf 73 65 20 69 74 20 6d 75 73 74 20 62 65 20 61 20  se it must be a 
2d0d0 54 45 4d 50 20 74 72 69 67 67 65 72 2e 20 2a 2f  TEMP trigger. */
2d0d1 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
2d0d2 70 53 63 68 65 6d 61 21 3d 30 20 29 3b 0a 20 20  pSchema!=0 );.  
2d0d3 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 54 61    assert( p->pTa
2d0d4 62 53 63 68 65 6d 61 21 3d 30 20 29 3b 0a 20 20  bSchema!=0 );.  
2d0d5 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 53 63    assert( p->pSc
2d0d6 68 65 6d 61 3d 3d 70 2d 3e 70 54 61 62 53 63 68  hema==p->pTabSch
2d0d7 65 6d 61 20 0a 20 20 20 20 20 20 20 20 20 7c 7c  ema .         ||
2d0d8 20 70 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 50 61   p->pSchema==pPa
2d0d9 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 31 5d 2e  rse->db->aDb[1].
2d0da 70 53 63 68 65 6d 61 20 29 3b 0a 0a 20 20 20 20  pSchema );..    
2d0db 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 77 68 65  /* Determine whe
2d0dc 74 68 65 72 20 77 65 20 73 68 6f 75 6c 64 20 63  ther we should c
2d0dd 6f 64 65 20 74 68 69 73 20 74 72 69 67 67 65 72  ode this trigger
2d0de 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 6f   */.    if( p->o
2d0df 70 3d 3d 6f 70 20 0a 20 20 20 20 20 26 26 20 70  p==op .     && p
2d0e0 2d 3e 74 72 5f 74 6d 3d 3d 74 72 5f 74 6d 20 0a  ->tr_tm==tr_tm .
2d0e1 20 20 20 20 20 26 26 20 63 68 65 63 6b 43 6f 6c       && checkCol
2d0e2 75 6d 6e 4f 76 65 72 6c 61 70 28 70 2d 3e 70 43  umnOverlap(p->pC
2d0e3 6f 6c 75 6d 6e 73 2c 20 70 43 68 61 6e 67 65 73  olumns, pChanges
2d0e4 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 73  ).    ){.      s
2d0e5 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69  qlite3CodeRowTri
2d0e6 67 67 65 72 44 69 72 65 63 74 28 70 50 61 72 73  ggerDirect(pPars
2d0e7 65 2c 20 70 2c 20 70 54 61 62 2c 20 72 65 67 2c  e, p, pTab, reg,
2d0e8 20 6f 72 63 6f 6e 66 2c 20 69 67 6e 6f 72 65 4a   orconf, ignoreJ
2d0e9 75 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ump);.    }.  }.
2d0ea 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 69 67 67 65 72  }../*.** Trigger
2d0eb 73 20 66 69 72 65 64 20 62 79 20 55 50 44 41 54  s fired by UPDAT
2d0ec 45 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74  E or DELETE stat
2d0ed 65 6d 65 6e 74 73 20 6d 61 79 20 61 63 63 65 73  ements may acces
2d0ee 73 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 0a  s values stored.
2d0ef 2a 2a 20 69 6e 20 74 68 65 20 6f 6c 64 2e 2a 20  ** in the old.* 
2d0f0 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 20 54 68  pseudo-table. Th
2d0f1 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
2d0f2 72 6e 73 20 61 20 33 32 2d 62 69 74 20 62 69 74  rns a 32-bit bit
2d0f3 6d 61 73 6b 0a 2a 2a 20 69 6e 64 69 63 61 74 69  mask.** indicati
2d0f4 6e 67 20 77 68 69 63 68 20 63 6f 6c 75 6d 6e 73  ng which columns
2d0f5 20 6f 66 20 74 68 65 20 6f 6c 64 2e 2a 20 74 61   of the old.* ta
2d0f6 62 6c 65 20 61 63 74 75 61 6c 6c 79 20 61 72 65  ble actually are
2d0f7 20 75 73 65 64 20 62 79 0a 2a 2a 20 74 72 69 67   used by.** trig
2d0f8 67 65 72 73 2e 20 54 68 69 73 20 69 6e 66 6f 72  gers. This infor
2d0f9 6d 61 74 69 6f 6e 20 6d 61 79 20 62 65 20 75 73  mation may be us
2d0fa 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72  ed by the caller
2d0fb 20 74 6f 20 61 76 6f 69 64 20 68 61 76 69 6e 67   to avoid having
2d0fc 0a 2a 2a 20 74 6f 20 6c 6f 61 64 20 74 68 65 20  .** to load the 
2d0fd 65 6e 74 69 72 65 20 6f 6c 64 2e 2a 20 72 65 63  entire old.* rec
2d0fe 6f 72 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20  ord into memory 
2d0ff 77 68 65 6e 20 65 78 65 63 75 74 69 6e 67 20 61  when executing a
2d100 6e 20 55 50 44 41 54 45 0a 2a 2a 20 6f 72 20 44  n UPDATE.** or D
2d101 45 4c 45 54 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a  ELETE command..*
2d102 2a 0a 2a 2a 20 42 69 74 20 30 20 6f 66 20 74 68  *.** Bit 0 of th
2d103 65 20 72 65 74 75 72 6e 65 64 20 6d 61 73 6b 20  e returned mask 
2d104 69 73 20 73 65 74 20 69 66 20 74 68 65 20 6c 65  is set if the le
2d105 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f  ft-most column o
2d106 66 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 6d  f the.** table m
2d107 61 79 20 62 65 20 61 63 63 65 73 73 65 64 20 75  ay be accessed u
2d108 73 69 6e 67 20 61 6e 20 6f 6c 64 2e 3c 63 6f 6c  sing an old.<col
2d109 3e 20 72 65 66 65 72 65 6e 63 65 2e 20 42 69 74  > reference. Bit
2d10a 20 31 20 69 73 20 73 65 74 20 69 66 0a 2a 2a 20   1 is set if.** 
2d10b 74 68 65 20 73 65 63 6f 6e 64 20 6c 65 66 74 6d  the second leftm
2d10c 6f 73 74 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65  ost column value
2d10d 20 69 73 20 72 65 71 75 69 72 65 64 2c 20 61 6e   is required, an
2d10e 64 20 73 6f 20 6f 6e 2e 20 49 66 20 74 68 65 72  d so on. If ther
2d10f 65 0a 2a 2a 20 61 72 65 20 6d 6f 72 65 20 74 68  e.** are more th
2d110 61 6e 20 33 32 20 63 6f 6c 75 6d 6e 73 20 69 6e  an 32 columns in
2d111 20 74 68 65 20 74 61 62 6c 65 2c 20 61 6e 64 20   the table, and 
2d112 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 66 20  at least one of 
2d113 74 68 65 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 77  the columns.** w
2d114 69 74 68 20 61 6e 20 69 6e 64 65 78 20 67 72 65  ith an index gre
2d115 61 74 65 72 20 74 68 61 6e 20 33 32 20 6d 61 79  ater than 32 may
2d116 20 62 65 20 61 63 63 65 73 73 65 64 2c 20 30 78   be accessed, 0x
2d117 66 66 66 66 66 66 66 66 20 69 73 20 72 65 74 75  ffffffff is retu
2d118 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69  rned..**.** It i
2d119 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74  s not possible t
2d11a 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74  o determine if t
2d11b 68 65 20 6f 6c 64 2e 72 6f 77 69 64 20 63 6f 6c  he old.rowid col
2d11c 75 6d 6e 20 69 73 20 61 63 63 65 73 73 65 64 0a  umn is accessed.
2d11d 2a 2a 20 62 79 20 74 72 69 67 67 65 72 73 2e 20  ** by triggers. 
2d11e 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20  The caller must 
2d11f 61 6c 77 61 79 73 20 61 73 73 75 6d 65 20 74 68  always assume th
2d120 61 74 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a 2a 20  at it is..**.** 
2d121 54 68 65 72 65 20 69 73 20 6e 6f 20 65 71 75 69  There is no equi
2d122 76 61 6c 65 6e 74 20 66 75 6e 63 74 69 6f 6e 20  valent function 
2d123 66 6f 72 20 6e 65 77 2e 2a 20 72 65 66 65 72 65  for new.* refere
2d124 6e 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nces..*/.SQLITE_
2d125 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69  PRIVATE u32 sqli
2d126 74 65 33 54 72 69 67 67 65 72 4f 6c 64 6d 61 73  te3TriggerOldmas
2d127 6b 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  k(.  Parse *pPar
2d128 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72  se,       /* Par
2d129 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  se context */.  
2d12a 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
2d12b 72 2c 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20  r,   /* List of 
2d12c 74 72 69 67 67 65 72 73 20 6f 6e 20 74 61 62 6c  triggers on tabl
2d12d 65 20 70 54 61 62 20 2a 2f 0a 20 20 45 78 70 72  e pTab */.  Expr
2d12e 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73 2c 20  List *pChanges, 
2d12f 20 2f 2a 20 43 68 61 6e 67 65 73 20 6c 69 73 74   /* Changes list
2d130 20 66 6f 72 20 61 6e 79 20 55 50 44 41 54 45 20   for any UPDATE 
2d131 4f 46 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20  OF triggers */. 
2d132 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
2d133 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
2d134 6c 65 20 74 6f 20 63 6f 64 65 20 74 72 69 67 67  le to code trigg
2d135 65 72 73 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e  ers from */.  in
2d136 74 20 6f 72 63 6f 6e 66 20 20 20 20 20 20 20 20  t orconf        
2d137 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e     /* Default ON
2d138 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79   CONFLICT policy
2d139 20 66 6f 72 20 74 72 69 67 67 65 72 20 73 74 65   for trigger ste
2d13a 70 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74  ps */.){.  const
2d13b 20 69 6e 74 20 6f 70 20 3d 20 70 43 68 61 6e 67   int op = pChang
2d13c 65 73 20 3f 20 54 4b 5f 55 50 44 41 54 45 20 3a  es ? TK_UPDATE :
2d13d 20 54 4b 5f 44 45 4c 45 54 45 3b 0a 20 20 75 33   TK_DELETE;.  u3
2d13e 32 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 54 72  2 mask = 0;.  Tr
2d13f 69 67 67 65 72 20 2a 70 3b 0a 0a 20 20 66 6f 72  igger *p;..  for
2d140 28 70 3d 70 54 72 69 67 67 65 72 3b 20 70 3b 20  (p=pTrigger; p; 
2d141 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  p=p->pNext){.   
2d142 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 6f 70 20 26   if( p->op==op &
2d143 26 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76 65  & checkColumnOve
2d144 72 6c 61 70 28 70 2d 3e 70 43 6f 6c 75 6d 6e 73  rlap(p->pColumns
2d145 2c 70 43 68 61 6e 67 65 73 29 20 29 7b 0a 20 20  ,pChanges) ){.  
2d146 20 20 20 20 54 72 69 67 67 65 72 50 72 67 20 2a      TriggerPrg *
2d147 70 50 72 67 3b 0a 20 20 20 20 20 20 70 50 72 67  pPrg;.      pPrg
2d148 20 3d 20 67 65 74 52 6f 77 54 72 69 67 67 65 72   = getRowTrigger
2d149 28 70 50 61 72 73 65 2c 20 70 2c 20 70 54 61 62  (pParse, p, pTab
2d14a 2c 20 6f 72 63 6f 6e 66 29 3b 0a 20 20 20 20 20  , orconf);.     
2d14b 20 69 66 28 20 70 50 72 67 20 29 7b 0a 20 20 20   if( pPrg ){.   
2d14c 20 20 20 20 20 6d 61 73 6b 20 7c 3d 20 70 50 72       mask |= pPr
2d14d 67 2d 3e 6f 6c 64 6d 61 73 6b 3b 0a 20 20 20 20  g->oldmask;.    
2d14e 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
2d14f 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a   return mask;.}.
2d150 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
2d151 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2d152 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a 2f 2a 2a  TRIGGER) */../**
2d153 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
2d154 20 6f 66 20 74 72 69 67 67 65 72 2e 63 20 2a 2a   of trigger.c **
2d155 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d156 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d157 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
2d158 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
2d159 69 6e 20 66 69 6c 65 20 75 70 64 61 74 65 2e 63  in file update.c
2d15a 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2d15b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d15c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
2d15d 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
2d15e 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
2d15f 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
2d160 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
2d161 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
2d162 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
2d163 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
2d164 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
2d165 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
2d166 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
2d167 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
2d168 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
2d169 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
2d16a 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
2d16b 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
2d16c 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
2d16d 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
2d16e 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
2d16f 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
2d170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d171 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d172 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d173 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d174 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
2d175 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65   contains C code
2d176 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61   routines that a
2d177 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  re called by the
2d178 20 70 61 72 73 65 72 0a 2a 2a 20 74 6f 20 68 61   parser.** to ha
2d179 6e 64 6c 65 20 55 50 44 41 54 45 20 73 74 61 74  ndle UPDATE stat
2d17a 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 24 49  ements..**.** $I
2d17b 64 3a 20 75 70 64 61 74 65 2e 63 2c 76 20 31 2e  d: update.c,v 1.
2d17c 32 30 37 20 32 30 30 39 2f 30 38 2f 30 38 20 31  207 2009/08/08 1
2d17d 38 3a 30 31 3a 30 38 20 64 72 68 20 45 78 70 20  8:01:08 drh Exp 
2d17e 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  $.*/..#ifndef SQ
2d17f 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2d180 4c 54 41 42 4c 45 0a 2f 2a 20 46 6f 72 77 61 72  LTABLE./* Forwar
2d181 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f  d declaration */
2d182 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 70 64  .static void upd
2d183 61 74 65 56 69 72 74 75 61 6c 54 61 62 6c 65 28  ateVirtualTable(
2d184 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2d185 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  ,       /* The p
2d186 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2d187 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  /.  SrcList *pSr
2d188 63 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  c,       /* The 
2d189 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74 6f  virtual table to
2d18a 20 62 65 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0a   be modified */.
2d18b 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
2d18c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69         /* The vi
2d18d 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20  rtual table */. 
2d18e 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e   ExprList *pChan
2d18f 67 65 73 2c 20 20 2f 2a 20 54 68 65 20 63 6f 6c  ges,  /* The col
2d190 75 6d 6e 73 20 74 6f 20 63 68 61 6e 67 65 20 69  umns to change i
2d191 6e 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61  n the UPDATE sta
2d192 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72  tement */.  Expr
2d193 20 2a 70 52 6f 77 69 64 45 78 70 72 2c 20 20 20   *pRowidExpr,   
2d194 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 75   /* Expression u
2d195 73 65 64 20 74 6f 20 72 65 63 6f 6d 70 75 74 65  sed to recompute
2d196 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20   the rowid */.  
2d197 69 6e 74 20 2a 61 58 52 65 66 2c 20 20 20 20 20  int *aXRef,     
2d198 20 20 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20       /* Mapping 
2d199 66 72 6f 6d 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  from columns of 
2d19a 70 54 61 62 20 74 6f 20 65 6e 74 72 69 65 73 20  pTab to entries 
2d19b 69 6e 20 70 43 68 61 6e 67 65 73 20 2a 2f 0a 20  in pChanges */. 
2d19c 20 45 78 70 72 20 2a 70 57 68 65 72 65 20 20 20   Expr *pWhere   
2d19d 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63        /* WHERE c
2d19e 6c 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 44  lause of the UPD
2d19f 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ATE statement */
2d1a0 0a 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .);.#endif /* SQ
2d1a1 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2d1a2 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  LTABLE */../*.**
2d1a3 20 54 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   The most recent
2d1a4 6c 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75 63  ly coded instruc
2d1a5 74 69 6f 6e 20 77 61 73 20 61 6e 20 4f 50 5f 43  tion was an OP_C
2d1a6 6f 6c 75 6d 6e 20 74 6f 20 72 65 74 72 69 65 76  olumn to retriev
2d1a7 65 20 74 68 65 0a 2a 2a 20 69 2d 74 68 20 63 6f  e the.** i-th co
2d1a8 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 70 54  lumn of table pT
2d1a9 61 62 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65  ab. This routine
2d1aa 20 73 65 74 73 20 74 68 65 20 50 34 20 70 61 72   sets the P4 par
2d1ab 61 6d 65 74 65 72 20 6f 66 20 74 68 65 20 0a 2a  ameter of the .*
2d1ac 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 74  * OP_Column to t
2d1ad 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
2d1ae 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20  , if any..**.** 
2d1af 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  The default valu
2d1b0 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 73  e of a column is
2d1b1 20 73 70 65 63 69 66 69 65 64 20 62 79 20 61 20   specified by a 
2d1b2 44 45 46 41 55 4c 54 20 63 6c 61 75 73 65 20 69  DEFAULT clause i
2d1b3 6e 20 74 68 65 20 0a 2a 2a 20 63 6f 6c 75 6d 6e  n the .** column
2d1b4 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20 54 68 69   definition. Thi
2d1b5 73 20 77 61 73 20 65 69 74 68 65 72 20 73 75 70  s was either sup
2d1b6 70 6c 69 65 64 20 62 79 20 74 68 65 20 75 73 65  plied by the use
2d1b7 72 20 77 68 65 6e 20 74 68 65 20 74 61 62 6c 65  r when the table
2d1b8 0a 2a 2a 20 77 61 73 20 63 72 65 61 74 65 64 2c  .** was created,
2d1b9 20 6f 72 20 61 64 64 65 64 20 6c 61 74 65 72 20   or added later 
2d1ba 74 6f 20 74 68 65 20 74 61 62 6c 65 20 64 65 66  to the table def
2d1bb 69 6e 69 74 69 6f 6e 20 62 79 20 61 6e 20 41 4c  inition by an AL
2d1bc 54 45 52 20 54 41 42 4c 45 0a 2a 2a 20 63 6f 6d  TER TABLE.** com
2d1bd 6d 61 6e 64 2e 20 49 66 20 74 68 65 20 6c 61 74  mand. If the lat
2d1be 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20 72 6f  ter, then the ro
2d1bf 77 2d 72 65 63 6f 72 64 73 20 69 6e 20 74 68 65  w-records in the
2d1c0 20 74 61 62 6c 65 20 62 74 72 65 65 20 6f 6e 20   table btree on 
2d1c1 64 69 73 6b 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20  disk.** may not 
2d1c2 63 6f 6e 74 61 69 6e 20 61 20 76 61 6c 75 65 20  contain a value 
2d1c3 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61  for the column a
2d1c4 6e 64 20 74 68 65 20 64 65 66 61 75 6c 74 20 76  nd the default v
2d1c5 61 6c 75 65 2c 20 74 61 6b 65 6e 0a 2a 2a 20 66  alue, taken.** f
2d1c6 72 6f 6d 20 74 68 65 20 50 34 20 70 61 72 61 6d  rom the P4 param
2d1c7 65 74 65 72 20 6f 66 20 74 68 65 20 4f 50 5f 43  eter of the OP_C
2d1c8 6f 6c 75 6d 6e 20 69 6e 73 74 72 75 63 74 69 6f  olumn instructio
2d1c9 6e 2c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  n, is returned i
2d1ca 6e 73 74 65 61 64 2e 0a 2a 2a 20 49 66 20 74 68  nstead..** If th
2d1cb 65 20 66 6f 72 6d 65 72 2c 20 74 68 65 6e 20 61  e former, then a
2d1cc 6c 6c 20 72 6f 77 2d 72 65 63 6f 72 64 73 20 61  ll row-records a
2d1cd 72 65 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  re guaranteed to
2d1ce 20 69 6e 63 6c 75 64 65 20 61 20 76 61 6c 75 65   include a value
2d1cf 0a 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 6c 75  .** for the colu
2d1d0 6d 6e 20 61 6e 64 20 74 68 65 20 50 34 20 76 61  mn and the P4 va
2d1d1 6c 75 65 20 69 73 20 6e 6f 74 20 72 65 71 75 69  lue is not requi
2d1d2 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6c 75 6d  red..**.** Colum
2d1d3 6e 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 63 72  n definitions cr
2d1d4 65 61 74 65 64 20 62 79 20 61 6e 20 41 4c 54 45  eated by an ALTE
2d1d5 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 20  R TABLE command 
2d1d6 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 0a 2a  may only have .*
2d1d7 2a 20 6c 69 74 65 72 61 6c 20 64 65 66 61 75 6c  * literal defaul
2d1d8 74 20 76 61 6c 75 65 73 20 73 70 65 63 69 66 69  t values specifi
2d1d9 65 64 3a 20 61 20 6e 75 6d 62 65 72 2c 20 6e 75  ed: a number, nu
2d1da 6c 6c 20 6f 72 20 61 20 73 74 72 69 6e 67 2e 20  ll or a string. 
2d1db 28 49 66 20 61 20 6d 6f 72 65 0a 2a 2a 20 63 6f  (If a more.** co
2d1dc 6d 70 6c 69 63 61 74 65 64 20 64 65 66 61 75 6c  mplicated defaul
2d1dd 74 20 65 78 70 72 65 73 73 69 6f 6e 20 76 61 6c  t expression val
2d1de 75 65 20 77 61 73 20 70 72 6f 76 69 64 65 64 2c  ue was provided,
2d1df 20 69 74 20 69 73 20 65 76 61 6c 75 61 74 65 64   it is evaluated
2d1e0 20 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 41 4c   .** when the AL
2d1e1 54 45 52 20 54 41 42 4c 45 20 69 73 20 65 78 65  TER TABLE is exe
2d1e2 63 75 74 65 64 20 61 6e 64 20 6f 6e 65 20 6f 66  cuted and one of
2d1e3 20 74 68 65 20 6c 69 74 65 72 61 6c 20 76 61 6c   the literal val
2d1e4 75 65 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69  ues written.** i
2d1e5 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 6d  nto the sqlite_m
2d1e6 61 73 74 65 72 20 74 61 62 6c 65 2e 29 0a 2a 2a  aster table.).**
2d1e7 0a 2a 2a 20 54 68 65 72 65 66 6f 72 65 2c 20 74  .** Therefore, t
2d1e8 68 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20  he P4 parameter 
2d1e9 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64  is only required
2d1ea 20 69 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   if the default 
2d1eb 76 61 6c 75 65 20 66 6f 72 0a 2a 2a 20 74 68 65  value for.** the
2d1ec 20 63 6f 6c 75 6d 6e 20 69 73 20 61 20 6c 69 74   column is a lit
2d1ed 65 72 61 6c 20 6e 75 6d 62 65 72 2c 20 73 74 72  eral number, str
2d1ee 69 6e 67 20 6f 72 20 6e 75 6c 6c 2e 20 54 68 65  ing or null. The
2d1ef 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f   sqlite3ValueFro
2d1f0 6d 45 78 70 72 28 29 0a 2a 2a 20 66 75 6e 63 74  mExpr().** funct
2d1f1 69 6f 6e 20 69 73 20 63 61 70 61 62 6c 65 20 6f  ion is capable o
2d1f2 66 20 74 72 61 6e 73 66 6f 72 6d 69 6e 67 20 74  f transforming t
2d1f3 68 65 73 65 20 74 79 70 65 73 20 6f 66 20 65 78  hese types of ex
2d1f4 70 72 65 73 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a  pressions into.*
2d1f5 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  * sqlite3_value 
2d1f6 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 49  objects..**.** I
2d1f7 66 20 70 61 72 61 6d 65 74 65 72 20 69 52 65 67  f parameter iReg
2d1f8 20 69 73 20 6e 6f 74 20 6e 65 67 61 74 69 76 65   is not negative
2d1f9 2c 20 63 6f 64 65 20 61 6e 20 4f 50 5f 52 65 61  , code an OP_Rea
2d1fa 6c 41 66 66 69 6e 69 74 79 20 69 6e 73 74 72 75  lAffinity instru
2d1fb 63 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 72 65 67 69  ction.** on regi
2d1fc 73 74 65 72 20 69 52 65 67 2e 20 54 68 69 73 20  ster iReg. This 
2d1fd 69 73 20 75 73 65 64 20 77 68 65 6e 20 61 6e 20  is used when an 
2d1fe 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 74 65 67  equivalent integ
2d1ff 65 72 20 76 61 6c 75 65 20 69 73 20 0a 2a 2a 20  er value is .** 
2d200 73 74 6f 72 65 64 20 69 6e 20 70 6c 61 63 65 20  stored in place 
2d201 6f 66 20 61 6e 20 38 2d 62 79 74 65 20 66 6c 6f  of an 8-byte flo
2d202 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75  ating point valu
2d203 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 61  e in order to sa
2d204 76 65 20 0a 2a 2a 20 73 70 61 63 65 2e 0a 2a 2f  ve .** space..*/
2d205 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2d206 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6c 75  void sqlite3Colu
2d207 6d 6e 44 65 66 61 75 6c 74 28 56 64 62 65 20 2a  mnDefault(Vdbe *
2d208 76 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  v, Table *pTab, 
2d209 69 6e 74 20 69 2c 20 69 6e 74 20 69 52 65 67 29  int i, int iReg)
2d20a 7b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  {.  assert( pTab
2d20b 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 70 54  !=0 );.  if( !pT
2d20c 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20  ab->pSelect ){. 
2d20d 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65     sqlite3_value
2d20e 20 2a 70 56 61 6c 75 65 3b 0a 20 20 20 20 75 38   *pValue;.    u8
2d20f 20 65 6e 63 20 3d 20 45 4e 43 28 73 71 6c 69 74   enc = ENC(sqlit
2d210 65 33 56 64 62 65 44 62 28 76 29 29 3b 0a 20 20  e3VdbeDb(v));.  
2d211 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d    Column *pCol =
2d212 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 3b   &pTab->aCol[i];
2d213 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
2d214 28 28 76 2c 20 22 25 73 2e 25 73 22 2c 20 70 54  ((v, "%s.%s", pT
2d215 61 62 2d 3e 7a 4e 61 6d 65 2c 20 70 43 6f 6c 2d  ab->zName, pCol-
2d216 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 61 73  >zName));.    as
2d217 73 65 72 74 28 20 69 3c 70 54 61 62 2d 3e 6e 43  sert( i<pTab->nC
2d218 6f 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ol );.    sqlite
2d219 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 73  3ValueFromExpr(s
2d21a 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 2c  qlite3VdbeDb(v),
2d21b 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 2c 20 65 6e   pCol->pDflt, en
2d21c 63 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  c, .            
2d21d 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f               pCo
2d21e 6c 2d 3e 61 66 66 69 6e 69 74 79 2c 20 26 70 56  l->affinity, &pV
2d21f 61 6c 75 65 29 3b 0a 20 20 20 20 69 66 28 20 70  alue);.    if( p
2d220 56 61 6c 75 65 20 29 7b 0a 20 20 20 20 20 20 73  Value ){.      s
2d221 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2d222 50 34 28 76 2c 20 2d 31 2c 20 28 63 6f 6e 73 74  P4(v, -1, (const
2d223 20 63 68 61 72 20 2a 29 70 56 61 6c 75 65 2c 20   char *)pValue, 
2d224 50 34 5f 4d 45 4d 29 3b 0a 20 20 20 20 7d 0a 23  P4_MEM);.    }.#
2d225 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2d226 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
2d227 54 0a 20 20 20 20 69 66 28 20 69 52 65 67 3e 3d  T.    if( iReg>=
2d228 30 20 26 26 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  0 && pTab->aCol[
2d229 69 5d 2e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c  i].affinity==SQL
2d22a 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 29 7b 0a  ITE_AFF_REAL ){.
2d22b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2d22c 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65  eAddOp1(v, OP_Re
2d22d 61 6c 41 66 66 69 6e 69 74 79 2c 20 69 52 65 67  alAffinity, iReg
2d22e 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
2d22f 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f    }.}../*.** Pro
2d230 63 65 73 73 20 61 6e 20 55 50 44 41 54 45 20 73  cess an UPDATE s
2d231 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  tatement..**.** 
2d232 20 20 55 50 44 41 54 45 20 4f 52 20 49 47 4e 4f    UPDATE OR IGNO
2d233 52 45 20 74 61 62 6c 65 5f 77 78 79 7a 20 53 45  RE table_wxyz SE
2d234 54 20 61 3d 62 2c 20 63 3d 64 20 57 48 45 52 45  T a=b, c=d WHERE
2d235 20 65 3c 35 20 41 4e 44 20 66 20 4e 4f 54 20 4e   e<5 AND f NOT N
2d236 55 4c 4c 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20  ULL;.**         
2d237 20 5c 5f 5f 5f 5f 5f 5f 5f 2f 20 5c 5f 5f 5f 5f   \_______/ \____
2d238 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f  ____/     \_____
2d239 5f 2f 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f  _/       \______
2d23a 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 20 20 20  __________/.*   
2d23b 20 20 20 20 20 20 20 20 20 6f 6e 45 72 72 6f 72           onError
2d23c 20 20 20 70 54 61 62 4c 69 73 74 20 20 20 20 20     pTabList     
2d23d 20 70 43 68 61 6e 67 65 73 20 20 20 20 20 20 20   pChanges       
2d23e 20 20 20 20 20 20 70 57 68 65 72 65 0a 2a 2f 0a        pWhere.*/.
2d23f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2d240 6f 69 64 20 73 71 6c 69 74 65 33 55 70 64 61 74  oid sqlite3Updat
2d241 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
2d242 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  se,         /* T
2d243 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78  he parser contex
2d244 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  t */.  SrcList *
2d245 70 54 61 62 4c 69 73 74 2c 20 20 20 20 20 2f 2a  pTabList,     /*
2d246 20 54 68 65 20 74 61 62 6c 65 20 69 6e 20 77 68   The table in wh
2d247 69 63 68 20 77 65 20 73 68 6f 75 6c 64 20 63 68  ich we should ch
2d248 61 6e 67 65 20 74 68 69 6e 67 73 20 2a 2f 0a 20  ange things */. 
2d249 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e   ExprList *pChan
2d24a 67 65 73 2c 20 20 20 20 2f 2a 20 54 68 69 6e 67  ges,    /* Thing
2d24b 73 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64 20  s to be changed 
2d24c 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72  */.  Expr *pWher
2d24d 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  e,          /* T
2d24e 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  he WHERE clause.
2d24f 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f    May be null */
2d250 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 20 20  .  int onError  
2d251 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77            /* How
2d252 20 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74   to handle const
2d253 72 61 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a  raint errors */.
2d254 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20  ){.  int i, j;  
2d255 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2d256 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a  oop counters */.
2d257 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
2d258 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2d259 74 61 62 6c 65 20 74 6f 20 62 65 20 75 70 64 61  table to be upda
2d25a 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ted */.  int add
2d25b 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  r = 0;          
2d25c 2f 2a 20 56 44 42 45 20 69 6e 73 74 72 75 63 74  /* VDBE instruct
2d25d 69 6f 6e 20 61 64 64 72 65 73 73 20 6f 66 20 74  ion address of t
2d25e 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
2d25f 6c 6f 6f 70 20 2a 2f 0a 20 20 57 68 65 72 65 49  loop */.  WhereI
2d260 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20  nfo *pWInfo;    
2d261 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
2d262 61 62 6f 75 74 20 74 68 65 20 57 48 45 52 45 20  about the WHERE 
2d263 63 6c 61 75 73 65 20 2a 2f 0a 20 20 56 64 62 65  clause */.  Vdbe
2d264 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20   *v;            
2d265 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
2d266 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e  l database engin
2d267 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49  e */.  Index *pI
2d268 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  dx;           /*
2d269 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
2d26a 72 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69  r indices */.  i
2d26b 6e 74 20 6e 49 64 78 3b 20 20 20 20 20 20 20 20  nt nIdx;        
2d26c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2d26d 6f 66 20 69 6e 64 69 63 65 73 20 74 68 61 74 20  of indices that 
2d26e 6e 65 65 64 20 75 70 64 61 74 69 6e 67 20 2a 2f  need updating */
2d26f 0a 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20  .  int iCur;    
2d270 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42            /* VDB
2d271 45 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  E Cursor number 
2d272 6f 66 20 70 54 61 62 20 2a 2f 0a 20 20 73 71 6c  of pTab */.  sql
2d273 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
2d274 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
2d275 61 73 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ase structure */
2d276 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78 20  .  int *aRegIdx 
2d277 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4f 6e 65  = 0;      /* One
2d278 20 72 65 67 69 73 74 65 72 20 61 73 73 69 67 6e   register assign
2d279 65 64 20 74 6f 20 65 61 63 68 20 69 6e 64 65 78  ed to each index
2d27a 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 2a   to be updated *
2d27b 2f 0a 20 20 69 6e 74 20 2a 61 58 52 65 66 20 3d  /.  int *aXRef =
2d27c 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 61 58   0;        /* aX
2d27d 52 65 66 5b 69 5d 20 69 73 20 74 68 65 20 69 6e  Ref[i] is the in
2d27e 64 65 78 20 69 6e 20 70 43 68 61 6e 67 65 73 2d  dex in pChanges-
2d27f 3e 61 5b 5d 20 6f 66 20 74 68 65 0a 20 20 20 20  >a[] of the.    
2d280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d281 20 20 20 20 20 2a 2a 20 61 6e 20 65 78 70 72 65       ** an expre
2d282 73 73 69 6f 6e 20 66 6f 72 20 74 68 65 20 69 2d  ssion for the i-
2d283 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  th column of the
2d284 20 74 61 62 6c 65 2e 0a 20 20 20 20 20 20 20 20   table..        
2d285 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d286 20 2a 2a 20 61 58 52 65 66 5b 69 5d 3d 3d 2d 31   ** aXRef[i]==-1
2d287 20 69 66 20 74 68 65 20 69 2d 74 68 20 63 6f 6c   if the i-th col
2d288 75 6d 6e 20 69 73 20 6e 6f 74 20 63 68 61 6e 67  umn is not chang
2d289 65 64 2e 20 2a 2f 0a 20 20 69 6e 74 20 63 68 6e  ed. */.  int chn
2d28a 67 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20  gRowid;         
2d28b 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 72  /* True if the r
2d28c 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20  ecord number is 
2d28d 62 65 69 6e 67 20 63 68 61 6e 67 65 64 20 2a 2f  being changed */
2d28e 0a 20 20 45 78 70 72 20 2a 70 52 6f 77 69 64 45  .  Expr *pRowidE
2d28f 78 70 72 20 3d 20 30 3b 20 20 2f 2a 20 45 78 70  xpr = 0;  /* Exp
2d290 72 65 73 73 69 6f 6e 20 64 65 66 69 6e 69 6e 67  ression defining
2d291 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20   the new record 
2d292 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20  number */.  int 
2d293 6f 70 65 6e 41 6c 6c 20 3d 20 30 3b 20 20 20 20  openAll = 0;    
2d294 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6c     /* True if al
2d295 6c 20 69 6e 64 69 63 65 73 20 6e 65 65 64 20 74  l indices need t
2d296 6f 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20  o be opened */. 
2d297 20 41 75 74 68 43 6f 6e 74 65 78 74 20 73 43 6f   AuthContext sCo
2d298 6e 74 65 78 74 3b 20 20 2f 2a 20 54 68 65 20 61  ntext;  /* The a
2d299 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e  uthorization con
2d29a 74 65 78 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f  text */.  NameCo
2d29b 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20  ntext sNC;      
2d29c 20 2f 2a 20 54 68 65 20 6e 61 6d 65 2d 63 6f 6e   /* The name-con
2d29d 74 65 78 74 20 74 6f 20 72 65 73 6f 6c 76 65 20  text to resolve 
2d29e 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 2a  expressions in *
2d29f 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
2d2a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
2d2a1 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
2d2a2 67 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  g the table bein
2d2a3 67 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 69  g updated */.  i
2d2a4 6e 74 20 6a 31 3b 20 20 20 20 20 20 20 20 20 20  nt j1;          
2d2a5 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
2d2a6 65 73 20 6f 66 20 6a 75 6d 70 20 69 6e 73 74 72  es of jump instr
2d2a7 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74  uctions */.  int
2d2a8 20 6f 6b 4f 6e 65 50 61 73 73 3b 20 20 20 20 20   okOnePass;     
2d2a9 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
2d2aa 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 74  one-pass algorit
2d2ab 68 6d 20 77 69 74 68 6f 75 74 20 74 68 65 20 46  hm without the F
2d2ac 49 46 4f 20 2a 2f 0a 20 20 69 6e 74 20 68 61 73  IFO */.  int has
2d2ad 46 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  FK;             
2d2ae 2f 2a 20 54 72 75 65 20 69 66 20 66 6f 72 65 69  /* True if forei
2d2af 67 6e 20 6b 65 79 20 70 72 6f 63 65 73 73 69 6e  gn key processin
2d2b0 67 20 69 73 20 72 65 71 75 69 72 65 64 20 2a 2f  g is required */
2d2b1 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2d2b2 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20  _OMIT_TRIGGER.  
2d2b3 69 6e 74 20 69 73 56 69 65 77 3b 20 20 20 20 20  int isView;     
2d2b4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d2b5 54 72 79 69 6e 67 20 74 6f 20 75 70 64 61 74 65  Trying to update
2d2b6 20 61 20 76 69 65 77 20 2a 2f 0a 20 20 54 72 69   a view */.  Tri
2d2b7 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20  gger *pTrigger; 
2d2b8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73            /* Lis
2d2b9 74 20 6f 66 20 74 72 69 67 67 65 72 73 20 6f 6e  t of triggers on
2d2ba 20 70 54 61 62 2c 20 69 66 20 72 65 71 75 69 72   pTab, if requir
2d2bb 65 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20  ed */.#endif..  
2d2bc 2f 2a 20 52 65 67 69 73 74 65 72 20 41 6c 6c 6f  /* Register Allo
2d2bd 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74  cations */.  int
2d2be 20 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20 30   regRowCount = 0
2d2bf 3b 20 20 20 2f 2a 20 41 20 63 6f 75 6e 74 20 6f  ;   /* A count o
2d2c0 66 20 72 6f 77 73 20 63 68 61 6e 67 65 64 20 2a  f rows changed *
2d2c1 2f 0a 20 20 69 6e 74 20 72 65 67 4f 6c 64 52 6f  /.  int regOldRo
2d2c2 77 69 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  wid;       /* Th
2d2c3 65 20 6f 6c 64 20 72 6f 77 69 64 20 2a 2f 0a 20  e old rowid */. 
2d2c4 20 69 6e 74 20 72 65 67 4e 65 77 52 6f 77 69 64   int regNewRowid
2d2c5 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e  ;       /* The n
2d2c6 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e  ew rowid */.  in
2d2c7 74 20 72 65 67 4e 65 77 3b 0a 20 20 69 6e 74 20  t regNew;.  int 
2d2c8 72 65 67 4f 6c 64 20 3d 20 30 3b 0a 20 20 69 6e  regOld = 0;.  in
2d2c9 74 20 72 65 67 52 6f 77 53 65 74 20 3d 20 30 3b  t regRowSet = 0;
2d2ca 20 20 20 20 20 2f 2a 20 52 6f 77 73 65 74 20 6f       /* Rowset o
2d2cb 66 20 72 6f 77 73 20 74 6f 20 62 65 20 75 70 64  f rows to be upd
2d2cc 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ated */.  int re
2d2cd 67 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20  gRec;           
2d2ce 20 2f 2a 20 52 65 67 69 73 74 65 72 20 75 73 65   /* Register use
2d2cf 64 20 66 6f 72 20 6e 65 77 20 74 61 62 6c 65 20  d for new table 
2d2d0 72 65 63 6f 72 64 20 74 6f 20 69 6e 73 65 72 74  record to insert
2d2d1 20 2a 2f 0a 0a 20 20 6d 65 6d 73 65 74 28 26 73   */..  memset(&s
2d2d2 43 6f 6e 74 65 78 74 2c 20 30 2c 20 73 69 7a 65  Context, 0, size
2d2d3 6f 66 28 73 43 6f 6e 74 65 78 74 29 29 3b 0a 20  of(sContext));. 
2d2d4 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
2d2d5 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
2d2d6 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  nErr || db->mall
2d2d7 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
2d2d8 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61  goto update_clea
2d2d9 6e 75 70 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  nup;.  }.  asser
2d2da 74 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72  t( pTabList->nSr
2d2db 63 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f  c==1 );..  /* Lo
2d2dc 63 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 77  cate the table w
2d2dd 68 69 63 68 20 77 65 20 77 61 6e 74 20 74 6f 20  hich we want to 
2d2de 75 70 64 61 74 65 2e 20 0a 20 20 2a 2f 0a 20 20  update. .  */.  
2d2df 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72  pTab = sqlite3Sr
2d2e0 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72  cListLookup(pPar
2d2e1 73 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20  se, pTabList);. 
2d2e2 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 67   if( pTab==0 ) g
2d2e3 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
2d2e4 75 70 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69  up;.  iDb = sqli
2d2e5 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
2d2e6 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61  (pParse->db, pTa
2d2e7 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 20 20  b->pSchema);..  
2d2e8 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 69 66  /* Figure out if
2d2e9 20 77 65 20 68 61 76 65 20 61 6e 79 20 74 72 69   we have any tri
2d2ea 67 67 65 72 73 20 61 6e 64 20 69 66 20 74 68 65  ggers and if the
2d2eb 20 74 61 62 6c 65 20 62 65 69 6e 67 0a 20 20 2a   table being.  *
2d2ec 2a 20 75 70 64 61 74 65 64 20 69 73 20 61 20 76  * updated is a v
2d2ed 69 65 77 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65  iew..  */.#ifnde
2d2ee 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
2d2ef 49 47 47 45 52 0a 20 20 70 54 72 69 67 67 65 72  IGGER.  pTrigger
2d2f0 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
2d2f1 72 73 45 78 69 73 74 28 70 50 61 72 73 65 2c 20  rsExist(pParse, 
2d2f2 70 54 61 62 2c 20 54 4b 5f 55 50 44 41 54 45 2c  pTab, TK_UPDATE,
2d2f3 20 70 43 68 61 6e 67 65 73 2c 20 30 29 3b 0a 20   pChanges, 0);. 
2d2f4 20 69 73 56 69 65 77 20 3d 20 70 54 61 62 2d 3e   isView = pTab->
2d2f5 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23 65 6c 73  pSelect!=0;.#els
2d2f6 65 0a 23 20 64 65 66 69 6e 65 20 70 54 72 69 67  e.# define pTrig
2d2f7 67 65 72 20 30 0a 23 20 64 65 66 69 6e 65 20 69  ger 0.# define i
2d2f8 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 23  sView 0.#endif.#
2d2f9 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
2d2fa 54 5f 56 49 45 57 0a 23 20 75 6e 64 65 66 20 69  T_VIEW.# undef i
2d2fb 73 56 69 65 77 0a 23 20 64 65 66 69 6e 65 20 69  sView.# define i
2d2fc 73 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 0a  sView 0.#endif..
2d2fd 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 69 65    if( sqlite3Vie
2d2fe 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
2d2ff 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b  pParse, pTab) ){
2d300 0a 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65  .    goto update
2d301 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20  _cleanup;.  }.  
2d302 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 65 61  if( sqlite3IsRea
2d303 64 4f 6e 6c 79 28 70 50 61 72 73 65 2c 20 70 54  dOnly(pParse, pT
2d304 61 62 2c 20 28 70 54 72 69 67 67 65 72 3f 31 3a  ab, (pTrigger?1:
2d305 30 29 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  0)) ){.    goto 
2d306 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
2d307 20 20 7d 0a 20 20 61 58 52 65 66 20 3d 20 73 71    }.  aXRef = sq
2d308 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
2d309 28 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29  (db, sizeof(int)
2d30a 20 2a 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b   * pTab->nCol );
2d30b 0a 20 20 69 66 28 20 61 58 52 65 66 3d 3d 30 20  .  if( aXRef==0 
2d30c 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c  ) goto update_cl
2d30d 65 61 6e 75 70 3b 0a 20 20 66 6f 72 28 69 3d 30  eanup;.  for(i=0
2d30e 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; i<pTab->nCol; 
2d30f 69 2b 2b 29 20 61 58 52 65 66 5b 69 5d 20 3d 20  i++) aXRef[i] = 
2d310 2d 31 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61  -1;..  /* Alloca
2d311 74 65 20 61 20 63 75 72 73 6f 72 73 20 66 6f 72  te a cursors for
2d312 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
2d313 73 65 20 74 61 62 6c 65 20 61 6e 64 20 66 6f 72  se table and for
2d314 20 61 6c 6c 20 69 6e 64 69 63 65 73 2e 0a 20 20   all indices..  
2d315 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 63 75 72  ** The index cur
2d316 73 6f 72 73 20 6d 69 67 68 74 20 6e 6f 74 20 62  sors might not b
2d317 65 20 75 73 65 64 2c 20 62 75 74 20 69 66 20 74  e used, but if t
2d318 68 65 79 20 61 72 65 20 75 73 65 64 20 74 68 65  hey are used the
2d319 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20 6f  y.  ** need to o
2d31a 63 63 75 72 20 72 69 67 68 74 20 61 66 74 65 72  ccur right after
2d31b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 75   the database cu
2d31c 72 73 6f 72 2e 20 20 53 6f 20 67 6f 20 61 68 65  rsor.  So go ahe
2d31d 61 64 20 61 6e 64 0a 20 20 2a 2a 20 61 6c 6c 6f  ad and.  ** allo
2d31e 63 61 74 65 20 65 6e 6f 75 67 68 20 73 70 61 63  cate enough spac
2d31f 65 2c 20 6a 75 73 74 20 69 6e 20 63 61 73 65 2e  e, just in case.
2d320 0a 20 20 2a 2f 0a 20 20 70 54 61 62 4c 69 73 74  .  */.  pTabList
2d321 2d 3e 61 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d  ->a[0].iCursor =
2d322 20 69 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e   iCur = pParse->
2d323 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f 72 28 70 49  nTab++;.  for(pI
2d324 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
2d325 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
2d326 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 50  ->pNext){.    pP
2d327 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
2d328 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  }..  /* Initiali
2d329 7a 65 20 74 68 65 20 6e 61 6d 65 2d 63 6f 6e 74  ze the name-cont
2d32a 65 78 74 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28  ext */.  memset(
2d32b 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28  &sNC, 0, sizeof(
2d32c 73 4e 43 29 29 3b 0a 20 20 73 4e 43 2e 70 50 61  sNC));.  sNC.pPa
2d32d 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20  rse = pParse;.  
2d32e 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70  sNC.pSrcList = p
2d32f 54 61 62 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 52  TabList;..  /* R
2d330 65 73 6f 6c 76 65 20 74 68 65 20 63 6f 6c 75 6d  esolve the colum
2d331 6e 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 74  n names in all t
2d332 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f  he expressions o
2d333 66 20 74 68 65 0a 20 20 2a 2a 20 6f 66 20 74 68  f the.  ** of th
2d334 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  e UPDATE stateme
2d335 6e 74 2e 20 20 41 6c 73 6f 20 66 69 6e 64 20 74  nt.  Also find t
2d336 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 0a  he column index.
2d337 20 20 2a 2a 20 66 6f 72 20 65 61 63 68 20 63 6f    ** for each co
2d338 6c 75 6d 6e 20 74 6f 20 62 65 20 75 70 64 61 74  lumn to be updat
2d339 65 64 20 69 6e 20 74 68 65 20 70 43 68 61 6e 67  ed in the pChang
2d33a 65 73 20 61 72 72 61 79 2e 20 20 46 6f 72 20 65  es array.  For e
2d33b 61 63 68 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20  ach.  ** column 
2d33c 74 6f 20 62 65 20 75 70 64 61 74 65 64 2c 20 6d  to be updated, m
2d33d 61 6b 65 20 73 75 72 65 20 77 65 20 68 61 76 65  ake sure we have
2d33e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 74   authorization t
2d33f 6f 20 63 68 61 6e 67 65 0a 20 20 2a 2a 20 74 68  o change.  ** th
2d340 61 74 20 63 6f 6c 75 6d 6e 2e 0a 20 20 2a 2f 0a  at column..  */.
2d341 20 20 63 68 6e 67 52 6f 77 69 64 20 3d 20 30 3b    chngRowid = 0;
2d342 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43  .  for(i=0; i<pC
2d343 68 61 6e 67 65 73 2d 3e 6e 45 78 70 72 3b 20 69  hanges->nExpr; i
2d344 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  ++){.    if( sql
2d345 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
2d346 61 6d 65 73 28 26 73 4e 43 2c 20 70 43 68 61 6e  ames(&sNC, pChan
2d347 67 65 73 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29  ges->a[i].pExpr)
2d348 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 75   ){.      goto u
2d349 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  pdate_cleanup;. 
2d34a 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 30     }.    for(j=0
2d34b 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; j<pTab->nCol; 
2d34c 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  j++){.      if( 
2d34d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
2d34e 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61  Tab->aCol[j].zNa
2d34f 6d 65 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b  me, pChanges->a[
2d350 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a  i].zName)==0 ){.
2d351 20 20 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 70          if( j==p
2d352 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20  Tab->iPKey ){.  
2d353 20 20 20 20 20 20 20 20 63 68 6e 67 52 6f 77 69          chngRowi
2d354 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  d = 1;.         
2d355 20 70 52 6f 77 69 64 45 78 70 72 20 3d 20 70 43   pRowidExpr = pC
2d356 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70 45 78  hanges->a[i].pEx
2d357 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  pr;.        }.  
2d358 20 20 20 20 20 20 61 58 52 65 66 5b 6a 5d 20 3d        aXRef[j] =
2d359 20 69 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61   i;.        brea
2d35a 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
2d35b 0a 20 20 20 20 69 66 28 20 6a 3e 3d 70 54 61 62  .    if( j>=pTab
2d35c 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20  ->nCol ){.      
2d35d 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f 77  if( sqlite3IsRow
2d35e 69 64 28 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69  id(pChanges->a[i
2d35f 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20  ].zName) ){.    
2d360 20 20 20 20 63 68 6e 67 52 6f 77 69 64 20 3d 20      chngRowid = 
2d361 31 3b 0a 20 20 20 20 20 20 20 20 70 52 6f 77 69  1;.        pRowi
2d362 64 45 78 70 72 20 3d 20 70 43 68 61 6e 67 65 73  dExpr = pChanges
2d363 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[i].pExpr;.  
2d364 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2d365 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2d366 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73  sg(pParse, "no s
2d367 75 63 68 20 63 6f 6c 75 6d 6e 3a 20 25 73 22 2c  uch column: %s",
2d368 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e   pChanges->a[i].
2d369 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  zName);.        
2d36a 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61  goto update_clea
2d36b 6e 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nup;.      }.   
2d36c 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
2d36d 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
2d36e 54 49 4f 4e 0a 20 20 20 20 7b 0a 20 20 20 20 20  TION.    {.     
2d36f 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 72   int rc;.      r
2d370 63 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43  c = sqlite3AuthC
2d371 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
2d372 49 54 45 5f 55 50 44 41 54 45 2c 20 70 54 61 62  ITE_UPDATE, pTab
2d373 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20  ->zName,.       
2d374 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d375 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a      pTab->aCol[j
2d376 5d 2e 7a 4e 61 6d 65 2c 20 64 62 2d 3e 61 44 62  ].zName, db->aDb
2d377 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20  [iDb].zName);.  
2d378 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
2d379 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 20  TE_DENY ){.     
2d37a 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63     goto update_c
2d37b 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 7d 65  leanup;.      }e
2d37c 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  lse if( rc==SQLI
2d37d 54 45 5f 49 47 4e 4f 52 45 20 29 7b 0a 20 20 20  TE_IGNORE ){.   
2d37e 20 20 20 20 20 61 58 52 65 66 5b 6a 5d 20 3d 20       aXRef[j] = 
2d37f 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  -1;.      }.    
2d380 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20  }.#endif.  }..  
2d381 68 61 73 46 4b 20 3d 20 73 71 6c 69 74 65 33 46  hasFK = sqlite3F
2d382 6b 52 65 71 75 69 72 65 64 28 70 50 61 72 73 65  kRequired(pParse
2d383 2c 20 70 54 61 62 2c 20 61 58 52 65 66 2c 20 63  , pTab, aXRef, c
2d384 68 6e 67 52 6f 77 69 64 29 3b 0a 0a 20 20 2f 2a  hngRowid);..  /*
2d385 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79   Allocate memory
2d386 20 66 6f 72 20 74 68 65 20 61 72 72 61 79 20 61   for the array a
2d387 52 65 67 49 64 78 5b 5d 2e 20 20 54 68 65 72 65  RegIdx[].  There
2d388 20 69 73 20 6f 6e 65 20 65 6e 74 72 79 20 69 6e   is one entry in
2d389 20 74 68 65 0a 20 20 2a 2a 20 61 72 72 61 79 20   the.  ** array 
2d38a 66 6f 72 20 65 61 63 68 20 69 6e 64 65 78 20 61  for each index a
2d38b 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
2d38c 61 62 6c 65 20 62 65 69 6e 67 20 75 70 64 61 74  able being updat
2d38d 65 64 2e 20 20 46 69 6c 6c 20 69 6e 0a 20 20 2a  ed.  Fill in.  *
2d38e 2a 20 74 68 65 20 76 61 6c 75 65 20 77 69 74 68  * the value with
2d38f 20 61 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62   a register numb
2d390 65 72 20 66 6f 72 20 69 6e 64 69 63 65 73 20 74  er for indices t
2d391 68 61 74 20 61 72 65 20 74 6f 20 62 65 20 75 73  hat are to be us
2d392 65 64 0a 20 20 2a 2a 20 61 6e 64 20 77 69 74 68  ed.  ** and with
2d393 20 7a 65 72 6f 20 66 6f 72 20 75 6e 75 73 65 64   zero for unused
2d394 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2f 0a 20   indices..  */. 
2d395 20 66 6f 72 28 6e 49 64 78 3d 30 2c 20 70 49 64   for(nIdx=0, pId
2d396 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
2d397 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
2d398 3e 70 4e 65 78 74 2c 20 6e 49 64 78 2b 2b 29 7b  >pNext, nIdx++){
2d399 7d 0a 20 20 69 66 28 20 6e 49 64 78 3e 30 20 29  }.  if( nIdx>0 )
2d39a 7b 0a 20 20 20 20 61 52 65 67 49 64 78 20 3d 20  {.    aRegIdx = 
2d39b 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
2d39c 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 49 6e  aw(db, sizeof(In
2d39d 64 65 78 2a 29 20 2a 20 6e 49 64 78 20 29 3b 0a  dex*) * nIdx );.
2d39e 20 20 20 20 69 66 28 20 61 52 65 67 49 64 78 3d      if( aRegIdx=
2d39f 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65  =0 ) goto update
2d3a0 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20  _cleanup;.  }.  
2d3a1 66 6f 72 28 6a 3d 30 2c 20 70 49 64 78 3d 70 54  for(j=0, pIdx=pT
2d3a2 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78  ab->pIndex; pIdx
2d3a3 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65  ; pIdx=pIdx->pNe
2d3a4 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 69 6e  xt, j++){.    in
2d3a5 74 20 72 65 67 3b 0a 20 20 20 20 69 66 28 20 63  t reg;.    if( c
2d3a6 68 6e 67 52 6f 77 69 64 20 29 7b 0a 20 20 20 20  hngRowid ){.    
2d3a7 20 20 72 65 67 20 3d 20 2b 2b 70 50 61 72 73 65    reg = ++pParse
2d3a8 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d 65 6c 73  ->nMem;.    }els
2d3a9 65 7b 0a 20 20 20 20 20 20 72 65 67 20 3d 20 30  e{.      reg = 0
2d3aa 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
2d3ab 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   i<pIdx->nColumn
2d3ac 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
2d3ad 69 66 28 20 61 58 52 65 66 5b 70 49 64 78 2d 3e  if( aXRef[pIdx->
2d3ae 61 69 43 6f 6c 75 6d 6e 5b 69 5d 5d 3e 3d 30 20  aiColumn[i]]>=0 
2d3af 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 67  ){.          reg
2d3b0 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
2d3b1 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  m;.          bre
2d3b2 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
2d3b3 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2d3b4 61 52 65 67 49 64 78 5b 6a 5d 20 3d 20 72 65 67  aRegIdx[j] = reg
2d3b5 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69  ;.  }..  /* Begi
2d3b6 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64  n generating cod
2d3b7 65 2e 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69  e. */.  v = sqli
2d3b8 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
2d3b9 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  e);.  if( v==0 )
2d3ba 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65   goto update_cle
2d3bb 61 6e 75 70 3b 0a 20 20 69 66 28 20 70 50 61 72  anup;.  if( pPar
2d3bc 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 20  se->nested==0 ) 
2d3bd 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74  sqlite3VdbeCount
2d3be 43 68 61 6e 67 65 73 28 76 29 3b 0a 20 20 73 71  Changes(v);.  sq
2d3bf 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
2d3c0 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c  peration(pParse,
2d3c1 20 31 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64   1, iDb);..#ifnd
2d3c2 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2d3c3 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 2f 2a  IRTUALTABLE.  /*
2d3c4 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   Virtual tables 
2d3c5 6d 75 73 74 20 62 65 20 68 61 6e 64 6c 65 64 20  must be handled 
2d3c6 73 65 70 61 72 61 74 65 6c 79 20 2a 2f 0a 20 20  separately */.  
2d3c7 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
2d3c8 61 62 29 20 29 7b 0a 20 20 20 20 75 70 64 61 74  ab) ){.    updat
2d3c9 65 56 69 72 74 75 61 6c 54 61 62 6c 65 28 70 50  eVirtualTable(pP
2d3ca 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20  arse, pTabList, 
2d3cb 70 54 61 62 2c 20 70 43 68 61 6e 67 65 73 2c 20  pTab, pChanges, 
2d3cc 70 52 6f 77 69 64 45 78 70 72 2c 20 61 58 52 65  pRowidExpr, aXRe
2d3cd 66 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  f,.             
2d3ce 20 20 20 20 20 20 20 20 20 20 70 57 68 65 72 65            pWhere
2d3cf 29 3b 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20  );.    pWhere = 
2d3d0 30 3b 0a 20 20 20 20 70 54 61 62 4c 69 73 74 20  0;.    pTabList 
2d3d1 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20 75 70  = 0;.    goto up
2d3d2 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  date_cleanup;.  
2d3d3 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41  }.#endif..  /* A
2d3d4 6c 6c 6f 63 61 74 65 20 72 65 71 75 69 72 65 64  llocate required
2d3d5 20 72 65 67 69 73 74 65 72 73 2e 20 2a 2f 0a 20   registers. */. 
2d3d6 20 72 65 67 4f 6c 64 52 6f 77 69 64 20 3d 20 72   regOldRowid = r
2d3d7 65 67 4e 65 77 52 6f 77 69 64 20 3d 20 2b 2b 70  egNewRowid = ++p
2d3d8 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 69  Parse->nMem;.  i
2d3d9 66 28 20 70 54 72 69 67 67 65 72 20 7c 7c 20 68  f( pTrigger || h
2d3da 61 73 46 4b 20 29 7b 0a 20 20 20 20 72 65 67 4f  asFK ){.    regO
2d3db 6c 64 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65  ld = pParse->nMe
2d3dc 6d 20 2b 20 31 3b 0a 20 20 20 20 70 50 61 72 73  m + 1;.    pPars
2d3dd 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62 2d  e->nMem += pTab-
2d3de 3e 6e 43 6f 6c 3b 0a 20 20 7d 0a 20 20 69 66 28  >nCol;.  }.  if(
2d3df 20 63 68 6e 67 52 6f 77 69 64 20 7c 7c 20 70 54   chngRowid || pT
2d3e0 72 69 67 67 65 72 20 7c 7c 20 68 61 73 46 4b 20  rigger || hasFK 
2d3e1 29 7b 0a 20 20 20 20 72 65 67 4e 65 77 52 6f 77  ){.    regNewRow
2d3e2 69 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  id = ++pParse->n
2d3e3 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72 65 67 4e 65  Mem;.  }.  regNe
2d3e4 77 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  w = pParse->nMem
2d3e5 20 2b 20 31 3b 0a 20 20 70 50 61 72 73 65 2d 3e   + 1;.  pParse->
2d3e6 6e 4d 65 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e 43  nMem += pTab->nC
2d3e7 6f 6c 3b 0a 20 20 72 65 67 52 65 63 20 3d 20 2b  ol;.  regRec = +
2d3e8 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 0a  +pParse->nMem;..
2d3e9 20 20 2f 2a 20 53 74 61 72 74 20 74 68 65 20 76    /* Start the v
2d3ea 69 65 77 20 63 6f 6e 74 65 78 74 2e 20 2a 2f 0a  iew context. */.
2d3eb 20 20 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a    if( isView ){.
2d3ec 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 68 43      sqlite3AuthC
2d3ed 6f 6e 74 65 78 74 50 75 73 68 28 70 50 61 72 73  ontextPush(pPars
2d3ee 65 2c 20 26 73 43 6f 6e 74 65 78 74 2c 20 70 54  e, &sContext, pT
2d3ef 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a  ab->zName);.  }.
2d3f0 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20  .  /* If we are 
2d3f1 74 72 79 69 6e 67 20 74 6f 20 75 70 64 61 74 65  trying to update
2d3f2 20 61 20 76 69 65 77 2c 20 72 65 61 6c 69 7a 65   a view, realize
2d3f3 20 74 68 61 74 20 76 69 65 77 20 69 6e 74 6f 0a   that view into.
2d3f4 20 20 2a 2a 20 61 20 65 70 68 65 6d 65 72 61 6c    ** a ephemeral
2d3f5 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23 69 66   table..  */.#if
2d3f6 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2d3f7 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 20 21  _OMIT_VIEW) && !
2d3f8 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2d3f9 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20 69  MIT_TRIGGER).  i
2d3fa 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20  f( isView ){.   
2d3fb 20 73 71 6c 69 74 65 33 4d 61 74 65 72 69 61 6c   sqlite3Material
2d3fc 69 7a 65 56 69 65 77 28 70 50 61 72 73 65 2c 20  izeView(pParse, 
2d3fd 70 54 61 62 2c 20 70 57 68 65 72 65 2c 20 69 43  pTab, pWhere, iC
2d3fe 75 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ur);.  }.#endif.
2d3ff 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68  .  /* Resolve th
2d400 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69  e column names i
2d401 6e 20 61 6c 6c 20 74 68 65 20 65 78 70 72 65 73  n all the expres
2d402 73 69 6f 6e 73 20 69 6e 20 74 68 65 0a 20 20 2a  sions in the.  *
2d403 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a  * WHERE clause..
2d404 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
2d405 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d  e3ResolveExprNam
2d406 65 73 28 26 73 4e 43 2c 20 70 57 68 65 72 65 29  es(&sNC, pWhere)
2d407 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 75 70 64   ){.    goto upd
2d408 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ate_cleanup;.  }
2d409 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 74 68 65  ..  /* Begin the
2d40a 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 0a 20   database scan. 
2d40b 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
2d40c 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
2d40d 6c 6c 2c 20 30 2c 20 72 65 67 4f 6c 64 52 6f 77  ll, 0, regOldRow
2d40e 69 64 29 3b 0a 20 20 70 57 49 6e 66 6f 20 3d 20  id);.  pWInfo = 
2d40f 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69  sqlite3WhereBegi
2d410 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69  n(pParse, pTabLi
2d411 73 74 2c 20 70 57 68 65 72 65 2c 30 2c 20 57 48  st, pWhere,0, WH
2d412 45 52 45 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49  ERE_ONEPASS_DESI
2d413 52 45 44 29 3b 0a 20 20 69 66 28 20 70 57 49 6e  RED);.  if( pWIn
2d414 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70 64  fo==0 ) goto upd
2d415 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 6f  ate_cleanup;.  o
2d416 6b 4f 6e 65 50 61 73 73 20 3d 20 70 57 49 6e 66  kOnePass = pWInf
2d417 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 3b 0a 0a 20  o->okOnePass;.. 
2d418 20 2f 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 65   /* Remember the
2d419 20 72 6f 77 69 64 20 6f 66 20 65 76 65 72 79 20   rowid of every 
2d41a 69 74 65 6d 20 74 6f 20 62 65 20 75 70 64 61 74  item to be updat
2d41b 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ed..  */.  sqlit
2d41c 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2d41d 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c 20  OP_Rowid, iCur, 
2d41e 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20  regOldRowid);.  
2d41f 69 66 28 20 21 6f 6b 4f 6e 65 50 61 73 73 20 29  if( !okOnePass )
2d420 7b 0a 20 20 20 20 72 65 67 52 6f 77 53 65 74 20  {.    regRowSet 
2d421 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2d422 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2d423 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f  eAddOp2(v, OP_Ro
2d424 77 53 65 74 41 64 64 2c 20 72 65 67 52 6f 77 53  wSetAdd, regRowS
2d425 65 74 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29  et, regOldRowid)
2d426 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 64 20  ;.  }..  /* End 
2d427 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 61  the database sca
2d428 6e 20 6c 6f 6f 70 2e 0a 20 20 2a 2f 0a 20 20 73  n loop..  */.  s
2d429 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70  qlite3WhereEnd(p
2d42a 57 49 6e 66 6f 29 3b 0a 0a 20 20 2f 2a 20 49 6e  WInfo);..  /* In
2d42b 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 75  itialize the cou
2d42c 6e 74 20 6f 66 20 75 70 64 61 74 65 64 20 72 6f  nt of updated ro
2d42d 77 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 64  ws.  */.  if( (d
2d42e 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
2d42f 45 5f 43 6f 75 6e 74 52 6f 77 73 29 20 26 26 20  E_CountRows) && 
2d430 21 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65  !pParse->pTrigge
2d431 72 54 61 62 20 29 7b 0a 20 20 20 20 72 65 67 52  rTab ){.    regR
2d432 6f 77 43 6f 75 6e 74 20 3d 20 2b 2b 70 50 61 72  owCount = ++pPar
2d433 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71  se->nMem;.    sq
2d434 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2d435 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
2d436 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74 29 3b 0a  , regRowCount);.
2d437 20 20 7d 0a 0a 20 20 69 66 28 20 21 69 73 56 69    }..  if( !isVi
2d438 65 77 20 29 7b 0a 20 20 20 20 2f 2a 20 0a 20 20  ew ){.    /* .  
2d439 20 20 2a 2a 20 4f 70 65 6e 20 65 76 65 72 79 20    ** Open every 
2d43a 69 6e 64 65 78 20 74 68 61 74 20 6e 65 65 64 73  index that needs
2d43b 20 75 70 64 61 74 69 6e 67 2e 20 20 4e 6f 74 65   updating.  Note
2d43c 20 74 68 61 74 20 69 66 20 61 6e 79 0a 20 20 20   that if any.   
2d43d 20 2a 2a 20 69 6e 64 65 78 20 63 6f 75 6c 64 20   ** index could 
2d43e 70 6f 74 65 6e 74 69 61 6c 6c 79 20 69 6e 76 6f  potentially invo
2d43f 6b 65 20 61 20 52 45 50 4c 41 43 45 20 63 6f 6e  ke a REPLACE con
2d440 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e  flict resolution
2d441 20 0a 20 20 20 20 2a 2a 20 61 63 74 69 6f 6e 2c   .    ** action,
2d442 20 74 68 65 6e 20 77 65 20 6e 65 65 64 20 74 6f   then we need to
2d443 20 6f 70 65 6e 20 61 6c 6c 20 69 6e 64 69 63 65   open all indice
2d444 73 20 62 65 63 61 75 73 65 20 77 65 20 6d 69 67  s because we mig
2d445 68 74 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20 74  ht need.    ** t
2d446 6f 20 62 65 20 64 65 6c 65 74 69 6e 67 20 73 6f  o be deleting so
2d447 6d 65 20 72 65 63 6f 72 64 73 2e 0a 20 20 20 20  me records..    
2d448 2a 2f 0a 20 20 20 20 69 66 28 20 21 6f 6b 4f 6e  */.    if( !okOn
2d449 65 50 61 73 73 20 29 20 73 71 6c 69 74 65 33 4f  ePass ) sqlite3O
2d44a 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c  penTable(pParse,
2d44b 20 69 43 75 72 2c 20 69 44 62 2c 20 70 54 61 62   iCur, iDb, pTab
2d44c 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b  , OP_OpenWrite);
2d44d 20 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f   .    if( onErro
2d44e 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 7b  r==OE_Replace ){
2d44f 0a 20 20 20 20 20 20 6f 70 65 6e 41 6c 6c 20 3d  .      openAll =
2d450 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   1;.    }else{. 
2d451 20 20 20 20 20 6f 70 65 6e 41 6c 6c 20 3d 20 30       openAll = 0
2d452 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64 78  ;.      for(pIdx
2d453 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
2d454 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
2d455 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20  pNext){.        
2d456 69 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f  if( pIdx->onErro
2d457 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 7b  r==OE_Replace ){
2d458 0a 20 20 20 20 20 20 20 20 20 20 6f 70 65 6e 41  .          openA
2d459 6c 6c 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ll = 1;.        
2d45a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
2d45b 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
2d45c 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49  .    for(i=0, pI
2d45d 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
2d45e 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
2d45f 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b 29 7b 0a 20  ->pNext, i++){. 
2d460 20 20 20 20 20 69 66 28 20 6f 70 65 6e 41 6c 6c       if( openAll
2d461 20 7c 7c 20 61 52 65 67 49 64 78 5b 69 5d 3e 30   || aRegIdx[i]>0
2d462 20 29 7b 0a 20 20 20 20 20 20 20 20 4b 65 79 49   ){.        KeyI
2d463 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69  nfo *pKey = sqli
2d464 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28  te3IndexKeyinfo(
2d465 70 50 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20  pParse, pIdx);. 
2d466 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2d467 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
2d468 70 65 6e 57 72 69 74 65 2c 20 69 43 75 72 2b 69  penWrite, iCur+i
2d469 2b 31 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20  +1, pIdx->tnum, 
2d46a 69 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20  iDb,.           
2d46b 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61              (cha
2d46c 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49  r*)pKey, P4_KEYI
2d46d 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20  NFO_HANDOFF);.  
2d46e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
2d46f 61 72 73 65 2d 3e 6e 54 61 62 3e 69 43 75 72 2b  arse->nTab>iCur+
2d470 69 2b 31 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20  i+1 );.      }. 
2d471 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54     }.  }..  /* T
2d472 6f 70 20 6f 66 20 74 68 65 20 75 70 64 61 74 65  op of the update
2d473 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 66 28 20 6f   loop */.  if( o
2d474 6b 4f 6e 65 50 61 73 73 20 29 7b 0a 20 20 20 20  kOnePass ){.    
2d475 69 6e 74 20 61 31 20 3d 20 73 71 6c 69 74 65 33  int a1 = sqlite3
2d476 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2d477 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 4f 6c 64  _NotNull, regOld
2d478 52 6f 77 69 64 29 3b 0a 20 20 20 20 61 64 64 72  Rowid);.    addr
2d479 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2d47a 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29  dOp0(v, OP_Goto)
2d47b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2d47c 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 31 29  eJumpHere(v, a1)
2d47d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
2d47e 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
2d47f 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52 6f  eAddOp3(v, OP_Ro
2d480 77 53 65 74 52 65 61 64 2c 20 72 65 67 52 6f 77  wSetRead, regRow
2d481 53 65 74 2c 20 30 2c 20 72 65 67 4f 6c 64 52 6f  Set, 0, regOldRo
2d482 77 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  wid);.  }..  /* 
2d483 4d 61 6b 65 20 63 75 72 73 6f 72 20 69 43 75 72  Make cursor iCur
2d484 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 72 65   point to the re
2d485 63 6f 72 64 20 74 68 61 74 20 69 73 20 62 65 69  cord that is bei
2d486 6e 67 20 75 70 64 61 74 65 64 2e 20 49 66 0a 20  ng updated. If. 
2d487 20 2a 2a 20 74 68 69 73 20 72 65 63 6f 72 64 20   ** this record 
2d488 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 66  does not exist f
2d489 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 20 28  or some reason (
2d48a 64 65 6c 65 74 65 64 20 62 79 20 61 20 74 72 69  deleted by a tri
2d48b 67 67 65 72 2c 0a 20 20 2a 2a 20 66 6f 72 20 65  gger,.  ** for e
2d48c 78 61 6d 70 6c 65 2c 20 74 68 65 6e 20 6a 75 6d  xample, then jum
2d48d 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 74  p to the next it
2d48e 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 52  eration of the R
2d48f 6f 77 53 65 74 20 6c 6f 6f 70 2e 20 20 2a 2f 0a  owSet loop.  */.
2d490 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2d491 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69  Op3(v, OP_NotExi
2d492 73 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72 2c  sts, iCur, addr,
2d493 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 0a   regOldRowid);..
2d494 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65 63 6f    /* If the reco
2d495 72 64 20 6e 75 6d 62 65 72 20 77 69 6c 6c 20 63  rd number will c
2d496 68 61 6e 67 65 2c 20 73 65 74 20 72 65 67 69 73  hange, set regis
2d497 74 65 72 20 72 65 67 4e 65 77 52 6f 77 69 64 20  ter regNewRowid 
2d498 74 6f 0a 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 20  to.  ** contain 
2d499 74 68 65 20 6e 65 77 20 76 61 6c 75 65 2e 20 49  the new value. I
2d49a 66 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d  f the record num
2d49b 62 65 72 20 69 73 20 6e 6f 74 20 62 65 69 6e 67  ber is not being
2d49c 20 6d 6f 64 69 66 69 65 64 2c 0a 20 20 2a 2a 20   modified,.  ** 
2d49d 74 68 65 6e 20 72 65 67 4e 65 77 52 6f 77 69 64  then regNewRowid
2d49e 20 69 73 20 74 68 65 20 73 61 6d 65 20 72 65 67   is the same reg
2d49f 69 73 74 65 72 20 61 73 20 72 65 67 4f 6c 64 52  ister as regOldR
2d4a0 6f 77 69 64 2c 20 77 68 69 63 68 20 69 73 0a 20  owid, which is. 
2d4a1 20 2a 2a 20 61 6c 72 65 61 64 79 20 70 6f 70 75   ** already popu
2d4a2 6c 61 74 65 64 2e 20 20 2a 2f 0a 20 20 61 73 73  lated.  */.  ass
2d4a3 65 72 74 28 20 63 68 6e 67 52 6f 77 69 64 20 7c  ert( chngRowid |
2d4a4 7c 20 70 54 72 69 67 67 65 72 20 7c 7c 20 68 61  | pTrigger || ha
2d4a5 73 46 4b 20 7c 7c 20 72 65 67 4f 6c 64 52 6f 77  sFK || regOldRow
2d4a6 69 64 3d 3d 72 65 67 4e 65 77 52 6f 77 69 64 20  id==regNewRowid 
2d4a7 29 3b 0a 20 20 69 66 28 20 63 68 6e 67 52 6f 77  );.  if( chngRow
2d4a8 69 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  id ){.    sqlite
2d4a9 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
2d4aa 2c 20 70 52 6f 77 69 64 45 78 70 72 2c 20 72 65  , pRowidExpr, re
2d4ab 67 4e 65 77 52 6f 77 69 64 29 3b 0a 20 20 20 20  gNewRowid);.    
2d4ac 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2d4ad 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e  1(v, OP_MustBeIn
2d4ae 74 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29 3b  t, regNewRowid);
2d4af 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
2d4b0 65 72 65 20 61 72 65 20 74 72 69 67 67 65 72 73  ere are triggers
2d4b1 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2c 20   on this table, 
2d4b2 70 6f 70 75 6c 61 74 65 20 61 6e 20 61 72 72 61  populate an arra
2d4b3 79 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 0a  y of registers .
2d4b4 20 20 2a 2a 20 77 69 74 68 20 74 68 65 20 72 65    ** with the re
2d4b5 71 75 69 72 65 64 20 6f 6c 64 2e 2a 20 63 6f 6c  quired old.* col
2d4b6 75 6d 6e 20 64 61 74 61 2e 20 20 2a 2f 0a 20 20  umn data.  */.  
2d4b7 69 66 28 20 68 61 73 46 4b 20 7c 7c 20 70 54 72  if( hasFK || pTr
2d4b8 69 67 67 65 72 20 29 7b 0a 20 20 20 20 75 33 32  igger ){.    u32
2d4b9 20 6f 6c 64 6d 61 73 6b 20 3d 20 28 68 61 73 46   oldmask = (hasF
2d4ba 4b 20 3f 20 73 71 6c 69 74 65 33 46 6b 4f 6c 64  K ? sqlite3FkOld
2d4bb 6d 61 73 6b 28 70 50 61 72 73 65 2c 20 70 54 61  mask(pParse, pTa
2d4bc 62 29 20 3a 20 30 29 3b 0a 20 20 20 20 6f 6c 64  b) : 0);.    old
2d4bd 6d 61 73 6b 20 7c 3d 20 73 71 6c 69 74 65 33 54  mask |= sqlite3T
2d4be 72 69 67 67 65 72 4f 6c 64 6d 61 73 6b 28 70 50  riggerOldmask(pP
2d4bf 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20  arse, pTrigger, 
2d4c0 70 43 68 61 6e 67 65 73 2c 20 70 54 61 62 2c 20  pChanges, pTab, 
2d4c1 6f 6e 45 72 72 6f 72 29 3b 0a 20 20 20 20 66 6f  onError);.    fo
2d4c2 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e  r(i=0; i<pTab->n
2d4c3 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
2d4c4 20 69 66 28 20 61 58 52 65 66 5b 69 5d 3c 30 20   if( aXRef[i]<0 
2d4c5 7c 7c 20 6f 6c 64 6d 61 73 6b 3d 3d 30 78 66 66  || oldmask==0xff
2d4c6 66 66 66 66 66 66 20 7c 7c 20 28 6f 6c 64 6d 61  ffffff || (oldma
2d4c7 73 6b 20 26 20 28 31 3c 3c 69 29 29 20 29 7b 0a  sk & (1<<i)) ){.
2d4c8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2d4c9 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2d4ca 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69 2c  Column, iCur, i,
2d4cb 20 72 65 67 4f 6c 64 2b 69 29 3b 0a 20 20 20 20   regOld+i);.    
2d4cc 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d      sqlite3Colum
2d4cd 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62  nDefault(v, pTab
2d4ce 2c 20 69 2c 20 72 65 67 4f 6c 64 2b 69 29 3b 0a  , i, regOld+i);.
2d4cf 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2d4d0 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2d4d1 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
2d4d2 6c 2c 20 30 2c 20 72 65 67 4f 6c 64 2b 69 29 3b  l, 0, regOld+i);
2d4d3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2d4d4 20 20 20 69 66 28 20 63 68 6e 67 52 6f 77 69 64     if( chngRowid
2d4d5 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
2d4d6 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2d4d7 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 65 67 4f 6c  , OP_Copy, regOl
2d4d8 64 52 6f 77 69 64 2c 20 72 65 67 4e 65 77 52 6f  dRowid, regNewRo
2d4d9 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  wid);.    }.  }.
2d4da 0a 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74  .  /* Populate t
2d4db 68 65 20 61 72 72 61 79 20 6f 66 20 72 65 67 69  he array of regi
2d4dc 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20  sters beginning 
2d4dd 61 74 20 72 65 67 4e 65 77 20 77 69 74 68 20 74  at regNew with t
2d4de 68 65 20 6e 65 77 0a 20 20 2a 2a 20 72 6f 77 20  he new.  ** row 
2d4df 64 61 74 61 2e 20 54 68 69 73 20 61 72 72 61 79  data. This array
2d4e0 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 65 63   is used to chec
2d4e1 6b 20 63 6f 6e 73 74 61 69 6e 74 73 2c 20 63 72  k constaints, cr
2d4e2 65 61 74 65 20 74 68 65 20 6e 65 77 0a 20 20 2a  eate the new.  *
2d4e3 2a 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65  * table and inde
2d4e4 78 20 72 65 63 6f 72 64 73 2c 20 61 6e 64 20 61  x records, and a
2d4e5 73 20 74 68 65 20 76 61 6c 75 65 73 20 66 6f 72  s the values for
2d4e6 20 61 6e 79 20 6e 65 77 2e 2a 20 72 65 66 65 72   any new.* refer
2d4e7 65 6e 63 65 73 0a 20 20 2a 2a 20 6d 61 64 65 20  ences.  ** made 
2d4e8 62 79 20 74 72 69 67 67 65 72 73 2e 20 20 2a 2f  by triggers.  */
2d4e9 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54  .  for(i=0; i<pT
2d4ea 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ab->nCol; i++){.
2d4eb 20 20 20 20 69 66 28 20 69 3d 3d 70 54 61 62 2d      if( i==pTab-
2d4ec 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  >iPKey ){.      
2d4ed 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2d4ee 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
2d4ef 20 72 65 67 4e 65 77 2b 69 29 3b 0a 20 20 20 20   regNew+i);.    
2d4f0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6a 20 3d  }else{.      j =
2d4f1 20 61 58 52 65 66 5b 69 5d 3b 0a 20 20 20 20 20   aXRef[i];.     
2d4f2 20 69 66 28 20 6a 3c 30 20 29 7b 0a 20 20 20 20   if( j<0 ){.    
2d4f3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2d4f4 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
2d4f5 6d 6e 2c 20 69 43 75 72 2c 20 69 2c 20 72 65 67  mn, iCur, i, reg
2d4f6 4e 65 77 2b 69 29 3b 0a 20 20 20 20 20 20 20 20  New+i);.        
2d4f7 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66  sqlite3ColumnDef
2d4f8 61 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 2c  ault(v, pTab, i,
2d4f9 20 72 65 67 4e 65 77 2b 69 29 3b 0a 20 20 20 20   regNew+i);.    
2d4fa 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2d4fb 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
2d4fc 28 70 50 61 72 73 65 2c 20 70 43 68 61 6e 67 65  (pParse, pChange
2d4fd 73 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 72  s->a[j].pExpr, r
2d4fe 65 67 4e 65 77 2b 69 29 3b 0a 20 20 20 20 20 20  egNew+i);.      
2d4ff 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
2d500 2a 20 46 69 72 65 20 61 6e 79 20 42 45 46 4f 52  * Fire any BEFOR
2d501 45 20 55 50 44 41 54 45 20 74 72 69 67 67 65 72  E UPDATE trigger
2d502 73 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73 20  s. This happens 
2d503 62 65 66 6f 72 65 20 63 6f 6e 73 74 72 61 69 6e  before constrain
2d504 74 73 20 61 72 65 0a 20 20 2a 2a 20 76 65 72 69  ts are.  ** veri
2d505 66 69 65 64 2e 20 4f 6e 65 20 63 6f 75 6c 64 20  fied. One could 
2d506 61 72 67 75 65 20 74 68 61 74 20 74 68 69 73 20  argue that this 
2d507 69 73 20 77 72 6f 6e 67 2e 20 20 2a 2f 0a 20 20  is wrong.  */.  
2d508 69 66 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a  if( pTrigger ){.
2d509 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2d50a 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 66 66 69  ddOp2(v, OP_Affi
2d50b 6e 69 74 79 2c 20 72 65 67 4e 65 77 2c 20 70 54  nity, regNew, pT
2d50c 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 73  ab->nCol);.    s
2d50d 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e  qlite3TableAffin
2d50e 69 74 79 53 74 72 28 76 2c 20 70 54 61 62 29 3b  ityStr(v, pTab);
2d50f 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65  .    sqlite3Code
2d510 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73  RowTrigger(pPars
2d511 65 2c 20 70 54 72 69 67 67 65 72 2c 20 54 4b 5f  e, pTrigger, TK_
2d512 55 50 44 41 54 45 2c 20 70 43 68 61 6e 67 65 73  UPDATE, pChanges
2d513 2c 20 0a 20 20 20 20 20 20 20 20 54 52 49 47 47  , .        TRIGG
2d514 45 52 5f 42 45 46 4f 52 45 2c 20 70 54 61 62 2c  ER_BEFORE, pTab,
2d515 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c 20 6f 6e   regOldRowid, on
2d516 45 72 72 6f 72 2c 20 61 64 64 72 29 3b 0a 0a 20  Error, addr);.. 
2d517 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 2d 74 72     /* The row-tr
2d518 69 67 67 65 72 20 6d 61 79 20 68 61 76 65 20 64  igger may have d
2d519 65 6c 65 74 65 64 20 74 68 65 20 72 6f 77 20 62  eleted the row b
2d51a 65 69 6e 67 20 75 70 64 61 74 65 64 2e 20 49 6e  eing updated. In
2d51b 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 63 61 73   this.    ** cas
2d51c 65 2c 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 6e  e, jump to the n
2d51d 65 78 74 20 72 6f 77 2e 20 4e 6f 20 75 70 64 61  ext row. No upda
2d51e 74 65 73 20 6f 72 20 41 46 54 45 52 20 74 72 69  tes or AFTER tri
2d51f 67 67 65 72 73 20 61 72 65 20 0a 20 20 20 20 2a  ggers are .    *
2d520 2a 20 72 65 71 75 69 72 65 64 2e 20 54 68 69 73  * required. This
2d521 20 62 65 68 61 76 69 6f 75 72 20 2d 20 77 68 61   behaviour - wha
2d522 74 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 74  t happens when t
2d523 68 65 20 72 6f 77 20 62 65 69 6e 67 20 75 70 64  he row being upd
2d524 61 74 65 64 0a 20 20 20 20 2a 2a 20 69 73 20 64  ated.    ** is d
2d525 65 6c 65 74 65 64 20 6f 72 20 72 65 6e 61 6d 65  eleted or rename
2d526 64 20 62 79 20 61 20 42 45 46 4f 52 45 20 74 72  d by a BEFORE tr
2d527 69 67 67 65 72 20 2d 20 69 73 20 6c 65 66 74 20  igger - is left 
2d528 75 6e 64 65 66 69 6e 65 64 20 69 6e 20 74 68 65  undefined in the
2d529 0a 20 20 20 20 2a 2a 20 64 6f 63 75 6d 65 6e 74  .    ** document
2d52a 61 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 73  ation.  */.    s
2d52b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2d52c 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  (v, OP_NotExists
2d52d 2c 20 69 43 75 72 2c 20 61 64 64 72 2c 20 72 65  , iCur, addr, re
2d52e 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 7d 0a  gOldRowid);.  }.
2d52f 0a 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29  .  if( !isView )
2d530 7b 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f 6e  {..    /* Do con
2d531 73 74 72 61 69 6e 74 20 63 68 65 63 6b 73 2e 20  straint checks. 
2d532 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 47 65  */.    sqlite3Ge
2d533 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74  nerateConstraint
2d534 43 68 65 63 6b 73 28 70 50 61 72 73 65 2c 20 70  Checks(pParse, p
2d535 54 61 62 2c 20 69 43 75 72 2c 20 72 65 67 4e 65  Tab, iCur, regNe
2d536 77 52 6f 77 69 64 2c 0a 20 20 20 20 20 20 20 20  wRowid,.        
2d537 61 52 65 67 49 64 78 2c 20 28 63 68 6e 67 52 6f  aRegIdx, (chngRo
2d538 77 69 64 3f 72 65 67 4f 6c 64 52 6f 77 69 64 3a  wid?regOldRowid:
2d539 30 29 2c 20 31 2c 20 6f 6e 45 72 72 6f 72 2c 20  0), 1, onError, 
2d53a 61 64 64 72 2c 20 30 29 3b 0a 0a 20 20 20 20 2f  addr, 0);..    /
2d53b 2a 20 44 6f 20 46 4b 20 63 6f 6e 73 74 72 61 69  * Do FK constrai
2d53c 6e 74 20 63 68 65 63 6b 73 2e 20 2a 2f 0a 20 20  nt checks. */.  
2d53d 20 20 69 66 28 20 68 61 73 46 4b 20 29 7b 0a 20    if( hasFK ){. 
2d53e 20 20 20 20 20 73 71 6c 69 74 65 33 46 6b 43 68       sqlite3FkCh
2d53f 65 63 6b 28 70 50 61 72 73 65 2c 20 70 54 61 62  eck(pParse, pTab
2d540 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c 20 30  , regOldRowid, 0
2d541 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
2d542 20 44 65 6c 65 74 65 20 74 68 65 20 69 6e 64 65   Delete the inde
2d543 78 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69  x entries associ
2d544 61 74 65 64 20 77 69 74 68 20 74 68 65 20 63 75  ated with the cu
2d545 72 72 65 6e 74 20 72 65 63 6f 72 64 2e 20 20 2a  rrent record.  *
2d546 2f 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74  /.    j1 = sqlit
2d547 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2d548 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69 43  OP_NotExists, iC
2d549 75 72 2c 20 30 2c 20 72 65 67 4f 6c 64 52 6f 77  ur, 0, regOldRow
2d54a 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  id);.    sqlite3
2d54b 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78  GenerateRowIndex
2d54c 44 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20 70  Delete(pParse, p
2d54d 54 61 62 2c 20 69 43 75 72 2c 20 61 52 65 67 49  Tab, iCur, aRegI
2d54e 64 78 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49  dx);.  .    /* I
2d54f 66 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 72  f changing the r
2d550 65 63 6f 72 64 20 6e 75 6d 62 65 72 2c 20 64 65  ecord number, de
2d551 6c 65 74 65 20 74 68 65 20 6f 6c 64 20 72 65 63  lete the old rec
2d552 6f 72 64 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28  ord.  */.    if(
2d553 20 68 61 73 46 4b 20 7c 7c 20 63 68 6e 67 52 6f   hasFK || chngRo
2d554 77 69 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  wid ){.      sql
2d555 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2d556 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20 69 43 75  , OP_Delete, iCu
2d557 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20  r, 0);.    }.   
2d558 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
2d559 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 0a 20 20  Here(v, j1);..  
2d55a 20 20 69 66 28 20 68 61 73 46 4b 20 29 7b 0a 20    if( hasFK ){. 
2d55b 20 20 20 20 20 73 71 6c 69 74 65 33 46 6b 43 68       sqlite3FkCh
2d55c 65 63 6b 28 70 50 61 72 73 65 2c 20 70 54 61 62  eck(pParse, pTab
2d55d 2c 20 30 2c 20 72 65 67 4e 65 77 52 6f 77 69 64  , 0, regNewRowid
2d55e 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  );.    }.  .    
2d55f 2f 2a 20 49 6e 73 65 72 74 20 74 68 65 20 6e 65  /* Insert the ne
2d560 77 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20  w index entries 
2d561 61 6e 64 20 74 68 65 20 6e 65 77 20 72 65 63 6f  and the new reco
2d562 72 64 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  rd. */.    sqlit
2d563 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74  e3CompleteInsert
2d564 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62  ion(pParse, pTab
2d565 2c 20 69 43 75 72 2c 20 72 65 67 4e 65 77 52 6f  , iCur, regNewRo
2d566 77 69 64 2c 20 61 52 65 67 49 64 78 2c 20 31 2c  wid, aRegIdx, 1,
2d567 20 30 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a 20   0, 0);..    /* 
2d568 44 6f 20 61 6e 79 20 4f 4e 20 43 41 53 43 41 44  Do any ON CASCAD
2d569 45 2c 20 53 45 54 20 4e 55 4c 4c 20 6f 72 20 53  E, SET NULL or S
2d56a 45 54 20 44 45 46 41 55 4c 54 20 6f 70 65 72 61  ET DEFAULT opera
2d56b 74 69 6f 6e 73 20 72 65 71 75 69 72 65 64 20 74  tions required t
2d56c 6f 0a 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 20  o.    ** handle 
2d56d 72 6f 77 73 20 28 70 6f 73 73 69 62 6c 79 20 69  rows (possibly i
2d56e 6e 20 6f 74 68 65 72 20 74 61 62 6c 65 73 29 20  n other tables) 
2d56f 74 68 61 74 20 72 65 66 65 72 20 76 69 61 20 61  that refer via a
2d570 20 66 6f 72 65 69 67 6e 20 6b 65 79 0a 20 20 20   foreign key.   
2d571 20 2a 2a 20 74 6f 20 74 68 65 20 72 6f 77 20 6a   ** to the row j
2d572 75 73 74 20 75 70 64 61 74 65 64 2e 20 2a 2f 20  ust updated. */ 
2d573 0a 20 20 20 20 69 66 28 20 68 61 73 46 4b 20 29  .    if( hasFK )
2d574 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 46  {.      sqlite3F
2d575 6b 41 63 74 69 6f 6e 73 28 70 50 61 72 73 65 2c  kActions(pParse,
2d576 20 70 54 61 62 2c 20 70 43 68 61 6e 67 65 73 2c   pTab, pChanges,
2d577 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20   regOldRowid);. 
2d578 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
2d579 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 6f 77  ncrement the row
2d57a 20 63 6f 75 6e 74 65 72 20 0a 20 20 2a 2f 0a 20   counter .  */. 
2d57b 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 20   if( (db->flags 
2d57c 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f  & SQLITE_CountRo
2d57d 77 73 29 20 26 26 20 21 70 50 61 72 73 65 2d 3e  ws) && !pParse->
2d57e 70 54 72 69 67 67 65 72 54 61 62 29 7b 0a 20 20  pTriggerTab){.  
2d57f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2d580 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d  Op2(v, OP_AddImm
2d581 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74 2c 20 31  , regRowCount, 1
2d582 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  );.  }..  sqlite
2d583 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28  3CodeRowTrigger(
2d584 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72  pParse, pTrigger
2d585 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 43 68  , TK_UPDATE, pCh
2d586 61 6e 67 65 73 2c 20 0a 20 20 20 20 20 20 54 52  anges, .      TR
2d587 49 47 47 45 52 5f 41 46 54 45 52 2c 20 70 54 61  IGGER_AFTER, pTa
2d588 62 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c 20  b, regOldRowid, 
2d589 6f 6e 45 72 72 6f 72 2c 20 61 64 64 72 29 3b 0a  onError, addr);.
2d58a 0a 20 20 2f 2a 20 52 65 70 65 61 74 20 74 68 65  .  /* Repeat the
2d58b 20 61 62 6f 76 65 20 77 69 74 68 20 74 68 65 20   above with the 
2d58c 6e 65 78 74 20 72 65 63 6f 72 64 20 74 6f 20 62  next record to b
2d58d 65 20 75 70 64 61 74 65 64 2c 20 75 6e 74 69 6c  e updated, until
2d58e 0a 20 20 2a 2a 20 61 6c 6c 20 72 65 63 6f 72 64  .  ** all record
2d58f 20 73 65 6c 65 63 74 65 64 20 62 79 20 74 68 65   selected by the
2d590 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 68 61   WHERE clause ha
2d591 76 65 20 62 65 65 6e 20 75 70 64 61 74 65 64 2e  ve been updated.
2d592 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  .  */.  sqlite3V
2d593 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2d594 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 29 3b 0a  Goto, 0, addr);.
2d595 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2d596 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a  pHere(v, addr);.
2d597 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20  .  /* Close all 
2d598 74 61 62 6c 65 73 20 2a 2f 0a 20 20 66 6f 72 28  tables */.  for(
2d599 69 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  i=0, pIdx=pTab->
2d59a 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
2d59b 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20  dx=pIdx->pNext, 
2d59c 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6f 70  i++){.    if( op
2d59d 65 6e 41 6c 6c 20 7c 7c 20 61 52 65 67 49 64 78  enAll || aRegIdx
2d59e 5b 69 5d 3e 30 20 29 7b 0a 20 20 20 20 20 20 73  [i]>0 ){.      s
2d59f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2d5a0 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43  (v, OP_Close, iC
2d5a1 75 72 2b 69 2b 31 2c 20 30 29 3b 0a 20 20 20 20  ur+i+1, 0);.    
2d5a2 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  }.  }.  sqlite3V
2d5a3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2d5a4 43 6c 6f 73 65 2c 20 69 43 75 72 2c 20 30 29 3b  Close, iCur, 0);
2d5a5 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68  ..  /* Update th
2d5a6 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  e sqlite_sequenc
2d5a7 65 20 74 61 62 6c 65 20 62 79 20 73 74 6f 72 69  e table by stori
2d5a8 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ng the content o
2d5a9 66 20 74 68 65 0a 20 20 2a 2a 20 6d 61 78 69 6d  f the.  ** maxim
2d5aa 75 6d 20 72 6f 77 69 64 20 63 6f 75 6e 74 65 72  um rowid counter
2d5ab 20 76 61 6c 75 65 73 20 72 65 63 6f 72 64 65 64   values recorded
2d5ac 20 77 68 69 6c 65 20 69 6e 73 65 72 74 69 6e 67   while inserting
2d5ad 20 69 6e 74 6f 0a 20 20 2a 2a 20 61 75 74 6f 69   into.  ** autoi
2d5ae 6e 63 72 65 6d 65 6e 74 20 74 61 62 6c 65 73 2e  ncrement tables.
2d5af 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 72  .  */.  if( pPar
2d5b0 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 26 26  se->nested==0 &&
2d5b1 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65   pParse->pTrigge
2d5b2 72 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73  rTab==0 ){.    s
2d5b3 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d  qlite3Autoincrem
2d5b4 65 6e 74 45 6e 64 28 70 50 61 72 73 65 29 3b 0a  entEnd(pParse);.
2d5b5 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52    }..  /*.  ** R
2d5b6 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
2d5b7 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77 65   of rows that we
2d5b8 72 65 20 63 68 61 6e 67 65 64 2e 20 49 66 20 74  re changed. If t
2d5b9 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 0a  his routine is .
2d5ba 20 20 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 20    ** generating 
2d5bb 63 6f 64 65 20 62 65 63 61 75 73 65 20 6f 66 20  code because of 
2d5bc 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
2d5bd 33 4e 65 73 74 65 64 50 61 72 73 65 28 29 2c 20  3NestedParse(), 
2d5be 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 69 6e 76 6f  do not.  ** invo
2d5bf 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  ke the callback 
2d5c0 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20  function..  */. 
2d5c1 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 26   if( (db->flags&
2d5c2 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
2d5c3 29 20 26 26 20 21 70 50 61 72 73 65 2d 3e 70 54  ) && !pParse->pT
2d5c4 72 69 67 67 65 72 54 61 62 20 26 26 20 21 70 50  riggerTab && !pP
2d5c5 61 72 73 65 2d 3e 6e 65 73 74 65 64 20 29 7b 0a  arse->nested ){.
2d5c6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2d5c7 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
2d5c8 6c 74 52 6f 77 2c 20 72 65 67 52 6f 77 43 6f 75  ltRow, regRowCou
2d5c9 6e 74 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  nt, 1);.    sqli
2d5ca 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
2d5cb 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c  s(v, 1);.    sql
2d5cc 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
2d5cd 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
2d5ce 5f 4e 41 4d 45 2c 20 22 72 6f 77 73 20 75 70 64  _NAME, "rows upd
2d5cf 61 74 65 64 22 2c 20 53 51 4c 49 54 45 5f 53 54  ated", SQLITE_ST
2d5d0 41 54 49 43 29 3b 0a 20 20 7d 0a 0a 75 70 64 61  ATIC);.  }..upda
2d5d1 74 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71  te_cleanup:.  sq
2d5d2 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
2d5d3 50 6f 70 28 26 73 43 6f 6e 74 65 78 74 29 3b 0a  Pop(&sContext);.
2d5d4 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2d5d5 64 62 2c 20 61 52 65 67 49 64 78 29 3b 0a 20 20  db, aRegIdx);.  
2d5d6 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2d5d7 2c 20 61 58 52 65 66 29 3b 0a 20 20 73 71 6c 69  , aXRef);.  sqli
2d5d8 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
2d5d9 28 64 62 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a  (db, pTabList);.
2d5da 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
2d5db 74 44 65 6c 65 74 65 28 64 62 2c 20 70 43 68 61  tDelete(db, pCha
2d5dc 6e 67 65 73 29 3b 0a 20 20 73 71 6c 69 74 65 33  nges);.  sqlite3
2d5dd 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
2d5de 57 68 65 72 65 29 3b 0a 20 20 72 65 74 75 72 6e  Where);.  return
2d5df 3b 0a 7d 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ;.}./* Make sure
2d5e0 20 22 69 73 56 69 65 77 22 20 61 6e 64 20 6f 74   "isView" and ot
2d5e1 68 65 72 20 6d 61 63 72 6f 73 20 64 65 66 69 6e  her macros defin
2d5e2 65 64 20 61 62 6f 76 65 20 61 72 65 20 75 6e 64  ed above are und
2d5e3 65 66 69 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  efined. Otherwis
2d5e4 65 0a 2a 2a 20 74 68 65 6c 79 20 6d 61 79 20 69  e.** thely may i
2d5e5 6e 74 65 72 66 65 72 65 20 77 69 74 68 20 63 6f  nterfere with co
2d5e6 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20 6f 74 68  mpilation of oth
2d5e7 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20  er functions in 
2d5e8 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 28 6f 72  this file.** (or
2d5e9 20 69 6e 20 61 6e 6f 74 68 65 72 20 66 69 6c 65   in another file
2d5ea 2c 20 69 66 20 74 68 69 73 20 66 69 6c 65 20 62  , if this file b
2d5eb 65 63 6f 6d 65 73 20 70 61 72 74 20 6f 66 20 74  ecomes part of t
2d5ec 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 29  he amalgamation)
2d5ed 2e 20 20 2a 2f 0a 23 69 66 64 65 66 20 69 73 56  .  */.#ifdef isV
2d5ee 69 65 77 0a 20 23 75 6e 64 65 66 20 69 73 56 69  iew. #undef isVi
2d5ef 65 77 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  ew.#endif.#ifdef
2d5f0 20 70 54 72 69 67 67 65 72 0a 20 23 75 6e 64 65   pTrigger. #unde
2d5f1 66 20 70 54 72 69 67 67 65 72 0a 23 65 6e 64 69  f pTrigger.#endi
2d5f2 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
2d5f3 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
2d5f4 42 4c 45 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  BLE./*.** Genera
2d5f5 74 65 20 63 6f 64 65 20 66 6f 72 20 61 6e 20 55  te code for an U
2d5f6 50 44 41 54 45 20 6f 66 20 61 20 76 69 72 74 75  PDATE of a virtu
2d5f7 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  al table..**.** 
2d5f8 54 68 65 20 73 74 72 61 74 65 67 79 20 69 73 20  The strategy is 
2d5f9 74 68 61 74 20 77 65 20 63 72 65 61 74 65 20 61  that we create a
2d5fa 6e 20 65 70 68 65 6d 65 72 69 61 6c 20 74 61 62  n ephemerial tab
2d5fb 6c 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  le that contains
2d5fc 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20 72 6f 77  .** for each row
2d5fd 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64 3a 0a   to be changed:.
2d5fe 2a 2a 0a 2a 2a 20 20 20 28 41 29 20 20 54 68 65  **.**   (A)  The
2d5ff 20 6f 72 69 67 69 6e 61 6c 20 72 6f 77 69 64 20   original rowid 
2d600 6f 66 20 74 68 61 74 20 72 6f 77 2e 0a 2a 2a 20  of that row..** 
2d601 20 20 28 42 29 20 20 54 68 65 20 72 65 76 69 73    (B)  The revis
2d602 65 64 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65  ed rowid for the
2d603 20 72 6f 77 2e 20 28 6e 6f 74 65 31 29 0a 2a 2a   row. (note1).**
2d604 20 20 20 28 43 29 20 20 54 68 65 20 63 6f 6e 74     (C)  The cont
2d605 65 6e 74 20 6f 66 20 65 76 65 72 79 20 63 6f 6c  ent of every col
2d606 75 6d 6e 20 69 6e 20 74 68 65 20 72 6f 77 2e 0a  umn in the row..
2d607 2a 2a 0a 2a 2a 20 54 68 65 6e 20 77 65 20 6c 6f  **.** Then we lo
2d608 6f 70 20 6f 76 65 72 20 74 68 69 73 20 65 70 68  op over this eph
2d609 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 61 6e 64  emeral table and
2d60a 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69 6e   for each row in
2d60b 0a 2a 2a 20 74 68 65 20 65 70 68 65 72 6d 65 72  .** the ephermer
2d60c 61 6c 20 74 61 62 6c 65 20 63 61 6c 6c 20 56 55  al table call VU
2d60d 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  pdate..**.** Whe
2d60e 6e 20 66 69 6e 69 73 68 65 64 2c 20 64 72 6f 70  n finished, drop
2d60f 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74   the ephemeral t
2d610 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 28 6e 6f 74  able..**.** (not
2d611 65 31 29 20 41 63 74 75 61 6c 6c 79 2c 20 69 66  e1) Actually, if
2d612 20 77 65 20 6b 6e 6f 77 20 69 6e 20 61 64 76 61   we know in adva
2d613 6e 63 65 20 74 68 61 74 20 28 41 29 20 69 73 20  nce that (A) is 
2d614 61 6c 77 61 79 73 20 74 68 65 20 73 61 6d 65 0a  always the same.
2d615 2a 2a 20 61 73 20 28 42 29 20 77 65 20 6f 6e 6c  ** as (B) we onl
2d616 79 20 73 74 6f 72 65 20 28 41 29 2c 20 74 68 65  y store (A), the
2d617 6e 20 64 75 70 6c 69 63 61 74 65 20 28 41 29 20  n duplicate (A) 
2d618 77 68 65 6e 20 70 75 6c 6c 69 6e 67 0a 2a 2a 20  when pulling.** 
2d619 69 74 20 6f 75 74 20 6f 66 20 74 68 65 20 65 70  it out of the ep
2d61a 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 62 65  hemeral table be
2d61b 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 56 55 70  fore calling VUp
2d61c 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  date..*/.static 
2d61d 76 6f 69 64 20 75 70 64 61 74 65 56 69 72 74 75  void updateVirtu
2d61e 61 6c 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65  alTable(.  Parse
2d61f 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2d620 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63  /* The parsing c
2d621 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c  ontext */.  SrcL
2d622 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20  ist *pSrc,      
2d623 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20   /* The virtual 
2d624 74 61 62 6c 65 20 74 6f 20 62 65 20 6d 6f 64 69  table to be modi
2d625 66 69 65 64 20 2a 2f 0a 20 20 54 61 62 6c 65 20  fied */.  Table 
2d626 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 2f  *pTab,         /
2d627 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 74 61  * The virtual ta
2d628 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ble */.  ExprLis
2d629 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 2f 2a  t *pChanges,  /*
2d62a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 74 6f 20   The columns to 
2d62b 63 68 61 6e 67 65 20 69 6e 20 74 68 65 20 55 50  change in the UP
2d62c 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a  DATE statement *
2d62d 2f 0a 20 20 45 78 70 72 20 2a 70 52 6f 77 69 64  /.  Expr *pRowid
2d62e 2c 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72  ,        /* Expr
2d62f 65 73 73 69 6f 6e 20 75 73 65 64 20 74 6f 20 72  ession used to r
2d630 65 63 6f 6d 70 75 74 65 20 74 68 65 20 72 6f 77  ecompute the row
2d631 69 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 58 52  id */.  int *aXR
2d632 65 66 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ef,          /* 
2d633 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 63 6f 6c  Mapping from col
2d634 75 6d 6e 73 20 6f 66 20 70 54 61 62 20 74 6f 20  umns of pTab to 
2d635 65 6e 74 72 69 65 73 20 69 6e 20 70 43 68 61 6e  entries in pChan
2d636 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ges */.  Expr *p
2d637 57 68 65 72 65 20 20 20 20 20 20 20 20 20 2f 2a  Where         /*
2d638 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66   WHERE clause of
2d639 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61 74   the UPDATE stat
2d63a 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 56 64  ement */.){.  Vd
2d63b 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
2d63c 70 56 64 62 65 3b 20 20 2f 2a 20 56 69 72 74 75  pVdbe;  /* Virtu
2d63d 61 6c 20 6d 61 63 68 69 6e 65 20 75 6e 64 65 72  al machine under
2d63e 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f   construction */
2d63f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
2d640 69 73 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  ist = 0;     /* 
2d641 54 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f  The result set o
2d642 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  f the SELECT sta
2d643 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 6c 65  tement */.  Sele
2d644 63 74 20 2a 70 53 65 6c 65 63 74 20 3d 20 30 3b  ct *pSelect = 0;
2d645 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c        /* The SEL
2d646 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ECT statement */
2d647 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20  .  Expr *pExpr; 
2d648 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d649 54 65 6d 70 6f 72 61 72 79 20 65 78 70 72 65 73  Temporary expres
2d64a 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 65 70  sion */.  int ep
2d64b 68 65 6d 54 61 62 3b 20 20 20 20 20 20 20 20 20  hemTab;         
2d64c 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 6f 6c      /* Table hol
2d64d 64 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 20  ding the result 
2d64e 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 2a 2f  of the SELECT */
2d64f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
2d650 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d651 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
2d652 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20 20    int addr;     
2d653 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
2d654 64 64 72 65 73 73 20 6f 66 20 74 6f 70 20 6f 66  ddress of top of
2d655 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 69   loop */.  int i
2d656 52 65 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Reg;            
2d657 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 72 65       /* First re
2d658 67 69 73 74 65 72 20 69 6e 20 73 65 74 20 70 61  gister in set pa
2d659 73 73 65 64 20 74 6f 20 4f 50 5f 56 55 70 64 61  ssed to OP_VUpda
2d65a 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  te */.  sqlite3 
2d65b 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2d65c 3b 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f  ; /* Database co
2d65d 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f  nnection */.  co
2d65e 6e 73 74 20 63 68 61 72 20 2a 70 56 54 61 62 20  nst char *pVTab 
2d65f 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73  = (const char*)s
2d660 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28  qlite3GetVTable(
2d661 64 62 2c 20 70 54 61 62 29 3b 0a 20 20 53 65 6c  db, pTab);.  Sel
2d662 65 63 74 44 65 73 74 20 64 65 73 74 3b 0a 0a 20  ectDest dest;.. 
2d663 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 74 68   /* Construct th
2d664 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
2d665 6e 74 20 74 68 61 74 20 77 69 6c 6c 20 66 69 6e  nt that will fin
2d666 64 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 73  d the new values
2d667 20 66 6f 72 0a 20 20 2a 2a 20 61 6c 6c 20 75 70   for.  ** all up
2d668 64 61 74 65 64 20 72 6f 77 73 2e 20 0a 20 20 2a  dated rows. .  *
2d669 2f 0a 20 20 70 45 4c 69 73 74 20 3d 20 73 71 6c  /.  pEList = sql
2d66a 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
2d66b 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20 0a 20  nd(pParse, 0, . 
2d66c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d66d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d66e 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 45  sqlite3CreateIdE
2d66f 78 70 72 28 70 50 61 72 73 65 2c 20 22 5f 72 6f  xpr(pParse, "_ro
2d670 77 69 64 5f 22 29 29 3b 0a 20 20 69 66 28 20 70  wid_"));.  if( p
2d671 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 70 45 4c  Rowid ){.    pEL
2d672 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ist = sqlite3Exp
2d673 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
2d674 73 65 2c 20 70 45 4c 69 73 74 2c 0a 20 20 20 20  se, pEList,.    
2d675 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d676 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
2d677 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
2d678 2c 20 70 52 6f 77 69 64 2c 20 30 29 29 3b 0a 20  , pRowid, 0));. 
2d679 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61   }.  assert( pTa
2d67a 62 2d 3e 69 50 4b 65 79 3c 30 20 29 3b 0a 20 20  b->iPKey<0 );.  
2d67b 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d  for(i=0; i<pTab-
2d67c 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
2d67d 20 69 66 28 20 61 58 52 65 66 5b 69 5d 3e 3d 30   if( aXRef[i]>=0
2d67e 20 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72 20   ){.      pExpr 
2d67f 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
2d680 28 64 62 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61  (db, pChanges->a
2d681 5b 61 58 52 65 66 5b 69 5d 5d 2e 70 45 78 70 72  [aXRef[i]].pExpr
2d682 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  , 0);.    }else{
2d683 0a 20 20 20 20 20 20 70 45 78 70 72 20 3d 20 73  .      pExpr = s
2d684 71 6c 69 74 65 33 43 72 65 61 74 65 49 64 45 78  qlite3CreateIdEx
2d685 70 72 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d  pr(pParse, pTab-
2d686 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b  >aCol[i].zName);
2d687 0a 20 20 20 20 7d 0a 20 20 20 20 70 45 4c 69 73  .    }.    pELis
2d688 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  t = sqlite3ExprL
2d689 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
2d68a 2c 20 70 45 4c 69 73 74 2c 20 70 45 78 70 72 29  , pEList, pExpr)
2d68b 3b 0a 20 20 7d 0a 20 20 70 53 65 6c 65 63 74 20  ;.  }.  pSelect 
2d68c 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e  = sqlite3SelectN
2d68d 65 77 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73  ew(pParse, pELis
2d68e 74 2c 20 70 53 72 63 2c 20 70 57 68 65 72 65 2c  t, pSrc, pWhere,
2d68f 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2d690 30 29 3b 0a 20 20 0a 20 20 2f 2a 20 43 72 65 61  0);.  .  /* Crea
2d691 74 65 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c  te the ephemeral
2d692 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
2d693 68 20 74 68 65 20 75 70 64 61 74 65 20 72 65 73  h the update res
2d694 75 6c 74 73 20 77 69 6c 6c 0a 20 20 2a 2a 20 62  ults will.  ** b
2d695 65 20 73 74 6f 72 65 64 2e 0a 20 20 2a 2f 0a 20  e stored..  */. 
2d696 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20 20   assert( v );.  
2d697 65 70 68 65 6d 54 61 62 20 3d 20 70 50 61 72 73  ephemTab = pPars
2d698 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 73 71 6c  e->nTab++;.  sql
2d699 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2d69a 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
2d69b 61 6c 2c 20 65 70 68 65 6d 54 61 62 2c 20 70 54  al, ephemTab, pT
2d69c 61 62 2d 3e 6e 43 6f 6c 2b 31 2b 28 70 52 6f 77  ab->nCol+1+(pRow
2d69d 69 64 21 3d 30 29 29 3b 0a 0a 20 20 2f 2a 20 66  id!=0));..  /* f
2d69e 69 6c 6c 20 74 68 65 20 65 70 68 65 6d 65 72 61  ill the ephemera
2d69f 6c 20 74 61 62 6c 65 20 0a 20 20 2a 2f 0a 20 20  l table .  */.  
2d6a0 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73  sqlite3SelectDes
2d6a1 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54  tInit(&dest, SRT
2d6a2 5f 54 61 62 6c 65 2c 20 65 70 68 65 6d 54 61 62  _Table, ephemTab
2d6a3 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  );.  sqlite3Sele
2d6a4 63 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65  ct(pParse, pSele
2d6a5 63 74 2c 20 26 64 65 73 74 29 3b 0a 0a 20 20 2f  ct, &dest);..  /
2d6a6 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2d6a7 74 6f 20 73 63 61 6e 20 74 68 65 20 65 70 68 65  to scan the ephe
2d6a8 6d 65 72 61 6c 20 74 61 62 6c 65 20 61 6e 64 20  meral table and 
2d6a9 63 61 6c 6c 20 56 55 70 64 61 74 65 2e 20 2a 2f  call VUpdate. */
2d6aa 0a 20 20 69 52 65 67 20 3d 20 2b 2b 70 50 61 72  .  iReg = ++pPar
2d6ab 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 70 50 61 72  se->nMem;.  pPar
2d6ac 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62  se->nMem += pTab
2d6ad 2d 3e 6e 43 6f 6c 2b 31 3b 0a 20 20 61 64 64 72  ->nCol+1;.  addr
2d6ae 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2d6af 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
2d6b0 64 2c 20 65 70 68 65 6d 54 61 62 2c 20 30 29 3b  d, ephemTab, 0);
2d6b1 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2d6b2 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
2d6b3 6e 2c 20 20 65 70 68 65 6d 54 61 62 2c 20 30 2c  n,  ephemTab, 0,
2d6b4 20 69 52 65 67 29 3b 0a 20 20 73 71 6c 69 74 65   iReg);.  sqlite
2d6b5 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2d6b6 50 5f 43 6f 6c 75 6d 6e 2c 20 65 70 68 65 6d 54  P_Column, ephemT
2d6b7 61 62 2c 20 28 70 52 6f 77 69 64 3f 31 3a 30 29  ab, (pRowid?1:0)
2d6b8 2c 20 69 52 65 67 2b 31 29 3b 0a 20 20 66 6f 72  , iReg+1);.  for
2d6b9 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43  (i=0; i<pTab->nC
2d6ba 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71  ol; i++){.    sq
2d6bb 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2d6bc 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 65 70  v, OP_Column, ep
2d6bd 68 65 6d 54 61 62 2c 20 69 2b 31 2b 28 70 52 6f  hemTab, i+1+(pRo
2d6be 77 69 64 21 3d 30 29 2c 20 69 52 65 67 2b 32 2b  wid!=0), iReg+2+
2d6bf 69 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  i);.  }.  sqlite
2d6c0 33 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c  3VtabMakeWritabl
2d6c1 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b  e(pParse, pTab);
2d6c2 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2d6c3 64 4f 70 34 28 76 2c 20 4f 50 5f 56 55 70 64 61  dOp4(v, OP_VUpda
2d6c4 74 65 2c 20 30 2c 20 70 54 61 62 2d 3e 6e 43 6f  te, 0, pTab->nCo
2d6c5 6c 2b 32 2c 20 69 52 65 67 2c 20 70 56 54 61 62  l+2, iReg, pVTab
2d6c6 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 73 71  , P4_VTAB);.  sq
2d6c7 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50  lite3MayAbort(pP
2d6c8 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  arse);.  sqlite3
2d6c9 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2d6ca 5f 4e 65 78 74 2c 20 65 70 68 65 6d 54 61 62 2c  _Next, ephemTab,
2d6cb 20 61 64 64 72 2b 31 29 3b 0a 20 20 73 71 6c 69   addr+1);.  sqli
2d6cc 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2d6cd 76 2c 20 61 64 64 72 29 3b 0a 20 20 73 71 6c 69  v, addr);.  sqli
2d6ce 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2d6cf 20 4f 50 5f 43 6c 6f 73 65 2c 20 65 70 68 65 6d   OP_Close, ephem
2d6d0 54 61 62 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 43  Tab, 0);..  /* C
2d6d1 6c 65 61 6e 75 70 20 2a 2f 0a 20 20 73 71 6c 69  leanup */.  sqli
2d6d2 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
2d6d3 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 20 20 0a  db, pSelect);  .
2d6d4 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
2d6d5 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2d6d6 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  ABLE */../******
2d6d7 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
2d6d8 75 70 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  update.c *******
2d6d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d6da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d6db 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
2d6dc 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
2d6dd 69 6c 65 20 76 61 63 75 75 6d 2e 63 20 2a 2a 2a  ile vacuum.c ***
2d6de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d6df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d6e0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
2d6e1 30 30 33 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a  003 April 6.**.*
2d6e2 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
2d6e3 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
2d6e4 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
2d6e5 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
2d6e6 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
2d6e7 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
2d6e8 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
2d6e9 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
2d6ea 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
2d6eb 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
2d6ec 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
2d6ed 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
2d6ee 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
2d6ef 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
2d6f0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
2d6f1 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
2d6f2 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
2d6f3 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
2d6f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d6f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d6f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d6f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
2d6f8 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
2d6f9 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d   code used to im
2d6fa 70 6c 65 6d 65 6e 74 20 74 68 65 20 56 41 43 55  plement the VACU
2d6fb 55 4d 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a  UM command..**.*
2d6fc 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 63 6f  * Most of the co
2d6fd 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
2d6fe 6d 61 79 20 62 65 20 6f 6d 69 74 74 65 64 20 62  may be omitted b
2d6ff 79 20 64 65 66 69 6e 69 6e 67 20 74 68 65 0a 2a  y defining the.*
2d700 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41  * SQLITE_OMIT_VA
2d701 43 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a  CUUM macro..**.*
2d702 2a 20 24 49 64 3a 20 76 61 63 75 75 6d 2e 63 2c  * $Id: vacuum.c,
2d703 76 20 31 2e 39 31 20 32 30 30 39 2f 30 37 2f 30  v 1.91 2009/07/0
2d704 32 20 30 37 3a 34 37 3a 33 33 20 64 61 6e 69 65  2 07:47:33 danie
2d705 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
2d706 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
2d707 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d  LITE_OMIT_VACUUM
2d708 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
2d709 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48  LITE_OMIT_ATTACH
2d70a 29 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20  )./*.** Execute 
2d70b 7a 53 71 6c 20 6f 6e 20 64 61 74 61 62 61 73 65  zSql on database
2d70c 20 64 62 2e 20 52 65 74 75 72 6e 20 61 6e 20 65   db. Return an e
2d70d 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74  rror code..*/.st
2d70e 61 74 69 63 20 69 6e 74 20 65 78 65 63 53 71 6c  atic int execSql
2d70f 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
2d710 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 29 7b  nst char *zSql){
2d711 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2d712 2a 70 53 74 6d 74 3b 0a 20 20 56 56 41 5f 4f 4e  *pStmt;.  VVA_ON
2d713 4c 59 28 20 69 6e 74 20 72 63 3b 20 29 0a 20 20  LY( int rc; ).  
2d714 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a 20 20 20  if( !zSql ){.   
2d715 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
2d716 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20  OMEM;.  }.  if( 
2d717 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74  SQLITE_OK!=sqlit
2d718 65 33 5f 70 72 65 70 61 72 65 28 64 62 2c 20 7a  e3_prepare(db, z
2d719 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c  Sql, -1, &pStmt,
2d71a 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72   0) ){.    retur
2d71b 6e 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  n sqlite3_errcod
2d71c 65 28 64 62 29 3b 0a 20 20 7d 0a 20 20 56 56 41  e(db);.  }.  VVA
2d71d 5f 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 73 71  _ONLY( rc = ) sq
2d71e 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74  lite3_step(pStmt
2d71f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21  );.  assert( rc!
2d720 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 3b 0a 20  =SQLITE_ROW );. 
2d721 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f   return sqlite3_
2d722 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b  finalize(pStmt);
2d723 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74  .}../*.** Execut
2d724 65 20 7a 53 71 6c 20 6f 6e 20 64 61 74 61 62 61  e zSql on databa
2d725 73 65 20 64 62 2e 20 54 68 65 20 73 74 61 74 65  se db. The state
2d726 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 65 78 61  ment returns exa
2d727 63 74 6c 79 0a 2a 2a 20 6f 6e 65 20 63 6f 6c 75  ctly.** one colu
2d728 6d 6e 2e 20 45 78 65 63 75 74 65 20 74 68 69 73  mn. Execute this
2d729 20 61 73 20 53 51 4c 20 6f 6e 20 74 68 65 20 73   as SQL on the s
2d72a 61 6d 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f  ame database..*/
2d72b 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78 65 63  .static int exec
2d72c 45 78 65 63 53 71 6c 28 73 71 6c 69 74 65 33 20  ExecSql(sqlite3 
2d72d 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *db, const char 
2d72e 2a 7a 53 71 6c 29 7b 0a 20 20 73 71 6c 69 74 65  *zSql){.  sqlite
2d72f 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20  3_stmt *pStmt;. 
2d730 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d   int rc;..  rc =
2d731 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
2d732 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26  (db, zSql, -1, &
2d733 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28  pStmt, 0);.  if(
2d734 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2d735 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 77   return rc;..  w
2d736 68 69 6c 65 28 20 53 51 4c 49 54 45 5f 52 4f 57  hile( SQLITE_ROW
2d737 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70  ==sqlite3_step(p
2d738 53 74 6d 74 29 20 29 7b 0a 20 20 20 20 72 63 20  Stmt) ){.    rc 
2d739 3d 20 65 78 65 63 53 71 6c 28 64 62 2c 20 28 63  = execSql(db, (c
2d73a 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c  har*)sqlite3_col
2d73b 75 6d 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c 20  umn_text(pStmt, 
2d73c 30 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  0));.    if( rc!
2d73d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
2d73e 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61      sqlite3_fina
2d73f 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 20 20 20  lize(pStmt);.   
2d740 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
2d741 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72    }.  }..  retur
2d742 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  n sqlite3_finali
2d743 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a  ze(pStmt);.}../*
2d744 0a 2a 2a 20 54 68 65 20 6e 6f 6e 2d 73 74 61 6e  .** The non-stan
2d745 64 61 72 64 20 56 41 43 55 55 4d 20 63 6f 6d 6d  dard VACUUM comm
2d746 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 63  and is used to c
2d747 6c 65 61 6e 20 75 70 20 74 68 65 20 64 61 74 61  lean up the data
2d748 62 61 73 65 2c 0a 2a 2a 20 63 6f 6c 6c 61 70 73  base,.** collaps
2d749 65 20 66 72 65 65 20 73 70 61 63 65 2c 20 65 74  e free space, et
2d74a 63 2e 20 20 49 74 20 69 73 20 6d 6f 64 65 6c 6c  c.  It is modell
2d74b 65 64 20 61 66 74 65 72 20 74 68 65 20 56 41 43  ed after the VAC
2d74c 55 55 4d 20 63 6f 6d 6d 61 6e 64 0a 2a 2a 20 69  UUM command.** i
2d74d 6e 20 50 6f 73 74 67 72 65 53 51 4c 2e 0a 2a 2a  n PostgreSQL..**
2d74e 0a 2a 2a 20 49 6e 20 76 65 72 73 69 6f 6e 20 31  .** In version 1
2d74f 2e 30 2e 78 20 6f 66 20 53 51 4c 69 74 65 2c 20  .0.x of SQLite, 
2d750 74 68 65 20 56 41 43 55 55 4d 20 63 6f 6d 6d 61  the VACUUM comma
2d751 6e 64 20 77 6f 75 6c 64 20 63 61 6c 6c 0a 2a 2a  nd would call.**
2d752 20 67 64 62 6d 5f 72 65 6f 72 67 61 6e 69 7a 65   gdbm_reorganize
2d753 28 29 20 6f 6e 20 61 6c 6c 20 74 68 65 20 64 61  () on all the da
2d754 74 61 62 61 73 65 20 74 61 62 6c 65 73 2e 20 20  tabase tables.  
2d755 42 75 74 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a  But beginning.**
2d756 20 77 69 74 68 20 32 2e 30 2e 30 2c 20 53 51 4c   with 2.0.0, SQL
2d757 69 74 65 20 6e 6f 20 6c 6f 6e 67 65 72 20 75 73  ite no longer us
2d758 65 73 20 47 44 42 4d 20 73 6f 20 74 68 69 73 20  es GDBM so this 
2d759 63 6f 6d 6d 61 6e 64 20 68 61 73 0a 2a 2a 20 62  command has.** b
2d75a 65 63 6f 6d 65 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ecome a no-op..*
2d75b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2d75c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61 63   void sqlite3Vac
2d75d 75 75 6d 28 50 61 72 73 65 20 2a 70 50 61 72 73  uum(Parse *pPars
2d75e 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  e){.  Vdbe *v = 
2d75f 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2d760 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20  Parse);.  if( v 
2d761 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
2d762 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 56  beAddOp2(v, OP_V
2d763 61 63 75 75 6d 2c 20 30 2c 20 30 29 3b 0a 20 20  acuum, 0, 0);.  
2d764 7d 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  }.  return;.}../
2d765 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2d766 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  e implements the
2d767 20 4f 50 5f 56 61 63 75 75 6d 20 6f 70 63 6f 64   OP_Vacuum opcod
2d768 65 20 6f 66 20 74 68 65 20 56 44 42 45 2e 0a 2a  e of the VDBE..*
2d769 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2d76a 20 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 56   int sqlite3RunV
2d76b 61 63 75 75 6d 28 63 68 61 72 20 2a 2a 70 7a 45  acuum(char **pzE
2d76c 72 72 4d 73 67 2c 20 73 71 6c 69 74 65 33 20 2a  rrMsg, sqlite3 *
2d76d 64 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  db){.  int rc = 
2d76e 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 2f  SQLITE_OK;     /
2d76f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72  * Return code fr
2d770 6f 6d 20 73 65 72 76 69 63 65 20 72 6f 75 74 69  om service routi
2d771 6e 65 73 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a  nes */.  Btree *
2d772 70 4d 61 69 6e 3b 20 20 20 20 20 20 20 20 20 20  pMain;          
2d773 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
2d774 20 62 65 69 6e 67 20 76 61 63 75 75 6d 65 64 20   being vacuumed 
2d775 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 54 65 6d  */.  Btree *pTem
2d776 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p;           /* 
2d777 54 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61  The temporary da
2d778 74 61 62 61 73 65 20 77 65 20 76 61 63 75 75 6d  tabase we vacuum
2d779 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 68 61 72 20   into */.  char 
2d77a 2a 7a 53 71 6c 20 3d 20 30 3b 20 20 20 20 20 20  *zSql = 0;      
2d77b 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d     /* SQL statem
2d77c 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 73 61  ents */.  int sa
2d77d 76 65 64 5f 66 6c 61 67 73 3b 20 20 20 20 20 20  ved_flags;      
2d77e 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65    /* Saved value
2d77f 20 6f 66 20 74 68 65 20 64 62 2d 3e 66 6c 61 67   of the db->flag
2d780 73 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 64  s */.  int saved
2d781 5f 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 2f  _nChange;      /
2d782 2a 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66  * Saved value of
2d783 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a   db->nChange */.
2d784 20 20 69 6e 74 20 73 61 76 65 64 5f 6e 54 6f 74    int saved_nTot
2d785 61 6c 43 68 61 6e 67 65 3b 20 2f 2a 20 53 61 76  alChange; /* Sav
2d786 65 64 20 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e  ed value of db->
2d787 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 2a 2f 0a  nTotalChange */.
2d788 20 20 44 62 20 2a 70 44 62 20 3d 20 30 3b 20 20    Db *pDb = 0;  
2d789 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2d78a 61 62 61 73 65 20 74 6f 20 64 65 74 61 63 68 20  abase to detach 
2d78b 61 74 20 65 6e 64 20 6f 66 20 76 61 63 75 75 6d  at end of vacuum
2d78c 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 65 6d 44   */.  int isMemD
2d78d 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b;            /*
2d78e 20 54 72 75 65 20 69 66 20 76 61 63 75 75 6d 69   True if vacuumi
2d78f 6e 67 20 61 20 3a 6d 65 6d 6f 72 79 3a 20 64 61  ng a :memory: da
2d790 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20  tabase */.  int 
2d791 6e 52 65 73 3b 0a 0a 20 20 69 66 28 20 21 64 62  nRes;..  if( !db
2d792 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a  ->autoCommit ){.
2d793 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
2d794 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 64  ring(pzErrMsg, d
2d795 62 2c 20 22 63 61 6e 6e 6f 74 20 56 41 43 55 55  b, "cannot VACUU
2d796 4d 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 20  M from within a 
2d797 74 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20  transaction");. 
2d798 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2d799 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f  _ERROR;.  }..  /
2d79a 2a 20 53 61 76 65 20 74 68 65 20 63 75 72 72 65  * Save the curre
2d79b 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  nt value of the 
2d79c 64 61 74 61 62 61 73 65 20 66 6c 61 67 73 20 73  database flags s
2d79d 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 62 65  o that it can be
2d79e 20 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65 64 20   .  ** restored 
2d79f 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
2d7a0 2e 20 54 68 65 6e 20 73 65 74 20 74 68 65 20 77  . Then set the w
2d7a1 72 69 74 61 62 6c 65 2d 73 63 68 65 6d 61 20 66  ritable-schema f
2d7a2 6c 61 67 2c 20 61 6e 64 0a 20 20 2a 2a 20 64 69  lag, and.  ** di
2d7a3 73 61 62 6c 65 20 43 48 45 43 4b 20 61 6e 64 20  sable CHECK and 
2d7a4 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
2d7a5 74 72 61 69 6e 74 73 2e 20 20 2a 2f 0a 20 20 73  traints.  */.  s
2d7a6 61 76 65 64 5f 66 6c 61 67 73 20 3d 20 64 62 2d  aved_flags = db-
2d7a7 3e 66 6c 61 67 73 3b 0a 20 20 73 61 76 65 64 5f  >flags;.  saved_
2d7a8 6e 43 68 61 6e 67 65 20 3d 20 64 62 2d 3e 6e 43  nChange = db->nC
2d7a9 68 61 6e 67 65 3b 0a 20 20 73 61 76 65 64 5f 6e  hange;.  saved_n
2d7aa 54 6f 74 61 6c 43 68 61 6e 67 65 20 3d 20 64 62  TotalChange = db
2d7ab 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b 0a  ->nTotalChange;.
2d7ac 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
2d7ad 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d  QLITE_WriteSchem
2d7ae 61 20 7c 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72  a | SQLITE_Ignor
2d7af 65 43 68 65 63 6b 73 3b 0a 20 20 64 62 2d 3e 66  eChecks;.  db->f
2d7b0 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f  lags &= ~SQLITE_
2d7b1 46 6f 72 65 69 67 6e 4b 65 79 73 3b 0a 0a 20 20  ForeignKeys;..  
2d7b2 70 4d 61 69 6e 20 3d 20 64 62 2d 3e 61 44 62 5b  pMain = db->aDb[
2d7b3 30 5d 2e 70 42 74 3b 0a 20 20 69 73 4d 65 6d 44  0].pBt;.  isMemD
2d7b4 62 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  b = sqlite3Pager
2d7b5 49 73 4d 65 6d 64 62 28 73 71 6c 69 74 65 33 42  IsMemdb(sqlite3B
2d7b6 74 72 65 65 50 61 67 65 72 28 70 4d 61 69 6e 29  treePager(pMain)
2d7b7 29 3b 0a 0a 20 20 2f 2a 20 41 74 74 61 63 68 20  );..  /* Attach 
2d7b8 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61  the temporary da
2d7b9 74 61 62 61 73 65 20 61 73 20 27 76 61 63 75 75  tabase as 'vacuu
2d7ba 6d 5f 64 62 27 2e 20 54 68 65 20 73 79 6e 63 68  m_db'. The synch
2d7bb 72 6f 6e 6f 75 73 20 70 72 61 67 6d 61 0a 20 20  ronous pragma.  
2d7bc 2a 2a 20 63 61 6e 20 62 65 20 73 65 74 20 74 6f  ** can be set to
2d7bd 20 27 6f 66 66 27 20 66 6f 72 20 74 68 69 73 20   'off' for this 
2d7be 66 69 6c 65 2c 20 61 73 20 69 74 20 69 73 20 6e  file, as it is n
2d7bf 6f 74 20 72 65 63 6f 76 65 72 65 64 20 69 66 20  ot recovered if 
2d7c0 61 20 63 72 61 73 68 0a 20 20 2a 2a 20 6f 63 63  a crash.  ** occ
2d7c1 75 72 73 20 61 6e 79 77 61 79 2e 20 54 68 65 20  urs anyway. The 
2d7c2 69 6e 74 65 67 72 69 74 79 20 6f 66 20 74 68 65  integrity of the
2d7c3 20 64 61 74 61 62 61 73 65 20 69 73 20 6d 61 69   database is mai
2d7c4 6e 74 61 69 6e 65 64 20 62 79 20 61 0a 20 20 2a  ntained by a.  *
2d7c5 2a 20 28 70 6f 73 73 69 62 6c 79 20 73 79 6e 63  * (possibly sync
2d7c6 68 72 6f 6e 6f 75 73 29 20 74 72 61 6e 73 61 63  hronous) transac
2d7c7 74 69 6f 6e 20 6f 70 65 6e 65 64 20 6f 6e 20 74  tion opened on t
2d7c8 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
2d7c9 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 73 71 6c   before.  ** sql
2d7ca 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c  ite3BtreeCopyFil
2d7cb 65 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20  e() is called.. 
2d7cc 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 6f 70 74 69   **.  ** An opti
2d7cd 6d 69 73 61 74 69 6f 6e 20 77 6f 75 6c 64 20 62  misation would b
2d7ce 65 20 74 6f 20 75 73 65 20 61 20 6e 6f 6e 2d 6a  e to use a non-j
2d7cf 6f 75 72 6e 61 6c 65 64 20 70 61 67 65 72 2e 0a  ournaled pager..
2d7d0 20 20 2a 2a 20 28 4c 61 74 65 72 3a 29 20 49 20    ** (Later:) I 
2d7d1 74 72 69 65 64 20 73 65 74 74 69 6e 67 20 22 50  tried setting "P
2d7d2 52 41 47 4d 41 20 76 61 63 75 75 6d 5f 64 62 2e  RAGMA vacuum_db.
2d7d3 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4f 46 46  journal_mode=OFF
2d7d4 22 20 62 75 74 0a 20 20 2a 2a 20 74 68 61 74 20  " but.  ** that 
2d7d5 61 63 74 75 61 6c 6c 79 20 6d 61 64 65 20 74 68  actually made th
2d7d6 65 20 56 41 43 55 55 4d 20 72 75 6e 20 73 6c 6f  e VACUUM run slo
2d7d7 77 65 72 2e 20 20 56 65 72 79 20 6c 69 74 74 6c  wer.  Very littl
2d7d8 65 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 0a 20 20  e journalling.  
2d7d9 2a 2a 20 61 63 74 75 61 6c 6c 79 20 6f 63 63 75  ** actually occu
2d7da 72 73 20 77 68 65 6e 20 64 6f 69 6e 67 20 61 20  rs when doing a 
2d7db 76 61 63 75 75 6d 20 73 69 6e 63 65 20 74 68 65  vacuum since the
2d7dc 20 76 61 63 75 75 6d 5f 64 62 20 69 73 20 69 6e   vacuum_db is in
2d7dd 69 74 69 61 6c 6c 79 0a 20 20 2a 2a 20 65 6d 70  itially.  ** emp
2d7de 74 79 2e 20 20 4f 6e 6c 79 20 74 68 65 20 6a 6f  ty.  Only the jo
2d7df 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73 20  urnal header is 
2d7e0 77 72 69 74 74 65 6e 2e 20 20 41 70 70 61 72 65  written.  Appare
2d7e1 6e 74 6c 79 20 69 74 20 74 61 6b 65 73 20 6d 6f  ntly it takes mo
2d7e2 72 65 0a 20 20 2a 2a 20 74 69 6d 65 20 74 6f 20  re.  ** time to 
2d7e3 70 61 72 73 65 20 61 6e 64 20 72 75 6e 20 74 68  parse and run th
2d7e4 65 20 50 52 41 47 4d 41 20 74 6f 20 74 75 72 6e  e PRAGMA to turn
2d7e5 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20 6f 66 66   journalling off
2d7e6 20 74 68 61 6e 20 69 74 20 64 6f 65 73 0a 20 20   than it does.  
2d7e7 2a 2a 20 74 6f 20 77 72 69 74 65 20 74 68 65 20  ** to write the 
2d7e8 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66  journal header f
2d7e9 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 7a 53 71 6c  ile..  */.  zSql
2d7ea 20 3d 20 22 41 54 54 41 43 48 20 27 27 20 41 53   = "ATTACH '' AS
2d7eb 20 76 61 63 75 75 6d 5f 64 62 3b 22 3b 0a 20 20   vacuum_db;";.  
2d7ec 72 63 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c  rc = execSql(db,
2d7ed 20 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 72 63   zSql);.  if( rc
2d7ee 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
2d7ef 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
2d7f0 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61  ;.  pDb = &db->a
2d7f1 44 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d 3b 0a 20  Db[db->nDb-1];. 
2d7f2 20 61 73 73 65 72 74 28 20 73 74 72 63 6d 70 28   assert( strcmp(
2d7f3 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 2d  db->aDb[db->nDb-
2d7f4 31 5d 2e 7a 4e 61 6d 65 2c 22 76 61 63 75 75 6d  1].zName,"vacuum
2d7f5 5f 64 62 22 29 3d 3d 30 20 29 3b 0a 20 20 70 54  _db")==0 );.  pT
2d7f6 65 6d 70 20 3d 20 64 62 2d 3e 61 44 62 5b 64 62  emp = db->aDb[db
2d7f7 2d 3e 6e 44 62 2d 31 5d 2e 70 42 74 3b 0a 0a 20  ->nDb-1].pBt;.. 
2d7f8 20 6e 52 65 73 20 3d 20 73 71 6c 69 74 65 33 42   nRes = sqlite3B
2d7f9 74 72 65 65 47 65 74 52 65 73 65 72 76 65 28 70  treeGetReserve(p
2d7fa 4d 61 69 6e 29 3b 0a 0a 20 20 2f 2a 20 41 20 56  Main);..  /* A V
2d7fb 41 43 55 55 4d 20 63 61 6e 6e 6f 74 20 63 68 61  ACUUM cannot cha
2d7fc 6e 67 65 20 74 68 65 20 70 61 67 65 73 69 7a 65  nge the pagesize
2d7fd 20 6f 66 20 61 6e 20 65 6e 63 72 79 70 74 65 64   of an encrypted
2d7fe 20 64 61 74 61 62 61 73 65 2e 20 2a 2f 0a 23 69   database. */.#i
2d7ff 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f  fdef SQLITE_HAS_
2d800 43 4f 44 45 43 0a 20 20 69 66 28 20 64 62 2d 3e  CODEC.  if( db->
2d801 6e 65 78 74 50 61 67 65 73 69 7a 65 20 29 7b 0a  nextPagesize ){.
2d802 20 20 20 20 65 78 74 65 72 6e 20 76 6f 69 64 20      extern void 
2d803 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b  sqlite3CodecGetK
2d804 65 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  ey(sqlite3*, int
2d805 2c 20 76 6f 69 64 2a 2a 2c 20 69 6e 74 2a 29 3b  , void**, int*);
2d806 0a 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20  .    int nKey;. 
2d807 20 20 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 20     char *zKey;. 
2d808 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 63 47     sqlite3CodecG
2d809 65 74 4b 65 79 28 64 62 2c 20 30 2c 20 28 76 6f  etKey(db, 0, (vo
2d80a 69 64 2a 2a 29 26 7a 4b 65 79 2c 20 26 6e 4b 65  id**)&zKey, &nKe
2d80b 79 29 3b 0a 20 20 20 20 69 66 28 20 6e 4b 65 79  y);.    if( nKey
2d80c 20 29 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73   ) db->nextPages
2d80d 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e  ize = 0;.  }.#en
2d80e 64 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69 74  dif..  if( sqlit
2d80f 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53 69  e3BtreeSetPageSi
2d810 7a 65 28 70 54 65 6d 70 2c 20 73 71 6c 69 74 65  ze(pTemp, sqlite
2d811 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a  3BtreeGetPageSiz
2d812 65 28 70 4d 61 69 6e 29 2c 20 6e 52 65 73 2c 20  e(pMain), nRes, 
2d813 30 29 0a 20 20 20 7c 7c 20 28 21 69 73 4d 65 6d  0).   || (!isMem
2d814 44 62 20 26 26 20 73 71 6c 69 74 65 33 42 74 72  Db && sqlite3Btr
2d815 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 54  eeSetPageSize(pT
2d816 65 6d 70 2c 20 64 62 2d 3e 6e 65 78 74 50 61 67  emp, db->nextPag
2d817 65 73 69 7a 65 2c 20 6e 52 65 73 2c 20 30 29 29  esize, nRes, 0))
2d818 0a 20 20 20 7c 7c 20 4e 45 56 45 52 28 64 62 2d  .   || NEVER(db-
2d819 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 0a 20  >mallocFailed). 
2d81a 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
2d81b 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67  ITE_NOMEM;.    g
2d81c 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75  oto end_of_vacuu
2d81d 6d 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 65 78  m;.  }.  rc = ex
2d81e 65 63 53 71 6c 28 64 62 2c 20 22 50 52 41 47 4d  ecSql(db, "PRAGM
2d81f 41 20 76 61 63 75 75 6d 5f 64 62 2e 73 79 6e 63  A vacuum_db.sync
2d820 68 72 6f 6e 6f 75 73 3d 4f 46 46 22 29 3b 0a 20  hronous=OFF");. 
2d821 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2d822 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  OK ){.    goto e
2d823 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20  nd_of_vacuum;.  
2d824 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
2d825 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
2d826 4d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  M.  sqlite3Btree
2d827 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70 54  SetAutoVacuum(pT
2d828 65 6d 70 2c 20 64 62 2d 3e 6e 65 78 74 41 75 74  emp, db->nextAut
2d829 6f 76 61 63 3e 3d 30 20 3f 20 64 62 2d 3e 6e 65  ovac>=0 ? db->ne
2d82a 78 74 41 75 74 6f 76 61 63 20 3a 0a 20 20 20 20  xtAutovac :.    
2d82b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d82c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d82d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
2d82e 72 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d  reeGetAutoVacuum
2d82f 28 70 4d 61 69 6e 29 29 3b 0a 23 65 6e 64 69 66  (pMain));.#endif
2d830 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 74  ..  /* Begin a t
2d831 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20  ransaction */.  
2d832 72 63 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c  rc = execSql(db,
2d833 20 22 42 45 47 49 4e 20 45 58 43 4c 55 53 49 56   "BEGIN EXCLUSIV
2d834 45 3b 22 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  E;");.  if( rc!=
2d835 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
2d836 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a   end_of_vacuum;.
2d837 0a 20 20 2f 2a 20 51 75 65 72 79 20 74 68 65 20  .  /* Query the 
2d838 73 63 68 65 6d 61 20 6f 66 20 74 68 65 20 6d 61  schema of the ma
2d839 69 6e 20 64 61 74 61 62 61 73 65 2e 20 43 72 65  in database. Cre
2d83a 61 74 65 20 61 20 6d 69 72 72 6f 72 20 73 63 68  ate a mirror sch
2d83b 65 6d 61 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20  ema.  ** in the 
2d83c 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61  temporary databa
2d83d 73 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  se..  */.  rc = 
2d83e 65 78 65 63 45 78 65 63 53 71 6c 28 64 62 2c 20  execExecSql(db, 
2d83f 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 27  .      "SELECT '
2d840 43 52 45 41 54 45 20 54 41 42 4c 45 20 76 61 63  CREATE TABLE vac
2d841 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 73 75 62 73  uum_db.' || subs
2d842 74 72 28 73 71 6c 2c 31 34 29 20 22 0a 20 20 20  tr(sql,14) ".   
2d843 20 20 20 22 20 20 46 52 4f 4d 20 73 71 6c 69 74     "  FROM sqlit
2d844 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20 74  e_master WHERE t
2d845 79 70 65 3d 27 74 61 62 6c 65 27 20 41 4e 44 20  ype='table' AND 
2d846 6e 61 6d 65 21 3d 27 73 71 6c 69 74 65 5f 73 65  name!='sqlite_se
2d847 71 75 65 6e 63 65 27 22 0a 20 20 20 20 20 20 22  quence'".      "
2d848 20 20 20 41 4e 44 20 72 6f 6f 74 70 61 67 65 3e     AND rootpage>
2d849 30 22 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63  0".  );.  if( rc
2d84a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
2d84b 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
2d84c 3b 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65  ;.  rc = execExe
2d84d 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20  cSql(db, .      
2d84e 22 53 45 4c 45 43 54 20 27 43 52 45 41 54 45 20  "SELECT 'CREATE 
2d84f 49 4e 44 45 58 20 76 61 63 75 75 6d 5f 64 62 2e  INDEX vacuum_db.
2d850 27 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c 2c  ' || substr(sql,
2d851 31 34 29 22 0a 20 20 20 20 20 20 22 20 20 46 52  14)".      "  FR
2d852 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  OM sqlite_master
2d853 20 57 48 45 52 45 20 73 71 6c 20 4c 49 4b 45 20   WHERE sql LIKE 
2d854 27 43 52 45 41 54 45 20 49 4e 44 45 58 20 25 27  'CREATE INDEX %'
2d855 20 22 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53   ");.  if( rc!=S
2d856 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
2d857 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20  end_of_vacuum;. 
2d858 20 72 63 20 3d 20 65 78 65 63 45 78 65 63 53 71   rc = execExecSq
2d859 6c 28 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45  l(db, .      "SE
2d85a 4c 45 43 54 20 27 43 52 45 41 54 45 20 55 4e 49  LECT 'CREATE UNI
2d85b 51 55 45 20 49 4e 44 45 58 20 76 61 63 75 75 6d  QUE INDEX vacuum
2d85c 5f 64 62 2e 27 20 7c 7c 20 73 75 62 73 74 72 28  _db.' || substr(
2d85d 73 71 6c 2c 32 31 29 20 22 0a 20 20 20 20 20 20  sql,21) ".      
2d85e 22 20 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d  "  FROM sqlite_m
2d85f 61 73 74 65 72 20 57 48 45 52 45 20 73 71 6c 20  aster WHERE sql 
2d860 4c 49 4b 45 20 27 43 52 45 41 54 45 20 55 4e 49  LIKE 'CREATE UNI
2d861 51 55 45 20 49 4e 44 45 58 20 25 27 22 29 3b 0a  QUE INDEX %'");.
2d862 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2d863 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
2d864 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20  f_vacuum;..  /* 
2d865 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65  Loop through the
2d866 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6d   tables in the m
2d867 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20 46 6f  ain database. Fo
2d868 72 20 65 61 63 68 2c 20 64 6f 0a 20 20 2a 2a 20  r each, do.  ** 
2d869 61 6e 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20  an "INSERT INTO 
2d86a 76 61 63 75 75 6d 5f 64 62 2e 78 78 78 20 53 45  vacuum_db.xxx SE
2d86b 4c 45 43 54 20 2a 20 46 52 4f 4d 20 78 78 78 3b  LECT * FROM xxx;
2d86c 22 20 74 6f 20 63 6f 70 79 0a 20 20 2a 2a 20 74  " to copy.  ** t
2d86d 68 65 20 63 6f 6e 74 65 6e 74 73 20 74 6f 20 74  he contents to t
2d86e 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74  he temporary dat
2d86f 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 72 63  abase..  */.  rc
2d870 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28 64   = execExecSql(d
2d871 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43  b, .      "SELEC
2d872 54 20 27 49 4e 53 45 52 54 20 49 4e 54 4f 20 76  T 'INSERT INTO v
2d873 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 71 75  acuum_db.' || qu
2d874 6f 74 65 28 6e 61 6d 65 29 20 22 0a 20 20 20 20  ote(name) ".    
2d875 20 20 22 7c 7c 20 27 20 53 45 4c 45 43 54 20 2a    "|| ' SELECT *
2d876 20 46 52 4f 4d 20 27 20 7c 7c 20 71 75 6f 74 65   FROM ' || quote
2d877 28 6e 61 6d 65 29 20 7c 7c 20 27 3b 27 22 0a 20  (name) || ';'". 
2d878 20 20 20 20 20 22 46 52 4f 4d 20 73 71 6c 69 74       "FROM sqlit
2d879 65 5f 6d 61 73 74 65 72 20 22 0a 20 20 20 20 20  e_master ".     
2d87a 20 22 57 48 45 52 45 20 74 79 70 65 20 3d 20 27   "WHERE type = '
2d87b 74 61 62 6c 65 27 20 41 4e 44 20 6e 61 6d 65 21  table' AND name!
2d87c 3d 27 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  ='sqlite_sequenc
2d87d 65 27 20 22 0a 20 20 20 20 20 20 22 20 20 41 4e  e' ".      "  AN
2d87e 44 20 72 6f 6f 74 70 61 67 65 3e 30 22 0a 0a 20  D rootpage>0".. 
2d87f 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51   );.  if( rc!=SQ
2d880 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
2d881 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 20  nd_of_vacuum;.. 
2d882 20 2f 2a 20 43 6f 70 79 20 6f 76 65 72 20 74 68   /* Copy over th
2d883 65 20 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65  e sequence table
2d884 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65  .  */.  rc = exe
2d885 63 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20  cExecSql(db, .  
2d886 20 20 20 20 22 53 45 4c 45 43 54 20 27 44 45 4c      "SELECT 'DEL
2d887 45 54 45 20 46 52 4f 4d 20 76 61 63 75 75 6d 5f  ETE FROM vacuum_
2d888 64 62 2e 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61  db.' || quote(na
2d889 6d 65 29 20 7c 7c 20 27 3b 27 20 22 0a 20 20 20  me) || ';' ".   
2d88a 20 20 20 22 46 52 4f 4d 20 76 61 63 75 75 6d 5f     "FROM vacuum_
2d88b 64 62 2e 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  db.sqlite_master
2d88c 20 57 48 45 52 45 20 6e 61 6d 65 3d 27 73 71 6c   WHERE name='sql
2d88d 69 74 65 5f 73 65 71 75 65 6e 63 65 27 20 22 0a  ite_sequence' ".
2d88e 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53    );.  if( rc!=S
2d88f 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
2d890 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20  end_of_vacuum;. 
2d891 20 72 63 20 3d 20 65 78 65 63 45 78 65 63 53 71   rc = execExecSq
2d892 6c 28 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45  l(db, .      "SE
2d893 4c 45 43 54 20 27 49 4e 53 45 52 54 20 49 4e 54  LECT 'INSERT INT
2d894 4f 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c  O vacuum_db.' ||
2d895 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 22 0a 20   quote(name) ". 
2d896 20 20 20 20 20 22 7c 7c 20 27 20 53 45 4c 45 43       "|| ' SELEC
2d897 54 20 2a 20 46 52 4f 4d 20 27 20 7c 7c 20 71 75  T * FROM ' || qu
2d898 6f 74 65 28 6e 61 6d 65 29 20 7c 7c 20 27 3b 27  ote(name) || ';'
2d899 20 22 0a 20 20 20 20 20 20 22 46 52 4f 4d 20 76   ".      "FROM v
2d89a 61 63 75 75 6d 5f 64 62 2e 73 71 6c 69 74 65 5f  acuum_db.sqlite_
2d89b 6d 61 73 74 65 72 20 57 48 45 52 45 20 6e 61 6d  master WHERE nam
2d89c 65 3d 3d 27 73 71 6c 69 74 65 5f 73 65 71 75 65  e=='sqlite_seque
2d89d 6e 63 65 27 3b 22 0a 20 20 29 3b 0a 20 20 69 66  nce';".  );.  if
2d89e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2d89f 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61  ) goto end_of_va
2d8a0 63 75 75 6d 3b 0a 0a 0a 20 20 2f 2a 20 43 6f 70  cuum;...  /* Cop
2d8a1 79 20 74 68 65 20 74 72 69 67 67 65 72 73 2c 20  y the triggers, 
2d8a2 76 69 65 77 73 2c 20 61 6e 64 20 76 69 72 74 75  views, and virtu
2d8a3 61 6c 20 74 61 62 6c 65 73 20 66 72 6f 6d 20 74  al tables from t
2d8a4 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
2d8a5 0a 20 20 2a 2a 20 6f 76 65 72 20 74 6f 20 74 68  .  ** over to th
2d8a6 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61  e temporary data
2d8a7 62 61 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 20 74  base.  None of t
2d8a8 68 65 73 65 20 6f 62 6a 65 63 74 73 20 68 61 73  hese objects has
2d8a9 20 61 6e 79 0a 20 20 2a 2a 20 61 73 73 6f 63 69   any.  ** associ
2d8aa 61 74 65 64 20 73 74 6f 72 61 67 65 2c 20 73 6f  ated storage, so
2d8ab 20 61 6c 6c 20 77 65 20 68 61 76 65 20 74 6f 20   all we have to 
2d8ac 64 6f 20 69 73 20 63 6f 70 79 20 74 68 65 69 72  do is copy their
2d8ad 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 66 72   entries.  ** fr
2d8ae 6f 6d 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41  om the SQLITE_MA
2d8af 53 54 45 52 20 74 61 62 6c 65 2e 0a 20 20 2a 2f  STER table..  */
2d8b0 0a 20 20 72 63 20 3d 20 65 78 65 63 53 71 6c 28  .  rc = execSql(
2d8b1 64 62 2c 0a 20 20 20 20 20 20 22 49 4e 53 45 52  db,.      "INSER
2d8b2 54 20 49 4e 54 4f 20 76 61 63 75 75 6d 5f 64 62  T INTO vacuum_db
2d8b3 2e 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 22  .sqlite_master "
2d8b4 0a 20 20 20 20 20 20 22 20 20 53 45 4c 45 43 54  .      "  SELECT
2d8b5 20 74 79 70 65 2c 20 6e 61 6d 65 2c 20 74 62 6c   type, name, tbl
2d8b6 5f 6e 61 6d 65 2c 20 72 6f 6f 74 70 61 67 65 2c  _name, rootpage,
2d8b7 20 73 71 6c 22 0a 20 20 20 20 20 20 22 20 20 20   sql".      "   
2d8b8 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73   FROM sqlite_mas
2d8b9 74 65 72 22 0a 20 20 20 20 20 20 22 20 20 20 57  ter".      "   W
2d8ba 48 45 52 45 20 74 79 70 65 3d 27 76 69 65 77 27  HERE type='view'
2d8bb 20 4f 52 20 74 79 70 65 3d 27 74 72 69 67 67 65   OR type='trigge
2d8bc 72 27 22 0a 20 20 20 20 20 20 22 20 20 20 20 20  r'".      "     
2d8bd 20 4f 52 20 28 74 79 70 65 3d 27 74 61 62 6c 65   OR (type='table
2d8be 27 20 41 4e 44 20 72 6f 6f 74 70 61 67 65 3d 30  ' AND rootpage=0
2d8bf 29 22 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63  )".  );.  if( rc
2d8c0 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76   ) goto end_of_v
2d8c1 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20 41 74 20  acuum;..  /* At 
2d8c2 74 68 69 73 20 70 6f 69 6e 74 2c 20 75 6e 6c 65  this point, unle
2d8c3 73 73 20 74 68 65 20 6d 61 69 6e 20 64 62 20 77  ss the main db w
2d8c4 61 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 6d  as completely em
2d8c5 70 74 79 2c 20 74 68 65 72 65 20 69 73 20 6e 6f  pty, there is no
2d8c6 77 20 61 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63  w a.  ** transac
2d8c7 74 69 6f 6e 20 6f 70 65 6e 20 6f 6e 20 74 68 65  tion open on the
2d8c8 20 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65   vacuum database
2d8c9 2c 20 62 75 74 20 6e 6f 74 20 6f 6e 20 74 68 65  , but not on the
2d8ca 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e 0a   main database..
2d8cb 20 20 2a 2a 20 4f 70 65 6e 20 61 20 62 74 72 65    ** Open a btre
2d8cc 65 20 6c 65 76 65 6c 20 74 72 61 6e 73 61 63 74  e level transact
2d8cd 69 6f 6e 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20  ion on the main 
2d8ce 64 61 74 61 62 61 73 65 2e 20 54 68 69 73 20 61  database. This a
2d8cf 6c 6c 6f 77 73 20 61 0a 20 20 2a 2a 20 63 61 6c  llows a.  ** cal
2d8d0 6c 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65  l to sqlite3Btre
2d8d1 65 43 6f 70 79 46 69 6c 65 28 29 2e 20 54 68 65  eCopyFile(). The
2d8d2 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 62   main database b
2d8d3 74 72 65 65 20 6c 65 76 65 6c 0a 20 20 2a 2a 20  tree level.  ** 
2d8d4 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 74  transaction is t
2d8d5 68 65 6e 20 63 6f 6d 6d 69 74 74 65 64 2c 20 73  hen committed, s
2d8d6 6f 20 74 68 65 20 53 51 4c 20 6c 65 76 65 6c 20  o the SQL level 
2d8d7 6e 65 76 65 72 20 6b 6e 6f 77 73 20 69 74 20 77  never knows it w
2d8d8 61 73 0a 20 20 2a 2a 20 6f 70 65 6e 65 64 20 66  as.  ** opened f
2d8d9 6f 72 20 77 72 69 74 69 6e 67 2e 20 54 68 69 73  or writing. This
2d8da 20 77 61 79 2c 20 74 68 65 20 53 51 4c 20 74 72   way, the SQL tr
2d8db 61 6e 73 61 63 74 69 6f 6e 20 75 73 65 64 20 74  ansaction used t
2d8dc 6f 20 63 72 65 61 74 65 20 74 68 65 0a 20 20 2a  o create the.  *
2d8dd 2a 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61  * temporary data
2d8de 62 61 73 65 20 6e 65 76 65 72 20 6e 65 65 64 73  base never needs
2d8df 20 74 6f 20 62 65 20 63 6f 6d 6d 69 74 74 65 64   to be committed
2d8e0 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75  ..  */.  {.    u
2d8e1 33 32 20 6d 65 74 61 3b 0a 20 20 20 20 69 6e 74  32 meta;.    int
2d8e2 20 69 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73   i;..    /* This
2d8e3 20 61 72 72 61 79 20 64 65 74 65 72 6d 69 6e 65   array determine
2d8e4 73 20 77 68 69 63 68 20 6d 65 74 61 20 6d 65 74  s which meta met
2d8e5 61 20 76 61 6c 75 65 73 20 61 72 65 20 70 72 65  a values are pre
2d8e6 73 65 72 76 65 64 20 69 6e 20 74 68 65 0a 20 20  served in the.  
2d8e7 20 20 2a 2a 20 76 61 63 75 75 6d 2e 20 20 45 76    ** vacuum.  Ev
2d8e8 65 6e 20 65 6e 74 72 69 65 73 20 61 72 65 20 74  en entries are t
2d8e9 68 65 20 6d 65 74 61 20 76 61 6c 75 65 20 6e 75  he meta value nu
2d8ea 6d 62 65 72 20 61 6e 64 20 6f 64 64 20 65 6e 74  mber and odd ent
2d8eb 72 69 65 73 0a 20 20 20 20 2a 2a 20 61 72 65 20  ries.    ** are 
2d8ec 61 6e 20 69 6e 63 72 65 6d 65 6e 74 20 74 6f 20  an increment to 
2d8ed 61 70 70 6c 79 20 74 6f 20 74 68 65 20 6d 65 74  apply to the met
2d8ee 61 20 76 61 6c 75 65 20 61 66 74 65 72 20 74 68  a value after th
2d8ef 65 20 76 61 63 75 75 6d 2e 0a 20 20 20 20 2a 2a  e vacuum..    **
2d8f0 20 54 68 65 20 69 6e 63 72 65 6d 65 6e 74 20 69   The increment i
2d8f1 73 20 75 73 65 64 20 74 6f 20 69 6e 63 72 65 61  s used to increa
2d8f2 73 65 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f  se the schema co
2d8f3 6f 6b 69 65 20 73 6f 20 74 68 61 74 20 6f 74 68  okie so that oth
2d8f4 65 72 0a 20 20 20 20 2a 2a 20 63 6f 6e 6e 65 63  er.    ** connec
2d8f5 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 73 61 6d  tions to the sam
2d8f6 65 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20  e database will 
2d8f7 6b 6e 6f 77 20 74 6f 20 72 65 72 65 61 64 20 74  know to reread t
2d8f8 68 65 20 73 63 68 65 6d 61 2e 0a 20 20 20 20 2a  he schema..    *
2d8f9 2f 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  /.    static con
2d8fa 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
2d8fb 20 61 43 6f 70 79 5b 5d 20 3d 20 7b 0a 20 20 20   aCopy[] = {.   
2d8fc 20 20 20 20 42 54 52 45 45 5f 53 43 48 45 4d 41      BTREE_SCHEMA
2d8fd 5f 56 45 52 53 49 4f 4e 2c 20 20 20 20 20 31 2c  _VERSION,     1,
2d8fe 20 20 2f 2a 20 41 64 64 20 6f 6e 65 20 74 6f 20    /* Add one to 
2d8ff 74 68 65 20 6f 6c 64 20 73 63 68 65 6d 61 20 63  the old schema c
2d900 6f 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 20 20 20  ookie */.       
2d901 42 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43 41  BTREE_DEFAULT_CA
2d902 43 48 45 5f 53 49 5a 45 2c 20 30 2c 20 20 2f 2a  CHE_SIZE, 0,  /*
2d903 20 50 72 65 73 65 72 76 65 20 74 68 65 20 64 65   Preserve the de
2d904 66 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65  fault page cache
2d905 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20 20 20   size */.       
2d906 42 54 52 45 45 5f 54 45 58 54 5f 45 4e 43 4f 44  BTREE_TEXT_ENCOD
2d907 49 4e 47 2c 20 20 20 20 20 20 30 2c 20 20 2f 2a  ING,      0,  /*
2d908 20 50 72 65 73 65 72 76 65 20 74 68 65 20 74 65   Preserve the te
2d909 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20  xt encoding */. 
2d90a 20 20 20 20 20 20 42 54 52 45 45 5f 55 53 45 52        BTREE_USER
2d90b 5f 56 45 52 53 49 4f 4e 2c 20 20 20 20 20 20 20  _VERSION,       
2d90c 30 2c 20 20 2f 2a 20 50 72 65 73 65 72 76 65 20  0,  /* Preserve 
2d90d 74 68 65 20 75 73 65 72 20 76 65 72 73 69 6f 6e  the user version
2d90e 20 2a 2f 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20   */.    };..    
2d90f 61 73 73 65 72 74 28 20 31 3d 3d 73 71 6c 69 74  assert( 1==sqlit
2d910 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73  e3BtreeIsInTrans
2d911 28 70 54 65 6d 70 29 20 29 3b 0a 20 20 20 20 61  (pTemp) );.    a
2d912 73 73 65 72 74 28 20 31 3d 3d 73 71 6c 69 74 65  ssert( 1==sqlite
2d913 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28  3BtreeIsInTrans(
2d914 70 4d 61 69 6e 29 20 29 3b 0a 0a 20 20 20 20 2f  pMain) );..    /
2d915 2a 20 43 6f 70 79 20 42 74 72 65 65 20 6d 65 74  * Copy Btree met
2d916 61 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 20 20  a values */.    
2d917 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79  for(i=0; i<Array
2d918 53 69 7a 65 28 61 43 6f 70 79 29 3b 20 69 2b 3d  Size(aCopy); i+=
2d919 32 29 7b 0a 20 20 20 20 20 20 2f 2a 20 47 65 74  2){.      /* Get
2d91a 4d 65 74 61 28 29 20 61 6e 64 20 55 70 64 61 74  Meta() and Updat
2d91b 65 4d 65 74 61 28 29 20 63 61 6e 6e 6f 74 20 66  eMeta() cannot f
2d91c 61 69 6c 20 69 6e 20 74 68 69 73 20 63 6f 6e 74  ail in this cont
2d91d 65 78 74 20 62 65 63 61 75 73 65 0a 20 20 20 20  ext because.    
2d91e 20 20 2a 2a 20 77 65 20 61 6c 72 65 61 64 79 20    ** we already 
2d91f 68 61 76 65 20 70 61 67 65 20 31 20 6c 6f 61 64  have page 1 load
2d920 65 64 20 69 6e 74 6f 20 63 61 63 68 65 20 61 6e  ed into cache an
2d921 64 20 6d 61 72 6b 65 64 20 64 69 72 74 79 2e 20  d marked dirty. 
2d922 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
2d923 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 4d 61  BtreeGetMeta(pMa
2d924 69 6e 2c 20 61 43 6f 70 79 5b 69 5d 2c 20 26 6d  in, aCopy[i], &m
2d925 65 74 61 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  eta);.      rc =
2d926 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64   sqlite3BtreeUpd
2d927 61 74 65 4d 65 74 61 28 70 54 65 6d 70 2c 20 61  ateMeta(pTemp, a
2d928 43 6f 70 79 5b 69 5d 2c 20 6d 65 74 61 2b 61 43  Copy[i], meta+aC
2d929 6f 70 79 5b 69 2b 31 5d 29 3b 0a 20 20 20 20 20  opy[i+1]);.     
2d92a 20 69 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53   if( NEVER(rc!=S
2d92b 51 4c 49 54 45 5f 4f 4b 29 20 29 20 67 6f 74 6f  QLITE_OK) ) goto
2d92c 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a   end_of_vacuum;.
2d92d 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20      }..    rc = 
2d92e 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79  sqlite3BtreeCopy
2d92f 46 69 6c 65 28 70 4d 61 69 6e 2c 20 70 54 65 6d  File(pMain, pTem
2d930 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  p);.    if( rc!=
2d931 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
2d932 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a   end_of_vacuum;.
2d933 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2d934 42 74 72 65 65 43 6f 6d 6d 69 74 28 70 54 65 6d  BtreeCommit(pTem
2d935 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  p);.    if( rc!=
2d936 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
2d937 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a   end_of_vacuum;.
2d938 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2d939 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
2d93a 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53     sqlite3BtreeS
2d93b 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70 4d 61  etAutoVacuum(pMa
2d93c 69 6e 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65  in, sqlite3Btree
2d93d 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70 54  GetAutoVacuum(pT
2d93e 65 6d 70 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20  emp));.#endif.  
2d93f 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d  }..  assert( rc=
2d940 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
2d941 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
2d942 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 4d 61  eSetPageSize(pMa
2d943 69 6e 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65  in, sqlite3Btree
2d944 47 65 74 50 61 67 65 53 69 7a 65 28 70 54 65 6d  GetPageSize(pTem
2d945 70 29 2c 20 6e 52 65 73 2c 31 29 3b 0a 0a 65 6e  p), nRes,1);..en
2d946 64 5f 6f 66 5f 76 61 63 75 75 6d 3a 0a 20 20 2f  d_of_vacuum:.  /
2d947 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20 6f 72  * Restore the or
2d948 69 67 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20  iginal value of 
2d949 64 62 2d 3e 66 6c 61 67 73 20 2a 2f 0a 20 20 64  db->flags */.  d
2d94a 62 2d 3e 66 6c 61 67 73 20 3d 20 73 61 76 65 64  b->flags = saved
2d94b 5f 66 6c 61 67 73 3b 0a 20 20 64 62 2d 3e 6e 43  _flags;.  db->nC
2d94c 68 61 6e 67 65 20 3d 20 73 61 76 65 64 5f 6e 43  hange = saved_nC
2d94d 68 61 6e 67 65 3b 0a 20 20 64 62 2d 3e 6e 54 6f  hange;.  db->nTo
2d94e 74 61 6c 43 68 61 6e 67 65 20 3d 20 73 61 76 65  talChange = save
2d94f 64 5f 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b 0a  d_nTotalChange;.
2d950 0a 20 20 2f 2a 20 43 75 72 72 65 6e 74 6c 79 20  .  /* Currently 
2d951 74 68 65 72 65 20 69 73 20 61 6e 20 53 51 4c 20  there is an SQL 
2d952 6c 65 76 65 6c 20 74 72 61 6e 73 61 63 74 69 6f  level transactio
2d953 6e 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 76 61  n open on the va
2d954 63 75 75 6d 0a 20 20 2a 2a 20 64 61 74 61 62 61  cuum.  ** databa
2d955 73 65 2e 20 4e 6f 20 6c 6f 63 6b 73 20 61 72 65  se. No locks are
2d956 20 68 65 6c 64 20 6f 6e 20 61 6e 79 20 6f 74 68   held on any oth
2d957 65 72 20 66 69 6c 65 73 20 28 73 69 6e 63 65 20  er files (since 
2d958 74 68 65 20 6d 61 69 6e 20 66 69 6c 65 0a 20 20  the main file.  
2d959 2a 2a 20 77 61 73 20 63 6f 6d 6d 69 74 74 65 64  ** was committed
2d95a 20 61 74 20 74 68 65 20 62 74 72 65 65 20 6c 65   at the btree le
2d95b 76 65 6c 29 2e 20 53 6f 20 69 74 20 73 61 66 65  vel). So it safe
2d95c 20 74 6f 20 65 6e 64 20 74 68 65 20 74 72 61 6e   to end the tran
2d95d 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20 62 79 20  saction.  ** by 
2d95e 6d 61 6e 75 61 6c 6c 79 20 73 65 74 74 69 6e 67  manually setting
2d95f 20 74 68 65 20 61 75 74 6f 43 6f 6d 6d 69 74 20   the autoCommit 
2d960 66 6c 61 67 20 74 6f 20 74 72 75 65 20 61 6e 64  flag to true and
2d961 20 64 65 74 61 63 68 69 6e 67 20 74 68 65 0a 20   detaching the. 
2d962 20 2a 2a 20 76 61 63 75 75 6d 20 64 61 74 61 62   ** vacuum datab
2d963 61 73 65 2e 20 54 68 65 20 76 61 63 75 75 6d 5f  ase. The vacuum_
2d964 64 62 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  db journal file 
2d965 69 73 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20  is deleted when 
2d966 74 68 65 20 70 61 67 65 72 0a 20 20 2a 2a 20 69  the pager.  ** i
2d967 73 20 63 6c 6f 73 65 64 20 62 79 20 74 68 65 20  s closed by the 
2d968 44 45 54 41 43 48 2e 0a 20 20 2a 2f 0a 20 20 64  DETACH..  */.  d
2d969 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20  b->autoCommit = 
2d96a 31 3b 0a 0a 20 20 69 66 28 20 70 44 62 20 29 7b  1;..  if( pDb ){
2d96b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
2d96c 65 43 6c 6f 73 65 28 70 44 62 2d 3e 70 42 74 29  eClose(pDb->pBt)
2d96d 3b 0a 20 20 20 20 70 44 62 2d 3e 70 42 74 20 3d  ;.    pDb->pBt =
2d96e 20 30 3b 0a 20 20 20 20 70 44 62 2d 3e 70 53 63   0;.    pDb->pSc
2d96f 68 65 6d 61 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  hema = 0;.  }.. 
2d970 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
2d971 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
2d972 30 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63  0);..  return rc
2d973 3b 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53  ;.}.#endif  /* S
2d974 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55  QLITE_OMIT_VACUU
2d975 4d 20 26 26 20 53 51 4c 49 54 45 5f 4f 4d 49 54  M && SQLITE_OMIT
2d976 5f 41 54 54 41 43 48 20 2a 2f 0a 0a 2f 2a 2a 2a  _ATTACH */../***
2d977 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
2d978 6f 66 20 76 61 63 75 75 6d 2e 63 20 2a 2a 2a 2a  of vacuum.c ****
2d979 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d97a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d97b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2d97c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
2d97d 6e 20 66 69 6c 65 20 76 74 61 62 2e 63 20 2a 2a  n file vtab.c **
2d97e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d97f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
2d981 2a 20 32 30 30 36 20 4a 75 6e 65 20 31 30 0a 2a  * 2006 June 10.*
2d982 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
2d983 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
2d984 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
2d985 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
2d986 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
2d987 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
2d988 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
2d989 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
2d98a 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
2d98b 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
2d98c 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
2d98d 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
2d98e 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
2d98f 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
2d990 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
2d991 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
2d992 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
2d993 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
2d994 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d995 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d996 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d997 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
2d998 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
2d999 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f  ins code used to
2d99a 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74 20   help implement 
2d99b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a  virtual tables..
2d99c 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 74 61 62 2e  **.** $Id: vtab.
2d99d 63 2c 76 20 31 2e 39 34 20 32 30 30 39 2f 30 38  c,v 1.94 2009/08
2d99e 2f 30 38 20 31 38 3a 30 31 3a 30 38 20 64 72 68  /08 18:01:08 drh
2d99f 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65   Exp $.*/.#ifnde
2d9a0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2d9a1 52 54 55 41 4c 54 41 42 4c 45 0a 0a 2f 2a 0a 2a  RTUALTABLE../*.*
2d9a2 2a 20 54 68 65 20 61 63 74 75 61 6c 20 66 75 6e  * The actual fun
2d9a3 63 74 69 6f 6e 20 74 68 61 74 20 64 6f 65 73 20  ction that does 
2d9a4 74 68 65 20 77 6f 72 6b 20 6f 66 20 63 72 65 61  the work of crea
2d9a5 74 69 6e 67 20 61 20 6e 65 77 20 6d 6f 64 75 6c  ting a new modul
2d9a6 65 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  e..** This funct
2d9a7 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  ion implements t
2d9a8 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  he sqlite3_creat
2d9a9 65 5f 6d 6f 64 75 6c 65 28 29 20 61 6e 64 0a 2a  e_module() and.*
2d9aa 2a 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  * sqlite3_create
2d9ab 5f 6d 6f 64 75 6c 65 5f 76 32 28 29 20 69 6e 74  _module_v2() int
2d9ac 65 72 66 61 63 65 73 2e 0a 2a 2f 0a 73 74 61 74  erfaces..*/.stat
2d9ad 69 63 20 69 6e 74 20 63 72 65 61 74 65 4d 6f 64  ic int createMod
2d9ae 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ule(.  sqlite3 *
2d9af 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
2d9b0 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2d9b1 73 65 20 69 6e 20 77 68 69 63 68 20 6d 6f 64 75  se in which modu
2d9b2 6c 65 20 69 73 20 72 65 67 69 73 74 65 72 65 64  le is registered
2d9b3 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
2d9b4 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20   *zName,        
2d9b5 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 61 73        /* Name as
2d9b6 73 69 67 6e 65 64 20 74 6f 20 74 68 69 73 20 6d  signed to this m
2d9b7 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  odule */.  const
2d9b8 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
2d9b9 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20 54 68  *pModule,  /* Th
2d9ba 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20  e definition of 
2d9bb 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20  the module */.  
2d9bc 76 6f 69 64 20 2a 70 41 75 78 2c 20 20 20 20 20  void *pAux,     
2d9bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d9be 2f 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74  /* Context point
2d9bf 65 72 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78  er for xCreate/x
2d9c0 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69  Connect */.  voi
2d9c1 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f  d (*xDestroy)(vo
2d9c2 69 64 20 2a 29 20 20 20 20 20 20 20 20 2f 2a 20  id *)        /* 
2d9c3 4d 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74 6f  Module destructo
2d9c4 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b  r function */.){
2d9c5 0a 20 20 69 6e 74 20 72 63 2c 20 6e 4e 61 6d 65  .  int rc, nName
2d9c6 3b 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64  ;.  Module *pMod
2d9c7 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ;..  sqlite3_mut
2d9c8 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
2d9c9 65 78 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73  ex);.  nName = s
2d9ca 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
2d9cb 4e 61 6d 65 29 3b 0a 20 20 70 4d 6f 64 20 3d 20  Name);.  pMod = 
2d9cc 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65  (Module *)sqlite
2d9cd 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
2d9ce 20 73 69 7a 65 6f 66 28 4d 6f 64 75 6c 65 29 20   sizeof(Module) 
2d9cf 2b 20 6e 4e 61 6d 65 20 2b 20 31 29 3b 0a 20 20  + nName + 1);.  
2d9d0 69 66 28 20 70 4d 6f 64 20 29 7b 0a 20 20 20 20  if( pMod ){.    
2d9d1 4d 6f 64 75 6c 65 20 2a 70 44 65 6c 3b 0a 20 20  Module *pDel;.  
2d9d2 20 20 63 68 61 72 20 2a 7a 43 6f 70 79 20 3d 20    char *zCopy = 
2d9d3 28 63 68 61 72 20 2a 29 28 26 70 4d 6f 64 5b 31  (char *)(&pMod[1
2d9d4 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a  ]);.    memcpy(z
2d9d5 43 6f 70 79 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  Copy, zName, nNa
2d9d6 6d 65 2b 31 29 3b 0a 20 20 20 20 70 4d 6f 64 2d  me+1);.    pMod-
2d9d7 3e 7a 4e 61 6d 65 20 3d 20 7a 43 6f 70 79 3b 0a  >zName = zCopy;.
2d9d8 20 20 20 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c      pMod->pModul
2d9d9 65 20 3d 20 70 4d 6f 64 75 6c 65 3b 0a 20 20 20  e = pModule;.   
2d9da 20 70 4d 6f 64 2d 3e 70 41 75 78 20 3d 20 70 41   pMod->pAux = pA
2d9db 75 78 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e 78 44  ux;.    pMod->xD
2d9dc 65 73 74 72 6f 79 20 3d 20 78 44 65 73 74 72 6f  estroy = xDestro
2d9dd 79 3b 0a 20 20 20 20 70 44 65 6c 20 3d 20 28 4d  y;.    pDel = (M
2d9de 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65 33 48  odule *)sqlite3H
2d9df 61 73 68 49 6e 73 65 72 74 28 26 64 62 2d 3e 61  ashInsert(&db->a
2d9e0 4d 6f 64 75 6c 65 2c 20 7a 43 6f 70 79 2c 20 6e  Module, zCopy, n
2d9e1 4e 61 6d 65 2c 20 28 76 6f 69 64 2a 29 70 4d 6f  Name, (void*)pMo
2d9e2 64 29 3b 0a 20 20 20 20 69 66 28 20 70 44 65 6c  d);.    if( pDel
2d9e3 20 26 26 20 70 44 65 6c 2d 3e 78 44 65 73 74 72   && pDel->xDestr
2d9e4 6f 79 20 29 7b 0a 20 20 20 20 20 20 70 44 65 6c  oy ){.      pDel
2d9e5 2d 3e 78 44 65 73 74 72 6f 79 28 70 44 65 6c 2d  ->xDestroy(pDel-
2d9e6 3e 70 41 75 78 29 3b 0a 20 20 20 20 7d 0a 20 20  >pAux);.    }.  
2d9e7 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2d9e8 64 62 2c 20 70 44 65 6c 29 3b 0a 20 20 20 20 69  db, pDel);.    i
2d9e9 66 28 20 70 44 65 6c 3d 3d 70 4d 6f 64 20 29 7b  f( pDel==pMod ){
2d9ea 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
2d9eb 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
2d9ec 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65   }.    sqlite3Re
2d9ed 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d  setInternalSchem
2d9ee 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 65 6c 73  a(db, 0);.  }els
2d9ef 65 20 69 66 28 20 78 44 65 73 74 72 6f 79 20 29  e if( xDestroy )
2d9f0 7b 0a 20 20 20 20 78 44 65 73 74 72 6f 79 28 70  {.    xDestroy(p
2d9f1 41 75 78 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d  Aux);.  }.  rc =
2d9f2 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
2d9f3 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a  db, SQLITE_OK);.
2d9f4 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2d9f5 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
2d9f6 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
2d9f7 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 65 72 6e 61  .../*.** Externa
2d9f8 6c 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 75  l API function u
2d9f9 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20  sed to create a 
2d9fa 6e 65 77 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  new virtual-tabl
2d9fb 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c  e module..*/.SQL
2d9fc 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2d9fd 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
2d9fe 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
2d9ff 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2da00 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
2da01 20 69 6e 20 77 68 69 63 68 20 6d 6f 64 75 6c 65   in which module
2da02 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20 2a   is registered *
2da03 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
2da04 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20  zName,          
2da05 20 20 20 20 2f 2a 20 4e 61 6d 65 20 61 73 73 69      /* Name assi
2da06 67 6e 65 64 20 74 6f 20 74 68 69 73 20 6d 6f 64  gned to this mod
2da07 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73  ule */.  const s
2da08 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
2da09 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20 54 68 65 20  Module,  /* The 
2da0a 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68  definition of th
2da0b 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f  e module */.  vo
2da0c 69 64 20 2a 70 41 75 78 20 20 20 20 20 20 20 20  id *pAux        
2da0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2da0e 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72   Context pointer
2da0f 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f   for xCreate/xCo
2da10 6e 6e 65 63 74 20 2a 2f 0a 29 7b 0a 20 20 72 65  nnect */.){.  re
2da11 74 75 72 6e 20 63 72 65 61 74 65 4d 6f 64 75 6c  turn createModul
2da12 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 70 4d 6f  e(db, zName, pMo
2da13 64 75 6c 65 2c 20 70 41 75 78 2c 20 30 29 3b 0a  dule, pAux, 0);.
2da14 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 65 72 6e 61  }../*.** Externa
2da15 6c 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 75  l API function u
2da16 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20  sed to create a 
2da17 6e 65 77 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  new virtual-tabl
2da18 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c  e module..*/.SQL
2da19 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2da1a 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
2da1b 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20  e_v2(.  sqlite3 
2da1c 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
2da1d 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
2da1e 61 73 65 20 69 6e 20 77 68 69 63 68 20 6d 6f 64  ase in which mod
2da1f 75 6c 65 20 69 73 20 72 65 67 69 73 74 65 72 65  ule is registere
2da20 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  d */.  const cha
2da21 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20  r *zName,       
2da22 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 61         /* Name a
2da23 73 73 69 67 6e 65 64 20 74 6f 20 74 68 69 73 20  ssigned to this 
2da24 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  module */.  cons
2da25 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
2da26 20 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20 54   *pModule,  /* T
2da27 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66  he definition of
2da28 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20   the module */. 
2da29 20 76 6f 69 64 20 2a 70 41 75 78 2c 20 20 20 20   void *pAux,    
2da2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2da2b 20 2f 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e   /* Context poin
2da2c 74 65 72 20 66 6f 72 20 78 43 72 65 61 74 65 2f  ter for xCreate/
2da2d 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 76 6f  xConnect */.  vo
2da2e 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76  id (*xDestroy)(v
2da2f 6f 69 64 20 2a 29 20 20 20 20 20 20 20 20 2f 2a  oid *)        /*
2da30 20 4d 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74   Module destruct
2da31 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29  or function */.)
2da32 7b 0a 20 20 72 65 74 75 72 6e 20 63 72 65 61 74  {.  return creat
2da33 65 4d 6f 64 75 6c 65 28 64 62 2c 20 7a 4e 61 6d  eModule(db, zNam
2da34 65 2c 20 70 4d 6f 64 75 6c 65 2c 20 70 41 75 78  e, pModule, pAux
2da35 2c 20 78 44 65 73 74 72 6f 79 29 3b 0a 7d 0a 0a  , xDestroy);.}..
2da36 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 76  /*.** Lock the v
2da37 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 6f 20  irtual table so 
2da38 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62  that it cannot b
2da39 65 20 64 69 73 63 6f 6e 6e 65 63 74 65 64 2e 0a  e disconnected..
2da3a 2a 2a 20 4c 6f 63 6b 73 20 6e 65 73 74 2e 20 20  ** Locks nest.  
2da3b 45 76 65 72 79 20 6c 6f 63 6b 20 73 68 6f 75 6c  Every lock shoul
2da3c 64 20 68 61 76 65 20 61 20 63 6f 72 72 65 73 70  d have a corresp
2da3d 6f 6e 64 69 6e 67 20 75 6e 6c 6f 63 6b 2e 0a 2a  onding unlock..*
2da3e 2a 20 49 66 20 61 6e 20 75 6e 6c 6f 63 6b 20 69  * If an unlock i
2da3f 73 20 6f 6d 69 74 74 65 64 2c 20 72 65 73 6f 75  s omitted, resou
2da40 72 63 65 73 20 6c 65 61 6b 73 20 77 69 6c 6c 20  rces leaks will 
2da41 6f 63 63 75 72 2e 20 20 0a 2a 2a 0a 2a 2a 20 49  occur.  .**.** I
2da42 66 20 61 20 64 69 73 63 6f 6e 6e 65 63 74 20 69  f a disconnect i
2da43 73 20 61 74 74 65 6d 70 74 65 64 20 77 68 69 6c  s attempted whil
2da44 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  e a virtual tabl
2da45 65 20 69 73 20 6c 6f 63 6b 65 64 2c 0a 2a 2a 20  e is locked,.** 
2da46 74 68 65 20 64 69 73 63 6f 6e 6e 65 63 74 20 69  the disconnect i
2da47 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c  s deferred until
2da48 20 61 6c 6c 20 6c 6f 63 6b 73 20 68 61 76 65 20   all locks have 
2da49 62 65 65 6e 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f  been removed..*/
2da4a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2da4b 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
2da4c 4c 6f 63 6b 28 56 54 61 62 6c 65 20 2a 70 56 54  Lock(VTable *pVT
2da4d 61 62 29 7b 0a 20 20 70 56 54 61 62 2d 3e 6e 52  ab){.  pVTab->nR
2da4e 65 66 2b 2b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  ef++;.}.../*.** 
2da4f 70 54 61 62 20 69 73 20 61 20 70 6f 69 6e 74 65  pTab is a pointe
2da50 72 20 74 6f 20 61 20 54 61 62 6c 65 20 73 74 72  r to a Table str
2da51 75 63 74 75 72 65 20 72 65 70 72 65 73 65 6e 74  ucture represent
2da52 69 6e 67 20 61 20 76 69 72 74 75 61 6c 2d 74 61  ing a virtual-ta
2da53 62 6c 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ble..** Return a
2da54 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
2da55 56 54 61 62 6c 65 20 6f 62 6a 65 63 74 20 75 73  VTable object us
2da56 65 64 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e  ed by connection
2da57 20 64 62 20 74 6f 20 61 63 63 65 73 73 20 0a 2a   db to access .*
2da58 2a 20 74 68 69 73 20 76 69 72 74 75 61 6c 2d 74  * this virtual-t
2da59 61 62 6c 65 2c 20 69 66 20 6f 6e 65 20 68 61 73  able, if one has
2da5a 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c 20 6f   been created, o
2da5b 72 20 4e 55 4c 4c 20 6f 74 68 65 72 77 69 73 65  r NULL otherwise
2da5c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2da5d 41 54 45 20 56 54 61 62 6c 65 20 2a 73 71 6c 69  ATE VTable *sqli
2da5e 74 65 33 47 65 74 56 54 61 62 6c 65 28 73 71 6c  te3GetVTable(sql
2da5f 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c 65 20  ite3 *db, Table 
2da60 2a 70 54 61 62 29 7b 0a 20 20 56 54 61 62 6c 65  *pTab){.  VTable
2da61 20 2a 70 56 74 61 62 3b 0a 20 20 61 73 73 65 72   *pVtab;.  asser
2da62 74 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  t( IsVirtual(pTa
2da63 62 29 20 29 3b 0a 20 20 66 6f 72 28 70 56 74 61  b) );.  for(pVta
2da64 62 3d 70 54 61 62 2d 3e 70 56 54 61 62 6c 65 3b  b=pTab->pVTable;
2da65 20 70 56 74 61 62 20 26 26 20 70 56 74 61 62 2d   pVtab && pVtab-
2da66 3e 64 62 21 3d 64 62 3b 20 70 56 74 61 62 3d 70  >db!=db; pVtab=p
2da67 56 74 61 62 2d 3e 70 4e 65 78 74 29 3b 0a 20 20  Vtab->pNext);.  
2da68 72 65 74 75 72 6e 20 70 56 74 61 62 3b 0a 7d 0a  return pVtab;.}.
2da69 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74  ./*.** Decrement
2da6a 20 74 68 65 20 72 65 66 2d 63 6f 75 6e 74 20 6f   the ref-count o
2da6b 6e 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  n a virtual tabl
2da6c 65 20 6f 62 6a 65 63 74 2e 20 57 68 65 6e 20 74  e object. When t
2da6d 68 65 20 72 65 66 2d 63 6f 75 6e 74 0a 2a 2a 20  he ref-count.** 
2da6e 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 63 61  reaches zero, ca
2da6f 6c 6c 20 74 68 65 20 78 44 69 73 63 6f 6e 6e 65  ll the xDisconne
2da70 63 74 28 29 20 6d 65 74 68 6f 64 20 74 6f 20 64  ct() method to d
2da71 65 6c 65 74 65 20 74 68 65 20 6f 62 6a 65 63 74  elete the object
2da72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2da73 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2da74 56 74 61 62 55 6e 6c 6f 63 6b 28 56 54 61 62 6c  VtabUnlock(VTabl
2da75 65 20 2a 70 56 54 61 62 29 7b 0a 20 20 73 71 6c  e *pVTab){.  sql
2da76 69 74 65 33 20 2a 64 62 20 3d 20 70 56 54 61 62  ite3 *db = pVTab
2da77 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 28  ->db;..  assert(
2da78 20 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28   db );.  assert(
2da79 20 70 56 54 61 62 2d 3e 6e 52 65 66 3e 30 20 29   pVTab->nRef>0 )
2da7a 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
2da7b 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f 6b  te3SafetyCheckOk
2da7c 28 64 62 29 20 29 3b 0a 0a 20 20 70 56 54 61 62  (db) );..  pVTab
2da7d 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20  ->nRef--;.  if( 
2da7e 70 56 54 61 62 2d 3e 6e 52 65 66 3d 3d 30 20 29  pVTab->nRef==0 )
2da7f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74  {.    sqlite3_vt
2da80 61 62 20 2a 70 20 3d 20 70 56 54 61 62 2d 3e 70  ab *p = pVTab->p
2da81 56 74 61 62 3b 0a 20 20 20 20 69 66 28 20 70 20  Vtab;.    if( p 
2da82 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ){.#ifdef SQLITE
2da83 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 66 28  _DEBUG.      if(
2da84 20 70 56 54 61 62 2d 3e 64 62 2d 3e 6d 61 67 69   pVTab->db->magi
2da85 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  c==SQLITE_MAGIC_
2da86 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20  BUSY ){.        
2da87 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
2da88 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20  etyOff(db);.    
2da89 20 20 20 20 70 2d 3e 70 4d 6f 64 75 6c 65 2d 3e      p->pModule->
2da8a 78 44 69 73 63 6f 6e 6e 65 63 74 28 70 29 3b 0a  xDisconnect(p);.
2da8b 20 20 20 20 20 20 20 20 28 76 6f 69 64 29 73 71          (void)sq
2da8c 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
2da8d 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 0a  );.      } else.
2da8e 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20  #endif.      {. 
2da8f 20 20 20 20 20 20 20 70 2d 3e 70 4d 6f 64 75 6c         p->pModul
2da90 65 2d 3e 78 44 69 73 63 6f 6e 6e 65 63 74 28 70  e->xDisconnect(p
2da91 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
2da92 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2da93 65 65 28 64 62 2c 20 70 56 54 61 62 29 3b 0a 20  ee(db, pVTab);. 
2da94 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c   }.}../*.** Tabl
2da95 65 20 70 20 69 73 20 61 20 76 69 72 74 75 61 6c  e p is a virtual
2da96 20 74 61 62 6c 65 2e 20 54 68 69 73 20 66 75 6e   table. This fun
2da97 63 74 69 6f 6e 20 6d 6f 76 65 73 20 61 6c 6c 20  ction moves all 
2da98 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 0a  elements in the.
2da99 2a 2a 20 70 2d 3e 70 56 54 61 62 6c 65 20 6c 69  ** p->pVTable li
2da9a 73 74 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65  st to the sqlite
2da9b 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20 6c 69  3.pDisconnect li
2da9c 73 74 73 20 6f 66 20 74 68 65 69 72 20 61 73 73  sts of their ass
2da9d 6f 63 69 61 74 65 64 0a 2a 2a 20 64 61 74 61 62  ociated.** datab
2da9e 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ase connections 
2da9f 74 6f 20 62 65 20 64 69 73 63 6f 6e 6e 65 63 74  to be disconnect
2daa0 65 64 20 61 74 20 74 68 65 20 6e 65 78 74 20 6f  ed at the next o
2daa1 70 70 6f 72 74 75 6e 69 74 79 2e 20 0a 2a 2a 20  pportunity. .** 
2daa2 45 78 63 65 70 74 2c 20 69 66 20 61 72 67 75 6d  Except, if argum
2daa3 65 6e 74 20 64 62 20 69 73 20 6e 6f 74 20 4e 55  ent db is not NU
2daa4 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 65 6e 74  LL, then the ent
2daa5 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ry associated wi
2daa6 74 68 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  th.** connection
2daa7 20 64 62 20 69 73 20 6c 65 66 74 20 69 6e 20 74   db is left in t
2daa8 68 65 20 70 2d 3e 70 56 54 61 62 6c 65 20 6c 69  he p->pVTable li
2daa9 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 56 54  st..*/.static VT
2daaa 61 62 6c 65 20 2a 76 74 61 62 44 69 73 63 6f 6e  able *vtabDiscon
2daab 6e 65 63 74 41 6c 6c 28 73 71 6c 69 74 65 33 20  nectAll(sqlite3 
2daac 2a 64 62 2c 20 54 61 62 6c 65 20 2a 70 29 7b 0a  *db, Table *p){.
2daad 20 20 56 54 61 62 6c 65 20 2a 70 52 65 74 20 3d    VTable *pRet =
2daae 20 30 3b 0a 20 20 56 54 61 62 6c 65 20 2a 70 56   0;.  VTable *pV
2daaf 54 61 62 6c 65 20 3d 20 70 2d 3e 70 56 54 61 62  Table = p->pVTab
2dab0 6c 65 3b 0a 20 20 70 2d 3e 70 56 54 61 62 6c 65  le;.  p->pVTable
2dab1 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 41 73 73 65   = 0;..  /* Asse
2dab2 72 74 20 74 68 61 74 20 74 68 65 20 6d 75 74 65  rt that the mute
2dab3 78 20 28 69 66 20 61 6e 79 29 20 61 73 73 6f 63  x (if any) assoc
2dab4 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 42  iated with the B
2dab5 74 53 68 61 72 65 64 20 64 61 74 61 62 61 73 65  tShared database
2dab6 20 0a 20 20 2a 2a 20 74 68 61 74 20 63 6f 6e 74   .  ** that cont
2dab7 61 69 6e 73 20 74 61 62 6c 65 20 70 20 69 73 20  ains table p is 
2dab8 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c  held by the call
2dab9 65 72 2e 20 53 65 65 20 68 65 61 64 65 72 20 63  er. See header c
2daba 6f 6d 6d 65 6e 74 73 20 0a 20 20 2a 2a 20 61 62  omments .  ** ab
2dabb 6f 76 65 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c  ove function sql
2dabc 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69  ite3VtabUnlockLi
2dabd 73 74 28 29 20 66 6f 72 20 61 6e 20 65 78 70 6c  st() for an expl
2dabe 61 6e 61 74 69 6f 6e 20 6f 66 20 77 68 79 0a 20  anation of why. 
2dabf 20 2a 2a 20 74 68 69 73 20 6d 61 6b 65 73 20 69   ** this makes i
2dac0 74 20 73 61 66 65 20 74 6f 20 61 63 63 65 73 73  t safe to access
2dac1 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69   the sqlite3.pDi
2dac2 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74 20 6f 66  sconnect list of
2dac3 20 61 6e 79 0a 20 20 2a 2a 20 64 61 74 61 62 61   any.  ** databa
2dac4 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68  se connection th
2dac5 61 74 20 6d 61 79 20 68 61 76 65 20 61 6e 20 65  at may have an e
2dac6 6e 74 72 79 20 69 6e 20 74 68 65 20 70 2d 3e 70  ntry in the p->p
2dac7 56 54 61 62 6c 65 20 6c 69 73 74 2e 20 20 2a 2f  VTable list.  */
2dac8 0a 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d 30  .  assert( db==0
2dac9 20 7c 7c 0a 20 20 20 20 73 71 6c 69 74 65 33 42   ||.    sqlite3B
2daca 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 64  treeHoldsMutex(d
2dacb 62 2d 3e 61 44 62 5b 73 71 6c 69 74 65 33 53 63  b->aDb[sqlite3Sc
2dacc 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
2dacd 70 2d 3e 70 53 63 68 65 6d 61 29 5d 2e 70 42 74  p->pSchema)].pBt
2dace 29 20 0a 20 20 29 3b 0a 0a 20 20 77 68 69 6c 65  ) .  );..  while
2dacf 28 20 70 56 54 61 62 6c 65 20 29 7b 0a 20 20 20  ( pVTable ){.   
2dad0 20 73 71 6c 69 74 65 33 20 2a 64 62 32 20 3d 20   sqlite3 *db2 = 
2dad1 70 56 54 61 62 6c 65 2d 3e 64 62 3b 0a 20 20 20  pVTable->db;.   
2dad2 20 56 54 61 62 6c 65 20 2a 70 4e 65 78 74 20 3d   VTable *pNext =
2dad3 20 70 56 54 61 62 6c 65 2d 3e 70 4e 65 78 74 3b   pVTable->pNext;
2dad4 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 32  .    assert( db2
2dad5 20 29 3b 0a 20 20 20 20 69 66 28 20 64 62 32 3d   );.    if( db2=
2dad6 3d 64 62 20 29 7b 0a 20 20 20 20 20 20 70 52 65  =db ){.      pRe
2dad7 74 20 3d 20 70 56 54 61 62 6c 65 3b 0a 20 20 20  t = pVTable;.   
2dad8 20 20 20 70 2d 3e 70 56 54 61 62 6c 65 20 3d 20     p->pVTable = 
2dad9 70 52 65 74 3b 0a 20 20 20 20 20 20 70 52 65 74  pRet;.      pRet
2dada 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20  ->pNext = 0;.   
2dadb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 56   }else{.      pV
2dadc 54 61 62 6c 65 2d 3e 70 4e 65 78 74 20 3d 20 64  Table->pNext = d
2dadd 62 32 2d 3e 70 44 69 73 63 6f 6e 6e 65 63 74 3b  b2->pDisconnect;
2dade 0a 20 20 20 20 20 20 64 62 32 2d 3e 70 44 69 73  .      db2->pDis
2dadf 63 6f 6e 6e 65 63 74 20 3d 20 70 56 54 61 62 6c  connect = pVTabl
2dae0 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 56 54  e;.    }.    pVT
2dae1 61 62 6c 65 20 3d 20 70 4e 65 78 74 3b 0a 20 20  able = pNext;.  
2dae2 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 21 64 62  }..  assert( !db
2dae3 20 7c 7c 20 70 52 65 74 20 29 3b 0a 20 20 72 65   || pRet );.  re
2dae4 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 0a 2f  turn pRet;.}.../
2dae5 2a 0a 2a 2a 20 44 69 73 63 6f 6e 6e 65 63 74 20  *.** Disconnect 
2dae6 61 6c 6c 20 74 68 65 20 76 69 72 74 75 61 6c 20  all the virtual 
2dae7 74 61 62 6c 65 20 6f 62 6a 65 63 74 73 20 69 6e  table objects in
2dae8 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69   the sqlite3.pDi
2dae9 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74 2e 0a 2a  sconnect list..*
2daea 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
2daeb 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 63  on may only be c
2daec 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20 6d  alled when the m
2daed 75 74 65 78 65 73 20 61 73 73 6f 63 69 61 74 65  utexes associate
2daee 64 20 77 69 74 68 20 61 6c 6c 0a 2a 2a 20 73 68  d with all.** sh
2daef 61 72 65 64 20 62 2d 74 72 65 65 20 64 61 74 61  ared b-tree data
2daf0 62 61 73 65 73 20 6f 70 65 6e 65 64 20 75 73 69  bases opened usi
2daf1 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62  ng connection db
2daf2 20 61 72 65 20 68 65 6c 64 20 62 79 20 74 68 65   are held by the
2daf3 20 0a 2a 2a 20 63 61 6c 6c 65 72 2e 20 54 68 69   .** caller. Thi
2daf4 73 20 69 73 20 64 6f 6e 65 20 74 6f 20 70 72 6f  s is done to pro
2daf5 74 65 63 74 20 74 68 65 20 73 71 6c 69 74 65 33  tect the sqlite3
2daf6 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20 6c 69 73  .pDisconnect lis
2daf7 74 2e 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65  t. The.** sqlite
2daf8 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20 6c 69  3.pDisconnect li
2daf9 73 74 20 69 73 20 61 63 63 65 73 73 65 64 20 6f  st is accessed o
2dafa 6e 6c 79 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  nly as follows:.
2dafb 2a 2a 0a 2a 2a 20 20 20 31 29 20 42 79 20 74 68  **.**   1) By th
2dafc 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 49 6e 20  is function. In 
2dafd 74 68 69 73 20 63 61 73 65 2c 20 61 6c 6c 20 42  this case, all B
2dafe 74 53 68 61 72 65 64 20 6d 75 74 65 78 65 73 20  tShared mutexes 
2daff 61 6e 64 20 74 68 65 20 6d 75 74 65 78 0a 2a 2a  and the mutex.**
2db00 20 20 20 20 20 20 61 73 73 6f 63 69 61 74 65 64        associated
2db01 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61   with the databa
2db02 73 65 20 68 61 6e 64 6c 65 20 69 74 73 65 6c 66  se handle itself
2db03 20 6d 75 73 74 20 62 65 20 68 65 6c 64 2e 0a 2a   must be held..*
2db04 2a 0a 2a 2a 20 20 20 32 29 20 42 79 20 66 75 6e  *.**   2) By fun
2db05 63 74 69 6f 6e 20 76 74 61 62 44 69 73 63 6f 6e  ction vtabDiscon
2db06 6e 65 63 74 41 6c 6c 28 29 2c 20 77 68 65 6e 20  nectAll(), when 
2db07 69 74 20 61 64 64 73 20 61 20 56 54 61 62 6c 65  it adds a VTable
2db08 20 65 6e 74 72 79 20 74 6f 0a 2a 2a 20 20 20 20   entry to.**    
2db09 20 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44    the sqlite3.pD
2db0a 69 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74 2e 20  isconnect list. 
2db0b 49 6e 20 74 68 69 73 20 63 61 73 65 20 65 69 74  In this case eit
2db0c 68 65 72 20 74 68 65 20 42 74 53 68 61 72 65 64  her the BtShared
2db0d 20 6d 75 74 65 78 0a 2a 2a 20 20 20 20 20 20 61   mutex.**      a
2db0e 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
2db0f 68 65 20 64 61 74 61 62 61 73 65 20 74 68 65 20  he database the 
2db10 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73  virtual table is
2db11 20 73 74 6f 72 65 64 20 69 6e 20 69 73 20 68 65   stored in is he
2db12 6c 64 0a 2a 2a 20 20 20 20 20 20 6f 72 2c 20 69  ld.**      or, i
2db13 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  f the virtual ta
2db14 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ble is stored in
2db15 20 61 20 6e 6f 6e 2d 73 68 61 72 61 62 6c 65 20   a non-sharable 
2db16 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0a 2a  database, then.*
2db17 2a 20 20 20 20 20 20 74 68 65 20 64 61 74 61 62  *      the datab
2db18 61 73 65 20 68 61 6e 64 6c 65 20 6d 75 74 65 78  ase handle mutex
2db19 20 69 73 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20   is held..**.** 
2db1a 41 73 20 61 20 72 65 73 75 6c 74 2c 20 61 20 73  As a result, a s
2db1b 71 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65  qlite3.pDisconne
2db1c 63 74 20 63 61 6e 6e 6f 74 20 62 65 20 61 63 63  ct cannot be acc
2db1d 65 73 73 65 64 20 73 69 6d 75 6c 74 61 6e 65 6f  essed simultaneo
2db1e 75 73 6c 79 20 0a 2a 2a 20 62 79 20 6d 75 6c 74  usly .** by mult
2db1f 69 70 6c 65 20 74 68 72 65 61 64 73 2e 20 49 74  iple threads. It
2db20 20 69 73 20 74 68 72 65 61 64 2d 73 61 66 65 2e   is thread-safe.
2db21 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2db22 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
2db23 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 73 71  tabUnlockList(sq
2db24 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 54  lite3 *db){.  VT
2db25 61 62 6c 65 20 2a 70 20 3d 20 64 62 2d 3e 70 44  able *p = db->pD
2db26 69 73 63 6f 6e 6e 65 63 74 3b 0a 20 20 64 62 2d  isconnect;.  db-
2db27 3e 70 44 69 73 63 6f 6e 6e 65 63 74 20 3d 20 30  >pDisconnect = 0
2db28 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
2db29 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c  ite3BtreeHoldsAl
2db2a 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b 0a  lMutexes(db) );.
2db2b 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
2db2c 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
2db2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 69 66  >mutex) );..  if
2db2e 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ( p ){.    sqlit
2db2f 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64  e3ExpirePrepared
2db30 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a  Statements(db);.
2db31 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 56      do {.      V
2db32 54 61 62 6c 65 20 2a 70 4e 65 78 74 20 3d 20 70  Table *pNext = p
2db33 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 73  ->pNext;.      s
2db34 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b  qlite3VtabUnlock
2db35 28 70 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 70  (p);.      p = p
2db36 4e 65 78 74 3b 0a 20 20 20 20 7d 77 68 69 6c 65  Next;.    }while
2db37 28 20 70 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ( p );.  }.}../*
2db38 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 79 20 61 6e  .** Clear any an
2db39 64 20 61 6c 6c 20 76 69 72 74 75 61 6c 2d 74 61  d all virtual-ta
2db3a 62 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ble information 
2db3b 66 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 20 72  from the Table r
2db3c 65 63 6f 72 64 2e 0a 2a 2a 20 54 68 69 73 20 72  ecord..** This r
2db3d 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
2db3e 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 6a  , for example, j
2db3f 75 73 74 20 62 65 66 6f 72 65 20 64 65 6c 65 74  ust before delet
2db40 69 6e 67 20 74 68 65 20 54 61 62 6c 65 0a 2a 2a  ing the Table.**
2db41 20 72 65 63 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 53   record..**.** S
2db42 69 6e 63 65 20 69 74 20 69 73 20 61 20 76 69 72  ince it is a vir
2db43 74 75 61 6c 2d 74 61 62 6c 65 2c 20 74 68 65 20  tual-table, the 
2db44 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  Table structure 
2db45 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74  contains a point
2db46 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 68 65 61  er.** to the hea
2db47 64 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69  d of a linked li
2db48 73 74 20 6f 66 20 56 54 61 62 6c 65 20 73 74 72  st of VTable str
2db49 75 63 74 75 72 65 73 2e 20 45 61 63 68 20 56 54  uctures. Each VT
2db4a 61 62 6c 65 20 0a 2a 2a 20 73 74 72 75 63 74 75  able .** structu
2db4b 72 65 20 69 73 20 61 73 73 6f 63 69 61 74 65 64  re is associated
2db4c 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65 20 73   with a single s
2db4d 71 6c 69 74 65 33 2a 20 75 73 65 72 20 6f 66 20  qlite3* user of 
2db4e 74 68 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 20 54  the schema..** T
2db4f 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
2db50 6e 74 20 6f 66 20 74 68 65 20 56 54 61 62 6c 65  nt of the VTable
2db51 20 73 74 72 75 63 74 75 72 65 20 61 73 73 6f 63   structure assoc
2db52 69 61 74 65 64 20 77 69 74 68 20 64 61 74 61 62  iated with datab
2db53 61 73 65 20 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69  ase .** connecti
2db54 6f 6e 20 64 62 20 69 73 20 64 65 63 72 65 6d 65  on db is decreme
2db55 6e 74 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79  nted immediately
2db56 20 28 77 68 69 63 68 20 6d 61 79 20 6c 65 61 64   (which may lead
2db57 20 74 6f 20 74 68 65 20 0a 2a 2a 20 73 74 72 75   to the .** stru
2db58 63 74 75 72 65 20 62 65 69 6e 67 20 78 44 69 73  cture being xDis
2db59 63 6f 6e 6e 65 63 74 65 64 20 61 6e 64 20 66 72  connected and fr
2db5a 65 65 29 2e 20 41 6e 79 20 6f 74 68 65 72 20 56  ee). Any other V
2db5b 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 73  Table structures
2db5c 0a 2a 2a 20 69 6e 20 74 68 65 20 6c 69 73 74 20  .** in the list 
2db5d 61 72 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65  are moved to the
2db5e 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e   sqlite3.pDiscon
2db5f 6e 65 63 74 20 6c 69 73 74 20 6f 66 20 74 68 65  nect list of the
2db60 20 61 73 73 6f 63 69 61 74 65 64 20 0a 2a 2a 20   associated .** 
2db61 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2db62 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
2db63 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2db64 74 65 33 56 74 61 62 43 6c 65 61 72 28 54 61 62  te3VtabClear(Tab
2db65 6c 65 20 2a 70 29 7b 0a 20 20 76 74 61 62 44 69  le *p){.  vtabDi
2db66 73 63 6f 6e 6e 65 63 74 41 6c 6c 28 30 2c 20 70  sconnectAll(0, p
2db67 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 7a 4d 6f  );.  if( p->azMo
2db68 64 75 6c 65 41 72 67 20 29 7b 0a 20 20 20 20 69  duleArg ){.    i
2db69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
2db6a 30 3b 20 69 3c 70 2d 3e 6e 4d 6f 64 75 6c 65 41  0; i<p->nModuleA
2db6b 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  rg; i++){.      
2db6c 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d  sqlite3DbFree(p-
2db6d 3e 64 62 4d 65 6d 2c 20 70 2d 3e 61 7a 4d 6f 64  >dbMem, p->azMod
2db6e 75 6c 65 41 72 67 5b 69 5d 29 3b 0a 20 20 20 20  uleArg[i]);.    
2db6f 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  }.    sqlite3DbF
2db70 72 65 65 28 70 2d 3e 64 62 4d 65 6d 2c 20 70 2d  ree(p->dbMem, p-
2db71 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 29 3b 0a 20  >azModuleArg);. 
2db72 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20   }.}../*.** Add 
2db73 61 20 6e 65 77 20 6d 6f 64 75 6c 65 20 61 72 67  a new module arg
2db74 75 6d 65 6e 74 20 74 6f 20 70 54 61 62 6c 65 2d  ument to pTable-
2db75 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 5d 2e 0a  >azModuleArg[]..
2db76 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 69 73  ** The string is
2db77 20 6e 6f 74 20 63 6f 70 69 65 64 20 2d 20 74 68   not copied - th
2db78 65 20 70 6f 69 6e 74 65 72 20 69 73 20 73 74 6f  e pointer is sto
2db79 72 65 64 2e 20 20 54 68 65 0a 2a 2a 20 73 74 72  red.  The.** str
2db7a 69 6e 67 20 77 69 6c 6c 20 62 65 20 66 72 65 65  ing will be free
2db7b 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  d automatically 
2db7c 77 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 69  when the table i
2db7d 73 0a 2a 2a 20 64 65 6c 65 74 65 64 2e 0a 2a 2f  s.** deleted..*/
2db7e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 64 64  .static void add
2db7f 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 73  ModuleArgument(s
2db80 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c  qlite3 *db, Tabl
2db81 65 20 2a 70 54 61 62 6c 65 2c 20 63 68 61 72 20  e *pTable, char 
2db82 2a 7a 41 72 67 29 7b 0a 20 20 69 6e 74 20 69 20  *zArg){.  int i 
2db83 3d 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c  = pTable->nModul
2db84 65 41 72 67 2b 2b 3b 0a 20 20 69 6e 74 20 6e 42  eArg++;.  int nB
2db85 79 74 65 73 20 3d 20 73 69 7a 65 6f 66 28 63 68  ytes = sizeof(ch
2db86 61 72 20 2a 29 2a 28 31 2b 70 54 61 62 6c 65 2d  ar *)*(1+pTable-
2db87 3e 6e 4d 6f 64 75 6c 65 41 72 67 29 3b 0a 20 20  >nModuleArg);.  
2db88 63 68 61 72 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41  char **azModuleA
2db89 72 67 3b 0a 20 20 61 7a 4d 6f 64 75 6c 65 41 72  rg;.  azModuleAr
2db8a 67 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  g = sqlite3DbRea
2db8b 6c 6c 6f 63 28 64 62 2c 20 70 54 61 62 6c 65 2d  lloc(db, pTable-
2db8c 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 2c 20 6e 42  >azModuleArg, nB
2db8d 79 74 65 73 29 3b 0a 20 20 69 66 28 20 61 7a 4d  ytes);.  if( azM
2db8e 6f 64 75 6c 65 41 72 67 3d 3d 30 20 29 7b 0a 20  oduleArg==0 ){. 
2db8f 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 66 6f     int j;.    fo
2db90 72 28 6a 3d 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29  r(j=0; j<i; j++)
2db91 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
2db92 62 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65  bFree(db, pTable
2db93 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 6a 5d  ->azModuleArg[j]
2db94 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
2db95 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2db96 41 72 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Arg);.    sqlite
2db97 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 61 62  3DbFree(db, pTab
2db98 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 29  le->azModuleArg)
2db99 3b 0a 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e 4d  ;.    pTable->nM
2db9a 6f 64 75 6c 65 41 72 67 20 3d 20 30 3b 0a 20 20  oduleArg = 0;.  
2db9b 7d 65 6c 73 65 7b 0a 20 20 20 20 61 7a 4d 6f 64  }else{.    azMod
2db9c 75 6c 65 41 72 67 5b 69 5d 20 3d 20 7a 41 72 67  uleArg[i] = zArg
2db9d 3b 0a 20 20 20 20 61 7a 4d 6f 64 75 6c 65 41 72  ;.    azModuleAr
2db9e 67 5b 69 2b 31 5d 20 3d 20 30 3b 0a 20 20 7d 0a  g[i+1] = 0;.  }.
2db9f 20 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75    pTable->azModu
2dba0 6c 65 41 72 67 20 3d 20 61 7a 4d 6f 64 75 6c 65  leArg = azModule
2dba1 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  Arg;.}../*.** Th
2dba2 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74  e parser calls t
2dba3 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e  his routine when
2dba4 20 69 74 20 66 69 72 73 74 20 73 65 65 73 20 61   it first sees a
2dba5 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20   CREATE VIRTUAL 
2dba6 54 41 42 4c 45 0a 2a 2a 20 73 74 61 74 65 6d 65  TABLE.** stateme
2dba7 6e 74 2e 20 20 54 68 65 20 6d 6f 64 75 6c 65 20  nt.  The module 
2dba8 6e 61 6d 65 20 68 61 73 20 62 65 65 6e 20 70 61  name has been pa
2dba9 72 73 65 64 2c 20 62 75 74 20 74 68 65 20 6f 70  rsed, but the op
2dbaa 74 69 6f 6e 61 6c 20 6c 69 73 74 0a 2a 2a 20 6f  tional list.** o
2dbab 66 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61  f parameters tha
2dbac 74 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6d 6f 64  t follow the mod
2dbad 75 6c 65 20 6e 61 6d 65 20 61 72 65 20 73 74 69  ule name are sti
2dbae 6c 6c 20 70 65 6e 64 69 6e 67 2e 0a 2a 2f 0a 53  ll pending..*/.S
2dbaf 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2dbb0 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 42 65  id sqlite3VtabBe
2dbb1 67 69 6e 50 61 72 73 65 28 0a 20 20 50 61 72 73  ginParse(.  Pars
2dbb2 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2dbb3 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
2dbb4 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  text */.  Token 
2dbb5 2a 70 4e 61 6d 65 31 2c 20 20 20 20 20 20 20 20  *pName1,        
2dbb6 2f 2a 20 4e 61 6d 65 20 6f 66 20 6e 65 77 20 74  /* Name of new t
2dbb7 61 62 6c 65 2c 20 6f 72 20 64 61 74 61 62 61 73  able, or databas
2dbb8 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65  e name */.  Toke
2dbb9 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20  n *pName2,      
2dbba 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 6e 65 77    /* Name of new
2dbbb 20 74 61 62 6c 65 20 6f 72 20 4e 55 4c 4c 20 2a   table or NULL *
2dbbc 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4d 6f 64 75  /.  Token *pModu
2dbbd 6c 65 4e 61 6d 65 20 20 20 20 2f 2a 20 4e 61 6d  leName    /* Nam
2dbbe 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20  e of the module 
2dbbf 66 6f 72 20 74 68 65 20 76 69 72 74 75 61 6c 20  for the virtual 
2dbc0 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e  table */.){.  in
2dbc1 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
2dbc2 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
2dbc3 61 73 65 20 74 68 65 20 74 61 62 6c 65 20 69 73  ase the table is
2dbc4 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 20 69   being created i
2dbc5 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  n */.  Table *pT
2dbc6 61 62 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a 20  able;        /* 
2dbc7 54 68 65 20 6e 65 77 20 76 69 72 74 75 61 6c 20  The new virtual 
2dbc8 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74  table */.  sqlit
2dbc9 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20  e3 *db;         
2dbca 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
2dbcb 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 73 71  nection */..  sq
2dbcc 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28  lite3StartTable(
2dbcd 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20  pParse, pName1, 
2dbce 70 4e 61 6d 65 32 2c 20 30 2c 20 30 2c 20 31 2c  pName2, 0, 0, 1,
2dbcf 20 30 29 3b 0a 20 20 70 54 61 62 6c 65 20 3d 20   0);.  pTable = 
2dbd0 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
2dbd1 65 3b 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d  e;.  if( pTable=
2dbd2 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61  =0 ) return;.  a
2dbd3 73 73 65 72 74 28 20 30 3d 3d 70 54 61 62 6c 65  ssert( 0==pTable
2dbd4 2d 3e 70 49 6e 64 65 78 20 29 3b 0a 0a 20 20 64  ->pIndex );..  d
2dbd5 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2dbd6 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
2dbd7 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
2dbd8 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61   pTable->pSchema
2dbd9 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  );.  assert( iDb
2dbda 3e 3d 30 20 29 3b 0a 0a 20 20 70 54 61 62 6c 65  >=0 );..  pTable
2dbdb 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46  ->tabFlags |= TF
2dbdc 5f 56 69 72 74 75 61 6c 3b 0a 20 20 70 54 61 62  _Virtual;.  pTab
2dbdd 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 20 3d  le->nModuleArg =
2dbde 20 30 3b 0a 20 20 61 64 64 4d 6f 64 75 6c 65 41   0;.  addModuleA
2dbdf 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 54 61 62  rgument(db, pTab
2dbe0 6c 65 2c 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  le, sqlite3NameF
2dbe1 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4d 6f  romToken(db, pMo
2dbe2 64 75 6c 65 4e 61 6d 65 29 29 3b 0a 20 20 61 64  duleName));.  ad
2dbe3 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74 28  dModuleArgument(
2dbe4 64 62 2c 20 70 54 61 62 6c 65 2c 20 73 71 6c 69  db, pTable, sqli
2dbe5 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
2dbe6 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
2dbe7 6d 65 29 29 3b 0a 20 20 61 64 64 4d 6f 64 75 6c  me));.  addModul
2dbe8 65 41 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 54  eArgument(db, pT
2dbe9 61 62 6c 65 2c 20 73 71 6c 69 74 65 33 44 62 53  able, sqlite3DbS
2dbea 74 72 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65  trDup(db, pTable
2dbeb 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 70 50 61  ->zName));.  pPa
2dbec 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e  rse->sNameToken.
2dbed 6e 20 3d 20 28 69 6e 74 29 28 26 70 4d 6f 64 75  n = (int)(&pModu
2dbee 6c 65 4e 61 6d 65 2d 3e 7a 5b 70 4d 6f 64 75 6c  leName->z[pModul
2dbef 65 4e 61 6d 65 2d 3e 6e 5d 20 2d 20 70 4e 61 6d  eName->n] - pNam
2dbf0 65 31 2d 3e 7a 29 3b 0a 0a 23 69 66 6e 64 65 66  e1->z);..#ifndef
2dbf1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
2dbf2 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20  HORIZATION.  /* 
2dbf3 43 72 65 61 74 69 6e 67 20 61 20 76 69 72 74 75  Creating a virtu
2dbf4 61 6c 20 74 61 62 6c 65 20 69 6e 76 6f 6b 65 73  al table invokes
2dbf5 20 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69   the authorizati
2dbf6 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 74 77 69 63  on callback twic
2dbf7 65 2e 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73  e..  ** The firs
2dbf8 74 20 69 6e 76 6f 63 61 74 69 6f 6e 2c 20 74 6f  t invocation, to
2dbf9 20 6f 62 74 61 69 6e 20 70 65 72 6d 69 73 73 69   obtain permissi
2dbfa 6f 6e 20 74 6f 20 49 4e 53 45 52 54 20 61 20 72  on to INSERT a r
2dbfb 6f 77 20 69 6e 74 6f 20 74 68 65 0a 20 20 2a 2a  ow into the.  **
2dbfc 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
2dbfd 61 62 6c 65 2c 20 68 61 73 20 61 6c 72 65 61 64  able, has alread
2dbfe 79 20 62 65 65 6e 20 6d 61 64 65 20 62 79 20 73  y been made by s
2dbff 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65  qlite3StartTable
2dc00 28 29 2e 0a 20 20 2a 2a 20 54 68 65 20 73 65 63  ()..  ** The sec
2dc01 6f 6e 64 20 63 61 6c 6c 2c 20 74 6f 20 6f 62 74  ond call, to obt
2dc02 61 69 6e 20 70 65 72 6d 69 73 73 69 6f 6e 20 74  ain permission t
2dc03 6f 20 63 72 65 61 74 65 20 74 68 65 20 74 61 62  o create the tab
2dc04 6c 65 2c 20 69 73 20 6d 61 64 65 20 6e 6f 77 2e  le, is made now.
2dc05 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62  .  */.  if( pTab
2dc06 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 20  le->azModuleArg 
2dc07 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75  ){.    sqlite3Au
2dc08 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
2dc09 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 54  SQLITE_CREATE_VT
2dc0a 41 42 4c 45 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e  ABLE, pTable->zN
2dc0b 61 6d 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20  ame, .          
2dc0c 20 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75    pTable->azModu
2dc0d 6c 65 41 72 67 5b 30 5d 2c 20 70 50 61 72 73 65  leArg[0], pParse
2dc0e 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  ->db->aDb[iDb].z
2dc0f 4e 61 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  Name);.  }.#endi
2dc10 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  f.}../*.** This 
2dc11 72 6f 75 74 69 6e 65 20 74 61 6b 65 73 20 74 68  routine takes th
2dc12 65 20 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e  e module argumen
2dc13 74 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20  t that has been 
2dc14 61 63 63 75 6d 75 6c 61 74 69 6e 67 0a 2a 2a 20  accumulating.** 
2dc15 69 6e 20 70 50 61 72 73 65 2d 3e 7a 41 72 67 5b  in pParse->zArg[
2dc16 5d 20 61 6e 64 20 61 70 70 65 6e 64 73 20 69 74  ] and appends it
2dc17 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 20   to the list of 
2dc18 61 72 67 75 6d 65 6e 74 73 20 6f 6e 20 74 68 65  arguments on the
2dc19 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c  .** virtual tabl
2dc1a 65 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65  e currently unde
2dc1b 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 69  r construction i
2dc1c 6e 20 70 50 61 72 73 65 2d 3e 70 54 61 62 6c 65  n pParse->pTable
2dc1d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2dc1e 20 61 64 64 41 72 67 75 6d 65 6e 74 54 6f 56 74   addArgumentToVt
2dc1f 61 62 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ab(Parse *pParse
2dc20 29 7b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  ){.  if( pParse-
2dc21 3e 73 41 72 67 2e 7a 20 26 26 20 41 4c 57 41 59  >sArg.z && ALWAY
2dc22 53 28 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  S(pParse->pNewTa
2dc23 62 6c 65 29 20 29 7b 0a 20 20 20 20 63 6f 6e 73  ble) ){.    cons
2dc24 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e  t char *z = (con
2dc25 73 74 20 63 68 61 72 2a 29 70 50 61 72 73 65 2d  st char*)pParse-
2dc26 3e 73 41 72 67 2e 7a 3b 0a 20 20 20 20 69 6e 74  >sArg.z;.    int
2dc27 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 73 41 72   n = pParse->sAr
2dc28 67 2e 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  g.n;.    sqlite3
2dc29 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2dc2a 62 3b 0a 20 20 20 20 61 64 64 4d 6f 64 75 6c 65  b;.    addModule
2dc2b 41 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 50 61  Argument(db, pPa
2dc2c 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2c 20  rse->pNewTable, 
2dc2d 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70  sqlite3DbStrNDup
2dc2e 28 64 62 2c 20 7a 2c 20 6e 29 29 3b 0a 20 20 7d  (db, z, n));.  }
2dc2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61  .}../*.** The pa
2dc30 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  rser calls this 
2dc31 72 6f 75 74 69 6e 65 20 61 66 74 65 72 20 74 68  routine after th
2dc32 65 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c  e CREATE VIRTUAL
2dc33 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
2dc34 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 6f 6d  .** has been com
2dc35 70 6c 65 74 65 6c 79 20 70 61 72 73 65 64 2e 0a  pletely parsed..
2dc36 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2dc37 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
2dc38 61 62 46 69 6e 69 73 68 50 61 72 73 65 28 50 61  abFinishParse(Pa
2dc39 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b  rse *pParse, Tok
2dc3a 65 6e 20 2a 70 45 6e 64 29 7b 0a 20 20 54 61 62  en *pEnd){.  Tab
2dc3b 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61 72 73  le *pTab = pPars
2dc3c 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 20 20 2f  e->pNewTable;  /
2dc3d 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e  * The table bein
2dc3e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 20 2a 2f  g constructed */
2dc3f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2dc40 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20   pParse->db;    
2dc41 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
2dc42 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
2dc43 2a 2f 0a 0a 20 20 69 66 28 20 70 54 61 62 3d 3d  */..  if( pTab==
2dc44 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 64  0 ) return;.  ad
2dc45 64 41 72 67 75 6d 65 6e 74 54 6f 56 74 61 62 28  dArgumentToVtab(
2dc46 70 50 61 72 73 65 29 3b 0a 20 20 70 50 61 72 73  pParse);.  pPars
2dc47 65 2d 3e 73 41 72 67 2e 7a 20 3d 20 30 3b 0a 20  e->sArg.z = 0;. 
2dc48 20 69 66 28 20 70 54 61 62 2d 3e 6e 4d 6f 64 75   if( pTab->nModu
2dc49 6c 65 41 72 67 3c 31 20 29 20 72 65 74 75 72 6e  leArg<1 ) return
2dc4a 3b 0a 20 20 0a 20 20 2f 2a 20 49 66 20 74 68 65  ;.  .  /* If the
2dc4b 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20   CREATE VIRTUAL 
2dc4c 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20  TABLE statement 
2dc4d 69 73 20 62 65 69 6e 67 20 65 6e 74 65 72 65 64  is being entered
2dc4e 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 66 69   for the.  ** fi
2dc4f 72 73 74 20 74 69 6d 65 20 28 69 6e 20 6f 74 68  rst time (in oth
2dc50 65 72 20 77 6f 72 64 73 20 69 66 20 74 68 65 20  er words if the 
2dc51 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73  virtual table is
2dc52 20 61 63 74 75 61 6c 6c 79 20 62 65 69 6e 67 0a   actually being.
2dc53 20 20 2a 2a 20 63 72 65 61 74 65 64 20 6e 6f 77    ** created now
2dc54 20 69 6e 73 74 65 61 64 20 6f 66 20 6a 75 73 74   instead of just
2dc55 20 62 65 69 6e 67 20 72 65 61 64 20 6f 75 74 20   being read out 
2dc56 6f 66 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  of sqlite_master
2dc57 29 20 74 68 65 6e 0a 20 20 2a 2a 20 64 6f 20 61  ) then.  ** do a
2dc58 64 64 69 74 69 6f 6e 61 6c 20 69 6e 69 74 69 61  dditional initia
2dc59 6c 69 7a 61 74 69 6f 6e 20 77 6f 72 6b 20 61 6e  lization work an
2dc5a 64 20 73 74 6f 72 65 20 74 68 65 20 73 74 61 74  d store the stat
2dc5b 65 6d 65 6e 74 20 74 65 78 74 0a 20 20 2a 2a 20  ement text.  ** 
2dc5c 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  in the sqlite_ma
2dc5d 73 74 65 72 20 74 61 62 6c 65 2e 0a 20 20 2a 2f  ster table..  */
2dc5e 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74  .  if( !db->init
2dc5f 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 63 68 61  .busy ){.    cha
2dc60 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 20 20 63 68  r *zStmt;.    ch
2dc61 61 72 20 2a 7a 57 68 65 72 65 3b 0a 20 20 20 20  ar *zWhere;.    
2dc62 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 56 64 62  int iDb;.    Vdb
2dc63 65 20 2a 76 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f  e *v;..    /* Co
2dc64 6d 70 75 74 65 20 74 68 65 20 63 6f 6d 70 6c 65  mpute the comple
2dc65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 43  te text of the C
2dc66 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41  REATE VIRTUAL TA
2dc67 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  BLE statement */
2dc68 0a 20 20 20 20 69 66 28 20 70 45 6e 64 20 29 7b  .    if( pEnd ){
2dc69 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 73  .      pParse->s
2dc6a 4e 61 6d 65 54 6f 6b 65 6e 2e 6e 20 3d 20 28 69  NameToken.n = (i
2dc6b 6e 74 29 28 70 45 6e 64 2d 3e 7a 20 2d 20 70 50  nt)(pEnd->z - pP
2dc6c 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e  arse->sNameToken
2dc6d 2e 7a 29 20 2b 20 70 45 6e 64 2d 3e 6e 3b 0a 20  .z) + pEnd->n;. 
2dc6e 20 20 20 7d 0a 20 20 20 20 7a 53 74 6d 74 20 3d     }.    zStmt =
2dc6f 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
2dc70 64 62 2c 20 22 43 52 45 41 54 45 20 56 49 52 54  db, "CREATE VIRT
2dc71 55 41 4c 20 54 41 42 4c 45 20 25 54 22 2c 20 26  UAL TABLE %T", &
2dc72 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b  pParse->sNameTok
2dc73 65 6e 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20 73  en);..    /* A s
2dc74 6c 6f 74 20 66 6f 72 20 74 68 65 20 72 65 63 6f  lot for the reco
2dc75 72 64 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  rd has already b
2dc76 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e  een allocated in
2dc77 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c   the .    ** SQL
2dc78 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65  ITE_MASTER table
2dc79 2e 20 20 57 65 20 6a 75 73 74 20 6e 65 65 64 20  .  We just need 
2dc7a 74 6f 20 75 70 64 61 74 65 20 74 68 61 74 20 73  to update that s
2dc7b 6c 6f 74 20 77 69 74 68 20 61 6c 6c 0a 20 20 20  lot with all.   
2dc7c 20 2a 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74   ** the informat
2dc7d 69 6f 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65 63  ion we've collec
2dc7e 74 65 64 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20  ted.  .    **.  
2dc7f 20 20 2a 2a 20 54 68 65 20 56 4d 20 72 65 67 69    ** The VM regi
2dc80 73 74 65 72 20 6e 75 6d 62 65 72 20 70 50 61 72  ster number pPar
2dc81 73 65 2d 3e 72 65 67 52 6f 77 69 64 20 68 6f 6c  se->regRowid hol
2dc82 64 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20  ds the rowid of 
2dc83 61 6e 0a 20 20 20 20 2a 2a 20 65 6e 74 72 79 20  an.    ** entry 
2dc84 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  in the sqlite_ma
2dc85 73 74 65 72 20 74 61 62 6c 65 20 74 68 74 20 77  ster table tht w
2dc86 61 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 74  as created for t
2dc87 68 69 73 20 76 74 61 62 0a 20 20 20 20 2a 2a 20  his vtab.    ** 
2dc88 62 79 20 73 71 6c 69 74 65 33 53 74 61 72 74 54  by sqlite3StartT
2dc89 61 62 6c 65 28 29 2e 0a 20 20 20 20 2a 2f 0a 20  able()..    */. 
2dc8a 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33     iDb = sqlite3
2dc8b 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62  SchemaToIndex(db
2dc8c 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
2dc8d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73  ;.    sqlite3Nes
2dc8e 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
2dc8f 0a 20 20 20 20 20 20 22 55 50 44 41 54 45 20 25  .      "UPDATE %
2dc90 51 2e 25 73 20 22 0a 20 20 20 20 20 20 20 20 20  Q.%s ".         
2dc91 22 53 45 54 20 74 79 70 65 3d 27 74 61 62 6c 65  "SET type='table
2dc92 27 2c 20 6e 61 6d 65 3d 25 51 2c 20 74 62 6c 5f  ', name=%Q, tbl_
2dc93 6e 61 6d 65 3d 25 51 2c 20 72 6f 6f 74 70 61 67  name=%Q, rootpag
2dc94 65 3d 30 2c 20 73 71 6c 3d 25 51 20 22 0a 20 20  e=0, sql=%Q ".  
2dc95 20 20 20 20 20 22 57 48 45 52 45 20 72 6f 77 69       "WHERE rowi
2dc96 64 3d 23 25 64 22 2c 0a 20 20 20 20 20 20 64 62  d=#%d",.      db
2dc97 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
2dc98 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
2dc99 44 62 29 2c 0a 20 20 20 20 20 20 70 54 61 62 2d  Db),.      pTab-
2dc9a 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70 54  >zName,.      pT
2dc9b 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20  ab->zName,.     
2dc9c 20 7a 53 74 6d 74 2c 0a 20 20 20 20 20 20 70 50   zStmt,.      pP
2dc9d 61 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 0a 20  arse->regRowid. 
2dc9e 20 20 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65     );.    sqlite
2dc9f 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74 6d  3DbFree(db, zStm
2dca0 74 29 3b 0a 20 20 20 20 76 20 3d 20 73 71 6c 69  t);.    v = sqli
2dca1 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
2dca2 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43  e);.    sqlite3C
2dca3 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72  hangeCookie(pPar
2dca4 73 65 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 73  se, iDb);..    s
2dca5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2dca6 28 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 30  (v, OP_Expire, 0
2dca7 2c 20 30 29 3b 0a 20 20 20 20 7a 57 68 65 72 65  , 0);.    zWhere
2dca8 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
2dca9 66 28 64 62 2c 20 22 6e 61 6d 65 3d 27 25 71 27  f(db, "name='%q'
2dcaa 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  ", pTab->zName);
2dcab 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2dcac 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72  AddOp4(v, OP_Par
2dcad 73 65 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 31  seSchema, iDb, 1
2dcae 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20 50 34 5f  , 0, zWhere, P4_
2dcaf 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 73 71  DYNAMIC);.    sq
2dcb0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2dcb1 76 2c 20 4f 50 5f 56 43 72 65 61 74 65 2c 20 69  v, OP_VCreate, i
2dcb2 44 62 2c 20 30 2c 20 30 2c 20 0a 20 20 20 20 20  Db, 0, 0, .     
2dcb3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dcb4 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c      pTab->zName,
2dcb5 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2dcb6 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 20 2b 20  (pTab->zName) + 
2dcb7 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  1);.  }..  /* If
2dcb8 20 77 65 20 61 72 65 20 72 65 72 65 61 64 69 6e   we are rereadin
2dcb9 67 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  g the sqlite_mas
2dcba 74 65 72 20 74 61 62 6c 65 20 63 72 65 61 74 65  ter table create
2dcbb 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20   the in-memory. 
2dcbc 20 2a 2a 20 72 65 63 6f 72 64 20 6f 66 20 74 68   ** record of th
2dcbd 65 20 74 61 62 6c 65 2e 20 54 68 65 20 78 43 6f  e table. The xCo
2dcbe 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 20 69  nnect() method i
2dcbf 73 20 6e 6f 74 20 63 61 6c 6c 65 64 20 75 6e 74  s not called unt
2dcc0 69 6c 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73  il.  ** the firs
2dcc1 74 20 74 69 6d 65 20 74 68 65 20 76 69 72 74 75  t time the virtu
2dcc2 61 6c 20 74 61 62 6c 65 20 69 73 20 75 73 65 64  al table is used
2dcc3 20 69 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 65   in an SQL state
2dcc4 6d 65 6e 74 2e 20 54 68 69 73 0a 20 20 2a 2a 20  ment. This.  ** 
2dcc5 61 6c 6c 6f 77 73 20 61 20 73 63 68 65 6d 61 20  allows a schema 
2dcc6 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 76 69  that contains vi
2dcc7 72 74 75 61 6c 20 74 61 62 6c 65 73 20 74 6f 20  rtual tables to 
2dcc8 62 65 20 6c 6f 61 64 65 64 20 62 65 66 6f 72 65  be loaded before
2dcc9 0a 20 20 2a 2a 20 74 68 65 20 72 65 71 75 69 72  .  ** the requir
2dcca 65 64 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ed virtual table
2dccb 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
2dccc 20 61 72 65 20 72 65 67 69 73 74 65 72 65 64 2e   are registered.
2dccd 20 20 2a 2f 0a 20 20 65 6c 73 65 20 7b 0a 20 20    */.  else {.  
2dcce 20 20 54 61 62 6c 65 20 2a 70 4f 6c 64 3b 0a 20    Table *pOld;. 
2dccf 20 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65     Schema *pSche
2dcd0 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65  ma = pTab->pSche
2dcd1 6d 61 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  ma;.    const ch
2dcd2 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 54 61 62  ar *zName = pTab
2dcd3 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 6e 74  ->zName;.    int
2dcd4 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   nName = sqlite3
2dcd5 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b  Strlen30(zName);
2dcd6 0a 20 20 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69  .    pOld = sqli
2dcd7 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70  te3HashInsert(&p
2dcd8 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c  Schema->tblHash,
2dcd9 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70   zName, nName, p
2dcda 54 61 62 29 3b 0a 20 20 20 20 69 66 28 20 70 4f  Tab);.    if( pO
2dcdb 6c 64 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e  ld ){.      db->
2dcdc 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
2dcdd 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2dcde 70 54 61 62 3d 3d 70 4f 6c 64 20 29 3b 20 20 2f  pTab==pOld );  /
2dcdf 2a 20 4d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61  * Malloc must ha
2dce0 76 65 20 66 61 69 6c 65 64 20 69 6e 73 69 64 65  ve failed inside
2dce1 20 48 61 73 68 49 6e 73 65 72 74 28 29 20 2a 2f   HashInsert() */
2dce2 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
2dce3 20 20 20 7d 0a 20 20 20 20 70 53 63 68 65 6d 61     }.    pSchema
2dce4 2d 3e 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64  ->db = pParse->d
2dce5 62 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70  b;.    pParse->p
2dce6 4e 65 77 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20  NewTable = 0;.  
2dce7 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70  }.}../*.** The p
2dce8 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73  arser calls this
2dce9 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74   routine when it
2dcea 20 73 65 65 73 20 74 68 65 20 66 69 72 73 74 20   sees the first 
2dceb 74 6f 6b 65 6e 0a 2a 2a 20 6f 66 20 61 6e 20 61  token.** of an a
2dcec 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 6d  rgument to the m
2dced 6f 64 75 6c 65 20 6e 61 6d 65 20 69 6e 20 61 20  odule name in a 
2dcee 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54  CREATE VIRTUAL T
2dcef 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a  ABLE statement..
2dcf0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2dcf1 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
2dcf2 61 62 41 72 67 49 6e 69 74 28 50 61 72 73 65 20  abArgInit(Parse 
2dcf3 2a 70 50 61 72 73 65 29 7b 0a 20 20 61 64 64 41  *pParse){.  addA
2dcf4 72 67 75 6d 65 6e 74 54 6f 56 74 61 62 28 70 50  rgumentToVtab(pP
2dcf5 61 72 73 65 29 3b 0a 20 20 70 50 61 72 73 65 2d  arse);.  pParse-
2dcf6 3e 73 41 72 67 2e 7a 20 3d 20 30 3b 0a 20 20 70  >sArg.z = 0;.  p
2dcf7 50 61 72 73 65 2d 3e 73 41 72 67 2e 6e 20 3d 20  Parse->sArg.n = 
2dcf8 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  0;.}../*.** The 
2dcf9 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
2dcfa 73 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 65 61  s routine for ea
2dcfb 63 68 20 74 6f 6b 65 6e 20 61 66 74 65 72 20 74  ch token after t
2dcfc 68 65 20 66 69 72 73 74 20 74 6f 6b 65 6e 0a 2a  he first token.*
2dcfd 2a 20 69 6e 20 61 6e 20 61 72 67 75 6d 65 6e 74  * in an argument
2dcfe 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 6e   to the module n
2dcff 61 6d 65 20 69 6e 20 61 20 43 52 45 41 54 45 20  ame in a CREATE 
2dd00 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74  VIRTUAL TABLE st
2dd01 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  atement..*/.SQLI
2dd02 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2dd03 73 71 6c 69 74 65 33 56 74 61 62 41 72 67 45 78  sqlite3VtabArgEx
2dd04 74 65 6e 64 28 50 61 72 73 65 20 2a 70 50 61 72  tend(Parse *pPar
2dd05 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 29 7b 0a 20  se, Token *p){. 
2dd06 20 54 6f 6b 65 6e 20 2a 70 41 72 67 20 3d 20 26   Token *pArg = &
2dd07 70 50 61 72 73 65 2d 3e 73 41 72 67 3b 0a 20 20  pParse->sArg;.  
2dd08 69 66 28 20 70 41 72 67 2d 3e 7a 3d 3d 30 20 29  if( pArg->z==0 )
2dd09 7b 0a 20 20 20 20 70 41 72 67 2d 3e 7a 20 3d 20  {.    pArg->z = 
2dd0a 70 2d 3e 7a 3b 0a 20 20 20 20 70 41 72 67 2d 3e  p->z;.    pArg->
2dd0b 6e 20 3d 20 70 2d 3e 6e 3b 0a 20 20 7d 65 6c 73  n = p->n;.  }els
2dd0c 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 70 41  e{.    assert(pA
2dd0d 72 67 2d 3e 7a 20 3c 20 70 2d 3e 7a 29 3b 0a 20  rg->z < p->z);. 
2dd0e 20 20 20 70 41 72 67 2d 3e 6e 20 3d 20 28 69 6e     pArg->n = (in
2dd0f 74 29 28 26 70 2d 3e 7a 5b 70 2d 3e 6e 5d 20 2d  t)(&p->z[p->n] -
2dd10 20 70 41 72 67 2d 3e 7a 29 3b 0a 20 20 7d 0a 7d   pArg->z);.  }.}
2dd11 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61  ../*.** Invoke a
2dd12 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63   virtual table c
2dd13 6f 6e 73 74 72 75 63 74 6f 72 20 28 65 69 74 68  onstructor (eith
2dd14 65 72 20 78 43 72 65 61 74 65 20 6f 72 20 78 43  er xCreate or xC
2dd15 6f 6e 6e 65 63 74 29 2e 20 54 68 65 0a 2a 2a 20  onnect). The.** 
2dd16 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66  pointer to the f
2dd17 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b  unction to invok
2dd18 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74  e is passed as t
2dd19 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65  he fourth parame
2dd1a 74 65 72 0a 2a 2a 20 74 6f 20 74 68 69 73 20 70  ter.** to this p
2dd1b 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61  rocedure..*/.sta
2dd1c 74 69 63 20 69 6e 74 20 76 74 61 62 43 61 6c 6c  tic int vtabCall
2dd1d 43 6f 6e 73 74 72 75 63 74 6f 72 28 0a 20 20 73  Constructor(.  s
2dd1e 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 54  qlite3 *db, .  T
2dd1f 61 62 6c 65 20 2a 70 54 61 62 2c 0a 20 20 4d 6f  able *pTab,.  Mo
2dd20 64 75 6c 65 20 2a 70 4d 6f 64 2c 0a 20 20 69 6e  dule *pMod,.  in
2dd21 74 20 28 2a 78 43 6f 6e 73 74 72 75 63 74 29 28  t (*xConstruct)(
2dd22 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 69  sqlite3*,void*,i
2dd23 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 63 6f  nt,const char*co
2dd24 6e 73 74 2a 2c 73 71 6c 69 74 65 33 5f 76 74 61  nst*,sqlite3_vta
2dd25 62 2a 2a 2c 63 68 61 72 2a 2a 29 2c 0a 20 20 63  b**,char**),.  c
2dd26 68 61 72 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20  har **pzErr.){. 
2dd27 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62 6c 65   VTable *pVTable
2dd28 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 63 6f  ;.  int rc;.  co
2dd29 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a  nst char *const*
2dd2a 61 7a 41 72 67 20 3d 20 28 63 6f 6e 73 74 20 63  azArg = (const c
2dd2b 68 61 72 20 2a 63 6f 6e 73 74 2a 29 70 54 61 62  har *const*)pTab
2dd2c 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 0a 20  ->azModuleArg;. 
2dd2d 20 69 6e 74 20 6e 41 72 67 20 3d 20 70 54 61 62   int nArg = pTab
2dd2e 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 3b 0a 20 20  ->nModuleArg;.  
2dd2f 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a  char *zErr = 0;.
2dd30 20 20 63 68 61 72 20 2a 7a 4d 6f 64 75 6c 65 4e    char *zModuleN
2dd31 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ame = sqlite3MPr
2dd32 69 6e 74 66 28 64 62 2c 20 22 25 73 22 2c 20 70  intf(db, "%s", p
2dd33 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20  Tab->zName);..  
2dd34 69 66 28 20 21 7a 4d 6f 64 75 6c 65 4e 61 6d 65  if( !zModuleName
2dd35 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
2dd36 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
2dd37 0a 0a 20 20 70 56 54 61 62 6c 65 20 3d 20 73 71  ..  pVTable = sq
2dd38 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
2dd39 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 56 54 61  o(db, sizeof(VTa
2dd3a 62 6c 65 29 29 3b 0a 20 20 69 66 28 20 21 70 56  ble));.  if( !pV
2dd3b 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 73 71 6c  Table ){.    sql
2dd3c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2dd3d 4d 6f 64 75 6c 65 4e 61 6d 65 29 3b 0a 20 20 20  ModuleName);.   
2dd3e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
2dd3f 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 56 54 61  OMEM;.  }.  pVTa
2dd40 62 6c 65 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20  ble->db = db;.  
2dd41 70 56 54 61 62 6c 65 2d 3e 70 4d 6f 64 20 3d 20  pVTable->pMod = 
2dd42 70 4d 6f 64 3b 0a 0a 20 20 61 73 73 65 72 74 28  pMod;..  assert(
2dd43 20 21 64 62 2d 3e 70 56 54 61 62 20 29 3b 0a 20   !db->pVTab );. 
2dd44 20 61 73 73 65 72 74 28 20 78 43 6f 6e 73 74 72   assert( xConstr
2dd45 75 63 74 20 29 3b 0a 20 20 64 62 2d 3e 70 56 54  uct );.  db->pVT
2dd46 61 62 20 3d 20 70 54 61 62 3b 0a 0a 20 20 2f 2a  ab = pTab;..  /*
2dd47 20 49 6e 76 6f 6b 65 20 74 68 65 20 76 69 72 74   Invoke the virt
2dd48 75 61 6c 20 74 61 62 6c 65 20 63 6f 6e 73 74 72  ual table constr
2dd49 75 63 74 6f 72 20 2a 2f 0a 20 20 28 76 6f 69 64  uctor */.  (void
2dd4a 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66  )sqlite3SafetyOf
2dd4b 66 28 64 62 29 3b 0a 20 20 72 63 20 3d 20 78 43  f(db);.  rc = xC
2dd4c 6f 6e 73 74 72 75 63 74 28 64 62 2c 20 70 4d 6f  onstruct(db, pMo
2dd4d 64 2d 3e 70 41 75 78 2c 20 6e 41 72 67 2c 20 61  d->pAux, nArg, a
2dd4e 7a 41 72 67 2c 20 26 70 56 54 61 62 6c 65 2d 3e  zArg, &pVTable->
2dd4f 70 56 74 61 62 2c 20 26 7a 45 72 72 29 3b 0a 20  pVtab, &zErr);. 
2dd50 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
2dd51 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 69 66  fetyOn(db);.  if
2dd52 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
2dd53 45 4d 20 29 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  EM ) db->mallocF
2dd54 61 69 6c 65 64 20 3d 20 31 3b 0a 0a 20 20 69 66  ailed = 1;..  if
2dd55 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20  ( SQLITE_OK!=rc 
2dd56 29 7b 0a 20 20 20 20 69 66 28 20 7a 45 72 72 3d  ){.    if( zErr=
2dd57 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45  =0 ){.      *pzE
2dd58 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  rr = sqlite3MPri
2dd59 6e 74 66 28 64 62 2c 20 22 76 74 61 62 6c 65 20  ntf(db, "vtable 
2dd5a 63 6f 6e 73 74 72 75 63 74 6f 72 20 66 61 69 6c  constructor fail
2dd5b 65 64 3a 20 25 73 22 2c 20 7a 4d 6f 64 75 6c 65  ed: %s", zModule
2dd5c 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Name);.    }else
2dd5d 20 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 20   {.      *pzErr 
2dd5e 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2dd5f 28 64 62 2c 20 22 25 73 22 2c 20 7a 45 72 72 29  (db, "%s", zErr)
2dd60 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ;.      sqlite3D
2dd61 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b  bFree(db, zErr);
2dd62 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2dd63 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 56 54  e3DbFree(db, pVT
2dd64 61 62 6c 65 29 3b 0a 20 20 7d 65 6c 73 65 20 69  able);.  }else i
2dd65 66 28 20 41 4c 57 41 59 53 28 70 56 54 61 62 6c  f( ALWAYS(pVTabl
2dd66 65 2d 3e 70 56 74 61 62 29 20 29 7b 0a 20 20 20  e->pVtab) ){.   
2dd67 20 2f 2a 20 4a 75 73 74 69 66 69 63 61 74 69 6f   /* Justificatio
2dd68 6e 20 6f 66 20 41 4c 57 41 59 53 28 29 3a 20 20  n of ALWAYS():  
2dd69 41 20 63 6f 72 72 65 63 74 20 76 74 61 62 20 63  A correct vtab c
2dd6a 6f 6e 73 74 72 75 63 74 6f 72 20 6d 75 73 74 20  onstructor must 
2dd6b 61 6c 6c 6f 63 61 74 65 0a 20 20 20 20 2a 2a 20  allocate.    ** 
2dd6c 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62  the sqlite3_vtab
2dd6d 20 6f 62 6a 65 63 74 20 69 66 20 73 75 63 63 65   object if succe
2dd6e 73 73 66 75 6c 2e 20 20 2a 2f 0a 20 20 20 20 70  ssful.  */.    p
2dd6f 56 54 61 62 6c 65 2d 3e 70 56 74 61 62 2d 3e 70  VTable->pVtab->p
2dd70 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64 2d 3e 70  Module = pMod->p
2dd71 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 70 56 54 61  Module;.    pVTa
2dd72 62 6c 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20  ble->nRef = 1;. 
2dd73 20 20 20 69 66 28 20 64 62 2d 3e 70 56 54 61 62     if( db->pVTab
2dd74 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20   ){.      const 
2dd75 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 20 3d 20  char *zFormat = 
2dd76 22 76 74 61 62 6c 65 20 63 6f 6e 73 74 72 75 63  "vtable construc
2dd77 74 6f 72 20 64 69 64 20 6e 6f 74 20 64 65 63 6c  tor did not decl
2dd78 61 72 65 20 73 63 68 65 6d 61 3a 20 25 73 22 3b  are schema: %s";
2dd79 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 20 3d 20  .      *pzErr = 
2dd7a 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
2dd7b 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 70 54 61 62  b, zFormat, pTab
2dd7c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ->zName);.      
2dd7d 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63  sqlite3VtabUnloc
2dd7e 6b 28 70 56 54 61 62 6c 65 29 3b 0a 20 20 20 20  k(pVTable);.    
2dd7f 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
2dd80 52 4f 52 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ROR;.    }else{.
2dd81 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b 0a        int iCol;.
2dd82 20 20 20 20 20 20 2f 2a 20 49 66 20 65 76 65 72        /* If ever
2dd83 79 74 68 69 6e 67 20 77 65 6e 74 20 61 63 63 6f  ything went acco
2dd84 72 64 69 6e 67 20 74 6f 20 70 6c 61 6e 2c 20 6c  rding to plan, l
2dd85 69 6e 6b 20 74 68 65 20 6e 65 77 20 56 54 61 62  ink the new VTab
2dd86 6c 65 20 73 74 72 75 63 74 75 72 65 0a 20 20 20  le structure.   
2dd87 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6c     ** into the l
2dd88 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65  inked list heade
2dd89 64 20 62 79 20 70 54 61 62 2d 3e 70 56 54 61 62  d by pTab->pVTab
2dd8a 6c 65 2e 20 54 68 65 6e 20 6c 6f 6f 70 20 74 68  le. Then loop th
2dd8b 72 6f 75 67 68 20 74 68 65 20 0a 20 20 20 20 20  rough the .     
2dd8c 20 2a 2a 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74   ** columns of t
2dd8d 68 65 20 74 61 62 6c 65 20 74 6f 20 73 65 65 20  he table to see 
2dd8e 69 66 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 63  if any of them c
2dd8f 6f 6e 74 61 69 6e 20 74 68 65 20 74 6f 6b 65 6e  ontain the token
2dd90 20 22 68 69 64 64 65 6e 22 2e 0a 20 20 20 20 20   "hidden"..     
2dd91 20 2a 2a 20 49 66 20 73 6f 2c 20 73 65 74 20 74   ** If so, set t
2dd92 68 65 20 43 6f 6c 75 6d 6e 2e 69 73 48 69 64 64  he Column.isHidd
2dd93 65 6e 20 66 6c 61 67 20 61 6e 64 20 72 65 6d 6f  en flag and remo
2dd94 76 65 20 74 68 65 20 74 6f 6b 65 6e 20 66 72 6f  ve the token fro
2dd95 6d 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 74  m.      ** the t
2dd96 79 70 65 20 73 74 72 69 6e 67 2e 20 20 2a 2f 0a  ype string.  */.
2dd97 20 20 20 20 20 20 70 56 54 61 62 6c 65 2d 3e 70        pVTable->p
2dd98 4e 65 78 74 20 3d 20 70 54 61 62 2d 3e 70 56 54  Next = pTab->pVT
2dd99 61 62 6c 65 3b 0a 20 20 20 20 20 20 70 54 61 62  able;.      pTab
2dd9a 2d 3e 70 56 54 61 62 6c 65 20 3d 20 70 56 54 61  ->pVTable = pVTa
2dd9b 62 6c 65 3b 0a 0a 20 20 20 20 20 20 66 6f 72 28  ble;..      for(
2dd9c 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61  iCol=0; iCol<pTa
2dd9d 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29  b->nCol; iCol++)
2dd9e 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  {.        char *
2dd9f 7a 54 79 70 65 20 3d 20 70 54 61 62 2d 3e 61 43  zType = pTab->aC
2dda0 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 70 65 3b 0a  ol[iCol].zType;.
2dda1 20 20 20 20 20 20 20 20 69 6e 74 20 6e 54 79 70          int nTyp
2dda2 65 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  e;.        int i
2dda3 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66   = 0;.        if
2dda4 28 20 21 7a 54 79 70 65 20 29 20 63 6f 6e 74 69  ( !zType ) conti
2dda5 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 6e 54 79  nue;.        nTy
2dda6 70 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  pe = sqlite3Strl
2dda7 65 6e 33 30 28 7a 54 79 70 65 29 3b 0a 20 20 20  en30(zType);.   
2dda8 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
2dda9 53 74 72 4e 49 43 6d 70 28 22 68 69 64 64 65 6e  StrNICmp("hidden
2ddaa 22 2c 20 7a 54 79 70 65 2c 20 36 29 7c 7c 28 7a  ", zType, 6)||(z
2ddab 54 79 70 65 5b 36 5d 20 26 26 20 7a 54 79 70 65  Type[6] && zType
2ddac 5b 36 5d 21 3d 27 20 27 29 20 29 7b 0a 20 20 20  [6]!=' ') ){.   
2ddad 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20         for(i=0; 
2ddae 69 3c 6e 54 79 70 65 3b 20 69 2b 2b 29 7b 0a 20  i<nType; i++){. 
2ddaf 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 28             if( (
2ddb0 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 4e 49 43  0==sqlite3StrNIC
2ddb1 6d 70 28 22 20 68 69 64 64 65 6e 22 2c 20 26 7a  mp(" hidden", &z
2ddb2 54 79 70 65 5b 69 5d 2c 20 37 29 29 0a 20 20 20  Type[i], 7)).   
2ddb3 20 20 20 20 20 20 20 20 20 20 26 26 20 28 7a 54            && (zT
2ddb4 79 70 65 5b 69 2b 37 5d 3d 3d 27 5c 30 27 20 7c  ype[i+7]=='\0' |
2ddb5 7c 20 7a 54 79 70 65 5b 69 2b 37 5d 3d 3d 27 20  | zType[i+7]==' 
2ddb6 27 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 29  ').            )
2ddb7 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2ddb8 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  i++;.           
2ddb9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2ddba 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2ddbb 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
2ddbc 20 20 20 20 20 20 69 66 28 20 69 3c 6e 54 79 70        if( i<nTyp
2ddbd 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  e ){.          i
2ddbe 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20  nt j;.          
2ddbf 69 6e 74 20 6e 44 65 6c 20 3d 20 36 20 2b 20 28  int nDel = 6 + (
2ddc0 7a 54 79 70 65 5b 69 2b 36 5d 20 3f 20 31 20 3a  zType[i+6] ? 1 :
2ddc1 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 66   0);.          f
2ddc2 6f 72 28 6a 3d 69 3b 20 28 6a 2b 6e 44 65 6c 29  or(j=i; (j+nDel)
2ddc3 3c 3d 6e 54 79 70 65 3b 20 6a 2b 2b 29 7b 0a 20  <=nType; j++){. 
2ddc4 20 20 20 20 20 20 20 20 20 20 20 7a 54 79 70 65             zType
2ddc5 5b 6a 5d 20 3d 20 7a 54 79 70 65 5b 6a 2b 6e 44  [j] = zType[j+nD
2ddc6 65 6c 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  el];.          }
2ddc7 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a  .          if( z
2ddc8 54 79 70 65 5b 69 5d 3d 3d 27 5c 30 27 20 26 26  Type[i]=='\0' &&
2ddc9 20 69 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20   i>0 ){.        
2ddca 20 20 20 20 61 73 73 65 72 74 28 7a 54 79 70 65      assert(zType
2ddcb 5b 69 2d 31 5d 3d 3d 27 20 27 29 3b 0a 20 20 20  [i-1]==' ');.   
2ddcc 20 20 20 20 20 20 20 20 20 7a 54 79 70 65 5b 69           zType[i
2ddcd 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20  -1] = '\0';.    
2ddce 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2ddcf 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f    pTab->aCol[iCo
2ddd0 6c 5d 2e 69 73 48 69 64 64 65 6e 20 3d 20 31 3b  l].isHidden = 1;
2ddd1 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2ddd2 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
2ddd3 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2ddd4 2c 20 7a 4d 6f 64 75 6c 65 4e 61 6d 65 29 3b 0a  , zModuleName);.
2ddd5 20 20 64 62 2d 3e 70 56 54 61 62 20 3d 20 30 3b    db->pVTab = 0;
2ddd6 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2ddd7 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
2ddd8 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20  tion is invoked 
2ddd9 62 79 20 74 68 65 20 70 61 72 73 65 72 20 74 6f  by the parser to
2ddda 20 63 61 6c 6c 20 74 68 65 20 78 43 6f 6e 6e 65   call the xConne
2dddb 63 74 28 29 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f  ct() method.** o
2dddc 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  f the virtual ta
2dddd 62 6c 65 20 70 54 61 62 2e 20 49 66 20 61 6e 20  ble pTab. If an 
2ddde 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e  error occurs, an
2dddf 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
2dde0 65 74 75 72 6e 65 64 20 0a 2a 2a 20 61 6e 64 20  eturned .** and 
2dde1 61 6e 20 65 72 72 6f 72 20 6c 65 66 74 20 69 6e  an error left in
2dde2 20 70 50 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54   pParse..**.** T
2dde3 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f  his call is a no
2dde4 2d 6f 70 20 69 66 20 74 61 62 6c 65 20 70 54 61  -op if table pTa
2dde5 62 20 69 73 20 6e 6f 74 20 61 20 76 69 72 74 75  b is not a virtu
2dde6 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  al table..*/.SQL
2dde7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2dde8 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43  sqlite3VtabCallC
2dde9 6f 6e 6e 65 63 74 28 50 61 72 73 65 20 2a 70 50  onnect(Parse *pP
2ddea 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61  arse, Table *pTa
2ddeb 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  b){.  sqlite3 *d
2ddec 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2dded 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d    const char *zM
2ddee 6f 64 3b 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d  od;.  Module *pM
2ddef 6f 64 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  od;.  int rc;.. 
2ddf0 20 61 73 73 65 72 74 28 20 70 54 61 62 20 29 3b   assert( pTab );
2ddf1 0a 20 20 69 66 28 20 28 70 54 61 62 2d 3e 74 61  .  if( (pTab->ta
2ddf2 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74  bFlags & TF_Virt
2ddf3 75 61 6c 29 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  ual)==0 || sqlit
2ddf4 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20  e3GetVTable(db, 
2ddf5 70 54 61 62 29 20 29 7b 0a 20 20 20 20 72 65 74  pTab) ){.    ret
2ddf6 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
2ddf7 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20   }..  /* Locate 
2ddf8 74 68 65 20 72 65 71 75 69 72 65 64 20 76 69 72  the required vir
2ddf9 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
2ddfa 65 20 2a 2f 0a 20 20 7a 4d 6f 64 20 3d 20 70 54  e */.  zMod = pT
2ddfb 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b  ab->azModuleArg[
2ddfc 30 5d 3b 0a 20 20 70 4d 6f 64 20 3d 20 28 4d 6f  0];.  pMod = (Mo
2ddfd 64 75 6c 65 2a 29 73 71 6c 69 74 65 33 48 61 73  dule*)sqlite3Has
2ddfe 68 46 69 6e 64 28 26 64 62 2d 3e 61 4d 6f 64 75  hFind(&db->aModu
2ddff 6c 65 2c 20 7a 4d 6f 64 2c 20 73 71 6c 69 74 65  le, zMod, sqlite
2de00 33 53 74 72 6c 65 6e 33 30 28 7a 4d 6f 64 29 29  3Strlen30(zMod))
2de01 3b 0a 0a 20 20 69 66 28 20 21 70 4d 6f 64 20 29  ;..  if( !pMod )
2de02 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
2de03 20 2a 7a 4d 6f 64 75 6c 65 20 3d 20 70 54 61 62   *zModule = pTab
2de04 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d  ->azModuleArg[0]
2de05 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  ;.    sqlite3Err
2de06 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
2de07 6f 20 73 75 63 68 20 6d 6f 64 75 6c 65 3a 20 25  o such module: %
2de08 73 22 2c 20 7a 4d 6f 64 75 6c 65 29 3b 0a 20 20  s", zModule);.  
2de09 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
2de0a 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ROR;.  }else{.  
2de0b 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30    char *zErr = 0
2de0c 3b 0a 20 20 20 20 72 63 20 3d 20 76 74 61 62 43  ;.    rc = vtabC
2de0d 61 6c 6c 43 6f 6e 73 74 72 75 63 74 6f 72 28 64  allConstructor(d
2de0e 62 2c 20 70 54 61 62 2c 20 70 4d 6f 64 2c 20 70  b, pTab, pMod, p
2de0f 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43  Mod->pModule->xC
2de10 6f 6e 6e 65 63 74 2c 20 26 7a 45 72 72 29 3b 0a  onnect, &zErr);.
2de11 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
2de12 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73  TE_OK ){.      s
2de13 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2de14 50 61 72 73 65 2c 20 22 25 73 22 2c 20 7a 45 72  Parse, "%s", zEr
2de15 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  r);.    }.    sq
2de16 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2de17 7a 45 72 72 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  zErr);.  }..  re
2de18 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
2de19 2a 20 41 64 64 20 74 68 65 20 76 69 72 74 75 61  * Add the virtua
2de1a 6c 20 74 61 62 6c 65 20 70 56 54 61 62 20 74 6f  l table pVTab to
2de1b 20 74 68 65 20 61 72 72 61 79 20 73 71 6c 69 74   the array sqlit
2de1c 65 33 2e 61 56 54 72 61 6e 73 5b 5d 2e 0a 2a 2f  e3.aVTrans[]..*/
2de1d 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 54  .static int addT
2de1e 6f 56 54 72 61 6e 73 28 73 71 6c 69 74 65 33 20  oVTrans(sqlite3 
2de1f 2a 64 62 2c 20 56 54 61 62 6c 65 20 2a 70 56 54  *db, VTable *pVT
2de20 61 62 29 7b 0a 20 20 63 6f 6e 73 74 20 69 6e 74  ab){.  const int
2de21 20 41 52 52 41 59 5f 49 4e 43 52 20 3d 20 35 3b   ARRAY_INCR = 5;
2de22 0a 0a 20 20 2f 2a 20 47 72 6f 77 20 74 68 65 20  ..  /* Grow the 
2de23 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20  sqlite3.aVTrans 
2de24 61 72 72 61 79 20 69 66 20 72 65 71 75 69 72 65  array if require
2de25 64 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e  d */.  if( (db->
2de26 6e 56 54 72 61 6e 73 25 41 52 52 41 59 5f 49 4e  nVTrans%ARRAY_IN
2de27 43 52 29 3d 3d 30 20 29 7b 0a 20 20 20 20 56 54  CR)==0 ){.    VT
2de28 61 62 6c 65 20 2a 2a 61 56 54 72 61 6e 73 3b 0a  able **aVTrans;.
2de29 20 20 20 20 69 6e 74 20 6e 42 79 74 65 73 20 3d      int nBytes =
2de2a 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f   sizeof(sqlite3_
2de2b 76 74 61 62 20 2a 29 20 2a 20 28 64 62 2d 3e 6e  vtab *) * (db->n
2de2c 56 54 72 61 6e 73 20 2b 20 41 52 52 41 59 5f 49  VTrans + ARRAY_I
2de2d 4e 43 52 29 3b 0a 20 20 20 20 61 56 54 72 61 6e  NCR);.    aVTran
2de2e 73 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61  s = sqlite3DbRea
2de2f 6c 6c 6f 63 28 64 62 2c 20 28 76 6f 69 64 20 2a  lloc(db, (void *
2de30 29 64 62 2d 3e 61 56 54 72 61 6e 73 2c 20 6e 42  )db->aVTrans, nB
2de31 79 74 65 73 29 3b 0a 20 20 20 20 69 66 28 20 21  ytes);.    if( !
2de32 61 56 54 72 61 6e 73 20 29 7b 0a 20 20 20 20 20  aVTrans ){.     
2de33 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
2de34 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OMEM;.    }.    
2de35 6d 65 6d 73 65 74 28 26 61 56 54 72 61 6e 73 5b  memset(&aVTrans[
2de36 64 62 2d 3e 6e 56 54 72 61 6e 73 5d 2c 20 30 2c  db->nVTrans], 0,
2de37 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f   sizeof(sqlite3_
2de38 76 74 61 62 20 2a 29 2a 41 52 52 41 59 5f 49 4e  vtab *)*ARRAY_IN
2de39 43 52 29 3b 0a 20 20 20 20 64 62 2d 3e 61 56 54  CR);.    db->aVT
2de3a 72 61 6e 73 20 3d 20 61 56 54 72 61 6e 73 3b 0a  rans = aVTrans;.
2de3b 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 70 56    }..  /* Add pV
2de3c 74 61 62 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  tab to the end o
2de3d 66 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e  f sqlite3.aVTran
2de3e 73 20 2a 2f 0a 20 20 64 62 2d 3e 61 56 54 72 61  s */.  db->aVTra
2de3f 6e 73 5b 64 62 2d 3e 6e 56 54 72 61 6e 73 2b 2b  ns[db->nVTrans++
2de40 5d 20 3d 20 70 56 54 61 62 3b 0a 20 20 73 71 6c  ] = pVTab;.  sql
2de41 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 70 56 54  ite3VtabLock(pVT
2de42 61 62 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ab);.  return SQ
2de43 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
2de44 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
2de45 69 73 20 69 6e 76 6f 6b 65 64 20 62 79 20 74 68  is invoked by th
2de46 65 20 76 64 62 65 20 74 6f 20 63 61 6c 6c 20 74  e vdbe to call t
2de47 68 65 20 78 43 72 65 61 74 65 20 6d 65 74 68 6f  he xCreate metho
2de48 64 0a 2a 2a 20 6f 66 20 74 68 65 20 76 69 72 74  d.** of the virt
2de49 75 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65 64 20  ual table named 
2de4a 7a 54 61 62 20 69 6e 20 64 61 74 61 62 61 73 65  zTab in database
2de4b 20 69 44 62 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20   iDb. .**.** If 
2de4c 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
2de4d 20 2a 70 7a 45 72 72 20 69 73 20 73 65 74 20 74   *pzErr is set t
2de4e 6f 20 70 6f 69 6e 74 20 61 6e 20 61 6e 20 45 6e  o point an an En
2de4f 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 0a 2a  glish language.*
2de50 2a 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66  * description of
2de51 20 74 68 65 20 65 72 72 6f 72 20 61 6e 64 20 61   the error and a
2de52 6e 20 53 51 4c 49 54 45 5f 58 58 58 20 65 72 72  n SQLITE_XXX err
2de53 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
2de54 6e 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 69 73 20  ned..** In this 
2de55 63 61 73 65 20 74 68 65 20 63 61 6c 6c 65 72 20  case the caller 
2de56 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65  must call sqlite
2de57 33 44 62 46 72 65 65 28 64 62 2c 20 29 20 6f 6e  3DbFree(db, ) on
2de58 20 2a 70 7a 45 72 72 2e 0a 2a 2f 0a 53 51 4c 49   *pzErr..*/.SQLI
2de59 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2de5a 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 72  qlite3VtabCallCr
2de5b 65 61 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62  eate(sqlite3 *db
2de5c 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74  , int iDb, const
2de5d 20 63 68 61 72 20 2a 7a 54 61 62 2c 20 63 68 61   char *zTab, cha
2de5e 72 20 2a 2a 70 7a 45 72 72 29 7b 0a 20 20 69 6e  r **pzErr){.  in
2de5f 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
2de60 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  ;.  Table *pTab;
2de61 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b  .  Module *pMod;
2de62 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2de63 4d 6f 64 3b 0a 0a 20 20 70 54 61 62 20 3d 20 73  Mod;..  pTab = s
2de64 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
2de65 64 62 2c 20 7a 54 61 62 2c 20 64 62 2d 3e 61 44  db, zTab, db->aD
2de66 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  b[iDb].zName);. 
2de67 20 61 73 73 65 72 74 28 20 70 54 61 62 20 26 26   assert( pTab &&
2de68 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73   (pTab->tabFlags
2de69 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d   & TF_Virtual)!=
2de6a 30 20 26 26 20 21 70 54 61 62 2d 3e 70 56 54 61  0 && !pTab->pVTa
2de6b 62 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63  ble );..  /* Loc
2de6c 61 74 65 20 74 68 65 20 72 65 71 75 69 72 65 64  ate the required
2de6d 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
2de6e 6f 64 75 6c 65 20 2a 2f 0a 20 20 7a 4d 6f 64 20  odule */.  zMod 
2de6f 3d 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65  = pTab->azModule
2de70 41 72 67 5b 30 5d 3b 0a 20 20 70 4d 6f 64 20 3d  Arg[0];.  pMod =
2de71 20 28 4d 6f 64 75 6c 65 2a 29 73 71 6c 69 74 65   (Module*)sqlite
2de72 33 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61  3HashFind(&db->a
2de73 4d 6f 64 75 6c 65 2c 20 7a 4d 6f 64 2c 20 73 71  Module, zMod, sq
2de74 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4d  lite3Strlen30(zM
2de75 6f 64 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  od));..  /* If t
2de76 68 65 20 6d 6f 64 75 6c 65 20 68 61 73 20 62 65  he module has be
2de77 65 6e 20 72 65 67 69 73 74 65 72 65 64 20 61 6e  en registered an
2de78 64 20 69 6e 63 6c 75 64 65 73 20 61 20 43 72 65  d includes a Cre
2de79 61 74 65 20 6d 65 74 68 6f 64 2c 20 0a 20 20 2a  ate method, .  *
2de7a 2a 20 69 6e 76 6f 6b 65 20 69 74 20 6e 6f 77 2e  * invoke it now.
2de7b 20 49 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 68   If the module h
2de7c 61 73 20 6e 6f 74 20 62 65 65 6e 20 72 65 67 69  as not been regi
2de7d 73 74 65 72 65 64 2c 20 72 65 74 75 72 6e 20 61  stered, return a
2de7e 6e 20 0a 20 20 2a 2a 20 65 72 72 6f 72 2e 20 4f  n .  ** error. O
2de7f 74 68 65 72 77 69 73 65 2c 20 64 6f 20 6e 6f 74  therwise, do not
2de80 68 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  hing..  */.  if(
2de81 20 21 70 4d 6f 64 20 29 7b 0a 20 20 20 20 2a 70   !pMod ){.    *p
2de82 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50  zErr = sqlite3MP
2de83 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73 75  rintf(db, "no su
2de84 63 68 20 6d 6f 64 75 6c 65 3a 20 25 73 22 2c 20  ch module: %s", 
2de85 7a 4d 6f 64 29 3b 0a 20 20 20 20 72 63 20 3d 20  zMod);.    rc = 
2de86 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
2de87 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
2de88 76 74 61 62 43 61 6c 6c 43 6f 6e 73 74 72 75 63  vtabCallConstruc
2de89 74 6f 72 28 64 62 2c 20 70 54 61 62 2c 20 70 4d  tor(db, pTab, pM
2de8a 6f 64 2c 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c  od, pMod->pModul
2de8b 65 2d 3e 78 43 72 65 61 74 65 2c 20 70 7a 45 72  e->xCreate, pzEr
2de8c 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75  r);.  }..  /* Ju
2de8d 73 74 69 66 69 63 61 74 69 6f 6e 20 6f 66 20 41  stification of A
2de8e 4c 57 41 59 53 28 29 3a 20 20 54 68 65 20 78 43  LWAYS():  The xC
2de8f 6f 6e 73 74 72 75 63 74 6f 72 20 6d 65 74 68 6f  onstructor metho
2de90 64 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f  d is required to
2de91 0a 20 20 2a 2a 20 63 72 65 61 74 65 20 61 20 76  .  ** create a v
2de92 61 6c 69 64 20 73 71 6c 69 74 65 33 5f 76 74 61  alid sqlite3_vta
2de93 62 20 69 66 20 69 74 20 72 65 74 75 72 6e 73 20  b if it returns 
2de94 53 51 4c 49 54 45 5f 4f 4b 2e 20 2a 2f 0a 20 20  SQLITE_OK. */.  
2de95 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
2de96 4b 20 26 26 20 41 4c 57 41 59 53 28 73 71 6c 69  K && ALWAYS(sqli
2de97 74 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c  te3GetVTable(db,
2de98 20 70 54 61 62 29 29 20 29 7b 0a 20 20 20 20 20   pTab)) ){.     
2de99 20 72 63 20 3d 20 61 64 64 54 6f 56 54 72 61 6e   rc = addToVTran
2de9a 73 28 64 62 2c 20 73 71 6c 69 74 65 33 47 65 74  s(db, sqlite3Get
2de9b 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29  VTable(db, pTab)
2de9c 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  );.  }..  return
2de9d 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
2de9e 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
2de9f 73 65 64 20 74 6f 20 73 65 74 20 74 68 65 20 73  sed to set the s
2dea0 63 68 65 6d 61 20 6f 66 20 61 20 76 69 72 74 75  chema of a virtu
2dea1 61 6c 20 74 61 62 6c 65 2e 20 20 49 74 20 69 73  al table.  It is
2dea2 20 6f 6e 6c 79 0a 2a 2a 20 76 61 6c 69 64 20 74   only.** valid t
2dea3 6f 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63  o call this func
2dea4 74 69 6f 6e 20 66 72 6f 6d 20 77 69 74 68 69 6e  tion from within
2dea5 20 74 68 65 20 78 43 72 65 61 74 65 28 29 20 6f   the xCreate() o
2dea6 72 20 78 43 6f 6e 6e 65 63 74 28 29 20 6f 66 20  r xConnect() of 
2dea7 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62  a.** virtual tab
2dea8 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51  le module..*/.SQ
2dea9 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2deaa 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61  ite3_declare_vta
2deab 62 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  b(sqlite3 *db, c
2deac 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 72 65 61  onst char *zCrea
2dead 74 65 54 61 62 6c 65 29 7b 0a 20 20 50 61 72 73  teTable){.  Pars
2deae 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20 20 69 6e  e *pParse;..  in
2deaf 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
2deb0 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  ;.  Table *pTab;
2deb1 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20  .  char *zErr = 
2deb2 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  0;..  sqlite3_mu
2deb3 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
2deb4 74 65 78 29 3b 0a 20 20 70 54 61 62 20 3d 20 64  tex);.  pTab = d
2deb5 62 2d 3e 70 56 54 61 62 3b 0a 20 20 69 66 28 20  b->pVTab;.  if( 
2deb6 21 70 54 61 62 20 29 7b 0a 20 20 20 20 73 71 6c  !pTab ){.    sql
2deb7 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
2deb8 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 30 29 3b  LITE_MISUSE, 0);
2deb9 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
2deba 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
2debb 65 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ex);.    return 
2debc 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
2debd 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 70 54   }.  assert( (pT
2debe 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  ab->tabFlags & T
2debf 46 5f 56 69 72 74 75 61 6c 29 21 3d 30 20 29 3b  F_Virtual)!=0 );
2dec0 0a 0a 20 20 70 50 61 72 73 65 20 3d 20 73 71 6c  ..  pParse = sql
2dec1 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65  ite3StackAllocZe
2dec2 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70  ro(db, sizeof(*p
2dec3 50 61 72 73 65 29 29 3b 0a 20 20 69 66 28 20 70  Parse));.  if( p
2dec4 50 61 72 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20  Parse==0 ){.    
2dec5 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
2dec6 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  M;.  }else{.    
2dec7 70 50 61 72 73 65 2d 3e 64 65 63 6c 61 72 65 56  pParse->declareV
2dec8 74 61 62 20 3d 20 31 3b 0a 20 20 20 20 70 50 61  tab = 1;.    pPa
2dec9 72 73 65 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20  rse->db = db;.  
2deca 0a 20 20 20 20 69 66 28 20 0a 20 20 20 20 20 20  .    if( .      
2decb 20 20 53 51 4c 49 54 45 5f 4f 4b 20 3d 3d 20 73    SQLITE_OK == s
2decc 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 28  qlite3RunParser(
2decd 70 50 61 72 73 65 2c 20 7a 43 72 65 61 74 65 54  pParse, zCreateT
2dece 61 62 6c 65 2c 20 26 7a 45 72 72 29 20 26 26 20  able, &zErr) && 
2decf 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
2ded0 3e 70 4e 65 77 54 61 62 6c 65 20 26 26 20 0a 20  >pNewTable && . 
2ded1 20 20 20 20 20 20 20 21 70 50 61 72 73 65 2d 3e         !pParse->
2ded2 70 4e 65 77 54 61 62 6c 65 2d 3e 70 53 65 6c 65  pNewTable->pSele
2ded3 63 74 20 26 26 20 0a 20 20 20 20 20 20 20 20 28  ct && .        (
2ded4 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
2ded5 65 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46  e->tabFlags & TF
2ded6 5f 56 69 72 74 75 61 6c 29 3d 3d 30 0a 20 20 20  _Virtual)==0.   
2ded7 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 70   ){.      if( !p
2ded8 54 61 62 2d 3e 61 43 6f 6c 20 29 7b 0a 20 20 20  Tab->aCol ){.   
2ded9 20 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 20       pTab->aCol 
2deda 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  = pParse->pNewTa
2dedb 62 6c 65 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20  ble->aCol;.     
2dedc 20 20 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20     pTab->nCol = 
2dedd 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
2dede 65 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 20  e->nCol;.       
2dedf 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
2dee0 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20  le->nCol = 0;.  
2dee1 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 70 4e        pParse->pN
2dee2 65 77 54 61 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20  ewTable->aCol = 
2dee3 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
2dee4 20 64 62 2d 3e 70 56 54 61 62 20 3d 20 30 3b 0a   db->pVTab = 0;.
2dee5 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20      } else {.   
2dee6 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
2dee7 64 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  db, SQLITE_ERROR
2dee8 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 73  , zErr);.      s
2dee9 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2deea 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 72 63   zErr);.      rc
2deeb 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
2deec 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73  .    }.    pPars
2deed 65 2d 3e 64 65 63 6c 61 72 65 56 74 61 62 20 3d  e->declareVtab =
2deee 20 30 3b 0a 20 20 0a 20 20 20 20 69 66 28 20 70   0;.  .    if( p
2deef 50 61 72 73 65 2d 3e 70 56 64 62 65 20 29 7b 0a  Parse->pVdbe ){.
2def0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2def1 65 46 69 6e 61 6c 69 7a 65 28 70 50 61 72 73 65  eFinalize(pParse
2def2 2d 3e 70 56 64 62 65 29 3b 0a 20 20 20 20 7d 0a  ->pVdbe);.    }.
2def3 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
2def4 65 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 70  eTable(pParse->p
2def5 4e 65 77 54 61 62 6c 65 29 3b 0a 20 20 20 20 73  NewTable);.    s
2def6 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28  qlite3StackFree(
2def7 64 62 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 7d  db, pParse);.  }
2def8 0a 0a 20 20 61 73 73 65 72 74 28 20 28 72 63 26  ..  assert( (rc&
2def9 30 78 66 66 29 3d 3d 72 63 20 29 3b 0a 20 20 72  0xff)==rc );.  r
2defa 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
2defb 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71  it(db, rc);.  sq
2defc 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
2defd 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
2defe 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
2deff 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
2df00 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 79 20  n is invoked by 
2df01 74 68 65 20 76 64 62 65 20 74 6f 20 63 61 6c 6c  the vdbe to call
2df02 20 74 68 65 20 78 44 65 73 74 72 6f 79 20 6d 65   the xDestroy me
2df03 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20 76  thod.** of the v
2df04 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6e 61 6d  irtual table nam
2df05 65 64 20 7a 54 61 62 20 69 6e 20 64 61 74 61 62  ed zTab in datab
2df06 61 73 65 20 69 44 62 2e 20 54 68 69 73 20 6f 63  ase iDb. This oc
2df07 63 75 72 73 0a 2a 2a 20 77 68 65 6e 20 61 20 44  curs.** when a D
2df08 52 4f 50 20 54 41 42 4c 45 20 69 73 20 6d 65 6e  ROP TABLE is men
2df09 74 69 6f 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  tioned..**.** Th
2df0a 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d  is call is a no-
2df0b 6f 70 20 69 66 20 7a 54 61 62 20 69 73 20 6e 6f  op if zTab is no
2df0c 74 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  t a virtual tabl
2df0d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
2df0e 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2df0f 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 28  VtabCallDestroy(
2df10 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
2df11 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   iDb, const char
2df12 20 2a 7a 54 61 62 29 7b 0a 20 20 69 6e 74 20 72   *zTab){.  int r
2df13 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
2df14 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 0a 20   Table *pTab;.. 
2df15 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46   pTab = sqlite3F
2df16 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61  indTable(db, zTa
2df17 62 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  b, db->aDb[iDb].
2df18 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 41 4c  zName);.  if( AL
2df19 57 41 59 53 28 70 54 61 62 21 3d 30 20 26 26 20  WAYS(pTab!=0 && 
2df1a 70 54 61 62 2d 3e 70 56 54 61 62 6c 65 21 3d 30  pTab->pVTable!=0
2df1b 29 20 29 7b 0a 20 20 20 20 56 54 61 62 6c 65 20  ) ){.    VTable 
2df1c 2a 70 20 3d 20 76 74 61 62 44 69 73 63 6f 6e 6e  *p = vtabDisconn
2df1d 65 63 74 41 6c 6c 28 64 62 2c 20 70 54 61 62 29  ectAll(db, pTab)
2df1e 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ;..    rc = sqli
2df1f 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
2df20 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63  ;.    assert( rc
2df21 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  ==SQLITE_OK );. 
2df22 20 20 20 72 63 20 3d 20 70 2d 3e 70 4d 6f 64 2d     rc = p->pMod-
2df23 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 65 73 74 72  >pModule->xDestr
2df24 6f 79 28 70 2d 3e 70 56 74 61 62 29 3b 0a 20 20  oy(p->pVtab);.  
2df25 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
2df26 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 0a 20 20  afetyOn(db);..  
2df27 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20    /* Remove the 
2df28 73 71 6c 69 74 65 33 5f 76 74 61 62 2a 20 66 72  sqlite3_vtab* fr
2df29 6f 6d 20 74 68 65 20 61 56 54 72 61 6e 73 5b 5d  om the aVTrans[]
2df2a 20 61 72 72 61 79 2c 20 69 66 20 61 70 70 6c 69   array, if appli
2df2b 63 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 66 28  cable */.    if(
2df2c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
2df2d 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
2df2e 70 54 61 62 2d 3e 70 56 54 61 62 6c 65 3d 3d 70  pTab->pVTable==p
2df2f 20 26 26 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20   && p->pNext==0 
2df30 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 56 74 61  );.      p->pVta
2df31 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 54 61  b = 0;.      pTa
2df32 62 2d 3e 70 56 54 61 62 6c 65 20 3d 20 30 3b 0a  b->pVTable = 0;.
2df33 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61        sqlite3Vta
2df34 62 55 6e 6c 6f 63 6b 28 70 29 3b 0a 20 20 20 20  bUnlock(p);.    
2df35 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
2df36 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
2df37 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 76 6f 6b  s function invok
2df38 65 73 20 65 69 74 68 65 72 20 74 68 65 20 78 52  es either the xR
2df39 6f 6c 6c 62 61 63 6b 20 6f 72 20 78 43 6f 6d 6d  ollback or xComm
2df3a 69 74 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20  it method.** of 
2df3b 65 61 63 68 20 6f 66 20 74 68 65 20 76 69 72 74  each of the virt
2df3c 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68  ual tables in th
2df3d 65 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e  e sqlite3.aVTran
2df3e 73 20 61 72 72 61 79 2e 20 54 68 65 20 6d 65 74  s array. The met
2df3f 68 6f 64 0a 2a 2a 20 63 61 6c 6c 65 64 20 69 73  hod.** called is
2df40 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74   identified by t
2df41 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
2df42 6e 74 2c 20 22 6f 66 66 73 65 74 22 2c 20 77 68  nt, "offset", wh
2df43 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 6f 66  ich is.** the of
2df44 66 73 65 74 20 6f 66 20 74 68 65 20 6d 65 74 68  fset of the meth
2df45 6f 64 20 74 6f 20 63 61 6c 6c 20 69 6e 20 74 68  od to call in th
2df46 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  e sqlite3_module
2df47 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a   structure..**.*
2df48 2a 20 54 68 65 20 61 72 72 61 79 20 69 73 20 63  * The array is c
2df49 6c 65 61 72 65 64 20 61 66 74 65 72 20 69 6e 76  leared after inv
2df4a 6f 6b 69 6e 67 20 74 68 65 20 63 61 6c 6c 62 61  oking the callba
2df4b 63 6b 73 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20  cks. .*/.static 
2df4c 76 6f 69 64 20 63 61 6c 6c 46 69 6e 61 6c 69 73  void callFinalis
2df4d 65 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  er(sqlite3 *db, 
2df4e 69 6e 74 20 6f 66 66 73 65 74 29 7b 0a 20 20 69  int offset){.  i
2df4f 6e 74 20 69 3b 0a 20 20 69 66 28 20 64 62 2d 3e  nt i;.  if( db->
2df50 61 56 54 72 61 6e 73 20 29 7b 0a 20 20 20 20 66  aVTrans ){.    f
2df51 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 56  or(i=0; i<db->nV
2df52 54 72 61 6e 73 3b 20 69 2b 2b 29 7b 0a 20 20 20  Trans; i++){.   
2df53 20 20 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62     VTable *pVTab
2df54 20 3d 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69   = db->aVTrans[i
2df55 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ];.      sqlite3
2df56 5f 76 74 61 62 20 2a 70 20 3d 20 70 56 54 61 62  _vtab *p = pVTab
2df57 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20 20 20 69  ->pVtab;.      i
2df58 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20  f( p ){.        
2df59 69 6e 74 20 28 2a 78 29 28 73 71 6c 69 74 65 33  int (*x)(sqlite3
2df5a 5f 76 74 61 62 20 2a 29 3b 0a 20 20 20 20 20 20  _vtab *);.      
2df5b 20 20 78 20 3d 20 2a 28 69 6e 74 20 28 2a 2a 29    x = *(int (**)
2df5c 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29  (sqlite3_vtab *)
2df5d 29 28 28 63 68 61 72 20 2a 29 70 2d 3e 70 4d 6f  )((char *)p->pMo
2df5e 64 75 6c 65 20 2b 20 6f 66 66 73 65 74 29 3b 0a  dule + offset);.
2df5f 20 20 20 20 20 20 20 20 69 66 28 20 78 20 29 20          if( x ) 
2df60 78 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  x(p);.      }.  
2df61 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 55      sqlite3VtabU
2df62 6e 6c 6f 63 6b 28 70 56 54 61 62 29 3b 0a 20 20  nlock(pVTab);.  
2df63 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44    }.    sqlite3D
2df64 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 56  bFree(db, db->aV
2df65 54 72 61 6e 73 29 3b 0a 20 20 20 20 64 62 2d 3e  Trans);.    db->
2df66 6e 56 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20 20  nVTrans = 0;.   
2df67 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20 30   db->aVTrans = 0
2df68 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ;.  }.}../*.** I
2df69 6e 76 6f 6b 65 20 74 68 65 20 78 53 79 6e 63 20  nvoke the xSync 
2df6a 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c 20 76 69  method of all vi
2df6b 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 20  rtual tables in 
2df6c 74 68 65 20 73 71 6c 69 74 65 33 2e 61 56 54 72  the sqlite3.aVTr
2df6d 61 6e 73 0a 2a 2a 20 61 72 72 61 79 2e 20 52 65  ans.** array. Re
2df6e 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63  turn the error c
2df6f 6f 64 65 20 66 6f 72 20 74 68 65 20 66 69 72 73  ode for the firs
2df70 74 20 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63  t error that occ
2df71 75 72 73 2c 20 6f 72 0a 2a 2a 20 53 51 4c 49 54  urs, or.** SQLIT
2df72 45 5f 4f 4b 20 69 66 20 61 6c 6c 20 78 53 79 6e  E_OK if all xSyn
2df73 63 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65  c operations are
2df74 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2a 0a   successful..**.
2df75 2a 2a 20 53 65 74 20 2a 70 7a 45 72 72 6d 73 67  ** Set *pzErrmsg
2df76 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20 62   to point to a b
2df77 75 66 66 65 72 20 74 68 61 74 20 73 68 6f 75 6c  uffer that shoul
2df78 64 20 62 65 20 72 65 6c 65 61 73 65 64 20 75 73  d be released us
2df79 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 33 44  ing .** sqlite3D
2df7a 62 46 72 65 65 28 29 20 63 6f 6e 74 61 69 6e 69  bFree() containi
2df7b 6e 67 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  ng an error mess
2df7c 61 67 65 2c 20 69 66 20 6f 6e 65 20 69 73 20 61  age, if one is a
2df7d 76 61 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  vailable..*/.SQL
2df7e 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2df7f 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28  sqlite3VtabSync(
2df80 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61  sqlite3 *db, cha
2df81 72 20 2a 2a 70 7a 45 72 72 6d 73 67 29 7b 0a 20  r **pzErrmsg){. 
2df82 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63   int i;.  int rc
2df83 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
2df84 69 6e 74 20 72 63 73 61 66 65 74 79 3b 0a 20 20  int rcsafety;.  
2df85 56 54 61 62 6c 65 20 2a 2a 61 56 54 72 61 6e 73  VTable **aVTrans
2df86 20 3d 20 64 62 2d 3e 61 56 54 72 61 6e 73 3b 0a   = db->aVTrans;.
2df87 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53  .  rc = sqlite3S
2df88 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20  afetyOff(db);.  
2df89 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20 30 3b  db->aVTrans = 0;
2df8a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d  .  for(i=0; rc==
2df8b 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64  SQLITE_OK && i<d
2df8c 62 2d 3e 6e 56 54 72 61 6e 73 3b 20 69 2b 2b 29  b->nVTrans; i++)
2df8d 7b 0a 20 20 20 20 69 6e 74 20 28 2a 78 29 28 73  {.    int (*x)(s
2df8e 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 3b 0a  qlite3_vtab *);.
2df8f 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
2df90 20 2a 70 56 74 61 62 20 3d 20 61 56 54 72 61 6e   *pVtab = aVTran
2df91 73 5b 69 5d 2d 3e 70 56 74 61 62 3b 0a 20 20 20  s[i]->pVtab;.   
2df92 20 69 66 28 20 70 56 74 61 62 20 26 26 20 28 78   if( pVtab && (x
2df93 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c   = pVtab->pModul
2df94 65 2d 3e 78 53 79 6e 63 29 21 3d 30 20 29 7b 0a  e->xSync)!=0 ){.
2df95 20 20 20 20 20 20 72 63 20 3d 20 78 28 70 56 74        rc = x(pVt
2df96 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ab);.      sqlit
2df97 65 33 44 62 46 72 65 65 28 64 62 2c 20 2a 70 7a  e3DbFree(db, *pz
2df98 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20 2a  Errmsg);.      *
2df99 70 7a 45 72 72 6d 73 67 20 3d 20 70 56 74 61 62  pzErrmsg = pVtab
2df9a 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 20  ->zErrMsg;.     
2df9b 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20   pVtab->zErrMsg 
2df9c 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 0;.    }.  }. 
2df9d 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20 61   db->aVTrans = a
2df9e 56 54 72 61 6e 73 3b 0a 20 20 72 63 73 61 66 65  VTrans;.  rcsafe
2df9f 74 79 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65  ty = sqlite3Safe
2dfa0 74 79 4f 6e 28 64 62 29 3b 0a 0a 20 20 69 66 28  tyOn(db);..  if(
2dfa1 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
2dfa2 7b 0a 20 20 20 20 72 63 20 3d 20 72 63 73 61 66  {.    rc = rcsaf
2dfa3 65 74 79 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ety;.  }.  retur
2dfa4 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  n rc;.}../*.** I
2dfa5 6e 76 6f 6b 65 20 74 68 65 20 78 52 6f 6c 6c 62  nvoke the xRollb
2dfa6 61 63 6b 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c  ack method of al
2dfa7 6c 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  l virtual tables
2dfa8 20 69 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69   in the .** sqli
2dfa9 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61  te3.aVTrans arra
2dfaa 79 2e 20 54 68 65 6e 20 63 6c 65 61 72 20 74 68  y. Then clear th
2dfab 65 20 61 72 72 61 79 20 69 74 73 65 6c 66 2e 0a  e array itself..
2dfac 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2dfad 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61  E int sqlite3Vta
2dfae 62 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74 65  bRollback(sqlite
2dfaf 33 20 2a 64 62 29 7b 0a 20 20 63 61 6c 6c 46 69  3 *db){.  callFi
2dfb0 6e 61 6c 69 73 65 72 28 64 62 2c 20 6f 66 66 73  naliser(db, offs
2dfb1 65 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d 6f 64  etof(sqlite3_mod
2dfb2 75 6c 65 2c 78 52 6f 6c 6c 62 61 63 6b 29 29 3b  ule,xRollback));
2dfb3 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
2dfb4 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  _OK;.}../*.** In
2dfb5 76 6f 6b 65 20 74 68 65 20 78 43 6f 6d 6d 69 74  voke the xCommit
2dfb6 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c 20 76   method of all v
2dfb7 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e  irtual tables in
2dfb8 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33   the .** sqlite3
2dfb9 2e 61 56 54 72 61 6e 73 20 61 72 72 61 79 2e 20  .aVTrans array. 
2dfba 54 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20 61  Then clear the a
2dfbb 72 72 61 79 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a  rray itself..*/.
2dfbc 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2dfbd 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f  nt sqlite3VtabCo
2dfbe 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62  mmit(sqlite3 *db
2dfbf 29 7b 0a 20 20 63 61 6c 6c 46 69 6e 61 6c 69 73  ){.  callFinalis
2dfc0 65 72 28 64 62 2c 20 6f 66 66 73 65 74 6f 66 28  er(db, offsetof(
2dfc1 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2c 78  sqlite3_module,x
2dfc2 43 6f 6d 6d 69 74 29 29 3b 0a 20 20 72 65 74 75  Commit));.  retu
2dfc3 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
2dfc4 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 69  ./*.** If the vi
2dfc5 72 74 75 61 6c 20 74 61 62 6c 65 20 70 56 74 61  rtual table pVta
2dfc6 62 20 73 75 70 70 6f 72 74 73 20 74 68 65 20 74  b supports the t
2dfc7 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 74 65 72  ransaction inter
2dfc8 66 61 63 65 0a 2a 2a 20 28 78 42 65 67 69 6e 2f  face.** (xBegin/
2dfc9 78 52 6f 6c 6c 62 61 63 6b 2f 78 43 6f 6d 6d 69  xRollback/xCommi
2dfca 74 20 61 6e 64 20 6f 70 74 69 6f 6e 61 6c 6c 79  t and optionally
2dfcb 20 78 53 79 6e 63 29 20 61 6e 64 20 61 20 74 72   xSync) and a tr
2dfcc 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20  ansaction is.** 
2dfcd 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 6f 70  not currently op
2dfce 65 6e 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78  en, invoke the x
2dfcf 42 65 67 69 6e 20 6d 65 74 68 6f 64 20 6e 6f 77  Begin method now
2dfd0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 78  ..**.** If the x
2dfd1 42 65 67 69 6e 20 63 61 6c 6c 20 69 73 20 73 75  Begin call is su
2dfd2 63 63 65 73 73 66 75 6c 2c 20 70 6c 61 63 65 20  ccessful, place 
2dfd3 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62  the sqlite3_vtab
2dfd4 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 69 6e 20 74   pointer.** in t
2dfd5 68 65 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61  he sqlite3.aVTra
2dfd6 6e 73 20 61 72 72 61 79 2e 0a 2a 2f 0a 53 51 4c  ns array..*/.SQL
2dfd7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2dfd8 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e  sqlite3VtabBegin
2dfd9 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 56 54  (sqlite3 *db, VT
2dfda 61 62 6c 65 20 2a 70 56 54 61 62 29 7b 0a 20 20  able *pVTab){.  
2dfdb 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
2dfdc 4f 4b 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  OK;.  const sqli
2dfdd 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
2dfde 75 6c 65 3b 0a 0a 20 20 2f 2a 20 53 70 65 63 69  ule;..  /* Speci
2dfdf 61 6c 20 63 61 73 65 3a 20 49 66 20 64 62 2d 3e  al case: If db->
2dfe0 61 56 54 72 61 6e 73 20 69 73 20 4e 55 4c 4c 20  aVTrans is NULL 
2dfe1 61 6e 64 20 64 62 2d 3e 6e 56 54 72 61 6e 73 20  and db->nVTrans 
2dfe2 69 73 20 67 72 65 61 74 65 72 0a 20 20 2a 2a 20  is greater.  ** 
2dfe3 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20  than zero, then 
2dfe4 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
2dfe5 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72   being called fr
2dfe6 6f 6d 20 77 69 74 68 69 6e 20 61 0a 20 20 2a 2a  om within a.  **
2dfe7 20 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c 65 20   virtual module 
2dfe8 78 53 79 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b  xSync() callback
2dfe9 2e 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20  . It is illegal 
2dfea 74 6f 20 77 72 69 74 65 20 74 6f 20 0a 20 20 2a  to write to .  *
2dfeb 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c 65  * virtual module
2dfec 20 74 61 62 6c 65 73 20 69 6e 20 74 68 69 73 20   tables in this 
2dfed 63 61 73 65 2c 20 73 6f 20 72 65 74 75 72 6e 20  case, so return 
2dfee 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 20  SQLITE_LOCKED.. 
2dfef 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
2dff0 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20  3VtabInSync(db) 
2dff1 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
2dff2 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 7d  LITE_LOCKED;.  }
2dff3 0a 20 20 69 66 28 20 21 70 56 54 61 62 20 29 7b  .  if( !pVTab ){
2dff4 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2dff5 54 45 5f 4f 4b 3b 0a 20 20 7d 20 0a 20 20 70 4d  TE_OK;.  } .  pM
2dff6 6f 64 75 6c 65 20 3d 20 70 56 54 61 62 2d 3e 70  odule = pVTab->p
2dff7 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a  Vtab->pModule;..
2dff8 20 20 69 66 28 20 70 4d 6f 64 75 6c 65 2d 3e 78    if( pModule->x
2dff9 42 65 67 69 6e 20 29 7b 0a 20 20 20 20 69 6e 74  Begin ){.    int
2dffa 20 69 3b 0a 0a 0a 20 20 20 20 2f 2a 20 49 66 20   i;...    /* If 
2dffb 70 56 74 61 62 20 69 73 20 61 6c 72 65 61 64 79  pVtab is already
2dffc 20 69 6e 20 74 68 65 20 61 56 54 72 61 6e 73 20   in the aVTrans 
2dffd 61 72 72 61 79 2c 20 72 65 74 75 72 6e 20 65 61  array, return ea
2dffe 72 6c 79 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69  rly */.    for(i
2dfff 3d 30 3b 20 69 3c 64 62 2d 3e 6e 56 54 72 61 6e  =0; i<db->nVTran
2e000 73 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  s; i++){.      i
2e001 66 28 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69  f( db->aVTrans[i
2e002 5d 3d 3d 70 56 54 61 62 20 29 7b 0a 20 20 20 20  ]==pVTab ){.    
2e003 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2e004 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  E_OK;.      }.  
2e005 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 6e 76 6f    }..    /* Invo
2e006 6b 65 20 74 68 65 20 78 42 65 67 69 6e 20 6d 65  ke the xBegin me
2e007 74 68 6f 64 20 2a 2f 0a 20 20 20 20 72 63 20 3d  thod */.    rc =
2e008 20 70 4d 6f 64 75 6c 65 2d 3e 78 42 65 67 69 6e   pModule->xBegin
2e009 28 70 56 54 61 62 2d 3e 70 56 74 61 62 29 3b 0a  (pVTab->pVtab);.
2e00a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
2e00b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
2e00c 63 20 3d 20 61 64 64 54 6f 56 54 72 61 6e 73 28  c = addToVTrans(
2e00d 64 62 2c 20 70 56 54 61 62 29 3b 0a 20 20 20 20  db, pVTab);.    
2e00e 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
2e00f 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  c;.}../*.** The 
2e010 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
2e011 28 70 44 65 66 29 20 69 73 20 61 20 66 75 6e 63  (pDef) is a func
2e012 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74  tion implementat
2e013 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 73 65 63  ion.  The.** sec
2e014 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 28 70  ond parameter (p
2e015 45 78 70 72 29 20 69 73 20 74 68 65 20 66 69 72  Expr) is the fir
2e016 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  st argument to t
2e017 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a  his function..**
2e018 20 49 66 20 70 45 78 70 72 20 69 73 20 61 20 63   If pExpr is a c
2e019 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72 74 75  olumn in a virtu
2e01a 61 6c 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 6c  al table, then l
2e01b 65 74 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a  et the virtual.*
2e01c 2a 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e  * table implemen
2e01d 74 61 74 69 6f 6e 20 68 61 76 65 20 61 6e 20 6f  tation have an o
2e01e 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 6f 76  pportunity to ov
2e01f 65 72 6c 6f 61 64 20 74 68 65 20 66 75 6e 63 74  erload the funct
2e020 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ion..**.** This 
2e021 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
2e022 74 6f 20 61 6c 6c 6f 77 20 76 69 72 74 75 61 6c  to allow virtual
2e023 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74   table implement
2e024 61 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 6f 76 65  ations to.** ove
2e025 72 6c 6f 61 64 20 4d 41 54 43 48 2c 20 4c 49 4b  rload MATCH, LIK
2e026 45 2c 20 47 4c 4f 42 2c 20 61 6e 64 20 52 45 47  E, GLOB, and REG
2e027 45 58 50 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a  EXP operators..*
2e028 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 65 69 74 68  *.** Return eith
2e029 65 72 20 74 68 65 20 70 44 65 66 20 61 72 67 75  er the pDef argu
2e02a 6d 65 6e 74 20 28 69 6e 64 69 63 61 74 69 6e 67  ment (indicating
2e02b 20 6e 6f 20 63 68 61 6e 67 65 29 20 6f 72 20 61   no change) or a
2e02c 20 0a 2a 2a 20 6e 65 77 20 46 75 6e 63 44 65 66   .** new FuncDef
2e02d 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20   structure that 
2e02e 69 73 20 6d 61 72 6b 65 64 20 61 73 20 65 70 68  is marked as eph
2e02f 65 6d 65 72 61 6c 20 75 73 69 6e 67 20 74 68 65  emeral using the
2e030 0a 2a 2a 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  .** SQLITE_FUNC_
2e031 45 50 48 45 4d 20 66 6c 61 67 2e 0a 2a 2f 0a 53  EPHEM flag..*/.S
2e032 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 75  QLITE_PRIVATE Fu
2e033 6e 63 44 65 66 20 2a 73 71 6c 69 74 65 33 56 74  ncDef *sqlite3Vt
2e034 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69  abOverloadFuncti
2e035 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  on(.  sqlite3 *d
2e036 62 2c 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73  b,    /* Databas
2e037 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f 72  e connection for
2e038 20 72 65 70 6f 72 74 69 6e 67 20 6d 61 6c 6c 6f   reporting mallo
2e039 63 20 70 72 6f 62 6c 65 6d 73 20 2a 2f 0a 20 20  c problems */.  
2e03a 46 75 6e 63 44 65 66 20 2a 70 44 65 66 2c 20 20  FuncDef *pDef,  
2e03b 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 70  /* Function to p
2e03c 6f 73 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61 64  ossibly overload
2e03d 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20   */.  int nArg, 
2e03e 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2e03f 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  of arguments to 
2e040 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  the function */.
2e041 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 20    Expr *pExpr   
2e042 20 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d    /* First argum
2e043 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74  ent to the funct
2e044 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c  ion */.){.  Tabl
2e045 65 20 2a 70 54 61 62 3b 0a 20 20 73 71 6c 69 74  e *pTab;.  sqlit
2e046 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a  e3_vtab *pVtab;.
2e047 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65    sqlite3_module
2e048 20 2a 70 4d 6f 64 3b 0a 20 20 76 6f 69 64 20 28   *pMod;.  void (
2e049 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f  *xFunc)(sqlite3_
2e04a 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
2e04b 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 20 3d 20  ite3_value**) = 
2e04c 30 3b 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 20  0;.  void *pArg 
2e04d 3d 20 30 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a  = 0;.  FuncDef *
2e04e 70 4e 65 77 3b 0a 20 20 69 6e 74 20 72 63 20 3d  pNew;.  int rc =
2e04f 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 77   0;.  char *zLow
2e050 65 72 4e 61 6d 65 3b 0a 20 20 75 6e 73 69 67 6e  erName;.  unsign
2e051 65 64 20 63 68 61 72 20 2a 7a 3b 0a 0a 0a 20 20  ed char *z;...  
2e052 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  /* Check to see 
2e053 74 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64  the left operand
2e054 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20   is a column in 
2e055 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
2e056 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  */.  if( NEVER(p
2e057 45 78 70 72 3d 3d 30 29 20 29 20 72 65 74 75 72  Expr==0) ) retur
2e058 6e 20 70 44 65 66 3b 0a 20 20 69 66 28 20 70 45  n pDef;.  if( pE
2e059 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55  xpr->op!=TK_COLU
2e05a 4d 4e 20 29 20 72 65 74 75 72 6e 20 70 44 65 66  MN ) return pDef
2e05b 3b 0a 20 20 70 54 61 62 20 3d 20 70 45 78 70 72  ;.  pTab = pExpr
2e05c 2d 3e 70 54 61 62 3b 0a 20 20 69 66 28 20 4e 45  ->pTab;.  if( NE
2e05d 56 45 52 28 70 54 61 62 3d 3d 30 29 20 29 20 72  VER(pTab==0) ) r
2e05e 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 20 69 66  eturn pDef;.  if
2e05f 28 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67  ( (pTab->tabFlag
2e060 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29 3d  s & TF_Virtual)=
2e061 3d 30 20 29 20 72 65 74 75 72 6e 20 70 44 65 66  =0 ) return pDef
2e062 3b 0a 20 20 70 56 74 61 62 20 3d 20 73 71 6c 69  ;.  pVtab = sqli
2e063 74 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c  te3GetVTable(db,
2e064 20 70 54 61 62 29 2d 3e 70 56 74 61 62 3b 0a 20   pTab)->pVtab;. 
2e065 20 61 73 73 65 72 74 28 20 70 56 74 61 62 21 3d   assert( pVtab!=
2e066 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
2e067 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 21 3d 30  Vtab->pModule!=0
2e068 20 29 3b 0a 20 20 70 4d 6f 64 20 3d 20 28 73 71   );.  pMod = (sq
2e069 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 70  lite3_module *)p
2e06a 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20  Vtab->pModule;. 
2e06b 20 69 66 28 20 70 4d 6f 64 2d 3e 78 46 69 6e 64   if( pMod->xFind
2e06c 46 75 6e 63 74 69 6f 6e 3d 3d 30 20 29 20 72 65  Function==0 ) re
2e06d 74 75 72 6e 20 70 44 65 66 3b 0a 20 0a 20 20 2f  turn pDef;. .  /
2e06e 2a 20 43 61 6c 6c 20 74 68 65 20 78 46 69 6e 64  * Call the xFind
2e06f 46 75 6e 63 74 69 6f 6e 20 6d 65 74 68 6f 64 20  Function method 
2e070 6f 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  on the virtual t
2e071 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  able implementat
2e072 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20 73 65 65 20  ion.  ** to see 
2e073 69 66 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  if the implement
2e074 61 74 69 6f 6e 20 77 61 6e 74 73 20 74 6f 20 6f  ation wants to o
2e075 76 65 72 6c 6f 61 64 20 74 68 69 73 20 66 75 6e  verload this fun
2e076 63 74 69 6f 6e 20 0a 20 20 2a 2f 0a 20 20 7a 4c  ction .  */.  zL
2e077 6f 77 65 72 4e 61 6d 65 20 3d 20 73 71 6c 69 74  owerName = sqlit
2e078 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70  e3DbStrDup(db, p
2e079 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69  Def->zName);.  i
2e07a 66 28 20 7a 4c 6f 77 65 72 4e 61 6d 65 20 29 7b  f( zLowerName ){
2e07b 0a 20 20 20 20 66 6f 72 28 7a 3d 28 75 6e 73 69  .    for(z=(unsi
2e07c 67 6e 65 64 20 63 68 61 72 2a 29 7a 4c 6f 77 65  gned char*)zLowe
2e07d 72 4e 61 6d 65 3b 20 2a 7a 3b 20 7a 2b 2b 29 7b  rName; *z; z++){
2e07e 0a 20 20 20 20 20 20 2a 7a 20 3d 20 73 71 6c 69  .      *z = sqli
2e07f 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
2e080 2a 7a 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  *z];.    }.    r
2e081 63 20 3d 20 70 4d 6f 64 2d 3e 78 46 69 6e 64 46  c = pMod->xFindF
2e082 75 6e 63 74 69 6f 6e 28 70 56 74 61 62 2c 20 6e  unction(pVtab, n
2e083 41 72 67 2c 20 7a 4c 6f 77 65 72 4e 61 6d 65 2c  Arg, zLowerName,
2e084 20 26 78 46 75 6e 63 2c 20 26 70 41 72 67 29 3b   &xFunc, &pArg);
2e085 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2e086 65 65 28 64 62 2c 20 7a 4c 6f 77 65 72 4e 61 6d  ee(db, zLowerNam
2e087 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  e);.  }.  if( rc
2e088 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
2e089 6e 20 70 44 65 66 3b 0a 20 20 7d 0a 0a 20 20 2f  n pDef;.  }..  /
2e08a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 65  * Create a new e
2e08b 70 68 65 6d 65 72 61 6c 20 66 75 6e 63 74 69 6f  phemeral functio
2e08c 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 66 6f 72  n definition for
2e08d 20 74 68 65 20 6f 76 65 72 6c 6f 61 64 65 64 0a   the overloaded.
2e08e 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 2a 2f    ** function */
2e08f 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
2e090 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
2e091 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 0a  , sizeof(*pNew).
2e092 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e093 20 20 20 20 20 20 20 20 20 20 20 20 20 2b 20 73               + s
2e094 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70  qlite3Strlen30(p
2e095 44 65 66 2d 3e 7a 4e 61 6d 65 29 20 2b 20 31 29  Def->zName) + 1)
2e096 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20  ;.  if( pNew==0 
2e097 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 44  ){.    return pD
2e098 65 66 3b 0a 20 20 7d 0a 20 20 2a 70 4e 65 77 20  ef;.  }.  *pNew 
2e099 3d 20 2a 70 44 65 66 3b 0a 20 20 70 4e 65 77 2d  = *pDef;.  pNew-
2e09a 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 20 2a  >zName = (char *
2e09b 29 26 70 4e 65 77 5b 31 5d 3b 0a 20 20 6d 65 6d  )&pNew[1];.  mem
2e09c 63 70 79 28 70 4e 65 77 2d 3e 7a 4e 61 6d 65 2c  cpy(pNew->zName,
2e09d 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 73 71   pDef->zName, sq
2e09e 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 44  lite3Strlen30(pD
2e09f 65 66 2d 3e 7a 4e 61 6d 65 29 2b 31 29 3b 0a 20  ef->zName)+1);. 
2e0a0 20 70 4e 65 77 2d 3e 78 46 75 6e 63 20 3d 20 78   pNew->xFunc = x
2e0a1 46 75 6e 63 3b 0a 20 20 70 4e 65 77 2d 3e 70 55  Func;.  pNew->pU
2e0a2 73 65 72 44 61 74 61 20 3d 20 70 41 72 67 3b 0a  serData = pArg;.
2e0a3 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d    pNew->flags |=
2e0a4 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48   SQLITE_FUNC_EPH
2e0a5 45 4d 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65  EM;.  return pNe
2e0a6 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65  w;.}../*.** Make
2e0a7 20 73 75 72 65 20 76 69 72 74 75 61 6c 20 74 61   sure virtual ta
2e0a8 62 6c 65 20 70 54 61 62 20 69 73 20 63 6f 6e 74  ble pTab is cont
2e0a9 61 69 6e 65 64 20 69 6e 20 74 68 65 20 70 50 61  ained in the pPa
2e0aa 72 73 65 2d 3e 61 70 56 69 72 74 75 61 6c 4c 6f  rse->apVirtualLo
2e0ab 63 6b 5b 5d 0a 2a 2a 20 61 72 72 61 79 20 73 6f  ck[].** array so
2e0ac 20 74 68 61 74 20 61 6e 20 4f 50 5f 56 42 65 67   that an OP_VBeg
2e0ad 69 6e 20 77 69 6c 6c 20 67 65 74 20 67 65 6e 65  in will get gene
2e0ae 72 61 74 65 64 20 66 6f 72 20 69 74 2e 20 20 41  rated for it.  A
2e0af 64 64 20 70 54 61 62 20 74 6f 20 74 68 65 0a 2a  dd pTab to the.*
2e0b0 2a 20 61 72 72 61 79 20 69 66 20 69 74 20 69 73  * array if it is
2e0b1 20 6d 69 73 73 69 6e 67 2e 20 20 49 66 20 70 54   missing.  If pT
2e0b2 61 62 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e  ab is already in
2e0b3 20 74 68 65 20 61 72 72 61 79 2c 20 74 68 69 73   the array, this
2e0b4 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61   routine.** is a
2e0b5 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54   no-op..*/.SQLIT
2e0b6 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2e0b7 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57 72  qlite3VtabMakeWr
2e0b8 69 74 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50  itable(Parse *pP
2e0b9 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61  arse, Table *pTa
2e0ba 62 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 54 6f  b){.  Parse *pTo
2e0bb 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33  plevel = sqlite3
2e0bc 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50  ParseToplevel(pP
2e0bd 61 72 73 65 29 3b 0a 20 20 69 6e 74 20 69 2c 20  arse);.  int i, 
2e0be 6e 3b 0a 20 20 54 61 62 6c 65 20 2a 2a 61 70 56  n;.  Table **apV
2e0bf 74 61 62 4c 6f 63 6b 3b 0a 0a 20 20 61 73 73 65  tabLock;..  asse
2e0c0 72 74 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  rt( IsVirtual(pT
2e0c1 61 62 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30  ab) );.  for(i=0
2e0c2 3b 20 69 3c 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e  ; i<pToplevel->n
2e0c3 56 74 61 62 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a  VtabLock; i++){.
2e0c4 20 20 20 20 69 66 28 20 70 54 61 62 3d 3d 70 54      if( pTab==pT
2e0c5 6f 70 6c 65 76 65 6c 2d 3e 61 70 56 74 61 62 4c  oplevel->apVtabL
2e0c6 6f 63 6b 5b 69 5d 20 29 20 72 65 74 75 72 6e 3b  ock[i] ) return;
2e0c7 0a 20 20 7d 0a 20 20 6e 20 3d 20 28 70 54 6f 70  .  }.  n = (pTop
2e0c8 6c 65 76 65 6c 2d 3e 6e 56 74 61 62 4c 6f 63 6b  level->nVtabLock
2e0c9 2b 31 29 2a 73 69 7a 65 6f 66 28 70 54 6f 70 6c  +1)*sizeof(pTopl
2e0ca 65 76 65 6c 2d 3e 61 70 56 74 61 62 4c 6f 63 6b  evel->apVtabLock
2e0cb 5b 30 5d 29 3b 0a 20 20 61 70 56 74 61 62 4c 6f  [0]);.  apVtabLo
2e0cc 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  ck = sqlite3_rea
2e0cd 6c 6c 6f 63 28 70 54 6f 70 6c 65 76 65 6c 2d 3e  lloc(pToplevel->
2e0ce 61 70 56 74 61 62 4c 6f 63 6b 2c 20 6e 29 3b 0a  apVtabLock, n);.
2e0cf 20 20 69 66 28 20 61 70 56 74 61 62 4c 6f 63 6b    if( apVtabLock
2e0d0 20 29 7b 0a 20 20 20 20 70 54 6f 70 6c 65 76 65   ){.    pTopleve
2e0d1 6c 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 20 3d 20  l->apVtabLock = 
2e0d2 61 70 56 74 61 62 4c 6f 63 6b 3b 0a 20 20 20 20  apVtabLock;.    
2e0d3 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 70 56 74 61  pToplevel->apVta
2e0d4 62 4c 6f 63 6b 5b 70 54 6f 70 6c 65 76 65 6c 2d  bLock[pToplevel-
2e0d5 3e 6e 56 74 61 62 4c 6f 63 6b 2b 2b 5d 20 3d 20  >nVtabLock++] = 
2e0d6 70 54 61 62 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  pTab;.  }else{. 
2e0d7 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 64 62     pToplevel->db
2e0d8 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
2e0d9 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69   1;.  }.}..#endi
2e0da 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2e0db 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
2e0dc 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
2e0dd 2a 20 45 6e 64 20 6f 66 20 76 74 61 62 2e 63 20  * End of vtab.c 
2e0de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e0df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e0e1 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
2e0e2 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 77 68 65  * Begin file whe
2e0e3 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  re.c ***********
2e0e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e0e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e0e6 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
2e0e7 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
2e0e8 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
2e0e9 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
2e0ea 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
2e0eb 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
2e0ec 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
2e0ed 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
2e0ee 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
2e0ef 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
2e0f0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
2e0f1 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
2e0f2 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
2e0f3 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
2e0f4 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
2e0f5 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
2e0f6 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
2e0f7 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
2e0f8 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
2e0f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e0fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e0fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e0fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e0fd 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
2e0fe 20 6d 6f 64 75 6c 65 20 63 6f 6e 74 61 69 6e 73   module contains
2e0ff 20 43 20 63 6f 64 65 20 74 68 61 74 20 67 65 6e   C code that gen
2e100 65 72 61 74 65 73 20 56 44 42 45 20 63 6f 64 65  erates VDBE code
2e101 20 75 73 65 64 20 74 6f 20 70 72 6f 63 65 73 73   used to process
2e102 0a 2a 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c  .** the WHERE cl
2e103 61 75 73 65 20 6f 66 20 53 51 4c 20 73 74 61 74  ause of SQL stat
2e104 65 6d 65 6e 74 73 2e 20 20 54 68 69 73 20 6d 6f  ements.  This mo
2e105 64 75 6c 65 20 69 73 20 72 65 73 70 6f 6e 73 69  dule is responsi
2e106 62 6c 65 20 66 6f 72 0a 2a 2a 20 67 65 6e 65 72  ble for.** gener
2e107 61 74 69 6e 67 20 74 68 65 20 63 6f 64 65 20 74  ating the code t
2e108 68 61 74 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67  hat loops throug
2e109 68 20 61 20 74 61 62 6c 65 20 6c 6f 6f 6b 69 6e  h a table lookin
2e10a 67 20 66 6f 72 20 61 70 70 6c 69 63 61 62 6c 65  g for applicable
2e10b 0a 2a 2a 20 72 6f 77 73 2e 20 20 49 6e 64 69 63  .** rows.  Indic
2e10c 65 73 20 61 72 65 20 73 65 6c 65 63 74 65 64 20  es are selected 
2e10d 61 6e 64 20 75 73 65 64 20 74 6f 20 73 70 65 65  and used to spee
2e10e 64 20 74 68 65 20 73 65 61 72 63 68 20 77 68 65  d the search whe
2e10f 6e 20 64 6f 69 6e 67 0a 2a 2a 20 73 6f 20 69 73  n doing.** so is
2e110 20 61 70 70 6c 69 63 61 62 6c 65 2e 20 20 42 65   applicable.  Be
2e111 63 61 75 73 65 20 74 68 69 73 20 6d 6f 64 75 6c  cause this modul
2e112 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65  e is responsible
2e113 20 66 6f 72 20 73 65 6c 65 63 74 69 6e 67 0a 2a   for selecting.*
2e114 2a 20 69 6e 64 69 63 65 73 2c 20 79 6f 75 20 6d  * indices, you m
2e115 69 67 68 74 20 61 6c 73 6f 20 74 68 69 6e 6b 20  ight also think 
2e116 6f 66 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61  of this module a
2e117 73 20 74 68 65 20 22 71 75 65 72 79 20 6f 70 74  s the "query opt
2e118 69 6d 69 7a 65 72 22 2e 0a 2a 2a 0a 2a 2a 20 24  imizer"..**.** $
2e119 49 64 3a 20 77 68 65 72 65 2e 63 2c 76 20 31 2e  Id: where.c,v 1.
2e11a 34 31 31 20 32 30 30 39 2f 30 37 2f 33 31 20 30  411 2009/07/31 0
2e11b 36 3a 31 34 3a 35 32 20 64 61 6e 69 65 6c 6b 31  6:14:52 danielk1
2e11c 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  977 Exp $.*/../*
2e11d 0a 2a 2a 20 54 72 61 63 65 20 6f 75 74 70 75 74  .** Trace output
2e11e 20 6d 61 63 72 6f 73 0a 2a 2f 0a 23 69 66 20 64   macros.*/.#if d
2e11f 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45  efined(SQLITE_TE
2e120 53 54 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  ST) || defined(S
2e121 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 53 51 4c  QLITE_DEBUG).SQL
2e122 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2e123 73 71 6c 69 74 65 33 57 68 65 72 65 54 72 61 63  sqlite3WhereTrac
2e124 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 23 69  e = 0;.#endif.#i
2e125 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
2e126 5f 54 45 53 54 29 20 26 26 20 64 65 66 69 6e 65  _TEST) && define
2e127 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a  d(SQLITE_DEBUG).
2e128 23 20 64 65 66 69 6e 65 20 57 48 45 52 45 54 52  # define WHERETR
2e129 41 43 45 28 58 29 20 20 69 66 28 73 71 6c 69 74  ACE(X)  if(sqlit
2e12a 65 33 57 68 65 72 65 54 72 61 63 65 29 20 73 71  e3WhereTrace) sq
2e12b 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2e12c 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e   X.#else.# defin
2e12d 65 20 57 48 45 52 45 54 52 41 43 45 28 58 29 0a  e WHERETRACE(X).
2e12e 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 6f 72 77 61  #endif../* Forwa
2e12f 72 64 20 72 65 66 65 72 65 6e 63 65 0a 2a 2f 0a  rd reference.*/.
2e130 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57  typedef struct W
2e131 68 65 72 65 43 6c 61 75 73 65 20 57 68 65 72 65  hereClause Where
2e132 43 6c 61 75 73 65 3b 0a 74 79 70 65 64 65 66 20  Clause;.typedef 
2e133 73 74 72 75 63 74 20 57 68 65 72 65 4d 61 73 6b  struct WhereMask
2e134 53 65 74 20 57 68 65 72 65 4d 61 73 6b 53 65 74  Set WhereMaskSet
2e135 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
2e136 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 57 68 65   WhereOrInfo Whe
2e137 72 65 4f 72 49 6e 66 6f 3b 0a 74 79 70 65 64 65  reOrInfo;.typede
2e138 66 20 73 74 72 75 63 74 20 57 68 65 72 65 41 6e  f struct WhereAn
2e139 64 49 6e 66 6f 20 57 68 65 72 65 41 6e 64 49 6e  dInfo WhereAndIn
2e13a 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  fo;.typedef stru
2e13b 63 74 20 57 68 65 72 65 43 6f 73 74 20 57 68 65  ct WhereCost Whe
2e13c 72 65 43 6f 73 74 3b 0a 0a 2f 2a 0a 2a 2a 20 54  reCost;../*.** T
2e13d 68 65 20 71 75 65 72 79 20 67 65 6e 65 72 61 74  he query generat
2e13e 6f 72 20 75 73 65 73 20 61 6e 20 61 72 72 61 79  or uses an array
2e13f 20 6f 66 20 69 6e 73 74 61 6e 63 65 73 20 6f 66   of instances of
2e140 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
2e141 74 6f 0a 2a 2a 20 68 65 6c 70 20 69 74 20 61 6e  to.** help it an
2e142 61 6c 79 7a 65 20 74 68 65 20 73 75 62 65 78 70  alyze the subexp
2e143 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20  ressions of the 
2e144 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 45  WHERE clause.  E
2e145 61 63 68 20 57 48 45 52 45 0a 2a 2a 20 63 6c 61  ach WHERE.** cla
2e146 75 73 65 20 73 75 62 65 78 70 72 65 73 73 69 6f  use subexpressio
2e147 6e 20 69 73 20 73 65 70 61 72 61 74 65 64 20 66  n is separated f
2e148 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 73 20 62  rom the others b
2e149 79 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 73 2c  y AND operators,
2e14a 0a 2a 2a 20 75 73 75 61 6c 6c 79 2c 20 6f 72 20  .** usually, or 
2e14b 73 6f 6d 65 74 69 6d 65 73 20 73 75 62 65 78 70  sometimes subexp
2e14c 72 65 73 73 69 6f 6e 73 20 73 65 70 61 72 61 74  ressions separat
2e14d 65 64 20 62 79 20 4f 52 2e 0a 2a 2a 0a 2a 2a 20  ed by OR..**.** 
2e14e 41 6c 6c 20 57 68 65 72 65 54 65 72 6d 73 20 61  All WhereTerms a
2e14f 72 65 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74  re collected int
2e150 6f 20 61 20 73 69 6e 67 6c 65 20 57 68 65 72 65  o a single Where
2e151 43 6c 61 75 73 65 20 73 74 72 75 63 74 75 72 65  Clause structure
2e152 2e 20 20 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  .  .** The follo
2e153 77 69 6e 67 20 69 64 65 6e 74 69 74 79 20 68 6f  wing identity ho
2e154 6c 64 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  lds:.**.**      
2e155 20 20 57 68 65 72 65 54 65 72 6d 2e 70 57 43 2d    WhereTerm.pWC-
2e156 3e 61 5b 57 68 65 72 65 54 65 72 6d 2e 69 64 78  >a[WhereTerm.idx
2e157 5d 20 3d 3d 20 57 68 65 72 65 54 65 72 6d 0a 2a  ] == WhereTerm.*
2e158 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 74 65 72 6d  *.** When a term
2e159 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a   is of the form:
2e15a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
2e15b 20 20 20 20 58 20 3c 6f 70 3e 20 3c 65 78 70 72      X <op> <expr
2e15c 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 58 20  >.**.** where X 
2e15d 69 73 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  is a column name
2e15e 20 61 6e 64 20 3c 6f 70 3e 20 69 73 20 6f 6e 65   and <op> is one
2e15f 20 6f 66 20 63 65 72 74 61 69 6e 20 6f 70 65 72   of certain oper
2e160 61 74 6f 72 73 2c 0a 2a 2a 20 74 68 65 6e 20 57  ators,.** then W
2e161 68 65 72 65 54 65 72 6d 2e 6c 65 66 74 43 75 72  hereTerm.leftCur
2e162 73 6f 72 20 61 6e 64 20 57 68 65 72 65 54 65 72  sor and WhereTer
2e163 6d 2e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 72  m.u.leftColumn r
2e164 65 63 6f 72 64 20 74 68 65 0a 2a 2a 20 63 75 72  ecord the.** cur
2e165 73 6f 72 20 6e 75 6d 62 65 72 20 61 6e 64 20 63  sor number and c
2e166 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 66 6f 72  olumn number for
2e167 20 58 2e 20 20 57 68 65 72 65 54 65 72 6d 2e 65   X.  WhereTerm.e
2e168 4f 70 65 72 61 74 6f 72 20 72 65 63 6f 72 64 73  Operator records
2e169 0a 2a 2a 20 74 68 65 20 3c 6f 70 3e 20 75 73 69  .** the <op> usi
2e16a 6e 67 20 61 20 62 69 74 6d 61 73 6b 20 65 6e 63  ng a bitmask enc
2e16b 6f 64 69 6e 67 20 64 65 66 69 6e 65 64 20 62 79  oding defined by
2e16c 20 57 4f 5f 78 78 78 20 62 65 6c 6f 77 2e 20 20   WO_xxx below.  
2e16d 54 68 65 0a 2a 2a 20 75 73 65 20 6f 66 20 61 20  The.** use of a 
2e16e 62 69 74 6d 61 73 6b 20 65 6e 63 6f 64 69 6e 67  bitmask encoding
2e16f 20 66 6f 72 20 74 68 65 20 6f 70 65 72 61 74 6f   for the operato
2e170 72 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f 20 73  r allows us to s
2e171 65 61 72 63 68 0a 2a 2a 20 71 75 69 63 6b 6c 79  earch.** quickly
2e172 20 66 6f 72 20 74 65 72 6d 73 20 74 68 61 74 20   for terms that 
2e173 6d 61 74 63 68 20 61 6e 79 20 6f 66 20 73 65 76  match any of sev
2e174 65 72 61 6c 20 64 69 66 66 65 72 65 6e 74 20 6f  eral different o
2e175 70 65 72 61 74 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20  perators..**.** 
2e176 41 20 57 68 65 72 65 54 65 72 6d 20 6d 69 67 68  A WhereTerm migh
2e177 74 20 61 6c 73 6f 20 62 65 20 74 77 6f 20 6f 72  t also be two or
2e178 20 6d 6f 72 65 20 73 75 62 74 65 72 6d 73 20 63   more subterms c
2e179 6f 6e 6e 65 63 74 65 64 20 62 79 20 4f 52 3a 0a  onnected by OR:.
2e17a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 28 74  **.**         (t
2e17b 31 2e 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 29  1.X <op> <expr>)
2e17c 20 4f 52 20 28 74 31 2e 59 20 3c 6f 70 3e 20 3c   OR (t1.Y <op> <
2e17d 65 78 70 72 3e 29 20 4f 52 20 2e 2e 2e 2e 0a 2a  expr>) OR .....*
2e17e 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 73 65 63  *.** In this sec
2e17f 6f 6e 64 20 63 61 73 65 2c 20 77 74 46 6c 61 67  ond case, wtFlag
2e180 20 61 73 20 74 68 65 20 54 45 52 4d 5f 4f 52 49   as the TERM_ORI
2e181 4e 46 4f 20 73 65 74 20 61 6e 64 20 65 4f 70 65  NFO set and eOpe
2e182 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52 0a 2a 2a 20  rator==WO_OR.** 
2e183 61 6e 64 20 74 68 65 20 57 68 65 72 65 54 65 72  and the WhereTer
2e184 6d 2e 75 2e 70 4f 72 49 6e 66 6f 20 66 69 65 6c  m.u.pOrInfo fiel
2e185 64 20 70 6f 69 6e 74 73 20 74 6f 20 61 75 78 69  d points to auxi
2e186 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f  liary informatio
2e187 6e 20 74 68 61 74 0a 2a 2a 20 69 73 20 63 6f 6c  n that.** is col
2e188 6c 65 63 74 65 64 20 61 62 6f 75 74 20 74 68 65  lected about the
2e189 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74 65 72 6d  .**.** If a term
2e18a 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c   in the WHERE cl
2e18b 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61  ause does not ma
2e18c 74 63 68 20 65 69 74 68 65 72 20 6f 66 20 74 68  tch either of th
2e18d 65 20 74 77 6f 20 70 72 65 76 69 6f 75 73 0a 2a  e two previous.*
2e18e 2a 20 63 61 74 65 67 6f 72 69 65 73 2c 20 74 68  * categories, th
2e18f 65 6e 20 65 4f 70 65 72 61 74 6f 72 3d 3d 30 2e  en eOperator==0.
2e190 20 20 54 68 65 20 57 68 65 72 65 54 65 72 6d 2e    The WhereTerm.
2e191 70 45 78 70 72 20 66 69 65 6c 64 20 69 73 20 73  pExpr field is s
2e192 74 69 6c 6c 20 73 65 74 0a 2a 2a 20 74 6f 20 74  till set.** to t
2e193 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 75 62 65  he original sube
2e194 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e 74 65 6e  xpression conten
2e195 74 20 61 6e 64 20 77 74 46 6c 61 67 73 20 69 73  t and wtFlags is
2e196 20 73 65 74 20 75 70 20 61 70 70 72 6f 70 72 69   set up appropri
2e197 61 74 65 6c 79 0a 2a 2a 20 62 75 74 20 6e 6f 20  ately.** but no 
2e198 6f 74 68 65 72 20 66 69 65 6c 64 73 20 69 6e 20  other fields in 
2e199 74 68 65 20 57 68 65 72 65 54 65 72 6d 20 6f 62  the WhereTerm ob
2e19a 6a 65 63 74 20 61 72 65 20 6d 65 61 6e 69 6e 67  ject are meaning
2e19b 66 75 6c 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  ful..**.** When 
2e19c 65 4f 70 65 72 61 74 6f 72 21 3d 30 2c 20 70 72  eOperator!=0, pr
2e19d 65 72 65 71 52 69 67 68 74 20 61 6e 64 20 70 72  ereqRight and pr
2e19e 65 72 65 71 41 6c 6c 20 72 65 63 6f 72 64 20 73  ereqAll record s
2e19f 65 74 73 20 6f 66 20 63 75 72 73 6f 72 20 6e 75  ets of cursor nu
2e1a0 6d 62 65 72 73 2c 0a 2a 2a 20 62 75 74 20 74 68  mbers,.** but th
2e1a1 65 79 20 64 6f 20 73 6f 20 69 6e 64 69 72 65 63  ey do so indirec
2e1a2 74 6c 79 2e 20 20 41 20 73 69 6e 67 6c 65 20 57  tly.  A single W
2e1a3 68 65 72 65 4d 61 73 6b 53 65 74 20 73 74 72 75  hereMaskSet stru
2e1a4 63 74 75 72 65 20 74 72 61 6e 73 6c 61 74 65 73  cture translates
2e1a5 0a 2a 2a 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  .** cursor numbe
2e1a6 72 20 69 6e 74 6f 20 62 69 74 73 20 61 6e 64 20  r into bits and 
2e1a7 74 68 65 20 74 72 61 6e 73 6c 61 74 65 64 20 62  the translated b
2e1a8 69 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  it is stored in 
2e1a9 74 68 65 20 70 72 65 72 65 71 0a 2a 2a 20 66 69  the prereq.** fi
2e1aa 65 6c 64 73 2e 20 20 54 68 65 20 74 72 61 6e 73  elds.  The trans
2e1ab 6c 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69  lation is used i
2e1ac 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 78 69 6d  n order to maxim
2e1ad 69 7a 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ize the number o
2e1ae 66 0a 2a 2a 20 62 69 74 73 20 74 68 61 74 20 77  f.** bits that w
2e1af 69 6c 6c 20 66 69 74 20 69 6e 20 61 20 42 69 74  ill fit in a Bit
2e1b0 6d 61 73 6b 2e 20 20 54 68 65 20 56 44 42 45 20  mask.  The VDBE 
2e1b1 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 6d  cursor numbers m
2e1b2 69 67 68 74 20 62 65 0a 2a 2a 20 73 70 72 65 61  ight be.** sprea
2e1b3 64 20 6f 75 74 20 6f 76 65 72 20 74 68 65 20 6e  d out over the n
2e1b4 6f 6e 2d 6e 65 67 61 74 69 76 65 20 69 6e 74 65  on-negative inte
2e1b5 67 65 72 73 2e 20 20 46 6f 72 20 65 78 61 6d 70  gers.  For examp
2e1b6 6c 65 2c 20 74 68 65 20 63 75 72 73 6f 72 0a 2a  le, the cursor.*
2e1b7 2a 20 6e 75 6d 62 65 72 73 20 6d 69 67 68 74 20  * numbers might 
2e1b8 62 65 20 33 2c 20 38 2c 20 39 2c 20 31 30 2c 20  be 3, 8, 9, 10, 
2e1b9 32 30 2c 20 32 33 2c 20 34 31 2c 20 61 6e 64 20  20, 23, 41, and 
2e1ba 34 35 2e 20 20 54 68 65 20 57 68 65 72 65 4d 61  45.  The WhereMa
2e1bb 73 6b 53 65 74 0a 2a 2a 20 74 72 61 6e 73 6c 61  skSet.** transla
2e1bc 74 65 73 20 74 68 65 73 65 20 73 70 61 72 73 65  tes these sparse
2e1bd 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20   cursor numbers 
2e1be 69 6e 74 6f 20 63 6f 6e 73 65 63 75 74 69 76 65  into consecutive
2e1bf 20 69 6e 74 65 67 65 72 73 0a 2a 2a 20 62 65 67   integers.** beg
2e1c0 69 6e 6e 69 6e 67 20 77 69 74 68 20 30 20 69 6e  inning with 0 in
2e1c1 20 6f 72 64 65 72 20 74 6f 20 6d 61 6b 65 20 74   order to make t
2e1c2 68 65 20 62 65 73 74 20 70 6f 73 73 69 62 6c 65  he best possible
2e1c3 20 75 73 65 20 6f 66 20 74 68 65 20 61 76 61 69   use of the avai
2e1c4 6c 61 62 6c 65 0a 2a 2a 20 62 69 74 73 20 69 6e  lable.** bits in
2e1c5 20 74 68 65 20 42 69 74 6d 61 73 6b 2e 20 20 53   the Bitmask.  S
2e1c6 6f 2c 20 69 6e 20 74 68 65 20 65 78 61 6d 70 6c  o, in the exampl
2e1c7 65 20 61 62 6f 76 65 2c 20 74 68 65 20 63 75 72  e above, the cur
2e1c8 73 6f 72 20 6e 75 6d 62 65 72 73 0a 2a 2a 20 77  sor numbers.** w
2e1c9 6f 75 6c 64 20 62 65 20 6d 61 70 70 65 64 20 69  ould be mapped i
2e1ca 6e 74 6f 20 69 6e 74 65 67 65 72 73 20 30 20 74  nto integers 0 t
2e1cb 68 72 6f 75 67 68 20 37 2e 0a 2a 2a 0a 2a 2a 20  hrough 7..**.** 
2e1cc 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 65  The number of te
2e1cd 72 6d 73 20 69 6e 20 61 20 6a 6f 69 6e 20 69 73  rms in a join is
2e1ce 20 6c 69 6d 69 74 65 64 20 62 79 20 74 68 65 20   limited by the 
2e1cf 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 0a 2a  number of bits.*
2e1d0 2a 20 69 6e 20 70 72 65 72 65 71 52 69 67 68 74  * in prereqRight
2e1d1 20 61 6e 64 20 70 72 65 72 65 71 41 6c 6c 2e 20   and prereqAll. 
2e1d2 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 73 20   The default is 
2e1d3 36 34 20 62 69 74 73 2c 20 68 65 6e 63 65 20 53  64 bits, hence S
2e1d4 51 4c 69 74 65 0a 2a 2a 20 69 73 20 6f 6e 6c 79  QLite.** is only
2e1d5 20 61 62 6c 65 20 74 6f 20 70 72 6f 63 65 73 73   able to process
2e1d6 20 6a 6f 69 6e 73 20 77 69 74 68 20 36 34 20 6f   joins with 64 o
2e1d7 72 20 66 65 77 65 72 20 74 61 62 6c 65 73 2e 0a  r fewer tables..
2e1d8 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
2e1d9 74 20 57 68 65 72 65 54 65 72 6d 20 57 68 65 72  t WhereTerm Wher
2e1da 65 54 65 72 6d 3b 0a 73 74 72 75 63 74 20 57 68  eTerm;.struct Wh
2e1db 65 72 65 54 65 72 6d 20 7b 0a 20 20 45 78 70 72  ereTerm {.  Expr
2e1dc 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20   *pExpr;        
2e1dd 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
2e1de 6f 20 74 68 65 20 73 75 62 65 78 70 72 65 73 73  o the subexpress
2e1df 69 6f 6e 20 74 68 61 74 20 69 73 20 74 68 69 73  ion that is this
2e1e0 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 69   term */.  int i
2e1e1 50 61 72 65 6e 74 3b 20 20 20 20 20 20 20 20 20  Parent;         
2e1e2 20 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 70 57     /* Disable pW
2e1e3 43 2d 3e 61 5b 69 50 61 72 65 6e 74 5d 20 77 68  C->a[iParent] wh
2e1e4 65 6e 20 74 68 69 73 20 74 65 72 6d 20 64 69 73  en this term dis
2e1e5 61 62 6c 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6c  abled */.  int l
2e1e6 65 66 74 43 75 72 73 6f 72 3b 20 20 20 20 20 20  eftCursor;      
2e1e7 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d     /* Cursor num
2e1e8 62 65 72 20 6f 66 20 58 20 69 6e 20 22 58 20 3c  ber of X in "X <
2e1e9 6f 70 3e 20 3c 65 78 70 72 3e 22 20 2a 2f 0a 20  op> <expr>" */. 
2e1ea 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 69 6e 74   union {.    int
2e1eb 20 6c 65 66 74 43 6f 6c 75 6d 6e 3b 20 20 20 20   leftColumn;    
2e1ec 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e       /* Column n
2e1ed 75 6d 62 65 72 20 6f 66 20 58 20 69 6e 20 22 58  umber of X in "X
2e1ee 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 20 2a 2f   <op> <expr>" */
2e1ef 0a 20 20 20 20 57 68 65 72 65 4f 72 49 6e 66 6f  .    WhereOrInfo
2e1f0 20 2a 70 4f 72 49 6e 66 6f 3b 20 20 20 2f 2a 20   *pOrInfo;   /* 
2e1f1 45 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f  Extra informatio
2e1f2 6e 20 69 66 20 65 4f 70 65 72 61 74 6f 72 3d 3d  n if eOperator==
2e1f3 57 4f 5f 4f 52 20 2a 2f 0a 20 20 20 20 57 68 65  WO_OR */.    Whe
2e1f4 72 65 41 6e 64 49 6e 66 6f 20 2a 70 41 6e 64 49  reAndInfo *pAndI
2e1f5 6e 66 6f 3b 20 2f 2a 20 45 78 74 72 61 20 69 6e  nfo; /* Extra in
2e1f6 66 6f 72 6d 61 74 69 6f 6e 20 69 66 20 65 4f 70  formation if eOp
2e1f7 65 72 61 74 6f 72 3d 3d 57 4f 5f 41 4e 44 20 2a  erator==WO_AND *
2e1f8 2f 0a 20 20 7d 20 75 3b 0a 20 20 75 31 36 20 65  /.  } u;.  u16 e
2e1f9 4f 70 65 72 61 74 6f 72 3b 20 20 20 20 20 20 20  Operator;       
2e1fa 20 20 20 2f 2a 20 41 20 57 4f 5f 78 78 20 76 61     /* A WO_xx va
2e1fb 6c 75 65 20 64 65 73 63 72 69 62 69 6e 67 20 3c  lue describing <
2e1fc 6f 70 3e 20 2a 2f 0a 20 20 75 38 20 77 74 46 6c  op> */.  u8 wtFl
2e1fd 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
2e1fe 20 2f 2a 20 54 45 52 4d 5f 78 78 78 20 62 69 74   /* TERM_xxx bit
2e1ff 20 66 6c 61 67 73 2e 20 20 53 65 65 20 62 65 6c   flags.  See bel
2e200 6f 77 20 2a 2f 0a 20 20 75 38 20 6e 43 68 69 6c  ow */.  u8 nChil
2e201 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
2e202 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 69  /* Number of chi
2e203 6c 64 72 65 6e 20 74 68 61 74 20 6d 75 73 74 20  ldren that must 
2e204 64 69 73 61 62 6c 65 20 75 73 20 2a 2f 0a 20 20  disable us */.  
2e205 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43  WhereClause *pWC
2e206 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63  ;       /* The c
2e207 6c 61 75 73 65 20 74 68 69 73 20 74 65 72 6d 20  lause this term 
2e208 69 73 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 20  is part of */.  
2e209 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 52 69  Bitmask prereqRi
2e20a 67 68 74 3b 20 20 20 20 2f 2a 20 42 69 74 6d 61  ght;    /* Bitma
2e20b 73 6b 20 6f 66 20 74 61 62 6c 65 73 20 75 73 65  sk of tables use
2e20c 64 20 62 79 20 70 45 78 70 72 2d 3e 70 52 69 67  d by pExpr->pRig
2e20d 68 74 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20  ht */.  Bitmask 
2e20e 70 72 65 72 65 71 41 6c 6c 3b 20 20 20 20 20 20  prereqAll;      
2e20f 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 74 61  /* Bitmask of ta
2e210 62 6c 65 73 20 72 65 66 65 72 65 6e 63 65 64 20  bles referenced 
2e211 62 79 20 70 45 78 70 72 20 2a 2f 0a 7d 3b 0a 0a  by pExpr */.};..
2e212 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61  /*.** Allowed va
2e213 6c 75 65 73 20 6f 66 20 57 68 65 72 65 54 65 72  lues of WhereTer
2e214 6d 2e 77 74 46 6c 61 67 73 0a 2a 2f 0a 23 64 65  m.wtFlags.*/.#de
2e215 66 69 6e 65 20 54 45 52 4d 5f 44 59 4e 41 4d 49  fine TERM_DYNAMI
2e216 43 20 20 20 20 30 78 30 31 20 20 20 2f 2a 20 4e  C    0x01   /* N
2e217 65 65 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69  eed to call sqli
2e218 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
2e219 2c 20 70 45 78 70 72 29 20 2a 2f 0a 23 64 65 66  , pExpr) */.#def
2e21a 69 6e 65 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  ine TERM_VIRTUAL
2e21b 20 20 20 20 30 78 30 32 20 20 20 2f 2a 20 41 64      0x02   /* Ad
2e21c 64 65 64 20 62 79 20 74 68 65 20 6f 70 74 69 6d  ded by the optim
2e21d 69 7a 65 72 2e 20 20 44 6f 20 6e 6f 74 20 63 6f  izer.  Do not co
2e21e 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45  de */.#define TE
2e21f 52 4d 5f 43 4f 44 45 44 20 20 20 20 20 20 30 78  RM_CODED      0x
2e220 30 34 20 20 20 2f 2a 20 54 68 69 73 20 74 65 72  04   /* This ter
2e221 6d 20 69 73 20 61 6c 72 65 61 64 79 20 63 6f 64  m is already cod
2e222 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45  ed */.#define TE
2e223 52 4d 5f 43 4f 50 49 45 44 20 20 20 20 20 30 78  RM_COPIED     0x
2e224 30 38 20 20 20 2f 2a 20 48 61 73 20 61 20 63 68  08   /* Has a ch
2e225 69 6c 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54  ild */.#define T
2e226 45 52 4d 5f 4f 52 49 4e 46 4f 20 20 20 20 20 30  ERM_ORINFO     0
2e227 78 31 30 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f  x10   /* Need to
2e228 20 66 72 65 65 20 74 68 65 20 57 68 65 72 65 54   free the WhereT
2e229 65 72 6d 2e 75 2e 70 4f 72 49 6e 66 6f 20 6f 62  erm.u.pOrInfo ob
2e22a 6a 65 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ject */.#define 
2e22b 54 45 52 4d 5f 41 4e 44 49 4e 46 4f 20 20 20 20  TERM_ANDINFO    
2e22c 30 78 32 30 20 20 20 2f 2a 20 4e 65 65 64 20 74  0x20   /* Need t
2e22d 6f 20 66 72 65 65 20 74 68 65 20 57 68 65 72 65  o free the Where
2e22e 54 65 72 6d 2e 75 2e 70 41 6e 64 49 6e 66 6f 20  Term.u.pAndInfo 
2e22f 6f 62 6a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54  obj */.#define T
2e230 45 52 4d 5f 4f 52 5f 4f 4b 20 20 20 20 20 20 30  ERM_OR_OK      0
2e231 78 34 30 20 20 20 2f 2a 20 55 73 65 64 20 64 75  x40   /* Used du
2e232 72 69 6e 67 20 4f 52 2d 63 6c 61 75 73 65 20 70  ring OR-clause p
2e233 72 6f 63 65 73 73 69 6e 67 20 2a 2f 0a 0a 2f 2a  rocessing */../*
2e234 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
2e235 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
2e236 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73   structure holds
2e237 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   all information
2e238 20 61 62 6f 75 74 20 61 0a 2a 2a 20 57 48 45 52   about a.** WHER
2e239 45 20 63 6c 61 75 73 65 2e 20 20 4d 6f 73 74 6c  E clause.  Mostl
2e23a 79 20 74 68 69 73 20 69 73 20 61 20 63 6f 6e 74  y this is a cont
2e23b 61 69 6e 65 72 20 66 6f 72 20 6f 6e 65 20 6f 72  ainer for one or
2e23c 20 6d 6f 72 65 20 57 68 65 72 65 54 65 72 6d 73   more WhereTerms
2e23d 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72  ..*/.struct Wher
2e23e 65 43 6c 61 75 73 65 20 7b 0a 20 20 50 61 72 73  eClause {.  Pars
2e23f 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20  e *pParse;      
2e240 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
2e241 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
2e242 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d  WhereMaskSet *pM
2e243 61 73 6b 53 65 74 3b 20 20 2f 2a 20 4d 61 70 70  askSet;  /* Mapp
2e244 69 6e 67 20 6f 66 20 74 61 62 6c 65 20 63 75 72  ing of table cur
2e245 73 6f 72 20 6e 75 6d 62 65 72 73 20 74 6f 20 62  sor numbers to b
2e246 69 74 6d 61 73 6b 73 20 2a 2f 0a 20 20 42 69 74  itmasks */.  Bit
2e247 6d 61 73 6b 20 76 6d 61 73 6b 3b 20 20 20 20 20  mask vmask;     
2e248 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b        /* Bitmask
2e249 20 69 64 65 6e 74 69 66 79 69 6e 67 20 76 69 72   identifying vir
2e24a 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72 73 6f  tual table curso
2e24b 72 73 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20  rs */.  u8 op;  
2e24c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e24d 20 2f 2a 20 53 70 6c 69 74 20 6f 70 65 72 61 74   /* Split operat
2e24e 6f 72 2e 20 20 54 4b 5f 41 4e 44 20 6f 72 20 54  or.  TK_AND or T
2e24f 4b 5f 4f 52 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  K_OR */.  int nT
2e250 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  erm;            
2e251 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2e252 74 65 72 6d 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  terms */.  int n
2e253 53 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20  Slot;           
2e254 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
2e255 20 65 6e 74 72 69 65 73 20 69 6e 20 61 5b 5d 20   entries in a[] 
2e256 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a  */.  WhereTerm *
2e257 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  a;            /*
2e258 20 45 61 63 68 20 61 5b 5d 20 64 65 73 63 72 69   Each a[] descri
2e259 62 65 73 20 61 20 74 65 72 6d 20 6f 66 20 74 68  bes a term of th
2e25a 65 20 57 48 45 52 45 20 63 6c 75 61 73 65 20 2a  e WHERE cluase *
2e25b 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  /.#if defined(SQ
2e25c 4c 49 54 45 5f 53 4d 41 4c 4c 5f 53 54 41 43 4b  LITE_SMALL_STACK
2e25d 29 0a 20 20 57 68 65 72 65 54 65 72 6d 20 61 53  ).  WhereTerm aS
2e25e 74 61 74 69 63 5b 31 5d 3b 20 20 20 20 2f 2a 20  tatic[1];    /* 
2e25f 49 6e 69 74 69 61 6c 20 73 74 61 74 69 63 20 73  Initial static s
2e260 70 61 63 65 20 66 6f 72 20 61 5b 5d 20 2a 2f 0a  pace for a[] */.
2e261 23 65 6c 73 65 0a 20 20 57 68 65 72 65 54 65 72  #else.  WhereTer
2e262 6d 20 61 53 74 61 74 69 63 5b 38 5d 3b 20 20 20  m aStatic[8];   
2e263 20 2f 2a 20 49 6e 69 74 69 61 6c 20 73 74 61 74   /* Initial stat
2e264 69 63 20 73 70 61 63 65 20 66 6f 72 20 61 5b 5d  ic space for a[]
2e265 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f   */.#endif.};../
2e266 2a 0a 2a 2a 20 41 20 57 68 65 72 65 54 65 72 6d  *.** A WhereTerm
2e267 20 77 69 74 68 20 65 4f 70 65 72 61 74 6f 72 3d   with eOperator=
2e268 3d 57 4f 5f 4f 52 20 68 61 73 20 69 74 73 20 75  =WO_OR has its u
2e269 2e 70 4f 72 49 6e 66 6f 20 70 6f 69 6e 74 65 72  .pOrInfo pointer
2e26a 20 73 65 74 20 74 6f 0a 2a 2a 20 61 20 64 79 6e   set to.** a dyn
2e26b 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74  amically allocat
2e26c 65 64 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ed instance of t
2e26d 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
2e26e 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63  ucture..*/.struc
2e26f 74 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 7b 0a  t WhereOrInfo {.
2e270 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 77 63    WhereClause wc
2e271 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65  ;          /* De
2e272 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 69 6e 74 6f  composition into
2e273 20 73 75 62 74 65 72 6d 73 20 2a 2f 0a 20 20 42   subterms */.  B
2e274 69 74 6d 61 73 6b 20 69 6e 64 65 78 61 62 6c 65  itmask indexable
2e275 3b 20 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61  ;       /* Bitma
2e276 73 6b 20 6f 66 20 61 6c 6c 20 69 6e 64 65 78 61  sk of all indexa
2e277 62 6c 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68  ble tables in th
2e278 65 20 63 6c 61 75 73 65 20 2a 2f 0a 7d 3b 0a 0a  e clause */.};..
2e279 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65 54 65 72  /*.** A WhereTer
2e27a 6d 20 77 69 74 68 20 65 4f 70 65 72 61 74 6f 72  m with eOperator
2e27b 3d 3d 57 4f 5f 41 4e 44 20 68 61 73 20 69 74 73  ==WO_AND has its
2e27c 20 75 2e 70 41 6e 64 49 6e 66 6f 20 70 6f 69 6e   u.pAndInfo poin
2e27d 74 65 72 20 73 65 74 20 74 6f 0a 2a 2a 20 61 20  ter set to.** a 
2e27e 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f  dynamically allo
2e27f 63 61 74 65 64 20 69 6e 73 74 61 6e 63 65 20 6f  cated instance o
2e280 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
2e281 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74  structure..*/.st
2e282 72 75 63 74 20 57 68 65 72 65 41 6e 64 49 6e 66  ruct WhereAndInf
2e283 6f 20 7b 0a 20 20 57 68 65 72 65 43 6c 61 75 73  o {.  WhereClaus
2e284 65 20 77 63 3b 20 20 20 20 20 20 20 20 20 20 2f  e wc;          /
2e285 2a 20 54 68 65 20 73 75 62 65 78 70 72 65 73 73  * The subexpress
2e286 69 6f 6e 20 62 72 6f 6b 65 6e 20 6f 75 74 20 2a  ion broken out *
2e287 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  /.};../*.** An i
2e288 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
2e289 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
2e28a 72 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f  re keeps track o
2e28b 66 20 61 20 6d 61 70 70 69 6e 67 0a 2a 2a 20 62  f a mapping.** b
2e28c 65 74 77 65 65 6e 20 56 44 42 45 20 63 75 72 73  etween VDBE curs
2e28d 6f 72 20 6e 75 6d 62 65 72 73 20 61 6e 64 20 62  or numbers and b
2e28e 69 74 73 20 6f 66 20 74 68 65 20 62 69 74 6d 61  its of the bitma
2e28f 73 6b 73 20 69 6e 20 57 68 65 72 65 54 65 72 6d  sks in WhereTerm
2e290 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 44 42 45  ..**.** The VDBE
2e291 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20   cursor numbers 
2e292 61 72 65 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65  are small intege
2e293 72 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20  rs contained in 
2e294 0a 2a 2a 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  .** SrcList_item
2e295 2e 69 43 75 72 73 6f 72 20 61 6e 64 20 45 78 70  .iCursor and Exp
2e296 72 2e 69 54 61 62 6c 65 20 66 69 65 6c 64 73 2e  r.iTable fields.
2e297 20 20 46 6f 72 20 61 6e 79 20 67 69 76 65 6e 20    For any given 
2e298 57 48 45 52 45 20 0a 2a 2a 20 63 6c 61 75 73 65  WHERE .** clause
2e299 2c 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d  , the cursor num
2e29a 62 65 72 73 20 6d 69 67 68 74 20 6e 6f 74 20 62  bers might not b
2e29b 65 67 69 6e 20 77 69 74 68 20 30 20 61 6e 64 20  egin with 0 and 
2e29c 74 68 65 79 20 6d 69 67 68 74 0a 2a 2a 20 63 6f  they might.** co
2e29d 6e 74 61 69 6e 20 67 61 70 73 20 69 6e 20 74 68  ntain gaps in th
2e29e 65 20 6e 75 6d 62 65 72 69 6e 67 20 73 65 71 75  e numbering sequ
2e29f 65 6e 63 65 2e 20 20 42 75 74 20 77 65 20 77 61  ence.  But we wa
2e2a0 6e 74 20 74 6f 20 6d 61 6b 65 20 6d 61 78 69 6d  nt to make maxim
2e2a1 75 6d 0a 2a 2a 20 75 73 65 20 6f 66 20 74 68 65  um.** use of the
2e2a2 20 62 69 74 73 20 69 6e 20 6f 75 72 20 62 69 74   bits in our bit
2e2a3 6d 61 73 6b 73 2e 20 20 54 68 69 73 20 73 74 72  masks.  This str
2e2a4 75 63 74 75 72 65 20 70 72 6f 76 69 64 65 73 20  ucture provides 
2e2a5 61 20 6d 61 70 70 69 6e 67 0a 2a 2a 20 66 72 6f  a mapping.** fro
2e2a6 6d 20 74 68 65 20 73 70 61 72 73 65 20 63 75 72  m the sparse cur
2e2a7 73 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e 74 6f  sor numbers into
2e2a8 20 63 6f 6e 73 65 63 75 74 69 76 65 20 69 6e 74   consecutive int
2e2a9 65 67 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 0a  egers beginning.
2e2aa 2a 2a 20 77 69 74 68 20 30 2e 0a 2a 2a 0a 2a 2a  ** with 0..**.**
2e2ab 20 49 66 20 57 68 65 72 65 4d 61 73 6b 53 65 74   If WhereMaskSet
2e2ac 2e 69 78 5b 41 5d 3d 3d 42 20 69 74 20 6d 65 61  .ix[A]==B it mea
2e2ad 6e 73 20 74 68 61 74 20 54 68 65 20 41 2d 74 68  ns that The A-th
2e2ae 20 62 69 74 20 6f 66 20 61 20 42 69 74 6d 61 73   bit of a Bitmas
2e2af 6b 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 73  k.** corresponds
2e2b0 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d   VDBE cursor num
2e2b1 62 65 72 20 42 2e 20 20 54 68 65 20 41 2d 74 68  ber B.  The A-th
2e2b2 20 62 69 74 20 6f 66 20 61 20 62 69 74 6d 61 73   bit of a bitmas
2e2b3 6b 20 69 73 20 31 3c 3c 41 2e 0a 2a 2a 0a 2a 2a  k is 1<<A..**.**
2e2b4 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66   For example, if
2e2b5 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2e2b6 65 20 65 78 70 72 65 73 73 69 6f 6e 20 75 73 65  e expression use
2e2b7 64 20 74 68 65 73 65 20 56 44 42 45 0a 2a 2a 20  d these VDBE.** 
2e2b8 63 75 72 73 6f 72 73 3a 20 20 34 2c 20 35 2c 20  cursors:  4, 5, 
2e2b9 38 2c 20 32 39 2c 20 35 37 2c 20 37 33 2e 20 20  8, 29, 57, 73.  
2e2ba 54 68 65 6e 20 74 68 65 20 20 57 68 65 72 65 4d  Then the  WhereM
2e2bb 61 73 6b 53 65 74 20 73 74 72 75 63 74 75 72 65  askSet structure
2e2bc 0a 2a 2a 20 77 6f 75 6c 64 20 6d 61 70 20 74 68  .** would map th
2e2bd 6f 73 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  ose cursor numbe
2e2be 72 73 20 69 6e 74 6f 20 62 69 74 73 20 30 20 74  rs into bits 0 t
2e2bf 68 72 6f 75 67 68 20 35 2e 0a 2a 2a 0a 2a 2a 20  hrough 5..**.** 
2e2c0 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6d 61  Note that the ma
2e2c1 70 70 69 6e 67 20 69 73 20 6e 6f 74 20 6e 65 63  pping is not nec
2e2c2 65 73 73 61 72 69 6c 79 20 6f 72 64 65 72 65 64  essarily ordered
2e2c3 2e 20 20 49 6e 20 74 68 65 20 65 78 61 6d 70 6c  .  In the exampl
2e2c4 65 0a 2a 2a 20 61 62 6f 76 65 2c 20 74 68 65 20  e.** above, the 
2e2c5 6d 61 70 70 69 6e 67 20 6d 69 67 68 74 20 67 6f  mapping might go
2e2c6 20 6c 69 6b 65 20 74 68 69 73 3a 20 20 34 2d 3e   like this:  4->
2e2c7 33 2c 20 35 2d 3e 31 2c 20 38 2d 3e 32 2c 20 32  3, 5->1, 8->2, 2
2e2c8 39 2d 3e 30 2c 0a 2a 2a 20 35 37 2d 3e 35 2c 20  9->0,.** 57->5, 
2e2c9 37 33 2d 3e 34 2e 20 20 4f 72 20 6f 6e 65 20 6f  73->4.  Or one o
2e2ca 66 20 37 31 39 20 6f 74 68 65 72 20 63 6f 6d 62  f 719 other comb
2e2cb 69 6e 61 74 69 6f 6e 73 20 6d 69 67 68 74 20 62  inations might b
2e2cc 65 20 75 73 65 64 2e 20 49 74 0a 2a 2a 20 64 6f  e used. It.** do
2e2cd 65 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6d 61  es not really ma
2e2ce 74 74 65 72 2e 20 20 57 68 61 74 20 69 73 20 69  tter.  What is i
2e2cf 6d 70 6f 72 74 61 6e 74 20 69 73 20 74 68 61 74  mportant is that
2e2d0 20 73 70 61 72 73 65 20 63 75 72 73 6f 72 0a 2a   sparse cursor.*
2e2d1 2a 20 6e 75 6d 62 65 72 73 20 61 6c 6c 20 67 65  * numbers all ge
2e2d2 74 20 6d 61 70 70 65 64 20 69 6e 74 6f 20 62 69  t mapped into bi
2e2d3 74 20 6e 75 6d 62 65 72 73 20 74 68 61 74 20 62  t numbers that b
2e2d4 65 67 69 6e 20 77 69 74 68 20 30 20 61 6e 64 20  egin with 0 and 
2e2d5 63 6f 6e 74 61 69 6e 0a 2a 2a 20 6e 6f 20 67 61  contain.** no ga
2e2d6 70 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68  ps..*/.struct Wh
2e2d7 65 72 65 4d 61 73 6b 53 65 74 20 7b 0a 20 20 69  ereMaskSet {.  i
2e2d8 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20  nt n;           
2e2d9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e2da 4e 75 6d 62 65 72 20 6f 66 20 61 73 73 69 67 6e  Number of assign
2e2db 65 64 20 63 75 72 73 6f 72 20 76 61 6c 75 65 73  ed cursor values
2e2dc 20 2a 2f 0a 20 20 69 6e 74 20 69 78 5b 42 4d 53   */.  int ix[BMS
2e2dd 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ];              
2e2de 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 61 73      /* Cursor as
2e2df 73 69 67 6e 65 64 20 74 6f 20 65 61 63 68 20 62  signed to each b
2e2e0 69 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  it */.};../*.** 
2e2e1 41 20 57 68 65 72 65 43 6f 73 74 20 6f 62 6a 65  A WhereCost obje
2e2e2 63 74 20 72 65 63 6f 72 64 73 20 61 20 6c 6f 6f  ct records a loo
2e2e3 6b 75 70 20 73 74 72 61 74 65 67 79 20 61 6e 64  kup strategy and
2e2e4 20 74 68 65 20 65 73 74 69 6d 61 74 65 64 0a 2a   the estimated.*
2e2e5 2a 20 63 6f 73 74 20 6f 66 20 70 75 72 73 75 69  * cost of pursui
2e2e6 6e 67 20 74 68 61 74 20 73 74 72 61 74 65 67 79  ng that strategy
2e2e7 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72  ..*/.struct Wher
2e2e8 65 43 6f 73 74 20 7b 0a 20 20 57 68 65 72 65 50  eCost {.  WhereP
2e2e9 6c 61 6e 20 70 6c 61 6e 3b 20 20 20 20 2f 2a 20  lan plan;    /* 
2e2ea 54 68 65 20 6c 6f 6f 6b 75 70 20 73 74 72 61 74  The lookup strat
2e2eb 65 67 79 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20  egy */.  double 
2e2ec 72 43 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 4f  rCost;      /* O
2e2ed 76 65 72 61 6c 6c 20 63 6f 73 74 20 6f 66 20 70  verall cost of p
2e2ee 75 72 73 75 69 6e 67 20 74 68 69 73 20 73 65 61  ursuing this sea
2e2ef 72 63 68 20 73 74 72 61 74 65 67 79 20 2a 2f 0a  rch strategy */.
2e2f0 20 20 64 6f 75 62 6c 65 20 6e 52 6f 77 3b 20 20    double nRow;  
2e2f1 20 20 20 20 20 2f 2a 20 45 73 74 69 6d 61 74 65       /* Estimate
2e2f2 64 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74 70  d number of outp
2e2f3 75 74 20 72 6f 77 73 20 2a 2f 0a 20 20 42 69 74  ut rows */.  Bit
2e2f4 6d 61 73 6b 20 75 73 65 64 3b 20 20 20 20 20 20  mask used;      
2e2f5 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 63 75  /* Bitmask of cu
2e2f6 72 73 6f 72 73 20 75 73 65 64 20 62 79 20 74 68  rsors used by th
2e2f7 69 73 20 70 6c 61 6e 20 2a 2f 0a 7d 3b 0a 0a 2f  is plan */.};../
2e2f8 2a 0a 2a 2a 20 42 69 74 6d 61 73 6b 73 20 66 6f  *.** Bitmasks fo
2e2f9 72 20 74 68 65 20 6f 70 65 72 61 74 6f 72 73 20  r the operators 
2e2fa 74 68 61 74 20 69 6e 64 69 63 65 73 20 61 72 65  that indices are
2e2fb 20 61 62 6c 65 20 74 6f 20 65 78 70 6c 6f 69 74   able to exploit
2e2fc 2e 20 20 41 6e 0a 2a 2a 20 4f 52 2d 65 64 20 63  .  An.** OR-ed c
2e2fd 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74 68  ombination of th
2e2fe 65 73 65 20 76 61 6c 75 65 73 20 63 61 6e 20 62  ese values can b
2e2ff 65 20 75 73 65 64 20 77 68 65 6e 20 73 65 61 72  e used when sear
2e300 63 68 69 6e 67 20 66 6f 72 0a 2a 2a 20 74 65 72  ching for.** ter
2e301 6d 73 20 69 6e 20 74 68 65 20 77 68 65 72 65 20  ms in the where 
2e302 63 6c 61 75 73 65 2e 0a 2a 2f 0a 23 64 65 66 69  clause..*/.#defi
2e303 6e 65 20 57 4f 5f 49 4e 20 20 20 20 20 30 78 30  ne WO_IN     0x0
2e304 30 31 0a 23 64 65 66 69 6e 65 20 57 4f 5f 45 51  01.#define WO_EQ
2e305 20 20 20 20 20 30 78 30 30 32 0a 23 64 65 66 69       0x002.#defi
2e306 6e 65 20 57 4f 5f 4c 54 20 20 20 20 20 28 57 4f  ne WO_LT     (WO
2e307 5f 45 51 3c 3c 28 54 4b 5f 4c 54 2d 54 4b 5f 45  _EQ<<(TK_LT-TK_E
2e308 51 29 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f 4c  Q)).#define WO_L
2e309 45 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54  E     (WO_EQ<<(T
2e30a 4b 5f 4c 45 2d 54 4b 5f 45 51 29 29 0a 23 64 65  K_LE-TK_EQ)).#de
2e30b 66 69 6e 65 20 57 4f 5f 47 54 20 20 20 20 20 28  fine WO_GT     (
2e30c 57 4f 5f 45 51 3c 3c 28 54 4b 5f 47 54 2d 54 4b  WO_EQ<<(TK_GT-TK
2e30d 5f 45 51 29 29 0a 23 64 65 66 69 6e 65 20 57 4f  _EQ)).#define WO
2e30e 5f 47 45 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c  _GE     (WO_EQ<<
2e30f 28 54 4b 5f 47 45 2d 54 4b 5f 45 51 29 29 0a 23  (TK_GE-TK_EQ)).#
2e310 64 65 66 69 6e 65 20 57 4f 5f 4d 41 54 43 48 20  define WO_MATCH 
2e311 20 30 78 30 34 30 0a 23 64 65 66 69 6e 65 20 57   0x040.#define W
2e312 4f 5f 49 53 4e 55 4c 4c 20 30 78 30 38 30 0a 23  O_ISNULL 0x080.#
2e313 64 65 66 69 6e 65 20 57 4f 5f 4f 52 20 20 20 20  define WO_OR    
2e314 20 30 78 31 30 30 20 20 20 20 20 20 20 2f 2a 20   0x100       /* 
2e315 54 77 6f 20 6f 72 20 6d 6f 72 65 20 4f 52 2d 63  Two or more OR-c
2e316 6f 6e 6e 65 63 74 65 64 20 74 65 72 6d 73 20 2a  onnected terms *
2e317 2f 0a 23 64 65 66 69 6e 65 20 57 4f 5f 41 4e 44  /.#define WO_AND
2e318 20 20 20 20 30 78 32 30 30 20 20 20 20 20 20 20      0x200       
2e319 2f 2a 20 54 77 6f 20 6f 72 20 6d 6f 72 65 20 41  /* Two or more A
2e31a 4e 44 2d 63 6f 6e 6e 65 63 74 65 64 20 74 65 72  ND-connected ter
2e31b 6d 73 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 57  ms */..#define W
2e31c 4f 5f 41 4c 4c 20 20 20 20 30 78 66 66 66 20 20  O_ALL    0xfff  
2e31d 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20       /* Mask of 
2e31e 61 6c 6c 20 70 6f 73 73 69 62 6c 65 20 57 4f 5f  all possible WO_
2e31f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 23 64 65 66  * values */.#def
2e320 69 6e 65 20 57 4f 5f 53 49 4e 47 4c 45 20 30 78  ine WO_SINGLE 0x
2e321 30 66 66 20 20 20 20 20 20 20 2f 2a 20 4d 61 73  0ff       /* Mas
2e322 6b 20 6f 66 20 61 6c 6c 20 6e 6f 6e 2d 63 6f 6d  k of all non-com
2e323 70 6f 75 6e 64 20 57 4f 5f 2a 20 76 61 6c 75 65  pound WO_* value
2e324 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 75  s */../*.** Valu
2e325 65 20 66 6f 72 20 77 73 46 6c 61 67 73 20 72 65  e for wsFlags re
2e326 74 75 72 6e 65 64 20 62 79 20 62 65 73 74 49 6e  turned by bestIn
2e327 64 65 78 28 29 20 61 6e 64 20 73 74 6f 72 65 64  dex() and stored
2e328 20 69 6e 0a 2a 2a 20 57 68 65 72 65 4c 65 76 65   in.** WhereLeve
2e329 6c 2e 77 73 46 6c 61 67 73 2e 20 20 54 68 65 73  l.wsFlags.  Thes
2e32a 65 20 66 6c 61 67 73 20 64 65 74 65 72 6d 69 6e  e flags determin
2e32b 65 20 77 68 69 63 68 20 73 65 61 72 63 68 0a 2a  e which search.*
2e32c 2a 20 73 74 72 61 74 65 67 69 65 73 20 61 72 65  * strategies are
2e32d 20 61 70 70 72 6f 70 72 69 61 74 65 2e 0a 2a 2a   appropriate..**
2e32e 0a 2a 2a 20 54 68 65 20 6c 65 61 73 74 20 73 69  .** The least si
2e32f 67 6e 69 66 69 63 61 6e 74 20 31 32 20 62 69 74  gnificant 12 bit
2e330 73 20 69 73 20 72 65 73 65 72 76 65 64 20 61 73  s is reserved as
2e331 20 61 20 6d 61 73 6b 20 66 6f 72 20 57 4f 5f 20   a mask for WO_ 
2e332 76 61 6c 75 65 73 20 61 62 6f 76 65 2e 0a 2a 2a  values above..**
2e333 20 54 68 65 20 57 68 65 72 65 4c 65 76 65 6c 2e   The WhereLevel.
2e334 77 73 46 6c 61 67 73 20 66 69 65 6c 64 20 69 73  wsFlags field is
2e335 20 75 73 75 61 6c 6c 79 20 73 65 74 20 74 6f 20   usually set to 
2e336 57 4f 5f 49 4e 7c 57 4f 5f 45 51 7c 57 4f 5f 49  WO_IN|WO_EQ|WO_I
2e337 53 4e 55 4c 4c 2e 0a 2a 2a 20 42 75 74 20 69 66  SNULL..** But if
2e338 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 74 68   the table is th
2e339 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66  e right table of
2e33a 20 61 20 6c 65 66 74 20 6a 6f 69 6e 2c 20 57 68   a left join, Wh
2e33b 65 72 65 4c 65 76 65 6c 2e 77 73 46 6c 61 67 73  ereLevel.wsFlags
2e33c 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 57 4f  .** is set to WO
2e33d 5f 49 4e 7c 57 4f 5f 45 51 2e 20 20 54 68 65 20  _IN|WO_EQ.  The 
2e33e 57 68 65 72 65 4c 65 76 65 6c 2e 77 73 46 6c 61  WhereLevel.wsFla
2e33f 67 73 20 66 69 65 6c 64 20 63 61 6e 20 74 68 65  gs field can the
2e340 6e 20 62 65 20 75 73 65 64 20 61 73 0a 2a 2a 20  n be used as.** 
2e341 74 68 65 20 22 6f 70 22 20 70 61 72 61 6d 65 74  the "op" paramet
2e342 65 72 20 74 6f 20 66 69 6e 64 54 65 72 6d 20 77  er to findTerm w
2e343 68 65 6e 20 77 65 20 61 72 65 20 72 65 73 6f 6c  hen we are resol
2e344 76 69 6e 67 20 65 71 75 61 6c 69 74 79 20 63 6f  ving equality co
2e345 6e 73 74 72 61 69 6e 74 73 2e 0a 2a 2a 20 49 53  nstraints..** IS
2e346 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 73  NULL constraints
2e347 20 77 69 6c 6c 20 74 68 65 6e 20 6e 6f 74 20 62   will then not b
2e348 65 20 75 73 65 64 20 6f 6e 20 74 68 65 20 72 69  e used on the ri
2e349 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61 20 6c  ght table of a l
2e34a 65 66 74 0a 2a 2a 20 6a 6f 69 6e 2e 20 20 54 69  eft.** join.  Ti
2e34b 63 6b 65 74 73 20 23 32 31 37 37 20 61 6e 64 20  ckets #2177 and 
2e34c 23 32 31 38 39 2e 0a 2a 2f 0a 23 64 65 66 69 6e  #2189..*/.#defin
2e34d 65 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51  e WHERE_ROWID_EQ
2e34e 20 20 20 20 20 30 78 30 30 30 30 31 30 30 30 20       0x00001000 
2e34f 20 2f 2a 20 72 6f 77 69 64 3d 45 58 50 52 20 6f   /* rowid=EXPR o
2e350 72 20 72 6f 77 69 64 20 49 4e 20 28 2e 2e 2e 29  r rowid IN (...)
2e351 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
2e352 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 20 20 30  E_ROWID_RANGE  0
2e353 78 30 30 30 30 32 30 30 30 20 20 2f 2a 20 72 6f  x00002000  /* ro
2e354 77 69 64 3c 45 58 50 52 20 61 6e 64 2f 6f 72 20  wid<EXPR and/or 
2e355 72 6f 77 69 64 3e 45 58 50 52 20 2a 2f 0a 23 64  rowid>EXPR */.#d
2e356 65 66 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55  efine WHERE_COLU
2e357 4d 4e 5f 45 51 20 20 20 20 30 78 30 30 30 31 30  MN_EQ    0x00010
2e358 30 30 30 20 20 2f 2a 20 78 3d 45 58 50 52 20 6f  000  /* x=EXPR o
2e359 72 20 78 20 49 4e 20 28 2e 2e 2e 29 20 6f 72 20  r x IN (...) or 
2e35a 78 20 49 53 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65  x IS NULL */.#de
2e35b 66 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d  fine WHERE_COLUM
2e35c 4e 5f 52 41 4e 47 45 20 30 78 30 30 30 32 30 30  N_RANGE 0x000200
2e35d 30 30 20 20 2f 2a 20 78 3c 45 58 50 52 20 61 6e  00  /* x<EXPR an
2e35e 64 2f 6f 72 20 78 3e 45 58 50 52 20 2a 2f 0a 23  d/or x>EXPR */.#
2e35f 64 65 66 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c  define WHERE_COL
2e360 55 4d 4e 5f 49 4e 20 20 20 20 30 78 30 30 30 34  UMN_IN    0x0004
2e361 30 30 30 30 20 20 2f 2a 20 78 20 49 4e 20 28 2e  0000  /* x IN (.
2e362 2e 2e 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  ..) */.#define W
2e363 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c  HERE_COLUMN_NULL
2e364 20 20 30 78 30 30 30 38 30 30 30 30 20 20 2f 2a    0x00080000  /*
2e365 20 78 20 49 53 20 4e 55 4c 4c 20 2a 2f 0a 23 64   x IS NULL */.#d
2e366 65 66 69 6e 65 20 57 48 45 52 45 5f 49 4e 44 45  efine WHERE_INDE
2e367 58 45 44 20 20 20 20 20 20 30 78 30 30 30 66 30  XED      0x000f0
2e368 30 30 30 20 20 2f 2a 20 41 6e 79 74 68 69 6e 67  000  /* Anything
2e369 20 74 68 61 74 20 75 73 65 73 20 61 6e 20 69 6e   that uses an in
2e36a 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  dex */.#define W
2e36b 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 20 20 20  HERE_IN_ABLE    
2e36c 20 20 30 78 30 30 30 66 31 30 30 30 20 20 2f 2a    0x000f1000  /*
2e36d 20 41 62 6c 65 20 74 6f 20 73 75 70 70 6f 72 74   Able to support
2e36e 20 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72 20   an IN operator 
2e36f 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
2e370 5f 54 4f 50 5f 4c 49 4d 49 54 20 20 20 20 30 78  _TOP_LIMIT    0x
2e371 30 30 31 30 30 30 30 30 20 20 2f 2a 20 78 3c 45  00100000  /* x<E
2e372 58 50 52 20 6f 72 20 78 3c 3d 45 58 50 52 20 63  XPR or x<=EXPR c
2e373 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 23 64 65  onstraint */.#de
2e374 66 69 6e 65 20 57 48 45 52 45 5f 42 54 4d 5f 4c  fine WHERE_BTM_L
2e375 49 4d 49 54 20 20 20 20 30 78 30 30 32 30 30 30  IMIT    0x002000
2e376 30 30 20 20 2f 2a 20 78 3e 45 58 50 52 20 6f 72  00  /* x>EXPR or
2e377 20 78 3e 3d 45 58 50 52 20 63 6f 6e 73 74 72 61   x>=EXPR constra
2e378 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  int */.#define W
2e379 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 20 20 20  HERE_IDX_ONLY   
2e37a 20 20 30 78 30 30 38 30 30 30 30 30 20 20 2f 2a    0x00800000  /*
2e37b 20 55 73 65 20 69 6e 64 65 78 20 6f 6e 6c 79 20   Use index only 
2e37c 2d 20 6f 6d 69 74 20 74 61 62 6c 65 20 2a 2f 0a  - omit table */.
2e37d 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52  #define WHERE_OR
2e37e 44 45 52 42 59 20 20 20 20 20 20 30 78 30 31 30  DERBY      0x010
2e37f 30 30 30 30 30 20 20 2f 2a 20 4f 75 74 70 75 74  00000  /* Output
2e380 20 77 69 6c 6c 20 61 70 70 65 61 72 20 69 6e 20   will appear in 
2e381 63 6f 72 72 65 63 74 20 6f 72 64 65 72 20 2a 2f  correct order */
2e382 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 52  .#define WHERE_R
2e383 45 56 45 52 53 45 20 20 20 20 20 20 30 78 30 32  EVERSE      0x02
2e384 30 30 30 30 30 30 20 20 2f 2a 20 53 63 61 6e 20  000000  /* Scan 
2e385 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72  in reverse order
2e386 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
2e387 45 5f 55 4e 49 51 55 45 20 20 20 20 20 20 20 30  E_UNIQUE       0
2e388 78 30 34 30 30 30 30 30 30 20 20 2f 2a 20 53 65  x04000000  /* Se
2e389 6c 65 63 74 73 20 6e 6f 20 6d 6f 72 65 20 74 68  lects no more th
2e38a 61 6e 20 6f 6e 65 20 72 6f 77 20 2a 2f 0a 23 64  an one row */.#d
2e38b 65 66 69 6e 65 20 57 48 45 52 45 5f 56 49 52 54  efine WHERE_VIRT
2e38c 55 41 4c 54 41 42 4c 45 20 30 78 30 38 30 30 30  UALTABLE 0x08000
2e38d 30 30 30 20 20 2f 2a 20 55 73 65 20 76 69 72 74  000  /* Use virt
2e38e 75 61 6c 2d 74 61 62 6c 65 20 70 72 6f 63 65 73  ual-table proces
2e38f 73 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20  sing */.#define 
2e390 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 20  WHERE_MULTI_OR  
2e391 20 20 20 30 78 31 30 30 30 30 30 30 30 20 20 2f     0x10000000  /
2e392 2a 20 4f 52 20 75 73 69 6e 67 20 6d 75 6c 74 69  * OR using multi
2e393 70 6c 65 20 69 6e 64 69 63 65 73 20 2a 2f 0a 0a  ple indices */..
2e394 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  /*.** Initialize
2e395 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 65 64 20   a preallocated 
2e396 57 68 65 72 65 43 6c 61 75 73 65 20 73 74 72 75  WhereClause stru
2e397 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  cture..*/.static
2e398 20 76 6f 69 64 20 77 68 65 72 65 43 6c 61 75 73   void whereClaus
2e399 65 49 6e 69 74 28 0a 20 20 57 68 65 72 65 43 6c  eInit(.  WhereCl
2e39a 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20  ause *pWC,      
2e39b 20 20 2f 2a 20 54 68 65 20 57 68 65 72 65 43 6c    /* The WhereCl
2e39c 61 75 73 65 20 74 6f 20 62 65 20 69 6e 69 74 69  ause to be initi
2e39d 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 50 61 72 73  alized */.  Pars
2e39e 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2e39f 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
2e3a0 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2e3a1 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70   WhereMaskSet *p
2e3a2 4d 61 73 6b 53 65 74 20 20 20 2f 2a 20 4d 61 70  MaskSet   /* Map
2e3a3 70 69 6e 67 20 66 72 6f 6d 20 74 61 62 6c 65 20  ping from table 
2e3a4 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 74  cursor numbers t
2e3a5 6f 20 62 69 74 6d 61 73 6b 73 20 2a 2f 0a 29 7b  o bitmasks */.){
2e3a6 0a 20 20 70 57 43 2d 3e 70 50 61 72 73 65 20 3d  .  pWC->pParse =
2e3a7 20 70 50 61 72 73 65 3b 0a 20 20 70 57 43 2d 3e   pParse;.  pWC->
2e3a8 70 4d 61 73 6b 53 65 74 20 3d 20 70 4d 61 73 6b  pMaskSet = pMask
2e3a9 53 65 74 3b 0a 20 20 70 57 43 2d 3e 6e 54 65 72  Set;.  pWC->nTer
2e3aa 6d 20 3d 20 30 3b 0a 20 20 70 57 43 2d 3e 6e 53  m = 0;.  pWC->nS
2e3ab 6c 6f 74 20 3d 20 41 72 72 61 79 53 69 7a 65 28  lot = ArraySize(
2e3ac 70 57 43 2d 3e 61 53 74 61 74 69 63 29 3b 0a 20  pWC->aStatic);. 
2e3ad 20 70 57 43 2d 3e 61 20 3d 20 70 57 43 2d 3e 61   pWC->a = pWC->a
2e3ae 53 74 61 74 69 63 3b 0a 20 20 70 57 43 2d 3e 76  Static;.  pWC->v
2e3af 6d 61 73 6b 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20  mask = 0;.}../* 
2e3b0 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e 63  Forward referenc
2e3b1 65 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  e */.static void
2e3b2 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61   whereClauseClea
2e3b3 72 28 57 68 65 72 65 43 6c 61 75 73 65 2a 29 3b  r(WhereClause*);
2e3b4 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61  ../*.** Dealloca
2e3b5 74 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 73  te all memory as
2e3b6 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
2e3b7 57 68 65 72 65 4f 72 49 6e 66 6f 20 6f 62 6a 65  WhereOrInfo obje
2e3b8 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ct..*/.static vo
2e3b9 69 64 20 77 68 65 72 65 4f 72 49 6e 66 6f 44 65  id whereOrInfoDe
2e3ba 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62  lete(sqlite3 *db
2e3bb 2c 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 2a 70  , WhereOrInfo *p
2e3bc 29 7b 0a 20 20 77 68 65 72 65 43 6c 61 75 73 65  ){.  whereClause
2e3bd 43 6c 65 61 72 28 26 70 2d 3e 77 63 29 3b 0a 20  Clear(&p->wc);. 
2e3be 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2e3bf 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  b, p);.}../*.** 
2e3c0 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c 6c 20 6d  Deallocate all m
2e3c1 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64  emory associated
2e3c2 20 77 69 74 68 20 61 20 57 68 65 72 65 41 6e 64   with a WhereAnd
2e3c3 49 6e 66 6f 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a  Info object..*/.
2e3c4 73 74 61 74 69 63 20 76 6f 69 64 20 77 68 65 72  static void wher
2e3c5 65 41 6e 64 49 6e 66 6f 44 65 6c 65 74 65 28 73  eAndInfoDelete(s
2e3c6 71 6c 69 74 65 33 20 2a 64 62 2c 20 57 68 65 72  qlite3 *db, Wher
2e3c7 65 41 6e 64 49 6e 66 6f 20 2a 70 29 7b 0a 20 20  eAndInfo *p){.  
2e3c8 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61 72  whereClauseClear
2e3c9 28 26 70 2d 3e 77 63 29 3b 0a 20 20 73 71 6c 69  (&p->wc);.  sqli
2e3ca 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29  te3DbFree(db, p)
2e3cb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c  ;.}../*.** Deall
2e3cc 6f 63 61 74 65 20 61 20 57 68 65 72 65 43 6c 61  ocate a WhereCla
2e3cd 75 73 65 20 73 74 72 75 63 74 75 72 65 2e 20 20  use structure.  
2e3ce 54 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 20  The WhereClause 
2e3cf 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 74 73  structure.** its
2e3d0 65 6c 66 20 69 73 20 6e 6f 74 20 66 72 65 65 64  elf is not freed
2e3d1 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
2e3d2 69 73 20 74 68 65 20 69 6e 76 65 72 73 65 20 6f  is the inverse o
2e3d3 66 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 69  f whereClauseIni
2e3d4 74 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  t()..*/.static v
2e3d5 6f 69 64 20 77 68 65 72 65 43 6c 61 75 73 65 43  oid whereClauseC
2e3d6 6c 65 61 72 28 57 68 65 72 65 43 6c 61 75 73 65  lear(WhereClause
2e3d7 20 2a 70 57 43 29 7b 0a 20 20 69 6e 74 20 69 3b   *pWC){.  int i;
2e3d8 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 61 3b  .  WhereTerm *a;
2e3d9 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2e3da 20 70 57 43 2d 3e 70 50 61 72 73 65 2d 3e 64 62   pWC->pParse->db
2e3db 3b 0a 20 20 66 6f 72 28 69 3d 70 57 43 2d 3e 6e  ;.  for(i=pWC->n
2e3dc 54 65 72 6d 2d 31 2c 20 61 3d 70 57 43 2d 3e 61  Term-1, a=pWC->a
2e3dd 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 61 2b 2b  ; i>=0; i--, a++
2e3de 29 7b 0a 20 20 20 20 69 66 28 20 61 2d 3e 77 74  ){.    if( a->wt
2e3df 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 44 59 4e  Flags & TERM_DYN
2e3e0 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 73 71  AMIC ){.      sq
2e3e1 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
2e3e2 64 62 2c 20 61 2d 3e 70 45 78 70 72 29 3b 0a 20  db, a->pExpr);. 
2e3e3 20 20 20 7d 0a 20 20 20 20 69 66 28 20 61 2d 3e     }.    if( a->
2e3e4 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 4f  wtFlags & TERM_O
2e3e5 52 49 4e 46 4f 20 29 7b 0a 20 20 20 20 20 20 77  RINFO ){.      w
2e3e6 68 65 72 65 4f 72 49 6e 66 6f 44 65 6c 65 74 65  hereOrInfoDelete
2e3e7 28 64 62 2c 20 61 2d 3e 75 2e 70 4f 72 49 6e 66  (db, a->u.pOrInf
2e3e8 6f 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  o);.    }else if
2e3e9 28 20 61 2d 3e 77 74 46 6c 61 67 73 20 26 20 54  ( a->wtFlags & T
2e3ea 45 52 4d 5f 41 4e 44 49 4e 46 4f 20 29 7b 0a 20  ERM_ANDINFO ){. 
2e3eb 20 20 20 20 20 77 68 65 72 65 41 6e 64 49 6e 66       whereAndInf
2e3ec 6f 44 65 6c 65 74 65 28 64 62 2c 20 61 2d 3e 75  oDelete(db, a->u
2e3ed 2e 70 41 6e 64 49 6e 66 6f 29 3b 0a 20 20 20 20  .pAndInfo);.    
2e3ee 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 57 43 2d  }.  }.  if( pWC-
2e3ef 3e 61 21 3d 70 57 43 2d 3e 61 53 74 61 74 69 63  >a!=pWC->aStatic
2e3f0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
2e3f1 62 46 72 65 65 28 64 62 2c 20 70 57 43 2d 3e 61  bFree(db, pWC->a
2e3f2 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2e3f3 41 64 64 20 61 20 73 69 6e 67 6c 65 20 6e 65 77  Add a single new
2e3f4 20 57 68 65 72 65 54 65 72 6d 20 65 6e 74 72 79   WhereTerm entry
2e3f5 20 74 6f 20 74 68 65 20 57 68 65 72 65 43 6c 61   to the WhereCla
2e3f6 75 73 65 20 6f 62 6a 65 63 74 20 70 57 43 2e 0a  use object pWC..
2e3f7 2a 2a 20 54 68 65 20 6e 65 77 20 57 68 65 72 65  ** The new Where
2e3f8 54 65 72 6d 20 6f 62 6a 65 63 74 20 69 73 20 63  Term object is c
2e3f9 6f 6e 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20  onstructed from 
2e3fa 45 78 70 72 20 70 20 61 6e 64 20 77 69 74 68 20  Expr p and with 
2e3fb 77 74 46 6c 61 67 73 2e 0a 2a 2a 20 54 68 65 20  wtFlags..** The 
2e3fc 69 6e 64 65 78 20 69 6e 20 70 57 43 2d 3e 61 5b  index in pWC->a[
2e3fd 5d 20 6f 66 20 74 68 65 20 6e 65 77 20 57 68 65  ] of the new Whe
2e3fe 72 65 54 65 72 6d 20 69 73 20 72 65 74 75 72 6e  reTerm is return
2e3ff 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a  ed on success..*
2e400 2a 20 30 20 69 73 20 72 65 74 75 72 6e 65 64 20  * 0 is returned 
2e401 69 66 20 74 68 65 20 6e 65 77 20 57 68 65 72 65  if the new Where
2e402 54 65 72 6d 20 63 6f 75 6c 64 20 6e 6f 74 20 62  Term could not b
2e403 65 20 61 64 64 65 64 20 64 75 65 20 74 6f 20 61  e added due to a
2e404 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63   memory.** alloc
2e405 61 74 69 6f 6e 20 65 72 72 6f 72 2e 20 20 54 68  ation error.  Th
2e406 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
2e407 69 6f 6e 20 66 61 69 6c 75 72 65 20 77 69 6c 6c  ion failure will
2e408 20 62 65 20 72 65 63 6f 72 64 65 64 20 69 6e 0a   be recorded in.
2e409 2a 2a 20 74 68 65 20 64 62 2d 3e 6d 61 6c 6c 6f  ** the db->mallo
2e40a 63 46 61 69 6c 65 64 20 66 6c 61 67 20 73 6f 20  cFailed flag so 
2e40b 74 68 61 74 20 68 69 67 68 65 72 2d 6c 65 76 65  that higher-leve
2e40c 6c 20 66 75 6e 63 74 69 6f 6e 73 20 63 61 6e 20  l functions can 
2e40d 64 65 74 65 63 74 20 69 74 2e 0a 2a 2a 0a 2a 2a  detect it..**.**
2e40e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   This routine wi
2e40f 6c 6c 20 69 6e 63 72 65 61 73 65 20 74 68 65 20  ll increase the 
2e410 73 69 7a 65 20 6f 66 20 74 68 65 20 70 57 43 2d  size of the pWC-
2e411 3e 61 5b 5d 20 61 72 72 61 79 20 61 73 20 6e 65  >a[] array as ne
2e412 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 49  cessary..**.** I
2e413 66 20 74 68 65 20 77 74 46 6c 61 67 73 20 61 72  f the wtFlags ar
2e414 67 75 6d 65 6e 74 20 69 6e 63 6c 75 64 65 73 20  gument includes 
2e415 54 45 52 4d 5f 44 59 4e 41 4d 49 43 2c 20 74 68  TERM_DYNAMIC, th
2e416 65 6e 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74  en responsibilit
2e417 79 0a 2a 2a 20 66 6f 72 20 66 72 65 65 69 6e 67  y.** for freeing
2e418 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
2e419 70 20 69 73 20 61 73 73 75 6d 65 64 20 62 79 20  p is assumed by 
2e41a 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 20  the WhereClause 
2e41b 6f 62 6a 65 63 74 20 70 57 43 2e 0a 2a 2a 20 54  object pWC..** T
2e41c 68 69 73 20 69 73 20 74 72 75 65 20 65 76 65 6e  his is true even
2e41d 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   if this routine
2e41e 20 66 61 69 6c 73 20 74 6f 20 61 6c 6c 6f 63 61   fails to alloca
2e41f 74 65 20 61 20 6e 65 77 20 57 68 65 72 65 54 65  te a new WhereTe
2e420 72 6d 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e  rm..**.** WARNIN
2e421 47 3a 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  G:  This routine
2e422 20 6d 69 67 68 74 20 72 65 61 6c 6c 6f 63 61 74   might reallocat
2e423 65 20 74 68 65 20 73 70 61 63 65 20 75 73 65 64  e the space used
2e424 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 57 68 65   to store.** Whe
2e425 72 65 54 65 72 6d 73 2e 20 20 41 6c 6c 20 70 6f  reTerms.  All po
2e426 69 6e 74 65 72 73 20 74 6f 20 57 68 65 72 65 54  inters to WhereT
2e427 65 72 6d 73 20 73 68 6f 75 6c 64 20 62 65 20 69  erms should be i
2e428 6e 76 61 6c 69 64 61 74 65 64 20 61 66 74 65 72  nvalidated after
2e429 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 69 73  .** calling this
2e42a 20 72 6f 75 74 69 6e 65 2e 20 20 53 75 63 68 20   routine.  Such 
2e42b 70 6f 69 6e 74 65 72 73 20 6d 61 79 20 62 65 20  pointers may be 
2e42c 72 65 69 6e 69 74 69 61 6c 69 7a 65 64 20 62 79  reinitialized by
2e42d 20 72 65 66 65 72 65 6e 63 69 6e 67 0a 2a 2a 20   referencing.** 
2e42e 74 68 65 20 70 57 43 2d 3e 61 5b 5d 20 61 72 72  the pWC->a[] arr
2e42f 61 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ay..*/.static in
2e430 74 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73  t whereClauseIns
2e431 65 72 74 28 57 68 65 72 65 43 6c 61 75 73 65 20  ert(WhereClause 
2e432 2a 70 57 43 2c 20 45 78 70 72 20 2a 70 2c 20 75  *pWC, Expr *p, u
2e433 38 20 77 74 46 6c 61 67 73 29 7b 0a 20 20 57 68  8 wtFlags){.  Wh
2e434 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a  ereTerm *pTerm;.
2e435 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 69 66 28    int idx;.  if(
2e436 20 70 57 43 2d 3e 6e 54 65 72 6d 3e 3d 70 57 43   pWC->nTerm>=pWC
2e437 2d 3e 6e 53 6c 6f 74 20 29 7b 0a 20 20 20 20 57  ->nSlot ){.    W
2e438 68 65 72 65 54 65 72 6d 20 2a 70 4f 6c 64 20 3d  hereTerm *pOld =
2e439 20 70 57 43 2d 3e 61 3b 0a 20 20 20 20 73 71 6c   pWC->a;.    sql
2e43a 69 74 65 33 20 2a 64 62 20 3d 20 70 57 43 2d 3e  ite3 *db = pWC->
2e43b 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20  pParse->db;.    
2e43c 70 57 43 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33  pWC->a = sqlite3
2e43d 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
2e43e 73 69 7a 65 6f 66 28 70 57 43 2d 3e 61 5b 30 5d  sizeof(pWC->a[0]
2e43f 29 2a 70 57 43 2d 3e 6e 53 6c 6f 74 2a 32 20 29  )*pWC->nSlot*2 )
2e440 3b 0a 20 20 20 20 69 66 28 20 70 57 43 2d 3e 61  ;.    if( pWC->a
2e441 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ==0 ){.      if(
2e442 20 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f   wtFlags & TERM_
2e443 44 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20  DYNAMIC ){.     
2e444 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
2e445 6c 65 74 65 28 64 62 2c 20 70 29 3b 0a 20 20 20  lete(db, p);.   
2e446 20 20 20 7d 0a 20 20 20 20 20 20 70 57 43 2d 3e     }.      pWC->
2e447 61 20 3d 20 70 4f 6c 64 3b 0a 20 20 20 20 20 20  a = pOld;.      
2e448 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a  return 0;.    }.
2e449 20 20 20 20 6d 65 6d 63 70 79 28 70 57 43 2d 3e      memcpy(pWC->
2e44a 61 2c 20 70 4f 6c 64 2c 20 73 69 7a 65 6f 66 28  a, pOld, sizeof(
2e44b 70 57 43 2d 3e 61 5b 30 5d 29 2a 70 57 43 2d 3e  pWC->a[0])*pWC->
2e44c 6e 54 65 72 6d 29 3b 0a 20 20 20 20 69 66 28 20  nTerm);.    if( 
2e44d 70 4f 6c 64 21 3d 70 57 43 2d 3e 61 53 74 61 74  pOld!=pWC->aStat
2e44e 69 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ic ){.      sqli
2e44f 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4f  te3DbFree(db, pO
2e450 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ld);.    }.    p
2e451 57 43 2d 3e 6e 53 6c 6f 74 20 3d 20 73 71 6c 69  WC->nSlot = sqli
2e452 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28  te3DbMallocSize(
2e453 64 62 2c 20 70 57 43 2d 3e 61 29 2f 73 69 7a 65  db, pWC->a)/size
2e454 6f 66 28 70 57 43 2d 3e 61 5b 30 5d 29 3b 0a 20  of(pWC->a[0]);. 
2e455 20 7d 0a 20 20 70 54 65 72 6d 20 3d 20 26 70 57   }.  pTerm = &pW
2e456 43 2d 3e 61 5b 69 64 78 20 3d 20 70 57 43 2d 3e  C->a[idx = pWC->
2e457 6e 54 65 72 6d 2b 2b 5d 3b 0a 20 20 70 54 65 72  nTerm++];.  pTer
2e458 6d 2d 3e 70 45 78 70 72 20 3d 20 70 3b 0a 20 20  m->pExpr = p;.  
2e459 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 3d  pTerm->wtFlags =
2e45a 20 77 74 46 6c 61 67 73 3b 0a 20 20 70 54 65 72   wtFlags;.  pTer
2e45b 6d 2d 3e 70 57 43 20 3d 20 70 57 43 3b 0a 20 20  m->pWC = pWC;.  
2e45c 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 20 3d  pTerm->iParent =
2e45d 20 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20 69 64   -1;.  return id
2e45e 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  x;.}../*.** This
2e45f 20 72 6f 75 74 69 6e 65 20 69 64 65 6e 74 69 66   routine identif
2e460 69 65 73 20 73 75 62 65 78 70 72 65 73 73 69 6f  ies subexpressio
2e461 6e 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20  ns in the WHERE 
2e462 63 6c 61 75 73 65 20 77 68 65 72 65 0a 2a 2a 20  clause where.** 
2e463 65 61 63 68 20 73 75 62 65 78 70 72 65 73 73 69  each subexpressi
2e464 6f 6e 20 69 73 20 73 65 70 61 72 61 74 65 64 20  on is separated 
2e465 62 79 20 74 68 65 20 41 4e 44 20 6f 70 65 72 61  by the AND opera
2e466 74 6f 72 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65  tor or some othe
2e467 72 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 73 70  r.** operator sp
2e468 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 6f  ecified in the o
2e469 70 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68  p parameter.  Th
2e46a 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 73 74  e WhereClause st
2e46b 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 66 69  ructure.** is fi
2e46c 6c 6c 65 64 20 77 69 74 68 20 70 6f 69 6e 74 65  lled with pointe
2e46d 72 73 20 74 6f 20 73 75 62 65 78 70 72 65 73 73  rs to subexpress
2e46e 69 6f 6e 73 2e 20 20 46 6f 72 20 65 78 61 6d 70  ions.  For examp
2e46f 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 57 48 45  le:.**.**    WHE
2e470 52 45 20 20 61 3d 3d 27 68 65 6c 6c 6f 27 20 41  RE  a=='hello' A
2e471 4e 44 20 63 6f 61 6c 65 73 63 65 28 62 2c 31 31  ND coalesce(b,11
2e472 29 3c 31 30 20 41 4e 44 20 28 63 2b 31 32 21 3d  )<10 AND (c+12!=
2e473 64 20 4f 52 20 63 3d 3d 32 32 29 0a 2a 2a 20 20  d OR c==22).**  
2e474 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f           \______
2e475 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f  __/     \_______
2e476 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f  ________/     \_
2e477 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f  _______________/
2e478 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 73  .**            s
2e479 6c 6f 74 5b 30 5d 20 20 20 20 20 20 20 20 20 20  lot[0]          
2e47a 20 20 73 6c 6f 74 5b 31 5d 20 20 20 20 20 20 20    slot[1]       
2e47b 20 20 20 20 20 20 20 20 73 6c 6f 74 5b 32 5d 0a          slot[2].
2e47c 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e  **.** The origin
2e47d 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  al WHERE clause 
2e47e 69 6e 20 70 45 78 70 72 20 69 73 20 75 6e 61 6c  in pExpr is unal
2e47f 74 65 72 65 64 2e 20 20 41 6c 6c 20 74 68 69 73  tered.  All this
2e480 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64 6f 65 73   routine.** does
2e481 20 69 73 20 6d 61 6b 65 20 73 6c 6f 74 5b 5d 20   is make slot[] 
2e482 65 6e 74 72 69 65 73 20 70 6f 69 6e 74 20 74 6f  entries point to
2e483 20 73 75 62 73 74 72 75 63 74 75 72 65 20 77 69   substructure wi
2e484 74 68 69 6e 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a  thin pExpr..**.*
2e485 2a 20 49 6e 20 74 68 65 20 70 72 65 76 69 6f 75  * In the previou
2e486 73 20 73 65 6e 74 65 6e 63 65 20 61 6e 64 20 69  s sentence and i
2e487 6e 20 74 68 65 20 64 69 61 67 72 61 6d 2c 20 22  n the diagram, "
2e488 73 6c 6f 74 5b 5d 22 20 72 65 66 65 72 73 20 74  slot[]" refers t
2e489 6f 0a 2a 2a 20 74 68 65 20 57 68 65 72 65 43 6c  o.** the WhereCl
2e48a 61 75 73 65 2e 61 5b 5d 20 61 72 72 61 79 2e 20  ause.a[] array. 
2e48b 20 54 68 65 20 73 6c 6f 74 5b 5d 20 61 72 72 61   The slot[] arra
2e48c 79 20 67 72 6f 77 73 20 61 73 20 6e 65 65 64 65  y grows as neede
2e48d 64 20 74 6f 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20  d to contain.** 
2e48e 61 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 65  all terms of the
2e48f 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 2a   WHERE clause..*
2e490 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 68  /.static void wh
2e491 65 72 65 53 70 6c 69 74 28 57 68 65 72 65 43 6c  ereSplit(WhereCl
2e492 61 75 73 65 20 2a 70 57 43 2c 20 45 78 70 72 20  ause *pWC, Expr 
2e493 2a 70 45 78 70 72 2c 20 69 6e 74 20 6f 70 29 7b  *pExpr, int op){
2e494 0a 20 20 70 57 43 2d 3e 6f 70 20 3d 20 28 75 38  .  pWC->op = (u8
2e495 29 6f 70 3b 0a 20 20 69 66 28 20 70 45 78 70 72  )op;.  if( pExpr
2e496 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
2e497 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 6f  if( pExpr->op!=o
2e498 70 20 29 7b 0a 20 20 20 20 77 68 65 72 65 43 6c  p ){.    whereCl
2e499 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20  auseInsert(pWC, 
2e49a 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 7d 65 6c  pExpr, 0);.  }el
2e49b 73 65 7b 0a 20 20 20 20 77 68 65 72 65 53 70 6c  se{.    whereSpl
2e49c 69 74 28 70 57 43 2c 20 70 45 78 70 72 2d 3e 70  it(pWC, pExpr->p
2e49d 4c 65 66 74 2c 20 6f 70 29 3b 0a 20 20 20 20 77  Left, op);.    w
2e49e 68 65 72 65 53 70 6c 69 74 28 70 57 43 2c 20 70  hereSplit(pWC, p
2e49f 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70  Expr->pRight, op
2e4a0 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2e4a1 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 20 65 78  Initialize an ex
2e4a2 70 72 65 73 73 69 6f 6e 20 6d 61 73 6b 20 73 65  pression mask se
2e4a3 74 20 28 61 20 57 68 65 72 65 4d 61 73 6b 53 65  t (a WhereMaskSe
2e4a4 74 20 6f 62 6a 65 63 74 29 0a 2a 2f 0a 23 64 65  t object).*/.#de
2e4a5 66 69 6e 65 20 69 6e 69 74 4d 61 73 6b 53 65 74  fine initMaskSet
2e4a6 28 50 29 20 20 6d 65 6d 73 65 74 28 50 2c 20 30  (P)  memset(P, 0
2e4a7 2c 20 73 69 7a 65 6f 66 28 2a 50 29 29 0a 0a 2f  , sizeof(*P))../
2e4a8 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
2e4a9 62 69 74 6d 61 73 6b 20 66 6f 72 20 74 68 65 20  bitmask for the 
2e4aa 67 69 76 65 6e 20 63 75 72 73 6f 72 20 6e 75 6d  given cursor num
2e4ab 62 65 72 2e 20 20 52 65 74 75 72 6e 20 30 20 69  ber.  Return 0 i
2e4ac 66 0a 2a 2a 20 69 43 75 72 73 6f 72 20 69 73 20  f.** iCursor is 
2e4ad 6e 6f 74 20 69 6e 20 74 68 65 20 73 65 74 2e 0a  not in the set..
2e4ae 2a 2f 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73  */.static Bitmas
2e4af 6b 20 67 65 74 4d 61 73 6b 28 57 68 65 72 65 4d  k getMask(WhereM
2e4b0 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74  askSet *pMaskSet
2e4b1 2c 20 69 6e 74 20 69 43 75 72 73 6f 72 29 7b 0a  , int iCursor){.
2e4b2 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72    int i;.  asser
2e4b3 74 28 20 70 4d 61 73 6b 53 65 74 2d 3e 6e 3c 3d  t( pMaskSet->n<=
2e4b4 73 69 7a 65 6f 66 28 42 69 74 6d 61 73 6b 29 2a  sizeof(Bitmask)*
2e4b5 38 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  8 );.  for(i=0; 
2e4b6 69 3c 70 4d 61 73 6b 53 65 74 2d 3e 6e 3b 20 69  i<pMaskSet->n; i
2e4b7 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 4d 61  ++){.    if( pMa
2e4b8 73 6b 53 65 74 2d 3e 69 78 5b 69 5d 3d 3d 69 43  skSet->ix[i]==iC
2e4b9 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 72  ursor ){.      r
2e4ba 65 74 75 72 6e 20 28 28 42 69 74 6d 61 73 6b 29  eturn ((Bitmask)
2e4bb 31 29 3c 3c 69 3b 0a 20 20 20 20 7d 0a 20 20 7d  1)<<i;.    }.  }
2e4bc 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
2e4bd 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e  /*.** Create a n
2e4be 65 77 20 6d 61 73 6b 20 66 6f 72 20 63 75 72 73  ew mask for curs
2e4bf 6f 72 20 69 43 75 72 73 6f 72 2e 0a 2a 2a 0a 2a  or iCursor..**.*
2e4c0 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20 63  * There is one c
2e4c1 75 72 73 6f 72 20 70 65 72 20 74 61 62 6c 65 20  ursor per table 
2e4c2 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
2e4c3 73 65 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20  se.  The number 
2e4c4 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20  of.** tables in 
2e4c5 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2e4c6 69 73 20 6c 69 6d 69 74 65 64 20 62 79 20 61 20  is limited by a 
2e4c7 74 65 73 74 20 65 61 72 6c 79 20 69 6e 20 74 68  test early in th
2e4c8 65 0a 2a 2a 20 73 71 6c 69 74 65 33 57 68 65 72  e.** sqlite3Wher
2e4c9 65 42 65 67 69 6e 28 29 20 72 6f 75 74 69 6e 65  eBegin() routine
2e4ca 2e 20 20 53 6f 20 77 65 20 6b 6e 6f 77 20 74 68  .  So we know th
2e4cb 61 74 20 74 68 65 20 70 4d 61 73 6b 53 65 74 2d  at the pMaskSet-
2e4cc 3e 69 78 5b 5d 0a 2a 2a 20 61 72 72 61 79 20 77  >ix[].** array w
2e4cd 69 6c 6c 20 6e 65 76 65 72 20 6f 76 65 72 66 6c  ill never overfl
2e4ce 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ow..*/.static vo
2e4cf 69 64 20 63 72 65 61 74 65 4d 61 73 6b 28 57 68  id createMask(Wh
2e4d0 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73  ereMaskSet *pMas
2e4d1 6b 53 65 74 2c 20 69 6e 74 20 69 43 75 72 73 6f  kSet, int iCurso
2e4d2 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d  r){.  assert( pM
2e4d3 61 73 6b 53 65 74 2d 3e 6e 20 3c 20 41 72 72 61  askSet->n < Arra
2e4d4 79 53 69 7a 65 28 70 4d 61 73 6b 53 65 74 2d 3e  ySize(pMaskSet->
2e4d5 69 78 29 20 29 3b 0a 20 20 70 4d 61 73 6b 53 65  ix) );.  pMaskSe
2e4d6 74 2d 3e 69 78 5b 70 4d 61 73 6b 53 65 74 2d 3e  t->ix[pMaskSet->
2e4d7 6e 2b 2b 5d 20 3d 20 69 43 75 72 73 6f 72 3b 0a  n++] = iCursor;.
2e4d8 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
2e4d9 75 74 69 6e 65 20 77 61 6c 6b 73 20 28 72 65 63  utine walks (rec
2e4da 75 72 73 69 76 65 6c 79 29 20 61 6e 20 65 78 70  ursively) an exp
2e4db 72 65 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64  ression tree and
2e4dc 20 67 65 6e 65 72 61 74 65 73 0a 2a 2a 20 61 20   generates.** a 
2e4dd 62 69 74 6d 61 73 6b 20 69 6e 64 69 63 61 74 69  bitmask indicati
2e4de 6e 67 20 77 68 69 63 68 20 74 61 62 6c 65 73 20  ng which tables 
2e4df 61 72 65 20 75 73 65 64 20 69 6e 20 74 68 61 74  are used in that
2e4e0 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74   expression.** t
2e4e1 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72  ree..**.** In or
2e4e2 64 65 72 20 66 6f 72 20 74 68 69 73 20 72 6f 75  der for this rou
2e4e3 74 69 6e 65 20 74 6f 20 77 6f 72 6b 2c 20 74 68  tine to work, th
2e4e4 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
2e4e5 6f 6e 20 6d 75 73 74 20 68 61 76 65 0a 2a 2a 20  on must have.** 
2e4e6 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 76 6f 6b  previously invok
2e4e7 65 64 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  ed sqlite3Resolv
2e4e8 65 45 78 70 72 4e 61 6d 65 73 28 29 20 6f 6e 20  eExprNames() on 
2e4e9 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 20  the expression. 
2e4ea 20 53 65 65 0a 2a 2a 20 74 68 65 20 68 65 61 64   See.** the head
2e4eb 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68  er comment on th
2e4ec 61 74 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 61  at routine for a
2e4ed 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
2e4ee 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 73 71  ation..** The sq
2e4ef 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
2e4f0 4e 61 6d 65 73 28 29 20 72 6f 75 74 69 6e 65 73  Names() routines
2e4f1 20 6c 6f 6f 6b 73 20 66 6f 72 20 63 6f 6c 75 6d   looks for colum
2e4f2 6e 20 6e 61 6d 65 73 20 61 6e 64 0a 2a 2a 20 73  n names and.** s
2e4f3 65 74 73 20 74 68 65 69 72 20 6f 70 63 6f 64 65  ets their opcode
2e4f4 73 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 61  s to TK_COLUMN a
2e4f5 6e 64 20 74 68 65 69 72 20 45 78 70 72 2e 69 54  nd their Expr.iT
2e4f6 61 62 6c 65 20 66 69 65 6c 64 73 20 74 6f 0a 2a  able fields to.*
2e4f7 2a 20 74 68 65 20 56 44 42 45 20 63 75 72 73 6f  * the VDBE curso
2e4f8 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  r number of the 
2e4f9 74 61 62 6c 65 2e 20 20 54 68 69 73 20 72 6f 75  table.  This rou
2e4fa 74 69 6e 65 20 6a 75 73 74 20 68 61 73 20 74 6f  tine just has to
2e4fb 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 65 20 74 68  .** translate th
2e4fc 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73  e cursor numbers
2e4fd 20 69 6e 74 6f 20 62 69 74 6d 61 73 6b 20 76 61   into bitmask va
2e4fe 6c 75 65 73 20 61 6e 64 20 4f 52 20 61 6c 6c 0a  lues and OR all.
2e4ff 2a 2a 20 74 68 65 20 62 69 74 6d 61 73 6b 73 20  ** the bitmasks 
2e500 74 6f 67 65 74 68 65 72 2e 0a 2a 2f 0a 73 74 61  together..*/.sta
2e501 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72  tic Bitmask expr
2e502 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 57  ListTableUsage(W
2e503 68 65 72 65 4d 61 73 6b 53 65 74 2a 2c 20 45 78  hereMaskSet*, Ex
2e504 70 72 4c 69 73 74 2a 29 3b 0a 73 74 61 74 69 63  prList*);.static
2e505 20 42 69 74 6d 61 73 6b 20 65 78 70 72 53 65 6c   Bitmask exprSel
2e506 65 63 74 54 61 62 6c 65 55 73 61 67 65 28 57 68  ectTableUsage(Wh
2e507 65 72 65 4d 61 73 6b 53 65 74 2a 2c 20 53 65 6c  ereMaskSet*, Sel
2e508 65 63 74 2a 29 3b 0a 73 74 61 74 69 63 20 42 69  ect*);.static Bi
2e509 74 6d 61 73 6b 20 65 78 70 72 54 61 62 6c 65 55  tmask exprTableU
2e50a 73 61 67 65 28 57 68 65 72 65 4d 61 73 6b 53 65  sage(WhereMaskSe
2e50b 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 45 78 70  t *pMaskSet, Exp
2e50c 72 20 2a 70 29 7b 0a 20 20 42 69 74 6d 61 73 6b  r *p){.  Bitmask
2e50d 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 69 66 28   mask = 0;.  if(
2e50e 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   p==0 ) return 0
2e50f 3b 0a 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 54  ;.  if( p->op==T
2e510 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20  K_COLUMN ){.    
2e511 6d 61 73 6b 20 3d 20 67 65 74 4d 61 73 6b 28 70  mask = getMask(p
2e512 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 69 54 61 62  MaskSet, p->iTab
2e513 6c 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  le);.    return 
2e514 6d 61 73 6b 3b 0a 20 20 7d 0a 20 20 6d 61 73 6b  mask;.  }.  mask
2e515 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67   = exprTableUsag
2e516 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 70  e(pMaskSet, p->p
2e517 52 69 67 68 74 29 3b 0a 20 20 6d 61 73 6b 20 7c  Right);.  mask |
2e518 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65  = exprTableUsage
2e519 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 70 4c  (pMaskSet, p->pL
2e51a 65 66 74 29 3b 0a 20 20 69 66 28 20 45 78 70 72  eft);.  if( Expr
2e51b 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45  HasProperty(p, E
2e51c 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a  P_xIsSelect) ){.
2e51d 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72      mask |= expr
2e51e 53 65 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65  SelectTableUsage
2e51f 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 78 2e  (pMaskSet, p->x.
2e520 70 53 65 6c 65 63 74 29 3b 0a 20 20 7d 65 6c 73  pSelect);.  }els
2e521 65 7b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65  e{.    mask |= e
2e522 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67  xprListTableUsag
2e523 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 78  e(pMaskSet, p->x
2e524 2e 70 4c 69 73 74 29 3b 0a 20 20 7d 0a 20 20 72  .pList);.  }.  r
2e525 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 73 74  eturn mask;.}.st
2e526 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78 70  atic Bitmask exp
2e527 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28  rListTableUsage(
2e528 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d  WhereMaskSet *pM
2e529 61 73 6b 53 65 74 2c 20 45 78 70 72 4c 69 73 74  askSet, ExprList
2e52a 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20   *pList){.  int 
2e52b 69 3b 0a 20 20 42 69 74 6d 61 73 6b 20 6d 61 73  i;.  Bitmask mas
2e52c 6b 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 4c 69  k = 0;.  if( pLi
2e52d 73 74 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  st ){.    for(i=
2e52e 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70  0; i<pList->nExp
2e52f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d  r; i++){.      m
2e530 61 73 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c 65  ask |= exprTable
2e531 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
2e532 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70  pList->a[i].pExp
2e533 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  r);.    }.  }.  
2e534 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 73  return mask;.}.s
2e535 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78  tatic Bitmask ex
2e536 70 72 53 65 6c 65 63 74 54 61 62 6c 65 55 73 61  prSelectTableUsa
2e537 67 65 28 57 68 65 72 65 4d 61 73 6b 53 65 74 20  ge(WhereMaskSet 
2e538 2a 70 4d 61 73 6b 53 65 74 2c 20 53 65 6c 65 63  *pMaskSet, Selec
2e539 74 20 2a 70 53 29 7b 0a 20 20 42 69 74 6d 61 73  t *pS){.  Bitmas
2e53a 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 77 68  k mask = 0;.  wh
2e53b 69 6c 65 28 20 70 53 20 29 7b 0a 20 20 20 20 6d  ile( pS ){.    m
2e53c 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54  ask |= exprListT
2e53d 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
2e53e 65 74 2c 20 70 53 2d 3e 70 45 4c 69 73 74 29 3b  et, pS->pEList);
2e53f 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  .    mask |= exp
2e540 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28  rListTableUsage(
2e541 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 47  pMaskSet, pS->pG
2e542 72 6f 75 70 42 79 29 3b 0a 20 20 20 20 6d 61 73  roupBy);.    mas
2e543 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62  k |= exprListTab
2e544 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
2e545 2c 20 70 53 2d 3e 70 4f 72 64 65 72 42 79 29 3b  , pS->pOrderBy);
2e546 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  .    mask |= exp
2e547 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73  rTableUsage(pMas
2e548 6b 53 65 74 2c 20 70 53 2d 3e 70 57 68 65 72 65  kSet, pS->pWhere
2e549 29 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65  );.    mask |= e
2e54a 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d  xprTableUsage(pM
2e54b 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 48 61 76  askSet, pS->pHav
2e54c 69 6e 67 29 3b 0a 20 20 20 20 70 53 20 3d 20 70  ing);.    pS = p
2e54d 53 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20  S->pPrior;.  }. 
2e54e 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a   return mask;.}.
2e54f 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
2e550 55 45 20 69 66 20 74 68 65 20 67 69 76 65 6e 20  UE if the given 
2e551 6f 70 65 72 61 74 6f 72 20 69 73 20 6f 6e 65 20  operator is one 
2e552 6f 66 20 74 68 65 20 6f 70 65 72 61 74 6f 72 73  of the operators
2e553 20 74 68 61 74 20 69 73 0a 2a 2a 20 61 6c 6c 6f   that is.** allo
2e554 77 65 64 20 66 6f 72 20 61 6e 20 69 6e 64 65 78  wed for an index
2e555 61 62 6c 65 20 57 48 45 52 45 20 63 6c 61 75 73  able WHERE claus
2e556 65 20 74 65 72 6d 2e 20 20 54 68 65 20 61 6c 6c  e term.  The all
2e557 6f 77 65 64 20 6f 70 65 72 61 74 6f 72 73 20 61  owed operators a
2e558 72 65 0a 2a 2a 20 22 3d 22 2c 20 22 3c 22 2c 20  re.** "=", "<", 
2e559 22 3e 22 2c 20 22 3c 3d 22 2c 20 22 3e 3d 22 2c  ">", "<=", ">=",
2e55a 20 61 6e 64 20 22 49 4e 22 2e 0a 2a 2f 0a 73 74   and "IN"..*/.st
2e55b 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f 77 65 64  atic int allowed
2e55c 4f 70 28 69 6e 74 20 6f 70 29 7b 0a 20 20 61 73  Op(int op){.  as
2e55d 73 65 72 74 28 20 54 4b 5f 47 54 3e 54 4b 5f 45  sert( TK_GT>TK_E
2e55e 51 20 26 26 20 54 4b 5f 47 54 3c 54 4b 5f 47 45  Q && TK_GT<TK_GE
2e55f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 54 4b   );.  assert( TK
2e560 5f 4c 54 3e 54 4b 5f 45 51 20 26 26 20 54 4b 5f  _LT>TK_EQ && TK_
2e561 4c 54 3c 54 4b 5f 47 45 20 29 3b 0a 20 20 61 73  LT<TK_GE );.  as
2e562 73 65 72 74 28 20 54 4b 5f 4c 45 3e 54 4b 5f 45  sert( TK_LE>TK_E
2e563 51 20 26 26 20 54 4b 5f 4c 45 3c 54 4b 5f 47 45  Q && TK_LE<TK_GE
2e564 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 54 4b   );.  assert( TK
2e565 5f 47 45 3d 3d 54 4b 5f 45 51 2b 34 20 29 3b 0a  _GE==TK_EQ+4 );.
2e566 20 20 72 65 74 75 72 6e 20 6f 70 3d 3d 54 4b 5f    return op==TK_
2e567 49 4e 20 7c 7c 20 28 6f 70 3e 3d 54 4b 5f 45 51  IN || (op>=TK_EQ
2e568 20 26 26 20 6f 70 3c 3d 54 4b 5f 47 45 29 20 7c   && op<=TK_GE) |
2e569 7c 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 3b  | op==TK_ISNULL;
2e56a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70 20 74  .}../*.** Swap t
2e56b 77 6f 20 6f 62 6a 65 63 74 73 20 6f 66 20 74 79  wo objects of ty
2e56c 70 65 20 54 59 50 45 2e 0a 2a 2f 0a 23 64 65 66  pe TYPE..*/.#def
2e56d 69 6e 65 20 53 57 41 50 28 54 59 50 45 2c 41 2c  ine SWAP(TYPE,A,
2e56e 42 29 20 7b 54 59 50 45 20 74 3d 41 3b 20 41 3d  B) {TYPE t=A; A=
2e56f 42 3b 20 42 3d 74 3b 7d 0a 0a 2f 2a 0a 2a 2a 20  B; B=t;}../*.** 
2e570 43 6f 6d 6d 75 74 65 20 61 20 63 6f 6d 70 61 72  Commute a compar
2e571 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20  ison operator.  
2e572 45 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74  Expressions of t
2e573 68 65 20 66 6f 72 6d 20 22 58 20 6f 70 20 59 22  he form "X op Y"
2e574 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65  .** are converte
2e575 64 20 69 6e 74 6f 20 22 59 20 6f 70 20 58 22 2e  d into "Y op X".
2e576 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 63 6f 6c 6c  .**.** If a coll
2e577 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69  ation sequence i
2e578 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
2e579 68 20 65 69 74 68 65 72 20 74 68 65 20 6c 65 66  h either the lef
2e57a 74 20 6f 72 20 72 69 67 68 74 0a 2a 2a 20 73 69  t or right.** si
2e57b 64 65 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72  de of the compar
2e57c 69 73 6f 6e 2c 20 69 74 20 72 65 6d 61 69 6e 73  ison, it remains
2e57d 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2e57e 20 74 68 65 20 73 61 6d 65 20 73 69 64 65 20 61   the same side a
2e57f 66 74 65 72 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d  fter.** the comm
2e580 75 74 61 74 69 6f 6e 2e 20 53 6f 20 22 59 20 63  utation. So "Y c
2e581 6f 6c 6c 61 74 65 20 4e 4f 43 41 53 45 20 6f 70  ollate NOCASE op
2e582 20 58 22 20 62 65 63 6f 6d 65 73 20 0a 2a 2a 20   X" becomes .** 
2e583 22 58 20 63 6f 6c 6c 61 74 65 20 4e 4f 43 41 53  "X collate NOCAS
2e584 45 20 6f 70 20 59 22 2e 20 54 68 69 73 20 69 73  E op Y". This is
2e585 20 62 65 63 61 75 73 65 20 61 6e 79 20 63 6f 6c   because any col
2e586 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
2e587 6f 6e 0a 2a 2a 20 74 68 65 20 6c 65 66 74 20 68  on.** the left h
2e588 61 6e 64 20 73 69 64 65 20 6f 66 20 61 20 63 6f  and side of a co
2e589 6d 70 61 72 69 73 6f 6e 20 6f 76 65 72 72 69 64  mparison overrid
2e58a 65 73 20 61 6e 79 20 63 6f 6c 6c 61 74 69 6f 6e  es any collation
2e58b 20 73 65 71 75 65 6e 63 65 20 0a 2a 2a 20 61 74   sequence .** at
2e58c 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 72 69  tached to the ri
2e58d 67 68 74 2e 20 46 6f 72 20 74 68 65 20 73 61 6d  ght. For the sam
2e58e 65 20 72 65 61 73 6f 6e 20 74 68 65 20 45 50 5f  e reason the EP_
2e58f 45 78 70 43 6f 6c 6c 61 74 65 20 66 6c 61 67 0a  ExpCollate flag.
2e590 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 6d 6d 75 74  ** is not commut
2e591 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
2e592 69 64 20 65 78 70 72 43 6f 6d 6d 75 74 65 28 50  id exprCommute(P
2e593 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
2e594 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 75 31  pr *pExpr){.  u1
2e595 36 20 65 78 70 52 69 67 68 74 20 3d 20 28 70 45  6 expRight = (pE
2e596 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 66 6c 61  xpr->pRight->fla
2e597 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61  gs & EP_ExpColla
2e598 74 65 29 3b 0a 20 20 75 31 36 20 65 78 70 4c 65  te);.  u16 expLe
2e599 66 74 20 3d 20 28 70 45 78 70 72 2d 3e 70 4c 65  ft = (pExpr->pLe
2e59a 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45  ft->flags & EP_E
2e59b 78 70 43 6f 6c 6c 61 74 65 29 3b 0a 20 20 61 73  xpCollate);.  as
2e59c 73 65 72 74 28 20 61 6c 6c 6f 77 65 64 4f 70 28  sert( allowedOp(
2e59d 70 45 78 70 72 2d 3e 6f 70 29 20 26 26 20 70 45  pExpr->op) && pE
2e59e 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 4e 20 29  xpr->op!=TK_IN )
2e59f 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68  ;.  pExpr->pRigh
2e5a0 74 2d 3e 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  t->pColl = sqlit
2e5a1 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50  e3ExprCollSeq(pP
2e5a2 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69  arse, pExpr->pRi
2e5a3 67 68 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70  ght);.  pExpr->p
2e5a4 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20 3d 20 73 71  Left->pColl = sq
2e5a5 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
2e5a6 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
2e5a7 70 4c 65 66 74 29 3b 0a 20 20 53 57 41 50 28 43  pLeft);.  SWAP(C
2e5a8 6f 6c 6c 53 65 71 2a 2c 70 45 78 70 72 2d 3e 70  ollSeq*,pExpr->p
2e5a9 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 2c 70 45 78  Right->pColl,pEx
2e5aa 70 72 2d 3e 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c  pr->pLeft->pColl
2e5ab 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52 69 67  );.  pExpr->pRig
2e5ac 68 74 2d 3e 66 6c 61 67 73 20 3d 20 28 70 45 78  ht->flags = (pEx
2e5ad 70 72 2d 3e 70 52 69 67 68 74 2d 3e 66 6c 61 67  pr->pRight->flag
2e5ae 73 20 26 20 7e 45 50 5f 45 78 70 43 6f 6c 6c 61  s & ~EP_ExpColla
2e5af 74 65 29 20 7c 20 65 78 70 4c 65 66 74 3b 0a 20  te) | expLeft;. 
2e5b0 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 66   pExpr->pLeft->f
2e5b1 6c 61 67 73 20 3d 20 28 70 45 78 70 72 2d 3e 70  lags = (pExpr->p
2e5b2 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 7e 45  Left->flags & ~E
2e5b3 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29 20 7c 20  P_ExpCollate) | 
2e5b4 65 78 70 52 69 67 68 74 3b 0a 20 20 53 57 41 50  expRight;.  SWAP
2e5b5 28 45 78 70 72 2a 2c 70 45 78 70 72 2d 3e 70 52  (Expr*,pExpr->pR
2e5b6 69 67 68 74 2c 70 45 78 70 72 2d 3e 70 4c 65 66  ight,pExpr->pLef
2e5b7 74 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d  t);.  if( pExpr-
2e5b8 3e 6f 70 3e 3d 54 4b 5f 47 54 20 29 7b 0a 20 20  >op>=TK_GT ){.  
2e5b9 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d    assert( TK_LT=
2e5ba 3d 54 4b 5f 47 54 2b 32 20 29 3b 0a 20 20 20 20  =TK_GT+2 );.    
2e5bb 61 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 54  assert( TK_GE==T
2e5bc 4b 5f 4c 45 2b 32 20 29 3b 0a 20 20 20 20 61 73  K_LE+2 );.    as
2e5bd 73 65 72 74 28 20 54 4b 5f 47 54 3e 54 4b 5f 45  sert( TK_GT>TK_E
2e5be 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  Q );.    assert(
2e5bf 20 54 4b 5f 47 54 3c 54 4b 5f 4c 45 20 29 3b 0a   TK_GT<TK_LE );.
2e5c0 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
2e5c1 72 2d 3e 6f 70 3e 3d 54 4b 5f 47 54 20 26 26 20  r->op>=TK_GT && 
2e5c2 70 45 78 70 72 2d 3e 6f 70 3c 3d 54 4b 5f 47 45  pExpr->op<=TK_GE
2e5c3 20 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f   );.    pExpr->o
2e5c4 70 20 3d 20 28 28 70 45 78 70 72 2d 3e 6f 70 2d  p = ((pExpr->op-
2e5c5 54 4b 5f 47 54 29 5e 32 29 2b 54 4b 5f 47 54 3b  TK_GT)^2)+TK_GT;
2e5c6 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72  .  }.}../*.** Tr
2e5c7 61 6e 73 6c 61 74 65 20 66 72 6f 6d 20 54 4b 5f  anslate from TK_
2e5c8 78 78 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 57  xx operator to W
2e5c9 4f 5f 78 78 20 62 69 74 6d 61 73 6b 2e 0a 2a 2f  O_xx bitmask..*/
2e5ca 0a 73 74 61 74 69 63 20 75 31 36 20 6f 70 65 72  .static u16 oper
2e5cb 61 74 6f 72 4d 61 73 6b 28 69 6e 74 20 6f 70 29  atorMask(int op)
2e5cc 7b 0a 20 20 75 31 36 20 63 3b 0a 20 20 61 73 73  {.  u16 c;.  ass
2e5cd 65 72 74 28 20 61 6c 6c 6f 77 65 64 4f 70 28 6f  ert( allowedOp(o
2e5ce 70 29 20 29 3b 0a 20 20 69 66 28 20 6f 70 3d 3d  p) );.  if( op==
2e5cf 54 4b 5f 49 4e 20 29 7b 0a 20 20 20 20 63 20 3d  TK_IN ){.    c =
2e5d0 20 57 4f 5f 49 4e 3b 0a 20 20 7d 65 6c 73 65 20   WO_IN;.  }else 
2e5d1 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c  if( op==TK_ISNUL
2e5d2 4c 20 29 7b 0a 20 20 20 20 63 20 3d 20 57 4f 5f  L ){.    c = WO_
2e5d3 49 53 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b  ISNULL;.  }else{
2e5d4 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 57 4f  .    assert( (WO
2e5d5 5f 45 51 3c 3c 28 6f 70 2d 54 4b 5f 45 51 29 29  _EQ<<(op-TK_EQ))
2e5d6 20 3c 20 30 78 37 66 66 66 20 29 3b 0a 20 20 20   < 0x7fff );.   
2e5d7 20 63 20 3d 20 28 75 31 36 29 28 57 4f 5f 45 51   c = (u16)(WO_EQ
2e5d8 3c 3c 28 6f 70 2d 54 4b 5f 45 51 29 29 3b 0a 20  <<(op-TK_EQ));. 
2e5d9 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21   }.  assert( op!
2e5da 3d 54 4b 5f 49 53 4e 55 4c 4c 20 7c 7c 20 63 3d  =TK_ISNULL || c=
2e5db 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20  =WO_ISNULL );.  
2e5dc 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 49  assert( op!=TK_I
2e5dd 4e 20 7c 7c 20 63 3d 3d 57 4f 5f 49 4e 20 29 3b  N || c==WO_IN );
2e5de 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54  .  assert( op!=T
2e5df 4b 5f 45 51 20 7c 7c 20 63 3d 3d 57 4f 5f 45 51  K_EQ || c==WO_EQ
2e5e0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70   );.  assert( op
2e5e1 21 3d 54 4b 5f 4c 54 20 7c 7c 20 63 3d 3d 57 4f  !=TK_LT || c==WO
2e5e2 5f 4c 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _LT );.  assert(
2e5e3 20 6f 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 63 3d   op!=TK_LE || c=
2e5e4 3d 57 4f 5f 4c 45 20 29 3b 0a 20 20 61 73 73 65  =WO_LE );.  asse
2e5e5 72 74 28 20 6f 70 21 3d 54 4b 5f 47 54 20 7c 7c  rt( op!=TK_GT ||
2e5e6 20 63 3d 3d 57 4f 5f 47 54 20 29 3b 0a 20 20 61   c==WO_GT );.  a
2e5e7 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 47 45  ssert( op!=TK_GE
2e5e8 20 7c 7c 20 63 3d 3d 57 4f 5f 47 45 20 29 3b 0a   || c==WO_GE );.
2e5e9 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 2f    return c;.}../
2e5ea 2a 0a 2a 2a 20 53 65 61 72 63 68 20 66 6f 72 20  *.** Search for 
2e5eb 61 20 74 65 72 6d 20 69 6e 20 74 68 65 20 57 48  a term in the WH
2e5ec 45 52 45 20 63 6c 61 75 73 65 20 74 68 61 74 20  ERE clause that 
2e5ed 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  is of the form "
2e5ee 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 0a 2a  X <op> <expr>".*
2e5ef 2a 20 77 68 65 72 65 20 58 20 69 73 20 61 20 72  * where X is a r
2e5f0 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20  eference to the 
2e5f1 69 43 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65  iColumn of table
2e5f2 20 69 43 75 72 20 61 6e 64 20 3c 6f 70 3e 20 69   iCur and <op> i
2e5f3 73 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20  s one of.** the 
2e5f4 57 4f 5f 78 78 20 6f 70 65 72 61 74 6f 72 20 63  WO_xx operator c
2e5f5 6f 64 65 73 20 73 70 65 63 69 66 69 65 64 20 62  odes specified b
2e5f6 79 20 74 68 65 20 6f 70 20 70 61 72 61 6d 65 74  y the op paramet
2e5f7 65 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  er..** Return a 
2e5f8 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 74  pointer to the t
2e5f9 65 72 6d 2e 20 20 52 65 74 75 72 6e 20 30 20 69  erm.  Return 0 i
2e5fa 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2f 0a  f not found..*/.
2e5fb 73 74 61 74 69 63 20 57 68 65 72 65 54 65 72 6d  static WhereTerm
2e5fc 20 2a 66 69 6e 64 54 65 72 6d 28 0a 20 20 57 68   *findTerm(.  Wh
2e5fd 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20  ereClause *pWC, 
2e5fe 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45      /* The WHERE
2e5ff 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20 73 65   clause to be se
2e600 61 72 63 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20  arched */.  int 
2e601 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 20  iCur,           
2e602 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62    /* Cursor numb
2e603 65 72 20 6f 66 20 4c 48 53 20 2a 2f 0a 20 20 69  er of LHS */.  i
2e604 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20  nt iColumn,     
2e605 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e       /* Column n
2e606 75 6d 62 65 72 20 6f 66 20 4c 48 53 20 2a 2f 0a  umber of LHS */.
2e607 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61    Bitmask notRea
2e608 64 79 2c 20 20 20 20 20 2f 2a 20 52 48 53 20 6d  dy,     /* RHS m
2e609 75 73 74 20 6e 6f 74 20 6f 76 65 72 6c 61 70 20  ust not overlap 
2e60a 77 69 74 68 20 74 68 69 73 20 6d 61 73 6b 20 2a  with this mask *
2e60b 2f 0a 20 20 75 33 32 20 6f 70 2c 20 20 20 20 20  /.  u32 op,     
2e60c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73            /* Mas
2e60d 6b 20 6f 66 20 57 4f 5f 78 78 20 76 61 6c 75 65  k of WO_xx value
2e60e 73 20 64 65 73 63 72 69 62 69 6e 67 20 6f 70 65  s describing ope
2e60f 72 61 74 6f 72 20 2a 2f 0a 20 20 49 6e 64 65 78  rator */.  Index
2e610 20 2a 70 49 64 78 20 20 20 20 20 20 20 20 20 20   *pIdx          
2e611 20 2f 2a 20 4d 75 73 74 20 62 65 20 63 6f 6d 70   /* Must be comp
2e612 61 74 69 62 6c 65 20 77 69 74 68 20 74 68 69 73  atible with this
2e613 20 69 6e 64 65 78 2c 20 69 66 20 6e 6f 74 20 4e   index, if not N
2e614 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 57 68 65 72  ULL */.){.  Wher
2e615 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20  eTerm *pTerm;.  
2e616 69 6e 74 20 6b 3b 0a 20 20 61 73 73 65 72 74 28  int k;.  assert(
2e617 20 69 43 75 72 3e 3d 30 20 29 3b 0a 20 20 6f 70   iCur>=0 );.  op
2e618 20 26 3d 20 57 4f 5f 41 4c 4c 3b 0a 20 20 66 6f   &= WO_ALL;.  fo
2e619 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61 2c 20  r(pTerm=pWC->a, 
2e61a 6b 3d 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 6b 3b  k=pWC->nTerm; k;
2e61b 20 6b 2d 2d 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a   k--, pTerm++){.
2e61c 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c      if( pTerm->l
2e61d 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 0a  eftCursor==iCur.
2e61e 20 20 20 20 20 20 20 26 26 20 28 70 54 65 72 6d         && (pTerm
2e61f 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 26 20  ->prereqRight & 
2e620 6e 6f 74 52 65 61 64 79 29 3d 3d 30 0a 20 20 20  notReady)==0.   
2e621 20 20 20 20 26 26 20 70 54 65 72 6d 2d 3e 75 2e      && pTerm->u.
2e622 6c 65 66 74 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c  leftColumn==iCol
2e623 75 6d 6e 0a 20 20 20 20 20 20 20 26 26 20 28 70  umn.       && (p
2e624 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
2e625 26 20 6f 70 29 21 3d 30 0a 20 20 20 20 29 7b 0a  & op)!=0.    ){.
2e626 20 20 20 20 20 20 69 66 28 20 70 49 64 78 20 26        if( pIdx &
2e627 26 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  & pTerm->eOperat
2e628 6f 72 21 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29 7b  or!=WO_ISNULL ){
2e629 0a 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70  .        Expr *p
2e62a 58 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72  X = pTerm->pExpr
2e62b 3b 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65  ;.        CollSe
2e62c 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20  q *pColl;.      
2e62d 20 20 63 68 61 72 20 69 64 78 61 66 66 3b 0a 20    char idxaff;. 
2e62e 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20         int j;.  
2e62f 20 20 20 20 20 20 50 61 72 73 65 20 2a 70 50 61        Parse *pPa
2e630 72 73 65 20 3d 20 70 57 43 2d 3e 70 50 61 72 73  rse = pWC->pPars
2e631 65 3b 0a 0a 20 20 20 20 20 20 20 20 69 64 78 61  e;..        idxa
2e632 66 66 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c  ff = pIdx->pTabl
2e633 65 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 75 6d 6e 5d  e->aCol[iColumn]
2e634 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 20  .affinity;.     
2e635 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49     if( !sqlite3I
2e636 6e 64 65 78 41 66 66 69 6e 69 74 79 4f 6b 28 70  ndexAffinityOk(p
2e637 58 2c 20 69 64 78 61 66 66 29 20 29 20 63 6f 6e  X, idxaff) ) con
2e638 74 69 6e 75 65 3b 0a 0a 20 20 20 20 20 20 20 20  tinue;..        
2e639 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68  /* Figure out th
2e63a 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
2e63b 65 6e 63 65 20 72 65 71 75 69 72 65 64 20 66 72  ence required fr
2e63c 6f 6d 20 61 6e 20 69 6e 64 65 78 20 66 6f 72 0a  om an index for.
2e63d 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20 74 6f          ** it to
2e63e 20 62 65 20 75 73 65 66 75 6c 20 66 6f 72 20 6f   be useful for o
2e63f 70 74 69 6d 69 73 69 6e 67 20 65 78 70 72 65 73  ptimising expres
2e640 73 69 6f 6e 20 70 58 2e 20 53 74 6f 72 65 20 74  sion pX. Store t
2e641 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 76  his.        ** v
2e642 61 6c 75 65 20 69 6e 20 76 61 72 69 61 62 6c 65  alue in variable
2e643 20 70 43 6f 6c 6c 2e 0a 20 20 20 20 20 20 20 20   pColl..        
2e644 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  */.        asser
2e645 74 28 70 58 2d 3e 70 4c 65 66 74 29 3b 0a 20 20  t(pX->pLeft);.  
2e646 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71        pColl = sq
2e647 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61  lite3BinaryCompa
2e648 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  reCollSeq(pParse
2e649 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20 70 58 2d  , pX->pLeft, pX-
2e64a 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20  >pRight);.      
2e64b 20 20 61 73 73 65 72 74 28 70 43 6f 6c 6c 20 7c    assert(pColl |
2e64c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 29 3b  | pParse->nErr);
2e64d 0a 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  ..        for(j=
2e64e 30 3b 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d  0; pIdx->aiColum
2e64f 6e 5b 6a 5d 21 3d 69 43 6f 6c 75 6d 6e 3b 20 6a  n[j]!=iColumn; j
2e650 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
2e651 66 28 20 4e 45 56 45 52 28 6a 3e 3d 70 49 64 78  f( NEVER(j>=pIdx
2e652 2d 3e 6e 43 6f 6c 75 6d 6e 29 20 29 20 72 65 74  ->nColumn) ) ret
2e653 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  urn 0;.        }
2e654 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f  .        if( pCo
2e655 6c 6c 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  ll && sqlite3Str
2e656 49 43 6d 70 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  ICmp(pColl->zNam
2e657 65 2c 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b  e, pIdx->azColl[
2e658 6a 5d 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  j]) ) continue;.
2e659 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
2e65a 74 75 72 6e 20 70 54 65 72 6d 3b 0a 20 20 20 20  turn pTerm;.    
2e65b 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
2e65c 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20  ;.}../* Forward 
2e65d 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 74 61  reference */.sta
2e65e 74 69 63 20 76 6f 69 64 20 65 78 70 72 41 6e 61  tic void exprAna
2e65f 6c 79 7a 65 28 53 72 63 4c 69 73 74 2a 2c 20 57  lyze(SrcList*, W
2e660 68 65 72 65 43 6c 61 75 73 65 2a 2c 20 69 6e 74  hereClause*, int
2e661 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 65  );../*.** Call e
2e662 78 70 72 41 6e 61 6c 79 7a 65 20 6f 6e 20 61 6c  xprAnalyze on al
2e663 6c 20 74 65 72 6d 73 20 69 6e 20 61 20 57 48 45  l terms in a WHE
2e664 52 45 20 63 6c 61 75 73 65 2e 20 20 0a 2a 2a 0a  RE clause.  .**.
2e665 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  **.*/.static voi
2e666 64 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c  d exprAnalyzeAll
2e667 28 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61  (.  SrcList *pTa
2e668 62 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a 20  bList,       /* 
2e669 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2e66a 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65  */.  WhereClause
2e66b 20 2a 70 57 43 20 20 20 20 20 20 20 20 20 2f 2a   *pWC         /*
2e66c 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2e66d 65 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64  e to be analyzed
2e66e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
2e66f 20 20 66 6f 72 28 69 3d 70 57 43 2d 3e 6e 54 65    for(i=pWC->nTe
2e670 72 6d 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29  rm-1; i>=0; i--)
2e671 7b 0a 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a  {.    exprAnalyz
2e672 65 28 70 54 61 62 4c 69 73 74 2c 20 70 57 43 2c  e(pTabList, pWC,
2e673 20 69 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e   i);.  }.}..#ifn
2e674 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2e675 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  LIKE_OPTIMIZATIO
2e676 4e 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f  N./*.** Check to
2e677 20 73 65 65 20 69 66 20 74 68 65 20 67 69 76 65   see if the give
2e678 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  n expression is 
2e679 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 6f  a LIKE or GLOB o
2e67a 70 65 72 61 74 6f 72 20 74 68 61 74 0a 2a 2a 20  perator that.** 
2e67b 63 61 6e 20 62 65 20 6f 70 74 69 6d 69 7a 65 64  can be optimized
2e67c 20 75 73 69 6e 67 20 69 6e 65 71 75 61 6c 69 74   using inequalit
2e67d 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20  y constraints.  
2e67e 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 69  Return TRUE if i
2e67f 74 20 69 73 0a 2a 2a 20 73 6f 20 61 6e 64 20 66  t is.** so and f
2e680 61 6c 73 65 20 69 66 20 6e 6f 74 2e 0a 2a 2a 0a  alse if not..**.
2e681 2a 2a 20 49 6e 20 6f 72 64 65 72 20 66 6f 72 20  ** In order for 
2e682 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f 20  the operator to 
2e683 62 65 20 6f 70 74 69 6d 69 7a 69 62 6c 65 2c 20  be optimizible, 
2e684 74 68 65 20 52 48 53 20 6d 75 73 74 20 62 65 20  the RHS must be 
2e685 61 20 73 74 72 69 6e 67 0a 2a 2a 20 6c 69 74 65  a string.** lite
2e686 72 61 6c 20 74 68 61 74 20 64 6f 65 73 20 6e 6f  ral that does no
2e687 74 20 62 65 67 69 6e 20 77 69 74 68 20 61 20 77  t begin with a w
2e688 69 6c 64 63 61 72 64 2e 20 20 0a 2a 2f 0a 73 74  ildcard.  .*/.st
2e689 61 74 69 63 20 69 6e 74 20 69 73 4c 69 6b 65 4f  atic int isLikeO
2e68a 72 47 6c 6f 62 28 0a 20 20 50 61 72 73 65 20 2a  rGlob(.  Parse *
2e68b 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 50 61  pParse,    /* Pa
2e68c 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67  rsing and code g
2e68d 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78  enerating contex
2e68e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78  t */.  Expr *pEx
2e68f 70 72 2c 20 20 20 20 20 20 2f 2a 20 54 65 73 74  pr,      /* Test
2e690 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e   this expression
2e691 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 50 61 74   */.  int *pnPat
2e692 74 65 72 6e 2c 20 20 20 2f 2a 20 4e 75 6d 62 65  tern,   /* Numbe
2e693 72 20 6f 66 20 6e 6f 6e 2d 77 69 6c 64 63 61 72  r of non-wildcar
2e694 64 20 70 72 65 66 69 78 20 63 68 61 72 61 63 74  d prefix charact
2e695 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69  ers */.  int *pi
2e696 73 43 6f 6d 70 6c 65 74 65 2c 20 2f 2a 20 54 72  sComplete, /* Tr
2e697 75 65 20 69 66 20 74 68 65 20 6f 6e 6c 79 20 77  ue if the only w
2e698 69 6c 64 63 61 72 64 20 69 73 20 25 20 69 6e 20  ildcard is % in 
2e699 74 68 65 20 6c 61 73 74 20 63 68 61 72 61 63 74  the last charact
2e69a 65 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 6f  er */.  int *pno
2e69b 43 61 73 65 20 20 20 20 20 20 2f 2a 20 54 72 75  Case      /* Tru
2e69c 65 20 69 66 20 75 70 70 65 72 63 61 73 65 20 69  e if uppercase i
2e69d 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20  s equivalent to 
2e69e 6c 6f 77 65 72 63 61 73 65 20 2a 2f 0a 29 7b 0a  lowercase */.){.
2e69f 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b    const char *z;
2e6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e6a1 53 74 72 69 6e 67 20 6f 6e 20 52 48 53 20 6f 66  String on RHS of
2e6a2 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 2a   LIKE operator *
2e6a3 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74  /.  Expr *pRight
2e6a4 2c 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 20 2f  , *pLeft;      /
2e6a5 2a 20 52 69 67 68 74 20 61 6e 64 20 6c 65 66 74  * Right and left
2e6a6 20 73 69 7a 65 20 6f 66 20 4c 49 4b 45 20 6f 70   size of LIKE op
2e6a7 65 72 61 74 6f 72 20 2a 2f 0a 20 20 45 78 70 72  erator */.  Expr
2e6a8 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20  List *pList;    
2e6a9 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f         /* List o
2e6aa 66 20 6f 70 65 72 61 6e 64 73 20 74 6f 20 74 68  f operands to th
2e6ab 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20  e LIKE operator 
2e6ac 2a 2f 0a 20 20 69 6e 74 20 63 3b 20 20 20 20 20  */.  int c;     
2e6ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e6ae 2f 2a 20 4f 6e 65 20 63 68 61 72 61 63 74 65 72  /* One character
2e6af 20 69 6e 20 7a 5b 5d 20 2a 2f 0a 20 20 69 6e 74   in z[] */.  int
2e6b0 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20   cnt;           
2e6b1 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2e6b2 72 20 6f 66 20 6e 6f 6e 2d 77 69 6c 64 63 61 72  r of non-wildcar
2e6b3 64 20 70 72 65 66 69 78 20 63 68 61 72 61 63 74  d prefix charact
2e6b4 65 72 73 20 2a 2f 0a 20 20 63 68 61 72 20 77 63  ers */.  char wc
2e6b5 5b 33 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20  [3];            
2e6b6 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 20      /* Wildcard 
2e6b7 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0a 20 20  characters */.  
2e6b8 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20  CollSeq *pColl; 
2e6b9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
2e6ba 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2e6bb 20 66 6f 72 20 4c 48 53 20 2a 2f 0a 20 20 73 71   for LHS */.  sq
2e6bc 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2e6bd 73 65 2d 3e 64 62 3b 20 20 2f 2a 20 44 61 74 61  se->db;  /* Data
2e6be 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
2e6bf 2a 2f 0a 0a 20 20 69 66 28 20 21 73 71 6c 69 74  */..  if( !sqlit
2e6c0 65 33 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e  e3IsLikeFunction
2e6c1 28 64 62 2c 20 70 45 78 70 72 2c 20 70 6e 6f 43  (db, pExpr, pnoC
2e6c2 61 73 65 2c 20 77 63 29 20 29 7b 0a 20 20 20 20  ase, wc) ){.    
2e6c3 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 69  return 0;.  }.#i
2e6c4 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44  fdef SQLITE_EBCD
2e6c5 49 43 0a 20 20 69 66 28 20 2a 70 6e 6f 43 61 73  IC.  if( *pnoCas
2e6c6 65 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65  e ) return 0;.#e
2e6c7 6e 64 69 66 0a 20 20 70 4c 69 73 74 20 3d 20 70  ndif.  pList = p
2e6c8 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20  Expr->x.pList;. 
2e6c9 20 70 52 69 67 68 74 20 3d 20 70 4c 69 73 74 2d   pRight = pList-
2e6ca 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 69  >a[0].pExpr;.  i
2e6cb 66 28 20 70 52 69 67 68 74 2d 3e 6f 70 21 3d 54  f( pRight->op!=T
2e6cc 4b 5f 53 54 52 49 4e 47 20 29 7b 0a 20 20 20 20  K_STRING ){.    
2e6cd 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
2e6ce 70 4c 65 66 74 20 3d 20 70 4c 69 73 74 2d 3e 61  pLeft = pList->a
2e6cf 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 69 66 28  [1].pExpr;.  if(
2e6d0 20 70 4c 65 66 74 2d 3e 6f 70 21 3d 54 4b 5f 43   pLeft->op!=TK_C
2e6d1 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 72 65 74  OLUMN ){.    ret
2e6d2 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 43 6f  urn 0;.  }.  pCo
2e6d3 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ll = sqlite3Expr
2e6d4 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
2e6d5 70 4c 65 66 74 29 3b 0a 20 20 61 73 73 65 72 74  pLeft);.  assert
2e6d6 28 20 70 43 6f 6c 6c 21 3d 30 20 7c 7c 20 70 4c  ( pColl!=0 || pL
2e6d7 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 2d 31  eft->iColumn==-1
2e6d8 20 29 3b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 3d   );.  if( pColl=
2e6d9 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
2e6da 20 69 66 28 20 28 70 43 6f 6c 6c 2d 3e 74 79 70   if( (pColl->typ
2e6db 65 21 3d 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42  e!=SQLITE_COLL_B
2e6dc 49 4e 41 52 59 20 7c 7c 20 2a 70 6e 6f 43 61 73  INARY || *pnoCas
2e6dd 65 29 20 26 26 0a 20 20 20 20 20 20 28 70 43 6f  e) &&.      (pCo
2e6de 6c 6c 2d 3e 74 79 70 65 21 3d 53 51 4c 49 54 45  ll->type!=SQLITE
2e6df 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 20 7c 7c 20  _COLL_NOCASE || 
2e6e0 21 2a 70 6e 6f 43 61 73 65 29 20 29 7b 0a 20 20  !*pnoCase) ){.  
2e6e1 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
2e6e2 20 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70    if( sqlite3Exp
2e6e3 72 41 66 66 69 6e 69 74 79 28 70 4c 65 66 74 29  rAffinity(pLeft)
2e6e4 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  !=SQLITE_AFF_TEX
2e6e5 54 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  T ) return 0;.  
2e6e6 7a 20 3d 20 70 52 69 67 68 74 2d 3e 75 2e 7a 54  z = pRight->u.zT
2e6e7 6f 6b 65 6e 3b 0a 20 20 69 66 28 20 41 4c 57 41  oken;.  if( ALWA
2e6e8 59 53 28 7a 29 20 29 7b 0a 20 20 20 20 63 6e 74  YS(z) ){.    cnt
2e6e9 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28   = 0;.    while(
2e6ea 20 28 63 3d 7a 5b 63 6e 74 5d 29 21 3d 30 20 26   (c=z[cnt])!=0 &
2e6eb 26 20 63 21 3d 77 63 5b 30 5d 20 26 26 20 63 21  & c!=wc[0] && c!
2e6ec 3d 77 63 5b 31 5d 20 26 26 20 63 21 3d 77 63 5b  =wc[1] && c!=wc[
2e6ed 32 5d 20 29 7b 0a 20 20 20 20 20 20 63 6e 74 2b  2] ){.      cnt+
2e6ee 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  +;.    }.    if(
2e6ef 20 63 6e 74 21 3d 30 20 26 26 20 63 21 3d 30 20   cnt!=0 && c!=0 
2e6f0 26 26 20 32 35 35 21 3d 28 75 38 29 7a 5b 63 6e  && 255!=(u8)z[cn
2e6f1 74 2d 31 5d 20 29 7b 0a 20 20 20 20 20 20 2a 70  t-1] ){.      *p
2e6f2 69 73 43 6f 6d 70 6c 65 74 65 20 3d 20 7a 5b 63  isComplete = z[c
2e6f3 6e 74 5d 3d 3d 77 63 5b 30 5d 20 26 26 20 7a 5b  nt]==wc[0] && z[
2e6f4 63 6e 74 2b 31 5d 3d 3d 30 3b 0a 20 20 20 20 20  cnt+1]==0;.     
2e6f5 20 2a 70 6e 50 61 74 74 65 72 6e 20 3d 20 63 6e   *pnPattern = cn
2e6f6 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  t;.      return 
2e6f7 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  1;.    }.  }.  r
2e6f8 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69  eturn 0;.}.#endi
2e6f9 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2e6fa 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49  _LIKE_OPTIMIZATI
2e6fb 4f 4e 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20  ON */...#ifndef 
2e6fc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2e6fd 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 43  UALTABLE./*.** C
2e6fe 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
2e6ff 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73  he given express
2e700 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f  ion is of the fo
2e701 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  rm.**.**        
2e702 20 63 6f 6c 75 6d 6e 20 4d 41 54 43 48 20 65 78   column MATCH ex
2e703 70 72 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69  pr.**.** If it i
2e704 73 20 74 68 65 6e 20 72 65 74 75 72 6e 20 54 52  s then return TR
2e705 55 45 2e 20 20 49 66 20 6e 6f 74 2c 20 72 65 74  UE.  If not, ret
2e706 75 72 6e 20 46 41 4c 53 45 2e 0a 2a 2f 0a 73 74  urn FALSE..*/.st
2e707 61 74 69 63 20 69 6e 74 20 69 73 4d 61 74 63 68  atic int isMatch
2e708 4f 66 43 6f 6c 75 6d 6e 28 0a 20 20 45 78 70 72  OfColumn(.  Expr
2e709 20 2a 70 45 78 70 72 20 20 20 20 20 20 2f 2a 20   *pExpr      /* 
2e70a 54 65 73 74 20 74 68 69 73 20 65 78 70 72 65 73  Test this expres
2e70b 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70  sion */.){.  Exp
2e70c 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 0a 0a 20  rList *pList;.. 
2e70d 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d   if( pExpr->op!=
2e70e 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 29 7b 0a 20  TK_FUNCTION ){. 
2e70f 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
2e710 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
2e711 72 49 43 6d 70 28 70 45 78 70 72 2d 3e 75 2e 7a  rICmp(pExpr->u.z
2e712 54 6f 6b 65 6e 2c 22 6d 61 74 63 68 22 29 21 3d  Token,"match")!=
2e713 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
2e714 30 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 74 20 3d  0;.  }.  pList =
2e715 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b   pExpr->x.pList;
2e716 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e 45  .  if( pList->nE
2e717 78 70 72 21 3d 32 20 29 7b 0a 20 20 20 20 72 65  xpr!=2 ){.    re
2e718 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66  turn 0;.  }.  if
2e719 28 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45  ( pList->a[1].pE
2e71a 78 70 72 2d 3e 6f 70 20 21 3d 20 54 4b 5f 43 4f  xpr->op != TK_CO
2e71b 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 72 65 74 75  LUMN ){.    retu
2e71c 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn 0;.  }.  retu
2e71d 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  rn 1;.}.#endif /
2e71e 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
2e71f 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f  RTUALTABLE */../
2e720 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 42 61 73  *.** If the pBas
2e721 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 69  e expression ori
2e722 67 69 6e 61 74 65 64 20 69 6e 20 74 68 65 20 4f  ginated in the O
2e723 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73  N or USING claus
2e724 65 20 6f 66 0a 2a 2a 20 61 20 6a 6f 69 6e 2c 20  e of.** a join, 
2e725 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20 74 68  then transfer th
2e726 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 6d 61  e appropriate ma
2e727 72 6b 69 6e 67 73 20 6f 76 65 72 20 74 6f 20 64  rkings over to d
2e728 65 72 69 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  erived..*/.stati
2e729 63 20 76 6f 69 64 20 74 72 61 6e 73 66 65 72 4a  c void transferJ
2e72a 6f 69 6e 4d 61 72 6b 69 6e 67 73 28 45 78 70 72  oinMarkings(Expr
2e72b 20 2a 70 44 65 72 69 76 65 64 2c 20 45 78 70 72   *pDerived, Expr
2e72c 20 2a 70 42 61 73 65 29 7b 0a 20 20 70 44 65 72   *pBase){.  pDer
2e72d 69 76 65 64 2d 3e 66 6c 61 67 73 20 7c 3d 20 70  ived->flags |= p
2e72e 42 61 73 65 2d 3e 66 6c 61 67 73 20 26 20 45 50  Base->flags & EP
2e72f 5f 46 72 6f 6d 4a 6f 69 6e 3b 0a 20 20 70 44 65  _FromJoin;.  pDe
2e730 72 69 76 65 64 2d 3e 69 52 69 67 68 74 4a 6f 69  rived->iRightJoi
2e731 6e 54 61 62 6c 65 20 3d 20 70 42 61 73 65 2d 3e  nTable = pBase->
2e732 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 3b  iRightJoinTable;
2e733 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .}..#if !defined
2e734 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f  (SQLITE_OMIT_OR_
2e735 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 29 20 26 26  OPTIMIZATION) &&
2e736 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2e737 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a  _OMIT_SUBQUERY).
2e738 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20 61 20  /*.** Analyze a 
2e739 74 65 72 6d 20 74 68 61 74 20 63 6f 6e 73 69 73  term that consis
2e73a 74 73 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72  ts of two or mor
2e73b 65 20 4f 52 2d 63 6f 6e 6e 65 63 74 65 64 0a 2a  e OR-connected.*
2e73c 2a 20 73 75 62 74 65 72 6d 73 2e 20 20 53 6f 20  * subterms.  So 
2e73d 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2e 2e  in:.**.**     ..
2e73e 2e 20 57 48 45 52 45 20 20 28 61 3d 35 29 20 41  . WHERE  (a=5) A
2e73f 4e 44 20 28 62 3d 37 20 4f 52 20 63 3d 39 20 4f  ND (b=7 OR c=9 O
2e740 52 20 64 3d 31 33 29 20 41 4e 44 20 28 64 3d 31  R d=13) AND (d=1
2e741 33 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  3).**           
2e742 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5e                 ^
2e743 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e  ^^^^^^^^^^^^^^^^
2e744 5e 5e 5e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ^^^.**.** This r
2e745 6f 75 74 69 6e 65 20 61 6e 61 6c 79 7a 65 73 20  outine analyzes 
2e746 74 65 72 6d 73 20 73 75 63 68 20 61 73 20 74 68  terms such as th
2e747 65 20 6d 69 64 64 6c 65 20 74 65 72 6d 20 69 6e  e middle term in
2e748 20 74 68 65 20 61 62 6f 76 65 20 65 78 61 6d 70   the above examp
2e749 6c 65 2e 0a 2a 2a 20 41 20 57 68 65 72 65 4f 72  le..** A WhereOr
2e74a 54 65 72 6d 20 6f 62 6a 65 63 74 20 69 73 20 63  Term object is c
2e74b 6f 6d 70 75 74 65 64 20 61 6e 64 20 61 74 74 61  omputed and atta
2e74c 63 68 65 64 20 74 6f 20 74 68 65 20 74 65 72 6d  ched to the term
2e74d 20 75 6e 64 65 72 0a 2a 2a 20 61 6e 61 6c 79 73   under.** analys
2e74e 69 73 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f  is, regardless o
2e74f 66 20 74 68 65 20 6f 75 74 63 6f 6d 65 20 6f 66  f the outcome of
2e750 20 74 68 65 20 61 6e 61 6c 79 73 69 73 2e 20 20   the analysis.  
2e751 48 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  Hence:.**.**    
2e752 20 57 68 65 72 65 54 65 72 6d 2e 77 74 46 6c 61   WhereTerm.wtFla
2e753 67 73 20 20 20 7c 3d 20 20 54 45 52 4d 5f 4f 52  gs   |=  TERM_OR
2e754 49 4e 46 4f 0a 2a 2a 20 20 20 20 20 57 68 65 72  INFO.**     Wher
2e755 65 54 65 72 6d 2e 75 2e 70 4f 72 49 6e 66 6f 20  eTerm.u.pOrInfo 
2e756 20 3d 20 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c   =  a dynamicall
2e757 79 20 61 6c 6c 6f 63 61 74 65 64 20 57 68 65 72  y allocated Wher
2e758 65 4f 72 54 65 72 6d 20 6f 62 6a 65 63 74 0a 2a  eOrTerm object.*
2e759 2a 0a 2a 2a 20 54 68 65 20 74 65 72 6d 20 62 65  *.** The term be
2e75a 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 6d 75 73  ing analyzed mus
2e75b 74 20 68 61 76 65 20 74 77 6f 20 6f 72 20 6d 6f  t have two or mo
2e75c 72 65 20 6f 66 20 4f 52 2d 63 6f 6e 6e 65 63 74  re of OR-connect
2e75d 65 64 20 73 75 62 74 65 72 6d 73 2e 0a 2a 2a 20  ed subterms..** 
2e75e 41 20 73 69 6e 67 6c 65 20 73 75 62 74 65 72 6d  A single subterm
2e75f 20 6d 69 67 68 74 20 62 65 20 61 20 73 65 74 20   might be a set 
2e760 6f 66 20 41 4e 44 2d 63 6f 6e 6e 65 63 74 65 64  of AND-connected
2e761 20 73 75 62 2d 73 75 62 74 65 72 6d 73 2e 0a 2a   sub-subterms..*
2e762 2a 20 45 78 61 6d 70 6c 65 73 20 6f 66 20 74 65  * Examples of te
2e763 72 6d 73 20 75 6e 64 65 72 20 61 6e 61 6c 79 73  rms under analys
2e764 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 41  is:.**.**     (A
2e765 29 20 20 20 20 20 74 31 2e 78 3d 74 32 2e 79 20  )     t1.x=t2.y 
2e766 4f 52 20 74 31 2e 78 3d 74 32 2e 7a 20 4f 52 20  OR t1.x=t2.z OR 
2e767 74 31 2e 79 3d 31 35 20 4f 52 20 74 31 2e 7a 3d  t1.y=15 OR t1.z=
2e768 74 33 2e 61 2b 35 0a 2a 2a 20 20 20 20 20 28 42  t3.a+5.**     (B
2e769 29 20 20 20 20 20 78 3d 65 78 70 72 31 20 4f 52  )     x=expr1 OR
2e76a 20 65 78 70 72 32 3d 78 20 4f 52 20 78 3d 65 78   expr2=x OR x=ex
2e76b 70 72 33 0a 2a 2a 20 20 20 20 20 28 43 29 20 20  pr3.**     (C)  
2e76c 20 20 20 74 31 2e 78 3d 74 32 2e 79 20 4f 52 20     t1.x=t2.y OR 
2e76d 28 74 31 2e 78 3d 74 32 2e 7a 20 41 4e 44 20 74  (t1.x=t2.z AND t
2e76e 31 2e 79 3d 31 35 29 0a 2a 2a 20 20 20 20 20 28  1.y=15).**     (
2e76f 44 29 20 20 20 20 20 78 3d 65 78 70 72 31 20 4f  D)     x=expr1 O
2e770 52 20 28 79 3e 31 31 20 41 4e 44 20 79 3c 32 32  R (y>11 AND y<22
2e771 20 41 4e 44 20 7a 20 4c 49 4b 45 20 27 2a 68 65   AND z LIKE '*he
2e772 6c 6c 6f 2a 27 29 0a 2a 2a 20 20 20 20 20 28 45  llo*').**     (E
2e773 29 20 20 20 20 20 28 70 2e 61 3d 31 20 41 4e 44  )     (p.a=1 AND
2e774 20 71 2e 62 3d 32 20 41 4e 44 20 72 2e 63 3d 33   q.b=2 AND r.c=3
2e775 29 20 4f 52 20 28 70 2e 78 3d 34 20 41 4e 44 20  ) OR (p.x=4 AND 
2e776 71 2e 79 3d 35 20 41 4e 44 20 72 2e 7a 3d 36 29  q.y=5 AND r.z=6)
2e777 0a 2a 2a 0a 2a 2a 20 43 41 53 45 20 31 3a 0a 2a  .**.** CASE 1:.*
2e778 2a 0a 2a 2a 20 49 66 20 61 6c 6c 20 73 75 62 74  *.** If all subt
2e779 65 72 6d 73 20 61 72 65 20 6f 66 20 74 68 65 20  erms are of the 
2e77a 66 6f 72 6d 20 54 2e 43 3d 65 78 70 72 20 66 6f  form T.C=expr fo
2e77b 72 20 73 6f 6d 65 20 73 69 6e 67 6c 65 20 63 6f  r some single co
2e77c 6c 75 6d 6e 20 6f 66 20 43 0a 2a 2a 20 61 20 73  lumn of C.** a s
2e77d 69 6e 67 6c 65 20 74 61 62 6c 65 20 54 20 28 61  ingle table T (a
2e77e 73 20 73 68 6f 77 6e 20 69 6e 20 65 78 61 6d 70  s shown in examp
2e77f 6c 65 20 42 20 61 62 6f 76 65 29 20 74 68 65 6e  le B above) then
2e780 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 76 69   create a new vi
2e781 72 74 75 61 6c 0a 2a 2a 20 74 65 72 6d 20 74 68  rtual.** term th
2e782 61 74 20 69 73 20 61 6e 20 65 71 75 69 76 61 6c  at is an equival
2e783 65 6e 74 20 49 4e 20 65 78 70 72 65 73 73 69 6f  ent IN expressio
2e784 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  n.  In other wor
2e785 64 73 2c 20 69 66 20 74 68 65 20 74 65 72 6d 0a  ds, if the term.
2e786 2a 2a 20 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65  ** being analyze
2e787 64 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  d is:.**.**     
2e788 20 78 20 3d 20 65 78 70 72 31 20 20 4f 52 20 20   x = expr1  OR  
2e789 65 78 70 72 32 20 3d 20 78 20 20 4f 52 20 20 78  expr2 = x  OR  x
2e78a 20 3d 20 65 78 70 72 33 0a 2a 2a 0a 2a 2a 20 74   = expr3.**.** t
2e78b 68 65 6e 20 63 72 65 61 74 65 20 61 20 6e 65 77  hen create a new
2e78c 20 76 69 72 74 75 61 6c 20 74 65 72 6d 20 6c 69   virtual term li
2e78d 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ke this:.**.**  
2e78e 20 20 20 20 78 20 49 4e 20 28 65 78 70 72 31 2c      x IN (expr1,
2e78f 65 78 70 72 32 2c 65 78 70 72 33 29 0a 2a 2a 0a  expr2,expr3).**.
2e790 2a 2a 20 43 41 53 45 20 32 3a 0a 2a 2a 0a 2a 2a  ** CASE 2:.**.**
2e791 20 49 66 20 61 6c 6c 20 73 75 62 74 65 72 6d 73   If all subterms
2e792 20 61 72 65 20 69 6e 64 65 78 61 62 6c 65 20 62   are indexable b
2e793 79 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65  y a single table
2e794 20 54 2c 20 74 68 65 6e 20 73 65 74 0a 2a 2a 0a   T, then set.**.
2e795 2a 2a 20 20 20 20 20 57 68 65 72 65 54 65 72 6d  **     WhereTerm
2e796 2e 65 4f 70 65 72 61 74 6f 72 20 20 20 20 20 20  .eOperator      
2e797 20 20 20 20 20 20 20 20 3d 20 20 57 4f 5f 4f 52          =  WO_OR
2e798 0a 2a 2a 20 20 20 20 20 57 68 65 72 65 54 65 72  .**     WhereTer
2e799 6d 2e 75 2e 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64  m.u.pOrInfo->ind
2e79a 65 78 61 62 6c 65 20 20 7c 3d 20 20 74 68 65 20  exable  |=  the 
2e79b 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f  cursor number fo
2e79c 72 20 74 61 62 6c 65 20 54 0a 2a 2a 0a 2a 2a 20  r table T.**.** 
2e79d 41 20 73 75 62 74 65 72 6d 20 69 73 20 22 69 6e  A subterm is "in
2e79e 64 65 78 61 62 6c 65 22 20 69 66 20 69 74 20 69  dexable" if it i
2e79f 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a  s of the form.**
2e7a0 20 22 54 2e 43 20 3c 6f 70 3e 20 3c 65 78 70 72   "T.C <op> <expr
2e7a1 3e 22 20 77 68 65 72 65 20 43 20 69 73 20 61 6e  >" where C is an
2e7a2 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c  y column of tabl
2e7a3 65 20 54 20 61 6e 64 20 0a 2a 2a 20 3c 6f 70 3e  e T and .** <op>
2e7a4 20 69 73 20 6f 6e 65 20 6f 66 20 22 3d 22 2c 20   is one of "=", 
2e7a5 22 3c 22 2c 20 22 3c 3d 22 2c 20 22 3e 22 2c 20  "<", "<=", ">", 
2e7a6 22 3e 3d 22 2c 20 22 49 53 20 4e 55 4c 4c 22 2c  ">=", "IS NULL",
2e7a7 20 6f 72 20 22 49 4e 22 2e 0a 2a 2a 20 41 20 73   or "IN"..** A s
2e7a8 75 62 74 65 72 6d 20 69 73 20 61 6c 73 6f 20 69  ubterm is also i
2e7a9 6e 64 65 78 61 62 6c 65 20 69 66 20 69 74 20 69  ndexable if it i
2e7aa 73 20 61 6e 20 41 4e 44 20 6f 66 20 74 77 6f 20  s an AND of two 
2e7ab 6f 72 20 6d 6f 72 65 0a 2a 2a 20 73 75 62 73 75  or more.** subsu
2e7ac 62 74 65 72 6d 73 20 61 74 20 6c 65 61 73 74 20  bterms at least 
2e7ad 6f 6e 65 20 6f 66 20 77 68 69 63 68 20 69 73 20  one of which is 
2e7ae 69 6e 64 65 78 61 62 6c 65 2e 20 20 49 6e 64 65  indexable.  Inde
2e7af 78 61 62 6c 65 20 41 4e 44 20 0a 2a 2a 20 73 75  xable AND .** su
2e7b0 62 74 65 72 6d 73 20 68 61 76 65 20 74 68 65 69  bterms have thei
2e7b1 72 20 65 4f 70 65 72 61 74 6f 72 20 73 65 74 20  r eOperator set 
2e7b2 74 6f 20 57 4f 5f 41 4e 44 20 61 6e 64 20 74 68  to WO_AND and th
2e7b3 65 79 20 68 61 76 65 0a 2a 2a 20 75 2e 70 41 6e  ey have.** u.pAn
2e7b4 64 49 6e 66 6f 20 73 65 74 20 74 6f 20 61 20 64  dInfo set to a d
2e7b5 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63  ynamically alloc
2e7b6 61 74 65 64 20 57 68 65 72 65 41 6e 64 54 65 72  ated WhereAndTer
2e7b7 6d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20  m object..**.** 
2e7b8 46 72 6f 6d 20 61 6e 6f 74 68 65 72 20 70 6f 69  From another poi
2e7b9 6e 74 20 6f 66 20 76 69 65 77 2c 20 22 69 6e 64  nt of view, "ind
2e7ba 65 78 61 62 6c 65 22 20 6d 65 61 6e 73 20 74 68  exable" means th
2e7bb 61 74 20 74 68 65 20 73 75 62 74 65 72 6d 20 63  at the subterm c
2e7bc 6f 75 6c 64 0a 2a 2a 20 70 6f 74 65 6e 74 69 61  ould.** potentia
2e7bd 6c 6c 79 20 62 65 20 75 73 65 64 20 77 69 74 68  lly be used with
2e7be 20 61 6e 20 69 6e 64 65 78 20 69 66 20 61 6e 20   an index if an 
2e7bf 61 70 70 72 6f 70 72 69 61 74 65 20 69 6e 64 65  appropriate inde
2e7c0 78 20 65 78 69 73 74 73 2e 0a 2a 2a 20 54 68 69  x exists..** Thi
2e7c1 73 20 61 6e 61 6c 79 73 69 73 20 64 6f 65 73 20  s analysis does 
2e7c2 6e 6f 74 20 63 6f 6e 73 69 64 65 72 20 77 68 65  not consider whe
2e7c3 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
2e7c4 69 6e 64 65 78 20 65 78 69 73 74 73 3b 20 74 68  index exists; th
2e7c5 61 74 0a 2a 2a 20 69 73 20 73 6f 6d 65 74 68 69  at.** is somethi
2e7c6 6e 67 20 74 68 65 20 62 65 73 74 49 6e 64 65 78  ng the bestIndex
2e7c7 28 29 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  () routine will 
2e7c8 64 65 74 65 72 6d 69 6e 65 2e 20 20 54 68 69 73  determine.  This
2e7c9 20 61 6e 61 6c 79 73 69 73 0a 2a 2a 20 6f 6e 6c   analysis.** onl
2e7ca 79 20 6c 6f 6f 6b 73 20 61 74 20 77 68 65 74 68  y looks at wheth
2e7cb 65 72 20 73 75 62 74 65 72 6d 73 20 61 70 70 72  er subterms appr
2e7cc 6f 70 72 69 61 74 65 20 66 6f 72 20 69 6e 64 65  opriate for inde
2e7cd 78 69 6e 67 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a  xing exist..**.*
2e7ce 2a 20 41 6c 6c 20 65 78 61 6d 70 6c 65 73 20 41  * All examples A
2e7cf 20 74 68 72 6f 75 67 68 20 45 20 61 62 6f 76 65   through E above
2e7d0 20 61 6c 6c 20 73 61 74 69 73 66 79 20 63 61 73   all satisfy cas
2e7d1 65 20 32 2e 20 20 42 75 74 20 69 66 20 61 20 74  e 2.  But if a t
2e7d2 65 72 6d 0a 2a 2a 20 61 6c 73 6f 20 73 74 61 74  erm.** also stat
2e7d3 69 73 66 69 65 73 20 63 61 73 65 20 31 20 28 73  isfies case 1 (s
2e7d4 75 63 68 20 61 73 20 42 29 20 77 65 20 6b 6e 6f  uch as B) we kno
2e7d5 77 20 74 68 61 74 20 74 68 65 20 6f 70 74 69 6d  w that the optim
2e7d6 69 7a 65 72 20 77 69 6c 6c 0a 2a 2a 20 61 6c 77  izer will.** alw
2e7d7 61 79 73 20 70 72 65 66 65 72 20 63 61 73 65 20  ays prefer case 
2e7d8 31 2c 20 73 6f 20 69 6e 20 74 68 61 74 20 63 61  1, so in that ca
2e7d9 73 65 20 77 65 20 70 72 65 74 65 6e 64 20 74 68  se we pretend th
2e7da 61 74 20 63 61 73 65 20 32 20 69 73 20 6e 6f 74  at case 2 is not
2e7db 0a 2a 2a 20 73 61 74 69 73 66 69 65 64 2e 0a 2a  .** satisfied..*
2e7dc 2a 0a 2a 2a 20 49 74 20 6d 69 67 68 74 20 62 65  *.** It might be
2e7dd 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20 6d   the case that m
2e7de 75 6c 74 69 70 6c 65 20 74 61 62 6c 65 73 20 61  ultiple tables a
2e7df 72 65 20 69 6e 64 65 78 61 62 6c 65 2e 20 20 46  re indexable.  F
2e7e0 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 28  or example,.** (
2e7e1 45 29 20 61 62 6f 76 65 20 69 73 20 69 6e 64 65  E) above is inde
2e7e2 78 61 62 6c 65 20 6f 6e 20 74 61 62 6c 65 73 20  xable on tables 
2e7e3 50 2c 20 51 2c 20 61 6e 64 20 52 2e 0a 2a 2a 0a  P, Q, and R..**.
2e7e4 2a 2a 20 54 65 72 6d 73 20 74 68 61 74 20 73 61  ** Terms that sa
2e7e5 74 69 73 66 79 20 63 61 73 65 20 32 20 61 72 65  tisfy case 2 are
2e7e6 20 63 61 6e 64 69 64 61 74 65 73 20 66 6f 72 20   candidates for 
2e7e7 6c 6f 6f 6b 75 70 20 62 79 20 75 73 69 6e 67 0a  lookup by using.
2e7e8 2a 2a 20 73 65 70 61 72 61 74 65 20 69 6e 64 69  ** separate indi
2e7e9 63 65 73 20 74 6f 20 66 69 6e 64 20 72 6f 77 69  ces to find rowi
2e7ea 64 73 20 66 6f 72 20 65 61 63 68 20 73 75 62 74  ds for each subt
2e7eb 65 72 6d 20 61 6e 64 20 63 6f 6d 70 6f 73 69 6e  erm and composin
2e7ec 67 0a 2a 2a 20 74 68 65 20 75 6e 69 6f 6e 20 6f  g.** the union o
2e7ed 66 20 61 6c 6c 20 72 6f 77 69 64 73 20 75 73 69  f all rowids usi
2e7ee 6e 67 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65  ng a RowSet obje
2e7ef 63 74 2e 20 20 54 68 69 73 20 69 73 20 73 69 6d  ct.  This is sim
2e7f0 69 6c 61 72 0a 2a 2a 20 74 6f 20 22 62 69 74 6d  ilar.** to "bitm
2e7f1 61 70 20 69 6e 64 69 63 65 73 22 20 69 6e 20 6f  ap indices" in o
2e7f2 74 68 65 72 20 64 61 74 61 62 61 73 65 20 65 6e  ther database en
2e7f3 67 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 54 48  gines..**.** OTH
2e7f4 45 52 57 49 53 45 3a 0a 2a 2a 0a 2a 2a 20 49 66  ERWISE:.**.** If
2e7f5 20 6e 65 69 74 68 65 72 20 63 61 73 65 20 31 20   neither case 1 
2e7f6 6e 6f 72 20 63 61 73 65 20 32 20 61 70 70 6c 79  nor case 2 apply
2e7f7 2c 20 74 68 65 6e 20 6c 65 61 76 65 20 74 68 65  , then leave the
2e7f8 20 65 4f 70 65 72 61 74 6f 72 20 73 65 74 20 74   eOperator set t
2e7f9 6f 0a 2a 2a 20 7a 65 72 6f 2e 20 20 54 68 69 73  o.** zero.  This
2e7fa 20 74 65 72 6d 20 69 73 20 6e 6f 74 20 75 73 65   term is not use
2e7fb 66 75 6c 20 66 6f 72 20 73 65 61 72 63 68 2e 0a  ful for search..
2e7fc 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65  */.static void e
2e7fd 78 70 72 41 6e 61 6c 79 7a 65 4f 72 54 65 72 6d  xprAnalyzeOrTerm
2e7fe 28 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  (.  SrcList *pSr
2e7ff 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  c,            /*
2e800 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2e801 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73   */.  WhereClaus
2e802 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20  e *pWC,         
2e803 2f 2a 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20  /* the complete 
2e804 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
2e805 20 20 69 6e 74 20 69 64 78 54 65 72 6d 20 20 20    int idxTerm   
2e806 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2e807 6e 64 65 78 20 6f 66 20 74 68 65 20 4f 52 2d 74  ndex of the OR-t
2e808 65 72 6d 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a  erm to be analyz
2e809 65 64 20 2a 2f 0a 29 7b 0a 20 20 50 61 72 73 65  ed */.){.  Parse
2e80a 20 2a 70 50 61 72 73 65 20 3d 20 70 57 43 2d 3e   *pParse = pWC->
2e80b 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20  pParse;         
2e80c 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e     /* Parser con
2e80d 74 65 78 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65  text */.  sqlite
2e80e 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2e80f 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  db;             
2e810 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
2e811 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 57 68  nnection */.  Wh
2e812 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 20 3d  ereTerm *pTerm =
2e813 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d   &pWC->a[idxTerm
2e814 5d 3b 20 20 20 20 2f 2a 20 54 68 65 20 74 65 72  ];    /* The ter
2e815 6d 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64  m to be analyzed
2e816 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70   */.  Expr *pExp
2e817 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72  r = pTerm->pExpr
2e818 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
2e819 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
2e81a 6f 66 20 74 68 65 20 74 65 72 6d 20 2a 2f 0a 20  of the term */. 
2e81b 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70   WhereMaskSet *p
2e81c 4d 61 73 6b 53 65 74 20 3d 20 70 57 43 2d 3e 70  MaskSet = pWC->p
2e81d 4d 61 73 6b 53 65 74 3b 20 2f 2a 20 54 61 62 6c  MaskSet; /* Tabl
2e81e 65 20 75 73 65 20 6d 61 73 6b 73 20 2a 2f 0a 20  e use masks */. 
2e81f 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
2e820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e821 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
2e822 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 57   counters */.  W
2e823 68 65 72 65 43 6c 61 75 73 65 20 2a 70 4f 72 57  hereClause *pOrW
2e824 63 3b 20 20 20 20 20 20 20 2f 2a 20 42 72 65 61  c;       /* Brea
2e825 6b 75 70 20 6f 66 20 70 54 65 72 6d 20 69 6e 74  kup of pTerm int
2e826 6f 20 73 75 62 74 65 72 6d 73 20 2a 2f 0a 20 20  o subterms */.  
2e827 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 72 54 65  WhereTerm *pOrTe
2e828 72 6d 3b 20 20 20 20 20 20 20 2f 2a 20 41 20 53  rm;       /* A S
2e829 75 62 2d 74 65 72 6d 20 77 69 74 68 69 6e 20 74  ub-term within t
2e82a 68 65 20 70 4f 72 57 63 20 2a 2f 0a 20 20 57 68  he pOrWc */.  Wh
2e82b 65 72 65 4f 72 49 6e 66 6f 20 2a 70 4f 72 49 6e  ereOrInfo *pOrIn
2e82c 66 6f 3b 20 20 20 20 20 2f 2a 20 41 64 64 69 74  fo;     /* Addit
2e82d 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
2e82e 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  n associated wit
2e82f 68 20 70 54 65 72 6d 20 2a 2f 0a 20 20 42 69 74  h pTerm */.  Bit
2e830 6d 61 73 6b 20 63 68 6e 67 54 6f 49 4e 3b 20 20  mask chngToIN;  
2e831 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 73         /* Tables
2e832 20 74 68 61 74 20 6d 69 67 68 74 20 73 61 74 69   that might sati
2e833 73 66 79 20 63 61 73 65 20 31 20 2a 2f 0a 20 20  sfy case 1 */.  
2e834 42 69 74 6d 61 73 6b 20 69 6e 64 65 78 61 62 6c  Bitmask indexabl
2e835 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62  e;        /* Tab
2e836 6c 65 73 20 74 68 61 74 20 61 72 65 20 69 6e 64  les that are ind
2e837 65 78 61 62 6c 65 2c 20 73 61 74 69 73 66 79 69  exable, satisfyi
2e838 6e 67 20 63 61 73 65 20 32 20 2a 2f 0a 0a 20 20  ng case 2 */..  
2e839 2f 2a 0a 20 20 2a 2a 20 42 72 65 61 6b 20 74 68  /*.  ** Break th
2e83a 65 20 4f 52 20 63 6c 61 75 73 65 20 69 6e 74 6f  e OR clause into
2e83b 20 69 74 73 20 73 65 70 61 72 61 74 65 20 73 75   its separate su
2e83c 62 74 65 72 6d 73 2e 20 20 54 68 65 20 73 75 62  bterms.  The sub
2e83d 74 65 72 6d 73 20 61 72 65 0a 20 20 2a 2a 20 73  terms are.  ** s
2e83e 74 6f 72 65 64 20 69 6e 20 61 20 57 68 65 72 65  tored in a Where
2e83f 43 6c 61 75 73 65 20 73 74 72 75 63 74 75 72 65  Clause structure
2e840 20 63 6f 6e 74 61 69 6e 69 6e 67 20 77 69 74 68   containing with
2e841 69 6e 20 74 68 65 20 57 68 65 72 65 4f 72 49 6e  in the WhereOrIn
2e842 66 6f 0a 20 20 2a 2a 20 6f 62 6a 65 63 74 20 74  fo.  ** object t
2e843 68 61 74 20 69 73 20 61 74 74 61 63 68 65 64 20  hat is attached 
2e844 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  to the original 
2e845 4f 52 20 63 6c 61 75 73 65 20 74 65 72 6d 2e 0a  OR clause term..
2e846 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28    */.  assert( (
2e847 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  pTerm->wtFlags &
2e848 20 28 54 45 52 4d 5f 44 59 4e 41 4d 49 43 7c 54   (TERM_DYNAMIC|T
2e849 45 52 4d 5f 4f 52 49 4e 46 4f 7c 54 45 52 4d 5f  ERM_ORINFO|TERM_
2e84a 41 4e 44 49 4e 46 4f 29 29 3d 3d 30 20 29 3b 0a  ANDINFO))==0 );.
2e84b 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
2e84c 3e 6f 70 3d 3d 54 4b 5f 4f 52 20 29 3b 0a 20 20  >op==TK_OR );.  
2e84d 70 54 65 72 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f  pTerm->u.pOrInfo
2e84e 20 3d 20 70 4f 72 49 6e 66 6f 20 3d 20 73 71 6c   = pOrInfo = sql
2e84f 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
2e850 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4f 72  (db, sizeof(*pOr
2e851 49 6e 66 6f 29 29 3b 0a 20 20 69 66 28 20 70 4f  Info));.  if( pO
2e852 72 49 6e 66 6f 3d 3d 30 20 29 20 72 65 74 75 72  rInfo==0 ) retur
2e853 6e 3b 0a 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c  n;.  pTerm->wtFl
2e854 61 67 73 20 7c 3d 20 54 45 52 4d 5f 4f 52 49 4e  ags |= TERM_ORIN
2e855 46 4f 3b 0a 20 20 70 4f 72 57 63 20 3d 20 26 70  FO;.  pOrWc = &p
2e856 4f 72 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 77 68  OrInfo->wc;.  wh
2e857 65 72 65 43 6c 61 75 73 65 49 6e 69 74 28 70 4f  ereClauseInit(pO
2e858 72 57 63 2c 20 70 57 43 2d 3e 70 50 61 72 73 65  rWc, pWC->pParse
2e859 2c 20 70 4d 61 73 6b 53 65 74 29 3b 0a 20 20 77  , pMaskSet);.  w
2e85a 68 65 72 65 53 70 6c 69 74 28 70 4f 72 57 63 2c  hereSplit(pOrWc,
2e85b 20 70 45 78 70 72 2c 20 54 4b 5f 4f 52 29 3b 0a   pExpr, TK_OR);.
2e85c 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c    exprAnalyzeAll
2e85d 28 70 53 72 63 2c 20 70 4f 72 57 63 29 3b 0a 20  (pSrc, pOrWc);. 
2e85e 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
2e85f 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a  ailed ) return;.
2e860 20 20 61 73 73 65 72 74 28 20 70 4f 72 57 63 2d    assert( pOrWc-
2e861 3e 6e 54 65 72 6d 3e 3d 32 20 29 3b 0a 0a 20 20  >nTerm>=2 );..  
2e862 2f 2a 0a 20 20 2a 2a 20 43 6f 6d 70 75 74 65 20  /*.  ** Compute 
2e863 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c 65  the set of table
2e864 73 20 74 68 61 74 20 6d 69 67 68 74 20 73 61 74  s that might sat
2e865 69 73 66 79 20 63 61 73 65 73 20 31 20 6f 72 20  isfy cases 1 or 
2e866 32 2e 0a 20 20 2a 2f 0a 20 20 69 6e 64 65 78 61  2..  */.  indexa
2e867 62 6c 65 20 3d 20 7e 28 42 69 74 6d 61 73 6b 29  ble = ~(Bitmask)
2e868 30 3b 0a 20 20 63 68 6e 67 54 6f 49 4e 20 3d 20  0;.  chngToIN = 
2e869 7e 28 70 57 43 2d 3e 76 6d 61 73 6b 29 3b 0a 20  ~(pWC->vmask);. 
2e86a 20 66 6f 72 28 69 3d 70 4f 72 57 63 2d 3e 6e 54   for(i=pOrWc->nT
2e86b 65 72 6d 2d 31 2c 20 70 4f 72 54 65 72 6d 3d 70  erm-1, pOrTerm=p
2e86c 4f 72 57 63 2d 3e 61 3b 20 69 3e 3d 30 20 26 26  OrWc->a; i>=0 &&
2e86d 20 69 6e 64 65 78 61 62 6c 65 3b 20 69 2d 2d 2c   indexable; i--,
2e86e 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 20   pOrTerm++){.   
2e86f 20 69 66 28 20 28 70 4f 72 54 65 72 6d 2d 3e 65   if( (pOrTerm->e
2e870 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 53 49  Operator & WO_SI
2e871 4e 47 4c 45 29 3d 3d 30 20 29 7b 0a 20 20 20 20  NGLE)==0 ){.    
2e872 20 20 57 68 65 72 65 41 6e 64 49 6e 66 6f 20 2a    WhereAndInfo *
2e873 70 41 6e 64 49 6e 66 6f 3b 0a 20 20 20 20 20 20  pAndInfo;.      
2e874 61 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d  assert( pOrTerm-
2e875 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 30 20 29 3b  >eOperator==0 );
2e876 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28  .      assert( (
2e877 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73  pOrTerm->wtFlags
2e878 20 26 20 28 54 45 52 4d 5f 41 4e 44 49 4e 46 4f   & (TERM_ANDINFO
2e879 7c 54 45 52 4d 5f 4f 52 49 4e 46 4f 29 29 3d 3d  |TERM_ORINFO))==
2e87a 30 20 29 3b 0a 20 20 20 20 20 20 63 68 6e 67 54  0 );.      chngT
2e87b 6f 49 4e 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  oIN = 0;.      p
2e87c 41 6e 64 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65  AndInfo = sqlite
2e87d 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
2e87e 20 73 69 7a 65 6f 66 28 2a 70 41 6e 64 49 6e 66   sizeof(*pAndInf
2e87f 6f 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  o));.      if( p
2e880 41 6e 64 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20  AndInfo ){.     
2e881 20 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a     WhereClause *
2e882 70 41 6e 64 57 43 3b 0a 20 20 20 20 20 20 20 20  pAndWC;.        
2e883 57 68 65 72 65 54 65 72 6d 20 2a 70 41 6e 64 54  WhereTerm *pAndT
2e884 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  erm;.        int
2e885 20 6a 3b 0a 20 20 20 20 20 20 20 20 42 69 74 6d   j;.        Bitm
2e886 61 73 6b 20 62 20 3d 20 30 3b 0a 20 20 20 20 20  ask b = 0;.     
2e887 20 20 20 70 4f 72 54 65 72 6d 2d 3e 75 2e 70 41     pOrTerm->u.pA
2e888 6e 64 49 6e 66 6f 20 3d 20 70 41 6e 64 49 6e 66  ndInfo = pAndInf
2e889 6f 3b 0a 20 20 20 20 20 20 20 20 70 4f 72 54 65  o;.        pOrTe
2e88a 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54  rm->wtFlags |= T
2e88b 45 52 4d 5f 41 4e 44 49 4e 46 4f 3b 0a 20 20 20  ERM_ANDINFO;.   
2e88c 20 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e 65 4f       pOrTerm->eO
2e88d 70 65 72 61 74 6f 72 20 3d 20 57 4f 5f 41 4e 44  perator = WO_AND
2e88e 3b 0a 20 20 20 20 20 20 20 20 70 41 6e 64 57 43  ;.        pAndWC
2e88f 20 3d 20 26 70 41 6e 64 49 6e 66 6f 2d 3e 77 63   = &pAndInfo->wc
2e890 3b 0a 20 20 20 20 20 20 20 20 77 68 65 72 65 43  ;.        whereC
2e891 6c 61 75 73 65 49 6e 69 74 28 70 41 6e 64 57 43  lauseInit(pAndWC
2e892 2c 20 70 57 43 2d 3e 70 50 61 72 73 65 2c 20 70  , pWC->pParse, p
2e893 4d 61 73 6b 53 65 74 29 3b 0a 20 20 20 20 20 20  MaskSet);.      
2e894 20 20 77 68 65 72 65 53 70 6c 69 74 28 70 41 6e    whereSplit(pAn
2e895 64 57 43 2c 20 70 4f 72 54 65 72 6d 2d 3e 70 45  dWC, pOrTerm->pE
2e896 78 70 72 2c 20 54 4b 5f 41 4e 44 29 3b 0a 20 20  xpr, TK_AND);.  
2e897 20 20 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a        exprAnalyz
2e898 65 41 6c 6c 28 70 53 72 63 2c 20 70 41 6e 64 57  eAll(pSrc, pAndW
2e899 43 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74  C);.        test
2e89a 63 61 73 65 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63  case( db->malloc
2e89b 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20  Failed );.      
2e89c 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f    if( !db->mallo
2e89d 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20  cFailed ){.     
2e89e 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 41       for(j=0, pA
2e89f 6e 64 54 65 72 6d 3d 70 41 6e 64 57 43 2d 3e 61  ndTerm=pAndWC->a
2e8a0 3b 20 6a 3c 70 41 6e 64 57 43 2d 3e 6e 54 65 72  ; j<pAndWC->nTer
2e8a1 6d 3b 20 6a 2b 2b 2c 20 70 41 6e 64 54 65 72 6d  m; j++, pAndTerm
2e8a2 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
2e8a3 20 61 73 73 65 72 74 28 20 70 41 6e 64 54 65 72   assert( pAndTer
2e8a4 6d 2d 3e 70 45 78 70 72 20 29 3b 0a 20 20 20 20  m->pExpr );.    
2e8a5 20 20 20 20 20 20 20 20 69 66 28 20 61 6c 6c 6f          if( allo
2e8a6 77 65 64 4f 70 28 70 41 6e 64 54 65 72 6d 2d 3e  wedOp(pAndTerm->
2e8a7 70 45 78 70 72 2d 3e 6f 70 29 20 29 7b 0a 20 20  pExpr->op) ){.  
2e8a8 20 20 20 20 20 20 20 20 20 20 20 20 62 20 7c 3d              b |=
2e8a9 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65   getMask(pMaskSe
2e8aa 74 2c 20 70 41 6e 64 54 65 72 6d 2d 3e 6c 65 66  t, pAndTerm->lef
2e8ab 74 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20  tCursor);.      
2e8ac 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2e8ad 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
2e8ae 20 20 20 20 20 20 69 6e 64 65 78 61 62 6c 65 20        indexable 
2e8af 26 3d 20 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20  &= b;.      }.  
2e8b0 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 72 54    }else if( pOrT
2e8b1 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54  erm->wtFlags & T
2e8b2 45 52 4d 5f 43 4f 50 49 45 44 20 29 7b 0a 20 20  ERM_COPIED ){.  
2e8b3 20 20 20 20 2f 2a 20 53 6b 69 70 20 74 68 69 73      /* Skip this
2e8b4 20 74 65 72 6d 20 66 6f 72 20 6e 6f 77 2e 20 20   term for now.  
2e8b5 57 65 20 72 65 76 69 73 69 74 20 69 74 20 77 68  We revisit it wh
2e8b6 65 6e 20 77 65 20 70 72 6f 63 65 73 73 20 74 68  en we process th
2e8b7 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f 72 72 65  e.      ** corre
2e8b8 73 70 6f 6e 64 69 6e 67 20 54 45 52 4d 5f 56 49  sponding TERM_VI
2e8b9 52 54 55 41 4c 20 74 65 72 6d 20 2a 2f 0a 20 20  RTUAL term */.  
2e8ba 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 42    }else{.      B
2e8bb 69 74 6d 61 73 6b 20 62 3b 0a 20 20 20 20 20 20  itmask b;.      
2e8bc 62 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73  b = getMask(pMas
2e8bd 6b 53 65 74 2c 20 70 4f 72 54 65 72 6d 2d 3e 6c  kSet, pOrTerm->l
2e8be 65 66 74 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  eftCursor);.    
2e8bf 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 77    if( pOrTerm->w
2e8c0 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 49  tFlags & TERM_VI
2e8c1 52 54 55 41 4c 20 29 7b 0a 20 20 20 20 20 20 20  RTUAL ){.       
2e8c2 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 74 68   WhereTerm *pOth
2e8c3 65 72 20 3d 20 26 70 4f 72 57 63 2d 3e 61 5b 70  er = &pOrWc->a[p
2e8c4 4f 72 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 5d  OrTerm->iParent]
2e8c5 3b 0a 20 20 20 20 20 20 20 20 62 20 7c 3d 20 67  ;.        b |= g
2e8c6 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c  etMask(pMaskSet,
2e8c7 20 70 4f 74 68 65 72 2d 3e 6c 65 66 74 43 75 72   pOther->leftCur
2e8c8 73 6f 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  sor);.      }.  
2e8c9 20 20 20 20 69 6e 64 65 78 61 62 6c 65 20 26 3d      indexable &=
2e8ca 20 62 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f   b;.      if( pO
2e8cb 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  rTerm->eOperator
2e8cc 21 3d 57 4f 5f 45 51 20 29 7b 0a 20 20 20 20 20  !=WO_EQ ){.     
2e8cd 20 20 20 63 68 6e 67 54 6f 49 4e 20 3d 20 30 3b     chngToIN = 0;
2e8ce 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2e8cf 20 20 20 20 20 20 63 68 6e 67 54 6f 49 4e 20 26        chngToIN &
2e8d0 3d 20 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = b;.      }.   
2e8d1 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a   }.  }..  /*.  *
2e8d2 2a 20 52 65 63 6f 72 64 20 74 68 65 20 73 65 74  * Record the set
2e8d3 20 6f 66 20 74 61 62 6c 65 73 20 74 68 61 74 20   of tables that 
2e8d4 73 61 74 69 73 66 79 20 63 61 73 65 20 32 2e 20  satisfy case 2. 
2e8d5 20 54 68 65 20 73 65 74 20 6d 69 67 68 74 20 62   The set might b
2e8d6 65 0a 20 20 2a 2a 20 65 6d 70 74 79 2e 0a 20 20  e.  ** empty..  
2e8d7 2a 2f 0a 20 20 70 4f 72 49 6e 66 6f 2d 3e 69 6e  */.  pOrInfo->in
2e8d8 64 65 78 61 62 6c 65 20 3d 20 69 6e 64 65 78 61  dexable = indexa
2e8d9 62 6c 65 3b 0a 20 20 70 54 65 72 6d 2d 3e 65 4f  ble;.  pTerm->eO
2e8da 70 65 72 61 74 6f 72 20 3d 20 69 6e 64 65 78 61  perator = indexa
2e8db 62 6c 65 3d 3d 30 20 3f 20 30 20 3a 20 57 4f 5f  ble==0 ? 0 : WO_
2e8dc 4f 52 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 63  OR;..  /*.  ** c
2e8dd 68 6e 67 54 6f 49 4e 20 68 6f 6c 64 73 20 61 20  hngToIN holds a 
2e8de 73 65 74 20 6f 66 20 74 61 62 6c 65 73 20 74 68  set of tables th
2e8df 61 74 20 2a 6d 69 67 68 74 2a 20 73 61 74 69 73  at *might* satis
2e8e0 66 79 20 63 61 73 65 20 31 2e 20 20 42 75 74 0a  fy case 1.  But.
2e8e1 20 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20    ** we have to 
2e8e2 64 6f 20 73 6f 6d 65 20 61 64 64 69 74 69 6f 6e  do some addition
2e8e3 61 6c 20 63 68 65 63 6b 69 6e 67 20 74 6f 20 73  al checking to s
2e8e4 65 65 20 69 66 20 63 61 73 65 20 31 20 72 65 61  ee if case 1 rea
2e8e5 6c 6c 79 0a 20 20 2a 2a 20 69 73 20 73 61 74 69  lly.  ** is sati
2e8e6 73 66 69 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  sfied..  **.  **
2e8e7 20 63 68 6e 67 54 6f 49 4e 20 77 69 6c 6c 20 68   chngToIN will h
2e8e8 6f 6c 64 20 65 69 74 68 65 72 20 30 2c 20 31 2c  old either 0, 1,
2e8e9 20 6f 72 20 32 20 62 69 74 73 2e 20 20 54 68 65   or 2 bits.  The
2e8ea 20 30 2d 62 69 74 20 63 61 73 65 20 6d 65 61 6e   0-bit case mean
2e8eb 73 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 72  s.  ** that ther
2e8ec 65 20 69 73 20 6e 6f 20 70 6f 73 73 69 62 69 6c  e is no possibil
2e8ed 69 74 79 20 6f 66 20 74 72 61 6e 73 66 6f 72 6d  ity of transform
2e8ee 69 6e 67 20 74 68 65 20 4f 52 20 63 6c 61 75 73  ing the OR claus
2e8ef 65 20 69 6e 74 6f 20 61 6e 0a 20 20 2a 2a 20 49  e into an.  ** I
2e8f0 4e 20 6f 70 65 72 61 74 6f 72 20 62 65 63 61 75  N operator becau
2e8f1 73 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74  se one or more t
2e8f2 65 72 6d 73 20 69 6e 20 74 68 65 20 4f 52 20 63  erms in the OR c
2e8f3 6c 61 75 73 65 20 63 6f 6e 74 61 69 6e 0a 20 20  lause contain.  
2e8f4 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68  ** something oth
2e8f5 65 72 20 74 68 61 6e 20 3d 3d 20 6f 6e 20 61 20  er than == on a 
2e8f6 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 73 69  column in the si
2e8f7 6e 67 6c 65 20 74 61 62 6c 65 2e 20 20 54 68 65  ngle table.  The
2e8f8 20 31 2d 62 69 74 0a 20 20 2a 2a 20 63 61 73 65   1-bit.  ** case
2e8f9 20 6d 65 61 6e 73 20 74 68 61 74 20 65 76 65 72   means that ever
2e8fa 79 20 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52  y term of the OR
2e8fb 20 63 6c 61 75 73 65 20 69 73 20 6f 66 20 74 68   clause is of th
2e8fc 65 20 66 6f 72 6d 0a 20 20 2a 2a 20 22 74 61 62  e form.  ** "tab
2e8fd 6c 65 2e 63 6f 6c 75 6d 6e 3d 65 78 70 72 22 20  le.column=expr" 
2e8fe 66 6f 72 20 73 6f 6d 65 20 73 69 6e 67 6c 65 20  for some single 
2e8ff 74 61 62 6c 65 2e 20 20 54 68 65 20 6f 6e 65 20  table.  The one 
2e900 62 69 74 20 74 68 61 74 20 69 73 20 73 65 74 0a  bit that is set.
2e901 20 20 2a 2a 20 77 69 6c 6c 20 63 6f 72 72 65 73    ** will corres
2e902 70 6f 6e 64 20 74 6f 20 74 68 65 20 63 6f 6d 6d  pond to the comm
2e903 6f 6e 20 74 61 62 6c 65 2e 20 20 57 65 20 73 74  on table.  We st
2e904 69 6c 6c 20 6e 65 65 64 20 74 6f 20 63 68 65 63  ill need to chec
2e905 6b 20 74 6f 20 6d 61 6b 65 0a 20 20 2a 2a 20 73  k to make.  ** s
2e906 75 72 65 20 74 68 65 20 73 61 6d 65 20 63 6f 6c  ure the same col
2e907 75 6d 6e 20 69 73 20 75 73 65 64 20 6f 6e 20 61  umn is used on a
2e908 6c 6c 20 74 65 72 6d 73 2e 20 20 54 68 65 20 32  ll terms.  The 2
2e909 2d 62 69 74 20 63 61 73 65 20 69 73 20 77 68 65  -bit case is whe
2e90a 6e 0a 20 20 2a 2a 20 74 68 65 20 61 6c 6c 20 74  n.  ** the all t
2e90b 65 72 6d 73 20 61 72 65 20 6f 66 20 74 68 65 20  erms are of the 
2e90c 66 6f 72 6d 20 22 74 61 62 6c 65 31 2e 63 6f 6c  form "table1.col
2e90d 75 6d 6e 3d 74 61 62 6c 65 32 2e 63 6f 6c 75 6d  umn=table2.colum
2e90e 6e 22 2e 20 20 49 74 0a 20 20 2a 2a 20 6d 69 67  n".  It.  ** mig
2e90f 68 74 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74  ht be possible t
2e910 6f 20 66 6f 72 6d 20 61 6e 20 49 4e 20 6f 70 65  o form an IN ope
2e911 72 61 74 6f 72 20 77 69 74 68 20 65 69 74 68 65  rator with eithe
2e912 72 20 74 61 62 6c 65 31 2e 63 6f 6c 75 6d 6e 0a  r table1.column.
2e913 20 20 2a 2a 20 6f 72 20 74 61 62 6c 65 32 2e 63    ** or table2.c
2e914 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 4c 48 53  olumn as the LHS
2e915 20 69 66 20 65 69 74 68 65 72 20 69 73 20 63 6f   if either is co
2e916 6d 6d 6f 6e 20 74 6f 20 65 76 65 72 79 20 74 65  mmon to every te
2e917 72 6d 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 4f  rm of.  ** the O
2e918 52 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 0a 20  R clause..  **. 
2e919 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 65   ** Note that te
2e91a 72 6d 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  rms of the form 
2e91b 22 74 61 62 6c 65 2e 63 6f 6c 75 6d 6e 31 3d 74  "table.column1=t
2e91c 61 62 6c 65 2e 63 6f 6c 75 6d 6e 32 22 20 28 74  able.column2" (t
2e91d 68 65 0a 20 20 2a 2a 20 73 61 6d 65 20 74 61 62  he.  ** same tab
2e91e 6c 65 20 6f 6e 20 62 6f 74 68 20 73 69 7a 65 73  le on both sizes
2e91f 20 6f 66 20 74 68 65 20 3d 3d 29 20 63 61 6e 6e   of the ==) cann
2e920 6f 74 20 62 65 20 6f 70 74 69 6d 69 7a 65 64 2e  ot be optimized.
2e921 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 68 6e 67  .  */.  if( chng
2e922 54 6f 49 4e 20 29 7b 0a 20 20 20 20 69 6e 74 20  ToIN ){.    int 
2e923 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 3d 20 30  okToChngToIN = 0
2e924 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  ;     /* True if
2e925 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20   the conversion 
2e926 74 6f 20 49 4e 20 69 73 20 76 61 6c 69 64 20 2a  to IN is valid *
2e927 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d  /.    int iColum
2e928 6e 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20  n = -1;         
2e929 2f 2a 20 43 6f 6c 75 6d 6e 20 69 6e 64 65 78 20  /* Column index 
2e92a 6f 6e 20 6c 68 73 20 6f 66 20 49 4e 20 6f 70 65  on lhs of IN ope
2e92b 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69 6e 74  rator */.    int
2e92c 20 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b 20 20   iCursor = -1;  
2e92d 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
2e92e 63 75 72 73 6f 72 20 63 6f 6d 6d 6f 6e 20 74 6f  cursor common to
2e92f 20 61 6c 6c 20 74 65 72 6d 73 20 2a 2f 0a 20 20   all terms */.  
2e930 20 20 69 6e 74 20 6a 20 3d 20 30 3b 20 20 20 20    int j = 0;    
2e931 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2e932 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a  oop counter */..
2e933 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f      /* Search fo
2e934 72 20 61 20 74 61 62 6c 65 20 61 6e 64 20 63 6f  r a table and co
2e935 6c 75 6d 6e 20 74 68 61 74 20 61 70 70 65 61 72  lumn that appear
2e936 73 20 6f 6e 20 6f 6e 65 20 73 69 64 65 20 6f 72  s on one side or
2e937 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f 74 68 65   the.    ** othe
2e938 72 20 6f 66 20 74 68 65 20 3d 3d 20 6f 70 65 72  r of the == oper
2e939 61 74 6f 72 20 69 6e 20 65 76 65 72 79 20 73 75  ator in every su
2e93a 62 74 65 72 6d 2e 20 20 54 68 61 74 20 74 61 62  bterm.  That tab
2e93b 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a 20 20  le and column.  
2e93c 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 72 65 63    ** will be rec
2e93d 6f 72 64 65 64 20 69 6e 20 69 43 75 72 73 6f 72  orded in iCursor
2e93e 20 61 6e 64 20 69 43 6f 6c 75 6d 6e 2e 20 20 54   and iColumn.  T
2e93f 68 65 72 65 20 6d 69 67 68 74 20 6e 6f 74 20 62  here might not b
2e940 65 20 61 6e 79 0a 20 20 20 20 2a 2a 20 73 75 63  e any.    ** suc
2e941 68 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75  h table and colu
2e942 6d 6e 2e 20 20 53 65 74 20 6f 6b 54 6f 43 68 6e  mn.  Set okToChn
2e943 67 54 6f 49 4e 20 69 66 20 61 6e 20 61 70 70 72  gToIN if an appr
2e944 6f 70 72 69 61 74 65 20 74 61 62 6c 65 0a 20 20  opriate table.  
2e945 20 20 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20    ** and column 
2e946 69 73 20 66 6f 75 6e 64 20 62 75 74 20 6c 65 61  is found but lea
2e947 76 65 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20  ve okToChngToIN 
2e948 66 61 6c 73 65 20 69 66 20 6e 6f 74 20 66 6f 75  false if not fou
2e949 6e 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66  nd..    */.    f
2e94a 6f 72 28 6a 3d 30 3b 20 6a 3c 32 20 26 26 20 21  or(j=0; j<2 && !
2e94b 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 3b 20 6a 2b  okToChngToIN; j+
2e94c 2b 29 7b 0a 20 20 20 20 20 20 70 4f 72 54 65 72  +){.      pOrTer
2e94d 6d 20 3d 20 70 4f 72 57 63 2d 3e 61 3b 0a 20 20  m = pOrWc->a;.  
2e94e 20 20 20 20 66 6f 72 28 69 3d 70 4f 72 57 63 2d      for(i=pOrWc-
2e94f 3e 6e 54 65 72 6d 2d 31 3b 20 69 3e 3d 30 3b 20  >nTerm-1; i>=0; 
2e950 69 2d 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 7b  i--, pOrTerm++){
2e951 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2e952 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61   pOrTerm->eOpera
2e953 74 6f 72 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20 20  tor==WO_EQ );.  
2e954 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e 77        pOrTerm->w
2e955 74 46 6c 61 67 73 20 26 3d 20 7e 54 45 52 4d 5f  tFlags &= ~TERM_
2e956 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 69  OR_OK;.        i
2e957 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74  f( pOrTerm->left
2e958 43 75 72 73 6f 72 3d 3d 69 43 75 72 73 6f 72 20  Cursor==iCursor 
2e959 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
2e95a 54 68 69 73 20 69 73 20 74 68 65 20 32 2d 62 69  This is the 2-bi
2e95b 74 20 63 61 73 65 20 61 6e 64 20 77 65 20 61 72  t case and we ar
2e95c 65 20 6f 6e 20 74 68 65 20 73 65 63 6f 6e 64 20  e on the second 
2e95d 69 74 65 72 61 74 69 6f 6e 20 61 6e 64 0a 20 20  iteration and.  
2e95e 20 20 20 20 20 20 20 20 2a 2a 20 63 75 72 72 65          ** curre
2e95f 6e 74 20 74 65 72 6d 20 69 73 20 66 72 6f 6d 20  nt term is from 
2e960 74 68 65 20 66 69 72 73 74 20 69 74 65 72 61 74  the first iterat
2e961 69 6f 6e 2e 20 20 53 6f 20 73 6b 69 70 20 74 68  ion.  So skip th
2e962 69 73 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 20 20  is term. */.    
2e963 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6a 3d        assert( j=
2e964 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  =1 );.          
2e965 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
2e966 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
2e967 28 63 68 6e 67 54 6f 49 4e 20 26 20 67 65 74 4d  (chngToIN & getM
2e968 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 4f  ask(pMaskSet, pO
2e969 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  rTerm->leftCurso
2e96a 72 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  r))==0 ){.      
2e96b 20 20 20 20 2f 2a 20 54 68 69 73 20 74 65 72 6d      /* This term
2e96c 20 6d 75 73 74 20 62 65 20 6f 66 20 74 68 65 20   must be of the 
2e96d 66 6f 72 6d 20 74 31 2e 61 3d 3d 74 32 2e 62 20  form t1.a==t2.b 
2e96e 77 68 65 72 65 20 74 32 20 69 73 20 69 6e 20 74  where t2 is in t
2e96f 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  he.          ** 
2e970 63 68 6e 67 54 6f 49 4e 20 73 65 74 20 62 75 74  chngToIN set but
2e971 20 74 31 20 69 73 20 6e 6f 74 2e 20 20 54 68 69   t1 is not.  Thi
2e972 73 20 74 65 72 6d 20 77 69 6c 6c 20 62 65 20 65  s term will be e
2e973 69 74 68 65 72 20 70 72 65 63 65 65 64 65 64 0a  ither preceeded.
2e974 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20            ** or 
2e975 66 6f 6c 6c 77 65 64 20 62 79 20 61 6e 20 69 6e  follwed by an in
2e976 76 65 72 74 65 64 20 63 6f 70 79 20 28 74 32 2e  verted copy (t2.
2e977 62 3d 3d 74 31 2e 61 29 2e 20 20 53 6b 69 70 20  b==t1.a).  Skip 
2e978 74 68 69 73 20 74 65 72 6d 20 0a 20 20 20 20 20  this term .     
2e979 20 20 20 20 20 2a 2a 20 61 6e 64 20 75 73 65 20       ** and use 
2e97a 69 74 73 20 69 6e 76 65 72 73 69 6f 6e 2e 20 2a  its inversion. *
2e97b 2f 0a 20 20 20 20 20 20 20 20 20 20 74 65 73 74  /.          test
2e97c 63 61 73 65 28 20 70 4f 72 54 65 72 6d 2d 3e 77  case( pOrTerm->w
2e97d 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f  tFlags & TERM_CO
2e97e 50 49 45 44 20 29 3b 0a 20 20 20 20 20 20 20 20  PIED );.        
2e97f 20 20 74 65 73 74 63 61 73 65 28 20 70 4f 72 54    testcase( pOrT
2e980 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54  erm->wtFlags & T
2e981 45 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b 0a 20  ERM_VIRTUAL );. 
2e982 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
2e983 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67   pOrTerm->wtFlag
2e984 73 20 26 20 28 54 45 52 4d 5f 43 4f 50 49 45 44  s & (TERM_COPIED
2e985 7c 54 45 52 4d 5f 56 49 52 54 55 41 4c 29 20 29  |TERM_VIRTUAL) )
2e986 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74  ;.          cont
2e987 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  inue;.        }.
2e988 20 20 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20          iColumn 
2e989 3d 20 70 4f 72 54 65 72 6d 2d 3e 75 2e 6c 65 66  = pOrTerm->u.lef
2e98a 74 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20  tColumn;.       
2e98b 20 69 43 75 72 73 6f 72 20 3d 20 70 4f 72 54 65   iCursor = pOrTe
2e98c 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3b 0a  rm->leftCursor;.
2e98d 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2e98e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
2e98f 20 69 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20   i<0 ){.        
2e990 2f 2a 20 4e 6f 20 63 61 6e 64 69 64 61 74 65 20  /* No candidate 
2e991 74 61 62 6c 65 2b 63 6f 6c 75 6d 6e 20 77 61 73  table+column was
2e992 20 66 6f 75 6e 64 2e 20 20 54 68 69 73 20 63 61   found.  This ca
2e993 6e 20 6f 6e 6c 79 20 6f 63 63 75 72 0a 20 20 20  n only occur.   
2e994 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 73       ** on the s
2e995 65 63 6f 6e 64 20 69 74 65 72 61 74 69 6f 6e 20  econd iteration 
2e996 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  */.        asser
2e997 74 28 20 6a 3d 3d 31 20 29 3b 0a 20 20 20 20 20  t( j==1 );.     
2e998 20 20 20 61 73 73 65 72 74 28 20 28 63 68 6e 67     assert( (chng
2e999 54 6f 49 4e 26 28 63 68 6e 67 54 6f 49 4e 2d 31  ToIN&(chngToIN-1
2e99a 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  ))==0 );.       
2e99b 20 61 73 73 65 72 74 28 20 63 68 6e 67 54 6f 49   assert( chngToI
2e99c 4e 3d 3d 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b  N==getMask(pMask
2e99d 53 65 74 2c 20 69 43 75 72 73 6f 72 29 20 29 3b  Set, iCursor) );
2e99e 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2e99f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 74 65        }.      te
2e9a0 73 74 63 61 73 65 28 20 6a 3d 3d 31 20 29 3b 0a  stcase( j==1 );.
2e9a1 0a 20 20 20 20 20 20 2f 2a 20 57 65 20 68 61 76  .      /* We hav
2e9a2 65 20 66 6f 75 6e 64 20 61 20 63 61 6e 64 69 64  e found a candid
2e9a3 61 74 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f  ate table and co
2e9a4 6c 75 6d 6e 2e 20 20 43 68 65 63 6b 20 74 6f 20  lumn.  Check to 
2e9a5 73 65 65 20 69 66 20 74 68 61 74 0a 20 20 20 20  see if that.    
2e9a6 20 20 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 63    ** table and c
2e9a7 6f 6c 75 6d 6e 20 69 73 20 63 6f 6d 6d 6f 6e 20  olumn is common 
2e9a8 74 6f 20 65 76 65 72 79 20 74 65 72 6d 20 69 6e  to every term in
2e9a9 20 74 68 65 20 4f 52 20 63 6c 61 75 73 65 20 2a   the OR clause *
2e9aa 2f 0a 20 20 20 20 20 20 6f 6b 54 6f 43 68 6e 67  /.      okToChng
2e9ab 54 6f 49 4e 20 3d 20 31 3b 0a 20 20 20 20 20 20  ToIN = 1;.      
2e9ac 66 6f 72 28 3b 20 69 3e 3d 30 20 26 26 20 6f 6b  for(; i>=0 && ok
2e9ad 54 6f 43 68 6e 67 54 6f 49 4e 3b 20 69 2d 2d 2c  ToChngToIN; i--,
2e9ae 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 20   pOrTerm++){.   
2e9af 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72       assert( pOr
2e9b0 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d  Term->eOperator=
2e9b1 3d 57 4f 5f 45 51 20 29 3b 0a 20 20 20 20 20 20  =WO_EQ );.      
2e9b2 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c    if( pOrTerm->l
2e9b3 65 66 74 43 75 72 73 6f 72 21 3d 69 43 75 72 73  eftCursor!=iCurs
2e9b4 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  or ){.          
2e9b5 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73  pOrTerm->wtFlags
2e9b6 20 26 3d 20 7e 54 45 52 4d 5f 4f 52 5f 4f 4b 3b   &= ~TERM_OR_OK;
2e9b7 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
2e9b8 66 28 20 70 4f 72 54 65 72 6d 2d 3e 75 2e 6c 65  f( pOrTerm->u.le
2e9b9 66 74 43 6f 6c 75 6d 6e 21 3d 69 43 6f 6c 75 6d  ftColumn!=iColum
2e9ba 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6f  n ){.          o
2e9bb 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 3d 20 30 3b  kToChngToIN = 0;
2e9bc 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
2e9bd 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 61 66            int af
2e9be 66 4c 65 66 74 2c 20 61 66 66 52 69 67 68 74 3b  fLeft, affRight;
2e9bf 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66  .          /* If
2e9c0 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20   the right-hand 
2e9c1 73 69 64 65 20 69 73 20 61 6c 73 6f 20 61 20 63  side is also a c
2e9c2 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65 20  olumn, then the 
2e9c3 61 66 66 69 6e 69 74 69 65 73 0a 20 20 20 20 20  affinities.     
2e9c4 20 20 20 20 20 2a 2a 20 6f 66 20 62 6f 74 68 20       ** of both 
2e9c5 72 69 67 68 74 20 61 6e 64 20 6c 65 66 74 20 73  right and left s
2e9c6 69 64 65 73 20 6d 75 73 74 20 62 65 20 73 75 63  ides must be suc
2e9c7 68 20 74 68 61 74 20 6e 6f 20 74 79 70 65 0a 20  h that no type. 
2e9c8 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 76           ** conv
2e9c9 65 72 73 69 6f 6e 73 20 61 72 65 20 72 65 71 75  ersions are requ
2e9ca 69 72 65 64 20 6f 6e 20 74 68 65 20 72 69 67 68  ired on the righ
2e9cb 74 2e 20 20 28 54 69 63 6b 65 74 20 23 32 32 34  t.  (Ticket #224
2e9cc 39 29 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  9).          */.
2e9cd 20 20 20 20 20 20 20 20 20 20 61 66 66 52 69 67            affRig
2e9ce 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ht = sqlite3Expr
2e9cf 41 66 66 69 6e 69 74 79 28 70 4f 72 54 65 72 6d  Affinity(pOrTerm
2e9d0 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29  ->pExpr->pRight)
2e9d1 3b 0a 20 20 20 20 20 20 20 20 20 20 61 66 66 4c  ;.          affL
2e9d2 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  eft = sqlite3Exp
2e9d3 72 41 66 66 69 6e 69 74 79 28 70 4f 72 54 65 72  rAffinity(pOrTer
2e9d4 6d 2d 3e 70 45 78 70 72 2d 3e 70 4c 65 66 74 29  m->pExpr->pLeft)
2e9d5 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
2e9d6 61 66 66 52 69 67 68 74 21 3d 30 20 26 26 20 61  affRight!=0 && a
2e9d7 66 66 52 69 67 68 74 21 3d 61 66 66 4c 65 66 74  ffRight!=affLeft
2e9d8 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2e9d9 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 3d 20 30  okToChngToIN = 0
2e9da 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
2e9db 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  e{.            p
2e9dc 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20  OrTerm->wtFlags 
2e9dd 7c 3d 20 54 45 52 4d 5f 4f 52 5f 4f 4b 3b 0a 20  |= TERM_OR_OK;. 
2e9de 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2e9df 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
2e9e0 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 74 68   }..    /* At th
2e9e1 69 73 20 70 6f 69 6e 74 2c 20 6f 6b 54 6f 43 68  is point, okToCh
2e9e2 6e 67 54 6f 49 4e 20 69 73 20 74 72 75 65 20 69  ngToIN is true i
2e9e3 66 20 6f 72 69 67 69 6e 61 6c 20 70 54 65 72 6d  f original pTerm
2e9e4 20 73 61 74 69 73 66 69 65 73 0a 20 20 20 20 2a   satisfies.    *
2e9e5 2a 20 63 61 73 65 20 31 2e 20 20 49 6e 20 74 68  * case 1.  In th
2e9e6 61 74 20 63 61 73 65 2c 20 63 6f 6e 73 74 72 75  at case, constru
2e9e7 63 74 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c  ct a new virtual
2e9e8 20 74 65 72 6d 20 74 68 61 74 20 69 73 20 0a 20   term that is . 
2e9e9 20 20 20 2a 2a 20 70 54 65 72 6d 20 63 6f 6e 76     ** pTerm conv
2e9ea 65 72 74 65 64 20 69 6e 74 6f 20 61 6e 20 49 4e  erted into an IN
2e9eb 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20 20 20 2a   operator..    *
2e9ec 2f 0a 20 20 20 20 69 66 28 20 6f 6b 54 6f 43 68  /.    if( okToCh
2e9ed 6e 67 54 6f 49 4e 20 29 7b 0a 20 20 20 20 20 20  ngToIN ){.      
2e9ee 45 78 70 72 20 2a 70 44 75 70 3b 20 20 20 20 20  Expr *pDup;     
2e9ef 20 20 20 20 20 20 20 2f 2a 20 41 20 74 72 61 6e         /* A tran
2e9f0 73 69 65 6e 74 20 64 75 70 6c 69 63 61 74 65 20  sient duplicate 
2e9f1 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
2e9f2 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c      ExprList *pL
2e9f3 69 73 74 20 3d 20 30 3b 20 20 20 2f 2a 20 54 68  ist = 0;   /* Th
2e9f4 65 20 52 48 53 20 6f 66 20 74 68 65 20 49 4e 20  e RHS of the IN 
2e9f5 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20  operator */.    
2e9f6 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20    Expr *pLeft = 
2e9f7 30 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  0;       /* The 
2e9f8 4c 48 53 20 6f 66 20 74 68 65 20 49 4e 20 6f 70  LHS of the IN op
2e9f9 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20  erator */.      
2e9fa 45 78 70 72 20 2a 70 4e 65 77 3b 20 20 20 20 20  Expr *pNew;     
2e9fb 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f         /* The co
2e9fc 6d 70 6c 65 74 65 20 49 4e 20 6f 70 65 72 61 74  mplete IN operat
2e9fd 6f 72 20 2a 2f 0a 0a 20 20 20 20 20 20 66 6f 72  or */..      for
2e9fe 28 69 3d 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d  (i=pOrWc->nTerm-
2e9ff 31 2c 20 70 4f 72 54 65 72 6d 3d 70 4f 72 57 63  1, pOrTerm=pOrWc
2ea00 2d 3e 61 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20  ->a; i>=0; i--, 
2ea01 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20  pOrTerm++){.    
2ea02 20 20 20 20 69 66 28 20 28 70 4f 72 54 65 72 6d      if( (pOrTerm
2ea03 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d  ->wtFlags & TERM
2ea04 5f 4f 52 5f 4f 4b 29 3d 3d 30 20 29 20 63 6f 6e  _OR_OK)==0 ) con
2ea05 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 61  tinue;.        a
2ea06 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e  ssert( pOrTerm->
2ea07 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 45 51  eOperator==WO_EQ
2ea08 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65   );.        asse
2ea09 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66  rt( pOrTerm->lef
2ea0a 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 73 6f 72  tCursor==iCursor
2ea0b 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65   );.        asse
2ea0c 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e 75 2e 6c  rt( pOrTerm->u.l
2ea0d 65 66 74 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75  eftColumn==iColu
2ea0e 6d 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 70 44  mn );.        pD
2ea0f 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  up = sqlite3Expr
2ea10 44 75 70 28 64 62 2c 20 70 4f 72 54 65 72 6d 2d  Dup(db, pOrTerm-
2ea11 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20  >pExpr->pRight, 
2ea12 30 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 69 73  0);.        pLis
2ea13 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  t = sqlite3ExprL
2ea14 69 73 74 41 70 70 65 6e 64 28 70 57 43 2d 3e 70  istAppend(pWC->p
2ea15 50 61 72 73 65 2c 20 70 4c 69 73 74 2c 20 70 44  Parse, pList, pD
2ea16 75 70 29 3b 0a 20 20 20 20 20 20 20 20 70 4c 65  up);.        pLe
2ea17 66 74 20 3d 20 70 4f 72 54 65 72 6d 2d 3e 70 45  ft = pOrTerm->pE
2ea18 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20  xpr->pLeft;.    
2ea19 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74    }.      assert
2ea1a 28 20 70 4c 65 66 74 21 3d 30 20 29 3b 0a 20 20  ( pLeft!=0 );.  
2ea1b 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74      pDup = sqlit
2ea1c 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4c  e3ExprDup(db, pL
2ea1d 65 66 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 70  eft, 0);.      p
2ea1e 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 50 45 78  New = sqlite3PEx
2ea1f 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e  pr(pParse, TK_IN
2ea20 2c 20 70 44 75 70 2c 20 30 2c 20 30 29 3b 0a 20  , pDup, 0, 0);. 
2ea21 20 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b       if( pNew ){
2ea22 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 64 78  .        int idx
2ea23 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 74 72 61  New;.        tra
2ea24 6e 73 66 65 72 4a 6f 69 6e 4d 61 72 6b 69 6e 67  nsferJoinMarking
2ea25 73 28 70 4e 65 77 2c 20 70 45 78 70 72 29 3b 0a  s(pNew, pExpr);.
2ea26 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2ea27 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
2ea28 28 70 4e 65 77 2c 20 45 50 5f 78 49 73 53 65 6c  (pNew, EP_xIsSel
2ea29 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 20 20  ect) );.        
2ea2a 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 3d 20  pNew->x.pList = 
2ea2b 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20 69  pList;.        i
2ea2c 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c 61  dxNew = whereCla
2ea2d 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70  useInsert(pWC, p
2ea2e 4e 65 77 2c 20 54 45 52 4d 5f 56 49 52 54 55 41  New, TERM_VIRTUA
2ea2f 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b  L|TERM_DYNAMIC);
2ea30 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
2ea31 65 28 20 69 64 78 4e 65 77 3d 3d 30 20 29 3b 0a  e( idxNew==0 );.
2ea32 20 20 20 20 20 20 20 20 65 78 70 72 41 6e 61 6c          exprAnal
2ea33 79 7a 65 28 70 53 72 63 2c 20 70 57 43 2c 20 69  yze(pSrc, pWC, i
2ea34 64 78 4e 65 77 29 3b 0a 20 20 20 20 20 20 20 20  dxNew);.        
2ea35 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b  pTerm = &pWC->a[
2ea36 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20  idxTerm];.      
2ea37 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d    pWC->a[idxNew]
2ea38 2e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65  .iParent = idxTe
2ea39 72 6d 3b 0a 20 20 20 20 20 20 20 20 70 54 65 72  rm;.        pTer
2ea3a 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b 0a 20  m->nChild = 1;. 
2ea3b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2ea3c 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c      sqlite3ExprL
2ea3d 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c  istDelete(db, pL
2ea3e 69 73 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ist);.      }.  
2ea3f 20 20 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72      pTerm->eOper
2ea40 61 74 6f 72 20 3d 20 30 3b 20 20 2f 2a 20 63 61  ator = 0;  /* ca
2ea41 73 65 20 31 20 74 72 75 6d 70 73 20 63 61 73 65  se 1 trumps case
2ea42 20 32 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a   2 */.    }.  }.
2ea43 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c  }.#endif /* !SQL
2ea44 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49  ITE_OMIT_OR_OPTI
2ea45 4d 49 5a 41 54 49 4f 4e 20 26 26 20 21 53 51 4c  MIZATION && !SQL
2ea46 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
2ea47 59 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  Y */.../*.** The
2ea48 20 69 6e 70 75 74 20 74 6f 20 74 68 69 73 20 72   input to this r
2ea49 6f 75 74 69 6e 65 20 69 73 20 61 6e 20 57 68 65  outine is an Whe
2ea4a 72 65 54 65 72 6d 20 73 74 72 75 63 74 75 72 65  reTerm structure
2ea4b 20 77 69 74 68 20 6f 6e 6c 79 20 74 68 65 0a 2a   with only the.*
2ea4c 2a 20 22 70 45 78 70 72 22 20 66 69 65 6c 64 20  * "pExpr" field 
2ea4d 66 69 6c 6c 65 64 20 69 6e 2e 20 20 54 68 65 20  filled in.  The 
2ea4e 6a 6f 62 20 6f 66 20 74 68 69 73 20 72 6f 75 74  job of this rout
2ea4f 69 6e 65 20 69 73 20 74 6f 20 61 6e 61 6c 79 7a  ine is to analyz
2ea50 65 20 74 68 65 0a 2a 2a 20 73 75 62 65 78 70 72  e the.** subexpr
2ea51 65 73 73 69 6f 6e 20 61 6e 64 20 70 6f 70 75 6c  ession and popul
2ea52 61 74 65 20 61 6c 6c 20 74 68 65 20 6f 74 68 65  ate all the othe
2ea53 72 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20  r fields of the 
2ea54 57 68 65 72 65 54 65 72 6d 0a 2a 2a 20 73 74 72  WhereTerm.** str
2ea55 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  ucture..**.** If
2ea56 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
2ea57 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  is of the form "
2ea58 3c 65 78 70 72 3e 20 3c 6f 70 3e 20 58 22 20 69  <expr> <op> X" i
2ea59 74 20 67 65 74 73 20 63 6f 6d 6d 75 74 65 64 0a  t gets commuted.
2ea5a 2a 2a 20 74 6f 20 74 68 65 20 73 74 61 6e 64 61  ** to the standa
2ea5b 72 64 20 66 6f 72 6d 20 6f 66 20 22 58 20 3c 6f  rd form of "X <o
2ea5c 70 3e 20 3c 65 78 70 72 3e 22 2e 0a 2a 2a 0a 2a  p> <expr>"..**.*
2ea5d 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73  * If the express
2ea5e 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f  ion is of the fo
2ea5f 72 6d 20 22 58 20 3c 6f 70 3e 20 59 22 20 77 68  rm "X <op> Y" wh
2ea60 65 72 65 20 62 6f 74 68 20 58 20 61 6e 64 20 59  ere both X and Y
2ea61 20 61 72 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 73 2c   are.** columns,
2ea62 20 74 68 65 6e 20 74 68 65 20 6f 72 69 67 69 6e   then the origin
2ea63 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  al expression is
2ea64 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 61   unchanged and a
2ea65 20 6e 65 77 20 76 69 72 74 75 61 6c 0a 2a 2a 20   new virtual.** 
2ea66 74 65 72 6d 20 6f 66 20 74 68 65 20 66 6f 72 6d  term of the form
2ea67 20 22 59 20 3c 6f 70 3e 20 58 22 20 69 73 20 61   "Y <op> X" is a
2ea68 64 64 65 64 20 74 6f 20 74 68 65 20 57 48 45 52  dded to the WHER
2ea69 45 20 63 6c 61 75 73 65 20 61 6e 64 0a 2a 2a 20  E clause and.** 
2ea6a 61 6e 61 6c 79 7a 65 64 20 73 65 70 61 72 61 74  analyzed separat
2ea6b 65 6c 79 2e 20 20 54 68 65 20 6f 72 69 67 69 6e  ely.  The origin
2ea6c 61 6c 20 74 65 72 6d 20 69 73 20 6d 61 72 6b 65  al term is marke
2ea6d 64 20 77 69 74 68 20 54 45 52 4d 5f 43 4f 50 49  d with TERM_COPI
2ea6e 45 44 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e 65  ED.** and the ne
2ea6f 77 20 74 65 72 6d 20 69 73 20 6d 61 72 6b 65 64  w term is marked
2ea70 20 77 69 74 68 20 54 45 52 4d 5f 44 59 4e 41 4d   with TERM_DYNAM
2ea71 49 43 20 28 62 65 63 61 75 73 65 20 69 74 27 73  IC (because it's
2ea72 20 70 45 78 70 72 0a 2a 2a 20 6e 65 65 64 73 20   pExpr.** needs 
2ea73 74 6f 20 62 65 20 66 72 65 65 64 20 77 69 74 68  to be freed with
2ea74 20 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65   the WhereClause
2ea75 29 20 61 6e 64 20 54 45 52 4d 5f 56 49 52 54 55  ) and TERM_VIRTU
2ea76 41 4c 20 28 62 65 63 61 75 73 65 20 69 74 0a 2a  AL (because it.*
2ea77 2a 20 69 73 20 61 20 63 6f 6d 6d 75 74 65 64 20  * is a commuted 
2ea78 63 6f 70 79 20 6f 66 20 61 20 70 72 69 6f 72 20  copy of a prior 
2ea79 74 65 72 6d 2e 29 20 20 54 68 65 20 6f 72 69 67  term.)  The orig
2ea7a 69 6e 61 6c 20 74 65 72 6d 20 68 61 73 20 6e 43  inal term has nC
2ea7b 68 69 6c 64 3d 31 0a 2a 2a 20 61 6e 64 20 74 68  hild=1.** and th
2ea7c 65 20 63 6f 70 79 20 68 61 73 20 69 64 78 50 61  e copy has idxPa
2ea7d 72 65 6e 74 20 73 65 74 20 74 6f 20 74 68 65 20  rent set to the 
2ea7e 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6f 72 69  index of the ori
2ea7f 67 69 6e 61 6c 20 74 65 72 6d 2e 0a 2a 2f 0a 73  ginal term..*/.s
2ea80 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 41  tatic void exprA
2ea81 6e 61 6c 79 7a 65 28 0a 20 20 53 72 63 4c 69 73  nalyze(.  SrcLis
2ea82 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20 20  t *pSrc,        
2ea83 20 20 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20      /* the FROM 
2ea84 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72  clause */.  Wher
2ea85 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20  eClause *pWC,   
2ea86 20 20 20 20 20 20 2f 2a 20 74 68 65 20 57 48 45        /* the WHE
2ea87 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  RE clause */.  i
2ea88 6e 74 20 69 64 78 54 65 72 6d 20 20 20 20 20 20  nt idxTerm      
2ea89 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
2ea8a 78 20 6f 66 20 74 68 65 20 74 65 72 6d 20 74 6f  x of the term to
2ea8b 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a   be analyzed */.
2ea8c 29 7b 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a  ){.  WhereTerm *
2ea8d 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20  pTerm;          
2ea8e 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 65 72        /* The ter
2ea8f 6d 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64  m to be analyzed
2ea90 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b 53   */.  WhereMaskS
2ea91 65 74 20 2a 70 4d 61 73 6b 53 65 74 3b 20 20 20  et *pMaskSet;   
2ea92 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 6f 66         /* Set of
2ea93 20 74 61 62 6c 65 20 69 6e 64 65 78 20 6d 61 73   table index mas
2ea94 6b 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  ks */.  Expr *pE
2ea95 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  xpr;            
2ea96 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2ea97 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65  expression to be
2ea98 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 42   analyzed */.  B
2ea99 69 74 6d 61 73 6b 20 70 72 65 72 65 71 4c 65 66  itmask prereqLef
2ea9a 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
2ea9b 2f 2a 20 50 72 65 72 65 71 75 65 73 69 74 65 73  /* Prerequesites
2ea9c 20 6f 66 20 74 68 65 20 70 45 78 70 72 2d 3e 70   of the pExpr->p
2ea9d 4c 65 66 74 20 2a 2f 0a 20 20 42 69 74 6d 61 73  Left */.  Bitmas
2ea9e 6b 20 70 72 65 72 65 71 41 6c 6c 3b 20 20 20 20  k prereqAll;    
2ea9f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72             /* Pr
2eaa0 65 72 65 71 75 65 73 69 74 65 73 20 6f 66 20 70  erequesites of p
2eaa1 45 78 70 72 20 2a 2f 0a 20 20 42 69 74 6d 61 73  Expr */.  Bitmas
2eaa2 6b 20 65 78 74 72 61 52 69 67 68 74 20 3d 20 30  k extraRight = 0
2eaa3 3b 0a 20 20 69 6e 74 20 6e 50 61 74 74 65 72 6e  ;.  int nPattern
2eaa4 3b 0a 20 20 69 6e 74 20 69 73 43 6f 6d 70 6c 65  ;.  int isComple
2eaa5 74 65 3b 0a 20 20 69 6e 74 20 6e 6f 43 61 73 65  te;.  int noCase
2eaa6 3b 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20  ;.  int op;     
2eaa7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eaa8 20 20 20 20 20 2f 2a 20 54 6f 70 2d 6c 65 76 65       /* Top-leve
2eaa9 6c 20 6f 70 65 72 61 74 6f 72 2e 20 20 70 45 78  l operator.  pEx
2eaaa 70 72 2d 3e 6f 70 20 2a 2f 0a 20 20 50 61 72 73  pr->op */.  Pars
2eaab 65 20 2a 70 50 61 72 73 65 20 3d 20 70 57 43 2d  e *pParse = pWC-
2eaac 3e 70 50 61 72 73 65 3b 20 20 20 20 20 2f 2a 20  >pParse;     /* 
2eaad 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
2eaae 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
2eaaf 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20   = pParse->db;  
2eab0 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
2eab1 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
2eab2 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
2eab3 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72  cFailed ){.    r
2eab4 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 54 65  eturn;.  }.  pTe
2eab5 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78  rm = &pWC->a[idx
2eab6 54 65 72 6d 5d 3b 0a 20 20 70 4d 61 73 6b 53 65  Term];.  pMaskSe
2eab7 74 20 3d 20 70 57 43 2d 3e 70 4d 61 73 6b 53 65  t = pWC->pMaskSe
2eab8 74 3b 0a 20 20 70 45 78 70 72 20 3d 20 70 54 65  t;.  pExpr = pTe
2eab9 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 70 72 65  rm->pExpr;.  pre
2eaba 72 65 71 4c 65 66 74 20 3d 20 65 78 70 72 54 61  reqLeft = exprTa
2eabb 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
2eabc 74 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29  t, pExpr->pLeft)
2eabd 3b 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e  ;.  op = pExpr->
2eabe 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b  op;.  if( op==TK
2eabf 5f 49 4e 20 29 7b 0a 20 20 20 20 61 73 73 65 72  _IN ){.    asser
2eac0 74 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  t( pExpr->pRight
2eac1 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 45  ==0 );.    if( E
2eac2 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
2eac3 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65  Expr, EP_xIsSele
2eac4 63 74 29 20 29 7b 0a 20 20 20 20 20 20 70 54 65  ct) ){.      pTe
2eac5 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20  rm->prereqRight 
2eac6 3d 20 65 78 70 72 53 65 6c 65 63 74 54 61 62 6c  = exprSelectTabl
2eac7 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c  eUsage(pMaskSet,
2eac8 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63   pExpr->x.pSelec
2eac9 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  t);.    }else{. 
2eaca 20 20 20 20 20 70 54 65 72 6d 2d 3e 70 72 65 72       pTerm->prer
2eacb 65 71 52 69 67 68 74 20 3d 20 65 78 70 72 4c 69  eqRight = exprLi
2eacc 73 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  stTableUsage(pMa
2eacd 73 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e 78 2e  skSet, pExpr->x.
2eace 70 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20  pList);.    }.  
2eacf 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b  }else if( op==TK
2ead0 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 70  _ISNULL ){.    p
2ead1 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68  Term->prereqRigh
2ead2 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  t = 0;.  }else{.
2ead3 20 20 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65      pTerm->prere
2ead4 71 52 69 67 68 74 20 3d 20 65 78 70 72 54 61 62  qRight = exprTab
2ead5 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
2ead6 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29  , pExpr->pRight)
2ead7 3b 0a 20 20 7d 0a 20 20 70 72 65 72 65 71 41 6c  ;.  }.  prereqAl
2ead8 6c 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61  l = exprTableUsa
2ead9 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78  ge(pMaskSet, pEx
2eada 70 72 29 3b 0a 20 20 69 66 28 20 45 78 70 72 48  pr);.  if( ExprH
2eadb 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
2eadc 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29  , EP_FromJoin) )
2eadd 7b 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 78 20  {.    Bitmask x 
2eade 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53  = getMask(pMaskS
2eadf 65 74 2c 20 70 45 78 70 72 2d 3e 69 52 69 67 68  et, pExpr->iRigh
2eae0 74 4a 6f 69 6e 54 61 62 6c 65 29 3b 0a 20 20 20  tJoinTable);.   
2eae1 20 70 72 65 72 65 71 41 6c 6c 20 7c 3d 20 78 3b   prereqAll |= x;
2eae2 0a 20 20 20 20 65 78 74 72 61 52 69 67 68 74 20  .    extraRight 
2eae3 3d 20 78 2d 31 3b 20 20 2f 2a 20 4f 4e 20 63 6c  = x-1;  /* ON cl
2eae4 61 75 73 65 20 74 65 72 6d 73 20 6d 61 79 20 6e  ause terms may n
2eae5 6f 74 20 62 65 20 75 73 65 64 20 77 69 74 68 20  ot be used with 
2eae6 61 6e 20 69 6e 64 65 78 0a 20 20 20 20 20 20 20  an index.       
2eae7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eae8 2a 2a 20 6f 6e 20 6c 65 66 74 20 74 61 62 6c 65  ** on left table
2eae9 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 2e   of a LEFT JOIN.
2eaea 20 20 54 69 63 6b 65 74 20 23 33 30 31 35 20 2a    Ticket #3015 *
2eaeb 2f 0a 20 20 7d 0a 20 20 70 54 65 72 6d 2d 3e 70  /.  }.  pTerm->p
2eaec 72 65 72 65 71 41 6c 6c 20 3d 20 70 72 65 72 65  rereqAll = prere
2eaed 71 41 6c 6c 3b 0a 20 20 70 54 65 72 6d 2d 3e 6c  qAll;.  pTerm->l
2eaee 65 66 74 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0a  eftCursor = -1;.
2eaef 20 20 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74    pTerm->iParent
2eaf0 20 3d 20 2d 31 3b 0a 20 20 70 54 65 72 6d 2d 3e   = -1;.  pTerm->
2eaf1 65 4f 70 65 72 61 74 6f 72 20 3d 20 30 3b 0a 20  eOperator = 0;. 
2eaf2 20 69 66 28 20 61 6c 6c 6f 77 65 64 4f 70 28 6f   if( allowedOp(o
2eaf3 70 29 20 26 26 20 28 70 54 65 72 6d 2d 3e 70 72  p) && (pTerm->pr
2eaf4 65 72 65 71 52 69 67 68 74 20 26 20 70 72 65 72  ereqRight & prer
2eaf5 65 71 4c 65 66 74 29 3d 3d 30 20 29 7b 0a 20 20  eqLeft)==0 ){.  
2eaf6 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20    Expr *pLeft = 
2eaf7 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20  pExpr->pLeft;.  
2eaf8 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d    Expr *pRight =
2eaf9 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a   pExpr->pRight;.
2eafa 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f      if( pLeft->o
2eafb 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a  p==TK_COLUMN ){.
2eafc 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6c 65 66        pTerm->lef
2eafd 74 43 75 72 73 6f 72 20 3d 20 70 4c 65 66 74 2d  tCursor = pLeft-
2eafe 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 70  >iTable;.      p
2eaff 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75  Term->u.leftColu
2eb00 6d 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43 6f 6c  mn = pLeft->iCol
2eb01 75 6d 6e 3b 0a 20 20 20 20 20 20 70 54 65 72 6d  umn;.      pTerm
2eb02 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 6f 70  ->eOperator = op
2eb03 65 72 61 74 6f 72 4d 61 73 6b 28 6f 70 29 3b 0a  eratorMask(op);.
2eb04 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 52      }.    if( pR
2eb05 69 67 68 74 20 26 26 20 70 52 69 67 68 74 2d 3e  ight && pRight->
2eb06 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b  op==TK_COLUMN ){
2eb07 0a 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d  .      WhereTerm
2eb08 20 2a 70 4e 65 77 3b 0a 20 20 20 20 20 20 45 78   *pNew;.      Ex
2eb09 70 72 20 2a 70 44 75 70 3b 0a 20 20 20 20 20 20  pr *pDup;.      
2eb0a 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43  if( pTerm->leftC
2eb0b 75 72 73 6f 72 3e 3d 30 20 29 7b 0a 20 20 20 20  ursor>=0 ){.    
2eb0c 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a      int idxNew;.
2eb0d 20 20 20 20 20 20 20 20 70 44 75 70 20 3d 20 73          pDup = s
2eb0e 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
2eb0f 2c 20 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 20  , pExpr, 0);.   
2eb10 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c       if( db->mal
2eb11 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
2eb12 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2eb13 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 44 75  prDelete(db, pDu
2eb14 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65  p);.          re
2eb15 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a  turn;.        }.
2eb16 20 20 20 20 20 20 20 20 69 64 78 4e 65 77 20 3d          idxNew =
2eb17 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65   whereClauseInse
2eb18 72 74 28 70 57 43 2c 20 70 44 75 70 2c 20 54 45  rt(pWC, pDup, TE
2eb19 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f  RM_VIRTUAL|TERM_
2eb1a 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20  DYNAMIC);.      
2eb1b 20 20 69 66 28 20 69 64 78 4e 65 77 3d 3d 30 20    if( idxNew==0 
2eb1c 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20  ) return;.      
2eb1d 20 20 70 4e 65 77 20 3d 20 26 70 57 43 2d 3e 61    pNew = &pWC->a
2eb1e 5b 69 64 78 4e 65 77 5d 3b 0a 20 20 20 20 20 20  [idxNew];.      
2eb1f 20 20 70 4e 65 77 2d 3e 69 50 61 72 65 6e 74 20    pNew->iParent 
2eb20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 20  = idxTerm;.     
2eb21 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d     pTerm = &pWC-
2eb22 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20  >a[idxTerm];.   
2eb23 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69       pTerm->nChi
2eb24 6c 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ld = 1;.        
2eb25 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c  pTerm->wtFlags |
2eb26 3d 20 54 45 52 4d 5f 43 4f 50 49 45 44 3b 0a 20  = TERM_COPIED;. 
2eb27 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2eb28 20 20 20 20 70 44 75 70 20 3d 20 70 45 78 70 72      pDup = pExpr
2eb29 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d  ;.        pNew =
2eb2a 20 70 54 65 72 6d 3b 0a 20 20 20 20 20 20 7d 0a   pTerm;.      }.
2eb2b 20 20 20 20 20 20 65 78 70 72 43 6f 6d 6d 75 74        exprCommut
2eb2c 65 28 70 50 61 72 73 65 2c 20 70 44 75 70 29 3b  e(pParse, pDup);
2eb2d 0a 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20 70  .      pLeft = p
2eb2e 44 75 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20  Dup->pLeft;.    
2eb2f 20 20 70 4e 65 77 2d 3e 6c 65 66 74 43 75 72 73    pNew->leftCurs
2eb30 6f 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62  or = pLeft->iTab
2eb31 6c 65 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e  le;.      pNew->
2eb32 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70  u.leftColumn = p
2eb33 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20  Left->iColumn;. 
2eb34 20 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 72 65       pNew->prere
2eb35 71 52 69 67 68 74 20 3d 20 70 72 65 72 65 71 4c  qRight = prereqL
2eb36 65 66 74 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d  eft;.      pNew-
2eb37 3e 70 72 65 72 65 71 41 6c 6c 20 3d 20 70 72 65  >prereqAll = pre
2eb38 72 65 71 41 6c 6c 3b 0a 20 20 20 20 20 20 70 4e  reqAll;.      pN
2eb39 65 77 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20  ew->eOperator = 
2eb3a 6f 70 65 72 61 74 6f 72 4d 61 73 6b 28 70 44 75  operatorMask(pDu
2eb3b 70 2d 3e 6f 70 29 3b 0a 20 20 20 20 7d 0a 20 20  p->op);.    }.  
2eb3c 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
2eb3d 45 5f 4f 4d 49 54 5f 42 45 54 57 45 45 4e 5f 4f  E_OMIT_BETWEEN_O
2eb3e 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a  PTIMIZATION.  /*
2eb3f 20 49 66 20 61 20 74 65 72 6d 20 69 73 20 74 68   If a term is th
2eb40 65 20 42 45 54 57 45 45 4e 20 6f 70 65 72 61 74  e BETWEEN operat
2eb41 6f 72 2c 20 63 72 65 61 74 65 20 74 77 6f 20 6e  or, create two n
2eb42 65 77 20 76 69 72 74 75 61 6c 20 74 65 72 6d 73  ew virtual terms
2eb43 0a 20 20 2a 2a 20 74 68 61 74 20 64 65 66 69 6e  .  ** that defin
2eb44 65 20 74 68 65 20 72 61 6e 67 65 20 74 68 61 74  e the range that
2eb45 20 74 68 65 20 42 45 54 57 45 45 4e 20 69 6d 70   the BETWEEN imp
2eb46 6c 65 6d 65 6e 74 73 2e 20 20 46 6f 72 20 65 78  lements.  For ex
2eb47 61 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  ample:.  **.  **
2eb48 20 20 20 20 20 20 61 20 42 45 54 57 45 45 4e 20        a BETWEEN 
2eb49 62 20 41 4e 44 20 63 0a 20 20 2a 2a 0a 20 20 2a  b AND c.  **.  *
2eb4a 2a 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20 69  * is converted i
2eb4b 6e 74 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  nto:.  **.  **  
2eb4c 20 20 20 20 28 61 20 42 45 54 57 45 45 4e 20 62      (a BETWEEN b
2eb4d 20 41 4e 44 20 63 29 20 41 4e 44 20 28 61 3e 3d   AND c) AND (a>=
2eb4e 62 29 20 41 4e 44 20 28 61 3c 3d 63 29 0a 20 20  b) AND (a<=c).  
2eb4f 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 74 77 6f 20  **.  ** The two 
2eb50 6e 65 77 20 74 65 72 6d 73 20 61 72 65 20 61 64  new terms are ad
2eb51 64 65 64 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64  ded onto the end
2eb52 20 6f 66 20 74 68 65 20 57 68 65 72 65 43 6c 61   of the WhereCla
2eb53 75 73 65 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a  use object..  **
2eb54 20 54 68 65 20 6e 65 77 20 74 65 72 6d 73 20 61   The new terms a
2eb55 72 65 20 22 64 79 6e 61 6d 69 63 22 20 61 6e 64  re "dynamic" and
2eb56 20 61 72 65 20 63 68 69 6c 64 72 65 6e 20 6f 66   are children of
2eb57 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 42 45   the original BE
2eb58 54 57 45 45 4e 0a 20 20 2a 2a 20 74 65 72 6d 2e  TWEEN.  ** term.
2eb59 20 20 54 68 61 74 20 6d 65 61 6e 73 20 74 68 61    That means tha
2eb5a 74 20 69 66 20 74 68 65 20 42 45 54 57 45 45 4e  t if the BETWEEN
2eb5b 20 74 65 72 6d 20 69 73 20 63 6f 64 65 64 2c 20   term is coded, 
2eb5c 74 68 65 20 63 68 69 6c 64 72 65 6e 20 61 72 65  the children are
2eb5d 0a 20 20 2a 2a 20 73 6b 69 70 70 65 64 2e 20 20  .  ** skipped.  
2eb5e 4f 72 2c 20 69 66 20 74 68 65 20 63 68 69 6c 64  Or, if the child
2eb5f 72 65 6e 20 61 72 65 20 73 61 74 69 73 66 69 65  ren are satisfie
2eb60 64 20 62 79 20 61 6e 20 69 6e 64 65 78 2c 20 74  d by an index, t
2eb61 68 65 20 6f 72 69 67 69 6e 61 6c 0a 20 20 2a 2a  he original.  **
2eb62 20 42 45 54 57 45 45 4e 20 74 65 72 6d 20 69 73   BETWEEN term is
2eb63 20 73 6b 69 70 70 65 64 2e 0a 20 20 2a 2f 0a 20   skipped..  */. 
2eb64 20 65 6c 73 65 20 69 66 28 20 70 45 78 70 72 2d   else if( pExpr-
2eb65 3e 6f 70 3d 3d 54 4b 5f 42 45 54 57 45 45 4e 20  >op==TK_BETWEEN 
2eb66 26 26 20 70 57 43 2d 3e 6f 70 3d 3d 54 4b 5f 41  && pWC->op==TK_A
2eb67 4e 44 20 29 7b 0a 20 20 20 20 45 78 70 72 4c 69  ND ){.    ExprLi
2eb68 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70  st *pList = pExp
2eb69 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20  r->x.pList;.    
2eb6a 69 6e 74 20 69 3b 0a 20 20 20 20 73 74 61 74 69  int i;.    stati
2eb6b 63 20 63 6f 6e 73 74 20 75 38 20 6f 70 73 5b 5d  c const u8 ops[]
2eb6c 20 3d 20 7b 54 4b 5f 47 45 2c 20 54 4b 5f 4c 45   = {TK_GE, TK_LE
2eb6d 7d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  };.    assert( p
2eb6e 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 61  List!=0 );.    a
2eb6f 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45  ssert( pList->nE
2eb70 78 70 72 3d 3d 32 20 29 3b 0a 20 20 20 20 66 6f  xpr==2 );.    fo
2eb71 72 28 69 3d 30 3b 20 69 3c 32 3b 20 69 2b 2b 29  r(i=0; i<2; i++)
2eb72 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e  {.      Expr *pN
2eb73 65 77 45 78 70 72 3b 0a 20 20 20 20 20 20 69 6e  ewExpr;.      in
2eb74 74 20 69 64 78 4e 65 77 3b 0a 20 20 20 20 20 20  t idxNew;.      
2eb75 70 4e 65 77 45 78 70 72 20 3d 20 73 71 6c 69 74  pNewExpr = sqlit
2eb76 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2eb77 6f 70 73 5b 69 5d 2c 20 0a 20 20 20 20 20 20 20  ops[i], .       
2eb78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eb79 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2eb7a 72 44 75 70 28 64 62 2c 20 70 45 78 70 72 2d 3e  rDup(db, pExpr->
2eb7b 70 4c 65 66 74 2c 20 30 29 2c 0a 20 20 20 20 20  pLeft, 0),.     
2eb7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eb7d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2eb7e 78 70 72 44 75 70 28 64 62 2c 20 70 4c 69 73 74  xprDup(db, pList
2eb7f 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 30 29  ->a[i].pExpr, 0)
2eb80 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 64 78 4e  , 0);.      idxN
2eb81 65 77 20 3d 20 77 68 65 72 65 43 6c 61 75 73 65  ew = whereClause
2eb82 49 6e 73 65 72 74 28 70 57 43 2c 20 70 4e 65 77  Insert(pWC, pNew
2eb83 45 78 70 72 2c 20 54 45 52 4d 5f 56 49 52 54 55  Expr, TERM_VIRTU
2eb84 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29  AL|TERM_DYNAMIC)
2eb85 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
2eb86 28 20 69 64 78 4e 65 77 3d 3d 30 20 29 3b 0a 20  ( idxNew==0 );. 
2eb87 20 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a 65       exprAnalyze
2eb88 28 70 53 72 63 2c 20 70 57 43 2c 20 69 64 78 4e  (pSrc, pWC, idxN
2eb89 65 77 29 3b 0a 20 20 20 20 20 20 70 54 65 72 6d  ew);.      pTerm
2eb8a 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65   = &pWC->a[idxTe
2eb8b 72 6d 5d 3b 0a 20 20 20 20 20 20 70 57 43 2d 3e  rm];.      pWC->
2eb8c 61 5b 69 64 78 4e 65 77 5d 2e 69 50 61 72 65 6e  a[idxNew].iParen
2eb8d 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20  t = idxTerm;.   
2eb8e 20 7d 0a 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43   }.    pTerm->nC
2eb8f 68 69 6c 64 20 3d 20 32 3b 0a 20 20 7d 0a 23 65  hild = 2;.  }.#e
2eb90 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2eb91 4d 49 54 5f 42 45 54 57 45 45 4e 5f 4f 50 54 49  MIT_BETWEEN_OPTI
2eb92 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66  MIZATION */..#if
2eb93 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2eb94 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a  _OMIT_OR_OPTIMIZ
2eb95 41 54 49 4f 4e 29 20 26 26 20 21 64 65 66 69 6e  ATION) && !defin
2eb96 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
2eb97 55 42 51 55 45 52 59 29 0a 20 20 2f 2a 20 41 6e  UBQUERY).  /* An
2eb98 61 6c 79 7a 65 20 61 20 74 65 72 6d 20 74 68 61  alyze a term tha
2eb99 74 20 69 73 20 63 6f 6d 70 6f 73 65 64 20 6f 66  t is composed of
2eb9a 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73 75 62   two or more sub
2eb9b 74 65 72 6d 73 20 63 6f 6e 6e 65 63 74 65 64 20  terms connected 
2eb9c 62 79 0a 20 20 2a 2a 20 61 6e 20 4f 52 20 6f 70  by.  ** an OR op
2eb9d 65 72 61 74 6f 72 2e 0a 20 20 2a 2f 0a 20 20 65  erator..  */.  e
2eb9e 6c 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e 6f  lse if( pExpr->o
2eb9f 70 3d 3d 54 4b 5f 4f 52 20 29 7b 0a 20 20 20 20  p==TK_OR ){.    
2eba0 61 73 73 65 72 74 28 20 70 57 43 2d 3e 6f 70 3d  assert( pWC->op=
2eba1 3d 54 4b 5f 41 4e 44 20 29 3b 0a 20 20 20 20 65  =TK_AND );.    e
2eba2 78 70 72 41 6e 61 6c 79 7a 65 4f 72 54 65 72 6d  xprAnalyzeOrTerm
2eba3 28 70 53 72 63 2c 20 70 57 43 2c 20 69 64 78 54  (pSrc, pWC, idxT
2eba4 65 72 6d 29 3b 0a 20 20 20 20 70 54 65 72 6d 20  erm);.    pTerm 
2eba5 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72  = &pWC->a[idxTer
2eba6 6d 5d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f  m];.  }.#endif /
2eba7 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52  * SQLITE_OMIT_OR
2eba8 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f  _OPTIMIZATION */
2eba9 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2ebaa 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d  _OMIT_LIKE_OPTIM
2ebab 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 41 64 64  IZATION.  /* Add
2ebac 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 74 6f 20   constraints to 
2ebad 72 65 64 75 63 65 20 74 68 65 20 73 65 61 72 63  reduce the searc
2ebae 68 20 73 70 61 63 65 20 6f 6e 20 61 20 4c 49 4b  h space on a LIK
2ebaf 45 20 6f 72 20 47 4c 4f 42 0a 20 20 2a 2a 20 6f  E or GLOB.  ** o
2ebb0 70 65 72 61 74 6f 72 2e 0a 20 20 2a 2a 0a 20 20  perator..  **.  
2ebb1 2a 2a 20 41 20 6c 69 6b 65 20 70 61 74 74 65 72  ** A like patter
2ebb2 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 78  n of the form "x
2ebb3 20 4c 49 4b 45 20 27 61 62 63 25 27 22 20 69 73   LIKE 'abc%'" is
2ebb4 20 63 68 61 6e 67 65 64 20 69 6e 74 6f 20 63 6f   changed into co
2ebb5 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a 0a 20  nstraints.  **. 
2ebb6 20 2a 2a 20 20 20 20 20 20 20 20 20 20 78 3e 3d   **          x>=
2ebb7 27 61 62 63 27 20 41 4e 44 20 78 3c 27 61 62 64  'abc' AND x<'abd
2ebb8 27 20 41 4e 44 20 78 20 4c 49 4b 45 20 27 61 62  ' AND x LIKE 'ab
2ebb9 63 25 27 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  c%'.  **.  ** Th
2ebba 65 20 6c 61 73 74 20 63 68 61 72 61 63 74 65 72  e last character
2ebbb 20 6f 66 20 74 68 65 20 70 72 65 66 69 78 20 22   of the prefix "
2ebbc 61 62 63 22 20 69 73 20 69 6e 63 72 65 6d 65 6e  abc" is incremen
2ebbd 74 65 64 20 74 6f 20 66 6f 72 6d 20 74 68 65 0a  ted to form the.
2ebbe 20 20 2a 2a 20 74 65 72 6d 69 6e 61 74 69 6f 6e    ** termination
2ebbf 20 63 6f 6e 64 69 74 69 6f 6e 20 22 61 62 64 22   condition "abd"
2ebc0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 4c  ..  */.  if( isL
2ebc1 69 6b 65 4f 72 47 6c 6f 62 28 70 50 61 72 73 65  ikeOrGlob(pParse
2ebc2 2c 20 70 45 78 70 72 2c 20 26 6e 50 61 74 74 65  , pExpr, &nPatte
2ebc3 72 6e 2c 20 26 69 73 43 6f 6d 70 6c 65 74 65 2c  rn, &isComplete,
2ebc4 20 26 6e 6f 43 61 73 65 29 0a 20 20 20 20 20 20   &noCase).      
2ebc5 20 20 20 26 26 20 70 57 43 2d 3e 6f 70 3d 3d 54     && pWC->op==T
2ebc6 4b 5f 41 4e 44 20 29 7b 0a 20 20 20 20 45 78 70  K_AND ){.    Exp
2ebc7 72 20 2a 70 4c 65 66 74 2c 20 2a 70 52 69 67 68  r *pLeft, *pRigh
2ebc8 74 3b 0a 20 20 20 20 45 78 70 72 20 2a 70 53 74  t;.    Expr *pSt
2ebc9 72 31 2c 20 2a 70 53 74 72 32 3b 0a 20 20 20 20  r1, *pStr2;.    
2ebca 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72 31 2c  Expr *pNewExpr1,
2ebcb 20 2a 70 4e 65 77 45 78 70 72 32 3b 0a 20 20 20   *pNewExpr2;.   
2ebcc 20 69 6e 74 20 69 64 78 4e 65 77 31 2c 20 69 64   int idxNew1, id
2ebcd 78 4e 65 77 32 3b 0a 0a 20 20 20 20 70 4c 65 66  xNew2;..    pLef
2ebce 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  t = pExpr->x.pLi
2ebcf 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a  st->a[1].pExpr;.
2ebd0 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 45 78      pRight = pEx
2ebd1 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 30  pr->x.pList->a[0
2ebd2 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 70 53 74  ].pExpr;.    pSt
2ebd3 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r1 = sqlite3Expr
2ebd4 28 64 62 2c 20 54 4b 5f 53 54 52 49 4e 47 2c 20  (db, TK_STRING, 
2ebd5 70 52 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e  pRight->u.zToken
2ebd6 29 3b 0a 20 20 20 20 69 66 28 20 70 53 74 72 31  );.    if( pStr1
2ebd7 20 29 20 70 53 74 72 31 2d 3e 75 2e 7a 54 6f 6b   ) pStr1->u.zTok
2ebd8 65 6e 5b 6e 50 61 74 74 65 72 6e 5d 20 3d 20 30  en[nPattern] = 0
2ebd9 3b 0a 20 20 20 20 70 53 74 72 32 20 3d 20 73 71  ;.    pStr2 = sq
2ebda 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
2ebdb 20 70 53 74 72 31 2c 20 30 29 3b 0a 20 20 20 20   pStr1, 0);.    
2ebdc 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  if( !db->mallocF
2ebdd 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 75  ailed ){.      u
2ebde 38 20 63 2c 20 2a 70 43 3b 20 20 20 20 20 20 20  8 c, *pC;       
2ebdf 2f 2a 20 4c 61 73 74 20 63 68 61 72 61 63 74 65  /* Last characte
2ebe0 72 20 62 65 66 6f 72 65 20 74 68 65 20 66 69 72  r before the fir
2ebe1 73 74 20 77 69 6c 64 63 61 72 64 20 2a 2f 0a 20  st wildcard */. 
2ebe2 20 20 20 20 20 70 43 20 3d 20 28 75 38 2a 29 26       pC = (u8*)&
2ebe3 70 53 74 72 32 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b  pStr2->u.zToken[
2ebe4 6e 50 61 74 74 65 72 6e 2d 31 5d 3b 0a 20 20 20  nPattern-1];.   
2ebe5 20 20 20 63 20 3d 20 2a 70 43 3b 0a 20 20 20 20     c = *pC;.    
2ebe6 20 20 69 66 28 20 6e 6f 43 61 73 65 20 29 7b 0a    if( noCase ){.
2ebe7 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2ebe8 6f 69 6e 74 20 69 73 20 74 6f 20 69 6e 63 72 65  oint is to incre
2ebe9 6d 65 6e 74 20 74 68 65 20 6c 61 73 74 20 63 68  ment the last ch
2ebea 61 72 61 63 74 65 72 20 62 65 66 6f 72 65 20 74  aracter before t
2ebeb 68 65 20 66 69 72 73 74 0a 20 20 20 20 20 20 20  he first.       
2ebec 20 2a 2a 20 77 69 6c 64 63 61 72 64 2e 20 20 42   ** wildcard.  B
2ebed 75 74 20 69 66 20 77 65 20 69 6e 63 72 65 6d 65  ut if we increme
2ebee 6e 74 20 27 40 27 2c 20 74 68 61 74 20 77 69 6c  nt '@', that wil
2ebef 6c 20 70 75 73 68 20 69 74 20 69 6e 74 6f 20 74  l push it into t
2ebf0 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c  he.        ** al
2ebf1 70 68 61 62 65 74 69 63 20 72 61 6e 67 65 20 77  phabetic range w
2ebf2 68 65 72 65 20 63 61 73 65 20 63 6f 6e 76 65 72  here case conver
2ebf3 73 69 6f 6e 73 20 77 69 6c 6c 20 6d 65 73 73 20  sions will mess 
2ebf4 75 70 20 74 68 65 20 0a 20 20 20 20 20 20 20 20  up the .        
2ebf5 2a 2a 20 69 6e 65 71 75 61 6c 69 74 79 2e 20 20  ** inequality.  
2ebf6 54 6f 20 61 76 6f 69 64 20 74 68 69 73 2c 20 6d  To avoid this, m
2ebf7 61 6b 65 20 73 75 72 65 20 74 6f 20 61 6c 73 6f  ake sure to also
2ebf8 20 72 75 6e 20 74 68 65 20 66 75 6c 6c 0a 20 20   run the full.  
2ebf9 20 20 20 20 20 20 2a 2a 20 4c 49 4b 45 20 6f 6e        ** LIKE on
2ebfa 20 61 6c 6c 20 63 61 6e 64 69 64 61 74 65 20 65   all candidate e
2ebfb 78 70 72 65 73 73 69 6f 6e 73 20 62 79 20 63 6c  xpressions by cl
2ebfc 65 61 72 69 6e 67 20 74 68 65 20 69 73 43 6f 6d  earing the isCom
2ebfd 70 6c 65 74 65 20 66 6c 61 67 0a 20 20 20 20 20  plete flag.     
2ebfe 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66     */.        if
2ebff 28 20 63 3d 3d 27 41 27 2d 31 20 29 20 69 73 43  ( c=='A'-1 ) isC
2ec00 6f 6d 70 6c 65 74 65 20 3d 20 30 3b 0a 0a 20 20  omplete = 0;..  
2ec01 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65        c = sqlite
2ec02 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 63 5d  3UpperToLower[c]
2ec03 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2ec04 2a 70 43 20 3d 20 63 20 2b 20 31 3b 0a 20 20 20  *pC = c + 1;.   
2ec05 20 7d 0a 20 20 20 20 70 4e 65 77 45 78 70 72 31   }.    pNewExpr1
2ec06 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2ec07 70 50 61 72 73 65 2c 20 54 4b 5f 47 45 2c 20 73  pParse, TK_GE, s
2ec08 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
2ec09 2c 70 4c 65 66 74 2c 30 29 2c 70 53 74 72 31 2c  ,pLeft,0),pStr1,
2ec0a 30 29 3b 0a 20 20 20 20 69 64 78 4e 65 77 31 20  0);.    idxNew1 
2ec0b 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73  = whereClauseIns
2ec0c 65 72 74 28 70 57 43 2c 20 70 4e 65 77 45 78 70  ert(pWC, pNewExp
2ec0d 72 31 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  r1, TERM_VIRTUAL
2ec0e 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a  |TERM_DYNAMIC);.
2ec0f 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 64      testcase( id
2ec10 78 4e 65 77 31 3d 3d 30 20 29 3b 0a 20 20 20 20  xNew1==0 );.    
2ec11 65 78 70 72 41 6e 61 6c 79 7a 65 28 70 53 72 63  exprAnalyze(pSrc
2ec12 2c 20 70 57 43 2c 20 69 64 78 4e 65 77 31 29 3b  , pWC, idxNew1);
2ec13 0a 20 20 20 20 70 4e 65 77 45 78 70 72 32 20 3d  .    pNewExpr2 =
2ec14 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2ec15 61 72 73 65 2c 20 54 4b 5f 4c 54 2c 20 73 71 6c  arse, TK_LT, sql
2ec16 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 70  ite3ExprDup(db,p
2ec17 4c 65 66 74 2c 30 29 2c 70 53 74 72 32 2c 30 29  Left,0),pStr2,0)
2ec18 3b 0a 20 20 20 20 69 64 78 4e 65 77 32 20 3d 20  ;.    idxNew2 = 
2ec19 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72  whereClauseInser
2ec1a 74 28 70 57 43 2c 20 70 4e 65 77 45 78 70 72 32  t(pWC, pNewExpr2
2ec1b 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54  , TERM_VIRTUAL|T
2ec1c 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20  ERM_DYNAMIC);.  
2ec1d 20 20 74 65 73 74 63 61 73 65 28 20 69 64 78 4e    testcase( idxN
2ec1e 65 77 32 3d 3d 30 20 29 3b 0a 20 20 20 20 65 78  ew2==0 );.    ex
2ec1f 70 72 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20  prAnalyze(pSrc, 
2ec20 70 57 43 2c 20 69 64 78 4e 65 77 32 29 3b 0a 20  pWC, idxNew2);. 
2ec21 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d     pTerm = &pWC-
2ec22 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20  >a[idxTerm];.   
2ec23 20 69 66 28 20 69 73 43 6f 6d 70 6c 65 74 65 20   if( isComplete 
2ec24 29 7b 0a 20 20 20 20 20 20 70 57 43 2d 3e 61 5b  ){.      pWC->a[
2ec25 69 64 78 4e 65 77 31 5d 2e 69 50 61 72 65 6e 74  idxNew1].iParent
2ec26 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20   = idxTerm;.    
2ec27 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 32    pWC->a[idxNew2
2ec28 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54  ].iParent = idxT
2ec29 65 72 6d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d  erm;.      pTerm
2ec2a 2d 3e 6e 43 68 69 6c 64 20 3d 20 32 3b 0a 20 20  ->nChild = 2;.  
2ec2b 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f    }.  }.#endif /
2ec2c 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49  * SQLITE_OMIT_LI
2ec2d 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20  KE_OPTIMIZATION 
2ec2e 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
2ec2f 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2ec30 41 42 4c 45 0a 20 20 2f 2a 20 41 64 64 20 61 20  ABLE.  /* Add a 
2ec31 57 4f 5f 4d 41 54 43 48 20 61 75 78 69 6c 69 61  WO_MATCH auxilia
2ec32 72 79 20 74 65 72 6d 20 74 6f 20 74 68 65 20 63  ry term to the c
2ec33 6f 6e 73 74 72 61 69 6e 74 20 73 65 74 20 69 66  onstraint set if
2ec34 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 72 65 6e   the.  ** curren
2ec35 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  t expression is 
2ec36 6f 66 20 74 68 65 20 66 6f 72 6d 3a 20 20 63 6f  of the form:  co
2ec37 6c 75 6d 6e 20 4d 41 54 43 48 20 65 78 70 72 2e  lumn MATCH expr.
2ec38 0a 20 20 2a 2a 20 54 68 69 73 20 69 6e 66 6f 72  .  ** This infor
2ec39 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62  mation is used b
2ec3a 79 20 74 68 65 20 78 42 65 73 74 49 6e 64 65 78  y the xBestIndex
2ec3b 20 6d 65 74 68 6f 64 73 20 6f 66 0a 20 20 2a 2a   methods of.  **
2ec3c 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e   virtual tables.
2ec3d 20 20 54 68 65 20 6e 61 74 69 76 65 20 71 75 65    The native que
2ec3e 72 79 20 6f 70 74 69 6d 69 7a 65 72 20 64 6f 65  ry optimizer doe
2ec3f 73 20 6e 6f 74 20 61 74 74 65 6d 70 74 0a 20 20  s not attempt.  
2ec40 2a 2a 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e  ** to do anythin
2ec41 67 20 77 69 74 68 20 4d 41 54 43 48 20 66 75 6e  g with MATCH fun
2ec42 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69  ctions..  */.  i
2ec43 66 28 20 69 73 4d 61 74 63 68 4f 66 43 6f 6c 75  f( isMatchOfColu
2ec44 6d 6e 28 70 45 78 70 72 29 20 29 7b 0a 20 20 20  mn(pExpr) ){.   
2ec45 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a 20 20 20   int idxNew;.   
2ec46 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 2a   Expr *pRight, *
2ec47 70 4c 65 66 74 3b 0a 20 20 20 20 57 68 65 72 65  pLeft;.    Where
2ec48 54 65 72 6d 20 2a 70 4e 65 77 54 65 72 6d 3b 0a  Term *pNewTerm;.
2ec49 20 20 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72      Bitmask prer
2ec4a 65 71 43 6f 6c 75 6d 6e 2c 20 70 72 65 72 65 71  eqColumn, prereq
2ec4b 45 78 70 72 3b 0a 0a 20 20 20 20 70 52 69 67 68  Expr;..    pRigh
2ec4c 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  t = pExpr->x.pLi
2ec4d 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a  st->a[0].pExpr;.
2ec4e 20 20 20 20 70 4c 65 66 74 20 3d 20 70 45 78 70      pLeft = pExp
2ec4f 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d  r->x.pList->a[1]
2ec50 2e 70 45 78 70 72 3b 0a 20 20 20 20 70 72 65 72  .pExpr;.    prer
2ec51 65 71 45 78 70 72 20 3d 20 65 78 70 72 54 61 62  eqExpr = exprTab
2ec52 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
2ec53 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20 20 70  , pRight);.    p
2ec54 72 65 72 65 71 43 6f 6c 75 6d 6e 20 3d 20 65 78  rereqColumn = ex
2ec55 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  prTableUsage(pMa
2ec56 73 6b 53 65 74 2c 20 70 4c 65 66 74 29 3b 0a 20  skSet, pLeft);. 
2ec57 20 20 20 69 66 28 20 28 70 72 65 72 65 71 45 78     if( (prereqEx
2ec58 70 72 20 26 20 70 72 65 72 65 71 43 6f 6c 75 6d  pr & prereqColum
2ec59 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 45  n)==0 ){.      E
2ec5a 78 70 72 20 2a 70 4e 65 77 45 78 70 72 3b 0a 20  xpr *pNewExpr;. 
2ec5b 20 20 20 20 20 70 4e 65 77 45 78 70 72 20 3d 20       pNewExpr = 
2ec5c 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2ec5d 72 73 65 2c 20 54 4b 5f 4d 41 54 43 48 2c 20 0a  rse, TK_MATCH, .
2ec5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c                0,
2ec60 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
2ec61 64 62 2c 20 70 52 69 67 68 74 2c 20 30 29 2c 20  db, pRight, 0), 
2ec62 30 29 3b 0a 20 20 20 20 20 20 69 64 78 4e 65 77  0);.      idxNew
2ec63 20 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e   = whereClauseIn
2ec64 73 65 72 74 28 70 57 43 2c 20 70 4e 65 77 45 78  sert(pWC, pNewEx
2ec65 70 72 2c 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  pr, TERM_VIRTUAL
2ec66 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a  |TERM_DYNAMIC);.
2ec67 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2ec68 69 64 78 4e 65 77 3d 3d 30 20 29 3b 0a 20 20 20  idxNew==0 );.   
2ec69 20 20 20 70 4e 65 77 54 65 72 6d 20 3d 20 26 70     pNewTerm = &p
2ec6a 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d 3b 0a 20  WC->a[idxNew];. 
2ec6b 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 70       pNewTerm->p
2ec6c 72 65 72 65 71 52 69 67 68 74 20 3d 20 70 72 65  rereqRight = pre
2ec6d 72 65 71 45 78 70 72 3b 0a 20 20 20 20 20 20 70  reqExpr;.      p
2ec6e 4e 65 77 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  NewTerm->leftCur
2ec6f 73 6f 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61  sor = pLeft->iTa
2ec70 62 6c 65 3b 0a 20 20 20 20 20 20 70 4e 65 77 54  ble;.      pNewT
2ec71 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d  erm->u.leftColum
2ec72 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43 6f 6c 75  n = pLeft->iColu
2ec73 6d 6e 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65  mn;.      pNewTe
2ec74 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20  rm->eOperator = 
2ec75 57 4f 5f 4d 41 54 43 48 3b 0a 20 20 20 20 20 20  WO_MATCH;.      
2ec76 70 4e 65 77 54 65 72 6d 2d 3e 69 50 61 72 65 6e  pNewTerm->iParen
2ec77 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20  t = idxTerm;.   
2ec78 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d     pTerm = &pWC-
2ec79 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20  >a[idxTerm];.   
2ec7a 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64     pTerm->nChild
2ec7b 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 54 65 72   = 1;.      pTer
2ec7c 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45  m->wtFlags |= TE
2ec7d 52 4d 5f 43 4f 50 49 45 44 3b 0a 20 20 20 20 20  RM_COPIED;.     
2ec7e 20 70 4e 65 77 54 65 72 6d 2d 3e 70 72 65 72 65   pNewTerm->prere
2ec7f 71 41 6c 6c 20 3d 20 70 54 65 72 6d 2d 3e 70 72  qAll = pTerm->pr
2ec80 65 72 65 71 41 6c 6c 3b 0a 20 20 20 20 7d 0a 20  ereqAll;.    }. 
2ec81 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c   }.#endif /* SQL
2ec82 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2ec83 54 41 42 4c 45 20 2a 2f 0a 0a 20 20 2f 2a 20 50  TABLE */..  /* P
2ec84 72 65 76 65 6e 74 20 4f 4e 20 63 6c 61 75 73 65  revent ON clause
2ec85 20 74 65 72 6d 73 20 6f 66 20 61 20 4c 45 46 54   terms of a LEFT
2ec86 20 4a 4f 49 4e 20 66 72 6f 6d 20 62 65 69 6e 67   JOIN from being
2ec87 20 75 73 65 64 20 74 6f 20 64 72 69 76 65 0a 20   used to drive. 
2ec88 20 2a 2a 20 61 6e 20 69 6e 64 65 78 20 66 6f 72   ** an index for
2ec89 20 74 61 62 6c 65 73 20 74 6f 20 74 68 65 20 6c   tables to the l
2ec8a 65 66 74 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e  eft of the join.
2ec8b 0a 20 20 2a 2f 0a 20 20 70 54 65 72 6d 2d 3e 70  .  */.  pTerm->p
2ec8c 72 65 72 65 71 52 69 67 68 74 20 7c 3d 20 65 78  rereqRight |= ex
2ec8d 74 72 61 52 69 67 68 74 3b 0a 7d 0a 0a 2f 2a 0a  traRight;.}../*.
2ec8e 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
2ec8f 66 20 61 6e 79 20 6f 66 20 74 68 65 20 65 78 70  f any of the exp
2ec90 72 65 73 73 69 6f 6e 73 20 69 6e 20 70 4c 69 73  ressions in pLis
2ec91 74 2d 3e 61 5b 69 46 69 72 73 74 2e 2e 2e 5d 20  t->a[iFirst...] 
2ec92 63 6f 6e 74 61 69 6e 0a 2a 2a 20 61 20 72 65 66  contain.** a ref
2ec93 65 72 65 6e 63 65 20 74 6f 20 61 6e 79 20 74 61  erence to any ta
2ec94 62 6c 65 20 6f 74 68 65 72 20 74 68 61 6e 20 74  ble other than t
2ec95 68 65 20 69 42 61 73 65 20 74 61 62 6c 65 2e 0a  he iBase table..
2ec96 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
2ec97 66 65 72 65 6e 63 65 73 4f 74 68 65 72 54 61 62  ferencesOtherTab
2ec98 6c 65 73 28 0a 20 20 45 78 70 72 4c 69 73 74 20  les(.  ExprList 
2ec99 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 20 20 20  *pList,         
2ec9a 20 2f 2a 20 53 65 61 72 63 68 20 65 78 70 72 65   /* Search expre
2ec9b 73 73 69 6f 6e 73 20 69 6e 20 74 68 73 20 6c 69  ssions in ths li
2ec9c 73 74 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73  st */.  WhereMas
2ec9d 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20  kSet *pMaskSet, 
2ec9e 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f    /* Mapping fro
2ec9f 6d 20 74 61 62 6c 65 73 20 74 6f 20 62 69 74 6d  m tables to bitm
2eca0 61 70 73 20 2a 2f 0a 20 20 69 6e 74 20 69 46 69  aps */.  int iFi
2eca1 72 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  rst,            
2eca2 20 20 20 2f 2a 20 42 65 20 73 65 61 72 63 68 69     /* Be searchi
2eca3 6e 67 20 77 69 74 68 20 74 68 65 20 69 46 69 72  ng with the iFir
2eca4 73 74 2d 74 68 20 65 78 70 72 65 73 73 69 6f 6e  st-th expression
2eca5 20 2a 2f 0a 20 20 69 6e 74 20 69 42 61 73 65 20   */.  int iBase 
2eca6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eca7 2f 2a 20 49 67 6e 6f 72 65 20 72 65 66 65 72 65  /* Ignore refere
2eca8 6e 63 65 73 20 74 6f 20 74 68 69 73 20 74 61 62  nces to this tab
2eca9 6c 65 20 2a 2f 0a 29 7b 0a 20 20 42 69 74 6d 61  le */.){.  Bitma
2ecaa 73 6b 20 61 6c 6c 6f 77 65 64 20 3d 20 7e 67 65  sk allowed = ~ge
2ecab 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20  tMask(pMaskSet, 
2ecac 69 42 61 73 65 29 3b 0a 20 20 77 68 69 6c 65 28  iBase);.  while(
2ecad 20 69 46 69 72 73 74 3c 70 4c 69 73 74 2d 3e 6e   iFirst<pList->n
2ecae 45 78 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20  Expr ){.    if( 
2ecaf 28 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28  (exprTableUsage(
2ecb0 70 4d 61 73 6b 53 65 74 2c 20 70 4c 69 73 74 2d  pMaskSet, pList-
2ecb1 3e 61 5b 69 46 69 72 73 74 2b 2b 5d 2e 70 45 78  >a[iFirst++].pEx
2ecb2 70 72 29 26 61 6c 6c 6f 77 65 64 29 21 3d 30 20  pr)&allowed)!=0 
2ecb3 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
2ecb4 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  1;.    }.  }.  r
2ecb5 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a  eturn 0;.}.../*.
2ecb6 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2ecb7 64 65 63 69 64 65 73 20 69 66 20 70 49 64 78 20  decides if pIdx 
2ecb8 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73  can be used to s
2ecb9 61 74 69 73 66 79 20 74 68 65 20 4f 52 44 45 52  atisfy the ORDER
2ecba 20 42 59 0a 2a 2a 20 63 6c 61 75 73 65 2e 20 20   BY.** clause.  
2ecbb 49 66 20 69 74 20 63 61 6e 2c 20 69 74 20 72 65  If it can, it re
2ecbc 74 75 72 6e 73 20 31 2e 20 20 49 66 20 70 49 64  turns 1.  If pId
2ecbd 78 20 63 61 6e 6e 6f 74 20 73 61 74 69 73 66 79  x cannot satisfy
2ecbe 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59   the.** ORDER BY
2ecbf 20 63 6c 61 75 73 65 2c 20 74 68 69 73 20 72 6f   clause, this ro
2ecc0 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 30 2e  utine returns 0.
2ecc1 0a 2a 2a 0a 2a 2a 20 70 4f 72 64 65 72 42 79 20  .**.** pOrderBy 
2ecc2 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63  is an ORDER BY c
2ecc3 6c 61 75 73 65 20 66 72 6f 6d 20 61 20 53 45 4c  lause from a SEL
2ecc4 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ECT statement.  
2ecc5 70 54 61 62 20 69 73 20 74 68 65 0a 2a 2a 20 6c  pTab is the.** l
2ecc6 65 66 74 2d 6d 6f 73 74 20 74 61 62 6c 65 20 69  eft-most table i
2ecc7 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
2ecc8 65 20 6f 66 20 74 68 61 74 20 73 61 6d 65 20 53  e of that same S
2ecc9 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
2ecca 61 6e 64 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65  and.** the table
2eccb 20 68 61 73 20 61 20 63 75 72 73 6f 72 20 6e 75   has a cursor nu
2eccc 6d 62 65 72 20 6f 66 20 22 62 61 73 65 22 2e 20  mber of "base". 
2eccd 20 70 49 64 78 20 69 73 20 61 6e 20 69 6e 64 65   pIdx is an inde
2ecce 78 20 6f 6e 20 70 54 61 62 2e 0a 2a 2a 0a 2a 2a  x on pTab..**.**
2eccf 20 6e 45 71 43 6f 6c 20 69 73 20 74 68 65 20 6e   nEqCol is the n
2ecd0 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
2ecd1 20 6f 66 20 70 49 64 78 20 74 68 61 74 20 61 72   of pIdx that ar
2ecd2 65 20 75 73 65 64 20 61 73 20 65 71 75 61 6c 69  e used as equali
2ecd3 74 79 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74  ty.** constraint
2ecd4 73 2e 20 20 41 6e 79 20 6f 66 20 74 68 65 73 65  s.  Any of these
2ecd5 20 63 6f 6c 75 6d 6e 73 20 6d 61 79 20 62 65 20   columns may be 
2ecd6 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20 74 68 65  missing from the
2ecd7 20 4f 52 44 45 52 20 42 59 0a 2a 2a 20 63 6c 61   ORDER BY.** cla
2ecd8 75 73 65 20 61 6e 64 20 74 68 65 20 6d 61 74 63  use and the matc
2ecd9 68 20 63 61 6e 20 73 74 69 6c 6c 20 62 65 20 61  h can still be a
2ecda 20 73 75 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20   success..**.** 
2ecdb 41 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 65  All terms of the
2ecdc 20 4f 52 44 45 52 20 42 59 20 74 68 61 74 20 6d   ORDER BY that m
2ecdd 61 74 63 68 20 61 67 61 69 6e 73 74 20 74 68 65  atch against the
2ecde 20 69 6e 64 65 78 20 6d 75 73 74 20 62 65 20 65   index must be e
2ecdf 69 74 68 65 72 0a 2a 2a 20 41 53 43 20 6f 72 20  ither.** ASC or 
2ece0 44 45 53 43 2e 20 20 28 54 65 72 6d 73 20 6f 66  DESC.  (Terms of
2ece1 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
2ece2 61 75 73 65 20 70 61 73 74 20 74 68 65 20 65 6e  ause past the en
2ece3 64 20 6f 66 20 61 20 55 4e 49 51 55 45 0a 2a 2a  d of a UNIQUE.**
2ece4 20 69 6e 64 65 78 20 64 6f 20 6e 6f 74 20 6e 65   index do not ne
2ece5 65 64 20 74 6f 20 73 61 74 69 73 66 79 20 74 68  ed to satisfy th
2ece6 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 2e 29 20  is constraint.) 
2ece7 20 54 68 65 20 2a 70 62 52 65 76 20 76 61 6c 75   The *pbRev valu
2ece8 65 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 31  e is.** set to 1
2ece9 20 69 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   if the ORDER BY
2ecea 20 63 6c 61 75 73 65 20 69 73 20 61 6c 6c 20 44   clause is all D
2eceb 45 53 43 20 61 6e 64 20 69 74 20 69 73 20 73 65  ESC and it is se
2ecec 74 20 74 6f 20 30 20 69 66 0a 2a 2a 20 74 68 65  t to 0 if.** the
2eced 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2ecee 20 69 73 20 61 6c 6c 20 41 53 43 2e 0a 2a 2f 0a   is all ASC..*/.
2ecef 73 74 61 74 69 63 20 69 6e 74 20 69 73 53 6f 72  static int isSor
2ecf0 74 69 6e 67 49 6e 64 65 78 28 0a 20 20 50 61 72  tingIndex(.  Par
2ecf1 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2ecf2 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2ecf3 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65  context */.  Whe
2ecf4 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b  reMaskSet *pMask
2ecf5 53 65 74 2c 20 2f 2a 20 4d 61 70 70 69 6e 67 20  Set, /* Mapping 
2ecf6 66 72 6f 6d 20 74 61 62 6c 65 20 63 75 72 73 6f  from table curso
2ecf7 72 20 6e 75 6d 62 65 72 73 20 74 6f 20 62 69 74  r numbers to bit
2ecf8 6d 61 70 73 20 2a 2f 0a 20 20 49 6e 64 65 78 20  maps */.  Index 
2ecf9 2a 70 49 64 78 2c 20 20 20 20 20 20 20 20 20 20  *pIdx,          
2ecfa 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 77    /* The index w
2ecfb 65 20 61 72 65 20 74 65 73 74 69 6e 67 20 2a 2f  e are testing */
2ecfc 0a 20 20 69 6e 74 20 62 61 73 65 2c 20 20 20 20  .  int base,    
2ecfd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
2ecfe 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20  rsor number for 
2ecff 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20  the table to be 
2ed00 73 6f 72 74 65 64 20 2a 2f 0a 20 20 45 78 70 72  sorted */.  Expr
2ed01 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20  List *pOrderBy, 
2ed02 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52      /* The ORDER
2ed03 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
2ed04 69 6e 74 20 6e 45 71 43 6f 6c 2c 20 20 20 20 20  int nEqCol,     
2ed05 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2ed06 72 20 6f 66 20 69 6e 64 65 78 20 63 6f 6c 75 6d  r of index colum
2ed07 6e 73 20 77 69 74 68 20 3d 3d 20 63 6f 6e 73 74  ns with == const
2ed08 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20  raints */.  int 
2ed09 2a 70 62 52 65 76 20 20 20 20 20 20 20 20 20 20  *pbRev          
2ed0a 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 31 20      /* Set to 1 
2ed0b 69 66 20 4f 52 44 45 52 20 42 59 20 69 73 20 44  if ORDER BY is D
2ed0c 45 53 43 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ESC */.){.  int 
2ed0d 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20  i, j;           
2ed0e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2ed0f 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a  oop counters */.
2ed10 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 20    int sortOrder 
2ed11 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
2ed12 20 20 2f 2a 20 58 4f 52 20 6f 66 20 69 6e 64 65    /* XOR of inde
2ed13 78 20 61 6e 64 20 4f 52 44 45 52 20 42 59 20 73  x and ORDER BY s
2ed14 6f 72 74 20 64 69 72 65 63 74 69 6f 6e 20 2a 2f  ort direction */
2ed15 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20  .  int nTerm;   
2ed16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ed17 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2ed18 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 2a  ORDER BY terms *
2ed19 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  /.  struct ExprL
2ed1a 69 73 74 5f 69 74 65 6d 20 2a 70 54 65 72 6d 3b  ist_item *pTerm;
2ed1b 20 20 20 20 2f 2a 20 41 20 74 65 72 6d 20 6f 66      /* A term of
2ed1c 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
2ed1d 61 75 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ause */.  sqlite
2ed1e 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2ed1f 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  db;..  assert( p
2ed20 4f 72 64 65 72 42 79 21 3d 30 20 29 3b 0a 20 20  OrderBy!=0 );.  
2ed21 6e 54 65 72 6d 20 3d 20 70 4f 72 64 65 72 42 79  nTerm = pOrderBy
2ed22 2d 3e 6e 45 78 70 72 3b 0a 20 20 61 73 73 65 72  ->nExpr;.  asser
2ed23 74 28 20 6e 54 65 72 6d 3e 30 20 29 3b 0a 0a 20  t( nTerm>0 );.. 
2ed24 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 70 49 64   /* Argument pId
2ed25 78 20 6d 75 73 74 20 65 69 74 68 65 72 20 70 6f  x must either po
2ed26 69 6e 74 20 74 6f 20 61 20 27 72 65 61 6c 27 20  int to a 'real' 
2ed27 6e 61 6d 65 64 20 69 6e 64 65 78 20 73 74 72 75  named index stru
2ed28 63 74 75 72 65 2c 20 0a 20 20 2a 2a 20 6f 72 20  cture, .  ** or 
2ed29 61 6e 20 69 6e 64 65 78 20 73 74 72 75 63 74 75  an index structu
2ed2a 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20  re allocated on 
2ed2b 74 68 65 20 73 74 61 63 6b 20 62 79 20 62 65 73  the stack by bes
2ed2c 74 42 74 72 65 65 49 6e 64 65 78 28 29 20 74 6f  tBtreeIndex() to
2ed2d 0a 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 20  .  ** represent 
2ed2e 74 68 65 20 72 6f 77 69 64 20 69 6e 64 65 78 20  the rowid index 
2ed2f 74 68 61 74 20 69 73 20 70 61 72 74 20 6f 66 20  that is part of 
2ed30 65 76 65 72 79 20 74 61 62 6c 65 2e 20 20 2a 2f  every table.  */
2ed31 0a 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d  .  assert( pIdx-
2ed32 3e 7a 4e 61 6d 65 20 7c 7c 20 28 70 49 64 78 2d  >zName || (pIdx-
2ed33 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 26 26 20 70  >nColumn==1 && p
2ed34 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d  Idx->aiColumn[0]
2ed35 3d 3d 2d 31 29 20 29 3b 0a 0a 20 20 2f 2a 20 4d  ==-1) );..  /* M
2ed36 61 74 63 68 20 74 65 72 6d 73 20 6f 66 20 74 68  atch terms of th
2ed37 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
2ed38 65 20 61 67 61 69 6e 73 74 20 63 6f 6c 75 6d 6e  e against column
2ed39 73 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 69 6e  s of.  ** the in
2ed3a 64 65 78 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e  dex..  **.  ** N
2ed3b 6f 74 65 20 74 68 61 74 20 69 6e 64 69 63 65 73  ote that indices
2ed3c 20 68 61 76 65 20 70 49 64 78 2d 3e 6e 43 6f 6c   have pIdx->nCol
2ed3d 75 6d 6e 20 72 65 67 75 6c 61 72 20 63 6f 6c 75  umn regular colu
2ed3e 6d 6e 73 20 70 6c 75 73 0a 20 20 2a 2a 20 6f 6e  mns plus.  ** on
2ed3f 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c  e additional col
2ed40 75 6d 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  umn containing t
2ed41 68 65 20 72 6f 77 69 64 2e 20 20 54 68 65 20 72  he rowid.  The r
2ed42 6f 77 69 64 20 63 6f 6c 75 6d 6e 0a 20 20 2a 2a  owid column.  **
2ed43 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 69 73   of the index is
2ed44 20 61 6c 73 6f 20 61 6c 6c 6f 77 65 64 20 74 6f   also allowed to
2ed45 20 6d 61 74 63 68 20 61 67 61 69 6e 73 74 20 74   match against t
2ed46 68 65 20 4f 52 44 45 52 20 42 59 0a 20 20 2a 2a  he ORDER BY.  **
2ed47 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20   clause..  */.  
2ed48 66 6f 72 28 69 3d 6a 3d 30 2c 20 70 54 65 72 6d  for(i=j=0, pTerm
2ed49 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 6a 3c  =pOrderBy->a; j<
2ed4a 6e 54 65 72 6d 20 26 26 20 69 3c 3d 70 49 64 78  nTerm && i<=pIdx
2ed4b 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  ->nColumn; i++){
2ed4c 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72  .    Expr *pExpr
2ed4d 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65  ;       /* The e
2ed4e 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65  xpression of the
2ed4f 20 4f 52 44 45 52 20 42 59 20 70 54 65 72 6d 20   ORDER BY pTerm 
2ed50 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  */.    CollSeq *
2ed51 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 54 68 65  pColl;    /* The
2ed52 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
2ed53 6e 63 65 20 6f 66 20 70 45 78 70 72 20 2a 2f 0a  nce of pExpr */.
2ed54 20 20 20 20 69 6e 74 20 74 65 72 6d 53 6f 72 74      int termSort
2ed55 4f 72 64 65 72 3b 20 2f 2a 20 53 6f 72 74 20 6f  Order; /* Sort o
2ed56 72 64 65 72 20 66 6f 72 20 74 68 69 73 20 74 65  rder for this te
2ed57 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43  rm */.    int iC
2ed58 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 2f 2a 20  olumn;       /* 
2ed59 54 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20  The i-th column 
2ed5a 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 2d  of the index.  -
2ed5b 31 20 66 6f 72 20 72 6f 77 69 64 20 2a 2f 0a 20  1 for rowid */. 
2ed5c 20 20 20 69 6e 74 20 69 53 6f 72 74 4f 72 64 65     int iSortOrde
2ed5d 72 3b 20 20 20 20 2f 2a 20 31 20 66 6f 72 20 44  r;    /* 1 for D
2ed5e 45 53 43 2c 20 30 20 66 6f 72 20 41 53 43 20 6f  ESC, 0 for ASC o
2ed5f 6e 20 74 68 65 20 69 2d 74 68 20 69 6e 64 65 78  n the i-th index
2ed60 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 63 6f 6e   term */.    con
2ed61 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20  st char *zColl; 
2ed62 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63  /* Name of the c
2ed63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
2ed64 65 20 66 6f 72 20 69 2d 74 68 20 69 6e 64 65 78  e for i-th index
2ed65 20 74 65 72 6d 20 2a 2f 0a 0a 20 20 20 20 70 45   term */..    pE
2ed66 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78  xpr = pTerm->pEx
2ed67 70 72 3b 0a 20 20 20 20 69 66 28 20 70 45 78 70  pr;.    if( pExp
2ed68 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e  r->op!=TK_COLUMN
2ed69 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c   || pExpr->iTabl
2ed6a 65 21 3d 62 61 73 65 20 29 7b 0a 20 20 20 20 20  e!=base ){.     
2ed6b 20 2f 2a 20 43 61 6e 20 6e 6f 74 20 75 73 65 20   /* Can not use 
2ed6c 61 6e 20 69 6e 64 65 78 20 73 6f 72 74 20 6f 6e  an index sort on
2ed6d 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20 69   anything that i
2ed6e 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 69  s not a column i
2ed6f 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6c  n the.      ** l
2ed70 65 66 74 2d 6d 6f 73 74 20 74 61 62 6c 65 20 6f  eft-most table o
2ed71 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  f the FROM claus
2ed72 65 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b  e */.      break
2ed73 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 6f 6c  ;.    }.    pCol
2ed74 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  l = sqlite3ExprC
2ed75 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
2ed76 45 78 70 72 29 3b 0a 20 20 20 20 69 66 28 20 21  Expr);.    if( !
2ed77 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70  pColl ){.      p
2ed78 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74  Coll = db->pDflt
2ed79 43 6f 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Coll;.    }.    
2ed7a 69 66 28 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 20  if( pIdx->zName 
2ed7b 26 26 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75  && i<pIdx->nColu
2ed7c 6d 6e 20 29 7b 0a 20 20 20 20 20 20 69 43 6f 6c  mn ){.      iCol
2ed7d 75 6d 6e 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f  umn = pIdx->aiCo
2ed7e 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 69  lumn[i];.      i
2ed7f 66 28 20 69 43 6f 6c 75 6d 6e 3d 3d 70 49 64 78  f( iColumn==pIdx
2ed80 2d 3e 70 54 61 62 6c 65 2d 3e 69 50 4b 65 79 20  ->pTable->iPKey 
2ed81 29 7b 0a 20 20 20 20 20 20 20 20 69 43 6f 6c 75  ){.        iColu
2ed82 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d  mn = -1;.      }
2ed83 0a 20 20 20 20 20 20 69 53 6f 72 74 4f 72 64 65  .      iSortOrde
2ed84 72 20 3d 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f  r = pIdx->aSortO
2ed85 72 64 65 72 5b 69 5d 3b 0a 20 20 20 20 20 20 7a  rder[i];.      z
2ed86 43 6f 6c 6c 20 3d 20 70 49 64 78 2d 3e 61 7a 43  Coll = pIdx->azC
2ed87 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73  oll[i];.    }els
2ed88 65 7b 0a 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e  e{.      iColumn
2ed89 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 69 53 6f   = -1;.      iSo
2ed8a 72 74 4f 72 64 65 72 20 3d 20 30 3b 0a 20 20 20  rtOrder = 0;.   
2ed8b 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c     zColl = pColl
2ed8c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 7d 0a 20  ->zName;.    }. 
2ed8d 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43     if( pExpr->iC
2ed8e 6f 6c 75 6d 6e 21 3d 69 43 6f 6c 75 6d 6e 20 7c  olumn!=iColumn |
2ed8f 7c 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  | sqlite3StrICmp
2ed90 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a  (pColl->zName, z
2ed91 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 2f  Coll) ){.      /
2ed92 2a 20 54 65 72 6d 20 6a 20 6f 66 20 74 68 65 20  * Term j of the 
2ed93 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2ed94 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 63  does not match c
2ed95 6f 6c 75 6d 6e 20 69 20 6f 66 20 74 68 65 20 69  olumn i of the i
2ed96 6e 64 65 78 20 2a 2f 0a 20 20 20 20 20 20 69 66  ndex */.      if
2ed97 28 20 69 3c 6e 45 71 43 6f 6c 20 29 7b 0a 20 20  ( i<nEqCol ){.  
2ed98 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 69        /* If an i
2ed99 6e 64 65 78 20 63 6f 6c 75 6d 6e 20 74 68 61 74  ndex column that
2ed9a 20 69 73 20 63 6f 6e 73 74 72 61 69 6e 65 64 20   is constrained 
2ed9b 62 79 20 3d 3d 20 66 61 69 6c 73 20 74 6f 20 6d  by == fails to m
2ed9c 61 74 63 68 20 61 6e 0a 20 20 20 20 20 20 20 20  atch an.        
2ed9d 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  ** ORDER BY term
2ed9e 2c 20 74 68 61 74 20 69 73 20 4f 4b 2e 20 20 4a  , that is OK.  J
2ed9f 75 73 74 20 69 67 6e 6f 72 65 20 74 68 61 74 20  ust ignore that 
2eda0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e  column of the in
2eda1 64 65 78 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  dex.        */. 
2eda2 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
2eda3 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
2eda4 20 69 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d   i==pIdx->nColum
2eda5 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  n ){.        /* 
2eda6 49 6e 64 65 78 20 63 6f 6c 75 6d 6e 20 69 20 69  Index column i i
2eda7 73 20 74 68 65 20 72 6f 77 69 64 2e 20 20 41 6c  s the rowid.  Al
2eda8 6c 20 6f 74 68 65 72 20 74 65 72 6d 73 20 6d 61  l other terms ma
2eda9 74 63 68 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  tch. */.        
2edaa 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c  break;.      }el
2edab 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49  se{.        /* I
2edac 66 20 61 6e 20 69 6e 64 65 78 20 63 6f 6c 75 6d  f an index colum
2edad 6e 20 66 61 69 6c 73 20 74 6f 20 6d 61 74 63 68  n fails to match
2edae 20 61 6e 64 20 69 73 20 6e 6f 74 20 63 6f 6e 73   and is not cons
2edaf 74 72 61 69 6e 65 64 20 62 79 20 3d 3d 0a 20 20  trained by ==.  
2edb0 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68        ** then th
2edb1 65 20 69 6e 64 65 78 20 63 61 6e 6e 6f 74 20 73  e index cannot s
2edb2 61 74 69 73 66 79 20 74 68 65 20 4f 52 44 45 52  atisfy the ORDER
2edb3 20 42 59 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a   BY constraint..
2edb4 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
2edb5 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
2edb6 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61     }.    }.    a
2edb7 73 73 65 72 74 28 20 70 49 64 78 2d 3e 61 53 6f  ssert( pIdx->aSo
2edb8 72 74 4f 72 64 65 72 21 3d 30 20 7c 7c 20 69 43  rtOrder!=0 || iC
2edb9 6f 6c 75 6d 6e 3d 3d 2d 31 20 29 3b 0a 20 20 20  olumn==-1 );.   
2edba 20 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e   assert( pTerm->
2edbb 73 6f 72 74 4f 72 64 65 72 3d 3d 30 20 7c 7c 20  sortOrder==0 || 
2edbc 70 54 65 72 6d 2d 3e 73 6f 72 74 4f 72 64 65 72  pTerm->sortOrder
2edbd 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==1 );.    asser
2edbe 74 28 20 69 53 6f 72 74 4f 72 64 65 72 3d 3d 30  t( iSortOrder==0
2edbf 20 7c 7c 20 69 53 6f 72 74 4f 72 64 65 72 3d 3d   || iSortOrder==
2edc0 31 20 29 3b 0a 20 20 20 20 74 65 72 6d 53 6f 72  1 );.    termSor
2edc1 74 4f 72 64 65 72 20 3d 20 69 53 6f 72 74 4f 72  tOrder = iSortOr
2edc2 64 65 72 20 5e 20 70 54 65 72 6d 2d 3e 73 6f 72  der ^ pTerm->sor
2edc3 74 4f 72 64 65 72 3b 0a 20 20 20 20 69 66 28 20  tOrder;.    if( 
2edc4 69 3e 6e 45 71 43 6f 6c 20 29 7b 0a 20 20 20 20  i>nEqCol ){.    
2edc5 20 20 69 66 28 20 74 65 72 6d 53 6f 72 74 4f 72    if( termSortOr
2edc6 64 65 72 21 3d 73 6f 72 74 4f 72 64 65 72 20 29  der!=sortOrder )
2edc7 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  {.        /* Ind
2edc8 69 63 65 73 20 63 61 6e 20 6f 6e 6c 79 20 62 65  ices can only be
2edc9 20 75 73 65 64 20 69 66 20 61 6c 6c 20 4f 52 44   used if all ORD
2edca 45 52 20 42 59 20 74 65 72 6d 73 20 70 61 73 74  ER BY terms past
2edcb 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20   the.        ** 
2edcc 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
2edcd 69 6e 74 73 20 61 72 65 20 61 6c 6c 20 65 69 74  ints are all eit
2edce 68 65 72 20 44 45 53 43 20 6f 72 20 41 53 43 2e  her DESC or ASC.
2edcf 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75   */.        retu
2edd0 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
2edd1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
2edd2 6f 72 74 4f 72 64 65 72 20 3d 20 74 65 72 6d 53  ortOrder = termS
2edd3 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 7d 0a  ortOrder;.    }.
2edd4 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 70 54 65      j++;.    pTe
2edd5 72 6d 2b 2b 3b 0a 20 20 20 20 69 66 28 20 69 43  rm++;.    if( iC
2edd6 6f 6c 75 6d 6e 3c 30 20 26 26 20 21 72 65 66 65  olumn<0 && !refe
2edd7 72 65 6e 63 65 73 4f 74 68 65 72 54 61 62 6c 65  rencesOtherTable
2edd8 73 28 70 4f 72 64 65 72 42 79 2c 20 70 4d 61 73  s(pOrderBy, pMas
2edd9 6b 53 65 74 2c 20 6a 2c 20 62 61 73 65 29 20 29  kSet, j, base) )
2edda 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  {.      /* If th
2eddb 65 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e  e indexed column
2eddc 20 69 73 20 74 68 65 20 70 72 69 6d 61 72 79 20   is the primary 
2eddd 6b 65 79 20 61 6e 64 20 65 76 65 72 79 74 68 69  key and everythi
2edde 6e 67 20 6d 61 74 63 68 65 73 0a 20 20 20 20 20  ng matches.     
2eddf 20 2a 2a 20 73 6f 20 66 61 72 20 61 6e 64 20 6e   ** so far and n
2ede0 6f 6e 65 20 6f 66 20 74 68 65 20 4f 52 44 45 52  one of the ORDER
2ede1 20 42 59 20 74 65 72 6d 73 20 74 6f 20 74 68 65   BY terms to the
2ede2 20 72 69 67 68 74 20 72 65 66 65 72 65 6e 63 65   right reference
2ede3 20 6f 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20   other.      ** 
2ede4 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a 6f  tables in the jo
2ede5 69 6e 2c 20 74 68 65 6e 20 77 65 20 61 72 65 20  in, then we are 
2ede6 61 73 73 75 72 65 64 20 74 68 61 74 20 74 68 65  assured that the
2ede7 20 69 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73   index can be us
2ede8 65 64 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  ed .      ** to 
2ede9 73 6f 72 74 20 62 65 63 61 75 73 65 20 74 68 65  sort because the
2edea 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73 20   primary key is 
2edeb 75 6e 69 71 75 65 20 61 6e 64 20 73 6f 20 6e 6f  unique and so no
2edec 6e 65 20 6f 66 20 74 68 65 20 6f 74 68 65 72 0a  ne of the other.
2eded 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 73        ** columns
2edee 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 20 64   will make any d
2edef 69 66 66 65 72 65 6e 63 65 0a 20 20 20 20 20 20  ifference.      
2edf0 2a 2f 0a 20 20 20 20 20 20 6a 20 3d 20 6e 54 65  */.      j = nTe
2edf1 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  rm;.    }.  }.. 
2edf2 20 2a 70 62 52 65 76 20 3d 20 73 6f 72 74 4f 72   *pbRev = sortOr
2edf3 64 65 72 21 3d 30 3b 0a 20 20 69 66 28 20 6a 3e  der!=0;.  if( j>
2edf4 3d 6e 54 65 72 6d 20 29 7b 0a 20 20 20 20 2f 2a  =nTerm ){.    /*
2edf5 20 41 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68   All terms of th
2edf6 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
2edf7 65 20 61 72 65 20 63 6f 76 65 72 65 64 20 62 79  e are covered by
2edf8 20 74 68 69 73 20 69 6e 64 65 78 20 73 6f 0a 20   this index so. 
2edf9 20 20 20 2a 2a 20 74 68 69 73 20 69 6e 64 65 78     ** this index
2edfa 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72   can be used for
2edfb 20 73 6f 72 74 69 6e 67 2e 20 2a 2f 0a 20 20 20   sorting. */.   
2edfc 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
2edfd 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72   if( pIdx->onErr
2edfe 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 26 26 20 69  or!=OE_None && i
2edff 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 0a  ==pIdx->nColumn.
2ee00 20 20 20 20 20 20 26 26 20 21 72 65 66 65 72 65        && !refere
2ee01 6e 63 65 73 4f 74 68 65 72 54 61 62 6c 65 73 28  ncesOtherTables(
2ee02 70 4f 72 64 65 72 42 79 2c 20 70 4d 61 73 6b 53  pOrderBy, pMaskS
2ee03 65 74 2c 20 6a 2c 20 62 61 73 65 29 20 29 7b 0a  et, j, base) ){.
2ee04 20 20 20 20 2f 2a 20 41 6c 6c 20 74 65 72 6d 73      /* All terms
2ee05 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20 6d   of this index m
2ee06 61 74 63 68 20 73 6f 6d 65 20 70 72 65 66 69 78  atch some prefix
2ee07 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   of the ORDER BY
2ee08 20 63 6c 61 75 73 65 0a 20 20 20 20 2a 2a 20 61   clause.    ** a
2ee09 6e 64 20 74 68 65 20 69 6e 64 65 78 20 69 73 20  nd the index is 
2ee0a 55 4e 49 51 55 45 20 61 6e 64 20 6e 6f 20 74 65  UNIQUE and no te
2ee0b 72 6d 73 20 6f 6e 20 74 68 65 20 74 61 69 6c 20  rms on the tail 
2ee0c 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a  of the ORDER BY.
2ee0d 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 72 65      ** clause re
2ee0e 66 65 72 65 6e 63 65 20 6f 74 68 65 72 20 74 61  ference other ta
2ee0f 62 6c 65 73 20 69 6e 20 61 20 6a 6f 69 6e 2e 20  bles in a join. 
2ee10 20 49 66 20 74 68 69 73 20 69 73 20 61 6c 6c 20   If this is all 
2ee11 74 72 75 65 20 74 68 65 6e 0a 20 20 20 20 2a 2a  true then.    **
2ee12 20 74 68 65 20 6f 72 64 65 72 20 62 79 20 63 6c   the order by cl
2ee13 61 75 73 65 20 69 73 20 73 75 70 65 72 66 6c 75  ause is superflu
2ee14 6f 75 73 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75  ous. */.    retu
2ee15 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn 1;.  }.  retu
2ee16 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  rn 0;.}../*.** P
2ee17 72 65 70 61 72 65 20 61 20 63 72 75 64 65 20 65  repare a crude e
2ee18 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20 6c  stimate of the l
2ee19 6f 67 61 72 69 74 68 6d 20 6f 66 20 74 68 65 20  ogarithm of the 
2ee1a 69 6e 70 75 74 20 76 61 6c 75 65 2e 0a 2a 2a 20  input value..** 
2ee1b 54 68 65 20 72 65 73 75 6c 74 73 20 6e 65 65 64  The results need
2ee1c 20 6e 6f 74 20 62 65 20 65 78 61 63 74 2e 20 20   not be exact.  
2ee1d 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65  This is only use
2ee1e 64 20 66 6f 72 20 65 73 74 69 6d 61 74 69 6e 67  d for estimating
2ee1f 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20 63 6f  .** the total co
2ee20 73 74 20 6f 66 20 70 65 72 66 6f 72 6d 69 6e 67  st of performing
2ee21 20 6f 70 65 72 61 74 69 6f 6e 73 20 77 69 74 68   operations with
2ee22 20 4f 28 6c 6f 67 4e 29 20 6f 72 20 4f 28 4e 6c   O(logN) or O(Nl
2ee23 6f 67 4e 29 0a 2a 2a 20 63 6f 6d 70 6c 65 78 69  ogN).** complexi
2ee24 74 79 2e 20 20 42 65 63 61 75 73 65 20 4e 20 69  ty.  Because N i
2ee25 73 20 6a 75 73 74 20 61 20 67 75 65 73 73 2c 20  s just a guess, 
2ee26 69 74 20 69 73 20 6e 6f 20 67 72 65 61 74 20 74  it is no great t
2ee27 72 61 67 65 64 79 20 69 66 0a 2a 2a 20 6c 6f 67  ragedy if.** log
2ee28 4e 20 69 73 20 61 20 6c 69 74 74 6c 65 20 6f 66  N is a little of
2ee29 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 64 6f 75  f..*/.static dou
2ee2a 62 6c 65 20 65 73 74 4c 6f 67 28 64 6f 75 62 6c  ble estLog(doubl
2ee2b 65 20 4e 29 7b 0a 20 20 64 6f 75 62 6c 65 20 6c  e N){.  double l
2ee2c 6f 67 4e 20 3d 20 31 3b 0a 20 20 64 6f 75 62 6c  ogN = 1;.  doubl
2ee2d 65 20 78 20 3d 20 31 30 3b 0a 20 20 77 68 69 6c  e x = 10;.  whil
2ee2e 65 28 20 4e 3e 78 20 29 7b 0a 20 20 20 20 6c 6f  e( N>x ){.    lo
2ee2f 67 4e 20 2b 3d 20 31 3b 0a 20 20 20 20 78 20 2a  gN += 1;.    x *
2ee30 3d 20 31 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75  = 10;.  }.  retu
2ee31 72 6e 20 6c 6f 67 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn logN;.}../*.*
2ee32 2a 20 54 77 6f 20 72 6f 75 74 69 6e 65 73 20 66  * Two routines f
2ee33 6f 72 20 70 72 69 6e 74 69 6e 67 20 74 68 65 20  or printing the 
2ee34 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 73 71  content of an sq
2ee35 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
2ee36 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20  .** structure.  
2ee37 55 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  Used for testing
2ee38 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f   and debugging o
2ee39 6e 6c 79 2e 20 20 49 66 20 6e 65 69 74 68 65 72  nly.  If neither
2ee3a 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 20  .** SQLITE_TEST 
2ee3b 6f 72 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  or SQLITE_DEBUG 
2ee3c 61 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68 65  are defined, the
2ee3d 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  n these routines
2ee3e 0a 2a 2a 20 61 72 65 20 6e 6f 2d 6f 70 73 2e 0a  .** are no-ops..
2ee3f 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
2ee40 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2ee41 55 41 4c 54 41 42 4c 45 29 20 26 26 20 64 65 66  UALTABLE) && def
2ee42 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
2ee43 47 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 54  G).static void T
2ee44 52 41 43 45 5f 49 44 58 5f 49 4e 50 55 54 53 28  RACE_IDX_INPUTS(
2ee45 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2ee46 66 6f 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b  fo *p){.  int i;
2ee47 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 57  .  if( !sqlite3W
2ee48 68 65 72 65 54 72 61 63 65 20 29 20 72 65 74 75  hereTrace ) retu
2ee49 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  rn;.  for(i=0; i
2ee4a 3c 70 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  <p->nConstraint;
2ee4b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74   i++){.    sqlit
2ee4c 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20  e3DebugPrintf(" 
2ee4d 20 63 6f 6e 73 74 72 61 69 6e 74 5b 25 64 5d 3a   constraint[%d]:
2ee4e 20 63 6f 6c 3d 25 64 20 74 65 72 6d 69 64 3d 25   col=%d termid=%
2ee4f 64 20 6f 70 3d 25 64 20 75 73 61 62 6c 65 64 3d  d op=%d usabled=
2ee50 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 69 2c  %d\n",.       i,
2ee51 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73  .       p->aCons
2ee52 74 72 61 69 6e 74 5b 69 5d 2e 69 43 6f 6c 75 6d  traint[i].iColum
2ee53 6e 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f  n,.       p->aCo
2ee54 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 69 54 65 72  nstraint[i].iTer
2ee55 6d 4f 66 66 73 65 74 2c 0a 20 20 20 20 20 20 20  mOffset,.       
2ee56 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69  p->aConstraint[i
2ee57 5d 2e 6f 70 2c 0a 20 20 20 20 20 20 20 70 2d 3e  ].op,.       p->
2ee58 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 75  aConstraint[i].u
2ee59 73 61 62 6c 65 29 3b 0a 20 20 7d 0a 20 20 66 6f  sable);.  }.  fo
2ee5a 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 72 64  r(i=0; i<p->nOrd
2ee5b 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  erBy; i++){.    
2ee5c 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
2ee5d 74 66 28 22 20 20 6f 72 64 65 72 62 79 5b 25 64  tf("  orderby[%d
2ee5e 5d 3a 20 63 6f 6c 3d 25 64 20 64 65 73 63 3d 25  ]: col=%d desc=%
2ee5f 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 69 2c 0a  d\n",.       i,.
2ee60 20 20 20 20 20 20 20 70 2d 3e 61 4f 72 64 65 72         p->aOrder
2ee61 42 79 5b 69 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a 20  By[i].iColumn,. 
2ee62 20 20 20 20 20 20 70 2d 3e 61 4f 72 64 65 72 42        p->aOrderB
2ee63 79 5b 69 5d 2e 64 65 73 63 29 3b 0a 20 20 7d 0a  y[i].desc);.  }.
2ee64 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 54 52  }.static void TR
2ee65 41 43 45 5f 49 44 58 5f 4f 55 54 50 55 54 53 28  ACE_IDX_OUTPUTS(
2ee66 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2ee67 66 6f 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b  fo *p){.  int i;
2ee68 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 57  .  if( !sqlite3W
2ee69 68 65 72 65 54 72 61 63 65 20 29 20 72 65 74 75  hereTrace ) retu
2ee6a 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  rn;.  for(i=0; i
2ee6b 3c 70 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  <p->nConstraint;
2ee6c 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74   i++){.    sqlit
2ee6d 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20  e3DebugPrintf(" 
2ee6e 20 75 73 61 67 65 5b 25 64 5d 3a 20 61 72 67 76   usage[%d]: argv
2ee6f 49 64 78 3d 25 64 20 6f 6d 69 74 3d 25 64 5c 6e  Idx=%d omit=%d\n
2ee70 22 2c 0a 20 20 20 20 20 20 20 69 2c 0a 20 20 20  ",.       i,.   
2ee71 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69      p->aConstrai
2ee72 6e 74 55 73 61 67 65 5b 69 5d 2e 61 72 67 76 49  ntUsage[i].argvI
2ee73 6e 64 65 78 2c 0a 20 20 20 20 20 20 20 70 2d 3e  ndex,.       p->
2ee74 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
2ee75 5b 69 5d 2e 6f 6d 69 74 29 3b 0a 20 20 7d 0a 20  [i].omit);.  }. 
2ee76 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2ee77 6e 74 66 28 22 20 20 69 64 78 4e 75 6d 3d 25 64  ntf("  idxNum=%d
2ee78 5c 6e 22 2c 20 70 2d 3e 69 64 78 4e 75 6d 29 3b  \n", p->idxNum);
2ee79 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50  .  sqlite3DebugP
2ee7a 72 69 6e 74 66 28 22 20 20 69 64 78 53 74 72 3d  rintf("  idxStr=
2ee7b 25 73 5c 6e 22 2c 20 70 2d 3e 69 64 78 53 74 72  %s\n", p->idxStr
2ee7c 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75  );.  sqlite3Debu
2ee7d 67 50 72 69 6e 74 66 28 22 20 20 6f 72 64 65 72  gPrintf("  order
2ee7e 42 79 43 6f 6e 73 75 6d 65 64 3d 25 64 5c 6e 22  ByConsumed=%d\n"
2ee7f 2c 20 70 2d 3e 6f 72 64 65 72 42 79 43 6f 6e 73  , p->orderByCons
2ee80 75 6d 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 33  umed);.  sqlite3
2ee81 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 20 65  DebugPrintf("  e
2ee82 73 74 69 6d 61 74 65 64 43 6f 73 74 3d 25 67 5c  stimatedCost=%g\
2ee83 6e 22 2c 20 70 2d 3e 65 73 74 69 6d 61 74 65 64  n", p->estimated
2ee84 43 6f 73 74 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23  Cost);.}.#else.#
2ee85 64 65 66 69 6e 65 20 54 52 41 43 45 5f 49 44 58  define TRACE_IDX
2ee86 5f 49 4e 50 55 54 53 28 41 29 0a 23 64 65 66 69  _INPUTS(A).#defi
2ee87 6e 65 20 54 52 41 43 45 5f 49 44 58 5f 4f 55 54  ne TRACE_IDX_OUT
2ee88 50 55 54 53 28 41 29 0a 23 65 6e 64 69 66 0a 0a  PUTS(A).#endif..
2ee89 2f 2a 20 0a 2a 2a 20 52 65 71 75 69 72 65 64 20  /* .** Required 
2ee8a 62 65 63 61 75 73 65 20 62 65 73 74 49 6e 64 65  because bestInde
2ee8b 78 28 29 20 69 73 20 63 61 6c 6c 65 64 20 62 79  x() is called by
2ee8c 20 62 65 73 74 4f 72 43 6c 61 75 73 65 49 6e 64   bestOrClauseInd
2ee8d 65 78 28 29 20 0a 2a 2f 0a 73 74 61 74 69 63 20  ex() .*/.static 
2ee8e 76 6f 69 64 20 62 65 73 74 49 6e 64 65 78 28 0a  void bestIndex(.
2ee8f 20 20 20 20 50 61 72 73 65 2a 2c 20 57 68 65 72      Parse*, Wher
2ee90 65 43 6c 61 75 73 65 2a 2c 20 73 74 72 75 63 74  eClause*, struct
2ee91 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 2a 2c 20   SrcList_item*, 
2ee92 42 69 74 6d 61 73 6b 2c 20 45 78 70 72 4c 69 73  Bitmask, ExprLis
2ee93 74 2a 2c 20 57 68 65 72 65 43 6f 73 74 2a 29 3b  t*, WhereCost*);
2ee94 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
2ee95 74 69 6e 65 20 61 74 74 65 6d 70 74 73 20 74 6f  tine attempts to
2ee96 20 66 69 6e 64 20 61 6e 20 73 63 61 6e 6e 69 6e   find an scannin
2ee97 67 20 73 74 72 61 74 65 67 79 20 74 68 61 74 20  g strategy that 
2ee98 63 61 6e 20 62 65 20 75 73 65 64 20 0a 2a 2a 20  can be used .** 
2ee99 74 6f 20 6f 70 74 69 6d 69 7a 65 20 61 6e 20 27  to optimize an '
2ee9a 4f 52 27 20 65 78 70 72 65 73 73 69 6f 6e 20 74  OR' expression t
2ee9b 68 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 61  hat is part of a
2ee9c 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 0a   WHERE clause. .
2ee9d 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20  **.** The table 
2ee9e 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
2ee9f 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d  FROM clause term
2eea0 20 70 53 72 63 20 6d 61 79 20 62 65 20 65 69 74   pSrc may be eit
2eea1 68 65 72 20 61 0a 2a 2a 20 72 65 67 75 6c 61 72  her a.** regular
2eea2 20 42 2d 54 72 65 65 20 74 61 62 6c 65 20 6f 72   B-Tree table or
2eea3 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
2eea4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2eea5 20 62 65 73 74 4f 72 43 6c 61 75 73 65 49 6e 64   bestOrClauseInd
2eea6 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ex(.  Parse *pPa
2eea7 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
2eea8 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67    /* The parsing
2eea9 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68   context */.  Wh
2eeaa 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20  ereClause *pWC, 
2eeab 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2eeac 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
2eead 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
2eeae 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20 20 2f  t_item *pSrc,  /
2eeaf 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  * The FROM claus
2eeb0 65 20 74 65 72 6d 20 74 6f 20 73 65 61 72 63 68  e term to search
2eeb1 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f   */.  Bitmask no
2eeb2 74 52 65 61 64 79 2c 20 20 20 20 20 20 20 20 20  tReady,         
2eeb3 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 63 75 72    /* Mask of cur
2eeb4 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 6e 6f  sors that are no
2eeb5 74 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20  t available */. 
2eeb6 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
2eeb7 72 42 79 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rBy,         /* 
2eeb8 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  The ORDER BY cla
2eeb9 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 43 6f  use */.  WhereCo
2eeba 73 74 20 2a 70 43 6f 73 74 20 20 20 20 20 20 20  st *pCost       
2eebb 20 20 20 20 20 2f 2a 20 4c 6f 77 65 73 74 20 63       /* Lowest c
2eebc 6f 73 74 20 71 75 65 72 79 20 70 6c 61 6e 20 2a  ost query plan *
2eebd 2f 0a 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  /.){.#ifndef SQL
2eebe 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49  ITE_OMIT_OR_OPTI
2eebf 4d 49 5a 41 54 49 4f 4e 0a 20 20 63 6f 6e 73 74  MIZATION.  const
2eec0 20 69 6e 74 20 69 43 75 72 20 3d 20 70 53 72 63   int iCur = pSrc
2eec1 2d 3e 69 43 75 72 73 6f 72 3b 20 20 20 2f 2a 20  ->iCursor;   /* 
2eec2 54 68 65 20 63 75 72 73 6f 72 20 6f 66 20 74 68  The cursor of th
2eec3 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61 63  e table to be ac
2eec4 63 65 73 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73  cessed */.  cons
2eec5 74 20 42 69 74 6d 61 73 6b 20 6d 61 73 6b 53 72  t Bitmask maskSr
2eec6 63 20 3d 20 67 65 74 4d 61 73 6b 28 70 57 43 2d  c = getMask(pWC-
2eec7 3e 70 4d 61 73 6b 53 65 74 2c 20 69 43 75 72 29  >pMaskSet, iCur)
2eec8 3b 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 66 6f  ;  /* Bitmask fo
2eec9 72 20 70 53 72 63 20 2a 2f 0a 20 20 57 68 65 72  r pSrc */.  Wher
2eeca 65 54 65 72 6d 20 2a 20 63 6f 6e 73 74 20 70 57  eTerm * const pW
2eecb 43 45 6e 64 20 3d 20 26 70 57 43 2d 3e 61 5b 70  CEnd = &pWC->a[p
2eecc 57 43 2d 3e 6e 54 65 72 6d 5d 3b 20 20 20 20 20  WC->nTerm];     
2eecd 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 70 57 43     /* End of pWC
2eece 2d 3e 61 5b 5d 20 2a 2f 0a 20 20 57 68 65 72 65  ->a[] */.  Where
2eecf 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20  Term *pTerm;    
2eed0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2eed1 41 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 6f 66  A single term of
2eed2 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2eed3 65 20 2a 2f 0a 0a 20 20 2f 2a 20 53 65 61 72 63  e */..  /* Searc
2eed4 68 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  h the WHERE clau
2eed5 73 65 20 74 65 72 6d 73 20 66 6f 72 20 61 20 75  se terms for a u
2eed6 73 61 62 6c 65 20 57 4f 5f 4f 52 20 74 65 72 6d  sable WO_OR term
2eed7 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 54 65 72 6d  . */.  for(pTerm
2eed8 3d 70 57 43 2d 3e 61 3b 20 70 54 65 72 6d 3c 70  =pWC->a; pTerm<p
2eed9 57 43 45 6e 64 3b 20 70 54 65 72 6d 2b 2b 29 7b  WCEnd; pTerm++){
2eeda 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e  .    if( pTerm->
2eedb 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52  eOperator==WO_OR
2eedc 20 0a 20 20 20 20 20 26 26 20 28 28 70 54 65 72   .     && ((pTer
2eedd 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 20 26 20 7e  m->prereqAll & ~
2eede 6d 61 73 6b 53 72 63 29 20 26 20 6e 6f 74 52 65  maskSrc) & notRe
2eedf 61 64 79 29 3d 3d 30 0a 20 20 20 20 20 26 26 20  ady)==0.     && 
2eee0 28 70 54 65 72 6d 2d 3e 75 2e 70 4f 72 49 6e 66  (pTerm->u.pOrInf
2eee1 6f 2d 3e 69 6e 64 65 78 61 62 6c 65 20 26 20 6d  o->indexable & m
2eee2 61 73 6b 53 72 63 29 21 3d 30 20 0a 20 20 20 20  askSrc)!=0 .    
2eee3 29 7b 0a 20 20 20 20 20 20 57 68 65 72 65 43 6c  ){.      WhereCl
2eee4 61 75 73 65 20 2a 20 63 6f 6e 73 74 20 70 4f 72  ause * const pOr
2eee5 57 43 20 3d 20 26 70 54 65 72 6d 2d 3e 75 2e 70  WC = &pTerm->u.p
2eee6 4f 72 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20 20  OrInfo->wc;.    
2eee7 20 20 57 68 65 72 65 54 65 72 6d 20 2a 20 63 6f    WhereTerm * co
2eee8 6e 73 74 20 70 4f 72 57 43 45 6e 64 20 3d 20 26  nst pOrWCEnd = &
2eee9 70 4f 72 57 43 2d 3e 61 5b 70 4f 72 57 43 2d 3e  pOrWC->a[pOrWC->
2eeea 6e 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20 57 68  nTerm];.      Wh
2eeeb 65 72 65 54 65 72 6d 20 2a 70 4f 72 54 65 72 6d  ereTerm *pOrTerm
2eeec 3b 0a 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67  ;.      int flag
2eeed 73 20 3d 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f  s = WHERE_MULTI_
2eeee 4f 52 3b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65  OR;.      double
2eeef 20 72 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 20   rTotal = 0;.   
2eef0 20 20 20 64 6f 75 62 6c 65 20 6e 52 6f 77 20 3d     double nRow =
2eef1 20 30 3b 0a 20 20 20 20 20 20 42 69 74 6d 61 73   0;.      Bitmas
2eef2 6b 20 75 73 65 64 20 3d 20 30 3b 0a 0a 20 20 20  k used = 0;..   
2eef3 20 20 20 66 6f 72 28 70 4f 72 54 65 72 6d 3d 70     for(pOrTerm=p
2eef4 4f 72 57 43 2d 3e 61 3b 20 70 4f 72 54 65 72 6d  OrWC->a; pOrTerm
2eef5 3c 70 4f 72 57 43 45 6e 64 3b 20 70 4f 72 54 65  <pOrWCEnd; pOrTe
2eef6 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 57  rm++){.        W
2eef7 68 65 72 65 43 6f 73 74 20 73 54 65 72 6d 43 6f  hereCost sTermCo
2eef8 73 74 3b 0a 20 20 20 20 20 20 20 20 57 48 45 52  st;.        WHER
2eef9 45 54 52 41 43 45 28 28 22 2e 2e 2e 20 4d 75 6c  ETRACE(("... Mul
2eefa 74 69 2d 69 6e 64 65 78 20 4f 52 20 74 65 73 74  ti-index OR test
2eefb 69 6e 67 20 66 6f 72 20 74 65 72 6d 20 25 64 20  ing for term %d 
2eefc 6f 66 20 25 64 2e 2e 2e 2e 5c 6e 22 2c 20 0a 20  of %d....\n", . 
2eefd 20 20 20 20 20 20 20 20 20 28 70 4f 72 54 65 72           (pOrTer
2eefe 6d 20 2d 20 70 4f 72 57 43 2d 3e 61 29 2c 20 28  m - pOrWC->a), (
2eeff 70 54 65 72 6d 20 2d 20 70 57 43 2d 3e 61 29 0a  pTerm - pWC->a).
2ef00 20 20 20 20 20 20 20 20 29 29 3b 0a 20 20 20 20          ));.    
2ef01 20 20 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d      if( pOrTerm-
2ef02 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 41  >eOperator==WO_A
2ef03 4e 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ND ){.          
2ef04 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 41 6e  WhereClause *pAn
2ef05 64 57 43 20 3d 20 26 70 4f 72 54 65 72 6d 2d 3e  dWC = &pOrTerm->
2ef06 75 2e 70 41 6e 64 49 6e 66 6f 2d 3e 77 63 3b 0a  u.pAndInfo->wc;.
2ef07 20 20 20 20 20 20 20 20 20 20 62 65 73 74 49 6e            bestIn
2ef08 64 65 78 28 70 50 61 72 73 65 2c 20 70 41 6e 64  dex(pParse, pAnd
2ef09 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74 52 65 61  WC, pSrc, notRea
2ef0a 64 79 2c 20 30 2c 20 26 73 54 65 72 6d 43 6f 73  dy, 0, &sTermCos
2ef0b 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  t);.        }els
2ef0c 65 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c  e if( pOrTerm->l
2ef0d 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20  eftCursor==iCur 
2ef0e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 57 68 65  ){.          Whe
2ef0f 72 65 43 6c 61 75 73 65 20 74 65 6d 70 57 43 3b  reClause tempWC;
2ef10 0a 20 20 20 20 20 20 20 20 20 20 74 65 6d 70 57  .          tempW
2ef11 43 2e 70 50 61 72 73 65 20 3d 20 70 57 43 2d 3e  C.pParse = pWC->
2ef12 70 50 61 72 73 65 3b 0a 20 20 20 20 20 20 20 20  pParse;.        
2ef13 20 20 74 65 6d 70 57 43 2e 70 4d 61 73 6b 53 65    tempWC.pMaskSe
2ef14 74 20 3d 20 70 57 43 2d 3e 70 4d 61 73 6b 53 65  t = pWC->pMaskSe
2ef15 74 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65 6d  t;.          tem
2ef16 70 57 43 2e 6f 70 20 3d 20 54 4b 5f 41 4e 44 3b  pWC.op = TK_AND;
2ef17 0a 20 20 20 20 20 20 20 20 20 20 74 65 6d 70 57  .          tempW
2ef18 43 2e 61 20 3d 20 70 4f 72 54 65 72 6d 3b 0a 20  C.a = pOrTerm;. 
2ef19 20 20 20 20 20 20 20 20 20 74 65 6d 70 57 43 2e           tempWC.
2ef1a 6e 54 65 72 6d 20 3d 20 31 3b 0a 20 20 20 20 20  nTerm = 1;.     
2ef1b 20 20 20 20 20 62 65 73 74 49 6e 64 65 78 28 70       bestIndex(p
2ef1c 50 61 72 73 65 2c 20 26 74 65 6d 70 57 43 2c 20  Parse, &tempWC, 
2ef1d 70 53 72 63 2c 20 6e 6f 74 52 65 61 64 79 2c 20  pSrc, notReady, 
2ef1e 30 2c 20 26 73 54 65 72 6d 43 6f 73 74 29 3b 0a  0, &sTermCost);.
2ef1f 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2ef20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75           continu
2ef21 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  e;.        }.   
2ef22 20 20 20 20 20 72 54 6f 74 61 6c 20 2b 3d 20 73       rTotal += s
2ef23 54 65 72 6d 43 6f 73 74 2e 72 43 6f 73 74 3b 0a  TermCost.rCost;.
2ef24 20 20 20 20 20 20 20 20 6e 52 6f 77 20 2b 3d 20          nRow += 
2ef25 73 54 65 72 6d 43 6f 73 74 2e 6e 52 6f 77 3b 0a  sTermCost.nRow;.
2ef26 20 20 20 20 20 20 20 20 75 73 65 64 20 7c 3d 20          used |= 
2ef27 73 54 65 72 6d 43 6f 73 74 2e 75 73 65 64 3b 0a  sTermCost.used;.
2ef28 20 20 20 20 20 20 20 20 69 66 28 20 72 54 6f 74          if( rTot
2ef29 61 6c 3e 3d 70 43 6f 73 74 2d 3e 72 43 6f 73 74  al>=pCost->rCost
2ef2a 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
2ef2b 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  }..      /* If t
2ef2c 68 65 72 65 20 69 73 20 61 6e 20 4f 52 44 45 52  here is an ORDER
2ef2d 20 42 59 20 63 6c 61 75 73 65 2c 20 69 6e 63 72   BY clause, incr
2ef2e 65 61 73 65 20 74 68 65 20 73 63 61 6e 20 63 6f  ease the scan co
2ef2f 73 74 20 74 6f 20 61 63 63 6f 75 6e 74 20 0a 20  st to account . 
2ef30 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 20       ** for the 
2ef31 63 6f 73 74 20 6f 66 20 74 68 65 20 73 6f 72 74  cost of the sort
2ef32 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70  . */.      if( p
2ef33 4f 72 64 65 72 42 79 21 3d 30 20 29 7b 0a 20 20  OrderBy!=0 ){.  
2ef34 20 20 20 20 20 20 72 54 6f 74 61 6c 20 2b 3d 20        rTotal += 
2ef35 6e 52 6f 77 2a 65 73 74 4c 6f 67 28 6e 52 6f 77  nRow*estLog(nRow
2ef36 29 3b 0a 20 20 20 20 20 20 20 20 57 48 45 52 45  );.        WHERE
2ef37 54 52 41 43 45 28 28 22 2e 2e 2e 20 73 6f 72 74  TRACE(("... sort
2ef38 69 6e 67 20 69 6e 63 72 65 61 73 65 73 20 4f 52  ing increases OR
2ef39 20 63 6f 73 74 20 74 6f 20 25 2e 39 67 5c 6e 22   cost to %.9g\n"
2ef3a 2c 20 72 54 6f 74 61 6c 29 29 3b 0a 20 20 20 20  , rTotal));.    
2ef3b 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66    }..      /* If
2ef3c 20 74 68 65 20 63 6f 73 74 20 6f 66 20 73 63 61   the cost of sca
2ef3d 6e 6e 69 6e 67 20 75 73 69 6e 67 20 74 68 69 73  nning using this
2ef3e 20 4f 52 20 74 65 72 6d 20 66 6f 72 20 6f 70 74   OR term for opt
2ef3f 69 6d 69 7a 61 74 69 6f 6e 20 69 73 0a 20 20 20  imization is.   
2ef40 20 20 20 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20     ** less than 
2ef41 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 73 74  the current cost
2ef42 20 73 74 6f 72 65 64 20 69 6e 20 70 43 6f 73 74   stored in pCost
2ef43 2c 20 72 65 70 6c 61 63 65 20 74 68 65 20 63 6f  , replace the co
2ef44 6e 74 65 6e 74 73 0a 20 20 20 20 20 20 2a 2a 20  ntents.      ** 
2ef45 6f 66 20 70 43 6f 73 74 2e 20 2a 2f 0a 20 20 20  of pCost. */.   
2ef46 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
2ef47 2e 2e 2e 20 6d 75 6c 74 69 2d 69 6e 64 65 78 20  ... multi-index 
2ef48 4f 52 20 63 6f 73 74 3d 25 2e 39 67 20 6e 72 6f  OR cost=%.9g nro
2ef49 77 3d 25 2e 39 67 5c 6e 22 2c 20 72 54 6f 74 61  w=%.9g\n", rTota
2ef4a 6c 2c 20 6e 52 6f 77 29 29 3b 0a 20 20 20 20 20  l, nRow));.     
2ef4b 20 69 66 28 20 72 54 6f 74 61 6c 3c 70 43 6f 73   if( rTotal<pCos
2ef4c 74 2d 3e 72 43 6f 73 74 20 29 7b 0a 20 20 20 20  t->rCost ){.    
2ef4d 20 20 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74      pCost->rCost
2ef4e 20 3d 20 72 54 6f 74 61 6c 3b 0a 20 20 20 20 20   = rTotal;.     
2ef4f 20 20 20 70 43 6f 73 74 2d 3e 6e 52 6f 77 20 3d     pCost->nRow =
2ef50 20 6e 52 6f 77 3b 0a 20 20 20 20 20 20 20 20 70   nRow;.        p
2ef51 43 6f 73 74 2d 3e 75 73 65 64 20 3d 20 75 73 65  Cost->used = use
2ef52 64 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 73 74  d;.        pCost
2ef53 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 3d  ->plan.wsFlags =
2ef54 20 66 6c 61 67 73 3b 0a 20 20 20 20 20 20 20 20   flags;.        
2ef55 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 54  pCost->plan.u.pT
2ef56 65 72 6d 20 3d 20 70 54 65 72 6d 3b 0a 20 20 20  erm = pTerm;.   
2ef57 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23     }.    }.  }.#
2ef58 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2ef59 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41  OMIT_OR_OPTIMIZA
2ef5a 54 49 4f 4e 20 2a 2f 0a 7d 0a 0a 23 69 66 6e 64  TION */.}..#ifnd
2ef5b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2ef5c 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a 2a  IRTUALTABLE./*.*
2ef5d 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 70  * Allocate and p
2ef5e 6f 70 75 6c 61 74 65 20 61 6e 20 73 71 6c 69 74  opulate an sqlit
2ef5f 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74  e3_index_info st
2ef60 72 75 63 74 75 72 65 2e 20 49 74 20 69 73 20 74  ructure. It is t
2ef61 68 65 20 0a 2a 2a 20 72 65 73 70 6f 6e 73 69 62  he .** responsib
2ef62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c  ility of the cal
2ef63 6c 65 72 20 74 6f 20 65 76 65 6e 74 75 61 6c 6c  ler to eventuall
2ef64 79 20 72 65 6c 65 61 73 65 20 74 68 65 20 73 74  y release the st
2ef65 72 75 63 74 75 72 65 0a 2a 2a 20 62 79 20 70 61  ructure.** by pa
2ef66 73 73 69 6e 67 20 74 68 65 20 70 6f 69 6e 74 65  ssing the pointe
2ef67 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  r returned by th
2ef68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 73  is function to s
2ef69 71 6c 69 74 65 33 5f 66 72 65 65 28 29 2e 0a 2a  qlite3_free()..*
2ef6a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33  /.static sqlite3
2ef6b 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 61 6c 6c  _index_info *all
2ef6c 6f 63 61 74 65 49 6e 64 65 78 49 6e 66 6f 28 0a  ocateIndexInfo(.
2ef6d 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2ef6e 20 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20   .  WhereClause 
2ef6f 2a 70 57 43 2c 0a 20 20 73 74 72 75 63 74 20 53  *pWC,.  struct S
2ef70 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72  rcList_item *pSr
2ef71 63 2c 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  c,.  ExprList *p
2ef72 4f 72 64 65 72 42 79 0a 29 7b 0a 20 20 69 6e 74  OrderBy.){.  int
2ef73 20 69 2c 20 6a 3b 0a 20 20 69 6e 74 20 6e 54 65   i, j;.  int nTe
2ef74 72 6d 3b 0a 20 20 73 74 72 75 63 74 20 73 71 6c  rm;.  struct sql
2ef75 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74  ite3_index_const
2ef76 72 61 69 6e 74 20 2a 70 49 64 78 43 6f 6e 73 3b  raint *pIdxCons;
2ef77 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  .  struct sqlite
2ef78 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 20  3_index_orderby 
2ef79 2a 70 49 64 78 4f 72 64 65 72 42 79 3b 0a 20 20  *pIdxOrderBy;.  
2ef7a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
2ef7b 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f  ndex_constraint_
2ef7c 75 73 61 67 65 20 2a 70 55 73 61 67 65 3b 0a 20  usage *pUsage;. 
2ef7d 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72   WhereTerm *pTer
2ef7e 6d 3b 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42  m;.  int nOrderB
2ef7f 79 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 64  y;.  sqlite3_ind
2ef80 65 78 5f 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66  ex_info *pIdxInf
2ef81 6f 3b 0a 0a 20 20 57 48 45 52 45 54 52 41 43 45  o;..  WHERETRACE
2ef82 28 28 22 52 65 63 6f 6d 70 75 74 69 6e 67 20 69  (("Recomputing i
2ef83 6e 64 65 78 20 69 6e 66 6f 20 66 6f 72 20 25 73  ndex info for %s
2ef84 2e 2e 2e 5c 6e 22 2c 20 70 53 72 63 2d 3e 70 54  ...\n", pSrc->pT
2ef85 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 0a 20 20  ab->zName));..  
2ef86 2f 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d  /* Count the num
2ef87 62 65 72 20 6f 66 20 70 6f 73 73 69 62 6c 65 20  ber of possible 
2ef88 57 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f 6e  WHERE clause con
2ef89 73 74 72 61 69 6e 74 73 20 72 65 66 65 72 72 69  straints referri
2ef8a 6e 67 0a 20 20 2a 2a 20 74 6f 20 74 68 69 73 20  ng.  ** to this 
2ef8b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f  virtual table */
2ef8c 0a 20 20 66 6f 72 28 69 3d 6e 54 65 72 6d 3d 30  .  for(i=nTerm=0
2ef8d 2c 20 70 54 65 72 6d 3d 70 57 43 2d 3e 61 3b 20  , pTerm=pWC->a; 
2ef8e 69 3c 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 69 2b  i<pWC->nTerm; i+
2ef8f 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20  +, pTerm++){.   
2ef90 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74   if( pTerm->left
2ef91 43 75 72 73 6f 72 20 21 3d 20 70 53 72 63 2d 3e  Cursor != pSrc->
2ef92 69 43 75 72 73 6f 72 20 29 20 63 6f 6e 74 69 6e  iCursor ) contin
2ef93 75 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ue;.    assert( 
2ef94 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f  (pTerm->eOperato
2ef95 72 26 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  r&(pTerm->eOpera
2ef96 74 6f 72 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20  tor-1))==0 );.  
2ef97 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72    testcase( pTer
2ef98 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f  m->eOperator==WO
2ef99 5f 49 4e 20 29 3b 0a 20 20 20 20 74 65 73 74 63  _IN );.    testc
2ef9a 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65  ase( pTerm->eOpe
2ef9b 72 61 74 6f 72 3d 3d 57 4f 5f 49 53 4e 55 4c 4c  rator==WO_ISNULL
2ef9c 20 29 3b 0a 20 20 20 20 69 66 28 20 70 54 65 72   );.    if( pTer
2ef9d 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28  m->eOperator & (
2ef9e 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e 55 4c 4c 29  WO_IN|WO_ISNULL)
2ef9f 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
2efa0 20 6e 54 65 72 6d 2b 2b 3b 0a 20 20 7d 0a 0a 20   nTerm++;.  }.. 
2efa1 20 2f 2a 20 49 66 20 74 68 65 20 4f 52 44 45 52   /* If the ORDER
2efa2 20 42 59 20 63 6c 61 75 73 65 20 63 6f 6e 74 61   BY clause conta
2efa3 69 6e 73 20 6f 6e 6c 79 20 63 6f 6c 75 6d 6e 73  ins only columns
2efa4 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20   in the current 
2efa5 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 74 61  .  ** virtual ta
2efa6 62 6c 65 20 74 68 65 6e 20 61 6c 6c 6f 63 61 74  ble then allocat
2efa7 65 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20  e space for the 
2efa8 61 4f 72 64 65 72 42 79 20 70 61 72 74 20 6f 66  aOrderBy part of
2efa9 0a 20 20 2a 2a 20 74 68 65 20 73 71 6c 69 74 65  .  ** the sqlite
2efaa 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72  3_index_info str
2efab 75 63 74 75 72 65 2e 0a 20 20 2a 2f 0a 20 20 6e  ucture..  */.  n
2efac 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 69  OrderBy = 0;.  i
2efad 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  f( pOrderBy ){. 
2efae 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f     for(i=0; i<pO
2efaf 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69  rderBy->nExpr; i
2efb0 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20  ++){.      Expr 
2efb1 2a 70 45 78 70 72 20 3d 20 70 4f 72 64 65 72 42  *pExpr = pOrderB
2efb2 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20  y->a[i].pExpr;. 
2efb3 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e       if( pExpr->
2efb4 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c  op!=TK_COLUMN ||
2efb5 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 21 3d   pExpr->iTable!=
2efb6 70 53 72 63 2d 3e 69 43 75 72 73 6f 72 20 29 20  pSrc->iCursor ) 
2efb7 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
2efb8 20 69 66 28 20 69 3d 3d 70 4f 72 64 65 72 42 79   if( i==pOrderBy
2efb9 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20  ->nExpr ){.     
2efba 20 6e 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64   nOrderBy = pOrd
2efbb 65 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20  erBy->nExpr;.   
2efbc 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c   }.  }..  /* All
2efbd 6f 63 61 74 65 20 74 68 65 20 73 71 6c 69 74 65  ocate the sqlite
2efbe 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72  3_index_info str
2efbf 75 63 74 75 72 65 0a 20 20 2a 2f 0a 20 20 70 49  ucture.  */.  pI
2efc0 64 78 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33  dxInfo = sqlite3
2efc1 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 50 61  DbMallocZero(pPa
2efc2 72 73 65 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28  rse->db, sizeof(
2efc3 2a 70 49 64 78 49 6e 66 6f 29 0a 20 20 20 20 20  *pIdxInfo).     
2efc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2efc5 20 20 20 20 20 20 2b 20 28 73 69 7a 65 6f 66 28        + (sizeof(
2efc6 2a 70 49 64 78 43 6f 6e 73 29 20 2b 20 73 69 7a  *pIdxCons) + siz
2efc7 65 6f 66 28 2a 70 55 73 61 67 65 29 29 2a 6e 54  eof(*pUsage))*nT
2efc8 65 72 6d 0a 20 20 20 20 20 20 20 20 20 20 20 20  erm.            
2efc9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2b                 +
2efca 20 73 69 7a 65 6f 66 28 2a 70 49 64 78 4f 72 64   sizeof(*pIdxOrd
2efcb 65 72 42 79 29 2a 6e 4f 72 64 65 72 42 79 20 29  erBy)*nOrderBy )
2efcc 3b 0a 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f  ;.  if( pIdxInfo
2efcd 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
2efce 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2efcf 65 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  e, "out of memor
2efd0 79 22 29 3b 0a 20 20 20 20 2f 2a 20 28 64 6f 75  y");.    /* (dou
2efd1 62 6c 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66  ble)0 In case of
2efd2 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
2efd3 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a  ATING_POINT... *
2efd4 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  /.    return 0;.
2efd5 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61    }..  /* Initia
2efd6 6c 69 7a 65 20 74 68 65 20 73 74 72 75 63 74 75  lize the structu
2efd7 72 65 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  re.  The sqlite3
2efd8 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75  _index_info stru
2efd9 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 0a 20  cture contains. 
2efda 20 2a 2a 20 6d 61 6e 79 20 66 69 65 6c 64 73 20   ** many fields 
2efdb 74 68 61 74 20 61 72 65 20 64 65 63 6c 61 72 65  that are declare
2efdc 64 20 22 63 6f 6e 73 74 22 20 74 6f 20 70 72 65  d "const" to pre
2efdd 76 65 6e 74 20 78 42 65 73 74 49 6e 64 65 78 20  vent xBestIndex 
2efde 66 72 6f 6d 0a 20 20 2a 2a 20 63 68 61 6e 67 69  from.  ** changi
2efdf 6e 67 20 74 68 65 6d 2e 20 20 57 65 20 68 61 76  ng them.  We hav
2efe0 65 20 74 6f 20 64 6f 20 73 6f 6d 65 20 66 75 6e  e to do some fun
2efe1 6b 79 20 63 61 73 74 69 6e 67 20 69 6e 20 6f 72  ky casting in or
2efe2 64 65 72 20 74 6f 0a 20 20 2a 2a 20 69 6e 69 74  der to.  ** init
2efe3 69 61 6c 69 7a 65 20 74 68 6f 73 65 20 66 69 65  ialize those fie
2efe4 6c 64 73 2e 0a 20 20 2a 2f 0a 20 20 70 49 64 78  lds..  */.  pIdx
2efe5 43 6f 6e 73 20 3d 20 28 73 74 72 75 63 74 20 73  Cons = (struct s
2efe6 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e  qlite3_index_con
2efe7 73 74 72 61 69 6e 74 2a 29 26 70 49 64 78 49 6e  straint*)&pIdxIn
2efe8 66 6f 5b 31 5d 3b 0a 20 20 70 49 64 78 4f 72 64  fo[1];.  pIdxOrd
2efe9 65 72 42 79 20 3d 20 28 73 74 72 75 63 74 20 73  erBy = (struct s
2efea 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64  qlite3_index_ord
2efeb 65 72 62 79 2a 29 26 70 49 64 78 43 6f 6e 73 5b  erby*)&pIdxCons[
2efec 6e 54 65 72 6d 5d 3b 0a 20 20 70 55 73 61 67 65  nTerm];.  pUsage
2efed 20 3d 20 28 73 74 72 75 63 74 20 73 71 6c 69 74   = (struct sqlit
2efee 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61  e3_index_constra
2efef 69 6e 74 5f 75 73 61 67 65 2a 29 26 70 49 64 78  int_usage*)&pIdx
2eff0 4f 72 64 65 72 42 79 5b 6e 4f 72 64 65 72 42 79  OrderBy[nOrderBy
2eff1 5d 3b 0a 20 20 2a 28 69 6e 74 2a 29 26 70 49 64  ];.  *(int*)&pId
2eff2 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69  xInfo->nConstrai
2eff3 6e 74 20 3d 20 6e 54 65 72 6d 3b 0a 20 20 2a 28  nt = nTerm;.  *(
2eff4 69 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e  int*)&pIdxInfo->
2eff5 6e 4f 72 64 65 72 42 79 20 3d 20 6e 4f 72 64 65  nOrderBy = nOrde
2eff6 72 42 79 3b 0a 20 20 2a 28 73 74 72 75 63 74 20  rBy;.  *(struct 
2eff7 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
2eff8 6e 73 74 72 61 69 6e 74 2a 2a 29 26 70 49 64 78  nstraint**)&pIdx
2eff9 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
2effa 74 20 3d 20 70 49 64 78 43 6f 6e 73 3b 0a 20 20  t = pIdxCons;.  
2effb 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  *(struct sqlite3
2effc 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 2a 2a  _index_orderby**
2effd 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61 4f 72 64  )&pIdxInfo->aOrd
2effe 65 72 42 79 20 3d 20 70 49 64 78 4f 72 64 65 72  erBy = pIdxOrder
2efff 42 79 3b 0a 20 20 2a 28 73 74 72 75 63 74 20 73  By;.  *(struct s
2f000 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e  qlite3_index_con
2f001 73 74 72 61 69 6e 74 5f 75 73 61 67 65 2a 2a 29  straint_usage**)
2f002 26 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73  &pIdxInfo->aCons
2f003 74 72 61 69 6e 74 55 73 61 67 65 20 3d 0a 20 20  traintUsage =.  
2f004 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f005 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f006 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f007 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f008 20 70 55 73 61 67 65 3b 0a 0a 20 20 66 6f 72 28   pUsage;..  for(
2f009 69 3d 6a 3d 30 2c 20 70 54 65 72 6d 3d 70 57 43  i=j=0, pTerm=pWC
2f00a 2d 3e 61 3b 20 69 3c 70 57 43 2d 3e 6e 54 65 72  ->a; i<pWC->nTer
2f00b 6d 3b 20 69 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29  m; i++, pTerm++)
2f00c 7b 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d  {.    if( pTerm-
2f00d 3e 6c 65 66 74 43 75 72 73 6f 72 20 21 3d 20 70  >leftCursor != p
2f00e 53 72 63 2d 3e 69 43 75 72 73 6f 72 20 29 20 63  Src->iCursor ) c
2f00f 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 61 73 73  ontinue;.    ass
2f010 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 65 4f 70  ert( (pTerm->eOp
2f011 65 72 61 74 6f 72 26 28 70 54 65 72 6d 2d 3e 65  erator&(pTerm->e
2f012 4f 70 65 72 61 74 6f 72 2d 31 29 29 3d 3d 30 20  Operator-1))==0 
2f013 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
2f014 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
2f015 72 3d 3d 57 4f 5f 49 4e 20 29 3b 0a 20 20 20 20  r==WO_IN );.    
2f016 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d  testcase( pTerm-
2f017 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49  >eOperator==WO_I
2f018 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 69 66 28  SNULL );.    if(
2f019 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
2f01a 72 20 26 20 28 57 4f 5f 49 4e 7c 57 4f 5f 49 53  r & (WO_IN|WO_IS
2f01b 4e 55 4c 4c 29 20 29 20 63 6f 6e 74 69 6e 75 65  NULL) ) continue
2f01c 3b 0a 20 20 20 20 70 49 64 78 43 6f 6e 73 5b 6a  ;.    pIdxCons[j
2f01d 5d 2e 69 43 6f 6c 75 6d 6e 20 3d 20 70 54 65 72  ].iColumn = pTer
2f01e 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3b  m->u.leftColumn;
2f01f 0a 20 20 20 20 70 49 64 78 43 6f 6e 73 5b 6a 5d  .    pIdxCons[j]
2f020 2e 69 54 65 72 6d 4f 66 66 73 65 74 20 3d 20 69  .iTermOffset = i
2f021 3b 0a 20 20 20 20 70 49 64 78 43 6f 6e 73 5b 6a  ;.    pIdxCons[j
2f022 5d 2e 6f 70 20 3d 20 28 75 38 29 70 54 65 72 6d  ].op = (u8)pTerm
2f023 2d 3e 65 4f 70 65 72 61 74 6f 72 3b 0a 20 20 20  ->eOperator;.   
2f024 20 2f 2a 20 54 68 65 20 64 69 72 65 63 74 20 61   /* The direct a
2f025 73 73 69 67 6e 6d 65 6e 74 20 69 6e 20 74 68 65  ssignment in the
2f026 20 70 72 65 76 69 6f 75 73 20 6c 69 6e 65 20 69   previous line i
2f027 73 20 70 6f 73 73 69 62 6c 65 20 6f 6e 6c 79 20  s possible only 
2f028 62 65 63 61 75 73 65 0a 20 20 20 20 2a 2a 20 74  because.    ** t
2f029 68 65 20 57 4f 5f 20 61 6e 64 20 53 51 4c 49 54  he WO_ and SQLIT
2f02a 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
2f02b 4e 54 5f 20 63 6f 64 65 73 20 61 72 65 20 69 64  NT_ codes are id
2f02c 65 6e 74 69 63 61 6c 2e 20 20 54 68 65 0a 20 20  entical.  The.  
2f02d 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61    ** following a
2f02e 73 73 65 72 74 73 20 76 65 72 69 66 79 20 74 68  sserts verify th
2f02f 69 73 20 66 61 63 74 2e 20 2a 2f 0a 20 20 20 20  is fact. */.    
2f030 61 73 73 65 72 74 28 20 57 4f 5f 45 51 3d 3d 53  assert( WO_EQ==S
2f031 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
2f032 54 52 41 49 4e 54 5f 45 51 20 29 3b 0a 20 20 20  TRAINT_EQ );.   
2f033 20 61 73 73 65 72 74 28 20 57 4f 5f 4c 54 3d 3d   assert( WO_LT==
2f034 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
2f035 53 54 52 41 49 4e 54 5f 4c 54 20 29 3b 0a 20 20  STRAINT_LT );.  
2f036 20 20 61 73 73 65 72 74 28 20 57 4f 5f 4c 45 3d    assert( WO_LE=
2f037 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f  =SQLITE_INDEX_CO
2f038 4e 53 54 52 41 49 4e 54 5f 4c 45 20 29 3b 0a 20  NSTRAINT_LE );. 
2f039 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f 47 54     assert( WO_GT
2f03a 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  ==SQLITE_INDEX_C
2f03b 4f 4e 53 54 52 41 49 4e 54 5f 47 54 20 29 3b 0a  ONSTRAINT_GT );.
2f03c 20 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f 47      assert( WO_G
2f03d 45 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  E==SQLITE_INDEX_
2f03e 43 4f 4e 53 54 52 41 49 4e 54 5f 47 45 20 29 3b  CONSTRAINT_GE );
2f03f 0a 20 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f  .    assert( WO_
2f040 4d 41 54 43 48 3d 3d 53 51 4c 49 54 45 5f 49 4e  MATCH==SQLITE_IN
2f041 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d  DEX_CONSTRAINT_M
2f042 41 54 43 48 20 29 3b 0a 20 20 20 20 61 73 73 65  ATCH );.    asse
2f043 72 74 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  rt( pTerm->eOper
2f044 61 74 6f 72 20 26 20 28 57 4f 5f 45 51 7c 57 4f  ator & (WO_EQ|WO
2f045 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c  _LT|WO_LE|WO_GT|
2f046 57 4f 5f 47 45 7c 57 4f 5f 4d 41 54 43 48 29 20  WO_GE|WO_MATCH) 
2f047 29 3b 0a 20 20 20 20 6a 2b 2b 3b 0a 20 20 7d 0a  );.    j++;.  }.
2f048 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 72    for(i=0; i<nOr
2f049 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20 20 20  derBy; i++){.   
2f04a 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70   Expr *pExpr = p
2f04b 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 70 45  OrderBy->a[i].pE
2f04c 78 70 72 3b 0a 20 20 20 20 70 49 64 78 4f 72 64  xpr;.    pIdxOrd
2f04d 65 72 42 79 5b 69 5d 2e 69 43 6f 6c 75 6d 6e 20  erBy[i].iColumn 
2f04e 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  = pExpr->iColumn
2f04f 3b 0a 20 20 20 20 70 49 64 78 4f 72 64 65 72 42  ;.    pIdxOrderB
2f050 79 5b 69 5d 2e 64 65 73 63 20 3d 20 70 4f 72 64  y[i].desc = pOrd
2f051 65 72 42 79 2d 3e 61 5b 69 5d 2e 73 6f 72 74 4f  erBy->a[i].sortO
2f052 72 64 65 72 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  rder;.  }..  ret
2f053 75 72 6e 20 70 49 64 78 49 6e 66 6f 3b 0a 7d 0a  urn pIdxInfo;.}.
2f054 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65  ./*.** The table
2f055 20 6f 62 6a 65 63 74 20 72 65 66 65 72 65 6e 63   object referenc
2f056 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
2f057 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
2f058 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
2f059 0a 2a 2a 20 6d 75 73 74 20 72 65 70 72 65 73 65  .** must represe
2f05a 6e 74 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  nt a virtual tab
2f05b 6c 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  le. This functio
2f05c 6e 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 78 42  n invokes the xB
2f05d 65 73 74 49 6e 64 65 78 28 29 0a 2a 2a 20 6d 65  estIndex().** me
2f05e 74 68 6f 64 20 6f 66 20 74 68 65 20 76 69 72 74  thod of the virt
2f05f 75 61 6c 20 74 61 62 6c 65 20 77 69 74 68 20 74  ual table with t
2f060 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  he sqlite3_index
2f061 5f 69 6e 66 6f 20 70 6f 69 6e 74 65 72 20 70 61  _info pointer pa
2f062 73 73 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 61  ssed.** as the a
2f063 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49  rgument..**.** I
2f064 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
2f065 73 2c 20 70 50 61 72 73 65 20 69 73 20 70 6f 70  s, pParse is pop
2f066 75 6c 61 74 65 64 20 77 69 74 68 20 61 6e 20 65  ulated with an e
2f067 72 72 6f 72 20 6d 65 73 73 61 67 65 20 61 6e 64  rror message and
2f068 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 76   a.** non-zero v
2f069 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64  alue is returned
2f06a 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 30 20 69  . Otherwise, 0 i
2f06b 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74  s returned and t
2f06c 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72  he output.** par
2f06d 74 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  t of the sqlite3
2f06e 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75  _index_info stru
2f06f 63 74 75 72 65 20 69 73 20 6c 65 66 74 20 70 6f  cture is left po
2f070 70 75 6c 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 57  pulated..**.** W
2f071 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 6e  hether or not an
2f072 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e   error is return
2f073 65 64 2c 20 69 74 20 69 73 20 74 68 65 20 72 65  ed, it is the re
2f074 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20  sponsibility of 
2f075 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 20 74 6f  the.** caller to
2f076 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65   eventually free
2f077 20 70 2d 3e 69 64 78 53 74 72 20 69 66 20 70 2d   p->idxStr if p-
2f078 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74  >needToFreeIdxSt
2f079 72 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 74  r indicates.** t
2f07a 68 61 74 20 74 68 69 73 20 69 73 20 72 65 71 75  hat this is requ
2f07b 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ired..*/.static 
2f07c 69 6e 74 20 76 74 61 62 42 65 73 74 49 6e 64 65  int vtabBestInde
2f07d 78 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  x(Parse *pParse,
2f07e 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 73 71   Table *pTab, sq
2f07f 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
2f080 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f   *p){.  sqlite3_
2f081 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20 73 71  vtab *pVtab = sq
2f082 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 70  lite3GetVTable(p
2f083 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 29  Parse->db, pTab)
2f084 2d 3e 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 69  ->pVtab;.  int i
2f085 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 28  ;.  int rc;..  (
2f086 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
2f087 74 79 4f 66 66 28 70 50 61 72 73 65 2d 3e 64 62  tyOff(pParse->db
2f088 29 3b 0a 20 20 57 48 45 52 45 54 52 41 43 45 28  );.  WHERETRACE(
2f089 28 22 78 42 65 73 74 49 6e 64 65 78 20 66 6f 72  ("xBestIndex for
2f08a 20 25 73 5c 6e 22 2c 20 70 54 61 62 2d 3e 7a 4e   %s\n", pTab->zN
2f08b 61 6d 65 29 29 3b 0a 20 20 54 52 41 43 45 5f 49  ame));.  TRACE_I
2f08c 44 58 5f 49 4e 50 55 54 53 28 70 29 3b 0a 20 20  DX_INPUTS(p);.  
2f08d 72 63 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64  rc = pVtab->pMod
2f08e 75 6c 65 2d 3e 78 42 65 73 74 49 6e 64 65 78 28  ule->xBestIndex(
2f08f 70 56 74 61 62 2c 20 70 29 3b 0a 20 20 54 52 41  pVtab, p);.  TRA
2f090 43 45 5f 49 44 58 5f 4f 55 54 50 55 54 53 28 70  CE_IDX_OUTPUTS(p
2f091 29 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74  );.  (void)sqlit
2f092 65 33 53 61 66 65 74 79 4f 6e 28 70 50 61 72 73  e3SafetyOn(pPars
2f093 65 2d 3e 64 62 29 3b 0a 0a 20 20 69 66 28 20 72  e->db);..  if( r
2f094 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
2f095 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
2f096 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20  TE_NOMEM ){.    
2f097 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61    pParse->db->ma
2f098 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
2f099 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70      }else if( !p
2f09a 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 29 7b  Vtab->zErrMsg ){
2f09b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
2f09c 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2f09d 25 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72 53  %s", sqlite3ErrS
2f09e 74 72 28 72 63 29 29 3b 0a 20 20 20 20 7d 65 6c  tr(rc));.    }el
2f09f 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
2f0a0 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2f0a1 2c 20 22 25 73 22 2c 20 70 56 74 61 62 2d 3e 7a  , "%s", pVtab->z
2f0a2 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 7d 0a 20  ErrMsg);.    }. 
2f0a3 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72   }.  sqlite3DbFr
2f0a4 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  ee(pParse->db, p
2f0a5 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a  Vtab->zErrMsg);.
2f0a6 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67    pVtab->zErrMsg
2f0a7 20 3d 20 30 3b 0a 0a 20 20 66 6f 72 28 69 3d 30   = 0;..  for(i=0
2f0a8 3b 20 69 3c 70 2d 3e 6e 43 6f 6e 73 74 72 61 69  ; i<p->nConstrai
2f0a9 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  nt; i++){.    if
2f0aa 28 20 21 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  ( !p->aConstrain
2f0ab 74 5b 69 5d 2e 75 73 61 62 6c 65 20 26 26 20 70  t[i].usable && p
2f0ac 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  ->aConstraintUsa
2f0ad 67 65 5b 69 5d 2e 61 72 67 76 49 6e 64 65 78 3e  ge[i].argvIndex>
2f0ae 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
2f0af 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2f0b0 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 74  e, .          "t
2f0b1 61 62 6c 65 20 25 73 3a 20 78 42 65 73 74 49 6e  able %s: xBestIn
2f0b2 64 65 78 20 72 65 74 75 72 6e 65 64 20 61 6e 20  dex returned an 
2f0b3 69 6e 76 61 6c 69 64 20 70 6c 61 6e 22 2c 20 70  invalid plan", p
2f0b4 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Tab->zName);.   
2f0b5 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
2f0b6 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3b 0a 7d   pParse->nErr;.}
2f0b7 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65  .../*.** Compute
2f0b8 20 74 68 65 20 62 65 73 74 20 69 6e 64 65 78 20   the best index 
2f0b9 66 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74 61  for a virtual ta
2f0ba 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62  ble..**.** The b
2f0bb 65 73 74 20 69 6e 64 65 78 20 69 73 20 63 6f 6d  est index is com
2f0bc 70 75 74 65 64 20 62 79 20 74 68 65 20 78 42 65  puted by the xBe
2f0bd 73 74 49 6e 64 65 78 20 6d 65 74 68 6f 64 20 6f  stIndex method o
2f0be 66 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a  f the virtual.**
2f0bf 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 20   table module.  
2f0c0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
2f0c1 72 65 61 6c 6c 79 20 6a 75 73 74 20 61 20 77 72  really just a wr
2f0c2 61 70 70 65 72 20 74 68 61 74 20 73 65 74 73 20  apper that sets 
2f0c3 75 70 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65  up.** the sqlite
2f0c4 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72  3_index_info str
2f0c5 75 63 74 75 72 65 20 74 68 61 74 20 69 73 20 75  ucture that is u
2f0c6 73 65 64 20 74 6f 20 63 6f 6d 6d 75 6e 69 63 61  sed to communica
2f0c7 74 65 20 77 69 74 68 0a 2a 2a 20 78 42 65 73 74  te with.** xBest
2f0c8 49 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  Index..**.** In 
2f0c9 61 20 6a 6f 69 6e 2c 20 74 68 69 73 20 72 6f 75  a join, this rou
2f0ca 74 69 6e 65 20 6d 69 67 68 74 20 62 65 20 63 61  tine might be ca
2f0cb 6c 6c 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69  lled multiple ti
2f0cc 6d 65 73 20 66 6f 72 20 74 68 65 0a 2a 2a 20 73  mes for the.** s
2f0cd 61 6d 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ame virtual tabl
2f0ce 65 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f  e.  The sqlite3_
2f0cf 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63  index_info struc
2f0d0 74 75 72 65 20 69 73 20 63 72 65 61 74 65 64 0a  ture is created.
2f0d1 2a 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a  ** and initializ
2f0d2 65 64 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20  ed on the first 
2f0d3 69 6e 76 6f 63 61 74 69 6f 6e 20 61 6e 64 20 72  invocation and r
2f0d4 65 75 73 65 64 20 6f 6e 20 61 6c 6c 20 73 75 62  eused on all sub
2f0d5 73 65 71 75 65 6e 74 0a 2a 2a 20 69 6e 76 6f 63  sequent.** invoc
2f0d6 61 74 69 6f 6e 73 2e 20 20 54 68 65 20 73 71 6c  ations.  The sql
2f0d7 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
2f0d8 73 74 72 75 63 74 75 72 65 20 69 73 20 61 6c 73  structure is als
2f0d9 6f 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a 20 63  o used when.** c
2f0da 6f 64 65 20 69 73 20 67 65 6e 65 72 61 74 65 64  ode is generated
2f0db 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 76   to access the v
2f0dc 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54  irtual table.  T
2f0dd 68 65 20 77 68 65 72 65 49 6e 66 6f 44 65 6c 65  he whereInfoDele
2f0de 74 65 28 29 20 0a 2a 2a 20 72 6f 75 74 69 6e 65  te() .** routine
2f0df 20 74 61 6b 65 73 20 63 61 72 65 20 6f 66 20 66   takes care of f
2f0e0 72 65 65 69 6e 67 20 74 68 65 20 73 71 6c 69 74  reeing the sqlit
2f0e1 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74  e3_index_info st
2f0e2 72 75 63 74 75 72 65 20 61 66 74 65 72 0a 2a 2a  ructure after.**
2f0e3 20 65 76 65 72 79 62 6f 64 79 20 68 61 73 20 66   everybody has f
2f0e4 69 6e 69 73 68 65 64 20 77 69 74 68 20 69 74 2e  inished with it.
2f0e5 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2f0e6 62 65 73 74 56 69 72 74 75 61 6c 49 6e 64 65 78  bestVirtualIndex
2f0e7 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2f0e8 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
2f0e9 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69      /* The parsi
2f0ea 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2f0eb 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43  WhereClause *pWC
2f0ec 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2f0ed 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
2f0ee 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  use */.  struct 
2f0ef 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53  SrcList_item *pS
2f0f0 72 63 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  rc,      /* The 
2f0f1 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d  FROM clause term
2f0f2 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a 20 20   to search */.  
2f0f3 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79  Bitmask notReady
2f0f4 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2f0f5 2f 2a 20 4d 61 73 6b 20 6f 66 20 63 75 72 73 6f  /* Mask of curso
2f0f6 72 73 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20  rs that are not 
2f0f7 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 45  available */.  E
2f0f8 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
2f0f9 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  y,             /
2f0fa 2a 20 54 68 65 20 6f 72 64 65 72 20 62 79 20 63  * The order by c
2f0fb 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65  lause */.  Where
2f0fc 43 6f 73 74 20 2a 70 43 6f 73 74 2c 20 20 20 20  Cost *pCost,    
2f0fd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
2f0fe 77 65 73 74 20 63 6f 73 74 20 71 75 65 72 79 20  west cost query 
2f0ff 70 6c 61 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  plan */.  sqlite
2f100 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 2a 70  3_index_info **p
2f101 70 49 64 78 49 6e 66 6f 20 20 2f 2a 20 49 6e 64  pIdxInfo  /* Ind
2f102 65 78 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 70  ex information p
2f103 61 73 73 65 64 20 74 6f 20 78 42 65 73 74 49 6e  assed to xBestIn
2f104 64 65 78 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c  dex */.){.  Tabl
2f105 65 20 2a 70 54 61 62 20 3d 20 70 53 72 63 2d 3e  e *pTab = pSrc->
2f106 70 54 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f  pTab;.  sqlite3_
2f107 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 64 78  index_info *pIdx
2f108 49 6e 66 6f 3b 0a 20 20 73 74 72 75 63 74 20 73  Info;.  struct s
2f109 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e  qlite3_index_con
2f10a 73 74 72 61 69 6e 74 20 2a 70 49 64 78 43 6f 6e  straint *pIdxCon
2f10b 73 3b 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69  s;.  struct sqli
2f10c 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72  te3_index_constr
2f10d 61 69 6e 74 5f 75 73 61 67 65 20 2a 70 55 73 61  aint_usage *pUsa
2f10e 67 65 3b 0a 20 20 57 68 65 72 65 54 65 72 6d 20  ge;.  WhereTerm 
2f10f 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74 20 69 2c  *pTerm;.  int i,
2f110 20 6a 3b 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72   j;.  int nOrder
2f111 42 79 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73  By;..  /* Make s
2f112 75 72 65 20 77 73 46 6c 61 67 73 20 69 73 20 69  ure wsFlags is i
2f113 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 73 6f  nitialized to so
2f114 6d 65 20 73 61 6e 65 20 76 61 6c 75 65 2e 20 4f  me sane value. O
2f115 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65  therwise, if the
2f116 20 0a 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20 69 6e   .  ** malloc in
2f117 20 61 6c 6c 6f 63 61 74 65 49 6e 64 65 78 49 6e   allocateIndexIn
2f118 66 6f 28 29 20 66 61 69 6c 73 20 61 6e 64 20 74  fo() fails and t
2f119 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
2f11a 75 72 6e 73 20 6c 65 61 76 69 6e 67 0a 20 20 2a  urns leaving.  *
2f11b 2a 20 77 73 46 6c 61 67 73 20 69 6e 20 61 6e 20  * wsFlags in an 
2f11c 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 74  uninitialized st
2f11d 61 74 65 2c 20 74 68 65 20 63 61 6c 6c 65 72 20  ate, the caller 
2f11e 6d 61 79 20 62 65 68 61 76 65 20 75 6e 70 72 65  may behave unpre
2f11f 64 69 63 74 61 62 6c 79 2e 0a 20 20 2a 2f 0a 20  dictably..  */. 
2f120 20 6d 65 6d 73 65 74 28 70 43 6f 73 74 2c 20 30   memset(pCost, 0
2f121 2c 20 73 69 7a 65 6f 66 28 2a 70 43 6f 73 74 29  , sizeof(*pCost)
2f122 29 3b 0a 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e  );.  pCost->plan
2f123 2e 77 73 46 6c 61 67 73 20 3d 20 57 48 45 52 45  .wsFlags = WHERE
2f124 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 3b 0a 0a  _VIRTUALTABLE;..
2f125 20 20 2f 2a 20 49 66 20 74 68 65 20 73 71 6c 69    /* If the sqli
2f126 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73  te3_index_info s
2f127 74 72 75 63 74 75 72 65 20 68 61 73 20 6e 6f 74  tructure has not
2f128 20 62 65 65 6e 20 70 72 65 76 69 6f 75 73 6c 79   been previously
2f129 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  .  ** allocated 
2f12a 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c  and initialized,
2f12b 20 74 68 65 6e 20 61 6c 6c 6f 63 61 74 65 20 61   then allocate a
2f12c 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 74  nd initialize it
2f12d 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 70 49 64   now..  */.  pId
2f12e 78 49 6e 66 6f 20 3d 20 2a 70 70 49 64 78 49 6e  xInfo = *ppIdxIn
2f12f 66 6f 3b 0a 20 20 69 66 28 20 70 49 64 78 49 6e  fo;.  if( pIdxIn
2f130 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 70  fo==0 ){.    *pp
2f131 49 64 78 49 6e 66 6f 20 3d 20 70 49 64 78 49 6e  IdxInfo = pIdxIn
2f132 66 6f 20 3d 20 61 6c 6c 6f 63 61 74 65 49 6e 64  fo = allocateInd
2f133 65 78 49 6e 66 6f 28 70 50 61 72 73 65 2c 20 70  exInfo(pParse, p
2f134 57 43 2c 20 70 53 72 63 2c 20 70 4f 72 64 65 72  WC, pSrc, pOrder
2f135 42 79 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  By);.  }.  if( p
2f136 49 64 78 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20  IdxInfo==0 ){.  
2f137 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
2f138 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e   /* At this poin
2f139 74 2c 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  t, the sqlite3_i
2f13a 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74  ndex_info struct
2f13b 75 72 65 20 74 68 61 74 20 70 49 64 78 49 6e 66  ure that pIdxInf
2f13c 6f 20 70 6f 69 6e 74 73 0a 20 20 2a 2a 20 74 6f  o points.  ** to
2f13d 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20   will have been 
2f13e 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 65 69 74  initialized, eit
2f13f 68 65 72 20 64 75 72 69 6e 67 20 74 68 65 20 63  her during the c
2f140 75 72 72 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f  urrent invocatio
2f141 6e 20 6f 72 0a 20 20 2a 2a 20 64 75 72 69 6e 67  n or.  ** during
2f142 20 73 6f 6d 65 20 70 72 69 6f 72 20 69 6e 76 6f   some prior invo
2f143 63 61 74 69 6f 6e 2e 20 20 4e 6f 77 20 77 65 20  cation.  Now we 
2f144 6a 75 73 74 20 68 61 76 65 20 74 6f 20 63 75 73  just have to cus
2f145 74 6f 6d 69 7a 65 20 74 68 65 0a 20 20 2a 2a 20  tomize the.  ** 
2f146 64 65 74 61 69 6c 73 20 6f 66 20 70 49 64 78 49  details of pIdxI
2f147 6e 66 6f 20 66 6f 72 20 74 68 65 20 63 75 72 72  nfo for the curr
2f148 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 20 61  ent invocation a
2f149 6e 64 20 70 61 73 73 20 69 74 20 74 6f 0a 20 20  nd pass it to.  
2f14a 2a 2a 20 78 42 65 73 74 49 6e 64 65 78 2e 0a 20  ** xBestIndex.. 
2f14b 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 6f   */..  /* The mo
2f14c 64 75 6c 65 20 6e 61 6d 65 20 6d 75 73 74 20 62  dule name must b
2f14d 65 20 64 65 66 69 6e 65 64 2e 20 41 6c 73 6f 2c  e defined. Also,
2f14e 20 62 79 20 74 68 69 73 20 70 6f 69 6e 74 20 74   by this point t
2f14f 68 65 72 65 20 6d 75 73 74 0a 20 20 2a 2a 20 62  here must.  ** b
2f150 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  e a pointer to a
2f151 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73  n sqlite3_vtab s
2f152 74 72 75 63 74 75 72 65 2e 20 4f 74 68 65 72 77  tructure. Otherw
2f153 69 73 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  ise.  ** sqlite3
2f154 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d  ViewGetColumnNam
2f155 65 73 28 29 20 77 6f 75 6c 64 20 68 61 76 65 20  es() would have 
2f156 70 69 63 6b 65 64 20 75 70 20 74 68 65 20 65 72  picked up the er
2f157 72 6f 72 2e 20 0a 20 20 2a 2f 0a 20 20 61 73 73  ror. .  */.  ass
2f158 65 72 74 28 20 70 54 61 62 2d 3e 61 7a 4d 6f 64  ert( pTab->azMod
2f159 75 6c 65 41 72 67 20 26 26 20 70 54 61 62 2d 3e  uleArg && pTab->
2f15a 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 20 29  azModuleArg[0] )
2f15b 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
2f15c 74 65 33 47 65 74 56 54 61 62 6c 65 28 70 50 61  te3GetVTable(pPa
2f15d 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 29 20 29  rse->db, pTab) )
2f15e 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20  ;..  /* Set the 
2f15f 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 75 73  aConstraint[].us
2f160 61 62 6c 65 20 66 69 65 6c 64 73 20 61 6e 64 20  able fields and 
2f161 69 6e 69 74 69 61 6c 69 7a 65 20 61 6c 6c 20 0a  initialize all .
2f162 20 20 2a 2a 20 6f 75 74 70 75 74 20 76 61 72 69    ** output vari
2f163 61 62 6c 65 73 20 74 6f 20 7a 65 72 6f 2e 0a 20  ables to zero.. 
2f164 20 2a 2a 0a 20 20 2a 2a 20 61 43 6f 6e 73 74 72   **.  ** aConstr
2f165 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20 69 73  aint[].usable is
2f166 20 74 72 75 65 20 66 6f 72 20 63 6f 6e 73 74 72   true for constr
2f167 61 69 6e 74 73 20 77 68 65 72 65 20 74 68 65 20  aints where the 
2f168 72 69 67 68 74 2d 68 61 6e 64 0a 20 20 2a 2a 20  right-hand.  ** 
2f169 73 69 64 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e  side contains on
2f16a 6c 79 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  ly references to
2f16b 20 74 61 62 6c 65 73 20 74 6f 20 74 68 65 20 6c   tables to the l
2f16c 65 66 74 20 6f 66 20 74 68 65 20 63 75 72 72 65  eft of the curre
2f16d 6e 74 0a 20 20 2a 2a 20 74 61 62 6c 65 2e 20 20  nt.  ** table.  
2f16e 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
2f16f 69 66 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  if the constrain
2f170 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  t is of the form
2f171 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
2f172 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20 3d 20 65        column = e
2f173 78 70 72 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 6e  xpr.  **.  ** an
2f174 64 20 77 65 20 61 72 65 20 65 76 61 6c 75 61 74  d we are evaluat
2f175 69 6e 67 20 61 20 6a 6f 69 6e 2c 20 74 68 65 6e  ing a join, then
2f176 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20   the constraint 
2f177 6f 6e 20 63 6f 6c 75 6d 6e 20 69 73 20 0a 20 20  on column is .  
2f178 2a 2a 20 6f 6e 6c 79 20 76 61 6c 69 64 20 69 66  ** only valid if
2f179 20 61 6c 6c 20 74 61 62 6c 65 73 20 72 65 66 65   all tables refe
2f17a 72 65 6e 63 65 64 20 69 6e 20 65 78 70 72 20 6f  renced in expr o
2f17b 63 63 75 72 20 74 6f 20 74 68 65 20 6c 65 66 74  ccur to the left
2f17c 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 62  .  ** of the tab
2f17d 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 63 6f  le containing co
2f17e 6c 75 6d 6e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  lumn..  **.  ** 
2f17f 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 73  The aConstraints
2f180 5b 5d 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e  [] array contain
2f181 73 20 65 6e 74 72 69 65 73 20 66 6f 72 20 61 6c  s entries for al
2f182 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20  l constraints.  
2f183 2a 2a 20 6f 6e 20 74 68 65 20 63 75 72 72 65 6e  ** on the curren
2f184 74 20 74 61 62 6c 65 2e 20 20 54 68 61 74 20 77  t table.  That w
2f185 61 79 20 77 65 20 6f 6e 6c 79 20 68 61 76 65 20  ay we only have 
2f186 74 6f 20 63 6f 6d 70 75 74 65 20 69 74 20 6f 6e  to compute it on
2f187 63 65 0a 20 20 2a 2a 20 65 76 65 6e 20 74 68 6f  ce.  ** even tho
2f188 75 67 68 20 77 65 20 6d 69 67 68 74 20 74 72 79  ugh we might try
2f189 20 74 6f 20 70 69 63 6b 20 74 68 65 20 62 65 73   to pick the bes
2f18a 74 20 69 6e 64 65 78 20 6d 75 6c 74 69 70 6c 65  t index multiple
2f18b 20 74 69 6d 65 73 2e 0a 20 20 2a 2a 20 46 6f 72   times..  ** For
2f18c 20 65 61 63 68 20 61 74 74 65 6d 70 74 20 61 74   each attempt at
2f18d 20 70 69 63 6b 69 6e 67 20 61 6e 20 69 6e 64 65   picking an inde
2f18e 78 2c 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20  x, the order of 
2f18f 74 61 62 6c 65 73 20 69 6e 20 74 68 65 0a 20 20  tables in the.  
2f190 2a 2a 20 6a 6f 69 6e 20 6d 69 67 68 74 20 62 65  ** join might be
2f191 20 64 69 66 66 65 72 65 6e 74 20 73 6f 20 77 65   different so we
2f192 20 68 61 76 65 20 74 6f 20 72 65 63 6f 6d 70 75   have to recompu
2f193 74 65 20 74 68 65 20 75 73 61 62 6c 65 20 66 6c  te the usable fl
2f194 61 67 0a 20 20 2a 2a 20 65 61 63 68 20 74 69 6d  ag.  ** each tim
2f195 65 2e 0a 20 20 2a 2f 0a 20 20 70 49 64 78 43 6f  e..  */.  pIdxCo
2f196 6e 73 20 3d 20 2a 28 73 74 72 75 63 74 20 73 71  ns = *(struct sq
2f197 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73  lite3_index_cons
2f198 74 72 61 69 6e 74 2a 2a 29 26 70 49 64 78 49 6e  traint**)&pIdxIn
2f199 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 3b  fo->aConstraint;
2f19a 0a 20 20 70 55 73 61 67 65 20 3d 20 70 49 64 78  .  pUsage = pIdx
2f19b 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
2f19c 74 55 73 61 67 65 3b 0a 20 20 66 6f 72 28 69 3d  tUsage;.  for(i=
2f19d 30 3b 20 69 3c 70 49 64 78 49 6e 66 6f 2d 3e 6e  0; i<pIdxInfo->n
2f19e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b 2c  Constraint; i++,
2f19f 20 70 49 64 78 43 6f 6e 73 2b 2b 29 7b 0a 20 20   pIdxCons++){.  
2f1a0 20 20 6a 20 3d 20 70 49 64 78 43 6f 6e 73 2d 3e    j = pIdxCons->
2f1a1 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a 20 20 20  iTermOffset;.   
2f1a2 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61   pTerm = &pWC->a
2f1a3 5b 6a 5d 3b 0a 20 20 20 20 70 49 64 78 43 6f 6e  [j];.    pIdxCon
2f1a4 73 2d 3e 75 73 61 62 6c 65 20 3d 20 28 70 54 65  s->usable = (pTe
2f1a5 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 26  rm->prereqRight&
2f1a6 6e 6f 74 52 65 61 64 79 29 20 3f 20 30 20 3a 20  notReady) ? 0 : 
2f1a7 31 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28  1;.  }.  memset(
2f1a8 70 55 73 61 67 65 2c 20 30 2c 20 73 69 7a 65 6f  pUsage, 0, sizeo
2f1a9 66 28 70 55 73 61 67 65 5b 30 5d 29 2a 70 49 64  f(pUsage[0])*pId
2f1aa 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69  xInfo->nConstrai
2f1ab 6e 74 29 3b 0a 20 20 69 66 28 20 70 49 64 78 49  nt);.  if( pIdxI
2f1ac 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49  nfo->needToFreeI
2f1ad 64 78 53 74 72 20 29 7b 0a 20 20 20 20 73 71 6c  dxStr ){.    sql
2f1ae 69 74 65 33 5f 66 72 65 65 28 70 49 64 78 49 6e  ite3_free(pIdxIn
2f1af 66 6f 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20 7d  fo->idxStr);.  }
2f1b0 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78  .  pIdxInfo->idx
2f1b1 53 74 72 20 3d 20 30 3b 0a 20 20 70 49 64 78 49  Str = 0;.  pIdxI
2f1b2 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 30 3b  nfo->idxNum = 0;
2f1b3 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 65 65  .  pIdxInfo->nee
2f1b4 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 3d 20  dToFreeIdxStr = 
2f1b5 30 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6f  0;.  pIdxInfo->o
2f1b6 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 20 3d  rderByConsumed =
2f1b7 20 30 3b 0a 20 20 2f 2a 20 28 28 64 6f 75 62 6c   0;.  /* ((doubl
2f1b8 65 29 32 29 20 49 6e 20 63 61 73 65 20 6f 66 20  e)2) In case of 
2f1b9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
2f1ba 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f  TING_POINT... */
2f1bb 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74  .  pIdxInfo->est
2f1bc 69 6d 61 74 65 64 43 6f 73 74 20 3d 20 53 51 4c  imatedCost = SQL
2f1bd 49 54 45 5f 42 49 47 5f 44 42 4c 20 2f 20 28 28  ITE_BIG_DBL / ((
2f1be 64 6f 75 62 6c 65 29 32 29 3b 0a 20 20 6e 4f 72  double)2);.  nOr
2f1bf 64 65 72 42 79 20 3d 20 70 49 64 78 49 6e 66 6f  derBy = pIdxInfo
2f1c0 2d 3e 6e 4f 72 64 65 72 42 79 3b 0a 20 20 69 66  ->nOrderBy;.  if
2f1c1 28 20 21 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  ( !pOrderBy ){. 
2f1c2 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72     pIdxInfo->nOr
2f1c3 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 7d 0a 0a  derBy = 0;.  }..
2f1c4 20 20 69 66 28 20 76 74 61 62 42 65 73 74 49 6e    if( vtabBestIn
2f1c5 64 65 78 28 70 50 61 72 73 65 2c 20 70 54 61 62  dex(pParse, pTab
2f1c6 2c 20 70 49 64 78 49 6e 66 6f 29 20 29 7b 0a 20  , pIdxInfo) ){. 
2f1c7 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
2f1c8 20 20 70 49 64 78 43 6f 6e 73 20 3d 20 2a 28 73    pIdxCons = *(s
2f1c9 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
2f1ca 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 2a  dex_constraint**
2f1cb 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e  )&pIdxInfo->aCon
2f1cc 73 74 72 61 69 6e 74 3b 0a 20 20 66 6f 72 28 69  straint;.  for(i
2f1cd 3d 30 3b 20 69 3c 70 49 64 78 49 6e 66 6f 2d 3e  =0; i<pIdxInfo->
2f1ce 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b  nConstraint; i++
2f1cf 29 7b 0a 20 20 20 20 69 66 28 20 70 55 73 61 67  ){.    if( pUsag
2f1d0 65 5b 69 5d 2e 61 72 67 76 49 6e 64 65 78 3e 30  e[i].argvIndex>0
2f1d1 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 73 74 2d   ){.      pCost-
2f1d2 3e 75 73 65 64 20 7c 3d 20 70 57 43 2d 3e 61 5b  >used |= pWC->a[
2f1d3 70 49 64 78 43 6f 6e 73 5b 69 5d 2e 69 54 65 72  pIdxCons[i].iTer
2f1d4 6d 4f 66 66 73 65 74 5d 2e 70 72 65 72 65 71 52  mOffset].prereqR
2f1d5 69 67 68 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ight;.    }.  }.
2f1d6 0a 20 20 2f 2a 20 54 68 65 20 63 6f 73 74 20 69  .  /* The cost i
2f1d7 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f  s not allowed to
2f1d8 20 62 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20   be larger than 
2f1d9 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 20 28  SQLITE_BIG_DBL (
2f1da 74 68 65 0a 20 20 2a 2a 20 69 6e 69 74 61 6c 20  the.  ** inital 
2f1db 76 61 6c 75 65 20 6f 66 20 6c 6f 77 65 73 74 43  value of lowestC
2f1dc 6f 73 74 20 69 6e 20 74 68 69 73 20 6c 6f 6f 70  ost in this loop
2f1dd 2e 20 49 66 20 69 74 20 69 73 2c 20 74 68 65 6e  . If it is, then
2f1de 20 74 68 65 0a 20 20 2a 2a 20 28 63 6f 73 74 3c   the.  ** (cost<
2f1df 6c 6f 77 65 73 74 43 6f 73 74 29 20 74 65 73 74  lowestCost) test
2f1e0 20 62 65 6c 6f 77 20 77 69 6c 6c 20 6e 65 76 65   below will neve
2f1e1 72 20 62 65 20 74 72 75 65 2e 0a 20 20 2a 2a 20  r be true..  ** 
2f1e2 0a 20 20 2a 2a 20 55 73 65 20 22 28 64 6f 75 62  .  ** Use "(doub
2f1e3 6c 65 29 32 22 20 69 6e 73 74 65 61 64 20 6f 66  le)2" instead of
2f1e4 20 22 32 2e 30 22 20 69 6e 20 63 61 73 65 20 4f   "2.0" in case O
2f1e5 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
2f1e6 4e 54 20 0a 20 20 2a 2a 20 69 73 20 64 65 66 69  NT .  ** is defi
2f1e7 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ned..  */.  if( 
2f1e8 28 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 2f  (SQLITE_BIG_DBL/
2f1e9 28 28 64 6f 75 62 6c 65 29 32 29 29 3c 70 49 64  ((double)2))<pId
2f1ea 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64  xInfo->estimated
2f1eb 43 6f 73 74 20 29 7b 0a 20 20 20 20 70 43 6f 73  Cost ){.    pCos
2f1ec 74 2d 3e 72 43 6f 73 74 20 3d 20 28 53 51 4c 49  t->rCost = (SQLI
2f1ed 54 45 5f 42 49 47 5f 44 42 4c 2f 28 28 64 6f 75  TE_BIG_DBL/((dou
2f1ee 62 6c 65 29 32 29 29 3b 0a 20 20 7d 65 6c 73 65  ble)2));.  }else
2f1ef 7b 0a 20 20 20 20 70 43 6f 73 74 2d 3e 72 43 6f  {.    pCost->rCo
2f1f0 73 74 20 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 65  st = pIdxInfo->e
2f1f1 73 74 69 6d 61 74 65 64 43 6f 73 74 3b 0a 20 20  stimatedCost;.  
2f1f2 7d 0a 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e  }.  pCost->plan.
2f1f3 75 2e 70 56 74 61 62 49 64 78 20 3d 20 70 49 64  u.pVtabIdx = pId
2f1f4 78 49 6e 66 6f 3b 0a 20 20 69 66 28 20 70 49 64  xInfo;.  if( pId
2f1f5 78 49 6e 66 6f 2d 3e 6f 72 64 65 72 42 79 43 6f  xInfo->orderByCo
2f1f6 6e 73 75 6d 65 64 20 29 7b 0a 20 20 20 20 70 43  nsumed ){.    pC
2f1f7 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  ost->plan.wsFlag
2f1f8 73 20 7c 3d 20 57 48 45 52 45 5f 4f 52 44 45 52  s |= WHERE_ORDER
2f1f9 42 59 3b 0a 20 20 7d 0a 20 20 70 43 6f 73 74 2d  BY;.  }.  pCost-
2f1fa 3e 70 6c 61 6e 2e 6e 45 71 20 3d 20 30 3b 0a 20  >plan.nEq = 0;. 
2f1fb 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65   pIdxInfo->nOrde
2f1fc 72 42 79 20 3d 20 6e 4f 72 64 65 72 42 79 3b 0a  rBy = nOrderBy;.
2f1fd 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e  .  /* Try to fin
2f1fe 64 20 61 20 6d 6f 72 65 20 65 66 66 69 63 69 65  d a more efficie
2f1ff 6e 74 20 61 63 63 65 73 73 20 70 61 74 74 65 72  nt access patter
2f200 6e 20 62 79 20 75 73 69 6e 67 20 6d 75 6c 74 69  n by using multi
2f201 70 6c 65 20 69 6e 64 65 78 65 73 0a 20 20 2a 2a  ple indexes.  **
2f202 20 74 6f 20 6f 70 74 69 6d 69 7a 65 20 61 6e 20   to optimize an 
2f203 4f 52 20 65 78 70 72 65 73 73 69 6f 6e 20 77 69  OR expression wi
2f204 74 68 69 6e 20 74 68 65 20 57 48 45 52 45 20 63  thin the WHERE c
2f205 6c 61 75 73 65 2e 20 0a 20 20 2a 2f 0a 20 20 62  lause. .  */.  b
2f206 65 73 74 4f 72 43 6c 61 75 73 65 49 6e 64 65 78  estOrClauseIndex
2f207 28 70 50 61 72 73 65 2c 20 70 57 43 2c 20 70 53  (pParse, pWC, pS
2f208 72 63 2c 20 6e 6f 74 52 65 61 64 79 2c 20 70 4f  rc, notReady, pO
2f209 72 64 65 72 42 79 2c 20 70 43 6f 73 74 29 3b 0a  rderBy, pCost);.
2f20a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
2f20b 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2f20c 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  ABLE */../*.** A
2f20d 72 67 75 6d 65 6e 74 20 70 49 64 78 20 69 73 20  rgument pIdx is 
2f20e 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
2f20f 69 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20  index structure 
2f210 74 68 61 74 20 68 61 73 20 61 6e 20 61 72 72 61  that has an arra
2f211 79 20 6f 66 0a 2a 2a 20 53 51 4c 49 54 45 5f 49  y of.** SQLITE_I
2f212 4e 44 45 58 5f 53 41 4d 50 4c 45 53 20 65 76 65  NDEX_SAMPLES eve
2f213 6e 6c 79 20 73 70 61 63 65 64 20 73 61 6d 70 6c  nly spaced sampl
2f214 65 73 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  es of the first 
2f215 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 0a 2a  indexed column.*
2f216 2a 20 73 74 6f 72 65 64 20 69 6e 20 49 6e 64 65  * stored in Inde
2f217 78 2e 61 53 61 6d 70 6c 65 2e 20 54 68 65 20 64  x.aSample. The d
2f218 6f 6d 61 69 6e 20 6f 66 20 76 61 6c 75 65 73 20  omain of values 
2f219 73 74 6f 72 65 64 20 69 6e 20 73 61 69 64 20 63  stored in said c
2f21a 6f 6c 75 6d 6e 0a 2a 2a 20 6d 61 79 20 62 65 20  olumn.** may be 
2f21b 74 68 6f 75 67 68 74 20 6f 66 20 61 73 20 64 69  thought of as di
2f21c 76 69 64 65 64 20 69 6e 74 6f 20 28 53 51 4c 49  vided into (SQLI
2f21d 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53  TE_INDEX_SAMPLES
2f21e 2b 31 29 20 72 65 67 69 6f 6e 73 2e 0a 2a 2a 20  +1) regions..** 
2f21f 52 65 67 69 6f 6e 20 30 20 63 6f 6e 74 61 69 6e  Region 0 contain
2f220 73 20 61 6c 6c 20 76 61 6c 75 65 73 20 73 6d 61  s all values sma
2f221 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 66 69  ller than the fi
2f222 72 73 74 20 73 61 6d 70 6c 65 20 76 61 6c 75 65  rst sample value
2f223 2e 20 52 65 67 69 6f 6e 0a 2a 2a 20 31 20 63 6f  . Region.** 1 co
2f224 6e 74 61 69 6e 73 20 76 61 6c 75 65 73 20 6c 61  ntains values la
2f225 72 67 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  rger than or equ
2f226 61 6c 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  al to the value 
2f227 6f 66 20 74 68 65 20 66 69 72 73 74 20 73 61 6d  of the first sam
2f228 70 6c 65 2c 0a 2a 2a 20 62 75 74 20 73 6d 61 6c  ple,.** but smal
2f229 6c 65 72 20 74 68 61 6e 20 74 68 65 20 76 61 6c  ler than the val
2f22a 75 65 20 6f 66 20 74 68 65 20 73 65 63 6f 6e 64  ue of the second
2f22b 2e 20 41 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a  . And so on..**.
2f22c 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  ** If successful
2f22d 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
2f22e 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69 63 68  determines which
2f22f 20 6f 66 20 74 68 65 20 72 65 67 69 6f 6e 73 20   of the regions 
2f230 76 61 6c 75 65 20 0a 2a 2a 20 70 56 61 6c 20 6c  value .** pVal l
2f231 69 65 73 20 69 6e 2c 20 73 65 74 73 20 2a 70 69  ies in, sets *pi
2f232 52 65 67 69 6f 6e 20 74 6f 20 74 68 65 20 72 65  Region to the re
2f233 67 69 6f 6e 20 69 6e 64 65 78 20 28 61 20 76 61  gion index (a va
2f234 6c 75 65 20 62 65 74 77 65 65 6e 20 30 0a 2a 2a  lue between 0.**
2f235 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4e 44 45   and SQLITE_INDE
2f236 58 5f 53 41 4d 50 4c 45 53 2b 31 2c 20 69 6e 63  X_SAMPLES+1, inc
2f237 6c 75 73 69 76 65 29 20 61 6e 64 20 72 65 74 75  lusive) and retu
2f238 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a  rns SQLITE_OK..*
2f239 2a 20 4f 72 2c 20 69 66 20 61 6e 20 4f 4f 4d 20  * Or, if an OOM 
2f23a 6f 63 63 75 72 73 20 77 68 69 6c 65 20 63 6f 6e  occurs while con
2f23b 76 65 72 74 69 6e 67 20 74 65 78 74 20 76 61 6c  verting text val
2f23c 75 65 73 20 62 65 74 77 65 65 6e 20 65 6e 63 6f  ues between enco
2f23d 64 69 6e 67 73 2c 0a 2a 2a 20 53 51 4c 49 54 45  dings,.** SQLITE
2f23e 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e  _NOMEM is return
2f23f 65 64 20 61 6e 64 20 2a 70 69 52 65 67 69 6f 6e  ed and *piRegion
2f240 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a   is undefined..*
2f241 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
2f242 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 73 74 61  ENABLE_STAT2.sta
2f243 74 69 63 20 69 6e 74 20 77 68 65 72 65 52 61 6e  tic int whereRan
2f244 67 65 52 65 67 69 6f 6e 28 0a 20 20 50 61 72 73  geRegion(.  Pars
2f245 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2f246 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
2f247 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
2f248 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 2c  /.  Index *pIdx,
2f249 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f24a 2f 2a 20 49 6e 64 65 78 20 74 6f 20 63 6f 6e 73  /* Index to cons
2f24b 69 64 65 72 20 64 6f 6d 61 69 6e 20 6f 66 20 2a  ider domain of *
2f24c 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  /.  sqlite3_valu
2f24d 65 20 2a 70 56 61 6c 2c 20 20 20 20 20 20 20 20  e *pVal,        
2f24e 2f 2a 20 56 61 6c 75 65 20 74 6f 20 63 6f 6e 73  /* Value to cons
2f24f 69 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  ider */.  int *p
2f250 69 52 65 67 69 6f 6e 20 20 20 20 20 20 20 20 20  iRegion         
2f251 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65        /* OUT: Re
2f252 67 69 6f 6e 20 6f 66 20 64 6f 6d 61 69 6e 20 69  gion of domain i
2f253 6e 20 77 68 69 63 68 20 76 61 6c 75 65 20 6c 69  n which value li
2f254 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 41  es */.){.  if( A
2f255 4c 57 41 59 53 28 70 56 61 6c 29 20 29 7b 0a 20  LWAYS(pVal) ){. 
2f256 20 20 20 49 6e 64 65 78 53 61 6d 70 6c 65 20 2a     IndexSample *
2f257 61 53 61 6d 70 6c 65 20 3d 20 70 49 64 78 2d 3e  aSample = pIdx->
2f258 61 53 61 6d 70 6c 65 3b 0a 20 20 20 20 69 6e 74  aSample;.    int
2f259 20 69 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20   i = 0;.    int 
2f25a 65 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 5f  eType = sqlite3_
2f25b 76 61 6c 75 65 5f 74 79 70 65 28 70 56 61 6c 29  value_type(pVal)
2f25c 3b 0a 0a 20 20 20 20 69 66 28 20 65 54 79 70 65  ;..    if( eType
2f25d 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  ==SQLITE_INTEGER
2f25e 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54   || eType==SQLIT
2f25f 45 5f 46 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20  E_FLOAT ){.     
2f260 20 64 6f 75 62 6c 65 20 72 20 3d 20 73 71 6c 69   double r = sqli
2f261 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65  te3_value_double
2f262 28 70 56 61 6c 29 3b 0a 20 20 20 20 20 20 66 6f  (pVal);.      fo
2f263 72 28 69 3d 30 3b 20 69 3c 53 51 4c 49 54 45 5f  r(i=0; i<SQLITE_
2f264 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 3b 20 69  INDEX_SAMPLES; i
2f265 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
2f266 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 65 54 79 70   aSample[i].eTyp
2f267 65 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29  e==SQLITE_NULL )
2f268 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
2f269 20 20 20 69 66 28 20 61 53 61 6d 70 6c 65 5b 69     if( aSample[i
2f26a 5d 2e 65 54 79 70 65 3e 3d 53 51 4c 49 54 45 5f  ].eType>=SQLITE_
2f26b 54 45 58 54 20 7c 7c 20 61 53 61 6d 70 6c 65 5b  TEXT || aSample[
2f26c 69 5d 2e 75 2e 72 3e 72 20 29 20 62 72 65 61 6b  i].u.r>r ) break
2f26d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
2f26e 6c 73 65 7b 20 0a 20 20 20 20 20 20 73 71 6c 69  lse{ .      sqli
2f26f 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2f270 2d 3e 64 62 3b 0a 20 20 20 20 20 20 43 6f 6c 6c  ->db;.      Coll
2f271 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20  Seq *pColl;.    
2f272 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 3b 0a 20    const u8 *z;. 
2f273 20 20 20 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 20       int n;..   
2f274 20 20 20 2f 2a 20 70 56 61 6c 20 63 6f 6d 65 73     /* pVal comes
2f275 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 56 61 6c   from sqlite3Val
2f276 75 65 46 72 6f 6d 45 78 70 72 28 29 20 73 6f 20  ueFromExpr() so 
2f277 74 68 65 20 74 79 70 65 20 63 61 6e 6e 6f 74 20  the type cannot 
2f278 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20  be NULL */.     
2f279 20 61 73 73 65 72 74 28 20 65 54 79 70 65 3d 3d   assert( eType==
2f27a 53 51 4c 49 54 45 5f 54 45 58 54 20 7c 7c 20 65  SQLITE_TEXT || e
2f27b 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f  Type==SQLITE_BLO
2f27c 42 20 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20  B );..      if( 
2f27d 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 42 4c  eType==SQLITE_BL
2f27e 4f 42 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 20  OB ){.        z 
2f27f 3d 20 28 63 6f 6e 73 74 20 75 38 20 2a 29 73 71  = (const u8 *)sq
2f280 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
2f281 28 70 56 61 6c 29 3b 0a 20 20 20 20 20 20 20 20  (pVal);.        
2f282 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c  pColl = db->pDfl
2f283 74 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 61  tColl;.        a
2f284 73 73 65 72 74 28 20 70 43 6f 6c 6c 2d 3e 65 6e  ssert( pColl->en
2f285 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29  c==SQLITE_UTF8 )
2f286 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2f287 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73         pColl = s
2f288 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71  qlite3GetCollSeq
2f289 28 64 62 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  (db, SQLITE_UTF8
2f28a 2c 20 30 2c 20 2a 70 49 64 78 2d 3e 61 7a 43 6f  , 0, *pIdx->azCo
2f28b 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ll);.        if(
2f28c 20 70 43 6f 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20   pColl==0 ){.   
2f28d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
2f28e 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2f28f 6e 6f 20 73 75 63 68 20 63 6f 6c 6c 61 74 69 6f  no such collatio
2f290 6e 20 73 65 71 75 65 6e 63 65 3a 20 25 73 22 2c  n sequence: %s",
2f291 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2f292 20 20 20 20 20 20 20 20 20 20 20 2a 70 49 64 78             *pIdx
2f293 2d 3e 61 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20  ->azColl);.     
2f294 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
2f295 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20  TE_ERROR;.      
2f296 20 20 7d 0a 20 20 20 20 20 20 20 20 7a 20 3d 20    }.        z = 
2f297 28 63 6f 6e 73 74 20 75 38 20 2a 29 73 71 6c 69  (const u8 *)sqli
2f298 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61  te3ValueText(pVa
2f299 6c 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a  l, pColl->enc);.
2f29a 20 20 20 20 20 20 20 20 69 66 28 20 21 7a 20 29          if( !z )
2f29b 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
2f29c 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
2f29d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2f29e 20 20 20 61 73 73 65 72 74 28 20 7a 20 26 26 20     assert( z && 
2f29f 70 43 6f 6c 6c 20 26 26 20 70 43 6f 6c 6c 2d 3e  pColl && pColl->
2f2a0 78 43 6d 70 20 29 3b 0a 20 20 20 20 20 20 7d 0a  xCmp );.      }.
2f2a1 20 20 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65        n = sqlite
2f2a2 33 56 61 6c 75 65 42 79 74 65 73 28 70 56 61 6c  3ValueBytes(pVal
2f2a3 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 0a  , pColl->enc);..
2f2a4 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
2f2a5 3c 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41  <SQLITE_INDEX_SA
2f2a6 4d 50 4c 45 53 3b 20 69 2b 2b 29 7b 0a 20 20 20  MPLES; i++){.   
2f2a7 20 20 20 20 20 69 6e 74 20 72 3b 0a 20 20 20 20       int r;.    
2f2a8 20 20 20 20 69 6e 74 20 65 53 61 6d 70 6c 65 74      int eSamplet
2f2a9 79 70 65 20 3d 20 61 53 61 6d 70 6c 65 5b 69 5d  ype = aSample[i]
2f2aa 2e 65 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20  .eType;.        
2f2ab 69 66 28 20 65 53 61 6d 70 6c 65 74 79 70 65 3d  if( eSampletype=
2f2ac 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 7c 7c 20  =SQLITE_NULL || 
2f2ad 65 53 61 6d 70 6c 65 74 79 70 65 3c 65 54 79 70  eSampletype<eTyp
2f2ae 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  e ) continue;.  
2f2af 20 20 20 20 20 20 69 66 28 20 28 65 53 61 6d 70        if( (eSamp
2f2b0 6c 65 74 79 70 65 21 3d 65 54 79 70 65 29 20 29  letype!=eType) )
2f2b1 20 62 72 65 61 6b 3b 0a 23 69 66 6e 64 65 66 20   break;.#ifndef 
2f2b2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
2f2b3 36 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 43  6.        if( pC
2f2b4 6f 6c 6c 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45  oll->enc!=SQLITE
2f2b5 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 20 20 20  _UTF8 ){.       
2f2b6 20 20 20 69 6e 74 20 6e 53 61 6d 70 6c 65 3b 0a     int nSample;.
2f2b7 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
2f2b8 7a 53 61 6d 70 6c 65 20 3d 20 73 71 6c 69 74 65  zSample = sqlite
2f2b9 33 55 74 66 38 74 6f 31 36 28 0a 20 20 20 20 20  3Utf8to16(.     
2f2ba 20 20 20 20 20 20 20 20 20 64 62 2c 20 70 43 6f           db, pCo
2f2bb 6c 6c 2d 3e 65 6e 63 2c 20 61 53 61 6d 70 6c 65  ll->enc, aSample
2f2bc 5b 69 5d 2e 75 2e 7a 2c 20 61 53 61 6d 70 6c 65  [i].u.z, aSample
2f2bd 5b 69 5d 2e 6e 42 79 74 65 2c 20 26 6e 53 61 6d  [i].nByte, &nSam
2f2be 70 6c 65 0a 20 20 20 20 20 20 20 20 20 20 29 3b  ple.          );
2f2bf 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21  .          if( !
2f2c0 7a 53 61 6d 70 6c 65 20 29 7b 0a 20 20 20 20 20  zSample ){.     
2f2c1 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64         assert( d
2f2c2 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2f2c3 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  );.            r
2f2c4 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
2f2c5 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  EM;.          }.
2f2c6 20 20 20 20 20 20 20 20 20 20 72 20 3d 20 70 43            r = pC
2f2c7 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d  oll->xCmp(pColl-
2f2c8 3e 70 55 73 65 72 2c 20 6e 53 61 6d 70 6c 65 2c  >pUser, nSample,
2f2c9 20 7a 53 61 6d 70 6c 65 2c 20 6e 2c 20 7a 29 3b   zSample, n, z);
2f2ca 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2f2cb 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 61  e3DbFree(db, zSa
2f2cc 6d 70 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 7d  mple);.        }
2f2cd 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20  else.#endif.    
2f2ce 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20      {.          
2f2cf 72 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28  r = pColl->xCmp(
2f2d0 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 20 61 53  pColl->pUser, aS
2f2d1 61 6d 70 6c 65 5b 69 5d 2e 6e 42 79 74 65 2c 20  ample[i].nByte, 
2f2d2 61 53 61 6d 70 6c 65 5b 69 5d 2e 75 2e 7a 2c 20  aSample[i].u.z, 
2f2d3 6e 2c 20 7a 29 3b 0a 20 20 20 20 20 20 20 20 7d  n, z);.        }
2f2d4 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 3e 30  .        if( r>0
2f2d5 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
2f2d6 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73  }.    }..    ass
2f2d7 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 3d  ert( i>=0 && i<=
2f2d8 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d  SQLITE_INDEX_SAM
2f2d9 50 4c 45 53 20 29 3b 0a 20 20 20 20 2a 70 69 52  PLES );.    *piR
2f2da 65 67 69 6f 6e 20 3d 20 69 3b 0a 20 20 7d 0a 20  egion = i;.  }. 
2f2db 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
2f2dc 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 20 20 2f 2a  K;.}.#endif   /*
2f2dd 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45   #ifdef SQLITE_E
2f2de 4e 41 42 4c 45 5f 53 54 41 54 32 20 2a 2f 0a 0a  NABLE_STAT2 */..
2f2df 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
2f2e0 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 65  ion is used to e
2f2e1 73 74 69 6d 61 74 65 20 74 68 65 20 6e 75 6d 62  stimate the numb
2f2e2 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20  er of rows that 
2f2e3 77 69 6c 6c 20 62 65 20 76 69 73 69 74 65 64 0a  will be visited.
2f2e4 2a 2a 20 62 79 20 73 63 61 6e 6e 69 6e 67 20 61  ** by scanning a
2f2e5 6e 20 69 6e 64 65 78 20 66 6f 72 20 61 20 72 61  n index for a ra
2f2e6 6e 67 65 20 6f 66 20 76 61 6c 75 65 73 2e 20 54  nge of values. T
2f2e7 68 65 20 72 61 6e 67 65 20 6d 61 79 20 68 61 76  he range may hav
2f2e8 65 20 61 6e 20 75 70 70 65 72 0a 2a 2a 20 62 6f  e an upper.** bo
2f2e9 75 6e 64 2c 20 61 20 6c 6f 77 65 72 20 62 6f 75  und, a lower bou
2f2ea 6e 64 2c 20 6f 72 20 62 6f 74 68 2e 20 54 68 65  nd, or both. The
2f2eb 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
2f2ec 72 6d 73 20 74 68 61 74 20 73 65 74 20 74 68 65  rms that set the
2f2ed 20 75 70 70 65 72 0a 2a 2a 20 61 6e 64 20 6c 6f   upper.** and lo
2f2ee 77 65 72 20 62 6f 75 6e 64 73 20 61 72 65 20 72  wer bounds are r
2f2ef 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 70 4c  epresented by pL
2f2f0 6f 77 65 72 20 61 6e 64 20 70 55 70 70 65 72 20  ower and pUpper 
2f2f1 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 46 6f  respectively. Fo
2f2f2 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 61 73  r.** example, as
2f2f3 73 75 6d 69 6e 67 20 74 68 61 74 20 69 6e 64 65  suming that inde
2f2f4 78 20 70 20 69 73 20 6f 6e 20 74 31 28 61 29 3a  x p is on t1(a):
2f2f5 0a 2a 2a 0a 2a 2a 20 20 20 2e 2e 2e 20 46 52 4f  .**.**   ... FRO
2f2f6 4d 20 74 31 20 57 48 45 52 45 20 61 20 3e 20 3f  M t1 WHERE a > ?
2f2f7 20 41 4e 44 20 61 20 3c 20 3f 20 2e 2e 2e 0a 2a   AND a < ? ....*
2f2f8 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2f2f9 20 20 20 20 20 7c 5f 5f 5f 5f 5f 7c 20 20 20 7c       |_____|   |
2f2fa 5f 5f 5f 5f 5f 7c 0a 2a 2a 20 20 20 20 20 20 20  _____|.**       
2f2fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f2fc 7c 20 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20  |         |.**  
2f2fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f2fe 20 20 20 70 4c 6f 77 65 72 20 20 20 20 70 55 70     pLower    pUp
2f2ff 70 65 72 0a 2a 2a 0a 2a 2a 20 49 66 20 65 69 74  per.**.** If eit
2f300 68 65 72 20 6f 66 20 74 68 65 20 75 70 70 65 72  her of the upper
2f301 20 6f 72 20 6c 6f 77 65 72 20 62 6f 75 6e 64 20   or lower bound 
2f302 69 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 2c 20  is not present, 
2f303 74 68 65 6e 20 4e 55 4c 4c 20 69 73 20 70 61 73  then NULL is pas
2f304 73 65 64 20 69 6e 0a 2a 2a 20 70 6c 61 63 65 20  sed in.** place 
2f305 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  of the correspon
2f306 64 69 6e 67 20 57 68 65 72 65 54 65 72 6d 2e 0a  ding WhereTerm..
2f307 2a 2a 0a 2a 2a 20 54 68 65 20 6e 45 71 20 70 61  **.** The nEq pa
2f308 72 61 6d 65 74 65 72 20 69 73 20 70 61 73 73 65  rameter is passe
2f309 64 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  d the index of t
2f30a 68 65 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 20  he index column 
2f30b 73 75 62 6a 65 63 74 20 74 6f 20 74 68 65 0a 2a  subject to the.*
2f30c 2a 20 72 61 6e 67 65 20 63 6f 6e 73 74 72 61 69  * range constrai
2f30d 6e 74 2e 20 4f 72 2c 20 65 71 75 69 76 61 6c 65  nt. Or, equivale
2f30e 6e 74 6c 79 2c 20 74 68 65 20 6e 75 6d 62 65 72  ntly, the number
2f30f 20 6f 66 20 65 71 75 61 6c 69 74 79 20 63 6f 6e   of equality con
2f310 73 74 72 61 69 6e 74 73 0a 2a 2a 20 6f 70 74 69  straints.** opti
2f311 6d 69 7a 65 64 20 62 79 20 74 68 65 20 70 72 6f  mized by the pro
2f312 70 6f 73 65 64 20 69 6e 64 65 78 20 73 63 61 6e  posed index scan
2f313 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61  . For example, a
2f314 73 73 75 6d 69 6e 67 20 69 6e 64 65 78 20 70 20  ssuming index p 
2f315 69 73 0a 2a 2a 20 6f 6e 20 74 31 28 61 2c 20 62  is.** on t1(a, b
2f316 29 2c 20 61 6e 64 20 74 68 65 20 53 51 4c 20 71  ), and the SQL q
2f317 75 65 72 79 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  uery is:.**.**  
2f318 20 2e 2e 2e 20 46 52 4f 4d 20 74 31 20 57 48 45   ... FROM t1 WHE
2f319 52 45 20 61 20 3d 20 3f 20 41 4e 44 20 62 20 3e  RE a = ? AND b >
2f31a 20 3f 20 41 4e 44 20 62 20 3c 20 3f 20 2e 2e 2e   ? AND b < ? ...
2f31b 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 6e 45 71 20  .**.** then nEq 
2f31c 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64  should be passed
2f31d 20 74 68 65 20 76 61 6c 75 65 20 31 20 28 61 73   the value 1 (as
2f31e 20 74 68 65 20 72 61 6e 67 65 20 72 65 73 74 72   the range restr
2f31f 69 63 74 65 64 20 63 6f 6c 75 6d 6e 2c 0a 2a 2a  icted column,.**
2f320 20 62 2c 20 69 73 20 74 68 65 20 73 65 63 6f 6e   b, is the secon
2f321 64 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75  d left-most colu
2f322 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78 29  mn of the index)
2f323 2e 20 4f 72 2c 20 69 66 20 74 68 65 20 71 75 65  . Or, if the que
2f324 72 79 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2e  ry is:.**.**   .
2f325 2e 2e 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45  .. FROM t1 WHERE
2f326 20 61 20 3e 20 3f 20 41 4e 44 20 61 20 3c 20 3f   a > ? AND a < ?
2f327 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20   ....**.** then 
2f328 6e 45 71 20 73 68 6f 75 6c 64 20 62 65 20 70 61  nEq should be pa
2f329 73 73 65 64 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68  ssed 0..**.** Th
2f32a 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65  e returned value
2f32b 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 62   is an integer b
2f32c 65 74 77 65 65 6e 20 31 20 61 6e 64 20 31 30 30  etween 1 and 100
2f32d 2c 20 69 6e 63 6c 75 73 69 76 65 2e 20 41 20 72  , inclusive. A r
2f32e 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 6f  eturn.** value o
2f32f 66 20 31 20 69 6e 64 69 63 61 74 65 73 20 74 68  f 1 indicates th
2f330 61 74 20 74 68 65 20 70 72 6f 70 6f 73 65 64 20  at the proposed 
2f331 72 61 6e 67 65 20 73 63 61 6e 20 69 73 20 65 78  range scan is ex
2f332 70 65 63 74 65 64 20 74 6f 20 76 69 73 69 74 0a  pected to visit.
2f333 2a 2a 20 61 70 70 72 6f 78 69 6d 61 74 65 6c 79  ** approximately
2f334 20 31 2f 31 30 30 74 68 20 28 31 25 29 20 6f 66   1/100th (1%) of
2f335 20 74 68 65 20 72 6f 77 73 20 73 65 6c 65 63 74   the rows select
2f336 65 64 20 62 79 20 74 68 65 20 6e 45 71 20 65 71  ed by the nEq eq
2f337 75 61 6c 69 74 79 0a 2a 2a 20 63 6f 6e 73 74 72  uality.** constr
2f338 61 69 6e 74 73 20 28 69 66 20 61 6e 79 29 2e 20  aints (if any). 
2f339 41 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f  A return value o
2f33a 66 20 31 30 30 20 69 6e 64 69 63 61 74 65 73 20  f 100 indicates 
2f33b 74 68 61 74 20 69 74 20 69 73 20 65 78 70 65 63  that it is expec
2f33c 74 65 64 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  ted.** that the 
2f33d 72 61 6e 67 65 20 73 63 61 6e 20 77 69 6c 6c 20  range scan will 
2f33e 76 69 73 69 74 20 65 76 65 72 79 20 72 6f 77 20  visit every row 
2f33f 28 31 30 30 25 29 20 73 65 6c 65 63 74 65 64 20  (100%) selected 
2f340 62 79 20 74 68 65 20 65 71 75 61 6c 69 74 79 0a  by the equality.
2f341 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a  ** constraints..
2f342 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 61 62 73  **.** In the abs
2f343 65 6e 63 65 20 6f 66 20 73 71 6c 69 74 65 5f 73  ence of sqlite_s
2f344 74 61 74 32 20 41 4e 41 4c 59 5a 45 20 64 61 74  tat2 ANALYZE dat
2f345 61 2c 20 65 61 63 68 20 72 61 6e 67 65 20 69 6e  a, each range in
2f346 65 71 75 61 6c 69 74 79 0a 2a 2a 20 72 65 64 75  equality.** redu
2f347 63 65 73 20 74 68 65 20 73 65 61 72 63 68 20 73  ces the search s
2f348 70 61 63 65 20 62 79 20 32 2f 33 72 64 73 2e 20  pace by 2/3rds. 
2f349 20 48 65 6e 63 65 20 61 20 73 69 6e 67 6c 65 20   Hence a single 
2f34a 63 6f 6e 73 74 72 61 69 6e 74 20 28 78 3e 3f 29  constraint (x>?)
2f34b 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 20 61  .** results in a
2f34c 20 72 65 74 75 72 6e 20 6f 66 20 33 33 20 61 6e   return of 33 an
2f34d 64 20 61 20 72 61 6e 67 65 20 63 6f 6e 73 74 72  d a range constr
2f34e 61 69 6e 74 20 28 78 3e 3f 20 41 4e 44 20 78 3c  aint (x>? AND x<
2f34f 3f 29 20 72 65 73 75 6c 74 73 0a 2a 2a 20 69 6e  ?) results.** in
2f350 20 61 20 72 65 74 75 72 6e 20 6f 66 20 31 31 2e   a return of 11.
2f351 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
2f352 68 65 72 65 52 61 6e 67 65 53 63 61 6e 45 73 74  hereRangeScanEst
2f353 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2f354 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73  e,       /* Pars
2f355 69 6e 67 20 26 20 63 6f 64 65 20 67 65 6e 65 72  ing & code gener
2f356 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  ating context */
2f357 0a 20 20 49 6e 64 65 78 20 2a 70 2c 20 20 20 20  .  Index *p,    
2f358 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69          /* The i
2f359 6e 64 65 78 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ndex containing 
2f35a 74 68 65 20 72 61 6e 67 65 2d 63 6f 6d 70 61 72  the range-compar
2f35b 65 64 20 63 6f 6c 75 6d 6e 3b 20 22 78 22 20 2a  ed column; "x" *
2f35c 2f 0a 20 20 69 6e 74 20 6e 45 71 2c 20 20 20 20  /.  int nEq,    
2f35d 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 64 65           /* inde
2f35e 78 20 69 6e 74 6f 20 70 2d 3e 61 43 6f 6c 5b 5d  x into p->aCol[]
2f35f 20 6f 66 20 74 68 65 20 72 61 6e 67 65 2d 63 6f   of the range-co
2f360 6d 70 61 72 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f  mpared column */
2f361 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4c  .  WhereTerm *pL
2f362 6f 77 65 72 2c 20 20 20 2f 2a 20 4c 6f 77 65 72  ower,   /* Lower
2f363 20 62 6f 75 6e 64 20 6f 6e 20 74 68 65 20 72 61   bound on the ra
2f364 6e 67 65 2e 20 65 78 3a 20 22 78 3e 31 32 33 22  nge. ex: "x>123"
2f365 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a   Might be NULL *
2f366 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70  /.  WhereTerm *p
2f367 55 70 70 65 72 2c 20 20 20 2f 2a 20 55 70 70 65  Upper,   /* Uppe
2f368 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 65 20 72  r bound on the r
2f369 61 6e 67 65 2e 20 65 78 3a 20 22 78 3c 34 35 35  ange. ex: "x<455
2f36a 22 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20  " Might be NULL 
2f36b 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 45 73 74 20  */.  int *piEst 
2f36c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
2f36d 3a 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20 2a  : Return value *
2f36e 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  /.){.  int rc = 
2f36f 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 23 69 66 64  SQLITE_OK;..#ifd
2f370 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
2f371 5f 53 54 41 54 32 0a 20 20 73 71 6c 69 74 65 33  _STAT2.  sqlite3
2f372 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2f373 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  b;.  sqlite3_val
2f374 75 65 20 2a 70 4c 6f 77 65 72 56 61 6c 20 3d 20  ue *pLowerVal = 
2f375 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  0;.  sqlite3_val
2f376 75 65 20 2a 70 55 70 70 65 72 56 61 6c 20 3d 20  ue *pUpperVal = 
2f377 30 3b 0a 0a 20 20 69 66 28 20 6e 45 71 3d 3d 30  0;..  if( nEq==0
2f378 20 26 26 20 70 2d 3e 61 53 61 6d 70 6c 65 20 29   && p->aSample )
2f379 7b 0a 20 20 20 20 69 6e 74 20 69 45 73 74 3b 0a  {.    int iEst;.
2f37a 20 20 20 20 69 6e 74 20 69 4c 6f 77 65 72 20 3d      int iLower =
2f37b 20 30 3b 0a 20 20 20 20 69 6e 74 20 69 55 70 70   0;.    int iUpp
2f37c 65 72 20 3d 20 53 51 4c 49 54 45 5f 49 4e 44 45  er = SQLITE_INDE
2f37d 58 5f 53 41 4d 50 4c 45 53 3b 0a 20 20 20 20 75  X_SAMPLES;.    u
2f37e 38 20 61 66 66 20 3d 20 70 2d 3e 70 54 61 62 6c  8 aff = p->pTabl
2f37f 65 2d 3e 61 43 6f 6c 5b 30 5d 2e 61 66 66 69 6e  e->aCol[0].affin
2f380 69 74 79 3b 0a 0a 20 20 20 20 69 66 28 20 70 4c  ity;..    if( pL
2f381 6f 77 65 72 20 29 7b 0a 20 20 20 20 20 20 45 78  ower ){.      Ex
2f382 70 72 20 2a 70 45 78 70 72 20 3d 20 70 4c 6f 77  pr *pExpr = pLow
2f383 65 72 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68  er->pExpr->pRigh
2f384 74 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  t;.      rc = sq
2f385 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78  lite3ValueFromEx
2f386 70 72 28 64 62 2c 20 70 45 78 70 72 2c 20 53 51  pr(db, pExpr, SQ
2f387 4c 49 54 45 5f 55 54 46 38 2c 20 61 66 66 2c 20  LITE_UTF8, aff, 
2f388 26 70 4c 6f 77 65 72 56 61 6c 29 3b 0a 20 20 20  &pLowerVal);.   
2f389 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
2f38a 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 55 70 70  QLITE_OK && pUpp
2f38b 65 72 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72  er ){.      Expr
2f38c 20 2a 70 45 78 70 72 20 3d 20 70 55 70 70 65 72   *pExpr = pUpper
2f38d 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b  ->pExpr->pRight;
2f38e 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
2f38f 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72  te3ValueFromExpr
2f390 28 64 62 2c 20 70 45 78 70 72 2c 20 53 51 4c 49  (db, pExpr, SQLI
2f391 54 45 5f 55 54 46 38 2c 20 61 66 66 2c 20 26 70  TE_UTF8, aff, &p
2f392 55 70 70 65 72 56 61 6c 29 3b 0a 20 20 20 20 7d  UpperVal);.    }
2f393 0a 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ..    if( rc!=SQ
2f394 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 70 4c 6f 77  LITE_OK || (pLow
2f395 65 72 56 61 6c 3d 3d 30 20 26 26 20 70 55 70 70  erVal==0 && pUpp
2f396 65 72 56 61 6c 3d 3d 30 29 20 29 7b 0a 20 20 20  erVal==0) ){.   
2f397 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46     sqlite3ValueF
2f398 72 65 65 28 70 4c 6f 77 65 72 56 61 6c 29 3b 0a  ree(pLowerVal);.
2f399 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c        sqlite3Val
2f39a 75 65 46 72 65 65 28 70 55 70 70 65 72 56 61 6c  ueFree(pUpperVal
2f39b 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 72 61  );.      goto ra
2f39c 6e 67 65 5f 65 73 74 5f 66 61 6c 6c 62 61 63 6b  nge_est_fallback
2f39d 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
2f39e 70 4c 6f 77 65 72 56 61 6c 3d 3d 30 20 29 7b 0a  pLowerVal==0 ){.
2f39f 20 20 20 20 20 20 72 63 20 3d 20 77 68 65 72 65        rc = where
2f3a0 52 61 6e 67 65 52 65 67 69 6f 6e 28 70 50 61 72  RangeRegion(pPar
2f3a1 73 65 2c 20 70 2c 20 70 55 70 70 65 72 56 61 6c  se, p, pUpperVal
2f3a2 2c 20 26 69 55 70 70 65 72 29 3b 0a 20 20 20 20  , &iUpper);.    
2f3a3 20 20 69 66 28 20 70 4c 6f 77 65 72 20 29 20 69    if( pLower ) i
2f3a4 4c 6f 77 65 72 20 3d 20 69 55 70 70 65 72 2f 32  Lower = iUpper/2
2f3a5 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
2f3a6 70 55 70 70 65 72 56 61 6c 3d 3d 30 20 29 7b 0a  pUpperVal==0 ){.
2f3a7 20 20 20 20 20 20 72 63 20 3d 20 77 68 65 72 65        rc = where
2f3a8 52 61 6e 67 65 52 65 67 69 6f 6e 28 70 50 61 72  RangeRegion(pPar
2f3a9 73 65 2c 20 70 2c 20 70 4c 6f 77 65 72 56 61 6c  se, p, pLowerVal
2f3aa 2c 20 26 69 4c 6f 77 65 72 29 3b 0a 20 20 20 20  , &iLower);.    
2f3ab 20 20 69 66 28 20 70 55 70 70 65 72 20 29 20 69    if( pUpper ) i
2f3ac 55 70 70 65 72 20 3d 20 28 69 4c 6f 77 65 72 20  Upper = (iLower 
2f3ad 2b 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53  + SQLITE_INDEX_S
2f3ae 41 4d 50 4c 45 53 20 2b 20 31 29 2f 32 3b 0a 20  AMPLES + 1)/2;. 
2f3af 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2f3b0 72 63 20 3d 20 77 68 65 72 65 52 61 6e 67 65 52  rc = whereRangeR
2f3b1 65 67 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 2c  egion(pParse, p,
2f3b2 20 70 55 70 70 65 72 56 61 6c 2c 20 26 69 55 70   pUpperVal, &iUp
2f3b3 70 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  per);.      if( 
2f3b4 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
2f3b5 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 77 68  .        rc = wh
2f3b6 65 72 65 52 61 6e 67 65 52 65 67 69 6f 6e 28 70  ereRangeRegion(p
2f3b7 50 61 72 73 65 2c 20 70 2c 20 70 4c 6f 77 65 72  Parse, p, pLower
2f3b8 56 61 6c 2c 20 26 69 4c 6f 77 65 72 29 3b 0a 20  Val, &iLower);. 
2f3b9 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
2f3ba 20 20 69 45 73 74 20 3d 20 69 55 70 70 65 72 20    iEst = iUpper 
2f3bb 2d 20 69 4c 6f 77 65 72 3b 0a 20 20 20 20 74 65  - iLower;.    te
2f3bc 73 74 63 61 73 65 28 20 69 45 73 74 3d 3d 53 51  stcase( iEst==SQ
2f3bd 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c  LITE_INDEX_SAMPL
2f3be 45 53 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ES );.    assert
2f3bf 28 20 69 45 73 74 3c 3d 53 51 4c 49 54 45 5f 49  ( iEst<=SQLITE_I
2f3c0 4e 44 45 58 5f 53 41 4d 50 4c 45 53 20 29 3b 0a  NDEX_SAMPLES );.
2f3c1 20 20 20 20 69 66 28 20 69 45 73 74 3c 31 20 29      if( iEst<1 )
2f3c2 7b 0a 20 20 20 20 20 20 69 45 73 74 20 3d 20 31  {.      iEst = 1
2f3c3 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c  ;.    }..    sql
2f3c4 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 4c  ite3ValueFree(pL
2f3c5 6f 77 65 72 56 61 6c 29 3b 0a 20 20 20 20 73 71  owerVal);.    sq
2f3c6 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70  lite3ValueFree(p
2f3c7 55 70 70 65 72 56 61 6c 29 3b 0a 20 20 20 20 2a  UpperVal);.    *
2f3c8 70 69 45 73 74 20 3d 20 28 69 45 73 74 20 2a 20  piEst = (iEst * 
2f3c9 31 30 30 29 2f 53 51 4c 49 54 45 5f 49 4e 44 45  100)/SQLITE_INDE
2f3ca 58 5f 53 41 4d 50 4c 45 53 3b 0a 20 20 20 20 72  X_SAMPLES;.    r
2f3cb 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 72 61  eturn rc;.  }.ra
2f3cc 6e 67 65 5f 65 73 74 5f 66 61 6c 6c 62 61 63 6b  nge_est_fallback
2f3cd 3a 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44  :.#else.  UNUSED
2f3ce 5f 50 41 52 41 4d 45 54 45 52 28 70 50 61 72 73  _PARAMETER(pPars
2f3cf 65 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  e);.  UNUSED_PAR
2f3d0 41 4d 45 54 45 52 28 70 29 3b 0a 20 20 55 4e 55  AMETER(p);.  UNU
2f3d1 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 45  SED_PARAMETER(nE
2f3d2 71 29 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73  q);.#endif.  ass
2f3d3 65 72 74 28 20 70 4c 6f 77 65 72 20 7c 7c 20 70  ert( pLower || p
2f3d4 55 70 70 65 72 20 29 3b 0a 20 20 69 66 28 20 70  Upper );.  if( p
2f3d5 4c 6f 77 65 72 20 26 26 20 70 55 70 70 65 72 20  Lower && pUpper 
2f3d6 29 7b 0a 20 20 20 20 2a 70 69 45 73 74 20 3d 20  ){.    *piEst = 
2f3d7 31 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  11;.  }else{.   
2f3d8 20 2a 70 69 45 73 74 20 3d 20 33 33 3b 0a 20 20   *piEst = 33;.  
2f3d9 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
2f3da 0a 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68  .../*.** Find th
2f3db 65 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f 72  e query plan for
2f3dc 20 61 63 63 65 73 73 69 6e 67 20 61 20 70 61 72   accessing a par
2f3dd 74 69 63 75 6c 61 72 20 74 61 62 6c 65 2e 20 20  ticular table.  
2f3de 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 62 65 73  Write the.** bes
2f3df 74 20 71 75 65 72 79 20 70 6c 61 6e 20 61 6e 64  t query plan and
2f3e0 20 69 74 73 20 63 6f 73 74 20 69 6e 74 6f 20 74   its cost into t
2f3e1 68 65 20 57 68 65 72 65 43 6f 73 74 20 6f 62 6a  he WhereCost obj
2f3e2 65 63 74 20 73 75 70 70 6c 69 65 64 20 61 73 20  ect supplied as 
2f3e3 74 68 65 0a 2a 2a 20 6c 61 73 74 20 70 61 72 61  the.** last para
2f3e4 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  meter..**.** The
2f3e5 20 6c 6f 77 65 73 74 20 63 6f 73 74 20 70 6c 61   lowest cost pla
2f3e6 6e 20 77 69 6e 73 2e 20 20 54 68 65 20 63 6f 73  n wins.  The cos
2f3e7 74 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65  t is an estimate
2f3e8 20 6f 66 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f   of the amount o
2f3e9 66 0a 2a 2a 20 43 50 55 20 61 6e 64 20 64 69 73  f.** CPU and dis
2f3ea 6b 20 49 2f 4f 20 6e 65 65 64 20 74 6f 20 70 72  k I/O need to pr
2f3eb 6f 63 65 73 73 20 74 68 65 20 72 65 71 75 65 73  ocess the reques
2f3ec 74 20 75 73 69 6e 67 20 74 68 65 20 73 65 6c 65  t using the sele
2f3ed 63 74 65 64 20 70 6c 61 6e 2e 0a 2a 2a 20 46 61  cted plan..** Fa
2f3ee 63 74 6f 72 73 20 74 68 61 74 20 69 6e 66 6c 75  ctors that influ
2f3ef 65 6e 63 65 20 63 6f 73 74 20 69 6e 63 6c 75 64  ence cost includ
2f3f0 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 54  e:.**.**    *  T
2f3f1 68 65 20 65 73 74 69 6d 61 74 65 64 20 6e 75 6d  he estimated num
2f3f2 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 74  ber of rows that
2f3f3 20 77 69 6c 6c 20 62 65 20 72 65 74 72 69 65 76   will be retriev
2f3f4 65 64 2e 20 20 28 54 68 65 0a 2a 2a 20 20 20 20  ed.  (The.**    
2f3f5 20 20 20 66 65 77 65 72 20 74 68 65 20 62 65 74     fewer the bet
2f3f6 74 65 72 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 20 2a  ter.).**.**    *
2f3f7 20 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74    Whether or not
2f3f8 20 73 6f 72 74 69 6e 67 20 6d 75 73 74 20 6f 63   sorting must oc
2f3f9 63 75 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20  cur..**.**    * 
2f3fa 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   Whether or not 
2f3fb 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 73 65  there must be se
2f3fc 70 61 72 61 74 65 20 6c 6f 6f 6b 75 70 73 20 69  parate lookups i
2f3fd 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 69  n the.**       i
2f3fe 6e 64 65 78 20 61 6e 64 20 69 6e 20 74 68 65 20  ndex and in the 
2f3ff 6d 61 69 6e 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a  main table..**.*
2f400 2a 20 49 66 20 74 68 65 72 65 20 77 61 73 20 61  * If there was a
2f401 6e 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61  n INDEXED BY cla
2f402 75 73 65 20 28 70 53 72 63 2d 3e 70 49 6e 64 65  use (pSrc->pInde
2f403 78 29 20 61 74 74 61 63 68 65 64 20 74 6f 20 74  x) attached to t
2f404 68 65 20 74 61 62 6c 65 20 69 6e 0a 2a 2a 20 74  he table in.** t
2f405 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
2f406 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63  , then this func
2f407 74 69 6f 6e 20 6f 6e 6c 79 20 63 6f 6e 73 69 64  tion only consid
2f408 65 72 73 20 70 6c 61 6e 73 20 75 73 69 6e 67 20  ers plans using 
2f409 74 68 65 20 0a 2a 2a 20 6e 61 6d 65 64 20 69 6e  the .** named in
2f40a 64 65 78 2e 20 49 66 20 6e 6f 20 73 75 63 68 20  dex. If no such 
2f40b 70 6c 61 6e 20 69 73 20 66 6f 75 6e 64 2c 20 74  plan is found, t
2f40c 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64  hen the returned
2f40d 20 63 6f 73 74 20 69 73 0a 2a 2a 20 53 51 4c 49   cost is.** SQLI
2f40e 54 45 5f 42 49 47 5f 44 42 4c 2e 20 49 66 20 61  TE_BIG_DBL. If a
2f40f 20 70 6c 61 6e 20 69 73 20 66 6f 75 6e 64 20 74   plan is found t
2f410 68 61 74 20 75 73 65 73 20 74 68 65 20 6e 61 6d  hat uses the nam
2f411 65 64 20 69 6e 64 65 78 2c 20 0a 2a 2a 20 74 68  ed index, .** th
2f412 65 6e 20 74 68 65 20 63 6f 73 74 20 69 73 20 63  en the cost is c
2f413 61 6c 63 75 6c 61 74 65 64 20 69 6e 20 74 68 65  alculated in the
2f414 20 75 73 75 61 6c 20 77 61 79 2e 0a 2a 2a 0a 2a   usual way..**.*
2f415 2a 20 49 66 20 61 20 4e 4f 54 20 49 4e 44 45 58  * If a NOT INDEX
2f416 45 44 20 63 6c 61 75 73 65 20 28 70 53 72 63 2d  ED clause (pSrc-
2f417 3e 6e 6f 74 49 6e 64 65 78 65 64 21 3d 30 29 20  >notIndexed!=0) 
2f418 77 61 73 20 61 74 74 61 63 68 65 64 20 74 6f 20  was attached to 
2f419 74 68 65 20 74 61 62 6c 65 20 0a 2a 2a 20 69 6e  the table .** in
2f41a 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
2f41b 65 6d 65 6e 74 2c 20 74 68 65 6e 20 6e 6f 20 69  ement, then no i
2f41c 6e 64 65 78 65 73 20 61 72 65 20 63 6f 6e 73 69  ndexes are consi
2f41d 64 65 72 65 64 2e 20 48 6f 77 65 76 65 72 2c 20  dered. However, 
2f41e 74 68 65 20 0a 2a 2a 20 73 65 6c 65 63 74 65 64  the .** selected
2f41f 20 70 6c 61 6e 20 6d 61 79 20 73 74 69 6c 6c 20   plan may still 
2f420 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20 6f  take advantage o
2f421 66 20 74 68 65 20 74 61 62 6c 65 73 20 62 75 69  f the tables bui
2f422 6c 74 2d 69 6e 20 72 6f 77 69 64 0a 2a 2a 20 69  lt-in rowid.** i
2f423 6e 64 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ndex..*/.static 
2f424 76 6f 69 64 20 62 65 73 74 42 74 72 65 65 49 6e  void bestBtreeIn
2f425 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50  dex(.  Parse *pP
2f426 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
2f427 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e     /* The parsin
2f428 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57  g context */.  W
2f429 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c  hereClause *pWC,
2f42a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2f42b 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
2f42c 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  /.  struct SrcLi
2f42d 73 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20 20  st_item *pSrc,  
2f42e 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75  /* The FROM clau
2f42f 73 65 20 74 65 72 6d 20 74 6f 20 73 65 61 72 63  se term to searc
2f430 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e  h */.  Bitmask n
2f431 6f 74 52 65 61 64 79 2c 20 20 20 20 20 20 20 20  otReady,        
2f432 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 63 75     /* Mask of cu
2f433 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 6e  rsors that are n
2f434 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a  ot available */.
2f435 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64    ExprList *pOrd
2f436 65 72 42 79 2c 20 20 20 20 20 20 20 20 20 2f 2a  erBy,         /*
2f437 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   The ORDER BY cl
2f438 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 43  ause */.  WhereC
2f439 6f 73 74 20 2a 70 43 6f 73 74 20 20 20 20 20 20  ost *pCost      
2f43a 20 20 20 20 20 20 2f 2a 20 4c 6f 77 65 73 74 20        /* Lowest 
2f43b 63 6f 73 74 20 71 75 65 72 79 20 70 6c 61 6e 20  cost query plan 
2f43c 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 43 75 72  */.){.  int iCur
2f43d 20 3d 20 70 53 72 63 2d 3e 69 43 75 72 73 6f 72   = pSrc->iCursor
2f43e 3b 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f  ;   /* The curso
2f43f 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  r of the table t
2f440 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 2a 2f  o be accessed */
2f441 0a 20 20 49 6e 64 65 78 20 2a 70 50 72 6f 62 65  .  Index *pProbe
2f442 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2f443 2a 20 41 6e 20 69 6e 64 65 78 20 77 65 20 61 72  * An index we ar
2f444 65 20 65 76 61 6c 75 61 74 69 6e 67 20 2a 2f 0a  e evaluating */.
2f445 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20    Index *pIdx;  
2f446 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2f447 20 43 6f 70 79 20 6f 66 20 70 50 72 6f 62 65 2c   Copy of pProbe,
2f448 20 6f 72 20 7a 65 72 6f 20 66 6f 72 20 49 50 4b   or zero for IPK
2f449 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20   index */.  int 
2f44a 65 71 54 65 72 6d 4d 61 73 6b 3b 20 20 20 20 20  eqTermMask;     
2f44b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
2f44c 6e 74 20 6d 61 73 6b 20 6f 66 20 76 61 6c 69 64  nt mask of valid
2f44d 20 65 71 75 61 6c 69 74 79 20 6f 70 65 72 61 74   equality operat
2f44e 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78  ors */.  int idx
2f44f 45 71 54 65 72 6d 4d 61 73 6b 3b 20 20 20 20 20  EqTermMask;     
2f450 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6d 61       /* Index ma
2f451 73 6b 20 6f 66 20 76 61 6c 69 64 20 65 71 75 61  sk of valid equa
2f452 6c 69 74 79 20 6f 70 65 72 61 74 6f 72 73 20 2a  lity operators *
2f453 2f 0a 20 20 49 6e 64 65 78 20 73 50 6b 3b 20 20  /.  Index sPk;  
2f454 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f455 2f 2a 20 41 20 66 61 6b 65 20 69 6e 64 65 78 20  /* A fake index 
2f456 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 70  object for the p
2f457 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 20 20  rimary key */.  
2f458 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 61 69 52  unsigned int aiR
2f459 6f 77 45 73 74 50 6b 5b 32 5d 3b 20 2f 2a 20 54  owEstPk[2]; /* T
2f45a 68 65 20 61 69 52 6f 77 45 73 74 5b 5d 20 76 61  he aiRowEst[] va
2f45b 6c 75 65 20 66 6f 72 20 74 68 65 20 73 50 6b 20  lue for the sPk 
2f45c 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61  index */.  int a
2f45d 69 43 6f 6c 75 6d 6e 50 6b 20 3d 20 2d 31 3b 20  iColumnPk = -1; 
2f45e 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 43         /* The aC
2f45f 6f 6c 75 6d 6e 5b 5d 20 76 61 6c 75 65 20 66 6f  olumn[] value fo
2f460 72 20 74 68 65 20 73 50 6b 20 69 6e 64 65 78 20  r the sPk index 
2f461 2a 2f 0a 20 20 69 6e 74 20 77 73 46 6c 61 67 4d  */.  int wsFlagM
2f462 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  ask;            
2f463 20 2f 2a 20 41 6c 6c 6f 77 65 64 20 66 6c 61 67   /* Allowed flag
2f464 73 20 69 6e 20 70 43 6f 73 74 2d 3e 70 6c 61 6e  s in pCost->plan
2f465 2e 77 73 46 6c 61 67 20 2a 2f 0a 0a 20 20 2f 2a  .wsFlag */..  /*
2f466 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   Initialize the 
2f467 63 6f 73 74 20 74 6f 20 61 20 77 6f 72 73 74 2d  cost to a worst-
2f468 63 61 73 65 20 76 61 6c 75 65 20 2a 2f 0a 20 20  case value */.  
2f469 6d 65 6d 73 65 74 28 70 43 6f 73 74 2c 20 30 2c  memset(pCost, 0,
2f46a 20 73 69 7a 65 6f 66 28 2a 70 43 6f 73 74 29 29   sizeof(*pCost))
2f46b 3b 0a 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74  ;.  pCost->rCost
2f46c 20 3d 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42   = SQLITE_BIG_DB
2f46d 4c 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  L;..  /* If the 
2f46e 70 53 72 63 20 74 61 62 6c 65 20 69 73 20 74 68  pSrc table is th
2f46f 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66  e right table of
2f470 20 61 20 4c 45 46 54 20 4a 4f 49 4e 20 74 68 65   a LEFT JOIN the
2f471 6e 20 77 65 20 6d 61 79 20 6e 6f 74 0a 20 20 2a  n we may not.  *
2f472 2a 20 75 73 65 20 61 6e 20 69 6e 64 65 78 20 74  * use an index t
2f473 6f 20 73 61 74 69 73 66 79 20 49 53 20 4e 55 4c  o satisfy IS NUL
2f474 4c 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f 6e  L constraints on
2f475 20 74 68 61 74 20 74 61 62 6c 65 2e 20 20 54 68   that table.  Th
2f476 69 73 20 69 73 0a 20 20 2a 2a 20 62 65 63 61 75  is is.  ** becau
2f477 73 65 20 63 6f 6c 75 6d 6e 73 20 6d 69 67 68 74  se columns might
2f478 20 65 6e 64 20 75 70 20 62 65 69 6e 67 20 4e 55   end up being NU
2f479 4c 4c 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  LL if the table 
2f47a 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 2d  does not match -
2f47b 0a 20 20 2a 2a 20 61 20 63 69 72 63 75 6d 73 74  .  ** a circumst
2f47c 61 6e 63 65 20 77 68 69 63 68 20 74 68 65 20 69  ance which the i
2f47d 6e 64 65 78 20 63 61 6e 6e 6f 74 20 68 65 6c 70  ndex cannot help
2f47e 20 75 73 20 64 69 73 63 6f 76 65 72 2e 20 20 54   us discover.  T
2f47f 69 63 6b 65 74 20 23 32 31 37 37 2e 0a 20 20 2a  icket #2177..  *
2f480 2f 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 6a 6f  /.  if( pSrc->jo
2f481 69 6e 74 79 70 65 20 26 20 4a 54 5f 4c 45 46 54  intype & JT_LEFT
2f482 20 29 7b 0a 20 20 20 20 69 64 78 45 71 54 65 72   ){.    idxEqTer
2f483 6d 4d 61 73 6b 20 3d 20 57 4f 5f 45 51 7c 57 4f  mMask = WO_EQ|WO
2f484 5f 49 4e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  _IN;.  }else{.  
2f485 20 20 69 64 78 45 71 54 65 72 6d 4d 61 73 6b 20    idxEqTermMask 
2f486 3d 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 7c 57 4f  = WO_EQ|WO_IN|WO
2f487 5f 49 53 4e 55 4c 4c 3b 0a 20 20 7d 0a 0a 20 20  _ISNULL;.  }..  
2f488 69 66 28 20 70 53 72 63 2d 3e 70 49 6e 64 65 78  if( pSrc->pIndex
2f489 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 49 4e   ){.    /* An IN
2f48a 44 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 20  DEXED BY clause 
2f48b 73 70 65 63 69 66 69 65 73 20 61 20 70 61 72 74  specifies a part
2f48c 69 63 75 6c 61 72 20 69 6e 64 65 78 20 74 6f 20  icular index to 
2f48d 75 73 65 20 2a 2f 0a 20 20 20 20 70 49 64 78 20  use */.    pIdx 
2f48e 3d 20 70 50 72 6f 62 65 20 3d 20 70 53 72 63 2d  = pProbe = pSrc-
2f48f 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 77 73 46  >pIndex;.    wsF
2f490 6c 61 67 4d 61 73 6b 20 3d 20 7e 28 57 48 45 52  lagMask = ~(WHER
2f491 45 5f 52 4f 57 49 44 5f 45 51 7c 57 48 45 52 45  E_ROWID_EQ|WHERE
2f492 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29 3b 0a 20  _ROWID_RANGE);. 
2f493 20 20 20 65 71 54 65 72 6d 4d 61 73 6b 20 3d 20     eqTermMask = 
2f494 69 64 78 45 71 54 65 72 6d 4d 61 73 6b 3b 0a 20  idxEqTermMask;. 
2f495 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54   }else{.    /* T
2f496 68 65 72 65 20 69 73 20 6e 6f 20 49 4e 44 45 58  here is no INDEX
2f497 45 44 20 42 59 20 63 6c 61 75 73 65 2e 20 20 43  ED BY clause.  C
2f498 72 65 61 74 65 20 61 20 66 61 6b 65 20 49 6e 64  reate a fake Ind
2f499 65 78 20 6f 62 6a 65 63 74 20 74 6f 0a 20 20 20  ex object to.   
2f49a 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 20 74 68   ** represent th
2f49b 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f  e primary key */
2f49c 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 46 69 72  .    Index *pFir
2f49d 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  st;             
2f49e 20 20 20 2f 2a 20 41 6e 79 20 6f 74 68 65 72 20     /* Any other 
2f49f 69 6e 64 65 78 20 6f 6e 20 74 68 65 20 74 61 62  index on the tab
2f4a0 6c 65 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74  le */.    memset
2f4a1 28 26 73 50 6b 2c 20 30 2c 20 73 69 7a 65 6f 66  (&sPk, 0, sizeof
2f4a2 28 49 6e 64 65 78 29 29 3b 0a 20 20 20 20 73 50  (Index));.    sP
2f4a3 6b 2e 6e 43 6f 6c 75 6d 6e 20 3d 20 31 3b 0a 20  k.nColumn = 1;. 
2f4a4 20 20 20 73 50 6b 2e 61 69 43 6f 6c 75 6d 6e 20     sPk.aiColumn 
2f4a5 3d 20 26 61 69 43 6f 6c 75 6d 6e 50 6b 3b 0a 20  = &aiColumnPk;. 
2f4a6 20 20 20 73 50 6b 2e 61 69 52 6f 77 45 73 74 20     sPk.aiRowEst 
2f4a7 3d 20 61 69 52 6f 77 45 73 74 50 6b 3b 0a 20 20  = aiRowEstPk;.  
2f4a8 20 20 61 69 52 6f 77 45 73 74 50 6b 5b 31 5d 20    aiRowEstPk[1] 
2f4a9 3d 20 31 3b 0a 20 20 20 20 73 50 6b 2e 6f 6e 45  = 1;.    sPk.onE
2f4aa 72 72 6f 72 20 3d 20 4f 45 5f 52 65 70 6c 61 63  rror = OE_Replac
2f4ab 65 3b 0a 20 20 20 20 73 50 6b 2e 70 54 61 62 6c  e;.    sPk.pTabl
2f4ac 65 20 3d 20 70 53 72 63 2d 3e 70 54 61 62 3b 0a  e = pSrc->pTab;.
2f4ad 20 20 20 20 70 46 69 72 73 74 20 3d 20 70 53 72      pFirst = pSr
2f4ae 63 2d 3e 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  c->pTab->pIndex;
2f4af 0a 20 20 20 20 69 66 28 20 70 53 72 63 2d 3e 6e  .    if( pSrc->n
2f4b0 6f 74 49 6e 64 65 78 65 64 3d 3d 30 20 29 7b 0a  otIndexed==0 ){.
2f4b1 20 20 20 20 20 20 73 50 6b 2e 70 4e 65 78 74 20        sPk.pNext 
2f4b2 3d 20 70 46 69 72 73 74 3b 0a 20 20 20 20 7d 0a  = pFirst;.    }.
2f4b3 20 20 20 20 2f 2a 20 54 68 65 20 61 69 52 6f 77      /* The aiRow
2f4b4 45 73 74 50 6b 5b 30 5d 20 69 73 20 61 6e 20 65  EstPk[0] is an e
2f4b5 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20 74  stimate of the t
2f4b6 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72  otal number of r
2f4b7 6f 77 73 20 69 6e 20 74 68 65 0a 20 20 20 20 2a  ows in the.    *
2f4b8 2a 20 74 61 62 6c 65 2e 20 20 47 65 74 20 74 68  * table.  Get th
2f4b9 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  is information f
2f4ba 72 6f 6d 20 74 68 65 20 41 4e 41 4c 59 5a 45 20  rom the ANALYZE 
2f4bb 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 66 20 69  information if i
2f4bc 74 20 69 73 0a 20 20 20 20 2a 2a 20 61 76 61 69  t is.    ** avai
2f4bd 6c 61 62 6c 65 2e 20 20 49 66 20 6e 6f 74 20 61  lable.  If not a
2f4be 76 61 69 6c 61 62 6c 65 2c 20 61 73 73 75 6d 65  vailable, assume
2f4bf 20 74 68 65 20 74 61 62 6c 65 20 31 20 6d 69 6c   the table 1 mil
2f4c0 6c 69 6f 6e 20 72 6f 77 73 20 69 6e 20 73 69 7a  lion rows in siz
2f4c1 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  e..    */.    if
2f4c2 28 20 70 46 69 72 73 74 20 29 7b 0a 20 20 20 20  ( pFirst ){.    
2f4c3 20 20 61 73 73 65 72 74 28 20 70 46 69 72 73 74    assert( pFirst
2f4c4 2d 3e 61 69 52 6f 77 45 73 74 21 3d 30 20 29 3b  ->aiRowEst!=0 );
2f4c5 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 74 6f   /* Allocated to
2f4c6 67 65 74 68 65 72 20 77 69 74 68 20 70 46 69 72  gether with pFir
2f4c7 73 74 20 2a 2f 0a 20 20 20 20 20 20 61 69 52 6f  st */.      aiRo
2f4c8 77 45 73 74 50 6b 5b 30 5d 20 3d 20 70 46 69 72  wEstPk[0] = pFir
2f4c9 73 74 2d 3e 61 69 52 6f 77 45 73 74 5b 30 5d 3b  st->aiRowEst[0];
2f4ca 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2f4cb 20 20 61 69 52 6f 77 45 73 74 50 6b 5b 30 5d 20    aiRowEstPk[0] 
2f4cc 3d 20 31 30 30 30 30 30 30 3b 0a 20 20 20 20 7d  = 1000000;.    }
2f4cd 0a 20 20 20 20 70 50 72 6f 62 65 20 3d 20 26 73  .    pProbe = &s
2f4ce 50 6b 3b 0a 20 20 20 20 77 73 46 6c 61 67 4d 61  Pk;.    wsFlagMa
2f4cf 73 6b 20 3d 20 7e 28 0a 20 20 20 20 20 20 20 20  sk = ~(.        
2f4d0 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 7c  WHERE_COLUMN_IN|
2f4d1 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51 7c  WHERE_COLUMN_EQ|
2f4d2 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c  WHERE_COLUMN_NUL
2f4d3 4c 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52  L|WHERE_COLUMN_R
2f4d4 41 4e 47 45 0a 20 20 20 20 29 3b 0a 20 20 20 20  ANGE.    );.    
2f4d5 65 71 54 65 72 6d 4d 61 73 6b 20 3d 20 57 4f 5f  eqTermMask = WO_
2f4d6 45 51 7c 57 4f 5f 49 4e 3b 0a 20 20 20 20 70 49  EQ|WO_IN;.    pI
2f4d7 64 78 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  dx = 0;.  }..  /
2f4d8 2a 20 4c 6f 6f 70 20 6f 76 65 72 20 61 6c 6c 20  * Loop over all 
2f4d9 69 6e 64 69 63 65 73 20 6c 6f 6f 6b 69 6e 67 20  indices looking 
2f4da 66 6f 72 20 74 68 65 20 62 65 73 74 20 6f 6e 65  for the best one
2f4db 20 74 6f 20 75 73 65 0a 20 20 2a 2f 0a 20 20 66   to use.  */.  f
2f4dc 6f 72 28 3b 20 70 50 72 6f 62 65 3b 20 70 49 64  or(; pProbe; pId
2f4dd 78 3d 70 50 72 6f 62 65 3d 70 50 72 6f 62 65 2d  x=pProbe=pProbe-
2f4de 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 63 6f 6e  >pNext){.    con
2f4df 73 74 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  st unsigned int 
2f4e0 2a 20 63 6f 6e 73 74 20 61 69 52 6f 77 45 73 74  * const aiRowEst
2f4e1 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69 52 6f 77   = pProbe->aiRow
2f4e2 45 73 74 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20  Est;.    double 
2f4e3 63 6f 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  cost;           
2f4e4 20 20 20 20 20 2f 2a 20 43 6f 73 74 20 6f 66 20       /* Cost of 
2f4e5 75 73 69 6e 67 20 70 50 72 6f 62 65 20 2a 2f 0a  using pProbe */.
2f4e6 20 20 20 20 64 6f 75 62 6c 65 20 6e 52 6f 77 3b      double nRow;
2f4e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4e8 2f 2a 20 45 73 74 69 6d 61 74 65 64 20 6e 75 6d  /* Estimated num
2f4e9 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 72  ber of rows in r
2f4ea 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 20  esult set */.   
2f4eb 20 69 6e 74 20 72 65 76 3b 20 20 20 20 20 20 20   int rev;       
2f4ec 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2f4ed 54 72 75 65 20 74 6f 20 73 63 61 6e 20 69 6e 20  True to scan in 
2f4ee 72 65 76 65 72 73 65 20 6f 72 64 65 72 20 2a 2f  reverse order */
2f4ef 0a 20 20 20 20 69 6e 74 20 77 73 46 6c 61 67 73  .    int wsFlags
2f4f0 20 3d 20 30 3b 0a 20 20 20 20 42 69 74 6d 61 73   = 0;.    Bitmas
2f4f1 6b 20 75 73 65 64 20 3d 20 30 3b 0a 0a 20 20 20  k used = 0;..   
2f4f2 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e   /* The followin
2f4f3 67 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20  g variables are 
2f4f4 70 6f 70 75 6c 61 74 65 64 20 62 61 73 65 64 20  populated based 
2f4f5 6f 6e 20 74 68 65 20 70 72 6f 70 65 72 74 69 65  on the propertie
2f4f6 73 20 6f 66 0a 20 20 20 20 2a 2a 20 73 63 61 6e  s of.    ** scan
2f4f7 20 62 65 69 6e 67 20 65 76 61 6c 75 61 74 65 64   being evaluated
2f4f8 2e 20 54 68 65 79 20 61 72 65 20 74 68 65 6e 20  . They are then 
2f4f9 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
2f4fa 65 20 74 68 65 20 65 78 70 65 63 74 65 64 0a 20  e the expected. 
2f4fb 20 20 20 2a 2a 20 63 6f 73 74 20 61 6e 64 20 6e     ** cost and n
2f4fc 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 72 65  umber of rows re
2f4fd 74 75 72 6e 65 64 2e 0a 20 20 20 20 2a 2a 0a 20  turned..    **. 
2f4fe 20 20 20 2a 2a 20 20 6e 45 71 3a 20 0a 20 20 20     **  nEq: .   
2f4ff 20 2a 2a 20 20 20 20 4e 75 6d 62 65 72 20 6f 66   **    Number of
2f500 20 65 71 75 61 6c 69 74 79 20 74 65 72 6d 73 20   equality terms 
2f501 74 68 61 74 20 63 61 6e 20 62 65 20 69 6d 70 6c  that can be impl
2f502 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 74 68  emented using th
2f503 65 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2a 0a  e index..    **.
2f504 20 20 20 20 2a 2a 20 20 6e 49 6e 4d 75 6c 3a 20      **  nInMul: 
2f505 20 0a 20 20 20 20 2a 2a 20 20 20 20 54 68 65 20   .    **    The 
2f506 22 69 6e 2d 6d 75 6c 74 69 70 6c 69 65 72 22 2e  "in-multiplier".
2f507 20 54 68 69 73 20 69 73 20 61 6e 20 65 73 74 69   This is an esti
2f508 6d 61 74 65 20 6f 66 20 68 6f 77 20 6d 61 6e 79  mate of how many
2f509 20 73 65 65 6b 20 6f 70 65 72 61 74 69 6f 6e 73   seek operations
2f50a 20 0a 20 20 20 20 2a 2a 20 20 20 20 53 51 4c 69   .    **    SQLi
2f50b 74 65 20 6d 75 73 74 20 70 65 72 66 6f 72 6d 20  te must perform 
2f50c 6f 6e 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20  on the index in 
2f50d 71 75 65 73 74 69 6f 6e 2e 20 46 6f 72 20 65 78  question. For ex
2f50e 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 0a 20  ample, if the . 
2f50f 20 20 20 2a 2a 20 20 20 20 57 48 45 52 45 20 63     **    WHERE c
2f510 6c 61 75 73 65 20 69 73 3a 0a 20 20 20 20 2a 2a  lause is:.    **
2f511 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 57 48 45  .    **      WHE
2f512 52 45 20 61 20 49 4e 20 28 31 2c 20 32 2c 20 33  RE a IN (1, 2, 3
2f513 29 20 41 4e 44 20 62 20 49 4e 20 28 34 2c 20 35  ) AND b IN (4, 5
2f514 2c 20 36 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20  , 6).    **.    
2f515 2a 2a 20 20 20 20 53 51 4c 69 74 65 20 6d 75 73  **    SQLite mus
2f516 74 20 70 65 72 66 6f 72 6d 20 39 20 6c 6f 6f 6b  t perform 9 look
2f517 75 70 73 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20  ups on an index 
2f518 6f 6e 20 28 61 2c 20 62 29 2c 20 73 6f 20 6e 49  on (a, b), so nI
2f519 6e 4d 75 6c 20 69 73 20 0a 20 20 20 20 2a 2a 20  nMul is .    ** 
2f51a 20 20 20 73 65 74 20 74 6f 20 39 2e 20 47 69 76     set to 9. Giv
2f51b 65 6e 20 74 68 65 20 73 61 6d 65 20 73 63 68 65  en the same sche
2f51c 6d 61 20 61 6e 64 20 65 69 74 68 65 72 20 6f 66  ma and either of
2f51d 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 57   the following W
2f51e 48 45 52 45 20 0a 20 20 20 20 2a 2a 20 20 20 20  HERE .    **    
2f51f 63 6c 61 75 73 65 73 3a 0a 20 20 20 20 2a 2a 0a  clauses:.    **.
2f520 20 20 20 20 2a 2a 20 20 20 20 20 20 57 48 45 52      **      WHER
2f521 45 20 61 20 3d 20 20 31 0a 20 20 20 20 2a 2a 20  E a =  1.    ** 
2f522 20 20 20 20 20 57 48 45 52 45 20 61 20 3e 3d 20       WHERE a >= 
2f523 32 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  2.    **.    ** 
2f524 20 20 20 6e 49 6e 4d 75 6c 20 69 73 20 73 65 74     nInMul is set
2f525 20 74 6f 20 31 2e 0a 20 20 20 20 2a 2a 0a 20 20   to 1..    **.  
2f526 20 20 2a 2a 20 20 20 20 49 66 20 74 68 65 72 65    **    If there
2f527 20 65 78 69 73 74 73 20 61 20 57 48 45 52 45 20   exists a WHERE 
2f528 74 65 72 6d 20 6f 66 20 74 68 65 20 66 6f 72 6d  term of the form
2f529 20 22 78 20 49 4e 20 28 53 45 4c 45 43 54 20 2e   "x IN (SELECT .
2f52a 2e 2e 29 22 2c 20 74 68 65 6e 20 0a 20 20 20 20  ..)", then .    
2f52b 2a 2a 20 20 20 20 74 68 65 20 73 75 62 2d 73 65  **    the sub-se
2f52c 6c 65 63 74 20 69 73 20 61 73 73 75 6d 65 64 20  lect is assumed 
2f52d 74 6f 20 72 65 74 75 72 6e 20 32 35 20 72 6f 77  to return 25 row
2f52e 73 20 66 6f 72 20 74 68 65 20 70 75 72 70 6f 73  s for the purpos
2f52f 65 73 20 6f 66 20 0a 20 20 20 20 2a 2a 20 20 20  es of .    **   
2f530 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 6e 49 6e   determining nIn
2f531 4d 75 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  Mul..    **.    
2f532 2a 2a 20 20 62 49 6e 45 73 74 3a 20 20 0a 20 20  **  bInEst:  .  
2f533 20 20 2a 2a 20 20 20 20 53 65 74 20 74 6f 20 74    **    Set to t
2f534 72 75 65 20 69 66 20 74 68 65 72 65 20 77 61 73  rue if there was
2f535 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 22 78   at least one "x
2f536 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29   IN (SELECT ...)
2f537 22 20 74 65 72 6d 20 75 73 65 64 20 0a 20 20 20  " term used .   
2f538 20 2a 2a 20 20 20 20 69 6e 20 64 65 74 65 72 6d   **    in determ
2f539 69 6e 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20  ining the value 
2f53a 6f 66 20 6e 49 6e 4d 75 6c 2e 0a 20 20 20 20 2a  of nInMul..    *
2f53b 2a 0a 20 20 20 20 2a 2a 20 20 6e 42 6f 75 6e 64  *.    **  nBound
2f53c 3a 0a 20 20 20 20 2a 2a 20 20 20 20 41 6e 20 65  :.    **    An e
2f53d 73 74 69 6d 61 74 65 20 6f 6e 20 74 68 65 20 61  stimate on the a
2f53e 6d 6f 75 6e 74 20 6f 66 20 74 68 65 20 74 61 62  mount of the tab
2f53f 6c 65 20 74 68 61 74 20 6d 75 73 74 20 62 65 20  le that must be 
2f540 73 65 61 72 63 68 65 64 2e 20 20 41 0a 20 20 20  searched.  A.   
2f541 20 2a 2a 20 20 20 20 76 61 6c 75 65 20 6f 66 20   **    value of 
2f542 31 30 30 20 6d 65 61 6e 73 20 74 68 65 20 65 6e  100 means the en
2f543 74 69 72 65 20 74 61 62 6c 65 20 69 73 20 73 65  tire table is se
2f544 61 72 63 68 65 64 2e 20 20 52 61 6e 67 65 20 63  arched.  Range c
2f545 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 20 20 2a  onstraints.    *
2f546 2a 20 20 20 20 6d 69 67 68 74 20 72 65 64 75 63  *    might reduc
2f547 65 20 74 68 69 73 20 74 6f 20 61 20 76 61 6c 75  e this to a valu
2f548 65 20 6c 65 73 73 20 74 68 61 6e 20 31 30 30 20  e less than 100 
2f549 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74  to indicate that
2f54a 20 6f 6e 6c 79 0a 20 20 20 20 2a 2a 20 20 20 20   only.    **    
2f54b 61 20 66 72 61 63 74 69 6f 6e 20 6f 66 20 74 68  a fraction of th
2f54c 65 20 74 61 62 6c 65 20 6e 65 65 64 73 20 73 65  e table needs se
2f54d 61 72 63 68 69 6e 67 2e 20 20 49 6e 20 74 68 65  arching.  In the
2f54e 20 61 62 73 65 6e 63 65 20 6f 66 0a 20 20 20 20   absence of.    
2f54f 2a 2a 20 20 20 20 73 71 6c 69 74 65 5f 73 74 61  **    sqlite_sta
2f550 74 32 20 41 4e 41 4c 59 5a 45 20 64 61 74 61 2c  t2 ANALYZE data,
2f551 20 61 20 73 69 6e 67 6c 65 20 69 6e 65 71 75 61   a single inequa
2f552 6c 69 74 79 20 72 65 64 75 63 65 73 20 74 68 65  lity reduces the
2f553 20 73 65 61 72 63 68 0a 20 20 20 20 2a 2a 20 20   search.    **  
2f554 20 20 73 70 61 63 65 20 74 6f 20 31 2f 33 72 64    space to 1/3rd
2f555 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 69   its original si
2f556 7a 65 2e 20 20 53 6f 20 61 6e 20 78 3e 3f 20 63  ze.  So an x>? c
2f557 6f 6e 73 74 72 61 69 6e 74 20 72 65 64 75 63 65  onstraint reduce
2f558 73 0a 20 20 20 20 2a 2a 20 20 20 20 6e 42 6f 75  s.    **    nBou
2f559 6e 64 20 74 6f 20 33 33 2e 20 20 54 77 6f 20 63  nd to 33.  Two c
2f55a 6f 6e 73 74 72 61 69 6e 74 73 20 28 78 3e 3f 20  onstraints (x>? 
2f55b 41 4e 44 20 78 3c 3f 29 20 72 65 64 75 63 65 20  AND x<?) reduce 
2f55c 6e 42 6f 75 6e 64 20 74 6f 20 31 31 2e 0a 20 20  nBound to 11..  
2f55d 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 62 53 6f    **.    **  bSo
2f55e 72 74 3a 20 20 20 0a 20 20 20 20 2a 2a 20 20 20  rt:   .    **   
2f55f 20 42 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 20 69   Boolean. True i
2f560 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52  f there is an OR
2f561 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 74 68  DER BY clause th
2f562 61 74 20 77 69 6c 6c 20 72 65 71 75 69 72 65 20  at will require 
2f563 61 6e 20 0a 20 20 20 20 2a 2a 20 20 20 20 65 78  an .    **    ex
2f564 74 65 72 6e 61 6c 20 73 6f 72 74 20 28 69 2e 65  ternal sort (i.e
2f565 2e 20 73 63 61 6e 6e 69 6e 67 20 74 68 65 20 69  . scanning the i
2f566 6e 64 65 78 20 62 65 69 6e 67 20 65 76 61 6c 75  ndex being evalu
2f567 61 74 65 64 20 77 69 6c 6c 20 6e 6f 74 20 0a 20  ated will not . 
2f568 20 20 20 2a 2a 20 20 20 20 63 6f 72 72 65 63 74     **    correct
2f569 6c 79 20 6f 72 64 65 72 20 72 65 63 6f 72 64 73  ly order records
2f56a 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  )..    **.    **
2f56b 20 20 62 4c 6f 6f 6b 75 70 3a 20 0a 20 20 20 20    bLookup: .    
2f56c 2a 2a 20 20 20 20 42 6f 6f 6c 65 61 6e 2e 20 54  **    Boolean. T
2f56d 72 75 65 20 69 66 20 66 6f 72 20 65 61 63 68 20  rue if for each 
2f56e 69 6e 64 65 78 20 65 6e 74 72 79 20 76 69 73 69  index entry visi
2f56f 74 65 64 20 61 20 6c 6f 6f 6b 75 70 20 6f 6e 20  ted a lookup on 
2f570 74 68 65 20 0a 20 20 20 20 2a 2a 20 20 20 20 63  the .    **    c
2f571 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 62  orresponding tab
2f572 6c 65 20 62 2d 74 72 65 65 20 69 73 20 72 65 71  le b-tree is req
2f573 75 69 72 65 64 2e 20 54 68 69 73 20 69 73 20 61  uired. This is a
2f574 6c 77 61 79 73 20 66 61 6c 73 65 20 0a 20 20 20  lways false .   
2f575 20 2a 2a 20 20 20 20 66 6f 72 20 74 68 65 20 72   **    for the r
2f576 6f 77 69 64 20 69 6e 64 65 78 2e 20 46 6f 72 20  owid index. For 
2f577 6f 74 68 65 72 20 69 6e 64 65 78 65 73 2c 20 69  other indexes, i
2f578 74 20 69 73 20 74 72 75 65 20 75 6e 6c 65 73 73  t is true unless
2f579 20 61 6c 6c 20 74 68 65 20 0a 20 20 20 20 2a 2a   all the .    **
2f57a 20 20 20 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74      columns of t
2f57b 68 65 20 74 61 62 6c 65 20 75 73 65 64 20 62 79  he table used by
2f57c 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
2f57d 65 6d 65 6e 74 20 61 72 65 20 70 72 65 73 65 6e  ement are presen
2f57e 74 20 69 6e 20 0a 20 20 20 20 2a 2a 20 20 20 20  t in .    **    
2f57f 74 68 65 20 69 6e 64 65 78 20 28 73 75 63 68 20  the index (such 
2f580 61 6e 20 69 6e 64 65 78 20 69 73 20 73 6f 6d 65  an index is some
2f581 74 69 6d 65 73 20 64 65 73 63 72 69 62 65 64 20  times described 
2f582 61 73 20 61 20 63 6f 76 65 72 69 6e 67 20 69 6e  as a covering in
2f583 64 65 78 29 2e 0a 20 20 20 20 2a 2a 20 20 20 20  dex)..    **    
2f584 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 67 69 76  For example, giv
2f585 65 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 20  en the index on 
2f586 28 61 2c 20 62 29 2c 20 74 68 65 20 73 65 63 6f  (a, b), the seco
2f587 6e 64 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  nd of the follow
2f588 69 6e 67 20 0a 20 20 20 20 2a 2a 20 20 20 20 74  ing .    **    t
2f589 77 6f 20 71 75 65 72 69 65 73 20 72 65 71 75 69  wo queries requi
2f58a 72 65 73 20 74 61 62 6c 65 20 62 2d 74 72 65 65  res table b-tree
2f58b 20 6c 6f 6f 6b 75 70 73 2c 20 62 75 74 20 74 68   lookups, but th
2f58c 65 20 66 69 72 73 74 20 64 6f 65 73 20 6e 6f 74  e first does not
2f58d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
2f58e 20 20 20 20 20 20 20 20 20 20 20 20 53 45 4c 45              SELE
2f58f 43 54 20 61 2c 20 62 20 20 20 20 46 52 4f 4d 20  CT a, b    FROM 
2f590 74 62 6c 20 57 48 45 52 45 20 61 20 3d 20 31 3b  tbl WHERE a = 1;
2f591 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2f592 20 20 20 20 53 45 4c 45 43 54 20 61 2c 20 62 2c      SELECT a, b,
2f593 20 63 20 46 52 4f 4d 20 74 62 6c 20 57 48 45 52   c FROM tbl WHER
2f594 45 20 61 20 3d 20 31 3b 0a 20 20 20 20 2a 2f 0a  E a = 1;.    */.
2f595 20 20 20 20 69 6e 74 20 6e 45 71 3b 0a 20 20 20      int nEq;.   
2f596 20 69 6e 74 20 62 49 6e 45 73 74 20 3d 20 30 3b   int bInEst = 0;
2f597 0a 20 20 20 20 69 6e 74 20 6e 49 6e 4d 75 6c 20  .    int nInMul 
2f598 3d 20 31 3b 0a 20 20 20 20 69 6e 74 20 6e 42 6f  = 1;.    int nBo
2f599 75 6e 64 20 3d 20 31 30 30 3b 0a 20 20 20 20 69  und = 100;.    i
2f59a 6e 74 20 62 53 6f 72 74 20 3d 20 30 3b 0a 20 20  nt bSort = 0;.  
2f59b 20 20 69 6e 74 20 62 4c 6f 6f 6b 75 70 20 3d 20    int bLookup = 
2f59c 30 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 74 65 72  0;..    /* Deter
2f59d 6d 69 6e 65 20 74 68 65 20 76 61 6c 75 65 73 20  mine the values 
2f59e 6f 66 20 6e 45 71 20 61 6e 64 20 6e 49 6e 4d 75  of nEq and nInMu
2f59f 6c 20 2a 2f 0a 20 20 20 20 66 6f 72 28 6e 45 71  l */.    for(nEq
2f5a0 3d 30 3b 20 6e 45 71 3c 70 50 72 6f 62 65 2d 3e  =0; nEq<pProbe->
2f5a1 6e 43 6f 6c 75 6d 6e 3b 20 6e 45 71 2b 2b 29 7b  nColumn; nEq++){
2f5a2 0a 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d  .      WhereTerm
2f5a3 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20   *pTerm;        
2f5a4 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 74     /* A single t
2f5a5 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45  erm of the WHERE
2f5a6 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20   clause */.     
2f5a7 20 69 6e 74 20 6a 20 3d 20 70 50 72 6f 62 65 2d   int j = pProbe-
2f5a8 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45 71 5d 3b 0a  >aiColumn[nEq];.
2f5a9 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20 66 69        pTerm = fi
2f5aa 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72  ndTerm(pWC, iCur
2f5ab 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20 65  , j, notReady, e
2f5ac 71 54 65 72 6d 4d 61 73 6b 2c 20 70 49 64 78 29  qTermMask, pIdx)
2f5ad 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 65 72  ;.      if( pTer
2f5ae 6d 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20  m==0 ) break;.  
2f5af 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 28      wsFlags |= (
2f5b0 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51 7c  WHERE_COLUMN_EQ|
2f5b1 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 29 3b  WHERE_ROWID_EQ);
2f5b2 0a 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d  .      if( pTerm
2f5b3 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f  ->eOperator & WO
2f5b4 5f 49 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 45  _IN ){.        E
2f5b5 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 54 65  xpr *pExpr = pTe
2f5b6 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20  rm->pExpr;.     
2f5b7 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48     wsFlags |= WH
2f5b8 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 3b 0a 20  ERE_COLUMN_IN;. 
2f5b9 20 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48         if( ExprH
2f5ba 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
2f5bb 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
2f5bc 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 49 6e  ){.          nIn
2f5bd 4d 75 6c 20 2a 3d 20 32 35 3b 0a 20 20 20 20 20  Mul *= 25;.     
2f5be 20 20 20 20 20 62 49 6e 45 73 74 20 3d 20 31 3b       bInEst = 1;
2f5bf 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
2f5c0 66 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73  f( pExpr->x.pLis
2f5c1 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  t ){.          n
2f5c2 49 6e 4d 75 6c 20 2a 3d 20 70 45 78 70 72 2d 3e  InMul *= pExpr->
2f5c3 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 2b  x.pList->nExpr +
2f5c4 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
2f5c5 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54      }else if( pT
2f5c6 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26  erm->eOperator &
2f5c7 20 57 4f 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20   WO_ISNULL ){.  
2f5c8 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d        wsFlags |=
2f5c9 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55   WHERE_COLUMN_NU
2f5ca 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  LL;.      }.    
2f5cb 20 20 75 73 65 64 20 7c 3d 20 70 54 65 72 6d 2d    used |= pTerm-
2f5cc 3e 70 72 65 72 65 71 52 69 67 68 74 3b 0a 20 20  >prereqRight;.  
2f5cd 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 74 65    }..    /* Dete
2f5ce 72 6d 69 6e 65 20 74 68 65 20 76 61 6c 75 65 20  rmine the value 
2f5cf 6f 66 20 6e 42 6f 75 6e 64 2e 20 2a 2f 0a 20 20  of nBound. */.  
2f5d0 20 20 69 66 28 20 6e 45 71 3c 70 50 72 6f 62 65    if( nEq<pProbe
2f5d1 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->nColumn ){.   
2f5d2 20 20 20 69 6e 74 20 6a 20 3d 20 70 50 72 6f 62     int j = pProb
2f5d3 65 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45 71 5d  e->aiColumn[nEq]
2f5d4 3b 0a 20 20 20 20 20 20 69 66 28 20 66 69 6e 64  ;.      if( find
2f5d5 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20  Term(pWC, iCur, 
2f5d6 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f  j, notReady, WO_
2f5d7 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57  LT|WO_LE|WO_GT|W
2f5d8 4f 5f 47 45 2c 20 70 49 64 78 29 20 29 7b 0a 20  O_GE, pIdx) ){. 
2f5d9 20 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d         WhereTerm
2f5da 20 2a 70 54 6f 70 20 3d 20 66 69 6e 64 54 65 72   *pTop = findTer
2f5db 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20  m(pWC, iCur, j, 
2f5dc 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c  notReady, WO_LT|
2f5dd 57 4f 5f 4c 45 2c 20 70 49 64 78 29 3b 0a 20 20  WO_LE, pIdx);.  
2f5de 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20        WhereTerm 
2f5df 2a 70 42 74 6d 20 3d 20 66 69 6e 64 54 65 72 6d  *pBtm = findTerm
2f5e0 28 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e  (pWC, iCur, j, n
2f5e1 6f 74 52 65 61 64 79 2c 20 57 4f 5f 47 54 7c 57  otReady, WO_GT|W
2f5e2 4f 5f 47 45 2c 20 70 49 64 78 29 3b 0a 20 20 20  O_GE, pIdx);.   
2f5e3 20 20 20 20 20 77 68 65 72 65 52 61 6e 67 65 53       whereRangeS
2f5e4 63 61 6e 45 73 74 28 70 50 61 72 73 65 2c 20 70  canEst(pParse, p
2f5e5 50 72 6f 62 65 2c 20 6e 45 71 2c 20 70 42 74 6d  Probe, nEq, pBtm
2f5e6 2c 20 70 54 6f 70 2c 20 26 6e 42 6f 75 6e 64 29  , pTop, &nBound)
2f5e7 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54  ;.        if( pT
2f5e8 6f 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  op ){.          
2f5e9 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45  wsFlags |= WHERE
2f5ea 5f 54 4f 50 5f 4c 49 4d 49 54 3b 0a 20 20 20 20  _TOP_LIMIT;.    
2f5eb 20 20 20 20 20 20 75 73 65 64 20 7c 3d 20 70 54        used |= pT
2f5ec 6f 70 2d 3e 70 72 65 72 65 71 52 69 67 68 74 3b  op->prereqRight;
2f5ed 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2f5ee 20 20 20 69 66 28 20 70 42 74 6d 20 29 7b 0a 20     if( pBtm ){. 
2f5ef 20 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73           wsFlags
2f5f0 20 7c 3d 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49   |= WHERE_BTM_LI
2f5f1 4d 49 54 3b 0a 20 20 20 20 20 20 20 20 20 20 75  MIT;.          u
2f5f2 73 65 64 20 7c 3d 20 70 42 74 6d 2d 3e 70 72 65  sed |= pBtm->pre
2f5f3 72 65 71 52 69 67 68 74 3b 0a 20 20 20 20 20 20  reqRight;.      
2f5f4 20 20 7d 0a 20 20 20 20 20 20 20 20 77 73 46 6c    }.        wsFl
2f5f5 61 67 73 20 7c 3d 20 28 57 48 45 52 45 5f 43 4f  ags |= (WHERE_CO
2f5f6 4c 55 4d 4e 5f 52 41 4e 47 45 7c 57 48 45 52 45  LUMN_RANGE|WHERE
2f5f7 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29 3b 0a 20  _ROWID_RANGE);. 
2f5f8 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
2f5f9 20 69 66 28 20 70 50 72 6f 62 65 2d 3e 6f 6e 45   if( pProbe->onE
2f5fa 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b  rror!=OE_None ){
2f5fb 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2f5fc 20 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45   wsFlags & WHERE
2f5fd 5f 43 4f 4c 55 4d 4e 5f 49 4e 20 29 3b 0a 20 20  _COLUMN_IN );.  
2f5fe 20 20 20 20 74 65 73 74 63 61 73 65 28 20 77 73      testcase( ws
2f5ff 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 43 4f  Flags & WHERE_CO
2f600 4c 55 4d 4e 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20  LUMN_NULL );.   
2f601 20 20 20 69 66 28 20 28 77 73 46 6c 61 67 73 20     if( (wsFlags 
2f602 26 20 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  & (WHERE_COLUMN_
2f603 49 4e 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  IN|WHERE_COLUMN_
2f604 4e 55 4c 4c 29 29 3d 3d 30 20 29 7b 0a 20 20 20  NULL))==0 ){.   
2f605 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20       wsFlags |= 
2f606 57 48 45 52 45 5f 55 4e 49 51 55 45 3b 0a 20 20  WHERE_UNIQUE;.  
2f607 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
2f608 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
2f609 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  an ORDER BY clau
2f60a 73 65 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78  se and the index
2f60b 20 62 65 69 6e 67 20 63 6f 6e 73 69 64 65 72 65   being considere
2f60c 64 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 6e 61  d will.    ** na
2f60d 74 75 72 61 6c 6c 79 20 73 63 61 6e 20 72 6f 77  turally scan row
2f60e 73 20 69 6e 20 74 68 65 20 72 65 71 75 69 72 65  s in the require
2f60f 64 20 6f 72 64 65 72 2c 20 73 65 74 20 74 68 65  d order, set the
2f610 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6c 61   appropriate fla
2f611 67 73 0a 20 20 20 20 2a 2a 20 69 6e 20 77 73 46  gs.    ** in wsF
2f612 6c 61 67 73 2e 20 4f 74 68 65 72 77 69 73 65 2c  lags. Otherwise,
2f613 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20   if there is an 
2f614 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2f615 62 75 74 20 74 68 65 20 69 6e 64 65 78 0a 20 20  but the index.  
2f616 20 20 2a 2a 20 77 69 6c 6c 20 73 63 61 6e 20 72    ** will scan r
2f617 6f 77 73 20 69 6e 20 61 20 64 69 66 66 65 72 65  ows in a differe
2f618 6e 74 20 6f 72 64 65 72 2c 20 73 65 74 20 74 68  nt order, set th
2f619 65 20 62 53 6f 72 74 20 76 61 72 69 61 62 6c 65  e bSort variable
2f61a 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f  .  */.    if( pO
2f61b 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20  rderBy ){.      
2f61c 69 66 28 20 28 77 73 46 6c 61 67 73 20 26 20 28  if( (wsFlags & (
2f61d 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 7c  WHERE_COLUMN_IN|
2f61e 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c  WHERE_COLUMN_NUL
2f61f 4c 29 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 26  L))==0.        &
2f620 26 20 69 73 53 6f 72 74 69 6e 67 49 6e 64 65 78  & isSortingIndex
2f621 28 70 50 61 72 73 65 2c 70 57 43 2d 3e 70 4d 61  (pParse,pWC->pMa
2f622 73 6b 53 65 74 2c 70 50 72 6f 62 65 2c 69 43 75  skSet,pProbe,iCu
2f623 72 2c 70 4f 72 64 65 72 42 79 2c 6e 45 71 2c 26  r,pOrderBy,nEq,&
2f624 72 65 76 29 0a 20 20 20 20 20 20 29 7b 0a 20 20  rev).      ){.  
2f625 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d        wsFlags |=
2f626 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e   WHERE_ROWID_RAN
2f627 47 45 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  GE|WHERE_COLUMN_
2f628 52 41 4e 47 45 7c 57 48 45 52 45 5f 4f 52 44 45  RANGE|WHERE_ORDE
2f629 52 42 59 3b 0a 20 20 20 20 20 20 20 20 77 73 46  RBY;.        wsF
2f62a 6c 61 67 73 20 7c 3d 20 28 72 65 76 20 3f 20 57  lags |= (rev ? W
2f62b 48 45 52 45 5f 52 45 56 45 52 53 45 20 3a 20 30  HERE_REVERSE : 0
2f62c 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
2f62d 20 20 20 20 20 20 20 20 62 53 6f 72 74 20 3d 20          bSort = 
2f62e 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
2f62f 0a 0a 20 20 20 20 2f 2a 20 49 66 20 63 75 72 72  ..    /* If curr
2f630 65 6e 74 6c 79 20 63 61 6c 63 75 6c 61 74 69 6e  ently calculatin
2f631 67 20 74 68 65 20 63 6f 73 74 20 6f 66 20 75 73  g the cost of us
2f632 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 28 6e 6f  ing an index (no
2f633 74 20 74 68 65 20 49 50 4b 0a 20 20 20 20 2a 2a  t the IPK.    **
2f634 20 69 6e 64 65 78 29 2c 20 64 65 74 65 72 6d 69   index), determi
2f635 6e 65 20 69 66 20 61 6c 6c 20 72 65 71 75 69 72  ne if all requir
2f636 65 64 20 63 6f 6c 75 6d 6e 20 64 61 74 61 20 6d  ed column data m
2f637 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64 20 77  ay be obtained w
2f638 69 74 68 6f 75 74 20 0a 20 20 20 20 2a 2a 20 73  ithout .    ** s
2f639 65 65 6b 69 6e 67 20 74 6f 20 65 6e 74 72 69 65  eeking to entrie
2f63a 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 74 61  s in the main ta
2f63b 62 6c 65 20 28 69 2e 65 2e 20 69 66 20 74 68 65  ble (i.e. if the
2f63c 20 69 6e 64 65 78 20 69 73 20 61 20 63 6f 76 65   index is a cove
2f63d 72 69 6e 67 0a 20 20 20 20 2a 2a 20 69 6e 64 65  ring.    ** inde
2f63e 78 20 66 6f 72 20 74 68 69 73 20 71 75 65 72 79  x for this query
2f63f 29 2e 20 49 66 20 69 74 20 69 73 2c 20 73 65 74  ). If it is, set
2f640 20 74 68 65 20 57 48 45 52 45 5f 49 44 58 5f 4f   the WHERE_IDX_O
2f641 4e 4c 59 20 66 6c 61 67 20 69 6e 0a 20 20 20 20  NLY flag in.    
2f642 2a 2a 20 77 73 46 6c 61 67 73 2e 20 4f 74 68 65  ** wsFlags. Othe
2f643 72 77 69 73 65 2c 20 73 65 74 20 74 68 65 20 62  rwise, set the b
2f644 4c 6f 6f 6b 75 70 20 76 61 72 69 61 62 6c 65 20  Lookup variable 
2f645 74 6f 20 74 72 75 65 2e 20 20 2a 2f 0a 20 20 20  to true.  */.   
2f646 20 69 66 28 20 70 49 64 78 20 26 26 20 77 73 46   if( pIdx && wsF
2f647 6c 61 67 73 20 29 7b 0a 20 20 20 20 20 20 42 69  lags ){.      Bi
2f648 74 6d 61 73 6b 20 6d 20 3d 20 70 53 72 63 2d 3e  tmask m = pSrc->
2f649 63 6f 6c 55 73 65 64 3b 0a 20 20 20 20 20 20 69  colUsed;.      i
2f64a 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28  nt j;.      for(
2f64b 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f  j=0; j<pIdx->nCo
2f64c 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  lumn; j++){.    
2f64d 20 20 20 20 69 6e 74 20 78 20 3d 20 70 49 64 78      int x = pIdx
2f64e 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20  ->aiColumn[j];. 
2f64f 20 20 20 20 20 20 20 69 66 28 20 78 3c 42 4d 53         if( x<BMS
2f650 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  -1 ){.          
2f651 6d 20 26 3d 20 7e 28 28 28 42 69 74 6d 61 73 6b  m &= ~(((Bitmask
2f652 29 31 29 3c 3c 78 29 3b 0a 20 20 20 20 20 20 20  )1)<<x);.       
2f653 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
2f654 20 69 66 28 20 6d 3d 3d 30 20 29 7b 0a 20 20 20   if( m==0 ){.   
2f655 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20       wsFlags |= 
2f656 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 3b 0a  WHERE_IDX_ONLY;.
2f657 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2f658 20 20 20 20 20 62 4c 6f 6f 6b 75 70 20 3d 20 31       bLookup = 1
2f659 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2f65a 0a 20 20 20 20 2f 2a 2a 2a 2a 20 42 65 67 69 6e  .    /**** Begin
2f65b 20 61 64 64 69 6e 67 20 75 70 20 74 68 65 20 63   adding up the c
2f65c 6f 73 74 20 6f 66 20 75 73 69 6e 67 20 74 68 69  ost of using thi
2f65d 73 20 69 6e 64 65 78 20 28 4e 65 65 64 73 20 69  s index (Needs i
2f65e 6d 70 72 6f 76 65 6d 65 6e 74 73 29 0a 20 20 20  mprovements).   
2f65f 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 73 74 69 6d   **.    ** Estim
2f660 61 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ate the number o
2f661 66 20 72 6f 77 73 20 6f 66 20 6f 75 74 70 75 74  f rows of output
2f662 2e 20 20 46 6f 72 20 61 6e 20 49 4e 20 6f 70 65  .  For an IN ope
2f663 72 61 74 6f 72 2c 0a 20 20 20 20 2a 2a 20 64 6f  rator,.    ** do
2f664 20 6e 6f 74 20 6c 65 74 20 74 68 65 20 65 73 74   not let the est
2f665 69 6d 61 74 65 20 65 78 63 65 65 64 20 68 61 6c  imate exceed hal
2f666 66 20 74 68 65 20 72 6f 77 73 20 69 6e 20 74 68  f the rows in th
2f667 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a  e table..    */.
2f668 20 20 20 20 6e 52 6f 77 20 3d 20 28 64 6f 75 62      nRow = (doub
2f669 6c 65 29 28 61 69 52 6f 77 45 73 74 5b 6e 45 71  le)(aiRowEst[nEq
2f66a 5d 20 2a 20 6e 49 6e 4d 75 6c 29 3b 0a 20 20 20  ] * nInMul);.   
2f66b 20 69 66 28 20 62 49 6e 45 73 74 20 26 26 20 6e   if( bInEst && n
2f66c 52 6f 77 2a 32 3e 61 69 52 6f 77 45 73 74 5b 30  Row*2>aiRowEst[0
2f66d 5d 20 29 7b 0a 20 20 20 20 20 20 6e 52 6f 77 20  ] ){.      nRow 
2f66e 3d 20 61 69 52 6f 77 45 73 74 5b 30 5d 2f 32 3b  = aiRowEst[0]/2;
2f66f 0a 20 20 20 20 20 20 6e 49 6e 4d 75 6c 20 3d 20  .      nInMul = 
2f670 28 69 6e 74 29 28 6e 52 6f 77 20 2f 20 61 69 52  (int)(nRow / aiR
2f671 6f 77 45 73 74 5b 6e 45 71 5d 29 3b 0a 20 20 20  owEst[nEq]);.   
2f672 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 73 73 75 6d   }..    /* Assum
2f673 65 20 63 6f 6e 73 74 61 6e 74 20 63 6f 73 74 20  e constant cost 
2f674 74 6f 20 61 63 63 65 73 73 20 61 20 72 6f 77 20  to access a row 
2f675 61 6e 64 20 6c 6f 67 61 72 69 74 68 6d 69 63 20  and logarithmic 
2f676 63 6f 73 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64  cost to.    ** d
2f677 6f 20 61 20 62 69 6e 61 72 79 20 73 65 61 72 63  o a binary searc
2f678 68 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 69  h.  Hence, the i
2f679 6e 69 74 69 61 6c 20 63 6f 73 74 20 69 73 20 74  nitial cost is t
2f67a 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74  he number of out
2f67b 70 75 74 0a 20 20 20 20 2a 2a 20 72 6f 77 73 20  put.    ** rows 
2f67c 70 6c 75 73 20 6c 6f 67 32 28 74 61 62 6c 65 2d  plus log2(table-
2f67d 73 69 7a 65 29 20 74 69 6d 65 73 20 74 68 65 20  size) times the 
2f67e 6e 75 6d 62 65 72 20 6f 66 20 62 69 6e 61 72 79  number of binary
2f67f 20 73 65 61 72 63 68 65 73 2e 0a 20 20 20 20 2a   searches..    *
2f680 2f 0a 20 20 20 20 63 6f 73 74 20 3d 20 6e 52 6f  /.    cost = nRo
2f681 77 20 2b 20 6e 49 6e 4d 75 6c 2a 65 73 74 4c 6f  w + nInMul*estLo
2f682 67 28 61 69 52 6f 77 45 73 74 5b 30 5d 29 3b 0a  g(aiRowEst[0]);.
2f683 0a 20 20 20 20 2f 2a 20 41 64 6a 75 73 74 20 74  .    /* Adjust t
2f684 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
2f685 73 20 61 6e 64 20 74 68 65 20 63 6f 73 74 20 64  s and the cost d
2f686 6f 77 6e 77 61 72 64 20 74 6f 20 72 65 66 6c 65  ownward to refle
2f687 63 74 20 72 6f 77 73 0a 20 20 20 20 2a 2a 20 74  ct rows.    ** t
2f688 68 61 74 20 61 72 65 20 65 78 63 6c 75 64 65 64  hat are excluded
2f689 20 62 79 20 72 61 6e 67 65 20 63 6f 6e 73 74 72   by range constr
2f68a 61 69 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  aints..    */.  
2f68b 20 20 6e 52 6f 77 20 3d 20 28 6e 52 6f 77 20 2a    nRow = (nRow *
2f68c 20 28 64 6f 75 62 6c 65 29 6e 42 6f 75 6e 64 29   (double)nBound)
2f68d 20 2f 20 28 64 6f 75 62 6c 65 29 31 30 30 3b 0a   / (double)100;.
2f68e 20 20 20 20 63 6f 73 74 20 3d 20 28 63 6f 73 74      cost = (cost
2f68f 20 2a 20 28 64 6f 75 62 6c 65 29 6e 42 6f 75 6e   * (double)nBoun
2f690 64 29 20 2f 20 28 64 6f 75 62 6c 65 29 31 30 30  d) / (double)100
2f691 3b 0a 0a 20 20 20 20 2f 2a 20 41 64 64 20 69 6e  ;..    /* Add in
2f692 20 74 68 65 20 65 73 74 69 6d 61 74 65 64 20 63   the estimated c
2f693 6f 73 74 20 6f 66 20 73 6f 72 74 69 6e 67 20 74  ost of sorting t
2f694 68 65 20 72 65 73 75 6c 74 0a 20 20 20 20 2a 2f  he result.    */
2f695 0a 20 20 20 20 69 66 28 20 62 53 6f 72 74 20 29  .    if( bSort )
2f696 7b 0a 20 20 20 20 20 20 63 6f 73 74 20 2b 3d 20  {.      cost += 
2f697 63 6f 73 74 2a 65 73 74 4c 6f 67 28 63 6f 73 74  cost*estLog(cost
2f698 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
2f699 20 49 66 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74   If all informat
2f69a 69 6f 6e 20 63 61 6e 20 62 65 20 74 61 6b 65 6e  ion can be taken
2f69b 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 74   directly from t
2f69c 68 65 20 69 6e 64 65 78 2c 20 77 65 20 61 76 6f  he index, we avo
2f69d 69 64 0a 20 20 20 20 2a 2a 20 64 6f 69 6e 67 20  id.    ** doing 
2f69e 74 61 62 6c 65 20 6c 6f 6f 6b 75 70 73 2e 20 20  table lookups.  
2f69f 54 68 69 73 20 72 65 64 75 63 65 73 20 74 68 65  This reduces the
2f6a0 20 63 6f 73 74 20 62 79 20 68 61 6c 66 2e 20 20   cost by half.  
2f6a1 28 4e 6f 74 20 72 65 61 6c 6c 79 20 2d 0a 20 20  (Not really -.  
2f6a2 20 20 2a 2a 20 74 68 69 73 20 6e 65 65 64 73 20    ** this needs 
2f6a3 74 6f 20 62 65 20 66 69 78 65 64 2e 29 0a 20 20  to be fixed.).  
2f6a4 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 49 64    */.    if( pId
2f6a5 78 20 26 26 20 62 4c 6f 6f 6b 75 70 3d 3d 30 20  x && bLookup==0 
2f6a6 29 7b 0a 20 20 20 20 20 20 63 6f 73 74 20 2f 3d  ){.      cost /=
2f6a7 20 28 64 6f 75 62 6c 65 29 32 3b 0a 20 20 20 20   (double)2;.    
2f6a8 7d 0a 20 20 20 20 2f 2a 2a 2a 2a 20 43 6f 73 74  }.    /**** Cost
2f6a9 20 6f 66 20 75 73 69 6e 67 20 74 68 69 73 20 69   of using this i
2f6aa 6e 64 65 78 20 68 61 73 20 6e 6f 77 20 62 65 65  ndex has now bee
2f6ab 6e 20 63 6f 6d 70 75 74 65 64 20 2a 2a 2a 2a 2f  n computed ****/
2f6ac 0a 0a 20 20 20 20 57 48 45 52 45 54 52 41 43 45  ..    WHERETRACE
2f6ad 28 28 0a 20 20 20 20 20 20 22 74 62 6c 3d 25 73  ((.      "tbl=%s
2f6ae 20 69 64 78 3d 25 73 20 6e 45 71 3d 25 64 20 6e   idx=%s nEq=%d n
2f6af 49 6e 4d 75 6c 3d 25 64 20 6e 42 6f 75 6e 64 3d  InMul=%d nBound=
2f6b0 25 64 20 62 53 6f 72 74 3d 25 64 20 62 4c 6f 6f  %d bSort=%d bLoo
2f6b1 6b 75 70 3d 25 64 22 0a 20 20 20 20 20 20 22 20  kup=%d".      " 
2f6b2 77 73 46 6c 61 67 73 3d 25 64 20 20 20 28 6e 52  wsFlags=%d   (nR
2f6b3 6f 77 3d 25 2e 32 66 20 63 6f 73 74 3d 25 2e 32  ow=%.2f cost=%.2
2f6b4 66 29 5c 6e 22 2c 0a 20 20 20 20 20 20 70 53 72  f)\n",.      pSr
2f6b5 63 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  c->pTab->zName, 
2f6b6 28 70 49 64 78 20 3f 20 70 49 64 78 2d 3e 7a 4e  (pIdx ? pIdx->zN
2f6b7 61 6d 65 20 3a 20 22 69 70 6b 22 29 2c 20 0a 20  ame : "ipk"), . 
2f6b8 20 20 20 20 20 6e 45 71 2c 20 6e 49 6e 4d 75 6c       nEq, nInMul
2f6b9 2c 20 6e 42 6f 75 6e 64 2c 20 62 53 6f 72 74 2c  , nBound, bSort,
2f6ba 20 62 4c 6f 6f 6b 75 70 2c 20 77 73 46 6c 61 67   bLookup, wsFlag
2f6bb 73 2c 20 6e 52 6f 77 2c 20 63 6f 73 74 0a 20 20  s, nRow, cost.  
2f6bc 20 20 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66    ));..    /* If
2f6bd 20 74 68 69 73 20 69 6e 64 65 78 20 69 73 20 74   this index is t
2f6be 68 65 20 62 65 73 74 20 77 65 20 68 61 76 65 20  he best we have 
2f6bf 73 65 65 6e 20 73 6f 20 66 61 72 2c 20 74 68 65  seen so far, the
2f6c0 6e 20 72 65 63 6f 72 64 20 74 68 69 73 0a 20 20  n record this.  
2f6c1 20 20 2a 2a 20 69 6e 64 65 78 20 61 6e 64 20 69    ** index and i
2f6c2 74 73 20 63 6f 73 74 20 69 6e 20 74 68 65 20 70  ts cost in the p
2f6c3 43 6f 73 74 20 73 74 72 75 63 74 75 72 65 2e 0a  Cost structure..
2f6c4 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28      */.    if( (
2f6c5 21 70 49 64 78 20 7c 7c 20 77 73 46 6c 61 67 73  !pIdx || wsFlags
2f6c6 29 20 26 26 20 63 6f 73 74 3c 70 43 6f 73 74 2d  ) && cost<pCost-
2f6c7 3e 72 43 6f 73 74 20 29 7b 0a 20 20 20 20 20 20  >rCost ){.      
2f6c8 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20 63  pCost->rCost = c
2f6c9 6f 73 74 3b 0a 20 20 20 20 20 20 70 43 6f 73 74  ost;.      pCost
2f6ca 2d 3e 6e 52 6f 77 20 3d 20 6e 52 6f 77 3b 0a 20  ->nRow = nRow;. 
2f6cb 20 20 20 20 20 70 43 6f 73 74 2d 3e 75 73 65 64       pCost->used
2f6cc 20 3d 20 75 73 65 64 3b 0a 20 20 20 20 20 20 70   = used;.      p
2f6cd 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  Cost->plan.wsFla
2f6ce 67 73 20 3d 20 28 77 73 46 6c 61 67 73 26 77 73  gs = (wsFlags&ws
2f6cf 46 6c 61 67 4d 61 73 6b 29 3b 0a 20 20 20 20 20  FlagMask);.     
2f6d0 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 6e 45 71   pCost->plan.nEq
2f6d1 20 3d 20 6e 45 71 3b 0a 20 20 20 20 20 20 70 43   = nEq;.      pC
2f6d2 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78  ost->plan.u.pIdx
2f6d3 20 3d 20 70 49 64 78 3b 0a 20 20 20 20 7d 0a 0a   = pIdx;.    }..
2f6d4 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
2f6d5 77 61 73 20 61 6e 20 49 4e 44 45 58 45 44 20 42  was an INDEXED B
2f6d6 59 20 63 6c 61 75 73 65 2c 20 74 68 65 6e 20 6f  Y clause, then o
2f6d7 6e 6c 79 20 74 68 61 74 20 6f 6e 65 20 69 6e 64  nly that one ind
2f6d8 65 78 20 69 73 0a 20 20 20 20 2a 2a 20 63 6f 6e  ex is.    ** con
2f6d9 73 69 64 65 72 65 64 2e 20 2a 2f 0a 20 20 20 20  sidered. */.    
2f6da 69 66 28 20 70 53 72 63 2d 3e 70 49 6e 64 65 78  if( pSrc->pIndex
2f6db 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 2f   ) break;..    /
2f6dc 2a 20 52 65 73 65 74 20 6d 61 73 6b 73 20 66 6f  * Reset masks fo
2f6dd 72 20 74 68 65 20 6e 65 78 74 20 69 6e 64 65 78  r the next index
2f6de 20 69 6e 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a   in the loop */.
2f6df 20 20 20 20 77 73 46 6c 61 67 4d 61 73 6b 20 3d      wsFlagMask =
2f6e0 20 7e 28 57 48 45 52 45 5f 52 4f 57 49 44 5f 45   ~(WHERE_ROWID_E
2f6e1 51 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41  Q|WHERE_ROWID_RA
2f6e2 4e 47 45 29 3b 0a 20 20 20 20 65 71 54 65 72 6d  NGE);.    eqTerm
2f6e3 4d 61 73 6b 20 3d 20 69 64 78 45 71 54 65 72 6d  Mask = idxEqTerm
2f6e4 4d 61 73 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Mask;.  }..  /* 
2f6e5 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4f  If there is no O
2f6e6 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 61  RDER BY clause a
2f6e7 6e 64 20 74 68 65 20 53 51 4c 49 54 45 5f 52 65  nd the SQLITE_Re
2f6e8 76 65 72 73 65 4f 72 64 65 72 20 66 6c 61 67 0a  verseOrder flag.
2f6e9 20 20 2a 2a 20 69 73 20 73 65 74 2c 20 74 68 65    ** is set, the
2f6ea 6e 20 72 65 76 65 72 73 65 20 74 68 65 20 6f 72  n reverse the or
2f6eb 64 65 72 20 74 68 61 74 20 74 68 65 20 69 6e 64  der that the ind
2f6ec 65 78 20 77 69 6c 6c 20 62 65 20 73 63 61 6e 6e  ex will be scann
2f6ed 65 64 0a 20 20 2a 2a 20 69 6e 2e 20 54 68 69 73  ed.  ** in. This
2f6ee 20 69 73 20 75 73 65 64 20 66 6f 72 20 61 70 70   is used for app
2f6ef 6c 69 63 61 74 69 6f 6e 20 74 65 73 74 69 6e 67  lication testing
2f6f0 2c 20 74 6f 20 68 65 6c 70 20 66 69 6e 64 20 63  , to help find c
2f6f1 61 73 65 73 0a 20 20 2a 2a 20 77 68 65 72 65 20  ases.  ** where 
2f6f2 61 70 70 6c 69 63 61 74 69 6f 6e 20 62 65 68 61  application beha
2f6f3 76 69 6f 75 72 20 64 65 70 65 6e 64 73 20 6f 6e  viour depends on
2f6f4 20 74 68 65 20 28 75 6e 64 65 66 69 6e 65 64 29   the (undefined)
2f6f5 20 6f 72 64 65 72 20 74 68 61 74 0a 20 20 2a 2a   order that.  **
2f6f6 20 53 51 4c 69 74 65 20 6f 75 74 70 75 74 73 20   SQLite outputs 
2f6f7 72 6f 77 73 20 69 6e 20 69 6e 20 74 68 65 20 61  rows in in the a
2f6f8 62 73 65 6e 63 65 20 6f 66 20 61 6e 20 4f 52 44  bsence of an ORD
2f6f9 45 52 20 42 59 20 63 6c 61 75 73 65 2e 20 20 2a  ER BY clause.  *
2f6fa 2f 0a 20 20 69 66 28 20 21 70 4f 72 64 65 72 42  /.  if( !pOrderB
2f6fb 79 20 26 26 20 70 50 61 72 73 65 2d 3e 64 62 2d  y && pParse->db-
2f6fc 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
2f6fd 52 65 76 65 72 73 65 4f 72 64 65 72 20 29 7b 0a  ReverseOrder ){.
2f6fe 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e      pCost->plan.
2f6ff 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45  wsFlags |= WHERE
2f700 5f 52 45 56 45 52 53 45 3b 0a 20 20 7d 0a 0a 20  _REVERSE;.  }.. 
2f701 20 61 73 73 65 72 74 28 20 70 4f 72 64 65 72 42   assert( pOrderB
2f702 79 20 7c 7c 20 28 70 43 6f 73 74 2d 3e 70 6c 61  y || (pCost->pla
2f703 6e 2e 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f  n.wsFlags&WHERE_
2f704 4f 52 44 45 52 42 59 29 3d 3d 30 20 29 3b 0a 20  ORDERBY)==0 );. 
2f705 20 61 73 73 65 72 74 28 20 70 43 6f 73 74 2d 3e   assert( pCost->
2f706 70 6c 61 6e 2e 75 2e 70 49 64 78 3d 3d 30 20 7c  plan.u.pIdx==0 |
2f707 7c 20 28 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77  | (pCost->plan.w
2f708 73 46 6c 61 67 73 26 57 48 45 52 45 5f 52 4f 57  sFlags&WHERE_ROW
2f709 49 44 5f 45 51 29 3d 3d 30 20 29 3b 0a 20 20 61  ID_EQ)==0 );.  a
2f70a 73 73 65 72 74 28 20 70 53 72 63 2d 3e 70 49 6e  ssert( pSrc->pIn
2f70b 64 65 78 3d 3d 30 20 0a 20 20 20 20 20 20 20 7c  dex==0 .       |
2f70c 7c 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e  | pCost->plan.u.
2f70d 70 49 64 78 3d 3d 30 20 0a 20 20 20 20 20 20 20  pIdx==0 .       
2f70e 7c 7c 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75  || pCost->plan.u
2f70f 2e 70 49 64 78 3d 3d 70 53 72 63 2d 3e 70 49 6e  .pIdx==pSrc->pIn
2f710 64 65 78 20 0a 20 20 29 3b 0a 0a 20 20 57 48 45  dex .  );..  WHE
2f711 52 45 54 52 41 43 45 28 28 22 62 65 73 74 20 69  RETRACE(("best i
2f712 6e 64 65 78 20 69 73 3a 20 25 73 5c 6e 22 2c 20  ndex is: %s\n", 
2f713 0a 20 20 20 20 28 70 43 6f 73 74 2d 3e 70 6c 61  .    (pCost->pla
2f714 6e 2e 75 2e 70 49 64 78 20 3f 20 70 43 6f 73 74  n.u.pIdx ? pCost
2f715 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e 7a  ->plan.u.pIdx->z
2f716 4e 61 6d 65 20 3a 20 22 69 70 6b 22 29 0a 20 20  Name : "ipk").  
2f717 29 29 3b 0a 20 20 0a 20 20 62 65 73 74 4f 72 43  ));.  .  bestOrC
2f718 6c 61 75 73 65 49 6e 64 65 78 28 70 50 61 72 73  lauseIndex(pPars
2f719 65 2c 20 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f  e, pWC, pSrc, no
2f71a 74 52 65 61 64 79 2c 20 70 4f 72 64 65 72 42 79  tReady, pOrderBy
2f71b 2c 20 70 43 6f 73 74 29 3b 0a 20 20 70 43 6f 73  , pCost);.  pCos
2f71c 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  t->plan.wsFlags 
2f71d 7c 3d 20 65 71 54 65 72 6d 4d 61 73 6b 3b 0a 7d  |= eqTermMask;.}
2f71e 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65  ../*.** Find the
2f71f 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f 72 20   query plan for 
2f720 61 63 63 65 73 73 69 6e 67 20 74 61 62 6c 65 20  accessing table 
2f721 70 53 72 63 2d 3e 70 54 61 62 2e 20 57 72 69 74  pSrc->pTab. Writ
2f722 65 20 74 68 65 0a 2a 2a 20 62 65 73 74 20 71 75  e the.** best qu
2f723 65 72 79 20 70 6c 61 6e 20 61 6e 64 20 69 74 73  ery plan and its
2f724 20 63 6f 73 74 20 69 6e 74 6f 20 74 68 65 20 57   cost into the W
2f725 68 65 72 65 43 6f 73 74 20 6f 62 6a 65 63 74 20  hereCost object 
2f726 73 75 70 70 6c 69 65 64 20 0a 2a 2a 20 61 73 20  supplied .** as 
2f727 74 68 65 20 6c 61 73 74 20 70 61 72 61 6d 65 74  the last paramet
2f728 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  er. This functio
2f729 6e 20 6d 61 79 20 63 61 6c 63 75 6c 61 74 65 20  n may calculate 
2f72a 74 68 65 20 63 6f 73 74 20 6f 66 0a 2a 2a 20 62  the cost of.** b
2f72b 6f 74 68 20 72 65 61 6c 20 61 6e 64 20 76 69 72  oth real and vir
2f72c 74 75 61 6c 20 74 61 62 6c 65 20 73 63 61 6e 73  tual table scans
2f72d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2f72e 20 62 65 73 74 49 6e 64 65 78 28 0a 20 20 50 61   bestIndex(.  Pa
2f72f 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2f730 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2f731 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   parsing context
2f732 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73   */.  WhereClaus
2f733 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20  e *pWC,         
2f734 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
2f735 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63  lause */.  struc
2f736 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
2f737 70 53 72 63 2c 20 20 2f 2a 20 54 68 65 20 46 52  pSrc,  /* The FR
2f738 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 20 74  OM clause term t
2f739 6f 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 42 69  o search */.  Bi
2f73a 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20  tmask notReady, 
2f73b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73            /* Mas
2f73c 6b 20 6f 66 20 63 75 72 73 6f 72 73 20 74 68 61  k of cursors tha
2f73d 74 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61  t are not availa
2f73e 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ble */.  ExprLis
2f73f 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20  t *pOrderBy,    
2f740 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45       /* The ORDE
2f741 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20  R BY clause */. 
2f742 20 57 68 65 72 65 43 6f 73 74 20 2a 70 43 6f 73   WhereCost *pCos
2f743 74 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t            /* 
2f744 4c 6f 77 65 73 74 20 63 6f 73 74 20 71 75 65 72  Lowest cost quer
2f745 79 20 70 6c 61 6e 20 2a 2f 0a 29 7b 0a 23 69 66  y plan */.){.#if
2f746 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2f747 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
2f748 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 53  if( IsVirtual(pS
2f749 72 63 2d 3e 70 54 61 62 29 20 29 7b 0a 20 20 20  rc->pTab) ){.   
2f74a 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
2f74b 6e 66 6f 20 2a 70 20 3d 20 30 3b 0a 20 20 20 20  nfo *p = 0;.    
2f74c 62 65 73 74 56 69 72 74 75 61 6c 49 6e 64 65 78  bestVirtualIndex
2f74d 28 70 50 61 72 73 65 2c 20 70 57 43 2c 20 70 53  (pParse, pWC, pS
2f74e 72 63 2c 20 6e 6f 74 52 65 61 64 79 2c 20 70 4f  rc, notReady, pO
2f74f 72 64 65 72 42 79 2c 20 70 43 6f 73 74 2c 20 26  rderBy, pCost, &
2f750 70 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e  p);.    if( p->n
2f751 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20  eedToFreeIdxStr 
2f752 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2f753 5f 66 72 65 65 28 70 2d 3e 69 64 78 53 74 72 29  _free(p->idxStr)
2f754 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2f755 74 65 33 44 62 46 72 65 65 28 70 50 61 72 73 65  te3DbFree(pParse
2f756 2d 3e 64 62 2c 20 70 29 3b 0a 20 20 7d 65 6c 73  ->db, p);.  }els
2f757 65 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20  e.#endif.  {.   
2f758 20 62 65 73 74 42 74 72 65 65 49 6e 64 65 78 28   bestBtreeIndex(
2f759 70 50 61 72 73 65 2c 20 70 57 43 2c 20 70 53 72  pParse, pWC, pSr
2f75a 63 2c 20 6e 6f 74 52 65 61 64 79 2c 20 70 4f 72  c, notReady, pOr
2f75b 64 65 72 42 79 2c 20 70 43 6f 73 74 29 3b 0a 20  derBy, pCost);. 
2f75c 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69 73 61   }.}../*.** Disa
2f75d 62 6c 65 20 61 20 74 65 72 6d 20 69 6e 20 74 68  ble a term in th
2f75e 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20  e WHERE clause. 
2f75f 20 45 78 63 65 70 74 2c 20 64 6f 20 6e 6f 74 20   Except, do not 
2f760 64 69 73 61 62 6c 65 20 74 68 65 20 74 65 72 6d  disable the term
2f761 0a 2a 2a 20 69 66 20 69 74 20 63 6f 6e 74 72 6f  .** if it contro
2f762 6c 73 20 61 20 4c 45 46 54 20 4f 55 54 45 52 20  ls a LEFT OUTER 
2f763 4a 4f 49 4e 20 61 6e 64 20 69 74 20 64 69 64 20  JOIN and it did 
2f764 6e 6f 74 20 6f 72 69 67 69 6e 61 74 65 20 69 6e  not originate in
2f765 20 74 68 65 20 4f 4e 0a 2a 2a 20 6f 72 20 55 53   the ON.** or US
2f766 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 74 68  ING clause of th
2f767 61 74 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 43  at join..**.** C
2f768 6f 6e 73 69 64 65 72 20 74 68 65 20 74 65 72 6d  onsider the term
2f769 20 74 32 2e 7a 3d 27 6f 6b 27 20 69 6e 20 74 68   t2.z='ok' in th
2f76a 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 71 75 65 72  e following quer
2f76b 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29  ies:.**.**   (1)
2f76c 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
2f76d 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74 32 20  t1 LEFT JOIN t2 
2f76e 4f 4e 20 74 31 2e 61 3d 74 32 2e 78 20 57 48 45  ON t1.a=t2.x WHE
2f76f 52 45 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 20  RE t2.z='ok'.** 
2f770 20 20 28 32 29 20 20 53 45 4c 45 43 54 20 2a 20    (2)  SELECT * 
2f771 46 52 4f 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49  FROM t1 LEFT JOI
2f772 4e 20 74 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e  N t2 ON t1.a=t2.
2f773 78 20 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27 0a  x AND t2.z='ok'.
2f774 2a 2a 20 20 20 28 33 29 20 20 53 45 4c 45 43 54  **   (3)  SELECT
2f775 20 2a 20 46 52 4f 4d 20 74 31 2c 20 74 32 20 57   * FROM t1, t2 W
2f776 48 45 52 45 20 74 31 2e 61 3d 74 32 2e 78 20 41  HERE t1.a=t2.x A
2f777 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 0a  ND t2.z='ok'.**.
2f778 2a 2a 20 54 68 65 20 74 32 2e 7a 3d 27 6f 6b 27  ** The t2.z='ok'
2f779 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e 20   is disabled in 
2f77a 74 68 65 20 69 6e 20 28 32 29 20 62 65 63 61 75  the in (2) becau
2f77b 73 65 20 69 74 20 6f 72 69 67 69 6e 61 74 65 73  se it originates
2f77c 0a 2a 2a 20 69 6e 20 74 68 65 20 4f 4e 20 63 6c  .** in the ON cl
2f77d 61 75 73 65 2e 20 20 54 68 65 20 74 65 72 6d 20  ause.  The term 
2f77e 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e 20 28  is disabled in (
2f77f 33 29 20 62 65 63 61 75 73 65 20 69 74 20 69 73  3) because it is
2f780 20 6e 6f 74 20 70 61 72 74 0a 2a 2a 20 6f 66 20   not part.** of 
2f781 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49  a LEFT OUTER JOI
2f782 4e 2e 20 20 49 6e 20 28 31 29 2c 20 74 68 65 20  N.  In (1), the 
2f783 74 65 72 6d 20 69 73 20 6e 6f 74 20 64 69 73 61  term is not disa
2f784 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 44 69 73 61  bled..**.** Disa
2f785 62 6c 69 6e 67 20 61 20 74 65 72 6d 20 63 61 75  bling a term cau
2f786 73 65 73 20 74 68 61 74 20 74 65 72 6d 20 74 6f  ses that term to
2f787 20 6e 6f 74 20 62 65 20 74 65 73 74 65 64 20 69   not be tested i
2f788 6e 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f 70  n the inner loop
2f789 0a 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e  .** of the join.
2f78a 20 20 44 69 73 61 62 6c 69 6e 67 20 69 73 20 61    Disabling is a
2f78b 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20  n optimization. 
2f78c 20 57 68 65 6e 20 74 65 72 6d 73 20 61 72 65 20   When terms are 
2f78d 73 61 74 69 73 66 69 65 64 0a 2a 2a 20 62 79 20  satisfied.** by 
2f78e 69 6e 64 69 63 65 73 2c 20 77 65 20 64 69 73 61  indices, we disa
2f78f 62 6c 65 20 74 68 65 6d 20 74 6f 20 70 72 65 76  ble them to prev
2f790 65 6e 74 20 72 65 64 75 6e 64 61 6e 74 20 74 65  ent redundant te
2f791 73 74 73 20 69 6e 20 74 68 65 20 69 6e 6e 65 72  sts in the inner
2f792 0a 2a 2a 20 6c 6f 6f 70 2e 20 20 57 65 20 77 6f  .** loop.  We wo
2f793 75 6c 64 20 67 65 74 20 74 68 65 20 63 6f 72 72  uld get the corr
2f794 65 63 74 20 72 65 73 75 6c 74 73 20 69 66 20 6e  ect results if n
2f795 6f 74 68 69 6e 67 20 77 65 72 65 20 65 76 65 72  othing were ever
2f796 20 64 69 73 61 62 6c 65 64 2c 0a 2a 2a 20 62 75   disabled,.** bu
2f797 74 20 6a 6f 69 6e 73 20 6d 69 67 68 74 20 72 75  t joins might ru
2f798 6e 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65  n a little slowe
2f799 72 2e 20 20 54 68 65 20 74 72 69 63 6b 20 69 73  r.  The trick is
2f79a 20 74 6f 20 64 69 73 61 62 6c 65 20 61 73 20 6d   to disable as m
2f79b 75 63 68 0a 2a 2a 20 61 73 20 77 65 20 63 61 6e  uch.** as we can
2f79c 20 77 69 74 68 6f 75 74 20 64 69 73 61 62 6c 69   without disabli
2f79d 6e 67 20 74 6f 6f 20 6d 75 63 68 2e 20 20 49 66  ng too much.  If
2f79e 20 77 65 20 64 69 73 61 62 6c 65 64 20 69 6e 20   we disabled in 
2f79f 28 31 29 2c 20 77 65 27 64 20 67 65 74 0a 2a 2a  (1), we'd get.**
2f7a0 20 74 68 65 20 77 72 6f 6e 67 20 61 6e 73 77 65   the wrong answe
2f7a1 72 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20 23  r.  See ticket #
2f7a2 38 31 33 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  813..*/.static v
2f7a3 6f 69 64 20 64 69 73 61 62 6c 65 54 65 72 6d 28  oid disableTerm(
2f7a4 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76  WhereLevel *pLev
2f7a5 65 6c 2c 20 57 68 65 72 65 54 65 72 6d 20 2a 70  el, WhereTerm *p
2f7a6 54 65 72 6d 29 7b 0a 20 20 69 66 28 20 70 54 65  Term){.  if( pTe
2f7a7 72 6d 0a 20 20 20 20 20 20 26 26 20 41 4c 57 41  rm.      && ALWA
2f7a8 59 53 28 28 70 54 65 72 6d 2d 3e 77 74 46 6c 61  YS((pTerm->wtFla
2f7a9 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 45 44 29  gs & TERM_CODED)
2f7aa 3d 3d 30 29 0a 20 20 20 20 20 20 26 26 20 28 70  ==0).      && (p
2f7ab 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e  Level->iLeftJoin
2f7ac 3d 3d 30 20 7c 7c 20 45 78 70 72 48 61 73 50 72  ==0 || ExprHasPr
2f7ad 6f 70 65 72 74 79 28 70 54 65 72 6d 2d 3e 70 45  operty(pTerm->pE
2f7ae 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e  xpr, EP_FromJoin
2f7af 29 29 0a 20 20 29 7b 0a 20 20 20 20 70 54 65 72  )).  ){.    pTer
2f7b0 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45  m->wtFlags |= TE
2f7b1 52 4d 5f 43 4f 44 45 44 3b 0a 20 20 20 20 69 66  RM_CODED;.    if
2f7b2 28 20 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74  ( pTerm->iParent
2f7b3 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 57 68 65  >=0 ){.      Whe
2f7b4 72 65 54 65 72 6d 20 2a 70 4f 74 68 65 72 20 3d  reTerm *pOther =
2f7b5 20 26 70 54 65 72 6d 2d 3e 70 57 43 2d 3e 61 5b   &pTerm->pWC->a[
2f7b6 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 5d 3b  pTerm->iParent];
2f7b7 0a 20 20 20 20 20 20 69 66 28 20 28 2d 2d 70 4f  .      if( (--pO
2f7b8 74 68 65 72 2d 3e 6e 43 68 69 6c 64 29 3d 3d 30  ther->nChild)==0
2f7b9 20 29 7b 0a 20 20 20 20 20 20 20 20 64 69 73 61   ){.        disa
2f7ba 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20  bleTerm(pLevel, 
2f7bb 70 4f 74 68 65 72 29 3b 0a 20 20 20 20 20 20 7d  pOther);.      }
2f7bc 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
2f7bd 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 41  .** Code an OP_A
2f7be 66 66 69 6e 69 74 79 20 6f 70 63 6f 64 65 20 74  ffinity opcode t
2f7bf 6f 20 61 70 70 6c 79 20 74 68 65 20 63 6f 6c 75  o apply the colu
2f7c0 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69  mn affinity stri
2f7c1 6e 67 20 7a 41 66 66 0a 2a 2a 20 74 6f 20 74 68  ng zAff.** to th
2f7c2 65 20 6e 20 72 65 67 69 73 74 65 72 73 20 73 74  e n registers st
2f7c3 61 72 74 69 6e 67 20 61 74 20 62 61 73 65 2e 20  arting at base. 
2f7c4 0a 2a 2a 0a 2a 2a 20 42 75 66 66 65 72 20 7a 41  .**.** Buffer zA
2f7c5 66 66 20 77 61 73 20 61 6c 6c 6f 63 61 74 65 64  ff was allocated
2f7c6 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 44 62   using sqlite3Db
2f7c7 4d 61 6c 6c 6f 63 28 29 2e 20 49 74 20 69 73 20  Malloc(). It is 
2f7c8 74 68 65 20 0a 2a 2a 20 72 65 73 70 6f 6e 73 69  the .** responsi
2f7c9 62 69 6c 69 74 79 20 6f 66 20 74 68 69 73 20 66  bility of this f
2f7ca 75 6e 63 74 69 6f 6e 20 74 6f 20 61 72 72 61 6e  unction to arran
2f7cb 67 65 20 66 6f 72 20 69 74 20 74 6f 20 62 65 20  ge for it to be 
2f7cc 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 66 72  eventually.** fr
2f7cd 65 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65  eed using sqlite
2f7ce 33 44 62 46 72 65 65 28 29 2e 0a 2a 2f 0a 73 74  3DbFree()..*/.st
2f7cf 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 41 70  atic void codeAp
2f7d0 70 6c 79 41 66 66 69 6e 69 74 79 28 50 61 72 73  plyAffinity(Pars
2f7d1 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 62  e *pParse, int b
2f7d2 61 73 65 2c 20 69 6e 74 20 6e 2c 20 63 68 61 72  ase, int n, char
2f7d3 20 2a 7a 41 66 66 29 7b 0a 20 20 56 64 62 65 20   *zAff){.  Vdbe 
2f7d4 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
2f7d5 62 65 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21  be;.  assert( v!
2f7d6 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  =0 );.  sqlite3V
2f7d7 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2f7d8 41 66 66 69 6e 69 74 79 2c 20 62 61 73 65 2c 20  Affinity, base, 
2f7d9 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  n);.  sqlite3Vdb
2f7da 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
2f7db 20 7a 41 66 66 2c 20 50 34 5f 44 59 4e 41 4d 49   zAff, P4_DYNAMI
2f7dc 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  C);.  sqlite3Exp
2f7dd 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
2f7de 61 6e 67 65 28 70 50 61 72 73 65 2c 20 62 61 73  ange(pParse, bas
2f7df 65 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  e, n);.}.../*.**
2f7e0 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   Generate code f
2f7e1 6f 72 20 61 20 73 69 6e 67 6c 65 20 65 71 75 61  or a single equa
2f7e2 6c 69 74 79 20 74 65 72 6d 20 6f 66 20 74 68 65  lity term of the
2f7e3 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20   WHERE clause.  
2f7e4 41 6e 20 65 71 75 61 6c 69 74 79 0a 2a 2a 20 74  An equality.** t
2f7e5 65 72 6d 20 63 61 6e 20 62 65 20 65 69 74 68 65  erm can be eithe
2f7e6 72 20 58 3d 65 78 70 72 20 6f 72 20 58 20 49 4e  r X=expr or X IN
2f7e7 20 28 2e 2e 2e 29 2e 20 20 20 70 54 65 72 6d 20   (...).   pTerm 
2f7e8 69 73 20 74 68 65 20 74 65 72 6d 20 74 6f 20 62  is the term to b
2f7e9 65 20 0a 2a 2a 20 63 6f 64 65 64 2e 0a 2a 2a 0a  e .** coded..**.
2f7ea 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 76  ** The current v
2f7eb 61 6c 75 65 20 66 6f 72 20 74 68 65 20 63 6f 6e  alue for the con
2f7ec 73 74 72 61 69 6e 74 20 69 73 20 6c 65 66 74 20  straint is left 
2f7ed 69 6e 20 72 65 67 69 73 74 65 72 20 69 52 65 67  in register iReg
2f7ee 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 63 6f  ..**.** For a co
2f7ef 6e 73 74 72 61 69 6e 74 20 6f 66 20 74 68 65 20  nstraint of the 
2f7f0 66 6f 72 6d 20 58 3d 65 78 70 72 2c 20 74 68 65  form X=expr, the
2f7f1 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 65   expression is e
2f7f2 76 61 6c 75 61 74 65 64 20 61 6e 64 20 69 74 73  valuated and its
2f7f3 0a 2a 2a 20 72 65 73 75 6c 74 20 69 73 20 6c 65  .** result is le
2f7f4 66 74 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e  ft on the stack.
2f7f5 20 20 46 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74    For constraint
2f7f6 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 58 20  s of the form X 
2f7f7 49 4e 20 28 2e 2e 2e 29 0a 2a 2a 20 74 68 69 73  IN (...).** this
2f7f8 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 75 70   routine sets up
2f7f9 20 61 20 6c 6f 6f 70 20 74 68 61 74 20 77 69 6c   a loop that wil
2f7fa 6c 20 69 74 65 72 61 74 65 20 6f 76 65 72 20 61  l iterate over a
2f7fb 6c 6c 20 76 61 6c 75 65 73 20 6f 66 20 58 2e 0a  ll values of X..
2f7fc 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  */.static int co
2f7fd 64 65 45 71 75 61 6c 69 74 79 54 65 72 6d 28 0a  deEqualityTerm(.
2f7fe 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2f7ff 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
2f800 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2f801 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65    WhereTerm *pTe
2f802 72 6d 2c 20 20 20 2f 2a 20 54 68 65 20 74 65 72  rm,   /* The ter
2f803 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63  m of the WHERE c
2f804 6c 61 75 73 65 20 74 6f 20 62 65 20 63 6f 64 65  lause to be code
2f805 64 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65  d */.  WhereLeve
2f806 6c 20 2a 70 4c 65 76 65 6c 2c 20 2f 2a 20 57 68  l *pLevel, /* Wh
2f807 65 6e 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20  en level of the 
2f808 46 52 4f 4d 20 63 6c 61 75 73 65 20 77 65 20 61  FROM clause we a
2f809 72 65 20 77 6f 72 6b 69 6e 67 20 6f 6e 20 2a 2f  re working on */
2f80a 0a 20 20 69 6e 74 20 69 54 61 72 67 65 74 20 20  .  int iTarget  
2f80b 20 20 20 20 20 20 20 2f 2a 20 41 74 74 65 6d 70         /* Attemp
2f80c 74 20 74 6f 20 6c 65 61 76 65 20 72 65 73 75 6c  t to leave resul
2f80d 74 73 20 69 6e 20 74 68 69 73 20 72 65 67 69 73  ts in this regis
2f80e 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72  ter */.){.  Expr
2f80f 20 2a 70 58 20 3d 20 70 54 65 72 6d 2d 3e 70 45   *pX = pTerm->pE
2f810 78 70 72 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d  xpr;.  Vdbe *v =
2f811 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
2f812 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20 20 20    int iReg;     
2f813 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2f814 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67  Register holding
2f815 20 72 65 73 75 6c 74 73 20 2a 2f 0a 0a 20 20 61   results */..  a
2f816 73 73 65 72 74 28 20 69 54 61 72 67 65 74 3e 30  ssert( iTarget>0
2f817 20 29 3b 0a 20 20 69 66 28 20 70 58 2d 3e 6f 70   );.  if( pX->op
2f818 3d 3d 54 4b 5f 45 51 20 29 7b 0a 20 20 20 20 69  ==TK_EQ ){.    i
2f819 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70  Reg = sqlite3Exp
2f81a 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72  rCodeTarget(pPar
2f81b 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74 2c 20  se, pX->pRight, 
2f81c 69 54 61 72 67 65 74 29 3b 0a 20 20 7d 65 6c 73  iTarget);.  }els
2f81d 65 20 69 66 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b  e if( pX->op==TK
2f81e 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69  _ISNULL ){.    i
2f81f 52 65 67 20 3d 20 69 54 61 72 67 65 74 3b 0a 20  Reg = iTarget;. 
2f820 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2f821 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
2f822 20 30 2c 20 69 52 65 67 29 3b 0a 23 69 66 6e 64   0, iReg);.#ifnd
2f823 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
2f824 55 42 51 55 45 52 59 0a 20 20 7d 65 6c 73 65 7b  UBQUERY.  }else{
2f825 0a 20 20 20 20 69 6e 74 20 65 54 79 70 65 3b 0a  .    int eType;.
2f826 20 20 20 20 69 6e 74 20 69 54 61 62 3b 0a 20 20      int iTab;.  
2f827 20 20 73 74 72 75 63 74 20 49 6e 4c 6f 6f 70 20    struct InLoop 
2f828 2a 70 49 6e 3b 0a 0a 20 20 20 20 61 73 73 65 72  *pIn;..    asser
2f829 74 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e  t( pX->op==TK_IN
2f82a 20 29 3b 0a 20 20 20 20 69 52 65 67 20 3d 20 69   );.    iReg = i
2f82b 54 61 72 67 65 74 3b 0a 20 20 20 20 65 54 79 70  Target;.    eTyp
2f82c 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49  e = sqlite3FindI
2f82d 6e 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70  nIndex(pParse, p
2f82e 58 2c 20 30 29 3b 0a 20 20 20 20 69 54 61 62 20  X, 0);.    iTab 
2f82f 3d 20 70 58 2d 3e 69 54 61 62 6c 65 3b 0a 20 20  = pX->iTable;.  
2f830 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2f831 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64  Op2(v, OP_Rewind
2f832 2c 20 69 54 61 62 2c 20 30 29 3b 0a 20 20 20 20  , iTab, 0);.    
2f833 61 73 73 65 72 74 28 20 70 4c 65 76 65 6c 2d 3e  assert( pLevel->
2f834 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
2f835 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 29 3b 0a  HERE_IN_ABLE );.
2f836 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
2f837 75 2e 69 6e 2e 6e 49 6e 3d 3d 30 20 29 7b 0a 20  u.in.nIn==0 ){. 
2f838 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 61 64 64       pLevel->add
2f839 72 4e 78 74 20 3d 20 73 71 6c 69 74 65 33 56 64  rNxt = sqlite3Vd
2f83a 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
2f83b 20 20 20 20 7d 0a 20 20 20 20 70 4c 65 76 65 6c      }.    pLevel
2f83c 2d 3e 75 2e 69 6e 2e 6e 49 6e 2b 2b 3b 0a 20 20  ->u.in.nIn++;.  
2f83d 20 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61    pLevel->u.in.a
2f83e 49 6e 4c 6f 6f 70 20 3d 0a 20 20 20 20 20 20 20  InLoop =.       
2f83f 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
2f840 4f 72 46 72 65 65 28 70 50 61 72 73 65 2d 3e 64  OrFree(pParse->d
2f841 62 2c 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e  b, pLevel->u.in.
2f842 61 49 6e 4c 6f 6f 70 2c 0a 20 20 20 20 20 20 20  aInLoop,.       
2f843 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f844 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 4c         sizeof(pL
2f845 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f  evel->u.in.aInLo
2f846 6f 70 5b 30 5d 29 2a 70 4c 65 76 65 6c 2d 3e 75  op[0])*pLevel->u
2f847 2e 69 6e 2e 6e 49 6e 29 3b 0a 20 20 20 20 70 49  .in.nIn);.    pI
2f848 6e 20 3d 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e  n = pLevel->u.in
2f849 2e 61 49 6e 4c 6f 6f 70 3b 0a 20 20 20 20 69 66  .aInLoop;.    if
2f84a 28 20 70 49 6e 20 29 7b 0a 20 20 20 20 20 20 70  ( pIn ){.      p
2f84b 49 6e 20 2b 3d 20 70 4c 65 76 65 6c 2d 3e 75 2e  In += pLevel->u.
2f84c 69 6e 2e 6e 49 6e 20 2d 20 31 3b 0a 20 20 20 20  in.nIn - 1;.    
2f84d 20 20 70 49 6e 2d 3e 69 43 75 72 20 3d 20 69 54    pIn->iCur = iT
2f84e 61 62 3b 0a 20 20 20 20 20 20 69 66 28 20 65 54  ab;.      if( eT
2f84f 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f  ype==IN_INDEX_RO
2f850 57 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 70  WID ){.        p
2f851 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 20 3d 20  In->addrInTop = 
2f852 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2f853 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69  2(v, OP_Rowid, i
2f854 54 61 62 2c 20 69 52 65 67 29 3b 0a 20 20 20 20  Tab, iReg);.    
2f855 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2f856 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 20   pIn->addrInTop 
2f857 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2f858 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
2f859 2c 20 69 54 61 62 2c 20 30 2c 20 69 52 65 67 29  , iTab, 0, iReg)
2f85a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2f85b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2f85c 31 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20  1(v, OP_IsNull, 
2f85d 69 52 65 67 29 3b 0a 20 20 20 20 7d 65 6c 73 65  iReg);.    }else
2f85e 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e  {.      pLevel->
2f85f 75 2e 69 6e 2e 6e 49 6e 20 3d 20 30 3b 0a 20 20  u.in.nIn = 0;.  
2f860 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20    }.#endif.  }. 
2f861 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65   disableTerm(pLe
2f862 76 65 6c 2c 20 70 54 65 72 6d 29 3b 0a 20 20 72  vel, pTerm);.  r
2f863 65 74 75 72 6e 20 69 52 65 67 3b 0a 7d 0a 0a 2f  eturn iReg;.}../
2f864 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
2f865 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 76 61  de that will eva
2f866 6c 75 61 74 65 20 61 6c 6c 20 3d 3d 20 61 6e 64  luate all == and
2f867 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20   IN constraints 
2f868 66 6f 72 20 61 6e 0a 2a 2a 20 69 6e 64 65 78 2e  for an.** index.
2f869 20 20 54 68 65 20 76 61 6c 75 65 73 20 66 6f 72    The values for
2f86a 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73   all constraints
2f86b 20 61 72 65 20 6c 65 66 74 20 6f 6e 20 74 68 65   are left on the
2f86c 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f   stack..**.** Fo
2f86d 72 20 65 78 61 6d 70 6c 65 2c 20 63 6f 6e 73 69  r example, consi
2f86e 64 65 72 20 74 61 62 6c 65 20 74 31 28 61 2c 62  der table t1(a,b
2f86f 2c 63 2c 64 2c 65 2c 66 29 20 77 69 74 68 20 69  ,c,d,e,f) with i
2f870 6e 64 65 78 20 69 31 28 61 2c 62 2c 63 29 2e 0a  ndex i1(a,b,c)..
2f871 2a 2a 20 53 75 70 70 6f 73 65 20 74 68 65 20 57  ** Suppose the W
2f872 48 45 52 45 20 63 6c 61 75 73 65 20 69 73 20 74  HERE clause is t
2f873 68 69 73 3a 20 20 61 3d 3d 35 20 41 4e 44 20 62  his:  a==5 AND b
2f874 20 49 4e 20 28 31 2c 32 2c 33 29 20 41 4e 44 20   IN (1,2,3) AND 
2f875 63 3e 35 20 41 4e 44 20 63 3c 31 30 0a 2a 2a 20  c>5 AND c<10.** 
2f876 54 68 65 20 69 6e 64 65 78 20 68 61 73 20 61 73  The index has as
2f877 20 6d 61 6e 79 20 61 73 20 74 68 72 65 65 20 65   many as three e
2f878 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
2f879 6e 74 73 2c 20 62 75 74 20 69 6e 20 74 68 69 73  nts, but in this
2f87a 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 74 68 65  .** example, the
2f87b 20 74 68 69 72 64 20 22 63 22 20 76 61 6c 75 65   third "c" value
2f87c 20 69 73 20 61 6e 20 69 6e 65 71 75 61 6c 69 74   is an inequalit
2f87d 79 2e 20 20 53 6f 20 6f 6e 6c 79 20 74 77 6f 20  y.  So only two 
2f87e 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  .** constraints 
2f87f 61 72 65 20 63 6f 64 65 64 2e 20 20 54 68 69 73  are coded.  This
2f880 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 67 65   routine will ge
2f881 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 65  nerate code to e
2f882 76 61 6c 75 61 74 65 0a 2a 2a 20 61 3d 3d 35 20  valuate.** a==5 
2f883 61 6e 64 20 62 20 49 4e 20 28 31 2c 32 2c 33 29  and b IN (1,2,3)
2f884 2e 20 20 54 68 65 20 63 75 72 72 65 6e 74 20 76  .  The current v
2f885 61 6c 75 65 73 20 66 6f 72 20 61 20 61 6e 64 20  alues for a and 
2f886 62 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 64  b will be stored
2f887 0a 2a 2a 20 69 6e 20 63 6f 6e 73 65 63 75 74 69  .** in consecuti
2f888 76 65 20 72 65 67 69 73 74 65 72 73 20 61 6e 64  ve registers and
2f889 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
2f88a 65 20 66 69 72 73 74 20 72 65 67 69 73 74 65 72  e first register
2f88b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
2f88c 0a 2a 2a 20 49 6e 20 74 68 65 20 65 78 61 6d 70  .** In the examp
2f88d 6c 65 20 61 62 6f 76 65 20 6e 45 71 3d 3d 32 2e  le above nEq==2.
2f88e 20 20 42 75 74 20 74 68 69 73 20 73 75 62 72 6f    But this subro
2f88f 75 74 69 6e 65 20 77 6f 72 6b 73 20 66 6f 72 20  utine works for 
2f890 61 6e 79 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  any value.** of 
2f891 6e 45 71 20 69 6e 63 6c 75 64 69 6e 67 20 30 2e  nEq including 0.
2f892 20 20 49 66 20 6e 45 71 3d 3d 30 2c 20 74 68 69    If nEq==0, thi
2f893 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 65 61  s routine is nea
2f894 72 6c 79 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 20  rly a no-op..** 
2f895 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 69  The only thing i
2f896 74 20 64 6f 65 73 20 69 73 20 61 6c 6c 6f 63 61  t does is alloca
2f897 74 65 20 74 68 65 20 70 4c 65 76 65 6c 2d 3e 69  te the pLevel->i
2f898 4d 65 6d 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e  Mem memory cell.
2f899 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2f89a 69 6e 65 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63  ine always alloc
2f89b 61 74 65 73 20 61 74 20 6c 65 61 73 74 20 6f 6e  ates at least on
2f89c 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e  e memory cell an
2f89d 64 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65  d returns.** the
2f89e 20 69 6e 64 65 78 20 6f 66 20 74 68 61 74 20 6d   index of that m
2f89f 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 54 68 65 20  emory cell. The 
2f8a0 63 6f 64 65 20 74 68 61 74 0a 2a 2a 20 63 61 6c  code that.** cal
2f8a1 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
2f8a2 77 69 6c 6c 20 75 73 65 20 74 68 61 74 20 6d 65  will use that me
2f8a3 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 73 74 6f  mory cell to sto
2f8a4 72 65 20 74 68 65 20 74 65 72 6d 69 6e 61 74 69  re the terminati
2f8a5 6f 6e 0a 2a 2a 20 6b 65 79 20 76 61 6c 75 65 20  on.** key value 
2f8a6 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 20 20 49 66  of the loop.  If
2f8a7 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 49 4e 20   one or more IN 
2f8a8 6f 70 65 72 61 74 6f 72 73 20 61 70 70 65 61 72  operators appear
2f8a9 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72  , then.** this r
2f8aa 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73  outine allocates
2f8ab 20 61 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6e   an additional n
2f8ac 45 71 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  Eq memory cells 
2f8ad 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20  for internal.** 
2f8ae 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72  use..**.** Befor
2f8af 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 7a  e returning, *pz
2f8b0 41 66 66 20 69 73 20 73 65 74 20 74 6f 20 70 6f  Aff is set to po
2f8b1 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 20  int to a buffer 
2f8b2 63 6f 6e 74 61 69 6e 69 6e 67 20 61 0a 2a 2a 20  containing a.** 
2f8b3 63 6f 70 79 20 6f 66 20 74 68 65 20 63 6f 6c 75  copy of the colu
2f8b4 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69  mn affinity stri
2f8b5 6e 67 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  ng of the index 
2f8b6 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 0a  allocated using.
2f8b7 2a 2a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c  ** sqlite3DbMall
2f8b8 6f 63 28 29 2e 20 45 78 63 65 70 74 2c 20 65 6e  oc(). Except, en
2f8b9 74 72 69 65 73 20 69 6e 20 74 68 65 20 63 6f 70  tries in the cop
2f8ba 79 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20  y of the string 
2f8bb 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69  associated.** wi
2f8bc 74 68 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73  th equality cons
2f8bd 74 72 61 69 6e 74 73 20 74 68 61 74 20 75 73 65  traints that use
2f8be 20 4e 4f 4e 45 20 61 66 66 69 6e 69 74 79 20 61   NONE affinity a
2f8bf 72 65 20 73 65 74 20 74 6f 0a 2a 2a 20 53 51 4c  re set to.** SQL
2f8c0 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 2e 20 54 68  ITE_AFF_NONE. Th
2f8c1 69 73 20 69 73 20 74 6f 20 64 65 61 6c 20 77 69  is is to deal wi
2f8c2 74 68 20 53 51 4c 20 73 75 63 68 20 61 73 20 74  th SQL such as t
2f8c3 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a  he following:.**
2f8c4 0a 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41 42  .**   CREATE TAB
2f8c5 4c 45 20 74 31 28 61 20 54 45 58 54 20 50 52 49  LE t1(a TEXT PRI
2f8c6 4d 41 52 59 20 4b 45 59 2c 20 62 29 3b 0a 2a 2a  MARY KEY, b);.**
2f8c7 20 20 20 53 45 4c 45 43 54 20 2e 2e 2e 20 46 52     SELECT ... FR
2f8c8 4f 4d 20 74 31 20 41 53 20 74 32 2c 20 74 31 20  OM t1 AS t2, t1 
2f8c9 57 48 45 52 45 20 74 31 2e 61 20 3d 20 74 32 2e  WHERE t1.a = t2.
2f8ca 62 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20  b;.**.** In the 
2f8cb 65 78 61 6d 70 6c 65 20 61 62 6f 76 65 2c 20 74  example above, t
2f8cc 68 65 20 69 6e 64 65 78 20 6f 6e 20 74 31 28 61  he index on t1(a
2f8cd 29 20 68 61 73 20 54 45 58 54 20 61 66 66 69 6e  ) has TEXT affin
2f8ce 69 74 79 2e 20 42 75 74 20 73 69 6e 63 65 0a 2a  ity. But since.*
2f8cf 2a 20 74 68 65 20 72 69 67 68 74 20 68 61 6e 64  * the right hand
2f8d0 20 73 69 64 65 20 6f 66 20 74 68 65 20 65 71 75   side of the equ
2f8d1 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
2f8d2 20 28 74 32 2e 62 29 20 68 61 73 20 4e 4f 4e 45   (t2.b) has NONE
2f8d3 20 61 66 66 69 6e 69 74 79 2c 0a 2a 2a 20 6e 6f   affinity,.** no
2f8d4 20 63 6f 6e 76 65 72 73 69 6f 6e 20 73 68 6f 75   conversion shou
2f8d5 6c 64 20 62 65 20 61 74 74 65 6d 70 74 65 64 20  ld be attempted 
2f8d6 62 65 66 6f 72 65 20 75 73 69 6e 67 20 61 20 74  before using a t
2f8d7 32 2e 62 20 76 61 6c 75 65 20 61 73 20 70 61 72  2.b value as par
2f8d8 74 20 6f 66 0a 2a 2a 20 61 20 6b 65 79 20 74 6f  t of.** a key to
2f8d9 20 73 65 61 72 63 68 20 74 68 65 20 69 6e 64 65   search the inde
2f8da 78 2e 20 48 65 6e 63 65 20 74 68 65 20 66 69 72  x. Hence the fir
2f8db 73 74 20 62 79 74 65 20 69 6e 20 74 68 65 20 72  st byte in the r
2f8dc 65 74 75 72 6e 65 64 20 61 66 66 69 6e 69 74 79  eturned affinity
2f8dd 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 20 74 68  .** string in th
2f8de 69 73 20 65 78 61 6d 70 6c 65 20 77 6f 75 6c 64  is example would
2f8df 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54   be set to SQLIT
2f8e0 45 5f 41 46 46 5f 4e 4f 4e 45 2e 0a 2a 2f 0a 73  E_AFF_NONE..*/.s
2f8e1 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 41 6c  tatic int codeAl
2f8e2 6c 45 71 75 61 6c 69 74 79 54 65 72 6d 73 28 0a  lEqualityTerms(.
2f8e3 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2f8e4 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
2f8e5 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2f8e6 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76  WhereLevel *pLev
2f8e7 65 6c 2c 20 20 20 2f 2a 20 57 68 69 63 68 20 6e  el,   /* Which n
2f8e8 65 73 74 65 64 20 6c 6f 6f 70 20 6f 66 20 74 68  ested loop of th
2f8e9 65 20 46 52 4f 4d 20 77 65 20 61 72 65 20 63 6f  e FROM we are co
2f8ea 64 69 6e 67 20 2a 2f 0a 20 20 57 68 65 72 65 43  ding */.  WhereC
2f8eb 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20  lause *pWC,     
2f8ec 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
2f8ed 75 73 65 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  use */.  Bitmask
2f8ee 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20 2f   notReady,     /
2f8ef 2a 20 57 68 69 63 68 20 70 61 72 74 73 20 6f 66  * Which parts of
2f8f0 20 46 52 4f 4d 20 68 61 76 65 20 6e 6f 74 20 79   FROM have not y
2f8f1 65 74 20 62 65 65 6e 20 63 6f 64 65 64 20 2a 2f  et been coded */
2f8f2 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 52 65 67  .  int nExtraReg
2f8f3 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ,        /* Numb
2f8f4 65 72 20 6f 66 20 65 78 74 72 61 20 72 65 67 69  er of extra regi
2f8f5 73 74 65 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74  sters to allocat
2f8f6 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a  e */.  char **pz
2f8f7 41 66 66 20 20 20 20 20 20 20 20 20 20 2f 2a 20  Aff          /* 
2f8f8 4f 55 54 3a 20 53 65 74 20 74 6f 20 70 6f 69 6e  OUT: Set to poin
2f8f9 74 20 74 6f 20 61 66 66 69 6e 69 74 79 20 73 74  t to affinity st
2f8fa 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ring */.){.  int
2f8fb 20 6e 45 71 20 3d 20 70 4c 65 76 65 6c 2d 3e 70   nEq = pLevel->p
2f8fc 6c 61 6e 2e 6e 45 71 3b 20 20 20 2f 2a 20 54 68  lan.nEq;   /* Th
2f8fd 65 20 6e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 6f  e number of == o
2f8fe 72 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73  r IN constraints
2f8ff 20 74 6f 20 63 6f 64 65 20 2a 2f 0a 20 20 56 64   to code */.  Vd
2f900 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
2f901 70 56 64 62 65 3b 20 20 20 20 20 20 2f 2a 20 54  pVdbe;      /* T
2f902 68 65 20 76 6d 20 75 6e 64 65 72 20 63 6f 6e 73  he vm under cons
2f903 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 49 6e  truction */.  In
2f904 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20  dex *pIdx;      
2f905 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2f906 68 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20 75  he index being u
2f907 73 65 64 20 66 6f 72 20 74 68 69 73 20 6c 6f 6f  sed for this loo
2f908 70 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 20  p */.  int iCur 
2f909 3d 20 70 4c 65 76 65 6c 2d 3e 69 54 61 62 43 75  = pLevel->iTabCu
2f90a 72 3b 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73  r;   /* The curs
2f90b 6f 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  or of the table 
2f90c 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a  */.  WhereTerm *
2f90d 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20  pTerm;          
2f90e 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 63     /* A single c
2f90f 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 20 2a  onstraint term *
2f910 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20  /.  int j;      
2f911 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f912 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
2f913 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 42 61  r */.  int regBa
2f914 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  se;             
2f915 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72 65 67       /* Base reg
2f916 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  ister */.  int n
2f917 52 65 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Reg;            
2f918 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2f919 65 72 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  er of registers 
2f91a 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20  to allocate */. 
2f91b 20 63 68 61 72 20 2a 7a 41 66 66 3b 20 20 20 20   char *zAff;    
2f91c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2f91d 2a 20 41 66 66 69 6e 69 74 79 20 73 74 72 69 6e  * Affinity strin
2f91e 67 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a  g to return */..
2f91f 20 20 2f 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65    /* This module
2f920 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20   is only called 
2f921 6f 6e 20 71 75 65 72 79 20 70 6c 61 6e 73 20 74  on query plans t
2f922 68 61 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78  hat use an index
2f923 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  . */.  assert( p
2f924 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
2f925 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45  ags & WHERE_INDE
2f926 58 45 44 20 29 3b 0a 20 20 70 49 64 78 20 3d 20  XED );.  pIdx = 
2f927 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70  pLevel->plan.u.p
2f928 49 64 78 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72  Idx;..  /* Figur
2f929 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 6d  e out how many m
2f92a 65 6d 6f 72 79 20 63 65 6c 6c 73 20 77 65 20 77  emory cells we w
2f92b 69 6c 6c 20 6e 65 65 64 20 74 68 65 6e 20 61 6c  ill need then al
2f92c 6c 6f 63 61 74 65 20 74 68 65 6d 2e 0a 20 20 2a  locate them..  *
2f92d 2f 0a 20 20 72 65 67 42 61 73 65 20 3d 20 70 50  /.  regBase = pP
2f92e 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a  arse->nMem + 1;.
2f92f 20 20 6e 52 65 67 20 3d 20 70 4c 65 76 65 6c 2d    nReg = pLevel-
2f930 3e 70 6c 61 6e 2e 6e 45 71 20 2b 20 6e 45 78 74  >plan.nEq + nExt
2f931 72 61 52 65 67 3b 0a 20 20 70 50 61 72 73 65 2d  raReg;.  pParse-
2f932 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65 67 3b 0a 0a  >nMem += nReg;..
2f933 20 20 7a 41 66 66 20 3d 20 73 71 6c 69 74 65 33    zAff = sqlite3
2f934 44 62 53 74 72 44 75 70 28 70 50 61 72 73 65 2d  DbStrDup(pParse-
2f935 3e 64 62 2c 20 73 71 6c 69 74 65 33 49 6e 64 65  >db, sqlite3Inde
2f936 78 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20  xAffinityStr(v, 
2f937 70 49 64 78 29 29 3b 0a 20 20 69 66 28 20 21 7a  pIdx));.  if( !z
2f938 41 66 66 20 29 7b 0a 20 20 20 20 70 50 61 72 73  Aff ){.    pPars
2f939 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
2f93a 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20  led = 1;.  }..  
2f93b 2f 2a 20 45 76 61 6c 75 61 74 65 20 74 68 65 20  /* Evaluate the 
2f93c 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
2f93d 69 6e 74 73 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ints.  */.  asse
2f93e 72 74 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  rt( pIdx->nColum
2f93f 6e 3e 3d 6e 45 71 20 29 3b 0a 20 20 66 6f 72 28  n>=nEq );.  for(
2f940 6a 3d 30 3b 20 6a 3c 6e 45 71 3b 20 6a 2b 2b 29  j=0; j<nEq; j++)
2f941 7b 0a 20 20 20 20 69 6e 74 20 72 31 3b 0a 20 20  {.    int r1;.  
2f942 20 20 69 6e 74 20 6b 20 3d 20 70 49 64 78 2d 3e    int k = pIdx->
2f943 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20  aiColumn[j];.   
2f944 20 70 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72   pTerm = findTer
2f945 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6b 2c 20  m(pWC, iCur, k, 
2f946 6e 6f 74 52 65 61 64 79 2c 20 70 4c 65 76 65 6c  notReady, pLevel
2f947 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 2c 20  ->plan.wsFlags, 
2f948 70 49 64 78 29 3b 0a 20 20 20 20 69 66 28 20 4e  pIdx);.    if( N
2f949 45 56 45 52 28 70 54 65 72 6d 3d 3d 30 29 20 29  EVER(pTerm==0) )
2f94a 20 62 72 65 61 6b 3b 0a 20 20 20 20 61 73 73 65   break;.    asse
2f94b 72 74 28 20 28 70 54 65 72 6d 2d 3e 77 74 46 6c  rt( (pTerm->wtFl
2f94c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 45 44  ags & TERM_CODED
2f94d 29 3d 3d 30 20 29 3b 0a 20 20 20 20 72 31 20 3d  )==0 );.    r1 =
2f94e 20 63 6f 64 65 45 71 75 61 6c 69 74 79 54 65 72   codeEqualityTer
2f94f 6d 28 70 50 61 72 73 65 2c 20 70 54 65 72 6d 2c  m(pParse, pTerm,
2f950 20 70 4c 65 76 65 6c 2c 20 72 65 67 42 61 73 65   pLevel, regBase
2f951 2b 6a 29 3b 0a 20 20 20 20 69 66 28 20 72 31 21  +j);.    if( r1!
2f952 3d 72 65 67 42 61 73 65 2b 6a 20 29 7b 0a 20 20  =regBase+j ){.  
2f953 20 20 20 20 69 66 28 20 6e 52 65 67 3d 3d 31 20      if( nReg==1 
2f954 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2f955 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
2f956 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73 65  (pParse, regBase
2f957 29 3b 0a 20 20 20 20 20 20 20 20 72 65 67 42 61  );.        regBa
2f958 73 65 20 3d 20 72 31 3b 0a 20 20 20 20 20 20 7d  se = r1;.      }
2f959 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
2f95a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2f95b 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 31 2c  v, OP_SCopy, r1,
2f95c 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20 20   regBase+j);.   
2f95d 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 74     }.    }.    t
2f95e 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e  estcase( pTerm->
2f95f 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49  eOperator & WO_I
2f960 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 74 65 73  SNULL );.    tes
2f961 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f  tcase( pTerm->eO
2f962 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49 4e 20  perator & WO_IN 
2f963 29 3b 0a 20 20 20 20 69 66 28 20 28 70 54 65 72  );.    if( (pTer
2f964 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28  m->eOperator & (
2f965 57 4f 5f 49 53 4e 55 4c 4c 7c 57 4f 5f 49 4e 29  WO_ISNULL|WO_IN)
2f966 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  )==0 ){.      sq
2f967 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2f968 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65  v, OP_IsNull, re
2f969 67 42 61 73 65 2b 6a 2c 20 70 4c 65 76 65 6c 2d  gBase+j, pLevel-
2f96a 3e 61 64 64 72 42 72 6b 29 3b 0a 20 20 20 20 20  >addrBrk);.     
2f96b 20 69 66 28 20 7a 41 66 66 20 0a 20 20 20 20 20   if( zAff .     
2f96c 20 20 26 26 20 73 71 6c 69 74 65 33 43 6f 6d 70    && sqlite3Comp
2f96d 61 72 65 41 66 66 69 6e 69 74 79 28 70 54 65 72  areAffinity(pTer
2f96e 6d 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74  m->pExpr->pRight
2f96f 2c 20 7a 41 66 66 5b 6a 5d 29 3d 3d 53 51 4c 49  , zAff[j])==SQLI
2f970 54 45 5f 41 46 46 5f 4e 4f 4e 45 0a 20 20 20 20  TE_AFF_NONE.    
2f971 20 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 41 66    ){.        zAf
2f972 66 5b 6a 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46  f[j] = SQLITE_AF
2f973 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a  F_NONE;.      }.
2f974 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 7a 41      }.  }.  *pzA
2f975 66 66 20 3d 20 7a 41 66 66 3b 0a 20 20 72 65 74  ff = zAff;.  ret
2f976 75 72 6e 20 72 65 67 42 61 73 65 3b 0a 7d 0a 0a  urn regBase;.}..
2f977 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
2f978 6f 64 65 20 66 6f 72 20 74 68 65 20 73 74 61 72  ode for the star
2f979 74 20 6f 66 20 74 68 65 20 69 4c 65 76 65 6c 2d  t of the iLevel-
2f97a 74 68 20 6c 6f 6f 70 20 69 6e 20 74 68 65 20 57  th loop in the W
2f97b 48 45 52 45 20 63 6c 61 75 73 65 0a 2a 2a 20 69  HERE clause.** i
2f97c 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 65  mplementation de
2f97d 73 63 72 69 62 65 64 20 62 79 20 70 57 49 6e 66  scribed by pWInf
2f97e 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 42 69 74  o..*/.static Bit
2f97f 6d 61 73 6b 20 63 6f 64 65 4f 6e 65 4c 6f 6f 70  mask codeOneLoop
2f980 53 74 61 72 74 28 0a 20 20 57 68 65 72 65 49 6e  Start(.  WhereIn
2f981 66 6f 20 2a 70 57 49 6e 66 6f 2c 20 20 20 2f 2a  fo *pWInfo,   /*
2f982 20 43 6f 6d 70 6c 65 74 65 20 69 6e 66 6f 72 6d   Complete inform
2f983 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20  ation about the 
2f984 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
2f985 20 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20 20 20    int iLevel,   
2f986 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20         /* Which 
2f987 6c 65 76 65 6c 20 6f 66 20 70 57 49 6e 66 6f 2d  level of pWInfo-
2f988 3e 61 5b 5d 20 73 68 6f 75 6c 64 20 62 65 20 63  >a[] should be c
2f989 6f 64 65 64 20 2a 2f 0a 20 20 75 31 36 20 77 63  oded */.  u16 wc
2f98a 74 72 6c 46 6c 61 67 73 2c 20 20 20 20 20 20 2f  trlFlags,      /
2f98b 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 57 48 45  * One of the WHE
2f98c 52 45 5f 2a 20 66 6c 61 67 73 20 64 65 66 69 6e  RE_* flags defin
2f98d 65 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  ed in sqliteInt.
2f98e 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e  h */.  Bitmask n
2f98f 6f 74 52 65 61 64 79 20 20 20 20 20 2f 2a 20 57  otReady     /* W
2f990 68 69 63 68 20 74 61 62 6c 65 73 20 61 72 65 20  hich tables are 
2f991 63 75 72 72 65 6e 74 6c 79 20 61 76 61 69 6c 61  currently availa
2f992 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ble */.){.  int 
2f993 6a 2c 20 6b 3b 20 20 20 20 20 20 20 20 20 20 20  j, k;           
2f994 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
2f995 73 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b  s */.  int iCur;
2f996 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2f997 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 66  he VDBE cursor f
2f998 6f 72 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  or the table */.
2f999 20 20 69 6e 74 20 61 64 64 72 4e 78 74 3b 20 20    int addrNxt;  
2f99a 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20         /* Where 
2f99b 74 6f 20 6a 75 6d 70 20 74 6f 20 63 6f 6e 74 69  to jump to conti
2f99c 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e 65 78  nue with the nex
2f99d 74 20 49 4e 20 63 61 73 65 20 2a 2f 0a 20 20 69  t IN case */.  i
2f99e 6e 74 20 6f 6d 69 74 54 61 62 6c 65 3b 20 20 20  nt omitTable;   
2f99f 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77      /* True if w
2f9a0 65 20 75 73 65 20 74 68 65 20 69 6e 64 65 78 20  e use the index 
2f9a1 6f 6e 6c 79 20 2a 2f 0a 20 20 69 6e 74 20 62 52  only */.  int bR
2f9a2 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ev;            /
2f9a3 2a 20 54 72 75 65 20 69 66 20 77 65 20 6e 65 65  * True if we nee
2f9a4 64 20 74 6f 20 73 63 61 6e 20 69 6e 20 72 65 76  d to scan in rev
2f9a5 65 72 73 65 20 6f 72 64 65 72 20 2a 2f 0a 20 20  erse order */.  
2f9a6 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76  WhereLevel *pLev
2f9a7 65 6c 3b 20 20 2f 2a 20 54 68 65 20 77 68 65 72  el;  /* The wher
2f9a8 65 20 6c 65 76 65 6c 20 74 6f 20 62 65 20 63 6f  e level to be co
2f9a9 64 65 64 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c  ded */.  WhereCl
2f9aa 61 75 73 65 20 2a 70 57 43 3b 20 20 20 20 2f 2a  ause *pWC;    /*
2f9ab 20 44 65 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f   Decomposition o
2f9ac 66 20 74 68 65 20 65 6e 74 69 72 65 20 57 48 45  f the entire WHE
2f9ad 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57  RE clause */.  W
2f9ae 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b  hereTerm *pTerm;
2f9af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2f9b0 2a 20 41 20 57 48 45 52 45 20 63 6c 61 75 73 65  * A WHERE clause
2f9b1 20 74 65 72 6d 20 2a 2f 0a 20 20 50 61 72 73 65   term */.  Parse
2f9b2 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20   *pParse;       
2f9b3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
2f9b4 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
2f9b5 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20  .  Vdbe *v;     
2f9b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f9b7 20 20 20 2f 2a 20 54 68 65 20 70 72 65 70 61 72     /* The prepar
2f9b8 65 64 20 73 74 6d 74 20 75 6e 64 65 72 20 63 6f  ed stmt under co
2f9b9 6e 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a 20  nstructions */. 
2f9ba 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
2f9bb 69 74 65 6d 20 2a 70 54 61 62 49 74 65 6d 3b 20  item *pTabItem; 
2f9bc 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20   /* FROM clause 
2f9bd 74 65 72 6d 20 62 65 69 6e 67 20 63 6f 64 65 64  term being coded
2f9be 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 42 72   */.  int addrBr
2f9bf 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
2f9c0 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65        /* Jump he
2f9c1 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20  re to break out 
2f9c2 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  of the loop */. 
2f9c3 20 69 6e 74 20 61 64 64 72 43 6f 6e 74 3b 20 20   int addrCont;  
2f9c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f9c5 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f   /* Jump here to
2f9c6 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 6e   continue with n
2f9c7 65 78 74 20 63 79 63 6c 65 20 2a 2f 0a 20 20 69  ext cycle */.  i
2f9c8 6e 74 20 69 52 6f 77 69 64 52 65 67 20 3d 20 30  nt iRowidReg = 0
2f9c9 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69  ;        /* Rowi
2f9ca 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74  d is stored in t
2f9cb 68 69 73 20 72 65 67 69 73 74 65 72 2c 20 69 66  his register, if
2f9cc 20 6e 6f 74 20 7a 65 72 6f 20 2a 2f 0a 20 20 69   not zero */.  i
2f9cd 6e 74 20 69 52 65 6c 65 61 73 65 52 65 67 20 3d  nt iReleaseReg =
2f9ce 20 30 3b 20 20 20 20 20 20 2f 2a 20 54 65 6d 70   0;      /* Temp
2f9cf 20 72 65 67 69 73 74 65 72 20 74 6f 20 66 72 65   register to fre
2f9d0 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  e before returni
2f9d1 6e 67 20 2a 2f 0a 0a 20 20 70 50 61 72 73 65 20  ng */..  pParse 
2f9d2 3d 20 70 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65  = pWInfo->pParse
2f9d3 3b 0a 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e  ;.  v = pParse->
2f9d4 70 56 64 62 65 3b 0a 20 20 70 57 43 20 3d 20 70  pVdbe;.  pWC = p
2f9d5 57 49 6e 66 6f 2d 3e 70 57 43 3b 0a 20 20 70 4c  WInfo->pWC;.  pL
2f9d6 65 76 65 6c 20 3d 20 26 70 57 49 6e 66 6f 2d 3e  evel = &pWInfo->
2f9d7 61 5b 69 4c 65 76 65 6c 5d 3b 0a 20 20 70 54 61  a[iLevel];.  pTa
2f9d8 62 49 74 65 6d 20 3d 20 26 70 57 49 6e 66 6f 2d  bItem = &pWInfo-
2f9d9 3e 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65  >pTabList->a[pLe
2f9da 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 69  vel->iFrom];.  i
2f9db 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e  Cur = pTabItem->
2f9dc 69 43 75 72 73 6f 72 3b 0a 20 20 62 52 65 76 20  iCursor;.  bRev 
2f9dd 3d 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  = (pLevel->plan.
2f9de 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
2f9df 52 45 56 45 52 53 45 29 21 3d 30 3b 0a 20 20 6f  REVERSE)!=0;.  o
2f9e0 6d 69 74 54 61 62 6c 65 20 3d 20 28 70 4c 65 76  mitTable = (pLev
2f9e1 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2f9e2 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c   & WHERE_IDX_ONL
2f9e3 59 29 21 3d 30 20 0a 20 20 20 20 20 20 20 20 20  Y)!=0 .         
2f9e4 20 20 26 26 20 28 77 63 74 72 6c 46 6c 61 67 73    && (wctrlFlags
2f9e5 20 26 20 57 48 45 52 45 5f 46 4f 52 43 45 5f 54   & WHERE_FORCE_T
2f9e6 41 42 4c 45 29 3d 3d 30 3b 0a 0a 20 20 2f 2a 20  ABLE)==0;..  /* 
2f9e7 43 72 65 61 74 65 20 6c 61 62 65 6c 73 20 66 6f  Create labels fo
2f9e8 72 20 74 68 65 20 22 62 72 65 61 6b 22 20 61 6e  r the "break" an
2f9e9 64 20 22 63 6f 6e 74 69 6e 75 65 22 20 69 6e 73  d "continue" ins
2f9ea 74 72 75 63 74 69 6f 6e 73 0a 20 20 2a 2a 20 66  tructions.  ** f
2f9eb 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c  or the current l
2f9ec 6f 6f 70 2e 20 20 4a 75 6d 70 20 74 6f 20 61 64  oop.  Jump to ad
2f9ed 64 72 42 72 6b 20 74 6f 20 62 72 65 61 6b 20 6f  drBrk to break o
2f9ee 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2e 0a 20 20  ut of a loop..  
2f9ef 2a 2a 20 4a 75 6d 70 20 74 6f 20 63 6f 6e 74 20  ** Jump to cont 
2f9f0 74 6f 20 67 6f 20 69 6d 6d 65 64 69 61 74 65 6c  to go immediatel
2f9f1 79 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 74  y to the next it
2f9f2 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20  eration of the. 
2f9f3 20 2a 2a 20 6c 6f 6f 70 2e 0a 20 20 2a 2a 0a 20   ** loop..  **. 
2f9f4 20 2a 2a 20 57 68 65 6e 20 74 68 65 72 65 20 69   ** When there i
2f9f5 73 20 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72  s an IN operator
2f9f6 2c 20 77 65 20 61 6c 73 6f 20 68 61 76 65 20 61  , we also have a
2f9f7 20 22 61 64 64 72 4e 78 74 22 20 6c 61 62 65 6c   "addrNxt" label
2f9f8 20 74 68 61 74 0a 20 20 2a 2a 20 6d 65 61 6e 73   that.  ** means
2f9f9 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74   to continue wit
2f9fa 68 20 74 68 65 20 6e 65 78 74 20 49 4e 20 76 61  h the next IN va
2f9fb 6c 75 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 2e  lue combination.
2f9fc 20 20 57 68 65 6e 0a 20 20 2a 2a 20 74 68 65 72    When.  ** ther
2f9fd 65 20 61 72 65 20 6e 6f 20 49 4e 20 6f 70 65 72  e are no IN oper
2f9fe 61 74 6f 72 73 20 69 6e 20 74 68 65 20 63 6f 6e  ators in the con
2f9ff 73 74 72 61 69 6e 74 73 2c 20 74 68 65 20 22 61  straints, the "a
2fa00 64 64 72 4e 78 74 22 20 6c 61 62 65 6c 0a 20 20  ddrNxt" label.  
2fa01 2a 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  ** is the same a
2fa02 73 20 22 61 64 64 72 42 72 6b 22 2e 0a 20 20 2a  s "addrBrk"..  *
2fa03 2f 0a 20 20 61 64 64 72 42 72 6b 20 3d 20 70 4c  /.  addrBrk = pL
2fa04 65 76 65 6c 2d 3e 61 64 64 72 42 72 6b 20 3d 20  evel->addrBrk = 
2fa05 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74 20  pLevel->addrNxt 
2fa06 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
2fa07 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 61 64 64  eLabel(v);.  add
2fa08 72 43 6f 6e 74 20 3d 20 70 4c 65 76 65 6c 2d 3e  rCont = pLevel->
2fa09 61 64 64 72 43 6f 6e 74 20 3d 20 73 71 6c 69 74  addrCont = sqlit
2fa0a 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
2fa0b 76 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69  v);..  /* If thi
2fa0c 73 20 69 73 20 74 68 65 20 72 69 67 68 74 20 74  s is the right t
2fa0d 61 62 6c 65 20 6f 66 20 61 20 4c 45 46 54 20 4f  able of a LEFT O
2fa0e 55 54 45 52 20 4a 4f 49 4e 2c 20 61 6c 6c 6f 63  UTER JOIN, alloc
2fa0f 61 74 65 20 61 6e 64 0a 20 20 2a 2a 20 69 6e 69  ate and.  ** ini
2fa10 74 69 61 6c 69 7a 65 20 61 20 6d 65 6d 6f 72 79  tialize a memory
2fa11 20 63 65 6c 6c 20 74 68 61 74 20 72 65 63 6f 72   cell that recor
2fa12 64 73 20 69 66 20 74 68 69 73 20 74 61 62 6c 65  ds if this table
2fa13 20 6d 61 74 63 68 65 73 20 61 6e 79 0a 20 20 2a   matches any.  *
2fa14 2a 20 72 6f 77 20 6f 66 20 74 68 65 20 6c 65 66  * row of the lef
2fa15 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 6a  t table of the j
2fa16 6f 69 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  oin..  */.  if( 
2fa17 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 3e 30 20  pLevel->iFrom>0 
2fa18 26 26 20 28 70 54 61 62 49 74 65 6d 5b 30 5d 2e  && (pTabItem[0].
2fa19 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4c 45  jointype & JT_LE
2fa1a 46 54 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 4c  FT)!=0 ){.    pL
2fa1b 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20  evel->iLeftJoin 
2fa1c 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2fa1d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2fa1e 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
2fa1f 74 65 67 65 72 2c 20 30 2c 20 70 4c 65 76 65 6c  teger, 0, pLevel
2fa20 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a 20 20  ->iLeftJoin);.  
2fa21 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
2fa22 2c 20 22 69 6e 69 74 20 4c 45 46 54 20 4a 4f 49  , "init LEFT JOI
2fa23 4e 20 6e 6f 2d 6d 61 74 63 68 20 66 6c 61 67 22  N no-match flag"
2fa24 29 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66  ));.  }..#ifndef
2fa25 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
2fa26 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20  TUALTABLE.  if( 
2fa27 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77   (pLevel->plan.w
2fa28 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 56  sFlags & WHERE_V
2fa29 49 52 54 55 41 4c 54 41 42 4c 45 29 21 3d 30 20  IRTUALTABLE)!=0 
2fa2a 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65 20 30  ){.    /* Case 0
2fa2b 3a 20 20 54 68 65 20 74 61 62 6c 65 20 69 73 20  :  The table is 
2fa2c 61 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 2e  a virtual-table.
2fa2d 20 20 55 73 65 20 74 68 65 20 56 46 69 6c 74 65    Use the VFilte
2fa2e 72 20 61 6e 64 20 56 4e 65 78 74 0a 20 20 20 20  r and VNext.    
2fa2f 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20 61  **          to a
2fa30 63 63 65 73 73 20 74 68 65 20 64 61 74 61 2e 0a  ccess the data..
2fa31 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69      */.    int i
2fa32 52 65 67 3b 20 20 20 2f 2a 20 50 33 20 56 61 6c  Reg;   /* P3 Val
2fa33 75 65 20 66 6f 72 20 4f 50 5f 56 46 69 6c 74 65  ue for OP_VFilte
2fa34 72 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  r */.    sqlite3
2fa35 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 56 74  _index_info *pVt
2fa36 61 62 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d 3e  abIdx = pLevel->
2fa37 70 6c 61 6e 2e 75 2e 70 56 74 61 62 49 64 78 3b  plan.u.pVtabIdx;
2fa38 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6e 73 74 72  .    int nConstr
2fa39 61 69 6e 74 20 3d 20 70 56 74 61 62 49 64 78 2d  aint = pVtabIdx-
2fa3a 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20  >nConstraint;.  
2fa3b 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33    struct sqlite3
2fa3c 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
2fa3d 74 5f 75 73 61 67 65 20 2a 61 55 73 61 67 65 20  t_usage *aUsage 
2fa3e 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  =.              
2fa3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa41 20 20 70 56 74 61 62 49 64 78 2d 3e 61 43 6f 6e    pVtabIdx->aCon
2fa42 73 74 72 61 69 6e 74 55 73 61 67 65 3b 0a 20 20  straintUsage;.  
2fa43 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73    const struct s
2fa44 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e  qlite3_index_con
2fa45 73 74 72 61 69 6e 74 20 2a 61 43 6f 6e 73 74 72  straint *aConstr
2fa46 61 69 6e 74 20 3d 0a 20 20 20 20 20 20 20 20 20  aint =.         
2fa47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa49 20 20 20 20 20 20 20 70 56 74 61 62 49 64 78 2d         pVtabIdx-
2fa4a 3e 61 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 0a 20  >aConstraint;.. 
2fa4b 20 20 20 69 52 65 67 20 3d 20 73 71 6c 69 74 65     iReg = sqlite
2fa4c 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50  3GetTempRange(pP
2fa4d 61 72 73 65 2c 20 6e 43 6f 6e 73 74 72 61 69 6e  arse, nConstrain
2fa4e 74 2b 32 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d  t+2);.    for(j=
2fa4f 31 3b 20 6a 3c 3d 6e 43 6f 6e 73 74 72 61 69 6e  1; j<=nConstrain
2fa50 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 66  t; j++){.      f
2fa51 6f 72 28 6b 3d 30 3b 20 6b 3c 6e 43 6f 6e 73 74  or(k=0; k<nConst
2fa52 72 61 69 6e 74 3b 20 6b 2b 2b 29 7b 0a 20 20 20  raint; k++){.   
2fa53 20 20 20 20 20 69 66 28 20 61 55 73 61 67 65 5b       if( aUsage[
2fa54 6b 5d 2e 61 72 67 76 49 6e 64 65 78 3d 3d 6a 20  k].argvIndex==j 
2fa55 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  ){.          int
2fa56 20 69 54 65 72 6d 20 3d 20 61 43 6f 6e 73 74 72   iTerm = aConstr
2fa57 61 69 6e 74 5b 6b 5d 2e 69 54 65 72 6d 4f 66 66  aint[k].iTermOff
2fa58 73 65 74 3b 0a 20 20 20 20 20 20 20 20 20 20 73  set;.          s
2fa59 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
2fa5a 50 61 72 73 65 2c 20 70 57 43 2d 3e 61 5b 69 54  Parse, pWC->a[iT
2fa5b 65 72 6d 5d 2e 70 45 78 70 72 2d 3e 70 52 69 67  erm].pExpr->pRig
2fa5c 68 74 2c 20 69 52 65 67 2b 6a 2b 31 29 3b 0a 20  ht, iReg+j+1);. 
2fa5d 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2fa5e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2fa5f 7d 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 6e  }.      if( k==n
2fa60 43 6f 6e 73 74 72 61 69 6e 74 20 29 20 62 72 65  Constraint ) bre
2fa61 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  ak;.    }.    sq
2fa62 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2fa63 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70  v, OP_Integer, p
2fa64 56 74 61 62 49 64 78 2d 3e 69 64 78 4e 75 6d 2c  VtabIdx->idxNum,
2fa65 20 69 52 65 67 29 3b 0a 20 20 20 20 73 71 6c 69   iReg);.    sqli
2fa66 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2fa67 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6a 2d 31   OP_Integer, j-1
2fa68 2c 20 69 52 65 67 2b 31 29 3b 0a 20 20 20 20 73  , iReg+1);.    s
2fa69 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2fa6a 28 76 2c 20 4f 50 5f 56 46 69 6c 74 65 72 2c 20  (v, OP_VFilter, 
2fa6b 69 43 75 72 2c 20 61 64 64 72 42 72 6b 2c 20 69  iCur, addrBrk, i
2fa6c 52 65 67 2c 20 70 56 74 61 62 49 64 78 2d 3e 69  Reg, pVtabIdx->i
2fa6d 64 78 53 74 72 2c 0a 20 20 20 20 20 20 20 20 20  dxStr,.         
2fa6e 20 20 20 20 20 20 20 20 20 20 20 20 20 70 56 74               pVt
2fa6f 61 62 49 64 78 2d 3e 6e 65 65 64 54 6f 46 72 65  abIdx->needToFre
2fa70 65 49 64 78 53 74 72 20 3f 20 50 34 5f 4d 50 52  eIdxStr ? P4_MPR
2fa71 49 4e 54 46 20 3a 20 50 34 5f 53 54 41 54 49 43  INTF : P4_STATIC
2fa72 29 3b 0a 20 20 20 20 70 56 74 61 62 49 64 78 2d  );.    pVtabIdx-
2fa73 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74  >needToFreeIdxSt
2fa74 72 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 6a  r = 0;.    for(j
2fa75 3d 30 3b 20 6a 3c 6e 43 6f 6e 73 74 72 61 69 6e  =0; j<nConstrain
2fa76 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69  t; j++){.      i
2fa77 66 28 20 61 55 73 61 67 65 5b 6a 5d 2e 6f 6d 69  f( aUsage[j].omi
2fa78 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  t ){.        int
2fa79 20 69 54 65 72 6d 20 3d 20 61 43 6f 6e 73 74 72   iTerm = aConstr
2fa7a 61 69 6e 74 5b 6a 5d 2e 69 54 65 72 6d 4f 66 66  aint[j].iTermOff
2fa7b 73 65 74 3b 0a 20 20 20 20 20 20 20 20 64 69 73  set;.        dis
2fa7c 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c  ableTerm(pLevel,
2fa7d 20 26 70 57 43 2d 3e 61 5b 69 54 65 72 6d 5d 29   &pWC->a[iTerm])
2fa7e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2fa7f 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d      pLevel->op =
2fa80 20 4f 50 5f 56 4e 65 78 74 3b 0a 20 20 20 20 70   OP_VNext;.    p
2fa81 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69 43 75 72  Level->p1 = iCur
2fa82 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32  ;.    pLevel->p2
2fa83 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
2fa84 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20  rrentAddr(v);.  
2fa85 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2fa86 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65  TempRange(pParse
2fa87 2c 20 69 52 65 67 2c 20 6e 43 6f 6e 73 74 72 61  , iReg, nConstra
2fa88 69 6e 74 2b 32 29 3b 0a 20 20 7d 65 6c 73 65 0a  int+2);.  }else.
2fa89 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2fa8a 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2fa8b 4c 45 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4c 65  LE */..  if( pLe
2fa8c 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
2fa8d 73 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44 5f  s & WHERE_ROWID_
2fa8e 45 51 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73  EQ ){.    /* Cas
2fa8f 65 20 31 3a 20 20 57 65 20 63 61 6e 20 64 69 72  e 1:  We can dir
2fa90 65 63 74 6c 79 20 72 65 66 65 72 65 6e 63 65 20  ectly reference 
2fa91 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 75 73 69  a single row usi
2fa92 6e 67 20 61 6e 0a 20 20 20 20 2a 2a 20 20 20 20  ng an.    **    
2fa93 20 20 20 20 20 20 65 71 75 61 6c 69 74 79 20 63        equality c
2fa94 6f 6d 70 61 72 69 73 6f 6e 20 61 67 61 69 6e 73  omparison agains
2fa95 74 20 74 68 65 20 52 4f 57 49 44 20 66 69 65 6c  t the ROWID fiel
2fa96 64 2e 20 20 4f 72 0a 20 20 20 20 2a 2a 20 20 20  d.  Or.    **   
2fa97 20 20 20 20 20 20 20 77 65 20 72 65 66 65 72 65         we refere
2fa98 6e 63 65 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77  nce multiple row
2fa99 73 20 75 73 69 6e 67 20 61 20 22 72 6f 77 69 64  s using a "rowid
2fa9a 20 49 4e 20 28 2e 2e 2e 29 22 0a 20 20 20 20 2a   IN (...)".    *
2fa9b 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74  *          const
2fa9c 72 75 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ruct..    */.   
2fa9d 20 69 52 65 6c 65 61 73 65 52 65 67 20 3d 20 73   iReleaseReg = s
2fa9e 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2fa9f 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 70 54  (pParse);.    pT
2faa0 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70  erm = findTerm(p
2faa1 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f  WC, iCur, -1, no
2faa2 74 52 65 61 64 79 2c 20 57 4f 5f 45 51 7c 57 4f  tReady, WO_EQ|WO
2faa3 5f 49 4e 2c 20 30 29 3b 0a 20 20 20 20 61 73 73  _IN, 0);.    ass
2faa4 65 72 74 28 20 70 54 65 72 6d 21 3d 30 20 29 3b  ert( pTerm!=0 );
2faa5 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65  .    assert( pTe
2faa6 72 6d 2d 3e 70 45 78 70 72 21 3d 30 20 29 3b 0a  rm->pExpr!=0 );.
2faa7 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65 72      assert( pTer
2faa8 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69  m->leftCursor==i
2faa9 43 75 72 20 29 3b 0a 20 20 20 20 61 73 73 65 72  Cur );.    asser
2faaa 74 28 20 6f 6d 69 74 54 61 62 6c 65 3d 3d 30 20  t( omitTable==0 
2faab 29 3b 0a 20 20 20 20 69 52 6f 77 69 64 52 65 67  );.    iRowidReg
2faac 20 3d 20 63 6f 64 65 45 71 75 61 6c 69 74 79 54   = codeEqualityT
2faad 65 72 6d 28 70 50 61 72 73 65 2c 20 70 54 65 72  erm(pParse, pTer
2faae 6d 2c 20 70 4c 65 76 65 6c 2c 20 69 52 65 6c 65  m, pLevel, iRele
2faaf 61 73 65 52 65 67 29 3b 0a 20 20 20 20 61 64 64  aseReg);.    add
2fab0 72 4e 78 74 20 3d 20 70 4c 65 76 65 6c 2d 3e 61  rNxt = pLevel->a
2fab1 64 64 72 4e 78 74 3b 0a 20 20 20 20 73 71 6c 69  ddrNxt;.    sqli
2fab2 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2fab3 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 69   OP_MustBeInt, i
2fab4 52 6f 77 69 64 52 65 67 2c 20 61 64 64 72 4e 78  RowidReg, addrNx
2fab5 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  t);.    sqlite3V
2fab6 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2fab7 4e 6f 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c  NotExists, iCur,
2fab8 20 61 64 64 72 4e 78 74 2c 20 69 52 6f 77 69 64   addrNxt, iRowid
2fab9 52 65 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Reg);.    sqlite
2faba 33 45 78 70 72 43 61 63 68 65 53 74 6f 72 65 28  3ExprCacheStore(
2fabb 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20 2d 31  pParse, iCur, -1
2fabc 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20  , iRowidReg);.  
2fabd 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
2fabe 2c 20 22 70 6b 22 29 29 3b 0a 20 20 20 20 70 4c  , "pk"));.    pL
2fabf 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 4e 6f  evel->op = OP_No
2fac0 6f 70 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  op;.  }else if( 
2fac1 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
2fac2 6c 61 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57  lags & WHERE_ROW
2fac3 49 44 5f 52 41 4e 47 45 20 29 7b 0a 20 20 20 20  ID_RANGE ){.    
2fac4 2f 2a 20 43 61 73 65 20 32 3a 20 20 57 65 20 68  /* Case 2:  We h
2fac5 61 76 65 20 61 6e 20 69 6e 65 71 75 61 6c 69 74  ave an inequalit
2fac6 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 67 61  y comparison aga
2fac7 69 6e 73 74 20 74 68 65 20 52 4f 57 49 44 20 66  inst the ROWID f
2fac8 69 65 6c 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ield..    */.   
2fac9 20 69 6e 74 20 74 65 73 74 4f 70 20 3d 20 4f 50   int testOp = OP
2faca 5f 4e 6f 6f 70 3b 0a 20 20 20 20 69 6e 74 20 73  _Noop;.    int s
2facb 74 61 72 74 3b 0a 20 20 20 20 69 6e 74 20 6d 65  tart;.    int me
2facc 6d 45 6e 64 56 61 6c 75 65 20 3d 20 30 3b 0a 20  mEndValue = 0;. 
2facd 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 53     WhereTerm *pS
2face 74 61 72 74 2c 20 2a 70 45 6e 64 3b 0a 0a 20 20  tart, *pEnd;..  
2facf 20 20 61 73 73 65 72 74 28 20 6f 6d 69 74 54 61    assert( omitTa
2fad0 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70 53  ble==0 );.    pS
2fad1 74 61 72 74 20 3d 20 66 69 6e 64 54 65 72 6d 28  tart = findTerm(
2fad2 70 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e  pWC, iCur, -1, n
2fad3 6f 74 52 65 61 64 79 2c 20 57 4f 5f 47 54 7c 57  otReady, WO_GT|W
2fad4 4f 5f 47 45 2c 20 30 29 3b 0a 20 20 20 20 70 45  O_GE, 0);.    pE
2fad5 6e 64 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57  nd = findTerm(pW
2fad6 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74  C, iCur, -1, not
2fad7 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f  Ready, WO_LT|WO_
2fad8 4c 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  LE, 0);.    if( 
2fad9 62 52 65 76 20 29 7b 0a 20 20 20 20 20 20 70 54  bRev ){.      pT
2fada 65 72 6d 20 3d 20 70 53 74 61 72 74 3b 0a 20 20  erm = pStart;.  
2fadb 20 20 20 20 70 53 74 61 72 74 20 3d 20 70 45 6e      pStart = pEn
2fadc 64 3b 0a 20 20 20 20 20 20 70 45 6e 64 20 3d 20  d;.      pEnd = 
2fadd 70 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 20  pTerm;.    }.   
2fade 20 69 66 28 20 70 53 74 61 72 74 20 29 7b 0a 20   if( pStart ){. 
2fadf 20 20 20 20 20 45 78 70 72 20 2a 70 58 3b 20 20       Expr *pX;  
2fae0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2fae1 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  e expression tha
2fae2 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 73 74  t defines the st
2fae3 61 72 74 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 20  art bound */.   
2fae4 20 20 20 69 6e 74 20 72 31 2c 20 72 54 65 6d 70     int r1, rTemp
2fae5 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69  ;        /* Regi
2fae6 73 74 65 72 73 20 66 6f 72 20 68 6f 6c 64 69 6e  sters for holdin
2fae7 67 20 74 68 65 20 73 74 61 72 74 20 62 6f 75 6e  g the start boun
2fae8 64 61 72 79 20 2a 2f 0a 0a 20 20 20 20 20 20 2f  dary */..      /
2fae9 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
2faea 63 6f 6e 73 74 61 6e 74 20 6d 61 70 73 20 54 4b  constant maps TK
2faeb 5f 78 78 20 63 6f 64 65 73 20 69 6e 74 6f 20 63  _xx codes into c
2faec 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 0a 20 20  orresponding .  
2faed 20 20 20 20 2a 2a 20 73 65 65 6b 20 6f 70 63 6f      ** seek opco
2faee 64 65 73 2e 20 20 49 74 20 64 65 70 65 6e 64 73  des.  It depends
2faef 20 6f 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72   on a particular
2faf0 20 6f 72 64 65 72 69 6e 67 20 6f 66 20 54 4b 5f   ordering of TK_
2faf1 78 78 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  xx.      */.    
2faf2 20 20 63 6f 6e 73 74 20 75 38 20 61 4d 6f 76 65    const u8 aMove
2faf3 4f 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20  Op[] = {.       
2faf4 20 20 20 20 2f 2a 20 54 4b 5f 47 54 20 2a 2f 20      /* TK_GT */ 
2faf5 20 4f 50 5f 53 65 65 6b 47 74 2c 0a 20 20 20 20   OP_SeekGt,.    
2faf6 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 4c 45 20         /* TK_LE 
2faf7 2a 2f 20 20 4f 50 5f 53 65 65 6b 4c 65 2c 0a 20  */  OP_SeekLe,. 
2faf8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f            /* TK_
2faf9 4c 54 20 2a 2f 20 20 4f 50 5f 53 65 65 6b 4c 74  LT */  OP_SeekLt
2fafa 2c 0a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,.           /* 
2fafb 54 4b 5f 47 45 20 2a 2f 20 20 4f 50 5f 53 65 65  TK_GE */  OP_See
2fafc 6b 47 65 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20  kGe.      };.   
2fafd 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45     assert( TK_LE
2fafe 3d 3d 54 4b 5f 47 54 2b 31 20 29 3b 20 20 20 20  ==TK_GT+1 );    
2faff 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
2fb00 68 65 20 6f 72 64 65 72 69 6e 67 2e 2e 20 2a 2f  he ordering.. */
2fb01 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
2fb02 4b 5f 4c 54 3d 3d 54 4b 5f 47 54 2b 32 20 29 3b  K_LT==TK_GT+2 );
2fb03 20 20 20 20 20 20 2f 2a 20 20 2e 2e 2e 20 6f 66        /*  ... of
2fb04 20 74 68 65 20 54 4b 5f 78 78 20 76 61 6c 75 65   the TK_xx value
2fb05 73 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73  s... */.      as
2fb06 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 54 4b 5f  sert( TK_GE==TK_
2fb07 47 54 2b 33 20 29 3b 20 20 20 20 20 20 2f 2a 20  GT+3 );      /* 
2fb08 20 2e 2e 2e 20 69 73 20 63 6f 72 72 65 63 63 74   ... is correcct
2fb09 2e 20 2a 2f 0a 0a 20 20 20 20 20 20 70 58 20 3d  . */..      pX =
2fb0a 20 70 53 74 61 72 74 2d 3e 70 45 78 70 72 3b 0a   pStart->pExpr;.
2fb0b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 58        assert( pX
2fb0c 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  !=0 );.      ass
2fb0d 65 72 74 28 20 70 53 74 61 72 74 2d 3e 6c 65 66  ert( pStart->lef
2fb0e 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 29 3b  tCursor==iCur );
2fb0f 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69  .      r1 = sqli
2fb10 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
2fb11 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67  pParse, pX->pRig
2fb12 68 74 2c 20 26 72 54 65 6d 70 29 3b 0a 20 20 20  ht, &rTemp);.   
2fb13 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2fb14 64 4f 70 33 28 76 2c 20 61 4d 6f 76 65 4f 70 5b  dOp3(v, aMoveOp[
2fb15 70 58 2d 3e 6f 70 2d 54 4b 5f 47 54 5d 2c 20 69  pX->op-TK_GT], i
2fb16 43 75 72 2c 20 61 64 64 72 42 72 6b 2c 20 72 31  Cur, addrBrk, r1
2fb17 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
2fb18 6d 65 6e 74 28 28 76 2c 20 22 70 6b 22 29 29 3b  ment((v, "pk"));
2fb19 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2fb1a 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
2fb1b 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 31  hange(pParse, r1
2fb1c 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
2fb1d 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
2fb1e 67 28 70 50 61 72 73 65 2c 20 72 54 65 6d 70 29  g(pParse, rTemp)
2fb1f 3b 0a 20 20 20 20 20 20 64 69 73 61 62 6c 65 54  ;.      disableT
2fb20 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 53 74 61  erm(pLevel, pSta
2fb21 72 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  rt);.    }else{.
2fb22 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2fb23 65 41 64 64 4f 70 32 28 76 2c 20 62 52 65 76 20  eAddOp2(v, bRev 
2fb24 3f 20 4f 50 5f 4c 61 73 74 20 3a 20 4f 50 5f 52  ? OP_Last : OP_R
2fb25 65 77 69 6e 64 2c 20 69 43 75 72 2c 20 61 64 64  ewind, iCur, add
2fb26 72 42 72 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 20  rBrk);.    }.   
2fb27 20 69 66 28 20 70 45 6e 64 20 29 7b 0a 20 20 20   if( pEnd ){.   
2fb28 20 20 20 45 78 70 72 20 2a 70 58 3b 0a 20 20 20     Expr *pX;.   
2fb29 20 20 20 70 58 20 3d 20 70 45 6e 64 2d 3e 70 45     pX = pEnd->pE
2fb2a 78 70 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72  xpr;.      asser
2fb2b 74 28 20 70 58 21 3d 30 20 29 3b 0a 20 20 20 20  t( pX!=0 );.    
2fb2c 20 20 61 73 73 65 72 74 28 20 70 45 6e 64 2d 3e    assert( pEnd->
2fb2d 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72  leftCursor==iCur
2fb2e 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 45 6e 64   );.      memEnd
2fb2f 56 61 6c 75 65 20 3d 20 2b 2b 70 50 61 72 73 65  Value = ++pParse
2fb30 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 73 71  ->nMem;.      sq
2fb31 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
2fb32 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74  arse, pX->pRight
2fb33 2c 20 6d 65 6d 45 6e 64 56 61 6c 75 65 29 3b 0a  , memEndValue);.
2fb34 20 20 20 20 20 20 69 66 28 20 70 58 2d 3e 6f 70        if( pX->op
2fb35 3d 3d 54 4b 5f 4c 54 20 7c 7c 20 70 58 2d 3e 6f  ==TK_LT || pX->o
2fb36 70 3d 3d 54 4b 5f 47 54 20 29 7b 0a 20 20 20 20  p==TK_GT ){.    
2fb37 20 20 20 20 74 65 73 74 4f 70 20 3d 20 62 52 65      testOp = bRe
2fb38 76 20 3f 20 4f 50 5f 4c 65 20 3a 20 4f 50 5f 47  v ? OP_Le : OP_G
2fb39 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  e;.      }else{.
2fb3a 20 20 20 20 20 20 20 20 74 65 73 74 4f 70 20 3d          testOp =
2fb3b 20 62 52 65 76 20 3f 20 4f 50 5f 4c 74 20 3a 20   bRev ? OP_Lt : 
2fb3c 4f 50 5f 47 74 3b 0a 20 20 20 20 20 20 7d 0a 20  OP_Gt;.      }. 
2fb3d 20 20 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d       disableTerm
2fb3e 28 70 4c 65 76 65 6c 2c 20 70 45 6e 64 29 3b 0a  (pLevel, pEnd);.
2fb3f 20 20 20 20 7d 0a 20 20 20 20 73 74 61 72 74 20      }.    start 
2fb40 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
2fb41 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20  rentAddr(v);.   
2fb42 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 62 52   pLevel->op = bR
2fb43 65 76 20 3f 20 4f 50 5f 50 72 65 76 20 3a 20 4f  ev ? OP_Prev : O
2fb44 50 5f 4e 65 78 74 3b 0a 20 20 20 20 70 4c 65 76  P_Next;.    pLev
2fb45 65 6c 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0a 20  el->p1 = iCur;. 
2fb46 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20     pLevel->p2 = 
2fb47 73 74 61 72 74 3b 0a 20 20 20 20 70 4c 65 76 65  start;.    pLeve
2fb48 6c 2d 3e 70 35 20 3d 20 28 70 53 74 61 72 74 3d  l->p5 = (pStart=
2fb49 3d 30 20 26 26 20 70 45 6e 64 3d 3d 30 29 20 3f  =0 && pEnd==0) ?
2fb4a 31 3a 30 3b 0a 20 20 20 20 69 66 28 20 74 65 73  1:0;.    if( tes
2fb4b 74 4f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a  tOp!=OP_Noop ){.
2fb4c 20 20 20 20 20 20 69 52 6f 77 69 64 52 65 67 20        iRowidReg 
2fb4d 3d 20 69 52 65 6c 65 61 73 65 52 65 67 20 3d 20  = iReleaseReg = 
2fb4e 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
2fb4f 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  g(pParse);.     
2fb50 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2fb51 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20  p2(v, OP_Rowid, 
2fb52 69 43 75 72 2c 20 69 52 6f 77 69 64 52 65 67 29  iCur, iRowidReg)
2fb53 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
2fb54 78 70 72 43 61 63 68 65 53 74 6f 72 65 28 70 50  xprCacheStore(pP
2fb55 61 72 73 65 2c 20 69 43 75 72 2c 20 2d 31 2c 20  arse, iCur, -1, 
2fb56 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20  iRowidReg);.    
2fb57 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2fb58 4f 70 33 28 76 2c 20 74 65 73 74 4f 70 2c 20 6d  Op3(v, testOp, m
2fb59 65 6d 45 6e 64 56 61 6c 75 65 2c 20 61 64 64 72  emEndValue, addr
2fb5a 42 72 6b 2c 20 69 52 6f 77 69 64 52 65 67 29 3b  Brk, iRowidReg);
2fb5b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2fb5c 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51  beChangeP5(v, SQ
2fb5d 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
2fb5e 20 7c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46   | SQLITE_JUMPIF
2fb5f 4e 55 4c 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  NULL);.    }.  }
2fb60 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d  else if( pLevel-
2fb61 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2fb62 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41  (WHERE_COLUMN_RA
2fb63 4e 47 45 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  NGE|WHERE_COLUMN
2fb64 5f 45 51 29 20 29 7b 0a 20 20 20 20 2f 2a 20 43  _EQ) ){.    /* C
2fb65 61 73 65 20 33 3a 20 41 20 73 63 61 6e 20 75 73  ase 3: A scan us
2fb66 69 6e 67 20 61 6e 20 69 6e 64 65 78 2e 0a 20 20  ing an index..  
2fb67 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
2fb68 20 20 20 20 54 68 65 20 57 48 45 52 45 20 63 6c      The WHERE cl
2fb69 61 75 73 65 20 6d 61 79 20 63 6f 6e 74 61 69 6e  ause may contain
2fb6a 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 65 71   zero or more eq
2fb6b 75 61 6c 69 74 79 20 0a 20 20 20 20 2a 2a 20 20  uality .    **  
2fb6c 20 20 20 20 20 20 20 74 65 72 6d 73 20 28 22 3d         terms ("=
2fb6d 3d 22 20 6f 72 20 22 49 4e 22 20 6f 70 65 72 61  =" or "IN" opera
2fb6e 74 6f 72 73 29 20 74 68 61 74 20 72 65 66 65 72  tors) that refer
2fb6f 20 74 6f 20 74 68 65 20 4e 0a 20 20 20 20 2a 2a   to the N.    **
2fb70 20 20 20 20 20 20 20 20 20 6c 65 66 74 2d 6d 6f           left-mo
2fb71 73 74 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68  st columns of th
2fb72 65 20 69 6e 64 65 78 2e 20 49 74 20 6d 61 79 20  e index. It may 
2fb73 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 0a 20 20 20  also contain.   
2fb74 20 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 65 71   **         ineq
2fb75 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e  uality constrain
2fb76 74 73 20 28 3e 2c 20 3c 2c 20 3e 3d 20 6f 72 20  ts (>, <, >= or 
2fb77 3c 3d 29 20 6f 6e 20 74 68 65 20 69 6e 64 65 78  <=) on the index
2fb78 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  ed.    **       
2fb79 20 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 6d    column that im
2fb7a 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77  mediately follow
2fb7b 73 20 74 68 65 20 4e 20 65 71 75 61 6c 69 74 69  s the N equaliti
2fb7c 65 73 2e 20 4f 6e 6c 79 20 0a 20 20 20 20 2a 2a  es. Only .    **
2fb7d 20 20 20 20 20 20 20 20 20 74 68 65 20 72 69 67           the rig
2fb7e 68 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 63  ht-most column c
2fb7f 61 6e 20 62 65 20 61 6e 20 69 6e 65 71 75 61 6c  an be an inequal
2fb80 69 74 79 20 2d 20 74 68 65 20 72 65 73 74 20 6d  ity - the rest m
2fb81 75 73 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  ust.    **      
2fb82 20 20 20 75 73 65 20 74 68 65 20 22 3d 3d 22 20     use the "==" 
2fb83 61 6e 64 20 22 49 4e 22 20 6f 70 65 72 61 74 6f  and "IN" operato
2fb84 72 73 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  rs. For example,
2fb85 20 69 66 20 74 68 65 20 0a 20 20 20 20 2a 2a 20   if the .    ** 
2fb86 20 20 20 20 20 20 20 20 69 6e 64 65 78 20 69 73          index is
2fb87 20 6f 6e 20 28 78 2c 79 2c 7a 29 2c 20 74 68 65   on (x,y,z), the
2fb88 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
2fb89 63 6c 61 75 73 65 73 20 61 72 65 20 61 6c 6c 20  clauses are all 
2fb8a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2fb8b 6f 70 74 69 6d 69 7a 65 64 3a 0a 20 20 20 20 2a  optimized:.    *
2fb8c 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  *.    **        
2fb8d 20 20 20 20 78 3d 35 0a 20 20 20 20 2a 2a 20 20      x=5.    **  
2fb8e 20 20 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e            x=5 AN
2fb8f 44 20 79 3d 31 30 0a 20 20 20 20 2a 2a 20 20 20  D y=10.    **   
2fb90 20 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44           x=5 AND
2fb91 20 79 3c 31 30 0a 20 20 20 20 2a 2a 20 20 20 20   y<10.    **    
2fb92 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20          x=5 AND 
2fb93 79 3e 35 20 41 4e 44 20 79 3c 31 30 0a 20 20 20  y>5 AND y<10.   
2fb94 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 78   **            x
2fb95 3d 35 20 41 4e 44 20 79 3d 35 20 41 4e 44 20 7a  =5 AND y=5 AND z
2fb96 3c 3d 31 30 0a 20 20 20 20 2a 2a 0a 20 20 20 20  <=10.    **.    
2fb97 2a 2a 20 20 20 20 20 20 20 20 20 54 68 65 20 7a  **         The z
2fb98 3c 31 30 20 74 65 72 6d 20 6f 66 20 74 68 65 20  <10 term of the 
2fb99 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6e 6e 6f 74  following cannot
2fb9a 20 62 65 20 75 73 65 64 2c 20 6f 6e 6c 79 0a 20   be used, only. 
2fb9b 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 74 68     **         th
2fb9c 65 20 78 3d 35 20 74 65 72 6d 3a 0a 20 20 20 20  e x=5 term:.    
2fb9d 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  **.    **       
2fb9e 20 20 20 20 20 78 3d 35 20 41 4e 44 20 7a 3c 31       x=5 AND z<1
2fb9f 30 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  0.    **.    ** 
2fba0 20 20 20 20 20 20 20 20 4e 20 6d 61 79 20 62 65          N may be
2fba1 20 7a 65 72 6f 20 69 66 20 74 68 65 72 65 20 61   zero if there a
2fba2 72 65 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f  re inequality co
2fba3 6e 73 74 72 61 69 6e 74 73 2e 0a 20 20 20 20 2a  nstraints..    *
2fba4 2a 20 20 20 20 20 20 20 20 20 49 66 20 74 68 65  *         If the
2fba5 72 65 20 61 72 65 20 6e 6f 20 69 6e 65 71 75 61  re are no inequa
2fba6 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73  lity constraints
2fba7 2c 20 74 68 65 6e 20 4e 20 69 73 20 61 74 0a 20  , then N is at. 
2fba8 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 6c 65     **         le
2fba9 61 73 74 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a 0a  ast one..    **.
2fbaa 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 54      **         T
2fbab 68 69 73 20 63 61 73 65 20 69 73 20 61 6c 73 6f  his case is also
2fbac 20 75 73 65 64 20 77 68 65 6e 20 74 68 65 72 65   used when there
2fbad 20 61 72 65 20 6e 6f 20 57 48 45 52 45 20 63 6c   are no WHERE cl
2fbae 61 75 73 65 0a 20 20 20 20 2a 2a 20 20 20 20 20  ause.    **     
2fbaf 20 20 20 20 63 6f 6e 73 74 72 61 69 6e 74 73 20      constraints 
2fbb0 62 75 74 20 61 6e 20 69 6e 64 65 78 20 69 73 20  but an index is 
2fbb1 73 65 6c 65 63 74 65 64 20 61 6e 79 77 61 79 2c  selected anyway,
2fbb2 20 69 6e 20 6f 72 64 65 72 0a 20 20 20 20 2a 2a   in order.    **
2fbb3 20 20 20 20 20 20 20 20 20 74 6f 20 66 6f 72 63           to forc
2fbb4 65 20 74 68 65 20 6f 75 74 70 75 74 20 6f 72 64  e the output ord
2fbb5 65 72 20 74 6f 20 63 6f 6e 66 6f 72 6d 20 74 6f  er to conform to
2fbb6 20 61 6e 20 4f 52 44 45 52 20 42 59 2e 0a 20 20   an ORDER BY..  
2fbb7 20 20 2a 2f 20 20 0a 20 20 20 20 69 6e 74 20 61    */  .    int a
2fbb8 53 74 61 72 74 4f 70 5b 5d 20 3d 20 7b 0a 20 20  StartOp[] = {.  
2fbb9 20 20 20 20 30 2c 0a 20 20 20 20 20 20 30 2c 0a      0,.      0,.
2fbba 20 20 20 20 20 20 4f 50 5f 52 65 77 69 6e 64 2c        OP_Rewind,
2fbbb 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 3a             /* 2:
2fbbc 20 28 21 73 74 61 72 74 5f 63 6f 6e 73 74 72 61   (!start_constra
2fbbd 69 6e 74 73 20 26 26 20 73 74 61 72 74 45 71 20  ints && startEq 
2fbbe 26 26 20 20 21 62 52 65 76 29 20 2a 2f 0a 20 20  &&  !bRev) */.  
2fbbf 20 20 20 20 4f 50 5f 4c 61 73 74 2c 20 20 20 20      OP_Last,    
2fbc0 20 20 20 20 20 20 20 20 20 2f 2a 20 33 3a 20 28           /* 3: (
2fbc1 21 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e  !start_constrain
2fbc2 74 73 20 26 26 20 73 74 61 72 74 45 71 20 26 26  ts && startEq &&
2fbc3 20 20 20 62 52 65 76 29 20 2a 2f 0a 20 20 20 20     bRev) */.    
2fbc4 20 20 4f 50 5f 53 65 65 6b 47 74 2c 20 20 20 20    OP_SeekGt,    
2fbc5 20 20 20 20 20 20 20 2f 2a 20 34 3a 20 28 73 74         /* 4: (st
2fbc6 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20  art_constraints 
2fbc7 20 26 26 20 21 73 74 61 72 74 45 71 20 26 26 20   && !startEq && 
2fbc8 21 62 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20  !bRev) */.      
2fbc9 4f 50 5f 53 65 65 6b 4c 74 2c 20 20 20 20 20 20  OP_SeekLt,      
2fbca 20 20 20 20 20 2f 2a 20 35 3a 20 28 73 74 61 72       /* 5: (star
2fbcb 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 26  t_constraints  &
2fbcc 26 20 21 73 74 61 72 74 45 71 20 26 26 20 20 62  & !startEq &&  b
2fbcd 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50  Rev) */.      OP
2fbce 5f 53 65 65 6b 47 65 2c 20 20 20 20 20 20 20 20  _SeekGe,        
2fbcf 20 20 20 2f 2a 20 36 3a 20 28 73 74 61 72 74 5f     /* 6: (start_
2fbd0 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20  constraints  && 
2fbd1 20 73 74 61 72 74 45 71 20 26 26 20 21 62 52 65   startEq && !bRe
2fbd2 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53  v) */.      OP_S
2fbd3 65 65 6b 4c 65 20 20 20 20 20 20 20 20 20 20 20  eekLe           
2fbd4 20 2f 2a 20 37 3a 20 28 73 74 61 72 74 5f 63 6f   /* 7: (start_co
2fbd5 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20 20 73  nstraints  &&  s
2fbd6 74 61 72 74 45 71 20 26 26 20 20 62 52 65 76 29  tartEq &&  bRev)
2fbd7 20 2a 2f 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69   */.    };.    i
2fbd8 6e 74 20 61 45 6e 64 4f 70 5b 5d 20 3d 20 7b 0a  nt aEndOp[] = {.
2fbd9 20 20 20 20 20 20 4f 50 5f 4e 6f 6f 70 2c 20 20        OP_Noop,  
2fbda 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30 3a             /* 0:
2fbdb 20 28 21 65 6e 64 5f 63 6f 6e 73 74 72 61 69 6e   (!end_constrain
2fbdc 74 73 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f  ts) */.      OP_
2fbdd 49 64 78 47 45 2c 20 20 20 20 20 20 20 20 20 20  IdxGE,          
2fbde 20 20 2f 2a 20 31 3a 20 28 65 6e 64 5f 63 6f 6e    /* 1: (end_con
2fbdf 73 74 72 61 69 6e 74 73 20 26 26 20 21 62 52 65  straints && !bRe
2fbe0 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 49  v) */.      OP_I
2fbe1 64 78 4c 54 20 20 20 20 20 20 20 20 20 20 20 20  dxLT            
2fbe2 20 2f 2a 20 32 3a 20 28 65 6e 64 5f 63 6f 6e 73   /* 2: (end_cons
2fbe3 74 72 61 69 6e 74 73 20 26 26 20 62 52 65 76 29  traints && bRev)
2fbe4 20 2a 2f 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69   */.    };.    i
2fbe5 6e 74 20 6e 45 71 20 3d 20 70 4c 65 76 65 6c 2d  nt nEq = pLevel-
2fbe6 3e 70 6c 61 6e 2e 6e 45 71 3b 0a 20 20 20 20 69  >plan.nEq;.    i
2fbe7 6e 74 20 69 73 4d 69 6e 51 75 65 72 79 20 3d 20  nt isMinQuery = 
2fbe8 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  0;          /* I
2fbe9 66 20 74 68 69 73 20 69 73 20 61 6e 20 6f 70 74  f this is an opt
2fbea 69 6d 69 7a 65 64 20 53 45 4c 45 43 54 20 6d 69  imized SELECT mi
2fbeb 6e 28 78 29 2e 2e 20 2a 2f 0a 20 20 20 20 69 6e  n(x).. */.    in
2fbec 74 20 72 65 67 42 61 73 65 3b 20 20 20 20 20 20  t regBase;      
2fbed 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61             /* Ba
2fbee 73 65 20 72 65 67 69 73 74 65 72 20 68 6f 6c 64  se register hold
2fbef 69 6e 67 20 63 6f 6e 73 74 72 61 69 6e 74 20 76  ing constraint v
2fbf0 61 6c 75 65 73 20 2a 2f 0a 20 20 20 20 69 6e 74  alues */.    int
2fbf1 20 72 31 3b 20 20 20 20 20 20 20 20 20 20 20 20   r1;            
2fbf2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d            /* Tem
2fbf3 70 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20  p register */.  
2fbf4 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 52 61    WhereTerm *pRa
2fbf5 6e 67 65 53 74 61 72 74 20 3d 20 30 3b 20 20 2f  ngeStart = 0;  /
2fbf6 2a 20 49 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e  * Inequality con
2fbf7 73 74 72 61 69 6e 74 20 61 74 20 72 61 6e 67 65  straint at range
2fbf8 20 73 74 61 72 74 20 2a 2f 0a 20 20 20 20 57 68   start */.    Wh
2fbf9 65 72 65 54 65 72 6d 20 2a 70 52 61 6e 67 65 45  ereTerm *pRangeE
2fbfa 6e 64 20 3d 20 30 3b 20 20 20 20 2f 2a 20 49 6e  nd = 0;    /* In
2fbfb 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
2fbfc 69 6e 74 20 61 74 20 72 61 6e 67 65 20 65 6e 64  int at range end
2fbfd 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 74 61 72   */.    int star
2fbfe 74 45 71 3b 20 20 20 20 20 20 20 20 20 20 20 20  tEq;            
2fbff 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
2fc00 72 61 6e 67 65 20 73 74 61 72 74 20 75 73 65 73  range start uses
2fc01 20 3d 3d 2c 20 3e 3d 20 6f 72 20 3c 3d 20 2a 2f   ==, >= or <= */
2fc02 0a 20 20 20 20 69 6e 74 20 65 6e 64 45 71 3b 20  .    int endEq; 
2fc03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fc04 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72 61 6e    /* True if ran
2fc05 67 65 20 65 6e 64 20 75 73 65 73 20 3d 3d 2c 20  ge end uses ==, 
2fc06 3e 3d 20 6f 72 20 3c 3d 20 2a 2f 0a 20 20 20 20  >= or <= */.    
2fc07 69 6e 74 20 73 74 61 72 74 5f 63 6f 6e 73 74 72  int start_constr
2fc08 61 69 6e 74 73 3b 20 20 20 20 20 20 20 2f 2a 20  aints;       /* 
2fc09 53 74 61 72 74 20 6f 66 20 72 61 6e 67 65 20 69  Start of range i
2fc0a 73 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 2a 2f  s constrained */
2fc0b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6e 73 74 72  .    int nConstr
2fc0c 61 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  aint;           
2fc0d 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
2fc0e 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20  onstraint terms 
2fc0f 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  */.    Index *pI
2fc10 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  dx;         /* T
2fc11 68 65 20 69 6e 64 65 78 20 77 65 20 77 69 6c 6c  he index we will
2fc12 20 62 65 20 75 73 69 6e 67 20 2a 2f 0a 20 20 20   be using */.   
2fc13 20 69 6e 74 20 69 49 64 78 43 75 72 3b 20 20 20   int iIdxCur;   
2fc14 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42        /* The VDB
2fc15 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65  E cursor for the
2fc16 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 6e   index */.    in
2fc17 74 20 6e 45 78 74 72 61 52 65 67 20 3d 20 30 3b  t nExtraReg = 0;
2fc18 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2fc19 65 78 74 72 61 20 72 65 67 69 73 74 65 72 73 20  extra registers 
2fc1a 6e 65 65 64 65 64 20 2a 2f 0a 20 20 20 20 69 6e  needed */.    in
2fc1b 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  t op;           
2fc1c 20 20 20 2f 2a 20 49 6e 73 74 72 75 63 74 69 6f     /* Instructio
2fc1d 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20  n opcode */.    
2fc1e 63 68 61 72 20 2a 7a 41 66 66 3b 0a 0a 20 20 20  char *zAff;..   
2fc1f 20 70 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d 3e   pIdx = pLevel->
2fc20 70 6c 61 6e 2e 75 2e 70 49 64 78 3b 0a 20 20 20  plan.u.pIdx;.   
2fc21 20 69 49 64 78 43 75 72 20 3d 20 70 4c 65 76 65   iIdxCur = pLeve
2fc22 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20  l->iIdxCur;.    
2fc23 6b 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75  k = pIdx->aiColu
2fc24 6d 6e 5b 6e 45 71 5d 3b 20 20 20 20 20 2f 2a 20  mn[nEq];     /* 
2fc25 43 6f 6c 75 6d 6e 20 66 6f 72 20 69 6e 65 71 75  Column for inequ
2fc26 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
2fc27 73 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20  s */..    /* If 
2fc28 74 68 69 73 20 6c 6f 6f 70 20 73 61 74 69 73 66  this loop satisf
2fc29 69 65 73 20 61 20 73 6f 72 74 20 6f 72 64 65 72  ies a sort order
2fc2a 20 28 70 4f 72 64 65 72 42 79 29 20 72 65 71 75   (pOrderBy) requ
2fc2b 65 73 74 20 74 68 61 74 20 0a 20 20 20 20 2a 2a  est that .    **
2fc2c 20 77 61 73 20 70 61 73 73 65 64 20 74 6f 20 74   was passed to t
2fc2d 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  his function to 
2fc2e 69 6d 70 6c 65 6d 65 6e 74 20 61 20 22 53 45 4c  implement a "SEL
2fc2f 45 43 54 20 6d 69 6e 28 78 29 20 2e 2e 2e 22 20  ECT min(x) ..." 
2fc30 0a 20 20 20 20 2a 2a 20 71 75 65 72 79 2c 20 74  .    ** query, t
2fc31 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 77  hen the caller w
2fc32 69 6c 6c 20 6f 6e 6c 79 20 61 6c 6c 6f 77 20 74  ill only allow t
2fc33 68 65 20 6c 6f 6f 70 20 74 6f 20 72 75 6e 20 66  he loop to run f
2fc34 6f 72 0a 20 20 20 20 2a 2a 20 61 20 73 69 6e 67  or.    ** a sing
2fc35 6c 65 20 69 74 65 72 61 74 69 6f 6e 2e 20 54 68  le iteration. Th
2fc36 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  is means that th
2fc37 65 20 66 69 72 73 74 20 72 6f 77 20 72 65 74 75  e first row retu
2fc38 72 6e 65 64 0a 20 20 20 20 2a 2a 20 73 68 6f 75  rned.    ** shou
2fc39 6c 64 20 6e 6f 74 20 68 61 76 65 20 61 20 4e 55  ld not have a NU
2fc3a 4c 4c 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20  LL value stored 
2fc3b 69 6e 20 27 78 27 2e 20 49 66 20 63 6f 6c 75 6d  in 'x'. If colum
2fc3c 6e 20 27 78 27 20 69 73 0a 20 20 20 20 2a 2a 20  n 'x' is.    ** 
2fc3d 74 68 65 20 66 69 72 73 74 20 6f 6e 65 20 61 66  the first one af
2fc3e 74 65 72 20 74 68 65 20 6e 45 71 20 65 71 75 61  ter the nEq equa
2fc3f 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73  lity constraints
2fc40 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2c 0a 20   in the index,. 
2fc41 20 20 20 2a 2a 20 74 68 69 73 20 72 65 71 75 69     ** this requi
2fc42 72 65 73 20 73 6f 6d 65 20 73 70 65 63 69 61 6c  res some special
2fc43 20 68 61 6e 64 6c 69 6e 67 2e 0a 20 20 20 20 2a   handling..    *
2fc44 2f 0a 20 20 20 20 69 66 28 20 28 77 63 74 72 6c  /.    if( (wctrl
2fc45 46 6c 61 67 73 26 57 48 45 52 45 5f 4f 52 44 45  Flags&WHERE_ORDE
2fc46 52 42 59 5f 4d 49 4e 29 21 3d 30 0a 20 20 20 20  RBY_MIN)!=0.    
2fc47 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61   && (pLevel->pla
2fc48 6e 2e 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f  n.wsFlags&WHERE_
2fc49 4f 52 44 45 52 42 59 29 0a 20 20 20 20 20 26 26  ORDERBY).     &&
2fc4a 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e   (pIdx->nColumn>
2fc4b 6e 45 71 29 0a 20 20 20 20 29 7b 0a 20 20 20 20  nEq).    ){.    
2fc4c 20 20 2f 2a 20 61 73 73 65 72 74 28 20 70 4f 72    /* assert( pOr
2fc4d 64 65 72 42 79 2d 3e 6e 45 78 70 72 3d 3d 31 20  derBy->nExpr==1 
2fc4e 29 3b 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 61  ); */.      /* a
2fc4f 73 73 65 72 74 28 20 70 4f 72 64 65 72 42 79 2d  ssert( pOrderBy-
2fc50 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 69 43 6f  >a[0].pExpr->iCo
2fc51 6c 75 6d 6e 3d 3d 70 49 64 78 2d 3e 61 69 43 6f  lumn==pIdx->aiCo
2fc52 6c 75 6d 6e 5b 6e 45 71 5d 20 29 3b 20 2a 2f 0a  lumn[nEq] ); */.
2fc53 20 20 20 20 20 20 69 73 4d 69 6e 51 75 65 72 79        isMinQuery
2fc54 20 3d 20 31 3b 0a 20 20 20 20 20 20 6e 45 78 74   = 1;.      nExt
2fc55 72 61 52 65 67 20 3d 20 31 3b 0a 20 20 20 20 7d  raReg = 1;.    }
2fc56 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61 6e  ..    /* Find an
2fc57 79 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e  y inequality con
2fc58 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20 66 6f  straint terms fo
2fc59 72 20 74 68 65 20 73 74 61 72 74 20 61 6e 64 20  r the start and 
2fc5a 65 6e 64 20 0a 20 20 20 20 2a 2a 20 6f 66 20 74  end .    ** of t
2fc5b 68 65 20 72 61 6e 67 65 2e 20 0a 20 20 20 20 2a  he range. .    *
2fc5c 2f 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c  /.    if( pLevel
2fc5d 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
2fc5e 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54   WHERE_TOP_LIMIT
2fc5f 20 29 7b 0a 20 20 20 20 20 20 70 52 61 6e 67 65   ){.      pRange
2fc60 45 6e 64 20 3d 20 66 69 6e 64 54 65 72 6d 28 70  End = findTerm(p
2fc61 57 43 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f 74  WC, iCur, k, not
2fc62 52 65 61 64 79 2c 20 28 57 4f 5f 4c 54 7c 57 4f  Ready, (WO_LT|WO
2fc63 5f 4c 45 29 2c 20 70 49 64 78 29 3b 0a 20 20 20  _LE), pIdx);.   
2fc64 20 20 20 6e 45 78 74 72 61 52 65 67 20 3d 20 31     nExtraReg = 1
2fc65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
2fc66 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
2fc67 6c 61 67 73 20 26 20 57 48 45 52 45 5f 42 54 4d  lags & WHERE_BTM
2fc68 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20 20 20  _LIMIT ){.      
2fc69 70 52 61 6e 67 65 53 74 61 72 74 20 3d 20 66 69  pRangeStart = fi
2fc6a 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72  ndTerm(pWC, iCur
2fc6b 2c 20 6b 2c 20 6e 6f 74 52 65 61 64 79 2c 20 28  , k, notReady, (
2fc6c 57 4f 5f 47 54 7c 57 4f 5f 47 45 29 2c 20 70 49  WO_GT|WO_GE), pI
2fc6d 64 78 29 3b 0a 20 20 20 20 20 20 6e 45 78 74 72  dx);.      nExtr
2fc6e 61 52 65 67 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  aReg = 1;.    }.
2fc6f 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  .    /* Generate
2fc70 20 63 6f 64 65 20 74 6f 20 65 76 61 6c 75 61 74   code to evaluat
2fc71 65 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74  e all constraint
2fc72 20 74 65 72 6d 73 20 75 73 69 6e 67 20 3d 3d 20   terms using == 
2fc73 6f 72 20 49 4e 0a 20 20 20 20 2a 2a 20 61 6e 64  or IN.    ** and
2fc74 20 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65   store the value
2fc75 73 20 6f 66 20 74 68 6f 73 65 20 74 65 72 6d 73  s of those terms
2fc76 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20   in an array of 
2fc77 72 65 67 69 73 74 65 72 73 0a 20 20 20 20 2a 2a  registers.    **
2fc78 20 73 74 61 72 74 69 6e 67 20 61 74 20 72 65 67   starting at reg
2fc79 42 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  Base..    */.   
2fc7a 20 72 65 67 42 61 73 65 20 3d 20 63 6f 64 65 41   regBase = codeA
2fc7b 6c 6c 45 71 75 61 6c 69 74 79 54 65 72 6d 73 28  llEqualityTerms(
2fc7c 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2c  .        pParse,
2fc7d 20 70 4c 65 76 65 6c 2c 20 70 57 43 2c 20 6e 6f   pLevel, pWC, no
2fc7e 74 52 65 61 64 79 2c 20 6e 45 78 74 72 61 52 65  tReady, nExtraRe
2fc7f 67 2c 20 26 7a 41 66 66 0a 20 20 20 20 29 3b 0a  g, &zAff.    );.
2fc80 20 20 20 20 61 64 64 72 4e 78 74 20 3d 20 70 4c      addrNxt = pL
2fc81 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74 3b 0a 0a  evel->addrNxt;..
2fc82 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65      /* If we are
2fc83 20 64 6f 69 6e 67 20 61 20 72 65 76 65 72 73 65   doing a reverse
2fc84 20 6f 72 64 65 72 20 73 63 61 6e 20 6f 6e 20 61   order scan on a
2fc85 6e 20 61 73 63 65 6e 64 69 6e 67 20 69 6e 64 65  n ascending inde
2fc86 78 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 61 20 66  x, or.    ** a f
2fc87 6f 72 77 61 72 64 20 6f 72 64 65 72 20 73 63 61  orward order sca
2fc88 6e 20 6f 6e 20 61 20 64 65 73 63 65 6e 64 69 6e  n on a descendin
2fc89 67 20 69 6e 64 65 78 2c 20 69 6e 74 65 72 63 68  g index, interch
2fc8a 61 6e 67 65 20 74 68 65 20 0a 20 20 20 20 2a 2a  ange the .    **
2fc8b 20 73 74 61 72 74 20 61 6e 64 20 65 6e 64 20 74   start and end t
2fc8c 65 72 6d 73 20 28 70 52 61 6e 67 65 53 74 61 72  erms (pRangeStar
2fc8d 74 20 61 6e 64 20 70 52 61 6e 67 65 45 6e 64 29  t and pRangeEnd)
2fc8e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
2fc8f 20 62 52 65 76 3d 3d 28 70 49 64 78 2d 3e 61 53   bRev==(pIdx->aS
2fc90 6f 72 74 4f 72 64 65 72 5b 6e 45 71 5d 3d 3d 53  ortOrder[nEq]==S
2fc91 51 4c 49 54 45 5f 53 4f 5f 41 53 43 29 20 29 7b  QLITE_SO_ASC) ){
2fc92 0a 20 20 20 20 20 20 53 57 41 50 28 57 68 65 72  .      SWAP(Wher
2fc93 65 54 65 72 6d 20 2a 2c 20 70 52 61 6e 67 65 45  eTerm *, pRangeE
2fc94 6e 64 2c 20 70 52 61 6e 67 65 53 74 61 72 74 29  nd, pRangeStart)
2fc95 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 74 65 73  ;.    }..    tes
2fc96 74 63 61 73 65 28 20 70 52 61 6e 67 65 53 74 61  tcase( pRangeSta
2fc97 72 74 20 26 26 20 70 52 61 6e 67 65 53 74 61 72  rt && pRangeStar
2fc98 74 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57  t->eOperator & W
2fc99 4f 5f 4c 45 20 29 3b 0a 20 20 20 20 74 65 73 74  O_LE );.    test
2fc9a 63 61 73 65 28 20 70 52 61 6e 67 65 53 74 61 72  case( pRangeStar
2fc9b 74 20 26 26 20 70 52 61 6e 67 65 53 74 61 72 74  t && pRangeStart
2fc9c 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f  ->eOperator & WO
2fc9d 5f 47 45 20 29 3b 0a 20 20 20 20 74 65 73 74 63  _GE );.    testc
2fc9e 61 73 65 28 20 70 52 61 6e 67 65 45 6e 64 20 26  ase( pRangeEnd &
2fc9f 26 20 70 52 61 6e 67 65 45 6e 64 2d 3e 65 4f 70  & pRangeEnd->eOp
2fca0 65 72 61 74 6f 72 20 26 20 57 4f 5f 4c 45 20 29  erator & WO_LE )
2fca1 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
2fca2 70 52 61 6e 67 65 45 6e 64 20 26 26 20 70 52 61  pRangeEnd && pRa
2fca3 6e 67 65 45 6e 64 2d 3e 65 4f 70 65 72 61 74 6f  ngeEnd->eOperato
2fca4 72 20 26 20 57 4f 5f 47 45 20 29 3b 0a 20 20 20  r & WO_GE );.   
2fca5 20 73 74 61 72 74 45 71 20 3d 20 21 70 52 61 6e   startEq = !pRan
2fca6 67 65 53 74 61 72 74 20 7c 7c 20 70 52 61 6e 67  geStart || pRang
2fca7 65 53 74 61 72 74 2d 3e 65 4f 70 65 72 61 74 6f  eStart->eOperato
2fca8 72 20 26 20 28 57 4f 5f 4c 45 7c 57 4f 5f 47 45  r & (WO_LE|WO_GE
2fca9 29 3b 0a 20 20 20 20 65 6e 64 45 71 20 3d 20 20  );.    endEq =  
2fcaa 20 21 70 52 61 6e 67 65 45 6e 64 20 7c 7c 20 70   !pRangeEnd || p
2fcab 52 61 6e 67 65 45 6e 64 2d 3e 65 4f 70 65 72 61  RangeEnd->eOpera
2fcac 74 6f 72 20 26 20 28 57 4f 5f 4c 45 7c 57 4f 5f  tor & (WO_LE|WO_
2fcad 47 45 29 3b 0a 20 20 20 20 73 74 61 72 74 5f 63  GE);.    start_c
2fcae 6f 6e 73 74 72 61 69 6e 74 73 20 3d 20 70 52 61  onstraints = pRa
2fcaf 6e 67 65 53 74 61 72 74 20 7c 7c 20 6e 45 71 3e  ngeStart || nEq>
2fcb0 30 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 65 6b 20  0;..    /* Seek 
2fcb1 74 68 65 20 69 6e 64 65 78 20 63 75 72 73 6f 72  the index cursor
2fcb2 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66   to the start of
2fcb3 20 74 68 65 20 72 61 6e 67 65 2e 20 2a 2f 0a 20   the range. */. 
2fcb4 20 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d     nConstraint =
2fcb5 20 6e 45 71 3b 0a 20 20 20 20 69 66 28 20 70 52   nEq;.    if( pR
2fcb6 61 6e 67 65 53 74 61 72 74 20 29 7b 0a 20 20 20  angeStart ){.   
2fcb7 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 20     Expr *pRight 
2fcb8 3d 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 70  = pRangeStart->p
2fcb9 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20  Expr->pRight;.  
2fcba 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2fcbb 6f 64 65 28 70 50 61 72 73 65 2c 20 70 52 69 67  ode(pParse, pRig
2fcbc 68 74 2c 20 72 65 67 42 61 73 65 2b 6e 45 71 29  ht, regBase+nEq)
2fcbd 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2fcbe 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2fcbf 49 73 4e 75 6c 6c 2c 20 72 65 67 42 61 73 65 2b  IsNull, regBase+
2fcc0 6e 45 71 2c 20 61 64 64 72 4e 78 74 29 3b 0a 20  nEq, addrNxt);. 
2fcc1 20 20 20 20 20 69 66 28 20 7a 41 66 66 20 0a 20       if( zAff . 
2fcc2 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33        && sqlite3
2fcc3 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28  CompareAffinity(
2fcc4 70 52 69 67 68 74 2c 20 7a 41 66 66 5b 6e 43 6f  pRight, zAff[nCo
2fcc5 6e 73 74 72 61 69 6e 74 5d 29 3d 3d 53 51 4c 49  nstraint])==SQLI
2fcc6 54 45 5f 41 46 46 5f 4e 4f 4e 45 0a 20 20 20 20  TE_AFF_NONE.    
2fcc7 20 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20    ){.        /* 
2fcc8 53 69 6e 63 65 20 74 68 65 20 63 6f 6d 70 61 72  Since the compar
2fcc9 69 73 6f 6e 20 69 73 20 74 6f 20 62 65 20 70 65  ison is to be pe
2fcca 72 66 6f 72 6d 65 64 20 77 69 74 68 20 6e 6f 20  rformed with no 
2fccb 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 70 70 6c  conversions appl
2fccc 69 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  ied.        ** t
2fccd 6f 20 74 68 65 20 6f 70 65 72 61 6e 64 73 2c 20  o the operands, 
2fcce 73 65 74 20 74 68 65 20 61 66 66 69 6e 69 74 79  set the affinity
2fccf 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 70 52 69   to apply to pRi
2fcd0 67 68 74 20 74 6f 20 0a 20 20 20 20 20 20 20 20  ght to .        
2fcd1 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  ** SQLITE_AFF_NO
2fcd2 4e 45 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20  NE.  */.        
2fcd3 7a 41 66 66 5b 6e 43 6f 6e 73 74 72 61 69 6e 74  zAff[nConstraint
2fcd4 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ] = SQLITE_AFF_N
2fcd5 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ONE;.      }.   
2fcd6 20 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 2b     nConstraint++
2fcd7 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
2fcd8 69 73 4d 69 6e 51 75 65 72 79 20 29 7b 0a 20 20  isMinQuery ){.  
2fcd9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2fcda 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
2fcdb 2c 20 30 2c 20 72 65 67 42 61 73 65 2b 6e 45 71  , 0, regBase+nEq
2fcdc 29 3b 0a 20 20 20 20 20 20 6e 43 6f 6e 73 74 72  );.      nConstr
2fcdd 61 69 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 73 74  aint++;.      st
2fcde 61 72 74 45 71 20 3d 20 30 3b 0a 20 20 20 20 20  artEq = 0;.     
2fcdf 20 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e   start_constrain
2fce0 74 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  ts = 1;.    }.  
2fce1 20 20 63 6f 64 65 41 70 70 6c 79 41 66 66 69 6e    codeApplyAffin
2fce2 69 74 79 28 70 50 61 72 73 65 2c 20 72 65 67 42  ity(pParse, regB
2fce3 61 73 65 2c 20 6e 43 6f 6e 73 74 72 61 69 6e 74  ase, nConstraint
2fce4 2c 20 7a 41 66 66 29 3b 0a 20 20 20 20 6f 70 20  , zAff);.    op 
2fce5 3d 20 61 53 74 61 72 74 4f 70 5b 28 73 74 61 72  = aStartOp[(star
2fce6 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 3c 3c 32  t_constraints<<2
2fce7 29 20 2b 20 28 73 74 61 72 74 45 71 3c 3c 31 29  ) + (startEq<<1)
2fce8 20 2b 20 62 52 65 76 5d 3b 0a 20 20 20 20 61 73   + bRev];.    as
2fce9 73 65 72 74 28 20 6f 70 21 3d 30 20 29 3b 0a 20  sert( op!=0 );. 
2fcea 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
2fceb 3d 4f 50 5f 52 65 77 69 6e 64 20 29 3b 0a 20 20  =OP_Rewind );.  
2fcec 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
2fced 4f 50 5f 4c 61 73 74 20 29 3b 0a 20 20 20 20 74  OP_Last );.    t
2fcee 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f  estcase( op==OP_
2fcef 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20 74 65  SeekGt );.    te
2fcf0 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 53  stcase( op==OP_S
2fcf1 65 65 6b 47 65 20 29 3b 0a 20 20 20 20 74 65 73  eekGe );.    tes
2fcf2 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 53 65  tcase( op==OP_Se
2fcf3 65 6b 4c 65 20 29 3b 0a 20 20 20 20 74 65 73 74  ekLe );.    test
2fcf4 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 53 65 65  case( op==OP_See
2fcf5 6b 4c 74 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  kLt );.    sqlit
2fcf6 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2fcf7 6f 70 2c 20 69 49 64 78 43 75 72 2c 20 61 64 64  op, iIdxCur, add
2fcf8 72 4e 78 74 2c 20 72 65 67 42 61 73 65 2c 20 0a  rNxt, regBase, .
2fcf9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fcfa 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4e 54        SQLITE_INT
2fcfb 5f 54 4f 5f 50 54 52 28 6e 43 6f 6e 73 74 72 61  _TO_PTR(nConstra
2fcfc 69 6e 74 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b  int), P4_INT32);
2fcfd 0a 0a 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68  ..    /* Load th
2fcfe 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  e value for the 
2fcff 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  inequality const
2fd00 72 61 69 6e 74 20 61 74 20 74 68 65 20 65 6e 64  raint at the end
2fd01 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 72   of the.    ** r
2fd02 61 6e 67 65 20 28 69 66 20 61 6e 79 29 2e 0a 20  ange (if any).. 
2fd03 20 20 20 2a 2f 0a 20 20 20 20 6e 43 6f 6e 73 74     */.    nConst
2fd04 72 61 69 6e 74 20 3d 20 6e 45 71 3b 0a 20 20 20  raint = nEq;.   
2fd05 20 69 66 28 20 70 52 61 6e 67 65 45 6e 64 20 29   if( pRangeEnd )
2fd06 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 52  {.      Expr *pR
2fd07 69 67 68 74 20 3d 20 70 52 61 6e 67 65 45 6e 64  ight = pRangeEnd
2fd08 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b  ->pExpr->pRight;
2fd09 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2fd0a 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 70 50  prCacheRemove(pP
2fd0b 61 72 73 65 2c 20 72 65 67 42 61 73 65 2b 6e 45  arse, regBase+nE
2fd0c 71 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  q);.      sqlite
2fd0d 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
2fd0e 2c 20 70 52 69 67 68 74 2c 20 72 65 67 42 61 73  , pRight, regBas
2fd0f 65 2b 6e 45 71 29 3b 0a 20 20 20 20 20 20 73 71  e+nEq);.      sq
2fd10 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2fd11 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65  v, OP_IsNull, re
2fd12 67 42 61 73 65 2b 6e 45 71 2c 20 61 64 64 72 4e  gBase+nEq, addrN
2fd13 78 74 29 3b 0a 20 20 20 20 20 20 7a 41 66 66 20  xt);.      zAff 
2fd14 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
2fd15 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 41  p(pParse->db, zA
2fd16 66 66 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a  ff);.      if( z
2fd17 41 66 66 20 0a 20 20 20 20 20 20 20 26 26 20 73  Aff .       && s
2fd18 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66  qlite3CompareAff
2fd19 69 6e 69 74 79 28 70 52 69 67 68 74 2c 20 7a 41  inity(pRight, zA
2fd1a 66 66 5b 6e 43 6f 6e 73 74 72 61 69 6e 74 5d 29  ff[nConstraint])
2fd1b 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  ==SQLITE_AFF_NON
2fd1c 45 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20  E.      ){.     
2fd1d 20 20 20 2f 2a 20 53 69 6e 63 65 20 74 68 65 20     /* Since the 
2fd1e 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 6f  comparison is to
2fd1f 20 62 65 20 70 65 72 66 6f 72 6d 65 64 20 77 69   be performed wi
2fd20 74 68 20 6e 6f 20 63 6f 6e 76 65 72 73 69 6f 6e  th no conversion
2fd21 73 20 61 70 70 6c 69 65 64 0a 20 20 20 20 20 20  s applied.      
2fd22 20 20 2a 2a 20 74 6f 20 74 68 65 20 6f 70 65 72    ** to the oper
2fd23 61 6e 64 73 2c 20 73 65 74 20 74 68 65 20 61 66  ands, set the af
2fd24 66 69 6e 69 74 79 20 74 6f 20 61 70 70 6c 79 20  finity to apply 
2fd25 74 6f 20 70 52 69 67 68 74 20 74 6f 20 0a 20 20  to pRight to .  
2fd26 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f        ** SQLITE_
2fd27 41 46 46 5f 4e 4f 4e 45 2e 20 20 2a 2f 0a 20 20  AFF_NONE.  */.  
2fd28 20 20 20 20 20 20 7a 41 66 66 5b 6e 43 6f 6e 73        zAff[nCons
2fd29 74 72 61 69 6e 74 5d 20 3d 20 53 51 4c 49 54 45  traint] = SQLITE
2fd2a 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20  _AFF_NONE;.     
2fd2b 20 7d 0a 20 20 20 20 20 20 63 6f 64 65 41 70 70   }.      codeApp
2fd2c 6c 79 41 66 66 69 6e 69 74 79 28 70 50 61 72 73  lyAffinity(pPars
2fd2d 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 45 71 2b  e, regBase, nEq+
2fd2e 31 2c 20 7a 41 66 66 29 3b 0a 20 20 20 20 20 20  1, zAff);.      
2fd2f 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b 0a 20  nConstraint++;. 
2fd30 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 6f 70     }..    /* Top
2fd31 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 62 6f 64   of the loop bod
2fd32 79 20 2a 2f 0a 20 20 20 20 70 4c 65 76 65 6c 2d  y */.    pLevel-
2fd33 3e 70 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62  >p2 = sqlite3Vdb
2fd34 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
2fd35 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 69  ..    /* Check i
2fd36 66 20 74 68 65 20 69 6e 64 65 78 20 63 75 72 73  f the index curs
2fd37 6f 72 20 69 73 20 70 61 73 74 20 74 68 65 20 65  or is past the e
2fd38 6e 64 20 6f 66 20 74 68 65 20 72 61 6e 67 65 2e  nd of the range.
2fd39 20 2a 2f 0a 20 20 20 20 6f 70 20 3d 20 61 45 6e   */.    op = aEn
2fd3a 64 4f 70 5b 28 70 52 61 6e 67 65 45 6e 64 20 7c  dOp[(pRangeEnd |
2fd3b 7c 20 6e 45 71 29 20 2a 20 28 31 20 2b 20 62 52  | nEq) * (1 + bR
2fd3c 65 76 29 5d 3b 0a 20 20 20 20 74 65 73 74 63 61  ev)];.    testca
2fd3d 73 65 28 20 6f 70 3d 3d 4f 50 5f 4e 6f 6f 70 20  se( op==OP_Noop 
2fd3e 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
2fd3f 20 6f 70 3d 3d 4f 50 5f 49 64 78 47 45 20 29 3b   op==OP_IdxGE );
2fd40 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f  .    testcase( o
2fd41 70 3d 3d 4f 50 5f 49 64 78 4c 54 20 29 3b 0a 20  p==OP_IdxLT );. 
2fd42 20 20 20 69 66 28 20 6f 70 21 3d 4f 50 5f 4e 6f     if( op!=OP_No
2fd43 6f 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  op ){.      sqli
2fd44 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2fd45 20 6f 70 2c 20 69 49 64 78 43 75 72 2c 20 61 64   op, iIdxCur, ad
2fd46 64 72 4e 78 74 2c 20 72 65 67 42 61 73 65 2c 0a  drNxt, regBase,.
2fd47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fd48 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49          SQLITE_I
2fd49 4e 54 5f 54 4f 5f 50 54 52 28 6e 43 6f 6e 73 74  NT_TO_PTR(nConst
2fd4a 72 61 69 6e 74 29 2c 20 50 34 5f 49 4e 54 33 32  raint), P4_INT32
2fd4b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2fd4c 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
2fd4d 65 6e 64 45 71 21 3d 62 52 65 76 20 3f 31 3a 30  endEq!=bRev ?1:0
2fd4e 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
2fd4f 20 49 66 20 74 68 65 72 65 20 61 72 65 20 69 6e   If there are in
2fd50 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
2fd51 69 6e 74 73 2c 20 63 68 65 63 6b 20 74 68 61 74  ints, check that
2fd52 20 74 68 65 20 76 61 6c 75 65 0a 20 20 20 20 2a   the value.    *
2fd53 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63  * of the table c
2fd54 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65 20 69  olumn that the i
2fd55 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 74 72 61  nequality contra
2fd56 69 6e 73 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e  ins is not NULL.
2fd57 0a 20 20 20 20 2a 2a 20 49 66 20 69 74 20 69 73  .    ** If it is
2fd58 2c 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 6e 65  , jump to the ne
2fd59 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20  xt iteration of 
2fd5a 74 68 65 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f  the loop..    */
2fd5b 0a 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65  .    r1 = sqlite
2fd5c 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
2fd5d 73 65 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  se);.    testcas
2fd5e 65 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  e( pLevel->plan.
2fd5f 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
2fd60 42 54 4d 5f 4c 49 4d 49 54 20 29 3b 0a 20 20 20  BTM_LIMIT );.   
2fd61 20 74 65 73 74 63 61 73 65 28 20 70 4c 65 76 65   testcase( pLeve
2fd62 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
2fd63 26 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49  & WHERE_TOP_LIMI
2fd64 54 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65  T );.    if( pLe
2fd65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
2fd66 73 20 26 20 28 57 48 45 52 45 5f 42 54 4d 5f 4c  s & (WHERE_BTM_L
2fd67 49 4d 49 54 7c 57 48 45 52 45 5f 54 4f 50 5f 4c  IMIT|WHERE_TOP_L
2fd68 49 4d 49 54 29 20 29 7b 0a 20 20 20 20 20 20 73  IMIT) ){.      s
2fd69 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2fd6a 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69  (v, OP_Column, i
2fd6b 49 64 78 43 75 72 2c 20 6e 45 71 2c 20 72 31 29  IdxCur, nEq, r1)
2fd6c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2fd6d 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2fd6e 49 73 4e 75 6c 6c 2c 20 72 31 2c 20 61 64 64 72  IsNull, r1, addr
2fd6f 43 6f 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Cont);.    }.   
2fd70 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
2fd71 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
2fd72 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 65 6b  1);..    /* Seek
2fd73 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 73 6f   the table curso
2fd74 72 2c 20 69 66 20 72 65 71 75 69 72 65 64 20 2a  r, if required *
2fd75 2f 0a 20 20 20 20 64 69 73 61 62 6c 65 54 65 72  /.    disableTer
2fd76 6d 28 70 4c 65 76 65 6c 2c 20 70 52 61 6e 67 65  m(pLevel, pRange
2fd77 53 74 61 72 74 29 3b 0a 20 20 20 20 64 69 73 61  Start);.    disa
2fd78 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20  bleTerm(pLevel, 
2fd79 70 52 61 6e 67 65 45 6e 64 29 3b 0a 20 20 20 20  pRangeEnd);.    
2fd7a 69 66 28 20 21 6f 6d 69 74 54 61 62 6c 65 20 29  if( !omitTable )
2fd7b 7b 0a 20 20 20 20 20 20 69 52 6f 77 69 64 52 65  {.      iRowidRe
2fd7c 67 20 3d 20 69 52 65 6c 65 61 73 65 52 65 67 20  g = iReleaseReg 
2fd7d 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
2fd7e 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
2fd7f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2fd80 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 52 6f  dOp2(v, OP_IdxRo
2fd81 77 69 64 2c 20 69 49 64 78 43 75 72 2c 20 69 52  wid, iIdxCur, iR
2fd82 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20 20 20  owidReg);.      
2fd83 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
2fd84 53 74 6f 72 65 28 70 50 61 72 73 65 2c 20 69 43  Store(pParse, iC
2fd85 75 72 2c 20 2d 31 2c 20 69 52 6f 77 69 64 52 65  ur, -1, iRowidRe
2fd86 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  g);.      sqlite
2fd87 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2fd88 50 5f 53 65 65 6b 2c 20 69 43 75 72 2c 20 69 52  P_Seek, iCur, iR
2fd89 6f 77 69 64 52 65 67 29 3b 20 20 2f 2a 20 44 65  owidReg);  /* De
2fd8a 66 65 72 72 65 64 20 73 65 65 6b 20 2a 2f 0a 20  ferred seek */. 
2fd8b 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 63     }..    /* Rec
2fd8c 6f 72 64 20 74 68 65 20 69 6e 73 74 72 75 63 74  ord the instruct
2fd8d 69 6f 6e 20 75 73 65 64 20 74 6f 20 74 65 72 6d  ion used to term
2fd8e 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 2e 20  inate the loop. 
2fd8f 44 69 73 61 62 6c 65 20 0a 20 20 20 20 2a 2a 20  Disable .    ** 
2fd90 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72  WHERE clause ter
2fd91 6d 73 20 6d 61 64 65 20 72 65 64 75 6e 64 61 6e  ms made redundan
2fd92 74 20 62 79 20 74 68 65 20 69 6e 64 65 78 20 72  t by the index r
2fd93 61 6e 67 65 20 73 63 61 6e 2e 0a 20 20 20 20 2a  ange scan..    *
2fd94 2f 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70  /.    pLevel->op
2fd95 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f 50 72 65   = bRev ? OP_Pre
2fd96 76 20 3a 20 4f 50 5f 4e 65 78 74 3b 0a 20 20 20  v : OP_Next;.   
2fd97 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69 49   pLevel->p1 = iI
2fd98 64 78 43 75 72 3b 0a 20 20 7d 65 6c 73 65 0a 0a  dxCur;.  }else..
2fd99 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2fd9a 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54  MIT_OR_OPTIMIZAT
2fd9b 49 4f 4e 0a 20 20 69 66 28 20 70 4c 65 76 65 6c  ION.  if( pLevel
2fd9c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
2fd9d 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20   WHERE_MULTI_OR 
2fd9e 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65 20 34  ){.    /* Case 4
2fd9f 3a 20 20 54 77 6f 20 6f 72 20 6d 6f 72 65 20 73  :  Two or more s
2fda0 65 70 61 72 61 74 65 6c 79 20 69 6e 64 65 78 65  eparately indexe
2fda1 64 20 74 65 72 6d 73 20 63 6f 6e 6e 65 63 74 65  d terms connecte
2fda2 64 20 62 79 20 4f 52 0a 20 20 20 20 2a 2a 0a 20  d by OR.    **. 
2fda3 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 20     ** Example:. 
2fda4 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 43     **.    **   C
2fda5 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61  REATE TABLE t1(a
2fda6 2c 62 2c 63 2c 64 29 3b 0a 20 20 20 20 2a 2a 20  ,b,c,d);.    ** 
2fda7 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 69    CREATE INDEX i
2fda8 31 20 4f 4e 20 74 31 28 61 29 3b 0a 20 20 20 20  1 ON t1(a);.    
2fda9 2a 2a 20 20 20 43 52 45 41 54 45 20 49 4e 44 45  **   CREATE INDE
2fdaa 58 20 69 32 20 4f 4e 20 74 31 28 62 29 3b 0a 20  X i2 ON t1(b);. 
2fdab 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20 49     **   CREATE I
2fdac 4e 44 45 58 20 69 33 20 4f 4e 20 74 31 28 63 29  NDEX i3 ON t1(c)
2fdad 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ;.    **.    ** 
2fdae 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
2fdaf 74 31 20 57 48 45 52 45 20 61 3d 35 20 4f 52 20  t1 WHERE a=5 OR 
2fdb0 62 3d 37 20 4f 52 20 28 63 3d 31 31 20 41 4e 44  b=7 OR (c=11 AND
2fdb1 20 64 3d 31 33 29 0a 20 20 20 20 2a 2a 0a 20 20   d=13).    **.  
2fdb2 20 20 2a 2a 20 49 6e 20 74 68 65 20 65 78 61 6d    ** In the exam
2fdb3 70 6c 65 2c 20 74 68 65 72 65 20 61 72 65 20 74  ple, there are t
2fdb4 68 72 65 65 20 69 6e 64 65 78 65 64 20 74 65 72  hree indexed ter
2fdb5 6d 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20  ms connected by 
2fdb6 4f 52 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 74  OR..    ** The t
2fdb7 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 6c  op of the loop l
2fdb8 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a  ooks like this:.
2fdb9 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2fdba 20 20 20 20 20 20 20 4e 75 6c 6c 20 20 20 20 20         Null     
2fdbb 20 20 31 20 20 20 20 20 20 20 20 20 20 20 20 20    1             
2fdbc 20 20 20 23 20 5a 65 72 6f 20 74 68 65 20 72 6f     # Zero the ro
2fdbd 77 73 65 74 20 69 6e 20 72 65 67 20 31 0a 20 20  wset in reg 1.  
2fdbe 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 6e    **.    ** Then
2fdbf 2c 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65 78  , for each index
2fdc0 65 64 20 74 65 72 6d 2c 20 74 68 65 20 66 6f 6c  ed term, the fol
2fdc1 6c 6f 77 69 6e 67 2e 20 54 68 65 20 61 72 67 75  lowing. The argu
2fdc2 6d 65 6e 74 73 20 74 6f 0a 20 20 20 20 2a 2a 20  ments to.    ** 
2fdc3 52 6f 77 53 65 74 54 65 73 74 20 61 72 65 20 73  RowSetTest are s
2fdc4 75 63 68 20 74 68 61 74 20 74 68 65 20 72 6f 77  uch that the row
2fdc5 69 64 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  id of the curren
2fdc6 74 20 72 6f 77 20 69 73 20 69 6e 73 65 72 74 65  t row is inserte
2fdc7 64 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68  d.    ** into th
2fdc8 65 20 52 6f 77 53 65 74 2e 20 49 66 20 69 74 20  e RowSet. If it 
2fdc9 69 73 20 61 6c 72 65 61 64 79 20 70 72 65 73 65  is already prese
2fdca 6e 74 2c 20 63 6f 6e 74 72 6f 6c 20 73 6b 69 70  nt, control skip
2fdcb 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 47 6f 73  s the.    ** Gos
2fdcc 75 62 20 6f 70 63 6f 64 65 20 61 6e 64 20 6a 75  ub opcode and ju
2fdcd 6d 70 73 20 73 74 72 61 69 67 68 74 20 74 6f 20  mps straight to 
2fdce 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74  the code generat
2fdcf 65 64 20 62 79 20 57 68 65 72 65 45 6e 64 28 29  ed by WhereEnd()
2fdd0 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
2fdd1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 68         sqlite3Wh
2fdd2 65 72 65 42 65 67 69 6e 28 3c 74 65 72 6d 3e 29  ereBegin(<term>)
2fdd3 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2fdd4 20 52 6f 77 53 65 74 54 65 73 74 20 20 20 20 20   RowSetTest     
2fdd5 20 20 20 20 20 20 20 20 20 20 20 20 20 23 20 49               # I
2fdd6 6e 73 65 72 74 20 72 6f 77 69 64 20 69 6e 74 6f  nsert rowid into
2fdd7 20 72 6f 77 73 65 74 0a 20 20 20 20 2a 2a 20 20   rowset.    **  
2fdd8 20 20 20 20 20 20 20 20 47 6f 73 75 62 20 20 20          Gosub   
2fdd9 20 20 20 32 20 41 0a 20 20 20 20 2a 2a 20 20 20     2 A.    **   
2fdda 20 20 20 20 20 73 71 6c 69 74 65 33 57 68 65 72       sqlite3Wher
2fddb 65 45 6e 64 28 29 0a 20 20 20 20 2a 2a 0a 20 20  eEnd().    **.  
2fddc 20 20 2a 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20 74    ** Following t
2fddd 68 65 20 61 62 6f 76 65 2c 20 63 6f 64 65 20 74  he above, code t
2fdde 6f 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20  o terminate the 
2fddf 6c 6f 6f 70 2e 20 4c 61 62 65 6c 20 41 2c 20 74  loop. Label A, t
2fde0 68 65 20 74 61 72 67 65 74 0a 20 20 20 20 2a 2a  he target.    **
2fde1 20 6f 66 20 74 68 65 20 47 6f 73 75 62 20 61 62   of the Gosub ab
2fde2 6f 76 65 2c 20 6a 75 6d 70 73 20 74 6f 20 74 68  ove, jumps to th
2fde3 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 72 69  e instruction ri
2fde4 67 68 74 20 61 66 74 65 72 20 74 68 65 20 47 6f  ght after the Go
2fde5 74 6f 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  to..    **.    *
2fde6 2a 20 20 20 20 20 20 20 20 20 20 4e 75 6c 6c 20  *          Null 
2fde7 20 20 20 20 20 20 31 20 20 20 20 20 20 20 20 20        1         
2fde8 20 20 20 20 20 20 20 23 20 5a 65 72 6f 20 74 68         # Zero th
2fde9 65 20 72 6f 77 73 65 74 20 69 6e 20 72 65 67 20  e rowset in reg 
2fdea 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  1.    **        
2fdeb 20 20 47 6f 74 6f 20 20 20 20 20 20 20 42 20 20    Goto       B  
2fdec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 23 20                # 
2fded 54 68 65 20 6c 6f 6f 70 20 69 73 20 66 69 6e 69  The loop is fini
2fdee 73 68 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  shed..    **.   
2fdef 20 2a 2a 20 20 20 20 20 20 20 41 3a 20 3c 6c 6f   **       A: <lo
2fdf0 6f 70 20 62 6f 64 79 3e 20 20 20 20 20 20 20 20  op body>        
2fdf1 20 20 20 20 20 20 20 20 20 23 20 52 65 74 75 72           # Retur
2fdf2 6e 20 64 61 74 61 2c 20 77 68 61 74 65 76 65 72  n data, whatever
2fdf3 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
2fdf4 20 20 20 20 20 20 20 20 20 52 65 74 75 72 6e 20           Return 
2fdf5 20 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20      2           
2fdf6 20 20 20 20 20 23 20 4a 75 6d 70 20 62 61 63 6b       # Jump back
2fdf7 20 74 6f 20 74 68 65 20 47 6f 73 75 62 0a 20 20   to the Gosub.  
2fdf8 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
2fdf9 20 20 42 3a 20 3c 61 66 74 65 72 20 74 68 65 20    B: <after the 
2fdfa 6c 6f 6f 70 3e 0a 20 20 20 20 2a 2a 0a 20 20 20  loop>.    **.   
2fdfb 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 43 6c 61   */.    WhereCla
2fdfc 75 73 65 20 2a 70 4f 72 57 63 3b 20 20 20 20 2f  use *pOrWc;    /
2fdfd 2a 20 54 68 65 20 4f 52 2d 63 6c 61 75 73 65 20  * The OR-clause 
2fdfe 62 72 6f 6b 65 6e 20 6f 75 74 20 69 6e 74 6f 20  broken out into 
2fdff 73 75 62 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20  subterms */.    
2fe00 57 68 65 72 65 54 65 72 6d 20 2a 70 46 69 6e 61  WhereTerm *pFina
2fe01 6c 3b 20 20 20 20 20 2f 2a 20 46 69 6e 61 6c 20  l;     /* Final 
2fe02 73 75 62 74 65 72 6d 20 77 69 74 68 69 6e 20 74  subterm within t
2fe03 68 65 20 4f 52 2d 63 6c 61 75 73 65 2e 20 2a 2f  he OR-clause. */
2fe04 0a 20 20 20 20 53 72 63 4c 69 73 74 20 6f 6e 65  .    SrcList one
2fe05 54 61 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 53  Tab;        /* S
2fe06 68 6f 72 74 65 6e 65 64 20 74 61 62 6c 65 20 6c  hortened table l
2fe07 69 73 74 20 2a 2f 0a 0a 20 20 20 20 69 6e 74 20  ist */..    int 
2fe08 72 65 67 52 65 74 75 72 6e 20 3d 20 2b 2b 70 50  regReturn = ++pP
2fe09 61 72 73 65 2d 3e 6e 4d 65 6d 3b 20 20 20 20 20  arse->nMem;     
2fe0a 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
2fe0b 72 20 75 73 65 64 20 77 69 74 68 20 4f 50 5f 47  r used with OP_G
2fe0c 6f 73 75 62 20 2a 2f 0a 20 20 20 20 69 6e 74 20  osub */.    int 
2fe0d 72 65 67 52 6f 77 73 65 74 20 3d 20 30 3b 20 20  regRowset = 0;  
2fe0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe0f 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
2fe10 72 20 66 6f 72 20 52 6f 77 53 65 74 20 6f 62 6a  r for RowSet obj
2fe11 65 63 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72  ect */.    int r
2fe12 65 67 52 6f 77 69 64 20 3d 20 30 3b 20 20 20 20  egRowid = 0;    
2fe13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe14 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
2fe15 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64 20 2a   holding rowid *
2fe16 2f 0a 20 20 20 20 69 6e 74 20 69 4c 6f 6f 70 42  /.    int iLoopB
2fe17 6f 64 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ody = sqlite3Vdb
2fe18 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 20 20  eMakeLabel(v);  
2fe19 2f 2a 20 53 74 61 72 74 20 6f 66 20 6c 6f 6f 70  /* Start of loop
2fe1a 20 62 6f 64 79 20 2a 2f 0a 20 20 20 20 69 6e 74   body */.    int
2fe1b 20 69 52 65 74 49 6e 69 74 3b 20 20 20 20 20 20   iRetInit;      
2fe1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe1d 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
2fe1e 73 20 6f 66 20 72 65 67 52 65 74 75 72 6e 20 69  s of regReturn i
2fe1f 6e 69 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  nit */.    int i
2fe20 69 3b 0a 20 20 20 0a 20 20 20 20 70 54 65 72 6d  i;.   .    pTerm
2fe21 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e   = pLevel->plan.
2fe22 75 2e 70 54 65 72 6d 3b 0a 20 20 20 20 61 73 73  u.pTerm;.    ass
2fe23 65 72 74 28 20 70 54 65 72 6d 21 3d 30 20 29 3b  ert( pTerm!=0 );
2fe24 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65  .    assert( pTe
2fe25 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57  rm->eOperator==W
2fe26 4f 5f 4f 52 20 29 3b 0a 20 20 20 20 61 73 73 65  O_OR );.    asse
2fe27 72 74 28 20 28 70 54 65 72 6d 2d 3e 77 74 46 6c  rt( (pTerm->wtFl
2fe28 61 67 73 20 26 20 54 45 52 4d 5f 4f 52 49 4e 46  ags & TERM_ORINF
2fe29 4f 29 21 3d 30 20 29 3b 0a 20 20 20 20 70 4f 72  O)!=0 );.    pOr
2fe2a 57 63 20 3d 20 26 70 54 65 72 6d 2d 3e 75 2e 70  Wc = &pTerm->u.p
2fe2b 4f 72 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20 20  OrInfo->wc;.    
2fe2c 70 46 69 6e 61 6c 20 3d 20 26 70 4f 72 57 63 2d  pFinal = &pOrWc-
2fe2d 3e 61 5b 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d  >a[pOrWc->nTerm-
2fe2e 31 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20  1];..    /* Set 
2fe2f 75 70 20 61 20 53 72 63 4c 69 73 74 20 63 6f 6e  up a SrcList con
2fe30 74 61 69 6e 69 6e 67 20 6a 75 73 74 20 74 68 65  taining just the
2fe31 20 74 61 62 6c 65 20 62 65 69 6e 67 20 73 63 61   table being sca
2fe32 6e 6e 65 64 20 62 79 20 74 68 69 73 20 6c 6f 6f  nned by this loo
2fe33 70 2e 20 2a 2f 0a 20 20 20 20 6f 6e 65 54 61 62  p. */.    oneTab
2fe34 2e 6e 53 72 63 20 3d 20 31 3b 0a 20 20 20 20 6f  .nSrc = 1;.    o
2fe35 6e 65 54 61 62 2e 6e 41 6c 6c 6f 63 20 3d 20 31  neTab.nAlloc = 1
2fe36 3b 0a 20 20 20 20 6f 6e 65 54 61 62 2e 61 5b 30  ;.    oneTab.a[0
2fe37 5d 20 3d 20 2a 70 54 61 62 49 74 65 6d 3b 0a 0a  ] = *pTabItem;..
2fe38 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a      /* Initializ
2fe39 65 20 74 68 65 20 72 6f 77 73 65 74 20 72 65 67  e the rowset reg
2fe3a 69 73 74 65 72 20 74 6f 20 63 6f 6e 74 61 69 6e  ister to contain
2fe3b 20 4e 55 4c 4c 2e 20 41 6e 20 53 51 4c 20 4e 55   NULL. An SQL NU
2fe3c 4c 4c 20 69 73 20 0a 20 20 20 20 2a 2a 20 65 71  LL is .    ** eq
2fe3d 75 69 76 61 6c 65 6e 74 20 74 6f 20 61 6e 20 65  uivalent to an e
2fe3e 6d 70 74 79 20 72 6f 77 73 65 74 2e 0a 20 20 20  mpty rowset..   
2fe3f 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 20   **.    ** Also 
2fe40 69 6e 69 74 69 61 6c 69 7a 65 20 72 65 67 52 65  initialize regRe
2fe41 74 75 72 6e 20 74 6f 20 63 6f 6e 74 61 69 6e 20  turn to contain 
2fe42 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74  the address of t
2fe43 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 0a  he instruction .
2fe44 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65      ** immediate
2fe45 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65  ly following the
2fe46 20 4f 50 5f 52 65 74 75 72 6e 20 61 74 20 74 68   OP_Return at th
2fe47 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20  e bottom of the 
2fe48 6c 6f 6f 70 2e 20 54 68 69 73 0a 20 20 20 20 2a  loop. This.    *
2fe49 2a 20 69 73 20 72 65 71 75 69 72 65 64 20 69 6e  * is required in
2fe4a 20 61 20 66 65 77 20 6f 62 73 63 75 72 65 20 4c   a few obscure L
2fe4b 45 46 54 20 4a 4f 49 4e 20 63 61 73 65 73 20 77  EFT JOIN cases w
2fe4c 68 65 72 65 20 63 6f 6e 74 72 6f 6c 20 6a 75 6d  here control jum
2fe4d 70 73 0a 20 20 20 20 2a 2a 20 6f 76 65 72 20 74  ps.    ** over t
2fe4e 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f  he top of the lo
2fe4f 6f 70 20 69 6e 74 6f 20 74 68 65 20 62 6f 64 79  op into the body
2fe50 20 6f 66 20 69 74 2e 20 49 6e 20 74 68 69 73 20   of it. In this 
2fe51 63 61 73 65 20 74 68 65 20 0a 20 20 20 20 2a 2a  case the .    **
2fe52 20 63 6f 72 72 65 63 74 20 72 65 73 70 6f 6e 73   correct respons
2fe53 65 20 66 6f 72 20 74 68 65 20 65 6e 64 2d 6f 66  e for the end-of
2fe54 2d 6c 6f 6f 70 20 63 6f 64 65 20 28 74 68 65 20  -loop code (the 
2fe55 4f 50 5f 52 65 74 75 72 6e 29 20 69 73 20 74 6f  OP_Return) is to
2fe56 20 0a 20 20 20 20 2a 2a 20 66 61 6c 6c 20 74 68   .    ** fall th
2fe57 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78  rough to the nex
2fe58 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c 20 6a  t instruction, j
2fe59 75 73 74 20 61 73 20 61 6e 20 4f 50 5f 4e 65 78  ust as an OP_Nex
2fe5a 74 20 64 6f 65 73 20 69 66 0a 20 20 20 20 2a 2a  t does if.    **
2fe5b 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 75 6e   called on an un
2fe5c 69 6e 69 74 69 61 6c 69 7a 65 64 20 63 75 72 73  initialized curs
2fe5d 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  or..    */.    i
2fe5e 66 28 20 28 77 63 74 72 6c 46 6c 61 67 73 20 26  f( (wctrlFlags &
2fe5f 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41 54 45   WHERE_DUPLICATE
2fe60 53 5f 4f 4b 29 3d 3d 30 20 29 7b 0a 20 20 20 20  S_OK)==0 ){.    
2fe61 20 20 72 65 67 52 6f 77 73 65 74 20 3d 20 2b 2b    regRowset = ++
2fe62 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2fe63 20 20 20 20 72 65 67 52 6f 77 69 64 20 3d 20 2b      regRowid = +
2fe64 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
2fe65 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2fe66 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
2fe67 6c 2c 20 30 2c 20 72 65 67 52 6f 77 73 65 74 29  l, 0, regRowset)
2fe68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 52 65 74  ;.    }.    iRet
2fe69 49 6e 69 74 20 3d 20 73 71 6c 69 74 65 33 56 64  Init = sqlite3Vd
2fe6a 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2fe6b 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 52 65  nteger, 0, regRe
2fe6c 74 75 72 6e 29 3b 0a 0a 20 20 20 20 66 6f 72 28  turn);..    for(
2fe6d 69 69 3d 30 3b 20 69 69 3c 70 4f 72 57 63 2d 3e  ii=0; ii<pOrWc->
2fe6e 6e 54 65 72 6d 3b 20 69 69 2b 2b 29 7b 0a 20 20  nTerm; ii++){.  
2fe6f 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70      WhereTerm *p
2fe70 4f 72 54 65 72 6d 20 3d 20 26 70 4f 72 57 63 2d  OrTerm = &pOrWc-
2fe71 3e 61 5b 69 69 5d 3b 0a 20 20 20 20 20 20 69 66  >a[ii];.      if
2fe72 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43  ( pOrTerm->leftC
2fe73 75 72 73 6f 72 3d 3d 69 43 75 72 20 7c 7c 20 70  ursor==iCur || p
2fe74 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f  OrTerm->eOperato
2fe75 72 3d 3d 57 4f 5f 41 4e 44 20 29 7b 0a 20 20 20  r==WO_AND ){.   
2fe76 20 20 20 20 20 57 68 65 72 65 49 6e 66 6f 20 2a       WhereInfo *
2fe77 70 53 75 62 57 49 6e 66 6f 3b 20 20 20 20 20 20  pSubWInfo;      
2fe78 20 20 20 20 2f 2a 20 49 6e 66 6f 20 66 6f 72 20      /* Info for 
2fe79 73 69 6e 67 6c 65 20 4f 52 2d 74 65 72 6d 20 73  single OR-term s
2fe7a 63 61 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 2f  can */.        /
2fe7b 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74  * Loop through t
2fe7c 61 62 6c 65 20 65 6e 74 72 69 65 73 20 74 68 61  able entries tha
2fe7d 74 20 6d 61 74 63 68 20 74 65 72 6d 20 70 4f 72  t match term pOr
2fe7e 54 65 72 6d 2e 20 2a 2f 0a 20 20 20 20 20 20 20  Term. */.       
2fe7f 20 70 53 75 62 57 49 6e 66 6f 20 3d 20 73 71 6c   pSubWInfo = sql
2fe80 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70  ite3WhereBegin(p
2fe81 50 61 72 73 65 2c 20 26 6f 6e 65 54 61 62 2c 20  Parse, &oneTab, 
2fe82 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20  pOrTerm->pExpr, 
2fe83 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
2fe84 20 20 20 20 20 20 20 20 20 20 20 57 48 45 52 45             WHERE
2fe85 5f 4f 4d 49 54 5f 4f 50 45 4e 20 7c 20 57 48 45  _OMIT_OPEN | WHE
2fe86 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 20 7c 20  RE_OMIT_CLOSE | 
2fe87 57 48 45 52 45 5f 46 4f 52 43 45 5f 54 41 42 4c  WHERE_FORCE_TABL
2fe88 45 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  E);.        if( 
2fe89 70 53 75 62 57 49 6e 66 6f 20 29 7b 0a 20 20 20  pSubWInfo ){.   
2fe8a 20 20 20 20 20 20 20 69 66 28 20 28 77 63 74 72         if( (wctr
2fe8b 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 44  lFlags & WHERE_D
2fe8c 55 50 4c 49 43 41 54 45 53 5f 4f 4b 29 3d 3d 30  UPLICATES_OK)==0
2fe8d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2fe8e 69 6e 74 20 69 53 65 74 20 3d 20 28 28 69 69 3d  int iSet = ((ii=
2fe8f 3d 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d 31 29  =pOrWc->nTerm-1)
2fe90 3f 2d 31 3a 69 69 29 3b 0a 20 20 20 20 20 20 20  ?-1:ii);.       
2fe91 20 20 20 20 20 69 6e 74 20 72 3b 0a 20 20 20 20       int r;.    
2fe92 20 20 20 20 20 20 20 20 72 20 3d 20 73 71 6c 69          r = sqli
2fe93 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f  te3ExprCodeGetCo
2fe94 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70 54 61  lumn(pParse, pTa
2fe95 62 49 74 65 6d 2d 3e 70 54 61 62 2c 20 2d 31 2c  bItem->pTab, -1,
2fe96 20 69 43 75 72 2c 20 0a 20 20 20 20 20 20 20 20   iCur, .        
2fe97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe99 20 72 65 67 52 6f 77 69 64 2c 20 30 29 3b 0a 20   regRowid, 0);. 
2fe9a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2fe9b 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2fe9c 4f 50 5f 52 6f 77 53 65 74 54 65 73 74 2c 20 72  OP_RowSetTest, r
2fe9d 65 67 52 6f 77 73 65 74 2c 0a 20 20 20 20 20 20  egRowset,.      
2fe9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe9f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2fea0 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
2fea1 29 2b 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20  )+2,.           
2fea2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fea3 20 20 20 72 2c 20 53 51 4c 49 54 45 5f 49 4e 54     r, SQLITE_INT
2fea4 5f 54 4f 5f 50 54 52 28 69 53 65 74 29 2c 20 50  _TO_PTR(iSet), P
2fea5 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20 20 20  4_INT32);.      
2fea6 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
2fea7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2fea8 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72  2(v, OP_Gosub, r
2fea9 65 67 52 65 74 75 72 6e 2c 20 69 4c 6f 6f 70 42  egReturn, iLoopB
2feaa 6f 64 79 29 3b 0a 0a 20 20 20 20 20 20 20 20 20  ody);..         
2feab 20 2f 2a 20 46 69 6e 69 73 68 20 74 68 65 20 6c   /* Finish the l
2feac 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 61 62 6c  oop through tabl
2fead 65 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 6d  e entries that m
2feae 61 74 63 68 20 74 65 72 6d 20 70 4f 72 54 65 72  atch term pOrTer
2feaf 6d 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  m. */.          
2feb0 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28  sqlite3WhereEnd(
2feb1 70 53 75 62 57 49 6e 66 6f 29 3b 0a 20 20 20 20  pSubWInfo);.    
2feb2 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
2feb3 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
2feb4 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 69  dbeChangeP1(v, i
2feb5 52 65 74 49 6e 69 74 2c 20 73 71 6c 69 74 65 33  RetInit, sqlite3
2feb6 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
2feb7 76 29 29 3b 0a 20 20 20 20 2f 2a 20 73 71 6c 69  v));.    /* sqli
2feb8 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2feb9 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67   OP_Null, 0, reg
2feba 52 6f 77 73 65 74 29 3b 20 2a 2f 0a 20 20 20 20  Rowset); */.    
2febb 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2febc 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
2febd 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72 6b   pLevel->addrBrk
2febe 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2febf 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
2fec0 2c 20 69 4c 6f 6f 70 42 6f 64 79 29 3b 0a 0a 20  , iLoopBody);.. 
2fec1 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20     pLevel->op = 
2fec2 4f 50 5f 52 65 74 75 72 6e 3b 0a 20 20 20 20 70  OP_Return;.    p
2fec3 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 72 65 67 52  Level->p1 = regR
2fec4 65 74 75 72 6e 3b 0a 20 20 20 20 64 69 73 61 62  eturn;.    disab
2fec5 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70  leTerm(pLevel, p
2fec6 54 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 0a 23  Term);.  }else.#
2fec7 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2fec8 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41  OMIT_OR_OPTIMIZA
2fec9 54 49 4f 4e 20 2a 2f 0a 0a 20 20 7b 0a 20 20 20  TION */..  {.   
2feca 20 2f 2a 20 43 61 73 65 20 35 3a 20 20 54 68 65   /* Case 5:  The
2fecb 72 65 20 69 73 20 6e 6f 20 75 73 61 62 6c 65 20  re is no usable 
2fecc 69 6e 64 65 78 2e 20 20 57 65 20 6d 75 73 74 20  index.  We must 
2fecd 64 6f 20 61 20 63 6f 6d 70 6c 65 74 65 0a 20 20  do a complete.  
2fece 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 73 63    **          sc
2fecf 61 6e 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65  an of the entire
2fed0 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20   table..    */. 
2fed1 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
2fed2 75 38 20 61 53 74 65 70 5b 5d 20 3d 20 7b 20 4f  u8 aStep[] = { O
2fed3 50 5f 4e 65 78 74 2c 20 4f 50 5f 50 72 65 76 20  P_Next, OP_Prev 
2fed4 7d 3b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  };.    static co
2fed5 6e 73 74 20 75 38 20 61 53 74 61 72 74 5b 5d 20  nst u8 aStart[] 
2fed6 3d 20 7b 20 4f 50 5f 52 65 77 69 6e 64 2c 20 4f  = { OP_Rewind, O
2fed7 50 5f 4c 61 73 74 20 7d 3b 0a 20 20 20 20 61 73  P_Last };.    as
2fed8 73 65 72 74 28 20 62 52 65 76 3d 3d 30 20 7c 7c  sert( bRev==0 ||
2fed9 20 62 52 65 76 3d 3d 31 20 29 3b 0a 20 20 20 20   bRev==1 );.    
2feda 61 73 73 65 72 74 28 20 6f 6d 69 74 54 61 62 6c  assert( omitTabl
2fedb 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70 4c 65 76  e==0 );.    pLev
2fedc 65 6c 2d 3e 6f 70 20 3d 20 61 53 74 65 70 5b 62  el->op = aStep[b
2fedd 52 65 76 5d 3b 0a 20 20 20 20 70 4c 65 76 65 6c  Rev];.    pLevel
2fede 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0a 20 20 20  ->p1 = iCur;.   
2fedf 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 31 20   pLevel->p2 = 1 
2fee0 2b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  + sqlite3VdbeAdd
2fee1 4f 70 32 28 76 2c 20 61 53 74 61 72 74 5b 62 52  Op2(v, aStart[bR
2fee2 65 76 5d 2c 20 69 43 75 72 2c 20 61 64 64 72 42  ev], iCur, addrB
2fee3 72 6b 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  rk);.    pLevel-
2fee4 3e 70 35 20 3d 20 53 51 4c 49 54 45 5f 53 54 4d  >p5 = SQLITE_STM
2fee5 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e  TSTATUS_FULLSCAN
2fee6 5f 53 54 45 50 3b 0a 20 20 7d 0a 20 20 6e 6f 74  _STEP;.  }.  not
2fee7 52 65 61 64 79 20 26 3d 20 7e 67 65 74 4d 61 73  Ready &= ~getMas
2fee8 6b 28 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 2c  k(pWC->pMaskSet,
2fee9 20 69 43 75 72 29 3b 0a 0a 20 20 2f 2a 20 49 6e   iCur);..  /* In
2feea 73 65 72 74 20 63 6f 64 65 20 74 6f 20 74 65 73  sert code to tes
2feeb 74 20 65 76 65 72 79 20 73 75 62 65 78 70 72 65  t every subexpre
2feec 73 73 69 6f 6e 20 74 68 61 74 20 63 61 6e 20 62  ssion that can b
2feed 65 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 20 20 2a  e completely.  *
2feee 2a 20 63 6f 6d 70 75 74 65 64 20 75 73 69 6e 67  * computed using
2feef 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 65 74   the current set
2fef0 20 6f 66 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f   of tables..  */
2fef1 0a 20 20 6b 20 3d 20 30 3b 0a 20 20 66 6f 72 28  .  k = 0;.  for(
2fef2 70 54 65 72 6d 3d 70 57 43 2d 3e 61 2c 20 6a 3d  pTerm=pWC->a, j=
2fef3 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a 3e 30 3b  pWC->nTerm; j>0;
2fef4 20 6a 2d 2d 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a   j--, pTerm++){.
2fef5 20 20 20 20 45 78 70 72 20 2a 70 45 3b 0a 20 20      Expr *pE;.  
2fef6 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72    testcase( pTer
2fef7 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52  m->wtFlags & TER
2fef8 4d 5f 56 49 52 54 55 41 4c 20 29 3b 0a 20 20 20  M_VIRTUAL );.   
2fef9 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d   testcase( pTerm
2fefa 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d  ->wtFlags & TERM
2fefb 5f 43 4f 44 45 44 20 29 3b 0a 20 20 20 20 69 66  _CODED );.    if
2fefc 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73  ( pTerm->wtFlags
2fefd 20 26 20 28 54 45 52 4d 5f 56 49 52 54 55 41 4c   & (TERM_VIRTUAL
2fefe 7c 54 45 52 4d 5f 43 4f 44 45 44 29 20 29 20 63  |TERM_CODED) ) c
2feff 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28  ontinue;.    if(
2ff00 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41   (pTerm->prereqA
2ff01 6c 6c 20 26 20 6e 6f 74 52 65 61 64 79 29 21 3d  ll & notReady)!=
2ff02 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
2ff03 20 20 70 45 20 3d 20 70 54 65 72 6d 2d 3e 70 45    pE = pTerm->pE
2ff04 78 70 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28  xpr;.    assert(
2ff05 20 70 45 21 3d 30 20 29 3b 0a 20 20 20 20 69 66   pE!=0 );.    if
2ff06 28 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a  ( pLevel->iLeftJ
2ff07 6f 69 6e 20 26 26 20 21 45 78 70 72 48 61 73 50  oin && !ExprHasP
2ff08 72 6f 70 65 72 74 79 28 70 45 2c 20 45 50 5f 46  roperty(pE, EP_F
2ff09 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20  romJoin) ){.    
2ff0a 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
2ff0b 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  }.    sqlite3Exp
2ff0c 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c  rIfFalse(pParse,
2ff0d 20 70 45 2c 20 61 64 64 72 43 6f 6e 74 2c 20 53   pE, addrCont, S
2ff0e 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
2ff0f 29 3b 0a 20 20 20 20 6b 20 3d 20 31 3b 0a 20 20  );.    k = 1;.  
2ff10 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73    pTerm->wtFlags
2ff11 20 7c 3d 20 54 45 52 4d 5f 43 4f 44 45 44 3b 0a   |= TERM_CODED;.
2ff12 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20 61 20    }..  /* For a 
2ff13 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 2c  LEFT OUTER JOIN,
2ff14 20 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   generate code t
2ff15 68 61 74 20 77 69 6c 6c 20 72 65 63 6f 72 64 20  hat will record 
2ff16 74 68 65 20 66 61 63 74 20 74 68 61 74 0a 20 20  the fact that.  
2ff17 2a 2a 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20  ** at least one 
2ff18 72 6f 77 20 6f 66 20 74 68 65 20 72 69 67 68 74  row of the right
2ff19 20 74 61 62 6c 65 20 68 61 73 20 6d 61 74 63 68   table has match
2ff1a 65 64 20 74 68 65 20 6c 65 66 74 20 74 61 62 6c  ed the left tabl
2ff1b 65 2e 20 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20  e.  .  */.  if( 
2ff1c 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69  pLevel->iLeftJoi
2ff1d 6e 20 29 7b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  n ){.    pLevel-
2ff1e 3e 61 64 64 72 46 69 72 73 74 20 3d 20 73 71 6c  >addrFirst = sql
2ff1f 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
2ff20 64 64 72 28 76 29 3b 0a 20 20 20 20 73 71 6c 69  ddr(v);.    sqli
2ff21 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2ff22 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20   OP_Integer, 1, 
2ff23 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69  pLevel->iLeftJoi
2ff24 6e 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d  n);.    VdbeComm
2ff25 65 6e 74 28 28 76 2c 20 22 72 65 63 6f 72 64 20  ent((v, "record 
2ff26 4c 45 46 54 20 4a 4f 49 4e 20 68 69 74 22 29 29  LEFT JOIN hit"))
2ff27 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
2ff28 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72  rCacheClear(pPar
2ff29 73 65 29 3b 0a 20 20 20 20 66 6f 72 28 70 54 65  se);.    for(pTe
2ff2a 72 6d 3d 70 57 43 2d 3e 61 2c 20 6a 3d 30 3b 20  rm=pWC->a, j=0; 
2ff2b 6a 3c 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a 2b  j<pWC->nTerm; j+
2ff2c 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20  +, pTerm++){.   
2ff2d 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65     testcase( pTe
2ff2e 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45  rm->wtFlags & TE
2ff2f 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b 0a 20 20  RM_VIRTUAL );.  
2ff30 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54      testcase( pT
2ff31 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54  erm->wtFlags & T
2ff32 45 52 4d 5f 43 4f 44 45 44 20 29 3b 0a 20 20 20  ERM_CODED );.   
2ff33 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 77 74     if( pTerm->wt
2ff34 46 6c 61 67 73 20 26 20 28 54 45 52 4d 5f 56 49  Flags & (TERM_VI
2ff35 52 54 55 41 4c 7c 54 45 52 4d 5f 43 4f 44 45 44  RTUAL|TERM_CODED
2ff36 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  ) ) continue;.  
2ff37 20 20 20 20 69 66 28 20 28 70 54 65 72 6d 2d 3e      if( (pTerm->
2ff38 70 72 65 72 65 71 41 6c 6c 20 26 20 6e 6f 74 52  prereqAll & notR
2ff39 65 61 64 79 29 21 3d 30 20 29 20 63 6f 6e 74 69  eady)!=0 ) conti
2ff3a 6e 75 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72  nue;.      asser
2ff3b 74 28 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 20  t( pTerm->pExpr 
2ff3c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2ff3d 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72  ExprIfFalse(pPar
2ff3e 73 65 2c 20 70 54 65 72 6d 2d 3e 70 45 78 70 72  se, pTerm->pExpr
2ff3f 2c 20 61 64 64 72 43 6f 6e 74 2c 20 53 51 4c 49  , addrCont, SQLI
2ff40 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a  TE_JUMPIFNULL);.
2ff41 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 77 74 46        pTerm->wtF
2ff42 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 44  lags |= TERM_COD
2ff43 45 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ED;.    }.  }.  
2ff44 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2ff45 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 69 52  mpReg(pParse, iR
2ff46 65 6c 65 61 73 65 52 65 67 29 3b 0a 0a 20 20 72  eleaseReg);..  r
2ff47 65 74 75 72 6e 20 6e 6f 74 52 65 61 64 79 3b 0a  eturn notReady;.
2ff48 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  }..#if defined(S
2ff49 51 4c 49 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a  QLITE_TEST)./*.*
2ff4a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
2ff4b 76 61 72 69 61 62 6c 65 20 68 6f 6c 64 73 20 61  variable holds a
2ff4c 20 74 65 78 74 20 64 65 73 63 72 69 70 74 69 6f   text descriptio
2ff4d 6e 20 6f 66 20 71 75 65 72 79 20 70 6c 61 6e 20  n of query plan 
2ff4e 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 62 79 20  generated.** by 
2ff4f 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
2ff50 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 57  call to sqlite3W
2ff51 68 65 72 65 42 65 67 69 6e 28 29 2e 20 20 45 61  hereBegin().  Ea
2ff52 63 68 20 63 61 6c 6c 20 74 6f 20 57 68 65 72 65  ch call to Where
2ff53 42 65 67 69 6e 0a 2a 2a 20 6f 76 65 72 77 72 69  Begin.** overwri
2ff54 74 65 73 20 74 68 65 20 70 72 65 76 69 6f 75 73  tes the previous
2ff55 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74  .  This informat
2ff56 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72 20  ion is used for 
2ff57 74 65 73 74 69 6e 67 20 61 6e 64 0a 2a 2a 20 61  testing and.** a
2ff58 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a 2a 2f  nalysis only..*/
2ff59 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72  .SQLITE_API char
2ff5a 20 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70   sqlite3_query_p
2ff5b 6c 61 6e 5b 42 4d 53 2a 32 2a 34 30 5d 3b 20 20  lan[BMS*2*40];  
2ff5c 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 6a  /* Text of the j
2ff5d 6f 69 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  oin */.static in
2ff5e 74 20 6e 51 50 6c 61 6e 20 3d 20 30 3b 20 20 20  t nQPlan = 0;   
2ff5f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
2ff60 78 74 20 66 72 65 65 20 73 6c 6f 77 20 69 6e 20  xt free slow in 
2ff61 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 5d 20 2a 2f  _query_plan[] */
2ff62 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
2ff63 54 45 5f 54 45 53 54 20 2a 2f 0a 0a 0a 2f 2a 0a  TE_TEST */.../*.
2ff64 2a 2a 20 46 72 65 65 20 61 20 57 68 65 72 65 49  ** Free a WhereI
2ff65 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a 2f  nfo structure.*/
2ff66 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 68 65  .static void whe
2ff67 72 65 49 6e 66 6f 46 72 65 65 28 73 71 6c 69 74  reInfoFree(sqlit
2ff68 65 33 20 2a 64 62 2c 20 57 68 65 72 65 49 6e 66  e3 *db, WhereInf
2ff69 6f 20 2a 70 57 49 6e 66 6f 29 7b 0a 20 20 69 66  o *pWInfo){.  if
2ff6a 28 20 70 57 49 6e 66 6f 20 29 7b 0a 20 20 20 20  ( pWInfo ){.    
2ff6b 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69  int i;.    for(i
2ff6c 3d 30 3b 20 69 3c 70 57 49 6e 66 6f 2d 3e 6e 4c  =0; i<pWInfo->nL
2ff6d 65 76 65 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  evel; i++){.    
2ff6e 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f    sqlite3_index_
2ff6f 69 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 57  info *pInfo = pW
2ff70 49 6e 66 6f 2d 3e 61 5b 69 5d 2e 70 49 64 78 49  Info->a[i].pIdxI
2ff71 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 70  nfo;.      if( p
2ff72 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 20 20  Info ){.        
2ff73 2f 2a 20 61 73 73 65 72 74 28 20 70 49 6e 66 6f  /* assert( pInfo
2ff74 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53  ->needToFreeIdxS
2ff75 74 72 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c  tr==0 || db->mal
2ff76 6c 6f 63 46 61 69 6c 65 64 20 29 3b 20 2a 2f 0a  locFailed ); */.
2ff77 20 20 20 20 20 20 20 20 69 66 28 20 70 49 6e 66          if( pInf
2ff78 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78  o->needToFreeIdx
2ff79 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Str ){.         
2ff7a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49   sqlite3_free(pI
2ff7b 6e 66 6f 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20  nfo->idxStr);.  
2ff7c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2ff7d 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2ff7e 2c 20 70 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20  , pInfo);.      
2ff7f 7d 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 65 72  }.    }.    wher
2ff80 65 43 6c 61 75 73 65 43 6c 65 61 72 28 70 57 49  eClauseClear(pWI
2ff81 6e 66 6f 2d 3e 70 57 43 29 3b 0a 20 20 20 20 73  nfo->pWC);.    s
2ff82 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2ff83 20 70 57 49 6e 66 6f 29 3b 0a 20 20 7d 0a 7d 0a   pWInfo);.  }.}.
2ff84 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
2ff85 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
2ff86 66 20 74 68 65 20 6c 6f 6f 70 20 75 73 65 64 20  f the loop used 
2ff87 66 6f 72 20 57 48 45 52 45 20 63 6c 61 75 73 65  for WHERE clause
2ff88 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 20   processing..** 
2ff89 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
2ff8a 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
2ff8b 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75 63   an opaque struc
2ff8c 74 75 72 65 20 74 68 61 74 20 63 6f 6e 74 61 69  ture that contai
2ff8d 6e 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f  ns.** informatio
2ff8e 6e 20 6e 65 65 64 65 64 20 74 6f 20 74 65 72 6d  n needed to term
2ff8f 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 2e 20  inate the loop. 
2ff90 20 4c 61 74 65 72 2c 20 74 68 65 20 63 61 6c 6c   Later, the call
2ff91 69 6e 67 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 73  ing routine.** s
2ff92 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 20 73 71 6c  hould invoke sql
2ff93 69 74 65 33 57 68 65 72 65 45 6e 64 28 29 20 77  ite3WhereEnd() w
2ff94 69 74 68 20 74 68 65 20 72 65 74 75 72 6e 20 76  ith the return v
2ff95 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e  alue of this fun
2ff96 63 74 69 6f 6e 0a 2a 2a 20 69 6e 20 6f 72 64 65  ction.** in orde
2ff97 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68  r to complete th
2ff98 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 70  e WHERE clause p
2ff99 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a  rocessing..**.**
2ff9a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
2ff9b 75 72 73 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  urs, this routin
2ff9c 65 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a  e returns NULL..
2ff9d 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73 69 63 20  **.** The basic 
2ff9e 69 64 65 61 20 69 73 20 74 6f 20 64 6f 20 61 20  idea is to do a 
2ff9f 6e 65 73 74 65 64 20 6c 6f 6f 70 2c 20 6f 6e 65  nested loop, one
2ffa0 20 6c 6f 6f 70 20 66 6f 72 20 65 61 63 68 20 74   loop for each t
2ffa1 61 62 6c 65 20 69 6e 0a 2a 2a 20 74 68 65 20 46  able in.** the F
2ffa2 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20  ROM clause of a 
2ffa3 73 65 6c 65 63 74 2e 20 20 28 49 4e 53 45 52 54  select.  (INSERT
2ffa4 20 61 6e 64 20 55 50 44 41 54 45 20 73 74 61 74   and UPDATE stat
2ffa5 65 6d 65 6e 74 73 20 61 72 65 20 74 68 65 0a 2a  ements are the.*
2ffa6 2a 20 73 61 6d 65 20 61 73 20 61 20 53 45 4c 45  * same as a SELE
2ffa7 43 54 20 77 69 74 68 20 6f 6e 6c 79 20 61 20 73  CT with only a s
2ffa8 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 74  ingle table in t
2ffa9 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 29  he FROM clause.)
2ffaa 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65    For.** example
2ffab 2c 20 69 66 20 74 68 65 20 53 51 4c 20 69 73 20  , if the SQL is 
2ffac 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  this:.**.**     
2ffad 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
2ffae 74 31 2c 20 74 32 2c 20 74 33 20 57 48 45 52 45  t1, t2, t3 WHERE
2ffaf 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e   ...;.**.** Then
2ffb0 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61   the code genera
2ffb1 74 65 64 20 69 73 20 63 6f 6e 63 65 70 74 75 61  ted is conceptua
2ffb2 6c 6c 79 20 6c 69 6b 65 20 74 68 65 20 66 6f 6c  lly like the fol
2ffb3 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  lowing:.**.**   
2ffb4 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77 31 20     foreach row1 
2ffb5 69 6e 20 74 31 20 64 6f 20 20 20 20 20 20 20 5c  in t1 do       \
2ffb6 20 20 20 20 43 6f 64 65 20 67 65 6e 65 72 61 74      Code generat
2ffb7 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 66 6f 72  ed.**        for
2ffb8 65 61 63 68 20 72 6f 77 32 20 69 6e 20 74 32 20  each row2 in t2 
2ffb9 64 6f 20 20 20 20 20 20 7c 2d 2d 20 62 79 20 73  do      |-- by s
2ffba 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
2ffbb 28 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66  ().**          f
2ffbc 6f 72 65 61 63 68 20 72 6f 77 33 20 69 6e 20 74  oreach row3 in t
2ffbd 33 20 64 6f 20 20 20 2f 0a 2a 2a 20 20 20 20 20  3 do   /.**     
2ffbe 20 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20         ....**   
2ffbf 20 20 20 20 20 20 20 65 6e 64 20 20 20 20 20 20         end      
2ffc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
2ffc1 20 20 20 20 43 6f 64 65 20 67 65 6e 65 72 61 74      Code generat
2ffc2 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 65 6e 64  ed.**        end
2ffc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ffc4 20 20 20 20 20 20 20 20 7c 2d 2d 20 62 79 20 73          |-- by s
2ffc5 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 29  qlite3WhereEnd()
2ffc6 0a 2a 2a 20 20 20 20 20 20 65 6e 64 20 20 20 20  .**      end    
2ffc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ffc8 20 20 20 20 20 2f 0a 2a 2a 0a 2a 2a 20 4e 6f 74       /.**.** Not
2ffc9 65 20 74 68 61 74 20 74 68 65 20 6c 6f 6f 70 73  e that the loops
2ffca 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 6e 65   might not be ne
2ffcb 73 74 65 64 20 69 6e 20 74 68 65 20 6f 72 64 65  sted in the orde
2ffcc 72 20 69 6e 20 77 68 69 63 68 20 74 68 65 79 0a  r in which they.
2ffcd 2a 2a 20 61 70 70 65 61 72 20 69 6e 20 74 68 65  ** appear in the
2ffce 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 66 20   FROM clause if 
2ffcf 61 20 64 69 66 66 65 72 65 6e 74 20 6f 72 64 65  a different orde
2ffd0 72 20 69 73 20 62 65 74 74 65 72 20 61 62 6c 65  r is better able
2ffd1 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 75 73 65 20   to make.** use 
2ffd2 6f 66 20 69 6e 64 69 63 65 73 2e 20 20 4e 6f 74  of indices.  Not
2ffd3 65 20 61 6c 73 6f 20 74 68 61 74 20 77 68 65 6e  e also that when
2ffd4 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72   the IN operator
2ffd5 20 61 70 70 65 61 72 73 20 69 6e 0a 2a 2a 20 74   appears in.** t
2ffd6 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2c  he WHERE clause,
2ffd7 20 69 74 20 6d 69 67 68 74 20 72 65 73 75 6c 74   it might result
2ffd8 20 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6e   in additional n
2ffd9 65 73 74 65 64 20 6c 6f 6f 70 73 20 66 6f 72 0a  ested loops for.
2ffda 2a 2a 20 73 63 61 6e 6e 69 6e 67 20 74 68 72 6f  ** scanning thro
2ffdb 75 67 68 20 61 6c 6c 20 76 61 6c 75 65 73 20 6f  ugh all values o
2ffdc 6e 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64  n the right-hand
2ffdd 20 73 69 64 65 20 6f 66 20 74 68 65 20 49 4e 2e   side of the IN.
2ffde 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  .**.** There are
2ffdf 20 42 74 72 65 65 20 63 75 72 73 6f 72 73 20 61   Btree cursors a
2ffe0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65  ssociated with e
2ffe1 61 63 68 20 74 61 62 6c 65 2e 20 20 74 31 20 75  ach table.  t1 u
2ffe2 73 65 73 20 63 75 72 73 6f 72 0a 2a 2a 20 6e 75  ses cursor.** nu
2ffe3 6d 62 65 72 20 70 54 61 62 4c 69 73 74 2d 3e 61  mber pTabList->a
2ffe4 5b 30 5d 2e 69 43 75 72 73 6f 72 2e 20 20 74 32  [0].iCursor.  t2
2ffe5 20 75 73 65 73 20 74 68 65 20 63 75 72 73 6f 72   uses the cursor
2ffe6 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 31 5d 2e   pTabList->a[1].
2ffe7 69 43 75 72 73 6f 72 2e 0a 2a 2a 20 41 6e 64 20  iCursor..** And 
2ffe8 73 6f 20 66 6f 72 74 68 2e 20 20 54 68 69 73 20  so forth.  This 
2ffe9 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65  routine generate
2ffea 73 20 63 6f 64 65 20 74 6f 20 6f 70 65 6e 20 74  s code to open t
2ffeb 68 6f 73 65 20 56 44 42 45 20 63 75 72 73 6f 72  hose VDBE cursor
2ffec 73 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33  s.** and sqlite3
2ffed 57 68 65 72 65 45 6e 64 28 29 20 67 65 6e 65 72  WhereEnd() gener
2ffee 61 74 65 73 20 74 68 65 20 63 6f 64 65 20 74 6f  ates the code to
2ffef 20 63 6c 6f 73 65 20 74 68 65 6d 2e 0a 2a 2a 0a   close them..**.
2fff0 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 68 61 74  ** The code that
2fff1 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
2fff2 69 6e 28 29 20 67 65 6e 65 72 61 74 65 73 20 6c  in() generates l
2fff3 65 61 76 65 73 20 74 68 65 20 63 75 72 73 6f 72  eaves the cursor
2fff4 73 20 6e 61 6d 65 64 0a 2a 2a 20 69 6e 20 70 54  s named.** in pT
2fff5 61 62 4c 69 73 74 20 70 6f 69 6e 74 69 6e 67 20  abList pointing 
2fff6 61 74 20 74 68 65 69 72 20 61 70 70 72 6f 70 72  at their appropr
2fff7 69 61 74 65 20 65 6e 74 72 69 65 73 2e 20 20 54  iate entries.  T
2fff8 68 65 20 5b 2e 2e 2e 5d 20 63 6f 64 65 0a 2a 2a  he [...] code.**
2fff9 20 63 61 6e 20 75 73 65 20 4f 50 5f 43 6f 6c 75   can use OP_Colu
2fffa 6d 6e 20 61 6e 64 20 4f 50 5f 52 6f 77 69 64 20  mn and OP_Rowid 
2fffb 6f 70 63 6f 64 65 73 20 6f 6e 20 74 68 65 73 65  opcodes on these
2fffc 20 63 75 72 73 6f 72 73 20 74 6f 20 65 78 74 72   cursors to extr
2fffd 61 63 74 0a 2a 2a 20 64 61 74 61 20 66 72 6f 6d  act.** data from
2fffe 20 74 68 65 20 76 61 72 69 6f 75 73 20 74 61 62   the various tab
2ffff 6c 65 73 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e  les of the loop.
30000 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 57 48  .**.** If the WH
30001 45 52 45 20 63 6c 61 75 73 65 20 69 73 20 65 6d  ERE clause is em
30002 70 74 79 2c 20 74 68 65 20 66 6f 72 65 61 63 68  pty, the foreach
30003 20 6c 6f 6f 70 73 20 6d 75 73 74 20 65 61 63 68   loops must each
30004 20 73 63 61 6e 20 74 68 65 69 72 0a 2a 2a 20 65   scan their.** e
30005 6e 74 69 72 65 20 74 61 62 6c 65 73 2e 20 20 54  ntire tables.  T
30006 68 75 73 20 61 20 74 68 72 65 65 2d 77 61 79 20  hus a three-way 
30007 6a 6f 69 6e 20 69 73 20 61 6e 20 4f 28 4e 5e 33  join is an O(N^3
30008 29 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 75  ) operation.  Bu
30009 74 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c  t if.** the tabl
3000a 65 73 20 68 61 76 65 20 69 6e 64 69 63 65 73 20  es have indices 
3000b 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 74 65  and there are te
3000c 72 6d 73 20 69 6e 20 74 68 65 20 57 48 45 52 45  rms in the WHERE
3000d 20 63 6c 61 75 73 65 20 74 68 61 74 0a 2a 2a 20   clause that.** 
3000e 72 65 66 65 72 20 74 6f 20 74 68 6f 73 65 20 69  refer to those i
3000f 6e 64 69 63 65 73 2c 20 61 20 63 6f 6d 70 6c 65  ndices, a comple
30010 74 65 20 74 61 62 6c 65 20 73 63 61 6e 20 63 61  te table scan ca
30011 6e 20 62 65 20 61 76 6f 69 64 65 64 20 61 6e 64  n be avoided and
30012 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20 77 69 6c   the.** code wil
30013 6c 20 72 75 6e 20 6d 75 63 68 20 66 61 73 74 65  l run much faste
30014 72 2e 20 20 4d 6f 73 74 20 6f 66 20 74 68 65 20  r.  Most of the 
30015 77 6f 72 6b 20 6f 66 20 74 68 69 73 20 72 6f 75  work of this rou
30016 74 69 6e 65 20 69 73 20 63 68 65 63 6b 69 6e 67  tine is checking
30017 0a 2a 2a 20 74 6f 20 73 65 65 20 69 66 20 74 68  .** to see if th
30018 65 72 65 20 61 72 65 20 69 6e 64 69 63 65 73 20  ere are indices 
30019 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 64  that can be used
3001a 20 74 6f 20 73 70 65 65 64 20 75 70 20 74 68 65   to speed up the
3001b 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 65 72   loop..**.** Ter
3001c 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  ms of the WHERE 
3001d 63 6c 61 75 73 65 20 61 72 65 20 61 6c 73 6f 20  clause are also 
3001e 75 73 65 64 20 74 6f 20 6c 69 6d 69 74 20 77 68  used to limit wh
3001f 69 63 68 20 72 6f 77 73 20 61 63 74 75 61 6c 6c  ich rows actuall
30020 79 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 74 6f 20  y.** make it to 
30021 74 68 65 20 22 2e 2e 2e 22 20 69 6e 20 74 68 65  the "..." in the
30022 20 6d 69 64 64 6c 65 20 6f 66 20 74 68 65 20 6c   middle of the l
30023 6f 6f 70 2e 20 20 41 66 74 65 72 20 65 61 63 68  oop.  After each
30024 20 22 66 6f 72 65 61 63 68 22 2c 0a 2a 2a 20 74   "foreach",.** t
30025 65 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52  erms of the WHER
30026 45 20 63 6c 61 75 73 65 20 74 68 61 74 20 75 73  E clause that us
30027 65 20 6f 6e 6c 79 20 74 65 72 6d 73 20 69 6e 20  e only terms in 
30028 74 68 61 74 20 6c 6f 6f 70 20 61 6e 64 20 6f 75  that loop and ou
30029 74 65 72 0a 2a 2a 20 6c 6f 6f 70 73 20 61 72 65  ter.** loops are
3002a 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 69   evaluated and i
3002b 66 20 66 61 6c 73 65 20 61 20 6a 75 6d 70 20 69  f false a jump i
3002c 73 20 6d 61 64 65 20 61 72 6f 75 6e 64 20 61 6c  s made around al
3002d 6c 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20  l subsequent.** 
3002e 69 6e 6e 65 72 20 6c 6f 6f 70 73 20 28 6f 72 20  inner loops (or 
3002f 61 72 6f 75 6e 64 20 74 68 65 20 22 2e 2e 2e 22  around the "..."
30030 20 69 66 20 74 68 65 20 74 65 73 74 20 6f 63 63   if the test occ
30031 75 72 73 20 77 69 74 68 69 6e 20 74 68 65 20 69  urs within the i
30032 6e 6e 65 72 2d 0a 2a 2a 20 6d 6f 73 74 20 6c 6f  nner-.** most lo
30033 6f 70 29 0a 2a 2a 0a 2a 2a 20 4f 55 54 45 52 20  op).**.** OUTER 
30034 4a 4f 49 4e 53 0a 2a 2a 0a 2a 2a 20 41 6e 20 6f  JOINS.**.** An o
30035 75 74 65 72 20 6a 6f 69 6e 20 6f 66 20 74 61 62  uter join of tab
30036 6c 65 73 20 74 31 20 61 6e 64 20 74 32 20 69 73  les t1 and t2 is
30037 20 63 6f 6e 63 65 70 74 61 6c 6c 79 20 63 6f 64   conceptally cod
30038 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  ed as follows:.*
30039 2a 0a 2a 2a 20 20 20 20 66 6f 72 65 61 63 68 20  *.**    foreach 
3003a 72 6f 77 31 20 69 6e 20 74 31 20 64 6f 0a 2a 2a  row1 in t1 do.**
3003b 20 20 20 20 20 20 66 6c 61 67 20 3d 20 30 0a 2a        flag = 0.*
3003c 2a 20 20 20 20 20 20 66 6f 72 65 61 63 68 20 72  *      foreach r
3003d 6f 77 32 20 69 6e 20 74 32 20 64 6f 0a 2a 2a 20  ow2 in t2 do.** 
3003e 20 20 20 20 20 20 20 73 74 61 72 74 3a 0a 2a 2a         start:.**
3003f 20 20 20 20 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a            ....**
30040 20 20 20 20 20 20 20 20 20 20 66 6c 61 67 20 3d            flag =
30041 20 31 0a 2a 2a 20 20 20 20 20 20 65 6e 64 0a 2a   1.**      end.*
30042 2a 20 20 20 20 20 20 69 66 20 66 6c 61 67 3d 3d  *      if flag==
30043 30 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20  0 then.**       
30044 20 6d 6f 76 65 20 74 68 65 20 72 6f 77 32 20 63   move the row2 c
30045 75 72 73 6f 72 20 74 6f 20 61 20 6e 75 6c 6c 20  ursor to a null 
30046 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 67 6f  row.**        go
30047 74 6f 20 73 74 61 72 74 0a 2a 2a 20 20 20 20 20  to start.**     
30048 20 66 69 0a 2a 2a 20 20 20 20 65 6e 64 0a 2a 2a   fi.**    end.**
30049 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 43 4c 41  .** ORDER BY CLA
3004a 55 53 45 20 50 52 4f 43 45 53 53 49 4e 47 0a 2a  USE PROCESSING.*
3004b 2a 0a 2a 2a 20 2a 70 70 4f 72 64 65 72 42 79 20  *.** *ppOrderBy 
3004c 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
3004d 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
3004e 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20  use of a SELECT 
3004f 73 74 61 74 65 6d 65 6e 74 2c 0a 2a 2a 20 69 66  statement,.** if
30050 20 74 68 65 72 65 20 69 73 20 6f 6e 65 2e 20 20   there is one.  
30051 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4f  If there is no O
30052 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 6f  RDER BY clause o
30053 72 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e  r if this routin
30054 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 66  e.** is called f
30055 72 6f 6d 20 61 6e 20 55 50 44 41 54 45 20 6f 72  rom an UPDATE or
30056 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e   DELETE statemen
30057 74 2c 20 74 68 65 6e 20 70 70 4f 72 64 65 72 42  t, then ppOrderB
30058 79 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  y is NULL..**.**
30059 20 49 66 20 61 6e 20 69 6e 64 65 78 20 63 61 6e   If an index can
3005a 20 62 65 20 75 73 65 64 20 73 6f 20 74 68 61 74   be used so that
3005b 20 74 68 65 20 6e 61 74 75 72 61 6c 20 6f 75 74   the natural out
3005c 70 75 74 20 6f 72 64 65 72 20 6f 66 20 74 68 65  put order of the
3005d 20 74 61 62 6c 65 0a 2a 2a 20 73 63 61 6e 20 69   table.** scan i
3005e 73 20 63 6f 72 72 65 63 74 20 66 6f 72 20 74 68  s correct for th
3005f 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
30060 65 2c 20 74 68 65 6e 20 74 68 61 74 20 69 6e 64  e, then that ind
30061 65 78 20 69 73 20 75 73 65 64 20 61 6e 64 0a 2a  ex is used and.*
30062 2a 20 2a 70 70 4f 72 64 65 72 42 79 20 69 73 20  * *ppOrderBy is 
30063 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 20 54 68  set to NULL.  Th
30064 69 73 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a  is is an optimiz
30065 61 74 69 6f 6e 20 74 68 61 74 20 70 72 65 76 65  ation that preve
30066 6e 74 73 20 61 6e 0a 2a 2a 20 75 6e 6e 65 63 65  nts an.** unnece
30067 73 73 61 72 79 20 73 6f 72 74 20 6f 66 20 74 68  ssary sort of th
30068 65 20 72 65 73 75 6c 74 20 73 65 74 20 69 66 20  e result set if 
30069 61 6e 20 69 6e 64 65 78 20 61 70 70 72 6f 70 72  an index appropr
3006a 69 61 74 65 20 66 6f 72 20 74 68 65 0a 2a 2a 20  iate for the.** 
3006b 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
3006c 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e 0a  already exists..
3006d 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 77 68 65  **.** If the whe
3006e 72 65 20 63 6c 61 75 73 65 20 6c 6f 6f 70 73 20  re clause loops 
3006f 63 61 6e 6e 6f 74 20 62 65 20 61 72 72 61 6e 67  cannot be arrang
30070 65 64 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68  ed to provide th
30071 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20 6f 75 74  e correct.** out
30072 70 75 74 20 6f 72 64 65 72 2c 20 74 68 65 6e 20  put order, then 
30073 74 68 65 20 2a 70 70 4f 72 64 65 72 42 79 20 69  the *ppOrderBy i
30074 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a  s unchanged..*/.
30075 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 57  SQLITE_PRIVATE W
30076 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74 65  hereInfo *sqlite
30077 33 57 68 65 72 65 42 65 67 69 6e 28 0a 20 20 50  3WhereBegin(.  P
30078 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
30079 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
3007a 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
3007b 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
3007c 74 2c 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20  t,    /* A list 
3007d 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 74 6f  of all tables to
3007e 20 62 65 20 73 63 61 6e 6e 65 64 20 2a 2f 0a 20   be scanned */. 
3007f 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20   Expr *pWhere,  
30080 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48         /* The WH
30081 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
30082 45 78 70 72 4c 69 73 74 20 2a 2a 70 70 4f 72 64  ExprList **ppOrd
30083 65 72 42 79 2c 20 2f 2a 20 41 6e 20 4f 52 44 45  erBy, /* An ORDE
30084 52 20 42 59 20 63 6c 61 75 73 65 2c 20 6f 72 20  R BY clause, or 
30085 4e 55 4c 4c 20 2a 2f 0a 20 20 75 31 36 20 77 63  NULL */.  u16 wc
30086 74 72 6c 46 6c 61 67 73 20 20 20 20 20 20 20 20  trlFlags        
30087 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 57 48  /* One of the WH
30088 45 52 45 5f 2a 20 66 6c 61 67 73 20 64 65 66 69  ERE_* flags defi
30089 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74  ned in sqliteInt
3008a 2e 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  .h */.){.  int i
3008b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3008c 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
3008d 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  unter */.  int n
3008e 42 79 74 65 57 49 6e 66 6f 3b 20 20 20 20 20 20  ByteWInfo;      
3008f 20 20 20 20 20 20 2f 2a 20 4e 75 6d 2e 20 62 79        /* Num. by
30090 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  tes allocated fo
30091 72 20 57 68 65 72 65 49 6e 66 6f 20 73 74 72 75  r WhereInfo stru
30092 63 74 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66  ct */.  WhereInf
30093 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20 20  o *pWInfo;      
30094 20 20 20 2f 2a 20 57 69 6c 6c 20 62 65 63 6f 6d     /* Will becom
30095 65 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  e the return val
30096 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74  ue of this funct
30097 69 6f 6e 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76  ion */.  Vdbe *v
30098 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
30099 3b 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75  ;   /* The virtu
3009a 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69  al database engi
3009b 6e 65 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20  ne */.  Bitmask 
3009c 6e 6f 74 52 65 61 64 79 3b 20 20 20 20 20 20 20  notReady;       
3009d 20 20 20 2f 2a 20 43 75 72 73 6f 72 73 20 74 68     /* Cursors th
3009e 61 74 20 61 72 65 20 6e 6f 74 20 79 65 74 20 70  at are not yet p
3009f 6f 73 69 74 69 6f 6e 65 64 20 2a 2f 0a 20 20 57  ositioned */.  W
300a0 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61  hereMaskSet *pMa
300a1 73 6b 53 65 74 3b 20 20 20 20 2f 2a 20 54 68 65  skSet;    /* The
300a2 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 73 6b   expression mask
300a3 20 73 65 74 20 2a 2f 0a 20 20 57 68 65 72 65 43   set */.  WhereC
300a4 6c 61 75 73 65 20 2a 70 57 43 3b 20 20 20 20 20  lause *pWC;     
300a5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 63            /* Dec
300a6 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68  omposition of th
300a7 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
300a8 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  /.  struct SrcLi
300a9 73 74 5f 69 74 65 6d 20 2a 70 54 61 62 49 74 65  st_item *pTabIte
300aa 6d 3b 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20  m;  /* A single 
300ab 65 6e 74 72 79 20 66 72 6f 6d 20 70 54 61 62 4c  entry from pTabL
300ac 69 73 74 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65  ist */.  WhereLe
300ad 76 65 6c 20 2a 70 4c 65 76 65 6c 3b 20 20 20 20  vel *pLevel;    
300ae 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69           /* A si
300af 6e 67 6c 65 20 6c 65 76 65 6c 20 69 6e 20 74 68  ngle level in th
300b0 65 20 70 57 49 6e 66 6f 20 6c 69 73 74 20 2a 2f  e pWInfo list */
300b1 0a 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20  .  int iFrom;   
300b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
300b3 20 20 20 2f 2a 20 46 69 72 73 74 20 75 6e 75 73     /* First unus
300b4 65 64 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 65  ed FROM clause e
300b5 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  lement */.  int 
300b6 61 6e 64 46 6c 61 67 73 3b 20 20 20 20 20 20 20  andFlags;       
300b7 20 20 20 20 20 20 20 2f 2a 20 41 4e 44 2d 65 64         /* AND-ed
300b8 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20   combination of 
300b9 61 6c 6c 20 70 57 43 2d 3e 61 5b 5d 2e 77 74 46  all pWC->a[].wtF
300ba 6c 61 67 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65  lags */.  sqlite
300bb 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20  3 *db;          
300bc 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
300bd 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a   connection */..
300be 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20    /* The number 
300bf 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  of tables in the
300c0 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20   FROM clause is 
300c1 6c 69 6d 69 74 65 64 20 62 79 20 74 68 65 20 6e  limited by the n
300c2 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20 62 69  umber of.  ** bi
300c3 74 73 20 69 6e 20 61 20 42 69 74 6d 61 73 6b 20  ts in a Bitmask 
300c4 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62  .  */.  if( pTab
300c5 4c 69 73 74 2d 3e 6e 53 72 63 3e 42 4d 53 20 29  List->nSrc>BMS )
300c6 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
300c7 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61  orMsg(pParse, "a
300c8 74 20 6d 6f 73 74 20 25 64 20 74 61 62 6c 65 73  t most %d tables
300c9 20 69 6e 20 61 20 6a 6f 69 6e 22 2c 20 42 4d 53   in a join", BMS
300ca 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
300cb 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  .  }..  /* Alloc
300cc 61 74 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  ate and initiali
300cd 7a 65 20 74 68 65 20 57 68 65 72 65 49 6e 66 6f  ze the WhereInfo
300ce 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20   structure that 
300cf 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 0a  will become the.
300d0 20 20 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75    ** return valu
300d1 65 2e 20 41 20 73 69 6e 67 6c 65 20 61 6c 6c 6f  e. A single allo
300d2 63 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  cation is used t
300d3 6f 20 73 74 6f 72 65 20 74 68 65 20 57 68 65 72  o store the Wher
300d4 65 49 6e 66 6f 0a 20 20 2a 2a 20 73 74 72 75 63  eInfo.  ** struc
300d5 74 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  t, the contents 
300d6 6f 66 20 57 68 65 72 65 49 6e 66 6f 2e 61 5b 5d  of WhereInfo.a[]
300d7 2c 20 74 68 65 20 57 68 65 72 65 43 6c 61 75 73  , the WhereClaus
300d8 65 20 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2a  e structure.  **
300d9 20 61 6e 64 20 74 68 65 20 57 68 65 72 65 4d 61   and the WhereMa
300da 73 6b 53 65 74 20 73 74 72 75 63 74 75 72 65 2e  skSet structure.
300db 20 53 69 6e 63 65 20 57 68 65 72 65 43 6c 61 75   Since WhereClau
300dc 73 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 38  se contains an 8
300dd 2d 62 79 74 65 0a 20 20 2a 2a 20 66 69 65 6c 64  -byte.  ** field
300de 20 28 74 79 70 65 20 42 69 74 6d 61 73 6b 29 20   (type Bitmask) 
300df 69 74 20 6d 75 73 74 20 62 65 20 61 6c 69 67 6e  it must be align
300e0 65 64 20 6f 6e 20 61 6e 20 38 2d 62 79 74 65 20  ed on an 8-byte 
300e1 62 6f 75 6e 64 61 72 79 20 6f 6e 0a 20 20 2a 2a  boundary on.  **
300e2 20 73 6f 6d 65 20 61 72 63 68 69 74 65 63 74 75   some architectu
300e3 72 65 73 2e 20 48 65 6e 63 65 20 74 68 65 20 52  res. Hence the R
300e4 4f 55 4e 44 38 28 29 20 62 65 6c 6f 77 2e 0a 20  OUND8() below.. 
300e5 20 2a 2f 0a 20 20 64 62 20 3d 20 70 50 61 72 73   */.  db = pPars
300e6 65 2d 3e 64 62 3b 0a 20 20 6e 42 79 74 65 57 49  e->db;.  nByteWI
300e7 6e 66 6f 20 3d 20 52 4f 55 4e 44 38 28 73 69 7a  nfo = ROUND8(siz
300e8 65 6f 66 28 57 68 65 72 65 49 6e 66 6f 29 2b 28  eof(WhereInfo)+(
300e9 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 2d 31  pTabList->nSrc-1
300ea 29 2a 73 69 7a 65 6f 66 28 57 68 65 72 65 4c 65  )*sizeof(WhereLe
300eb 76 65 6c 29 29 3b 0a 20 20 70 57 49 6e 66 6f 20  vel));.  pWInfo 
300ec 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
300ed 63 5a 65 72 6f 28 64 62 2c 20 0a 20 20 20 20 20  cZero(db, .     
300ee 20 6e 42 79 74 65 57 49 6e 66 6f 20 2b 20 0a 20   nByteWInfo + . 
300ef 20 20 20 20 20 73 69 7a 65 6f 66 28 57 68 65 72       sizeof(Wher
300f0 65 43 6c 61 75 73 65 29 20 2b 0a 20 20 20 20 20  eClause) +.     
300f1 20 73 69 7a 65 6f 66 28 57 68 65 72 65 4d 61 73   sizeof(WhereMas
300f2 6b 53 65 74 29 0a 20 20 29 3b 0a 20 20 69 66 28  kSet).  );.  if(
300f3 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
300f4 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 77 68  d ){.    goto wh
300f5 65 72 65 42 65 67 69 6e 45 72 72 6f 72 3b 0a 20  ereBeginError;. 
300f6 20 7d 0a 20 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65   }.  pWInfo->nLe
300f7 76 65 6c 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e  vel = pTabList->
300f8 6e 53 72 63 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e  nSrc;.  pWInfo->
300f9 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
300fa 0a 20 20 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c  .  pWInfo->pTabL
300fb 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a  ist = pTabList;.
300fc 20 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b    pWInfo->iBreak
300fd 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
300fe 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 70 57  keLabel(v);.  pW
300ff 49 6e 66 6f 2d 3e 70 57 43 20 3d 20 70 57 43 20  Info->pWC = pWC 
30100 3d 20 28 57 68 65 72 65 43 6c 61 75 73 65 20 2a  = (WhereClause *
30101 29 26 28 28 75 38 20 2a 29 70 57 49 6e 66 6f 29  )&((u8 *)pWInfo)
30102 5b 6e 42 79 74 65 57 49 6e 66 6f 5d 3b 0a 20 20  [nByteWInfo];.  
30103 70 57 49 6e 66 6f 2d 3e 77 63 74 72 6c 46 6c 61  pWInfo->wctrlFla
30104 67 73 20 3d 20 77 63 74 72 6c 46 6c 61 67 73 3b  gs = wctrlFlags;
30105 0a 20 20 70 4d 61 73 6b 53 65 74 20 3d 20 28 57  .  pMaskSet = (W
30106 68 65 72 65 4d 61 73 6b 53 65 74 2a 29 26 70 57  hereMaskSet*)&pW
30107 43 5b 31 5d 3b 0a 0a 20 20 2f 2a 20 53 70 6c 69  C[1];..  /* Spli
30108 74 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  t the WHERE clau
30109 73 65 20 69 6e 74 6f 20 73 65 70 61 72 61 74 65  se into separate
3010a 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20   subexpressions 
3010b 77 68 65 72 65 20 65 61 63 68 0a 20 20 2a 2a 20  where each.  ** 
3010c 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 69 73  subexpression is
3010d 20 73 65 70 61 72 61 74 65 64 20 62 79 20 61 6e   separated by an
3010e 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 0a 20   AND operator.. 
3010f 20 2a 2f 0a 20 20 69 6e 69 74 4d 61 73 6b 53 65   */.  initMaskSe
30110 74 28 70 4d 61 73 6b 53 65 74 29 3b 0a 20 20 77  t(pMaskSet);.  w
30111 68 65 72 65 43 6c 61 75 73 65 49 6e 69 74 28 70  hereClauseInit(p
30112 57 43 2c 20 70 50 61 72 73 65 2c 20 70 4d 61 73  WC, pParse, pMas
30113 6b 53 65 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  kSet);.  sqlite3
30114 45 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74  ExprCodeConstant
30115 73 28 70 50 61 72 73 65 2c 20 70 57 68 65 72 65  s(pParse, pWhere
30116 29 3b 0a 20 20 77 68 65 72 65 53 70 6c 69 74 28  );.  whereSplit(
30117 70 57 43 2c 20 70 57 68 65 72 65 2c 20 54 4b 5f  pWC, pWhere, TK_
30118 41 4e 44 29 3b 0a 20 20 20 20 0a 20 20 2f 2a 20  AND);.    .  /* 
30119 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 61 20  Special case: a 
3011a 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 68 61  WHERE clause tha
3011b 74 20 69 73 20 63 6f 6e 73 74 61 6e 74 2e 20 20  t is constant.  
3011c 45 76 61 6c 75 61 74 65 20 74 68 65 0a 20 20 2a  Evaluate the.  *
3011d 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64  * expression and
3011e 20 65 69 74 68 65 72 20 6a 75 6d 70 20 6f 76 65   either jump ove
3011f 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 63 6f 64  r all of the cod
30120 65 20 6f 72 20 66 61 6c 6c 20 74 68 72 75 2e 0a  e or fall thru..
30121 20 20 2a 2f 0a 20 20 69 66 28 20 70 57 68 65 72    */.  if( pWher
30122 65 20 26 26 20 28 70 54 61 62 4c 69 73 74 2d 3e  e && (pTabList->
30123 6e 53 72 63 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  nSrc==0 || sqlit
30124 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
30125 4e 6f 74 4a 6f 69 6e 28 70 57 68 65 72 65 29 29  NotJoin(pWhere))
30126 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
30127 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73  xprIfFalse(pPars
30128 65 2c 20 70 57 68 65 72 65 2c 20 70 57 49 6e 66  e, pWhere, pWInf
30129 6f 2d 3e 69 42 72 65 61 6b 2c 20 53 51 4c 49 54  o->iBreak, SQLIT
3012a 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20  E_JUMPIFNULL);. 
3012b 20 20 20 70 57 68 65 72 65 20 3d 20 30 3b 0a 20     pWhere = 0;. 
3012c 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 69 67 6e 20   }..  /* Assign 
3012d 61 20 62 69 74 20 66 72 6f 6d 20 74 68 65 20 62  a bit from the b
3012e 69 74 6d 61 73 6b 20 74 6f 20 65 76 65 72 79 20  itmask to every 
3012f 74 65 72 6d 20 69 6e 20 74 68 65 20 46 52 4f 4d  term in the FROM
30130 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 0a 20 20   clause..  **.  
30131 2a 2a 20 57 68 65 6e 20 61 73 73 69 67 6e 69 6e  ** When assignin
30132 67 20 62 69 74 6d 61 73 6b 20 76 61 6c 75 65 73  g bitmask values
30133 20 74 6f 20 46 52 4f 4d 20 63 6c 61 75 73 65 20   to FROM clause 
30134 63 75 72 73 6f 72 73 2c 20 69 74 20 6d 75 73 74  cursors, it must
30135 20 62 65 0a 20 20 2a 2a 20 74 68 65 20 63 61 73   be.  ** the cas
30136 65 20 74 68 61 74 20 69 66 20 58 20 69 73 20 74  e that if X is t
30137 68 65 20 62 69 74 6d 61 73 6b 20 66 6f 72 20 74  he bitmask for t
30138 68 65 20 4e 2d 74 68 20 46 52 4f 4d 20 63 6c 61  he N-th FROM cla
30139 75 73 65 20 74 65 72 6d 20 74 68 65 6e 0a 20 20  use term then.  
3013a 2a 2a 20 74 68 65 20 62 69 74 6d 61 73 6b 20 66  ** the bitmask f
3013b 6f 72 20 61 6c 6c 20 46 52 4f 4d 20 63 6c 61 75  or all FROM clau
3013c 73 65 20 74 65 72 6d 73 20 74 6f 20 74 68 65 20  se terms to the 
3013d 6c 65 66 74 20 6f 66 20 74 68 65 20 4e 2d 74 68  left of the N-th
3013e 20 74 65 72 6d 0a 20 20 2a 2a 20 69 73 20 28 58   term.  ** is (X
3013f 2d 31 29 2e 20 20 20 41 6e 20 65 78 70 72 65 73  -1).   An expres
30140 73 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 4f 4e  sion from the ON
30141 20 63 6c 61 75 73 65 20 6f 66 20 61 20 4c 45 46   clause of a LEF
30142 54 20 4a 4f 49 4e 20 63 61 6e 20 75 73 65 0a 20  T JOIN can use. 
30143 20 2a 2a 20 69 74 73 20 45 78 70 72 2e 69 52 69   ** its Expr.iRi
30144 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 76 61 6c  ghtJoinTable val
30145 75 65 20 74 6f 20 66 69 6e 64 20 74 68 65 20 62  ue to find the b
30146 69 74 6d 61 73 6b 20 6f 66 20 74 68 65 20 72 69  itmask of the ri
30147 67 68 74 20 74 61 62 6c 65 0a 20 20 2a 2a 20 6f  ght table.  ** o
30148 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20 53 75 62  f the join.  Sub
30149 74 72 61 63 74 69 6e 67 20 6f 6e 65 20 66 72 6f  tracting one fro
3014a 6d 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c  m the right tabl
3014b 65 20 62 69 74 6d 61 73 6b 20 67 69 76 65 73 20  e bitmask gives 
3014c 61 0a 20 20 2a 2a 20 62 69 74 6d 61 73 6b 20 66  a.  ** bitmask f
3014d 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73 20 74 6f  or all tables to
3014e 20 74 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65   the left of the
3014f 20 6a 6f 69 6e 2e 20 20 4b 6e 6f 77 69 6e 67 20   join.  Knowing 
30150 74 68 65 20 62 69 74 6d 61 73 6b 0a 20 20 2a 2a  the bitmask.  **
30151 20 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73 20   for all tables 
30152 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 61  to the left of a
30153 20 6c 65 66 74 20 6a 6f 69 6e 20 69 73 20 69 6d   left join is im
30154 70 6f 72 74 61 6e 74 2e 20 20 54 69 63 6b 65 74  portant.  Ticket
30155 20 23 33 30 31 35 2e 0a 20 20 2a 2a 0a 20 20 2a   #3015..  **.  *
30156 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20  * Configure the 
30157 57 68 65 72 65 43 6c 61 75 73 65 2e 76 6d 61 73  WhereClause.vmas
30158 6b 20 76 61 72 69 61 62 6c 65 20 73 6f 20 74 68  k variable so th
30159 61 74 20 62 69 74 73 20 74 68 61 74 20 63 6f 72  at bits that cor
3015a 72 65 73 70 6f 6e 64 0a 20 20 2a 2a 20 74 6f 20  respond.  ** to 
3015b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75  virtual table cu
3015c 72 73 6f 72 73 20 61 72 65 20 73 65 74 2e 20 54  rsors are set. T
3015d 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 73  his is used to s
3015e 65 6c 65 63 74 69 76 65 6c 79 20 64 69 73 61 62  electively disab
3015f 6c 65 20 0a 20 20 2a 2a 20 74 68 65 20 4f 52 2d  le .  ** the OR-
30160 74 6f 2d 49 4e 20 74 72 61 6e 73 66 6f 72 6d 61  to-IN transforma
30161 74 69 6f 6e 20 69 6e 20 65 78 70 72 41 6e 61 6c  tion in exprAnal
30162 79 7a 65 4f 72 54 65 72 6d 28 29 2e 20 49 74 20  yzeOrTerm(). It 
30163 69 73 20 6e 6f 74 20 68 65 6c 70 66 75 6c 20 0a  is not helpful .
30164 20 20 2a 2a 20 77 69 74 68 20 76 69 72 74 75 61    ** with virtua
30165 6c 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20  l tables..  */. 
30166 20 61 73 73 65 72 74 28 20 70 57 43 2d 3e 76 6d   assert( pWC->vm
30167 61 73 6b 3d 3d 30 20 26 26 20 70 4d 61 73 6b 53  ask==0 && pMaskS
30168 65 74 2d 3e 6e 3d 3d 30 20 29 3b 0a 20 20 66 6f  et->n==0 );.  fo
30169 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c 69 73  r(i=0; i<pTabLis
3016a 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20  t->nSrc; i++){. 
3016b 20 20 20 63 72 65 61 74 65 4d 61 73 6b 28 70 4d     createMask(pM
3016c 61 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73 74  askSet, pTabList
3016d 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 29 3b  ->a[i].iCursor);
3016e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
3016f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
30170 45 0a 20 20 20 20 69 66 28 20 41 4c 57 41 59 53  E.    if( ALWAYS
30171 28 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e  (pTabList->a[i].
30172 70 54 61 62 29 20 26 26 20 49 73 56 69 72 74 75  pTab) && IsVirtu
30173 61 6c 28 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69  al(pTabList->a[i
30174 5d 2e 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20  ].pTab) ){.     
30175 20 70 57 43 2d 3e 76 6d 61 73 6b 20 7c 3d 20 28   pWC->vmask |= (
30176 28 42 69 74 6d 61 73 6b 29 31 20 3c 3c 20 69 29  (Bitmask)1 << i)
30177 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
30178 20 7d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55   }.#ifndef NDEBU
30179 47 0a 20 20 7b 0a 20 20 20 20 42 69 74 6d 61 73  G.  {.    Bitmas
3017a 6b 20 74 6f 54 68 65 4c 65 66 74 20 3d 20 30 3b  k toTheLeft = 0;
3017b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
3017c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20  pTabList->nSrc; 
3017d 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 69 74 6d  i++){.      Bitm
3017e 61 73 6b 20 6d 20 3d 20 67 65 74 4d 61 73 6b 28  ask m = getMask(
3017f 70 4d 61 73 6b 53 65 74 2c 20 70 54 61 62 4c 69  pMaskSet, pTabLi
30180 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72  st->a[i].iCursor
30181 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
30182 20 28 6d 2d 31 29 3d 3d 74 6f 54 68 65 4c 65 66   (m-1)==toTheLef
30183 74 20 29 3b 0a 20 20 20 20 20 20 74 6f 54 68 65  t );.      toThe
30184 4c 65 66 74 20 7c 3d 20 6d 3b 0a 20 20 20 20 7d  Left |= m;.    }
30185 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
30186 2a 20 41 6e 61 6c 79 7a 65 20 61 6c 6c 20 6f 66  * Analyze all of
30187 20 74 68 65 20 73 75 62 65 78 70 72 65 73 73 69   the subexpressi
30188 6f 6e 73 2e 20 20 4e 6f 74 65 20 74 68 61 74 20  ons.  Note that 
30189 65 78 70 72 41 6e 61 6c 79 7a 65 28 29 20 6d 69  exprAnalyze() mi
3018a 67 68 74 0a 20 20 2a 2a 20 61 64 64 20 6e 65 77  ght.  ** add new
3018b 20 76 69 72 74 75 61 6c 20 74 65 72 6d 73 20 6f   virtual terms o
3018c 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74  nto the end of t
3018d 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  he WHERE clause.
3018e 20 20 57 65 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a    We do not.  **
3018f 20 77 61 6e 74 20 74 6f 20 61 6e 61 6c 79 7a 65   want to analyze
30190 20 74 68 65 73 65 20 76 69 72 74 75 61 6c 20 74   these virtual t
30191 65 72 6d 73 2c 20 73 6f 20 73 74 61 72 74 20 61  erms, so start a
30192 6e 61 6c 79 7a 69 6e 67 20 61 74 20 74 68 65 20  nalyzing at the 
30193 65 6e 64 0a 20 20 2a 2a 20 61 6e 64 20 77 6f 72  end.  ** and wor
30194 6b 20 66 6f 72 77 61 72 64 20 73 6f 20 74 68 61  k forward so tha
30195 74 20 74 68 65 20 61 64 64 65 64 20 76 69 72 74  t the added virt
30196 75 61 6c 20 74 65 72 6d 73 20 61 72 65 20 6e 65  ual terms are ne
30197 76 65 72 20 70 72 6f 63 65 73 73 65 64 2e 0a 20  ver processed.. 
30198 20 2a 2f 0a 20 20 65 78 70 72 41 6e 61 6c 79 7a   */.  exprAnalyz
30199 65 41 6c 6c 28 70 54 61 62 4c 69 73 74 2c 20 70  eAll(pTabList, p
3019a 57 43 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d  WC);.  if( db->m
3019b 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
3019c 20 20 20 67 6f 74 6f 20 77 68 65 72 65 42 65 67     goto whereBeg
3019d 69 6e 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20  inError;.  }..  
3019e 2f 2a 20 43 68 6f 73 65 20 74 68 65 20 62 65 73  /* Chose the bes
3019f 74 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20 66  t index to use f
301a0 6f 72 20 65 61 63 68 20 74 61 62 6c 65 20 69 6e  or each table in
301a1 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
301a2 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73  ..  **.  ** This
301a3 20 6c 6f 6f 70 20 66 69 6c 6c 73 20 69 6e 20 74   loop fills in t
301a4 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65  he following fie
301a5 6c 64 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  lds:.  **.  **  
301a6 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 70 49 64   pWInfo->a[].pId
301a7 78 20 20 20 20 20 20 54 68 65 20 69 6e 64 65 78  x      The index
301a8 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 69 73   to use for this
301a9 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 6c 6f   level of the lo
301aa 6f 70 2e 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66  op..  **   pWInf
301ab 6f 2d 3e 61 5b 5d 2e 77 73 46 6c 61 67 73 20 20  o->a[].wsFlags  
301ac 20 57 48 45 52 45 5f 78 78 78 20 66 6c 61 67 73   WHERE_xxx flags
301ad 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
301ae 20 70 49 64 78 0a 20 20 2a 2a 20 20 20 70 57 49   pIdx.  **   pWI
301af 6e 66 6f 2d 3e 61 5b 5d 2e 6e 45 71 20 20 20 20  nfo->a[].nEq    
301b0 20 20 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66     The number of
301b1 20 3d 3d 20 61 6e 64 20 49 4e 20 63 6f 6e 73 74   == and IN const
301b2 72 61 69 6e 74 73 0a 20 20 2a 2a 20 20 20 70 57  raints.  **   pW
301b3 49 6e 66 6f 2d 3e 61 5b 5d 2e 69 46 72 6f 6d 20  Info->a[].iFrom 
301b4 20 20 20 20 57 68 69 63 68 20 74 65 72 6d 20 6f      Which term o
301b5 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  f the FROM claus
301b6 65 20 69 73 20 62 65 69 6e 67 20 63 6f 64 65 64  e is being coded
301b7 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e  .  **   pWInfo->
301b8 61 5b 5d 2e 69 54 61 62 43 75 72 20 20 20 54 68  a[].iTabCur   Th
301b9 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f  e VDBE cursor fo
301ba 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  r the database t
301bb 61 62 6c 65 0a 20 20 2a 2a 20 20 20 70 57 49 6e  able.  **   pWIn
301bc 66 6f 2d 3e 61 5b 5d 2e 69 49 64 78 43 75 72 20  fo->a[].iIdxCur 
301bd 20 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f    The VDBE curso
301be 72 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 0a  r for the index.
301bf 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61    **   pWInfo->a
301c0 5b 5d 2e 70 54 65 72 6d 20 20 20 20 20 57 68 65  [].pTerm     Whe
301c1 6e 20 77 73 46 6c 61 67 73 3d 3d 57 4f 5f 4f 52  n wsFlags==WO_OR
301c2 2c 20 74 68 65 20 4f 52 2d 63 6c 61 75 73 65 20  , the OR-clause 
301c3 74 65 72 6d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  term.  **.  ** T
301c4 68 69 73 20 6c 6f 6f 70 20 61 6c 73 6f 20 66 69  his loop also fi
301c5 67 75 72 65 73 20 6f 75 74 20 74 68 65 20 6e 65  gures out the ne
301c6 73 74 69 6e 67 20 6f 72 64 65 72 20 6f 66 20 74  sting order of t
301c7 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46 52 4f  ables in the FRO
301c8 4d 0a 20 20 2a 2a 20 63 6c 61 75 73 65 2e 0a 20  M.  ** clause.. 
301c9 20 2a 2f 0a 20 20 6e 6f 74 52 65 61 64 79 20 3d   */.  notReady =
301ca 20 7e 28 42 69 74 6d 61 73 6b 29 30 3b 0a 20 20   ~(Bitmask)0;.  
301cb 70 54 61 62 49 74 65 6d 20 3d 20 70 54 61 62 4c  pTabItem = pTabL
301cc 69 73 74 2d 3e 61 3b 0a 20 20 70 4c 65 76 65 6c  ist->a;.  pLevel
301cd 20 3d 20 70 57 49 6e 66 6f 2d 3e 61 3b 0a 20 20   = pWInfo->a;.  
301ce 61 6e 64 46 6c 61 67 73 20 3d 20 7e 30 3b 0a 20  andFlags = ~0;. 
301cf 20 57 48 45 52 45 54 52 41 43 45 28 28 22 2a 2a   WHERETRACE(("**
301d0 2a 20 4f 70 74 69 6d 69 7a 65 72 20 53 74 61 72  * Optimizer Star
301d1 74 20 2a 2a 2a 5c 6e 22 29 29 3b 0a 20 20 66 6f  t ***\n"));.  fo
301d2 72 28 69 3d 69 46 72 6f 6d 3d 30 2c 20 70 4c 65  r(i=iFrom=0, pLe
301d3 76 65 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20 69  vel=pWInfo->a; i
301d4 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b  <pTabList->nSrc;
301d5 20 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b   i++, pLevel++){
301d6 0a 20 20 20 20 57 68 65 72 65 43 6f 73 74 20 62  .    WhereCost b
301d7 65 73 74 50 6c 61 6e 3b 20 20 20 20 20 20 20 20  estPlan;        
301d8 20 2f 2a 20 4d 6f 73 74 20 65 66 66 69 63 69 65   /* Most efficie
301d9 6e 74 20 70 6c 61 6e 20 73 65 65 6e 20 73 6f 20  nt plan seen so 
301da 66 61 72 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78  far */.    Index
301db 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20   *pIdx;         
301dc 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
301dd 66 6f 72 20 46 52 4f 4d 20 74 61 62 6c 65 20 61  for FROM table a
301de 74 20 70 54 61 62 49 74 65 6d 20 2a 2f 0a 20 20  t pTabItem */.  
301df 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20    int j;        
301e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
301e1 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
301e2 72 20 46 52 4f 4d 20 74 61 62 6c 65 73 20 2a 2f  r FROM tables */
301e3 0a 20 20 20 20 69 6e 74 20 62 65 73 74 4a 20 3d  .    int bestJ =
301e4 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20 20 20   -1;            
301e5 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66   /* The value of
301e6 20 6a 20 2a 2f 0a 20 20 20 20 42 69 74 6d 61 73   j */.    Bitmas
301e7 6b 20 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  k m;            
301e8 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b        /* Bitmask
301e9 20 76 61 6c 75 65 20 66 6f 72 20 6a 20 6f 72 20   value for j or 
301ea 62 65 73 74 4a 20 2a 2f 0a 20 20 20 20 69 6e 74  bestJ */.    int
301eb 20 69 73 4f 70 74 69 6d 61 6c 3b 20 20 20 20 20   isOptimal;     
301ec 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
301ed 61 74 6f 72 20 66 6f 72 20 6f 70 74 69 6d 61 6c  ator for optimal
301ee 2f 6e 6f 6e 2d 6f 70 74 69 6d 61 6c 20 73 65 61  /non-optimal sea
301ef 72 63 68 20 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73  rch */..    mems
301f0 65 74 28 26 62 65 73 74 50 6c 61 6e 2c 20 30 2c  et(&bestPlan, 0,
301f1 20 73 69 7a 65 6f 66 28 62 65 73 74 50 6c 61 6e   sizeof(bestPlan
301f2 29 29 3b 0a 20 20 20 20 62 65 73 74 50 6c 61 6e  ));.    bestPlan
301f3 2e 72 43 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f  .rCost = SQLITE_
301f4 42 49 47 5f 44 42 4c 3b 0a 0a 20 20 20 20 2f 2a  BIG_DBL;..    /*
301f5 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68   Loop through th
301f6 65 20 72 65 6d 61 69 6e 69 6e 67 20 65 6e 74 72  e remaining entr
301f7 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ies in the FROM 
301f8 63 6c 61 75 73 65 20 74 6f 20 66 69 6e 64 20 74  clause to find t
301f9 68 65 0a 20 20 20 20 2a 2a 20 6e 65 78 74 20 6e  he.    ** next n
301fa 65 73 74 65 64 20 6c 6f 6f 70 2e 20 54 68 65 20  ested loop. The 
301fb 46 52 4f 4d 20 63 6c 61 75 73 65 20 65 6e 74 72  FROM clause entr
301fc 69 65 73 20 6d 61 79 20 62 65 20 69 74 65 72 61  ies may be itera
301fd 74 65 64 20 74 68 72 6f 75 67 68 0a 20 20 20 20  ted through.    
301fe 2a 2a 20 65 69 74 68 65 72 20 6f 6e 63 65 20 6f  ** either once o
301ff 72 20 74 77 69 63 65 2e 20 0a 20 20 20 20 2a 2a  r twice. .    **
30200 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 69 72 73  .    ** The firs
30201 74 20 69 74 65 72 61 74 69 6f 6e 2c 20 77 68 69  t iteration, whi
30202 63 68 20 69 73 20 61 6c 77 61 79 73 20 70 65 72  ch is always per
30203 66 6f 72 6d 65 64 2c 20 73 65 61 72 63 68 65 73  formed, searches
30204 20 66 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20   for the.    ** 
30205 46 52 4f 4d 20 63 6c 61 75 73 65 20 65 6e 74 72  FROM clause entr
30206 79 20 74 68 61 74 20 70 65 72 6d 69 74 73 20 74  y that permits t
30207 68 65 20 6c 6f 77 65 73 74 2d 63 6f 73 74 2c 20  he lowest-cost, 
30208 22 6f 70 74 69 6d 61 6c 22 20 73 63 61 6e 2e 20  "optimal" scan. 
30209 49 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 63  In.    ** this c
3020a 6f 6e 74 65 78 74 20 61 6e 20 6f 70 74 69 6d 61  ontext an optima
3020b 6c 20 73 63 61 6e 20 69 73 20 6f 6e 65 20 74 68  l scan is one th
3020c 61 74 20 75 73 65 73 20 74 68 65 20 73 61 6d 65  at uses the same
3020d 20 73 74 72 61 74 65 67 79 0a 20 20 20 20 2a 2a   strategy.    **
3020e 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 46   for the given F
3020f 52 4f 4d 20 63 6c 61 75 73 65 20 65 6e 74 72 79  ROM clause entry
30210 20 61 73 20 77 6f 75 6c 64 20 62 65 20 73 65 6c   as would be sel
30211 65 63 74 65 64 20 69 66 20 74 68 65 20 65 6e 74  ected if the ent
30212 72 79 0a 20 20 20 20 2a 2a 20 77 65 72 65 20 75  ry.    ** were u
30213 73 65 64 20 61 73 20 74 68 65 20 69 6e 6e 65 72  sed as the inner
30214 6d 6f 73 74 20 6e 65 73 74 65 64 20 6c 6f 6f 70  most nested loop
30215 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
30216 73 2c 20 61 20 74 61 62 6c 65 0a 20 20 20 20 2a  s, a table.    *
30217 2a 20 69 73 20 63 68 6f 73 65 6e 20 73 75 63 68  * is chosen such
30218 20 74 68 61 74 20 74 68 65 20 63 6f 73 74 20 6f   that the cost o
30219 66 20 72 75 6e 6e 69 6e 67 20 74 68 61 74 20 74  f running that t
3021a 61 62 6c 65 20 63 61 6e 6e 6f 74 20 62 65 20 72  able cannot be r
3021b 65 64 75 63 65 64 0a 20 20 20 20 2a 2a 20 62 79  educed.    ** by
3021c 20 77 61 69 74 69 6e 67 20 66 6f 72 20 6f 74 68   waiting for oth
3021d 65 72 20 74 61 62 6c 65 73 20 74 6f 20 72 75 6e  er tables to run
3021e 20 66 69 72 73 74 2e 0a 20 20 20 20 2a 2a 0a 20   first..    **. 
3021f 20 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64     ** The second
30220 20 69 74 65 72 61 74 69 6f 6e 20 69 73 20 6f 6e   iteration is on
30221 6c 79 20 70 65 72 66 6f 72 6d 65 64 20 69 66 20  ly performed if 
30222 6e 6f 20 6f 70 74 69 6d 61 6c 20 73 63 61 6e 20  no optimal scan 
30223 73 74 72 61 74 65 67 69 65 73 0a 20 20 20 20 2a  strategies.    *
30224 2a 20 77 65 72 65 20 66 6f 75 6e 64 20 62 79 20  * were found by 
30225 74 68 65 20 66 69 72 73 74 2e 20 54 68 69 73 20  the first. This 
30226 69 74 65 72 61 74 69 6f 6e 20 69 73 20 75 73 65  iteration is use
30227 64 20 74 6f 20 73 65 61 72 63 68 20 66 6f 72 20  d to search for 
30228 74 68 65 0a 20 20 20 20 2a 2a 20 6c 6f 77 65 73  the.    ** lowes
30229 74 20 63 6f 73 74 20 73 63 61 6e 20 6f 76 65 72  t cost scan over
3022a 61 6c 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  all..    **.    
3022b 2a 2a 20 50 72 65 76 69 6f 75 73 20 76 65 72 73  ** Previous vers
3022c 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 70  ions of SQLite p
3022d 65 72 66 6f 72 6d 65 64 20 6f 6e 6c 79 20 74 68  erformed only th
3022e 65 20 73 65 63 6f 6e 64 20 69 74 65 72 61 74 69  e second iterati
3022f 6f 6e 20 2d 0a 20 20 20 20 2a 2a 20 74 68 65 20  on -.    ** the 
30230 6e 65 78 74 20 6f 75 74 65 72 6d 6f 73 74 20 6c  next outermost l
30231 6f 6f 70 20 77 61 73 20 61 6c 77 61 79 73 20 74  oop was always t
30232 68 61 74 20 77 69 74 68 20 74 68 65 20 6c 6f 77  hat with the low
30233 65 73 74 20 6f 76 65 72 61 6c 6c 0a 20 20 20 20  est overall.    
30234 2a 2a 20 63 6f 73 74 2e 20 48 6f 77 65 76 65 72  ** cost. However
30235 2c 20 74 68 69 73 20 6d 65 61 6e 74 20 74 68 61  , this meant tha
30236 74 20 53 51 4c 69 74 65 20 63 6f 75 6c 64 20 73  t SQLite could s
30237 65 6c 65 63 74 20 74 68 65 20 77 72 6f 6e 67 20  elect the wrong 
30238 70 6c 61 6e 0a 20 20 20 20 2a 2a 20 66 6f 72 20  plan.    ** for 
30239 73 63 72 69 70 74 73 20 73 75 63 68 20 61 73 20  scripts such as 
3023a 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 20  the following:. 
3023b 20 20 20 2a 2a 20 20 20 0a 20 20 20 20 2a 2a 20     **   .    ** 
3023c 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74    CREATE TABLE t
3023d 31 28 61 2c 20 62 29 3b 20 0a 20 20 20 20 2a 2a  1(a, b); .    **
3023e 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
3023f 74 32 28 63 2c 20 64 29 3b 0a 20 20 20 20 2a 2a  t2(c, d);.    **
30240 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d     SELECT * FROM
30241 20 74 32 2c 20 74 31 20 57 48 45 52 45 20 74 32   t2, t1 WHERE t2
30242 2e 72 6f 77 69 64 20 3d 20 74 31 2e 61 3b 0a 20  .rowid = t1.a;. 
30243 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
30244 20 62 65 73 74 20 73 74 72 61 74 65 67 79 20 69   best strategy i
30245 73 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72  s to iterate thr
30246 6f 75 67 68 20 74 61 62 6c 65 20 74 31 20 66 69  ough table t1 fi
30247 72 73 74 2e 20 48 6f 77 65 76 65 72 20 69 74 0a  rst. However it.
30248 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 70 6f      ** is not po
30249 73 73 69 62 6c 65 20 74 6f 20 64 65 74 65 72 6d  ssible to determ
3024a 69 6e 65 20 74 68 69 73 20 77 69 74 68 20 61 20  ine this with a 
3024b 73 69 6d 70 6c 65 20 67 72 65 65 64 79 20 61 6c  simple greedy al
3024c 67 6f 72 69 74 68 6d 2e 0a 20 20 20 20 2a 2a 20  gorithm..    ** 
3024d 48 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20 74  However, since t
3024e 68 65 20 63 6f 73 74 20 6f 66 20 61 20 6c 69 6e  he cost of a lin
3024f 65 61 72 20 73 63 61 6e 20 74 68 72 6f 75 67 68  ear scan through
30250 20 74 61 62 6c 65 20 74 32 20 69 73 20 74 68 65   table t2 is the
30251 20 73 61 6d 65 20 0a 20 20 20 20 2a 2a 20 61 73   same .    ** as
30252 20 74 68 65 20 63 6f 73 74 20 6f 66 20 61 20 6c   the cost of a l
30253 69 6e 65 61 72 20 73 63 61 6e 20 74 68 72 6f 75  inear scan throu
30254 67 68 20 74 61 62 6c 65 20 74 31 2c 20 61 20 73  gh table t1, a s
30255 69 6d 70 6c 65 20 67 72 65 65 64 79 20 0a 20 20  imple greedy .  
30256 20 20 2a 2a 20 61 6c 67 6f 72 69 74 68 6d 20 6d    ** algorithm m
30257 61 79 20 63 68 6f 6f 73 65 20 74 6f 20 75 73 65  ay choose to use
30258 20 74 32 20 66 6f 72 20 74 68 65 20 6f 75 74 65   t2 for the oute
30259 72 20 6c 6f 6f 70 2c 20 77 68 69 63 68 20 69 73  r loop, which is
3025a 20 61 20 6d 75 63 68 0a 20 20 20 20 2a 2a 20 63   a much.    ** c
3025b 6f 73 74 6c 69 65 72 20 61 70 70 72 6f 61 63 68  ostlier approach
3025c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  ..    */.    for
3025d 28 69 73 4f 70 74 69 6d 61 6c 3d 31 3b 20 69 73  (isOptimal=1; is
3025e 4f 70 74 69 6d 61 6c 3e 3d 30 20 26 26 20 62 65  Optimal>=0 && be
3025f 73 74 4a 3c 30 3b 20 69 73 4f 70 74 69 6d 61 6c  stJ<0; isOptimal
30260 2d 2d 29 7b 0a 20 20 20 20 20 20 42 69 74 6d 61  --){.      Bitma
30261 73 6b 20 6d 61 73 6b 20 3d 20 28 69 73 4f 70 74  sk mask = (isOpt
30262 69 6d 61 6c 20 3f 20 30 20 3a 20 6e 6f 74 52 65  imal ? 0 : notRe
30263 61 64 79 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ady);.      asse
30264 72 74 28 20 28 70 54 61 62 4c 69 73 74 2d 3e 6e  rt( (pTabList->n
30265 53 72 63 2d 69 46 72 6f 6d 29 3e 31 20 7c 7c 20  Src-iFrom)>1 || 
30266 69 73 4f 70 74 69 6d 61 6c 20 29 3b 0a 20 20 20  isOptimal );.   
30267 20 20 20 66 6f 72 28 6a 3d 69 46 72 6f 6d 2c 20     for(j=iFrom, 
30268 70 54 61 62 49 74 65 6d 3d 26 70 54 61 62 4c 69  pTabItem=&pTabLi
30269 73 74 2d 3e 61 5b 6a 5d 3b 20 6a 3c 70 54 61 62  st->a[j]; j<pTab
3026a 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 6a 2b 2b 2c  List->nSrc; j++,
3026b 20 70 54 61 62 49 74 65 6d 2b 2b 29 7b 0a 20 20   pTabItem++){.  
3026c 20 20 20 20 20 20 69 6e 74 20 64 6f 4e 6f 74 52        int doNotR
3026d 65 6f 72 64 65 72 3b 20 20 20 20 2f 2a 20 54 72  eorder;    /* Tr
3026e 75 65 20 69 66 20 74 68 69 73 20 74 61 62 6c 65  ue if this table
3026f 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 72   should not be r
30270 65 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20 20 20  eordered */.    
30271 20 20 20 20 57 68 65 72 65 43 6f 73 74 20 73 43      WhereCost sC
30272 6f 73 74 3b 20 20 20 20 20 2f 2a 20 43 6f 73 74  ost;     /* Cost
30273 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f   information fro
30274 6d 20 62 65 73 74 5b 56 69 72 74 75 61 6c 5d 49  m best[Virtual]I
30275 6e 64 65 78 28 29 20 2a 2f 0a 20 20 20 20 20 20  ndex() */.      
30276 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64    ExprList *pOrd
30277 65 72 42 79 3b 20 20 2f 2a 20 4f 52 44 45 52 20  erBy;  /* ORDER 
30278 42 59 20 63 6c 61 75 73 65 20 66 6f 72 20 69 6e  BY clause for in
30279 64 65 78 20 74 6f 20 6f 70 74 69 6d 69 7a 65 20  dex to optimize 
3027a 2a 2f 0a 20 20 0a 20 20 20 20 20 20 20 20 64 6f  */.  .        do
3027b 4e 6f 74 52 65 6f 72 64 65 72 20 3d 20 20 28 70  NotReorder =  (p
3027c 54 61 62 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70  TabItem->jointyp
3027d 65 20 26 20 28 4a 54 5f 4c 45 46 54 7c 4a 54 5f  e & (JT_LEFT|JT_
3027e 43 52 4f 53 53 29 29 21 3d 30 3b 0a 20 20 20 20  CROSS))!=0;.    
3027f 20 20 20 20 69 66 28 20 6a 21 3d 69 46 72 6f 6d      if( j!=iFrom
30280 20 26 26 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72   && doNotReorder
30281 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
30282 20 20 6d 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d    m = getMask(pM
30283 61 73 6b 53 65 74 2c 20 70 54 61 62 49 74 65 6d  askSet, pTabItem
30284 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  ->iCursor);.    
30285 20 20 20 20 69 66 28 20 28 6d 20 26 20 6e 6f 74      if( (m & not
30286 52 65 61 64 79 29 3d 3d 30 20 29 7b 0a 20 20 20  Ready)==0 ){.   
30287 20 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 69 46         if( j==iF
30288 72 6f 6d 20 29 20 69 46 72 6f 6d 2b 2b 3b 0a 20  rom ) iFrom++;. 
30289 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75           continu
3028a 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  e;.        }.   
3028b 20 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d 20       pOrderBy = 
3028c 28 28 69 3d 3d 30 20 26 26 20 70 70 4f 72 64 65  ((i==0 && ppOrde
3028d 72 42 79 20 29 3f 2a 70 70 4f 72 64 65 72 42 79  rBy )?*ppOrderBy
3028e 3a 30 29 3b 0a 20 20 0a 20 20 20 20 20 20 20 20  :0);.  .        
3028f 61 73 73 65 72 74 28 20 70 54 61 62 49 74 65 6d  assert( pTabItem
30290 2d 3e 70 54 61 62 20 29 3b 0a 23 69 66 6e 64 65  ->pTab );.#ifnde
30291 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
30292 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20  RTUALTABLE.     
30293 20 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c     if( IsVirtual
30294 28 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 29  (pTabItem->pTab)
30295 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
30296 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
30297 20 2a 2a 70 70 20 3d 20 26 70 57 49 6e 66 6f 2d   **pp = &pWInfo-
30298 3e 61 5b 6a 5d 2e 70 49 64 78 49 6e 66 6f 3b 0a  >a[j].pIdxInfo;.
30299 20 20 20 20 20 20 20 20 20 20 62 65 73 74 56 69            bestVi
3029a 72 74 75 61 6c 49 6e 64 65 78 28 70 50 61 72 73  rtualIndex(pPars
3029b 65 2c 20 70 57 43 2c 20 70 54 61 62 49 74 65 6d  e, pWC, pTabItem
3029c 2c 20 6d 61 73 6b 2c 20 70 4f 72 64 65 72 42 79  , mask, pOrderBy
3029d 2c 20 26 73 43 6f 73 74 2c 20 70 70 29 3b 0a 20  , &sCost, pp);. 
3029e 20 20 20 20 20 20 20 7d 65 6c 73 65 20 0a 23 65         }else .#e
3029f 6e 64 69 66 0a 20 20 20 20 20 20 20 20 7b 0a 20  ndif.        {. 
302a0 20 20 20 20 20 20 20 20 20 62 65 73 74 42 74 72           bestBtr
302a1 65 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  eeIndex(pParse, 
302a2 70 57 43 2c 20 70 54 61 62 49 74 65 6d 2c 20 6d  pWC, pTabItem, m
302a3 61 73 6b 2c 20 70 4f 72 64 65 72 42 79 2c 20 26  ask, pOrderBy, &
302a4 73 43 6f 73 74 29 3b 0a 20 20 20 20 20 20 20 20  sCost);.        
302a5 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  }.        assert
302a6 28 20 69 73 4f 70 74 69 6d 61 6c 20 7c 7c 20 28  ( isOptimal || (
302a7 73 43 6f 73 74 2e 75 73 65 64 26 6e 6f 74 52 65  sCost.used&notRe
302a8 61 64 79 29 3d 3d 30 20 29 3b 0a 0a 20 20 20 20  ady)==0 );..    
302a9 20 20 20 20 69 66 28 20 28 73 43 6f 73 74 2e 75      if( (sCost.u
302aa 73 65 64 26 6e 6f 74 52 65 61 64 79 29 3d 3d 30  sed&notReady)==0
302ab 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 6a 3d  .         && (j=
302ac 3d 69 46 72 6f 6d 20 7c 7c 20 73 43 6f 73 74 2e  =iFrom || sCost.
302ad 72 43 6f 73 74 3c 62 65 73 74 50 6c 61 6e 2e 72  rCost<bestPlan.r
302ae 43 6f 73 74 29 20 0a 20 20 20 20 20 20 20 20 29  Cost) .        )
302af 7b 0a 20 20 20 20 20 20 20 20 20 20 62 65 73 74  {.          best
302b0 50 6c 61 6e 20 3d 20 73 43 6f 73 74 3b 0a 20 20  Plan = sCost;.  
302b1 20 20 20 20 20 20 20 20 62 65 73 74 4a 20 3d 20          bestJ = 
302b2 6a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  j;.        }.   
302b3 20 20 20 20 20 69 66 28 20 64 6f 4e 6f 74 52 65       if( doNotRe
302b4 6f 72 64 65 72 20 29 20 62 72 65 61 6b 3b 0a 20  order ) break;. 
302b5 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
302b6 20 61 73 73 65 72 74 28 20 62 65 73 74 4a 3e 3d   assert( bestJ>=
302b7 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
302b8 20 6e 6f 74 52 65 61 64 79 20 26 20 67 65 74 4d   notReady & getM
302b9 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 54  ask(pMaskSet, pT
302ba 61 62 4c 69 73 74 2d 3e 61 5b 62 65 73 74 4a 5d  abList->a[bestJ]
302bb 2e 69 43 75 72 73 6f 72 29 20 29 3b 0a 20 20 20  .iCursor) );.   
302bc 20 57 48 45 52 45 54 52 41 43 45 28 28 22 2a 2a   WHERETRACE(("**
302bd 2a 20 4f 70 74 69 6d 69 7a 65 72 20 73 65 6c 65  * Optimizer sele
302be 63 74 73 20 74 61 62 6c 65 20 25 64 20 66 6f 72  cts table %d for
302bf 20 6c 6f 6f 70 20 25 64 5c 6e 22 2c 20 62 65 73   loop %d\n", bes
302c0 74 4a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70  tJ,.           p
302c1 4c 65 76 65 6c 2d 70 57 49 6e 66 6f 2d 3e 61 29  Level-pWInfo->a)
302c2 29 3b 0a 20 20 20 20 69 66 28 20 28 62 65 73 74  );.    if( (best
302c3 50 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46 6c 61 67  Plan.plan.wsFlag
302c4 73 20 26 20 57 48 45 52 45 5f 4f 52 44 45 52 42  s & WHERE_ORDERB
302c5 59 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a  Y)!=0 ){.      *
302c6 70 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20  ppOrderBy = 0;. 
302c7 20 20 20 7d 0a 20 20 20 20 61 6e 64 46 6c 61 67     }.    andFlag
302c8 73 20 26 3d 20 62 65 73 74 50 6c 61 6e 2e 70 6c  s &= bestPlan.pl
302c9 61 6e 2e 77 73 46 6c 61 67 73 3b 0a 20 20 20 20  an.wsFlags;.    
302ca 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 20 3d 20 62  pLevel->plan = b
302cb 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 3b 0a 20 20  estPlan.plan;.  
302cc 20 20 69 66 28 20 62 65 73 74 50 6c 61 6e 2e 70    if( bestPlan.p
302cd 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
302ce 45 52 45 5f 49 4e 44 45 58 45 44 20 29 7b 0a 20  ERE_INDEXED ){. 
302cf 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 49 64       pLevel->iId
302d0 78 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  xCur = pParse->n
302d1 54 61 62 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65  Tab++;.    }else
302d2 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e  {.      pLevel->
302d3 69 49 64 78 43 75 72 20 3d 20 2d 31 3b 0a 20 20  iIdxCur = -1;.  
302d4 20 20 7d 0a 20 20 20 20 6e 6f 74 52 65 61 64 79    }.    notReady
302d5 20 26 3d 20 7e 67 65 74 4d 61 73 6b 28 70 4d 61   &= ~getMask(pMa
302d6 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73 74 2d  skSet, pTabList-
302d7 3e 61 5b 62 65 73 74 4a 5d 2e 69 43 75 72 73 6f  >a[bestJ].iCurso
302d8 72 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e  r);.    pLevel->
302d9 69 46 72 6f 6d 20 3d 20 28 75 38 29 62 65 73 74  iFrom = (u8)best
302da 4a 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b  J;..    /* Check
302db 20 74 68 61 74 20 69 66 20 74 68 65 20 74 61 62   that if the tab
302dc 6c 65 20 73 63 61 6e 6e 65 64 20 62 79 20 74 68  le scanned by th
302dd 69 73 20 6c 6f 6f 70 20 69 74 65 72 61 74 69 6f  is loop iteratio
302de 6e 20 68 61 64 20 61 6e 0a 20 20 20 20 2a 2a 20  n had an.    ** 
302df 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73  INDEXED BY claus
302e0 65 20 61 74 74 61 63 68 65 64 20 74 6f 20 69 74  e attached to it
302e1 2c 20 74 68 61 74 20 74 68 65 20 6e 61 6d 65 64  , that the named
302e2 20 69 6e 64 65 78 20 69 73 20 62 65 69 6e 67 0a   index is being.
302e3 20 20 20 20 2a 2a 20 75 73 65 64 20 66 6f 72 20      ** used for 
302e4 74 68 65 20 73 63 61 6e 2e 20 49 66 20 6e 6f 74  the scan. If not
302e5 2c 20 74 68 65 6e 20 71 75 65 72 79 20 63 6f 6d  , then query com
302e6 70 69 6c 61 74 69 6f 6e 20 68 61 73 20 66 61 69  pilation has fai
302e7 6c 65 64 2e 0a 20 20 20 20 2a 2a 20 52 65 74 75  led..    ** Retu
302e8 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20  rn an error..   
302e9 20 2a 2f 0a 20 20 20 20 70 49 64 78 20 3d 20 70   */.    pIdx = p
302ea 54 61 62 4c 69 73 74 2d 3e 61 5b 62 65 73 74 4a  TabList->a[bestJ
302eb 5d 2e 70 49 6e 64 65 78 3b 0a 20 20 20 20 69 66  ].pIndex;.    if
302ec 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20  ( pIdx ){.      
302ed 69 66 28 20 28 62 65 73 74 50 6c 61 6e 2e 70 6c  if( (bestPlan.pl
302ee 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
302ef 52 45 5f 49 4e 44 45 58 45 44 29 3d 3d 30 20 29  RE_INDEXED)==0 )
302f0 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
302f1 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
302f2 2c 20 22 63 61 6e 6e 6f 74 20 75 73 65 20 69 6e  , "cannot use in
302f3 64 65 78 3a 20 25 73 22 2c 20 70 49 64 78 2d 3e  dex: %s", pIdx->
302f4 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  zName);.        
302f5 67 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45  goto whereBeginE
302f6 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 65 6c 73  rror;.      }els
302f7 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  e{.        /* If
302f8 20 61 6e 20 49 4e 44 45 58 45 44 20 42 59 20 63   an INDEXED BY c
302f9 6c 61 75 73 65 20 69 73 20 75 73 65 64 2c 20 74  lause is used, t
302fa 68 65 20 62 65 73 74 49 6e 64 65 78 28 29 20 66  he bestIndex() f
302fb 75 6e 63 74 69 6f 6e 20 69 73 0a 20 20 20 20 20  unction is.     
302fc 20 20 20 2a 2a 20 67 75 61 72 61 6e 74 65 65 64     ** guaranteed
302fd 20 74 6f 20 66 69 6e 64 20 74 68 65 20 69 6e 64   to find the ind
302fe 65 78 20 73 70 65 63 69 66 69 65 64 20 69 6e 20  ex specified in 
302ff 74 68 65 20 49 4e 44 45 58 45 44 20 42 59 20 63  the INDEXED BY c
30300 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20 2a 2a  lause.        **
30301 20 69 66 20 69 74 20 66 69 6e 64 20 61 6e 20 69   if it find an i
30302 6e 64 65 78 20 61 74 20 61 6c 6c 2e 20 2a 2f 0a  ndex at all. */.
30303 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
30304 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 75 2e  bestPlan.plan.u.
30305 70 49 64 78 3d 3d 70 49 64 78 20 29 3b 0a 20 20  pIdx==pIdx );.  
30306 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
30307 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22 2a    WHERETRACE(("*
30308 2a 2a 20 4f 70 74 69 6d 69 7a 65 72 20 46 69 6e  ** Optimizer Fin
30309 69 73 68 65 64 20 2a 2a 2a 5c 6e 22 29 29 3b 0a  ished ***\n"));.
3030a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45    if( pParse->nE
3030b 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  rr || db->malloc
3030c 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f  Failed ){.    go
3030d 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45 72 72  to whereBeginErr
3030e 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  or;.  }..  /* If
3030f 20 74 68 65 20 74 6f 74 61 6c 20 71 75 65 72 79   the total query
30310 20 6f 6e 6c 79 20 73 65 6c 65 63 74 73 20 61 20   only selects a 
30311 73 69 6e 67 6c 65 20 72 6f 77 2c 20 74 68 65 6e  single row, then
30312 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a 20 20   the ORDER BY.  
30313 2a 2a 20 63 6c 61 75 73 65 20 69 73 20 69 72 72  ** clause is irr
30314 65 6c 65 76 61 6e 74 2e 0a 20 20 2a 2f 0a 20 20  elevant..  */.  
30315 69 66 28 20 28 61 6e 64 46 6c 61 67 73 20 26 20  if( (andFlags & 
30316 57 48 45 52 45 5f 55 4e 49 51 55 45 29 21 3d 30  WHERE_UNIQUE)!=0
30317 20 26 26 20 70 70 4f 72 64 65 72 42 79 20 29 7b   && ppOrderBy ){
30318 0a 20 20 20 20 2a 70 70 4f 72 64 65 72 42 79 20  .    *ppOrderBy 
30319 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  = 0;.  }..  /* I
3031a 66 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20  f the caller is 
3031b 61 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c  an UPDATE or DEL
3031c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 74 68  ETE statement th
3031d 61 74 20 69 73 20 72 65 71 75 65 73 74 69 6e 67  at is requesting
3031e 0a 20 20 2a 2a 20 74 6f 20 75 73 65 20 61 20 6f  .  ** to use a o
3031f 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68  ne-pass algorith
30320 6d 2c 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20  m, determine if 
30321 74 68 69 73 20 69 73 20 61 70 70 72 6f 70 72 69  this is appropri
30322 61 74 65 2e 0a 20 20 2a 2a 20 54 68 65 20 6f 6e  ate..  ** The on
30323 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d  e-pass algorithm
30324 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 69 66 20 74   only works if t
30325 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
30326 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a  constraints.  **
30327 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74   the statement t
30328 6f 20 75 70 64 61 74 65 20 61 20 73 69 6e 67 6c  o update a singl
30329 65 20 72 6f 77 2e 0a 20 20 2a 2f 0a 20 20 61 73  e row..  */.  as
3032a 73 65 72 74 28 20 28 77 63 74 72 6c 46 6c 61 67  sert( (wctrlFlag
3032b 73 20 26 20 57 48 45 52 45 5f 4f 4e 45 50 41 53  s & WHERE_ONEPAS
3032c 53 5f 44 45 53 49 52 45 44 29 3d 3d 30 20 7c 7c  S_DESIRED)==0 ||
3032d 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3d   pWInfo->nLevel=
3032e 3d 31 20 29 3b 0a 20 20 69 66 28 20 28 77 63 74  =1 );.  if( (wct
3032f 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  rlFlags & WHERE_
30330 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44 29  ONEPASS_DESIRED)
30331 21 3d 30 20 26 26 20 28 61 6e 64 46 6c 61 67 73  !=0 && (andFlags
30332 20 26 20 57 48 45 52 45 5f 55 4e 49 51 55 45 29   & WHERE_UNIQUE)
30333 21 3d 30 20 29 7b 0a 20 20 20 20 70 57 49 6e 66  !=0 ){.    pWInf
30334 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 20 3d 20 31  o->okOnePass = 1
30335 3b 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b  ;.    pWInfo->a[
30336 30 5d 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  0].plan.wsFlags 
30337 26 3d 20 7e 57 48 45 52 45 5f 49 44 58 5f 4f 4e  &= ~WHERE_IDX_ON
30338 4c 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70  LY;.  }..  /* Op
30339 65 6e 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e  en all tables in
3033a 20 74 68 65 20 70 54 61 62 4c 69 73 74 20 61 6e   the pTabList an
3033b 64 20 61 6e 79 20 69 6e 64 69 63 65 73 20 73 65  d any indices se
3033c 6c 65 63 74 65 64 20 66 6f 72 0a 20 20 2a 2a 20  lected for.  ** 
3033d 73 65 61 72 63 68 69 6e 67 20 74 68 6f 73 65 20  searching those 
3033e 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 73  tables..  */.  s
3033f 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79  qlite3CodeVerify
30340 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 2d  Schema(pParse, -
30341 31 29 3b 20 2f 2a 20 49 6e 73 65 72 74 20 74 68  1); /* Insert th
30342 65 20 63 6f 6f 6b 69 65 20 76 65 72 69 66 69 65  e cookie verifie
30343 72 20 47 6f 74 6f 20 2a 2f 0a 20 20 66 6f 72 28  r Goto */.  for(
30344 69 3d 30 2c 20 70 4c 65 76 65 6c 3d 70 57 49 6e  i=0, pLevel=pWIn
30345 66 6f 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73  fo->a; i<pTabLis
30346 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 4c  t->nSrc; i++, pL
30347 65 76 65 6c 2b 2b 29 7b 0a 20 20 20 20 54 61 62  evel++){.    Tab
30348 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 2f 2a  le *pTab;     /*
30349 20 54 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a   Table to open *
3034a 2f 0a 20 20 20 20 69 6e 74 20 69 44 62 3b 20 20  /.    int iDb;  
3034b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
3034c 6f 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  of database cont
3034d 61 69 6e 69 6e 67 20 74 61 62 6c 65 2f 69 6e 64  aining table/ind
3034e 65 78 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  ex */..#ifndef S
3034f 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41  QLITE_OMIT_EXPLA
30350 49 4e 0a 20 20 20 20 69 66 28 20 70 50 61 72 73  IN.    if( pPars
30351 65 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b  e->explain==2 ){
30352 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4d 73  .      char *zMs
30353 67 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20  g;.      struct 
30354 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
30355 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d  tem = &pTabList-
30356 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d  >a[pLevel->iFrom
30357 5d 3b 0a 20 20 20 20 20 20 7a 4d 73 67 20 3d 20  ];.      zMsg = 
30358 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
30359 62 2c 20 22 54 41 42 4c 45 20 25 73 22 2c 20 70  b, "TABLE %s", p
3035a 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  Item->zName);.  
3035b 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a      if( pItem->z
3035c 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 20 20 20  Alias ){.       
3035d 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d   zMsg = sqlite3M
3035e 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67  Appendf(db, zMsg
3035f 2c 20 22 25 73 20 41 53 20 25 73 22 2c 20 7a 4d  , "%s AS %s", zM
30360 73 67 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61  sg, pItem->zAlia
30361 73 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  s);.      }.    
30362 20 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70    if( (pLevel->p
30363 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
30364 45 52 45 5f 49 4e 44 45 58 45 44 29 21 3d 30 20  ERE_INDEXED)!=0 
30365 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20  ){.        zMsg 
30366 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64  = sqlite3MAppend
30367 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20  f(db, zMsg, "%s 
30368 57 49 54 48 20 49 4e 44 45 58 20 25 73 22 2c 0a  WITH INDEX %s",.
30369 20 20 20 20 20 20 20 20 20 20 20 7a 4d 73 67 2c             zMsg,
3036a 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e   pLevel->plan.u.
3036b 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pIdx->zName);.  
3036c 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c      }else if( pL
3036d 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
3036e 67 73 20 26 20 57 48 45 52 45 5f 4d 55 4c 54 49  gs & WHERE_MULTI
3036f 5f 4f 52 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  _OR ){.        z
30370 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70  Msg = sqlite3MAp
30371 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20  pendf(db, zMsg, 
30372 22 25 73 20 56 49 41 20 4d 55 4c 54 49 2d 49 4e  "%s VIA MULTI-IN
30373 44 45 58 20 55 4e 49 4f 4e 22 2c 20 7a 4d 73 67  DEX UNION", zMsg
30374 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  );.      }else i
30375 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  f( pLevel->plan.
30376 77 73 46 6c 61 67 73 20 26 20 28 57 48 45 52 45  wsFlags & (WHERE
30377 5f 52 4f 57 49 44 5f 45 51 7c 57 48 45 52 45 5f  _ROWID_EQ|WHERE_
30378 52 4f 57 49 44 5f 52 41 4e 47 45 29 20 29 7b 0a  ROWID_RANGE) ){.
30379 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73          zMsg = s
3037a 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64  qlite3MAppendf(d
3037b 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 55 53 49  b, zMsg, "%s USI
3037c 4e 47 20 50 52 49 4d 41 52 59 20 4b 45 59 22 2c  NG PRIMARY KEY",
3037d 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a   zMsg);.      }.
3037e 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
3037f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
30380 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20  .      else if( 
30381 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73  (pLevel->plan.ws
30382 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 56 49  Flags & WHERE_VI
30383 52 54 55 41 4c 54 41 42 4c 45 29 21 3d 30 20 29  RTUALTABLE)!=0 )
30384 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
30385 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 56  3_index_info *pV
30386 74 61 62 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d  tabIdx = pLevel-
30387 3e 70 6c 61 6e 2e 75 2e 70 56 74 61 62 49 64 78  >plan.u.pVtabIdx
30388 3b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d  ;.        zMsg =
30389 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66   sqlite3MAppendf
3038a 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 56  (db, zMsg, "%s V
3038b 49 52 54 55 41 4c 20 54 41 42 4c 45 20 49 4e 44  IRTUAL TABLE IND
3038c 45 58 20 25 64 3a 25 73 22 2c 20 7a 4d 73 67 2c  EX %d:%s", zMsg,
3038d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3038e 20 20 20 20 20 70 56 74 61 62 49 64 78 2d 3e 69       pVtabIdx->i
3038f 64 78 4e 75 6d 2c 20 70 56 74 61 62 49 64 78 2d  dxNum, pVtabIdx-
30390 3e 69 64 78 53 74 72 29 3b 0a 20 20 20 20 20 20  >idxStr);.      
30391 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69  }.#endif.      i
30392 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  f( pLevel->plan.
30393 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
30394 4f 52 44 45 52 42 59 20 29 7b 0a 20 20 20 20 20  ORDERBY ){.     
30395 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65     zMsg = sqlite
30396 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d  3MAppendf(db, zM
30397 73 67 2c 20 22 25 73 20 4f 52 44 45 52 20 42 59  sg, "%s ORDER BY
30398 22 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20  ", zMsg);.      
30399 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  }.      sqlite3V
3039a 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
3039b 45 78 70 6c 61 69 6e 2c 20 69 2c 20 70 4c 65 76  Explain, i, pLev
3039c 65 6c 2d 3e 69 46 72 6f 6d 2c 20 30 2c 20 7a 4d  el->iFrom, 0, zM
3039d 73 67 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b  sg, P4_DYNAMIC);
3039e 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  .    }.#endif /*
3039f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50   SQLITE_OMIT_EXP
303a0 4c 41 49 4e 20 2a 2f 0a 20 20 20 20 70 54 61 62  LAIN */.    pTab
303a1 49 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74  Item = &pTabList
303a2 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f  ->a[pLevel->iFro
303a3 6d 5d 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 70  m];.    pTab = p
303a4 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20  TabItem->pTab;. 
303a5 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33     iDb = sqlite3
303a6 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62  SchemaToIndex(db
303a7 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
303a8 3b 0a 20 20 20 20 69 66 28 20 28 70 54 61 62 2d  ;.    if( (pTab-
303a9 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 45  >tabFlags & TF_E
303aa 70 68 65 6d 65 72 61 6c 29 21 3d 30 20 7c 7c 20  phemeral)!=0 || 
303ab 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 20  pTab->pSelect ) 
303ac 63 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65  continue;.#ifnde
303ad 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
303ae 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69  RTUALTABLE.    i
303af 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  f( (pLevel->plan
303b0 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
303b1 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 21 3d  _VIRTUALTABLE)!=
303b2 30 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74  0 ){.      const
303b3 20 63 68 61 72 20 2a 70 56 54 61 62 20 3d 20 28   char *pVTab = (
303b4 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c  const char *)sql
303b5 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 64 62  ite3GetVTable(db
303b6 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20 69  , pTab);.      i
303b7 6e 74 20 69 43 75 72 20 3d 20 70 54 61 62 49 74  nt iCur = pTabIt
303b8 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20  em->iCursor;.   
303b9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
303ba 64 4f 70 34 28 76 2c 20 4f 50 5f 56 4f 70 65 6e  dOp4(v, OP_VOpen
303bb 2c 20 69 43 75 72 2c 20 30 2c 20 30 2c 20 70 56  , iCur, 0, 0, pV
303bc 54 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20  Tab, P4_VTAB);. 
303bd 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a     }else.#endif.
303be 20 20 20 20 69 66 28 20 28 70 4c 65 76 65 6c 2d      if( (pLevel-
303bf 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
303c0 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d  WHERE_IDX_ONLY)=
303c1 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 28  =0.         && (
303c2 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45  wctrlFlags & WHE
303c3 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 29 3d 3d 30  RE_OMIT_OPEN)==0
303c4 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6f 70   ){.      int op
303c5 20 3d 20 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65   = pWInfo->okOne
303c6 50 61 73 73 20 3f 20 4f 50 5f 4f 70 65 6e 57 72  Pass ? OP_OpenWr
303c7 69 74 65 20 3a 20 4f 50 5f 4f 70 65 6e 52 65 61  ite : OP_OpenRea
303c8 64 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  d;.      sqlite3
303c9 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65  OpenTable(pParse
303ca 2c 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 72  , pTabItem->iCur
303cb 73 6f 72 2c 20 69 44 62 2c 20 70 54 61 62 2c 20  sor, iDb, pTab, 
303cc 6f 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  op);.      if( !
303cd 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73  pWInfo->okOnePas
303ce 73 20 26 26 20 70 54 61 62 2d 3e 6e 43 6f 6c 3c  s && pTab->nCol<
303cf 42 4d 53 20 29 7b 0a 20 20 20 20 20 20 20 20 42  BMS ){.        B
303d0 69 74 6d 61 73 6b 20 62 20 3d 20 70 54 61 62 49  itmask b = pTabI
303d1 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 20  tem->colUsed;.  
303d2 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 30 3b        int n = 0;
303d3 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20 62  .        for(; b
303d4 3b 20 62 3d 62 3e 3e 31 2c 20 6e 2b 2b 29 7b 7d  ; b=b>>1, n++){}
303d5 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
303d6 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
303d7 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
303d8 6e 74 41 64 64 72 28 76 29 2d 31 2c 20 53 51 4c  ntAddr(v)-1, SQL
303d9 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 6e  ITE_INT_TO_PTR(n
303da 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20  ), P4_INT32);.  
303db 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c        assert( n<
303dc 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20  =pTab->nCol );. 
303dd 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
303de 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 54  {.      sqlite3T
303df 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c  ableLock(pParse,
303e0 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d   iDb, pTab->tnum
303e1 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  , 0, pTab->zName
303e2 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 65  );.    }.    pLe
303e3 76 65 6c 2d 3e 69 54 61 62 43 75 72 20 3d 20 70  vel->iTabCur = p
303e4 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  TabItem->iCursor
303e5 3b 0a 20 20 20 20 69 66 28 20 28 70 4c 65 76 65  ;.    if( (pLeve
303e6 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
303e7 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44 29  & WHERE_INDEXED)
303e8 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 49 6e 64  !=0 ){.      Ind
303e9 65 78 20 2a 70 49 78 20 3d 20 70 4c 65 76 65 6c  ex *pIx = pLevel
303ea 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3b 0a 20  ->plan.u.pIdx;. 
303eb 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b       KeyInfo *pK
303ec 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65  ey = sqlite3Inde
303ed 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
303ee 20 70 49 78 29 3b 0a 20 20 20 20 20 20 69 6e 74   pIx);.      int
303ef 20 69 49 64 78 43 75 72 20 3d 20 70 4c 65 76 65   iIdxCur = pLeve
303f0 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20  l->iIdxCur;.    
303f1 20 20 61 73 73 65 72 74 28 20 70 49 78 2d 3e 70    assert( pIx->p
303f2 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53  Schema==pTab->pS
303f3 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20 20 61  chema );.      a
303f4 73 73 65 72 74 28 20 69 49 64 78 43 75 72 3e 3d  ssert( iIdxCur>=
303f5 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 );.      sqlit
303f6 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
303f7 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 49 64  OP_OpenRead, iId
303f8 78 43 75 72 2c 20 70 49 78 2d 3e 74 6e 75 6d 2c  xCur, pIx->tnum,
303f9 20 69 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20   iDb,.          
303fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63                (c
303fb 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45  har*)pKey, P4_KE
303fc 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a  YINFO_HANDOFF);.
303fd 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
303fe 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49 78 2d  t((v, "%s", pIx-
303ff 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 7d 0a  >zName));.    }.
30400 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56      sqlite3CodeV
30401 65 72 69 66 79 53 63 68 65 6d 61 28 70 50 61 72  erifySchema(pPar
30402 73 65 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20  se, iDb);.  }.  
30403 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 20 3d 20 73  pWInfo->iTop = s
30404 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
30405 74 41 64 64 72 28 76 29 3b 0a 0a 20 20 2f 2a 20  tAddr(v);..  /* 
30406 47 65 6e 65 72 61 74 65 20 74 68 65 20 63 6f 64  Generate the cod
30407 65 20 74 6f 20 64 6f 20 74 68 65 20 73 65 61 72  e to do the sear
30408 63 68 2e 20 20 45 61 63 68 20 69 74 65 72 61 74  ch.  Each iterat
30409 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 0a 20  ion of the for. 
3040a 20 2a 2a 20 6c 6f 6f 70 20 62 65 6c 6f 77 20 67   ** loop below g
3040b 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 66 6f  enerates code fo
3040c 72 20 61 20 73 69 6e 67 6c 65 20 6e 65 73 74 65  r a single neste
3040d 64 20 6c 6f 6f 70 20 6f 66 20 74 68 65 20 56 4d  d loop of the VM
3040e 0a 20 20 2a 2a 20 70 72 6f 67 72 61 6d 2e 0a 20  .  ** program.. 
3040f 20 2a 2f 0a 20 20 6e 6f 74 52 65 61 64 79 20 3d   */.  notReady =
30410 20 7e 28 42 69 74 6d 61 73 6b 29 30 3b 0a 20 20   ~(Bitmask)0;.  
30411 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c  for(i=0; i<pTabL
30412 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b  ist->nSrc; i++){
30413 0a 20 20 20 20 6e 6f 74 52 65 61 64 79 20 3d 20  .    notReady = 
30414 63 6f 64 65 4f 6e 65 4c 6f 6f 70 53 74 61 72 74  codeOneLoopStart
30415 28 70 57 49 6e 66 6f 2c 20 69 2c 20 77 63 74 72  (pWInfo, i, wctr
30416 6c 46 6c 61 67 73 2c 20 6e 6f 74 52 65 61 64 79  lFlags, notReady
30417 29 3b 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e 69  );.    pWInfo->i
30418 43 6f 6e 74 69 6e 75 65 20 3d 20 70 57 49 6e 66  Continue = pWInf
30419 6f 2d 3e 61 5b 69 5d 2e 61 64 64 72 43 6f 6e 74  o->a[i].addrCont
3041a 3b 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51  ;.  }..#ifdef SQ
3041b 4c 49 54 45 5f 54 45 53 54 20 20 2f 2a 20 46 6f  LITE_TEST  /* Fo
3041c 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65  r testing and de
3041d 62 75 67 67 69 6e 67 20 75 73 65 20 6f 6e 6c 79  bugging use only
3041e 20 2a 2f 0a 20 20 2f 2a 20 52 65 63 6f 72 64 20   */.  /* Record 
3041f 69 6e 20 74 68 65 20 71 75 65 72 79 20 70 6c 61  in the query pla
30420 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  n information ab
30421 6f 75 74 20 74 68 65 20 63 75 72 72 65 6e 74 20  out the current 
30422 74 61 62 6c 65 0a 20 20 2a 2a 20 61 6e 64 20 74  table.  ** and t
30423 68 65 20 69 6e 64 65 78 20 75 73 65 64 20 74 6f  he index used to
30424 20 61 63 63 65 73 73 20 69 74 20 28 69 66 20 61   access it (if a
30425 6e 79 29 2e 20 20 49 66 20 74 68 65 20 74 61 62  ny).  If the tab
30426 6c 65 20 69 74 73 65 6c 66 0a 20 20 2a 2a 20 69  le itself.  ** i
30427 73 20 6e 6f 74 20 75 73 65 64 2c 20 69 74 73 20  s not used, its 
30428 6e 61 6d 65 20 69 73 20 6a 75 73 74 20 27 7b 7d  name is just '{}
30429 27 2e 20 20 49 66 20 6e 6f 20 69 6e 64 65 78 20  '.  If no index 
3042a 69 73 20 75 73 65 64 0a 20 20 2a 2a 20 74 68 65  is used.  ** the
3042b 20 69 6e 64 65 78 20 69 73 20 6c 69 73 74 65 64   index is listed
3042c 20 61 73 20 22 7b 7d 22 2e 20 20 49 66 20 74 68   as "{}".  If th
3042d 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73  e primary key is
3042e 20 75 73 65 64 20 74 68 65 0a 20 20 2a 2a 20 69   used the.  ** i
3042f 6e 64 65 78 20 6e 61 6d 65 20 69 73 20 27 2a 27  ndex name is '*'
30430 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  ..  */.  for(i=0
30431 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  ; i<pTabList->nS
30432 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68  rc; i++){.    ch
30433 61 72 20 2a 7a 3b 0a 20 20 20 20 69 6e 74 20 6e  ar *z;.    int n
30434 3b 0a 20 20 20 20 70 4c 65 76 65 6c 20 3d 20 26  ;.    pLevel = &
30435 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 3b 0a 20 20  pWInfo->a[i];.  
30436 20 20 70 54 61 62 49 74 65 6d 20 3d 20 26 70 54    pTabItem = &pT
30437 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65 6c  abList->a[pLevel
30438 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20 7a 20  ->iFrom];.    z 
30439 3d 20 70 54 61 62 49 74 65 6d 2d 3e 7a 41 6c 69  = pTabItem->zAli
3043a 61 73 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30  as;.    if( z==0
3043b 20 29 20 7a 20 3d 20 70 54 61 62 49 74 65 6d 2d   ) z = pTabItem-
3043c 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  >pTab->zName;.  
3043d 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72    n = sqlite3Str
3043e 6c 65 6e 33 30 28 7a 29 3b 0a 20 20 20 20 69 66  len30(z);.    if
3043f 28 20 6e 2b 6e 51 50 6c 61 6e 20 3c 20 73 69 7a  ( n+nQPlan < siz
30440 65 6f 66 28 73 71 6c 69 74 65 33 5f 71 75 65 72  eof(sqlite3_quer
30441 79 5f 70 6c 61 6e 29 2d 31 30 20 29 7b 0a 20 20  y_plan)-10 ){.  
30442 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
30443 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
30444 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 20 29 7b  HERE_IDX_ONLY ){
30445 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
30446 26 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70  &sqlite3_query_p
30447 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 22 7b 7d  lan[nQPlan], "{}
30448 22 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 6e  ", 2);.        n
30449 51 50 6c 61 6e 20 2b 3d 20 32 3b 0a 20 20 20 20  QPlan += 2;.    
3044a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
3044b 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74 65 33   memcpy(&sqlite3
3044c 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c  _query_plan[nQPl
3044d 61 6e 5d 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20  an], z, n);.    
3044e 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 6e 3b      nQPlan += n;
3044f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
30450 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61  qlite3_query_pla
30451 6e 5b 6e 51 50 6c 61 6e 2b 2b 5d 20 3d 20 27 20  n[nQPlan++] = ' 
30452 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 73  ';.    }.    tes
30453 74 63 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e 70  tcase( pLevel->p
30454 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
30455 45 52 45 5f 52 4f 57 49 44 5f 45 51 20 29 3b 0a  ERE_ROWID_EQ );.
30456 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 4c      testcase( pL
30457 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
30458 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44  gs & WHERE_ROWID
30459 5f 52 41 4e 47 45 20 29 3b 0a 20 20 20 20 69 66  _RANGE );.    if
3045a 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77  ( pLevel->plan.w
3045b 73 46 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f  sFlags & (WHERE_
3045c 52 4f 57 49 44 5f 45 51 7c 57 48 45 52 45 5f 52  ROWID_EQ|WHERE_R
3045d 4f 57 49 44 5f 52 41 4e 47 45 29 20 29 7b 0a 20  OWID_RANGE) ){. 
3045e 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c       memcpy(&sql
3045f 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
30460 6e 51 50 6c 61 6e 5d 2c 20 22 2a 20 22 2c 20 32  nQPlan], "* ", 2
30461 29 3b 0a 20 20 20 20 20 20 6e 51 50 6c 61 6e 20  );.      nQPlan 
30462 2b 3d 20 32 3b 0a 20 20 20 20 7d 65 6c 73 65 20  += 2;.    }else 
30463 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61  if( (pLevel->pla
30464 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
30465 45 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 29 7b  E_INDEXED)!=0 ){
30466 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c 69 74  .      n = sqlit
30467 65 33 53 74 72 6c 65 6e 33 30 28 70 4c 65 76 65  e3Strlen30(pLeve
30468 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e  l->plan.u.pIdx->
30469 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66  zName);.      if
3046a 28 20 6e 2b 6e 51 50 6c 61 6e 20 3c 20 73 69 7a  ( n+nQPlan < siz
3046b 65 6f 66 28 73 71 6c 69 74 65 33 5f 71 75 65 72  eof(sqlite3_quer
3046c 79 5f 70 6c 61 6e 29 2d 32 20 29 7b 0a 20 20 20  y_plan)-2 ){.   
3046d 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c       memcpy(&sql
3046e 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
3046f 6e 51 50 6c 61 6e 5d 2c 20 70 4c 65 76 65 6c 2d  nQPlan], pLevel-
30470 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e 7a 4e  >plan.u.pIdx->zN
30471 61 6d 65 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20  ame, n);.       
30472 20 6e 51 50 6c 61 6e 20 2b 3d 20 6e 3b 0a 20 20   nQPlan += n;.  
30473 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 71 75        sqlite3_qu
30474 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 2b  ery_plan[nQPlan+
30475 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20  +] = ' ';.      
30476 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
30477 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74     memcpy(&sqlit
30478 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51  e3_query_plan[nQ
30479 50 6c 61 6e 5d 2c 20 22 7b 7d 20 22 2c 20 33 29  Plan], "{} ", 3)
3047a 3b 0a 20 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b  ;.      nQPlan +
3047b 3d 20 33 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 3;.    }.  }. 
3047c 20 77 68 69 6c 65 28 20 6e 51 50 6c 61 6e 3e 30   while( nQPlan>0
3047d 20 26 26 20 73 71 6c 69 74 65 33 5f 71 75 65 72   && sqlite3_quer
3047e 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 2d 31 5d  y_plan[nQPlan-1]
3047f 3d 3d 27 20 27 20 29 7b 0a 20 20 20 20 73 71 6c  ==' ' ){.    sql
30480 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
30481 2d 2d 6e 51 50 6c 61 6e 5d 20 3d 20 30 3b 0a 20  --nQPlan] = 0;. 
30482 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 71 75 65   }.  sqlite3_que
30483 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 20  ry_plan[nQPlan] 
30484 3d 20 30 3b 0a 20 20 6e 51 50 6c 61 6e 20 3d 20  = 0;.  nQPlan = 
30485 30 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  0;.#endif /* SQL
30486 49 54 45 5f 54 45 53 54 20 2f 2f 20 54 65 73 74  ITE_TEST // Test
30487 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e  ing and debuggin
30488 67 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 0a 20  g use only */.. 
30489 20 2f 2a 20 52 65 63 6f 72 64 20 74 68 65 20 63   /* Record the c
3048a 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 61 64 64 72  ontinuation addr
3048b 65 73 73 20 69 6e 20 74 68 65 20 57 68 65 72 65  ess in the Where
3048c 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20  Info structure. 
3048d 20 54 68 65 6e 0a 20 20 2a 2a 20 63 6c 65 61 6e   Then.  ** clean
3048e 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a   up and return..
3048f 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 70 57    */.  return pW
30490 49 6e 66 6f 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70  Info;..  /* Jump
30491 20 68 65 72 65 20 69 66 20 6d 61 6c 6c 6f 63 20   here if malloc 
30492 66 61 69 6c 73 20 2a 2f 0a 77 68 65 72 65 42 65  fails */.whereBe
30493 67 69 6e 45 72 72 6f 72 3a 0a 20 20 77 68 65 72  ginError:.  wher
30494 65 49 6e 66 6f 46 72 65 65 28 64 62 2c 20 70 57  eInfoFree(db, pW
30495 49 6e 66 6f 29 3b 0a 20 20 72 65 74 75 72 6e 20  Info);.  return 
30496 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  0;.}../*.** Gene
30497 72 61 74 65 20 74 68 65 20 65 6e 64 20 6f 66 20  rate the end of 
30498 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 2e 20  the WHERE loop. 
30499 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e   See comments on
3049a 20 0a 2a 2a 20 73 71 6c 69 74 65 33 57 68 65 72   .** sqlite3Wher
3049b 65 42 65 67 69 6e 28 29 20 66 6f 72 20 61 64 64  eBegin() for add
3049c 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
3049d 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
3049e 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
3049f 74 65 33 57 68 65 72 65 45 6e 64 28 57 68 65 72  te3WhereEnd(Wher
304a0 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 29 7b 0a  eInfo *pWInfo){.
304a1 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 20    Parse *pParse 
304a2 3d 20 70 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65  = pWInfo->pParse
304a3 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  ;.  Vdbe *v = pP
304a4 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
304a5 6e 74 20 69 3b 0a 20 20 57 68 65 72 65 4c 65 76  nt i;.  WhereLev
304a6 65 6c 20 2a 70 4c 65 76 65 6c 3b 0a 20 20 53 72  el *pLevel;.  Sr
304a7 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 20  cList *pTabList 
304a8 3d 20 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 69  = pWInfo->pTabLi
304a9 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  st;.  sqlite3 *d
304aa 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
304ab 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 6c  .  /* Generate l
304ac 6f 6f 70 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20  oop termination 
304ad 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  code..  */.  sql
304ae 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65  ite3ExprCacheCle
304af 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 66 6f  ar(pParse);.  fo
304b0 72 28 69 3d 70 54 61 62 4c 69 73 74 2d 3e 6e 53  r(i=pTabList->nS
304b1 72 63 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29  rc-1; i>=0; i--)
304b2 7b 0a 20 20 20 20 70 4c 65 76 65 6c 20 3d 20 26  {.    pLevel = &
304b3 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 3b 0a 20 20  pWInfo->a[i];.  
304b4 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
304b5 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 70 4c 65  olveLabel(v, pLe
304b6 76 65 6c 2d 3e 61 64 64 72 43 6f 6e 74 29 3b 0a  vel->addrCont);.
304b7 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
304b8 6f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20  op!=OP_Noop ){. 
304b9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
304ba 41 64 64 4f 70 32 28 76 2c 20 70 4c 65 76 65 6c  AddOp2(v, pLevel
304bb 2d 3e 6f 70 2c 20 70 4c 65 76 65 6c 2d 3e 70 31  ->op, pLevel->p1
304bc 2c 20 70 4c 65 76 65 6c 2d 3e 70 32 29 3b 0a 20  , pLevel->p2);. 
304bd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
304be 43 68 61 6e 67 65 50 35 28 76 2c 20 70 4c 65 76  ChangeP5(v, pLev
304bf 65 6c 2d 3e 70 35 29 3b 0a 20 20 20 20 7d 0a 20  el->p5);.    }. 
304c0 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70     if( pLevel->p
304c1 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
304c2 45 52 45 5f 49 4e 5f 41 42 4c 45 20 26 26 20 70  ERE_IN_ABLE && p
304c3 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 3e  Level->u.in.nIn>
304c4 30 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63  0 ){.      struc
304c5 74 20 49 6e 4c 6f 6f 70 20 2a 70 49 6e 3b 0a 20  t InLoop *pIn;. 
304c6 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
304c7 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
304c8 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 70 4c 65  olveLabel(v, pLe
304c9 76 65 6c 2d 3e 61 64 64 72 4e 78 74 29 3b 0a 20  vel->addrNxt);. 
304ca 20 20 20 20 20 66 6f 72 28 6a 3d 70 4c 65 76 65       for(j=pLeve
304cb 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 2c 20 70 49 6e  l->u.in.nIn, pIn
304cc 3d 26 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61  =&pLevel->u.in.a
304cd 49 6e 4c 6f 6f 70 5b 6a 2d 31 5d 3b 20 6a 3e 30  InLoop[j-1]; j>0
304ce 3b 20 6a 2d 2d 2c 20 70 49 6e 2d 2d 29 7b 0a 20  ; j--, pIn--){. 
304cf 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
304d0 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 70 49  beJumpHere(v, pI
304d1 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 2b 31 29 3b  n->addrInTop+1);
304d2 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
304d3 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
304d4 5f 4e 65 78 74 2c 20 70 49 6e 2d 3e 69 43 75 72  _Next, pIn->iCur
304d5 2c 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70  , pIn->addrInTop
304d6 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
304d7 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
304d8 2c 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70  , pIn->addrInTop
304d9 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  -1);.      }.   
304da 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
304db 28 64 62 2c 20 70 4c 65 76 65 6c 2d 3e 75 2e 69  (db, pLevel->u.i
304dc 6e 2e 61 49 6e 4c 6f 6f 70 29 3b 0a 20 20 20 20  n.aInLoop);.    
304dd 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
304de 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
304df 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72 6b   pLevel->addrBrk
304e0 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65  );.    if( pLeve
304e1 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 29 7b 0a  l->iLeftJoin ){.
304e2 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a        int addr;.
304e3 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
304e4 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
304e5 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 70 4c 65 76  , OP_IfPos, pLev
304e6 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a  el->iLeftJoin);.
304e7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
304e8 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 75  eAddOp1(v, OP_Nu
304e9 6c 6c 52 6f 77 2c 20 70 54 61 62 4c 69 73 74 2d  llRow, pTabList-
304ea 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 29 3b 0a  >a[i].iCursor);.
304eb 20 20 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c        if( pLevel
304ec 2d 3e 69 49 64 78 43 75 72 3e 3d 30 20 29 7b 0a  ->iIdxCur>=0 ){.
304ed 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
304ee 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
304ef 4e 75 6c 6c 52 6f 77 2c 20 70 4c 65 76 65 6c 2d  NullRow, pLevel-
304f0 3e 69 49 64 78 43 75 72 29 3b 0a 20 20 20 20 20  >iIdxCur);.     
304f1 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65   }.      if( pLe
304f2 76 65 6c 2d 3e 6f 70 3d 3d 4f 50 5f 52 65 74 75  vel->op==OP_Retu
304f3 72 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  rn ){.        sq
304f4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
304f5 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 70 4c 65  v, OP_Gosub, pLe
304f6 76 65 6c 2d 3e 70 31 2c 20 70 4c 65 76 65 6c 2d  vel->p1, pLevel-
304f7 3e 61 64 64 72 46 69 72 73 74 29 3b 0a 20 20 20  >addrFirst);.   
304f8 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
304f9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
304fa 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
304fb 30 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 46  0, pLevel->addrF
304fc 69 72 73 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20  irst);.      }. 
304fd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
304fe 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
304ff 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
30500 2f 2a 20 54 68 65 20 22 62 72 65 61 6b 22 20 70  /* The "break" p
30501 6f 69 6e 74 20 69 73 20 68 65 72 65 2c 20 6a 75  oint is here, ju
30502 73 74 20 70 61 73 74 20 74 68 65 20 65 6e 64 20  st past the end 
30503 6f 66 20 74 68 65 20 6f 75 74 65 72 20 6c 6f 6f  of the outer loo
30504 70 2e 0a 20 20 2a 2a 20 53 65 74 20 69 74 2e 0a  p..  ** Set it..
30505 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
30506 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
30507 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b  , pWInfo->iBreak
30508 29 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61  );..  /* Close a
30509 6c 6c 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72  ll of the cursor
3050a 73 20 74 68 61 74 20 77 65 72 65 20 6f 70 65 6e  s that were open
3050b 65 64 20 62 79 20 73 71 6c 69 74 65 33 57 68 65  ed by sqlite3Whe
3050c 72 65 42 65 67 69 6e 2e 0a 20 20 2a 2f 0a 20 20  reBegin..  */.  
3050d 66 6f 72 28 69 3d 30 2c 20 70 4c 65 76 65 6c 3d  for(i=0, pLevel=
3050e 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54 61  pWInfo->a; i<pTa
3050f 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
30510 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20 20  , pLevel++){.   
30511 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
30512 69 74 65 6d 20 2a 70 54 61 62 49 74 65 6d 20 3d  item *pTabItem =
30513 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c   &pTabList->a[pL
30514 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20  evel->iFrom];.  
30515 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
30516 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 3b 0a  pTabItem->pTab;.
30517 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62      assert( pTab
30518 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 28  !=0 );.    if( (
30519 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26  pTab->tabFlags &
3051a 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 29 21 3d   TF_Ephemeral)!=
3051b 30 20 7c 7c 20 70 54 61 62 2d 3e 70 53 65 6c 65  0 || pTab->pSele
3051c 63 74 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  ct ) continue;. 
3051d 20 20 20 69 66 28 20 28 70 57 49 6e 66 6f 2d 3e     if( (pWInfo->
3051e 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45  wctrlFlags & WHE
3051f 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 29 3d 3d  RE_OMIT_CLOSE)==
30520 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21  0 ){.      if( !
30521 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73  pWInfo->okOnePas
30522 73 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e 70 6c  s && (pLevel->pl
30523 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
30524 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d 3d 30 20  RE_IDX_ONLY)==0 
30525 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
30526 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
30527 4f 50 5f 43 6c 6f 73 65 2c 20 70 54 61 62 49 74  OP_Close, pTabIt
30528 65 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20  em->iCursor);.  
30529 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
3052a 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73  (pLevel->plan.ws
3052b 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e  Flags & WHERE_IN
3052c 44 45 58 45 44 29 21 3d 30 20 29 7b 0a 20 20 20  DEXED)!=0 ){.   
3052d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
3052e 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
3052f 73 65 2c 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78  se, pLevel->iIdx
30530 43 75 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Cur);.      }.  
30531 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
30532 68 69 73 20 73 63 61 6e 20 75 73 65 73 20 61 6e  his scan uses an
30533 20 69 6e 64 65 78 2c 20 6d 61 6b 65 20 63 6f 64   index, make cod
30534 65 20 73 75 62 73 74 69 74 75 74 69 6f 6e 73 20  e substitutions 
30535 74 6f 20 72 65 61 64 20 64 61 74 61 0a 20 20 20  to read data.   
30536 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e 64   ** from the ind
30537 65 78 20 69 6e 20 70 72 65 66 65 72 65 6e 63 65  ex in preference
30538 20 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 20 53   to the table. S
30539 6f 6d 65 74 69 6d 65 73 2c 20 74 68 69 73 20 6d  ometimes, this m
3053a 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 65 20  eans.    ** the 
3053b 74 61 62 6c 65 20 6e 65 65 64 20 6e 65 76 65 72  table need never
3053c 20 62 65 20 72 65 61 64 20 66 72 6f 6d 2e 20 54   be read from. T
3053d 68 69 73 20 69 73 20 61 20 70 65 72 66 6f 72 6d  his is a perform
3053e 61 6e 63 65 20 62 6f 6f 73 74 2c 0a 20 20 20 20  ance boost,.    
3053f 2a 2a 20 61 73 20 74 68 65 20 76 64 62 65 20 6c  ** as the vdbe l
30540 65 76 65 6c 20 77 61 69 74 73 20 75 6e 74 69 6c  evel waits until
30541 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 72 65   the table is re
30542 61 64 20 62 65 66 6f 72 65 20 61 63 74 75 61 6c  ad before actual
30543 6c 79 0a 20 20 20 20 2a 2a 20 73 65 65 6b 69 6e  ly.    ** seekin
30544 67 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 73  g the table curs
30545 6f 72 20 74 6f 20 74 68 65 20 72 65 63 6f 72 64  or to the record
30546 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
30547 6f 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20 20  o the current.  
30548 20 20 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e    ** position in
30549 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 20 20   the index..    
3054a 2a 2a 20 0a 20 20 20 20 2a 2a 20 43 61 6c 6c 73  ** .    ** Calls
3054b 20 74 6f 20 74 68 65 20 63 6f 64 65 20 67 65 6e   to the code gen
3054c 65 72 61 74 6f 72 20 69 6e 20 62 65 74 77 65 65  erator in betwee
3054d 6e 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  n sqlite3WhereBe
3054e 67 69 6e 20 61 6e 64 0a 20 20 20 20 2a 2a 20 73  gin and.    ** s
3054f 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 20 77  qlite3WhereEnd w
30550 69 6c 6c 20 68 61 76 65 20 63 72 65 61 74 65 64  ill have created
30551 20 63 6f 64 65 20 74 68 61 74 20 72 65 66 65 72   code that refer
30552 65 6e 63 65 73 20 74 68 65 20 74 61 62 6c 65 0a  ences the table.
30553 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 2e      ** directly.
30554 20 20 54 68 69 73 20 6c 6f 6f 70 20 73 63 61 6e    This loop scan
30555 73 20 61 6c 6c 20 74 68 61 74 20 63 6f 64 65 20  s all that code 
30556 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6f 70 63 6f  looking for opco
30557 64 65 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20  des.    ** that 
30558 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 74 61  reference the ta
30559 62 6c 65 20 61 6e 64 20 63 6f 6e 76 65 72 74 73  ble and converts
3055a 20 74 68 65 6d 20 69 6e 74 6f 20 6f 70 63 6f 64   them into opcod
3055b 65 73 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 72  es that.    ** r
3055c 65 66 65 72 65 6e 63 65 20 74 68 65 20 69 6e 64  eference the ind
3055d 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ex..    */.    i
3055e 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  f( (pLevel->plan
3055f 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
30560 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 26 26 20  _INDEXED)!=0 && 
30561 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
30562 64 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 2c  d){.      int k,
30563 20 6a 2c 20 6c 61 73 74 3b 0a 20 20 20 20 20 20   j, last;.      
30564 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 20  VdbeOp *pOp;.   
30565 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d     Index *pIdx =
30566 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e   pLevel->plan.u.
30567 70 49 64 78 3b 0a 20 20 20 20 20 20 69 6e 74 20  pIdx;.      int 
30568 75 73 65 49 6e 64 65 78 4f 6e 6c 79 20 3d 20 70  useIndexOnly = p
30569 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
3056a 61 67 73 20 26 20 57 48 45 52 45 5f 49 44 58 5f  ags & WHERE_IDX_
3056b 4f 4e 4c 59 3b 0a 0a 20 20 20 20 20 20 61 73 73  ONLY;..      ass
3056c 65 72 74 28 20 70 49 64 78 21 3d 30 20 29 3b 0a  ert( pIdx!=0 );.
3056d 20 20 20 20 20 20 70 4f 70 20 3d 20 73 71 6c 69        pOp = sqli
3056e 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20  te3VdbeGetOp(v, 
3056f 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 29 3b 0a 20  pWInfo->iTop);. 
30570 20 20 20 20 20 6c 61 73 74 20 3d 20 73 71 6c 69       last = sqli
30571 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
30572 64 72 28 76 29 3b 0a 20 20 20 20 20 20 66 6f 72  dr(v);.      for
30573 28 6b 3d 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 3b  (k=pWInfo->iTop;
30574 20 6b 3c 6c 61 73 74 3b 20 6b 2b 2b 2c 20 70 4f   k<last; k++, pO
30575 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  p++){.        if
30576 28 20 70 4f 70 2d 3e 70 31 21 3d 70 4c 65 76 65  ( pOp->p1!=pLeve
30577 6c 2d 3e 69 54 61 62 43 75 72 20 29 20 63 6f 6e  l->iTabCur ) con
30578 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 69  tinue;.        i
30579 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  f( pOp->opcode==
3057a 4f 50 5f 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  OP_Column ){.   
3057b 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20         for(j=0; 
3057c 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  j<pIdx->nColumn;
3057d 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   j++){.         
3057e 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d     if( pOp->p2==
3057f 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a  pIdx->aiColumn[j
30580 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ] ){.           
30581 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 6a 3b 0a     pOp->p2 = j;.
30582 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4f                pO
30583 70 2d 3e 70 31 20 3d 20 70 4c 65 76 65 6c 2d 3e  p->p1 = pLevel->
30584 69 49 64 78 43 75 72 3b 0a 20 20 20 20 20 20 20  iIdxCur;.       
30585 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
30586 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
30587 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
30588 20 20 61 73 73 65 72 74 28 21 75 73 65 49 6e 64    assert(!useInd
30589 65 78 4f 6e 6c 79 20 7c 7c 20 6a 3c 70 49 64 78  exOnly || j<pIdx
3058a 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  ->nColumn);.    
3058b 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f      }else if( pO
3058c 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f  p->opcode==OP_Ro
3058d 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20  wid ){.         
3058e 20 70 4f 70 2d 3e 70 31 20 3d 20 70 4c 65 76 65   pOp->p1 = pLeve
3058f 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20  l->iIdxCur;.    
30590 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64        pOp->opcod
30591 65 20 3d 20 4f 50 5f 49 64 78 52 6f 77 69 64 3b  e = OP_IdxRowid;
30592 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
30593 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  f( pOp->opcode==
30594 4f 50 5f 4e 75 6c 6c 52 6f 77 20 26 26 20 75 73  OP_NullRow && us
30595 65 49 6e 64 65 78 4f 6e 6c 79 20 29 7b 0a 20 20  eIndexOnly ){.  
30596 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63          pOp->opc
30597 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20  ode = OP_Noop;. 
30598 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
30599 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
3059a 20 46 69 6e 61 6c 20 63 6c 65 61 6e 75 70 0a 20   Final cleanup. 
3059b 20 2a 2f 0a 20 20 77 68 65 72 65 49 6e 66 6f 46   */.  whereInfoF
3059c 72 65 65 28 64 62 2c 20 70 57 49 6e 66 6f 29 3b  ree(db, pWInfo);
3059d 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a  .  return;.}../*
3059e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
3059f 64 20 6f 66 20 77 68 65 72 65 2e 63 20 2a 2a 2a  d of where.c ***
305a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
305a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
305a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
305a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
305a4 67 69 6e 20 66 69 6c 65 20 70 61 72 73 65 2e 63  gin file parse.c
305a5 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
305a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
305a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
305a8 20 44 72 69 76 65 72 20 74 65 6d 70 6c 61 74 65   Driver template
305a9 20 66 6f 72 20 74 68 65 20 4c 45 4d 4f 4e 20 70   for the LEMON p
305aa 61 72 73 65 72 20 67 65 6e 65 72 61 74 6f 72 2e  arser generator.
305ab 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
305ac 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
305ad 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
305ae 65 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  e code..**.** Th
305af 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 22 6c  is version of "l
305b0 65 6d 70 61 72 2e 63 22 20 69 73 20 6d 6f 64 69  empar.c" is modi
305b1 66 69 65 64 2c 20 73 6c 69 67 68 74 6c 79 2c 20  fied, slightly, 
305b2 66 6f 72 20 75 73 65 20 62 79 20 53 51 4c 69 74  for use by SQLit
305b3 65 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 6d  e..** The only m
305b4 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 61 72 65  odifications are
305b5 20 74 68 65 20 61 64 64 69 74 69 6f 6e 20 6f 66   the addition of
305b6 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 4e 45 56   a couple of NEV
305b7 45 52 28 29 0a 2a 2a 20 6d 61 63 72 6f 73 20 74  ER().** macros t
305b8 6f 20 64 69 73 61 62 6c 65 20 74 65 73 74 73 20  o disable tests 
305b9 74 68 61 74 20 61 72 65 20 6e 65 65 64 65 64 20  that are needed 
305ba 69 6e 20 74 68 65 20 63 61 73 65 20 6f 66 20 61  in the case of a
305bb 20 67 65 6e 65 72 61 6c 0a 2a 2a 20 4c 41 4c 52   general.** LALR
305bc 28 31 29 20 67 72 61 6d 6d 61 72 20 62 75 74 20  (1) grammar but 
305bd 77 68 69 63 68 20 61 72 65 20 61 6c 77 61 79 73  which are always
305be 20 66 61 6c 73 65 20 69 6e 20 74 68 65 0a 2a 2a   false in the.**
305bf 20 73 70 65 63 69 66 69 63 20 67 72 61 6d 6d 61   specific gramma
305c0 72 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65  r used by SQLite
305c1 2e 0a 2a 2f 0a 2f 2a 20 46 69 72 73 74 20 6f 66  ..*/./* First of
305c2 66 2c 20 63 6f 64 65 20 69 73 20 69 6e 63 6c 75  f, code is inclu
305c3 64 65 64 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73  ded that follows
305c4 20 74 68 65 20 22 69 6e 63 6c 75 64 65 22 20 64   the "include" d
305c5 65 63 6c 61 72 61 74 69 6f 6e 0a 2a 2a 20 69 6e  eclaration.** in
305c6 20 74 68 65 20 69 6e 70 75 74 20 67 72 61 6d 6d   the input gramm
305c7 61 72 20 66 69 6c 65 2e 20 2a 2f 0a 0a 0a 2f 2a  ar file. */.../*
305c8 0a 2a 2a 20 44 69 73 61 62 6c 65 20 61 6c 6c 20  .** Disable all 
305c9 65 72 72 6f 72 20 72 65 63 6f 76 65 72 79 20 70  error recovery p
305ca 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 74 68 65  rocessing in the
305cb 20 70 61 72 73 65 72 20 70 75 73 68 2d 64 6f 77   parser push-dow
305cc 6e 0a 2a 2a 20 61 75 74 6f 6d 61 74 6f 6e 2e 0a  n.** automaton..
305cd 2a 2f 0a 23 64 65 66 69 6e 65 20 59 59 4e 4f 45  */.#define YYNOE
305ce 52 52 4f 52 52 45 43 4f 56 45 52 59 20 31 0a 0a  RRORRECOVERY 1..
305cf 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 79 79 74 65 73  /*.** Make yytes
305d0 74 63 61 73 65 28 29 20 74 68 65 20 73 61 6d 65  tcase() the same
305d1 20 61 73 20 74 65 73 74 63 61 73 65 28 29 0a 2a   as testcase().*
305d2 2f 0a 23 64 65 66 69 6e 65 20 79 79 74 65 73 74  /.#define yytest
305d3 63 61 73 65 28 58 29 20 74 65 73 74 63 61 73 65  case(X) testcase
305d4 28 58 29 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  (X)../*.** An in
305d5 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
305d6 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69  tructure holds i
305d7 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
305d8 20 74 68 65 0a 2a 2a 20 4c 49 4d 49 54 20 63 6c   the.** LIMIT cl
305d9 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54  ause of a SELECT
305da 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73   statement..*/.s
305db 74 72 75 63 74 20 4c 69 6d 69 74 56 61 6c 20 7b  truct LimitVal {
305dc 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 3b  .  Expr *pLimit;
305dd 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54      /* The LIMIT
305de 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 4e 55   expression.  NU
305df 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e  LL if there is n
305e0 6f 20 6c 69 6d 69 74 20 2a 2f 0a 20 20 45 78 70  o limit */.  Exp
305e1 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20 2f 2a  r *pOffset;   /*
305e2 20 54 68 65 20 4f 46 46 53 45 54 20 65 78 70 72   The OFFSET expr
305e3 65 73 73 69 6f 6e 2e 20 20 4e 55 4c 4c 20 69 66  ession.  NULL if
305e4 20 74 68 65 72 65 20 69 73 20 6e 6f 6e 65 20 2a   there is none *
305e5 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  /.};../*.** An i
305e6 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
305e7 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65  structure is use
305e8 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 4c  d to store the L
305e9 49 4b 45 2c 0a 2a 2a 20 47 4c 4f 42 2c 20 4e 4f  IKE,.** GLOB, NO
305ea 54 20 4c 49 4b 45 2c 20 61 6e 64 20 4e 4f 54 20  T LIKE, and NOT 
305eb 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 73 2e 0a  GLOB operators..
305ec 2a 2f 0a 73 74 72 75 63 74 20 4c 69 6b 65 4f 70  */.struct LikeOp
305ed 20 7b 0a 20 20 54 6f 6b 65 6e 20 65 4f 70 65 72   {.  Token eOper
305ee 61 74 6f 72 3b 20 20 2f 2a 20 22 6c 69 6b 65 22  ator;  /* "like"
305ef 20 6f 72 20 22 67 6c 6f 62 22 20 6f 72 20 22 72   or "glob" or "r
305f0 65 67 65 78 70 22 20 2a 2f 0a 20 20 69 6e 74 20  egexp" */.  int 
305f1 6e 6f 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  not;         /* 
305f2 54 72 75 65 20 69 66 20 74 68 65 20 4e 4f 54 20  True if the NOT 
305f3 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65  keyword is prese
305f4 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  nt */.};../*.** 
305f5 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
305f6 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
305f7 75 63 74 75 72 65 20 64 65 73 63 72 69 62 65 73  ucture describes
305f8 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 0a   the event of a.
305f9 2a 2a 20 54 52 49 47 47 45 52 2e 20 20 22 61 22  ** TRIGGER.  "a"
305fa 20 69 73 20 74 68 65 20 65 76 65 6e 74 20 74 79   is the event ty
305fb 70 65 2c 20 6f 6e 65 20 6f 66 20 54 4b 5f 55 50  pe, one of TK_UP
305fc 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c  DATE, TK_INSERT,
305fd 0a 2a 2a 20 54 4b 5f 44 45 4c 45 54 45 2c 20 6f  .** TK_DELETE, o
305fe 72 20 54 4b 5f 49 4e 53 54 45 41 44 2e 20 20 49  r TK_INSTEAD.  I
305ff 66 20 74 68 65 20 65 76 65 6e 74 20 69 73 20 6f  f the event is o
30600 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a  f the form.**.**
30601 20 20 20 20 20 20 55 50 44 41 54 45 20 4f 4e 20        UPDATE ON 
30602 28 61 2c 62 2c 63 29 0a 2a 2a 0a 2a 2a 20 54 68  (a,b,c).**.** Th
30603 65 6e 20 74 68 65 20 22 62 22 20 49 64 4c 69 73  en the "b" IdLis
30604 74 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c 69  t records the li
30605 73 74 20 22 61 2c 62 2c 63 22 2e 0a 2a 2f 0a 73  st "a,b,c"..*/.s
30606 74 72 75 63 74 20 54 72 69 67 45 76 65 6e 74 20  truct TrigEvent 
30607 7b 20 69 6e 74 20 61 3b 20 49 64 4c 69 73 74 20  { int a; IdList 
30608 2a 20 62 3b 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  * b; };../*.** A
30609 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
3060a 69 73 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c  is structure hol
3060b 64 73 20 74 68 65 20 41 54 54 41 43 48 20 6b 65  ds the ATTACH ke
3060c 79 20 61 6e 64 20 74 68 65 20 6b 65 79 20 74 79  y and the key ty
3060d 70 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 41 74  pe..*/.struct At
3060e 74 61 63 68 4b 65 79 20 7b 20 69 6e 74 20 74 79  tachKey { int ty
3060f 70 65 3b 20 20 54 6f 6b 65 6e 20 6b 65 79 3b 20  pe;  Token key; 
30610 7d 3b 0a 0a 0a 20 20 2f 2a 20 54 68 69 73 20 69  };...  /* This i
30611 73 20 61 20 75 74 69 6c 69 74 79 20 72 6f 75 74  s a utility rout
30612 69 6e 65 20 75 73 65 64 20 74 6f 20 73 65 74 20  ine used to set 
30613 74 68 65 20 45 78 70 72 53 70 61 6e 2e 7a 53 74  the ExprSpan.zSt
30614 61 72 74 20 61 6e 64 0a 20 20 2a 2a 20 45 78 70  art and.  ** Exp
30615 72 53 70 61 6e 2e 7a 45 6e 64 20 76 61 6c 75 65  rSpan.zEnd value
30616 73 20 6f 66 20 70 4f 75 74 20 73 6f 20 74 68 61  s of pOut so tha
30617 74 20 74 68 65 20 73 70 61 6e 20 63 6f 76 65 72  t the span cover
30618 73 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a 20  s the complete. 
30619 20 2a 2a 20 72 61 6e 67 65 20 6f 66 20 74 65 78   ** range of tex
3061a 74 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68  t beginning with
3061b 20 70 53 74 61 72 74 20 61 6e 64 20 67 6f 69 6e   pStart and goin
3061c 67 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  g to the end of 
3061d 70 45 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73 74 61  pEnd..  */.  sta
3061e 74 69 63 20 76 6f 69 64 20 73 70 61 6e 53 65 74  tic void spanSet
3061f 28 45 78 70 72 53 70 61 6e 20 2a 70 4f 75 74 2c  (ExprSpan *pOut,
30620 20 54 6f 6b 65 6e 20 2a 70 53 74 61 72 74 2c 20   Token *pStart, 
30621 54 6f 6b 65 6e 20 2a 70 45 6e 64 29 7b 0a 20 20  Token *pEnd){.  
30622 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72 74 20 3d    pOut->zStart =
30623 20 70 53 74 61 72 74 2d 3e 7a 3b 0a 20 20 20 20   pStart->z;.    
30624 70 4f 75 74 2d 3e 7a 45 6e 64 20 3d 20 26 70 45  pOut->zEnd = &pE
30625 6e 64 2d 3e 7a 5b 70 45 6e 64 2d 3e 6e 5d 3b 0a  nd->z[pEnd->n];.
30626 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72    }..  /* Constr
30627 75 63 74 20 61 20 6e 65 77 20 45 78 70 72 20 6f  uct a new Expr o
30628 62 6a 65 63 74 20 66 72 6f 6d 20 61 20 73 69 6e  bject from a sin
30629 67 6c 65 20 69 64 65 6e 74 69 66 69 65 72 2e 20  gle identifier. 
3062a 20 55 73 65 20 74 68 65 0a 20 20 2a 2a 20 6e 65   Use the.  ** ne
3062b 77 20 45 78 70 72 20 74 6f 20 70 6f 70 75 6c 61  w Expr to popula
3062c 74 65 20 70 4f 75 74 2e 20 20 53 65 74 20 74 68  te pOut.  Set th
3062d 65 20 73 70 61 6e 20 6f 66 20 70 4f 75 74 20 74  e span of pOut t
3062e 6f 20 62 65 20 74 68 65 20 69 64 65 6e 74 69 66  o be the identif
3062f 69 65 72 0a 20 20 2a 2a 20 74 68 61 74 20 63 72  ier.  ** that cr
30630 65 61 74 65 64 20 74 68 65 20 65 78 70 72 65 73  eated the expres
30631 73 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 74 61  sion..  */.  sta
30632 74 69 63 20 76 6f 69 64 20 73 70 61 6e 45 78 70  tic void spanExp
30633 72 28 45 78 70 72 53 70 61 6e 20 2a 70 4f 75 74  r(ExprSpan *pOut
30634 2c 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  , Parse *pParse,
30635 20 69 6e 74 20 6f 70 2c 20 54 6f 6b 65 6e 20 2a   int op, Token *
30636 70 56 61 6c 75 65 29 7b 0a 20 20 20 20 70 4f 75  pValue){.    pOu
30637 74 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c 69 74  t->pExpr = sqlit
30638 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
30639 6f 70 2c 20 30 2c 20 30 2c 20 70 56 61 6c 75 65  op, 0, 0, pValue
3063a 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 53 74  );.    pOut->zSt
3063b 61 72 74 20 3d 20 70 56 61 6c 75 65 2d 3e 7a 3b  art = pValue->z;
3063c 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e 64 20  .    pOut->zEnd 
3063d 3d 20 26 70 56 61 6c 75 65 2d 3e 7a 5b 70 56 61  = &pValue->z[pVa
3063e 6c 75 65 2d 3e 6e 5d 3b 0a 20 20 7d 0a 0a 20 20  lue->n];.  }..  
3063f 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  /* This routine 
30640 63 6f 6e 73 74 72 75 63 74 73 20 61 20 62 69 6e  constructs a bin
30641 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 6e  ary expression n
30642 6f 64 65 20 6f 75 74 20 6f 66 20 74 77 6f 20 45  ode out of two E
30643 78 70 72 53 70 61 6e 0a 20 20 2a 2a 20 6f 62 6a  xprSpan.  ** obj
30644 65 63 74 73 20 61 6e 64 20 75 73 65 73 20 74 68  ects and uses th
30645 65 20 72 65 73 75 6c 74 20 74 6f 20 70 6f 70 75  e result to popu
30646 6c 61 74 65 20 61 20 6e 65 77 20 45 78 70 72 53  late a new ExprS
30647 70 61 6e 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2f  pan object..  */
30648 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20 73  .  static void s
30649 70 61 6e 42 69 6e 61 72 79 45 78 70 72 28 0a 20  panBinaryExpr(. 
3064a 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70 4f 75     ExprSpan *pOu
3064b 74 2c 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20  t,     /* Write 
3064c 74 68 65 20 72 65 73 75 6c 74 20 68 65 72 65 20  the result here 
3064d 2a 2f 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50  */.    Parse *pP
3064e 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 54 68  arse,      /* Th
3064f 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  e parsing contex
30650 74 2e 20 20 45 72 72 6f 72 73 20 61 63 63 75 6d  t.  Errors accum
30651 75 6c 61 74 65 20 68 65 72 65 20 2a 2f 0a 20 20  ulate here */.  
30652 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20    int op,       
30653 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 69 6e        /* The bin
30654 61 72 79 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f  ary operation */
30655 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70  .    ExprSpan *p
30656 4c 65 66 74 2c 20 20 20 20 2f 2a 20 54 68 65 20  Left,    /* The 
30657 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  left operand */.
30658 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70 52      ExprSpan *pR
30659 69 67 68 74 20 20 20 20 2f 2a 20 54 68 65 20 72  ight    /* The r
3065a 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  ight operand */.
3065b 20 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 70    ){.    pOut->p
3065c 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45  Expr = sqlite3PE
3065d 78 70 72 28 70 50 61 72 73 65 2c 20 6f 70 2c 20  xpr(pParse, op, 
3065e 70 4c 65 66 74 2d 3e 70 45 78 70 72 2c 20 70 52  pLeft->pExpr, pR
3065f 69 67 68 74 2d 3e 70 45 78 70 72 2c 20 30 29 3b  ight->pExpr, 0);
30660 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72  .    pOut->zStar
30661 74 20 3d 20 70 4c 65 66 74 2d 3e 7a 53 74 61 72  t = pLeft->zStar
30662 74 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e  t;.    pOut->zEn
30663 64 20 3d 20 70 52 69 67 68 74 2d 3e 7a 45 6e 64  d = pRight->zEnd
30664 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 73  ;.  }..  /* Cons
30665 74 72 75 63 74 20 61 6e 20 65 78 70 72 65 73 73  truct an express
30666 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61 20 75  ion node for a u
30667 6e 61 72 79 20 70 6f 73 74 66 69 78 20 6f 70 65  nary postfix ope
30668 72 61 74 6f 72 0a 20 20 2a 2f 0a 20 20 73 74 61  rator.  */.  sta
30669 74 69 63 20 76 6f 69 64 20 73 70 61 6e 55 6e 61  tic void spanUna
3066a 72 79 50 6f 73 74 66 69 78 28 0a 20 20 20 20 45  ryPostfix(.    E
3066b 78 70 72 53 70 61 6e 20 2a 70 4f 75 74 2c 20 20  xprSpan *pOut,  
3066c 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
3066d 68 65 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f  he new expressio
3066e 6e 20 6e 6f 64 65 20 68 65 72 65 20 2a 2f 0a 20  n node here */. 
3066f 20 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65     Parse *pParse
30670 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  ,         /* Par
30671 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 74 6f 20  sing context to 
30672 72 65 63 6f 72 64 20 65 72 72 6f 72 73 20 2a 2f  record errors */
30673 0a 20 20 20 20 69 6e 74 20 6f 70 2c 20 20 20 20  .    int op,    
30674 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
30675 68 65 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20  he operator */. 
30676 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70 4f 70     ExprSpan *pOp
30677 65 72 61 6e 64 2c 20 20 20 20 2f 2a 20 54 68 65  erand,    /* The
30678 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20   operand */.    
30679 54 6f 6b 65 6e 20 2a 70 50 6f 73 74 4f 70 20 20  Token *pPostOp  
3067a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70         /* The op
3067b 65 72 61 6e 64 20 74 6f 6b 65 6e 20 66 6f 72 20  erand token for 
3067c 73 65 74 74 69 6e 67 20 74 68 65 20 73 70 61 6e  setting the span
3067d 20 2a 2f 0a 20 20 29 7b 0a 20 20 20 20 70 4f 75   */.  ){.    pOu
3067e 74 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c 69 74  t->pExpr = sqlit
3067f 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
30680 6f 70 2c 20 70 4f 70 65 72 61 6e 64 2d 3e 70 45  op, pOperand->pE
30681 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  xpr, 0, 0);.    
30682 70 4f 75 74 2d 3e 7a 53 74 61 72 74 20 3d 20 70  pOut->zStart = p
30683 4f 70 65 72 61 6e 64 2d 3e 7a 53 74 61 72 74 3b  Operand->zStart;
30684 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e 64 20  .    pOut->zEnd 
30685 3d 20 26 70 50 6f 73 74 4f 70 2d 3e 7a 5b 70 50  = &pPostOp->z[pP
30686 6f 73 74 4f 70 2d 3e 6e 5d 3b 0a 20 20 7d 20 20  ostOp->n];.  }  
30687 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30688 20 20 20 20 20 20 20 20 20 0a 0a 20 20 2f 2a 20           ..  /* 
30689 43 6f 6e 73 74 72 75 63 74 20 61 6e 20 65 78 70  Construct an exp
3068a 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72  ression node for
3068b 20 61 20 75 6e 61 72 79 20 70 72 65 66 69 78 20   a unary prefix 
3068c 6f 70 65 72 61 74 6f 72 0a 20 20 2a 2f 0a 20 20  operator.  */.  
3068d 73 74 61 74 69 63 20 76 6f 69 64 20 73 70 61 6e  static void span
3068e 55 6e 61 72 79 50 72 65 66 69 78 28 0a 20 20 20  UnaryPrefix(.   
3068f 20 45 78 70 72 53 70 61 6e 20 2a 70 4f 75 74 2c   ExprSpan *pOut,
30690 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
30691 20 74 68 65 20 6e 65 77 20 65 78 70 72 65 73 73   the new express
30692 69 6f 6e 20 6e 6f 64 65 20 68 65 72 65 20 2a 2f  ion node here */
30693 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50 61 72  .    Parse *pPar
30694 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50  se,         /* P
30695 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 74  arsing context t
30696 6f 20 72 65 63 6f 72 64 20 65 72 72 6f 72 73 20  o record errors 
30697 2a 2f 0a 20 20 20 20 69 6e 74 20 6f 70 2c 20 20  */.    int op,  
30698 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
30699 20 54 68 65 20 6f 70 65 72 61 74 6f 72 20 2a 2f   The operator */
3069a 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70  .    ExprSpan *p
3069b 4f 70 65 72 61 6e 64 2c 20 20 20 20 2f 2a 20 54  Operand,    /* T
3069c 68 65 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  he operand */.  
3069d 20 20 54 6f 6b 65 6e 20 2a 70 50 72 65 4f 70 20    Token *pPreOp 
3069e 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f          /* The o
3069f 70 65 72 61 6e 64 20 74 6f 6b 65 6e 20 66 6f 72  perand token for
306a0 20 73 65 74 74 69 6e 67 20 74 68 65 20 73 70 61   setting the spa
306a1 6e 20 2a 2f 0a 20 20 29 7b 0a 20 20 20 20 70 4f  n */.  ){.    pO
306a2 75 74 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c 69  ut->pExpr = sqli
306a3 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
306a4 20 6f 70 2c 20 70 4f 70 65 72 61 6e 64 2d 3e 70   op, pOperand->p
306a5 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Expr, 0, 0);.   
306a6 20 70 4f 75 74 2d 3e 7a 53 74 61 72 74 20 3d 20   pOut->zStart = 
306a7 70 50 72 65 4f 70 2d 3e 7a 3b 0a 20 20 20 20 70  pPreOp->z;.    p
306a8 4f 75 74 2d 3e 7a 45 6e 64 20 3d 20 70 4f 70 65  Out->zEnd = pOpe
306a9 72 61 6e 64 2d 3e 7a 45 6e 64 3b 0a 20 20 7d 0a  rand->zEnd;.  }.
306aa 2f 2a 20 4e 65 78 74 20 69 73 20 61 6c 6c 20 74  /* Next is all t
306ab 6f 6b 65 6e 20 76 61 6c 75 65 73 2c 20 69 6e 20  oken values, in 
306ac 61 20 66 6f 72 6d 20 73 75 69 74 61 62 6c 65 20  a form suitable 
306ad 66 6f 72 20 75 73 65 20 62 79 20 6d 61 6b 65 68  for use by makeh
306ae 65 61 64 65 72 73 2e 0a 2a 2a 20 54 68 69 73 20  eaders..** This 
306af 73 65 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20  section will be 
306b0 6e 75 6c 6c 20 75 6e 6c 65 73 73 20 6c 65 6d 6f  null unless lemo
306b1 6e 20 69 73 20 72 75 6e 20 77 69 74 68 20 74 68  n is run with th
306b2 65 20 2d 6d 20 73 77 69 74 63 68 2e 0a 2a 2f 0a  e -m switch..*/.
306b3 2f 2a 20 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e  /* .** These con
306b4 73 74 61 6e 74 73 20 28 61 6c 6c 20 67 65 6e 65  stants (all gene
306b5 72 61 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61  rated automatica
306b6 6c 6c 79 20 62 79 20 74 68 65 20 70 61 72 73 65  lly by the parse
306b7 72 20 67 65 6e 65 72 61 74 6f 72 29 0a 2a 2a 20  r generator).** 
306b8 73 70 65 63 69 66 79 20 74 68 65 20 76 61 72 69  specify the vari
306b9 6f 75 73 20 6b 69 6e 64 73 20 6f 66 20 74 6f 6b  ous kinds of tok
306ba 65 6e 73 20 28 74 65 72 6d 69 6e 61 6c 73 29 20  ens (terminals) 
306bb 74 68 61 74 20 74 68 65 20 70 61 72 73 65 72 0a  that the parser.
306bc 2a 2a 20 75 6e 64 65 72 73 74 61 6e 64 73 2e 20  ** understands. 
306bd 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 73 79 6d 62  .**.** Each symb
306be 6f 6c 20 68 65 72 65 20 69 73 20 61 20 74 65 72  ol here is a ter
306bf 6d 69 6e 61 6c 20 73 79 6d 62 6f 6c 20 69 6e 20  minal symbol in 
306c0 74 68 65 20 67 72 61 6d 6d 61 72 2e 0a 2a 2f 0a  the grammar..*/.
306c1 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
306c2 20 49 4e 54 45 52 46 41 43 45 20 6d 61 63 72 6f   INTERFACE macro
306c3 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a   is defined..*/.
306c4 23 69 66 6e 64 65 66 20 49 4e 54 45 52 46 41 43  #ifndef INTERFAC
306c5 45 0a 23 20 64 65 66 69 6e 65 20 49 4e 54 45 52  E.# define INTER
306c6 46 41 43 45 20 31 0a 23 65 6e 64 69 66 0a 2f 2a  FACE 1.#endif./*
306c7 20 54 68 65 20 6e 65 78 74 20 74 68 69 6e 67 20   The next thing 
306c8 69 6e 63 6c 75 64 65 64 20 69 73 20 73 65 72 69  included is seri
306c9 65 73 20 6f 66 20 64 65 66 69 6e 65 73 20 77 68  es of defines wh
306ca 69 63 68 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 76  ich control.** v
306cb 61 72 69 6f 75 73 20 61 73 70 65 63 74 73 20 6f  arious aspects o
306cc 66 20 74 68 65 20 67 65 6e 65 72 61 74 65 64 20  f the generated 
306cd 70 61 72 73 65 72 2e 0a 2a 2a 20 20 20 20 59 59  parser..**    YY
306ce 43 4f 44 45 54 59 50 45 20 20 20 20 20 20 20 20  CODETYPE        
306cf 20 69 73 20 74 68 65 20 64 61 74 61 20 74 79 70   is the data typ
306d0 65 20 75 73 65 64 20 66 6f 72 20 73 74 6f 72 69  e used for stori
306d1 6e 67 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 20  ng terminal.**  
306d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
306d3 20 20 20 20 20 61 6e 64 20 6e 6f 6e 74 65 72 6d       and nonterm
306d4 69 6e 61 6c 20 6e 75 6d 62 65 72 73 2e 20 20 22  inal numbers.  "
306d5 75 6e 73 69 67 6e 65 64 20 63 68 61 72 22 20 69  unsigned char" i
306d6 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  s.**            
306d7 20 20 20 20 20 20 20 20 20 20 20 75 73 65 64 20             used 
306d8 69 66 20 74 68 65 72 65 20 61 72 65 20 66 65 77  if there are few
306d9 65 72 20 74 68 61 6e 20 32 35 30 20 74 65 72 6d  er than 250 term
306da 69 6e 61 6c 73 0a 2a 2a 20 20 20 20 20 20 20 20  inals.**        
306db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
306dc 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 73 2e  nd nonterminals.
306dd 20 20 22 69 6e 74 22 20 69 73 20 75 73 65 64 20    "int" is used 
306de 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 20 20 20  otherwise..**   
306df 20 59 59 4e 4f 43 4f 44 45 20 20 20 20 20 20 20   YYNOCODE       
306e0 20 20 20 20 69 73 20 61 20 6e 75 6d 62 65 72 20      is a number 
306e1 6f 66 20 74 79 70 65 20 59 59 43 4f 44 45 54 59  of type YYCODETY
306e2 50 45 20 77 68 69 63 68 20 63 6f 72 72 65 73 70  PE which corresp
306e3 6f 6e 64 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  onds.**         
306e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
306e5 20 6e 6f 20 6c 65 67 61 6c 20 74 65 72 6d 69 6e   no legal termin
306e6 61 6c 20 6f 72 20 6e 6f 6e 74 65 72 6d 69 6e 61  al or nontermina
306e7 6c 20 6e 75 6d 62 65 72 2e 20 20 54 68 69 73 0a  l number.  This.
306e8 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
306e9 20 20 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20           number 
306ea 69 73 20 75 73 65 64 20 74 6f 20 66 69 6c 6c 20  is used to fill 
306eb 69 6e 20 65 6d 70 74 79 20 73 6c 6f 74 73 20 6f  in empty slots o
306ec 66 20 74 68 65 20 68 61 73 68 20 0a 2a 2a 20 20  f the hash .**  
306ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
306ee 20 20 20 20 20 74 61 62 6c 65 2e 0a 2a 2a 20 20       table..**  
306ef 20 20 59 59 46 41 4c 4c 42 41 43 4b 20 20 20 20    YYFALLBACK    
306f0 20 20 20 20 20 49 66 20 64 65 66 69 6e 65 64 2c       If defined,
306f1 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20   this indicates 
306f2 74 68 61 74 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  that one or more
306f3 20 74 6f 6b 65 6e 73 0a 2a 2a 20 20 20 20 20 20   tokens.**      
306f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
306f5 20 68 61 76 65 20 66 61 6c 6c 2d 62 61 63 6b 20   have fall-back 
306f6 76 61 6c 75 65 73 20 77 68 69 63 68 20 73 68 6f  values which sho
306f7 75 6c 64 20 62 65 20 75 73 65 64 20 69 66 20 74  uld be used if t
306f8 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  he.**           
306f9 20 20 20 20 20 20 20 20 20 20 20 20 6f 72 69 67              orig
306fa 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 74 68  inal value of th
306fb 65 20 74 6f 6b 65 6e 20 77 69 6c 6c 20 6e 6f 74  e token will not
306fc 20 70 61 72 73 65 2e 0a 2a 2a 20 20 20 20 59 59   parse..**    YY
306fd 41 43 54 49 4f 4e 54 59 50 45 20 20 20 20 20 20  ACTIONTYPE      
306fe 20 69 73 20 74 68 65 20 64 61 74 61 20 74 79 70   is the data typ
306ff 65 20 75 73 65 64 20 66 6f 72 20 73 74 6f 72 69  e used for stori
30700 6e 67 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 20  ng terminal.**  
30701 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30702 20 20 20 20 20 61 6e 64 20 6e 6f 6e 74 65 72 6d       and nonterm
30703 69 6e 61 6c 20 6e 75 6d 62 65 72 73 2e 20 20 22  inal numbers.  "
30704 75 6e 73 69 67 6e 65 64 20 63 68 61 72 22 20 69  unsigned char" i
30705 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  s.**            
30706 20 20 20 20 20 20 20 20 20 20 20 75 73 65 64 20             used 
30707 69 66 20 74 68 65 72 65 20 61 72 65 20 66 65 77  if there are few
30708 65 72 20 74 68 61 6e 20 32 35 30 20 72 75 6c 65  er than 250 rule
30709 73 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20  s and.**        
3070a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
3070b 74 61 74 65 73 20 63 6f 6d 62 69 6e 65 64 2e 20  tates combined. 
3070c 20 22 69 6e 74 22 20 69 73 20 75 73 65 64 20 6f   "int" is used o
3070d 74 68 65 72 77 69 73 65 2e 0a 2a 2a 20 20 20 20  therwise..**    
3070e 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b  sqlite3ParserTOK
3070f 45 4e 54 59 50 45 20 20 20 20 20 69 73 20 74 68  ENTYPE     is th
30710 65 20 64 61 74 61 20 74 79 70 65 20 75 73 65 64  e data type used
30711 20 66 6f 72 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e   for minor token
30712 73 20 67 69 76 65 6e 20 0a 2a 2a 20 20 20 20 20  s given .**     
30713 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30714 20 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68    directly to th
30715 65 20 70 61 72 73 65 72 20 66 72 6f 6d 20 74 68  e parser from th
30716 65 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2a 20  e tokenizer..** 
30717 20 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 20     YYMINORTYPE  
30718 20 20 20 20 20 20 69 73 20 74 68 65 20 64 61 74        is the dat
30719 61 20 74 79 70 65 20 75 73 65 64 20 66 6f 72 20  a type used for 
3071a 61 6c 6c 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 73  all minor tokens
3071b 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ..**            
3071c 20 20 20 20 20 20 20 20 20 20 20 54 68 69 73 20             This 
3071d 69 73 20 74 79 70 69 63 61 6c 6c 79 20 61 20 75  is typically a u
3071e 6e 69 6f 6e 20 6f 66 20 6d 61 6e 79 20 74 79 70  nion of many typ
3071f 65 73 2c 20 6f 6e 65 20 6f 66 0a 2a 2a 20 20 20  es, one of.**   
30720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30721 20 20 20 20 77 68 69 63 68 20 69 73 20 73 71 6c      which is sql
30722 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45 4e 54  ite3ParserTOKENT
30723 59 50 45 2e 20 20 54 68 65 20 65 6e 74 72 79 20  YPE.  The entry 
30724 69 6e 20 74 68 65 20 75 6e 69 6f 6e 0a 2a 2a 20  in the union.** 
30725 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30726 20 20 20 20 20 20 66 6f 72 20 62 61 73 65 20 74        for base t
30727 6f 6b 65 6e 73 20 69 73 20 63 61 6c 6c 65 64 20  okens is called 
30728 22 79 79 30 22 2e 0a 2a 2a 20 20 20 20 59 59 53  "yy0"..**    YYS
30729 54 41 43 4b 44 45 50 54 48 20 20 20 20 20 20 20  TACKDEPTH       
3072a 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 64  is the maximum d
3072b 65 70 74 68 20 6f 66 20 74 68 65 20 70 61 72 73  epth of the pars
3072c 65 72 27 73 20 73 74 61 63 6b 2e 20 20 49 66 0a  er's stack.  If.
3072d 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
3072e 20 20 20 20 20 20 20 20 20 7a 65 72 6f 20 74 68           zero th
3072f 65 20 73 74 61 63 6b 20 69 73 20 64 79 6e 61 6d  e stack is dynam
30730 69 63 61 6c 6c 79 20 73 69 7a 65 64 20 75 73 69  ically sized usi
30731 6e 67 20 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20  ng realloc().** 
30732 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72     sqlite3Parser
30733 41 52 47 5f 53 44 45 43 4c 20 20 20 20 20 41 20  ARG_SDECL     A 
30734 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 20  static variable 
30735 64 65 63 6c 61 72 61 74 69 6f 6e 20 66 6f 72 20  declaration for 
30736 74 68 65 20 25 65 78 74 72 61 5f 61 72 67 75 6d  the %extra_argum
30737 65 6e 74 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65  ent.**    sqlite
30738 33 50 61 72 73 65 72 41 52 47 5f 50 44 45 43 4c  3ParserARG_PDECL
30739 20 20 20 20 20 41 20 70 61 72 61 6d 65 74 65 72       A parameter
3073a 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 66 6f 72   declaration for
3073b 20 74 68 65 20 25 65 78 74 72 61 5f 61 72 67 75   the %extra_argu
3073c 6d 65 6e 74 0a 2a 2a 20 20 20 20 73 71 6c 69 74  ment.**    sqlit
3073d 65 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f 52  e3ParserARG_STOR
3073e 45 20 20 20 20 20 43 6f 64 65 20 74 6f 20 73 74  E     Code to st
3073f 6f 72 65 20 25 65 78 74 72 61 5f 61 72 67 75 6d  ore %extra_argum
30740 65 6e 74 20 69 6e 74 6f 20 79 79 70 50 61 72 73  ent into yypPars
30741 65 72 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33  er.**    sqlite3
30742 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48 20  ParserARG_FETCH 
30743 20 20 20 20 43 6f 64 65 20 74 6f 20 65 78 74 72      Code to extr
30744 61 63 74 20 25 65 78 74 72 61 5f 61 72 67 75 6d  act %extra_argum
30745 65 6e 74 20 66 72 6f 6d 20 79 79 70 50 61 72 73  ent from yypPars
30746 65 72 0a 2a 2a 20 20 20 20 59 59 4e 53 54 41 54  er.**    YYNSTAT
30747 45 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20  E           the 
30748 63 6f 6d 62 69 6e 65 64 20 6e 75 6d 62 65 72 20  combined number 
30749 6f 66 20 73 74 61 74 65 73 2e 0a 2a 2a 20 20 20  of states..**   
3074a 20 59 59 4e 52 55 4c 45 20 20 20 20 20 20 20 20   YYNRULE        
3074b 20 20 20 20 74 68 65 20 6e 75 6d 62 65 72 20 6f      the number o
3074c 66 20 72 75 6c 65 73 20 69 6e 20 74 68 65 20 67  f rules in the g
3074d 72 61 6d 6d 61 72 0a 2a 2a 20 20 20 20 59 59 45  rammar.**    YYE
3074e 52 52 4f 52 53 59 4d 42 4f 4c 20 20 20 20 20 20  RRORSYMBOL      
3074f 69 73 20 74 68 65 20 63 6f 64 65 20 6e 75 6d 62  is the code numb
30750 65 72 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20  er of the error 
30751 73 79 6d 62 6f 6c 2e 20 20 49 66 20 6e 6f 74 0a  symbol.  If not.
30752 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
30753 20 20 20 20 20 20 20 20 20 64 65 66 69 6e 65 64           defined
30754 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 20 65 72 72  , then do no err
30755 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a  or processing..*
30756 2f 0a 23 64 65 66 69 6e 65 20 59 59 43 4f 44 45  /.#define YYCODE
30757 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 63 68  TYPE unsigned ch
30758 61 72 0a 23 64 65 66 69 6e 65 20 59 59 4e 4f 43  ar.#define YYNOC
30759 4f 44 45 20 32 35 34 0a 23 64 65 66 69 6e 65 20  ODE 254.#define 
3075a 59 59 41 43 54 49 4f 4e 54 59 50 45 20 75 6e 73  YYACTIONTYPE uns
3075b 69 67 6e 65 64 20 73 68 6f 72 74 20 69 6e 74 0a  igned short int.
3075c 23 64 65 66 69 6e 65 20 59 59 57 49 4c 44 43 41  #define YYWILDCA
3075d 52 44 20 36 37 0a 23 64 65 66 69 6e 65 20 73 71  RD 67.#define sq
3075e 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45 4e  lite3ParserTOKEN
3075f 54 59 50 45 20 54 6f 6b 65 6e 0a 74 79 70 65 64  TYPE Token.typed
30760 65 66 20 75 6e 69 6f 6e 20 7b 0a 20 20 69 6e 74  ef union {.  int
30761 20 79 79 69 6e 69 74 3b 0a 20 20 73 71 6c 69 74   yyinit;.  sqlit
30762 65 33 50 61 72 73 65 72 54 4f 4b 45 4e 54 59 50  e3ParserTOKENTYP
30763 45 20 79 79 30 3b 0a 20 20 53 65 6c 65 63 74 2a  E yy0;.  Select*
30764 20 79 79 33 3b 0a 20 20 45 78 70 72 4c 69 73 74   yy3;.  ExprList
30765 2a 20 79 79 31 34 3b 0a 20 20 53 72 63 4c 69 73  * yy14;.  SrcLis
30766 74 2a 20 79 79 36 35 3b 0a 20 20 73 74 72 75 63  t* yy65;.  struc
30767 74 20 4c 69 6b 65 4f 70 20 79 79 39 36 3b 0a 20  t LikeOp yy96;. 
30768 20 45 78 70 72 2a 20 79 79 31 33 32 3b 0a 20 20   Expr* yy132;.  
30769 75 38 20 79 79 31 38 36 3b 0a 20 20 69 6e 74 20  u8 yy186;.  int 
3076a 79 79 33 32 38 3b 0a 20 20 45 78 70 72 53 70 61  yy328;.  ExprSpa
3076b 6e 20 79 79 33 34 36 3b 0a 20 20 73 74 72 75 63  n yy346;.  struc
3076c 74 20 54 72 69 67 45 76 65 6e 74 20 79 79 33 37  t TrigEvent yy37
3076d 38 3b 0a 20 20 49 64 4c 69 73 74 2a 20 79 79 34  8;.  IdList* yy4
3076e 30 38 3b 0a 20 20 73 74 72 75 63 74 20 7b 69 6e  08;.  struct {in
3076f 74 20 76 61 6c 75 65 3b 20 69 6e 74 20 6d 61 73  t value; int mas
30770 6b 3b 7d 20 79 79 34 32 39 3b 0a 20 20 54 72 69  k;} yy429;.  Tri
30771 67 67 65 72 53 74 65 70 2a 20 79 79 34 37 33 3b  ggerStep* yy473;
30772 0a 20 20 73 74 72 75 63 74 20 4c 69 6d 69 74 56  .  struct LimitV
30773 61 6c 20 79 79 34 37 36 3b 0a 7d 20 59 59 4d 49  al yy476;.} YYMI
30774 4e 4f 52 54 59 50 45 3b 0a 23 69 66 6e 64 65 66  NORTYPE;.#ifndef
30775 20 59 59 53 54 41 43 4b 44 45 50 54 48 0a 23 64   YYSTACKDEPTH.#d
30776 65 66 69 6e 65 20 59 59 53 54 41 43 4b 44 45 50  efine YYSTACKDEP
30777 54 48 20 31 30 30 0a 23 65 6e 64 69 66 0a 23 64  TH 100.#endif.#d
30778 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72  efine sqlite3Par
30779 73 65 72 41 52 47 5f 53 44 45 43 4c 20 50 61 72  serARG_SDECL Par
3077a 73 65 20 2a 70 50 61 72 73 65 3b 0a 23 64 65 66  se *pParse;.#def
3077b 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73 65  ine sqlite3Parse
3077c 72 41 52 47 5f 50 44 45 43 4c 20 2c 50 61 72 73  rARG_PDECL ,Pars
3077d 65 20 2a 70 50 61 72 73 65 0a 23 64 65 66 69 6e  e *pParse.#defin
3077e 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41  e sqlite3ParserA
3077f 52 47 5f 46 45 54 43 48 20 50 61 72 73 65 20 2a  RG_FETCH Parse *
30780 70 50 61 72 73 65 20 3d 20 79 79 70 50 61 72 73  pParse = yypPars
30781 65 72 2d 3e 70 50 61 72 73 65 0a 23 64 65 66 69  er->pParse.#defi
30782 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  ne sqlite3Parser
30783 41 52 47 5f 53 54 4f 52 45 20 79 79 70 50 61 72  ARG_STORE yypPar
30784 73 65 72 2d 3e 70 50 61 72 73 65 20 3d 20 70 50  ser->pParse = pP
30785 61 72 73 65 0a 23 64 65 66 69 6e 65 20 59 59 4e  arse.#define YYN
30786 53 54 41 54 45 20 36 32 39 0a 23 64 65 66 69 6e  STATE 629.#defin
30787 65 20 59 59 4e 52 55 4c 45 20 33 32 39 0a 23 64  e YYNRULE 329.#d
30788 65 66 69 6e 65 20 59 59 46 41 4c 4c 42 41 43 4b  efine YYFALLBACK
30789 20 31 0a 23 64 65 66 69 6e 65 20 59 59 5f 4e 4f   1.#define YY_NO
3078a 5f 41 43 54 49 4f 4e 20 20 20 20 20 20 28 59 59  _ACTION      (YY
3078b 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b 32  NSTATE+YYNRULE+2
3078c 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 41 43 43  ).#define YY_ACC
3078d 45 50 54 5f 41 43 54 49 4f 4e 20 20 28 59 59 4e  EPT_ACTION  (YYN
3078e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b 31 29  STATE+YYNRULE+1)
3078f 0a 23 64 65 66 69 6e 65 20 59 59 5f 45 52 52 4f  .#define YY_ERRO
30790 52 5f 41 43 54 49 4f 4e 20 20 20 28 59 59 4e 53  R_ACTION   (YYNS
30791 54 41 54 45 2b 59 59 4e 52 55 4c 45 29 0a 0a 2f  TATE+YYNRULE)../
30792 2a 20 54 68 65 20 79 79 7a 65 72 6f 6d 69 6e 6f  * The yyzeromino
30793 72 20 63 6f 6e 73 74 61 6e 74 20 69 73 20 75 73  r constant is us
30794 65 64 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65  ed to initialize
30795 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 0a 2a 2a   instances of.**
30796 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 6f 62 6a   YYMINORTYPE obj
30797 65 63 74 73 20 74 6f 20 7a 65 72 6f 2e 20 2a 2f  ects to zero. */
30798 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59 59  .static const YY
30799 4d 49 4e 4f 52 54 59 50 45 20 79 79 7a 65 72 6f  MINORTYPE yyzero
3079a 6d 69 6e 6f 72 20 3d 20 7b 20 30 20 7d 3b 0a 0a  minor = { 0 };..
3079b 2f 2a 20 44 65 66 69 6e 65 20 74 68 65 20 79 79  /* Define the yy
3079c 74 65 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f  testcase() macro
3079d 20 74 6f 20 62 65 20 61 20 6e 6f 2d 6f 70 20 69   to be a no-op i
3079e 66 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  f is not already
3079f 20 64 65 66 69 6e 65 64 0a 2a 2a 20 6f 74 68 65   defined.** othe
307a0 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 41 70 70  rwise..**.** App
307a1 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 63 68  lications can ch
307a2 6f 6f 73 65 20 74 6f 20 64 65 66 69 6e 65 20 79  oose to define y
307a3 79 74 65 73 74 63 61 73 65 28 29 20 69 6e 20 74  ytestcase() in t
307a4 68 65 20 25 69 6e 63 6c 75 64 65 20 73 65 63 74  he %include sect
307a5 69 6f 6e 0a 2a 2a 20 74 6f 20 61 20 6d 61 63 72  ion.** to a macr
307a6 6f 20 74 68 61 74 20 63 61 6e 20 61 73 73 69 73  o that can assis
307a7 74 20 69 6e 20 76 65 72 69 66 79 69 6e 67 20 63  t in verifying c
307a8 6f 64 65 20 63 6f 76 65 72 61 67 65 2e 20 20 46  ode coverage.  F
307a9 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e 0a 2a 2a  or production.**
307aa 20 63 6f 64 65 20 74 68 65 20 79 79 74 65 73 74   code the yytest
307ab 63 61 73 65 28 29 20 6d 61 63 72 6f 20 73 68 6f  case() macro sho
307ac 75 6c 64 20 62 65 20 74 75 72 6e 65 64 20 6f 66  uld be turned of
307ad 66 2e 20 20 42 75 74 20 69 74 20 69 73 20 75 73  f.  But it is us
307ae 65 66 75 6c 0a 2a 2a 20 66 6f 72 20 74 65 73 74  eful.** for test
307af 69 6e 67 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ing..*/.#ifndef 
307b0 79 79 74 65 73 74 63 61 73 65 0a 23 20 64 65 66  yytestcase.# def
307b1 69 6e 65 20 79 79 74 65 73 74 63 61 73 65 28 58  ine yytestcase(X
307b2 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20 4e 65  ).#endif.../* Ne
307b3 78 74 20 61 72 65 20 74 68 65 20 74 61 62 6c 65  xt are the table
307b4 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
307b5 69 6e 65 20 77 68 61 74 20 61 63 74 69 6f 6e 20  ine what action 
307b6 74 6f 20 74 61 6b 65 20 62 61 73 65 64 20 6f 6e  to take based on
307b7 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20   the.** current 
307b8 73 74 61 74 65 20 61 6e 64 20 6c 6f 6f 6b 61 68  state and lookah
307b9 65 61 64 20 74 6f 6b 65 6e 2e 20 20 54 68 65 73  ead token.  Thes
307ba 65 20 74 61 62 6c 65 73 20 61 72 65 20 75 73 65  e tables are use
307bb 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a  d to implement.*
307bc 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  * functions that
307bd 20 74 61 6b 65 20 61 20 73 74 61 74 65 20 6e 75   take a state nu
307be 6d 62 65 72 20 61 6e 64 20 6c 6f 6f 6b 61 68 65  mber and lookahe
307bf 61 64 20 76 61 6c 75 65 20 61 6e 64 20 72 65 74  ad value and ret
307c0 75 72 6e 20 61 6e 0a 2a 2a 20 61 63 74 69 6f 6e  urn an.** action
307c1 20 69 6e 74 65 67 65 72 2e 20 20 0a 2a 2a 0a 2a   integer.  .**.*
307c2 2a 20 53 75 70 70 6f 73 65 20 74 68 65 20 61 63  * Suppose the ac
307c3 74 69 6f 6e 20 69 6e 74 65 67 65 72 20 69 73 20  tion integer is 
307c4 4e 2e 20 20 54 68 65 6e 20 74 68 65 20 61 63 74  N.  Then the act
307c5 69 6f 6e 20 69 73 20 64 65 74 65 72 6d 69 6e 65  ion is determine
307c6 64 20 61 73 0a 2a 2a 20 66 6f 6c 6c 6f 77 73 0a  d as.** follows.
307c7 2a 2a 0a 2a 2a 20 20 20 30 20 3c 3d 20 4e 20 3c  **.**   0 <= N <
307c8 20 59 59 4e 53 54 41 54 45 20 20 20 20 20 20 20   YYNSTATE       
307c9 20 20 20 20 20 20 20 20 20 20 20 53 68 69 66 74             Shift
307ca 20 4e 2e 20 20 54 68 61 74 20 69 73 2c 20 70 75   N.  That is, pu
307cb 73 68 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61 64  sh the lookahead
307cc 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
307cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
307ce 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 6f           token o
307cf 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20 61 6e  nto the stack an
307d0 64 20 67 6f 74 6f 20 73 74 61 74 65 20 4e 2e 0a  d goto state N..
307d1 2a 2a 0a 2a 2a 20 20 20 59 59 4e 53 54 41 54 45  **.**   YYNSTATE
307d2 20 3c 3d 20 4e 20 3c 20 59 59 4e 53 54 41 54 45   <= N < YYNSTATE
307d3 2b 59 59 4e 52 55 4c 45 20 20 20 52 65 64 75 63  +YYNRULE   Reduc
307d4 65 20 62 79 20 72 75 6c 65 20 4e 2d 59 59 4e 53  e by rule N-YYNS
307d5 54 41 54 45 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e 20  TATE..**.**   N 
307d6 3d 3d 20 59 59 4e 53 54 41 54 45 2b 59 59 4e 52  == YYNSTATE+YYNR
307d7 55 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ULE             
307d8 20 41 20 73 79 6e 74 61 78 20 65 72 72 6f 72 20   A syntax error 
307d9 68 61 73 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a  has occurred..**
307da 0a 2a 2a 20 20 20 4e 20 3d 3d 20 59 59 4e 53 54  .**   N == YYNST
307db 41 54 45 2b 59 59 4e 52 55 4c 45 2b 31 20 20 20  ATE+YYNRULE+1   
307dc 20 20 20 20 20 20 20 20 20 54 68 65 20 70 61 72           The par
307dd 73 65 72 20 61 63 63 65 70 74 73 20 69 74 73 20  ser accepts its 
307de 69 6e 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e  input..**.**   N
307df 20 3d 3d 20 59 59 4e 53 54 41 54 45 2b 59 59 4e   == YYNSTATE+YYN
307e0 52 55 4c 45 2b 32 20 20 20 20 20 20 20 20 20 20  RULE+2          
307e1 20 20 4e 6f 20 73 75 63 68 20 61 63 74 69 6f 6e    No such action
307e2 2e 20 20 44 65 6e 6f 74 65 73 20 75 6e 75 73 65  .  Denotes unuse
307e3 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  d.**            
307e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
307e5 20 20 20 20 20 20 20 20 20 20 73 6c 6f 74 73 20            slots 
307e6 69 6e 20 74 68 65 20 79 79 5f 61 63 74 69 6f 6e  in the yy_action
307e7 5b 5d 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  [] table..**.** 
307e8 54 68 65 20 61 63 74 69 6f 6e 20 74 61 62 6c 65  The action table
307e9 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65 64 20   is constructed 
307ea 61 73 20 61 20 73 69 6e 67 6c 65 20 6c 61 72 67  as a single larg
307eb 65 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 79 79  e table named yy
307ec 5f 61 63 74 69 6f 6e 5b 5d 2e 0a 2a 2a 20 47 69  _action[]..** Gi
307ed 76 65 6e 20 73 74 61 74 65 20 53 20 61 6e 64 20  ven state S and 
307ee 6c 6f 6f 6b 61 68 65 61 64 20 58 2c 20 74 68 65  lookahead X, the
307ef 20 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 70 75   action is compu
307f0 74 65 64 20 61 73 0a 2a 2a 0a 2a 2a 20 20 20 20  ted as.**.**    
307f1 20 20 79 79 5f 61 63 74 69 6f 6e 5b 20 79 79 5f    yy_action[ yy_
307f2 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d 20 2b 20  shift_ofst[S] + 
307f3 58 20 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  X ].**.** If the
307f4 20 69 6e 64 65 78 20 76 61 6c 75 65 20 79 79 5f   index value yy_
307f5 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d 2b 58 20  shift_ofst[S]+X 
307f6 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20  is out of range 
307f7 6f 72 20 69 66 20 74 68 65 20 76 61 6c 75 65 0a  or if the value.
307f8 2a 2a 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b  ** yy_lookahead[
307f9 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d  yy_shift_ofst[S]
307fa 2b 58 5d 20 69 73 20 6e 6f 74 20 65 71 75 61 6c  +X] is not equal
307fb 20 74 6f 20 58 20 6f 72 20 69 66 20 79 79 5f 73   to X or if yy_s
307fc 68 69 66 74 5f 6f 66 73 74 5b 53 5d 0a 2a 2a 20  hift_ofst[S].** 
307fd 69 73 20 65 71 75 61 6c 20 74 6f 20 59 59 5f 53  is equal to YY_S
307fe 48 49 46 54 5f 55 53 45 5f 44 46 4c 54 2c 20 69  HIFT_USE_DFLT, i
307ff 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  t means that the
30800 20 61 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 69   action is not i
30801 6e 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 61  n the table.** a
30802 6e 64 20 74 68 61 74 20 79 79 5f 64 65 66 61 75  nd that yy_defau
30803 6c 74 5b 53 5d 20 73 68 6f 75 6c 64 20 62 65 20  lt[S] should be 
30804 75 73 65 64 20 69 6e 73 74 65 61 64 2e 20 20 0a  used instead.  .
30805 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 75 6c  **.** The formul
30806 61 20 61 62 6f 76 65 20 69 73 20 66 6f 72 20 63  a above is for c
30807 6f 6d 70 75 74 69 6e 67 20 74 68 65 20 61 63 74  omputing the act
30808 69 6f 6e 20 77 68 65 6e 20 74 68 65 20 6c 6f 6f  ion when the loo
30809 6b 61 68 65 61 64 20 69 73 0a 2a 2a 20 61 20 74  kahead is.** a t
3080a 65 72 6d 69 6e 61 6c 20 73 79 6d 62 6f 6c 2e 20  erminal symbol. 
3080b 20 49 66 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61   If the lookahea
3080c 64 20 69 73 20 61 20 6e 6f 6e 2d 74 65 72 6d 69  d is a non-termi
3080d 6e 61 6c 20 28 61 73 20 6f 63 63 75 72 73 20 61  nal (as occurs a
3080e 66 74 65 72 0a 2a 2a 20 61 20 72 65 64 75 63 65  fter.** a reduce
3080f 20 61 63 74 69 6f 6e 29 20 74 68 65 6e 20 74 68   action) then th
30810 65 20 79 79 5f 72 65 64 75 63 65 5f 6f 66 73 74  e yy_reduce_ofst
30811 5b 5d 20 61 72 72 61 79 20 69 73 20 75 73 65 64  [] array is used
30812 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   in place of.** 
30813 74 68 65 20 79 79 5f 73 68 69 66 74 5f 6f 66 73  the yy_shift_ofs
30814 74 5b 5d 20 61 72 72 61 79 20 61 6e 64 20 59 59  t[] array and YY
30815 5f 52 45 44 55 43 45 5f 55 53 45 5f 44 46 4c 54  _REDUCE_USE_DFLT
30816 20 69 73 20 75 73 65 64 20 69 6e 20 70 6c 61 63   is used in plac
30817 65 20 6f 66 0a 2a 2a 20 59 59 5f 53 48 49 46 54  e of.** YY_SHIFT
30818 5f 55 53 45 5f 44 46 4c 54 2e 0a 2a 2a 0a 2a 2a  _USE_DFLT..**.**
30819 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   The following a
3081a 72 65 20 74 68 65 20 74 61 62 6c 65 73 20 67 65  re the tables ge
3081b 6e 65 72 61 74 65 64 20 69 6e 20 74 68 69 73 20  nerated in this 
3081c 73 65 63 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20  section:.**.**  
3081d 79 79 5f 61 63 74 69 6f 6e 5b 5d 20 20 20 20 20  yy_action[]     
3081e 20 20 20 41 20 73 69 6e 67 6c 65 20 74 61 62 6c     A single tabl
3081f 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c 6c  e containing all
30820 20 61 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 79 79   actions..**  yy
30821 5f 6c 6f 6f 6b 61 68 65 61 64 5b 5d 20 20 20 20  _lookahead[]    
30822 20 41 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e   A table contain
30823 69 6e 67 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61  ing the lookahea
30824 64 20 66 6f 72 20 65 61 63 68 20 65 6e 74 72 79  d for each entry
30825 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   in.**          
30826 20 20 20 20 20 20 20 20 20 20 20 79 79 5f 61 63             yy_ac
30827 74 69 6f 6e 2e 20 20 55 73 65 64 20 74 6f 20 64  tion.  Used to d
30828 65 74 65 63 74 20 68 61 73 68 20 63 6f 6c 6c 69  etect hash colli
30829 73 69 6f 6e 73 2e 0a 2a 2a 20 20 79 79 5f 73 68  sions..**  yy_sh
3082a 69 66 74 5f 6f 66 73 74 5b 5d 20 20 20 20 46 6f  ift_ofst[]    Fo
3082b 72 20 65 61 63 68 20 73 74 61 74 65 2c 20 74 68  r each state, th
3082c 65 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 79 79  e offset into yy
3082d 5f 61 63 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 20  _action for.**  
3082e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3082f 20 20 20 73 68 69 66 74 69 6e 67 20 74 65 72 6d     shifting term
30830 69 6e 61 6c 73 2e 0a 2a 2a 20 20 79 79 5f 72 65  inals..**  yy_re
30831 64 75 63 65 5f 6f 66 73 74 5b 5d 20 20 20 46 6f  duce_ofst[]   Fo
30832 72 20 65 61 63 68 20 73 74 61 74 65 2c 20 74 68  r each state, th
30833 65 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 79 79  e offset into yy
30834 5f 61 63 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 20  _action for.**  
30835 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30836 20 20 20 73 68 69 66 74 69 6e 67 20 6e 6f 6e 2d     shifting non-
30837 74 65 72 6d 69 6e 61 6c 73 20 61 66 74 65 72 20  terminals after 
30838 61 20 72 65 64 75 63 65 2e 0a 2a 2a 20 20 79 79  a reduce..**  yy
30839 5f 64 65 66 61 75 6c 74 5b 5d 20 20 20 20 20 20  _default[]      
3083a 20 44 65 66 61 75 6c 74 20 61 63 74 69 6f 6e 20   Default action 
3083b 66 6f 72 20 65 61 63 68 20 73 74 61 74 65 2e 0a  for each state..
3083c 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
3083d 59 59 41 43 54 49 4f 4e 54 59 50 45 20 79 79 5f  YYACTIONTYPE yy_
3083e 61 63 74 69 6f 6e 5b 5d 20 3d 20 7b 0a 20 2f 2a  action[] = {. /*
3083f 20 20 20 20 20 30 20 2a 2f 20 20 20 33 31 32 2c       0 */   312,
30840 20 20 39 35 39 2c 20 20 31 38 32 2c 20 20 36 32    959,  182,  62
30841 38 2c 20 20 20 20 32 2c 20 20 31 35 37 2c 20 20  8,    2,  157,  
30842 32 31 39 2c 20 20 34 35 30 2c 20 20 20 32 34 2c  219,  450,   24,
30843 20 20 20 32 34 2c 0a 20 2f 2a 20 20 20 20 31 30     24,. /*    10
30844 20 2a 2f 20 20 20 20 32 34 2c 20 20 20 32 34 2c   */    24,   24,
30845 20 20 32 32 31 2c 20 20 20 32 36 2c 20 20 20 32    221,   26,   2
30846 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  6,   26,   26,  
30847 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c   27,   27,   28,
30848 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20 20  . /*    20 */   
30849 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c   28,   28,   29,
3084a 20 20 32 32 31 2c 20 20 34 32 34 2c 20 20 34 32    221,  424,  42
3084b 35 2c 20 20 20 33 30 2c 20 20 34 39 32 2c 20 20  5,   30,  492,  
3084c 20 33 33 2c 20 20 31 34 31 2c 0a 20 2f 2a 20 20   33,  141,. /*  
3084d 20 20 33 30 20 2a 2f 20 20 20 34 35 37 2c 20 20    30 */   457,  
3084e 34 36 33 2c 20 20 20 33 31 2c 20 20 20 32 36 2c  463,   31,   26,
3084f 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
30850 36 2c 20 20 20 32 37 2c 20 20 20 32 37 2c 20 20  6,   27,   27,  
30851 20 32 38 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a   28,. /*    40 *
30852 2f 20 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20  /    28,   28,  
30853 20 32 39 2c 20 20 32 32 31 2c 20 20 20 32 38 2c   29,  221,   28,
30854 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32     28,   28,   2
30855 39 2c 20 20 32 32 31 2c 20 20 20 32 33 2c 0a 20  9,  221,   23,. 
30856 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 20 32  /*    50 */    2
30857 32 2c 20 20 20 33 32 2c 20 20 34 36 35 2c 20 20  2,   32,  465,  
30858 34 36 36 2c 20 20 34 36 34 2c 20 20 34 36 34 2c  466,  464,  464,
30859 20 20 20 32 35 2c 20 20 20 32 35 2c 20 20 20 32     25,   25,   2
3085a 34 2c 20 20 20 32 34 2c 0a 20 2f 2a 20 20 20 20  4,   24,. /*    
3085b 36 30 20 2a 2f 20 20 20 20 32 34 2c 20 20 20 32  60 */    24,   2
3085c 34 2c 20 20 32 39 33 2c 20 20 20 32 36 2c 20 20  4,  293,   26,  
3085d 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c   26,   26,   26,
3085e 20 20 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32     27,   27,   2
3085f 38 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20  8,. /*    70 */ 
30860 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32     28,   28,   2
30861 39 2c 20 20 32 32 31 2c 20 20 33 31 32 2c 20 20  9,  221,  312,  
30862 34 35 30 2c 20 20 33 31 39 2c 20 20 34 37 39 2c  450,  319,  479,
30863 20 20 33 34 34 2c 20 20 32 30 38 2c 0a 20 2f 2a    344,  208,. /*
30864 20 20 20 20 38 30 20 2a 2f 20 20 20 20 34 37 2c      80 */    47,
30865 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
30866 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  6,   26,   27,  
30867 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c   27,   28,   28,
30868 20 20 20 32 38 2c 0a 20 2f 2a 20 20 20 20 39 30     28,. /*    90
30869 20 2a 2f 20 20 20 20 32 39 2c 20 20 32 32 31 2c   */    29,  221,
3086a 20 20 34 32 37 2c 20 20 34 32 38 2c 20 20 31 36    427,  428,  16
3086b 33 2c 20 20 33 33 39 2c 20 20 35 34 33 2c 20 20  3,  339,  543,  
3086c 33 36 38 2c 20 20 33 37 31 2c 20 20 33 37 32 2c  368,  371,  372,
3086d 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20 20  . /*   100 */   
3086e 35 32 31 2c 20 20 33 31 37 2c 20 20 34 37 32 2c  521,  317,  472,
3086f 20 20 34 37 33 2c 20 20 34 35 37 2c 20 20 34 36    473,  457,  46
30870 33 2c 20 20 32 39 36 2c 20 20 33 37 33 2c 20 20  3,  296,  373,  
30871 32 39 34 2c 20 20 20 32 31 2c 0a 20 2f 2a 20 20  294,   21,. /*  
30872 20 31 31 30 20 2a 2f 20 20 20 33 33 36 2c 20 20   110 */   336,  
30873 33 36 37 2c 20 20 34 31 39 2c 20 20 34 31 36 2c  367,  419,  416,
30874 20 20 34 32 34 2c 20 20 34 32 35 2c 20 20 35 32    424,  425,  52
30875 33 2c 20 20 20 20 31 2c 20 20 35 34 34 2c 20 20  3,    1,  544,  
30876 34 34 36 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a  446,. /*   120 *
30877 2f 20 20 20 20 38 30 2c 20 20 34 32 34 2c 20 20  /    80,  424,  
30878 34 32 35 2c 20 20 20 32 33 2c 20 20 20 32 32 2c  425,   23,   22,
30879 20 20 20 33 32 2c 20 20 34 36 35 2c 20 20 34 36     32,  465,  46
3087a 36 2c 20 20 34 36 34 2c 20 20 34 36 34 2c 0a 20  6,  464,  464,. 
3087b 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 20 32  /*   130 */    2
3087c 35 2c 20 20 20 32 35 2c 20 20 20 32 34 2c 20 20  5,   25,   24,  
3087d 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c   24,   24,   24,
3087e 20 20 35 36 34 2c 20 20 20 32 36 2c 20 20 20 32    564,   26,   2
3087f 36 2c 20 20 20 32 36 2c 0a 20 2f 2a 20 20 20 31  6,   26,. /*   1
30880 34 30 20 2a 2f 20 20 20 20 32 36 2c 20 20 20 32  40 */    26,   2
30881 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20  7,   27,   28,  
30882 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c   28,   28,   29,
30883 20 20 32 32 31 2c 20 20 33 31 32 2c 20 20 32 33    221,  312,  23
30884 33 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20  3,. /*   150 */ 
30885 20 20 33 31 39 2c 20 20 34 34 31 2c 20 20 35 35    319,  441,  55
30886 34 2c 20 20 31 35 32 2c 20 20 31 33 39 2c 20 20  4,  152,  139,  
30887 32 36 33 2c 20 20 33 36 35 2c 20 20 32 36 38 2c  263,  365,  268,
30888 20 20 33 36 36 2c 20 20 31 36 30 2c 0a 20 2f 2a    366,  160,. /*
30889 20 20 20 31 36 30 20 2a 2f 20 20 20 35 35 31 2c     160 */   551,
3088a 20 20 33 35 32 2c 20 20 33 33 32 2c 20 20 34 32    352,  332,  42
3088b 31 2c 20 20 32 32 32 2c 20 20 32 37 32 2c 20 20  1,  222,  272,  
3088c 33 36 32 2c 20 20 33 32 32 2c 20 20 32 31 38 2c  362,  322,  218,
3088d 20 20 35 35 37 2c 0a 20 2f 2a 20 20 20 31 37 30    557,. /*   170
3088e 20 2a 2f 20 20 20 31 31 36 2c 20 20 33 33 39 2c   */   116,  339,
3088f 20 20 32 34 38 2c 20 20 35 37 34 2c 20 20 34 37    248,  574,  47
30890 37 2c 20 20 32 32 33 2c 20 20 32 31 36 2c 20 20  7,  223,  216,  
30891 35 37 33 2c 20 20 34 35 37 2c 20 20 34 36 33 2c  573,  457,  463,
30892 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20 20  . /*   180 */   
30893 34 35 30 2c 20 20 20 35 39 2c 20 20 34 32 37 2c  450,   59,  427,
30894 20 20 34 32 38 2c 20 20 32 39 35 2c 20 20 36 31    428,  295,  61
30895 30 2c 20 20 33 33 36 2c 20 20 35 36 33 2c 20 20  0,  336,  563,  
30896 35 33 38 2c 20 20 34 32 37 2c 0a 20 2f 2a 20 20  538,  427,. /*  
30897 20 31 39 30 20 2a 2f 20 20 20 34 32 38 2c 20 20   190 */   428,  
30898 33 38 35 2c 20 20 36 30 38 2c 20 20 36 30 39 2c  385,  608,  609,
30899 20 20 35 36 32 2c 20 20 34 34 36 2c 20 20 20 38    562,  446,   8
3089a 37 2c 20 20 20 32 33 2c 20 20 20 32 32 2c 20 20  7,   23,   22,  
3089b 20 33 32 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a   32,. /*   200 *
3089c 2f 20 20 20 34 36 35 2c 20 20 34 36 36 2c 20 20  /   465,  466,  
3089d 34 36 34 2c 20 20 34 36 34 2c 20 20 20 32 35 2c  464,  464,   25,
3089e 20 20 20 32 35 2c 20 20 20 32 34 2c 20 20 20 32     25,   24,   2
3089f 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 0a 20  4,   24,   24,. 
308a0 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 20 34 34  /*   210 */   44
308a1 37 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  7,   26,   26,  
308a2 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c   26,   26,   27,
308a3 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32     27,   28,   2
308a4 38 2c 20 20 20 32 38 2c 0a 20 2f 2a 20 20 20 32  8,   28,. /*   2
308a5 32 30 20 2a 2f 20 20 20 20 32 39 2c 20 20 32 32  20 */    29,  22
308a6 31 2c 20 20 33 31 32 2c 20 20 32 33 33 2c 20 20  1,  312,  233,  
308a7 34 37 37 2c 20 20 32 32 33 2c 20 20 35 37 36 2c  477,  223,  576,
308a8 20 20 31 33 34 2c 20 20 31 33 39 2c 20 20 32 36    134,  139,  26
308a9 33 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20  3,. /*   230 */ 
308aa 20 20 33 36 35 2c 20 20 32 36 38 2c 20 20 33 36    365,  268,  36
308ab 36 2c 20 20 31 36 30 2c 20 20 34 30 36 2c 20 20  6,  160,  406,  
308ac 33 35 34 2c 20 20 32 32 36 2c 20 20 34 39 38 2c  354,  226,  498,
308ad 20 20 34 38 31 2c 20 20 32 37 32 2c 0a 20 2f 2a    481,  272,. /*
308ae 20 20 20 32 34 30 20 2a 2f 20 20 20 33 33 39 2c     240 */   339,
308af 20 20 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32     27,   27,   2
308b0 38 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20  8,   28,   28,  
308b1 20 32 39 2c 20 20 32 32 31 2c 20 20 34 35 30 2c   29,  221,  450,
308b2 20 20 34 34 32 2c 0a 20 2f 2a 20 20 20 32 35 30    442,. /*   250
308b3 20 2a 2f 20 20 20 31 39 39 2c 20 20 35 34 30 2c   */   199,  540,
308b4 20 20 34 35 37 2c 20 20 34 36 33 2c 20 20 33 34    457,  463,  34
308b5 39 2c 20 20 33 33 36 2c 20 20 31 36 33 2c 20 20  9,  336,  163,  
308b6 35 35 31 2c 20 20 20 36 36 2c 20 20 33 36 38 2c  551,   66,  368,
308b7 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20 20  . /*   260 */   
308b8 33 37 31 2c 20 20 33 37 32 2c 20 20 34 35 30 2c  371,  372,  450,
308b9 20 20 34 31 35 2c 20 20 34 34 36 2c 20 20 20 38    415,  446,   8
308ba 30 2c 20 20 35 32 32 2c 20 20 35 38 31 2c 20 20  0,  522,  581,  
308bb 34 30 31 2c 20 20 33 37 33 2c 0a 20 2f 2a 20 20  401,  373,. /*  
308bc 20 32 37 30 20 2a 2f 20 20 20 34 35 32 2c 20 20   270 */   452,  
308bd 20 32 33 2c 20 20 20 32 32 2c 20 20 20 33 32 2c   23,   22,   32,
308be 20 20 34 36 35 2c 20 20 34 36 36 2c 20 20 34 36    465,  466,  46
308bf 34 2c 20 20 34 36 34 2c 20 20 20 32 35 2c 20 20  4,  464,   25,  
308c0 20 32 35 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a   25,. /*   280 *
308c1 2f 20 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  /    24,   24,  
308c2 20 32 34 2c 20 20 20 32 34 2c 20 20 34 34 37 2c   24,   24,  447,
308c3 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
308c4 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 0a 20  6,   26,   27,. 
308c5 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 20 20 32  /*   290 */    2
308c6 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20  7,   28,   28,  
308c7 20 32 38 2c 20 20 20 32 39 2c 20 20 32 32 31 2c   28,   29,  221,
308c8 20 20 33 31 32 2c 20 20 33 33 39 2c 20 20 35 35    312,  339,  55
308c9 36 2c 20 20 36 30 37 2c 0a 20 2f 2a 20 20 20 33  6,  607,. /*   3
308ca 30 30 20 2a 2f 20 20 20 31 39 37 2c 20 20 34 35  00 */   197,  45
308cb 34 2c 20 20 34 35 34 2c 20 20 34 35 34 2c 20 20  4,  454,  454,  
308cc 35 34 36 2c 20 20 35 37 38 2c 20 20 33 35 32 2c  546,  578,  352,
308cd 20 20 31 39 38 2c 20 20 36 30 37 2c 20 20 34 34    198,  607,  44
308ce 30 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20  0,. /*   310 */ 
308cf 20 20 20 36 35 2c 20 20 33 35 31 2c 20 20 33 33     65,  351,  33
308d0 36 2c 20 20 34 32 36 2c 20 20 34 32 36 2c 20 20  6,  426,  426,  
308d1 33 39 39 2c 20 20 32 38 39 2c 20 20 34 32 34 2c  399,  289,  424,
308d2 20 20 34 32 35 2c 20 20 36 30 36 2c 0a 20 2f 2a    425,  606,. /*
308d3 20 20 20 33 32 30 20 2a 2f 20 20 20 36 30 35 2c     320 */   605,
308d4 20 20 34 34 36 2c 20 20 20 37 33 2c 20 20 34 32    446,   73,  42
308d5 36 2c 20 20 32 31 34 2c 20 20 32 31 39 2c 20 20  6,  214,  219,  
308d6 34 35 37 2c 20 20 34 36 33 2c 20 20 36 30 36 2c  457,  463,  606,
308d7 20 20 34 31 30 2c 0a 20 2f 2a 20 20 20 33 33 30    410,. /*   330
308d8 20 2a 2f 20 20 20 34 35 30 2c 20 20 32 34 31 2c   */   450,  241,
308d9 20 20 33 30 36 2c 20 20 31 39 36 2c 20 20 35 36    306,  196,  56
308da 35 2c 20 20 34 37 39 2c 20 20 35 35 35 2c 20 20  5,  479,  555,  
308db 32 30 38 2c 20 20 32 38 38 2c 20 20 20 32 39 2c  208,  288,   29,
308dc 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20 20  . /*   340 */   
308dd 32 32 31 2c 20 20 34 34 37 2c 20 20 20 20 34 2c  221,  447,    4,
308de 20 20 38 37 34 2c 20 20 35 30 34 2c 20 20 20 32    874,  504,   2
308df 33 2c 20 20 20 32 32 2c 20 20 20 33 32 2c 20 20  3,   22,   32,  
308e0 34 36 35 2c 20 20 34 36 36 2c 0a 20 2f 2a 20 20  465,  466,. /*  
308e1 20 33 35 30 20 2a 2f 20 20 20 34 36 34 2c 20 20   350 */   464,  
308e2 34 36 34 2c 20 20 20 32 35 2c 20 20 20 32 35 2c  464,   25,   25,
308e3 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32     24,   24,   2
308e4 34 2c 20 20 20 32 34 2c 20 20 34 34 37 2c 20 20  4,   24,  447,  
308e5 20 32 36 2c 0a 20 2f 2a 20 20 20 33 36 30 20 2a   26,. /*   360 *
308e6 2f 20 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  /    26,   26,  
308e7 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32 37 2c   26,   27,   27,
308e8 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32     28,   28,   2
308e9 38 2c 20 20 20 32 39 2c 20 20 32 32 31 2c 0a 20  8,   29,  221,. 
308ea 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20 20 33 31  /*   370 */   31
308eb 32 2c 20 20 31 36 33 2c 20 20 35 38 32 2c 20 20  2,  163,  582,  
308ec 33 33 39 2c 20 20 33 36 38 2c 20 20 33 37 31 2c  339,  368,  371,
308ed 20 20 33 37 32 2c 20 20 33 31 34 2c 20 20 34 32    372,  314,  42
308ee 34 2c 20 20 34 32 35 2c 0a 20 2f 2a 20 20 20 33  4,  425,. /*   3
308ef 38 30 20 2a 2f 20 20 20 36 30 34 2c 20 20 32 32  80 */   604,  22
308f0 32 2c 20 20 33 39 37 2c 20 20 32 32 37 2c 20 20  2,  397,  227,  
308f1 33 37 33 2c 20 20 34 32 37 2c 20 20 34 32 38 2c  373,  427,  428,
308f2 20 20 33 33 39 2c 20 20 33 33 36 2c 20 20 34 30    339,  336,  40
308f3 39 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20  9,. /*   390 */ 
308f4 20 20 32 32 32 2c 20 20 34 37 38 2c 20 20 33 33    222,  478,  33
308f5 39 2c 20 20 20 33 30 2c 20 20 33 39 36 2c 20 20  9,   30,  396,  
308f6 20 33 33 2c 20 20 31 34 31 2c 20 20 34 34 36 2c   33,  141,  446,
308f7 20 20 20 38 31 2c 20 20 20 36 32 2c 0a 20 2f 2a     81,   62,. /*
308f8 20 20 20 34 30 30 20 2a 2f 20 20 20 34 35 37 2c     400 */   457,
308f9 20 20 34 36 33 2c 20 20 33 33 36 2c 20 20 31 35    463,  336,  15
308fa 37 2c 20 20 34 30 30 2c 20 20 34 35 30 2c 20 20  7,  400,  450,  
308fb 35 30 34 2c 20 20 33 33 36 2c 20 20 34 33 38 2c  504,  336,  438,
308fc 20 20 34 32 36 2c 0a 20 2f 2a 20 20 20 34 31 30    426,. /*   410
308fd 20 2a 2f 20 20 20 35 30 30 2c 20 20 34 34 36 2c   */   500,  446,
308fe 20 20 20 38 37 2c 20 20 20 34 31 2c 20 20 33 38     87,   41,  38
308ff 30 2c 20 20 36 31 33 2c 20 20 34 34 36 2c 20 20  0,  613,  446,  
30900 20 38 30 2c 20 20 35 38 31 2c 20 20 20 32 33 2c   80,  581,   23,
30901 0a 20 2f 2a 20 20 20 34 32 30 20 2a 2f 20 20 20  . /*   420 */   
30902 20 32 32 2c 20 20 20 33 32 2c 20 20 34 36 35 2c   22,   32,  465,
30903 20 20 34 36 36 2c 20 20 34 36 34 2c 20 20 34 36    466,  464,  46
30904 34 2c 20 20 20 32 35 2c 20 20 20 32 35 2c 20 20  4,   25,   25,  
30905 20 32 34 2c 20 20 20 32 34 2c 0a 20 2f 2a 20 20   24,   24,. /*  
30906 20 34 33 30 20 2a 2f 20 20 20 20 32 34 2c 20 20   430 */    24,  
30907 20 32 34 2c 20 20 32 31 33 2c 20 20 20 32 36 2c   24,  213,   26,
30908 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
30909 36 2c 20 20 20 32 37 2c 20 20 20 32 37 2c 20 20  6,   27,   27,  
3090a 20 32 38 2c 0a 20 2f 2a 20 20 20 34 34 30 20 2a   28,. /*   440 *
3090b 2f 20 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20  /    28,   28,  
3090c 20 32 39 2c 20 20 32 32 31 2c 20 20 33 31 32 2c   29,  221,  312,
3090d 20 20 35 31 33 2c 20 20 34 32 37 2c 20 20 34 32    513,  427,  42
3090e 38 2c 20 20 35 31 37 2c 20 20 32 35 34 2c 0a 20  8,  517,  254,. 
3090f 2f 2a 20 20 20 34 35 30 20 2a 2f 20 20 20 35 32  /*   450 */   52
30910 34 2c 20 20 33 38 36 2c 20 20 32 32 35 2c 20 20  4,  386,  225,  
30911 33 33 39 2c 20 20 34 38 36 2c 20 20 33 36 33 2c  339,  486,  363,
30912 20 20 33 38 39 2c 20 20 33 33 39 2c 20 20 33 35    389,  339,  35
30913 36 2c 20 20 34 34 33 2c 0a 20 2f 2a 20 20 20 34  6,  443,. /*   4
30914 36 30 20 2a 2f 20 20 20 34 39 34 2c 20 20 32 33  60 */   494,  23
30915 36 2c 20 20 20 33 30 2c 20 20 34 39 37 2c 20 20  6,   30,  497,  
30916 20 33 33 2c 20 20 31 34 31 2c 20 20 33 39 39 2c   33,  141,  399,
30917 20 20 32 38 39 2c 20 20 33 33 36 2c 20 20 34 39    289,  336,  49
30918 35 2c 0a 20 2f 2a 20 20 20 34 37 30 20 2a 2f 20  5,. /*   470 */ 
30919 20 20 34 38 37 2c 20 20 35 30 31 2c 20 20 33 33    487,  501,  33
3091a 36 2c 20 20 34 35 30 2c 20 20 34 35 37 2c 20 20  6,  450,  457,  
3091b 34 36 33 2c 20 20 32 31 39 2c 20 20 34 34 36 2c  463,  219,  446,
3091c 20 20 20 39 35 2c 20 20 34 34 35 2c 0a 20 2f 2a     95,  445,. /*
3091d 20 20 20 34 38 30 20 2a 2f 20 20 20 20 36 38 2c     480 */    68,
3091e 20 20 34 34 36 2c 20 20 20 39 35 2c 20 20 34 34    446,   95,  44
3091f 34 2c 20 20 34 32 34 2c 20 20 34 32 35 2c 20 20  4,  424,  425,  
30920 34 38 38 2c 20 20 20 34 34 2c 20 20 33 34 38 2c  488,   44,  348,
30921 20 20 32 38 38 2c 0a 20 2f 2a 20 20 20 34 39 30    288,. /*   490
30922 20 2a 2f 20 20 20 35 30 34 2c 20 20 34 32 34 2c   */   504,  424,
30923 20 20 34 32 35 2c 20 20 20 32 33 2c 20 20 20 32    425,   23,   2
30924 32 2c 20 20 20 33 32 2c 20 20 34 36 35 2c 20 20  2,   32,  465,  
30925 34 36 36 2c 20 20 34 36 34 2c 20 20 34 36 34 2c  466,  464,  464,
30926 0a 20 2f 2a 20 20 20 35 30 30 20 2a 2f 20 20 20  . /*   500 */   
30927 20 32 35 2c 20 20 20 32 35 2c 20 20 20 32 34 2c   25,   25,   24,
30928 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32     24,   24,   2
30929 34 2c 20 20 33 39 31 2c 20 20 20 32 36 2c 20 20  4,  391,   26,  
3092a 20 32 36 2c 20 20 20 32 36 2c 0a 20 2f 2a 20 20   26,   26,. /*  
3092b 20 35 31 30 20 2a 2f 20 20 20 20 32 36 2c 20 20   510 */    26,  
3092c 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c   27,   27,   28,
3092d 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20 20 32     28,   28,   2
3092e 39 2c 20 20 32 32 31 2c 20 20 33 31 32 2c 20 20  9,  221,  312,  
3092f 33 36 31 2c 0a 20 2f 2a 20 20 20 35 32 30 20 2a  361,. /*   520 *
30930 2f 20 20 20 35 35 36 2c 20 20 34 32 36 2c 20 20  /   556,  426,  
30931 35 32 30 2c 20 20 33 32 38 2c 20 20 31 39 31 2c  520,  328,  191,
30932 20 20 32 37 31 2c 20 20 33 33 39 2c 20 20 33 32    271,  339,  32
30933 39 2c 20 20 32 34 37 2c 20 20 32 35 39 2c 0a 20  9,  247,  259,. 
30934 2f 2a 20 20 20 35 33 30 20 2a 2f 20 20 20 33 33  /*   530 */   33
30935 39 2c 20 20 35 36 36 2c 20 20 20 36 35 2c 20 20  9,  566,   65,  
30936 32 34 39 2c 20 20 33 33 36 2c 20 20 34 32 36 2c  249,  336,  426,
30937 20 20 34 32 34 2c 20 20 34 32 35 2c 20 20 34 34    424,  425,  44
30938 35 2c 20 20 35 31 36 2c 0a 20 2f 2a 20 20 20 35  5,  516,. /*   5
30939 34 30 20 2a 2f 20 20 20 34 32 36 2c 20 20 33 33  40 */   426,  33
3093a 36 2c 20 20 34 34 34 2c 20 20 34 34 36 2c 20 20  6,  444,  446,  
3093b 20 20 39 2c 20 20 33 33 36 2c 20 20 35 35 36 2c    9,  336,  556,
3093c 20 20 34 35 31 2c 20 20 34 35 37 2c 20 20 34 36    451,  457,  46
3093d 33 2c 0a 20 2f 2a 20 20 20 35 35 30 20 2a 2f 20  3,. /*   550 */ 
3093e 20 20 34 34 36 2c 20 20 20 37 34 2c 20 20 34 32    446,   74,  42
3093f 37 2c 20 20 34 32 38 2c 20 20 34 34 36 2c 20 20  7,  428,  446,  
30940 20 36 39 2c 20 20 31 39 32 2c 20 20 36 31 38 2c   69,  192,  618,
30941 20 20 20 36 35 2c 20 20 34 32 37 2c 0a 20 2f 2a     65,  427,. /*
30942 20 20 20 35 36 30 20 2a 2f 20 20 20 34 32 38 2c     560 */   428,
30943 20 20 34 32 36 2c 20 20 33 32 33 2c 20 20 32 37    426,  323,  27
30944 37 2c 20 20 20 31 36 2c 20 20 32 30 32 2c 20 20  7,   16,  202,  
30945 31 38 39 2c 20 20 20 32 33 2c 20 20 20 32 32 2c  189,   23,   22,
30946 20 20 20 33 32 2c 0a 20 2f 2a 20 20 20 35 37 30     32,. /*   570
30947 20 2a 2f 20 20 20 34 36 35 2c 20 20 34 36 36 2c   */   465,  466,
30948 20 20 34 36 34 2c 20 20 34 36 34 2c 20 20 20 32    464,  464,   2
30949 35 2c 20 20 20 32 35 2c 20 20 20 32 34 2c 20 20  5,   25,   24,  
3094a 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c   24,   24,   24,
3094b 0a 20 2f 2a 20 20 20 35 38 30 20 2a 2f 20 20 20  . /*   580 */   
3094c 32 35 35 2c 20 20 20 32 36 2c 20 20 20 32 36 2c  255,   26,   26,
3094d 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
3094e 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20  7,   27,   28,  
3094f 20 32 38 2c 20 20 20 32 38 2c 0a 20 2f 2a 20 20   28,   28,. /*  
30950 20 35 39 30 20 2a 2f 20 20 20 20 32 39 2c 20 20   590 */    29,  
30951 32 32 31 2c 20 20 33 31 32 2c 20 20 33 33 39 2c  221,  312,  339,
30952 20 20 34 38 36 2c 20 20 34 32 36 2c 20 20 35 33    486,  426,  53
30953 37 2c 20 20 32 33 35 2c 20 20 35 31 35 2c 20 20  7,  235,  515,  
30954 34 34 37 2c 0a 20 2f 2a 20 20 20 36 30 30 20 2a  447,. /*   600 *
30955 2f 20 20 20 33 33 39 2c 20 20 36 32 39 2c 20 20  /   339,  629,  
30956 34 31 39 2c 20 20 34 31 36 2c 20 20 34 32 37 2c  419,  416,  427,
30957 20 20 34 32 38 2c 20 20 32 31 37 2c 20 20 32 38    428,  217,  28
30958 31 2c 20 20 33 33 36 2c 20 20 32 37 39 2c 0a 20  1,  336,  279,. 
30959 2f 2a 20 20 20 36 31 30 20 2a 2f 20 20 20 34 38  /*   610 */   48
3095a 37 2c 20 20 32 30 33 2c 20 20 31 34 34 2c 20 20  7,  203,  144,  
3095b 35 32 36 2c 20 20 35 32 37 2c 20 20 33 33 36 2c  526,  527,  336,
3095c 20 20 33 39 31 2c 20 20 34 34 36 2c 20 20 20 37    391,  446,   7
3095d 38 2c 20 20 34 32 39 2c 0a 20 2f 2a 20 20 20 36  8,  429,. /*   6
3095e 32 30 20 2a 2f 20 20 20 34 33 30 2c 20 20 34 33  20 */   430,  43
3095f 31 2c 20 20 34 35 37 2c 20 20 34 36 33 2c 20 20  1,  457,  463,  
30960 34 34 36 2c 20 20 20 39 39 2c 20 20 34 38 38 2c  446,   99,  488,
30961 20 20 33 34 31 2c 20 20 35 32 38 2c 20 20 34 36    341,  528,  46
30962 38 2c 0a 20 2f 2a 20 20 20 36 33 30 20 2a 2f 20  8,. /*   630 */ 
30963 20 20 34 36 38 2c 20 20 34 32 36 2c 20 20 33 34    468,  426,  34
30964 33 2c 20 20 34 37 32 2c 20 20 34 37 33 2c 20 20  3,  472,  473,  
30965 36 32 36 2c 20 20 39 34 39 2c 20 20 34 37 34 2c  626,  949,  474,
30966 20 20 39 34 39 2c 20 20 35 32 39 2c 0a 20 2f 2a    949,  529,. /*
30967 20 20 20 36 34 30 20 2a 2f 20 20 20 34 34 37 2c     640 */   447,
30968 20 20 20 32 33 2c 20 20 20 32 32 2c 20 20 20 33     23,   22,   3
30969 32 2c 20 20 34 36 35 2c 20 20 34 36 36 2c 20 20  2,  465,  466,  
3096a 34 36 34 2c 20 20 34 36 34 2c 20 20 20 32 35 2c  464,  464,   25,
3096b 20 20 20 32 35 2c 0a 20 2f 2a 20 20 20 36 35 30     25,. /*   650
3096c 20 2a 2f 20 20 20 20 32 34 2c 20 20 20 32 34 2c   */    24,   24,
3096d 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 33 33     24,   24,  33
3096e 39 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  9,   26,   26,  
3096f 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c   26,   26,   27,
30970 0a 20 2f 2a 20 20 20 36 36 30 20 2a 2f 20 20 20  . /*   660 */   
30971 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c   27,   28,   28,
30972 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20 32 32     28,   29,  22
30973 31 2c 20 20 33 31 32 2c 20 20 33 33 39 2c 20 20  1,  312,  339,  
30974 31 36 32 2c 20 20 33 33 36 2c 0a 20 2f 2a 20 20  162,  336,. /*  
30975 20 36 37 30 20 2a 2f 20 20 20 32 37 35 2c 20 20   670 */   275,  
30976 32 38 33 2c 20 20 34 37 36 2c 20 20 33 37 36 2c  283,  476,  376,
30977 20 20 33 33 39 2c 20 20 35 37 39 2c 20 20 35 32    339,  579,  52
30978 37 2c 20 20 33 34 36 2c 20 20 34 34 36 2c 20 20  7,  346,  446,  
30979 20 39 38 2c 0a 20 2f 2a 20 20 20 36 38 30 20 2a   98,. /*   680 *
3097a 2f 20 20 20 36 32 32 2c 20 20 20 33 30 2c 20 20  /   622,   30,  
3097b 33 33 36 2c 20 20 20 33 33 2c 20 20 31 34 31 2c  336,   33,  141,
3097c 20 20 33 33 39 2c 20 20 34 32 36 2c 20 20 33 33    339,  426,  33
3097d 39 2c 20 20 35 30 38 2c 20 20 33 33 36 2c 0a 20  9,  508,  336,. 
3097e 2f 2a 20 20 20 36 39 30 20 2a 2f 20 20 20 34 36  /*   690 */   46
3097f 39 2c 20 20 34 34 36 2c 20 20 31 30 35 2c 20 20  9,  446,  105,  
30980 34 31 38 2c 20 20 20 20 32 2c 20 20 32 32 32 2c  418,    2,  222,
30981 20 20 34 35 37 2c 20 20 34 36 33 2c 20 20 34 34    457,  463,  44
30982 36 2c 20 20 31 30 31 2c 0a 20 2f 2a 20 20 20 37  6,  101,. /*   7
30983 30 30 20 2a 2f 20 20 20 33 33 36 2c 20 20 32 31  00 */   336,  21
30984 39 2c 20 20 33 33 36 2c 20 20 34 32 36 2c 20 20  9,  336,  426,  
30985 31 36 31 2c 20 20 36 32 36 2c 20 20 39 34 38 2c  161,  626,  948,
30986 20 20 32 39 30 2c 20 20 39 34 38 2c 20 20 34 34    290,  948,  44
30987 36 2c 0a 20 2f 2a 20 20 20 37 31 30 20 2a 2f 20  6,. /*   710 */ 
30988 20 20 31 30 38 2c 20 20 34 34 36 2c 20 20 31 30    108,  446,  10
30989 39 2c 20 20 33 39 38 2c 20 20 32 38 34 2c 20 20  9,  398,  284,  
3098a 20 32 33 2c 20 20 20 32 32 2c 20 20 20 33 32 2c   23,   22,   32,
3098b 20 20 34 36 35 2c 20 20 34 36 36 2c 0a 20 2f 2a    465,  466,. /*
3098c 20 20 20 37 32 30 20 2a 2f 20 20 20 34 36 34 2c     720 */   464,
3098d 20 20 34 36 34 2c 20 20 20 32 35 2c 20 20 20 32    464,   25,   2
3098e 35 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  5,   24,   24,  
3098f 20 32 34 2c 20 20 20 32 34 2c 20 20 33 33 39 2c   24,   24,  339,
30990 20 20 20 32 36 2c 0a 20 2f 2a 20 20 20 37 33 30     26,. /*   730
30991 20 2a 2f 20 20 20 20 32 36 2c 20 20 20 32 36 2c   */    26,   26,
30992 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32     26,   27,   2
30993 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20  7,   28,   28,  
30994 20 32 38 2c 20 20 20 32 39 2c 20 20 32 32 31 2c   28,   29,  221,
30995 0a 20 2f 2a 20 20 20 37 34 30 20 2a 2f 20 20 20  . /*   740 */   
30996 33 31 32 2c 20 20 33 33 39 2c 20 20 32 37 31 2c  312,  339,  271,
30997 20 20 33 33 36 2c 20 20 33 33 39 2c 20 20 20 35    336,  339,   5
30998 38 2c 20 20 35 33 35 2c 20 20 34 38 32 2c 20 20  8,  535,  482,  
30999 31 34 33 2c 20 20 33 33 39 2c 0a 20 2f 2a 20 20  143,  339,. /*  
3099a 20 37 35 30 20 2a 2f 20 20 20 36 32 32 2c 20 20   750 */   622,  
3099b 33 31 38 2c 20 20 34 34 36 2c 20 20 31 33 33 2c  318,  446,  133,
3099c 20 20 34 30 38 2c 20 20 32 35 37 2c 20 20 33 33    408,  257,  33
3099d 36 2c 20 20 34 32 36 2c 20 20 33 32 31 2c 20 20  6,  426,  321,  
3099e 33 33 36 2c 0a 20 2f 2a 20 20 20 37 36 30 20 2a  336,. /*   760 *
3099f 2f 20 20 20 33 35 37 2c 20 20 33 33 39 2c 20 20  /   357,  339,  
309a0 32 37 32 2c 20 20 34 32 36 2c 20 20 33 33 36 2c  272,  426,  336,
309a1 20 20 34 34 36 2c 20 20 31 33 35 2c 20 20 31 38    446,  135,  18
309a2 34 2c 20 20 34 34 36 2c 20 20 20 36 31 2c 0a 20  4,  446,   61,. 
309a3 2f 2a 20 20 20 37 37 30 20 2a 2f 20 20 20 34 35  /*   770 */   45
309a4 37 2c 20 20 34 36 33 2c 20 20 32 31 39 2c 20 20  7,  463,  219,  
309a5 34 34 36 2c 20 20 31 30 36 2c 20 20 34 32 36 2c  446,  106,  426,
309a6 20 20 33 33 36 2c 20 20 34 39 33 2c 20 20 33 34    336,  493,  34
309a7 31 2c 20 20 32 33 34 2c 0a 20 2f 2a 20 20 20 37  1,  234,. /*   7
309a8 38 30 20 2a 2f 20 20 20 34 36 38 2c 20 20 34 36  80 */   468,  46
309a9 38 2c 20 20 36 32 31 2c 20 20 33 31 30 2c 20 20  8,  621,  310,  
309aa 34 30 37 2c 20 20 34 34 36 2c 20 20 31 30 32 2c  407,  446,  102,
309ab 20 20 32 30 39 2c 20 20 31 34 34 2c 20 20 20 32    209,  144,   2
309ac 33 2c 0a 20 2f 2a 20 20 20 37 39 30 20 2a 2f 20  3,. /*   790 */ 
309ad 20 20 20 32 32 2c 20 20 20 33 32 2c 20 20 34 36     22,   32,  46
309ae 35 2c 20 20 34 36 36 2c 20 20 34 36 34 2c 20 20  5,  466,  464,  
309af 34 36 34 2c 20 20 20 32 35 2c 20 20 20 32 35 2c  464,   25,   25,
309b0 20 20 20 32 34 2c 20 20 20 32 34 2c 0a 20 2f 2a     24,   24,. /*
309b1 20 20 20 38 30 30 20 2a 2f 20 20 20 20 32 34 2c     800 */    24,
309b2 20 20 20 32 34 2c 20 20 33 33 39 2c 20 20 20 32     24,  339,   2
309b3 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  6,   26,   26,  
309b4 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32 37 2c   26,   27,   27,
309b5 20 20 20 32 38 2c 0a 20 2f 2a 20 20 20 38 31 30     28,. /*   810
309b6 20 2a 2f 20 20 20 20 32 38 2c 20 20 20 32 38 2c   */    28,   28,
309b7 20 20 20 32 39 2c 20 20 32 32 31 2c 20 20 33 31     29,  221,  31
309b8 32 2c 20 20 33 33 39 2c 20 20 32 37 31 2c 20 20  2,  339,  271,  
309b9 33 33 36 2c 20 20 33 33 39 2c 20 20 33 34 31 2c  336,  339,  341,
309ba 0a 20 2f 2a 20 20 20 38 32 30 20 2a 2f 20 20 20  . /*   820 */   
309bb 35 33 38 2c 20 20 34 36 38 2c 20 20 34 36 38 2c  538,  468,  468,
309bc 20 20 35 37 32 2c 20 20 33 38 33 2c 20 20 34 39    572,  383,  49
309bd 36 2c 20 20 34 34 36 2c 20 20 20 37 39 2c 20 20  6,  446,   79,  
309be 34 39 39 2c 20 20 35 34 39 2c 0a 20 2f 2a 20 20  499,  549,. /*  
309bf 20 38 33 30 20 2a 2f 20 20 20 33 33 36 2c 20 20   830 */   336,  
309c0 34 32 36 2c 20 20 35 30 38 2c 20 20 33 33 36 2c  426,  508,  336,
309c1 20 20 35 30 38 2c 20 20 33 34 31 2c 20 20 33 33    508,  341,  33
309c2 39 2c 20 20 34 36 38 2c 20 20 34 36 38 2c 20 20  9,  468,  468,  
309c3 34 34 36 2c 0a 20 2f 2a 20 20 20 38 34 30 20 2a  446,. /*   840 *
309c4 2f 20 20 20 31 30 33 2c 20 20 33 39 31 2c 20 20  /   103,  391,  
309c5 34 34 36 2c 20 20 20 37 30 2c 20 20 34 35 37 2c  446,   70,  457,
309c6 20 20 34 36 33 2c 20 20 35 37 32 2c 20 20 34 32    463,  572,  42
309c7 36 2c 20 20 20 34 30 2c 20 20 34 32 36 2c 0a 20  6,   40,  426,. 
309c8 2f 2a 20 20 20 38 35 30 20 2a 2f 20 20 20 20 34  /*   850 */    4
309c9 32 2c 20 20 33 33 36 2c 20 20 32 32 30 2c 20 20  2,  336,  220,  
309ca 33 32 34 2c 20 20 35 30 34 2c 20 20 33 34 31 2c  324,  504,  341,
309cb 20 20 34 32 36 2c 20 20 34 36 38 2c 20 20 34 36    426,  468,  46
309cc 38 2c 20 20 20 31 38 2c 0a 20 2f 2a 20 20 20 38  8,   18,. /*   8
309cd 36 30 20 2a 2f 20 20 20 34 34 36 2c 20 20 31 30  60 */   446,  10
309ce 30 2c 20 20 32 36 36 2c 20 20 20 32 33 2c 20 20  0,  266,   23,  
309cf 20 32 32 2c 20 20 20 33 32 2c 20 20 34 36 35 2c   22,   32,  465,
309d0 20 20 34 36 36 2c 20 20 34 36 34 2c 20 20 34 36    466,  464,  46
309d1 34 2c 0a 20 2f 2a 20 20 20 38 37 30 20 2a 2f 20  4,. /*   870 */ 
309d2 20 20 20 32 35 2c 20 20 20 32 35 2c 20 20 20 32     25,   25,   2
309d3 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  4,   24,   24,  
309d4 20 32 34 2c 20 20 33 33 39 2c 20 20 20 32 36 2c   24,  339,   26,
309d5 20 20 20 32 36 2c 20 20 20 32 36 2c 0a 20 2f 2a     26,   26,. /*
309d6 20 20 20 38 38 30 20 2a 2f 20 20 20 20 32 36 2c     880 */    26,
309d7 20 20 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32     27,   27,   2
309d8 38 2c 20 20 20 32 38 2c 20 20 20 32 38 2c 20 20  8,   28,   28,  
309d9 20 32 39 2c 20 20 32 32 31 2c 20 20 33 31 32 2c   29,  221,  312,
309da 20 20 33 33 39 2c 0a 20 2f 2a 20 20 20 38 39 30    339,. /*   890
309db 20 2a 2f 20 20 20 32 38 33 2c 20 20 33 33 36 2c   */   283,  336,
309dc 20 20 33 33 39 2c 20 20 32 36 31 2c 20 20 35 34    339,  261,  54
309dd 38 2c 20 20 33 38 34 2c 20 20 33 33 39 2c 20 20  8,  384,  339,  
309de 33 32 37 2c 20 20 31 34 32 2c 20 20 35 35 30 2c  327,  142,  550,
309df 0a 20 2f 2a 20 20 20 39 30 30 20 2a 2f 20 20 20  . /*   900 */   
309e0 34 34 36 2c 20 20 31 33 36 2c 20 20 34 37 35 2c  446,  136,  475,
309e1 20 20 34 37 35 2c 20 20 33 33 36 2c 20 20 34 32    475,  336,  42
309e2 36 2c 20 20 31 38 35 2c 20 20 33 33 36 2c 20 20  6,  185,  336,  
309e3 34 39 39 2c 20 20 33 39 36 2c 0a 20 2f 2a 20 20  499,  396,. /*  
309e4 20 39 31 30 20 2a 2f 20 20 20 33 33 39 2c 20 20   910 */   339,  
309e5 33 33 36 2c 20 20 33 37 30 2c 20 20 34 34 36 2c  336,  370,  446,
309e6 20 20 31 33 37 2c 20 20 32 35 36 2c 20 20 34 34    137,  256,  44
309e7 36 2c 20 20 31 33 38 2c 20 20 34 35 37 2c 20 20  6,  138,  457,  
309e8 34 36 33 2c 0a 20 2f 2a 20 20 20 39 32 30 20 2a  463,. /*   920 *
309e9 2f 20 20 20 34 34 36 2c 20 20 20 37 31 2c 20 20  /   446,   71,  
309ea 34 39 39 2c 20 20 33 36 30 2c 20 20 34 32 36 2c  499,  360,  426,
309eb 20 20 33 33 36 2c 20 20 31 36 31 2c 20 20 33 31    336,  161,  31
309ec 31 2c 20 20 36 32 33 2c 20 20 32 31 35 2c 0a 20  1,  623,  215,. 
309ed 2f 2a 20 20 20 39 33 30 20 2a 2f 20 20 20 34 32  /*   930 */   42
309ee 36 2c 20 20 33 35 39 2c 20 20 32 33 37 2c 20 20  6,  359,  237,  
309ef 34 31 32 2c 20 20 34 34 36 2c 20 20 20 38 32 2c  412,  446,   82,
309f0 20 20 32 30 30 2c 20 20 20 32 33 2c 20 20 20 33    200,   23,   3
309f1 34 2c 20 20 20 33 32 2c 0a 20 2f 2a 20 20 20 39  4,   32,. /*   9
309f2 34 30 20 2a 2f 20 20 20 34 36 35 2c 20 20 34 36  40 */   465,  46
309f3 36 2c 20 20 34 36 34 2c 20 20 34 36 34 2c 20 20  6,  464,  464,  
309f4 20 32 35 2c 20 20 20 32 35 2c 20 20 20 32 34 2c   25,   25,   24,
309f5 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 20 32     24,   24,   2
309f6 34 2c 0a 20 2f 2a 20 20 20 39 35 30 20 2a 2f 20  4,. /*   950 */ 
309f7 20 20 33 33 39 2c 20 20 20 32 36 2c 20 20 20 32    339,   26,   2
309f8 36 2c 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20  6,   26,   26,  
309f9 20 32 37 2c 20 20 20 32 37 2c 20 20 20 32 38 2c   27,   27,   28,
309fa 20 20 20 32 38 2c 20 20 20 32 38 2c 0a 20 2f 2a     28,   28,. /*
309fb 20 20 20 39 36 30 20 2a 2f 20 20 20 20 32 39 2c     960 */    29,
309fc 20 20 32 32 31 2c 20 20 33 31 32 2c 20 20 34 34    221,  312,  44
309fd 37 2c 20 20 32 37 31 2c 20 20 33 33 36 2c 20 20  7,  271,  336,  
309fe 33 33 39 2c 20 20 32 37 31 2c 20 20 33 34 30 2c  339,  271,  340,
309ff 20 20 32 31 30 2c 0a 20 2f 2a 20 20 20 39 37 30    210,. /*   970
30a00 20 2a 2f 20 20 20 34 34 37 2c 20 20 31 37 32 2c   */   447,  172,
30a01 20 20 36 32 35 2c 20 20 32 31 31 2c 20 20 34 34    625,  211,  44
30a02 36 2c 20 20 20 38 33 2c 20 20 32 34 30 2c 20 20  6,   83,  240,  
30a03 35 35 32 2c 20 20 31 34 32 2c 20 20 34 32 36 2c  552,  142,  426,
30a04 0a 20 2f 2a 20 20 20 39 38 30 20 2a 2f 20 20 20  . /*   980 */   
30a05 33 32 31 2c 20 20 33 33 36 2c 20 20 34 32 36 2c  321,  336,  426,
30a06 20 20 34 32 36 2c 20 20 33 33 39 2c 20 20 34 31    426,  339,  41
30a07 34 2c 20 20 33 33 31 2c 20 20 31 38 31 2c 20 20  4,  331,  181,  
30a08 34 35 38 2c 20 20 34 35 39 2c 0a 20 2f 2a 20 20  458,  459,. /*  
30a09 20 39 39 30 20 2a 2f 20 20 20 34 34 36 2c 20 20   990 */   446,  
30a0a 20 37 32 2c 20 20 34 35 37 2c 20 20 34 36 33 2c   72,  457,  463,
30a0b 20 20 34 37 30 2c 20 20 35 30 36 2c 20 20 20 36    470,  506,   6
30a0c 37 2c 20 20 31 35 38 2c 20 20 33 39 34 2c 20 20  7,  158,  394,  
30a0d 33 33 36 2c 0a 20 2f 2a 20 20 31 30 30 30 20 2a  336,. /*  1000 *
30a0e 2f 20 20 20 35 38 37 2c 20 20 33 32 35 2c 20 20  /   587,  325,  
30a0f 34 39 39 2c 20 20 34 34 37 2c 20 20 33 32 36 2c  499,  447,  326,
30a10 20 20 33 31 31 2c 20 20 36 32 34 2c 20 20 34 34    311,  624,  44
30a11 37 2c 20 20 34 34 36 2c 20 20 20 38 34 2c 0a 20  7,  446,   84,. 
30a12 2f 2a 20 20 31 30 31 30 20 2a 2f 20 20 20 34 36  /*  1010 */   46
30a13 31 2c 20 20 34 36 32 2c 20 20 20 32 32 2c 20 20  1,  462,   22,  
30a14 20 33 32 2c 20 20 34 36 35 2c 20 20 34 36 36 2c   32,  465,  466,
30a15 20 20 34 36 34 2c 20 20 34 36 34 2c 20 20 20 32    464,  464,   2
30a16 35 2c 20 20 20 32 35 2c 0a 20 2f 2a 20 20 31 30  5,   25,. /*  10
30a17 32 30 20 2a 2f 20 20 20 20 32 34 2c 20 20 20 32  20 */    24,   2
30a18 34 2c 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20  4,   24,   24,  
30a19 33 33 39 2c 20 20 20 32 36 2c 20 20 20 32 36 2c  339,   26,   26,
30a1a 20 20 20 32 36 2c 20 20 20 32 36 2c 20 20 20 32     26,   26,   2
30a1b 37 2c 0a 20 2f 2a 20 20 31 30 33 30 20 2a 2f 20  7,. /*  1030 */ 
30a1c 20 20 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32     27,   28,   2
30a1d 38 2c 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20  8,   28,   29,  
30a1e 32 32 31 2c 20 20 33 31 32 2c 20 20 34 36 30 2c  221,  312,  460,
30a1f 20 20 33 33 39 2c 20 20 33 33 36 2c 0a 20 2f 2a    339,  336,. /*
30a20 20 20 31 30 34 30 20 2a 2f 20 20 20 33 33 39 2c    1040 */   339,
30a21 20 20 32 38 33 2c 20 20 34 32 33 2c 20 20 33 39    283,  423,  39
30a22 33 2c 20 20 35 33 32 2c 20 20 35 33 33 2c 20 20  3,  532,  533,  
30a23 32 30 34 2c 20 20 32 30 35 2c 20 20 34 34 36 2c  204,  205,  446,
30a24 20 20 20 38 35 2c 0a 20 2f 2a 20 20 31 30 35 30     85,. /*  1050
30a25 20 2a 2f 20 20 20 36 32 35 2c 20 20 33 39 32 2c   */   625,  392,
30a26 20 20 35 34 37 2c 20 20 33 33 36 2c 20 20 31 36    547,  336,  16
30a27 32 2c 20 20 33 33 36 2c 20 20 34 32 36 2c 20 20  2,  336,  426,  
30a28 34 32 36 2c 20 20 33 33 39 2c 20 20 34 33 35 2c  426,  339,  435,
30a29 0a 20 2f 2a 20 20 31 30 36 30 20 2a 2f 20 20 20  . /*  1060 */   
30a2a 34 33 36 2c 20 20 33 33 39 2c 20 20 34 34 36 2c  436,  339,  446,
30a2b 20 20 31 30 34 2c 20 20 34 34 36 2c 20 20 20 38    104,  446,   8
30a2c 36 2c 20 20 34 35 37 2c 20 20 34 36 33 2c 20 20  6,  457,  463,  
30a2d 32 36 34 2c 20 20 32 39 31 2c 0a 20 2f 2a 20 20  264,  291,. /*  
30a2e 31 30 37 30 20 2a 2f 20 20 20 32 37 34 2c 20 20  1070 */   274,  
30a2f 20 34 39 2c 20 20 31 36 32 2c 20 20 33 33 36 2c   49,  162,  336,
30a30 20 20 34 32 36 2c 20 20 34 32 36 2c 20 20 33 33    426,  426,  33
30a31 36 2c 20 20 32 39 37 2c 20 20 32 36 35 2c 20 20  6,  297,  265,  
30a32 35 34 32 2c 0a 20 2f 2a 20 20 31 30 38 30 20 2a  542,. /*  1080 *
30a33 2f 20 20 20 35 34 31 2c 20 20 34 30 35 2c 20 20  /   541,  405,  
30a34 34 34 36 2c 20 20 20 38 38 2c 20 20 35 39 34 2c  446,   88,  594,
30a35 20 20 34 34 36 2c 20 20 20 38 39 2c 20 20 20 33    446,   89,   3
30a36 32 2c 20 20 34 36 35 2c 20 20 34 36 36 2c 0a 20  2,  465,  466,. 
30a37 2f 2a 20 20 31 30 39 30 20 2a 2f 20 20 20 34 36  /*  1090 */   46
30a38 34 2c 20 20 34 36 34 2c 20 20 20 32 35 2c 20 20  4,  464,   25,  
30a39 20 32 35 2c 20 20 20 32 34 2c 20 20 20 32 34 2c   25,   24,   24,
30a3a 20 20 20 32 34 2c 20 20 20 32 34 2c 20 20 36 30     24,   24,  60
30a3b 30 2c 20 20 20 32 36 2c 0a 20 2f 2a 20 20 31 31  0,   26,. /*  11
30a3c 30 30 20 2a 2f 20 20 20 20 32 36 2c 20 20 20 32  00 */    26,   2
30a3d 36 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  6,   26,   27,  
30a3e 20 32 37 2c 20 20 20 32 38 2c 20 20 20 32 38 2c   27,   28,   28,
30a3f 20 20 20 32 38 2c 20 20 20 32 39 2c 20 20 32 32     28,   29,  22
30a40 31 2c 0a 20 2f 2a 20 20 31 31 31 30 20 2a 2f 20  1,. /*  1110 */ 
30a41 20 20 20 33 36 2c 20 20 33 34 35 2c 20 20 33 33     36,  345,  33
30a42 39 2c 20 20 20 20 33 2c 20 20 32 31 34 2c 20 20  9,    3,  214,  
30a43 20 20 38 2c 20 20 34 32 32 2c 20 20 33 33 35 2c    8,  422,  335,
30a44 20 20 34 32 35 2c 20 20 34 33 37 2c 0a 20 2f 2a    425,  437,. /*
30a45 20 20 31 31 32 30 20 2a 2f 20 20 20 33 37 35 2c    1120 */   375,
30a46 20 20 31 34 38 2c 20 20 31 36 32 2c 20 20 20 33    148,  162,   3
30a47 36 2c 20 20 33 34 35 2c 20 20 33 33 39 2c 20 20  6,  345,  339,  
30a48 20 20 33 2c 20 20 33 33 36 2c 20 20 33 34 32 2c    3,  336,  342,
30a49 20 20 34 33 32 2c 0a 20 2f 2a 20 20 31 31 33 30    432,. /*  1130
30a4a 20 2a 2f 20 20 20 33 33 35 2c 20 20 34 32 35 2c   */   335,  425,
30a4b 20 20 31 34 39 2c 20 20 35 37 37 2c 20 20 34 32    149,  577,  42
30a4c 36 2c 20 20 31 36 32 2c 20 20 34 34 36 2c 20 20  6,  162,  446,  
30a4d 20 39 30 2c 20 20 31 35 31 2c 20 20 33 33 39 2c   90,  151,  339,
30a4e 0a 20 2f 2a 20 20 31 31 34 30 20 2a 2f 20 20 20  . /*  1140 */   
30a4f 33 33 36 2c 20 20 33 34 32 2c 20 20 34 33 34 2c  336,  342,  434,
30a50 20 20 33 33 39 2c 20 20 32 38 33 2c 20 20 34 33    339,  283,  43
30a51 33 2c 20 20 33 33 33 2c 20 20 33 34 37 2c 20 20  3,  333,  347,  
30a52 34 34 37 2c 20 20 34 34 36 2c 0a 20 2f 2a 20 20  447,  446,. /*  
30a53 31 31 35 30 20 2a 2f 20 20 20 20 37 35 2c 20 20  1150 */    75,  
30a54 35 38 38 2c 20 20 20 20 36 2c 20 20 31 35 38 2c  588,    6,  158,
30a55 20 20 33 33 36 2c 20 20 34 34 38 2c 20 20 31 34    336,  448,  14
30a56 30 2c 20 20 34 38 31 2c 20 20 33 33 36 2c 20 20  0,  481,  336,  
30a57 34 32 36 2c 0a 20 2f 2a 20 20 31 31 36 30 20 2a  426,. /*  1160 *
30a58 2f 20 20 20 33 34 37 2c 20 20 34 35 33 2c 20 20  /   347,  453,  
30a59 33 33 34 2c 20 20 34 34 36 2c 20 20 20 37 36 2c  334,  446,   76,
30a5a 20 20 20 34 39 2c 20 20 33 35 30 2c 20 20 34 34     49,  350,  44
30a5b 36 2c 20 20 20 39 31 2c 20 20 20 20 37 2c 0a 20  6,   91,    7,. 
30a5c 2f 2a 20 20 31 31 37 30 20 2a 2f 20 20 20 34 38  /*  1170 */   48
30a5d 31 2c 20 20 34 32 36 2c 20 20 33 39 37 2c 20 20  1,  426,  397,  
30a5e 32 38 33 2c 20 20 33 35 35 2c 20 20 32 35 30 2c  283,  355,  250,
30a5f 20 20 34 32 36 2c 20 20 20 33 39 2c 20 20 20 33    426,   39,   3
30a60 38 2c 20 20 32 35 31 2c 0a 20 2f 2a 20 20 31 31  8,  251,. /*  11
30a61 38 30 20 2a 2f 20 20 20 33 33 39 2c 20 20 34 32  80 */   339,  42
30a62 36 2c 20 20 20 34 38 2c 20 20 33 35 33 2c 20 20  6,   48,  353,  
30a63 20 33 37 2c 20 20 33 33 37 2c 20 20 33 33 38 2c   37,  337,  338,
30a64 20 20 35 39 36 2c 20 20 34 32 36 2c 20 20 34 35    596,  426,  45
30a65 32 2c 0a 20 2f 2a 20 20 31 31 39 30 20 2a 2f 20  2,. /*  1190 */ 
30a66 20 20 20 33 39 2c 20 20 20 33 38 2c 20 20 35 31     39,   38,  51
30a67 34 2c 20 20 32 35 32 2c 20 20 33 39 30 2c 20 20  4,  252,  390,  
30a68 33 33 36 2c 20 20 20 32 30 2c 20 20 20 33 37 2c  336,   20,   37,
30a69 20 20 33 33 37 2c 20 20 33 33 38 2c 0a 20 2f 2a    337,  338,. /*
30a6a 20 20 31 32 30 30 20 2a 2f 20 20 20 32 35 33 2c    1200 */   253,
30a6b 20 20 20 34 33 2c 20 20 34 35 32 2c 20 20 32 30     43,  452,  20
30a6c 36 2c 20 20 34 34 36 2c 20 20 20 39 32 2c 20 20  6,  446,   92,  
30a6d 32 31 39 2c 20 20 34 34 39 2c 20 20 32 34 32 2c  219,  449,  242,
30a6e 20 20 32 34 33 2c 0a 20 2f 2a 20 20 31 32 31 30    243,. /*  1210
30a6f 20 2a 2f 20 20 20 32 34 34 2c 20 20 31 35 30 2c   */   244,  150,
30a70 20 20 32 34 36 2c 20 20 32 38 33 2c 20 20 34 39    246,  283,  49
30a71 31 2c 20 20 35 39 33 2c 20 20 35 39 37 2c 20 20  1,  593,  597,  
30a72 34 39 30 2c 20 20 32 32 34 2c 20 20 32 35 38 2c  490,  224,  258,
30a73 0a 20 2f 2a 20 20 31 32 32 30 20 2a 2f 20 20 20  . /*  1220 */   
30a74 34 35 34 2c 20 20 34 35 34 2c 20 20 34 35 34 2c  454,  454,  454,
30a75 20 20 34 35 35 2c 20 20 34 35 36 2c 20 20 20 31    455,  456,   1
30a76 30 2c 20 20 35 30 33 2c 20 20 31 38 33 2c 20 20  0,  503,  183,  
30a77 34 32 36 2c 20 20 31 37 38 2c 0a 20 2f 2a 20 20  426,  178,. /*  
30a78 31 32 33 30 20 2a 2f 20 20 20 31 35 36 2c 20 20  1230 */   156,  
30a79 33 30 31 2c 20 20 34 32 36 2c 20 20 34 35 34 2c  301,  426,  454,
30a7a 20 20 34 35 34 2c 20 20 34 35 34 2c 20 20 34 35    454,  454,  45
30a7b 35 2c 20 20 34 35 36 2c 20 20 20 31 30 2c 20 20  5,  456,   10,  
30a7c 33 33 39 2c 0a 20 2f 2a 20 20 31 32 34 30 20 2a  339,. /*  1240 *
30a7d 2f 20 20 20 33 30 32 2c 20 20 34 32 36 2c 20 20  /   302,  426,  
30a7e 20 33 36 2c 20 20 33 34 35 2c 20 20 20 35 30 2c   36,  345,   50,
30a7f 20 20 20 20 33 2c 20 20 33 33 39 2c 20 20 35 30      3,  339,  50
30a80 35 2c 20 20 32 36 30 2c 20 20 33 33 35 2c 0a 20  5,  260,  335,. 
30a81 2f 2a 20 20 31 32 35 30 20 2a 2f 20 20 20 34 32  /*  1250 */   42
30a82 35 2c 20 20 32 36 32 2c 20 20 33 33 39 2c 20 20  5,  262,  339,  
30a83 31 37 36 2c 20 20 33 33 36 2c 20 20 35 38 31 2c  176,  336,  581,
30a84 20 20 35 39 38 2c 20 20 33 35 38 2c 20 20 33 36    598,  358,  36
30a85 34 2c 20 20 31 37 35 2c 0a 20 2f 2a 20 20 31 32  4,  175,. /*  12
30a86 36 30 20 2a 2f 20 20 20 33 34 32 2c 20 20 33 33  60 */   342,  33
30a87 36 2c 20 20 31 37 37 2c 20 20 34 34 36 2c 20 20  6,  177,  446,  
30a88 20 39 33 2c 20 20 20 34 36 2c 20 20 33 34 35 2c   93,   46,  345,
30a89 20 20 33 33 36 2c 20 20 20 20 33 2c 20 20 33 33    336,    3,  33
30a8a 39 2c 0a 20 2f 2a 20 20 31 32 37 30 20 2a 2f 20  9,. /*  1270 */ 
30a8b 20 20 34 34 36 2c 20 20 20 39 34 2c 20 20 33 33    446,   94,  33
30a8c 35 2c 20 20 34 32 35 2c 20 20 35 32 35 2c 20 20  5,  425,  525,  
30a8d 33 33 39 2c 20 20 34 34 36 2c 20 20 20 37 37 2c  339,  446,   77,
30a8e 20 20 33 32 30 2c 20 20 33 34 37 2c 0a 20 2f 2a    320,  347,. /*
30a8f 20 20 31 32 38 30 20 2a 2f 20 20 20 35 31 31 2c    1280 */   511,
30a90 20 20 33 33 39 2c 20 20 35 30 37 2c 20 20 33 34    339,  507,  34
30a91 32 2c 20 20 33 33 36 2c 20 20 35 38 39 2c 20 20  2,  336,  589,  
30a92 36 30 31 2c 20 20 20 35 36 2c 20 20 20 35 36 2c  601,   56,   56,
30a93 20 20 34 38 31 2c 0a 20 2f 2a 20 20 31 32 39 30    481,. /*  1290
30a94 20 2a 2f 20 20 20 33 33 36 2c 20 20 35 31 32 2c   */   336,  512,
30a95 20 20 32 38 33 2c 20 20 34 34 36 2c 20 20 20 31    283,  446,   1
30a96 37 2c 20 20 35 33 31 2c 20 20 33 33 36 2c 20 20  7,  531,  336,  
30a97 34 32 36 2c 20 20 35 33 30 2c 20 20 34 34 36 2c  426,  530,  446,
30a98 0a 20 2f 2a 20 20 31 33 30 30 20 2a 2f 20 20 20  . /*  1300 */   
30a99 20 39 36 2c 20 20 35 33 34 2c 20 20 33 34 37 2c   96,  534,  347,
30a9a 20 20 34 30 34 2c 20 20 32 39 38 2c 20 20 34 34    404,  298,  44
30a9b 36 2c 20 20 20 39 37 2c 20 20 34 32 36 2c 20 20  6,   97,  426,  
30a9c 33 31 33 2c 20 20 20 33 39 2c 0a 20 2f 2a 20 20  313,   39,. /*  
30a9d 31 33 31 30 20 2a 2f 20 20 20 20 33 38 2c 20 20  1310 */    38,  
30a9e 32 36 37 2c 20 20 34 38 31 2c 20 20 32 31 39 2c  267,  481,  219,
30a9f 20 20 35 33 35 2c 20 20 35 33 36 2c 20 20 20 33    535,  536,   3
30aa0 37 2c 20 20 33 33 37 2c 20 20 33 33 38 2c 20 20  7,  337,  338,  
30aa1 32 38 33 2c 0a 20 2f 2a 20 20 31 33 32 30 20 2a  283,. /*  1320 *
30aa2 2f 20 20 20 36 32 30 2c 20 20 34 35 32 2c 20 20  /   620,  452,  
30aa3 33 30 39 2c 20 20 32 38 33 2c 20 20 31 31 31 2c  309,  283,  111,
30aa4 20 20 20 31 39 2c 20 20 32 38 38 2c 20 20 35 30     19,  288,  50
30aa5 39 2c 20 20 32 36 39 2c 20 20 34 32 34 2c 0a 20  9,  269,  424,. 
30aa6 2f 2a 20 20 31 33 33 30 20 2a 2f 20 20 20 34 32  /*  1330 */   42
30aa7 35 2c 20 20 35 33 39 2c 20 20 20 33 39 2c 20 20  5,  539,   39,  
30aa8 20 33 38 2c 20 20 34 32 36 2c 20 20 32 33 38 2c   38,  426,  238,
30aa9 20 20 32 37 30 2c 20 20 34 31 31 2c 20 20 34 32    270,  411,  42
30aaa 36 2c 20 20 20 33 37 2c 0a 20 2f 2a 20 20 31 33  6,   37,. /*  13
30aab 34 30 20 2a 2f 20 20 20 33 33 37 2c 20 20 33 33  40 */   337,  33
30aac 38 2c 20 20 34 32 36 2c 20 20 34 32 36 2c 20 20  8,  426,  426,  
30aad 34 35 32 2c 20 20 35 35 38 2c 20 20 34 32 36 2c  452,  558,  426,
30aae 20 20 33 30 37 2c 20 20 32 33 31 2c 20 20 32 37    307,  231,  27
30aaf 36 2c 0a 20 2f 2a 20 20 31 33 35 30 20 2a 2f 20  6,. /*  1350 */ 
30ab0 20 20 32 37 38 2c 20 20 34 32 36 2c 20 20 34 35    278,  426,  45
30ab1 34 2c 20 20 34 35 34 2c 20 20 34 35 34 2c 20 20  4,  454,  454,  
30ab2 34 35 35 2c 20 20 34 35 36 2c 20 20 20 31 30 2c  455,  456,   10,
30ab3 20 20 35 35 33 2c 20 20 32 38 30 2c 0a 20 2f 2a    553,  280,. /*
30ab4 20 20 31 33 36 30 20 2a 2f 20 20 20 34 32 36 2c    1360 */   426,
30ab5 20 20 35 35 39 2c 20 20 32 33 39 2c 20 20 32 33    559,  239,  23
30ab6 30 2c 20 20 34 32 36 2c 20 20 34 32 36 2c 20 20  0,  426,  426,  
30ab7 32 39 39 2c 20 20 32 38 32 2c 20 20 32 38 37 2c  299,  282,  287,
30ab8 20 20 34 38 31 2c 0a 20 2f 2a 20 20 31 33 37 30    481,. /*  1370
30ab9 20 2a 2f 20 20 20 35 36 30 2c 20 20 33 38 38 2c   */   560,  388,
30aba 20 20 35 38 34 2c 20 20 32 33 32 2c 20 20 34 32    584,  232,  42
30abb 36 2c 20 20 34 35 34 2c 20 20 34 35 34 2c 20 20  6,  454,  454,  
30abc 34 35 34 2c 20 20 34 35 35 2c 20 20 34 35 36 2c  454,  455,  456,
30abd 0a 20 2f 2a 20 20 31 33 38 30 20 2a 2f 20 20 20  . /*  1380 */   
30abe 20 31 30 2c 20 20 35 36 31 2c 20 20 34 32 36 2c   10,  561,  426,
30abf 20 20 34 32 36 2c 20 20 35 38 35 2c 20 20 33 39    426,  585,  39
30ac0 35 2c 20 20 34 32 36 2c 20 20 34 32 36 2c 20 20  5,  426,  426,  
30ac1 32 39 32 2c 20 20 31 39 34 2c 0a 20 2f 2a 20 20  292,  194,. /*  
30ac2 31 33 39 30 20 2a 2f 20 20 20 31 39 35 2c 20 20  1390 */   195,  
30ac3 35 39 32 2c 20 20 36 30 33 2c 20 20 33 30 30 2c  592,  603,  300,
30ac4 20 20 33 30 33 2c 20 20 33 30 38 2c 20 20 33 37    303,  308,  37
30ac5 37 2c 20 20 35 32 32 2c 20 20 33 38 31 2c 20 20  7,  522,  381,  
30ac6 34 32 36 2c 0a 20 2f 2a 20 20 31 34 30 30 20 2a  426,. /*  1400 *
30ac7 2f 20 20 20 34 32 36 2c 20 20 34 35 32 2c 20 20  /   426,  452,  
30ac8 35 36 37 2c 20 20 34 32 36 2c 20 20 33 30 34 2c  567,  426,  304,
30ac9 20 20 36 31 37 2c 20 20 34 32 36 2c 20 20 34 32    617,  426,  42
30aca 36 2c 20 20 34 32 36 2c 20 20 34 32 36 2c 0a 20  6,  426,  426,. 
30acb 2f 2a 20 20 31 34 31 30 20 2a 2f 20 20 20 33 37  /*  1410 */   37
30acc 39 2c 20 20 20 35 33 2c 20 20 31 34 37 2c 20 20  9,   53,  147,  
30acd 31 36 35 2c 20 20 31 36 36 2c 20 20 31 36 37 2c  165,  166,  167,
30ace 20 20 35 38 30 2c 20 20 32 31 32 2c 20 20 35 36    580,  212,  56
30acf 39 2c 20 20 34 32 36 2c 0a 20 2f 2a 20 20 31 34  9,  426,. /*  14
30ad0 32 30 20 2a 2f 20 20 20 34 32 36 2c 20 20 32 38  20 */   426,  28
30ad1 35 2c 20 20 31 36 38 2c 20 20 35 37 30 2c 20 20  5,  168,  570,  
30ad2 33 38 37 2c 20 20 31 32 30 2c 20 20 31 32 33 2c  387,  120,  123,
30ad3 20 20 31 38 37 2c 20 20 35 39 30 2c 20 20 34 30    187,  590,  40
30ad4 32 2c 0a 20 2f 2a 20 20 31 34 33 30 20 2a 2f 20  2,. /*  1430 */ 
30ad5 20 20 34 30 33 2c 20 20 31 32 35 2c 20 20 34 35    403,  125,  45
30ad6 34 2c 20 20 34 35 34 2c 20 20 34 35 34 2c 20 20  4,  454,  454,  
30ad7 33 33 30 2c 20 20 35 39 39 2c 20 20 36 31 34 2c  330,  599,  614,
30ad8 20 20 31 38 36 2c 20 20 31 32 36 2c 0a 20 2f 2a    186,  126,. /*
30ad9 20 20 31 34 34 30 20 2a 2f 20 20 20 31 32 37 2c    1440 */   127,
30ada 20 20 31 32 38 2c 20 20 36 31 35 2c 20 20 36 31    128,  615,  61
30adb 36 2c 20 20 20 35 37 2c 20 20 20 36 30 2c 20 20  6,   57,   60,  
30adc 36 31 39 2c 20 20 31 30 37 2c 20 20 32 32 39 2c  619,  107,  229,
30add 20 20 20 36 34 2c 0a 20 2f 2a 20 20 31 34 35 30     64,. /*  1450
30ade 20 2a 2f 20 20 20 31 31 35 2c 20 20 34 32 30 2c   */   115,  420,
30adf 20 20 32 34 35 2c 20 20 31 33 30 2c 20 20 34 33    245,  130,  43
30ae0 39 2c 20 20 31 38 30 2c 20 20 33 31 35 2c 20 20  9,  180,  315,  
30ae1 32 30 37 2c 20 20 36 37 30 2c 20 20 33 31 36 2c  207,  670,  316,
30ae2 0a 20 2f 2a 20 20 31 34 36 30 20 2a 2f 20 20 20  . /*  1460 */   
30ae3 36 37 31 2c 20 20 34 36 37 2c 20 20 36 37 32 2c  671,  467,  672,
30ae4 20 20 31 35 33 2c 20 20 31 35 34 2c 20 20 20 33    153,  154,   3
30ae5 35 2c 20 20 34 38 33 2c 20 20 34 37 31 2c 20 20  5,  483,  471,  
30ae6 34 38 30 2c 20 20 31 38 38 2c 0a 20 2f 2a 20 20  480,  188,. /*  
30ae7 31 34 37 30 20 2a 2f 20 20 20 32 30 31 2c 20 20  1470 */   201,  
30ae8 31 35 35 2c 20 20 34 38 34 2c 20 20 20 20 35 2c  155,  484,    5,
30ae9 20 20 34 38 35 2c 20 20 34 38 39 2c 20 20 20 31    485,  489,   1
30aea 32 2c 20 20 35 30 32 2c 20 20 20 34 35 2c 20 20  2,  502,   45,  
30aeb 20 31 31 2c 0a 20 2f 2a 20 20 31 34 38 30 20 2a   11,. /*  1480 *
30aec 2f 20 20 20 31 31 30 2c 20 20 31 34 35 2c 20 20  /   110,  145,  
30aed 35 31 38 2c 20 20 35 31 39 2c 20 20 35 31 30 2c  518,  519,  510,
30aee 20 20 32 32 38 2c 20 20 20 35 31 2c 20 20 31 31    228,   51,  11
30aef 32 2c 20 20 33 36 39 2c 20 20 32 37 33 2c 0a 20  2,  369,  273,. 
30af0 2f 2a 20 20 31 34 39 30 20 2a 2f 20 20 20 31 31  /*  1490 */   11
30af1 33 2c 20 20 31 35 39 2c 20 20 35 34 35 2c 20 20  3,  159,  545,  
30af2 20 35 32 2c 20 20 33 37 34 2c 20 20 31 31 34 2c   52,  374,  114,
30af3 20 20 31 36 34 2c 20 20 32 36 35 2c 20 20 33 37    164,  265,  37
30af4 38 2c 20 20 31 39 30 2c 0a 20 2f 2a 20 20 31 35  8,  190,. /*  15
30af5 30 30 20 2a 2f 20 20 20 31 34 36 2c 20 20 35 36  00 */   146,  56
30af6 38 2c 20 20 31 31 37 2c 20 20 31 35 38 2c 20 20  8,  117,  158,  
30af7 32 38 36 2c 20 20 33 38 32 2c 20 20 31 36 39 2c  286,  382,  169,
30af8 20 20 31 31 39 2c 20 20 20 31 35 2c 20 20 35 38    119,   15,  58
30af9 33 2c 0a 20 2f 2a 20 20 31 35 31 30 20 2a 2f 20  3,. /*  1510 */ 
30afa 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20 31 32    170,  171,  12
30afb 31 2c 20 20 35 38 36 2c 20 20 31 32 32 2c 20 20  1,  586,  122,  
30afc 20 35 34 2c 20 20 20 35 35 2c 20 20 20 31 33 2c   54,   55,   13,
30afd 20 20 31 32 34 2c 20 20 35 39 31 2c 0a 20 2f 2a    124,  591,. /*
30afe 20 20 31 35 32 30 20 2a 2f 20 20 20 31 37 33 2c    1520 */   173,
30aff 20 20 31 37 34 2c 20 20 31 31 38 2c 20 20 35 37    174,  118,  57
30b00 35 2c 20 20 31 32 39 2c 20 20 35 39 35 2c 20 20  5,  129,  595,  
30b01 35 37 31 2c 20 20 31 33 31 2c 20 20 20 31 34 2c  571,  131,   14,
30b02 20 20 31 33 32 2c 0a 20 2f 2a 20 20 31 35 33 30    132,. /*  1530
30b03 20 2a 2f 20 20 20 36 31 31 2c 20 20 20 36 33 2c   */   611,   63,
30b04 20 20 36 31 32 2c 20 20 31 39 33 2c 20 20 36 30    612,  193,  60
30b05 32 2c 20 20 31 37 39 2c 20 20 33 30 35 2c 20 20  2,  179,  305,  
30b06 34 31 33 2c 20 20 34 31 37 2c 20 20 39 36 30 2c  413,  417,  960,
30b07 0a 20 2f 2a 20 20 31 35 34 30 20 2a 2f 20 20 20  . /*  1540 */   
30b08 36 32 37 2c 0a 7d 3b 0a 73 74 61 74 69 63 20 63  627,.};.static c
30b09 6f 6e 73 74 20 59 59 43 4f 44 45 54 59 50 45 20  onst YYCODETYPE 
30b0a 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 5d 20 3d  yy_lookahead[] =
30b0b 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20   {. /*     0 */ 
30b0c 20 20 20 31 39 2c 20 20 31 34 32 2c 20 20 31 34     19,  142,  14
30b0d 33 2c 20 20 31 34 34 2c 20 20 31 34 35 2c 20 20  3,  144,  145,  
30b0e 20 32 34 2c 20 20 31 31 35 2c 20 20 20 32 36 2c   24,  115,   26,
30b0f 20 20 20 37 37 2c 20 20 20 37 38 2c 0a 20 2f 2a     77,   78,. /*
30b10 20 20 20 20 31 30 20 2a 2f 20 20 20 20 37 39 2c      10 */    79,
30b11 20 20 20 38 30 2c 20 20 20 39 32 2c 20 20 20 38     80,   92,   8
30b12 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20  2,   83,   84,  
30b13 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c   85,   86,   87,
30b14 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20 20 32 30     88,. /*    20
30b15 20 2a 2f 20 20 20 20 38 39 2c 20 20 20 39 30 2c   */    89,   90,
30b16 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 32     91,   92,   2
30b17 36 2c 20 20 20 32 37 2c 20 20 32 32 32 2c 20 20  6,   27,  222,  
30b18 32 32 33 2c 20 20 32 32 34 2c 20 20 32 32 35 2c  223,  224,  225,
30b19 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20 20  . /*    30 */   
30b1a 20 34 39 2c 20 20 20 35 30 2c 20 20 20 38 31 2c   49,   50,   81,
30b1b 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
30b1c 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20  4,   85,   86,  
30b1d 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20   87,   88,. /*  
30b1e 20 20 34 30 20 2a 2f 20 20 20 20 38 39 2c 20 20    40 */    89,  
30b1f 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c   90,   91,   92,
30b20 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39     88,   89,   9
30b21 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20  0,   91,   92,  
30b22 20 36 38 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a   68,. /*    50 *
30b23 2f 20 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20  /    69,   70,  
30b24 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c   71,   72,   73,
30b25 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37     74,   75,   7
30b26 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 0a 20  6,   77,   78,. 
30b27 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 20 37  /*    60 */    7
30b28 39 2c 20 20 20 38 30 2c 20 20 20 31 36 2c 20 20  9,   80,   16,  
30b29 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
30b2a 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38     85,   86,   8
30b2b 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20 20  7,   88,. /*    
30b2c 37 30 20 2a 2f 20 20 20 20 38 39 2c 20 20 20 39  70 */    89,   9
30b2d 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20  0,   91,   92,  
30b2e 20 31 39 2c 20 20 20 39 34 2c 20 20 20 31 39 2c   19,   94,   19,
30b2f 20 20 31 36 36 2c 20 20 31 36 37 2c 20 20 31 36    166,  167,  16
30b30 38 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20  8,. /*    80 */ 
30b31 20 20 20 32 35 2c 20 20 20 38 32 2c 20 20 20 38     25,   82,   8
30b32 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20  3,   84,   85,  
30b33 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c   86,   87,   88,
30b34 20 20 20 38 39 2c 20 20 20 39 30 2c 0a 20 2f 2a     89,   90,. /*
30b35 20 20 20 20 39 30 20 2a 2f 20 20 20 20 39 31 2c      90 */    91,
30b36 20 20 20 39 32 2c 20 20 20 39 34 2c 20 20 20 39     92,   94,   9
30b37 35 2c 20 20 20 39 36 2c 20 20 31 35 30 2c 20 20  5,   96,  150,  
30b38 20 33 36 2c 20 20 20 39 39 2c 20 20 31 30 30 2c   36,   99,  100,
30b39 20 20 31 30 31 2c 0a 20 2f 2a 20 20 20 31 30 30    101,. /*   100
30b3a 20 2a 2f 20 20 20 31 37 34 2c 20 20 31 36 39 2c   */   174,  169,
30b3b 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20 20 34    170,  171,   4
30b3c 39 2c 20 20 20 35 30 2c 20 20 20 36 30 2c 20 20  9,   50,   60,  
30b3d 31 30 39 2c 20 20 20 36 32 2c 20 20 20 35 34 2c  109,   62,   54,
30b3e 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20 20  . /*   110 */   
30b3f 31 36 35 2c 20 20 20 35 31 2c 20 20 20 20 31 2c  165,   51,    1,
30b40 20 20 20 20 32 2c 20 20 20 32 36 2c 20 20 20 32      2,   26,   2
30b41 37 2c 20 20 31 37 34 2c 20 20 20 32 32 2c 20 20  7,  174,   22,  
30b42 20 35 38 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20   58,  174,. /*  
30b43 20 31 32 30 20 2a 2f 20 20 20 31 37 35 2c 20 20   120 */   175,  
30b44 20 32 36 2c 20 20 20 32 37 2c 20 20 20 36 38 2c   26,   27,   68,
30b45 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37     69,   70,   7
30b46 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20  1,   72,   73,  
30b47 20 37 34 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a   74,. /*   130 *
30b48 2f 20 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20  /    75,   76,  
30b49 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c   77,   78,   79,
30b4a 20 20 20 38 30 2c 20 20 31 38 36 2c 20 20 20 38     80,  186,   8
30b4b 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 0a 20  2,   83,   84,. 
30b4c 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 20 38  /*   140 */    8
30b4d 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  5,   86,   87,  
30b4e 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c   88,   89,   90,
30b4f 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31     91,   92,   1
30b50 39 2c 20 20 20 39 32 2c 0a 20 2f 2a 20 20 20 31  9,   92,. /*   1
30b51 35 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 31 37  50 */    19,  17
30b52 32 2c 20 20 31 37 33 2c 20 20 20 39 36 2c 20 20  2,  173,   96,  
30b53 20 39 37 2c 20 20 20 39 38 2c 20 20 20 39 39 2c   97,   98,   99,
30b54 20 20 31 30 30 2c 20 20 31 30 31 2c 20 20 31 30    100,  101,  10
30b55 32 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20  2,. /*   160 */ 
30b56 20 20 31 38 31 2c 20 20 32 31 36 2c 20 20 31 34    181,  216,  14
30b57 36 2c 20 20 31 34 37 2c 20 20 32 33 32 2c 20 20  6,  147,  232,  
30b58 31 30 38 2c 20 20 32 32 31 2c 20 20 31 30 37 2c  108,  221,  107,
30b59 20 20 31 35 32 2c 20 20 31 38 36 2c 0a 20 2f 2a    152,  186,. /*
30b5a 20 20 20 31 37 30 20 2a 2f 20 20 20 31 35 34 2c     170 */   154,
30b5b 20 20 31 35 30 2c 20 20 31 39 35 2c 20 20 20 33    150,  195,   3
30b5c 30 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  0,   86,   87,  
30b5d 31 36 30 2c 20 20 20 33 34 2c 20 20 20 34 39 2c  160,   34,   49,
30b5e 20 20 20 35 30 2c 0a 20 2f 2a 20 20 20 31 38 30     50,. /*   180
30b5f 20 2a 2f 20 20 20 20 32 36 2c 20 20 20 35 32 2c   */    26,   52,
30b60 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20 31 33     94,   95,  13
30b61 38 2c 20 20 20 39 37 2c 20 20 31 36 35 2c 20 20  8,   97,  165,  
30b62 31 38 31 2c 20 20 31 38 32 2c 20 20 20 39 34 2c  181,  182,   94,
30b63 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20 20  . /*   190 */   
30b64 20 39 35 2c 20 20 20 34 38 2c 20 20 31 30 34 2c   95,   48,  104,
30b65 20 20 31 30 35 2c 20 20 31 38 38 2c 20 20 31 37    105,  188,  17
30b66 34 2c 20 20 31 37 35 2c 20 20 20 36 38 2c 20 20  4,  175,   68,  
30b67 20 36 39 2c 20 20 20 37 30 2c 0a 20 2f 2a 20 20   69,   70,. /*  
30b68 20 32 30 30 20 2a 2f 20 20 20 20 37 31 2c 20 20   200 */    71,  
30b69 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c   72,   73,   74,
30b6a 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37     75,   76,   7
30b6b 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  7,   78,   79,  
30b6c 20 38 30 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a   80,. /*   210 *
30b6d 2f 20 20 20 31 39 34 2c 20 20 20 38 32 2c 20 20  /   194,   82,  
30b6e 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c   83,   84,   85,
30b6f 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38     86,   87,   8
30b70 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 0a 20  8,   89,   90,. 
30b71 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 20 20 39  /*   220 */    9
30b72 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20  1,   92,   19,  
30b73 20 39 32 2c 20 20 20 38 36 2c 20 20 20 38 37 2c   92,   86,   87,
30b74 20 20 20 32 31 2c 20 20 20 32 34 2c 20 20 20 39     21,   24,   9
30b75 37 2c 20 20 20 39 38 2c 0a 20 2f 2a 20 20 20 32  7,   98,. /*   2
30b76 33 30 20 2a 2f 20 20 20 20 39 39 2c 20 20 31 30  30 */    99,  10
30b77 30 2c 20 20 31 30 31 2c 20 20 31 30 32 2c 20 20  0,  101,  102,  
30b78 32 31 38 2c 20 20 32 31 34 2c 20 20 32 31 35 2c  218,  214,  215,
30b79 20 20 32 30 38 2c 20 20 20 36 36 2c 20 20 31 30    208,   66,  10
30b7a 38 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20  8,. /*   240 */ 
30b7b 20 20 31 35 30 2c 20 20 20 38 36 2c 20 20 20 38    150,   86,   8
30b7c 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20  7,   88,   89,  
30b7d 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c   90,   91,   92,
30b7e 20 20 20 39 34 2c 20 20 31 37 33 2c 0a 20 2f 2a     94,  173,. /*
30b7f 20 20 20 32 35 30 20 2a 2f 20 20 20 31 36 30 2c     250 */   160,
30b80 20 20 31 38 33 2c 20 20 20 34 39 2c 20 20 20 35    183,   49,   5
30b81 30 2c 20 20 31 39 31 2c 20 20 31 36 35 2c 20 20  0,  191,  165,  
30b82 20 39 36 2c 20 20 31 38 31 2c 20 20 20 32 32 2c   96,  181,   22,
30b83 20 20 20 39 39 2c 0a 20 2f 2a 20 20 20 32 36 30     99,. /*   260
30b84 20 2a 2f 20 20 20 31 30 30 2c 20 20 31 30 31 2c   */   100,  101,
30b85 20 20 20 32 36 2c 20 20 32 34 37 2c 20 20 31 37     26,  247,  17
30b86 34 2c 20 20 31 37 35 2c 20 20 20 39 34 2c 20 20  4,  175,   94,  
30b87 20 35 37 2c 20 20 20 36 33 2c 20 20 31 30 39 2c   57,   63,  109,
30b88 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20 20  . /*   270 */   
30b89 20 39 38 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   98,   68,   69,
30b8a 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
30b8b 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20  2,   73,   74,  
30b8c 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f 2a 20 20   75,   76,. /*  
30b8d 20 32 38 30 20 2a 2f 20 20 20 20 37 37 2c 20 20   280 */    77,  
30b8e 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
30b8f 20 20 31 39 34 2c 20 20 20 38 32 2c 20 20 20 38    194,   82,   8
30b90 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20  3,   84,   85,  
30b91 20 38 36 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a   86,. /*   290 *
30b92 2f 20 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20  /    87,   88,  
30b93 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c   89,   90,   91,
30b94 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20 31 35     92,   19,  15
30b95 30 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 0a 20  0,  150,  150,. 
30b96 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 20 20 32  /*   300 */    2
30b97 35 2c 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20  5,  129,  130,  
30b98 31 33 31 2c 20 20 31 38 33 2c 20 20 31 30 30 2c  131,  183,  100,
30b99 20 20 32 31 36 2c 20 20 31 36 30 2c 20 20 31 35    216,  160,  15
30b9a 30 2c 20 20 31 36 31 2c 0a 20 2f 2a 20 20 20 33  0,  161,. /*   3
30b9b 31 30 20 2a 2f 20 20 20 31 36 32 2c 20 20 32 32  10 */   162,  22
30b9c 31 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  1,  165,  165,  
30b9d 31 36 35 2c 20 20 31 30 35 2c 20 20 31 30 36 2c  165,  105,  106,
30b9e 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 31 37     26,   27,  17
30b9f 30 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20  0,. /*   320 */ 
30ba0 20 20 31 37 31 2c 20 20 31 37 34 2c 20 20 31 37    171,  174,  17
30ba1 35 2c 20 20 31 36 35 2c 20 20 31 36 30 2c 20 20  5,  165,  160,  
30ba2 31 31 35 2c 20 20 20 34 39 2c 20 20 20 35 30 2c  115,   49,   50,
30ba3 20 20 31 37 30 2c 20 20 31 37 31 2c 0a 20 2f 2a    170,  171,. /*
30ba4 20 20 20 33 33 30 20 2a 2f 20 20 20 20 39 34 2c     330 */    94,
30ba5 20 20 31 34 38 2c 20 20 31 36 33 2c 20 20 31 38    148,  163,  18
30ba6 35 2c 20 20 31 38 36 2c 20 20 31 36 36 2c 20 20  5,  186,  166,  
30ba7 31 36 37 2c 20 20 31 36 38 2c 20 20 31 32 38 2c  167,  168,  128,
30ba8 20 20 20 39 31 2c 0a 20 2f 2a 20 20 20 33 34 30     91,. /*   340
30ba9 20 2a 2f 20 20 20 20 39 32 2c 20 20 31 39 34 2c   */    92,  194,
30baa 20 20 31 39 36 2c 20 20 31 33 38 2c 20 20 31 36    196,  138,  16
30bab 36 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20  6,   68,   69,  
30bac 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
30bad 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20 20 20  . /*   350 */   
30bae 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c   73,   74,   75,
30baf 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37     76,   77,   7
30bb0 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20  8,   79,   80,  
30bb1 31 39 34 2c 20 20 20 38 32 2c 0a 20 2f 2a 20 20  194,   82,. /*  
30bb2 20 33 36 30 20 2a 2f 20 20 20 20 38 33 2c 20 20   360 */    83,  
30bb3 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c   84,   85,   86,
30bb4 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38     87,   88,   8
30bb5 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20  9,   90,   91,  
30bb6 20 39 32 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a   92,. /*   370 *
30bb7 2f 20 20 20 20 31 39 2c 20 20 20 39 36 2c 20 20  /    19,   96,  
30bb8 20 31 31 2c 20 20 31 35 30 2c 20 20 20 39 39 2c   11,  150,   99,
30bb9 20 20 31 30 30 2c 20 20 31 30 31 2c 20 20 31 35    100,  101,  15
30bba 35 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 0a 20  5,   26,   27,. 
30bbb 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20 20 32 33  /*   380 */   23
30bbc 31 2c 20 20 32 33 32 2c 20 20 32 31 38 2c 20 20  1,  232,  218,  
30bbd 32 30 35 2c 20 20 31 30 39 2c 20 20 20 39 34 2c  205,  109,   94,
30bbe 20 20 20 39 35 2c 20 20 31 35 30 2c 20 20 31 36     95,  150,  16
30bbf 35 2c 20 20 32 33 31 2c 0a 20 2f 2a 20 20 20 33  5,  231,. /*   3
30bc0 39 30 20 2a 2f 20 20 20 32 33 32 2c 20 20 31 36  90 */   232,  16
30bc1 36 2c 20 20 31 35 30 2c 20 20 32 32 32 2c 20 20  6,  150,  222,  
30bc2 31 35 30 2c 20 20 32 32 34 2c 20 20 32 32 35 2c  150,  224,  225,
30bc3 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 32 33    174,  175,  23
30bc4 35 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20  5,. /*   400 */ 
30bc5 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20 31 36     49,   50,  16
30bc6 35 2c 20 20 20 32 34 2c 20 20 32 34 30 2c 20 20  5,   24,  240,  
30bc7 20 32 36 2c 20 20 31 36 36 2c 20 20 31 36 35 2c   26,  166,  165,
30bc8 20 20 31 35 33 2c 20 20 31 36 35 2c 0a 20 2f 2a    153,  165,. /*
30bc9 20 20 20 34 31 30 20 2a 2f 20 20 20 31 31 39 2c     410 */   119,
30bca 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 33    174,  175,  13
30bcb 36 2c 20 20 32 33 37 2c 20 20 32 34 34 2c 20 20  6,  237,  244,  
30bcc 31 37 34 2c 20 20 31 37 35 2c 20 20 20 35 37 2c  174,  175,   57,
30bcd 20 20 20 36 38 2c 0a 20 2f 2a 20 20 20 34 32 30     68,. /*   420
30bce 20 2a 2f 20 20 20 20 36 39 2c 20 20 20 37 30 2c   */    69,   70,
30bcf 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37     71,   72,   7
30bd0 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20  3,   74,   75,  
30bd1 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c   76,   77,   78,
30bd2 0a 20 2f 2a 20 20 20 34 33 30 20 2a 2f 20 20 20  . /*   430 */   
30bd3 20 37 39 2c 20 20 20 38 30 2c 20 20 32 33 36 2c   79,   80,  236,
30bd4 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
30bd5 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20  4,   85,   86,  
30bd6 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20   87,   88,. /*  
30bd7 20 34 34 30 20 2a 2f 20 20 20 20 38 39 2c 20 20   440 */    89,  
30bd8 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c   90,   91,   92,
30bd9 20 20 20 31 39 2c 20 20 32 30 35 2c 20 20 20 39     19,  205,   9
30bda 34 2c 20 20 20 39 35 2c 20 20 20 32 33 2c 20 20  4,   95,   23,  
30bdb 32 32 36 2c 0a 20 2f 2a 20 20 20 34 35 30 20 2a  226,. /*   450 *
30bdc 2f 20 20 20 31 36 35 2c 20 20 32 32 39 2c 20 20  /   165,  229,  
30bdd 32 31 35 2c 20 20 31 35 30 2c 20 20 20 31 32 2c  215,  150,   12,
30bde 20 20 20 38 38 2c 20 20 32 33 34 2c 20 20 31 35     88,  234,  15
30bdf 30 2c 20 20 32 31 36 2c 20 20 31 37 34 2c 0a 20  0,  216,  174,. 
30be0 2f 2a 20 20 20 34 36 30 20 2a 2f 20 20 20 20 33  /*   460 */    3
30be1 32 2c 20 20 32 31 37 2c 20 20 32 32 32 2c 20 20  2,  217,  222,  
30be2 20 32 35 2c 20 20 32 32 34 2c 20 20 32 32 35 2c   25,  224,  225,
30be3 20 20 31 30 35 2c 20 20 31 30 36 2c 20 20 31 36    105,  106,  16
30be4 35 2c 20 20 20 34 31 2c 0a 20 2f 2a 20 20 20 34  5,   41,. /*   4
30be5 37 30 20 2a 2f 20 20 20 20 32 38 2c 20 20 31 31  70 */    28,  11
30be6 39 2c 20 20 31 36 35 2c 20 20 20 39 34 2c 20 20  9,  165,   94,  
30be7 20 34 39 2c 20 20 20 35 30 2c 20 20 31 31 35 2c   49,   50,  115,
30be8 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 31    174,  175,  11
30be9 32 2c 0a 20 2f 2a 20 20 20 34 38 30 20 2a 2f 20  2,. /*   480 */ 
30bea 20 20 20 32 32 2c 20 20 31 37 34 2c 20 20 31 37     22,  174,  17
30beb 35 2c 20 20 31 31 36 2c 20 20 20 32 36 2c 20 20  5,  116,   26,  
30bec 20 32 37 2c 20 20 20 34 34 2c 20 20 31 33 36 2c   27,   44,  136,
30bed 20 20 20 34 36 2c 20 20 31 32 38 2c 0a 20 2f 2a     46,  128,. /*
30bee 20 20 20 34 39 30 20 2a 2f 20 20 20 31 36 36 2c     490 */   166,
30bef 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20 36     26,   27,   6
30bf0 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20  8,   69,   70,  
30bf1 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c   71,   72,   73,
30bf2 20 20 20 37 34 2c 0a 20 2f 2a 20 20 20 35 30 30     74,. /*   500
30bf3 20 2a 2f 20 20 20 20 37 35 2c 20 20 20 37 36 2c   */    75,   76,
30bf4 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37     77,   78,   7
30bf5 39 2c 20 20 20 38 30 2c 20 20 31 35 30 2c 20 20  9,   80,  150,  
30bf6 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
30bf7 0a 20 2f 2a 20 20 20 35 31 30 20 2a 2f 20 20 20  . /*   510 */   
30bf8 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c   85,   86,   87,
30bf9 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39     88,   89,   9
30bfa 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20  0,   91,   92,  
30bfb 20 31 39 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20   19,  150,. /*  
30bfc 20 35 32 30 20 2a 2f 20 20 20 31 35 30 2c 20 20   520 */   150,  
30bfd 31 36 35 2c 20 20 20 32 33 2c 20 20 32 32 30 2c  165,   23,  220,
30bfe 20 20 31 39 36 2c 20 20 31 35 30 2c 20 20 31 35    196,  150,  15
30bff 30 2c 20 20 32 32 30 2c 20 20 31 35 38 2c 20 20  0,  220,  158,  
30c00 32 30 35 2c 0a 20 2f 2a 20 20 20 35 33 30 20 2a  205,. /*   530 *
30c01 2f 20 20 20 31 35 30 2c 20 20 31 36 31 2c 20 20  /   150,  161,  
30c02 31 36 32 2c 20 20 31 39 38 2c 20 20 31 36 35 2c  162,  198,  165,
30c03 20 20 31 36 35 2c 20 20 20 32 36 2c 20 20 20 32    165,   26,   2
30c04 37 2c 20 20 31 31 32 2c 20 20 20 32 33 2c 0a 20  7,  112,   23,. 
30c05 2f 2a 20 20 20 35 34 30 20 2a 2f 20 20 20 31 36  /*   540 */   16
30c06 35 2c 20 20 31 36 35 2c 20 20 31 31 36 2c 20 20  5,  165,  116,  
30c07 31 37 34 2c 20 20 31 37 35 2c 20 20 31 36 35 2c  174,  175,  165,
30c08 20 20 31 35 30 2c 20 20 31 36 36 2c 20 20 20 34    150,  166,   4
30c09 39 2c 20 20 20 35 30 2c 0a 20 2f 2a 20 20 20 35  9,   50,. /*   5
30c0a 35 30 20 2a 2f 20 20 20 31 37 34 2c 20 20 31 37  50 */   174,  17
30c0b 35 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20  5,   94,   95,  
30c0c 31 37 34 2c 20 20 31 37 35 2c 20 20 31 31 38 2c  174,  175,  118,
30c0d 20 20 31 36 31 2c 20 20 31 36 32 2c 20 20 20 39    161,  162,   9
30c0e 34 2c 0a 20 2f 2a 20 20 20 35 36 30 20 2a 2f 20  4,. /*   560 */ 
30c0f 20 20 20 39 35 2c 20 20 31 36 35 2c 20 20 31 38     95,  165,  18
30c10 37 2c 20 20 20 31 36 2c 20 20 20 32 32 2c 20 20  7,   16,   22,  
30c11 31 36 30 2c 20 20 20 32 34 2c 20 20 20 36 38 2c  160,   24,   68,
30c12 20 20 20 36 39 2c 20 20 20 37 30 2c 0a 20 2f 2a     69,   70,. /*
30c13 20 20 20 35 37 30 20 2a 2f 20 20 20 20 37 31 2c     570 */    71,
30c14 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37     72,   73,   7
30c15 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20  4,   75,   76,  
30c16 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c   77,   78,   79,
30c17 20 20 20 38 30 2c 0a 20 2f 2a 20 20 20 35 38 30     80,. /*   580
30c18 20 2a 2f 20 20 20 31 35 30 2c 20 20 20 38 32 2c   */   150,   82,
30c19 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38     83,   84,   8
30c1a 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  5,   86,   87,  
30c1b 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c   88,   89,   90,
30c1c 0a 20 2f 2a 20 20 20 35 39 30 20 2a 2f 20 20 20  . /*   590 */   
30c1d 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c   91,   92,   19,
30c1e 20 20 31 35 30 2c 20 20 20 31 32 2c 20 20 31 36    150,   12,  16
30c1f 35 2c 20 20 20 32 33 2c 20 20 32 34 31 2c 20 20  5,   23,  241,  
30c20 20 38 38 2c 20 20 31 39 34 2c 0a 20 2f 2a 20 20   88,  194,. /*  
30c21 20 36 30 30 20 2a 2f 20 20 20 31 35 30 2c 20 20   600 */   150,  
30c22 20 20 30 2c 20 20 20 20 31 2c 20 20 20 20 32 2c    0,    1,    2,
30c23 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20 31 36     94,   95,  16
30c24 30 2c 20 20 20 36 30 2c 20 20 31 36 35 2c 20 20  0,   60,  165,  
30c25 20 36 32 2c 0a 20 2f 2a 20 20 20 36 31 30 20 2a   62,. /*   610 *
30c26 2f 20 20 20 20 32 38 2c 20 20 32 30 36 2c 20 20  /    28,  206,  
30c27 32 30 37 2c 20 20 31 39 30 2c 20 20 31 39 31 2c  207,  190,  191,
30c28 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31 37    165,  150,  17
30c29 34 2c 20 20 31 37 35 2c 20 20 20 20 37 2c 0a 20  4,  175,    7,. 
30c2a 2f 2a 20 20 20 36 32 30 20 2a 2f 20 20 20 20 20  /*   620 */     
30c2b 38 2c 20 20 20 20 39 2c 20 20 20 34 39 2c 20 20  8,    9,   49,  
30c2c 20 35 30 2c 20 20 31 37 34 2c 20 20 31 37 35 2c   50,  174,  175,
30c2d 20 20 20 34 34 2c 20 20 31 31 31 2c 20 20 20 34     44,  111,   4
30c2e 36 2c 20 20 31 31 33 2c 0a 20 2f 2a 20 20 20 36  6,  113,. /*   6
30c2f 33 30 20 2a 2f 20 20 20 31 31 34 2c 20 20 31 36  30 */   114,  16
30c30 35 2c 20 20 31 36 39 2c 20 20 31 37 30 2c 20 20  5,  169,  170,  
30c31 31 37 31 2c 20 20 20 32 32 2c 20 20 20 32 33 2c  171,   22,   23,
30c32 20 20 32 33 33 2c 20 20 20 32 35 2c 20 20 20 35    233,   25,   5
30c33 37 2c 0a 20 2f 2a 20 20 20 36 34 30 20 2a 2f 20  7,. /*   640 */ 
30c34 20 20 31 39 34 2c 20 20 20 36 38 2c 20 20 20 36    194,   68,   6
30c35 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20  9,   70,   71,  
30c36 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c   72,   73,   74,
30c37 20 20 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f 2a     75,   76,. /*
30c38 20 20 20 36 35 30 20 2a 2f 20 20 20 20 37 37 2c     650 */    77,
30c39 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38     78,   79,   8
30c3a 30 2c 20 20 31 35 30 2c 20 20 20 38 32 2c 20 20  0,  150,   82,  
30c3b 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c   83,   84,   85,
30c3c 20 20 20 38 36 2c 0a 20 2f 2a 20 20 20 36 36 30     86,. /*   660
30c3d 20 2a 2f 20 20 20 20 38 37 2c 20 20 20 38 38 2c   */    87,   88,
30c3e 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39     89,   90,   9
30c3f 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20  1,   92,   19,  
30c40 31 35 30 2c 20 20 20 32 35 2c 20 20 31 36 35 2c  150,   25,  165,
30c41 0a 20 2f 2a 20 20 20 36 37 30 20 2a 2f 20 20 20  . /*   670 */   
30c42 20 32 33 2c 20 20 31 35 30 2c 20 20 32 33 33 2c   23,  150,  233,
30c43 20 20 20 31 39 2c 20 20 31 35 30 2c 20 20 31 39     19,  150,  19
30c44 30 2c 20 20 31 39 31 2c 20 20 32 32 38 2c 20 20  0,  191,  228,  
30c45 31 37 34 2c 20 20 31 37 35 2c 0a 20 2f 2a 20 20  174,  175,. /*  
30c46 20 36 38 30 20 2a 2f 20 20 20 20 36 37 2c 20 20   680 */    67,  
30c47 32 32 32 2c 20 20 31 36 35 2c 20 20 32 32 34 2c  222,  165,  224,
30c48 20 20 32 32 35 2c 20 20 31 35 30 2c 20 20 31 36    225,  150,  16
30c49 35 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20  5,  150,  150,  
30c4a 31 36 35 2c 0a 20 2f 2a 20 20 20 36 39 30 20 2a  165,. /*   690 *
30c4b 2f 20 20 20 20 32 33 2c 20 20 31 37 34 2c 20 20  /    23,  174,  
30c4c 31 37 35 2c 20 20 31 34 34 2c 20 20 31 34 35 2c  175,  144,  145,
30c4d 20 20 32 33 32 2c 20 20 20 34 39 2c 20 20 20 35    232,   49,   5
30c4e 30 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 0a 20  0,  174,  175,. 
30c4f 2f 2a 20 20 20 37 30 30 20 2a 2f 20 20 20 31 36  /*   700 */   16
30c50 35 2c 20 20 31 31 35 2c 20 20 31 36 35 2c 20 20  5,  115,  165,  
30c51 31 36 35 2c 20 20 20 35 30 2c 20 20 20 32 32 2c  165,   50,   22,
30c52 20 20 20 32 33 2c 20 20 32 34 31 2c 20 20 20 32     23,  241,   2
30c53 35 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20 20 37  5,  174,. /*   7
30c54 31 30 20 2a 2f 20 20 20 31 37 35 2c 20 20 31 37  10 */   175,  17
30c55 34 2c 20 20 31 37 35 2c 20 20 31 32 37 2c 20 20  4,  175,  127,  
30c56 31 39 33 2c 20 20 20 36 38 2c 20 20 20 36 39 2c  193,   68,   69,
30c57 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
30c58 32 2c 0a 20 2f 2a 20 20 20 37 32 30 20 2a 2f 20  2,. /*   720 */ 
30c59 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
30c5a 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20  5,   76,   77,  
30c5b 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
30c5c 20 20 31 35 30 2c 20 20 20 38 32 2c 0a 20 2f 2a    150,   82,. /*
30c5d 20 20 20 37 33 30 20 2a 2f 20 20 20 20 38 33 2c     730 */    83,
30c5e 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
30c5f 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20  6,   87,   88,  
30c60 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c   89,   90,   91,
30c61 20 20 20 39 32 2c 0a 20 2f 2a 20 20 20 37 34 30     92,. /*   740
30c62 20 2a 2f 20 20 20 20 31 39 2c 20 20 31 35 30 2c   */    19,  150,
30c63 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31 35    150,  165,  15
30c64 30 2c 20 20 20 32 34 2c 20 20 31 30 33 2c 20 20  0,   24,  103,  
30c65 20 32 33 2c 20 20 31 35 30 2c 20 20 31 35 30 2c   23,  150,  150,
30c66 0a 20 2f 2a 20 20 20 37 35 30 20 2a 2f 20 20 20  . /*   750 */   
30c67 20 36 37 2c 20 20 32 31 33 2c 20 20 31 37 34 2c   67,  213,  174,
30c68 20 20 31 37 35 2c 20 20 20 39 37 2c 20 20 32 30    175,   97,  20
30c69 39 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  9,  165,  165,  
30c6a 31 30 34 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20  104,  165,. /*  
30c6b 20 37 36 30 20 2a 2f 20 20 20 31 35 30 2c 20 20   760 */   150,  
30c6c 31 35 30 2c 20 20 31 30 38 2c 20 20 31 36 35 2c  150,  108,  165,
30c6d 20 20 31 36 35 2c 20 20 31 37 34 2c 20 20 31 37    165,  174,  17
30c6e 35 2c 20 20 20 32 33 2c 20 20 31 37 34 2c 20 20  5,   23,  174,  
30c6f 31 37 35 2c 0a 20 2f 2a 20 20 20 37 37 30 20 2a  175,. /*   770 *
30c70 2f 20 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20  /    49,   50,  
30c71 31 31 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c  115,  174,  175,
30c72 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 37    165,  165,  17
30c73 37 2c 20 20 31 31 31 2c 20 20 31 38 37 2c 0a 20  7,  111,  187,. 
30c74 2f 2a 20 20 20 37 38 30 20 2a 2f 20 20 20 31 31  /*   780 */   11
30c75 33 2c 20 20 31 31 34 2c 20 20 32 35 30 2c 20 20  3,  114,  250,  
30c76 32 35 31 2c 20 20 31 32 37 2c 20 20 31 37 34 2c  251,  127,  174,
30c77 20 20 31 37 35 2c 20 20 32 30 36 2c 20 20 32 30    175,  206,  20
30c78 37 2c 20 20 20 36 38 2c 0a 20 2f 2a 20 20 20 37  7,   68,. /*   7
30c79 39 30 20 2a 2f 20 20 20 20 36 39 2c 20 20 20 37  90 */    69,   7
30c7a 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20  0,   71,   72,  
30c7b 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c   73,   74,   75,
30c7c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37     76,   77,   7
30c7d 38 2c 0a 20 2f 2a 20 20 20 38 30 30 20 2a 2f 20  8,. /*   800 */ 
30c7e 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 31 35     79,   80,  15
30c7f 30 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20  0,   82,   83,  
30c80 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c   84,   85,   86,
30c81 20 20 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a     87,   88,. /*
30c82 20 20 20 38 31 30 20 2a 2f 20 20 20 20 38 39 2c     810 */    89,
30c83 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39     90,   91,   9
30c84 32 2c 20 20 20 31 39 2c 20 20 31 35 30 2c 20 20  2,   19,  150,  
30c85 31 35 30 2c 20 20 31 36 35 2c 20 20 31 35 30 2c  150,  165,  150,
30c86 20 20 31 31 31 2c 0a 20 2f 2a 20 20 20 38 32 30    111,. /*   820
30c87 20 2a 2f 20 20 20 31 38 32 2c 20 20 31 31 33 2c   */   182,  113,
30c88 20 20 31 31 34 2c 20 20 31 30 35 2c 20 20 31 30    114,  105,  10
30c89 36 2c 20 20 31 37 37 2c 20 20 31 37 34 2c 20 20  6,  177,  174,  
30c8a 31 37 35 2c 20 20 20 32 35 2c 20 20 31 36 36 2c  175,   25,  166,
30c8b 0a 20 2f 2a 20 20 20 38 33 30 20 2a 2f 20 20 20  . /*   830 */   
30c8c 31 36 35 2c 20 20 31 36 35 2c 20 20 31 35 30 2c  165,  165,  150,
30c8d 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31 31    165,  150,  11
30c8e 31 2c 20 20 31 35 30 2c 20 20 31 31 33 2c 20 20  1,  150,  113,  
30c8f 31 31 34 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20  114,  174,. /*  
30c90 20 38 34 30 20 2a 2f 20 20 20 31 37 35 2c 20 20   840 */   175,  
30c91 31 35 30 2c 20 20 31 37 34 2c 20 20 31 37 35 2c  150,  174,  175,
30c92 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20 31 32     49,   50,  12
30c93 38 2c 20 20 31 36 35 2c 20 20 31 33 35 2c 20 20  8,  165,  135,  
30c94 31 36 35 2c 0a 20 2f 2a 20 20 20 38 35 30 20 2a  165,. /*   850 *
30c95 2f 20 20 20 31 33 37 2c 20 20 31 36 35 2c 20 20  /   137,  165,  
30c96 31 39 37 2c 20 20 31 38 37 2c 20 20 31 36 36 2c  197,  187,  166,
30c97 20 20 31 31 31 2c 20 20 31 36 35 2c 20 20 31 31    111,  165,  11
30c98 33 2c 20 20 31 31 34 2c 20 20 32 30 34 2c 0a 20  3,  114,  204,. 
30c99 2f 2a 20 20 20 38 36 30 20 2a 2f 20 20 20 31 37  /*   860 */   17
30c9a 34 2c 20 20 31 37 35 2c 20 20 31 37 37 2c 20 20  4,  175,  177,  
30c9b 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c   68,   69,   70,
30c9c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37     71,   72,   7
30c9d 33 2c 20 20 20 37 34 2c 0a 20 2f 2a 20 20 20 38  3,   74,. /*   8
30c9e 37 30 20 2a 2f 20 20 20 20 37 35 2c 20 20 20 37  70 */    75,   7
30c9f 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20  6,   77,   78,  
30ca0 20 37 39 2c 20 20 20 38 30 2c 20 20 31 35 30 2c   79,   80,  150,
30ca1 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
30ca2 34 2c 0a 20 2f 2a 20 20 20 38 38 30 20 2a 2f 20  4,. /*   880 */ 
30ca3 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38     85,   86,   8
30ca4 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20  7,   88,   89,  
30ca5 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c   90,   91,   92,
30ca6 20 20 20 31 39 2c 20 20 31 35 30 2c 0a 20 2f 2a     19,  150,. /*
30ca7 20 20 20 38 39 30 20 2a 2f 20 20 20 31 35 30 2c     890 */   150,
30ca8 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 32 30    165,  150,  20
30ca9 35 2c 20 20 31 37 37 2c 20 20 32 31 33 2c 20 20  5,  177,  213,  
30caa 31 35 30 2c 20 20 32 31 33 2c 20 20 20 39 35 2c  150,  213,   95,
30cab 20 20 31 37 37 2c 0a 20 2f 2a 20 20 20 39 30 30    177,. /*   900
30cac 20 2a 2f 20 20 20 31 37 34 2c 20 20 31 37 35 2c   */   174,  175,
30cad 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20 31 36    129,  130,  16
30cae 35 2c 20 20 31 36 35 2c 20 20 20 32 33 2c 20 20  5,  165,   23,  
30caf 31 36 35 2c 20 20 20 32 35 2c 20 20 31 35 30 2c  165,   25,  150,
30cb0 0a 20 2f 2a 20 20 20 39 31 30 20 2a 2f 20 20 20  . /*   910 */   
30cb1 31 35 30 2c 20 20 31 36 35 2c 20 20 31 37 38 2c  150,  165,  178,
30cb2 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 35    174,  175,  15
30cb3 30 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  0,  174,  175,  
30cb4 20 34 39 2c 20 20 20 35 30 2c 0a 20 2f 2a 20 20   49,   50,. /*  
30cb5 20 39 32 30 20 2a 2f 20 20 20 31 37 34 2c 20 20   920 */   174,  
30cb6 31 37 35 2c 20 20 31 31 39 2c 20 20 20 31 39 2c  175,  119,   19,
30cb7 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 20 35    165,  165,   5
30cb8 30 2c 20 20 20 32 32 2c 20 20 20 32 33 2c 20 20  0,   22,   23,  
30cb9 31 36 30 2c 0a 20 2f 2a 20 20 20 39 33 30 20 2a  160,. /*   930 *
30cba 2f 20 20 20 31 36 35 2c 20 20 20 32 37 2c 20 20  /   165,   27,  
30cbb 32 34 31 2c 20 20 31 39 33 2c 20 20 31 37 34 2c  241,  193,  174,
30cbc 20 20 31 37 35 2c 20 20 31 36 30 2c 20 20 20 36    175,  160,   6
30cbd 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 0a 20  8,   69,   70,. 
30cbe 2f 2a 20 20 20 39 34 30 20 2a 2f 20 20 20 20 37  /*   940 */    7
30cbf 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20  1,   72,   73,  
30cc0 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c   74,   75,   76,
30cc1 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37     77,   78,   7
30cc2 39 2c 20 20 20 38 30 2c 0a 20 2f 2a 20 20 20 39  9,   80,. /*   9
30cc3 35 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 20 38  50 */   150,   8
30cc4 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20  2,   83,   84,  
30cc5 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c   85,   86,   87,
30cc6 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39     88,   89,   9
30cc7 30 2c 0a 20 2f 2a 20 20 20 39 36 30 20 2a 2f 20  0,. /*   960 */ 
30cc8 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31     91,   92,   1
30cc9 39 2c 20 20 31 39 34 2c 20 20 31 35 30 2c 20 20  9,  194,  150,  
30cca 31 36 35 2c 20 20 31 35 30 2c 20 20 31 35 30 2c  165,  150,  150,
30ccb 20 20 31 35 30 2c 20 20 31 36 30 2c 0a 20 2f 2a    150,  160,. /*
30ccc 20 20 20 39 37 30 20 2a 2f 20 20 20 31 39 34 2c     970 */   194,
30ccd 20 20 20 32 35 2c 20 20 20 36 37 2c 20 20 31 36     25,   67,  16
30cce 30 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  0,  174,  175,  
30ccf 32 31 37 2c 20 20 31 36 36 2c 20 20 20 39 35 2c  217,  166,   95,
30cd0 20 20 31 36 35 2c 0a 20 2f 2a 20 20 20 39 38 30    165,. /*   980
30cd1 20 2a 2f 20 20 20 31 30 34 2c 20 20 31 36 35 2c   */   104,  165,
30cd2 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 35    165,  165,  15
30cd3 30 2c 20 20 32 34 35 2c 20 20 32 34 38 2c 20 20  0,  245,  248,  
30cd4 32 34 39 2c 20 20 20 34 39 2c 20 20 20 35 30 2c  249,   49,   50,
30cd5 0a 20 2f 2a 20 20 20 39 39 30 20 2a 2f 20 20 20  . /*   990 */   
30cd6 31 37 34 2c 20 20 31 37 35 2c 20 20 20 34 39 2c  174,  175,   49,
30cd7 20 20 20 35 30 2c 20 20 20 32 33 2c 20 20 20 32     50,   23,   2
30cd8 33 2c 20 20 20 32 35 2c 20 20 20 32 35 2c 20 20  3,   25,   25,  
30cd9 32 34 32 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20  242,  165,. /*  
30cda 31 30 30 30 20 2a 2f 20 20 20 31 39 39 2c 20 20  1000 */   199,  
30cdb 31 38 37 2c 20 20 31 31 39 2c 20 20 31 39 34 2c  187,  119,  194,
30cdc 20 20 31 38 37 2c 20 20 20 32 32 2c 20 20 20 32    187,   22,   2
30cdd 33 2c 20 20 31 39 34 2c 20 20 31 37 34 2c 20 20  3,  194,  174,  
30cde 31 37 35 2c 0a 20 2f 2a 20 20 31 30 31 30 20 2a  175,. /*  1010 *
30cdf 2f 20 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20  /    71,   72,  
30ce0 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
30ce1 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37     72,   73,   7
30ce2 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 0a 20  4,   75,   76,. 
30ce3 2f 2a 20 20 31 30 32 30 20 2a 2f 20 20 20 20 37  /*  1020 */    7
30ce4 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  7,   78,   79,  
30ce5 20 38 30 2c 20 20 31 35 30 2c 20 20 20 38 32 2c   80,  150,   82,
30ce6 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38     83,   84,   8
30ce7 35 2c 20 20 20 38 36 2c 0a 20 2f 2a 20 20 31 30  5,   86,. /*  10
30ce8 33 30 20 2a 2f 20 20 20 20 38 37 2c 20 20 20 38  30 */    87,   8
30ce9 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20  8,   89,   90,  
30cea 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c   91,   92,   19,
30ceb 20 20 20 39 38 2c 20 20 31 35 30 2c 20 20 31 36     98,  150,  16
30cec 35 2c 0a 20 2f 2a 20 20 31 30 34 30 20 2a 2f 20  5,. /*  1040 */ 
30ced 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 35    150,  150,  15
30cee 30 2c 20 20 20 31 39 2c 20 20 20 20 37 2c 20 20  0,   19,    7,  
30cef 20 20 38 2c 20 20 31 30 35 2c 20 20 31 30 36 2c    8,  105,  106,
30cf0 20 20 31 37 34 2c 20 20 31 37 35 2c 0a 20 2f 2a    174,  175,. /*
30cf1 20 20 31 30 35 30 20 2a 2f 20 20 20 20 36 37 2c    1050 */    67,
30cf2 20 20 20 32 37 2c 20 20 20 32 33 2c 20 20 31 36     27,   23,  16
30cf3 35 2c 20 20 20 32 35 2c 20 20 31 36 35 2c 20 20  5,   25,  165,  
30cf4 31 36 35 2c 20 20 31 36 35 2c 20 20 31 35 30 2c  165,  165,  150,
30cf5 20 20 31 35 30 2c 0a 20 2f 2a 20 20 31 30 36 30    150,. /*  1060
30cf6 20 2a 2f 20 20 20 31 35 30 2c 20 20 31 35 30 2c   */   150,  150,
30cf7 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 37    174,  175,  17
30cf8 34 2c 20 20 31 37 35 2c 20 20 20 34 39 2c 20 20  4,  175,   49,  
30cf9 20 35 30 2c 20 20 20 39 38 2c 20 20 32 34 32 2c   50,   98,  242,
30cfa 0a 20 2f 2a 20 20 31 30 37 30 20 2a 2f 20 20 20  . /*  1070 */   
30cfb 20 32 33 2c 20 20 31 32 35 2c 20 20 20 32 35 2c   23,  125,   25,
30cfc 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36    165,  165,  16
30cfd 35 2c 20 20 31 36 35 2c 20 20 32 30 39 2c 20 20  5,  165,  209,  
30cfe 31 30 38 2c 20 20 20 39 37 2c 0a 20 2f 2a 20 20  108,   97,. /*  
30cff 31 30 38 30 20 2a 2f 20 20 20 20 39 38 2c 20 20  1080 */    98,  
30d00 32 30 39 2c 20 20 31 37 34 2c 20 20 31 37 35 2c  209,  174,  175,
30d01 20 20 31 39 33 2c 20 20 31 37 34 2c 20 20 31 37    193,  174,  17
30d02 35 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20  5,   70,   71,  
30d03 20 37 32 2c 0a 20 2f 2a 20 20 31 30 39 30 20 2a   72,. /*  1090 *
30d04 2f 20 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20  /    73,   74,  
30d05 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c   75,   76,   77,
30d06 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38     78,   79,   8
30d07 30 2c 20 20 31 39 39 2c 20 20 20 38 32 2c 0a 20  0,  199,   82,. 
30d08 2f 2a 20 20 31 31 30 30 20 2a 2f 20 20 20 20 38  /*  1100 */    8
30d09 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20  3,   84,   85,  
30d0a 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c   86,   87,   88,
30d0b 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39     89,   90,   9
30d0c 31 2c 20 20 20 39 32 2c 0a 20 2f 2a 20 20 31 31  1,   92,. /*  11
30d0d 31 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 20 32  10 */    19,   2
30d0e 30 2c 20 20 31 35 30 2c 20 20 20 32 32 2c 20 20  0,  150,   22,  
30d0f 31 36 30 2c 20 20 20 32 32 2c 20 20 31 34 39 2c  160,   22,  149,
30d10 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 31 35     26,   27,  15
30d11 30 2c 0a 20 2f 2a 20 20 31 31 32 30 20 2a 2f 20  0,. /*  1120 */ 
30d12 20 20 20 32 33 2c 20 20 20 20 36 2c 20 20 20 32     23,    6,   2
30d13 35 2c 20 20 20 31 39 2c 20 20 20 32 30 2c 20 20  5,   19,   20,  
30d14 31 35 30 2c 20 20 20 32 32 2c 20 20 31 36 35 2c  150,   22,  165,
30d15 20 20 20 33 37 2c 20 20 31 34 39 2c 0a 20 2f 2a     37,  149,. /*
30d16 20 20 31 31 33 30 20 2a 2f 20 20 20 20 32 36 2c    1130 */    26,
30d17 20 20 20 32 37 2c 20 20 31 35 31 2c 20 20 20 32     27,  151,   2
30d18 33 2c 20 20 31 36 35 2c 20 20 20 32 35 2c 20 20  3,  165,   25,  
30d19 31 37 34 2c 20 20 31 37 35 2c 20 20 31 35 31 2c  174,  175,  151,
30d1a 20 20 31 35 30 2c 0a 20 2f 2a 20 20 31 31 34 30    150,. /*  1140
30d1b 20 2a 2f 20 20 20 31 36 35 2c 20 20 20 33 37 2c   */   165,   37,
30d1c 20 20 20 31 33 2c 20 20 31 35 30 2c 20 20 31 35     13,  150,  15
30d1d 30 2c 20 20 31 34 39 2c 20 20 31 34 39 2c 20 20  0,  149,  149,  
30d1e 20 35 36 2c 20 20 31 39 34 2c 20 20 31 37 34 2c   56,  194,  174,
30d1f 0a 20 2f 2a 20 20 31 31 35 30 20 2a 2f 20 20 20  . /*  1150 */   
30d20 31 37 35 2c 20 20 20 32 33 2c 20 20 20 32 35 2c  175,   23,   25,
30d21 20 20 20 32 35 2c 20 20 31 36 35 2c 20 20 31 39     25,  165,  19
30d22 34 2c 20 20 31 35 30 2c 20 20 20 36 36 2c 20 20  4,  150,   66,  
30d23 31 36 35 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20  165,  165,. /*  
30d24 31 31 36 30 20 2a 2f 20 20 20 20 35 36 2c 20 20  1160 */    56,  
30d25 31 35 30 2c 20 20 31 35 39 2c 20 20 31 37 34 2c  150,  159,  174,
30d26 20 20 31 37 35 2c 20 20 31 32 35 2c 20 20 31 35    175,  125,  15
30d27 30 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  0,  174,  175,  
30d28 20 37 36 2c 0a 20 2f 2a 20 20 31 31 37 30 20 2a   76,. /*  1170 *
30d29 2f 20 20 20 20 36 36 2c 20 20 31 36 35 2c 20 20  /    66,  165,  
30d2a 32 31 38 2c 20 20 31 35 30 2c 20 20 31 32 32 2c  218,  150,  122,
30d2b 20 20 31 39 39 2c 20 20 31 36 35 2c 20 20 20 38    199,  165,   8
30d2c 36 2c 20 20 20 38 37 2c 20 20 32 30 30 2c 0a 20  6,   87,  200,. 
30d2d 2f 2a 20 20 31 31 38 30 20 2a 2f 20 20 20 31 35  /*  1180 */   15
30d2e 30 2c 20 20 31 36 35 2c 20 20 31 32 33 2c 20 20  0,  165,  123,  
30d2f 31 32 31 2c 20 20 20 39 33 2c 20 20 20 39 34 2c  121,   93,   94,
30d30 20 20 20 39 35 2c 20 20 31 39 33 2c 20 20 31 36     95,  193,  16
30d31 35 2c 20 20 20 39 38 2c 0a 20 2f 2a 20 20 31 31  5,   98,. /*  11
30d32 39 30 20 2a 2f 20 20 20 20 38 36 2c 20 20 20 38  90 */    86,   8
30d33 37 2c 20 20 20 38 38 2c 20 20 32 30 31 2c 20 20  7,   88,  201,  
30d34 32 34 30 2c 20 20 31 36 35 2c 20 20 31 32 34 2c  240,  165,  124,
30d35 20 20 20 39 33 2c 20 20 20 39 34 2c 20 20 20 39     93,   94,   9
30d36 35 2c 0a 20 2f 2a 20 20 31 32 30 30 20 2a 2f 20  5,. /*  1200 */ 
30d37 20 20 32 30 32 2c 20 20 31 33 35 2c 20 20 20 39    202,  135,   9
30d38 38 2c 20 20 20 20 35 2c 20 20 31 37 34 2c 20 20  8,    5,  174,  
30d39 31 37 35 2c 20 20 31 31 35 2c 20 20 32 30 33 2c  175,  115,  203,
30d3a 20 20 20 31 30 2c 20 20 20 31 31 2c 0a 20 2f 2a     10,   11,. /*
30d3b 20 20 31 32 31 30 20 2a 2f 20 20 20 20 31 32 2c    1210 */    12,
30d3c 20 20 20 31 33 2c 20 20 20 31 34 2c 20 20 31 35     13,   14,  15
30d3d 30 2c 20 20 31 35 37 2c 20 20 20 31 37 2c 20 20  0,  157,   17,  
30d3e 31 39 33 2c 20 20 31 35 30 2c 20 20 32 32 37 2c  193,  150,  227,
30d3f 20 20 32 31 30 2c 0a 20 2f 2a 20 20 31 32 32 30    210,. /*  1220
30d40 20 2a 2f 20 20 20 31 32 39 2c 20 20 31 33 30 2c   */   129,  130,
30d41 20 20 31 33 31 2c 20 20 31 33 32 2c 20 20 31 33    131,  132,  13
30d42 33 2c 20 20 31 33 34 2c 20 20 31 35 30 2c 20 20  3,  134,  150,  
30d43 31 35 37 2c 20 20 31 36 35 2c 20 20 20 33 31 2c  157,  165,   31,
30d44 0a 20 2f 2a 20 20 31 32 33 30 20 2a 2f 20 20 20  . /*  1230 */   
30d45 31 31 37 2c 20 20 20 33 33 2c 20 20 31 36 35 2c  117,   33,  165,
30d46 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20 31 33    129,  130,  13
30d47 31 2c 20 20 31 33 32 2c 20 20 31 33 33 2c 20 20  1,  132,  133,  
30d48 31 33 34 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20  134,  150,. /*  
30d49 31 32 34 30 20 2a 2f 20 20 20 20 34 32 2c 20 20  1240 */    42,  
30d4a 31 36 35 2c 20 20 20 31 39 2c 20 20 20 32 30 2c  165,   19,   20,
30d4b 20 20 31 30 34 2c 20 20 20 32 32 2c 20 20 31 35    104,   22,  15
30d4c 30 2c 20 20 32 31 31 2c 20 20 32 31 30 2c 20 20  0,  211,  210,  
30d4d 20 32 36 2c 0a 20 2f 2a 20 20 31 32 35 30 20 2a   26,. /*  1250 *
30d4e 2f 20 20 20 20 32 37 2c 20 20 32 31 30 2c 20 20  /    27,  210,  
30d4f 31 35 30 2c 20 20 20 35 35 2c 20 20 31 36 35 2c  150,   55,  165,
30d50 20 20 20 35 37 2c 20 20 31 39 33 2c 20 20 31 32     57,  193,  12
30d51 30 2c 20 20 31 30 34 2c 20 20 20 36 31 2c 0a 20  0,  104,   61,. 
30d52 2f 2a 20 20 31 32 36 30 20 2a 2f 20 20 20 20 33  /*  1260 */    3
30d53 37 2c 20 20 31 36 35 2c 20 20 20 36 34 2c 20 20  7,  165,   64,  
30d54 31 37 34 2c 20 20 31 37 35 2c 20 20 20 31 39 2c  174,  175,   19,
30d55 20 20 20 32 30 2c 20 20 31 36 35 2c 20 20 20 32     20,  165,   2
30d56 32 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 31 32  2,  150,. /*  12
30d57 37 30 20 2a 2f 20 20 20 31 37 34 2c 20 20 31 37  70 */   174,  17
30d58 35 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  5,   26,   27,  
30d59 31 37 36 2c 20 20 31 35 30 2c 20 20 31 37 34 2c  176,  150,  174,
30d5a 20 20 31 37 35 2c 20 20 20 34 37 2c 20 20 20 35    175,   47,   5
30d5b 36 2c 0a 20 2f 2a 20 20 31 32 38 30 20 2a 2f 20  6,. /*  1280 */ 
30d5c 20 20 32 31 31 2c 20 20 31 35 30 2c 20 20 31 35    211,  150,  15
30d5d 30 2c 20 20 20 33 37 2c 20 20 31 36 35 2c 20 20  0,   37,  165,  
30d5e 20 32 33 2c 20 20 20 32 33 2c 20 20 20 32 35 2c   23,   23,   25,
30d5f 20 20 20 32 35 2c 20 20 20 36 36 2c 0a 20 2f 2a     25,   66,. /*
30d60 20 20 31 32 39 30 20 2a 2f 20 20 20 31 36 35 2c    1290 */   165,
30d61 20 20 32 31 31 2c 20 20 31 35 30 2c 20 20 31 37    211,  150,  17
30d62 34 2c 20 20 31 37 35 2c 20 20 31 38 34 2c 20 20  4,  175,  184,  
30d63 31 36 35 2c 20 20 31 36 35 2c 20 20 31 37 36 2c  165,  165,  176,
30d64 20 20 31 37 34 2c 0a 20 2f 2a 20 20 31 33 30 30    174,. /*  1300
30d65 20 2a 2f 20 20 20 31 37 35 2c 20 20 31 37 38 2c   */   175,  178,
30d66 20 20 20 35 36 2c 20 20 31 30 35 2c 20 20 31 30     56,  105,  10
30d67 36 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  6,  174,  175,  
30d68 31 36 35 2c 20 20 31 31 30 2c 20 20 20 38 36 2c  165,  110,   86,
30d69 0a 20 2f 2a 20 20 31 33 31 30 20 2a 2f 20 20 20  . /*  1310 */   
30d6a 20 38 37 2c 20 20 31 37 36 2c 20 20 20 36 36 2c   87,  176,   66,
30d6b 20 20 31 31 35 2c 20 20 31 30 33 2c 20 20 31 37    115,  103,  17
30d6c 36 2c 20 20 20 39 33 2c 20 20 20 39 34 2c 20 20  6,   93,   94,  
30d6d 20 39 35 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20   95,  150,. /*  
30d6e 31 33 32 30 20 2a 2f 20 20 20 20 32 33 2c 20 20  1320 */    23,  
30d6f 20 39 38 2c 20 20 20 32 35 2c 20 20 31 35 30 2c   98,   25,  150,
30d70 20 20 20 32 32 2c 20 20 20 32 32 2c 20 20 31 32     22,   22,  12
30d71 38 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20  8,  150,  150,  
30d72 20 32 36 2c 0a 20 2f 2a 20 20 31 33 33 30 20 2a   26,. /*  1330 *
30d73 2f 20 20 20 20 32 37 2c 20 20 31 35 30 2c 20 20  /    27,  150,  
30d74 20 38 36 2c 20 20 20 38 37 2c 20 20 31 36 35 2c   86,   87,  165,
30d75 20 20 31 39 33 2c 20 20 31 35 30 2c 20 20 31 33    193,  150,  13
30d76 39 2c 20 20 31 36 35 2c 20 20 20 39 33 2c 0a 20  9,  165,   93,. 
30d77 2f 2a 20 20 31 33 34 30 20 2a 2f 20 20 20 20 39  /*  1340 */    9
30d78 34 2c 20 20 20 39 35 2c 20 20 31 36 35 2c 20 20  4,   95,  165,  
30d79 31 36 35 2c 20 20 20 39 38 2c 20 20 31 35 30 2c  165,   98,  150,
30d7a 20 20 31 36 35 2c 20 20 31 37 39 2c 20 20 20 39    165,  179,   9
30d7b 32 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 31 33  2,  150,. /*  13
30d7c 35 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31 36  50 */   150,  16
30d7d 35 2c 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20  5,  129,  130,  
30d7e 31 33 31 2c 20 20 31 33 32 2c 20 20 31 33 33 2c  131,  132,  133,
30d7f 20 20 31 33 34 2c 20 20 31 38 34 2c 20 20 31 35    134,  184,  15
30d80 30 2c 0a 20 2f 2a 20 20 31 33 36 30 20 2a 2f 20  0,. /*  1360 */ 
30d81 20 20 31 36 35 2c 20 20 31 37 36 2c 20 20 31 39    165,  176,  19
30d82 33 2c 20 20 32 33 30 2c 20 20 31 36 35 2c 20 20  3,  230,  165,  
30d83 31 36 35 2c 20 20 31 39 33 2c 20 20 31 35 30 2c  165,  193,  150,
30d84 20 20 31 35 30 2c 20 20 20 36 36 2c 0a 20 2f 2a    150,   66,. /*
30d85 20 20 31 33 37 30 20 2a 2f 20 20 20 31 37 36 2c    1370 */   176,
30d86 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 32 33    150,  150,  23
30d87 30 2c 20 20 31 36 35 2c 20 20 31 32 39 2c 20 20  0,  165,  129,  
30d88 31 33 30 2c 20 20 31 33 31 2c 20 20 31 33 32 2c  130,  131,  132,
30d89 20 20 31 33 33 2c 0a 20 2f 2a 20 20 31 33 38 30    133,. /*  1380
30d8a 20 2a 2f 20 20 20 31 33 34 2c 20 20 31 37 36 2c   */   134,  176,
30d8b 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 35    165,  165,  15
30d8c 30 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20  0,  150,  165,  
30d8d 31 36 35 2c 20 20 31 35 30 2c 20 20 20 38 36 2c  165,  150,   86,
30d8e 0a 20 2f 2a 20 20 31 33 39 30 20 2a 2f 20 20 20  . /*  1390 */   
30d8f 20 38 37 2c 20 20 31 35 30 2c 20 20 31 35 30 2c   87,  150,  150,
30d90 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 37    150,  150,  17
30d91 39 2c 20 20 20 31 38 2c 20 20 20 39 34 2c 20 20  9,   18,   94,  
30d92 20 34 35 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20   45,  165,. /*  
30d93 31 34 30 30 20 2a 2f 20 20 20 31 36 35 2c 20 20  1400 */   165,  
30d94 20 39 38 2c 20 20 31 35 37 2c 20 20 31 36 35 2c   98,  157,  165,
30d95 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 36    150,  150,  16
30d96 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  5,  165,  165,  
30d97 31 36 35 2c 0a 20 2f 2a 20 20 31 34 31 30 20 2a  165,. /*  1410 *
30d98 2f 20 20 20 31 35 37 2c 20 20 31 33 35 2c 20 20  /   157,  135,  
30d99 20 36 38 2c 20 20 31 35 36 2c 20 20 31 35 36 2c   68,  156,  156,
30d9a 20 20 31 35 36 2c 20 20 31 38 39 2c 20 20 31 35    156,  189,  15
30d9b 37 2c 20 20 31 35 37 2c 20 20 31 36 35 2c 0a 20  7,  157,  165,. 
30d9c 2f 2a 20 20 31 34 32 30 20 2a 2f 20 20 20 31 36  /*  1420 */   16
30d9d 35 2c 20 20 32 33 38 2c 20 20 31 35 36 2c 20 20  5,  238,  156,  
30d9e 32 33 39 2c 20 20 31 35 37 2c 20 20 31 38 39 2c  239,  157,  189,
30d9f 20 20 20 32 32 2c 20 20 32 31 39 2c 20 20 31 39     22,  219,  19
30da0 39 2c 20 20 31 35 37 2c 0a 20 2f 2a 20 20 31 34  9,  157,. /*  14
30da1 33 30 20 2a 2f 20 20 20 20 31 38 2c 20 20 31 39  30 */    18,  19
30da2 32 2c 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20  2,  129,  130,  
30da3 31 33 31 2c 20 20 31 35 37 2c 20 20 31 39 39 2c  131,  157,  199,
30da4 20 20 20 34 30 2c 20 20 32 31 39 2c 20 20 31 39     40,  219,  19
30da5 32 2c 0a 20 2f 2a 20 20 31 34 34 30 20 2a 2f 20  2,. /*  1440 */ 
30da6 20 20 31 39 32 2c 20 20 31 39 32 2c 20 20 31 35    192,  192,  15
30da7 37 2c 20 20 31 35 37 2c 20 20 32 34 33 2c 20 20  7,  157,  243,  
30da8 32 34 33 2c 20 20 20 33 38 2c 20 20 31 36 34 2c  243,   38,  164,
30da9 20 20 31 38 30 2c 20 20 32 34 36 2c 0a 20 2f 2a    180,  246,. /*
30daa 20 20 31 34 35 30 20 2a 2f 20 20 20 31 38 30 2c    1450 */   180,
30dab 20 20 20 20 31 2c 20 20 20 31 35 2c 20 20 31 38      1,   15,  18
30dac 39 2c 20 20 20 32 33 2c 20 20 32 34 39 2c 20 20  9,   23,  249,  
30dad 32 35 32 2c 20 20 20 32 32 2c 20 20 31 31 37 2c  252,   22,  117,
30dae 20 20 32 35 32 2c 0a 20 2f 2a 20 20 31 34 36 30    252,. /*  1460
30daf 20 2a 2f 20 20 20 31 31 37 2c 20 20 31 31 32 2c   */   117,  112,
30db0 20 20 31 31 37 2c 20 20 31 31 37 2c 20 20 31 31    117,  117,  11
30db1 37 2c 20 20 20 32 32 2c 20 20 20 31 31 2c 20 20  7,   22,   11,  
30db2 20 32 33 2c 20 20 20 32 33 2c 20 20 20 32 32 2c   23,   23,   22,
30db3 0a 20 2f 2a 20 20 31 34 37 30 20 2a 2f 20 20 20  . /*  1470 */   
30db4 20 32 32 2c 20 20 20 32 35 2c 20 20 20 32 33 2c   22,   25,   23,
30db5 20 20 20 33 35 2c 20 20 20 32 33 2c 20 20 20 32     35,   23,   2
30db6 33 2c 20 20 20 33 35 2c 20 20 31 31 39 2c 20 20  3,   35,  119,  
30db7 20 32 35 2c 20 20 20 32 35 2c 0a 20 2f 2a 20 20   25,   25,. /*  
30db8 31 34 38 30 20 2a 2f 20 20 20 20 32 32 2c 20 20  1480 */    22,  
30db9 31 31 37 2c 20 20 20 32 33 2c 20 20 20 32 33 2c  117,   23,   23,
30dba 20 20 20 32 37 2c 20 20 20 35 32 2c 20 20 20 32     27,   52,   2
30dbb 32 2c 20 20 20 32 32 2c 20 20 20 35 32 2c 20 20  2,   22,   52,  
30dbc 20 32 33 2c 0a 20 2f 2a 20 20 31 34 39 30 20 2a   23,. /*  1490 *
30dbd 2f 20 20 20 20 32 32 2c 20 20 20 33 35 2c 20 20  /    22,   35,  
30dbe 20 32 39 2c 20 20 20 32 32 2c 20 20 20 35 32 2c   29,   22,   52,
30dbf 20 20 20 32 32 2c 20 20 31 30 32 2c 20 20 31 30     22,  102,  10
30dc0 38 2c 20 20 20 31 39 2c 20 20 20 32 34 2c 0a 20  8,   19,   24,. 
30dc1 2f 2a 20 20 31 35 30 30 20 2a 2f 20 20 20 20 33  /*  1500 */    3
30dc2 39 2c 20 20 20 32 30 2c 20 20 31 30 34 2c 20 20  9,   20,  104,  
30dc3 20 32 35 2c 20 20 31 33 38 2c 20 20 20 34 33 2c   25,  138,   43,
30dc4 20 20 31 30 34 2c 20 20 20 32 32 2c 20 20 20 20    104,   22,    
30dc5 35 2c 20 20 20 20 31 2c 0a 20 2f 2a 20 20 31 35  5,    1,. /*  15
30dc6 31 30 20 2a 2f 20 20 20 31 31 37 2c 20 20 20 33  10 */   117,   3
30dc7 35 2c 20 20 31 30 37 2c 20 20 20 32 37 2c 20 20  5,  107,   27,  
30dc8 31 32 36 2c 20 20 20 37 36 2c 20 20 20 37 36 2c  126,   76,   76,
30dc9 20 20 20 32 32 2c 20 20 31 31 38 2c 20 20 20 20     22,  118,    
30dca 31 2c 0a 20 2f 2a 20 20 31 35 32 30 20 2a 2f 20  1,. /*  1520 */ 
30dcb 20 20 20 31 36 2c 20 20 31 32 30 2c 20 20 20 35     16,  120,   5
30dcc 33 2c 20 20 20 35 33 2c 20 20 31 31 38 2c 20 20  3,   53,  118,  
30dcd 20 32 30 2c 20 20 20 35 39 2c 20 20 31 30 37 2c   20,   59,  107,
30dce 20 20 20 32 32 2c 20 20 31 32 36 2c 0a 20 2f 2a     22,  126,. /*
30dcf 20 20 31 35 33 30 20 2a 2f 20 20 20 20 32 33 2c    1530 */    23,
30dd0 20 20 20 31 36 2c 20 20 20 32 33 2c 20 20 20 32     16,   23,   2
30dd1 32 2c 20 20 31 32 37 2c 20 20 20 31 35 2c 20 20  2,  127,   15,  
30dd2 31 34 30 2c 20 20 20 36 35 2c 20 20 20 20 33 2c  140,   65,    3,
30dd3 20 20 32 35 33 2c 0a 20 2f 2a 20 20 31 35 34 30    253,. /*  1540
30dd4 20 2a 2f 20 20 20 20 20 34 2c 0a 7d 3b 0a 23 64   */     4,.};.#d
30dd5 65 66 69 6e 65 20 59 59 5f 53 48 49 46 54 5f 55  efine YY_SHIFT_U
30dd6 53 45 5f 44 46 4c 54 20 28 2d 31 31 30 29 0a 23  SE_DFLT (-110).#
30dd7 64 65 66 69 6e 65 20 59 59 5f 53 48 49 46 54 5f  define YY_SHIFT_
30dd8 4d 41 58 20 34 31 37 0a 73 74 61 74 69 63 20 63  MAX 417.static c
30dd9 6f 6e 73 74 20 73 68 6f 72 74 20 79 79 5f 73 68  onst short yy_sh
30dda 69 66 74 5f 6f 66 73 74 5b 5d 20 3d 20 7b 0a 20  ift_ofst[] = {. 
30ddb 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20 20 31 31  /*     0 */   11
30ddc 31 2c 20 31 30 39 31 2c 20 31 31 39 38 2c 20 31  1, 1091, 1198, 1
30ddd 30 39 31 2c 20 31 32 32 33 2c 20 31 32 32 33 2c  091, 1223, 1223,
30dde 20 20 20 2d 32 2c 20 20 20 38 38 2c 20 20 20 38     -2,   88,   8
30ddf 38 2c 20 20 2d 31 39 2c 0a 20 2f 2a 20 20 20 20  8,  -19,. /*    
30de0 31 30 20 2a 2f 20 20 31 32 32 33 2c 20 31 32 32  10 */  1223, 122
30de1 33 2c 20 31 32 32 33 2c 20 31 32 32 33 2c 20 31  3, 1223, 1223, 1
30de2 32 32 33 2c 20 20 32 31 30 2c 20 20 34 36 35 2c  223,  210,  465,
30de3 20 20 31 32 39 2c 20 31 31 30 34 2c 20 31 32 32    129, 1104, 122
30de4 33 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20  3,. /*    20 */ 
30de5 20 31 32 32 33 2c 20 31 32 32 33 2c 20 31 32 32   1223, 1223, 122
30de6 33 2c 20 31 32 32 33 2c 20 31 32 32 33 2c 20 31  3, 1223, 1223, 1
30de7 32 32 33 2c 20 31 32 32 33 2c 20 31 32 32 33 2c  223, 1223, 1223,
30de8 20 31 32 32 33 2c 20 31 32 32 33 2c 0a 20 2f 2a   1223, 1223,. /*
30de9 20 20 20 20 33 30 20 2a 2f 20 20 31 32 32 33 2c      30 */  1223,
30dea 20 31 32 32 33 2c 20 31 32 34 36 2c 20 31 32 32   1223, 1246, 122
30deb 33 2c 20 31 32 32 33 2c 20 31 32 32 33 2c 20 31  3, 1223, 1223, 1
30dec 32 32 33 2c 20 31 32 32 33 2c 20 31 32 32 33 2c  223, 1223, 1223,
30ded 20 31 32 32 33 2c 0a 20 2f 2a 20 20 20 20 34 30   1223,. /*    40
30dee 20 2a 2f 20 20 31 32 32 33 2c 20 31 32 32 33 2c   */  1223, 1223,
30def 20 31 32 32 33 2c 20 31 32 32 33 2c 20 31 32 32   1223, 1223, 122
30df0 33 2c 20 31 32 32 33 2c 20 31 32 32 33 2c 20 31  3, 1223, 1223, 1
30df1 32 32 33 2c 20 31 32 32 33 2c 20 31 32 32 33 2c  223, 1223, 1223,
30df2 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 31  . /*    50 */  1
30df3 32 32 33 2c 20 31 32 32 33 2c 20 31 32 32 33 2c  223, 1223, 1223,
30df4 20 31 32 32 33 2c 20 31 32 32 33 2c 20 31 32 32   1223, 1223, 122
30df5 33 2c 20 31 32 32 33 2c 20 31 32 32 33 2c 20 31  3, 1223, 1223, 1
30df6 32 32 33 2c 20 31 32 32 33 2c 0a 20 2f 2a 20 20  223, 1223,. /*  
30df7 20 20 36 30 20 2a 2f 20 20 31 32 32 33 2c 20 20    60 */  1223,  
30df8 2d 34 39 2c 20 20 33 36 31 2c 20 20 34 36 35 2c  -49,  361,  465,
30df9 20 20 34 36 35 2c 20 20 31 35 34 2c 20 20 31 33    465,  154,  13
30dfa 38 2c 20 20 31 33 38 2c 20 2d 31 30 39 2c 20 20  8,  138, -109,  
30dfb 20 35 35 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a   55,. /*    70 *
30dfc 2f 20 20 20 32 30 33 2c 20 20 32 37 37 2c 20 20  /   203,  277,  
30dfd 33 35 31 2c 20 20 34 32 35 2c 20 20 34 39 39 2c  351,  425,  499,
30dfe 20 20 35 37 33 2c 20 20 36 34 37 2c 20 20 37 32    573,  647,  72
30dff 31 2c 20 20 37 39 35 2c 20 20 38 36 39 2c 0a 20  1,  795,  869,. 
30e00 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 37 39  /*    80 */   79
30e01 35 2c 20 20 37 39 35 2c 20 20 37 39 35 2c 20 20  5,  795,  795,  
30e02 37 39 35 2c 20 20 37 39 35 2c 20 20 37 39 35 2c  795,  795,  795,
30e03 20 20 37 39 35 2c 20 20 37 39 35 2c 20 20 37 39    795,  795,  79
30e04 35 2c 20 20 37 39 35 2c 0a 20 2f 2a 20 20 20 20  5,  795,. /*    
30e05 39 30 20 2a 2f 20 20 20 37 39 35 2c 20 20 37 39  90 */   795,  79
30e06 35 2c 20 20 37 39 35 2c 20 20 37 39 35 2c 20 20  5,  795,  795,  
30e07 37 39 35 2c 20 20 37 39 35 2c 20 20 37 39 35 2c  795,  795,  795,
30e08 20 20 37 39 35 2c 20 20 39 34 33 2c 20 31 30 31    795,  943, 101
30e09 37 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20  7,. /*   100 */ 
30e0a 20 31 30 31 37 2c 20 20 2d 36 39 2c 20 20 2d 36   1017,  -69,  -6
30e0b 39 2c 20 20 2d 36 39 2c 20 20 2d 36 39 2c 20 20  9,  -69,  -69,  
30e0c 20 2d 31 2c 20 20 20 2d 31 2c 20 20 20 35 37 2c   -1,   -1,   57,
30e0d 20 20 31 35 35 2c 20 20 2d 34 34 2c 0a 20 2f 2a    155,  -44,. /*
30e0e 20 20 20 31 31 30 20 2a 2f 20 20 20 34 36 35 2c     110 */   465,
30e0f 20 20 34 36 35 2c 20 20 34 36 35 2c 20 20 34 36    465,  465,  46
30e10 35 2c 20 20 34 36 35 2c 20 20 36 35 34 2c 20 20  5,  465,  654,  
30e11 32 30 35 2c 20 20 34 36 35 2c 20 20 34 36 35 2c  205,  465,  465,
30e12 20 20 34 36 35 2c 0a 20 2f 2a 20 20 20 31 32 30    465,. /*   120
30e13 20 2a 2f 20 20 20 34 36 35 2c 20 20 34 36 35 2c   */   465,  465,
30e14 20 20 34 36 35 2c 20 20 34 36 35 2c 20 20 34 36    465,  465,  46
30e15 35 2c 20 20 34 36 35 2c 20 20 34 36 35 2c 20 20  5,  465,  465,  
30e16 34 36 35 2c 20 20 34 36 35 2c 20 20 34 36 35 2c  465,  465,  465,
30e17 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 20  . /*   130 */   
30e18 34 36 35 2c 20 20 34 36 35 2c 20 20 34 36 35 2c  465,  465,  465,
30e19 20 20 32 34 38 2c 20 20 31 35 34 2c 20 20 2d 38    248,  154,  -8
30e1a 30 2c 20 2d 31 31 30 2c 20 2d 31 31 30 2c 20 2d  0, -110, -110, -
30e1b 31 31 30 2c 20 31 33 30 33 2c 0a 20 2f 2a 20 20  110, 1303,. /*  
30e1c 20 31 34 30 20 2a 2f 20 20 20 31 33 31 2c 20 20   140 */   131,  
30e1d 20 39 35 2c 20 20 32 39 31 2c 20 20 33 35 32 2c   95,  291,  352,
30e1e 20 20 34 35 38 2c 20 20 35 31 30 2c 20 20 35 38    458,  510,  58
30e1f 32 2c 20 20 35 38 32 2c 20 20 34 36 35 2c 20 20  2,  582,  465,  
30e20 34 36 35 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a  465,. /*   150 *
30e21 2f 20 20 20 34 36 35 2c 20 20 34 36 35 2c 20 20  /   465,  465,  
30e22 34 36 35 2c 20 20 34 36 35 2c 20 20 34 36 35 2c  465,  465,  465,
30e23 20 20 34 36 35 2c 20 20 34 36 35 2c 20 20 34 36    465,  465,  46
30e24 35 2c 20 20 34 36 35 2c 20 20 34 36 35 2c 0a 20  5,  465,  465,. 
30e25 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 34 36  /*   160 */   46
30e26 35 2c 20 20 34 36 35 2c 20 20 34 36 35 2c 20 20  5,  465,  465,  
30e27 34 36 35 2c 20 20 34 36 35 2c 20 20 34 36 35 2c  465,  465,  465,
30e28 20 20 34 36 35 2c 20 20 34 36 35 2c 20 20 34 36    465,  465,  46
30e29 35 2c 20 20 34 36 35 2c 0a 20 2f 2a 20 20 20 31  5,  465,. /*   1
30e2a 37 30 20 2a 2f 20 20 20 34 36 35 2c 20 20 34 36  70 */   465,  46
30e2b 35 2c 20 20 34 36 35 2c 20 20 34 36 35 2c 20 20  5,  465,  465,  
30e2c 34 36 35 2c 20 20 34 36 35 2c 20 20 34 36 35 2c  465,  465,  465,
30e2d 20 20 34 36 35 2c 20 20 34 36 35 2c 20 20 34 36    465,  465,  46
30e2e 35 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20  5,. /*   180 */ 
30e2f 20 20 36 31 33 2c 20 20 36 38 33 2c 20 20 36 30    613,  683,  60
30e30 31 2c 20 20 33 37 39 2c 20 20 33 37 39 2c 20 20  1,  379,  379,  
30e31 33 37 39 2c 20 20 36 35 37 2c 20 20 35 38 36 2c  379,  657,  586,
30e32 20 2d 31 30 39 2c 20 2d 31 30 39 2c 0a 20 2f 2a   -109, -109,. /*
30e33 20 20 20 31 39 30 20 2a 2f 20 20 2d 31 30 39 2c     190 */  -109,
30e34 20 2d 31 31 30 2c 20 2d 31 31 30 2c 20 2d 31 31   -110, -110, -11
30e35 30 2c 20 20 31 37 32 2c 20 20 31 37 32 2c 20 20  0,  172,  172,  
30e36 32 37 35 2c 20 20 31 36 30 2c 20 20 35 31 36 2c  275,  160,  516,
30e37 20 20 36 36 37 2c 0a 20 2f 2a 20 20 20 32 30 30    667,. /*   200
30e38 20 2a 2f 20 20 20 37 32 34 2c 20 20 34 34 32 2c   */   724,  442,
30e39 20 20 37 34 34 2c 20 20 38 38 33 2c 20 20 20 36    744,  883,   6
30e3a 30 2c 20 20 20 36 30 2c 20 20 36 31 32 2c 20 20  0,   60,  612,  
30e3b 33 36 37 2c 20 20 32 33 36 2c 20 20 38 30 33 2c  367,  236,  803,
30e3c 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 20  . /*   210 */   
30e3d 37 30 38 2c 20 20 37 30 38 2c 20 20 31 34 33 2c  708,  708,  143,
30e3e 20 20 37 31 38 2c 20 20 37 30 38 2c 20 20 37 30    718,  708,  70
30e3f 38 2c 20 20 37 30 38 2c 20 20 37 30 38 2c 20 20  8,  708,  708,  
30e40 35 34 32 2c 20 20 34 32 36 2c 0a 20 2f 2a 20 20  542,  426,. /*  
30e41 20 32 32 30 20 2a 2f 20 20 20 34 33 38 2c 20 20   220 */   438,  
30e42 31 35 34 2c 20 20 37 37 33 2c 20 20 37 37 33 2c  154,  773,  773,
30e43 20 20 37 31 33 2c 20 20 34 32 38 2c 20 20 34 32    713,  428,  42
30e44 38 2c 20 20 39 30 34 2c 20 20 34 32 38 2c 20 20  8,  904,  428,  
30e45 38 37 36 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a  876,. /*   230 *
30e46 2f 20 20 20 34 32 38 2c 20 20 31 35 34 2c 20 20  /   428,  154,  
30e47 34 32 38 2c 20 20 31 35 34 2c 20 20 36 34 33 2c  428,  154,  643,
30e48 20 31 30 32 34 2c 20 20 39 34 36 2c 20 31 30 32   1024,  946, 102
30e49 34 2c 20 20 39 30 34 2c 20 20 39 30 34 2c 0a 20  4,  904,  904,. 
30e4a 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20 20 39 34  /*   240 */   94
30e4b 36 2c 20 31 31 31 35 2c 20 31 31 31 35 2c 20 31  6, 1115, 1115, 1
30e4c 31 31 35 2c 20 31 31 31 35 2c 20 31 31 32 39 2c  115, 1115, 1129,
30e4d 20 31 31 32 39 2c 20 31 31 32 37 2c 20 2d 31 30   1129, 1127, -10
30e4e 39 2c 20 31 30 34 30 2c 0a 20 2f 2a 20 20 20 32  9, 1040,. /*   2
30e4f 35 30 20 2a 2f 20 20 31 30 35 32 2c 20 31 30 35  50 */  1052, 105
30e50 39 2c 20 31 30 36 32 2c 20 31 30 37 32 2c 20 31  9, 1062, 1072, 1
30e51 30 36 36 2c 20 31 31 31 33 2c 20 31 31 31 33 2c  066, 1113, 1113,
30e52 20 31 31 34 30 2c 20 31 31 33 37 2c 20 31 31 34   1140, 1137, 114
30e53 30 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20  0,. /*   260 */ 
30e54 20 31 31 33 37 2c 20 31 31 34 30 2c 20 31 31 33   1137, 1140, 113
30e55 37 2c 20 31 31 35 34 2c 20 31 31 35 34 2c 20 31  7, 1154, 1154, 1
30e56 32 33 31 2c 20 31 31 35 34 2c 20 31 32 31 31 2c  231, 1154, 1211,
30e57 20 31 31 35 34 2c 20 31 33 30 32 2c 0a 20 2f 2a   1154, 1302,. /*
30e58 20 20 20 32 37 30 20 2a 2f 20 20 31 32 35 36 2c     270 */  1256,
30e59 20 31 32 35 36 2c 20 31 32 33 31 2c 20 31 31 35   1256, 1231, 115
30e5a 34 2c 20 31 31 35 34 2c 20 31 31 35 34 2c 20 31  4, 1154, 1154, 1
30e5b 33 30 32 2c 20 31 33 37 38 2c 20 31 31 31 33 2c  302, 1378, 1113,
30e5c 20 31 33 37 38 2c 0a 20 2f 2a 20 20 20 32 38 30   1378,. /*   280
30e5d 20 2a 2f 20 20 31 31 31 33 2c 20 31 33 37 38 2c   */  1113, 1378,
30e5e 20 31 31 31 33 2c 20 31 31 31 33 2c 20 31 33 35   1113, 1113, 135
30e5f 33 2c 20 31 32 37 36 2c 20 31 33 37 38 2c 20 31  3, 1276, 1378, 1
30e60 31 31 33 2c 20 31 33 34 34 2c 20 31 33 34 34 2c  113, 1344, 1344,
30e61 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 31  . /*   290 */  1
30e62 34 30 34 2c 20 31 30 34 30 2c 20 31 31 31 33 2c  404, 1040, 1113,
30e63 20 31 34 31 32 2c 20 31 34 31 32 2c 20 31 34 31   1412, 1412, 141
30e64 32 2c 20 31 34 31 32 2c 20 31 30 34 30 2c 20 31  2, 1412, 1040, 1
30e65 33 34 34 2c 20 31 34 30 34 2c 0a 20 2f 2a 20 20  344, 1404,. /*  
30e66 20 33 30 30 20 2a 2f 20 20 31 31 31 33 2c 20 31   300 */  1113, 1
30e67 33 39 37 2c 20 31 33 39 37 2c 20 31 31 31 33 2c  397, 1397, 1113,
30e68 20 31 31 31 33 2c 20 31 34 30 38 2c 20 2d 31 31   1113, 1408, -11
30e69 30 2c 20 2d 31 31 30 2c 20 2d 31 31 30 2c 20 2d  0, -110, -110, -
30e6a 31 31 30 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a  110,. /*   310 *
30e6b 2f 20 20 2d 31 31 30 2c 20 2d 31 31 30 2c 20 20  /  -110, -110,  
30e6c 39 33 39 2c 20 20 20 34 36 2c 20 20 35 34 37 2c  939,   46,  547,
30e6d 20 20 39 30 35 2c 20 20 39 38 33 2c 20 20 39 37    905,  983,  97
30e6e 31 2c 20 20 39 37 32 2c 20 20 39 37 30 2c 0a 20  1,  972,  970,. 
30e6f 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20 31 30 33  /*   320 */  103
30e70 37 2c 20 20 39 34 31 2c 20 20 39 38 32 2c 20 31  7,  941,  982, 1
30e71 30 32 39 2c 20 31 30 34 37 2c 20 31 30 39 37 2c  029, 1047, 1097,
30e72 20 31 31 31 30 2c 20 31 31 32 38 2c 20 31 32 36   1110, 1128, 126
30e73 32 2c 20 31 32 36 33 2c 0a 20 2f 2a 20 20 20 33  2, 1263,. /*   3
30e74 33 30 20 2a 2f 20 20 31 30 39 33 2c 20 31 32 39  30 */  1093, 129
30e75 37 2c 20 31 34 35 30 2c 20 31 34 33 37 2c 20 31  7, 1450, 1437, 1
30e76 34 33 31 2c 20 31 34 33 35 2c 20 31 33 34 31 2c  431, 1435, 1341,
30e77 20 31 33 34 33 2c 20 31 33 34 35 2c 20 31 33 34   1343, 1345, 134
30e78 36 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20  6,. /*   340 */ 
30e79 20 31 33 34 37 2c 20 31 33 34 39 2c 20 31 34 34   1347, 1349, 144
30e7a 33 2c 20 31 34 34 34 2c 20 31 34 34 35 2c 20 31  3, 1444, 1445, 1
30e7b 34 34 37 2c 20 31 34 35 35 2c 20 31 34 34 38 2c  447, 1455, 1448,
30e7c 20 31 34 34 39 2c 20 31 34 34 36 2c 0a 20 2f 2a   1449, 1446,. /*
30e7d 20 20 20 33 35 30 20 2a 2f 20 20 31 34 35 31 2c     350 */  1451,
30e7e 20 31 34 35 32 2c 20 31 34 35 33 2c 20 31 34 33   1452, 1453, 143
30e7f 38 2c 20 31 34 35 34 2c 20 31 34 34 31 2c 20 31  8, 1454, 1441, 1
30e80 34 35 33 2c 20 31 33 35 38 2c 20 31 34 35 38 2c  453, 1358, 1458,
30e81 20 31 34 35 36 2c 0a 20 2f 2a 20 20 20 33 36 30   1456,. /*   360
30e82 20 2a 2f 20 20 31 34 35 37 2c 20 31 33 36 34 2c   */  1457, 1364,
30e83 20 31 34 35 39 2c 20 31 34 36 30 2c 20 31 34 36   1459, 1460, 146
30e84 31 2c 20 31 34 33 33 2c 20 31 34 36 34 2c 20 31  1, 1433, 1464, 1
30e85 34 36 33 2c 20 31 34 33 36 2c 20 31 34 36 35 2c  463, 1436, 1465,
30e86 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20 31  . /*   370 */  1
30e87 34 36 36 2c 20 31 34 36 38 2c 20 31 34 37 31 2c  466, 1468, 1471,
30e88 20 31 34 34 32 2c 20 31 34 37 33 2c 20 31 33 39   1442, 1473, 139
30e89 34 2c 20 31 33 38 39 2c 20 31 34 37 39 2c 20 31  4, 1389, 1479, 1
30e8a 34 38 31 2c 20 31 34 37 35 2c 0a 20 2f 2a 20 20  481, 1475,. /*  
30e8b 20 33 38 30 20 2a 2f 20 20 31 33 39 38 2c 20 31   380 */  1398, 1
30e8c 34 36 32 2c 20 31 34 36 37 2c 20 31 34 36 39 2c  462, 1467, 1469,
30e8d 20 31 34 37 38 2c 20 31 34 37 30 2c 20 31 33 36   1478, 1470, 136
30e8e 36 2c 20 31 34 30 32 2c 20 31 34 38 35 2c 20 31  6, 1402, 1485, 1
30e8f 35 30 33 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a  503,. /*   390 *
30e90 2f 20 20 31 35 30 38 2c 20 31 33 39 33 2c 20 31  /  1508, 1393, 1
30e91 34 37 36 2c 20 31 34 38 36 2c 20 31 34 30 35 2c  476, 1486, 1405,
30e92 20 31 34 33 39 2c 20 31 34 34 30 2c 20 31 33 38   1439, 1440, 138
30e93 38 2c 20 31 34 39 35 2c 20 31 34 30 30 2c 0a 20  8, 1495, 1400,. 
30e94 2f 2a 20 20 20 34 30 30 20 2a 2f 20 20 31 35 31  /*   400 */  151
30e95 38 2c 20 31 35 30 34 2c 20 31 34 30 31 2c 20 31  8, 1504, 1401, 1
30e96 35 30 35 2c 20 31 34 30 36 2c 20 31 34 32 30 2c  505, 1406, 1420,
30e97 20 31 34 30 33 2c 20 31 35 30 36 2c 20 31 34 30   1403, 1506, 140
30e98 37 2c 20 31 35 30 37 2c 0a 20 2f 2a 20 20 20 34  7, 1507,. /*   4
30e99 31 30 20 2a 2f 20 20 31 35 30 39 2c 20 31 35 31  10 */  1509, 151
30e9a 35 2c 20 31 34 37 32 2c 20 31 35 32 30 2c 20 31  5, 1472, 1520, 1
30e9b 33 39 36 2c 20 31 35 31 31 2c 20 31 35 33 35 2c  396, 1511, 1535,
30e9c 20 31 35 33 36 2c 0a 7d 3b 0a 23 64 65 66 69 6e   1536,.};.#defin
30e9d 65 20 59 59 5f 52 45 44 55 43 45 5f 55 53 45 5f  e YY_REDUCE_USE_
30e9e 44 46 4c 54 20 28 2d 31 39 37 29 0a 23 64 65 66  DFLT (-197).#def
30e9f 69 6e 65 20 59 59 5f 52 45 44 55 43 45 5f 4d 41  ine YY_REDUCE_MA
30ea0 58 20 33 31 31 0a 73 74 61 74 69 63 20 63 6f 6e  X 311.static con
30ea1 73 74 20 73 68 6f 72 74 20 79 79 5f 72 65 64 75  st short yy_redu
30ea2 63 65 5f 6f 66 73 74 5b 5d 20 3d 20 7b 0a 20 2f  ce_ofst[] = {. /
30ea3 2a 20 20 20 20 20 30 20 2a 2f 20 20 2d 31 34 31  *     0 */  -141
30ea4 2c 20 20 20 39 30 2c 20 20 20 31 36 2c 20 20 31  ,   90,   16,  1
30ea5 34 37 2c 20 20 2d 35 35 2c 20 20 20 32 31 2c 20  47,  -55,   21, 
30ea6 20 31 34 38 2c 20 20 31 34 39 2c 20 20 31 35 38   148,  149,  158
30ea7 2c 20 20 32 34 30 2c 0a 20 2f 2a 20 20 20 20 31  ,  240,. /*    1
30ea8 30 20 2a 2f 20 20 20 32 32 33 2c 20 20 32 33 37  0 */   223,  237
30ea9 2c 20 20 32 34 32 2c 20 20 33 30 33 2c 20 20 33  ,  242,  303,  3
30eaa 30 37 2c 20 20 31 36 34 2c 20 20 33 37 30 2c 20  07,  164,  370, 
30eab 20 31 37 31 2c 20 20 33 36 39 2c 20 20 33 37 36   171,  369,  376
30eac 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20  ,. /*    20 */  
30ead 20 33 38 30 2c 20 20 34 34 33 2c 20 20 34 35 30   380,  443,  450
30eae 2c 20 20 35 30 34 2c 20 20 35 31 37 2c 20 20 35  ,  504,  517,  5
30eaf 32 34 2c 20 20 35 33 35 2c 20 20 35 33 37 2c 20  24,  535,  537, 
30eb0 20 35 37 38 2c 20 20 35 39 31 2c 0a 20 2f 2a 20   578,  591,. /* 
30eb1 20 20 20 33 30 20 2a 2f 20 20 20 35 39 34 2c 20     30 */   594, 
30eb2 20 35 39 39 2c 20 20 36 31 31 2c 20 20 36 35 32   599,  611,  652
30eb3 2c 20 20 36 36 35 2c 20 20 36 36 38 2c 20 20 36  ,  665,  668,  6
30eb4 38 36 2c 20 20 37 32 36 2c 20 20 37 33 39 2c 20  86,  726,  739, 
30eb5 20 37 34 32 2c 0a 20 2f 2a 20 20 20 20 34 30 20   742,. /*    40 
30eb6 2a 2f 20 20 20 37 34 36 2c 20 20 37 36 30 2c 20  */   746,  760, 
30eb7 20 38 30 30 2c 20 20 38 31 36 2c 20 20 38 33 34   800,  816,  834
30eb8 2c 20 20 38 37 34 2c 20 20 38 38 38 2c 20 20 38  ,  874,  888,  8
30eb9 39 30 2c 20 20 39 30 38 2c 20 20 39 31 31 2c 0a  90,  908,  911,.
30eba 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 39   /*    50 */   9
30ebb 36 32 2c 20 20 39 37 35 2c 20 20 39 38 39 2c 20  62,  975,  989, 
30ebc 20 39 39 33 2c 20 31 30 33 30 2c 20 31 30 38 39   993, 1030, 1089
30ebd 2c 20 31 30 39 36 2c 20 31 31 30 32 2c 20 31 31  , 1096, 1102, 11
30ebe 31 39 2c 20 31 31 32 35 2c 0a 20 2f 2a 20 20 20  19, 1125,. /*   
30ebf 20 36 30 20 2a 2f 20 20 31 31 33 31 2c 20 2d 31   60 */  1131, -1
30ec0 39 36 2c 20 20 39 35 34 2c 20 20 37 34 30 2c 20  96,  954,  740, 
30ec1 20 33 39 36 2c 20 20 31 36 39 2c 20 20 2d 36 38   396,  169,  -68
30ec2 2c 20 20 34 36 33 2c 20 20 34 30 35 2c 20 20 34  ,  463,  405,  4
30ec3 35 39 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f  59,. /*    70 */
30ec4 20 20 20 34 35 39 2c 20 20 34 35 39 2c 20 20 34     459,  459,  4
30ec5 35 39 2c 20 20 34 35 39 2c 20 20 34 35 39 2c 20  59,  459,  459, 
30ec6 20 34 35 39 2c 20 20 34 35 39 2c 20 20 34 35 39   459,  459,  459
30ec7 2c 20 20 34 35 39 2c 20 20 34 35 39 2c 0a 20 2f  ,  459,  459,. /
30ec8 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 34 35 39  *    80 */   459
30ec9 2c 20 20 34 35 39 2c 20 20 34 35 39 2c 20 20 34  ,  459,  459,  4
30eca 35 39 2c 20 20 34 35 39 2c 20 20 34 35 39 2c 20  59,  459,  459, 
30ecb 20 34 35 39 2c 20 20 34 35 39 2c 20 20 34 35 39   459,  459,  459
30ecc 2c 20 20 34 35 39 2c 0a 20 2f 2a 20 20 20 20 39  ,  459,. /*    9
30ecd 30 20 2a 2f 20 20 20 34 35 39 2c 20 20 34 35 39  0 */   459,  459
30ece 2c 20 20 34 35 39 2c 20 20 34 35 39 2c 20 20 34  ,  459,  459,  4
30ecf 35 39 2c 20 20 34 35 39 2c 20 20 34 35 39 2c 20  59,  459,  459, 
30ed0 20 34 35 39 2c 20 20 34 35 39 2c 20 20 34 35 39   459,  459,  459
30ed1 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20  ,. /*   100 */  
30ed2 20 34 35 39 2c 20 20 34 35 39 2c 20 20 34 35 39   459,  459,  459
30ed3 2c 20 20 34 35 39 2c 20 20 34 35 39 2c 20 20 34  ,  459,  459,  4
30ed4 35 39 2c 20 20 34 35 39 2c 20 20 2d 32 31 2c 20  59,  459,  -21, 
30ed5 20 34 35 39 2c 20 20 34 35 39 2c 0a 20 2f 2a 20   459,  459,. /* 
30ed6 20 20 31 31 30 20 2a 2f 20 20 20 35 33 38 2c 20    110 */   538, 
30ed7 20 33 37 35 2c 20 20 35 39 32 2c 20 20 36 36 36   375,  592,  666
30ed8 2c 20 20 38 31 34 2c 20 20 20 20 36 2c 20 20 32  ,  814,    6,  2
30ed9 32 32 2c 20 20 35 32 31 2c 20 20 36 38 32 2c 20  22,  521,  682, 
30eda 20 38 31 37 2c 0a 20 2f 2a 20 20 20 31 32 30 20   817,. /*   120 
30edb 2a 2f 20 20 20 33 35 36 2c 20 20 32 34 34 2c 20  */   356,  244, 
30edc 20 34 36 36 2c 20 20 36 38 34 2c 20 20 36 39 31   466,  684,  691
30edd 2c 20 20 38 39 31 2c 20 20 39 39 34 2c 20 31 30  ,  891,  994, 10
30ede 32 33 2c 20 31 30 36 33 2c 20 31 31 34 32 2c 0a  23, 1063, 1142,.
30edf 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 31 31   /*   130 */  11
30ee0 36 39 2c 20 20 37 35 39 2c 20 31 31 37 33 2c 20  69,  759, 1173, 
30ee1 20 34 35 39 2c 20 20 2d 38 39 2c 20 20 34 35 39   459,  -89,  459
30ee2 2c 20 20 34 35 39 2c 20 20 34 35 39 2c 20 20 34  ,  459,  459,  4
30ee3 35 39 2c 20 20 32 38 35 2c 0a 20 2f 2a 20 20 20  59,  285,. /*   
30ee4 31 34 30 20 2a 2f 20 20 20 20 37 36 2c 20 20 34  140 */    76,  4
30ee5 33 30 2c 20 20 35 39 38 2c 20 20 36 31 30 2c 20  30,  598,  610, 
30ee6 20 37 36 35 2c 20 20 38 31 38 2c 20 20 34 32 33   765,  818,  423
30ee7 2c 20 20 34 38 35 2c 20 20 38 39 32 2c 20 20 39  ,  485,  892,  9
30ee8 30 39 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f  09,. /*   150 */
30ee9 20 20 20 39 31 30 2c 20 20 39 36 39 2c 20 31 30     910,  969, 10
30eea 30 36 2c 20 20 38 31 38 2c 20 31 30 31 31 2c 20  06,  818, 1011, 
30eeb 31 30 31 36 2c 20 31 30 36 37 2c 20 31 30 37 36  1016, 1067, 1076
30eec 2c 20 31 31 33 32 2c 20 31 31 37 37 2c 0a 20 2f  , 1132, 1177,. /
30eed 2a 20 20 20 31 36 30 20 2a 2f 20 20 31 31 37 38  *   160 */  1178
30eee 2c 20 31 31 38 31 2c 20 31 31 38 36 2c 20 31 31  , 1181, 1186, 11
30eef 39 35 2c 20 31 31 39 39 2c 20 31 32 30 30 2c 20  95, 1199, 1200, 
30ef0 31 32 30 39 2c 20 31 32 31 37 2c 20 31 32 31 38  1209, 1217, 1218
30ef1 2c 20 31 32 32 31 2c 0a 20 2f 2a 20 20 20 31 37  , 1221,. /*   17
30ef2 30 20 2a 2f 20 20 31 32 32 32 2c 20 31 32 33 34  0 */  1222, 1234
30ef3 2c 20 31 32 33 35 2c 20 31 32 33 38 2c 20 31 32  , 1235, 1238, 12
30ef4 34 31 2c 20 31 32 34 32 2c 20 31 32 34 33 2c 20  41, 1242, 1243, 
30ef5 31 32 34 34 2c 20 31 32 35 34 2c 20 31 32 35 35  1244, 1254, 1255
30ef6 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20  ,. /*   180 */  
30ef7 20 35 33 32 2c 20 20 35 33 32 2c 20 20 35 34 39   532,  532,  549
30ef8 2c 20 20 31 37 38 2c 20 20 33 32 34 2c 20 20 36  ,  178,  324,  6
30ef9 38 38 2c 20 20 34 34 36 2c 20 20 37 36 39 2c 20  88,  446,  769, 
30efa 20 37 37 36 2c 20 20 38 30 39 2c 0a 20 2f 2a 20   776,  809,. /* 
30efb 20 20 31 39 30 20 2a 2f 20 20 20 38 31 33 2c 20    190 */   813, 
30efc 20 36 35 35 2c 20 20 35 38 31 2c 20 20 37 33 38   655,  581,  738
30efd 2c 20 20 2d 37 34 2c 20 20 2d 35 38 2c 20 20 2d  ,  -74,  -58,  -
30efe 35 30 2c 20 20 2d 31 37 2c 20 20 2d 32 33 2c 20  50,  -17,  -23, 
30eff 20 2d 32 33 2c 0a 20 2f 2a 20 20 20 32 30 30 20   -23,. /*   200 
30f00 2a 2f 20 20 20 2d 32 33 2c 20 20 20 36 33 2c 20  */   -23,   63, 
30f01 20 2d 32 33 2c 20 20 20 32 39 2c 20 20 20 36 38   -23,   29,   68
30f02 2c 20 20 31 32 31 2c 20 20 31 38 33 2c 20 20 31  ,  121,  183,  1
30f03 34 36 2c 20 20 32 32 35 2c 20 20 20 32 39 2c 0a  46,  225,   29,.
30f04 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 20 2d   /*   210 */   -
30f05 32 33 2c 20 20 2d 32 33 2c 20 20 31 39 36 2c 20  23,  -23,  196, 
30f06 20 31 37 37 2c 20 20 2d 32 33 2c 20 20 2d 32 33   177,  -23,  -23
30f07 2c 20 20 2d 32 33 2c 20 20 2d 32 33 2c 20 20 32  ,  -23,  -23,  2
30f08 35 35 2c 20 20 33 32 38 2c 0a 20 2f 2a 20 20 20  55,  328,. /*   
30f09 32 32 30 20 2a 2f 20 20 20 33 33 35 2c 20 20 33  220 */   335,  3
30f0a 38 31 2c 20 20 34 30 34 2c 20 20 34 33 39 2c 20  81,  404,  439, 
30f0b 20 34 34 39 2c 20 20 36 30 30 2c 20 20 36 34 38   449,  600,  648
30f0c 2c 20 20 35 34 36 2c 20 20 36 38 35 2c 20 20 36  ,  546,  685,  6
30f0d 33 38 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f  38,. /*   230 */
30f0e 20 20 20 37 31 37 2c 20 20 36 36 33 2c 20 20 37     717,  663,  7
30f0f 32 32 2c 20 20 38 31 31 2c 20 20 37 33 34 2c 20  22,  811,  734, 
30f10 20 37 35 36 2c 20 20 38 30 31 2c 20 20 38 32 37   756,  801,  827
30f11 2c 20 20 38 36 38 2c 20 20 38 37 32 2c 0a 20 2f  ,  868,  872,. /
30f12 2a 20 20 20 32 34 30 20 2a 2f 20 20 20 38 39 39  *   240 */   899
30f13 2c 20 20 39 36 37 2c 20 20 39 38 30 2c 20 20 39  ,  967,  980,  9
30f14 39 36 2c 20 20 39 39 37 2c 20 20 39 38 31 2c 20  96,  997,  981, 
30f15 20 39 38 37 2c 20 31 30 30 33 2c 20 20 39 36 31   987, 1003,  961
30f16 2c 20 20 39 37 36 2c 0a 20 2f 2a 20 20 20 32 35  ,  976,. /*   25
30f17 30 20 2a 2f 20 20 20 39 37 39 2c 20 20 39 39 32  0 */   979,  992
30f18 2c 20 20 39 39 38 2c 20 31 30 30 34 2c 20 20 39  ,  998, 1004,  9
30f19 39 31 2c 20 31 30 35 37 2c 20 31 30 37 30 2c 20  91, 1057, 1070, 
30f1a 31 30 30 39 2c 20 31 30 33 36 2c 20 31 30 33 38  1009, 1036, 1038
30f1b 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20  ,. /*   260 */  
30f1c 31 30 36 39 2c 20 31 30 34 31 2c 20 31 30 38 30  1069, 1041, 1080
30f1d 2c 20 31 30 39 38 2c 20 31 31 32 32 2c 20 31 31  , 1098, 1122, 11
30f1e 31 31 2c 20 31 31 33 35 2c 20 31 31 32 33 2c 20  11, 1135, 1123, 
30f1f 31 31 33 39 2c 20 31 31 36 38 2c 0a 20 2f 2a 20  1139, 1168,. /* 
30f20 20 20 32 37 30 20 2a 2f 20 20 31 31 33 33 2c 20    270 */  1133, 
30f21 31 31 34 33 2c 20 31 31 37 34 2c 20 31 31 38 35  1143, 1174, 1185
30f22 2c 20 31 31 39 34 2c 20 31 32 30 35 2c 20 31 32  , 1194, 1205, 12
30f23 31 36 2c 20 31 32 35 37 2c 20 31 32 34 35 2c 20  16, 1257, 1245, 
30f24 31 32 35 38 2c 0a 20 2f 2a 20 20 20 32 38 30 20  1258,. /*   280 
30f25 2a 2f 20 20 31 32 35 33 2c 20 31 32 35 39 2c 20  */  1253, 1259, 
30f26 31 32 36 30 2c 20 31 32 36 31 2c 20 31 31 38 33  1260, 1261, 1183
30f27 2c 20 31 31 38 34 2c 20 31 32 36 36 2c 20 31 32  , 1184, 1266, 12
30f28 36 37 2c 20 31 32 32 37 2c 20 31 32 33 36 2c 0a  67, 1227, 1236,.
30f29 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 31 32   /*   290 */  12
30f2a 30 38 2c 20 31 32 32 39 2c 20 31 32 37 32 2c 20  08, 1229, 1272, 
30f2b 31 32 33 39 2c 20 31 32 34 37 2c 20 31 32 34 38  1239, 1247, 1248
30f2c 2c 20 31 32 34 39 2c 20 31 32 33 37 2c 20 31 32  , 1249, 1237, 12
30f2d 36 34 2c 20 31 32 31 39 2c 0a 20 2f 2a 20 20 20  64, 1219,. /*   
30f2e 33 30 30 20 2a 2f 20 20 31 32 37 38 2c 20 31 32  300 */  1278, 12
30f2f 30 31 2c 20 31 32 30 32 2c 20 31 32 38 35 2c 20  01, 1202, 1285, 
30f30 31 32 38 36 2c 20 31 32 30 33 2c 20 31 32 38 33  1286, 1203, 1283
30f31 2c 20 31 32 36 38 2c 20 31 32 37 30 2c 20 31 32  , 1268, 1270, 12
30f32 30 36 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f  06,. /*   310 */
30f33 20 20 31 32 30 34 2c 20 31 32 30 37 2c 0a 7d 3b    1204, 1207,.};
30f34 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59 59  .static const YY
30f35 41 43 54 49 4f 4e 54 59 50 45 20 79 79 5f 64 65  ACTIONTYPE yy_de
30f36 66 61 75 6c 74 5b 5d 20 3d 20 7b 0a 20 2f 2a 20  fault[] = {. /* 
30f37 20 20 20 20 30 20 2a 2f 20 20 20 36 33 34 2c 20      0 */   634, 
30f38 20 38 36 39 2c 20 20 39 35 38 2c 20 20 39 35 38   869,  958,  958
30f39 2c 20 20 38 36 39 2c 20 20 39 35 38 2c 20 20 39  ,  869,  958,  9
30f3a 35 38 2c 20 20 38 39 38 2c 20 20 38 39 38 2c 20  58,  898,  898, 
30f3b 20 37 35 37 2c 0a 20 2f 2a 20 20 20 20 31 30 20   757,. /*    10 
30f3c 2a 2f 20 20 20 38 36 37 2c 20 20 39 35 38 2c 20  */   867,  958, 
30f3d 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
30f3e 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30f3f 33 32 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a  32,  958,  958,.
30f40 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20 20 39   /*    20 */   9
30f41 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
30f42 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
30f43 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30f44 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20  58,  958,. /*   
30f45 20 33 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39   30 */   958,  9
30f46 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
30f47 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
30f48 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30f49 35 38 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f  58,. /*    40 */
30f4a 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39     958,  958,  9
30f4b 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
30f4c 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
30f4d 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f  ,  958,  958,. /
30f4e 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 39 35 38  *    50 */   958
30f4f 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30f50 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
30f51 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
30f52 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 20 36  ,  958,. /*    6
30f53 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 38 34 31  0 */   958,  841
30f54 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30f55 35 38 2c 20 20 36 37 33 2c 20 20 38 39 38 2c 20  58,  673,  898, 
30f56 20 38 39 38 2c 20 20 37 36 31 2c 20 20 37 39 32   898,  761,  792
30f57 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20  ,. /*    70 */  
30f58 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
30f59 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30f5a 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
30f5b 20 37 39 33 2c 20 20 39 35 38 2c 0a 20 2f 2a 20   793,  958,. /* 
30f5c 20 20 20 38 30 20 2a 2f 20 20 20 38 37 31 2c 20     80 */   871, 
30f5d 20 38 36 36 2c 20 20 38 36 32 2c 20 20 38 36 34   866,  862,  864
30f5e 2c 20 20 38 36 33 2c 20 20 38 37 30 2c 20 20 37  ,  863,  870,  7
30f5f 39 34 2c 20 20 37 38 33 2c 20 20 37 39 30 2c 20  94,  783,  790, 
30f60 20 37 39 37 2c 0a 20 2f 2a 20 20 20 20 39 30 20   797,. /*    90 
30f61 2a 2f 20 20 20 37 37 32 2c 20 20 39 31 31 2c 20  */   772,  911, 
30f62 20 37 39 39 2c 20 20 38 30 30 2c 20 20 38 30 36   799,  800,  806
30f63 2c 20 20 38 30 37 2c 20 20 39 33 33 2c 20 20 39  ,  807,  933,  9
30f64 33 31 2c 20 20 38 32 39 2c 20 20 38 32 38 2c 0a  31,  829,  828,.
30f65 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20 20 38   /*   100 */   8
30f66 34 37 2c 20 20 38 33 31 2c 20 20 38 34 35 2c 20  47,  831,  845, 
30f67 20 38 35 33 2c 20 20 38 34 36 2c 20 20 38 33 30   853,  846,  830
30f68 2c 20 20 38 34 30 2c 20 20 36 36 35 2c 20 20 38  ,  840,  665,  8
30f69 33 32 2c 20 20 38 33 33 2c 0a 20 2f 2a 20 20 20  32,  833,. /*   
30f6a 31 31 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39  110 */   958,  9
30f6b 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
30f6c 20 39 35 38 2c 20 20 37 32 36 2c 20 20 36 36 30   958,  726,  660
30f6d 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30f6e 35 38 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f  58,. /*   120 */
30f6f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39     958,  958,  9
30f70 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
30f71 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
30f72 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f  ,  958,  958,. /
30f73 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 39 35 38  *   130 */   958
30f74 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 38  ,  958,  958,  8
30f75 33 34 2c 20 20 39 35 38 2c 20 20 38 33 35 2c 20  34,  958,  835, 
30f76 20 38 34 38 2c 20 20 38 34 39 2c 20 20 38 35 30   848,  849,  850
30f77 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 31 34  ,  958,. /*   14
30f78 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38  0 */   958,  958
30f79 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30f7a 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
30f7b 20 39 35 38 2c 20 20 36 34 30 2c 20 20 39 35 38   958,  640,  958
30f7c 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20  ,. /*   150 */  
30f7d 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
30f7e 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30f7f 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
30f80 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20   958,  958,. /* 
30f81 20 20 31 36 30 20 2a 2f 20 20 20 39 35 38 2c 20    160 */   958, 
30f82 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
30f83 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30f84 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
30f85 20 39 35 38 2c 0a 20 2f 2a 20 20 20 31 37 30 20   958,. /*   170 
30f86 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20  */   958,  958, 
30f87 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
30f88 2c 20 20 38 38 32 2c 20 20 39 35 38 2c 20 20 39  ,  882,  958,  9
30f89 33 36 2c 20 20 39 33 38 2c 20 20 39 35 38 2c 0a  36,  938,  958,.
30f8a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20 20 39   /*   180 */   9
30f8b 35 38 2c 20 20 39 35 38 2c 20 20 36 33 34 2c 20  58,  958,  634, 
30f8c 20 37 35 37 2c 20 20 37 35 37 2c 20 20 37 35 37   757,  757,  757
30f8d 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30f8e 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20  58,  958,. /*   
30f8f 31 39 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 37  190 */   958,  7
30f90 35 31 2c 20 20 37 36 31 2c 20 20 39 35 30 2c 20  51,  761,  950, 
30f91 20 39 35 38 2c 20 20 39 35 38 2c 20 20 37 31 37   958,  958,  717
30f92 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30f93 35 38 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f  58,. /*   200 */
30f94 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39     958,  958,  9
30f95 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
30f96 20 39 35 38 2c 20 20 36 34 32 2c 20 20 37 34 39   958,  642,  749
30f97 2c 20 20 36 37 35 2c 20 20 37 35 39 2c 0a 20 2f  ,  675,  759,. /
30f98 2a 20 20 20 32 31 30 20 2a 2f 20 20 20 36 36 32  *   210 */   662
30f99 2c 20 20 37 33 38 2c 20 20 39 30 34 2c 20 20 39  ,  738,  904,  9
30f9a 35 38 2c 20 20 39 32 33 2c 20 20 39 32 31 2c 20  58,  923,  921, 
30f9b 20 37 34 30 2c 20 20 38 30 32 2c 20 20 39 35 38   740,  802,  958
30f9c 2c 20 20 37 34 39 2c 0a 20 2f 2a 20 20 20 32 32  ,  749,. /*   22
30f9d 30 20 2a 2f 20 20 20 37 35 38 2c 20 20 39 35 38  0 */   758,  958
30f9e 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 38  ,  958,  958,  8
30f9f 36 35 2c 20 20 37 38 36 2c 20 20 37 38 36 2c 20  65,  786,  786, 
30fa0 20 37 37 34 2c 20 20 37 38 36 2c 20 20 36 39 36   774,  786,  696
30fa1 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20  ,. /*   230 */  
30fa2 20 37 38 36 2c 20 20 39 35 38 2c 20 20 37 38 36   786,  958,  786
30fa3 2c 20 20 39 35 38 2c 20 20 36 39 39 2c 20 20 39  ,  958,  699,  9
30fa4 31 36 2c 20 20 37 39 36 2c 20 20 39 31 36 2c 20  16,  796,  916, 
30fa5 20 37 37 34 2c 20 20 37 37 34 2c 0a 20 2f 2a 20   774,  774,. /* 
30fa6 20 20 32 34 30 20 2a 2f 20 20 20 37 39 36 2c 20    240 */   796, 
30fa7 20 36 33 39 2c 20 20 36 33 39 2c 20 20 36 33 39   639,  639,  639
30fa8 2c 20 20 36 33 39 2c 20 20 36 35 30 2c 20 20 36  ,  639,  650,  6
30fa9 35 30 2c 20 20 37 31 36 2c 20 20 39 35 38 2c 20  50,  716,  958, 
30faa 20 37 39 36 2c 0a 20 2f 2a 20 20 20 32 35 30 20   796,. /*   250 
30fab 2a 2f 20 20 20 37 38 37 2c 20 20 37 38 39 2c 20  */   787,  789, 
30fac 20 37 37 39 2c 20 20 37 39 31 2c 20 20 39 35 38   779,  791,  958
30fad 2c 20 20 37 36 35 2c 20 20 37 36 35 2c 20 20 37  ,  765,  765,  7
30fae 37 33 2c 20 20 37 37 38 2c 20 20 37 37 33 2c 0a  73,  778,  773,.
30faf 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20 20 37   /*   260 */   7
30fb0 37 38 2c 20 20 37 37 33 2c 20 20 37 37 38 2c 20  78,  773,  778, 
30fb1 20 37 32 38 2c 20 20 37 32 38 2c 20 20 37 31 33   728,  728,  713
30fb2 2c 20 20 37 32 38 2c 20 20 36 39 39 2c 20 20 37  ,  728,  699,  7
30fb3 32 38 2c 20 20 38 37 35 2c 0a 20 2f 2a 20 20 20  28,  875,. /*   
30fb4 32 37 30 20 2a 2f 20 20 20 38 37 39 2c 20 20 38  270 */   879,  8
30fb5 37 39 2c 20 20 37 31 33 2c 20 20 37 32 38 2c 20  79,  713,  728, 
30fb6 20 37 32 38 2c 20 20 37 32 38 2c 20 20 38 37 35   728,  728,  875
30fb7 2c 20 20 36 35 37 2c 20 20 37 36 35 2c 20 20 36  ,  657,  765,  6
30fb8 35 37 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f  57,. /*   280 */
30fb9 20 20 20 37 36 35 2c 20 20 36 35 37 2c 20 20 37     765,  657,  7
30fba 36 35 2c 20 20 37 36 35 2c 20 20 39 30 38 2c 20  65,  765,  908, 
30fbb 20 39 31 30 2c 20 20 36 35 37 2c 20 20 37 36 35   910,  657,  765
30fbc 2c 20 20 37 33 30 2c 20 20 37 33 30 2c 0a 20 2f  ,  730,  730,. /
30fbd 2a 20 20 20 32 39 30 20 2a 2f 20 20 20 38 30 38  *   290 */   808
30fbe 2c 20 20 37 39 36 2c 20 20 37 36 35 2c 20 20 37  ,  796,  765,  7
30fbf 33 37 2c 20 20 37 33 37 2c 20 20 37 33 37 2c 20  37,  737,  737, 
30fc0 20 37 33 37 2c 20 20 37 39 36 2c 20 20 37 33 30   737,  796,  730
30fc1 2c 20 20 38 30 38 2c 0a 20 2f 2a 20 20 20 33 30  ,  808,. /*   30
30fc2 30 20 2a 2f 20 20 20 37 36 35 2c 20 20 39 33 35  0 */   765,  935
30fc3 2c 20 20 39 33 35 2c 20 20 37 36 35 2c 20 20 37  ,  935,  765,  7
30fc4 36 35 2c 20 20 39 34 33 2c 20 20 36 38 33 2c 20  65,  943,  683, 
30fc5 20 37 30 31 2c 20 20 37 30 31 2c 20 20 39 35 30   701,  701,  950
30fc6 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20  ,. /*   310 */  
30fc7 20 39 35 35 2c 20 20 39 35 35 2c 20 20 39 35 38   955,  955,  958
30fc8 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30fc9 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
30fca 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20   958,  958,. /* 
30fcb 20 20 33 32 30 20 2a 2f 20 20 20 39 35 38 2c 20    320 */   958, 
30fcc 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
30fcd 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30fce 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
30fcf 20 39 35 38 2c 0a 20 2f 2a 20 20 20 33 33 30 20   958,. /*   330 
30fd0 2a 2f 20 20 20 38 38 34 2c 20 20 39 35 38 2c 20  */   884,  958, 
30fd1 20 39 35 38 2c 20 20 36 34 38 2c 20 20 39 35 38   958,  648,  958
30fd2 2c 20 20 36 36 37 2c 20 20 38 31 35 2c 20 20 38  ,  667,  815,  8
30fd3 32 30 2c 20 20 38 31 36 2c 20 20 39 35 38 2c 0a  20,  816,  958,.
30fd4 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20 20 38   /*   340 */   8
30fd5 31 37 2c 20 20 37 34 33 2c 20 20 39 35 38 2c 20  17,  743,  958, 
30fd6 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
30fd7 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30fd8 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20  58,  958,. /*   
30fd9 33 35 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39  350 */   958,  9
30fda 35 38 2c 20 20 38 36 38 2c 20 20 39 35 38 2c 20  58,  868,  958, 
30fdb 20 37 38 30 2c 20 20 39 35 38 2c 20 20 37 38 38   780,  958,  788
30fdc 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30fdd 35 38 2c 0a 20 2f 2a 20 20 20 33 36 30 20 2a 2f  58,. /*   360 */
30fde 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39     958,  958,  9
30fdf 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
30fe0 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
30fe1 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f  ,  958,  958,. /
30fe2 2a 20 20 20 33 37 30 20 2a 2f 20 20 20 39 35 38  *   370 */   958
30fe3 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30fe4 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
30fe5 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
30fe6 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 33 38  ,  958,. /*   38
30fe7 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38  0 */   958,  958
30fe8 2c 20 20 39 35 38 2c 20 20 39 30 36 2c 20 20 39  ,  958,  906,  9
30fe9 30 37 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  07,  958,  958, 
30fea 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
30feb 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20 20  ,. /*   390 */  
30fec 20 39 35 38 2c 20 20 39 31 34 2c 20 20 39 35 38   958,  914,  958
30fed 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30fee 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
30fef 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20   958,  958,. /* 
30ff0 20 20 34 30 30 20 2a 2f 20 20 20 39 35 38 2c 20    400 */   958, 
30ff1 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
30ff2 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
30ff3 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
30ff4 20 39 35 38 2c 0a 20 2f 2a 20 20 20 34 31 30 20   958,. /*   410 
30ff5 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20  */   958,  958, 
30ff6 20 39 34 32 2c 20 20 39 35 38 2c 20 20 39 35 38   942,  958,  958
30ff7 2c 20 20 39 34 35 2c 20 20 36 33 35 2c 20 20 39  ,  945,  635,  9
30ff8 35 38 2c 20 20 36 33 30 2c 20 20 36 33 32 2c 0a  58,  630,  632,.
30ff9 20 2f 2a 20 20 20 34 32 30 20 2a 2f 20 20 20 36   /*   420 */   6
30ffa 33 33 2c 20 20 36 33 37 2c 20 20 36 33 38 2c 20  33,  637,  638, 
30ffb 20 36 34 31 2c 20 20 36 36 37 2c 20 20 36 36 38   641,  667,  668
30ffc 2c 20 20 36 37 30 2c 20 20 36 37 31 2c 20 20 36  ,  670,  671,  6
30ffd 37 32 2c 20 20 36 34 33 2c 0a 20 2f 2a 20 20 20  72,  643,. /*   
30ffe 34 33 30 20 2a 2f 20 20 20 36 34 34 2c 20 20 36  430 */   644,  6
30fff 34 35 2c 20 20 36 34 36 2c 20 20 36 34 37 2c 20  45,  646,  647, 
31000 20 36 34 39 2c 20 20 36 35 33 2c 20 20 36 35 31   649,  653,  651
31001 2c 20 20 36 35 32 2c 20 20 36 35 34 2c 20 20 36  ,  652,  654,  6
31002 36 31 2c 0a 20 2f 2a 20 20 20 34 34 30 20 2a 2f  61,. /*   440 */
31003 20 20 20 36 36 33 2c 20 20 36 38 32 2c 20 20 36     663,  682,  6
31004 38 34 2c 20 20 36 38 36 2c 20 20 37 34 37 2c 20  84,  686,  747, 
31005 20 37 34 38 2c 20 20 38 31 32 2c 20 20 37 34 31   748,  812,  741
31006 2c 20 20 37 34 32 2c 20 20 37 34 36 2c 0a 20 2f  ,  742,  746,. /
31007 2a 20 20 20 34 35 30 20 2a 2f 20 20 20 36 36 39  *   450 */   669
31008 2c 20 20 38 32 33 2c 20 20 38 31 34 2c 20 20 38  ,  823,  814,  8
31009 31 38 2c 20 20 38 31 39 2c 20 20 38 32 31 2c 20  18,  819,  821, 
3100a 20 38 32 32 2c 20 20 38 33 36 2c 20 20 38 33 37   822,  836,  837
3100b 2c 20 20 38 33 39 2c 0a 20 2f 2a 20 20 20 34 36  ,  839,. /*   46
3100c 30 20 2a 2f 20 20 20 38 34 34 2c 20 20 38 35 32  0 */   844,  852
3100d 2c 20 20 38 35 35 2c 20 20 38 33 38 2c 20 20 38  ,  855,  838,  8
3100e 34 33 2c 20 20 38 35 31 2c 20 20 38 35 34 2c 20  43,  851,  854, 
3100f 20 37 34 34 2c 20 20 37 34 35 2c 20 20 38 35 38   744,  745,  858
31010 2c 0a 20 2f 2a 20 20 20 34 37 30 20 2a 2f 20 20  ,. /*   470 */  
31011 20 36 37 36 2c 20 20 36 37 37 2c 20 20 36 38 30   676,  677,  680
31012 2c 20 20 36 38 31 2c 20 20 38 39 34 2c 20 20 38  ,  681,  894,  8
31013 39 36 2c 20 20 38 39 35 2c 20 20 38 39 37 2c 20  96,  895,  897, 
31014 20 36 37 39 2c 20 20 36 37 38 2c 0a 20 2f 2a 20   679,  678,. /* 
31015 20 20 34 38 30 20 2a 2f 20 20 20 38 32 34 2c 20    480 */   824, 
31016 20 38 32 37 2c 20 20 38 36 30 2c 20 20 38 36 31   827,  860,  861
31017 2c 20 20 39 32 34 2c 20 20 39 32 35 2c 20 20 39  ,  924,  925,  9
31018 32 36 2c 20 20 39 32 37 2c 20 20 39 32 38 2c 20  26,  927,  928, 
31019 20 38 35 36 2c 0a 20 2f 2a 20 20 20 34 39 30 20   856,. /*   490 
3101a 2a 2f 20 20 20 37 36 36 2c 20 20 38 35 39 2c 20  */   766,  859, 
3101b 20 38 34 32 2c 20 20 37 38 31 2c 20 20 37 38 34   842,  781,  784
3101c 2c 20 20 37 38 35 2c 20 20 37 38 32 2c 20 20 37  ,  785,  782,  7
3101d 35 30 2c 20 20 37 36 30 2c 20 20 37 36 38 2c 0a  50,  760,  768,.
3101e 20 2f 2a 20 20 20 35 30 30 20 2a 2f 20 20 20 37   /*   500 */   7
3101f 36 39 2c 20 20 37 37 30 2c 20 20 37 37 31 2c 20  69,  770,  771, 
31020 20 37 35 35 2c 20 20 37 35 36 2c 20 20 37 36 32   755,  756,  762
31021 2c 20 20 37 37 37 2c 20 20 38 31 30 2c 20 20 38  ,  777,  810,  8
31022 31 31 2c 20 20 37 37 35 2c 0a 20 2f 2a 20 20 20  11,  775,. /*   
31023 35 31 30 20 2a 2f 20 20 20 37 37 36 2c 20 20 37  510 */   776,  7
31024 36 33 2c 20 20 37 36 34 2c 20 20 37 35 32 2c 20  63,  764,  752, 
31025 20 37 35 33 2c 20 20 37 35 34 2c 20 20 38 35 37   753,  754,  857
31026 2c 20 20 38 31 33 2c 20 20 38 32 35 2c 20 20 38  ,  813,  825,  8
31027 32 36 2c 0a 20 2f 2a 20 20 20 35 32 30 20 2a 2f  26,. /*   520 */
31028 20 20 20 36 38 37 2c 20 20 36 38 38 2c 20 20 38     687,  688,  8
31029 32 30 2c 20 20 36 38 39 2c 20 20 36 39 30 2c 20  20,  689,  690, 
3102a 20 36 39 31 2c 20 20 37 32 39 2c 20 20 37 33 32   691,  729,  732
3102b 2c 20 20 37 33 33 2c 20 20 37 33 34 2c 0a 20 2f  ,  733,  734,. /
3102c 2a 20 20 20 35 33 30 20 2a 2f 20 20 20 36 39 32  *   530 */   692
3102d 2c 20 20 37 31 31 2c 20 20 37 31 34 2c 20 20 37  ,  711,  714,  7
3102e 31 35 2c 20 20 36 39 33 2c 20 20 37 30 30 2c 20  15,  693,  700, 
3102f 20 36 39 34 2c 20 20 36 39 35 2c 20 20 37 30 32   694,  695,  702
31030 2c 20 20 37 30 33 2c 0a 20 2f 2a 20 20 20 35 34  ,  703,. /*   54
31031 30 20 2a 2f 20 20 20 37 30 34 2c 20 20 37 30 36  0 */   704,  706
31032 2c 20 20 37 30 37 2c 20 20 37 30 38 2c 20 20 37  ,  707,  708,  7
31033 30 39 2c 20 20 37 31 30 2c 20 20 37 30 35 2c 20  09,  710,  705, 
31034 20 38 37 36 2c 20 20 38 37 37 2c 20 20 38 38 30   876,  877,  880
31035 2c 0a 20 2f 2a 20 20 20 35 35 30 20 2a 2f 20 20  ,. /*   550 */  
31036 20 38 37 38 2c 20 20 36 39 37 2c 20 20 36 39 38   878,  697,  698
31037 2c 20 20 37 31 32 2c 20 20 36 38 35 2c 20 20 36  ,  712,  685,  6
31038 37 34 2c 20 20 36 36 36 2c 20 20 37 31 38 2c 20  74,  666,  718, 
31039 20 37 32 31 2c 20 20 37 32 32 2c 0a 20 2f 2a 20   721,  722,. /* 
3103a 20 20 35 36 30 20 2a 2f 20 20 20 37 32 33 2c 20    560 */   723, 
3103b 20 37 32 34 2c 20 20 37 32 35 2c 20 20 37 32 37   724,  725,  727
3103c 2c 20 20 37 31 39 2c 20 20 37 32 30 2c 20 20 36  ,  719,  720,  6
3103d 36 34 2c 20 20 36 35 35 2c 20 20 36 35 38 2c 20  64,  655,  658, 
3103e 20 37 36 37 2c 0a 20 2f 2a 20 20 20 35 37 30 20   767,. /*   570 
3103f 2a 2f 20 20 20 39 30 30 2c 20 20 39 30 39 2c 20  */   900,  909, 
31040 20 39 30 35 2c 20 20 39 30 31 2c 20 20 39 30 32   905,  901,  902
31041 2c 20 20 39 30 33 2c 20 20 36 35 39 2c 20 20 38  ,  903,  659,  8
31042 37 32 2c 20 20 38 37 33 2c 20 20 37 33 31 2c 0a  72,  873,  731,.
31043 20 2f 2a 20 20 20 35 38 30 20 2a 2f 20 20 20 38   /*   580 */   8
31044 30 34 2c 20 20 38 30 35 2c 20 20 38 39 39 2c 20  04,  805,  899, 
31045 20 39 31 32 2c 20 20 39 31 35 2c 20 20 39 31 37   912,  915,  917
31046 2c 20 20 39 31 38 2c 20 20 39 31 39 2c 20 20 38  ,  918,  919,  8
31047 30 39 2c 20 20 39 32 30 2c 0a 20 2f 2a 20 20 20  09,  920,. /*   
31048 35 39 30 20 2a 2f 20 20 20 39 32 32 2c 20 20 39  590 */   922,  9
31049 31 33 2c 20 20 39 34 37 2c 20 20 36 35 36 2c 20  13,  947,  656, 
3104a 20 37 33 35 2c 20 20 37 33 36 2c 20 20 37 33 39   735,  736,  739
3104b 2c 20 20 38 38 31 2c 20 20 39 32 39 2c 20 20 37  ,  881,  929,  7
3104c 39 35 2c 0a 20 2f 2a 20 20 20 36 30 30 20 2a 2f  95,. /*   600 */
3104d 20 20 20 37 39 38 2c 20 20 38 30 31 2c 20 20 38     798,  801,  8
3104e 30 33 2c 20 20 38 38 33 2c 20 20 38 38 35 2c 20  03,  883,  885, 
3104f 20 38 38 37 2c 20 20 38 38 39 2c 20 20 38 39 30   887,  889,  890
31050 2c 20 20 38 39 31 2c 20 20 38 39 32 2c 0a 20 2f  ,  891,  892,. /
31051 2a 20 20 20 36 31 30 20 2a 2f 20 20 20 38 39 33  *   610 */   893
31052 2c 20 20 38 38 36 2c 20 20 38 38 38 2c 20 20 39  ,  886,  888,  9
31053 33 30 2c 20 20 39 33 34 2c 20 20 39 33 37 2c 20  30,  934,  937, 
31054 20 39 33 39 2c 20 20 39 34 30 2c 20 20 39 34 31   939,  940,  941
31055 2c 20 20 39 34 34 2c 0a 20 2f 2a 20 20 20 36 32  ,  944,. /*   62
31056 30 20 2a 2f 20 20 20 39 34 36 2c 20 20 39 35 31  0 */   946,  951
31057 2c 20 20 39 35 32 2c 20 20 39 35 33 2c 20 20 39  ,  952,  953,  9
31058 35 36 2c 20 20 39 35 37 2c 20 20 39 35 34 2c 20  56,  957,  954, 
31059 20 36 33 36 2c 20 20 36 33 31 2c 0a 7d 3b 0a 23   636,  631,.};.#
3105a 64 65 66 69 6e 65 20 59 59 5f 53 5a 5f 41 43 54  define YY_SZ_ACT
3105b 54 41 42 20 28 69 6e 74 29 28 73 69 7a 65 6f 66  TAB (int)(sizeof
3105c 28 79 79 5f 61 63 74 69 6f 6e 29 2f 73 69 7a 65  (yy_action)/size
3105d 6f 66 28 79 79 5f 61 63 74 69 6f 6e 5b 30 5d 29  of(yy_action[0])
3105e 29 0a 0a 2f 2a 20 54 68 65 20 6e 65 78 74 20 74  )../* The next t
3105f 61 62 6c 65 20 6d 61 70 73 20 74 6f 6b 65 6e 73  able maps tokens
31060 20 69 6e 74 6f 20 66 61 6c 6c 62 61 63 6b 20 74   into fallback t
31061 6f 6b 65 6e 73 2e 20 20 49 66 20 61 20 63 6f 6e  okens.  If a con
31062 73 74 72 75 63 74 0a 2a 2a 20 6c 69 6b 65 20 74  struct.** like t
31063 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a  he following:.**
31064 20 0a 2a 2a 20 20 20 20 20 20 25 66 61 6c 6c 62   .**      %fallb
31065 61 63 6b 20 49 44 20 58 20 59 20 5a 2e 0a 2a 2a  ack ID X Y Z..**
31066 0a 2a 2a 20 61 70 70 65 61 72 73 20 69 6e 20 74  .** appears in t
31067 68 65 20 67 72 61 6d 6d 61 72 2c 20 74 68 65 6e  he grammar, then
31068 20 49 44 20 62 65 63 6f 6d 65 73 20 61 20 66 61   ID becomes a fa
31069 6c 6c 62 61 63 6b 20 74 6f 6b 65 6e 20 66 6f 72  llback token for
3106a 20 58 2c 20 59 2c 0a 2a 2a 20 61 6e 64 20 5a 2e   X, Y,.** and Z.
3106b 20 20 57 68 65 6e 65 76 65 72 20 6f 6e 65 20 6f    Whenever one o
3106c 66 20 74 68 65 20 74 6f 6b 65 6e 73 20 58 2c 20  f the tokens X, 
3106d 59 2c 20 6f 72 20 5a 20 69 73 20 69 6e 70 75 74  Y, or Z is input
3106e 20 74 6f 20 74 68 65 20 70 61 72 73 65 72 0a 2a   to the parser.*
3106f 2a 20 62 75 74 20 69 74 20 64 6f 65 73 20 6e 6f  * but it does no
31070 74 20 70 61 72 73 65 2c 20 74 68 65 20 74 79 70  t parse, the typ
31071 65 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69  e of the token i
31072 73 20 63 68 61 6e 67 65 64 20 74 6f 20 49 44 20  s changed to ID 
31073 61 6e 64 0a 2a 2a 20 74 68 65 20 70 61 72 73 65  and.** the parse
31074 20 69 73 20 72 65 74 72 69 65 64 20 62 65 66 6f   is retried befo
31075 72 65 20 61 6e 20 65 72 72 6f 72 20 69 73 20 74  re an error is t
31076 68 72 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66  hrown..*/.#ifdef
31077 20 59 59 46 41 4c 4c 42 41 43 4b 0a 73 74 61 74   YYFALLBACK.stat
31078 69 63 20 63 6f 6e 73 74 20 59 59 43 4f 44 45 54  ic const YYCODET
31079 59 50 45 20 79 79 46 61 6c 6c 62 61 63 6b 5b 5d  YPE yyFallback[]
3107a 20 3d 20 7b 0a 20 20 20 20 30 2c 20 20 2f 2a 20   = {.    0,  /* 
3107b 20 20 20 20 20 20 20 20 20 24 20 3d 3e 20 6e 6f           $ => no
3107c 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20  thing */.    0, 
3107d 20 2f 2a 20 20 20 20 20 20 20 53 45 4d 49 20 3d   /*       SEMI =
3107e 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20  > nothing */.   
3107f 32 36 2c 20 20 2f 2a 20 20 20 20 45 58 50 4c 41  26,  /*    EXPLA
31080 49 4e 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  IN => ID */.   2
31081 36 2c 20 20 2f 2a 20 20 20 20 20 20 51 55 45 52  6,  /*      QUER
31082 59 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  Y => ID */.   26
31083 2c 20 20 2f 2a 20 20 20 20 20 20 20 50 4c 41 4e  ,  /*       PLAN
31084 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
31085 20 20 2f 2a 20 20 20 20 20 20 42 45 47 49 4e 20    /*      BEGIN 
31086 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20  => ID */.    0, 
31087 20 2f 2a 20 54 52 41 4e 53 41 43 54 49 4f 4e 20   /* TRANSACTION 
31088 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
31089 20 32 36 2c 20 20 2f 2a 20 20 20 44 45 46 45 52   26,  /*   DEFER
3108a 52 45 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  RED => ID */.   
3108b 32 36 2c 20 20 2f 2a 20 20 49 4d 4d 45 44 49 41  26,  /*  IMMEDIA
3108c 54 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  TE => ID */.   2
3108d 36 2c 20 20 2f 2a 20 20 45 58 43 4c 55 53 49 56  6,  /*  EXCLUSIV
3108e 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30  E => ID */.    0
3108f 2c 20 20 2f 2a 20 20 20 20 20 43 4f 4d 4d 49 54  ,  /*     COMMIT
31090 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
31091 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20    26,  /*       
31092 20 45 4e 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20   END => ID */.  
31093 20 32 36 2c 20 20 2f 2a 20 20 20 52 4f 4c 4c 42   26,  /*   ROLLB
31094 41 43 4b 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ACK => ID */.   
31095 32 36 2c 20 20 2f 2a 20 20 53 41 56 45 50 4f 49  26,  /*  SAVEPOI
31096 4e 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  NT => ID */.   2
31097 36 2c 20 20 2f 2a 20 20 20 20 52 45 4c 45 41 53  6,  /*    RELEAS
31098 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30  E => ID */.    0
31099 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 54 4f  ,  /*         TO
3109a 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
3109b 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 54     0,  /*      T
3109c 41 42 4c 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  ABLE => nothing 
3109d 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
3109e 20 20 43 52 45 41 54 45 20 3d 3e 20 6e 6f 74 68    CREATE => noth
3109f 69 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f  ing */.   26,  /
310a0 2a 20 20 20 20 20 20 20 20 20 49 46 20 3d 3e 20  *         IF => 
310a1 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ID */.    0,  /*
310a2 20 20 20 20 20 20 20 20 4e 4f 54 20 3d 3e 20 6e          NOT => n
310a3 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
310a4 20 20 2f 2a 20 20 20 20 20 45 58 49 53 54 53 20    /*     EXISTS 
310a5 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
310a6 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 54   26,  /*       T
310a7 45 4d 50 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  EMP => ID */.   
310a8 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20   0,  /*         
310a9 4c 50 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f  LP => nothing */
310aa 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20  .    0,  /*     
310ab 20 20 20 20 52 50 20 3d 3e 20 6e 6f 74 68 69 6e      RP => nothin
310ac 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20  g */.    0,  /* 
310ad 20 20 20 20 20 20 20 20 41 53 20 3d 3e 20 6e 6f          AS => no
310ae 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20  thing */.    0, 
310af 20 2f 2a 20 20 20 20 20 20 43 4f 4d 4d 41 20 3d   /*      COMMA =
310b0 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20  > nothing */.   
310b1 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20   0,  /*         
310b2 49 44 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f  ID => nothing */
310b3 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 49  .    0,  /*    I
310b4 4e 44 45 58 45 44 20 3d 3e 20 6e 6f 74 68 69 6e  NDEXED => nothin
310b5 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  g */.   26,  /* 
310b6 20 20 20 20 20 41 42 4f 52 54 20 3d 3e 20 49 44       ABORT => ID
310b7 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
310b8 20 20 20 41 43 54 49 4f 4e 20 3d 3e 20 49 44 20     ACTION => ID 
310b9 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
310ba 20 20 20 41 46 54 45 52 20 3d 3e 20 49 44 20 2a     AFTER => ID *
310bb 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
310bc 41 4e 41 4c 59 5a 45 20 3d 3e 20 49 44 20 2a 2f  ANALYZE => ID */
310bd 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
310be 20 20 20 41 53 43 20 3d 3e 20 49 44 20 2a 2f 0a     ASC => ID */.
310bf 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 41     26,  /*     A
310c0 54 54 41 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20  TTACH => ID */. 
310c1 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 42 45    26,  /*     BE
310c2 46 4f 52 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  FORE => ID */.  
310c3 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20   26,  /*        
310c4 20 42 59 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20   BY => ID */.   
310c5 32 36 2c 20 20 2f 2a 20 20 20 20 43 41 53 43 41  26,  /*    CASCA
310c6 44 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  DE => ID */.   2
310c7 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 43 41 53  6,  /*       CAS
310c8 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  T => ID */.   26
310c9 2c 20 20 2f 2a 20 20 20 43 4f 4c 55 4d 4e 4b 57  ,  /*   COLUMNKW
310ca 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
310cb 20 20 2f 2a 20 20 20 43 4f 4e 46 4c 49 43 54 20    /*   CONFLICT 
310cc 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
310cd 20 2f 2a 20 20 20 44 41 54 41 42 41 53 45 20 3d   /*   DATABASE =
310ce 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
310cf 2f 2a 20 20 20 20 20 20 20 44 45 53 43 20 3d 3e  /*       DESC =>
310d0 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
310d1 2a 20 20 20 20 20 44 45 54 41 43 48 20 3d 3e 20  *     DETACH => 
310d2 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
310d3 20 20 20 20 20 20 20 45 41 43 48 20 3d 3e 20 49         EACH => I
310d4 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
310d5 20 20 20 20 20 20 46 41 49 4c 20 3d 3e 20 49 44        FAIL => ID
310d6 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
310d7 20 20 20 20 20 20 46 4f 52 20 3d 3e 20 49 44 20        FOR => ID 
310d8 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
310d9 20 20 49 47 4e 4f 52 45 20 3d 3e 20 49 44 20 2a    IGNORE => ID *
310da 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 49 4e  /.   26,  /*  IN
310db 49 54 49 41 4c 4c 59 20 3d 3e 20 49 44 20 2a 2f  ITIALLY => ID */
310dc 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 49  .   26,  /*    I
310dd 4e 53 54 45 41 44 20 3d 3e 20 49 44 20 2a 2f 0a  NSTEAD => ID */.
310de 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 4c 49     26,  /*    LI
310df 4b 45 5f 4b 57 20 3d 3e 20 49 44 20 2a 2f 0a 20  KE_KW => ID */. 
310e0 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 4d    26,  /*      M
310e1 41 54 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  ATCH => ID */.  
310e2 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20   26,  /*        
310e3 20 4e 4f 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20   NO => ID */.   
310e4 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 4b  26,  /*        K
310e5 45 59 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  EY => ID */.   2
310e6 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 4f  6,  /*         O
310e7 46 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  F => ID */.   26
310e8 2c 20 20 2f 2a 20 20 20 20 20 4f 46 46 53 45 54  ,  /*     OFFSET
310e9 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
310ea 20 20 2f 2a 20 20 20 20 20 50 52 41 47 4d 41 20    /*     PRAGMA 
310eb 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
310ec 20 2f 2a 20 20 20 20 20 20 52 41 49 53 45 20 3d   /*      RAISE =
310ed 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
310ee 2f 2a 20 20 20 20 52 45 50 4c 41 43 45 20 3d 3e  /*    REPLACE =>
310ef 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
310f0 2a 20 20 20 52 45 53 54 52 49 43 54 20 3d 3e 20  *   RESTRICT => 
310f1 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
310f2 20 20 20 20 20 20 20 20 52 4f 57 20 3d 3e 20 49          ROW => I
310f3 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
310f4 20 20 20 54 52 49 47 47 45 52 20 3d 3e 20 49 44     TRIGGER => ID
310f5 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
310f6 20 20 20 56 41 43 55 55 4d 20 3d 3e 20 49 44 20     VACUUM => ID 
310f7 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
310f8 20 20 20 20 56 49 45 57 20 3d 3e 20 49 44 20 2a      VIEW => ID *
310f9 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
310fa 56 49 52 54 55 41 4c 20 3d 3e 20 49 44 20 2a 2f  VIRTUAL => ID */
310fb 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 52  .   26,  /*    R
310fc 45 49 4e 44 45 58 20 3d 3e 20 49 44 20 2a 2f 0a  EINDEX => ID */.
310fd 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 52     26,  /*     R
310fe 45 4e 41 4d 45 20 3d 3e 20 49 44 20 2a 2f 0a 20  ENAME => ID */. 
310ff 20 20 32 36 2c 20 20 2f 2a 20 20 20 43 54 49 4d    26,  /*   CTIM
31100 45 5f 4b 57 20 3d 3e 20 49 44 20 2a 2f 0a 7d 3b  E_KW => ID */.};
31101 0a 23 65 6e 64 69 66 20 2f 2a 20 59 59 46 41 4c  .#endif /* YYFAL
31102 4c 42 41 43 4b 20 2a 2f 0a 0a 2f 2a 20 54 68 65  LBACK */../* The
31103 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
31104 74 75 72 65 20 72 65 70 72 65 73 65 6e 74 73 20  ture represents 
31105 61 20 73 69 6e 67 6c 65 20 65 6c 65 6d 65 6e 74  a single element
31106 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 72 73 65   of the.** parse
31107 72 27 73 20 73 74 61 63 6b 2e 20 20 49 6e 66 6f  r's stack.  Info
31108 72 6d 61 74 69 6f 6e 20 73 74 6f 72 65 64 20 69  rmation stored i
31109 6e 63 6c 75 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  ncludes:.**.**  
3110a 20 2b 20 20 54 68 65 20 73 74 61 74 65 20 6e 75   +  The state nu
3110b 6d 62 65 72 20 66 6f 72 20 74 68 65 20 70 61 72  mber for the par
3110c 73 65 72 20 61 74 20 74 68 69 73 20 6c 65 76 65  ser at this leve
3110d 6c 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 0a  l of the stack..
3110e 2a 2a 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20 76  **.**   +  The v
3110f 61 6c 75 65 20 6f 66 20 74 68 65 20 74 6f 6b 65  alue of the toke
31110 6e 20 73 74 6f 72 65 64 20 61 74 20 74 68 69 73  n stored at this
31111 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 73 74   level of the st
31112 61 63 6b 2e 0a 2a 2a 20 20 20 20 20 20 28 49 6e  ack..**      (In
31113 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68   other words, th
31114 65 20 22 6d 61 6a 6f 72 22 20 74 6f 6b 65 6e 2e  e "major" token.
31115 29 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 20 54 68 65  ).**.**   +  The
31116 20 73 65 6d 61 6e 74 69 63 20 76 61 6c 75 65 20   semantic value 
31117 73 74 6f 72 65 64 20 61 74 20 74 68 69 73 20 6c  stored at this l
31118 65 76 65 6c 20 6f 66 20 74 68 65 20 73 74 61 63  evel of the stac
31119 6b 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 20  k.  This is.**  
3111a 20 20 20 20 74 68 65 20 69 6e 66 6f 72 6d 61 74      the informat
3111b 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65 20  ion used by the 
3111c 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20  action routines 
3111d 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 2e 0a  in the grammar..
3111e 2a 2a 20 20 20 20 20 20 49 74 20 69 73 20 73 6f  **      It is so
3111f 6d 65 74 69 6d 65 73 20 63 61 6c 6c 65 64 20 74  metimes called t
31120 68 65 20 22 6d 69 6e 6f 72 22 20 74 6f 6b 65 6e  he "minor" token
31121 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 79 79 53 74  ..*/.struct yySt
31122 61 63 6b 45 6e 74 72 79 20 7b 0a 20 20 59 59 41  ackEntry {.  YYA
31123 43 54 49 4f 4e 54 59 50 45 20 73 74 61 74 65 6e  CTIONTYPE staten
31124 6f 3b 20 20 2f 2a 20 54 68 65 20 73 74 61 74 65  o;  /* The state
31125 2d 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 59 59 43  -number */.  YYC
31126 4f 44 45 54 59 50 45 20 6d 61 6a 6f 72 3b 20 20  ODETYPE major;  
31127 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72      /* The major
31128 20 74 6f 6b 65 6e 20 76 61 6c 75 65 2e 20 20 54   token value.  T
31129 68 69 73 20 69 73 20 74 68 65 20 63 6f 64 65 0a  his is the code.
3112a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3112b 20 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62           ** numb
3112c 65 72 20 66 6f 72 20 74 68 65 20 74 6f 6b 65 6e  er for the token
3112d 20 61 74 20 74 68 69 73 20 73 74 61 63 6b 20 6c   at this stack l
3112e 65 76 65 6c 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f  evel */.  YYMINO
3112f 52 54 59 50 45 20 6d 69 6e 6f 72 3b 20 20 20 20  RTYPE minor;    
31130 20 2f 2a 20 54 68 65 20 75 73 65 72 2d 73 75 70   /* The user-sup
31131 70 6c 69 65 64 20 6d 69 6e 6f 72 20 74 6f 6b 65  plied minor toke
31132 6e 20 76 61 6c 75 65 2e 20 20 54 68 69 73 0a 20  n value.  This. 
31133 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31134 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 74 68          ** is th
31135 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 74  e value of the t
31136 6f 6b 65 6e 20 20 2a 2f 0a 7d 3b 0a 74 79 70 65  oken  */.};.type
31137 64 65 66 20 73 74 72 75 63 74 20 79 79 53 74 61  def struct yySta
31138 63 6b 45 6e 74 72 79 20 79 79 53 74 61 63 6b 45  ckEntry yyStackE
31139 6e 74 72 79 3b 0a 0a 2f 2a 20 54 68 65 20 73 74  ntry;../* The st
3113a 61 74 65 20 6f 66 20 74 68 65 20 70 61 72 73 65  ate of the parse
3113b 72 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20  r is completely 
3113c 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 61 6e 20  contained in an 
3113d 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74  instance of.** t
3113e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
3113f 75 63 74 75 72 65 20 2a 2f 0a 73 74 72 75 63 74  ucture */.struct
31140 20 79 79 50 61 72 73 65 72 20 7b 0a 20 20 69 6e   yyParser {.  in
31141 74 20 79 79 69 64 78 3b 20 20 20 20 20 20 20 20  t yyidx;        
31142 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
31143 6e 64 65 78 20 6f 66 20 74 6f 70 20 65 6c 65 6d  ndex of top elem
31144 65 6e 74 20 69 6e 20 73 74 61 63 6b 20 2a 2f 0a  ent in stack */.
31145 23 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41  #ifdef YYTRACKMA
31146 58 53 54 41 43 4b 44 45 50 54 48 0a 20 20 69 6e  XSTACKDEPTH.  in
31147 74 20 79 79 69 64 78 4d 61 78 3b 20 20 20 20 20  t yyidxMax;     
31148 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
31149 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20  aximum value of 
3114a 79 79 69 64 78 20 2a 2f 0a 23 65 6e 64 69 66 0a  yyidx */.#endif.
3114b 20 20 69 6e 74 20 79 79 65 72 72 63 6e 74 3b 20    int yyerrcnt; 
3114c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3114d 2f 2a 20 53 68 69 66 74 73 20 6c 65 66 74 20 62  /* Shifts left b
3114e 65 66 6f 72 65 20 6f 75 74 20 6f 66 20 74 68 65  efore out of the
3114f 20 65 72 72 6f 72 20 2a 2f 0a 20 20 73 71 6c 69   error */.  sqli
31150 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 44 45  te3ParserARG_SDE
31151 43 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CL              
31152 20 20 2f 2a 20 41 20 70 6c 61 63 65 20 74 6f 20    /* A place to 
31153 68 6f 6c 64 20 25 65 78 74 72 61 5f 61 72 67 75  hold %extra_argu
31154 6d 65 6e 74 20 2a 2f 0a 23 69 66 20 59 59 53 54  ment */.#if YYST
31155 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20 69 6e  ACKDEPTH<=0.  in
31156 74 20 79 79 73 74 6b 73 7a 3b 20 20 20 20 20 20  t yystksz;      
31157 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
31158 75 72 72 65 6e 74 20 73 69 64 65 20 6f 66 20 74  urrent side of t
31159 68 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20 79 79  he stack */.  yy
3115a 53 74 61 63 6b 45 6e 74 72 79 20 2a 79 79 73 74  StackEntry *yyst
3115b 61 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ack;        /* T
3115c 68 65 20 70 61 72 73 65 72 27 73 20 73 74 61 63  he parser's stac
3115d 6b 20 2a 2f 0a 23 65 6c 73 65 0a 20 20 79 79 53  k */.#else.  yyS
3115e 74 61 63 6b 45 6e 74 72 79 20 79 79 73 74 61 63  tackEntry yystac
3115f 6b 5b 59 59 53 54 41 43 4b 44 45 50 54 48 5d 3b  k[YYSTACKDEPTH];
31160 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 27    /* The parser'
31161 73 20 73 74 61 63 6b 20 2a 2f 0a 23 65 6e 64 69  s stack */.#endi
31162 66 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72  f.};.typedef str
31163 75 63 74 20 79 79 50 61 72 73 65 72 20 79 79 50  uct yyParser yyP
31164 61 72 73 65 72 3b 0a 0a 23 69 66 6e 64 65 66 20  arser;..#ifndef 
31165 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 46 49  NDEBUG.static FI
31166 4c 45 20 2a 79 79 54 72 61 63 65 46 49 4c 45 20  LE *yyTraceFILE 
31167 3d 20 30 3b 0a 73 74 61 74 69 63 20 63 68 61 72  = 0;.static char
31168 20 2a 79 79 54 72 61 63 65 50 72 6f 6d 70 74 20   *yyTracePrompt 
31169 3d 20 30 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 4e  = 0;.#endif /* N
3116a 44 45 42 55 47 20 2a 2f 0a 0a 23 69 66 6e 64 65  DEBUG */..#ifnde
3116b 66 20 4e 44 45 42 55 47 0a 2f 2a 20 0a 2a 2a 20  f NDEBUG./* .** 
3116c 54 75 72 6e 20 70 61 72 73 65 72 20 74 72 61 63  Turn parser trac
3116d 69 6e 67 20 6f 6e 20 62 79 20 67 69 76 69 6e 67  ing on by giving
3116e 20 61 20 73 74 72 65 61 6d 20 74 6f 20 77 68 69   a stream to whi
3116f 63 68 20 74 6f 20 77 72 69 74 65 20 74 68 65 20  ch to write the 
31170 74 72 61 63 65 0a 2a 2a 20 61 6e 64 20 61 20 70  trace.** and a p
31171 72 6f 6d 70 74 20 74 6f 20 70 72 65 66 61 63 65  rompt to preface
31172 20 65 61 63 68 20 74 72 61 63 65 20 6d 65 73 73   each trace mess
31173 61 67 65 2e 20 20 54 72 61 63 69 6e 67 20 69 73  age.  Tracing is
31174 20 74 75 72 6e 65 64 20 6f 66 66 0a 2a 2a 20 62   turned off.** b
31175 79 20 6d 61 6b 69 6e 67 20 65 69 74 68 65 72 20  y making either 
31176 61 72 67 75 6d 65 6e 74 20 4e 55 4c 4c 20 0a 2a  argument NULL .*
31177 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a 20  *.** Inputs:.** 
31178 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 41 20 46  <ul>.** <li> A F
31179 49 4c 45 2a 20 74 6f 20 77 68 69 63 68 20 74 72  ILE* to which tr
3117a 61 63 65 20 6f 75 74 70 75 74 20 73 68 6f 75 6c  ace output shoul
3117b 64 20 62 65 20 77 72 69 74 74 65 6e 2e 0a 2a 2a  d be written..**
3117c 20 20 20 20 20 20 49 66 20 4e 55 4c 4c 2c 20 74        If NULL, t
3117d 68 65 6e 20 74 72 61 63 69 6e 67 20 69 73 20 74  hen tracing is t
3117e 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2a 20 3c 6c  urned off..** <l
3117f 69 3e 20 41 20 70 72 65 66 69 78 20 73 74 72 69  i> A prefix stri
31180 6e 67 20 77 72 69 74 74 65 6e 20 61 74 20 74 68  ng written at th
31181 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65  e beginning of e
31182 76 65 72 79 0a 2a 2a 20 20 20 20 20 20 6c 69 6e  very.**      lin
31183 65 20 6f 66 20 74 72 61 63 65 20 6f 75 74 70 75  e of trace outpu
31184 74 2e 20 20 49 66 20 4e 55 4c 4c 2c 20 74 68 65  t.  If NULL, the
31185 6e 20 74 72 61 63 69 6e 67 20 69 73 0a 2a 2a 20  n tracing is.** 
31186 20 20 20 20 20 74 75 72 6e 65 64 20 6f 66 66 2e       turned off.
31187 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20  .** </ul>.**.** 
31188 4f 75 74 70 75 74 73 3a 0a 2a 2a 20 4e 6f 6e 65  Outputs:.** None
31189 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
3118a 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
3118b 50 61 72 73 65 72 54 72 61 63 65 28 46 49 4c 45  ParserTrace(FILE
3118c 20 2a 54 72 61 63 65 46 49 4c 45 2c 20 63 68 61   *TraceFILE, cha
3118d 72 20 2a 7a 54 72 61 63 65 50 72 6f 6d 70 74 29  r *zTracePrompt)
3118e 7b 0a 20 20 79 79 54 72 61 63 65 46 49 4c 45 20  {.  yyTraceFILE 
3118f 3d 20 54 72 61 63 65 46 49 4c 45 3b 0a 20 20 79  = TraceFILE;.  y
31190 79 54 72 61 63 65 50 72 6f 6d 70 74 20 3d 20 7a  yTracePrompt = z
31191 54 72 61 63 65 50 72 6f 6d 70 74 3b 0a 20 20 69  TracePrompt;.  i
31192 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 3d 3d  f( yyTraceFILE==
31193 30 20 29 20 79 79 54 72 61 63 65 50 72 6f 6d 70  0 ) yyTracePromp
31194 74 20 3d 20 30 3b 0a 20 20 65 6c 73 65 20 69 66  t = 0;.  else if
31195 28 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 3d  ( yyTracePrompt=
31196 3d 30 20 29 20 79 79 54 72 61 63 65 46 49 4c 45  =0 ) yyTraceFILE
31197 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f   = 0;.}.#endif /
31198 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 23 69 66  * NDEBUG */..#if
31199 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20 46  ndef NDEBUG./* F
3119a 6f 72 20 74 72 61 63 69 6e 67 20 73 68 69 66 74  or tracing shift
3119b 73 2c 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20  s, the names of 
3119c 61 6c 6c 20 74 65 72 6d 69 6e 61 6c 73 20 61 6e  all terminals an
3119d 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 73 0a 2a  d nonterminals.*
3119e 2a 20 61 72 65 20 72 65 71 75 69 72 65 64 2e 20  * are required. 
3119f 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   The following t
311a0 61 62 6c 65 20 73 75 70 70 6c 69 65 73 20 74 68  able supplies th
311a1 65 73 65 20 6e 61 6d 65 73 20 2a 2f 0a 73 74 61  ese names */.sta
311a2 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
311a3 63 6f 6e 73 74 20 79 79 54 6f 6b 65 6e 4e 61 6d  const yyTokenNam
311a4 65 5b 5d 20 3d 20 7b 20 0a 20 20 22 24 22 2c 20  e[] = { .  "$", 
311a5 20 20 20 20 20 20 20 20 20 20 20 20 22 53 45 4d              "SEM
311a6 49 22 2c 20 20 20 20 20 20 20 20 20 20 22 45 58  I",          "EX
311a7 50 4c 41 49 4e 22 2c 20 20 20 20 20 20 20 22 51  PLAIN",       "Q
311a8 55 45 52 59 22 2c 20 20 20 20 20 20 20 0a 20 20  UERY",       .  
311a9 22 50 4c 41 4e 22 2c 20 20 20 20 20 20 20 20 20  "PLAN",         
311aa 20 22 42 45 47 49 4e 22 2c 20 20 20 20 20 20 20   "BEGIN",       
311ab 20 20 22 54 52 41 4e 53 41 43 54 49 4f 4e 22 2c    "TRANSACTION",
311ac 20 20 20 22 44 45 46 45 52 52 45 44 22 2c 20 20     "DEFERRED",  
311ad 20 20 0a 20 20 22 49 4d 4d 45 44 49 41 54 45 22    .  "IMMEDIATE"
311ae 2c 20 20 20 20 20 22 45 58 43 4c 55 53 49 56 45  ,     "EXCLUSIVE
311af 22 2c 20 20 20 20 20 22 43 4f 4d 4d 49 54 22 2c  ",     "COMMIT",
311b0 20 20 20 20 20 20 20 20 22 45 4e 44 22 2c 20 20          "END",  
311b1 20 20 20 20 20 20 20 0a 20 20 22 52 4f 4c 4c 42         .  "ROLLB
311b2 41 43 4b 22 2c 20 20 20 20 20 20 22 53 41 56 45  ACK",      "SAVE
311b3 50 4f 49 4e 54 22 2c 20 20 20 20 20 22 52 45 4c  POINT",     "REL
311b4 45 41 53 45 22 2c 20 20 20 20 20 20 20 22 54 4f  EASE",       "TO
311b5 22 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20 22  ",          .  "
311b6 54 41 42 4c 45 22 2c 20 20 20 20 20 20 20 20 20  TABLE",         
311b7 22 43 52 45 41 54 45 22 2c 20 20 20 20 20 20 20  "CREATE",       
311b8 20 22 49 46 22 2c 20 20 20 20 20 20 20 20 20 20   "IF",          
311b9 20 20 22 4e 4f 54 22 2c 20 20 20 20 20 20 20 20    "NOT",        
311ba 20 0a 20 20 22 45 58 49 53 54 53 22 2c 20 20 20   .  "EXISTS",   
311bb 20 20 20 20 20 22 54 45 4d 50 22 2c 20 20 20 20       "TEMP",    
311bc 20 20 20 20 20 20 22 4c 50 22 2c 20 20 20 20 20        "LP",     
311bd 20 20 20 20 20 20 20 22 52 50 22 2c 20 20 20 20         "RP",    
311be 20 20 20 20 20 20 0a 20 20 22 41 53 22 2c 20 20        .  "AS",  
311bf 20 20 20 20 20 20 20 20 20 20 22 43 4f 4d 4d 41            "COMMA
311c0 22 2c 20 20 20 20 20 20 20 20 20 22 49 44 22 2c  ",         "ID",
311c1 20 20 20 20 20 20 20 20 20 20 20 20 22 49 4e 44              "IND
311c2 45 58 45 44 22 2c 20 20 20 20 20 0a 20 20 22 41  EXED",     .  "A
311c3 42 4f 52 54 22 2c 20 20 20 20 20 20 20 20 20 22  BORT",         "
311c4 41 43 54 49 4f 4e 22 2c 20 20 20 20 20 20 20 20  ACTION",        
311c5 22 41 46 54 45 52 22 2c 20 20 20 20 20 20 20 20  "AFTER",        
311c6 20 22 41 4e 41 4c 59 5a 45 22 2c 20 20 20 20 20   "ANALYZE",     
311c7 0a 20 20 22 41 53 43 22 2c 20 20 20 20 20 20 20  .  "ASC",       
311c8 20 20 20 20 22 41 54 54 41 43 48 22 2c 20 20 20      "ATTACH",   
311c9 20 20 20 20 20 22 42 45 46 4f 52 45 22 2c 20 20       "BEFORE",  
311ca 20 20 20 20 20 20 22 42 59 22 2c 20 20 20 20 20        "BY",     
311cb 20 20 20 20 20 0a 20 20 22 43 41 53 43 41 44 45       .  "CASCADE
311cc 22 2c 20 20 20 20 20 20 20 22 43 41 53 54 22 2c  ",       "CAST",
311cd 20 20 20 20 20 20 20 20 20 20 22 43 4f 4c 55 4d            "COLUM
311ce 4e 4b 57 22 2c 20 20 20 20 20 20 22 43 4f 4e 46  NKW",      "CONF
311cf 4c 49 43 54 22 2c 20 20 20 20 0a 20 20 22 44 41  LICT",    .  "DA
311d0 54 41 42 41 53 45 22 2c 20 20 20 20 20 20 22 44  TABASE",      "D
311d1 45 53 43 22 2c 20 20 20 20 20 20 20 20 20 20 22  ESC",          "
311d2 44 45 54 41 43 48 22 2c 20 20 20 20 20 20 20 20  DETACH",        
311d3 22 45 41 43 48 22 2c 20 20 20 20 20 20 20 20 0a  "EACH",        .
311d4 20 20 22 46 41 49 4c 22 2c 20 20 20 20 20 20 20    "FAIL",       
311d5 20 20 20 22 46 4f 52 22 2c 20 20 20 20 20 20 20     "FOR",       
311d6 20 20 20 20 22 49 47 4e 4f 52 45 22 2c 20 20 20      "IGNORE",   
311d7 20 20 20 20 20 22 49 4e 49 54 49 41 4c 4c 59 22       "INITIALLY"
311d8 2c 20 20 20 0a 20 20 22 49 4e 53 54 45 41 44 22  ,   .  "INSTEAD"
311d9 2c 20 20 20 20 20 20 20 22 4c 49 4b 45 5f 4b 57  ,       "LIKE_KW
311da 22 2c 20 20 20 20 20 20 20 22 4d 41 54 43 48 22  ",       "MATCH"
311db 2c 20 20 20 20 20 20 20 20 20 22 4e 4f 22 2c 20  ,         "NO", 
311dc 20 20 20 20 20 20 20 20 20 0a 20 20 22 4b 45 59           .  "KEY
311dd 22 2c 20 20 20 20 20 20 20 20 20 20 20 22 4f 46  ",           "OF
311de 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 4f  ",            "O
311df 46 46 53 45 54 22 2c 20 20 20 20 20 20 20 20 22  FFSET",        "
311e0 50 52 41 47 4d 41 22 2c 20 20 20 20 20 20 0a 20  PRAGMA",      . 
311e1 20 22 52 41 49 53 45 22 2c 20 20 20 20 20 20 20   "RAISE",       
311e2 20 20 22 52 45 50 4c 41 43 45 22 2c 20 20 20 20    "REPLACE",    
311e3 20 20 20 22 52 45 53 54 52 49 43 54 22 2c 20 20     "RESTRICT",  
311e4 20 20 20 20 22 52 4f 57 22 2c 20 20 20 20 20 20      "ROW",      
311e5 20 20 20 0a 20 20 22 54 52 49 47 47 45 52 22 2c     .  "TRIGGER",
311e6 20 20 20 20 20 20 20 22 56 41 43 55 55 4d 22 2c         "VACUUM",
311e7 20 20 20 20 20 20 20 20 22 56 49 45 57 22 2c 20          "VIEW", 
311e8 20 20 20 20 20 20 20 20 20 22 56 49 52 54 55 41           "VIRTUA
311e9 4c 22 2c 20 20 20 20 20 0a 20 20 22 52 45 49 4e  L",     .  "REIN
311ea 44 45 58 22 2c 20 20 20 20 20 20 20 22 52 45 4e  DEX",       "REN
311eb 41 4d 45 22 2c 20 20 20 20 20 20 20 20 22 43 54  AME",        "CT
311ec 49 4d 45 5f 4b 57 22 2c 20 20 20 20 20 20 22 41  IME_KW",      "A
311ed 4e 59 22 2c 20 20 20 20 20 20 20 20 20 0a 20 20  NY",         .  
311ee 22 4f 52 22 2c 20 20 20 20 20 20 20 20 20 20 20  "OR",           
311ef 20 22 41 4e 44 22 2c 20 20 20 20 20 20 20 20 20   "AND",         
311f0 20 20 22 49 53 22 2c 20 20 20 20 20 20 20 20 20    "IS",         
311f1 20 20 20 22 42 45 54 57 45 45 4e 22 2c 20 20 20     "BETWEEN",   
311f2 20 20 0a 20 20 22 49 4e 22 2c 20 20 20 20 20 20    .  "IN",      
311f3 20 20 20 20 20 20 22 49 53 4e 55 4c 4c 22 2c 20        "ISNULL", 
311f4 20 20 20 20 20 20 20 22 4e 4f 54 4e 55 4c 4c 22         "NOTNULL"
311f5 2c 20 20 20 20 20 20 20 22 4e 45 22 2c 20 20 20  ,       "NE",   
311f6 20 20 20 20 20 20 20 0a 20 20 22 45 51 22 2c 20         .  "EQ", 
311f7 20 20 20 20 20 20 20 20 20 20 20 22 47 54 22 2c             "GT",
311f8 20 20 20 20 20 20 20 20 20 20 20 20 22 4c 45 22              "LE"
311f9 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 4c 54  ,            "LT
311fa 22 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20 22  ",          .  "
311fb 47 45 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  GE",            
311fc 22 45 53 43 41 50 45 22 2c 20 20 20 20 20 20 20  "ESCAPE",       
311fd 20 22 42 49 54 41 4e 44 22 2c 20 20 20 20 20 20   "BITAND",      
311fe 20 20 22 42 49 54 4f 52 22 2c 20 20 20 20 20 20    "BITOR",      
311ff 20 0a 20 20 22 4c 53 48 49 46 54 22 2c 20 20 20   .  "LSHIFT",   
31200 20 20 20 20 20 22 52 53 48 49 46 54 22 2c 20 20       "RSHIFT",  
31201 20 20 20 20 20 20 22 50 4c 55 53 22 2c 20 20 20        "PLUS",   
31202 20 20 20 20 20 20 20 22 4d 49 4e 55 53 22 2c 20         "MINUS", 
31203 20 20 20 20 20 20 0a 20 20 22 53 54 41 52 22 2c        .  "STAR",
31204 20 20 20 20 20 20 20 20 20 20 22 53 4c 41 53 48            "SLASH
31205 22 2c 20 20 20 20 20 20 20 20 20 22 52 45 4d 22  ",         "REM"
31206 2c 20 20 20 20 20 20 20 20 20 20 20 22 43 4f 4e  ,           "CON
31207 43 41 54 22 2c 20 20 20 20 20 20 0a 20 20 22 43  CAT",      .  "C
31208 4f 4c 4c 41 54 45 22 2c 20 20 20 20 20 20 20 22  OLLATE",       "
31209 42 49 54 4e 4f 54 22 2c 20 20 20 20 20 20 20 20  BITNOT",        
3120a 22 53 54 52 49 4e 47 22 2c 20 20 20 20 20 20 20  "STRING",       
3120b 20 22 4a 4f 49 4e 5f 4b 57 22 2c 20 20 20 20 20   "JOIN_KW",     
3120c 0a 20 20 22 43 4f 4e 53 54 52 41 49 4e 54 22 2c  .  "CONSTRAINT",
3120d 20 20 20 20 22 44 45 46 41 55 4c 54 22 2c 20 20      "DEFAULT",  
3120e 20 20 20 20 20 22 4e 55 4c 4c 22 2c 20 20 20 20       "NULL",    
3120f 20 20 20 20 20 20 22 50 52 49 4d 41 52 59 22 2c        "PRIMARY",
31210 20 20 20 20 20 0a 20 20 22 55 4e 49 51 55 45 22       .  "UNIQUE"
31211 2c 20 20 20 20 20 20 20 20 22 43 48 45 43 4b 22  ,        "CHECK"
31212 2c 20 20 20 20 20 20 20 20 20 22 52 45 46 45 52  ,         "REFER
31213 45 4e 43 45 53 22 2c 20 20 20 20 22 41 55 54 4f  ENCES",    "AUTO
31214 49 4e 43 52 22 2c 20 20 20 20 0a 20 20 22 4f 4e  INCR",    .  "ON
31215 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 44  ",            "D
31216 45 4c 45 54 45 22 2c 20 20 20 20 20 20 20 20 22  ELETE",        "
31217 55 50 44 41 54 45 22 2c 20 20 20 20 20 20 20 20  UPDATE",        
31218 22 53 45 54 22 2c 20 20 20 20 20 20 20 20 20 0a  "SET",         .
31219 20 20 22 44 45 46 45 52 52 41 42 4c 45 22 2c 20    "DEFERRABLE", 
3121a 20 20 20 22 46 4f 52 45 49 47 4e 22 2c 20 20 20     "FOREIGN",   
3121b 20 20 20 20 22 44 52 4f 50 22 2c 20 20 20 20 20      "DROP",     
3121c 20 20 20 20 20 22 55 4e 49 4f 4e 22 2c 20 20 20       "UNION",   
3121d 20 20 20 20 0a 20 20 22 41 4c 4c 22 2c 20 20 20      .  "ALL",   
3121e 20 20 20 20 20 20 20 20 22 45 58 43 45 50 54 22          "EXCEPT"
3121f 2c 20 20 20 20 20 20 20 20 22 49 4e 54 45 52 53  ,        "INTERS
31220 45 43 54 22 2c 20 20 20 20 20 22 53 45 4c 45 43  ECT",     "SELEC
31221 54 22 2c 20 20 20 20 20 20 0a 20 20 22 44 49 53  T",      .  "DIS
31222 54 49 4e 43 54 22 2c 20 20 20 20 20 20 22 44 4f  TINCT",      "DO
31223 54 22 2c 20 20 20 20 20 20 20 20 20 20 20 22 46  T",           "F
31224 52 4f 4d 22 2c 20 20 20 20 20 20 20 20 20 20 22  ROM",          "
31225 4a 4f 49 4e 22 2c 20 20 20 20 20 20 20 20 0a 20  JOIN",        . 
31226 20 22 55 53 49 4e 47 22 2c 20 20 20 20 20 20 20   "USING",       
31227 20 20 22 4f 52 44 45 52 22 2c 20 20 20 20 20 20    "ORDER",      
31228 20 20 20 22 47 52 4f 55 50 22 2c 20 20 20 20 20     "GROUP",     
31229 20 20 20 20 22 48 41 56 49 4e 47 22 2c 20 20 20      "HAVING",   
3122a 20 20 20 0a 20 20 22 4c 49 4d 49 54 22 2c 20 20     .  "LIMIT",  
3122b 20 20 20 20 20 20 20 22 57 48 45 52 45 22 2c 20         "WHERE", 
3122c 20 20 20 20 20 20 20 20 22 49 4e 54 4f 22 2c 20          "INTO", 
3122d 20 20 20 20 20 20 20 20 20 22 56 41 4c 55 45 53           "VALUES
3122e 22 2c 20 20 20 20 20 20 0a 20 20 22 49 4e 53 45  ",      .  "INSE
3122f 52 54 22 2c 20 20 20 20 20 20 20 20 22 49 4e 54  RT",        "INT
31230 45 47 45 52 22 2c 20 20 20 20 20 20 20 22 46 4c  EGER",       "FL
31231 4f 41 54 22 2c 20 20 20 20 20 20 20 20 20 22 42  OAT",         "B
31232 4c 4f 42 22 2c 20 20 20 20 20 20 20 20 0a 20 20  LOB",        .  
31233 22 52 45 47 49 53 54 45 52 22 2c 20 20 20 20 20  "REGISTER",     
31234 20 22 56 41 52 49 41 42 4c 45 22 2c 20 20 20 20   "VARIABLE",    
31235 20 20 22 43 41 53 45 22 2c 20 20 20 20 20 20 20    "CASE",       
31236 20 20 20 22 57 48 45 4e 22 2c 20 20 20 20 20 20     "WHEN",      
31237 20 20 0a 20 20 22 54 48 45 4e 22 2c 20 20 20 20    .  "THEN",    
31238 20 20 20 20 20 20 22 45 4c 53 45 22 2c 20 20 20        "ELSE",   
31239 20 20 20 20 20 20 20 22 49 4e 44 45 58 22 2c 20         "INDEX", 
3123a 20 20 20 20 20 20 20 20 22 41 4c 54 45 52 22 2c          "ALTER",
3123b 20 20 20 20 20 20 20 0a 20 20 22 41 44 44 22 2c         .  "ADD",
3123c 20 20 20 20 20 20 20 20 20 20 20 22 65 72 72 6f             "erro
3123d 72 22 2c 20 20 20 20 20 20 20 20 20 22 69 6e 70  r",         "inp
3123e 75 74 22 2c 20 20 20 20 20 20 20 20 20 22 63 6d  ut",         "cm
3123f 64 6c 69 73 74 22 2c 20 20 20 20 20 0a 20 20 22  dlist",     .  "
31240 65 63 6d 64 22 2c 20 20 20 20 20 20 20 20 20 20  ecmd",          
31241 22 65 78 70 6c 61 69 6e 22 2c 20 20 20 20 20 20  "explain",      
31242 20 22 63 6d 64 78 22 2c 20 20 20 20 20 20 20 20   "cmdx",        
31243 20 20 22 63 6d 64 22 2c 20 20 20 20 20 20 20 20    "cmd",        
31244 20 0a 20 20 22 74 72 61 6e 73 74 79 70 65 22 2c   .  "transtype",
31245 20 20 20 20 20 22 74 72 61 6e 73 5f 6f 70 74 22       "trans_opt"
31246 2c 20 20 20 20 20 22 6e 6d 22 2c 20 20 20 20 20  ,     "nm",     
31247 20 20 20 20 20 20 20 22 73 61 76 65 70 6f 69 6e         "savepoin
31248 74 5f 6f 70 74 22 2c 0a 20 20 22 63 72 65 61 74  t_opt",.  "creat
31249 65 5f 74 61 62 6c 65 22 2c 20 20 22 63 72 65 61  e_table",  "crea
3124a 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 22 2c 20  te_table_args", 
3124b 20 22 63 72 65 61 74 65 6b 77 22 2c 20 20 20 20   "createkw",    
3124c 20 20 22 74 65 6d 70 22 2c 20 20 20 20 20 20 20    "temp",       
3124d 20 0a 20 20 22 69 66 6e 6f 74 65 78 69 73 74 73   .  "ifnotexists
3124e 22 2c 20 20 20 22 64 62 6e 6d 22 2c 20 20 20 20  ",   "dbnm",    
3124f 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 6c 69 73        "columnlis
31250 74 22 2c 20 20 20 20 22 63 6f 6e 73 6c 69 73 74  t",    "conslist
31251 5f 6f 70 74 22 2c 0a 20 20 22 73 65 6c 65 63 74  _opt",.  "select
31252 22 2c 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d  ",        "colum
31253 6e 22 2c 20 20 20 20 20 20 20 20 22 63 6f 6c 75  n",        "colu
31254 6d 6e 69 64 22 2c 20 20 20 20 20 20 22 74 79 70  mnid",      "typ
31255 65 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 63  e",        .  "c
31256 61 72 67 6c 69 73 74 22 2c 20 20 20 20 20 20 22  arglist",      "
31257 69 64 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  id",            
31258 22 69 64 73 22 2c 20 20 20 20 20 20 20 20 20 20  "ids",          
31259 20 22 74 79 70 65 74 6f 6b 65 6e 22 2c 20 20 20   "typetoken",   
3125a 0a 20 20 22 74 79 70 65 6e 61 6d 65 22 2c 20 20  .  "typename",  
3125b 20 20 20 20 22 73 69 67 6e 65 64 22 2c 20 20 20      "signed",   
3125c 20 20 20 20 20 22 70 6c 75 73 5f 6e 75 6d 22 2c       "plus_num",
3125d 20 20 20 20 20 20 22 6d 69 6e 75 73 5f 6e 75 6d        "minus_num
3125e 22 2c 20 20 20 0a 20 20 22 63 61 72 67 22 2c 20  ",   .  "carg", 
3125f 20 20 20 20 20 20 20 20 20 22 63 63 6f 6e 73 22           "ccons"
31260 2c 20 20 20 20 20 20 20 20 20 22 74 65 72 6d 22  ,         "term"
31261 2c 20 20 20 20 20 20 20 20 20 20 22 65 78 70 72  ,          "expr
31262 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 6f 6e  ",        .  "on
31263 63 6f 6e 66 22 2c 20 20 20 20 20 20 20 20 22 73  conf",        "s
31264 6f 72 74 6f 72 64 65 72 22 2c 20 20 20 20 20 22  ortorder",     "
31265 61 75 74 6f 69 6e 63 22 2c 20 20 20 20 20 20 20  autoinc",       
31266 22 69 64 78 6c 69 73 74 5f 6f 70 74 22 2c 20 0a  "idxlist_opt", .
31267 20 20 22 72 65 66 61 72 67 73 22 2c 20 20 20 20    "refargs",    
31268 20 20 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61     "defer_subcla
31269 75 73 65 22 2c 20 20 22 72 65 66 61 72 67 22 2c  use",  "refarg",
3126a 20 20 20 20 20 20 20 20 22 72 65 66 61 63 74 22          "refact"
3126b 2c 20 20 20 20 20 20 0a 20 20 22 69 6e 69 74 5f  ,      .  "init_
3126c 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70  deferred_pred_op
3126d 74 22 2c 20 20 22 63 6f 6e 73 6c 69 73 74 22 2c  t",  "conslist",
3126e 20 20 20 20 20 20 22 74 63 6f 6e 73 22 2c 20 20        "tcons",  
3126f 20 20 20 20 20 20 20 22 69 64 78 6c 69 73 74 22         "idxlist"
31270 2c 20 20 20 20 20 0a 20 20 22 64 65 66 65 72 5f  ,     .  "defer_
31271 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 22 2c 20  subclause_opt", 
31272 20 22 6f 72 63 6f 6e 66 22 2c 20 20 20 20 20 20   "orconf",      
31273 20 20 22 72 65 73 6f 6c 76 65 74 79 70 65 22 2c    "resolvetype",
31274 20 20 20 22 72 61 69 73 65 74 79 70 65 22 2c 20     "raisetype", 
31275 20 20 0a 20 20 22 69 66 65 78 69 73 74 73 22 2c    .  "ifexists",
31276 20 20 20 20 20 20 22 66 75 6c 6c 6e 61 6d 65 22        "fullname"
31277 2c 20 20 20 20 20 20 22 6f 6e 65 73 65 6c 65 63  ,      "oneselec
31278 74 22 2c 20 20 20 20 20 22 6d 75 6c 74 69 73 65  t",     "multise
31279 6c 65 63 74 5f 6f 70 22 2c 0a 20 20 22 64 69 73  lect_op",.  "dis
3127a 74 69 6e 63 74 22 2c 20 20 20 20 20 20 22 73 65  tinct",      "se
3127b 6c 63 6f 6c 6c 69 73 74 22 2c 20 20 20 20 22 66  lcollist",    "f
3127c 72 6f 6d 22 2c 20 20 20 20 20 20 20 20 20 20 22  rom",          "
3127d 77 68 65 72 65 5f 6f 70 74 22 2c 20 20 20 0a 20  where_opt",   . 
3127e 20 22 67 72 6f 75 70 62 79 5f 6f 70 74 22 2c 20   "groupby_opt", 
3127f 20 20 22 68 61 76 69 6e 67 5f 6f 70 74 22 2c 20    "having_opt", 
31280 20 20 20 22 6f 72 64 65 72 62 79 5f 6f 70 74 22     "orderby_opt"
31281 2c 20 20 20 22 6c 69 6d 69 74 5f 6f 70 74 22 2c  ,   "limit_opt",
31282 20 20 20 0a 20 20 22 73 63 6c 70 22 2c 20 20 20     .  "sclp",   
31283 20 20 20 20 20 20 20 22 61 73 22 2c 20 20 20 20         "as",    
31284 20 20 20 20 20 20 20 20 22 73 65 6c 74 61 62 6c          "seltabl
31285 69 73 74 22 2c 20 20 20 20 22 73 74 6c 5f 70 72  ist",    "stl_pr
31286 65 66 69 78 22 2c 20 20 0a 20 20 22 6a 6f 69 6e  efix",  .  "join
31287 6f 70 22 2c 20 20 20 20 20 20 20 20 22 69 6e 64  op",        "ind
31288 65 78 65 64 5f 6f 70 74 22 2c 20 20 20 22 6f 6e  exed_opt",   "on
31289 5f 6f 70 74 22 2c 20 20 20 20 20 20 20 20 22 75  _opt",        "u
3128a 73 69 6e 67 5f 6f 70 74 22 2c 20 20 20 0a 20 20  sing_opt",   .  
3128b 22 6a 6f 69 6e 6f 70 32 22 2c 20 20 20 20 20 20  "joinop2",      
3128c 20 22 69 6e 73 63 6f 6c 6c 69 73 74 22 2c 20 20   "inscollist",  
3128d 20 20 22 73 6f 72 74 6c 69 73 74 22 2c 20 20 20    "sortlist",   
3128e 20 20 20 22 73 6f 72 74 69 74 65 6d 22 2c 20 20     "sortitem",  
3128f 20 20 0a 20 20 22 6e 65 78 70 72 6c 69 73 74 22    .  "nexprlist"
31290 2c 20 20 20 20 20 22 73 65 74 6c 69 73 74 22 2c  ,     "setlist",
31291 20 20 20 20 20 20 20 22 69 6e 73 65 72 74 5f 63         "insert_c
31292 6d 64 22 2c 20 20 20 20 22 69 6e 73 63 6f 6c 6c  md",    "inscoll
31293 69 73 74 5f 6f 70 74 22 2c 0a 20 20 22 69 74 65  ist_opt",.  "ite
31294 6d 6c 69 73 74 22 2c 20 20 20 20 20 20 22 65 78  mlist",      "ex
31295 70 72 6c 69 73 74 22 2c 20 20 20 20 20 20 22 6c  prlist",      "l
31296 69 6b 65 6f 70 22 2c 20 20 20 20 20 20 20 20 22  ikeop",        "
31297 65 73 63 61 70 65 22 2c 20 20 20 20 20 20 0a 20  escape",      . 
31298 20 22 62 65 74 77 65 65 6e 5f 6f 70 22 2c 20 20   "between_op",  
31299 20 20 22 69 6e 5f 6f 70 22 2c 20 20 20 20 20 20    "in_op",      
3129a 20 20 20 22 63 61 73 65 5f 6f 70 65 72 61 6e 64     "case_operand
3129b 22 2c 20 20 22 63 61 73 65 5f 65 78 70 72 6c 69  ",  "case_exprli
3129c 73 74 22 2c 0a 20 20 22 63 61 73 65 5f 65 6c 73  st",.  "case_els
3129d 65 22 2c 20 20 20 20 20 22 75 6e 69 71 75 65 66  e",     "uniquef
3129e 6c 61 67 22 2c 20 20 20 20 22 63 6f 6c 6c 61 74  lag",    "collat
3129f 65 22 2c 20 20 20 20 20 20 20 22 6e 6d 6e 75 6d  e",       "nmnum
312a0 22 2c 20 20 20 20 20 20 20 0a 20 20 22 70 6c 75  ",       .  "plu
312a1 73 5f 6f 70 74 22 2c 20 20 20 20 20 20 22 6e 75  s_opt",      "nu
312a2 6d 62 65 72 22 2c 20 20 20 20 20 20 20 20 22 74  mber",        "t
312a3 72 69 67 67 65 72 5f 64 65 63 6c 22 2c 20 20 22  rigger_decl",  "
312a4 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74  trigger_cmd_list
312a5 22 2c 0a 20 20 22 74 72 69 67 67 65 72 5f 74 69  ",.  "trigger_ti
312a6 6d 65 22 2c 20 20 22 74 72 69 67 67 65 72 5f 65  me",  "trigger_e
312a7 76 65 6e 74 22 2c 20 20 22 66 6f 72 65 61 63 68  vent",  "foreach
312a8 5f 63 6c 61 75 73 65 22 2c 20 20 22 77 68 65 6e  _clause",  "when
312a9 5f 63 6c 61 75 73 65 22 2c 20 0a 20 20 22 74 72  _clause", .  "tr
312aa 69 67 67 65 72 5f 63 6d 64 22 2c 20 20 20 22 74  igger_cmd",   "t
312ab 72 6e 6d 22 2c 20 20 20 20 20 20 20 20 20 20 22  rnm",          "
312ac 74 72 69 64 78 62 79 22 2c 20 20 20 20 20 20 20  tridxby",       
312ad 22 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74  "database_kw_opt
312ae 22 2c 0a 20 20 22 6b 65 79 5f 6f 70 74 22 2c 20  ",.  "key_opt", 
312af 20 20 20 20 20 20 22 61 64 64 5f 63 6f 6c 75 6d        "add_colum
312b0 6e 5f 66 75 6c 6c 6e 61 6d 65 22 2c 20 20 22 6b  n_fullname",  "k
312b1 77 63 6f 6c 75 6d 6e 5f 6f 70 74 22 2c 20 20 22  wcolumn_opt",  "
312b2 63 72 65 61 74 65 5f 76 74 61 62 22 2c 20 0a 20  create_vtab", . 
312b3 20 22 76 74 61 62 61 72 67 6c 69 73 74 22 2c 20   "vtabarglist", 
312b4 20 20 22 76 74 61 62 61 72 67 22 2c 20 20 20 20    "vtabarg",    
312b5 20 20 20 22 76 74 61 62 61 72 67 74 6f 6b 65 6e     "vtabargtoken
312b6 22 2c 20 20 22 6c 70 22 2c 20 20 20 20 20 20 20  ",  "lp",       
312b7 20 20 20 0a 20 20 22 61 6e 79 6c 69 73 74 22 2c     .  "anylist",
312b8 20 20 20 20 20 0a 7d 3b 0a 23 65 6e 64 69 66 20       .};.#endif 
312b9 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 23 69  /* NDEBUG */..#i
312ba 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 20  fndef NDEBUG./* 
312bb 46 6f 72 20 74 72 61 63 69 6e 67 20 72 65 64 75  For tracing redu
312bc 63 65 20 61 63 74 69 6f 6e 73 2c 20 74 68 65 20  ce actions, the 
312bd 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20 72 75 6c  names of all rul
312be 65 73 20 61 72 65 20 72 65 71 75 69 72 65 64 2e  es are required.
312bf 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
312c0 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 79 79 52   char *const yyR
312c1 75 6c 65 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20 2f  uleName[] = {. /
312c2 2a 20 20 20 30 20 2a 2f 20 22 69 6e 70 75 74 20  *   0 */ "input 
312c3 3a 3a 3d 20 63 6d 64 6c 69 73 74 22 2c 0a 20 2f  ::= cmdlist",. /
312c4 2a 20 20 20 31 20 2a 2f 20 22 63 6d 64 6c 69 73  *   1 */ "cmdlis
312c5 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74 20 65 63  t ::= cmdlist ec
312c6 6d 64 22 2c 0a 20 2f 2a 20 20 20 32 20 2a 2f 20  md",. /*   2 */ 
312c7 22 63 6d 64 6c 69 73 74 20 3a 3a 3d 20 65 63 6d  "cmdlist ::= ecm
312c8 64 22 2c 0a 20 2f 2a 20 20 20 33 20 2a 2f 20 22  d",. /*   3 */ "
312c9 65 63 6d 64 20 3a 3a 3d 20 53 45 4d 49 22 2c 0a  ecmd ::= SEMI",.
312ca 20 2f 2a 20 20 20 34 20 2a 2f 20 22 65 63 6d 64   /*   4 */ "ecmd
312cb 20 3a 3a 3d 20 65 78 70 6c 61 69 6e 20 63 6d 64   ::= explain cmd
312cc 78 20 53 45 4d 49 22 2c 0a 20 2f 2a 20 20 20 35  x SEMI",. /*   5
312cd 20 2a 2f 20 22 65 78 70 6c 61 69 6e 20 3a 3a 3d   */ "explain ::=
312ce 22 2c 0a 20 2f 2a 20 20 20 36 20 2a 2f 20 22 65  ",. /*   6 */ "e
312cf 78 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41  xplain ::= EXPLA
312d0 49 4e 22 2c 0a 20 2f 2a 20 20 20 37 20 2a 2f 20  IN",. /*   7 */ 
312d1 22 65 78 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50  "explain ::= EXP
312d2 4c 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 22  LAIN QUERY PLAN"
312d3 2c 0a 20 2f 2a 20 20 20 38 20 2a 2f 20 22 63 6d  ,. /*   8 */ "cm
312d4 64 78 20 3a 3a 3d 20 63 6d 64 22 2c 0a 20 2f 2a  dx ::= cmd",. /*
312d5 20 20 20 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d     9 */ "cmd ::=
312d6 20 42 45 47 49 4e 20 74 72 61 6e 73 74 79 70 65   BEGIN transtype
312d7 20 74 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a   trans_opt",. /*
312d8 20 20 31 30 20 2a 2f 20 22 74 72 61 6e 73 5f 6f    10 */ "trans_o
312d9 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 31 31  pt ::=",. /*  11
312da 20 2a 2f 20 22 74 72 61 6e 73 5f 6f 70 74 20 3a   */ "trans_opt :
312db 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f 4e 22 2c  := TRANSACTION",
312dc 0a 20 2f 2a 20 20 31 32 20 2a 2f 20 22 74 72 61  . /*  12 */ "tra
312dd 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53  ns_opt ::= TRANS
312de 41 43 54 49 4f 4e 20 6e 6d 22 2c 0a 20 2f 2a 20  ACTION nm",. /* 
312df 20 31 33 20 2a 2f 20 22 74 72 61 6e 73 74 79 70   13 */ "transtyp
312e0 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 31 34 20  e ::=",. /*  14 
312e1 2a 2f 20 22 74 72 61 6e 73 74 79 70 65 20 3a 3a  */ "transtype ::
312e2 3d 20 44 45 46 45 52 52 45 44 22 2c 0a 20 2f 2a  = DEFERRED",. /*
312e3 20 20 31 35 20 2a 2f 20 22 74 72 61 6e 73 74 79    15 */ "transty
312e4 70 65 20 3a 3a 3d 20 49 4d 4d 45 44 49 41 54 45  pe ::= IMMEDIATE
312e5 22 2c 0a 20 2f 2a 20 20 31 36 20 2a 2f 20 22 74  ",. /*  16 */ "t
312e6 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 45 58 43  ranstype ::= EXC
312e7 4c 55 53 49 56 45 22 2c 0a 20 2f 2a 20 20 31 37  LUSIVE",. /*  17
312e8 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 43 4f 4d   */ "cmd ::= COM
312e9 4d 49 54 20 74 72 61 6e 73 5f 6f 70 74 22 2c 0a  MIT trans_opt",.
312ea 20 2f 2a 20 20 31 38 20 2a 2f 20 22 63 6d 64 20   /*  18 */ "cmd 
312eb 3a 3a 3d 20 45 4e 44 20 74 72 61 6e 73 5f 6f 70  ::= END trans_op
312ec 74 22 2c 0a 20 2f 2a 20 20 31 39 20 2a 2f 20 22  t",. /*  19 */ "
312ed 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b  cmd ::= ROLLBACK
312ee 20 74 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a   trans_opt",. /*
312ef 20 20 32 30 20 2a 2f 20 22 73 61 76 65 70 6f 69    20 */ "savepoi
312f0 6e 74 5f 6f 70 74 20 3a 3a 3d 20 53 41 56 45 50  nt_opt ::= SAVEP
312f1 4f 49 4e 54 22 2c 0a 20 2f 2a 20 20 32 31 20 2a  OINT",. /*  21 *
312f2 2f 20 22 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74  / "savepoint_opt
312f3 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 32 32 20 2a   ::=",. /*  22 *
312f4 2f 20 22 63 6d 64 20 3a 3a 3d 20 53 41 56 45 50  / "cmd ::= SAVEP
312f5 4f 49 4e 54 20 6e 6d 22 2c 0a 20 2f 2a 20 20 32  OINT nm",. /*  2
312f6 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 45  3 */ "cmd ::= RE
312f7 4c 45 41 53 45 20 73 61 76 65 70 6f 69 6e 74 5f  LEASE savepoint_
312f8 6f 70 74 20 6e 6d 22 2c 0a 20 2f 2a 20 20 32 34  opt nm",. /*  24
312f9 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 4f 4c   */ "cmd ::= ROL
312fa 4c 42 41 43 4b 20 74 72 61 6e 73 5f 6f 70 74 20  LBACK trans_opt 
312fb 54 4f 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74  TO savepoint_opt
312fc 20 6e 6d 22 2c 0a 20 2f 2a 20 20 32 35 20 2a 2f   nm",. /*  25 */
312fd 20 22 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65   "cmd ::= create
312fe 5f 74 61 62 6c 65 20 63 72 65 61 74 65 5f 74 61  _table create_ta
312ff 62 6c 65 5f 61 72 67 73 22 2c 0a 20 2f 2a 20 20  ble_args",. /*  
31300 32 36 20 2a 2f 20 22 63 72 65 61 74 65 5f 74 61  26 */ "create_ta
31301 62 6c 65 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77  ble ::= createkw
31302 20 74 65 6d 70 20 54 41 42 4c 45 20 69 66 6e 6f   temp TABLE ifno
31303 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 22  texists nm dbnm"
31304 2c 0a 20 2f 2a 20 20 32 37 20 2a 2f 20 22 63 72  ,. /*  27 */ "cr
31305 65 61 74 65 6b 77 20 3a 3a 3d 20 43 52 45 41 54  eatekw ::= CREAT
31306 45 22 2c 0a 20 2f 2a 20 20 32 38 20 2a 2f 20 22  E",. /*  28 */ "
31307 69 66 6e 6f 74 65 78 69 73 74 73 20 3a 3a 3d 22  ifnotexists ::="
31308 2c 0a 20 2f 2a 20 20 32 39 20 2a 2f 20 22 69 66  ,. /*  29 */ "if
31309 6e 6f 74 65 78 69 73 74 73 20 3a 3a 3d 20 49 46  notexists ::= IF
3130a 20 4e 4f 54 20 45 58 49 53 54 53 22 2c 0a 20 2f   NOT EXISTS",. /
3130b 2a 20 20 33 30 20 2a 2f 20 22 74 65 6d 70 20 3a  *  30 */ "temp :
3130c 3a 3d 20 54 45 4d 50 22 2c 0a 20 2f 2a 20 20 33  := TEMP",. /*  3
3130d 31 20 2a 2f 20 22 74 65 6d 70 20 3a 3a 3d 22 2c  1 */ "temp ::=",
3130e 0a 20 2f 2a 20 20 33 32 20 2a 2f 20 22 63 72 65  . /*  32 */ "cre
3130f 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 20 3a  ate_table_args :
31310 3a 3d 20 4c 50 20 63 6f 6c 75 6d 6e 6c 69 73 74  := LP columnlist
31311 20 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 52 50   conslist_opt RP
31312 22 2c 0a 20 2f 2a 20 20 33 33 20 2a 2f 20 22 63  ",. /*  33 */ "c
31313 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73  reate_table_args
31314 20 3a 3a 3d 20 41 53 20 73 65 6c 65 63 74 22 2c   ::= AS select",
31315 0a 20 2f 2a 20 20 33 34 20 2a 2f 20 22 63 6f 6c  . /*  34 */ "col
31316 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75  umnlist ::= colu
31317 6d 6e 6c 69 73 74 20 43 4f 4d 4d 41 20 63 6f 6c  mnlist COMMA col
31318 75 6d 6e 22 2c 0a 20 2f 2a 20 20 33 35 20 2a 2f  umn",. /*  35 */
31319 20 22 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d   "columnlist ::=
3131a 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a 20 20 33   column",. /*  3
3131b 36 20 2a 2f 20 22 63 6f 6c 75 6d 6e 20 3a 3a 3d  6 */ "column ::=
3131c 20 63 6f 6c 75 6d 6e 69 64 20 74 79 70 65 20 63   columnid type c
3131d 61 72 67 6c 69 73 74 22 2c 0a 20 2f 2a 20 20 33  arglist",. /*  3
3131e 37 20 2a 2f 20 22 63 6f 6c 75 6d 6e 69 64 20 3a  7 */ "columnid :
3131f 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a 20 20 33 38 20  := nm",. /*  38 
31320 2a 2f 20 22 69 64 20 3a 3a 3d 20 49 44 22 2c 0a  */ "id ::= ID",.
31321 20 2f 2a 20 20 33 39 20 2a 2f 20 22 69 64 20 3a   /*  39 */ "id :
31322 3a 3d 20 49 4e 44 45 58 45 44 22 2c 0a 20 2f 2a  := INDEXED",. /*
31323 20 20 34 30 20 2a 2f 20 22 69 64 73 20 3a 3a 3d    40 */ "ids ::=
31324 20 49 44 7c 53 54 52 49 4e 47 22 2c 0a 20 2f 2a   ID|STRING",. /*
31325 20 20 34 31 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20    41 */ "nm ::= 
31326 69 64 22 2c 0a 20 2f 2a 20 20 34 32 20 2a 2f 20  id",. /*  42 */ 
31327 22 6e 6d 20 3a 3a 3d 20 53 54 52 49 4e 47 22 2c  "nm ::= STRING",
31328 0a 20 2f 2a 20 20 34 33 20 2a 2f 20 22 6e 6d 20  . /*  43 */ "nm 
31329 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 22 2c 0a 20 2f  ::= JOIN_KW",. /
3132a 2a 20 20 34 34 20 2a 2f 20 22 74 79 70 65 20 3a  *  44 */ "type :
3132b 3a 3d 22 2c 0a 20 2f 2a 20 20 34 35 20 2a 2f 20  :=",. /*  45 */ 
3132c 22 74 79 70 65 20 3a 3a 3d 20 74 79 70 65 74 6f  "type ::= typeto
3132d 6b 65 6e 22 2c 0a 20 2f 2a 20 20 34 36 20 2a 2f  ken",. /*  46 */
3132e 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20   "typetoken ::= 
3132f 74 79 70 65 6e 61 6d 65 22 2c 0a 20 2f 2a 20 20  typename",. /*  
31330 34 37 20 2a 2f 20 22 74 79 70 65 74 6f 6b 65 6e  47 */ "typetoken
31331 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20 4c 50   ::= typename LP
31332 20 73 69 67 6e 65 64 20 52 50 22 2c 0a 20 2f 2a   signed RP",. /*
31333 20 20 34 38 20 2a 2f 20 22 74 79 70 65 74 6f 6b    48 */ "typetok
31334 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20  en ::= typename 
31335 4c 50 20 73 69 67 6e 65 64 20 43 4f 4d 4d 41 20  LP signed COMMA 
31336 73 69 67 6e 65 64 20 52 50 22 2c 0a 20 2f 2a 20  signed RP",. /* 
31337 20 34 39 20 2a 2f 20 22 74 79 70 65 6e 61 6d 65   49 */ "typename
31338 20 3a 3a 3d 20 69 64 73 22 2c 0a 20 2f 2a 20 20   ::= ids",. /*  
31339 35 30 20 2a 2f 20 22 74 79 70 65 6e 61 6d 65 20  50 */ "typename 
3133a 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20 69 64 73  ::= typename ids
3133b 22 2c 0a 20 2f 2a 20 20 35 31 20 2a 2f 20 22 73  ",. /*  51 */ "s
3133c 69 67 6e 65 64 20 3a 3a 3d 20 70 6c 75 73 5f 6e  igned ::= plus_n
3133d 75 6d 22 2c 0a 20 2f 2a 20 20 35 32 20 2a 2f 20  um",. /*  52 */ 
3133e 22 73 69 67 6e 65 64 20 3a 3a 3d 20 6d 69 6e 75  "signed ::= minu
3133f 73 5f 6e 75 6d 22 2c 0a 20 2f 2a 20 20 35 33 20  s_num",. /*  53 
31340 2a 2f 20 22 63 61 72 67 6c 69 73 74 20 3a 3a 3d  */ "carglist ::=
31341 20 63 61 72 67 6c 69 73 74 20 63 61 72 67 22 2c   carglist carg",
31342 0a 20 2f 2a 20 20 35 34 20 2a 2f 20 22 63 61 72  . /*  54 */ "car
31343 67 6c 69 73 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  glist ::=",. /* 
31344 20 35 35 20 2a 2f 20 22 63 61 72 67 20 3a 3a 3d   55 */ "carg ::=
31345 20 43 4f 4e 53 54 52 41 49 4e 54 20 6e 6d 20 63   CONSTRAINT nm c
31346 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20 35 36 20 2a  cons",. /*  56 *
31347 2f 20 22 63 61 72 67 20 3a 3a 3d 20 63 63 6f 6e  / "carg ::= ccon
31348 73 22 2c 0a 20 2f 2a 20 20 35 37 20 2a 2f 20 22  s",. /*  57 */ "
31349 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c  ccons ::= DEFAUL
3134a 54 20 74 65 72 6d 22 2c 0a 20 2f 2a 20 20 35 38  T term",. /*  58
3134b 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44   */ "ccons ::= D
3134c 45 46 41 55 4c 54 20 4c 50 20 65 78 70 72 20 52  EFAULT LP expr R
3134d 50 22 2c 0a 20 2f 2a 20 20 35 39 20 2a 2f 20 22  P",. /*  59 */ "
3134e 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c  ccons ::= DEFAUL
3134f 54 20 50 4c 55 53 20 74 65 72 6d 22 2c 0a 20 2f  T PLUS term",. /
31350 2a 20 20 36 30 20 2a 2f 20 22 63 63 6f 6e 73 20  *  60 */ "ccons 
31351 3a 3a 3d 20 44 45 46 41 55 4c 54 20 4d 49 4e 55  ::= DEFAULT MINU
31352 53 20 74 65 72 6d 22 2c 0a 20 2f 2a 20 20 36 31  S term",. /*  61
31353 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44   */ "ccons ::= D
31354 45 46 41 55 4c 54 20 69 64 22 2c 0a 20 2f 2a 20  EFAULT id",. /* 
31355 20 36 32 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   62 */ "ccons ::
31356 3d 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 22 2c 0a  = NULL onconf",.
31357 20 2f 2a 20 20 36 33 20 2a 2f 20 22 63 63 6f 6e   /*  63 */ "ccon
31358 73 20 3a 3a 3d 20 4e 4f 54 20 4e 55 4c 4c 20 6f  s ::= NOT NULL o
31359 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 36 34 20  nconf",. /*  64 
3135a 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 50 52  */ "ccons ::= PR
3135b 49 4d 41 52 59 20 4b 45 59 20 73 6f 72 74 6f 72  IMARY KEY sortor
3135c 64 65 72 20 6f 6e 63 6f 6e 66 20 61 75 74 6f 69  der onconf autoi
3135d 6e 63 22 2c 0a 20 2f 2a 20 20 36 35 20 2a 2f 20  nc",. /*  65 */ 
3135e 22 63 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55  "ccons ::= UNIQU
3135f 45 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20  E onconf",. /*  
31360 36 36 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d  66 */ "ccons ::=
31361 20 43 48 45 43 4b 20 4c 50 20 65 78 70 72 20 52   CHECK LP expr R
31362 50 22 2c 0a 20 2f 2a 20 20 36 37 20 2a 2f 20 22  P",. /*  67 */ "
31363 63 63 6f 6e 73 20 3a 3a 3d 20 52 45 46 45 52 45  ccons ::= REFERE
31364 4e 43 45 53 20 6e 6d 20 69 64 78 6c 69 73 74 5f  NCES nm idxlist_
31365 6f 70 74 20 72 65 66 61 72 67 73 22 2c 0a 20 2f  opt refargs",. /
31366 2a 20 20 36 38 20 2a 2f 20 22 63 63 6f 6e 73 20  *  68 */ "ccons 
31367 3a 3a 3d 20 64 65 66 65 72 5f 73 75 62 63 6c 61  ::= defer_subcla
31368 75 73 65 22 2c 0a 20 2f 2a 20 20 36 39 20 2a 2f  use",. /*  69 */
31369 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4c 4c   "ccons ::= COLL
3136a 41 54 45 20 69 64 73 22 2c 0a 20 2f 2a 20 20 37  ATE ids",. /*  7
3136b 30 20 2a 2f 20 22 61 75 74 6f 69 6e 63 20 3a 3a  0 */ "autoinc ::
3136c 3d 22 2c 0a 20 2f 2a 20 20 37 31 20 2a 2f 20 22  =",. /*  71 */ "
3136d 61 75 74 6f 69 6e 63 20 3a 3a 3d 20 41 55 54 4f  autoinc ::= AUTO
3136e 49 4e 43 52 22 2c 0a 20 2f 2a 20 20 37 32 20 2a  INCR",. /*  72 *
3136f 2f 20 22 72 65 66 61 72 67 73 20 3a 3a 3d 22 2c  / "refargs ::=",
31370 0a 20 2f 2a 20 20 37 33 20 2a 2f 20 22 72 65 66  . /*  73 */ "ref
31371 61 72 67 73 20 3a 3a 3d 20 72 65 66 61 72 67 73  args ::= refargs
31372 20 72 65 66 61 72 67 22 2c 0a 20 2f 2a 20 20 37   refarg",. /*  7
31373 34 20 2a 2f 20 22 72 65 66 61 72 67 20 3a 3a 3d  4 */ "refarg ::=
31374 20 4d 41 54 43 48 20 6e 6d 22 2c 0a 20 2f 2a 20   MATCH nm",. /* 
31375 20 37 35 20 2a 2f 20 22 72 65 66 61 72 67 20 3a   75 */ "refarg :
31376 3a 3d 20 4f 4e 20 44 45 4c 45 54 45 20 72 65 66  := ON DELETE ref
31377 61 63 74 22 2c 0a 20 2f 2a 20 20 37 36 20 2a 2f  act",. /*  76 */
31378 20 22 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20   "refarg ::= ON 
31379 55 50 44 41 54 45 20 72 65 66 61 63 74 22 2c 0a  UPDATE refact",.
3137a 20 2f 2a 20 20 37 37 20 2a 2f 20 22 72 65 66 61   /*  77 */ "refa
3137b 63 74 20 3a 3a 3d 20 53 45 54 20 4e 55 4c 4c 22  ct ::= SET NULL"
3137c 2c 0a 20 2f 2a 20 20 37 38 20 2a 2f 20 22 72 65  ,. /*  78 */ "re
3137d 66 61 63 74 20 3a 3a 3d 20 53 45 54 20 44 45 46  fact ::= SET DEF
3137e 41 55 4c 54 22 2c 0a 20 2f 2a 20 20 37 39 20 2a  AULT",. /*  79 *
3137f 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 43 41  / "refact ::= CA
31380 53 43 41 44 45 22 2c 0a 20 2f 2a 20 20 38 30 20  SCADE",. /*  80 
31381 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 52  */ "refact ::= R
31382 45 53 54 52 49 43 54 22 2c 0a 20 2f 2a 20 20 38  ESTRICT",. /*  8
31383 31 20 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d  1 */ "refact ::=
31384 20 4e 4f 20 41 43 54 49 4f 4e 22 2c 0a 20 2f 2a   NO ACTION",. /*
31385 20 20 38 32 20 2a 2f 20 22 64 65 66 65 72 5f 73    82 */ "defer_s
31386 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20 4e 4f 54  ubclause ::= NOT
31387 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e 69 74   DEFERRABLE init
31388 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f  _deferred_pred_o
31389 70 74 22 2c 0a 20 2f 2a 20 20 38 33 20 2a 2f 20  pt",. /*  83 */ 
3138a 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65  "defer_subclause
3138b 20 3a 3a 3d 20 44 45 46 45 52 52 41 42 4c 45 20   ::= DEFERRABLE 
3138c 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72  init_deferred_pr
3138d 65 64 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 38 34  ed_opt",. /*  84
3138e 20 2a 2f 20 22 69 6e 69 74 5f 64 65 66 65 72 72   */ "init_deferr
3138f 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 22  ed_pred_opt ::="
31390 2c 0a 20 2f 2a 20 20 38 35 20 2a 2f 20 22 69 6e  ,. /*  85 */ "in
31391 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64  it_deferred_pred
31392 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c  _opt ::= INITIAL
31393 4c 59 20 44 45 46 45 52 52 45 44 22 2c 0a 20 2f  LY DEFERRED",. /
31394 2a 20 20 38 36 20 2a 2f 20 22 69 6e 69 74 5f 64  *  86 */ "init_d
31395 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74  eferred_pred_opt
31396 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20 49   ::= INITIALLY I
31397 4d 4d 45 44 49 41 54 45 22 2c 0a 20 2f 2a 20 20  MMEDIATE",. /*  
31398 38 37 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 5f  87 */ "conslist_
31399 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 38  opt ::=",. /*  8
3139a 38 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 5f 6f  8 */ "conslist_o
3139b 70 74 20 3a 3a 3d 20 43 4f 4d 4d 41 20 63 6f 6e  pt ::= COMMA con
3139c 73 6c 69 73 74 22 2c 0a 20 2f 2a 20 20 38 39 20  slist",. /*  89 
3139d 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d  */ "conslist ::=
3139e 20 63 6f 6e 73 6c 69 73 74 20 43 4f 4d 4d 41 20   conslist COMMA 
3139f 74 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20 39 30 20  tcons",. /*  90 
313a0 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d  */ "conslist ::=
313a1 20 63 6f 6e 73 6c 69 73 74 20 74 63 6f 6e 73 22   conslist tcons"
313a2 2c 0a 20 2f 2a 20 20 39 31 20 2a 2f 20 22 63 6f  ,. /*  91 */ "co
313a3 6e 73 6c 69 73 74 20 3a 3a 3d 20 74 63 6f 6e 73  nslist ::= tcons
313a4 22 2c 0a 20 2f 2a 20 20 39 32 20 2a 2f 20 22 74  ",. /*  92 */ "t
313a5 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4e 53 54 52 41  cons ::= CONSTRA
313a6 49 4e 54 20 6e 6d 22 2c 0a 20 2f 2a 20 20 39 33  INT nm",. /*  93
313a7 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 50   */ "tcons ::= P
313a8 52 49 4d 41 52 59 20 4b 45 59 20 4c 50 20 69 64  RIMARY KEY LP id
313a9 78 6c 69 73 74 20 61 75 74 6f 69 6e 63 20 52 50  xlist autoinc RP
313aa 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 39   onconf",. /*  9
313ab 34 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20  4 */ "tcons ::= 
313ac 55 4e 49 51 55 45 20 4c 50 20 69 64 78 6c 69 73  UNIQUE LP idxlis
313ad 74 20 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f  t RP onconf",. /
313ae 2a 20 20 39 35 20 2a 2f 20 22 74 63 6f 6e 73 20  *  95 */ "tcons 
313af 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20 65 78 70  ::= CHECK LP exp
313b0 72 20 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f  r RP onconf",. /
313b1 2a 20 20 39 36 20 2a 2f 20 22 74 63 6f 6e 73 20  *  96 */ "tcons 
313b2 3a 3a 3d 20 46 4f 52 45 49 47 4e 20 4b 45 59 20  ::= FOREIGN KEY 
313b3 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20 52 45  LP idxlist RP RE
313b4 46 45 52 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c  FERENCES nm idxl
313b5 69 73 74 5f 6f 70 74 20 72 65 66 61 72 67 73 20  ist_opt refargs 
313b6 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f  defer_subclause_
313b7 6f 70 74 22 2c 0a 20 2f 2a 20 20 39 37 20 2a 2f  opt",. /*  97 */
313b8 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73   "defer_subclaus
313b9 65 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  e_opt ::=",. /* 
313ba 20 39 38 20 2a 2f 20 22 64 65 66 65 72 5f 73 75   98 */ "defer_su
313bb 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a 3d 20  bclause_opt ::= 
313bc 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 22  defer_subclause"
313bd 2c 0a 20 2f 2a 20 20 39 39 20 2a 2f 20 22 6f 6e  ,. /*  99 */ "on
313be 63 6f 6e 66 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  conf ::=",. /* 1
313bf 30 30 20 2a 2f 20 22 6f 6e 63 6f 6e 66 20 3a 3a  00 */ "onconf ::
313c0 3d 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 72 65  = ON CONFLICT re
313c1 73 6f 6c 76 65 74 79 70 65 22 2c 0a 20 2f 2a 20  solvetype",. /* 
313c2 31 30 31 20 2a 2f 20 22 6f 72 63 6f 6e 66 20 3a  101 */ "orconf :
313c3 3a 3d 22 2c 0a 20 2f 2a 20 31 30 32 20 2a 2f 20  :=",. /* 102 */ 
313c4 22 6f 72 63 6f 6e 66 20 3a 3a 3d 20 4f 52 20 72  "orconf ::= OR r
313c5 65 73 6f 6c 76 65 74 79 70 65 22 2c 0a 20 2f 2a  esolvetype",. /*
313c6 20 31 30 33 20 2a 2f 20 22 72 65 73 6f 6c 76 65   103 */ "resolve
313c7 74 79 70 65 20 3a 3a 3d 20 72 61 69 73 65 74 79  type ::= raisety
313c8 70 65 22 2c 0a 20 2f 2a 20 31 30 34 20 2a 2f 20  pe",. /* 104 */ 
313c9 22 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d  "resolvetype ::=
313ca 20 49 47 4e 4f 52 45 22 2c 0a 20 2f 2a 20 31 30   IGNORE",. /* 10
313cb 35 20 2a 2f 20 22 72 65 73 6f 6c 76 65 74 79 70  5 */ "resolvetyp
313cc 65 20 3a 3a 3d 20 52 45 50 4c 41 43 45 22 2c 0a  e ::= REPLACE",.
313cd 20 2f 2a 20 31 30 36 20 2a 2f 20 22 63 6d 64 20   /* 106 */ "cmd 
313ce 3a 3a 3d 20 44 52 4f 50 20 54 41 42 4c 45 20 69  ::= DROP TABLE i
313cf 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65  fexists fullname
313d0 22 2c 0a 20 2f 2a 20 31 30 37 20 2a 2f 20 22 69  ",. /* 107 */ "i
313d1 66 65 78 69 73 74 73 20 3a 3a 3d 20 49 46 20 45  fexists ::= IF E
313d2 58 49 53 54 53 22 2c 0a 20 2f 2a 20 31 30 38 20  XISTS",. /* 108 
313d3 2a 2f 20 22 69 66 65 78 69 73 74 73 20 3a 3a 3d  */ "ifexists ::=
313d4 22 2c 0a 20 2f 2a 20 31 30 39 20 2a 2f 20 22 63  ",. /* 109 */ "c
313d5 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20  md ::= createkw 
313d6 74 65 6d 70 20 56 49 45 57 20 69 66 6e 6f 74 65  temp VIEW ifnote
313d7 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 41 53  xists nm dbnm AS
313d8 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 31   select",. /* 11
313d9 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 52  0 */ "cmd ::= DR
313da 4f 50 20 56 49 45 57 20 69 66 65 78 69 73 74 73  OP VIEW ifexists
313db 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20   fullname",. /* 
313dc 31 31 31 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  111 */ "cmd ::= 
313dd 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 31 32  select",. /* 112
313de 20 2a 2f 20 22 73 65 6c 65 63 74 20 3a 3a 3d 20   */ "select ::= 
313df 6f 6e 65 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20  oneselect",. /* 
313e0 31 31 33 20 2a 2f 20 22 73 65 6c 65 63 74 20 3a  113 */ "select :
313e1 3a 3d 20 73 65 6c 65 63 74 20 6d 75 6c 74 69 73  := select multis
313e2 65 6c 65 63 74 5f 6f 70 20 6f 6e 65 73 65 6c 65  elect_op onesele
313e3 63 74 22 2c 0a 20 2f 2a 20 31 31 34 20 2a 2f 20  ct",. /* 114 */ 
313e4 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20  "multiselect_op 
313e5 3a 3a 3d 20 55 4e 49 4f 4e 22 2c 0a 20 2f 2a 20  ::= UNION",. /* 
313e6 31 31 35 20 2a 2f 20 22 6d 75 6c 74 69 73 65 6c  115 */ "multisel
313e7 65 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e  ect_op ::= UNION
313e8 20 41 4c 4c 22 2c 0a 20 2f 2a 20 31 31 36 20 2a   ALL",. /* 116 *
313e9 2f 20 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f  / "multiselect_o
313ea 70 20 3a 3a 3d 20 45 58 43 45 50 54 7c 49 4e 54  p ::= EXCEPT|INT
313eb 45 52 53 45 43 54 22 2c 0a 20 2f 2a 20 31 31 37  ERSECT",. /* 117
313ec 20 2a 2f 20 22 6f 6e 65 73 65 6c 65 63 74 20 3a   */ "oneselect :
313ed 3a 3d 20 53 45 4c 45 43 54 20 64 69 73 74 69 6e  := SELECT distin
313ee 63 74 20 73 65 6c 63 6f 6c 6c 69 73 74 20 66 72  ct selcollist fr
313ef 6f 6d 20 77 68 65 72 65 5f 6f 70 74 20 67 72 6f  om where_opt gro
313f0 75 70 62 79 5f 6f 70 74 20 68 61 76 69 6e 67 5f  upby_opt having_
313f1 6f 70 74 20 6f 72 64 65 72 62 79 5f 6f 70 74 20  opt orderby_opt 
313f2 6c 69 6d 69 74 5f 6f 70 74 22 2c 0a 20 2f 2a 20  limit_opt",. /* 
313f3 31 31 38 20 2a 2f 20 22 64 69 73 74 69 6e 63 74  118 */ "distinct
313f4 20 3a 3a 3d 20 44 49 53 54 49 4e 43 54 22 2c 0a   ::= DISTINCT",.
313f5 20 2f 2a 20 31 31 39 20 2a 2f 20 22 64 69 73 74   /* 119 */ "dist
313f6 69 6e 63 74 20 3a 3a 3d 20 41 4c 4c 22 2c 0a 20  inct ::= ALL",. 
313f7 2f 2a 20 31 32 30 20 2a 2f 20 22 64 69 73 74 69  /* 120 */ "disti
313f8 6e 63 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32  nct ::=",. /* 12
313f9 31 20 2a 2f 20 22 73 63 6c 70 20 3a 3a 3d 20 73  1 */ "sclp ::= s
313fa 65 6c 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41 22  elcollist COMMA"
313fb 2c 0a 20 2f 2a 20 31 32 32 20 2a 2f 20 22 73 63  ,. /* 122 */ "sc
313fc 6c 70 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32 33  lp ::=",. /* 123
313fd 20 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73 74 20   */ "selcollist 
313fe 3a 3a 3d 20 73 63 6c 70 20 65 78 70 72 20 61 73  ::= sclp expr as
313ff 22 2c 0a 20 2f 2a 20 31 32 34 20 2a 2f 20 22 73  ",. /* 124 */ "s
31400 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63  elcollist ::= sc
31401 6c 70 20 53 54 41 52 22 2c 0a 20 2f 2a 20 31 32  lp STAR",. /* 12
31402 35 20 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69 73 74  5 */ "selcollist
31403 20 3a 3a 3d 20 73 63 6c 70 20 6e 6d 20 44 4f 54   ::= sclp nm DOT
31404 20 53 54 41 52 22 2c 0a 20 2f 2a 20 31 32 36 20   STAR",. /* 126 
31405 2a 2f 20 22 61 73 20 3a 3a 3d 20 41 53 20 6e 6d  */ "as ::= AS nm
31406 22 2c 0a 20 2f 2a 20 31 32 37 20 2a 2f 20 22 61  ",. /* 127 */ "a
31407 73 20 3a 3a 3d 20 69 64 73 22 2c 0a 20 2f 2a 20  s ::= ids",. /* 
31408 31 32 38 20 2a 2f 20 22 61 73 20 3a 3a 3d 22 2c  128 */ "as ::=",
31409 0a 20 2f 2a 20 31 32 39 20 2a 2f 20 22 66 72 6f  . /* 129 */ "fro
3140a 6d 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33 30 20  m ::=",. /* 130 
3140b 2a 2f 20 22 66 72 6f 6d 20 3a 3a 3d 20 46 52 4f  */ "from ::= FRO
3140c 4d 20 73 65 6c 74 61 62 6c 69 73 74 22 2c 0a 20  M seltablist",. 
3140d 2f 2a 20 31 33 31 20 2a 2f 20 22 73 74 6c 5f 70  /* 131 */ "stl_p
3140e 72 65 66 69 78 20 3a 3a 3d 20 73 65 6c 74 61 62  refix ::= seltab
3140f 6c 69 73 74 20 6a 6f 69 6e 6f 70 22 2c 0a 20 2f  list joinop",. /
31410 2a 20 31 33 32 20 2a 2f 20 22 73 74 6c 5f 70 72  * 132 */ "stl_pr
31411 65 66 69 78 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  efix ::=",. /* 1
31412 33 33 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73  33 */ "seltablis
31413 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78  t ::= stl_prefix
31414 20 6e 6d 20 64 62 6e 6d 20 61 73 20 69 6e 64 65   nm dbnm as inde
31415 78 65 64 5f 6f 70 74 20 6f 6e 5f 6f 70 74 20 75  xed_opt on_opt u
31416 73 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31  sing_opt",. /* 1
31417 33 34 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73  34 */ "seltablis
31418 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78  t ::= stl_prefix
31419 20 4c 50 20 73 65 6c 65 63 74 20 52 50 20 61 73   LP select RP as
3141a 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70   on_opt using_op
3141b 74 22 2c 0a 20 2f 2a 20 31 33 35 20 2a 2f 20 22  t",. /* 135 */ "
3141c 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73  seltablist ::= s
3141d 74 6c 5f 70 72 65 66 69 78 20 4c 50 20 73 65 6c  tl_prefix LP sel
3141e 74 61 62 6c 69 73 74 20 52 50 20 61 73 20 6f 6e  tablist RP as on
3141f 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 22 2c  _opt using_opt",
31420 0a 20 2f 2a 20 31 33 36 20 2a 2f 20 22 64 62 6e  . /* 136 */ "dbn
31421 6d 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33 37 20  m ::=",. /* 137 
31422 2a 2f 20 22 64 62 6e 6d 20 3a 3a 3d 20 44 4f 54  */ "dbnm ::= DOT
31423 20 6e 6d 22 2c 0a 20 2f 2a 20 31 33 38 20 2a 2f   nm",. /* 138 */
31424 20 22 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 6e   "fullname ::= n
31425 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 31 33 39  m dbnm",. /* 139
31426 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20   */ "joinop ::= 
31427 43 4f 4d 4d 41 7c 4a 4f 49 4e 22 2c 0a 20 2f 2a  COMMA|JOIN",. /*
31428 20 31 34 30 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20   140 */ "joinop 
31429 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 4a 4f 49 4e  ::= JOIN_KW JOIN
3142a 22 2c 0a 20 2f 2a 20 31 34 31 20 2a 2f 20 22 6a  ",. /* 141 */ "j
3142b 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b  oinop ::= JOIN_K
3142c 57 20 6e 6d 20 4a 4f 49 4e 22 2c 0a 20 2f 2a 20  W nm JOIN",. /* 
3142d 31 34 32 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a  142 */ "joinop :
3142e 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 6e 6d  := JOIN_KW nm nm
3142f 20 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31 34 33 20   JOIN",. /* 143 
31430 2a 2f 20 22 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 4f  */ "on_opt ::= O
31431 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 34 34  N expr",. /* 144
31432 20 2a 2f 20 22 6f 6e 5f 6f 70 74 20 3a 3a 3d 22   */ "on_opt ::="
31433 2c 0a 20 2f 2a 20 31 34 35 20 2a 2f 20 22 69 6e  ,. /* 145 */ "in
31434 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 22 2c 0a  dexed_opt ::=",.
31435 20 2f 2a 20 31 34 36 20 2a 2f 20 22 69 6e 64 65   /* 146 */ "inde
31436 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 44 45  xed_opt ::= INDE
31437 58 45 44 20 42 59 20 6e 6d 22 2c 0a 20 2f 2a 20  XED BY nm",. /* 
31438 31 34 37 20 2a 2f 20 22 69 6e 64 65 78 65 64 5f  147 */ "indexed_
31439 6f 70 74 20 3a 3a 3d 20 4e 4f 54 20 49 4e 44 45  opt ::= NOT INDE
3143a 58 45 44 22 2c 0a 20 2f 2a 20 31 34 38 20 2a 2f  XED",. /* 148 */
3143b 20 22 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20   "using_opt ::= 
3143c 55 53 49 4e 47 20 4c 50 20 69 6e 73 63 6f 6c 6c  USING LP inscoll
3143d 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31 34 39  ist RP",. /* 149
3143e 20 2a 2f 20 22 75 73 69 6e 67 5f 6f 70 74 20 3a   */ "using_opt :
3143f 3a 3d 22 2c 0a 20 2f 2a 20 31 35 30 20 2a 2f 20  :=",. /* 150 */ 
31440 22 6f 72 64 65 72 62 79 5f 6f 70 74 20 3a 3a 3d  "orderby_opt ::=
31441 22 2c 0a 20 2f 2a 20 31 35 31 20 2a 2f 20 22 6f  ",. /* 151 */ "o
31442 72 64 65 72 62 79 5f 6f 70 74 20 3a 3a 3d 20 4f  rderby_opt ::= O
31443 52 44 45 52 20 42 59 20 73 6f 72 74 6c 69 73 74  RDER BY sortlist
31444 22 2c 0a 20 2f 2a 20 31 35 32 20 2a 2f 20 22 73  ",. /* 152 */ "s
31445 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72 74  ortlist ::= sort
31446 6c 69 73 74 20 43 4f 4d 4d 41 20 73 6f 72 74 69  list COMMA sorti
31447 74 65 6d 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a  tem sortorder",.
31448 20 2f 2a 20 31 35 33 20 2a 2f 20 22 73 6f 72 74   /* 153 */ "sort
31449 6c 69 73 74 20 3a 3a 3d 20 73 6f 72 74 69 74 65  list ::= sortite
3144a 6d 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f  m sortorder",. /
3144b 2a 20 31 35 34 20 2a 2f 20 22 73 6f 72 74 69 74  * 154 */ "sortit
3144c 65 6d 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f  em ::= expr",. /
3144d 2a 20 31 35 35 20 2a 2f 20 22 73 6f 72 74 6f 72  * 155 */ "sortor
3144e 64 65 72 20 3a 3a 3d 20 41 53 43 22 2c 0a 20 2f  der ::= ASC",. /
3144f 2a 20 31 35 36 20 2a 2f 20 22 73 6f 72 74 6f 72  * 156 */ "sortor
31450 64 65 72 20 3a 3a 3d 20 44 45 53 43 22 2c 0a 20  der ::= DESC",. 
31451 2f 2a 20 31 35 37 20 2a 2f 20 22 73 6f 72 74 6f  /* 157 */ "sorto
31452 72 64 65 72 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  rder ::=",. /* 1
31453 35 38 20 2a 2f 20 22 67 72 6f 75 70 62 79 5f 6f  58 */ "groupby_o
31454 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35 39  pt ::=",. /* 159
31455 20 2a 2f 20 22 67 72 6f 75 70 62 79 5f 6f 70 74   */ "groupby_opt
31456 20 3a 3a 3d 20 47 52 4f 55 50 20 42 59 20 6e 65   ::= GROUP BY ne
31457 78 70 72 6c 69 73 74 22 2c 0a 20 2f 2a 20 31 36  xprlist",. /* 16
31458 30 20 2a 2f 20 22 68 61 76 69 6e 67 5f 6f 70 74  0 */ "having_opt
31459 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 36 31 20 2a   ::=",. /* 161 *
3145a 2f 20 22 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a  / "having_opt ::
3145b 3d 20 48 41 56 49 4e 47 20 65 78 70 72 22 2c 0a  = HAVING expr",.
3145c 20 2f 2a 20 31 36 32 20 2a 2f 20 22 6c 69 6d 69   /* 162 */ "limi
3145d 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  t_opt ::=",. /* 
3145e 31 36 33 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70  163 */ "limit_op
3145f 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72  t ::= LIMIT expr
31460 22 2c 0a 20 2f 2a 20 31 36 34 20 2a 2f 20 22 6c  ",. /* 164 */ "l
31461 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d  imit_opt ::= LIM
31462 49 54 20 65 78 70 72 20 4f 46 46 53 45 54 20 65  IT expr OFFSET e
31463 78 70 72 22 2c 0a 20 2f 2a 20 31 36 35 20 2a 2f  xpr",. /* 165 */
31464 20 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20   "limit_opt ::= 
31465 4c 49 4d 49 54 20 65 78 70 72 20 43 4f 4d 4d 41  LIMIT expr COMMA
31466 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36 36 20   expr",. /* 166 
31467 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 45 4c 45  */ "cmd ::= DELE
31468 54 45 20 46 52 4f 4d 20 66 75 6c 6c 6e 61 6d 65  TE FROM fullname
31469 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 77 68 65   indexed_opt whe
3146a 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 36 37  re_opt",. /* 167
3146b 20 2a 2f 20 22 77 68 65 72 65 5f 6f 70 74 20 3a   */ "where_opt :
3146c 3a 3d 22 2c 0a 20 2f 2a 20 31 36 38 20 2a 2f 20  :=",. /* 168 */ 
3146d 22 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d 20 57  "where_opt ::= W
3146e 48 45 52 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20  HERE expr",. /* 
3146f 31 36 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  169 */ "cmd ::= 
31470 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20 66 75  UPDATE orconf fu
31471 6c 6c 6e 61 6d 65 20 69 6e 64 65 78 65 64 5f 6f  llname indexed_o
31472 70 74 20 53 45 54 20 73 65 74 6c 69 73 74 20 77  pt SET setlist w
31473 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31  here_opt",. /* 1
31474 37 30 20 2a 2f 20 22 73 65 74 6c 69 73 74 20 3a  70 */ "setlist :
31475 3a 3d 20 73 65 74 6c 69 73 74 20 43 4f 4d 4d 41  := setlist COMMA
31476 20 6e 6d 20 45 51 20 65 78 70 72 22 2c 0a 20 2f   nm EQ expr",. /
31477 2a 20 31 37 31 20 2a 2f 20 22 73 65 74 6c 69 73  * 171 */ "setlis
31478 74 20 3a 3a 3d 20 6e 6d 20 45 51 20 65 78 70 72  t ::= nm EQ expr
31479 22 2c 0a 20 2f 2a 20 31 37 32 20 2a 2f 20 22 63  ",. /* 172 */ "c
3147a 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d  md ::= insert_cm
3147b 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20  d INTO fullname 
3147c 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 56  inscollist_opt V
3147d 41 4c 55 45 53 20 4c 50 20 69 74 65 6d 6c 69 73  ALUES LP itemlis
3147e 74 20 52 50 22 2c 0a 20 2f 2a 20 31 37 33 20 2a  t RP",. /* 173 *
3147f 2f 20 22 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72  / "cmd ::= inser
31480 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e  t_cmd INTO fulln
31481 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f  ame inscollist_o
31482 70 74 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20  pt select",. /* 
31483 31 37 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  174 */ "cmd ::= 
31484 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20  insert_cmd INTO 
31485 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c  fullname inscoll
31486 69 73 74 5f 6f 70 74 20 44 45 46 41 55 4c 54 20  ist_opt DEFAULT 
31487 56 41 4c 55 45 53 22 2c 0a 20 2f 2a 20 31 37 35  VALUES",. /* 175
31488 20 2a 2f 20 22 69 6e 73 65 72 74 5f 63 6d 64 20   */ "insert_cmd 
31489 3a 3a 3d 20 49 4e 53 45 52 54 20 6f 72 63 6f 6e  ::= INSERT orcon
3148a 66 22 2c 0a 20 2f 2a 20 31 37 36 20 2a 2f 20 22  f",. /* 176 */ "
3148b 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20 52  insert_cmd ::= R
3148c 45 50 4c 41 43 45 22 2c 0a 20 2f 2a 20 31 37 37  EPLACE",. /* 177
3148d 20 2a 2f 20 22 69 74 65 6d 6c 69 73 74 20 3a 3a   */ "itemlist ::
3148e 3d 20 69 74 65 6d 6c 69 73 74 20 43 4f 4d 4d 41  = itemlist COMMA
3148f 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 37 38 20   expr",. /* 178 
31490 2a 2f 20 22 69 74 65 6d 6c 69 73 74 20 3a 3a 3d  */ "itemlist ::=
31491 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 37 39 20   expr",. /* 179 
31492 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f  */ "inscollist_o
31493 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 38 30  pt ::=",. /* 180
31494 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 5f   */ "inscollist_
31495 6f 70 74 20 3a 3a 3d 20 4c 50 20 69 6e 73 63 6f  opt ::= LP insco
31496 6c 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31  llist RP",. /* 1
31497 38 31 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73  81 */ "inscollis
31498 74 20 3a 3a 3d 20 69 6e 73 63 6f 6c 6c 69 73 74  t ::= inscollist
31499 20 43 4f 4d 4d 41 20 6e 6d 22 2c 0a 20 2f 2a 20   COMMA nm",. /* 
3149a 31 38 32 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69  182 */ "inscolli
3149b 73 74 20 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a 20  st ::= nm",. /* 
3149c 31 38 33 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  183 */ "expr ::=
3149d 20 74 65 72 6d 22 2c 0a 20 2f 2a 20 31 38 34 20   term",. /* 184 
3149e 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4c 50 20  */ "expr ::= LP 
3149f 65 78 70 72 20 52 50 22 2c 0a 20 2f 2a 20 31 38  expr RP",. /* 18
314a0 35 20 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20 4e  5 */ "term ::= N
314a1 55 4c 4c 22 2c 0a 20 2f 2a 20 31 38 36 20 2a 2f  ULL",. /* 186 */
314a2 20 22 65 78 70 72 20 3a 3a 3d 20 69 64 22 2c 0a   "expr ::= id",.
314a3 20 2f 2a 20 31 38 37 20 2a 2f 20 22 65 78 70 72   /* 187 */ "expr
314a4 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 22 2c 0a 20   ::= JOIN_KW",. 
314a5 2f 2a 20 31 38 38 20 2a 2f 20 22 65 78 70 72 20  /* 188 */ "expr 
314a6 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 22 2c 0a  ::= nm DOT nm",.
314a7 20 2f 2a 20 31 38 39 20 2a 2f 20 22 65 78 70 72   /* 189 */ "expr
314a8 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 44   ::= nm DOT nm D
314a9 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20 31 39 30 20  OT nm",. /* 190 
314aa 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20 49 4e 54  */ "term ::= INT
314ab 45 47 45 52 7c 46 4c 4f 41 54 7c 42 4c 4f 42 22  EGER|FLOAT|BLOB"
314ac 2c 0a 20 2f 2a 20 31 39 31 20 2a 2f 20 22 74 65  ,. /* 191 */ "te
314ad 72 6d 20 3a 3a 3d 20 53 54 52 49 4e 47 22 2c 0a  rm ::= STRING",.
314ae 20 2f 2a 20 31 39 32 20 2a 2f 20 22 65 78 70 72   /* 192 */ "expr
314af 20 3a 3a 3d 20 52 45 47 49 53 54 45 52 22 2c 0a   ::= REGISTER",.
314b0 20 2f 2a 20 31 39 33 20 2a 2f 20 22 65 78 70 72   /* 193 */ "expr
314b1 20 3a 3a 3d 20 56 41 52 49 41 42 4c 45 22 2c 0a   ::= VARIABLE",.
314b2 20 2f 2a 20 31 39 34 20 2a 2f 20 22 65 78 70 72   /* 194 */ "expr
314b3 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4c 4c 41 54   ::= expr COLLAT
314b4 45 20 69 64 73 22 2c 0a 20 2f 2a 20 31 39 35 20  E ids",. /* 195 
314b5 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 43 41 53  */ "expr ::= CAS
314b6 54 20 4c 50 20 65 78 70 72 20 41 53 20 74 79 70  T LP expr AS typ
314b7 65 74 6f 6b 65 6e 20 52 50 22 2c 0a 20 2f 2a 20  etoken RP",. /* 
314b8 31 39 36 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  196 */ "expr ::=
314b9 20 49 44 20 4c 50 20 64 69 73 74 69 6e 63 74 20   ID LP distinct 
314ba 65 78 70 72 6c 69 73 74 20 52 50 22 2c 0a 20 2f  exprlist RP",. /
314bb 2a 20 31 39 37 20 2a 2f 20 22 65 78 70 72 20 3a  * 197 */ "expr :
314bc 3a 3d 20 49 44 20 4c 50 20 53 54 41 52 20 52 50  := ID LP STAR RP
314bd 22 2c 0a 20 2f 2a 20 31 39 38 20 2a 2f 20 22 74  ",. /* 198 */ "t
314be 65 72 6d 20 3a 3a 3d 20 43 54 49 4d 45 5f 4b 57  erm ::= CTIME_KW
314bf 22 2c 0a 20 2f 2a 20 31 39 39 20 2a 2f 20 22 65  ",. /* 199 */ "e
314c0 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 41 4e 44  xpr ::= expr AND
314c1 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 30 20   expr",. /* 200 
314c2 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
314c3 72 20 4f 52 20 65 78 70 72 22 2c 0a 20 2f 2a 20  r OR expr",. /* 
314c4 32 30 31 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  201 */ "expr ::=
314c5 20 65 78 70 72 20 4c 54 7c 47 54 7c 47 45 7c 4c   expr LT|GT|GE|L
314c6 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 32  E expr",. /* 202
314c7 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
314c8 70 72 20 45 51 7c 4e 45 20 65 78 70 72 22 2c 0a  pr EQ|NE expr",.
314c9 20 2f 2a 20 32 30 33 20 2a 2f 20 22 65 78 70 72   /* 203 */ "expr
314ca 20 3a 3a 3d 20 65 78 70 72 20 42 49 54 41 4e 44   ::= expr BITAND
314cb 7c 42 49 54 4f 52 7c 4c 53 48 49 46 54 7c 52 53  |BITOR|LSHIFT|RS
314cc 48 49 46 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20  HIFT expr",. /* 
314cd 32 30 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  204 */ "expr ::=
314ce 20 65 78 70 72 20 50 4c 55 53 7c 4d 49 4e 55 53   expr PLUS|MINUS
314cf 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 35 20   expr",. /* 205 
314d0 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
314d1 72 20 53 54 41 52 7c 53 4c 41 53 48 7c 52 45 4d  r STAR|SLASH|REM
314d2 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 36 20   expr",. /* 206 
314d3 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
314d4 72 20 43 4f 4e 43 41 54 20 65 78 70 72 22 2c 0a  r CONCAT expr",.
314d5 20 2f 2a 20 32 30 37 20 2a 2f 20 22 6c 69 6b 65   /* 207 */ "like
314d6 6f 70 20 3a 3a 3d 20 4c 49 4b 45 5f 4b 57 22 2c  op ::= LIKE_KW",
314d7 0a 20 2f 2a 20 32 30 38 20 2a 2f 20 22 6c 69 6b  . /* 208 */ "lik
314d8 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4c 49 4b 45  eop ::= NOT LIKE
314d9 5f 4b 57 22 2c 0a 20 2f 2a 20 32 30 39 20 2a 2f  _KW",. /* 209 */
314da 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4d 41 54   "likeop ::= MAT
314db 43 48 22 2c 0a 20 2f 2a 20 32 31 30 20 2a 2f 20  CH",. /* 210 */ 
314dc 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20  "likeop ::= NOT 
314dd 4d 41 54 43 48 22 2c 0a 20 2f 2a 20 32 31 31 20  MATCH",. /* 211 
314de 2a 2f 20 22 65 73 63 61 70 65 20 3a 3a 3d 20 45  */ "escape ::= E
314df 53 43 41 50 45 20 65 78 70 72 22 2c 0a 20 2f 2a  SCAPE expr",. /*
314e0 20 32 31 32 20 2a 2f 20 22 65 73 63 61 70 65 20   212 */ "escape 
314e1 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 31 33 20 2a 2f  ::=",. /* 213 */
314e2 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
314e3 6c 69 6b 65 6f 70 20 65 78 70 72 20 65 73 63 61  likeop expr esca
314e4 70 65 22 2c 0a 20 2f 2a 20 32 31 34 20 2a 2f 20  pe",. /* 214 */ 
314e5 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49  "expr ::= expr I
314e6 53 4e 55 4c 4c 7c 4e 4f 54 4e 55 4c 4c 22 2c 0a  SNULL|NOTNULL",.
314e7 20 2f 2a 20 32 31 35 20 2a 2f 20 22 65 78 70 72   /* 215 */ "expr
314e8 20 3a 3a 3d 20 65 78 70 72 20 4e 4f 54 20 4e 55   ::= expr NOT NU
314e9 4c 4c 22 2c 0a 20 2f 2a 20 32 31 36 20 2a 2f 20  LL",. /* 216 */ 
314ea 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49  "expr ::= expr I
314eb 53 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 31 37  S expr",. /* 217
314ec 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
314ed 70 72 20 49 53 20 4e 4f 54 20 65 78 70 72 22 2c  pr IS NOT expr",
314ee 0a 20 2f 2a 20 32 31 38 20 2a 2f 20 22 65 78 70  . /* 218 */ "exp
314ef 72 20 3a 3a 3d 20 4e 4f 54 20 65 78 70 72 22 2c  r ::= NOT expr",
314f0 0a 20 2f 2a 20 32 31 39 20 2a 2f 20 22 65 78 70  . /* 219 */ "exp
314f1 72 20 3a 3a 3d 20 42 49 54 4e 4f 54 20 65 78 70  r ::= BITNOT exp
314f2 72 22 2c 0a 20 2f 2a 20 32 32 30 20 2a 2f 20 22  r",. /* 220 */ "
314f3 65 78 70 72 20 3a 3a 3d 20 4d 49 4e 55 53 20 65  expr ::= MINUS e
314f4 78 70 72 22 2c 0a 20 2f 2a 20 32 32 31 20 2a 2f  xpr",. /* 221 */
314f5 20 22 65 78 70 72 20 3a 3a 3d 20 50 4c 55 53 20   "expr ::= PLUS 
314f6 65 78 70 72 22 2c 0a 20 2f 2a 20 32 32 32 20 2a  expr",. /* 222 *
314f7 2f 20 22 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a  / "between_op ::
314f8 3d 20 42 45 54 57 45 45 4e 22 2c 0a 20 2f 2a 20  = BETWEEN",. /* 
314f9 32 32 33 20 2a 2f 20 22 62 65 74 77 65 65 6e 5f  223 */ "between_
314fa 6f 70 20 3a 3a 3d 20 4e 4f 54 20 42 45 54 57 45  op ::= NOT BETWE
314fb 45 4e 22 2c 0a 20 2f 2a 20 32 32 34 20 2a 2f 20  EN",. /* 224 */ 
314fc 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 62  "expr ::= expr b
314fd 65 74 77 65 65 6e 5f 6f 70 20 65 78 70 72 20 41  etween_op expr A
314fe 4e 44 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 32  ND expr",. /* 22
314ff 35 20 2a 2f 20 22 69 6e 5f 6f 70 20 3a 3a 3d 20  5 */ "in_op ::= 
31500 49 4e 22 2c 0a 20 2f 2a 20 32 32 36 20 2a 2f 20  IN",. /* 226 */ 
31501 22 69 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 49  "in_op ::= NOT I
31502 4e 22 2c 0a 20 2f 2a 20 32 32 37 20 2a 2f 20 22  N",. /* 227 */ "
31503 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e  expr ::= expr in
31504 5f 6f 70 20 4c 50 20 65 78 70 72 6c 69 73 74 20  _op LP exprlist 
31505 52 50 22 2c 0a 20 2f 2a 20 32 32 38 20 2a 2f 20  RP",. /* 228 */ 
31506 22 65 78 70 72 20 3a 3a 3d 20 4c 50 20 73 65 6c  "expr ::= LP sel
31507 65 63 74 20 52 50 22 2c 0a 20 2f 2a 20 32 32 39  ect RP",. /* 229
31508 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
31509 70 72 20 69 6e 5f 6f 70 20 4c 50 20 73 65 6c 65  pr in_op LP sele
3150a 63 74 20 52 50 22 2c 0a 20 2f 2a 20 32 33 30 20  ct RP",. /* 230 
3150b 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70  */ "expr ::= exp
3150c 72 20 69 6e 5f 6f 70 20 6e 6d 20 64 62 6e 6d 22  r in_op nm dbnm"
3150d 2c 0a 20 2f 2a 20 32 33 31 20 2a 2f 20 22 65 78  ,. /* 231 */ "ex
3150e 70 72 20 3a 3a 3d 20 45 58 49 53 54 53 20 4c 50  pr ::= EXISTS LP
3150f 20 73 65 6c 65 63 74 20 52 50 22 2c 0a 20 2f 2a   select RP",. /*
31510 20 32 33 32 20 2a 2f 20 22 65 78 70 72 20 3a 3a   232 */ "expr ::
31511 3d 20 43 41 53 45 20 63 61 73 65 5f 6f 70 65 72  = CASE case_oper
31512 61 6e 64 20 63 61 73 65 5f 65 78 70 72 6c 69 73  and case_exprlis
31513 74 20 63 61 73 65 5f 65 6c 73 65 20 45 4e 44 22  t case_else END"
31514 2c 0a 20 2f 2a 20 32 33 33 20 2a 2f 20 22 63 61  ,. /* 233 */ "ca
31515 73 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20  se_exprlist ::= 
31516 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 57 48  case_exprlist WH
31517 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78 70  EN expr THEN exp
31518 72 22 2c 0a 20 2f 2a 20 32 33 34 20 2a 2f 20 22  r",. /* 234 */ "
31519 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a  case_exprlist ::
3151a 3d 20 57 48 45 4e 20 65 78 70 72 20 54 48 45 4e  = WHEN expr THEN
3151b 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 33 35 20   expr",. /* 235 
3151c 2a 2f 20 22 63 61 73 65 5f 65 6c 73 65 20 3a 3a  */ "case_else ::
3151d 3d 20 45 4c 53 45 20 65 78 70 72 22 2c 0a 20 2f  = ELSE expr",. /
3151e 2a 20 32 33 36 20 2a 2f 20 22 63 61 73 65 5f 65  * 236 */ "case_e
3151f 6c 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 33  lse ::=",. /* 23
31520 37 20 2a 2f 20 22 63 61 73 65 5f 6f 70 65 72 61  7 */ "case_opera
31521 6e 64 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20 2f  nd ::= expr",. /
31522 2a 20 32 33 38 20 2a 2f 20 22 63 61 73 65 5f 6f  * 238 */ "case_o
31523 70 65 72 61 6e 64 20 3a 3a 3d 22 2c 0a 20 2f 2a  perand ::=",. /*
31524 20 32 33 39 20 2a 2f 20 22 65 78 70 72 6c 69 73   239 */ "exprlis
31525 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74 22  t ::= nexprlist"
31526 2c 0a 20 2f 2a 20 32 34 30 20 2a 2f 20 22 65 78  ,. /* 240 */ "ex
31527 70 72 6c 69 73 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  prlist ::=",. /*
31528 20 32 34 31 20 2a 2f 20 22 6e 65 78 70 72 6c 69   241 */ "nexprli
31529 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74  st ::= nexprlist
3152a 20 43 4f 4d 4d 41 20 65 78 70 72 22 2c 0a 20 2f   COMMA expr",. /
3152b 2a 20 32 34 32 20 2a 2f 20 22 6e 65 78 70 72 6c  * 242 */ "nexprl
3152c 69 73 74 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20  ist ::= expr",. 
3152d 2f 2a 20 32 34 33 20 2a 2f 20 22 63 6d 64 20 3a  /* 243 */ "cmd :
3152e 3a 3d 20 63 72 65 61 74 65 6b 77 20 75 6e 69 71  := createkw uniq
3152f 75 65 66 6c 61 67 20 49 4e 44 45 58 20 69 66 6e  ueflag INDEX ifn
31530 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d  otexists nm dbnm
31531 20 4f 4e 20 6e 6d 20 4c 50 20 69 64 78 6c 69 73   ON nm LP idxlis
31532 74 20 52 50 22 2c 0a 20 2f 2a 20 32 34 34 20 2a  t RP",. /* 244 *
31533 2f 20 22 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a  / "uniqueflag ::
31534 3d 20 55 4e 49 51 55 45 22 2c 0a 20 2f 2a 20 32  = UNIQUE",. /* 2
31535 34 35 20 2a 2f 20 22 75 6e 69 71 75 65 66 6c 61  45 */ "uniquefla
31536 67 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 34 36 20  g ::=",. /* 246 
31537 2a 2f 20 22 69 64 78 6c 69 73 74 5f 6f 70 74 20  */ "idxlist_opt 
31538 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 34 37 20 2a 2f  ::=",. /* 247 */
31539 20 22 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a   "idxlist_opt ::
3153a 3d 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 22  = LP idxlist RP"
3153b 2c 0a 20 2f 2a 20 32 34 38 20 2a 2f 20 22 69 64  ,. /* 248 */ "id
3153c 78 6c 69 73 74 20 3a 3a 3d 20 69 64 78 6c 69 73  xlist ::= idxlis
3153d 74 20 43 4f 4d 4d 41 20 6e 6d 20 63 6f 6c 6c 61  t COMMA nm colla
3153e 74 65 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20  te sortorder",. 
3153f 2f 2a 20 32 34 39 20 2a 2f 20 22 69 64 78 6c 69  /* 249 */ "idxli
31540 73 74 20 3a 3a 3d 20 6e 6d 20 63 6f 6c 6c 61 74  st ::= nm collat
31541 65 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f  e sortorder",. /
31542 2a 20 32 35 30 20 2a 2f 20 22 63 6f 6c 6c 61 74  * 250 */ "collat
31543 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 35 31 20  e ::=",. /* 251 
31544 2a 2f 20 22 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20  */ "collate ::= 
31545 43 4f 4c 4c 41 54 45 20 69 64 73 22 2c 0a 20 2f  COLLATE ids",. /
31546 2a 20 32 35 32 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 252 */ "cmd ::
31547 3d 20 44 52 4f 50 20 49 4e 44 45 58 20 69 66 65  = DROP INDEX ife
31548 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c  xists fullname",
31549 0a 20 2f 2a 20 32 35 33 20 2a 2f 20 22 63 6d 64  . /* 253 */ "cmd
3154a 20 3a 3a 3d 20 56 41 43 55 55 4d 22 2c 0a 20 2f   ::= VACUUM",. /
3154b 2a 20 32 35 34 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 254 */ "cmd ::
3154c 3d 20 56 41 43 55 55 4d 20 6e 6d 22 2c 0a 20 2f  = VACUUM nm",. /
3154d 2a 20 32 35 35 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 255 */ "cmd ::
3154e 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d  = PRAGMA nm dbnm
3154f 22 2c 0a 20 2f 2a 20 32 35 36 20 2a 2f 20 22 63  ",. /* 256 */ "c
31550 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d  md ::= PRAGMA nm
31551 20 64 62 6e 6d 20 45 51 20 6e 6d 6e 75 6d 22 2c   dbnm EQ nmnum",
31552 0a 20 2f 2a 20 32 35 37 20 2a 2f 20 22 63 6d 64  . /* 257 */ "cmd
31553 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64   ::= PRAGMA nm d
31554 62 6e 6d 20 4c 50 20 6e 6d 6e 75 6d 20 52 50 22  bnm LP nmnum RP"
31555 2c 0a 20 2f 2a 20 32 35 38 20 2a 2f 20 22 63 6d  ,. /* 258 */ "cm
31556 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20  d ::= PRAGMA nm 
31557 64 62 6e 6d 20 45 51 20 6d 69 6e 75 73 5f 6e 75  dbnm EQ minus_nu
31558 6d 22 2c 0a 20 2f 2a 20 32 35 39 20 2a 2f 20 22  m",. /* 259 */ "
31559 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e  cmd ::= PRAGMA n
3155a 6d 20 64 62 6e 6d 20 4c 50 20 6d 69 6e 75 73 5f  m dbnm LP minus_
3155b 6e 75 6d 20 52 50 22 2c 0a 20 2f 2a 20 32 36 30  num RP",. /* 260
3155c 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 70   */ "nmnum ::= p
3155d 6c 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a 20 32 36  lus_num",. /* 26
3155e 31 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20  1 */ "nmnum ::= 
3155f 6e 6d 22 2c 0a 20 2f 2a 20 32 36 32 20 2a 2f 20  nm",. /* 262 */ 
31560 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 4f 4e 22 2c 0a  "nmnum ::= ON",.
31561 20 2f 2a 20 32 36 33 20 2a 2f 20 22 6e 6d 6e 75   /* 263 */ "nmnu
31562 6d 20 3a 3a 3d 20 44 45 4c 45 54 45 22 2c 0a 20  m ::= DELETE",. 
31563 2f 2a 20 32 36 34 20 2a 2f 20 22 6e 6d 6e 75 6d  /* 264 */ "nmnum
31564 20 3a 3a 3d 20 44 45 46 41 55 4c 54 22 2c 0a 20   ::= DEFAULT",. 
31565 2f 2a 20 32 36 35 20 2a 2f 20 22 70 6c 75 73 5f  /* 265 */ "plus_
31566 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6f 70 74  num ::= plus_opt
31567 20 6e 75 6d 62 65 72 22 2c 0a 20 2f 2a 20 32 36   number",. /* 26
31568 36 20 2a 2f 20 22 6d 69 6e 75 73 5f 6e 75 6d 20  6 */ "minus_num 
31569 3a 3a 3d 20 4d 49 4e 55 53 20 6e 75 6d 62 65 72  ::= MINUS number
3156a 22 2c 0a 20 2f 2a 20 32 36 37 20 2a 2f 20 22 6e  ",. /* 267 */ "n
3156b 75 6d 62 65 72 20 3a 3a 3d 20 49 4e 54 45 47 45  umber ::= INTEGE
3156c 52 7c 46 4c 4f 41 54 22 2c 0a 20 2f 2a 20 32 36  R|FLOAT",. /* 26
3156d 38 20 2a 2f 20 22 70 6c 75 73 5f 6f 70 74 20 3a  8 */ "plus_opt :
3156e 3a 3d 20 50 4c 55 53 22 2c 0a 20 2f 2a 20 32 36  := PLUS",. /* 26
3156f 39 20 2a 2f 20 22 70 6c 75 73 5f 6f 70 74 20 3a  9 */ "plus_opt :
31570 3a 3d 22 2c 0a 20 2f 2a 20 32 37 30 20 2a 2f 20  :=",. /* 270 */ 
31571 22 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b  "cmd ::= createk
31572 77 20 74 72 69 67 67 65 72 5f 64 65 63 6c 20 42  w trigger_decl B
31573 45 47 49 4e 20 74 72 69 67 67 65 72 5f 63 6d 64  EGIN trigger_cmd
31574 5f 6c 69 73 74 20 45 4e 44 22 2c 0a 20 2f 2a 20  _list END",. /* 
31575 32 37 31 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  271 */ "trigger_
31576 64 65 63 6c 20 3a 3a 3d 20 74 65 6d 70 20 54 52  decl ::= temp TR
31577 49 47 47 45 52 20 69 66 6e 6f 74 65 78 69 73 74  IGGER ifnotexist
31578 73 20 6e 6d 20 64 62 6e 6d 20 74 72 69 67 67 65  s nm dbnm trigge
31579 72 5f 74 69 6d 65 20 74 72 69 67 67 65 72 5f 65  r_time trigger_e
3157a 76 65 6e 74 20 4f 4e 20 66 75 6c 6c 6e 61 6d 65  vent ON fullname
3157b 20 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 20   foreach_clause 
3157c 77 68 65 6e 5f 63 6c 61 75 73 65 22 2c 0a 20 2f  when_clause",. /
3157d 2a 20 32 37 32 20 2a 2f 20 22 74 72 69 67 67 65  * 272 */ "trigge
3157e 72 5f 74 69 6d 65 20 3a 3a 3d 20 42 45 46 4f 52  r_time ::= BEFOR
3157f 45 22 2c 0a 20 2f 2a 20 32 37 33 20 2a 2f 20 22  E",. /* 273 */ "
31580 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d  trigger_time ::=
31581 20 41 46 54 45 52 22 2c 0a 20 2f 2a 20 32 37 34   AFTER",. /* 274
31582 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 74 69 6d   */ "trigger_tim
31583 65 20 3a 3a 3d 20 49 4e 53 54 45 41 44 20 4f 46  e ::= INSTEAD OF
31584 22 2c 0a 20 2f 2a 20 32 37 35 20 2a 2f 20 22 74  ",. /* 275 */ "t
31585 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 22  rigger_time ::="
31586 2c 0a 20 2f 2a 20 32 37 36 20 2a 2f 20 22 74 72  ,. /* 276 */ "tr
31587 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20  igger_event ::= 
31588 44 45 4c 45 54 45 7c 49 4e 53 45 52 54 22 2c 0a  DELETE|INSERT",.
31589 20 2f 2a 20 32 37 37 20 2a 2f 20 22 74 72 69 67   /* 277 */ "trig
3158a 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50  ger_event ::= UP
3158b 44 41 54 45 22 2c 0a 20 2f 2a 20 32 37 38 20 2a  DATE",. /* 278 *
3158c 2f 20 22 74 72 69 67 67 65 72 5f 65 76 65 6e 74  / "trigger_event
3158d 20 3a 3a 3d 20 55 50 44 41 54 45 20 4f 46 20 69   ::= UPDATE OF i
3158e 6e 73 63 6f 6c 6c 69 73 74 22 2c 0a 20 2f 2a 20  nscollist",. /* 
3158f 32 37 39 20 2a 2f 20 22 66 6f 72 65 61 63 68 5f  279 */ "foreach_
31590 63 6c 61 75 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a  clause ::=",. /*
31591 20 32 38 30 20 2a 2f 20 22 66 6f 72 65 61 63 68   280 */ "foreach
31592 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 46 4f 52 20  _clause ::= FOR 
31593 45 41 43 48 20 52 4f 57 22 2c 0a 20 2f 2a 20 32  EACH ROW",. /* 2
31594 38 31 20 2a 2f 20 22 77 68 65 6e 5f 63 6c 61 75  81 */ "when_clau
31595 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 38 32  se ::=",. /* 282
31596 20 2a 2f 20 22 77 68 65 6e 5f 63 6c 61 75 73 65   */ "when_clause
31597 20 3a 3a 3d 20 57 48 45 4e 20 65 78 70 72 22 2c   ::= WHEN expr",
31598 0a 20 2f 2a 20 32 38 33 20 2a 2f 20 22 74 72 69  . /* 283 */ "tri
31599 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a  gger_cmd_list ::
3159a 3d 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69  = trigger_cmd_li
3159b 73 74 20 74 72 69 67 67 65 72 5f 63 6d 64 20 53  st trigger_cmd S
3159c 45 4d 49 22 2c 0a 20 2f 2a 20 32 38 34 20 2a 2f  EMI",. /* 284 */
3159d 20 22 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69   "trigger_cmd_li
3159e 73 74 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f 63  st ::= trigger_c
3159f 6d 64 20 53 45 4d 49 22 2c 0a 20 2f 2a 20 32 38  md SEMI",. /* 28
315a0 35 20 2a 2f 20 22 74 72 6e 6d 20 3a 3a 3d 20 6e  5 */ "trnm ::= n
315a1 6d 22 2c 0a 20 2f 2a 20 32 38 36 20 2a 2f 20 22  m",. /* 286 */ "
315a2 74 72 6e 6d 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20  trnm ::= nm DOT 
315a3 6e 6d 22 2c 0a 20 2f 2a 20 32 38 37 20 2a 2f 20  nm",. /* 287 */ 
315a4 22 74 72 69 64 78 62 79 20 3a 3a 3d 22 2c 0a 20  "tridxby ::=",. 
315a5 2f 2a 20 32 38 38 20 2a 2f 20 22 74 72 69 64 78  /* 288 */ "tridx
315a6 62 79 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20 42  by ::= INDEXED B
315a7 59 20 6e 6d 22 2c 0a 20 2f 2a 20 32 38 39 20 2a  Y nm",. /* 289 *
315a8 2f 20 22 74 72 69 64 78 62 79 20 3a 3a 3d 20 4e  / "tridxby ::= N
315a9 4f 54 20 49 4e 44 45 58 45 44 22 2c 0a 20 2f 2a  OT INDEXED",. /*
315aa 20 32 39 30 20 2a 2f 20 22 74 72 69 67 67 65 72   290 */ "trigger
315ab 5f 63 6d 64 20 3a 3a 3d 20 55 50 44 41 54 45 20  _cmd ::= UPDATE 
315ac 6f 72 63 6f 6e 66 20 74 72 6e 6d 20 74 72 69 64  orconf trnm trid
315ad 78 62 79 20 53 45 54 20 73 65 74 6c 69 73 74 20  xby SET setlist 
315ae 77 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20  where_opt",. /* 
315af 32 39 31 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  291 */ "trigger_
315b0 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63  cmd ::= insert_c
315b1 6d 64 20 49 4e 54 4f 20 74 72 6e 6d 20 69 6e 73  md INTO trnm ins
315b2 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55  collist_opt VALU
315b3 45 53 20 4c 50 20 69 74 65 6d 6c 69 73 74 20 52  ES LP itemlist R
315b4 50 22 2c 0a 20 2f 2a 20 32 39 32 20 2a 2f 20 22  P",. /* 292 */ "
315b5 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20  trigger_cmd ::= 
315b6 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20  insert_cmd INTO 
315b7 74 72 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f  trnm inscollist_
315b8 6f 70 74 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a  opt select",. /*
315b9 20 32 39 33 20 2a 2f 20 22 74 72 69 67 67 65 72   293 */ "trigger
315ba 5f 63 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20  _cmd ::= DELETE 
315bb 46 52 4f 4d 20 74 72 6e 6d 20 74 72 69 64 78 62  FROM trnm tridxb
315bc 79 20 77 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f  y where_opt",. /
315bd 2a 20 32 39 34 20 2a 2f 20 22 74 72 69 67 67 65  * 294 */ "trigge
315be 72 5f 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74  r_cmd ::= select
315bf 22 2c 0a 20 2f 2a 20 32 39 35 20 2a 2f 20 22 65  ",. /* 295 */ "e
315c0 78 70 72 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50  xpr ::= RAISE LP
315c1 20 49 47 4e 4f 52 45 20 52 50 22 2c 0a 20 2f 2a   IGNORE RP",. /*
315c2 20 32 39 36 20 2a 2f 20 22 65 78 70 72 20 3a 3a   296 */ "expr ::
315c3 3d 20 52 41 49 53 45 20 4c 50 20 72 61 69 73 65  = RAISE LP raise
315c4 74 79 70 65 20 43 4f 4d 4d 41 20 6e 6d 20 52 50  type COMMA nm RP
315c5 22 2c 0a 20 2f 2a 20 32 39 37 20 2a 2f 20 22 72  ",. /* 297 */ "r
315c6 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 52 4f 4c  aisetype ::= ROL
315c7 4c 42 41 43 4b 22 2c 0a 20 2f 2a 20 32 39 38 20  LBACK",. /* 298 
315c8 2a 2f 20 22 72 61 69 73 65 74 79 70 65 20 3a 3a  */ "raisetype ::
315c9 3d 20 41 42 4f 52 54 22 2c 0a 20 2f 2a 20 32 39  = ABORT",. /* 29
315ca 39 20 2a 2f 20 22 72 61 69 73 65 74 79 70 65 20  9 */ "raisetype 
315cb 3a 3a 3d 20 46 41 49 4c 22 2c 0a 20 2f 2a 20 33  ::= FAIL",. /* 3
315cc 30 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44  00 */ "cmd ::= D
315cd 52 4f 50 20 54 52 49 47 47 45 52 20 69 66 65 78  ROP TRIGGER ifex
315ce 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a  ists fullname",.
315cf 20 2f 2a 20 33 30 31 20 2a 2f 20 22 63 6d 64 20   /* 301 */ "cmd 
315d0 3a 3a 3d 20 41 54 54 41 43 48 20 64 61 74 61 62  ::= ATTACH datab
315d1 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72 20  ase_kw_opt expr 
315d2 41 53 20 65 78 70 72 20 6b 65 79 5f 6f 70 74 22  AS expr key_opt"
315d3 2c 0a 20 2f 2a 20 33 30 32 20 2a 2f 20 22 63 6d  ,. /* 302 */ "cm
315d4 64 20 3a 3a 3d 20 44 45 54 41 43 48 20 64 61 74  d ::= DETACH dat
315d5 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70  abase_kw_opt exp
315d6 72 22 2c 0a 20 2f 2a 20 33 30 33 20 2a 2f 20 22  r",. /* 303 */ "
315d7 6b 65 79 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  key_opt ::=",. /
315d8 2a 20 33 30 34 20 2a 2f 20 22 6b 65 79 5f 6f 70  * 304 */ "key_op
315d9 74 20 3a 3a 3d 20 4b 45 59 20 65 78 70 72 22 2c  t ::= KEY expr",
315da 0a 20 2f 2a 20 33 30 35 20 2a 2f 20 22 64 61 74  . /* 305 */ "dat
315db 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d  abase_kw_opt ::=
315dc 20 44 41 54 41 42 41 53 45 22 2c 0a 20 2f 2a 20   DATABASE",. /* 
315dd 33 30 36 20 2a 2f 20 22 64 61 74 61 62 61 73 65  306 */ "database
315de 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  _kw_opt ::=",. /
315df 2a 20 33 30 37 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 307 */ "cmd ::
315e0 3d 20 52 45 49 4e 44 45 58 22 2c 0a 20 2f 2a 20  = REINDEX",. /* 
315e1 33 30 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  308 */ "cmd ::= 
315e2 52 45 49 4e 44 45 58 20 6e 6d 20 64 62 6e 6d 22  REINDEX nm dbnm"
315e3 2c 0a 20 2f 2a 20 33 30 39 20 2a 2f 20 22 63 6d  ,. /* 309 */ "cm
315e4 64 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45 22 2c 0a  d ::= ANALYZE",.
315e5 20 2f 2a 20 33 31 30 20 2a 2f 20 22 63 6d 64 20   /* 310 */ "cmd 
315e6 3a 3a 3d 20 41 4e 41 4c 59 5a 45 20 6e 6d 20 64  ::= ANALYZE nm d
315e7 62 6e 6d 22 2c 0a 20 2f 2a 20 33 31 31 20 2a 2f  bnm",. /* 311 */
315e8 20 22 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52 20   "cmd ::= ALTER 
315e9 54 41 42 4c 45 20 66 75 6c 6c 6e 61 6d 65 20 52  TABLE fullname R
315ea 45 4e 41 4d 45 20 54 4f 20 6e 6d 22 2c 0a 20 2f  ENAME TO nm",. /
315eb 2a 20 33 31 32 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 312 */ "cmd ::
315ec 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20 61 64  = ALTER TABLE ad
315ed 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d  d_column_fullnam
315ee 65 20 41 44 44 20 6b 77 63 6f 6c 75 6d 6e 5f 6f  e ADD kwcolumn_o
315ef 70 74 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a 20  pt column",. /* 
315f0 33 31 33 20 2a 2f 20 22 61 64 64 5f 63 6f 6c 75  313 */ "add_colu
315f1 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20  mn_fullname ::= 
315f2 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 33  fullname",. /* 3
315f3 31 34 20 2a 2f 20 22 6b 77 63 6f 6c 75 6d 6e 5f  14 */ "kwcolumn_
315f4 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 31  opt ::=",. /* 31
315f5 35 20 2a 2f 20 22 6b 77 63 6f 6c 75 6d 6e 5f 6f  5 */ "kwcolumn_o
315f6 70 74 20 3a 3a 3d 20 43 4f 4c 55 4d 4e 4b 57 22  pt ::= COLUMNKW"
315f7 2c 0a 20 2f 2a 20 33 31 36 20 2a 2f 20 22 63 6d  ,. /* 316 */ "cm
315f8 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74 61  d ::= create_vta
315f9 62 22 2c 0a 20 2f 2a 20 33 31 37 20 2a 2f 20 22  b",. /* 317 */ "
315fa 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76  cmd ::= create_v
315fb 74 61 62 20 4c 50 20 76 74 61 62 61 72 67 6c 69  tab LP vtabargli
315fc 73 74 20 52 50 22 2c 0a 20 2f 2a 20 33 31 38 20  st RP",. /* 318 
315fd 2a 2f 20 22 63 72 65 61 74 65 5f 76 74 61 62 20  */ "create_vtab 
315fe 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 56 49 52  ::= createkw VIR
315ff 54 55 41 4c 20 54 41 42 4c 45 20 6e 6d 20 64 62  TUAL TABLE nm db
31600 6e 6d 20 55 53 49 4e 47 20 6e 6d 22 2c 0a 20 2f  nm USING nm",. /
31601 2a 20 33 31 39 20 2a 2f 20 22 76 74 61 62 61 72  * 319 */ "vtabar
31602 67 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61 72  glist ::= vtabar
31603 67 22 2c 0a 20 2f 2a 20 33 32 30 20 2a 2f 20 22  g",. /* 320 */ "
31604 76 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20  vtabarglist ::= 
31605 76 74 61 62 61 72 67 6c 69 73 74 20 43 4f 4d 4d  vtabarglist COMM
31606 41 20 76 74 61 62 61 72 67 22 2c 0a 20 2f 2a 20  A vtabarg",. /* 
31607 33 32 31 20 2a 2f 20 22 76 74 61 62 61 72 67 20  321 */ "vtabarg 
31608 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 32 32 20 2a 2f  ::=",. /* 322 */
31609 20 22 76 74 61 62 61 72 67 20 3a 3a 3d 20 76 74   "vtabarg ::= vt
3160a 61 62 61 72 67 20 76 74 61 62 61 72 67 74 6f 6b  abarg vtabargtok
3160b 65 6e 22 2c 0a 20 2f 2a 20 33 32 33 20 2a 2f 20  en",. /* 323 */ 
3160c 22 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a  "vtabargtoken ::
3160d 3d 20 41 4e 59 22 2c 0a 20 2f 2a 20 33 32 34 20  = ANY",. /* 324 
3160e 2a 2f 20 22 76 74 61 62 61 72 67 74 6f 6b 65 6e  */ "vtabargtoken
3160f 20 3a 3a 3d 20 6c 70 20 61 6e 79 6c 69 73 74 20   ::= lp anylist 
31610 52 50 22 2c 0a 20 2f 2a 20 33 32 35 20 2a 2f 20  RP",. /* 325 */ 
31611 22 6c 70 20 3a 3a 3d 20 4c 50 22 2c 0a 20 2f 2a  "lp ::= LP",. /*
31612 20 33 32 36 20 2a 2f 20 22 61 6e 79 6c 69 73 74   326 */ "anylist
31613 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 32 37 20 2a   ::=",. /* 327 *
31614 2f 20 22 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61  / "anylist ::= a
31615 6e 79 6c 69 73 74 20 4c 50 20 61 6e 79 6c 69 73  nylist LP anylis
31616 74 20 52 50 22 2c 0a 20 2f 2a 20 33 32 38 20 2a  t RP",. /* 328 *
31617 2f 20 22 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61  / "anylist ::= a
31618 6e 79 6c 69 73 74 20 41 4e 59 22 2c 0a 7d 3b 0a  nylist ANY",.};.
31619 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47  #endif /* NDEBUG
3161a 20 2a 2f 0a 0a 0a 23 69 66 20 59 59 53 54 41 43   */...#if YYSTAC
3161b 4b 44 45 50 54 48 3c 3d 30 0a 2f 2a 0a 2a 2a 20  KDEPTH<=0./*.** 
3161c 54 72 79 20 74 6f 20 69 6e 63 72 65 61 73 65 20  Try to increase 
3161d 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
3161e 70 61 72 73 65 72 20 73 74 61 63 6b 2e 0a 2a 2f  parser stack..*/
3161f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 47  .static void yyG
31620 72 6f 77 53 74 61 63 6b 28 79 79 50 61 72 73 65  rowStack(yyParse
31621 72 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 65 77  r *p){.  int new
31622 53 69 7a 65 3b 0a 20 20 79 79 53 74 61 63 6b 45  Size;.  yyStackE
31623 6e 74 72 79 20 2a 70 4e 65 77 3b 0a 0a 20 20 6e  ntry *pNew;..  n
31624 65 77 53 69 7a 65 20 3d 20 70 2d 3e 79 79 73 74  ewSize = p->yyst
31625 6b 73 7a 2a 32 20 2b 20 31 30 30 3b 0a 20 20 70  ksz*2 + 100;.  p
31626 4e 65 77 20 3d 20 72 65 61 6c 6c 6f 63 28 70 2d  New = realloc(p-
31627 3e 79 79 73 74 61 63 6b 2c 20 6e 65 77 53 69 7a  >yystack, newSiz
31628 65 2a 73 69 7a 65 6f 66 28 70 4e 65 77 5b 30 5d  e*sizeof(pNew[0]
31629 29 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 20 29  ));.  if( pNew )
3162a 7b 0a 20 20 20 20 70 2d 3e 79 79 73 74 61 63 6b  {.    p->yystack
3162b 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 70 2d 3e   = pNew;.    p->
3162c 79 79 73 74 6b 73 7a 20 3d 20 6e 65 77 53 69 7a  yystksz = newSiz
3162d 65 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  e;.#ifndef NDEBU
3162e 47 0a 20 20 20 20 69 66 28 20 79 79 54 72 61 63  G.    if( yyTrac
3162f 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 66  eFILE ){.      f
31630 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49  printf(yyTraceFI
31631 4c 45 2c 22 25 73 53 74 61 63 6b 20 67 72 6f 77  LE,"%sStack grow
31632 73 20 74 6f 20 25 64 20 65 6e 74 72 69 65 73 21  s to %d entries!
31633 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
31634 20 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74     yyTracePrompt
31635 2c 20 70 2d 3e 79 79 73 74 6b 73 7a 29 3b 0a 20  , p->yystksz);. 
31636 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
31637 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 0a 2a 2a  }.#endif../* .**
31638 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   This function a
31639 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 20 70  llocates a new p
3163a 61 72 73 65 72 2e 0a 2a 2a 20 54 68 65 20 6f 6e  arser..** The on
3163b 6c 79 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  ly argument is a
3163c 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75   pointer to a fu
3163d 6e 63 74 69 6f 6e 20 77 68 69 63 68 20 77 6f 72  nction which wor
3163e 6b 73 20 6c 69 6b 65 0a 2a 2a 20 6d 61 6c 6c 6f  ks like.** mallo
3163f 63 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a  c..**.** Inputs:
31640 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  .** A pointer to
31641 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 75 73   the function us
31642 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d  ed to allocate m
31643 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 4f 75 74  emory..**.** Out
31644 70 75 74 73 3a 0a 2a 2a 20 41 20 70 6f 69 6e 74  puts:.** A point
31645 65 72 20 74 6f 20 61 20 70 61 72 73 65 72 2e 20  er to a parser. 
31646 20 54 68 69 73 20 70 6f 69 6e 74 65 72 20 69 73   This pointer is
31647 20 75 73 65 64 20 69 6e 20 73 75 62 73 65 71 75   used in subsequ
31648 65 6e 74 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20  ent calls.** to 
31649 73 71 6c 69 74 65 33 50 61 72 73 65 72 20 61 6e  sqlite3Parser an
3164a 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 46  d sqlite3ParserF
3164b 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ree..*/.SQLITE_P
3164c 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
3164d 69 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 28  ite3ParserAlloc(
3164e 76 6f 69 64 20 2a 28 2a 6d 61 6c 6c 6f 63 50 72  void *(*mallocPr
3164f 6f 63 29 28 73 69 7a 65 5f 74 29 29 7b 0a 20 20  oc)(size_t)){.  
31650 79 79 50 61 72 73 65 72 20 2a 70 50 61 72 73 65  yyParser *pParse
31651 72 3b 0a 20 20 70 50 61 72 73 65 72 20 3d 20 28  r;.  pParser = (
31652 79 79 50 61 72 73 65 72 2a 29 28 2a 6d 61 6c 6c  yyParser*)(*mall
31653 6f 63 50 72 6f 63 29 28 20 28 73 69 7a 65 5f 74  ocProc)( (size_t
31654 29 73 69 7a 65 6f 66 28 79 79 50 61 72 73 65 72  )sizeof(yyParser
31655 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73  ) );.  if( pPars
31656 65 72 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65  er ){.    pParse
31657 72 2d 3e 79 79 69 64 78 20 3d 20 2d 31 3b 0a 23  r->yyidx = -1;.#
31658 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58  ifdef YYTRACKMAX
31659 53 54 41 43 4b 44 45 50 54 48 0a 20 20 20 20 70  STACKDEPTH.    p
3165a 50 61 72 73 65 72 2d 3e 79 79 69 64 78 4d 61 78  Parser->yyidxMax
3165b 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66   = 0;.#endif.#if
3165c 20 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30   YYSTACKDEPTH<=0
3165d 0a 20 20 20 20 70 50 61 72 73 65 72 2d 3e 79 79  .    pParser->yy
3165e 73 74 61 63 6b 20 3d 20 4e 55 4c 4c 3b 0a 20 20  stack = NULL;.  
3165f 20 20 70 50 61 72 73 65 72 2d 3e 79 79 73 74 6b    pParser->yystk
31660 73 7a 20 3d 20 30 3b 0a 20 20 20 20 79 79 47 72  sz = 0;.    yyGr
31661 6f 77 53 74 61 63 6b 28 70 50 61 72 73 65 72 29  owStack(pParser)
31662 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72  ;.#endif.  }.  r
31663 65 74 75 72 6e 20 70 50 61 72 73 65 72 3b 0a 7d  eturn pParser;.}
31664 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ../* The followi
31665 6e 67 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65  ng function dele
31666 74 65 73 20 74 68 65 20 76 61 6c 75 65 20 61 73  tes the value as
31667 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 0a  sociated with a.
31668 2a 2a 20 73 79 6d 62 6f 6c 2e 20 20 54 68 65 20  ** symbol.  The 
31669 73 79 6d 62 6f 6c 20 63 61 6e 20 62 65 20 65 69  symbol can be ei
3166a 74 68 65 72 20 61 20 74 65 72 6d 69 6e 61 6c 20  ther a terminal 
3166b 6f 72 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 2e 0a  or nonterminal..
3166c 2a 2a 20 22 79 79 6d 61 6a 6f 72 22 20 69 73 20  ** "yymajor" is 
3166d 74 68 65 20 73 79 6d 62 6f 6c 20 63 6f 64 65 2c  the symbol code,
3166e 20 61 6e 64 20 22 79 79 70 6d 69 6e 6f 72 22 20   and "yypminor" 
3166f 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a  is a pointer to.
31670 2a 2a 20 74 68 65 20 76 61 6c 75 65 2e 0a 2a 2f  ** the value..*/
31671 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f  .static void yy_
31672 64 65 73 74 72 75 63 74 6f 72 28 0a 20 20 79 79  destructor(.  yy
31673 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65  Parser *yypParse
31674 72 2c 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72  r,    /* The par
31675 73 65 72 20 2a 2f 0a 20 20 59 59 43 4f 44 45 54  ser */.  YYCODET
31676 59 50 45 20 79 79 6d 61 6a 6f 72 2c 20 20 20 20  YPE yymajor,    
31677 20 2f 2a 20 54 79 70 65 20 63 6f 64 65 20 66 6f   /* Type code fo
31678 72 20 6f 62 6a 65 63 74 20 74 6f 20 64 65 73 74  r object to dest
31679 72 6f 79 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52  roy */.  YYMINOR
3167a 54 59 50 45 20 2a 79 79 70 6d 69 6e 6f 72 20 20  TYPE *yypminor  
3167b 20 2f 2a 20 54 68 65 20 6f 62 6a 65 63 74 20 74   /* The object t
3167c 6f 20 62 65 20 64 65 73 74 72 6f 79 65 64 20 2a  o be destroyed *
3167d 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 61  /.){.  sqlite3Pa
3167e 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 20  rserARG_FETCH;. 
3167f 20 73 77 69 74 63 68 28 20 79 79 6d 61 6a 6f 72   switch( yymajor
31680 20 29 7b 0a 20 20 20 20 2f 2a 20 48 65 72 65 20   ){.    /* Here 
31681 69 73 20 69 6e 73 65 72 74 65 64 20 74 68 65 20  is inserted the 
31682 61 63 74 69 6f 6e 73 20 77 68 69 63 68 20 74 61  actions which ta
31683 6b 65 20 70 6c 61 63 65 20 77 68 65 6e 20 61 0a  ke place when a.
31684 20 20 20 20 2a 2a 20 74 65 72 6d 69 6e 61 6c 20      ** terminal 
31685 6f 72 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 20  or non-terminal 
31686 69 73 20 64 65 73 74 72 6f 79 65 64 2e 20 20 54  is destroyed.  T
31687 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 0a 20  his can happen. 
31688 20 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 73     ** when the s
31689 79 6d 62 6f 6c 20 69 73 20 70 6f 70 70 65 64 20  ymbol is popped 
3168a 66 72 6f 6d 20 74 68 65 20 73 74 61 63 6b 20 64  from the stack d
3168b 75 72 69 6e 67 20 61 0a 20 20 20 20 2a 2a 20 72  uring a.    ** r
3168c 65 64 75 63 65 20 6f 72 20 64 75 72 69 6e 67 20  educe or during 
3168d 65 72 72 6f 72 20 70 72 6f 63 65 73 73 69 6e 67  error processing
3168e 20 6f 72 20 77 68 65 6e 20 61 20 70 61 72 73 65   or when a parse
3168f 72 20 69 73 20 0a 20 20 20 20 2a 2a 20 62 65 69  r is .    ** bei
31690 6e 67 20 64 65 73 74 72 6f 79 65 64 20 62 65 66  ng destroyed bef
31691 6f 72 65 20 69 74 20 69 73 20 66 69 6e 69 73 68  ore it is finish
31692 65 64 20 70 61 72 73 69 6e 67 2e 0a 20 20 20 20  ed parsing..    
31693 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 3a 20  **.    ** Note: 
31694 64 75 72 69 6e 67 20 61 20 72 65 64 75 63 65 2c  during a reduce,
31695 20 74 68 65 20 6f 6e 6c 79 20 73 79 6d 62 6f 6c   the only symbol
31696 73 20 64 65 73 74 72 6f 79 65 64 20 61 72 65 20  s destroyed are 
31697 74 68 6f 73 65 0a 20 20 20 20 2a 2a 20 77 68 69  those.    ** whi
31698 63 68 20 61 70 70 65 61 72 20 6f 6e 20 74 68 65  ch appear on the
31699 20 52 48 53 20 6f 66 20 74 68 65 20 72 75 6c 65   RHS of the rule
3169a 2c 20 62 75 74 20 77 68 69 63 68 20 61 72 65 20  , but which are 
3169b 6e 6f 74 20 75 73 65 64 0a 20 20 20 20 2a 2a 20  not used.    ** 
3169c 69 6e 73 69 64 65 20 74 68 65 20 43 20 63 6f 64  inside the C cod
3169d 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  e..    */.    ca
3169e 73 65 20 31 36 30 3a 20 2f 2a 20 73 65 6c 65 63  se 160: /* selec
3169f 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 39  t */.    case 19
316a0 34 3a 20 2f 2a 20 6f 6e 65 73 65 6c 65 63 74 20  4: /* oneselect 
316a1 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 53 65 6c 65  */.{.sqlite3Sele
316a2 63 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  ctDelete(pParse-
316a3 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e  >db, (yypminor->
316a4 79 79 33 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62  yy3));.}.      b
316a5 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 31  reak;.    case 1
316a6 37 34 3a 20 2f 2a 20 74 65 72 6d 20 2a 2f 0a 20  74: /* term */. 
316a7 20 20 20 63 61 73 65 20 31 37 35 3a 20 2f 2a 20     case 175: /* 
316a8 65 78 70 72 20 2a 2f 0a 20 20 20 20 63 61 73 65  expr */.    case
316a9 20 32 32 33 3a 20 2f 2a 20 65 73 63 61 70 65 20   223: /* escape 
316aa 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70 72  */.{.sqlite3Expr
316ab 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
316ac 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79  b, (yypminor->yy
316ad 33 34 36 29 2e 70 45 78 70 72 29 3b 0a 7d 0a 20  346).pExpr);.}. 
316ae 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
316af 63 61 73 65 20 31 37 39 3a 20 2f 2a 20 69 64 78  case 179: /* idx
316b0 6c 69 73 74 5f 6f 70 74 20 2a 2f 0a 20 20 20 20  list_opt */.    
316b1 63 61 73 65 20 31 38 37 3a 20 2f 2a 20 69 64 78  case 187: /* idx
316b2 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  list */.    case
316b3 20 31 39 37 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c   197: /* selcoll
316b4 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ist */.    case 
316b5 32 30 30 3a 20 2f 2a 20 67 72 6f 75 70 62 79 5f  200: /* groupby_
316b6 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  opt */.    case 
316b7 32 30 32 3a 20 2f 2a 20 6f 72 64 65 72 62 79 5f  202: /* orderby_
316b8 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  opt */.    case 
316b9 32 30 34 3a 20 2f 2a 20 73 63 6c 70 20 2a 2f 0a  204: /* sclp */.
316ba 20 20 20 20 63 61 73 65 20 32 31 34 3a 20 2f 2a      case 214: /*
316bb 20 73 6f 72 74 6c 69 73 74 20 2a 2f 0a 20 20 20   sortlist */.   
316bc 20 63 61 73 65 20 32 31 36 3a 20 2f 2a 20 6e 65   case 216: /* ne
316bd 78 70 72 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63  xprlist */.    c
316be 61 73 65 20 32 31 37 3a 20 2f 2a 20 73 65 74 6c  ase 217: /* setl
316bf 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ist */.    case 
316c0 32 32 30 3a 20 2f 2a 20 69 74 65 6d 6c 69 73 74  220: /* itemlist
316c1 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 32 31   */.    case 221
316c2 3a 20 2f 2a 20 65 78 70 72 6c 69 73 74 20 2a 2f  : /* exprlist */
316c3 0a 20 20 20 20 63 61 73 65 20 32 32 37 3a 20 2f  .    case 227: /
316c4 2a 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20  * case_exprlist 
316c5 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70 72  */.{.sqlite3Expr
316c6 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73  ListDelete(pPars
316c7 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72  e->db, (yypminor
316c8 2d 3e 79 79 31 34 29 29 3b 0a 7d 0a 20 20 20 20  ->yy14));.}.    
316c9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
316ca 65 20 31 39 33 3a 20 2f 2a 20 66 75 6c 6c 6e 61  e 193: /* fullna
316cb 6d 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31  me */.    case 1
316cc 39 38 3a 20 2f 2a 20 66 72 6f 6d 20 2a 2f 0a 20  98: /* from */. 
316cd 20 20 20 63 61 73 65 20 32 30 36 3a 20 2f 2a 20     case 206: /* 
316ce 73 65 6c 74 61 62 6c 69 73 74 20 2a 2f 0a 20 20  seltablist */.  
316cf 20 20 63 61 73 65 20 32 30 37 3a 20 2f 2a 20 73    case 207: /* s
316d0 74 6c 5f 70 72 65 66 69 78 20 2a 2f 0a 7b 0a 73  tl_prefix */.{.s
316d1 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c  qlite3SrcListDel
316d2 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
316d3 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 36 35 29  (yypminor->yy65)
316d4 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b  );.}.      break
316d5 3b 0a 20 20 20 20 63 61 73 65 20 31 39 39 3a 20  ;.    case 199: 
316d6 2f 2a 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a  /* where_opt */.
316d7 20 20 20 20 63 61 73 65 20 32 30 31 3a 20 2f 2a      case 201: /*
316d8 20 68 61 76 69 6e 67 5f 6f 70 74 20 2a 2f 0a 20   having_opt */. 
316d9 20 20 20 63 61 73 65 20 32 31 30 3a 20 2f 2a 20     case 210: /* 
316da 6f 6e 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61  on_opt */.    ca
316db 73 65 20 32 31 35 3a 20 2f 2a 20 73 6f 72 74 69  se 215: /* sorti
316dc 74 65 6d 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  tem */.    case 
316dd 32 32 36 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65  226: /* case_ope
316de 72 61 6e 64 20 2a 2f 0a 20 20 20 20 63 61 73 65  rand */.    case
316df 20 32 32 38 3a 20 2f 2a 20 63 61 73 65 5f 65 6c   228: /* case_el
316e0 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  se */.    case 2
316e1 33 39 3a 20 2f 2a 20 77 68 65 6e 5f 63 6c 61 75  39: /* when_clau
316e2 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  se */.    case 2
316e3 34 34 3a 20 2f 2a 20 6b 65 79 5f 6f 70 74 20 2a  44: /* key_opt *
316e4 2f 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70 72 44  /.{.sqlite3ExprD
316e5 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
316e6 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 31  , (yypminor->yy1
316e7 33 32 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72  32));.}.      br
316e8 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 32 31  eak;.    case 21
316e9 31 3a 20 2f 2a 20 75 73 69 6e 67 5f 6f 70 74 20  1: /* using_opt 
316ea 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 31 33 3a  */.    case 213:
316eb 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 20 2a   /* inscollist *
316ec 2f 0a 20 20 20 20 63 61 73 65 20 32 31 39 3a 20  /.    case 219: 
316ed 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70  /* inscollist_op
316ee 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 49 64  t */.{.sqlite3Id
316ef 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73  ListDelete(pPars
316f0 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72  e->db, (yypminor
316f1 2d 3e 79 79 34 30 38 29 29 3b 0a 7d 0a 20 20 20  ->yy408));.}.   
316f2 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
316f3 73 65 20 32 33 35 3a 20 2f 2a 20 74 72 69 67 67  se 235: /* trigg
316f4 65 72 5f 63 6d 64 5f 6c 69 73 74 20 2a 2f 0a 20  er_cmd_list */. 
316f5 20 20 20 63 61 73 65 20 32 34 30 3a 20 2f 2a 20     case 240: /* 
316f6 74 72 69 67 67 65 72 5f 63 6d 64 20 2a 2f 0a 7b  trigger_cmd */.{
316f7 0a 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72  .sqlite3DeleteTr
316f8 69 67 67 65 72 53 74 65 70 28 70 50 61 72 73 65  iggerStep(pParse
316f9 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d  ->db, (yypminor-
316fa 3e 79 79 34 37 33 29 29 3b 0a 7d 0a 20 20 20 20  >yy473));.}.    
316fb 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
316fc 65 20 32 33 37 3a 20 2f 2a 20 74 72 69 67 67 65  e 237: /* trigge
316fd 72 5f 65 76 65 6e 74 20 2a 2f 0a 7b 0a 73 71 6c  r_event */.{.sql
316fe 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
316ff 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79  (pParse->db, (yy
31700 70 6d 69 6e 6f 72 2d 3e 79 79 33 37 38 29 2e 62  pminor->yy378).b
31701 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b  );.}.      break
31702 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20  ;.    default:  
31703 62 72 65 61 6b 3b 20 20 20 2f 2a 20 49 66 20 6e  break;   /* If n
31704 6f 20 64 65 73 74 72 75 63 74 6f 72 20 61 63 74  o destructor act
31705 69 6f 6e 20 73 70 65 63 69 66 69 65 64 3a 20 64  ion specified: d
31706 6f 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 7d  o nothing */.  }
31707 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 20 74 68  .}../*.** Pop th
31708 65 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b  e parser's stack
31709 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   once..**.** If 
3170a 74 68 65 72 65 20 69 73 20 61 20 64 65 73 74 72  there is a destr
3170b 75 63 74 6f 72 20 72 6f 75 74 69 6e 65 20 61 73  uctor routine as
3170c 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
3170d 65 20 74 6f 6b 65 6e 20 77 68 69 63 68 0a 2a 2a  e token which.**
3170e 20 69 73 20 70 6f 70 70 65 64 20 66 72 6f 6d 20   is popped from 
3170f 74 68 65 20 73 74 61 63 6b 2c 20 74 68 65 6e 20  the stack, then 
31710 63 61 6c 6c 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 52  call it..**.** R
31711 65 74 75 72 6e 20 74 68 65 20 6d 61 6a 6f 72 20  eturn the major 
31712 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 20 66 6f 72  token number for
31713 20 74 68 65 20 73 79 6d 62 6f 6c 20 70 6f 70 70   the symbol popp
31714 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
31715 74 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f  t yy_pop_parser_
31716 73 74 61 63 6b 28 79 79 50 61 72 73 65 72 20 2a  stack(yyParser *
31717 70 50 61 72 73 65 72 29 7b 0a 20 20 59 59 43 4f  pParser){.  YYCO
31718 44 45 54 59 50 45 20 79 79 6d 61 6a 6f 72 3b 0a  DETYPE yymajor;.
31719 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a    yyStackEntry *
3171a 79 79 74 6f 73 20 3d 20 26 70 50 61 72 73 65 72  yytos = &pParser
3171b 2d 3e 79 79 73 74 61 63 6b 5b 70 50 61 72 73 65  ->yystack[pParse
3171c 72 2d 3e 79 79 69 64 78 5d 3b 0a 0a 20 20 2f 2a  r->yyidx];..  /*
3171d 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6d 65 63   There is no mec
3171e 68 61 6e 69 73 6d 20 62 79 20 77 68 69 63 68 20  hanism by which 
3171f 74 68 65 20 70 61 72 73 65 72 20 73 74 61 63 6b  the parser stack
31720 20 63 61 6e 20 62 65 20 70 6f 70 70 65 64 20 62   can be popped b
31721 65 6c 6f 77 0a 20 20 2a 2a 20 65 6d 70 74 79 20  elow.  ** empty 
31722 69 6e 20 53 51 4c 69 74 65 2e 20 20 2a 2f 0a 20  in SQLite.  */. 
31723 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 72 73   if( NEVER(pPars
31724 65 72 2d 3e 79 79 69 64 78 3c 30 29 20 29 20 72  er->yyidx<0) ) r
31725 65 74 75 72 6e 20 30 3b 0a 23 69 66 6e 64 65 66  eturn 0;.#ifndef
31726 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79   NDEBUG.  if( yy
31727 54 72 61 63 65 46 49 4c 45 20 26 26 20 70 50 61  TraceFILE && pPa
31728 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29  rser->yyidx>=0 )
31729 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79 79  {.    fprintf(yy
3172a 54 72 61 63 65 46 49 4c 45 2c 22 25 73 50 6f 70  TraceFILE,"%sPop
3172b 70 69 6e 67 20 25 73 5c 6e 22 2c 0a 20 20 20 20  ping %s\n",.    
3172c 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c    yyTracePrompt,
3172d 0a 20 20 20 20 20 20 79 79 54 6f 6b 65 6e 4e 61  .      yyTokenNa
3172e 6d 65 5b 79 79 74 6f 73 2d 3e 6d 61 6a 6f 72 5d  me[yytos->major]
3172f 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
31730 79 79 6d 61 6a 6f 72 20 3d 20 79 79 74 6f 73 2d  yymajor = yytos-
31731 3e 6d 61 6a 6f 72 3b 0a 20 20 79 79 5f 64 65 73  >major;.  yy_des
31732 74 72 75 63 74 6f 72 28 70 50 61 72 73 65 72 2c  tructor(pParser,
31733 20 79 79 6d 61 6a 6f 72 2c 20 26 79 79 74 6f 73   yymajor, &yytos
31734 2d 3e 6d 69 6e 6f 72 29 3b 0a 20 20 70 50 61 72  ->minor);.  pPar
31735 73 65 72 2d 3e 79 79 69 64 78 2d 2d 3b 0a 20 20  ser->yyidx--;.  
31736 72 65 74 75 72 6e 20 79 79 6d 61 6a 6f 72 3b 0a  return yymajor;.
31737 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44 65 61 6c 6c 6f  }../* .** Deallo
31738 63 61 74 65 20 61 6e 64 20 64 65 73 74 72 6f 79  cate and destroy
31739 20 61 20 70 61 72 73 65 72 2e 20 20 44 65 73 74   a parser.  Dest
3173a 72 75 63 74 6f 72 73 20 61 72 65 20 61 6c 6c 20  ructors are all 
3173b 63 61 6c 6c 65 64 20 66 6f 72 0a 2a 2a 20 61 6c  called for.** al
3173c 6c 20 73 74 61 63 6b 20 65 6c 65 6d 65 6e 74 73  l stack elements
3173d 20 62 65 66 6f 72 65 20 73 68 75 74 74 69 6e 67   before shutting
3173e 20 74 68 65 20 70 61 72 73 65 72 20 64 6f 77 6e   the parser down
3173f 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a  ..**.** Inputs:.
31740 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20  ** <ul>.** <li> 
31741 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
31742 65 20 70 61 72 73 65 72 2e 20 20 54 68 69 73 20  e parser.  This 
31743 73 68 6f 75 6c 64 20 62 65 20 61 20 70 6f 69 6e  should be a poin
31744 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 6f 62 74  ter.**       obt
31745 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
31746 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 2e 0a 2a  e3ParserAlloc..*
31747 2a 20 3c 6c 69 3e 20 20 41 20 70 6f 69 6e 74 65  * <li>  A pointe
31748 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20  r to a function 
31749 75 73 65 64 20 74 6f 20 72 65 63 6c 61 69 6d 20  used to reclaim 
3174a 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a  memory obtained.
3174b 2a 2a 20 20 20 20 20 20 20 66 72 6f 6d 20 6d 61  **       from ma
3174c 6c 6c 6f 63 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a  lloc..** </ul>.*
3174d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
3174e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72   void sqlite3Par
3174f 73 65 72 46 72 65 65 28 0a 20 20 76 6f 69 64 20  serFree(.  void 
31750 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
31751 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
31752 72 73 65 72 20 74 6f 20 62 65 20 64 65 6c 65 74  rser to be delet
31753 65 64 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 66  ed */.  void (*f
31754 72 65 65 50 72 6f 63 29 28 76 6f 69 64 2a 29 20  reeProc)(void*) 
31755 20 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20      /* Function 
31756 75 73 65 64 20 74 6f 20 72 65 63 6c 61 69 6d 20  used to reclaim 
31757 6d 65 6d 6f 72 79 20 2a 2f 0a 29 7b 0a 20 20 79  memory */.){.  y
31758 79 50 61 72 73 65 72 20 2a 70 50 61 72 73 65 72  yParser *pParser
31759 20 3d 20 28 79 79 50 61 72 73 65 72 2a 29 70 3b   = (yyParser*)p;
3175a 0a 20 20 2f 2a 20 49 6e 20 53 51 4c 69 74 65 2c  .  /* In SQLite,
3175b 20 77 65 20 6e 65 76 65 72 20 74 72 79 20 74 6f   we never try to
3175c 20 64 65 73 74 72 6f 79 20 61 20 70 61 72 73 65   destroy a parse
3175d 72 20 74 68 61 74 20 77 61 73 20 6e 6f 74 20 73  r that was not s
3175e 75 63 63 65 73 73 66 75 6c 6c 79 0a 20 20 2a 2a  uccessfully.  **
3175f 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20   created in the 
31760 66 69 72 73 74 20 70 6c 61 63 65 2e 20 2a 2f 0a  first place. */.
31761 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 72    if( NEVER(pPar
31762 73 65 72 3d 3d 30 29 20 29 20 72 65 74 75 72 6e  ser==0) ) return
31763 3b 0a 20 20 77 68 69 6c 65 28 20 70 50 61 72 73  ;.  while( pPars
31764 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79  er->yyidx>=0 ) y
31765 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61  y_pop_parser_sta
31766 63 6b 28 70 50 61 72 73 65 72 29 3b 0a 23 69 66  ck(pParser);.#if
31767 20 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30   YYSTACKDEPTH<=0
31768 0a 20 20 66 72 65 65 28 70 50 61 72 73 65 72 2d  .  free(pParser-
31769 3e 79 79 73 74 61 63 6b 29 3b 0a 23 65 6e 64 69  >yystack);.#endi
3176a 66 0a 20 20 28 2a 66 72 65 65 50 72 6f 63 29 28  f.  (*freeProc)(
3176b 28 76 6f 69 64 2a 29 70 50 61 72 73 65 72 29 3b  (void*)pParser);
3176c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
3176d 20 74 68 65 20 70 65 61 6b 20 64 65 70 74 68 20   the peak depth 
3176e 6f 66 20 74 68 65 20 73 74 61 63 6b 20 66 6f 72  of the stack for
3176f 20 61 20 70 61 72 73 65 72 2e 0a 2a 2f 0a 23 69   a parser..*/.#i
31770 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53  fdef YYTRACKMAXS
31771 54 41 43 4b 44 45 50 54 48 0a 53 51 4c 49 54 45  TACKDEPTH.SQLITE
31772 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
31773 69 74 65 33 50 61 72 73 65 72 53 74 61 63 6b 50  ite3ParserStackP
31774 65 61 6b 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20  eak(void *p){.  
31775 79 79 50 61 72 73 65 72 20 2a 70 50 61 72 73 65  yyParser *pParse
31776 72 20 3d 20 28 79 79 50 61 72 73 65 72 2a 29 70  r = (yyParser*)p
31777 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72 73  ;.  return pPars
31778 65 72 2d 3e 79 79 69 64 78 4d 61 78 3b 0a 7d 0a  er->yyidxMax;.}.
31779 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69  #endif../*.** Fi
3177a 6e 64 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  nd the appropria
3177b 74 65 20 61 63 74 69 6f 6e 20 66 6f 72 20 61 20  te action for a 
3177c 70 61 72 73 65 72 20 67 69 76 65 6e 20 74 68 65  parser given the
3177d 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 6c 6f 6f   terminal.** loo
3177e 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 69 4c  k-ahead token iL
3177f 6f 6f 6b 41 68 65 61 64 2e 0a 2a 2a 0a 2a 2a 20  ookAhead..**.** 
31780 49 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61  If the look-ahea
31781 64 20 74 6f 6b 65 6e 20 69 73 20 59 59 4e 4f 43  d token is YYNOC
31782 4f 44 45 2c 20 74 68 65 6e 20 63 68 65 63 6b 20  ODE, then check 
31783 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 61 63  to see if the ac
31784 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 6e 64 65 70  tion is.** indep
31785 65 6e 64 65 6e 74 20 6f 66 20 74 68 65 20 6c 6f  endent of the lo
31786 6f 6b 2d 61 68 65 61 64 2e 20 20 49 66 20 69 74  ok-ahead.  If it
31787 20 69 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20   is, return the 
31788 61 63 74 69 6f 6e 2c 20 6f 74 68 65 72 77 69 73  action, otherwis
31789 65 0a 2a 2a 20 72 65 74 75 72 6e 20 59 59 5f 4e  e.** return YY_N
3178a 4f 5f 41 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61  O_ACTION..*/.sta
3178b 74 69 63 20 69 6e 74 20 79 79 5f 66 69 6e 64 5f  tic int yy_find_
3178c 73 68 69 66 74 5f 61 63 74 69 6f 6e 28 0a 20 20  shift_action(.  
3178d 79 79 50 61 72 73 65 72 20 2a 70 50 61 72 73 65  yyParser *pParse
3178e 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  r,        /* The
3178f 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 59 59 43   parser */.  YYC
31790 4f 44 45 54 59 50 45 20 69 4c 6f 6f 6b 41 68 65  ODETYPE iLookAhe
31791 61 64 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f  ad     /* The lo
31792 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 2a  ok-ahead token *
31793 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
31794 69 6e 74 20 73 74 61 74 65 6e 6f 20 3d 20 70 50  int stateno = pP
31795 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 70  arser->yystack[p
31796 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 2e 73  Parser->yyidx].s
31797 74 61 74 65 6e 6f 3b 0a 20 0a 20 20 69 66 28 20  tateno;. .  if( 
31798 73 74 61 74 65 6e 6f 3e 59 59 5f 53 48 49 46 54  stateno>YY_SHIFT
31799 5f 4d 41 58 20 7c 7c 20 28 69 20 3d 20 79 79 5f  _MAX || (i = yy_
3179a 73 68 69 66 74 5f 6f 66 73 74 5b 73 74 61 74 65  shift_ofst[state
3179b 6e 6f 5d 29 3d 3d 59 59 5f 53 48 49 46 54 5f 55  no])==YY_SHIFT_U
3179c 53 45 5f 44 46 4c 54 20 29 7b 0a 20 20 20 20 72  SE_DFLT ){.    r
3179d 65 74 75 72 6e 20 79 79 5f 64 65 66 61 75 6c 74  eturn yy_default
3179e 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d 0a 20  [stateno];.  }. 
3179f 20 61 73 73 65 72 74 28 20 69 4c 6f 6f 6b 41 68   assert( iLookAh
317a0 65 61 64 21 3d 59 59 4e 4f 43 4f 44 45 20 29 3b  ead!=YYNOCODE );
317a1 0a 20 20 69 20 2b 3d 20 69 4c 6f 6f 6b 41 68 65  .  i += iLookAhe
317a2 61 64 3b 0a 20 20 69 66 28 20 69 3c 30 20 7c 7c  ad;.  if( i<0 ||
317a3 20 69 3e 3d 59 59 5f 53 5a 5f 41 43 54 54 41 42   i>=YY_SZ_ACTTAB
317a4 20 7c 7c 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64   || yy_lookahead
317a5 5b 69 5d 21 3d 69 4c 6f 6f 6b 41 68 65 61 64 20  [i]!=iLookAhead 
317a6 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 75 73  ){.    /* The us
317a7 65 72 20 6f 66 20 22 3b 22 20 69 6e 73 74 65 61  er of ";" instea
317a8 64 20 6f 66 20 22 5c 30 30 30 22 20 61 73 20 61  d of "\000" as a
317a9 20 73 74 61 74 65 6d 65 6e 74 20 74 65 72 6d 69   statement termi
317aa 6e 61 74 6f 72 20 69 6e 20 53 51 4c 69 74 65 0a  nator in SQLite.
317ab 20 20 20 20 2a 2a 20 6d 65 61 6e 73 20 74 68 61      ** means tha
317ac 74 20 77 65 20 61 6c 77 61 79 73 20 68 61 76 65  t we always have
317ad 20 61 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f   a look-ahead to
317ae 6b 65 6e 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ken. */.    if( 
317af 69 4c 6f 6f 6b 41 68 65 61 64 3e 30 20 29 7b 0a  iLookAhead>0 ){.
317b0 23 69 66 64 65 66 20 59 59 46 41 4c 4c 42 41 43  #ifdef YYFALLBAC
317b1 4b 0a 20 20 20 20 20 20 59 59 43 4f 44 45 54 59  K.      YYCODETY
317b2 50 45 20 69 46 61 6c 6c 62 61 63 6b 3b 20 20 20  PE iFallback;   
317b3 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c           /* Fall
317b4 62 61 63 6b 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  back token */.  
317b5 20 20 20 20 69 66 28 20 69 4c 6f 6f 6b 41 68 65      if( iLookAhe
317b6 61 64 3c 73 69 7a 65 6f 66 28 79 79 46 61 6c 6c  ad<sizeof(yyFall
317b7 62 61 63 6b 29 2f 73 69 7a 65 6f 66 28 79 79 46  back)/sizeof(yyF
317b8 61 6c 6c 62 61 63 6b 5b 30 5d 29 0a 20 20 20 20  allback[0]).    
317b9 20 20 20 20 20 20 20 20 20 26 26 20 28 69 46 61           && (iFa
317ba 6c 6c 62 61 63 6b 20 3d 20 79 79 46 61 6c 6c 62  llback = yyFallb
317bb 61 63 6b 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d 29  ack[iLookAhead])
317bc 21 3d 30 20 29 7b 0a 23 69 66 6e 64 65 66 20 4e  !=0 ){.#ifndef N
317bd 44 45 42 55 47 0a 20 20 20 20 20 20 20 20 69 66  DEBUG.        if
317be 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b  ( yyTraceFILE ){
317bf 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e  .          fprin
317c0 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 20  tf(yyTraceFILE, 
317c1 22 25 73 46 41 4c 4c 42 41 43 4b 20 25 73 20 3d  "%sFALLBACK %s =
317c2 3e 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20  > %s\n",.       
317c3 20 20 20 20 20 20 79 79 54 72 61 63 65 50 72 6f        yyTracePro
317c4 6d 70 74 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d 65  mpt, yyTokenName
317c5 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d 2c 20 79 79  [iLookAhead], yy
317c6 54 6f 6b 65 6e 4e 61 6d 65 5b 69 46 61 6c 6c 62  TokenName[iFallb
317c7 61 63 6b 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d  ack]);.        }
317c8 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
317c9 72 65 74 75 72 6e 20 79 79 5f 66 69 6e 64 5f 73  return yy_find_s
317ca 68 69 66 74 5f 61 63 74 69 6f 6e 28 70 50 61 72  hift_action(pPar
317cb 73 65 72 2c 20 69 46 61 6c 6c 62 61 63 6b 29 3b  ser, iFallback);
317cc 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
317cd 23 69 66 64 65 66 20 59 59 57 49 4c 44 43 41 52  #ifdef YYWILDCAR
317ce 44 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20  D.      {.      
317cf 20 20 69 6e 74 20 6a 20 3d 20 69 20 2d 20 69 4c    int j = i - iL
317d0 6f 6f 6b 41 68 65 61 64 20 2b 20 59 59 57 49 4c  ookAhead + YYWIL
317d1 44 43 41 52 44 3b 0a 20 20 20 20 20 20 20 20 69  DCARD;.        i
317d2 66 28 20 6a 3e 3d 30 20 26 26 20 6a 3c 59 59 5f  f( j>=0 && j<YY_
317d3 53 5a 5f 41 43 54 54 41 42 20 26 26 20 79 79 5f  SZ_ACTTAB && yy_
317d4 6c 6f 6f 6b 61 68 65 61 64 5b 6a 5d 3d 3d 59 59  lookahead[j]==YY
317d5 57 49 4c 44 43 41 52 44 20 29 7b 0a 23 69 66 6e  WILDCARD ){.#ifn
317d6 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 20  def NDEBUG.     
317d7 20 20 20 20 20 69 66 28 20 79 79 54 72 61 63 65       if( yyTrace
317d8 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 20 20  FILE ){.        
317d9 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72      fprintf(yyTr
317da 61 63 65 46 49 4c 45 2c 20 22 25 73 57 49 4c 44  aceFILE, "%sWILD
317db 43 41 52 44 20 25 73 20 3d 3e 20 25 73 5c 6e 22  CARD %s => %s\n"
317dc 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
317dd 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 20   yyTracePrompt, 
317de 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69 4c 6f 6f  yyTokenName[iLoo
317df 6b 41 68 65 61 64 5d 2c 20 79 79 54 6f 6b 65 6e  kAhead], yyToken
317e0 4e 61 6d 65 5b 59 59 57 49 4c 44 43 41 52 44 5d  Name[YYWILDCARD]
317e1 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 23  );.          }.#
317e2 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20  endif /* NDEBUG 
317e3 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  */.          ret
317e4 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e 5b 6a 5d  urn yy_action[j]
317e5 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
317e6 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 59 59    }.#endif /* YY
317e7 57 49 4c 44 43 41 52 44 20 2a 2f 0a 20 20 20 20  WILDCARD */.    
317e8 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f  }.    return yy_
317e9 64 65 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d  default[stateno]
317ea 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
317eb 65 74 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e 5b  eturn yy_action[
317ec 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  i];.  }.}../*.**
317ed 20 46 69 6e 64 20 74 68 65 20 61 70 70 72 6f 70   Find the approp
317ee 72 69 61 74 65 20 61 63 74 69 6f 6e 20 66 6f 72  riate action for
317ef 20 61 20 70 61 72 73 65 72 20 67 69 76 65 6e 20   a parser given 
317f0 74 68 65 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c  the non-terminal
317f1 0a 2a 2a 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74  .** look-ahead t
317f2 6f 6b 65 6e 20 69 4c 6f 6f 6b 41 68 65 61 64 2e  oken iLookAhead.
317f3 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f  .**.** If the lo
317f4 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 69  ok-ahead token i
317f5 73 20 59 59 4e 4f 43 4f 44 45 2c 20 74 68 65 6e  s YYNOCODE, then
317f6 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   check to see if
317f7 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 0a 2a   the action is.*
317f8 2a 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 6f 66  * independent of
317f9 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 2e   the look-ahead.
317fa 20 20 49 66 20 69 74 20 69 73 2c 20 72 65 74 75    If it is, retu
317fb 72 6e 20 74 68 65 20 61 63 74 69 6f 6e 2c 20 6f  rn the action, o
317fc 74 68 65 72 77 69 73 65 0a 2a 2a 20 72 65 74 75  therwise.** retu
317fd 72 6e 20 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e 2e  rn YY_NO_ACTION.
317fe 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 79  .*/.static int y
317ff 79 5f 66 69 6e 64 5f 72 65 64 75 63 65 5f 61 63  y_find_reduce_ac
31800 74 69 6f 6e 28 0a 20 20 69 6e 74 20 73 74 61 74  tion(.  int stat
31801 65 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20  eno,            
31802 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 74 61    /* Current sta
31803 74 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 59  te number */.  Y
31804 59 43 4f 44 45 54 59 50 45 20 69 4c 6f 6f 6b 41  YCODETYPE iLookA
31805 68 65 61 64 20 20 20 20 20 2f 2a 20 54 68 65 20  head     /* The 
31806 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e  look-ahead token
31807 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
31808 23 69 66 64 65 66 20 59 59 45 52 52 4f 52 53 59  #ifdef YYERRORSY
31809 4d 42 4f 4c 0a 20 20 69 66 28 20 73 74 61 74 65  MBOL.  if( state
3180a 6e 6f 3e 59 59 5f 52 45 44 55 43 45 5f 4d 41 58  no>YY_REDUCE_MAX
3180b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 79   ){.    return y
3180c 79 5f 64 65 66 61 75 6c 74 5b 73 74 61 74 65 6e  y_default[staten
3180d 6f 5d 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20  o];.  }.#else.  
3180e 61 73 73 65 72 74 28 20 73 74 61 74 65 6e 6f 3c  assert( stateno<
3180f 3d 59 59 5f 52 45 44 55 43 45 5f 4d 41 58 20 29  =YY_REDUCE_MAX )
31810 3b 0a 23 65 6e 64 69 66 0a 20 20 69 20 3d 20 79  ;.#endif.  i = y
31811 79 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b 73 74  y_reduce_ofst[st
31812 61 74 65 6e 6f 5d 3b 0a 20 20 61 73 73 65 72 74  ateno];.  assert
31813 28 20 69 21 3d 59 59 5f 52 45 44 55 43 45 5f 55  ( i!=YY_REDUCE_U
31814 53 45 5f 44 46 4c 54 20 29 3b 0a 20 20 61 73 73  SE_DFLT );.  ass
31815 65 72 74 28 20 69 4c 6f 6f 6b 41 68 65 61 64 21  ert( iLookAhead!
31816 3d 59 59 4e 4f 43 4f 44 45 20 29 3b 0a 20 20 69  =YYNOCODE );.  i
31817 20 2b 3d 20 69 4c 6f 6f 6b 41 68 65 61 64 3b 0a   += iLookAhead;.
31818 23 69 66 64 65 66 20 59 59 45 52 52 4f 52 53 59  #ifdef YYERRORSY
31819 4d 42 4f 4c 0a 20 20 69 66 28 20 69 3c 30 20 7c  MBOL.  if( i<0 |
3181a 7c 20 69 3e 3d 59 59 5f 53 5a 5f 41 43 54 54 41  | i>=YY_SZ_ACTTA
3181b 42 20 7c 7c 20 79 79 5f 6c 6f 6f 6b 61 68 65 61  B || yy_lookahea
3181c 64 5b 69 5d 21 3d 69 4c 6f 6f 6b 41 68 65 61 64  d[i]!=iLookAhead
3181d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 79   ){.    return y
3181e 79 5f 64 65 66 61 75 6c 74 5b 73 74 61 74 65 6e  y_default[staten
3181f 6f 5d 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20  o];.  }.#else.  
31820 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
31821 69 3c 59 59 5f 53 5a 5f 41 43 54 54 41 42 20 29  i<YY_SZ_ACTTAB )
31822 3b 0a 20 20 61 73 73 65 72 74 28 20 79 79 5f 6c  ;.  assert( yy_l
31823 6f 6f 6b 61 68 65 61 64 5b 69 5d 3d 3d 69 4c 6f  ookahead[i]==iLo
31824 6f 6b 41 68 65 61 64 20 29 3b 0a 23 65 6e 64 69  okAhead );.#endi
31825 66 0a 20 20 72 65 74 75 72 6e 20 79 79 5f 61 63  f.  return yy_ac
31826 74 69 6f 6e 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a  tion[i];.}../*.*
31827 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
31828 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
31829 64 20 69 66 20 74 68 65 20 73 74 61 63 6b 20 6f  d if the stack o
3182a 76 65 72 66 6c 6f 77 73 2e 0a 2a 2f 0a 73 74 61  verflows..*/.sta
3182b 74 69 63 20 76 6f 69 64 20 79 79 53 74 61 63 6b  tic void yyStack
3182c 4f 76 65 72 66 6c 6f 77 28 79 79 50 61 72 73 65  Overflow(yyParse
3182d 72 20 2a 79 79 70 50 61 72 73 65 72 2c 20 59 59  r *yypParser, YY
3182e 4d 49 4e 4f 52 54 59 50 45 20 2a 79 79 70 4d 69  MINORTYPE *yypMi
3182f 6e 6f 72 29 7b 0a 20 20 20 73 71 6c 69 74 65 33  nor){.   sqlite3
31830 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b  ParserARG_FETCH;
31831 0a 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79  .   yypParser->y
31832 79 69 64 78 2d 2d 3b 0a 23 69 66 6e 64 65 66 20  yidx--;.#ifndef 
31833 4e 44 45 42 55 47 0a 20 20 20 69 66 28 20 79 79  NDEBUG.   if( yy
31834 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20  TraceFILE ){.   
31835 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
31836 65 46 49 4c 45 2c 22 25 73 53 74 61 63 6b 20 4f  eFILE,"%sStack O
31837 76 65 72 66 6c 6f 77 21 5c 6e 22 2c 79 79 54 72  verflow!\n",yyTr
31838 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20 7d  acePrompt);.   }
31839 0a 23 65 6e 64 69 66 0a 20 20 20 77 68 69 6c 65  .#endif.   while
3183a 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  ( yypParser->yyi
3183b 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70  dx>=0 ) yy_pop_p
3183c 61 72 73 65 72 5f 73 74 61 63 6b 28 79 79 70 50  arser_stack(yypP
3183d 61 72 73 65 72 29 3b 0a 20 20 20 2f 2a 20 48 65  arser);.   /* He
3183e 72 65 20 63 6f 64 65 20 69 73 20 69 6e 73 65 72  re code is inser
3183f 74 65 64 20 77 68 69 63 68 20 77 69 6c 6c 20 65  ted which will e
31840 78 65 63 75 74 65 20 69 66 20 74 68 65 20 70 61  xecute if the pa
31841 72 73 65 72 0a 20 20 20 2a 2a 20 73 74 61 63 6b  rser.   ** stack
31842 20 65 76 65 72 79 20 6f 76 65 72 66 6c 6f 77 73   every overflows
31843 20 2a 2f 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41   */..  UNUSED_PA
31844 52 41 4d 45 54 45 52 28 79 79 70 4d 69 6e 6f 72  RAMETER(yypMinor
31845 29 3b 20 2f 2a 20 53 69 6c 65 6e 63 65 20 73 6f  ); /* Silence so
31846 6d 65 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e  me compiler warn
31847 69 6e 67 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ings */.  sqlite
31848 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
31849 2c 20 22 70 61 72 73 65 72 20 73 74 61 63 6b 20  , "parser stack 
3184a 6f 76 65 72 66 6c 6f 77 22 29 3b 0a 20 20 70 50  overflow");.  pP
3184b 61 72 73 65 2d 3e 70 61 72 73 65 45 72 72 6f 72  arse->parseError
3184c 20 3d 20 31 3b 0a 20 20 20 73 71 6c 69 74 65 33   = 1;.   sqlite3
3184d 50 61 72 73 65 72 41 52 47 5f 53 54 4f 52 45 3b  ParserARG_STORE;
3184e 20 2f 2a 20 53 75 70 70 72 65 73 73 20 77 61 72   /* Suppress war
3184f 6e 69 6e 67 20 61 62 6f 75 74 20 75 6e 75 73 65  ning about unuse
31850 64 20 25 65 78 74 72 61 5f 61 72 67 75 6d 65 6e  d %extra_argumen
31851 74 20 76 61 72 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a  t var */.}../*.*
31852 2a 20 50 65 72 66 6f 72 6d 20 61 20 73 68 69 66  * Perform a shif
31853 74 20 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  t action..*/.sta
31854 74 69 63 20 76 6f 69 64 20 79 79 5f 73 68 69 66  tic void yy_shif
31855 74 28 0a 20 20 79 79 50 61 72 73 65 72 20 2a 79  t(.  yyParser *y
31856 79 70 50 61 72 73 65 72 2c 20 20 20 20 20 20 20  ypParser,       
31857 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
31858 20 74 6f 20 62 65 20 73 68 69 66 74 65 64 20 2a   to be shifted *
31859 2f 0a 20 20 69 6e 74 20 79 79 4e 65 77 53 74 61  /.  int yyNewSta
3185a 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  te,             
3185b 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 73 74 61    /* The new sta
3185c 74 65 20 74 6f 20 73 68 69 66 74 20 69 6e 20 2a  te to shift in *
3185d 2f 0a 20 20 69 6e 74 20 79 79 4d 61 6a 6f 72 2c  /.  int yyMajor,
3185e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3185f 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74    /* The major t
31860 6f 6b 65 6e 20 74 6f 20 73 68 69 66 74 20 69 6e  oken to shift in
31861 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50   */.  YYMINORTYP
31862 45 20 2a 79 79 70 4d 69 6e 6f 72 20 20 20 20 20  E *yypMinor     
31863 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
31864 6f 20 74 68 65 20 6d 69 6e 6f 72 20 74 6f 6b 65  o the minor toke
31865 6e 20 74 6f 20 73 68 69 66 74 20 69 6e 20 2a 2f  n to shift in */
31866 0a 29 7b 0a 20 20 79 79 53 74 61 63 6b 45 6e 74  .){.  yyStackEnt
31867 72 79 20 2a 79 79 74 6f 73 3b 0a 20 20 79 79 70  ry *yytos;.  yyp
31868 50 61 72 73 65 72 2d 3e 79 79 69 64 78 2b 2b 3b  Parser->yyidx++;
31869 0a 23 69 66 64 65 66 20 59 59 54 52 41 43 4b 4d  .#ifdef YYTRACKM
3186a 41 58 53 54 41 43 4b 44 45 50 54 48 0a 20 20 69  AXSTACKDEPTH.  i
3186b 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  f( yypParser->yy
3186c 69 64 78 3e 79 79 70 50 61 72 73 65 72 2d 3e 79  idx>yypParser->y
3186d 79 69 64 78 4d 61 78 20 29 7b 0a 20 20 20 20 79  yidxMax ){.    y
3186e 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 4d  ypParser->yyidxM
3186f 61 78 20 3d 20 79 79 70 50 61 72 73 65 72 2d 3e  ax = yypParser->
31870 79 79 69 64 78 3b 0a 20 20 7d 0a 23 65 6e 64 69  yyidx;.  }.#endi
31871 66 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45 50  f.#if YYSTACKDEP
31872 54 48 3e 30 20 0a 20 20 69 66 28 20 79 79 70 50  TH>0 .  if( yypP
31873 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 59 59  arser->yyidx>=YY
31874 53 54 41 43 4b 44 45 50 54 48 20 29 7b 0a 20 20  STACKDEPTH ){.  
31875 20 20 79 79 53 74 61 63 6b 4f 76 65 72 66 6c 6f    yyStackOverflo
31876 77 28 79 79 70 50 61 72 73 65 72 2c 20 79 79 70  w(yypParser, yyp
31877 4d 69 6e 6f 72 29 3b 0a 20 20 20 20 72 65 74 75  Minor);.    retu
31878 72 6e 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20  rn;.  }.#else.  
31879 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79  if( yypParser->y
3187a 79 69 64 78 3e 3d 79 79 70 50 61 72 73 65 72 2d  yidx>=yypParser-
3187b 3e 79 79 73 74 6b 73 7a 20 29 7b 0a 20 20 20 20  >yystksz ){.    
3187c 79 79 47 72 6f 77 53 74 61 63 6b 28 79 79 70 50  yyGrowStack(yypP
3187d 61 72 73 65 72 29 3b 0a 20 20 20 20 69 66 28 20  arser);.    if( 
3187e 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
3187f 3e 3d 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73  >=yypParser->yys
31880 74 6b 73 7a 20 29 7b 0a 20 20 20 20 20 20 79 79  tksz ){.      yy
31881 53 74 61 63 6b 4f 76 65 72 66 6c 6f 77 28 79 79  StackOverflow(yy
31882 70 50 61 72 73 65 72 2c 20 79 79 70 4d 69 6e 6f  pParser, yypMino
31883 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  r);.      return
31884 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
31885 69 66 0a 20 20 79 79 74 6f 73 20 3d 20 26 79 79  if.  yytos = &yy
31886 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b  pParser->yystack
31887 5b 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64  [yypParser->yyid
31888 78 5d 3b 0a 20 20 79 79 74 6f 73 2d 3e 73 74 61  x];.  yytos->sta
31889 74 65 6e 6f 20 3d 20 28 59 59 41 43 54 49 4f 4e  teno = (YYACTION
3188a 54 59 50 45 29 79 79 4e 65 77 53 74 61 74 65 3b  TYPE)yyNewState;
3188b 0a 20 20 79 79 74 6f 73 2d 3e 6d 61 6a 6f 72 20  .  yytos->major 
3188c 3d 20 28 59 59 43 4f 44 45 54 59 50 45 29 79 79  = (YYCODETYPE)yy
3188d 4d 61 6a 6f 72 3b 0a 20 20 79 79 74 6f 73 2d 3e  Major;.  yytos->
3188e 6d 69 6e 6f 72 20 3d 20 2a 79 79 70 4d 69 6e 6f  minor = *yypMino
3188f 72 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  r;.#ifndef NDEBU
31890 47 0a 20 20 69 66 28 20 79 79 54 72 61 63 65 46  G.  if( yyTraceF
31891 49 4c 45 20 26 26 20 79 79 70 50 61 72 73 65 72  ILE && yypParser
31892 2d 3e 79 79 69 64 78 3e 30 20 29 7b 0a 20 20 20  ->yyidx>0 ){.   
31893 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 70 72 69   int i;.    fpri
31894 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
31895 22 25 73 53 68 69 66 74 20 25 64 5c 6e 22 2c 79  "%sShift %d\n",y
31896 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 79 79 4e  yTracePrompt,yyN
31897 65 77 53 74 61 74 65 29 3b 0a 20 20 20 20 66 70  ewState);.    fp
31898 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
31899 45 2c 22 25 73 53 74 61 63 6b 3a 22 2c 79 79 54  E,"%sStack:",yyT
3189a 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20  racePrompt);.   
3189b 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 79 79 70   for(i=1; i<=yyp
3189c 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3b 20 69  Parser->yyidx; i
3189d 2b 2b 29 0a 20 20 20 20 20 20 66 70 72 69 6e 74  ++).      fprint
3189e 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 20  f(yyTraceFILE," 
3189f 25 73 22 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b  %s",yyTokenName[
318a0 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61  yypParser->yysta
318a1 63 6b 5b 69 5d 2e 6d 61 6a 6f 72 5d 29 3b 0a 20  ck[i].major]);. 
318a2 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
318a3 63 65 46 49 4c 45 2c 22 5c 6e 22 29 3b 0a 20 20  ceFILE,"\n");.  
318a4 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 20 54  }.#endif.}../* T
318a5 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62  he following tab
318a6 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f  le contains info
318a7 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 76  rmation about ev
318a8 65 72 79 20 72 75 6c 65 20 74 68 61 74 0a 2a 2a  ery rule that.**
318a9 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67 20   is used during 
318aa 74 68 65 20 72 65 64 75 63 65 2e 0a 2a 2f 0a 73  the reduce..*/.s
318ab 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75  tatic const stru
318ac 63 74 20 7b 0a 20 20 59 59 43 4f 44 45 54 59 50  ct {.  YYCODETYP
318ad 45 20 6c 68 73 3b 20 20 20 20 20 20 20 20 20 2f  E lhs;         /
318ae 2a 20 53 79 6d 62 6f 6c 20 6f 6e 20 74 68 65 20  * Symbol on the 
318af 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f  left-hand side o
318b0 66 20 74 68 65 20 72 75 6c 65 20 2a 2f 0a 20 20  f the rule */.  
318b1 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6e 72  unsigned char nr
318b2 68 73 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  hs;     /* Numbe
318b3 72 20 6f 66 20 72 69 67 68 74 2d 68 61 6e 64 20  r of right-hand 
318b4 73 69 64 65 20 73 79 6d 62 6f 6c 73 20 69 6e 20  side symbols in 
318b5 74 68 65 20 72 75 6c 65 20 2a 2f 0a 7d 20 79 79  the rule */.} yy
318b6 52 75 6c 65 49 6e 66 6f 5b 5d 20 3d 20 7b 0a 20  RuleInfo[] = {. 
318b7 20 7b 20 31 34 32 2c 20 31 20 7d 2c 0a 20 20 7b   { 142, 1 },.  {
318b8 20 31 34 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   143, 2 },.  { 1
318b9 34 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 34  43, 1 },.  { 144
318ba 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 34 2c 20  , 1 },.  { 144, 
318bb 33 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 30 20  3 },.  { 145, 0 
318bc 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 31 20 7d 2c  },.  { 145, 1 },
318bd 0a 20 20 7b 20 31 34 35 2c 20 33 20 7d 2c 0a 20  .  { 145, 3 },. 
318be 20 7b 20 31 34 36 2c 20 31 20 7d 2c 0a 20 20 7b   { 146, 1 },.  {
318bf 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   147, 3 },.  { 1
318c0 34 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34 39  49, 0 },.  { 149
318c1 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 39 2c 20  , 1 },.  { 149, 
318c2 32 20 7d 2c 0a 20 20 7b 20 31 34 38 2c 20 30 20  2 },.  { 148, 0 
318c3 7d 2c 0a 20 20 7b 20 31 34 38 2c 20 31 20 7d 2c  },.  { 148, 1 },
318c4 0a 20 20 7b 20 31 34 38 2c 20 31 20 7d 2c 0a 20  .  { 148, 1 },. 
318c5 20 7b 20 31 34 38 2c 20 31 20 7d 2c 0a 20 20 7b   { 148, 1 },.  {
318c6 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   147, 2 },.  { 1
318c7 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 2 },.  { 147
318c8 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 35 31 2c 20  , 2 },.  { 151, 
318c9 31 20 7d 2c 0a 20 20 7b 20 31 35 31 2c 20 30 20  1 },.  { 151, 0 
318ca 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c  },.  { 147, 2 },
318cb 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20  .  { 147, 3 },. 
318cc 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b   { 147, 5 },.  {
318cd 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   147, 2 },.  { 1
318ce 35 32 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 35 34  52, 6 },.  { 154
318cf 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 36 2c 20  , 1 },.  { 156, 
318d0 30 20 7d 2c 0a 20 20 7b 20 31 35 36 2c 20 33 20  0 },.  { 156, 3 
318d1 7d 2c 0a 20 20 7b 20 31 35 35 2c 20 31 20 7d 2c  },.  { 155, 1 },
318d2 0a 20 20 7b 20 31 35 35 2c 20 30 20 7d 2c 0a 20  .  { 155, 0 },. 
318d3 20 7b 20 31 35 33 2c 20 34 20 7d 2c 0a 20 20 7b   { 153, 4 },.  {
318d4 20 31 35 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   153, 2 },.  { 1
318d5 35 38 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 35 38  58, 3 },.  { 158
318d6 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 31 2c 20  , 1 },.  { 161, 
318d7 33 20 7d 2c 0a 20 20 7b 20 31 36 32 2c 20 31 20  3 },.  { 162, 1 
318d8 7d 2c 0a 20 20 7b 20 31 36 35 2c 20 31 20 7d 2c  },.  { 165, 1 },
318d9 0a 20 20 7b 20 31 36 35 2c 20 31 20 7d 2c 0a 20  .  { 165, 1 },. 
318da 20 7b 20 31 36 36 2c 20 31 20 7d 2c 0a 20 20 7b   { 166, 1 },.  {
318db 20 31 35 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   150, 1 },.  { 1
318dc 35 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 30  50, 1 },.  { 150
318dd 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 33 2c 20  , 1 },.  { 163, 
318de 30 20 7d 2c 0a 20 20 7b 20 31 36 33 2c 20 31 20  0 },.  { 163, 1 
318df 7d 2c 0a 20 20 7b 20 31 36 37 2c 20 31 20 7d 2c  },.  { 167, 1 },
318e0 0a 20 20 7b 20 31 36 37 2c 20 34 20 7d 2c 0a 20  .  { 167, 4 },. 
318e1 20 7b 20 31 36 37 2c 20 36 20 7d 2c 0a 20 20 7b   { 167, 6 },.  {
318e2 20 31 36 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   168, 1 },.  { 1
318e3 36 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 36 39  68, 2 },.  { 169
318e4 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20  , 1 },.  { 169, 
318e5 31 20 7d 2c 0a 20 20 7b 20 31 36 34 2c 20 32 20  1 },.  { 164, 2 
318e6 7d 2c 0a 20 20 7b 20 31 36 34 2c 20 30 20 7d 2c  },.  { 164, 0 },
318e7 0a 20 20 7b 20 31 37 32 2c 20 33 20 7d 2c 0a 20  .  { 172, 3 },. 
318e8 20 7b 20 31 37 32 2c 20 31 20 7d 2c 0a 20 20 7b   { 172, 1 },.  {
318e9 20 31 37 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   173, 2 },.  { 1
318ea 37 33 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 33  73, 4 },.  { 173
318eb 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20  , 3 },.  { 173, 
318ec 33 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 32 20  3 },.  { 173, 2 
318ed 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 32 20 7d 2c  },.  { 173, 2 },
318ee 0a 20 20 7b 20 31 37 33 2c 20 33 20 7d 2c 0a 20  .  { 173, 3 },. 
318ef 20 7b 20 31 37 33 2c 20 35 20 7d 2c 0a 20 20 7b   { 173, 5 },.  {
318f0 20 31 37 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   173, 2 },.  { 1
318f1 37 33 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 33  73, 4 },.  { 173
318f2 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20  , 4 },.  { 173, 
318f3 31 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 32 20  1 },.  { 173, 2 
318f4 7d 2c 0a 20 20 7b 20 31 37 38 2c 20 30 20 7d 2c  },.  { 178, 0 },
318f5 0a 20 20 7b 20 31 37 38 2c 20 31 20 7d 2c 0a 20  .  { 178, 1 },. 
318f6 20 7b 20 31 38 30 2c 20 30 20 7d 2c 0a 20 20 7b   { 180, 0 },.  {
318f7 20 31 38 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   180, 2 },.  { 1
318f8 38 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 32  82, 2 },.  { 182
318f9 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38 32 2c 20  , 3 },.  { 182, 
318fa 33 20 7d 2c 0a 20 20 7b 20 31 38 33 2c 20 32 20  3 },.  { 183, 2 
318fb 7d 2c 0a 20 20 7b 20 31 38 33 2c 20 32 20 7d 2c  },.  { 183, 2 },
318fc 0a 20 20 7b 20 31 38 33 2c 20 31 20 7d 2c 0a 20  .  { 183, 1 },. 
318fd 20 7b 20 31 38 33 2c 20 31 20 7d 2c 0a 20 20 7b   { 183, 1 },.  {
318fe 20 31 38 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   183, 2 },.  { 1
318ff 38 31 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38 31  81, 3 },.  { 181
31900 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 34 2c 20  , 2 },.  { 184, 
31901 30 20 7d 2c 0a 20 20 7b 20 31 38 34 2c 20 32 20  0 },.  { 184, 2 
31902 7d 2c 0a 20 20 7b 20 31 38 34 2c 20 32 20 7d 2c  },.  { 184, 2 },
31903 0a 20 20 7b 20 31 35 39 2c 20 30 20 7d 2c 0a 20  .  { 159, 0 },. 
31904 20 7b 20 31 35 39 2c 20 32 20 7d 2c 0a 20 20 7b   { 159, 2 },.  {
31905 20 31 38 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   185, 3 },.  { 1
31906 38 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 35  85, 2 },.  { 185
31907 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38 36 2c 20  , 1 },.  { 186, 
31908 32 20 7d 2c 0a 20 20 7b 20 31 38 36 2c 20 37 20  2 },.  { 186, 7 
31909 7d 2c 0a 20 20 7b 20 31 38 36 2c 20 35 20 7d 2c  },.  { 186, 5 },
3190a 0a 20 20 7b 20 31 38 36 2c 20 35 20 7d 2c 0a 20  .  { 186, 5 },. 
3190b 20 7b 20 31 38 36 2c 20 31 30 20 7d 2c 0a 20 20   { 186, 10 },.  
3190c 7b 20 31 38 38 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 188, 0 },.  { 
3190d 31 38 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  188, 1 },.  { 17
3190e 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37 36 2c  6, 0 },.  { 176,
3190f 20 33 20 7d 2c 0a 20 20 7b 20 31 38 39 2c 20 30   3 },.  { 189, 0
31910 20 7d 2c 0a 20 20 7b 20 31 38 39 2c 20 32 20 7d   },.  { 189, 2 }
31911 2c 0a 20 20 7b 20 31 39 30 2c 20 31 20 7d 2c 0a  ,.  { 190, 1 },.
31912 20 20 7b 20 31 39 30 2c 20 31 20 7d 2c 0a 20 20    { 190, 1 },.  
31913 7b 20 31 39 30 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 190, 1 },.  { 
31914 31 34 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 39  147, 4 },.  { 19
31915 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39 32 2c  2, 2 },.  { 192,
31916 20 30 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 38   0 },.  { 147, 8
31917 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d   },.  { 147, 4 }
31918 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a  ,.  { 147, 1 },.
31919 20 20 7b 20 31 36 30 2c 20 31 20 7d 2c 0a 20 20    { 160, 1 },.  
3191a 7b 20 31 36 30 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 160, 3 },.  { 
3191b 31 39 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39  195, 1 },.  { 19
3191c 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39 35 2c  5, 2 },.  { 195,
3191d 20 31 20 7d 2c 0a 20 20 7b 20 31 39 34 2c 20 39   1 },.  { 194, 9
3191e 20 7d 2c 0a 20 20 7b 20 31 39 36 2c 20 31 20 7d   },.  { 196, 1 }
3191f 2c 0a 20 20 7b 20 31 39 36 2c 20 31 20 7d 2c 0a  ,.  { 196, 1 },.
31920 20 20 7b 20 31 39 36 2c 20 30 20 7d 2c 0a 20 20    { 196, 0 },.  
31921 7b 20 32 30 34 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 204, 2 },.  { 
31922 32 30 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39  204, 0 },.  { 19
31923 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 39 37 2c  7, 3 },.  { 197,
31924 20 32 20 7d 2c 0a 20 20 7b 20 31 39 37 2c 20 34   2 },.  { 197, 4
31925 20 7d 2c 0a 20 20 7b 20 32 30 35 2c 20 32 20 7d   },.  { 205, 2 }
31926 2c 0a 20 20 7b 20 32 30 35 2c 20 31 20 7d 2c 0a  ,.  { 205, 1 },.
31927 20 20 7b 20 32 30 35 2c 20 30 20 7d 2c 0a 20 20    { 205, 0 },.  
31928 7b 20 31 39 38 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 198, 0 },.  { 
31929 31 39 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30  198, 2 },.  { 20
3192a 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 37 2c  7, 2 },.  { 207,
3192b 20 30 20 7d 2c 0a 20 20 7b 20 32 30 36 2c 20 37   0 },.  { 206, 7
3192c 20 7d 2c 0a 20 20 7b 20 32 30 36 2c 20 37 20 7d   },.  { 206, 7 }
3192d 2c 0a 20 20 7b 20 32 30 36 2c 20 37 20 7d 2c 0a  ,.  { 206, 7 },.
3192e 20 20 7b 20 31 35 37 2c 20 30 20 7d 2c 0a 20 20    { 157, 0 },.  
3192f 7b 20 31 35 37 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 157, 2 },.  { 
31930 31 39 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30  193, 2 },.  { 20
31931 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 30 38 2c  8, 1 },.  { 208,
31932 20 32 20 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 33   2 },.  { 208, 3
31933 20 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 34 20 7d   },.  { 208, 4 }
31934 2c 0a 20 20 7b 20 32 31 30 2c 20 32 20 7d 2c 0a  ,.  { 210, 2 },.
31935 20 20 7b 20 32 31 30 2c 20 30 20 7d 2c 0a 20 20    { 210, 0 },.  
31936 7b 20 32 30 39 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 209, 0 },.  { 
31937 32 30 39 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 30  209, 3 },.  { 20
31938 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31 31 2c  9, 2 },.  { 211,
31939 20 34 20 7d 2c 0a 20 20 7b 20 32 31 31 2c 20 30   4 },.  { 211, 0
3193a 20 7d 2c 0a 20 20 7b 20 32 30 32 2c 20 30 20 7d   },.  { 202, 0 }
3193b 2c 0a 20 20 7b 20 32 30 32 2c 20 33 20 7d 2c 0a  ,.  { 202, 3 },.
3193c 20 20 7b 20 32 31 34 2c 20 34 20 7d 2c 0a 20 20    { 214, 4 },.  
3193d 7b 20 32 31 34 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 214, 2 },.  { 
3193e 32 31 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  215, 1 },.  { 17
3193f 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 37 2c  7, 1 },.  { 177,
31940 20 31 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 30   1 },.  { 177, 0
31941 20 7d 2c 0a 20 20 7b 20 32 30 30 2c 20 30 20 7d   },.  { 200, 0 }
31942 2c 0a 20 20 7b 20 32 30 30 2c 20 33 20 7d 2c 0a  ,.  { 200, 3 },.
31943 20 20 7b 20 32 30 31 2c 20 30 20 7d 2c 0a 20 20    { 201, 0 },.  
31944 7b 20 32 30 31 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 201, 2 },.  { 
31945 32 30 33 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30  203, 0 },.  { 20
31946 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 33 2c  3, 2 },.  { 203,
31947 20 34 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 34   4 },.  { 203, 4
31948 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d   },.  { 147, 5 }
31949 2c 0a 20 20 7b 20 31 39 39 2c 20 30 20 7d 2c 0a  ,.  { 199, 0 },.
3194a 20 20 7b 20 31 39 39 2c 20 32 20 7d 2c 0a 20 20    { 199, 2 },.  
3194b 7b 20 31 34 37 2c 20 37 20 7d 2c 0a 20 20 7b 20  { 147, 7 },.  { 
3194c 32 31 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 31  217, 5 },.  { 21
3194d 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 3 },.  { 147,
3194e 20 38 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35   8 },.  { 147, 5
3194f 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d   },.  { 147, 6 }
31950 2c 0a 20 20 7b 20 32 31 38 2c 20 32 20 7d 2c 0a  ,.  { 218, 2 },.
31951 20 20 7b 20 32 31 38 2c 20 31 20 7d 2c 0a 20 20    { 218, 1 },.  
31952 7b 20 32 32 30 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 220, 3 },.  { 
31953 32 32 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 31  220, 1 },.  { 21
31954 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 31 39 2c  9, 0 },.  { 219,
31955 20 33 20 7d 2c 0a 20 20 7b 20 32 31 33 2c 20 33   3 },.  { 213, 3
31956 20 7d 2c 0a 20 20 7b 20 32 31 33 2c 20 31 20 7d   },.  { 213, 1 }
31957 2c 0a 20 20 7b 20 31 37 35 2c 20 31 20 7d 2c 0a  ,.  { 175, 1 },.
31958 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20    { 175, 3 },.  
31959 7b 20 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 174, 1 },.  { 
3195a 31 37 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  175, 1 },.  { 17
3195b 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 1 },.  { 175,
3195c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35   3 },.  { 175, 5
3195d 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 31 20 7d   },.  { 174, 1 }
3195e 2c 0a 20 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0a  ,.  { 174, 1 },.
3195f 20 20 7b 20 31 37 35 2c 20 31 20 7d 2c 0a 20 20    { 175, 1 },.  
31960 7b 20 31 37 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 175, 1 },.  { 
31961 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37  175, 3 },.  { 17
31962 35 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 6 },.  { 175,
31963 20 35 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 34   5 },.  { 175, 4
31964 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20 31 20 7d   },.  { 174, 1 }
31965 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a  ,.  { 175, 3 },.
31966 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20    { 175, 3 },.  
31967 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 175, 3 },.  { 
31968 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37  175, 3 },.  { 17
31969 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 3 },.  { 175,
3196a 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33   3 },.  { 175, 3
3196b 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d   },.  { 175, 3 }
3196c 2c 0a 20 20 7b 20 32 32 32 2c 20 31 20 7d 2c 0a  ,.  { 222, 1 },.
3196d 20 20 7b 20 32 32 32 2c 20 32 20 7d 2c 0a 20 20    { 222, 2 },.  
3196e 7b 20 32 32 32 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 222, 1 },.  { 
3196f 32 32 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 32  222, 2 },.  { 22
31970 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 32 33 2c  3, 2 },.  { 223,
31971 20 30 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 34   0 },.  { 175, 4
31972 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 32 20 7d   },.  { 175, 2 }
31973 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a  ,.  { 175, 3 },.
31974 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20    { 175, 3 },.  
31975 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 175, 4 },.  { 
31976 31 37 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37  175, 2 },.  { 17
31977 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 2 },.  { 175,
31978 20 32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 32   2 },.  { 175, 2
31979 20 7d 2c 0a 20 20 7b 20 32 32 34 2c 20 31 20 7d   },.  { 224, 1 }
3197a 2c 0a 20 20 7b 20 32 32 34 2c 20 32 20 7d 2c 0a  ,.  { 224, 2 },.
3197b 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20 20    { 175, 5 },.  
3197c 7b 20 32 32 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 225, 1 },.  { 
3197d 32 32 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37  225, 2 },.  { 17
3197e 35 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 37 35 2c  5, 5 },.  { 175,
3197f 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35   3 },.  { 175, 5
31980 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d   },.  { 175, 4 }
31981 2c 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a  ,.  { 175, 4 },.
31982 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20 20    { 175, 5 },.  
31983 7b 20 32 32 37 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 227, 5 },.  { 
31984 32 32 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 32  227, 4 },.  { 22
31985 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 32 38 2c  8, 2 },.  { 228,
31986 20 30 20 7d 2c 0a 20 20 7b 20 32 32 36 2c 20 31   0 },.  { 226, 1
31987 20 7d 2c 0a 20 20 7b 20 32 32 36 2c 20 30 20 7d   },.  { 226, 0 }
31988 2c 0a 20 20 7b 20 32 32 31 2c 20 31 20 7d 2c 0a  ,.  { 221, 1 },.
31989 20 20 7b 20 32 32 31 2c 20 30 20 7d 2c 0a 20 20    { 221, 0 },.  
3198a 7b 20 32 31 36 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 216, 3 },.  { 
3198b 32 31 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  216, 1 },.  { 14
3198c 37 2c 20 31 31 20 7d 2c 0a 20 20 7b 20 32 32 39  7, 11 },.  { 229
3198d 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 39 2c 20  , 1 },.  { 229, 
3198e 30 20 7d 2c 0a 20 20 7b 20 31 37 39 2c 20 30 20  0 },.  { 179, 0 
3198f 7d 2c 0a 20 20 7b 20 31 37 39 2c 20 33 20 7d 2c  },.  { 179, 3 },
31990 0a 20 20 7b 20 31 38 37 2c 20 35 20 7d 2c 0a 20  .  { 187, 5 },. 
31991 20 7b 20 31 38 37 2c 20 33 20 7d 2c 0a 20 20 7b   { 187, 3 },.  {
31992 20 32 33 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   230, 0 },.  { 2
31993 33 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37  30, 2 },.  { 147
31994 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 4 },.  { 147, 
31995 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20  1 },.  { 147, 2 
31996 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c  },.  { 147, 3 },
31997 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20  .  { 147, 5 },. 
31998 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b   { 147, 6 },.  {
31999 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31   147, 5 },.  { 1
3199a 34 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 32 33 31  47, 6 },.  { 231
3199b 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 31 2c 20  , 1 },.  { 231, 
3199c 31 20 7d 2c 0a 20 20 7b 20 32 33 31 2c 20 31 20  1 },.  { 231, 1 
3199d 7d 2c 0a 20 20 7b 20 32 33 31 2c 20 31 20 7d 2c  },.  { 231, 1 },
3199e 0a 20 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a 20  .  { 231, 1 },. 
3199f 20 7b 20 31 37 30 2c 20 32 20 7d 2c 0a 20 20 7b   { 170, 2 },.  {
319a0 20 31 37 31 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   171, 2 },.  { 2
319a1 33 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 32  33, 1 },.  { 232
319a2 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20  , 1 },.  { 232, 
319a3 30 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35 20  0 },.  { 147, 5 
319a4 7d 2c 0a 20 20 7b 20 32 33 34 2c 20 31 31 20 7d  },.  { 234, 11 }
319a5 2c 0a 20 20 7b 20 32 33 36 2c 20 31 20 7d 2c 0a  ,.  { 236, 1 },.
319a6 20 20 7b 20 32 33 36 2c 20 31 20 7d 2c 0a 20 20    { 236, 1 },.  
319a7 7b 20 32 33 36 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 236, 2 },.  { 
319a8 32 33 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33  236, 0 },.  { 23
319a9 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 37 2c  7, 1 },.  { 237,
319aa 20 31 20 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 33   1 },.  { 237, 3
319ab 20 7d 2c 0a 20 20 7b 20 32 33 38 2c 20 30 20 7d   },.  { 238, 0 }
319ac 2c 0a 20 20 7b 20 32 33 38 2c 20 33 20 7d 2c 0a  ,.  { 238, 3 },.
319ad 20 20 7b 20 32 33 39 2c 20 30 20 7d 2c 0a 20 20    { 239, 0 },.  
319ae 7b 20 32 33 39 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 239, 2 },.  { 
319af 32 33 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33  235, 3 },.  { 23
319b0 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 34 31 2c  5, 2 },.  { 241,
319b1 20 31 20 7d 2c 0a 20 20 7b 20 32 34 31 2c 20 33   1 },.  { 241, 3
319b2 20 7d 2c 0a 20 20 7b 20 32 34 32 2c 20 30 20 7d   },.  { 242, 0 }
319b3 2c 0a 20 20 7b 20 32 34 32 2c 20 33 20 7d 2c 0a  ,.  { 242, 3 },.
319b4 20 20 7b 20 32 34 32 2c 20 32 20 7d 2c 0a 20 20    { 242, 2 },.  
319b5 7b 20 32 34 30 2c 20 37 20 7d 2c 0a 20 20 7b 20  { 240, 7 },.  { 
319b6 32 34 30 2c 20 38 20 7d 2c 0a 20 20 7b 20 32 34  240, 8 },.  { 24
319b7 30 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 34 30 2c  0, 5 },.  { 240,
319b8 20 35 20 7d 2c 0a 20 20 7b 20 32 34 30 2c 20 31   5 },.  { 240, 1
319b9 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d   },.  { 175, 4 }
319ba 2c 0a 20 20 7b 20 31 37 35 2c 20 36 20 7d 2c 0a  ,.  { 175, 6 },.
319bb 20 20 7b 20 31 39 31 2c 20 31 20 7d 2c 0a 20 20    { 191, 1 },.  
319bc 7b 20 31 39 31 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 191, 1 },.  { 
319bd 31 39 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  191, 1 },.  { 14
319be 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 4 },.  { 147,
319bf 20 36 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33   6 },.  { 147, 3
319c0 20 7d 2c 0a 20 20 7b 20 32 34 34 2c 20 30 20 7d   },.  { 244, 0 }
319c1 2c 0a 20 20 7b 20 32 34 34 2c 20 32 20 7d 2c 0a  ,.  { 244, 2 },.
319c2 20 20 7b 20 32 34 33 2c 20 31 20 7d 2c 0a 20 20    { 243, 1 },.  
319c3 7b 20 32 34 33 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 243, 0 },.  { 
319c4 31 34 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  147, 1 },.  { 14
319c5 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 3 },.  { 147,
319c6 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33   1 },.  { 147, 3
319c7 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d   },.  { 147, 6 }
319c8 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a  ,.  { 147, 6 },.
319c9 20 20 7b 20 32 34 35 2c 20 31 20 7d 2c 0a 20 20    { 245, 1 },.  
319ca 7b 20 32 34 36 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 246, 0 },.  { 
319cb 32 34 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  246, 1 },.  { 14
319cc 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 1 },.  { 147,
319cd 20 34 20 7d 2c 0a 20 20 7b 20 32 34 37 2c 20 37   4 },.  { 247, 7
319ce 20 7d 2c 0a 20 20 7b 20 32 34 38 2c 20 31 20 7d   },.  { 248, 1 }
319cf 2c 0a 20 20 7b 20 32 34 38 2c 20 33 20 7d 2c 0a  ,.  { 248, 3 },.
319d0 20 20 7b 20 32 34 39 2c 20 30 20 7d 2c 0a 20 20    { 249, 0 },.  
319d1 7b 20 32 34 39 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 249, 2 },.  { 
319d2 32 35 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 35  250, 1 },.  { 25
319d3 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 35 31 2c  0, 3 },.  { 251,
319d4 20 31 20 7d 2c 0a 20 20 7b 20 32 35 32 2c 20 30   1 },.  { 252, 0
319d5 20 7d 2c 0a 20 20 7b 20 32 35 32 2c 20 34 20 7d   },.  { 252, 4 }
319d6 2c 0a 20 20 7b 20 32 35 32 2c 20 32 20 7d 2c 0a  ,.  { 252, 2 },.
319d7 7d 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  };..static void 
319d8 79 79 5f 61 63 63 65 70 74 28 79 79 50 61 72 73  yy_accept(yyPars
319d9 65 72 2a 29 3b 20 20 2f 2a 20 46 6f 72 77 61 72  er*);  /* Forwar
319da 64 20 44 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f  d Declaration */
319db 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20  ../*.** Perform 
319dc 61 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e 20  a reduce action 
319dd 61 6e 64 20 74 68 65 20 73 68 69 66 74 20 74 68  and the shift th
319de 61 74 20 6d 75 73 74 20 69 6d 6d 65 64 69 61 74  at must immediat
319df 65 6c 79 0a 2a 2a 20 66 6f 6c 6c 6f 77 20 74 68  ely.** follow th
319e0 65 20 72 65 64 75 63 65 2e 0a 2a 2f 0a 73 74 61  e reduce..*/.sta
319e1 74 69 63 20 76 6f 69 64 20 79 79 5f 72 65 64 75  tic void yy_redu
319e2 63 65 28 0a 20 20 79 79 50 61 72 73 65 72 20 2a  ce(.  yyParser *
319e3 79 79 70 50 61 72 73 65 72 2c 20 20 20 20 20 20  yypParser,      
319e4 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
319e5 20 2a 2f 0a 20 20 69 6e 74 20 79 79 72 75 6c 65   */.  int yyrule
319e6 6e 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  no              
319e7 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
319e8 74 68 65 20 72 75 6c 65 20 62 79 20 77 68 69 63  the rule by whic
319e9 68 20 74 6f 20 72 65 64 75 63 65 20 2a 2f 0a 29  h to reduce */.)
319ea 7b 0a 20 20 69 6e 74 20 79 79 67 6f 74 6f 3b 20  {.  int yygoto; 
319eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
319ec 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20      /* The next 
319ed 73 74 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20 79  state */.  int y
319ee 79 61 63 74 3b 20 20 20 20 20 20 20 20 20 20 20  yact;           
319ef 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
319f0 65 20 6e 65 78 74 20 61 63 74 69 6f 6e 20 2a 2f  e next action */
319f1 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 79  .  YYMINORTYPE y
319f2 79 67 6f 74 6f 6d 69 6e 6f 72 3b 20 20 20 20 20  ygotominor;     
319f3 20 20 20 2f 2a 20 54 68 65 20 4c 48 53 20 6f 66     /* The LHS of
319f4 20 74 68 65 20 72 75 6c 65 20 72 65 64 75 63 65   the rule reduce
319f5 64 20 2a 2f 0a 20 20 79 79 53 74 61 63 6b 45 6e  d */.  yyStackEn
319f6 74 72 79 20 2a 79 79 6d 73 70 3b 20 20 20 20 20  try *yymsp;     
319f7 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f         /* The to
319f8 70 20 6f 66 20 74 68 65 20 70 61 72 73 65 72 27  p of the parser'
319f9 73 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74  s stack */.  int
319fa 20 79 79 73 69 7a 65 3b 20 20 20 20 20 20 20 20   yysize;        
319fb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
319fc 41 6d 6f 75 6e 74 20 74 6f 20 70 6f 70 20 74 68  Amount to pop th
319fd 65 20 73 74 61 63 6b 20 2a 2f 0a 20 20 73 71 6c  e stack */.  sql
319fe 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 46 45  ite3ParserARG_FE
319ff 54 43 48 3b 0a 20 20 79 79 6d 73 70 20 3d 20 26  TCH;.  yymsp = &
31a00 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61  yypParser->yysta
31a01 63 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e 79 79  ck[yypParser->yy
31a02 69 64 78 5d 3b 0a 23 69 66 6e 64 65 66 20 4e 44  idx];.#ifndef ND
31a03 45 42 55 47 0a 20 20 69 66 28 20 79 79 54 72 61  EBUG.  if( yyTra
31a04 63 65 46 49 4c 45 20 26 26 20 79 79 72 75 6c 65  ceFILE && yyrule
31a05 6e 6f 3e 3d 30 20 0a 20 20 20 20 20 20 20 20 26  no>=0 .        &
31a06 26 20 79 79 72 75 6c 65 6e 6f 3c 28 69 6e 74 29  & yyruleno<(int)
31a07 28 73 69 7a 65 6f 66 28 79 79 52 75 6c 65 4e 61  (sizeof(yyRuleNa
31a08 6d 65 29 2f 73 69 7a 65 6f 66 28 79 79 52 75 6c  me)/sizeof(yyRul
31a09 65 4e 61 6d 65 5b 30 5d 29 29 20 29 7b 0a 20 20  eName[0])) ){.  
31a0a 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
31a0b 65 46 49 4c 45 2c 20 22 25 73 52 65 64 75 63 65  eFILE, "%sReduce
31a0c 20 5b 25 73 5d 2e 5c 6e 22 2c 20 79 79 54 72 61   [%s].\n", yyTra
31a0d 63 65 50 72 6f 6d 70 74 2c 0a 20 20 20 20 20 20  cePrompt,.      
31a0e 79 79 52 75 6c 65 4e 61 6d 65 5b 79 79 72 75 6c  yyRuleName[yyrul
31a0f 65 6e 6f 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  eno]);.  }.#endi
31a10 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a  f /* NDEBUG */..
31a11 20 20 2f 2a 20 53 69 6c 65 6e 63 65 20 63 6f 6d    /* Silence com
31a12 70 6c 61 69 6e 74 73 20 66 72 6f 6d 20 70 75 72  plaints from pur
31a13 69 66 79 20 61 62 6f 75 74 20 79 79 67 6f 74 6f  ify about yygoto
31a14 6d 69 6e 6f 72 20 62 65 69 6e 67 20 75 6e 69 6e  minor being unin
31a15 69 74 69 61 6c 69 7a 65 64 0a 20 20 2a 2a 20 69  itialized.  ** i
31a16 6e 20 73 6f 6d 65 20 63 61 73 65 73 20 77 68 65  n some cases whe
31a17 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 69  n it is copied i
31a18 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20 61 66  nto the stack af
31a19 74 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ter the followin
31a1a 67 0a 20 20 2a 2a 20 73 77 69 74 63 68 2e 20 20  g.  ** switch.  
31a1b 79 79 67 6f 74 6f 6d 69 6e 6f 72 20 69 73 20 75  yygotominor is u
31a1c 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 68 65  ninitialized whe
31a1d 6e 20 61 20 72 75 6c 65 20 72 65 64 75 63 65 73  n a rule reduces
31a1e 20 74 68 61 74 20 64 6f 65 73 0a 20 20 2a 2a 20   that does.  ** 
31a1f 6e 6f 74 20 73 65 74 20 74 68 65 20 76 61 6c 75  not set the valu
31a20 65 20 6f 66 20 69 74 73 20 6c 65 66 74 2d 68 61  e of its left-ha
31a21 6e 64 20 73 69 64 65 20 6e 6f 6e 74 65 72 6d 69  nd side nontermi
31a22 6e 61 6c 2e 20 20 4c 65 61 76 69 6e 67 20 74 68  nal.  Leaving th
31a23 65 0a 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66 20  e.  ** value of 
31a24 74 68 65 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20  the nonterminal 
31a25 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 73  uninitialized is
31a26 20 75 74 74 65 72 6c 79 20 68 61 72 6d 6c 65 73   utterly harmles
31a27 73 20 61 73 20 6c 6f 6e 67 0a 20 20 2a 2a 20 61  s as long.  ** a
31a28 73 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e  s the value is n
31a29 65 76 65 72 20 75 73 65 64 2e 20 20 53 6f 20 72  ever used.  So r
31a2a 65 61 6c 6c 79 20 74 68 65 20 6f 6e 6c 79 20 74  eally the only t
31a2b 68 69 6e 67 20 74 68 69 73 20 63 6f 64 65 0a 20  hing this code. 
31a2c 20 2a 2a 20 61 63 63 6f 6d 70 6c 69 73 68 65 73   ** accomplishes
31a2d 20 69 73 20 74 6f 20 71 75 69 65 74 65 6e 20 70   is to quieten p
31a2e 75 72 69 66 79 2e 20 20 0a 20 20 2a 2a 0a 20 20  urify.  .  **.  
31a2f 2a 2a 20 32 30 30 37 2d 30 31 2d 31 36 3a 20 20  ** 2007-01-16:  
31a30 54 68 65 20 77 69 72 65 73 68 61 72 6b 20 70 72  The wireshark pr
31a31 6f 6a 65 63 74 20 28 77 77 77 2e 77 69 72 65 73  oject (www.wires
31a32 68 61 72 6b 2e 6f 72 67 29 20 72 65 70 6f 72 74  hark.org) report
31a33 73 20 74 68 61 74 0a 20 20 2a 2a 20 77 69 74 68  s that.  ** with
31a34 6f 75 74 20 74 68 69 73 20 63 6f 64 65 2c 20 74  out this code, t
31a35 68 65 69 72 20 70 61 72 73 65 72 20 73 65 67 66  heir parser segf
31a36 61 75 6c 74 73 2e 20 20 49 27 6d 20 6e 6f 74 20  aults.  I'm not 
31a37 73 75 72 65 20 77 68 61 74 20 74 68 65 72 65 0a  sure what there.
31a38 20 20 2a 2a 20 70 61 72 73 65 72 20 69 73 20 64    ** parser is d
31a39 6f 69 6e 67 20 74 6f 20 6d 61 6b 65 20 74 68 69  oing to make thi
31a3a 73 20 68 61 70 70 65 6e 2e 20 20 54 68 69 73 20  s happen.  This 
31a3b 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 62 75  is the second bu
31a3c 67 20 72 65 70 6f 72 74 0a 20 20 2a 2a 20 66 72  g report.  ** fr
31a3d 6f 6d 20 77 69 72 65 73 68 61 72 6b 20 74 68 69  om wireshark thi
31a3e 73 20 77 65 65 6b 2e 20 20 43 6c 65 61 72 6c 79  s week.  Clearly
31a3f 20 74 68 65 79 20 61 72 65 20 73 74 72 65 73 73   they are stress
31a40 69 6e 67 20 4c 65 6d 6f 6e 20 69 6e 20 77 61 79  ing Lemon in way
31a41 73 0a 20 20 2a 2a 20 74 68 61 74 20 69 74 20 68  s.  ** that it h
31a42 61 73 20 6e 6f 74 20 62 65 65 6e 20 70 72 65 76  as not been prev
31a43 69 6f 75 73 6c 79 20 73 74 72 65 73 73 65 64 2e  iously stressed.
31a44 2e 2e 20 20 28 53 51 4c 69 74 65 20 74 69 63 6b  ..  (SQLite tick
31a45 65 74 20 23 32 31 37 32 29 0a 20 20 2a 2f 0a 20  et #2172).  */. 
31a46 20 2f 2a 6d 65 6d 73 65 74 28 26 79 79 67 6f 74   /*memset(&yygot
31a47 6f 6d 69 6e 6f 72 2c 20 30 2c 20 73 69 7a 65 6f  ominor, 0, sizeo
31a48 66 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 29 29 3b  f(yygotominor));
31a49 2a 2f 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.  yygotominor
31a4a 20 3d 20 79 79 7a 65 72 6f 6d 69 6e 6f 72 3b 0a   = yyzerominor;.
31a4b 0a 0a 20 20 73 77 69 74 63 68 28 20 79 79 72 75  ..  switch( yyru
31a4c 6c 65 6e 6f 20 29 7b 0a 20 20 2f 2a 20 42 65 67  leno ){.  /* Beg
31a4d 69 6e 6e 69 6e 67 20 68 65 72 65 20 61 72 65 20  inning here are 
31a4e 74 68 65 20 72 65 64 75 63 74 69 6f 6e 20 63 61  the reduction ca
31a4f 73 65 73 2e 20 20 41 20 74 79 70 69 63 61 6c 20  ses.  A typical 
31a50 65 78 61 6d 70 6c 65 0a 20 20 2a 2a 20 66 6f 6c  example.  ** fol
31a51 6c 6f 77 73 3a 0a 20 20 2a 2a 20 20 20 63 61 73  lows:.  **   cas
31a52 65 20 30 3a 0a 20 20 2a 2a 20 20 23 6c 69 6e 65  e 0:.  **  #line
31a53 20 3c 6c 69 6e 65 6e 6f 3e 20 3c 67 72 61 6d 6d   <lineno> <gramm
31a54 61 72 66 69 6c 65 3e 0a 20 20 2a 2a 20 20 20 20  arfile>.  **    
31a55 20 7b 20 2e 2e 2e 20 7d 20 20 20 20 20 20 20 20   { ... }        
31a56 20 20 20 2f 2f 20 55 73 65 72 20 73 75 70 70 6c     // User suppl
31a57 69 65 64 20 63 6f 64 65 0a 20 20 2a 2a 20 20 23  ied code.  **  #
31a58 6c 69 6e 65 20 3c 6c 69 6e 65 6e 6f 3e 20 3c 74  line <lineno> <t
31a59 68 69 73 66 69 6c 65 3e 0a 20 20 2a 2a 20 20 20  hisfile>.  **   
31a5a 20 20 62 72 65 61 6b 3b 0a 20 20 2a 2f 0a 20 20    break;.  */.  
31a5b 20 20 20 20 63 61 73 65 20 35 3a 20 2f 2a 20 65      case 5: /* e
31a5c 78 70 6c 61 69 6e 20 3a 3a 3d 20 2a 2f 0a 7b 20  xplain ::= */.{ 
31a5d 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73  sqlite3BeginPars
31a5e 65 28 70 50 61 72 73 65 2c 20 30 29 3b 20 7d 0a  e(pParse, 0); }.
31a5f 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31a60 20 20 20 20 20 63 61 73 65 20 36 3a 20 2f 2a 20       case 6: /* 
31a61 65 78 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50 4c  explain ::= EXPL
31a62 41 49 4e 20 2a 2f 0a 7b 20 73 71 6c 69 74 65 33  AIN */.{ sqlite3
31a63 42 65 67 69 6e 50 61 72 73 65 28 70 50 61 72 73  BeginParse(pPars
31a64 65 2c 20 31 29 3b 20 7d 0a 20 20 20 20 20 20 20  e, 1); }.       
31a65 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31a66 73 65 20 37 3a 20 2f 2a 20 65 78 70 6c 61 69 6e  se 7: /* explain
31a67 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 20 51 55 45   ::= EXPLAIN QUE
31a68 52 59 20 50 4c 41 4e 20 2a 2f 0a 7b 20 73 71 6c  RY PLAN */.{ sql
31a69 69 74 65 33 42 65 67 69 6e 50 61 72 73 65 28 70  ite3BeginParse(p
31a6a 50 61 72 73 65 2c 20 32 29 3b 20 7d 0a 20 20 20  Parse, 2); }.   
31a6b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
31a6c 20 20 63 61 73 65 20 38 3a 20 2f 2a 20 63 6d 64    case 8: /* cmd
31a6d 78 20 3a 3a 3d 20 63 6d 64 20 2a 2f 0a 7b 20 73  x ::= cmd */.{ s
31a6e 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69  qlite3FinishCodi
31a6f 6e 67 28 70 50 61 72 73 65 29 3b 20 7d 0a 20 20  ng(pParse); }.  
31a70 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31a71 20 20 20 63 61 73 65 20 39 3a 20 2f 2a 20 63 6d     case 9: /* cm
31a72 64 20 3a 3a 3d 20 42 45 47 49 4e 20 74 72 61 6e  d ::= BEGIN tran
31a73 73 74 79 70 65 20 74 72 61 6e 73 5f 6f 70 74 20  stype trans_opt 
31a74 2a 2f 0a 7b 73 71 6c 69 74 65 33 42 65 67 69 6e  */.{sqlite3Begin
31a75 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 72  Transaction(pPar
31a76 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  se, yymsp[-1].mi
31a77 6e 6f 72 2e 79 79 33 32 38 29 3b 7d 0a 20 20 20  nor.yy328);}.   
31a78 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
31a79 20 20 63 61 73 65 20 31 33 3a 20 2f 2a 20 74 72    case 13: /* tr
31a7a 61 6e 73 74 79 70 65 20 3a 3a 3d 20 2a 2f 0a 7b  anstype ::= */.{
31a7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32  yygotominor.yy32
31a7c 38 20 3d 20 54 4b 5f 44 45 46 45 52 52 45 44 3b  8 = TK_DEFERRED;
31a7d 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31a7e 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 3a 20  .      case 14: 
31a7f 2f 2a 20 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d  /* transtype ::=
31a80 20 44 45 46 45 52 52 45 44 20 2a 2f 0a 20 20 20   DEFERRED */.   
31a81 20 20 20 63 61 73 65 20 31 35 3a 20 2f 2a 20 74     case 15: /* t
31a82 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 49 4d 4d  ranstype ::= IMM
31a83 45 44 49 41 54 45 20 2a 2f 20 79 79 74 65 73 74  EDIATE */ yytest
31a84 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
31a85 35 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  5);.      case 1
31a86 36 3a 20 2f 2a 20 74 72 61 6e 73 74 79 70 65 20  6: /* transtype 
31a87 3a 3a 3d 20 45 58 43 4c 55 53 49 56 45 20 2a 2f  ::= EXCLUSIVE */
31a88 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
31a89 6c 65 6e 6f 3d 3d 31 36 29 3b 0a 20 20 20 20 20  leno==16);.     
31a8a 20 63 61 73 65 20 31 31 34 3a 20 2f 2a 20 6d 75   case 114: /* mu
31a8b 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d  ltiselect_op ::=
31a8c 20 55 4e 49 4f 4e 20 2a 2f 20 79 79 74 65 73 74   UNION */ yytest
31a8d 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
31a8e 31 34 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  14);.      case 
31a8f 31 31 36 3a 20 2f 2a 20 6d 75 6c 74 69 73 65 6c  116: /* multisel
31a90 65 63 74 5f 6f 70 20 3a 3a 3d 20 45 58 43 45 50  ect_op ::= EXCEP
31a91 54 7c 49 4e 54 45 52 53 45 43 54 20 2a 2f 20 79  T|INTERSECT */ y
31a92 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31a93 6e 6f 3d 3d 31 31 36 29 3b 0a 7b 79 79 67 6f 74  no==116);.{yygot
31a94 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 79  ominor.yy328 = y
31a95 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 3b 7d 0a  ymsp[0].major;}.
31a96 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31a97 20 20 20 20 20 63 61 73 65 20 31 37 3a 20 2f 2a       case 17: /*
31a98 20 63 6d 64 20 3a 3a 3d 20 43 4f 4d 4d 49 54 20   cmd ::= COMMIT 
31a99 74 72 61 6e 73 5f 6f 70 74 20 2a 2f 0a 20 20 20  trans_opt */.   
31a9a 20 20 20 63 61 73 65 20 31 38 3a 20 2f 2a 20 63     case 18: /* c
31a9b 6d 64 20 3a 3a 3d 20 45 4e 44 20 74 72 61 6e 73  md ::= END trans
31a9c 5f 6f 70 74 20 2a 2f 20 79 79 74 65 73 74 63 61  _opt */ yytestca
31a9d 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 38 29  se(yyruleno==18)
31a9e 3b 0a 7b 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74  ;.{sqlite3Commit
31a9f 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 72  Transaction(pPar
31aa0 73 65 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  se);}.        br
31aa1 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31aa2 31 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52  19: /* cmd ::= R
31aa3 4f 4c 4c 42 41 43 4b 20 74 72 61 6e 73 5f 6f 70  OLLBACK trans_op
31aa4 74 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 52 6f 6c  t */.{sqlite3Rol
31aa5 6c 62 61 63 6b 54 72 61 6e 73 61 63 74 69 6f 6e  lbackTransaction
31aa6 28 70 50 61 72 73 65 29 3b 7d 0a 20 20 20 20 20  (pParse);}.     
31aa7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31aa8 63 61 73 65 20 32 32 3a 20 2f 2a 20 63 6d 64 20  case 22: /* cmd 
31aa9 3a 3a 3d 20 53 41 56 45 50 4f 49 4e 54 20 6e 6d  ::= SAVEPOINT nm
31aaa 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53   */.{.  sqlite3S
31aab 61 76 65 70 6f 69 6e 74 28 70 50 61 72 73 65 2c  avepoint(pParse,
31aac 20 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e   SAVEPOINT_BEGIN
31aad 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
31aae 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20  r.yy0);.}.      
31aaf 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31ab0 61 73 65 20 32 33 3a 20 2f 2a 20 63 6d 64 20 3a  ase 23: /* cmd :
31ab1 3a 3d 20 52 45 4c 45 41 53 45 20 73 61 76 65 70  := RELEASE savep
31ab2 6f 69 6e 74 5f 6f 70 74 20 6e 6d 20 2a 2f 0a 7b  oint_opt nm */.{
31ab3 0a 20 20 73 71 6c 69 74 65 33 53 61 76 65 70 6f  .  sqlite3Savepo
31ab4 69 6e 74 28 70 50 61 72 73 65 2c 20 53 41 56 45  int(pParse, SAVE
31ab5 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 26  POINT_RELEASE, &
31ab6 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
31ab7 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  y0);.}.        b
31ab8 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31ab9 20 32 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20   24: /* cmd ::= 
31aba 52 4f 4c 4c 42 41 43 4b 20 74 72 61 6e 73 5f 6f  ROLLBACK trans_o
31abb 70 74 20 54 4f 20 73 61 76 65 70 6f 69 6e 74 5f  pt TO savepoint_
31abc 6f 70 74 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71  opt nm */.{.  sq
31abd 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28 70  lite3Savepoint(p
31abe 50 61 72 73 65 2c 20 53 41 56 45 50 4f 49 4e 54  Parse, SAVEPOINT
31abf 5f 52 4f 4c 4c 42 41 43 4b 2c 20 26 79 79 6d 73  _ROLLBACK, &yyms
31ac0 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
31ac1 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
31ac2 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 36 3a  ;.      case 26:
31ac3 20 2f 2a 20 63 72 65 61 74 65 5f 74 61 62 6c 65   /* create_table
31ac4 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74 65   ::= createkw te
31ac5 6d 70 20 54 41 42 4c 45 20 69 66 6e 6f 74 65 78  mp TABLE ifnotex
31ac6 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a  ists nm dbnm */.
31ac7 7b 0a 20 20 20 73 71 6c 69 74 65 33 53 74 61 72  {.   sqlite3Star
31ac8 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 26 79  tTable(pParse,&y
31ac9 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
31aca 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
31acb 6f 72 2e 79 79 30 2c 79 79 6d 73 70 5b 2d 34 5d  or.yy0,yymsp[-4]
31acc 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 30 2c 30  .minor.yy328,0,0
31acd 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  ,yymsp[-2].minor
31ace 2e 79 79 33 32 38 29 3b 0a 7d 0a 20 20 20 20 20  .yy328);.}.     
31acf 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31ad0 63 61 73 65 20 32 37 3a 20 2f 2a 20 63 72 65 61  case 27: /* crea
31ad1 74 65 6b 77 20 3a 3a 3d 20 43 52 45 41 54 45 20  tekw ::= CREATE 
31ad2 2a 2f 0a 7b 0a 20 20 70 50 61 72 73 65 2d 3e 64  */.{.  pParse->d
31ad3 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e  b->lookaside.bEn
31ad4 61 62 6c 65 64 20 3d 20 30 3b 0a 20 20 79 79 67  abled = 0;.  yyg
31ad5 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 79  otominor.yy0 = y
31ad6 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
31ad7 30 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0;.}.        bre
31ad8 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
31ad9 38 3a 20 2f 2a 20 69 66 6e 6f 74 65 78 69 73 74  8: /* ifnotexist
31ada 73 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63  s ::= */.      c
31adb 61 73 65 20 33 31 3a 20 2f 2a 20 74 65 6d 70 20  ase 31: /* temp 
31adc 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
31add 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 31 29 3b  e(yyruleno==31);
31ade 0a 20 20 20 20 20 20 63 61 73 65 20 37 30 3a 20  .      case 70: 
31adf 2f 2a 20 61 75 74 6f 69 6e 63 20 3a 3a 3d 20 2a  /* autoinc ::= *
31ae0 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
31ae1 75 6c 65 6e 6f 3d 3d 37 30 29 3b 0a 20 20 20 20  uleno==70);.    
31ae2 20 20 63 61 73 65 20 38 32 3a 20 2f 2a 20 64 65    case 82: /* de
31ae3 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 3a 3a  fer_subclause ::
31ae4 3d 20 4e 4f 54 20 44 45 46 45 52 52 41 42 4c 45  = NOT DEFERRABLE
31ae5 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70   init_deferred_p
31ae6 72 65 64 5f 6f 70 74 20 2a 2f 20 79 79 74 65 73  red_opt */ yytes
31ae7 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
31ae8 38 32 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  82);.      case 
31ae9 38 34 3a 20 2f 2a 20 69 6e 69 74 5f 64 65 66 65  84: /* init_defe
31aea 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a  rred_pred_opt ::
31aeb 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
31aec 79 79 72 75 6c 65 6e 6f 3d 3d 38 34 29 3b 0a 20  yyruleno==84);. 
31aed 20 20 20 20 20 63 61 73 65 20 38 36 3a 20 2f 2a       case 86: /*
31aee 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70   init_deferred_p
31aef 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54  red_opt ::= INIT
31af0 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 20  IALLY IMMEDIATE 
31af1 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31af2 72 75 6c 65 6e 6f 3d 3d 38 36 29 3b 0a 20 20 20  ruleno==86);.   
31af3 20 20 20 63 61 73 65 20 39 37 3a 20 2f 2a 20 64     case 97: /* d
31af4 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f  efer_subclause_o
31af5 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  pt ::= */ yytest
31af6 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 39  case(yyruleno==9
31af7 37 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  7);.      case 1
31af8 30 38 3a 20 2f 2a 20 69 66 65 78 69 73 74 73 20  08: /* ifexists 
31af9 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
31afa 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 30 38 29  e(yyruleno==108)
31afb 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 39  ;.      case 119
31afc 3a 20 2f 2a 20 64 69 73 74 69 6e 63 74 20 3a 3a  : /* distinct ::
31afd 3d 20 41 4c 4c 20 2a 2f 20 79 79 74 65 73 74 63  = ALL */ yytestc
31afe 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 31  ase(yyruleno==11
31aff 39 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  9);.      case 1
31b00 32 30 3a 20 2f 2a 20 64 69 73 74 69 6e 63 74 20  20: /* distinct 
31b01 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
31b02 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 32 30 29  e(yyruleno==120)
31b03 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 32  ;.      case 222
31b04 3a 20 2f 2a 20 62 65 74 77 65 65 6e 5f 6f 70 20  : /* between_op 
31b05 3a 3a 3d 20 42 45 54 57 45 45 4e 20 2a 2f 20 79  ::= BETWEEN */ y
31b06 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31b07 6e 6f 3d 3d 32 32 32 29 3b 0a 20 20 20 20 20 20  no==222);.      
31b08 63 61 73 65 20 32 32 35 3a 20 2f 2a 20 69 6e 5f  case 225: /* in_
31b09 6f 70 20 3a 3a 3d 20 49 4e 20 2a 2f 20 79 79 74  op ::= IN */ yyt
31b0a 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
31b0b 3d 3d 32 32 35 29 3b 0a 7b 79 79 67 6f 74 6f 6d  ==225);.{yygotom
31b0c 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 30 3b 7d  inor.yy328 = 0;}
31b0d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31b0e 20 20 20 20 20 20 63 61 73 65 20 32 39 3a 20 2f        case 29: /
31b0f 2a 20 69 66 6e 6f 74 65 78 69 73 74 73 20 3a 3a  * ifnotexists ::
31b10 3d 20 49 46 20 4e 4f 54 20 45 58 49 53 54 53 20  = IF NOT EXISTS 
31b11 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33 30  */.      case 30
31b12 3a 20 2f 2a 20 74 65 6d 70 20 3a 3a 3d 20 54 45  : /* temp ::= TE
31b13 4d 50 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  MP */ yytestcase
31b14 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 30 29 3b 0a  (yyruleno==30);.
31b15 20 20 20 20 20 20 63 61 73 65 20 37 31 3a 20 2f        case 71: /
31b16 2a 20 61 75 74 6f 69 6e 63 20 3a 3a 3d 20 41 55  * autoinc ::= AU
31b17 54 4f 49 4e 43 52 20 2a 2f 20 79 79 74 65 73 74  TOINCR */ yytest
31b18 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 37  case(yyruleno==7
31b19 31 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38  1);.      case 8
31b1a 35 3a 20 2f 2a 20 69 6e 69 74 5f 64 65 66 65 72  5: /* init_defer
31b1b 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d  red_pred_opt ::=
31b1c 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52   INITIALLY DEFER
31b1d 52 45 44 20 2a 2f 20 79 79 74 65 73 74 63 61 73  RED */ yytestcas
31b1e 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 38 35 29 3b  e(yyruleno==85);
31b1f 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 37 3a  .      case 107:
31b20 20 2f 2a 20 69 66 65 78 69 73 74 73 20 3a 3a 3d   /* ifexists ::=
31b21 20 49 46 20 45 58 49 53 54 53 20 2a 2f 20 79 79   IF EXISTS */ yy
31b22 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
31b23 6f 3d 3d 31 30 37 29 3b 0a 20 20 20 20 20 20 63  o==107);.      c
31b24 61 73 65 20 31 31 38 3a 20 2f 2a 20 64 69 73 74  ase 118: /* dist
31b25 69 6e 63 74 20 3a 3a 3d 20 44 49 53 54 49 4e 43  inct ::= DISTINC
31b26 54 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  T */ yytestcase(
31b27 79 79 72 75 6c 65 6e 6f 3d 3d 31 31 38 29 3b 0a  yyruleno==118);.
31b28 20 20 20 20 20 20 63 61 73 65 20 32 32 33 3a 20        case 223: 
31b29 2f 2a 20 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a  /* between_op ::
31b2a 3d 20 4e 4f 54 20 42 45 54 57 45 45 4e 20 2a 2f  = NOT BETWEEN */
31b2b 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
31b2c 6c 65 6e 6f 3d 3d 32 32 33 29 3b 0a 20 20 20 20  leno==223);.    
31b2d 20 20 63 61 73 65 20 32 32 36 3a 20 2f 2a 20 69    case 226: /* i
31b2e 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 49 4e 20  n_op ::= NOT IN 
31b2f 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31b30 72 75 6c 65 6e 6f 3d 3d 32 32 36 29 3b 0a 7b 79  ruleno==226);.{y
31b31 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38  ygotominor.yy328
31b32 20 3d 20 31 3b 7d 0a 20 20 20 20 20 20 20 20 62   = 1;}.        b
31b33 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31b34 20 33 32 3a 20 2f 2a 20 63 72 65 61 74 65 5f 74   32: /* create_t
31b35 61 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 4c 50  able_args ::= LP
31b36 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 63 6f 6e 73   columnlist cons
31b37 6c 69 73 74 5f 6f 70 74 20 52 50 20 2a 2f 0a 7b  list_opt RP */.{
31b38 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62  .  sqlite3EndTab
31b39 6c 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  le(pParse,&yymsp
31b3a 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-1].minor.yy0,&
31b3b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
31b3c 79 30 2c 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y0,0);.}.       
31b3d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31b3e 73 65 20 33 33 3a 20 2f 2a 20 63 72 65 61 74 65  se 33: /* create
31b3f 5f 74 61 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20  _table_args ::= 
31b40 41 53 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a 20  AS select */.{. 
31b41 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65   sqlite3EndTable
31b42 28 70 50 61 72 73 65 2c 30 2c 30 2c 79 79 6d 73  (pParse,0,0,yyms
31b43 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b  p[0].minor.yy3);
31b44 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
31b45 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
31b46 62 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  b, yymsp[0].mino
31b47 72 2e 79 79 33 29 3b 0a 7d 0a 20 20 20 20 20 20  r.yy3);.}.      
31b48 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31b49 61 73 65 20 33 36 3a 20 2f 2a 20 63 6f 6c 75 6d  ase 36: /* colum
31b4a 6e 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 69 64 20 74  n ::= columnid t
31b4b 79 70 65 20 63 61 72 67 6c 69 73 74 20 2a 2f 0a  ype carglist */.
31b4c 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
31b4d 79 79 30 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 32  yy0.z = yymsp[-2
31b4e 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20  ].minor.yy0.z;. 
31b4f 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30   yygotominor.yy0
31b50 2e 6e 20 3d 20 28 69 6e 74 29 28 70 50 61 72 73  .n = (int)(pPars
31b51 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a 2d  e->sLastToken.z-
31b52 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
31b53 79 79 30 2e 7a 29 20 2b 20 70 50 61 72 73 65 2d  yy0.z) + pParse-
31b54 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 6e 3b 0a 7d  >sLastToken.n;.}
31b55 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31b56 20 20 20 20 20 20 63 61 73 65 20 33 37 3a 20 2f        case 37: /
31b57 2a 20 63 6f 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e  * columnid ::= n
31b58 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  m */.{.  sqlite3
31b59 41 64 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65  AddColumn(pParse
31b5a 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
31b5b 2e 79 79 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d  .yy0);.  yygotom
31b5c 69 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73 70  inor.yy0 = yymsp
31b5d 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a 7d  [0].minor.yy0;.}
31b5e 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31b5f 20 20 20 20 20 20 63 61 73 65 20 33 38 3a 20 2f        case 38: /
31b60 2a 20 69 64 20 3a 3a 3d 20 49 44 20 2a 2f 0a 20  * id ::= ID */. 
31b61 20 20 20 20 20 63 61 73 65 20 33 39 3a 20 2f 2a       case 39: /*
31b62 20 69 64 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20   id ::= INDEXED 
31b63 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31b64 72 75 6c 65 6e 6f 3d 3d 33 39 29 3b 0a 20 20 20  ruleno==39);.   
31b65 20 20 20 63 61 73 65 20 34 30 3a 20 2f 2a 20 69     case 40: /* i
31b66 64 73 20 3a 3a 3d 20 49 44 7c 53 54 52 49 4e 47  ds ::= ID|STRING
31b67 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
31b68 79 72 75 6c 65 6e 6f 3d 3d 34 30 29 3b 0a 20 20  yruleno==40);.  
31b69 20 20 20 20 63 61 73 65 20 34 31 3a 20 2f 2a 20      case 41: /* 
31b6a 6e 6d 20 3a 3a 3d 20 69 64 20 2a 2f 20 79 79 74  nm ::= id */ yyt
31b6b 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
31b6c 3d 3d 34 31 29 3b 0a 20 20 20 20 20 20 63 61 73  ==41);.      cas
31b6d 65 20 34 32 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d 20  e 42: /* nm ::= 
31b6e 53 54 52 49 4e 47 20 2a 2f 20 79 79 74 65 73 74  STRING */ yytest
31b6f 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34  case(yyruleno==4
31b70 32 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 34  2);.      case 4
31b71 33 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d 20 4a 4f 49  3: /* nm ::= JOI
31b72 4e 5f 4b 57 20 2a 2f 20 79 79 74 65 73 74 63 61  N_KW */ yytestca
31b73 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 33 29  se(yyruleno==43)
31b74 3b 0a 20 20 20 20 20 20 63 61 73 65 20 34 36 3a  ;.      case 46:
31b75 20 2f 2a 20 74 79 70 65 74 6f 6b 65 6e 20 3a 3a   /* typetoken ::
31b76 3d 20 74 79 70 65 6e 61 6d 65 20 2a 2f 20 79 79  = typename */ yy
31b77 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
31b78 6f 3d 3d 34 36 29 3b 0a 20 20 20 20 20 20 63 61  o==46);.      ca
31b79 73 65 20 34 39 3a 20 2f 2a 20 74 79 70 65 6e 61  se 49: /* typena
31b7a 6d 65 20 3a 3a 3d 20 69 64 73 20 2a 2f 20 79 79  me ::= ids */ yy
31b7b 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
31b7c 6f 3d 3d 34 39 29 3b 0a 20 20 20 20 20 20 63 61  o==49);.      ca
31b7d 73 65 20 31 32 36 3a 20 2f 2a 20 61 73 20 3a 3a  se 126: /* as ::
31b7e 3d 20 41 53 20 6e 6d 20 2a 2f 20 79 79 74 65 73  = AS nm */ yytes
31b7f 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
31b80 31 32 36 29 3b 0a 20 20 20 20 20 20 63 61 73 65  126);.      case
31b81 20 31 32 37 3a 20 2f 2a 20 61 73 20 3a 3a 3d 20   127: /* as ::= 
31b82 69 64 73 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ids */ yytestcas
31b83 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 32 37 29  e(yyruleno==127)
31b84 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 37  ;.      case 137
31b85 3a 20 2f 2a 20 64 62 6e 6d 20 3a 3a 3d 20 44 4f  : /* dbnm ::= DO
31b86 54 20 6e 6d 20 2a 2f 20 79 79 74 65 73 74 63 61  T nm */ yytestca
31b87 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 33 37  se(yyruleno==137
31b88 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 34  );.      case 14
31b89 36 3a 20 2f 2a 20 69 6e 64 65 78 65 64 5f 6f 70  6: /* indexed_op
31b8a 74 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20 42 59  t ::= INDEXED BY
31b8b 20 6e 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73   nm */ yytestcas
31b8c 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 34 36 29  e(yyruleno==146)
31b8d 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 31  ;.      case 251
31b8e 3a 20 2f 2a 20 63 6f 6c 6c 61 74 65 20 3a 3a 3d  : /* collate ::=
31b8f 20 43 4f 4c 4c 41 54 45 20 69 64 73 20 2a 2f 20   COLLATE ids */ 
31b90 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31b91 65 6e 6f 3d 3d 32 35 31 29 3b 0a 20 20 20 20 20  eno==251);.     
31b92 20 63 61 73 65 20 32 36 30 3a 20 2f 2a 20 6e 6d   case 260: /* nm
31b93 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d  num ::= plus_num
31b94 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
31b95 79 72 75 6c 65 6e 6f 3d 3d 32 36 30 29 3b 0a 20  yruleno==260);. 
31b96 20 20 20 20 20 63 61 73 65 20 32 36 31 3a 20 2f       case 261: /
31b97 2a 20 6e 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d 20 2a  * nmnum ::= nm *
31b98 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
31b99 75 6c 65 6e 6f 3d 3d 32 36 31 29 3b 0a 20 20 20  uleno==261);.   
31b9a 20 20 20 63 61 73 65 20 32 36 32 3a 20 2f 2a 20     case 262: /* 
31b9b 6e 6d 6e 75 6d 20 3a 3a 3d 20 4f 4e 20 2a 2f 20  nmnum ::= ON */ 
31b9c 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31b9d 65 6e 6f 3d 3d 32 36 32 29 3b 0a 20 20 20 20 20  eno==262);.     
31b9e 20 63 61 73 65 20 32 36 33 3a 20 2f 2a 20 6e 6d   case 263: /* nm
31b9f 6e 75 6d 20 3a 3a 3d 20 44 45 4c 45 54 45 20 2a  num ::= DELETE *
31ba0 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
31ba1 75 6c 65 6e 6f 3d 3d 32 36 33 29 3b 0a 20 20 20  uleno==263);.   
31ba2 20 20 20 63 61 73 65 20 32 36 34 3a 20 2f 2a 20     case 264: /* 
31ba3 6e 6d 6e 75 6d 20 3a 3a 3d 20 44 45 46 41 55 4c  nmnum ::= DEFAUL
31ba4 54 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  T */ yytestcase(
31ba5 79 79 72 75 6c 65 6e 6f 3d 3d 32 36 34 29 3b 0a  yyruleno==264);.
31ba6 20 20 20 20 20 20 63 61 73 65 20 32 36 35 3a 20        case 265: 
31ba7 2f 2a 20 70 6c 75 73 5f 6e 75 6d 20 3a 3a 3d 20  /* plus_num ::= 
31ba8 70 6c 75 73 5f 6f 70 74 20 6e 75 6d 62 65 72 20  plus_opt number 
31ba9 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31baa 72 75 6c 65 6e 6f 3d 3d 32 36 35 29 3b 0a 20 20  ruleno==265);.  
31bab 20 20 20 20 63 61 73 65 20 32 36 36 3a 20 2f 2a      case 266: /*
31bac 20 6d 69 6e 75 73 5f 6e 75 6d 20 3a 3a 3d 20 4d   minus_num ::= M
31bad 49 4e 55 53 20 6e 75 6d 62 65 72 20 2a 2f 20 79  INUS number */ y
31bae 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31baf 6e 6f 3d 3d 32 36 36 29 3b 0a 20 20 20 20 20 20  no==266);.      
31bb0 63 61 73 65 20 32 36 37 3a 20 2f 2a 20 6e 75 6d  case 267: /* num
31bb1 62 65 72 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c  ber ::= INTEGER|
31bb2 46 4c 4f 41 54 20 2a 2f 20 79 79 74 65 73 74 63  FLOAT */ yytestc
31bb3 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 36  ase(yyruleno==26
31bb4 37 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  7);.      case 2
31bb5 38 35 3a 20 2f 2a 20 74 72 6e 6d 20 3a 3a 3d 20  85: /* trnm ::= 
31bb6 6e 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  nm */ yytestcase
31bb7 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 38 35 29 3b  (yyruleno==285);
31bb8 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
31bb9 30 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0 = yymsp[0].min
31bba 6f 72 2e 79 79 30 3b 7d 0a 20 20 20 20 20 20 20  or.yy0;}.       
31bbb 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31bbc 73 65 20 34 35 3a 20 2f 2a 20 74 79 70 65 20 3a  se 45: /* type :
31bbd 3a 3d 20 74 79 70 65 74 6f 6b 65 6e 20 2a 2f 0a  := typetoken */.
31bbe 7b 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d  {sqlite3AddColum
31bbf 6e 54 79 70 65 28 70 50 61 72 73 65 2c 26 79 79  nType(pParse,&yy
31bc0 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
31bc1 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
31bc2 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 34 37  k;.      case 47
31bc3 3a 20 2f 2a 20 74 79 70 65 74 6f 6b 65 6e 20 3a  : /* typetoken :
31bc4 3a 3d 20 74 79 70 65 6e 61 6d 65 20 4c 50 20 73  := typename LP s
31bc5 69 67 6e 65 64 20 52 50 20 2a 2f 0a 7b 0a 20 20  igned RP */.{.  
31bc6 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
31bc7 7a 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  z = yymsp[-3].mi
31bc8 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67  nor.yy0.z;.  yyg
31bc9 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d  otominor.yy0.n =
31bca 20 28 69 6e 74 29 28 26 79 79 6d 73 70 5b 30 5d   (int)(&yymsp[0]
31bcb 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d  .minor.yy0.z[yym
31bcc 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
31bcd 6e 5d 20 2d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d  n] - yymsp[-3].m
31bce 69 6e 6f 72 2e 79 79 30 2e 7a 29 3b 0a 7d 0a 20  inor.yy0.z);.}. 
31bcf 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31bd0 20 20 20 20 63 61 73 65 20 34 38 3a 20 2f 2a 20      case 48: /* 
31bd1 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79  typetoken ::= ty
31bd2 70 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64  pename LP signed
31bd3 20 43 4f 4d 4d 41 20 73 69 67 6e 65 64 20 52 50   COMMA signed RP
31bd4 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
31bd5 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 79 79 6d 73  nor.yy0.z = yyms
31bd6 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  p[-5].minor.yy0.
31bd7 7a 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  z;.  yygotominor
31bd8 2e 79 79 30 2e 6e 20 3d 20 28 69 6e 74 29 28 26  .yy0.n = (int)(&
31bd9 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
31bda 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69  y0.z[yymsp[0].mi
31bdb 6e 6f 72 2e 79 79 30 2e 6e 5d 20 2d 20 79 79 6d  nor.yy0.n] - yym
31bdc 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-5].minor.yy0
31bdd 2e 7a 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  .z);.}.        b
31bde 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31bdf 20 35 30 3a 20 2f 2a 20 74 79 70 65 6e 61 6d 65   50: /* typename
31be0 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20 69 64   ::= typename id
31be1 73 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  s */.{yygotomino
31be2 72 2e 79 79 30 2e 7a 3d 79 79 6d 73 70 5b 2d 31  r.yy0.z=yymsp[-1
31be3 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 20 79  ].minor.yy0.z; y
31be4 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e  ygotominor.yy0.n
31be5 3d 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  =yymsp[0].minor.
31be6 79 79 30 2e 6e 2b 28 69 6e 74 29 28 79 79 6d 73  yy0.n+(int)(yyms
31be7 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  p[0].minor.yy0.z
31be8 2d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  -yymsp[-1].minor
31be9 2e 79 79 30 2e 7a 29 3b 7d 0a 20 20 20 20 20 20  .yy0.z);}.      
31bea 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31beb 61 73 65 20 35 37 3a 20 2f 2a 20 63 63 6f 6e 73  ase 57: /* ccons
31bec 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 74 65 72   ::= DEFAULT ter
31bed 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  m */.      case 
31bee 35 39 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d  59: /* ccons ::=
31bef 20 44 45 46 41 55 4c 54 20 50 4c 55 53 20 74 65   DEFAULT PLUS te
31bf0 72 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  rm */ yytestcase
31bf1 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 39 29 3b 0a  (yyruleno==59);.
31bf2 7b 73 71 6c 69 74 65 33 41 64 64 44 65 66 61 75  {sqlite3AddDefau
31bf3 6c 74 56 61 6c 75 65 28 70 50 61 72 73 65 2c 26  ltValue(pParse,&
31bf4 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
31bf5 79 33 34 36 29 3b 7d 0a 20 20 20 20 20 20 20 20  y346);}.        
31bf6 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
31bf7 65 20 35 38 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a  e 58: /* ccons :
31bf8 3a 3d 20 44 45 46 41 55 4c 54 20 4c 50 20 65 78  := DEFAULT LP ex
31bf9 70 72 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65  pr RP */.{sqlite
31bfa 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65  3AddDefaultValue
31bfb 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d  (pParse,&yymsp[-
31bfc 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 29 3b  1].minor.yy346);
31bfd 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31bfe 0a 20 20 20 20 20 20 63 61 73 65 20 36 30 3a 20  .      case 60: 
31bff 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46  /* ccons ::= DEF
31c00 41 55 4c 54 20 4d 49 4e 55 53 20 74 65 72 6d 20  AULT MINUS term 
31c01 2a 2f 0a 7b 0a 20 20 45 78 70 72 53 70 61 6e 20  */.{.  ExprSpan 
31c02 76 3b 0a 20 20 76 2e 70 45 78 70 72 20 3d 20 73  v;.  v.pExpr = s
31c03 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
31c04 73 65 2c 20 54 4b 5f 55 4d 49 4e 55 53 2c 20 79  se, TK_UMINUS, y
31c05 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
31c06 33 34 36 2e 70 45 78 70 72 2c 20 30 2c 20 30 29  346.pExpr, 0, 0)
31c07 3b 0a 20 20 76 2e 7a 53 74 61 72 74 20 3d 20 79  ;.  v.zStart = y
31c08 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
31c09 79 30 2e 7a 3b 0a 20 20 76 2e 7a 45 6e 64 20 3d  y0.z;.  v.zEnd =
31c0a 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
31c0b 79 79 33 34 36 2e 7a 45 6e 64 3b 0a 20 20 73 71  yy346.zEnd;.  sq
31c0c 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 56  lite3AddDefaultV
31c0d 61 6c 75 65 28 70 50 61 72 73 65 2c 26 76 29 3b  alue(pParse,&v);
31c0e 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
31c0f 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 31 3a  ;.      case 61:
31c10 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45   /* ccons ::= DE
31c11 46 41 55 4c 54 20 69 64 20 2a 2f 0a 7b 0a 20 20  FAULT id */.{.  
31c12 45 78 70 72 53 70 61 6e 20 76 3b 0a 20 20 73 70  ExprSpan v;.  sp
31c13 61 6e 45 78 70 72 28 26 76 2c 20 70 50 61 72 73  anExpr(&v, pPars
31c14 65 2c 20 54 4b 5f 53 54 52 49 4e 47 2c 20 26 79  e, TK_STRING, &y
31c15 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
31c16 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 41 64 64  0);.  sqlite3Add
31c17 44 65 66 61 75 6c 74 56 61 6c 75 65 28 70 50 61  DefaultValue(pPa
31c18 72 73 65 2c 26 76 29 3b 0a 7d 0a 20 20 20 20 20  rse,&v);.}.     
31c19 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31c1a 63 61 73 65 20 36 33 3a 20 2f 2a 20 63 63 6f 6e  case 63: /* ccon
31c1b 73 20 3a 3a 3d 20 4e 4f 54 20 4e 55 4c 4c 20 6f  s ::= NOT NULL o
31c1c 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65  nconf */.{sqlite
31c1d 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 70 50 61 72  3AddNotNull(pPar
31c1e 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  se, yymsp[0].min
31c1f 6f 72 2e 79 79 33 32 38 29 3b 7d 0a 20 20 20 20  or.yy328);}.    
31c20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
31c21 20 63 61 73 65 20 36 34 3a 20 2f 2a 20 63 63 6f   case 64: /* cco
31c22 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b  ns ::= PRIMARY K
31c23 45 59 20 73 6f 72 74 6f 72 64 65 72 20 6f 6e 63  EY sortorder onc
31c24 6f 6e 66 20 61 75 74 6f 69 6e 63 20 2a 2f 0a 7b  onf autoinc */.{
31c25 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72  sqlite3AddPrimar
31c26 79 4b 65 79 28 70 50 61 72 73 65 2c 30 2c 79 79  yKey(pParse,0,yy
31c27 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
31c28 33 32 38 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  328,yymsp[0].min
31c29 6f 72 2e 79 79 33 32 38 2c 79 79 6d 73 70 5b 2d  or.yy328,yymsp[-
31c2a 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b  2].minor.yy328);
31c2b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31c2c 0a 20 20 20 20 20 20 63 61 73 65 20 36 35 3a 20  .      case 65: 
31c2d 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49  /* ccons ::= UNI
31c2e 51 55 45 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73  QUE onconf */.{s
31c2f 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65  qlite3CreateInde
31c30 78 28 70 50 61 72 73 65 2c 30 2c 30 2c 30 2c 30  x(pParse,0,0,0,0
31c31 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ,yymsp[0].minor.
31c32 79 79 33 32 38 2c 30 2c 30 2c 30 2c 30 29 3b 7d  yy328,0,0,0,0);}
31c33 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31c34 20 20 20 20 20 20 63 61 73 65 20 36 36 3a 20 2f        case 66: /
31c35 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43  * ccons ::= CHEC
31c36 4b 20 4c 50 20 65 78 70 72 20 52 50 20 2a 2f 0a  K LP expr RP */.
31c37 7b 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b  {sqlite3AddCheck
31c38 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 72 73  Constraint(pPars
31c39 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  e,yymsp[-1].mino
31c3a 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 7d  r.yy346.pExpr);}
31c3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31c3c 20 20 20 20 20 20 63 61 73 65 20 36 37 3a 20 2f        case 67: /
31c3d 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 52 45 46 45  * ccons ::= REFE
31c3e 52 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c 69 73  RENCES nm idxlis
31c3f 74 5f 6f 70 74 20 72 65 66 61 72 67 73 20 2a 2f  t_opt refargs */
31c40 0a 7b 73 71 6c 69 74 65 33 43 72 65 61 74 65 46  .{sqlite3CreateF
31c41 6f 72 65 69 67 6e 4b 65 79 28 70 50 61 72 73 65  oreignKey(pParse
31c42 2c 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  ,0,&yymsp[-2].mi
31c43 6e 6f 72 2e 79 79 30 2c 79 79 6d 73 70 5b 2d 31  nor.yy0,yymsp[-1
31c44 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d  ].minor.yy14,yym
31c45 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  sp[0].minor.yy32
31c46 38 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  8);}.        bre
31c47 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36  ak;.      case 6
31c48 38 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20  8: /* ccons ::= 
31c49 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20  defer_subclause 
31c4a 2a 2f 0a 7b 73 71 6c 69 74 65 33 44 65 66 65 72  */.{sqlite3Defer
31c4b 46 6f 72 65 69 67 6e 4b 65 79 28 70 50 61 72 73  ForeignKey(pPars
31c4c 65 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  e,yymsp[0].minor
31c4d 2e 79 79 33 32 38 29 3b 7d 0a 20 20 20 20 20 20  .yy328);}.      
31c4e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31c4f 61 73 65 20 36 39 3a 20 2f 2a 20 63 63 6f 6e 73  ase 69: /* ccons
31c50 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64 73   ::= COLLATE ids
31c51 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 43   */.{sqlite3AddC
31c52 6f 6c 6c 61 74 65 54 79 70 65 28 70 50 61 72 73  ollateType(pPars
31c53 65 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  e, &yymsp[0].min
31c54 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
31c55 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31c56 61 73 65 20 37 32 3a 20 2f 2a 20 72 65 66 61 72  ase 72: /* refar
31c57 67 73 20 3a 3a 3d 20 2a 2f 0a 7b 20 79 79 67 6f  gs ::= */.{ yygo
31c58 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20  tominor.yy328 = 
31c59 4f 45 5f 4e 6f 6e 65 20 2a 20 30 78 30 30 30 31  OE_None * 0x0001
31c5a 30 31 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  01; }.        br
31c5b 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31c5c 37 33 3a 20 2f 2a 20 72 65 66 61 72 67 73 20 3a  73: /* refargs :
31c5d 3a 3d 20 72 65 66 61 72 67 73 20 72 65 66 61 72  := refargs refar
31c5e 67 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  g */.{ yygotomin
31c5f 6f 72 2e 79 79 33 32 38 20 3d 20 28 79 79 6d 73  or.yy328 = (yyms
31c60 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-1].minor.yy32
31c61 38 20 26 20 7e 79 79 6d 73 70 5b 30 5d 2e 6d 69  8 & ~yymsp[0].mi
31c62 6e 6f 72 2e 79 79 34 32 39 2e 6d 61 73 6b 29 20  nor.yy429.mask) 
31c63 7c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  | yymsp[0].minor
31c64 2e 79 79 34 32 39 2e 76 61 6c 75 65 3b 20 7d 0a  .yy429.value; }.
31c65 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31c66 20 20 20 20 20 63 61 73 65 20 37 34 3a 20 2f 2a       case 74: /*
31c67 20 72 65 66 61 72 67 20 3a 3a 3d 20 4d 41 54 43   refarg ::= MATC
31c68 48 20 6e 6d 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f  H nm */.{ yygoto
31c69 6d 69 6e 6f 72 2e 79 79 34 32 39 2e 76 61 6c 75  minor.yy429.valu
31c6a 65 20 3d 20 30 3b 20 20 20 20 20 79 79 67 6f 74  e = 0;     yygot
31c6b 6f 6d 69 6e 6f 72 2e 79 79 34 32 39 2e 6d 61 73  ominor.yy429.mas
31c6c 6b 20 3d 20 30 78 30 30 30 30 30 30 3b 20 7d 0a  k = 0x000000; }.
31c6d 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31c6e 20 20 20 20 20 63 61 73 65 20 37 35 3a 20 2f 2a       case 75: /*
31c6f 20 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 44   refarg ::= ON D
31c70 45 4c 45 54 45 20 72 65 66 61 63 74 20 2a 2f 0a  ELETE refact */.
31c71 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
31c72 34 32 39 2e 76 61 6c 75 65 20 3d 20 79 79 6d 73  429.value = yyms
31c73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  p[0].minor.yy328
31c74 3b 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  ;     yygotomino
31c75 72 2e 79 79 34 32 39 2e 6d 61 73 6b 20 3d 20 30  r.yy429.mask = 0
31c76 78 30 30 30 30 66 66 3b 20 7d 0a 20 20 20 20 20  x0000ff; }.     
31c77 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31c78 63 61 73 65 20 37 36 3a 20 2f 2a 20 72 65 66 61  case 76: /* refa
31c79 72 67 20 3a 3a 3d 20 4f 4e 20 55 50 44 41 54 45  rg ::= ON UPDATE
31c7a 20 72 65 66 61 63 74 20 2a 2f 0a 7b 20 79 79 67   refact */.{ yyg
31c7b 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 32 39 2e 76  otominor.yy429.v
31c7c 61 6c 75 65 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  alue = yymsp[0].
31c7d 6d 69 6e 6f 72 2e 79 79 33 32 38 3c 3c 38 3b 20  minor.yy328<<8; 
31c7e 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
31c7f 32 39 2e 6d 61 73 6b 20 3d 20 30 78 30 30 66 66  29.mask = 0x00ff
31c80 30 30 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  00; }.        br
31c81 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31c82 37 37 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a  77: /* refact ::
31c83 3d 20 53 45 54 20 4e 55 4c 4c 20 2a 2f 0a 7b 20  = SET NULL */.{ 
31c84 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32  yygotominor.yy32
31c85 38 20 3d 20 4f 45 5f 53 65 74 4e 75 6c 6c 3b 20  8 = OE_SetNull; 
31c86 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31c87 0a 20 20 20 20 20 20 63 61 73 65 20 37 38 3a 20  .      case 78: 
31c88 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20 53 45  /* refact ::= SE
31c89 54 20 44 45 46 41 55 4c 54 20 2a 2f 0a 7b 20 79  T DEFAULT */.{ y
31c8a 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38  ygotominor.yy328
31c8b 20 3d 20 4f 45 5f 53 65 74 44 66 6c 74 3b 20 7d   = OE_SetDflt; }
31c8c 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31c8d 20 20 20 20 20 20 63 61 73 65 20 37 39 3a 20 2f        case 79: /
31c8e 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20 43 41 53  * refact ::= CAS
31c8f 43 41 44 45 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f  CADE */.{ yygoto
31c90 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45  minor.yy328 = OE
31c91 5f 43 61 73 63 61 64 65 3b 20 7d 0a 20 20 20 20  _Cascade; }.    
31c92 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
31c93 20 63 61 73 65 20 38 30 3a 20 2f 2a 20 72 65 66   case 80: /* ref
31c94 61 63 74 20 3a 3a 3d 20 52 45 53 54 52 49 43 54  act ::= RESTRICT
31c95 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
31c96 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f 52 65 73  r.yy328 = OE_Res
31c97 74 72 69 63 74 3b 20 7d 0a 20 20 20 20 20 20 20  trict; }.       
31c98 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31c99 73 65 20 38 31 3a 20 2f 2a 20 72 65 66 61 63 74  se 81: /* refact
31c9a 20 3a 3a 3d 20 4e 4f 20 41 43 54 49 4f 4e 20 2a   ::= NO ACTION *
31c9b 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
31c9c 79 79 33 32 38 20 3d 20 4f 45 5f 4e 6f 6e 65 3b  yy328 = OE_None;
31c9d 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
31c9e 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38 33 3a  ;.      case 83:
31c9f 20 2f 2a 20 64 65 66 65 72 5f 73 75 62 63 6c 61   /* defer_subcla
31ca0 75 73 65 20 3a 3a 3d 20 44 45 46 45 52 52 41 42  use ::= DEFERRAB
31ca1 4c 45 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64  LE init_deferred
31ca2 5f 70 72 65 64 5f 6f 70 74 20 2a 2f 0a 20 20 20  _pred_opt */.   
31ca3 20 20 20 63 61 73 65 20 39 38 3a 20 2f 2a 20 64     case 98: /* d
31ca4 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f  efer_subclause_o
31ca5 70 74 20 3a 3a 3d 20 64 65 66 65 72 5f 73 75 62  pt ::= defer_sub
31ca6 63 6c 61 75 73 65 20 2a 2f 20 79 79 74 65 73 74  clause */ yytest
31ca7 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 39  case(yyruleno==9
31ca8 38 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  8);.      case 1
31ca9 30 30 3a 20 2f 2a 20 6f 6e 63 6f 6e 66 20 3a 3a  00: /* onconf ::
31caa 3d 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 72 65  = ON CONFLICT re
31cab 73 6f 6c 76 65 74 79 70 65 20 2a 2f 20 79 79 74  solvetype */ yyt
31cac 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
31cad 3d 3d 31 30 30 29 3b 0a 20 20 20 20 20 20 63 61  ==100);.      ca
31cae 73 65 20 31 30 33 3a 20 2f 2a 20 72 65 73 6f 6c  se 103: /* resol
31caf 76 65 74 79 70 65 20 3a 3a 3d 20 72 61 69 73 65  vetype ::= raise
31cb0 74 79 70 65 20 2a 2f 20 79 79 74 65 73 74 63 61  type */ yytestca
31cb1 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 30 33  se(yyruleno==103
31cb2 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  );.{yygotominor.
31cb3 79 79 33 32 38 20 3d 20 79 79 6d 73 70 5b 30 5d  yy328 = yymsp[0]
31cb4 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 7d 0a 20  .minor.yy328;}. 
31cb5 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31cb6 20 20 20 20 63 61 73 65 20 38 37 3a 20 2f 2a 20      case 87: /* 
31cb7 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d  conslist_opt ::=
31cb8 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
31cb9 2e 79 79 30 2e 6e 20 3d 20 30 3b 20 79 79 67 6f  .yy0.n = 0; yygo
31cba 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20  tominor.yy0.z = 
31cbb 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
31cbc 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38 38  k;.      case 88
31cbd 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73 74 5f 6f 70  : /* conslist_op
31cbe 74 20 3a 3a 3d 20 43 4f 4d 4d 41 20 63 6f 6e 73  t ::= COMMA cons
31cbf 6c 69 73 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  list */.{yygotom
31cc0 69 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73 70  inor.yy0 = yymsp
31cc1 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 7d  [-1].minor.yy0;}
31cc2 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31cc3 20 20 20 20 20 20 63 61 73 65 20 39 33 3a 20 2f        case 93: /
31cc4 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d  * tcons ::= PRIM
31cc5 41 52 59 20 4b 45 59 20 4c 50 20 69 64 78 6c 69  ARY KEY LP idxli
31cc6 73 74 20 61 75 74 6f 69 6e 63 20 52 50 20 6f 6e  st autoinc RP on
31cc7 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  conf */.{sqlite3
31cc8 41 64 64 50 72 69 6d 61 72 79 4b 65 79 28 70 50  AddPrimaryKey(pP
31cc9 61 72 73 65 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d  arse,yymsp[-3].m
31cca 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d 73 70 5b  inor.yy14,yymsp[
31ccb 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 79  0].minor.yy328,y
31ccc 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
31ccd 79 33 32 38 2c 30 29 3b 7d 0a 20 20 20 20 20 20  y328,0);}.      
31cce 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31ccf 61 73 65 20 39 34 3a 20 2f 2a 20 74 63 6f 6e 73  ase 94: /* tcons
31cd0 20 3a 3a 3d 20 55 4e 49 51 55 45 20 4c 50 20 69   ::= UNIQUE LP i
31cd1 64 78 6c 69 73 74 20 52 50 20 6f 6e 63 6f 6e 66  dxlist RP onconf
31cd2 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 43 72 65 61   */.{sqlite3Crea
31cd3 74 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c 30  teIndex(pParse,0
31cd4 2c 30 2c 30 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d  ,0,0,yymsp[-2].m
31cd5 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d 73 70 5b  inor.yy14,yymsp[
31cd6 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 30  0].minor.yy328,0
31cd7 2c 30 2c 30 2c 30 29 3b 7d 0a 20 20 20 20 20 20  ,0,0,0);}.      
31cd8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31cd9 61 73 65 20 39 35 3a 20 2f 2a 20 74 63 6f 6e 73  ase 95: /* tcons
31cda 20 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20 65 78   ::= CHECK LP ex
31cdb 70 72 20 52 50 20 6f 6e 63 6f 6e 66 20 2a 2f 0a  pr RP onconf */.
31cdc 7b 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b  {sqlite3AddCheck
31cdd 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 72 73  Constraint(pPars
31cde 65 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  e,yymsp[-2].mino
31cdf 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 7d  r.yy346.pExpr);}
31ce0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31ce1 20 20 20 20 20 20 63 61 73 65 20 39 36 3a 20 2f        case 96: /
31ce2 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20 46 4f 52 45  * tcons ::= FORE
31ce3 49 47 4e 20 4b 45 59 20 4c 50 20 69 64 78 6c 69  IGN KEY LP idxli
31ce4 73 74 20 52 50 20 52 45 46 45 52 45 4e 43 45 53  st RP REFERENCES
31ce5 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74 20   nm idxlist_opt 
31ce6 72 65 66 61 72 67 73 20 64 65 66 65 72 5f 73 75  refargs defer_su
31ce7 62 63 6c 61 75 73 65 5f 6f 70 74 20 2a 2f 0a 7b  bclause_opt */.{
31ce8 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72 65 61  .    sqlite3Crea
31ce9 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 70 50 61  teForeignKey(pPa
31cea 72 73 65 2c 20 79 79 6d 73 70 5b 2d 36 5d 2e 6d  rse, yymsp[-6].m
31ceb 69 6e 6f 72 2e 79 79 31 34 2c 20 26 79 79 6d 73  inor.yy14, &yyms
31cec 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-3].minor.yy0,
31ced 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
31cee 2e 79 79 31 34 2c 20 79 79 6d 73 70 5b 2d 31 5d  .yy14, yymsp[-1]
31cef 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 0a 20  .minor.yy328);. 
31cf0 20 20 20 73 71 6c 69 74 65 33 44 65 66 65 72 46     sqlite3DeferF
31cf1 6f 72 65 69 67 6e 4b 65 79 28 70 50 61 72 73 65  oreignKey(pParse
31cf2 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
31cf3 2e 79 79 33 32 38 29 3b 0a 7d 0a 20 20 20 20 20  .yy328);.}.     
31cf4 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31cf5 63 61 73 65 20 39 39 3a 20 2f 2a 20 6f 6e 63 6f  case 99: /* onco
31cf6 6e 66 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74  nf ::= */.{yygot
31cf7 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f  ominor.yy328 = O
31cf8 45 5f 44 65 66 61 75 6c 74 3b 7d 0a 20 20 20 20  E_Default;}.    
31cf9 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
31cfa 20 63 61 73 65 20 31 30 31 3a 20 2f 2a 20 6f 72   case 101: /* or
31cfb 63 6f 6e 66 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67  conf ::= */.{yyg
31cfc 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 36 20 3d  otominor.yy186 =
31cfd 20 4f 45 5f 44 65 66 61 75 6c 74 3b 7d 0a 20 20   OE_Default;}.  
31cfe 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31cff 20 20 20 63 61 73 65 20 31 30 32 3a 20 2f 2a 20     case 102: /* 
31d00 6f 72 63 6f 6e 66 20 3a 3a 3d 20 4f 52 20 72 65  orconf ::= OR re
31d01 73 6f 6c 76 65 74 79 70 65 20 2a 2f 0a 7b 79 79  solvetype */.{yy
31d02 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 36 20  gotominor.yy186 
31d03 3d 20 28 75 38 29 79 79 6d 73 70 5b 30 5d 2e 6d  = (u8)yymsp[0].m
31d04 69 6e 6f 72 2e 79 79 33 32 38 3b 7d 0a 20 20 20  inor.yy328;}.   
31d05 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
31d06 20 20 63 61 73 65 20 31 30 34 3a 20 2f 2a 20 72    case 104: /* r
31d07 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20 49  esolvetype ::= I
31d08 47 4e 4f 52 45 20 2a 2f 0a 7b 79 79 67 6f 74 6f  GNORE */.{yygoto
31d09 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45  minor.yy328 = OE
31d0a 5f 49 67 6e 6f 72 65 3b 7d 0a 20 20 20 20 20 20  _Ignore;}.      
31d0b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31d0c 61 73 65 20 31 30 35 3a 20 2f 2a 20 72 65 73 6f  ase 105: /* reso
31d0d 6c 76 65 74 79 70 65 20 3a 3a 3d 20 52 45 50 4c  lvetype ::= REPL
31d0e 41 43 45 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ACE */.{yygotomi
31d0f 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f 52  nor.yy328 = OE_R
31d10 65 70 6c 61 63 65 3b 7d 0a 20 20 20 20 20 20 20  eplace;}.       
31d11 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31d12 73 65 20 31 30 36 3a 20 2f 2a 20 63 6d 64 20 3a  se 106: /* cmd :
31d13 3a 3d 20 44 52 4f 50 20 54 41 42 4c 45 20 69 66  := DROP TABLE if
31d14 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 20  exists fullname 
31d15 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 44 72  */.{.  sqlite3Dr
31d16 6f 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  opTable(pParse, 
31d17 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
31d18 79 36 35 2c 20 30 2c 20 79 79 6d 73 70 5b 2d 31  y65, 0, yymsp[-1
31d19 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 0a  ].minor.yy328);.
31d1a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31d1b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 39 3a  .      case 109:
31d1c 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63 72 65 61   /* cmd ::= crea
31d1d 74 65 6b 77 20 74 65 6d 70 20 56 49 45 57 20 69  tekw temp VIEW i
31d1e 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62  fnotexists nm db
31d1f 6e 6d 20 41 53 20 73 65 6c 65 63 74 20 2a 2f 0a  nm AS select */.
31d20 7b 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74  {.  sqlite3Creat
31d21 65 56 69 65 77 28 70 50 61 72 73 65 2c 20 26 79  eView(pParse, &y
31d22 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-7].minor.y
31d23 79 30 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d  y0, &yymsp[-3].m
31d24 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70  inor.yy0, &yymsp
31d25 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-2].minor.yy0, 
31d26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
31d27 79 33 2c 20 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69  y3, yymsp[-6].mi
31d28 6e 6f 72 2e 79 79 33 32 38 2c 20 79 79 6d 73 70  nor.yy328, yymsp
31d29 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  [-4].minor.yy328
31d2a 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
31d2b 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
31d2c 31 30 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44  10: /* cmd ::= D
31d2d 52 4f 50 20 56 49 45 57 20 69 66 65 78 69 73 74  ROP VIEW ifexist
31d2e 73 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a  s fullname */.{.
31d2f 20 20 73 71 6c 69 74 65 33 44 72 6f 70 54 61 62    sqlite3DropTab
31d30 6c 65 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  le(pParse, yymsp
31d31 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20  [0].minor.yy65, 
31d32 31 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  1, yymsp[-1].min
31d33 6f 72 2e 79 79 33 32 38 29 3b 0a 7d 0a 20 20 20  or.yy328);.}.   
31d34 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
31d35 20 20 63 61 73 65 20 31 31 31 3a 20 2f 2a 20 63    case 111: /* c
31d36 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74 20 2a 2f  md ::= select */
31d37 0a 7b 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20  .{.  SelectDest 
31d38 64 65 73 74 20 3d 20 7b 53 52 54 5f 4f 75 74 70  dest = {SRT_Outp
31d39 75 74 2c 20 30 2c 20 30 2c 20 30 2c 20 30 7d 3b  ut, 0, 0, 0, 0};
31d3a 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
31d3b 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30  (pParse, yymsp[0
31d3c 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2c 20 26 64 65  ].minor.yy3, &de
31d3d 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  st);.  sqlite3Se
31d3e 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61 72 73  lectDelete(pPars
31d3f 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 30 5d 2e  e->db, yymsp[0].
31d40 6d 69 6e 6f 72 2e 79 79 33 29 3b 0a 7d 0a 20 20  minor.yy3);.}.  
31d41 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31d42 20 20 20 63 61 73 65 20 31 31 32 3a 20 2f 2a 20     case 112: /* 
31d43 73 65 6c 65 63 74 20 3a 3a 3d 20 6f 6e 65 73 65  select ::= onese
31d44 6c 65 63 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  lect */.{yygotom
31d45 69 6e 6f 72 2e 79 79 33 20 3d 20 79 79 6d 73 70  inor.yy3 = yymsp
31d46 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 3b 7d 0a  [0].minor.yy3;}.
31d47 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31d48 20 20 20 20 20 63 61 73 65 20 31 31 33 3a 20 2f       case 113: /
31d49 2a 20 73 65 6c 65 63 74 20 3a 3a 3d 20 73 65 6c  * select ::= sel
31d4a 65 63 74 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f  ect multiselect_
31d4b 6f 70 20 6f 6e 65 73 65 6c 65 63 74 20 2a 2f 0a  op oneselect */.
31d4c 7b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 30 5d  {.  if( yymsp[0]
31d4d 2e 6d 69 6e 6f 72 2e 79 79 33 20 29 7b 0a 20 20  .minor.yy3 ){.  
31d4e 20 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72    yymsp[0].minor
31d4f 2e 79 79 33 2d 3e 6f 70 20 3d 20 28 75 38 29 79  .yy3->op = (u8)y
31d50 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
31d51 79 33 32 38 3b 0a 20 20 20 20 79 79 6d 73 70 5b  y328;.    yymsp[
31d52 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2d 3e 70 50  0].minor.yy3->pP
31d53 72 69 6f 72 20 3d 20 79 79 6d 73 70 5b 2d 32 5d  rior = yymsp[-2]
31d54 2e 6d 69 6e 6f 72 2e 79 79 33 3b 0a 20 20 7d 65  .minor.yy3;.  }e
31d55 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
31d56 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61  SelectDelete(pPa
31d57 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d  rse->db, yymsp[-
31d58 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b 0a 20  2].minor.yy3);. 
31d59 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   }.  yygotominor
31d5a 2e 79 79 33 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  .yy3 = yymsp[0].
31d5b 6d 69 6e 6f 72 2e 79 79 33 3b 0a 7d 0a 20 20 20  minor.yy3;.}.   
31d5c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
31d5d 20 20 63 61 73 65 20 31 31 35 3a 20 2f 2a 20 6d    case 115: /* m
31d5e 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a  ultiselect_op ::
31d5f 3d 20 55 4e 49 4f 4e 20 41 4c 4c 20 2a 2f 0a 7b  = UNION ALL */.{
31d60 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32  yygotominor.yy32
31d61 38 20 3d 20 54 4b 5f 41 4c 4c 3b 7d 0a 20 20 20  8 = TK_ALL;}.   
31d62 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
31d63 20 20 63 61 73 65 20 31 31 37 3a 20 2f 2a 20 6f    case 117: /* o
31d64 6e 65 73 65 6c 65 63 74 20 3a 3a 3d 20 53 45 4c  neselect ::= SEL
31d65 45 43 54 20 64 69 73 74 69 6e 63 74 20 73 65 6c  ECT distinct sel
31d66 63 6f 6c 6c 69 73 74 20 66 72 6f 6d 20 77 68 65  collist from whe
31d67 72 65 5f 6f 70 74 20 67 72 6f 75 70 62 79 5f 6f  re_opt groupby_o
31d68 70 74 20 68 61 76 69 6e 67 5f 6f 70 74 20 6f 72  pt having_opt or
31d69 64 65 72 62 79 5f 6f 70 74 20 6c 69 6d 69 74 5f  derby_opt limit_
31d6a 6f 70 74 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74  opt */.{.  yygot
31d6b 6f 6d 69 6e 6f 72 2e 79 79 33 20 3d 20 73 71 6c  ominor.yy3 = sql
31d6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50  ite3SelectNew(pP
31d6d 61 72 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d  arse,yymsp[-6].m
31d6e 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d 73 70 5b  inor.yy14,yymsp[
31d6f 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 79  -5].minor.yy65,y
31d70 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
31d71 79 31 33 32 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d  y132,yymsp[-3].m
31d72 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d 73 70 5b  inor.yy14,yymsp[
31d73 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c  -2].minor.yy132,
31d74 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
31d75 79 79 31 34 2c 79 79 6d 73 70 5b 2d 37 5d 2e 6d  yy14,yymsp[-7].m
31d76 69 6e 6f 72 2e 79 79 33 32 38 2c 79 79 6d 73 70  inor.yy328,yymsp
31d77 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 36 2e  [0].minor.yy476.
31d78 70 4c 69 6d 69 74 2c 79 79 6d 73 70 5b 30 5d 2e  pLimit,yymsp[0].
31d79 6d 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4f 66 66  minor.yy476.pOff
31d7a 73 65 74 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  set);.}.        
31d7b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
31d7c 65 20 31 32 31 3a 20 2f 2a 20 73 63 6c 70 20 3a  e 121: /* sclp :
31d7d 3a 3d 20 73 65 6c 63 6f 6c 6c 69 73 74 20 43 4f  := selcollist CO
31d7e 4d 4d 41 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  MMA */.      cas
31d7f 65 20 32 34 37 3a 20 2f 2a 20 69 64 78 6c 69 73  e 247: /* idxlis
31d80 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20 69 64 78  t_opt ::= LP idx
31d81 6c 69 73 74 20 52 50 20 2a 2f 20 79 79 74 65 73  list RP */ yytes
31d82 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
31d83 32 34 37 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e  247);.{yygotomin
31d84 6f 72 2e 79 79 31 34 20 3d 20 79 79 6d 73 70 5b  or.yy14 = yymsp[
31d85 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 3b 7d  -1].minor.yy14;}
31d86 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31d87 20 20 20 20 20 20 63 61 73 65 20 31 32 32 3a 20        case 122: 
31d88 2f 2a 20 73 63 6c 70 20 3a 3a 3d 20 2a 2f 0a 20  /* sclp ::= */. 
31d89 20 20 20 20 20 63 61 73 65 20 31 35 30 3a 20 2f       case 150: /
31d8a 2a 20 6f 72 64 65 72 62 79 5f 6f 70 74 20 3a 3a  * orderby_opt ::
31d8b 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
31d8c 79 79 72 75 6c 65 6e 6f 3d 3d 31 35 30 29 3b 0a  yyruleno==150);.
31d8d 20 20 20 20 20 20 63 61 73 65 20 31 35 38 3a 20        case 158: 
31d8e 2f 2a 20 67 72 6f 75 70 62 79 5f 6f 70 74 20 3a  /* groupby_opt :
31d8f 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  := */ yytestcase
31d90 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 35 38 29 3b  (yyruleno==158);
31d91 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 30 3a  .      case 240:
31d92 20 2f 2a 20 65 78 70 72 6c 69 73 74 20 3a 3a 3d   /* exprlist ::=
31d93 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
31d94 79 72 75 6c 65 6e 6f 3d 3d 32 34 30 29 3b 0a 20  yruleno==240);. 
31d95 20 20 20 20 20 63 61 73 65 20 32 34 36 3a 20 2f       case 246: /
31d96 2a 20 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a  * idxlist_opt ::
31d97 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
31d98 79 79 72 75 6c 65 6e 6f 3d 3d 32 34 36 29 3b 0a  yyruleno==246);.
31d99 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
31d9a 34 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20  4 = 0;}.        
31d9b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
31d9c 65 20 31 32 33 3a 20 2f 2a 20 73 65 6c 63 6f 6c  e 123: /* selcol
31d9d 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20 65 78  list ::= sclp ex
31d9e 70 72 20 61 73 20 2a 2f 0a 7b 0a 20 20 20 79 79  pr as */.{.   yy
31d9f 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d  gotominor.yy14 =
31da0 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
31da1 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 79  Append(pParse, y
31da2 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
31da3 79 31 34 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  y14, yymsp[-1].m
31da4 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
31da5 29 3b 0a 20 20 20 69 66 28 20 79 79 6d 73 70 5b  );.   if( yymsp[
31da6 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3e 30  0].minor.yy0.n>0
31da7 20 29 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   ) sqlite3ExprLi
31da8 73 74 53 65 74 4e 61 6d 65 28 70 50 61 72 73 65  stSetName(pParse
31da9 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
31daa 31 34 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  14, &yymsp[0].mi
31dab 6e 6f 72 2e 79 79 30 2c 20 31 29 3b 0a 20 20 20  nor.yy0, 1);.   
31dac 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53  sqlite3ExprListS
31dad 65 74 53 70 61 6e 28 70 50 61 72 73 65 2c 79 79  etSpan(pParse,yy
31dae 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 26  gotominor.yy14,&
31daf 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
31db0 79 79 33 34 36 29 3b 0a 7d 0a 20 20 20 20 20 20  yy346);.}.      
31db1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31db2 61 73 65 20 31 32 34 3a 20 2f 2a 20 73 65 6c 63  ase 124: /* selc
31db3 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20  ollist ::= sclp 
31db4 53 54 41 52 20 2a 2f 0a 7b 0a 20 20 45 78 70 72  STAR */.{.  Expr
31db5 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70   *p = sqlite3Exp
31db6 72 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 54 4b  r(pParse->db, TK
31db7 5f 41 4c 4c 2c 20 30 29 3b 0a 20 20 79 79 67 6f  _ALL, 0);.  yygo
31db8 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73  tominor.yy14 = s
31db9 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
31dba 70 65 6e 64 28 70 50 61 72 73 65 2c 20 79 79 6d  pend(pParse, yym
31dbb 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
31dbc 34 2c 20 70 29 3b 0a 7d 0a 20 20 20 20 20 20 20  4, p);.}.       
31dbd 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31dbe 73 65 20 31 32 35 3a 20 2f 2a 20 73 65 6c 63 6f  se 125: /* selco
31dbf 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20 6e  llist ::= sclp n
31dc0 6d 20 44 4f 54 20 53 54 41 52 20 2a 2f 0a 7b 0a  m DOT STAR */.{.
31dc1 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d    Expr *pRight =
31dc2 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
31dc3 61 72 73 65 2c 20 54 4b 5f 41 4c 4c 2c 20 30 2c  arse, TK_ALL, 0,
31dc4 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69   0, &yymsp[0].mi
31dc5 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78 70 72  nor.yy0);.  Expr
31dc6 20 2a 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65   *pLeft = sqlite
31dc7 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
31dc8 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d  K_ID, 0, 0, &yym
31dc9 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
31dca 29 3b 0a 20 20 45 78 70 72 20 2a 70 44 6f 74 20  );.  Expr *pDot 
31dcb 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
31dcc 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 70  Parse, TK_DOT, p
31dcd 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 30 29  Left, pRight, 0)
31dce 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
31dcf 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78  yy14 = sqlite3Ex
31dd0 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
31dd1 72 73 65 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  rse,yymsp[-3].mi
31dd2 6e 6f 72 2e 79 79 31 34 2c 20 70 44 6f 74 29 3b  nor.yy14, pDot);
31dd3 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
31dd4 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 38  ;.      case 128
31dd5 3a 20 2f 2a 20 61 73 20 3a 3a 3d 20 2a 2f 0a 7b  : /* as ::= */.{
31dd6 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
31dd7 6e 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20  n = 0;}.        
31dd8 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
31dd9 65 20 31 32 39 3a 20 2f 2a 20 66 72 6f 6d 20 3a  e 129: /* from :
31dda 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  := */.{yygotomin
31ddb 6f 72 2e 79 79 36 35 20 3d 20 73 71 6c 69 74 65  or.yy65 = sqlite
31ddc 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 50  3DbMallocZero(pP
31ddd 61 72 73 65 2d 3e 64 62 2c 20 73 69 7a 65 6f 66  arse->db, sizeof
31dde 28 2a 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  (*yygotominor.yy
31ddf 36 35 29 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  65));}.        b
31de0 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31de1 20 31 33 30 3a 20 2f 2a 20 66 72 6f 6d 20 3a 3a   130: /* from ::
31de2 3d 20 46 52 4f 4d 20 73 65 6c 74 61 62 6c 69 73  = FROM seltablis
31de3 74 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  t */.{.  yygotom
31de4 69 6e 6f 72 2e 79 79 36 35 20 3d 20 79 79 6d 73  inor.yy65 = yyms
31de5 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 3b  p[0].minor.yy65;
31de6 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
31de7 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28 79  tShiftJoinType(y
31de8 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 29  ygotominor.yy65)
31de9 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
31dea 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33  k;.      case 13
31deb 31 3a 20 2f 2a 20 73 74 6c 5f 70 72 65 66 69 78  1: /* stl_prefix
31dec 20 3a 3a 3d 20 73 65 6c 74 61 62 6c 69 73 74 20   ::= seltablist 
31ded 6a 6f 69 6e 6f 70 20 2a 2f 0a 7b 0a 20 20 20 79  joinop */.{.   y
31dee 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20  ygotominor.yy65 
31def 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  = yymsp[-1].mino
31df0 72 2e 79 79 36 35 3b 0a 20 20 20 69 66 28 20 41  r.yy65;.   if( A
31df1 4c 57 41 59 53 28 79 79 67 6f 74 6f 6d 69 6e 6f  LWAYS(yygotomino
31df2 72 2e 79 79 36 35 20 26 26 20 79 79 67 6f 74 6f  r.yy65 && yygoto
31df3 6d 69 6e 6f 72 2e 79 79 36 35 2d 3e 6e 53 72 63  minor.yy65->nSrc
31df4 3e 30 29 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  >0) ) yygotomino
31df5 72 2e 79 79 36 35 2d 3e 61 5b 79 79 67 6f 74 6f  r.yy65->a[yygoto
31df6 6d 69 6e 6f 72 2e 79 79 36 35 2d 3e 6e 53 72 63  minor.yy65->nSrc
31df7 2d 31 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 28  -1].jointype = (
31df8 75 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  u8)yymsp[0].mino
31df9 72 2e 79 79 33 32 38 3b 0a 7d 0a 20 20 20 20 20  r.yy328;.}.     
31dfa 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31dfb 63 61 73 65 20 31 33 32 3a 20 2f 2a 20 73 74 6c  case 132: /* stl
31dfc 5f 70 72 65 66 69 78 20 3a 3a 3d 20 2a 2f 0a 7b  _prefix ::= */.{
31dfd 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35  yygotominor.yy65
31dfe 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62   = 0;}.        b
31dff 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31e00 20 31 33 33 3a 20 2f 2a 20 73 65 6c 74 61 62 6c   133: /* seltabl
31e01 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66  ist ::= stl_pref
31e02 69 78 20 6e 6d 20 64 62 6e 6d 20 61 73 20 69 6e  ix nm dbnm as in
31e03 64 65 78 65 64 5f 6f 70 74 20 6f 6e 5f 6f 70 74  dexed_opt on_opt
31e04 20 75 73 69 6e 67 5f 6f 70 74 20 2a 2f 0a 7b 0a   using_opt */.{.
31e05 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
31e06 36 35 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c  65 = sqlite3SrcL
31e07 69 73 74 41 70 70 65 6e 64 46 72 6f 6d 54 65 72  istAppendFromTer
31e08 6d 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  m(pParse,yymsp[-
31e09 36 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 26 79  6].minor.yy65,&y
31e0a 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-5].minor.y
31e0b 79 30 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  y0,&yymsp[-4].mi
31e0c 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d  nor.yy0,&yymsp[-
31e0d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 2c 79  3].minor.yy0,0,y
31e0e 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
31e0f 79 31 33 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  y132,yymsp[0].mi
31e10 6e 6f 72 2e 79 79 34 30 38 29 3b 0a 20 20 73 71  nor.yy408);.  sq
31e11 6c 69 74 65 33 53 72 63 4c 69 73 74 49 6e 64 65  lite3SrcListInde
31e12 78 65 64 42 79 28 70 50 61 72 73 65 2c 20 79 79  xedBy(pParse, yy
31e13 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 2c 20  gotominor.yy65, 
31e14 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
31e15 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  .yy0);.}.       
31e16 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31e17 73 65 20 31 33 34 3a 20 2f 2a 20 73 65 6c 74 61  se 134: /* selta
31e18 62 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72  blist ::= stl_pr
31e19 65 66 69 78 20 4c 50 20 73 65 6c 65 63 74 20 52  efix LP select R
31e1a 50 20 61 73 20 6f 6e 5f 6f 70 74 20 75 73 69 6e  P as on_opt usin
31e1b 67 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 20 20 79  g_opt */.{.    y
31e1c 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20  ygotominor.yy65 
31e1d 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  = sqlite3SrcList
31e1e 41 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70  AppendFromTerm(p
31e1f 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e  Parse,yymsp[-6].
31e20 6d 69 6e 6f 72 2e 79 79 36 35 2c 30 2c 30 2c 26  minor.yy65,0,0,&
31e21 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
31e22 79 79 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  yy0,yymsp[-4].mi
31e23 6e 6f 72 2e 79 79 33 2c 79 79 6d 73 70 5b 2d 31  nor.yy3,yymsp[-1
31e24 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 79 79  ].minor.yy132,yy
31e25 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
31e26 30 38 29 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20  08);.  }.       
31e27 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31e28 73 65 20 31 33 35 3a 20 2f 2a 20 73 65 6c 74 61  se 135: /* selta
31e29 62 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72  blist ::= stl_pr
31e2a 65 66 69 78 20 4c 50 20 73 65 6c 74 61 62 6c 69  efix LP seltabli
31e2b 73 74 20 52 50 20 61 73 20 6f 6e 5f 6f 70 74 20  st RP as on_opt 
31e2c 75 73 69 6e 67 5f 6f 70 74 20 2a 2f 0a 7b 0a 20  using_opt */.{. 
31e2d 20 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 36 5d     if( yymsp[-6]
31e2e 2e 6d 69 6e 6f 72 2e 79 79 36 35 3d 3d 30 20 26  .minor.yy65==0 &
31e2f 26 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  & yymsp[-2].mino
31e30 72 2e 79 79 30 2e 6e 3d 3d 30 20 26 26 20 79 79  r.yy0.n==0 && yy
31e31 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
31e32 31 33 32 3d 3d 30 20 26 26 20 79 79 6d 73 70 5b  132==0 && yymsp[
31e33 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 3d 3d  0].minor.yy408==
31e34 30 20 29 7b 0a 20 20 20 20 20 20 79 79 67 6f 74  0 ){.      yygot
31e35 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 3d 20 79 79  ominor.yy65 = yy
31e36 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
31e37 36 35 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  65;.    }else{. 
31e38 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 75       Select *pSu
31e39 62 71 75 65 72 79 3b 0a 20 20 20 20 20 20 73 71  bquery;.      sq
31e3a 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 66  lite3SrcListShif
31e3b 74 4a 6f 69 6e 54 79 70 65 28 79 79 6d 73 70 5b  tJoinType(yymsp[
31e3c 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 29 3b  -4].minor.yy65);
31e3d 0a 20 20 20 20 20 20 70 53 75 62 71 75 65 72 79  .      pSubquery
31e3e 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
31e3f 4e 65 77 28 70 50 61 72 73 65 2c 30 2c 79 79 6d  New(pParse,0,yym
31e40 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 36  sp[-4].minor.yy6
31e41 35 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 29  5,0,0,0,0,0,0,0)
31e42 3b 0a 20 20 20 20 20 20 79 79 67 6f 74 6f 6d 69  ;.      yygotomi
31e43 6e 6f 72 2e 79 79 36 35 20 3d 20 73 71 6c 69 74  nor.yy65 = sqlit
31e44 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46  e3SrcListAppendF
31e45 72 6f 6d 54 65 72 6d 28 70 50 61 72 73 65 2c 79  romTerm(pParse,y
31e46 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-6].minor.y
31e47 79 36 35 2c 30 2c 30 2c 26 79 79 6d 73 70 5b 2d  y65,0,0,&yymsp[-
31e48 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 70 53 75  2].minor.yy0,pSu
31e49 62 71 75 65 72 79 2c 79 79 6d 73 70 5b 2d 31 5d  bquery,yymsp[-1]
31e4a 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 79 79 6d  .minor.yy132,yym
31e4b 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30  sp[0].minor.yy40
31e4c 38 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  8);.    }.  }.  
31e4d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31e4e 20 20 20 63 61 73 65 20 31 33 36 3a 20 2f 2a 20     case 136: /* 
31e4f 64 62 6e 6d 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  dbnm ::= */.    
31e50 20 20 63 61 73 65 20 31 34 35 3a 20 2f 2a 20 69    case 145: /* i
31e51 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 2a  ndexed_opt ::= *
31e52 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
31e53 75 6c 65 6e 6f 3d 3d 31 34 35 29 3b 0a 7b 79 79  uleno==145);.{yy
31e54 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3d  gotominor.yy0.z=
31e55 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  0; yygotominor.y
31e56 79 30 2e 6e 3d 30 3b 7d 0a 20 20 20 20 20 20 20  y0.n=0;}.       
31e57 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31e58 73 65 20 31 33 38 3a 20 2f 2a 20 66 75 6c 6c 6e  se 138: /* fulln
31e59 61 6d 65 20 3a 3a 3d 20 6e 6d 20 64 62 6e 6d 20  ame ::= nm dbnm 
31e5a 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
31e5b 79 79 36 35 20 3d 20 73 71 6c 69 74 65 33 53 72  yy65 = sqlite3Sr
31e5c 63 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  cListAppend(pPar
31e5d 73 65 2d 3e 64 62 2c 30 2c 26 79 79 6d 73 70 5b  se->db,0,&yymsp[
31e5e 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -1].minor.yy0,&y
31e5f 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
31e60 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
31e61 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
31e62 33 39 3a 20 2f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a  39: /* joinop ::
31e63 3d 20 43 4f 4d 4d 41 7c 4a 4f 49 4e 20 2a 2f 0a  = COMMA|JOIN */.
31e64 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
31e65 33 32 38 20 3d 20 4a 54 5f 49 4e 4e 45 52 3b 20  328 = JT_INNER; 
31e66 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31e67 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 30 3a  .      case 140:
31e68 20 2f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a   /* joinop ::= J
31e69 4f 49 4e 5f 4b 57 20 4a 4f 49 4e 20 2a 2f 0a 7b  OIN_KW JOIN */.{
31e6a 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
31e6b 32 38 20 3d 20 73 71 6c 69 74 65 33 4a 6f 69 6e  28 = sqlite3Join
31e6c 54 79 70 65 28 70 50 61 72 73 65 2c 26 79 79 6d  Type(pParse,&yym
31e6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
31e6e 2c 30 2c 30 29 3b 20 7d 0a 20 20 20 20 20 20 20  ,0,0); }.       
31e6f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31e70 73 65 20 31 34 31 3a 20 2f 2a 20 6a 6f 69 6e 6f  se 141: /* joino
31e71 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d  p ::= JOIN_KW nm
31e72 20 4a 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74   JOIN */.{ yygot
31e73 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 73  ominor.yy328 = s
31e74 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28 70  qlite3JoinType(p
31e75 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 32 5d  Parse,&yymsp[-2]
31e76 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
31e77 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-1].minor.yy0,
31e78 30 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  0); }.        br
31e79 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31e7a 31 34 32 3a 20 2f 2a 20 6a 6f 69 6e 6f 70 20 3a  142: /* joinop :
31e7b 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 6e 6d  := JOIN_KW nm nm
31e7c 20 4a 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74   JOIN */.{ yygot
31e7d 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 73  ominor.yy328 = s
31e7e 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28 70  qlite3JoinType(p
31e7f 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 33 5d  Parse,&yymsp[-3]
31e80 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
31e81 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-2].minor.yy0,
31e82 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
31e83 2e 79 79 30 29 3b 20 7d 0a 20 20 20 20 20 20 20  .yy0); }.       
31e84 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31e85 73 65 20 31 34 33 3a 20 2f 2a 20 6f 6e 5f 6f 70  se 143: /* on_op
31e86 74 20 3a 3a 3d 20 4f 4e 20 65 78 70 72 20 2a 2f  t ::= ON expr */
31e87 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 34 3a  .      case 154:
31e88 20 2f 2a 20 73 6f 72 74 69 74 65 6d 20 3a 3a 3d   /* sortitem ::=
31e89 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63   expr */ yytestc
31e8a 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 35  ase(yyruleno==15
31e8b 34 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  4);.      case 1
31e8c 36 31 3a 20 2f 2a 20 68 61 76 69 6e 67 5f 6f 70  61: /* having_op
31e8d 74 20 3a 3a 3d 20 48 41 56 49 4e 47 20 65 78 70  t ::= HAVING exp
31e8e 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  r */ yytestcase(
31e8f 79 79 72 75 6c 65 6e 6f 3d 3d 31 36 31 29 3b 0a  yyruleno==161);.
31e90 20 20 20 20 20 20 63 61 73 65 20 31 36 38 3a 20        case 168: 
31e91 2f 2a 20 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d  /* where_opt ::=
31e92 20 57 48 45 52 45 20 65 78 70 72 20 2a 2f 20 79   WHERE expr */ y
31e93 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31e94 6e 6f 3d 3d 31 36 38 29 3b 0a 20 20 20 20 20 20  no==168);.      
31e95 63 61 73 65 20 32 33 35 3a 20 2f 2a 20 63 61 73  case 235: /* cas
31e96 65 5f 65 6c 73 65 20 3a 3a 3d 20 45 4c 53 45 20  e_else ::= ELSE 
31e97 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61  expr */ yytestca
31e98 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 33 35  se(yyruleno==235
31e99 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33  );.      case 23
31e9a 37 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65 72 61  7: /* case_opera
31e9b 6e 64 20 3a 3a 3d 20 65 78 70 72 20 2a 2f 20 79  nd ::= expr */ y
31e9c 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31e9d 6e 6f 3d 3d 32 33 37 29 3b 0a 7b 79 79 67 6f 74  no==237);.{yygot
31e9e 6f 6d 69 6e 6f 72 2e 79 79 31 33 32 20 3d 20 79  ominor.yy132 = y
31e9f 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
31ea0 33 34 36 2e 70 45 78 70 72 3b 7d 0a 20 20 20 20  346.pExpr;}.    
31ea1 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
31ea2 20 63 61 73 65 20 31 34 34 3a 20 2f 2a 20 6f 6e   case 144: /* on
31ea3 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  _opt ::= */.    
31ea4 20 20 63 61 73 65 20 31 36 30 3a 20 2f 2a 20 68    case 160: /* h
31ea5 61 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 2a 2f  aving_opt ::= */
31ea6 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
31ea7 6c 65 6e 6f 3d 3d 31 36 30 29 3b 0a 20 20 20 20  leno==160);.    
31ea8 20 20 63 61 73 65 20 31 36 37 3a 20 2f 2a 20 77    case 167: /* w
31ea9 68 65 72 65 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20  here_opt ::= */ 
31eaa 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31eab 65 6e 6f 3d 3d 31 36 37 29 3b 0a 20 20 20 20 20  eno==167);.     
31eac 20 63 61 73 65 20 32 33 36 3a 20 2f 2a 20 63 61   case 236: /* ca
31ead 73 65 5f 65 6c 73 65 20 3a 3a 3d 20 2a 2f 20 79  se_else ::= */ y
31eae 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31eaf 6e 6f 3d 3d 32 33 36 29 3b 0a 20 20 20 20 20 20  no==236);.      
31eb0 63 61 73 65 20 32 33 38 3a 20 2f 2a 20 63 61 73  case 238: /* cas
31eb1 65 5f 6f 70 65 72 61 6e 64 20 3a 3a 3d 20 2a 2f  e_operand ::= */
31eb2 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
31eb3 6c 65 6e 6f 3d 3d 32 33 38 29 3b 0a 7b 79 79 67  leno==238);.{yyg
31eb4 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 33 32 20 3d  otominor.yy132 =
31eb5 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   0;}.        bre
31eb6 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
31eb7 34 37 3a 20 2f 2a 20 69 6e 64 65 78 65 64 5f 6f  47: /* indexed_o
31eb8 70 74 20 3a 3a 3d 20 4e 4f 54 20 49 4e 44 45 58  pt ::= NOT INDEX
31eb9 45 44 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  ED */.{yygotomin
31eba 6f 72 2e 79 79 30 2e 7a 3d 30 3b 20 79 79 67 6f  or.yy0.z=0; yygo
31ebb 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 31 3b  tominor.yy0.n=1;
31ebc 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31ebd 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 38 3a  .      case 148:
31ebe 20 2f 2a 20 75 73 69 6e 67 5f 6f 70 74 20 3a 3a   /* using_opt ::
31ebf 3d 20 55 53 49 4e 47 20 4c 50 20 69 6e 73 63 6f  = USING LP insco
31ec0 6c 6c 69 73 74 20 52 50 20 2a 2f 0a 20 20 20 20  llist RP */.    
31ec1 20 20 63 61 73 65 20 31 38 30 3a 20 2f 2a 20 69    case 180: /* i
31ec2 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a  nscollist_opt ::
31ec3 3d 20 4c 50 20 69 6e 73 63 6f 6c 6c 69 73 74 20  = LP inscollist 
31ec4 52 50 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  RP */ yytestcase
31ec5 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 38 30 29 3b  (yyruleno==180);
31ec6 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
31ec7 34 30 38 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e  408 = yymsp[-1].
31ec8 6d 69 6e 6f 72 2e 79 79 34 30 38 3b 7d 0a 20 20  minor.yy408;}.  
31ec9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31eca 20 20 20 63 61 73 65 20 31 34 39 3a 20 2f 2a 20     case 149: /* 
31ecb 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 2a 2f  using_opt ::= */
31ecc 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 39 3a  .      case 179:
31ecd 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f   /* inscollist_o
31ece 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  pt ::= */ yytest
31ecf 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
31ed0 37 39 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  79);.{yygotomino
31ed1 72 2e 79 79 34 30 38 20 3d 20 30 3b 7d 0a 20 20  r.yy408 = 0;}.  
31ed2 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31ed3 20 20 20 63 61 73 65 20 31 35 31 3a 20 2f 2a 20     case 151: /* 
31ed4 6f 72 64 65 72 62 79 5f 6f 70 74 20 3a 3a 3d 20  orderby_opt ::= 
31ed5 4f 52 44 45 52 20 42 59 20 73 6f 72 74 6c 69 73  ORDER BY sortlis
31ed6 74 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  t */.      case 
31ed7 31 35 39 3a 20 2f 2a 20 67 72 6f 75 70 62 79 5f  159: /* groupby_
31ed8 6f 70 74 20 3a 3a 3d 20 47 52 4f 55 50 20 42 59  opt ::= GROUP BY
31ed9 20 6e 65 78 70 72 6c 69 73 74 20 2a 2f 20 79 79   nexprlist */ yy
31eda 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
31edb 6f 3d 3d 31 35 39 29 3b 0a 20 20 20 20 20 20 63  o==159);.      c
31edc 61 73 65 20 32 33 39 3a 20 2f 2a 20 65 78 70 72  ase 239: /* expr
31edd 6c 69 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69  list ::= nexprli
31ede 73 74 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  st */ yytestcase
31edf 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 33 39 29 3b  (yyruleno==239);
31ee0 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
31ee1 31 34 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  14 = yymsp[0].mi
31ee2 6e 6f 72 2e 79 79 31 34 3b 7d 0a 20 20 20 20 20  nor.yy14;}.     
31ee3 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31ee4 63 61 73 65 20 31 35 32 3a 20 2f 2a 20 73 6f 72  case 152: /* sor
31ee5 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72 74 6c 69  tlist ::= sortli
31ee6 73 74 20 43 4f 4d 4d 41 20 73 6f 72 74 69 74 65  st COMMA sortite
31ee7 6d 20 73 6f 72 74 6f 72 64 65 72 20 2a 2f 0a 7b  m sortorder */.{
31ee8 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
31ee9 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70  y14 = sqlite3Exp
31eea 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
31eeb 73 65 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  se,yymsp[-3].min
31eec 6f 72 2e 79 79 31 34 2c 79 79 6d 73 70 5b 2d 31  or.yy14,yymsp[-1
31eed 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 29 3b 0a  ].minor.yy132);.
31eee 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
31eef 72 2e 79 79 31 34 20 29 20 79 79 67 6f 74 6f 6d  r.yy14 ) yygotom
31ef0 69 6e 6f 72 2e 79 79 31 34 2d 3e 61 5b 79 79 67  inor.yy14->a[yyg
31ef1 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2d 3e 6e  otominor.yy14->n
31ef2 45 78 70 72 2d 31 5d 2e 73 6f 72 74 4f 72 64 65  Expr-1].sortOrde
31ef3 72 20 3d 20 28 75 38 29 79 79 6d 73 70 5b 30 5d  r = (u8)yymsp[0]
31ef4 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 0a 7d 0a  .minor.yy328;.}.
31ef5 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31ef6 20 20 20 20 20 63 61 73 65 20 31 35 33 3a 20 2f       case 153: /
31ef7 2a 20 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73  * sortlist ::= s
31ef8 6f 72 74 69 74 65 6d 20 73 6f 72 74 6f 72 64 65  ortitem sortorde
31ef9 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  r */.{.  yygotom
31efa 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69  inor.yy14 = sqli
31efb 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
31efc 64 28 70 50 61 72 73 65 2c 30 2c 79 79 6d 73 70  d(pParse,0,yymsp
31efd 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32  [-1].minor.yy132
31efe 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d  );.  if( yygotom
31eff 69 6e 6f 72 2e 79 79 31 34 20 26 26 20 41 4c 57  inor.yy14 && ALW
31f00 41 59 53 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  AYS(yygotominor.
31f01 79 79 31 34 2d 3e 61 29 20 29 20 79 79 67 6f 74  yy14->a) ) yygot
31f02 6f 6d 69 6e 6f 72 2e 79 79 31 34 2d 3e 61 5b 30  ominor.yy14->a[0
31f03 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 28 75  ].sortOrder = (u
31f04 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  8)yymsp[0].minor
31f05 2e 79 79 33 32 38 3b 0a 7d 0a 20 20 20 20 20 20  .yy328;.}.      
31f06 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31f07 61 73 65 20 31 35 35 3a 20 2f 2a 20 73 6f 72 74  ase 155: /* sort
31f08 6f 72 64 65 72 20 3a 3a 3d 20 41 53 43 20 2a 2f  order ::= ASC */
31f09 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 37 3a  .      case 157:
31f0a 20 2f 2a 20 73 6f 72 74 6f 72 64 65 72 20 3a 3a   /* sortorder ::
31f0b 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
31f0c 79 79 72 75 6c 65 6e 6f 3d 3d 31 35 37 29 3b 0a  yyruleno==157);.
31f0d 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  {yygotominor.yy3
31f0e 32 38 20 3d 20 53 51 4c 49 54 45 5f 53 4f 5f 41  28 = SQLITE_SO_A
31f0f 53 43 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  SC;}.        bre
31f10 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
31f11 35 36 3a 20 2f 2a 20 73 6f 72 74 6f 72 64 65 72  56: /* sortorder
31f12 20 3a 3a 3d 20 44 45 53 43 20 2a 2f 0a 7b 79 79   ::= DESC */.{yy
31f13 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20  gotominor.yy328 
31f14 3d 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45 53 43  = SQLITE_SO_DESC
31f15 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
31f16 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 32  ;.      case 162
31f17 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74 20 3a  : /* limit_opt :
31f18 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  := */.{yygotomin
31f19 6f 72 2e 79 79 34 37 36 2e 70 4c 69 6d 69 74 20  or.yy476.pLimit 
31f1a 3d 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  = 0; yygotominor
31f1b 2e 79 79 34 37 36 2e 70 4f 66 66 73 65 74 20 3d  .yy476.pOffset =
31f1c 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   0;}.        bre
31f1d 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
31f1e 36 33 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74  63: /* limit_opt
31f1f 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 20   ::= LIMIT expr 
31f20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
31f21 79 79 34 37 36 2e 70 4c 69 6d 69 74 20 3d 20 79  yy476.pLimit = y
31f22 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
31f23 33 34 36 2e 70 45 78 70 72 3b 20 79 79 67 6f 74  346.pExpr; yygot
31f24 6f 6d 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4f 66  ominor.yy476.pOf
31f25 66 73 65 74 20 3d 20 30 3b 7d 0a 20 20 20 20 20  fset = 0;}.     
31f26 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31f27 63 61 73 65 20 31 36 34 3a 20 2f 2a 20 6c 69 6d  case 164: /* lim
31f28 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54  it_opt ::= LIMIT
31f29 20 65 78 70 72 20 4f 46 46 53 45 54 20 65 78 70   expr OFFSET exp
31f2a 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  r */.{yygotomino
31f2b 72 2e 79 79 34 37 36 2e 70 4c 69 6d 69 74 20 3d  r.yy476.pLimit =
31f2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
31f2d 2e 79 79 33 34 36 2e 70 45 78 70 72 3b 20 79 79  .yy346.pExpr; yy
31f2e 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36 2e  gotominor.yy476.
31f2f 70 4f 66 66 73 65 74 20 3d 20 79 79 6d 73 70 5b  pOffset = yymsp[
31f30 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  0].minor.yy346.p
31f31 45 78 70 72 3b 7d 0a 20 20 20 20 20 20 20 20 62  Expr;}.        b
31f32 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31f33 20 31 36 35 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f   165: /* limit_o
31f34 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70  pt ::= LIMIT exp
31f35 72 20 43 4f 4d 4d 41 20 65 78 70 72 20 2a 2f 0a  r COMMA expr */.
31f36 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34  {yygotominor.yy4
31f37 37 36 2e 70 4f 66 66 73 65 74 20 3d 20 79 79 6d  76.pOffset = yym
31f38 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-2].minor.yy3
31f39 34 36 2e 70 45 78 70 72 3b 20 79 79 67 6f 74 6f  46.pExpr; yygoto
31f3a 6d 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4c 69 6d  minor.yy476.pLim
31f3b 69 74 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  it = yymsp[0].mi
31f3c 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 3b  nor.yy346.pExpr;
31f3d 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31f3e 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 36 3a  .      case 166:
31f3f 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 45 4c 45   /* cmd ::= DELE
31f40 54 45 20 46 52 4f 4d 20 66 75 6c 6c 6e 61 6d 65  TE FROM fullname
31f41 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 77 68 65   indexed_opt whe
31f42 72 65 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 73 71  re_opt */.{.  sq
31f43 6c 69 74 65 33 53 72 63 4c 69 73 74 49 6e 64 65  lite3SrcListInde
31f44 78 65 64 42 79 28 70 50 61 72 73 65 2c 20 79 79  xedBy(pParse, yy
31f45 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
31f46 36 35 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  65, &yymsp[-1].m
31f47 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c  inor.yy0);.  sql
31f48 69 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 70  ite3DeleteFrom(p
31f49 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 32 5d 2e  Parse,yymsp[-2].
31f4a 6d 69 6e 6f 72 2e 79 79 36 35 2c 79 79 6d 73 70  minor.yy65,yymsp
31f4b 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 29  [0].minor.yy132)
31f4c 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
31f4d 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36  k;.      case 16
31f4e 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 55 50  9: /* cmd ::= UP
31f4f 44 41 54 45 20 6f 72 63 6f 6e 66 20 66 75 6c 6c  DATE orconf full
31f50 6e 61 6d 65 20 69 6e 64 65 78 65 64 5f 6f 70 74  name indexed_opt
31f51 20 53 45 54 20 73 65 74 6c 69 73 74 20 77 68 65   SET setlist whe
31f52 72 65 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 73 71  re_opt */.{.  sq
31f53 6c 69 74 65 33 53 72 63 4c 69 73 74 49 6e 64 65  lite3SrcListInde
31f54 78 65 64 42 79 28 70 50 61 72 73 65 2c 20 79 79  xedBy(pParse, yy
31f55 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
31f56 36 35 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d  65, &yymsp[-3].m
31f57 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c  inor.yy0);.  sql
31f58 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63  ite3ExprListChec
31f59 6b 4c 65 6e 67 74 68 28 70 50 61 72 73 65 2c 79  kLength(pParse,y
31f5a 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
31f5b 79 31 34 2c 22 73 65 74 20 6c 69 73 74 22 29 3b  y14,"set list");
31f5c 20 0a 20 20 73 71 6c 69 74 65 33 55 70 64 61 74   .  sqlite3Updat
31f5d 65 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  e(pParse,yymsp[-
31f5e 34 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 79 79  4].minor.yy65,yy
31f5f 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
31f60 31 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  14,yymsp[0].mino
31f61 72 2e 79 79 31 33 32 2c 79 79 6d 73 70 5b 2d 35  r.yy132,yymsp[-5
31f62 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 36 29 3b 0a  ].minor.yy186);.
31f63 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31f64 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 30 3a  .      case 170:
31f65 20 2f 2a 20 73 65 74 6c 69 73 74 20 3a 3a 3d 20   /* setlist ::= 
31f66 73 65 74 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d  setlist COMMA nm
31f67 20 45 51 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20   EQ expr */.{.  
31f68 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34  yygotominor.yy14
31f69 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
31f6a 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
31f6b 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72   yymsp[-4].minor
31f6c 2e 79 79 31 34 2c 20 79 79 6d 73 70 5b 30 5d 2e  .yy14, yymsp[0].
31f6d 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
31f6e 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  r);.  sqlite3Exp
31f6f 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 70 50 61  rListSetName(pPa
31f70 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  rse, yygotominor
31f71 2e 79 79 31 34 2c 20 26 79 79 6d 73 70 5b 2d 32  .yy14, &yymsp[-2
31f72 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 31 29 3b  ].minor.yy0, 1);
31f73 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
31f74 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 31  ;.      case 171
31f75 3a 20 2f 2a 20 73 65 74 6c 69 73 74 20 3a 3a 3d  : /* setlist ::=
31f76 20 6e 6d 20 45 51 20 65 78 70 72 20 2a 2f 0a 7b   nm EQ expr */.{
31f77 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
31f78 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70  y14 = sqlite3Exp
31f79 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
31f7a 73 65 2c 20 30 2c 20 79 79 6d 73 70 5b 30 5d 2e  se, 0, yymsp[0].
31f7b 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
31f7c 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  r);.  sqlite3Exp
31f7d 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 70 50 61  rListSetName(pPa
31f7e 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  rse, yygotominor
31f7f 2e 79 79 31 34 2c 20 26 79 79 6d 73 70 5b 2d 32  .yy14, &yymsp[-2
31f80 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 31 29 3b  ].minor.yy0, 1);
31f81 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
31f82 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 32  ;.      case 172
31f83 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 69 6e 73  : /* cmd ::= ins
31f84 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c  ert_cmd INTO ful
31f85 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74  lname inscollist
31f86 5f 6f 70 74 20 56 41 4c 55 45 53 20 4c 50 20 69  _opt VALUES LP i
31f87 74 65 6d 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 73  temlist RP */.{s
31f88 71 6c 69 74 65 33 49 6e 73 65 72 74 28 70 50 61  qlite3Insert(pPa
31f89 72 73 65 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d  rse, yymsp[-5].m
31f8a 69 6e 6f 72 2e 79 79 36 35 2c 20 79 79 6d 73 70  inor.yy65, yymsp
31f8b 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c  [-1].minor.yy14,
31f8c 20 30 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69   0, yymsp[-4].mi
31f8d 6e 6f 72 2e 79 79 34 30 38 2c 20 79 79 6d 73 70  nor.yy408, yymsp
31f8e 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 36  [-7].minor.yy186
31f8f 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
31f90 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 37  k;.      case 17
31f91 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 69 6e  3: /* cmd ::= in
31f92 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75  sert_cmd INTO fu
31f93 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73  llname inscollis
31f94 74 5f 6f 70 74 20 73 65 6c 65 63 74 20 2a 2f 0a  t_opt select */.
31f95 7b 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 70  {sqlite3Insert(p
31f96 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 32 5d  Parse, yymsp[-2]
31f97 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 30 2c 20  .minor.yy65, 0, 
31f98 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
31f99 79 33 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  y3, yymsp[-1].mi
31f9a 6e 6f 72 2e 79 79 34 30 38 2c 20 79 79 6d 73 70  nor.yy408, yymsp
31f9b 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 36  [-4].minor.yy186
31f9c 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
31f9d 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 37  k;.      case 17
31f9e 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 69 6e  4: /* cmd ::= in
31f9f 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75  sert_cmd INTO fu
31fa0 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73  llname inscollis
31fa1 74 5f 6f 70 74 20 44 45 46 41 55 4c 54 20 56 41  t_opt DEFAULT VA
31fa2 4c 55 45 53 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  LUES */.{sqlite3
31fa3 49 6e 73 65 72 74 28 70 50 61 72 73 65 2c 20 79  Insert(pParse, y
31fa4 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
31fa5 79 36 35 2c 20 30 2c 20 30 2c 20 79 79 6d 73 70  y65, 0, 0, yymsp
31fa6 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38  [-2].minor.yy408
31fa7 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f  , yymsp[-5].mino
31fa8 72 2e 79 79 31 38 36 29 3b 7d 0a 20 20 20 20 20  r.yy186);}.     
31fa9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31faa 63 61 73 65 20 31 37 35 3a 20 2f 2a 20 69 6e 73  case 175: /* ins
31fab 65 72 74 5f 63 6d 64 20 3a 3a 3d 20 49 4e 53 45  ert_cmd ::= INSE
31fac 52 54 20 6f 72 63 6f 6e 66 20 2a 2f 0a 7b 79 79  RT orconf */.{yy
31fad 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 36 20  gotominor.yy186 
31fae 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
31faf 2e 79 79 31 38 36 3b 7d 0a 20 20 20 20 20 20 20  .yy186;}.       
31fb0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31fb1 73 65 20 31 37 36 3a 20 2f 2a 20 69 6e 73 65 72  se 176: /* inser
31fb2 74 5f 63 6d 64 20 3a 3a 3d 20 52 45 50 4c 41 43  t_cmd ::= REPLAC
31fb3 45 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  E */.{yygotomino
31fb4 72 2e 79 79 31 38 36 20 3d 20 4f 45 5f 52 65 70  r.yy186 = OE_Rep
31fb5 6c 61 63 65 3b 7d 0a 20 20 20 20 20 20 20 20 62  lace;}.        b
31fb6 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31fb7 20 31 37 37 3a 20 2f 2a 20 69 74 65 6d 6c 69 73   177: /* itemlis
31fb8 74 20 3a 3a 3d 20 69 74 65 6d 6c 69 73 74 20 43  t ::= itemlist C
31fb9 4f 4d 4d 41 20 65 78 70 72 20 2a 2f 0a 20 20 20  OMMA expr */.   
31fba 20 20 20 63 61 73 65 20 32 34 31 3a 20 2f 2a 20     case 241: /* 
31fbb 6e 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 6e 65  nexprlist ::= ne
31fbc 78 70 72 6c 69 73 74 20 43 4f 4d 4d 41 20 65 78  xprlist COMMA ex
31fbd 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  pr */ yytestcase
31fbe 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 34 31 29 3b  (yyruleno==241);
31fbf 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
31fc0 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  14 = sqlite3Expr
31fc1 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
31fc2 65 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  e,yymsp[-2].mino
31fc3 72 2e 79 79 31 34 2c 79 79 6d 73 70 5b 30 5d 2e  r.yy14,yymsp[0].
31fc4 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
31fc5 72 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  r);}.        bre
31fc6 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
31fc7 37 38 3a 20 2f 2a 20 69 74 65 6d 6c 69 73 74 20  78: /* itemlist 
31fc8 3a 3a 3d 20 65 78 70 72 20 2a 2f 0a 20 20 20 20  ::= expr */.    
31fc9 20 20 63 61 73 65 20 32 34 32 3a 20 2f 2a 20 6e    case 242: /* n
31fca 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 65 78 70  exprlist ::= exp
31fcb 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  r */ yytestcase(
31fcc 79 79 72 75 6c 65 6e 6f 3d 3d 32 34 32 29 3b 0a  yyruleno==242);.
31fcd 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
31fce 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  4 = sqlite3ExprL
31fcf 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
31fd0 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  ,0,yymsp[0].mino
31fd1 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 7d  r.yy346.pExpr);}
31fd2 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31fd3 20 20 20 20 20 20 63 61 73 65 20 31 38 31 3a 20        case 181: 
31fd4 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a  /* inscollist ::
31fd5 3d 20 69 6e 73 63 6f 6c 6c 69 73 74 20 43 4f 4d  = inscollist COM
31fd6 4d 41 20 6e 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f  MA nm */.{yygoto
31fd7 6d 69 6e 6f 72 2e 79 79 34 30 38 20 3d 20 73 71  minor.yy408 = sq
31fd8 6c 69 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e  lite3IdListAppen
31fd9 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 79 79 6d  d(pParse->db,yym
31fda 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-2].minor.yy4
31fdb 30 38 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  08,&yymsp[0].min
31fdc 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
31fdd 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31fde 61 73 65 20 31 38 32 3a 20 2f 2a 20 69 6e 73 63  ase 182: /* insc
31fdf 6f 6c 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 2a 2f  ollist ::= nm */
31fe0 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
31fe1 34 30 38 20 3d 20 73 71 6c 69 74 65 33 49 64 4c  408 = sqlite3IdL
31fe2 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
31fe3 2d 3e 64 62 2c 30 2c 26 79 79 6d 73 70 5b 30 5d  ->db,0,&yymsp[0]
31fe4 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20  .minor.yy0);}.  
31fe5 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31fe6 20 20 20 63 61 73 65 20 31 38 33 3a 20 2f 2a 20     case 183: /* 
31fe7 65 78 70 72 20 3a 3a 3d 20 74 65 72 6d 20 2a 2f  expr ::= term */
31fe8 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 31 3a  .      case 211:
31fe9 20 2f 2a 20 65 73 63 61 70 65 20 3a 3a 3d 20 45   /* escape ::= E
31fea 53 43 41 50 45 20 65 78 70 72 20 2a 2f 20 79 79  SCAPE expr */ yy
31feb 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
31fec 6f 3d 3d 32 31 31 29 3b 0a 7b 79 79 67 6f 74 6f  o==211);.{yygoto
31fed 6d 69 6e 6f 72 2e 79 79 33 34 36 20 3d 20 79 79  minor.yy346 = yy
31fee 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
31fef 34 36 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  46;}.        bre
31ff0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
31ff1 38 34 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  84: /* expr ::= 
31ff2 4c 50 20 65 78 70 72 20 52 50 20 2a 2f 0a 7b 79  LP expr RP */.{y
31ff3 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
31ff4 2e 70 45 78 70 72 20 3d 20 79 79 6d 73 70 5b 2d  .pExpr = yymsp[-
31ff5 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  1].minor.yy346.p
31ff6 45 78 70 72 3b 20 73 70 61 6e 53 65 74 28 26 79  Expr; spanSet(&y
31ff7 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
31ff8 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ,&yymsp[-2].mino
31ff9 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
31ffa 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20  minor.yy0);}.   
31ffb 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
31ffc 20 20 63 61 73 65 20 31 38 35 3a 20 2f 2a 20 74    case 185: /* t
31ffd 65 72 6d 20 3a 3a 3d 20 4e 55 4c 4c 20 2a 2f 0a  erm ::= NULL */.
31ffe 20 20 20 20 20 20 63 61 73 65 20 31 39 30 3a 20        case 190: 
31fff 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 49 4e 54 45  /* term ::= INTE
32000 47 45 52 7c 46 4c 4f 41 54 7c 42 4c 4f 42 20 2a  GER|FLOAT|BLOB *
32001 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
32002 75 6c 65 6e 6f 3d 3d 31 39 30 29 3b 0a 20 20 20  uleno==190);.   
32003 20 20 20 63 61 73 65 20 31 39 31 3a 20 2f 2a 20     case 191: /* 
32004 74 65 72 6d 20 3a 3a 3d 20 53 54 52 49 4e 47 20  term ::= STRING 
32005 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
32006 72 75 6c 65 6e 6f 3d 3d 31 39 31 29 3b 0a 7b 73  ruleno==191);.{s
32007 70 61 6e 45 78 70 72 28 26 79 79 67 6f 74 6f 6d  panExpr(&yygotom
32008 69 6e 6f 72 2e 79 79 33 34 36 2c 20 70 50 61 72  inor.yy346, pPar
32009 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 61 6a  se, yymsp[0].maj
3200a 6f 72 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  or, &yymsp[0].mi
3200b 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20  nor.yy0);}.     
3200c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3200d 63 61 73 65 20 31 38 36 3a 20 2f 2a 20 65 78 70  case 186: /* exp
3200e 72 20 3a 3a 3d 20 69 64 20 2a 2f 0a 20 20 20 20  r ::= id */.    
3200f 20 20 63 61 73 65 20 31 38 37 3a 20 2f 2a 20 65    case 187: /* e
32010 78 70 72 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20  xpr ::= JOIN_KW 
32011 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
32012 72 75 6c 65 6e 6f 3d 3d 31 38 37 29 3b 0a 7b 73  ruleno==187);.{s
32013 70 61 6e 45 78 70 72 28 26 79 79 67 6f 74 6f 6d  panExpr(&yygotom
32014 69 6e 6f 72 2e 79 79 33 34 36 2c 20 70 50 61 72  inor.yy346, pPar
32015 73 65 2c 20 54 4b 5f 49 44 2c 20 26 79 79 6d 73  se, TK_ID, &yyms
32016 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
32017 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32018 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 38 3a  .      case 188:
32019 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 6e 6d 20   /* expr ::= nm 
3201a 44 4f 54 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 45 78  DOT nm */.{.  Ex
3201b 70 72 20 2a 74 65 6d 70 31 20 3d 20 73 71 6c 69  pr *temp1 = sqli
3201c 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
3201d 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79   TK_ID, 0, 0, &y
3201e 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
3201f 79 30 29 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d  y0);.  Expr *tem
32020 70 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  p2 = sqlite3PExp
32021 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c  r(pParse, TK_ID,
32022 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d   0, 0, &yymsp[0]
32023 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 79  .minor.yy0);.  y
32024 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
32025 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  .pExpr = sqlite3
32026 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
32027 5f 44 4f 54 2c 20 74 65 6d 70 31 2c 20 74 65 6d  _DOT, temp1, tem
32028 70 32 2c 20 30 29 3b 0a 20 20 73 70 61 6e 53 65  p2, 0);.  spanSe
32029 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  t(&yygotominor.y
3202a 79 33 34 36 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e  y346,&yymsp[-2].
3202b 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
3202c 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
3202d 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3202e 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 39 3a  .      case 189:
3202f 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 6e 6d 20   /* expr ::= nm 
32030 44 4f 54 20 6e 6d 20 44 4f 54 20 6e 6d 20 2a 2f  DOT nm DOT nm */
32031 0a 7b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70 31  .{.  Expr *temp1
32032 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
32033 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30  pParse, TK_ID, 0
32034 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 34 5d 2e  , 0, &yymsp[-4].
32035 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78  minor.yy0);.  Ex
32036 70 72 20 2a 74 65 6d 70 32 20 3d 20 73 71 6c 69  pr *temp2 = sqli
32037 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
32038 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79   TK_ID, 0, 0, &y
32039 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
3203a 79 30 29 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d  y0);.  Expr *tem
3203b 70 33 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  p3 = sqlite3PExp
3203c 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c  r(pParse, TK_ID,
3203d 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d   0, 0, &yymsp[0]
3203e 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45  .minor.yy0);.  E
3203f 78 70 72 20 2a 74 65 6d 70 34 20 3d 20 73 71 6c  xpr *temp4 = sql
32040 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
32041 2c 20 54 4b 5f 44 4f 54 2c 20 74 65 6d 70 32 2c  , TK_DOT, temp2,
32042 20 74 65 6d 70 33 2c 20 30 29 3b 0a 20 20 79 79   temp3, 0);.  yy
32043 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
32044 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50  pExpr = sqlite3P
32045 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
32046 44 4f 54 2c 20 74 65 6d 70 31 2c 20 74 65 6d 70  DOT, temp1, temp
32047 34 2c 20 30 29 3b 0a 20 20 73 70 61 6e 53 65 74  4, 0);.  spanSet
32048 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  (&yygotominor.yy
32049 33 34 36 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d  346,&yymsp[-4].m
3204a 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
3204b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d  0].minor.yy0);.}
3204c 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3204d 20 20 20 20 20 20 63 61 73 65 20 31 39 32 3a 20        case 192: 
3204e 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 52 45 47 49  /* expr ::= REGI
3204f 53 54 45 52 20 2a 2f 0a 7b 0a 20 20 2f 2a 20 57  STER */.{.  /* W
32050 68 65 6e 20 64 6f 69 6e 67 20 61 20 6e 65 73 74  hen doing a nest
32051 65 64 20 70 61 72 73 65 2c 20 6f 6e 65 20 63 61  ed parse, one ca
32052 6e 20 69 6e 63 6c 75 64 65 20 74 65 72 6d 73 20  n include terms 
32053 69 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  in an expression
32054 0a 20 20 2a 2a 20 74 68 61 74 20 6c 6f 6f 6b 20  .  ** that look 
32055 6c 69 6b 65 20 74 68 69 73 3a 20 20 20 23 31 20  like this:   #1 
32056 23 32 20 2e 2e 2e 20 20 54 68 65 73 65 20 74 65  #2 ...  These te
32057 72 6d 73 20 72 65 66 65 72 20 74 6f 20 72 65 67  rms refer to reg
32058 69 73 74 65 72 73 0a 20 20 2a 2a 20 69 6e 20 74  isters.  ** in t
32059 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  he virtual machi
3205a 6e 65 2e 20 20 23 4e 20 69 73 20 74 68 65 20 4e  ne.  #N is the N
3205b 2d 74 68 20 72 65 67 69 73 74 65 72 2e 20 2a 2f  -th register. */
3205c 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
3205d 65 73 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20  ested==0 ){.    
3205e 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
3205f 70 50 61 72 73 65 2c 20 22 6e 65 61 72 20 5c 22  pParse, "near \"
32060 25 54 5c 22 3a 20 73 79 6e 74 61 78 20 65 72 72  %T\": syntax err
32061 6f 72 22 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  or", &yymsp[0].m
32062 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 20 20 79  inor.yy0);.    y
32063 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
32064 2e 70 45 78 70 72 20 3d 20 30 3b 0a 20 20 7d 65  .pExpr = 0;.  }e
32065 6c 73 65 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  lse{.    yygotom
32066 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
32067 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
32068 70 50 61 72 73 65 2c 20 54 4b 5f 52 45 47 49 53  pParse, TK_REGIS
32069 54 45 52 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73  TER, 0, 0, &yyms
3206a 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
3206b 0a 20 20 20 20 69 66 28 20 79 79 67 6f 74 6f 6d  .    if( yygotom
3206c 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
3206d 20 29 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74   ) sqlite3GetInt
3206e 33 32 28 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  32(&yymsp[0].min
3206f 6f 72 2e 79 79 30 2e 7a 5b 31 5d 2c 20 26 79 79  or.yy0.z[1], &yy
32070 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
32071 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 29 3b 0a  pExpr->iTable);.
32072 20 20 7d 0a 20 20 73 70 61 6e 53 65 74 28 26 79    }.  spanSet(&y
32073 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
32074 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
32075 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d  r.yy0, &yymsp[0]
32076 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20  .minor.yy0);.}. 
32077 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32078 20 20 20 20 63 61 73 65 20 31 39 33 3a 20 2f 2a      case 193: /*
32079 20 65 78 70 72 20 3a 3a 3d 20 56 41 52 49 41 42   expr ::= VARIAB
3207a 4c 45 20 2a 2f 0a 7b 0a 20 20 73 70 61 6e 45 78  LE */.{.  spanEx
3207b 70 72 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  pr(&yygotominor.
3207c 79 79 33 34 36 2c 20 70 50 61 72 73 65 2c 20 54  yy346, pParse, T
3207d 4b 5f 56 41 52 49 41 42 4c 45 2c 20 26 79 79 6d  K_VARIABLE, &yym
3207e 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
3207f 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 41  ;.  sqlite3ExprA
32080 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 70  ssignVarNumber(p
32081 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e  Parse, yygotomin
32082 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b  or.yy346.pExpr);
32083 0a 20 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f  .  spanSet(&yygo
32084 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 20 26  tominor.yy346, &
32085 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32086 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  y0, &yymsp[0].mi
32087 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20  nor.yy0);.}.    
32088 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32089 20 63 61 73 65 20 31 39 34 3a 20 2f 2a 20 65 78   case 194: /* ex
3208a 70 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4c 4c  pr ::= expr COLL
3208b 41 54 45 20 69 64 73 20 2a 2f 0a 7b 0a 20 20 79  ATE ids */.{.  y
3208c 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
3208d 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  .pExpr = sqlite3
3208e 45 78 70 72 53 65 74 43 6f 6c 6c 28 70 50 61 72  ExprSetColl(pPar
3208f 73 65 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  se, yymsp[-2].mi
32090 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c  nor.yy346.pExpr,
32091 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
32092 2e 79 79 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d  .yy0);.  yygotom
32093 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72  inor.yy346.zStar
32094 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  t = yymsp[-2].mi
32095 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74  nor.yy346.zStart
32096 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
32097 79 79 33 34 36 2e 7a 45 6e 64 20 3d 20 26 79 79  yy346.zEnd = &yy
32098 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
32099 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  .z[yymsp[0].mino
3209a 72 2e 79 79 30 2e 6e 5d 3b 0a 7d 0a 20 20 20 20  r.yy0.n];.}.    
3209b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3209c 20 63 61 73 65 20 31 39 35 3a 20 2f 2a 20 65 78   case 195: /* ex
3209d 70 72 20 3a 3a 3d 20 43 41 53 54 20 4c 50 20 65  pr ::= CAST LP e
3209e 78 70 72 20 41 53 20 74 79 70 65 74 6f 6b 65 6e  xpr AS typetoken
3209f 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74   RP */.{.  yygot
320a0 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
320a1 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  pr = sqlite3PExp
320a2 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 43 41 53  r(pParse, TK_CAS
320a3 54 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  T, yymsp[-3].min
320a4 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20  or.yy346.pExpr, 
320a5 30 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  0, &yymsp[-1].mi
320a6 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 70 61 6e  nor.yy0);.  span
320a7 53 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72  Set(&yygotominor
320a8 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 2d 35  .yy346,&yymsp[-5
320a9 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
320aa 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
320ab 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
320ac 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 39  k;.      case 19
320ad 36 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 49  6: /* expr ::= I
320ae 44 20 4c 50 20 64 69 73 74 69 6e 63 74 20 65 78  D LP distinct ex
320af 70 72 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 0a 20  prlist RP */.{. 
320b0 20 69 66 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d   if( yymsp[-1].m
320b1 69 6e 6f 72 2e 79 79 31 34 20 26 26 20 79 79 6d  inor.yy14 && yym
320b2 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
320b3 34 2d 3e 6e 45 78 70 72 3e 70 50 61 72 73 65 2d  4->nExpr>pParse-
320b4 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
320b5 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f  TE_LIMIT_FUNCTIO
320b6 4e 5f 41 52 47 5d 20 29 7b 0a 20 20 20 20 73 71  N_ARG] ){.    sq
320b7 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
320b8 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20  arse, "too many 
320b9 61 72 67 75 6d 65 6e 74 73 20 6f 6e 20 66 75 6e  arguments on fun
320ba 63 74 69 6f 6e 20 25 54 22 2c 20 26 79 79 6d 73  ction %T", &yyms
320bb 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-4].minor.yy0)
320bc 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69  ;.  }.  yygotomi
320bd 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
320be 3d 20 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e  = sqlite3ExprFun
320bf 63 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 79 79  ction(pParse, yy
320c0 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
320c1 31 34 2c 20 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d  14, &yymsp[-4].m
320c2 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 70 61  inor.yy0);.  spa
320c3 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f  nSet(&yygotomino
320c4 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 2d  r.yy346,&yymsp[-
320c5 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  4].minor.yy0,&yy
320c6 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
320c7 29 3b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d  );.  if( yymsp[-
320c8 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 20 26  2].minor.yy328 &
320c9 26 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  & yygotominor.yy
320ca 33 34 36 2e 70 45 78 70 72 20 29 7b 0a 20 20 20  346.pExpr ){.   
320cb 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
320cc 34 36 2e 70 45 78 70 72 2d 3e 66 6c 61 67 73 20  46.pExpr->flags 
320cd 7c 3d 20 45 50 5f 44 69 73 74 69 6e 63 74 3b 0a  |= EP_Distinct;.
320ce 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20 20 62 72    }.}.        br
320cf 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
320d0 31 39 37 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  197: /* expr ::=
320d1 20 49 44 20 4c 50 20 53 54 41 52 20 52 50 20 2a   ID LP STAR RP *
320d2 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
320d3 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20  r.yy346.pExpr = 
320d4 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74  sqlite3ExprFunct
320d5 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 26  ion(pParse, 0, &
320d6 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
320d7 79 79 30 29 3b 0a 20 20 73 70 61 6e 53 65 74 28  yy0);.  spanSet(
320d8 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  &yygotominor.yy3
320d9 34 36 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  46,&yymsp[-3].mi
320da 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30  nor.yy0,&yymsp[0
320db 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a  ].minor.yy0);.}.
320dc 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
320dd 20 20 20 20 20 63 61 73 65 20 31 39 38 3a 20 2f       case 198: /
320de 2a 20 74 65 72 6d 20 3a 3a 3d 20 43 54 49 4d 45  * term ::= CTIME
320df 5f 4b 57 20 2a 2f 0a 7b 0a 20 20 2f 2a 20 54 68  _KW */.{.  /* Th
320e0 65 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 2c 20  e CURRENT_TIME, 
320e1 43 55 52 52 45 4e 54 5f 44 41 54 45 2c 20 61 6e  CURRENT_DATE, an
320e2 64 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 53 54  d CURRENT_TIMEST
320e3 41 4d 50 20 76 61 6c 75 65 73 20 61 72 65 0a 20  AMP values are. 
320e4 20 2a 2a 20 74 72 65 61 74 65 64 20 61 73 20 66   ** treated as f
320e5 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 72 65  unctions that re
320e6 74 75 72 6e 20 63 6f 6e 73 74 61 6e 74 73 20 2a  turn constants *
320e7 2f 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.  yygotominor.
320e8 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71  yy346.pExpr = sq
320e9 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f  lite3ExprFunctio
320ea 6e 28 70 50 61 72 73 65 2c 20 30 2c 26 79 79 6d  n(pParse, 0,&yym
320eb 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
320ec 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69  ;.  if( yygotomi
320ed 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
320ee 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  ){.    yygotomin
320ef 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e  or.yy346.pExpr->
320f0 6f 70 20 3d 20 54 4b 5f 43 4f 4e 53 54 5f 46 55  op = TK_CONST_FU
320f1 4e 43 3b 20 20 0a 20 20 7d 0a 20 20 73 70 61 6e  NC;  .  }.  span
320f2 53 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72  Set(&yygotominor
320f3 2e 79 79 33 34 36 2c 20 26 79 79 6d 73 70 5b 30  .yy346, &yymsp[0
320f4 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
320f5 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
320f6 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
320f7 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
320f8 39 39 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  99: /* expr ::= 
320f9 65 78 70 72 20 41 4e 44 20 65 78 70 72 20 2a 2f  expr AND expr */
320fa 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 30 3a  .      case 200:
320fb 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70   /* expr ::= exp
320fc 72 20 4f 52 20 65 78 70 72 20 2a 2f 20 79 79 74  r OR expr */ yyt
320fd 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
320fe 3d 3d 32 30 30 29 3b 0a 20 20 20 20 20 20 63 61  ==200);.      ca
320ff 73 65 20 32 30 31 3a 20 2f 2a 20 65 78 70 72 20  se 201: /* expr 
32100 3a 3a 3d 20 65 78 70 72 20 4c 54 7c 47 54 7c 47  ::= expr LT|GT|G
32101 45 7c 4c 45 20 65 78 70 72 20 2a 2f 20 79 79 74  E|LE expr */ yyt
32102 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
32103 3d 3d 32 30 31 29 3b 0a 20 20 20 20 20 20 63 61  ==201);.      ca
32104 73 65 20 32 30 32 3a 20 2f 2a 20 65 78 70 72 20  se 202: /* expr 
32105 3a 3a 3d 20 65 78 70 72 20 45 51 7c 4e 45 20 65  ::= expr EQ|NE e
32106 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73  xpr */ yytestcas
32107 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 32 29  e(yyruleno==202)
32108 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 33  ;.      case 203
32109 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78  : /* expr ::= ex
3210a 70 72 20 42 49 54 41 4e 44 7c 42 49 54 4f 52 7c  pr BITAND|BITOR|
3210b 4c 53 48 49 46 54 7c 52 53 48 49 46 54 20 65 78  LSHIFT|RSHIFT ex
3210c 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  pr */ yytestcase
3210d 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 33 29 3b  (yyruleno==203);
3210e 0a 20 20 20 20 20 20 63 61 73 65 20 32 30 34 3a  .      case 204:
3210f 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70   /* expr ::= exp
32110 72 20 50 4c 55 53 7c 4d 49 4e 55 53 20 65 78 70  r PLUS|MINUS exp
32111 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  r */ yytestcase(
32112 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 34 29 3b 0a  yyruleno==204);.
32113 20 20 20 20 20 20 63 61 73 65 20 32 30 35 3a 20        case 205: 
32114 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
32115 20 53 54 41 52 7c 53 4c 41 53 48 7c 52 45 4d 20   STAR|SLASH|REM 
32116 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61  expr */ yytestca
32117 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 35  se(yyruleno==205
32118 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 30  );.      case 20
32119 36 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65  6: /* expr ::= e
3211a 78 70 72 20 43 4f 4e 43 41 54 20 65 78 70 72 20  xpr CONCAT expr 
3211b 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3211c 72 75 6c 65 6e 6f 3d 3d 32 30 36 29 3b 0a 7b 73  ruleno==206);.{s
3211d 70 61 6e 42 69 6e 61 72 79 45 78 70 72 28 26 79  panBinaryExpr(&y
3211e 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
3211f 2c 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 31  ,pParse,yymsp[-1
32120 5d 2e 6d 61 6a 6f 72 2c 26 79 79 6d 73 70 5b 2d  ].major,&yymsp[-
32121 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26  2].minor.yy346,&
32122 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32123 79 33 34 36 29 3b 7d 0a 20 20 20 20 20 20 20 20  y346);}.        
32124 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32125 65 20 32 30 37 3a 20 2f 2a 20 6c 69 6b 65 6f 70  e 207: /* likeop
32126 20 3a 3a 3d 20 4c 49 4b 45 5f 4b 57 20 2a 2f 0a   ::= LIKE_KW */.
32127 20 20 20 20 20 20 63 61 73 65 20 32 30 39 3a 20        case 209: 
32128 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4d 41  /* likeop ::= MA
32129 54 43 48 20 2a 2f 20 79 79 74 65 73 74 63 61 73  TCH */ yytestcas
3212a 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 39 29  e(yyruleno==209)
3212b 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ;.{yygotominor.y
3212c 79 39 36 2e 65 4f 70 65 72 61 74 6f 72 20 3d 20  y96.eOperator = 
3212d 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3212e 79 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  y0; yygotominor.
3212f 79 79 39 36 2e 6e 6f 74 20 3d 20 30 3b 7d 0a 20  yy96.not = 0;}. 
32130 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32131 20 20 20 20 63 61 73 65 20 32 30 38 3a 20 2f 2a      case 208: /*
32132 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20   likeop ::= NOT 
32133 4c 49 4b 45 5f 4b 57 20 2a 2f 0a 20 20 20 20 20  LIKE_KW */.     
32134 20 63 61 73 65 20 32 31 30 3a 20 2f 2a 20 6c 69   case 210: /* li
32135 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4d 41 54  keop ::= NOT MAT
32136 43 48 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  CH */ yytestcase
32137 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 31 30 29 3b  (yyruleno==210);
32138 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
32139 39 36 2e 65 4f 70 65 72 61 74 6f 72 20 3d 20 79  96.eOperator = y
3213a 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3213b 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  0; yygotominor.y
3213c 79 39 36 2e 6e 6f 74 20 3d 20 31 3b 7d 0a 20 20  y96.not = 1;}.  
3213d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3213e 20 20 20 63 61 73 65 20 32 31 32 3a 20 2f 2a 20     case 212: /* 
3213f 65 73 63 61 70 65 20 3a 3a 3d 20 2a 2f 0a 7b 6d  escape ::= */.{m
32140 65 6d 73 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e  emset(&yygotomin
32141 6f 72 2e 79 79 33 34 36 2c 30 2c 73 69 7a 65 6f  or.yy346,0,sizeo
32142 66 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  f(yygotominor.yy
32143 33 34 36 29 29 3b 7d 0a 20 20 20 20 20 20 20 20  346));}.        
32144 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32145 65 20 32 31 33 3a 20 2f 2a 20 65 78 70 72 20 3a  e 213: /* expr :
32146 3a 3d 20 65 78 70 72 20 6c 69 6b 65 6f 70 20 65  := expr likeop e
32147 78 70 72 20 65 73 63 61 70 65 20 2a 2f 0a 7b 0a  xpr escape */.{.
32148 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
32149 74 3b 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c  t;.  pList = sql
3214a 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
3214b 6e 64 28 70 50 61 72 73 65 2c 30 2c 20 79 79 6d  nd(pParse,0, yym
3214c 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
3214d 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 70 4c 69  46.pExpr);.  pLi
3214e 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
3214f 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
32150 65 2c 70 4c 69 73 74 2c 20 79 79 6d 73 70 5b 2d  e,pList, yymsp[-
32151 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  3].minor.yy346.p
32152 45 78 70 72 29 3b 0a 20 20 69 66 28 20 79 79 6d  Expr);.  if( yym
32153 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  sp[0].minor.yy34
32154 36 2e 70 45 78 70 72 20 29 7b 0a 20 20 20 20 70  6.pExpr ){.    p
32155 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
32156 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
32157 72 73 65 2c 70 4c 69 73 74 2c 20 79 79 6d 73 70  rse,pList, yymsp
32158 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  [0].minor.yy346.
32159 70 45 78 70 72 29 3b 0a 20 20 7d 0a 20 20 79 79  pExpr);.  }.  yy
3215a 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
3215b 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45  pExpr = sqlite3E
3215c 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50 61 72  xprFunction(pPar
3215d 73 65 2c 20 70 4c 69 73 74 2c 20 26 79 79 6d 73  se, pList, &yyms
3215e 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 39 36  p[-2].minor.yy96
3215f 2e 65 4f 70 65 72 61 74 6f 72 29 3b 0a 20 20 69  .eOperator);.  i
32160 66 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  f( yymsp[-2].min
32161 6f 72 2e 79 79 39 36 2e 6e 6f 74 20 29 20 79 79  or.yy96.not ) yy
32162 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
32163 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50  pExpr = sqlite3P
32164 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
32165 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  NOT, yygotominor
32166 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20 30 2c  .yy346.pExpr, 0,
32167 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e   0);.  yygotomin
32168 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74 20  or.yy346.zStart 
32169 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  = yymsp[-3].mino
3216a 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74 3b 0a  r.yy346.zStart;.
3216b 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3216c 33 34 36 2e 7a 45 6e 64 20 3d 20 79 79 6d 73 70  346.zEnd = yymsp
3216d 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  [-1].minor.yy346
3216e 2e 7a 45 6e 64 3b 0a 20 20 69 66 28 20 79 79 67  .zEnd;.  if( yyg
3216f 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
32170 45 78 70 72 20 29 20 79 79 67 6f 74 6f 6d 69 6e  Expr ) yygotomin
32171 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e  or.yy346.pExpr->
32172 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e 66 69  flags |= EP_Infi
32173 78 46 75 6e 63 3b 0a 7d 0a 20 20 20 20 20 20 20  xFunc;.}.       
32174 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32175 73 65 20 32 31 34 3a 20 2f 2a 20 65 78 70 72 20  se 214: /* expr 
32176 3a 3a 3d 20 65 78 70 72 20 49 53 4e 55 4c 4c 7c  ::= expr ISNULL|
32177 4e 4f 54 4e 55 4c 4c 20 2a 2f 0a 7b 73 70 61 6e  NOTNULL */.{span
32178 55 6e 61 72 79 50 6f 73 74 66 69 78 28 26 79 79  UnaryPostfix(&yy
32179 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c  gotominor.yy346,
3217a 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d 2e  pParse,yymsp[0].
3217b 6d 61 6a 6f 72 2c 26 79 79 6d 73 70 5b 2d 31 5d  major,&yymsp[-1]
3217c 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79  .minor.yy346,&yy
3217d 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
3217e 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
3217f 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 31  k;.      case 21
32180 35 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65  5: /* expr ::= e
32181 78 70 72 20 4e 4f 54 20 4e 55 4c 4c 20 2a 2f 0a  xpr NOT NULL */.
32182 7b 73 70 61 6e 55 6e 61 72 79 50 6f 73 74 66 69  {spanUnaryPostfi
32183 78 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  x(&yygotominor.y
32184 79 33 34 36 2c 70 50 61 72 73 65 2c 54 4b 5f 4e  y346,pParse,TK_N
32185 4f 54 4e 55 4c 4c 2c 26 79 79 6d 73 70 5b 2d 32  OTNULL,&yymsp[-2
32186 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79  ].minor.yy346,&y
32187 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32188 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
32189 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
3218a 31 36 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  16: /* expr ::= 
3218b 65 78 70 72 20 49 53 20 65 78 70 72 20 2a 2f 0a  expr IS expr */.
3218c 7b 0a 20 20 73 70 61 6e 42 69 6e 61 72 79 45 78  {.  spanBinaryEx
3218d 70 72 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  pr(&yygotominor.
3218e 79 79 33 34 36 2c 70 50 61 72 73 65 2c 54 4b 5f  yy346,pParse,TK_
3218f 49 53 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  IS,&yymsp[-2].mi
32190 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70  nor.yy346,&yymsp
32191 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 29  [0].minor.yy346)
32192 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
32193 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
32194 3d 3d 30 20 20 26 26 20 79 79 6d 73 70 5b 30 5d  ==0  && yymsp[0]
32195 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  .minor.yy346.pEx
32196 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c 20  pr->op==TK_NULL 
32197 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  ){.    yygotomin
32198 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e  or.yy346.pExpr->
32199 6f 70 20 3d 20 54 4b 5f 49 53 4e 55 4c 4c 3b 0a  op = TK_ISNULL;.
3219a 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20 20 62 72    }.}.        br
3219b 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3219c 32 31 37 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  217: /* expr ::=
3219d 20 65 78 70 72 20 49 53 20 4e 4f 54 20 65 78 70   expr IS NOT exp
3219e 72 20 2a 2f 0a 7b 0a 20 20 73 70 61 6e 42 69 6e  r */.{.  spanBin
3219f 61 72 79 45 78 70 72 28 26 79 79 67 6f 74 6f 6d  aryExpr(&yygotom
321a0 69 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61 72 73  inor.yy346,pPars
321a1 65 2c 54 4b 5f 49 53 4e 4f 54 2c 26 79 79 6d 73  e,TK_ISNOT,&yyms
321a2 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-3].minor.yy34
321a3 36 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  6,&yymsp[0].mino
321a4 72 2e 79 79 33 34 36 29 3b 0a 20 20 69 66 28 20  r.yy346);.  if( 
321a5 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c  pParse->db->mall
321a6 6f 63 46 61 69 6c 65 64 3d 3d 30 20 20 26 26 20  ocFailed==0  && 
321a7 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
321a8 79 33 34 36 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d  y346.pExpr->op==
321a9 54 4b 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 79  TK_NULL ){.    y
321aa 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
321ab 2e 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f  .pExpr->op = TK_
321ac 4e 4f 54 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a 20  NOTNULL;.  }.}. 
321ad 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
321ae 20 20 20 20 63 61 73 65 20 32 31 38 3a 20 2f 2a      case 218: /*
321af 20 65 78 70 72 20 3a 3a 3d 20 4e 4f 54 20 65 78   expr ::= NOT ex
321b0 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  pr */.      case
321b1 20 32 31 39 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   219: /* expr ::
321b2 3d 20 42 49 54 4e 4f 54 20 65 78 70 72 20 2a 2f  = BITNOT expr */
321b3 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
321b4 6c 65 6e 6f 3d 3d 32 31 39 29 3b 0a 7b 73 70 61  leno==219);.{spa
321b5 6e 55 6e 61 72 79 50 72 65 66 69 78 28 26 79 79  nUnaryPrefix(&yy
321b6 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c  gotominor.yy346,
321b7 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 31 5d  pParse,yymsp[-1]
321b8 2e 6d 61 6a 6f 72 2c 26 79 79 6d 73 70 5b 30 5d  .major,&yymsp[0]
321b9 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79  .minor.yy346,&yy
321ba 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
321bb 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
321bc 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
321bd 32 30 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  20: /* expr ::= 
321be 4d 49 4e 55 53 20 65 78 70 72 20 2a 2f 0a 7b 73  MINUS expr */.{s
321bf 70 61 6e 55 6e 61 72 79 50 72 65 66 69 78 28 26  panUnaryPrefix(&
321c0 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
321c1 36 2c 70 50 61 72 73 65 2c 54 4b 5f 55 4d 49 4e  6,pParse,TK_UMIN
321c2 55 53 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  US,&yymsp[0].min
321c3 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b  or.yy346,&yymsp[
321c4 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d  -1].minor.yy0);}
321c5 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
321c6 20 20 20 20 20 20 63 61 73 65 20 32 32 31 3a 20        case 221: 
321c7 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 50 4c 55 53  /* expr ::= PLUS
321c8 20 65 78 70 72 20 2a 2f 0a 7b 73 70 61 6e 55 6e   expr */.{spanUn
321c9 61 72 79 50 72 65 66 69 78 28 26 79 79 67 6f 74  aryPrefix(&yygot
321ca 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61  ominor.yy346,pPa
321cb 72 73 65 2c 54 4b 5f 55 50 4c 55 53 2c 26 79 79  rse,TK_UPLUS,&yy
321cc 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
321cd 34 36 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  46,&yymsp[-1].mi
321ce 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20  nor.yy0);}.     
321cf 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
321d0 63 61 73 65 20 32 32 34 3a 20 2f 2a 20 65 78 70  case 224: /* exp
321d1 72 20 3a 3a 3d 20 65 78 70 72 20 62 65 74 77 65  r ::= expr betwe
321d2 65 6e 5f 6f 70 20 65 78 70 72 20 41 4e 44 20 65  en_op expr AND e
321d3 78 70 72 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 4c  xpr */.{.  ExprL
321d4 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 73 71 6c  ist *pList = sql
321d5 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
321d6 6e 64 28 70 50 61 72 73 65 2c 30 2c 20 79 79 6d  nd(pParse,0, yym
321d7 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-2].minor.yy3
321d8 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 70 4c 69  46.pExpr);.  pLi
321d9 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
321da 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
321db 65 2c 70 4c 69 73 74 2c 20 79 79 6d 73 70 5b 30  e,pList, yymsp[0
321dc 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  ].minor.yy346.pE
321dd 78 70 72 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  xpr);.  yygotomi
321de 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
321df 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
321e0 50 61 72 73 65 2c 20 54 4b 5f 42 45 54 57 45 45  Parse, TK_BETWEE
321e1 4e 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  N, yymsp[-4].min
321e2 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20  or.yy346.pExpr, 
321e3 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 79 79 67  0, 0);.  if( yyg
321e4 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
321e5 45 78 70 72 20 29 7b 0a 20 20 20 20 79 79 67 6f  Expr ){.    yygo
321e6 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
321e7 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 70  xpr->x.pList = p
321e8 4c 69 73 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  List;.  }else{. 
321e9 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
321ea 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  stDelete(pParse-
321eb 3e 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 7d  >db, pList);.  }
321ec 20 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 33   .  if( yymsp[-3
321ed 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 20 29 20  ].minor.yy328 ) 
321ee 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
321ef 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  6.pExpr = sqlite
321f0 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
321f1 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e  K_NOT, yygotomin
321f2 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20  or.yy346.pExpr, 
321f3 30 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d  0, 0);.  yygotom
321f4 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72  inor.yy346.zStar
321f5 74 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  t = yymsp[-4].mi
321f6 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74  nor.yy346.zStart
321f7 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
321f8 79 79 33 34 36 2e 7a 45 6e 64 20 3d 20 79 79 6d  yy346.zEnd = yym
321f9 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  sp[0].minor.yy34
321fa 36 2e 7a 45 6e 64 3b 0a 7d 0a 20 20 20 20 20 20  6.zEnd;.}.      
321fb 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
321fc 61 73 65 20 32 32 37 3a 20 2f 2a 20 65 78 70 72  ase 227: /* expr
321fd 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20   ::= expr in_op 
321fe 4c 50 20 65 78 70 72 6c 69 73 74 20 52 50 20 2a  LP exprlist RP *
321ff 2f 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69  /.{.    yygotomi
32200 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
32201 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
32202 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 79 79  Parse, TK_IN, yy
32203 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
32204 33 34 36 2e 70 45 78 70 72 2c 20 30 2c 20 30 29  346.pExpr, 0, 0)
32205 3b 0a 20 20 20 20 69 66 28 20 79 79 67 6f 74 6f  ;.    if( yygoto
32206 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
32207 72 20 29 7b 0a 20 20 20 20 20 20 79 79 67 6f 74  r ){.      yygot
32208 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
32209 70 72 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 79 79  pr->x.pList = yy
3220a 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
3220b 31 34 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  14;.      sqlite
3220c 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70  3ExprSetHeight(p
3220d 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e  Parse, yygotomin
3220e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b  or.yy346.pExpr);
3220f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
32210 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
32211 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
32212 64 62 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  db, yymsp[-1].mi
32213 6e 6f 72 2e 79 79 31 34 29 3b 0a 20 20 20 20 7d  nor.yy14);.    }
32214 0a 20 20 20 20 69 66 28 20 79 79 6d 73 70 5b 2d  .    if( yymsp[-
32215 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 20 29  3].minor.yy328 )
32216 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
32217 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74  46.pExpr = sqlit
32218 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
32219 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69  TK_NOT, yygotomi
3221a 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c  nor.yy346.pExpr,
3221b 20 30 2c 20 30 29 3b 0a 20 20 20 20 79 79 67 6f   0, 0);.    yygo
3221c 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53  tominor.yy346.zS
3221d 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 34 5d  tart = yymsp[-4]
3221e 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74  .minor.yy346.zSt
3221f 61 72 74 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  art;.    yygotom
32220 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64 20  inor.yy346.zEnd 
32221 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  = &yymsp[0].mino
32222 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d  r.yy0.z[yymsp[0]
32223 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 20  .minor.yy0.n];. 
32224 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
32225 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 38  ;.      case 228
32226 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4c 50  : /* expr ::= LP
32227 20 73 65 6c 65 63 74 20 52 50 20 2a 2f 0a 7b 0a   select RP */.{.
32228 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
32229 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71  yy346.pExpr = sq
3222a 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
3222b 65 2c 20 54 4b 5f 53 45 4c 45 43 54 2c 20 30 2c  e, TK_SELECT, 0,
3222c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20   0, 0);.    if( 
3222d 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3222e 36 2e 70 45 78 70 72 20 29 7b 0a 20 20 20 20 20  6.pExpr ){.     
3222f 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
32230 34 36 2e 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c  46.pExpr->x.pSel
32231 65 63 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e  ect = yymsp[-1].
32232 6d 69 6e 6f 72 2e 79 79 33 3b 0a 20 20 20 20 20  minor.yy3;.     
32233 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79   ExprSetProperty
32234 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  (yygotominor.yy3
32235 34 36 2e 70 45 78 70 72 2c 20 45 50 5f 78 49 73  46.pExpr, EP_xIs
32236 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 73  Select);.      s
32237 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69  qlite3ExprSetHei
32238 67 68 74 28 70 50 61 72 73 65 2c 20 79 79 67 6f  ght(pParse, yygo
32239 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
3223a 78 70 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  xpr);.    }else{
3223b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65  .      sqlite3Se
3223c 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61 72 73  lectDelete(pPars
3223d 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 31 5d  e->db, yymsp[-1]
3223e 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b 0a 20 20 20  .minor.yy3);.   
3223f 20 7d 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e   }.    yygotomin
32240 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74 20  or.yy346.zStart 
32241 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  = yymsp[-2].mino
32242 72 2e 79 79 30 2e 7a 3b 0a 20 20 20 20 79 79 67  r.yy0.z;.    yyg
32243 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  otominor.yy346.z
32244 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e  End = &yymsp[0].
32245 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73  minor.yy0.z[yyms
32246 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e  p[0].minor.yy0.n
32247 5d 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62  ];.  }.        b
32248 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32249 20 32 32 39 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   229: /* expr ::
3224a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20  = expr in_op LP 
3224b 73 65 6c 65 63 74 20 52 50 20 2a 2f 0a 7b 0a 20  select RP */.{. 
3224c 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
3224d 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c  y346.pExpr = sql
3224e 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
3224f 2c 20 54 4b 5f 49 4e 2c 20 79 79 6d 73 70 5b 2d  , TK_IN, yymsp[-
32250 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  4].minor.yy346.p
32251 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Expr, 0, 0);.   
32252 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   if( yygotominor
32253 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29 7b 0a  .yy346.pExpr ){.
32254 20 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f        yygotomino
32255 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 78  r.yy346.pExpr->x
32256 2e 70 53 65 6c 65 63 74 20 3d 20 79 79 6d 73 70  .pSelect = yymsp
32257 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 3b 0a  [-1].minor.yy3;.
32258 20 20 20 20 20 20 45 78 70 72 53 65 74 50 72 6f        ExprSetPro
32259 70 65 72 74 79 28 79 79 67 6f 74 6f 6d 69 6e 6f  perty(yygotomino
3225a 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20 45  r.yy346.pExpr, E
3225b 50 5f 78 49 73 53 65 6c 65 63 74 29 3b 0a 20 20  P_xIsSelect);.  
3225c 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53      sqlite3ExprS
3225d 65 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c  etHeight(pParse,
3225e 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
3225f 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d  46.pExpr);.    }
32260 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
32261 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
32262 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73  pParse->db, yyms
32263 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29  p[-1].minor.yy3)
32264 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
32265 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
32266 79 79 33 32 38 20 29 20 79 79 67 6f 74 6f 6d 69  yy328 ) yygotomi
32267 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
32268 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
32269 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79  Parse, TK_NOT, y
3226a 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
3226b 2e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20  .pExpr, 0, 0);. 
3226c 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
3226d 79 33 34 36 2e 7a 53 74 61 72 74 20 3d 20 79 79  y346.zStart = yy
3226e 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
3226f 33 34 36 2e 7a 53 74 61 72 74 3b 0a 20 20 20 20  346.zStart;.    
32270 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
32271 36 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b  6.zEnd = &yymsp[
32272 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79  0].minor.yy0.z[y
32273 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32274 30 2e 6e 5d 3b 0a 20 20 7d 0a 20 20 20 20 20 20  0.n];.  }.      
32275 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
32276 61 73 65 20 32 33 30 3a 20 2f 2a 20 65 78 70 72  ase 230: /* expr
32277 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20   ::= expr in_op 
32278 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 0a 20 20 20  nm dbnm */.{.   
32279 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 20 3d   SrcList *pSrc =
3227a 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
3227b 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62  ppend(pParse->db
3227c 2c 20 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  , 0,&yymsp[-1].m
3227d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
3227e 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  0].minor.yy0);. 
3227f 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
32280 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c  y346.pExpr = sql
32281 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
32282 2c 20 54 4b 5f 49 4e 2c 20 79 79 6d 73 70 5b 2d  , TK_IN, yymsp[-
32283 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  3].minor.yy346.p
32284 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Expr, 0, 0);.   
32285 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   if( yygotominor
32286 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29 7b 0a  .yy346.pExpr ){.
32287 20 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f        yygotomino
32288 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 78  r.yy346.pExpr->x
32289 2e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74  .pSelect = sqlit
3228a 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72  e3SelectNew(pPar
3228b 73 65 2c 20 30 2c 70 53 72 63 2c 30 2c 30 2c 30  se, 0,pSrc,0,0,0
3228c 2c 30 2c 30 2c 30 2c 30 29 3b 0a 20 20 20 20 20  ,0,0,0,0);.     
3228d 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79   ExprSetProperty
3228e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  (yygotominor.yy3
3228f 34 36 2e 70 45 78 70 72 2c 20 45 50 5f 78 49 73  46.pExpr, EP_xIs
32290 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 73  Select);.      s
32291 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69  qlite3ExprSetHei
32292 67 68 74 28 70 50 61 72 73 65 2c 20 79 79 67 6f  ght(pParse, yygo
32293 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
32294 78 70 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  xpr);.    }else{
32295 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72  .      sqlite3Sr
32296 63 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72  cListDelete(pPar
32297 73 65 2d 3e 64 62 2c 20 70 53 72 63 29 3b 0a 20  se->db, pSrc);. 
32298 20 20 20 7d 0a 20 20 20 20 69 66 28 20 79 79 6d     }.    if( yym
32299 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-2].minor.yy3
3229a 32 38 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  28 ) yygotominor
3229b 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73  .yy346.pExpr = s
3229c 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
3229d 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f  se, TK_NOT, yygo
3229e 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
3229f 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  xpr, 0, 0);.    
322a0 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
322a1 36 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70  6.zStart = yymsp
322a2 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  [-3].minor.yy346
322a3 2e 7a 53 74 61 72 74 3b 0a 20 20 20 20 79 79 67  .zStart;.    yyg
322a4 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  otominor.yy346.z
322a5 45 6e 64 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  End = yymsp[0].m
322a6 69 6e 6f 72 2e 79 79 30 2e 7a 20 3f 20 26 79 79  inor.yy0.z ? &yy
322a7 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
322a8 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  .z[yymsp[0].mino
322a9 72 2e 79 79 30 2e 6e 5d 20 3a 20 26 79 79 6d 73  r.yy0.n] : &yyms
322aa 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  p[-1].minor.yy0.
322ab 7a 5b 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  z[yymsp[-1].mino
322ac 72 2e 79 79 30 2e 6e 5d 3b 0a 20 20 7d 0a 20 20  r.yy0.n];.  }.  
322ad 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
322ae 20 20 20 63 61 73 65 20 32 33 31 3a 20 2f 2a 20     case 231: /* 
322af 65 78 70 72 20 3a 3a 3d 20 45 58 49 53 54 53 20  expr ::= EXISTS 
322b0 4c 50 20 73 65 6c 65 63 74 20 52 50 20 2a 2f 0a  LP select RP */.
322b1 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 20 3d 20  {.    Expr *p = 
322b2 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
322b3 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  6.pExpr = sqlite
322b4 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
322b5 4b 5f 45 58 49 53 54 53 2c 20 30 2c 20 30 2c 20  K_EXISTS, 0, 0, 
322b6 30 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b  0);.    if( p ){
322b7 0a 20 20 20 20 20 20 70 2d 3e 78 2e 70 53 65 6c  .      p->x.pSel
322b8 65 63 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e  ect = yymsp[-1].
322b9 6d 69 6e 6f 72 2e 79 79 33 3b 0a 20 20 20 20 20  minor.yy3;.     
322ba 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79   ExprSetProperty
322bb 28 70 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  (p, EP_xIsSelect
322bc 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
322bd 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50  ExprSetHeight(pP
322be 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 7d 65  arse, p);.    }e
322bf 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
322c0 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70  e3SelectDelete(p
322c1 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70  Parse->db, yymsp
322c2 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b  [-1].minor.yy3);
322c3 0a 20 20 20 20 7d 0a 20 20 20 20 79 79 67 6f 74  .    }.    yygot
322c4 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74  ominor.yy346.zSt
322c5 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e  art = yymsp[-3].
322c6 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 20  minor.yy0.z;.   
322c7 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
322c8 34 36 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70  46.zEnd = &yymsp
322c9 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b  [0].minor.yy0.z[
322ca 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
322cb 79 30 2e 6e 5d 3b 0a 20 20 7d 0a 20 20 20 20 20  y0.n];.  }.     
322cc 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
322cd 63 61 73 65 20 32 33 32 3a 20 2f 2a 20 65 78 70  case 232: /* exp
322ce 72 20 3a 3a 3d 20 43 41 53 45 20 63 61 73 65 5f  r ::= CASE case_
322cf 6f 70 65 72 61 6e 64 20 63 61 73 65 5f 65 78 70  operand case_exp
322d0 72 6c 69 73 74 20 63 61 73 65 5f 65 6c 73 65 20  rlist case_else 
322d1 45 4e 44 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74  END */.{.  yygot
322d2 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
322d3 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  pr = sqlite3PExp
322d4 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 43 41 53  r(pParse, TK_CAS
322d5 45 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  E, yymsp[-3].min
322d6 6f 72 2e 79 79 31 33 32 2c 20 79 79 6d 73 70 5b  or.yy132, yymsp[
322d7 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c  -1].minor.yy132,
322d8 20 30 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74   0);.  if( yygot
322d9 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
322da 70 72 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f  pr ){.    yygoto
322db 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
322dc 72 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 79 79 6d  r->x.pList = yym
322dd 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-2].minor.yy1
322de 34 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  4;.    sqlite3Ex
322df 70 72 53 65 74 48 65 69 67 68 74 28 70 50 61 72  prSetHeight(pPar
322e0 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  se, yygotominor.
322e1 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20  yy346.pExpr);.  
322e2 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
322e3 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
322e4 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d  (pParse->db, yym
322e5 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-2].minor.yy1
322e6 34 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f  4);.  }.  yygoto
322e7 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61  minor.yy346.zSta
322e8 72 74 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  rt = yymsp[-4].m
322e9 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79  inor.yy0.z;.  yy
322ea 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
322eb 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d  zEnd = &yymsp[0]
322ec 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d  .minor.yy0.z[yym
322ed 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
322ee 6e 5d 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  n];.}.        br
322ef 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
322f0 32 33 33 3a 20 2f 2a 20 63 61 73 65 5f 65 78 70  233: /* case_exp
322f1 72 6c 69 73 74 20 3a 3a 3d 20 63 61 73 65 5f 65  rlist ::= case_e
322f2 78 70 72 6c 69 73 74 20 57 48 45 4e 20 65 78 70  xprlist WHEN exp
322f3 72 20 54 48 45 4e 20 65 78 70 72 20 2a 2f 0a 7b  r THEN expr */.{
322f4 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
322f5 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70  y14 = sqlite3Exp
322f6 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
322f7 73 65 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  se,yymsp[-4].min
322f8 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73 70 5b 2d  or.yy14, yymsp[-
322f9 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  2].minor.yy346.p
322fa 45 78 70 72 29 3b 0a 20 20 79 79 67 6f 74 6f 6d  Expr);.  yygotom
322fb 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69  inor.yy14 = sqli
322fc 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
322fd 64 28 70 50 61 72 73 65 2c 79 79 67 6f 74 6f 6d  d(pParse,yygotom
322fe 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73 70  inor.yy14, yymsp
322ff 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  [0].minor.yy346.
32300 70 45 78 70 72 29 3b 0a 7d 0a 20 20 20 20 20 20  pExpr);.}.      
32301 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
32302 61 73 65 20 32 33 34 3a 20 2f 2a 20 63 61 73 65  ase 234: /* case
32303 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 57 48  _exprlist ::= WH
32304 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78 70  EN expr THEN exp
32305 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  r */.{.  yygotom
32306 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69  inor.yy14 = sqli
32307 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
32308 64 28 70 50 61 72 73 65 2c 30 2c 20 79 79 6d 73  d(pParse,0, yyms
32309 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-2].minor.yy34
3230a 36 2e 70 45 78 70 72 29 3b 0a 20 20 79 79 67 6f  6.pExpr);.  yygo
3230b 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73  tominor.yy14 = s
3230c 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
3230d 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 67 6f  pend(pParse,yygo
3230e 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79  tominor.yy14, yy
3230f 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
32310 34 36 2e 70 45 78 70 72 29 3b 0a 7d 0a 20 20 20  46.pExpr);.}.   
32311 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32312 20 20 63 61 73 65 20 32 34 33 3a 20 2f 2a 20 63    case 243: /* c
32313 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20  md ::= createkw 
32314 75 6e 69 71 75 65 66 6c 61 67 20 49 4e 44 45 58  uniqueflag INDEX
32315 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20   ifnotexists nm 
32316 64 62 6e 6d 20 4f 4e 20 6e 6d 20 4c 50 20 69 64  dbnm ON nm LP id
32317 78 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 0a 20 20  xlist RP */.{.  
32318 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64  sqlite3CreateInd
32319 65 78 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73  ex(pParse, &yyms
3231a 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-6].minor.yy0,
3231b 20 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f   &yymsp[-5].mino
3231c 72 2e 79 79 30 2c 20 0a 20 20 20 20 20 20 20 20  r.yy0, .        
3231d 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
3231e 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
3231f 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 30 2c 26  d(pParse->db,0,&
32320 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
32321 79 79 30 2c 30 29 2c 20 79 79 6d 73 70 5b 2d 31  yy0,0), yymsp[-1
32322 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79  ].minor.yy14, yy
32323 6d 73 70 5b 2d 39 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-9].minor.yy
32324 33 32 38 2c 0a 20 20 20 20 20 20 20 20 20 20 20  328,.           
32325 20 20 20 20 20 20 20 20 20 20 20 26 79 79 6d 73             &yyms
32326 70 5b 2d 31 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  p[-10].minor.yy0
32327 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
32328 72 2e 79 79 30 2c 20 53 51 4c 49 54 45 5f 53 4f  r.yy0, SQLITE_SO
32329 5f 41 53 43 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e  _ASC, yymsp[-7].
3232a 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 0a 7d 0a  minor.yy328);.}.
3232b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3232c 20 20 20 20 20 63 61 73 65 20 32 34 34 3a 20 2f       case 244: /
3232d 2a 20 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d  * uniqueflag ::=
3232e 20 55 4e 49 51 55 45 20 2a 2f 0a 20 20 20 20 20   UNIQUE */.     
3232f 20 63 61 73 65 20 32 39 38 3a 20 2f 2a 20 72 61   case 298: /* ra
32330 69 73 65 74 79 70 65 20 3a 3a 3d 20 41 42 4f 52  isetype ::= ABOR
32331 54 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  T */ yytestcase(
32332 79 79 72 75 6c 65 6e 6f 3d 3d 32 39 38 29 3b 0a  yyruleno==298);.
32333 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  {yygotominor.yy3
32334 32 38 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 7d 0a  28 = OE_Abort;}.
32335 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32336 20 20 20 20 20 63 61 73 65 20 32 34 35 3a 20 2f       case 245: /
32337 2a 20 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d  * uniqueflag ::=
32338 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
32339 2e 79 79 33 32 38 20 3d 20 4f 45 5f 4e 6f 6e 65  .yy328 = OE_None
3233a 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3233b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 38  ;.      case 248
3233c 3a 20 2f 2a 20 69 64 78 6c 69 73 74 20 3a 3a 3d  : /* idxlist ::=
3233d 20 69 64 78 6c 69 73 74 20 43 4f 4d 4d 41 20 6e   idxlist COMMA n
3233e 6d 20 63 6f 6c 6c 61 74 65 20 73 6f 72 74 6f 72  m collate sortor
3233f 64 65 72 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20  der */.{.  Expr 
32340 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 79 79  *p = 0;.  if( yy
32341 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
32342 30 2e 6e 3e 30 20 29 7b 0a 20 20 20 20 70 20 3d  0.n>0 ){.    p =
32343 20 73 71 6c 69 74 65 33 45 78 70 72 28 70 50 61   sqlite3Expr(pPa
32344 72 73 65 2d 3e 64 62 2c 20 54 4b 5f 43 4f 4c 55  rse->db, TK_COLU
32345 4d 4e 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  MN, 0);.    sqli
32346 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 70  te3ExprSetColl(p
32347 50 61 72 73 65 2c 20 70 2c 20 26 79 79 6d 73 70  Parse, p, &yymsp
32348 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-1].minor.yy0);
32349 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  .  }.  yygotomin
3234a 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65  or.yy14 = sqlite
3234b 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
3234c 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 34 5d  pParse,yymsp[-4]
3234d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 70 29 3b  .minor.yy14, p);
3234e 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  .  sqlite3ExprLi
3234f 73 74 53 65 74 4e 61 6d 65 28 70 50 61 72 73 65  stSetName(pParse
32350 2c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  ,yygotominor.yy1
32351 34 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  4,&yymsp[-2].min
32352 6f 72 2e 79 79 30 2c 31 29 3b 0a 20 20 73 71 6c  or.yy0,1);.  sql
32353 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63  ite3ExprListChec
32354 6b 4c 65 6e 67 74 68 28 70 50 61 72 73 65 2c 20  kLength(pParse, 
32355 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34  yygotominor.yy14
32356 2c 20 22 69 6e 64 65 78 22 29 3b 0a 20 20 69 66  , "index");.  if
32357 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
32358 31 34 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  14 ) yygotominor
32359 2e 79 79 31 34 2d 3e 61 5b 79 79 67 6f 74 6f 6d  .yy14->a[yygotom
3235a 69 6e 6f 72 2e 79 79 31 34 2d 3e 6e 45 78 70 72  inor.yy14->nExpr
3235b 2d 31 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20  -1].sortOrder = 
3235c 28 75 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  (u8)yymsp[0].min
3235d 6f 72 2e 79 79 33 32 38 3b 0a 7d 0a 20 20 20 20  or.yy328;.}.    
3235e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3235f 20 63 61 73 65 20 32 34 39 3a 20 2f 2a 20 69 64   case 249: /* id
32360 78 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 63 6f 6c  xlist ::= nm col
32361 6c 61 74 65 20 73 6f 72 74 6f 72 64 65 72 20 2a  late sortorder *
32362 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20  /.{.  Expr *p = 
32363 30 3b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d  0;.  if( yymsp[-
32364 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3e 30  1].minor.yy0.n>0
32365 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69   ){.    p = sqli
32366 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
32367 20 54 4b 5f 43 4f 4c 55 4d 4e 2c 20 30 2c 20 30   TK_COLUMN, 0, 0
32368 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
32369 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 70 50 61  3ExprSetColl(pPa
3236a 72 73 65 2c 20 70 2c 20 26 79 79 6d 73 70 5b 2d  rse, p, &yymsp[-
3236b 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  1].minor.yy0);. 
3236c 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   }.  yygotominor
3236d 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45  .yy14 = sqlite3E
3236e 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
3236f 61 72 73 65 2c 30 2c 20 70 29 3b 0a 20 20 73 71  arse,0, p);.  sq
32370 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74  lite3ExprListSet
32371 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 79 79 67  Name(pParse, yyg
32372 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26  otominor.yy14, &
32373 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
32374 79 79 30 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74  yy0, 1);.  sqlit
32375 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c  e3ExprListCheckL
32376 65 6e 67 74 68 28 70 50 61 72 73 65 2c 20 79 79  ength(pParse, yy
32377 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20  gotominor.yy14, 
32378 22 69 6e 64 65 78 22 29 3b 0a 20 20 69 66 28 20  "index");.  if( 
32379 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34  yygotominor.yy14
3237a 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79   ) yygotominor.y
3237b 79 31 34 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e  y14->a[yygotomin
3237c 6f 72 2e 79 79 31 34 2d 3e 6e 45 78 70 72 2d 31  or.yy14->nExpr-1
3237d 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 28 75  ].sortOrder = (u
3237e 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  8)yymsp[0].minor
3237f 2e 79 79 33 32 38 3b 0a 7d 0a 20 20 20 20 20 20  .yy328;.}.      
32380 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
32381 61 73 65 20 32 35 30 3a 20 2f 2a 20 63 6f 6c 6c  ase 250: /* coll
32382 61 74 65 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f  ate ::= */.{yygo
32383 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20  tominor.yy0.z = 
32384 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  0; yygotominor.y
32385 79 30 2e 6e 20 3d 20 30 3b 7d 0a 20 20 20 20 20  y0.n = 0;}.     
32386 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32387 63 61 73 65 20 32 35 32 3a 20 2f 2a 20 63 6d 64  case 252: /* cmd
32388 20 3a 3a 3d 20 44 52 4f 50 20 49 4e 44 45 58 20   ::= DROP INDEX 
32389 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d  ifexists fullnam
3238a 65 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 44 72 6f  e */.{sqlite3Dro
3238b 70 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 79  pIndex(pParse, y
3238c 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3238d 36 35 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  65, yymsp[-1].mi
3238e 6e 6f 72 2e 79 79 33 32 38 29 3b 7d 0a 20 20 20  nor.yy328);}.   
3238f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32390 20 20 63 61 73 65 20 32 35 33 3a 20 2f 2a 20 63    case 253: /* c
32391 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d 20 2a 2f  md ::= VACUUM */
32392 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 34 3a  .      case 254:
32393 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 56 41 43 55   /* cmd ::= VACU
32394 55 4d 20 6e 6d 20 2a 2f 20 79 79 74 65 73 74 63  UM nm */ yytestc
32395 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 35  ase(yyruleno==25
32396 34 29 3b 0a 7b 73 71 6c 69 74 65 33 56 61 63 75  4);.{sqlite3Vacu
32397 75 6d 28 70 50 61 72 73 65 29 3b 7d 0a 20 20 20  um(pParse);}.   
32398 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32399 20 20 63 61 73 65 20 32 35 35 3a 20 2f 2a 20 63    case 255: /* c
3239a 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d  md ::= PRAGMA nm
3239b 20 64 62 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65   dbnm */.{sqlite
3239c 33 50 72 61 67 6d 61 28 70 50 61 72 73 65 2c 26  3Pragma(pParse,&
3239d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3239e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy0,&yymsp[0].mi
3239f 6e 6f 72 2e 79 79 30 2c 30 2c 30 29 3b 7d 0a 20  nor.yy0,0,0);}. 
323a0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
323a1 20 20 20 20 63 61 73 65 20 32 35 36 3a 20 2f 2a      case 256: /*
323a2 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20   cmd ::= PRAGMA 
323a3 6e 6d 20 64 62 6e 6d 20 45 51 20 6e 6d 6e 75 6d  nm dbnm EQ nmnum
323a4 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 50 72 61 67   */.{sqlite3Prag
323a5 6d 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  ma(pParse,&yymsp
323a6 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-3].minor.yy0,&
323a7 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
323a8 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy0,&yymsp[0].mi
323a9 6e 6f 72 2e 79 79 30 2c 30 29 3b 7d 0a 20 20 20  nor.yy0,0);}.   
323aa 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
323ab 20 20 63 61 73 65 20 32 35 37 3a 20 2f 2a 20 63    case 257: /* c
323ac 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d  md ::= PRAGMA nm
323ad 20 64 62 6e 6d 20 4c 50 20 6e 6d 6e 75 6d 20 52   dbnm LP nmnum R
323ae 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 50 72 61  P */.{sqlite3Pra
323af 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73  gma(pParse,&yyms
323b0 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-4].minor.yy0,
323b1 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  &yymsp[-3].minor
323b2 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  .yy0,&yymsp[-1].
323b3 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 7d 0a 20  minor.yy0,0);}. 
323b4 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
323b5 20 20 20 20 63 61 73 65 20 32 35 38 3a 20 2f 2a      case 258: /*
323b6 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20   cmd ::= PRAGMA 
323b7 6e 6d 20 64 62 6e 6d 20 45 51 20 6d 69 6e 75 73  nm dbnm EQ minus
323b8 5f 6e 75 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  _num */.{sqlite3
323b9 50 72 61 67 6d 61 28 70 50 61 72 73 65 2c 26 79  Pragma(pParse,&y
323ba 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
323bb 79 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  y0,&yymsp[-2].mi
323bc 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30  nor.yy0,&yymsp[0
323bd 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 31 29 3b 7d  ].minor.yy0,1);}
323be 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
323bf 20 20 20 20 20 20 63 61 73 65 20 32 35 39 3a 20        case 259: 
323c0 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d  /* cmd ::= PRAGM
323c1 41 20 6e 6d 20 64 62 6e 6d 20 4c 50 20 6d 69 6e  A nm dbnm LP min
323c2 75 73 5f 6e 75 6d 20 52 50 20 2a 2f 0a 7b 73 71  us_num RP */.{sq
323c3 6c 69 74 65 33 50 72 61 67 6d 61 28 70 50 61 72  lite3Pragma(pPar
323c4 73 65 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  se,&yymsp[-4].mi
323c5 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d  nor.yy0,&yymsp[-
323c6 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  3].minor.yy0,&yy
323c7 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
323c8 30 2c 31 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  0,1);}.        b
323c9 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
323ca 20 32 37 30 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   270: /* cmd ::=
323cb 20 63 72 65 61 74 65 6b 77 20 74 72 69 67 67 65   createkw trigge
323cc 72 5f 64 65 63 6c 20 42 45 47 49 4e 20 74 72 69  r_decl BEGIN tri
323cd 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 45 4e  gger_cmd_list EN
323ce 44 20 2a 2f 0a 7b 0a 20 20 54 6f 6b 65 6e 20 61  D */.{.  Token a
323cf 6c 6c 3b 0a 20 20 61 6c 6c 2e 7a 20 3d 20 79 79  ll;.  all.z = yy
323d0 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
323d1 30 2e 7a 3b 0a 20 20 61 6c 6c 2e 6e 20 3d 20 28  0.z;.  all.n = (
323d2 69 6e 74 29 28 79 79 6d 73 70 5b 30 5d 2e 6d 69  int)(yymsp[0].mi
323d3 6e 6f 72 2e 79 79 30 2e 7a 20 2d 20 79 79 6d 73  nor.yy0.z - yyms
323d4 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  p[-3].minor.yy0.
323d5 7a 29 20 2b 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  z) + yymsp[0].mi
323d6 6e 6f 72 2e 79 79 30 2e 6e 3b 0a 20 20 73 71 6c  nor.yy0.n;.  sql
323d7 69 74 65 33 46 69 6e 69 73 68 54 72 69 67 67 65  ite3FinishTrigge
323d8 72 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  r(pParse, yymsp[
323d9 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 2c  -1].minor.yy473,
323da 20 26 61 6c 6c 29 3b 0a 7d 0a 20 20 20 20 20 20   &all);.}.      
323db 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
323dc 61 73 65 20 32 37 31 3a 20 2f 2a 20 74 72 69 67  ase 271: /* trig
323dd 67 65 72 5f 64 65 63 6c 20 3a 3a 3d 20 74 65 6d  ger_decl ::= tem
323de 70 20 54 52 49 47 47 45 52 20 69 66 6e 6f 74 65  p TRIGGER ifnote
323df 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20 74 72  xists nm dbnm tr
323e0 69 67 67 65 72 5f 74 69 6d 65 20 74 72 69 67 67  igger_time trigg
323e1 65 72 5f 65 76 65 6e 74 20 4f 4e 20 66 75 6c 6c  er_event ON full
323e2 6e 61 6d 65 20 66 6f 72 65 61 63 68 5f 63 6c 61  name foreach_cla
323e3 75 73 65 20 77 68 65 6e 5f 63 6c 61 75 73 65 20  use when_clause 
323e4 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 42 65  */.{.  sqlite3Be
323e5 67 69 6e 54 72 69 67 67 65 72 28 70 50 61 72 73  ginTrigger(pPars
323e6 65 2c 20 26 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69  e, &yymsp[-7].mi
323e7 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b  nor.yy0, &yymsp[
323e8 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79  -6].minor.yy0, y
323e9 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-5].minor.y
323ea 79 33 32 38 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  y328, yymsp[-4].
323eb 6d 69 6e 6f 72 2e 79 79 33 37 38 2e 61 2c 20 79  minor.yy378.a, y
323ec 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
323ed 79 33 37 38 2e 62 2c 20 79 79 6d 73 70 5b 2d 32  y378.b, yymsp[-2
323ee 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 79 79  ].minor.yy65, yy
323ef 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31  msp[0].minor.yy1
323f0 33 32 2c 20 79 79 6d 73 70 5b 2d 31 30 5d 2e 6d  32, yymsp[-10].m
323f1 69 6e 6f 72 2e 79 79 33 32 38 2c 20 79 79 6d 73  inor.yy328, yyms
323f2 70 5b 2d 38 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-8].minor.yy32
323f3 38 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  8);.  yygotomino
323f4 72 2e 79 79 30 20 3d 20 28 79 79 6d 73 70 5b 2d  r.yy0 = (yymsp[-
323f5 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 3d  6].minor.yy0.n==
323f6 30 3f 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f  0?yymsp[-7].mino
323f7 72 2e 79 79 30 3a 79 79 6d 73 70 5b 2d 36 5d 2e  r.yy0:yymsp[-6].
323f8 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20  minor.yy0);.}.  
323f9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
323fa 20 20 20 63 61 73 65 20 32 37 32 3a 20 2f 2a 20     case 272: /* 
323fb 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d  trigger_time ::=
323fc 20 42 45 46 4f 52 45 20 2a 2f 0a 20 20 20 20 20   BEFORE */.     
323fd 20 63 61 73 65 20 32 37 35 3a 20 2f 2a 20 74 72   case 275: /* tr
323fe 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 2a  igger_time ::= *
323ff 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
32400 75 6c 65 6e 6f 3d 3d 32 37 35 29 3b 0a 7b 20 79  uleno==275);.{ y
32401 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38  ygotominor.yy328
32402 20 3d 20 54 4b 5f 42 45 46 4f 52 45 3b 20 7d 0a   = TK_BEFORE; }.
32403 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32404 20 20 20 20 20 63 61 73 65 20 32 37 33 3a 20 2f       case 273: /
32405 2a 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a  * trigger_time :
32406 3a 3d 20 41 46 54 45 52 20 2a 2f 0a 7b 20 79 79  := AFTER */.{ yy
32407 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20  gotominor.yy328 
32408 3d 20 54 4b 5f 41 46 54 45 52 3b 20 20 7d 0a 20  = TK_AFTER;  }. 
32409 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3240a 20 20 20 20 63 61 73 65 20 32 37 34 3a 20 2f 2a      case 274: /*
3240b 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a   trigger_time ::
3240c 3d 20 49 4e 53 54 45 41 44 20 4f 46 20 2a 2f 0a  = INSTEAD OF */.
3240d 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
3240e 33 32 38 20 3d 20 54 4b 5f 49 4e 53 54 45 41 44  328 = TK_INSTEAD
3240f 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
32410 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 36  ;.      case 276
32411 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 65 76 65  : /* trigger_eve
32412 6e 74 20 3a 3a 3d 20 44 45 4c 45 54 45 7c 49 4e  nt ::= DELETE|IN
32413 53 45 52 54 20 2a 2f 0a 20 20 20 20 20 20 63 61  SERT */.      ca
32414 73 65 20 32 37 37 3a 20 2f 2a 20 74 72 69 67 67  se 277: /* trigg
32415 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44  er_event ::= UPD
32416 41 54 45 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ATE */ yytestcas
32417 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 37 37 29  e(yyruleno==277)
32418 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ;.{yygotominor.y
32419 79 33 37 38 2e 61 20 3d 20 79 79 6d 73 70 5b 30  y378.a = yymsp[0
3241a 5d 2e 6d 61 6a 6f 72 3b 20 79 79 67 6f 74 6f 6d  ].major; yygotom
3241b 69 6e 6f 72 2e 79 79 33 37 38 2e 62 20 3d 20 30  inor.yy378.b = 0
3241c 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3241d 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 38  ;.      case 278
3241e 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 65 76 65  : /* trigger_eve
3241f 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45 20 4f 46  nt ::= UPDATE OF
32420 20 69 6e 73 63 6f 6c 6c 69 73 74 20 2a 2f 0a 7b   inscollist */.{
32421 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37  yygotominor.yy37
32422 38 2e 61 20 3d 20 54 4b 5f 55 50 44 41 54 45 3b  8.a = TK_UPDATE;
32423 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
32424 37 38 2e 62 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  78.b = yymsp[0].
32425 6d 69 6e 6f 72 2e 79 79 34 30 38 3b 7d 0a 20 20  minor.yy408;}.  
32426 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32427 20 20 20 63 61 73 65 20 32 38 31 3a 20 2f 2a 20     case 281: /* 
32428 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d 20  when_clause ::= 
32429 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33 30  */.      case 30
3242a 33 3a 20 2f 2a 20 6b 65 79 5f 6f 70 74 20 3a 3a  3: /* key_opt ::
3242b 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
3242c 79 79 72 75 6c 65 6e 6f 3d 3d 33 30 33 29 3b 0a  yyruleno==303);.
3242d 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
3242e 31 33 32 20 3d 20 30 3b 20 7d 0a 20 20 20 20 20  132 = 0; }.     
3242f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32430 63 61 73 65 20 32 38 32 3a 20 2f 2a 20 77 68 65  case 282: /* whe
32431 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 57 48 45  n_clause ::= WHE
32432 4e 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20  N expr */.      
32433 63 61 73 65 20 33 30 34 3a 20 2f 2a 20 6b 65 79  case 304: /* key
32434 5f 6f 70 74 20 3a 3a 3d 20 4b 45 59 20 65 78 70  _opt ::= KEY exp
32435 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  r */ yytestcase(
32436 79 79 72 75 6c 65 6e 6f 3d 3d 33 30 34 29 3b 0a  yyruleno==304);.
32437 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
32438 31 33 32 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  132 = yymsp[0].m
32439 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
3243a 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
3243b 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38  k;.      case 28
3243c 33 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d  3: /* trigger_cm
3243d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69 67 67  d_list ::= trigg
3243e 65 72 5f 63 6d 64 5f 6c 69 73 74 20 74 72 69 67  er_cmd_list trig
3243f 67 65 72 5f 63 6d 64 20 53 45 4d 49 20 2a 2f 0a  ger_cmd SEMI */.
32440 7b 0a 20 20 61 73 73 65 72 74 28 20 79 79 6d 73  {.  assert( yyms
32441 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37  p[-2].minor.yy47
32442 33 21 3d 30 20 29 3b 0a 20 20 79 79 6d 73 70 5b  3!=0 );.  yymsp[
32443 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 2d  -2].minor.yy473-
32444 3e 70 4c 61 73 74 2d 3e 70 4e 65 78 74 20 3d 20  >pLast->pNext = 
32445 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
32446 79 79 34 37 33 3b 0a 20 20 79 79 6d 73 70 5b 2d  yy473;.  yymsp[-
32447 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 2d 3e  2].minor.yy473->
32448 70 4c 61 73 74 20 3d 20 79 79 6d 73 70 5b 2d 31  pLast = yymsp[-1
32449 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 3b 0a 20  ].minor.yy473;. 
3244a 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
3244b 37 33 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  73 = yymsp[-2].m
3244c 69 6e 6f 72 2e 79 79 34 37 33 3b 0a 7d 0a 20 20  inor.yy473;.}.  
3244d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3244e 20 20 20 63 61 73 65 20 32 38 34 3a 20 2f 2a 20     case 284: /* 
3244f 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74  trigger_cmd_list
32450 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f 63 6d 64   ::= trigger_cmd
32451 20 53 45 4d 49 20 2a 2f 0a 7b 20 0a 20 20 61 73   SEMI */.{ .  as
32452 73 65 72 74 28 20 79 79 6d 73 70 5b 2d 31 5d 2e  sert( yymsp[-1].
32453 6d 69 6e 6f 72 2e 79 79 34 37 33 21 3d 30 20 29  minor.yy473!=0 )
32454 3b 0a 20 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  ;.  yymsp[-1].mi
32455 6e 6f 72 2e 79 79 34 37 33 2d 3e 70 4c 61 73 74  nor.yy473->pLast
32456 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
32457 6f 72 2e 79 79 34 37 33 3b 0a 20 20 79 79 67 6f  or.yy473;.  yygo
32458 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 33 20 3d 20  tominor.yy473 = 
32459 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3245a 79 79 34 37 33 3b 0a 7d 0a 20 20 20 20 20 20 20  yy473;.}.       
3245b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3245c 73 65 20 32 38 36 3a 20 2f 2a 20 74 72 6e 6d 20  se 286: /* trnm 
3245d 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 2a 2f  ::= nm DOT nm */
3245e 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
3245f 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  .yy0 = yymsp[0].
32460 6d 69 6e 6f 72 2e 79 79 30 3b 0a 20 20 73 71 6c  minor.yy0;.  sql
32461 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
32462 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 71  rse, .        "q
32463 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e  ualified table n
32464 61 6d 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c  ames are not all
32465 6f 77 65 64 20 6f 6e 20 49 4e 53 45 52 54 2c 20  owed on INSERT, 
32466 55 50 44 41 54 45 2c 20 61 6e 64 20 44 45 4c 45  UPDATE, and DELE
32467 54 45 20 22 0a 20 20 20 20 20 20 20 20 22 73 74  TE ".        "st
32468 61 74 65 6d 65 6e 74 73 20 77 69 74 68 69 6e 20  atements within 
32469 74 72 69 67 67 65 72 73 22 29 3b 0a 7d 0a 20 20  triggers");.}.  
3246a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3246b 20 20 20 63 61 73 65 20 32 38 38 3a 20 2f 2a 20     case 288: /* 
3246c 74 72 69 64 78 62 79 20 3a 3a 3d 20 49 4e 44 45  tridxby ::= INDE
3246d 58 45 44 20 42 59 20 6e 6d 20 2a 2f 0a 7b 0a 20  XED BY nm */.{. 
3246e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
3246f 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
32470 20 22 74 68 65 20 49 4e 44 45 58 45 44 20 42 59   "the INDEXED BY
32471 20 63 6c 61 75 73 65 20 69 73 20 6e 6f 74 20 61   clause is not a
32472 6c 6c 6f 77 65 64 20 6f 6e 20 55 50 44 41 54 45  llowed on UPDATE
32473 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74 65   or DELETE state
32474 6d 65 6e 74 73 20 22 0a 20 20 20 20 20 20 20 20  ments ".        
32475 22 77 69 74 68 69 6e 20 74 72 69 67 67 65 72 73  "within triggers
32476 22 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  ");.}.        br
32477 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32478 32 38 39 3a 20 2f 2a 20 74 72 69 64 78 62 79 20  289: /* tridxby 
32479 3a 3a 3d 20 4e 4f 54 20 49 4e 44 45 58 45 44 20  ::= NOT INDEXED 
3247a 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 45 72  */.{.  sqlite3Er
3247b 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 0a 20  rorMsg(pParse,. 
3247c 20 20 20 20 20 20 20 22 74 68 65 20 4e 4f 54 20         "the NOT 
3247d 49 4e 44 45 58 45 44 20 63 6c 61 75 73 65 20 69  INDEXED clause i
3247e 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f 6e  s not allowed on
3247f 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54   UPDATE or DELET
32480 45 20 73 74 61 74 65 6d 65 6e 74 73 20 22 0a 20  E statements ". 
32481 20 20 20 20 20 20 20 22 77 69 74 68 69 6e 20 74         "within t
32482 72 69 67 67 65 72 73 22 29 3b 0a 7d 0a 20 20 20  riggers");.}.   
32483 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32484 20 20 63 61 73 65 20 32 39 30 3a 20 2f 2a 20 74    case 290: /* t
32485 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 55  rigger_cmd ::= U
32486 50 44 41 54 45 20 6f 72 63 6f 6e 66 20 74 72 6e  PDATE orconf trn
32487 6d 20 74 72 69 64 78 62 79 20 53 45 54 20 73 65  m tridxby SET se
32488 74 6c 69 73 74 20 77 68 65 72 65 5f 6f 70 74 20  tlist where_opt 
32489 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.{ yygotominor
3248a 2e 79 79 34 37 33 20 3d 20 73 71 6c 69 74 65 33  .yy473 = sqlite3
3248b 54 72 69 67 67 65 72 55 70 64 61 74 65 53 74 65  TriggerUpdateSte
3248c 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79  p(pParse->db, &y
3248d 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
3248e 79 30 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  y0, yymsp[-1].mi
3248f 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73 70 5b  nor.yy14, yymsp[
32490 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 20  0].minor.yy132, 
32491 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e  yymsp[-5].minor.
32492 79 79 31 38 36 29 3b 20 7d 0a 20 20 20 20 20 20  yy186); }.      
32493 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
32494 61 73 65 20 32 39 31 3a 20 2f 2a 20 74 72 69 67  ase 291: /* trig
32495 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65  ger_cmd ::= inse
32496 72 74 5f 63 6d 64 20 49 4e 54 4f 20 74 72 6e 6d  rt_cmd INTO trnm
32497 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20   inscollist_opt 
32498 56 41 4c 55 45 53 20 4c 50 20 69 74 65 6d 6c 69  VALUES LP itemli
32499 73 74 20 52 50 20 2a 2f 0a 7b 79 79 67 6f 74 6f  st RP */.{yygoto
3249a 6d 69 6e 6f 72 2e 79 79 34 37 33 20 3d 20 73 71  minor.yy473 = sq
3249b 6c 69 74 65 33 54 72 69 67 67 65 72 49 6e 73 65  lite3TriggerInse
3249c 72 74 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64  rtStep(pParse->d
3249d 62 2c 20 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69  b, &yymsp[-5].mi
3249e 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d  nor.yy0, yymsp[-
3249f 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 2c 20  4].minor.yy408, 
324a0 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
324a1 79 79 31 34 2c 20 30 2c 20 79 79 6d 73 70 5b 2d  yy14, 0, yymsp[-
324a2 37 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 36 29 3b  7].minor.yy186);
324a3 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
324a4 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 32 3a  .      case 292:
324a5 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 20   /* trigger_cmd 
324a6 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49  ::= insert_cmd I
324a7 4e 54 4f 20 74 72 6e 6d 20 69 6e 73 63 6f 6c 6c  NTO trnm inscoll
324a8 69 73 74 5f 6f 70 74 20 73 65 6c 65 63 74 20 2a  ist_opt select *
324a9 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
324aa 79 34 37 33 20 3d 20 73 71 6c 69 74 65 33 54 72  y473 = sqlite3Tr
324ab 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 70 28  iggerInsertStep(
324ac 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d  pParse->db, &yym
324ad 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
324ae 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
324af 72 2e 79 79 34 30 38 2c 20 30 2c 20 79 79 6d 73  r.yy408, 0, yyms
324b0 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2c 20  p[0].minor.yy3, 
324b1 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
324b2 79 79 31 38 36 29 3b 7d 0a 20 20 20 20 20 20 20  yy186);}.       
324b3 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
324b4 73 65 20 32 39 33 3a 20 2f 2a 20 74 72 69 67 67  se 293: /* trigg
324b5 65 72 5f 63 6d 64 20 3a 3a 3d 20 44 45 4c 45 54  er_cmd ::= DELET
324b6 45 20 46 52 4f 4d 20 74 72 6e 6d 20 74 72 69 64  E FROM trnm trid
324b7 78 62 79 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f  xby where_opt */
324b8 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
324b9 34 37 33 20 3d 20 73 71 6c 69 74 65 33 54 72 69  473 = sqlite3Tri
324ba 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 28 70  ggerDeleteStep(p
324bb 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73  Parse->db, &yyms
324bc 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-2].minor.yy0,
324bd 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
324be 79 79 31 33 32 29 3b 7d 0a 20 20 20 20 20 20 20  yy132);}.       
324bf 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
324c0 73 65 20 32 39 34 3a 20 2f 2a 20 74 72 69 67 67  se 294: /* trigg
324c1 65 72 5f 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63  er_cmd ::= selec
324c2 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  t */.{yygotomino
324c3 72 2e 79 79 34 37 33 20 3d 20 73 71 6c 69 74 65  r.yy473 = sqlite
324c4 33 54 72 69 67 67 65 72 53 65 6c 65 63 74 53 74  3TriggerSelectSt
324c5 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79  ep(pParse->db, y
324c6 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
324c7 33 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  3); }.        br
324c8 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
324c9 32 39 35 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  295: /* expr ::=
324ca 20 52 41 49 53 45 20 4c 50 20 49 47 4e 4f 52 45   RAISE LP IGNORE
324cb 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74   RP */.{.  yygot
324cc 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
324cd 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  pr = sqlite3PExp
324ce 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 52 41 49  r(pParse, TK_RAI
324cf 53 45 2c 20 30 2c 20 30 2c 20 30 29 3b 20 0a 20  SE, 0, 0, 0); . 
324d0 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   if( yygotominor
324d1 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29 7b 0a  .yy346.pExpr ){.
324d2 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
324d3 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 61 66 66  yy346.pExpr->aff
324d4 69 6e 69 74 79 20 3d 20 4f 45 5f 49 67 6e 6f 72  inity = OE_Ignor
324d5 65 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d  e;.  }.  yygotom
324d6 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72  inor.yy346.zStar
324d7 74 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  t = yymsp[-3].mi
324d8 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67  nor.yy0.z;.  yyg
324d9 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  otominor.yy346.z
324da 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e  End = &yymsp[0].
324db 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73  minor.yy0.z[yyms
324dc 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e  p[0].minor.yy0.n
324dd 5d 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ];.}.        bre
324de 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
324df 39 36 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  96: /* expr ::= 
324e0 52 41 49 53 45 20 4c 50 20 72 61 69 73 65 74 79  RAISE LP raisety
324e1 70 65 20 43 4f 4d 4d 41 20 6e 6d 20 52 50 20 2a  pe COMMA nm RP *
324e2 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
324e3 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20  r.yy346.pExpr = 
324e4 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
324e5 72 73 65 2c 20 54 4b 5f 52 41 49 53 45 2c 20 30  rse, TK_RAISE, 0
324e6 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e  , 0, &yymsp[-1].
324e7 6d 69 6e 6f 72 2e 79 79 30 29 3b 20 0a 20 20 69  minor.yy0); .  i
324e8 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
324e9 79 33 34 36 2e 70 45 78 70 72 20 29 20 7b 0a 20  y346.pExpr ) {. 
324ea 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
324eb 79 33 34 36 2e 70 45 78 70 72 2d 3e 61 66 66 69  y346.pExpr->affi
324ec 6e 69 74 79 20 3d 20 28 63 68 61 72 29 79 79 6d  nity = (char)yym
324ed 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-3].minor.yy3
324ee 32 38 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f  28;.  }.  yygoto
324ef 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61  minor.yy346.zSta
324f0 72 74 20 3d 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d  rt = yymsp[-5].m
324f1 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79  inor.yy0.z;.  yy
324f2 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
324f3 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d  zEnd = &yymsp[0]
324f4 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d  .minor.yy0.z[yym
324f5 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
324f6 6e 5d 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  n];.}.        br
324f7 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
324f8 32 39 37 3a 20 2f 2a 20 72 61 69 73 65 74 79 70  297: /* raisetyp
324f9 65 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 2a  e ::= ROLLBACK *
324fa 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
324fb 79 33 32 38 20 3d 20 4f 45 5f 52 6f 6c 6c 62 61  y328 = OE_Rollba
324fc 63 6b 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ck;}.        bre
324fd 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
324fe 39 39 3a 20 2f 2a 20 72 61 69 73 65 74 79 70 65  99: /* raisetype
324ff 20 3a 3a 3d 20 46 41 49 4c 20 2a 2f 0a 7b 79 79   ::= FAIL */.{yy
32500 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20  gotominor.yy328 
32501 3d 20 4f 45 5f 46 61 69 6c 3b 7d 0a 20 20 20 20  = OE_Fail;}.    
32502 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32503 20 63 61 73 65 20 33 30 30 3a 20 2f 2a 20 63 6d   case 300: /* cm
32504 64 20 3a 3a 3d 20 44 52 4f 50 20 54 52 49 47 47  d ::= DROP TRIGG
32505 45 52 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c  ER ifexists full
32506 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  name */.{.  sqli
32507 74 65 33 44 72 6f 70 54 72 69 67 67 65 72 28 70  te3DropTrigger(p
32508 50 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d 2e 6d  Parse,yymsp[0].m
32509 69 6e 6f 72 2e 79 79 36 35 2c 79 79 6d 73 70 5b  inor.yy65,yymsp[
3250a 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29  -1].minor.yy328)
3250b 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
3250c 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 30  k;.      case 30
3250d 31 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41 54  1: /* cmd ::= AT
3250e 54 41 43 48 20 64 61 74 61 62 61 73 65 5f 6b 77  TACH database_kw
3250f 5f 6f 70 74 20 65 78 70 72 20 41 53 20 65 78 70  _opt expr AS exp
32510 72 20 6b 65 79 5f 6f 70 74 20 2a 2f 0a 7b 0a 20  r key_opt */.{. 
32511 20 73 71 6c 69 74 65 33 41 74 74 61 63 68 28 70   sqlite3Attach(p
32512 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 33 5d  Parse, yymsp[-3]
32513 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  .minor.yy346.pEx
32514 70 72 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  pr, yymsp[-1].mi
32515 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c  nor.yy346.pExpr,
32516 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
32517 79 79 31 33 32 29 3b 0a 7d 0a 20 20 20 20 20 20  yy132);.}.      
32518 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
32519 61 73 65 20 33 30 32 3a 20 2f 2a 20 63 6d 64 20  ase 302: /* cmd 
3251a 3a 3a 3d 20 44 45 54 41 43 48 20 64 61 74 61 62  ::= DETACH datab
3251b 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72 20  ase_kw_opt expr 
3251c 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 44 65  */.{.  sqlite3De
3251d 74 61 63 68 28 70 50 61 72 73 65 2c 20 79 79 6d  tach(pParse, yym
3251e 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  sp[0].minor.yy34
3251f 36 2e 70 45 78 70 72 29 3b 0a 7d 0a 20 20 20 20  6.pExpr);.}.    
32520 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32521 20 63 61 73 65 20 33 30 37 3a 20 2f 2a 20 63 6d   case 307: /* cm
32522 64 20 3a 3a 3d 20 52 45 49 4e 44 45 58 20 2a 2f  d ::= REINDEX */
32523 0a 7b 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78  .{sqlite3Reindex
32524 28 70 50 61 72 73 65 2c 20 30 2c 20 30 29 3b 7d  (pParse, 0, 0);}
32525 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32526 20 20 20 20 20 20 63 61 73 65 20 33 30 38 3a 20        case 308: 
32527 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44  /* cmd ::= REIND
32528 45 58 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 73  EX nm dbnm */.{s
32529 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28 70 50  qlite3Reindex(pP
3252a 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 31 5d  arse, &yymsp[-1]
3252b 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d  .minor.yy0, &yym
3252c 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
3252d 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3252e 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 39  ;.      case 309
3252f 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41 4e 41  : /* cmd ::= ANA
32530 4c 59 5a 45 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  LYZE */.{sqlite3
32531 41 6e 61 6c 79 7a 65 28 70 50 61 72 73 65 2c 20  Analyze(pParse, 
32532 30 2c 20 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  0, 0);}.        
32533 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32534 65 20 33 31 30 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 310: /* cmd ::
32535 3d 20 41 4e 41 4c 59 5a 45 20 6e 6d 20 64 62 6e  = ANALYZE nm dbn
32536 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 6e 61  m */.{sqlite3Ana
32537 6c 79 7a 65 28 70 50 61 72 73 65 2c 20 26 79 79  lyze(pParse, &yy
32538 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
32539 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0, &yymsp[0].min
3253a 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
3253b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3253c 61 73 65 20 33 31 31 3a 20 2f 2a 20 63 6d 64 20  ase 311: /* cmd 
3253d 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20  ::= ALTER TABLE 
3253e 66 75 6c 6c 6e 61 6d 65 20 52 45 4e 41 4d 45 20  fullname RENAME 
3253f 54 4f 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c  TO nm */.{.  sql
32540 69 74 65 33 41 6c 74 65 72 52 65 6e 61 6d 65 54  ite3AlterRenameT
32541 61 62 6c 65 28 70 50 61 72 73 65 2c 79 79 6d 73  able(pParse,yyms
32542 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35  p[-3].minor.yy65
32543 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
32544 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  .yy0);.}.       
32545 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32546 73 65 20 33 31 32 3a 20 2f 2a 20 63 6d 64 20 3a  se 312: /* cmd :
32547 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20 61  := ALTER TABLE a
32548 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61  dd_column_fullna
32549 6d 65 20 41 44 44 20 6b 77 63 6f 6c 75 6d 6e 5f  me ADD kwcolumn_
3254a 6f 70 74 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 7b 0a  opt column */.{.
3254b 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 69    sqlite3AlterFi
3254c 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 70 50  nishAddColumn(pP
3254d 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  arse, &yymsp[0].
3254e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20  minor.yy0);.}.  
3254f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32550 20 20 20 63 61 73 65 20 33 31 33 3a 20 2f 2a 20     case 313: /* 
32551 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e  add_column_fulln
32552 61 6d 65 20 3a 3a 3d 20 66 75 6c 6c 6e 61 6d 65  ame ::= fullname
32553 20 2a 2f 0a 7b 0a 20 20 70 50 61 72 73 65 2d 3e   */.{.  pParse->
32554 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45  db->lookaside.bE
32555 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 20 20 73 71  nabled = 0;.  sq
32556 6c 69 74 65 33 41 6c 74 65 72 42 65 67 69 6e 41  lite3AlterBeginA
32557 64 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c  ddColumn(pParse,
32558 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
32559 79 79 36 35 29 3b 0a 7d 0a 20 20 20 20 20 20 20  yy65);.}.       
3255a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3255b 73 65 20 33 31 36 3a 20 2f 2a 20 63 6d 64 20 3a  se 316: /* cmd :
3255c 3a 3d 20 63 72 65 61 74 65 5f 76 74 61 62 20 2a  := create_vtab *
3255d 2f 0a 7b 73 71 6c 69 74 65 33 56 74 61 62 46 69  /.{sqlite3VtabFi
3255e 6e 69 73 68 50 61 72 73 65 28 70 50 61 72 73 65  nishParse(pParse
3255f 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ,0);}.        br
32560 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32561 33 31 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  317: /* cmd ::= 
32562 63 72 65 61 74 65 5f 76 74 61 62 20 4c 50 20 76  create_vtab LP v
32563 74 61 62 61 72 67 6c 69 73 74 20 52 50 20 2a 2f  tabarglist RP */
32564 0a 7b 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e  .{sqlite3VtabFin
32565 69 73 68 50 61 72 73 65 28 70 50 61 72 73 65 2c  ishParse(pParse,
32566 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
32567 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  yy0);}.        b
32568 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32569 20 33 31 38 3a 20 2f 2a 20 63 72 65 61 74 65 5f   318: /* create_
3256a 76 74 61 62 20 3a 3a 3d 20 63 72 65 61 74 65 6b  vtab ::= createk
3256b 77 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20  w VIRTUAL TABLE 
3256c 6e 6d 20 64 62 6e 6d 20 55 53 49 4e 47 20 6e 6d  nm dbnm USING nm
3256d 20 2a 2f 0a 7b 0a 20 20 20 20 73 71 6c 69 74 65   */.{.    sqlite
3256e 33 56 74 61 62 42 65 67 69 6e 50 61 72 73 65 28  3VtabBeginParse(
3256f 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d  pParse, &yymsp[-
32570 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79  3].minor.yy0, &y
32571 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
32572 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  y0, &yymsp[0].mi
32573 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20  nor.yy0);.}.    
32574 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32575 20 63 61 73 65 20 33 32 31 3a 20 2f 2a 20 76 74   case 321: /* vt
32576 61 62 61 72 67 20 3a 3a 3d 20 2a 2f 0a 7b 73 71  abarg ::= */.{sq
32577 6c 69 74 65 33 56 74 61 62 41 72 67 49 6e 69 74  lite3VtabArgInit
32578 28 70 50 61 72 73 65 29 3b 7d 0a 20 20 20 20 20  (pParse);}.     
32579 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3257a 63 61 73 65 20 33 32 33 3a 20 2f 2a 20 76 74 61  case 323: /* vta
3257b 62 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20 41 4e  bargtoken ::= AN
3257c 59 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  Y */.      case 
3257d 33 32 34 3a 20 2f 2a 20 76 74 61 62 61 72 67 74  324: /* vtabargt
3257e 6f 6b 65 6e 20 3a 3a 3d 20 6c 70 20 61 6e 79 6c  oken ::= lp anyl
3257f 69 73 74 20 52 50 20 2a 2f 20 79 79 74 65 73 74  ist RP */ yytest
32580 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33  case(yyruleno==3
32581 32 34 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  24);.      case 
32582 33 32 35 3a 20 2f 2a 20 6c 70 20 3a 3a 3d 20 4c  325: /* lp ::= L
32583 50 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  P */ yytestcase(
32584 79 79 72 75 6c 65 6e 6f 3d 3d 33 32 35 29 3b 0a  yyruleno==325);.
32585 7b 73 71 6c 69 74 65 33 56 74 61 62 41 72 67 45  {sqlite3VtabArgE
32586 78 74 65 6e 64 28 70 50 61 72 73 65 2c 26 79 79  xtend(pParse,&yy
32587 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
32588 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
32589 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74  k;.      default
3258a 3a 0a 20 20 20 20 20 20 2f 2a 20 28 30 29 20 69  :.      /* (0) i
3258b 6e 70 75 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74  nput ::= cmdlist
3258c 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3258d 79 72 75 6c 65 6e 6f 3d 3d 30 29 3b 0a 20 20 20  yruleno==0);.   
3258e 20 20 20 2f 2a 20 28 31 29 20 63 6d 64 6c 69 73     /* (1) cmdlis
3258f 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74 20 65 63  t ::= cmdlist ec
32590 6d 64 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  md */ yytestcase
32591 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 29 3b 0a 20  (yyruleno==1);. 
32592 20 20 20 20 20 2f 2a 20 28 32 29 20 63 6d 64 6c       /* (2) cmdl
32593 69 73 74 20 3a 3a 3d 20 65 63 6d 64 20 2a 2f 20  ist ::= ecmd */ 
32594 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32595 65 6e 6f 3d 3d 32 29 3b 0a 20 20 20 20 20 20 2f  eno==2);.      /
32596 2a 20 28 33 29 20 65 63 6d 64 20 3a 3a 3d 20 53  * (3) ecmd ::= S
32597 45 4d 49 20 2a 2f 20 79 79 74 65 73 74 63 61 73  EMI */ yytestcas
32598 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 29 3b 0a  e(yyruleno==3);.
32599 20 20 20 20 20 20 2f 2a 20 28 34 29 20 65 63 6d        /* (4) ecm
3259a 64 20 3a 3a 3d 20 65 78 70 6c 61 69 6e 20 63 6d  d ::= explain cm
3259b 64 78 20 53 45 4d 49 20 2a 2f 20 79 79 74 65 73  dx SEMI */ yytes
3259c 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3259d 34 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 31 30  4);.      /* (10
3259e 29 20 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20  ) trans_opt ::= 
3259f 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
325a0 72 75 6c 65 6e 6f 3d 3d 31 30 29 3b 0a 20 20 20  ruleno==10);.   
325a1 20 20 20 2f 2a 20 28 31 31 29 20 74 72 61 6e 73     /* (11) trans
325a2 5f 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43  _opt ::= TRANSAC
325a3 54 49 4f 4e 20 2a 2f 20 79 79 74 65 73 74 63 61  TION */ yytestca
325a4 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 31 29  se(yyruleno==11)
325a5 3b 0a 20 20 20 20 20 20 2f 2a 20 28 31 32 29 20  ;.      /* (12) 
325a6 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54 52  trans_opt ::= TR
325a7 41 4e 53 41 43 54 49 4f 4e 20 6e 6d 20 2a 2f 20  ANSACTION nm */ 
325a8 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
325a9 65 6e 6f 3d 3d 31 32 29 3b 0a 20 20 20 20 20 20  eno==12);.      
325aa 2f 2a 20 28 32 30 29 20 73 61 76 65 70 6f 69 6e  /* (20) savepoin
325ab 74 5f 6f 70 74 20 3a 3a 3d 20 53 41 56 45 50 4f  t_opt ::= SAVEPO
325ac 49 4e 54 20 2a 2f 20 79 79 74 65 73 74 63 61 73  INT */ yytestcas
325ad 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 29 3b  e(yyruleno==20);
325ae 0a 20 20 20 20 20 20 2f 2a 20 28 32 31 29 20 73  .      /* (21) s
325af 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 3a 3a 3d  avepoint_opt ::=
325b0 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
325b1 79 72 75 6c 65 6e 6f 3d 3d 32 31 29 3b 0a 20 20  yruleno==21);.  
325b2 20 20 20 20 2f 2a 20 28 32 35 29 20 63 6d 64 20      /* (25) cmd 
325b3 3a 3a 3d 20 63 72 65 61 74 65 5f 74 61 62 6c 65  ::= create_table
325b4 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72   create_table_ar
325b5 67 73 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  gs */ yytestcase
325b6 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 35 29 3b 0a  (yyruleno==25);.
325b7 20 20 20 20 20 20 2f 2a 20 28 33 34 29 20 63 6f        /* (34) co
325b8 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c  lumnlist ::= col
325b9 75 6d 6e 6c 69 73 74 20 43 4f 4d 4d 41 20 63 6f  umnlist COMMA co
325ba 6c 75 6d 6e 20 2a 2f 20 79 79 74 65 73 74 63 61  lumn */ yytestca
325bb 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 34 29  se(yyruleno==34)
325bc 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 35 29 20  ;.      /* (35) 
325bd 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20 63  columnlist ::= c
325be 6f 6c 75 6d 6e 20 2a 2f 20 79 79 74 65 73 74 63  olumn */ yytestc
325bf 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 35  ase(yyruleno==35
325c0 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 34 34 29  );.      /* (44)
325c1 20 74 79 70 65 20 3a 3a 3d 20 2a 2f 20 79 79 74   type ::= */ yyt
325c2 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
325c3 3d 3d 34 34 29 3b 0a 20 20 20 20 20 20 2f 2a 20  ==44);.      /* 
325c4 28 35 31 29 20 73 69 67 6e 65 64 20 3a 3a 3d 20  (51) signed ::= 
325c5 70 6c 75 73 5f 6e 75 6d 20 2a 2f 20 79 79 74 65  plus_num */ yyte
325c6 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
325c7 3d 35 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  =51);.      /* (
325c8 35 32 29 20 73 69 67 6e 65 64 20 3a 3a 3d 20 6d  52) signed ::= m
325c9 69 6e 75 73 5f 6e 75 6d 20 2a 2f 20 79 79 74 65  inus_num */ yyte
325ca 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
325cb 3d 35 32 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  =52);.      /* (
325cc 35 33 29 20 63 61 72 67 6c 69 73 74 20 3a 3a 3d  53) carglist ::=
325cd 20 63 61 72 67 6c 69 73 74 20 63 61 72 67 20 2a   carglist carg *
325ce 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
325cf 75 6c 65 6e 6f 3d 3d 35 33 29 3b 0a 20 20 20 20  uleno==53);.    
325d0 20 20 2f 2a 20 28 35 34 29 20 63 61 72 67 6c 69    /* (54) cargli
325d1 73 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  st ::= */ yytest
325d2 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 35  case(yyruleno==5
325d3 34 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 35 35  4);.      /* (55
325d4 29 20 63 61 72 67 20 3a 3a 3d 20 43 4f 4e 53 54  ) carg ::= CONST
325d5 52 41 49 4e 54 20 6e 6d 20 63 63 6f 6e 73 20 2a  RAINT nm ccons *
325d6 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
325d7 75 6c 65 6e 6f 3d 3d 35 35 29 3b 0a 20 20 20 20  uleno==55);.    
325d8 20 20 2f 2a 20 28 35 36 29 20 63 61 72 67 20 3a    /* (56) carg :
325d9 3a 3d 20 63 63 6f 6e 73 20 2a 2f 20 79 79 74 65  := ccons */ yyte
325da 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
325db 3d 35 36 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  =56);.      /* (
325dc 36 32 29 20 63 63 6f 6e 73 20 3a 3a 3d 20 4e 55  62) ccons ::= NU
325dd 4c 4c 20 6f 6e 63 6f 6e 66 20 2a 2f 20 79 79 74  LL onconf */ yyt
325de 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
325df 3d 3d 36 32 29 3b 0a 20 20 20 20 20 20 2f 2a 20  ==62);.      /* 
325e0 28 38 39 29 20 63 6f 6e 73 6c 69 73 74 20 3a 3a  (89) conslist ::
325e1 3d 20 63 6f 6e 73 6c 69 73 74 20 43 4f 4d 4d 41  = conslist COMMA
325e2 20 74 63 6f 6e 73 20 2a 2f 20 79 79 74 65 73 74   tcons */ yytest
325e3 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 38  case(yyruleno==8
325e4 39 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 39 30  9);.      /* (90
325e5 29 20 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63  ) conslist ::= c
325e6 6f 6e 73 6c 69 73 74 20 74 63 6f 6e 73 20 2a 2f  onslist tcons */
325e7 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
325e8 6c 65 6e 6f 3d 3d 39 30 29 3b 0a 20 20 20 20 20  leno==90);.     
325e9 20 2f 2a 20 28 39 31 29 20 63 6f 6e 73 6c 69 73   /* (91) conslis
325ea 74 20 3a 3a 3d 20 74 63 6f 6e 73 20 2a 2f 20 79  t ::= tcons */ y
325eb 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
325ec 6e 6f 3d 3d 39 31 29 3b 0a 20 20 20 20 20 20 2f  no==91);.      /
325ed 2a 20 28 39 32 29 20 74 63 6f 6e 73 20 3a 3a 3d  * (92) tcons ::=
325ee 20 43 4f 4e 53 54 52 41 49 4e 54 20 6e 6d 20 2a   CONSTRAINT nm *
325ef 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
325f0 75 6c 65 6e 6f 3d 3d 39 32 29 3b 0a 20 20 20 20  uleno==92);.    
325f1 20 20 2f 2a 20 28 32 36 38 29 20 70 6c 75 73 5f    /* (268) plus_
325f2 6f 70 74 20 3a 3a 3d 20 50 4c 55 53 20 2a 2f 20  opt ::= PLUS */ 
325f3 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
325f4 65 6e 6f 3d 3d 32 36 38 29 3b 0a 20 20 20 20 20  eno==268);.     
325f5 20 2f 2a 20 28 32 36 39 29 20 70 6c 75 73 5f 6f   /* (269) plus_o
325f6 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  pt ::= */ yytest
325f7 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
325f8 36 39 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32  69);.      /* (2
325f9 37 39 29 20 66 6f 72 65 61 63 68 5f 63 6c 61 75  79) foreach_clau
325fa 73 65 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  se ::= */ yytest
325fb 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
325fc 37 39 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32  79);.      /* (2
325fd 38 30 29 20 66 6f 72 65 61 63 68 5f 63 6c 61 75  80) foreach_clau
325fe 73 65 20 3a 3a 3d 20 46 4f 52 20 45 41 43 48 20  se ::= FOR EACH 
325ff 52 4f 57 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ROW */ yytestcas
32600 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 38 30 29  e(yyruleno==280)
32601 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32 38 37 29  ;.      /* (287)
32602 20 74 72 69 64 78 62 79 20 3a 3a 3d 20 2a 2f 20   tridxby ::= */ 
32603 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32604 65 6e 6f 3d 3d 32 38 37 29 3b 0a 20 20 20 20 20  eno==287);.     
32605 20 2f 2a 20 28 33 30 35 29 20 64 61 74 61 62 61   /* (305) databa
32606 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20 44 41  se_kw_opt ::= DA
32607 54 41 42 41 53 45 20 2a 2f 20 79 79 74 65 73 74  TABASE */ yytest
32608 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33  case(yyruleno==3
32609 30 35 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33  05);.      /* (3
3260a 30 36 29 20 64 61 74 61 62 61 73 65 5f 6b 77 5f  06) database_kw_
3260b 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  opt ::= */ yytes
3260c 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3260d 33 30 36 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  306);.      /* (
3260e 33 31 34 29 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70  314) kwcolumn_op
3260f 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  t ::= */ yytestc
32610 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 31  ase(yyruleno==31
32611 34 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 31  4);.      /* (31
32612 35 29 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20  5) kwcolumn_opt 
32613 3a 3a 3d 20 43 4f 4c 55 4d 4e 4b 57 20 2a 2f 20  ::= COLUMNKW */ 
32614 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32615 65 6e 6f 3d 3d 33 31 35 29 3b 0a 20 20 20 20 20  eno==315);.     
32616 20 2f 2a 20 28 33 31 39 29 20 76 74 61 62 61 72   /* (319) vtabar
32617 67 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61 72  glist ::= vtabar
32618 67 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  g */ yytestcase(
32619 79 79 72 75 6c 65 6e 6f 3d 3d 33 31 39 29 3b 0a  yyruleno==319);.
3261a 20 20 20 20 20 20 2f 2a 20 28 33 32 30 29 20 76        /* (320) v
3261b 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76  tabarglist ::= v
3261c 74 61 62 61 72 67 6c 69 73 74 20 43 4f 4d 4d 41  tabarglist COMMA
3261d 20 76 74 61 62 61 72 67 20 2a 2f 20 79 79 74 65   vtabarg */ yyte
3261e 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3261f 3d 33 32 30 29 3b 0a 20 20 20 20 20 20 2f 2a 20  =320);.      /* 
32620 28 33 32 32 29 20 76 74 61 62 61 72 67 20 3a 3a  (322) vtabarg ::
32621 3d 20 76 74 61 62 61 72 67 20 76 74 61 62 61 72  = vtabarg vtabar
32622 67 74 6f 6b 65 6e 20 2a 2f 20 79 79 74 65 73 74  gtoken */ yytest
32623 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33  case(yyruleno==3
32624 32 32 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33  22);.      /* (3
32625 32 36 29 20 61 6e 79 6c 69 73 74 20 3a 3a 3d 20  26) anylist ::= 
32626 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
32627 72 75 6c 65 6e 6f 3d 3d 33 32 36 29 3b 0a 20 20  ruleno==326);.  
32628 20 20 20 20 2f 2a 20 28 33 32 37 29 20 61 6e 79      /* (327) any
32629 6c 69 73 74 20 3a 3a 3d 20 61 6e 79 6c 69 73 74  list ::= anylist
3262a 20 4c 50 20 61 6e 79 6c 69 73 74 20 52 50 20 2a   LP anylist RP *
3262b 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3262c 75 6c 65 6e 6f 3d 3d 33 32 37 29 3b 0a 20 20 20  uleno==327);.   
3262d 20 20 20 2f 2a 20 28 33 32 38 29 20 61 6e 79 6c     /* (328) anyl
3262e 69 73 74 20 3a 3a 3d 20 61 6e 79 6c 69 73 74 20  ist ::= anylist 
3262f 41 4e 59 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ANY */ yytestcas
32630 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32 38 29  e(yyruleno==328)
32631 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
32632 0a 20 20 7d 3b 0a 20 20 79 79 67 6f 74 6f 20 3d  .  };.  yygoto =
32633 20 79 79 52 75 6c 65 49 6e 66 6f 5b 79 79 72 75   yyRuleInfo[yyru
32634 6c 65 6e 6f 5d 2e 6c 68 73 3b 0a 20 20 79 79 73  leno].lhs;.  yys
32635 69 7a 65 20 3d 20 79 79 52 75 6c 65 49 6e 66 6f  ize = yyRuleInfo
32636 5b 79 79 72 75 6c 65 6e 6f 5d 2e 6e 72 68 73 3b  [yyruleno].nrhs;
32637 0a 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  .  yypParser->yy
32638 69 64 78 20 2d 3d 20 79 79 73 69 7a 65 3b 0a 20  idx -= yysize;. 
32639 20 79 79 61 63 74 20 3d 20 79 79 5f 66 69 6e 64   yyact = yy_find
3263a 5f 72 65 64 75 63 65 5f 61 63 74 69 6f 6e 28 79  _reduce_action(y
3263b 79 6d 73 70 5b 2d 79 79 73 69 7a 65 5d 2e 73 74  ymsp[-yysize].st
3263c 61 74 65 6e 6f 2c 28 59 59 43 4f 44 45 54 59 50  ateno,(YYCODETYP
3263d 45 29 79 79 67 6f 74 6f 29 3b 0a 20 20 69 66 28  E)yygoto);.  if(
3263e 20 79 79 61 63 74 20 3c 20 59 59 4e 53 54 41 54   yyact < YYNSTAT
3263f 45 20 29 7b 0a 23 69 66 64 65 66 20 4e 44 45 42  E ){.#ifdef NDEB
32640 55 47 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20  UG.    /* If we 
32641 61 72 65 20 6e 6f 74 20 64 65 62 75 67 67 69 6e  are not debuggin
32642 67 20 61 6e 64 20 74 68 65 20 72 65 64 75 63 65  g and the reduce
32643 20 61 63 74 69 6f 6e 20 70 6f 70 70 65 64 20 61   action popped a
32644 74 20 6c 65 61 73 74 0a 20 20 20 20 2a 2a 20 6f  t least.    ** o
32645 6e 65 20 65 6c 65 6d 65 6e 74 20 6f 66 66 20 74  ne element off t
32646 68 65 20 73 74 61 63 6b 2c 20 74 68 65 6e 20 77  he stack, then w
32647 65 20 63 61 6e 20 70 75 73 68 20 74 68 65 20 6e  e can push the n
32648 65 77 20 65 6c 65 6d 65 6e 74 20 62 61 63 6b 0a  ew element back.
32649 20 20 20 20 2a 2a 20 6f 6e 74 6f 20 74 68 65 20      ** onto the 
3264a 73 74 61 63 6b 20 68 65 72 65 2c 20 61 6e 64 20  stack here, and 
3264b 73 6b 69 70 20 74 68 65 20 73 74 61 63 6b 20 6f  skip the stack o
3264c 76 65 72 66 6c 6f 77 20 74 65 73 74 20 69 6e 20  verflow test in 
3264d 79 79 5f 73 68 69 66 74 28 29 2e 0a 20 20 20 20  yy_shift()..    
3264e 2a 2a 20 54 68 61 74 20 67 69 76 65 73 20 61 20  ** That gives a 
3264f 73 69 67 6e 69 66 69 63 61 6e 74 20 73 70 65 65  significant spee
32650 64 20 69 6d 70 72 6f 76 65 6d 65 6e 74 2e 20 2a  d improvement. *
32651 2f 0a 20 20 20 20 69 66 28 20 79 79 73 69 7a 65  /.    if( yysize
32652 20 29 7b 0a 20 20 20 20 20 20 79 79 70 50 61 72   ){.      yypPar
32653 73 65 72 2d 3e 79 79 69 64 78 2b 2b 3b 0a 20 20  ser->yyidx++;.  
32654 20 20 20 20 79 79 6d 73 70 20 2d 3d 20 79 79 73      yymsp -= yys
32655 69 7a 65 2d 31 3b 0a 20 20 20 20 20 20 79 79 6d  ize-1;.      yym
32656 73 70 2d 3e 73 74 61 74 65 6e 6f 20 3d 20 28 59  sp->stateno = (Y
32657 59 41 43 54 49 4f 4e 54 59 50 45 29 79 79 61 63  YACTIONTYPE)yyac
32658 74 3b 0a 20 20 20 20 20 20 79 79 6d 73 70 2d 3e  t;.      yymsp->
32659 6d 61 6a 6f 72 20 3d 20 28 59 59 43 4f 44 45 54  major = (YYCODET
3265a 59 50 45 29 79 79 67 6f 74 6f 3b 0a 20 20 20 20  YPE)yygoto;.    
3265b 20 20 79 79 6d 73 70 2d 3e 6d 69 6e 6f 72 20 3d    yymsp->minor =
3265c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 3b 0a 20 20   yygotominor;.  
3265d 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20    }else.#endif. 
3265e 20 20 20 7b 0a 20 20 20 20 20 20 79 79 5f 73 68     {.      yy_sh
3265f 69 66 74 28 79 79 70 50 61 72 73 65 72 2c 79 79  ift(yypParser,yy
32660 61 63 74 2c 79 79 67 6f 74 6f 2c 26 79 79 67 6f  act,yygoto,&yygo
32661 74 6f 6d 69 6e 6f 72 29 3b 0a 20 20 20 20 7d 0a  tominor);.    }.
32662 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
32663 65 72 74 28 20 79 79 61 63 74 20 3d 3d 20 59 59  ert( yyact == YY
32664 4e 53 54 41 54 45 20 2b 20 59 59 4e 52 55 4c 45  NSTATE + YYNRULE
32665 20 2b 20 31 20 29 3b 0a 20 20 20 20 79 79 5f 61   + 1 );.    yy_a
32666 63 63 65 70 74 28 79 79 70 50 61 72 73 65 72 29  ccept(yypParser)
32667 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
32668 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64  he following cod
32669 65 20 65 78 65 63 75 74 65 73 20 77 68 65 6e 20  e executes when 
3266a 74 68 65 20 70 61 72 73 65 20 66 61 69 6c 73 0a  the parse fails.
3266b 2a 2f 0a 23 69 66 6e 64 65 66 20 59 59 4e 4f 45  */.#ifndef YYNOE
3266c 52 52 4f 52 52 45 43 4f 56 45 52 59 0a 73 74 61  RRORRECOVERY.sta
3266d 74 69 63 20 76 6f 69 64 20 79 79 5f 70 61 72 73  tic void yy_pars
3266e 65 5f 66 61 69 6c 65 64 28 0a 20 20 79 79 50 61  e_failed(.  yyPa
3266f 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72 20  rser *yypParser 
32670 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
32671 20 70 61 72 73 65 72 20 2a 2f 0a 29 7b 0a 20 20   parser */.){.  
32672 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
32673 5f 46 45 54 43 48 3b 0a 23 69 66 6e 64 65 66 20  _FETCH;.#ifndef 
32674 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79 54  NDEBUG.  if( yyT
32675 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20  raceFILE ){.    
32676 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46  fprintf(yyTraceF
32677 49 4c 45 2c 22 25 73 46 61 69 6c 21 5c 6e 22 2c  ILE,"%sFail!\n",
32678 79 79 54 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a  yyTracePrompt);.
32679 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 77 68 69    }.#endif.  whi
3267a 6c 65 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79  le( yypParser->y
3267b 79 69 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70  yidx>=0 ) yy_pop
3267c 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28 79 79  _parser_stack(yy
3267d 70 50 61 72 73 65 72 29 3b 0a 20 20 2f 2a 20 48  pParser);.  /* H
3267e 65 72 65 20 63 6f 64 65 20 69 73 20 69 6e 73 65  ere code is inse
3267f 72 74 65 64 20 77 68 69 63 68 20 77 69 6c 6c 20  rted which will 
32680 62 65 20 65 78 65 63 75 74 65 64 20 77 68 65 6e  be executed when
32681 65 76 65 72 20 74 68 65 0a 20 20 2a 2a 20 70 61  ever the.  ** pa
32682 72 73 65 72 20 66 61 69 6c 73 20 2a 2f 0a 20 20  rser fails */.  
32683 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
32684 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70 72  _STORE; /* Suppr
32685 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f 75  ess warning abou
32686 74 20 75 6e 75 73 65 64 20 25 65 78 74 72 61 5f  t unused %extra_
32687 61 72 67 75 6d 65 6e 74 20 76 61 72 69 61 62 6c  argument variabl
32688 65 20 2a 2f 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  e */.}.#endif /*
32689 20 59 59 4e 4f 45 52 52 4f 52 52 45 43 4f 56 45   YYNOERRORRECOVE
3268a 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  RY */../*.** The
3268b 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20   following code 
3268c 65 78 65 63 75 74 65 73 20 77 68 65 6e 20 61 20  executes when a 
3268d 73 79 6e 74 61 78 20 65 72 72 6f 72 20 66 69 72  syntax error fir
3268e 73 74 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74  st occurs..*/.st
3268f 61 74 69 63 20 76 6f 69 64 20 79 79 5f 73 79 6e  atic void yy_syn
32690 74 61 78 5f 65 72 72 6f 72 28 0a 20 20 79 79 50  tax_error(.  yyP
32691 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72  arser *yypParser
32692 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ,           /* T
32693 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 69  he parser */.  i
32694 6e 74 20 79 79 6d 61 6a 6f 72 2c 20 20 20 20 20  nt yymajor,     
32695 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
32696 20 54 68 65 20 6d 61 6a 6f 72 20 74 79 70 65 20   The major type 
32697 6f 66 20 74 68 65 20 65 72 72 6f 72 20 74 6f 6b  of the error tok
32698 65 6e 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54  en */.  YYMINORT
32699 59 50 45 20 79 79 6d 69 6e 6f 72 20 20 20 20 20  YPE yyminor     
3269a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 69         /* The mi
3269b 6e 6f 72 20 74 79 70 65 20 6f 66 20 74 68 65 20  nor type of the 
3269c 65 72 72 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a 29  error token */.)
3269d 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  {.  sqlite3Parse
3269e 72 41 52 47 5f 46 45 54 43 48 3b 0a 23 64 65 66  rARG_FETCH;.#def
3269f 69 6e 65 20 54 4f 4b 45 4e 20 28 79 79 6d 69 6e  ine TOKEN (yymin
326a0 6f 72 2e 79 79 30 29 0a 0a 20 20 55 4e 55 53 45  or.yy0)..  UNUSE
326a1 44 5f 50 41 52 41 4d 45 54 45 52 28 79 79 6d 61  D_PARAMETER(yyma
326a2 6a 6f 72 29 3b 20 20 2f 2a 20 53 69 6c 65 6e 63  jor);  /* Silenc
326a3 65 20 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 20  e some compiler 
326a4 77 61 72 6e 69 6e 67 73 20 2a 2f 0a 20 20 61 73  warnings */.  as
326a5 73 65 72 74 28 20 54 4f 4b 45 4e 2e 7a 5b 30 5d  sert( TOKEN.z[0]
326a6 20 29 3b 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65   );  /* The toke
326a7 6e 69 7a 65 72 20 61 6c 77 61 79 73 20 67 69 76  nizer always giv
326a8 65 73 20 75 73 20 61 20 74 6f 6b 65 6e 20 2a 2f  es us a token */
326a9 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d  .  sqlite3ErrorM
326aa 73 67 28 70 50 61 72 73 65 2c 20 22 6e 65 61 72  sg(pParse, "near
326ab 20 5c 22 25 54 5c 22 3a 20 73 79 6e 74 61 78 20   \"%T\": syntax 
326ac 65 72 72 6f 72 22 2c 20 26 54 4f 4b 45 4e 29 3b  error", &TOKEN);
326ad 0a 20 20 70 50 61 72 73 65 2d 3e 70 61 72 73 65  .  pParse->parse
326ae 45 72 72 6f 72 20 3d 20 31 3b 0a 20 20 73 71 6c  Error = 1;.  sql
326af 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 54  ite3ParserARG_ST
326b0 4f 52 45 3b 20 2f 2a 20 53 75 70 70 72 65 73 73  ORE; /* Suppress
326b1 20 77 61 72 6e 69 6e 67 20 61 62 6f 75 74 20 75   warning about u
326b2 6e 75 73 65 64 20 25 65 78 74 72 61 5f 61 72 67  nused %extra_arg
326b3 75 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 20 2a  ument variable *
326b4 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  /.}../*.** The f
326b5 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 65 78 65 63  ollowing is exec
326b6 75 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 61  uted when the pa
326b7 72 73 65 72 20 61 63 63 65 70 74 73 0a 2a 2f 0a  rser accepts.*/.
326b8 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 61  static void yy_a
326b9 63 63 65 70 74 28 0a 20 20 79 79 50 61 72 73 65  ccept(.  yyParse
326ba 72 20 2a 79 79 70 50 61 72 73 65 72 20 20 20 20  r *yypParser    
326bb 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
326bc 72 73 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  rser */.){.  sql
326bd 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 46 45  ite3ParserARG_FE
326be 54 43 48 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  TCH;.#ifndef NDE
326bf 42 55 47 0a 20 20 69 66 28 20 79 79 54 72 61 63  BUG.  if( yyTrac
326c0 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 66 70 72  eFILE ){.    fpr
326c1 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
326c2 2c 22 25 73 41 63 63 65 70 74 21 5c 6e 22 2c 79  ,"%sAccept!\n",y
326c3 79 54 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20  yTracePrompt);. 
326c4 20 7d 0a 23 65 6e 64 69 66 0a 20 20 77 68 69 6c   }.#endif.  whil
326c5 65 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  e( yypParser->yy
326c6 69 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f  idx>=0 ) yy_pop_
326c7 70 61 72 73 65 72 5f 73 74 61 63 6b 28 79 79 70  parser_stack(yyp
326c8 50 61 72 73 65 72 29 3b 0a 20 20 2f 2a 20 48 65  Parser);.  /* He
326c9 72 65 20 63 6f 64 65 20 69 73 20 69 6e 73 65 72  re code is inser
326ca 74 65 64 20 77 68 69 63 68 20 77 69 6c 6c 20 62  ted which will b
326cb 65 20 65 78 65 63 75 74 65 64 20 77 68 65 6e 65  e executed whene
326cc 76 65 72 20 74 68 65 0a 20 20 2a 2a 20 70 61 72  ver the.  ** par
326cd 73 65 72 20 61 63 63 65 70 74 73 20 2a 2f 0a 20  ser accepts */. 
326ce 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
326cf 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70  G_STORE; /* Supp
326d0 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f  ress warning abo
326d1 75 74 20 75 6e 75 73 65 64 20 25 65 78 74 72 61  ut unused %extra
326d2 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 69 61 62  _argument variab
326d3 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 20 54 68 65 20  le */.}../* The 
326d4 6d 61 69 6e 20 70 61 72 73 65 72 20 70 72 6f 67  main parser prog
326d5 72 61 6d 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73  ram..** The firs
326d6 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20  t argument is a 
326d7 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72  pointer to a str
326d8 75 63 74 75 72 65 20 6f 62 74 61 69 6e 65 64 20  ucture obtained 
326d9 66 72 6f 6d 0a 2a 2a 20 22 73 71 6c 69 74 65 33  from.** "sqlite3
326da 50 61 72 73 65 72 41 6c 6c 6f 63 22 20 77 68 69  ParserAlloc" whi
326db 63 68 20 64 65 73 63 72 69 62 65 73 20 74 68 65  ch describes the
326dc 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f   current state o
326dd 66 20 74 68 65 20 70 61 72 73 65 72 2e 0a 2a 2a  f the parser..**
326de 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   The second argu
326df 6d 65 6e 74 20 69 73 20 74 68 65 20 6d 61 6a 6f  ment is the majo
326e0 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e 20  r token number. 
326e1 20 54 68 65 20 74 68 69 72 64 20 69 73 0a 2a 2a   The third is.**
326e2 20 74 68 65 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e   the minor token
326e3 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20 6f 70  .  The fourth op
326e4 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 20  tional argument 
326e5 69 73 20 77 68 61 74 65 76 65 72 20 74 68 65 0a  is whatever the.
326e6 2a 2a 20 75 73 65 72 20 77 61 6e 74 73 20 28 61  ** user wants (a
326e7 6e 64 20 73 70 65 63 69 66 69 65 64 20 69 6e 20  nd specified in 
326e8 74 68 65 20 67 72 61 6d 6d 61 72 29 20 61 6e 64  the grammar) and
326e9 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 66 6f   is available fo
326ea 72 0a 2a 2a 20 75 73 65 20 62 79 20 74 68 65 20  r.** use by the 
326eb 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e  action routines.
326ec 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a  .**.** Inputs:.*
326ed 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 41  * <ul>.** <li> A
326ee 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
326ef 70 61 72 73 65 72 20 28 61 6e 20 6f 70 61 71 75  parser (an opaqu
326f0 65 20 73 74 72 75 63 74 75 72 65 2e 29 0a 2a 2a  e structure.).**
326f1 20 3c 6c 69 3e 20 54 68 65 20 6d 61 6a 6f 72 20   <li> The major 
326f2 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a  token number..**
326f3 20 3c 6c 69 3e 20 54 68 65 20 6d 69 6e 6f 72 20   <li> The minor 
326f4 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a  token number..**
326f5 20 3c 6c 69 3e 20 41 6e 20 6f 70 74 69 6f 6e 20   <li> An option 
326f6 61 72 67 75 6d 65 6e 74 20 6f 66 20 61 20 67 72  argument of a gr
326f7 61 6d 6d 61 72 2d 73 70 65 63 69 66 69 65 64 20  ammar-specified 
326f8 74 79 70 65 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a  type..** </ul>.*
326f9 2a 0a 2a 2a 20 4f 75 74 70 75 74 73 3a 0a 2a 2a  *.** Outputs:.**
326fa 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   None..*/.SQLITE
326fb 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
326fc 6c 69 74 65 33 50 61 72 73 65 72 28 0a 20 20 76  lite3Parser(.  v
326fd 6f 69 64 20 2a 79 79 70 2c 20 20 20 20 20 20 20  oid *yyp,       
326fe 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
326ff 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 69  he parser */.  i
32700 6e 74 20 79 79 6d 61 6a 6f 72 2c 20 20 20 20 20  nt yymajor,     
32701 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
32702 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 63  he major token c
32703 6f 64 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  ode number */.  
32704 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b  sqlite3ParserTOK
32705 45 4e 54 59 50 45 20 79 79 6d 69 6e 6f 72 20 20  ENTYPE yyminor  
32706 20 20 20 20 20 2f 2a 20 54 68 65 20 76 61 6c 75       /* The valu
32707 65 20 66 6f 72 20 74 68 65 20 74 6f 6b 65 6e 20  e for the token 
32708 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73  */.  sqlite3Pars
32709 65 72 41 52 47 5f 50 44 45 43 4c 20 20 20 20 20  erARG_PDECL     
3270a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 74            /* Opt
3270b 69 6f 6e 61 6c 20 25 65 78 74 72 61 5f 61 72 67  ional %extra_arg
3270c 75 6d 65 6e 74 20 70 61 72 61 6d 65 74 65 72 20  ument parameter 
3270d 2a 2f 0a 29 7b 0a 20 20 59 59 4d 49 4e 4f 52 54  */.){.  YYMINORT
3270e 59 50 45 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e  YPE yyminorunion
3270f 3b 0a 20 20 69 6e 74 20 79 79 61 63 74 3b 20 20  ;.  int yyact;  
32710 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
32711 20 70 61 72 73 65 72 20 61 63 74 69 6f 6e 2e 20   parser action. 
32712 2a 2f 0a 20 20 69 6e 74 20 79 79 65 6e 64 6f 66  */.  int yyendof
32713 69 6e 70 75 74 3b 20 20 20 20 20 2f 2a 20 54 72  input;     /* Tr
32714 75 65 20 69 66 20 77 65 20 61 72 65 20 61 74 20  ue if we are at 
32715 74 68 65 20 65 6e 64 20 6f 66 20 69 6e 70 75 74  the end of input
32716 20 2a 2f 0a 23 69 66 64 65 66 20 59 59 45 52 52   */.#ifdef YYERR
32717 4f 52 53 59 4d 42 4f 4c 0a 20 20 69 6e 74 20 79  ORSYMBOL.  int y
32718 79 65 72 72 6f 72 68 69 74 20 3d 20 30 3b 20 20  yerrorhit = 0;  
32719 20 2f 2a 20 54 72 75 65 20 69 66 20 79 79 6d 61   /* True if yyma
3271a 6a 6f 72 20 68 61 73 20 69 6e 76 6f 6b 65 64 20  jor has invoked 
3271b 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 23 65 6e 64  an error */.#end
3271c 69 66 0a 20 20 79 79 50 61 72 73 65 72 20 2a 79  if.  yyParser *y
3271d 79 70 50 61 72 73 65 72 3b 20 20 2f 2a 20 54 68  ypParser;  /* Th
3271e 65 20 70 61 72 73 65 72 20 2a 2f 0a 0a 20 20 2f  e parser */..  /
3271f 2a 20 28 72 65 29 69 6e 69 74 69 61 6c 69 7a 65  * (re)initialize
32720 20 74 68 65 20 70 61 72 73 65 72 2c 20 69 66 20   the parser, if 
32721 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a 20 20 79  necessary */.  y
32722 79 70 50 61 72 73 65 72 20 3d 20 28 79 79 50 61  ypParser = (yyPa
32723 72 73 65 72 2a 29 79 79 70 3b 0a 20 20 69 66 28  rser*)yyp;.  if(
32724 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64   yypParser->yyid
32725 78 3c 30 20 29 7b 0a 23 69 66 20 59 59 53 54 41  x<0 ){.#if YYSTA
32726 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20 20 20 69  CKDEPTH<=0.    i
32727 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  f( yypParser->yy
32728 73 74 6b 73 7a 20 3c 3d 30 20 29 7b 0a 20 20 20  stksz <=0 ){.   
32729 20 20 20 2f 2a 6d 65 6d 73 65 74 28 26 79 79 6d     /*memset(&yym
3272a 69 6e 6f 72 75 6e 69 6f 6e 2c 20 30 2c 20 73 69  inorunion, 0, si
3272b 7a 65 6f 66 28 79 79 6d 69 6e 6f 72 75 6e 69 6f  zeof(yyminorunio
3272c 6e 29 29 3b 2a 2f 0a 20 20 20 20 20 20 79 79 6d  n));*/.      yym
3272d 69 6e 6f 72 75 6e 69 6f 6e 20 3d 20 79 79 7a 65  inorunion = yyze
3272e 72 6f 6d 69 6e 6f 72 3b 0a 20 20 20 20 20 20 79  rominor;.      y
3272f 79 53 74 61 63 6b 4f 76 65 72 66 6c 6f 77 28 79  yStackOverflow(y
32730 79 70 50 61 72 73 65 72 2c 20 26 79 79 6d 69 6e  ypParser, &yymin
32731 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20  orunion);.      
32732 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 23 65  return;.    }.#e
32733 6e 64 69 66 0a 20 20 20 20 79 79 70 50 61 72 73  ndif.    yypPars
32734 65 72 2d 3e 79 79 69 64 78 20 3d 20 30 3b 0a 20  er->yyidx = 0;. 
32735 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79     yypParser->yy
32736 65 72 72 63 6e 74 20 3d 20 2d 31 3b 0a 20 20 20  errcnt = -1;.   
32737 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74   yypParser->yyst
32738 61 63 6b 5b 30 5d 2e 73 74 61 74 65 6e 6f 20 3d  ack[0].stateno =
32739 20 30 3b 0a 20 20 20 20 79 79 70 50 61 72 73 65   0;.    yypParse
3273a 72 2d 3e 79 79 73 74 61 63 6b 5b 30 5d 2e 6d 61  r->yystack[0].ma
3273b 6a 6f 72 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 79  jor = 0;.  }.  y
3273c 79 6d 69 6e 6f 72 75 6e 69 6f 6e 2e 79 79 30 20  yminorunion.yy0 
3273d 3d 20 79 79 6d 69 6e 6f 72 3b 0a 20 20 79 79 65  = yyminor;.  yye
3273e 6e 64 6f 66 69 6e 70 75 74 20 3d 20 28 79 79 6d  ndofinput = (yym
3273f 61 6a 6f 72 3d 3d 30 29 3b 0a 20 20 73 71 6c 69  ajor==0);.  sqli
32740 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f  te3ParserARG_STO
32741 52 45 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  RE;..#ifndef NDE
32742 42 55 47 0a 20 20 69 66 28 20 79 79 54 72 61 63  BUG.  if( yyTrac
32743 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 66 70 72  eFILE ){.    fpr
32744 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
32745 2c 22 25 73 49 6e 70 75 74 20 25 73 5c 6e 22 2c  ,"%sInput %s\n",
32746 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 79 79  yyTracePrompt,yy
32747 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 6d 61 6a 6f  TokenName[yymajo
32748 72 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  r]);.  }.#endif.
32749 0a 20 20 64 6f 7b 0a 20 20 20 20 79 79 61 63 74  .  do{.    yyact
3274a 20 3d 20 79 79 5f 66 69 6e 64 5f 73 68 69 66 74   = yy_find_shift
3274b 5f 61 63 74 69 6f 6e 28 79 79 70 50 61 72 73 65  _action(yypParse
3274c 72 2c 28 59 59 43 4f 44 45 54 59 50 45 29 79 79  r,(YYCODETYPE)yy
3274d 6d 61 6a 6f 72 29 3b 0a 20 20 20 20 69 66 28 20  major);.    if( 
3274e 79 79 61 63 74 3c 59 59 4e 53 54 41 54 45 20 29  yyact<YYNSTATE )
3274f 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
32750 21 79 79 65 6e 64 6f 66 69 6e 70 75 74 20 29 3b  !yyendofinput );
32751 20 20 2f 2a 20 49 6d 70 6f 73 73 69 62 6c 65 20    /* Impossible 
32752 74 6f 20 73 68 69 66 74 20 74 68 65 20 24 20 74  to shift the $ t
32753 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 79 79  oken */.      yy
32754 5f 73 68 69 66 74 28 79 79 70 50 61 72 73 65 72  _shift(yypParser
32755 2c 79 79 61 63 74 2c 79 79 6d 61 6a 6f 72 2c 26  ,yyact,yymajor,&
32756 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20  yyminorunion);. 
32757 20 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e       yypParser->
32758 79 79 65 72 72 63 6e 74 2d 2d 3b 0a 20 20 20 20  yyerrcnt--;.    
32759 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f    yymajor = YYNO
3275a 43 4f 44 45 3b 0a 20 20 20 20 7d 65 6c 73 65 20  CODE;.    }else 
3275b 69 66 28 20 79 79 61 63 74 20 3c 20 59 59 4e 53  if( yyact < YYNS
3275c 54 41 54 45 20 2b 20 59 59 4e 52 55 4c 45 20 29  TATE + YYNRULE )
3275d 7b 0a 20 20 20 20 20 20 79 79 5f 72 65 64 75 63  {.      yy_reduc
3275e 65 28 79 79 70 50 61 72 73 65 72 2c 79 79 61 63  e(yypParser,yyac
3275f 74 2d 59 59 4e 53 54 41 54 45 29 3b 0a 20 20 20  t-YYNSTATE);.   
32760 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73   }else{.      as
32761 73 65 72 74 28 20 79 79 61 63 74 20 3d 3d 20 59  sert( yyact == Y
32762 59 5f 45 52 52 4f 52 5f 41 43 54 49 4f 4e 20 29  Y_ERROR_ACTION )
32763 3b 0a 23 69 66 64 65 66 20 59 59 45 52 52 4f 52  ;.#ifdef YYERROR
32764 53 59 4d 42 4f 4c 0a 20 20 20 20 20 20 69 6e 74  SYMBOL.      int
32765 20 79 79 6d 78 3b 0a 23 65 6e 64 69 66 0a 23 69   yymx;.#endif.#i
32766 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20  fndef NDEBUG.   
32767 20 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49     if( yyTraceFI
32768 4c 45 20 29 7b 0a 20 20 20 20 20 20 20 20 66 70  LE ){.        fp
32769 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
3276a 45 2c 22 25 73 53 79 6e 74 61 78 20 45 72 72 6f  E,"%sSyntax Erro
3276b 72 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f  r!\n",yyTracePro
3276c 6d 70 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65  mpt);.      }.#e
3276d 6e 64 69 66 0a 23 69 66 64 65 66 20 59 59 45 52  ndif.#ifdef YYER
3276e 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 20 20 20 20  RORSYMBOL.      
3276f 2f 2a 20 41 20 73 79 6e 74 61 78 20 65 72 72 6f  /* A syntax erro
32770 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 0a  r has occurred..
32771 20 20 20 20 20 20 2a 2a 20 54 68 65 20 72 65 73        ** The res
32772 70 6f 6e 73 65 20 74 6f 20 61 6e 20 65 72 72 6f  ponse to an erro
32773 72 20 64 65 70 65 6e 64 73 20 75 70 6f 6e 20 77  r depends upon w
32774 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
32775 65 0a 20 20 20 20 20 20 2a 2a 20 67 72 61 6d 6d  e.      ** gramm
32776 61 72 20 64 65 66 69 6e 65 73 20 61 6e 20 65 72  ar defines an er
32777 72 6f 72 20 74 6f 6b 65 6e 20 22 45 52 52 4f 52  ror token "ERROR
32778 22 2e 20 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20  ".  .      **.  
32779 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 77      ** This is w
3277a 68 61 74 20 77 65 20 64 6f 20 69 66 20 74 68 65  hat we do if the
3277b 20 67 72 61 6d 6d 61 72 20 64 6f 65 73 20 64 65   grammar does de
3277c 66 69 6e 65 20 45 52 52 4f 52 3a 0a 20 20 20 20  fine ERROR:.    
3277d 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a    **.      **  *
3277e 20 43 61 6c 6c 20 74 68 65 20 25 73 79 6e 74 61   Call the %synta
3277f 78 5f 65 72 72 6f 72 20 66 75 6e 63 74 69 6f 6e  x_error function
32780 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
32781 20 2a 2a 20 20 2a 20 42 65 67 69 6e 20 70 6f 70   **  * Begin pop
32782 70 69 6e 67 20 74 68 65 20 73 74 61 63 6b 20 75  ping the stack u
32783 6e 74 69 6c 20 77 65 20 65 6e 74 65 72 20 61 20  ntil we enter a 
32784 73 74 61 74 65 20 77 68 65 72 65 0a 20 20 20 20  state where.    
32785 20 20 2a 2a 20 20 20 20 69 74 20 69 73 20 6c 65    **    it is le
32786 67 61 6c 20 74 6f 20 73 68 69 66 74 20 74 68 65  gal to shift the
32787 20 65 72 72 6f 72 20 73 79 6d 62 6f 6c 2c 20 74   error symbol, t
32788 68 65 6e 20 73 68 69 66 74 0a 20 20 20 20 20 20  hen shift.      
32789 2a 2a 20 20 20 20 74 68 65 20 65 72 72 6f 72 20  **    the error 
3278a 73 79 6d 62 6f 6c 2e 0a 20 20 20 20 20 20 2a 2a  symbol..      **
3278b 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 53 65 74  .      **  * Set
3278c 20 74 68 65 20 65 72 72 6f 72 20 63 6f 75 6e 74   the error count
3278d 20 74 6f 20 74 68 72 65 65 2e 0a 20 20 20 20 20   to three..     
3278e 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20   **.      **  * 
3278f 42 65 67 69 6e 20 61 63 63 65 70 74 69 6e 67 20  Begin accepting 
32790 61 6e 64 20 73 68 69 66 74 69 6e 67 20 6e 65 77  and shifting new
32791 20 74 6f 6b 65 6e 73 2e 20 20 4e 6f 20 6e 65 77   tokens.  No new
32792 20 65 72 72 6f 72 0a 20 20 20 20 20 20 2a 2a 20   error.      ** 
32793 20 20 20 70 72 6f 63 65 73 73 69 6e 67 20 77 69     processing wi
32794 6c 6c 20 6f 63 63 75 72 20 75 6e 74 69 6c 20 74  ll occur until t
32795 68 72 65 65 20 74 6f 6b 65 6e 73 20 68 61 76 65  hree tokens have
32796 20 62 65 65 6e 0a 20 20 20 20 20 20 2a 2a 20 20   been.      **  
32797 20 20 73 68 69 66 74 65 64 20 73 75 63 63 65 73    shifted succes
32798 73 66 75 6c 6c 79 2e 0a 20 20 20 20 20 20 2a 2a  sfully..      **
32799 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
3279a 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79  if( yypParser->y
3279b 79 65 72 72 63 6e 74 3c 30 20 29 7b 0a 20 20 20  yerrcnt<0 ){.   
3279c 20 20 20 20 20 79 79 5f 73 79 6e 74 61 78 5f 65       yy_syntax_e
3279d 72 72 6f 72 28 79 79 70 50 61 72 73 65 72 2c 79  rror(yypParser,y
3279e 79 6d 61 6a 6f 72 2c 79 79 6d 69 6e 6f 72 75 6e  ymajor,yyminorun
3279f 69 6f 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ion);.      }.  
327a0 20 20 20 20 79 79 6d 78 20 3d 20 79 79 70 50 61      yymx = yypPa
327a1 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79  rser->yystack[yy
327a2 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 2e  pParser->yyidx].
327a3 6d 61 6a 6f 72 3b 0a 20 20 20 20 20 20 69 66 28  major;.      if(
327a4 20 79 79 6d 78 3d 3d 59 59 45 52 52 4f 52 53 59   yymx==YYERRORSY
327a5 4d 42 4f 4c 20 7c 7c 20 79 79 65 72 72 6f 72 68  MBOL || yyerrorh
327a6 69 74 20 29 7b 0a 23 69 66 6e 64 65 66 20 4e 44  it ){.#ifndef ND
327a7 45 42 55 47 0a 20 20 20 20 20 20 20 20 69 66 28  EBUG.        if(
327a8 20 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a   yyTraceFILE ){.
327a9 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e 74            fprint
327aa 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25  f(yyTraceFILE,"%
327ab 73 44 69 73 63 61 72 64 20 69 6e 70 75 74 20 74  sDiscard input t
327ac 6f 6b 65 6e 20 25 73 5c 6e 22 2c 0a 20 20 20 20  oken %s\n",.    
327ad 20 20 20 20 20 20 20 20 20 79 79 54 72 61 63 65           yyTrace
327ae 50 72 6f 6d 70 74 2c 79 79 54 6f 6b 65 6e 4e 61  Prompt,yyTokenNa
327af 6d 65 5b 79 79 6d 61 6a 6f 72 5d 29 3b 0a 20 20  me[yymajor]);.  
327b0 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
327b1 20 20 20 20 20 20 20 79 79 5f 64 65 73 74 72 75         yy_destru
327b2 63 74 6f 72 28 79 79 70 50 61 72 73 65 72 2c 20  ctor(yypParser, 
327b3 28 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d 61  (YYCODETYPE)yyma
327b4 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f  jor,&yyminorunio
327b5 6e 29 3b 0a 20 20 20 20 20 20 20 20 79 79 6d 61  n);.        yyma
327b6 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a  jor = YYNOCODE;.
327b7 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
327b8 20 20 20 20 20 20 77 68 69 6c 65 28 0a 20 20 20        while(.   
327b9 20 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72         yypParser
327ba 2d 3e 79 79 69 64 78 20 3e 3d 20 30 20 26 26 0a  ->yyidx >= 0 &&.
327bb 20 20 20 20 20 20 20 20 20 20 79 79 6d 78 20 21            yymx !
327bc 3d 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 20  = YYERRORSYMBOL 
327bd 26 26 0a 20 20 20 20 20 20 20 20 20 20 28 79 79  &&.          (yy
327be 61 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f 72 65  act = yy_find_re
327bf 64 75 63 65 5f 61 63 74 69 6f 6e 28 0a 20 20 20  duce_action(.   
327c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
327c1 20 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e       yypParser->
327c2 79 79 73 74 61 63 6b 5b 79 79 70 50 61 72 73 65  yystack[yypParse
327c3 72 2d 3e 79 79 69 64 78 5d 2e 73 74 61 74 65 6e  r->yyidx].staten
327c4 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  o,.             
327c5 20 20 20 20 20 20 20 20 20 20 20 59 59 45 52 52             YYERR
327c6 4f 52 53 59 4d 42 4f 4c 29 29 20 3e 3d 20 59 59  ORSYMBOL)) >= YY
327c7 4e 53 54 41 54 45 0a 20 20 20 20 20 20 20 20 29  NSTATE.        )
327c8 7b 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f 70  {.          yy_p
327c9 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28  op_parser_stack(
327ca 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20 20 20  yypParser);.    
327cb 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
327cc 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  ( yypParser->yyi
327cd 64 78 20 3c 20 30 20 7c 7c 20 79 79 6d 61 6a 6f  dx < 0 || yymajo
327ce 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  r==0 ){.        
327cf 20 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72 28    yy_destructor(
327d0 79 79 70 50 61 72 73 65 72 2c 28 59 59 43 4f 44  yypParser,(YYCOD
327d1 45 54 59 50 45 29 79 79 6d 61 6a 6f 72 2c 26 79  ETYPE)yymajor,&y
327d2 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20  yminorunion);.  
327d3 20 20 20 20 20 20 20 20 79 79 5f 70 61 72 73 65          yy_parse
327d4 5f 66 61 69 6c 65 64 28 79 79 70 50 61 72 73 65  _failed(yypParse
327d5 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 79 79  r);.          yy
327d6 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45  major = YYNOCODE
327d7 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ;.        }else 
327d8 69 66 28 20 79 79 6d 78 21 3d 59 59 45 52 52 4f  if( yymx!=YYERRO
327d9 52 53 59 4d 42 4f 4c 20 29 7b 0a 20 20 20 20 20  RSYMBOL ){.     
327da 20 20 20 20 20 59 59 4d 49 4e 4f 52 54 59 50 45       YYMINORTYPE
327db 20 75 32 3b 0a 20 20 20 20 20 20 20 20 20 20 75   u2;.          u
327dc 32 2e 59 59 45 52 52 53 59 4d 44 54 20 3d 20 30  2.YYERRSYMDT = 0
327dd 3b 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f 73  ;.          yy_s
327de 68 69 66 74 28 79 79 70 50 61 72 73 65 72 2c 79  hift(yypParser,y
327df 79 61 63 74 2c 59 59 45 52 52 4f 52 53 59 4d 42  yact,YYERRORSYMB
327e0 4f 4c 2c 26 75 32 29 3b 0a 20 20 20 20 20 20 20  OL,&u2);.       
327e1 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
327e2 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 65 72   yypParser->yyer
327e3 72 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20 20 20  rcnt = 3;.      
327e4 79 79 65 72 72 6f 72 68 69 74 20 3d 20 31 3b 0a  yyerrorhit = 1;.
327e5 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 59 59  #elif defined(YY
327e6 4e 4f 45 52 52 4f 52 52 45 43 4f 56 45 52 59 29  NOERRORRECOVERY)
327e7 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
327e8 20 59 59 4e 4f 45 52 52 4f 52 52 45 43 4f 56 45   YYNOERRORRECOVE
327e9 52 59 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69  RY macro is defi
327ea 6e 65 64 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74  ned, then do not
327eb 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20 20 20   attempt to.    
327ec 20 20 2a 2a 20 64 6f 20 61 6e 79 20 6b 69 6e 64    ** do any kind
327ed 20 6f 66 20 65 72 72 6f 72 20 72 65 63 6f 76 65   of error recove
327ee 72 79 2e 20 20 49 6e 73 74 65 61 64 2c 20 73 69  ry.  Instead, si
327ef 6d 70 6c 79 20 69 6e 76 6f 6b 65 20 74 68 65 20  mply invoke the 
327f0 73 79 6e 74 61 78 0a 20 20 20 20 20 20 2a 2a 20  syntax.      ** 
327f1 65 72 72 6f 72 20 72 6f 75 74 69 6e 65 20 61 6e  error routine an
327f2 64 20 63 6f 6e 74 69 6e 75 65 20 67 6f 69 6e 67  d continue going
327f3 20 61 73 20 69 66 20 6e 6f 74 68 69 6e 67 20 68   as if nothing h
327f4 61 64 20 68 61 70 70 65 6e 65 64 2e 0a 20 20 20  ad happened..   
327f5 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41     **.      ** A
327f6 70 70 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20  pplications can 
327f7 73 65 74 20 74 68 69 73 20 6d 61 63 72 6f 20 28  set this macro (
327f8 66 6f 72 20 65 78 61 6d 70 6c 65 20 69 6e 73 69  for example insi
327f9 64 65 20 25 69 6e 63 6c 75 64 65 29 20 69 66 0a  de %include) if.
327fa 20 20 20 20 20 20 2a 2a 20 74 68 65 79 20 69 6e        ** they in
327fb 74 65 6e 64 20 74 6f 20 61 62 61 6e 64 6f 6e 20  tend to abandon 
327fc 74 68 65 20 70 61 72 73 65 20 75 70 6f 6e 20 74  the parse upon t
327fd 68 65 20 66 69 72 73 74 20 73 79 6e 74 61 78 20  he first syntax 
327fe 65 72 72 6f 72 20 73 65 65 6e 2e 0a 20 20 20 20  error seen..    
327ff 20 20 2a 2f 0a 20 20 20 20 20 20 79 79 5f 73 79    */.      yy_sy
32800 6e 74 61 78 5f 65 72 72 6f 72 28 79 79 70 50 61  ntax_error(yypPa
32801 72 73 65 72 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d  rser,yymajor,yym
32802 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20  inorunion);.    
32803 20 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72 28    yy_destructor(
32804 79 79 70 50 61 72 73 65 72 2c 28 59 59 43 4f 44  yypParser,(YYCOD
32805 45 54 59 50 45 29 79 79 6d 61 6a 6f 72 2c 26 79  ETYPE)yymajor,&y
32806 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20  yminorunion);.  
32807 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59      yymajor = YY
32808 4e 4f 43 4f 44 45 3b 0a 20 20 20 20 20 20 0a 23  NOCODE;.      .#
32809 65 6c 73 65 20 20 2f 2a 20 59 59 45 52 52 4f 52  else  /* YYERROR
3280a 53 59 4d 42 4f 4c 20 69 73 20 6e 6f 74 20 64 65  SYMBOL is not de
3280b 66 69 6e 65 64 20 2a 2f 0a 20 20 20 20 20 20 2f  fined */.      /
3280c 2a 20 54 68 69 73 20 69 73 20 77 68 61 74 20 77  * This is what w
3280d 65 20 64 6f 20 69 66 20 74 68 65 20 67 72 61 6d  e do if the gram
3280e 6d 61 72 20 64 6f 65 73 20 6e 6f 74 20 64 65 66  mar does not def
3280f 69 6e 65 20 45 52 52 4f 52 3a 0a 20 20 20 20 20  ine ERROR:.     
32810 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20   **.      **  * 
32811 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f 72 20  Report an error 
32812 6d 65 73 73 61 67 65 2c 20 61 6e 64 20 74 68 72  message, and thr
32813 6f 77 20 61 77 61 79 20 74 68 65 20 69 6e 70 75  ow away the inpu
32814 74 20 74 6f 6b 65 6e 2e 0a 20 20 20 20 20 20 2a  t token..      *
32815 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 49 66  *.      **  * If
32816 20 74 68 65 20 69 6e 70 75 74 20 74 6f 6b 65 6e   the input token
32817 20 69 73 20 24 2c 20 74 68 65 6e 20 66 61 69 6c   is $, then fail
32818 20 74 68 65 20 70 61 72 73 65 2e 0a 20 20 20 20   the parse..    
32819 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41 73    **.      ** As
3281a 20 62 65 66 6f 72 65 2c 20 73 75 62 73 65 71 75   before, subsequ
3281b 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67  ent error messag
3281c 65 73 20 61 72 65 20 73 75 70 70 72 65 73 73 65  es are suppresse
3281d 64 20 75 6e 74 69 6c 0a 20 20 20 20 20 20 2a 2a  d until.      **
3281e 20 74 68 72 65 65 20 69 6e 70 75 74 20 74 6f 6b   three input tok
3281f 65 6e 73 20 68 61 76 65 20 62 65 65 6e 20 73 75  ens have been su
32820 63 63 65 73 73 66 75 6c 6c 79 20 73 68 69 66 74  ccessfully shift
32821 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ed..      */.   
32822 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72     if( yypParser
32823 2d 3e 79 79 65 72 72 63 6e 74 3c 3d 30 20 29 7b  ->yyerrcnt<=0 ){
32824 0a 20 20 20 20 20 20 20 20 79 79 5f 73 79 6e 74  .        yy_synt
32825 61 78 5f 65 72 72 6f 72 28 79 79 70 50 61 72 73  ax_error(yypPars
32826 65 72 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d 69 6e  er,yymajor,yymin
32827 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20  orunion);.      
32828 7d 0a 20 20 20 20 20 20 79 79 70 50 61 72 73 65  }.      yypParse
32829 72 2d 3e 79 79 65 72 72 63 6e 74 20 3d 20 33 3b  r->yyerrcnt = 3;
3282a 0a 20 20 20 20 20 20 79 79 5f 64 65 73 74 72 75  .      yy_destru
3282b 63 74 6f 72 28 79 79 70 50 61 72 73 65 72 2c 28  ctor(yypParser,(
3282c 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d 61 6a  YYCODETYPE)yymaj
3282d 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e  or,&yyminorunion
3282e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 79 79 65  );.      if( yye
3282f 6e 64 6f 66 69 6e 70 75 74 20 29 7b 0a 20 20 20  ndofinput ){.   
32830 20 20 20 20 20 79 79 5f 70 61 72 73 65 5f 66 61       yy_parse_fa
32831 69 6c 65 64 28 79 79 70 50 61 72 73 65 72 29 3b  iled(yypParser);
32832 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 79  .      }.      y
32833 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44  ymajor = YYNOCOD
32834 45 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a  E;.#endif.    }.
32835 20 20 7d 77 68 69 6c 65 28 20 79 79 6d 61 6a 6f    }while( yymajo
32836 72 21 3d 59 59 4e 4f 43 4f 44 45 20 26 26 20 79  r!=YYNOCODE && y
32837 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e  ypParser->yyidx>
32838 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a  =0 );.  return;.
32839 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
3283a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 2e  ** End of parse.
3283b 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
3283c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3283d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3283e 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
3283f 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 74 6f  ** Begin file to
32840 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  kenize.c *******
32841 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32842 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32843 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
32844 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
32845 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
32846 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
32847 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
32848 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
32849 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
3284a 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
3284b 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
3284c 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
3284d 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
3284e 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
3284f 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
32850 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
32851 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
32852 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
32853 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
32854 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
32855 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
32856 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32857 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32858 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32859 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3285a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 41 6e 20  *********.** An 
3285b 74 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 53 51  tokenizer for SQ
3285c 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  L.**.** This fil
3285d 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64  e contains C cod
3285e 65 20 74 68 61 74 20 73 70 6c 69 74 73 20 61 6e  e that splits an
3285f 20 53 51 4c 20 69 6e 70 75 74 20 73 74 72 69 6e   SQL input strin
32860 67 20 75 70 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64  g up into.** ind
32861 69 76 69 64 75 61 6c 20 74 6f 6b 65 6e 73 20 61  ividual tokens a
32862 6e 64 20 73 65 6e 64 73 20 74 68 6f 73 65 20 74  nd sends those t
32863 6f 6b 65 6e 73 20 6f 6e 65 2d 62 79 2d 6f 6e 65  okens one-by-one
32864 20 6f 76 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20   over to the.** 
32865 70 61 72 73 65 72 20 66 6f 72 20 61 6e 61 6c 79  parser for analy
32866 73 69 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  sis..**.** $Id: 
32867 74 6f 6b 65 6e 69 7a 65 2e 63 2c 76 20 31 2e 31  tokenize.c,v 1.1
32868 36 33 20 32 30 30 39 2f 30 37 2f 30 33 20 32 32  63 2009/07/03 22
32869 3a 35 34 3a 33 37 20 64 72 68 20 45 78 70 20 24  :54:37 drh Exp $
3286a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63  .*/../*.** The c
3286b 68 61 72 4d 61 70 28 29 20 6d 61 63 72 6f 20 6d  harMap() macro m
3286c 61 70 73 20 61 6c 70 68 61 62 65 74 69 63 20 63  aps alphabetic c
3286d 68 61 72 61 63 74 65 72 73 20 69 6e 74 6f 20 74  haracters into t
3286e 68 65 69 72 0a 2a 2a 20 6c 6f 77 65 72 2d 63 61  heir.** lower-ca
3286f 73 65 20 41 53 43 49 49 20 65 71 75 69 76 61 6c  se ASCII equival
32870 65 6e 74 2e 20 20 4f 6e 20 41 53 43 49 49 20 6d  ent.  On ASCII m
32871 61 63 68 69 6e 65 73 2c 20 74 68 69 73 20 69 73  achines, this is
32872 20 6a 75 73 74 0a 2a 2a 20 61 6e 20 75 70 70 65   just.** an uppe
32873 72 2d 74 6f 2d 6c 6f 77 65 72 20 63 61 73 65 20  r-to-lower case 
32874 6d 61 70 2e 20 20 4f 6e 20 45 42 43 44 49 43 20  map.  On EBCDIC 
32875 6d 61 63 68 69 6e 65 73 20 77 65 20 61 6c 73 6f  machines we also
32876 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 61 64 6a 75   need.** to adju
32877 73 74 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 2e  st the encoding.
32878 20 20 4f 6e 6c 79 20 61 6c 70 68 61 62 65 74 69    Only alphabeti
32879 63 20 63 68 61 72 61 63 74 65 72 73 20 61 6e 64  c characters and
3287a 20 75 6e 64 65 72 73 63 6f 72 65 73 0a 2a 2a 20   underscores.** 
3287b 6e 65 65 64 20 74 6f 20 62 65 20 74 72 61 6e 73  need to be trans
3287c 6c 61 74 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66  lated..*/.#ifdef
3287d 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 23 20   SQLITE_ASCII.# 
3287e 64 65 66 69 6e 65 20 63 68 61 72 4d 61 70 28 58  define charMap(X
3287f 29 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f  ) sqlite3UpperTo
32880 4c 6f 77 65 72 5b 28 75 6e 73 69 67 6e 65 64 20  Lower[(unsigned 
32881 63 68 61 72 29 58 5d 0a 23 65 6e 64 69 66 0a 23  char)X].#endif.#
32882 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43  ifdef SQLITE_EBC
32883 44 49 43 0a 23 20 64 65 66 69 6e 65 20 63 68 61  DIC.# define cha
32884 72 4d 61 70 28 58 29 20 65 62 63 64 69 63 54 6f  rMap(X) ebcdicTo
32885 41 73 63 69 69 5b 28 75 6e 73 69 67 6e 65 64 20  Ascii[(unsigned 
32886 63 68 61 72 29 58 5d 0a 63 6f 6e 73 74 20 75 6e  char)X].const un
32887 73 69 67 6e 65 64 20 63 68 61 72 20 65 62 63 64  signed char ebcd
32888 69 63 54 6f 41 73 63 69 69 5b 5d 20 3d 20 7b 0a  icToAscii[] = {.
32889 2f 2a 20 30 20 20 20 31 20 20 20 32 20 20 20 33  /* 0   1   2   3
3288a 20 20 20 34 20 20 20 35 20 20 20 36 20 20 20 37     4   5   6   7
3288b 20 20 20 38 20 20 20 39 20 20 20 41 20 20 20 42     8   9   A   B
3288c 20 20 20 43 20 20 20 44 20 20 20 45 20 20 20 46     C   D   E   F
3288d 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30   */.   0,  0,  0
3288e 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
3288f 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
32890 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
32891 2c 20 20 30 2c 20 20 2f 2a 20 30 78 20 2a 2f 0a  ,  0,  /* 0x */.
32892 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30     0,  0,  0,  0
32893 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
32894 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
32895 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
32896 2c 20 20 2f 2a 20 31 78 20 2a 2f 0a 20 20 20 30  ,  /* 1x */.   0
32897 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
32898 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
32899 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
3289a 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f  ,  0,  0,  0,  /
3289b 2a 20 32 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30  * 2x */.   0,  0
3289c 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
3289d 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
3289e 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
3289f 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 33 78  ,  0,  0,  /* 3x
328a0 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30   */.   0,  0,  0
328a1 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328a2 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328a3 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328a4 2c 20 20 30 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a  ,  0,  /* 4x */.
328a5 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30     0,  0,  0,  0
328a6 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328a7 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328a8 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328a9 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 30  ,  /* 5x */.   0
328aa 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328ab 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328ac 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328ad 2c 20 39 35 2c 20 20 30 2c 20 20 30 2c 20 20 2f  , 95,  0,  0,  /
328ae 2a 20 36 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30  * 6x */.   0,  0
328af 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328b0 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328b1 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328b2 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 37 78  ,  0,  0,  /* 7x
328b3 20 2a 2f 0a 20 20 20 30 2c 20 39 37 2c 20 39 38   */.   0, 97, 98
328b4 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 32  , 99,100,101,102
328b5 2c 31 30 33 2c 31 30 34 2c 31 30 35 2c 20 20 30  ,103,104,105,  0
328b6 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328b7 2c 20 20 30 2c 20 20 2f 2a 20 38 78 20 2a 2f 0a  ,  0,  /* 8x */.
328b8 20 20 20 30 2c 31 30 36 2c 31 30 37 2c 31 30 38     0,106,107,108
328b9 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 32  ,109,110,111,112
328ba 2c 31 31 33 2c 31 31 34 2c 20 20 30 2c 20 20 30  ,113,114,  0,  0
328bb 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328bc 2c 20 20 2f 2a 20 39 78 20 2a 2f 0a 20 20 20 30  ,  /* 9x */.   0
328bd 2c 20 20 30 2c 31 31 35 2c 31 31 36 2c 31 31 37  ,  0,115,116,117
328be 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31  ,118,119,120,121
328bf 2c 31 32 32 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,122,  0,  0,  0
328c0 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f  ,  0,  0,  0,  /
328c1 2a 20 41 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30  * Ax */.   0,  0
328c2 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328c3 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328c4 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328c5 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 42 78  ,  0,  0,  /* Bx
328c6 20 2a 2f 0a 20 20 20 30 2c 20 39 37 2c 20 39 38   */.   0, 97, 98
328c7 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c 31 30 32  , 99,100,101,102
328c8 2c 31 30 33 2c 31 30 34 2c 31 30 35 2c 20 20 30  ,103,104,105,  0
328c9 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328ca 2c 20 20 30 2c 20 20 2f 2a 20 43 78 20 2a 2f 0a  ,  0,  /* Cx */.
328cb 20 20 20 30 2c 31 30 36 2c 31 30 37 2c 31 30 38     0,106,107,108
328cc 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 32  ,109,110,111,112
328cd 2c 31 31 33 2c 31 31 34 2c 20 20 30 2c 20 20 30  ,113,114,  0,  0
328ce 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328cf 2c 20 20 2f 2a 20 44 78 20 2a 2f 0a 20 20 20 30  ,  /* Dx */.   0
328d0 2c 20 20 30 2c 31 31 35 2c 31 31 36 2c 31 31 37  ,  0,115,116,117
328d1 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31  ,118,119,120,121
328d2 2c 31 32 32 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,122,  0,  0,  0
328d3 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f  ,  0,  0,  0,  /
328d4 2a 20 45 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30  * Ex */.   0,  0
328d5 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328d6 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328d7 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30  ,  0,  0,  0,  0
328d8 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 46 78  ,  0,  0,  /* Fx
328d9 20 2a 2f 0a 7d 3b 0a 23 65 6e 64 69 66 0a 0a 2f   */.};.#endif../
328da 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
328db 4b 65 79 77 6f 72 64 43 6f 64 65 20 66 75 6e 63  KeywordCode func
328dc 74 69 6f 6e 20 6c 6f 6f 6b 73 20 75 70 20 61 6e  tion looks up an
328dd 20 69 64 65 6e 74 69 66 69 65 72 20 74 6f 20 64   identifier to d
328de 65 74 65 72 6d 69 6e 65 20 69 66 0a 2a 2a 20 69  etermine if.** i
328df 74 20 69 73 20 61 20 6b 65 79 77 6f 72 64 2e 20  t is a keyword. 
328e0 20 49 66 20 69 74 20 69 73 20 61 20 6b 65 79 77   If it is a keyw
328e1 6f 72 64 2c 20 74 68 65 20 74 6f 6b 65 6e 20 63  ord, the token c
328e2 6f 64 65 20 6f 66 20 74 68 61 74 20 6b 65 79 77  ode of that keyw
328e3 6f 72 64 20 69 73 20 0a 2a 2a 20 72 65 74 75 72  ord is .** retur
328e4 6e 65 64 2e 20 20 49 66 20 74 68 65 20 69 6e 70  ned.  If the inp
328e5 75 74 20 69 73 20 6e 6f 74 20 61 20 6b 65 79 77  ut is not a keyw
328e6 6f 72 64 2c 20 54 4b 5f 49 44 20 69 73 20 72 65  ord, TK_ID is re
328e7 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  turned..**.** Th
328e8 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
328e9 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
328ea 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 62   was generated b
328eb 79 20 61 20 70 72 6f 67 72 61 6d 2c 0a 2a 2a 20  y a program,.** 
328ec 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 2c  mkkeywordhash.h,
328ed 20 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 65 20   located in the 
328ee 74 6f 6f 6c 20 73 75 62 64 69 72 65 63 74 6f 72  tool subdirector
328ef 79 20 6f 66 20 74 68 65 20 64 69 73 74 72 69 62  y of the distrib
328f0 75 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 6f 75  ution..** The ou
328f1 74 70 75 74 20 6f 66 20 74 68 65 20 6d 6b 6b 65  tput of the mkke
328f2 79 77 6f 72 64 68 61 73 68 2e 63 20 70 72 6f 67  ywordhash.c prog
328f3 72 61 6d 20 69 73 20 77 72 69 74 74 65 6e 20 69  ram is written i
328f4 6e 74 6f 20 61 20 66 69 6c 65 0a 2a 2a 20 6e 61  nto a file.** na
328f5 6d 65 64 20 6b 65 79 77 6f 72 64 68 61 73 68 2e  med keywordhash.
328f6 68 20 61 6e 64 20 74 68 65 6e 20 69 6e 63 6c 75  h and then inclu
328f7 64 65 64 20 69 6e 74 6f 20 74 68 69 73 20 73 6f  ded into this so
328f8 75 72 63 65 20 66 69 6c 65 20 62 79 0a 2a 2a 20  urce file by.** 
328f9 74 68 65 20 23 69 6e 63 6c 75 64 65 20 62 65 6c  the #include bel
328fa 6f 77 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ow..*/./********
328fb 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6b  ****** Include k
328fc 65 79 77 6f 72 64 68 61 73 68 2e 68 20 69 6e 20  eywordhash.h in 
328fd 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 6f  the middle of to
328fe 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  kenize.c *******
328ff 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
32900 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
32901 65 20 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 20  e keywordhash.h 
32902 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32903 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32904 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 20 54 68  *****/./***** Th
32905 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
32906 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67   automatically g
32907 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 2a 2a  enerated code **
32908 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ****.**.** The c
32909 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ode in this file
3290a 20 68 61 73 20 62 65 65 6e 20 61 75 74 6f 6d 61   has been automa
3290b 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65  tically generate
3290c 64 20 62 79 0a 2a 2a 0a 2a 2a 20 20 20 20 20 24  d by.**.**     $
3290d 48 65 61 64 65 72 3a 20 2f 68 6f 6d 65 2f 64 72  Header: /home/dr
3290e 68 2f 73 71 6c 69 74 65 2f 74 72 61 6e 73 2f 63  h/sqlite/trans/c
3290f 76 73 2f 73 71 6c 69 74 65 2f 73 71 6c 69 74 65  vs/sqlite/sqlite
32910 2f 74 6f 6f 6c 2f 6d 6b 6b 65 79 77 6f 72 64 68  /tool/mkkeywordh
32911 61 73 68 2e 63 2c 76 20 31 2e 33 38 20 32 30 30  ash.c,v 1.38 200
32912 39 2f 30 36 2f 30 39 20 31 34 3a 32 37 3a 34 31  9/06/09 14:27:41
32913 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a   drh Exp $.**.**
32914 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
32915 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  s file implement
32916 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  s a function tha
32917 74 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65  t determines whe
32918 74 68 65 72 0a 2a 2a 20 6f 72 20 6e 6f 74 20 61  ther.** or not a
32919 20 67 69 76 65 6e 20 69 64 65 6e 74 69 66 69 65   given identifie
3291a 72 20 69 73 20 72 65 61 6c 6c 79 20 61 6e 20 53  r is really an S
3291b 51 4c 20 6b 65 79 77 6f 72 64 2e 20 20 54 68 65  QL keyword.  The
3291c 20 73 61 6d 65 20 74 68 69 6e 67 0a 2a 2a 20 6d   same thing.** m
3291d 69 67 68 74 20 62 65 20 69 6d 70 6c 65 6d 65 6e  ight be implemen
3291e 74 65 64 20 6d 6f 72 65 20 64 69 72 65 63 74 6c  ted more directl
3291f 79 20 75 73 69 6e 67 20 61 20 68 61 6e 64 2d 77  y using a hand-w
32920 72 69 74 74 65 6e 20 68 61 73 68 20 74 61 62 6c  ritten hash tabl
32921 65 2e 0a 2a 2a 20 42 75 74 20 62 79 20 75 73 69  e..** But by usi
32922 6e 67 20 74 68 69 73 20 61 75 74 6f 6d 61 74 69  ng this automati
32923 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20  cally generated 
32924 63 6f 64 65 2c 20 74 68 65 20 73 69 7a 65 20 6f  code, the size o
32925 66 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 69 73  f the code.** is
32926 20 73 75 62 73 74 61 6e 74 69 61 6c 6c 79 20 72   substantially r
32927 65 64 75 63 65 64 2e 20 20 54 68 69 73 20 69 73  educed.  This is
32928 20 69 6d 70 6f 72 74 61 6e 74 20 66 6f 72 20 65   important for e
32929 6d 62 65 64 64 65 64 20 61 70 70 6c 69 63 61 74  mbedded applicat
3292a 69 6f 6e 73 0a 2a 2a 20 6f 6e 20 70 6c 61 74 66  ions.** on platf
3292b 6f 72 6d 73 20 77 69 74 68 20 6c 69 6d 69 74 65  orms with limite
3292c 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 2f 2a 20  d memory..*/./* 
3292d 48 61 73 68 20 73 63 6f 72 65 3a 20 31 37 35 20  Hash score: 175 
3292e 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6b 65  */.static int ke
3292f 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73 74 20  ywordCode(const 
32930 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b  char *z, int n){
32931 0a 20 20 2f 2a 20 7a 54 65 78 74 5b 5d 20 65 6e  .  /* zText[] en
32932 63 6f 64 65 73 20 38 31 31 20 62 79 74 65 73 20  codes 811 bytes 
32933 6f 66 20 6b 65 79 77 6f 72 64 73 20 69 6e 20 35  of keywords in 5
32934 34 31 20 62 79 74 65 73 20 2a 2f 0a 20 20 2f 2a  41 bytes */.  /*
32935 20 20 20 52 45 49 4e 44 45 58 45 44 45 53 43 41     REINDEXEDESCA
32936 50 45 41 43 48 45 43 4b 45 59 42 45 46 4f 52 45  PEACHECKEYBEFORE
32937 49 47 4e 4f 52 45 47 45 58 50 4c 41 49 4e 53 54  IGNOREGEXPLAINST
32938 45 41 44 44 41 54 41 42 41 53 45 4c 45 43 54 20  EADDATABASELECT 
32939 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20        */.  /*   
3293a 41 42 4c 45 46 54 48 45 4e 44 45 46 45 52 52 41  ABLEFTHENDEFERRA
3293b 42 4c 45 4c 53 45 58 43 45 50 54 52 41 4e 53 41  BLELSEXCEPTRANSA
3293c 43 54 49 4f 4e 41 54 55 52 41 4c 54 45 52 41 49  CTIONATURALTERAI
3293d 53 45 58 43 4c 55 53 49 56 45 20 20 20 20 20 20  SEXCLUSIVE      
3293e 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 58 49 53     */.  /*   XIS
3293f 54 53 41 56 45 50 4f 49 4e 54 45 52 53 45 43 54  TSAVEPOINTERSECT
32940 52 49 47 47 45 52 45 46 45 52 45 4e 43 45 53 43  RIGGEREFERENCESC
32941 4f 4e 53 54 52 41 49 4e 54 4f 46 46 53 45 54 45  ONSTRAINTOFFSETE
32942 4d 50 4f 52 41 52 59 20 20 20 20 20 20 20 20 20  MPORARY         
32943 2a 2f 0a 20 20 2f 2a 20 20 20 55 4e 49 51 55 45  */.  /*   UNIQUE
32944 52 59 41 54 54 41 43 48 41 56 49 4e 47 52 4f 55  RYATTACHAVINGROU
32945 50 44 41 54 45 42 45 47 49 4e 4e 45 52 45 4c 45  PDATEBEGINNERELE
32946 41 53 45 42 45 54 57 45 45 4e 4f 54 4e 55 4c 4c  ASEBETWEENOTNULL
32947 49 4b 45 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  IKE          */.
32948 20 20 2f 2a 20 20 20 43 41 53 43 41 44 45 4c 45    /*   CASCADELE
32949 54 45 43 41 53 45 43 4f 4c 4c 41 54 45 43 52 45  TECASECOLLATECRE
3294a 41 54 45 43 55 52 52 45 4e 54 5f 44 41 54 45 44  ATECURRENT_DATED
3294b 45 54 41 43 48 49 4d 4d 45 44 49 41 54 45 4a 4f  ETACHIMMEDIATEJO
3294c 49 4e 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f  IN        */.  /
3294d 2a 20 20 20 53 45 52 54 4d 41 54 43 48 50 4c 41  *   SERTMATCHPLA
3294e 4e 41 4c 59 5a 45 50 52 41 47 4d 41 42 4f 52 54  NALYZEPRAGMABORT
3294f 56 41 4c 55 45 53 56 49 52 54 55 41 4c 49 4d 49  VALUESVIRTUALIMI
32950 54 57 48 45 4e 57 48 45 52 45 4e 41 4d 45 20 20  TWHENWHERENAME  
32951 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20         */.  /*  
32952 20 41 46 54 45 52 45 50 4c 41 43 45 41 4e 44 45   AFTEREPLACEANDE
32953 46 41 55 4c 54 41 55 54 4f 49 4e 43 52 45 4d 45  FAULTAUTOINCREME
32954 4e 54 43 41 53 54 43 4f 4c 55 4d 4e 43 4f 4d 4d  NTCASTCOLUMNCOMM
32955 49 54 43 4f 4e 46 4c 49 43 54 43 52 4f 53 53 20  ITCONFLICTCROSS 
32956 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 43 55      */.  /*   CU
32957 52 52 45 4e 54 5f 54 49 4d 45 53 54 41 4d 50 52  RRENT_TIMESTAMPR
32958 49 4d 41 52 59 44 45 46 45 52 52 45 44 49 53 54  IMARYDEFERREDIST
32959 49 4e 43 54 44 52 4f 50 46 41 49 4c 46 52 4f 4d  INCTDROPFAILFROM
3295a 46 55 4c 4c 47 4c 4f 42 59 49 46 20 20 20 20 20  FULLGLOBYIF     
3295b 20 2a 2f 0a 20 20 2f 2a 20 20 20 49 53 4e 55 4c   */.  /*   ISNUL
3295c 4c 4f 52 44 45 52 45 53 54 52 49 43 54 4f 55 54  LORDERESTRICTOUT
3295d 45 52 49 47 48 54 52 4f 4c 4c 42 41 43 4b 52 4f  ERIGHTROLLBACKRO
3295e 57 55 4e 49 4f 4e 55 53 49 4e 47 56 41 43 55 55  WUNIONUSINGVACUU
3295f 4d 56 49 45 57 20 20 20 20 20 20 20 20 20 2a 2f  MVIEW         */
32960 0a 20 20 2f 2a 20 20 20 49 4e 49 54 49 41 4c 4c  .  /*   INITIALL
32961 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Y               
32962 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32963 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32964 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20             */.  
32965 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
32966 72 20 7a 54 65 78 74 5b 35 34 30 5d 20 3d 20 7b  r zText[540] = {
32967 0a 20 20 20 20 27 52 27 2c 27 45 27 2c 27 49 27  .    'R','E','I'
32968 2c 27 4e 27 2c 27 44 27 2c 27 45 27 2c 27 58 27  ,'N','D','E','X'
32969 2c 27 45 27 2c 27 44 27 2c 27 45 27 2c 27 53 27  ,'E','D','E','S'
3296a 2c 27 43 27 2c 27 41 27 2c 27 50 27 2c 27 45 27  ,'C','A','P','E'
3296b 2c 27 41 27 2c 27 43 27 2c 27 48 27 2c 0a 20 20  ,'A','C','H',.  
3296c 20 20 27 45 27 2c 27 43 27 2c 27 4b 27 2c 27 45    'E','C','K','E
3296d 27 2c 27 59 27 2c 27 42 27 2c 27 45 27 2c 27 46  ','Y','B','E','F
3296e 27 2c 27 4f 27 2c 27 52 27 2c 27 45 27 2c 27 49  ','O','R','E','I
3296f 27 2c 27 47 27 2c 27 4e 27 2c 27 4f 27 2c 27 52  ','G','N','O','R
32970 27 2c 27 45 27 2c 27 47 27 2c 0a 20 20 20 20 27  ','E','G',.    '
32971 45 27 2c 27 58 27 2c 27 50 27 2c 27 4c 27 2c 27  E','X','P','L','
32972 41 27 2c 27 49 27 2c 27 4e 27 2c 27 53 27 2c 27  A','I','N','S','
32973 54 27 2c 27 45 27 2c 27 41 27 2c 27 44 27 2c 27  T','E','A','D','
32974 44 27 2c 27 41 27 2c 27 54 27 2c 27 41 27 2c 27  D','A','T','A','
32975 42 27 2c 27 41 27 2c 0a 20 20 20 20 27 53 27 2c  B','A',.    'S',
32976 27 45 27 2c 27 4c 27 2c 27 45 27 2c 27 43 27 2c  'E','L','E','C',
32977 27 54 27 2c 27 41 27 2c 27 42 27 2c 27 4c 27 2c  'T','A','B','L',
32978 27 45 27 2c 27 46 27 2c 27 54 27 2c 27 48 27 2c  'E','F','T','H',
32979 27 45 27 2c 27 4e 27 2c 27 44 27 2c 27 45 27 2c  'E','N','D','E',
3297a 27 46 27 2c 0a 20 20 20 20 27 45 27 2c 27 52 27  'F',.    'E','R'
3297b 2c 27 52 27 2c 27 41 27 2c 27 42 27 2c 27 4c 27  ,'R','A','B','L'
3297c 2c 27 45 27 2c 27 4c 27 2c 27 53 27 2c 27 45 27  ,'E','L','S','E'
3297d 2c 27 58 27 2c 27 43 27 2c 27 45 27 2c 27 50 27  ,'X','C','E','P'
3297e 2c 27 54 27 2c 27 52 27 2c 27 41 27 2c 27 4e 27  ,'T','R','A','N'
3297f 2c 0a 20 20 20 20 27 53 27 2c 27 41 27 2c 27 43  ,.    'S','A','C
32980 27 2c 27 54 27 2c 27 49 27 2c 27 4f 27 2c 27 4e  ','T','I','O','N
32981 27 2c 27 41 27 2c 27 54 27 2c 27 55 27 2c 27 52  ','A','T','U','R
32982 27 2c 27 41 27 2c 27 4c 27 2c 27 54 27 2c 27 45  ','A','L','T','E
32983 27 2c 27 52 27 2c 27 41 27 2c 27 49 27 2c 0a 20  ','R','A','I',. 
32984 20 20 20 27 53 27 2c 27 45 27 2c 27 58 27 2c 27     'S','E','X','
32985 43 27 2c 27 4c 27 2c 27 55 27 2c 27 53 27 2c 27  C','L','U','S','
32986 49 27 2c 27 56 27 2c 27 45 27 2c 27 58 27 2c 27  I','V','E','X','
32987 49 27 2c 27 53 27 2c 27 54 27 2c 27 53 27 2c 27  I','S','T','S','
32988 41 27 2c 27 56 27 2c 27 45 27 2c 0a 20 20 20 20  A','V','E',.    
32989 27 50 27 2c 27 4f 27 2c 27 49 27 2c 27 4e 27 2c  'P','O','I','N',
3298a 27 54 27 2c 27 45 27 2c 27 52 27 2c 27 53 27 2c  'T','E','R','S',
3298b 27 45 27 2c 27 43 27 2c 27 54 27 2c 27 52 27 2c  'E','C','T','R',
3298c 27 49 27 2c 27 47 27 2c 27 47 27 2c 27 45 27 2c  'I','G','G','E',
3298d 27 52 27 2c 27 45 27 2c 0a 20 20 20 20 27 46 27  'R','E',.    'F'
3298e 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c 27 4e 27  ,'E','R','E','N'
3298f 2c 27 43 27 2c 27 45 27 2c 27 53 27 2c 27 43 27  ,'C','E','S','C'
32990 2c 27 4f 27 2c 27 4e 27 2c 27 53 27 2c 27 54 27  ,'O','N','S','T'
32991 2c 27 52 27 2c 27 41 27 2c 27 49 27 2c 27 4e 27  ,'R','A','I','N'
32992 2c 27 54 27 2c 0a 20 20 20 20 27 4f 27 2c 27 46  ,'T',.    'O','F
32993 27 2c 27 46 27 2c 27 53 27 2c 27 45 27 2c 27 54  ','F','S','E','T
32994 27 2c 27 45 27 2c 27 4d 27 2c 27 50 27 2c 27 4f  ','E','M','P','O
32995 27 2c 27 52 27 2c 27 41 27 2c 27 52 27 2c 27 59  ','R','A','R','Y
32996 27 2c 27 55 27 2c 27 4e 27 2c 27 49 27 2c 27 51  ','U','N','I','Q
32997 27 2c 0a 20 20 20 20 27 55 27 2c 27 45 27 2c 27  ',.    'U','E','
32998 52 27 2c 27 59 27 2c 27 41 27 2c 27 54 27 2c 27  R','Y','A','T','
32999 54 27 2c 27 41 27 2c 27 43 27 2c 27 48 27 2c 27  T','A','C','H','
3299a 41 27 2c 27 56 27 2c 27 49 27 2c 27 4e 27 2c 27  A','V','I','N','
3299b 47 27 2c 27 52 27 2c 27 4f 27 2c 27 55 27 2c 0a  G','R','O','U',.
3299c 20 20 20 20 27 50 27 2c 27 44 27 2c 27 41 27 2c      'P','D','A',
3299d 27 54 27 2c 27 45 27 2c 27 42 27 2c 27 45 27 2c  'T','E','B','E',
3299e 27 47 27 2c 27 49 27 2c 27 4e 27 2c 27 4e 27 2c  'G','I','N','N',
3299f 27 45 27 2c 27 52 27 2c 27 45 27 2c 27 4c 27 2c  'E','R','E','L',
329a0 27 45 27 2c 27 41 27 2c 27 53 27 2c 0a 20 20 20  'E','A','S',.   
329a1 20 27 45 27 2c 27 42 27 2c 27 45 27 2c 27 54 27   'E','B','E','T'
329a2 2c 27 57 27 2c 27 45 27 2c 27 45 27 2c 27 4e 27  ,'W','E','E','N'
329a3 2c 27 4f 27 2c 27 54 27 2c 27 4e 27 2c 27 55 27  ,'O','T','N','U'
329a4 2c 27 4c 27 2c 27 4c 27 2c 27 49 27 2c 27 4b 27  ,'L','L','I','K'
329a5 2c 27 45 27 2c 27 43 27 2c 0a 20 20 20 20 27 41  ,'E','C',.    'A
329a6 27 2c 27 53 27 2c 27 43 27 2c 27 41 27 2c 27 44  ','S','C','A','D
329a7 27 2c 27 45 27 2c 27 4c 27 2c 27 45 27 2c 27 54  ','E','L','E','T
329a8 27 2c 27 45 27 2c 27 43 27 2c 27 41 27 2c 27 53  ','E','C','A','S
329a9 27 2c 27 45 27 2c 27 43 27 2c 27 4f 27 2c 27 4c  ','E','C','O','L
329aa 27 2c 27 4c 27 2c 0a 20 20 20 20 27 41 27 2c 27  ','L',.    'A','
329ab 54 27 2c 27 45 27 2c 27 43 27 2c 27 52 27 2c 27  T','E','C','R','
329ac 45 27 2c 27 41 27 2c 27 54 27 2c 27 45 27 2c 27  E','A','T','E','
329ad 43 27 2c 27 55 27 2c 27 52 27 2c 27 52 27 2c 27  C','U','R','R','
329ae 45 27 2c 27 4e 27 2c 27 54 27 2c 27 5f 27 2c 27  E','N','T','_','
329af 44 27 2c 0a 20 20 20 20 27 41 27 2c 27 54 27 2c  D',.    'A','T',
329b0 27 45 27 2c 27 44 27 2c 27 45 27 2c 27 54 27 2c  'E','D','E','T',
329b1 27 41 27 2c 27 43 27 2c 27 48 27 2c 27 49 27 2c  'A','C','H','I',
329b2 27 4d 27 2c 27 4d 27 2c 27 45 27 2c 27 44 27 2c  'M','M','E','D',
329b3 27 49 27 2c 27 41 27 2c 27 54 27 2c 27 45 27 2c  'I','A','T','E',
329b4 0a 20 20 20 20 27 4a 27 2c 27 4f 27 2c 27 49 27  .    'J','O','I'
329b5 2c 27 4e 27 2c 27 53 27 2c 27 45 27 2c 27 52 27  ,'N','S','E','R'
329b6 2c 27 54 27 2c 27 4d 27 2c 27 41 27 2c 27 54 27  ,'T','M','A','T'
329b7 2c 27 43 27 2c 27 48 27 2c 27 50 27 2c 27 4c 27  ,'C','H','P','L'
329b8 2c 27 41 27 2c 27 4e 27 2c 27 41 27 2c 0a 20 20  ,'A','N','A',.  
329b9 20 20 27 4c 27 2c 27 59 27 2c 27 5a 27 2c 27 45    'L','Y','Z','E
329ba 27 2c 27 50 27 2c 27 52 27 2c 27 41 27 2c 27 47  ','P','R','A','G
329bb 27 2c 27 4d 27 2c 27 41 27 2c 27 42 27 2c 27 4f  ','M','A','B','O
329bc 27 2c 27 52 27 2c 27 54 27 2c 27 56 27 2c 27 41  ','R','T','V','A
329bd 27 2c 27 4c 27 2c 27 55 27 2c 0a 20 20 20 20 27  ','L','U',.    '
329be 45 27 2c 27 53 27 2c 27 56 27 2c 27 49 27 2c 27  E','S','V','I','
329bf 52 27 2c 27 54 27 2c 27 55 27 2c 27 41 27 2c 27  R','T','U','A','
329c0 4c 27 2c 27 49 27 2c 27 4d 27 2c 27 49 27 2c 27  L','I','M','I','
329c1 54 27 2c 27 57 27 2c 27 48 27 2c 27 45 27 2c 27  T','W','H','E','
329c2 4e 27 2c 27 57 27 2c 0a 20 20 20 20 27 48 27 2c  N','W',.    'H',
329c3 27 45 27 2c 27 52 27 2c 27 45 27 2c 27 4e 27 2c  'E','R','E','N',
329c4 27 41 27 2c 27 4d 27 2c 27 45 27 2c 27 41 27 2c  'A','M','E','A',
329c5 27 46 27 2c 27 54 27 2c 27 45 27 2c 27 52 27 2c  'F','T','E','R',
329c6 27 45 27 2c 27 50 27 2c 27 4c 27 2c 27 41 27 2c  'E','P','L','A',
329c7 27 43 27 2c 0a 20 20 20 20 27 45 27 2c 27 41 27  'C',.    'E','A'
329c8 2c 27 4e 27 2c 27 44 27 2c 27 45 27 2c 27 46 27  ,'N','D','E','F'
329c9 2c 27 41 27 2c 27 55 27 2c 27 4c 27 2c 27 54 27  ,'A','U','L','T'
329ca 2c 27 41 27 2c 27 55 27 2c 27 54 27 2c 27 4f 27  ,'A','U','T','O'
329cb 2c 27 49 27 2c 27 4e 27 2c 27 43 27 2c 27 52 27  ,'I','N','C','R'
329cc 2c 0a 20 20 20 20 27 45 27 2c 27 4d 27 2c 27 45  ,.    'E','M','E
329cd 27 2c 27 4e 27 2c 27 54 27 2c 27 43 27 2c 27 41  ','N','T','C','A
329ce 27 2c 27 53 27 2c 27 54 27 2c 27 43 27 2c 27 4f  ','S','T','C','O
329cf 27 2c 27 4c 27 2c 27 55 27 2c 27 4d 27 2c 27 4e  ','L','U','M','N
329d0 27 2c 27 43 27 2c 27 4f 27 2c 27 4d 27 2c 0a 20  ','C','O','M',. 
329d1 20 20 20 27 4d 27 2c 27 49 27 2c 27 54 27 2c 27     'M','I','T','
329d2 43 27 2c 27 4f 27 2c 27 4e 27 2c 27 46 27 2c 27  C','O','N','F','
329d3 4c 27 2c 27 49 27 2c 27 43 27 2c 27 54 27 2c 27  L','I','C','T','
329d4 43 27 2c 27 52 27 2c 27 4f 27 2c 27 53 27 2c 27  C','R','O','S','
329d5 53 27 2c 27 43 27 2c 27 55 27 2c 0a 20 20 20 20  S','C','U',.    
329d6 27 52 27 2c 27 52 27 2c 27 45 27 2c 27 4e 27 2c  'R','R','E','N',
329d7 27 54 27 2c 27 5f 27 2c 27 54 27 2c 27 49 27 2c  'T','_','T','I',
329d8 27 4d 27 2c 27 45 27 2c 27 53 27 2c 27 54 27 2c  'M','E','S','T',
329d9 27 41 27 2c 27 4d 27 2c 27 50 27 2c 27 52 27 2c  'A','M','P','R',
329da 27 49 27 2c 27 4d 27 2c 0a 20 20 20 20 27 41 27  'I','M',.    'A'
329db 2c 27 52 27 2c 27 59 27 2c 27 44 27 2c 27 45 27  ,'R','Y','D','E'
329dc 2c 27 46 27 2c 27 45 27 2c 27 52 27 2c 27 52 27  ,'F','E','R','R'
329dd 2c 27 45 27 2c 27 44 27 2c 27 49 27 2c 27 53 27  ,'E','D','I','S'
329de 2c 27 54 27 2c 27 49 27 2c 27 4e 27 2c 27 43 27  ,'T','I','N','C'
329df 2c 27 54 27 2c 0a 20 20 20 20 27 44 27 2c 27 52  ,'T',.    'D','R
329e0 27 2c 27 4f 27 2c 27 50 27 2c 27 46 27 2c 27 41  ','O','P','F','A
329e1 27 2c 27 49 27 2c 27 4c 27 2c 27 46 27 2c 27 52  ','I','L','F','R
329e2 27 2c 27 4f 27 2c 27 4d 27 2c 27 46 27 2c 27 55  ','O','M','F','U
329e3 27 2c 27 4c 27 2c 27 4c 27 2c 27 47 27 2c 27 4c  ','L','L','G','L
329e4 27 2c 0a 20 20 20 20 27 4f 27 2c 27 42 27 2c 27  ',.    'O','B','
329e5 59 27 2c 27 49 27 2c 27 46 27 2c 27 49 27 2c 27  Y','I','F','I','
329e6 53 27 2c 27 4e 27 2c 27 55 27 2c 27 4c 27 2c 27  S','N','U','L','
329e7 4c 27 2c 27 4f 27 2c 27 52 27 2c 27 44 27 2c 27  L','O','R','D','
329e8 45 27 2c 27 52 27 2c 27 45 27 2c 27 53 27 2c 0a  E','R','E','S',.
329e9 20 20 20 20 27 54 27 2c 27 52 27 2c 27 49 27 2c      'T','R','I',
329ea 27 43 27 2c 27 54 27 2c 27 4f 27 2c 27 55 27 2c  'C','T','O','U',
329eb 27 54 27 2c 27 45 27 2c 27 52 27 2c 27 49 27 2c  'T','E','R','I',
329ec 27 47 27 2c 27 48 27 2c 27 54 27 2c 27 52 27 2c  'G','H','T','R',
329ed 27 4f 27 2c 27 4c 27 2c 27 4c 27 2c 0a 20 20 20  'O','L','L',.   
329ee 20 27 42 27 2c 27 41 27 2c 27 43 27 2c 27 4b 27   'B','A','C','K'
329ef 2c 27 52 27 2c 27 4f 27 2c 27 57 27 2c 27 55 27  ,'R','O','W','U'
329f0 2c 27 4e 27 2c 27 49 27 2c 27 4f 27 2c 27 4e 27  ,'N','I','O','N'
329f1 2c 27 55 27 2c 27 53 27 2c 27 49 27 2c 27 4e 27  ,'U','S','I','N'
329f2 2c 27 47 27 2c 27 56 27 2c 0a 20 20 20 20 27 41  ,'G','V',.    'A
329f3 27 2c 27 43 27 2c 27 55 27 2c 27 55 27 2c 27 4d  ','C','U','U','M
329f4 27 2c 27 56 27 2c 27 49 27 2c 27 45 27 2c 27 57  ','V','I','E','W
329f5 27 2c 27 49 27 2c 27 4e 27 2c 27 49 27 2c 27 54  ','I','N','I','T
329f6 27 2c 27 49 27 2c 27 41 27 2c 27 4c 27 2c 27 4c  ','I','A','L','L
329f7 27 2c 27 59 27 2c 0a 20 20 7d 3b 0a 20 20 73 74  ','Y',.  };.  st
329f8 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
329f9 6e 65 64 20 63 68 61 72 20 61 48 61 73 68 5b 31  ned char aHash[1
329fa 32 37 5d 20 3d 20 7b 0a 20 20 20 20 20 20 37 32  27] = {.      72
329fb 2c 20 31 30 31 2c 20 31 31 34 2c 20 20 37 30 2c  , 101, 114,  70,
329fc 20 20 20 30 2c 20 20 34 34 2c 20 20 20 30 2c 20     0,  44,   0, 
329fd 20 20 30 2c 20 20 37 38 2c 20 20 20 30 2c 20 20    0,  78,   0,  
329fe 37 33 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20  73,   0,   0,.  
329ff 20 20 20 20 34 32 2c 20 20 31 32 2c 20 20 37 34      42,  12,  74
32a00 2c 20 20 31 35 2c 20 20 20 30 2c 20 31 31 33 2c  ,  15,   0, 113,
32a01 20 20 38 31 2c 20 20 35 30 2c 20 31 30 38 2c 20    81,  50, 108, 
32a02 20 20 30 2c 20 20 31 39 2c 20 20 20 30 2c 20 20    0,  19,   0,  
32a03 20 30 2c 0a 20 20 20 20 20 31 31 38 2c 20 20 20   0,.     118,   
32a04 30 2c 20 31 31 36 2c 20 31 31 31 2c 20 20 20 30  0, 116, 111,   0
32a05 2c 20 20 32 32 2c 20 20 38 39 2c 20 20 20 30 2c  ,  22,  89,   0,
32a06 20 20 20 39 2c 20 20 20 30 2c 20 20 20 30 2c 20     9,   0,   0, 
32a07 20 36 36 2c 20 20 36 37 2c 0a 20 20 20 20 20 20   66,  67,.      
32a08 20 30 2c 20 20 36 35 2c 20 20 20 36 2c 20 20 20   0,  65,   6,   
32a09 30 2c 20 20 34 38 2c 20 20 38 36 2c 20 20 39 38  0,  48,  86,  98
32a0a 2c 20 20 20 30 2c 20 31 31 35 2c 20 20 39 37 2c  ,   0, 115,  97,
32a0b 20 20 20 30 2c 20 20 20 30 2c 20 20 34 35 2c 0a     0,   0,  45,.
32a0c 20 20 20 20 20 20 20 30 2c 20 20 39 39 2c 20 20         0,  99,  
32a0d 32 34 2c 20 20 20 30 2c 20 20 31 37 2c 20 20 20  24,   0,  17,   
32a0e 30 2c 20 31 31 39 2c 20 20 34 39 2c 20 20 32 33  0, 119,  49,  23
32a0f 2c 20 20 20 30 2c 20 20 20 35 2c 20 31 30 36 2c  ,   0,   5, 106,
32a10 20 20 32 35 2c 0a 20 20 20 20 20 20 39 32 2c 20    25,.      92, 
32a11 20 20 30 2c 20 20 20 30 2c 20 31 32 31 2c 20 31    0,   0, 121, 1
32a12 30 32 2c 20 20 35 36 2c 20 31 32 30 2c 20 20 35  02,  56, 120,  5
32a13 33 2c 20 20 32 38 2c 20 20 35 31 2c 20 20 20 30  3,  28,  51,   0
32a14 2c 20 20 38 37 2c 20 20 20 30 2c 0a 20 20 20 20  ,  87,   0,.    
32a15 20 20 39 36 2c 20 20 32 36 2c 20 20 20 30 2c 20    96,  26,   0, 
32a16 20 39 35 2c 20 20 20 30 2c 20 20 20 30 2c 20 20   95,   0,   0,  
32a17 20 30 2c 20 20 39 31 2c 20 20 38 38 2c 20 20 39   0,  91,  88,  9
32a18 33 2c 20 20 38 34 2c 20 31 30 35 2c 20 20 31 34  3,  84, 105,  14
32a19 2c 0a 20 20 20 20 20 20 33 39 2c 20 31 30 34 2c  ,.      39, 104,
32a1a 20 20 20 30 2c 20 20 37 37 2c 20 20 20 30 2c 20     0,  77,   0, 
32a1b 20 31 38 2c 20 20 38 35 2c 20 31 30 37 2c 20 20   18,  85, 107,  
32a1c 33 32 2c 20 20 20 30 2c 20 31 31 37 2c 20 20 37  32,   0, 117,  7
32a1d 36 2c 20 31 30 39 2c 0a 20 20 20 20 20 20 35 39  6, 109,.      59
32a1e 2c 20 20 34 36 2c 20 20 38 30 2c 20 20 20 30 2c  ,  46,  80,   0,
32a1f 20 20 20 30 2c 20 20 39 30 2c 20 20 34 30 2c 20     0,  90,  40, 
32a20 20 20 30 2c 20 31 31 32 2c 20 20 20 30 2c 20 20    0, 112,   0,  
32a21 33 36 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20  36,   0,   0,.  
32a22 20 20 20 20 32 39 2c 20 20 20 30 2c 20 20 38 32      29,   0,  82
32a23 2c 20 20 35 38 2c 20 20 36 30 2c 20 20 20 30 2c  ,  58,  60,   0,
32a24 20 20 32 30 2c 20 20 35 37 2c 20 20 20 30 2c 20    20,  57,   0, 
32a25 20 35 32 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74   52,.  };.  stat
32a26 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  ic const unsigne
32a27 64 20 63 68 61 72 20 61 4e 65 78 74 5b 31 32 31  d char aNext[121
32a28 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 30 2c 20  ] = {.       0, 
32a29 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
32a2a 20 34 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   4,   0,   0,   
32a2b 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
32a2c 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20  ,   0,   0,.    
32a2d 20 20 20 30 2c 20 20 20 32 2c 20 20 20 30 2c 20     0,   2,   0, 
32a2e 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
32a2f 20 30 2c 20 20 20 30 2c 20 20 31 33 2c 20 20 20   0,   0,  13,   
32a30 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
32a31 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20 37 2c  ,.       0,   7,
32a32 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
32a33 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
32a34 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
32a35 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20 30  0,   0,.       0
32a36 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
32a37 20 20 20 30 2c 20 20 33 33 2c 20 20 32 31 2c 20     0,  33,  21, 
32a38 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
32a39 34 33 2c 20 20 20 33 2c 20 20 34 37 2c 0a 20 20  43,   3,  47,.  
32a3a 20 20 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30       0,   0,   0
32a3b 2c 20 20 20 30 2c 20 20 33 30 2c 20 20 35 34 2c  ,   0,  30,  54,
32a3c 20 20 20 30 2c 20 20 20 30 2c 20 20 33 38 2c 20     0,   0,  38, 
32a3d 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
32a3e 20 31 2c 0a 20 20 20 20 20 20 36 32 2c 20 20 20   1,.      62,   
32a3f 30 2c 20 20 20 30 2c 20 20 36 33 2c 20 20 20 30  0,   0,  63,   0
32a40 2c 20 20 34 31 2c 20 20 20 30 2c 20 20 20 30 2c  ,  41,   0,   0,
32a41 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
32a42 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20    0,   0,.      
32a43 36 31 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20  61,   0,   0,   
32a44 30 2c 20 20 20 30 2c 20 20 33 31 2c 20 20 35 35  0,   0,  31,  55
32a45 2c 20 20 31 36 2c 20 20 33 34 2c 20 20 31 30 2c  ,  16,  34,  10,
32a46 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a     0,   0,   0,.
32a47 20 20 20 20 20 20 20 30 2c 20 20 20 30 2c 20 20         0,   0,  
32a48 20 30 2c 20 20 20 30 2c 20 20 31 31 2c 20 20 36   0,   0,  11,  6
32a49 38 2c 20 20 37 35 2c 20 20 20 30 2c 20 20 20 38  8,  75,   0,   8
32a4a 2c 20 20 20 30 2c 20 31 30 30 2c 20 20 39 34 2c  ,   0, 100,  94,
32a4b 20 20 20 30 2c 0a 20 20 20 20 20 31 30 33 2c 20     0,.     103, 
32a4c 20 20 30 2c 20 20 38 33 2c 20 20 20 30 2c 20 20    0,  83,   0,  
32a4d 37 31 2c 20 20 20 30 2c 20 20 20 30 2c 20 31 31  71,   0,   0, 11
32a4e 30 2c 20 20 32 37 2c 20 20 33 37 2c 20 20 36 39  0,  27,  37,  69
32a4f 2c 20 20 37 39 2c 20 20 20 30 2c 0a 20 20 20 20  ,  79,   0,.    
32a50 20 20 33 35 2c 20 20 36 34 2c 20 20 20 30 2c 20    35,  64,   0, 
32a51 20 20 30 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74    0,.  };.  stat
32a52 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  ic const unsigne
32a53 64 20 63 68 61 72 20 61 4c 65 6e 5b 31 32 31 5d  d char aLen[121]
32a54 20 3d 20 7b 0a 20 20 20 20 20 20 20 37 2c 20 20   = {.       7,  
32a55 20 37 2c 20 20 20 35 2c 20 20 20 34 2c 20 20 20   7,   5,   4,   
32a56 36 2c 20 20 20 34 2c 20 20 20 35 2c 20 20 20 33  6,   4,   5,   3
32a57 2c 20 20 20 36 2c 20 20 20 37 2c 20 20 20 33 2c  ,   6,   7,   3,
32a58 20 20 20 36 2c 20 20 20 36 2c 0a 20 20 20 20 20     6,   6,.     
32a59 20 20 37 2c 20 20 20 37 2c 20 20 20 33 2c 20 20    7,   7,   3,  
32a5a 20 38 2c 20 20 20 32 2c 20 20 20 36 2c 20 20 20   8,   2,   6,   
32a5b 35 2c 20 20 20 34 2c 20 20 20 34 2c 20 20 20 33  5,   4,   4,   3
32a5c 2c 20 20 31 30 2c 20 20 20 34 2c 20 20 20 36 2c  ,  10,   4,   6,
32a5d 0a 20 20 20 20 20 20 31 31 2c 20 20 20 36 2c 20  .      11,   6, 
32a5e 20 20 32 2c 20 20 20 37 2c 20 20 20 35 2c 20 20    2,   7,   5,  
32a5f 20 35 2c 20 20 20 39 2c 20 20 20 36 2c 20 20 20   5,   9,   6,   
32a60 39 2c 20 20 20 39 2c 20 20 20 37 2c 20 20 31 30  9,   9,   7,  10
32a61 2c 20 20 31 30 2c 0a 20 20 20 20 20 20 20 34 2c  ,  10,.       4,
32a62 20 20 20 36 2c 20 20 20 32 2c 20 20 20 33 2c 20     6,   2,   3, 
32a63 20 20 34 2c 20 20 20 39 2c 20 20 20 32 2c 20 20    4,   9,   2,  
32a64 20 36 2c 20 20 20 35 2c 20 20 20 36 2c 20 20 20   6,   5,   6,   
32a65 36 2c 20 20 20 35 2c 20 20 20 36 2c 0a 20 20 20  6,   5,   6,.   
32a66 20 20 20 20 35 2c 20 20 20 35 2c 20 20 20 37 2c      5,   5,   7,
32a67 20 20 20 37 2c 20 20 20 37 2c 20 20 20 32 2c 20     7,   7,   2, 
32a68 20 20 33 2c 20 20 20 34 2c 20 20 20 34 2c 20 20    3,   4,   4,  
32a69 20 37 2c 20 20 20 33 2c 20 20 20 36 2c 20 20 20   7,   3,   6,   
32a6a 34 2c 0a 20 20 20 20 20 20 20 37 2c 20 20 20 36  4,.       7,   6
32a6b 2c 20 20 31 32 2c 20 20 20 36 2c 20 20 20 39 2c  ,  12,   6,   9,
32a6c 20 20 20 34 2c 20 20 20 36 2c 20 20 20 35 2c 20     4,   6,   5, 
32a6d 20 20 34 2c 20 20 20 37 2c 20 20 20 36 2c 20 20    4,   7,   6,  
32a6e 20 35 2c 20 20 20 36 2c 0a 20 20 20 20 20 20 20   5,   6,.       
32a6f 37 2c 20 20 20 35 2c 20 20 20 34 2c 20 20 20 35  7,   5,   4,   5
32a70 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20 37 2c  ,   6,   5,   7,
32a71 20 20 20 33 2c 20 20 20 37 2c 20 20 31 33 2c 20     3,   7,  13, 
32a72 20 20 32 2c 20 20 20 32 2c 20 20 20 34 2c 0a 20    2,   2,   4,. 
32a73 20 20 20 20 20 20 36 2c 20 20 20 36 2c 20 20 20        6,   6,   
32a74 38 2c 20 20 20 35 2c 20 20 31 37 2c 20 20 31 32  8,   5,  17,  12
32a75 2c 20 20 20 37 2c 20 20 20 38 2c 20 20 20 38 2c  ,   7,   8,   8,
32a76 20 20 20 32 2c 20 20 20 34 2c 20 20 20 34 2c 20     2,   4,   4, 
32a77 20 20 34 2c 0a 20 20 20 20 20 20 20 34 2c 20 20    4,.       4,  
32a78 20 34 2c 20 20 20 32 2c 20 20 20 32 2c 20 20 20   4,   2,   2,   
32a79 36 2c 20 20 20 35 2c 20 20 20 38 2c 20 20 20 35  6,   5,   8,   5
32a7a 2c 20 20 20 35 2c 20 20 20 38 2c 20 20 20 33 2c  ,   5,   8,   3,
32a7b 20 20 20 35 2c 20 20 20 35 2c 0a 20 20 20 20 20     5,   5,.     
32a7c 20 20 36 2c 20 20 20 34 2c 20 20 20 39 2c 20 20    6,   4,   9,  
32a7d 20 33 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69   3,.  };.  stati
32a7e 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  c const unsigned
32a7f 20 73 68 6f 72 74 20 69 6e 74 20 61 4f 66 66 73   short int aOffs
32a80 65 74 5b 31 32 31 5d 20 3d 20 7b 0a 20 20 20 20  et[121] = {.    
32a81 20 20 20 30 2c 20 20 20 32 2c 20 20 20 32 2c 20     0,   2,   2, 
32a82 20 20 38 2c 20 20 20 39 2c 20 20 31 34 2c 20 20    8,   9,  14,  
32a83 31 36 2c 20 20 32 30 2c 20 20 32 33 2c 20 20 32  16,  20,  23,  2
32a84 35 2c 20 20 32 35 2c 20 20 32 39 2c 20 20 33 33  5,  25,  29,  33
32a85 2c 0a 20 20 20 20 20 20 33 36 2c 20 20 34 31 2c  ,.      36,  41,
32a86 20 20 34 36 2c 20 20 34 38 2c 20 20 35 33 2c 20    46,  48,  53, 
32a87 20 35 34 2c 20 20 35 39 2c 20 20 36 32 2c 20 20   54,  59,  62,  
32a88 36 35 2c 20 20 36 37 2c 20 20 36 39 2c 20 20 37  65,  67,  69,  7
32a89 38 2c 20 20 38 31 2c 0a 20 20 20 20 20 20 38 36  8,  81,.      86
32a8a 2c 20 20 39 31 2c 20 20 39 35 2c 20 20 39 36 2c  ,  91,  95,  96,
32a8b 20 31 30 31 2c 20 31 30 35 2c 20 31 30 39 2c 20   101, 105, 109, 
32a8c 31 31 37 2c 20 31 32 32 2c 20 31 32 38 2c 20 31  117, 122, 128, 1
32a8d 33 36 2c 20 31 34 32 2c 20 31 35 32 2c 0a 20 20  36, 142, 152,.  
32a8e 20 20 20 31 35 39 2c 20 31 36 32 2c 20 31 36 32     159, 162, 162
32a8f 2c 20 31 36 35 2c 20 31 36 37 2c 20 31 36 37 2c  , 165, 167, 167,
32a90 20 31 37 31 2c 20 31 37 36 2c 20 31 37 39 2c 20   171, 176, 179, 
32a91 31 38 34 2c 20 31 38 39 2c 20 31 39 34 2c 20 31  184, 189, 194, 1
32a92 39 37 2c 0a 20 20 20 20 20 32 30 33 2c 20 32 30  97,.     203, 20
32a93 36 2c 20 32 31 30 2c 20 32 31 37 2c 20 32 32 33  6, 210, 217, 223
32a94 2c 20 32 32 33 2c 20 32 32 33 2c 20 32 32 36 2c  , 223, 223, 226,
32a95 20 32 32 39 2c 20 32 33 33 2c 20 32 33 34 2c 20   229, 233, 234, 
32a96 32 33 38 2c 20 32 34 34 2c 0a 20 20 20 20 20 32  238, 244,.     2
32a97 34 38 2c 20 32 35 35 2c 20 32 36 31 2c 20 32 37  48, 255, 261, 27
32a98 33 2c 20 32 37 39 2c 20 32 38 38 2c 20 32 39 30  3, 279, 288, 290
32a99 2c 20 32 39 36 2c 20 33 30 31 2c 20 33 30 33 2c  , 296, 301, 303,
32a9a 20 33 31 30 2c 20 33 31 35 2c 20 33 32 30 2c 0a   310, 315, 320,.
32a9b 20 20 20 20 20 33 32 36 2c 20 33 33 32 2c 20 33       326, 332, 3
32a9c 33 37 2c 20 33 34 31 2c 20 33 34 34 2c 20 33 35  37, 341, 344, 35
32a9d 30 2c 20 33 35 34 2c 20 33 36 31 2c 20 33 36 33  0, 354, 361, 363
32a9e 2c 20 33 37 30 2c 20 33 37 32 2c 20 33 37 34 2c  , 370, 372, 374,
32a9f 20 33 38 33 2c 0a 20 20 20 20 20 33 38 37 2c 20   383,.     387, 
32aa0 33 39 33 2c 20 33 39 39 2c 20 34 30 37 2c 20 34  393, 399, 407, 4
32aa1 31 32 2c 20 34 31 32 2c 20 34 32 38 2c 20 34 33  12, 412, 428, 43
32aa2 35 2c 20 34 34 32 2c 20 34 34 33 2c 20 34 35 30  5, 442, 443, 450
32aa3 2c 20 34 35 34 2c 20 34 35 38 2c 0a 20 20 20 20  , 454, 458,.    
32aa4 20 34 36 32 2c 20 34 36 36 2c 20 34 36 39 2c 20   462, 466, 469, 
32aa5 34 37 31 2c 20 34 37 33 2c 20 34 37 39 2c 20 34  471, 473, 479, 4
32aa6 38 33 2c 20 34 39 31 2c 20 34 39 35 2c 20 35 30  83, 491, 495, 50
32aa7 30 2c 20 35 30 38 2c 20 35 31 31 2c 20 35 31 36  0, 508, 511, 516
32aa8 2c 0a 20 20 20 20 20 35 32 31 2c 20 35 32 37 2c  ,.     521, 527,
32aa9 20 35 33 31 2c 20 35 33 36 2c 0a 20 20 7d 3b 0a   531, 536,.  };.
32aaa 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
32aab 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 43 6f  nsigned char aCo
32aac 64 65 5b 31 32 31 5d 20 3d 20 7b 0a 20 20 20 20  de[121] = {.    
32aad 54 4b 5f 52 45 49 4e 44 45 58 2c 20 20 20 20 54  TK_REINDEX,    T
32aae 4b 5f 49 4e 44 45 58 45 44 2c 20 20 20 20 54 4b  K_INDEXED,    TK
32aaf 5f 49 4e 44 45 58 2c 20 20 20 20 20 20 54 4b 5f  _INDEX,      TK_
32ab0 44 45 53 43 2c 20 20 20 20 20 20 20 54 4b 5f 45  DESC,       TK_E
32ab1 53 43 41 50 45 2c 20 20 20 20 20 0a 20 20 20 20  SCAPE,     .    
32ab2 54 4b 5f 45 41 43 48 2c 20 20 20 20 20 20 20 54  TK_EACH,       T
32ab3 4b 5f 43 48 45 43 4b 2c 20 20 20 20 20 20 54 4b  K_CHECK,      TK
32ab4 5f 4b 45 59 2c 20 20 20 20 20 20 20 20 54 4b 5f  _KEY,        TK_
32ab5 42 45 46 4f 52 45 2c 20 20 20 20 20 54 4b 5f 46  BEFORE,     TK_F
32ab6 4f 52 45 49 47 4e 2c 20 20 20 20 0a 20 20 20 20  OREIGN,    .    
32ab7 54 4b 5f 46 4f 52 2c 20 20 20 20 20 20 20 20 54  TK_FOR,        T
32ab8 4b 5f 49 47 4e 4f 52 45 2c 20 20 20 20 20 54 4b  K_IGNORE,     TK
32ab9 5f 4c 49 4b 45 5f 4b 57 2c 20 20 20 20 54 4b 5f  _LIKE_KW,    TK_
32aba 45 58 50 4c 41 49 4e 2c 20 20 20 20 54 4b 5f 49  EXPLAIN,    TK_I
32abb 4e 53 54 45 41 44 2c 20 20 20 20 0a 20 20 20 20  NSTEAD,    .    
32abc 54 4b 5f 41 44 44 2c 20 20 20 20 20 20 20 20 54  TK_ADD,        T
32abd 4b 5f 44 41 54 41 42 41 53 45 2c 20 20 20 54 4b  K_DATABASE,   TK
32abe 5f 41 53 2c 20 20 20 20 20 20 20 20 20 54 4b 5f  _AS,         TK_
32abf 53 45 4c 45 43 54 2c 20 20 20 20 20 54 4b 5f 54  SELECT,     TK_T
32ac0 41 42 4c 45 2c 20 20 20 20 20 20 0a 20 20 20 20  ABLE,      .    
32ac1 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54  TK_JOIN_KW,    T
32ac2 4b 5f 54 48 45 4e 2c 20 20 20 20 20 20 20 54 4b  K_THEN,       TK
32ac3 5f 45 4e 44 2c 20 20 20 20 20 20 20 20 54 4b 5f  _END,        TK_
32ac4 44 45 46 45 52 52 41 42 4c 45 2c 20 54 4b 5f 45  DEFERRABLE, TK_E
32ac5 4c 53 45 2c 20 20 20 20 20 20 20 0a 20 20 20 20  LSE,       .    
32ac6 54 4b 5f 45 58 43 45 50 54 2c 20 20 20 20 20 54  TK_EXCEPT,     T
32ac7 4b 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 54 4b  K_TRANSACTION,TK
32ac8 5f 41 43 54 49 4f 4e 2c 20 20 20 20 20 54 4b 5f  _ACTION,     TK_
32ac9 4f 4e 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 4a  ON,         TK_J
32aca 4f 49 4e 5f 4b 57 2c 20 20 20 20 0a 20 20 20 20  OIN_KW,    .    
32acb 54 4b 5f 41 4c 54 45 52 2c 20 20 20 20 20 20 54  TK_ALTER,      T
32acc 4b 5f 52 41 49 53 45 2c 20 20 20 20 20 20 54 4b  K_RAISE,      TK
32acd 5f 45 58 43 4c 55 53 49 56 45 2c 20 20 54 4b 5f  _EXCLUSIVE,  TK_
32ace 45 58 49 53 54 53 2c 20 20 20 20 20 54 4b 5f 53  EXISTS,     TK_S
32acf 41 56 45 50 4f 49 4e 54 2c 20 20 0a 20 20 20 20  AVEPOINT,  .    
32ad0 54 4b 5f 49 4e 54 45 52 53 45 43 54 2c 20 20 54  TK_INTERSECT,  T
32ad1 4b 5f 54 52 49 47 47 45 52 2c 20 20 20 20 54 4b  K_TRIGGER,    TK
32ad2 5f 52 45 46 45 52 45 4e 43 45 53 2c 20 54 4b 5f  _REFERENCES, TK_
32ad3 43 4f 4e 53 54 52 41 49 4e 54 2c 20 54 4b 5f 49  CONSTRAINT, TK_I
32ad4 4e 54 4f 2c 20 20 20 20 20 20 20 0a 20 20 20 20  NTO,       .    
32ad5 54 4b 5f 4f 46 46 53 45 54 2c 20 20 20 20 20 54  TK_OFFSET,     T
32ad6 4b 5f 4f 46 2c 20 20 20 20 20 20 20 20 20 54 4b  K_OF,         TK
32ad7 5f 53 45 54 2c 20 20 20 20 20 20 20 20 54 4b 5f  _SET,        TK_
32ad8 54 45 4d 50 2c 20 20 20 20 20 20 20 54 4b 5f 54  TEMP,       TK_T
32ad9 45 4d 50 2c 20 20 20 20 20 20 20 0a 20 20 20 20  EMP,       .    
32ada 54 4b 5f 4f 52 2c 20 20 20 20 20 20 20 20 20 54  TK_OR,         T
32adb 4b 5f 55 4e 49 51 55 45 2c 20 20 20 20 20 54 4b  K_UNIQUE,     TK
32adc 5f 51 55 45 52 59 2c 20 20 20 20 20 20 54 4b 5f  _QUERY,      TK_
32add 41 54 54 41 43 48 2c 20 20 20 20 20 54 4b 5f 48  ATTACH,     TK_H
32ade 41 56 49 4e 47 2c 20 20 20 20 20 0a 20 20 20 20  AVING,     .    
32adf 54 4b 5f 47 52 4f 55 50 2c 20 20 20 20 20 20 54  TK_GROUP,      T
32ae0 4b 5f 55 50 44 41 54 45 2c 20 20 20 20 20 54 4b  K_UPDATE,     TK
32ae1 5f 42 45 47 49 4e 2c 20 20 20 20 20 20 54 4b 5f  _BEGIN,      TK_
32ae2 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f 52  JOIN_KW,    TK_R
32ae3 45 4c 45 41 53 45 2c 20 20 20 20 0a 20 20 20 20  ELEASE,    .    
32ae4 54 4b 5f 42 45 54 57 45 45 4e 2c 20 20 20 20 54  TK_BETWEEN,    T
32ae5 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 20 20 20 54 4b  K_NOTNULL,    TK
32ae6 5f 4e 4f 2c 20 20 20 20 20 20 20 20 20 54 4b 5f  _NO,         TK_
32ae7 4e 4f 54 2c 20 20 20 20 20 20 20 20 54 4b 5f 4e  NOT,        TK_N
32ae8 55 4c 4c 2c 20 20 20 20 20 20 20 0a 20 20 20 20  ULL,       .    
32ae9 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20 20 20 54  TK_LIKE_KW,    T
32aea 4b 5f 43 41 53 43 41 44 45 2c 20 20 20 20 54 4b  K_CASCADE,    TK
32aeb 5f 41 53 43 2c 20 20 20 20 20 20 20 20 54 4b 5f  _ASC,        TK_
32aec 44 45 4c 45 54 45 2c 20 20 20 20 20 54 4b 5f 43  DELETE,     TK_C
32aed 41 53 45 2c 20 20 20 20 20 20 20 0a 20 20 20 20  ASE,       .    
32aee 54 4b 5f 43 4f 4c 4c 41 54 45 2c 20 20 20 20 54  TK_COLLATE,    T
32aef 4b 5f 43 52 45 41 54 45 2c 20 20 20 20 20 54 4b  K_CREATE,     TK
32af0 5f 43 54 49 4d 45 5f 4b 57 2c 20 20 20 54 4b 5f  _CTIME_KW,   TK_
32af1 44 45 54 41 43 48 2c 20 20 20 20 20 54 4b 5f 49  DETACH,     TK_I
32af2 4d 4d 45 44 49 41 54 45 2c 20 20 0a 20 20 20 20  MMEDIATE,  .    
32af3 54 4b 5f 4a 4f 49 4e 2c 20 20 20 20 20 20 20 54  TK_JOIN,       T
32af4 4b 5f 49 4e 53 45 52 54 2c 20 20 20 20 20 54 4b  K_INSERT,     TK
32af5 5f 4d 41 54 43 48 2c 20 20 20 20 20 20 54 4b 5f  _MATCH,      TK_
32af6 50 4c 41 4e 2c 20 20 20 20 20 20 20 54 4b 5f 41  PLAN,       TK_A
32af7 4e 41 4c 59 5a 45 2c 20 20 20 20 0a 20 20 20 20  NALYZE,    .    
32af8 54 4b 5f 50 52 41 47 4d 41 2c 20 20 20 20 20 54  TK_PRAGMA,     T
32af9 4b 5f 41 42 4f 52 54 2c 20 20 20 20 20 20 54 4b  K_ABORT,      TK
32afa 5f 56 41 4c 55 45 53 2c 20 20 20 20 20 54 4b 5f  _VALUES,     TK_
32afb 56 49 52 54 55 41 4c 2c 20 20 20 20 54 4b 5f 4c  VIRTUAL,    TK_L
32afc 49 4d 49 54 2c 20 20 20 20 20 20 0a 20 20 20 20  IMIT,      .    
32afd 54 4b 5f 57 48 45 4e 2c 20 20 20 20 20 20 20 54  TK_WHEN,       T
32afe 4b 5f 57 48 45 52 45 2c 20 20 20 20 20 20 54 4b  K_WHERE,      TK
32aff 5f 52 45 4e 41 4d 45 2c 20 20 20 20 20 54 4b 5f  _RENAME,     TK_
32b00 41 46 54 45 52 2c 20 20 20 20 20 20 54 4b 5f 52  AFTER,      TK_R
32b01 45 50 4c 41 43 45 2c 20 20 20 20 0a 20 20 20 20  EPLACE,    .    
32b02 54 4b 5f 41 4e 44 2c 20 20 20 20 20 20 20 20 54  TK_AND,        T
32b03 4b 5f 44 45 46 41 55 4c 54 2c 20 20 20 20 54 4b  K_DEFAULT,    TK
32b04 5f 41 55 54 4f 49 4e 43 52 2c 20 20 20 54 4b 5f  _AUTOINCR,   TK_
32b05 54 4f 2c 20 20 20 20 20 20 20 20 20 54 4b 5f 49  TO,         TK_I
32b06 4e 2c 20 20 20 20 20 20 20 20 20 0a 20 20 20 20  N,         .    
32b07 54 4b 5f 43 41 53 54 2c 20 20 20 20 20 20 20 54  TK_CAST,       T
32b08 4b 5f 43 4f 4c 55 4d 4e 4b 57 2c 20 20 20 54 4b  K_COLUMNKW,   TK
32b09 5f 43 4f 4d 4d 49 54 2c 20 20 20 20 20 54 4b 5f  _COMMIT,     TK_
32b0a 43 4f 4e 46 4c 49 43 54 2c 20 20 20 54 4b 5f 4a  CONFLICT,   TK_J
32b0b 4f 49 4e 5f 4b 57 2c 20 20 20 20 0a 20 20 20 20  OIN_KW,    .    
32b0c 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20 20 20 54  TK_CTIME_KW,   T
32b0d 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20 20 20 54 4b  K_CTIME_KW,   TK
32b0e 5f 50 52 49 4d 41 52 59 2c 20 20 20 20 54 4b 5f  _PRIMARY,    TK_
32b0f 44 45 46 45 52 52 45 44 2c 20 20 20 54 4b 5f 44  DEFERRED,   TK_D
32b10 49 53 54 49 4e 43 54 2c 20 20 20 0a 20 20 20 20  ISTINCT,   .    
32b11 54 4b 5f 49 53 2c 20 20 20 20 20 20 20 20 20 54  TK_IS,         T
32b12 4b 5f 44 52 4f 50 2c 20 20 20 20 20 20 20 54 4b  K_DROP,       TK
32b13 5f 46 41 49 4c 2c 20 20 20 20 20 20 20 54 4b 5f  _FAIL,       TK_
32b14 46 52 4f 4d 2c 20 20 20 20 20 20 20 54 4b 5f 4a  FROM,       TK_J
32b15 4f 49 4e 5f 4b 57 2c 20 20 20 20 0a 20 20 20 20  OIN_KW,    .    
32b16 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20 20 20 54  TK_LIKE_KW,    T
32b17 4b 5f 42 59 2c 20 20 20 20 20 20 20 20 20 54 4b  K_BY,         TK
32b18 5f 49 46 2c 20 20 20 20 20 20 20 20 20 54 4b 5f  _IF,         TK_
32b19 49 53 4e 55 4c 4c 2c 20 20 20 20 20 54 4b 5f 4f  ISNULL,     TK_O
32b1a 52 44 45 52 2c 20 20 20 20 20 20 0a 20 20 20 20  RDER,      .    
32b1b 54 4b 5f 52 45 53 54 52 49 43 54 2c 20 20 20 54  TK_RESTRICT,   T
32b1c 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b  K_JOIN_KW,    TK
32b1d 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 54 4b 5f  _JOIN_KW,    TK_
32b1e 52 4f 4c 4c 42 41 43 4b 2c 20 20 20 54 4b 5f 52  ROLLBACK,   TK_R
32b1f 4f 57 2c 20 20 20 20 20 20 20 20 0a 20 20 20 20  OW,        .    
32b20 54 4b 5f 55 4e 49 4f 4e 2c 20 20 20 20 20 20 54  TK_UNION,      T
32b21 4b 5f 55 53 49 4e 47 2c 20 20 20 20 20 20 54 4b  K_USING,      TK
32b22 5f 56 41 43 55 55 4d 2c 20 20 20 20 20 54 4b 5f  _VACUUM,     TK_
32b23 56 49 45 57 2c 20 20 20 20 20 20 20 54 4b 5f 49  VIEW,       TK_I
32b24 4e 49 54 49 41 4c 4c 59 2c 20 20 0a 20 20 20 20  NITIALLY,  .    
32b25 54 4b 5f 41 4c 4c 2c 20 20 20 20 20 20 20 20 0a  TK_ALL,        .
32b26 20 20 7d 3b 0a 20 20 69 6e 74 20 68 2c 20 69 3b    };.  int h, i;
32b27 0a 20 20 69 66 28 20 6e 3c 32 20 29 20 72 65 74  .  if( n<2 ) ret
32b28 75 72 6e 20 54 4b 5f 49 44 3b 0a 20 20 68 20 3d  urn TK_ID;.  h =
32b29 20 28 28 63 68 61 72 4d 61 70 28 7a 5b 30 5d 29   ((charMap(z[0])
32b2a 2a 34 29 20 5e 0a 20 20 20 20 20 20 28 63 68 61  *4) ^.      (cha
32b2b 72 4d 61 70 28 7a 5b 6e 2d 31 5d 29 2a 33 29 20  rMap(z[n-1])*3) 
32b2c 5e 0a 20 20 20 20 20 20 6e 29 20 25 20 31 32 37  ^.      n) % 127
32b2d 3b 0a 20 20 66 6f 72 28 69 3d 28 28 69 6e 74 29  ;.  for(i=((int)
32b2e 61 48 61 73 68 5b 68 5d 29 2d 31 3b 20 69 3e 3d  aHash[h])-1; i>=
32b2f 30 3b 20 69 3d 28 28 69 6e 74 29 61 4e 65 78 74  0; i=((int)aNext
32b30 5b 69 5d 29 2d 31 29 7b 0a 20 20 20 20 69 66 28  [i])-1){.    if(
32b31 20 61 4c 65 6e 5b 69 5d 3d 3d 6e 20 26 26 20 73   aLen[i]==n && s
32b32 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 26  qlite3StrNICmp(&
32b33 7a 54 65 78 74 5b 61 4f 66 66 73 65 74 5b 69 5d  zText[aOffset[i]
32b34 5d 2c 7a 2c 6e 29 3d 3d 30 20 29 7b 0a 20 20 20  ],z,n)==0 ){.   
32b35 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32b36 30 20 29 3b 20 2f 2a 20 52 45 49 4e 44 45 58 20  0 ); /* REINDEX 
32b37 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32b38 65 28 20 69 3d 3d 31 20 29 3b 20 2f 2a 20 49 4e  e( i==1 ); /* IN
32b39 44 45 58 45 44 20 2a 2f 0a 20 20 20 20 20 20 74  DEXED */.      t
32b3a 65 73 74 63 61 73 65 28 20 69 3d 3d 32 20 29 3b  estcase( i==2 );
32b3b 20 2f 2a 20 49 4e 44 45 58 20 2a 2f 0a 20 20 20   /* INDEX */.   
32b3c 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32b3d 33 20 29 3b 20 2f 2a 20 44 45 53 43 20 2a 2f 0a  3 ); /* DESC */.
32b3e 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32b3f 69 3d 3d 34 20 29 3b 20 2f 2a 20 45 53 43 41 50  i==4 ); /* ESCAP
32b40 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
32b41 61 73 65 28 20 69 3d 3d 35 20 29 3b 20 2f 2a 20  ase( i==5 ); /* 
32b42 45 41 43 48 20 2a 2f 0a 20 20 20 20 20 20 74 65  EACH */.      te
32b43 73 74 63 61 73 65 28 20 69 3d 3d 36 20 29 3b 20  stcase( i==6 ); 
32b44 2f 2a 20 43 48 45 43 4b 20 2a 2f 0a 20 20 20 20  /* CHECK */.    
32b45 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37    testcase( i==7
32b46 20 29 3b 20 2f 2a 20 4b 45 59 20 2a 2f 0a 20 20   ); /* KEY */.  
32b47 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32b48 3d 38 20 29 3b 20 2f 2a 20 42 45 46 4f 52 45 20  =8 ); /* BEFORE 
32b49 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32b4a 65 28 20 69 3d 3d 39 20 29 3b 20 2f 2a 20 46 4f  e( i==9 ); /* FO
32b4b 52 45 49 47 4e 20 2a 2f 0a 20 20 20 20 20 20 74  REIGN */.      t
32b4c 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 20 29  estcase( i==10 )
32b4d 3b 20 2f 2a 20 46 4f 52 20 2a 2f 0a 20 20 20 20  ; /* FOR */.    
32b4e 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
32b4f 31 20 29 3b 20 2f 2a 20 49 47 4e 4f 52 45 20 2a  1 ); /* IGNORE *
32b50 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32b51 28 20 69 3d 3d 31 32 20 29 3b 20 2f 2a 20 52 45  ( i==12 ); /* RE
32b52 47 45 58 50 20 2a 2f 0a 20 20 20 20 20 20 74 65  GEXP */.      te
32b53 73 74 63 61 73 65 28 20 69 3d 3d 31 33 20 29 3b  stcase( i==13 );
32b54 20 2f 2a 20 45 58 50 4c 41 49 4e 20 2a 2f 0a 20   /* EXPLAIN */. 
32b55 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32b56 3d 3d 31 34 20 29 3b 20 2f 2a 20 49 4e 53 54 45  ==14 ); /* INSTE
32b57 41 44 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  AD */.      test
32b58 63 61 73 65 28 20 69 3d 3d 31 35 20 29 3b 20 2f  case( i==15 ); /
32b59 2a 20 41 44 44 20 2a 2f 0a 20 20 20 20 20 20 74  * ADD */.      t
32b5a 65 73 74 63 61 73 65 28 20 69 3d 3d 31 36 20 29  estcase( i==16 )
32b5b 3b 20 2f 2a 20 44 41 54 41 42 41 53 45 20 2a 2f  ; /* DATABASE */
32b5c 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32b5d 20 69 3d 3d 31 37 20 29 3b 20 2f 2a 20 41 53 20   i==17 ); /* AS 
32b5e 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32b5f 65 28 20 69 3d 3d 31 38 20 29 3b 20 2f 2a 20 53  e( i==18 ); /* S
32b60 45 4c 45 43 54 20 2a 2f 0a 20 20 20 20 20 20 74  ELECT */.      t
32b61 65 73 74 63 61 73 65 28 20 69 3d 3d 31 39 20 29  estcase( i==19 )
32b62 3b 20 2f 2a 20 54 41 42 4c 45 20 2a 2f 0a 20 20  ; /* TABLE */.  
32b63 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32b64 3d 32 30 20 29 3b 20 2f 2a 20 4c 45 46 54 20 2a  =20 ); /* LEFT *
32b65 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32b66 28 20 69 3d 3d 32 31 20 29 3b 20 2f 2a 20 54 48  ( i==21 ); /* TH
32b67 45 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  EN */.      test
32b68 63 61 73 65 28 20 69 3d 3d 32 32 20 29 3b 20 2f  case( i==22 ); /
32b69 2a 20 45 4e 44 20 2a 2f 0a 20 20 20 20 20 20 74  * END */.      t
32b6a 65 73 74 63 61 73 65 28 20 69 3d 3d 32 33 20 29  estcase( i==23 )
32b6b 3b 20 2f 2a 20 44 45 46 45 52 52 41 42 4c 45 20  ; /* DEFERRABLE 
32b6c 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32b6d 65 28 20 69 3d 3d 32 34 20 29 3b 20 2f 2a 20 45  e( i==24 ); /* E
32b6e 4c 53 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  LSE */.      tes
32b6f 74 63 61 73 65 28 20 69 3d 3d 32 35 20 29 3b 20  tcase( i==25 ); 
32b70 2f 2a 20 45 58 43 45 50 54 20 2a 2f 0a 20 20 20  /* EXCEPT */.   
32b71 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32b72 32 36 20 29 3b 20 2f 2a 20 54 52 41 4e 53 41 43  26 ); /* TRANSAC
32b73 54 49 4f 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65  TION */.      te
32b74 73 74 63 61 73 65 28 20 69 3d 3d 32 37 20 29 3b  stcase( i==27 );
32b75 20 2f 2a 20 41 43 54 49 4f 4e 20 2a 2f 0a 20 20   /* ACTION */.  
32b76 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32b77 3d 32 38 20 29 3b 20 2f 2a 20 4f 4e 20 2a 2f 0a  =28 ); /* ON */.
32b78 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32b79 69 3d 3d 32 39 20 29 3b 20 2f 2a 20 4e 41 54 55  i==29 ); /* NATU
32b7a 52 41 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  RAL */.      tes
32b7b 74 63 61 73 65 28 20 69 3d 3d 33 30 20 29 3b 20  tcase( i==30 ); 
32b7c 2f 2a 20 41 4c 54 45 52 20 2a 2f 0a 20 20 20 20  /* ALTER */.    
32b7d 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33    testcase( i==3
32b7e 31 20 29 3b 20 2f 2a 20 52 41 49 53 45 20 2a 2f  1 ); /* RAISE */
32b7f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32b80 20 69 3d 3d 33 32 20 29 3b 20 2f 2a 20 45 58 43   i==32 ); /* EXC
32b81 4c 55 53 49 56 45 20 2a 2f 0a 20 20 20 20 20 20  LUSIVE */.      
32b82 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33 33 20  testcase( i==33 
32b83 29 3b 20 2f 2a 20 45 58 49 53 54 53 20 2a 2f 0a  ); /* EXISTS */.
32b84 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32b85 69 3d 3d 33 34 20 29 3b 20 2f 2a 20 53 41 56 45  i==34 ); /* SAVE
32b86 50 4f 49 4e 54 20 2a 2f 0a 20 20 20 20 20 20 74  POINT */.      t
32b87 65 73 74 63 61 73 65 28 20 69 3d 3d 33 35 20 29  estcase( i==35 )
32b88 3b 20 2f 2a 20 49 4e 54 45 52 53 45 43 54 20 2a  ; /* INTERSECT *
32b89 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32b8a 28 20 69 3d 3d 33 36 20 29 3b 20 2f 2a 20 54 52  ( i==36 ); /* TR
32b8b 49 47 47 45 52 20 2a 2f 0a 20 20 20 20 20 20 74  IGGER */.      t
32b8c 65 73 74 63 61 73 65 28 20 69 3d 3d 33 37 20 29  estcase( i==37 )
32b8d 3b 20 2f 2a 20 52 45 46 45 52 45 4e 43 45 53 20  ; /* REFERENCES 
32b8e 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32b8f 65 28 20 69 3d 3d 33 38 20 29 3b 20 2f 2a 20 43  e( i==38 ); /* C
32b90 4f 4e 53 54 52 41 49 4e 54 20 2a 2f 0a 20 20 20  ONSTRAINT */.   
32b91 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32b92 33 39 20 29 3b 20 2f 2a 20 49 4e 54 4f 20 2a 2f  39 ); /* INTO */
32b93 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32b94 20 69 3d 3d 34 30 20 29 3b 20 2f 2a 20 4f 46 46   i==40 ); /* OFF
32b95 53 45 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  SET */.      tes
32b96 74 63 61 73 65 28 20 69 3d 3d 34 31 20 29 3b 20  tcase( i==41 ); 
32b97 2f 2a 20 4f 46 20 2a 2f 0a 20 20 20 20 20 20 74  /* OF */.      t
32b98 65 73 74 63 61 73 65 28 20 69 3d 3d 34 32 20 29  estcase( i==42 )
32b99 3b 20 2f 2a 20 53 45 54 20 2a 2f 0a 20 20 20 20  ; /* SET */.    
32b9a 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34    testcase( i==4
32b9b 33 20 29 3b 20 2f 2a 20 54 45 4d 50 20 2a 2f 0a  3 ); /* TEMP */.
32b9c 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32b9d 69 3d 3d 34 34 20 29 3b 20 2f 2a 20 54 45 4d 50  i==44 ); /* TEMP
32b9e 4f 52 41 52 59 20 2a 2f 0a 20 20 20 20 20 20 74  ORARY */.      t
32b9f 65 73 74 63 61 73 65 28 20 69 3d 3d 34 35 20 29  estcase( i==45 )
32ba0 3b 20 2f 2a 20 4f 52 20 2a 2f 0a 20 20 20 20 20  ; /* OR */.     
32ba1 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34 36   testcase( i==46
32ba2 20 29 3b 20 2f 2a 20 55 4e 49 51 55 45 20 2a 2f   ); /* UNIQUE */
32ba3 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32ba4 20 69 3d 3d 34 37 20 29 3b 20 2f 2a 20 51 55 45   i==47 ); /* QUE
32ba5 52 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  RY */.      test
32ba6 63 61 73 65 28 20 69 3d 3d 34 38 20 29 3b 20 2f  case( i==48 ); /
32ba7 2a 20 41 54 54 41 43 48 20 2a 2f 0a 20 20 20 20  * ATTACH */.    
32ba8 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34    testcase( i==4
32ba9 39 20 29 3b 20 2f 2a 20 48 41 56 49 4e 47 20 2a  9 ); /* HAVING *
32baa 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32bab 28 20 69 3d 3d 35 30 20 29 3b 20 2f 2a 20 47 52  ( i==50 ); /* GR
32bac 4f 55 50 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  OUP */.      tes
32bad 74 63 61 73 65 28 20 69 3d 3d 35 31 20 29 3b 20  tcase( i==51 ); 
32bae 2f 2a 20 55 50 44 41 54 45 20 2a 2f 0a 20 20 20  /* UPDATE */.   
32baf 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32bb0 35 32 20 29 3b 20 2f 2a 20 42 45 47 49 4e 20 2a  52 ); /* BEGIN *
32bb1 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32bb2 28 20 69 3d 3d 35 33 20 29 3b 20 2f 2a 20 49 4e  ( i==53 ); /* IN
32bb3 4e 45 52 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  NER */.      tes
32bb4 74 63 61 73 65 28 20 69 3d 3d 35 34 20 29 3b 20  tcase( i==54 ); 
32bb5 2f 2a 20 52 45 4c 45 41 53 45 20 2a 2f 0a 20 20  /* RELEASE */.  
32bb6 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32bb7 3d 35 35 20 29 3b 20 2f 2a 20 42 45 54 57 45 45  =55 ); /* BETWEE
32bb8 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  N */.      testc
32bb9 61 73 65 28 20 69 3d 3d 35 36 20 29 3b 20 2f 2a  ase( i==56 ); /*
32bba 20 4e 4f 54 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20   NOTNULL */.    
32bbb 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35    testcase( i==5
32bbc 37 20 29 3b 20 2f 2a 20 4e 4f 20 2a 2f 0a 20 20  7 ); /* NO */.  
32bbd 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32bbe 3d 35 38 20 29 3b 20 2f 2a 20 4e 4f 54 20 2a 2f  =58 ); /* NOT */
32bbf 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32bc0 20 69 3d 3d 35 39 20 29 3b 20 2f 2a 20 4e 55 4c   i==59 ); /* NUL
32bc1 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  L */.      testc
32bc2 61 73 65 28 20 69 3d 3d 36 30 20 29 3b 20 2f 2a  ase( i==60 ); /*
32bc3 20 4c 49 4b 45 20 2a 2f 0a 20 20 20 20 20 20 74   LIKE */.      t
32bc4 65 73 74 63 61 73 65 28 20 69 3d 3d 36 31 20 29  estcase( i==61 )
32bc5 3b 20 2f 2a 20 43 41 53 43 41 44 45 20 2a 2f 0a  ; /* CASCADE */.
32bc6 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32bc7 69 3d 3d 36 32 20 29 3b 20 2f 2a 20 41 53 43 20  i==62 ); /* ASC 
32bc8 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32bc9 65 28 20 69 3d 3d 36 33 20 29 3b 20 2f 2a 20 44  e( i==63 ); /* D
32bca 45 4c 45 54 45 20 2a 2f 0a 20 20 20 20 20 20 74  ELETE */.      t
32bcb 65 73 74 63 61 73 65 28 20 69 3d 3d 36 34 20 29  estcase( i==64 )
32bcc 3b 20 2f 2a 20 43 41 53 45 20 2a 2f 0a 20 20 20  ; /* CASE */.   
32bcd 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32bce 36 35 20 29 3b 20 2f 2a 20 43 4f 4c 4c 41 54 45  65 ); /* COLLATE
32bcf 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32bd0 73 65 28 20 69 3d 3d 36 36 20 29 3b 20 2f 2a 20  se( i==66 ); /* 
32bd1 43 52 45 41 54 45 20 2a 2f 0a 20 20 20 20 20 20  CREATE */.      
32bd2 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 37 20  testcase( i==67 
32bd3 29 3b 20 2f 2a 20 43 55 52 52 45 4e 54 5f 44 41  ); /* CURRENT_DA
32bd4 54 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  TE */.      test
32bd5 63 61 73 65 28 20 69 3d 3d 36 38 20 29 3b 20 2f  case( i==68 ); /
32bd6 2a 20 44 45 54 41 43 48 20 2a 2f 0a 20 20 20 20  * DETACH */.    
32bd7 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36    testcase( i==6
32bd8 39 20 29 3b 20 2f 2a 20 49 4d 4d 45 44 49 41 54  9 ); /* IMMEDIAT
32bd9 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
32bda 61 73 65 28 20 69 3d 3d 37 30 20 29 3b 20 2f 2a  ase( i==70 ); /*
32bdb 20 4a 4f 49 4e 20 2a 2f 0a 20 20 20 20 20 20 74   JOIN */.      t
32bdc 65 73 74 63 61 73 65 28 20 69 3d 3d 37 31 20 29  estcase( i==71 )
32bdd 3b 20 2f 2a 20 49 4e 53 45 52 54 20 2a 2f 0a 20  ; /* INSERT */. 
32bde 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32bdf 3d 3d 37 32 20 29 3b 20 2f 2a 20 4d 41 54 43 48  ==72 ); /* MATCH
32be0 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32be1 73 65 28 20 69 3d 3d 37 33 20 29 3b 20 2f 2a 20  se( i==73 ); /* 
32be2 50 4c 41 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65  PLAN */.      te
32be3 73 74 63 61 73 65 28 20 69 3d 3d 37 34 20 29 3b  stcase( i==74 );
32be4 20 2f 2a 20 41 4e 41 4c 59 5a 45 20 2a 2f 0a 20   /* ANALYZE */. 
32be5 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32be6 3d 3d 37 35 20 29 3b 20 2f 2a 20 50 52 41 47 4d  ==75 ); /* PRAGM
32be7 41 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  A */.      testc
32be8 61 73 65 28 20 69 3d 3d 37 36 20 29 3b 20 2f 2a  ase( i==76 ); /*
32be9 20 41 42 4f 52 54 20 2a 2f 0a 20 20 20 20 20 20   ABORT */.      
32bea 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 37 20  testcase( i==77 
32beb 29 3b 20 2f 2a 20 56 41 4c 55 45 53 20 2a 2f 0a  ); /* VALUES */.
32bec 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32bed 69 3d 3d 37 38 20 29 3b 20 2f 2a 20 56 49 52 54  i==78 ); /* VIRT
32bee 55 41 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  UAL */.      tes
32bef 74 63 61 73 65 28 20 69 3d 3d 37 39 20 29 3b 20  tcase( i==79 ); 
32bf0 2f 2a 20 4c 49 4d 49 54 20 2a 2f 0a 20 20 20 20  /* LIMIT */.    
32bf1 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38    testcase( i==8
32bf2 30 20 29 3b 20 2f 2a 20 57 48 45 4e 20 2a 2f 0a  0 ); /* WHEN */.
32bf3 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32bf4 69 3d 3d 38 31 20 29 3b 20 2f 2a 20 57 48 45 52  i==81 ); /* WHER
32bf5 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
32bf6 61 73 65 28 20 69 3d 3d 38 32 20 29 3b 20 2f 2a  ase( i==82 ); /*
32bf7 20 52 45 4e 41 4d 45 20 2a 2f 0a 20 20 20 20 20   RENAME */.     
32bf8 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38 33   testcase( i==83
32bf9 20 29 3b 20 2f 2a 20 41 46 54 45 52 20 2a 2f 0a   ); /* AFTER */.
32bfa 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32bfb 69 3d 3d 38 34 20 29 3b 20 2f 2a 20 52 45 50 4c  i==84 ); /* REPL
32bfc 41 43 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ACE */.      tes
32bfd 74 63 61 73 65 28 20 69 3d 3d 38 35 20 29 3b 20  tcase( i==85 ); 
32bfe 2f 2a 20 41 4e 44 20 2a 2f 0a 20 20 20 20 20 20  /* AND */.      
32bff 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38 36 20  testcase( i==86 
32c00 29 3b 20 2f 2a 20 44 45 46 41 55 4c 54 20 2a 2f  ); /* DEFAULT */
32c01 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32c02 20 69 3d 3d 38 37 20 29 3b 20 2f 2a 20 41 55 54   i==87 ); /* AUT
32c03 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 20 20  OINCREMENT */.  
32c04 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32c05 3d 38 38 20 29 3b 20 2f 2a 20 54 4f 20 2a 2f 0a  =88 ); /* TO */.
32c06 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32c07 69 3d 3d 38 39 20 29 3b 20 2f 2a 20 49 4e 20 2a  i==89 ); /* IN *
32c08 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32c09 28 20 69 3d 3d 39 30 20 29 3b 20 2f 2a 20 43 41  ( i==90 ); /* CA
32c0a 53 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  ST */.      test
32c0b 63 61 73 65 28 20 69 3d 3d 39 31 20 29 3b 20 2f  case( i==91 ); /
32c0c 2a 20 43 4f 4c 55 4d 4e 20 2a 2f 0a 20 20 20 20  * COLUMN */.    
32c0d 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39    testcase( i==9
32c0e 32 20 29 3b 20 2f 2a 20 43 4f 4d 4d 49 54 20 2a  2 ); /* COMMIT *
32c0f 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32c10 28 20 69 3d 3d 39 33 20 29 3b 20 2f 2a 20 43 4f  ( i==93 ); /* CO
32c11 4e 46 4c 49 43 54 20 2a 2f 0a 20 20 20 20 20 20  NFLICT */.      
32c12 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39 34 20  testcase( i==94 
32c13 29 3b 20 2f 2a 20 43 52 4f 53 53 20 2a 2f 0a 20  ); /* CROSS */. 
32c14 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32c15 3d 3d 39 35 20 29 3b 20 2f 2a 20 43 55 52 52 45  ==95 ); /* CURRE
32c16 4e 54 5f 54 49 4d 45 53 54 41 4d 50 20 2a 2f 0a  NT_TIMESTAMP */.
32c17 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32c18 69 3d 3d 39 36 20 29 3b 20 2f 2a 20 43 55 52 52  i==96 ); /* CURR
32c19 45 4e 54 5f 54 49 4d 45 20 2a 2f 0a 20 20 20 20  ENT_TIME */.    
32c1a 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39    testcase( i==9
32c1b 37 20 29 3b 20 2f 2a 20 50 52 49 4d 41 52 59 20  7 ); /* PRIMARY 
32c1c 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32c1d 65 28 20 69 3d 3d 39 38 20 29 3b 20 2f 2a 20 44  e( i==98 ); /* D
32c1e 45 46 45 52 52 45 44 20 2a 2f 0a 20 20 20 20 20  EFERRED */.     
32c1f 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39 39   testcase( i==99
32c20 20 29 3b 20 2f 2a 20 44 49 53 54 49 4e 43 54 20   ); /* DISTINCT 
32c21 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32c22 65 28 20 69 3d 3d 31 30 30 20 29 3b 20 2f 2a 20  e( i==100 ); /* 
32c23 49 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  IS */.      test
32c24 63 61 73 65 28 20 69 3d 3d 31 30 31 20 29 3b 20  case( i==101 ); 
32c25 2f 2a 20 44 52 4f 50 20 2a 2f 0a 20 20 20 20 20  /* DROP */.     
32c26 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30   testcase( i==10
32c27 32 20 29 3b 20 2f 2a 20 46 41 49 4c 20 2a 2f 0a  2 ); /* FAIL */.
32c28 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32c29 69 3d 3d 31 30 33 20 29 3b 20 2f 2a 20 46 52 4f  i==103 ); /* FRO
32c2a 4d 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  M */.      testc
32c2b 61 73 65 28 20 69 3d 3d 31 30 34 20 29 3b 20 2f  ase( i==104 ); /
32c2c 2a 20 46 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20  * FULL */.      
32c2d 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 35  testcase( i==105
32c2e 20 29 3b 20 2f 2a 20 47 4c 4f 42 20 2a 2f 0a 20   ); /* GLOB */. 
32c2f 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32c30 3d 3d 31 30 36 20 29 3b 20 2f 2a 20 42 59 20 2a  ==106 ); /* BY *
32c31 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32c32 28 20 69 3d 3d 31 30 37 20 29 3b 20 2f 2a 20 49  ( i==107 ); /* I
32c33 46 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  F */.      testc
32c34 61 73 65 28 20 69 3d 3d 31 30 38 20 29 3b 20 2f  ase( i==108 ); /
32c35 2a 20 49 53 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20  * ISNULL */.    
32c36 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
32c37 30 39 20 29 3b 20 2f 2a 20 4f 52 44 45 52 20 2a  09 ); /* ORDER *
32c38 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32c39 28 20 69 3d 3d 31 31 30 20 29 3b 20 2f 2a 20 52  ( i==110 ); /* R
32c3a 45 53 54 52 49 43 54 20 2a 2f 0a 20 20 20 20 20  ESTRICT */.     
32c3b 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31   testcase( i==11
32c3c 31 20 29 3b 20 2f 2a 20 4f 55 54 45 52 20 2a 2f  1 ); /* OUTER */
32c3d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32c3e 20 69 3d 3d 31 31 32 20 29 3b 20 2f 2a 20 52 49   i==112 ); /* RI
32c3f 47 48 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  GHT */.      tes
32c40 74 63 61 73 65 28 20 69 3d 3d 31 31 33 20 29 3b  tcase( i==113 );
32c41 20 2f 2a 20 52 4f 4c 4c 42 41 43 4b 20 2a 2f 0a   /* ROLLBACK */.
32c42 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32c43 69 3d 3d 31 31 34 20 29 3b 20 2f 2a 20 52 4f 57  i==114 ); /* ROW
32c44 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32c45 73 65 28 20 69 3d 3d 31 31 35 20 29 3b 20 2f 2a  se( i==115 ); /*
32c46 20 55 4e 49 4f 4e 20 2a 2f 0a 20 20 20 20 20 20   UNION */.      
32c47 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31 36  testcase( i==116
32c48 20 29 3b 20 2f 2a 20 55 53 49 4e 47 20 2a 2f 0a   ); /* USING */.
32c49 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32c4a 69 3d 3d 31 31 37 20 29 3b 20 2f 2a 20 56 41 43  i==117 ); /* VAC
32c4b 55 55 4d 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  UUM */.      tes
32c4c 74 63 61 73 65 28 20 69 3d 3d 31 31 38 20 29 3b  tcase( i==118 );
32c4d 20 2f 2a 20 56 49 45 57 20 2a 2f 0a 20 20 20 20   /* VIEW */.    
32c4e 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
32c4f 31 39 20 29 3b 20 2f 2a 20 49 4e 49 54 49 41 4c  19 ); /* INITIAL
32c50 4c 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  LY */.      test
32c51 63 61 73 65 28 20 69 3d 3d 31 32 30 20 29 3b 20  case( i==120 ); 
32c52 2f 2a 20 41 4c 4c 20 2a 2f 0a 20 20 20 20 20 20  /* ALL */.      
32c53 72 65 74 75 72 6e 20 61 43 6f 64 65 5b 69 5d 3b  return aCode[i];
32c54 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
32c55 75 72 6e 20 54 4b 5f 49 44 3b 0a 7d 0a 53 51 4c  urn TK_ID;.}.SQL
32c56 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
32c57 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f  sqlite3KeywordCo
32c58 64 65 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  de(const unsigne
32c59 64 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e  d char *z, int n
32c5a 29 7b 0a 20 20 72 65 74 75 72 6e 20 6b 65 79 77  ){.  return keyw
32c5b 6f 72 64 43 6f 64 65 28 28 63 68 61 72 2a 29 7a  ordCode((char*)z
32c5c 2c 20 6e 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  , n);.}../******
32c5d 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
32c5e 6b 65 79 77 6f 72 64 68 61 73 68 2e 68 20 2a 2a  keywordhash.h **
32c5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32c61 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
32c62 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
32c63 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
32c64 74 20 6f 66 66 20 69 6e 20 74 6f 6b 65 6e 69 7a  t off in tokeniz
32c65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
32c66 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  *******/.../*.**
32c67 20 49 66 20 58 20 69 73 20 61 20 63 68 61 72 61   If X is a chara
32c68 63 74 65 72 20 74 68 61 74 20 63 61 6e 20 62 65  cter that can be
32c69 20 75 73 65 64 20 69 6e 20 61 6e 20 69 64 65 6e   used in an iden
32c6a 74 69 66 69 65 72 20 74 68 65 6e 0a 2a 2a 20 49  tifier then.** I
32c6b 64 43 68 61 72 28 58 29 20 77 69 6c 6c 20 62 65  dChar(X) will be
32c6c 20 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69 73   true.  Otherwis
32c6d 65 20 69 74 20 69 73 20 66 61 6c 73 65 2e 0a 2a  e it is false..*
32c6e 2a 0a 2a 2a 20 46 6f 72 20 41 53 43 49 49 2c 20  *.** For ASCII, 
32c6f 61 6e 79 20 63 68 61 72 61 63 74 65 72 20 77 69  any character wi
32c70 74 68 20 74 68 65 20 68 69 67 68 2d 6f 72 64 65  th the high-orde
32c71 72 20 62 69 74 20 73 65 74 20 69 73 0a 2a 2a 20  r bit set is.** 
32c72 61 6c 6c 6f 77 65 64 20 69 6e 20 61 6e 20 69 64  allowed in an id
32c73 65 6e 74 69 66 69 65 72 2e 20 20 46 6f 72 20 37  entifier.  For 7
32c74 2d 62 69 74 20 63 68 61 72 61 63 74 65 72 73 2c  -bit characters,
32c75 20 0a 2a 2a 20 73 71 6c 69 74 65 33 49 73 49 64   .** sqlite3IsId
32c76 43 68 61 72 5b 58 5d 20 6d 75 73 74 20 62 65 20  Char[X] must be 
32c77 31 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 45 42 43  1..**.** For EBC
32c78 44 49 43 2c 20 74 68 65 20 72 75 6c 65 73 20 61  DIC, the rules a
32c79 72 65 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 78 20  re more complex 
32c7a 62 75 74 20 68 61 76 65 20 74 68 65 20 73 61 6d  but have the sam
32c7b 65 0a 2a 2a 20 65 6e 64 20 72 65 73 75 6c 74 2e  e.** end result.
32c7c 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31  .**.** Ticket #1
32c7d 30 36 36 2e 20 20 74 68 65 20 53 51 4c 20 73 74  066.  the SQL st
32c7e 61 6e 64 61 72 64 20 64 6f 65 73 20 6e 6f 74 20  andard does not 
32c7f 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20 74 68 65  allow '$' in the
32c80 0a 2a 2a 20 6d 69 64 64 6c 65 20 6f 66 20 69 64  .** middle of id
32c81 65 6e 74 66 69 65 72 73 2e 20 20 42 75 74 20 6d  entfiers.  But m
32c82 61 6e 79 20 53 51 4c 20 69 6d 70 6c 65 6d 65 6e  any SQL implemen
32c83 74 61 74 69 6f 6e 73 20 64 6f 2e 20 0a 2a 2a 20  tations do. .** 
32c84 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 6c 6f  SQLite will allo
32c85 77 20 27 24 27 20 69 6e 20 69 64 65 6e 74 69 66  w '$' in identif
32c86 69 65 72 73 20 66 6f 72 20 63 6f 6d 70 61 74 69  iers for compati
32c87 62 69 6c 69 74 79 2e 0a 2a 2a 20 42 75 74 20 74  bility..** But t
32c88 68 65 20 66 65 61 74 75 72 65 20 69 73 20 75 6e  he feature is un
32c89 64 6f 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 23  documented..*/.#
32c8a 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43  ifdef SQLITE_ASC
32c8b 49 49 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  II.SQLITE_PRIVAT
32c8c 45 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c  E const char sql
32c8d 69 74 65 33 49 73 41 73 63 69 69 49 64 43 68 61  ite3IsAsciiIdCha
32c8e 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20 78 31  r[] = {./* x0 x1
32c8f 20 78 32 20 78 33 20 78 34 20 78 35 20 78 36 20   x2 x3 x4 x5 x6 
32c90 78 37 20 78 38 20 78 39 20 78 41 20 78 42 20 78  x7 x8 x9 xA xB x
32c91 43 20 78 44 20 78 45 20 78 46 20 2a 2f 0a 20 20  C xD xE xF */.  
32c92 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c    0, 0, 0, 0, 1,
32c93 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
32c94 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
32c95 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 20  ,  /* 2x */.    
32c96 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
32c97 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
32c98 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
32c99 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20 30 2c   /* 3x */.    0,
32c9a 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32c9b 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
32c9c 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f  , 1, 1, 1, 1,  /
32c9d 2a 20 34 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31  * 4x */.    1, 1
32c9e 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
32c9f 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
32ca0 30 2c 20 30 2c 20 30 2c 20 31 2c 20 20 2f 2a 20  0, 0, 0, 1,  /* 
32ca1 35 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20  5x */.    0, 1, 
32ca2 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
32ca3 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
32ca4 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 36 78   1, 1, 1,  /* 6x
32ca5 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c   */.    1, 1, 1,
32ca6 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32ca7 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30  1, 1, 1, 0, 0, 0
32ca8 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 37 78 20 2a  , 0, 0,  /* 7x *
32ca9 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 49 64 43  /.};.#define IdC
32caa 68 61 72 28 43 29 20 20 28 28 28 63 3d 43 29 26  har(C)  (((c=C)&
32cab 30 78 38 30 29 21 3d 30 20 7c 7c 20 28 63 3e 30  0x80)!=0 || (c>0
32cac 78 31 66 20 26 26 20 73 71 6c 69 74 65 33 49 73  x1f && sqlite3Is
32cad 41 73 63 69 69 49 64 43 68 61 72 5b 63 2d 30 78  AsciiIdChar[c-0x
32cae 32 30 5d 29 29 0a 23 65 6e 64 69 66 0a 23 69 66  20])).#endif.#if
32caf 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49  def SQLITE_EBCDI
32cb0 43 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  C.SQLITE_PRIVATE
32cb1 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69   const char sqli
32cb2 74 65 33 49 73 45 62 63 64 69 63 49 64 43 68 61  te3IsEbcdicIdCha
32cb3 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20 78 31  r[] = {./* x0 x1
32cb4 20 78 32 20 78 33 20 78 34 20 78 35 20 78 36 20   x2 x3 x4 x5 x6 
32cb5 78 37 20 78 38 20 78 39 20 78 41 20 78 42 20 78  x7 x8 x9 xA xB x
32cb6 43 20 78 44 20 78 45 20 78 46 20 2a 2f 0a 20 20  C xD xE xF */.  
32cb7 20 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c    0, 0, 1, 1, 1,
32cb8 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32cb9 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
32cba 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20 20  ,  /* 4x */.    
32cbb 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
32cbc 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
32cbd 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 0, 0, 0, 0, 
32cbe 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 30 2c   /* 5x */.    0,
32cbf 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
32cc0 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
32cc1 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 1, 0, 0,  /
32cc2 2a 20 36 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * 6x */.    0, 1
32cc3 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
32cc4 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 1, 0, 0, 0, 
32cc5 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
32cc6 37 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20  7x */.    0, 1, 
32cc7 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
32cc8 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 31 2c  , 1, 1, 0, 0, 1,
32cc9 20 31 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 38 78   1, 1, 0,  /* 8x
32cca 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
32ccb 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32ccc 31 2c 20 31 2c 20 30 2c 20 30 2c 20 31 2c 20 30  1, 1, 0, 0, 1, 0
32ccd 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 39 78 20 2a  , 1, 0,  /* 9x *
32cce 2f 0a 20 20 20 20 31 2c 20 30 2c 20 31 2c 20 31  /.    1, 0, 1, 1
32ccf 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
32cd0 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20   1, 1, 0, 1, 1, 
32cd1 31 2c 20 30 2c 20 20 2f 2a 20 41 78 20 2a 2f 0a  1, 0,  /* Ax */.
32cd2 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20      0, 0, 0, 0, 
32cd3 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
32cd4 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
32cd5 20 30 2c 20 20 2f 2a 20 42 78 20 2a 2f 0a 20 20   0,  /* Bx */.  
32cd6 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
32cd7 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32cd8 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
32cd9 2c 20 20 2f 2a 20 43 78 20 2a 2f 0a 20 20 20 20  ,  /* Cx */.    
32cda 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
32cdb 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
32cdc 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32cdd 20 2f 2a 20 44 78 20 2a 2f 0a 20 20 20 20 30 2c   /* Dx */.    0,
32cde 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   0, 1, 1, 1, 1, 
32cdf 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31  1, 1, 1, 1, 0, 1
32ce0 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f  , 1, 1, 1, 1,  /
32ce1 2a 20 45 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31  * Ex */.    1, 1
32ce2 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
32ce3 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20   1, 1, 1, 0, 1, 
32ce4 31 2c 20 31 2c 20 31 2c 20 30 2c 20 20 2f 2a 20  1, 1, 1, 0,  /* 
32ce5 46 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65  Fx */.};.#define
32ce6 20 49 64 43 68 61 72 28 43 29 20 20 28 28 28 63   IdChar(C)  (((c
32ce7 3d 43 29 3e 3d 30 78 34 32 20 26 26 20 73 71 6c  =C)>=0x42 && sql
32ce8 69 74 65 33 49 73 45 62 63 64 69 63 49 64 43 68  ite3IsEbcdicIdCh
32ce9 61 72 5b 63 2d 30 78 34 30 5d 29 29 0a 23 65 6e  ar[c-0x40])).#en
32cea 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  dif.../*.** Retu
32ceb 72 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66  rn the length of
32cec 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20   the token that 
32ced 62 65 67 69 6e 73 20 61 74 20 7a 5b 30 5d 2e 20  begins at z[0]. 
32cee 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f  .** Store the to
32cef 6b 65 6e 20 74 79 70 65 20 69 6e 20 2a 74 6f 6b  ken type in *tok
32cf0 65 6e 54 79 70 65 20 62 65 66 6f 72 65 20 72 65  enType before re
32cf1 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49  turning..*/.SQLI
32cf2 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
32cf3 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 63  qlite3GetToken(c
32cf4 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
32cf5 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 74 6f 6b 65  ar *z, int *toke
32cf6 6e 54 79 70 65 29 7b 0a 20 20 69 6e 74 20 69 2c  nType){.  int i,
32cf7 20 63 3b 0a 20 20 73 77 69 74 63 68 28 20 2a 7a   c;.  switch( *z
32cf8 20 29 7b 0a 20 20 20 20 63 61 73 65 20 27 20 27   ){.    case ' '
32cf9 3a 20 63 61 73 65 20 27 5c 74 27 3a 20 63 61 73  : case '\t': cas
32cfa 65 20 27 5c 6e 27 3a 20 63 61 73 65 20 27 5c 66  e '\n': case '\f
32cfb 27 3a 20 63 61 73 65 20 27 5c 72 27 3a 20 7b 0a  ': case '\r': {.
32cfc 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32cfd 7a 5b 30 5d 3d 3d 27 20 27 20 29 3b 0a 20 20 20  z[0]==' ' );.   
32cfe 20 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30     testcase( z[0
32cff 5d 3d 3d 27 5c 74 27 20 29 3b 0a 20 20 20 20 20  ]=='\t' );.     
32d00 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d   testcase( z[0]=
32d01 3d 27 5c 6e 27 20 29 3b 0a 20 20 20 20 20 20 74  ='\n' );.      t
32d02 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27  estcase( z[0]=='
32d03 5c 66 27 20 29 3b 0a 20 20 20 20 20 20 74 65 73  \f' );.      tes
32d04 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 5c 72  tcase( z[0]=='\r
32d05 27 20 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  ' );.      for(i
32d06 3d 31 3b 20 73 71 6c 69 74 65 33 49 73 73 70 61  =1; sqlite3Isspa
32d07 63 65 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d  ce(z[i]); i++){}
32d08 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
32d09 65 20 3d 20 54 4b 5f 53 50 41 43 45 3b 0a 20 20  e = TK_SPACE;.  
32d0a 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20      return i;.  
32d0b 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2d 27    }.    case '-'
32d0c 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b  : {.      if( z[
32d0d 31 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20  1]=='-' ){.     
32d0e 20 20 20 66 6f 72 28 69 3d 32 3b 20 28 63 3d 7a     for(i=2; (c=z
32d0f 5b 69 5d 29 21 3d 30 20 26 26 20 63 21 3d 27 5c  [i])!=0 && c!='\
32d10 6e 27 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20  n'; i++){}.     
32d11 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
32d12 54 4b 5f 53 50 41 43 45 3b 0a 20 20 20 20 20 20  TK_SPACE;.      
32d13 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20    return i;.    
32d14 20 20 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e    }.      *token
32d15 54 79 70 65 20 3d 20 54 4b 5f 4d 49 4e 55 53 3b  Type = TK_MINUS;
32d16 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
32d17 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
32d18 27 28 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f  '(': {.      *to
32d19 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 50 3b  kenType = TK_LP;
32d1a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
32d1b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
32d1c 27 29 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f  ')': {.      *to
32d1d 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 52 50 3b  kenType = TK_RP;
32d1e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
32d1f 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
32d20 27 3b 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f  ';': {.      *to
32d21 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 45 4d  kenType = TK_SEM
32d22 49 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  I;.      return 
32d23 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  1;.    }.    cas
32d24 65 20 27 2b 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e '+': {.      *
32d25 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 50  tokenType = TK_P
32d26 4c 55 53 3b 0a 20 20 20 20 20 20 72 65 74 75 72  LUS;.      retur
32d27 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 1;.    }.    c
32d28 61 73 65 20 27 2a 27 3a 20 7b 0a 20 20 20 20 20  ase '*': {.     
32d29 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
32d2a 5f 53 54 41 52 3b 0a 20 20 20 20 20 20 72 65 74  _STAR;.      ret
32d2b 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
32d2c 20 63 61 73 65 20 27 2f 27 3a 20 7b 0a 20 20 20   case '/': {.   
32d2d 20 20 20 69 66 28 20 7a 5b 31 5d 21 3d 27 2a 27     if( z[1]!='*'
32d2e 20 7c 7c 20 7a 5b 32 5d 3d 3d 30 20 29 7b 0a 20   || z[2]==0 ){. 
32d2f 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
32d30 65 20 3d 20 54 4b 5f 53 4c 41 53 48 3b 0a 20 20  e = TK_SLASH;.  
32d31 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
32d32 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f        }.      fo
32d33 72 28 69 3d 33 2c 20 63 3d 7a 5b 32 5d 3b 20 28  r(i=3, c=z[2]; (
32d34 63 21 3d 27 2a 27 20 7c 7c 20 7a 5b 69 5d 21 3d  c!='*' || z[i]!=
32d35 27 2f 27 29 20 26 26 20 28 63 3d 7a 5b 69 5d 29  '/') && (c=z[i])
32d36 21 3d 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20  !=0; i++){}.    
32d37 20 20 69 66 28 20 63 20 29 20 69 2b 2b 3b 0a 20    if( c ) i++;. 
32d38 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
32d39 3d 20 54 4b 5f 53 50 41 43 45 3b 0a 20 20 20 20  = TK_SPACE;.    
32d3a 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20    return i;.    
32d3b 7d 0a 20 20 20 20 63 61 73 65 20 27 25 27 3a 20  }.    case '%': 
32d3c 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  {.      *tokenTy
32d3d 70 65 20 3d 20 54 4b 5f 52 45 4d 3b 0a 20 20 20  pe = TK_REM;.   
32d3e 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
32d3f 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3d 27 3a   }.    case '=':
32d40 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54   {.      *tokenT
32d41 79 70 65 20 3d 20 54 4b 5f 45 51 3b 0a 20 20 20  ype = TK_EQ;.   
32d42 20 20 20 72 65 74 75 72 6e 20 31 20 2b 20 28 7a     return 1 + (z
32d43 5b 31 5d 3d 3d 27 3d 27 29 3b 0a 20 20 20 20 7d  [1]=='=');.    }
32d44 0a 20 20 20 20 63 61 73 65 20 27 3c 27 3a 20 7b  .    case '<': {
32d45 0a 20 20 20 20 20 20 69 66 28 20 28 63 3d 7a 5b  .      if( (c=z[
32d46 31 5d 29 3d 3d 27 3d 27 20 29 7b 0a 20 20 20 20  1])=='=' ){.    
32d47 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
32d48 20 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 20 20   TK_LE;.        
32d49 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20  return 2;.      
32d4a 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 3e 27  }else if( c=='>'
32d4b 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b   ){.        *tok
32d4c 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4e 45 3b 0a  enType = TK_NE;.
32d4d 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32          return 2
32d4e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
32d4f 28 20 63 3d 3d 27 3c 27 20 29 7b 0a 20 20 20 20  ( c=='<' ){.    
32d50 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
32d51 20 54 4b 5f 4c 53 48 49 46 54 3b 0a 20 20 20 20   TK_LSHIFT;.    
32d52 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
32d53 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
32d54 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
32d55 54 4b 5f 4c 54 3b 0a 20 20 20 20 20 20 20 20 72  TK_LT;.        r
32d56 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
32d57 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
32d58 27 3e 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  '>': {.      if(
32d59 20 28 63 3d 7a 5b 31 5d 29 3d 3d 27 3d 27 20 29   (c=z[1])=='=' )
32d5a 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
32d5b 54 79 70 65 20 3d 20 54 4b 5f 47 45 3b 0a 20 20  Type = TK_GE;.  
32d5c 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a        return 2;.
32d5d 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
32d5e 63 3d 3d 27 3e 27 20 29 7b 0a 20 20 20 20 20 20  c=='>' ){.      
32d5f 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
32d60 4b 5f 52 53 48 49 46 54 3b 0a 20 20 20 20 20 20  K_RSHIFT;.      
32d61 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20    return 2;.    
32d62 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
32d63 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
32d64 5f 47 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74  _GT;.        ret
32d65 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 1;.      }. 
32d66 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 21     }.    case '!
32d67 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 7a  ': {.      if( z
32d68 5b 31 5d 21 3d 27 3d 27 20 29 7b 0a 20 20 20 20  [1]!='=' ){.    
32d69 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
32d6a 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20   TK_ILLEGAL;.   
32d6b 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20       return 2;. 
32d6c 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
32d6d 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
32d6e 20 54 4b 5f 4e 45 3b 0a 20 20 20 20 20 20 20 20   TK_NE;.        
32d6f 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20  return 2;.      
32d70 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  }.    }.    case
32d71 20 27 7c 27 3a 20 7b 0a 20 20 20 20 20 20 69 66   '|': {.      if
32d72 28 20 7a 5b 31 5d 21 3d 27 7c 27 20 29 7b 0a 20  ( z[1]!='|' ){. 
32d73 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
32d74 65 20 3d 20 54 4b 5f 42 49 54 4f 52 3b 0a 20 20  e = TK_BITOR;.  
32d75 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
32d76 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
32d77 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
32d78 3d 20 54 4b 5f 43 4f 4e 43 41 54 3b 0a 20 20 20  = TK_CONCAT;.   
32d79 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20       return 2;. 
32d7a 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
32d7b 20 63 61 73 65 20 27 2c 27 3a 20 7b 0a 20 20 20   case ',': {.   
32d7c 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
32d7d 54 4b 5f 43 4f 4d 4d 41 3b 0a 20 20 20 20 20 20  TK_COMMA;.      
32d7e 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
32d7f 20 20 20 20 63 61 73 65 20 27 26 27 3a 20 7b 0a      case '&': {.
32d80 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
32d81 20 3d 20 54 4b 5f 42 49 54 41 4e 44 3b 0a 20 20   = TK_BITAND;.  
32d82 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
32d83 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 7e 27    }.    case '~'
32d84 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e  : {.      *token
32d85 54 79 70 65 20 3d 20 54 4b 5f 42 49 54 4e 4f 54  Type = TK_BITNOT
32d86 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ;.      return 1
32d87 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
32d88 20 27 60 27 3a 0a 20 20 20 20 63 61 73 65 20 27   '`':.    case '
32d89 5c 27 27 3a 0a 20 20 20 20 63 61 73 65 20 27 22  \'':.    case '"
32d8a 27 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64  ': {.      int d
32d8b 65 6c 69 6d 20 3d 20 7a 5b 30 5d 3b 0a 20 20 20  elim = z[0];.   
32d8c 20 20 20 74 65 73 74 63 61 73 65 28 20 64 65 6c     testcase( del
32d8d 69 6d 3d 3d 27 60 27 20 29 3b 0a 20 20 20 20 20  im=='`' );.     
32d8e 20 74 65 73 74 63 61 73 65 28 20 64 65 6c 69 6d   testcase( delim
32d8f 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 20 20  =='\'' );.      
32d90 74 65 73 74 63 61 73 65 28 20 64 65 6c 69 6d 3d  testcase( delim=
32d91 3d 27 22 27 20 29 3b 0a 20 20 20 20 20 20 66 6f  ='"' );.      fo
32d92 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d 29 21  r(i=1; (c=z[i])!
32d93 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  =0; i++){.      
32d94 20 20 69 66 28 20 63 3d 3d 64 65 6c 69 6d 20 29    if( c==delim )
32d95 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
32d96 7a 5b 69 2b 31 5d 3d 3d 64 65 6c 69 6d 20 29 7b  z[i+1]==delim ){
32d97 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b  .            i++
32d98 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
32d99 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  e{.            b
32d9a 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
32d9b 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
32d9c 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 3d    }.      if( c=
32d9d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20 20 20 20  ='\'' ){.       
32d9e 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
32d9f 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 20 20 20  _STRING;.       
32da0 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20   return i+1;.   
32da1 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 21 3d     }else if( c!=
32da2 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f  0 ){.        *to
32da3 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 44 3b  kenType = TK_ID;
32da4 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
32da5 69 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  i+1;.      }else
32da6 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
32da7 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41  Type = TK_ILLEGA
32da8 4c 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  L;.        retur
32da9 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n i;.      }.   
32daa 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2e 27 3a   }.    case '.':
32dab 20 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   {.#ifndef SQLIT
32dac 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
32dad 50 4f 49 4e 54 0a 20 20 20 20 20 20 69 66 28 20  POINT.      if( 
32dae 21 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28  !sqlite3Isdigit(
32daf 7a 5b 31 5d 29 20 29 0a 23 65 6e 64 69 66 0a 20  z[1]) ).#endif. 
32db0 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 2a       {.        *
32db1 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 44  tokenType = TK_D
32db2 4f 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  OT;.        retu
32db3 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 1;.      }.  
32db4 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6e 65      /* If the ne
32db5 78 74 20 63 68 61 72 61 63 74 65 72 20 69 73 20  xt character is 
32db6 61 20 64 69 67 69 74 2c 20 74 68 69 73 20 69 73  a digit, this is
32db7 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e   a floating poin
32db8 74 0a 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65  t.      ** numbe
32db9 72 20 74 68 61 74 20 62 65 67 69 6e 73 20 77 69  r that begins wi
32dba 74 68 20 22 2e 22 2e 20 20 46 61 6c 6c 20 74 68  th ".".  Fall th
32dbb 72 75 20 69 6e 74 6f 20 74 68 65 20 6e 65 78 74  ru into the next
32dbc 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d 0a 20   case */.    }. 
32dbd 20 20 20 63 61 73 65 20 27 30 27 3a 20 63 61 73     case '0': cas
32dbe 65 20 27 31 27 3a 20 63 61 73 65 20 27 32 27 3a  e '1': case '2':
32dbf 20 63 61 73 65 20 27 33 27 3a 20 63 61 73 65 20   case '3': case 
32dc0 27 34 27 3a 0a 20 20 20 20 63 61 73 65 20 27 35  '4':.    case '5
32dc1 27 3a 20 63 61 73 65 20 27 36 27 3a 20 63 61 73  ': case '6': cas
32dc2 65 20 27 37 27 3a 20 63 61 73 65 20 27 38 27 3a  e '7': case '8':
32dc3 20 63 61 73 65 20 27 39 27 3a 20 7b 0a 20 20 20   case '9': {.   
32dc4 20 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30     testcase( z[0
32dc5 5d 3d 3d 27 30 27 20 29 3b 20 20 74 65 73 74 63  ]=='0' );  testc
32dc6 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 31 27 20 29  ase( z[0]=='1' )
32dc7 3b 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30  ;  testcase( z[0
32dc8 5d 3d 3d 27 32 27 20 29 3b 0a 20 20 20 20 20 20  ]=='2' );.      
32dc9 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d  testcase( z[0]==
32dca 27 33 27 20 29 3b 20 20 74 65 73 74 63 61 73 65  '3' );  testcase
32dcb 28 20 7a 5b 30 5d 3d 3d 27 34 27 20 29 3b 20 20  ( z[0]=='4' );  
32dcc 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d  testcase( z[0]==
32dcd 27 35 27 20 29 3b 0a 20 20 20 20 20 20 74 65 73  '5' );.      tes
32dce 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 36 27  tcase( z[0]=='6'
32dcf 20 29 3b 20 20 74 65 73 74 63 61 73 65 28 20 7a   );  testcase( z
32dd0 5b 30 5d 3d 3d 27 37 27 20 29 3b 20 20 74 65 73  [0]=='7' );  tes
32dd1 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 38 27  tcase( z[0]=='8'
32dd2 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
32dd3 73 65 28 20 7a 5b 30 5d 3d 3d 27 39 27 20 29 3b  se( z[0]=='9' );
32dd4 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
32dd5 65 20 3d 20 54 4b 5f 49 4e 54 45 47 45 52 3b 0a  e = TK_INTEGER;.
32dd6 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 73        for(i=0; s
32dd7 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b  qlite3Isdigit(z[
32dd8 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 23 69 66 6e  i]); i++){}.#ifn
32dd9 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
32dda 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20  FLOATING_POINT. 
32ddb 20 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 27       if( z[i]=='
32ddc 2e 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69 2b  .' ){.        i+
32ddd 2b 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  +;.        while
32dde 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  ( sqlite3Isdigit
32ddf 28 7a 5b 69 5d 29 20 29 7b 20 69 2b 2b 3b 20 7d  (z[i]) ){ i++; }
32de0 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
32de1 79 70 65 20 3d 20 54 4b 5f 46 4c 4f 41 54 3b 0a  ype = TK_FLOAT;.
32de2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
32de3 28 20 28 7a 5b 69 5d 3d 3d 27 65 27 20 7c 7c 20  ( (z[i]=='e' || 
32de4 7a 5b 69 5d 3d 3d 27 45 27 29 20 26 26 0a 20 20  z[i]=='E') &&.  
32de5 20 20 20 20 20 20 20 20 20 28 20 73 71 6c 69 74           ( sqlit
32de6 65 33 49 73 64 69 67 69 74 28 7a 5b 69 2b 31 5d  e3Isdigit(z[i+1]
32de7 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  ) .            |
32de8 7c 20 28 28 7a 5b 69 2b 31 5d 3d 3d 27 2b 27 20  | ((z[i+1]=='+' 
32de9 7c 7c 20 7a 5b 69 2b 31 5d 3d 3d 27 2d 27 29 20  || z[i+1]=='-') 
32dea 26 26 20 73 71 6c 69 74 65 33 49 73 64 69 67 69  && sqlite3Isdigi
32deb 74 28 7a 5b 69 2b 32 5d 29 29 0a 20 20 20 20 20  t(z[i+2])).     
32dec 20 20 20 20 20 20 29 0a 20 20 20 20 20 20 29 7b        ).      ){
32ded 0a 20 20 20 20 20 20 20 20 69 20 2b 3d 20 32 3b  .        i += 2;
32dee 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
32def 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a  sqlite3Isdigit(z
32df0 5b 69 5d 29 20 29 7b 20 69 2b 2b 3b 20 7d 0a 20  [i]) ){ i++; }. 
32df1 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
32df2 65 20 3d 20 54 4b 5f 46 4c 4f 41 54 3b 0a 20 20  e = TK_FLOAT;.  
32df3 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
32df4 20 20 20 77 68 69 6c 65 28 20 49 64 43 68 61 72     while( IdChar
32df5 28 7a 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20  (z[i]) ){.      
32df6 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
32df7 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20  K_ILLEGAL;.     
32df8 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a     i++;.      }.
32df9 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a        return i;.
32dfa 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
32dfb 5b 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f 72 28  [': {.      for(
32dfc 69 3d 31 2c 20 63 3d 7a 5b 30 5d 3b 20 63 21 3d  i=1, c=z[0]; c!=
32dfd 27 5d 27 20 26 26 20 28 63 3d 7a 5b 69 5d 29 21  ']' && (c=z[i])!
32dfe 3d 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20  =0; i++){}.     
32dff 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 63 3d   *tokenType = c=
32e00 3d 27 5d 27 20 3f 20 54 4b 5f 49 44 20 3a 20 54  =']' ? TK_ID : T
32e01 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20  K_ILLEGAL;.     
32e02 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d   return i;.    }
32e03 0a 20 20 20 20 63 61 73 65 20 27 3f 27 3a 20 7b  .    case '?': {
32e04 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
32e05 65 20 3d 20 54 4b 5f 56 41 52 49 41 42 4c 45 3b  e = TK_VARIABLE;
32e06 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20  .      for(i=1; 
32e07 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a  sqlite3Isdigit(z
32e08 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  [i]); i++){}.   
32e09 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20     return i;.   
32e0a 20 7d 0a 20 20 20 20 63 61 73 65 20 27 23 27 3a   }.    case '#':
32e0b 20 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31   {.      for(i=1
32e0c 3b 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  ; sqlite3Isdigit
32e0d 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20  (z[i]); i++){}. 
32e0e 20 20 20 20 20 69 66 28 20 69 3e 31 20 29 7b 0a       if( i>1 ){.
32e0f 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 61 6d          /* Param
32e10 65 74 65 72 73 20 6f 66 20 74 68 65 20 66 6f 72  eters of the for
32e11 6d 20 23 4e 4e 4e 20 28 77 68 65 72 65 20 4e 4e  m #NNN (where NN
32e12 4e 20 69 73 20 61 20 6e 75 6d 62 65 72 29 20 61  N is a number) a
32e13 72 65 20 75 73 65 64 0a 20 20 20 20 20 20 20 20  re used.        
32e14 2a 2a 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62 79  ** internally by
32e15 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
32e16 72 73 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20  rse.  */.       
32e17 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
32e18 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20 20 20  _REGISTER;.     
32e19 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20     return i;.   
32e1a 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 46 61     }.      /* Fa
32e1b 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20  ll through into 
32e1c 74 68 65 20 6e 65 78 74 20 63 61 73 65 20 69 66  the next case if
32e1d 20 74 68 65 20 27 23 27 20 69 73 20 6e 6f 74 20   the '#' is not 
32e1e 66 6f 6c 6c 6f 77 65 64 20 62 79 0a 20 20 20 20  followed by.    
32e1f 20 20 2a 2a 20 61 20 64 69 67 69 74 2e 20 54 72    ** a digit. Tr
32e20 79 20 74 6f 20 6d 61 74 63 68 20 23 41 41 41 41  y to match #AAAA
32e21 20 77 68 65 72 65 20 41 41 41 41 20 69 73 20 61   where AAAA is a
32e22 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d 65 2e   parameter name.
32e23 20 2a 2f 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65   */.    }.#ifnde
32e24 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 43  f SQLITE_OMIT_TC
32e25 4c 5f 56 41 52 49 41 42 4c 45 0a 20 20 20 20 63  L_VARIABLE.    c
32e26 61 73 65 20 27 24 27 3a 0a 23 65 6e 64 69 66 0a  ase '$':.#endif.
32e27 20 20 20 20 63 61 73 65 20 27 40 27 3a 20 20 2f      case '@':  /
32e28 2a 20 46 6f 72 20 63 6f 6d 70 61 74 69 62 69 6c  * For compatibil
32e29 69 74 79 20 77 69 74 68 20 4d 53 20 53 51 4c 20  ity with MS SQL 
32e2a 53 65 72 76 65 72 20 2a 2f 0a 20 20 20 20 63 61  Server */.    ca
32e2b 73 65 20 27 3a 27 3a 20 7b 0a 20 20 20 20 20 20  se ':': {.      
32e2c 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 20 20 20  int n = 0;.     
32e2d 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d   testcase( z[0]=
32e2e 3d 27 24 27 20 29 3b 20 20 74 65 73 74 63 61 73  ='$' );  testcas
32e2f 65 28 20 7a 5b 30 5d 3d 3d 27 40 27 20 29 3b 20  e( z[0]=='@' ); 
32e30 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d   testcase( z[0]=
32e31 3d 27 3a 27 20 29 3b 0a 20 20 20 20 20 20 2a 74  =':' );.      *t
32e32 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 56 41  okenType = TK_VA
32e33 52 49 41 42 4c 45 3b 0a 20 20 20 20 20 20 66 6f  RIABLE;.      fo
32e34 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d 29 21  r(i=1; (c=z[i])!
32e35 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  =0; i++){.      
32e36 20 20 69 66 28 20 49 64 43 68 61 72 28 63 29 20    if( IdChar(c) 
32e37 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 2b 2b  ){.          n++
32e38 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
32e39 5f 4f 4d 49 54 5f 54 43 4c 5f 56 41 52 49 41 42  _OMIT_TCL_VARIAB
32e3a 4c 45 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  LE.        }else
32e3b 20 69 66 28 20 63 3d 3d 27 28 27 20 26 26 20 6e   if( c=='(' && n
32e3c 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  >0 ){.          
32e3d 64 6f 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  do{.            
32e3e 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  i++;.          }
32e3f 77 68 69 6c 65 28 20 28 63 3d 7a 5b 69 5d 29 21  while( (c=z[i])!
32e40 3d 30 20 26 26 20 21 73 71 6c 69 74 65 33 49 73  =0 && !sqlite3Is
32e41 73 70 61 63 65 28 63 29 20 26 26 20 63 21 3d 27  space(c) && c!='
32e42 29 27 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  )' );.          
32e43 69 66 28 20 63 3d 3d 27 29 27 20 29 7b 0a 20 20  if( c==')' ){.  
32e44 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20            i++;. 
32e45 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
32e46 20 20 20 20 20 20 20 20 20 20 20 20 2a 74 6f 6b              *tok
32e47 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45  enType = TK_ILLE
32e48 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  GAL;.          }
32e49 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
32e4a 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ;.        }else 
32e4b 69 66 28 20 63 3d 3d 27 3a 27 20 26 26 20 7a 5b  if( c==':' && z[
32e4c 69 2b 31 5d 3d 3d 27 3a 27 20 29 7b 0a 20 20 20  i+1]==':' ){.   
32e4d 20 20 20 20 20 20 20 69 2b 2b 3b 0a 23 65 6e 64         i++;.#end
32e4e 69 66 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  if.        }else
32e4f 7b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  {.          brea
32e50 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
32e51 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6e     }.      if( n
32e52 3d 3d 30 20 29 20 2a 74 6f 6b 65 6e 54 79 70 65  ==0 ) *tokenType
32e53 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20   = TK_ILLEGAL;. 
32e54 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20       return i;. 
32e55 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
32e56 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49  ITE_OMIT_BLOB_LI
32e57 54 45 52 41 4c 0a 20 20 20 20 63 61 73 65 20 27  TERAL.    case '
32e58 78 27 3a 20 63 61 73 65 20 27 58 27 3a 20 7b 0a  x': case 'X': {.
32e59 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32e5a 7a 5b 30 5d 3d 3d 27 78 27 20 29 3b 20 74 65 73  z[0]=='x' ); tes
32e5b 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 58 27  tcase( z[0]=='X'
32e5c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b   );.      if( z[
32e5d 31 5d 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20  1]=='\'' ){.    
32e5e 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
32e5f 20 54 4b 5f 42 4c 4f 42 3b 0a 20 20 20 20 20 20   TK_BLOB;.      
32e60 20 20 66 6f 72 28 69 3d 32 3b 20 28 63 3d 7a 5b    for(i=2; (c=z[
32e61 69 5d 29 21 3d 30 20 26 26 20 63 21 3d 27 5c 27  i])!=0 && c!='\'
32e62 27 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  '; i++){.       
32e63 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49     if( !sqlite3I
32e64 73 78 64 69 67 69 74 28 63 29 20 29 7b 0a 20 20  sxdigit(c) ){.  
32e65 20 20 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e            *token
32e66 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41  Type = TK_ILLEGA
32e67 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  L;.          }. 
32e68 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
32e69 20 69 66 28 20 69 25 32 20 7c 7c 20 21 63 20 29   if( i%2 || !c )
32e6a 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
32e6b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20  _ILLEGAL;.      
32e6c 20 20 69 66 28 20 63 20 29 20 69 2b 2b 3b 0a 20    if( c ) i++;. 
32e6d 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b         return i;
32e6e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f  .      }.      /
32e6f 2a 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c  * Otherwise fall
32e70 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20   through to the 
32e71 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20 20 20  next case */.   
32e72 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 64 65   }.#endif.    de
32e73 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 69  fault: {.      i
32e74 66 28 20 21 49 64 43 68 61 72 28 2a 7a 29 20 29  f( !IdChar(*z) )
32e75 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  {.        break;
32e76 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66  .      }.      f
32e77 6f 72 28 69 3d 31 3b 20 49 64 43 68 61 72 28 7a  or(i=1; IdChar(z
32e78 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  [i]); i++){}.   
32e79 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
32e7a 6b 65 79 77 6f 72 64 43 6f 64 65 28 28 63 68 61  keywordCode((cha
32e7b 72 2a 29 7a 2c 20 69 29 3b 0a 20 20 20 20 20 20  r*)z, i);.      
32e7c 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a  return i;.    }.
32e7d 20 20 7d 0a 20 20 2a 74 6f 6b 65 6e 54 79 70 65    }.  *tokenType
32e7e 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20   = TK_ILLEGAL;. 
32e7f 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
32e80 0a 2a 2a 20 52 75 6e 20 74 68 65 20 70 61 72 73  .** Run the pars
32e81 65 72 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20  er on the given 
32e82 53 51 4c 20 73 74 72 69 6e 67 2e 20 20 54 68 65  SQL string.  The
32e83 20 70 61 72 73 65 72 20 73 74 72 75 63 74 75 72   parser structur
32e84 65 20 69 73 0a 2a 2a 20 70 61 73 73 65 64 20 69  e is.** passed i
32e85 6e 2e 20 20 41 6e 20 53 51 4c 49 54 45 5f 20 73  n.  An SQLITE_ s
32e86 74 61 74 75 73 20 63 6f 64 65 20 69 73 20 72 65  tatus code is re
32e87 74 75 72 6e 65 64 2e 20 20 49 66 20 61 6e 20 65  turned.  If an e
32e88 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a 2a 20 74  rror occurs.** t
32e89 68 65 6e 20 61 6e 20 61 6e 64 20 61 74 74 65 6d  hen an and attem
32e8a 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 77 72  pt is made to wr
32e8b 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ite an error mes
32e8c 73 61 67 65 20 69 6e 74 6f 20 0a 2a 2a 20 6d 65  sage into .** me
32e8d 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
32e8e 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  om sqlite3_mallo
32e8f 63 28 29 20 61 6e 64 20 74 6f 20 6d 61 6b 65 20  c() and to make 
32e90 2a 70 7a 45 72 72 4d 73 67 20 70 6f 69 6e 74 20  *pzErrMsg point 
32e91 74 6f 20 74 68 61 74 0a 2a 2a 20 65 72 72 6f 72  to that.** error
32e92 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a 53 51 4c   message..*/.SQL
32e93 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
32e94 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72  sqlite3RunParser
32e95 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
32e96 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
32e97 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73  , char **pzErrMs
32e98 67 29 7b 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d  g){.  int nErr =
32e99 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
32e9a 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
32e9b 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f 75 6e  of errors encoun
32e9c 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  tered */.  int i
32e9d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
32e9e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
32e9f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
32ea0 76 6f 69 64 20 2a 70 45 6e 67 69 6e 65 3b 20 20  void *pEngine;  
32ea1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32ea2 2f 2a 20 54 68 65 20 4c 45 4d 4f 4e 2d 67 65 6e  /* The LEMON-gen
32ea3 65 72 61 74 65 64 20 4c 41 4c 52 28 31 29 20 70  erated LALR(1) p
32ea4 61 72 73 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74  arser */.  int t
32ea5 6f 6b 65 6e 54 79 70 65 3b 20 20 20 20 20 20 20  okenType;       
32ea6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 79             /* ty
32ea7 70 65 20 6f 66 20 74 68 65 20 6e 65 78 74 20 74  pe of the next t
32ea8 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6c 61  oken */.  int la
32ea9 73 74 54 6f 6b 65 6e 50 61 72 73 65 64 20 3d 20  stTokenParsed = 
32eaa 2d 31 3b 20 20 20 20 20 20 20 2f 2a 20 74 79 70  -1;       /* typ
32eab 65 20 6f 66 20 74 68 65 20 70 72 65 76 69 6f 75  e of the previou
32eac 73 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 75 38 20  s token */.  u8 
32ead 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b  enableLookaside;
32eae 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
32eaf 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20 64  Saved value of d
32eb0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e  b->lookaside.bEn
32eb1 61 62 6c 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74  abled */.  sqlit
32eb2 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
32eb3 3e 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  >db;       /* Th
32eb4 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
32eb5 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6d  ction */.  int m
32eb6 78 53 71 6c 4c 65 6e 3b 20 20 20 20 20 20 20 20  xSqlLen;        
32eb7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
32eb8 78 20 6c 65 6e 67 74 68 20 6f 66 20 61 6e 20 53  x length of an S
32eb9 51 4c 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 0a 20  QL string */... 
32eba 20 6d 78 53 71 6c 4c 65 6e 20 3d 20 64 62 2d 3e   mxSqlLen = db->
32ebb 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
32ebc 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 48 5d 3b  MIT_SQL_LENGTH];
32ebd 0a 20 20 69 66 28 20 64 62 2d 3e 61 63 74 69 76  .  if( db->activ
32ebe 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b 0a 20  eVdbeCnt==0 ){. 
32ebf 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65     db->u1.isInte
32ec0 72 72 75 70 74 65 64 20 3d 20 30 3b 0a 20 20 7d  rrupted = 0;.  }
32ec1 0a 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20  .  pParse->rc = 
32ec2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 50 61  SQLITE_OK;.  pPa
32ec3 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 7a 53 71  rse->zTail = zSq
32ec4 6c 3b 0a 20 20 69 20 3d 20 30 3b 0a 20 20 61 73  l;.  i = 0;.  as
32ec5 73 65 72 74 28 20 70 7a 45 72 72 4d 73 67 21 3d  sert( pzErrMsg!=
32ec6 30 20 29 3b 0a 20 20 70 45 6e 67 69 6e 65 20 3d  0 );.  pEngine =
32ec7 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 6c   sqlite3ParserAl
32ec8 6c 6f 63 28 28 76 6f 69 64 2a 28 2a 29 28 73 69  loc((void*(*)(si
32ec9 7a 65 5f 74 29 29 73 71 6c 69 74 65 33 4d 61 6c  ze_t))sqlite3Mal
32eca 6c 6f 63 29 3b 0a 20 20 69 66 28 20 70 45 6e 67  loc);.  if( pEng
32ecb 69 6e 65 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62  ine==0 ){.    db
32ecc 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
32ecd 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   1;.    return S
32ece 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
32ecf 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
32ed0 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 30 20  e->pNewTable==0 
32ed1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
32ed2 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72  rse->pNewTrigger
32ed3 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
32ed4 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 3d 3d 30   pParse->nVar==0
32ed5 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
32ed6 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 3d 3d  arse->nVarExpr==
32ed7 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
32ed8 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41  Parse->nVarExprA
32ed9 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20 61 73 73  lloc==0 );.  ass
32eda 65 72 74 28 20 70 50 61 72 73 65 2d 3e 61 70 56  ert( pParse->apV
32edb 61 72 45 78 70 72 3d 3d 30 20 29 3b 0a 20 20 65  arExpr==0 );.  e
32edc 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 20 3d  nableLookaside =
32edd 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62   db->lookaside.b
32ede 45 6e 61 62 6c 65 64 3b 0a 20 20 69 66 28 20 64  Enabled;.  if( d
32edf 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74  b->lookaside.pSt
32ee0 61 72 74 20 29 20 64 62 2d 3e 6c 6f 6f 6b 61 73  art ) db->lookas
32ee1 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 31  ide.bEnabled = 1
32ee2 3b 0a 20 20 77 68 69 6c 65 28 20 21 64 62 2d 3e  ;.  while( !db->
32ee3 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20  mallocFailed && 
32ee4 7a 53 71 6c 5b 69 5d 21 3d 30 20 29 7b 0a 20 20  zSql[i]!=0 ){.  
32ee5 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 29    assert( i>=0 )
32ee6 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 73 4c  ;.    pParse->sL
32ee7 61 73 74 54 6f 6b 65 6e 2e 7a 20 3d 20 26 7a 53  astToken.z = &zS
32ee8 71 6c 5b 69 5d 3b 0a 20 20 20 20 70 50 61 72 73  ql[i];.    pPars
32ee9 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 6e 20  e->sLastToken.n 
32eea 3d 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65  = sqlite3GetToke
32eeb 6e 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  n((unsigned char
32eec 2a 29 26 7a 53 71 6c 5b 69 5d 2c 26 74 6f 6b 65  *)&zSql[i],&toke
32eed 6e 54 79 70 65 29 3b 0a 20 20 20 20 69 20 2b 3d  nType);.    i +=
32eee 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f   pParse->sLastTo
32eef 6b 65 6e 2e 6e 3b 0a 20 20 20 20 69 66 28 20 69  ken.n;.    if( i
32ef0 3e 6d 78 53 71 6c 4c 65 6e 20 29 7b 0a 20 20 20  >mxSqlLen ){.   
32ef1 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
32ef2 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20  SQLITE_TOOBIG;. 
32ef3 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32ef4 7d 0a 20 20 20 20 73 77 69 74 63 68 28 20 74 6f  }.    switch( to
32ef5 6b 65 6e 54 79 70 65 20 29 7b 0a 20 20 20 20 20  kenType ){.     
32ef6 20 63 61 73 65 20 54 4b 5f 53 50 41 43 45 3a 20   case TK_SPACE: 
32ef7 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62  {.        if( db
32ef8 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74  ->u1.isInterrupt
32ef9 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ed ){.          
32efa 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
32efb 70 50 61 72 73 65 2c 20 22 69 6e 74 65 72 72 75  pParse, "interru
32efc 70 74 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20  pt");.          
32efd 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c  pParse->rc = SQL
32efe 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20  ITE_INTERRUPT;. 
32eff 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62           goto ab
32f00 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20 20 20 20  ort_parse;.     
32f01 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65     }.        bre
32f02 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
32f03 20 20 63 61 73 65 20 54 4b 5f 49 4c 4c 45 47 41    case TK_ILLEGA
32f04 4c 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  L: {.        sql
32f05 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 2a  ite3DbFree(db, *
32f06 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20  pzErrMsg);.     
32f07 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73     *pzErrMsg = s
32f08 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
32f09 2c 20 22 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20  , "unrecognized 
32f0a 74 6f 6b 65 6e 3a 20 5c 22 25 54 5c 22 22 2c 0a  token: \"%T\"",.
32f0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32f0c 20 20 20 20 20 20 20 20 26 70 50 61 72 73 65 2d          &pParse-
32f0d 3e 73 4c 61 73 74 54 6f 6b 65 6e 29 3b 0a 20 20  >sLastToken);.  
32f0e 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20        nErr++;.  
32f0f 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74        goto abort
32f10 5f 70 61 72 73 65 3b 0a 20 20 20 20 20 20 7d 0a  _parse;.      }.
32f11 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45        case TK_SE
32f12 4d 49 3a 20 7b 0a 20 20 20 20 20 20 20 20 70 50  MI: {.        pP
32f13 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a  arse->zTail = &z
32f14 53 71 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20  Sql[i];.        
32f15 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 69 6e 74  /* Fall thru int
32f16 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 61  o the default ca
32f17 73 65 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20  se */.      }.  
32f18 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
32f19 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
32f1a 72 73 65 72 28 70 45 6e 67 69 6e 65 2c 20 74 6f  rser(pEngine, to
32f1b 6b 65 6e 54 79 70 65 2c 20 70 50 61 72 73 65 2d  kenType, pParse-
32f1c 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61  >sLastToken, pPa
32f1d 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 6c 61  rse);.        la
32f1e 73 74 54 6f 6b 65 6e 50 61 72 73 65 64 20 3d 20  stTokenParsed = 
32f1f 74 6f 6b 65 6e 54 79 70 65 3b 0a 20 20 20 20 20  tokenType;.     
32f20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72     if( pParse->r
32f21 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
32f22 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61            goto a
32f23 62 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20 20 20  bort_parse;.    
32f24 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72      }.        br
32f25 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
32f26 20 7d 0a 20 20 7d 0a 61 62 6f 72 74 5f 70 61 72   }.  }.abort_par
32f27 73 65 3a 0a 20 20 69 66 28 20 7a 53 71 6c 5b 69  se:.  if( zSql[i
32f28 5d 3d 3d 30 20 26 26 20 6e 45 72 72 3d 3d 30 20  ]==0 && nErr==0 
32f29 26 26 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53  && pParse->rc==S
32f2a 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
32f2b 69 66 28 20 6c 61 73 74 54 6f 6b 65 6e 50 61 72  if( lastTokenPar
32f2c 73 65 64 21 3d 54 4b 5f 53 45 4d 49 20 29 7b 0a  sed!=TK_SEMI ){.
32f2d 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 72        sqlite3Par
32f2e 73 65 72 28 70 45 6e 67 69 6e 65 2c 20 54 4b 5f  ser(pEngine, TK_
32f2f 53 45 4d 49 2c 20 70 50 61 72 73 65 2d 3e 73 4c  SEMI, pParse->sL
32f30 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61 72 73 65  astToken, pParse
32f31 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  );.      pParse-
32f32 3e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c 5b 69  >zTail = &zSql[i
32f33 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  ];.    }.    sql
32f34 69 74 65 33 50 61 72 73 65 72 28 70 45 6e 67 69  ite3Parser(pEngi
32f35 6e 65 2c 20 30 2c 20 70 50 61 72 73 65 2d 3e 73  ne, 0, pParse->s
32f36 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61 72 73  LastToken, pPars
32f37 65 29 3b 0a 20 20 7d 0a 23 69 66 64 65 66 20 59  e);.  }.#ifdef Y
32f38 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45  YTRACKMAXSTACKDE
32f39 50 54 48 0a 20 20 73 71 6c 69 74 65 33 53 74 61  PTH.  sqlite3Sta
32f3a 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54  tusSet(SQLITE_ST
32f3b 41 54 55 53 5f 50 41 52 53 45 52 5f 53 54 41 43  ATUS_PARSER_STAC
32f3c 4b 2c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  K,.      sqlite3
32f3d 50 61 72 73 65 72 53 74 61 63 6b 50 65 61 6b 28  ParserStackPeak(
32f3e 70 45 6e 67 69 6e 65 29 0a 20 20 29 3b 0a 23 65  pEngine).  );.#e
32f3f 6e 64 69 66 20 2f 2a 20 59 59 44 45 42 55 47 20  ndif /* YYDEBUG 
32f40 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73  */.  sqlite3Pars
32f41 65 72 46 72 65 65 28 70 45 6e 67 69 6e 65 2c 20  erFree(pEngine, 
32f42 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20  sqlite3_free);. 
32f43 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62   db->lookaside.b
32f44 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62 6c 65  Enabled = enable
32f45 4c 6f 6f 6b 61 73 69 64 65 3b 0a 20 20 69 66 28  Lookaside;.  if(
32f46 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
32f47 64 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d  d ){.    pParse-
32f48 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d  >rc = SQLITE_NOM
32f49 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50  EM;.  }.  if( pP
32f4a 61 72 73 65 2d 3e 72 63 21 3d 53 51 4c 49 54 45  arse->rc!=SQLITE
32f4b 5f 4f 4b 20 26 26 20 70 50 61 72 73 65 2d 3e 72  _OK && pParse->r
32f4c 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 26  c!=SQLITE_DONE &
32f4d 26 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73  & pParse->zErrMs
32f4e 67 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  g==0 ){.    sqli
32f4f 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 50  te3SetString(&pP
32f50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  arse->zErrMsg, d
32f51 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33  b, "%s", sqlite3
32f52 45 72 72 53 74 72 28 70 50 61 72 73 65 2d 3e 72  ErrStr(pParse->r
32f53 63 29 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  c));.  }.  asser
32f54 74 28 20 70 7a 45 72 72 4d 73 67 21 3d 30 20 29  t( pzErrMsg!=0 )
32f55 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
32f56 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 2a  zErrMsg ){.    *
32f57 70 7a 45 72 72 4d 73 67 20 3d 20 70 50 61 72 73  pzErrMsg = pPars
32f58 65 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20  e->zErrMsg;.    
32f59 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20  pParse->zErrMsg 
32f5a 3d 20 30 3b 0a 20 20 20 20 6e 45 72 72 2b 2b 3b  = 0;.    nErr++;
32f5b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73  .  }.  if( pPars
32f5c 65 2d 3e 70 56 64 62 65 20 26 26 20 70 50 61 72  e->pVdbe && pPar
32f5d 73 65 2d 3e 6e 45 72 72 3e 30 20 26 26 20 70 50  se->nErr>0 && pP
32f5e 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20  arse->nested==0 
32f5f 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
32f60 62 65 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  beDelete(pParse-
32f61 3e 70 56 64 62 65 29 3b 0a 20 20 20 20 70 50 61  >pVdbe);.    pPa
32f62 72 73 65 2d 3e 70 56 64 62 65 20 3d 20 30 3b 0a  rse->pVdbe = 0;.
32f63 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
32f64 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
32f65 41 43 48 45 0a 20 20 69 66 28 20 70 50 61 72 73  ACHE.  if( pPars
32f66 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a  e->nested==0 ){.
32f67 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
32f68 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 54  e(db, pParse->aT
32f69 61 62 6c 65 4c 6f 63 6b 29 3b 0a 20 20 20 20 70  ableLock);.    p
32f6a 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63  Parse->aTableLoc
32f6b 6b 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72 73  k = 0;.    pPars
32f6c 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 20 3d 20  e->nTableLock = 
32f6d 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69  0;.  }.#endif.#i
32f6e 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
32f6f 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
32f70 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
32f71 62 2c 20 70 50 61 72 73 65 2d 3e 61 70 56 74 61  b, pParse->apVta
32f72 62 4c 6f 63 6b 29 3b 0a 23 65 6e 64 69 66 0a 0a  bLock);.#endif..
32f73 20 20 69 66 28 20 21 49 4e 5f 44 45 43 4c 41 52    if( !IN_DECLAR
32f74 45 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 2f 2a  E_VTAB ){.    /*
32f75 20 49 66 20 74 68 65 20 70 50 61 72 73 65 2d 3e   If the pParse->
32f76 64 65 63 6c 61 72 65 56 74 61 62 20 66 6c 61 67  declareVtab flag
32f77 20 69 73 20 73 65 74 2c 20 64 6f 20 6e 6f 74 20   is set, do not 
32f78 64 65 6c 65 74 65 20 61 6e 79 20 74 61 62 6c 65  delete any table
32f79 20 0a 20 20 20 20 2a 2a 20 73 74 72 75 63 74 75   .    ** structu
32f7a 72 65 20 62 75 69 6c 74 20 75 70 20 69 6e 20 70  re built up in p
32f7b 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
32f7c 2e 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 63 6f  . The calling co
32f7d 64 65 20 28 73 65 65 20 76 74 61 62 2e 63 29 0a  de (see vtab.c).
32f7e 20 20 20 20 2a 2a 20 77 69 6c 6c 20 74 61 6b 65      ** will take
32f7f 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20   responsibility 
32f80 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65 20  for freeing the 
32f81 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 2e  Table structure.
32f82 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
32f83 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70  te3DeleteTable(p
32f84 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
32f85 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  );.  }..  sqlite
32f86 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 64  3DeleteTrigger(d
32f87 62 2c 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  b, pParse->pNewT
32f88 72 69 67 67 65 72 29 3b 0a 20 20 73 71 6c 69 74  rigger);.  sqlit
32f89 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 50 61  e3DbFree(db, pPa
32f8a 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 29 3b  rse->apVarExpr);
32f8b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
32f8c 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 41 6c  (db, pParse->aAl
32f8d 69 61 73 29 3b 0a 20 20 77 68 69 6c 65 28 20 70  ias);.  while( p
32f8e 50 61 72 73 65 2d 3e 70 41 69 6e 63 20 29 7b 0a  Parse->pAinc ){.
32f8f 20 20 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20      AutoincInfo 
32f90 2a 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 41 69  *p = pParse->pAi
32f91 6e 63 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  nc;.    pParse->
32f92 70 41 69 6e 63 20 3d 20 70 2d 3e 70 4e 65 78 74  pAinc = p->pNext
32f93 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
32f94 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a  ree(db, p);.  }.
32f95 20 20 77 68 69 6c 65 28 20 70 50 61 72 73 65 2d    while( pParse-
32f96 3e 70 5a 6f 6d 62 69 65 54 61 62 20 29 7b 0a 20  >pZombieTab ){. 
32f97 20 20 20 54 61 62 6c 65 20 2a 70 20 3d 20 70 50     Table *p = pP
32f98 61 72 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62  arse->pZombieTab
32f99 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70 5a  ;.    pParse->pZ
32f9a 6f 6d 62 69 65 54 61 62 20 3d 20 70 2d 3e 70 4e  ombieTab = p->pN
32f9b 65 78 74 5a 6f 6d 62 69 65 3b 0a 20 20 20 20 73  extZombie;.    s
32f9c 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c  qlite3DeleteTabl
32f9d 65 28 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  e(p);.  }.  if( 
32f9e 6e 45 72 72 3e 30 20 26 26 20 70 50 61 72 73 65  nErr>0 && pParse
32f9f 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc==SQLITE_OK 
32fa0 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72  ){.    pParse->r
32fa1 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
32fa2 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e  ;.  }.  return n
32fa3 45 72 72 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  Err;.}../*******
32fa4 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74  ******* End of t
32fa5 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a  okenize.c ******
32fa6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32fa7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32fa8 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
32fa9 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
32faa 6c 65 20 63 6f 6d 70 6c 65 74 65 2e 63 20 2a 2a  le complete.c **
32fab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32fac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32fad 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
32fae 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
32faf 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
32fb0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
32fb1 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
32fb2 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
32fb3 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
32fb4 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
32fb5 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
32fb6 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
32fb7 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
32fb8 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
32fb9 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
32fba 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
32fbb 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
32fbc 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
32fbd 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
32fbe 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
32fbf 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
32fc0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
32fc1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32fc2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32fc3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32fc4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
32fc5 2a 20 41 6e 20 74 6f 6b 65 6e 69 7a 65 72 20 66  * An tokenizer f
32fc6 6f 72 20 53 51 4c 0a 2a 2a 0a 2a 2a 20 54 68 69  or SQL.**.** Thi
32fc7 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
32fc8 43 20 63 6f 64 65 20 74 68 61 74 20 69 6d 70 6c  C code that impl
32fc9 65 6d 65 6e 74 73 20 74 68 65 20 73 71 6c 69 74  ements the sqlit
32fca 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 20 41 50  e3_complete() AP
32fcb 49 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 20  I..** This code 
32fcc 75 73 65 64 20 74 6f 20 62 65 20 70 61 72 74 20  used to be part 
32fcd 6f 66 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72  of the tokenizer
32fce 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 20  .c source file. 
32fcf 20 42 75 74 20 62 79 0a 2a 2a 20 73 65 70 61 72   But by.** separ
32fd0 61 74 69 6e 67 20 69 74 20 6f 75 74 2c 20 74 68  ating it out, th
32fd1 65 20 63 6f 64 65 20 77 69 6c 6c 20 62 65 20 61  e code will be a
32fd2 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 6f 6d 69  utomatically omi
32fd3 74 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 74 61  tted from.** sta
32fd4 74 69 63 20 6c 69 6e 6b 73 20 74 68 61 74 20 64  tic links that d
32fd5 6f 20 6e 6f 74 20 75 73 65 20 69 74 2e 0a 2a 2a  o not use it..**
32fd6 0a 2a 2a 20 24 49 64 3a 20 63 6f 6d 70 6c 65 74  .** $Id: complet
32fd7 65 2e 63 2c 76 20 31 2e 38 20 32 30 30 39 2f 30  e.c,v 1.8 2009/0
32fd8 34 2f 32 38 20 30 34 3a 34 36 3a 34 32 20 64 72  4/28 04:46:42 dr
32fd9 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64  h Exp $.*/.#ifnd
32fda 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
32fdb 4f 4d 50 4c 45 54 45 0a 0a 2f 2a 0a 2a 2a 20 54  OMPLETE../*.** T
32fdc 68 69 73 20 69 73 20 64 65 66 69 6e 65 64 20 69  his is defined i
32fdd 6e 20 74 6f 6b 65 6e 69 7a 65 2e 63 2e 20 20 57  n tokenize.c.  W
32fde 65 20 6a 75 73 74 20 68 61 76 65 20 74 6f 20 69  e just have to i
32fdf 6d 70 6f 72 74 20 74 68 65 20 64 65 66 69 6e 69  mport the defini
32fe0 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  tion..*/.#ifndef
32fe1 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41   SQLITE_AMALGAMA
32fe2 54 49 4f 4e 0a 23 69 66 64 65 66 20 53 51 4c 49  TION.#ifdef SQLI
32fe3 54 45 5f 41 53 43 49 49 0a 53 51 4c 49 54 45 5f  TE_ASCII.SQLITE_
32fe4 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
32fe5 61 72 20 73 71 6c 69 74 65 33 49 73 41 73 63 69  ar sqlite3IsAsci
32fe6 69 49 64 43 68 61 72 5b 5d 3b 0a 23 64 65 66 69  iIdChar[];.#defi
32fe7 6e 65 20 49 64 43 68 61 72 28 43 29 20 20 28 28  ne IdChar(C)  ((
32fe8 28 63 3d 43 29 26 30 78 38 30 29 21 3d 30 20 7c  (c=C)&0x80)!=0 |
32fe9 7c 20 28 63 3e 30 78 31 66 20 26 26 20 73 71 6c  | (c>0x1f && sql
32fea 69 74 65 33 49 73 41 73 63 69 69 49 64 43 68 61  ite3IsAsciiIdCha
32feb 72 5b 63 2d 30 78 32 30 5d 29 29 0a 23 65 6e 64  r[c-0x20])).#end
32fec 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
32fed 5f 45 42 43 44 49 43 0a 53 51 4c 49 54 45 5f 50  _EBCDIC.SQLITE_P
32fee 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
32fef 72 20 73 71 6c 69 74 65 33 49 73 45 62 63 64 69  r sqlite3IsEbcdi
32ff0 63 49 64 43 68 61 72 5b 5d 3b 0a 23 64 65 66 69  cIdChar[];.#defi
32ff1 6e 65 20 49 64 43 68 61 72 28 43 29 20 20 28 28  ne IdChar(C)  ((
32ff2 28 63 3d 43 29 3e 3d 30 78 34 32 20 26 26 20 73  (c=C)>=0x42 && s
32ff3 71 6c 69 74 65 33 49 73 45 62 63 64 69 63 49 64  qlite3IsEbcdicId
32ff4 43 68 61 72 5b 63 2d 30 78 34 30 5d 29 29 0a 23  Char[c-0x40])).#
32ff5 65 6e 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20  endif.#endif /* 
32ff6 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54  SQLITE_AMALGAMAT
32ff7 49 4f 4e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54  ION */.../*.** T
32ff8 6f 6b 65 6e 20 74 79 70 65 73 20 75 73 65 64 20  oken types used 
32ff9 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  by the sqlite3_c
32ffa 6f 6d 70 6c 65 74 65 28 29 20 72 6f 75 74 69 6e  omplete() routin
32ffb 65 2e 20 20 53 65 65 20 74 68 65 20 68 65 61 64  e.  See the head
32ffc 65 72 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 6f  er.** comments o
32ffd 6e 20 74 68 61 74 20 70 72 6f 63 65 64 75 72 65  n that procedure
32ffe 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
32fff 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
33000 23 64 65 66 69 6e 65 20 74 6b 53 45 4d 49 20 20  #define tkSEMI  
33001 20 20 30 0a 23 64 65 66 69 6e 65 20 74 6b 57 53    0.#define tkWS
33002 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20        1.#define 
33003 74 6b 4f 54 48 45 52 20 20 20 32 0a 23 64 65 66  tkOTHER   2.#def
33004 69 6e 65 20 74 6b 45 58 50 4c 41 49 4e 20 33 0a  ine tkEXPLAIN 3.
33005 23 64 65 66 69 6e 65 20 74 6b 43 52 45 41 54 45  #define tkCREATE
33006 20 20 34 0a 23 64 65 66 69 6e 65 20 74 6b 54 45    4.#define tkTE
33007 4d 50 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20  MP    5.#define 
33008 74 6b 54 52 49 47 47 45 52 20 36 0a 23 64 65 66  tkTRIGGER 6.#def
33009 69 6e 65 20 74 6b 45 4e 44 20 20 20 20 20 37 0a  ine tkEND     7.
3300a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
3300b 55 45 20 69 66 20 74 68 65 20 67 69 76 65 6e 20  UE if the given 
3300c 53 51 4c 20 73 74 72 69 6e 67 20 65 6e 64 73 20  SQL string ends 
3300d 69 6e 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a  in a semicolon..
3300e 2a 2a 0a 2a 2a 20 53 70 65 63 69 61 6c 20 68 61  **.** Special ha
3300f 6e 64 6c 69 6e 67 20 69 73 20 72 65 71 75 69 72  ndling is requir
33010 65 20 66 6f 72 20 43 52 45 41 54 45 20 54 52 49  e for CREATE TRI
33011 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74 73 2e  GGER statements.
33012 0a 2a 2a 20 57 68 65 6e 65 76 65 72 20 74 68 65  .** Whenever the
33013 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20   CREATE TRIGGER 
33014 6b 65 79 77 6f 72 64 73 20 61 72 65 20 73 65 65  keywords are see
33015 6e 2c 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  n, the statement
33016 0a 2a 2a 20 6d 75 73 74 20 65 6e 64 20 77 69 74  .** must end wit
33017 68 20 22 3b 45 4e 44 3b 22 2e 0a 2a 2a 0a 2a 2a  h ";END;"..**.**
33018 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61   This implementa
33019 74 69 6f 6e 20 75 73 65 73 20 61 20 73 74 61 74  tion uses a stat
3301a 65 20 6d 61 63 68 69 6e 65 20 77 69 74 68 20 37  e machine with 7
3301b 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20   states:.**.**  
3301c 20 28 30 29 20 53 54 41 52 54 20 20 20 20 20 41   (0) START     A
3301d 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
3301e 6f 72 20 65 6e 64 20 6f 66 20 61 6e 20 53 51 4c  or end of an SQL
3301f 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 69   statement.  Thi
33020 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 20 20 20  s routine.**    
33021 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74               ret
33022 75 72 6e 73 20 31 20 69 66 20 69 74 20 65 6e 64  urns 1 if it end
33023 73 20 69 6e 20 74 68 65 20 53 54 41 52 54 20 73  s in the START s
33024 74 61 74 65 20 61 6e 64 20 30 20 69 66 20 69 74  tate and 0 if it
33025 20 65 6e 64 73 0a 2a 2a 20 20 20 20 20 20 20 20   ends.**        
33026 20 20 20 20 20 20 20 20 20 69 6e 20 61 6e 79 20           in any 
33027 6f 74 68 65 72 20 73 74 61 74 65 2e 0a 2a 2a 0a  other state..**.
33028 2a 2a 20 20 20 28 31 29 20 4e 4f 52 4d 41 4c 20  **   (1) NORMAL 
33029 20 20 20 57 65 20 61 72 65 20 69 6e 20 74 68 65     We are in the
3302a 20 6d 69 64 64 6c 65 20 6f 66 20 73 74 61 74 65   middle of state
3302b 6d 65 6e 74 20 77 68 69 63 68 20 65 6e 64 73 20  ment which ends 
3302c 77 69 74 68 20 61 20 73 69 6e 67 6c 65 0a 2a 2a  with a single.**
3302d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3302e 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a   semicolon..**.*
3302f 2a 20 20 20 28 32 29 20 45 58 50 4c 41 49 4e 20  *   (2) EXPLAIN 
33030 20 20 54 68 65 20 6b 65 79 77 6f 72 64 20 45 58    The keyword EX
33031 50 4c 41 49 4e 20 68 61 73 20 62 65 65 6e 20 73  PLAIN has been s
33032 65 65 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e  een at the begin
33033 6e 69 6e 67 20 6f 66 20 0a 2a 2a 20 20 20 20 20  ning of .**     
33034 20 20 20 20 20 20 20 20 20 20 20 20 61 20 73 74              a st
33035 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20  atement..**.**  
33036 20 28 33 29 20 43 52 45 41 54 45 20 20 20 20 54   (3) CREATE    T
33037 68 65 20 6b 65 79 77 6f 72 64 20 43 52 45 41 54  he keyword CREAT
33038 45 20 68 61 73 20 62 65 65 6e 20 73 65 65 6e 20  E has been seen 
33039 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
3303a 20 6f 66 20 61 0a 2a 2a 20 20 20 20 20 20 20 20   of a.**        
3303b 20 20 20 20 20 20 20 20 20 73 74 61 74 65 6d 65           stateme
3303c 6e 74 2c 20 70 6f 73 73 69 62 6c 79 20 70 72 65  nt, possibly pre
3303d 63 65 65 64 65 64 20 62 79 20 45 58 50 4c 41 49  ceeded by EXPLAI
3303e 4e 20 61 6e 64 2f 6f 72 20 66 6f 6c 6c 6f 77 65  N and/or followe
3303f 64 20 62 79 0a 2a 2a 20 20 20 20 20 20 20 20 20  d by.**         
33040 20 20 20 20 20 20 20 20 54 45 4d 50 20 6f 72 20          TEMP or 
33041 54 45 4d 50 4f 52 41 52 59 0a 2a 2a 0a 2a 2a 20  TEMPORARY.**.** 
33042 20 20 28 34 29 20 54 52 49 47 47 45 52 20 20 20    (4) TRIGGER   
33043 57 65 20 61 72 65 20 69 6e 20 74 68 65 20 6d 69  We are in the mi
33044 64 64 6c 65 20 6f 66 20 61 20 74 72 69 67 67 65  ddle of a trigge
33045 72 20 64 65 66 69 6e 69 74 69 6f 6e 20 74 68 61  r definition tha
33046 74 20 6d 75 73 74 20 62 65 0a 2a 2a 20 20 20 20  t must be.**    
33047 20 20 20 20 20 20 20 20 20 20 20 20 20 65 6e 64               end
33048 65 64 20 62 79 20 61 20 73 65 6d 69 63 6f 6c 6f  ed by a semicolo
33049 6e 2c 20 74 68 65 20 6b 65 79 77 6f 72 64 20 45  n, the keyword E
3304a 4e 44 2c 20 61 6e 64 20 61 6e 6f 74 68 65 72 20  ND, and another 
3304b 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a  semicolon..**.**
3304c 20 20 20 28 35 29 20 53 45 4d 49 20 20 20 20 20     (5) SEMI     
3304d 20 57 65 27 76 65 20 73 65 65 6e 20 74 68 65 20   We've seen the 
3304e 66 69 72 73 74 20 73 65 6d 69 63 6f 6c 6f 6e 20  first semicolon 
3304f 69 6e 20 74 68 65 20 22 3b 45 4e 44 3b 22 20 74  in the ";END;" t
33050 68 61 74 20 6f 63 63 75 72 73 20 61 74 0a 2a 2a  hat occurs at.**
33051 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33052 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 74 72   the end of a tr
33053 69 67 67 65 72 20 64 65 66 69 6e 69 74 69 6f 6e  igger definition
33054 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 36 29 20 45 4e  ..**.**   (6) EN
33055 44 20 20 20 20 20 20 20 57 65 27 76 65 20 73 65  D       We've se
33056 65 6e 20 74 68 65 20 22 3b 45 4e 44 22 20 6f 66  en the ";END" of
33057 20 74 68 65 20 22 3b 45 4e 44 3b 22 20 74 68 61   the ";END;" tha
33058 74 20 6f 63 63 75 72 73 20 61 74 20 74 68 65 20  t occurs at the 
33059 65 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  end.**          
3305a 20 20 20 20 20 20 20 6f 66 20 61 20 74 72 69 67         of a trig
3305b 67 65 72 20 64 69 66 69 6e 69 74 69 6f 6e 2e 0a  ger difinition..
3305c 2a 2a 0a 2a 2a 20 54 72 61 6e 73 69 74 69 6f 6e  **.** Transition
3305d 73 20 62 65 74 77 65 65 6e 20 73 74 61 74 65 73  s between states
3305e 20 61 62 6f 76 65 20 61 72 65 20 64 65 74 65 72   above are deter
3305f 6d 69 6e 65 64 20 62 79 20 74 6f 6b 65 6e 73 20  mined by tokens 
33060 65 78 74 72 61 63 74 65 64 0a 2a 2a 20 66 72 6f  extracted.** fro
33061 6d 20 74 68 65 20 69 6e 70 75 74 2e 20 20 54 68  m the input.  Th
33062 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 6f 6b 65  e following toke
33063 6e 73 20 61 72 65 20 73 69 67 6e 69 66 69 63 61  ns are significa
33064 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 30 29 20  nt:.**.**   (0) 
33065 74 6b 53 45 4d 49 20 20 20 20 20 20 41 20 73 65  tkSEMI      A se
33066 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 20 20 20 28 31  micolon..**   (1
33067 29 20 74 6b 57 53 20 20 20 20 20 20 20 20 57 68  ) tkWS        Wh
33068 69 74 65 73 70 61 63 65 0a 2a 2a 20 20 20 28 32  itespace.**   (2
33069 29 20 74 6b 4f 54 48 45 52 20 20 20 20 20 41 6e  ) tkOTHER     An
3306a 79 20 6f 74 68 65 72 20 53 51 4c 20 74 6f 6b 65  y other SQL toke
3306b 6e 2e 0a 2a 2a 20 20 20 28 33 29 20 74 6b 45 58  n..**   (3) tkEX
3306c 50 4c 41 49 4e 20 20 20 54 68 65 20 22 65 78 70  PLAIN   The "exp
3306d 6c 61 69 6e 22 20 6b 65 79 77 6f 72 64 2e 0a 2a  lain" keyword..*
3306e 2a 20 20 20 28 34 29 20 74 6b 43 52 45 41 54 45  *   (4) tkCREATE
3306f 20 20 20 20 54 68 65 20 22 63 72 65 61 74 65 22      The "create"
33070 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28   keyword..**   (
33071 35 29 20 74 6b 54 45 4d 50 20 20 20 20 20 20 54  5) tkTEMP      T
33072 68 65 20 22 74 65 6d 70 22 20 6f 72 20 22 74 65  he "temp" or "te
33073 6d 70 6f 72 61 72 79 22 20 6b 65 79 77 6f 72 64  mporary" keyword
33074 2e 0a 2a 2a 20 20 20 28 36 29 20 74 6b 54 52 49  ..**   (6) tkTRI
33075 47 47 45 52 20 20 20 54 68 65 20 22 74 72 69 67  GGER   The "trig
33076 67 65 72 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a  ger" keyword..**
33077 20 20 20 28 37 29 20 74 6b 45 4e 44 20 20 20 20     (7) tkEND    
33078 20 20 20 54 68 65 20 22 65 6e 64 22 20 6b 65 79     The "end" key
33079 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 69 74  word..**.** Whit
3307a 65 73 70 61 63 65 20 6e 65 76 65 72 20 63 61 75  espace never cau
3307b 73 65 73 20 61 20 73 74 61 74 65 20 74 72 61 6e  ses a state tran
3307c 73 69 74 69 6f 6e 20 61 6e 64 20 69 73 20 61 6c  sition and is al
3307d 77 61 79 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a  ways ignored..**
3307e 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c  .** If we compil
3307f 65 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4f 4d  e with SQLITE_OM
33080 49 54 5f 54 52 49 47 47 45 52 2c 20 61 6c 6c 20  IT_TRIGGER, all 
33081 6f 66 20 74 68 65 20 63 6f 6d 70 75 74 61 74 69  of the computati
33082 6f 6e 20 6e 65 65 64 65 64 0a 2a 2a 20 74 6f 20  on needed.** to 
33083 72 65 63 6f 67 6e 69 7a 65 20 74 68 65 20 65 6e  recognize the en
33084 64 20 6f 66 20 61 20 74 72 69 67 67 65 72 20 63  d of a trigger c
33085 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20  an be omitted.  
33086 41 6c 6c 20 77 65 20 68 61 76 65 20 74 6f 20 64  All we have to d
33087 6f 0a 2a 2a 20 69 73 20 6c 6f 6f 6b 20 66 6f 72  o.** is look for
33088 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 20 74 68 61   a semicolon tha
33089 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66  t is not part of
3308a 20 61 6e 20 73 74 72 69 6e 67 20 6f 72 20 63 6f   an string or co
3308b 6d 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  mment..*/.SQLITE
3308c 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
3308d 5f 63 6f 6d 70 6c 65 74 65 28 63 6f 6e 73 74 20  _complete(const 
3308e 63 68 61 72 20 2a 7a 53 71 6c 29 7b 0a 20 20 75  char *zSql){.  u
3308f 38 20 73 74 61 74 65 20 3d 20 30 3b 20 20 20 2f  8 state = 0;   /
33090 2a 20 43 75 72 72 65 6e 74 20 73 74 61 74 65 2c  * Current state,
33091 20 75 73 69 6e 67 20 6e 75 6d 62 65 72 73 20 64   using numbers d
33092 65 66 69 6e 65 64 20 69 6e 20 68 65 61 64 65 72  efined in header
33093 20 63 6f 6d 6d 65 6e 74 20 2a 2f 0a 20 20 75 38   comment */.  u8
33094 20 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 2f 2a   token;       /*
33095 20 56 61 6c 75 65 20 6f 66 20 74 68 65 20 6e 65   Value of the ne
33096 78 74 20 74 6f 6b 65 6e 20 2a 2f 0a 0a 23 69 66  xt token */..#if
33097 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
33098 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 41 20  _TRIGGER.  /* A 
33099 63 6f 6d 70 6c 65 78 20 73 74 61 74 65 6d 65 6e  complex statemen
3309a 74 20 6d 61 63 68 69 6e 65 20 75 73 65 64 20 74  t machine used t
3309b 6f 20 64 65 74 65 63 74 20 74 68 65 20 65 6e 64  o detect the end
3309c 20 6f 66 20 61 20 43 52 45 41 54 45 20 54 52 49   of a CREATE TRI
3309d 47 47 45 52 0a 20 20 2a 2a 20 73 74 61 74 65 6d  GGER.  ** statem
3309e 65 6e 74 2e 20 20 54 68 69 73 20 69 73 20 74 68  ent.  This is th
3309f 65 20 6e 6f 72 6d 61 6c 20 63 61 73 65 2e 0a 20  e normal case.. 
330a0 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   */.  static con
330a1 73 74 20 75 38 20 74 72 61 6e 73 5b 37 5d 5b 38  st u8 trans[7][8
330a2 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20  ] = {.          
330a3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f             /* To
330a4 6b 65 6e 3a 20 20 20 20 20 20 20 20 20 20 20 20  ken:            
330a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
330a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
330a7 20 20 20 20 2a 2f 0a 20 20 20 20 20 2f 2a 20 53      */.     /* S
330a8 74 61 74 65 3a 20 20 20 20 20 20 20 2a 2a 20 20  tate:       **  
330a9 53 45 4d 49 20 20 57 53 20 20 4f 54 48 45 52 20  SEMI  WS  OTHER 
330aa 45 58 50 4c 41 49 4e 20 20 43 52 45 41 54 45 20  EXPLAIN  CREATE 
330ab 20 54 45 4d 50 20 20 54 52 49 47 47 45 52 20 20   TEMP  TRIGGER  
330ac 45 4e 44 20 20 2a 2f 0a 20 20 20 20 20 2f 2a 20  END  */.     /* 
330ad 30 20 20 20 53 54 41 52 54 3a 20 2a 2f 20 7b 20  0   START: */ { 
330ae 20 20 20 30 2c 20 20 30 2c 20 20 20 20 20 31 2c     0,  0,     1,
330af 20 20 20 20 20 20 32 2c 20 20 20 20 20 20 33 2c        2,      3,
330b0 20 20 20 20 31 2c 20 20 20 20 20 20 20 31 2c 20      1,       1, 
330b1 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a    1,  },.     /*
330b2 20 31 20 20 4e 4f 52 4d 41 4c 3a 20 2a 2f 20 7b   1  NORMAL: */ {
330b3 20 20 20 20 30 2c 20 20 31 2c 20 20 20 20 20 31      0,  1,     1
330b4 2c 20 20 20 20 20 20 31 2c 20 20 20 20 20 20 31  ,      1,      1
330b5 2c 20 20 20 20 31 2c 20 20 20 20 20 20 20 31 2c  ,    1,       1,
330b6 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20 20 2f     1,  },.     /
330b7 2a 20 32 20 45 58 50 4c 41 49 4e 3a 20 2a 2f 20  * 2 EXPLAIN: */ 
330b8 7b 20 20 20 20 30 2c 20 20 32 2c 20 20 20 20 20  {    0,  2,     
330b9 32 2c 20 20 20 20 20 20 31 2c 20 20 20 20 20 20  2,      1,      
330ba 33 2c 20 20 20 20 31 2c 20 20 20 20 20 20 20 31  3,    1,       1
330bb 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20 20  ,   1,  },.     
330bc 2f 2a 20 33 20 20 43 52 45 41 54 45 3a 20 2a 2f  /* 3  CREATE: */
330bd 20 7b 20 20 20 20 30 2c 20 20 33 2c 20 20 20 20   {    0,  3,    
330be 20 31 2c 20 20 20 20 20 20 31 2c 20 20 20 20 20   1,      1,     
330bf 20 31 2c 20 20 20 20 33 2c 20 20 20 20 20 20 20   1,    3,       
330c0 34 2c 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20  4,   1,  },.    
330c1 20 2f 2a 20 34 20 54 52 49 47 47 45 52 3a 20 2a   /* 4 TRIGGER: *
330c2 2f 20 7b 20 20 20 20 35 2c 20 20 34 2c 20 20 20  / {    5,  4,   
330c3 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20 20    4,      4,    
330c4 20 20 34 2c 20 20 20 20 34 2c 20 20 20 20 20 20    4,    4,      
330c5 20 34 2c 20 20 20 34 2c 20 20 7d 2c 0a 20 20 20   4,   4,  },.   
330c6 20 20 2f 2a 20 35 20 20 20 20 53 45 4d 49 3a 20    /* 5    SEMI: 
330c7 2a 2f 20 7b 20 20 20 20 35 2c 20 20 35 2c 20 20  */ {    5,  5,  
330c8 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20     4,      4,   
330c9 20 20 20 34 2c 20 20 20 20 34 2c 20 20 20 20 20     4,    4,     
330ca 20 20 34 2c 20 20 20 36 2c 20 20 7d 2c 0a 20 20    4,   6,  },.  
330cb 20 20 20 2f 2a 20 36 20 20 20 20 20 45 4e 44 3a     /* 6     END:
330cc 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 36 2c 20   */ {    0,  6, 
330cd 20 20 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20      4,      4,  
330ce 20 20 20 20 34 2c 20 20 20 20 34 2c 20 20 20 20      4,    4,    
330cf 20 20 20 34 2c 20 20 20 34 2c 20 20 7d 2c 0a 20     4,   4,  },. 
330d0 20 7d 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 49   };.#else.  /* I
330d1 66 20 74 72 69 67 67 65 72 73 20 61 72 65 20 6e  f triggers are n
330d2 6f 74 20 73 75 70 70 6f 72 65 64 20 62 79 20 74  ot suppored by t
330d3 68 69 73 20 63 6f 6d 70 69 6c 65 20 74 68 65 6e  his compile then
330d4 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6d   the statement m
330d5 61 63 68 69 6e 65 0a 20 20 2a 2a 20 75 73 65 64  achine.  ** used
330d6 20 74 6f 20 64 65 74 65 63 74 20 74 68 65 20 65   to detect the e
330d7 6e 64 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e  nd of a statemen
330d8 74 20 69 73 20 6d 75 63 68 20 73 69 6d 70 6c 69  t is much simpli
330d9 65 72 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63  er.  */.  static
330da 20 63 6f 6e 73 74 20 75 38 20 74 72 61 6e 73 5b   const u8 trans[
330db 32 5d 5b 33 5d 20 3d 20 7b 0a 20 20 20 20 20 20  2][3] = {.      
330dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
330dd 2a 20 54 6f 6b 65 6e 3a 20 20 20 20 20 20 20 20  * Token:        
330de 20 20 20 2a 2f 0a 20 20 20 20 20 2f 2a 20 53 74     */.     /* St
330df 61 74 65 3a 20 20 20 20 20 20 20 2a 2a 20 20 53  ate:       **  S
330e0 45 4d 49 20 20 57 53 20 20 4f 54 48 45 52 20 2a  EMI  WS  OTHER *
330e1 2f 0a 20 20 20 20 20 2f 2a 20 30 20 20 20 53 54  /.     /* 0   ST
330e2 41 52 54 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20  ART: */ {    0, 
330e3 20 30 2c 20 20 20 20 20 31 2c 20 7d 2c 0a 20 20   0,     1, },.  
330e4 20 20 20 2f 2a 20 31 20 20 4e 4f 52 4d 41 4c 3a     /* 1  NORMAL:
330e5 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 31 2c 20   */ {    0,  1, 
330e6 20 20 20 20 31 2c 20 7d 2c 0a 20 20 7d 3b 0a 23      1, },.  };.#
330e7 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
330e8 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a  OMIT_TRIGGER */.
330e9 0a 20 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c 20  .  while( *zSql 
330ea 29 7b 0a 20 20 20 20 73 77 69 74 63 68 28 20 2a  ){.    switch( *
330eb 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20 20 63 61  zSql ){.      ca
330ec 73 65 20 27 3b 27 3a 20 7b 20 20 2f 2a 20 41 20  se ';': {  /* A 
330ed 73 65 6d 69 63 6f 6c 6f 6e 20 2a 2f 0a 20 20 20  semicolon */.   
330ee 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 53       token = tkS
330ef 45 4d 49 3b 0a 20 20 20 20 20 20 20 20 62 72 65  EMI;.        bre
330f0 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
330f1 20 20 63 61 73 65 20 27 20 27 3a 0a 20 20 20 20    case ' ':.    
330f2 20 20 63 61 73 65 20 27 5c 72 27 3a 0a 20 20 20    case '\r':.   
330f3 20 20 20 63 61 73 65 20 27 5c 74 27 3a 0a 20 20     case '\t':.  
330f4 20 20 20 20 63 61 73 65 20 27 5c 6e 27 3a 0a 20      case '\n':. 
330f5 20 20 20 20 20 63 61 73 65 20 27 5c 66 27 3a 20       case '\f': 
330f6 7b 20 20 2f 2a 20 57 68 69 74 65 20 73 70 61 63  {  /* White spac
330f7 65 20 69 73 20 69 67 6e 6f 72 65 64 20 2a 2f 0a  e is ignored */.
330f8 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
330f9 74 6b 57 53 3b 0a 20 20 20 20 20 20 20 20 62 72  tkWS;.        br
330fa 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
330fb 20 20 20 63 61 73 65 20 27 2f 27 3a 20 7b 20 20     case '/': {  
330fc 20 2f 2a 20 43 2d 73 74 79 6c 65 20 63 6f 6d 6d   /* C-style comm
330fd 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20  ents */.        
330fe 69 66 28 20 7a 53 71 6c 5b 31 5d 21 3d 27 2a 27  if( zSql[1]!='*'
330ff 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 74 6f   ){.          to
33100 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20  ken = tkOTHER;. 
33101 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
33102 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
33103 20 20 7a 53 71 6c 20 2b 3d 20 32 3b 0a 20 20 20    zSql += 2;.   
33104 20 20 20 20 20 77 68 69 6c 65 28 20 7a 53 71 6c       while( zSql
33105 5b 30 5d 20 26 26 20 28 7a 53 71 6c 5b 30 5d 21  [0] && (zSql[0]!
33106 3d 27 2a 27 20 7c 7c 20 7a 53 71 6c 5b 31 5d 21  ='*' || zSql[1]!
33107 3d 27 2f 27 29 20 29 7b 20 7a 53 71 6c 2b 2b 3b  ='/') ){ zSql++;
33108 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a   }.        if( z
33109 53 71 6c 5b 30 5d 3d 3d 30 20 29 20 72 65 74 75  Sql[0]==0 ) retu
3310a 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 7a 53  rn 0;.        zS
3310b 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 74 6f  ql++;.        to
3310c 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a 20 20 20 20  ken = tkWS;.    
3310d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3310e 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27 2d   }.      case '-
3310f 27 3a 20 7b 20 20 20 2f 2a 20 53 51 4c 2d 73 74  ': {   /* SQL-st
33110 79 6c 65 20 63 6f 6d 6d 65 6e 74 73 20 66 72 6f  yle comments fro
33111 6d 20 22 2d 2d 22 20 74 6f 20 65 6e 64 20 6f 66  m "--" to end of
33112 20 6c 69 6e 65 20 2a 2f 0a 20 20 20 20 20 20 20   line */.       
33113 20 69 66 28 20 7a 53 71 6c 5b 31 5d 21 3d 27 2d   if( zSql[1]!='-
33114 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 74  ' ){.          t
33115 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a  oken = tkOTHER;.
33116 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
33117 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
33118 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c 20     while( *zSql 
33119 26 26 20 2a 7a 53 71 6c 21 3d 27 5c 6e 27 20 29  && *zSql!='\n' )
3311a 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20  { zSql++; }.    
3311b 20 20 20 20 69 66 28 20 2a 7a 53 71 6c 3d 3d 30      if( *zSql==0
3311c 20 29 20 72 65 74 75 72 6e 20 73 74 61 74 65 3d   ) return state=
3311d 3d 30 3b 0a 20 20 20 20 20 20 20 20 74 6f 6b 65  =0;.        toke
3311e 6e 20 3d 20 74 6b 57 53 3b 0a 20 20 20 20 20 20  n = tkWS;.      
3311f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
33120 0a 20 20 20 20 20 20 63 61 73 65 20 27 5b 27 3a  .      case '[':
33121 20 7b 20 20 20 2f 2a 20 4d 69 63 72 6f 73 6f 66   {   /* Microsof
33122 74 2d 73 74 79 6c 65 20 69 64 65 6e 74 69 66 69  t-style identifi
33123 65 72 73 20 69 6e 20 5b 2e 2e 2e 5d 20 2a 2f 0a  ers in [...] */.
33124 20 20 20 20 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a          zSql++;.
33125 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a          while( *
33126 7a 53 71 6c 20 26 26 20 2a 7a 53 71 6c 21 3d 27  zSql && *zSql!='
33127 5d 27 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a  ]' ){ zSql++; }.
33128 20 20 20 20 20 20 20 20 69 66 28 20 2a 7a 53 71          if( *zSq
33129 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  l==0 ) return 0;
3312a 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d  .        token =
3312b 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20   tkOTHER;.      
3312c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
3312d 0a 20 20 20 20 20 20 63 61 73 65 20 27 60 27 3a  .      case '`':
3312e 20 20 20 20 20 2f 2a 20 47 72 61 76 65 2d 61 63       /* Grave-ac
3312f 63 65 6e 74 20 71 75 6f 74 65 64 20 73 79 6d 62  cent quoted symb
33130 6f 6c 73 20 75 73 65 64 20 62 79 20 4d 79 53 51  ols used by MySQ
33131 4c 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  L */.      case 
33132 27 22 27 3a 20 20 20 20 20 2f 2a 20 73 69 6e 67  '"':     /* sing
33133 6c 65 2d 20 61 6e 64 20 64 6f 75 62 6c 65 2d 71  le- and double-q
33134 75 6f 74 65 64 20 73 74 72 69 6e 67 73 20 2a 2f  uoted strings */
33135 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c 27 27  .      case '\''
33136 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  : {.        int 
33137 63 20 3d 20 2a 7a 53 71 6c 3b 0a 20 20 20 20 20  c = *zSql;.     
33138 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20 20     zSql++;.     
33139 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c 20     while( *zSql 
3313a 26 26 20 2a 7a 53 71 6c 21 3d 63 20 29 7b 20 7a  && *zSql!=c ){ z
3313b 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20  Sql++; }.       
3313c 20 69 66 28 20 2a 7a 53 71 6c 3d 3d 30 20 29 20   if( *zSql==0 ) 
3313d 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
3313e 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45    token = tkOTHE
3313f 52 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  R;.        break
33140 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
33141 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
33142 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20     int c;.      
33143 20 20 69 66 28 20 49 64 43 68 61 72 28 28 75 38    if( IdChar((u8
33144 29 2a 7a 53 71 6c 29 20 29 7b 0a 20 20 20 20 20  )*zSql) ){.     
33145 20 20 20 20 20 2f 2a 20 4b 65 79 77 6f 72 64 73       /* Keywords
33146 20 61 6e 64 20 75 6e 71 75 6f 74 65 64 20 69 64   and unquoted id
33147 65 6e 74 69 66 69 65 72 73 20 2a 2f 0a 20 20 20  entifiers */.   
33148 20 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 0a         int nId;.
33149 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6e 49            for(nI
3314a 64 3d 31 3b 20 49 64 43 68 61 72 28 7a 53 71 6c  d=1; IdChar(zSql
3314b 5b 6e 49 64 5d 29 3b 20 6e 49 64 2b 2b 29 7b 7d  [nId]); nId++){}
3314c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
3314d 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20  MIT_TRIGGER.    
3314e 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
3314f 4f 54 48 45 52 3b 0a 23 65 6c 73 65 0a 20 20 20  OTHER;.#else.   
33150 20 20 20 20 20 20 20 73 77 69 74 63 68 28 20 2a         switch( *
33151 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  zSql ){.        
33152 20 20 20 20 63 61 73 65 20 27 63 27 3a 20 63 61      case 'c': ca
33153 73 65 20 27 43 27 3a 20 7b 0a 20 20 20 20 20 20  se 'C': {.      
33154 20 20 20 20 20 20 20 20 69 66 28 20 6e 49 64 3d          if( nId=
33155 3d 36 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  =6 && sqlite3Str
33156 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 63 72 65  NICmp(zSql, "cre
33157 61 74 65 22 2c 20 36 29 3d 3d 30 20 29 7b 0a 20  ate", 6)==0 ){. 
33158 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
33159 6f 6b 65 6e 20 3d 20 74 6b 43 52 45 41 54 45 3b  oken = tkCREATE;
3315a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
3315b 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
3315c 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
3315d 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20  OTHER;.         
3315e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
3315f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
33160 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
33161 20 20 20 20 20 20 63 61 73 65 20 27 74 27 3a 20        case 't': 
33162 63 61 73 65 20 27 54 27 3a 20 7b 0a 20 20 20 20  case 'T': {.    
33163 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 49            if( nI
33164 64 3d 3d 37 20 26 26 20 73 71 6c 69 74 65 33 53  d==7 && sqlite3S
33165 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 74  trNICmp(zSql, "t
33166 72 69 67 67 65 72 22 2c 20 37 29 3d 3d 30 20 29  rigger", 7)==0 )
33167 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
33168 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 54 52 49 47    token = tkTRIG
33169 47 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20  GER;.           
3316a 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 49 64     }else if( nId
3316b 3d 3d 34 20 26 26 20 73 71 6c 69 74 65 33 53 74  ==4 && sqlite3St
3316c 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 74 65  rNICmp(zSql, "te
3316d 6d 70 22 2c 20 34 29 3d 3d 30 20 29 7b 0a 20 20  mp", 4)==0 ){.  
3316e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
3316f 6b 65 6e 20 3d 20 74 6b 54 45 4d 50 3b 0a 20 20  ken = tkTEMP;.  
33170 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73              }els
33171 65 20 69 66 28 20 6e 49 64 3d 3d 39 20 26 26 20  e if( nId==9 && 
33172 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
33173 7a 53 71 6c 2c 20 22 74 65 6d 70 6f 72 61 72 79  zSql, "temporary
33174 22 2c 20 39 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ", 9)==0 ){.    
33175 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65              toke
33176 6e 20 3d 20 74 6b 54 45 4d 50 3b 0a 20 20 20 20  n = tkTEMP;.    
33177 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
33178 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
33179 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52   token = tkOTHER
3317a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
3317b 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
3317c 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
3317d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
3317e 20 63 61 73 65 20 27 65 27 3a 20 20 63 61 73 65   case 'e':  case
3317f 20 27 45 27 3a 20 7b 0a 20 20 20 20 20 20 20 20   'E': {.        
33180 20 20 20 20 20 20 69 66 28 20 6e 49 64 3d 3d 33        if( nId==3
33181 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49   && sqlite3StrNI
33182 43 6d 70 28 7a 53 71 6c 2c 20 22 65 6e 64 22 2c  Cmp(zSql, "end",
33183 20 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20   3)==0 ){.      
33184 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
33185 3d 20 74 6b 45 4e 44 3b 0a 20 20 20 20 20 20 20  = tkEND;.       
33186 20 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 69 66         }else.#if
33187 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
33188 5f 45 58 50 4c 41 49 4e 0a 20 20 20 20 20 20 20  _EXPLAIN.       
33189 20 20 20 20 20 20 20 69 66 28 20 6e 49 64 3d 3d         if( nId==
3318a 37 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e  7 && sqlite3StrN
3318b 49 43 6d 70 28 7a 53 71 6c 2c 20 22 65 78 70 6c  ICmp(zSql, "expl
3318c 61 69 6e 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20  ain", 7)==0 ){. 
3318d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
3318e 6f 6b 65 6e 20 3d 20 74 6b 45 58 50 4c 41 49 4e  oken = tkEXPLAIN
3318f 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
33190 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20  }else.#endif.   
33191 20 20 20 20 20 20 20 20 20 20 20 7b 0a 20 20 20             {.   
33192 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b               tok
33193 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20  en = tkOTHER;.  
33194 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
33195 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
33196 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  k;.            }
33197 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 65 66  .            def
33198 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20  ault: {.        
33199 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
3319a 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20  OTHER;.         
3319b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3319c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3319d 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20      }.#endif /* 
3319e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
3319f 47 45 52 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  GER */.         
331a0 20 7a 53 71 6c 20 2b 3d 20 6e 49 64 2d 31 3b 0a   zSql += nId-1;.
331a1 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
331a2 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 72           /* Oper
331a3 61 74 6f 72 73 20 61 6e 64 20 73 70 65 63 69 61  ators and specia
331a4 6c 20 73 79 6d 62 6f 6c 73 20 2a 2f 0a 20 20 20  l symbols */.   
331a5 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
331a6 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20  kOTHER;.        
331a7 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
331a8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
331a9 20 20 20 73 74 61 74 65 20 3d 20 74 72 61 6e 73     state = trans
331aa 5b 73 74 61 74 65 5d 5b 74 6f 6b 65 6e 5d 3b 0a  [state][token];.
331ab 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 7d 0a      zSql++;.  }.
331ac 20 20 72 65 74 75 72 6e 20 73 74 61 74 65 3d 3d    return state==
331ad 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  0;.}..#ifndef SQ
331ae 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
331af 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
331b0 6e 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  ne is the same a
331b1 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f  s the sqlite3_co
331b2 6d 70 6c 65 74 65 28 29 20 72 6f 75 74 69 6e 65  mplete() routine
331b3 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 61 62   described.** ab
331b4 6f 76 65 2c 20 65 78 63 65 70 74 20 74 68 61 74  ove, except that
331b5 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69   the parameter i
331b6 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 62 65  s required to be
331b7 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 2c   UTF-16 encoded,
331b8 20 6e 6f 74 0a 2a 2a 20 55 54 46 2d 38 2e 0a 2a   not.** UTF-8..*
331b9 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
331ba 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
331bb 65 31 36 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a  e16(const void *
331bc 7a 53 71 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33  zSql){.  sqlite3
331bd 5f 76 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20  _value *pVal;.  
331be 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71 6c  char const *zSql
331bf 38 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  8;.  int rc = SQ
331c0 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 23 69 66  LITE_NOMEM;..#if
331c1 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
331c2 5f 41 55 54 4f 49 4e 49 54 0a 20 20 72 63 20 3d  _AUTOINIT.  rc =
331c3 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
331c4 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20  ize();.  if( rc 
331c5 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e  ) return rc;.#en
331c6 64 69 66 0a 20 20 70 56 61 6c 20 3d 20 73 71 6c  dif.  pVal = sql
331c7 69 74 65 33 56 61 6c 75 65 4e 65 77 28 30 29 3b  ite3ValueNew(0);
331c8 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53  .  sqlite3ValueS
331c9 65 74 53 74 72 28 70 56 61 6c 2c 20 2d 31 2c 20  etStr(pVal, -1, 
331ca 7a 53 71 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  zSql, SQLITE_UTF
331cb 31 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45  16NATIVE, SQLITE
331cc 5f 53 54 41 54 49 43 29 3b 0a 20 20 7a 53 71 6c  _STATIC);.  zSql
331cd 38 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65  8 = sqlite3Value
331ce 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54  Text(pVal, SQLIT
331cf 45 5f 55 54 46 38 29 3b 0a 20 20 69 66 28 20 7a  E_UTF8);.  if( z
331d0 53 71 6c 38 20 29 7b 0a 20 20 20 20 72 63 20 3d  Sql8 ){.    rc =
331d1 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
331d2 65 28 7a 53 71 6c 38 29 3b 0a 20 20 7d 65 6c 73  e(zSql8);.  }els
331d3 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  e{.    rc = SQLI
331d4 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
331d5 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
331d6 28 70 56 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e  (pVal);.  return
331d7 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
331d8 30 2c 20 72 63 29 3b 0a 7d 0a 23 65 6e 64 69 66  0, rc);.}.#endif
331d9 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
331da 55 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20  UTF16 */.#endif 
331db 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  /* SQLITE_OMIT_C
331dc 4f 4d 50 4c 45 54 45 20 2a 2f 0a 0a 2f 2a 2a 2a  OMPLETE */../***
331dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
331de 6f 66 20 63 6f 6d 70 6c 65 74 65 2e 63 20 2a 2a  of complete.c **
331df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
331e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
331e3 6e 20 66 69 6c 65 20 6d 61 69 6e 2e 63 20 2a 2a  n file main.c **
331e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
331e7 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
331e8 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
331e9 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
331ea 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
331eb 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
331ec 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
331ed 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
331ee 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
331ef 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
331f0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
331f1 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
331f2 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
331f3 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
331f4 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
331f5 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
331f6 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
331f7 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
331f8 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
331f9 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
331fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331fe 2a 2a 0a 2a 2a 20 4d 61 69 6e 20 66 69 6c 65 20  **.** Main file 
331ff 66 6f 72 20 74 68 65 20 53 51 4c 69 74 65 20 6c  for the SQLite l
33200 69 62 72 61 72 79 2e 20 20 54 68 65 20 72 6f 75  ibrary.  The rou
33201 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69  tines in this fi
33202 6c 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20  le.** implement 
33203 74 68 65 20 70 72 6f 67 72 61 6d 6d 65 72 20 69  the programmer i
33204 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20  nterface to the 
33205 6c 69 62 72 61 72 79 2e 20 20 52 6f 75 74 69 6e  library.  Routin
33206 65 73 20 69 6e 0a 2a 2a 20 6f 74 68 65 72 20 66  es in.** other f
33207 69 6c 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74  iles are for int
33208 65 72 6e 61 6c 20 75 73 65 20 62 79 20 53 51 4c  ernal use by SQL
33209 69 74 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e  ite and should n
3320a 6f 74 20 62 65 0a 2a 2a 20 61 63 63 65 73 73 65  ot be.** accesse
3320b 64 20 62 79 20 75 73 65 72 73 20 6f 66 20 74 68  d by users of th
3320c 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 0a 23  e library..*/..#
3320d 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
3320e 42 4c 45 5f 46 54 53 33 0a 2f 2a 2a 2a 2a 2a 2a  BLE_FTS3./******
3320f 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
33210 20 66 74 73 33 2e 68 20 69 6e 20 74 68 65 20 6d   fts3.h in the m
33211 69 64 64 6c 65 20 6f 66 20 6d 61 69 6e 2e 63 20  iddle of main.c 
33212 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33213 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
33214 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
33215 69 6c 65 20 66 74 73 33 2e 68 20 2a 2a 2a 2a 2a  ile fts3.h *****
33216 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33217 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33218 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
33219 30 30 36 20 4f 63 74 20 31 30 0a 2a 2a 0a 2a 2a  006 Oct 10.**.**
3321a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
3321b 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
3321c 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
3321d 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
3321e 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
3321f 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
33220 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
33221 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
33222 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
33223 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
33224 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
33225 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
33226 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
33227 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
33228 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
33229 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
3322a 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
3322b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3322c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3322d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3322e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3322f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
33230 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
33231 20 66 69 6c 65 20 69 73 20 75 73 65 64 20 62 79   file is used by
33232 20 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20 77   programs that w
33233 61 6e 74 20 74 6f 20 6c 69 6e 6b 20 61 67 61 69  ant to link agai
33234 6e 73 74 20 74 68 65 0a 2a 2a 20 46 54 53 33 20  nst the.** FTS3 
33235 6c 69 62 72 61 72 79 2e 20 20 41 6c 6c 20 69 74  library.  All it
33236 20 64 6f 65 73 20 69 73 20 64 65 63 6c 61 72 65   does is declare
33237 20 74 68 65 20 73 71 6c 69 74 65 33 46 74 73 33   the sqlite3Fts3
33238 49 6e 69 74 28 29 20 69 6e 74 65 72 66 61 63 65  Init() interface
33239 2e 0a 2a 2f 0a 0a 23 69 66 20 30 0a 65 78 74 65  ..*/..#if 0.exte
3323a 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 20  rn "C" {.#endif 
3323b 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20   /* __cplusplus 
3323c 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */..SQLITE_PRIVA
3323d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
3323e 73 33 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a  s3Init(sqlite3 *
3323f 64 62 29 3b 0a 0a 23 69 66 20 30 0a 7d 20 20 2f  db);..#if 0.}  /
33240 2a 20 65 78 74 65 72 6e 20 22 43 22 20 2a 2f 0a  * extern "C" */.
33241 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c  #endif  /* __cpl
33242 75 73 70 6c 75 73 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  usplus */../****
33243 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
33244 66 20 66 74 73 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a  f fts3.h *******
33245 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33246 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33247 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
33248 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
33249 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
3324a 65 66 74 20 6f 66 66 20 69 6e 20 6d 61 69 6e 2e  eft off in main.
3324b 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
3324c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69  *********/.#endi
3324d 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
3324e 45 4e 41 42 4c 45 5f 52 54 52 45 45 0a 2f 2a 2a  ENABLE_RTREE./**
3324f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
33250 6c 75 64 65 20 72 74 72 65 65 2e 68 20 69 6e 20  lude rtree.h in 
33251 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6d 61  the middle of ma
33252 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  in.c ***********
33253 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
33254 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
33255 69 6e 20 66 69 6c 65 20 72 74 72 65 65 2e 68 20  in file rtree.h 
33256 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33257 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33258 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
33259 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 36 0a 2a  ** 2008 May 26.*
3325a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
3325b 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
3325c 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
3325d 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
3325e 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
3325f 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
33260 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
33261 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
33262 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
33263 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
33264 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
33265 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
33266 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
33267 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
33268 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
33269 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
3326a 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
3326b 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
3326c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3326d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3326e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3326f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33270 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65  **.**.** This he
33271 61 64 65 72 20 66 69 6c 65 20 69 73 20 75 73 65  ader file is use
33272 64 20 62 79 20 70 72 6f 67 72 61 6d 73 20 74 68  d by programs th
33273 61 74 20 77 61 6e 74 20 74 6f 20 6c 69 6e 6b 20  at want to link 
33274 61 67 61 69 6e 73 74 20 74 68 65 0a 2a 2a 20 52  against the.** R
33275 54 52 45 45 20 6c 69 62 72 61 72 79 2e 20 20 41  TREE library.  A
33276 6c 6c 20 69 74 20 64 6f 65 73 20 69 73 20 64 65  ll it does is de
33277 63 6c 61 72 65 20 74 68 65 20 73 71 6c 69 74 65  clare the sqlite
33278 33 52 74 72 65 65 49 6e 69 74 28 29 20 69 6e 74  3RtreeInit() int
33279 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a 23 69 66 20  erface..*/..#if 
3327a 30 0a 65 78 74 65 72 6e 20 22 43 22 20 7b 0a 23  0.extern "C" {.#
3327b 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75  endif  /* __cplu
3327c 73 70 6c 75 73 20 2a 2f 0a 0a 53 51 4c 49 54 45  splus */..SQLITE
3327d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
3327e 69 74 65 33 52 74 72 65 65 49 6e 69 74 28 73 71  ite3RtreeInit(sq
3327f 6c 69 74 65 33 20 2a 64 62 29 3b 0a 0a 23 69 66  lite3 *db);..#if
33280 20 30 0a 7d 20 20 2f 2a 20 65 78 74 65 72 6e 20   0.}  /* extern 
33281 22 43 22 20 2a 2f 0a 23 65 6e 64 69 66 20 20 2f  "C" */.#endif  /
33282 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f  * __cplusplus */
33283 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
33284 2a 20 45 6e 64 20 6f 66 20 72 74 72 65 65 2e 68  * End of rtree.h
33285 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
33286 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33287 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33288 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
33289 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
3328a 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
3328b 6e 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a  n main.c *******
3328c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3328d 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  /.#endif.#ifdef 
3328e 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43  SQLITE_ENABLE_IC
3328f 55 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  U./*************
33290 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65  * Include sqlite
33291 69 63 75 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  icu.h in the mid
33292 64 6c 65 20 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a  dle of main.c **
33293 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33294 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
33295 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c  * Begin file sql
33296 69 74 65 69 63 75 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteicu.h *******
33297 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33298 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33299 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79  /./*.** 2008 May
3329a 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   26.**.** The au
3329b 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
3329c 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
3329d 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
3329e 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
3329f 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
332a0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
332a1 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
332a2 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
332a3 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
332a4 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
332a5 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
332a6 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
332a7 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
332a8 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
332a9 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
332aa 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
332ab 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
332ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
332ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
332ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
332af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
332b0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
332b1 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 69  is header file i
332b2 73 20 75 73 65 64 20 62 79 20 70 72 6f 67 72 61  s used by progra
332b3 6d 73 20 74 68 61 74 20 77 61 6e 74 20 74 6f 20  ms that want to 
332b4 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 65  link against the
332b5 0a 2a 2a 20 49 43 55 20 65 78 74 65 6e 73 69 6f  .** ICU extensio
332b6 6e 2e 20 20 41 6c 6c 20 69 74 20 64 6f 65 73 20  n.  All it does 
332b7 69 73 20 64 65 63 6c 61 72 65 20 74 68 65 20 73  is declare the s
332b8 71 6c 69 74 65 33 49 63 75 49 6e 69 74 28 29 20  qlite3IcuInit() 
332b9 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a 23  interface..*/..#
332ba 69 66 20 30 0a 65 78 74 65 72 6e 20 22 43 22 20  if 0.extern "C" 
332bb 7b 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63  {.#endif  /* __c
332bc 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 53 51 4c  plusplus */..SQL
332bd 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
332be 73 71 6c 69 74 65 33 49 63 75 49 6e 69 74 28 73  sqlite3IcuInit(s
332bf 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 0a 23 69  qlite3 *db);..#i
332c0 66 20 30 0a 7d 20 20 2f 2a 20 65 78 74 65 72 6e  f 0.}  /* extern
332c1 20 22 43 22 20 2a 2f 0a 23 65 6e 64 69 66 20 20   "C" */.#endif  
332c2 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a  /* __cplusplus *
332c3 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /.../***********
332c4 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74  *** End of sqlit
332c5 65 69 63 75 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eicu.h *********
332c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
332c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
332c8 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
332c9 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
332ca 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
332cb 20 69 6e 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a   in main.c *****
332cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
332cd 2a 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  **/.#endif../*.*
332ce 2a 20 54 68 65 20 76 65 72 73 69 6f 6e 20 6f 66  * The version of
332cf 20 74 68 65 20 6c 69 62 72 61 72 79 0a 2a 2f 0a   the library.*/.
332d0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41  #ifndef SQLITE_A
332d1 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49  MALGAMATION.SQLI
332d2 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61  TE_API const cha
332d3 72 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f  r sqlite3_versio
332d4 6e 5b 5d 20 3d 20 53 51 4c 49 54 45 5f 56 45 52  n[] = SQLITE_VER
332d5 53 49 4f 4e 3b 0a 23 65 6e 64 69 66 0a 53 51 4c  SION;.#endif.SQL
332d6 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
332d7 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c 69 62 76  ar *sqlite3_libv
332d8 65 72 73 69 6f 6e 28 76 6f 69 64 29 7b 20 72 65  ersion(void){ re
332d9 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 76 65 72  turn sqlite3_ver
332da 73 69 6f 6e 3b 20 7d 0a 53 51 4c 49 54 45 5f 41  sion; }.SQLITE_A
332db 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
332dc 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64 28  qlite3_sourceid(
332dd 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51  void){ return SQ
332de 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 3b 20  LITE_SOURCE_ID; 
332df 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
332e0 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73   sqlite3_libvers
332e1 69 6f 6e 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29  ion_number(void)
332e2 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  { return SQLITE_
332e3 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 3b 20  VERSION_NUMBER; 
332e4 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
332e5 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 73   sqlite3_threads
332e6 61 66 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72  afe(void){ retur
332e7 6e 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  n SQLITE_THREADS
332e8 41 46 45 3b 20 7d 0a 0a 23 69 66 20 21 64 65 66  AFE; }..#if !def
332e9 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
332ea 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 69 6e  _TRACE) && defin
332eb 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
332ec 5f 49 4f 54 52 41 43 45 29 0a 2f 2a 0a 2a 2a 20  _IOTRACE)./*.** 
332ed 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  If the following
332ee 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65   function pointe
332ef 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e  r is not NULL an
332f0 64 20 69 66 0a 2a 2a 20 53 51 4c 49 54 45 5f 45  d if.** SQLITE_E
332f1 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20 69 73  NABLE_IOTRACE is
332f2 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20 6d   enabled, then m
332f3 65 73 73 61 67 65 73 20 64 65 73 63 72 69 62 69  essages describi
332f4 6e 67 0a 2a 2a 20 49 2f 4f 20 61 63 74 69 76 65  ng.** I/O active
332f5 20 61 72 65 20 77 72 69 74 74 65 6e 20 75 73 69   are written usi
332f6 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ng this function
332f7 2e 20 20 54 68 65 73 65 20 6d 65 73 73 61 67 65  .  These message
332f8 73 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e 64 65  s.** are intende
332f9 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20  d for debugging 
332fa 61 63 74 69 76 69 74 79 20 6f 6e 6c 79 2e 0a 2a  activity only..*
332fb 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
332fc 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 49   void (*sqlite3I
332fd 6f 54 72 61 63 65 29 28 63 6f 6e 73 74 20 63 68  oTrace)(const ch
332fe 61 72 2a 2c 20 2e 2e 2e 29 20 3d 20 30 3b 0a 23  ar*, ...) = 0;.#
332ff 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  endif../*.** If 
33300 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c  the following gl
33301 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 70 6f  obal variable po
33302 69 6e 74 73 20 74 6f 20 61 20 73 74 72 69 6e 67  ints to a string
33303 20 77 68 69 63 68 20 69 73 20 74 68 65 0a 2a 2a   which is the.**
33304 20 6e 61 6d 65 20 6f 66 20 61 20 64 69 72 65 63   name of a direc
33305 74 6f 72 79 2c 20 74 68 65 6e 20 74 68 61 74 20  tory, then that 
33306 64 69 72 65 63 74 6f 72 79 20 77 69 6c 6c 20 62  directory will b
33307 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a  e used to store.
33308 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  ** temporary fil
33309 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  es..**.** See al
3330a 73 6f 20 74 68 65 20 22 50 52 41 47 4d 41 20 74  so the "PRAGMA t
3330b 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74  emp_store_direct
3330c 6f 72 79 22 20 53 51 4c 20 63 6f 6d 6d 61 6e 64  ory" SQL command
3330d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3330e 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 74 65  char *sqlite3_te
3330f 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 3d 20 30  mp_directory = 0
33310 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  ;../*.** Initial
33311 69 7a 65 20 53 51 4c 69 74 65 2e 20 20 0a 2a 2a  ize SQLite.  .**
33312 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
33313 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20   must be called 
33314 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68  to initialize th
33315 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
33316 69 6f 6e 2c 0a 2a 2a 20 56 46 53 2c 20 61 6e 64  ion,.** VFS, and
33317 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d   mutex subsystem
33318 73 20 70 72 69 6f 72 20 74 6f 20 64 6f 69 6e 67  s prior to doing
33319 20 61 6e 79 20 73 65 72 69 6f 75 73 20 77 6f 72   any serious wor
3331a 6b 20 77 69 74 68 0a 2a 2a 20 53 51 4c 69 74 65  k with.** SQLite
3331b 2e 20 20 42 75 74 20 61 73 20 6c 6f 6e 67 20 61  .  But as long a
3331c 73 20 79 6f 75 20 64 6f 20 6e 6f 74 20 63 6f 6d  s you do not com
3331d 70 69 6c 65 20 77 69 74 68 20 53 51 4c 49 54 45  pile with SQLITE
3331e 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 2a  _OMIT_AUTOINIT.*
3331f 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * this routine w
33320 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 61 75  ill be called au
33321 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 20 6b  tomatically by k
33322 65 79 20 72 6f 75 74 69 6e 65 73 20 73 75 63 68  ey routines such
33323 20 61 73 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f   as.** sqlite3_o
33324 70 65 6e 28 29 2e 20 20 0a 2a 2a 0a 2a 2a 20 54  pen().  .**.** T
33325 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
33326 20 6e 6f 2d 6f 70 20 65 78 63 65 70 74 20 6f 6e   no-op except on
33327 20 69 74 73 20 76 65 72 79 20 66 69 72 73 74 20   its very first 
33328 63 61 6c 6c 20 66 6f 72 20 74 68 65 20 70 72 6f  call for the pro
33329 63 65 73 73 2c 0a 2a 2a 20 6f 72 20 66 6f 72 20  cess,.** or for 
3332a 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 61  the first call a
3332b 66 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 73  fter a call to s
3332c 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 2e  qlite3_shutdown.
3332d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
3332e 20 74 68 72 65 61 64 20 74 6f 20 63 61 6c 6c 20   thread to call 
3332f 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e  this routine run
33330 73 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a 61  s the initializa
33331 74 69 6f 6e 20 74 6f 0a 2a 2a 20 63 6f 6d 70 6c  tion to.** compl
33332 65 74 69 6f 6e 2e 20 20 49 66 20 73 75 62 73 65  etion.  If subse
33333 71 75 65 6e 74 20 74 68 72 65 61 64 73 20 63 61  quent threads ca
33334 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ll this routine 
33335 62 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74  before the first
33336 0a 2a 2a 20 74 68 72 65 61 64 20 68 61 73 20 66  .** thread has f
33337 69 6e 69 73 68 65 64 20 74 68 65 20 69 6e 69 74  inished the init
33338 69 61 6c 69 7a 61 74 69 6f 6e 20 70 72 6f 63 65  ialization proce
33339 73 73 2c 20 74 68 65 6e 20 74 68 65 20 73 75 62  ss, then the sub
3333a 73 65 71 75 65 6e 74 0a 2a 2a 20 74 68 72 65 61  sequent.** threa
3333b 64 73 20 6d 75 73 74 20 62 6c 6f 63 6b 20 75 6e  ds must block un
3333c 74 69 6c 20 74 68 65 20 66 69 72 73 74 20 74 68  til the first th
3333d 72 65 61 64 20 66 69 6e 69 73 68 65 73 20 77 69  read finishes wi
3333e 74 68 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a  th the initializ
3333f 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ation..**.** The
33340 20 66 69 72 73 74 20 74 68 72 65 61 64 20 6d 69   first thread mi
33341 67 68 74 20 63 61 6c 6c 20 74 68 69 73 20 72 6f  ght call this ro
33342 75 74 69 6e 65 20 72 65 63 75 72 73 69 76 65 6c  utine recursivel
33343 79 2e 20 20 52 65 63 75 72 73 69 76 65 0a 2a 2a  y.  Recursive.**
33344 20 63 61 6c 6c 73 20 74 6f 20 74 68 69 73 20 72   calls to this r
33345 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6e 6f  outine should no
33346 74 20 62 6c 6f 63 6b 2c 20 6f 66 20 63 6f 75 72  t block, of cour
33347 73 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20 74  se.  Otherwise t
33348 68 65 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 61  he.** initializa
33349 74 69 6f 6e 20 70 72 6f 63 65 73 73 20 77 6f 75  tion process wou
3334a 6c 64 20 6e 65 76 65 72 20 63 6f 6d 70 6c 65 74  ld never complet
3334b 65 2e 0a 2a 2a 0a 2a 2a 20 4c 65 74 20 58 20 62  e..**.** Let X b
3334c 65 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65  e the first thre
3334d 61 64 20 74 6f 20 65 6e 74 65 72 20 74 68 69 73  ad to enter this
3334e 20 72 6f 75 74 69 6e 65 2e 20 20 4c 65 74 20 59   routine.  Let Y
3334f 20 62 65 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a   be some other.*
33350 2a 20 74 68 72 65 61 64 2e 20 20 54 68 65 6e 20  * thread.  Then 
33351 77 68 69 6c 65 20 74 68 65 20 69 6e 69 74 69 61  while the initia
33352 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20  l invocation of 
33353 74 68 69 73 20 72 6f 75 74 69 6e 65 20 62 79 20  this routine by 
33354 58 20 69 73 0a 2a 2a 20 69 6e 63 6f 6d 70 6c 65  X is.** incomple
33355 74 65 2c 20 69 74 20 69 73 20 72 65 71 75 69 72  te, it is requir
33356 65 64 20 74 68 61 74 3a 0a 2a 2a 0a 2a 2a 20 20  ed that:.**.**  
33357 20 20 2a 20 20 43 61 6c 6c 73 20 74 6f 20 74 68    *  Calls to th
33358 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20  is routine from 
33359 59 20 6d 75 73 74 20 62 6c 6f 63 6b 20 75 6e 74  Y must block unt
3335a 69 6c 20 74 68 65 20 6f 75 74 65 72 2d 6d 6f 73  il the outer-mos
3335b 74 0a 2a 2a 20 20 20 20 20 20 20 63 61 6c 6c 20  t.**       call 
3335c 62 79 20 58 20 63 6f 6d 70 6c 65 74 65 73 2e 0a  by X completes..
3335d 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 52 65 63 75  **.**    *  Recu
3335e 72 73 69 76 65 20 63 61 6c 6c 73 20 74 6f 20 74  rsive calls to t
3335f 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d  his routine from
33360 20 74 68 72 65 61 64 20 58 20 72 65 74 75 72 6e   thread X return
33361 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20   immediately.** 
33362 20 20 20 20 20 20 77 69 74 68 6f 75 74 20 62 6c        without bl
33363 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54  ocking..*/.SQLIT
33364 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
33365 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69  3_initialize(voi
33366 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  d){.  sqlite3_mu
33367 74 65 78 20 2a 70 4d 61 73 74 65 72 3b 20 20 20  tex *pMaster;   
33368 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33369 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 73     /* The main s
3336a 74 61 74 69 63 20 6d 75 74 65 78 20 2a 2f 0a 20  tatic mutex */. 
3336b 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
3336c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3336d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3336e 20 52 65 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0a   Result code */.
3336f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
33370 4d 49 54 5f 57 53 44 0a 20 20 72 63 20 3d 20 73  MIT_WSD.  rc = s
33371 71 6c 69 74 65 33 5f 77 73 64 5f 69 6e 69 74 28  qlite3_wsd_init(
33372 34 30 39 36 2c 20 32 34 29 3b 0a 20 20 69 66 28  4096, 24);.  if(
33373 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
33374 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
33375 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
33376 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20 61  * If SQLite is a
33377 6c 72 65 61 64 79 20 63 6f 6d 70 6c 65 74 65 6c  lready completel
33378 79 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74  y initialized, t
33379 68 65 6e 20 74 68 69 73 20 63 61 6c 6c 0a 20 20  hen this call.  
3337a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e  ** to sqlite3_in
3337b 69 74 69 61 6c 69 7a 65 28 29 20 73 68 6f 75 6c  itialize() shoul
3337c 64 20 62 65 20 61 20 6e 6f 2d 6f 70 2e 20 20 42  d be a no-op.  B
3337d 75 74 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a  ut the initializ
3337e 61 74 69 6f 6e 0a 20 20 2a 2a 20 6d 75 73 74 20  ation.  ** must 
3337f 62 65 20 63 6f 6d 70 6c 65 74 65 2e 20 20 53 6f  be complete.  So
33380 20 69 73 49 6e 69 74 20 6d 75 73 74 20 6e 6f 74   isInit must not
33381 20 62 65 20 73 65 74 20 75 6e 74 69 6c 20 74 68   be set until th
33382 65 20 76 65 72 79 20 65 6e 64 0a 20 20 2a 2a 20  e very end.  ** 
33383 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  of this routine.
33384 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
33385 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
33386 69 73 49 6e 69 74 20 29 20 72 65 74 75 72 6e 20  isInit ) return 
33387 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a  SQLITE_OK;..  /*
33388 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6d   Make sure the m
33389 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 20 69  utex subsystem i
3338a 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20  s initialized.  
3338b 49 66 20 75 6e 61 62 6c 65 20 74 6f 20 0a 20 20  If unable to .  
3338c 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68  ** initialize th
3338d 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65  e mutex subsyste
3338e 6d 2c 20 72 65 74 75 72 6e 20 65 61 72 6c 79 20  m, return early 
3338f 77 69 74 68 20 74 68 65 20 65 72 72 6f 72 2e 0a  with the error..
33390 20 20 2a 2a 20 49 66 20 74 68 65 20 73 79 73 74    ** If the syst
33391 65 6d 20 69 73 20 73 6f 20 73 69 63 6b 20 74 68  em is so sick th
33392 61 74 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65  at we are unable
33393 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6d   to allocate a m
33394 75 74 65 78 2c 0a 20 20 2a 2a 20 74 68 65 72 65  utex,.  ** there
33395 20 69 73 20 6e 6f 74 20 6d 75 63 68 20 53 51 4c   is not much SQL
33396 69 74 65 20 69 73 20 67 6f 69 6e 67 20 74 6f 20  ite is going to 
33397 62 65 20 61 62 6c 65 20 74 6f 20 64 6f 2e 0a 20  be able to do.. 
33398 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 6d 75 74   **.  ** The mut
33399 65 78 20 73 75 62 73 79 73 74 65 6d 20 6d 75 73  ex subsystem mus
3339a 74 20 74 61 6b 65 20 63 61 72 65 20 6f 66 20 73  t take care of s
3339b 65 72 69 61 6c 69 7a 69 6e 67 20 69 74 73 20 6f  erializing its o
3339c 77 6e 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69  wn.  ** initiali
3339d 7a 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 72  zation..  */.  r
3339e 63 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78  c = sqlite3Mutex
3339f 49 6e 69 74 28 29 3b 0a 20 20 69 66 28 20 72 63  Init();.  if( rc
333a0 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
333a1 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
333a2 68 65 20 6d 61 6c 6c 6f 63 28 29 20 73 79 73 74  he malloc() syst
333a3 65 6d 20 61 6e 64 20 74 68 65 20 72 65 63 75 72  em and the recur
333a4 73 69 76 65 20 70 49 6e 69 74 4d 75 74 65 78 20  sive pInitMutex 
333a5 6d 75 74 65 78 2e 0a 20 20 2a 2a 20 54 68 69 73  mutex..  ** This
333a6 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 70 72   operation is pr
333a7 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20 53  otected by the S
333a8 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74  TATIC_MASTER mut
333a9 65 78 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 20  ex.  Note that. 
333aa 20 2a 2a 20 4d 75 74 65 78 41 6c 6c 6f 63 28 29   ** MutexAlloc()
333ab 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 61   is called for a
333ac 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 70 72   static mutex pr
333ad 69 6f 72 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a  ior to initializ
333ae 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 6d 61 6c  ing the.  ** mal
333af 6c 6f 63 20 73 75 62 73 79 73 74 65 6d 20 2d 20  loc subsystem - 
333b0 74 68 69 73 20 69 6d 70 6c 69 65 73 20 74 68 61  this implies tha
333b1 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  t the allocation
333b2 20 6f 66 20 61 20 73 74 61 74 69 63 0a 20 20 2a   of a static.  *
333b3 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 6e 6f 74  * mutex must not
333b4 20 72 65 71 75 69 72 65 20 73 75 70 70 6f 72 74   require support
333b5 20 66 72 6f 6d 20 74 68 65 20 6d 61 6c 6c 6f 63   from the malloc
333b6 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a 2f   subsystem..  */
333b7 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 73 71 6c  .  pMaster = sql
333b8 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
333b9 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
333ba 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 71  IC_MASTER);.  sq
333bb 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
333bc 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 73 71  r(pMaster);.  sq
333bd 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
333be 67 2e 69 73 4d 75 74 65 78 49 6e 69 74 20 3d 20  g.isMutexInit = 
333bf 31 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65  1;.  if( !sqlite
333c0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73  3GlobalConfig.is
333c1 4d 61 6c 6c 6f 63 49 6e 69 74 20 29 7b 0a 20 20  MallocInit ){.  
333c2 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4d 61    rc = sqlite3Ma
333c3 6c 6c 6f 63 49 6e 69 74 28 29 3b 0a 20 20 7d 0a  llocInit();.  }.
333c4 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
333c5 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
333c6 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69  e3GlobalConfig.i
333c7 73 4d 61 6c 6c 6f 63 49 6e 69 74 20 3d 20 31 3b  sMallocInit = 1;
333c8 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65  .    if( !sqlite
333c9 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49  3GlobalConfig.pI
333ca 6e 69 74 4d 75 74 65 78 20 29 7b 0a 20 20 20 20  nitMutex ){.    
333cb 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
333cc 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78  onfig.pInitMutex
333cd 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 73 71   =.           sq
333ce 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
333cf 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
333d0 55 52 53 49 56 45 29 3b 0a 20 20 20 20 20 20 69  URSIVE);.      i
333d1 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
333d2 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65  Config.bCoreMute
333d3 78 20 26 26 20 21 73 71 6c 69 74 65 33 47 6c 6f  x && !sqlite3Glo
333d4 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d  balConfig.pInitM
333d5 75 74 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20  utex ){.        
333d6 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
333d7 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  M;.      }.    }
333d8 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
333d9 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
333da 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
333db 66 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75 74 65  fig.nRefInitMute
333dc 78 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  x++;.  }.  sqlit
333dd 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
333de 4d 61 73 74 65 72 29 3b 0a 0a 20 20 2f 2a 20 49  Master);..  /* I
333df 66 20 72 63 20 69 73 20 6e 6f 74 20 53 51 4c 49  f rc is not SQLI
333e0 54 45 5f 4f 4b 20 61 74 20 74 68 69 73 20 70 6f  TE_OK at this po
333e1 69 6e 74 2c 20 74 68 65 6e 20 65 69 74 68 65 72  int, then either
333e2 20 74 68 65 20 6d 61 6c 6c 6f 63 0a 20 20 2a 2a   the malloc.  **
333e3 20 73 75 62 73 79 73 74 65 6d 20 63 6f 75 6c 64   subsystem could
333e4 20 6e 6f 74 20 62 65 20 69 6e 69 74 69 61 6c 69   not be initiali
333e5 7a 65 64 20 6f 72 20 74 68 65 20 73 79 73 74 65  zed or the syste
333e6 6d 20 66 61 69 6c 65 64 20 74 6f 20 61 6c 6c 6f  m failed to allo
333e7 63 61 74 65 0a 20 20 2a 2a 20 74 68 65 20 70 49  cate.  ** the pI
333e8 6e 69 74 4d 75 74 65 78 20 6d 75 74 65 78 2e 20  nitMutex mutex. 
333e9 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  Return an error 
333ea 69 6e 20 65 69 74 68 65 72 20 63 61 73 65 2e 20  in either case. 
333eb 20 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51   */.  if( rc!=SQ
333ec 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
333ed 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
333ee 20 2f 2a 20 44 6f 20 74 68 65 20 72 65 73 74 20   /* Do the rest 
333ef 6f 66 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a  of the initializ
333f0 61 74 69 6f 6e 20 75 6e 64 65 72 20 74 68 65 20  ation under the 
333f1 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20  recursive mutex 
333f2 73 6f 0a 20 20 2a 2a 20 74 68 61 74 20 77 65 20  so.  ** that we 
333f3 77 69 6c 6c 20 62 65 20 61 62 6c 65 20 74 6f 20  will be able to 
333f4 68 61 6e 64 6c 65 20 72 65 63 75 72 73 69 76 65  handle recursive
333f5 20 63 61 6c 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a   calls into.  **
333f6 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
333f7 69 7a 65 28 29 2e 20 20 54 68 65 20 72 65 63 75  ize().  The recu
333f8 72 73 69 76 65 20 63 61 6c 6c 73 20 6e 6f 72 6d  rsive calls norm
333f9 61 6c 6c 79 20 63 6f 6d 65 20 74 68 72 6f 75 67  ally come throug
333fa 68 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6f  h.  ** sqlite3_o
333fb 73 5f 69 6e 69 74 28 29 20 77 68 65 6e 20 69 74  s_init() when it
333fc 20 69 6e 76 6f 6b 65 73 20 73 71 6c 69 74 65 33   invokes sqlite3
333fd 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 29 2c  _vfs_register(),
333fe 20 62 75 74 20 6f 74 68 65 72 0a 20 20 2a 2a 20   but other.  ** 
333ff 72 65 63 75 72 73 69 76 65 20 63 61 6c 6c 73 20  recursive calls 
33400 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20 70 6f  might also be po
33401 73 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73  ssible..  */.  s
33402 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
33403 65 72 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  er(sqlite3Global
33404 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65  Config.pInitMute
33405 78 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  x);.  if( sqlite
33406 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73  3GlobalConfig.is
33407 49 6e 69 74 3d 3d 30 20 26 26 20 73 71 6c 69 74  Init==0 && sqlit
33408 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69  e3GlobalConfig.i
33409 6e 50 72 6f 67 72 65 73 73 3d 3d 30 20 29 7b 0a  nProgress==0 ){.
3340a 20 20 20 20 46 75 6e 63 44 65 66 48 61 73 68 20      FuncDefHash 
3340b 2a 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c  *pHash = &GLOBAL
3340c 28 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71  (FuncDefHash, sq
3340d 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74  lite3GlobalFunct
3340e 69 6f 6e 73 29 3b 0a 20 20 20 20 73 71 6c 69 74  ions);.    sqlit
3340f 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69  e3GlobalConfig.i
33410 6e 50 72 6f 67 72 65 73 73 20 3d 20 31 3b 0a 20  nProgress = 1;. 
33411 20 20 20 6d 65 6d 73 65 74 28 70 48 61 73 68 2c     memset(pHash,
33412 20 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74   0, sizeof(sqlit
33413 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e  e3GlobalFunction
33414 73 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  s));.    sqlite3
33415 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 75  RegisterGlobalFu
33416 6e 63 74 69 6f 6e 73 28 29 3b 0a 20 20 20 20 69  nctions();.    i
33417 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
33418 43 6f 6e 66 69 67 2e 69 73 50 43 61 63 68 65 49  Config.isPCacheI
33419 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  nit==0 ){.      
3341a 72 63 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63  rc = sqlite3Pcac
3341b 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a  heInitialize();.
3341c 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
3341d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3341e 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
3341f 61 6c 43 6f 6e 66 69 67 2e 69 73 50 43 61 63 68  alConfig.isPCach
33420 65 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 20  eInit = 1;.     
33421 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 49   rc = sqlite3OsI
33422 6e 69 74 28 29 3b 0a 20 20 20 20 7d 0a 20 20 20  nit();.    }.   
33423 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
33424 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  OK ){.      sqli
33425 74 65 33 50 43 61 63 68 65 42 75 66 66 65 72 53  te3PCacheBufferS
33426 65 74 75 70 28 20 73 71 6c 69 74 65 33 47 6c 6f  etup( sqlite3Glo
33427 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 2c  balConfig.pPage,
33428 20 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69   .          sqli
33429 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
3342a 73 7a 50 61 67 65 2c 20 73 71 6c 69 74 65 33 47  szPage, sqlite3G
3342b 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67  lobalConfig.nPag
3342c 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
3342d 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73  3GlobalConfig.is
3342e 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  Init = 1;.    }.
3342f 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
33430 6c 43 6f 6e 66 69 67 2e 69 6e 50 72 6f 67 72 65  lConfig.inProgre
33431 73 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71  ss = 0;.  }.  sq
33432 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
33433 65 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  e(sqlite3GlobalC
33434 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78  onfig.pInitMutex
33435 29 3b 0a 0a 20 20 2f 2a 20 47 6f 20 62 61 63 6b  );..  /* Go back
33436 20 75 6e 64 65 72 20 74 68 65 20 73 74 61 74 69   under the stati
33437 63 20 6d 75 74 65 78 20 61 6e 64 20 63 6c 65 61  c mutex and clea
33438 6e 20 75 70 20 74 68 65 20 72 65 63 75 72 73 69  n up the recursi
33439 76 65 0a 20 20 2a 2a 20 6d 75 74 65 78 20 74 6f  ve.  ** mutex to
3343a 20 70 72 65 76 65 6e 74 20 61 20 72 65 73 6f 75   prevent a resou
3343b 72 63 65 20 6c 65 61 6b 2e 0a 20 20 2a 2f 0a 20  rce leak..  */. 
3343c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
3343d 6e 74 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20  nter(pMaster);. 
3343e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3343f 6e 66 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75 74  nfig.nRefInitMut
33440 65 78 2d 2d 3b 0a 20 20 69 66 28 20 73 71 6c 69  ex--;.  if( sqli
33441 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
33442 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 3c 3d 30  nRefInitMutex<=0
33443 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
33444 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
33445 66 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75 74 65  fig.nRefInitMute
33446 78 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69  x==0 );.    sqli
33447 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73  te3_mutex_free(s
33448 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
33449 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 29 3b 0a  ig.pInitMutex);.
3344a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
3344b 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74  lConfig.pInitMut
3344c 65 78 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71  ex = 0;.  }.  sq
3344d 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
3344e 65 28 70 4d 61 73 74 65 72 29 3b 0a 0a 20 20 2f  e(pMaster);..  /
3344f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
33450 69 73 20 6a 75 73 74 20 61 20 73 61 6e 69 74 79  is just a sanity
33451 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73   check to make s
33452 75 72 65 20 53 51 4c 69 74 65 20 68 61 73 0a 20  ure SQLite has. 
33453 20 2a 2a 20 62 65 65 6e 20 63 6f 6d 70 69 6c 65   ** been compile
33454 64 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 49 74  d correctly.  It
33455 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f   is important to
33456 20 72 75 6e 20 74 68 69 73 20 63 6f 64 65 2c 20   run this code, 
33457 62 75 74 0a 20 20 2a 2a 20 77 65 20 64 6f 6e 27  but.  ** we don'
33458 74 20 77 61 6e 74 20 74 6f 20 72 75 6e 20 69 74  t want to run it
33459 20 74 6f 6f 20 6f 66 74 65 6e 20 61 6e 64 20 73   too often and s
3345a 6f 61 6b 20 75 70 20 43 50 55 20 63 79 63 6c 65  oak up CPU cycle
3345b 73 20 66 6f 72 20 6e 6f 0a 20 20 2a 2a 20 72 65  s for no.  ** re
3345c 61 73 6f 6e 2e 20 20 53 6f 20 77 65 20 72 75 6e  ason.  So we run
3345d 20 69 74 20 6f 6e 63 65 20 64 75 72 69 6e 67 20   it once during 
3345e 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a  initialization..
3345f 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45    */.#ifndef NDE
33460 42 55 47 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  BUG.#ifndef SQLI
33461 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
33462 5f 50 4f 49 4e 54 0a 20 20 2f 2a 20 54 68 69 73  _POINT.  /* This
33463 20 73 65 63 74 69 6f 6e 20 6f 66 20 63 6f 64 65   section of code
33464 27 73 20 6f 6e 6c 79 20 22 6f 75 74 70 75 74 22  's only "output"
33465 20 69 73 20 76 69 61 20 61 73 73 65 72 74 28 29   is via assert()
33466 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 2a 2f 0a   statements. */.
33467 20 20 69 66 20 28 20 72 63 3d 3d 53 51 4c 49 54    if ( rc==SQLIT
33468 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 36 34 20  E_OK ){.    u64 
33469 78 20 3d 20 28 28 28 75 36 34 29 31 29 3c 3c 36  x = (((u64)1)<<6
3346a 33 29 2d 31 3b 0a 20 20 20 20 64 6f 75 62 6c 65  3)-1;.    double
3346b 20 79 3b 0a 20 20 20 20 61 73 73 65 72 74 28 73   y;.    assert(s
3346c 69 7a 65 6f 66 28 78 29 3d 3d 38 29 3b 0a 20 20  izeof(x)==8);.  
3346d 20 20 61 73 73 65 72 74 28 73 69 7a 65 6f 66 28    assert(sizeof(
3346e 78 29 3d 3d 73 69 7a 65 6f 66 28 79 29 29 3b 0a  x)==sizeof(y));.
3346f 20 20 20 20 6d 65 6d 63 70 79 28 26 79 2c 20 26      memcpy(&y, &
33470 78 2c 20 38 29 3b 0a 20 20 20 20 61 73 73 65 72  x, 8);.    asser
33471 74 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28  t( sqlite3IsNaN(
33472 79 29 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  y) );.  }.#endif
33473 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72  .#endif..  retur
33474 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55  n rc;.}../*.** U
33475 6e 64 6f 20 74 68 65 20 65 66 66 65 63 74 73 20  ndo the effects 
33476 6f 66 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  of sqlite3_initi
33477 61 6c 69 7a 65 28 29 2e 20 20 4d 75 73 74 20 6e  alize().  Must n
33478 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 77 68 69  ot be called whi
33479 6c 65 0a 2a 2a 20 74 68 65 72 65 20 61 72 65 20  le.** there are 
3347a 6f 75 74 73 74 61 6e 64 69 6e 67 20 64 61 74 61  outstanding data
3347b 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
3347c 20 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63   or memory alloc
3347d 61 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 77 68 69  ations or.** whi
3347e 6c 65 20 61 6e 79 20 70 61 72 74 20 6f 66 20 53  le any part of S
3347f 51 4c 69 74 65 20 69 73 20 6f 74 68 65 72 77 69  QLite is otherwi
33480 73 65 20 69 6e 20 75 73 65 20 69 6e 20 61 6e 79  se in use in any
33481 20 74 68 72 65 61 64 2e 20 20 54 68 69 73 0a 2a   thread.  This.*
33482 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74  * routine is not
33483 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 42 75   threadsafe.  Bu
33484 74 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20  t it is safe to 
33485 69 6e 76 6f 6b 65 20 74 68 69 73 20 72 6f 75 74  invoke this rout
33486 69 6e 65 0a 2a 2a 20 6f 6e 20 77 68 65 6e 20 53  ine.** on when S
33487 51 4c 69 74 65 20 69 73 20 61 6c 72 65 61 64 79  QLite is already
33488 20 73 68 75 74 20 64 6f 77 6e 2e 20 20 49 66 20   shut down.  If 
33489 53 51 4c 69 74 65 20 69 73 20 61 6c 72 65 61 64  SQLite is alread
3348a 79 20 73 68 75 74 20 64 6f 77 6e 0a 2a 2a 20 77  y shut down.** w
3348b 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
3348c 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 74 68 65   is invoked, the
3348d 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
3348e 73 20 61 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d  s a harmless no-
3348f 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  op..*/.SQLITE_AP
33490 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 68  I int sqlite3_sh
33491 75 74 64 6f 77 6e 28 76 6f 69 64 29 7b 0a 20 20  utdown(void){.  
33492 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
33493 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20 29  lConfig.isInit )
33494 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6f 73  {.    sqlite3_os
33495 5f 65 6e 64 28 29 3b 0a 20 20 20 20 73 71 6c 69  _end();.    sqli
33496 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65  te3_reset_auto_e
33497 78 74 65 6e 73 69 6f 6e 28 29 3b 0a 20 20 20 20  xtension();.    
33498 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
33499 66 69 67 2e 69 73 49 6e 69 74 20 3d 20 30 3b 0a  fig.isInit = 0;.
3349a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65    }.  if( sqlite
3349b 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73  3GlobalConfig.is
3349c 50 43 61 63 68 65 49 6e 69 74 20 29 7b 0a 20 20  PCacheInit ){.  
3349d 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53    sqlite3PcacheS
3349e 68 75 74 64 6f 77 6e 28 29 3b 0a 20 20 20 20 73  hutdown();.    s
3349f 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
334a0 69 67 2e 69 73 50 43 61 63 68 65 49 6e 69 74 20  ig.isPCacheInit 
334a1 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  = 0;.  }.  if( s
334a2 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
334a3 69 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20  ig.isMallocInit 
334a4 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61  ){.    sqlite3Ma
334a5 6c 6c 6f 63 45 6e 64 28 29 3b 0a 20 20 20 20 73  llocEnd();.    s
334a6 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
334a7 69 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20  ig.isMallocInit 
334a8 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  = 0;.  }.  if( s
334a9 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
334aa 69 67 2e 69 73 4d 75 74 65 78 49 6e 69 74 20 29  ig.isMutexInit )
334ab 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 75 74  {.    sqlite3Mut
334ac 65 78 45 6e 64 28 29 3b 0a 20 20 20 20 73 71 6c  exEnd();.    sql
334ad 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
334ae 2e 69 73 4d 75 74 65 78 49 6e 69 74 20 3d 20 30  .isMutexInit = 0
334af 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
334b0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
334b1 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 61 6c 6c  .** This API all
334b2 6f 77 73 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  ows applications
334b3 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 67   to modify the g
334b4 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72 61 74  lobal configurat
334b5 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 53 51  ion of.** the SQ
334b6 4c 69 74 65 20 6c 69 62 72 61 72 79 20 61 74 20  Lite library at 
334b7 72 75 6e 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20  run-time..**.** 
334b8 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f  This routine sho
334b9 75 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c  uld only be call
334ba 65 64 20 77 68 65 6e 20 74 68 65 72 65 20 61 72  ed when there ar
334bb 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67  e no outstanding
334bc 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e  .** database con
334bd 6e 65 63 74 69 6f 6e 73 20 6f 72 20 6d 65 6d 6f  nections or memo
334be 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 20  ry allocations. 
334bf 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
334c0 20 6e 6f 74 0a 2a 2a 20 74 68 72 65 61 64 73 61   not.** threadsa
334c1 66 65 2e 20 20 46 61 69 6c 75 72 65 20 74 6f 20  fe.  Failure to 
334c2 68 65 65 64 20 74 68 65 73 65 20 77 61 72 6e 69  heed these warni
334c3 6e 67 73 20 63 61 6e 20 6c 65 61 64 20 74 6f 20  ngs can lead to 
334c4 75 6e 70 72 65 64 69 63 74 61 62 6c 65 0a 2a 2a  unpredictable.**
334c5 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2f 0a 53 51   behavior..*/.SQ
334c6 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
334c7 69 74 65 33 5f 63 6f 6e 66 69 67 28 69 6e 74 20  ite3_config(int 
334c8 6f 70 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c  op, ...){.  va_l
334c9 69 73 74 20 61 70 3b 0a 20 20 69 6e 74 20 72 63  ist ap;.  int rc
334ca 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
334cb 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66   /* sqlite3_conf
334cc 69 67 28 29 20 73 68 61 6c 6c 20 72 65 74 75 72  ig() shall retur
334cd 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20  n SQLITE_MISUSE 
334ce 69 66 20 69 74 20 69 73 20 69 6e 76 6f 6b 65 64  if it is invoked
334cf 20 77 68 69 6c 65 0a 20 20 2a 2a 20 74 68 65 20   while.  ** the 
334d0 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 69  SQLite library i
334d1 73 20 69 6e 20 75 73 65 2e 20 2a 2f 0a 20 20 69  s in use. */.  i
334d2 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
334d3 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20 29 20  Config.isInit ) 
334d4 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
334d5 53 55 53 45 3b 0a 0a 20 20 76 61 5f 73 74 61 72  SUSE;..  va_star
334d6 74 28 61 70 2c 20 6f 70 29 3b 0a 20 20 73 77 69  t(ap, op);.  swi
334d7 74 63 68 28 20 6f 70 20 29 7b 0a 0a 20 20 20 20  tch( op ){..    
334d8 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 66 69 67 75  /* Mutex configu
334d9 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 61  ration options a
334da 72 65 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c  re only availabl
334db 65 20 69 6e 20 61 20 74 68 72 65 61 64 73 61 66  e in a threadsaf
334dc 65 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65  e.    ** compile
334dd 2e 20 0a 20 20 20 20 2a 2f 0a 23 69 66 20 53 51  . .    */.#if SQ
334de 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a  LITE_THREADSAFE.
334df 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
334e0 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52  CONFIG_SINGLETHR
334e1 45 41 44 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  EAD: {.      /* 
334e2 44 69 73 61 62 6c 65 20 61 6c 6c 20 6d 75 74 65  Disable all mute
334e3 78 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 73 71  xing */.      sq
334e4 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
334e5 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 3d 20 30  g.bCoreMutex = 0
334e6 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  ;.      sqlite3G
334e7 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 46 75 6c  lobalConfig.bFul
334e8 6c 4d 75 74 65 78 20 3d 20 30 3b 0a 20 20 20 20  lMutex = 0;.    
334e9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
334ea 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43     case SQLITE_C
334eb 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41  ONFIG_MULTITHREA
334ec 44 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44 69  D: {.      /* Di
334ed 73 61 62 6c 65 20 6d 75 74 65 78 69 6e 67 20 6f  sable mutexing o
334ee 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  f database conne
334ef 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 20 20 20 20  ctions */.      
334f0 2f 2a 20 45 6e 61 62 6c 65 20 6d 75 74 65 78 69  /* Enable mutexi
334f1 6e 67 20 6f 66 20 63 6f 72 65 20 64 61 74 61 20  ng of core data 
334f2 73 74 72 75 63 74 75 72 65 73 20 2a 2f 0a 20 20  structures */.  
334f3 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
334f4 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74  lConfig.bCoreMut
334f5 65 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 73 71  ex = 1;.      sq
334f6 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
334f7 67 2e 62 46 75 6c 6c 4d 75 74 65 78 20 3d 20 30  g.bFullMutex = 0
334f8 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
334f9 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
334fa 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 45 52 49  LITE_CONFIG_SERI
334fb 41 4c 49 5a 45 44 3a 20 7b 0a 20 20 20 20 20 20  ALIZED: {.      
334fc 2f 2a 20 45 6e 61 62 6c 65 20 61 6c 6c 20 6d 75  /* Enable all mu
334fd 74 65 78 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20  texing */.      
334fe 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
334ff 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 3d  fig.bCoreMutex =
33500 20 31 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   1;.      sqlite
33501 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 46  3GlobalConfig.bF
33502 75 6c 6c 4d 75 74 65 78 20 3d 20 31 3b 0a 20 20  ullMutex = 1;.  
33503 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
33504 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
33505 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 3a 20 7b  _CONFIG_MUTEX: {
33506 0a 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 66  .      /* Specif
33507 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65  y an alternative
33508 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74   mutex implement
33509 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73  ation */.      s
3350a 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
3350b 69 67 2e 6d 75 74 65 78 20 3d 20 2a 76 61 5f 61  ig.mutex = *va_a
3350c 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 6d  rg(ap, sqlite3_m
3350d 75 74 65 78 5f 6d 65 74 68 6f 64 73 2a 29 3b 0a  utex_methods*);.
3350e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3350f 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49   }.    case SQLI
33510 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54  TE_CONFIG_GETMUT
33511 45 58 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 52  EX: {.      /* R
33512 65 74 72 69 65 76 65 20 74 68 65 20 63 75 72 72  etrieve the curr
33513 65 6e 74 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d  ent mutex implem
33514 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  entation */.    
33515 20 20 2a 76 61 5f 61 72 67 28 61 70 2c 20 73 71    *va_arg(ap, sq
33516 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
33517 6f 64 73 2a 29 20 3d 20 73 71 6c 69 74 65 33 47  ods*) = sqlite3G
33518 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
33519 78 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  x;.      break;.
3351a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 0a 20      }.#endif... 
3351b 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43     case SQLITE_C
3351c 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3a 20 7b 0a  ONFIG_MALLOC: {.
3351d 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 66 79        /* Specify
3351e 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20   an alternative 
3351f 6d 61 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65 6e 74  malloc implement
33520 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73  ation */.      s
33521 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
33522 69 67 2e 6d 20 3d 20 2a 76 61 5f 61 72 67 28 61  ig.m = *va_arg(a
33523 70 2c 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  p, sqlite3_mem_m
33524 65 74 68 6f 64 73 2a 29 3b 0a 20 20 20 20 20 20  ethods*);.      
33525 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
33526 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e   case SQLITE_CON
33527 46 49 47 5f 47 45 54 4d 41 4c 4c 4f 43 3a 20 7b  FIG_GETMALLOC: {
33528 0a 20 20 20 20 20 20 2f 2a 20 52 65 74 72 69 65  .      /* Retrie
33529 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d  ve the current m
3352a 61 6c 6c 6f 63 28 29 20 69 6d 70 6c 65 6d 65 6e  alloc() implemen
3352b 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  tation */.      
3352c 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
3352d 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f  lConfig.m.xMallo
3352e 63 3d 3d 30 20 29 20 73 71 6c 69 74 65 33 4d 65  c==0 ) sqlite3Me
3352f 6d 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20  mSetDefault();. 
33530 20 20 20 20 20 2a 76 61 5f 61 72 67 28 61 70 2c       *va_arg(ap,
33531 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
33532 68 6f 64 73 2a 29 20 3d 20 73 71 6c 69 74 65 33  hods*) = sqlite3
33533 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 3b 0a  GlobalConfig.m;.
33534 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
33535 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49   }.    case SQLI
33536 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41  TE_CONFIG_MEMSTA
33537 54 55 53 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  TUS: {.      /* 
33538 45 6e 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c  Enable or disabl
33539 65 20 74 68 65 20 6d 61 6c 6c 6f 63 20 73 74 61  e the malloc sta
3353a 74 75 73 20 63 6f 6c 6c 65 63 74 69 6f 6e 20 2a  tus collection *
3353b 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  /.      sqlite3G
3353c 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d  lobalConfig.bMem
3353d 73 74 61 74 20 3d 20 76 61 5f 61 72 67 28 61 70  stat = va_arg(ap
3353e 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 62 72  , int);.      br
3353f 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
33540 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ase SQLITE_CONFI
33541 47 5f 53 43 52 41 54 43 48 3a 20 7b 0a 20 20 20  G_SCRATCH: {.   
33542 20 20 20 2f 2a 20 44 65 73 69 67 6e 61 74 65 20     /* Designate 
33543 61 20 62 75 66 66 65 72 20 66 6f 72 20 73 63 72  a buffer for scr
33544 61 74 63 68 20 6d 65 6d 6f 72 79 20 73 70 61 63  atch memory spac
33545 65 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  e */.      sqlit
33546 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
33547 53 63 72 61 74 63 68 20 3d 20 76 61 5f 61 72 67  Scratch = va_arg
33548 28 61 70 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 20  (ap, void*);.   
33549 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
3354a 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68  Config.szScratch
3354b 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
3354c 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
3354d 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53  3GlobalConfig.nS
3354e 63 72 61 74 63 68 20 3d 20 76 61 5f 61 72 67 28  cratch = va_arg(
3354f 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ap, int);.      
33550 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
33551 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e   case SQLITE_CON
33552 46 49 47 5f 50 41 47 45 43 41 43 48 45 3a 20 7b  FIG_PAGECACHE: {
33553 0a 20 20 20 20 20 20 2f 2a 20 44 65 73 69 67 6e  .      /* Design
33554 61 74 65 20 61 20 62 75 66 66 65 72 20 66 6f 72  ate a buffer for
33555 20 70 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f   page cache memo
33556 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20  ry space */.    
33557 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
33558 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d 20 76 61  onfig.pPage = va
33559 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b  _arg(ap, void*);
3355a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
3355b 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67  obalConfig.szPag
3355c 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69  e = va_arg(ap, i
3355d 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  nt);.      sqlit
3355e 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
3355f 50 61 67 65 20 3d 20 76 61 5f 61 72 67 28 61 70  Page = va_arg(ap
33560 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 62 72  , int);.      br
33561 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
33562 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  case SQLITE_CONF
33563 49 47 5f 50 43 41 43 48 45 3a 20 7b 0a 20 20 20  IG_PCACHE: {.   
33564 20 20 20 2f 2a 20 53 70 65 63 69 66 79 20 61 6e     /* Specify an
33565 20 61 6c 74 65 72 6e 61 74 69 76 65 20 70 61 67   alternative pag
33566 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e  e cache implemen
33567 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  tation */.      
33568 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
33569 66 69 67 2e 70 63 61 63 68 65 20 3d 20 2a 76 61  fig.pcache = *va
3356a 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33  _arg(ap, sqlite3
3356b 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a  _pcache_methods*
3356c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
3356d 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20      }..    case 
3356e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45  SQLITE_CONFIG_GE
3356f 54 50 43 41 43 48 45 3a 20 7b 0a 20 20 20 20 20  TPCACHE: {.     
33570 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
33571 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e  alConfig.pcache.
33572 78 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20  xInit==0 ){.    
33573 20 20 20 20 73 71 6c 69 74 65 33 50 43 61 63 68      sqlite3PCach
33574 65 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20  eSetDefault();. 
33575 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 76 61       }.      *va
33576 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33  _arg(ap, sqlite3
33577 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a  _pcache_methods*
33578 29 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  ) = sqlite3Globa
33579 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 3b 0a  lConfig.pcache;.
3357a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3357b 20 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28   }..#if defined(
3357c 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
3357d 4d 53 59 53 33 29 20 7c 7c 20 64 65 66 69 6e 65  MSYS3) || define
3357e 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
3357f 4d 45 4d 53 59 53 35 29 0a 20 20 20 20 63 61 73  MEMSYS5).    cas
33580 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
33581 48 45 41 50 3a 20 7b 0a 20 20 20 20 20 20 2f 2a  HEAP: {.      /*
33582 20 44 65 73 69 67 6e 61 74 65 20 61 20 62 75 66   Designate a buf
33583 66 65 72 20 66 6f 72 20 68 65 61 70 20 6d 65 6d  fer for heap mem
33584 6f 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 20  ory space */.   
33585 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
33586 43 6f 6e 66 69 67 2e 70 48 65 61 70 20 3d 20 76  Config.pHeap = v
33587 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 2a 29  a_arg(ap, void*)
33588 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  ;.      sqlite3G
33589 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61  lobalConfig.nHea
3358a 70 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69  p = va_arg(ap, i
3358b 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  nt);.      sqlit
3358c 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
3358d 6e 52 65 71 20 3d 20 76 61 5f 61 72 67 28 61 70  nReq = va_arg(ap
3358e 2c 20 69 6e 74 29 3b 0a 0a 20 20 20 20 20 20 69  , int);..      i
3358f 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
33590 43 6f 6e 66 69 67 2e 70 48 65 61 70 3d 3d 30 20  Config.pHeap==0 
33591 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  ){.        /* If
33592 20 74 68 65 20 68 65 61 70 20 70 6f 69 6e 74 65   the heap pointe
33593 72 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20  r is NULL, then 
33594 72 65 73 74 6f 72 65 20 74 68 65 20 6d 61 6c 6c  restore the mall
33595 6f 63 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  oc implementatio
33596 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 61 63  n.        ** bac
33597 6b 20 74 6f 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  k to NULL pointe
33598 72 73 20 74 6f 6f 2e 20 20 54 68 69 73 20 77 69  rs too.  This wi
33599 6c 6c 20 63 61 75 73 65 20 74 68 65 20 6d 61 6c  ll cause the mal
3359a 6c 6f 63 20 74 6f 20 67 6f 0a 20 20 20 20 20 20  loc to go.      
3359b 20 20 2a 2a 20 62 61 63 6b 20 74 6f 20 69 74 73    ** back to its
3359c 20 64 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65   default impleme
3359d 6e 74 61 74 69 6f 6e 20 77 68 65 6e 20 73 71 6c  ntation when sql
3359e 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
3359f 29 20 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  ) is.        ** 
335a0 72 75 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  run..        */.
335a1 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26          memset(&
335a2 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
335a3 66 69 67 2e 6d 2c 20 30 2c 20 73 69 7a 65 6f 66  fig.m, 0, sizeof
335a4 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  (sqlite3GlobalCo
335a5 6e 66 69 67 2e 6d 29 29 3b 0a 20 20 20 20 20 20  nfig.m));.      
335a6 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
335a7 2a 20 54 68 65 20 68 65 61 70 20 70 6f 69 6e 74  * The heap point
335a8 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  er is not NULL, 
335a9 74 68 65 6e 20 69 6e 73 74 61 6c 6c 20 6f 6e 65  then install one
335aa 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20   of the.        
335ab 2a 2a 20 6d 65 6d 35 2e 63 2f 6d 65 6d 33 2e 63  ** mem5.c/mem3.c
335ac 20 6d 65 74 68 6f 64 73 2e 20 49 66 20 6e 65 69   methods. If nei
335ad 74 68 65 72 20 45 4e 41 42 4c 45 5f 4d 45 4d 53  ther ENABLE_MEMS
335ae 59 53 33 20 6e 6f 72 0a 20 20 20 20 20 20 20 20  YS3 nor.        
335af 2a 2a 20 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  ** ENABLE_MEMSYS
335b0 35 20 69 73 20 64 65 66 69 6e 65 64 2c 20 72 65  5 is defined, re
335b1 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20  turn an error.. 
335b2 20 20 20 20 20 20 20 2a 2f 0a 23 69 66 64 65 66         */.#ifdef
335b3 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
335b4 45 4d 53 59 53 33 0a 20 20 20 20 20 20 20 20 73  EMSYS3.        s
335b5 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
335b6 69 67 2e 6d 20 3d 20 2a 73 71 6c 69 74 65 33 4d  ig.m = *sqlite3M
335b7 65 6d 47 65 74 4d 65 6d 73 79 73 33 28 29 3b 0a  emGetMemsys3();.
335b8 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
335b9 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53  LITE_ENABLE_MEMS
335ba 59 53 35 0a 20 20 20 20 20 20 20 20 73 71 6c 69  YS5.        sqli
335bb 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
335bc 6d 20 3d 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47  m = *sqlite3MemG
335bd 65 74 4d 65 6d 73 79 73 35 28 29 3b 0a 23 65 6e  etMemsys5();.#en
335be 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  dif.      }.    
335bf 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
335c0 65 6e 64 69 66 0a 0a 20 20 20 20 63 61 73 65 20  endif..    case 
335c1 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f  SQLITE_CONFIG_LO
335c2 4f 4b 41 53 49 44 45 3a 20 7b 0a 20 20 20 20 20  OKASIDE: {.     
335c3 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
335c4 6e 66 69 67 2e 73 7a 4c 6f 6f 6b 61 73 69 64 65  nfig.szLookaside
335c5 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
335c6 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
335c7 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 4c  3GlobalConfig.nL
335c8 6f 6f 6b 61 73 69 64 65 20 3d 20 76 61 5f 61 72  ookaside = va_ar
335c9 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20  g(ap, int);.    
335ca 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
335cb 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
335cc 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
335cd 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 62 72  _ERROR;.      br
335ce 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
335cf 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72   va_end(ap);.  r
335d0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
335d1 2a 2a 20 53 65 74 20 75 70 20 74 68 65 20 6c 6f  ** Set up the lo
335d2 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 73 20  okaside buffers 
335d3 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 20 63  for a database c
335d4 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 20 52 65  onnection..** Re
335d5 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  turn SQLITE_OK o
335d6 6e 20 73 75 63 63 65 73 73 2e 20 20 0a 2a 2a 20  n success.  .** 
335d7 49 66 20 6c 6f 6f 6b 61 73 69 64 65 20 69 73 20  If lookaside is 
335d8 61 6c 72 65 61 64 79 20 61 63 74 69 76 65 2c 20  already active, 
335d9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
335da 53 59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 7a  SY..**.** The sz
335db 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68   parameter is th
335dc 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
335dd 73 20 69 6e 20 65 61 63 68 20 6c 6f 6f 6b 61 73  s in each lookas
335de 69 64 65 20 73 6c 6f 74 2e 0a 2a 2a 20 54 68 65  ide slot..** The
335df 20 63 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69   cnt parameter i
335e0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
335e1 73 6c 6f 74 73 2e 20 20 49 66 20 70 53 74 61 72  slots.  If pStar
335e2 74 20 69 73 20 4e 55 4c 4c 20 74 68 65 0a 2a 2a  t is NULL the.**
335e3 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 6c   space for the l
335e4 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20  ookaside memory 
335e5 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  is obtained from
335e6 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
335e7 29 2e 0a 2a 2a 20 49 66 20 70 53 74 61 72 74 20  )..** If pStart 
335e8 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e  is not NULL then
335e9 20 69 74 20 69 73 20 73 7a 2a 63 6e 74 20 62 79   it is sz*cnt by
335ea 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 74 6f  tes of memory to
335eb 20 75 73 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20   use for.** the 
335ec 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79  lookaside memory
335ed 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
335ee 73 65 74 75 70 4c 6f 6f 6b 61 73 69 64 65 28 73  setupLookaside(s
335ef 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64  qlite3 *db, void
335f0 20 2a 70 42 75 66 2c 20 69 6e 74 20 73 7a 2c 20   *pBuf, int sz, 
335f1 69 6e 74 20 63 6e 74 29 7b 0a 20 20 76 6f 69 64  int cnt){.  void
335f2 20 2a 70 53 74 61 72 74 3b 0a 20 20 69 66 28 20   *pStart;.  if( 
335f3 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f  db->lookaside.nO
335f4 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ut ){.    return
335f5 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
335f6 7d 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e 79 20  }.  /* Free any 
335f7 65 78 69 73 74 69 6e 67 20 6c 6f 6f 6b 61 73 69  existing lookasi
335f8 64 65 20 62 75 66 66 65 72 20 66 6f 72 20 74 68  de buffer for th
335f9 69 73 20 68 61 6e 64 6c 65 20 62 65 66 6f 72 65  is handle before
335fa 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 69 6e 67  .  ** allocating
335fb 20 61 20 6e 65 77 20 6f 6e 65 20 73 6f 20 77 65   a new one so we
335fc 20 64 6f 6e 27 74 20 68 61 76 65 20 74 6f 20 68   don't have to h
335fd 61 76 65 20 73 70 61 63 65 20 66 6f 72 20 0a 20  ave space for . 
335fe 20 2a 2a 20 62 6f 74 68 20 61 74 20 74 68 65 20   ** both at the 
335ff 73 61 6d 65 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a  same time..  */.
33600 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73    if( db->lookas
33601 69 64 65 2e 62 4d 61 6c 6c 6f 63 65 64 20 29 7b  ide.bMalloced ){
33602 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
33603 65 28 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  e(db->lookaside.
33604 70 53 74 61 72 74 29 3b 0a 20 20 7d 0a 20 20 2f  pStart);.  }.  /
33605 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 61 20  * The size of a 
33606 6c 6f 6f 6b 61 73 69 64 65 20 73 6c 6f 74 20 6e  lookaside slot n
33607 65 65 64 73 20 74 6f 20 62 65 20 6c 61 72 67 65  eeds to be large
33608 72 20 74 68 61 6e 20 61 20 70 6f 69 6e 74 65 72  r than a pointer
33609 0a 20 20 2a 2a 20 74 6f 20 62 65 20 75 73 65 66  .  ** to be usef
3360a 75 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ul..  */.  if( s
3360b 7a 3c 3d 28 69 6e 74 29 73 69 7a 65 6f 66 28 4c  z<=(int)sizeof(L
3360c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29 20 29  ookasideSlot*) )
3360d 20 73 7a 20 3d 20 30 3b 0a 20 20 69 66 28 20 63   sz = 0;.  if( c
3360e 6e 74 3c 30 20 29 20 63 6e 74 20 3d 20 30 3b 0a  nt<0 ) cnt = 0;.
3360f 20 20 69 66 28 20 73 7a 3d 3d 30 20 7c 7c 20 63    if( sz==0 || c
33610 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 7a 20  nt==0 ){.    sz 
33611 3d 20 30 3b 0a 20 20 20 20 70 53 74 61 72 74 20  = 0;.    pStart 
33612 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  = 0;.  }else if(
33613 20 70 42 75 66 3d 3d 30 20 29 7b 0a 20 20 20 20   pBuf==0 ){.    
33614 73 7a 20 3d 20 52 4f 55 4e 44 38 28 73 7a 29 3b  sz = ROUND8(sz);
33615 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69  .    sqlite3Begi
33616 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  nBenignMalloc();
33617 0a 20 20 20 20 70 53 74 61 72 74 20 3d 20 73 71  .    pStart = sq
33618 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73 7a 2a  lite3Malloc( sz*
33619 63 6e 74 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  cnt );.    sqlit
3361a 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f  e3EndBenignMallo
3361b 63 28 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  c();.  }else{.  
3361c 20 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e    sz = ROUNDDOWN
3361d 38 28 73 7a 29 3b 0a 20 20 20 20 70 53 74 61 72  8(sz);.    pStar
3361e 74 20 3d 20 70 42 75 66 3b 0a 20 20 7d 0a 20 20  t = pBuf;.  }.  
3361f 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53  db->lookaside.pS
33620 74 61 72 74 20 3d 20 70 53 74 61 72 74 3b 0a 20  tart = pStart;. 
33621 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70   db->lookaside.p
33622 46 72 65 65 20 3d 20 30 3b 0a 20 20 64 62 2d 3e  Free = 0;.  db->
33623 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 20 3d 20 28  lookaside.sz = (
33624 75 31 36 29 73 7a 3b 0a 20 20 69 66 28 20 70 53  u16)sz;.  if( pS
33625 74 61 72 74 20 29 7b 0a 20 20 20 20 69 6e 74 20  tart ){.    int 
33626 69 3b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 64 65  i;.    Lookaside
33627 53 6c 6f 74 20 2a 70 3b 0a 20 20 20 20 61 73 73  Slot *p;.    ass
33628 65 72 74 28 20 73 7a 20 3e 20 28 69 6e 74 29 73  ert( sz > (int)s
33629 69 7a 65 6f 66 28 4c 6f 6f 6b 61 73 69 64 65 53  izeof(LookasideS
3362a 6c 6f 74 2a 29 20 29 3b 0a 20 20 20 20 70 20 3d  lot*) );.    p =
3362b 20 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 2a   (LookasideSlot*
3362c 29 70 53 74 61 72 74 3b 0a 20 20 20 20 66 6f 72  )pStart;.    for
3362d 28 69 3d 63 6e 74 2d 31 3b 20 69 3e 3d 30 3b 20  (i=cnt-1; i>=0; 
3362e 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70  i--){.      p->p
3362f 4e 65 78 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61  Next = db->looka
33630 73 69 64 65 2e 70 46 72 65 65 3b 0a 20 20 20 20  side.pFree;.    
33631 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
33632 70 46 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 20  pFree = p;.     
33633 20 70 20 3d 20 28 4c 6f 6f 6b 61 73 69 64 65 53   p = (LookasideS
33634 6c 6f 74 2a 29 26 28 28 75 38 2a 29 70 29 5b 73  lot*)&((u8*)p)[s
33635 7a 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62  z];.    }.    db
33636 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e 64  ->lookaside.pEnd
33637 20 3d 20 70 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f   = p;.    db->lo
33638 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
33639 20 3d 20 31 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f   = 1;.    db->lo
3363a 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c 6f 63 65  okaside.bMalloce
3363b 64 20 3d 20 70 42 75 66 3d 3d 30 20 3f 31 3a 30  d = pBuf==0 ?1:0
3363c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  ;.  }else{.    d
3363d 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e  b->lookaside.pEn
3363e 64 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 6c  d = 0;.    db->l
3363f 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65  ookaside.bEnable
33640 64 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 6c  d = 0;.    db->l
33641 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c 6f 63  ookaside.bMalloc
33642 65 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  ed = 0;.  }.  re
33643 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
33644 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
33645 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69  the mutex associ
33646 61 74 65 64 20 77 69 74 68 20 61 20 64 61 74 61  ated with a data
33647 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
33648 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  .*/.SQLITE_API s
33649 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71  qlite3_mutex *sq
3364a 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 28 73  lite3_db_mutex(s
3364b 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 72  qlite3 *db){.  r
3364c 65 74 75 72 6e 20 64 62 2d 3e 6d 75 74 65 78 3b  eturn db->mutex;
3364d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 66 69 67  .}../*.** Config
3364e 75 72 61 74 69 6f 6e 20 73 65 74 74 69 6e 67 73  uration settings
3364f 20 66 6f 72 20 61 6e 20 69 6e 64 69 76 69 64 75   for an individu
33650 61 6c 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  al database conn
33651 65 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45  ection.*/.SQLITE
33652 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
33653 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69 74  _db_config(sqlit
33654 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 70 2c 20  e3 *db, int op, 
33655 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
33656 61 70 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  ap;.  int rc;.  
33657 76 61 5f 73 74 61 72 74 28 61 70 2c 20 6f 70 29  va_start(ap, op)
33658 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29  ;.  switch( op )
33659 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
3365a 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41  E_DBCONFIG_LOOKA
3365b 53 49 44 45 3a 20 7b 0a 20 20 20 20 20 20 76 6f  SIDE: {.      vo
3365c 69 64 20 2a 70 42 75 66 20 3d 20 76 61 5f 61 72  id *pBuf = va_ar
3365d 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b 0a 20 20  g(ap, void*);.  
3365e 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 76 61 5f      int sz = va_
3365f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
33660 20 20 20 20 69 6e 74 20 63 6e 74 20 3d 20 76 61      int cnt = va
33661 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20  _arg(ap, int);. 
33662 20 20 20 20 20 72 63 20 3d 20 73 65 74 75 70 4c       rc = setupL
33663 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 42 75  ookaside(db, pBu
33664 66 2c 20 73 7a 2c 20 63 6e 74 29 3b 0a 20 20 20  f, sz, cnt);.   
33665 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
33666 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
33667 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
33668 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 62 72  _ERROR;.      br
33669 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
3366a 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72   va_end(ap);.  r
3366b 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
3366c 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
3366d 69 66 20 74 68 65 20 62 75 66 66 65 72 20 7a 5b  if the buffer z[
3366e 30 2e 2e 6e 2d 31 5d 20 63 6f 6e 74 61 69 6e 73  0..n-1] contains
3366f 20 61 6c 6c 20 73 70 61 63 65 73 2e 0a 2a 2f 0a   all spaces..*/.
33670 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 53 70  static int allSp
33671 61 63 65 73 28 63 6f 6e 73 74 20 63 68 61 72 20  aces(const char 
33672 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 77 68  *z, int n){.  wh
33673 69 6c 65 28 20 6e 3e 30 20 26 26 20 7a 5b 6e 2d  ile( n>0 && z[n-
33674 31 5d 3d 3d 27 20 27 20 29 7b 20 6e 2d 2d 3b 20  1]==' ' ){ n--; 
33675 7d 0a 20 20 72 65 74 75 72 6e 20 6e 3d 3d 30 3b  }.  return n==0;
33676 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69  .}../*.** This i
33677 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f  s the default co
33678 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llating function
33679 20 6e 61 6d 65 64 20 22 42 49 4e 41 52 59 22 20   named "BINARY" 
3367a 77 68 69 63 68 20 69 73 20 61 6c 77 61 79 73 0a  which is always.
3367b 2a 2a 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a  ** available..**
3367c 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 64 46 6c  .** If the padFl
3367d 61 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e  ag argument is n
3367e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 73 70 61  ot NULL then spa
3367f 63 65 20 70 61 64 64 69 6e 67 20 61 74 20 74 68  ce padding at th
33680 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 73 74 72 69  e end.** of stri
33681 6e 67 73 20 69 73 20 69 67 6e 6f 72 65 64 2e 20  ngs is ignored. 
33682 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 73   This implements
33683 20 74 68 65 20 52 54 52 49 4d 20 63 6f 6c 6c 61   the RTRIM colla
33684 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
33685 69 6e 74 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 28  int binCollFunc(
33686 0a 20 20 76 6f 69 64 20 2a 70 61 64 46 6c 61 67  .  void *padFlag
33687 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63  ,.  int nKey1, c
33688 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31  onst void *pKey1
33689 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 32 2c 20 63  ,.  int nKey2, c
3368a 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32  onst void *pKey2
3368b 0a 29 7b 0a 20 20 69 6e 74 20 72 63 2c 20 6e 3b  .){.  int rc, n;
3368c 0a 20 20 6e 20 3d 20 6e 4b 65 79 31 3c 6e 4b 65  .  n = nKey1<nKe
3368d 79 32 20 3f 20 6e 4b 65 79 31 20 3a 20 6e 4b 65  y2 ? nKey1 : nKe
3368e 79 32 3b 0a 20 20 72 63 20 3d 20 6d 65 6d 63 6d  y2;.  rc = memcm
3368f 70 28 70 4b 65 79 31 2c 20 70 4b 65 79 32 2c 20  p(pKey1, pKey2, 
33690 6e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30 20  n);.  if( rc==0 
33691 29 7b 0a 20 20 20 20 69 66 28 20 70 61 64 46 6c  ){.    if( padFl
33692 61 67 0a 20 20 20 20 20 26 26 20 61 6c 6c 53 70  ag.     && allSp
33693 61 63 65 73 28 28 28 63 68 61 72 2a 29 70 4b 65  aces(((char*)pKe
33694 79 31 29 2b 6e 2c 20 6e 4b 65 79 31 2d 6e 29 0a  y1)+n, nKey1-n).
33695 20 20 20 20 20 26 26 20 61 6c 6c 53 70 61 63 65       && allSpace
33696 73 28 28 28 63 68 61 72 2a 29 70 4b 65 79 32 29  s(((char*)pKey2)
33697 2b 6e 2c 20 6e 4b 65 79 32 2d 6e 29 0a 20 20 20  +n, nKey2-n).   
33698 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4c 65 61   ){.      /* Lea
33699 76 65 20 72 63 20 75 6e 63 68 61 6e 67 65 64 20  ve rc unchanged 
3369a 61 74 20 30 20 2a 2f 0a 20 20 20 20 7d 65 6c 73  at 0 */.    }els
3369b 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 4b  e{.      rc = nK
3369c 65 79 31 20 2d 20 6e 4b 65 79 32 3b 0a 20 20 20  ey1 - nKey2;.   
3369d 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
3369e 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 6f  rc;.}../*.** Ano
3369f 74 68 65 72 20 62 75 69 6c 74 2d 69 6e 20 63 6f  ther built-in co
336a0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
336a1 3a 20 4e 4f 43 41 53 45 2e 20 0a 2a 2a 0a 2a 2a  : NOCASE. .**.**
336a2 20 54 68 69 73 20 63 6f 6c 6c 61 74 69 6e 67 20   This collating 
336a3 73 65 71 75 65 6e 63 65 20 69 73 20 69 6e 74 65  sequence is inte
336a4 6e 64 65 64 20 74 6f 20 62 65 20 75 73 65 64 20  nded to be used 
336a5 66 6f 72 20 22 63 61 73 65 20 69 6e 64 65 70 65  for "case indepe
336a6 6e 64 61 6e 74 0a 2a 2a 20 63 6f 6d 70 61 72 69  ndant.** compari
336a7 73 6f 6e 22 2e 20 53 51 4c 69 74 65 27 73 20 6b  son". SQLite's k
336a8 6e 6f 77 6c 65 64 67 65 20 6f 66 20 75 70 70 65  nowledge of uppe
336a9 72 20 61 6e 64 20 6c 6f 77 65 72 20 63 61 73 65  r and lower case
336aa 20 65 71 75 69 76 61 6c 65 6e 74 73 0a 2a 2a 20   equivalents.** 
336ab 65 78 74 65 6e 64 73 20 6f 6e 6c 79 20 74 6f 20  extends only to 
336ac 74 68 65 20 32 36 20 63 68 61 72 61 63 74 65 72  the 26 character
336ad 73 20 75 73 65 64 20 69 6e 20 74 68 65 20 45 6e  s used in the En
336ae 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 2e 0a  glish language..
336af 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20 6d 6f 6d  **.** At the mom
336b0 65 6e 74 20 74 68 65 72 65 20 69 73 20 6f 6e 6c  ent there is onl
336b1 79 20 61 20 55 54 46 2d 38 20 69 6d 70 6c 65 6d  y a UTF-8 implem
336b2 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  entation..*/.sta
336b3 74 69 63 20 69 6e 74 20 6e 6f 63 61 73 65 43 6f  tic int nocaseCo
336b4 6c 6c 61 74 69 6e 67 46 75 6e 63 28 0a 20 20 76  llatingFunc(.  v
336b5 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c 0a 20 20  oid *NotUsed,.  
336b6 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74  int nKey1, const
336b7 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 0a 20 20   void *pKey1,.  
336b8 69 6e 74 20 6e 4b 65 79 32 2c 20 63 6f 6e 73 74  int nKey2, const
336b9 20 76 6f 69 64 20 2a 70 4b 65 79 32 0a 29 7b 0a   void *pKey2.){.
336ba 20 20 69 6e 74 20 72 20 3d 20 73 71 6c 69 74 65    int r = sqlite
336bb 33 53 74 72 4e 49 43 6d 70 28 0a 20 20 20 20 20  3StrNICmp(.     
336bc 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70   (const char *)p
336bd 4b 65 79 31 2c 20 28 63 6f 6e 73 74 20 63 68 61  Key1, (const cha
336be 72 20 2a 29 70 4b 65 79 32 2c 20 28 6e 4b 65 79  r *)pKey2, (nKey
336bf 31 3c 6e 4b 65 79 32 29 3f 6e 4b 65 79 31 3a 6e  1<nKey2)?nKey1:n
336c0 4b 65 79 32 29 3b 0a 20 20 55 4e 55 53 45 44 5f  Key2);.  UNUSED_
336c1 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
336c2 64 29 3b 0a 20 20 69 66 28 20 30 3d 3d 72 20 29  d);.  if( 0==r )
336c3 7b 0a 20 20 20 20 72 20 3d 20 6e 4b 65 79 31 2d  {.    r = nKey1-
336c4 6e 4b 65 79 32 3b 0a 20 20 7d 0a 20 20 72 65 74  nKey2;.  }.  ret
336c5 75 72 6e 20 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn r;.}../*.** 
336c6 52 65 74 75 72 6e 20 74 68 65 20 52 4f 57 49 44  Return the ROWID
336c7 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
336c8 65 6e 74 20 69 6e 73 65 72 74 0a 2a 2f 0a 53 51  ent insert.*/.SQ
336c9 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 5f  LITE_API sqlite_
336ca 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6c 61  int64 sqlite3_la
336cb 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
336cc 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
336cd 72 65 74 75 72 6e 20 64 62 2d 3e 6c 61 73 74 52  return db->lastR
336ce 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  owid;.}../*.** R
336cf 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
336d0 20 6f 66 20 63 68 61 6e 67 65 73 20 69 6e 20 74   of changes in t
336d1 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63  he most recent c
336d2 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 65  all to sqlite3_e
336d3 78 65 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  xec()..*/.SQLITE
336d4 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
336d5 5f 63 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33  _changes(sqlite3
336d6 20 2a 64 62 29 7b 0a 20 20 72 65 74 75 72 6e 20   *db){.  return 
336d7 64 62 2d 3e 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a  db->nChange;.}..
336d8 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
336d9 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67   number of chang
336da 65 73 20 73 69 6e 63 65 20 74 68 65 20 64 61 74  es since the dat
336db 61 62 61 73 65 20 68 61 6e 64 6c 65 20 77 61 73  abase handle was
336dc 20 6f 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49   opened..*/.SQLI
336dd 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
336de 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
336df 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
336e0 20 72 65 74 75 72 6e 20 64 62 2d 3e 6e 54 6f 74   return db->nTot
336e1 61 6c 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a  alChange;.}../*.
336e2 2a 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 6f 70 65  ** Close all ope
336e3 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 54 68  n savepoints. Th
336e4 69 73 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 6c 79  is function only
336e5 20 6d 61 6e 69 70 75 6c 61 74 65 73 20 66 69 65   manipulates fie
336e6 6c 64 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61  lds of the.** da
336e7 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 6f 62  tabase handle ob
336e8 6a 65 63 74 2c 20 69 74 20 64 6f 65 73 20 6e 6f  ject, it does no
336e9 74 20 63 6c 6f 73 65 20 61 6e 79 20 73 61 76 65  t close any save
336ea 70 6f 69 6e 74 73 20 74 68 61 74 20 6d 61 79 20  points that may 
336eb 62 65 20 6f 70 65 6e 0a 2a 2a 20 61 74 20 74 68  be open.** at th
336ec 65 20 62 2d 74 72 65 65 2f 70 61 67 65 72 20 6c  e b-tree/pager l
336ed 65 76 65 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  evel..*/.SQLITE_
336ee 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
336ef 69 74 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69  ite3CloseSavepoi
336f0 6e 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29  nts(sqlite3 *db)
336f1 7b 0a 20 20 77 68 69 6c 65 28 20 64 62 2d 3e 70  {.  while( db->p
336f2 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20  Savepoint ){.   
336f3 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 54 6d 70   Savepoint *pTmp
336f4 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e   = db->pSavepoin
336f5 74 3b 0a 20 20 20 20 64 62 2d 3e 70 53 61 76 65  t;.    db->pSave
336f6 70 6f 69 6e 74 20 3d 20 70 54 6d 70 2d 3e 70 4e  point = pTmp->pN
336f7 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ext;.    sqlite3
336f8 44 62 46 72 65 65 28 64 62 2c 20 70 54 6d 70 29  DbFree(db, pTmp)
336f9 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 6e 53 61 76  ;.  }.  db->nSav
336fa 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 64 62  epoint = 0;.  db
336fb 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20 3d 20 30  ->nStatement = 0
336fc 3b 0a 20 20 64 62 2d 3e 69 73 54 72 61 6e 73 61  ;.  db->isTransa
336fd 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 20 3d  ctionSavepoint =
336fe 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f   0;.}../*.** Clo
336ff 73 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20 53  se an existing S
33700 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 0a 2a  QLite database.*
33701 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
33702 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 73   sqlite3_close(s
33703 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 48  qlite3 *db){.  H
33704 61 73 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 69 6e  ashElem *i;.  in
33705 74 20 6a 3b 0a 0a 20 20 69 66 28 20 21 64 62 20  t j;..  if( !db 
33706 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
33707 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69  LITE_OK;.  }.  i
33708 66 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74  f( !sqlite3Safet
33709 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64  yCheckSickOrOk(d
3370a 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  b) ){.    return
3370b 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
3370c 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
3370d 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
3370e 74 65 78 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33  tex);..  sqlite3
3370f 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
33710 65 6d 61 28 64 62 2c 20 30 29 3b 0a 0a 20 20 2f  ema(db, 0);..  /
33711 2a 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69  * If a transacti
33712 6f 6e 20 69 73 20 6f 70 65 6e 2c 20 74 68 65 20  on is open, the 
33713 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
33714 65 6d 61 28 29 20 63 61 6c 6c 20 61 62 6f 76 65  ema() call above
33715 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 68  .  ** will not h
33716 61 76 65 20 63 61 6c 6c 65 64 20 74 68 65 20 78  ave called the x
33717 44 69 73 63 6f 6e 6e 65 63 74 28 29 20 6d 65 74  Disconnect() met
33718 68 6f 64 20 6f 6e 20 61 6e 79 20 76 69 72 74 75  hod on any virtu
33719 61 6c 0a 20 20 2a 2a 20 74 61 62 6c 65 73 20 69  al.  ** tables i
3371a 6e 20 74 68 65 20 64 62 2d 3e 61 56 54 72 61 6e  n the db->aVTran
3371b 73 5b 5d 20 61 72 72 61 79 2e 20 54 68 65 20 66  s[] array. The f
3371c 6f 6c 6c 6f 77 69 6e 67 20 73 71 6c 69 74 65 33  ollowing sqlite3
3371d 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 29 0a 20  VtabRollback(). 
3371e 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c 20 64 6f   ** call will do
3371f 20 73 6f 2e 20 57 65 20 6e 65 65 64 20 74 6f 20   so. We need to 
33720 64 6f 20 74 68 69 73 20 62 65 66 6f 72 65 20 74  do this before t
33721 68 65 20 63 68 65 63 6b 20 66 6f 72 20 61 63 74  he check for act
33722 69 76 65 0a 20 20 2a 2a 20 53 51 4c 20 73 74 61  ive.  ** SQL sta
33723 74 65 6d 65 6e 74 73 20 62 65 6c 6f 77 2c 20 61  tements below, a
33724 73 20 74 68 65 20 76 2d 74 61 62 6c 65 20 69 6d  s the v-table im
33725 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 61 79  plementation may
33726 20 62 65 20 73 74 6f 72 69 6e 67 0a 20 20 2a 2a   be storing.  **
33727 20 73 6f 6d 65 20 70 72 65 70 61 72 65 64 20 73   some prepared s
33728 74 61 74 65 6d 65 6e 74 73 20 69 6e 74 65 72 6e  tatements intern
33729 61 6c 6c 79 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  ally..  */.  sql
3372a 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b  ite3VtabRollback
3372b 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  (db);..  /* If t
3372c 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f 75 74  here are any out
3372d 73 74 61 6e 64 69 6e 67 20 56 4d 73 2c 20 72 65  standing VMs, re
3372e 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
3372f 2e 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 70  . */.  if( db->p
33730 56 64 62 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  Vdbe ){.    sqli
33731 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
33732 49 54 45 5f 42 55 53 59 2c 20 0a 20 20 20 20 20  ITE_BUSY, .     
33733 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 63 6c     "unable to cl
33734 6f 73 65 20 64 75 65 20 74 6f 20 75 6e 66 69 6e  ose due to unfin
33735 61 6c 69 73 65 64 20 73 74 61 74 65 6d 65 6e 74  alised statement
33736 73 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  s");.    sqlite3
33737 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
33738 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74  >mutex);.    ret
33739 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  urn SQLITE_BUSY;
3373a 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 73  .  }.  assert( s
3373b 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
3373c 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 3b  kSickOrOk(db) );
3373d 0a 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 64  ..  for(j=0; j<d
3373e 62 2d 3e 6e 44 62 3b 20 6a 2b 2b 29 7b 0a 20 20  b->nDb; j++){.  
3373f 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64    Btree *pBt = d
33740 62 2d 3e 61 44 62 5b 6a 5d 2e 70 42 74 3b 0a 20  b->aDb[j].pBt;. 
33741 20 20 20 69 66 28 20 70 42 74 20 26 26 20 73 71     if( pBt && sq
33742 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 42 61  lite3BtreeIsInBa
33743 63 6b 75 70 28 70 42 74 29 20 29 7b 0a 20 20 20  ckup(pBt) ){.   
33744 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
33745 64 62 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 2c  db, SQLITE_BUSY,
33746 20 0a 20 20 20 20 20 20 20 20 20 20 22 75 6e 61   .          "una
33747 62 6c 65 20 74 6f 20 63 6c 6f 73 65 20 64 75 65  ble to close due
33748 20 74 6f 20 75 6e 66 69 6e 69 73 68 65 64 20 62   to unfinished b
33749 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 22  ackup operation"
3374a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
3374b 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
3374c 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 72  >mutex);.      r
3374d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
3374e 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  Y;.    }.  }..  
3374f 2f 2a 20 46 72 65 65 20 61 6e 79 20 6f 75 74 73  /* Free any outs
33750 74 61 6e 64 69 6e 67 20 53 61 76 65 70 6f 69 6e  tanding Savepoin
33751 74 20 73 74 72 75 63 74 75 72 65 73 2e 20 2a 2f  t structures. */
33752 0a 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53  .  sqlite3CloseS
33753 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a 0a  avepoints(db);..
33754 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 64 62 2d    for(j=0; j<db-
33755 3e 6e 44 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  >nDb; j++){.    
33756 73 74 72 75 63 74 20 44 62 20 2a 70 44 62 20 3d  struct Db *pDb =
33757 20 26 64 62 2d 3e 61 44 62 5b 6a 5d 3b 0a 20 20   &db->aDb[j];.  
33758 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74 20 29    if( pDb->pBt )
33759 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  {.      sqlite3B
3375a 74 72 65 65 43 6c 6f 73 65 28 70 44 62 2d 3e 70  treeClose(pDb->p
3375b 42 74 29 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e  Bt);.      pDb->
3375c 70 42 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  pBt = 0;.      i
3375d 66 28 20 6a 21 3d 31 20 29 7b 0a 20 20 20 20 20  f( j!=1 ){.     
3375e 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 20     pDb->pSchema 
3375f 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
33760 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
33761 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
33762 65 6d 61 28 64 62 2c 20 30 29 3b 0a 0a 20 20 2f  ema(db, 0);..  /
33763 2a 20 54 65 6c 6c 20 74 68 65 20 63 6f 64 65 20  * Tell the code 
33764 69 6e 20 6e 6f 74 69 66 79 2e 63 20 74 68 61 74  in notify.c that
33765 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20   the connection 
33766 6e 6f 20 6c 6f 6e 67 65 72 20 68 6f 6c 64 73 20  no longer holds 
33767 61 6e 79 0a 20 20 2a 2a 20 6c 6f 63 6b 73 20 61  any.  ** locks a
33768 6e 64 20 64 6f 65 73 20 6e 6f 74 20 72 65 71 75  nd does not requ
33769 69 72 65 20 61 6e 79 20 66 75 72 74 68 65 72 20  ire any further 
3376a 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
3376b 6c 6c 62 61 63 6b 73 2e 0a 20 20 2a 2f 0a 20 20  llbacks..  */.  
3376c 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f  sqlite3Connectio
3376d 6e 43 6c 6f 73 65 64 28 64 62 29 3b 0a 0a 20 20  nClosed(db);..  
3376e 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 44 62 3c  assert( db->nDb<
3376f 3d 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =2 );.  assert( 
33770 64 62 2d 3e 61 44 62 3d 3d 64 62 2d 3e 61 44 62  db->aDb==db->aDb
33771 53 74 61 74 69 63 20 29 3b 0a 20 20 66 6f 72 28  Static );.  for(
33772 6a 3d 30 3b 20 6a 3c 41 72 72 61 79 53 69 7a 65  j=0; j<ArraySize
33773 28 64 62 2d 3e 61 46 75 6e 63 2e 61 29 3b 20 6a  (db->aFunc.a); j
33774 2b 2b 29 7b 0a 20 20 20 20 46 75 6e 63 44 65 66  ++){.    FuncDef
33775 20 2a 70 4e 65 78 74 2c 20 2a 70 48 61 73 68 2c   *pNext, *pHash,
33776 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 70 3d 64   *p;.    for(p=d
33777 62 2d 3e 61 46 75 6e 63 2e 61 5b 6a 5d 3b 20 70  b->aFunc.a[j]; p
33778 3b 20 70 3d 70 48 61 73 68 29 7b 0a 20 20 20 20  ; p=pHash){.    
33779 20 20 70 48 61 73 68 20 3d 20 70 2d 3e 70 48 61    pHash = p->pHa
3377a 73 68 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28  sh;.      while(
3377b 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4e   p ){.        pN
3377c 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a  ext = p->pNext;.
3377d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
3377e 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20  bFree(db, p);.  
3377f 20 20 20 20 20 20 70 20 3d 20 70 4e 65 78 74 3b        p = pNext;
33780 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
33781 20 7d 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74   }.  for(i=sqlit
33782 65 48 61 73 68 46 69 72 73 74 28 26 64 62 2d 3e  eHashFirst(&db->
33783 61 43 6f 6c 6c 53 65 71 29 3b 20 69 3b 20 69 3d  aCollSeq); i; i=
33784 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69  sqliteHashNext(i
33785 29 29 7b 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20  )){.    CollSeq 
33786 2a 70 43 6f 6c 6c 20 3d 20 28 43 6f 6c 6c 53 65  *pColl = (CollSe
33787 71 20 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61  q *)sqliteHashDa
33788 74 61 28 69 29 3b 0a 20 20 20 20 2f 2a 20 49 6e  ta(i);.    /* In
33789 76 6f 6b 65 20 61 6e 79 20 64 65 73 74 72 75 63  voke any destruc
3378a 74 6f 72 73 20 72 65 67 69 73 74 65 72 65 64 20  tors registered 
3378b 66 6f 72 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  for collation se
3378c 71 75 65 6e 63 65 20 75 73 65 72 20 64 61 74 61  quence user data
3378d 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 6a 3d 30  . */.    for(j=0
3378e 3b 20 6a 3c 33 3b 20 6a 2b 2b 29 7b 0a 20 20 20  ; j<3; j++){.   
3378f 20 20 20 69 66 28 20 70 43 6f 6c 6c 5b 6a 5d 2e     if( pColl[j].
33790 78 44 65 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  xDel ){.        
33791 70 43 6f 6c 6c 5b 6a 5d 2e 78 44 65 6c 28 70 43  pColl[j].xDel(pC
33792 6f 6c 6c 5b 6a 5d 2e 70 55 73 65 72 29 3b 0a 20  oll[j].pUser);. 
33793 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
33794 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
33795 62 2c 20 70 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 20  b, pColl);.  }. 
33796 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61   sqlite3HashClea
33797 72 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 29  r(&db->aCollSeq)
33798 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
33799 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
3379a 4c 45 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74  LE.  for(i=sqlit
3379b 65 48 61 73 68 46 69 72 73 74 28 26 64 62 2d 3e  eHashFirst(&db->
3379c 61 4d 6f 64 75 6c 65 29 3b 20 69 3b 20 69 3d 73  aModule); i; i=s
3379d 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69 29  qliteHashNext(i)
3379e 29 7b 0a 20 20 20 20 4d 6f 64 75 6c 65 20 2a 70  ){.    Module *p
3379f 4d 6f 64 20 3d 20 28 4d 6f 64 75 6c 65 20 2a 29  Mod = (Module *)
337a0 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69  sqliteHashData(i
337a1 29 3b 0a 20 20 20 20 69 66 28 20 70 4d 6f 64 2d  );.    if( pMod-
337a2 3e 78 44 65 73 74 72 6f 79 20 29 7b 0a 20 20 20  >xDestroy ){.   
337a3 20 20 20 70 4d 6f 64 2d 3e 78 44 65 73 74 72 6f     pMod->xDestro
337a4 79 28 70 4d 6f 64 2d 3e 70 41 75 78 29 3b 0a 20  y(pMod->pAux);. 
337a5 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
337a6 44 62 46 72 65 65 28 64 62 2c 20 70 4d 6f 64 29  DbFree(db, pMod)
337a7 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 48  ;.  }.  sqlite3H
337a8 61 73 68 43 6c 65 61 72 28 26 64 62 2d 3e 61 4d  ashClear(&db->aM
337a9 6f 64 75 6c 65 29 3b 0a 23 65 6e 64 69 66 0a 0a  odule);.#endif..
337aa 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
337ab 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29  b, SQLITE_OK, 0)
337ac 3b 20 2f 2a 20 44 65 61 6c 6c 6f 63 61 74 65 73  ; /* Deallocates
337ad 20 61 6e 79 20 63 61 63 68 65 64 20 65 72 72 6f   any cached erro
337ae 72 20 73 74 72 69 6e 67 73 2e 20 2a 2f 0a 20 20  r strings. */.  
337af 69 66 28 20 64 62 2d 3e 70 45 72 72 20 29 7b 0a  if( db->pErr ){.
337b0 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65      sqlite3Value
337b1 46 72 65 65 28 64 62 2d 3e 70 45 72 72 29 3b 0a  Free(db->pErr);.
337b2 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 43 6c 6f    }.  sqlite3Clo
337b3 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 64 62 29  seExtensions(db)
337b4 3b 0a 0a 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d  ;..  db->magic =
337b5 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52   SQLITE_MAGIC_ER
337b6 52 4f 52 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 74  ROR;..  /* The t
337b7 65 6d 70 2d 64 61 74 61 62 61 73 65 20 73 63 68  emp-database sch
337b8 65 6d 61 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ema is allocated
337b9 20 64 69 66 66 65 72 65 6e 74 6c 79 20 66 72 6f   differently fro
337ba 6d 20 74 68 65 20 6f 74 68 65 72 20 73 63 68 65  m the other sche
337bb 6d 61 0a 20 20 2a 2a 20 6f 62 6a 65 63 74 73 20  ma.  ** objects 
337bc 28 75 73 69 6e 67 20 73 71 6c 69 74 65 4d 61 6c  (using sqliteMal
337bd 6c 6f 63 28 29 20 64 69 72 65 63 74 6c 79 2c 20  loc() directly, 
337be 69 6e 73 74 65 61 64 20 6f 66 20 73 71 6c 69 74  instead of sqlit
337bf 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 29 29  e3BtreeSchema())
337c0 2e 0a 20 20 2a 2a 20 53 6f 20 69 74 20 6e 65 65  ..  ** So it nee
337c1 64 73 20 74 6f 20 62 65 20 66 72 65 65 64 20 68  ds to be freed h
337c2 65 72 65 2e 20 54 6f 64 6f 3a 20 57 68 79 20 6e  ere. Todo: Why n
337c3 6f 74 20 72 6f 6c 6c 20 74 68 65 20 74 65 6d 70  ot roll the temp
337c4 20 73 63 68 65 6d 61 20 69 6e 74 6f 0a 20 20 2a   schema into.  *
337c5 2a 20 74 68 65 20 73 61 6d 65 20 73 71 6c 69 74  * the same sqlit
337c6 65 4d 61 6c 6c 6f 63 28 29 20 61 73 20 74 68 65  eMalloc() as the
337c7 20 6f 6e 65 20 74 68 61 74 20 61 6c 6c 6f 63 61   one that alloca
337c8 74 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65  tes the database
337c9 20 0a 20 20 2a 2a 20 73 74 72 75 63 74 75 72 65   .  ** structure
337ca 3f 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ?.  */.  sqlite3
337cb 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61  DbFree(db, db->a
337cc 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 29 3b 0a  Db[1].pSchema);.
337cd 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
337ce 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
337cf 3b 0a 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20  ;.  db->magic = 
337d0 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 43 4c 4f  SQLITE_MAGIC_CLO
337d1 53 45 44 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  SED;.  sqlite3_m
337d2 75 74 65 78 5f 66 72 65 65 28 64 62 2d 3e 6d 75  utex_free(db->mu
337d3 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20  tex);.  assert( 
337d4 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f  db->lookaside.nO
337d5 75 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 46 61 69  ut==0 );  /* Fai
337d6 6c 73 20 6f 6e 20 61 20 6c 6f 6f 6b 61 73 69 64  ls on a lookasid
337d7 65 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 2a 2f  e memory leak */
337d8 0a 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61  .  if( db->looka
337d9 73 69 64 65 2e 62 4d 61 6c 6c 6f 63 65 64 20 29  side.bMalloced )
337da 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
337db 65 65 28 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65  ee(db->lookaside
337dc 2e 70 53 74 61 72 74 29 3b 0a 20 20 7d 0a 20 20  .pStart);.  }.  
337dd 73 71 6c 69 74 65 33 5f 66 72 65 65 28 64 62 29  sqlite3_free(db)
337de 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
337df 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  E_OK;.}../*.** R
337e0 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 64 61 74 61  ollback all data
337e1 62 61 73 65 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53  base files..*/.S
337e2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
337e3 69 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61  id sqlite3Rollba
337e4 63 6b 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64  ckAll(sqlite3 *d
337e5 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  b){.  int i;.  i
337e6 6e 74 20 69 6e 54 72 61 6e 73 20 3d 20 30 3b 0a  nt inTrans = 0;.
337e7 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
337e8 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
337e9 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c  >mutex) );.  sql
337ea 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
337eb 61 6c 6c 6f 63 28 29 3b 0a 20 20 66 6f 72 28 69  alloc();.  for(i
337ec 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
337ed 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d  ++){.    if( db-
337ee 3e 61 44 62 5b 69 5d 2e 70 42 74 20 29 7b 0a 20  >aDb[i].pBt ){. 
337ef 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
337f0 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 64  BtreeIsInTrans(d
337f1 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 20 29  b->aDb[i].pBt) )
337f2 7b 0a 20 20 20 20 20 20 20 20 69 6e 54 72 61 6e  {.        inTran
337f3 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  s = 1;.      }. 
337f4 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
337f5 65 52 6f 6c 6c 62 61 63 6b 28 64 62 2d 3e 61 44  eRollback(db->aD
337f6 62 5b 69 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20  b[i].pBt);.     
337f7 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 69 6e 54 72   db->aDb[i].inTr
337f8 61 6e 73 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  ans = 0;.    }. 
337f9 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62   }.  sqlite3Vtab
337fa 52 6f 6c 6c 62 61 63 6b 28 64 62 29 3b 0a 20 20  Rollback(db);.  
337fb 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
337fc 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 69 66 28  Malloc();..  if(
337fd 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54   db->flags&SQLIT
337fe 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 20  E_InternChanges 
337ff 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  ){.    sqlite3Ex
33800 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74  pirePreparedStat
33801 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20  ements(db);.    
33802 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65  sqlite3ResetInte
33803 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30  rnalSchema(db, 0
33804 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6e 79  );.  }..  /* Any
33805 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72   deferred constr
33806 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 73 20  aint violations 
33807 68 61 76 65 20 6e 6f 77 20 62 65 65 6e 20 72 65  have now been re
33808 73 6f 6c 76 65 64 2e 20 2a 2f 0a 20 20 64 62 2d  solved. */.  db-
33809 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 20 3d  >nDeferredCons =
3380a 20 30 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65   0;..  /* If one
3380b 20 68 61 73 20 62 65 65 6e 20 63 6f 6e 66 69 67   has been config
3380c 75 72 65 64 2c 20 69 6e 76 6f 6b 65 20 74 68 65  ured, invoke the
3380d 20 72 6f 6c 6c 62 61 63 6b 2d 68 6f 6f 6b 20 63   rollback-hook c
3380e 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 66 28  allback */.  if(
3380f 20 64 62 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43 61   db->xRollbackCa
33810 6c 6c 62 61 63 6b 20 26 26 20 28 69 6e 54 72 61  llback && (inTra
33811 6e 73 20 7c 7c 20 21 64 62 2d 3e 61 75 74 6f 43  ns || !db->autoC
33812 6f 6d 6d 69 74 29 20 29 7b 0a 20 20 20 20 64 62  ommit) ){.    db
33813 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62  ->xRollbackCallb
33814 61 63 6b 28 64 62 2d 3e 70 52 6f 6c 6c 62 61 63  ack(db->pRollbac
33815 6b 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  kArg);.  }.}../*
33816 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 73 74 61  .** Return a sta
33817 74 69 63 20 73 74 72 69 6e 67 20 74 68 61 74 20  tic string that 
33818 64 65 73 63 72 69 62 65 73 20 74 68 65 20 6b 69  describes the ki
33819 6e 64 20 6f 66 20 65 72 72 6f 72 20 73 70 65 63  nd of error spec
3381a 69 66 69 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20  ified in the.** 
3381b 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  argument..*/.SQL
3381c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
3381d 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 45  t char *sqlite3E
3381e 72 72 53 74 72 28 69 6e 74 20 72 63 29 7b 0a 20  rrStr(int rc){. 
3381f 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
33820 61 72 2a 20 63 6f 6e 73 74 20 61 4d 73 67 5b 5d  ar* const aMsg[]
33821 20 3d 20 7b 0a 20 20 20 20 2f 2a 20 53 51 4c 49   = {.    /* SQLI
33822 54 45 5f 4f 4b 20 20 20 20 20 20 20 20 20 20 2a  TE_OK          *
33823 2f 20 22 6e 6f 74 20 61 6e 20 65 72 72 6f 72 22  / "not an error"
33824 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
33825 45 52 52 4f 52 20 20 20 20 20 20 20 2a 2f 20 22  ERROR       */ "
33826 53 51 4c 20 6c 6f 67 69 63 20 65 72 72 6f 72 20  SQL logic error 
33827 6f 72 20 6d 69 73 73 69 6e 67 20 64 61 74 61 62  or missing datab
33828 61 73 65 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c  ase",.    /* SQL
33829 49 54 45 5f 49 4e 54 45 52 4e 41 4c 20 20 20 20  ITE_INTERNAL    
3382a 2a 2f 20 30 2c 0a 20 20 20 20 2f 2a 20 53 51 4c  */ 0,.    /* SQL
3382b 49 54 45 5f 50 45 52 4d 20 20 20 20 20 20 20 20  ITE_PERM        
3382c 2a 2f 20 22 61 63 63 65 73 73 20 70 65 72 6d 69  */ "access permi
3382d 73 73 69 6f 6e 20 64 65 6e 69 65 64 22 2c 0a 20  ssion denied",. 
3382e 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 42 4f     /* SQLITE_ABO
3382f 52 54 20 20 20 20 20 20 20 2a 2f 20 22 63 61 6c  RT       */ "cal
33830 6c 62 61 63 6b 20 72 65 71 75 65 73 74 65 64 20  lback requested 
33831 71 75 65 72 79 20 61 62 6f 72 74 22 2c 0a 20 20  query abort",.  
33832 20 20 2f 2a 20 53 51 4c 49 54 45 5f 42 55 53 59    /* SQLITE_BUSY
33833 20 20 20 20 20 20 20 20 2a 2f 20 22 64 61 74 61          */ "data
33834 62 61 73 65 20 69 73 20 6c 6f 63 6b 65 64 22 2c  base is locked",
33835 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4c  .    /* SQLITE_L
33836 4f 43 4b 45 44 20 20 20 20 20 20 2a 2f 20 22 64  OCKED      */ "d
33837 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 69 73  atabase table is
33838 20 6c 6f 63 6b 65 64 22 2c 0a 20 20 20 20 2f 2a   locked",.    /*
33839 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 20 20   SQLITE_NOMEM   
3383a 20 20 20 20 2a 2f 20 22 6f 75 74 20 6f 66 20 6d      */ "out of m
3383b 65 6d 6f 72 79 22 2c 0a 20 20 20 20 2f 2a 20 53  emory",.    /* S
3383c 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20 20  QLITE_READONLY  
3383d 20 20 2a 2f 20 22 61 74 74 65 6d 70 74 20 74 6f    */ "attempt to
3383e 20 77 72 69 74 65 20 61 20 72 65 61 64 6f 6e 6c   write a readonl
3383f 79 20 64 61 74 61 62 61 73 65 22 2c 0a 20 20 20  y database",.   
33840 20 2f 2a 20 53 51 4c 49 54 45 5f 49 4e 54 45 52   /* SQLITE_INTER
33841 52 55 50 54 20 20 20 2a 2f 20 22 69 6e 74 65 72  RUPT   */ "inter
33842 72 75 70 74 65 64 22 2c 0a 20 20 20 20 2f 2a 20  rupted",.    /* 
33843 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 20 20 20  SQLITE_IOERR    
33844 20 20 20 2a 2f 20 22 64 69 73 6b 20 49 2f 4f 20     */ "disk I/O 
33845 65 72 72 6f 72 22 2c 0a 20 20 20 20 2f 2a 20 53  error",.    /* S
33846 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 20 20  QLITE_CORRUPT   
33847 20 20 2a 2f 20 22 64 61 74 61 62 61 73 65 20 64    */ "database d
33848 69 73 6b 20 69 6d 61 67 65 20 69 73 20 6d 61 6c  isk image is mal
33849 66 6f 72 6d 65 64 22 2c 0a 20 20 20 20 2f 2a 20  formed",.    /* 
3384a 53 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 20  SQLITE_NOTFOUND 
3384b 20 20 20 2a 2f 20 30 2c 0a 20 20 20 20 2f 2a 20     */ 0,.    /* 
3384c 53 51 4c 49 54 45 5f 46 55 4c 4c 20 20 20 20 20  SQLITE_FULL     
3384d 20 20 20 2a 2f 20 22 64 61 74 61 62 61 73 65 20     */ "database 
3384e 6f 72 20 64 69 73 6b 20 69 73 20 66 75 6c 6c 22  or disk is full"
3384f 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
33850 43 41 4e 54 4f 50 45 4e 20 20 20 20 2a 2f 20 22  CANTOPEN    */ "
33851 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 64  unable to open d
33852 61 74 61 62 61 73 65 20 66 69 6c 65 22 2c 0a 20  atabase file",. 
33853 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 50 52 4f     /* SQLITE_PRO
33854 54 4f 43 4f 4c 20 20 20 20 2a 2f 20 30 2c 0a 20  TOCOL    */ 0,. 
33855 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4d 50     /* SQLITE_EMP
33856 54 59 20 20 20 20 20 20 20 2a 2f 20 22 74 61 62  TY       */ "tab
33857 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 64  le contains no d
33858 61 74 61 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c  ata",.    /* SQL
33859 49 54 45 5f 53 43 48 45 4d 41 20 20 20 20 20 20  ITE_SCHEMA      
3385a 2a 2f 20 22 64 61 74 61 62 61 73 65 20 73 63 68  */ "database sch
3385b 65 6d 61 20 68 61 73 20 63 68 61 6e 67 65 64 22  ema has changed"
3385c 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
3385d 54 4f 4f 42 49 47 20 20 20 20 20 20 2a 2f 20 22  TOOBIG      */ "
3385e 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74  string or blob t
3385f 6f 6f 20 62 69 67 22 2c 0a 20 20 20 20 2f 2a 20  oo big",.    /* 
33860 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
33861 54 20 20 2a 2f 20 22 63 6f 6e 73 74 72 61 69 6e  T  */ "constrain
33862 74 20 66 61 69 6c 65 64 22 2c 0a 20 20 20 20 2f  t failed",.    /
33863 2a 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43  * SQLITE_MISMATC
33864 48 20 20 20 20 2a 2f 20 22 64 61 74 61 74 79 70  H    */ "datatyp
33865 65 20 6d 69 73 6d 61 74 63 68 22 2c 0a 20 20 20  e mismatch",.   
33866 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55 53   /* SQLITE_MISUS
33867 45 20 20 20 20 20 20 2a 2f 20 22 6c 69 62 72 61  E      */ "libra
33868 72 79 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 65  ry routine calle
33869 64 20 6f 75 74 20 6f 66 20 73 65 71 75 65 6e 63  d out of sequenc
3386a 65 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  e",.    /* SQLIT
3386b 45 5f 4e 4f 4c 46 53 20 20 20 20 20 20 20 2a 2f  E_NOLFS       */
3386c 20 22 6c 61 72 67 65 20 66 69 6c 65 20 73 75 70   "large file sup
3386d 70 6f 72 74 20 69 73 20 64 69 73 61 62 6c 65 64  port is disabled
3386e 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45  ",.    /* SQLITE
3386f 5f 41 55 54 48 20 20 20 20 20 20 20 20 2a 2f 20  _AUTH        */ 
33870 22 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 64  "authorization d
33871 65 6e 69 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53  enied",.    /* S
33872 51 4c 49 54 45 5f 46 4f 52 4d 41 54 20 20 20 20  QLITE_FORMAT    
33873 20 20 2a 2f 20 22 61 75 78 69 6c 69 61 72 79 20    */ "auxiliary 
33874 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74 20  database format 
33875 65 72 72 6f 72 22 2c 0a 20 20 20 20 2f 2a 20 53  error",.    /* S
33876 51 4c 49 54 45 5f 52 41 4e 47 45 20 20 20 20 20  QLITE_RANGE     
33877 20 20 2a 2f 20 22 62 69 6e 64 20 6f 72 20 63 6f    */ "bind or co
33878 6c 75 6d 6e 20 69 6e 64 65 78 20 6f 75 74 20 6f  lumn index out o
33879 66 20 72 61 6e 67 65 22 2c 0a 20 20 20 20 2f 2a  f range",.    /*
3387a 20 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 20 20   SQLITE_NOTADB  
3387b 20 20 20 20 2a 2f 20 22 66 69 6c 65 20 69 73 20      */ "file is 
3387c 65 6e 63 72 79 70 74 65 64 20 6f 72 20 69 73 20  encrypted or is 
3387d 6e 6f 74 20 61 20 64 61 74 61 62 61 73 65 22 2c  not a database",
3387e 0a 20 20 7d 3b 0a 20 20 72 63 20 26 3d 20 30 78  .  };.  rc &= 0x
3387f 66 66 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53  ff;.  if( ALWAYS
33880 28 72 63 3e 3d 30 29 20 26 26 20 72 63 3c 28 69  (rc>=0) && rc<(i
33881 6e 74 29 28 73 69 7a 65 6f 66 28 61 4d 73 67 29  nt)(sizeof(aMsg)
33882 2f 73 69 7a 65 6f 66 28 61 4d 73 67 5b 30 5d 29  /sizeof(aMsg[0])
33883 29 20 26 26 20 61 4d 73 67 5b 72 63 5d 21 3d 30  ) && aMsg[rc]!=0
33884 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 61   ){.    return a
33885 4d 73 67 5b 72 63 5d 3b 0a 20 20 7d 65 6c 73 65  Msg[rc];.  }else
33886 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 22 75 6e  {.    return "un
33887 6b 6e 6f 77 6e 20 65 72 72 6f 72 22 3b 0a 20 20  known error";.  
33888 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
33889 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e  routine implemen
3388a 74 73 20 61 20 62 75 73 79 20 63 61 6c 6c 62 61  ts a busy callba
3388b 63 6b 20 74 68 61 74 20 73 6c 65 65 70 73 20 61  ck that sleeps a
3388c 6e 64 20 74 72 69 65 73 0a 2a 2a 20 61 67 61 69  nd tries.** agai
3388d 6e 20 75 6e 74 69 6c 20 61 20 74 69 6d 65 6f 75  n until a timeou
3388e 74 20 76 61 6c 75 65 20 69 73 20 72 65 61 63 68  t value is reach
3388f 65 64 2e 20 20 54 68 65 20 74 69 6d 65 6f 75 74  ed.  The timeout
33890 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 6e 20   value is.** an 
33891 69 6e 74 65 67 65 72 20 6e 75 6d 62 65 72 20 6f  integer number o
33892 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 70  f milliseconds p
33893 61 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 20  assed in as the 
33894 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e  first.** argumen
33895 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
33896 20 73 71 6c 69 74 65 44 65 66 61 75 6c 74 42 75   sqliteDefaultBu
33897 73 79 43 61 6c 6c 62 61 63 6b 28 0a 20 76 6f 69  syCallback(. voi
33898 64 20 2a 70 74 72 2c 20 20 20 20 20 20 20 20 20  d *ptr,         
33899 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
3389a 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
3389b 20 69 6e 74 20 63 6f 75 6e 74 20 20 20 20 20 20   int count      
3389c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
3389d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 61 62  ber of times tab
3389e 6c 65 20 68 61 73 20 62 65 65 6e 20 62 75 73 79  le has been busy
3389f 20 2a 2f 0a 29 7b 0a 23 69 66 20 53 51 4c 49 54   */.){.#if SQLIT
338a0 45 5f 4f 53 5f 57 49 4e 20 7c 7c 20 28 64 65 66  E_OS_WIN || (def
338a1 69 6e 65 64 28 48 41 56 45 5f 55 53 4c 45 45 50  ined(HAVE_USLEEP
338a2 29 20 26 26 20 48 41 56 45 5f 55 53 4c 45 45 50  ) && HAVE_USLEEP
338a3 29 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ).  static const
338a4 20 75 38 20 64 65 6c 61 79 73 5b 5d 20 3d 0a 20   u8 delays[] =. 
338a5 20 20 20 20 7b 20 31 2c 20 32 2c 20 35 2c 20 31      { 1, 2, 5, 1
338a6 30 2c 20 31 35 2c 20 32 30 2c 20 32 35 2c 20 32  0, 15, 20, 25, 2
338a7 35 2c 20 20 32 35 2c 20 20 35 30 2c 20 20 35 30  5,  25,  50,  50
338a8 2c 20 31 30 30 20 7d 3b 0a 20 20 73 74 61 74 69  , 100 };.  stati
338a9 63 20 63 6f 6e 73 74 20 75 38 20 74 6f 74 61 6c  c const u8 total
338aa 73 5b 5d 20 3d 0a 20 20 20 20 20 7b 20 30 2c 20  s[] =.     { 0, 
338ab 31 2c 20 33 2c 20 20 38 2c 20 31 38 2c 20 33 33  1, 3,  8, 18, 33
338ac 2c 20 35 33 2c 20 37 38 2c 20 31 30 33 2c 20 31  , 53, 78, 103, 1
338ad 32 38 2c 20 31 37 38 2c 20 32 32 38 20 7d 3b 0a  28, 178, 228 };.
338ae 23 20 64 65 66 69 6e 65 20 4e 44 45 4c 41 59 20  # define NDELAY 
338af 28 73 69 7a 65 6f 66 28 64 65 6c 61 79 73 29 2f  (sizeof(delays)/
338b0 73 69 7a 65 6f 66 28 64 65 6c 61 79 73 5b 30 5d  sizeof(delays[0]
338b1 29 29 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  )).  sqlite3 *db
338b2 20 3d 20 28 73 71 6c 69 74 65 33 20 2a 29 70 74   = (sqlite3 *)pt
338b3 72 3b 0a 20 20 69 6e 74 20 74 69 6d 65 6f 75 74  r;.  int timeout
338b4 20 3d 20 64 62 2d 3e 62 75 73 79 54 69 6d 65 6f   = db->busyTimeo
338b5 75 74 3b 0a 20 20 69 6e 74 20 64 65 6c 61 79 2c  ut;.  int delay,
338b6 20 70 72 69 6f 72 3b 0a 0a 20 20 61 73 73 65 72   prior;..  asser
338b7 74 28 20 63 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20  t( count>=0 );. 
338b8 20 69 66 28 20 63 6f 75 6e 74 20 3c 20 4e 44 45   if( count < NDE
338b9 4c 41 59 20 29 7b 0a 20 20 20 20 64 65 6c 61 79  LAY ){.    delay
338ba 20 3d 20 64 65 6c 61 79 73 5b 63 6f 75 6e 74 5d   = delays[count]
338bb 3b 0a 20 20 20 20 70 72 69 6f 72 20 3d 20 74 6f  ;.    prior = to
338bc 74 61 6c 73 5b 63 6f 75 6e 74 5d 3b 0a 20 20 7d  tals[count];.  }
338bd 65 6c 73 65 7b 0a 20 20 20 20 64 65 6c 61 79 20  else{.    delay 
338be 3d 20 64 65 6c 61 79 73 5b 4e 44 45 4c 41 59 2d  = delays[NDELAY-
338bf 31 5d 3b 0a 20 20 20 20 70 72 69 6f 72 20 3d 20  1];.    prior = 
338c0 74 6f 74 61 6c 73 5b 4e 44 45 4c 41 59 2d 31 5d  totals[NDELAY-1]
338c1 20 2b 20 64 65 6c 61 79 2a 28 63 6f 75 6e 74 2d   + delay*(count-
338c2 28 4e 44 45 4c 41 59 2d 31 29 29 3b 0a 20 20 7d  (NDELAY-1));.  }
338c3 0a 20 20 69 66 28 20 70 72 69 6f 72 20 2b 20 64  .  if( prior + d
338c4 65 6c 61 79 20 3e 20 74 69 6d 65 6f 75 74 20 29  elay > timeout )
338c5 7b 0a 20 20 20 20 64 65 6c 61 79 20 3d 20 74 69  {.    delay = ti
338c6 6d 65 6f 75 74 20 2d 20 70 72 69 6f 72 3b 0a 20  meout - prior;. 
338c7 20 20 20 69 66 28 20 64 65 6c 61 79 3c 3d 30 20     if( delay<=0 
338c8 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a  ) return 0;.  }.
338c9 20 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70    sqlite3OsSleep
338ca 28 64 62 2d 3e 70 56 66 73 2c 20 64 65 6c 61 79  (db->pVfs, delay
338cb 2a 31 30 30 30 29 3b 0a 20 20 72 65 74 75 72 6e  *1000);.  return
338cc 20 31 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69   1;.#else.  sqli
338cd 74 65 33 20 2a 64 62 20 3d 20 28 73 71 6c 69 74  te3 *db = (sqlit
338ce 65 33 20 2a 29 70 74 72 3b 0a 20 20 69 6e 74 20  e3 *)ptr;.  int 
338cf 74 69 6d 65 6f 75 74 20 3d 20 28 28 73 71 6c 69  timeout = ((sqli
338d0 74 65 33 20 2a 29 70 74 72 29 2d 3e 62 75 73 79  te3 *)ptr)->busy
338d1 54 69 6d 65 6f 75 74 3b 0a 20 20 69 66 28 20 28  Timeout;.  if( (
338d2 63 6f 75 6e 74 2b 31 29 2a 31 30 30 30 20 3e 20  count+1)*1000 > 
338d3 74 69 6d 65 6f 75 74 20 29 7b 0a 20 20 20 20 72  timeout ){.    r
338d4 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73  eturn 0;.  }.  s
338d5 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 64 62  qlite3OsSleep(db
338d6 2d 3e 70 56 66 73 2c 20 31 30 30 30 30 30 30 29  ->pVfs, 1000000)
338d7 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 65  ;.  return 1;.#e
338d8 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  ndif.}../*.** In
338d9 76 6f 6b 65 20 74 68 65 20 67 69 76 65 6e 20 62  voke the given b
338da 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a  usy handler..**.
338db 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
338dc 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61  is called when a
338dd 6e 20 6f 70 65 72 61 74 69 6f 6e 20 66 61 69 6c  n operation fail
338de 65 64 20 77 69 74 68 20 61 20 6c 6f 63 6b 2e 0a  ed with a lock..
338df 2a 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74 69  ** If this routi
338e0 6e 65 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a  ne returns non-z
338e1 65 72 6f 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73  ero, the lock is
338e2 20 72 65 74 72 69 65 64 2e 20 20 49 66 20 69 74   retried.  If it
338e3 0a 2a 2a 20 72 65 74 75 72 6e 73 20 30 2c 20 74  .** returns 0, t
338e4 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 62 6f  he operation abo
338e5 72 74 73 20 77 69 74 68 20 61 6e 20 53 51 4c 49  rts with an SQLI
338e6 54 45 5f 42 55 53 59 20 65 72 72 6f 72 2e 0a 2a  TE_BUSY error..*
338e7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
338e8 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 76 6f   int sqlite3Invo
338e9 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28 42 75  keBusyHandler(Bu
338ea 73 79 48 61 6e 64 6c 65 72 20 2a 70 29 7b 0a 20  syHandler *p){. 
338eb 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 4e   int rc;.  if( N
338ec 45 56 45 52 28 70 3d 3d 30 29 20 7c 7c 20 70 2d  EVER(p==0) || p-
338ed 3e 78 46 75 6e 63 3d 3d 30 20 7c 7c 20 70 2d 3e  >xFunc==0 || p->
338ee 6e 42 75 73 79 3c 30 20 29 20 72 65 74 75 72 6e  nBusy<0 ) return
338ef 20 30 3b 0a 20 20 72 63 20 3d 20 70 2d 3e 78 46   0;.  rc = p->xF
338f0 75 6e 63 28 70 2d 3e 70 41 72 67 2c 20 70 2d 3e  unc(p->pArg, p->
338f1 6e 42 75 73 79 29 3b 0a 20 20 69 66 28 20 72 63  nBusy);.  if( rc
338f2 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 42  ==0 ){.    p->nB
338f3 75 73 79 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73  usy = -1;.  }els
338f4 65 7b 0a 20 20 20 20 70 2d 3e 6e 42 75 73 79 2b  e{.    p->nBusy+
338f5 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  +;.  }.  return 
338f6 72 63 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  rc; .}../*.** Th
338f7 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
338f8 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63  the busy callbac
338f9 6b 20 66 6f 72 20 61 6e 20 53 71 6c 69 74 65 20  k for an Sqlite 
338fa 64 61 74 61 62 61 73 65 20 74 6f 20 74 68 65 0a  database to the.
338fb 2a 2a 20 67 69 76 65 6e 20 63 61 6c 6c 62 61 63  ** given callbac
338fc 6b 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  k function with 
338fd 74 68 65 20 67 69 76 65 6e 20 61 72 67 75 6d 65  the given argume
338fe 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  nt..*/.SQLITE_AP
338ff 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75  I int sqlite3_bu
33900 73 79 5f 68 61 6e 64 6c 65 72 28 0a 20 20 73 71  sy_handler(.  sq
33901 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 69 6e 74  lite3 *db,.  int
33902 20 28 2a 78 42 75 73 79 29 28 76 6f 69 64 2a 2c   (*xBusy)(void*,
33903 69 6e 74 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41  int),.  void *pA
33904 72 67 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  rg.){.  sqlite3_
33905 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
33906 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 62 75  mutex);.  db->bu
33907 73 79 48 61 6e 64 6c 65 72 2e 78 46 75 6e 63 20  syHandler.xFunc 
33908 3d 20 78 42 75 73 79 3b 0a 20 20 64 62 2d 3e 62  = xBusy;.  db->b
33909 75 73 79 48 61 6e 64 6c 65 72 2e 70 41 72 67 20  usyHandler.pArg 
3390a 3d 20 70 41 72 67 3b 0a 20 20 64 62 2d 3e 62 75  = pArg;.  db->bu
3390b 73 79 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20  syHandler.nBusy 
3390c 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  = 0;.  sqlite3_m
3390d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
3390e 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
3390f 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69  SQLITE_OK;.}..#i
33910 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
33911 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42  T_PROGRESS_CALLB
33912 41 43 4b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  ACK./*.** This r
33913 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20  outine sets the 
33914 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
33915 6b 20 66 6f 72 20 61 6e 20 53 71 6c 69 74 65 20  k for an Sqlite 
33916 64 61 74 61 62 61 73 65 20 74 6f 20 74 68 65 0a  database to the.
33917 2a 2a 20 67 69 76 65 6e 20 63 61 6c 6c 62 61 63  ** given callbac
33918 6b 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  k function with 
33919 74 68 65 20 67 69 76 65 6e 20 61 72 67 75 6d 65  the given argume
3391a 6e 74 2e 20 54 68 65 20 70 72 6f 67 72 65 73 73  nt. The progress
3391b 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 0a 2a   callback will.*
3391c 2a 20 62 65 20 69 6e 76 6f 6b 65 64 20 65 76 65  * be invoked eve
3391d 72 79 20 6e 4f 70 73 20 6f 70 63 6f 64 65 73 2e  ry nOps opcodes.
3391e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
3391f 6f 69 64 20 73 71 6c 69 74 65 33 5f 70 72 6f 67  oid sqlite3_prog
33920 72 65 73 73 5f 68 61 6e 64 6c 65 72 28 0a 20 20  ress_handler(.  
33921 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20  sqlite3 *db, .  
33922 69 6e 74 20 6e 4f 70 73 2c 0a 20 20 69 6e 74 20  int nOps,.  int 
33923 28 2a 78 50 72 6f 67 72 65 73 73 29 28 76 6f 69  (*xProgress)(voi
33924 64 2a 29 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41  d*), .  void *pA
33925 72 67 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  rg.){.  sqlite3_
33926 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
33927 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6e 4f  mutex);.  if( nO
33928 70 73 3e 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e  ps>0 ){.    db->
33929 78 50 72 6f 67 72 65 73 73 20 3d 20 78 50 72 6f  xProgress = xPro
3392a 67 72 65 73 73 3b 0a 20 20 20 20 64 62 2d 3e 6e  gress;.    db->n
3392b 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 6e 4f  ProgressOps = nO
3392c 70 73 3b 0a 20 20 20 20 64 62 2d 3e 70 50 72 6f  ps;.    db->pPro
3392d 67 72 65 73 73 41 72 67 20 3d 20 70 41 72 67 3b  gressArg = pArg;
3392e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62  .  }else{.    db
3392f 2d 3e 78 50 72 6f 67 72 65 73 73 20 3d 20 30 3b  ->xProgress = 0;
33930 0a 20 20 20 20 64 62 2d 3e 6e 50 72 6f 67 72 65  .    db->nProgre
33931 73 73 4f 70 73 20 3d 20 30 3b 0a 20 20 20 20 64  ssOps = 0;.    d
33932 62 2d 3e 70 50 72 6f 67 72 65 73 73 41 72 67 20  b->pProgressArg 
33933 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  = 0;.  }.  sqlit
33934 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
33935 62 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 23 65 6e  b->mutex);.}.#en
33936 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  dif.../*.** This
33937 20 72 6f 75 74 69 6e 65 20 69 6e 73 74 61 6c 6c   routine install
33938 73 20 61 20 64 65 66 61 75 6c 74 20 62 75 73 79  s a default busy
33939 20 68 61 6e 64 6c 65 72 20 74 68 61 74 20 77 61   handler that wa
3393a 69 74 73 20 66 6f 72 20 74 68 65 0a 2a 2a 20 73  its for the.** s
3393b 70 65 63 69 66 69 65 64 20 6e 75 6d 62 65 72 20  pecified number 
3393c 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20  of milliseconds 
3393d 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
3393e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50   0..*/.SQLITE_AP
3393f 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75  I int sqlite3_bu
33940 73 79 5f 74 69 6d 65 6f 75 74 28 73 71 6c 69 74  sy_timeout(sqlit
33941 65 33 20 2a 64 62 2c 20 69 6e 74 20 6d 73 29 7b  e3 *db, int ms){
33942 0a 20 20 69 66 28 20 6d 73 3e 30 20 29 7b 0a 20  .  if( ms>0 ){. 
33943 20 20 20 64 62 2d 3e 62 75 73 79 54 69 6d 65 6f     db->busyTimeo
33944 75 74 20 3d 20 6d 73 3b 0a 20 20 20 20 73 71 6c  ut = ms;.    sql
33945 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65  ite3_busy_handle
33946 72 28 64 62 2c 20 73 71 6c 69 74 65 44 65 66 61  r(db, sqliteDefa
33947 75 6c 74 42 75 73 79 43 61 6c 6c 62 61 63 6b 2c  ultBusyCallback,
33948 20 28 76 6f 69 64 2a 29 64 62 29 3b 0a 20 20 7d   (void*)db);.  }
33949 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
3394a 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 64  3_busy_handler(d
3394b 62 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  b, 0, 0);.  }.  
3394c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
3394d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 75 73 65  ;.}../*.** Cause
3394e 20 61 6e 79 20 70 65 6e 64 69 6e 67 20 6f 70 65   any pending ope
3394f 72 61 74 69 6f 6e 20 74 6f 20 73 74 6f 70 20 61  ration to stop a
33950 74 20 69 74 73 20 65 61 72 6c 69 65 73 74 20 6f  t its earliest o
33951 70 70 6f 72 74 75 6e 69 74 79 2e 0a 2a 2f 0a 53  pportunity..*/.S
33952 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
33953 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
33954 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
33955 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72   db->u1.isInterr
33956 75 70 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f  upted = 1;.}.../
33957 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
33958 6f 6e 20 69 73 20 65 78 61 63 74 6c 79 20 74 68  on is exactly th
33959 65 20 73 61 6d 65 20 61 73 20 73 71 6c 69 74 65  e same as sqlite
3395a 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
3395b 6e 28 29 2c 20 65 78 63 65 70 74 0a 2a 2a 20 74  n(), except.** t
3395c 68 61 74 20 69 74 20 69 73 20 64 65 73 69 67 6e  hat it is design
3395d 65 64 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 20  ed to be called 
3395e 62 79 20 69 6e 74 65 72 6e 61 6c 20 63 6f 64 65  by internal code
3395f 2e 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65  . The difference
33960 20 69 73 0a 2a 2a 20 74 68 61 74 20 69 66 20 61   is.** that if a
33961 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20   malloc() fails 
33962 69 6e 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  in sqlite3_creat
33963 65 5f 66 75 6e 63 74 69 6f 6e 28 29 2c 20 61 6e  e_function(), an
33964 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69   error code.** i
33965 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74  s returned and t
33966 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  he mallocFailed 
33967 66 6c 61 67 20 63 6c 65 61 72 65 64 2e 20 0a 2a  flag cleared. .*
33968 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
33969 20 69 6e 74 20 73 71 6c 69 74 65 33 43 72 65 61   int sqlite3Crea
3396a 74 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  teFunc(.  sqlite
3396b 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63  3 *db,.  const c
3396c 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61  har *zFunctionNa
3396d 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a  me,.  int nArg,.
3396e 20 20 69 6e 74 20 65 6e 63 2c 0a 20 20 76 6f 69    int enc,.  voi
3396f 64 20 2a 70 55 73 65 72 44 61 74 61 2c 0a 20 20  d *pUserData,.  
33970 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71  void (*xFunc)(sq
33971 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
33972 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
33973 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78   **),.  void (*x
33974 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f  Step)(sqlite3_co
33975 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
33976 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20  e3_value **),.  
33977 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73  void (*xFinal)(s
33978 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
33979 0a 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70  .){.  FuncDef *p
3397a 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 0a  ;.  int nName;..
3397b 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
3397c 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
3397d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28  >mutex) );.  if(
3397e 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 3d 3d   zFunctionName==
3397f 30 20 7c 7c 0a 20 20 20 20 20 20 28 78 46 75 6e  0 ||.      (xFun
33980 63 20 26 26 20 28 78 46 69 6e 61 6c 20 7c 7c 20  c && (xFinal || 
33981 78 53 74 65 70 29 29 20 7c 7c 20 0a 20 20 20 20  xStep)) || .    
33982 20 20 28 21 78 46 75 6e 63 20 26 26 20 28 78 46    (!xFunc && (xF
33983 69 6e 61 6c 20 26 26 20 21 78 53 74 65 70 29 29  inal && !xStep))
33984 20 7c 7c 0a 20 20 20 20 20 20 28 21 78 46 75 6e   ||.      (!xFun
33985 63 20 26 26 20 28 21 78 46 69 6e 61 6c 20 26 26  c && (!xFinal &&
33986 20 78 53 74 65 70 29 29 20 7c 7c 0a 20 20 20 20   xStep)) ||.    
33987 20 20 28 6e 41 72 67 3c 2d 31 20 7c 7c 20 6e 41    (nArg<-1 || nA
33988 72 67 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55  rg>SQLITE_MAX_FU
33989 4e 43 54 49 4f 4e 5f 41 52 47 29 20 7c 7c 0a 20  NCTION_ARG) ||. 
3398a 20 20 20 20 20 28 32 35 35 3c 28 6e 4e 61 6d 65       (255<(nName
3398b 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
3398c 33 30 28 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d  30( zFunctionNam
3398d 65 29 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75  e))) ){.    retu
3398e 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
3398f 3b 0a 20 20 7d 0a 20 20 0a 23 69 66 6e 64 65 66  ;.  }.  .#ifndef
33990 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
33991 31 36 0a 20 20 2f 2a 20 49 66 20 53 51 4c 49 54  16.  /* If SQLIT
33992 45 5f 55 54 46 31 36 20 69 73 20 73 70 65 63 69  E_UTF16 is speci
33993 66 69 65 64 20 61 73 20 74 68 65 20 65 6e 63 6f  fied as the enco
33994 64 69 6e 67 20 74 79 70 65 2c 20 74 72 61 6e 73  ding type, trans
33995 66 6f 72 6d 20 74 68 69 73 0a 20 20 2a 2a 20 74  form this.  ** t
33996 6f 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f  o one of SQLITE_
33997 55 54 46 31 36 4c 45 20 6f 72 20 53 51 4c 49 54  UTF16LE or SQLIT
33998 45 5f 55 54 46 31 36 42 45 20 75 73 69 6e 67 20  E_UTF16BE using 
33999 74 68 65 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f  the.  ** SQLITE_
3399a 55 54 46 31 36 4e 41 54 49 56 45 20 6d 61 63 72  UTF16NATIVE macr
3399b 6f 2e 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20  o. SQLITE_UTF16 
3399c 69 73 20 6e 6f 74 20 75 73 65 64 20 69 6e 74 65  is not used inte
3399d 72 6e 61 6c 6c 79 2e 0a 20 20 2a 2a 0a 20 20 2a  rnally..  **.  *
3399e 2a 20 49 66 20 53 51 4c 49 54 45 5f 41 4e 59 20  * If SQLITE_ANY 
3399f 69 73 20 73 70 65 63 69 66 69 65 64 2c 20 61 64  is specified, ad
339a0 64 20 74 68 72 65 65 20 76 65 72 73 69 6f 6e 73  d three versions
339a1 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
339a2 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 68 61 73  .  ** to the has
339a3 68 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  h table..  */.  
339a4 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f  if( enc==SQLITE_
339a5 55 54 46 31 36 20 29 7b 0a 20 20 20 20 65 6e 63  UTF16 ){.    enc
339a6 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e   = SQLITE_UTF16N
339a7 41 54 49 56 45 3b 0a 20 20 7d 65 6c 73 65 20 69  ATIVE;.  }else i
339a8 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 41  f( enc==SQLITE_A
339a9 4e 59 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63  NY ){.    int rc
339aa 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
339ab 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c  e3CreateFunc(db,
339ac 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20   zFunctionName, 
339ad 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46  nArg, SQLITE_UTF
339ae 38 2c 0a 20 20 20 20 20 20 20 20 20 70 55 73 65  8,.         pUse
339af 72 44 61 74 61 2c 20 78 46 75 6e 63 2c 20 78 53  rData, xFunc, xS
339b0 74 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20 20  tep, xFinal);.  
339b1 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
339b2 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  _OK ){.      rc 
339b3 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46  = sqlite3CreateF
339b4 75 6e 63 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f  unc(db, zFunctio
339b5 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 53 51 4c  nName, nArg, SQL
339b6 49 54 45 5f 55 54 46 31 36 4c 45 2c 0a 20 20 20  ITE_UTF16LE,.   
339b7 20 20 20 20 20 20 20 70 55 73 65 72 44 61 74 61         pUserData
339b8 2c 20 78 46 75 6e 63 2c 20 78 53 74 65 70 2c 20  , xFunc, xStep, 
339b9 78 46 69 6e 61 6c 29 3b 0a 20 20 20 20 7d 0a 20  xFinal);.    }. 
339ba 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
339bb 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
339bc 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
339bd 20 20 20 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f     enc = SQLITE_
339be 55 54 46 31 36 42 45 3b 0a 20 20 7d 0a 23 65 6c  UTF16BE;.  }.#el
339bf 73 65 0a 20 20 65 6e 63 20 3d 20 53 51 4c 49 54  se.  enc = SQLIT
339c0 45 5f 55 54 46 38 3b 0a 23 65 6e 64 69 66 0a 20  E_UTF8;.#endif. 
339c1 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20   .  /* Check if 
339c2 61 6e 20 65 78 69 73 74 69 6e 67 20 66 75 6e 63  an existing func
339c3 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 6f 76  tion is being ov
339c4 65 72 72 69 64 64 65 6e 20 6f 72 20 64 65 6c 65  erridden or dele
339c5 74 65 64 2e 20 49 66 20 73 6f 2c 0a 20 20 2a 2a  ted. If so,.  **
339c6 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 61   and there are a
339c7 63 74 69 76 65 20 56 4d 73 2c 20 74 68 65 6e 20  ctive VMs, then 
339c8 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
339c9 53 59 2e 20 49 66 20 61 20 66 75 6e 63 74 69 6f  SY. If a functio
339ca 6e 0a 20 20 2a 2a 20 69 73 20 62 65 69 6e 67 20  n.  ** is being 
339cb 6f 76 65 72 72 69 64 64 65 6e 2f 64 65 6c 65 74  overridden/delet
339cc 65 64 20 62 75 74 20 74 68 65 72 65 20 61 72 65  ed but there are
339cd 20 6e 6f 20 61 63 74 69 76 65 20 56 4d 73 2c 20   no active VMs, 
339ce 61 6c 6c 6f 77 20 74 68 65 0a 20 20 2a 2a 20 6f  allow the.  ** o
339cf 70 65 72 61 74 69 6f 6e 20 74 6f 20 63 6f 6e 74  peration to cont
339d0 69 6e 75 65 20 62 75 74 20 69 6e 76 61 6c 69 64  inue but invalid
339d1 61 74 65 20 61 6c 6c 20 70 72 65 63 6f 6d 70 69  ate all precompi
339d2 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  led statements..
339d3 20 20 2a 2f 0a 20 20 70 20 3d 20 73 71 6c 69 74    */.  p = sqlit
339d4 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64  e3FindFunction(d
339d5 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65  b, zFunctionName
339d6 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 28  , nName, nArg, (
339d7 75 38 29 65 6e 63 2c 20 30 29 3b 0a 20 20 69 66  u8)enc, 0);.  if
339d8 28 20 70 20 26 26 20 70 2d 3e 69 50 72 65 66 45  ( p && p->iPrefE
339d9 6e 63 3d 3d 65 6e 63 20 26 26 20 70 2d 3e 6e 41  nc==enc && p->nA
339da 72 67 3d 3d 6e 41 72 67 20 29 7b 0a 20 20 20 20  rg==nArg ){.    
339db 69 66 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64  if( db->activeVd
339dc 62 65 43 6e 74 20 29 7b 0a 20 20 20 20 20 20 73  beCnt ){.      s
339dd 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
339de 53 51 4c 49 54 45 5f 42 55 53 59 2c 20 0a 20 20  SQLITE_BUSY, .  
339df 20 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f        "unable to
339e0 20 64 65 6c 65 74 65 2f 6d 6f 64 69 66 79 20 75   delete/modify u
339e1 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 75 65  ser-function due
339e2 20 74 6f 20 61 63 74 69 76 65 20 73 74 61 74 65   to active state
339e3 6d 65 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 61  ments");.      a
339e4 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c  ssert( !db->mall
339e5 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20  ocFailed );.    
339e6 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
339e7 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  BUSY;.    }else{
339e8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
339e9 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74  pirePreparedStat
339ea 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20  ements(db);.    
339eb 7d 0a 20 20 7d 0a 0a 20 20 70 20 3d 20 73 71 6c  }.  }..  p = sql
339ec 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e  ite3FindFunction
339ed 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61  (db, zFunctionNa
339ee 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c  me, nName, nArg,
339ef 20 28 75 38 29 65 6e 63 2c 20 31 29 3b 0a 20 20   (u8)enc, 1);.  
339f0 61 73 73 65 72 74 28 70 20 7c 7c 20 64 62 2d 3e  assert(p || db->
339f1 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 20  mallocFailed);. 
339f2 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72   if( !p ){.    r
339f3 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
339f4 45 4d 3b 0a 20 20 7d 0a 20 20 70 2d 3e 66 6c 61  EM;.  }.  p->fla
339f5 67 73 20 3d 20 30 3b 0a 20 20 70 2d 3e 78 46 75  gs = 0;.  p->xFu
339f6 6e 63 20 3d 20 78 46 75 6e 63 3b 0a 20 20 70 2d  nc = xFunc;.  p-
339f7 3e 78 53 74 65 70 20 3d 20 78 53 74 65 70 3b 0a  >xStep = xStep;.
339f8 20 20 70 2d 3e 78 46 69 6e 61 6c 69 7a 65 20 3d    p->xFinalize =
339f9 20 78 46 69 6e 61 6c 3b 0a 20 20 70 2d 3e 70 55   xFinal;.  p->pU
339fa 73 65 72 44 61 74 61 20 3d 20 70 55 73 65 72 44  serData = pUserD
339fb 61 74 61 3b 0a 20 20 70 2d 3e 6e 41 72 67 20 3d  ata;.  p->nArg =
339fc 20 28 75 31 36 29 6e 41 72 67 3b 0a 20 20 72 65   (u16)nArg;.  re
339fd 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
339fe 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  }../*.** Create 
339ff 6e 65 77 20 75 73 65 72 20 66 75 6e 63 74 69 6f  new user functio
33a00 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ns..*/.SQLITE_AP
33a01 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72  I int sqlite3_cr
33a02 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20  eate_function(. 
33a03 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20   sqlite3 *db,.  
33a04 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e  const char *zFun
33a05 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74  ctionName,.  int
33a06 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 6e 63   nArg,.  int enc
33a07 2c 0a 20 20 76 6f 69 64 20 2a 70 2c 0a 20 20 76  ,.  void *p,.  v
33a08 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c  oid (*xFunc)(sql
33a09 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
33a0a 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  t,sqlite3_value 
33a0b 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53  **),.  void (*xS
33a0c 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  tep)(sqlite3_con
33a0d 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
33a0e 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76  3_value **),.  v
33a0f 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71  oid (*xFinal)(sq
33a10 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a  lite3_context*).
33a11 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
33a12 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
33a13 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
33a14 20 72 63 20 3d 20 73 71 6c 69 74 65 33 43 72 65   rc = sqlite3Cre
33a15 61 74 65 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e  ateFunc(db, zFun
33a16 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c  ctionName, nArg,
33a17 20 65 6e 63 2c 20 70 2c 20 78 46 75 6e 63 2c 20   enc, p, xFunc, 
33a18 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a  xStep, xFinal);.
33a19 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
33a1a 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20  iExit(db, rc);. 
33a1b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
33a1c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
33a1d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
33a1e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
33a1f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54  OMIT_UTF16.SQLIT
33a20 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
33a21 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
33a22 6e 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  n16(.  sqlite3 *
33a23 64 62 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  db,.  const void
33a24 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c   *zFunctionName,
33a25 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69  .  int nArg,.  i
33a26 6e 74 20 65 54 65 78 74 52 65 70 2c 0a 20 20 76  nt eTextRep,.  v
33a27 6f 69 64 20 2a 70 2c 0a 20 20 76 6f 69 64 20 28  oid *p,.  void (
33a28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f  *xFunc)(sqlite3_
33a29 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
33a2a 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20  ite3_value**),. 
33a2b 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73   void (*xStep)(s
33a2c 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
33a2d 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
33a2e 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78  e**),.  void (*x
33a2f 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63  Final)(sqlite3_c
33a30 6f 6e 74 65 78 74 2a 29 0a 29 7b 0a 20 20 69 6e  ontext*).){.  in
33a31 74 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 46  t rc;.  char *zF
33a32 75 6e 63 38 3b 0a 20 20 73 71 6c 69 74 65 33 5f  unc8;.  sqlite3_
33a33 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
33a34 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74  mutex);.  assert
33a35 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
33a36 6c 65 64 20 29 3b 0a 20 20 7a 46 75 6e 63 38 20  led );.  zFunc8 
33a37 3d 20 73 71 6c 69 74 65 33 55 74 66 31 36 74 6f  = sqlite3Utf16to
33a38 38 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e  8(db, zFunctionN
33a39 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 72 63 20 3d  ame, -1);.  rc =
33a3a 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75   sqlite3CreateFu
33a3b 6e 63 28 64 62 2c 20 7a 46 75 6e 63 38 2c 20 6e  nc(db, zFunc8, n
33a3c 41 72 67 2c 20 65 54 65 78 74 52 65 70 2c 20 70  Arg, eTextRep, p
33a3d 2c 20 78 46 75 6e 63 2c 20 78 53 74 65 70 2c 20  , xFunc, xStep, 
33a3e 78 46 69 6e 61 6c 29 3b 0a 20 20 73 71 6c 69 74  xFinal);.  sqlit
33a3f 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 46 75  e3DbFree(db, zFu
33a40 6e 63 38 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  nc8);.  rc = sql
33a41 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
33a42 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  rc);.  sqlite3_m
33a43 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
33a44 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
33a45 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f  rc;.}.#endif.../
33a46 2a 0a 2a 2a 20 44 65 63 6c 61 72 65 20 74 68 61  *.** Declare tha
33a47 74 20 61 20 66 75 6e 63 74 69 6f 6e 20 68 61 73  t a function has
33a48 20 62 65 65 6e 20 6f 76 65 72 6c 6f 61 64 65 64   been overloaded
33a49 20 62 79 20 61 20 76 69 72 74 75 61 6c 20 74 61   by a virtual ta
33a4a 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ble..**.** If th
33a4b 65 20 66 75 6e 63 74 69 6f 6e 20 61 6c 72 65 61  e function alrea
33a4c 64 79 20 65 78 69 73 74 73 20 61 73 20 61 20 72  dy exists as a r
33a4d 65 67 75 6c 61 72 20 67 6c 6f 62 61 6c 20 66 75  egular global fu
33a4e 6e 63 74 69 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20  nction, then.** 
33a4f 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
33a50 61 20 6e 6f 2d 6f 70 2e 20 20 49 66 20 74 68 65  a no-op.  If the
33a51 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e   function does n
33a52 6f 74 20 65 78 69 73 74 2c 20 74 68 65 6e 20 63  ot exist, then c
33a53 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6f  reate.** a new o
33a54 6e 65 20 74 68 61 74 20 61 6c 77 61 79 73 20 74  ne that always t
33a55 68 72 6f 77 73 20 61 20 72 75 6e 2d 74 69 6d 65  hrows a run-time
33a56 20 65 72 72 6f 72 2e 20 20 0a 2a 2a 0a 2a 2a 20   error.  .**.** 
33a57 57 68 65 6e 20 76 69 72 74 75 61 6c 20 74 61 62  When virtual tab
33a58 6c 65 73 20 69 6e 74 65 6e 64 20 74 6f 20 70 72  les intend to pr
33a59 6f 76 69 64 65 20 61 6e 20 6f 76 65 72 6c 6f 61  ovide an overloa
33a5a 64 65 64 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68  ded function, th
33a5b 65 79 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c  ey.** should cal
33a5c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74  l this routine t
33a5d 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  o make sure the 
33a5e 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 20  global function 
33a5f 65 78 69 73 74 73 2e 0a 2a 2a 20 41 20 67 6c 6f  exists..** A glo
33a60 62 61 6c 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73  bal function mus
33a61 74 20 65 78 69 73 74 20 69 6e 20 6f 72 64 65 72  t exist in order
33a62 20 66 6f 72 20 6e 61 6d 65 20 72 65 73 6f 6c 75   for name resolu
33a63 74 69 6f 6e 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20  tion to work.** 
33a64 70 72 6f 70 65 72 6c 79 2e 0a 2a 2f 0a 53 51 4c  properly..*/.SQL
33a65 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
33a66 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e  te3_overload_fun
33a67 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33  ction(.  sqlite3
33a68 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68   *db,.  const ch
33a69 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74  ar *zName,.  int
33a6a 20 6e 41 72 67 0a 29 7b 0a 20 20 69 6e 74 20 6e   nArg.){.  int n
33a6b 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74  Name = sqlite3St
33a6c 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20  rlen30(zName);. 
33a6d 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74   int rc;.  sqlit
33a6e 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
33a6f 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28  b->mutex);.  if(
33a70 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63   sqlite3FindFunc
33a71 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  tion(db, zName, 
33a72 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 53 51 4c  nName, nArg, SQL
33a73 49 54 45 5f 55 54 46 38 2c 20 30 29 3d 3d 30 20  ITE_UTF8, 0)==0 
33a74 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72  ){.    sqlite3Cr
33a75 65 61 74 65 46 75 6e 63 28 64 62 2c 20 7a 4e 61  eateFunc(db, zNa
33a76 6d 65 2c 20 6e 41 72 67 2c 20 53 51 4c 49 54 45  me, nArg, SQLITE
33a77 5f 55 54 46 38 2c 0a 20 20 20 20 20 20 20 20 20  _UTF8,.         
33a78 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
33a79 73 71 6c 69 74 65 33 49 6e 76 61 6c 69 64 46 75  sqlite3InvalidFu
33a7a 6e 63 74 69 6f 6e 2c 20 30 2c 20 30 29 3b 0a 20  nction, 0, 0);. 
33a7b 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65   }.  rc = sqlite
33a7c 33 41 70 69 45 78 69 74 28 64 62 2c 20 53 51 4c  3ApiExit(db, SQL
33a7d 49 54 45 5f 4f 4b 29 3b 0a 20 20 73 71 6c 69 74  ITE_OK);.  sqlit
33a7e 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
33a7f 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
33a80 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64  urn rc;.}..#ifnd
33a81 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
33a82 52 41 43 45 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73  RACE./*.** Regis
33a83 74 65 72 20 61 20 74 72 61 63 65 20 66 75 6e 63  ter a trace func
33a84 74 69 6f 6e 2e 20 20 54 68 65 20 70 41 72 67 20  tion.  The pArg 
33a85 66 72 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75  from the previou
33a86 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 20 74  sly registered t
33a87 72 61 63 65 0a 2a 2a 20 69 73 20 72 65 74 75 72  race.** is retur
33a88 6e 65 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 20 4e  ned.  .**.** A N
33a89 55 4c 4c 20 74 72 61 63 65 20 66 75 6e 63 74 69  ULL trace functi
33a8a 6f 6e 20 6d 65 61 6e 73 20 74 68 61 74 20 6e 6f  on means that no
33a8b 20 74 72 61 63 69 6e 67 20 69 73 20 65 78 65 63   tracing is exec
33a8c 75 74 65 73 2e 20 20 41 20 6e 6f 6e 2d 4e 55 4c  utes.  A non-NUL
33a8d 4c 0a 2a 2a 20 74 72 61 63 65 20 69 73 20 61 20  L.** trace is a 
33a8e 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e  pointer to a fun
33a8f 63 74 69 6f 6e 20 74 68 61 74 20 69 73 20 69 6e  ction that is in
33a90 76 6f 6b 65 64 20 61 74 20 74 68 65 20 73 74 61  voked at the sta
33a91 72 74 20 6f 66 20 65 61 63 68 0a 2a 2a 20 53 51  rt of each.** SQ
33a92 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a  L statement..*/.
33a93 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
33a94 2a 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73  *sqlite3_trace(s
33a95 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64  qlite3 *db, void
33a96 20 28 2a 78 54 72 61 63 65 29 28 76 6f 69 64 2a   (*xTrace)(void*
33a97 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c 20 76  ,const char*), v
33a98 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 76 6f  oid *pArg){.  vo
33a99 69 64 20 2a 70 4f 6c 64 3b 0a 20 20 73 71 6c 69  id *pOld;.  sqli
33a9a 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
33a9b 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 4f  db->mutex);.  pO
33a9c 6c 64 20 3d 20 64 62 2d 3e 70 54 72 61 63 65 41  ld = db->pTraceA
33a9d 72 67 3b 0a 20 20 64 62 2d 3e 78 54 72 61 63 65  rg;.  db->xTrace
33a9e 20 3d 20 78 54 72 61 63 65 3b 0a 20 20 64 62 2d   = xTrace;.  db-
33a9f 3e 70 54 72 61 63 65 41 72 67 20 3d 20 70 41 72  >pTraceArg = pAr
33aa0 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  g;.  sqlite3_mut
33aa1 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
33aa2 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4f  ex);.  return pO
33aa3 6c 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52 65 67 69  ld;.}./*.** Regi
33aa4 73 74 65 72 20 61 20 70 72 6f 66 69 6c 65 20 66  ster a profile f
33aa5 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 70 41  unction.  The pA
33aa6 72 67 20 66 72 6f 6d 20 74 68 65 20 70 72 65 76  rg from the prev
33aa7 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65  iously registere
33aa8 64 20 0a 2a 2a 20 70 72 6f 66 69 6c 65 20 66 75  d .** profile fu
33aa9 6e 63 74 69 6f 6e 20 69 73 20 72 65 74 75 72 6e  nction is return
33aaa 65 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 20 4e 55  ed.  .**.** A NU
33aab 4c 4c 20 70 72 6f 66 69 6c 65 20 66 75 6e 63 74  LL profile funct
33aac 69 6f 6e 20 6d 65 61 6e 73 20 74 68 61 74 20 6e  ion means that n
33aad 6f 20 70 72 6f 66 69 6c 69 6e 67 20 69 73 20 65  o profiling is e
33aae 78 65 63 75 74 65 73 2e 20 20 41 20 6e 6f 6e 2d  xecutes.  A non-
33aaf 4e 55 4c 4c 0a 2a 2a 20 70 72 6f 66 69 6c 65 20  NULL.** profile 
33ab0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
33ab1 61 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  a function that 
33ab2 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 20 74 68  is invoked at th
33ab3 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 0a  e conclusion of.
33ab4 2a 2a 20 65 61 63 68 20 53 51 4c 20 73 74 61 74  ** each SQL stat
33ab5 65 6d 65 6e 74 20 74 68 61 74 20 69 73 20 72 75  ement that is ru
33ab6 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  n..*/.SQLITE_API
33ab7 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 70   void *sqlite3_p
33ab8 72 6f 66 69 6c 65 28 0a 20 20 73 71 6c 69 74 65  rofile(.  sqlite
33ab9 33 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20 28 2a  3 *db,.  void (*
33aba 78 50 72 6f 66 69 6c 65 29 28 76 6f 69 64 2a 2c  xProfile)(void*,
33abb 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69  const char*,sqli
33abc 74 65 5f 75 69 6e 74 36 34 29 2c 0a 20 20 76 6f  te_uint64),.  vo
33abd 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 76 6f  id *pArg.){.  vo
33abe 69 64 20 2a 70 4f 6c 64 3b 0a 20 20 73 71 6c 69  id *pOld;.  sqli
33abf 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
33ac0 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 4f  db->mutex);.  pO
33ac1 6c 64 20 3d 20 64 62 2d 3e 70 50 72 6f 66 69 6c  ld = db->pProfil
33ac2 65 41 72 67 3b 0a 20 20 64 62 2d 3e 78 50 72 6f  eArg;.  db->xPro
33ac3 66 69 6c 65 20 3d 20 78 50 72 6f 66 69 6c 65 3b  file = xProfile;
33ac4 0a 20 20 64 62 2d 3e 70 50 72 6f 66 69 6c 65 41  .  db->pProfileA
33ac5 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c  rg = pArg;.  sql
33ac6 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
33ac7 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
33ac8 65 74 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 23 65  eturn pOld;.}.#e
33ac9 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
33aca 4d 49 54 5f 54 52 41 43 45 20 2a 2f 0a 0a 2f 2a  MIT_TRACE */../*
33acb 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20  ** EXPERIMENTAL 
33acc 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74  ***.**.** Regist
33acd 65 72 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 6f  er a function to
33ace 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e   be invoked when
33acf 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63   a transaction c
33ad0 6f 6d 6d 65 6e 74 73 2e 0a 2a 2a 20 49 66 20 74  omments..** If t
33ad1 68 65 20 69 6e 76 6f 6b 65 64 20 66 75 6e 63 74  he invoked funct
33ad2 69 6f 6e 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d  ion returns non-
33ad3 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 63  zero, then the c
33ad4 6f 6d 6d 69 74 20 62 65 63 6f 6d 65 73 20 61 0a  ommit becomes a.
33ad5 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a  ** rollback..*/.
33ad6 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
33ad7 2a 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f  *sqlite3_commit_
33ad8 68 6f 6f 6b 28 0a 20 20 73 71 6c 69 74 65 33 20  hook(.  sqlite3 
33ad9 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
33ada 20 20 2f 2a 20 41 74 74 61 63 68 20 74 68 65 20    /* Attach the 
33adb 68 6f 6f 6b 20 74 6f 20 74 68 69 73 20 64 61 74  hook to this dat
33adc 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 28  abase */.  int (
33add 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64  *xCallback)(void
33ade 2a 29 2c 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e  *),  /* Function
33adf 20 74 6f 20 69 6e 76 6f 6b 65 20 6f 6e 20 65 61   to invoke on ea
33ae0 63 68 20 63 6f 6d 6d 69 74 20 2a 2f 0a 20 20 76  ch commit */.  v
33ae1 6f 69 64 20 2a 70 41 72 67 20 20 20 20 20 20 20  oid *pArg       
33ae2 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75           /* Argu
33ae3 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63  ment to the func
33ae4 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 76 6f 69  tion */.){.  voi
33ae5 64 20 2a 70 4f 6c 64 3b 0a 20 20 73 71 6c 69 74  d *pOld;.  sqlit
33ae6 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
33ae7 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 4f 6c  b->mutex);.  pOl
33ae8 64 20 3d 20 64 62 2d 3e 70 43 6f 6d 6d 69 74 41  d = db->pCommitA
33ae9 72 67 3b 0a 20 20 64 62 2d 3e 78 43 6f 6d 6d 69  rg;.  db->xCommi
33aea 74 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c  tCallback = xCal
33aeb 6c 62 61 63 6b 3b 0a 20 20 64 62 2d 3e 70 43 6f  lback;.  db->pCo
33aec 6d 6d 69 74 41 72 67 20 3d 20 70 41 72 67 3b 0a  mmitArg = pArg;.
33aed 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
33aee 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
33aef 3b 0a 20 20 72 65 74 75 72 6e 20 70 4f 6c 64 3b  ;.  return pOld;
33af0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74  .}../*.** Regist
33af1 65 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f  er a callback to
33af2 20 62 65 20 69 6e 76 6f 6b 65 64 20 65 61 63 68   be invoked each
33af3 20 74 69 6d 65 20 61 20 72 6f 77 20 69 73 20 75   time a row is u
33af4 70 64 61 74 65 64 2c 0a 2a 2a 20 69 6e 73 65 72  pdated,.** inser
33af5 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 20 75  ted or deleted u
33af6 73 69 6e 67 20 74 68 69 73 20 64 61 74 61 62 61  sing this databa
33af7 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a  se connection..*
33af8 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
33af9 64 20 2a 73 71 6c 69 74 65 33 5f 75 70 64 61 74  d *sqlite3_updat
33afa 65 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c 69 74 65  e_hook(.  sqlite
33afb 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
33afc 20 20 20 20 2f 2a 20 41 74 74 61 63 68 20 74 68      /* Attach th
33afd 65 20 68 6f 6f 6b 20 74 6f 20 74 68 69 73 20 64  e hook to this d
33afe 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 76 6f 69  atabase */.  voi
33aff 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76  d (*xCallback)(v
33b00 6f 69 64 2a 2c 69 6e 74 2c 63 68 61 72 20 63 6f  oid*,int,char co
33b01 6e 73 74 20 2a 2c 63 68 61 72 20 63 6f 6e 73 74  nst *,char const
33b02 20 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29   *,sqlite_int64)
33b03 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 20 20  ,.  void *pArg  
33b04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
33b05 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   Argument to the
33b06 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a   function */.){.
33b07 20 20 76 6f 69 64 20 2a 70 52 65 74 3b 0a 20 20    void *pRet;.  
33b08 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
33b09 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
33b0a 20 20 70 52 65 74 20 3d 20 64 62 2d 3e 70 55 70    pRet = db->pUp
33b0b 64 61 74 65 41 72 67 3b 0a 20 20 64 62 2d 3e 78  dateArg;.  db->x
33b0c 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 3d  UpdateCallback =
33b0d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 64 62   xCallback;.  db
33b0e 2d 3e 70 55 70 64 61 74 65 41 72 67 20 3d 20 70  ->pUpdateArg = p
33b0f 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  Arg;.  sqlite3_m
33b10 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
33b11 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
33b12 70 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  pRet;.}../*.** R
33b13 65 67 69 73 74 65 72 20 61 20 63 61 6c 6c 62 61  egister a callba
33b14 63 6b 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64  ck to be invoked
33b15 20 65 61 63 68 20 74 69 6d 65 20 61 20 74 72 61   each time a tra
33b16 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c  nsaction is roll
33b17 65 64 0a 2a 2a 20 62 61 63 6b 20 62 79 20 74 68  ed.** back by th
33b18 69 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  is database conn
33b19 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ection..*/.SQLIT
33b1a 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
33b1b 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f  te3_rollback_hoo
33b1c 6b 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  k(.  sqlite3 *db
33b1d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
33b1e 2a 20 41 74 74 61 63 68 20 74 68 65 20 68 6f 6f  * Attach the hoo
33b1f 6b 20 74 6f 20 74 68 69 73 20 64 61 74 61 62 61  k to this databa
33b20 73 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  se */.  void (*x
33b21 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29  Callback)(void*)
33b22 2c 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 75  , /* Callback fu
33b23 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64  nction */.  void
33b24 20 2a 70 41 72 67 20 20 20 20 20 20 20 20 20 20   *pArg          
33b25 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
33b26 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f  t to the functio
33b27 6e 20 2a 2f 0a 29 7b 0a 20 20 76 6f 69 64 20 2a  n */.){.  void *
33b28 70 52 65 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f  pRet;.  sqlite3_
33b29 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
33b2a 6d 75 74 65 78 29 3b 0a 20 20 70 52 65 74 20 3d  mutex);.  pRet =
33b2b 20 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41 72   db->pRollbackAr
33b2c 67 3b 0a 20 20 64 62 2d 3e 78 52 6f 6c 6c 62 61  g;.  db->xRollba
33b2d 63 6b 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61  ckCallback = xCa
33b2e 6c 6c 62 61 63 6b 3b 0a 20 20 64 62 2d 3e 70 52  llback;.  db->pR
33b2f 6f 6c 6c 62 61 63 6b 41 72 67 20 3d 20 70 41 72  ollbackArg = pAr
33b30 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  g;.  sqlite3_mut
33b31 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
33b32 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52  ex);.  return pR
33b33 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  et;.}../*.** Thi
33b34 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
33b35 6e 73 20 74 72 75 65 20 69 66 20 6d 61 69 6e 2d  ns true if main-
33b36 6d 65 6d 6f 72 79 20 73 68 6f 75 6c 64 20 62 65  memory should be
33b37 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66   used instead of
33b38 0a 2a 2a 20 61 20 74 65 6d 70 6f 72 61 72 79 20  .** a temporary 
33b39 66 69 6c 65 20 66 6f 72 20 74 72 61 6e 73 69 65  file for transie
33b3a 6e 74 20 70 61 67 65 72 20 66 69 6c 65 73 20 61  nt pager files a
33b3b 6e 64 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75  nd statement jou
33b3c 72 6e 61 6c 73 2e 0a 2a 2a 20 54 68 65 20 76 61  rnals..** The va
33b3d 6c 75 65 20 72 65 74 75 72 6e 65 64 20 64 65 70  lue returned dep
33b3e 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75  ends on the valu
33b3f 65 20 6f 66 20 64 62 2d 3e 74 65 6d 70 5f 73 74  e of db->temp_st
33b40 6f 72 65 20 28 72 75 6e 74 69 6d 65 0a 2a 2a 20  ore (runtime.** 
33b41 70 61 72 61 6d 65 74 65 72 29 20 61 6e 64 20 74  parameter) and t
33b42 68 65 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20  he compile time 
33b43 76 61 6c 75 65 20 6f 66 20 53 51 4c 49 54 45 5f  value of SQLITE_
33b44 54 45 4d 50 5f 53 54 4f 52 45 2e 20 54 68 65 0a  TEMP_STORE. The.
33b45 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62  ** following tab
33b46 6c 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65  le describes the
33b47 20 72 65 6c 61 74 69 6f 6e 73 68 69 70 20 62 65   relationship be
33b48 74 77 65 65 6e 20 74 68 65 73 65 20 74 77 6f 20  tween these two 
33b49 76 61 6c 75 65 73 0a 2a 2a 20 61 6e 64 20 74 68  values.** and th
33b4a 69 73 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74  is functions ret
33b4b 75 72 6e 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a  urn value..**.**
33b4c 20 20 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53     SQLITE_TEMP_S
33b4d 54 4f 52 45 20 20 20 20 20 64 62 2d 3e 74 65 6d  TORE     db->tem
33b4e 70 5f 73 74 6f 72 65 20 20 20 20 20 4c 6f 63 61  p_store     Loca
33b4f 74 69 6f 6e 20 6f 66 20 74 65 6d 70 6f 72 61 72  tion of temporar
33b50 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20  y database.**   
33b51 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
33b52 2d 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  -     ----------
33b53 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d  ----     -------
33b54 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
33b55 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 30 20 20  -------.**   0  
33b56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33b57 20 20 20 61 6e 79 20 20 20 20 20 20 20 20 20 20     any          
33b58 20 20 20 20 20 20 66 69 6c 65 20 20 20 20 20 20        file      
33b59 28 72 65 74 75 72 6e 20 30 29 0a 2a 2a 20 20 20  (return 0).**   
33b5a 31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1               
33b5b 20 20 20 20 20 20 31 20 20 20 20 20 20 20 20 20        1         
33b5c 20 20 20 20 20 20 20 20 20 66 69 6c 65 20 20 20           file   
33b5d 20 20 20 28 72 65 74 75 72 6e 20 30 29 0a 2a 2a     (return 0).**
33b5e 20 20 20 31 20 20 20 20 20 20 20 20 20 20 20 20     1            
33b5f 20 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20           2      
33b60 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f              memo
33b61 72 79 20 20 20 20 28 72 65 74 75 72 6e 20 31 29  ry    (return 1)
33b62 0a 2a 2a 20 20 20 31 20 20 20 20 20 20 20 20 20  .**   1         
33b63 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20              0   
33b64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66                 f
33b65 69 6c 65 20 20 20 20 20 20 28 72 65 74 75 72 6e  ile      (return
33b66 20 30 29 0a 2a 2a 20 20 20 32 20 20 20 20 20 20   0).**   2      
33b67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
33b68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33b69 20 20 66 69 6c 65 20 20 20 20 20 20 28 72 65 74    file      (ret
33b6a 75 72 6e 20 30 29 0a 2a 2a 20 20 20 32 20 20 20  urn 0).**   2   
33b6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33b6c 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20    2             
33b6d 20 20 20 20 20 6d 65 6d 6f 72 79 20 20 20 20 28       memory    (
33b6e 72 65 74 75 72 6e 20 31 29 0a 2a 2a 20 20 20 32  return 1).**   2
33b6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33b70 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20       0          
33b71 20 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 20          memory  
33b72 20 20 28 72 65 74 75 72 6e 20 31 29 0a 2a 2a 20    (return 1).** 
33b73 20 20 33 20 20 20 20 20 20 20 20 20 20 20 20 20    3             
33b74 20 20 20 20 20 20 20 20 61 6e 79 20 20 20 20 20          any     
33b75 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f 72             memor
33b76 79 20 20 20 20 28 72 65 74 75 72 6e 20 31 29 0a  y    (return 1).
33b77 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
33b78 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54 65 6d  E int sqlite3Tem
33b79 70 49 6e 4d 65 6d 6f 72 79 28 63 6f 6e 73 74 20  pInMemory(const 
33b7a 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 23 69  sqlite3 *db){.#i
33b7b 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54  f SQLITE_TEMP_ST
33b7c 4f 52 45 3d 3d 31 0a 20 20 72 65 74 75 72 6e 20  ORE==1.  return 
33b7d 28 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65  ( db->temp_store
33b7e 3d 3d 32 20 29 3b 0a 23 65 6e 64 69 66 0a 23 69  ==2 );.#endif.#i
33b7f 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54  f SQLITE_TEMP_ST
33b80 4f 52 45 3d 3d 32 0a 20 20 72 65 74 75 72 6e 20  ORE==2.  return 
33b81 28 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65  ( db->temp_store
33b82 21 3d 31 20 29 3b 0a 23 65 6e 64 69 66 0a 23 69  !=1 );.#endif.#i
33b83 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54  f SQLITE_TEMP_ST
33b84 4f 52 45 3d 3d 33 0a 20 20 72 65 74 75 72 6e 20  ORE==3.  return 
33b85 31 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51  1;.#endif.#if SQ
33b86 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3c  LITE_TEMP_STORE<
33b87 31 20 7c 7c 20 53 51 4c 49 54 45 5f 54 45 4d 50  1 || SQLITE_TEMP
33b88 5f 53 54 4f 52 45 3e 33 0a 20 20 72 65 74 75 72  _STORE>3.  retur
33b89 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  n 0;.#endif.}../
33b8a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
33b8b 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63  e is called to c
33b8c 72 65 61 74 65 20 61 20 63 6f 6e 6e 65 63 74 69  reate a connecti
33b8d 6f 6e 20 74 6f 20 61 20 64 61 74 61 62 61 73 65  on to a database
33b8e 20 42 54 72 65 65 0a 2a 2a 20 64 72 69 76 65 72   BTree.** driver
33b8f 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20  .  If zFilename 
33b90 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61  is the name of a
33b91 20 66 69 6c 65 2c 20 74 68 65 6e 20 74 68 61 74   file, then that
33b92 20 66 69 6c 65 20 69 73 0a 2a 2a 20 6f 70 65 6e   file is.** open
33b93 65 64 20 61 6e 64 20 75 73 65 64 2e 20 20 49 66  ed and used.  If
33b94 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68   zFilename is th
33b95 65 20 6d 61 67 69 63 20 6e 61 6d 65 20 22 3a 6d  e magic name ":m
33b96 65 6d 6f 72 79 3a 22 20 74 68 65 6e 0a 2a 2a 20  emory:" then.** 
33b97 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
33b98 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79  stored in memory
33b99 20 28 61 6e 64 20 69 73 20 74 68 75 73 20 66 6f   (and is thus fo
33b9a 72 67 6f 74 74 65 6e 20 61 73 20 73 6f 6f 6e 20  rgotten as soon 
33b9b 61 73 0a 2a 2a 20 74 68 65 20 63 6f 6e 6e 65 63  as.** the connec
33b9c 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 29  tion is closed.)
33b9d 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69    If zFilename i
33b9e 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20  s NULL then the 
33b9f 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 61  database.** is a
33ba0 20 22 76 69 72 74 75 61 6c 22 20 64 61 74 61 62   "virtual" datab
33ba1 61 73 65 20 66 6f 72 20 74 72 61 6e 73 69 65 6e  ase for transien
33ba2 74 20 75 73 65 20 6f 6e 6c 79 20 61 6e 64 20 69  t use only and i
33ba3 73 20 64 65 6c 65 74 65 64 20 61 73 0a 2a 2a 20  s deleted as.** 
33ba4 73 6f 6f 6e 20 61 73 20 74 68 65 20 63 6f 6e 6e  soon as the conn
33ba5 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64  ection is closed
33ba6 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72 74 75 61  ..**.** A virtua
33ba7 6c 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 62  l database can b
33ba8 65 20 65 69 74 68 65 72 20 61 20 64 69 73 6b 20  e either a disk 
33ba9 66 69 6c 65 20 28 74 68 61 74 20 69 73 20 61 75  file (that is au
33baa 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 64  tomatically.** d
33bab 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65 20  eleted when the 
33bac 66 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 29 20  file is closed) 
33bad 6f 72 20 69 74 20 61 6e 20 62 65 20 68 65 6c 64  or it an be held
33bae 20 65 6e 74 69 72 65 6c 79 20 69 6e 20 6d 65 6d   entirely in mem
33baf 6f 72 79 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ory..** The sqli
33bb0 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28  te3TempInMemory(
33bb1 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  ) function is us
33bb2 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
33bb3 77 68 69 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45  which..*/.SQLITE
33bb4 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
33bb5 69 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 79  ite3BtreeFactory
33bb6 28 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  (.  const sqlite
33bb7 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 2f 2a  3 *db,        /*
33bb8 20 4d 61 69 6e 20 64 61 74 61 62 61 73 65 20 77   Main database w
33bb9 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61 75 78 20  hen opening aux 
33bba 6f 74 68 65 72 77 69 73 65 20 30 20 2a 2f 0a 20  otherwise 0 */. 
33bbb 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
33bbc 6c 65 6e 61 6d 65 2c 20 20 20 20 2f 2a 20 4e 61  lename,    /* Na
33bbd 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63  me of the file c
33bbe 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 42 54  ontaining the BT
33bbf 72 65 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  ree database */.
33bc0 20 20 69 6e 74 20 6f 6d 69 74 4a 6f 75 72 6e 61    int omitJourna
33bc1 6c 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69  l,          /* i
33bc2 66 20 54 52 55 45 20 74 68 65 6e 20 64 6f 20 6e  f TRUE then do n
33bc3 6f 74 20 6a 6f 75 72 6e 61 6c 20 74 68 69 73 20  ot journal this 
33bc4 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  file */.  int nC
33bc5 61 63 68 65 2c 20 20 20 20 20 20 20 20 20 20 20  ache,           
33bc6 20 20 20 20 2f 2a 20 48 6f 77 20 6d 61 6e 79 20      /* How many 
33bc7 70 61 67 65 73 20 69 6e 20 74 68 65 20 70 61 67  pages in the pag
33bc8 65 20 63 61 63 68 65 20 2a 2f 0a 20 20 69 6e 74  e cache */.  int
33bc9 20 76 66 73 46 6c 61 67 73 2c 20 20 20 20 20 20   vfsFlags,      
33bca 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
33bcb 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74  passed through t
33bcc 6f 20 76 66 73 4f 70 65 6e 20 2a 2f 0a 20 20 42  o vfsOpen */.  B
33bcd 74 72 65 65 20 2a 2a 70 70 42 74 72 65 65 20 20  tree **ppBtree  
33bce 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
33bcf 74 65 72 20 74 6f 20 6e 65 77 20 42 74 72 65 65  ter to new Btree
33bd0 20 6f 62 6a 65 63 74 20 77 72 69 74 74 65 6e 20   object written 
33bd1 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  here */.){.  int
33bd2 20 62 74 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20   btFlags = 0;.  
33bd3 69 6e 74 20 72 63 3b 0a 20 20 0a 20 20 61 73 73  int rc;.  .  ass
33bd4 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
33bd5 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
33bd6 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
33bd7 70 70 42 74 72 65 65 20 21 3d 20 30 29 3b 0a 20  ppBtree != 0);. 
33bd8 20 69 66 28 20 6f 6d 69 74 4a 6f 75 72 6e 61 6c   if( omitJournal
33bd9 20 29 7b 0a 20 20 20 20 62 74 46 6c 61 67 73 20   ){.    btFlags 
33bda 7c 3d 20 42 54 52 45 45 5f 4f 4d 49 54 5f 4a 4f  |= BTREE_OMIT_JO
33bdb 55 52 4e 41 4c 3b 0a 20 20 7d 0a 20 20 69 66 28  URNAL;.  }.  if(
33bdc 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c   db->flags & SQL
33bdd 49 54 45 5f 4e 6f 52 65 61 64 6c 6f 63 6b 20 29  ITE_NoReadlock )
33bde 7b 0a 20 20 20 20 62 74 46 6c 61 67 73 20 7c 3d  {.    btFlags |=
33bdf 20 42 54 52 45 45 5f 4e 4f 5f 52 45 41 44 4c 4f   BTREE_NO_READLO
33be0 43 4b 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20  CK;.  }.#ifndef 
33be1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f  SQLITE_OMIT_MEMO
33be2 52 59 44 42 0a 20 20 69 66 28 20 7a 46 69 6c 65  RYDB.  if( zFile
33be3 6e 61 6d 65 3d 3d 30 20 26 26 20 73 71 6c 69 74  name==0 && sqlit
33be4 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 64  e3TempInMemory(d
33be5 62 29 20 29 7b 0a 20 20 20 20 7a 46 69 6c 65 6e  b) ){.    zFilen
33be6 61 6d 65 20 3d 20 22 3a 6d 65 6d 6f 72 79 3a 22  ame = ":memory:"
33be7 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
33be8 69 66 28 20 28 76 66 73 46 6c 61 67 73 20 26 20  if( (vfsFlags & 
33be9 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
33bea 5f 44 42 29 21 3d 30 20 26 26 20 28 7a 46 69 6c  _DB)!=0 && (zFil
33beb 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 2a 7a 46 69  ename==0 || *zFi
33bec 6c 65 6e 61 6d 65 3d 3d 30 29 20 29 7b 0a 20 20  lename==0) ){.  
33bed 20 20 76 66 73 46 6c 61 67 73 20 3d 20 28 76 66    vfsFlags = (vf
33bee 73 46 6c 61 67 73 20 26 20 7e 53 51 4c 49 54 45  sFlags & ~SQLITE
33bef 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 20 7c  _OPEN_MAIN_DB) |
33bf0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d   SQLITE_OPEN_TEM
33bf1 50 5f 44 42 3b 0a 20 20 7d 0a 20 20 72 63 20 3d  P_DB;.  }.  rc =
33bf2 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65   sqlite3BtreeOpe
33bf3 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 28 73 71  n(zFilename, (sq
33bf4 6c 69 74 65 33 20 2a 29 64 62 2c 20 70 70 42 74  lite3 *)db, ppBt
33bf5 72 65 65 2c 20 62 74 46 6c 61 67 73 2c 20 76 66  ree, btFlags, vf
33bf6 73 46 6c 61 67 73 29 3b 0a 0a 20 20 2f 2a 20 49  sFlags);..  /* I
33bf7 66 20 74 68 65 20 42 2d 54 72 65 65 20 77 61 73  f the B-Tree was
33bf8 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70   successfully op
33bf9 65 6e 65 64 2c 20 73 65 74 20 74 68 65 20 70 61  ened, set the pa
33bfa 67 65 72 2d 63 61 63 68 65 20 73 69 7a 65 20 74  ger-cache size t
33bfb 6f 20 74 68 65 0a 20 20 2a 2a 20 64 65 66 61 75  o the.  ** defau
33bfc 6c 74 20 76 61 6c 75 65 2e 20 45 78 63 65 70 74  lt value. Except
33bfd 2c 20 69 66 20 74 68 65 20 63 61 6c 6c 20 74 6f  , if the call to
33bfe 20 42 74 72 65 65 4f 70 65 6e 28 29 20 72 65 74   BtreeOpen() ret
33bff 75 72 6e 65 64 20 61 20 68 61 6e 64 6c 65 0a 20  urned a handle. 
33c00 20 2a 2a 20 6f 70 65 6e 20 6f 6e 20 61 6e 20 65   ** open on an e
33c01 78 69 73 74 69 6e 67 20 73 68 61 72 65 64 20 70  xisting shared p
33c02 61 67 65 72 2d 63 61 63 68 65 2c 20 64 6f 20 6e  ager-cache, do n
33c03 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20 70 61  ot change the pa
33c04 67 65 72 2d 63 61 63 68 65 20 0a 20 20 2a 2a 20  ger-cache .  ** 
33c05 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  size..  */.  if(
33c06 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
33c07 26 20 30 3d 3d 73 71 6c 69 74 65 33 42 74 72 65  & 0==sqlite3Btre
33c08 65 53 63 68 65 6d 61 28 2a 70 70 42 74 72 65 65  eSchema(*ppBtree
33c09 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20 73  , 0, 0) ){.    s
33c0a 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61  qlite3BtreeSetCa
33c0b 63 68 65 53 69 7a 65 28 2a 70 70 42 74 72 65 65  cheSize(*ppBtree
33c0c 2c 20 6e 43 61 63 68 65 29 3b 0a 20 20 7d 0a 20  , nCache);.  }. 
33c0d 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
33c0e 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 55 54 46 2d  *.** Return UTF-
33c0f 38 20 65 6e 63 6f 64 65 64 20 45 6e 67 6c 69 73  8 encoded Englis
33c10 68 20 6c 61 6e 67 75 61 67 65 20 65 78 70 6c 61  h language expla
33c11 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f  nation of the mo
33c12 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 65 72 72  st recent.** err
33c13 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  or..*/.SQLITE_AP
33c14 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
33c15 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71 6c  lite3_errmsg(sql
33c16 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 63 6f 6e  ite3 *db){.  con
33c17 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 66  st char *z;.  if
33c18 28 20 21 64 62 20 29 7b 0a 20 20 20 20 72 65 74  ( !db ){.    ret
33c19 75 72 6e 20 73 71 6c 69 74 65 33 45 72 72 53 74  urn sqlite3ErrSt
33c1a 72 28 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b  r(SQLITE_NOMEM);
33c1b 0a 20 20 7d 0a 20 20 69 66 28 20 21 73 71 6c 69  .  }.  if( !sqli
33c1c 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 53 69  te3SafetyCheckSi
33c1d 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20 20  ckOrOk(db) ){.  
33c1e 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
33c1f 45 72 72 53 74 72 28 53 51 4c 49 54 45 5f 4d 49  ErrStr(SQLITE_MI
33c20 53 55 53 45 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  SUSE);.  }.  sql
33c21 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
33c22 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69  (db->mutex);.  i
33c23 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
33c24 6c 65 64 20 29 7b 0a 20 20 20 20 7a 20 3d 20 73  led ){.    z = s
33c25 71 6c 69 74 65 33 45 72 72 53 74 72 28 53 51 4c  qlite3ErrStr(SQL
33c26 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 7d 65  ITE_NOMEM);.  }e
33c27 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 28 63 68  lse{.    z = (ch
33c28 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
33c29 65 5f 74 65 78 74 28 64 62 2d 3e 70 45 72 72 29  e_text(db->pErr)
33c2a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 64  ;.    assert( !d
33c2b 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
33c2c 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20  );.    if( z==0 
33c2d 29 7b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c  ){.      z = sql
33c2e 69 74 65 33 45 72 72 53 74 72 28 64 62 2d 3e 65  ite3ErrStr(db->e
33c2f 72 72 43 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20  rrCode);.    }. 
33c30 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
33c31 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
33c32 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b  ex);.  return z;
33c33 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
33c34 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a  TE_OMIT_UTF16./*
33c35 0a 2a 2a 20 52 65 74 75 72 6e 20 55 54 46 2d 31  .** Return UTF-1
33c36 36 20 65 6e 63 6f 64 65 64 20 45 6e 67 6c 69 73  6 encoded Englis
33c37 68 20 6c 61 6e 67 75 61 67 65 20 65 78 70 6c 61  h language expla
33c38 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f  nation of the mo
33c39 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 65 72 72  st recent.** err
33c3a 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  or..*/.SQLITE_AP
33c3b 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
33c3c 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 73  lite3_errmsg16(s
33c3d 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73  qlite3 *db){.  s
33c3e 74 61 74 69 63 20 63 6f 6e 73 74 20 75 31 36 20  tatic const u16 
33c3f 6f 75 74 4f 66 4d 65 6d 5b 5d 20 3d 20 7b 0a 20  outOfMem[] = {. 
33c40 20 20 20 27 6f 27 2c 20 27 75 27 2c 20 27 74 27     'o', 'u', 't'
33c41 2c 20 27 20 27 2c 20 27 6f 27 2c 20 27 66 27 2c  , ' ', 'o', 'f',
33c42 20 27 20 27 2c 20 27 6d 27 2c 20 27 65 27 2c 20   ' ', 'm', 'e', 
33c43 27 6d 27 2c 20 27 6f 27 2c 20 27 72 27 2c 20 27  'm', 'o', 'r', '
33c44 79 27 2c 20 30 0a 20 20 7d 3b 0a 20 20 73 74 61  y', 0.  };.  sta
33c45 74 69 63 20 63 6f 6e 73 74 20 75 31 36 20 6d 69  tic const u16 mi
33c46 73 75 73 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 27  suse[] = {.    '
33c47 6c 27 2c 20 27 69 27 2c 20 27 62 27 2c 20 27 72  l', 'i', 'b', 'r
33c48 27 2c 20 27 61 27 2c 20 27 72 27 2c 20 27 79 27  ', 'a', 'r', 'y'
33c49 2c 20 27 20 27 2c 20 0a 20 20 20 20 27 72 27 2c  , ' ', .    'r',
33c4a 20 27 6f 27 2c 20 27 75 27 2c 20 27 74 27 2c 20   'o', 'u', 't', 
33c4b 27 69 27 2c 20 27 6e 27 2c 20 27 65 27 2c 20 27  'i', 'n', 'e', '
33c4c 20 27 2c 20 0a 20 20 20 20 27 63 27 2c 20 27 61   ', .    'c', 'a
33c4d 27 2c 20 27 6c 27 2c 20 27 6c 27 2c 20 27 65 27  ', 'l', 'l', 'e'
33c4e 2c 20 27 64 27 2c 20 27 20 27 2c 20 0a 20 20 20  , 'd', ' ', .   
33c4f 20 27 6f 27 2c 20 27 75 27 2c 20 27 74 27 2c 20   'o', 'u', 't', 
33c50 27 20 27 2c 20 0a 20 20 20 20 27 6f 27 2c 20 27  ' ', .    'o', '
33c51 66 27 2c 20 27 20 27 2c 20 0a 20 20 20 20 27 73  f', ' ', .    's
33c52 27 2c 20 27 65 27 2c 20 27 71 27 2c 20 27 75 27  ', 'e', 'q', 'u'
33c53 2c 20 27 65 27 2c 20 27 6e 27 2c 20 27 63 27 2c  , 'e', 'n', 'c',
33c54 20 27 65 27 2c 20 30 0a 20 20 7d 3b 0a 0a 20 20   'e', 0.  };..  
33c55 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 3b 0a 20  const void *z;. 
33c56 20 69 66 28 20 21 64 62 20 29 7b 0a 20 20 20 20   if( !db ){.    
33c57 72 65 74 75 72 6e 20 28 76 6f 69 64 20 2a 29 6f  return (void *)o
33c58 75 74 4f 66 4d 65 6d 3b 0a 20 20 7d 0a 20 20 69  utOfMem;.  }.  i
33c59 66 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74  f( !sqlite3Safet
33c5a 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64  yCheckSickOrOk(d
33c5b 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  b) ){.    return
33c5c 20 28 76 6f 69 64 20 2a 29 6d 69 73 75 73 65 3b   (void *)misuse;
33c5d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
33c5e 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
33c5f 75 74 65 78 29 3b 0a 20 20 69 66 28 20 64 62 2d  utex);.  if( db-
33c60 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
33c61 0a 20 20 20 20 7a 20 3d 20 28 76 6f 69 64 20 2a  .    z = (void *
33c62 29 6f 75 74 4f 66 4d 65 6d 3b 0a 20 20 7d 65 6c  )outOfMem;.  }el
33c63 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69  se{.    z = sqli
33c64 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
33c65 28 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20 20 20  (db->pErr);.    
33c66 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20  if( z==0 ){.    
33c67 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65    sqlite3ValueSe
33c68 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d  tStr(db->pErr, -
33c69 31 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72  1, sqlite3ErrStr
33c6a 28 64 62 2d 3e 65 72 72 43 6f 64 65 29 2c 0a 20  (db->errCode),. 
33c6b 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
33c6c 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54  _UTF8, SQLITE_ST
33c6d 41 54 49 43 29 3b 0a 20 20 20 20 20 20 7a 20 3d  ATIC);.      z =
33c6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
33c6f 65 78 74 31 36 28 64 62 2d 3e 70 45 72 72 29 3b  ext16(db->pErr);
33c70 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 41 20  .    }.    /* A 
33c71 6d 61 6c 6c 6f 63 28 29 20 6d 61 79 20 68 61 76  malloc() may hav
33c72 65 20 66 61 69 6c 65 64 20 77 69 74 68 69 6e 20  e failed within 
33c73 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  the call to sqli
33c74 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
33c75 28 29 0a 20 20 20 20 2a 2a 20 61 62 6f 76 65 2e  ().    ** above.
33c76 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
33c77 63 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 64  case, then the d
33c78 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
33c79 66 6c 61 67 20 6e 65 65 64 73 20 74 6f 0a 20 20  flag needs to.  
33c7a 20 20 2a 2a 20 62 65 20 63 6c 65 61 72 65 64 20    ** be cleared 
33c7b 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
33c7c 2e 20 44 6f 20 74 68 69 73 20 64 69 72 65 63 74  . Do this direct
33c7d 6c 79 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 76  ly, instead of v
33c7e 69 61 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  ia.    ** sqlite
33c7f 33 41 70 69 45 78 69 74 28 29 2c 20 74 6f 20 61  3ApiExit(), to a
33c80 76 6f 69 64 20 73 65 74 74 69 6e 67 20 74 68 65  void setting the
33c81 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
33c82 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a   error message..
33c83 20 20 20 20 2a 2f 0a 20 20 20 20 64 62 2d 3e 6d      */.    db->m
33c84 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b  allocFailed = 0;
33c85 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
33c86 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
33c87 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
33c88 7a 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  z;.}.#endif /* S
33c89 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
33c8a 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72   */../*.** Retur
33c8b 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  n the most recen
33c8c 74 20 65 72 72 6f 72 20 63 6f 64 65 20 67 65 6e  t error code gen
33c8d 65 72 61 74 65 64 20 62 79 20 61 6e 20 53 51 4c  erated by an SQL
33c8e 69 74 65 20 72 6f 75 74 69 6e 65 2e 20 49 66 20  ite routine. If 
33c8f 4e 55 4c 4c 20 69 73 0a 2a 2a 20 70 61 73 73 65  NULL is.** passe
33c90 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  d to this functi
33c91 6f 6e 2c 20 77 65 20 61 73 73 75 6d 65 20 61 20  on, we assume a 
33c92 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65 64 20  malloc() failed 
33c93 64 75 72 69 6e 67 20 73 71 6c 69 74 65 33 5f 6f  during sqlite3_o
33c94 70 65 6e 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pen()..*/.SQLITE
33c95 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
33c96 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65 33  _errcode(sqlite3
33c97 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62 20   *db){.  if( db 
33c98 26 26 20 21 73 71 6c 69 74 65 33 53 61 66 65 74  && !sqlite3Safet
33c99 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64  yCheckSickOrOk(d
33c9a 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  b) ){.    return
33c9b 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
33c9c 20 20 7d 0a 20 20 69 66 28 20 21 64 62 20 7c 7c    }.  if( !db ||
33c9d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
33c9e 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  d ){.    return 
33c9f 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
33ca0 7d 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 65  }.  return db->e
33ca1 72 72 43 6f 64 65 20 26 20 64 62 2d 3e 65 72 72  rrCode & db->err
33ca2 4d 61 73 6b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  Mask;.}.SQLITE_A
33ca3 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  PI int sqlite3_e
33ca4 78 74 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28  xtended_errcode(
33ca5 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
33ca6 69 66 28 20 64 62 20 26 26 20 21 73 71 6c 69 74  if( db && !sqlit
33ca7 65 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63  e3SafetyCheckSic
33ca8 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20  kOrOk(db) ){.   
33ca9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d   return SQLITE_M
33caa 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28  ISUSE;.  }.  if(
33cab 20 21 64 62 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c   !db || db->mall
33cac 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
33cad 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
33cae 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  MEM;.  }.  retur
33caf 6e 20 64 62 2d 3e 65 72 72 43 6f 64 65 3b 0a 7d  n db->errCode;.}
33cb0 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
33cb1 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6e 67 20 66   new collating f
33cb2 75 6e 63 74 69 6f 6e 20 66 6f 72 20 64 61 74 61  unction for data
33cb3 62 61 73 65 20 22 64 62 22 2e 20 20 54 68 65 20  base "db".  The 
33cb4 6e 61 6d 65 20 69 73 20 7a 4e 61 6d 65 0a 2a 2a  name is zName.**
33cb5 20 61 6e 64 20 74 68 65 20 65 6e 63 6f 64 69 6e   and the encodin
33cb6 67 20 69 73 20 65 6e 63 2e 0a 2a 2f 0a 73 74 61  g is enc..*/.sta
33cb7 74 69 63 20 69 6e 74 20 63 72 65 61 74 65 43 6f  tic int createCo
33cb8 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74  llation(.  sqlit
33cb9 65 33 2a 20 64 62 2c 0a 20 20 63 6f 6e 73 74 20  e3* db,.  const 
33cba 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20  char *zName, .  
33cbb 75 38 20 65 6e 63 2c 0a 20 20 75 38 20 63 6f 6c  u8 enc,.  u8 col
33cbc 6c 54 79 70 65 2c 0a 20 20 76 6f 69 64 2a 20 70  lType,.  void* p
33cbd 43 74 78 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d  Ctx,.  int(*xCom
33cbe 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  pare)(void*,int,
33cbf 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
33cc0 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 0a 20 20  const void*),.  
33cc1 76 6f 69 64 28 2a 78 44 65 6c 29 28 76 6f 69 64  void(*xDel)(void
33cc2 2a 29 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20  *).){.  CollSeq 
33cc3 2a 70 43 6f 6c 6c 3b 0a 20 20 69 6e 74 20 65 6e  *pColl;.  int en
33cc4 63 32 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20  c2;.  int nName 
33cc5 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
33cc6 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 0a 20 20 61  0(zName);.  .  a
33cc7 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
33cc8 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
33cc9 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  tex) );..  /* If
33cca 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20 69 73   SQLITE_UTF16 is
33ccb 20 73 70 65 63 69 66 69 65 64 20 61 73 20 74 68   specified as th
33ccc 65 20 65 6e 63 6f 64 69 6e 67 20 74 79 70 65 2c  e encoding type,
33ccd 20 74 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 0a   transform this.
33cce 20 20 2a 2a 20 74 6f 20 6f 6e 65 20 6f 66 20 53    ** to one of S
33ccf 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72  QLITE_UTF16LE or
33cd0 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20   SQLITE_UTF16BE 
33cd1 75 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 53  using the.  ** S
33cd2 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
33cd3 45 20 6d 61 63 72 6f 2e 20 53 51 4c 49 54 45 5f  E macro. SQLITE_
33cd4 55 54 46 31 36 20 69 73 20 6e 6f 74 20 75 73 65  UTF16 is not use
33cd5 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20  d internally..  
33cd6 2a 2f 0a 20 20 65 6e 63 32 20 3d 20 65 6e 63 3b  */.  enc2 = enc;
33cd7 0a 20 20 74 65 73 74 63 61 73 65 28 20 65 6e 63  .  testcase( enc
33cd8 32 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 20  2==SQLITE_UTF16 
33cd9 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 65  );.  testcase( e
33cda 6e 63 32 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31  nc2==SQLITE_UTF1
33cdb 36 5f 41 4c 49 47 4e 45 44 20 29 3b 0a 20 20 69  6_ALIGNED );.  i
33cdc 66 28 20 65 6e 63 32 3d 3d 53 51 4c 49 54 45 5f  f( enc2==SQLITE_
33cdd 55 54 46 31 36 20 7c 7c 20 65 6e 63 32 3d 3d 53  UTF16 || enc2==S
33cde 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47  QLITE_UTF16_ALIG
33cdf 4e 45 44 20 29 7b 0a 20 20 20 20 65 6e 63 32 20  NED ){.    enc2 
33ce0 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  = SQLITE_UTF16NA
33ce1 54 49 56 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20  TIVE;.  }.  if( 
33ce2 65 6e 63 32 3c 53 51 4c 49 54 45 5f 55 54 46 38  enc2<SQLITE_UTF8
33ce3 20 7c 7c 20 65 6e 63 32 3e 53 51 4c 49 54 45 5f   || enc2>SQLITE_
33ce4 55 54 46 31 36 42 45 20 29 7b 0a 20 20 20 20 72  UTF16BE ){.    r
33ce5 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
33ce6 55 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  USE;.  }..  /* C
33ce7 68 65 63 6b 20 69 66 20 74 68 69 73 20 63 61 6c  heck if this cal
33ce8 6c 20 69 73 20 72 65 6d 6f 76 69 6e 67 20 6f 72  l is removing or
33ce9 20 72 65 70 6c 61 63 69 6e 67 20 61 6e 20 65 78   replacing an ex
33cea 69 73 74 69 6e 67 20 63 6f 6c 6c 61 74 69 6f 6e  isting collation
33ceb 20 0a 20 20 2a 2a 20 73 65 71 75 65 6e 63 65 2e   .  ** sequence.
33cec 20 49 66 20 73 6f 2c 20 61 6e 64 20 74 68 65 72   If so, and ther
33ced 65 20 61 72 65 20 61 63 74 69 76 65 20 56 4d 73  e are active VMs
33cee 2c 20 72 65 74 75 72 6e 20 62 75 73 79 2e 20 49  , return busy. I
33cef 66 20 74 68 65 72 65 0a 20 20 2a 2a 20 61 72 65  f there.  ** are
33cf0 20 6e 6f 20 61 63 74 69 76 65 20 56 4d 73 2c 20   no active VMs, 
33cf1 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70  invalidate any p
33cf2 72 65 2d 63 6f 6d 70 69 6c 65 64 20 73 74 61 74  re-compiled stat
33cf3 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 70  ements..  */.  p
33cf4 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69  Coll = sqlite3Fi
33cf5 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 28 75  ndCollSeq(db, (u
33cf6 38 29 65 6e 63 32 2c 20 7a 4e 61 6d 65 2c 20 30  8)enc2, zName, 0
33cf7 29 3b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20 26  );.  if( pColl &
33cf8 26 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 7b  & pColl->xCmp ){
33cf9 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74  .    if( db->act
33cfa 69 76 65 56 64 62 65 43 6e 74 20 29 7b 0a 20 20  iveVdbeCnt ){.  
33cfb 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
33cfc 28 64 62 2c 20 53 51 4c 49 54 45 5f 42 55 53 59  (db, SQLITE_BUSY
33cfd 2c 20 0a 20 20 20 20 20 20 20 20 22 75 6e 61 62  , .        "unab
33cfe 6c 65 20 74 6f 20 64 65 6c 65 74 65 2f 6d 6f 64  le to delete/mod
33cff 69 66 79 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  ify collation se
33d00 71 75 65 6e 63 65 20 64 75 65 20 74 6f 20 61 63  quence due to ac
33d01 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73 22  tive statements"
33d02 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
33d03 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20  SQLITE_BUSY;.   
33d04 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78   }.    sqlite3Ex
33d05 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74  pirePreparedStat
33d06 65 6d 65 6e 74 73 28 64 62 29 3b 0a 0a 20 20 20  ements(db);..   
33d07 20 2f 2a 20 49 66 20 63 6f 6c 6c 61 74 69 6f 6e   /* If collation
33d08 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 20   sequence pColl 
33d09 77 61 73 20 63 72 65 61 74 65 64 20 64 69 72 65  was created dire
33d0a 63 74 6c 79 20 62 79 20 61 20 63 61 6c 6c 20 74  ctly by a call t
33d0b 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33  o.    ** sqlite3
33d0c 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
33d0d 6e 2c 20 61 6e 64 20 6e 6f 74 20 67 65 6e 65 72  n, and not gener
33d0e 61 74 65 64 20 62 79 20 73 79 6e 74 68 43 6f 6c  ated by synthCol
33d0f 6c 53 65 71 28 29 2c 0a 20 20 20 20 2a 2a 20 74  lSeq(),.    ** t
33d10 68 65 6e 20 61 6e 79 20 63 6f 70 69 65 73 20 6d  hen any copies m
33d11 61 64 65 20 62 79 20 73 79 6e 74 68 43 6f 6c 6c  ade by synthColl
33d12 53 65 71 28 29 20 6e 65 65 64 20 74 6f 20 62 65  Seq() need to be
33d13 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 20 20   invalidated..  
33d14 20 20 2a 2a 20 41 6c 73 6f 2c 20 63 6f 6c 6c 61    ** Also, colla
33d15 74 69 6f 6e 20 64 65 73 74 72 75 63 74 6f 72 20  tion destructor 
33d16 2d 20 43 6f 6c 6c 53 65 71 2e 78 44 65 6c 28 29  - CollSeq.xDel()
33d17 20 2d 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20   - function may 
33d18 6e 65 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 62  need.    ** to b
33d19 65 20 63 61 6c 6c 65 64 2e 0a 20 20 20 20 2a 2f  e called..    */
33d1a 20 0a 20 20 20 20 69 66 28 20 28 70 43 6f 6c 6c   .    if( (pColl
33d1b 2d 3e 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f  ->enc & ~SQLITE_
33d1c 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 3d 3d  UTF16_ALIGNED)==
33d1d 65 6e 63 32 20 29 7b 0a 20 20 20 20 20 20 43 6f  enc2 ){.      Co
33d1e 6c 6c 53 65 71 20 2a 61 43 6f 6c 6c 20 3d 20 73  llSeq *aColl = s
33d1f 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26  qlite3HashFind(&
33d20 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20 7a 4e  db->aCollSeq, zN
33d21 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20  ame, nName);.   
33d22 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
33d23 66 6f 72 28 6a 3d 30 3b 20 6a 3c 33 3b 20 6a 2b  for(j=0; j<3; j+
33d24 2b 29 7b 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c  +){.        Coll
33d25 53 65 71 20 2a 70 20 3d 20 26 61 43 6f 6c 6c 5b  Seq *p = &aColl[
33d26 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  j];.        if( 
33d27 70 2d 3e 65 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65  p->enc==pColl->e
33d28 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  nc ){.          
33d29 69 66 28 20 70 2d 3e 78 44 65 6c 20 29 7b 0a 20  if( p->xDel ){. 
33d2a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 78 44             p->xD
33d2b 65 6c 28 70 2d 3e 70 55 73 65 72 29 3b 0a 20 20  el(p->pUser);.  
33d2c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
33d2d 20 20 20 20 70 2d 3e 78 43 6d 70 20 3d 20 30 3b      p->xCmp = 0;
33d2e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
33d2f 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
33d30 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46  pColl = sqlite3F
33d31 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 28  indCollSeq(db, (
33d32 75 38 29 65 6e 63 32 2c 20 7a 4e 61 6d 65 2c 20  u8)enc2, zName, 
33d33 31 29 3b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 20  1);.  if( pColl 
33d34 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e 78 43  ){.    pColl->xC
33d35 6d 70 20 3d 20 78 43 6f 6d 70 61 72 65 3b 0a 20  mp = xCompare;. 
33d36 20 20 20 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 20     pColl->pUser 
33d37 3d 20 70 43 74 78 3b 0a 20 20 20 20 70 43 6f 6c  = pCtx;.    pCol
33d38 6c 2d 3e 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0a  l->xDel = xDel;.
33d39 20 20 20 20 70 43 6f 6c 6c 2d 3e 65 6e 63 20 3d      pColl->enc =
33d3a 20 28 75 38 29 28 65 6e 63 32 20 7c 20 28 65 6e   (u8)(enc2 | (en
33d3b 63 20 26 20 53 51 4c 49 54 45 5f 55 54 46 31 36  c & SQLITE_UTF16
33d3c 5f 41 4c 49 47 4e 45 44 29 29 3b 0a 20 20 20 20  _ALIGNED));.    
33d3d 70 43 6f 6c 6c 2d 3e 74 79 70 65 20 3d 20 63 6f  pColl->type = co
33d3e 6c 6c 54 79 70 65 3b 0a 20 20 7d 0a 20 20 73 71  llType;.  }.  sq
33d3f 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
33d40 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20  QLITE_OK, 0);.  
33d41 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
33d42 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ;.}.../*.** This
33d43 20 61 72 72 61 79 20 64 65 66 69 6e 65 73 20 68   array defines h
33d44 61 72 64 20 75 70 70 65 72 20 62 6f 75 6e 64 73  ard upper bounds
33d45 20 6f 6e 20 6c 69 6d 69 74 20 76 61 6c 75 65 73   on limit values
33d46 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 69 74 69 61  .  The.** initia
33d47 6c 69 7a 65 72 20 6d 75 73 74 20 62 65 20 6b 65  lizer must be ke
33d48 70 74 20 69 6e 20 73 79 6e 63 20 77 69 74 68 20  pt in sync with 
33d49 74 68 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54  the SQLITE_LIMIT
33d4a 5f 2a 0a 2a 2a 20 23 64 65 66 69 6e 65 73 20 69  _*.** #defines i
33d4b 6e 20 73 71 6c 69 74 65 33 2e 68 2e 0a 2a 2f 0a  n sqlite3.h..*/.
33d4c 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74  static const int
33d4d 20 61 48 61 72 64 4c 69 6d 69 74 5b 5d 20 3d 20   aHardLimit[] = 
33d4e 7b 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  {.  SQLITE_MAX_L
33d4f 45 4e 47 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f  ENGTH,.  SQLITE_
33d50 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 2c 0a  MAX_SQL_LENGTH,.
33d51 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c    SQLITE_MAX_COL
33d52 55 4d 4e 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41  UMN,.  SQLITE_MA
33d53 58 5f 45 58 50 52 5f 44 45 50 54 48 2c 0a 20 20  X_EXPR_DEPTH,.  
33d54 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f  SQLITE_MAX_COMPO
33d55 55 4e 44 5f 53 45 4c 45 43 54 2c 0a 20 20 53 51  UND_SELECT,.  SQ
33d56 4c 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50  LITE_MAX_VDBE_OP
33d57 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46  ,.  SQLITE_MAX_F
33d58 55 4e 43 54 49 4f 4e 5f 41 52 47 2c 0a 20 20 53  UNCTION_ARG,.  S
33d59 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48  QLITE_MAX_ATTACH
33d5a 45 44 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58  ED,.  SQLITE_MAX
33d5b 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45  _LIKE_PATTERN_LE
33d5c 4e 47 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f 4d  NGTH,.  SQLITE_M
33d5d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42  AX_VARIABLE_NUMB
33d5e 45 52 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58  ER,.  SQLITE_MAX
33d5f 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 2c 0a  _TRIGGER_DEPTH,.
33d60 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73  };../*.** Make s
33d61 75 72 65 20 74 68 65 20 68 61 72 64 20 6c 69 6d  ure the hard lim
33d62 69 74 73 20 61 72 65 20 73 65 74 20 74 6f 20 72  its are set to r
33d63 65 61 73 6f 6e 61 62 6c 65 20 76 61 6c 75 65 73  easonable values
33d64 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  .*/.#if SQLITE_M
33d65 41 58 5f 4c 45 4e 47 54 48 3c 31 30 30 0a 23 20  AX_LENGTH<100.# 
33d66 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58  error SQLITE_MAX
33d67 5f 4c 45 4e 47 54 48 20 6d 75 73 74 20 62 65 20  _LENGTH must be 
33d68 61 74 20 6c 65 61 73 74 20 31 30 30 0a 23 65 6e  at least 100.#en
33d69 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  dif.#if SQLITE_M
33d6a 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 3c 31 30  AX_SQL_LENGTH<10
33d6b 30 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45  0.# error SQLITE
33d6c 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54 48 20  _MAX_SQL_LENGTH 
33d6d 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73 74  must be at least
33d6e 20 31 30 30 0a 23 65 6e 64 69 66 0a 23 69 66 20   100.#endif.#if 
33d6f 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c  SQLITE_MAX_SQL_L
33d70 45 4e 47 54 48 3e 53 51 4c 49 54 45 5f 4d 41 58  ENGTH>SQLITE_MAX
33d71 5f 4c 45 4e 47 54 48 0a 23 20 65 72 72 6f 72 20  _LENGTH.# error 
33d72 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c  SQLITE_MAX_SQL_L
33d73 45 4e 47 54 48 20 6d 75 73 74 20 6e 6f 74 20 62  ENGTH must not b
33d74 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 53  e greater than S
33d75 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48  QLITE_MAX_LENGTH
33d76 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
33d77 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f  TE_MAX_COMPOUND_
33d78 53 45 4c 45 43 54 3c 32 0a 23 20 65 72 72 6f 72  SELECT<2.# error
33d79 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d 50   SQLITE_MAX_COMP
33d7a 4f 55 4e 44 5f 53 45 4c 45 43 54 20 6d 75 73 74  OUND_SELECT must
33d7b 20 62 65 20 61 74 20 6c 65 61 73 74 20 32 0a 23   be at least 2.#
33d7c 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45  endif.#if SQLITE
33d7d 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 3c 34 30 0a  _MAX_VDBE_OP<40.
33d7e 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d  # error SQLITE_M
33d7f 41 58 5f 56 44 42 45 5f 4f 50 20 6d 75 73 74 20  AX_VDBE_OP must 
33d80 62 65 20 61 74 20 6c 65 61 73 74 20 34 30 0a 23  be at least 40.#
33d81 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45  endif.#if SQLITE
33d82 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52  _MAX_FUNCTION_AR
33d83 47 3c 30 20 7c 7c 20 53 51 4c 49 54 45 5f 4d 41  G<0 || SQLITE_MA
33d84 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 3e 31  X_FUNCTION_ARG>1
33d85 30 30 30 0a 23 20 65 72 72 6f 72 20 53 51 4c 49  000.# error SQLI
33d86 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f  TE_MAX_FUNCTION_
33d87 41 52 47 20 6d 75 73 74 20 62 65 20 62 65 74 77  ARG must be betw
33d88 65 65 6e 20 30 20 61 6e 64 20 31 30 30 30 0a 23  een 0 and 1000.#
33d89 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45  endif.#if SQLITE
33d8a 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 3c 30 20  _MAX_ATTACHED<0 
33d8b 7c 7c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54  || SQLITE_MAX_AT
33d8c 54 41 43 48 45 44 3e 33 30 0a 23 20 65 72 72 6f  TACHED>30.# erro
33d8d 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54  r SQLITE_MAX_ATT
33d8e 41 43 48 45 44 20 6d 75 73 74 20 62 65 20 62 65  ACHED must be be
33d8f 74 77 65 65 6e 20 30 20 61 6e 64 20 33 30 0a 23  tween 0 and 30.#
33d90 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45  endif.#if SQLITE
33d91 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52  _MAX_LIKE_PATTER
33d92 4e 5f 4c 45 4e 47 54 48 3c 31 0a 23 20 65 72 72  N_LENGTH<1.# err
33d93 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49  or SQLITE_MAX_LI
33d94 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
33d95 48 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61  H must be at lea
33d96 73 74 20 31 0a 23 65 6e 64 69 66 0a 23 69 66 20  st 1.#endif.#if 
33d97 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41  SQLITE_MAX_VARIA
33d98 42 4c 45 5f 4e 55 4d 42 45 52 3c 31 0a 23 20 65  BLE_NUMBER<1.# e
33d99 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f  rror SQLITE_MAX_
33d9a 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 20  VARIABLE_NUMBER 
33d9b 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73 74  must be at least
33d9c 20 31 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51   1.#endif.#if SQ
33d9d 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 3e  LITE_MAX_COLUMN>
33d9e 33 32 37 36 37 0a 23 20 65 72 72 6f 72 20 53 51  32767.# error SQ
33d9f 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20  LITE_MAX_COLUMN 
33da0 6d 75 73 74 20 6e 6f 74 20 65 78 63 65 65 64 20  must not exceed 
33da1 33 32 37 36 37 0a 23 65 6e 64 69 66 0a 23 69 66  32767.#endif.#if
33da2 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47   SQLITE_MAX_TRIG
33da3 47 45 52 5f 44 45 50 54 48 3c 31 0a 23 20 65 72  GER_DEPTH<1.# er
33da4 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54  ror SQLITE_MAX_T
33da5 52 49 47 47 45 52 5f 44 45 50 54 48 20 6d 75 73  RIGGER_DEPTH mus
33da6 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 31 0a  t be at least 1.
33da7 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43  #endif.../*.** C
33da8 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20  hange the value 
33da9 6f 66 20 61 20 6c 69 6d 69 74 2e 20 20 52 65 70  of a limit.  Rep
33daa 6f 72 74 20 74 68 65 20 6f 6c 64 20 76 61 6c 75  ort the old valu
33dab 65 2e 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 76 61  e..** If an inva
33dac 6c 69 64 20 6c 69 6d 69 74 20 69 6e 64 65 78 20  lid limit index 
33dad 69 73 20 73 75 70 70 6c 69 65 64 2c 20 72 65 70  is supplied, rep
33dae 6f 72 74 20 2d 31 2e 0a 2a 2a 20 4d 61 6b 65 20  ort -1..** Make 
33daf 6e 6f 20 63 68 61 6e 67 65 73 20 62 75 74 20 73  no changes but s
33db0 74 69 6c 6c 20 72 65 70 6f 72 74 20 74 68 65 20  till report the 
33db1 6f 6c 64 20 76 61 6c 75 65 20 69 66 20 74 68 65  old value if the
33db2 0a 2a 2a 20 6e 65 77 20 6c 69 6d 69 74 20 69 73  .** new limit is
33db3 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a   negative..**.**
33db4 20 41 20 6e 65 77 20 6c 6f 77 65 72 20 6c 69 6d   A new lower lim
33db5 69 74 20 64 6f 65 73 20 6e 6f 74 20 73 68 72 69  it does not shri
33db6 6e 6b 20 65 78 69 73 74 69 6e 67 20 63 6f 6e 73  nk existing cons
33db7 74 72 75 63 74 73 2e 0a 2a 2a 20 49 74 20 6d 65  tructs..** It me
33db8 72 65 6c 79 20 70 72 65 76 65 6e 74 73 20 6e 65  rely prevents ne
33db9 77 20 63 6f 6e 73 74 72 75 63 74 73 20 74 68 61  w constructs tha
33dba 74 20 65 78 63 65 65 64 20 74 68 65 20 6c 69 6d  t exceed the lim
33dbb 69 74 0a 2a 2a 20 66 72 6f 6d 20 66 6f 72 6d 69  it.** from formi
33dbc 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ng..*/.SQLITE_AP
33dbd 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69  I int sqlite3_li
33dbe 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  mit(sqlite3 *db,
33dbf 20 69 6e 74 20 6c 69 6d 69 74 49 64 2c 20 69 6e   int limitId, in
33dc0 74 20 6e 65 77 4c 69 6d 69 74 29 7b 0a 20 20 69  t newLimit){.  i
33dc1 6e 74 20 6f 6c 64 4c 69 6d 69 74 3b 0a 20 20 69  nt oldLimit;.  i
33dc2 66 28 20 6c 69 6d 69 74 49 64 3c 30 20 7c 7c 20  f( limitId<0 || 
33dc3 6c 69 6d 69 74 49 64 3e 3d 53 51 4c 49 54 45 5f  limitId>=SQLITE_
33dc4 4e 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20 72  N_LIMIT ){.    r
33dc5 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20  eturn -1;.  }.  
33dc6 6f 6c 64 4c 69 6d 69 74 20 3d 20 64 62 2d 3e 61  oldLimit = db->a
33dc7 4c 69 6d 69 74 5b 6c 69 6d 69 74 49 64 5d 3b 0a  Limit[limitId];.
33dc8 20 20 69 66 28 20 6e 65 77 4c 69 6d 69 74 3e 3d    if( newLimit>=
33dc9 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 65 77  0 ){.    if( new
33dca 4c 69 6d 69 74 3e 61 48 61 72 64 4c 69 6d 69 74  Limit>aHardLimit
33dcb 5b 6c 69 6d 69 74 49 64 5d 20 29 7b 0a 20 20 20  [limitId] ){.   
33dcc 20 20 20 6e 65 77 4c 69 6d 69 74 20 3d 20 61 48     newLimit = aH
33dcd 61 72 64 4c 69 6d 69 74 5b 6c 69 6d 69 74 49 64  ardLimit[limitId
33dce 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d  ];.    }.    db-
33dcf 3e 61 4c 69 6d 69 74 5b 6c 69 6d 69 74 49 64 5d  >aLimit[limitId]
33dd0 20 3d 20 6e 65 77 4c 69 6d 69 74 3b 0a 20 20 7d   = newLimit;.  }
33dd1 0a 20 20 72 65 74 75 72 6e 20 6f 6c 64 4c 69 6d  .  return oldLim
33dd2 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  it;.}../*.** Thi
33dd3 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74  s routine does t
33dd4 68 65 20 77 6f 72 6b 20 6f 66 20 6f 70 65 6e 69  he work of openi
33dd5 6e 67 20 61 20 64 61 74 61 62 61 73 65 20 6f 6e  ng a database on
33dd6 20 62 65 68 61 6c 66 20 6f 66 0a 2a 2a 20 73 71   behalf of.** sq
33dd7 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64  lite3_open() and
33dd8 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28   sqlite3_open16(
33dd9 29 2e 20 54 68 65 20 64 61 74 61 62 61 73 65 20  ). The database 
33dda 66 69 6c 65 6e 61 6d 65 20 22 7a 46 69 6c 65 6e  filename "zFilen
33ddb 61 6d 65 22 20 20 0a 2a 2a 20 69 73 20 55 54 46  ame"  .** is UTF
33ddc 2d 38 20 65 6e 63 6f 64 65 64 2e 0a 2a 2f 0a 73  -8 encoded..*/.s
33ddd 74 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 44 61  tatic int openDa
33dde 74 61 62 61 73 65 28 0a 20 20 63 6f 6e 73 74 20  tabase(.  const 
33ddf 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
33de0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c   /* Database fil
33de1 65 6e 61 6d 65 20 55 54 46 2d 38 20 65 6e 63 6f  ename UTF-8 enco
33de2 64 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ded */.  sqlite3
33de3 20 2a 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20   **ppDb,        
33de4 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 65 64  /* OUT: Returned
33de5 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
33de6 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 66   */.  unsigned f
33de7 6c 61 67 73 2c 20 20 20 20 20 20 20 20 2f 2a 20  lags,        /* 
33de8 4f 70 65 72 61 74 69 6f 6e 61 6c 20 66 6c 61 67  Operational flag
33de9 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  s */.  const cha
33dea 72 20 2a 7a 56 66 73 20 20 20 20 20 20 20 2f 2a  r *zVfs       /*
33deb 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 56 46 53   Name of the VFS
33dec 20 74 6f 20 75 73 65 20 2a 2f 0a 29 7b 0a 20 20   to use */.){.  
33ded 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69  sqlite3 *db;.  i
33dee 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 73 54  nt rc;.  int isT
33def 68 72 65 61 64 73 61 66 65 3b 0a 0a 20 20 2a 70  hreadsafe;..  *p
33df0 70 44 62 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66  pDb = 0;.#ifndef
33df1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
33df2 4f 49 4e 49 54 0a 20 20 72 63 20 3d 20 73 71 6c  OINIT.  rc = sql
33df3 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
33df4 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
33df5 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a  turn rc;.#endif.
33df6 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
33df7 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65  obalConfig.bCore
33df8 4d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20  Mutex==0 ){.    
33df9 69 73 54 68 72 65 61 64 73 61 66 65 20 3d 20 30  isThreadsafe = 0
33dfa 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c  ;.  }else if( fl
33dfb 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
33dfc 4e 5f 4e 4f 4d 55 54 45 58 20 29 7b 0a 20 20 20  N_NOMUTEX ){.   
33dfd 20 69 73 54 68 72 65 61 64 73 61 66 65 20 3d 20   isThreadsafe = 
33dfe 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66  0;.  }else if( f
33dff 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50  lags & SQLITE_OP
33e00 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20 29 7b 0a  EN_FULLMUTEX ){.
33e01 20 20 20 20 69 73 54 68 72 65 61 64 73 61 66 65      isThreadsafe
33e02 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 1;.  }else{. 
33e03 20 20 20 69 73 54 68 72 65 61 64 73 61 66 65 20     isThreadsafe 
33e04 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
33e05 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78  onfig.bFullMutex
33e06 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 67  ;.  }.  if( flag
33e07 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
33e08 50 52 49 56 41 54 45 43 41 43 48 45 20 29 7b 0a  PRIVATECACHE ){.
33e09 20 20 20 20 66 6c 61 67 73 20 26 3d 20 7e 53 51      flags &= ~SQ
33e0a 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44  LITE_OPEN_SHARED
33e0b 43 41 43 48 45 3b 0a 20 20 7d 65 6c 73 65 20 69  CACHE;.  }else i
33e0c 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
33e0d 43 6f 6e 66 69 67 2e 73 68 61 72 65 64 43 61 63  Config.sharedCac
33e0e 68 65 45 6e 61 62 6c 65 64 20 29 7b 0a 20 20 20  heEnabled ){.   
33e0f 20 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45   flags |= SQLITE
33e10 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48  _OPEN_SHAREDCACH
33e11 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6d  E;.  }..  /* Rem
33e12 6f 76 65 20 68 61 72 6d 66 75 6c 20 62 69 74 73  ove harmful bits
33e13 20 66 72 6f 6d 20 74 68 65 20 66 6c 61 67 73 20   from the flags 
33e14 70 61 72 61 6d 65 74 65 72 0a 20 20 2a 2a 0a 20  parameter.  **. 
33e15 20 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4f   ** The SQLITE_O
33e16 50 45 4e 5f 4e 4f 4d 55 54 45 58 20 61 6e 64 20  PEN_NOMUTEX and 
33e17 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c  SQLITE_OPEN_FULL
33e18 4d 55 54 45 58 20 66 6c 61 67 73 20 77 65 72 65  MUTEX flags were
33e19 0a 20 20 2a 2a 20 64 65 61 6c 74 20 77 69 74 68  .  ** dealt with
33e1a 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73   in the previous
33e1b 20 63 6f 64 65 20 62 6c 6f 63 6b 2e 20 20 42 65   code block.  Be
33e1c 73 69 64 65 73 20 74 68 65 73 65 2c 20 74 68 65  sides these, the
33e1d 20 6f 6e 6c 79 0a 20 20 2a 2a 20 76 61 6c 69 64   only.  ** valid
33e1e 20 69 6e 70 75 74 20 66 6c 61 67 73 20 66 6f 72   input flags for
33e1f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
33e20 28 29 20 61 72 65 20 53 51 4c 49 54 45 5f 4f 50  () are SQLITE_OP
33e21 45 4e 5f 52 45 41 44 4f 4e 4c 59 2c 0a 20 20 2a  EN_READONLY,.  *
33e22 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  * SQLITE_OPEN_RE
33e23 41 44 57 52 49 54 45 2c 20 61 6e 64 20 53 51 4c  ADWRITE, and SQL
33e24 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 2e  ITE_OPEN_CREATE.
33e25 20 20 53 69 6c 65 6e 74 6c 79 20 6d 61 73 6b 0a    Silently mask.
33e26 20 20 2a 2a 20 6f 66 66 20 61 6c 6c 20 6f 74 68    ** off all oth
33e27 65 72 20 66 6c 61 67 73 2e 0a 20 20 2a 2f 0a 20  er flags..  */. 
33e28 20 66 6c 61 67 73 20 26 3d 20 20 7e 28 20 53 51   flags &=  ~( SQ
33e29 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45  LITE_OPEN_DELETE
33e2a 4f 4e 43 4c 4f 53 45 20 7c 0a 20 20 20 20 20 20  ONCLOSE |.      
33e2b 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
33e2c 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c  OPEN_EXCLUSIVE |
33e2d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
33e2e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
33e2f 5f 44 42 20 7c 0a 20 20 20 20 20 20 20 20 20 20  _DB |.          
33e30 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
33e31 5f 54 45 4d 50 5f 44 42 20 7c 20 0a 20 20 20 20  _TEMP_DB | .    
33e32 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
33e33 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54  E_OPEN_TRANSIENT
33e34 5f 44 42 20 7c 20 0a 20 20 20 20 20 20 20 20 20  _DB | .         
33e35 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
33e36 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c  N_MAIN_JOURNAL |
33e37 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
33e38 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d   SQLITE_OPEN_TEM
33e39 50 5f 4a 4f 55 52 4e 41 4c 20 7c 20 0a 20 20 20  P_JOURNAL | .   
33e3a 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
33e3b 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e  TE_OPEN_SUBJOURN
33e3c 41 4c 20 7c 20 0a 20 20 20 20 20 20 20 20 20 20  AL | .          
33e3d 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
33e3e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20  _MASTER_JOURNAL 
33e3f 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  |.              
33e40 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d   SQLITE_OPEN_NOM
33e41 55 54 45 58 20 7c 0a 20 20 20 20 20 20 20 20 20  UTEX |.         
33e42 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
33e43 4e 5f 46 55 4c 4c 4d 55 54 45 58 0a 20 20 20 20  N_FULLMUTEX.    
33e44 20 20 20 20 20 20 20 20 20 29 3b 0a 0a 20 20 2f           );..  /
33e45 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 73  * Allocate the s
33e46 71 6c 69 74 65 20 64 61 74 61 20 73 74 72 75 63  qlite data struc
33e47 74 75 72 65 20 2a 2f 0a 20 20 64 62 20 3d 20 73  ture */.  db = s
33e48 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
33e49 28 20 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33  ( sizeof(sqlite3
33e4a 29 20 29 3b 0a 20 20 69 66 28 20 64 62 3d 3d 30  ) );.  if( db==0
33e4b 20 29 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f   ) goto opendb_o
33e4c 75 74 3b 0a 20 20 69 66 28 20 69 73 54 68 72 65  ut;.  if( isThre
33e4d 61 64 73 61 66 65 20 29 7b 0a 20 20 20 20 64 62  adsafe ){.    db
33e4e 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65  ->mutex = sqlite
33e4f 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
33e50 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
33e51 56 45 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d  VE);.    if( db-
33e52 3e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20  >mutex==0 ){.   
33e53 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
33e54 64 62 29 3b 0a 20 20 20 20 20 20 64 62 20 3d 20  db);.      db = 
33e55 30 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70  0;.      goto op
33e56 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  endb_out;.    }.
33e57 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
33e58 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
33e59 74 65 78 29 3b 0a 20 20 64 62 2d 3e 65 72 72 4d  tex);.  db->errM
33e5a 61 73 6b 20 3d 20 30 78 66 66 3b 0a 20 20 64 62  ask = 0xff;.  db
33e5b 2d 3e 6e 44 62 20 3d 20 32 3b 0a 20 20 64 62 2d  ->nDb = 2;.  db-
33e5c 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f  >magic = SQLITE_
33e5d 4d 41 47 49 43 5f 42 55 53 59 3b 0a 20 20 64 62  MAGIC_BUSY;.  db
33e5e 2d 3e 61 44 62 20 3d 20 64 62 2d 3e 61 44 62 53  ->aDb = db->aDbS
33e5f 74 61 74 69 63 3b 0a 0a 20 20 61 73 73 65 72 74  tatic;..  assert
33e60 28 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 4c 69  ( sizeof(db->aLi
33e61 6d 69 74 29 3d 3d 73 69 7a 65 6f 66 28 61 48 61  mit)==sizeof(aHa
33e62 72 64 4c 69 6d 69 74 29 20 29 3b 0a 20 20 6d 65  rdLimit) );.  me
33e63 6d 63 70 79 28 64 62 2d 3e 61 4c 69 6d 69 74 2c  mcpy(db->aLimit,
33e64 20 61 48 61 72 64 4c 69 6d 69 74 2c 20 73 69 7a   aHardLimit, siz
33e65 65 6f 66 28 64 62 2d 3e 61 4c 69 6d 69 74 29 29  eof(db->aLimit))
33e66 3b 0a 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  ;.  db->autoComm
33e67 69 74 20 3d 20 31 3b 0a 20 20 64 62 2d 3e 6e 65  it = 1;.  db->ne
33e68 78 74 41 75 74 6f 76 61 63 20 3d 20 2d 31 3b 0a  xtAutovac = -1;.
33e69 20 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69    db->nextPagesi
33e6a 7a 65 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 66 6c  ze = 0;.  db->fl
33e6b 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 53 68  ags |= SQLITE_Sh
33e6c 6f 72 74 43 6f 6c 4e 61 6d 65 73 0a 23 69 66 20  ortColNames.#if 
33e6d 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46  SQLITE_DEFAULT_F
33e6e 49 4c 45 5f 46 4f 52 4d 41 54 3c 34 0a 20 20 20  ILE_FORMAT<4.   
33e6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
33e70 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c  SQLITE_LegacyFil
33e71 65 46 6d 74 0a 23 65 6e 64 69 66 0a 23 69 66 64  eFmt.#endif.#ifd
33e72 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
33e73 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a  _LOAD_EXTENSION.
33e74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33e75 20 7c 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78   | SQLITE_LoadEx
33e76 74 65 6e 73 69 6f 6e 0a 23 65 6e 64 69 66 0a 23  tension.#endif.#
33e77 69 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  if SQLITE_DEFAUL
33e78 54 5f 52 45 43 55 52 53 49 56 45 5f 54 52 49 47  T_RECURSIVE_TRIG
33e79 47 45 52 53 0a 20 20 20 20 20 20 20 20 20 20 20  GERS.           
33e7a 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 52        | SQLITE_R
33e7b 65 63 54 72 69 67 67 65 72 73 0a 23 65 6e 64 69  ecTriggers.#endi
33e7c 66 0a 20 20 20 20 20 20 3b 0a 20 20 73 71 6c 69  f.      ;.  sqli
33e7d 74 65 33 48 61 73 68 49 6e 69 74 28 26 64 62 2d  te3HashInit(&db-
33e7e 3e 61 43 6f 6c 6c 53 65 71 29 3b 0a 23 69 66 6e  >aCollSeq);.#ifn
33e7f 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
33e80 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 73  VIRTUALTABLE.  s
33e81 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26  qlite3HashInit(&
33e82 64 62 2d 3e 61 4d 6f 64 75 6c 65 29 3b 0a 23 65  db->aModule);.#e
33e83 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 70 56 66 73  ndif..  db->pVfs
33e84 20 3d 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 66   = sqlite3_vfs_f
33e85 69 6e 64 28 7a 56 66 73 29 3b 0a 20 20 69 66 28  ind(zVfs);.  if(
33e86 20 21 64 62 2d 3e 70 56 66 73 20 29 7b 0a 20 20   !db->pVfs ){.  
33e87 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
33e88 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ROR;.    sqlite3
33e89 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 22 6e  Error(db, rc, "n
33e8a 6f 20 73 75 63 68 20 76 66 73 3a 20 25 73 22 2c  o such vfs: %s",
33e8b 20 7a 56 66 73 29 3b 0a 20 20 20 20 67 6f 74 6f   zVfs);.    goto
33e8c 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d   opendb_out;.  }
33e8d 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 64  ..  /* Add the d
33e8e 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e  efault collation
33e8f 20 73 65 71 75 65 6e 63 65 20 42 49 4e 41 52 59   sequence BINARY
33e90 2e 20 42 49 4e 41 52 59 20 77 6f 72 6b 73 20 66  . BINARY works f
33e91 6f 72 20 62 6f 74 68 20 55 54 46 2d 38 0a 20 20  or both UTF-8.  
33e92 2a 2a 20 61 6e 64 20 55 54 46 2d 31 36 2c 20 73  ** and UTF-16, s
33e93 6f 20 61 64 64 20 61 20 76 65 72 73 69 6f 6e 20  o add a version 
33e94 66 6f 72 20 65 61 63 68 20 74 6f 20 61 76 6f 69  for each to avoi
33e95 64 20 61 6e 79 20 75 6e 6e 65 63 65 73 73 61 72  d any unnecessar
33e96 79 0a 20 20 2a 2a 20 63 6f 6e 76 65 72 73 69 6f  y.  ** conversio
33e97 6e 73 2e 20 54 68 65 20 6f 6e 6c 79 20 65 72 72  ns. The only err
33e98 6f 72 20 74 68 61 74 20 63 61 6e 20 6f 63 63 75  or that can occu
33e99 72 20 68 65 72 65 20 69 73 20 61 20 6d 61 6c 6c  r here is a mall
33e9a 6f 63 28 29 20 66 61 69 6c 75 72 65 2e 0a 20 20  oc() failure..  
33e9b 2a 2f 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c 61  */.  createColla
33e9c 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e 41 52 59  tion(db, "BINARY
33e9d 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  ", SQLITE_UTF8, 
33e9e 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41  SQLITE_COLL_BINA
33e9f 52 59 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20  RY, 0,.         
33ea0 20 20 20 20 20 20 20 20 20 62 69 6e 43 6f 6c 6c           binColl
33ea1 46 75 6e 63 2c 20 30 29 3b 0a 20 20 63 72 65 61  Func, 0);.  crea
33ea2 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20  teCollation(db, 
33ea3 22 42 49 4e 41 52 59 22 2c 20 53 51 4c 49 54 45  "BINARY", SQLITE
33ea4 5f 55 54 46 31 36 42 45 2c 20 53 51 4c 49 54 45  _UTF16BE, SQLITE
33ea5 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 2c 20 30 2c  _COLL_BINARY, 0,
33ea6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
33ea7 20 20 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20     binCollFunc, 
33ea8 30 29 3b 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c  0);.  createColl
33ea9 61 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e 41 52  ation(db, "BINAR
33eaa 59 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  Y", SQLITE_UTF16
33eab 4c 45 2c 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f  LE, SQLITE_COLL_
33eac 42 49 4e 41 52 59 2c 20 30 2c 0a 20 20 20 20 20  BINARY, 0,.     
33ead 20 20 20 20 20 20 20 20 20 20 20 20 20 62 69 6e               bin
33eae 43 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20 20  CollFunc, 0);.  
33eaf 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28  createCollation(
33eb0 64 62 2c 20 22 52 54 52 49 4d 22 2c 20 53 51 4c  db, "RTRIM", SQL
33eb1 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45  ITE_UTF8, SQLITE
33eb2 5f 43 4f 4c 4c 5f 55 53 45 52 2c 20 28 76 6f 69  _COLL_USER, (voi
33eb3 64 2a 29 31 2c 0a 20 20 20 20 20 20 20 20 20 20  d*)1,.          
33eb4 20 20 20 20 20 20 20 20 62 69 6e 43 6f 6c 6c 46          binCollF
33eb5 75 6e 63 2c 20 30 29 3b 0a 20 20 69 66 28 20 64  unc, 0);.  if( d
33eb6 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
33eb7 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e  ){.    goto open
33eb8 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 64 62  db_out;.  }.  db
33eb9 2d 3e 70 44 66 6c 74 43 6f 6c 6c 20 3d 20 73 71  ->pDfltColl = sq
33eba 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
33ebb 28 64 62 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  (db, SQLITE_UTF8
33ebc 2c 20 22 42 49 4e 41 52 59 22 2c 20 30 29 3b 0a  , "BINARY", 0);.
33ebd 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 70 44    assert( db->pD
33ebe 66 6c 74 43 6f 6c 6c 21 3d 30 20 29 3b 0a 0a 20  fltColl!=0 );.. 
33ebf 20 2f 2a 20 41 6c 73 6f 20 61 64 64 20 61 20 55   /* Also add a U
33ec0 54 46 2d 38 20 63 61 73 65 2d 69 6e 73 65 6e 73  TF-8 case-insens
33ec1 69 74 69 76 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  itive collation 
33ec2 73 65 71 75 65 6e 63 65 2e 20 2a 2f 0a 20 20 63  sequence. */.  c
33ec3 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64  reateCollation(d
33ec4 62 2c 20 22 4e 4f 43 41 53 45 22 2c 20 53 51 4c  b, "NOCASE", SQL
33ec5 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45  ITE_UTF8, SQLITE
33ec6 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 2c 20 30 2c  _COLL_NOCASE, 0,
33ec7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
33ec8 20 20 20 6e 6f 63 61 73 65 43 6f 6c 6c 61 74 69     nocaseCollati
33ec9 6e 67 46 75 6e 63 2c 20 30 29 3b 0a 0a 20 20 2f  ngFunc, 0);..  /
33eca 2a 20 4f 70 65 6e 20 74 68 65 20 62 61 63 6b 65  * Open the backe
33ecb 6e 64 20 64 61 74 61 62 61 73 65 20 64 72 69 76  nd database driv
33ecc 65 72 20 2a 2f 0a 20 20 64 62 2d 3e 6f 70 65 6e  er */.  db->open
33ecd 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20  Flags = flags;. 
33ece 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
33ecf 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20 7a 46  eeFactory(db, zF
33ed0 69 6c 65 6e 61 6d 65 2c 20 30 2c 20 53 51 4c 49  ilename, 0, SQLI
33ed1 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45  TE_DEFAULT_CACHE
33ed2 5f 53 49 5a 45 2c 20 0a 20 20 20 20 20 20 20 20  _SIZE, .        
33ed3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33ed4 20 20 20 66 6c 61 67 73 20 7c 20 53 51 4c 49 54     flags | SQLIT
33ed5 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 2c 0a  E_OPEN_MAIN_DB,.
33ed6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33ed7 20 20 20 20 20 20 20 20 20 20 20 26 64 62 2d 3e             &db->
33ed8 61 44 62 5b 30 5d 2e 70 42 74 29 3b 0a 20 20 69  aDb[0].pBt);.  i
33ed9 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
33eda 20 29 7b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   ){.    if( rc==
33edb 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d  SQLITE_IOERR_NOM
33edc 45 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  EM ){.      rc =
33edd 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
33ede 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
33edf 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 30 29  Error(db, rc, 0)
33ee0 3b 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64  ;.    goto opend
33ee1 62 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 64 62 2d  b_out;.  }.  db-
33ee2 3e 61 44 62 5b 30 5d 2e 70 53 63 68 65 6d 61 20  >aDb[0].pSchema 
33ee3 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47  = sqlite3SchemaG
33ee4 65 74 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b 30  et(db, db->aDb[0
33ee5 5d 2e 70 42 74 29 3b 0a 20 20 64 62 2d 3e 61 44  ].pBt);.  db->aD
33ee6 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 3d 20 73  b[1].pSchema = s
33ee7 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74 28  qlite3SchemaGet(
33ee8 64 62 2c 20 30 29 3b 0a 0a 0a 20 20 2f 2a 20 54  db, 0);...  /* T
33ee9 68 65 20 64 65 66 61 75 6c 74 20 73 61 66 65 74  he default safet
33eea 79 5f 6c 65 76 65 6c 20 66 6f 72 20 74 68 65 20  y_level for the 
33eeb 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 69 73  main database is
33eec 20 27 66 75 6c 6c 27 3b 20 66 6f 72 20 74 68 65   'full'; for the
33eed 20 74 65 6d 70 0a 20 20 2a 2a 20 64 61 74 61 62   temp.  ** datab
33eee 61 73 65 20 69 74 20 69 73 20 27 4e 4f 4e 45 27  ase it is 'NONE'
33eef 2e 20 54 68 69 73 20 6d 61 74 63 68 65 73 20 74  . This matches t
33ef0 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 20 64  he pager layer d
33ef1 65 66 61 75 6c 74 73 2e 20 20 0a 20 20 2a 2f 0a  efaults.  .  */.
33ef2 20 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 7a 4e 61    db->aDb[0].zNa
33ef3 6d 65 20 3d 20 22 6d 61 69 6e 22 3b 0a 20 20 64  me = "main";.  d
33ef4 62 2d 3e 61 44 62 5b 30 5d 2e 73 61 66 65 74 79  b->aDb[0].safety
33ef5 5f 6c 65 76 65 6c 20 3d 20 33 3b 0a 20 20 64 62  _level = 3;.  db
33ef6 2d 3e 61 44 62 5b 31 5d 2e 7a 4e 61 6d 65 20 3d  ->aDb[1].zName =
33ef7 20 22 74 65 6d 70 22 3b 0a 20 20 64 62 2d 3e 61   "temp";.  db->a
33ef8 44 62 5b 31 5d 2e 73 61 66 65 74 79 5f 6c 65 76  Db[1].safety_lev
33ef9 65 6c 20 3d 20 31 3b 0a 0a 20 20 64 62 2d 3e 6d  el = 1;..  db->m
33efa 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41  agic = SQLITE_MA
33efb 47 49 43 5f 4f 50 45 4e 3b 0a 20 20 69 66 28 20  GIC_OPEN;.  if( 
33efc 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
33efd 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65   ){.    goto ope
33efe 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  ndb_out;.  }..  
33eff 2f 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c 20  /* Register all 
33f00 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f  built-in functio
33f01 6e 73 2c 20 62 75 74 20 64 6f 20 6e 6f 74 20 61  ns, but do not a
33f02 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 74  ttempt to read t
33f03 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65  he.  ** database
33f04 20 73 63 68 65 6d 61 20 79 65 74 2e 20 54 68 69   schema yet. Thi
33f05 73 20 69 73 20 64 65 6c 61 79 65 64 20 75 6e 74  s is delayed unt
33f06 69 6c 20 74 68 65 20 66 69 72 73 74 20 74 69 6d  il the first tim
33f07 65 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20  e the database. 
33f08 20 2a 2a 20 69 73 20 61 63 63 65 73 73 65 64 2e   ** is accessed.
33f09 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45  .  */.  sqlite3E
33f0a 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
33f0b 4f 4b 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65  OK, 0);.  sqlite
33f0c 33 52 65 67 69 73 74 65 72 42 75 69 6c 74 69 6e  3RegisterBuiltin
33f0d 46 75 6e 63 74 69 6f 6e 73 28 64 62 29 3b 0a 0a  Functions(db);..
33f0e 20 20 2f 2a 20 4c 6f 61 64 20 61 75 74 6f 6d 61    /* Load automa
33f0f 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 20 2d  tic extensions -
33f10 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 68 61 74   extensions that
33f11 20 68 61 76 65 20 62 65 65 6e 20 72 65 67 69 73   have been regis
33f12 74 65 72 65 64 0a 20 20 2a 2a 20 75 73 69 6e 67  tered.  ** using
33f13 20 74 68 65 20 73 71 6c 69 74 65 33 5f 61 75 74   the sqlite3_aut
33f14 6f 6d 61 74 69 63 5f 65 78 74 65 6e 73 69 6f 6e  omatic_extension
33f15 28 29 20 41 50 49 2e 0a 20 20 2a 2f 0a 20 20 73  () API..  */.  s
33f16 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64 45 78  qlite3AutoLoadEx
33f17 74 65 6e 73 69 6f 6e 73 28 64 62 29 3b 0a 20 20  tensions(db);.  
33f18 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 72 72  rc = sqlite3_err
33f19 63 6f 64 65 28 64 62 29 3b 0a 20 20 69 66 28 20  code(db);.  if( 
33f1a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
33f1b 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62  .    goto opendb
33f1c 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 23 69 66 64 65  _out;.  }..#ifde
33f1d 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
33f1e 46 54 53 31 0a 20 20 69 66 28 20 21 64 62 2d 3e  FTS1.  if( !db->
33f1f 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
33f20 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73      extern int s
33f21 71 6c 69 74 65 33 46 74 73 31 49 6e 69 74 28 73  qlite3Fts1Init(s
33f22 71 6c 69 74 65 33 2a 29 3b 0a 20 20 20 20 72 63  qlite3*);.    rc
33f23 20 3d 20 73 71 6c 69 74 65 33 46 74 73 31 49 6e   = sqlite3Fts1In
33f24 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64  it(db);.  }.#end
33f25 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
33f26 45 5f 45 4e 41 42 4c 45 5f 46 54 53 32 0a 20 20  E_ENABLE_FTS2.  
33f27 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  if( !db->mallocF
33f28 61 69 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c  ailed && rc==SQL
33f29 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 65 78  ITE_OK ){.    ex
33f2a 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33  tern int sqlite3
33f2b 46 74 73 32 49 6e 69 74 28 73 71 6c 69 74 65 33  Fts2Init(sqlite3
33f2c 2a 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  *);.    rc = sql
33f2d 69 74 65 33 46 74 73 32 49 6e 69 74 28 64 62 29  ite3Fts2Init(db)
33f2e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ;.  }.#endif..#i
33f2f 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
33f30 4c 45 5f 46 54 53 33 0a 20 20 69 66 28 20 21 64  LE_FTS3.  if( !d
33f31 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
33f32 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
33f33 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
33f34 69 74 65 33 46 74 73 33 49 6e 69 74 28 64 62 29  ite3Fts3Init(db)
33f35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ;.  }.#endif..#i
33f36 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
33f37 4c 45 5f 49 43 55 0a 20 20 69 66 28 20 21 64 62  LE_ICU.  if( !db
33f38 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26  ->mallocFailed &
33f39 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  & rc==SQLITE_OK 
33f3a 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
33f3b 74 65 33 49 63 75 49 6e 69 74 28 64 62 29 3b 0a  te3IcuInit(db);.
33f3c 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64    }.#endif..#ifd
33f3d 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
33f3e 5f 52 54 52 45 45 0a 20 20 69 66 28 20 21 64 62  _RTREE.  if( !db
33f3f 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26  ->mallocFailed &
33f40 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29  & rc==SQLITE_OK)
33f41 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
33f42 65 33 52 74 72 65 65 49 6e 69 74 28 64 62 29 3b  e3RtreeInit(db);
33f43 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 73  .  }.#endif..  s
33f44 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
33f45 72 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 2d 44  rc, 0);..  /* -D
33f46 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c  SQLITE_DEFAULT_L
33f47 4f 43 4b 49 4e 47 5f 4d 4f 44 45 3d 31 20 6d 61  OCKING_MODE=1 ma
33f48 6b 65 73 20 45 58 43 4c 55 53 49 56 45 20 74 68  kes EXCLUSIVE th
33f49 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e  e default lockin
33f4a 67 0a 20 20 2a 2a 20 6d 6f 64 65 2e 20 20 2d 44  g.  ** mode.  -D
33f4b 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c  SQLITE_DEFAULT_L
33f4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 3d 30 20 6d 61  OCKING_MODE=0 ma
33f4d 6b 65 20 4e 4f 52 4d 41 4c 20 74 68 65 20 64 65  ke NORMAL the de
33f4e 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 0a 20 20  fault locking.  
33f4f 2a 2a 20 6d 6f 64 65 2e 20 20 44 6f 69 6e 67 20  ** mode.  Doing 
33f50 6e 6f 74 68 69 6e 67 20 61 74 20 61 6c 6c 20 61  nothing at all a
33f51 6c 73 6f 20 6d 61 6b 65 73 20 4e 4f 52 4d 41 4c  lso makes NORMAL
33f52 20 74 68 65 20 64 65 66 61 75 6c 74 2e 0a 20 20   the default..  
33f53 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
33f54 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47  _DEFAULT_LOCKING
33f55 5f 4d 4f 44 45 0a 20 20 64 62 2d 3e 64 66 6c 74  _MODE.  db->dflt
33f56 4c 6f 63 6b 4d 6f 64 65 20 3d 20 53 51 4c 49 54  LockMode = SQLIT
33f57 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e  E_DEFAULT_LOCKIN
33f58 47 5f 4d 4f 44 45 3b 0a 20 20 73 71 6c 69 74 65  G_MODE;.  sqlite
33f59 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64  3PagerLockingMod
33f5a 65 28 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  e(sqlite3BtreePa
33f5b 67 65 72 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70  ger(db->aDb[0].p
33f5c 42 74 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Bt),.           
33f5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
33f5e 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f  QLITE_DEFAULT_LO
33f5f 43 4b 49 4e 47 5f 4d 4f 44 45 29 3b 0a 23 65 6e  CKING_MODE);.#en
33f60 64 69 66 0a 0a 20 20 2f 2a 20 45 6e 61 62 6c 65  dif..  /* Enable
33f61 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 2d 6d   the lookaside-m
33f62 61 6c 6c 6f 63 20 73 75 62 73 79 73 74 65 6d 20  alloc subsystem 
33f63 2a 2f 0a 20 20 73 65 74 75 70 4c 6f 6f 6b 61 73  */.  setupLookas
33f64 69 64 65 28 64 62 2c 20 30 2c 20 73 71 6c 69 74  ide(db, 0, sqlit
33f65 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73  e3GlobalConfig.s
33f66 7a 4c 6f 6f 6b 61 73 69 64 65 2c 0a 20 20 20 20  zLookaside,.    
33f67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33f68 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
33f69 6c 43 6f 6e 66 69 67 2e 6e 4c 6f 6f 6b 61 73 69  lConfig.nLookasi
33f6a 64 65 29 3b 0a 0a 6f 70 65 6e 64 62 5f 6f 75 74  de);..opendb_out
33f6b 3a 0a 20 20 69 66 28 20 64 62 20 29 7b 0a 20 20  :.  if( db ){.  
33f6c 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 75    assert( db->mu
33f6d 74 65 78 21 3d 30 20 7c 7c 20 69 73 54 68 72 65  tex!=0 || isThre
33f6e 61 64 73 61 66 65 3d 3d 30 20 7c 7c 20 73 71 6c  adsafe==0 || sql
33f6f 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
33f70 2e 62 46 75 6c 6c 4d 75 74 65 78 3d 3d 30 20 29  .bFullMutex==0 )
33f71 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
33f72 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
33f73 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d  tex);.  }.  rc =
33f74 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65   sqlite3_errcode
33f75 28 64 62 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  (db);.  if( rc==
33f76 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a  SQLITE_NOMEM ){.
33f77 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73      sqlite3_clos
33f78 65 28 64 62 29 3b 0a 20 20 20 20 64 62 20 3d 20  e(db);.    db = 
33f79 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72  0;.  }else if( r
33f7a 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
33f7b 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20      db->magic = 
33f7c 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49 43  SQLITE_MAGIC_SIC
33f7d 4b 3b 0a 20 20 7d 0a 20 20 2a 70 70 44 62 20 3d  K;.  }.  *ppDb =
33f7e 20 64 62 3b 0a 20 20 72 65 74 75 72 6e 20 73 71   db;.  return sq
33f7f 6c 69 74 65 33 41 70 69 45 78 69 74 28 30 2c 20  lite3ApiExit(0, 
33f80 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70  rc);.}../*.** Op
33f81 65 6e 20 61 20 6e 65 77 20 64 61 74 61 62 61 73  en a new databas
33f82 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c  e handle..*/.SQL
33f83 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
33f84 74 65 33 5f 6f 70 65 6e 28 0a 20 20 63 6f 6e 73  te3_open(.  cons
33f85 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
33f86 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a  e, .  sqlite3 **
33f87 70 70 44 62 20 0a 29 7b 0a 20 20 72 65 74 75 72  ppDb .){.  retur
33f88 6e 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28 7a  n openDatabase(z
33f89 46 69 6c 65 6e 61 6d 65 2c 20 70 70 44 62 2c 0a  Filename, ppDb,.
33f8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33f8b 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
33f8c 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53 51  N_READWRITE | SQ
33f8d 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
33f8e 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  , 0);.}.SQLITE_A
33f8f 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
33f90 70 65 6e 5f 76 32 28 0a 20 20 63 6f 6e 73 74 20  pen_v2(.  const 
33f91 63 68 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20  char *filename, 
33f92 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69    /* Database fi
33f93 6c 65 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a  lename (UTF-8) *
33f94 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70  /.  sqlite3 **pp
33f95 44 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  Db,         /* O
33f96 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61  UT: SQLite db ha
33f97 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  ndle */.  int fl
33f98 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  ags,            
33f99 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 20    /* Flags */.  
33f9a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73  const char *zVfs
33f9b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
33f9c 6f 66 20 56 46 53 20 6d 6f 64 75 6c 65 20 74 6f  of VFS module to
33f9d 20 75 73 65 20 2a 2f 0a 29 7b 0a 20 20 72 65 74   use */.){.  ret
33f9e 75 72 6e 20 6f 70 65 6e 44 61 74 61 62 61 73 65  urn openDatabase
33f9f 28 66 69 6c 65 6e 61 6d 65 2c 20 70 70 44 62 2c  (filename, ppDb,
33fa0 20 66 6c 61 67 73 2c 20 7a 56 66 73 29 3b 0a 7d   flags, zVfs);.}
33fa1 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
33fa2 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a  _OMIT_UTF16./*.*
33fa3 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 64 61 74  * Open a new dat
33fa4 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f  abase handle..*/
33fa5 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
33fa6 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 0a  sqlite3_open16(.
33fa7 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46    const void *zF
33fa8 69 6c 65 6e 61 6d 65 2c 20 0a 20 20 73 71 6c 69  ilename, .  sqli
33fa9 74 65 33 20 2a 2a 70 70 44 62 0a 29 7b 0a 20 20  te3 **ppDb.){.  
33faa 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46 69 6c  char const *zFil
33fab 65 6e 61 6d 65 38 3b 20 20 20 2f 2a 20 7a 46 69  ename8;   /* zFi
33fac 6c 65 6e 61 6d 65 20 65 6e 63 6f 64 65 64 20 69  lename encoded i
33fad 6e 20 55 54 46 2d 38 20 69 6e 73 74 65 61 64 20  n UTF-8 instead 
33fae 6f 66 20 55 54 46 2d 31 36 20 2a 2f 0a 20 20 73  of UTF-16 */.  s
33faf 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
33fb0 61 6c 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  al;.  int rc;.. 
33fb1 20 61 73 73 65 72 74 28 20 7a 46 69 6c 65 6e 61   assert( zFilena
33fb2 6d 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  me );.  assert( 
33fb3 70 70 44 62 20 29 3b 0a 20 20 2a 70 70 44 62 20  ppDb );.  *ppDb 
33fb4 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  = 0;.#ifndef SQL
33fb5 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49  ITE_OMIT_AUTOINI
33fb6 54 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  T.  rc = sqlite3
33fb7 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20  _initialize();. 
33fb8 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
33fb9 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20 20 70 56   rc;.#endif.  pV
33fba 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75  al = sqlite3Valu
33fbb 65 4e 65 77 28 30 29 3b 0a 20 20 73 71 6c 69 74  eNew(0);.  sqlit
33fbc 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 70 56  e3ValueSetStr(pV
33fbd 61 6c 2c 20 2d 31 2c 20 7a 46 69 6c 65 6e 61 6d  al, -1, zFilenam
33fbe 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e  e, SQLITE_UTF16N
33fbf 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 53 54  ATIVE, SQLITE_ST
33fc0 41 54 49 43 29 3b 0a 20 20 7a 46 69 6c 65 6e 61  ATIC);.  zFilena
33fc1 6d 65 38 20 3d 20 73 71 6c 69 74 65 33 56 61 6c  me8 = sqlite3Val
33fc2 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c  ueText(pVal, SQL
33fc3 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69 66 28  ITE_UTF8);.  if(
33fc4 20 7a 46 69 6c 65 6e 61 6d 65 38 20 29 7b 0a 20   zFilename8 ){. 
33fc5 20 20 20 72 63 20 3d 20 6f 70 65 6e 44 61 74 61     rc = openData
33fc6 62 61 73 65 28 7a 46 69 6c 65 6e 61 6d 65 38 2c  base(zFilename8,
33fc7 20 70 70 44 62 2c 0a 20 20 20 20 20 20 20 20 20   ppDb,.         
33fc8 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
33fc9 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
33fca 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e  TE | SQLITE_OPEN
33fcb 5f 43 52 45 41 54 45 2c 20 30 29 3b 0a 20 20 20  _CREATE, 0);.   
33fcc 20 61 73 73 65 72 74 28 20 2a 70 70 44 62 20 7c   assert( *ppDb |
33fcd 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  | rc==SQLITE_NOM
33fce 45 4d 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63  EM );.    if( rc
33fcf 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21  ==SQLITE_OK && !
33fd0 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 2a 70  DbHasProperty(*p
33fd1 70 44 62 2c 20 30 2c 20 44 42 5f 53 63 68 65 6d  pDb, 0, DB_Schem
33fd2 61 4c 6f 61 64 65 64 29 20 29 7b 0a 20 20 20 20  aLoaded) ){.    
33fd3 20 20 45 4e 43 28 2a 70 70 44 62 29 20 3d 20 53    ENC(*ppDb) = S
33fd4 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
33fd5 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  E;.    }.  }else
33fd6 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
33fd7 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 73  E_NOMEM;.  }.  s
33fd8 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
33fd9 70 56 61 6c 29 3b 0a 0a 20 20 72 65 74 75 72 6e  pVal);..  return
33fda 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
33fdb 30 2c 20 72 63 29 3b 0a 7d 0a 23 65 6e 64 69 66  0, rc);.}.#endif
33fdc 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
33fdd 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  UTF16 */../*.** 
33fde 52 65 67 69 73 74 65 72 20 61 20 6e 65 77 20 63  Register a new c
33fdf 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
33fe0 65 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62  e with the datab
33fe1 61 73 65 20 68 61 6e 64 6c 65 20 64 62 2e 0a 2a  ase handle db..*
33fe2 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
33fe3 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
33fe4 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71 6c  collation(.  sql
33fe5 69 74 65 33 2a 20 64 62 2c 20 0a 20 20 63 6f 6e  ite3* db, .  con
33fe6 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
33fe7 0a 20 20 69 6e 74 20 65 6e 63 2c 20 0a 20 20 76  .  int enc, .  v
33fe8 6f 69 64 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74  oid* pCtx,.  int
33fe9 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64  (*xCompare)(void
33fea 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
33feb 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
33fec 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  *).){.  int rc;.
33fed 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
33fee 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
33fef 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d  ;.  assert( !db-
33ff0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
33ff1 0a 20 20 72 63 20 3d 20 63 72 65 61 74 65 43 6f  .  rc = createCo
33ff2 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d  llation(db, zNam
33ff3 65 2c 20 28 75 38 29 65 6e 63 2c 20 53 51 4c 49  e, (u8)enc, SQLI
33ff4 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 2c 20 70 43  TE_COLL_USER, pC
33ff5 74 78 2c 20 78 43 6f 6d 70 61 72 65 2c 20 30 29  tx, xCompare, 0)
33ff6 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
33ff7 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b  ApiExit(db, rc);
33ff8 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
33ff9 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
33ffa 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
33ffb 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65  }../*.** Registe
33ffc 72 20 61 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f  r a new collatio
33ffd 6e 20 73 65 71 75 65 6e 63 65 20 77 69 74 68 20  n sequence with 
33ffe 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
33fff 64 6c 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49 54  dle db..*/.SQLIT
34000 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
34001 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
34002 6f 6e 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33  on_v2(.  sqlite3
34003 2a 20 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63  * db, .  const c
34004 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69  har *zName, .  i
34005 6e 74 20 65 6e 63 2c 20 0a 20 20 76 6f 69 64 2a  nt enc, .  void*
34006 20 70 43 74 78 2c 0a 20 20 69 6e 74 28 2a 78 43   pCtx,.  int(*xC
34007 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e  ompare)(void*,in
34008 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  t,const void*,in
34009 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 0a  t,const void*),.
3400a 20 20 76 6f 69 64 28 2a 78 44 65 6c 29 28 76 6f    void(*xDel)(vo
3400b 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63  id*).){.  int rc
3400c 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
3400d 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
3400e 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64  x);.  assert( !d
3400f 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
34010 29 3b 0a 20 20 72 63 20 3d 20 63 72 65 61 74 65  );.  rc = create
34011 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 7a 4e  Collation(db, zN
34012 61 6d 65 2c 20 28 75 38 29 65 6e 63 2c 20 53 51  ame, (u8)enc, SQ
34013 4c 49 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 2c 20  LITE_COLL_USER, 
34014 70 43 74 78 2c 20 78 43 6f 6d 70 61 72 65 2c 20  pCtx, xCompare, 
34015 78 44 65 6c 29 3b 0a 20 20 72 63 20 3d 20 73 71  xDel);.  rc = sq
34016 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c  lite3ApiExit(db,
34017 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f   rc);.  sqlite3_
34018 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
34019 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
3401a 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20   rc;.}..#ifndef 
3401b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
3401c 36 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  6./*.** Register
3401d 20 61 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e   a new collation
3401e 20 73 65 71 75 65 6e 63 65 20 77 69 74 68 20 74   sequence with t
3401f 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
34020 6c 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45  le db..*/.SQLITE
34021 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
34022 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
34023 6e 31 36 28 0a 20 20 73 71 6c 69 74 65 33 2a 20  n16(.  sqlite3* 
34024 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69  db, .  const voi
34025 64 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  d *zName,.  int 
34026 65 6e 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70 43  enc, .  void* pC
34027 74 78 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70  tx,.  int(*xComp
34028 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  are)(void*,int,c
34029 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63  onst void*,int,c
3402a 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 7b 0a 20  onst void*).){. 
3402b 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
3402c 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61  _OK;.  char *zNa
3402d 6d 65 38 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  me8;.  sqlite3_m
3402e 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
3402f 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28  utex);.  assert(
34030 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
34031 65 64 20 29 3b 0a 20 20 7a 4e 61 6d 65 38 20 3d  ed );.  zName8 =
34032 20 73 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38   sqlite3Utf16to8
34033 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 2d 31 29 3b  (db, zName, -1);
34034 0a 20 20 69 66 28 20 7a 4e 61 6d 65 38 20 29 7b  .  if( zName8 ){
34035 0a 20 20 20 20 72 63 20 3d 20 63 72 65 61 74 65  .    rc = create
34036 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 7a 4e  Collation(db, zN
34037 61 6d 65 38 2c 20 28 75 38 29 65 6e 63 2c 20 53  ame8, (u8)enc, S
34038 51 4c 49 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 2c  QLITE_COLL_USER,
34039 20 70 43 74 78 2c 20 78 43 6f 6d 70 61 72 65 2c   pCtx, xCompare,
3403a 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
3403b 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65  DbFree(db, zName
3403c 38 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73  8);.  }.  rc = s
3403d 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62  qlite3ApiExit(db
3403e 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33  , rc);.  sqlite3
3403f 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
34040 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
34041 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  n rc;.}.#endif /
34042 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
34043 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65  F16 */../*.** Re
34044 67 69 73 74 65 72 20 61 20 63 6f 6c 6c 61 74 69  gister a collati
34045 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 61 63 74  on sequence fact
34046 6f 72 79 20 63 61 6c 6c 62 61 63 6b 20 77 69 74  ory callback wit
34047 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  h the database h
34048 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20 52 65 70  andle.** db. Rep
34049 6c 61 63 65 20 61 6e 79 20 70 72 65 76 69 6f 75  lace any previou
3404a 73 6c 79 20 69 6e 73 74 61 6c 6c 65 64 20 63 6f  sly installed co
3404b 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
3404c 20 66 61 63 74 6f 72 79 2e 0a 2a 2f 0a 53 51 4c   factory..*/.SQL
3404d 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
3404e 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65  te3_collation_ne
3404f 65 64 65 64 28 0a 20 20 73 71 6c 69 74 65 33 20  eded(.  sqlite3 
34050 2a 64 62 2c 20 0a 20 20 76 6f 69 64 20 2a 70 43  *db, .  void *pC
34051 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c 20 0a 20  ollNeededArg, . 
34052 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64   void(*xCollNeed
34053 65 64 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65  ed)(void*,sqlite
34054 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c  3*,int eTextRep,
34055 63 6f 6e 73 74 20 63 68 61 72 2a 29 0a 29 7b 0a  const char*).){.
34056 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
34057 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
34058 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65  ;.  db->xCollNee
34059 64 65 64 20 3d 20 78 43 6f 6c 6c 4e 65 65 64 65  ded = xCollNeede
3405a 64 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65  d;.  db->xCollNe
3405b 65 64 65 64 31 36 20 3d 20 30 3b 0a 20 20 64 62  eded16 = 0;.  db
3405c 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67  ->pCollNeededArg
3405d 20 3d 20 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72   = pCollNeededAr
3405e 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  g;.  sqlite3_mut
3405f 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
34060 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ex);.  return SQ
34061 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e  LITE_OK;.}..#ifn
34062 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
34063 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 52 65 67 69  UTF16./*.** Regi
34064 73 74 65 72 20 61 20 63 6f 6c 6c 61 74 69 6f 6e  ster a collation
34065 20 73 65 71 75 65 6e 63 65 20 66 61 63 74 6f 72   sequence factor
34066 79 20 63 61 6c 6c 62 61 63 6b 20 77 69 74 68 20  y callback with 
34067 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
34068 64 6c 65 0a 2a 2a 20 64 62 2e 20 52 65 70 6c 61  dle.** db. Repla
34069 63 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 6c  ce any previousl
3406a 79 20 69 6e 73 74 61 6c 6c 65 64 20 63 6f 6c 6c  y installed coll
3406b 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66  ation sequence f
3406c 61 63 74 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54  actory..*/.SQLIT
3406d 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3406e 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64  3_collation_need
3406f 65 64 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20  ed16(.  sqlite3 
34070 2a 64 62 2c 20 0a 20 20 76 6f 69 64 20 2a 70 43  *db, .  void *pC
34071 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c 20 0a 20  ollNeededArg, . 
34072 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64   void(*xCollNeed
34073 65 64 31 36 29 28 76 6f 69 64 2a 2c 73 71 6c 69  ed16)(void*,sqli
34074 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65  te3*,int eTextRe
34075 70 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29  p,const void*).)
34076 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
34077 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
34078 78 29 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e  x);.  db->xCollN
34079 65 65 64 65 64 20 3d 20 30 3b 0a 20 20 64 62 2d  eeded = 0;.  db-
3407a 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20 3d  >xCollNeeded16 =
3407b 20 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 3b 0a   xCollNeeded16;.
3407c 20 20 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65    db->pCollNeede
3407d 64 41 72 67 20 3d 20 70 43 6f 6c 6c 4e 65 65 64  dArg = pCollNeed
3407e 65 64 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33  edArg;.  sqlite3
3407f 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
34080 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
34081 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23  n SQLITE_OK;.}.#
34082 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
34083 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 23  OMIT_UTF16 */..#
34084 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
34085 49 54 5f 47 4c 4f 42 41 4c 52 45 43 4f 56 45 52  IT_GLOBALRECOVER
34086 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
34087 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a  OMIT_DEPRECATED.
34088 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
34089 69 6f 6e 20 69 73 20 6e 6f 77 20 61 6e 20 61 6e  ion is now an an
3408a 61 63 68 72 6f 6e 69 73 6d 2e 20 49 74 20 75 73  achronism. It us
3408b 65 64 20 74 6f 20 62 65 20 75 73 65 64 20 74 6f  ed to be used to
3408c 20 72 65 63 6f 76 65 72 20 66 72 6f 6d 20 61 0a   recover from a.
3408d 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c  ** malloc() fail
3408e 75 72 65 2c 20 62 75 74 20 53 51 4c 69 74 65 20  ure, but SQLite 
3408f 6e 6f 77 20 64 6f 65 73 20 74 68 69 73 20 61 75  now does this au
34090 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2f 0a  tomatically..*/.
34091 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
34092 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65  qlite3_global_re
34093 63 6f 76 65 72 28 76 6f 69 64 29 7b 0a 20 20 72  cover(void){.  r
34094 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
34095 0a 7d 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66  .}.#endif.#endif
34096 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20 74 6f 20  ../*.** Test to 
34097 73 65 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e  see whether or n
34098 6f 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ot the database 
34099 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e  connection is in
3409a 20 61 75 74 6f 63 6f 6d 6d 69 74 0a 2a 2a 20 6d   autocommit.** m
3409b 6f 64 65 2e 20 20 52 65 74 75 72 6e 20 54 52 55  ode.  Return TRU
3409c 45 20 69 66 20 69 74 20 69 73 20 61 6e 64 20 46  E if it is and F
3409d 41 4c 53 45 20 69 66 20 6e 6f 74 2e 20 20 41 75  ALSE if not.  Au
3409e 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73  tocommit mode is
3409f 20 6f 6e 0a 2a 2a 20 62 79 20 64 65 66 61 75 6c   on.** by defaul
340a0 74 2e 20 20 41 75 74 6f 63 6f 6d 6d 69 74 20 69  t.  Autocommit i
340a1 73 20 64 69 73 61 62 6c 65 64 20 62 79 20 61 20  s disabled by a 
340a2 42 45 47 49 4e 20 73 74 61 74 65 6d 65 6e 74 20  BEGIN statement 
340a3 61 6e 64 20 72 65 65 6e 61 62 6c 65 64 0a 2a 2a  and reenabled.**
340a4 20 62 79 20 74 68 65 20 6e 65 78 74 20 43 4f 4d   by the next COM
340a5 4d 49 54 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2e  MIT or ROLLBACK.
340a6 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 20 54 48 49 53  .**.******* THIS
340a7 20 49 53 20 41 4e 20 45 58 50 45 52 49 4d 45 4e   IS AN EXPERIMEN
340a8 54 41 4c 20 41 50 49 20 41 4e 44 20 49 53 20 53  TAL API AND IS S
340a9 55 42 4a 45 43 54 20 54 4f 20 43 48 41 4e 47 45  UBJECT TO CHANGE
340aa 20 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54   ******.*/.SQLIT
340ab 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
340ac 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74  3_get_autocommit
340ad 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
340ae 20 72 65 74 75 72 6e 20 64 62 2d 3e 61 75 74 6f   return db->auto
340af 43 6f 6d 6d 69 74 3b 0a 7d 0a 0a 23 69 66 64 65  Commit;.}..#ifde
340b0 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f  f SQLITE_DEBUG./
340b1 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
340b2 6e 67 20 72 6f 75 74 69 6e 65 20 69 73 20 73 75  ng routine is su
340b3 62 74 69 74 75 74 65 64 20 66 6f 72 20 63 6f 6e  btituted for con
340b4 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 43 4f 52  stant SQLITE_COR
340b5 52 55 50 54 20 69 6e 0a 2a 2a 20 64 65 62 75 67  RUPT in.** debug
340b6 67 69 6e 67 20 62 75 69 6c 64 73 2e 20 20 54 68  ging builds.  Th
340b7 69 73 20 70 72 6f 76 69 64 65 73 20 61 20 77 61  is provides a wa
340b8 79 20 74 6f 20 73 65 74 20 61 20 62 72 65 61 6b  y to set a break
340b9 70 6f 69 6e 74 20 66 6f 72 20 77 68 65 6e 0a 2a  point for when.*
340ba 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20  * corruption is 
340bb 66 69 72 73 74 20 64 65 74 65 63 74 65 64 2e 0a  first detected..
340bc 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
340bd 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 72  E int sqlite3Cor
340be 72 75 70 74 28 76 6f 69 64 29 7b 0a 20 20 72 65  rupt(void){.  re
340bf 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
340c0 55 50 54 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  UPT;.}.#endif..#
340c1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
340c2 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a  IT_DEPRECATED./*
340c3 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 63 6f  .** This is a co
340c4 6e 76 65 6e 69 65 6e 63 65 20 72 6f 75 74 69 6e  nvenience routin
340c5 65 20 74 68 61 74 20 6d 61 6b 65 73 20 73 75 72  e that makes sur
340c6 65 20 74 68 61 74 20 61 6c 6c 20 74 68 72 65 61  e that all threa
340c7 64 2d 73 70 65 63 69 66 69 63 0a 2a 2a 20 64 61  d-specific.** da
340c8 74 61 20 66 6f 72 20 74 68 69 73 20 74 68 72 65  ta for this thre
340c9 61 64 20 68 61 73 20 62 65 65 6e 20 64 65 61 6c  ad has been deal
340ca 6c 6f 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 53  located..**.** S
340cb 51 4c 69 74 65 20 6e 6f 20 6c 6f 6e 67 65 72 20  QLite no longer 
340cc 75 73 65 73 20 74 68 72 65 61 64 2d 73 70 65 63  uses thread-spec
340cd 69 66 69 63 20 64 61 74 61 20 73 6f 20 74 68 69  ific data so thi
340ce 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 77  s routine is now
340cf 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20 20 49 74   a.** no-op.  It
340d0 20 69 73 20 72 65 74 61 69 6e 65 64 20 66 6f 72   is retained for
340d1 20 68 69 73 74 6f 72 69 63 61 6c 20 63 6f 6d 70   historical comp
340d2 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2f 0a 53 51  atibility..*/.SQ
340d3 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
340d4 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65  lite3_thread_cle
340d5 61 6e 75 70 28 76 6f 69 64 29 7b 0a 7d 0a 23 65  anup(void){.}.#e
340d6 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ndif../*.** Retu
340d7 72 6e 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74  rn meta informat
340d8 69 6f 6e 20 61 62 6f 75 74 20 61 20 73 70 65 63  ion about a spec
340d9 69 66 69 63 20 63 6f 6c 75 6d 6e 20 6f 66 20 61  ific column of a
340da 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e   database table.
340db 0a 2a 2a 20 53 65 65 20 63 6f 6d 6d 65 6e 74 20  .** See comment 
340dc 69 6e 20 73 71 6c 69 74 65 33 2e 68 20 28 73 71  in sqlite3.h (sq
340dd 6c 69 74 65 2e 68 2e 69 6e 29 20 66 6f 72 20 64  lite.h.in) for d
340de 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 69 66 64 65  etails..*/.#ifde
340df 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
340e0 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a  COLUMN_METADATA.
340e1 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
340e2 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c  qlite3_table_col
340e3 75 6d 6e 5f 6d 65 74 61 64 61 74 61 28 0a 20 20  umn_metadata(.  
340e4 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
340e5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
340e6 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65  onnection handle
340e7 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
340e8 20 2a 7a 44 62 4e 61 6d 65 2c 20 20 20 20 20 20   *zDbName,      
340e9 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 61    /* Database na
340ea 6d 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20  me or NULL */.  
340eb 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62  const char *zTab
340ec 6c 65 4e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 54  leName,     /* T
340ed 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63  able name */.  c
340ee 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75  onst char *zColu
340ef 6d 6e 4e 61 6d 65 2c 20 20 20 20 2f 2a 20 43 6f  mnName,    /* Co
340f0 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63  lumn name */.  c
340f1 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70 7a 44 61  har const **pzDa
340f2 74 61 54 79 70 65 2c 20 20 20 20 2f 2a 20 4f 55  taType,    /* OU
340f3 54 50 55 54 3a 20 44 65 63 6c 61 72 65 64 20 64  TPUT: Declared d
340f4 61 74 61 20 74 79 70 65 20 2a 2f 0a 20 20 63 68  ata type */.  ch
340f5 61 72 20 63 6f 6e 73 74 20 2a 2a 70 7a 43 6f 6c  ar const **pzCol
340f6 6c 53 65 71 2c 20 20 20 20 20 2f 2a 20 4f 55 54  lSeq,     /* OUT
340f7 50 55 54 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73  PUT: Collation s
340f8 65 71 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a  equence name */.
340f9 20 20 69 6e 74 20 2a 70 4e 6f 74 4e 75 6c 6c 2c    int *pNotNull,
340fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
340fb 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20 69 66   OUTPUT: True if
340fc 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72   NOT NULL constr
340fd 61 69 6e 74 20 65 78 69 73 74 73 20 2a 2f 0a 20  aint exists */. 
340fe 20 69 6e 74 20 2a 70 50 72 69 6d 61 72 79 4b 65   int *pPrimaryKe
340ff 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  y,           /* 
34100 4f 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20  OUTPUT: True if 
34101 63 6f 6c 75 6d 6e 20 70 61 72 74 20 6f 66 20 50  column part of P
34102 4b 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 41 75 74  K */.  int *pAut
34103 6f 69 6e 63 20 20 20 20 20 20 20 20 20 20 20 20  oinc            
34104 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72     /* OUTPUT: Tr
34105 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20  ue if column is 
34106 61 75 74 6f 2d 69 6e 63 72 65 6d 65 6e 74 20 2a  auto-increment *
34107 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
34108 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d   char *zErrMsg =
34109 20 30 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   0;.  Table *pTa
3410a 62 20 3d 20 30 3b 0a 20 20 43 6f 6c 75 6d 6e 20  b = 0;.  Column 
3410b 2a 70 43 6f 6c 20 3d 20 30 3b 0a 20 20 69 6e 74  *pCol = 0;.  int
3410c 20 69 43 6f 6c 3b 0a 0a 20 20 63 68 61 72 20 63   iCol;..  char c
3410d 6f 6e 73 74 20 2a 7a 44 61 74 61 54 79 70 65 20  onst *zDataType 
3410e 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73  = 0;.  char cons
3410f 74 20 2a 7a 43 6f 6c 6c 53 65 71 20 3d 20 30 3b  t *zCollSeq = 0;
34110 0a 20 20 69 6e 74 20 6e 6f 74 6e 75 6c 6c 20 3d  .  int notnull =
34111 20 30 3b 0a 20 20 69 6e 74 20 70 72 69 6d 61 72   0;.  int primar
34112 79 6b 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20  ykey = 0;.  int 
34113 61 75 74 6f 69 6e 63 20 3d 20 30 3b 0a 0a 20 20  autoinc = 0;..  
34114 2f 2a 20 45 6e 73 75 72 65 20 74 68 65 20 64 61  /* Ensure the da
34115 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61  tabase schema ha
34116 73 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 2a 2f  s been loaded */
34117 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
34118 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
34119 29 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74  );.  (void)sqlit
3411a 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a  e3SafetyOn(db);.
3411b 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
3411c 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 72 63  terAll(db);.  rc
3411d 20 3d 20 73 71 6c 69 74 65 33 49 6e 69 74 28 64   = sqlite3Init(d
3411e 62 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20 20  b, &zErrMsg);.  
3411f 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72  if( SQLITE_OK!=r
34120 63 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 72  c ){.    goto er
34121 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  ror_out;.  }..  
34122 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61  /* Locate the ta
34123 62 6c 65 20 69 6e 20 71 75 65 73 74 69 6f 6e 20  ble in question 
34124 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69  */.  pTab = sqli
34125 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c  te3FindTable(db,
34126 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 7a 44 62   zTableName, zDb
34127 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 70 54  Name);.  if( !pT
34128 61 62 20 7c 7c 20 70 54 61 62 2d 3e 70 53 65 6c  ab || pTab->pSel
34129 65 63 74 20 29 7b 0a 20 20 20 20 70 54 61 62 20  ect ){.    pTab 
3412a 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20 65 72  = 0;.    goto er
3412b 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  ror_out;.  }..  
3412c 2f 2a 20 46 69 6e 64 20 74 68 65 20 63 6f 6c 75  /* Find the colu
3412d 6d 6e 20 66 6f 72 20 77 68 69 63 68 20 69 6e 66  mn for which inf
3412e 6f 20 69 73 20 72 65 71 75 65 73 74 65 64 20 2a  o is requested *
3412f 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49  /.  if( sqlite3I
34130 73 52 6f 77 69 64 28 7a 43 6f 6c 75 6d 6e 4e 61  sRowid(zColumnNa
34131 6d 65 29 20 29 7b 0a 20 20 20 20 69 43 6f 6c 20  me) ){.    iCol 
34132 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20  = pTab->iPKey;. 
34133 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29     if( iCol>=0 )
34134 7b 0a 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 26  {.      pCol = &
34135 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d  pTab->aCol[iCol]
34136 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
34137 0a 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b  .    for(iCol=0;
34138 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c   iCol<pTab->nCol
34139 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20  ; iCol++){.     
3413a 20 70 43 6f 6c 20 3d 20 26 70 54 61 62 2d 3e 61   pCol = &pTab->a
3413b 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 20  Col[iCol];.     
3413c 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53   if( 0==sqlite3S
3413d 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61  trICmp(pCol->zNa
3413e 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 29  me, zColumnName)
3413f 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61   ){.        brea
34140 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
34141 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70  .    if( iCol==p
34142 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20  Tab->nCol ){.   
34143 20 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20     pTab = 0;.   
34144 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f 75     goto error_ou
34145 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  t;.    }.  }..  
34146 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  /* The following
34147 20 62 6c 6f 63 6b 20 73 74 6f 72 65 73 20 74 68   block stores th
34148 65 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74 69  e meta informati
34149 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20  on that will be 
3414a 72 65 74 75 72 6e 65 64 0a 20 20 2a 2a 20 74 6f  returned.  ** to
3414b 20 74 68 65 20 63 61 6c 6c 65 72 20 69 6e 20 6c   the caller in l
3414c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 7a  ocal variables z
3414d 44 61 74 61 54 79 70 65 2c 20 7a 43 6f 6c 6c 53  DataType, zCollS
3414e 65 71 2c 20 6e 6f 74 6e 75 6c 6c 2c 20 70 72 69  eq, notnull, pri
3414f 6d 61 72 79 6b 65 79 0a 20 20 2a 2a 20 61 6e 64  marykey.  ** and
34150 20 61 75 74 6f 69 6e 63 2e 20 41 74 20 74 68 69   autoinc. At thi
34151 73 20 70 6f 69 6e 74 20 74 68 65 72 65 20 61 72  s point there ar
34152 65 20 74 77 6f 20 70 6f 73 73 69 62 69 6c 69 74  e two possibilit
34153 69 65 73 3a 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20  ies:.  ** .  ** 
34154 20 20 20 20 31 2e 20 54 68 65 20 73 70 65 63 69      1. The speci
34155 66 69 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  fied column name
34156 20 77 61 73 20 72 6f 77 69 64 22 2c 20 22 6f 69   was rowid", "oi
34157 64 22 20 6f 72 20 22 5f 72 6f 77 69 64 5f 22 20  d" or "_rowid_" 
34158 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 61 6e 64  .  **        and
34159 20 74 68 65 72 65 20 69 73 20 6e 6f 20 65 78 70   there is no exp
3415a 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64  licitly declared
3415b 20 49 50 4b 20 63 6f 6c 75 6d 6e 2e 20 0a 20 20   IPK column. .  
3415c 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 32 2e 20 54  **.  **     2. T
3415d 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20  he table is not 
3415e 61 20 76 69 65 77 20 61 6e 64 20 74 68 65 20 63  a view and the c
3415f 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69 64 65 6e 74  olumn name ident
34160 69 66 69 65 64 20 61 6e 20 0a 20 20 2a 2a 20 20  ified an .  **  
34161 20 20 20 20 20 20 65 78 70 6c 69 63 69 74 6c 79        explicitly
34162 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e   declared column
34163 2e 20 43 6f 70 79 20 6d 65 74 61 20 69 6e 66 6f  . Copy meta info
34164 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 2a 70 43  rmation from *pC
34165 6f 6c 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28 20  ol..  */ .  if( 
34166 70 43 6f 6c 20 29 7b 0a 20 20 20 20 7a 44 61 74  pCol ){.    zDat
34167 61 54 79 70 65 20 3d 20 70 43 6f 6c 2d 3e 7a 54  aType = pCol->zT
34168 79 70 65 3b 0a 20 20 20 20 7a 43 6f 6c 6c 53 65  ype;.    zCollSe
34169 71 20 3d 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 3b  q = pCol->zColl;
3416a 0a 20 20 20 20 6e 6f 74 6e 75 6c 6c 20 3d 20 70  .    notnull = p
3416b 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 21 3d 30 3b  Col->notNull!=0;
3416c 0a 20 20 20 20 70 72 69 6d 61 72 79 6b 65 79 20  .    primarykey 
3416d 20 3d 20 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b   = pCol->isPrimK
3416e 65 79 21 3d 30 3b 0a 20 20 20 20 61 75 74 6f 69  ey!=0;.    autoi
3416f 6e 63 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79  nc = pTab->iPKey
34170 3d 3d 69 43 6f 6c 20 26 26 20 28 70 54 61 62 2d  ==iCol && (pTab-
34171 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41  >tabFlags & TF_A
34172 75 74 6f 69 6e 63 72 65 6d 65 6e 74 29 21 3d 30  utoincrement)!=0
34173 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a  ;.  }else{.    z
34174 44 61 74 61 54 79 70 65 20 3d 20 22 49 4e 54 45  DataType = "INTE
34175 47 45 52 22 3b 0a 20 20 20 20 70 72 69 6d 61 72  GER";.    primar
34176 79 6b 65 79 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  ykey = 1;.  }.  
34177 69 66 28 20 21 7a 43 6f 6c 6c 53 65 71 20 29 7b  if( !zCollSeq ){
34178 0a 20 20 20 20 7a 43 6f 6c 6c 53 65 71 20 3d 20  .    zCollSeq = 
34179 22 42 49 4e 41 52 59 22 3b 0a 20 20 7d 0a 0a 65  "BINARY";.  }..e
3417a 72 72 6f 72 5f 6f 75 74 3a 0a 20 20 73 71 6c 69  rror_out:.  sqli
3417b 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c  te3BtreeLeaveAll
3417c 28 64 62 29 3b 0a 20 20 28 76 6f 69 64 29 73 71  (db);.  (void)sq
3417d 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
3417e 62 29 3b 0a 0a 20 20 2f 2a 20 57 68 65 74 68 65  b);..  /* Whethe
3417f 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 63  r the function c
34180 61 6c 6c 20 73 75 63 63 65 65 64 65 64 20 6f 72  all succeeded or
34181 20 66 61 69 6c 65 64 2c 20 73 65 74 20 74 68 65   failed, set the
34182 20 6f 75 74 70 75 74 20 70 61 72 61 6d 65 74 65   output paramete
34183 72 73 0a 20 20 2a 2a 20 74 6f 20 77 68 61 74 65  rs.  ** to whate
34184 76 65 72 20 74 68 65 69 72 20 6c 6f 63 61 6c 20  ver their local 
34185 63 6f 75 6e 74 65 72 70 61 72 74 73 20 63 6f 6e  counterparts con
34186 74 61 69 6e 2e 20 49 66 20 61 6e 20 65 72 72 6f  tain. If an erro
34187 72 20 64 69 64 20 6f 63 63 75 72 2c 0a 20 20 2a  r did occur,.  *
34188 2a 20 74 68 69 73 20 68 61 73 20 74 68 65 20 65  * this has the e
34189 66 66 65 63 74 20 6f 66 20 7a 65 72 6f 69 6e 67  ffect of zeroing
3418a 20 61 6c 6c 20 6f 75 74 70 75 74 20 70 61 72 61   all output para
3418b 6d 65 74 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 69  meters..  */.  i
3418c 66 28 20 70 7a 44 61 74 61 54 79 70 65 20 29 20  f( pzDataType ) 
3418d 2a 70 7a 44 61 74 61 54 79 70 65 20 3d 20 7a 44  *pzDataType = zD
3418e 61 74 61 54 79 70 65 3b 0a 20 20 69 66 28 20 70  ataType;.  if( p
3418f 7a 43 6f 6c 6c 53 65 71 20 29 20 2a 70 7a 43 6f  zCollSeq ) *pzCo
34190 6c 6c 53 65 71 20 3d 20 7a 43 6f 6c 6c 53 65 71  llSeq = zCollSeq
34191 3b 0a 20 20 69 66 28 20 70 4e 6f 74 4e 75 6c 6c  ;.  if( pNotNull
34192 20 29 20 2a 70 4e 6f 74 4e 75 6c 6c 20 3d 20 6e   ) *pNotNull = n
34193 6f 74 6e 75 6c 6c 3b 0a 20 20 69 66 28 20 70 50  otnull;.  if( pP
34194 72 69 6d 61 72 79 4b 65 79 20 29 20 2a 70 50 72  rimaryKey ) *pPr
34195 69 6d 61 72 79 4b 65 79 20 3d 20 70 72 69 6d 61  imaryKey = prima
34196 72 79 6b 65 79 3b 0a 20 20 69 66 28 20 70 41 75  rykey;.  if( pAu
34197 74 6f 69 6e 63 20 29 20 2a 70 41 75 74 6f 69 6e  toinc ) *pAutoin
34198 63 20 3d 20 61 75 74 6f 69 6e 63 3b 0a 0a 20 20  c = autoinc;..  
34199 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72  if( SQLITE_OK==r
3419a 63 20 26 26 20 21 70 54 61 62 20 29 7b 0a 20 20  c && !pTab ){.  
3419b 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
3419c 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20  db, zErrMsg);.  
3419d 20 20 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69    zErrMsg = sqli
3419e 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
3419f 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 20 63 6f  no such table co
341a0 6c 75 6d 6e 3a 20 25 73 2e 25 73 22 2c 20 7a 54  lumn: %s.%s", zT
341a1 61 62 6c 65 4e 61 6d 65 2c 0a 20 20 20 20 20 20  ableName,.      
341a2 20 20 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 29 3b 0a    zColumnName);.
341a3 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
341a4 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 73 71 6c  ERROR;.  }.  sql
341a5 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63  ite3Error(db, rc
341a6 2c 20 28 7a 45 72 72 4d 73 67 3f 22 25 73 22 3a  , (zErrMsg?"%s":
341a7 30 29 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20  0), zErrMsg);.  
341a8 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
341a9 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 72 63  , zErrMsg);.  rc
341aa 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
341ab 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c  t(db, rc);.  sql
341ac 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
341ad 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
341ae 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64  eturn rc;.}.#end
341af 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20  if../*.** Sleep 
341b0 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69  for a little whi
341b1 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  le.  Return the 
341b2 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73  amount of time s
341b3 6c 65 70 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lept..*/.SQLITE_
341b4 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
341b5 73 6c 65 65 70 28 69 6e 74 20 6d 73 29 7b 0a 20  sleep(int ms){. 
341b6 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
341b7 66 73 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  fs;.  int rc;.  
341b8 70 56 66 73 20 3d 20 73 71 6c 69 74 65 33 5f 76  pVfs = sqlite3_v
341b9 66 73 5f 66 69 6e 64 28 30 29 3b 0a 20 20 69 66  fs_find(0);.  if
341ba 28 20 70 56 66 73 3d 3d 30 20 29 20 72 65 74 75  ( pVfs==0 ) retu
341bb 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 69 73  rn 0;..  /* This
341bc 20 66 75 6e 63 74 69 6f 6e 20 77 6f 72 6b 73 20   function works 
341bd 69 6e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 2c  in milliseconds,
341be 20 62 75 74 20 74 68 65 20 75 6e 64 65 72 6c 79   but the underly
341bf 69 6e 67 20 4f 73 53 6c 65 65 70 28 29 20 0a 20  ing OsSleep() . 
341c0 20 2a 2a 20 41 50 49 20 75 73 65 73 20 6d 69 63   ** API uses mic
341c1 72 6f 73 65 63 6f 6e 64 73 2e 20 48 65 6e 63 65  roseconds. Hence
341c2 20 74 68 65 20 31 30 30 30 27 73 2e 0a 20 20 2a   the 1000's..  *
341c3 2f 0a 20 20 72 63 20 3d 20 28 73 71 6c 69 74 65  /.  rc = (sqlite
341c4 33 4f 73 53 6c 65 65 70 28 70 56 66 73 2c 20 31  3OsSleep(pVfs, 1
341c5 30 30 30 2a 6d 73 29 2f 31 30 30 30 29 3b 0a 20  000*ms)/1000);. 
341c6 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
341c7 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64  *.** Enable or d
341c8 69 73 61 62 6c 65 20 74 68 65 20 65 78 74 65 6e  isable the exten
341c9 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73  ded result codes
341ca 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
341cb 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65  int sqlite3_exte
341cc 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65  nded_result_code
341cd 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  s(sqlite3 *db, i
341ce 6e 74 20 6f 6e 6f 66 66 29 7b 0a 20 20 73 71 6c  nt onoff){.  sql
341cf 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
341d0 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64  (db->mutex);.  d
341d1 62 2d 3e 65 72 72 4d 61 73 6b 20 3d 20 6f 6e 6f  b->errMask = ono
341d2 66 66 20 3f 20 30 78 66 66 66 66 66 66 66 66 20  ff ? 0xffffffff 
341d3 3a 20 30 78 66 66 3b 0a 20 20 73 71 6c 69 74 65  : 0xff;.  sqlite
341d4 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
341d5 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
341d6 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
341d7 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68  ./*.** Invoke th
341d8 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d  e xFileControl m
341d9 65 74 68 6f 64 20 6f 6e 20 61 20 70 61 72 74 69  ethod on a parti
341da 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 2e 0a  cular database..
341db 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
341dc 74 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63  t sqlite3_file_c
341dd 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 20 2a  ontrol(sqlite3 *
341de 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
341df 7a 44 62 4e 61 6d 65 2c 20 69 6e 74 20 6f 70 2c  zDbName, int op,
341e0 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20   void *pArg){.  
341e1 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
341e2 45 52 52 4f 52 3b 0a 20 20 69 6e 74 20 69 44 62  ERROR;.  int iDb
341e3 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
341e4 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
341e5 78 29 3b 0a 20 20 69 66 28 20 7a 44 62 4e 61 6d  x);.  if( zDbNam
341e6 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69 44 62 20  e==0 ){.    iDb 
341e7 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
341e8 20 20 66 6f 72 28 69 44 62 3d 30 3b 20 69 44 62    for(iDb=0; iDb
341e9 3c 64 62 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b 29  <db->nDb; iDb++)
341ea 7b 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63  {.      if( strc
341eb 6d 70 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  mp(db->aDb[iDb].
341ec 7a 4e 61 6d 65 2c 20 7a 44 62 4e 61 6d 65 29 3d  zName, zDbName)=
341ed 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
341ee 7d 0a 20 20 7d 0a 20 20 69 66 28 20 69 44 62 3c  }.  }.  if( iDb<
341ef 64 62 2d 3e 6e 44 62 20 29 7b 0a 20 20 20 20 42  db->nDb ){.    B
341f0 74 72 65 65 20 2a 70 42 74 72 65 65 20 3d 20 64  tree *pBtree = d
341f1 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b  b->aDb[iDb].pBt;
341f2 0a 20 20 20 20 69 66 28 20 70 42 74 72 65 65 20  .    if( pBtree 
341f3 29 7b 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a  ){.      Pager *
341f4 70 50 61 67 65 72 3b 0a 20 20 20 20 20 20 73 71  pPager;.      sq
341f5 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 3b 0a  lite3_file *fd;.
341f6 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
341f7 65 65 45 6e 74 65 72 28 70 42 74 72 65 65 29 3b  eeEnter(pBtree);
341f8 0a 20 20 20 20 20 20 70 50 61 67 65 72 20 3d 20  .      pPager = 
341f9 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
341fa 72 28 70 42 74 72 65 65 29 3b 0a 20 20 20 20 20  r(pBtree);.     
341fb 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 21   assert( pPager!
341fc 3d 30 20 29 3b 0a 20 20 20 20 20 20 66 64 20 3d  =0 );.      fd =
341fd 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c   sqlite3PagerFil
341fe 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20  e(pPager);.     
341ff 20 61 73 73 65 72 74 28 20 66 64 21 3d 30 20 29   assert( fd!=0 )
34200 3b 0a 20 20 20 20 20 20 69 66 28 20 66 64 2d 3e  ;.      if( fd->
34201 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  pMethods ){.    
34202 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
34203 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 66 64  OsFileControl(fd
34204 2c 20 6f 70 2c 20 70 41 72 67 29 3b 0a 20 20 20  , op, pArg);.   
34205 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
34206 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 42 74  e3BtreeLeave(pBt
34207 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ree);.    }.  }.
34208 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
34209 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
3420a 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 20 20  ;.  return rc;  
3420b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72   .}../*.** Inter
3420c 66 61 63 65 20 74 6f 20 74 68 65 20 74 65 73 74  face to the test
3420d 69 6e 67 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 53 51  ing logic..*/.SQ
3420e 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
3420f 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f  ite3_test_contro
34210 6c 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a  l(int op, ...){.
34211 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 23 69    int rc = 0;.#i
34212 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
34213 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 20  T_BUILTIN_TEST. 
34214 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 76   va_list ap;.  v
34215 61 5f 73 74 61 72 74 28 61 70 2c 20 6f 70 29 3b  a_start(ap, op);
34216 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b  .  switch( op ){
34217 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  ..    /*.    ** 
34218 53 61 76 65 20 74 68 65 20 63 75 72 72 65 6e 74  Save the current
34219 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 50 52   state of the PR
3421a 4e 47 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  NG..    */.    c
3421b 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ase SQLITE_TESTC
3421c 54 52 4c 5f 50 52 4e 47 5f 53 41 56 45 3a 20 7b  TRL_PRNG_SAVE: {
3421d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 72  .      sqlite3Pr
3421e 6e 67 53 61 76 65 53 74 61 74 65 28 29 3b 0a 20  ngSaveState();. 
3421f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
34220 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a  }..    /*.    **
34221 20 52 65 73 74 6f 72 65 20 74 68 65 20 73 74 61   Restore the sta
34222 74 65 20 6f 66 20 74 68 65 20 50 52 4e 47 20 74  te of the PRNG t
34223 6f 20 74 68 65 20 6c 61 73 74 20 73 74 61 74 65  o the last state
34224 20 73 61 76 65 64 20 75 73 69 6e 67 0a 20 20 20   saved using.   
34225 20 2a 2a 20 50 52 4e 47 5f 53 41 56 45 2e 20 20   ** PRNG_SAVE.  
34226 49 66 20 50 52 4e 47 5f 53 41 56 45 20 68 61 73  If PRNG_SAVE has
34227 20 6e 65 76 65 72 20 62 65 66 6f 72 65 20 62 65   never before be
34228 65 6e 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 0a  en called, then.
34229 20 20 20 20 2a 2a 20 74 68 69 73 20 76 65 72 62      ** this verb
3422a 20 61 63 74 73 20 6c 69 6b 65 20 50 52 4e 47 5f   acts like PRNG_
3422b 52 45 53 45 54 2e 0a 20 20 20 20 2a 2f 0a 20 20  RESET..    */.  
3422c 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45    case SQLITE_TE
3422d 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 54  STCTRL_PRNG_REST
3422e 4f 52 45 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  ORE: {.      sql
3422f 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65 53  ite3PrngRestoreS
34230 74 61 74 65 28 29 3b 0a 20 20 20 20 20 20 62 72  tate();.      br
34231 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
34232 2f 2a 0a 20 20 20 20 2a 2a 20 52 65 73 65 74 20  /*.    ** Reset 
34233 74 68 65 20 50 52 4e 47 20 62 61 63 6b 20 74 6f  the PRNG back to
34234 20 69 74 73 20 75 6e 69 6e 69 74 69 61 6c 69 7a   its uninitializ
34235 65 64 20 73 74 61 74 65 2e 20 20 54 68 65 20 6e  ed state.  The n
34236 65 78 74 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20  ext call.    ** 
34237 74 6f 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f  to sqlite3_rando
34238 6d 6e 65 73 73 28 29 20 77 69 6c 6c 20 72 65 73  mness() will res
34239 65 65 64 20 74 68 65 20 50 52 4e 47 20 75 73 69  eed the PRNG usi
3423a 6e 67 20 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c  ng a single call
3423b 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 78  .    ** to the x
3423c 52 61 6e 64 6f 6d 6e 65 73 73 20 6d 65 74 68 6f  Randomness metho
3423d 64 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74  d of the default
3423e 20 56 46 53 2e 0a 20 20 20 20 2a 2f 0a 20 20 20   VFS..    */.   
3423f 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53   case SQLITE_TES
34240 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 45 54  TCTRL_PRNG_RESET
34241 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
34242 33 50 72 6e 67 52 65 73 65 74 53 74 61 74 65 28  3PrngResetState(
34243 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
34244 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20      }..    /*.  
34245 20 20 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74 65    **  sqlite3_te
34246 73 74 5f 63 6f 6e 74 72 6f 6c 28 42 49 54 56 45  st_control(BITVE
34247 43 5f 54 45 53 54 2c 20 73 69 7a 65 2c 20 70 72  C_TEST, size, pr
34248 6f 67 72 61 6d 29 0a 20 20 20 20 2a 2a 0a 20 20  ogram).    **.  
34249 20 20 2a 2a 20 52 75 6e 20 61 20 74 65 73 74 20    ** Run a test 
3424a 61 67 61 69 6e 73 74 20 61 20 42 69 74 76 65 63  against a Bitvec
3424b 20 6f 62 6a 65 63 74 20 6f 66 20 73 69 7a 65 2e   object of size.
3424c 20 20 54 68 65 20 70 72 6f 67 72 61 6d 20 61 72    The program ar
3424d 67 75 6d 65 6e 74 0a 20 20 20 20 2a 2a 20 69 73  gument.    ** is
3424e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 74   an array of int
3424f 65 67 65 72 73 20 74 68 61 74 20 64 65 66 69 6e  egers that defin
34250 65 73 20 74 68 65 20 74 65 73 74 2e 20 20 52 65  es the test.  Re
34251 74 75 72 6e 20 2d 31 20 6f 6e 20 61 0a 20 20 20  turn -1 on a.   
34252 20 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63   ** memory alloc
34253 61 74 69 6f 6e 20 65 72 72 6f 72 2c 20 30 20 6f  ation error, 0 o
34254 6e 20 73 75 63 63 65 73 73 2c 20 6f 72 20 6e 6f  n success, or no
34255 6e 2d 7a 65 72 6f 20 66 6f 72 20 61 6e 20 65 72  n-zero for an er
34256 72 6f 72 2e 0a 20 20 20 20 2a 2a 20 53 65 65 20  ror..    ** See 
34257 74 68 65 20 73 71 6c 69 74 65 33 42 69 74 76 65  the sqlite3Bitve
34258 63 42 75 69 6c 74 69 6e 54 65 73 74 28 29 20 66  cBuiltinTest() f
34259 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
3425a 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 20 20 2a  formation..    *
3425b 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  /.    case SQLIT
3425c 45 5f 54 45 53 54 43 54 52 4c 5f 42 49 54 56 45  E_TESTCTRL_BITVE
3425d 43 5f 54 45 53 54 3a 20 7b 0a 20 20 20 20 20 20  C_TEST: {.      
3425e 69 6e 74 20 73 7a 20 3d 20 76 61 5f 61 72 67 28  int sz = va_arg(
3425f 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ap, int);.      
34260 69 6e 74 20 2a 61 50 72 6f 67 20 3d 20 76 61 5f  int *aProg = va_
34261 61 72 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0a 20  arg(ap, int*);. 
34262 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
34263 33 42 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65  3BitvecBuiltinTe
34264 73 74 28 73 7a 2c 20 61 50 72 6f 67 29 3b 0a 20  st(sz, aProg);. 
34265 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
34266 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a  }..    /*.    **
34267 20 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63    sqlite3_test_c
34268 6f 6e 74 72 6f 6c 28 42 45 4e 49 47 4e 5f 4d 41  ontrol(BENIGN_MA
34269 4c 4c 4f 43 5f 48 4f 4f 4b 53 2c 20 78 42 65 67  LLOC_HOOKS, xBeg
3426a 69 6e 2c 20 78 45 6e 64 29 0a 20 20 20 20 2a 2a  in, xEnd).    **
3426b 0a 20 20 20 20 2a 2a 20 52 65 67 69 73 74 65 72  .    ** Register
3426c 20 68 6f 6f 6b 73 20 74 6f 20 63 61 6c 6c 20 74   hooks to call t
3426d 6f 20 69 6e 64 69 63 61 74 65 20 77 68 69 63 68  o indicate which
3426e 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72   malloc() failur
3426f 65 73 20 0a 20 20 20 20 2a 2a 20 61 72 65 20 62  es .    ** are b
34270 65 6e 69 67 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  enign..    */.  
34271 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45    case SQLITE_TE
34272 53 54 43 54 52 4c 5f 42 45 4e 49 47 4e 5f 4d 41  STCTRL_BENIGN_MA
34273 4c 4c 4f 43 5f 48 4f 4f 4b 53 3a 20 7b 0a 20 20  LLOC_HOOKS: {.  
34274 20 20 20 20 74 79 70 65 64 65 66 20 76 6f 69 64      typedef void
34275 20 28 2a 76 6f 69 64 5f 66 75 6e 63 74 69 6f 6e   (*void_function
34276 29 28 76 6f 69 64 29 3b 0a 20 20 20 20 20 20 76  )(void);.      v
34277 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 20 78 42 65  oid_function xBe
34278 6e 69 67 6e 42 65 67 69 6e 3b 0a 20 20 20 20 20  nignBegin;.     
34279 20 76 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 20 78   void_function x
3427a 42 65 6e 69 67 6e 45 6e 64 3b 0a 20 20 20 20 20  BenignEnd;.     
3427b 20 78 42 65 6e 69 67 6e 42 65 67 69 6e 20 3d 20   xBenignBegin = 
3427c 76 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 5f  va_arg(ap, void_
3427d 66 75 6e 63 74 69 6f 6e 29 3b 0a 20 20 20 20 20  function);.     
3427e 20 78 42 65 6e 69 67 6e 45 6e 64 20 3d 20 76 61   xBenignEnd = va
3427f 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 5f 66 75  _arg(ap, void_fu
34280 6e 63 74 69 6f 6e 29 3b 0a 20 20 20 20 20 20 73  nction);.      s
34281 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c  qlite3BenignMall
34282 6f 63 48 6f 6f 6b 73 28 78 42 65 6e 69 67 6e 42  ocHooks(xBenignB
34283 65 67 69 6e 2c 20 78 42 65 6e 69 67 6e 45 6e 64  egin, xBenignEnd
34284 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
34285 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20      }..    /*.  
34286 20 20 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74 65    **  sqlite3_te
34287 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c 49 54  st_control(SQLIT
34288 45 5f 54 45 53 54 43 54 52 4c 5f 50 45 4e 44 49  E_TESTCTRL_PENDI
34289 4e 47 5f 42 59 54 45 2c 20 75 6e 73 69 67 6e 65  NG_BYTE, unsigne
3428a 64 20 69 6e 74 20 58 29 0a 20 20 20 20 2a 2a 0a  d int X).    **.
3428b 20 20 20 20 2a 2a 20 53 65 74 20 74 68 65 20 50      ** Set the P
3428c 45 4e 44 49 4e 47 20 62 79 74 65 20 74 6f 20 74  ENDING byte to t
3428d 68 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20  he value in the 
3428e 61 72 67 75 6d 65 6e 74 2c 20 69 66 20 58 3e 30  argument, if X>0
3428f 2e 0a 20 20 20 20 2a 2a 20 4d 61 6b 65 20 6e 6f  ..    ** Make no
34290 20 63 68 61 6e 67 65 73 20 69 66 20 58 3d 3d 30   changes if X==0
34291 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 76 61  .  Return the va
34292 6c 75 65 20 6f 66 20 74 68 65 20 70 65 6e 64 69  lue of the pendi
34293 6e 67 20 62 79 74 65 0a 20 20 20 20 2a 2a 20 61  ng byte.    ** a
34294 73 20 69 74 20 65 78 69 73 74 69 6e 67 20 62 65  s it existing be
34295 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74 69 6e  fore this routin
34296 65 20 77 61 73 20 63 61 6c 6c 65 64 2e 0a 20 20  e was called..  
34297 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 4d 50 4f    **.    ** IMPO
34298 52 54 41 4e 54 3a 20 20 43 68 61 6e 67 69 6e 67  RTANT:  Changing
34299 20 74 68 65 20 50 45 4e 44 49 4e 47 20 62 79 74   the PENDING byt
3429a 65 20 66 72 6f 6d 20 30 78 34 30 30 30 30 30 30  e from 0x4000000
3429b 30 20 72 65 73 75 6c 74 73 20 69 6e 0a 20 20 20  0 results in.   
3429c 20 2a 2a 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69   ** an incompati
3429d 62 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  ble database fil
3429e 65 20 66 6f 72 6d 61 74 2e 20 20 43 68 61 6e 67  e format.  Chang
3429f 69 6e 67 20 74 68 65 20 50 45 4e 44 49 4e 47 20  ing the PENDING 
342a0 62 79 74 65 0a 20 20 20 20 2a 2a 20 77 68 69 6c  byte.    ** whil
342a1 65 20 61 6e 79 20 64 61 74 61 62 61 73 65 20 63  e any database c
342a2 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 70 65  onnection is ope
342a3 6e 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64  n results in und
342a4 65 66 69 6e 65 64 20 61 6e 64 0a 20 20 20 20 2a  efined and.    *
342a5 2a 20 64 69 6c 65 74 65 72 69 6f 75 73 20 62 65  * dileterious be
342a6 68 61 76 69 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20  havior..    */. 
342a7 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54     case SQLITE_T
342a8 45 53 54 43 54 52 4c 5f 50 45 4e 44 49 4e 47 5f  ESTCTRL_PENDING_
342a9 42 59 54 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e  BYTE: {.      un
342aa 73 69 67 6e 65 64 20 69 6e 74 20 6e 65 77 56 61  signed int newVa
342ab 6c 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 75  l = va_arg(ap, u
342ac 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a 20 20  nsigned int);.  
342ad 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
342ae 50 65 6e 64 69 6e 67 42 79 74 65 3b 0a 20 20 20  PendingByte;.   
342af 20 20 20 69 66 28 20 6e 65 77 56 61 6c 20 29 20     if( newVal ) 
342b0 73 71 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79  sqlite3PendingBy
342b1 74 65 20 3d 20 6e 65 77 56 61 6c 3b 0a 20 20 20  te = newVal;.   
342b2 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
342b3 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20  .    /*.    **  
342b4 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
342b5 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54  trol(SQLITE_TEST
342b6 43 54 52 4c 5f 41 53 53 45 52 54 2c 20 69 6e 74  CTRL_ASSERT, int
342b7 20 58 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a   X).    **.    *
342b8 2a 20 54 68 69 73 20 61 63 74 69 6f 6e 20 70 72  * This action pr
342b9 6f 76 69 64 65 73 20 61 20 72 75 6e 2d 74 69 6d  ovides a run-tim
342ba 65 20 74 65 73 74 20 74 6f 20 73 65 65 20 77 68  e test to see wh
342bb 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a 20 20 20  ether or not.   
342bc 20 2a 2a 20 61 73 73 65 72 74 28 29 20 77 61 73   ** assert() was
342bd 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70   enabled at comp
342be 69 6c 65 2d 74 69 6d 65 2e 20 20 49 66 20 58 20  ile-time.  If X 
342bf 69 73 20 74 72 75 65 20 61 6e 64 20 61 73 73 65  is true and asse
342c0 72 74 28 29 0a 20 20 20 20 2a 2a 20 69 73 20 65  rt().    ** is e
342c1 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 65  nabled, then the
342c2 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
342c3 20 74 72 75 65 2e 20 20 49 66 20 58 20 69 73 20   true.  If X is 
342c4 74 72 75 65 20 61 6e 64 0a 20 20 20 20 2a 2a 20  true and.    ** 
342c5 61 73 73 65 72 74 28 29 20 69 73 20 64 69 73 61  assert() is disa
342c6 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 20 72  bled, then the r
342c7 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 7a  eturn value is z
342c8 65 72 6f 2e 20 20 49 66 20 58 20 69 73 0a 20 20  ero.  If X is.  
342c9 20 20 2a 2a 20 66 61 6c 73 65 20 61 6e 64 20 61    ** false and a
342ca 73 73 65 72 74 28 29 20 69 73 20 65 6e 61 62 6c  ssert() is enabl
342cb 65 64 2c 20 74 68 65 6e 20 74 68 65 20 61 73 73  ed, then the ass
342cc 65 72 74 69 6f 6e 20 66 69 72 65 73 20 61 6e 64  ertion fires and
342cd 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 72 6f 63   the.    ** proc
342ce 65 73 73 20 61 62 6f 72 74 73 2e 20 20 49 66 20  ess aborts.  If 
342cf 58 20 69 73 20 66 61 6c 73 65 20 61 6e 64 20 61  X is false and a
342d0 73 73 65 72 74 28 29 20 69 73 20 64 69 73 61 62  ssert() is disab
342d1 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 0a 20 20  led, then the.  
342d2 20 20 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75    ** return valu
342d3 65 20 69 73 20 7a 65 72 6f 2e 0a 20 20 20 20 2a  e is zero..    *
342d4 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  /.    case SQLIT
342d5 45 5f 54 45 53 54 43 54 52 4c 5f 41 53 53 45 52  E_TESTCTRL_ASSER
342d6 54 3a 20 7b 0a 20 20 20 20 20 20 76 6f 6c 61 74  T: {.      volat
342d7 69 6c 65 20 69 6e 74 20 78 20 3d 20 30 3b 0a 20  ile int x = 0;. 
342d8 20 20 20 20 20 61 73 73 65 72 74 28 20 28 78 20       assert( (x 
342d9 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29  = va_arg(ap,int)
342da 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 72 63  )!=0 );.      rc
342db 20 3d 20 78 3b 0a 20 20 20 20 20 20 62 72 65 61   = x;.      brea
342dc 6b 3b 0a 20 20 20 20 7d 0a 0a 0a 20 20 20 20 2f  k;.    }...    /
342dd 2a 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69 74 65  *.    **  sqlite
342de 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53  3_test_control(S
342df 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41  QLITE_TESTCTRL_A
342e0 4c 57 41 59 53 2c 20 69 6e 74 20 58 29 0a 20 20  LWAYS, int X).  
342e1 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73    **.    ** This
342e2 20 61 63 74 69 6f 6e 20 70 72 6f 76 69 64 65 73   action provides
342e3 20 61 20 72 75 6e 2d 74 69 6d 65 20 74 65 73 74   a run-time test
342e4 20 74 6f 20 73 65 65 20 68 6f 77 20 74 68 65 20   to see how the 
342e5 41 4c 57 41 59 53 20 61 6e 64 0a 20 20 20 20 2a  ALWAYS and.    *
342e6 2a 20 4e 45 56 45 52 20 6d 61 63 72 6f 73 20 77  * NEVER macros w
342e7 65 72 65 20 64 65 66 69 6e 65 64 20 61 74 20 63  ere defined at c
342e8 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 20 20 20  ompile-time..   
342e9 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72   **.    ** The r
342ea 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 41  eturn value is A
342eb 4c 57 41 59 53 28 58 29 2e 20 20 0a 20 20 20 20  LWAYS(X).  .    
342ec 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65  **.    ** The re
342ed 63 6f 6d 6d 65 6e 64 65 64 20 74 65 73 74 20 69  commended test i
342ee 73 20 58 3d 3d 32 2e 20 20 49 66 20 74 68 65 20  s X==2.  If the 
342ef 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
342f0 32 2c 20 74 68 61 74 20 6d 65 61 6e 73 0a 20 20  2, that means.  
342f1 20 20 2a 2a 20 41 4c 57 41 59 53 28 29 20 61 6e    ** ALWAYS() an
342f2 64 20 4e 45 56 45 52 28 29 20 61 72 65 20 62 6f  d NEVER() are bo
342f3 74 68 20 6e 6f 2d 6f 70 20 70 61 73 73 2d 74 68  th no-op pass-th
342f4 72 6f 75 67 68 20 6d 61 63 72 6f 73 2c 20 77 68  rough macros, wh
342f5 69 63 68 20 69 73 20 74 68 65 0a 20 20 20 20 2a  ich is the.    *
342f6 2a 20 64 65 66 61 75 6c 74 20 73 65 74 74 69 6e  * default settin
342f7 67 2e 20 20 49 66 20 74 68 65 20 72 65 74 75 72  g.  If the retur
342f8 6e 20 76 61 6c 75 65 20 69 73 20 31 2c 20 74 68  n value is 1, th
342f9 65 6e 20 41 4c 57 41 59 53 28 29 20 69 73 20 65  en ALWAYS() is e
342fa 69 74 68 65 72 0a 20 20 20 20 2a 2a 20 68 61 72  ither.    ** har
342fb 64 2d 63 6f 64 65 64 20 74 6f 20 74 72 75 65 20  d-coded to true 
342fc 6f 72 20 65 6c 73 65 20 69 74 20 61 73 73 65 72  or else it asser
342fd 74 73 20 69 66 20 69 74 73 20 61 72 67 75 6d 65  ts if its argume
342fe 6e 74 20 69 73 20 66 61 6c 73 65 2e 0a 20 20 20  nt is false..   
342ff 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 62 65   ** The first be
34300 68 61 76 69 6f 72 20 28 68 61 72 64 2d 63 6f 64  havior (hard-cod
34301 65 64 20 74 6f 20 74 72 75 65 29 20 69 73 20 74  ed to true) is t
34302 68 65 20 63 61 73 65 20 69 66 0a 20 20 20 20 2a  he case if.    *
34303 2a 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52  * SQLITE_TESTCTR
34304 4c 5f 41 53 53 45 52 54 20 73 68 6f 77 73 20 74  L_ASSERT shows t
34305 68 61 74 20 61 73 73 65 72 74 28 29 20 69 73 20  hat assert() is 
34306 64 69 73 61 62 6c 65 64 20 61 6e 64 20 74 68 65  disabled and the
34307 20 73 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 62   second.    ** b
34308 65 68 61 76 69 6f 72 20 28 61 73 73 65 72 74 20  ehavior (assert 
34309 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  if the argument 
3430a 74 6f 20 41 4c 57 41 59 53 28 29 20 69 73 20 66  to ALWAYS() is f
3430b 61 6c 73 65 29 20 69 73 20 74 68 65 20 63 61 73  alse) is the cas
3430c 65 20 69 66 0a 20 20 20 20 2a 2a 20 53 51 4c 49  e if.    ** SQLI
3430d 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 53 53 45  TE_TESTCTRL_ASSE
3430e 52 54 20 73 68 6f 77 73 20 74 68 61 74 20 61 73  RT shows that as
3430f 73 65 72 74 28 29 20 69 73 20 65 6e 61 62 6c 65  sert() is enable
34310 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  d..    **.    **
34311 20 54 68 65 20 72 75 6e 2d 74 69 6d 65 20 74 65   The run-time te
34312 73 74 20 70 72 6f 63 65 64 75 72 65 20 6d 69 67  st procedure mig
34313 68 74 20 6c 6f 6f 6b 20 73 6f 6d 65 74 68 69 6e  ht look somethin
34314 67 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20 20  g like this:.   
34315 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 69 66   **.    **    if
34316 28 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63  ( sqlite3_test_c
34317 6f 6e 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45  ontrol(SQLITE_TE
34318 53 54 43 54 52 4c 5f 41 4c 57 41 59 53 2c 20 32  STCTRL_ALWAYS, 2
34319 29 3d 3d 32 20 29 7b 0a 20 20 20 20 2a 2a 20 20  )==2 ){.    **  
3431a 20 20 20 20 2f 2f 20 41 4c 57 41 59 53 28 29 20      // ALWAYS() 
3431b 61 6e 64 20 4e 45 56 45 52 28 29 20 61 72 65 20  and NEVER() are 
3431c 6e 6f 2d 6f 70 20 70 61 73 73 2d 74 68 72 6f 75  no-op pass-throu
3431d 67 68 20 6d 61 63 72 6f 73 0a 20 20 20 20 2a 2a  gh macros.    **
3431e 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71      }else if( sq
3431f 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72  lite3_test_contr
34320 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ol(SQLITE_TESTCT
34321 52 4c 5f 41 53 53 45 52 54 2c 20 31 29 20 29 7b  RL_ASSERT, 1) ){
34322 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 2f 2f 20  .    **      // 
34323 41 4c 57 41 59 53 28 78 29 20 61 73 73 65 72 74  ALWAYS(x) assert
34324 73 20 74 68 61 74 20 78 20 69 73 20 74 72 75 65  s that x is true
34325 2e 20 4e 45 56 45 52 28 78 29 20 61 73 73 65 72  . NEVER(x) asser
34326 74 73 20 78 20 69 73 20 66 61 6c 73 65 2e 0a 20  ts x is false.. 
34327 20 20 20 2a 2a 20 20 20 20 7d 65 6c 73 65 7b 0a     **    }else{.
34328 20 20 20 20 2a 2a 20 20 20 20 20 20 2f 2f 20 41      **      // A
34329 4c 57 41 59 53 28 78 29 20 69 73 20 61 20 63 6f  LWAYS(x) is a co
3432a 6e 73 74 61 6e 74 20 31 2e 20 20 4e 45 56 45 52  nstant 1.  NEVER
3432b 28 78 29 20 69 73 20 61 20 63 6f 6e 73 74 61 6e  (x) is a constan
3432c 74 20 30 2e 0a 20 20 20 20 2a 2a 20 20 20 20 7d  t 0..    **    }
3432d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
3432e 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c   SQLITE_TESTCTRL
3432f 5f 41 4c 57 41 59 53 3a 20 7b 0a 20 20 20 20 20  _ALWAYS: {.     
34330 20 69 6e 74 20 78 20 3d 20 76 61 5f 61 72 67 28   int x = va_arg(
34331 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 72  ap,int);.      r
34332 63 20 3d 20 41 4c 57 41 59 53 28 78 29 3b 0a 20  c = ALWAYS(x);. 
34333 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
34334 7d 0a 0a 20 20 20 20 2f 2a 20 20 20 73 71 6c 69  }..    /*   sqli
34335 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  te3_test_control
34336 28 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c  (SQLITE_TESTCTRL
34337 5f 52 45 53 45 52 56 45 2c 20 73 71 6c 69 74 65  _RESERVE, sqlite
34338 33 20 2a 64 62 2c 20 69 6e 74 20 4e 29 0a 20 20  3 *db, int N).  
34339 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 53 65 74 20    **.    ** Set 
3433a 74 68 65 20 6e 52 65 73 65 72 76 65 20 73 69 7a  the nReserve siz
3433b 65 20 74 6f 20 4e 20 66 6f 72 20 74 68 65 20 6d  e to N for the m
3433c 61 69 6e 20 64 61 74 61 62 61 73 65 20 6f 6e 20  ain database on 
3433d 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20  the database.   
3433e 20 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64   ** connection d
3433f 62 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  b..    */.    ca
34340 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  se SQLITE_TESTCT
34341 52 4c 5f 52 45 53 45 52 56 45 3a 20 7b 0a 20 20  RL_RESERVE: {.  
34342 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
34343 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c  = va_arg(ap, sql
34344 69 74 65 33 2a 29 3b 0a 20 20 20 20 20 20 69 6e  ite3*);.      in
34345 74 20 78 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  t x = va_arg(ap,
34346 69 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  int);.      sqli
34347 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
34348 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20  db->mutex);.    
34349 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65    sqlite3BtreeSe
3434a 74 50 61 67 65 53 69 7a 65 28 64 62 2d 3e 61 44  tPageSize(db->aD
3434b 62 5b 30 5d 2e 70 42 74 2c 20 30 2c 20 78 2c 20  b[0].pBt, 0, x, 
3434c 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
3434d 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
3434e 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  ->mutex);.      
3434f 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
34350 7d 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  }.  va_end(ap);.
34351 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
34352 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45  _OMIT_BUILTIN_TE
34353 53 54 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  ST */.  return r
34354 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  c;.}../*********
34355 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 61 69  ***** End of mai
34356 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
34357 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34358 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34359 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
3435a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
3435b 20 6e 6f 74 69 66 79 2e 63 20 2a 2a 2a 2a 2a 2a   notify.c ******
3435c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3435d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3435e 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 39  ****/./*.** 2009
3435f 20 4d 61 72 63 68 20 33 0a 2a 2a 0a 2a 2a 20 54   March 3.**.** T
34360 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
34361 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
34362 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
34363 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
34364 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
34365 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
34366 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
34367 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
34368 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
34369 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
3436a 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
3436b 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
3436c 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
3436d 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
3436e 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
3436f 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
34370 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
34371 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34372 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34373 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34374 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34375 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
34376 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
34377 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
34378 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
34379 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79  e3_unlock_notify
3437a 28 29 0a 2a 2a 20 41 50 49 20 6d 65 74 68 6f 64  ().** API method
3437b 20 61 6e 64 20 69 74 73 20 61 73 73 6f 63 69 61   and its associa
3437c 74 65 64 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74  ted functionalit
3437d 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6e 6f  y..**.** $Id: no
3437e 74 69 66 79 2e 63 2c 76 20 31 2e 34 20 32 30 30  tify.c,v 1.4 200
3437f 39 2f 30 34 2f 30 37 20 32 32 3a 30 36 3a 35 37  9/04/07 22:06:57
34380 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f   drh Exp $.*/../
34381 2a 20 4f 6d 69 74 20 74 68 69 73 20 65 6e 74 69  * Omit this enti
34382 72 65 20 66 69 6c 65 20 69 66 20 53 51 4c 49 54  re file if SQLIT
34383 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f  E_ENABLE_UNLOCK_
34384 4e 4f 54 49 46 59 20 69 73 20 6e 6f 74 20 64 65  NOTIFY is not de
34385 66 69 6e 65 64 2e 20 2a 2f 0a 23 69 66 64 65 66  fined. */.#ifdef
34386 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55   SQLITE_ENABLE_U
34387 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 0a 2f 2a  NLOCK_NOTIFY../*
34388 0a 2a 2a 20 50 75 62 6c 69 63 20 69 6e 74 65 72  .** Public inter
34389 66 61 63 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 73  faces:.**.**   s
3438a 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e  qlite3Connection
3438b 42 6c 6f 63 6b 65 64 28 29 0a 2a 2a 20 20 20 73  Blocked().**   s
3438c 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e  qlite3Connection
3438d 55 6e 6c 6f 63 6b 65 64 28 29 0a 2a 2a 20 20 20  Unlocked().**   
3438e 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f  sqlite3Connectio
3438f 6e 43 6c 6f 73 65 64 28 29 0a 2a 2a 20 20 20 73  nClosed().**   s
34390 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f  qlite3_unlock_no
34391 74 69 66 79 28 29 0a 2a 2f 0a 0a 23 64 65 66 69  tify().*/..#defi
34392 6e 65 20 61 73 73 65 72 74 4d 75 74 65 78 48 65  ne assertMutexHe
34393 6c 64 28 29 20 5c 0a 20 20 61 73 73 65 72 74 28  ld() \.  assert(
34394 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
34395 65 6c 64 28 73 71 6c 69 74 65 33 4d 75 74 65 78  eld(sqlite3Mutex
34396 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
34397 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
34398 29 29 20 29 0a 0a 2f 2a 0a 2a 2a 20 48 65 61 64  )) )../*.** Head
34399 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73   of a linked lis
3439a 74 20 6f 66 20 61 6c 6c 20 73 71 6c 69 74 65 33  t of all sqlite3
3439b 20 6f 62 6a 65 63 74 73 20 63 72 65 61 74 65 64   objects created
3439c 20 62 79 20 74 68 69 73 20 70 72 6f 63 65 73 73   by this process
3439d 0a 2a 2a 20 66 6f 72 20 77 68 69 63 68 20 65 69  .** for which ei
3439e 74 68 65 72 20 73 71 6c 69 74 65 33 2e 70 42 6c  ther sqlite3.pBl
3439f 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
343a0 20 6f 72 20 73 71 6c 69 74 65 33 2e 70 55 6e 6c   or sqlite3.pUnl
343a1 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  ockConnection.**
343a2 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e 20 54 68   is not NULL. Th
343a3 69 73 20 76 61 72 69 61 62 6c 65 20 6d 61 79 20  is variable may 
343a4 6f 6e 6c 79 20 61 63 63 65 73 73 65 64 20 77 68  only accessed wh
343a5 69 6c 65 20 74 68 65 20 53 54 41 54 49 43 5f 4d  ile the STATIC_M
343a6 41 53 54 45 52 0a 2a 2a 20 6d 75 74 65 78 20 69  ASTER.** mutex i
343a7 73 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69  s held..*/.stati
343a8 63 20 73 71 6c 69 74 65 33 20 2a 53 51 4c 49 54  c sqlite3 *SQLIT
343a9 45 5f 57 53 44 20 73 71 6c 69 74 65 33 42 6c 6f  E_WSD sqlite3Blo
343aa 63 6b 65 64 4c 69 73 74 20 3d 20 30 3b 0a 0a 23  ckedList = 0;..#
343ab 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a  ifndef NDEBUG./*
343ac 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
343ad 6e 20 69 73 20 61 20 63 6f 6d 70 6c 65 78 20 61  n is a complex a
343ae 73 73 65 72 74 28 29 20 74 68 61 74 20 76 65 72  ssert() that ver
343af 69 66 69 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77  ifies the follow
343b0 69 6e 67 20 0a 2a 2a 20 70 72 6f 70 65 72 74 69  ing .** properti
343b1 65 73 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 65  es of the blocke
343b2 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69  d connections li
343b3 73 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 45  st:.**.**   1) E
343b4 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65  ach entry in the
343b5 20 6c 69 73 74 20 68 61 73 20 61 20 6e 6f 6e 2d   list has a non-
343b6 4e 55 4c 4c 20 76 61 6c 75 65 20 66 6f 72 20 65  NULL value for e
343b7 69 74 68 65 72 20 0a 2a 2a 20 20 20 20 20 20 70  ither .**      p
343b8 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
343b9 20 6f 72 20 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e   or pBlockingCon
343ba 6e 65 63 74 69 6f 6e 2c 20 6f 72 20 62 6f 74 68  nection, or both
343bb 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 41 6c 6c  ..**.**   2) All
343bc 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20   entries in the 
343bd 6c 69 73 74 20 74 68 61 74 20 73 68 61 72 65 20  list that share 
343be 61 20 63 6f 6d 6d 6f 6e 20 76 61 6c 75 65 20 66  a common value f
343bf 6f 72 20 0a 2a 2a 20 20 20 20 20 20 78 55 6e 6c  or .**      xUnl
343c0 6f 63 6b 4e 6f 74 69 66 79 20 61 72 65 20 67 72  ockNotify are gr
343c1 6f 75 70 65 64 20 74 6f 67 65 74 68 65 72 2e 0a  ouped together..
343c2 2a 2a 0a 2a 2a 20 20 20 33 29 20 49 66 20 74 68  **.**   3) If th
343c3 65 20 61 72 67 75 6d 65 6e 74 20 64 62 20 69 73  e argument db is
343c4 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
343c5 6e 6f 6e 65 20 6f 66 20 74 68 65 20 65 6e 74 72  none of the entr
343c6 69 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20  ies in the.**   
343c7 20 20 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65     blocked conne
343c8 63 74 69 6f 6e 73 20 6c 69 73 74 20 68 61 76 65  ctions list have
343c9 20 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69   pUnlockConnecti
343ca 6f 6e 20 6f 72 20 70 42 6c 6f 63 6b 69 6e 67 43  on or pBlockingC
343cb 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 20 20 20  onnection.**    
343cc 20 20 73 65 74 20 74 6f 20 64 62 2e 20 54 68 69    set to db. Thi
343cd 73 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 63  s is used when c
343ce 6c 6f 73 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f  losing connectio
343cf 6e 20 64 62 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  n db..*/.static 
343d0 76 6f 69 64 20 63 68 65 63 6b 4c 69 73 74 50 72  void checkListPr
343d1 6f 70 65 72 74 69 65 73 28 73 71 6c 69 74 65 33  operties(sqlite3
343d2 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74 65 33   *db){.  sqlite3
343d3 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 73 71 6c   *p;.  for(p=sql
343d4 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74 3b  ite3BlockedList;
343d5 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 42 6c   p; p=p->pNextBl
343d6 6f 63 6b 65 64 29 7b 0a 20 20 20 20 69 6e 74 20  ocked){.    int 
343d7 73 65 65 6e 20 3d 20 30 3b 0a 20 20 20 20 73 71  seen = 0;.    sq
343d8 6c 69 74 65 33 20 2a 70 32 3b 0a 0a 20 20 20 20  lite3 *p2;..    
343d9 2f 2a 20 56 65 72 69 66 79 20 70 72 6f 70 65 72  /* Verify proper
343da 74 79 20 28 31 29 20 2a 2f 0a 20 20 20 20 61 73  ty (1) */.    as
343db 73 65 72 74 28 20 70 2d 3e 70 55 6e 6c 6f 63 6b  sert( p->pUnlock
343dc 43 6f 6e 6e 65 63 74 69 6f 6e 20 7c 7c 20 70 2d  Connection || p-
343dd 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63  >pBlockingConnec
343de 74 69 6f 6e 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  tion );..    /* 
343df 56 65 72 69 66 79 20 70 72 6f 70 65 72 74 79 20  Verify property 
343e0 28 32 29 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70  (2) */.    for(p
343e1 32 3d 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64  2=sqlite3Blocked
343e2 4c 69 73 74 3b 20 70 32 21 3d 70 3b 20 70 32 3d  List; p2!=p; p2=
343e3 70 32 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64  p2->pNextBlocked
343e4 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 32 2d  ){.      if( p2-
343e5 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 3d 3d  >xUnlockNotify==
343e6 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79  p->xUnlockNotify
343e7 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20 20   ) seen = 1;.   
343e8 20 20 20 61 73 73 65 72 74 28 20 70 32 2d 3e 78     assert( p2->x
343e9 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 3d 3d 70 2d  UnlockNotify==p-
343ea 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 7c  >xUnlockNotify |
343eb 7c 20 21 73 65 65 6e 20 29 3b 0a 20 20 20 20 20  | !seen );.     
343ec 20 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c   assert( db==0 |
343ed 7c 20 70 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e  | p->pUnlockConn
343ee 65 63 74 69 6f 6e 21 3d 64 62 20 29 3b 0a 20 20  ection!=db );.  
343ef 20 20 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d      assert( db==
343f0 30 20 7c 7c 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e  0 || p->pBlockin
343f1 67 43 6f 6e 6e 65 63 74 69 6f 6e 21 3d 64 62 20  gConnection!=db 
343f2 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23  );.    }.  }.}.#
343f3 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 63 68  else.# define ch
343f4 65 63 6b 4c 69 73 74 50 72 6f 70 65 72 74 69 65  eckListPropertie
343f5 73 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  s(x).#endif../*.
343f6 2a 2a 20 52 65 6d 6f 76 65 20 63 6f 6e 6e 65 63  ** Remove connec
343f7 74 69 6f 6e 20 64 62 20 66 72 6f 6d 20 74 68 65  tion db from the
343f8 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
343f9 69 6f 6e 73 20 6c 69 73 74 2e 20 49 66 20 63 6f  ions list. If co
343fa 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 64 62 20 69  nnection.** db i
343fb 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
343fc 61 20 70 61 72 74 20 6f 66 20 74 68 65 20 6c 69  a part of the li
343fd 73 74 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  st, this functio
343fe 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  n is a no-op..*/
343ff 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6d  .static void rem
34400 6f 76 65 46 72 6f 6d 42 6c 6f 63 6b 65 64 4c 69  oveFromBlockedLi
34401 73 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  st(sqlite3 *db){
34402 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 3b  .  sqlite3 **pp;
34403 0a 20 20 61 73 73 65 72 74 4d 75 74 65 78 48 65  .  assertMutexHe
34404 6c 64 28 29 3b 0a 20 20 66 6f 72 28 70 70 3d 26  ld();.  for(pp=&
34405 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69  sqlite3BlockedLi
34406 73 74 3b 20 2a 70 70 3b 20 70 70 20 3d 20 26 28  st; *pp; pp = &(
34407 2a 70 70 29 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b  *pp)->pNextBlock
34408 65 64 29 7b 0a 20 20 20 20 69 66 28 20 2a 70 70  ed){.    if( *pp
34409 3d 3d 64 62 20 29 7b 0a 20 20 20 20 20 20 2a 70  ==db ){.      *p
3440a 70 20 3d 20 28 2a 70 70 29 2d 3e 70 4e 65 78 74  p = (*pp)->pNext
3440b 42 6c 6f 63 6b 65 64 3b 0a 20 20 20 20 20 20 62  Blocked;.      b
3440c 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
3440d 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 63 6f 6e  }../*.** Add con
3440e 6e 65 63 74 69 6f 6e 20 64 62 20 74 6f 20 74 68  nection db to th
3440f 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63  e blocked connec
34410 74 69 6f 6e 73 20 6c 69 73 74 2e 20 49 74 20 69  tions list. It i
34411 73 20 61 73 73 75 6d 65 64 0a 2a 2a 20 74 68 61  s assumed.** tha
34412 74 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65  t it is not alre
34413 61 64 79 20 61 20 70 61 72 74 20 6f 66 20 74 68  ady a part of th
34414 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69  e list..*/.stati
34415 63 20 76 6f 69 64 20 61 64 64 54 6f 42 6c 6f 63  c void addToBloc
34416 6b 65 64 4c 69 73 74 28 73 71 6c 69 74 65 33 20  kedList(sqlite3 
34417 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  *db){.  sqlite3 
34418 2a 2a 70 70 3b 0a 20 20 61 73 73 65 72 74 4d 75  **pp;.  assertMu
34419 74 65 78 48 65 6c 64 28 29 3b 0a 20 20 66 6f 72  texHeld();.  for
3441a 28 0a 20 20 20 20 70 70 3d 26 73 71 6c 69 74 65  (.    pp=&sqlite
3441b 33 42 6c 6f 63 6b 65 64 4c 69 73 74 3b 20 0a 20  3BlockedList; . 
3441c 20 20 20 2a 70 70 20 26 26 20 28 2a 70 70 29 2d     *pp && (*pp)-
3441d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 21 3d  >xUnlockNotify!=
3441e 64 62 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66  db->xUnlockNotif
3441f 79 3b 20 0a 20 20 20 20 70 70 3d 26 28 2a 70 70  y; .    pp=&(*pp
34420 29 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64 0a  )->pNextBlocked.
34421 20 20 29 3b 0a 20 20 64 62 2d 3e 70 4e 65 78 74    );.  db->pNext
34422 42 6c 6f 63 6b 65 64 20 3d 20 2a 70 70 3b 0a 20  Blocked = *pp;. 
34423 20 2a 70 70 20 3d 20 64 62 3b 0a 7d 0a 0a 2f 2a   *pp = db;.}../*
34424 0a 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 53  .** Obtain the S
34425 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74  TATIC_MASTER mut
34426 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ex..*/.static vo
34427 69 64 20 65 6e 74 65 72 4d 75 74 65 78 28 76 6f  id enterMutex(vo
34428 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  id){.  sqlite3_m
34429 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74  utex_enter(sqlit
3442a 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
3442b 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
3442c 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 63 68 65  _MASTER));.  che
3442d 63 6b 4c 69 73 74 50 72 6f 70 65 72 74 69 65 73  ckListProperties
3442e 28 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  (0);.}../*.** Re
3442f 6c 65 61 73 65 20 74 68 65 20 53 54 41 54 49 43  lease the STATIC
34430 5f 4d 41 53 54 45 52 20 6d 75 74 65 78 2e 0a 2a  _MASTER mutex..*
34431 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65  /.static void le
34432 61 76 65 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a  aveMutex(void){.
34433 20 20 61 73 73 65 72 74 4d 75 74 65 78 48 65 6c    assertMutexHel
34434 64 28 29 3b 0a 20 20 63 68 65 63 6b 4c 69 73 74  d();.  checkList
34435 50 72 6f 70 65 72 74 69 65 73 28 30 29 3b 0a 20  Properties(0);. 
34436 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
34437 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65  eave(sqlite3Mute
34438 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
34439 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
3443a 52 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  R));.}../*.** Re
3443b 67 69 73 74 65 72 20 61 6e 20 75 6e 6c 6f 63 6b  gister an unlock
3443c 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
3443d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ..**.** This is 
3443e 63 61 6c 6c 65 64 20 61 66 74 65 72 20 63 6f 6e  called after con
3443f 6e 65 63 74 69 6f 6e 20 22 64 62 22 20 68 61 73  nection "db" has
34440 20 61 74 74 65 6d 70 74 65 64 20 73 6f 6d 65 20   attempted some 
34441 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 62 75 74  operation.** but
34442 20 68 61 73 20 72 65 63 65 69 76 65 64 20 61 6e   has received an
34443 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 65   SQLITE_LOCKED e
34444 72 72 6f 72 20 62 65 63 61 75 73 65 20 61 6e 6f  rror because ano
34445 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  ther connection.
34446 2a 2a 20 28 63 61 6c 6c 20 69 74 20 70 4f 74 68  ** (call it pOth
34447 65 72 29 20 69 6e 20 74 68 65 20 73 61 6d 65 20  er) in the same 
34448 70 72 6f 63 65 73 73 20 77 61 73 20 62 75 73 79  process was busy
34449 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20   using the same 
3444a 73 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 2e  shared.** cache.
3444b 20 20 70 4f 74 68 65 72 20 69 73 20 66 6f 75 6e    pOther is foun
3444c 64 20 62 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20  d by looking at 
3444d 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e  db->pBlockingCon
3444e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49  nection..**.** I
3444f 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 62 6c  f there is no bl
34450 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f  ocking connectio
34451 6e 2c 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  n, the callback 
34452 69 73 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64  is invoked immed
34453 69 61 74 65 6c 79 2c 0a 2a 2a 20 62 65 66 6f 72  iately,.** befor
34454 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  e this routine r
34455 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  eturns..**.** If
34456 20 70 4f 74 68 65 72 20 69 73 20 61 6c 72 65 61   pOther is alrea
34457 64 79 20 62 6c 6f 63 6b 65 64 20 6f 6e 20 64 62  dy blocked on db
34458 2c 20 74 68 65 6e 20 72 65 70 6f 72 74 20 53 51  , then report SQ
34459 4c 49 54 45 5f 4c 4f 43 4b 45 44 2c 20 74 6f 20  LITE_LOCKED, to 
3445a 69 6e 64 69 63 61 74 65 0a 2a 2a 20 61 20 64 65  indicate.** a de
3445b 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4f 74  adlock..**.** Ot
3445c 68 65 72 77 69 73 65 2c 20 6d 61 6b 65 20 61 72  herwise, make ar
3445d 72 61 6e 67 65 6d 65 6e 74 73 20 74 6f 20 69 6e  rangements to in
3445e 76 6f 6b 65 20 78 4e 6f 74 69 66 79 20 77 68 65  voke xNotify whe
3445f 6e 20 70 4f 74 68 65 72 20 64 72 6f 70 73 0a 2a  n pOther drops.*
34460 2a 20 69 74 73 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a  * its locks..**.
34461 2a 2a 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20  ** Each call to 
34462 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 76 65  this routine ove
34463 72 72 69 64 65 73 20 61 6e 79 20 70 72 69 6f 72  rrides any prior
34464 20 63 61 6c 6c 62 61 63 6b 73 20 72 65 67 69 73   callbacks regis
34465 74 65 72 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20  tered.** on the 
34466 73 61 6d 65 20 22 64 62 22 2e 20 20 49 66 20 78  same "db".  If x
34467 4e 6f 74 69 66 79 3d 3d 30 20 74 68 65 6e 20 61  Notify==0 then a
34468 6e 79 20 70 72 69 6f 72 20 63 61 6c 6c 62 61 63  ny prior callbac
34469 6b 73 20 61 72 65 20 69 6d 6d 65 64 69 61 74 65  ks are immediate
3446a 6c 79 0a 2a 2a 20 63 61 6e 63 65 6c 6c 65 64 2e  ly.** cancelled.
3446b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
3446c 6e 74 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63  nt sqlite3_unloc
3446d 6b 5f 6e 6f 74 69 66 79 28 0a 20 20 73 71 6c 69  k_notify(.  sqli
3446e 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20  te3 *db,.  void 
3446f 28 2a 78 4e 6f 74 69 66 79 29 28 76 6f 69 64 20  (*xNotify)(void 
34470 2a 2a 2c 20 69 6e 74 29 2c 0a 20 20 76 6f 69 64  **, int),.  void
34471 20 2a 70 41 72 67 0a 29 7b 0a 20 20 69 6e 74 20   *pArg.){.  int 
34472 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
34473 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
34474 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
34475 29 3b 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 28  );.  enterMutex(
34476 29 3b 0a 0a 20 20 69 66 28 20 78 4e 6f 74 69 66  );..  if( xNotif
34477 79 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 6d 6f  y==0 ){.    remo
34478 76 65 46 72 6f 6d 42 6c 6f 63 6b 65 64 4c 69 73  veFromBlockedLis
34479 74 28 64 62 29 3b 0a 20 20 20 20 64 62 2d 3e 70  t(db);.    db->p
3447a 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
3447b 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 78 55   = 0;.    db->xU
3447c 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 3d 20 30 3b  nlockNotify = 0;
3447d 0a 20 20 20 20 64 62 2d 3e 70 55 6e 6c 6f 63 6b  .    db->pUnlock
3447e 41 72 67 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  Arg = 0;.  }else
3447f 20 69 66 28 20 30 3d 3d 64 62 2d 3e 70 42 6c 6f   if( 0==db->pBlo
34480 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20  ckingConnection 
34481 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 62 6c  ){.    /* The bl
34482 6f 63 6b 69 6e 67 20 74 72 61 6e 73 61 63 74 69  ocking transacti
34483 6f 6e 20 68 61 73 20 62 65 65 6e 20 63 6f 6e 63  on has been conc
34484 6c 75 64 65 64 2e 20 4f 72 20 74 68 65 72 65 20  luded. Or there 
34485 6e 65 76 65 72 20 77 61 73 20 61 20 0a 20 20 20  never was a .   
34486 20 2a 2a 20 62 6c 6f 63 6b 69 6e 67 20 74 72 61   ** blocking tra
34487 6e 73 61 63 74 69 6f 6e 2e 20 49 6e 20 65 69 74  nsaction. In eit
34488 68 65 72 20 63 61 73 65 2c 20 69 6e 76 6f 6b 65  her case, invoke
34489 20 74 68 65 20 6e 6f 74 69 66 79 20 63 61 6c 6c   the notify call
3448a 62 61 63 6b 0a 20 20 20 20 2a 2a 20 69 6d 6d 65  back.    ** imme
3448b 64 69 61 74 65 6c 79 2e 20 0a 20 20 20 20 2a 2f  diately. .    */
3448c 0a 20 20 20 20 78 4e 6f 74 69 66 79 28 26 70 41  .    xNotify(&pA
3448d 72 67 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b  rg, 1);.  }else{
3448e 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 70 3b  .    sqlite3 *p;
3448f 0a 0a 20 20 20 20 66 6f 72 28 70 3d 64 62 2d 3e  ..    for(p=db->
34490 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74  pBlockingConnect
34491 69 6f 6e 3b 20 70 20 26 26 20 70 21 3d 64 62 3b  ion; p && p!=db;
34492 20 70 3d 70 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e   p=p->pUnlockCon
34493 6e 65 63 74 69 6f 6e 29 7b 7d 0a 20 20 20 20 69  nection){}.    i
34494 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 72 63  f( p ){.      rc
34495 20 3d 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44   = SQLITE_LOCKED
34496 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
34497 2a 20 44 65 61 64 6c 6f 63 6b 20 64 65 74 65 63  * Deadlock detec
34498 74 65 64 2e 20 2a 2f 0a 20 20 20 20 7d 65 6c 73  ted. */.    }els
34499 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 70 55 6e  e{.      db->pUn
3449a 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d  lockConnection =
3449b 20 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f   db->pBlockingCo
3449c 6e 6e 65 63 74 69 6f 6e 3b 0a 20 20 20 20 20 20  nnection;.      
3449d 64 62 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66  db->xUnlockNotif
3449e 79 20 3d 20 78 4e 6f 74 69 66 79 3b 0a 20 20 20  y = xNotify;.   
3449f 20 20 20 64 62 2d 3e 70 55 6e 6c 6f 63 6b 41 72     db->pUnlockAr
344a0 67 20 3d 20 70 41 72 67 3b 0a 20 20 20 20 20 20  g = pArg;.      
344a1 72 65 6d 6f 76 65 46 72 6f 6d 42 6c 6f 63 6b 65  removeFromBlocke
344a2 64 4c 69 73 74 28 64 62 29 3b 0a 20 20 20 20 20  dList(db);.     
344a3 20 61 64 64 54 6f 42 6c 6f 63 6b 65 64 4c 69 73   addToBlockedLis
344a4 74 28 64 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  t(db);.    }.  }
344a5 0a 0a 20 20 6c 65 61 76 65 4d 75 74 65 78 28 29  ..  leaveMutex()
344a6 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d  ;.  assert( !db-
344a7 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
344a8 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  .  sqlite3Error(
344a9 64 62 2c 20 72 63 2c 20 28 72 63 3f 22 64 61 74  db, rc, (rc?"dat
344aa 61 62 61 73 65 20 69 73 20 64 65 61 64 6c 6f 63  abase is deadloc
344ab 6b 65 64 22 3a 30 29 29 3b 0a 20 20 73 71 6c 69  ked":0));.  sqli
344ac 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
344ad 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
344ae 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
344af 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
344b0 69 73 20 63 61 6c 6c 65 64 20 77 68 69 6c 65 20  is called while 
344b1 73 74 65 70 70 69 6e 67 20 6f 72 20 70 72 65 70  stepping or prep
344b2 61 72 69 6e 67 20 61 20 73 74 61 74 65 6d 65 6e  aring a statemen
344b3 74 20 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64  t .** associated
344b4 20 77 69 74 68 20 63 6f 6e 6e 65 63 74 69 6f 6e   with connection
344b5 20 64 62 2e 20 54 68 65 20 6f 70 65 72 61 74 69   db. The operati
344b6 6f 6e 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53  on will return S
344b7 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 0a 2a 2a 20  QLITE_LOCKED.** 
344b8 74 6f 20 74 68 65 20 75 73 65 72 20 62 65 63 61  to the user beca
344b9 75 73 65 20 69 74 20 72 65 71 75 69 72 65 73 20  use it requires 
344ba 61 20 6c 6f 63 6b 20 74 68 61 74 20 77 69 6c 6c  a lock that will
344bb 20 6e 6f 74 20 62 65 20 61 76 61 69 6c 61 62 6c   not be availabl
344bc 65 0a 2a 2a 20 75 6e 74 69 6c 20 63 6f 6e 6e 65  e.** until conne
344bd 63 74 69 6f 6e 20 70 42 6c 6f 63 6b 65 72 20 63  ction pBlocker c
344be 6f 6e 63 6c 75 64 65 73 20 69 74 73 20 63 75 72  oncludes its cur
344bf 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  rent transaction
344c0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
344c1 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
344c2 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65  ConnectionBlocke
344c3 64 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 73  d(sqlite3 *db, s
344c4 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b 65 72  qlite3 *pBlocker
344c5 29 7b 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 28  ){.  enterMutex(
344c6 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 70 42 6c  );.  if( db->pBl
344c7 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
344c8 3d 3d 30 20 26 26 20 64 62 2d 3e 70 55 6e 6c 6f  ==0 && db->pUnlo
344c9 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30 20  ckConnection==0 
344ca 29 7b 0a 20 20 20 20 61 64 64 54 6f 42 6c 6f 63  ){.    addToBloc
344cb 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a 20 20 7d  kedList(db);.  }
344cc 0a 20 20 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67  .  db->pBlocking
344cd 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 70 42 6c  Connection = pBl
344ce 6f 63 6b 65 72 3b 0a 20 20 6c 65 61 76 65 4d 75  ocker;.  leaveMu
344cf 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  tex();.}../*.** 
344d0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
344d1 20 63 61 6c 6c 65 64 20 77 68 65 6e 0a 2a 2a 20   called when.** 
344d2 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
344d3 6f 70 65 6e 65 64 20 62 79 20 64 61 74 61 62 61  opened by databa
344d4 73 65 20 64 62 20 68 61 73 20 6a 75 73 74 20 66  se db has just f
344d5 69 6e 69 73 68 65 64 2e 20 4c 6f 63 6b 73 20 68  inished. Locks h
344d6 65 6c 64 20 0a 2a 2a 20 62 79 20 64 61 74 61 62  eld .** by datab
344d7 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64  ase connection d
344d8 62 20 68 61 76 65 20 62 65 65 6e 20 72 65 6c 65  b have been rele
344d9 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ased..**.** This
344da 20 66 75 6e 63 74 69 6f 6e 20 6c 6f 6f 70 73 20   function loops 
344db 74 68 72 6f 75 67 68 20 65 61 63 68 20 65 6e 74  through each ent
344dc 72 79 20 69 6e 20 74 68 65 20 62 6c 6f 63 6b 65  ry in the blocke
344dd 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a 2a 2a  d connections.**
344de 20 6c 69 73 74 20 61 6e 64 20 64 6f 65 73 20 74   list and does t
344df 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a  he following:.**
344e0 0a 2a 2a 20 20 20 31 29 20 49 66 20 74 68 65 20  .**   1) If the 
344e1 73 71 6c 69 74 65 33 2e 70 42 6c 6f 63 6b 69 6e  sqlite3.pBlockin
344e2 67 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d 65 6d 62  gConnection memb
344e3 65 72 20 6f 66 20 61 20 6c 69 73 74 20 65 6e 74  er of a list ent
344e4 72 79 20 69 73 0a 2a 2a 20 20 20 20 20 20 73 65  ry is.**      se
344e5 74 20 74 6f 20 64 62 2c 20 74 68 65 6e 20 73 65  t to db, then se
344e6 74 20 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65  t pBlockingConne
344e7 63 74 69 6f 6e 3d 30 2e 0a 2a 2a 0a 2a 2a 20 20  ction=0..**.**  
344e8 20 32 29 20 49 66 20 74 68 65 20 73 71 6c 69 74   2) If the sqlit
344e9 65 33 2e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63  e3.pUnlockConnec
344ea 74 69 6f 6e 20 6d 65 6d 62 65 72 20 6f 66 20 61  tion member of a
344eb 20 6c 69 73 74 20 65 6e 74 72 79 20 69 73 0a 2a   list entry is.*
344ec 2a 20 20 20 20 20 20 73 65 74 20 74 6f 20 64 62  *      set to db
344ed 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20 74 68  , then invoke th
344ee 65 20 63 6f 6e 66 69 67 75 72 65 64 20 75 6e 6c  e configured unl
344ef 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
344f0 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  ack and.**      
344f1 73 65 74 20 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65  set pUnlockConne
344f2 63 74 69 6f 6e 3d 30 2e 0a 2a 2a 0a 2a 2a 20 20  ction=0..**.**  
344f3 20 33 29 20 49 66 20 74 68 65 20 74 77 6f 20 73   3) If the two s
344f4 74 65 70 73 20 61 62 6f 76 65 20 6d 65 61 6e 20  teps above mean 
344f5 74 68 61 74 20 70 42 6c 6f 63 6b 69 6e 67 43 6f  that pBlockingCo
344f6 6e 6e 65 63 74 69 6f 6e 3d 3d 30 20 61 6e 64 0a  nnection==0 and.
344f7 2a 2a 20 20 20 20 20 20 70 55 6e 6c 6f 63 6b 43  **      pUnlockC
344f8 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30 2c 20 72 65  onnection==0, re
344f9 6d 6f 76 65 20 74 68 65 20 65 6e 74 72 79 20 66  move the entry f
344fa 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b 65 64 20  rom the blocked 
344fb 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a 2a 2a 20 20  connections.**  
344fc 20 20 20 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c      list..*/.SQL
344fd 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
344fe 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69   sqlite3Connecti
344ff 6f 6e 55 6e 6c 6f 63 6b 65 64 28 73 71 6c 69 74  onUnlocked(sqlit
34500 65 33 20 2a 64 62 29 7b 0a 20 20 76 6f 69 64 20  e3 *db){.  void 
34501 28 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 29  (*xUnlockNotify)
34502 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 29 20 3d  (void **, int) =
34503 20 30 3b 20 2f 2a 20 55 6e 6c 6f 63 6b 2d 6e 6f   0; /* Unlock-no
34504 74 69 66 79 20 63 62 20 74 6f 20 69 6e 76 6f 6b  tify cb to invok
34505 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 20  e */.  int nArg 
34506 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
34507 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34508 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
34509 72 69 65 73 20 69 6e 20 61 41 72 67 5b 5d 20 2a  ries in aArg[] *
3450a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70  /.  sqlite3 **pp
3450b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3450c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3450d 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c  Iterator variabl
3450e 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2a 61 41  e */.  void **aA
3450f 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rg;             
34510 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20 74    /* Arguments t
34511 6f 20 74 68 65 20 75 6e 6c 6f 63 6b 20 63 61 6c  o the unlock cal
34512 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 20  lback */.  void 
34513 2a 2a 61 44 79 6e 20 3d 20 30 3b 20 20 20 20 20  **aDyn = 0;     
34514 20 20 20 20 20 20 2f 2a 20 44 79 6e 61 6d 69 63        /* Dynamic
34515 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73  ally allocated s
34516 70 61 63 65 20 66 6f 72 20 61 41 72 67 5b 5d 20  pace for aArg[] 
34517 2a 2f 0a 20 20 76 6f 69 64 20 2a 61 53 74 61 74  */.  void *aStat
34518 69 63 5b 31 36 5d 3b 20 20 20 20 20 20 20 20 20  ic[16];         
34519 2f 2a 20 53 74 61 72 74 65 72 20 73 70 61 63 65  /* Starter space
3451a 20 66 6f 72 20 61 41 72 67 5b 5d 2e 20 20 4e 6f   for aArg[].  No
3451b 20 6d 61 6c 6c 6f 63 20 72 65 71 75 69 72 65 64   malloc required
3451c 20 2a 2f 0a 0a 20 20 61 41 72 67 20 3d 20 61 53   */..  aArg = aS
3451d 74 61 74 69 63 3b 0a 20 20 65 6e 74 65 72 4d 75  tatic;.  enterMu
3451e 74 65 78 28 29 3b 20 20 20 20 20 20 20 20 20 2f  tex();         /
3451f 2a 20 45 6e 74 65 72 20 53 54 41 54 49 43 5f 4d  * Enter STATIC_M
34520 41 53 54 45 52 20 6d 75 74 65 78 20 2a 2f 0a 0a  ASTER mutex */..
34521 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 72    /* This loop r
34522 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 63  uns once for eac
34523 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 62  h entry in the b
34524 6c 6f 63 6b 65 64 2d 63 6f 6e 6e 65 63 74 69 6f  locked-connectio
34525 6e 73 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 66 6f  ns list. */.  fo
34526 72 28 70 70 3d 26 73 71 6c 69 74 65 33 42 6c 6f  r(pp=&sqlite3Blo
34527 63 6b 65 64 4c 69 73 74 3b 20 2a 70 70 3b 20 2f  ckedList; *pp; /
34528 2a 20 6e 6f 2d 6f 70 20 2a 2f 20 29 7b 0a 20 20  * no-op */ ){.  
34529 20 20 73 71 6c 69 74 65 33 20 2a 70 20 3d 20 2a    sqlite3 *p = *
3452a 70 70 3b 0a 0a 20 20 20 20 2f 2a 20 53 74 65 70  pp;..    /* Step
3452b 20 31 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70   1. */.    if( p
3452c 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65  ->pBlockingConne
3452d 63 74 69 6f 6e 3d 3d 64 62 20 29 7b 0a 20 20 20  ction==db ){.   
3452e 20 20 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43     p->pBlockingC
3452f 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 30 3b 0a 20  onnection = 0;. 
34530 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 74 65     }..    /* Ste
34531 70 20 32 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  p 2. */.    if( 
34532 70 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63  p->pUnlockConnec
34533 74 69 6f 6e 3d 3d 64 62 20 29 7b 0a 20 20 20 20  tion==db ){.    
34534 20 20 61 73 73 65 72 74 28 20 70 2d 3e 78 55 6e    assert( p->xUn
34535 6c 6f 63 6b 4e 6f 74 69 66 79 20 29 3b 0a 20 20  lockNotify );.  
34536 20 20 20 20 69 66 28 20 70 2d 3e 78 55 6e 6c 6f      if( p->xUnlo
34537 63 6b 4e 6f 74 69 66 79 21 3d 78 55 6e 6c 6f 63  ckNotify!=xUnloc
34538 6b 4e 6f 74 69 66 79 20 26 26 20 6e 41 72 67 21  kNotify && nArg!
34539 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 78 55  =0 ){.        xU
3453a 6e 6c 6f 63 6b 4e 6f 74 69 66 79 28 61 41 72 67  nlockNotify(aArg
3453b 2c 20 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 20  , nArg);.       
3453c 20 6e 41 72 67 20 3d 20 30 3b 0a 20 20 20 20 20   nArg = 0;.     
3453d 20 7d 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65   }..      sqlite
3453e 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c  3BeginBenignMall
3453f 6f 63 28 29 3b 0a 20 20 20 20 20 20 61 73 73 65  oc();.      asse
34540 72 74 28 20 61 41 72 67 3d 3d 61 44 79 6e 20 7c  rt( aArg==aDyn |
34541 7c 20 28 61 44 79 6e 3d 3d 30 20 26 26 20 61 41  | (aDyn==0 && aA
34542 72 67 3d 3d 61 53 74 61 74 69 63 29 20 29 3b 0a  rg==aStatic) );.
34543 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 41        assert( nA
34544 72 67 3c 3d 28 69 6e 74 29 41 72 72 61 79 53 69  rg<=(int)ArraySi
34545 7a 65 28 61 53 74 61 74 69 63 29 20 7c 7c 20 61  ze(aStatic) || a
34546 41 72 67 3d 3d 61 44 79 6e 20 29 3b 0a 20 20 20  Arg==aDyn );.   
34547 20 20 20 69 66 28 20 28 21 61 44 79 6e 20 26 26     if( (!aDyn &&
34548 20 6e 41 72 67 3d 3d 28 69 6e 74 29 41 72 72 61   nArg==(int)Arra
34549 79 53 69 7a 65 28 61 53 74 61 74 69 63 29 29 0a  ySize(aStatic)).
3454a 20 20 20 20 20 20 20 7c 7c 20 28 61 44 79 6e 20         || (aDyn 
3454b 26 26 20 6e 41 72 67 3d 3d 28 69 6e 74 29 28 73  && nArg==(int)(s
3454c 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69  qlite3DbMallocSi
3454d 7a 65 28 64 62 2c 20 61 44 79 6e 29 2f 73 69 7a  ze(db, aDyn)/siz
3454e 65 6f 66 28 76 6f 69 64 2a 29 29 29 0a 20 20 20  eof(void*))).   
3454f 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a     ){.        /*
34550 20 54 68 65 20 61 41 72 67 5b 5d 20 61 72 72 61   The aArg[] arra
34551 79 20 6e 65 65 64 73 20 74 6f 20 67 72 6f 77 2e  y needs to grow.
34552 20 2a 2f 0a 20 20 20 20 20 20 20 20 76 6f 69 64   */.        void
34553 20 2a 2a 70 4e 65 77 20 3d 20 28 76 6f 69 64 20   **pNew = (void 
34554 2a 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  **)sqlite3Malloc
34555 28 6e 41 72 67 2a 73 69 7a 65 6f 66 28 76 6f 69  (nArg*sizeof(voi
34556 64 20 2a 29 2a 32 29 3b 0a 20 20 20 20 20 20 20  d *)*2);.       
34557 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20   if( pNew ){.   
34558 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 4e         memcpy(pN
34559 65 77 2c 20 61 41 72 67 2c 20 6e 41 72 67 2a 73  ew, aArg, nArg*s
3455a 69 7a 65 6f 66 28 76 6f 69 64 20 2a 29 29 3b 0a  izeof(void *));.
3455b 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
3455c 33 5f 66 72 65 65 28 61 44 79 6e 29 3b 0a 20 20  3_free(aDyn);.  
3455d 20 20 20 20 20 20 20 20 61 44 79 6e 20 3d 20 61          aDyn = a
3455e 41 72 67 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20  Arg = pNew;.    
3455f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
34560 20 20 20 20 20 2f 2a 20 54 68 69 73 20 6f 63 63       /* This occ
34561 75 72 73 20 77 68 65 6e 20 74 68 65 20 61 72 72  urs when the arr
34562 61 79 20 6f 66 20 63 6f 6e 74 65 78 74 20 70 6f  ay of context po
34563 69 6e 74 65 72 73 20 74 68 61 74 20 6e 65 65 64  inters that need
34564 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a   to.          **
34565 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 74 68   be passed to th
34566 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  e unlock-notify 
34567 63 61 6c 6c 62 61 63 6b 20 69 73 20 6c 61 72 67  callback is larg
34568 65 72 20 74 68 61 6e 20 74 68 65 0a 20 20 20 20  er than the.    
34569 20 20 20 20 20 20 2a 2a 20 61 53 74 61 74 69 63        ** aStatic
3456a 5b 5d 20 61 72 72 61 79 20 61 6c 6c 6f 63 61 74  [] array allocat
3456b 65 64 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20  ed on the stack 
3456c 61 6e 64 20 74 68 65 20 61 74 74 65 6d 70 74 20  and the attempt 
3456d 74 6f 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  to .          **
3456e 20 61 6c 6c 6f 63 61 74 65 20 61 20 6c 61 72 67   allocate a larg
3456f 65 72 20 61 72 72 61 79 20 66 72 6f 6d 20 74 68  er array from th
34570 65 20 68 65 61 70 20 68 61 73 20 66 61 69 6c 65  e heap has faile
34571 64 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 0a  d..          **.
34572 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69            ** Thi
34573 73 20 69 73 20 61 20 64 69 66 66 69 63 75 6c 74  s is a difficult
34574 20 73 69 74 75 61 74 69 6f 6e 20 74 6f 20 68 61   situation to ha
34575 6e 64 6c 65 2e 20 52 65 74 75 72 6e 69 6e 67 20  ndle. Returning 
34576 61 6e 20 65 72 72 6f 72 0a 20 20 20 20 20 20 20  an error.       
34577 20 20 20 2a 2a 20 63 6f 64 65 20 74 6f 20 74 68     ** code to th
34578 65 20 63 61 6c 6c 65 72 20 69 73 20 69 6e 73 75  e caller is insu
34579 66 66 69 63 69 65 6e 74 2c 20 61 73 20 65 76 65  fficient, as eve
3457a 6e 20 69 66 20 61 6e 20 65 72 72 6f 72 20 63 6f  n if an error co
3457b 64 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  de.          ** 
3457c 69 73 20 72 65 74 75 72 6e 65 64 20 74 68 65 20  is returned the 
3457d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 63  transaction on c
3457e 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 77 69 6c  onnection db wil
3457f 6c 20 73 74 69 6c 6c 20 62 65 0a 20 20 20 20 20  l still be.     
34580 20 20 20 20 20 2a 2a 20 63 6c 6f 73 65 64 20 61       ** closed a
34581 6e 64 20 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f  nd the unlock-no
34582 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 73 20 6f  tify callbacks o
34583 6e 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63  n blocked connec
34584 74 69 6f 6e 73 0a 20 20 20 20 20 20 20 20 20 20  tions.          
34585 2a 2a 20 77 69 6c 6c 20 67 6f 20 75 6e 69 73 73  ** will go uniss
34586 75 65 64 2e 20 54 68 69 73 20 6d 69 67 68 74 20  ued. This might 
34587 63 61 75 73 65 20 74 68 65 20 61 70 70 6c 69 63  cause the applic
34588 61 74 69 6f 6e 20 74 6f 20 77 61 69 74 0a 20 20  ation to wait.  
34589 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 64 65 66          ** indef
3458a 69 6e 69 74 65 6c 79 20 66 6f 72 20 61 6e 20 75  initely for an u
3458b 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c  nlock-notify cal
3458c 6c 62 61 63 6b 20 74 68 61 74 20 77 69 6c 6c 20  lback that will 
3458d 6e 65 76 65 72 20 0a 20 20 20 20 20 20 20 20 20  never .         
3458e 20 2a 2a 20 61 72 72 69 76 65 2e 0a 20 20 20 20   ** arrive..    
3458f 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
34590 20 20 20 2a 2a 20 49 6e 73 74 65 61 64 2c 20 69     ** Instead, i
34591 6e 76 6f 6b 65 20 74 68 65 20 75 6e 6c 6f 63 6b  nvoke the unlock
34592 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
34593 20 77 69 74 68 20 74 68 65 20 63 6f 6e 74 65 78   with the contex
34594 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61  t.          ** a
34595 72 72 61 79 20 61 6c 72 65 61 64 79 20 61 63 63  rray already acc
34596 75 6d 75 6c 61 74 65 64 2e 20 57 65 20 63 61 6e  umulated. We can
34597 20 74 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20   then clear the 
34598 61 72 72 61 79 20 61 6e 64 0a 20 20 20 20 20 20  array and.      
34599 20 20 20 20 2a 2a 20 62 65 67 69 6e 20 61 63 63      ** begin acc
3459a 75 6d 75 6c 61 74 69 6e 67 20 61 6e 79 20 66 75  umulating any fu
3459b 72 74 68 65 72 20 63 6f 6e 74 65 78 74 20 70 6f  rther context po
3459c 69 6e 74 65 72 73 20 77 69 74 68 6f 75 74 20 0a  inters without .
3459d 20 20 20 20 20 20 20 20 20 20 2a 2a 20 72 65 71            ** req
3459e 75 69 72 69 6e 67 20 61 6e 79 20 64 79 6e 61 6d  uiring any dynam
3459f 69 63 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 54  ic allocation. T
345a0 68 69 73 20 69 73 20 73 75 62 2d 6f 70 74 69 6d  his is sub-optim
345a1 61 6c 20 62 65 63 61 75 73 65 0a 20 20 20 20 20  al because.     
345a2 20 20 20 20 20 2a 2a 20 69 74 20 6d 65 61 6e 73       ** it means
345a3 20 74 68 61 74 20 69 6e 73 74 65 61 64 20 6f 66   that instead of
345a4 20 6f 6e 65 20 63 61 6c 6c 62 61 63 6b 20 77 69   one callback wi
345a5 74 68 20 61 20 6c 61 72 67 65 20 61 72 72 61 79  th a large array
345a6 20 6f 66 0a 20 20 20 20 20 20 20 20 20 20 2a 2a   of.          **
345a7 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72   context pointer
345a8 73 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  s the applicatio
345a9 6e 20 77 69 6c 6c 20 72 65 63 65 69 76 65 20 74  n will receive t
345aa 77 6f 20 6f 72 20 6d 6f 72 65 0a 20 20 20 20 20  wo or more.     
345ab 20 20 20 20 20 2a 2a 20 63 61 6c 6c 62 61 63 6b       ** callback
345ac 73 20 77 69 74 68 20 73 6d 61 6c 6c 65 72 20 61  s with smaller a
345ad 72 72 61 79 73 20 6f 66 20 63 6f 6e 74 65 78 74  rrays of context
345ae 20 70 6f 69 6e 74 65 72 73 2c 20 77 68 69 63 68   pointers, which
345af 20 77 69 6c 6c 0a 20 20 20 20 20 20 20 20 20 20   will.          
345b0 2a 2a 20 72 65 64 75 63 65 20 74 68 65 20 61 70  ** reduce the ap
345b1 70 6c 69 63 61 74 69 6f 6e 73 20 61 62 69 6c 69  plications abili
345b2 74 79 20 74 6f 20 70 72 69 6f 72 69 74 69 7a 65  ty to prioritize
345b3 20 6d 75 6c 74 69 70 6c 65 20 0a 20 20 20 20 20   multiple .     
345b4 20 20 20 20 20 2a 2a 20 63 6f 6e 6e 65 63 74 69       ** connecti
345b5 6f 6e 73 2e 20 42 75 74 20 69 74 20 69 73 20 74  ons. But it is t
345b6 68 65 20 62 65 73 74 20 74 68 61 74 20 63 61 6e  he best that can
345b7 20 62 65 20 64 6f 6e 65 20 75 6e 64 65 72 20 74   be done under t
345b8 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  he.          ** 
345b9 63 69 72 63 75 6d 73 74 61 6e 63 65 73 2e 0a 20  circumstances.. 
345ba 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
345bb 20 20 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74        xUnlockNot
345bc 69 66 79 28 61 41 72 67 2c 20 6e 41 72 67 29 3b  ify(aArg, nArg);
345bd 0a 20 20 20 20 20 20 20 20 20 20 6e 41 72 67 20  .          nArg 
345be 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 0;.        }. 
345bf 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
345c0 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
345c1 6c 6f 63 28 29 3b 0a 0a 20 20 20 20 20 20 61 41  loc();..      aA
345c2 72 67 5b 6e 41 72 67 2b 2b 5d 20 3d 20 70 2d 3e  rg[nArg++] = p->
345c3 70 55 6e 6c 6f 63 6b 41 72 67 3b 0a 20 20 20 20  pUnlockArg;.    
345c4 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20    xUnlockNotify 
345c5 3d 20 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69  = p->xUnlockNoti
345c6 66 79 3b 0a 20 20 20 20 20 20 70 2d 3e 70 55 6e  fy;.      p->pUn
345c7 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d  lockConnection =
345c8 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 78 55 6e   0;.      p->xUn
345c9 6c 6f 63 6b 4e 6f 74 69 66 79 20 3d 20 30 3b 0a  lockNotify = 0;.
345ca 20 20 20 20 20 20 70 2d 3e 70 55 6e 6c 6f 63 6b        p->pUnlock
345cb 41 72 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a  Arg = 0;.    }..
345cc 20 20 20 20 2f 2a 20 53 74 65 70 20 33 2e 20 2a      /* Step 3. *
345cd 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 42 6c  /.    if( p->pBl
345ce 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
345cf 3d 3d 30 20 26 26 20 70 2d 3e 70 55 6e 6c 6f 63  ==0 && p->pUnloc
345d0 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30 20 29  kConnection==0 )
345d1 7b 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76  {.      /* Remov
345d2 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 20 66  e connection p f
345d3 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b 65 64 20  rom the blocked 
345d4 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74  connections list
345d5 2e 20 2a 2f 0a 20 20 20 20 20 20 2a 70 70 20 3d  . */.      *pp =
345d6 20 70 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64   p->pNextBlocked
345d7 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74  ;.      p->pNext
345d8 42 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a 20 20 20  Blocked = 0;.   
345d9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 70   }else{.      pp
345da 20 3d 20 26 70 2d 3e 70 4e 65 78 74 42 6c 6f 63   = &p->pNextBloc
345db 6b 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ked;.    }.  }..
345dc 20 20 69 66 28 20 6e 41 72 67 21 3d 30 20 29 7b    if( nArg!=0 ){
345dd 0a 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69  .    xUnlockNoti
345de 66 79 28 61 41 72 67 2c 20 6e 41 72 67 29 3b 0a  fy(aArg, nArg);.
345df 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72    }.  sqlite3_fr
345e0 65 65 28 61 44 79 6e 29 3b 0a 20 20 6c 65 61 76  ee(aDyn);.  leav
345e1 65 4d 75 74 65 78 28 29 3b 20 20 20 20 20 20 20  eMutex();       
345e2 20 20 2f 2a 20 4c 65 61 76 65 20 53 54 41 54 49    /* Leave STATI
345e3 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78 20 2a  C_MASTER mutex *
345e4 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  /.}../*.** This 
345e5 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74  is called when t
345e6 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
345e7 65 63 74 69 6f 6e 20 70 61 73 73 65 64 20 61 73  ection passed as
345e8 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 20   an argument is 
345e9 0a 2a 2a 20 62 65 69 6e 67 20 63 6c 6f 73 65 64  .** being closed
345ea 2e 20 54 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  . The connection
345eb 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d   is removed from
345ec 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 6c 69 73   the blocked lis
345ed 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
345ee 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
345ef 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65  3ConnectionClose
345f0 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  d(sqlite3 *db){.
345f1 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74    sqlite3Connect
345f2 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 64 62 29 3b  ionUnlocked(db);
345f3 0a 20 20 65 6e 74 65 72 4d 75 74 65 78 28 29 3b  .  enterMutex();
345f4 0a 20 20 72 65 6d 6f 76 65 46 72 6f 6d 42 6c 6f  .  removeFromBlo
345f5 63 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a 20 20  ckedList(db);.  
345f6 63 68 65 63 6b 4c 69 73 74 50 72 6f 70 65 72 74  checkListPropert
345f7 69 65 73 28 64 62 29 3b 0a 20 20 6c 65 61 76 65  ies(db);.  leave
345f8 4d 75 74 65 78 28 29 3b 0a 7d 0a 23 65 6e 64 69  Mutex();.}.#endi
345f9 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
345fa 2a 2a 20 45 6e 64 20 6f 66 20 6e 6f 74 69 66 79  ** End of notify
345fb 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
345fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
345fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
345fe 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
345ff 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74  ** Begin file ft
34600 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s3.c ***********
34601 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34602 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34603 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4f 63  */./*.** 2006 Oc
34604 74 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  t 10.**.** The a
34605 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
34606 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
34607 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
34608 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
34609 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
3460a 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
3460b 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
3460c 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
3460d 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
3460e 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
3460f 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
34610 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
34611 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
34612 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
34613 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
34614 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
34615 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
34616 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34617 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34618 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34619 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3461a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
3461b 68 69 73 20 69 73 20 61 6e 20 53 51 4c 69 74 65  his is an SQLite
3461c 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
3461d 74 69 6e 67 20 66 75 6c 6c 2d 74 65 78 74 20 73  ting full-text s
3461e 65 61 72 63 68 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  earch..*/../*.**
3461f 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
34620 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63  s file is only c
34621 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a  ompiled if:.**.*
34622 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33  *     * The FTS3
34623 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67   module is being
34624 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78 74   built as an ext
34625 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  ension.**       
34626 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
34627 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f  QLITE_CORE is no
34628 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a  t defined), or.*
34629 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46  *.**     * The F
3462a 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65  TS3 module is be
3462b 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f 20 74  ing built into t
3462c 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20  he core of.**   
3462d 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20 77      SQLite (in w
3462e 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45  hich case SQLITE
3462f 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73 20  _ENABLE_FTS3 is 
34630 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 0a 2f 2a  defined)..*/../*
34631 20 54 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e   TODO(shess) Con
34632 73 69 64 65 72 20 65 78 70 6f 72 74 69 6e 67 20  sider exporting 
34633 74 68 69 73 20 63 6f 6d 6d 65 6e 74 20 74 6f 20  this comment to 
34634 61 6e 20 48 54 4d 4c 20 66 69 6c 65 20 6f 72 20  an HTML file or 
34635 74 68 65 0a 2a 2a 20 77 69 6b 69 2e 0a 2a 2f 0a  the.** wiki..*/.
34636 2f 2a 20 54 68 65 20 66 75 6c 6c 2d 74 65 78 74  /* The full-text
34637 20 69 6e 64 65 78 20 69 73 20 73 74 6f 72 65 64   index is stored
34638 20 69 6e 20 61 20 73 65 72 69 65 73 20 6f 66 20   in a series of 
34639 62 2b 74 72 65 65 20 28 2d 6c 69 6b 65 29 0a 2a  b+tree (-like).*
3463a 2a 20 73 74 72 75 63 74 75 72 65 73 20 63 61 6c  * structures cal
3463b 6c 65 64 20 73 65 67 6d 65 6e 74 73 20 77 68 69  led segments whi
3463c 63 68 20 6d 61 70 20 74 65 72 6d 73 20 74 6f 20  ch map terms to 
3463d 64 6f 63 6c 69 73 74 73 2e 20 20 54 68 65 0a 2a  doclists.  The.*
3463e 2a 20 73 74 72 75 63 74 75 72 65 73 20 61 72 65  * structures are
3463f 20 6c 69 6b 65 20 62 2b 74 72 65 65 73 20 69 6e   like b+trees in
34640 20 6c 61 79 6f 75 74 2c 20 62 75 74 20 61 72 65   layout, but are
34641 20 63 6f 6e 73 74 72 75 63 74 65 64 20 66 72 6f   constructed fro
34642 6d 20 74 68 65 0a 2a 2a 20 62 6f 74 74 6f 6d 20  m the.** bottom 
34643 75 70 20 69 6e 20 6f 70 74 69 6d 61 6c 20 66 61  up in optimal fa
34644 73 68 69 6f 6e 20 61 6e 64 20 61 72 65 20 6e 6f  shion and are no
34645 74 20 75 70 64 61 74 61 62 6c 65 2e 20 20 53 69  t updatable.  Si
34646 6e 63 65 20 74 72 65 65 73 0a 2a 2a 20 61 72 65  nce trees.** are
34647 20 62 75 69 6c 74 20 66 72 6f 6d 20 74 68 65 20   built from the 
34648 62 6f 74 74 6f 6d 20 75 70 2c 20 74 68 69 6e 67  bottom up, thing
34649 73 20 77 69 6c 6c 20 62 65 20 64 65 73 63 72 69  s will be descri
3464a 62 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20  bed from the.** 
3464b 62 6f 74 74 6f 6d 20 75 70 2e 0a 2a 2a 0a 2a 2a  bottom up..**.**
3464c 0a 2a 2a 2a 2a 20 56 61 72 69 6e 74 73 20 2a 2a  .**** Varints **
3464d 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73 69 63 20  **.** The basic 
3464e 75 6e 69 74 20 6f 66 20 65 6e 63 6f 64 69 6e 67  unit of encoding
3464f 20 69 73 20 61 20 76 61 72 69 61 62 6c 65 2d 6c   is a variable-l
34650 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 63 61  ength integer ca
34651 6c 6c 65 64 20 61 0a 2a 2a 20 76 61 72 69 6e 74  lled a.** varint
34652 2e 20 20 57 65 20 65 6e 63 6f 64 65 20 76 61 72  .  We encode var
34653 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74  iable-length int
34654 65 67 65 72 73 20 69 6e 20 6c 69 74 74 6c 65 2d  egers in little-
34655 65 6e 64 69 61 6e 20 6f 72 64 65 72 0a 2a 2a 20  endian order.** 
34656 75 73 69 6e 67 20 73 65 76 65 6e 20 62 69 74 73  using seven bits
34657 20 2a 20 70 65 72 20 62 79 74 65 20 61 73 20 66   * per byte as f
34658 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b 45  ollows:.**.** KE
34659 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 41 20  Y:.**         A 
3465a 3d 20 30 78 78 78 78 78 78 78 20 20 20 20 37 20  = 0xxxxxxx    7 
3465b 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64  bits of data and
3465c 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a   one flag bit.**
3465d 20 20 20 20 20 20 20 20 20 42 20 3d 20 31 78 78           B = 1xx
3465e 78 78 78 78 78 20 20 20 20 37 20 62 69 74 73 20  xxxxx    7 bits 
3465f 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20  of data and one 
34660 66 6c 61 67 20 62 69 74 0a 2a 2a 0a 2a 2a 20 20  flag bit.**.**  
34661 37 20 62 69 74 73 20 2d 20 41 0a 2a 2a 20 31 34  7 bits - A.** 14
34662 20 62 69 74 73 20 2d 20 42 41 0a 2a 2a 20 32 31   bits - BA.** 21
34663 20 62 69 74 73 20 2d 20 42 42 41 0a 2a 2a 20 61   bits - BBA.** a
34664 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nd so on..**.** 
34665 54 68 69 73 20 69 73 20 69 64 65 6e 74 69 63 61  This is identica
34666 6c 20 74 6f 20 68 6f 77 20 73 71 6c 69 74 65 20  l to how sqlite 
34667 65 6e 63 6f 64 65 73 20 76 61 72 69 6e 74 73 20  encodes varints 
34668 28 73 65 65 20 75 74 69 6c 2e 63 29 2e 0a 2a 2a  (see util.c)..**
34669 0a 2a 2a 0a 2a 2a 2a 2a 20 44 6f 63 75 6d 65 6e  .**.**** Documen
3466a 74 20 6c 69 73 74 73 20 2a 2a 2a 2a 0a 2a 2a 20  t lists ****.** 
3466b 41 20 64 6f 63 6c 69 73 74 20 28 64 6f 63 75 6d  A doclist (docum
3466c 65 6e 74 20 6c 69 73 74 29 20 68 6f 6c 64 73 20  ent list) holds 
3466d 61 20 64 6f 63 69 64 2d 73 6f 72 74 65 64 20 6c  a docid-sorted l
3466e 69 73 74 20 6f 66 20 68 69 74 73 20 66 6f 72 20  ist of hits for 
3466f 61 0a 2a 2a 20 67 69 76 65 6e 20 74 65 72 6d 2e  a.** given term.
34670 20 20 44 6f 63 6c 69 73 74 73 20 68 6f 6c 64 20    Doclists hold 
34671 64 6f 63 69 64 73 2c 20 61 6e 64 20 63 61 6e 20  docids, and can 
34672 6f 70 74 69 6f 6e 61 6c 6c 79 20 61 73 73 6f 63  optionally assoc
34673 69 61 74 65 0a 2a 2a 20 74 6f 6b 65 6e 20 70 6f  iate.** token po
34674 73 69 74 69 6f 6e 73 20 61 6e 64 20 6f 66 66 73  sitions and offs
34675 65 74 73 20 77 69 74 68 20 64 6f 63 69 64 73 2e  ets with docids.
34676 0a 2a 2a 0a 2a 2a 20 41 20 44 4c 5f 50 4f 53 49  .**.** A DL_POSI
34677 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 64 6f  TIONS_OFFSETS do
34678 63 6c 69 73 74 20 69 73 20 73 74 6f 72 65 64 20  clist is stored 
34679 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a  like this:.**.**
3467a 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 76 61   array {.**   va
3467b 72 69 6e 74 20 64 6f 63 69 64 3b 0a 2a 2a 20 20  rint docid;.**  
3467c 20 61 72 72 61 79 20 7b 20 20 20 20 20 20 20 20   array {        
3467d 20 20 20 20 20 20 20 20 28 70 6f 73 69 74 69 6f          (positio
3467e 6e 20 6c 69 73 74 20 66 6f 72 20 63 6f 6c 75 6d  n list for colum
3467f 6e 20 30 29 0a 2a 2a 20 20 20 20 20 76 61 72 69  n 0).**     vari
34680 6e 74 20 70 6f 73 69 74 69 6f 6e 3b 20 20 20 20  nt position;    
34681 20 28 64 65 6c 74 61 20 66 72 6f 6d 20 70 72 65   (delta from pre
34682 76 69 6f 75 73 20 70 6f 73 69 74 69 6f 6e 20 70  vious position p
34683 6c 75 73 20 50 4f 53 5f 42 41 53 45 29 0a 2a 2a  lus POS_BASE).**
34684 20 20 20 20 20 76 61 72 69 6e 74 20 73 74 61 72       varint star
34685 74 4f 66 66 73 65 74 3b 20 20 28 64 65 6c 74 61  tOffset;  (delta
34686 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 73   from previous s
34687 74 61 72 74 4f 66 66 73 65 74 29 0a 2a 2a 20 20  tartOffset).**  
34688 20 20 20 76 61 72 69 6e 74 20 65 6e 64 4f 66 66     varint endOff
34689 73 65 74 3b 20 20 20 20 28 64 65 6c 74 61 20 66  set;    (delta f
3468a 72 6f 6d 20 73 74 61 72 74 4f 66 66 73 65 74 29  rom startOffset)
3468b 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 20 20 61 72 72  .**   }.**   arr
3468c 61 79 20 7b 0a 2a 2a 20 20 20 20 20 76 61 72 69  ay {.**     vari
3468d 6e 74 20 50 4f 53 5f 43 4f 4c 55 4d 4e 3b 20 20  nt POS_COLUMN;  
3468e 20 28 6d 61 72 6b 73 20 73 74 61 72 74 20 6f 66   (marks start of
3468f 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 66   position list f
34690 6f 72 20 6e 65 77 20 63 6f 6c 75 6d 6e 29 0a 2a  or new column).*
34691 2a 20 20 20 20 20 76 61 72 69 6e 74 20 63 6f 6c  *     varint col
34692 75 6d 6e 3b 20 20 20 20 20 20 20 28 69 6e 64 65  umn;       (inde
34693 78 20 6f 66 20 6e 65 77 20 63 6f 6c 75 6d 6e 29  x of new column)
34694 0a 2a 2a 20 20 20 20 20 61 72 72 61 79 20 7b 0a  .**     array {.
34695 2a 2a 20 20 20 20 20 20 20 76 61 72 69 6e 74 20  **       varint 
34696 70 6f 73 69 74 69 6f 6e 3b 20 20 20 28 64 65 6c  position;   (del
34697 74 61 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73  ta from previous
34698 20 70 6f 73 69 74 69 6f 6e 20 70 6c 75 73 20 50   position plus P
34699 4f 53 5f 42 41 53 45 29 0a 2a 2a 20 20 20 20 20  OS_BASE).**     
3469a 20 20 76 61 72 69 6e 74 20 73 74 61 72 74 4f 66    varint startOf
3469b 66 73 65 74 3b 28 64 65 6c 74 61 20 66 72 6f 6d  fset;(delta from
3469c 20 70 72 65 76 69 6f 75 73 20 73 74 61 72 74 4f   previous startO
3469d 66 66 73 65 74 29 0a 2a 2a 20 20 20 20 20 20 20  ffset).**       
3469e 76 61 72 69 6e 74 20 65 6e 64 4f 66 66 73 65 74  varint endOffset
3469f 3b 20 20 28 64 65 6c 74 61 20 66 72 6f 6d 20 73  ;  (delta from s
346a0 74 61 72 74 4f 66 66 73 65 74 29 0a 2a 2a 20 20  tartOffset).**  
346a1 20 20 20 7d 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 20     }.**   }.**  
346a2 20 76 61 72 69 6e 74 20 50 4f 53 5f 45 4e 44 3b   varint POS_END;
346a3 20 20 20 20 20 20 20 20 28 6d 61 72 6b 73 20 65          (marks e
346a4 6e 64 20 6f 66 20 70 6f 73 69 74 69 6f 6e 73 20  nd of positions 
346a5 66 6f 72 20 74 68 69 73 20 64 6f 63 75 6d 65 6e  for this documen
346a6 74 2e 0a 2a 2a 20 7d 0a 2a 2a 0a 2a 2a 20 48 65  t..** }.**.** He
346a7 72 65 2c 20 61 72 72 61 79 20 7b 20 58 20 7d 20  re, array { X } 
346a8 6d 65 61 6e 73 20 7a 65 72 6f 20 6f 72 20 6d 6f  means zero or mo
346a9 72 65 20 6f 63 63 75 72 72 65 6e 63 65 73 20 6f  re occurrences o
346aa 66 20 58 2c 20 61 64 6a 61 63 65 6e 74 20 69 6e  f X, adjacent in
346ab 0a 2a 2a 20 6d 65 6d 6f 72 79 2e 20 20 41 20 22  .** memory.  A "
346ac 70 6f 73 69 74 69 6f 6e 22 20 69 73 20 61 6e 20  position" is an 
346ad 69 6e 64 65 78 20 6f 66 20 61 20 74 6f 6b 65 6e  index of a token
346ae 20 69 6e 20 74 68 65 20 74 6f 6b 65 6e 20 73 74   in the token st
346af 72 65 61 6d 0a 2a 2a 20 67 65 6e 65 72 61 74 65  ream.** generate
346b0 64 20 62 79 20 74 68 65 20 74 6f 6b 65 6e 69 7a  d by the tokeniz
346b1 65 72 2c 20 77 68 69 6c 65 20 61 6e 20 22 6f 66  er, while an "of
346b2 66 73 65 74 22 20 69 73 20 61 20 62 79 74 65 20  fset" is a byte 
346b3 6f 66 66 73 65 74 2c 0a 2a 2a 20 62 6f 74 68 20  offset,.** both 
346b4 62 61 73 65 64 20 61 74 20 30 2e 20 20 4e 6f 74  based at 0.  Not
346b5 65 20 74 68 61 74 20 50 4f 53 5f 45 4e 44 20 61  e that POS_END a
346b6 6e 64 20 50 4f 53 5f 43 4f 4c 55 4d 4e 20 6f 63  nd POS_COLUMN oc
346b7 63 75 72 20 69 6e 20 74 68 65 0a 2a 2a 20 73 61  cur in the.** sa
346b8 6d 65 20 6c 6f 67 69 63 61 6c 20 70 6c 61 63 65  me logical place
346b9 20 61 73 20 74 68 65 20 70 6f 73 69 74 69 6f 6e   as the position
346ba 20 65 6c 65 6d 65 6e 74 2c 20 61 6e 64 20 61 63   element, and ac
346bb 74 20 61 73 20 73 65 6e 74 69 6e 61 6c 73 0a 2a  t as sentinals.*
346bc 2a 20 65 6e 64 69 6e 67 20 61 20 70 6f 73 69 74  * ending a posit
346bd 69 6f 6e 20 6c 69 73 74 20 61 72 72 61 79 2e 0a  ion list array..
346be 2a 2a 0a 2a 2a 20 41 20 44 4c 5f 50 4f 53 49 54  **.** A DL_POSIT
346bf 49 4f 4e 53 20 64 6f 63 6c 69 73 74 20 6f 6d 69  IONS doclist omi
346c0 74 73 20 74 68 65 20 73 74 61 72 74 4f 66 66 73  ts the startOffs
346c1 65 74 20 61 6e 64 20 65 6e 64 4f 66 66 73 65 74  et and endOffset
346c2 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  .** information.
346c3 20 20 41 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f    A DL_DOCIDS do
346c4 63 6c 69 73 74 20 6f 6d 69 74 73 20 62 6f 74 68  clist omits both
346c5 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 61 6e   the position an
346c6 64 0a 2a 2a 20 6f 66 66 73 65 74 20 69 6e 66 6f  d.** offset info
346c7 72 6d 61 74 69 6f 6e 2c 20 62 65 63 6f 6d 69 6e  rmation, becomin
346c8 67 20 61 6e 20 61 72 72 61 79 20 6f 66 20 76 61  g an array of va
346c9 72 69 6e 74 2d 65 6e 63 6f 64 65 64 20 64 6f 63  rint-encoded doc
346ca 69 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 2d 64 69  ids..**.** On-di
346cb 73 6b 20 64 61 74 61 20 69 73 20 73 74 6f 72 65  sk data is store
346cc 64 20 61 73 20 74 79 70 65 20 44 4c 5f 44 45 46  d as type DL_DEF
346cd 41 55 4c 54 2c 20 73 6f 20 77 65 20 64 6f 6e 27  AULT, so we don'
346ce 74 20 73 65 72 69 61 6c 69 7a 65 0a 2a 2a 20 74  t serialize.** t
346cf 68 65 20 74 79 70 65 2e 20 20 44 75 65 20 74 6f  he type.  Due to
346d0 20 68 6f 77 20 64 65 6c 65 74 69 6f 6e 20 69 73   how deletion is
346d1 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20   implemented in 
346d2 74 68 65 20 73 65 67 6d 65 6e 74 61 74 69 6f 6e  the segmentation
346d3 0a 2a 2a 20 73 79 73 74 65 6d 2c 20 6f 6e 2d 64  .** system, on-d
346d4 69 73 6b 20 64 6f 63 6c 69 73 74 73 20 4d 55 53  isk doclists MUS
346d5 54 20 73 74 6f 72 65 20 61 74 20 6c 65 61 73 74  T store at least
346d6 20 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a   positions..**.*
346d7 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 6c  *.**** Segment l
346d8 65 61 66 20 6e 6f 64 65 73 20 2a 2a 2a 2a 0a 2a  eaf nodes ****.*
346d9 2a 20 53 65 67 6d 65 6e 74 20 6c 65 61 66 20 6e  * Segment leaf n
346da 6f 64 65 73 20 73 74 6f 72 65 20 74 65 72 6d 73  odes store terms
346db 20 61 6e 64 20 64 6f 63 6c 69 73 74 73 2c 20 6f   and doclists, o
346dc 72 64 65 72 65 64 20 62 79 20 74 65 72 6d 2e 20  rdered by term. 
346dd 20 4c 65 61 66 0a 2a 2a 20 6e 6f 64 65 73 20 61   Leaf.** nodes a
346de 72 65 20 77 72 69 74 74 65 6e 20 75 73 69 6e 67  re written using
346df 20 4c 65 61 66 57 72 69 74 65 72 2c 20 61 6e 64   LeafWriter, and
346e0 20 72 65 61 64 20 75 73 69 6e 67 20 4c 65 61 66   read using Leaf
346e1 52 65 61 64 65 72 20 28 74 6f 0a 2a 2a 20 69 74  Reader (to.** it
346e2 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 20  erate through a 
346e3 73 69 6e 67 6c 65 20 6c 65 61 66 20 6e 6f 64 65  single leaf node
346e4 27 73 20 64 61 74 61 29 20 61 6e 64 20 4c 65 61  's data) and Lea
346e5 76 65 73 52 65 61 64 65 72 20 28 74 6f 0a 2a 2a  vesReader (to.**
346e6 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
346e7 20 61 20 73 65 67 6d 65 6e 74 27 73 20 65 6e 74   a segment's ent
346e8 69 72 65 20 6c 65 61 66 20 6c 61 79 65 72 29 2e  ire leaf layer).
346e9 20 20 4c 65 61 66 20 6e 6f 64 65 73 20 68 61 76    Leaf nodes hav
346ea 65 0a 2a 2a 20 74 68 65 20 66 6f 72 6d 61 74 3a  e.** the format:
346eb 0a 2a 2a 0a 2a 2a 20 76 61 72 69 6e 74 20 69 48  .**.** varint iH
346ec 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20  eight;          
346ed 20 20 20 28 68 65 69 67 68 74 20 66 72 6f 6d 20     (height from 
346ee 6c 65 61 66 20 6c 65 76 65 6c 2c 20 61 6c 77 61  leaf level, alwa
346ef 79 73 20 30 29 0a 2a 2a 20 76 61 72 69 6e 74 20  ys 0).** varint 
346f0 6e 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20  nTerm;          
346f1 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20       (length of 
346f2 66 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a 20 63  first term).** c
346f3 68 61 72 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d  har pTerm[nTerm]
346f4 3b 20 20 20 20 20 20 20 20 20 20 28 63 6f 6e 74  ;          (cont
346f5 65 6e 74 20 6f 66 20 66 69 72 73 74 20 74 65 72  ent of first ter
346f6 6d 29 0a 2a 2a 20 76 61 72 69 6e 74 20 6e 44 6f  m).** varint nDo
346f7 63 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20  clist;          
346f8 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 74 65 72    (length of ter
346f9 6d 27 73 20 61 73 73 6f 63 69 61 74 65 64 20 64  m's associated d
346fa 6f 63 6c 69 73 74 29 0a 2a 2a 20 63 68 61 72 20  oclist).** char 
346fb 70 44 6f 63 6c 69 73 74 5b 6e 44 6f 63 6c 69 73  pDoclist[nDoclis
346fc 74 5d 3b 20 20 20 20 28 63 6f 6e 74 65 6e 74 20  t];    (content 
346fd 6f 66 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 61  of doclist).** a
346fe 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20 20 20 20  rray {.**       
346ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34700 20 20 20 20 20 20 28 66 75 72 74 68 65 72 20 74        (further t
34701 65 72 6d 73 20 61 72 65 20 64 65 6c 74 61 2d 65  erms are delta-e
34702 6e 63 6f 64 65 64 29 0a 2a 2a 20 20 20 76 61 72  ncoded).**   var
34703 69 6e 74 20 6e 50 72 65 66 69 78 3b 20 20 20 20  int nPrefix;    
34704 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f         (length o
34705 66 20 70 72 65 66 69 78 20 73 68 61 72 65 64 20  f prefix shared 
34706 77 69 74 68 20 70 72 65 76 69 6f 75 73 20 74 65  with previous te
34707 72 6d 29 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20  rm).**   varint 
34708 6e 53 75 66 66 69 78 3b 20 20 20 20 20 20 20 20  nSuffix;        
34709 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 75 6e     (length of un
3470a 73 68 61 72 65 64 20 73 75 66 66 69 78 29 0a 2a  shared suffix).*
3470b 2a 20 20 20 63 68 61 72 20 70 54 65 72 6d 53 75  *   char pTermSu
3470c 66 66 69 78 5b 6e 53 75 66 66 69 78 5d 3b 28 75  ffix[nSuffix];(u
3470d 6e 73 68 61 72 65 64 20 73 75 66 66 69 78 20 6f  nshared suffix o
3470e 66 20 6e 65 78 74 20 74 65 72 6d 29 0a 2a 2a 20  f next term).** 
3470f 20 20 76 61 72 69 6e 74 20 6e 44 6f 63 6c 69 73    varint nDoclis
34710 74 3b 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e  t;          (len
34711 67 74 68 20 6f 66 20 74 65 72 6d 27 73 20 61 73  gth of term's as
34712 73 6f 63 69 61 74 65 64 20 64 6f 63 6c 69 73 74  sociated doclist
34713 29 0a 2a 2a 20 20 20 63 68 61 72 20 70 44 6f 63  ).**   char pDoc
34714 6c 69 73 74 5b 6e 44 6f 63 6c 69 73 74 5d 3b 20  list[nDoclist]; 
34715 20 28 63 6f 6e 74 65 6e 74 20 6f 66 20 64 6f 63   (content of doc
34716 6c 69 73 74 29 0a 2a 2a 20 7d 0a 2a 2a 0a 2a 2a  list).** }.**.**
34717 20 48 65 72 65 2c 20 61 72 72 61 79 20 7b 20 58   Here, array { X
34718 20 7d 20 6d 65 61 6e 73 20 7a 65 72 6f 20 6f 72   } means zero or
34719 20 6d 6f 72 65 20 6f 63 63 75 72 72 65 6e 63 65   more occurrence
3471a 73 20 6f 66 20 58 2c 20 61 64 6a 61 63 65 6e 74  s of X, adjacent
3471b 20 69 6e 0a 2a 2a 20 6d 65 6d 6f 72 79 2e 0a 2a   in.** memory..*
3471c 2a 0a 2a 2a 20 4c 65 61 66 20 6e 6f 64 65 73 20  *.** Leaf nodes 
3471d 61 72 65 20 62 72 6f 6b 65 6e 20 69 6e 74 6f 20  are broken into 
3471e 62 6c 6f 63 6b 73 20 77 68 69 63 68 20 61 72 65  blocks which are
3471f 20 73 74 6f 72 65 64 20 63 6f 6e 74 69 67 75 6f   stored contiguo
34720 75 73 6c 79 20 69 6e 0a 2a 2a 20 74 68 65 20 25  usly in.** the %
34721 5f 73 65 67 6d 65 6e 74 73 20 74 61 62 6c 65 20  _segments table 
34722 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e  in sorted order.
34723 20 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61    This means tha
34724 74 20 77 68 65 6e 20 74 68 65 20 65 6e 64 0a 2a  t when the end.*
34725 2a 20 6f 66 20 61 20 6e 6f 64 65 20 69 73 20 72  * of a node is r
34726 65 61 63 68 65 64 2c 20 74 68 65 20 6e 65 78 74  eached, the next
34727 20 74 65 72 6d 20 69 73 20 69 6e 20 74 68 65 20   term is in the 
34728 6e 6f 64 65 20 77 69 74 68 20 74 68 65 20 6e 65  node with the ne
34729 78 74 0a 2a 2a 20 67 72 65 61 74 65 72 20 6e 6f  xt.** greater no
3472a 64 65 20 69 64 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77  de id..**.** New
3472b 20 64 61 74 61 20 69 73 20 73 70 69 6c 6c 65 64   data is spilled
3472c 20 74 6f 20 61 20 6e 65 77 20 6c 65 61 66 20 6e   to a new leaf n
3472d 6f 64 65 20 77 68 65 6e 20 74 68 65 20 63 75 72  ode when the cur
3472e 72 65 6e 74 20 6e 6f 64 65 0a 2a 2a 20 65 78 63  rent node.** exc
3472f 65 65 64 73 20 4c 45 41 46 5f 4d 41 58 20 62 79  eeds LEAF_MAX by
34730 74 65 73 20 28 64 65 66 61 75 6c 74 20 32 30 34  tes (default 204
34731 38 29 2e 20 20 4e 65 77 20 64 61 74 61 20 77 68  8).  New data wh
34732 69 63 68 20 69 74 73 65 6c 66 20 69 73 0a 2a 2a  ich itself is.**
34733 20 6c 61 72 67 65 72 20 74 68 61 6e 20 53 54 41   larger than STA
34734 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 28 64 65 66  NDALONE_MIN (def
34735 61 75 6c 74 20 31 30 32 34 29 20 69 73 20 70 6c  ault 1024) is pl
34736 61 63 65 64 20 69 6e 20 61 20 73 74 61 6e 64 61  aced in a standa
34737 6c 6f 6e 65 0a 2a 2a 20 6e 6f 64 65 20 28 61 20  lone.** node (a 
34738 6c 65 61 66 20 6e 6f 64 65 20 77 69 74 68 20 61  leaf node with a
34739 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 61 6e 64   single term and
3473a 20 64 6f 63 6c 69 73 74 29 2e 20 20 54 68 65 20   doclist).  The 
3473b 67 6f 61 6c 20 6f 66 0a 2a 2a 20 74 68 65 73 65  goal of.** these
3473c 20 73 65 74 74 69 6e 67 73 20 69 73 20 74 6f 20   settings is to 
3473d 70 61 63 6b 20 74 6f 67 65 74 68 65 72 20 67 72  pack together gr
3473e 6f 75 70 73 20 6f 66 20 73 6d 61 6c 6c 20 64 6f  oups of small do
3473f 63 6c 69 73 74 73 20 77 68 69 6c 65 0a 2a 2a 20  clists while.** 
34740 6d 61 6b 69 6e 67 20 69 74 20 65 66 66 69 63 69  making it effici
34741 65 6e 74 20 74 6f 20 64 69 72 65 63 74 6c 79 20  ent to directly 
34742 61 63 63 65 73 73 20 6c 61 72 67 65 20 64 6f 63  access large doc
34743 6c 69 73 74 73 2e 20 20 54 68 65 0a 2a 2a 20 61  lists.  The.** a
34744 73 73 75 6d 70 74 69 6f 6e 20 69 73 20 74 68 61  ssumption is tha
34745 74 20 6c 61 72 67 65 20 64 6f 63 6c 69 73 74 73  t large doclists
34746 20 72 65 70 72 65 73 65 6e 74 20 74 65 72 6d 73   represent terms
34747 20 77 68 69 63 68 20 61 72 65 20 6d 6f 72 65 0a   which are more.
34748 2a 2a 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20  ** likely to be 
34749 71 75 65 72 79 20 74 61 72 67 65 74 73 2e 0a 2a  query targets..*
3474a 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  *.** TODO(shess)
3474b 20 49 74 20 6d 61 79 20 62 65 20 75 73 65 66 75   It may be usefu
3474c 6c 20 66 6f 72 20 62 6c 6f 63 6b 69 6e 67 20 64  l for blocking d
3474d 65 63 69 73 69 6f 6e 73 20 74 6f 20 62 65 20 6d  ecisions to be m
3474e 6f 72 65 0a 2a 2a 20 64 79 6e 61 6d 69 63 2e 20  ore.** dynamic. 
3474f 20 46 6f 72 20 69 6e 73 74 61 6e 63 65 2c 20 69   For instance, i
34750 74 20 6d 61 79 20 6d 61 6b 65 20 6d 6f 72 65 20  t may make more 
34751 73 65 6e 73 65 20 74 6f 20 68 61 76 65 20 61 20  sense to have a 
34752 32 2e 35 6b 20 6c 65 61 66 0a 2a 2a 20 6e 6f 64  2.5k leaf.** nod
34753 65 20 72 61 74 68 65 72 20 74 68 61 6e 20 73 70  e rather than sp
34754 6c 69 74 74 69 6e 67 20 69 6e 74 6f 20 32 6b 20  litting into 2k 
34755 61 6e 64 20 2e 35 6b 20 6e 6f 64 65 73 2e 20 20  and .5k nodes.  
34756 4d 79 20 69 6e 74 75 69 74 69 6f 6e 20 69 73 0a  My intuition is.
34757 2a 2a 20 74 68 61 74 20 74 68 69 73 20 6d 69 67  ** that this mig
34758 68 74 20 65 78 74 65 6e 64 20 74 68 72 6f 75 67  ht extend throug
34759 68 20 32 78 20 6f 72 20 34 78 20 74 68 65 20 70  h 2x or 4x the p
3475a 61 67 65 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 0a 2a  agesize..**.**.*
3475b 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 69 6e 74 65  *** Segment inte
3475c 72 69 6f 72 20 6e 6f 64 65 73 20 2a 2a 2a 2a 0a  rior nodes ****.
3475d 2a 2a 20 53 65 67 6d 65 6e 74 20 69 6e 74 65 72  ** Segment inter
3475e 69 6f 72 20 6e 6f 64 65 73 20 73 74 6f 72 65 20  ior nodes store 
3475f 62 6c 6f 63 6b 69 64 73 20 66 6f 72 20 73 75 62  blockids for sub
34760 74 72 65 65 20 6e 6f 64 65 73 20 61 6e 64 20 74  tree nodes and t
34761 65 72 6d 73 0a 2a 2a 20 74 6f 20 64 65 73 63 72  erms.** to descr
34762 69 62 65 20 77 68 61 74 20 64 61 74 61 20 69 73  ibe what data is
34763 20 73 74 6f 72 65 64 20 62 79 20 74 68 65 20 65   stored by the e
34764 61 63 68 20 73 75 62 74 72 65 65 2e 20 20 49 6e  ach subtree.  In
34765 74 65 72 69 6f 72 0a 2a 2a 20 6e 6f 64 65 73 20  terior.** nodes 
34766 61 72 65 20 77 72 69 74 74 65 6e 20 75 73 69 6e  are written usin
34767 67 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72  g InteriorWriter
34768 2c 20 61 6e 64 20 72 65 61 64 20 75 73 69 6e 67  , and read using
34769 0a 2a 2a 20 49 6e 74 65 72 69 6f 72 52 65 61 64  .** InteriorRead
3476a 65 72 2e 20 20 49 6e 74 65 72 69 6f 72 57 72 69  er.  InteriorWri
3476b 74 65 72 73 20 61 72 65 20 63 72 65 61 74 65 64  ters are created
3476c 20 61 73 20 6e 65 65 64 65 64 20 77 68 65 6e 0a   as needed when.
3476d 2a 2a 20 53 65 67 6d 65 6e 74 57 72 69 74 65 72  ** SegmentWriter
3476e 20 63 72 65 61 74 65 73 20 6e 65 77 20 6c 65 61   creates new lea
3476f 66 20 6e 6f 64 65 73 2c 20 6f 72 20 77 68 65 6e  f nodes, or when
34770 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64   an interior nod
34771 65 0a 2a 2a 20 69 74 73 65 6c 66 20 67 72 6f 77  e.** itself grow
34772 73 20 74 6f 6f 20 62 69 67 20 61 6e 64 20 6d 75  s too big and mu
34773 73 74 20 62 65 20 73 70 6c 69 74 2e 20 20 54 68  st be split.  Th
34774 65 20 66 6f 72 6d 61 74 20 6f 66 20 69 6e 74 65  e format of inte
34775 72 69 6f 72 0a 2a 2a 20 6e 6f 64 65 73 3a 0a 2a  rior.** nodes:.*
34776 2a 0a 2a 2a 20 76 61 72 69 6e 74 20 69 48 65 69  *.** varint iHei
34777 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 28  ght;           (
34778 68 65 69 67 68 74 20 66 72 6f 6d 20 6c 65 61 66  height from leaf
34779 20 6c 65 76 65 6c 2c 20 61 6c 77 61 79 73 20 3e   level, always >
3477a 30 29 0a 2a 2a 20 76 61 72 69 6e 74 20 69 42 6c  0).** varint iBl
3477b 6f 63 6b 69 64 3b 20 20 20 20 20 20 20 20 20 20  ockid;          
3477c 28 62 6c 6f 63 6b 20 69 64 20 6f 66 20 6e 6f 64  (block id of nod
3477d 65 27 73 20 6c 65 66 74 6d 6f 73 74 20 73 75 62  e's leftmost sub
3477e 74 72 65 65 29 0a 2a 2a 20 6f 70 74 69 6f 6e 61  tree).** optiona
3477f 6c 20 7b 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20  l {.**   varint 
34780 6e 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20  nTerm;          
34781 20 28 6c 65 6e 67 74 68 20 6f 66 20 66 69 72 73   (length of firs
34782 74 20 74 65 72 6d 29 0a 2a 2a 20 20 20 63 68 61  t term).**   cha
34783 72 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 3b 20  r pTerm[nTerm]; 
34784 20 20 20 20 20 28 63 6f 6e 74 65 6e 74 20 6f 66       (content of
34785 20 66 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a 20   first term).** 
34786 20 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20    array {.**    
34787 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34788 20 20 20 20 20 20 20 20 20 20 20 20 28 66 75 72              (fur
34789 74 68 65 72 20 74 65 72 6d 73 20 61 72 65 20 64  ther terms are d
3478a 65 6c 74 61 2d 65 6e 63 6f 64 65 64 29 0a 2a 2a  elta-encoded).**
3478b 20 20 20 20 20 76 61 72 69 6e 74 20 6e 50 72 65       varint nPre
3478c 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  fix;            
3478d 28 6c 65 6e 67 74 68 20 6f 66 20 73 68 61 72 65  (length of share
3478e 64 20 70 72 65 66 69 78 20 77 69 74 68 20 70 72  d prefix with pr
3478f 65 76 69 6f 75 73 20 74 65 72 6d 29 0a 2a 2a 20  evious term).** 
34790 20 20 20 20 76 61 72 69 6e 74 20 6e 53 75 66 66      varint nSuff
34791 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 28  ix;            (
34792 6c 65 6e 67 74 68 20 6f 66 20 75 6e 73 68 61 72  length of unshar
34793 65 64 20 73 75 66 66 69 78 29 0a 2a 2a 20 20 20  ed suffix).**   
34794 20 20 63 68 61 72 20 70 54 65 72 6d 53 75 66 66    char pTermSuff
34795 69 78 5b 6e 53 75 66 66 69 78 5d 3b 20 28 75 6e  ix[nSuffix]; (un
34796 73 68 61 72 65 64 20 73 75 66 66 69 78 20 6f 66  shared suffix of
34797 20 6e 65 78 74 20 74 65 72 6d 29 0a 2a 2a 20 20   next term).**  
34798 20 7d 0a 2a 2a 20 7d 0a 2a 2a 0a 2a 2a 20 48 65   }.** }.**.** He
34799 72 65 2c 20 6f 70 74 69 6f 6e 61 6c 20 7b 20 58  re, optional { X
3479a 20 7d 20 6d 65 61 6e 73 20 61 6e 20 6f 70 74 69   } means an opti
3479b 6f 6e 61 6c 20 65 6c 65 6d 65 6e 74 2c 20 77 68  onal element, wh
3479c 69 6c 65 20 61 72 72 61 79 20 7b 20 58 20 7d 0a  ile array { X }.
3479d 2a 2a 20 6d 65 61 6e 73 20 7a 65 72 6f 20 6f 72  ** means zero or
3479e 20 6d 6f 72 65 20 6f 63 63 75 72 72 65 6e 63 65   more occurrence
3479f 73 20 6f 66 20 58 2c 20 61 64 6a 61 63 65 6e 74  s of X, adjacent
347a0 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a   in memory..**.*
347a1 2a 20 41 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f  * An interior no
347a2 64 65 20 65 6e 63 6f 64 65 73 20 6e 20 74 65 72  de encodes n ter
347a3 6d 73 20 73 65 70 61 72 61 74 69 6e 67 20 6e 2b  ms separating n+
347a4 31 20 73 75 62 74 72 65 65 73 2e 20 20 54 68 65  1 subtrees.  The
347a5 0a 2a 2a 20 73 75 62 74 72 65 65 20 62 6c 6f 63  .** subtree bloc
347a6 6b 73 20 61 72 65 20 63 6f 6e 74 69 67 75 6f 75  ks are contiguou
347a7 73 2c 20 73 6f 20 6f 6e 6c 79 20 74 68 65 20 66  s, so only the f
347a8 69 72 73 74 20 73 75 62 74 72 65 65 27 73 20 62  irst subtree's b
347a9 6c 6f 63 6b 69 64 0a 2a 2a 20 69 73 20 65 6e 63  lockid.** is enc
347aa 6f 64 65 64 2e 20 20 54 68 65 20 73 75 62 74 72  oded.  The subtr
347ab 65 65 20 61 74 20 69 42 6c 6f 63 6b 69 64 20 77  ee at iBlockid w
347ac 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 6c 6c 20  ill contain all 
347ad 74 65 72 6d 73 20 6c 65 73 73 0a 2a 2a 20 74 68  terms less.** th
347ae 61 6e 20 74 68 65 20 66 69 72 73 74 20 74 65 72  an the first ter
347af 6d 20 65 6e 63 6f 64 65 64 20 28 6f 72 20 61 6c  m encoded (or al
347b0 6c 20 74 65 72 6d 73 20 69 66 20 6e 6f 20 74 65  l terms if no te
347b1 72 6d 20 69 73 20 65 6e 63 6f 64 65 64 29 2e 0a  rm is encoded)..
347b2 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66 6f  ** Otherwise, fo
347b3 72 20 74 65 72 6d 73 20 67 72 65 61 74 65 72 20  r terms greater 
347b4 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
347b5 20 70 54 65 72 6d 5b 69 5d 20 62 75 74 20 6c 65   pTerm[i] but le
347b6 73 73 0a 2a 2a 20 74 68 61 6e 20 70 54 65 72 6d  ss.** than pTerm
347b7 5b 69 2b 31 5d 2c 20 74 68 65 20 73 75 62 74 72  [i+1], the subtr
347b8 65 65 20 66 6f 72 20 74 68 61 74 20 74 65 72 6d  ee for that term
347b9 20 77 69 6c 6c 20 62 65 20 72 6f 6f 74 65 64 20   will be rooted 
347ba 61 74 0a 2a 2a 20 69 42 6c 6f 63 6b 69 64 2b 69  at.** iBlockid+i
347bb 2e 20 20 49 6e 74 65 72 69 6f 72 20 6e 6f 64 65  .  Interior node
347bc 73 20 6f 6e 6c 79 20 73 74 6f 72 65 20 65 6e 6f  s only store eno
347bd 75 67 68 20 74 65 72 6d 20 64 61 74 61 20 74 6f  ugh term data to
347be 0a 2a 2a 20 64 69 73 74 69 6e 67 75 69 73 68 20  .** distinguish 
347bf 61 64 6a 61 63 65 6e 74 20 63 68 69 6c 64 72 65  adjacent childre
347c0 6e 20 28 69 66 20 74 68 65 20 72 69 67 68 74 6d  n (if the rightm
347c1 6f 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ost term of the 
347c2 6c 65 66 74 0a 2a 2a 20 63 68 69 6c 64 20 69 73  left.** child is
347c3 20 22 73 6f 6d 65 74 68 69 6e 67 22 2c 20 61 6e   "something", an
347c4 64 20 74 68 65 20 6c 65 66 74 6d 6f 73 74 20 74  d the leftmost t
347c5 65 72 6d 20 6f 66 20 74 68 65 20 72 69 67 68 74  erm of the right
347c6 20 63 68 69 6c 64 20 69 73 0a 2a 2a 20 22 77 69   child is.** "wi
347c7 63 6b 65 64 22 2c 20 6f 6e 6c 79 20 22 77 22 20  cked", only "w" 
347c8 69 73 20 73 74 6f 72 65 64 29 2e 0a 2a 2a 0a 2a  is stored)..**.*
347c9 2a 20 4e 65 77 20 64 61 74 61 20 69 73 20 73 70  * New data is sp
347ca 69 6c 6c 65 64 20 74 6f 20 61 20 6e 65 77 20 69  illed to a new i
347cb 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 61 74 20  nterior node at 
347cc 74 68 65 20 73 61 6d 65 20 68 65 69 67 68 74 20  the same height 
347cd 77 68 65 6e 0a 2a 2a 20 74 68 65 20 63 75 72 72  when.** the curr
347ce 65 6e 74 20 6e 6f 64 65 20 65 78 63 65 65 64 73  ent node exceeds
347cf 20 49 4e 54 45 52 49 4f 52 5f 4d 41 58 20 62 79   INTERIOR_MAX by
347d0 74 65 73 20 28 64 65 66 61 75 6c 74 20 32 30 34  tes (default 204
347d1 38 29 2e 0a 2a 2a 20 49 4e 54 45 52 49 4f 52 5f  8)..** INTERIOR_
347d2 4d 49 4e 5f 54 45 52 4d 53 20 28 64 65 66 61 75  MIN_TERMS (defau
347d3 6c 74 20 37 29 20 6b 65 65 70 73 20 6c 61 72 67  lt 7) keeps larg
347d4 65 20 74 65 72 6d 73 20 66 72 6f 6d 20 6d 6f 6e  e terms from mon
347d5 6f 70 6f 6c 69 7a 69 6e 67 0a 2a 2a 20 69 6e 74  opolizing.** int
347d6 65 72 69 6f 72 20 6e 6f 64 65 73 20 61 6e 64 20  erior nodes and 
347d7 6d 61 6b 69 6e 67 20 74 68 65 20 74 72 65 65 20  making the tree 
347d8 74 6f 6f 20 73 6b 69 6e 6e 79 2e 20 20 54 68 65  too skinny.  The
347d9 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 0a   interior nodes.
347da 2a 2a 20 61 74 20 61 20 67 69 76 65 6e 20 68 65  ** at a given he
347db 69 67 68 74 20 61 72 65 20 6e 61 74 75 72 61 6c  ight are natural
347dc 6c 79 20 74 72 61 63 6b 65 64 20 62 79 20 69 6e  ly tracked by in
347dd 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 61 74 0a  terior nodes at.
347de 2a 2a 20 68 65 69 67 68 74 2b 31 2c 20 61 6e 64  ** height+1, and
347df 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a   so on..**.**.**
347e0 2a 2a 20 53 65 67 6d 65 6e 74 20 64 69 72 65 63  ** Segment direc
347e1 74 6f 72 79 20 2a 2a 2a 2a 0a 2a 2a 20 54 68 65  tory ****.** The
347e2 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74 6f   segment directo
347e3 72 79 20 69 6e 20 74 61 62 6c 65 20 25 5f 73 65  ry in table %_se
347e4 67 64 69 72 20 73 74 6f 72 65 73 20 6d 65 74 61  gdir stores meta
347e5 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72  -information for
347e6 0a 2a 2a 20 6d 65 72 67 69 6e 67 20 61 6e 64 20  .** merging and 
347e7 64 65 6c 65 74 69 6e 67 20 73 65 67 6d 65 6e 74  deleting segment
347e8 73 2c 20 61 6e 64 20 61 6c 73 6f 20 74 68 65 20  s, and also the 
347e9 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 74 68 65  root node of the
347ea 0a 2a 2a 20 73 65 67 6d 65 6e 74 27 73 20 74 72  .** segment's tr
347eb 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 6f  ee..**.** The ro
347ec 6f 74 20 6e 6f 64 65 20 69 73 20 74 68 65 20 74  ot node is the t
347ed 6f 70 20 6e 6f 64 65 20 6f 66 20 74 68 65 20 73  op node of the s
347ee 65 67 6d 65 6e 74 27 73 20 74 72 65 65 20 61 66  egment's tree af
347ef 74 65 72 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20  ter encoding.** 
347f0 74 68 65 20 65 6e 74 69 72 65 20 73 65 67 6d 65  the entire segme
347f1 6e 74 2c 20 72 65 73 74 72 69 63 74 65 64 20 74  nt, restricted t
347f2 6f 20 52 4f 4f 54 5f 4d 41 58 20 62 79 74 65 73  o ROOT_MAX bytes
347f3 20 28 64 65 66 61 75 6c 74 20 31 30 32 34 29 2e   (default 1024).
347f4 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6c 64 20 62  .** This could b
347f5 65 20 65 69 74 68 65 72 20 61 20 6c 65 61 66 20  e either a leaf 
347f6 6e 6f 64 65 20 6f 72 20 61 6e 20 69 6e 74 65 72  node or an inter
347f7 69 6f 72 20 6e 6f 64 65 2e 20 20 49 66 20 74 68  ior node.  If th
347f8 65 20 74 6f 70 0a 2a 2a 20 6e 6f 64 65 20 72 65  e top.** node re
347f9 71 75 69 72 65 73 20 6d 6f 72 65 20 74 68 61 6e  quires more than
347fa 20 52 4f 4f 54 5f 4d 41 58 20 62 79 74 65 73 2c   ROOT_MAX bytes,
347fb 20 69 74 20 69 73 20 66 6c 75 73 68 65 64 20 74   it is flushed t
347fc 6f 20 25 5f 73 65 67 6d 65 6e 74 73 0a 2a 2a 20  o %_segments.** 
347fd 61 6e 64 20 61 20 6e 65 77 20 72 6f 6f 74 20 69  and a new root i
347fe 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 69 73 20  nterior node is 
347ff 67 65 6e 65 72 61 74 65 64 20 28 77 68 69 63 68  generated (which
34800 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 66   should always f
34801 69 74 0a 2a 2a 20 77 69 74 68 69 6e 20 52 4f 4f  it.** within ROO
34802 54 5f 4d 41 58 20 62 65 63 61 75 73 65 20 69 74  T_MAX because it
34803 20 6f 6e 6c 79 20 6e 65 65 64 73 20 73 70 61 63   only needs spac
34804 65 20 66 6f 72 20 32 20 76 61 72 69 6e 74 73 2c  e for 2 varints,
34805 20 74 68 65 0a 2a 2a 20 68 65 69 67 68 74 20 61   the.** height a
34806 6e 64 20 74 68 65 20 62 6c 6f 63 6b 69 64 20 6f  nd the blockid o
34807 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 72  f the previous r
34808 6f 6f 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  oot)..**.** The 
34809 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e  meta-information
3480a 20 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74 20   in the segment 
3480b 64 69 72 65 63 74 6f 72 79 20 69 73 3a 0a 2a 2a  directory is:.**
3480c 20 20 20 6c 65 76 65 6c 20 20 20 20 20 20 20 20     level        
3480d 20 20 20 20 20 20 20 2d 20 73 65 67 6d 65 6e 74         - segment
3480e 20 6c 65 76 65 6c 20 28 73 65 65 20 62 65 6c 6f   level (see belo
3480f 77 29 0a 2a 2a 20 20 20 69 64 78 20 20 20 20 20  w).**   idx     
34810 20 20 20 20 20 20 20 20 20 20 20 20 2d 20 69 6e              - in
34811 64 65 78 20 77 69 74 68 69 6e 20 6c 65 76 65 6c  dex within level
34812 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
34813 20 20 20 20 20 20 20 20 20 20 2d 20 28 6c 65 76            - (lev
34814 65 6c 2c 69 64 78 20 75 6e 69 71 75 65 6c 79 20  el,idx uniquely 
34815 69 64 65 6e 74 69 66 79 20 61 20 73 65 67 6d 65  identify a segme
34816 6e 74 29 0a 2a 2a 20 20 20 73 74 61 72 74 5f 62  nt).**   start_b
34817 6c 6f 63 6b 20 20 20 20 20 20 20 20 20 2d 20 66  lock         - f
34818 69 72 73 74 20 6c 65 61 66 20 6e 6f 64 65 0a 2a  irst leaf node.*
34819 2a 20 20 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62  *   leaves_end_b
3481a 6c 6f 63 6b 20 20 20 20 2d 20 6c 61 73 74 20 6c  lock    - last l
3481b 65 61 66 20 6e 6f 64 65 0a 2a 2a 20 20 20 65 6e  eaf node.**   en
3481c 64 5f 62 6c 6f 63 6b 20 20 20 20 20 20 20 20 20  d_block         
3481d 20 20 2d 20 6c 61 73 74 20 62 6c 6f 63 6b 20 28    - last block (
3481e 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74 65 72 69  including interi
3481f 6f 72 20 6e 6f 64 65 73 29 0a 2a 2a 20 20 20 72  or nodes).**   r
34820 6f 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20  oot             
34821 20 20 20 2d 20 63 6f 6e 74 65 6e 74 73 20 6f 66     - contents of
34822 20 72 6f 6f 74 20 6e 6f 64 65 0a 2a 2a 0a 2a 2a   root node.**.**
34823 20 49 66 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64   If the root nod
34824 65 20 69 73 20 61 20 6c 65 61 66 20 6e 6f 64 65  e is a leaf node
34825 2c 20 74 68 65 6e 20 73 74 61 72 74 5f 62 6c 6f  , then start_blo
34826 63 6b 2c 0a 2a 2a 20 6c 65 61 76 65 73 5f 65 6e  ck,.** leaves_en
34827 64 5f 62 6c 6f 63 6b 2c 20 61 6e 64 20 65 6e 64  d_block, and end
34828 5f 62 6c 6f 63 6b 20 61 72 65 20 61 6c 6c 20 30  _block are all 0
34829 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67  ..**.**.**** Seg
3482a 6d 65 6e 74 20 6d 65 72 67 69 6e 67 20 2a 2a 2a  ment merging ***
3482b 2a 0a 2a 2a 20 54 6f 20 61 6d 6f 72 74 69 7a 65  *.** To amortize
3482c 20 75 70 64 61 74 65 20 63 6f 73 74 73 2c 20 73   update costs, s
3482d 65 67 6d 65 6e 74 73 20 61 72 65 20 67 72 6f 75  egments are grou
3482e 70 65 64 20 69 6e 74 6f 20 6c 65 76 65 6c 73 20  ped into levels 
3482f 61 6e 64 0a 2a 2a 20 6d 65 72 67 65 64 20 69 6e  and.** merged in
34830 20 62 61 74 63 68 65 73 2e 20 20 45 61 63 68 20   batches.  Each 
34831 69 6e 63 72 65 61 73 65 20 69 6e 20 6c 65 76 65  increase in leve
34832 6c 20 72 65 70 72 65 73 65 6e 74 73 20 65 78 70  l represents exp
34833 6f 6e 65 6e 74 69 61 6c 6c 79 0a 2a 2a 20 6d 6f  onentially.** mo
34834 72 65 20 64 6f 63 75 6d 65 6e 74 73 2e 0a 2a 2a  re documents..**
34835 0a 2a 2a 20 4e 65 77 20 64 6f 63 75 6d 65 6e 74  .** New document
34836 73 20 28 61 63 74 75 61 6c 6c 79 2c 20 64 6f 63  s (actually, doc
34837 75 6d 65 6e 74 20 75 70 64 61 74 65 73 29 20 61  ument updates) a
34838 72 65 20 74 6f 6b 65 6e 69 7a 65 64 20 61 6e 64  re tokenized and
34839 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69 6e 64 69  .** written indi
3483a 76 69 64 75 61 6c 6c 79 20 28 75 73 69 6e 67 20  vidually (using 
3483b 4c 65 61 66 57 72 69 74 65 72 29 20 74 6f 20 61  LeafWriter) to a
3483c 20 6c 65 76 65 6c 20 30 20 73 65 67 6d 65 6e 74   level 0 segment
3483d 2c 20 77 69 74 68 0a 2a 2a 20 69 6e 63 72 65 6d  , with.** increm
3483e 65 6e 74 69 6e 67 20 69 64 78 2e 20 20 57 68 65  enting idx.  Whe
3483f 6e 20 69 64 78 20 72 65 61 63 68 65 73 20 4d 45  n idx reaches ME
34840 52 47 45 5f 43 4f 55 4e 54 20 28 64 65 66 61 75  RGE_COUNT (defau
34841 6c 74 20 31 36 29 2c 20 61 6c 6c 0a 2a 2a 20 6c  lt 16), all.** l
34842 65 76 65 6c 20 30 20 73 65 67 6d 65 6e 74 73 20  evel 0 segments 
34843 61 72 65 20 6d 65 72 67 65 64 20 69 6e 74 6f 20  are merged into 
34844 61 20 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20 31  a single level 1
34845 20 73 65 67 6d 65 6e 74 2e 20 20 4c 65 76 65 6c   segment.  Level
34846 20 31 0a 2a 2a 20 69 73 20 70 6f 70 75 6c 61 74   1.** is populat
34847 65 64 20 6c 69 6b 65 20 6c 65 76 65 6c 20 30 2c  ed like level 0,
34848 20 61 6e 64 20 65 76 65 6e 74 75 61 6c 6c 79 20   and eventually 
34849 4d 45 52 47 45 5f 43 4f 55 4e 54 20 6c 65 76 65  MERGE_COUNT leve
3484a 6c 20 31 0a 2a 2a 20 73 65 67 6d 65 6e 74 73 20  l 1.** segments 
3484b 61 72 65 20 6d 65 72 67 65 64 20 74 6f 20 61 20  are merged to a 
3484c 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20 32 20 73  single level 2 s
3484d 65 67 6d 65 6e 74 20 28 72 65 70 72 65 73 65 6e  egment (represen
3484e 74 69 6e 67 0a 2a 2a 20 4d 45 52 47 45 5f 43 4f  ting.** MERGE_CO
3484f 55 4e 54 5e 32 20 75 70 64 61 74 65 73 29 2c 20  UNT^2 updates), 
34850 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a  and so on..**.**
34851 20 41 20 73 65 67 6d 65 6e 74 20 6d 65 72 67 65   A segment merge
34852 20 74 72 61 76 65 72 73 65 73 20 61 6c 6c 20 73   traverses all s
34853 65 67 6d 65 6e 74 73 20 61 74 20 61 20 67 69 76  egments at a giv
34854 65 6e 20 6c 65 76 65 6c 20 69 6e 0a 2a 2a 20 70  en level in.** p
34855 61 72 61 6c 6c 65 6c 2c 20 70 65 72 66 6f 72 6d  arallel, perform
34856 69 6e 67 20 61 20 73 74 72 61 69 67 68 74 66 6f  ing a straightfo
34857 72 77 61 72 64 20 73 6f 72 74 65 64 20 6d 65 72  rward sorted mer
34858 67 65 2e 20 20 53 69 6e 63 65 20 73 65 67 6d 65  ge.  Since segme
34859 6e 74 0a 2a 2a 20 6c 65 61 66 20 6e 6f 64 65 73  nt.** leaf nodes
3485a 20 61 72 65 20 77 72 69 74 74 65 6e 20 69 6e 20   are written in 
3485b 74 6f 20 74 68 65 20 25 5f 73 65 67 6d 65 6e 74  to the %_segment
3485c 73 20 74 61 62 6c 65 20 69 6e 20 6f 72 64 65 72  s table in order
3485d 2c 20 74 68 69 73 0a 2a 2a 20 6d 65 72 67 65 20  , this.** merge 
3485e 74 72 61 76 65 72 73 65 73 20 74 68 65 20 75 6e  traverses the un
3485f 64 65 72 6c 79 69 6e 67 20 73 71 6c 69 74 65 20  derlying sqlite 
34860 64 69 73 6b 20 73 74 72 75 63 74 75 72 65 73 20  disk structures 
34861 65 66 66 69 63 69 65 6e 74 6c 79 2e 0a 2a 2a 20  efficiently..** 
34862 41 66 74 65 72 20 74 68 65 20 6d 65 72 67 65 2c  After the merge,
34863 20 61 6c 6c 20 73 65 67 6d 65 6e 74 20 62 6c 6f   all segment blo
34864 63 6b 73 20 66 72 6f 6d 20 74 68 65 20 6d 65 72  cks from the mer
34865 67 65 64 20 6c 65 76 65 6c 20 61 72 65 0a 2a 2a  ged level are.**
34866 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20   deleted..**.** 
34867 4d 45 52 47 45 5f 43 4f 55 4e 54 20 63 6f 6e 74  MERGE_COUNT cont
34868 72 6f 6c 73 20 68 6f 77 20 6f 66 74 65 6e 20 77  rols how often w
34869 65 20 6d 65 72 67 65 20 73 65 67 6d 65 6e 74 73  e merge segments
3486a 2e 20 20 31 36 20 73 65 65 6d 73 20 74 6f 20 62  .  16 seems to b
3486b 65 0a 2a 2a 20 73 6f 6d 65 77 68 61 74 20 6f 66  e.** somewhat of
3486c 20 61 20 73 77 65 65 74 20 73 70 6f 74 20 66 6f   a sweet spot fo
3486d 72 20 69 6e 73 65 72 74 69 6f 6e 20 70 65 72 66  r insertion perf
3486e 6f 72 6d 61 6e 63 65 2e 20 20 33 32 20 61 6e 64  ormance.  32 and
3486f 20 36 34 20 73 68 6f 77 0a 2a 2a 20 76 65 72 79   64 show.** very
34870 20 73 69 6d 69 6c 61 72 20 70 65 72 66 6f 72 6d   similar perform
34871 61 6e 63 65 20 6e 75 6d 62 65 72 73 20 74 6f 20  ance numbers to 
34872 31 36 20 6f 6e 20 69 6e 73 65 72 74 69 6f 6e 2c  16 on insertion,
34873 20 74 68 6f 75 67 68 20 74 68 65 79 27 72 65 0a   though they're.
34874 2a 2a 20 61 20 74 69 6e 79 20 62 69 74 20 73 6c  ** a tiny bit sl
34875 6f 77 65 72 20 28 70 65 72 68 61 70 73 20 64 75  ower (perhaps du
34876 65 20 74 6f 20 6d 6f 72 65 20 6f 76 65 72 68 65  e to more overhe
34877 61 64 20 69 6e 20 6d 65 72 67 65 2d 74 69 6d 65  ad in merge-time
34878 0a 2a 2a 20 73 6f 72 74 69 6e 67 29 2e 20 20 38  .** sorting).  8
34879 20 69 73 20 61 62 6f 75 74 20 32 30 25 20 73 6c   is about 20% sl
3487a 6f 77 65 72 20 74 68 61 6e 20 31 36 2c 20 34 20  ower than 16, 4 
3487b 61 62 6f 75 74 20 35 30 25 20 73 6c 6f 77 65 72  about 50% slower
3487c 20 74 68 61 6e 0a 2a 2a 20 31 36 2c 20 32 20 61   than.** 16, 2 a
3487d 62 6f 75 74 20 36 36 25 20 73 6c 6f 77 65 72 20  bout 66% slower 
3487e 74 68 61 6e 20 31 36 2e 0a 2a 2a 0a 2a 2a 20 41  than 16..**.** A
3487f 74 20 71 75 65 72 79 20 74 69 6d 65 2c 20 68 69  t query time, hi
34880 67 68 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 69  gh MERGE_COUNT i
34881 6e 63 72 65 61 73 65 73 20 74 68 65 20 6e 75 6d  ncreases the num
34882 62 65 72 20 6f 66 20 73 65 67 6d 65 6e 74 73 0a  ber of segments.
34883 2a 2a 20 77 68 69 63 68 20 6e 65 65 64 20 74 6f  ** which need to
34884 20 62 65 20 73 63 61 6e 6e 65 64 20 61 6e 64 20   be scanned and 
34885 6d 65 72 67 65 64 2e 20 20 46 6f 72 20 69 6e 73  merged.  For ins
34886 74 61 6e 63 65 2c 20 77 69 74 68 20 31 30 30 6b  tance, with 100k
34887 20 64 6f 63 73 0a 2a 2a 20 69 6e 73 65 72 74 65   docs.** inserte
34888 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 45 52 47  d:.**.**    MERG
34889 45 5f 43 4f 55 4e 54 20 20 20 73 65 67 6d 65 6e  E_COUNT   segmen
3488a 74 73 0a 2a 2a 20 20 20 20 20 20 20 31 36 20 20  ts.**       16  
3488b 20 20 20 20 20 20 20 20 20 32 35 0a 2a 2a 20 20           25.**  
3488c 20 20 20 20 20 20 38 20 20 20 20 20 20 20 20 20        8         
3488d 20 20 31 32 0a 2a 2a 20 20 20 20 20 20 20 20 34    12.**        4
3488e 20 20 20 20 20 20 20 20 20 20 20 31 30 0a 2a 2a             10.**
3488f 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20          2       
34890 20 20 20 20 20 36 0a 2a 2a 0a 2a 2a 20 54 68 69       6.**.** Thi
34891 73 20 61 70 70 65 61 72 73 20 74 6f 20 68 61 76  s appears to hav
34892 65 20 6f 6e 6c 79 20 61 20 6d 6f 64 65 72 61 74  e only a moderat
34893 65 20 69 6d 70 61 63 74 20 6f 6e 20 71 75 65 72  e impact on quer
34894 69 65 73 20 66 6f 72 20 76 65 72 79 0a 2a 2a 20  ies for very.** 
34895 66 72 65 71 75 65 6e 74 20 74 65 72 6d 73 20 28  frequent terms (
34896 77 68 69 63 68 20 61 72 65 20 73 6f 6d 65 77 68  which are somewh
34897 61 74 20 64 6f 6d 69 6e 61 74 65 64 20 62 79 20  at dominated by 
34898 73 65 67 6d 65 6e 74 20 6d 65 72 67 65 0a 2a 2a  segment merge.**
34899 20 63 6f 73 74 73 29 2c 20 61 6e 64 20 69 6e 66   costs), and inf
3489a 72 65 71 75 65 6e 74 20 61 6e 64 20 6e 6f 6e 2d  requent and non-
3489b 65 78 69 73 74 65 6e 74 20 74 65 72 6d 73 20 73  existent terms s
3489c 74 69 6c 6c 20 73 65 65 6d 20 74 6f 20 62 65 20  till seem to be 
3489d 66 61 73 74 0a 2a 2a 20 65 76 65 6e 20 77 69 74  fast.** even wit
3489e 68 20 6d 61 6e 79 20 73 65 67 6d 65 6e 74 73 2e  h many segments.
3489f 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68 65 73  .**.** TODO(shes
348a0 73 29 20 54 68 61 74 20 73 61 69 64 2c 20 69 74  s) That said, it
348a1 20 77 6f 75 6c 64 20 62 65 20 6e 69 63 65 20 74   would be nice t
348a2 6f 20 68 61 76 65 20 61 20 62 65 74 74 65 72 20  o have a better 
348a3 71 75 65 72 79 2d 73 69 64 65 0a 2a 2a 20 61 72  query-side.** ar
348a4 67 75 6d 65 6e 74 20 66 6f 72 20 4d 45 52 47 45  gument for MERGE
348a5 5f 43 4f 55 4e 54 20 6f 66 20 31 36 2e 20 20 41  _COUNT of 16.  A
348a6 6c 73 6f 2c 20 69 74 20 69 73 20 70 6f 73 73 69  lso, it is possi
348a7 62 6c 65 2f 6c 69 6b 65 6c 79 20 74 68 61 74 0a  ble/likely that.
348a8 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73  ** optimizations
348a9 20 74 6f 20 74 68 69 6e 67 73 20 6c 69 6b 65 20   to things like 
348aa 64 6f 63 6c 69 73 74 20 6d 65 72 67 69 6e 67 20  doclist merging 
348ab 77 69 6c 6c 20 73 77 69 6e 67 20 74 68 65 20 73  will swing the s
348ac 77 65 65 74 0a 2a 2a 20 73 70 6f 74 20 61 72 6f  weet.** spot aro
348ad 75 6e 64 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a  und..**.**.**.**
348ae 2a 2a 20 48 61 6e 64 6c 69 6e 67 20 6f 66 20 64  ** Handling of d
348af 65 6c 65 74 69 6f 6e 73 20 61 6e 64 20 75 70 64  eletions and upd
348b0 61 74 65 73 20 2a 2a 2a 2a 0a 2a 2a 20 53 69 6e  ates ****.** Sin
348b1 63 65 20 77 65 27 72 65 20 75 73 69 6e 67 20 61  ce we're using a
348b2 20 73 65 67 6d 65 6e 74 65 64 20 73 74 72 75 63   segmented struc
348b3 74 75 72 65 2c 20 77 69 74 68 20 6e 6f 20 64 6f  ture, with no do
348b4 63 69 64 2d 6f 72 69 65 6e 74 65 64 0a 2a 2a 20  cid-oriented.** 
348b5 69 6e 64 65 78 20 69 6e 74 6f 20 74 68 65 20 74  index into the t
348b6 65 72 6d 20 69 6e 64 65 78 2c 20 77 65 20 63 6c  erm index, we cl
348b7 65 61 72 6c 79 20 63 61 6e 6e 6f 74 20 73 69 6d  early cannot sim
348b8 70 6c 79 20 75 70 64 61 74 65 20 74 68 65 20 74  ply update the t
348b9 65 72 6d 0a 2a 2a 20 69 6e 64 65 78 20 77 68 65  erm.** index whe
348ba 6e 20 61 20 64 6f 63 75 6d 65 6e 74 20 69 73 20  n a document is 
348bb 64 65 6c 65 74 65 64 20 6f 72 20 75 70 64 61 74  deleted or updat
348bc 65 64 2e 20 20 46 6f 72 20 64 65 6c 65 74 69 6f  ed.  For deletio
348bd 6e 73 2c 20 77 65 0a 2a 2a 20 77 72 69 74 65 20  ns, we.** write 
348be 61 6e 20 65 6d 70 74 79 20 64 6f 63 6c 69 73 74  an empty doclist
348bf 20 28 76 61 72 69 6e 74 28 64 6f 63 69 64 29 20   (varint(docid) 
348c0 76 61 72 69 6e 74 28 50 4f 53 5f 45 4e 44 29 29  varint(POS_END))
348c1 2c 20 66 6f 72 20 75 70 64 61 74 65 73 0a 2a 2a  , for updates.**
348c2 20 77 65 20 73 69 6d 70 6c 79 20 77 72 69 74 65   we simply write
348c3 20 74 68 65 20 6e 65 77 20 64 6f 63 6c 69 73 74   the new doclist
348c4 2e 20 20 53 65 67 6d 65 6e 74 20 6d 65 72 67 65  .  Segment merge
348c5 73 20 6f 76 65 72 77 72 69 74 65 20 6f 6c 64 65  s overwrite olde
348c6 72 0a 2a 2a 20 64 61 74 61 20 66 6f 72 20 61 20  r.** data for a 
348c7 70 61 72 74 69 63 75 6c 61 72 20 64 6f 63 69 64  particular docid
348c8 20 77 69 74 68 20 6e 65 77 65 72 20 64 61 74 61   with newer data
348c9 2c 20 73 6f 20 64 65 6c 65 74 65 73 20 6f 72 20  , so deletes or 
348ca 75 70 64 61 74 65 73 0a 2a 2a 20 77 69 6c 6c 20  updates.** will 
348cb 65 76 65 6e 74 75 61 6c 6c 79 20 6f 76 65 72 74  eventually overt
348cc 61 6b 65 20 74 68 65 20 65 61 72 6c 69 65 72 20  ake the earlier 
348cd 64 61 74 61 20 61 6e 64 20 6b 6e 6f 63 6b 20 69  data and knock i
348ce 74 20 6f 75 74 2e 20 20 54 68 65 0a 2a 2a 20 71  t out.  The.** q
348cf 75 65 72 79 20 6c 6f 67 69 63 20 6c 69 6b 65 77  uery logic likew
348d0 69 73 65 20 6d 65 72 67 65 73 20 64 6f 63 6c 69  ise merges docli
348d1 73 74 73 20 73 6f 20 74 68 61 74 20 6e 65 77 65  sts so that newe
348d2 72 20 64 61 74 61 20 6b 6e 6f 63 6b 73 20 6f 75  r data knocks ou
348d3 74 0a 2a 2a 20 6f 6c 64 65 72 20 64 61 74 61 2e  t.** older data.
348d4 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68 65 73  .**.** TODO(shes
348d5 73 29 20 50 72 6f 76 69 64 65 20 61 20 56 41 43  s) Provide a VAC
348d6 55 55 4d 20 74 79 70 65 20 6f 70 65 72 61 74 69  UUM type operati
348d7 6f 6e 20 74 6f 20 63 6c 65 61 72 20 6f 75 74 20  on to clear out 
348d8 61 6c 6c 0a 2a 2a 20 64 65 6c 65 74 69 6f 6e 73  all.** deletions
348d9 20 61 6e 64 20 64 75 70 6c 69 63 61 74 69 6f 6e   and duplication
348da 73 2e 20 20 54 68 69 73 20 77 6f 75 6c 64 20 62  s.  This would b
348db 61 73 69 63 61 6c 6c 79 20 62 65 20 61 20 66 6f  asically be a fo
348dc 72 63 65 64 20 6d 65 72 67 65 0a 2a 2a 20 69 6e  rced merge.** in
348dd 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 65 67 6d  to a single segm
348de 65 6e 74 2e 0a 2a 2f 0a 0a 23 69 66 20 21 64 65  ent..*/..#if !de
348df 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
348e0 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
348e1 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
348e2 29 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  )..#if defined(S
348e3 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
348e4 33 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  3) && !defined(S
348e5 51 4c 49 54 45 5f 43 4f 52 45 29 0a 23 20 64 65  QLITE_CORE).# de
348e6 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45  fine SQLITE_CORE
348e7 20 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 2a 2a   1.#endif.../***
348e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
348e9 75 64 65 20 66 74 73 33 5f 65 78 70 72 2e 68 20  ude fts3_expr.h 
348ea 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
348eb 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   fts3.c ********
348ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
348ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
348ee 6e 20 66 69 6c 65 20 66 74 73 33 5f 65 78 70 72  n file fts3_expr
348ef 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
348f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
348f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
348f2 2a 20 32 30 30 38 20 4e 6f 76 20 32 38 0a 2a 2a  * 2008 Nov 28.**
348f3 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
348f4 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
348f5 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
348f6 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
348f7 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
348f8 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
348f9 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
348fa 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
348fb 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
348fc 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
348fd 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
348fe 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
348ff 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
34900 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
34901 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
34902 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
34903 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
34904 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
34905 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34906 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34907 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34908 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34909 2a 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  *.**.*/../******
3490a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
3490b 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e   fts3_tokenizer.
3490c 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
3490d 6f 66 20 66 74 73 33 5f 65 78 70 72 2e 68 20 2a  of fts3_expr.h *
3490e 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
3490f 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
34910 69 6c 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  ile fts3_tokeniz
34911 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.h ***********
34912 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34913 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
34914 30 30 36 20 4a 75 6c 79 20 31 30 0a 2a 2a 0a 2a  006 July 10.**.*
34915 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
34916 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
34917 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
34918 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  code..**.*******
34919 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3491a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3491b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3491c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3491d 2a 2a 0a 2a 2a 20 44 65 66 69 6e 65 73 20 74 68  **.** Defines th
3491e 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74  e interface to t
3491f 6f 6b 65 6e 69 7a 65 72 73 20 75 73 65 64 20 62  okenizers used b
34920 79 20 66 75 6c 6c 74 65 78 74 2d 73 65 61 72 63  y fulltext-searc
34921 68 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61 72 65  h.  There.** are
34922 20 74 68 72 65 65 20 62 61 73 69 63 20 63 6f 6d   three basic com
34923 70 6f 6e 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 73  ponents:.**.** s
34924 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
34925 5f 6d 6f 64 75 6c 65 20 69 73 20 61 20 73 69 6e  _module is a sin
34926 67 6c 65 74 6f 6e 20 64 65 66 69 6e 69 6e 67 20  gleton defining 
34927 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a  the tokenizer.**
34928 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e 63 74   interface funct
34929 69 6f 6e 73 2e 20 20 54 68 69 73 20 69 73 20 65  ions.  This is e
3492a 73 73 65 6e 74 69 61 6c 6c 79 20 74 68 65 20 63  ssentially the c
3492b 6c 61 73 73 20 73 74 72 75 63 74 75 72 65 20 66  lass structure f
3492c 6f 72 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72 73  or.** tokenizers
3492d 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ..**.** sqlite3_
3492e 74 6f 6b 65 6e 69 7a 65 72 20 69 73 20 75 73 65  tokenizer is use
3492f 64 20 74 6f 20 64 65 66 69 6e 65 20 61 20 70 61  d to define a pa
34930 72 74 69 63 75 6c 61 72 20 74 6f 6b 65 6e 69 7a  rticular tokeniz
34931 65 72 2c 20 70 65 72 68 61 70 73 0a 2a 2a 20 69  er, perhaps.** i
34932 6e 63 6c 75 64 69 6e 67 20 63 75 73 74 6f 6d 69  ncluding customi
34933 7a 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69  zation informati
34934 6f 6e 20 64 65 66 69 6e 65 64 20 61 74 20 63 72  on defined at cr
34935 65 61 74 69 6f 6e 20 74 69 6d 65 2e 0a 2a 2a 0a  eation time..**.
34936 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  ** sqlite3_token
34937 69 7a 65 72 5f 63 75 72 73 6f 72 20 69 73 20 67  izer_cursor is g
34938 65 6e 65 72 61 74 65 64 20 62 79 20 61 20 74 6f  enerated by a to
34939 6b 65 6e 69 7a 65 72 20 74 6f 20 67 65 6e 65 72  kenizer to gener
3493a 61 74 65 0a 2a 2a 20 74 6f 6b 65 6e 73 20 66 72  ate.** tokens fr
3493b 6f 6d 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  om a particular 
3493c 69 6e 70 75 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65  input..*/.#ifnde
3493d 66 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45  f _FTS3_TOKENIZE
3493e 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 46 54  R_H_.#define _FT
3493f 53 33 5f 54 4f 4b 45 4e 49 5a 45 52 5f 48 5f 0a  S3_TOKENIZER_H_.
34940 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
34941 4f 6e 6c 79 20 75 73 65 64 20 66 6f 72 20 53 51  Only used for SQ
34942 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 53 51 4c 49  LITE_OK and SQLI
34943 54 45 5f 44 4f 4e 45 20 61 74 20 74 68 69 73 20  TE_DONE at this 
34944 74 69 6d 65 2e 0a 2a 2a 20 49 66 20 74 6f 6b 65  time..** If toke
34945 6e 69 7a 65 72 73 20 61 72 65 20 74 6f 20 62 65  nizers are to be
34946 20 61 6c 6c 6f 77 65 64 20 74 6f 20 63 61 6c 6c   allowed to call
34947 20 73 71 6c 69 74 65 33 5f 2a 28 29 20 66 75 6e   sqlite3_*() fun
34948 63 74 69 6f 6e 73 2c 20 74 68 65 6e 0a 2a 2a 20  ctions, then.** 
34949 77 65 20 77 69 6c 6c 20 6e 65 65 64 20 61 20 77  we will need a w
3494a 61 79 20 74 6f 20 72 65 67 69 73 74 65 72 20 74  ay to register t
3494b 68 65 20 41 50 49 20 63 6f 6e 73 69 73 74 65 6e  he API consisten
3494c 74 6c 79 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53  tly..*/../*.** S
3494d 74 72 75 63 74 75 72 65 73 20 75 73 65 64 20 62  tructures used b
3494e 79 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20  y the tokenizer 
3494f 69 6e 74 65 72 66 61 63 65 2e 20 57 68 65 6e 20  interface. When 
34950 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65 72 0a  a new tokenizer.
34951 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
34952 6e 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2c  n is registered,
34953 20 74 68 65 20 63 61 6c 6c 65 72 20 70 72 6f 76   the caller prov
34954 69 64 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74  ides a pointer t
34955 6f 0a 2a 2a 20 61 6e 20 73 71 6c 69 74 65 33 5f  o.** an sqlite3_
34956 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
34957 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 6f 69 6e   containing poin
34958 74 65 72 73 20 74 6f 20 74 68 65 20 63 61 6c 6c  ters to the call
34959 62 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  back.** function
3495a 73 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 61  s that make up a
3495b 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
3495c 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 6e 20  ..**.** When an 
3495d 66 74 73 33 20 74 61 62 6c 65 20 69 73 20 63 72  fts3 table is cr
3495e 65 61 74 65 64 2c 20 69 74 20 70 61 73 73 65 73  eated, it passes
3495f 20 61 6e 79 20 61 72 67 75 6d 65 6e 74 73 20 70   any arguments p
34960 61 73 73 65 64 20 74 6f 0a 2a 2a 20 74 68 65 20  assed to.** the 
34961 74 6f 6b 65 6e 69 7a 65 72 20 63 6c 61 75 73 65  tokenizer clause
34962 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 56   of the CREATE V
34963 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61  IRTUAL TABLE sta
34964 74 65 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a 2a  tement to the.**
34965 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
34966 65 72 5f 6d 6f 64 75 6c 65 2e 78 43 72 65 61 74  er_module.xCreat
34967 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 6f 66 20  e() function of 
34968 74 68 65 20 72 65 71 75 65 73 74 65 64 20 74 6f  the requested to
34969 6b 65 6e 69 7a 65 72 0a 2a 2a 20 69 6d 70 6c 65  kenizer.** imple
3496a 6d 65 6e 74 61 74 69 6f 6e 2e 20 54 68 65 20 78  mentation. The x
3496b 43 72 65 61 74 65 28 29 20 66 75 6e 63 74 69 6f  Create() functio
3496c 6e 20 69 6e 20 74 75 72 6e 20 72 65 74 75 72 6e  n in turn return
3496d 73 20 61 6e 20 0a 2a 2a 20 73 71 6c 69 74 65 33  s an .** sqlite3
3496e 5f 74 6f 6b 65 6e 69 7a 65 72 20 73 74 72 75 63  _tokenizer struc
3496f 74 75 72 65 20 72 65 70 72 65 73 65 6e 74 69 6e  ture representin
34970 67 20 74 68 65 20 73 70 65 63 69 66 69 63 20 74  g the specific t
34971 6f 6b 65 6e 69 7a 65 72 20 74 6f 0a 2a 2a 20 62  okenizer to.** b
34972 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 66  e used for the f
34973 74 73 33 20 74 61 62 6c 65 20 28 63 75 73 74 6f  ts3 table (custo
34974 6d 69 7a 65 64 20 62 79 20 74 68 65 20 74 6f 6b  mized by the tok
34975 65 6e 69 7a 65 72 20 63 6c 61 75 73 65 20 61 72  enizer clause ar
34976 67 75 6d 65 6e 74 73 29 2e 0a 2a 2a 0a 2a 2a 20  guments)..**.** 
34977 54 6f 20 74 6f 6b 65 6e 69 7a 65 20 61 6e 20 69  To tokenize an i
34978 6e 70 75 74 20 62 75 66 66 65 72 2c 20 74 68 65  nput buffer, the
34979 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
3497a 65 72 5f 6d 6f 64 75 6c 65 2e 78 4f 70 65 6e 28  er_module.xOpen(
3497b 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 69 73 20 63  ).** method is c
3497c 61 6c 6c 65 64 2e 20 49 74 20 72 65 74 75 72 6e  alled. It return
3497d 73 20 61 6e 20 73 71 6c 69 74 65 33 5f 74 6f 6b  s an sqlite3_tok
3497e 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 6f 62  enizer_cursor ob
3497f 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20 6d 61 79  ject.** that may
34980 20 62 65 20 75 73 65 64 20 74 6f 20 74 6f 6b 65   be used to toke
34981 6e 69 7a 65 20 61 20 73 70 65 63 69 66 69 63 20  nize a specific 
34982 69 6e 70 75 74 20 62 75 66 66 65 72 20 62 61 73  input buffer bas
34983 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 74 6f 6b  ed on.** the tok
34984 65 6e 69 7a 61 74 69 6f 6e 20 72 75 6c 65 73 20  enization rules 
34985 73 75 70 70 6c 69 65 64 20 62 79 20 61 20 73 70  supplied by a sp
34986 65 63 69 66 69 63 20 73 71 6c 69 74 65 33 5f 74  ecific sqlite3_t
34987 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 6f 62 6a 65  okenizer.** obje
34988 63 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ct..*/.typedef s
34989 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 74 6f  truct sqlite3_to
3498a 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 73  kenizer_module s
3498b 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3498c 5f 6d 6f 64 75 6c 65 3b 0a 74 79 70 65 64 65 66  _module;.typedef
3498d 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
3498e 74 6f 6b 65 6e 69 7a 65 72 20 73 71 6c 69 74 65  tokenizer sqlite
3498f 33 5f 74 6f 6b 65 6e 69 7a 65 72 3b 0a 74 79 70  3_tokenizer;.typ
34990 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
34991 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
34992 72 73 6f 72 20 73 71 6c 69 74 65 33 5f 74 6f 6b  rsor sqlite3_tok
34993 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 3b 0a 0a  enizer_cursor;..
34994 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 74  struct sqlite3_t
34995 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
34996 7b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 74 72  {..  /*.  ** Str
34997 75 63 74 75 72 65 20 76 65 72 73 69 6f 6e 2e 20  ucture version. 
34998 53 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65  Should always be
34999 20 73 65 74 20 74 6f 20 30 2e 0a 20 20 2a 2f 0a   set to 0..  */.
3499a 20 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a    int iVersion;.
3499b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 72 65 61 74  .  /*.  ** Creat
3499c 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65  e a new tokenize
3499d 72 2e 20 54 68 65 20 76 61 6c 75 65 73 20 69 6e  r. The values in
3499e 20 74 68 65 20 61 72 67 76 5b 5d 20 61 72 72 61   the argv[] arra
3499f 79 20 61 72 65 20 74 68 65 0a 20 20 2a 2a 20 61  y are the.  ** a
349a0 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20  rguments passed 
349a1 74 6f 20 74 68 65 20 22 74 6f 6b 65 6e 69 7a 65  to the "tokenize
349a2 72 22 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  r" clause of the
349a3 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 0a   CREATE VIRTUAL.
349a4 20 20 2a 2a 20 54 41 42 4c 45 20 73 74 61 74 65    ** TABLE state
349a5 6d 65 6e 74 20 74 68 61 74 20 63 72 65 61 74 65  ment that create
349a6 64 20 74 68 65 20 66 74 73 33 20 74 61 62 6c 65  d the fts3 table
349a7 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69  . For example, i
349a8 66 0a 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f  f.  ** the follo
349a9 77 69 6e 67 20 53 51 4c 20 69 73 20 65 78 65 63  wing SQL is exec
349aa 75 74 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  uted:.  **.  ** 
349ab 20 20 43 52 45 41 54 45 20 2e 2e 20 55 53 49 4e    CREATE .. USIN
349ac 47 20 66 74 73 33 28 20 2e 2e 2e 20 2c 20 74 6f  G fts3( ... , to
349ad 6b 65 6e 69 7a 65 72 20 3c 74 6f 6b 65 6e 69 7a  kenizer <tokeniz
349ae 65 72 2d 6e 61 6d 65 3e 20 61 72 67 31 20 61 72  er-name> arg1 ar
349af 67 32 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 74 68  g2).  **.  ** th
349b0 65 6e 20 61 72 67 63 20 69 73 20 73 65 74 20 74  en argc is set t
349b1 6f 20 32 2c 20 61 6e 64 20 74 68 65 20 61 72 67  o 2, and the arg
349b2 76 5b 5d 20 61 72 72 61 79 20 63 6f 6e 74 61 69  v[] array contai
349b3 6e 73 20 70 6f 69 6e 74 65 72 73 0a 20 20 2a 2a  ns pointers.  **
349b4 20 74 6f 20 74 68 65 20 73 74 72 69 6e 67 73 20   to the strings 
349b5 22 61 72 67 31 22 20 61 6e 64 20 22 61 72 67 32  "arg1" and "arg2
349b6 22 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69  "..  **.  ** Thi
349b7 73 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20  s method should 
349b8 72 65 74 75 72 6e 20 65 69 74 68 65 72 20 53 51  return either SQ
349b9 4c 49 54 45 5f 4f 4b 20 28 30 29 2c 20 6f 72 20  LITE_OK (0), or 
349ba 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20  an SQLite error 
349bb 0a 20 20 2a 2a 20 63 6f 64 65 2e 20 49 66 20 53  .  ** code. If S
349bc 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
349bd 72 6e 65 64 2c 20 74 68 65 6e 20 2a 70 70 54 6f  rned, then *ppTo
349be 6b 65 6e 69 7a 65 72 20 73 68 6f 75 6c 64 20 62  kenizer should b
349bf 65 20 73 65 74 0a 20 20 2a 2a 20 74 6f 20 70 6f  e set.  ** to po
349c0 69 6e 74 20 61 74 20 74 68 65 20 6e 65 77 6c 79  int at the newly
349c1 20 63 72 65 61 74 65 64 20 74 6f 6b 65 6e 69 7a   created tokeniz
349c2 65 72 20 73 74 72 75 63 74 75 72 65 2e 20 54 68  er structure. Th
349c3 65 20 67 65 6e 65 72 69 63 0a 20 20 2a 2a 20 73  e generic.  ** s
349c4 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
349c5 2e 70 4d 6f 64 75 6c 65 20 76 61 72 69 61 62 6c  .pModule variabl
349c6 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  e should not be 
349c7 69 6e 69 74 69 61 6c 69 73 65 64 20 62 79 0a 20  initialised by. 
349c8 20 2a 2a 20 74 68 69 73 20 63 61 6c 6c 62 61 63   ** this callbac
349c9 6b 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77 69  k. The caller wi
349ca 6c 6c 20 64 6f 20 73 6f 2e 0a 20 20 2a 2f 0a 20  ll do so..  */. 
349cb 20 69 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28   int (*xCreate)(
349cc 0a 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 20  .    int argc,  
349cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
349ce 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
349cf 20 6f 66 20 61 72 67 76 20 61 72 72 61 79 20 2a   of argv array *
349d0 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  /.    const char
349d1 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 20 20 20   *const*argv,   
349d2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b            /* Tok
349d3 65 6e 69 7a 65 72 20 61 72 67 75 6d 65 6e 74 20  enizer argument 
349d4 73 74 72 69 6e 67 73 20 2a 2f 0a 20 20 20 20 73  strings */.    s
349d5 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
349d6 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20 20   **ppTokenizer  
349d7 20 20 20 2f 2a 20 4f 55 54 3a 20 43 72 65 61 74     /* OUT: Creat
349d8 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a  ed tokenizer */.
349d9 20 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20    );..  /*.  ** 
349da 44 65 73 74 72 6f 79 20 61 6e 20 65 78 69 73 74  Destroy an exist
349db 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72 2e 20 54  ing tokenizer. T
349dc 68 65 20 66 74 73 33 20 6d 6f 64 75 6c 65 20 63  he fts3 module c
349dd 61 6c 6c 73 20 74 68 69 73 20 6d 65 74 68 6f 64  alls this method
349de 0a 20 20 2a 2a 20 65 78 61 63 74 6c 79 20 6f 6e  .  ** exactly on
349df 63 65 20 66 6f 72 20 65 61 63 68 20 73 75 63 63  ce for each succ
349e0 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 78  essful call to x
349e1 43 72 65 61 74 65 28 29 2e 0a 20 20 2a 2f 0a 20  Create()..  */. 
349e2 20 69 6e 74 20 28 2a 78 44 65 73 74 72 6f 79 29   int (*xDestroy)
349e3 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a  (sqlite3_tokeniz
349e4 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 29 3b  er *pTokenizer);
349e5 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 72 65 61  ..  /*.  ** Crea
349e6 74 65 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20 63  te a tokenizer c
349e7 75 72 73 6f 72 20 74 6f 20 74 6f 6b 65 6e 69 7a  ursor to tokeniz
349e8 65 20 61 6e 20 69 6e 70 75 74 20 62 75 66 66 65  e an input buffe
349e9 72 2e 20 54 68 65 20 63 61 6c 6c 65 72 0a 20 20  r. The caller.  
349ea 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  ** is responsibl
349eb 65 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 74  e for ensuring t
349ec 68 61 74 20 74 68 65 20 69 6e 70 75 74 20 62 75  hat the input bu
349ed 66 66 65 72 20 72 65 6d 61 69 6e 73 20 76 61 6c  ffer remains val
349ee 69 64 0a 20 20 2a 2a 20 75 6e 74 69 6c 20 74 68  id.  ** until th
349ef 65 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f 73  e cursor is clos
349f0 65 64 20 28 75 73 69 6e 67 20 74 68 65 20 78 43  ed (using the xC
349f1 6c 6f 73 65 28 29 20 6d 65 74 68 6f 64 29 2e 20  lose() method). 
349f2 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 4f  .  */.  int (*xO
349f3 70 65 6e 29 28 0a 20 20 20 20 73 71 6c 69 74 65  pen)(.    sqlite
349f4 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f  3_tokenizer *pTo
349f5 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20 20 20 2f  kenizer,       /
349f6 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 6f 62 6a 65  * Tokenizer obje
349f7 63 74 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20  ct */.    const 
349f8 63 68 61 72 20 2a 70 49 6e 70 75 74 2c 20 69 6e  char *pInput, in
349f9 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 2f  t nBytes,      /
349fa 2a 20 49 6e 70 75 74 20 62 75 66 66 65 72 20 2a  * Input buffer *
349fb 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 74 6f  /.    sqlite3_to
349fc 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
349fd 2a 70 70 43 75 72 73 6f 72 20 20 2f 2a 20 4f 55  *ppCursor  /* OU
349fe 54 3a 20 43 72 65 61 74 65 64 20 74 6f 6b 65 6e  T: Created token
349ff 69 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20  izer cursor */. 
34a00 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 44   );..  /*.  ** D
34a01 65 73 74 72 6f 79 20 61 6e 20 65 78 69 73 74 69  estroy an existi
34a02 6e 67 20 74 6f 6b 65 6e 69 7a 65 72 20 63 75 72  ng tokenizer cur
34a03 73 6f 72 2e 20 54 68 65 20 66 74 73 33 20 6d 6f  sor. The fts3 mo
34a04 64 75 6c 65 20 63 61 6c 6c 73 20 74 68 69 73 20  dule calls this 
34a05 0a 20 20 2a 2a 20 6d 65 74 68 6f 64 20 65 78 61  .  ** method exa
34a06 63 74 6c 79 20 6f 6e 63 65 20 66 6f 72 20 65 61  ctly once for ea
34a07 63 68 20 73 75 63 63 65 73 73 66 75 6c 20 63 61  ch successful ca
34a08 6c 6c 20 74 6f 20 78 4f 70 65 6e 28 29 2e 0a 20  ll to xOpen().. 
34a09 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f   */.  int (*xClo
34a0a 73 65 29 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65  se)(sqlite3_toke
34a0b 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43  nizer_cursor *pC
34a0c 75 72 73 6f 72 29 3b 0a 0a 20 20 2f 2a 0a 20 20  ursor);..  /*.  
34a0d 2a 2a 20 52 65 74 72 69 65 76 65 20 74 68 65 20  ** Retrieve the 
34a0e 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20  next token from 
34a0f 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 63 75  the tokenizer cu
34a10 72 73 6f 72 20 70 43 75 72 73 6f 72 2e 20 54 68  rsor pCursor. Th
34a11 69 73 0a 20 20 2a 2a 20 6d 65 74 68 6f 64 20 73  is.  ** method s
34a12 68 6f 75 6c 64 20 65 69 74 68 65 72 20 72 65 74  hould either ret
34a13 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e  urn SQLITE_OK an
34a14 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 73  d set the values
34a15 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 22 4f 55   of the.  ** "OU
34a16 54 22 20 76 61 72 69 61 62 6c 65 73 20 69 64 65  T" variables ide
34a17 6e 74 69 66 69 65 64 20 62 65 6c 6f 77 2c 20 6f  ntified below, o
34a18 72 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 74 6f  r SQLITE_DONE to
34a19 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 0a 20   indicate that. 
34a1a 20 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74   ** the end of t
34a1b 68 65 20 62 75 66 66 65 72 20 68 61 73 20 62 65  he buffer has be
34a1c 65 6e 20 72 65 61 63 68 65 64 2c 20 6f 72 20 61  en reached, or a
34a1d 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
34a1e 6f 64 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 2a  ode..  **.  ** *
34a1f 70 70 54 6f 6b 65 6e 20 73 68 6f 75 6c 64 20 62  ppToken should b
34a20 65 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61  e set to point a
34a21 74 20 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61  t a buffer conta
34a22 69 6e 69 6e 67 20 74 68 65 20 0a 20 20 2a 2a 20  ining the .  ** 
34a23 6e 6f 72 6d 61 6c 69 7a 65 64 20 76 65 72 73 69  normalized versi
34a24 6f 6e 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20  on of the token 
34a25 28 69 2e 65 2e 20 61 66 74 65 72 20 61 6e 79 20  (i.e. after any 
34a26 63 61 73 65 2d 66 6f 6c 64 69 6e 67 20 61 6e 64  case-folding and
34a27 2f 6f 72 0a 20 20 2a 2a 20 73 74 65 6d 6d 69 6e  /or.  ** stemmin
34a28 67 20 68 61 73 20 62 65 65 6e 20 70 65 72 66 6f  g has been perfo
34a29 72 6d 65 64 29 2e 20 2a 70 6e 42 79 74 65 73 20  rmed). *pnBytes 
34a2a 73 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f  should be set to
34a2b 20 74 68 65 20 6c 65 6e 67 74 68 0a 20 20 2a 2a   the length.  **
34a2c 20 6f 66 20 74 68 69 73 20 62 75 66 66 65 72 20   of this buffer 
34a2d 69 6e 20 62 79 74 65 73 2e 20 54 68 65 20 69 6e  in bytes. The in
34a2e 70 75 74 20 74 65 78 74 20 74 68 61 74 20 67 65  put text that ge
34a2f 6e 65 72 61 74 65 64 20 74 68 65 20 74 6f 6b 65  nerated the toke
34a30 6e 20 69 73 0a 20 20 2a 2a 20 69 64 65 6e 74 69  n is.  ** identi
34a31 66 69 65 64 20 62 79 20 74 68 65 20 62 79 74 65  fied by the byte
34a32 20 6f 66 66 73 65 74 73 20 72 65 74 75 72 6e 65   offsets returne
34a33 64 20 69 6e 20 2a 70 69 53 74 61 72 74 4f 66 66  d in *piStartOff
34a34 73 65 74 20 61 6e 64 0a 20 20 2a 2a 20 2a 70 69  set and.  ** *pi
34a35 45 6e 64 4f 66 66 73 65 74 2e 20 2a 70 69 53 74  EndOffset. *piSt
34a36 61 72 74 4f 66 66 73 65 74 20 73 68 6f 75 6c 64  artOffset should
34a37 20 62 65 20 73 65 74 20 74 6f 20 74 68 65 20 69   be set to the i
34a38 6e 64 65 78 20 6f 66 20 74 68 65 20 66 69 72 73  ndex of the firs
34a39 74 0a 20 20 2a 2a 20 62 79 74 65 20 6f 66 20 74  t.  ** byte of t
34a3a 68 65 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20  he token in the 
34a3b 69 6e 70 75 74 20 62 75 66 66 65 72 2e 20 2a 70  input buffer. *p
34a3c 69 45 6e 64 4f 66 66 73 65 74 20 73 68 6f 75 6c  iEndOffset shoul
34a3d 64 20 62 65 20 73 65 74 0a 20 20 2a 2a 20 74 6f  d be set.  ** to
34a3e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
34a3f 65 20 66 69 72 73 74 20 62 79 74 65 20 6a 75 73  e first byte jus
34a40 74 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f  t past the end o
34a41 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 6e 0a 20  f the token in. 
34a42 20 2a 2a 20 74 68 65 20 69 6e 70 75 74 20 62 75   ** the input bu
34a43 66 66 65 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ffer..  **.  ** 
34a44 54 68 65 20 62 75 66 66 65 72 20 2a 70 70 54 6f  The buffer *ppTo
34a45 6b 65 6e 20 69 73 20 73 65 74 20 74 6f 20 70 6f  ken is set to po
34a46 69 6e 74 20 61 74 20 69 73 20 6d 61 6e 61 67 65  int at is manage
34a47 64 20 62 79 20 74 68 65 20 74 6f 6b 65 6e 69 7a  d by the tokeniz
34a48 65 72 0a 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e  er.  ** implemen
34a49 74 61 74 69 6f 6e 2e 20 49 74 20 69 73 20 6f 6e  tation. It is on
34a4a 6c 79 20 72 65 71 75 69 72 65 64 20 74 6f 20 62  ly required to b
34a4b 65 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68  e valid until th
34a4c 65 20 6e 65 78 74 20 63 61 6c 6c 0a 20 20 2a 2a  e next call.  **
34a4d 20 74 6f 20 78 4e 65 78 74 28 29 20 6f 72 20 78   to xNext() or x
34a4e 43 6c 6f 73 65 28 29 2e 20 0a 20 20 2a 2f 0a 20  Close(). .  */. 
34a4f 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
34a50 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e  current implemen
34a51 74 61 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20  tation requires 
34a52 70 49 6e 70 75 74 20 74 6f 20 62 65 0a 20 20 2a  pInput to be.  *
34a53 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64  * nul-terminated
34a54 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20 65  .  This should e
34a55 69 74 68 65 72 20 62 65 20 66 69 78 65 64 2c 20  ither be fixed, 
34a56 6f 72 20 70 49 6e 70 75 74 2f 6e 42 79 74 65 73  or pInput/nBytes
34a57 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20  .  ** should be 
34a58 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 7a 49 6e  converted to zIn
34a59 70 75 74 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  put..  */.  int 
34a5a 28 2a 78 4e 65 78 74 29 28 0a 20 20 20 20 73 71  (*xNext)(.    sq
34a5b 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
34a5c 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c  cursor *pCursor,
34a5d 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20     /* Tokenizer 
34a5e 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 63 6f  cursor */.    co
34a5f 6e 73 74 20 63 68 61 72 20 2a 2a 70 70 54 6f 6b  nst char **ppTok
34a60 65 6e 2c 20 69 6e 74 20 2a 70 6e 42 79 74 65 73  en, int *pnBytes
34a61 2c 20 20 2f 2a 20 4f 55 54 3a 20 4e 6f 72 6d 61  ,  /* OUT: Norma
34a62 6c 69 7a 65 64 20 74 65 78 74 20 66 6f 72 20 74  lized text for t
34a63 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20  oken */.    int 
34a64 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20  *piStartOffset, 
34a65 20 2f 2a 20 4f 55 54 3a 20 42 79 74 65 20 6f 66   /* OUT: Byte of
34a66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 69 6e  fset of token in
34a67 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 2a 2f   input buffer */
34a68 0a 20 20 20 20 69 6e 74 20 2a 70 69 45 6e 64 4f  .    int *piEndO
34a69 66 66 73 65 74 2c 20 20 20 20 2f 2a 20 4f 55 54  ffset,    /* OUT
34a6a 3a 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66  : Byte offset of
34a6b 20 65 6e 64 20 6f 66 20 74 6f 6b 65 6e 20 69 6e   end of token in
34a6c 20 69 6e 70 75 74 20 62 75 66 66 65 72 20 2a 2f   input buffer */
34a6d 0a 20 20 20 20 69 6e 74 20 2a 70 69 50 6f 73 69  .    int *piPosi
34a6e 74 69 6f 6e 20 20 20 20 20 20 2f 2a 20 4f 55 54  tion      /* OUT
34a6f 3a 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65  : Number of toke
34a70 6e 73 20 72 65 74 75 72 6e 65 64 20 62 65 66 6f  ns returned befo
34a71 72 65 20 74 68 69 73 20 6f 6e 65 20 2a 2f 0a 20  re this one */. 
34a72 20 29 3b 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 73   );.};..struct s
34a73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
34a74 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74   {.  const sqlit
34a75 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
34a76 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 2f  ule *pModule;  /
34a77 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 66 6f 72  * The module for
34a78 20 74 68 69 73 20 74 6f 6b 65 6e 69 7a 65 72 20   this tokenizer 
34a79 2a 2f 0a 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65  */.  /* Tokenize
34a7a 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  r implementation
34a7b 73 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79  s will typically
34a7c 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20   add additional 
34a7d 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 73 74  fields */.};..st
34a7e 72 75 63 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  ruct sqlite3_tok
34a7f 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 7b 0a  enizer_cursor {.
34a80 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
34a81 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 3b  zer *pTokenizer;
34a82 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69         /* Tokeni
34a83 7a 65 72 20 66 6f 72 20 74 68 69 73 20 63 75 72  zer for this cur
34a84 73 6f 72 2e 20 2a 2f 0a 20 20 2f 2a 20 54 6f 6b  sor. */.  /* Tok
34a85 65 6e 69 7a 65 72 20 69 6d 70 6c 65 6d 65 6e 74  enizer implement
34a86 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69  ations will typi
34a87 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69  cally add additi
34a88 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d  onal fields */.}
34a89 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 46 54  ;..#endif /* _FT
34a8a 53 33 5f 54 4f 4b 45 4e 49 5a 45 52 5f 48 5f 20  S3_TOKENIZER_H_ 
34a8b 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
34a8c 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f  *** End of fts3_
34a8d 74 6f 6b 65 6e 69 7a 65 72 2e 68 20 2a 2a 2a 2a  tokenizer.h ****
34a8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34a8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34a90 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
34a91 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
34a92 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
34a93 20 69 6e 20 66 74 73 33 5f 65 78 70 72 2e 68 20   in fts3_expr.h 
34a94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34a95 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  **/../*.** The f
34a96 6f 6c 6c 6f 77 69 6e 67 20 64 65 73 63 72 69 62  ollowing describ
34a97 65 73 20 74 68 65 20 73 79 6e 74 61 78 20 73 75  es the syntax su
34a98 70 70 6f 72 74 65 64 20 62 79 20 74 68 65 20 66  pported by the f
34a99 74 73 33 20 4d 41 54 43 48 0a 2a 2a 20 6f 70 65  ts3 MATCH.** ope
34a9a 72 61 74 6f 72 20 69 6e 20 61 20 73 69 6d 69 6c  rator in a simil
34a9b 61 72 20 66 6f 72 6d 61 74 20 74 6f 20 74 68 61  ar format to tha
34a9c 74 20 75 73 65 64 20 62 79 20 74 68 65 20 6c 65  t used by the le
34a9d 6d 6f 6e 20 70 61 72 73 65 72 0a 2a 2a 20 67 65  mon parser.** ge
34a9e 6e 65 72 61 74 6f 72 2e 20 54 68 69 73 20 6d 6f  nerator. This mo
34a9f 64 75 6c 65 20 64 6f 65 73 20 6e 6f 74 20 75 73  dule does not us
34aa0 65 20 61 63 74 75 61 6c 6c 79 20 6c 65 6d 6f 6e  e actually lemon
34aa1 2c 20 69 74 20 75 73 65 73 20 61 0a 2a 2a 20 63  , it uses a.** c
34aa2 75 73 74 6f 6d 20 70 61 72 73 65 72 2e 0a 2a 2a  ustom parser..**
34aa3 0a 2a 2a 20 20 20 71 75 65 72 79 20 3a 3a 3d 20  .**   query ::= 
34aa4 61 6e 64 65 78 70 72 20 28 4f 52 20 61 6e 64 65  andexpr (OR ande
34aa5 78 70 72 29 2a 2e 0a 2a 2a 0a 2a 2a 20 20 20 61  xpr)*..**.**   a
34aa6 6e 64 65 78 70 72 20 3a 3a 3d 20 6e 6f 74 65 78  ndexpr ::= notex
34aa7 70 72 20 28 41 4e 44 3f 20 6e 6f 74 65 78 70 72  pr (AND? notexpr
34aa8 29 2a 2e 0a 2a 2a 0a 2a 2a 20 20 20 6e 6f 74 65  )*..**.**   note
34aa9 78 70 72 20 3a 3a 3d 20 6e 65 61 72 65 78 70 72  xpr ::= nearexpr
34aaa 20 28 4e 4f 54 20 6e 65 61 72 65 78 70 72 7c 2d   (NOT nearexpr|-
34aab 54 4f 4b 45 4e 29 2a 2e 0a 2a 2a 20 20 20 6e 6f  TOKEN)*..**   no
34aac 74 65 78 70 72 20 3a 3a 3d 20 4c 50 20 71 75 65  texpr ::= LP que
34aad 72 79 20 52 50 2e 0a 2a 2a 0a 2a 2a 20 20 20 6e  ry RP..**.**   n
34aae 65 61 72 65 78 70 72 20 3a 3a 3d 20 70 68 72 61  earexpr ::= phra
34aaf 73 65 20 28 4e 45 41 52 20 64 69 73 74 61 6e 63  se (NEAR distanc
34ab0 65 5f 6f 70 74 20 6e 65 61 72 65 78 70 72 29 2a  e_opt nearexpr)*
34ab1 2e 0a 2a 2a 0a 2a 2a 20 20 20 64 69 73 74 61 6e  ..**.**   distan
34ab2 63 65 5f 6f 70 74 20 3a 3a 3d 20 2e 0a 2a 2a 20  ce_opt ::= ..** 
34ab3 20 20 64 69 73 74 61 6e 63 65 5f 6f 70 74 20 3a    distance_opt :
34ab4 3a 3d 20 2f 20 49 4e 54 45 47 45 52 2e 0a 2a 2a  := / INTEGER..**
34ab5 0a 2a 2a 20 20 20 70 68 72 61 73 65 20 3a 3a 3d  .**   phrase ::=
34ab6 20 54 4f 4b 45 4e 2e 0a 2a 2a 20 20 20 70 68 72   TOKEN..**   phr
34ab7 61 73 65 20 3a 3a 3d 20 43 4f 4c 55 4d 4e 3a 54  ase ::= COLUMN:T
34ab8 4f 4b 45 4e 2e 0a 2a 2a 20 20 20 70 68 72 61 73  OKEN..**   phras
34ab9 65 20 3a 3a 3d 20 22 54 4f 4b 45 4e 20 54 4f 4b  e ::= "TOKEN TOK
34aba 45 4e 20 54 4f 4b 45 4e 2e 2e 2e 22 2e 0a 2a 2f  EN TOKEN..."..*/
34abb 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
34abc 20 46 74 73 33 45 78 70 72 20 46 74 73 33 45 78   Fts3Expr Fts3Ex
34abd 70 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  pr;.typedef stru
34abe 63 74 20 46 74 73 33 50 68 72 61 73 65 20 46 74  ct Fts3Phrase Ft
34abf 73 33 50 68 72 61 73 65 3b 0a 0a 2f 2a 0a 2a 2a  s3Phrase;../*.**
34ac0 20 41 20 22 70 68 72 61 73 65 22 20 69 73 20 61   A "phrase" is a
34ac1 20 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 6e 65   sequence of one
34ac2 20 6f 72 20 6d 6f 72 65 20 74 6f 6b 65 6e 73 20   or more tokens 
34ac3 74 68 61 74 20 6d 75 73 74 20 6d 61 74 63 68 20  that must match 
34ac4 69 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 2e 20  in.** sequence. 
34ac5 20 41 20 73 69 6e 67 6c 65 20 74 6f 6b 65 6e 20   A single token 
34ac6 69 73 20 74 68 65 20 62 61 73 65 20 63 61 73 65  is the base case
34ac7 20 61 6e 64 20 74 68 65 20 6d 6f 73 74 20 63 6f   and the most co
34ac8 6d 6d 6f 6e 20 63 61 73 65 2e 0a 2a 2a 20 46 6f  mmon case..** Fo
34ac9 72 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20  r a sequence of 
34aca 74 6f 6b 65 6e 73 20 63 6f 6e 74 61 69 6e 65 64  tokens contained
34acb 20 69 6e 20 22 2e 2e 2e 22 2c 20 6e 54 6f 6b 65   in "...", nToke
34acc 6e 20 77 69 6c 6c 20 62 65 20 74 68 65 20 6e 75  n will be the nu
34acd 6d 62 65 72 0a 2a 2a 20 6f 66 20 74 6f 6b 65 6e  mber.** of token
34ace 73 20 69 6e 20 74 68 65 20 73 74 72 69 6e 67 2e  s in the string.
34acf 0a 2a 2f 0a 73 74 72 75 63 74 20 46 74 73 33 50  .*/.struct Fts3P
34ad0 68 72 61 73 65 20 7b 0a 20 20 69 6e 74 20 6e 54  hrase {.  int nT
34ad1 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 2f  oken;          /
34ad2 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65  * Number of toke
34ad3 6e 73 20 69 6e 20 74 68 65 20 70 68 72 61 73 65  ns in the phrase
34ad4 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d   */.  int iColum
34ad5 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  n;         /* In
34ad6 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 74 68  dex of column th
34ad7 69 73 20 70 68 72 61 73 65 20 6d 75 73 74 20 6d  is phrase must m
34ad8 61 74 63 68 20 2a 2f 0a 20 20 69 6e 74 20 69 73  atch */.  int is
34ad9 4e 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Not;           /
34ada 2a 20 50 68 72 61 73 65 20 70 72 65 66 69 78 65  * Phrase prefixe
34adb 64 20 62 79 20 75 6e 61 72 79 20 6e 6f 74 20 28  d by unary not (
34adc 2d 29 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20  -) operator */. 
34add 20 73 74 72 75 63 74 20 50 68 72 61 73 65 54 6f   struct PhraseTo
34ade 6b 65 6e 20 7b 0a 20 20 20 20 63 68 61 72 20 2a  ken {.    char *
34adf 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  z;              
34ae0 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 74  /* Text of the t
34ae1 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20  oken */.    int 
34ae2 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
34ae3 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
34ae4 79 74 65 73 20 69 6e 20 62 75 66 66 65 72 20 70  ytes in buffer p
34ae5 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 20 2a  ointed to by z *
34ae6 2f 0a 20 20 20 20 69 6e 74 20 69 73 50 72 65 66  /.    int isPref
34ae7 69 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ix;         /* T
34ae8 72 75 65 20 69 66 20 74 6f 6b 65 6e 20 65 6e 64  rue if token end
34ae9 73 20 69 6e 20 77 69 74 68 20 61 20 22 2a 22 20  s in with a "*" 
34aea 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 7d  character */.  }
34aeb 20 61 54 6f 6b 65 6e 5b 31 5d 3b 20 20 20 20 20   aToken[1];     
34aec 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79      /* One entry
34aed 20 66 6f 72 20 65 61 63 68 20 74 6f 6b 65 6e 20   for each token 
34aee 69 6e 20 74 68 65 20 70 68 72 61 73 65 20 2a 2f  in the phrase */
34aef 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 72 65  .};../*.** A tre
34af0 65 20 6f 66 20 74 68 65 73 65 20 6f 62 6a 65 63  e of these objec
34af1 74 73 20 66 6f 72 6d 73 20 74 68 65 20 52 48 53  ts forms the RHS
34af2 20 6f 66 20 61 20 4d 41 54 43 48 20 6f 70 65 72   of a MATCH oper
34af3 61 74 6f 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ator..*/.struct 
34af4 46 74 73 33 45 78 70 72 20 7b 0a 20 20 69 6e 74  Fts3Expr {.  int
34af5 20 65 54 79 70 65 3b 20 20 20 20 20 20 20 20 20   eType;         
34af6 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
34af7 66 20 74 68 65 20 46 54 53 51 55 45 52 59 5f 58  f the FTSQUERY_X
34af8 58 58 20 76 61 6c 75 65 73 20 64 65 66 69 6e 65  XX values define
34af9 64 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74  d below */.  int
34afa 20 6e 4e 65 61 72 3b 20 20 20 20 20 20 20 20 20   nNear;         
34afb 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 69 64          /* Valid
34afc 20 69 66 20 65 54 79 70 65 3d 3d 46 54 53 51 55   if eType==FTSQU
34afd 45 52 59 5f 4e 45 41 52 20 2a 2f 0a 20 20 46 74  ERY_NEAR */.  Ft
34afe 73 33 45 78 70 72 20 2a 70 50 61 72 65 6e 74 3b  s3Expr *pParent;
34aff 20 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 72           /* pPar
34b00 65 6e 74 2d 3e 70 4c 65 66 74 3d 3d 74 68 69 73  ent->pLeft==this
34b01 20 6f 72 20 70 50 61 72 65 6e 74 2d 3e 70 52 69   or pParent->pRi
34b02 67 68 74 3d 3d 74 68 69 73 20 2a 2f 0a 20 20 46  ght==this */.  F
34b03 74 73 33 45 78 70 72 20 2a 70 4c 65 66 74 3b 20  ts3Expr *pLeft; 
34b04 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66            /* Lef
34b05 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 46  t operand */.  F
34b06 74 73 33 45 78 70 72 20 2a 70 52 69 67 68 74 3b  ts3Expr *pRight;
34b07 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67            /* Rig
34b08 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  ht operand */.  
34b09 46 74 73 33 50 68 72 61 73 65 20 2a 70 50 68 72  Fts3Phrase *pPhr
34b0a 61 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 56 61  ase;       /* Va
34b0b 6c 69 64 20 69 66 20 65 54 79 70 65 3d 3d 46 54  lid if eType==FT
34b0c 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 2a 2f  SQUERY_PHRASE */
34b0d 0a 7d 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  .};..SQLITE_PRIV
34b0e 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
34b0f 74 73 33 45 78 70 72 50 61 72 73 65 28 73 71 6c  ts3ExprParse(sql
34b10 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
34b11 2c 20 63 68 61 72 20 2a 2a 2c 20 69 6e 74 2c 20  , char **, int, 
34b12 69 6e 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20  int, .          
34b13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
34b14 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74  onst char *, int
34b15 2c 20 46 74 73 33 45 78 70 72 20 2a 2a 29 3b 0a  , Fts3Expr **);.
34b16 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
34b17 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 45  oid sqlite3Fts3E
34b18 78 70 72 46 72 65 65 28 46 74 73 33 45 78 70 72  xprFree(Fts3Expr
34b19 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 61 6e 64   *);../*.** Cand
34b1a 69 64 61 74 65 20 76 61 6c 75 65 73 20 66 6f 72  idate values for
34b1b 20 46 74 73 33 51 75 65 72 79 2e 65 54 79 70 65   Fts3Query.eType
34b1c 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20  . Note that the 
34b1d 6f 72 64 65 72 20 6f 66 20 74 68 65 20 66 69 72  order of the fir
34b1e 73 74 0a 2a 2a 20 66 6f 75 72 20 76 61 6c 75 65  st.** four value
34b1f 73 20 69 73 20 69 6e 20 6f 72 64 65 72 20 6f 66  s is in order of
34b20 20 70 72 65 63 65 64 65 6e 63 65 20 77 68 65 6e   precedence when
34b21 20 70 61 72 73 69 6e 67 20 65 78 70 72 65 73 73   parsing express
34b22 69 6f 6e 73 2e 20 46 6f 72 20 0a 2a 2a 20 65 78  ions. For .** ex
34b23 61 6d 70 6c 65 2c 20 74 68 65 20 66 6f 6c 6c 6f  ample, the follo
34b24 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 61  wing:.**.**   "a
34b25 20 4f 52 20 62 20 41 4e 44 20 63 20 4e 4f 54 20   OR b AND c NOT 
34b26 64 20 4e 45 41 52 20 65 22 0a 2a 2a 0a 2a 2a 20  d NEAR e".**.** 
34b27 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f  is equivalent to
34b28 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 61 20 4f 52 20  :.**.**   "a OR 
34b29 28 62 20 41 4e 44 20 28 63 20 4e 4f 54 20 28 64  (b AND (c NOT (d
34b2a 20 4e 45 41 52 20 65 29 29 29 22 0a 2a 2f 0a 23   NEAR e)))".*/.#
34b2b 64 65 66 69 6e 65 20 46 54 53 51 55 45 52 59 5f  define FTSQUERY_
34b2c 4e 45 41 52 20 20 20 31 0a 23 64 65 66 69 6e 65  NEAR   1.#define
34b2d 20 46 54 53 51 55 45 52 59 5f 4e 4f 54 20 20 20   FTSQUERY_NOT   
34b2e 20 32 0a 23 64 65 66 69 6e 65 20 46 54 53 51 55   2.#define FTSQU
34b2f 45 52 59 5f 41 4e 44 20 20 20 20 33 0a 23 64 65  ERY_AND    3.#de
34b30 66 69 6e 65 20 46 54 53 51 55 45 52 59 5f 4f 52  fine FTSQUERY_OR
34b31 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 46       4.#define F
34b32 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 35  TSQUERY_PHRASE 5
34b33 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
34b34 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56  TEST.SQLITE_PRIV
34b35 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
34b36 46 74 73 33 45 78 70 72 49 6e 69 74 54 65 73 74  Fts3ExprInitTest
34b37 49 6e 74 65 72 66 61 63 65 28 73 71 6c 69 74 65  Interface(sqlite
34b38 33 20 2a 64 62 29 3b 0a 23 65 6e 64 69 66 0a 0a  3 *db);.#endif..
34b39 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
34b3a 45 6e 64 20 6f 66 20 66 74 73 33 5f 65 78 70 72  End of fts3_expr
34b3b 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
34b3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
34b3e 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
34b3f 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
34b40 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
34b41 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  fts3.c *********
34b42 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
34b43 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
34b44 49 6e 63 6c 75 64 65 20 66 74 73 33 5f 68 61 73  Include fts3_has
34b45 68 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  h.h in the middl
34b46 65 20 6f 66 20 66 74 73 33 2e 63 20 2a 2a 2a 2a  e of fts3.c ****
34b47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
34b48 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
34b49 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f  Begin file fts3_
34b4a 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  hash.h *********
34b4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
34b4d 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
34b4e 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68  mber 22.**.** Th
34b4f 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
34b50 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
34b51 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
34b52 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
34b53 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
34b54 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
34b55 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
34b56 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
34b57 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
34b58 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
34b59 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
34b5a 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
34b5b 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
34b5c 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
34b5d 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
34b5e 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
34b5f 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
34b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b64 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ******.** This i
34b65 73 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c  s the header fil
34b66 65 20 66 6f 72 20 74 68 65 20 67 65 6e 65 72 69  e for the generi
34b67 63 20 68 61 73 68 2d 74 61 62 6c 65 20 69 6d 70  c hash-table imp
34b68 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a 2a 20 75 73  lemenation.** us
34b69 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 20 20 57  ed in SQLite.  W
34b6a 65 27 76 65 20 6d 6f 64 69 66 69 65 64 20 69 74  e've modified it
34b6b 20 73 6c 69 67 68 74 6c 79 20 74 6f 20 73 65 72   slightly to ser
34b6c 76 65 20 61 73 20 61 20 73 74 61 6e 64 61 6c 6f  ve as a standalo
34b6d 6e 65 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65  ne.** hash table
34b6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
34b6f 66 6f 72 20 74 68 65 20 66 75 6c 6c 2d 74 65 78  for the full-tex
34b70 74 20 69 6e 64 65 78 69 6e 67 20 6d 6f 64 75 6c  t indexing modul
34b71 65 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 6e 64 65 66  e..**.*/.#ifndef
34b72 20 5f 46 54 53 33 5f 48 41 53 48 5f 48 5f 0a 23   _FTS3_HASH_H_.#
34b73 64 65 66 69 6e 65 20 5f 46 54 53 33 5f 48 41 53  define _FTS3_HAS
34b74 48 5f 48 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  H_H_../* Forward
34b75 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66   declarations of
34b76 20 73 74 72 75 63 74 75 72 65 73 2e 20 2a 2f 0a   structures. */.
34b77 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 66  typedef struct f
34b78 74 73 33 48 61 73 68 20 66 74 73 33 48 61 73 68  ts3Hash fts3Hash
34b79 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
34b7a 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 66 74   fts3HashElem ft
34b7b 73 33 48 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20  s3HashElem;../* 
34b7c 41 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20  A complete hash 
34b7d 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74  table is an inst
34b7e 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
34b7f 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e  owing structure.
34b80 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 6e 61 6c  .** The internal
34b81 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  s of this struct
34b82 75 72 65 20 61 72 65 20 69 6e 74 65 6e 64 65 64  ure are intended
34b83 20 74 6f 20 62 65 20 6f 70 61 71 75 65 20 2d 2d   to be opaque --
34b84 20 63 6c 69 65 6e 74 0a 2a 2a 20 63 6f 64 65 20   client.** code 
34b85 73 68 6f 75 6c 64 20 6e 6f 74 20 61 74 74 65 6d  should not attem
34b86 70 74 20 74 6f 20 61 63 63 65 73 73 20 6f 72 20  pt to access or 
34b87 6d 6f 64 69 66 79 20 74 68 65 20 66 69 65 6c 64  modify the field
34b88 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  s of this struct
34b89 75 72 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2e  ure.** directly.
34b8a 20 20 43 68 61 6e 67 65 20 74 68 69 73 20 73 74    Change this st
34b8b 72 75 63 74 75 72 65 20 6f 6e 6c 79 20 62 79 20  ructure only by 
34b8c 75 73 69 6e 67 20 74 68 65 20 72 6f 75 74 69 6e  using the routin
34b8d 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77  es below..** How
34b8e 65 76 65 72 2c 20 6d 61 6e 79 20 6f 66 20 74 68  ever, many of th
34b8f 65 20 22 70 72 6f 63 65 64 75 72 65 73 22 20 61  e "procedures" a
34b90 6e 64 20 22 66 75 6e 63 74 69 6f 6e 73 22 20 66  nd "functions" f
34b91 6f 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 6e 64  or modifying and
34b92 0a 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74 68  .** accessing th
34b93 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65  is structure are
34b94 20 72 65 61 6c 6c 79 20 6d 61 63 72 6f 73 2c 20   really macros, 
34b95 73 6f 20 77 65 20 63 61 6e 27 74 20 72 65 61 6c  so we can't real
34b96 6c 79 20 6d 61 6b 65 0a 2a 2a 20 74 68 69 73 20  ly make.** this 
34b97 73 74 72 75 63 74 75 72 65 20 6f 70 61 71 75 65  structure opaque
34b98 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 66 74 73 33  ..*/.struct fts3
34b99 48 61 73 68 20 7b 0a 20 20 63 68 61 72 20 6b 65  Hash {.  char ke
34b9a 79 43 6c 61 73 73 3b 20 20 20 20 20 20 20 20 20  yClass;         
34b9b 20 2f 2a 20 48 41 53 48 5f 49 4e 54 2c 20 5f 50   /* HASH_INT, _P
34b9c 4f 49 4e 54 45 52 2c 20 5f 53 54 52 49 4e 47 2c  OINTER, _STRING,
34b9d 20 5f 42 49 4e 41 52 59 20 2a 2f 0a 20 20 63 68   _BINARY */.  ch
34b9e 61 72 20 63 6f 70 79 4b 65 79 3b 20 20 20 20 20  ar copyKey;     
34b9f 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
34ba0 20 63 6f 70 79 20 6f 66 20 6b 65 79 20 6d 61 64   copy of key mad
34ba1 65 20 6f 6e 20 69 6e 73 65 72 74 20 2a 2f 0a 20  e on insert */. 
34ba2 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20   int count;     
34ba3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
34ba4 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
34ba5 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20   this table */. 
34ba6 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 66   fts3HashElem *f
34ba7 69 72 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 20  irst;    /* The 
34ba8 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66  first element of
34ba9 20 74 68 65 20 61 72 72 61 79 20 2a 2f 0a 20 20   the array */.  
34baa 69 6e 74 20 68 74 73 69 7a 65 3b 20 20 20 20 20  int htsize;     
34bab 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
34bac 72 20 6f 66 20 62 75 63 6b 65 74 73 20 69 6e 20  r of buckets in 
34bad 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a  the hash table *
34bae 2f 0a 20 20 73 74 72 75 63 74 20 5f 66 74 73 33  /.  struct _fts3
34baf 68 74 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 74  ht {        /* t
34bb0 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f  he hash table */
34bb1 0a 20 20 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20  .    int count; 
34bb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
34bb3 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
34bb4 65 73 20 77 69 74 68 20 74 68 69 73 20 68 61 73  es with this has
34bb5 68 20 2a 2f 0a 20 20 20 20 66 74 73 33 48 61 73  h */.    fts3Has
34bb6 68 45 6c 65 6d 20 2a 63 68 61 69 6e 3b 20 20 20  hElem *chain;   
34bb7 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
34bb8 66 69 72 73 74 20 65 6e 74 72 79 20 77 69 74 68  first entry with
34bb9 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 20   this hash */.  
34bba 7d 20 2a 68 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45 61  } *ht;.};../* Ea
34bbb 63 68 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68  ch element in th
34bbc 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20  e hash table is 
34bbd 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
34bbe 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a  he following .**
34bbf 20 73 74 72 75 63 74 75 72 65 2e 20 20 41 6c 6c   structure.  All
34bc0 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20 73 74   elements are st
34bc1 6f 72 65 64 20 6f 6e 20 61 20 73 69 6e 67 6c 65  ored on a single
34bc2 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c   doubly-linked l
34bc3 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 67 61 69 6e  ist..**.** Again
34bc4 2c 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  , this structure
34bc5 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20   is intended to 
34bc6 62 65 20 6f 70 61 71 75 65 2c 20 62 75 74 20 69  be opaque, but i
34bc7 74 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 0a 2a  t can't really.*
34bc8 2a 20 62 65 20 6f 70 61 71 75 65 20 62 65 63 61  * be opaque beca
34bc9 75 73 65 20 69 74 20 69 73 20 75 73 65 64 20 62  use it is used b
34bca 79 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 73 74 72  y macros..*/.str
34bcb 75 63 74 20 66 74 73 33 48 61 73 68 45 6c 65 6d  uct fts3HashElem
34bcc 20 7b 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65   {.  fts3HashEle
34bcd 6d 20 2a 6e 65 78 74 2c 20 2a 70 72 65 76 3b 20  m *next, *prev; 
34bce 2f 2a 20 4e 65 78 74 20 61 6e 64 20 70 72 65 76  /* Next and prev
34bcf 69 6f 75 73 20 65 6c 65 6d 65 6e 74 73 20 69 6e  ious elements in
34bd0 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
34bd1 76 6f 69 64 20 2a 64 61 74 61 3b 20 20 20 20 20  void *data;     
34bd2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
34bd3 74 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ta associated wi
34bd4 74 68 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20  th this element 
34bd5 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4b 65 79 3b  */.  void *pKey;
34bd6 20 69 6e 74 20 6e 4b 65 79 3b 20 20 20 20 20 20   int nKey;      
34bd7 2f 2a 20 4b 65 79 20 61 73 73 6f 63 69 61 74 65  /* Key associate
34bd8 64 20 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d  d with this elem
34bd9 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ent */.};../*.**
34bda 20 54 68 65 72 65 20 61 72 65 20 32 20 64 69 66   There are 2 dif
34bdb 66 65 72 65 6e 74 20 6d 6f 64 65 73 20 6f 66 20  ferent modes of 
34bdc 6f 70 65 72 61 74 69 6f 6e 20 66 6f 72 20 61 20  operation for a 
34bdd 68 61 73 68 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a  hash table:.**.*
34bde 2a 20 20 20 46 54 53 33 5f 48 41 53 48 5f 53 54  *   FTS3_HASH_ST
34bdf 52 49 4e 47 20 20 20 20 20 20 20 20 70 4b 65 79  RING        pKey
34be0 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73 74 72   points to a str
34be1 69 6e 67 20 74 68 61 74 20 69 73 20 6e 4b 65 79  ing that is nKey
34be2 20 62 79 74 65 73 20 6c 6f 6e 67 0a 2a 2a 20 20   bytes long.**  
34be3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34be4 20 20 20 20 20 20 20 20 20 28 69 6e 63 6c 75 64           (includ
34be5 69 6e 67 20 74 68 65 20 6e 75 6c 6c 2d 74 65 72  ing the null-ter
34be6 6d 69 6e 61 74 6f 72 2c 20 69 66 20 61 6e 79 29  minator, if any)
34be7 2e 20 20 43 61 73 65 0a 2a 2a 20 20 20 20 20 20  .  Case.**      
34be8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34be9 20 20 20 20 20 69 73 20 72 65 73 70 65 63 74 65       is respecte
34bea 64 20 69 6e 20 63 6f 6d 70 61 72 69 73 6f 6e 73  d in comparisons
34beb 2e 0a 2a 2a 0a 2a 2a 20 20 20 46 54 53 33 5f 48  ..**.**   FTS3_H
34bec 41 53 48 5f 42 49 4e 41 52 59 20 20 20 20 20 20  ASH_BINARY      
34bed 20 20 70 4b 65 79 20 70 6f 69 6e 74 73 20 74 6f    pKey points to
34bee 20 62 69 6e 61 72 79 20 64 61 74 61 20 6e 4b 65   binary data nKe
34bef 79 20 62 79 74 65 73 20 6c 6f 6e 67 2e 20 0a 2a  y bytes long. .*
34bf0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
34bf1 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63              memc
34bf2 6d 70 28 29 20 69 73 20 75 73 65 64 20 74 6f 20  mp() is used to 
34bf3 63 6f 6d 70 61 72 65 20 6b 65 79 73 2e 0a 2a 2a  compare keys..**
34bf4 0a 2a 2a 20 41 20 63 6f 70 79 20 6f 66 20 74 68  .** A copy of th
34bf5 65 20 6b 65 79 20 69 73 20 6d 61 64 65 20 69 66  e key is made if
34bf6 20 74 68 65 20 63 6f 70 79 4b 65 79 20 70 61 72   the copyKey par
34bf7 61 6d 65 74 65 72 20 74 6f 20 66 74 73 33 48 61  ameter to fts3Ha
34bf8 73 68 49 6e 69 74 20 69 73 20 31 2e 20 20 0a 2a  shInit is 1.  .*
34bf9 2f 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 48  /.#define FTS3_H
34bfa 41 53 48 5f 53 54 52 49 4e 47 20 20 20 20 31 0a  ASH_STRING    1.
34bfb 23 64 65 66 69 6e 65 20 46 54 53 33 5f 48 41 53  #define FTS3_HAS
34bfc 48 5f 42 49 4e 41 52 59 20 20 20 20 32 0a 0a 2f  H_BINARY    2../
34bfd 2a 0a 2a 2a 20 41 63 63 65 73 73 20 72 6f 75 74  *.** Access rout
34bfe 69 6e 65 73 2e 20 20 54 6f 20 64 65 6c 65 74 65  ines.  To delete
34bff 2c 20 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20  , insert a NULL 
34c00 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49  pointer..*/.SQLI
34c01 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
34c02 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49  sqlite3Fts3HashI
34c03 6e 69 74 28 66 74 73 33 48 61 73 68 2a 2c 20 69  nit(fts3Hash*, i
34c04 6e 74 20 6b 65 79 74 79 70 65 2c 20 69 6e 74 20  nt keytype, int 
34c05 63 6f 70 79 4b 65 79 29 3b 0a 53 51 4c 49 54 45  copyKey);.SQLITE
34c06 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
34c07 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e  qlite3Fts3HashIn
34c08 73 65 72 74 28 66 74 73 33 48 61 73 68 2a 2c 20  sert(fts3Hash*, 
34c09 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
34c0a 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64  , int nKey, void
34c0b 20 2a 70 44 61 74 61 29 3b 0a 53 51 4c 49 54 45   *pData);.SQLITE
34c0c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
34c0d 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 46 69  qlite3Fts3HashFi
34c0e 6e 64 28 63 6f 6e 73 74 20 66 74 73 33 48 61 73  nd(const fts3Has
34c0f 68 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  h*, const void *
34c10 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b  pKey, int nKey);
34c11 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
34c12 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
34c13 48 61 73 68 43 6c 65 61 72 28 66 74 73 33 48 61  HashClear(fts3Ha
34c14 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 68 6f  sh*);../*.** Sho
34c15 72 74 68 61 6e 64 20 66 6f 72 20 74 68 65 20 66  rthand for the f
34c16 75 6e 63 74 69 6f 6e 73 20 61 62 6f 76 65 0a 2a  unctions above.*
34c17 2f 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61  /.#define fts3Ha
34c18 73 68 49 6e 69 74 20 20 20 73 71 6c 69 74 65 33  shInit   sqlite3
34c19 46 74 73 33 48 61 73 68 49 6e 69 74 0a 23 64 65  Fts3HashInit.#de
34c1a 66 69 6e 65 20 66 74 73 33 48 61 73 68 49 6e 73  fine fts3HashIns
34c1b 65 72 74 20 73 71 6c 69 74 65 33 46 74 73 33 48  ert sqlite3Fts3H
34c1c 61 73 68 49 6e 73 65 72 74 0a 23 64 65 66 69 6e  ashInsert.#defin
34c1d 65 20 66 74 73 33 48 61 73 68 46 69 6e 64 20 20  e fts3HashFind  
34c1e 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
34c1f 46 69 6e 64 0a 23 64 65 66 69 6e 65 20 66 74 73  Find.#define fts
34c20 33 48 61 73 68 43 6c 65 61 72 20 20 73 71 6c 69  3HashClear  sqli
34c21 74 65 33 46 74 73 33 48 61 73 68 43 6c 65 61 72  te3Fts3HashClear
34c22 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66  ../*.** Macros f
34c23 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
34c24 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20  all elements of 
34c25 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54  a hash table.  T
34c26 68 65 20 69 64 69 6f 6d 20 69 73 0a 2a 2a 20 6c  he idiom is.** l
34c27 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
34c28 20 20 66 74 73 33 48 61 73 68 20 68 3b 0a 2a 2a    fts3Hash h;.**
34c29 20 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20     fts3HashElem 
34c2a 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a 2a 20  *p;.**   ....** 
34c2b 20 20 66 6f 72 28 70 3d 66 74 73 33 48 61 73 68    for(p=fts3Hash
34c2c 46 69 72 73 74 28 26 68 29 3b 20 70 3b 20 70 3d  First(&h); p; p=
34c2d 66 74 73 33 48 61 73 68 4e 65 78 74 28 70 29 29  fts3HashNext(p))
34c2e 7b 0a 2a 2a 20 20 20 20 20 53 6f 6d 65 53 74 72  {.**     SomeStr
34c2f 75 63 74 75 72 65 20 2a 70 44 61 74 61 20 3d 20  ucture *pData = 
34c30 66 74 73 33 48 61 73 68 44 61 74 61 28 70 29 3b  fts3HashData(p);
34c31 0a 2a 2a 20 20 20 20 20 2f 2f 20 64 6f 20 73 6f  .**     // do so
34c32 6d 65 74 68 69 6e 67 20 77 69 74 68 20 70 44 61  mething with pDa
34c33 74 61 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a 23 64 65  ta.**   }.*/.#de
34c34 66 69 6e 65 20 66 74 73 33 48 61 73 68 46 69 72  fine fts3HashFir
34c35 73 74 28 48 29 20 20 28 28 48 29 2d 3e 66 69 72  st(H)  ((H)->fir
34c36 73 74 29 0a 23 64 65 66 69 6e 65 20 66 74 73 33  st).#define fts3
34c37 48 61 73 68 4e 65 78 74 28 45 29 20 20 20 28 28  HashNext(E)   ((
34c38 45 29 2d 3e 6e 65 78 74 29 0a 23 64 65 66 69 6e  E)->next).#defin
34c39 65 20 66 74 73 33 48 61 73 68 44 61 74 61 28 45  e fts3HashData(E
34c3a 29 20 20 20 28 28 45 29 2d 3e 64 61 74 61 29 0a  )   ((E)->data).
34c3b 23 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68  #define fts3Hash
34c3c 4b 65 79 28 45 29 20 20 20 20 28 28 45 29 2d 3e  Key(E)    ((E)->
34c3d 70 4b 65 79 29 0a 23 64 65 66 69 6e 65 20 66 74  pKey).#define ft
34c3e 73 33 48 61 73 68 4b 65 79 73 69 7a 65 28 45 29  s3HashKeysize(E)
34c3f 20 28 28 45 29 2d 3e 6e 4b 65 79 29 0a 0a 2f 2a   ((E)->nKey)../*
34c40 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e  .** Number of en
34c41 74 72 69 65 73 20 69 6e 20 61 20 68 61 73 68 20  tries in a hash 
34c42 74 61 62 6c 65 0a 2a 2f 0a 23 64 65 66 69 6e 65  table.*/.#define
34c43 20 66 74 73 33 48 61 73 68 43 6f 75 6e 74 28 48   fts3HashCount(H
34c44 29 20 20 28 28 48 29 2d 3e 63 6f 75 6e 74 29 0a  )  ((H)->count).
34c45 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 46 54 53 33  .#endif /* _FTS3
34c46 5f 48 41 53 48 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a  _HASH_H_ */../**
34c47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
34c48 20 6f 66 20 66 74 73 33 5f 68 61 73 68 2e 68 20   of fts3_hash.h 
34c49 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
34c4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
34c4d 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
34c4e 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 66 74 73   left off in fts
34c4f 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  3.c ************
34c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 66  ***********/.#if
34c51 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45  ndef SQLITE_CORE
34c52 20 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e   .  SQLITE_EXTEN
34c53 53 49 4f 4e 5f 49 4e 49 54 31 0a 23 65 6e 64 69  SION_INIT1.#endi
34c54 66 0a 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  f.../* TODO(shes
34c55 73 29 20 4d 41 4e 2c 20 74 68 69 73 20 74 68 69  s) MAN, this thi
34c56 6e 67 20 6e 65 65 64 73 20 73 6f 6d 65 20 72 65  ng needs some re
34c57 66 61 63 74 6f 72 69 6e 67 2e 20 20 41 74 20 6d  factoring.  At m
34c58 69 6e 69 6d 75 6d 2c 20 69 74 0a 2a 2a 20 77 6f  inimum, it.** wo
34c59 75 6c 64 20 62 65 20 6e 69 63 65 20 74 6f 20 6f  uld be nice to o
34c5a 72 64 65 72 20 74 68 65 20 66 69 6c 65 20 62 65  rder the file be
34c5b 74 74 65 72 2c 20 70 65 72 68 61 70 73 20 73 6f  tter, perhaps so
34c5c 6d 65 74 68 69 6e 67 20 61 6c 6f 6e 67 20 74 68  mething along th
34c5d 65 0a 2a 2a 20 6c 69 6e 65 73 20 6f 66 3a 0a 2a  e.** lines of:.*
34c5e 2a 0a 2a 2a 20 20 2d 20 75 74 69 6c 69 74 79 20  *.**  - utility 
34c5f 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 2d 20  functions.**  - 
34c60 74 61 62 6c 65 20 73 65 74 75 70 20 66 75 6e 63  table setup func
34c61 74 69 6f 6e 73 0a 2a 2a 20 20 2d 20 74 61 62 6c  tions.**  - tabl
34c62 65 20 75 70 64 61 74 65 20 66 75 6e 63 74 69 6f  e update functio
34c63 6e 73 0a 2a 2a 20 20 2d 20 74 61 62 6c 65 20 71  ns.**  - table q
34c64 75 65 72 79 20 66 75 6e 63 74 69 6f 6e 73 0a 2a  uery functions.*
34c65 2a 0a 2a 2a 20 50 75 74 20 74 68 65 20 71 75 65  *.** Put the que
34c66 72 79 20 66 75 6e 63 74 69 6f 6e 73 20 6c 61 73  ry functions las
34c67 74 20 62 65 63 61 75 73 65 20 74 68 65 79 27 72  t because they'r
34c68 65 20 6c 69 6b 65 6c 79 20 74 6f 20 72 65 66 65  e likely to refe
34c69 72 65 6e 63 65 0a 2a 2a 20 74 79 70 65 64 65 66  rence.** typedef
34c6a 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e 73 20 66  s or functions f
34c6b 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 20 75 70  rom the table up
34c6c 64 61 74 65 20 73 65 63 74 69 6f 6e 2e 0a 2a 2f  date section..*/
34c6d 0a 0a 23 69 66 20 30 0a 23 20 64 65 66 69 6e 65  ..#if 0.# define
34c6e 20 46 54 53 54 52 41 43 45 28 41 29 20 20 70 72   FTSTRACE(A)  pr
34c6f 69 6e 74 66 20 41 3b 20 66 66 6c 75 73 68 28 73  intf A; fflush(s
34c70 74 64 6f 75 74 29 0a 23 65 6c 73 65 0a 23 20 64  tdout).#else.# d
34c71 65 66 69 6e 65 20 46 54 53 54 52 41 43 45 28 41  efine FTSTRACE(A
34c72 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 49 74 20  ).#endif../* It 
34c73 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 63  is not safe to c
34c74 61 6c 6c 20 69 73 73 70 61 63 65 28 29 2c 20 74  all isspace(), t
34c75 6f 6c 6f 77 65 72 28 29 2c 20 6f 72 20 69 73 61  olower(), or isa
34c76 6c 6e 75 6d 28 29 20 6f 6e 0a 2a 2a 20 68 69 2d  lnum() on.** hi-
34c77 62 69 74 2d 73 65 74 20 63 68 61 72 61 63 74 65  bit-set characte
34c78 72 73 2e 20 20 54 68 69 73 20 69 73 20 74 68 65  rs.  This is the
34c79 20 73 61 6d 65 20 73 6f 6c 75 74 69 6f 6e 20 75   same solution u
34c7a 73 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 74 6f  sed in the.** to
34c7b 6b 65 6e 69 7a 65 72 2e 0a 2a 2f 0a 2f 2a 20 54  kenizer..*/./* T
34c7c 4f 44 4f 28 73 68 65 73 73 29 20 54 68 65 20 73  ODO(shess) The s
34c7d 6e 69 70 70 65 74 2d 67 65 6e 65 72 61 74 69 6f  nippet-generatio
34c7e 6e 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 62 65  n code should be
34c7f 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 74 6f   using the.** to
34c80 6b 65 6e 69 7a 65 72 2d 67 65 6e 65 72 61 74 65  kenizer-generate
34c81 64 20 74 6f 6b 65 6e 73 20 72 61 74 68 65 72 20  d tokens rather 
34c82 74 68 61 6e 20 64 6f 69 6e 67 20 69 74 73 20 6f  than doing its o
34c83 77 6e 20 6c 6f 63 61 6c 0a 2a 2a 20 74 6f 6b 65  wn local.** toke
34c84 6e 69 7a 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20  nization..*/./* 
34c85 54 4f 44 4f 28 73 68 65 73 73 29 20 49 73 20 5f  TODO(shess) Is _
34c86 5f 69 73 61 73 63 69 69 28 29 20 61 20 70 6f 72  _isascii() a por
34c87 74 61 62 6c 65 20 76 65 72 73 69 6f 6e 20 6f 66  table version of
34c88 20 28 63 26 30 78 38 30 29 3d 3d 30 3f 20 2a 2f   (c&0x80)==0? */
34c89 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 66 65  .static int safe
34c8a 5f 69 73 73 70 61 63 65 28 63 68 61 72 20 63 29  _isspace(char c)
34c8b 7b 0a 20 20 72 65 74 75 72 6e 20 28 63 26 30 78  {.  return (c&0x
34c8c 38 30 29 3d 3d 30 20 3f 20 69 73 73 70 61 63 65  80)==0 ? isspace
34c8d 28 63 29 20 3a 20 30 3b 0a 7d 0a 73 74 61 74 69  (c) : 0;.}.stati
34c8e 63 20 69 6e 74 20 73 61 66 65 5f 74 6f 6c 6f 77  c int safe_tolow
34c8f 65 72 28 63 68 61 72 20 63 29 7b 0a 20 20 72 65  er(char c){.  re
34c90 74 75 72 6e 20 28 63 26 30 78 38 30 29 3d 3d 30  turn (c&0x80)==0
34c91 20 3f 20 74 6f 6c 6f 77 65 72 28 63 29 20 3a 20   ? tolower(c) : 
34c92 63 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  c;.}.static int 
34c93 73 61 66 65 5f 69 73 61 6c 6e 75 6d 28 63 68 61  safe_isalnum(cha
34c94 72 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28  r c){.  return (
34c95 63 26 30 78 38 30 29 3d 3d 30 20 3f 20 69 73 61  c&0x80)==0 ? isa
34c96 6c 6e 75 6d 28 63 29 20 3a 20 30 3b 0a 7d 0a 0a  lnum(c) : 0;.}..
34c97 74 79 70 65 64 65 66 20 65 6e 75 6d 20 44 6f 63  typedef enum Doc
34c98 4c 69 73 74 54 79 70 65 20 7b 0a 20 20 44 4c 5f  ListType {.  DL_
34c99 44 4f 43 49 44 53 2c 20 20 20 20 20 20 20 20 20  DOCIDS,         
34c9a 20 20 20 20 20 2f 2a 20 64 6f 63 69 64 73 20 6f       /* docids o
34c9b 6e 6c 79 20 2a 2f 0a 20 20 44 4c 5f 50 4f 53 49  nly */.  DL_POSI
34c9c 54 49 4f 4e 53 2c 20 20 20 20 20 20 20 20 20 20  TIONS,          
34c9d 20 2f 2a 20 64 6f 63 69 64 73 20 2b 20 70 6f 73   /* docids + pos
34c9e 69 74 69 6f 6e 73 20 2a 2f 0a 20 20 44 4c 5f 50  itions */.  DL_P
34c9f 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53  OSITIONS_OFFSETS
34ca0 20 20 20 20 2f 2a 20 64 6f 63 69 64 73 20 2b 20      /* docids + 
34ca1 70 6f 73 69 74 69 6f 6e 73 20 2b 20 6f 66 66 73  positions + offs
34ca2 65 74 73 20 2a 2f 0a 7d 20 44 6f 63 4c 69 73 74  ets */.} DocList
34ca3 54 79 70 65 3b 0a 0a 2f 2a 0a 2a 2a 20 42 79 20  Type;../*.** By 
34ca4 64 65 66 61 75 6c 74 2c 20 6f 6e 6c 79 20 70 6f  default, only po
34ca5 73 69 74 69 6f 6e 73 20 61 6e 64 20 6e 6f 74 20  sitions and not 
34ca6 6f 66 66 73 65 74 73 20 61 72 65 20 73 74 6f 72  offsets are stor
34ca7 65 64 20 69 6e 20 74 68 65 20 64 6f 63 6c 69 73  ed in the doclis
34ca8 74 73 2e 0a 2a 2a 20 54 6f 20 63 68 61 6e 67 65  ts..** To change
34ca9 20 74 68 69 73 20 73 6f 20 74 68 61 74 20 6f 66   this so that of
34caa 66 73 65 74 73 20 61 72 65 20 73 74 6f 72 65 64  fsets are stored
34cab 20 74 6f 6f 2c 20 63 6f 6d 70 69 6c 65 20 77 69   too, compile wi
34cac 74 68 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  th.**.**        
34cad 20 20 2d 44 44 4c 5f 44 45 46 41 55 4c 54 3d 44    -DDL_DEFAULT=D
34cae 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53  L_POSITIONS_OFFS
34caf 45 54 53 0a 2a 2a 0a 2a 2a 20 49 66 20 44 4c 5f  ETS.**.** If DL_
34cb0 44 45 46 41 55 4c 54 20 69 73 20 73 65 74 20 74  DEFAULT is set t
34cb1 6f 20 44 4c 5f 44 4f 43 49 44 53 2c 20 79 6f 75  o DL_DOCIDS, you
34cb2 72 20 74 61 62 6c 65 20 63 61 6e 20 6f 6e 6c 79  r table can only
34cb3 20 62 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20   be inserted.** 
34cb4 69 6e 74 6f 20 28 6e 6f 20 64 65 6c 65 74 65 73  into (no deletes
34cb5 20 6f 72 20 75 70 64 61 74 65 73 29 2e 0a 2a 2f   or updates)..*/
34cb6 0a 23 69 66 6e 64 65 66 20 44 4c 5f 44 45 46 41  .#ifndef DL_DEFA
34cb7 55 4c 54 0a 23 20 64 65 66 69 6e 65 20 44 4c 5f  ULT.# define DL_
34cb8 44 45 46 41 55 4c 54 20 44 4c 5f 50 4f 53 49 54  DEFAULT DL_POSIT
34cb9 49 4f 4e 53 0a 23 65 6e 64 69 66 0a 0a 65 6e 75  IONS.#endif..enu
34cba 6d 20 7b 0a 20 20 50 4f 53 5f 45 4e 44 20 3d 20  m {.  POS_END = 
34cbb 30 2c 20 20 20 20 20 20 20 20 2f 2a 20 65 6e 64  0,        /* end
34cbc 20 6f 66 20 74 68 69 73 20 70 6f 73 69 74 69 6f   of this positio
34cbd 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 50 4f 53 5f  n list */.  POS_
34cbe 43 4f 4c 55 4d 4e 2c 20 20 20 20 20 20 20 20 20  COLUMN,         
34cbf 2f 2a 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e  /* followed by n
34cc0 65 77 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  ew column number
34cc1 20 2a 2f 0a 20 20 50 4f 53 5f 42 41 53 45 0a 7d   */.  POS_BASE.}
34cc2 3b 0a 0a 2f 2a 20 4d 45 52 47 45 5f 43 4f 55 4e  ;../* MERGE_COUN
34cc3 54 20 63 6f 6e 74 72 6f 6c 73 20 68 6f 77 20 6f  T controls how o
34cc4 66 74 65 6e 20 77 65 20 6d 65 72 67 65 20 73 65  ften we merge se
34cc5 67 6d 65 6e 74 73 20 28 73 65 65 20 63 6f 6d 6d  gments (see comm
34cc6 65 6e 74 20 61 74 0a 2a 2a 20 74 6f 70 20 6f 66  ent at.** top of
34cc7 20 66 69 6c 65 29 2e 0a 2a 2f 0a 23 64 65 66 69   file)..*/.#defi
34cc8 6e 65 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 31  ne MERGE_COUNT 1
34cc9 36 0a 0a 2f 2a 20 75 74 69 6c 69 74 79 20 66 75  6../* utility fu
34cca 6e 63 74 69 6f 6e 73 20 2a 2f 0a 0a 2f 2a 20 43  nctions */../* C
34ccb 4c 45 41 52 28 29 20 61 6e 64 20 53 43 52 41 4d  LEAR() and SCRAM
34ccc 42 4c 45 28 29 20 61 62 73 74 72 61 63 74 20 6d  BLE() abstract m
34ccd 65 6d 73 65 74 28 29 20 6f 6e 20 61 20 70 6f 69  emset() on a poi
34cce 6e 74 65 72 20 74 6f 20 61 20 73 69 6e 67 6c 65  nter to a single
34ccf 0a 2a 2a 20 72 65 63 6f 72 64 20 74 6f 20 70 72  .** record to pr
34cd0 65 76 65 6e 74 20 65 72 72 6f 72 73 20 6f 66 20  event errors of 
34cd1 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20  the form:.**.** 
34cd2 6d 79 5f 66 75 6e 63 74 69 6f 6e 28 53 6f 6d 65  my_function(Some
34cd3 54 79 70 65 20 2a 62 29 7b 0a 2a 2a 20 20 20 6d  Type *b){.**   m
34cd4 65 6d 73 65 74 28 62 2c 20 27 5c 30 27 2c 20 73  emset(b, '\0', s
34cd5 69 7a 65 6f 66 28 62 29 29 3b 20 20 2f 2f 20 73  izeof(b));  // s
34cd6 69 7a 65 6f 66 28 62 29 21 3d 73 69 7a 65 6f 66  izeof(b)!=sizeof
34cd7 28 2a 62 29 0a 2a 2a 20 7d 0a 2a 2f 0a 2f 2a 20  (*b).** }.*/./* 
34cd8 54 4f 44 4f 28 73 68 65 73 73 29 20 4f 62 76 69  TODO(shess) Obvi
34cd9 6f 75 73 20 63 61 6e 64 69 64 61 74 65 73 20 66  ous candidates f
34cda 6f 72 20 61 20 68 65 61 64 65 72 20 66 69 6c 65  or a header file
34cdb 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 43 4c 45  . */.#define CLE
34cdc 41 52 28 62 29 20 6d 65 6d 73 65 74 28 62 2c 20  AR(b) memset(b, 
34cdd 27 5c 30 27 2c 20 73 69 7a 65 6f 66 28 2a 28 62  '\0', sizeof(*(b
34cde 29 29 29 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  )))..#ifndef NDE
34cdf 42 55 47 0a 23 20 20 64 65 66 69 6e 65 20 53 43  BUG.#  define SC
34ce0 52 41 4d 42 4c 45 28 62 29 20 6d 65 6d 73 65 74  RAMBLE(b) memset
34ce1 28 62 2c 20 30 78 35 35 2c 20 73 69 7a 65 6f 66  (b, 0x55, sizeof
34ce2 28 2a 28 62 29 29 29 0a 23 65 6c 73 65 0a 23 20  (*(b))).#else.# 
34ce3 20 64 65 66 69 6e 65 20 53 43 52 41 4d 42 4c 45   define SCRAMBLE
34ce4 28 62 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 57  (b).#endif../* W
34ce5 65 20 6d 61 79 20 6e 65 65 64 20 75 70 20 74 6f  e may need up to
34ce6 20 56 41 52 49 4e 54 5f 4d 41 58 20 62 79 74 65   VARINT_MAX byte
34ce7 73 20 74 6f 20 73 74 6f 72 65 20 61 6e 20 65 6e  s to store an en
34ce8 63 6f 64 65 64 20 36 34 2d 62 69 74 20 69 6e 74  coded 64-bit int
34ce9 65 67 65 72 2e 20 2a 2f 0a 23 64 65 66 69 6e 65  eger. */.#define
34cea 20 56 41 52 49 4e 54 5f 4d 41 58 20 31 30 0a 0a   VARINT_MAX 10..
34ceb 2f 2a 20 57 72 69 74 65 20 61 20 36 34 2d 62 69  /* Write a 64-bi
34cec 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  t variable-lengt
34ced 68 20 69 6e 74 65 67 65 72 20 74 6f 20 6d 65 6d  h integer to mem
34cee 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20  ory starting at 
34cef 70 5b 30 5d 2e 0a 20 2a 20 54 68 65 20 6c 65 6e  p[0].. * The len
34cf0 67 74 68 20 6f 66 20 64 61 74 61 20 77 72 69 74  gth of data writ
34cf1 74 65 6e 20 77 69 6c 6c 20 62 65 20 62 65 74 77  ten will be betw
34cf2 65 65 6e 20 31 20 61 6e 64 20 56 41 52 49 4e 54  een 1 and VARINT
34cf3 5f 4d 41 58 20 62 79 74 65 73 2e 0a 20 2a 20 54  _MAX bytes.. * T
34cf4 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
34cf5 65 73 20 77 72 69 74 74 65 6e 20 69 73 20 72 65  es written is re
34cf6 74 75 72 6e 65 64 2e 20 2a 2f 0a 73 74 61 74 69  turned. */.stati
34cf7 63 20 69 6e 74 20 66 74 73 33 50 75 74 56 61 72  c int fts3PutVar
34cf8 69 6e 74 28 63 68 61 72 20 2a 70 2c 20 73 71 6c  int(char *p, sql
34cf9 69 74 65 5f 69 6e 74 36 34 20 76 29 7b 0a 20 20  ite_int64 v){.  
34cfa 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 71  unsigned char *q
34cfb 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
34cfc 72 20 2a 29 20 70 3b 0a 20 20 73 71 6c 69 74 65  r *) p;.  sqlite
34cfd 5f 75 69 6e 74 36 34 20 76 75 20 3d 20 76 3b 0a  _uint64 vu = v;.
34cfe 20 20 64 6f 7b 0a 20 20 20 20 2a 71 2b 2b 20 3d    do{.    *q++ =
34cff 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29   (unsigned char)
34d00 20 28 28 76 75 20 26 20 30 78 37 66 29 20 7c 20   ((vu & 0x7f) | 
34d01 30 78 38 30 29 3b 0a 20 20 20 20 76 75 20 3e 3e  0x80);.    vu >>
34d02 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76  = 7;.  }while( v
34d03 75 21 3d 30 20 29 3b 0a 20 20 71 5b 2d 31 5d 20  u!=0 );.  q[-1] 
34d04 26 3d 20 30 78 37 66 3b 20 20 2f 2a 20 74 75 72  &= 0x7f;  /* tur
34d05 6e 20 6f 66 66 20 68 69 67 68 20 62 69 74 20 69  n off high bit i
34d06 6e 20 66 69 6e 61 6c 20 62 79 74 65 20 2a 2f 0a  n final byte */.
34d07 20 20 61 73 73 65 72 74 28 20 71 20 2d 20 28 75    assert( q - (u
34d08 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70  nsigned char *)p
34d09 20 3c 3d 20 56 41 52 49 4e 54 5f 4d 41 58 20 29   <= VARINT_MAX )
34d0a 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29  ;.  return (int)
34d0b 20 28 71 20 2d 20 28 75 6e 73 69 67 6e 65 64 20   (q - (unsigned 
34d0c 63 68 61 72 20 2a 29 70 29 3b 0a 7d 0a 0a 2f 2a  char *)p);.}../*
34d0d 20 52 65 61 64 20 61 20 36 34 2d 62 69 74 20 76   Read a 64-bit v
34d0e 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
34d0f 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d 65 6d 6f  nteger from memo
34d10 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 70  ry starting at p
34d11 5b 30 5d 2e 0a 20 2a 20 52 65 74 75 72 6e 20 74  [0].. * Return t
34d12 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
34d13 65 73 20 72 65 61 64 2c 20 6f 72 20 30 20 6f 6e  es read, or 0 on
34d14 20 65 72 72 6f 72 2e 0a 20 2a 20 54 68 65 20 76   error.. * The v
34d15 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20 69  alue is stored i
34d16 6e 20 2a 76 2e 20 2a 2f 0a 73 74 61 74 69 63 20  n *v. */.static 
34d17 69 6e 74 20 66 74 73 33 47 65 74 56 61 72 69 6e  int fts3GetVarin
34d18 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 2c  t(const char *p,
34d19 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 76   sqlite_int64 *v
34d1a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  ){.  const unsig
34d1b 6e 65 64 20 63 68 61 72 20 2a 71 20 3d 20 28 63  ned char *q = (c
34d1c 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
34d1d 61 72 20 2a 29 20 70 3b 0a 20 20 73 71 6c 69 74  ar *) p;.  sqlit
34d1e 65 5f 75 69 6e 74 36 34 20 78 20 3d 20 30 2c 20  e_uint64 x = 0, 
34d1f 79 20 3d 20 31 3b 0a 20 20 77 68 69 6c 65 28 20  y = 1;.  while( 
34d20 28 2a 71 20 26 20 30 78 38 30 29 20 3d 3d 20 30  (*q & 0x80) == 0
34d21 78 38 30 20 29 7b 0a 20 20 20 20 78 20 2b 3d 20  x80 ){.    x += 
34d22 79 20 2a 20 28 2a 71 2b 2b 20 26 20 30 78 37 66  y * (*q++ & 0x7f
34d23 29 3b 0a 20 20 20 20 79 20 3c 3c 3d 20 37 3b 0a  );.    y <<= 7;.
34d24 20 20 20 20 69 66 28 20 71 20 2d 20 28 75 6e 73      if( q - (uns
34d25 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 20 3e  igned char *)p >
34d26 3d 20 56 41 52 49 4e 54 5f 4d 41 58 20 29 7b 20  = VARINT_MAX ){ 
34d27 20 2f 2a 20 62 61 64 20 64 61 74 61 20 2a 2f 0a   /* bad data */.
34d28 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20        assert( 0 
34d29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
34d2a 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 78  0;.    }.  }.  x
34d2b 20 2b 3d 20 79 20 2a 20 28 2a 71 2b 2b 29 3b 0a   += y * (*q++);.
34d2c 20 20 2a 76 20 3d 20 28 73 71 6c 69 74 65 5f 69    *v = (sqlite_i
34d2d 6e 74 36 34 29 20 78 3b 0a 20 20 72 65 74 75 72  nt64) x;.  retur
34d2e 6e 20 28 69 6e 74 29 20 28 71 20 2d 20 28 75 6e  n (int) (q - (un
34d2f 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 29  signed char *)p)
34d30 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
34d31 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
34d32 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 2c 20 69  const char *p, i
34d33 6e 74 20 2a 70 69 29 7b 0a 20 73 71 6c 69 74 65  nt *pi){. sqlite
34d34 5f 69 6e 74 36 34 20 69 3b 0a 20 69 6e 74 20 72  _int64 i;. int r
34d35 65 74 20 3d 20 66 74 73 33 47 65 74 56 61 72 69  et = fts3GetVari
34d36 6e 74 28 70 2c 20 26 69 29 3b 0a 20 2a 70 69 20  nt(p, &i);. *pi 
34d37 3d 20 28 69 6e 74 29 20 69 3b 0a 20 61 73 73 65  = (int) i;. asse
34d38 72 74 28 20 2a 70 69 3d 3d 69 20 29 3b 0a 20 72  rt( *pi==i );. r
34d39 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a  eturn ret;.}../*
34d3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d3e 2a 2a 2f 0a 2f 2a 20 44 61 74 61 42 75 66 66 65  **/./* DataBuffe
34d3f 72 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6c  r is used to col
34d40 6c 65 63 74 20 64 61 74 61 20 69 6e 74 6f 20 61  lect data into a
34d41 20 62 75 66 66 65 72 20 69 6e 20 70 69 65 63 65   buffer in piece
34d42 6d 65 61 6c 0a 2a 2a 20 66 61 73 68 69 6f 6e 2e  meal.** fashion.
34d43 20 20 49 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20    It implements 
34d44 74 68 65 20 75 73 75 61 6c 20 64 69 73 74 69 6e  the usual distin
34d45 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 61 6d  ction between am
34d46 6f 75 6e 74 20 6f 66 0a 2a 2a 20 64 61 74 61 20  ount of.** data 
34d47 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65 64  currently stored
34d48 20 28 6e 44 61 74 61 29 20 61 6e 64 20 62 75 66   (nData) and buf
34d49 66 65 72 20 63 61 70 61 63 69 74 79 20 28 6e 43  fer capacity (nC
34d4a 61 70 61 63 69 74 79 29 2e 0a 2a 2a 0a 2a 2a 20  apacity)..**.** 
34d4b 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 20 2d  dataBufferInit -
34d4c 20 63 72 65 61 74 65 20 61 20 62 75 66 66 65 72   create a buffer
34d4d 20 77 69 74 68 20 67 69 76 65 6e 20 69 6e 69 74   with given init
34d4e 69 61 6c 20 63 61 70 61 63 69 74 79 2e 0a 2a 2a  ial capacity..**
34d4f 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74   dataBufferReset
34d50 20 2d 20 66 6f 72 67 65 74 20 62 75 66 66 65 72   - forget buffer
34d51 27 73 20 64 61 74 61 2c 20 72 65 74 61 69 6e 69  's data, retaini
34d52 6e 67 20 63 61 70 61 63 69 74 79 2e 0a 2a 2a 20  ng capacity..** 
34d53 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
34d54 79 20 2d 20 66 72 65 65 20 62 75 66 66 65 72 27  y - free buffer'
34d55 73 20 64 61 74 61 2e 0a 2a 2a 20 64 61 74 61 42  s data..** dataB
34d56 75 66 66 65 72 53 77 61 70 20 2d 20 73 77 61 70  ufferSwap - swap
34d57 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 77 6f   contents of two
34d58 20 62 75 66 66 65 72 73 2e 0a 2a 2a 20 64 61 74   buffers..** dat
34d59 61 42 75 66 66 65 72 45 78 70 61 6e 64 20 2d 20  aBufferExpand - 
34d5a 65 78 70 61 6e 64 20 63 61 70 61 63 69 74 79 20  expand capacity 
34d5b 77 69 74 68 6f 75 74 20 61 64 64 69 6e 67 20 64  without adding d
34d5c 61 74 61 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66  ata..** dataBuff
34d5d 65 72 41 70 70 65 6e 64 20 2d 20 61 70 70 65 6e  erAppend - appen
34d5e 64 20 64 61 74 61 2e 0a 2a 2a 20 64 61 74 61 42  d data..** dataB
34d5f 75 66 66 65 72 41 70 70 65 6e 64 32 20 2d 20 61  ufferAppend2 - a
34d60 70 70 65 6e 64 20 74 77 6f 20 70 69 65 63 65 73  ppend two pieces
34d61 20 6f 66 20 64 61 74 61 20 61 74 20 6f 6e 63 65   of data at once
34d62 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72 52  ..** dataBufferR
34d63 65 70 6c 61 63 65 20 2d 20 72 65 70 6c 61 63 65  eplace - replace
34d64 20 62 75 66 66 65 72 27 73 20 64 61 74 61 2e 0a   buffer's data..
34d65 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
34d66 74 20 44 61 74 61 42 75 66 66 65 72 20 7b 0a 20  t DataBuffer {. 
34d67 20 63 68 61 72 20 2a 70 44 61 74 61 3b 20 20 20   char *pData;   
34d68 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
34d69 72 20 74 6f 20 6d 61 6c 6c 6f 63 27 65 64 20 62  r to malloc'ed b
34d6a 75 66 66 65 72 2e 20 2a 2f 0a 20 20 69 6e 74 20  uffer. */.  int 
34d6b 6e 43 61 70 61 63 69 74 79 3b 20 20 20 20 20 20  nCapacity;      
34d6c 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 44 61    /* Size of pDa
34d6d 74 61 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20  ta buffer. */.  
34d6e 69 6e 74 20 6e 44 61 74 61 3b 20 20 20 20 20 20  int nData;      
34d6f 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20        /* End of 
34d70 64 61 74 61 20 6c 6f 61 64 65 64 20 69 6e 74 6f  data loaded into
34d71 20 70 44 61 74 61 2e 20 2a 2f 0a 7d 20 44 61 74   pData. */.} Dat
34d72 61 42 75 66 66 65 72 3b 0a 0a 73 74 61 74 69 63  aBuffer;..static
34d73 20 76 6f 69 64 20 64 61 74 61 42 75 66 66 65 72   void dataBuffer
34d74 49 6e 69 74 28 44 61 74 61 42 75 66 66 65 72 20  Init(DataBuffer 
34d75 2a 70 42 75 66 66 65 72 2c 20 69 6e 74 20 6e 43  *pBuffer, int nC
34d76 61 70 61 63 69 74 79 29 7b 0a 20 20 61 73 73 65  apacity){.  asse
34d77 72 74 28 20 6e 43 61 70 61 63 69 74 79 3e 3d 30  rt( nCapacity>=0
34d78 20 29 3b 0a 20 20 70 42 75 66 66 65 72 2d 3e 6e   );.  pBuffer->n
34d79 44 61 74 61 20 3d 20 30 3b 0a 20 20 70 42 75 66  Data = 0;.  pBuf
34d7a 66 65 72 2d 3e 6e 43 61 70 61 63 69 74 79 20 3d  fer->nCapacity =
34d7b 20 6e 43 61 70 61 63 69 74 79 3b 0a 20 20 70 42   nCapacity;.  pB
34d7c 75 66 66 65 72 2d 3e 70 44 61 74 61 20 3d 20 6e  uffer->pData = n
34d7d 43 61 70 61 63 69 74 79 3d 3d 30 20 3f 20 4e 55  Capacity==0 ? NU
34d7e 4c 4c 20 3a 20 73 71 6c 69 74 65 33 5f 6d 61 6c  LL : sqlite3_mal
34d7f 6c 6f 63 28 6e 43 61 70 61 63 69 74 79 29 3b 0a  loc(nCapacity);.
34d80 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61  }.static void da
34d81 74 61 42 75 66 66 65 72 52 65 73 65 74 28 44 61  taBufferReset(Da
34d82 74 61 42 75 66 66 65 72 20 2a 70 42 75 66 66 65  taBuffer *pBuffe
34d83 72 29 7b 0a 20 20 70 42 75 66 66 65 72 2d 3e 6e  r){.  pBuffer->n
34d84 44 61 74 61 20 3d 20 30 3b 0a 7d 0a 73 74 61 74  Data = 0;.}.stat
34d85 69 63 20 76 6f 69 64 20 64 61 74 61 42 75 66 66  ic void dataBuff
34d86 65 72 44 65 73 74 72 6f 79 28 44 61 74 61 42 75  erDestroy(DataBu
34d87 66 66 65 72 20 2a 70 42 75 66 66 65 72 29 7b 0a  ffer *pBuffer){.
34d88 20 20 69 66 28 20 70 42 75 66 66 65 72 2d 3e 70    if( pBuffer->p
34d89 44 61 74 61 21 3d 4e 55 4c 4c 20 29 20 73 71 6c  Data!=NULL ) sql
34d8a 69 74 65 33 5f 66 72 65 65 28 70 42 75 66 66 65  ite3_free(pBuffe
34d8b 72 2d 3e 70 44 61 74 61 29 3b 0a 20 20 53 43 52  r->pData);.  SCR
34d8c 41 4d 42 4c 45 28 70 42 75 66 66 65 72 29 3b 0a  AMBLE(pBuffer);.
34d8d 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61  }.static void da
34d8e 74 61 42 75 66 66 65 72 53 77 61 70 28 44 61 74  taBufferSwap(Dat
34d8f 61 42 75 66 66 65 72 20 2a 70 42 75 66 66 65 72  aBuffer *pBuffer
34d90 31 2c 20 44 61 74 61 42 75 66 66 65 72 20 2a 70  1, DataBuffer *p
34d91 42 75 66 66 65 72 32 29 7b 0a 20 20 44 61 74 61  Buffer2){.  Data
34d92 42 75 66 66 65 72 20 74 6d 70 20 3d 20 2a 70 42  Buffer tmp = *pB
34d93 75 66 66 65 72 31 3b 0a 20 20 2a 70 42 75 66 66  uffer1;.  *pBuff
34d94 65 72 31 20 3d 20 2a 70 42 75 66 66 65 72 32 3b  er1 = *pBuffer2;
34d95 0a 20 20 2a 70 42 75 66 66 65 72 32 20 3d 20 74  .  *pBuffer2 = t
34d96 6d 70 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  mp;.}.static voi
34d97 64 20 64 61 74 61 42 75 66 66 65 72 45 78 70 61  d dataBufferExpa
34d98 6e 64 28 44 61 74 61 42 75 66 66 65 72 20 2a 70  nd(DataBuffer *p
34d99 42 75 66 66 65 72 2c 20 69 6e 74 20 6e 41 64 64  Buffer, int nAdd
34d9a 43 61 70 61 63 69 74 79 29 7b 0a 20 20 61 73 73  Capacity){.  ass
34d9b 65 72 74 28 20 6e 41 64 64 43 61 70 61 63 69 74  ert( nAddCapacit
34d9c 79 3e 30 20 29 3b 0a 20 20 2f 2a 20 54 4f 44 4f  y>0 );.  /* TODO
34d9d 28 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72  (shess) Consider
34d9e 20 65 78 70 61 6e 64 69 6e 67 20 6d 6f 72 65 20   expanding more 
34d9f 61 67 67 72 65 73 73 69 76 65 6c 79 2e 20 20 4e  aggressively.  N
34da0 6f 74 65 20 74 68 61 74 20 74 68 65 0a 20 20 2a  ote that the.  *
34da1 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d 61 6c  * underlying mal
34da2 6c 6f 63 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  loc implementati
34da3 6f 6e 20 6d 61 79 20 74 61 6b 65 20 63 61 72 65  on may take care
34da4 20 6f 66 20 73 75 63 68 20 74 68 69 6e 67 73 20   of such things 
34da5 66 6f 72 0a 20 20 2a 2a 20 75 73 20 61 6c 72 65  for.  ** us alre
34da6 61 64 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ady..  */.  if( 
34da7 70 42 75 66 66 65 72 2d 3e 6e 44 61 74 61 2b 6e  pBuffer->nData+n
34da8 41 64 64 43 61 70 61 63 69 74 79 3e 70 42 75 66  AddCapacity>pBuf
34da9 66 65 72 2d 3e 6e 43 61 70 61 63 69 74 79 20 29  fer->nCapacity )
34daa 7b 0a 20 20 20 20 70 42 75 66 66 65 72 2d 3e 6e  {.    pBuffer->n
34dab 43 61 70 61 63 69 74 79 20 3d 20 70 42 75 66 66  Capacity = pBuff
34dac 65 72 2d 3e 6e 44 61 74 61 2b 6e 41 64 64 43 61  er->nData+nAddCa
34dad 70 61 63 69 74 79 3b 0a 20 20 20 20 70 42 75 66  pacity;.    pBuf
34dae 66 65 72 2d 3e 70 44 61 74 61 20 3d 20 73 71 6c  fer->pData = sql
34daf 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 42 75  ite3_realloc(pBu
34db0 66 66 65 72 2d 3e 70 44 61 74 61 2c 20 70 42 75  ffer->pData, pBu
34db1 66 66 65 72 2d 3e 6e 43 61 70 61 63 69 74 79 29  ffer->nCapacity)
34db2 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76  ;.  }.}.static v
34db3 6f 69 64 20 64 61 74 61 42 75 66 66 65 72 41 70  oid dataBufferAp
34db4 70 65 6e 64 28 44 61 74 61 42 75 66 66 65 72 20  pend(DataBuffer 
34db5 2a 70 42 75 66 66 65 72 2c 0a 20 20 20 20 20 20  *pBuffer,.      
34db6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34db7 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
34db8 72 20 2a 70 53 6f 75 72 63 65 2c 20 69 6e 74 20  r *pSource, int 
34db9 6e 53 6f 75 72 63 65 29 7b 0a 20 20 61 73 73 65  nSource){.  asse
34dba 72 74 28 20 6e 53 6f 75 72 63 65 3e 30 20 26 26  rt( nSource>0 &&
34dbb 20 70 53 6f 75 72 63 65 21 3d 4e 55 4c 4c 20 29   pSource!=NULL )
34dbc 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 45 78  ;.  dataBufferEx
34dbd 70 61 6e 64 28 70 42 75 66 66 65 72 2c 20 6e 53  pand(pBuffer, nS
34dbe 6f 75 72 63 65 29 3b 0a 20 20 6d 65 6d 63 70 79  ource);.  memcpy
34dbf 28 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61 2b  (pBuffer->pData+
34dc0 70 42 75 66 66 65 72 2d 3e 6e 44 61 74 61 2c 20  pBuffer->nData, 
34dc1 70 53 6f 75 72 63 65 2c 20 6e 53 6f 75 72 63 65  pSource, nSource
34dc2 29 3b 0a 20 20 70 42 75 66 66 65 72 2d 3e 6e 44  );.  pBuffer->nD
34dc3 61 74 61 20 2b 3d 20 6e 53 6f 75 72 63 65 3b 0a  ata += nSource;.
34dc4 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61  }.static void da
34dc5 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32 28  taBufferAppend2(
34dc6 44 61 74 61 42 75 66 66 65 72 20 2a 70 42 75 66  DataBuffer *pBuf
34dc7 66 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  fer,.           
34dc8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34dc9 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
34dca 53 6f 75 72 63 65 31 2c 20 69 6e 74 20 6e 53 6f  Source1, int nSo
34dcb 75 72 63 65 31 2c 0a 20 20 20 20 20 20 20 20 20  urce1,.         
34dcc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34dcd 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
34dce 2a 70 53 6f 75 72 63 65 32 2c 20 69 6e 74 20 6e  *pSource2, int n
34dcf 53 6f 75 72 63 65 32 29 7b 0a 20 20 61 73 73 65  Source2){.  asse
34dd0 72 74 28 20 6e 53 6f 75 72 63 65 31 3e 30 20 26  rt( nSource1>0 &
34dd1 26 20 70 53 6f 75 72 63 65 31 21 3d 4e 55 4c 4c  & pSource1!=NULL
34dd2 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 53   );.  assert( nS
34dd3 6f 75 72 63 65 32 3e 30 20 26 26 20 70 53 6f 75  ource2>0 && pSou
34dd4 72 63 65 32 21 3d 4e 55 4c 4c 20 29 3b 0a 20 20  rce2!=NULL );.  
34dd5 64 61 74 61 42 75 66 66 65 72 45 78 70 61 6e 64  dataBufferExpand
34dd6 28 70 42 75 66 66 65 72 2c 20 6e 53 6f 75 72 63  (pBuffer, nSourc
34dd7 65 31 2b 6e 53 6f 75 72 63 65 32 29 3b 0a 20 20  e1+nSource2);.  
34dd8 6d 65 6d 63 70 79 28 70 42 75 66 66 65 72 2d 3e  memcpy(pBuffer->
34dd9 70 44 61 74 61 2b 70 42 75 66 66 65 72 2d 3e 6e  pData+pBuffer->n
34dda 44 61 74 61 2c 20 70 53 6f 75 72 63 65 31 2c 20  Data, pSource1, 
34ddb 6e 53 6f 75 72 63 65 31 29 3b 0a 20 20 6d 65 6d  nSource1);.  mem
34ddc 63 70 79 28 70 42 75 66 66 65 72 2d 3e 70 44 61  cpy(pBuffer->pDa
34ddd 74 61 2b 70 42 75 66 66 65 72 2d 3e 6e 44 61 74  ta+pBuffer->nDat
34dde 61 2b 6e 53 6f 75 72 63 65 31 2c 20 70 53 6f 75  a+nSource1, pSou
34ddf 72 63 65 32 2c 20 6e 53 6f 75 72 63 65 32 29 3b  rce2, nSource2);
34de0 0a 20 20 70 42 75 66 66 65 72 2d 3e 6e 44 61 74  .  pBuffer->nDat
34de1 61 20 2b 3d 20 6e 53 6f 75 72 63 65 31 2b 6e 53  a += nSource1+nS
34de2 6f 75 72 63 65 32 3b 0a 7d 0a 73 74 61 74 69 63  ource2;.}.static
34de3 20 76 6f 69 64 20 64 61 74 61 42 75 66 66 65 72   void dataBuffer
34de4 52 65 70 6c 61 63 65 28 44 61 74 61 42 75 66 66  Replace(DataBuff
34de5 65 72 20 2a 70 42 75 66 66 65 72 2c 0a 20 20 20  er *pBuffer,.   
34de6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34de7 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
34de8 20 63 68 61 72 20 2a 70 53 6f 75 72 63 65 2c 20   char *pSource, 
34de9 69 6e 74 20 6e 53 6f 75 72 63 65 29 7b 0a 20 20  int nSource){.  
34dea 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28  dataBufferReset(
34deb 70 42 75 66 66 65 72 29 3b 0a 20 20 64 61 74 61  pBuffer);.  data
34dec 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 42 75  BufferAppend(pBu
34ded 66 66 65 72 2c 20 70 53 6f 75 72 63 65 2c 20 6e  ffer, pSource, n
34dee 53 6f 75 72 63 65 29 3b 0a 7d 0a 0a 2f 2a 20 53  Source);.}../* S
34def 74 72 69 6e 67 42 75 66 66 65 72 20 69 73 20 61  tringBuffer is a
34df0 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64   null-terminated
34df1 20 76 65 72 73 69 6f 6e 20 6f 66 20 44 61 74 61   version of Data
34df2 42 75 66 66 65 72 2e 20 2a 2f 0a 74 79 70 65 64  Buffer. */.typed
34df3 65 66 20 73 74 72 75 63 74 20 53 74 72 69 6e 67  ef struct String
34df4 42 75 66 66 65 72 20 7b 0a 20 20 44 61 74 61 42  Buffer {.  DataB
34df5 75 66 66 65 72 20 62 3b 20 20 20 20 20 20 20 20  uffer b;        
34df6 20 20 20 20 2f 2a 20 49 6e 63 6c 75 64 65 73 20      /* Includes 
34df7 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72 2e  null terminator.
34df8 20 2a 2f 0a 7d 20 53 74 72 69 6e 67 42 75 66 66   */.} StringBuff
34df9 65 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  er;..static void
34dfa 20 69 6e 69 74 53 74 72 69 6e 67 42 75 66 66 65   initStringBuffe
34dfb 72 28 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a  r(StringBuffer *
34dfc 73 62 29 7b 0a 20 20 64 61 74 61 42 75 66 66 65  sb){.  dataBuffe
34dfd 72 49 6e 69 74 28 26 73 62 2d 3e 62 2c 20 31 30  rInit(&sb->b, 10
34dfe 30 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72  0);.  dataBuffer
34dff 52 65 70 6c 61 63 65 28 26 73 62 2d 3e 62 2c 20  Replace(&sb->b, 
34e00 22 22 2c 20 31 29 3b 0a 7d 0a 73 74 61 74 69 63  "", 1);.}.static
34e01 20 69 6e 74 20 73 74 72 69 6e 67 42 75 66 66 65   int stringBuffe
34e02 72 4c 65 6e 67 74 68 28 53 74 72 69 6e 67 42 75  rLength(StringBu
34e03 66 66 65 72 20 2a 73 62 29 7b 0a 20 20 72 65 74  ffer *sb){.  ret
34e04 75 72 6e 20 73 62 2d 3e 62 2e 6e 44 61 74 61 2d  urn sb->b.nData-
34e05 31 3b 0a 7d 0a 73 74 61 74 69 63 20 63 68 61 72  1;.}.static char
34e06 20 2a 73 74 72 69 6e 67 42 75 66 66 65 72 44 61   *stringBufferDa
34e07 74 61 28 53 74 72 69 6e 67 42 75 66 66 65 72 20  ta(StringBuffer 
34e08 2a 73 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  *sb){.  return s
34e09 62 2d 3e 62 2e 70 44 61 74 61 3b 0a 7d 0a 73 74  b->b.pData;.}.st
34e0a 61 74 69 63 20 76 6f 69 64 20 73 74 72 69 6e 67  atic void string
34e0b 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 53 74  BufferDestroy(St
34e0c 72 69 6e 67 42 75 66 66 65 72 20 2a 73 62 29 7b  ringBuffer *sb){
34e0d 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73  .  dataBufferDes
34e0e 74 72 6f 79 28 26 73 62 2d 3e 62 29 3b 0a 7d 0a  troy(&sb->b);.}.
34e0f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 61 70  .static void nap
34e10 70 65 6e 64 28 53 74 72 69 6e 67 42 75 66 66 65  pend(StringBuffe
34e11 72 20 2a 73 62 2c 20 63 6f 6e 73 74 20 63 68 61  r *sb, const cha
34e12 72 20 2a 7a 46 72 6f 6d 2c 20 69 6e 74 20 6e 46  r *zFrom, int nF
34e13 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20  rom){.  assert( 
34e14 73 62 2d 3e 62 2e 6e 44 61 74 61 3e 30 20 29 3b  sb->b.nData>0 );
34e15 0a 20 20 69 66 28 20 6e 46 72 6f 6d 3e 30 20 29  .  if( nFrom>0 )
34e16 7b 0a 20 20 20 20 73 62 2d 3e 62 2e 6e 44 61 74  {.    sb->b.nDat
34e17 61 2d 2d 3b 0a 20 20 20 20 64 61 74 61 42 75 66  a--;.    dataBuf
34e18 66 65 72 41 70 70 65 6e 64 32 28 26 73 62 2d 3e  ferAppend2(&sb->
34e19 62 2c 20 7a 46 72 6f 6d 2c 20 6e 46 72 6f 6d 2c  b, zFrom, nFrom,
34e1a 20 22 22 2c 20 31 29 3b 0a 20 20 7d 0a 7d 0a 73   "", 1);.  }.}.s
34e1b 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e  tatic void appen
34e1c 64 28 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a  d(StringBuffer *
34e1d 73 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  sb, const char *
34e1e 7a 46 72 6f 6d 29 7b 0a 20 20 6e 61 70 70 65 6e  zFrom){.  nappen
34e1f 64 28 73 62 2c 20 7a 46 72 6f 6d 2c 20 73 74 72  d(sb, zFrom, str
34e20 6c 65 6e 28 7a 46 72 6f 6d 29 29 3b 0a 7d 0a 0a  len(zFrom));.}..
34e21 2f 2a 20 41 70 70 65 6e 64 20 61 20 6c 69 73 74  /* Append a list
34e22 20 6f 66 20 73 74 72 69 6e 67 73 20 73 65 70 61   of strings sepa
34e23 72 61 74 65 64 20 62 79 20 63 6f 6d 6d 61 73 2e  rated by commas.
34e24 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20   */.static void 
34e25 61 70 70 65 6e 64 4c 69 73 74 28 53 74 72 69 6e  appendList(Strin
34e26 67 42 75 66 66 65 72 20 2a 73 62 2c 20 69 6e 74  gBuffer *sb, int
34e27 20 6e 53 74 72 69 6e 67 2c 20 63 68 61 72 20 2a   nString, char *
34e28 2a 61 7a 53 74 72 69 6e 67 29 7b 0a 20 20 69 6e  *azString){.  in
34e29 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  t i;.  for(i=0; 
34e2a 69 3c 6e 53 74 72 69 6e 67 3b 20 2b 2b 69 29 7b  i<nString; ++i){
34e2b 0a 20 20 20 20 69 66 28 20 69 3e 30 20 29 20 61  .    if( i>0 ) a
34e2c 70 70 65 6e 64 28 73 62 2c 20 22 2c 20 22 29 3b  ppend(sb, ", ");
34e2d 0a 20 20 20 20 61 70 70 65 6e 64 28 73 62 2c 20  .    append(sb, 
34e2e 61 7a 53 74 72 69 6e 67 5b 69 5d 29 3b 0a 20 20  azString[i]);.  
34e2f 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  }.}..static int 
34e30 65 6e 64 73 49 6e 57 68 69 74 65 53 70 61 63 65  endsInWhiteSpace
34e31 28 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 70  (StringBuffer *p
34e32 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 74 72 69  ){.  return stri
34e33 6e 67 42 75 66 66 65 72 4c 65 6e 67 74 68 28 70  ngBufferLength(p
34e34 29 3e 30 20 26 26 0a 20 20 20 20 73 61 66 65 5f  )>0 &&.    safe_
34e35 69 73 73 70 61 63 65 28 73 74 72 69 6e 67 42 75  isspace(stringBu
34e36 66 66 65 72 44 61 74 61 28 70 29 5b 73 74 72 69  fferData(p)[stri
34e37 6e 67 42 75 66 66 65 72 4c 65 6e 67 74 68 28 70  ngBufferLength(p
34e38 29 2d 31 5d 29 3b 0a 7d 0a 0a 2f 2a 20 49 66 20  )-1]);.}../* If 
34e39 74 68 65 20 53 74 72 69 6e 67 42 75 66 66 65 72  the StringBuffer
34e3a 20 65 6e 64 73 20 69 6e 20 73 6f 6d 65 74 68 69   ends in somethi
34e3b 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 77 68  ng other than wh
34e3c 69 74 65 20 73 70 61 63 65 2c 20 61 64 64 20 61  ite space, add a
34e3d 0a 2a 2a 20 73 69 6e 67 6c 65 20 73 70 61 63 65  .** single space
34e3e 20 63 68 61 72 61 63 74 65 72 20 74 6f 20 74 68   character to th
34e3f 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  e end..*/.static
34e40 20 76 6f 69 64 20 61 70 70 65 6e 64 57 68 69 74   void appendWhit
34e41 65 53 70 61 63 65 28 53 74 72 69 6e 67 42 75 66  eSpace(StringBuf
34e42 66 65 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 73  fer *p){.  if( s
34e43 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74  tringBufferLengt
34e44 68 28 70 29 3d 3d 30 20 29 20 72 65 74 75 72 6e  h(p)==0 ) return
34e45 3b 0a 20 20 69 66 28 20 21 65 6e 64 73 49 6e 57  ;.  if( !endsInW
34e46 68 69 74 65 53 70 61 63 65 28 70 29 20 29 20 61  hiteSpace(p) ) a
34e47 70 70 65 6e 64 28 70 2c 20 22 20 22 29 3b 0a 7d  ppend(p, " ");.}
34e48 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 77 68 69 74  ../* Remove whit
34e49 65 20 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65  e space from the
34e4a 20 65 6e 64 20 6f 66 20 74 68 65 20 53 74 72 69   end of the Stri
34e4b 6e 67 42 75 66 66 65 72 20 2a 2f 0a 73 74 61 74  ngBuffer */.stat
34e4c 69 63 20 76 6f 69 64 20 74 72 69 6d 57 68 69 74  ic void trimWhit
34e4d 65 53 70 61 63 65 28 53 74 72 69 6e 67 42 75 66  eSpace(StringBuf
34e4e 66 65 72 20 2a 70 29 7b 0a 20 20 77 68 69 6c 65  fer *p){.  while
34e4f 28 20 65 6e 64 73 49 6e 57 68 69 74 65 53 70 61  ( endsInWhiteSpa
34e50 63 65 28 70 29 20 29 7b 0a 20 20 20 20 70 2d 3e  ce(p) ){.    p->
34e51 62 2e 70 44 61 74 61 5b 2d 2d 70 2d 3e 62 2e 6e  b.pData[--p->b.n
34e52 44 61 74 61 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a  Data-1] = '\0';.
34e53 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a    }.}../********
34e54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20  ***********/./* 
34e58 44 4c 52 65 61 64 65 72 20 69 73 20 75 73 65 64  DLReader is used
34e59 20 74 6f 20 72 65 61 64 20 64 6f 63 75 6d 65 6e   to read documen
34e5a 74 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 20  t elements from 
34e5b 61 20 64 6f 63 6c 69 73 74 2e 20 20 54 68 65 0a  a doclist.  The.
34e5c 2a 2a 20 63 75 72 72 65 6e 74 20 64 6f 63 69 64  ** current docid
34e5d 20 69 73 20 63 61 63 68 65 64 2c 20 73 6f 20 64   is cached, so d
34e5e 6c 72 44 6f 63 69 64 28 29 20 69 73 20 66 61 73  lrDocid() is fas
34e5f 74 2e 20 20 44 4c 52 65 61 64 65 72 20 64 6f 65  t.  DLReader doe
34e60 73 20 6e 6f 74 0a 2a 2a 20 6f 77 6e 20 74 68 65  s not.** own the
34e61 20 64 6f 63 6c 69 73 74 20 62 75 66 66 65 72 2e   doclist buffer.
34e62 0a 2a 2a 0a 2a 2a 20 64 6c 72 41 74 45 6e 64 20  .**.** dlrAtEnd 
34e63 2d 20 74 72 75 65 20 69 66 20 74 68 65 72 65 27  - true if there'
34e64 73 20 6e 6f 20 6d 6f 72 65 20 64 61 74 61 20 74  s no more data t
34e65 6f 20 72 65 61 64 2e 0a 2a 2a 20 64 6c 72 44 6f  o read..** dlrDo
34e66 63 69 64 20 2d 20 64 6f 63 69 64 20 6f 66 20 63  cid - docid of c
34e67 75 72 72 65 6e 74 20 64 6f 63 75 6d 65 6e 74 2e  urrent document.
34e68 0a 2a 2a 20 64 6c 72 44 6f 63 44 61 74 61 20 2d  .** dlrDocData -
34e69 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 66 6f   doclist data fo
34e6a 72 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d 65  r current docume
34e6b 6e 74 20 28 69 6e 63 6c 75 64 69 6e 67 20 64 6f  nt (including do
34e6c 63 69 64 29 2e 0a 2a 2a 20 64 6c 72 44 6f 63 44  cid)..** dlrDocD
34e6d 61 74 61 42 79 74 65 73 20 2d 20 6c 65 6e 67 74  ataBytes - lengt
34e6e 68 20 6f 66 20 73 61 6d 65 2e 0a 2a 2a 20 64 6c  h of same..** dl
34e6f 72 41 6c 6c 44 61 74 61 42 79 74 65 73 20 2d 20  rAllDataBytes - 
34e70 6c 65 6e 67 74 68 20 6f 66 20 61 6c 6c 20 72 65  length of all re
34e71 6d 61 69 6e 69 6e 67 20 64 61 74 61 2e 0a 2a 2a  maining data..**
34e72 20 64 6c 72 50 6f 73 44 61 74 61 20 2d 20 70 6f   dlrPosData - po
34e73 73 69 74 69 6f 6e 20 64 61 74 61 20 66 6f 72 20  sition data for 
34e74 63 75 72 72 65 6e 74 20 64 6f 63 75 6d 65 6e 74  current document
34e75 2e 0a 2a 2a 20 64 6c 72 50 6f 73 44 61 74 61 4c  ..** dlrPosDataL
34e76 65 6e 20 2d 20 6c 65 6e 67 74 68 20 6f 66 20 70  en - length of p
34e77 6f 73 20 64 61 74 61 20 66 6f 72 20 63 75 72 72  os data for curr
34e78 65 6e 74 20 64 6f 63 75 6d 65 6e 74 20 28 69 6e  ent document (in
34e79 63 6c 20 50 4f 53 5f 45 4e 44 29 2e 0a 2a 2a 20  cl POS_END)..** 
34e7a 64 6c 72 53 74 65 70 20 2d 20 73 74 65 70 20 74  dlrStep - step t
34e7b 6f 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d 65  o current docume
34e7c 6e 74 2e 0a 2a 2a 20 64 6c 72 49 6e 69 74 20 2d  nt..** dlrInit -
34e7d 20 69 6e 69 74 69 61 6c 20 66 6f 72 20 64 6f 63   initial for doc
34e7e 6c 69 73 74 20 6f 66 20 67 69 76 65 6e 20 74 79  list of given ty
34e7f 70 65 20 61 67 61 69 6e 73 74 20 67 69 76 65 6e  pe against given
34e80 20 64 61 74 61 2e 0a 2a 2a 20 64 6c 72 44 65 73   data..** dlrDes
34e81 74 72 6f 79 20 2d 20 63 6c 65 61 6e 20 75 70 2e  troy - clean up.
34e82 0a 2a 2a 0a 2a 2a 20 45 78 70 65 63 74 65 64 20  .**.** Expected 
34e83 75 73 61 67 65 20 69 73 20 73 6f 6d 65 74 68 69  usage is somethi
34e84 6e 67 20 6c 69 6b 65 3a 0a 2a 2a 0a 2a 2a 20 20  ng like:.**.**  
34e85 20 44 4c 52 65 61 64 65 72 20 72 65 61 64 65 72   DLReader reader
34e86 3b 0a 2a 2a 20 20 20 64 6c 72 49 6e 69 74 28 26  ;.**   dlrInit(&
34e87 72 65 61 64 65 72 2c 20 70 44 61 74 61 2c 20 6e  reader, pData, n
34e88 44 61 74 61 29 3b 0a 2a 2a 20 20 20 77 68 69 6c  Data);.**   whil
34e89 65 28 20 21 64 6c 72 41 74 45 6e 64 28 26 72 65  e( !dlrAtEnd(&re
34e8a 61 64 65 72 29 20 29 7b 0a 2a 2a 20 20 20 20 20  ader) ){.**     
34e8b 2f 2f 20 63 61 6c 6c 73 20 74 6f 20 64 6c 72 44  // calls to dlrD
34e8c 6f 63 69 64 28 29 20 61 6e 64 20 6b 69 6e 2e 0a  ocid() and kin..
34e8d 2a 2a 20 20 20 20 20 64 6c 72 53 74 65 70 28 26  **     dlrStep(&
34e8e 72 65 61 64 65 72 29 3b 0a 2a 2a 20 20 20 7d 0a  reader);.**   }.
34e8f 2a 2a 20 20 20 64 6c 72 44 65 73 74 72 6f 79 28  **   dlrDestroy(
34e90 26 72 65 61 64 65 72 29 3b 0a 2a 2f 0a 74 79 70  &reader);.*/.typ
34e91 65 64 65 66 20 73 74 72 75 63 74 20 44 4c 52 65  edef struct DLRe
34e92 61 64 65 72 20 7b 0a 20 20 44 6f 63 4c 69 73 74  ader {.  DocList
34e93 54 79 70 65 20 69 54 79 70 65 3b 0a 20 20 63 6f  Type iType;.  co
34e94 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 3b  nst char *pData;
34e95 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a 0a 20  .  int nData;.. 
34e96 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44   sqlite_int64 iD
34e97 6f 63 69 64 3b 0a 20 20 69 6e 74 20 6e 45 6c 65  ocid;.  int nEle
34e98 6d 65 6e 74 3b 0a 7d 20 44 4c 52 65 61 64 65 72  ment;.} DLReader
34e99 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6c  ;..static int dl
34e9a 72 41 74 45 6e 64 28 44 4c 52 65 61 64 65 72 20  rAtEnd(DLReader 
34e9b 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73  *pReader){.  ass
34e9c 65 72 74 28 20 70 52 65 61 64 65 72 2d 3e 6e 44  ert( pReader->nD
34e9d 61 74 61 3e 3d 30 20 29 3b 0a 20 20 72 65 74 75  ata>=0 );.  retu
34e9e 72 6e 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  rn pReader->nDat
34e9f 61 3d 3d 30 3b 0a 7d 0a 73 74 61 74 69 63 20 73  a==0;.}.static s
34ea0 71 6c 69 74 65 5f 69 6e 74 36 34 20 64 6c 72 44  qlite_int64 dlrD
34ea1 6f 63 69 64 28 44 4c 52 65 61 64 65 72 20 2a 70  ocid(DLReader *p
34ea2 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72  Reader){.  asser
34ea3 74 28 20 21 64 6c 72 41 74 45 6e 64 28 70 52 65  t( !dlrAtEnd(pRe
34ea4 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72  ader) );.  retur
34ea5 6e 20 70 52 65 61 64 65 72 2d 3e 69 44 6f 63 69  n pReader->iDoci
34ea6 64 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73  d;.}.static cons
34ea7 74 20 63 68 61 72 20 2a 64 6c 72 44 6f 63 44 61  t char *dlrDocDa
34ea8 74 61 28 44 4c 52 65 61 64 65 72 20 2a 70 52 65  ta(DLReader *pRe
34ea9 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28  ader){.  assert(
34eaa 20 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61 64   !dlrAtEnd(pRead
34eab 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  er) );.  return 
34eac 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 3b 0a  pReader->pData;.
34ead 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6c 72  }.static int dlr
34eae 44 6f 63 44 61 74 61 42 79 74 65 73 28 44 4c 52  DocDataBytes(DLR
34eaf 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
34eb0 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c 72 41  .  assert( !dlrA
34eb1 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
34eb2 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65  .  return pReade
34eb3 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3b 0a 7d 0a 73  r->nElement;.}.s
34eb4 74 61 74 69 63 20 69 6e 74 20 64 6c 72 41 6c 6c  tatic int dlrAll
34eb5 44 61 74 61 42 79 74 65 73 28 44 4c 52 65 61 64  DataBytes(DLRead
34eb6 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
34eb7 61 73 73 65 72 74 28 20 21 64 6c 72 41 74 45 6e  assert( !dlrAtEn
34eb8 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20  d(pReader) );.  
34eb9 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
34eba 6e 44 61 74 61 3b 0a 7d 0a 2f 2a 20 54 4f 44 4f  nData;.}./* TODO
34ebb 28 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72  (shess) Consider
34ebc 20 61 64 64 69 6e 67 20 61 20 66 69 65 6c 64 20   adding a field 
34ebd 74 6f 20 74 72 61 63 6b 20 69 44 6f 63 69 64 20  to track iDocid 
34ebe 76 61 72 69 6e 74 20 6c 65 6e 67 74 68 0a 2a 2a  varint length.**
34ebf 20 74 6f 20 6d 61 6b 65 20 74 68 65 73 65 20 74   to make these t
34ec0 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20 66 61 73  wo functions fas
34ec1 74 65 72 2e 20 20 54 68 69 73 20 6d 69 67 68 74  ter.  This might
34ec2 20 6d 61 74 74 65 72 20 28 61 20 74 69 6e 79 20   matter (a tiny 
34ec3 62 69 74 29 0a 2a 2a 20 66 6f 72 20 71 75 65 72  bit).** for quer
34ec4 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ies..*/.static c
34ec5 6f 6e 73 74 20 63 68 61 72 20 2a 64 6c 72 50 6f  onst char *dlrPo
34ec6 73 44 61 74 61 28 44 4c 52 65 61 64 65 72 20 2a  sData(DLReader *
34ec7 70 52 65 61 64 65 72 29 7b 0a 20 20 73 71 6c 69  pReader){.  sqli
34ec8 74 65 5f 69 6e 74 36 34 20 69 44 75 6d 6d 79 3b  te_int64 iDummy;
34ec9 0a 20 20 69 6e 74 20 6e 20 3d 20 66 74 73 33 47  .  int n = fts3G
34eca 65 74 56 61 72 69 6e 74 28 70 52 65 61 64 65 72  etVarint(pReader
34ecb 2d 3e 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79  ->pData, &iDummy
34ecc 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c  );.  assert( !dl
34ecd 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20  rAtEnd(pReader) 
34ece 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  );.  return pRea
34ecf 64 65 72 2d 3e 70 44 61 74 61 2b 6e 3b 0a 7d 0a  der->pData+n;.}.
34ed0 73 74 61 74 69 63 20 69 6e 74 20 64 6c 72 50 6f  static int dlrPo
34ed1 73 44 61 74 61 4c 65 6e 28 44 4c 52 65 61 64 65  sDataLen(DLReade
34ed2 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 73  r *pReader){.  s
34ed3 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 75 6d  qlite_int64 iDum
34ed4 6d 79 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 66 74  my;.  int n = ft
34ed5 73 33 47 65 74 56 61 72 69 6e 74 28 70 52 65 61  s3GetVarint(pRea
34ed6 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 69 44 75  der->pData, &iDu
34ed7 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20  mmy);.  assert( 
34ed8 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65  !dlrAtEnd(pReade
34ed9 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  r) );.  return p
34eda 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74  Reader->nElement
34edb 2d 6e 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  -n;.}.static voi
34edc 64 20 64 6c 72 53 74 65 70 28 44 4c 52 65 61 64  d dlrStep(DLRead
34edd 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
34ede 61 73 73 65 72 74 28 20 21 64 6c 72 41 74 45 6e  assert( !dlrAtEn
34edf 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 0a 20  d(pReader) );.. 
34ee0 20 2f 2a 20 53 6b 69 70 20 70 61 73 74 20 63 75   /* Skip past cu
34ee1 72 72 65 6e 74 20 64 6f 63 6c 69 73 74 20 65 6c  rrent doclist el
34ee2 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 61 73 73 65  ement. */.  asse
34ee3 72 74 28 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c  rt( pReader->nEl
34ee4 65 6d 65 6e 74 3c 3d 70 52 65 61 64 65 72 2d 3e  ement<=pReader->
34ee5 6e 44 61 74 61 20 29 3b 0a 20 20 70 52 65 61 64  nData );.  pRead
34ee6 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20 70 52 65  er->pData += pRe
34ee7 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3b 0a  ader->nElement;.
34ee8 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61    pReader->nData
34ee9 20 2d 3d 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c   -= pReader->nEl
34eea 65 6d 65 6e 74 3b 0a 0a 20 20 2f 2a 20 49 66 20  ement;..  /* If 
34eeb 74 68 65 72 65 20 69 73 20 6d 6f 72 65 20 64 61  there is more da
34eec 74 61 2c 20 72 65 61 64 20 74 68 65 20 6e 65 78  ta, read the nex
34eed 74 20 64 6f 63 6c 69 73 74 20 65 6c 65 6d 65 6e  t doclist elemen
34eee 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 52 65 61  t. */.  if( pRea
34eef 64 65 72 2d 3e 6e 44 61 74 61 21 3d 30 20 29 7b  der->nData!=0 ){
34ef0 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  .    sqlite_int6
34ef1 34 20 69 44 6f 63 69 64 44 65 6c 74 61 3b 0a 20  4 iDocidDelta;. 
34ef2 20 20 20 69 6e 74 20 69 44 75 6d 6d 79 2c 20 6e     int iDummy, n
34ef3 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
34ef4 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c  (pReader->pData,
34ef5 20 26 69 44 6f 63 69 64 44 65 6c 74 61 29 3b 0a   &iDocidDelta);.
34ef6 20 20 20 20 70 52 65 61 64 65 72 2d 3e 69 44 6f      pReader->iDo
34ef7 63 69 64 20 2b 3d 20 69 44 6f 63 69 64 44 65 6c  cid += iDocidDel
34ef8 74 61 3b 0a 20 20 20 20 69 66 28 20 70 52 65 61  ta;.    if( pRea
34ef9 64 65 72 2d 3e 69 54 79 70 65 3e 3d 44 4c 5f 50  der->iType>=DL_P
34efa 4f 53 49 54 49 4f 4e 53 20 29 7b 0a 20 20 20 20  OSITIONS ){.    
34efb 20 20 61 73 73 65 72 74 28 20 6e 3c 70 52 65 61    assert( n<pRea
34efc 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20  der->nData );.  
34efd 20 20 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a      while( 1 ){.
34efe 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74 73          n += fts
34eff 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65  3GetVarint32(pRe
34f00 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26  ader->pData+n, &
34f01 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20  iDummy);.       
34f02 20 61 73 73 65 72 74 28 20 6e 3c 3d 70 52 65 61   assert( n<=pRea
34f03 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20  der->nData );.  
34f04 20 20 20 20 20 20 69 66 28 20 69 44 75 6d 6d 79        if( iDummy
34f05 3d 3d 50 4f 53 5f 45 4e 44 20 29 20 62 72 65 61  ==POS_END ) brea
34f06 6b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  k;.        if( i
34f07 44 75 6d 6d 79 3d 3d 50 4f 53 5f 43 4f 4c 55 4d  Dummy==POS_COLUM
34f08 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  N ){.          n
34f09 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e   += fts3GetVarin
34f0a 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61  t32(pReader->pDa
34f0b 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  ta+n, &iDummy);.
34f0c 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
34f0d 28 20 6e 3c 70 52 65 61 64 65 72 2d 3e 6e 44 61  ( n<pReader->nDa
34f0e 74 61 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  ta );.        }e
34f0f 6c 73 65 20 69 66 28 20 70 52 65 61 64 65 72 2d  lse if( pReader-
34f10 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49 54  >iType==DL_POSIT
34f11 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 7b 0a  IONS_OFFSETS ){.
34f12 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66            n += f
34f13 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
34f14 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c  Reader->pData+n,
34f15 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20   &iDummy);.     
34f16 20 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65       n += fts3Ge
34f17 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65  tVarint32(pReade
34f18 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69 44 75  r->pData+n, &iDu
34f19 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20  mmy);.          
34f1a 61 73 73 65 72 74 28 20 6e 3c 70 52 65 61 64 65  assert( n<pReade
34f1b 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 20 20  r->nData );.    
34f1c 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
34f1d 20 20 7d 0a 20 20 20 20 70 52 65 61 64 65 72 2d    }.    pReader-
34f1e 3e 6e 45 6c 65 6d 65 6e 74 20 3d 20 6e 3b 0a 20  >nElement = n;. 
34f1f 20 20 20 61 73 73 65 72 74 28 20 70 52 65 61 64     assert( pRead
34f20 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3c 3d 70 52  er->nElement<=pR
34f21 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a  eader->nData );.
34f22 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69    }.}.static voi
34f23 64 20 64 6c 72 49 6e 69 74 28 44 4c 52 65 61 64  d dlrInit(DLRead
34f24 65 72 20 2a 70 52 65 61 64 65 72 2c 20 44 6f 63  er *pReader, Doc
34f25 4c 69 73 74 54 79 70 65 20 69 54 79 70 65 2c 0a  ListType iType,.
34f26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34f27 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
34f28 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61  pData, int nData
34f29 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 44 61  ){.  assert( pDa
34f2a 74 61 21 3d 4e 55 4c 4c 20 26 26 20 6e 44 61 74  ta!=NULL && nDat
34f2b 61 21 3d 30 20 29 3b 0a 20 20 70 52 65 61 64 65  a!=0 );.  pReade
34f2c 72 2d 3e 69 54 79 70 65 20 3d 20 69 54 79 70 65  r->iType = iType
34f2d 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61  ;.  pReader->pDa
34f2e 74 61 20 3d 20 70 44 61 74 61 3b 0a 20 20 70 52  ta = pData;.  pR
34f2f 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20 6e  eader->nData = n
34f30 44 61 74 61 3b 0a 20 20 70 52 65 61 64 65 72 2d  Data;.  pReader-
34f31 3e 6e 45 6c 65 6d 65 6e 74 20 3d 20 30 3b 0a 20  >nElement = 0;. 
34f32 20 70 52 65 61 64 65 72 2d 3e 69 44 6f 63 69 64   pReader->iDocid
34f33 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 4c 6f 61 64   = 0;..  /* Load
34f34 20 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65   the first eleme
34f35 6e 74 27 73 20 64 61 74 61 2e 20 20 54 68 65 72  nt's data.  Ther
34f36 65 20 6d 75 73 74 20 62 65 20 61 20 66 69 72 73  e must be a firs
34f37 74 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20  t element. */.  
34f38 64 6c 72 53 74 65 70 28 70 52 65 61 64 65 72 29  dlrStep(pReader)
34f39 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
34f3a 64 6c 72 44 65 73 74 72 6f 79 28 44 4c 52 65 61  dlrDestroy(DLRea
34f3b 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
34f3c 20 53 43 52 41 4d 42 4c 45 28 70 52 65 61 64 65   SCRAMBLE(pReade
34f3d 72 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e  r);.}..#ifndef N
34f3e 44 45 42 55 47 0a 2f 2a 20 56 65 72 69 66 79 20  DEBUG./* Verify 
34f3f 74 68 61 74 20 74 68 65 20 64 6f 63 6c 69 73 74  that the doclist
34f40 20 63 61 6e 20 62 65 20 76 61 6c 69 64 6c 79 20   can be validly 
34f41 64 65 63 6f 64 65 64 2e 20 20 41 6c 73 6f 20 72  decoded.  Also r
34f42 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 6c 61  eturns the.** la
34f43 73 74 20 64 6f 63 69 64 20 66 6f 75 6e 64 20 62  st docid found b
34f44 65 63 61 75 73 65 20 69 74 20 69 73 20 63 6f 6e  ecause it is con
34f45 76 65 6e 69 65 6e 74 20 69 6e 20 6f 74 68 65 72  venient in other
34f46 20 61 73 73 65 72 74 69 6f 6e 73 20 66 6f 72 0a   assertions for.
34f47 2a 2a 20 44 4c 57 72 69 74 65 72 2e 0a 2a 2f 0a  ** DLWriter..*/.
34f48 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c  static void docL
34f49 69 73 74 56 61 6c 69 64 61 74 65 28 44 6f 63 4c  istValidate(DocL
34f4a 69 73 74 54 79 70 65 20 69 54 79 70 65 2c 20 63  istType iType, c
34f4b 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61  onst char *pData
34f4c 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20  , int nData,.   
34f4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34f4e 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f           sqlite_
34f4f 69 6e 74 36 34 20 2a 70 4c 61 73 74 44 6f 63 69  int64 *pLastDoci
34f50 64 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  d){.  sqlite_int
34f51 36 34 20 69 50 72 65 76 44 6f 63 69 64 20 3d 20  64 iPrevDocid = 
34f52 30 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61  0;.  assert( nDa
34f53 74 61 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74  ta>0 );.  assert
34f54 28 20 70 44 61 74 61 21 3d 30 20 29 3b 0a 20 20  ( pData!=0 );.  
34f55 61 73 73 65 72 74 28 20 70 44 61 74 61 2b 6e 44  assert( pData+nD
34f56 61 74 61 3e 70 44 61 74 61 20 29 3b 0a 20 20 77  ata>pData );.  w
34f57 68 69 6c 65 28 20 6e 44 61 74 61 21 3d 30 20 29  hile( nData!=0 )
34f58 7b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74  {.    sqlite_int
34f59 36 34 20 69 44 6f 63 69 64 44 65 6c 74 61 3b 0a  64 iDocidDelta;.
34f5a 20 20 20 20 69 6e 74 20 6e 20 3d 20 66 74 73 33      int n = fts3
34f5b 47 65 74 56 61 72 69 6e 74 28 70 44 61 74 61 2c  GetVarint(pData,
34f5c 20 26 69 44 6f 63 69 64 44 65 6c 74 61 29 3b 0a   &iDocidDelta);.
34f5d 20 20 20 20 69 50 72 65 76 44 6f 63 69 64 20 2b      iPrevDocid +
34f5e 3d 20 69 44 6f 63 69 64 44 65 6c 74 61 3b 0a 20  = iDocidDelta;. 
34f5f 20 20 20 69 66 28 20 69 54 79 70 65 3e 44 4c 5f     if( iType>DL_
34f60 44 4f 43 49 44 53 20 29 7b 0a 20 20 20 20 20 20  DOCIDS ){.      
34f61 69 6e 74 20 69 44 75 6d 6d 79 3b 0a 20 20 20 20  int iDummy;.    
34f62 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20    while( 1 ){.  
34f63 20 20 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47        n += fts3G
34f64 65 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61  etVarint32(pData
34f65 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20  +n, &iDummy);.  
34f66 20 20 20 20 20 20 69 66 28 20 69 44 75 6d 6d 79        if( iDummy
34f67 3d 3d 50 4f 53 5f 45 4e 44 20 29 20 62 72 65 61  ==POS_END ) brea
34f68 6b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  k;.        if( i
34f69 44 75 6d 6d 79 3d 3d 50 4f 53 5f 43 4f 4c 55 4d  Dummy==POS_COLUM
34f6a 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  N ){.          n
34f6b 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e   += fts3GetVarin
34f6c 74 33 32 28 70 44 61 74 61 2b 6e 2c 20 26 69 44  t32(pData+n, &iD
34f6d 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d  ummy);.        }
34f6e 65 6c 73 65 20 69 66 28 20 69 54 79 70 65 3e 44  else if( iType>D
34f6f 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 29 7b 0a 20  L_POSITIONS ){. 
34f70 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74           n += ft
34f71 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44  s3GetVarint32(pD
34f72 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b  ata+n, &iDummy);
34f73 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20  .          n += 
34f74 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
34f75 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79  pData+n, &iDummy
34f76 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
34f77 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 3d       assert( n<=
34f78 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 20 20 7d  nData );.      }
34f79 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
34f7a 74 28 20 6e 3c 3d 6e 44 61 74 61 20 29 3b 0a 20  t( n<=nData );. 
34f7b 20 20 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20     pData += n;. 
34f7c 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a 20     nData -= n;. 
34f7d 20 7d 0a 20 20 69 66 28 20 70 4c 61 73 74 44 6f   }.  if( pLastDo
34f7e 63 69 64 20 29 20 2a 70 4c 61 73 74 44 6f 63 69  cid ) *pLastDoci
34f7f 64 20 3d 20 69 50 72 65 76 44 6f 63 69 64 3b 0a  d = iPrevDocid;.
34f80 7d 0a 23 64 65 66 69 6e 65 20 41 53 53 45 52 54  }.#define ASSERT
34f81 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 69  _VALID_DOCLIST(i
34f82 2c 20 70 2c 20 6e 2c 20 6f 29 20 64 6f 63 4c 69  , p, n, o) docLi
34f83 73 74 56 61 6c 69 64 61 74 65 28 69 2c 20 70 2c  stValidate(i, p,
34f84 20 6e 2c 20 6f 29 0a 23 65 6c 73 65 0a 23 64 65   n, o).#else.#de
34f85 66 69 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49  fine ASSERT_VALI
34f86 44 5f 44 4f 43 4c 49 53 54 28 69 2c 20 70 2c 20  D_DOCLIST(i, p, 
34f87 6e 2c 20 6f 29 20 61 73 73 65 72 74 28 20 31 20  n, o) assert( 1 
34f88 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  ).#endif../*****
34f89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
34f8d 2f 2a 20 44 4c 57 72 69 74 65 72 20 69 73 20 75  /* DLWriter is u
34f8e 73 65 64 20 74 6f 20 77 72 69 74 65 20 64 6f 63  sed to write doc
34f8f 6c 69 73 74 20 64 61 74 61 20 74 6f 20 61 20 44  list data to a D
34f90 61 74 61 42 75 66 66 65 72 2e 20 20 44 4c 57 72  ataBuffer.  DLWr
34f91 69 74 65 72 0a 2a 2a 20 61 6c 77 61 79 73 20 61  iter.** always a
34f92 70 70 65 6e 64 73 20 74 6f 20 74 68 65 20 62 75  ppends to the bu
34f93 66 66 65 72 20 61 6e 64 20 64 6f 65 73 20 6e 6f  ffer and does no
34f94 74 20 6f 77 6e 20 69 74 2e 0a 2a 2a 0a 2a 2a 20  t own it..**.** 
34f95 64 6c 77 49 6e 69 74 20 2d 20 69 6e 69 74 69 61  dlwInit - initia
34f96 6c 69 7a 65 20 74 6f 20 77 72 69 74 65 20 61 20  lize to write a 
34f97 67 69 76 65 6e 20 74 79 70 65 20 64 6f 63 6c 69  given type docli
34f98 73 74 74 6f 20 61 20 62 75 66 66 65 72 2e 0a 2a  stto a buffer..*
34f99 2a 20 64 6c 77 44 65 73 74 72 6f 79 20 2d 20 63  * dlwDestroy - c
34f9a 6c 65 61 72 20 74 68 65 20 77 72 69 74 65 72 27  lear the writer'
34f9b 73 20 6d 65 6d 6f 72 79 2e 20 20 44 6f 65 73 20  s memory.  Does 
34f9c 6e 6f 74 20 66 72 65 65 20 62 75 66 66 65 72 2e  not free buffer.
34f9d 0a 2a 2a 20 64 6c 77 41 70 70 65 6e 64 20 2d 20  .** dlwAppend - 
34f9e 61 70 70 65 6e 64 20 72 61 77 20 64 6f 63 6c 69  append raw docli
34f9f 73 74 20 64 61 74 61 20 74 6f 20 62 75 66 66 65  st data to buffe
34fa0 72 2e 0a 2a 2a 20 64 6c 77 43 6f 70 79 20 2d 20  r..** dlwCopy - 
34fa1 63 6f 70 79 20 6e 65 78 74 20 64 6f 63 6c 69 73  copy next doclis
34fa2 74 20 66 72 6f 6d 20 72 65 61 64 65 72 20 74 6f  t from reader to
34fa3 20 77 72 69 74 65 72 2e 0a 2a 2a 20 64 6c 77 41   writer..** dlwA
34fa4 64 64 20 2d 20 63 6f 6e 73 74 72 75 63 74 20 64  dd - construct d
34fa5 6f 63 6c 69 73 74 20 65 6c 65 6d 65 6e 74 20 61  oclist element a
34fa6 6e 64 20 61 70 70 65 6e 64 20 74 6f 20 62 75 66  nd append to buf
34fa7 66 65 72 2e 0a 2a 2a 20 20 20 20 4f 6e 6c 79 20  fer..**    Only 
34fa8 61 70 70 6c 79 20 64 6c 77 41 64 64 28 29 20 74  apply dlwAdd() t
34fa9 6f 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c  o DL_DOCIDS docl
34faa 69 73 74 73 20 28 65 6c 73 65 20 75 73 65 20 50  ists (else use P
34fab 4c 57 72 69 74 65 72 29 2e 0a 2a 2f 0a 74 79 70  LWriter)..*/.typ
34fac 65 64 65 66 20 73 74 72 75 63 74 20 44 4c 57 72  edef struct DLWr
34fad 69 74 65 72 20 7b 0a 20 20 44 6f 63 4c 69 73 74  iter {.  DocList
34fae 54 79 70 65 20 69 54 79 70 65 3b 0a 20 20 44 61  Type iType;.  Da
34faf 74 61 42 75 66 66 65 72 20 2a 62 3b 0a 20 20 73  taBuffer *b;.  s
34fb0 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 50 72 65  qlite_int64 iPre
34fb1 76 44 6f 63 69 64 3b 0a 23 69 66 6e 64 65 66 20  vDocid;.#ifndef 
34fb2 4e 44 45 42 55 47 0a 20 20 69 6e 74 20 68 61 73  NDEBUG.  int has
34fb3 5f 69 50 72 65 76 44 6f 63 69 64 3b 0a 23 65 6e  _iPrevDocid;.#en
34fb4 64 69 66 0a 7d 20 44 4c 57 72 69 74 65 72 3b 0a  dif.} DLWriter;.
34fb5 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c 77  .static void dlw
34fb6 49 6e 69 74 28 44 4c 57 72 69 74 65 72 20 2a 70  Init(DLWriter *p
34fb7 57 72 69 74 65 72 2c 20 44 6f 63 4c 69 73 74 54  Writer, DocListT
34fb8 79 70 65 20 69 54 79 70 65 2c 20 44 61 74 61 42  ype iType, DataB
34fb9 75 66 66 65 72 20 2a 62 29 7b 0a 20 20 70 57 72  uffer *b){.  pWr
34fba 69 74 65 72 2d 3e 62 20 3d 20 62 3b 0a 20 20 70  iter->b = b;.  p
34fbb 57 72 69 74 65 72 2d 3e 69 54 79 70 65 20 3d 20  Writer->iType = 
34fbc 69 54 79 70 65 3b 0a 20 20 70 57 72 69 74 65 72  iType;.  pWriter
34fbd 2d 3e 69 50 72 65 76 44 6f 63 69 64 20 3d 20 30  ->iPrevDocid = 0
34fbe 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ;.#ifndef NDEBUG
34fbf 0a 20 20 70 57 72 69 74 65 72 2d 3e 68 61 73 5f  .  pWriter->has_
34fc0 69 50 72 65 76 44 6f 63 69 64 20 3d 20 30 3b 0a  iPrevDocid = 0;.
34fc1 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74 69 63 20  #endif.}.static 
34fc2 76 6f 69 64 20 64 6c 77 44 65 73 74 72 6f 79 28  void dlwDestroy(
34fc3 44 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  DLWriter *pWrite
34fc4 72 29 7b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70  r){.  SCRAMBLE(p
34fc5 57 72 69 74 65 72 29 3b 0a 7d 0a 2f 2a 20 69 46  Writer);.}./* iF
34fc6 69 72 73 74 44 6f 63 69 64 20 69 73 20 74 68 65  irstDocid is the
34fc7 20 66 69 72 73 74 20 64 6f 63 69 64 20 69 6e 20   first docid in 
34fc8 74 68 65 20 64 6f 63 6c 69 73 74 20 69 6e 20 70  the doclist in p
34fc9 44 61 74 61 2e 20 20 49 74 20 69 73 0a 2a 2a 20  Data.  It is.** 
34fca 6e 65 65 64 65 64 20 62 65 63 61 75 73 65 20 70  needed because p
34fcb 44 61 74 61 20 6d 61 79 20 70 6f 69 6e 74 20 77  Data may point w
34fcc 69 74 68 69 6e 20 61 20 6c 61 72 67 65 72 20 64  ithin a larger d
34fcd 6f 63 6c 69 73 74 2c 20 69 6e 20 77 68 69 63 68  oclist, in which
34fce 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 66 69 72  .** case the fir
34fcf 73 74 20 69 74 65 6d 20 77 6f 75 6c 64 20 62 65  st item would be
34fd0 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 2e 0a   delta-encoded..
34fd1 2a 2a 0a 2a 2a 20 69 4c 61 73 74 44 6f 63 69 64  **.** iLastDocid
34fd2 20 69 73 20 74 68 65 20 66 69 6e 61 6c 20 64 6f   is the final do
34fd3 63 69 64 20 69 6e 20 74 68 65 20 64 6f 63 6c 69  cid in the docli
34fd4 73 74 20 69 6e 20 70 44 61 74 61 2e 20 20 49 74  st in pData.  It
34fd5 20 69 73 0a 2a 2a 20 6e 65 65 64 65 64 20 74 6f   is.** needed to
34fd6 20 63 72 65 61 74 65 20 74 68 65 20 6e 65 77 20   create the new 
34fd7 69 50 72 65 76 44 6f 63 69 64 20 66 6f 72 20 66  iPrevDocid for f
34fd8 75 74 75 72 65 20 64 65 6c 74 61 2d 65 6e 63 6f  uture delta-enco
34fd9 64 69 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 63 6f  ding.  The.** co
34fda 64 65 20 63 6f 75 6c 64 20 64 65 63 6f 64 65 20  de could decode 
34fdb 74 68 65 20 70 61 73 73 65 64 20 64 6f 63 6c 69  the passed docli
34fdc 73 74 20 74 6f 20 72 65 63 72 65 61 74 65 20 69  st to recreate i
34fdd 4c 61 73 74 44 6f 63 69 64 2c 20 62 75 74 0a 2a  LastDocid, but.*
34fde 2a 20 74 68 65 20 6f 6e 6c 79 20 63 75 72 72 65  * the only curre
34fdf 6e 74 20 75 73 65 72 20 28 64 6f 63 4c 69 73 74  nt user (docList
34fe0 4d 65 72 67 65 29 20 61 6c 72 65 61 64 79 20 68  Merge) already h
34fe1 61 73 20 64 65 63 6f 64 65 64 20 74 68 69 73 0a  as decoded this.
34fe2 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  ** information..
34fe3 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  */./* TODO(shess
34fe4 29 20 54 68 69 73 20 68 61 73 20 62 65 63 6f 6d  ) This has becom
34fe5 65 20 6a 75 73 74 20 61 20 68 65 6c 70 65 72 20  e just a helper 
34fe6 66 6f 72 20 64 6f 63 4c 69 73 74 4d 65 72 67 65  for docListMerge
34fe7 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20 61 20  ..** Consider a 
34fe8 72 65 66 61 63 74 6f 72 20 74 6f 20 6d 61 6b 65  refactor to make
34fe9 20 74 68 69 73 20 63 6c 65 61 6e 65 72 2e 0a 2a   this cleaner..*
34fea 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c  /.static void dl
34feb 77 41 70 70 65 6e 64 28 44 4c 57 72 69 74 65 72  wAppend(DLWriter
34fec 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20   *pWriter,.     
34fed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34fee 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61   const char *pDa
34fef 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20  ta, int nData,. 
34ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34ff1 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
34ff2 34 20 69 46 69 72 73 74 44 6f 63 69 64 2c 20 73  4 iFirstDocid, s
34ff3 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c 61 73  qlite_int64 iLas
34ff4 74 44 6f 63 69 64 29 7b 0a 20 20 73 71 6c 69 74  tDocid){.  sqlit
34ff5 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 20 3d  e_int64 iDocid =
34ff6 20 30 3b 0a 20 20 63 68 61 72 20 63 5b 56 41 52   0;.  char c[VAR
34ff7 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20  INT_MAX];.  int 
34ff8 6e 46 69 72 73 74 4f 6c 64 2c 20 6e 46 69 72 73  nFirstOld, nFirs
34ff9 74 4e 65 77 3b 20 20 20 20 20 2f 2a 20 4f 6c 64  tNew;     /* Old
34ffa 20 61 6e 64 20 6e 65 77 20 76 61 72 69 6e 74 20   and new varint 
34ffb 6c 65 6e 20 6f 66 20 66 69 72 73 74 20 64 6f 63  len of first doc
34ffc 69 64 2e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 4e  id. */.#ifndef N
34ffd 44 45 42 55 47 0a 20 20 73 71 6c 69 74 65 5f 69  DEBUG.  sqlite_i
34ffe 6e 74 36 34 20 69 4c 61 73 74 44 6f 63 69 64 44  nt64 iLastDocidD
34fff 65 6c 74 61 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  elta;.#endif..  
35000 2f 2a 20 52 65 63 6f 64 65 20 74 68 65 20 69 6e  /* Recode the in
35001 69 74 69 61 6c 20 64 6f 63 69 64 20 61 73 20 64  itial docid as d
35002 65 6c 74 61 20 66 72 6f 6d 20 69 50 72 65 76 44  elta from iPrevD
35003 6f 63 69 64 2e 20 2a 2f 0a 20 20 6e 46 69 72 73  ocid. */.  nFirs
35004 74 4f 6c 64 20 3d 20 66 74 73 33 47 65 74 56 61  tOld = fts3GetVa
35005 72 69 6e 74 28 70 44 61 74 61 2c 20 26 69 44 6f  rint(pData, &iDo
35006 63 69 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20  cid);.  assert( 
35007 6e 46 69 72 73 74 4f 6c 64 3c 6e 44 61 74 61 20  nFirstOld<nData 
35008 7c 7c 20 28 6e 46 69 72 73 74 4f 6c 64 3d 3d 6e  || (nFirstOld==n
35009 44 61 74 61 20 26 26 20 70 57 72 69 74 65 72 2d  Data && pWriter-
3500a 3e 69 54 79 70 65 3d 3d 44 4c 5f 44 4f 43 49 44  >iType==DL_DOCID
3500b 53 29 20 29 3b 0a 20 20 6e 46 69 72 73 74 4e 65  S) );.  nFirstNe
3500c 77 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e  w = fts3PutVarin
3500d 74 28 63 2c 20 69 46 69 72 73 74 44 6f 63 69 64  t(c, iFirstDocid
3500e 2d 70 57 72 69 74 65 72 2d 3e 69 50 72 65 76 44  -pWriter->iPrevD
3500f 6f 63 69 64 29 3b 0a 0a 20 20 2f 2a 20 56 65 72  ocid);..  /* Ver
35010 69 66 79 20 74 68 61 74 20 74 68 65 20 69 6e 63  ify that the inc
35011 6f 6d 69 6e 67 20 64 6f 63 6c 69 73 74 20 69 73  oming doclist is
35012 20 76 61 6c 69 64 20 41 4e 44 20 74 68 61 74 20   valid AND that 
35013 69 74 20 65 6e 64 73 20 77 69 74 68 0a 20 20 2a  it ends with.  *
35014 2a 20 74 68 65 20 65 78 70 65 63 74 65 64 20 64  * the expected d
35015 6f 63 69 64 2e 20 20 54 68 69 73 20 69 73 20 65  ocid.  This is e
35016 73 73 65 6e 74 69 61 6c 20 62 65 63 61 75 73 65  ssential because
35017 20 77 65 27 6c 6c 20 74 72 75 73 74 20 74 68 69   we'll trust thi
35018 73 0a 20 20 2a 2a 20 64 6f 63 69 64 20 69 6e 20  s.  ** docid in 
35019 66 75 74 75 72 65 20 64 65 6c 74 61 2d 65 6e 63  future delta-enc
3501a 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 41 53  oding..  */.  AS
3501b 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c 49  SERT_VALID_DOCLI
3501c 53 54 28 70 57 72 69 74 65 72 2d 3e 69 54 79 70  ST(pWriter->iTyp
3501d 65 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c  e, pData, nData,
3501e 20 26 69 4c 61 73 74 44 6f 63 69 64 44 65 6c 74   &iLastDocidDelt
3501f 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4c  a);.  assert( iL
35020 61 73 74 44 6f 63 69 64 3d 3d 69 46 69 72 73 74  astDocid==iFirst
35021 44 6f 63 69 64 2d 69 44 6f 63 69 64 2b 69 4c 61  Docid-iDocid+iLa
35022 73 74 44 6f 63 69 64 44 65 6c 74 61 20 29 3b 0a  stDocidDelta );.
35023 0a 20 20 2f 2a 20 41 70 70 65 6e 64 20 72 65 63  .  /* Append rec
35024 6f 64 65 64 20 69 6e 69 74 69 61 6c 20 64 6f 63  oded initial doc
35025 69 64 20 61 6e 64 20 65 76 65 72 79 74 68 69 6e  id and everythin
35026 67 20 65 6c 73 65 2e 20 20 52 65 73 74 20 6f 66  g else.  Rest of
35027 20 64 6f 63 69 64 73 0a 20 20 2a 2a 20 73 68 6f   docids.  ** sho
35028 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 64 65  uld have been de
35029 6c 74 61 2d 65 6e 63 6f 64 65 64 20 66 72 6f 6d  lta-encoded from
3502a 20 70 72 65 76 69 6f 75 73 20 69 6e 69 74 69 61   previous initia
3502b 6c 20 64 6f 63 69 64 2e 0a 20 20 2a 2f 0a 20 20  l docid..  */.  
3502c 69 66 28 20 6e 46 69 72 73 74 4f 6c 64 3c 6e 44  if( nFirstOld<nD
3502d 61 74 61 20 29 7b 0a 20 20 20 20 64 61 74 61 42  ata ){.    dataB
3502e 75 66 66 65 72 41 70 70 65 6e 64 32 28 70 57 72  ufferAppend2(pWr
3502f 69 74 65 72 2d 3e 62 2c 20 63 2c 20 6e 46 69 72  iter->b, c, nFir
35030 73 74 4e 65 77 2c 0a 20 20 20 20 20 20 20 20 20  stNew,.         
35031 20 20 20 20 20 20 20 20 20 20 20 20 20 70 44 61               pDa
35032 74 61 2b 6e 46 69 72 73 74 4f 6c 64 2c 20 6e 44  ta+nFirstOld, nD
35033 61 74 61 2d 6e 46 69 72 73 74 4f 6c 64 29 3b 0a  ata-nFirstOld);.
35034 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 61 74    }else{.    dat
35035 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 57  aBufferAppend(pW
35036 72 69 74 65 72 2d 3e 62 2c 20 63 2c 20 6e 46 69  riter->b, c, nFi
35037 72 73 74 4e 65 77 29 3b 0a 20 20 7d 0a 20 20 70  rstNew);.  }.  p
35038 57 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f 63  Writer->iPrevDoc
35039 69 64 20 3d 20 69 4c 61 73 74 44 6f 63 69 64 3b  id = iLastDocid;
3503a 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  .}.static void d
3503b 6c 77 43 6f 70 79 28 44 4c 57 72 69 74 65 72 20  lwCopy(DLWriter 
3503c 2a 70 57 72 69 74 65 72 2c 20 44 4c 52 65 61 64  *pWriter, DLRead
3503d 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
3503e 64 6c 77 41 70 70 65 6e 64 28 70 57 72 69 74 65  dlwAppend(pWrite
3503f 72 2c 20 64 6c 72 44 6f 63 44 61 74 61 28 70 52  r, dlrDocData(pR
35040 65 61 64 65 72 29 2c 20 64 6c 72 44 6f 63 44 61  eader), dlrDocDa
35041 74 61 42 79 74 65 73 28 70 52 65 61 64 65 72 29  taBytes(pReader)
35042 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 6c  ,.            dl
35043 72 44 6f 63 69 64 28 70 52 65 61 64 65 72 29 2c  rDocid(pReader),
35044 20 64 6c 72 44 6f 63 69 64 28 70 52 65 61 64 65   dlrDocid(pReade
35045 72 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  r));.}.static vo
35046 69 64 20 64 6c 77 41 64 64 28 44 4c 57 72 69 74  id dlwAdd(DLWrit
35047 65 72 20 2a 70 57 72 69 74 65 72 2c 20 73 71 6c  er *pWriter, sql
35048 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64  ite_int64 iDocid
35049 29 7b 0a 20 20 63 68 61 72 20 63 5b 56 41 52 49  ){.  char c[VARI
3504a 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 6e  NT_MAX];.  int n
3504b 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74   = fts3PutVarint
3504c 28 63 2c 20 69 44 6f 63 69 64 2d 70 57 72 69 74  (c, iDocid-pWrit
3504d 65 72 2d 3e 69 50 72 65 76 44 6f 63 69 64 29 3b  er->iPrevDocid);
3504e 0a 0a 20 20 2f 2a 20 44 6f 63 69 64 73 20 6d 75  ..  /* Docids mu
3504f 73 74 20 61 73 63 65 6e 64 2e 20 2a 2f 0a 20 20  st ascend. */.  
35050 61 73 73 65 72 74 28 20 21 70 57 72 69 74 65 72  assert( !pWriter
35051 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64  ->has_iPrevDocid
35052 20 7c 7c 20 69 44 6f 63 69 64 3e 70 57 72 69 74   || iDocid>pWrit
35053 65 72 2d 3e 69 50 72 65 76 44 6f 63 69 64 20 29  er->iPrevDocid )
35054 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57 72 69  ;.  assert( pWri
35055 74 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 44  ter->iType==DL_D
35056 4f 43 49 44 53 20 29 3b 0a 0a 20 20 64 61 74 61  OCIDS );..  data
35057 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 57 72  BufferAppend(pWr
35058 69 74 65 72 2d 3e 62 2c 20 63 2c 20 6e 29 3b 0a  iter->b, c, n);.
35059 20 20 70 57 72 69 74 65 72 2d 3e 69 50 72 65 76    pWriter->iPrev
3505a 44 6f 63 69 64 20 3d 20 69 44 6f 63 69 64 3b 0a  Docid = iDocid;.
3505b 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
3505c 20 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 69 50   pWriter->has_iP
3505d 72 65 76 44 6f 63 69 64 20 3d 20 31 3b 0a 23 65  revDocid = 1;.#e
3505e 6e 64 69 66 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  ndif.}../*******
3505f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35061 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35062 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
35063 20 50 4c 52 65 61 64 65 72 20 69 73 20 75 73 65   PLReader is use
35064 64 20 74 6f 20 72 65 61 64 20 64 61 74 61 20 66  d to read data f
35065 72 6f 6d 20 61 20 64 6f 63 75 6d 65 6e 74 27 73  rom a document's
35066 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 2e 20   position list. 
35067 20 41 73 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65   As.** the calle
35068 72 20 73 74 65 70 73 20 74 68 72 6f 75 67 68 20  r steps through 
35069 74 68 65 20 6c 69 73 74 2c 20 64 61 74 61 20 69  the list, data i
3506a 73 20 63 61 63 68 65 64 20 73 6f 20 74 68 61 74  s cached so that
3506b 20 76 61 72 69 6e 74 73 0a 2a 2a 20 6f 6e 6c 79   varints.** only
3506c 20 6e 65 65 64 20 74 6f 20 62 65 20 64 65 63 6f   need to be deco
3506d 64 65 64 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20  ded once..**.** 
3506e 70 6c 72 49 6e 69 74 2c 20 70 6c 72 44 65 73 74  plrInit, plrDest
3506f 72 6f 79 20 2d 20 63 72 65 61 74 65 2f 64 65 73  roy - create/des
35070 74 72 6f 79 20 61 20 72 65 61 64 65 72 2e 0a 2a  troy a reader..*
35071 2a 20 70 6c 72 43 6f 6c 75 6d 6e 2c 20 70 6c 72  * plrColumn, plr
35072 50 6f 73 69 74 69 6f 6e 2c 20 70 6c 72 53 74 61  Position, plrSta
35073 72 74 4f 66 66 73 65 74 2c 20 70 6c 72 45 6e 64  rtOffset, plrEnd
35074 4f 66 66 73 65 74 20 2d 20 61 63 63 65 73 73 6f  Offset - accesso
35075 72 73 0a 2a 2a 20 70 6c 72 41 74 45 6e 64 20 2d  rs.** plrAtEnd -
35076 20 61 74 20 65 6e 64 20 6f 66 20 73 74 72 65 61   at end of strea
35077 6d 2c 20 6f 6e 6c 79 20 63 61 6c 6c 20 70 6c 72  m, only call plr
35078 44 65 73 74 72 6f 79 20 6f 6e 63 65 20 74 72 75  Destroy once tru
35079 65 2e 0a 2a 2a 20 70 6c 72 53 74 65 70 20 2d 20  e..** plrStep - 
3507a 73 74 65 70 20 74 6f 20 74 68 65 20 6e 65 78 74  step to the next
3507b 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 74 79 70   element..*/.typ
3507c 65 64 65 66 20 73 74 72 75 63 74 20 50 4c 52 65  edef struct PLRe
3507d 61 64 65 72 20 7b 0a 20 20 2f 2a 20 54 68 65 73  ader {.  /* Thes
3507e 65 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 6e  e refer to the n
3507f 65 78 74 20 70 6f 73 69 74 69 6f 6e 27 73 20 64  ext position's d
35080 61 74 61 2e 20 20 6e 44 61 74 61 20 77 69 6c 6c  ata.  nData will
35081 20 72 65 61 63 68 20 30 20 77 68 65 6e 0a 20 20   reach 0 when.  
35082 2a 2a 20 72 65 61 64 69 6e 67 20 74 68 65 20 6c  ** reading the l
35083 61 73 74 20 70 6f 73 69 74 69 6f 6e 2c 20 73 6f  ast position, so
35084 20 70 6c 72 53 74 65 70 28 29 20 73 69 67 6e 61   plrStep() signa
35085 6c 73 20 45 4f 46 20 62 79 20 73 65 74 74 69 6e  ls EOF by settin
35086 67 0a 20 20 2a 2a 20 70 44 61 74 61 20 74 6f 20  g.  ** pData to 
35087 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 63 6f 6e  NULL..  */.  con
35088 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 3b 0a  st char *pData;.
35089 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a 0a 20 20    int nData;..  
3508a 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79 70  DocListType iTyp
3508b 65 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  e;.  int iColumn
3508c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65  ;         /* the
3508d 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 72 65 61   last column rea
3508e 64 20 2a 2f 0a 20 20 69 6e 74 20 69 50 6f 73 69  d */.  int iPosi
3508f 74 69 6f 6e 3b 20 20 20 20 20 20 20 2f 2a 20 74  tion;       /* t
35090 68 65 20 6c 61 73 74 20 70 6f 73 69 74 69 6f 6e  he last position
35091 20 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20 69   read */.  int i
35092 53 74 61 72 74 4f 66 66 73 65 74 3b 20 20 20 20  StartOffset;    
35093 2f 2a 20 74 68 65 20 6c 61 73 74 20 73 74 61 72  /* the last star
35094 74 20 6f 66 66 73 65 74 20 72 65 61 64 20 2a 2f  t offset read */
35095 0a 20 20 69 6e 74 20 69 45 6e 64 4f 66 66 73 65  .  int iEndOffse
35096 74 3b 20 20 20 20 20 20 2f 2a 20 74 68 65 20 6c  t;      /* the l
35097 61 73 74 20 65 6e 64 20 6f 66 66 73 65 74 20 72  ast end offset r
35098 65 61 64 20 2a 2f 0a 7d 20 50 4c 52 65 61 64 65  ead */.} PLReade
35099 72 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 70  r;..static int p
3509a 6c 72 41 74 45 6e 64 28 50 4c 52 65 61 64 65 72  lrAtEnd(PLReader
3509b 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65   *pReader){.  re
3509c 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 70 44  turn pReader->pD
3509d 61 74 61 3d 3d 4e 55 4c 4c 3b 0a 7d 0a 73 74 61  ata==NULL;.}.sta
3509e 74 69 63 20 69 6e 74 20 70 6c 72 43 6f 6c 75 6d  tic int plrColum
3509f 6e 28 50 4c 52 65 61 64 65 72 20 2a 70 52 65 61  n(PLReader *pRea
350a0 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20  der){.  assert( 
350a1 21 70 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65  !plrAtEnd(pReade
350a2 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  r) );.  return p
350a3 52 65 61 64 65 72 2d 3e 69 43 6f 6c 75 6d 6e 3b  Reader->iColumn;
350a4 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6c  .}.static int pl
350a5 72 50 6f 73 69 74 69 6f 6e 28 50 4c 52 65 61 64  rPosition(PLRead
350a6 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
350a7 61 73 73 65 72 74 28 20 21 70 6c 72 41 74 45 6e  assert( !plrAtEn
350a8 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20  d(pReader) );.  
350a9 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
350aa 69 50 6f 73 69 74 69 6f 6e 3b 0a 7d 0a 73 74 61  iPosition;.}.sta
350ab 74 69 63 20 69 6e 74 20 70 6c 72 53 74 61 72 74  tic int plrStart
350ac 4f 66 66 73 65 74 28 50 4c 52 65 61 64 65 72 20  Offset(PLReader 
350ad 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73  *pReader){.  ass
350ae 65 72 74 28 20 21 70 6c 72 41 74 45 6e 64 28 70  ert( !plrAtEnd(p
350af 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74  Reader) );.  ret
350b0 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 69 53 74  urn pReader->iSt
350b1 61 72 74 4f 66 66 73 65 74 3b 0a 7d 0a 73 74 61  artOffset;.}.sta
350b2 74 69 63 20 69 6e 74 20 70 6c 72 45 6e 64 4f 66  tic int plrEndOf
350b3 66 73 65 74 28 50 4c 52 65 61 64 65 72 20 2a 70  fset(PLReader *p
350b4 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72  Reader){.  asser
350b5 74 28 20 21 70 6c 72 41 74 45 6e 64 28 70 52 65  t( !plrAtEnd(pRe
350b6 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72  ader) );.  retur
350b7 6e 20 70 52 65 61 64 65 72 2d 3e 69 45 6e 64 4f  n pReader->iEndO
350b8 66 66 73 65 74 3b 0a 7d 0a 73 74 61 74 69 63 20  ffset;.}.static 
350b9 76 6f 69 64 20 70 6c 72 53 74 65 70 28 50 4c 52  void plrStep(PLR
350ba 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
350bb 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 0a 20 20  .  int i, n;..  
350bc 61 73 73 65 72 74 28 20 21 70 6c 72 41 74 45 6e  assert( !plrAtEn
350bd 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 0a 20  d(pReader) );.. 
350be 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e 6e 44   if( pReader->nD
350bf 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 70 52  ata==0 ){.    pR
350c0 65 61 64 65 72 2d 3e 70 44 61 74 61 20 3d 20 4e  eader->pData = N
350c1 55 4c 4c 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ULL;.    return;
350c2 0a 20 20 7d 0a 0a 20 20 6e 20 3d 20 66 74 73 33  .  }..  n = fts3
350c3 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61  GetVarint32(pRea
350c4 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 69 29 3b  der->pData, &i);
350c5 0a 20 20 69 66 28 20 69 3d 3d 50 4f 53 5f 43 4f  .  if( i==POS_CO
350c6 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 6e 20 2b 3d  LUMN ){.    n +=
350c7 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
350c8 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b  (pReader->pData+
350c9 6e 2c 20 26 70 52 65 61 64 65 72 2d 3e 69 43 6f  n, &pReader->iCo
350ca 6c 75 6d 6e 29 3b 0a 20 20 20 20 70 52 65 61 64  lumn);.    pRead
350cb 65 72 2d 3e 69 50 6f 73 69 74 69 6f 6e 20 3d 20  er->iPosition = 
350cc 30 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  0;.    pReader->
350cd 69 53 74 61 72 74 4f 66 66 73 65 74 20 3d 20 30  iStartOffset = 0
350ce 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47  ;.    n += fts3G
350cf 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64  etVarint32(pRead
350d0 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69 29  er->pData+n, &i)
350d1 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 68 6f 75 6c  ;.  }.  /* Shoul
350d2 64 20 6e 65 76 65 72 20 73 65 65 20 61 64 6a 61  d never see adja
350d3 63 65 6e 74 20 63 6f 6c 75 6d 6e 20 63 68 61 6e  cent column chan
350d4 67 65 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  ges. */.  assert
350d5 28 20 69 21 3d 50 4f 53 5f 43 4f 4c 55 4d 4e 20  ( i!=POS_COLUMN 
350d6 29 3b 0a 0a 20 20 69 66 28 20 69 3d 3d 50 4f 53  );..  if( i==POS
350d7 5f 45 4e 44 20 29 7b 0a 20 20 20 20 70 52 65 61  _END ){.    pRea
350d8 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a  der->nData = 0;.
350d9 20 20 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61      pReader->pDa
350da 74 61 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72  ta = NULL;.    r
350db 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 52  eturn;.  }..  pR
350dc 65 61 64 65 72 2d 3e 69 50 6f 73 69 74 69 6f 6e  eader->iPosition
350dd 20 2b 3d 20 69 2d 50 4f 53 5f 42 41 53 45 3b 0a   += i-POS_BASE;.
350de 20 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e 69    if( pReader->i
350df 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f  Type==DL_POSITIO
350e0 4e 53 5f 4f 46 46 53 45 54 53 20 29 7b 0a 20 20  NS_OFFSETS ){.  
350e1 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61    n += fts3GetVa
350e2 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e  rint32(pReader->
350e3 70 44 61 74 61 2b 6e 2c 20 26 69 29 3b 0a 20 20  pData+n, &i);.  
350e4 20 20 70 52 65 61 64 65 72 2d 3e 69 53 74 61 72    pReader->iStar
350e5 74 4f 66 66 73 65 74 20 2b 3d 20 69 3b 0a 20 20  tOffset += i;.  
350e6 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61    n += fts3GetVa
350e7 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e  rint32(pReader->
350e8 70 44 61 74 61 2b 6e 2c 20 26 69 29 3b 0a 20 20  pData+n, &i);.  
350e9 20 20 70 52 65 61 64 65 72 2d 3e 69 45 6e 64 4f    pReader->iEndO
350ea 66 66 73 65 74 20 3d 20 70 52 65 61 64 65 72 2d  ffset = pReader-
350eb 3e 69 53 74 61 72 74 4f 66 66 73 65 74 2b 69 3b  >iStartOffset+i;
350ec 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e  .  }.  assert( n
350ed 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61  <=pReader->nData
350ee 20 29 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 70   );.  pReader->p
350ef 44 61 74 61 20 2b 3d 20 6e 3b 0a 20 20 70 52 65  Data += n;.  pRe
350f0 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d 20 6e  ader->nData -= n
350f1 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64  ;.}..static void
350f2 20 70 6c 72 49 6e 69 74 28 50 4c 52 65 61 64 65   plrInit(PLReade
350f3 72 20 2a 70 52 65 61 64 65 72 2c 20 44 4c 52 65  r *pReader, DLRe
350f4 61 64 65 72 20 2a 70 44 4c 52 65 61 64 65 72 29  ader *pDLReader)
350f5 7b 0a 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61  {.  pReader->pDa
350f6 74 61 20 3d 20 64 6c 72 50 6f 73 44 61 74 61 28  ta = dlrPosData(
350f7 70 44 4c 52 65 61 64 65 72 29 3b 0a 20 20 70 52  pDLReader);.  pR
350f8 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20 64  eader->nData = d
350f9 6c 72 50 6f 73 44 61 74 61 4c 65 6e 28 70 44 4c  lrPosDataLen(pDL
350fa 52 65 61 64 65 72 29 3b 0a 20 20 70 52 65 61 64  Reader);.  pRead
350fb 65 72 2d 3e 69 54 79 70 65 20 3d 20 70 44 4c 52  er->iType = pDLR
350fc 65 61 64 65 72 2d 3e 69 54 79 70 65 3b 0a 20 20  eader->iType;.  
350fd 70 52 65 61 64 65 72 2d 3e 69 43 6f 6c 75 6d 6e  pReader->iColumn
350fe 20 3d 20 30 3b 0a 20 20 70 52 65 61 64 65 72 2d   = 0;.  pReader-
350ff 3e 69 50 6f 73 69 74 69 6f 6e 20 3d 20 30 3b 0a  >iPosition = 0;.
35100 20 20 70 52 65 61 64 65 72 2d 3e 69 53 74 61 72    pReader->iStar
35101 74 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 70  tOffset = 0;.  p
35102 52 65 61 64 65 72 2d 3e 69 45 6e 64 4f 66 66 73  Reader->iEndOffs
35103 65 74 20 3d 20 30 3b 0a 20 20 70 6c 72 53 74 65  et = 0;.  plrSte
35104 70 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a 73 74  p(pReader);.}.st
35105 61 74 69 63 20 76 6f 69 64 20 70 6c 72 44 65 73  atic void plrDes
35106 74 72 6f 79 28 50 4c 52 65 61 64 65 72 20 2a 70  troy(PLReader *p
35107 52 65 61 64 65 72 29 7b 0a 20 20 53 43 52 41 4d  Reader){.  SCRAM
35108 42 4c 45 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a  BLE(pReader);.}.
35109 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3510a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3510b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3510c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3510d 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 50 4c 57 72 69 74  *****/./* PLWrit
3510e 65 72 20 69 73 20 75 73 65 64 20 69 6e 20 63 6f  er is used in co
3510f 6e 73 74 72 75 63 74 69 6e 67 20 61 20 64 6f 63  nstructing a doc
35110 75 6d 65 6e 74 27 73 20 70 6f 73 69 74 69 6f 6e  ument's position
35111 20 6c 69 73 74 2e 20 20 41 73 20 61 0a 2a 2a 20   list.  As a.** 
35112 63 6f 6e 76 65 6e 69 65 6e 63 65 2c 20 69 66 20  convenience, if 
35113 69 54 79 70 65 20 69 73 20 44 4c 5f 44 4f 43 49  iType is DL_DOCI
35114 44 53 2c 20 50 4c 57 72 69 74 65 72 20 62 65 63  DS, PLWriter bec
35115 6f 6d 65 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  omes a no-op..**
35116 20 50 4c 57 72 69 74 65 72 20 77 72 69 74 65 73   PLWriter writes
35117 20 74 6f 20 74 68 65 20 61 73 73 6f 63 69 61 74   to the associat
35118 65 64 20 44 4c 57 72 69 74 65 72 27 73 20 62 75  ed DLWriter's bu
35119 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 6c 77 49  ffer..**.** plwI
3511a 6e 69 74 20 2d 20 69 6e 69 74 20 66 6f 72 20 77  nit - init for w
3511b 72 69 74 69 6e 67 20 61 20 64 6f 63 75 6d 65 6e  riting a documen
3511c 74 27 73 20 70 6f 73 6c 69 73 74 2e 0a 2a 2a 20  t's poslist..** 
3511d 70 6c 77 44 65 73 74 72 6f 79 20 2d 20 63 6c 65  plwDestroy - cle
3511e 61 72 20 61 20 77 72 69 74 65 72 2e 0a 2a 2a 20  ar a writer..** 
3511f 70 6c 77 41 64 64 20 2d 20 61 70 70 65 6e 64 20  plwAdd - append 
35120 70 6f 73 69 74 69 6f 6e 20 61 6e 64 20 6f 66 66  position and off
35121 73 65 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  set information.
35122 0a 2a 2a 20 70 6c 77 43 6f 70 79 20 2d 20 63 6f  .** plwCopy - co
35123 70 79 20 6e 65 78 74 20 70 6f 73 69 74 69 6f 6e  py next position
35124 27 73 20 64 61 74 61 20 66 72 6f 6d 20 72 65 61  's data from rea
35125 64 65 72 20 74 6f 20 77 72 69 74 65 72 2e 0a 2a  der to writer..*
35126 2a 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 20 2d  * plwTerminate -
35127 20 61 64 64 20 61 6e 79 20 6e 65 63 65 73 73 61   add any necessa
35128 72 79 20 64 6f 63 6c 69 73 74 20 74 65 72 6d 69  ry doclist termi
35129 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c  nator..**.** Cal
3512a 6c 69 6e 67 20 70 6c 77 41 64 64 28 29 20 61 66  ling plwAdd() af
3512b 74 65 72 20 70 6c 77 54 65 72 6d 69 6e 61 74 65  ter plwTerminate
3512c 28 29 20 6d 61 79 20 72 65 73 75 6c 74 20 69 6e  () may result in
3512d 20 61 20 63 6f 72 72 75 70 74 0a 2a 2a 20 64 6f   a corrupt.** do
3512e 63 6c 69 73 74 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44  clist..*/./* TOD
3512f 4f 28 73 68 65 73 73 29 20 55 6e 74 69 6c 20 77  O(shess) Until w
35130 65 27 76 65 20 77 72 69 74 74 65 6e 20 74 68 65  e've written the
35131 20 73 65 63 6f 6e 64 20 69 74 65 6d 2c 20 77 65   second item, we
35132 20 63 61 6e 20 63 61 63 68 65 20 74 68 65 0a 2a   can cache the.*
35133 2a 20 66 69 72 73 74 20 69 74 65 6d 27 73 20 69  * first item's i
35134 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 54 68 65  nformation.  The
35135 6e 20 77 65 27 64 20 68 61 76 65 20 74 68 72 65  n we'd have thre
35136 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20  e states:.**.** 
35137 2d 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 69  - initialized wi
35138 74 68 20 64 6f 63 69 64 2c 20 6e 6f 20 70 6f 73  th docid, no pos
35139 69 74 69 6f 6e 73 2e 0a 2a 2a 20 2d 20 64 6f 63  itions..** - doc
3513a 69 64 20 61 6e 64 20 6f 6e 65 20 70 6f 73 69 74  id and one posit
3513b 69 6f 6e 2e 0a 2a 2a 20 2d 20 64 6f 63 69 64 20  ion..** - docid 
3513c 61 6e 64 20 6d 75 6c 74 69 70 6c 65 20 70 6f 73  and multiple pos
3513d 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  itions..**.** On
3513e 6c 79 20 74 68 65 20 6c 61 73 74 20 73 74 61 74  ly the last stat
3513f 65 20 6e 65 65 64 73 20 74 6f 20 61 63 74 75 61  e needs to actua
35140 6c 6c 79 20 77 72 69 74 65 20 74 6f 20 64 6c 77  lly write to dlw
35141 2d 3e 62 2c 20 77 68 69 63 68 20 77 6f 75 6c 64  ->b, which would
35142 0a 2a 2a 20 62 65 20 61 6e 20 69 6d 70 72 6f 76  .** be an improv
35143 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 44 4c 43  ement in the DLC
35144 6f 6c 6c 65 63 74 6f 72 20 63 61 73 65 2e 0a 2a  ollector case..*
35145 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
35146 20 50 4c 57 72 69 74 65 72 20 7b 0a 20 20 44 4c   PLWriter {.  DL
35147 57 72 69 74 65 72 20 2a 64 6c 77 3b 0a 0a 20 20  Writer *dlw;..  
35148 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20  int iColumn;    
35149 2f 2a 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75  /* the last colu
3514a 6d 6e 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20  mn written */.  
3514b 69 6e 74 20 69 50 6f 73 3b 20 20 20 20 20 20 20  int iPos;       
3514c 2f 2a 20 74 68 65 20 6c 61 73 74 20 70 6f 73 69  /* the last posi
3514d 74 69 6f 6e 20 77 72 69 74 74 65 6e 20 2a 2f 0a  tion written */.
3514e 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20    int iOffset;  
3514f 20 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 73 74    /* the last st
35150 61 72 74 20 6f 66 66 73 65 74 20 77 72 69 74 74  art offset writt
35151 65 6e 20 2a 2f 0a 7d 20 50 4c 57 72 69 74 65 72  en */.} PLWriter
35152 3b 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  ;../* TODO(shess
35153 29 20 49 6e 20 74 68 65 20 63 61 73 65 20 77 68  ) In the case wh
35154 65 72 65 20 74 68 65 20 70 61 72 65 6e 74 20 69  ere the parent i
35155 73 20 72 65 61 64 69 6e 67 20 74 68 65 73 65 20  s reading these 
35156 76 61 6c 75 65 73 0a 2a 2a 20 66 72 6f 6d 20 61  values.** from a
35157 20 50 4c 52 65 61 64 65 72 2c 20 77 65 20 63 6f   PLReader, we co
35158 75 6c 64 20 6f 70 74 69 6d 69 7a 65 20 74 6f 20  uld optimize to 
35159 61 20 63 6f 70 79 20 69 66 20 74 68 61 74 20 50  a copy if that P
3515a 4c 52 65 61 64 65 72 20 68 61 73 0a 2a 2a 20 74  LReader has.** t
3515b 68 65 20 73 61 6d 65 20 74 79 70 65 20 61 73 20  he same type as 
3515c 70 57 72 69 74 65 72 2e 0a 2a 2f 0a 73 74 61 74  pWriter..*/.stat
3515d 69 63 20 76 6f 69 64 20 70 6c 77 41 64 64 28 50  ic void plwAdd(P
3515e 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  LWriter *pWriter
3515f 2c 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 69  , int iColumn, i
35160 6e 74 20 69 50 6f 73 2c 0a 20 20 20 20 20 20 20  nt iPos,.       
35161 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
35162 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 69 6e  iStartOffset, in
35163 74 20 69 45 6e 64 4f 66 66 73 65 74 29 7b 0a 20  t iEndOffset){. 
35164 20 2f 2a 20 57 6f 72 73 74 2d 63 61 73 65 20 73   /* Worst-case s
35165 70 61 63 65 20 66 6f 72 20 50 4f 53 5f 43 4f 4c  pace for POS_COL
35166 55 4d 4e 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 50  UMN, iColumn, iP
35167 6f 73 44 65 6c 74 61 2c 0a 20 20 2a 2a 20 69 53  osDelta,.  ** iS
35168 74 61 72 74 4f 66 66 73 65 74 44 65 6c 74 61 2c  tartOffsetDelta,
35169 20 61 6e 64 20 69 45 6e 64 4f 66 66 73 65 74 44   and iEndOffsetD
3516a 65 6c 74 61 2e 0a 20 20 2a 2f 0a 20 20 63 68 61  elta..  */.  cha
3516b 72 20 63 5b 35 2a 56 41 52 49 4e 54 5f 4d 41 58  r c[5*VARINT_MAX
3516c 5d 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a  ];.  int n = 0;.
3516d 0a 20 20 2f 2a 20 42 61 6e 20 70 6c 77 41 64 64  .  /* Ban plwAdd
3516e 28 29 20 61 66 74 65 72 20 70 6c 77 54 65 72 6d  () after plwTerm
3516f 69 6e 61 74 65 28 29 2e 20 2a 2f 0a 20 20 61 73  inate(). */.  as
35170 73 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e 69  sert( pWriter->i
35171 50 6f 73 21 3d 2d 31 20 29 3b 0a 0a 20 20 69 66  Pos!=-1 );..  if
35172 28 20 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e  ( pWriter->dlw->
35173 69 54 79 70 65 3d 3d 44 4c 5f 44 4f 43 49 44 53  iType==DL_DOCIDS
35174 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69 66   ) return;..  if
35175 28 20 69 43 6f 6c 75 6d 6e 21 3d 70 57 72 69 74  ( iColumn!=pWrit
35176 65 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20  er->iColumn ){. 
35177 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74 56     n += fts3PutV
35178 61 72 69 6e 74 28 63 2b 6e 2c 20 50 4f 53 5f 43  arint(c+n, POS_C
35179 4f 4c 55 4d 4e 29 3b 0a 20 20 20 20 6e 20 2b 3d  OLUMN);.    n +=
3517a 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
3517b 2b 6e 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20  +n, iColumn);.  
3517c 20 20 70 57 72 69 74 65 72 2d 3e 69 43 6f 6c 75    pWriter->iColu
3517d 6d 6e 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20  mn = iColumn;.  
3517e 20 20 70 57 72 69 74 65 72 2d 3e 69 50 6f 73 20    pWriter->iPos 
3517f 3d 20 30 3b 0a 20 20 20 20 70 57 72 69 74 65 72  = 0;.    pWriter
35180 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20  ->iOffset = 0;. 
35181 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69 50 6f   }.  assert( iPo
35182 73 3e 3d 70 57 72 69 74 65 72 2d 3e 69 50 6f 73  s>=pWriter->iPos
35183 20 29 3b 0a 20 20 6e 20 2b 3d 20 66 74 73 33 50   );.  n += fts3P
35184 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 50 4f  utVarint(c+n, PO
35185 53 5f 42 41 53 45 2b 28 69 50 6f 73 2d 70 57 72  S_BASE+(iPos-pWr
35186 69 74 65 72 2d 3e 69 50 6f 73 29 29 3b 0a 20 20  iter->iPos));.  
35187 70 57 72 69 74 65 72 2d 3e 69 50 6f 73 20 3d 20  pWriter->iPos = 
35188 69 50 6f 73 3b 0a 20 20 69 66 28 20 70 57 72 69  iPos;.  if( pWri
35189 74 65 72 2d 3e 64 6c 77 2d 3e 69 54 79 70 65 3d  ter->dlw->iType=
3518a 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46  =DL_POSITIONS_OF
3518b 46 53 45 54 53 20 29 7b 0a 20 20 20 20 61 73 73  FSETS ){.    ass
3518c 65 72 74 28 20 69 53 74 61 72 74 4f 66 66 73 65  ert( iStartOffse
3518d 74 3e 3d 70 57 72 69 74 65 72 2d 3e 69 4f 66 66  t>=pWriter->iOff
3518e 73 65 74 20 29 3b 0a 20 20 20 20 6e 20 2b 3d 20  set );.    n += 
3518f 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b  fts3PutVarint(c+
35190 6e 2c 20 69 53 74 61 72 74 4f 66 66 73 65 74 2d  n, iStartOffset-
35191 70 57 72 69 74 65 72 2d 3e 69 4f 66 66 73 65 74  pWriter->iOffset
35192 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  );.    pWriter->
35193 69 4f 66 66 73 65 74 20 3d 20 69 53 74 61 72 74  iOffset = iStart
35194 4f 66 66 73 65 74 3b 0a 20 20 20 20 61 73 73 65  Offset;.    asse
35195 72 74 28 20 69 45 6e 64 4f 66 66 73 65 74 3e 3d  rt( iEndOffset>=
35196 69 53 74 61 72 74 4f 66 66 73 65 74 20 29 3b 0a  iStartOffset );.
35197 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74      n += fts3Put
35198 56 61 72 69 6e 74 28 63 2b 6e 2c 20 69 45 6e 64  Varint(c+n, iEnd
35199 4f 66 66 73 65 74 2d 69 53 74 61 72 74 4f 66 66  Offset-iStartOff
3519a 73 65 74 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61  set);.  }.  data
3519b 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 57 72  BufferAppend(pWr
3519c 69 74 65 72 2d 3e 64 6c 77 2d 3e 62 2c 20 63 2c  iter->dlw->b, c,
3519d 20 6e 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f   n);.}.static vo
3519e 69 64 20 70 6c 77 43 6f 70 79 28 50 4c 57 72 69  id plwCopy(PLWri
3519f 74 65 72 20 2a 70 57 72 69 74 65 72 2c 20 50 4c  ter *pWriter, PL
351a0 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
351a1 7b 0a 20 20 70 6c 77 41 64 64 28 70 57 72 69 74  {.  plwAdd(pWrit
351a2 65 72 2c 20 70 6c 72 43 6f 6c 75 6d 6e 28 70 52  er, plrColumn(pR
351a3 65 61 64 65 72 29 2c 20 70 6c 72 50 6f 73 69 74  eader), plrPosit
351a4 69 6f 6e 28 70 52 65 61 64 65 72 29 2c 0a 20 20  ion(pReader),.  
351a5 20 20 20 20 20 20 20 70 6c 72 53 74 61 72 74 4f         plrStartO
351a6 66 66 73 65 74 28 70 52 65 61 64 65 72 29 2c 20  ffset(pReader), 
351a7 70 6c 72 45 6e 64 4f 66 66 73 65 74 28 70 52 65  plrEndOffset(pRe
351a8 61 64 65 72 29 29 3b 0a 7d 0a 73 74 61 74 69 63  ader));.}.static
351a9 20 76 6f 69 64 20 70 6c 77 49 6e 69 74 28 50 4c   void plwInit(PL
351aa 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c  Writer *pWriter,
351ab 20 44 4c 57 72 69 74 65 72 20 2a 64 6c 77 2c 20   DLWriter *dlw, 
351ac 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
351ad 63 69 64 29 7b 0a 20 20 63 68 61 72 20 63 5b 56  cid){.  char c[V
351ae 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e  ARINT_MAX];.  in
351af 74 20 6e 3b 0a 0a 20 20 70 57 72 69 74 65 72 2d  t n;..  pWriter-
351b0 3e 64 6c 77 20 3d 20 64 6c 77 3b 0a 0a 20 20 2f  >dlw = dlw;..  /
351b1 2a 20 44 6f 63 69 64 73 20 6d 75 73 74 20 61 73  * Docids must as
351b2 63 65 6e 64 2e 20 2a 2f 0a 20 20 61 73 73 65 72  cend. */.  asser
351b3 74 28 20 21 70 57 72 69 74 65 72 2d 3e 64 6c 77  t( !pWriter->dlw
351b4 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64  ->has_iPrevDocid
351b5 20 7c 7c 20 69 44 6f 63 69 64 3e 70 57 72 69 74   || iDocid>pWrit
351b6 65 72 2d 3e 64 6c 77 2d 3e 69 50 72 65 76 44 6f  er->dlw->iPrevDo
351b7 63 69 64 20 29 3b 0a 20 20 6e 20 3d 20 66 74 73  cid );.  n = fts
351b8 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20 69 44  3PutVarint(c, iD
351b9 6f 63 69 64 2d 70 57 72 69 74 65 72 2d 3e 64 6c  ocid-pWriter->dl
351ba 77 2d 3e 69 50 72 65 76 44 6f 63 69 64 29 3b 0a  w->iPrevDocid);.
351bb 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
351bc 6e 64 28 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d  nd(pWriter->dlw-
351bd 3e 62 2c 20 63 2c 20 6e 29 3b 0a 20 20 70 57 72  >b, c, n);.  pWr
351be 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 50 72 65 76  iter->dlw->iPrev
351bf 44 6f 63 69 64 20 3d 20 69 44 6f 63 69 64 3b 0a  Docid = iDocid;.
351c0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
351c1 20 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 68   pWriter->dlw->h
351c2 61 73 5f 69 50 72 65 76 44 6f 63 69 64 20 3d 20  as_iPrevDocid = 
351c3 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 70 57 72  1;.#endif..  pWr
351c4 69 74 65 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20  iter->iColumn = 
351c5 30 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50  0;.  pWriter->iP
351c6 6f 73 20 3d 20 30 3b 0a 20 20 70 57 72 69 74 65  os = 0;.  pWrite
351c7 72 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a  r->iOffset = 0;.
351c8 7d 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  }./* TODO(shess)
351c9 20 53 68 6f 75 6c 64 20 70 6c 77 44 65 73 74 72   Should plwDestr
351ca 6f 79 28 29 20 61 6c 73 6f 20 74 65 72 6d 69 6e  oy() also termin
351cb 61 74 65 20 74 68 65 20 64 6f 63 6c 69 73 74 3f  ate the doclist?
351cc 20 20 42 75 74 0a 2a 2a 20 74 68 65 6e 20 70 6c    But.** then pl
351cd 77 44 65 73 74 72 6f 79 28 29 20 77 6f 75 6c 64  wDestroy() would
351ce 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 6a 75   no longer be ju
351cf 73 74 20 61 20 64 65 73 74 72 75 63 74 6f 72 2c  st a destructor,
351d0 20 69 74 20 77 6f 75 6c 64 0a 2a 2a 20 61 6c 73   it would.** als
351d1 6f 20 62 65 20 64 6f 69 6e 67 20 77 6f 72 6b 2c  o be doing work,
351d2 20 77 68 69 63 68 20 69 73 6e 27 74 20 63 6f 6e   which isn't con
351d3 73 69 73 74 65 6e 74 20 77 69 74 68 20 74 68 65  sistent with the
351d4 20 6f 76 65 72 61 6c 6c 20 69 64 69 6f 6d 2e 0a   overall idiom..
351d5 2a 2a 20 41 6e 6f 74 68 65 72 20 6f 70 74 69 6f  ** Another optio
351d6 6e 20 77 6f 75 6c 64 20 62 65 20 66 6f 72 20 70  n would be for p
351d7 6c 77 41 64 64 28 29 20 74 6f 20 61 6c 77 61 79  lwAdd() to alway
351d8 73 20 61 70 70 65 6e 64 20 61 6e 79 20 6e 65 63  s append any nec
351d9 65 73 73 61 72 79 0a 2a 2a 20 74 65 72 6d 69 6e  essary.** termin
351da 61 74 6f 72 2c 20 73 6f 20 74 68 61 74 20 74 68  ator, so that th
351db 65 20 6f 75 74 70 75 74 20 69 73 20 61 6c 77 61  e output is alwa
351dc 79 73 20 63 6f 72 72 65 63 74 2e 20 20 42 75 74  ys correct.  But
351dd 20 74 68 61 74 20 77 6f 75 6c 64 0a 2a 2a 20 61   that would.** a
351de 64 64 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 77  dd incremental w
351df 6f 72 6b 20 74 6f 20 74 68 65 20 63 6f 6d 6d 6f  ork to the commo
351e0 6e 20 63 61 73 65 20 77 69 74 68 20 74 68 65 20  n case with the 
351e1 6f 6e 6c 79 20 62 65 6e 65 66 69 74 20 62 65 69  only benefit bei
351e2 6e 67 0a 2a 2a 20 41 50 49 20 65 6c 65 67 61 6e  ng.** API elegan
351e3 63 65 2e 20 20 50 75 6e 74 20 66 6f 72 20 6e 6f  ce.  Punt for no
351e4 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  w..*/.static voi
351e5 64 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 50  d plwTerminate(P
351e6 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  LWriter *pWriter
351e7 29 7b 0a 20 20 69 66 28 20 70 57 72 69 74 65 72  ){.  if( pWriter
351e8 2d 3e 64 6c 77 2d 3e 69 54 79 70 65 3e 44 4c 5f  ->dlw->iType>DL_
351e9 44 4f 43 49 44 53 20 29 7b 0a 20 20 20 20 63 68  DOCIDS ){.    ch
351ea 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 5d  ar c[VARINT_MAX]
351eb 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 66 74  ;.    int n = ft
351ec 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20 50  s3PutVarint(c, P
351ed 4f 53 5f 45 4e 44 29 3b 0a 20 20 20 20 64 61 74  OS_END);.    dat
351ee 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 57  aBufferAppend(pW
351ef 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 62 2c 20 63  riter->dlw->b, c
351f0 2c 20 6e 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65  , n);.  }.#ifnde
351f1 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 4d 61  f NDEBUG.  /* Ma
351f2 72 6b 20 61 73 20 74 65 72 6d 69 6e 61 74 65 64  rk as terminated
351f3 20 66 6f 72 20 61 73 73 65 72 74 20 69 6e 20 70   for assert in p
351f4 6c 77 41 64 64 28 29 2e 20 2a 2f 0a 20 20 70 57  lwAdd(). */.  pW
351f5 72 69 74 65 72 2d 3e 69 50 6f 73 20 3d 20 2d 31  riter->iPos = -1
351f6 3b 0a 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74 69  ;.#endif.}.stati
351f7 63 20 76 6f 69 64 20 70 6c 77 44 65 73 74 72 6f  c void plwDestro
351f8 79 28 50 4c 57 72 69 74 65 72 20 2a 70 57 72 69  y(PLWriter *pWri
351f9 74 65 72 29 7b 0a 20 20 53 43 52 41 4d 42 4c 45  ter){.  SCRAMBLE
351fa 28 70 57 72 69 74 65 72 29 3b 0a 7d 0a 0a 2f 2a  (pWriter);.}../*
351fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
351fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
351fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
351fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
351ff 2a 2a 2f 0a 2f 2a 20 44 4c 43 6f 6c 6c 65 63 74  **/./* DLCollect
35200 6f 72 20 77 72 61 70 73 20 50 4c 57 72 69 74 65  or wraps PLWrite
35201 72 20 61 6e 64 20 44 4c 57 72 69 74 65 72 20 74  r and DLWriter t
35202 6f 20 70 72 6f 76 69 64 65 20 61 0a 2a 2a 20 64  o provide a.** d
35203 79 6e 61 6d 69 63 61 6c 6c 79 2d 61 6c 6c 6f 63  ynamically-alloc
35204 61 74 65 64 20 64 6f 63 6c 69 73 74 20 61 72 65  ated doclist are
35205 61 20 74 6f 20 75 73 65 20 64 75 72 69 6e 67 20  a to use during 
35206 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 2e 0a 2a 2a  tokenization..**
35207 0a 2a 2a 20 64 6c 63 4e 65 77 20 2d 20 6d 61 6c  .** dlcNew - mal
35208 6c 6f 63 20 75 70 20 61 6e 64 20 69 6e 69 74 69  loc up and initi
35209 61 6c 69 7a 65 20 61 20 63 6f 6c 6c 65 63 74 6f  alize a collecto
3520a 72 2e 0a 2a 2a 20 64 6c 63 44 65 6c 65 74 65 20  r..** dlcDelete 
3520b 2d 20 64 65 73 74 72 6f 79 20 61 20 63 6f 6c 6c  - destroy a coll
3520c 65 63 74 6f 72 20 61 6e 64 20 61 6c 6c 20 63 6f  ector and all co
3520d 6e 74 61 69 6e 65 64 20 69 74 65 6d 73 2e 0a 2a  ntained items..*
3520e 2a 20 64 6c 63 41 64 64 50 6f 73 20 2d 20 61 70  * dlcAddPos - ap
3520f 70 65 6e 64 20 70 6f 73 69 74 69 6f 6e 20 61 6e  pend position an
35210 64 20 6f 66 66 73 65 74 20 69 6e 66 6f 72 6d 61  d offset informa
35211 74 69 6f 6e 2e 0a 2a 2a 20 64 6c 63 41 64 64 44  tion..** dlcAddD
35212 6f 63 6c 69 73 74 20 2d 20 61 64 64 20 74 68 65  oclist - add the
35213 20 63 6f 6c 6c 65 63 74 65 64 20 64 6f 63 6c 69   collected docli
35214 73 74 20 74 6f 20 74 68 65 20 67 69 76 65 6e 20  st to the given 
35215 62 75 66 66 65 72 2e 0a 2a 2a 20 64 6c 63 4e 65  buffer..** dlcNe
35216 78 74 20 2d 20 74 65 72 6d 69 6e 61 74 65 20 74  xt - terminate t
35217 68 65 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d  he current docum
35218 65 6e 74 20 61 6e 64 20 6f 70 65 6e 20 61 6e 6f  ent and open ano
35219 74 68 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ther..*/.typedef
3521a 20 73 74 72 75 63 74 20 44 4c 43 6f 6c 6c 65 63   struct DLCollec
3521b 74 6f 72 20 7b 0a 20 20 44 61 74 61 42 75 66 66  tor {.  DataBuff
3521c 65 72 20 62 3b 0a 20 20 44 4c 57 72 69 74 65 72  er b;.  DLWriter
3521d 20 64 6c 77 3b 0a 20 20 50 4c 57 72 69 74 65 72   dlw;.  PLWriter
3521e 20 70 6c 77 3b 0a 7d 20 44 4c 43 6f 6c 6c 65 63   plw;.} DLCollec
3521f 74 6f 72 3b 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68  tor;../* TODO(sh
35220 65 73 73 29 20 54 68 69 73 20 63 6f 75 6c 64 20  ess) This could 
35221 61 6c 73 6f 20 62 65 20 64 6f 6e 65 20 62 79 20  also be done by 
35222 63 61 6c 6c 69 6e 67 20 70 6c 77 54 65 72 6d 69  calling plwTermi
35223 6e 61 74 65 28 29 20 61 6e 64 0a 2a 2a 20 64 61  nate() and.** da
35224 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 29  taBufferAppend()
35225 2e 20 20 49 20 74 72 69 65 64 20 74 68 61 74 2c  .  I tried that,
35226 20 65 78 70 65 63 74 69 6e 67 20 6e 6f 6d 69 6e   expecting nomin
35227 61 6c 20 70 65 72 66 6f 72 6d 61 6e 63 65 0a 2a  al performance.*
35228 2a 20 64 69 66 66 65 72 65 6e 63 65 73 2c 20 62  * differences, b
35229 75 74 20 69 74 20 73 65 65 6d 65 64 20 74 6f 20  ut it seemed to 
3522a 70 72 65 74 74 79 20 72 65 6c 69 61 62 6c 79 20  pretty reliably 
3522b 62 65 20 77 6f 72 74 68 20 31 25 20 74 6f 20 63  be worth 1% to c
3522c 6f 64 65 0a 2a 2a 20 69 74 20 74 68 69 73 20 77  ode.** it this w
3522d 61 79 2e 20 20 49 20 73 75 73 70 65 63 74 20 69  ay.  I suspect i
3522e 74 20 69 73 20 74 68 65 20 69 6e 63 72 65 6d 65  t is the increme
3522f 6e 74 61 6c 20 6d 61 6c 6c 6f 63 20 6f 76 65 72  ntal malloc over
35230 68 65 61 64 20 28 73 6f 6d 65 0a 2a 2a 20 70 65  head (some.** pe
35231 72 63 65 6e 74 61 67 65 20 6f 66 20 74 68 65 20  rcentage of the 
35232 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 29 20 63  plwTerminate() c
35233 61 6c 6c 73 20 77 69 6c 6c 20 63 61 75 73 65 20  alls will cause 
35234 61 20 72 65 61 6c 6c 6f 63 29 2c 20 73 6f 0a 2a  a realloc), so.*
35235 2a 20 74 68 69 73 20 6d 69 67 68 74 20 62 65 20  * this might be 
35236 77 6f 72 74 68 20 72 65 76 69 73 69 74 69 6e 67  worth revisiting
35237 20 69 66 20 74 68 65 20 44 61 74 61 42 75 66 66   if the DataBuff
35238 65 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  er implementatio
35239 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 0a 2a 2f  n.** changes..*/
3523a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c 63  .static void dlc
3523b 41 64 64 44 6f 63 6c 69 73 74 28 44 4c 43 6f 6c  AddDoclist(DLCol
3523c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c 65 63 74  lector *pCollect
3523d 6f 72 2c 20 44 61 74 61 42 75 66 66 65 72 20 2a  or, DataBuffer *
3523e 62 29 7b 0a 20 20 69 66 28 20 70 43 6f 6c 6c 65  b){.  if( pColle
3523f 63 74 6f 72 2d 3e 64 6c 77 2e 69 54 79 70 65 3e  ctor->dlw.iType>
35240 44 4c 5f 44 4f 43 49 44 53 20 29 7b 0a 20 20 20  DL_DOCIDS ){.   
35241 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d   char c[VARINT_M
35242 41 58 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  AX];.    int n =
35243 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
35244 2c 20 50 4f 53 5f 45 4e 44 29 3b 0a 20 20 20 20  , POS_END);.    
35245 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
35246 32 28 62 2c 20 70 43 6f 6c 6c 65 63 74 6f 72 2d  2(b, pCollector-
35247 3e 62 2e 70 44 61 74 61 2c 20 70 43 6f 6c 6c 65  >b.pData, pColle
35248 63 74 6f 72 2d 3e 62 2e 6e 44 61 74 61 2c 20 63  ctor->b.nData, c
35249 2c 20 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  , n);.  }else{. 
3524a 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70     dataBufferApp
3524b 65 6e 64 28 62 2c 20 70 43 6f 6c 6c 65 63 74 6f  end(b, pCollecto
3524c 72 2d 3e 62 2e 70 44 61 74 61 2c 20 70 43 6f 6c  r->b.pData, pCol
3524d 6c 65 63 74 6f 72 2d 3e 62 2e 6e 44 61 74 61 29  lector->b.nData)
3524e 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76  ;.  }.}.static v
3524f 6f 69 64 20 64 6c 63 4e 65 78 74 28 44 4c 43 6f  oid dlcNext(DLCo
35250 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c 65 63  llector *pCollec
35251 74 6f 72 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36  tor, sqlite_int6
35252 34 20 69 44 6f 63 69 64 29 7b 0a 20 20 70 6c 77  4 iDocid){.  plw
35253 54 65 72 6d 69 6e 61 74 65 28 26 70 43 6f 6c 6c  Terminate(&pColl
35254 65 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a 20 20 70  ector->plw);.  p
35255 6c 77 44 65 73 74 72 6f 79 28 26 70 43 6f 6c 6c  lwDestroy(&pColl
35256 65 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a 20 20 70  ector->plw);.  p
35257 6c 77 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63 74  lwInit(&pCollect
35258 6f 72 2d 3e 70 6c 77 2c 20 26 70 43 6f 6c 6c 65  or->plw, &pColle
35259 63 74 6f 72 2d 3e 64 6c 77 2c 20 69 44 6f 63 69  ctor->dlw, iDoci
3525a 64 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  d);.}.static voi
3525b 64 20 64 6c 63 41 64 64 50 6f 73 28 44 4c 43 6f  d dlcAddPos(DLCo
3525c 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c 65 63  llector *pCollec
3525d 74 6f 72 2c 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  tor, int iColumn
3525e 2c 20 69 6e 74 20 69 50 6f 73 2c 0a 20 20 20 20  , int iPos,.    
3525f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35260 20 20 69 6e 74 20 69 53 74 61 72 74 4f 66 66 73    int iStartOffs
35261 65 74 2c 20 69 6e 74 20 69 45 6e 64 4f 66 66 73  et, int iEndOffs
35262 65 74 29 7b 0a 20 20 70 6c 77 41 64 64 28 26 70  et){.  plwAdd(&p
35263 43 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 2c 20  Collector->plw, 
35264 69 43 6f 6c 75 6d 6e 2c 20 69 50 6f 73 2c 20 69  iColumn, iPos, i
35265 53 74 61 72 74 4f 66 66 73 65 74 2c 20 69 45 6e  StartOffset, iEn
35266 64 4f 66 66 73 65 74 29 3b 0a 7d 0a 0a 73 74 61  dOffset);.}..sta
35267 74 69 63 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20  tic DLCollector 
35268 2a 64 6c 63 4e 65 77 28 73 71 6c 69 74 65 5f 69  *dlcNew(sqlite_i
35269 6e 74 36 34 20 69 44 6f 63 69 64 2c 20 44 6f 63  nt64 iDocid, Doc
3526a 4c 69 73 74 54 79 70 65 20 69 54 79 70 65 29 7b  ListType iType){
3526b 0a 20 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a  .  DLCollector *
3526c 70 43 6f 6c 6c 65 63 74 6f 72 20 3d 20 73 71 6c  pCollector = sql
3526d 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
3526e 6f 66 28 44 4c 43 6f 6c 6c 65 63 74 6f 72 29 29  of(DLCollector))
3526f 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e  ;.  dataBufferIn
35270 69 74 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e  it(&pCollector->
35271 62 2c 20 30 29 3b 0a 20 20 64 6c 77 49 6e 69 74  b, 0);.  dlwInit
35272 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 64 6c  (&pCollector->dl
35273 77 2c 20 69 54 79 70 65 2c 20 26 70 43 6f 6c 6c  w, iType, &pColl
35274 65 63 74 6f 72 2d 3e 62 29 3b 0a 20 20 70 6c 77  ector->b);.  plw
35275 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63 74 6f 72  Init(&pCollector
35276 2d 3e 70 6c 77 2c 20 26 70 43 6f 6c 6c 65 63 74  ->plw, &pCollect
35277 6f 72 2d 3e 64 6c 77 2c 20 69 44 6f 63 69 64 29  or->dlw, iDocid)
35278 3b 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c  ;.  return pColl
35279 65 63 74 6f 72 3b 0a 7d 0a 73 74 61 74 69 63 20  ector;.}.static 
3527a 76 6f 69 64 20 64 6c 63 44 65 6c 65 74 65 28 44  void dlcDelete(D
3527b 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c  LCollector *pCol
3527c 6c 65 63 74 6f 72 29 7b 0a 20 20 70 6c 77 44 65  lector){.  plwDe
3527d 73 74 72 6f 79 28 26 70 43 6f 6c 6c 65 63 74 6f  stroy(&pCollecto
3527e 72 2d 3e 70 6c 77 29 3b 0a 20 20 64 6c 77 44 65  r->plw);.  dlwDe
3527f 73 74 72 6f 79 28 26 70 43 6f 6c 6c 65 63 74 6f  stroy(&pCollecto
35280 72 2d 3e 64 6c 77 29 3b 0a 20 20 64 61 74 61 42  r->dlw);.  dataB
35281 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 70 43  ufferDestroy(&pC
35282 6f 6c 6c 65 63 74 6f 72 2d 3e 62 29 3b 0a 20 20  ollector->b);.  
35283 53 43 52 41 4d 42 4c 45 28 70 43 6f 6c 6c 65 63  SCRAMBLE(pCollec
35284 74 6f 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  tor);.  sqlite3_
35285 66 72 65 65 28 70 43 6f 6c 6c 65 63 74 6f 72 29  free(pCollector)
35286 3b 0a 7d 0a 0a 0a 2f 2a 20 43 6f 70 79 20 74 68  ;.}.../* Copy th
35287 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 6f  e doclist data o
35288 66 20 69 54 79 70 65 20 69 6e 20 70 44 61 74 61  f iType in pData
35289 2f 6e 44 61 74 61 20 69 6e 74 6f 20 2a 6f 75 74  /nData into *out
3528a 2c 20 74 72 69 6d 6d 69 6e 67 0a 2a 2a 20 75 6e  , trimming.** un
3528b 6e 65 63 65 73 73 61 72 79 20 64 61 74 61 20 61  necessary data a
3528c 73 20 77 65 20 67 6f 2e 20 20 4f 6e 6c 79 20 63  s we go.  Only c
3528d 6f 6c 75 6d 6e 73 20 6d 61 74 63 68 69 6e 67 20  olumns matching 
3528e 69 43 6f 6c 75 6d 6e 20 61 72 65 0a 2a 2a 20 63  iColumn are.** c
3528f 6f 70 69 65 64 2c 20 61 6c 6c 20 63 6f 6c 75 6d  opied, all colum
35290 6e 73 20 63 6f 70 69 65 64 20 69 66 20 69 43 6f  ns copied if iCo
35291 6c 75 6d 6e 20 69 73 20 2d 31 2e 20 20 45 6c 65  lumn is -1.  Ele
35292 6d 65 6e 74 73 20 77 69 74 68 20 6e 6f 0a 2a 2a  ments with no.**
35293 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e   matching column
35294 73 20 61 72 65 20 64 72 6f 70 70 65 64 2e 20 20  s are dropped.  
35295 54 68 65 20 6f 75 74 70 75 74 20 69 73 20 61 6e  The output is an
35296 20 69 4f 75 74 54 79 70 65 20 64 6f 63 6c 69 73   iOutType doclis
35297 74 2e 0a 2a 2f 0a 2f 2a 20 4e 4f 54 45 28 73 68  t..*/./* NOTE(sh
35298 65 73 73 29 20 54 68 69 73 20 63 6f 64 65 20 69  ess) This code i
35299 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20 61 66 74  s only valid aft
3529a 65 72 20 61 6c 6c 20 64 6f 63 6c 69 73 74 73 20  er all doclists 
3529b 61 72 65 20 6d 65 72 67 65 64 2e 0a 2a 2a 20 49  are merged..** I
3529c 66 20 74 68 69 73 20 69 73 20 72 75 6e 20 62 65  f this is run be
3529d 66 6f 72 65 20 6d 65 72 67 65 73 2c 20 74 68 65  fore merges, the
3529e 6e 20 64 6f 63 6c 69 73 74 20 69 74 65 6d 73 20  n doclist items 
3529f 77 68 69 63 68 20 72 65 70 72 65 73 65 6e 74 0a  which represent.
352a0 2a 2a 20 64 65 6c 65 74 69 6f 6e 20 77 69 6c 6c  ** deletion will
352a1 20 62 65 20 74 72 69 6d 6d 65 64 2c 20 61 6e 64   be trimmed, and
352a2 20 77 69 6c 6c 20 74 68 75 73 20 6e 6f 74 20 65   will thus not e
352a3 66 66 65 63 74 20 61 20 64 65 6c 65 74 69 6f 6e  ffect a deletion
352a4 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68 65 20 6d  .** during the m
352a5 65 72 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  erge..*/.static 
352a6 76 6f 69 64 20 64 6f 63 4c 69 73 74 54 72 69 6d  void docListTrim
352a7 28 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79  (DocListType iTy
352a8 70 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  pe, const char *
352a9 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61  pData, int nData
352aa 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
352ab 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 43            int iC
352ac 6f 6c 75 6d 6e 2c 20 44 6f 63 4c 69 73 74 54 79  olumn, DocListTy
352ad 70 65 20 69 4f 75 74 54 79 70 65 2c 20 44 61 74  pe iOutType, Dat
352ae 61 42 75 66 66 65 72 20 2a 6f 75 74 29 7b 0a 20  aBuffer *out){. 
352af 20 44 4c 52 65 61 64 65 72 20 64 6c 52 65 61 64   DLReader dlRead
352b0 65 72 3b 0a 20 20 44 4c 57 72 69 74 65 72 20 64  er;.  DLWriter d
352b1 6c 57 72 69 74 65 72 3b 0a 0a 20 20 61 73 73 65  lWriter;..  asse
352b2 72 74 28 20 69 4f 75 74 54 79 70 65 3c 3d 69 54  rt( iOutType<=iT
352b3 79 70 65 20 29 3b 0a 0a 20 20 64 6c 72 49 6e 69  ype );..  dlrIni
352b4 74 28 26 64 6c 52 65 61 64 65 72 2c 20 69 54 79  t(&dlReader, iTy
352b5 70 65 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61  pe, pData, nData
352b6 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28 26 64 6c  );.  dlwInit(&dl
352b7 57 72 69 74 65 72 2c 20 69 4f 75 74 54 79 70 65  Writer, iOutType
352b8 2c 20 6f 75 74 29 3b 0a 0a 20 20 77 68 69 6c 65  , out);..  while
352b9 28 20 21 64 6c 72 41 74 45 6e 64 28 26 64 6c 52  ( !dlrAtEnd(&dlR
352ba 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20 50 4c  eader) ){.    PL
352bb 52 65 61 64 65 72 20 70 6c 52 65 61 64 65 72 3b  Reader plReader;
352bc 0a 20 20 20 20 50 4c 57 72 69 74 65 72 20 70 6c  .    PLWriter pl
352bd 57 72 69 74 65 72 3b 0a 20 20 20 20 69 6e 74 20  Writer;.    int 
352be 6d 61 74 63 68 20 3d 20 30 3b 0a 0a 20 20 20 20  match = 0;..    
352bf 70 6c 72 49 6e 69 74 28 26 70 6c 52 65 61 64 65  plrInit(&plReade
352c0 72 2c 20 26 64 6c 52 65 61 64 65 72 29 3b 0a 0a  r, &dlReader);..
352c1 20 20 20 20 77 68 69 6c 65 28 20 21 70 6c 72 41      while( !plrA
352c2 74 45 6e 64 28 26 70 6c 52 65 61 64 65 72 29 20  tEnd(&plReader) 
352c3 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f  ){.      if( iCo
352c4 6c 75 6d 6e 3d 3d 2d 31 20 7c 7c 20 70 6c 72 43  lumn==-1 || plrC
352c5 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65 72 29  olumn(&plReader)
352c6 3d 3d 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ==iColumn ){.   
352c7 20 20 20 20 20 69 66 28 20 21 6d 61 74 63 68 20       if( !match 
352c8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77  ){.          plw
352c9 49 6e 69 74 28 26 70 6c 57 72 69 74 65 72 2c 20  Init(&plWriter, 
352ca 26 64 6c 57 72 69 74 65 72 2c 20 64 6c 72 44 6f  &dlWriter, dlrDo
352cb 63 69 64 28 26 64 6c 52 65 61 64 65 72 29 29 3b  cid(&dlReader));
352cc 0a 20 20 20 20 20 20 20 20 20 20 6d 61 74 63 68  .          match
352cd 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 1;.        }.
352ce 20 20 20 20 20 20 20 20 70 6c 77 41 64 64 28 26          plwAdd(&
352cf 70 6c 57 72 69 74 65 72 2c 20 70 6c 72 43 6f 6c  plWriter, plrCol
352d0 75 6d 6e 28 26 70 6c 52 65 61 64 65 72 29 2c 20  umn(&plReader), 
352d1 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26 70 6c 52  plrPosition(&plR
352d2 65 61 64 65 72 29 2c 0a 20 20 20 20 20 20 20 20  eader),.        
352d3 20 20 20 20 20 20 20 70 6c 72 53 74 61 72 74 4f         plrStartO
352d4 66 66 73 65 74 28 26 70 6c 52 65 61 64 65 72 29  ffset(&plReader)
352d5 2c 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28 26  , plrEndOffset(&
352d6 70 6c 52 65 61 64 65 72 29 29 3b 0a 20 20 20 20  plReader));.    
352d7 20 20 7d 0a 20 20 20 20 20 20 70 6c 72 53 74 65    }.      plrSte
352d8 70 28 26 70 6c 52 65 61 64 65 72 29 3b 0a 20 20  p(&plReader);.  
352d9 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 61 74 63    }.    if( matc
352da 68 20 29 7b 0a 20 20 20 20 20 20 70 6c 77 54 65  h ){.      plwTe
352db 72 6d 69 6e 61 74 65 28 26 70 6c 57 72 69 74 65  rminate(&plWrite
352dc 72 29 3b 0a 20 20 20 20 20 20 70 6c 77 44 65 73  r);.      plwDes
352dd 74 72 6f 79 28 26 70 6c 57 72 69 74 65 72 29 3b  troy(&plWriter);
352de 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 6c 72 44  .    }..    plrD
352df 65 73 74 72 6f 79 28 26 70 6c 52 65 61 64 65 72  estroy(&plReader
352e0 29 3b 0a 20 20 20 20 64 6c 72 53 74 65 70 28 26  );.    dlrStep(&
352e1 64 6c 52 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20  dlReader);.  }. 
352e2 20 64 6c 77 44 65 73 74 72 6f 79 28 26 64 6c 57   dlwDestroy(&dlW
352e3 72 69 74 65 72 29 3b 0a 20 20 64 6c 72 44 65 73  riter);.  dlrDes
352e4 74 72 6f 79 28 26 64 6c 52 65 61 64 65 72 29 3b  troy(&dlReader);
352e5 0a 7d 0a 0a 2f 2a 20 55 73 65 64 20 62 79 20 64  .}../* Used by d
352e6 6f 63 4c 69 73 74 4d 65 72 67 65 28 29 20 74 6f  ocListMerge() to
352e7 20 6b 65 65 70 20 64 6f 63 6c 69 73 74 73 20 69   keep doclists i
352e8 6e 20 74 68 65 20 61 73 63 65 6e 64 69 6e 67 20  n the ascending 
352e9 6f 72 64 65 72 20 62 79 0a 2a 2a 20 64 6f 63 69  order by.** doci
352ea 64 2c 20 74 68 65 6e 20 61 73 63 65 6e 64 69 6e  d, then ascendin
352eb 67 20 6f 72 64 65 72 20 62 79 20 61 67 65 20 28  g order by age (
352ec 73 6f 20 74 68 65 20 6e 65 77 65 73 74 20 63 6f  so the newest co
352ed 6d 65 73 20 66 69 72 73 74 29 2e 0a 2a 2f 0a 74  mes first)..*/.t
352ee 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4f 72  ypedef struct Or
352ef 64 65 72 65 64 44 4c 52 65 61 64 65 72 20 7b 0a  deredDLReader {.
352f0 20 20 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61    DLReader *pRea
352f1 64 65 72 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28  der;..  /* TODO(
352f2 73 68 65 73 73 29 20 49 66 20 77 65 20 61 73 73  shess) If we ass
352f3 75 6d 65 20 74 68 61 74 20 64 6f 63 4c 69 73 74  ume that docList
352f4 4d 65 72 67 65 20 70 52 65 61 64 65 72 73 20 69  Merge pReaders i
352f5 73 20 6f 72 64 65 72 65 64 20 62 79 0a 20 20 2a  s ordered by.  *
352f6 2a 20 61 67 65 20 28 77 68 69 63 68 20 77 65 20  * age (which we 
352f7 64 6f 29 2c 20 74 68 65 6e 20 77 65 20 63 6f 75  do), then we cou
352f8 6c 64 20 75 73 65 20 70 52 65 61 64 65 72 20 63  ld use pReader c
352f9 6f 6d 70 61 72 69 73 6f 6e 73 20 74 6f 20 62 72  omparisons to br
352fa 65 61 6b 0a 20 20 2a 2a 20 74 69 65 73 2e 0a 20  eak.  ** ties.. 
352fb 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 3b 0a 7d   */.  int idx;.}
352fc 20 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72   OrderedDLReader
352fd 3b 0a 0a 2f 2a 20 4f 72 64 65 72 20 65 6f 66 20  ;../* Order eof 
352fe 74 6f 20 65 6e 64 2c 20 74 68 65 6e 20 62 79 20  to end, then by 
352ff 64 6f 63 69 64 20 61 73 63 2c 20 69 64 78 20 64  docid asc, idx d
35300 65 73 63 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  esc. */.static i
35301 6e 74 20 6f 72 64 65 72 65 64 44 4c 52 65 61 64  nt orderedDLRead
35302 65 72 43 6d 70 28 4f 72 64 65 72 65 64 44 4c 52  erCmp(OrderedDLR
35303 65 61 64 65 72 20 2a 72 31 2c 20 4f 72 64 65 72  eader *r1, Order
35304 65 64 44 4c 52 65 61 64 65 72 20 2a 72 32 29 7b  edDLReader *r2){
35305 0a 20 20 69 66 28 20 64 6c 72 41 74 45 6e 64 28  .  if( dlrAtEnd(
35306 72 31 2d 3e 70 52 65 61 64 65 72 29 20 29 7b 0a  r1->pReader) ){.
35307 20 20 20 20 69 66 28 20 64 6c 72 41 74 45 6e 64      if( dlrAtEnd
35308 28 72 32 2d 3e 70 52 65 61 64 65 72 29 20 29 20  (r2->pReader) ) 
35309 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 42 6f  return 0;  /* Bo
3530a 74 68 20 61 74 45 6e 64 28 29 2e 20 2a 2f 0a 20  th atEnd(). */. 
3530b 20 20 20 72 65 74 75 72 6e 20 31 3b 20 20 20 20     return 1;    
3530c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3530d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c            /* Onl
3530e 79 20 72 31 20 61 74 45 6e 64 28 29 2e 20 2a 2f  y r1 atEnd(). */
3530f 0a 20 20 7d 0a 20 20 69 66 28 20 64 6c 72 41 74  .  }.  if( dlrAt
35310 45 6e 64 28 72 32 2d 3e 70 52 65 61 64 65 72 29  End(r2->pReader)
35311 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 20 20 20   ) return -1;   
35312 2f 2a 20 4f 6e 6c 79 20 72 32 20 61 74 45 6e 64  /* Only r2 atEnd
35313 28 29 2e 20 2a 2f 0a 0a 20 20 69 66 28 20 64 6c  (). */..  if( dl
35314 72 44 6f 63 69 64 28 72 31 2d 3e 70 52 65 61 64  rDocid(r1->pRead
35315 65 72 29 3c 64 6c 72 44 6f 63 69 64 28 72 32 2d  er)<dlrDocid(r2-
35316 3e 70 52 65 61 64 65 72 29 20 29 20 72 65 74 75  >pReader) ) retu
35317 72 6e 20 2d 31 3b 0a 20 20 69 66 28 20 64 6c 72  rn -1;.  if( dlr
35318 44 6f 63 69 64 28 72 31 2d 3e 70 52 65 61 64 65  Docid(r1->pReade
35319 72 29 3e 64 6c 72 44 6f 63 69 64 28 72 32 2d 3e  r)>dlrDocid(r2->
3531a 70 52 65 61 64 65 72 29 20 29 20 72 65 74 75 72  pReader) ) retur
3531b 6e 20 31 3b 0a 0a 20 20 2f 2a 20 44 65 73 63 65  n 1;..  /* Desce
3531c 6e 64 69 6e 67 20 6f 6e 20 69 64 78 2e 20 2a 2f  nding on idx. */
3531d 0a 20 20 72 65 74 75 72 6e 20 72 32 2d 3e 69 64  .  return r2->id
3531e 78 2d 72 31 2d 3e 69 64 78 3b 0a 7d 0a 0a 2f 2a  x-r1->idx;.}../*
3531f 20 42 75 62 62 6c 65 20 70 5b 30 5d 20 74 6f 20   Bubble p[0] to 
35320 61 70 70 72 6f 70 72 69 61 74 65 20 70 6c 61 63  appropriate plac
35321 65 20 69 6e 20 70 5b 31 2e 2e 6e 2d 31 5d 2e 20  e in p[1..n-1]. 
35322 20 41 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a   Assumes that.**
35323 20 70 5b 31 2e 2e 6e 2d 31 5d 20 69 73 20 61 6c   p[1..n-1] is al
35324 72 65 61 64 79 20 73 6f 72 74 65 64 2e 0a 2a 2f  ready sorted..*/
35325 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
35326 49 73 20 74 68 69 73 20 66 72 65 71 75 65 6e 74  Is this frequent
35327 20 65 6e 6f 75 67 68 20 74 6f 20 77 61 72 72 61   enough to warra
35328 6e 74 20 61 20 62 69 6e 61 72 79 20 73 65 61 72  nt a binary sear
35329 63 68 3f 0a 2a 2a 20 42 65 66 6f 72 65 20 69 6d  ch?.** Before im
3532a 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 61 74 2c  plementing that,
3532b 20 69 6e 73 74 72 75 6d 65 6e 74 20 74 68 65 20   instrument the 
3532c 63 6f 64 65 20 74 6f 20 63 68 65 63 6b 2e 20 20  code to check.  
3532d 49 6e 20 6d 6f 73 74 0a 2a 2a 20 63 75 72 72 65  In most.** curre
3532e 6e 74 20 75 73 61 67 65 2c 20 49 20 65 78 70 65  nt usage, I expe
3532f 63 74 20 74 68 61 74 20 70 5b 30 5d 20 77 69 6c  ct that p[0] wil
35330 6c 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 70  l be less than p
35331 5b 31 5d 20 61 20 76 65 72 79 0a 2a 2a 20 68 69  [1] a very.** hi
35332 67 68 20 70 72 6f 70 6f 72 74 69 6f 6e 20 6f 66  gh proportion of
35333 20 74 68 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74   the time..*/.st
35334 61 74 69 63 20 76 6f 69 64 20 6f 72 64 65 72 65  atic void ordere
35335 64 44 4c 52 65 61 64 65 72 52 65 6f 72 64 65 72  dDLReaderReorder
35336 28 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72  (OrderedDLReader
35337 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 77   *p, int n){.  w
35338 68 69 6c 65 28 20 6e 3e 31 20 26 26 20 6f 72 64  hile( n>1 && ord
35339 65 72 65 64 44 4c 52 65 61 64 65 72 43 6d 70 28  eredDLReaderCmp(
3533a 70 2c 20 70 2b 31 29 3e 30 20 29 7b 0a 20 20 20  p, p+1)>0 ){.   
3533b 20 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72   OrderedDLReader
3533c 20 74 6d 70 20 3d 20 70 5b 30 5d 3b 0a 20 20 20   tmp = p[0];.   
3533d 20 70 5b 30 5d 20 3d 20 70 5b 31 5d 3b 0a 20 20   p[0] = p[1];.  
3533e 20 20 70 5b 31 5d 20 3d 20 74 6d 70 3b 0a 20 20    p[1] = tmp;.  
3533f 20 20 6e 2d 2d 3b 0a 20 20 20 20 70 2b 2b 3b 0a    n--;.    p++;.
35340 20 20 7d 0a 7d 0a 0a 2f 2a 20 47 69 76 65 6e 20    }.}../* Given 
35341 61 6e 20 61 72 72 61 79 20 6f 66 20 64 6f 63 6c  an array of docl
35342 69 73 74 20 72 65 61 64 65 72 73 2c 20 6d 65 72  ist readers, mer
35343 67 65 20 74 68 65 69 72 20 64 6f 63 6c 69 73 74  ge their doclist
35344 20 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20 69 6e 74   elements.** int
35345 6f 20 6f 75 74 20 69 6e 20 73 6f 72 74 65 64 20  o out in sorted 
35346 6f 72 64 65 72 20 28 62 79 20 64 6f 63 69 64 29  order (by docid)
35347 2c 20 64 72 6f 70 70 69 6e 67 20 65 6c 65 6d 65  , dropping eleme
35348 6e 74 73 20 66 72 6f 6d 20 6f 6c 64 65 72 0a 2a  nts from older.*
35349 2a 20 72 65 61 64 65 72 73 20 77 68 65 6e 20 74  * readers when t
3534a 68 65 72 65 20 69 73 20 61 20 64 75 70 6c 69 63  here is a duplic
3534b 61 74 65 20 64 6f 63 69 64 2e 20 20 70 52 65 61  ate docid.  pRea
3534c 64 65 72 73 20 69 73 20 61 73 73 75 6d 65 64 20  ders is assumed 
3534d 74 6f 20 62 65 0a 2a 2a 20 6f 72 64 65 72 65 64  to be.** ordered
3534e 20 62 79 20 61 67 65 2c 20 6f 6c 64 65 73 74 20   by age, oldest 
3534f 66 69 72 73 74 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44  first..*/./* TOD
35350 4f 28 73 68 65 73 73 29 20 6e 52 65 61 64 65 72  O(shess) nReader
35351 73 20 6d 75 73 74 20 62 65 20 3c 3d 20 4d 45 52  s must be <= MER
35352 47 45 5f 43 4f 55 4e 54 2e 20 20 54 68 69 73 20  GE_COUNT.  This 
35353 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79 0a  should probably.
35354 2a 2a 20 62 65 20 66 69 78 65 64 2e 0a 2a 2f 0a  ** be fixed..*/.
35355 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c  static void docL
35356 69 73 74 4d 65 72 67 65 28 44 61 74 61 42 75 66  istMerge(DataBuf
35357 66 65 72 20 2a 6f 75 74 2c 0a 20 20 20 20 20 20  fer *out,.      
35358 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35359 20 20 20 44 4c 52 65 61 64 65 72 20 2a 70 52 65     DLReader *pRe
3535a 61 64 65 72 73 2c 20 69 6e 74 20 6e 52 65 61 64  aders, int nRead
3535b 65 72 73 29 7b 0a 20 20 4f 72 64 65 72 65 64 44  ers){.  OrderedD
3535c 4c 52 65 61 64 65 72 20 72 65 61 64 65 72 73 5b  LReader readers[
3535d 4d 45 52 47 45 5f 43 4f 55 4e 54 5d 3b 0a 20 20  MERGE_COUNT];.  
3535e 44 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b  DLWriter writer;
3535f 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 63  .  int i, n;.  c
35360 6f 6e 73 74 20 63 68 61 72 20 2a 70 53 74 61 72  onst char *pStar
35361 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 53 74  t = 0;.  int nSt
35362 61 72 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  art = 0;.  sqlit
35363 65 5f 69 6e 74 36 34 20 69 46 69 72 73 74 44 6f  e_int64 iFirstDo
35364 63 69 64 20 3d 20 30 2c 20 69 4c 61 73 74 44 6f  cid = 0, iLastDo
35365 63 69 64 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65  cid = 0;..  asse
35366 72 74 28 20 6e 52 65 61 64 65 72 73 3e 30 20 29  rt( nReaders>0 )
35367 3b 0a 20 20 69 66 28 20 6e 52 65 61 64 65 72 73  ;.  if( nReaders
35368 3d 3d 31 20 29 7b 0a 20 20 20 20 64 61 74 61 42  ==1 ){.    dataB
35369 75 66 66 65 72 41 70 70 65 6e 64 28 6f 75 74 2c  ufferAppend(out,
3536a 20 64 6c 72 44 6f 63 44 61 74 61 28 70 52 65 61   dlrDocData(pRea
3536b 64 65 72 73 29 2c 20 64 6c 72 41 6c 6c 44 61 74  ders), dlrAllDat
3536c 61 42 79 74 65 73 28 70 52 65 61 64 65 72 73 29  aBytes(pReaders)
3536d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
3536e 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 52   }..  assert( nR
3536f 65 61 64 65 72 73 3c 3d 4d 45 52 47 45 5f 43 4f  eaders<=MERGE_CO
35370 55 4e 54 20 29 3b 0a 20 20 6e 20 3d 20 30 3b 0a  UNT );.  n = 0;.
35371 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65    for(i=0; i<nRe
35372 61 64 65 72 73 3b 20 69 2b 2b 29 7b 0a 20 20 20  aders; i++){.   
35373 20 61 73 73 65 72 74 28 20 70 52 65 61 64 65 72   assert( pReader
35374 73 5b 69 5d 2e 69 54 79 70 65 3d 3d 70 52 65 61  s[i].iType==pRea
35375 64 65 72 73 5b 30 5d 2e 69 54 79 70 65 20 29 3b  ders[0].iType );
35376 0a 20 20 20 20 72 65 61 64 65 72 73 5b 69 5d 2e  .    readers[i].
35377 70 52 65 61 64 65 72 20 3d 20 70 52 65 61 64 65  pReader = pReade
35378 72 73 2b 69 3b 0a 20 20 20 20 72 65 61 64 65 72  rs+i;.    reader
35379 73 5b 69 5d 2e 69 64 78 20 3d 20 69 3b 0a 20 20  s[i].idx = i;.  
3537a 20 20 6e 20 2b 3d 20 64 6c 72 41 6c 6c 44 61 74    n += dlrAllDat
3537b 61 42 79 74 65 73 28 26 70 52 65 61 64 65 72 73  aBytes(&pReaders
3537c 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 43  [i]);.  }.  /* C
3537d 6f 6e 73 65 72 76 61 74 69 76 65 6c 79 20 73 69  onservatively si
3537e 7a 65 20 6f 75 74 70 75 74 20 74 6f 20 73 75 6d  ze output to sum
3537f 20 6f 66 20 69 6e 70 75 74 73 2e 20 20 4f 75 74   of inputs.  Out
35380 70 75 74 20 73 68 6f 75 6c 64 20 65 6e 64 0a 20  put should end. 
35381 20 2a 2a 20 75 70 20 73 74 72 69 63 74 6c 79 20   ** up strictly 
35382 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 69 6e 70  smaller than inp
35383 75 74 2e 0a 20 20 2a 2f 0a 20 20 64 61 74 61 42  ut..  */.  dataB
35384 75 66 66 65 72 45 78 70 61 6e 64 28 6f 75 74 2c  ufferExpand(out,
35385 20 6e 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 74   n);..  /* Get t
35386 68 65 20 72 65 61 64 65 72 73 20 69 6e 74 6f 20  he readers into 
35387 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 2a 2f  sorted order. */
35388 0a 20 20 77 68 69 6c 65 28 20 69 2d 2d 3e 30 20  .  while( i-->0 
35389 29 7b 0a 20 20 20 20 6f 72 64 65 72 65 64 44 4c  ){.    orderedDL
3538a 52 65 61 64 65 72 52 65 6f 72 64 65 72 28 72 65  ReaderReorder(re
3538b 61 64 65 72 73 2b 69 2c 20 6e 52 65 61 64 65 72  aders+i, nReader
3538c 73 2d 69 29 3b 0a 20 20 7d 0a 0a 20 20 64 6c 77  s-i);.  }..  dlw
3538d 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 70 52  Init(&writer, pR
3538e 65 61 64 65 72 73 5b 30 5d 2e 69 54 79 70 65 2c  eaders[0].iType,
3538f 20 6f 75 74 29 3b 0a 20 20 77 68 69 6c 65 28 20   out);.  while( 
35390 21 64 6c 72 41 74 45 6e 64 28 72 65 61 64 65 72  !dlrAtEnd(reader
35391 73 5b 30 5d 2e 70 52 65 61 64 65 72 29 20 29 7b  s[0].pReader) ){
35392 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  .    sqlite_int6
35393 34 20 69 44 6f 63 69 64 20 3d 20 64 6c 72 44 6f  4 iDocid = dlrDo
35394 63 69 64 28 72 65 61 64 65 72 73 5b 30 5d 2e 70  cid(readers[0].p
35395 52 65 61 64 65 72 29 3b 0a 0a 20 20 20 20 2f 2a  Reader);..    /*
35396 20 49 66 20 74 68 69 73 20 69 73 20 61 20 63 6f   If this is a co
35397 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68  ntinuation of th
35398 65 20 63 75 72 72 65 6e 74 20 62 75 66 66 65 72  e current buffer
35399 20 74 6f 20 63 6f 70 79 2c 20 65 78 74 65 6e 64   to copy, extend
3539a 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 62 75 66  .    ** that buf
3539b 66 65 72 2e 20 20 6d 65 6d 63 70 79 28 29 20 73  fer.  memcpy() s
3539c 65 65 6d 73 20 74 6f 20 62 65 20 6d 6f 72 65 20  eems to be more 
3539d 65 66 66 69 63 69 65 6e 74 20 69 66 20 69 74 20  efficient if it 
3539e 68 61 73 20 61 0a 20 20 20 20 2a 2a 20 6c 6f 74  has a.    ** lot
3539f 73 20 6f 66 20 64 61 74 61 20 74 6f 20 63 6f 70  s of data to cop
353a0 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  y..    */.    if
353a1 28 20 64 6c 72 44 6f 63 44 61 74 61 28 72 65 61  ( dlrDocData(rea
353a2 64 65 72 73 5b 30 5d 2e 70 52 65 61 64 65 72 29  ders[0].pReader)
353a3 3d 3d 70 53 74 61 72 74 2b 6e 53 74 61 72 74 20  ==pStart+nStart 
353a4 29 7b 0a 20 20 20 20 20 20 6e 53 74 61 72 74 20  ){.      nStart 
353a5 2b 3d 20 64 6c 72 44 6f 63 44 61 74 61 42 79 74  += dlrDocDataByt
353a6 65 73 28 72 65 61 64 65 72 73 5b 30 5d 2e 70 52  es(readers[0].pR
353a7 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 65 6c 73  eader);.    }els
353a8 65 7b 0a 20 20 20 20 20 20 69 66 28 20 70 53 74  e{.      if( pSt
353a9 61 72 74 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  art!=0 ){.      
353aa 20 20 64 6c 77 41 70 70 65 6e 64 28 26 77 72 69    dlwAppend(&wri
353ab 74 65 72 2c 20 70 53 74 61 72 74 2c 20 6e 53 74  ter, pStart, nSt
353ac 61 72 74 2c 20 69 46 69 72 73 74 44 6f 63 69 64  art, iFirstDocid
353ad 2c 20 69 4c 61 73 74 44 6f 63 69 64 29 3b 0a 20  , iLastDocid);. 
353ae 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 53 74       }.      pSt
353af 61 72 74 20 3d 20 64 6c 72 44 6f 63 44 61 74 61  art = dlrDocData
353b0 28 72 65 61 64 65 72 73 5b 30 5d 2e 70 52 65 61  (readers[0].pRea
353b1 64 65 72 29 3b 0a 20 20 20 20 20 20 6e 53 74 61  der);.      nSta
353b2 72 74 20 3d 20 64 6c 72 44 6f 63 44 61 74 61 42  rt = dlrDocDataB
353b3 79 74 65 73 28 72 65 61 64 65 72 73 5b 30 5d 2e  ytes(readers[0].
353b4 70 52 65 61 64 65 72 29 3b 0a 20 20 20 20 20 20  pReader);.      
353b5 69 46 69 72 73 74 44 6f 63 69 64 20 3d 20 69 44  iFirstDocid = iD
353b6 6f 63 69 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ocid;.    }.    
353b7 69 4c 61 73 74 44 6f 63 69 64 20 3d 20 69 44 6f  iLastDocid = iDo
353b8 63 69 64 3b 0a 20 20 20 20 64 6c 72 53 74 65 70  cid;.    dlrStep
353b9 28 72 65 61 64 65 72 73 5b 30 5d 2e 70 52 65 61  (readers[0].pRea
353ba 64 65 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 72  der);..    /* Dr
353bb 6f 70 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f 6c  op all of the ol
353bc 64 65 72 20 65 6c 65 6d 65 6e 74 73 20 77 69 74  der elements wit
353bd 68 20 74 68 65 20 73 61 6d 65 20 64 6f 63 69 64  h the same docid
353be 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 31  . */.    for(i=1
353bf 3b 20 69 3c 6e 52 65 61 64 65 72 73 20 26 26 0a  ; i<nReaders &&.
353c0 20 20 20 20 20 20 20 20 20 20 20 20 20 21 64 6c               !dl
353c1 72 41 74 45 6e 64 28 72 65 61 64 65 72 73 5b 69  rAtEnd(readers[i
353c2 5d 2e 70 52 65 61 64 65 72 29 20 26 26 0a 20 20  ].pReader) &&.  
353c3 20 20 20 20 20 20 20 20 20 20 20 64 6c 72 44 6f             dlrDo
353c4 63 69 64 28 72 65 61 64 65 72 73 5b 69 5d 2e 70  cid(readers[i].p
353c5 52 65 61 64 65 72 29 3d 3d 69 44 6f 63 69 64 3b  Reader)==iDocid;
353c6 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 64 6c 72   i++){.      dlr
353c7 53 74 65 70 28 72 65 61 64 65 72 73 5b 69 5d 2e  Step(readers[i].
353c8 70 52 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a  pReader);.    }.
353c9 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68 65 20  .    /* Get the 
353ca 72 65 61 64 65 72 73 20 62 61 63 6b 20 69 6e 74  readers back int
353cb 6f 20 6f 72 64 65 72 2e 20 2a 2f 0a 20 20 20 20  o order. */.    
353cc 77 68 69 6c 65 28 20 69 2d 2d 3e 30 20 29 7b 0a  while( i-->0 ){.
353cd 20 20 20 20 20 20 6f 72 64 65 72 65 64 44 4c 52        orderedDLR
353ce 65 61 64 65 72 52 65 6f 72 64 65 72 28 72 65 61  eaderReorder(rea
353cf 64 65 72 73 2b 69 2c 20 6e 52 65 61 64 65 72 73  ders+i, nReaders
353d0 2d 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  -i);.    }.  }..
353d1 20 20 2f 2a 20 43 6f 70 79 20 6f 76 65 72 20 61    /* Copy over a
353d2 6e 79 20 72 65 6d 61 69 6e 69 6e 67 20 65 6c 65  ny remaining ele
353d3 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20 69 66 28 20  ments. */.  if( 
353d4 6e 53 74 61 72 74 3e 30 20 29 20 64 6c 77 41 70  nStart>0 ) dlwAp
353d5 70 65 6e 64 28 26 77 72 69 74 65 72 2c 20 70 53  pend(&writer, pS
353d6 74 61 72 74 2c 20 6e 53 74 61 72 74 2c 20 69 46  tart, nStart, iF
353d7 69 72 73 74 44 6f 63 69 64 2c 20 69 4c 61 73 74  irstDocid, iLast
353d8 44 6f 63 69 64 29 3b 0a 20 20 64 6c 77 44 65 73  Docid);.  dlwDes
353d9 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d  troy(&writer);.}
353da 0a 0a 2f 2a 20 48 65 6c 70 65 72 20 66 75 6e 63  ../* Helper func
353db 74 69 6f 6e 20 66 6f 72 20 70 6f 73 4c 69 73 74  tion for posList
353dc 55 6e 69 6f 6e 28 29 2e 20 20 43 6f 6d 70 61 72  Union().  Compar
353dd 65 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 70  es the current p
353de 6f 73 69 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65  osition.** betwe
353df 65 6e 20 6c 65 66 74 20 61 6e 64 20 72 69 67 68  en left and righ
353e0 74 2c 20 72 65 74 75 72 6e 69 6e 67 20 61 73 20  t, returning as 
353e1 73 74 61 6e 64 61 72 64 20 43 20 69 64 69 6f 6d  standard C idiom
353e2 20 6f 66 20 3c 30 20 69 66 0a 2a 2a 20 6c 65 66   of <0 if.** lef
353e3 74 3c 72 69 67 68 74 2c 20 3e 30 20 69 66 20 6c  t<right, >0 if l
353e4 65 66 74 3e 72 69 67 68 74 2c 20 61 6e 64 20 30  eft>right, and 0
353e5 20 69 66 20 6c 65 66 74 3d 3d 72 69 67 68 74 2e   if left==right.
353e6 20 20 22 45 6e 64 22 20 61 6c 77 61 79 73 0a 2a    "End" always.*
353e7 2a 20 63 6f 6d 70 61 72 65 73 20 67 72 65 61 74  * compares great
353e8 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  er..*/.static in
353e9 74 20 70 6f 73 4c 69 73 74 43 6d 70 28 50 4c 52  t posListCmp(PLR
353ea 65 61 64 65 72 20 2a 70 4c 65 66 74 2c 20 50 4c  eader *pLeft, PL
353eb 52 65 61 64 65 72 20 2a 70 52 69 67 68 74 29 7b  Reader *pRight){
353ec 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74  .  assert( pLeft
353ed 2d 3e 69 54 79 70 65 3d 3d 70 52 69 67 68 74 2d  ->iType==pRight-
353ee 3e 69 54 79 70 65 20 29 3b 0a 20 20 69 66 28 20  >iType );.  if( 
353ef 70 4c 65 66 74 2d 3e 69 54 79 70 65 3d 3d 44 4c  pLeft->iType==DL
353f0 5f 44 4f 43 49 44 53 20 29 20 72 65 74 75 72 6e  _DOCIDS ) return
353f1 20 30 3b 0a 0a 20 20 69 66 28 20 70 6c 72 41 74   0;..  if( plrAt
353f2 45 6e 64 28 70 4c 65 66 74 29 20 29 20 72 65 74  End(pLeft) ) ret
353f3 75 72 6e 20 70 6c 72 41 74 45 6e 64 28 70 52 69  urn plrAtEnd(pRi
353f4 67 68 74 29 20 3f 20 30 20 3a 20 31 3b 0a 20 20  ght) ? 0 : 1;.  
353f5 69 66 28 20 70 6c 72 41 74 45 6e 64 28 70 52 69  if( plrAtEnd(pRi
353f6 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 2d 31  ght) ) return -1
353f7 3b 0a 0a 20 20 69 66 28 20 70 6c 72 43 6f 6c 75  ;..  if( plrColu
353f8 6d 6e 28 70 4c 65 66 74 29 3c 70 6c 72 43 6f 6c  mn(pLeft)<plrCol
353f9 75 6d 6e 28 70 52 69 67 68 74 29 20 29 20 72 65  umn(pRight) ) re
353fa 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66 28 20 70  turn -1;.  if( p
353fb 6c 72 43 6f 6c 75 6d 6e 28 70 4c 65 66 74 29 3e  lrColumn(pLeft)>
353fc 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69 67 68 74  plrColumn(pRight
353fd 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 0a 20  ) ) return 1;.. 
353fe 20 69 66 28 20 70 6c 72 50 6f 73 69 74 69 6f 6e   if( plrPosition
353ff 28 70 4c 65 66 74 29 3c 70 6c 72 50 6f 73 69 74  (pLeft)<plrPosit
35400 69 6f 6e 28 70 52 69 67 68 74 29 20 29 20 72 65  ion(pRight) ) re
35401 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66 28 20 70  turn -1;.  if( p
35402 6c 72 50 6f 73 69 74 69 6f 6e 28 70 4c 65 66 74  lrPosition(pLeft
35403 29 3e 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 52  )>plrPosition(pR
35404 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 31  ight) ) return 1
35405 3b 0a 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 69  ;.  if( pLeft->i
35406 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f  Type==DL_POSITIO
35407 4e 53 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a  NS ) return 0;..
35408 20 20 69 66 28 20 70 6c 72 53 74 61 72 74 4f 66    if( plrStartOf
35409 66 73 65 74 28 70 4c 65 66 74 29 3c 70 6c 72 53  fset(pLeft)<plrS
3540a 74 61 72 74 4f 66 66 73 65 74 28 70 52 69 67 68  tartOffset(pRigh
3540b 74 29 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a  t) ) return -1;.
3540c 20 20 69 66 28 20 70 6c 72 53 74 61 72 74 4f 66    if( plrStartOf
3540d 66 73 65 74 28 70 4c 65 66 74 29 3e 70 6c 72 53  fset(pLeft)>plrS
3540e 74 61 72 74 4f 66 66 73 65 74 28 70 52 69 67 68  tartOffset(pRigh
3540f 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 0a  t) ) return 1;..
35410 20 20 69 66 28 20 70 6c 72 45 6e 64 4f 66 66 73    if( plrEndOffs
35411 65 74 28 70 4c 65 66 74 29 3c 70 6c 72 45 6e 64  et(pLeft)<plrEnd
35412 4f 66 66 73 65 74 28 70 52 69 67 68 74 29 20 29  Offset(pRight) )
35413 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66   return -1;.  if
35414 28 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28 70  ( plrEndOffset(p
35415 4c 65 66 74 29 3e 70 6c 72 45 6e 64 4f 66 66 73  Left)>plrEndOffs
35416 65 74 28 70 52 69 67 68 74 29 20 29 20 72 65 74  et(pRight) ) ret
35417 75 72 6e 20 31 3b 0a 0a 20 20 72 65 74 75 72 6e  urn 1;..  return
35418 20 30 3b 0a 7d 0a 0a 2f 2a 20 57 72 69 74 65 20   0;.}../* Write 
35419 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20 70 6f 73  the union of pos
3541a 69 74 69 6f 6e 20 6c 69 73 74 73 20 69 6e 20 70  ition lists in p
3541b 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 20  Left and pRight 
3541c 74 6f 20 70 4f 75 74 2e 0a 2a 2a 20 22 55 6e 69  to pOut..** "Uni
3541d 6f 6e 22 20 69 6e 20 74 68 69 73 20 63 61 73 65  on" in this case
3541e 20 6d 65 61 6e 69 6e 67 20 22 41 6c 6c 20 75 6e   meaning "All un
3541f 69 71 75 65 20 70 6f 73 69 74 69 6f 6e 20 74 75  ique position tu
35420 70 6c 65 73 22 2e 20 20 53 68 6f 75 6c 64 0a 2a  ples".  Should.*
35421 2a 20 77 6f 72 6b 20 77 69 74 68 20 61 6e 79 20  * work with any 
35422 64 6f 63 6c 69 73 74 20 74 79 70 65 2c 20 74 68  doclist type, th
35423 6f 75 67 68 20 62 6f 74 68 20 69 6e 70 75 74 73  ough both inputs
35424 20 61 6e 64 20 74 68 65 20 6f 75 74 70 75 74 0a   and the output.
35425 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65  ** should be the
35426 20 73 61 6d 65 20 74 79 70 65 2e 0a 2a 2f 0a 73   same type..*/.s
35427 74 61 74 69 63 20 76 6f 69 64 20 70 6f 73 4c 69  tatic void posLi
35428 73 74 55 6e 69 6f 6e 28 44 4c 52 65 61 64 65 72  stUnion(DLReader
35429 20 2a 70 4c 65 66 74 2c 20 44 4c 52 65 61 64 65   *pLeft, DLReade
3542a 72 20 2a 70 52 69 67 68 74 2c 20 44 4c 57 72 69  r *pRight, DLWri
3542b 74 65 72 20 2a 70 4f 75 74 29 7b 0a 20 20 50 4c  ter *pOut){.  PL
3542c 52 65 61 64 65 72 20 6c 65 66 74 2c 20 72 69 67  Reader left, rig
3542d 68 74 3b 0a 20 20 50 4c 57 72 69 74 65 72 20 77  ht;.  PLWriter w
3542e 72 69 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74  riter;..  assert
3542f 28 20 64 6c 72 44 6f 63 69 64 28 70 4c 65 66 74  ( dlrDocid(pLeft
35430 29 3d 3d 64 6c 72 44 6f 63 69 64 28 70 52 69 67  )==dlrDocid(pRig
35431 68 74 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ht) );.  assert(
35432 20 70 4c 65 66 74 2d 3e 69 54 79 70 65 3d 3d 70   pLeft->iType==p
35433 52 69 67 68 74 2d 3e 69 54 79 70 65 20 29 3b 0a  Right->iType );.
35434 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d    assert( pLeft-
35435 3e 69 54 79 70 65 3d 3d 70 4f 75 74 2d 3e 69 54  >iType==pOut->iT
35436 79 70 65 20 29 3b 0a 0a 20 20 70 6c 72 49 6e 69  ype );..  plrIni
35437 74 28 26 6c 65 66 74 2c 20 70 4c 65 66 74 29 3b  t(&left, pLeft);
35438 0a 20 20 70 6c 72 49 6e 69 74 28 26 72 69 67 68  .  plrInit(&righ
35439 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 70 6c  t, pRight);.  pl
3543a 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 70  wInit(&writer, p
3543b 4f 75 74 2c 20 64 6c 72 44 6f 63 69 64 28 70 4c  Out, dlrDocid(pL
3543c 65 66 74 29 29 3b 0a 0a 20 20 77 68 69 6c 65 28  eft));..  while(
3543d 20 21 70 6c 72 41 74 45 6e 64 28 26 6c 65 66 74   !plrAtEnd(&left
3543e 29 20 7c 7c 20 21 70 6c 72 41 74 45 6e 64 28 26  ) || !plrAtEnd(&
3543f 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 69 6e  right) ){.    in
35440 74 20 63 20 3d 20 70 6f 73 4c 69 73 74 43 6d 70  t c = posListCmp
35441 28 26 6c 65 66 74 2c 20 26 72 69 67 68 74 29 3b  (&left, &right);
35442 0a 20 20 20 20 69 66 28 20 63 3c 30 20 29 7b 0a  .    if( c<0 ){.
35443 20 20 20 20 20 20 70 6c 77 43 6f 70 79 28 26 77        plwCopy(&w
35444 72 69 74 65 72 2c 20 26 6c 65 66 74 29 3b 0a 20  riter, &left);. 
35445 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 6c 65       plrStep(&le
35446 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ft);.    }else i
35447 66 28 20 63 3e 30 20 29 7b 0a 20 20 20 20 20 20  f( c>0 ){.      
35448 70 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72 2c  plwCopy(&writer,
35449 20 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20 20   &right);.      
3544a 70 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b  plrStep(&right);
3544b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
3544c 20 20 70 6c 77 43 6f 70 79 28 26 77 72 69 74 65    plwCopy(&write
3544d 72 2c 20 26 6c 65 66 74 29 3b 0a 20 20 20 20 20  r, &left);.     
3544e 20 70 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b   plrStep(&left);
3544f 0a 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26  .      plrStep(&
35450 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20  right);.    }.  
35451 7d 0a 0a 20 20 70 6c 77 54 65 72 6d 69 6e 61 74  }..  plwTerminat
35452 65 28 26 77 72 69 74 65 72 29 3b 0a 20 20 70 6c  e(&writer);.  pl
35453 77 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72  wDestroy(&writer
35454 29 3b 0a 20 20 70 6c 72 44 65 73 74 72 6f 79 28  );.  plrDestroy(
35455 26 6c 65 66 74 29 3b 0a 20 20 70 6c 72 44 65 73  &left);.  plrDes
35456 74 72 6f 79 28 26 72 69 67 68 74 29 3b 0a 7d 0a  troy(&right);.}.
35457 0a 2f 2a 20 57 72 69 74 65 20 74 68 65 20 75 6e  ./* Write the un
35458 69 6f 6e 20 6f 66 20 64 6f 63 6c 69 73 74 73 20  ion of doclists 
35459 69 6e 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69  in pLeft and pRi
3545a 67 68 74 20 74 6f 20 70 4f 75 74 2e 20 20 46 6f  ght to pOut.  Fo
3545b 72 0a 2a 2a 20 64 6f 63 69 64 73 20 69 6e 20 63  r.** docids in c
3545c 6f 6d 6d 6f 6e 20 62 65 74 77 65 65 6e 20 74 68  ommon between th
3545d 65 20 69 6e 70 75 74 73 2c 20 74 68 65 20 75 6e  e inputs, the un
3545e 69 6f 6e 20 6f 66 20 74 68 65 20 70 6f 73 69 74  ion of the posit
3545f 69 6f 6e 0a 2a 2a 20 6c 69 73 74 73 20 69 73 20  ion.** lists is 
35460 77 72 69 74 74 65 6e 2e 20 20 49 6e 70 75 74 73  written.  Inputs
35461 20 61 6e 64 20 6f 75 74 70 75 74 73 20 61 72 65   and outputs are
35462 20 61 6c 77 61 79 73 20 74 79 70 65 20 44 4c 5f   always type DL_
35463 44 45 46 41 55 4c 54 2e 0a 2a 2f 0a 73 74 61 74  DEFAULT..*/.stat
35464 69 63 20 76 6f 69 64 20 64 6f 63 4c 69 73 74 55  ic void docListU
35465 6e 69 6f 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68  nion(.  const ch
35466 61 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20 6e  ar *pLeft, int n
35467 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68  Left,.  const ch
35468 61 72 20 2a 70 52 69 67 68 74 2c 20 69 6e 74 20  ar *pRight, int 
35469 6e 52 69 67 68 74 2c 0a 20 20 44 61 74 61 42 75  nRight,.  DataBu
3546a 66 66 65 72 20 2a 70 4f 75 74 20 20 20 20 20 20  ffer *pOut      
3546b 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6d  /* Write the com
3546c 62 69 6e 65 64 20 64 6f 63 6c 69 73 74 20 68 65  bined doclist he
3546d 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c 52 65 61  re */.){.  DLRea
3546e 64 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74 3b  der left, right;
3546f 0a 20 20 44 4c 57 72 69 74 65 72 20 77 72 69 74  .  DLWriter writ
35470 65 72 3b 0a 0a 20 20 69 66 28 20 6e 4c 65 66 74  er;..  if( nLeft
35471 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e  ==0 ){.    if( n
35472 52 69 67 68 74 21 3d 30 29 20 64 61 74 61 42 75  Right!=0) dataBu
35473 66 66 65 72 41 70 70 65 6e 64 28 70 4f 75 74 2c  fferAppend(pOut,
35474 20 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29   pRight, nRight)
35475 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
35476 7d 0a 20 20 69 66 28 20 6e 52 69 67 68 74 3d 3d  }.  if( nRight==
35477 30 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66  0 ){.    dataBuf
35478 66 65 72 41 70 70 65 6e 64 28 70 4f 75 74 2c 20  ferAppend(pOut, 
35479 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20  pLeft, nLeft);. 
3547a 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
3547b 20 20 64 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c    dlrInit(&left,
3547c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70 4c 65   DL_DEFAULT, pLe
3547d 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 64 6c  ft, nLeft);.  dl
3547e 72 49 6e 69 74 28 26 72 69 67 68 74 2c 20 44 4c  rInit(&right, DL
3547f 5f 44 45 46 41 55 4c 54 2c 20 70 52 69 67 68 74  _DEFAULT, pRight
35480 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20 64 6c 77  , nRight);.  dlw
35481 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 44 4c  Init(&writer, DL
35482 5f 44 45 46 41 55 4c 54 2c 20 70 4f 75 74 29 3b  _DEFAULT, pOut);
35483 0a 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72 41  ..  while( !dlrA
35484 74 45 6e 64 28 26 6c 65 66 74 29 20 7c 7c 20 21  tEnd(&left) || !
35485 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29  dlrAtEnd(&right)
35486 20 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72 41   ){.    if( dlrA
35487 74 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b 0a  tEnd(&right) ){.
35488 20 20 20 20 20 20 64 6c 77 43 6f 70 79 28 26 77        dlwCopy(&w
35489 72 69 74 65 72 2c 20 26 6c 65 66 74 29 3b 0a 20  riter, &left);. 
3548a 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65       dlrStep(&le
3548b 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ft);.    }else i
3548c 66 28 20 64 6c 72 41 74 45 6e 64 28 26 6c 65 66  f( dlrAtEnd(&lef
3548d 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 43  t) ){.      dlwC
3548e 6f 70 79 28 26 77 72 69 74 65 72 2c 20 26 72 69  opy(&writer, &ri
3548f 67 68 74 29 3b 0a 20 20 20 20 20 20 64 6c 72 53  ght);.      dlrS
35490 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20  tep(&right);.   
35491 20 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44 6f   }else if( dlrDo
35492 63 69 64 28 26 6c 65 66 74 29 3c 64 6c 72 44 6f  cid(&left)<dlrDo
35493 63 69 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20  cid(&right) ){. 
35494 20 20 20 20 20 64 6c 77 43 6f 70 79 28 26 77 72       dlwCopy(&wr
35495 69 74 65 72 2c 20 26 6c 65 66 74 29 3b 0a 20 20  iter, &left);.  
35496 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66      dlrStep(&lef
35497 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  t);.    }else if
35498 28 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74  ( dlrDocid(&left
35499 29 3e 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68  )>dlrDocid(&righ
3549a 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 43  t) ){.      dlwC
3549b 6f 70 79 28 26 77 72 69 74 65 72 2c 20 26 72 69  opy(&writer, &ri
3549c 67 68 74 29 3b 0a 20 20 20 20 20 20 64 6c 72 53  ght);.      dlrS
3549d 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20  tep(&right);.   
3549e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 6f   }else{.      po
3549f 73 4c 69 73 74 55 6e 69 6f 6e 28 26 6c 65 66 74  sListUnion(&left
354a0 2c 20 26 72 69 67 68 74 2c 20 26 77 72 69 74 65  , &right, &write
354a1 72 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65  r);.      dlrSte
354a2 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20  p(&left);.      
354a3 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b  dlrStep(&right);
354a4 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 6c  .    }.  }..  dl
354a5 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b  rDestroy(&left);
354a6 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72  .  dlrDestroy(&r
354a7 69 67 68 74 29 3b 0a 20 20 64 6c 77 44 65 73 74  ight);.  dlwDest
354a8 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a  roy(&writer);.}.
354a9 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ./* .** This fun
354aa 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 61 73  ction is used as
354ab 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6d 70   part of the imp
354ac 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70  lementation of p
354ad 68 72 61 73 65 20 61 6e 64 0a 2a 2a 20 4e 45 41  hrase and.** NEA
354ae 52 20 6d 61 74 63 68 69 6e 67 2e 0a 2a 2a 0a 2a  R matching..**.*
354af 2a 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67  * pLeft and pRig
354b0 68 74 20 61 72 65 20 44 4c 52 65 61 64 65 72 73  ht are DLReaders
354b1 20 70 6f 73 69 74 69 6f 6e 65 64 20 74 6f 20 74   positioned to t
354b2 68 65 20 73 61 6d 65 20 64 6f 63 69 64 20 69 6e  he same docid in
354b3 0a 2a 2a 20 6c 69 73 74 73 20 6f 66 20 74 79 70  .** lists of typ
354b4 65 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 2e 20 54  e DL_POSITION. T
354b5 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 72 69  his function wri
354b6 74 65 73 20 61 6e 20 65 6e 74 72 79 20 74 6f 20  tes an entry to 
354b7 74 68 65 0a 2a 2a 20 44 4c 57 72 69 74 65 72 20  the.** DLWriter 
354b8 70 4f 75 74 20 66 6f 72 20 65 61 63 68 20 70 6f  pOut for each po
354b9 73 69 74 69 6f 6e 20 69 6e 20 70 52 69 67 68 74  sition in pRight
354ba 20 74 68 61 74 20 69 73 20 6c 65 73 73 20 74 68   that is less th
354bb 61 6e 0a 2a 2a 20 28 6e 4e 65 61 72 2b 31 29 20  an.** (nNear+1) 
354bc 67 72 65 61 74 65 72 20 28 62 75 74 20 6e 6f 74  greater (but not
354bd 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 73 6d 61   equal to or sma
354be 6c 6c 65 72 29 20 74 68 61 6e 20 61 20 70 6f 73  ller) than a pos
354bf 69 74 69 6f 6e 20 0a 2a 2a 20 69 6e 20 70 4c 65  ition .** in pLe
354c0 66 74 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  ft. For example,
354c1 20 69 66 20 6e 4e 65 61 72 20 69 73 20 30 2c 20   if nNear is 0, 
354c2 61 6e 64 20 74 68 65 20 70 6f 73 69 74 69 6f 6e  and the position
354c3 73 20 63 6f 6e 74 61 69 6e 65 64 0a 2a 2a 20 62  s contained.** b
354c4 79 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67  y pLeft and pRig
354c5 68 74 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  ht are:.**.**   
354c6 20 70 4c 65 66 74 3a 20 20 35 20 31 30 20 31 35   pLeft:  5 10 15
354c7 20 32 30 0a 2a 2a 20 20 20 20 70 52 69 67 68 74   20.**    pRight
354c8 3a 20 36 20 20 39 20 31 37 20 32 31 0a 2a 2a 0a  : 6  9 17 21.**.
354c9 2a 2a 20 74 68 65 6e 20 74 68 65 20 64 6f 63 69  ** then the doci
354ca 64 20 69 73 20 61 64 64 65 64 20 74 6f 20 70 4f  d is added to pO
354cb 75 74 2e 20 49 66 20 70 4f 75 74 20 69 73 20 6f  ut. If pOut is o
354cc 66 20 74 79 70 65 20 44 4c 5f 50 4f 53 49 54 49  f type DL_POSITI
354cd 4f 4e 53 2c 0a 2a 2a 20 74 68 65 6e 20 61 20 70  ONS,.** then a p
354ce 6f 73 69 74 69 6f 6e 69 64 73 20 22 36 22 20 61  ositionids "6" a
354cf 6e 64 20 22 32 31 22 20 61 72 65 20 61 6c 73 6f  nd "21" are also
354d0 20 61 64 64 65 64 20 74 6f 20 70 4f 75 74 2e 0a   added to pOut..
354d1 2a 2a 0a 2a 2a 20 49 66 20 62 6f 6f 6c 65 61 6e  **.** If boolean
354d2 20 61 72 67 75 6d 65 6e 74 20 69 73 53 61 76 65   argument isSave
354d3 4c 65 66 74 20 69 73 20 74 72 75 65 2c 20 74 68  Left is true, th
354d4 65 6e 20 70 6f 73 69 74 69 6f 6e 69 64 73 20 61  en positionids a
354d5 72 65 20 63 6f 70 69 65 64 0a 2a 2a 20 66 72 6f  re copied.** fro
354d6 6d 20 70 4c 65 66 74 20 69 6e 73 74 65 61 64 20  m pLeft instead 
354d7 6f 66 20 70 52 69 67 68 74 2e 20 49 6e 20 74 68  of pRight. In th
354d8 65 20 65 78 61 6d 70 6c 65 20 61 62 6f 76 65 2c  e example above,
354d9 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20 22   the positions "
354da 35 22 0a 2a 2a 20 61 6e 64 20 22 32 30 22 20 77  5".** and "20" w
354db 6f 75 6c 64 20 62 65 20 61 64 64 65 64 20 69 6e  ould be added in
354dc 73 74 65 61 64 20 6f 66 20 22 36 22 20 61 6e 64  stead of "6" and
354dd 20 22 32 31 22 2e 0a 2a 2f 0a 73 74 61 74 69 63   "21"..*/.static
354de 20 76 6f 69 64 20 70 6f 73 4c 69 73 74 50 68 72   void posListPhr
354df 61 73 65 4d 65 72 67 65 28 0a 20 20 44 4c 52 65  aseMerge(.  DLRe
354e0 61 64 65 72 20 2a 70 4c 65 66 74 2c 20 0a 20 20  ader *pLeft, .  
354e1 44 4c 52 65 61 64 65 72 20 2a 70 52 69 67 68 74  DLReader *pRight
354e2 2c 0a 20 20 69 6e 74 20 6e 4e 65 61 72 2c 0a 20  ,.  int nNear,. 
354e3 20 69 6e 74 20 69 73 53 61 76 65 4c 65 66 74 2c   int isSaveLeft,
354e4 0a 20 20 44 4c 57 72 69 74 65 72 20 2a 70 4f 75  .  DLWriter *pOu
354e5 74 0a 29 7b 0a 20 20 50 4c 52 65 61 64 65 72 20  t.){.  PLReader 
354e6 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 50  left, right;.  P
354e7 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a  LWriter writer;.
354e8 20 20 69 6e 74 20 6d 61 74 63 68 20 3d 20 30 3b    int match = 0;
354e9 0a 0a 20 20 61 73 73 65 72 74 28 20 64 6c 72 44  ..  assert( dlrD
354ea 6f 63 69 64 28 70 4c 65 66 74 29 3d 3d 64 6c 72  ocid(pLeft)==dlr
354eb 44 6f 63 69 64 28 70 52 69 67 68 74 29 20 29 3b  Docid(pRight) );
354ec 0a 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 2d  .  assert( pOut-
354ed 3e 69 54 79 70 65 21 3d 44 4c 5f 50 4f 53 49 54  >iType!=DL_POSIT
354ee 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 3b 0a  IONS_OFFSETS );.
354ef 0a 20 20 70 6c 72 49 6e 69 74 28 26 6c 65 66 74  .  plrInit(&left
354f0 2c 20 70 4c 65 66 74 29 3b 0a 20 20 70 6c 72 49  , pLeft);.  plrI
354f1 6e 69 74 28 26 72 69 67 68 74 2c 20 70 52 69 67  nit(&right, pRig
354f2 68 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21  ht);..  while( !
354f3 70 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29 20  plrAtEnd(&left) 
354f4 26 26 20 21 70 6c 72 41 74 45 6e 64 28 26 72 69  && !plrAtEnd(&ri
354f5 67 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28 20  ght) ){.    if( 
354f6 70 6c 72 43 6f 6c 75 6d 6e 28 26 6c 65 66 74 29  plrColumn(&left)
354f7 3c 70 6c 72 43 6f 6c 75 6d 6e 28 26 72 69 67 68  <plrColumn(&righ
354f8 74 29 20 29 7b 0a 20 20 20 20 20 20 70 6c 72 53  t) ){.      plrS
354f9 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20  tep(&left);.    
354fa 7d 65 6c 73 65 20 69 66 28 20 70 6c 72 43 6f 6c  }else if( plrCol
354fb 75 6d 6e 28 26 6c 65 66 74 29 3e 70 6c 72 43 6f  umn(&left)>plrCo
354fc 6c 75 6d 6e 28 26 72 69 67 68 74 29 20 29 7b 0a  lumn(&right) ){.
354fd 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 72        plrStep(&r
354fe 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ight);.    }else
354ff 20 69 66 28 20 70 6c 72 50 6f 73 69 74 69 6f 6e   if( plrPosition
35500 28 26 6c 65 66 74 29 3e 3d 70 6c 72 50 6f 73 69  (&left)>=plrPosi
35501 74 69 6f 6e 28 26 72 69 67 68 74 29 20 29 7b 0a  tion(&right) ){.
35502 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 72        plrStep(&r
35503 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ight);.    }else
35504 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 6c 72  {.      if( (plr
35505 50 6f 73 69 74 69 6f 6e 28 26 72 69 67 68 74 29  Position(&right)
35506 2d 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26 6c 65  -plrPosition(&le
35507 66 74 29 29 3c 3d 28 6e 4e 65 61 72 2b 31 29 20  ft))<=(nNear+1) 
35508 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  ){.        if( !
35509 6d 61 74 63 68 20 29 7b 0a 20 20 20 20 20 20 20  match ){.       
3550a 20 20 20 70 6c 77 49 6e 69 74 28 26 77 72 69 74     plwInit(&writ
3550b 65 72 2c 20 70 4f 75 74 2c 20 64 6c 72 44 6f 63  er, pOut, dlrDoc
3550c 69 64 28 70 4c 65 66 74 29 29 3b 0a 20 20 20 20  id(pLeft));.    
3550d 20 20 20 20 20 20 6d 61 74 63 68 20 3d 20 31 3b        match = 1;
3550e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
3550f 20 20 20 69 66 28 20 21 69 73 53 61 76 65 4c 65     if( !isSaveLe
35510 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ft ){.          
35511 70 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c 20  plwAdd(&writer, 
35512 70 6c 72 43 6f 6c 75 6d 6e 28 26 72 69 67 68 74  plrColumn(&right
35513 29 2c 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26  ), plrPosition(&
35514 72 69 67 68 74 29 2c 20 30 2c 20 30 29 3b 0a 20  right), 0, 0);. 
35515 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
35516 20 20 20 20 20 20 20 20 70 6c 77 41 64 64 28 26          plwAdd(&
35517 77 72 69 74 65 72 2c 20 70 6c 72 43 6f 6c 75 6d  writer, plrColum
35518 6e 28 26 6c 65 66 74 29 2c 20 70 6c 72 50 6f 73  n(&left), plrPos
35519 69 74 69 6f 6e 28 26 6c 65 66 74 29 2c 20 30 2c  ition(&left), 0,
3551a 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   0);.        }. 
3551b 20 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26         plrStep(&
3551c 72 69 67 68 74 29 3b 0a 20 20 20 20 20 20 7d 65  right);.      }e
3551d 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 6c 72  lse{.        plr
3551e 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20  Step(&left);.   
3551f 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
35520 20 20 69 66 28 20 6d 61 74 63 68 20 29 7b 0a 20    if( match ){. 
35521 20 20 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28     plwTerminate(
35522 26 77 72 69 74 65 72 29 3b 0a 20 20 20 20 70 6c  &writer);.    pl
35523 77 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72  wDestroy(&writer
35524 29 3b 0a 20 20 7d 0a 0a 20 20 70 6c 72 44 65 73  );.  }..  plrDes
35525 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 70  troy(&left);.  p
35526 6c 72 44 65 73 74 72 6f 79 28 26 72 69 67 68 74  lrDestroy(&right
35527 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  );.}../*.** Comp
35528 61 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 70  are the values p
35529 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65  ointed to by the
3552a 20 50 4c 52 65 61 64 65 72 73 20 70 61 73 73 65   PLReaders passe
3552b 64 20 61 73 20 61 72 67 75 6d 65 6e 74 73 2e 20  d as arguments. 
3552c 0a 2a 2a 20 52 65 74 75 72 6e 20 2d 31 20 69 66  .** Return -1 if
3552d 20 74 68 65 20 76 61 6c 75 65 20 70 6f 69 6e 74   the value point
3552e 65 64 20 74 6f 20 62 79 20 70 4c 65 66 74 20 69  ed to by pLeft i
3552f 73 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73  s considered les
35530 73 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 76 61  s than.** the va
35531 6c 75 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  lue pointed to b
35532 79 20 70 52 69 67 68 74 2c 20 2b 31 20 69 66 20  y pRight, +1 if 
35533 69 74 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  it is considered
35534 20 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e   greater.** than
35535 20 69 74 2c 20 6f 72 20 30 20 69 66 20 69 74 20   it, or 0 if it 
35536 69 73 20 65 71 75 61 6c 2e 20 69 2e 65 2e 0a 2a  is equal. i.e..*
35537 2a 0a 2a 2a 20 20 20 20 20 28 2a 70 4c 65 66 74  *.**     (*pLeft
35538 20 2d 20 2a 70 52 69 67 68 74 29 0a 2a 2a 0a 2a   - *pRight).**.*
35539 2a 20 41 20 50 4c 52 65 61 64 65 72 20 74 68 61  * A PLReader tha
3553a 74 20 69 73 20 69 6e 20 74 68 65 20 45 4f 46 20  t is in the EOF 
3553b 63 6f 6e 64 69 74 69 6f 6e 20 69 73 20 63 6f 6e  condition is con
3553c 73 69 64 65 72 65 64 20 67 72 65 61 74 65 72 20  sidered greater 
3553d 74 68 61 6e 0a 2a 2a 20 61 6e 79 20 6f 74 68 65  than.** any othe
3553e 72 2e 20 49 66 20 6e 65 69 74 68 65 72 20 61 72  r. If neither ar
3553f 67 75 6d 65 6e 74 20 69 73 20 69 6e 20 45 4f 46  gument is in EOF
35540 20 73 74 61 74 65 2c 20 74 68 65 20 72 65 74 75   state, the retu
35541 72 6e 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 70  rn value of.** p
35542 6c 72 43 6f 6c 75 6d 6e 28 29 20 69 73 20 75 73  lrColumn() is us
35543 65 64 2e 20 49 66 20 74 68 65 20 70 6c 72 43 6f  ed. If the plrCo
35544 6c 75 6d 6e 28 29 20 76 61 6c 75 65 73 20 61 72  lumn() values ar
35545 65 20 65 71 75 61 6c 2c 20 74 68 65 0a 2a 2a 20  e equal, the.** 
35546 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 6f 6e  comparison is on
35547 20 74 68 65 20 62 61 73 69 73 20 6f 66 20 70 6c   the basis of pl
35548 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a  rPosition()..*/.
35549 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72 43 6f  static int plrCo
3554a 6d 70 61 72 65 28 50 4c 52 65 61 64 65 72 20 2a  mpare(PLReader *
3554b 70 4c 65 66 74 2c 20 50 4c 52 65 61 64 65 72 20  pLeft, PLReader 
3554c 2a 70 52 69 67 68 74 29 7b 0a 20 20 61 73 73 65  *pRight){.  asse
3554d 72 74 28 21 70 6c 72 41 74 45 6e 64 28 70 4c 65  rt(!plrAtEnd(pLe
3554e 66 74 29 20 7c 7c 20 21 70 6c 72 41 74 45 6e 64  ft) || !plrAtEnd
3554f 28 70 52 69 67 68 74 29 29 3b 0a 0a 20 20 69 66  (pRight));..  if
35550 28 20 70 6c 72 41 74 45 6e 64 28 70 52 69 67 68  ( plrAtEnd(pRigh
35551 74 29 20 7c 7c 20 70 6c 72 41 74 45 6e 64 28 70  t) || plrAtEnd(p
35552 4c 65 66 74 29 20 29 7b 0a 20 20 20 20 72 65 74  Left) ){.    ret
35553 75 72 6e 20 28 70 6c 72 41 74 45 6e 64 28 70 52  urn (plrAtEnd(pR
35554 69 67 68 74 29 20 3f 20 2d 31 20 3a 20 31 29 3b  ight) ? -1 : 1);
35555 0a 20 20 7d 0a 20 20 69 66 28 20 70 6c 72 43 6f  .  }.  if( plrCo
35556 6c 75 6d 6e 28 70 4c 65 66 74 29 21 3d 70 6c 72  lumn(pLeft)!=plr
35557 43 6f 6c 75 6d 6e 28 70 52 69 67 68 74 29 20 29  Column(pRight) )
35558 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 28 70  {.    return ((p
35559 6c 72 43 6f 6c 75 6d 6e 28 70 4c 65 66 74 29 3c  lrColumn(pLeft)<
3555a 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69 67 68 74  plrColumn(pRight
3555b 29 29 20 3f 20 2d 31 20 3a 20 31 29 3b 0a 20 20  )) ? -1 : 1);.  
3555c 7d 0a 20 20 69 66 28 20 70 6c 72 50 6f 73 69 74  }.  if( plrPosit
3555d 69 6f 6e 28 70 4c 65 66 74 29 21 3d 70 6c 72 50  ion(pLeft)!=plrP
3555e 6f 73 69 74 69 6f 6e 28 70 52 69 67 68 74 29 20  osition(pRight) 
3555f 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 28  ){.    return ((
35560 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 4c 65 66  plrPosition(pLef
35561 74 29 3c 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70  t)<plrPosition(p
35562 52 69 67 68 74 29 29 20 3f 20 2d 31 20 3a 20 31  Right)) ? -1 : 1
35563 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
35564 30 3b 0a 7d 0a 0a 2f 2a 20 57 65 20 68 61 76 65  0;.}../* We have
35565 20 74 77 6f 20 64 6f 63 6c 69 73 74 73 20 77 69   two doclists wi
35566 74 68 20 70 6f 73 69 74 69 6f 6e 73 3a 20 20 70  th positions:  p
35567 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 2e  Left and pRight.
35568 20 44 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e   Depending.** on
35569 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
3556a 65 20 6e 4e 65 61 72 20 70 61 72 61 6d 65 74 65  e nNear paramete
3556b 72 2c 20 70 65 72 66 6f 72 6d 20 65 69 74 68 65  r, perform eithe
3556c 72 20 61 20 70 68 72 61 73 65 0a 2a 2a 20 69 6e  r a phrase.** in
3556d 74 65 72 73 65 63 74 69 6f 6e 20 28 69 66 20 6e  tersection (if n
3556e 4e 65 61 72 3d 3d 30 29 20 6f 72 20 61 20 4e 45  Near==0) or a NE
3556f 41 52 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20  AR intersection 
35570 28 69 66 20 6e 4e 65 61 72 3e 30 29 0a 2a 2a 20  (if nNear>0).** 
35571 61 6e 64 20 77 72 69 74 65 20 74 68 65 20 72 65  and write the re
35572 73 75 6c 74 73 20 69 6e 74 6f 20 70 4f 75 74 2e  sults into pOut.
35573 0a 2a 2a 0a 2a 2a 20 41 20 70 68 72 61 73 65 20  .**.** A phrase 
35574 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 6d 65 61  intersection mea
35575 6e 73 20 74 68 61 74 20 74 77 6f 20 64 6f 63 75  ns that two docu
35576 6d 65 6e 74 73 20 6f 6e 6c 79 20 6d 61 74 63 68  ments only match
35577 0a 2a 2a 20 69 66 20 70 4c 65 66 74 2e 69 50 6f  .** if pLeft.iPo
35578 73 2b 31 3d 3d 70 52 69 67 68 74 2e 69 50 6f 73  s+1==pRight.iPos
35579 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 45 41 52 20 69  ..**.** A NEAR i
3557a 6e 74 65 72 73 65 63 74 69 6f 6e 20 6d 65 61 6e  ntersection mean
3557b 73 20 74 68 61 74 20 74 77 6f 20 64 6f 63 75 6d  s that two docum
3557c 65 6e 74 73 20 6f 6e 6c 79 20 6d 61 74 63 68 20  ents only match 
3557d 69 66 20 0a 2a 2a 20 28 61 62 73 28 70 4c 65 66  if .** (abs(pLef
3557e 74 2e 69 50 6f 73 2d 70 52 69 67 68 74 2e 69 50  t.iPos-pRight.iP
3557f 6f 73 29 3c 6e 4e 65 61 72 29 2e 0a 2a 2a 0a 2a  os)<nNear)..**.*
35580 2a 20 49 66 20 61 20 4e 45 41 52 20 69 6e 74 65  * If a NEAR inte
35581 72 73 65 63 74 69 6f 6e 20 69 73 20 72 65 71 75  rsection is requ
35582 65 73 74 65 64 2c 20 74 68 65 6e 20 74 68 65 20  ested, then the 
35583 6e 50 68 72 61 73 65 20 61 72 67 75 6d 65 6e 74  nPhrase argument
35584 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 70 61   should.** be pa
35585 73 73 65 64 20 74 68 65 20 6e 75 6d 62 65 72 20  ssed the number 
35586 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20 74 68 65  of tokens in the
35587 20 74 77 6f 20 6f 70 65 72 61 6e 64 73 20 74 6f   two operands to
35588 20 74 68 65 20 4e 45 41 52 20 6f 70 65 72 61 74   the NEAR operat
35589 6f 72 0a 2a 2a 20 63 6f 6d 62 69 6e 65 64 2e 20  or.** combined. 
3558a 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a  For example:.**.
3558b 2a 2a 20 20 20 20 20 20 20 51 75 65 72 79 20 73  **       Query s
3558c 79 6e 74 61 78 20 20 20 20 20 20 20 20 20 20 20  yntax           
3558d 20 20 20 20 6e 50 68 72 61 73 65 0a 2a 2a 20 20      nPhrase.**  
3558e 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d      ------------
3558f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
35590 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20  --------.**     
35591 20 20 22 41 20 42 20 43 22 20 4e 45 41 52 20 22    "A B C" NEAR "
35592 44 20 45 22 20 20 20 20 20 20 20 20 20 35 0a 2a  D E"         5.*
35593 2a 20 20 20 20 20 20 20 41 20 4e 45 41 52 20 42  *       A NEAR B
35594 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35595 20 20 20 32 0a 2a 2a 0a 2a 2a 20 69 54 79 70 65     2.**.** iType
35596 20 63 6f 6e 74 72 6f 6c 73 20 74 68 65 20 74 79   controls the ty
35597 70 65 20 6f 66 20 64 61 74 61 20 77 72 69 74 74  pe of data writt
35598 65 6e 20 74 6f 20 70 4f 75 74 2e 20 20 49 66 20  en to pOut.  If 
35599 69 54 79 70 65 20 69 73 0a 2a 2a 20 44 4c 5f 50  iType is.** DL_P
3559a 4f 53 49 54 49 4f 4e 53 2c 20 74 68 65 20 70 6f  OSITIONS, the po
3559b 73 69 74 69 6f 6e 73 20 61 72 65 20 74 68 6f 73  sitions are thos
3559c 65 20 66 72 6f 6d 20 70 52 69 67 68 74 2e 0a 2a  e from pRight..*
3559d 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f  /.static void do
3559e 63 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67 65  cListPhraseMerge
3559f 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
355a0 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74  pLeft, int nLeft
355a1 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
355a2 70 52 69 67 68 74 2c 20 69 6e 74 20 6e 52 69 67  pRight, int nRig
355a3 68 74 2c 0a 20 20 69 6e 74 20 6e 4e 65 61 72 2c  ht,.  int nNear,
355a4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30              /* 0
355a5 20 66 6f 72 20 61 20 70 68 72 61 73 65 20 6d 65   for a phrase me
355a6 72 67 65 2c 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f  rge, non-zero fo
355a7 72 20 61 20 4e 45 41 52 20 6d 65 72 67 65 20 2a  r a NEAR merge *
355a8 2f 0a 20 20 69 6e 74 20 6e 50 68 72 61 73 65 2c  /.  int nPhrase,
355a9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
355aa 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e  ber of tokens in
355ab 20 6c 65 66 74 2b 72 69 67 68 74 20 6f 70 65 72   left+right oper
355ac 61 6e 64 73 20 74 6f 20 4e 45 41 52 20 2a 2f 0a  ands to NEAR */.
355ad 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54    DocListType iT
355ae 79 70 65 2c 20 20 20 20 2f 2a 20 54 79 70 65 20  ype,    /* Type 
355af 6f 66 20 64 6f 63 6c 69 73 74 20 74 6f 20 77 72  of doclist to wr
355b0 69 74 65 20 74 6f 20 70 4f 75 74 20 2a 2f 0a 20  ite to pOut */. 
355b1 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 4f 75   DataBuffer *pOu
355b2 74 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20  t      /* Write 
355b3 74 68 65 20 63 6f 6d 62 69 6e 65 64 20 64 6f 63  the combined doc
355b4 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  list here */.){.
355b5 20 20 44 4c 52 65 61 64 65 72 20 6c 65 66 74 2c    DLReader left,
355b6 20 72 69 67 68 74 3b 0a 20 20 44 4c 57 72 69 74   right;.  DLWrit
355b7 65 72 20 77 72 69 74 65 72 3b 0a 0a 20 20 69 66  er writer;..  if
355b8 28 20 6e 4c 65 66 74 3d 3d 30 20 7c 7c 20 6e 52  ( nLeft==0 || nR
355b9 69 67 68 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  ight==0 ) return
355ba 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 54 79  ;..  assert( iTy
355bb 70 65 21 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  pe!=DL_POSITIONS
355bc 5f 4f 46 46 53 45 54 53 20 29 3b 0a 0a 20 20 64  _OFFSETS );..  d
355bd 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c 20 44 4c  lrInit(&left, DL
355be 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 70 4c 65 66  _POSITIONS, pLef
355bf 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 64 6c 72  t, nLeft);.  dlr
355c0 49 6e 69 74 28 26 72 69 67 68 74 2c 20 44 4c 5f  Init(&right, DL_
355c1 50 4f 53 49 54 49 4f 4e 53 2c 20 70 52 69 67 68  POSITIONS, pRigh
355c2 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20 64 6c  t, nRight);.  dl
355c3 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 69  wInit(&writer, i
355c4 54 79 70 65 2c 20 70 4f 75 74 29 3b 0a 0a 20 20  Type, pOut);..  
355c5 77 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64  while( !dlrAtEnd
355c6 28 26 6c 65 66 74 29 20 26 26 20 21 64 6c 72 41  (&left) && !dlrA
355c7 74 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b 0a  tEnd(&right) ){.
355c8 20 20 20 20 69 66 28 20 64 6c 72 44 6f 63 69 64      if( dlrDocid
355c9 28 26 6c 65 66 74 29 3c 64 6c 72 44 6f 63 69 64  (&left)<dlrDocid
355ca 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20  (&right) ){.    
355cb 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66 74 29    dlrStep(&left)
355cc 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
355cd 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74 29  dlrDocid(&right)
355ce 3c 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29  <dlrDocid(&left)
355cf 20 29 7b 0a 20 20 20 20 20 20 64 6c 72 53 74 65   ){.      dlrSte
355d0 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d  p(&right);.    }
355d1 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20  else{.      if( 
355d2 6e 4e 65 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20  nNear==0 ){.    
355d3 20 20 20 20 70 6f 73 4c 69 73 74 50 68 72 61 73      posListPhras
355d4 65 4d 65 72 67 65 28 26 6c 65 66 74 2c 20 26 72  eMerge(&left, &r
355d5 69 67 68 74 2c 20 30 2c 20 30 2c 20 26 77 72 69  ight, 0, 0, &wri
355d6 74 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ter);.      }els
355d7 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  e{.        /* Th
355d8 69 73 20 63 61 73 65 20 6f 63 63 75 72 73 20 77  is case occurs w
355d9 68 65 6e 20 74 77 6f 20 74 65 72 6d 73 20 28 73  hen two terms (s
355da 69 6d 70 6c 65 20 74 65 72 6d 73 20 6f 72 20 70  imple terms or p
355db 68 72 61 73 65 73 29 20 61 72 65 0a 20 20 20 20  hrases) are.    
355dc 20 20 20 20 20 2a 20 63 6f 6e 6e 65 63 74 65 64       * connected
355dd 20 62 79 20 61 20 4e 45 41 52 20 6f 70 65 72 61   by a NEAR opera
355de 74 6f 72 2c 20 73 70 61 6e 20 28 6e 4e 65 61 72  tor, span (nNear
355df 2b 31 29 2e 20 69 2e 65 2e 0a 20 20 20 20 20 20  +1). i.e..      
355e0 20 20 20 2a 0a 20 20 20 20 20 20 20 20 20 2a 20     *.         * 
355e1 20 20 20 20 27 22 74 65 72 72 69 62 6c 65 20 63      '"terrible c
355e2 6f 6d 70 61 6e 79 22 20 4e 45 41 52 20 77 69 64  ompany" NEAR wid
355e3 67 65 74 27 0a 20 20 20 20 20 20 20 20 20 2a 2f  get'.         */
355e4 0a 20 20 20 20 20 20 20 20 44 61 74 61 42 75 66  .        DataBuf
355e5 66 65 72 20 6f 6e 65 20 3d 20 7b 30 2c 20 30 2c  fer one = {0, 0,
355e6 20 30 7d 3b 0a 20 20 20 20 20 20 20 20 44 61 74   0};.        Dat
355e7 61 42 75 66 66 65 72 20 74 77 6f 20 3d 20 7b 30  aBuffer two = {0
355e8 2c 20 30 2c 20 30 7d 3b 0a 0a 20 20 20 20 20 20  , 0, 0};..      
355e9 20 20 44 4c 57 72 69 74 65 72 20 64 6c 77 72 69    DLWriter dlwri
355ea 74 65 72 32 3b 0a 20 20 20 20 20 20 20 20 44 4c  ter2;.        DL
355eb 52 65 61 64 65 72 20 64 72 31 20 3d 20 7b 30 2c  Reader dr1 = {0,
355ec 20 30 2c 20 30 2c 20 30 2c 20 30 7d 3b 20 0a 20   0, 0, 0, 0}; . 
355ed 20 20 20 20 20 20 20 44 4c 52 65 61 64 65 72 20         DLReader 
355ee 64 72 32 20 3d 20 7b 30 2c 20 30 2c 20 30 2c 20  dr2 = {0, 0, 0, 
355ef 30 2c 20 30 7d 3b 0a 0a 20 20 20 20 20 20 20 20  0, 0};..        
355f0 64 6c 77 49 6e 69 74 28 26 64 6c 77 72 69 74 65  dlwInit(&dlwrite
355f1 72 32 2c 20 69 54 79 70 65 2c 20 26 6f 6e 65 29  r2, iType, &one)
355f2 3b 0a 20 20 20 20 20 20 20 20 70 6f 73 4c 69 73  ;.        posLis
355f3 74 50 68 72 61 73 65 4d 65 72 67 65 28 26 72 69  tPhraseMerge(&ri
355f4 67 68 74 2c 20 26 6c 65 66 74 2c 20 6e 4e 65 61  ght, &left, nNea
355f5 72 2d 33 2b 6e 50 68 72 61 73 65 2c 20 31 2c 20  r-3+nPhrase, 1, 
355f6 26 64 6c 77 72 69 74 65 72 32 29 3b 0a 20 20 20  &dlwriter2);.   
355f7 20 20 20 20 20 64 6c 77 49 6e 69 74 28 26 64 6c       dlwInit(&dl
355f8 77 72 69 74 65 72 32 2c 20 69 54 79 70 65 2c 20  writer2, iType, 
355f9 26 74 77 6f 29 3b 0a 20 20 20 20 20 20 20 20 70  &two);.        p
355fa 6f 73 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67  osListPhraseMerg
355fb 65 28 26 6c 65 66 74 2c 20 26 72 69 67 68 74 2c  e(&left, &right,
355fc 20 6e 4e 65 61 72 2d 31 2c 20 30 2c 20 26 64 6c   nNear-1, 0, &dl
355fd 77 72 69 74 65 72 32 29 3b 0a 0a 20 20 20 20 20  writer2);..     
355fe 20 20 20 69 66 28 20 6f 6e 65 2e 6e 44 61 74 61     if( one.nData
355ff 29 20 64 6c 72 49 6e 69 74 28 26 64 72 31 2c 20  ) dlrInit(&dr1, 
35600 69 54 79 70 65 2c 20 6f 6e 65 2e 70 44 61 74 61  iType, one.pData
35601 2c 20 6f 6e 65 2e 6e 44 61 74 61 29 3b 0a 20 20  , one.nData);.  
35602 20 20 20 20 20 20 69 66 28 20 74 77 6f 2e 6e 44        if( two.nD
35603 61 74 61 29 20 64 6c 72 49 6e 69 74 28 26 64 72  ata) dlrInit(&dr
35604 32 2c 20 69 54 79 70 65 2c 20 74 77 6f 2e 70 44  2, iType, two.pD
35605 61 74 61 2c 20 74 77 6f 2e 6e 44 61 74 61 29 3b  ata, two.nData);
35606 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 64  ..        if( !d
35607 6c 72 41 74 45 6e 64 28 26 64 72 31 29 20 7c 7c  lrAtEnd(&dr1) ||
35608 20 21 64 6c 72 41 74 45 6e 64 28 26 64 72 32 29   !dlrAtEnd(&dr2)
35609 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 50 4c   ){.          PL
3560a 52 65 61 64 65 72 20 70 72 31 20 3d 20 7b 30 7d  Reader pr1 = {0}
3560b 3b 0a 20 20 20 20 20 20 20 20 20 20 50 4c 52 65  ;.          PLRe
3560c 61 64 65 72 20 70 72 32 20 3d 20 7b 30 7d 3b 0a  ader pr2 = {0};.
3560d 0a 20 20 20 20 20 20 20 20 20 20 50 4c 57 72 69  .          PLWri
3560e 74 65 72 20 70 6c 77 72 69 74 65 72 3b 0a 20 20  ter plwriter;.  
3560f 20 20 20 20 20 20 20 20 70 6c 77 49 6e 69 74 28          plwInit(
35610 26 70 6c 77 72 69 74 65 72 2c 20 26 77 72 69 74  &plwriter, &writ
35611 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 64 6c 72  er, dlrDocid(dlr
35612 41 74 45 6e 64 28 26 64 72 31 29 3f 26 64 72 32  AtEnd(&dr1)?&dr2
35613 3a 26 64 72 31 29 29 3b 0a 0a 20 20 20 20 20 20  :&dr1));..      
35614 20 20 20 20 69 66 28 20 6f 6e 65 2e 6e 44 61 74      if( one.nDat
35615 61 20 29 20 70 6c 72 49 6e 69 74 28 26 70 72 31  a ) plrInit(&pr1
35616 2c 20 26 64 72 31 29 3b 0a 20 20 20 20 20 20 20  , &dr1);.       
35617 20 20 20 69 66 28 20 74 77 6f 2e 6e 44 61 74 61     if( two.nData
35618 20 29 20 70 6c 72 49 6e 69 74 28 26 70 72 32 2c   ) plrInit(&pr2,
35619 20 26 64 72 32 29 3b 0a 20 20 20 20 20 20 20 20   &dr2);.        
3561a 20 20 77 68 69 6c 65 28 20 21 70 6c 72 41 74 45    while( !plrAtE
3561b 6e 64 28 26 70 72 31 29 20 7c 7c 20 21 70 6c 72  nd(&pr1) || !plr
3561c 41 74 45 6e 64 28 26 70 72 32 29 20 29 7b 0a 20  AtEnd(&pr2) ){. 
3561d 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69             int i
3561e 43 6f 6d 70 61 72 65 20 3d 20 70 6c 72 43 6f 6d  Compare = plrCom
3561f 70 61 72 65 28 26 70 72 31 2c 20 26 70 72 32 29  pare(&pr1, &pr2)
35620 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 77  ;.            sw
35621 69 74 63 68 28 20 69 43 6f 6d 70 61 72 65 20 29  itch( iCompare )
35622 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
35623 63 61 73 65 20 2d 31 3a 0a 20 20 20 20 20 20 20  case -1:.       
35624 20 20 20 20 20 20 20 20 20 70 6c 77 43 6f 70 79           plwCopy
35625 28 26 70 6c 77 72 69 74 65 72 2c 20 26 70 72 31  (&plwriter, &pr1
35626 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
35627 20 20 20 70 6c 72 53 74 65 70 28 26 70 72 31 29     plrStep(&pr1)
35628 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
35629 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
3562a 20 20 20 20 20 20 20 63 61 73 65 20 31 3a 0a 20         case 1:. 
3562b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
3562c 6c 77 43 6f 70 79 28 26 70 6c 77 72 69 74 65 72  lwCopy(&plwriter
3562d 2c 20 26 70 72 32 29 3b 0a 20 20 20 20 20 20 20  , &pr2);.       
3562e 20 20 20 20 20 20 20 20 20 70 6c 72 53 74 65 70           plrStep
3562f 28 26 70 72 32 29 3b 0a 20 20 20 20 20 20 20 20  (&pr2);.        
35630 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
35631 20 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73               cas
35632 65 20 30 3a 0a 20 20 20 20 20 20 20 20 20 20 20  e 0:.           
35633 20 20 20 20 20 70 6c 77 43 6f 70 79 28 26 70 6c       plwCopy(&pl
35634 77 72 69 74 65 72 2c 20 26 70 72 31 29 3b 0a 20  writer, &pr1);. 
35635 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
35636 6c 72 53 74 65 70 28 26 70 72 31 29 3b 0a 20 20  lrStep(&pr1);.  
35637 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6c                pl
35638 72 53 74 65 70 28 26 70 72 32 29 3b 0a 20 20 20  rStep(&pr2);.   
35639 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
3563a 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
3563b 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  }.          }.  
3563c 20 20 20 20 20 20 20 20 70 6c 77 54 65 72 6d 69          plwTermi
3563d 6e 61 74 65 28 26 70 6c 77 72 69 74 65 72 29 3b  nate(&plwriter);
3563e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
3563f 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73     dataBufferDes
35640 74 72 6f 79 28 26 6f 6e 65 29 3b 0a 20 20 20 20  troy(&one);.    
35641 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65      dataBufferDe
35642 73 74 72 6f 79 28 26 74 77 6f 29 3b 0a 20 20 20  stroy(&two);.   
35643 20 20 20 7d 0a 20 20 20 20 20 20 64 6c 72 53 74     }.      dlrSt
35644 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 20  ep(&left);.     
35645 20 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   dlrStep(&right)
35646 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64  ;.    }.  }..  d
35647 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29  lrDestroy(&left)
35648 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26  ;.  dlrDestroy(&
35649 72 69 67 68 74 29 3b 0a 20 20 64 6c 77 44 65 73  right);.  dlwDes
3564a 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d  troy(&writer);.}
3564b 0a 0a 2f 2a 20 57 65 20 68 61 76 65 20 74 77 6f  ../* We have two
3564c 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69   DL_DOCIDS docli
3564d 73 74 73 3a 20 20 70 4c 65 66 74 20 61 6e 64 20  sts:  pLeft and 
3564e 70 52 69 67 68 74 2e 0a 2a 2a 20 57 72 69 74 65  pRight..** Write
3564f 20 74 68 65 20 69 6e 74 65 72 73 65 63 74 69 6f   the intersectio
35650 6e 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20 64  n of these two d
35651 6f 63 6c 69 73 74 73 20 69 6e 74 6f 20 70 4f 75  oclists into pOu
35652 74 20 61 73 20 61 0a 2a 2a 20 44 4c 5f 44 4f 43  t as a.** DL_DOC
35653 49 44 53 20 64 6f 63 6c 69 73 74 2e 0a 2a 2f 0a  IDS doclist..*/.
35654 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c  static void docL
35655 69 73 74 41 6e 64 4d 65 72 67 65 28 0a 20 20 63  istAndMerge(.  c
35656 6f 6e 73 74 20 63 68 61 72 20 2a 70 4c 65 66 74  onst char *pLeft
35657 2c 20 69 6e 74 20 6e 4c 65 66 74 2c 0a 20 20 63  , int nLeft,.  c
35658 6f 6e 73 74 20 63 68 61 72 20 2a 70 52 69 67 68  onst char *pRigh
35659 74 2c 20 69 6e 74 20 6e 52 69 67 68 74 2c 0a 20  t, int nRight,. 
3565a 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 4f 75   DataBuffer *pOu
3565b 74 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20  t      /* Write 
3565c 74 68 65 20 63 6f 6d 62 69 6e 65 64 20 64 6f 63  the combined doc
3565d 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  list here */.){.
3565e 20 20 44 4c 52 65 61 64 65 72 20 6c 65 66 74 2c    DLReader left,
3565f 20 72 69 67 68 74 3b 0a 20 20 44 4c 57 72 69 74   right;.  DLWrit
35660 65 72 20 77 72 69 74 65 72 3b 0a 0a 20 20 69 66  er writer;..  if
35661 28 20 6e 4c 65 66 74 3d 3d 30 20 7c 7c 20 6e 52  ( nLeft==0 || nR
35662 69 67 68 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  ight==0 ) return
35663 3b 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26 6c 65  ;..  dlrInit(&le
35664 66 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70  ft, DL_DOCIDS, p
35665 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20  Left, nLeft);.  
35666 64 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c 20  dlrInit(&right, 
35667 44 4c 5f 44 4f 43 49 44 53 2c 20 70 52 69 67 68  DL_DOCIDS, pRigh
35668 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20 64 6c  t, nRight);.  dl
35669 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 44  wInit(&writer, D
3566a 4c 5f 44 4f 43 49 44 53 2c 20 70 4f 75 74 29 3b  L_DOCIDS, pOut);
3566b 0a 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72 41  ..  while( !dlrA
3566c 74 45 6e 64 28 26 6c 65 66 74 29 20 26 26 20 21  tEnd(&left) && !
3566d 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29  dlrAtEnd(&right)
3566e 20 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72 44   ){.    if( dlrD
3566f 6f 63 69 64 28 26 6c 65 66 74 29 3c 64 6c 72 44  ocid(&left)<dlrD
35670 6f 63 69 64 28 26 72 69 67 68 74 29 20 29 7b 0a  ocid(&right) ){.
35671 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c        dlrStep(&l
35672 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  eft);.    }else 
35673 69 66 28 20 64 6c 72 44 6f 63 69 64 28 26 72 69  if( dlrDocid(&ri
35674 67 68 74 29 3c 64 6c 72 44 6f 63 69 64 28 26 6c  ght)<dlrDocid(&l
35675 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c  eft) ){.      dl
35676 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20  rStep(&right);. 
35677 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
35678 64 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c 20  dlwAdd(&writer, 
35679 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 29  dlrDocid(&left))
3567a 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28  ;.      dlrStep(
3567b 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 64 6c  &left);.      dl
3567c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20  rStep(&right);. 
3567d 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 6c 72 44     }.  }..  dlrD
3567e 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20  estroy(&left);. 
3567f 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72 69 67   dlrDestroy(&rig
35680 68 74 29 3b 0a 20 20 64 6c 77 44 65 73 74 72 6f  ht);.  dlwDestro
35681 79 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a 0a 2f  y(&writer);.}../
35682 2a 20 57 65 20 68 61 76 65 20 74 77 6f 20 44 4c  * We have two DL
35683 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 73  _DOCIDS doclists
35684 3a 20 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69  :  pLeft and pRi
35685 67 68 74 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68  ght..** Write th
35686 65 20 75 6e 69 6f 6e 20 6f 66 20 74 68 65 73 65  e union of these
35687 20 74 77 6f 20 64 6f 63 6c 69 73 74 73 20 69 6e   two doclists in
35688 74 6f 20 70 4f 75 74 20 61 73 20 61 0a 2a 2a 20  to pOut as a.** 
35689 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69 73  DL_DOCIDS doclis
3568a 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
3568b 64 20 64 6f 63 4c 69 73 74 4f 72 4d 65 72 67 65  d docListOrMerge
3568c 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
3568d 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74  pLeft, int nLeft
3568e 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
3568f 70 52 69 67 68 74 2c 20 69 6e 74 20 6e 52 69 67  pRight, int nRig
35690 68 74 2c 0a 20 20 44 61 74 61 42 75 66 66 65 72  ht,.  DataBuffer
35691 20 2a 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57   *pOut      /* W
35692 72 69 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65  rite the combine
35693 64 20 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a  d doclist here *
35694 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20  /.){.  DLReader 
35695 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44  left, right;.  D
35696 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a  LWriter writer;.
35697 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20  .  if( nLeft==0 
35698 29 7b 0a 20 20 20 20 69 66 28 20 6e 52 69 67 68  ){.    if( nRigh
35699 74 21 3d 30 20 29 20 64 61 74 61 42 75 66 66 65  t!=0 ) dataBuffe
3569a 72 41 70 70 65 6e 64 28 70 4f 75 74 2c 20 70 52  rAppend(pOut, pR
3569b 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20  ight, nRight);. 
3569c 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
3569d 20 69 66 28 20 6e 52 69 67 68 74 3d 3d 30 20 29   if( nRight==0 )
3569e 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  {.    dataBuffer
3569f 41 70 70 65 6e 64 28 70 4f 75 74 2c 20 70 4c 65  Append(pOut, pLe
356a0 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 20 20  ft, nLeft);.    
356a1 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 64  return;.  }..  d
356a2 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c 20 44 4c  lrInit(&left, DL
356a3 5f 44 4f 43 49 44 53 2c 20 70 4c 65 66 74 2c 20  _DOCIDS, pLeft, 
356a4 6e 4c 65 66 74 29 3b 0a 20 20 64 6c 72 49 6e 69  nLeft);.  dlrIni
356a5 74 28 26 72 69 67 68 74 2c 20 44 4c 5f 44 4f 43  t(&right, DL_DOC
356a6 49 44 53 2c 20 70 52 69 67 68 74 2c 20 6e 52 69  IDS, pRight, nRi
356a7 67 68 74 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28  ght);.  dlwInit(
356a8 26 77 72 69 74 65 72 2c 20 44 4c 5f 44 4f 43 49  &writer, DL_DOCI
356a9 44 53 2c 20 70 4f 75 74 29 3b 0a 0a 20 20 77 68  DS, pOut);..  wh
356aa 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28 26  ile( !dlrAtEnd(&
356ab 6c 65 66 74 29 20 7c 7c 20 21 64 6c 72 41 74 45  left) || !dlrAtE
356ac 6e 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20  nd(&right) ){.  
356ad 20 20 69 66 28 20 64 6c 72 41 74 45 6e 64 28 26    if( dlrAtEnd(&
356ae 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20  right) ){.      
356af 64 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c 20  dlwAdd(&writer, 
356b0 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 29  dlrDocid(&left))
356b1 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28  ;.      dlrStep(
356b2 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73  &left);.    }els
356b3 65 20 69 66 28 20 64 6c 72 41 74 45 6e 64 28 26  e if( dlrAtEnd(&
356b4 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64  left) ){.      d
356b5 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c 20 64  lwAdd(&writer, d
356b6 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74 29 29  lrDocid(&right))
356b7 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28  ;.      dlrStep(
356b8 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c  &right);.    }el
356b9 73 65 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28  se if( dlrDocid(
356ba 26 6c 65 66 74 29 3c 64 6c 72 44 6f 63 69 64 28  &left)<dlrDocid(
356bb 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20  &right) ){.     
356bc 20 64 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c   dlwAdd(&writer,
356bd 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29   dlrDocid(&left)
356be 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  );.      dlrStep
356bf 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c  (&left);.    }el
356c0 73 65 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28  se if( dlrDocid(
356c1 26 72 69 67 68 74 29 3c 64 6c 72 44 6f 63 69 64  &right)<dlrDocid
356c2 28 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20  (&left) ){.     
356c3 20 64 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c   dlwAdd(&writer,
356c4 20 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74   dlrDocid(&right
356c5 29 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65  ));.      dlrSte
356c6 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d  p(&right);.    }
356c7 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 6c 77 41  else{.      dlwA
356c8 64 64 28 26 77 72 69 74 65 72 2c 20 64 6c 72 44  dd(&writer, dlrD
356c9 6f 63 69 64 28 26 6c 65 66 74 29 29 3b 0a 20 20  ocid(&left));.  
356ca 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66      dlrStep(&lef
356cb 74 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65  t);.      dlrSte
356cc 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d  p(&right);.    }
356cd 0a 20 20 7d 0a 0a 20 20 64 6c 72 44 65 73 74 72  .  }..  dlrDestr
356ce 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 64 6c 72  oy(&left);.  dlr
356cf 44 65 73 74 72 6f 79 28 26 72 69 67 68 74 29 3b  Destroy(&right);
356d0 0a 20 20 64 6c 77 44 65 73 74 72 6f 79 28 26 77  .  dlwDestroy(&w
356d1 72 69 74 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 57 65  riter);.}../* We
356d2 20 68 61 76 65 20 74 77 6f 20 44 4c 5f 44 4f 43   have two DL_DOC
356d3 49 44 53 20 64 6f 63 6c 69 73 74 73 3a 20 20 70  IDS doclists:  p
356d4 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74 2e  Left and pRight.
356d5 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 70  .** Write into p
356d6 4f 75 74 20 61 73 20 44 4c 5f 44 4f 43 49 44 53  Out as DL_DOCIDS
356d7 20 64 6f 63 6c 69 73 74 20 63 6f 6e 74 61 69 6e   doclist contain
356d8 69 6e 67 20 61 6c 6c 20 64 6f 63 75 6d 65 6e 74  ing all document
356d9 73 20 74 68 61 74 0a 2a 2a 20 6f 63 63 75 72 20  s that.** occur 
356da 69 6e 20 70 4c 65 66 74 20 62 75 74 20 6e 6f 74  in pLeft but not
356db 20 69 6e 20 70 52 69 67 68 74 2e 0a 2a 2f 0a 73   in pRight..*/.s
356dc 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c 69  tatic void docLi
356dd 73 74 45 78 63 65 70 74 4d 65 72 67 65 28 0a 20  stExceptMerge(. 
356de 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4c 65   const char *pLe
356df 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74 2c 0a 20  ft, int nLeft,. 
356e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52 69   const char *pRi
356e1 67 68 74 2c 20 69 6e 74 20 6e 52 69 67 68 74 2c  ght, int nRight,
356e2 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 70  .  DataBuffer *p
356e3 4f 75 74 20 20 20 20 20 20 2f 2a 20 57 72 69 74  Out      /* Writ
356e4 65 20 74 68 65 20 63 6f 6d 62 69 6e 65 64 20 64  e the combined d
356e5 6f 63 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 29  oclist here */.)
356e6 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 6c 65 66  {.  DLReader lef
356e7 74 2c 20 72 69 67 68 74 3b 0a 20 20 44 4c 57 72  t, right;.  DLWr
356e8 69 74 65 72 20 77 72 69 74 65 72 3b 0a 0a 20 20  iter writer;..  
356e9 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20 29 20 72  if( nLeft==0 ) r
356ea 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 6e 52 69  eturn;.  if( nRi
356eb 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61  ght==0 ){.    da
356ec 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 70  taBufferAppend(p
356ed 4f 75 74 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66  Out, pLeft, nLef
356ee 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  t);.    return;.
356ef 20 20 7d 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26    }..  dlrInit(&
356f0 6c 65 66 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c  left, DL_DOCIDS,
356f1 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a   pLeft, nLeft);.
356f2 20 20 64 6c 72 49 6e 69 74 28 26 72 69 67 68 74    dlrInit(&right
356f3 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 52 69  , DL_DOCIDS, pRi
356f4 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20  ght, nRight);.  
356f5 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c  dlwInit(&writer,
356f6 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 4f 75 74   DL_DOCIDS, pOut
356f7 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21 64 6c  );..  while( !dl
356f8 72 41 74 45 6e 64 28 26 6c 65 66 74 29 20 29 7b  rAtEnd(&left) ){
356f9 0a 20 20 20 20 77 68 69 6c 65 28 20 21 64 6c 72  .    while( !dlr
356fa 41 74 45 6e 64 28 26 72 69 67 68 74 29 20 26 26  AtEnd(&right) &&
356fb 20 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74   dlrDocid(&right
356fc 29 3c 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74  )<dlrDocid(&left
356fd 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 72 53 74  ) ){.      dlrSt
356fe 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20  ep(&right);.    
356ff 7d 0a 20 20 20 20 69 66 28 20 64 6c 72 41 74 45  }.    if( dlrAtE
35700 6e 64 28 26 72 69 67 68 74 29 20 7c 7c 20 64 6c  nd(&right) || dl
35701 72 44 6f 63 69 64 28 26 6c 65 66 74 29 3c 64 6c  rDocid(&left)<dl
35702 72 44 6f 63 69 64 28 26 72 69 67 68 74 29 20 29  rDocid(&right) )
35703 7b 0a 20 20 20 20 20 20 64 6c 77 41 64 64 28 26  {.      dlwAdd(&
35704 77 72 69 74 65 72 2c 20 64 6c 72 44 6f 63 69 64  writer, dlrDocid
35705 28 26 6c 65 66 74 29 29 3b 0a 20 20 20 20 7d 0a  (&left));.    }.
35706 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66      dlrStep(&lef
35707 74 29 3b 0a 20 20 7d 0a 0a 20 20 64 6c 72 44 65  t);.  }..  dlrDe
35708 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20  stroy(&left);.  
35709 64 6c 72 44 65 73 74 72 6f 79 28 26 72 69 67 68  dlrDestroy(&righ
3570a 74 29 3b 0a 20 20 64 6c 77 44 65 73 74 72 6f 79  t);.  dlwDestroy
3570b 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a 0a 73 74  (&writer);.}..st
3570c 61 74 69 63 20 63 68 61 72 20 2a 73 74 72 69 6e  atic char *strin
3570d 67 5f 64 75 70 5f 6e 28 63 6f 6e 73 74 20 63 68  g_dup_n(const ch
3570e 61 72 20 2a 73 2c 20 69 6e 74 20 6e 29 7b 0a 20  ar *s, int n){. 
3570f 20 63 68 61 72 20 2a 73 74 72 20 3d 20 73 71 6c   char *str = sql
35710 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 20 2b 20  ite3_malloc(n + 
35711 31 29 3b 0a 20 20 6d 65 6d 63 70 79 28 73 74 72  1);.  memcpy(str
35712 2c 20 73 2c 20 6e 29 3b 0a 20 20 73 74 72 5b 6e  , s, n);.  str[n
35713 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 72 65 74 75  ] = '\0';.  retu
35714 72 6e 20 73 74 72 3b 0a 7d 0a 0a 2f 2a 20 44 75  rn str;.}../* Du
35715 70 6c 69 63 61 74 65 20 61 20 73 74 72 69 6e 67  plicate a string
35716 3b 20 74 68 65 20 63 61 6c 6c 65 72 20 6d 75 73  ; the caller mus
35717 74 20 66 72 65 65 28 29 20 74 68 65 20 72 65 74  t free() the ret
35718 75 72 6e 65 64 20 73 74 72 69 6e 67 2e 0a 20 2a  urned string.. *
35719 20 28 57 65 20 64 6f 6e 27 74 20 75 73 65 20 73   (We don't use s
3571a 74 72 64 75 70 28 29 20 73 69 6e 63 65 20 69 74  trdup() since it
3571b 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20   is not part of 
3571c 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c  the standard C l
3571d 69 62 72 61 72 79 20 61 6e 64 0a 20 2a 20 6d 61  ibrary and. * ma
3571e 79 20 6e 6f 74 20 62 65 20 61 76 61 69 6c 61 62  y not be availab
3571f 6c 65 20 65 76 65 72 79 77 68 65 72 65 2e 29 20  le everywhere.) 
35720 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  */.static char *
35721 73 74 72 69 6e 67 5f 64 75 70 28 63 6f 6e 73 74  string_dup(const
35722 20 63 68 61 72 20 2a 73 29 7b 0a 20 20 72 65 74   char *s){.  ret
35723 75 72 6e 20 73 74 72 69 6e 67 5f 64 75 70 5f 6e  urn string_dup_n
35724 28 73 2c 20 73 74 72 6c 65 6e 28 73 29 29 3b 0a  (s, strlen(s));.
35725 7d 0a 0a 2f 2a 20 46 6f 72 6d 61 74 20 61 20 73  }../* Format a s
35726 74 72 69 6e 67 2c 20 72 65 70 6c 61 63 69 6e 67  tring, replacing
35727 20 65 61 63 68 20 6f 63 63 75 72 72 65 6e 63 65   each occurrence
35728 20 6f 66 20 74 68 65 20 25 20 63 68 61 72 61 63   of the % charac
35729 74 65 72 20 77 69 74 68 0a 20 2a 20 7a 44 62 2e  ter with. * zDb.
3572a 7a 4e 61 6d 65 2e 20 20 54 68 69 73 20 6d 61 79  zName.  This may
3572b 20 62 65 20 6d 6f 72 65 20 63 6f 6e 76 65 6e 69   be more conveni
3572c 65 6e 74 20 74 68 61 6e 20 73 71 6c 69 74 65 5f  ent than sqlite_
3572d 6d 70 72 69 6e 74 66 28 29 0a 20 2a 20 77 68 65  mprintf(). * whe
3572e 6e 20 6f 6e 65 20 73 74 72 69 6e 67 20 69 73 20  n one string is 
3572f 75 73 65 64 20 72 65 70 65 61 74 65 64 6c 79 20  used repeatedly 
35730 69 6e 20 61 20 66 6f 72 6d 61 74 20 73 74 72 69  in a format stri
35731 6e 67 2e 0a 20 2a 20 54 68 65 20 63 61 6c 6c 65  ng.. * The calle
35732 72 20 6d 75 73 74 20 66 72 65 65 28 29 20 74 68  r must free() th
35733 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  e returned strin
35734 67 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63 68 61  g. */.static cha
35735 72 20 2a 73 74 72 69 6e 67 5f 66 6f 72 6d 61 74  r *string_format
35736 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f  (const char *zFo
35737 72 6d 61 74 2c 0a 20 20 20 20 20 20 20 20 20 20  rmat,.          
35738 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35739 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
3573a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
3573b 61 6d 65 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68  ame){.  const ch
3573c 61 72 20 2a 70 3b 0a 20 20 73 69 7a 65 5f 74 20  ar *p;.  size_t 
3573d 6c 65 6e 20 3d 20 30 3b 0a 20 20 73 69 7a 65 5f  len = 0;.  size_
3573e 74 20 6e 44 62 20 3d 20 73 74 72 6c 65 6e 28 7a  t nDb = strlen(z
3573f 44 62 29 3b 0a 20 20 73 69 7a 65 5f 74 20 6e 4e  Db);.  size_t nN
35740 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a 4e 61  ame = strlen(zNa
35741 6d 65 29 3b 0a 20 20 73 69 7a 65 5f 74 20 6e 46  me);.  size_t nF
35742 75 6c 6c 54 61 62 6c 65 4e 61 6d 65 20 3d 20 6e  ullTableName = n
35743 44 62 2b 31 2b 6e 4e 61 6d 65 3b 0a 20 20 63 68  Db+1+nName;.  ch
35744 61 72 20 2a 72 65 73 75 6c 74 3b 0a 20 20 63 68  ar *result;.  ch
35745 61 72 20 2a 72 3b 0a 0a 20 20 2f 2a 20 66 69 72  ar *r;..  /* fir
35746 73 74 20 63 6f 6d 70 75 74 65 20 6c 65 6e 67 74  st compute lengt
35747 68 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 66 6f  h needed */.  fo
35748 72 28 70 20 3d 20 7a 46 6f 72 6d 61 74 20 3b 20  r(p = zFormat ; 
35749 2a 70 20 3b 20 2b 2b 70 29 7b 0a 20 20 20 20 6c  *p ; ++p){.    l
3574a 65 6e 20 2b 3d 20 28 2a 70 3d 3d 27 25 27 20 3f  en += (*p=='%' ?
3574b 20 6e 46 75 6c 6c 54 61 62 6c 65 4e 61 6d 65 20   nFullTableName 
3574c 3a 20 31 29 3b 0a 20 20 7d 0a 20 20 6c 65 6e 20  : 1);.  }.  len 
3574d 2b 3d 20 31 3b 20 20 2f 2a 20 66 6f 72 20 6e 75  += 1;  /* for nu
3574e 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72 20 2a 2f  ll terminator */
3574f 0a 0a 20 20 72 20 3d 20 72 65 73 75 6c 74 20 3d  ..  r = result =
35750 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
35751 6c 65 6e 29 3b 0a 20 20 66 6f 72 28 70 20 3d 20  len);.  for(p = 
35752 7a 46 6f 72 6d 61 74 3b 20 2a 70 3b 20 2b 2b 70  zFormat; *p; ++p
35753 29 7b 0a 20 20 20 20 69 66 28 20 2a 70 3d 3d 27  ){.    if( *p=='
35754 25 27 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63  %' ){.      memc
35755 70 79 28 72 2c 20 7a 44 62 2c 20 6e 44 62 29 3b  py(r, zDb, nDb);
35756 0a 20 20 20 20 20 20 72 20 2b 3d 20 6e 44 62 3b  .      r += nDb;
35757 0a 20 20 20 20 20 20 2a 72 2b 2b 20 3d 20 27 2e  .      *r++ = '.
35758 27 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ';.      memcpy(
35759 72 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  r, zName, nName)
3575a 3b 0a 20 20 20 20 20 20 72 20 2b 3d 20 6e 4e 61  ;.      r += nNa
3575b 6d 65 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b  me;.    } else {
3575c 0a 20 20 20 20 20 20 2a 72 2b 2b 20 3d 20 2a 70  .      *r++ = *p
3575d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 72  ;.    }.  }.  *r
3575e 2b 2b 20 3d 20 27 5c 30 27 3b 0a 20 20 61 73 73  ++ = '\0';.  ass
3575f 65 72 74 28 20 72 20 3d 3d 20 72 65 73 75 6c 74  ert( r == result
35760 20 2b 20 6c 65 6e 20 29 3b 0a 20 20 72 65 74 75   + len );.  retu
35761 72 6e 20 72 65 73 75 6c 74 3b 0a 7d 0a 0a 73 74  rn result;.}..st
35762 61 74 69 63 20 69 6e 74 20 73 71 6c 5f 65 78 65  atic int sql_exe
35763 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  c(sqlite3 *db, c
35764 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 20  onst char *zDb, 
35765 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
35766 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
35767 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
35768 72 20 2a 7a 46 6f 72 6d 61 74 29 7b 0a 20 20 63  r *zFormat){.  c
35769 68 61 72 20 2a 7a 43 6f 6d 6d 61 6e 64 20 3d 20  har *zCommand = 
3576a 73 74 72 69 6e 67 5f 66 6f 72 6d 61 74 28 7a 46  string_format(zF
3576b 6f 72 6d 61 74 2c 20 7a 44 62 2c 20 7a 4e 61 6d  ormat, zDb, zNam
3576c 65 29 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  e);.  int rc;.  
3576d 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20  FTSTRACE(("FTS3 
3576e 73 71 6c 3a 20 25 73 5c 6e 22 2c 20 7a 43 6f 6d  sql: %s\n", zCom
3576f 6d 61 6e 64 29 29 3b 0a 20 20 72 63 20 3d 20 73  mand));.  rc = s
35770 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20  qlite3_exec(db, 
35771 7a 43 6f 6d 6d 61 6e 64 2c 20 4e 55 4c 4c 2c 20  zCommand, NULL, 
35772 30 2c 20 4e 55 4c 4c 29 3b 0a 20 20 73 71 6c 69  0, NULL);.  sqli
35773 74 65 33 5f 66 72 65 65 28 7a 43 6f 6d 6d 61 6e  te3_free(zComman
35774 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  d);.  return rc;
35775 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .}..static int s
35776 71 6c 5f 70 72 65 70 61 72 65 28 73 71 6c 69 74  ql_prepare(sqlit
35777 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68  e3 *db, const ch
35778 61 72 20 2a 7a 44 62 2c 20 63 6f 6e 73 74 20 63  ar *zDb, const c
35779 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20  har *zName,.    
3577a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3577b 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20     sqlite3_stmt 
3577c 2a 2a 70 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20  **ppStmt, const 
3577d 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 29 7b 0a  char *zFormat){.
3577e 20 20 63 68 61 72 20 2a 7a 43 6f 6d 6d 61 6e 64    char *zCommand
3577f 20 3d 20 73 74 72 69 6e 67 5f 66 6f 72 6d 61 74   = string_format
35780 28 7a 46 6f 72 6d 61 74 2c 20 7a 44 62 2c 20 7a  (zFormat, zDb, z
35781 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20 72 63 3b  Name);.  int rc;
35782 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54  .  FTSTRACE(("FT
35783 53 33 20 70 72 65 70 61 72 65 3a 20 25 73 5c 6e  S3 prepare: %s\n
35784 22 2c 20 7a 43 6f 6d 6d 61 6e 64 29 29 3b 0a 20  ", zCommand));. 
35785 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72   rc = sqlite3_pr
35786 65 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 43 6f  epare_v2(db, zCo
35787 6d 6d 61 6e 64 2c 20 2d 31 2c 20 70 70 53 74 6d  mmand, -1, ppStm
35788 74 2c 20 4e 55 4c 4c 29 3b 0a 20 20 73 71 6c 69  t, NULL);.  sqli
35789 74 65 33 5f 66 72 65 65 28 7a 43 6f 6d 6d 61 6e  te3_free(zComman
3578a 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  d);.  return rc;
3578b 0a 7d 0a 0a 2f 2a 20 65 6e 64 20 75 74 69 6c 69  .}../* end utili
3578c 74 79 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a  ty functions */.
3578d 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65  ./* Forward refe
3578e 72 65 6e 63 65 20 2a 2f 0a 74 79 70 65 64 65 66  rence */.typedef
3578f 20 73 74 72 75 63 74 20 66 75 6c 6c 74 65 78 74   struct fulltext
35790 5f 76 74 61 62 20 66 75 6c 6c 74 65 78 74 5f 76  _vtab fulltext_v
35791 74 61 62 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  tab;../*.** An i
35792 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
35793 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
35794 72 65 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f  re keeps track o
35795 66 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 6d  f generated.** m
35796 61 74 63 68 69 6e 67 2d 77 6f 72 64 20 6f 66 66  atching-word off
35797 73 65 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  set information 
35798 61 6e 64 20 73 6e 69 70 70 65 74 73 2e 0a 2a 2f  and snippets..*/
35799 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
3579a 53 6e 69 70 70 65 74 20 7b 0a 20 20 69 6e 74 20  Snippet {.  int 
3579b 6e 4d 61 74 63 68 3b 20 20 20 20 20 2f 2a 20 54  nMatch;     /* T
3579c 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6d  otal number of m
3579d 61 74 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20  atches */.  int 
3579e 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a 20 53  nAlloc;     /* S
3579f 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 66  pace allocated f
357a0 6f 72 20 61 4d 61 74 63 68 5b 5d 20 2a 2f 0a 20  or aMatch[] */. 
357a1 20 73 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d   struct snippetM
357a2 61 74 63 68 20 7b 20 2f 2a 20 4f 6e 65 20 65 6e  atch { /* One en
357a3 74 72 79 20 66 6f 72 20 65 61 63 68 20 6d 61 74  try for each mat
357a4 63 68 69 6e 67 20 74 65 72 6d 20 2a 2f 0a 20 20  ching term */.  
357a5 20 20 63 68 61 72 20 73 6e 53 74 61 74 75 73 3b    char snStatus;
357a6 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73         /* Status
357a7 20 66 6c 61 67 20 66 6f 72 20 75 73 65 20 77 68   flag for use wh
357a8 69 6c 65 20 63 6f 6e 73 74 72 75 63 74 69 6e 67  ile constructing
357a9 20 73 6e 69 70 70 65 74 73 20 2a 2f 0a 20 20 20   snippets */.   
357aa 20 73 68 6f 72 74 20 69 6e 74 20 69 43 6f 6c 3b   short int iCol;
357ab 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c        /* The col
357ac 75 6d 6e 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  umn that contain
357ad 73 20 74 68 65 20 6d 61 74 63 68 20 2a 2f 0a 20  s the match */. 
357ae 20 20 20 73 68 6f 72 74 20 69 6e 74 20 69 54 65     short int iTe
357af 72 6d 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 69  rm;     /* The i
357b0 6e 64 65 78 20 69 6e 20 51 75 65 72 79 2e 70 54  ndex in Query.pT
357b1 65 72 6d 73 5b 5d 20 6f 66 20 74 68 65 20 6d 61  erms[] of the ma
357b2 74 63 68 69 6e 67 20 74 65 72 6d 20 2a 2f 0a 20  tching term */. 
357b3 20 20 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 20 20     int iToken;  
357b4 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69          /* The i
357b5 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61 74 63  ndex of the matc
357b6 68 69 6e 67 20 64 6f 63 75 6d 65 6e 74 20 74 6f  hing document to
357b7 6b 65 6e 20 2a 2f 0a 20 20 20 20 73 68 6f 72 74  ken */.    short
357b8 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20   int nByte;     
357b9 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
357ba 65 73 20 69 6e 20 74 68 65 20 74 65 72 6d 20 2a  es in the term *
357bb 2f 0a 20 20 20 20 69 6e 74 20 69 53 74 61 72 74  /.    int iStart
357bc 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ;          /* Th
357bd 65 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20  e offset to the 
357be 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72 20  first character 
357bf 6f 66 20 74 68 65 20 74 65 72 6d 20 2a 2f 0a 20  of the term */. 
357c0 20 7d 20 2a 61 4d 61 74 63 68 3b 20 20 20 20 20   } *aMatch;     
357c1 20 2f 2a 20 50 6f 69 6e 74 73 20 74 6f 20 73 70   /* Points to sp
357c2 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ace obtained fro
357c3 6d 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68  m malloc */.  ch
357c4 61 72 20 2a 7a 4f 66 66 73 65 74 3b 20 20 2f 2a  ar *zOffset;  /*
357c5 20 54 65 78 74 20 72 65 6e 64 65 72 69 6e 67 20   Text rendering 
357c6 6f 66 20 61 4d 61 74 63 68 5b 5d 20 2a 2f 0a 20  of aMatch[] */. 
357c7 20 69 6e 74 20 6e 4f 66 66 73 65 74 3b 20 20 20   int nOffset;   
357c8 20 2f 2a 20 73 74 72 6c 65 6e 28 7a 4f 66 66 73   /* strlen(zOffs
357c9 65 74 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  et) */.  char *z
357ca 53 6e 69 70 70 65 74 3b 20 2f 2a 20 53 6e 69 70  Snippet; /* Snip
357cb 70 65 74 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e  pet text */.  in
357cc 74 20 6e 53 6e 69 70 70 65 74 3b 20 20 20 2f 2a  t nSnippet;   /*
357cd 20 73 74 72 6c 65 6e 28 7a 53 6e 69 70 70 65 74   strlen(zSnippet
357ce 29 20 2a 2f 0a 7d 20 53 6e 69 70 70 65 74 3b 0a  ) */.} Snippet;.
357cf 0a 0a 74 79 70 65 64 65 66 20 65 6e 75 6d 20 51  ..typedef enum Q
357d0 75 65 72 79 54 79 70 65 20 7b 0a 20 20 51 55 45  ueryType {.  QUE
357d1 52 59 5f 47 45 4e 45 52 49 43 2c 20 20 20 2f 2a  RY_GENERIC,   /*
357d2 20 74 61 62 6c 65 20 73 63 61 6e 20 2a 2f 0a 20   table scan */. 
357d3 20 51 55 45 52 59 5f 44 4f 43 49 44 2c 20 20 20   QUERY_DOCID,   
357d4 20 20 2f 2a 20 6c 6f 6f 6b 75 70 20 62 79 20 64    /* lookup by d
357d5 6f 63 69 64 20 2a 2f 0a 20 20 51 55 45 52 59 5f  ocid */.  QUERY_
357d6 46 55 4c 4c 54 45 58 54 20 20 20 2f 2a 20 51 55  FULLTEXT   /* QU
357d7 45 52 59 5f 46 55 4c 4c 54 45 58 54 20 2b 20 5b  ERY_FULLTEXT + [
357d8 69 5d 20 69 73 20 61 20 66 75 6c 6c 2d 74 65 78  i] is a full-tex
357d9 74 20 73 65 61 72 63 68 20 66 6f 72 20 63 6f 6c  t search for col
357da 75 6d 6e 20 69 2a 2f 0a 7d 20 51 75 65 72 79 54  umn i*/.} QueryT
357db 79 70 65 3b 0a 0a 74 79 70 65 64 65 66 20 65 6e  ype;..typedef en
357dc 75 6d 20 66 75 6c 6c 74 65 78 74 5f 73 74 61 74  um fulltext_stat
357dd 65 6d 65 6e 74 20 7b 0a 20 20 43 4f 4e 54 45 4e  ement {.  CONTEN
357de 54 5f 49 4e 53 45 52 54 5f 53 54 4d 54 2c 0a 20  T_INSERT_STMT,. 
357df 20 43 4f 4e 54 45 4e 54 5f 53 45 4c 45 43 54 5f   CONTENT_SELECT_
357e0 53 54 4d 54 2c 0a 20 20 43 4f 4e 54 45 4e 54 5f  STMT,.  CONTENT_
357e1 55 50 44 41 54 45 5f 53 54 4d 54 2c 0a 20 20 43  UPDATE_STMT,.  C
357e2 4f 4e 54 45 4e 54 5f 44 45 4c 45 54 45 5f 53 54  ONTENT_DELETE_ST
357e3 4d 54 2c 0a 20 20 43 4f 4e 54 45 4e 54 5f 45 58  MT,.  CONTENT_EX
357e4 49 53 54 53 5f 53 54 4d 54 2c 0a 0a 20 20 42 4c  ISTS_STMT,..  BL
357e5 4f 43 4b 5f 49 4e 53 45 52 54 5f 53 54 4d 54 2c  OCK_INSERT_STMT,
357e6 0a 20 20 42 4c 4f 43 4b 5f 53 45 4c 45 43 54 5f  .  BLOCK_SELECT_
357e7 53 54 4d 54 2c 0a 20 20 42 4c 4f 43 4b 5f 44 45  STMT,.  BLOCK_DE
357e8 4c 45 54 45 5f 53 54 4d 54 2c 0a 20 20 42 4c 4f  LETE_STMT,.  BLO
357e9 43 4b 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f 53 54  CK_DELETE_ALL_ST
357ea 4d 54 2c 0a 0a 20 20 53 45 47 44 49 52 5f 4d 41  MT,..  SEGDIR_MA
357eb 58 5f 49 4e 44 45 58 5f 53 54 4d 54 2c 0a 20 20  X_INDEX_STMT,.  
357ec 53 45 47 44 49 52 5f 53 45 54 5f 53 54 4d 54 2c  SEGDIR_SET_STMT,
357ed 0a 20 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54  .  SEGDIR_SELECT
357ee 5f 4c 45 56 45 4c 5f 53 54 4d 54 2c 0a 20 20 53  _LEVEL_STMT,.  S
357ef 45 47 44 49 52 5f 53 50 41 4e 5f 53 54 4d 54 2c  EGDIR_SPAN_STMT,
357f0 0a 20 20 53 45 47 44 49 52 5f 44 45 4c 45 54 45  .  SEGDIR_DELETE
357f1 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f  _STMT,.  SEGDIR_
357f2 53 45 4c 45 43 54 5f 53 45 47 4d 45 4e 54 5f 53  SELECT_SEGMENT_S
357f3 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 53 45  TMT,.  SEGDIR_SE
357f4 4c 45 43 54 5f 41 4c 4c 5f 53 54 4d 54 2c 0a 20  LECT_ALL_STMT,. 
357f5 20 53 45 47 44 49 52 5f 44 45 4c 45 54 45 5f 41   SEGDIR_DELETE_A
357f6 4c 4c 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44 49  LL_STMT,.  SEGDI
357f7 52 5f 43 4f 55 4e 54 5f 53 54 4d 54 2c 0a 0a 20  R_COUNT_STMT,.. 
357f8 20 4d 41 58 5f 53 54 4d 54 20 20 20 20 20 20 20   MAX_STMT       
357f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
357fa 20 41 6c 77 61 79 73 20 61 74 20 65 6e 64 21 20   Always at end! 
357fb 2a 2f 0a 7d 20 66 75 6c 6c 74 65 78 74 5f 73 74  */.} fulltext_st
357fc 61 74 65 6d 65 6e 74 3b 0a 0a 2f 2a 20 54 68 65  atement;../* The
357fd 73 65 20 6d 75 73 74 20 65 78 61 63 74 6c 79 20  se must exactly 
357fe 6d 61 74 63 68 20 74 68 65 20 65 6e 75 6d 20 61  match the enum a
357ff 62 6f 76 65 2e 20 2a 2f 0a 2f 2a 20 54 4f 44 4f  bove. */./* TODO
35800 28 73 68 65 73 73 29 3a 20 49 73 20 74 68 65 72  (shess): Is ther
35801 65 20 73 6f 6d 65 20 72 69 73 6b 20 74 68 61 74  e some risk that
35802 20 61 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c   a statement wil
35803 6c 20 62 65 20 75 73 65 64 20 69 6e 20 74 77 6f  l be used in two
35804 0a 2a 2a 20 63 75 72 73 6f 72 73 20 61 74 20 6f  .** cursors at o
35805 6e 63 65 2c 20 65 2e 67 2e 20 20 69 66 20 61 20  nce, e.g.  if a 
35806 71 75 65 72 79 20 6a 6f 69 6e 73 20 61 20 76 69  query joins a vi
35807 72 74 75 61 6c 20 74 61 62 6c 65 20 74 6f 20 69  rtual table to i
35808 74 73 65 6c 66 3f 0a 2a 2a 20 49 66 20 73 6f 20  tself?.** If so 
35809 70 65 72 68 61 70 73 20 77 65 20 73 68 6f 75 6c  perhaps we shoul
3580a 64 20 6d 6f 76 65 20 73 6f 6d 65 20 6f 66 20 74  d move some of t
3580b 68 65 73 65 20 74 6f 20 74 68 65 20 63 75 72 73  hese to the curs
3580c 6f 72 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74  or object..*/.st
3580d 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
3580e 2a 63 6f 6e 73 74 20 66 75 6c 6c 74 65 78 74 5f  *const fulltext_
3580f 7a 53 74 61 74 65 6d 65 6e 74 5b 4d 41 58 5f 53  zStatement[MAX_S
35810 54 4d 54 5d 20 3d 20 7b 0a 20 20 2f 2a 20 43 4f  TMT] = {.  /* CO
35811 4e 54 45 4e 54 5f 49 4e 53 45 52 54 20 2a 2f 20  NTENT_INSERT */ 
35812 4e 55 4c 4c 2c 20 20 2f 2a 20 67 65 6e 65 72 61  NULL,  /* genera
35813 74 65 64 20 69 6e 20 63 6f 6e 74 65 6e 74 49 6e  ted in contentIn
35814 73 65 72 74 53 74 61 74 65 6d 65 6e 74 28 29 20  sertStatement() 
35815 2a 2f 0a 20 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f  */.  /* CONTENT_
35816 53 45 4c 45 43 54 20 2a 2f 20 4e 55 4c 4c 2c 20  SELECT */ NULL, 
35817 20 2f 2a 20 67 65 6e 65 72 61 74 65 64 20 69 6e   /* generated in
35818 20 63 6f 6e 74 65 6e 74 53 65 6c 65 63 74 53 74   contentSelectSt
35819 61 74 65 6d 65 6e 74 28 29 20 2a 2f 0a 20 20 2f  atement() */.  /
3581a 2a 20 43 4f 4e 54 45 4e 54 5f 55 50 44 41 54 45  * CONTENT_UPDATE
3581b 20 2a 2f 20 4e 55 4c 4c 2c 20 20 2f 2a 20 67 65   */ NULL,  /* ge
3581c 6e 65 72 61 74 65 64 20 69 6e 20 63 6f 6e 74 65  nerated in conte
3581d 6e 74 55 70 64 61 74 65 53 74 61 74 65 6d 65 6e  ntUpdateStatemen
3581e 74 28 29 20 2a 2f 0a 20 20 2f 2a 20 43 4f 4e 54  t() */.  /* CONT
3581f 45 4e 54 5f 44 45 4c 45 54 45 20 2a 2f 20 22 64  ENT_DELETE */ "d
35820 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f 63 6f 6e  elete from %_con
35821 74 65 6e 74 20 77 68 65 72 65 20 64 6f 63 69 64  tent where docid
35822 20 3d 20 3f 22 2c 0a 20 20 2f 2a 20 43 4f 4e 54   = ?",.  /* CONT
35823 45 4e 54 5f 45 58 49 53 54 53 20 2a 2f 20 22 73  ENT_EXISTS */ "s
35824 65 6c 65 63 74 20 64 6f 63 69 64 20 66 72 6f 6d  elect docid from
35825 20 25 5f 63 6f 6e 74 65 6e 74 20 6c 69 6d 69 74   %_content limit
35826 20 31 22 2c 0a 0a 20 20 2f 2a 20 42 4c 4f 43 4b   1",..  /* BLOCK
35827 5f 49 4e 53 45 52 54 20 2a 2f 0a 20 20 22 69 6e  _INSERT */.  "in
35828 73 65 72 74 20 69 6e 74 6f 20 25 5f 73 65 67 6d  sert into %_segm
35829 65 6e 74 73 20 28 62 6c 6f 63 6b 69 64 2c 20 62  ents (blockid, b
3582a 6c 6f 63 6b 29 20 76 61 6c 75 65 73 20 28 6e 75  lock) values (nu
3582b 6c 6c 2c 20 3f 29 22 2c 0a 20 20 2f 2a 20 42 4c  ll, ?)",.  /* BL
3582c 4f 43 4b 5f 53 45 4c 45 43 54 20 2a 2f 20 22 73  OCK_SELECT */ "s
3582d 65 6c 65 63 74 20 62 6c 6f 63 6b 20 66 72 6f 6d  elect block from
3582e 20 25 5f 73 65 67 6d 65 6e 74 73 20 77 68 65 72   %_segments wher
3582f 65 20 62 6c 6f 63 6b 69 64 20 3d 20 3f 22 2c 0a  e blockid = ?",.
35830 20 20 2f 2a 20 42 4c 4f 43 4b 5f 44 45 4c 45 54    /* BLOCK_DELET
35831 45 20 2a 2f 20 22 64 65 6c 65 74 65 20 66 72 6f  E */ "delete fro
35832 6d 20 25 5f 73 65 67 6d 65 6e 74 73 20 77 68 65  m %_segments whe
35833 72 65 20 62 6c 6f 63 6b 69 64 20 62 65 74 77 65  re blockid betwe
35834 65 6e 20 3f 20 61 6e 64 20 3f 22 2c 0a 20 20 2f  en ? and ?",.  /
35835 2a 20 42 4c 4f 43 4b 5f 44 45 4c 45 54 45 5f 41  * BLOCK_DELETE_A
35836 4c 4c 20 2a 2f 20 22 64 65 6c 65 74 65 20 66 72  LL */ "delete fr
35837 6f 6d 20 25 5f 73 65 67 6d 65 6e 74 73 22 2c 0a  om %_segments",.
35838 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f 4d 41 58  .  /* SEGDIR_MAX
35839 5f 49 4e 44 45 58 20 2a 2f 20 22 73 65 6c 65 63  _INDEX */ "selec
3583a 74 20 6d 61 78 28 69 64 78 29 20 66 72 6f 6d 20  t max(idx) from 
3583b 25 5f 73 65 67 64 69 72 20 77 68 65 72 65 20 6c  %_segdir where l
3583c 65 76 65 6c 20 3d 20 3f 22 2c 0a 20 20 2f 2a 20  evel = ?",.  /* 
3583d 53 45 47 44 49 52 5f 53 45 54 20 2a 2f 20 22 69  SEGDIR_SET */ "i
3583e 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f 73 65 67  nsert into %_seg
3583f 64 69 72 20 76 61 6c 75 65 73 20 28 3f 2c 20 3f  dir values (?, ?
35840 2c 20 3f 2c 20 3f 2c 20 3f 2c 20 3f 29 22 2c 0a  , ?, ?, ?, ?)",.
35841 20 20 2f 2a 20 53 45 47 44 49 52 5f 53 45 4c 45    /* SEGDIR_SELE
35842 43 54 5f 4c 45 56 45 4c 20 2a 2f 0a 20 20 22 73  CT_LEVEL */.  "s
35843 65 6c 65 63 74 20 73 74 61 72 74 5f 62 6c 6f 63  elect start_bloc
35844 6b 2c 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c  k, leaves_end_bl
35845 6f 63 6b 2c 20 72 6f 6f 74 20 66 72 6f 6d 20 25  ock, root from %
35846 5f 73 65 67 64 69 72 20 22 0a 20 20 22 20 77 68  _segdir ".  " wh
35847 65 72 65 20 6c 65 76 65 6c 20 3d 20 3f 20 6f 72  ere level = ? or
35848 64 65 72 20 62 79 20 69 64 78 22 2c 0a 20 20 2f  der by idx",.  /
35849 2a 20 53 45 47 44 49 52 5f 53 50 41 4e 20 2a 2f  * SEGDIR_SPAN */
3584a 0a 20 20 22 73 65 6c 65 63 74 20 6d 69 6e 28 73  .  "select min(s
3584b 74 61 72 74 5f 62 6c 6f 63 6b 29 2c 20 6d 61 78  tart_block), max
3584c 28 65 6e 64 5f 62 6c 6f 63 6b 29 20 66 72 6f 6d  (end_block) from
3584d 20 25 5f 73 65 67 64 69 72 20 22 0a 20 20 22 20   %_segdir ".  " 
3584e 77 68 65 72 65 20 6c 65 76 65 6c 20 3d 20 3f 20  where level = ? 
3584f 61 6e 64 20 73 74 61 72 74 5f 62 6c 6f 63 6b 20  and start_block 
35850 3c 3e 20 30 22 2c 0a 20 20 2f 2a 20 53 45 47 44  <> 0",.  /* SEGD
35851 49 52 5f 44 45 4c 45 54 45 20 2a 2f 20 22 64 65  IR_DELETE */ "de
35852 6c 65 74 65 20 66 72 6f 6d 20 25 5f 73 65 67 64  lete from %_segd
35853 69 72 20 77 68 65 72 65 20 6c 65 76 65 6c 20 3d  ir where level =
35854 20 3f 22 2c 0a 0a 20 20 2f 2a 20 4e 4f 54 45 28   ?",..  /* NOTE(
35855 73 68 65 73 73 29 3a 20 54 68 65 20 66 69 72 73  shess): The firs
35856 74 20 74 68 72 65 65 20 72 65 73 75 6c 74 73 20  t three results 
35857 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
35858 20 74 77 6f 0a 20 20 2a 2a 20 73 74 61 74 65 6d   two.  ** statem
35859 65 6e 74 73 20 6d 75 73 74 20 6d 61 74 63 68 2e  ents must match.
3585a 0a 20 20 2a 2f 0a 20 20 2f 2a 20 53 45 47 44 49  .  */.  /* SEGDI
3585b 52 5f 53 45 4c 45 43 54 5f 53 45 47 4d 45 4e 54  R_SELECT_SEGMENT
3585c 20 2a 2f 0a 20 20 22 73 65 6c 65 63 74 20 73 74   */.  "select st
3585d 61 72 74 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76 65  art_block, leave
3585e 73 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 72 6f 6f  s_end_block, roo
3585f 74 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 20  t from %_segdir 
35860 22 0a 20 20 22 20 77 68 65 72 65 20 6c 65 76 65  ".  " where leve
35861 6c 20 3d 20 3f 20 61 6e 64 20 69 64 78 20 3d 20  l = ? and idx = 
35862 3f 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f  ?",.  /* SEGDIR_
35863 53 45 4c 45 43 54 5f 41 4c 4c 20 2a 2f 0a 20 20  SELECT_ALL */.  
35864 22 73 65 6c 65 63 74 20 73 74 61 72 74 5f 62 6c  "select start_bl
35865 6f 63 6b 2c 20 6c 65 61 76 65 73 5f 65 6e 64 5f  ock, leaves_end_
35866 62 6c 6f 63 6b 2c 20 72 6f 6f 74 20 66 72 6f 6d  block, root from
35867 20 25 5f 73 65 67 64 69 72 20 22 0a 20 20 22 20   %_segdir ".  " 
35868 6f 72 64 65 72 20 62 79 20 6c 65 76 65 6c 20 64  order by level d
35869 65 73 63 2c 20 69 64 78 20 61 73 63 22 2c 0a 20  esc, idx asc",. 
3586a 20 2f 2a 20 53 45 47 44 49 52 5f 44 45 4c 45 54   /* SEGDIR_DELET
3586b 45 5f 41 4c 4c 20 2a 2f 20 22 64 65 6c 65 74 65  E_ALL */ "delete
3586c 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 22 2c   from %_segdir",
3586d 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f 43 4f 55  .  /* SEGDIR_COU
3586e 4e 54 20 2a 2f 20 22 73 65 6c 65 63 74 20 63 6f  NT */ "select co
3586f 75 6e 74 28 2a 29 2c 20 69 66 6e 75 6c 6c 28 6d  unt(*), ifnull(m
35870 61 78 28 6c 65 76 65 6c 29 2c 30 29 20 66 72 6f  ax(level),0) fro
35871 6d 20 25 5f 73 65 67 64 69 72 22 2c 0a 7d 3b 0a  m %_segdir",.};.
35872 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6e 6e 65 63 74  ./*.** A connect
35873 69 6f 6e 20 74 6f 20 61 20 66 75 6c 6c 74 65 78  ion to a fulltex
35874 74 20 69 6e 64 65 78 20 69 73 20 61 6e 20 69 6e  t index is an in
35875 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
35876 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63  llowing.** struc
35877 74 75 72 65 2e 20 20 54 68 65 20 78 43 72 65 61  ture.  The xCrea
35878 74 65 20 61 6e 64 20 78 43 6f 6e 6e 65 63 74 20  te and xConnect 
35879 6d 65 74 68 6f 64 73 20 63 72 65 61 74 65 20 61  methods create a
3587a 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66  n instance.** of
3587b 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
3587c 61 6e 64 20 78 44 65 73 74 72 6f 79 20 61 6e 64  and xDestroy and
3587d 20 78 44 69 73 63 6f 6e 6e 65 63 74 20 66 72 65   xDisconnect fre
3587e 65 20 74 68 61 74 20 69 6e 73 74 61 6e 63 65 2e  e that instance.
3587f 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 6d 65  .** All other me
35880 74 68 6f 64 73 20 72 65 63 65 69 76 65 20 61 20  thods receive a 
35881 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73  pointer to the s
35882 74 72 75 63 74 75 72 65 20 61 73 20 6f 6e 65 20  tructure as one 
35883 6f 66 20 74 68 65 69 72 0a 2a 2a 20 61 72 67 75  of their.** argu
35884 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  ments..*/.struct
35885 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 7b   fulltext_vtab {
35886 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
35887 62 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20  base;           
35888 20 20 20 20 2f 2a 20 42 61 73 65 20 63 6c 61 73      /* Base clas
35889 73 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65  s used by SQLite
3588a 20 63 6f 72 65 20 2a 2f 0a 20 20 73 71 6c 69 74   core */.  sqlit
3588b 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20  e3 *db;         
3588c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
3588d 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
3588e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73  ection */.  cons
3588f 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20  t char *zDb;    
35890 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35891 6c 6f 67 69 63 61 6c 20 64 61 74 61 62 61 73 65  logical database
35892 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74   name */.  const
35893 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
35894 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 76              /* v
35895 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6e 61 6d  irtual table nam
35896 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75  e */.  int nColu
35897 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mn;             
35898 20 20 20 20 20 20 20 20 2f 2a 20 6e 75 6d 62 65          /* numbe
35899 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
3589a 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f  virtual table */
3589b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 75  .  char **azColu
3589c 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mn;             
3589d 20 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 61      /* column na
3589e 6d 65 73 2e 20 20 6d 61 6c 6c 6f 63 65 64 20 2a  mes.  malloced *
3589f 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6e  /.  char **azCon
358a0 74 65 6e 74 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  tentColumn;     
358a1 20 20 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e       /* column n
358a2 61 6d 65 73 20 69 6e 20 63 6f 6e 74 65 6e 74 20  ames in content 
358a3 74 61 62 6c 65 3b 20 6d 61 6c 6c 6f 63 65 64 20  table; malloced 
358a4 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  */.  sqlite3_tok
358a5 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a  enizer *pTokeniz
358a6 65 72 3b 20 20 20 2f 2a 20 74 6f 6b 65 6e 69 7a  er;   /* tokeniz
358a7 65 72 20 66 6f 72 20 69 6e 73 65 72 74 73 20 61  er for inserts a
358a8 6e 64 20 71 75 65 72 69 65 73 20 2a 2f 0a 0a 20  nd queries */.. 
358a9 20 2f 2a 20 50 72 65 63 6f 6d 70 69 6c 65 64 20   /* Precompiled 
358aa 73 74 61 74 65 6d 65 6e 74 73 20 77 68 69 63 68  statements which
358ab 20 77 65 20 6b 65 65 70 20 61 73 20 6c 6f 6e 67   we keep as long
358ac 20 61 73 20 74 68 65 20 74 61 62 6c 65 20 69 73   as the table is
358ad 0a 20 20 2a 2a 20 6f 70 65 6e 2e 0a 20 20 2a 2f  .  ** open..  */
358ae 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
358af 2a 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d  *pFulltextStatem
358b0 65 6e 74 73 5b 4d 41 58 5f 53 54 4d 54 5d 3b 0a  ents[MAX_STMT];.
358b1 0a 20 20 2f 2a 20 50 72 65 63 6f 6d 70 69 6c 65  .  /* Precompile
358b2 64 20 73 74 61 74 65 6d 65 6e 74 73 20 75 73 65  d statements use
358b3 64 20 66 6f 72 20 73 65 67 6d 65 6e 74 20 6d 65  d for segment me
358b4 72 67 65 73 2e 20 20 57 65 20 72 75 6e 20 61 0a  rges.  We run a.
358b5 20 20 2a 2a 20 73 65 70 61 72 61 74 65 20 73 65    ** separate se
358b6 6c 65 63 74 20 61 63 72 6f 73 73 20 74 68 65 20  lect across the 
358b7 6c 65 61 66 20 6c 65 76 65 6c 20 6f 66 20 65 61  leaf level of ea
358b8 63 68 20 74 72 65 65 20 62 65 69 6e 67 20 6d 65  ch tree being me
358b9 72 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  rged..  */.  sql
358ba 69 74 65 33 5f 73 74 6d 74 20 2a 70 4c 65 61 66  ite3_stmt *pLeaf
358bb 53 65 6c 65 63 74 53 74 6d 74 73 5b 4d 45 52 47  SelectStmts[MERG
358bc 45 5f 43 4f 55 4e 54 5d 3b 0a 20 20 2f 2a 20 54  E_COUNT];.  /* T
358bd 68 65 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65  he statement use
358be 64 20 74 6f 20 70 72 65 70 61 72 65 20 70 4c 65  d to prepare pLe
358bf 61 66 53 65 6c 65 63 74 53 74 6d 74 73 2e 20 2a  afSelectStmts. *
358c0 2f 0a 23 64 65 66 69 6e 65 20 4c 45 41 46 5f 53  /.#define LEAF_S
358c1 45 4c 45 43 54 20 5c 0a 20 20 22 73 65 6c 65 63  ELECT \.  "selec
358c2 74 20 62 6c 6f 63 6b 20 66 72 6f 6d 20 25 5f 73  t block from %_s
358c3 65 67 6d 65 6e 74 73 20 77 68 65 72 65 20 62 6c  egments where bl
358c4 6f 63 6b 69 64 20 62 65 74 77 65 65 6e 20 3f 20  ockid between ? 
358c5 61 6e 64 20 3f 20 6f 72 64 65 72 20 62 79 20 62  and ? order by b
358c6 6c 6f 63 6b 69 64 22 0a 0a 20 20 2f 2a 20 54 68  lockid"..  /* Th
358c7 65 73 65 20 62 75 66 66 65 72 20 70 65 6e 64 69  ese buffer pendi
358c8 6e 67 20 69 6e 64 65 78 20 75 70 64 61 74 65 73  ng index updates
358c9 20 64 75 72 69 6e 67 20 74 72 61 6e 73 61 63 74   during transact
358ca 69 6f 6e 73 2e 0a 20 20 2a 2a 20 6e 50 65 6e 64  ions..  ** nPend
358cb 69 6e 67 44 61 74 61 20 65 73 74 69 6d 61 74 65  ingData estimate
358cc 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 69 7a  s the memory siz
358cd 65 20 6f 66 20 74 68 65 20 70 65 6e 64 69 6e 67  e of the pending
358ce 20 64 61 74 61 2e 20 20 49 74 0a 20 20 2a 2a 20   data.  It.  ** 
358cf 64 6f 65 73 6e 27 74 20 69 6e 63 6c 75 64 65 20  doesn't include 
358d0 74 68 65 20 68 61 73 68 2d 62 75 63 6b 65 74 20  the hash-bucket 
358d1 6f 76 65 72 68 65 61 64 2c 20 6e 6f 72 20 61 6e  overhead, nor an
358d2 79 20 6d 61 6c 6c 6f 63 0a 20 20 2a 2a 20 6f 76  y malloc.  ** ov
358d3 65 72 68 65 61 64 2e 20 20 57 68 65 6e 20 6e 50  erhead.  When nP
358d4 65 6e 64 69 6e 67 44 61 74 61 20 65 78 63 65 65  endingData excee
358d5 64 73 20 6b 50 65 6e 64 69 6e 67 54 68 72 65 73  ds kPendingThres
358d6 68 6f 6c 64 2c 20 74 68 65 0a 20 20 2a 2a 20 62  hold, the.  ** b
358d7 75 66 66 65 72 20 69 73 20 66 6c 75 73 68 65 64  uffer is flushed
358d8 20 65 76 65 6e 20 62 65 66 6f 72 65 20 74 68 65   even before the
358d9 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6c 6f   transaction clo
358da 73 65 73 2e 0a 20 20 2a 2a 20 70 65 6e 64 69 6e  ses..  ** pendin
358db 67 54 65 72 6d 73 20 73 74 6f 72 65 73 20 74 68  gTerms stores th
358dc 65 20 64 61 74 61 2c 20 61 6e 64 20 69 73 20 6f  e data, and is o
358dd 6e 6c 79 20 76 61 6c 69 64 20 77 68 65 6e 20 6e  nly valid when n
358de 50 65 6e 64 69 6e 67 44 61 74 61 0a 20 20 2a 2a  PendingData.  **
358df 20 69 73 20 3e 3d 30 20 28 6e 50 65 6e 64 69 6e   is >=0 (nPendin
358e0 67 44 61 74 61 3c 30 20 6d 65 61 6e 73 20 70 65  gData<0 means pe
358e1 6e 64 69 6e 67 54 65 72 6d 73 20 68 61 73 20 6e  ndingTerms has n
358e2 6f 74 20 62 65 65 6e 0a 20 20 2a 2a 20 69 6e 69  ot been.  ** ini
358e3 74 69 61 6c 69 7a 65 64 29 2e 20 20 69 50 72 65  tialized).  iPre
358e4 76 44 6f 63 69 64 20 69 73 20 74 68 65 20 6c 61  vDocid is the la
358e5 73 74 20 64 6f 63 69 64 20 77 72 69 74 74 65 6e  st docid written
358e6 2c 20 75 73 65 64 20 74 6f 20 6d 61 6b 65 0a 20  , used to make. 
358e7 20 2a 2a 20 63 65 72 74 61 69 6e 20 77 65 27 72   ** certain we'r
358e8 65 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 20 73  e inserting in s
358e9 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a 20 20 2a  orted order..  *
358ea 2f 0a 20 20 69 6e 74 20 6e 50 65 6e 64 69 6e 67  /.  int nPending
358eb 44 61 74 61 3b 0a 23 64 65 66 69 6e 65 20 6b 50  Data;.#define kP
358ec 65 6e 64 69 6e 67 54 68 72 65 73 68 6f 6c 64 20  endingThreshold 
358ed 28 31 2a 31 30 32 34 2a 31 30 32 34 29 0a 20 20  (1*1024*1024).  
358ee 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 50 72  sqlite_int64 iPr
358ef 65 76 44 6f 63 69 64 3b 0a 20 20 66 74 73 33 48  evDocid;.  fts3H
358f0 61 73 68 20 70 65 6e 64 69 6e 67 54 65 72 6d 73  ash pendingTerms
358f1 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e  ;.};../*.** When
358f2 20 74 68 65 20 63 6f 72 65 20 77 61 6e 74 73 20   the core wants 
358f3 74 6f 20 64 6f 20 61 20 71 75 65 72 79 2c 20 69  to do a query, i
358f4 74 20 63 72 65 61 74 65 20 61 20 63 75 72 73 6f  t create a curso
358f5 72 20 75 73 69 6e 67 20 61 0a 2a 2a 20 63 61 6c  r using a.** cal
358f6 6c 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54 68 69  l to xOpen.  Thi
358f7 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61  s structure is a
358f8 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 61 20  n instance of a 
358f9 63 75 72 73 6f 72 2e 20 20 49 74 0a 2a 2a 20 69  cursor.  It.** i
358fa 73 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 78  s destroyed by x
358fb 43 6c 6f 73 65 2e 0a 2a 2f 0a 74 79 70 65 64 65  Close..*/.typede
358fc 66 20 73 74 72 75 63 74 20 66 75 6c 6c 74 65 78  f struct fulltex
358fd 74 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c  t_cursor {.  sql
358fe 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
358ff 20 62 61 73 65 3b 20 20 20 20 20 20 20 20 2f 2a   base;        /*
35900 20 42 61 73 65 20 63 6c 61 73 73 20 75 73 65 64   Base class used
35901 20 62 79 20 53 51 4c 69 74 65 20 63 6f 72 65 20   by SQLite core 
35902 2a 2f 0a 20 20 51 75 65 72 79 54 79 70 65 20 69  */.  QueryType i
35903 43 75 72 73 6f 72 54 79 70 65 3b 20 20 20 20 20  CursorType;     
35904 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66        /* Copy of
35905 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
35906 6e 66 6f 2e 69 64 78 4e 75 6d 20 2a 2f 0a 20 20  nfo.idxNum */.  
35907 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
35908 74 6d 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  tmt;            
35909 20 2f 2a 20 50 72 65 70 61 72 65 64 20 73 74 61   /* Prepared sta
3590a 74 65 6d 65 6e 74 20 69 6e 20 75 73 65 20 62 79  tement in use by
3590b 20 74 68 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20   the cursor */. 
3590c 20 69 6e 74 20 65 6f 66 3b 20 20 20 20 20 20 20   int eof;       
3590d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3590e 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 74 20    /* True if at 
3590f 45 6e 64 20 4f 66 20 52 65 73 75 6c 74 73 20 2a  End Of Results *
35910 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 45  /.  Fts3Expr *pE
35911 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  xpr;            
35912 20 20 20 20 20 2f 2a 20 50 61 72 73 65 64 20 4d       /* Parsed M
35913 41 54 43 48 20 71 75 65 72 79 20 73 74 72 69 6e  ATCH query strin
35914 67 20 2a 2f 0a 20 20 53 6e 69 70 70 65 74 20 73  g */.  Snippet s
35915 6e 69 70 70 65 74 3b 20 20 20 20 20 20 20 20 20  nippet;         
35916 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65          /* Cache
35917 64 20 73 6e 69 70 70 65 74 20 66 6f 72 20 74 68  d snippet for th
35918 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 2a 2f  e current row */
35919 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20  .  int iColumn; 
3591a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3591b 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 62 65      /* Column be
3591c 69 6e 67 20 73 65 61 72 63 68 65 64 20 2a 2f 0a  ing searched */.
3591d 20 20 44 61 74 61 42 75 66 66 65 72 20 72 65 73    DataBuffer res
3591e 75 6c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ult;            
3591f 20 20 20 2f 2a 20 44 6f 63 6c 69 73 74 20 72 65     /* Doclist re
35920 73 75 6c 74 73 20 66 72 6f 6d 20 66 75 6c 6c 74  sults from fullt
35921 65 78 74 51 75 65 72 79 20 2a 2f 0a 20 20 44 4c  extQuery */.  DL
35922 52 65 61 64 65 72 20 72 65 61 64 65 72 3b 20 20  Reader reader;  
35923 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35924 2a 20 52 65 73 75 6c 74 20 72 65 61 64 65 72 20  * Result reader 
35925 69 66 20 72 65 73 75 6c 74 20 6e 6f 74 20 65 6d  if result not em
35926 70 74 79 20 2a 2f 0a 7d 20 66 75 6c 6c 74 65 78  pty */.} fulltex
35927 74 5f 63 75 72 73 6f 72 3b 0a 0a 73 74 61 74 69  t_cursor;..stati
35928 63 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  c fulltext_vtab 
35929 2a 63 75 72 73 6f 72 5f 76 74 61 62 28 66 75 6c  *cursor_vtab(ful
3592a 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 63 29  ltext_cursor *c)
3592b 7b 0a 20 20 72 65 74 75 72 6e 20 28 66 75 6c 6c  {.  return (full
3592c 74 65 78 74 5f 76 74 61 62 20 2a 29 20 63 2d 3e  text_vtab *) c->
3592d 62 61 73 65 2e 70 56 74 61 62 3b 0a 7d 0a 0a 73  base.pVtab;.}..s
3592e 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
3592f 74 65 33 5f 6d 6f 64 75 6c 65 20 66 74 73 33 4d  te3_module fts3M
35930 6f 64 75 6c 65 3b 20 20 20 2f 2a 20 66 6f 72 77  odule;   /* forw
35931 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  ard declaration 
35932 2a 2f 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 20  */../* Return a 
35933 64 79 6e 61 6d 69 63 61 6c 6c 79 20 67 65 6e 65  dynamically gene
35934 72 61 74 65 64 20 73 74 61 74 65 6d 65 6e 74 20  rated statement 
35935 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20 2a 20 20  of the form. *  
35936 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f 63   insert into %_c
35937 6f 6e 74 65 6e 74 20 28 64 6f 63 69 64 2c 20 2e  ontent (docid, .
35938 2e 2e 29 20 76 61 6c 75 65 73 20 28 3f 2c 20 2e  ..) values (?, .
35939 2e 2e 29 0a 20 2a 2f 0a 73 74 61 74 69 63 20 63  ..). */.static c
3593a 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 74 65  onst char *conte
3593b 6e 74 49 6e 73 65 72 74 53 74 61 74 65 6d 65 6e  ntInsertStatemen
3593c 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  t(fulltext_vtab 
3593d 2a 76 29 7b 0a 20 20 53 74 72 69 6e 67 42 75 66  *v){.  StringBuf
3593e 66 65 72 20 73 62 3b 0a 20 20 69 6e 74 20 69 3b  fer sb;.  int i;
3593f 0a 0a 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75  ..  initStringBu
35940 66 66 65 72 28 26 73 62 29 3b 0a 20 20 61 70 70  ffer(&sb);.  app
35941 65 6e 64 28 26 73 62 2c 20 22 69 6e 73 65 72 74  end(&sb, "insert
35942 20 69 6e 74 6f 20 25 5f 63 6f 6e 74 65 6e 74 20   into %_content 
35943 28 64 6f 63 69 64 2c 20 22 29 3b 0a 20 20 61 70  (docid, ");.  ap
35944 70 65 6e 64 4c 69 73 74 28 26 73 62 2c 20 76 2d  pendList(&sb, v-
35945 3e 6e 43 6f 6c 75 6d 6e 2c 20 76 2d 3e 61 7a 43  >nColumn, v->azC
35946 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20  ontentColumn);. 
35947 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 29 20   append(&sb, ") 
35948 76 61 6c 75 65 73 20 28 3f 22 29 3b 0a 20 20 66  values (?");.  f
35949 6f 72 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f  or(i=0; i<v->nCo
3594a 6c 75 6d 6e 3b 20 2b 2b 69 29 0a 20 20 20 20 61  lumn; ++i).    a
3594b 70 70 65 6e 64 28 26 73 62 2c 20 22 2c 20 3f 22  ppend(&sb, ", ?"
3594c 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 62 2c  );.  append(&sb,
3594d 20 22 29 22 29 3b 0a 20 20 72 65 74 75 72 6e 20   ")");.  return 
3594e 73 74 72 69 6e 67 42 75 66 66 65 72 44 61 74 61  stringBufferData
3594f 28 26 73 62 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74  (&sb);.}../* Ret
35950 75 72 6e 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c  urn a dynamicall
35951 79 20 67 65 6e 65 72 61 74 65 64 20 73 74 61 74  y generated stat
35952 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 6f 72  ement of the for
35953 6d 0a 20 2a 20 20 20 73 65 6c 65 63 74 20 3c 63  m. *   select <c
35954 6f 6e 74 65 6e 74 20 63 6f 6c 75 6d 6e 73 3e 20  ontent columns> 
35955 66 72 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74 20 77  from %_content w
35956 68 65 72 65 20 64 6f 63 69 64 20 3d 20 3f 0a 20  here docid = ?. 
35957 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
35958 63 68 61 72 20 2a 63 6f 6e 74 65 6e 74 53 65 6c  char *contentSel
35959 65 63 74 53 74 61 74 65 6d 65 6e 74 28 66 75 6c  ectStatement(ful
3595a 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a  ltext_vtab *v){.
3595b 20 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 73    StringBuffer s
3595c 62 3b 0a 20 20 69 6e 69 74 53 74 72 69 6e 67 42  b;.  initStringB
3595d 75 66 66 65 72 28 26 73 62 29 3b 0a 20 20 61 70  uffer(&sb);.  ap
3595e 70 65 6e 64 28 26 73 62 2c 20 22 53 45 4c 45 43  pend(&sb, "SELEC
3595f 54 20 22 29 3b 0a 20 20 61 70 70 65 6e 64 4c 69  T ");.  appendLi
35960 73 74 28 26 73 62 2c 20 76 2d 3e 6e 43 6f 6c 75  st(&sb, v->nColu
35961 6d 6e 2c 20 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74  mn, v->azContent
35962 43 6f 6c 75 6d 6e 29 3b 0a 20 20 61 70 70 65 6e  Column);.  appen
35963 64 28 26 73 62 2c 20 22 20 46 52 4f 4d 20 25 5f  d(&sb, " FROM %_
35964 63 6f 6e 74 65 6e 74 20 57 48 45 52 45 20 64 6f  content WHERE do
35965 63 69 64 20 3d 20 3f 22 29 3b 0a 20 20 72 65 74  cid = ?");.  ret
35966 75 72 6e 20 73 74 72 69 6e 67 42 75 66 66 65 72  urn stringBuffer
35967 44 61 74 61 28 26 73 62 29 3b 0a 7d 0a 0a 2f 2a  Data(&sb);.}../*
35968 20 52 65 74 75 72 6e 20 61 20 64 79 6e 61 6d 69   Return a dynami
35969 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20  cally generated 
3596a 73 74 61 74 65 6d 65 6e 74 20 6f 66 20 74 68 65  statement of the
3596b 20 66 6f 72 6d 0a 20 2a 20 20 20 75 70 64 61 74   form. *   updat
3596c 65 20 25 5f 63 6f 6e 74 65 6e 74 20 73 65 74 20  e %_content set 
3596d 5b 63 6f 6c 5f 30 5d 20 3d 20 3f 2c 20 5b 63 6f  [col_0] = ?, [co
3596e 6c 5f 31 5d 20 3d 20 3f 2c 20 2e 2e 2e 0a 20 2a  l_1] = ?, .... *
3596f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35970 20 20 20 20 77 68 65 72 65 20 64 6f 63 69 64 20      where docid 
35971 3d 20 3f 0a 20 2a 2f 0a 73 74 61 74 69 63 20 63  = ?. */.static c
35972 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 74 65  onst char *conte
35973 6e 74 55 70 64 61 74 65 53 74 61 74 65 6d 65 6e  ntUpdateStatemen
35974 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  t(fulltext_vtab 
35975 2a 76 29 7b 0a 20 20 53 74 72 69 6e 67 42 75 66  *v){.  StringBuf
35976 66 65 72 20 73 62 3b 0a 20 20 69 6e 74 20 69 3b  fer sb;.  int i;
35977 0a 0a 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75  ..  initStringBu
35978 66 66 65 72 28 26 73 62 29 3b 0a 20 20 61 70 70  ffer(&sb);.  app
35979 65 6e 64 28 26 73 62 2c 20 22 75 70 64 61 74 65  end(&sb, "update
3597a 20 25 5f 63 6f 6e 74 65 6e 74 20 73 65 74 20 22   %_content set "
3597b 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
3597c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29  v->nColumn; ++i)
3597d 20 7b 0a 20 20 20 20 69 66 28 20 69 3e 30 20 29   {.    if( i>0 )
3597e 7b 0a 20 20 20 20 20 20 61 70 70 65 6e 64 28 26  {.      append(&
3597f 73 62 2c 20 22 2c 20 22 29 3b 0a 20 20 20 20 7d  sb, ", ");.    }
35980 0a 20 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c  .    append(&sb,
35981 20 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c   v->azContentCol
35982 75 6d 6e 5b 69 5d 29 3b 0a 20 20 20 20 61 70 70  umn[i]);.    app
35983 65 6e 64 28 26 73 62 2c 20 22 20 3d 20 3f 22 29  end(&sb, " = ?")
35984 3b 0a 20 20 7d 0a 20 20 61 70 70 65 6e 64 28 26  ;.  }.  append(&
35985 73 62 2c 20 22 20 77 68 65 72 65 20 64 6f 63 69  sb, " where doci
35986 64 20 3d 20 3f 22 29 3b 0a 20 20 72 65 74 75 72  d = ?");.  retur
35987 6e 20 73 74 72 69 6e 67 42 75 66 66 65 72 44 61  n stringBufferDa
35988 74 61 28 26 73 62 29 3b 0a 7d 0a 0a 2f 2a 20 50  ta(&sb);.}../* P
35989 75 74 73 20 61 20 66 72 65 73 68 6c 79 2d 70 72  uts a freshly-pr
3598a 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
3598b 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 69   determined by i
3598c 53 74 6d 74 20 69 6e 20 2a 70 70 53 74 6d 74 2e  Stmt in *ppStmt.
3598d 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 64 69 63  .** If the indic
3598e 61 74 65 64 20 73 74 61 74 65 6d 65 6e 74 20 68  ated statement h
3598f 61 73 20 6e 65 76 65 72 20 62 65 65 6e 20 70 72  as never been pr
35990 65 70 61 72 65 64 2c 20 69 74 20 69 73 20 70 72  epared, it is pr
35991 65 70 61 72 65 64 0a 2a 2a 20 61 6e 64 20 63 61  epared.** and ca
35992 63 68 65 64 2c 20 6f 74 68 65 72 77 69 73 65 20  ched, otherwise 
35993 74 68 65 20 63 61 63 68 65 64 20 76 65 72 73 69  the cached versi
35994 6f 6e 20 69 73 20 72 65 73 65 74 2e 0a 2a 2f 0a  on is reset..*/.
35995 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 5f 67  static int sql_g
35996 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 66 75 6c  et_statement(ful
35997 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 66  ltext_vtab *v, f
35998 75 6c 6c 74 65 78 74 5f 73 74 61 74 65 6d 65 6e  ulltext_statemen
35999 74 20 69 53 74 6d 74 2c 0a 20 20 20 20 20 20 20  t iStmt,.       
3599a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3599b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74        sqlite3_st
3599c 6d 74 20 2a 2a 70 70 53 74 6d 74 29 7b 0a 20 20  mt **ppStmt){.  
3599d 61 73 73 65 72 74 28 20 69 53 74 6d 74 3c 4d 41  assert( iStmt<MA
3599e 58 5f 53 54 4d 54 20 29 3b 0a 20 20 69 66 28 20  X_STMT );.  if( 
3599f 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74  v->pFulltextStat
359a0 65 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 3d 3d 4e  ements[iStmt]==N
359a1 55 4c 4c 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  ULL ){.    const
359a2 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 20   char *zStmt;.  
359a3 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 73 77    int rc;.    sw
359a4 69 74 63 68 28 20 69 53 74 6d 74 20 29 7b 0a 20  itch( iStmt ){. 
359a5 20 20 20 20 20 63 61 73 65 20 43 4f 4e 54 45 4e       case CONTEN
359a6 54 5f 49 4e 53 45 52 54 5f 53 54 4d 54 3a 0a 20  T_INSERT_STMT:. 
359a7 20 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 63         zStmt = c
359a8 6f 6e 74 65 6e 74 49 6e 73 65 72 74 53 74 61 74  ontentInsertStat
359a9 65 6d 65 6e 74 28 76 29 3b 20 62 72 65 61 6b 3b  ement(v); break;
359aa 0a 20 20 20 20 20 20 63 61 73 65 20 43 4f 4e 54  .      case CONT
359ab 45 4e 54 5f 53 45 4c 45 43 54 5f 53 54 4d 54 3a  ENT_SELECT_STMT:
359ac 0a 20 20 20 20 20 20 20 20 7a 53 74 6d 74 20 3d  .        zStmt =
359ad 20 63 6f 6e 74 65 6e 74 53 65 6c 65 63 74 53 74   contentSelectSt
359ae 61 74 65 6d 65 6e 74 28 76 29 3b 20 62 72 65 61  atement(v); brea
359af 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 43 4f  k;.      case CO
359b0 4e 54 45 4e 54 5f 55 50 44 41 54 45 5f 53 54 4d  NTENT_UPDATE_STM
359b1 54 3a 0a 20 20 20 20 20 20 20 20 7a 53 74 6d 74  T:.        zStmt
359b2 20 3d 20 63 6f 6e 74 65 6e 74 55 70 64 61 74 65   = contentUpdate
359b3 53 74 61 74 65 6d 65 6e 74 28 76 29 3b 20 62 72  Statement(v); br
359b4 65 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75  eak;.      defau
359b5 6c 74 3a 0a 20 20 20 20 20 20 20 20 7a 53 74 6d  lt:.        zStm
359b6 74 20 3d 20 66 75 6c 6c 74 65 78 74 5f 7a 53 74  t = fulltext_zSt
359b7 61 74 65 6d 65 6e 74 5b 69 53 74 6d 74 5d 3b 0a  atement[iStmt];.
359b8 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73      }.    rc = s
359b9 71 6c 5f 70 72 65 70 61 72 65 28 76 2d 3e 64 62  ql_prepare(v->db
359ba 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61  , v->zDb, v->zNa
359bb 6d 65 2c 20 26 76 2d 3e 70 46 75 6c 6c 74 65 78  me, &v->pFulltex
359bc 74 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d  tStatements[iStm
359bd 74 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  t],.            
359be 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 53 74               zSt
359bf 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 7a 53 74  mt);.    if( zSt
359c0 6d 74 20 21 3d 20 66 75 6c 6c 74 65 78 74 5f 7a  mt != fulltext_z
359c1 53 74 61 74 65 6d 65 6e 74 5b 69 53 74 6d 74 5d  Statement[iStmt]
359c2 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 28  ) sqlite3_free((
359c3 76 6f 69 64 20 2a 29 20 7a 53 74 6d 74 29 3b 0a  void *) zStmt);.
359c4 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
359c5 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
359c6 63 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  c;.  } else {.  
359c7 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74    int rc = sqlit
359c8 65 33 5f 72 65 73 65 74 28 76 2d 3e 70 46 75 6c  e3_reset(v->pFul
359c9 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73 5b  ltextStatements[
359ca 69 53 74 6d 74 5d 29 3b 0a 20 20 20 20 69 66 28  iStmt]);.    if(
359cb 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
359cc 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
359cd 0a 20 20 2a 70 70 53 74 6d 74 20 3d 20 76 2d 3e  .  *ppStmt = v->
359ce 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d 65  pFulltextStateme
359cf 6e 74 73 5b 69 53 74 6d 74 5d 3b 0a 20 20 72 65  nts[iStmt];.  re
359d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
359d1 7d 0a 0a 2f 2a 20 4c 69 6b 65 20 73 71 6c 69 74  }../* Like sqlit
359d2 65 33 5f 73 74 65 70 28 29 2c 20 62 75 74 20 63  e3_step(), but c
359d3 6f 6e 76 65 72 74 20 53 51 4c 49 54 45 5f 44 4f  onvert SQLITE_DO
359d4 4e 45 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20  NE to SQLITE_OK 
359d5 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 52 4f  and.** SQLITE_RO
359d6 57 20 74 6f 20 53 51 4c 49 54 45 5f 45 52 52 4f  W to SQLITE_ERRO
359d7 52 2e 20 20 55 73 65 66 75 6c 20 66 6f 72 20 73  R.  Useful for s
359d8 74 61 74 65 6d 65 6e 74 73 20 6c 69 6b 65 20 55  tatements like U
359d9 50 44 41 54 45 2c 0a 2a 2a 20 77 68 65 72 65 20  PDATE,.** where 
359da 77 65 20 65 78 70 65 63 74 20 6e 6f 20 72 65 73  we expect no res
359db 75 6c 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ults..*/.static 
359dc 69 6e 74 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73  int sql_single_s
359dd 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  tep(sqlite3_stmt
359de 20 2a 73 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d   *s){.  int rc =
359df 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
359e0 3b 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d  ;.  return (rc==
359e1 53 51 4c 49 54 45 5f 44 4f 4e 45 29 20 3f 20 53  SQLITE_DONE) ? S
359e2 51 4c 49 54 45 5f 4f 4b 20 3a 20 72 63 3b 0a 7d  QLITE_OK : rc;.}
359e3 0a 0a 2f 2a 20 4c 69 6b 65 20 73 71 6c 5f 67 65  ../* Like sql_ge
359e4 74 5f 73 74 61 74 65 6d 65 6e 74 28 29 2c 20 62  t_statement(), b
359e5 75 74 20 66 6f 72 20 73 70 65 63 69 61 6c 20 72  ut for special r
359e6 65 70 6c 69 63 61 74 65 64 20 4c 45 41 46 5f 53  eplicated LEAF_S
359e7 45 4c 45 43 54 0a 2a 2a 20 73 74 61 74 65 6d 65  ELECT.** stateme
359e8 6e 74 73 2e 20 20 69 64 78 20 2d 31 20 69 73 20  nts.  idx -1 is 
359e9 61 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 66  a special case f
359ea 6f 72 20 61 6e 20 75 6e 63 61 63 68 65 64 20 76  or an uncached v
359eb 65 72 73 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65  ersion of.** the
359ec 20 73 74 61 74 65 6d 65 6e 74 20 28 75 73 65 64   statement (used
359ed 20 69 6e 20 74 68 65 20 6f 70 74 69 6d 69 7a 65   in the optimize
359ee 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29   implementation)
359ef 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ..*/./* TODO(she
359f0 73 73 29 20 57 72 69 74 65 20 76 65 72 73 69 6f  ss) Write versio
359f1 6e 20 66 6f 72 20 67 65 6e 65 72 69 63 20 73 74  n for generic st
359f2 61 74 65 6d 65 6e 74 73 20 61 6e 64 20 74 68 65  atements and the
359f3 6e 20 73 68 61 72 65 0a 2a 2a 20 74 68 61 74 20  n share.** that 
359f4 62 65 74 77 65 65 6e 20 74 68 65 20 63 61 63 68  between the cach
359f5 65 64 2d 73 74 61 74 65 6d 65 6e 74 20 66 75 6e  ed-statement fun
359f6 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69  ctions..*/.stati
359f7 63 20 69 6e 74 20 73 71 6c 5f 67 65 74 5f 6c 65  c int sql_get_le
359f8 61 66 5f 73 74 61 74 65 6d 65 6e 74 28 66 75 6c  af_statement(ful
359f9 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69  ltext_vtab *v, i
359fa 6e 74 20 69 64 78 2c 0a 20 20 20 20 20 20 20 20  nt idx,.        
359fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
359fc 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
359fd 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 29  3_stmt **ppStmt)
359fe 7b 0a 20 20 61 73 73 65 72 74 28 20 69 64 78 3e  {.  assert( idx>
359ff 3d 2d 31 20 26 26 20 69 64 78 3c 4d 45 52 47 45  =-1 && idx<MERGE
35a00 5f 43 4f 55 4e 54 20 29 3b 0a 20 20 69 66 28 20  _COUNT );.  if( 
35a01 69 64 78 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 72  idx==-1 ){.    r
35a02 65 74 75 72 6e 20 73 71 6c 5f 70 72 65 70 61 72  eturn sql_prepar
35a03 65 28 76 2d 3e 64 62 2c 20 76 2d 3e 7a 44 62 2c  e(v->db, v->zDb,
35a04 20 76 2d 3e 7a 4e 61 6d 65 2c 20 70 70 53 74 6d   v->zName, ppStm
35a05 74 2c 20 4c 45 41 46 5f 53 45 4c 45 43 54 29 3b  t, LEAF_SELECT);
35a06 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 76 2d 3e  .  }else if( v->
35a07 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73  pLeafSelectStmts
35a08 5b 69 64 78 5d 3d 3d 4e 55 4c 4c 20 29 7b 0a 20  [idx]==NULL ){. 
35a09 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f     int rc = sql_
35a0a 70 72 65 70 61 72 65 28 76 2d 3e 64 62 2c 20 76  prepare(v->db, v
35a0b 2d 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c  ->zDb, v->zName,
35a0c 20 26 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74   &v->pLeafSelect
35a0d 53 74 6d 74 73 5b 69 64 78 5d 2c 0a 20 20 20 20  Stmts[idx],.    
35a0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35a0f 20 20 20 20 20 4c 45 41 46 5f 53 45 4c 45 43 54       LEAF_SELECT
35a10 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
35a11 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
35a12 6e 20 72 63 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  n rc;.  }else{. 
35a13 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69     int rc = sqli
35a14 74 65 33 5f 72 65 73 65 74 28 76 2d 3e 70 4c 65  te3_reset(v->pLe
35a15 61 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 69 64  afSelectStmts[id
35a16 78 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  x]);.    if( rc!
35a17 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
35a18 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a  urn rc;.  }..  *
35a19 70 70 53 74 6d 74 20 3d 20 76 2d 3e 70 4c 65 61  ppStmt = v->pLea
35a1a 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 69 64 78  fSelectStmts[idx
35a1b 5d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ];.  return SQLI
35a1c 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 69 6e 73  TE_OK;.}../* ins
35a1d 65 72 74 20 69 6e 74 6f 20 25 5f 63 6f 6e 74 65  ert into %_conte
35a1e 6e 74 20 28 64 6f 63 69 64 2c 20 2e 2e 2e 29 20  nt (docid, ...) 
35a1f 76 61 6c 75 65 73 20 28 5b 64 6f 63 69 64 5d 2c  values ([docid],
35a20 20 5b 70 56 61 6c 75 65 73 5d 29 0a 2a 2a 20 49   [pValues]).** I
35a21 66 20 74 68 65 20 64 6f 63 69 64 20 63 6f 6e 74  f the docid cont
35a22 61 69 6e 73 20 53 51 4c 20 4e 55 4c 4c 2c 20 74  ains SQL NULL, t
35a23 68 65 6e 20 61 20 75 6e 69 71 75 65 20 64 6f 63  hen a unique doc
35a24 69 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 67 65  id will be.** ge
35a25 6e 65 72 61 74 65 64 2e 0a 2a 2f 0a 73 74 61 74  nerated..*/.stat
35a26 69 63 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 69  ic int content_i
35a27 6e 73 65 72 74 28 66 75 6c 6c 74 65 78 74 5f 76  nsert(fulltext_v
35a28 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 33 5f  tab *v, sqlite3_
35a29 76 61 6c 75 65 20 2a 64 6f 63 69 64 2c 0a 20 20  value *docid,.  
35a2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35a2b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
35a2c 76 61 6c 75 65 20 2a 2a 70 56 61 6c 75 65 73 29  value **pValues)
35a2d 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  {.  sqlite3_stmt
35a2e 20 2a 73 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20   *s;.  int i;.  
35a2f 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74  int rc = sql_get
35a30 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f  _statement(v, CO
35a31 4e 54 45 4e 54 5f 49 4e 53 45 52 54 5f 53 54 4d  NTENT_INSERT_STM
35a32 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63  T, &s);.  if( rc
35a33 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
35a34 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
35a35 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61   sqlite3_bind_va
35a36 6c 75 65 28 73 2c 20 31 2c 20 64 6f 63 69 64 29  lue(s, 1, docid)
35a37 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
35a38 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
35a39 63 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  c;..  for(i=0; i
35a3a 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69  <v->nColumn; ++i
35a3b 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
35a3c 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 73  te3_bind_value(s
35a3d 2c 20 32 2b 69 2c 20 70 56 61 6c 75 65 73 5b 69  , 2+i, pValues[i
35a3e 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  ]);.    if( rc!=
35a3f 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
35a40 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 65  rn rc;.  }..  re
35a41 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65 5f  turn sql_single_
35a42 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 75  step(s);.}../* u
35a43 70 64 61 74 65 20 25 5f 63 6f 6e 74 65 6e 74 20  pdate %_content 
35a44 73 65 74 20 63 6f 6c 30 20 3d 20 70 56 61 6c 75  set col0 = pValu
35a45 65 73 5b 30 5d 2c 20 63 6f 6c 31 20 3d 20 70 56  es[0], col1 = pV
35a46 61 6c 75 65 73 5b 31 5d 2c 20 2e 2e 2e 0a 20 2a  alues[1], .... *
35a47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35a48 20 20 77 68 65 72 65 20 64 6f 63 69 64 20 3d 20    where docid = 
35a49 5b 69 44 6f 63 69 64 5d 20 2a 2f 0a 73 74 61 74  [iDocid] */.stat
35a4a 69 63 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 75  ic int content_u
35a4b 70 64 61 74 65 28 66 75 6c 6c 74 65 78 74 5f 76  pdate(fulltext_v
35a4c 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 33 5f  tab *v, sqlite3_
35a4d 76 61 6c 75 65 20 2a 2a 70 56 61 6c 75 65 73 2c  value **pValues,
35a4e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
35a4f 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
35a50 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 29 7b  e_int64 iDocid){
35a51 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
35a52 2a 73 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  *s;.  int i;.  i
35a53 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  nt rc = sql_get_
35a54 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f 4e  statement(v, CON
35a55 54 45 4e 54 5f 55 50 44 41 54 45 5f 53 54 4d 54  TENT_UPDATE_STMT
35a56 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
35a57 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
35a58 75 72 6e 20 72 63 3b 0a 0a 20 20 66 6f 72 28 69  urn rc;..  for(i
35a59 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e  =0; i<v->nColumn
35a5a 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 72 63 20 3d  ; ++i){.    rc =
35a5b 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61   sqlite3_bind_va
35a5c 6c 75 65 28 73 2c 20 31 2b 69 2c 20 70 56 61 6c  lue(s, 1+i, pVal
35a5d 75 65 73 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28  ues[i]);.    if(
35a5e 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
35a5f 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
35a60 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
35a61 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31 2b  bind_int64(s, 1+
35a62 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 69 44 6f 63  v->nColumn, iDoc
35a63 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  id);.  if( rc!=S
35a64 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
35a65 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20  n rc;..  return 
35a66 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28  sql_single_step(
35a67 73 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  s);.}..static vo
35a68 69 64 20 66 72 65 65 53 74 72 69 6e 67 41 72 72  id freeStringArr
35a69 61 79 28 69 6e 74 20 6e 53 74 72 69 6e 67 2c 20  ay(int nString, 
35a6a 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 53 74  const char **pSt
35a6b 72 69 6e 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  ring){.  int i;.
35a6c 0a 20 20 66 6f 72 20 28 69 3d 30 20 3b 20 69 20  .  for (i=0 ; i 
35a6d 3c 20 6e 53 74 72 69 6e 67 20 3b 20 2b 2b 69 29  < nString ; ++i)
35a6e 20 7b 0a 20 20 20 20 69 66 28 20 70 53 74 72 69   {.    if( pStri
35a6f 6e 67 5b 69 5d 21 3d 4e 55 4c 4c 20 29 20 73 71  ng[i]!=NULL ) sq
35a70 6c 69 74 65 33 5f 66 72 65 65 28 28 76 6f 69 64  lite3_free((void
35a71 20 2a 29 20 70 53 74 72 69 6e 67 5b 69 5d 29 3b   *) pString[i]);
35a72 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66  .  }.  sqlite3_f
35a73 72 65 65 28 28 76 6f 69 64 20 2a 29 20 70 53 74  ree((void *) pSt
35a74 72 69 6e 67 29 3b 0a 7d 0a 0a 2f 2a 20 73 65 6c  ring);.}../* sel
35a75 65 63 74 20 2a 20 66 72 6f 6d 20 25 5f 63 6f 6e  ect * from %_con
35a76 74 65 6e 74 20 77 68 65 72 65 20 64 6f 63 69 64  tent where docid
35a77 20 3d 20 5b 69 44 6f 63 69 64 5d 0a 20 2a 20 54   = [iDocid]. * T
35a78 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 64  he caller must d
35a79 65 6c 65 74 65 20 74 68 65 20 72 65 74 75 72 6e  elete the return
35a7a 65 64 20 61 72 72 61 79 20 61 6e 64 20 61 6c 6c  ed array and all
35a7b 20 73 74 72 69 6e 67 73 20 69 6e 20 69 74 2e 0a   strings in it..
35a7c 20 2a 20 6e 75 6c 6c 20 66 69 65 6c 64 73 20 77   * null fields w
35a7d 69 6c 6c 20 62 65 20 4e 55 4c 4c 20 69 6e 20 74  ill be NULL in t
35a7e 68 65 20 72 65 74 75 72 6e 65 64 20 61 72 72 61  he returned arra
35a7f 79 2e 0a 20 2a 0a 20 2a 20 54 4f 44 4f 3a 20 50  y.. *. * TODO: P
35a80 65 72 68 61 70 73 20 77 65 20 73 68 6f 75 6c 64  erhaps we should
35a81 20 72 65 74 75 72 6e 20 70 6f 69 6e 74 65 72 2f   return pointer/
35a82 6c 65 6e 67 74 68 20 73 74 72 69 6e 67 73 20 68  length strings h
35a83 65 72 65 20 66 6f 72 20 63 6f 6e 73 69 73 74 65  ere for consiste
35a84 6e 63 79 0a 20 2a 20 77 69 74 68 20 6f 74 68 65  ncy. * with othe
35a85 72 20 63 6f 64 65 20 77 68 69 63 68 20 75 73 65  r code which use
35a86 73 20 70 6f 69 6e 74 65 72 2f 6c 65 6e 67 74 68  s pointer/length
35a87 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  . */.static int 
35a88 63 6f 6e 74 65 6e 74 5f 73 65 6c 65 63 74 28 66  content_select(f
35a89 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
35a8a 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44   sqlite_int64 iD
35a8b 6f 63 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20  ocid,.          
35a8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35a8d 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 2a 70 56  const char ***pV
35a8e 61 6c 75 65 73 29 7b 0a 20 20 73 71 6c 69 74 65  alues){.  sqlite
35a8f 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 63 6f 6e  3_stmt *s;.  con
35a90 73 74 20 63 68 61 72 20 2a 2a 76 61 6c 75 65 73  st char **values
35a91 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  ;.  int i;.  int
35a92 20 72 63 3b 0a 0a 20 20 2a 70 56 61 6c 75 65 73   rc;..  *pValues
35a93 20 3d 20 4e 55 4c 4c 3b 0a 0a 20 20 72 63 20 3d   = NULL;..  rc =
35a94 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65   sql_get_stateme
35a95 6e 74 28 76 2c 20 43 4f 4e 54 45 4e 54 5f 53 45  nt(v, CONTENT_SE
35a96 4c 45 43 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a  LECT_STMT, &s);.
35a97 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
35a98 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
35a99 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
35a9a 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31  _bind_int64(s, 1
35a9b 2c 20 69 44 6f 63 69 64 29 3b 0a 20 20 69 66 28  , iDocid);.  if(
35a9c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
35a9d 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
35a9e 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
35a9f 28 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  (s);.  if( rc!=S
35aa0 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75  QLITE_ROW ) retu
35aa1 72 6e 20 72 63 3b 0a 0a 20 20 76 61 6c 75 65 73  rn rc;..  values
35aa2 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
35aa3 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  *) sqlite3_mallo
35aa4 63 28 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 2a 20 73  c(v->nColumn * s
35aa5 69 7a 65 6f 66 28 63 6f 6e 73 74 20 63 68 61 72  izeof(const char
35aa6 20 2a 29 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b   *));.  for(i=0;
35aa7 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b   i<v->nColumn; +
35aa8 2b 69 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  +i){.    if( sql
35aa9 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65  ite3_column_type
35aaa 28 73 2c 20 69 29 3d 3d 53 51 4c 49 54 45 5f 4e  (s, i)==SQLITE_N
35aab 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 76 61 6c  ULL ){.      val
35aac 75 65 73 5b 69 5d 20 3d 20 4e 55 4c 4c 3b 0a 20  ues[i] = NULL;. 
35aad 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
35aae 76 61 6c 75 65 73 5b 69 5d 20 3d 20 73 74 72 69  values[i] = stri
35aaf 6e 67 5f 64 75 70 28 28 63 68 61 72 2a 29 73 71  ng_dup((char*)sq
35ab0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
35ab1 74 28 73 2c 20 69 29 29 3b 0a 20 20 20 20 7d 0a  t(s, i));.    }.
35ab2 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70    }..  /* We exp
35ab3 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77  ect only one row
35ab4 2e 20 20 57 65 20 6d 75 73 74 20 65 78 65 63 75  .  We must execu
35ab5 74 65 20 61 6e 6f 74 68 65 72 20 73 71 6c 69 74  te another sqlit
35ab6 65 33 5f 73 74 65 70 28 29 0a 20 20 20 2a 20 74  e3_step().   * t
35ab7 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 69  o complete the i
35ab8 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77  teration; otherw
35ab9 69 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69  ise the table wi
35aba 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b 65 64  ll remain locked
35abb 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69  . */.  rc = sqli
35abc 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69  te3_step(s);.  i
35abd 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
35abe 4e 45 20 29 7b 0a 20 20 20 20 2a 70 56 61 6c 75  NE ){.    *pValu
35abf 65 73 20 3d 20 76 61 6c 75 65 73 3b 0a 20 20 20  es = values;.   
35ac0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
35ac1 4b 3b 0a 20 20 7d 0a 0a 20 20 66 72 65 65 53 74  K;.  }..  freeSt
35ac2 72 69 6e 67 41 72 72 61 79 28 76 2d 3e 6e 43 6f  ringArray(v->nCo
35ac3 6c 75 6d 6e 2c 20 76 61 6c 75 65 73 29 3b 0a 20  lumn, values);. 
35ac4 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
35ac5 2a 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f  * delete from %_
35ac6 63 6f 6e 74 65 6e 74 20 77 68 65 72 65 20 64 6f  content where do
35ac7 63 69 64 20 3d 20 5b 69 44 6f 63 69 64 20 5d 20  cid = [iDocid ] 
35ac8 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  */.static int co
35ac9 6e 74 65 6e 74 5f 64 65 6c 65 74 65 28 66 75 6c  ntent_delete(ful
35aca 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73  ltext_vtab *v, s
35acb 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63  qlite_int64 iDoc
35acc 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73  id){.  sqlite3_s
35acd 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63  tmt *s;.  int rc
35ace 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65   = sql_get_state
35acf 6d 65 6e 74 28 76 2c 20 43 4f 4e 54 45 4e 54 5f  ment(v, CONTENT_
35ad0 44 45 4c 45 54 45 5f 53 54 4d 54 2c 20 26 73 29  DELETE_STMT, &s)
35ad1 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
35ad2 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
35ad3 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
35ad4 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c  e3_bind_int64(s,
35ad5 20 31 2c 20 69 44 6f 63 69 64 29 3b 0a 20 20 69   1, iDocid);.  i
35ad6 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
35ad7 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
35ad8 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67   return sql_sing
35ad9 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f  le_step(s);.}../
35ada 2a 20 52 65 74 75 72 6e 73 20 53 51 4c 49 54 45  * Returns SQLITE
35adb 5f 52 4f 57 20 69 66 20 61 6e 79 20 72 6f 77 73  _ROW if any rows
35adc 20 65 78 69 73 74 20 69 6e 20 25 5f 63 6f 6e 74   exist in %_cont
35add 65 6e 74 2c 20 53 51 4c 49 54 45 5f 44 4f 4e 45  ent, SQLITE_DONE
35ade 20 69 66 0a 2a 2a 20 6e 6f 20 72 6f 77 73 20 65   if.** no rows e
35adf 78 69 73 74 2c 20 61 6e 64 20 61 6e 79 20 65 72  xist, and any er
35ae0 72 6f 72 20 69 6e 20 63 61 73 65 20 6f 66 20 66  ror in case of f
35ae1 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69  ailure..*/.stati
35ae2 63 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 65 78  c int content_ex
35ae3 69 73 74 73 28 66 75 6c 6c 74 65 78 74 5f 76 74  ists(fulltext_vt
35ae4 61 62 20 2a 76 29 7b 0a 20 20 73 71 6c 69 74 65  ab *v){.  sqlite
35ae5 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74  3_stmt *s;.  int
35ae6 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
35ae7 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f 4e 54 45  atement(v, CONTE
35ae8 4e 54 5f 45 58 49 53 54 53 5f 53 54 4d 54 2c 20  NT_EXISTS_STMT, 
35ae9 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
35aea 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
35aeb 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
35aec 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20  lite3_step(s);. 
35aed 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35aee 52 4f 57 20 29 20 72 65 74 75 72 6e 20 72 63 3b  ROW ) return rc;
35aef 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70 65 63 74  ..  /* We expect
35af0 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e 20 20   only one row.  
35af1 57 65 20 6d 75 73 74 20 65 78 65 63 75 74 65 20  We must execute 
35af2 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65 33 5f  another sqlite3_
35af3 73 74 65 70 28 29 0a 20 20 20 2a 20 74 6f 20 63  step().   * to c
35af4 6f 6d 70 6c 65 74 65 20 74 68 65 20 69 74 65 72  omplete the iter
35af5 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69 73 65  ation; otherwise
35af6 20 74 68 65 20 74 61 62 6c 65 20 77 69 6c 6c 20   the table will 
35af7 72 65 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e 20 2a  remain locked. *
35af8 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  /.  rc = sqlite3
35af9 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20  _step(s);.  if( 
35afa 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
35afb 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
35afc 52 4f 57 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  ROW;.  if( rc==S
35afd 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75  QLITE_ROW ) retu
35afe 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
35aff 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
35b00 0a 2f 2a 20 69 6e 73 65 72 74 20 69 6e 74 6f 20  ./* insert into 
35b01 25 5f 73 65 67 6d 65 6e 74 73 20 76 61 6c 75 65  %_segments value
35b02 73 20 28 5b 70 44 61 74 61 5d 29 0a 2a 2a 20 20  s ([pData]).**  
35b03 20 72 65 74 75 72 6e 73 20 61 73 73 69 67 6e 65   returns assigne
35b04 64 20 62 6c 6f 63 6b 69 64 20 69 6e 20 2a 70 69  d blockid in *pi
35b05 42 6c 6f 63 6b 69 64 0a 2a 2f 0a 73 74 61 74 69  Blockid.*/.stati
35b06 63 20 69 6e 74 20 62 6c 6f 63 6b 5f 69 6e 73 65  c int block_inse
35b07 72 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  rt(fulltext_vtab
35b08 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   *v, const char 
35b09 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74  *pData, int nDat
35b0a 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
35b0b 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
35b0c 65 5f 69 6e 74 36 34 20 2a 70 69 42 6c 6f 63 6b  e_int64 *piBlock
35b0d 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73  id){.  sqlite3_s
35b0e 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63  tmt *s;.  int rc
35b0f 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65   = sql_get_state
35b10 6d 65 6e 74 28 76 2c 20 42 4c 4f 43 4b 5f 49 4e  ment(v, BLOCK_IN
35b11 53 45 52 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a  SERT_STMT, &s);.
35b12 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
35b13 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
35b14 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
35b15 5f 62 69 6e 64 5f 62 6c 6f 62 28 73 2c 20 31 2c  _bind_blob(s, 1,
35b16 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 53   pData, nData, S
35b17 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
35b18 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35b19 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
35b1a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
35b1b 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20 72  step(s);.  if( r
35b1c 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20  c==SQLITE_ROW ) 
35b1d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
35b1e 52 4f 52 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  ROR;.  if( rc!=S
35b1f 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65 74  QLITE_DONE ) ret
35b20 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 62 6c  urn rc;..  /* bl
35b21 6f 63 6b 69 64 20 63 6f 6c 75 6d 6e 20 69 73 20  ockid column is 
35b22 61 6e 20 61 6c 69 61 73 20 66 6f 72 20 72 6f 77  an alias for row
35b23 69 64 2e 20 2a 2f 0a 20 20 2a 70 69 42 6c 6f 63  id. */.  *piBloc
35b24 6b 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61  kid = sqlite3_la
35b25 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
35b26 76 2d 3e 64 62 29 3b 0a 20 20 72 65 74 75 72 6e  v->db);.  return
35b27 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
35b28 2a 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f  * delete from %_
35b29 73 65 67 6d 65 6e 74 73 0a 2a 2a 20 20 20 77 68  segments.**   wh
35b2a 65 72 65 20 62 6c 6f 63 6b 69 64 20 62 65 74 77  ere blockid betw
35b2b 65 65 6e 20 5b 69 53 74 61 72 74 42 6c 6f 63 6b  een [iStartBlock
35b2c 69 64 5d 20 61 6e 64 20 5b 69 45 6e 64 42 6c 6f  id] and [iEndBlo
35b2d 63 6b 69 64 5d 0a 2a 2a 0a 2a 2a 20 44 65 6c 65  ckid].**.** Dele
35b2e 74 65 73 20 74 68 65 20 72 61 6e 67 65 20 6f 66  tes the range of
35b2f 20 62 6c 6f 63 6b 73 2c 20 69 6e 63 6c 75 73 69   blocks, inclusi
35b30 76 65 2c 20 75 73 65 64 20 74 6f 20 64 65 6c 65  ve, used to dele
35b31 74 65 20 74 68 65 20 62 6c 6f 63 6b 73 0a 2a 2a  te the blocks.**
35b32 20 77 68 69 63 68 20 66 6f 72 6d 20 61 20 73 65   which form a se
35b33 67 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  gment..*/.static
35b34 20 69 6e 74 20 62 6c 6f 63 6b 5f 64 65 6c 65 74   int block_delet
35b35 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  e(fulltext_vtab 
35b36 2a 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  *v,.            
35b37 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
35b38 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42  te_int64 iStartB
35b39 6c 6f 63 6b 69 64 2c 20 73 71 6c 69 74 65 5f 69  lockid, sqlite_i
35b3a 6e 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64  nt64 iEndBlockid
35b3b 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
35b3c 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d  t *s;.  int rc =
35b3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65   sql_get_stateme
35b3e 6e 74 28 76 2c 20 42 4c 4f 43 4b 5f 44 45 4c 45  nt(v, BLOCK_DELE
35b3f 54 45 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20  TE_STMT, &s);.  
35b40 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
35b41 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
35b42 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
35b43 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31 2c 20  ind_int64(s, 1, 
35b44 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 29 3b 0a  iStartBlockid);.
35b45 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
35b46 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
35b47 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
35b48 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 32  _bind_int64(s, 2
35b49 2c 20 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a  , iEndBlockid);.
35b4a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
35b4b 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
35b4c 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73  ..  return sql_s
35b4d 69 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d  ingle_step(s);.}
35b4e 0a 0a 2f 2a 20 52 65 74 75 72 6e 73 20 53 51 4c  ../* Returns SQL
35b4f 49 54 45 5f 52 4f 57 20 77 69 74 68 20 2a 70 69  ITE_ROW with *pi
35b50 64 78 20 73 65 74 20 74 6f 20 74 68 65 20 6d 61  dx set to the ma
35b51 78 69 6d 75 6d 20 73 65 67 6d 65 6e 74 20 69 64  ximum segment id
35b52 78 20 66 6f 75 6e 64 0a 2a 2a 20 61 74 20 69 4c  x found.** at iL
35b53 65 76 65 6c 2e 20 20 52 65 74 75 72 6e 73 20 53  evel.  Returns S
35b54 51 4c 49 54 45 5f 44 4f 4e 45 20 69 66 20 74 68  QLITE_DONE if th
35b55 65 72 65 20 61 72 65 20 6e 6f 20 73 65 67 6d 65  ere are no segme
35b56 6e 74 73 20 61 74 0a 2a 2a 20 69 4c 65 76 65 6c  nts at.** iLevel
35b57 2e 20 20 4f 74 68 65 72 77 69 73 65 20 72 65 74  .  Otherwise ret
35b58 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2e 0a 2a  urns an error..*
35b59 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 67  /.static int seg
35b5a 64 69 72 5f 6d 61 78 5f 69 6e 64 65 78 28 66 75  dir_max_index(fu
35b5b 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
35b5c 69 6e 74 20 69 4c 65 76 65 6c 2c 20 69 6e 74 20  int iLevel, int 
35b5d 2a 70 69 64 78 29 7b 0a 20 20 73 71 6c 69 74 65  *pidx){.  sqlite
35b5e 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74  3_stmt *s;.  int
35b5f 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
35b60 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49  atement(v, SEGDI
35b61 52 5f 4d 41 58 5f 49 4e 44 45 58 5f 53 54 4d 54  R_MAX_INDEX_STMT
35b62 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
35b63 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
35b64 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
35b65 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
35b66 28 73 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a  (s, 1, iLevel);.
35b67 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
35b68 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
35b69 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
35b6a 5f 73 74 65 70 28 73 29 3b 0a 20 20 2f 2a 20 53  _step(s);.  /* S
35b6b 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 67 65 74  hould always get
35b6c 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 72 6f   at least one ro
35b6d 77 20 64 75 65 20 74 6f 20 68 6f 77 20 6d 61 78  w due to how max
35b6e 28 29 20 77 6f 72 6b 73 2e 20 2a 2f 0a 20 20 69  () works. */.  i
35b6f 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
35b70 4e 45 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  NE ) return SQLI
35b71 54 45 5f 44 4f 4e 45 3b 0a 20 20 69 66 28 20 72  TE_DONE;.  if( r
35b72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20  c!=SQLITE_ROW ) 
35b73 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a  return rc;..  /*
35b74 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 74 68 61 74   NULL means that
35b75 20 74 68 65 72 65 20 77 65 72 65 20 6e 6f 20 69   there were no i
35b76 6e 70 75 74 73 20 74 6f 20 6d 61 78 28 29 2e 20  nputs to max(). 
35b77 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  */.  if( SQLITE_
35b78 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 63 6f  NULL==sqlite3_co
35b79 6c 75 6d 6e 5f 74 79 70 65 28 73 2c 20 30 29 20  lumn_type(s, 0) 
35b7a 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
35b7b 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 20  te3_step(s);.   
35b7c 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
35b7d 52 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ROW ) return SQL
35b7e 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 72  ITE_ERROR;.    r
35b7f 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
35b80 20 2a 70 69 64 78 20 3d 20 73 71 6c 69 74 65 33   *pidx = sqlite3
35b81 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 2c 20 30  _column_int(s, 0
35b82 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70 65  );..  /* We expe
35b83 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e  ct only one row.
35b84 20 20 57 65 20 6d 75 73 74 20 65 78 65 63 75 74    We must execut
35b85 65 20 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65  e another sqlite
35b86 33 5f 73 74 65 70 28 29 0a 20 20 20 2a 20 74 6f  3_step().   * to
35b87 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 69 74   complete the it
35b88 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69  eration; otherwi
35b89 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 6c  se the table wil
35b8a 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e  l remain locked.
35b8b 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
35b8c 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66  e3_step(s);.  if
35b8d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc==SQLITE_ROW
35b8e 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
35b8f 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28 20 72 63  _ERROR;.  if( rc
35b90 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20  !=SQLITE_DONE ) 
35b91 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 65 74  return rc;.  ret
35b92 75 72 6e 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a  urn SQLITE_ROW;.
35b93 7d 0a 0a 2f 2a 20 69 6e 73 65 72 74 20 69 6e 74  }../* insert int
35b94 6f 20 25 5f 73 65 67 64 69 72 20 76 61 6c 75 65  o %_segdir value
35b95 73 20 28 0a 2a 2a 20 20 20 5b 69 4c 65 76 65 6c  s (.**   [iLevel
35b96 5d 2c 20 5b 69 64 78 5d 2c 0a 2a 2a 20 20 20 5b  ], [idx],.**   [
35b97 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 5d 2c 20  iStartBlockid], 
35b98 5b 69 4c 65 61 76 65 73 45 6e 64 42 6c 6f 63 6b  [iLeavesEndBlock
35b99 69 64 5d 2c 20 5b 69 45 6e 64 42 6c 6f 63 6b 69  id], [iEndBlocki
35b9a 64 5d 2c 0a 2a 2a 20 20 20 5b 70 52 6f 6f 74 44  d],.**   [pRootD
35b9b 61 74 61 5d 0a 2a 2a 20 29 0a 2a 2f 0a 73 74 61  ata].** ).*/.sta
35b9c 74 69 63 20 69 6e 74 20 73 65 67 64 69 72 5f 73  tic int segdir_s
35b9d 65 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  et(fulltext_vtab
35b9e 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 2c   *v, int iLevel,
35b9f 20 69 6e 74 20 69 64 78 2c 0a 20 20 20 20 20 20   int idx,.      
35ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35ba1 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74  sqlite_int64 iSt
35ba2 61 72 74 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20  artBlockid,.    
35ba3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35ba4 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
35ba5 4c 65 61 76 65 73 45 6e 64 42 6c 6f 63 6b 69 64  LeavesEndBlockid
35ba6 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
35ba7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69          sqlite_i
35ba8 6e 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64  nt64 iEndBlockid
35ba9 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
35baa 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
35bab 61 72 20 2a 70 52 6f 6f 74 44 61 74 61 2c 20 69  ar *pRootData, i
35bac 6e 74 20 6e 52 6f 6f 74 44 61 74 61 29 7b 0a 20  nt nRootData){. 
35bad 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73   sqlite3_stmt *s
35bae 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c  ;.  int rc = sql
35baf 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
35bb0 2c 20 53 45 47 44 49 52 5f 53 45 54 5f 53 54 4d  , SEGDIR_SET_STM
35bb1 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63  T, &s);.  if( rc
35bb2 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
35bb3 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
35bb4 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
35bb5 74 28 73 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b  t(s, 1, iLevel);
35bb6 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
35bb7 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
35bb8 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
35bb9 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 32 2c  3_bind_int(s, 2,
35bba 20 69 64 78 29 3b 0a 20 20 69 66 28 20 72 63 21   idx);.  if( rc!
35bbb 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
35bbc 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
35bbd 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
35bbe 36 34 28 73 2c 20 33 2c 20 69 53 74 61 72 74 42  64(s, 3, iStartB
35bbf 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72  lockid);.  if( r
35bc0 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
35bc1 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
35bc2 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  = sqlite3_bind_i
35bc3 6e 74 36 34 28 73 2c 20 34 2c 20 69 4c 65 61 76  nt64(s, 4, iLeav
35bc4 65 73 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20  esEndBlockid);. 
35bc5 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35bc6 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
35bc7 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
35bc8 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 35 2c  bind_int64(s, 5,
35bc9 20 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20   iEndBlockid);. 
35bca 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35bcb 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
35bcc 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
35bcd 62 69 6e 64 5f 62 6c 6f 62 28 73 2c 20 36 2c 20  bind_blob(s, 6, 
35bce 70 52 6f 6f 74 44 61 74 61 2c 20 6e 52 6f 6f 74  pRootData, nRoot
35bcf 44 61 74 61 2c 20 53 51 4c 49 54 45 5f 53 54 41  Data, SQLITE_STA
35bd0 54 49 43 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  TIC);.  if( rc!=
35bd1 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
35bd2 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e  rn rc;..  return
35bd3 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70   sql_single_step
35bd4 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 51 75 65 72 69  (s);.}../* Queri
35bd5 65 73 20 25 5f 73 65 67 64 69 72 20 66 6f 72 20  es %_segdir for 
35bd6 74 68 65 20 62 6c 6f 63 6b 20 73 70 61 6e 20 6f  the block span o
35bd7 66 20 74 68 65 20 73 65 67 6d 65 6e 74 73 20 69  f the segments i
35bd8 6e 20 6c 65 76 65 6c 0a 2a 2a 20 69 4c 65 76 65  n level.** iLeve
35bd9 6c 2e 20 20 52 65 74 75 72 6e 73 20 53 51 4c 49  l.  Returns SQLI
35bda 54 45 5f 44 4f 4e 45 20 69 66 20 74 68 65 72 65  TE_DONE if there
35bdb 20 61 72 65 20 6e 6f 20 62 6c 6f 63 6b 73 20 66   are no blocks f
35bdc 6f 72 20 69 4c 65 76 65 6c 2c 0a 2a 2a 20 53 51  or iLevel,.** SQ
35bdd 4c 49 54 45 5f 52 4f 57 20 69 66 20 74 68 65 72  LITE_ROW if ther
35bde 65 20 61 72 65 20 62 6c 6f 63 6b 73 2c 20 65 6c  e are blocks, el
35bdf 73 65 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a  se an error..*/.
35be0 73 74 61 74 69 63 20 69 6e 74 20 73 65 67 64 69  static int segdi
35be1 72 5f 73 70 61 6e 28 66 75 6c 6c 74 65 78 74 5f  r_span(fulltext_
35be2 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65  vtab *v, int iLe
35be3 76 65 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20  vel,.           
35be4 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
35be5 74 65 5f 69 6e 74 36 34 20 2a 70 69 53 74 61 72  te_int64 *piStar
35be6 74 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20  tBlockid,.      
35be7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35be8 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
35be9 69 45 6e 64 42 6c 6f 63 6b 69 64 29 7b 0a 20 20  iEndBlockid){.  
35bea 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b  sqlite3_stmt *s;
35beb 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f  .  int rc = sql_
35bec 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c  get_statement(v,
35bed 20 53 45 47 44 49 52 5f 53 50 41 4e 5f 53 54 4d   SEGDIR_SPAN_STM
35bee 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63  T, &s);.  if( rc
35bef 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
35bf0 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
35bf1 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
35bf2 74 28 73 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b  t(s, 1, iLevel);
35bf3 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
35bf4 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
35bf5 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
35bf6 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28  3_step(s);.  if(
35bf7 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc==SQLITE_DONE
35bf8 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
35bf9 5f 44 4f 4e 45 3b 20 20 2f 2a 20 53 68 6f 75 6c  _DONE;  /* Shoul
35bfa 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e 20 2a  d never happen *
35bfb 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  /.  if( rc!=SQLI
35bfc 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20  TE_ROW ) return 
35bfd 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 68  rc;..  /* This h
35bfe 61 70 70 65 6e 73 20 69 66 20 61 6c 6c 20 73 65  appens if all se
35bff 67 6d 65 6e 74 73 20 61 74 20 74 68 69 73 20 6c  gments at this l
35c00 65 76 65 6c 20 61 72 65 20 65 6e 74 69 72 65 6c  evel are entirel
35c01 79 20 69 6e 6c 69 6e 65 2e 20 2a 2f 0a 20 20 69  y inline. */.  i
35c02 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d  f( SQLITE_NULL==
35c03 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
35c04 79 70 65 28 73 2c 20 30 29 20 29 7b 0a 20 20 20  ype(s, 0) ){.   
35c05 20 2f 2a 20 57 65 20 65 78 70 65 63 74 20 6f 6e   /* We expect on
35c06 6c 79 20 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20  ly one row.  We 
35c07 6d 75 73 74 20 65 78 65 63 75 74 65 20 61 6e 6f  must execute ano
35c08 74 68 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65  ther sqlite3_ste
35c09 70 28 29 0a 20 20 20 20 20 2a 20 74 6f 20 63 6f  p().     * to co
35c0a 6d 70 6c 65 74 65 20 74 68 65 20 69 74 65 72 61  mplete the itera
35c0b 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69 73 65 20  tion; otherwise 
35c0c 74 68 65 20 74 61 62 6c 65 20 77 69 6c 6c 20 72  the table will r
35c0d 65 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e 20 2a 2f  emain locked. */
35c0e 0a 20 20 20 20 69 6e 74 20 72 63 32 20 3d 20 73  .    int rc2 = s
35c0f 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a  qlite3_step(s);.
35c10 20 20 20 20 69 66 28 20 72 63 32 3d 3d 53 51 4c      if( rc2==SQL
35c11 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e  ITE_ROW ) return
35c12 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
35c13 20 20 20 72 65 74 75 72 6e 20 72 63 32 3b 0a 20     return rc2;. 
35c14 20 7d 0a 0a 20 20 2a 70 69 53 74 61 72 74 42 6c   }..  *piStartBl
35c15 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74 65 33 5f  ockid = sqlite3_
35c16 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20  column_int64(s, 
35c17 30 29 3b 0a 20 20 2a 70 69 45 6e 64 42 6c 6f 63  0);.  *piEndBloc
35c18 6b 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  kid = sqlite3_co
35c19 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29  lumn_int64(s, 1)
35c1a 3b 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70 65 63  ;..  /* We expec
35c1b 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e 20  t only one row. 
35c1c 20 57 65 20 6d 75 73 74 20 65 78 65 63 75 74 65   We must execute
35c1d 20 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65 33   another sqlite3
35c1e 5f 73 74 65 70 28 29 0a 20 20 20 2a 20 74 6f 20  _step().   * to 
35c1f 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 69 74 65  complete the ite
35c20 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69 73  ration; otherwis
35c21 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 6c 6c  e the table will
35c22 20 72 65 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e 20   remain locked. 
35c23 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
35c24 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28  3_step(s);.  if(
35c25 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20   rc==SQLITE_ROW 
35c26 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
35c27 45 52 52 4f 52 3b 0a 20 20 69 66 28 20 72 63 21  ERROR;.  if( rc!
35c28 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72  =SQLITE_DONE ) r
35c29 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 65 74 75  eturn rc;.  retu
35c2a 72 6e 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 7d  rn SQLITE_ROW;.}
35c2b 0a 0a 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20  ../* Delete the 
35c2c 73 65 67 6d 65 6e 74 20 62 6c 6f 63 6b 73 20 61  segment blocks a
35c2d 6e 64 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63  nd segment direc
35c2e 74 6f 72 79 20 72 65 63 6f 72 64 73 20 66 6f 72  tory records for
35c2f 20 61 6c 6c 0a 2a 2a 20 73 65 67 6d 65 6e 74 73   all.** segments
35c30 20 61 74 20 69 4c 65 76 65 6c 2e 0a 2a 2f 0a 73   at iLevel..*/.s
35c31 74 61 74 69 63 20 69 6e 74 20 73 65 67 64 69 72  tatic int segdir
35c32 5f 64 65 6c 65 74 65 28 66 75 6c 6c 74 65 78 74  _delete(fulltext
35c33 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c  _vtab *v, int iL
35c34 65 76 65 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33  evel){.  sqlite3
35c35 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 73 71 6c 69  _stmt *s;.  sqli
35c36 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42  te_int64 iStartB
35c37 6c 6f 63 6b 69 64 2c 20 69 45 6e 64 42 6c 6f 63  lockid, iEndBloc
35c38 6b 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  kid;.  int rc = 
35c39 73 65 67 64 69 72 5f 73 70 61 6e 28 76 2c 20 69  segdir_span(v, i
35c3a 4c 65 76 65 6c 2c 20 26 69 53 74 61 72 74 42 6c  Level, &iStartBl
35c3b 6f 63 6b 69 64 2c 20 26 69 45 6e 64 42 6c 6f 63  ockid, &iEndBloc
35c3c 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  kid);.  if( rc!=
35c3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 72 63  SQLITE_ROW && rc
35c3e 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20  !=SQLITE_DONE ) 
35c3f 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 69 66  return rc;..  if
35c40 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc==SQLITE_ROW
35c41 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 6c 6f   ){.    rc = blo
35c42 63 6b 5f 64 65 6c 65 74 65 28 76 2c 20 69 53 74  ck_delete(v, iSt
35c43 61 72 74 42 6c 6f 63 6b 69 64 2c 20 69 45 6e 64  artBlockid, iEnd
35c44 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 20 20 69 66  Blockid);.    if
35c45 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
35c46 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d  ) return rc;.  }
35c47 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68  ..  /* Delete th
35c48 65 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74  e segment direct
35c49 6f 72 79 20 69 74 73 65 6c 66 2e 20 2a 2f 0a 20  ory itself. */. 
35c4a 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
35c4b 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49  atement(v, SEGDI
35c4c 52 5f 44 45 4c 45 54 45 5f 53 54 4d 54 2c 20 26  R_DELETE_STMT, &
35c4d 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  s);.  if( rc!=SQ
35c4e 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
35c4f 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
35c50 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
35c51 73 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a 20  s, 1, iLevel);. 
35c52 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35c53 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
35c54 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69  .  return sql_si
35c55 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a  ngle_step(s);.}.
35c56 0a 2f 2a 20 44 65 6c 65 74 65 20 65 6e 74 69 72  ./* Delete entir
35c57 65 20 66 74 73 20 69 6e 64 65 78 2c 20 53 51 4c  e fts index, SQL
35c58 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73  ITE_OK on succes
35c59 73 2c 20 72 65 6c 65 76 61 6e 74 20 65 72 72 6f  s, relevant erro
35c5a 72 20 6f 6e 0a 2a 2a 20 66 61 69 6c 75 72 65 2e  r on.** failure.
35c5b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
35c5c 65 67 64 69 72 5f 64 65 6c 65 74 65 5f 61 6c 6c  egdir_delete_all
35c5d 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
35c5e 76 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  v){.  sqlite3_st
35c5f 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20  mt *s;.  int rc 
35c60 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d  = sql_get_statem
35c61 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 44 45  ent(v, SEGDIR_DE
35c62 4c 45 54 45 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26  LETE_ALL_STMT, &
35c63 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  s);.  if( rc!=SQ
35c64 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
35c65 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
35c66 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b  _single_step(s);
35c67 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
35c68 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
35c69 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 5f 67 65  ;..  rc = sql_ge
35c6a 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 42  t_statement(v, B
35c6b 4c 4f 43 4b 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f  LOCK_DELETE_ALL_
35c6c 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28  STMT, &s);.  if(
35c6d 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
35c6e 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
35c6f 65 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65  eturn sql_single
35c70 5f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20  _step(s);.}../* 
35c71 52 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f  Returns SQLITE_O
35c72 4b 20 77 69 74 68 20 2a 70 6e 53 65 67 6d 65 6e  K with *pnSegmen
35c73 74 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75  ts set to the nu
35c74 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
35c75 69 6e 0a 2a 2a 20 25 5f 73 65 67 64 69 72 20 61  in.** %_segdir a
35c76 6e 64 20 2a 70 69 4d 61 78 4c 65 76 65 6c 20 73  nd *piMaxLevel s
35c77 65 74 20 74 6f 20 74 68 65 20 68 69 67 68 65 73  et to the highes
35c78 74 20 6c 65 76 65 6c 20 77 68 69 63 68 20 68 61  t level which ha
35c79 73 20 61 0a 2a 2a 20 73 65 67 6d 65 6e 74 2e 20  s a.** segment. 
35c7a 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72   Otherwise retur
35c7b 6e 73 20 74 68 65 20 53 51 4c 69 74 65 20 65 72  ns the SQLite er
35c7c 72 6f 72 20 77 68 69 63 68 20 63 61 75 73 65 64  ror which caused
35c7d 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61   failure..*/.sta
35c7e 74 69 63 20 69 6e 74 20 73 65 67 64 69 72 5f 63  tic int segdir_c
35c7f 6f 75 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74  ount(fulltext_vt
35c80 61 62 20 2a 76 2c 20 69 6e 74 20 2a 70 6e 53 65  ab *v, int *pnSe
35c81 67 6d 65 6e 74 73 2c 20 69 6e 74 20 2a 70 69 4d  gments, int *piM
35c82 61 78 4c 65 76 65 6c 29 7b 0a 20 20 73 71 6c 69  axLevel){.  sqli
35c83 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69  te3_stmt *s;.  i
35c84 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  nt rc = sql_get_
35c85 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47  statement(v, SEG
35c86 44 49 52 5f 43 4f 55 4e 54 5f 53 54 4d 54 2c 20  DIR_COUNT_STMT, 
35c87 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
35c88 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
35c89 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
35c8a 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20  lite3_step(s);. 
35c8b 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a   /* TODO(shess):
35c8c 20 54 68 69 73 20 63 61 73 65 20 73 68 6f 75 6c   This case shoul
35c8d 64 20 6e 6f 74 20 62 65 20 70 6f 73 73 69 62 6c  d not be possibl
35c8e 65 3f 20 20 53 68 6f 75 6c 64 20 73 74 72 6f 6e  e?  Should stron
35c8f 67 65 72 0a 20 20 2a 2a 20 6d 65 61 73 75 72 65  ger.  ** measure
35c90 73 20 62 65 20 74 61 6b 65 6e 20 69 66 20 69 74  s be taken if it
35c91 20 68 61 70 70 65 6e 73 3f 0a 20 20 2a 2f 0a 20   happens?.  */. 
35c92 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
35c93 44 4f 4e 45 20 29 7b 0a 20 20 20 20 2a 70 6e 53  DONE ){.    *pnS
35c94 65 67 6d 65 6e 74 73 20 3d 20 30 3b 0a 20 20 20  egments = 0;.   
35c95 20 2a 70 69 4d 61 78 4c 65 76 65 6c 20 3d 20 30   *piMaxLevel = 0
35c96 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
35c97 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66  ITE_OK;.  }.  if
35c98 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc!=SQLITE_ROW
35c99 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
35c9a 20 2a 70 6e 53 65 67 6d 65 6e 74 73 20 3d 20 73   *pnSegments = s
35c9b 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
35c9c 74 28 73 2c 20 30 29 3b 0a 20 20 2a 70 69 4d 61  t(s, 0);.  *piMa
35c9d 78 4c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33  xLevel = sqlite3
35c9e 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 2c 20 31  _column_int(s, 1
35c9f 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70 65  );..  /* We expe
35ca0 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e  ct only one row.
35ca1 20 20 57 65 20 6d 75 73 74 20 65 78 65 63 75 74    We must execut
35ca2 65 20 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65  e another sqlite
35ca3 33 5f 73 74 65 70 28 29 0a 20 20 20 2a 20 74 6f  3_step().   * to
35ca4 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 69 74   complete the it
35ca5 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69  eration; otherwi
35ca6 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 6c  se the table wil
35ca7 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e  l remain locked.
35ca8 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
35ca9 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66  e3_step(s);.  if
35caa 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
35cab 45 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  E ) return SQLIT
35cac 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 72 63 3d 3d  E_OK;.  if( rc==
35cad 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74  SQLITE_ROW ) ret
35cae 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
35caf 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
35cb0 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  ../* TODO(shess)
35cb1 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72   clearPendingTer
35cb2 6d 73 28 29 20 69 73 20 66 61 72 20 64 6f 77 6e  ms() is far down
35cb3 20 74 68 65 20 66 69 6c 65 20 62 65 63 61 75 73   the file becaus
35cb4 65 0a 2a 2a 20 77 72 69 74 65 5a 65 72 6f 53 65  e.** writeZeroSe
35cb5 67 6d 65 6e 74 28 29 20 69 73 20 66 61 72 20 64  gment() is far d
35cb6 6f 77 6e 20 74 68 65 20 66 69 6c 65 20 62 65 63  own the file bec
35cb7 61 75 73 65 20 4c 65 61 66 57 72 69 74 65 72 20  ause LeafWriter 
35cb8 69 73 20 66 61 72 0a 2a 2a 20 64 6f 77 6e 20 74  is far.** down t
35cb9 68 65 20 66 69 6c 65 2e 20 20 43 6f 6e 73 69 64  he file.  Consid
35cba 65 72 20 72 65 66 61 63 74 6f 72 69 6e 67 20 74  er refactoring t
35cbb 68 65 20 63 6f 64 65 20 74 6f 20 6d 6f 76 65 20  he code to move 
35cbc 74 68 65 20 6e 6f 6e 2d 76 74 61 62 0a 2a 2a 20  the non-vtab.** 
35cbd 63 6f 64 65 20 61 62 6f 76 65 20 74 68 65 20 76  code above the v
35cbe 74 61 62 20 63 6f 64 65 20 73 6f 20 74 68 61 74  tab code so that
35cbf 20 77 65 20 64 6f 6e 27 74 20 6e 65 65 64 20 74   we don't need t
35cc0 68 69 73 20 66 6f 72 77 61 72 64 0a 2a 2a 20 72  his forward.** r
35cc1 65 66 65 72 65 6e 63 65 2e 0a 2a 2f 0a 73 74 61  eference..*/.sta
35cc2 74 69 63 20 69 6e 74 20 63 6c 65 61 72 50 65 6e  tic int clearPen
35cc3 64 69 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65  dingTerms(fullte
35cc4 78 74 5f 76 74 61 62 20 2a 76 29 3b 0a 0a 2f 2a  xt_vtab *v);../*
35cc5 0a 2a 2a 20 46 72 65 65 20 74 68 65 20 6d 65 6d  .** Free the mem
35cc6 6f 72 79 20 75 73 65 64 20 74 6f 20 63 6f 6e 74  ory used to cont
35cc7 61 69 6e 20 61 20 66 75 6c 6c 74 65 78 74 5f 76  ain a fulltext_v
35cc8 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  tab structure..*
35cc9 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 75  /.static void fu
35cca 6c 6c 74 65 78 74 5f 76 74 61 62 5f 64 65 73 74  lltext_vtab_dest
35ccb 72 6f 79 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  roy(fulltext_vta
35ccc 62 20 2a 76 29 7b 0a 20 20 69 6e 74 20 69 53 74  b *v){.  int iSt
35ccd 6d 74 2c 20 69 3b 0a 0a 20 20 46 54 53 54 52 41  mt, i;..  FTSTRA
35cce 43 45 28 28 22 46 54 53 33 20 44 65 73 74 72 6f  CE(("FTS3 Destro
35ccf 79 20 25 70 5c 6e 22 2c 20 76 29 29 3b 0a 20 20  y %p\n", v));.  
35cd0 66 6f 72 28 20 69 53 74 6d 74 3d 30 3b 20 69 53  for( iStmt=0; iS
35cd1 74 6d 74 3c 4d 41 58 5f 53 54 4d 54 3b 20 69 53  tmt<MAX_STMT; iS
35cd2 74 6d 74 2b 2b 20 29 7b 0a 20 20 20 20 69 66 28  tmt++ ){.    if(
35cd3 20 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74 61   v->pFulltextSta
35cd4 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 21 3d  tements[iStmt]!=
35cd5 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 73 71  NULL ){.      sq
35cd6 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 76  lite3_finalize(v
35cd7 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65  ->pFulltextState
35cd8 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 29 3b 0a 20  ments[iStmt]);. 
35cd9 20 20 20 20 20 76 2d 3e 70 46 75 6c 6c 74 65 78       v->pFulltex
35cda 74 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d  tStatements[iStm
35cdb 74 5d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d  t] = NULL;.    }
35cdc 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 20 69 3d 30  .  }..  for( i=0
35cdd 3b 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 3b  ; i<MERGE_COUNT;
35cde 20 69 2b 2b 20 29 7b 0a 20 20 20 20 69 66 28 20   i++ ){.    if( 
35cdf 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74 53 74  v->pLeafSelectSt
35ce0 6d 74 73 5b 69 5d 21 3d 4e 55 4c 4c 20 29 7b 0a  mts[i]!=NULL ){.
35ce1 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69        sqlite3_fi
35ce2 6e 61 6c 69 7a 65 28 76 2d 3e 70 4c 65 61 66 53  nalize(v->pLeafS
35ce3 65 6c 65 63 74 53 74 6d 74 73 5b 69 5d 29 3b 0a  electStmts[i]);.
35ce4 20 20 20 20 20 20 76 2d 3e 70 4c 65 61 66 53 65        v->pLeafSe
35ce5 6c 65 63 74 53 74 6d 74 73 5b 69 5d 20 3d 20 4e  lectStmts[i] = N
35ce6 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ULL;.    }.  }..
35ce7 20 20 69 66 28 20 76 2d 3e 70 54 6f 6b 65 6e 69    if( v->pTokeni
35ce8 7a 65 72 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20  zer!=NULL ){.   
35ce9 20 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e   v->pTokenizer->
35cea 70 4d 6f 64 75 6c 65 2d 3e 78 44 65 73 74 72 6f  pModule->xDestro
35ceb 79 28 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 29  y(v->pTokenizer)
35cec 3b 0a 20 20 20 20 76 2d 3e 70 54 6f 6b 65 6e 69  ;.    v->pTokeni
35ced 7a 65 72 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a  zer = NULL;.  }.
35cee 0a 20 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54  .  clearPendingT
35cef 65 72 6d 73 28 76 29 3b 0a 0a 20 20 73 71 6c 69  erms(v);..  sqli
35cf0 74 65 33 5f 66 72 65 65 28 76 2d 3e 61 7a 43 6f  te3_free(v->azCo
35cf1 6c 75 6d 6e 29 3b 0a 20 20 66 6f 72 28 69 20 3d  lumn);.  for(i =
35cf2 20 30 3b 20 69 20 3c 20 76 2d 3e 6e 43 6f 6c 75   0; i < v->nColu
35cf3 6d 6e 3b 20 2b 2b 69 29 20 7b 0a 20 20 20 20 73  mn; ++i) {.    s
35cf4 71 6c 69 74 65 33 5f 66 72 65 65 28 76 2d 3e 61  qlite3_free(v->a
35cf5 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 5b 69  zContentColumn[i
35cf6 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ]);.  }.  sqlite
35cf7 33 5f 66 72 65 65 28 76 2d 3e 61 7a 43 6f 6e 74  3_free(v->azCont
35cf8 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20 73 71  entColumn);.  sq
35cf9 6c 69 74 65 33 5f 66 72 65 65 28 76 29 3b 0a 7d  lite3_free(v);.}
35cfa 0a 0a 2f 2a 0a 2a 2a 20 54 6f 6b 65 6e 20 74 79  ../*.** Token ty
35cfb 70 65 73 20 66 6f 72 20 70 61 72 73 69 6e 67 20  pes for parsing 
35cfc 74 68 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f  the arguments to
35cfd 20 78 43 6f 6e 6e 65 63 74 20 6f 72 20 78 43 72   xConnect or xCr
35cfe 65 61 74 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  eate..*/.#define
35cff 20 54 4f 4b 45 4e 5f 45 4f 46 20 20 20 20 20 20   TOKEN_EOF      
35d00 20 20 20 30 20 20 20 20 2f 2a 20 45 6e 64 20 6f     0    /* End o
35d01 66 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69 6e  f file */.#defin
35d02 65 20 54 4f 4b 45 4e 5f 53 50 41 43 45 20 20 20  e TOKEN_SPACE   
35d03 20 20 20 20 31 20 20 20 20 2f 2a 20 41 6e 79 20      1    /* Any 
35d04 6b 69 6e 64 20 6f 66 20 77 68 69 74 65 73 70 61  kind of whitespa
35d05 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4f  ce */.#define TO
35d06 4b 45 4e 5f 49 44 20 20 20 20 20 20 20 20 20 20  KEN_ID          
35d07 32 20 20 20 20 2f 2a 20 41 6e 20 69 64 65 6e 74  2    /* An ident
35d08 69 66 69 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  ifier */.#define
35d09 20 54 4f 4b 45 4e 5f 53 54 52 49 4e 47 20 20 20   TOKEN_STRING   
35d0a 20 20 20 33 20 20 20 20 2f 2a 20 41 20 73 74 72     3    /* A str
35d0b 69 6e 67 20 6c 69 74 65 72 61 6c 20 2a 2f 0a 23  ing literal */.#
35d0c 64 65 66 69 6e 65 20 54 4f 4b 45 4e 5f 50 55 4e  define TOKEN_PUN
35d0d 43 54 20 20 20 20 20 20 20 34 20 20 20 20 2f 2a  CT       4    /*
35d0e 20 41 20 73 69 6e 67 6c 65 20 70 75 6e 63 74 75   A single punctu
35d0f 61 74 69 6f 6e 20 63 68 61 72 61 63 74 65 72 20  ation character 
35d10 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 58 20 69  */../*.** If X i
35d11 73 20 61 20 63 68 61 72 61 63 74 65 72 20 74 68  s a character th
35d12 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 69  at can be used i
35d13 6e 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20  n an identifier 
35d14 74 68 65 6e 0a 2a 2a 20 66 74 73 49 64 43 68 61  then.** ftsIdCha
35d15 72 28 58 29 20 77 69 6c 6c 20 62 65 20 74 72 75  r(X) will be tru
35d16 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69 74  e.  Otherwise it
35d17 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a   is false..**.**
35d18 20 46 6f 72 20 41 53 43 49 49 2c 20 61 6e 79 20   For ASCII, any 
35d19 63 68 61 72 61 63 74 65 72 20 77 69 74 68 20 74  character with t
35d1a 68 65 20 68 69 67 68 2d 6f 72 64 65 72 20 62 69  he high-order bi
35d1b 74 20 73 65 74 20 69 73 0a 2a 2a 20 61 6c 6c 6f  t set is.** allo
35d1c 77 65 64 20 69 6e 20 61 6e 20 69 64 65 6e 74 69  wed in an identi
35d1d 66 69 65 72 2e 20 20 46 6f 72 20 37 2d 62 69 74  fier.  For 7-bit
35d1e 20 63 68 61 72 61 63 74 65 72 73 2c 20 0a 2a 2a   characters, .**
35d1f 20 69 73 46 74 73 49 64 43 68 61 72 5b 58 5d 20   isFtsIdChar[X] 
35d20 6d 75 73 74 20 62 65 20 31 2e 0a 2a 2a 0a 2a 2a  must be 1..**.**
35d21 20 54 69 63 6b 65 74 20 23 31 30 36 36 2e 20 20   Ticket #1066.  
35d22 74 68 65 20 53 51 4c 20 73 74 61 6e 64 61 72 64  the SQL standard
35d23 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20   does not allow 
35d24 27 24 27 20 69 6e 20 74 68 65 0a 2a 2a 20 6d 69  '$' in the.** mi
35d25 64 64 6c 65 20 6f 66 20 69 64 65 6e 74 66 69 65  ddle of identfie
35d26 72 73 2e 20 20 42 75 74 20 6d 61 6e 79 20 53 51  rs.  But many SQ
35d27 4c 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  L implementation
35d28 73 20 64 6f 2e 20 0a 2a 2a 20 53 51 4c 69 74 65  s do. .** SQLite
35d29 20 77 69 6c 6c 20 61 6c 6c 6f 77 20 27 24 27 20   will allow '$' 
35d2a 69 6e 20 69 64 65 6e 74 69 66 69 65 72 73 20 66  in identifiers f
35d2b 6f 72 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  or compatibility
35d2c 2e 0a 2a 2a 20 42 75 74 20 74 68 65 20 66 65 61  ..** But the fea
35d2d 74 75 72 65 20 69 73 20 75 6e 64 6f 63 75 6d 65  ture is undocume
35d2e 6e 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  nted..*/.static 
35d2f 63 6f 6e 73 74 20 63 68 61 72 20 69 73 46 74 73  const char isFts
35d30 49 64 43 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20  IdChar[] = {./* 
35d31 78 30 20 78 31 20 78 32 20 78 33 20 78 34 20 78  x0 x1 x2 x3 x4 x
35d32 35 20 78 36 20 78 37 20 78 38 20 78 39 20 78 41  5 x6 x7 x8 x9 xA
35d33 20 78 42 20 78 43 20 78 44 20 78 45 20 78 46 20   xB xC xD xE xF 
35d34 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20  */.    0, 0, 0, 
35d35 30 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 1, 0, 0, 0, 0
35d36 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
35d37 20 30 2c 20 30 2c 20 20 2f 2a 20 32 78 20 2a 2f   0, 0,  /* 2x */
35d38 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c  .    1, 1, 1, 1,
35d39 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
35d3a 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 0, 0, 0, 0, 0
35d3b 2c 20 30 2c 20 20 2f 2a 20 33 78 20 2a 2f 0a 20  , 0,  /* 3x */. 
35d3c 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31     0, 1, 1, 1, 1
35d3d 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
35d3e 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
35d3f 31 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20  1,  /* 4x */.   
35d40 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
35d41 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
35d42 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c  , 0, 0, 0, 0, 1,
35d43 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 30    /* 5x */.    0
35d44 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
35d45 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
35d46 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20  1, 1, 1, 1, 1,  
35d47 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20 31 2c 20  /* 6x */.    1, 
35d48 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
35d49 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
35d4a 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a   0, 0, 0, 0,  /*
35d4b 20 37 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e   7x */.};.#defin
35d4c 65 20 66 74 73 49 64 43 68 61 72 28 43 29 20 20  e ftsIdChar(C)  
35d4d 28 28 28 63 3d 43 29 26 30 78 38 30 29 21 3d 30  (((c=C)&0x80)!=0
35d4e 20 7c 7c 20 28 63 3e 30 78 31 66 20 26 26 20 69   || (c>0x1f && i
35d4f 73 46 74 73 49 64 43 68 61 72 5b 63 2d 30 78 32  sFtsIdChar[c-0x2
35d50 30 5d 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  0])).../*.** Ret
35d51 75 72 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f  urn the length o
35d52 66 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74  f the token that
35d53 20 62 65 67 69 6e 73 20 61 74 20 7a 5b 30 5d 2e   begins at z[0].
35d54 20 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 74   .** Store the t
35d55 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 2a 74 6f  oken type in *to
35d56 6b 65 6e 54 79 70 65 20 62 65 66 6f 72 65 20 72  kenType before r
35d57 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61  eturning..*/.sta
35d58 74 69 63 20 69 6e 74 20 66 74 73 47 65 74 54 6f  tic int ftsGetTo
35d59 6b 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ken(const char *
35d5a 7a 2c 20 69 6e 74 20 2a 74 6f 6b 65 6e 54 79 70  z, int *tokenTyp
35d5b 65 29 7b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a  e){.  int i, c;.
35d5c 20 20 73 77 69 74 63 68 28 20 2a 7a 20 29 7b 0a    switch( *z ){.
35d5d 20 20 20 20 63 61 73 65 20 30 3a 20 7b 0a 20 20      case 0: {.  
35d5e 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
35d5f 20 54 4f 4b 45 4e 5f 45 4f 46 3b 0a 20 20 20 20   TOKEN_EOF;.    
35d60 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
35d61 7d 0a 20 20 20 20 63 61 73 65 20 27 20 27 3a 20  }.    case ' ': 
35d62 63 61 73 65 20 27 5c 74 27 3a 20 63 61 73 65 20  case '\t': case 
35d63 27 5c 6e 27 3a 20 63 61 73 65 20 27 5c 66 27 3a  '\n': case '\f':
35d64 20 63 61 73 65 20 27 5c 72 27 3a 20 7b 0a 20 20   case '\r': {.  
35d65 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 73 61 66      for(i=1; saf
35d66 65 5f 69 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b  e_isspace(z[i]);
35d67 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74   i++){}.      *t
35d68 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e  okenType = TOKEN
35d69 5f 53 50 41 43 45 3b 0a 20 20 20 20 20 20 72 65  _SPACE;.      re
35d6a 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20  turn i;.    }.  
35d6b 20 20 63 61 73 65 20 27 60 27 3a 0a 20 20 20 20    case '`':.    
35d6c 63 61 73 65 20 27 5c 27 27 3a 0a 20 20 20 20 63  case '\'':.    c
35d6d 61 73 65 20 27 22 27 3a 20 7b 0a 20 20 20 20 20  ase '"': {.     
35d6e 20 69 6e 74 20 64 65 6c 69 6d 20 3d 20 7a 5b 30   int delim = z[0
35d6f 5d 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31  ];.      for(i=1
35d70 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69  ; (c=z[i])!=0; i
35d71 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
35d72 20 63 3d 3d 64 65 6c 69 6d 20 29 7b 0a 20 20 20   c==delim ){.   
35d73 20 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31         if( z[i+1
35d74 5d 3d 3d 64 65 6c 69 6d 20 29 7b 0a 20 20 20 20  ]==delim ){.    
35d75 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20          i++;.   
35d76 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
35d77 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
35d78 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
35d79 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
35d7a 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
35d7b 3d 20 54 4f 4b 45 4e 5f 53 54 52 49 4e 47 3b 0a  = TOKEN_STRING;.
35d7c 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 20 2b        return i +
35d7d 20 28 63 21 3d 30 29 3b 0a 20 20 20 20 7d 0a 20   (c!=0);.    }. 
35d7e 20 20 20 63 61 73 65 20 27 5b 27 3a 20 7b 0a 20     case '[': {. 
35d7f 20 20 20 20 20 66 6f 72 28 69 3d 31 2c 20 63 3d       for(i=1, c=
35d80 7a 5b 30 5d 3b 20 63 21 3d 27 5d 27 20 26 26 20  z[0]; c!=']' && 
35d81 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b  (c=z[i])!=0; i++
35d82 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e  ){}.      *token
35d83 54 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 49 44 3b  Type = TOKEN_ID;
35d84 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b  .      return i;
35d85 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
35d86 6c 74 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20  lt: {.      if( 
35d87 21 66 74 73 49 64 43 68 61 72 28 2a 7a 29 20 29  !ftsIdChar(*z) )
35d88 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  {.        break;
35d89 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66  .      }.      f
35d8a 6f 72 28 69 3d 31 3b 20 66 74 73 49 64 43 68 61  or(i=1; ftsIdCha
35d8b 72 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a  r(z[i]); i++){}.
35d8c 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
35d8d 20 3d 20 54 4f 4b 45 4e 5f 49 44 3b 0a 20 20 20   = TOKEN_ID;.   
35d8e 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20     return i;.   
35d8f 20 7d 0a 20 20 7d 0a 20 20 2a 74 6f 6b 65 6e 54   }.  }.  *tokenT
35d90 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 50 55 4e 43  ype = TOKEN_PUNC
35d91 54 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  T;.  return 1;.}
35d92 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 6f 6b 65 6e 20  ../*.** A token 
35d93 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 61  extracted from a
35d94 20 73 74 72 69 6e 67 20 69 73 20 61 6e 20 69 6e   string is an in
35d95 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
35d96 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63  llowing.** struc
35d97 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ture..*/.typedef
35d98 20 73 74 72 75 63 74 20 46 74 73 54 6f 6b 65 6e   struct FtsToken
35d99 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20   {.  const char 
35d9a 2a 7a 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  *z;       /* Poi
35d9b 6e 74 65 72 20 74 6f 20 74 6f 6b 65 6e 20 74 65  nter to token te
35d9c 78 74 2e 20 20 4e 6f 74 20 27 5c 30 30 30 27 20  xt.  Not '\000' 
35d9d 74 65 72 6d 69 6e 61 74 65 64 20 2a 2f 0a 20 20  terminated */.  
35d9e 73 68 6f 72 74 20 69 6e 74 20 6e 3b 20 20 20 20  short int n;    
35d9f 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
35da0 66 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78 74  f the token text
35da1 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 7d 20   in bytes. */.} 
35da2 46 74 73 54 6f 6b 65 6e 3b 0a 0a 2f 2a 0a 2a 2a  FtsToken;../*.**
35da3 20 47 69 76 65 6e 20 61 20 69 6e 70 75 74 20 73   Given a input s
35da4 74 72 69 6e 67 20 28 77 68 69 63 68 20 69 73 20  tring (which is 
35da5 72 65 61 6c 6c 79 20 6f 6e 65 20 6f 66 20 74 68  really one of th
35da6 65 20 61 72 67 76 5b 5d 20 70 61 72 61 6d 65 74  e argv[] paramet
35da7 65 72 73 0a 2a 2a 20 70 61 73 73 65 64 20 69 6e  ers.** passed in
35da8 74 6f 20 78 43 6f 6e 6e 65 63 74 20 6f 72 20 78  to xConnect or x
35da9 43 72 65 61 74 65 29 20 73 70 6c 69 74 20 74 68  Create) split th
35daa 65 20 73 74 72 69 6e 67 20 75 70 20 69 6e 74 6f  e string up into
35dab 20 74 6f 6b 65 6e 73 2e 0a 2a 2a 20 52 65 74 75   tokens..** Retu
35dac 72 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70  rn an array of p
35dad 6f 69 6e 74 65 72 73 20 74 6f 20 27 5c 30 30 30  ointers to '\000
35dae 27 20 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72  ' terminated str
35daf 69 6e 67 73 2c 20 6f 6e 65 20 73 74 72 69 6e 67  ings, one string
35db0 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20 6e 6f 6e  .** for each non
35db1 2d 77 68 69 74 65 73 70 61 63 65 20 74 6f 6b 65  -whitespace toke
35db2 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  n..**.** The ret
35db3 75 72 6e 65 64 20 61 72 72 61 79 20 69 73 20 74  urned array is t
35db4 65 72 6d 69 6e 61 74 65 64 20 62 79 20 61 20 73  erminated by a s
35db5 69 6e 67 6c 65 20 4e 55 4c 4c 20 70 6f 69 6e 74  ingle NULL point
35db6 65 72 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20  er..**.** Space 
35db7 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74 75  to hold the retu
35db8 72 6e 65 64 20 61 72 72 61 79 20 69 73 20 6f 62  rned array is ob
35db9 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 20 73 69  tained from a si
35dba 6e 67 6c 65 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 61  ngle.** malloc a
35dbb 6e 64 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65  nd should be fre
35dbc 65 64 20 62 79 20 70 61 73 73 69 6e 67 20 74 68  ed by passing th
35dbd 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 74  e return value t
35dbe 6f 20 66 72 65 65 28 29 2e 0a 2a 2a 20 54 68 65  o free()..** The
35dbf 20 69 6e 64 69 76 69 64 75 61 6c 20 73 74 72 69   individual stri
35dc0 6e 67 73 20 77 69 74 68 69 6e 20 74 68 65 20 74  ngs within the t
35dc1 6f 6b 65 6e 20 6c 69 73 74 20 61 72 65 20 61 6c  oken list are al
35dc2 6c 20 61 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74  l a part of.** t
35dc3 68 65 20 73 69 6e 67 6c 65 20 6d 65 6d 6f 72 79  he single memory
35dc4 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 6e 64 20   allocation and 
35dc5 77 69 6c 6c 20 61 6c 6c 20 62 65 20 66 72 65 65  will all be free
35dc6 64 20 61 74 20 6f 6e 63 65 2e 0a 2a 2f 0a 73 74  d at once..*/.st
35dc7 61 74 69 63 20 63 68 61 72 20 2a 2a 74 6f 6b 65  atic char **toke
35dc8 6e 69 7a 65 53 74 72 69 6e 67 28 63 6f 6e 73 74  nizeString(const
35dc9 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 70   char *z, int *p
35dca 6e 54 6f 6b 65 6e 29 7b 0a 20 20 69 6e 74 20 6e  nToken){.  int n
35dcb 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 46 74 73  Token = 0;.  Fts
35dcc 54 6f 6b 65 6e 20 2a 61 54 6f 6b 65 6e 20 3d 20  Token *aToken = 
35dcd 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20  sqlite3_malloc( 
35dce 73 74 72 6c 65 6e 28 7a 29 20 2a 20 73 69 7a 65  strlen(z) * size
35dcf 6f 66 28 61 54 6f 6b 65 6e 5b 30 5d 29 20 29 3b  of(aToken[0]) );
35dd0 0a 20 20 69 6e 74 20 6e 20 3d 20 31 3b 0a 20 20  .  int n = 1;.  
35dd1 69 6e 74 20 65 2c 20 69 3b 0a 20 20 69 6e 74 20  int e, i;.  int 
35dd2 74 6f 74 61 6c 53 69 7a 65 20 3d 20 30 3b 0a 20  totalSize = 0;. 
35dd3 20 63 68 61 72 20 2a 2a 61 7a 54 6f 6b 65 6e 3b   char **azToken;
35dd4 0a 20 20 63 68 61 72 20 2a 7a 43 6f 70 79 3b 0a  .  char *zCopy;.
35dd5 20 20 77 68 69 6c 65 28 20 6e 3e 30 20 29 7b 0a    while( n>0 ){.
35dd6 20 20 20 20 6e 20 3d 20 66 74 73 47 65 74 54 6f      n = ftsGetTo
35dd7 6b 65 6e 28 7a 2c 20 26 65 29 3b 0a 20 20 20 20  ken(z, &e);.    
35dd8 69 66 28 20 65 21 3d 54 4f 4b 45 4e 5f 53 50 41  if( e!=TOKEN_SPA
35dd9 43 45 20 29 7b 0a 20 20 20 20 20 20 61 54 6f 6b  CE ){.      aTok
35dda 65 6e 5b 6e 54 6f 6b 65 6e 5d 2e 7a 20 3d 20 7a  en[nToken].z = z
35ddb 3b 0a 20 20 20 20 20 20 61 54 6f 6b 65 6e 5b 6e  ;.      aToken[n
35ddc 54 6f 6b 65 6e 5d 2e 6e 20 3d 20 6e 3b 0a 20 20  Token].n = n;.  
35ddd 20 20 20 20 6e 54 6f 6b 65 6e 2b 2b 3b 0a 20 20      nToken++;.  
35dde 20 20 20 20 74 6f 74 61 6c 53 69 7a 65 20 2b 3d      totalSize +=
35ddf 20 6e 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   n+1;.    }.    
35de0 7a 20 2b 3d 20 6e 3b 0a 20 20 7d 0a 20 20 61 7a  z += n;.  }.  az
35de1 54 6f 6b 65 6e 20 3d 20 28 63 68 61 72 2a 2a 29  Token = (char**)
35de2 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20  sqlite3_malloc( 
35de3 6e 54 6f 6b 65 6e 2a 73 69 7a 65 6f 66 28 63 68  nToken*sizeof(ch
35de4 61 72 2a 29 20 2b 20 74 6f 74 61 6c 53 69 7a 65  ar*) + totalSize
35de5 20 29 3b 0a 20 20 7a 43 6f 70 79 20 3d 20 28 63   );.  zCopy = (c
35de6 68 61 72 2a 29 26 61 7a 54 6f 6b 65 6e 5b 6e 54  har*)&azToken[nT
35de7 6f 6b 65 6e 5d 3b 0a 20 20 6e 54 6f 6b 65 6e 2d  oken];.  nToken-
35de8 2d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  -;.  for(i=0; i<
35de9 6e 54 6f 6b 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20  nToken; i++){.  
35dea 20 20 61 7a 54 6f 6b 65 6e 5b 69 5d 20 3d 20 7a    azToken[i] = z
35deb 43 6f 70 79 3b 0a 20 20 20 20 6e 20 3d 20 61 54  Copy;.    n = aT
35dec 6f 6b 65 6e 5b 69 5d 2e 6e 3b 0a 20 20 20 20 6d  oken[i].n;.    m
35ded 65 6d 63 70 79 28 7a 43 6f 70 79 2c 20 61 54 6f  emcpy(zCopy, aTo
35dee 6b 65 6e 5b 69 5d 2e 7a 2c 20 6e 29 3b 0a 20 20  ken[i].z, n);.  
35def 20 20 7a 43 6f 70 79 5b 6e 5d 20 3d 20 30 3b 0a    zCopy[n] = 0;.
35df0 20 20 20 20 7a 43 6f 70 79 20 2b 3d 20 6e 2b 31      zCopy += n+1
35df1 3b 0a 20 20 7d 0a 20 20 61 7a 54 6f 6b 65 6e 5b  ;.  }.  azToken[
35df2 6e 54 6f 6b 65 6e 5d 20 3d 20 30 3b 0a 20 20 73  nToken] = 0;.  s
35df3 71 6c 69 74 65 33 5f 66 72 65 65 28 61 54 6f 6b  qlite3_free(aTok
35df4 65 6e 29 3b 0a 20 20 2a 70 6e 54 6f 6b 65 6e 20  en);.  *pnToken 
35df5 3d 20 6e 54 6f 6b 65 6e 3b 0a 20 20 72 65 74 75  = nToken;.  retu
35df6 72 6e 20 61 7a 54 6f 6b 65 6e 3b 0a 7d 0a 0a 2f  rn azToken;.}../
35df7 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e 20  *.** Convert an 
35df8 53 51 4c 2d 73 74 79 6c 65 20 71 75 6f 74 65 64  SQL-style quoted
35df9 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20 6e   string into a n
35dfa 6f 72 6d 61 6c 20 73 74 72 69 6e 67 20 62 79 20  ormal string by 
35dfb 72 65 6d 6f 76 69 6e 67 0a 2a 2a 20 74 68 65 20  removing.** the 
35dfc 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 73  quote characters
35dfd 2e 20 20 54 68 65 20 63 6f 6e 76 65 72 73 69 6f  .  The conversio
35dfe 6e 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61  n is done in-pla
35dff 63 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 69  ce.  If the.** i
35e00 6e 70 75 74 20 64 6f 65 73 20 6e 6f 74 20 62 65  nput does not be
35e01 67 69 6e 20 77 69 74 68 20 61 20 71 75 6f 74 65  gin with a quote
35e02 20 63 68 61 72 61 63 74 65 72 2c 20 74 68 65 6e   character, then
35e03 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
35e04 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a   is a no-op..**.
35e05 2a 2a 20 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a  ** Examples:.**.
35e06 2a 2a 20 20 20 20 20 22 61 62 63 22 20 20 20 62  **     "abc"   b
35e07 65 63 6f 6d 65 73 20 20 20 61 62 63 0a 2a 2a 20  ecomes   abc.** 
35e08 20 20 20 20 27 78 79 7a 27 20 20 20 62 65 63 6f      'xyz'   beco
35e09 6d 65 73 20 20 20 78 79 7a 0a 2a 2a 20 20 20 20  mes   xyz.**    
35e0a 20 5b 70 71 72 5d 20 20 20 62 65 63 6f 6d 65 73   [pqr]   becomes
35e0b 20 20 20 70 71 72 0a 2a 2a 20 20 20 20 20 60 6d     pqr.**     `m
35e0c 6e 6f 60 20 20 20 62 65 63 6f 6d 65 73 20 20 20  no`   becomes   
35e0d 6d 6e 6f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  mno.*/.static vo
35e0e 69 64 20 64 65 71 75 6f 74 65 53 74 72 69 6e 67  id dequoteString
35e0f 28 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e 74  (char *z){.  int
35e10 20 71 75 6f 74 65 3b 0a 20 20 69 6e 74 20 69 2c   quote;.  int i,
35e11 20 6a 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29   j;.  if( z==0 )
35e12 20 72 65 74 75 72 6e 3b 0a 20 20 71 75 6f 74 65   return;.  quote
35e13 20 3d 20 7a 5b 30 5d 3b 0a 20 20 73 77 69 74 63   = z[0];.  switc
35e14 68 28 20 71 75 6f 74 65 20 29 7b 0a 20 20 20 20  h( quote ){.    
35e15 63 61 73 65 20 27 5c 27 27 3a 20 20 62 72 65 61  case '\'':  brea
35e16 6b 3b 0a 20 20 20 20 63 61 73 65 20 27 22 27 3a  k;.    case '"':
35e17 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
35e18 73 65 20 27 60 27 3a 20 20 20 62 72 65 61 6b 3b  se '`':   break;
35e19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35e1a 2f 2a 20 46 6f 72 20 4d 79 53 51 4c 20 63 6f 6d  /* For MySQL com
35e1b 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20  patibility */.  
35e1c 20 20 63 61 73 65 20 27 5b 27 3a 20 20 20 71 75    case '[':   qu
35e1d 6f 74 65 20 3d 20 27 5d 27 3b 20 20 62 72 65 61  ote = ']';  brea
35e1e 6b 3b 20 20 2f 2a 20 46 6f 72 20 4d 53 20 53 71  k;  /* For MS Sq
35e1f 6c 53 65 72 76 65 72 20 63 6f 6d 70 61 74 69 62  lServer compatib
35e20 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 64 65 66  ility */.    def
35e21 61 75 6c 74 3a 20 20 20 20 72 65 74 75 72 6e 3b  ault:    return;
35e22 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 2c 20  .  }.  for(i=1, 
35e23 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 7b  j=0; z[i]; i++){
35e24 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 71  .    if( z[i]==q
35e25 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 69 66  uote ){.      if
35e26 28 20 7a 5b 69 2b 31 5d 3d 3d 71 75 6f 74 65 20  ( z[i+1]==quote 
35e27 29 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b  ){.        z[j++
35e28 5d 20 3d 20 71 75 6f 74 65 3b 0a 20 20 20 20 20  ] = quote;.     
35e29 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 7d 65     i++;.      }e
35e2a 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a  lse{.        z[j
35e2b 2b 2b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ++] = 0;.       
35e2c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
35e2d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
35e2e 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a   z[j++] = z[i];.
35e2f 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
35e30 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 61 7a 49  ** The input azI
35e31 6e 20 69 73 20 61 20 4e 55 4c 4c 2d 74 65 72 6d  n is a NULL-term
35e32 69 6e 61 74 65 64 20 6c 69 73 74 20 6f 66 20 74  inated list of t
35e33 6f 6b 65 6e 73 2e 20 20 52 65 6d 6f 76 65 20 74  okens.  Remove t
35e34 68 65 20 66 69 72 73 74 0a 2a 2a 20 74 6f 6b 65  he first.** toke
35e35 6e 20 61 6e 64 20 61 6c 6c 20 70 75 6e 63 74 75  n and all punctu
35e36 61 74 69 6f 6e 20 74 6f 6b 65 6e 73 2e 20 20 52  ation tokens.  R
35e37 65 6d 6f 76 65 20 74 68 65 20 71 75 6f 74 65 73  emove the quotes
35e38 20 66 72 6f 6d 0a 2a 2a 20 61 72 6f 75 6e 64 20   from.** around 
35e39 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20 74  string literal t
35e3a 6f 6b 65 6e 73 2e 0a 2a 2a 0a 2a 2a 20 45 78 61  okens..**.** Exa
35e3b 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  mple:.**.**     
35e3c 69 6e 70 75 74 3a 20 20 20 20 20 20 74 6f 6b 65  input:      toke
35e3d 6e 69 7a 65 20 63 68 69 6e 65 73 65 20 28 20 27  nize chinese ( '
35e3e 73 69 6d 70 6c 69 66 65 64 27 20 2c 20 27 6d 69  simplifed' , 'mi
35e3f 78 65 64 27 20 29 0a 2a 2a 20 20 20 20 20 6f 75  xed' ).**     ou
35e40 74 70 75 74 3a 20 20 20 20 20 63 68 69 6e 65 73  tput:     chines
35e41 65 20 73 69 6d 70 6c 69 66 65 64 20 6d 69 78 65  e simplifed mixe
35e42 64 0a 2a 2a 0a 2a 2a 20 41 6e 6f 74 68 65 72 20  d.**.** Another 
35e43 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20  example:.**.**  
35e44 20 20 20 69 6e 70 75 74 3a 20 20 20 20 20 20 64     input:      d
35e45 65 6c 69 6d 69 74 65 72 73 20 28 20 27 5b 27 20  elimiters ( '[' 
35e46 2c 20 27 5d 27 20 2c 20 27 2e 2e 2e 27 20 29 0a  , ']' , '...' ).
35e47 2a 2a 20 20 20 20 20 6f 75 74 70 75 74 3a 20 20  **     output:  
35e48 20 20 20 5b 20 5d 20 2e 2e 2e 0a 2a 2f 0a 73 74     [ ] ....*/.st
35e49 61 74 69 63 20 76 6f 69 64 20 74 6f 6b 65 6e 4c  atic void tokenL
35e4a 69 73 74 54 6f 49 64 4c 69 73 74 28 63 68 61 72  istToIdList(char
35e4b 20 2a 2a 61 7a 49 6e 29 7b 0a 20 20 69 6e 74 20   **azIn){.  int 
35e4c 69 2c 20 6a 3b 0a 20 20 69 66 28 20 61 7a 49 6e  i, j;.  if( azIn
35e4d 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c   ){.    for(i=0,
35e4e 20 6a 3d 2d 31 3b 20 61 7a 49 6e 5b 69 5d 3b 20   j=-1; azIn[i]; 
35e4f 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
35e50 73 61 66 65 5f 69 73 61 6c 6e 75 6d 28 61 7a 49  safe_isalnum(azI
35e51 6e 5b 69 5d 5b 30 5d 29 20 7c 7c 20 61 7a 49 6e  n[i][0]) || azIn
35e52 5b 69 5d 5b 31 5d 20 29 7b 0a 20 20 20 20 20 20  [i][1] ){.      
35e53 20 20 64 65 71 75 6f 74 65 53 74 72 69 6e 67 28    dequoteString(
35e54 61 7a 49 6e 5b 69 5d 29 3b 0a 20 20 20 20 20 20  azIn[i]);.      
35e55 20 20 69 66 28 20 6a 3e 3d 30 20 29 7b 0a 20 20    if( j>=0 ){.  
35e56 20 20 20 20 20 20 20 20 61 7a 49 6e 5b 6a 5d 20          azIn[j] 
35e57 3d 20 61 7a 49 6e 5b 69 5d 3b 0a 20 20 20 20 20  = azIn[i];.     
35e58 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6a 2b 2b     }.        j++
35e59 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
35e5a 20 20 20 20 61 7a 49 6e 5b 6a 5d 20 3d 20 30 3b      azIn[j] = 0;
35e5b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46  .  }.}.../*.** F
35e5c 69 6e 64 20 74 68 65 20 66 69 72 73 74 20 61 6c  ind the first al
35e5d 70 68 61 6e 75 6d 65 72 69 63 20 74 6f 6b 65 6e  phanumeric token
35e5e 20 69 6e 20 74 68 65 20 73 74 72 69 6e 67 20 7a   in the string z
35e5f 49 6e 2e 20 20 4e 75 6c 6c 2d 74 65 72 6d 69 6e  In.  Null-termin
35e60 61 74 65 0a 2a 2a 20 74 68 69 73 20 74 6f 6b 65  ate.** this toke
35e61 6e 2e 20 20 52 65 6d 6f 76 65 20 61 6e 79 20 71  n.  Remove any q
35e62 75 6f 74 61 74 69 6f 6e 20 6d 61 72 6b 73 2e 20  uotation marks. 
35e63 20 41 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f   And return a po
35e64 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20  inter to.** the 
35e65 72 65 73 75 6c 74 2e 0a 2a 2f 0a 73 74 61 74 69  result..*/.stati
35e66 63 20 63 68 61 72 20 2a 66 69 72 73 74 54 6f 6b  c char *firstTok
35e67 65 6e 28 63 68 61 72 20 2a 7a 49 6e 2c 20 63 68  en(char *zIn, ch
35e68 61 72 20 2a 2a 70 7a 54 61 69 6c 29 7b 0a 20 20  ar **pzTail){.  
35e69 69 6e 74 20 6e 2c 20 74 74 79 70 65 3b 0a 20 20  int n, ttype;.  
35e6a 77 68 69 6c 65 28 31 29 7b 0a 20 20 20 20 6e 20  while(1){.    n 
35e6b 3d 20 66 74 73 47 65 74 54 6f 6b 65 6e 28 7a 49  = ftsGetToken(zI
35e6c 6e 2c 20 26 74 74 79 70 65 29 3b 0a 20 20 20 20  n, &ttype);.    
35e6d 69 66 28 20 74 74 79 70 65 3d 3d 54 4f 4b 45 4e  if( ttype==TOKEN
35e6e 5f 53 50 41 43 45 20 29 7b 0a 20 20 20 20 20 20  _SPACE ){.      
35e6f 7a 49 6e 20 2b 3d 20 6e 3b 0a 20 20 20 20 7d 65  zIn += n;.    }e
35e70 6c 73 65 20 69 66 28 20 74 74 79 70 65 3d 3d 54  lse if( ttype==T
35e71 4f 4b 45 4e 5f 45 4f 46 20 29 7b 0a 20 20 20 20  OKEN_EOF ){.    
35e72 20 20 2a 70 7a 54 61 69 6c 20 3d 20 7a 49 6e 3b    *pzTail = zIn;
35e73 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
35e74 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
35e75 20 20 7a 49 6e 5b 6e 5d 20 3d 20 30 3b 0a 20 20    zIn[n] = 0;.  
35e76 20 20 20 20 2a 70 7a 54 61 69 6c 20 3d 20 26 7a      *pzTail = &z
35e77 49 6e 5b 31 5d 3b 0a 20 20 20 20 20 20 64 65 71  In[1];.      deq
35e78 75 6f 74 65 53 74 72 69 6e 67 28 7a 49 6e 29 3b  uoteString(zIn);
35e79 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 7a 49  .      return zI
35e7a 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f  n;.    }.  }.  /
35e7b 2a 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a 7d 0a  *NOTREACHED*/.}.
35e7c 0a 2f 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  ./* Return true 
35e7d 69 66 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  if....**.**   * 
35e7e 20 73 20 62 65 67 69 6e 73 20 77 69 74 68 20 74   s begins with t
35e7f 68 65 20 73 74 72 69 6e 67 20 74 2c 20 69 67 6e  he string t, ign
35e80 6f 72 69 6e 67 20 63 61 73 65 0a 2a 2a 20 20 20  oring case.**   
35e81 2a 20 20 73 20 69 73 20 6c 6f 6e 67 65 72 20 74  *  s is longer t
35e82 68 61 6e 20 74 0a 2a 2a 20 20 20 2a 20 20 54 68  han t.**   *  Th
35e83 65 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65  e first characte
35e84 72 20 6f 66 20 73 20 62 65 79 6f 6e 64 20 74 20  r of s beyond t 
35e85 69 73 20 6e 6f 74 20 61 20 61 6c 70 68 61 6e 75  is not a alphanu
35e86 6d 65 72 69 63 0a 2a 2a 20 0a 2a 2a 20 49 67 6e  meric.** .** Ign
35e87 6f 72 65 20 6c 65 61 64 69 6e 67 20 73 70 61 63  ore leading spac
35e88 65 20 69 6e 20 2a 73 2e 0a 2a 2a 0a 2a 2a 20 54  e in *s..**.** T
35e89 6f 20 70 75 74 20 69 74 20 61 6e 6f 74 68 65 72  o put it another
35e8a 20 77 61 79 2c 20 72 65 74 75 72 6e 20 74 72 75   way, return tru
35e8b 65 20 69 66 20 74 68 65 20 66 69 72 73 74 20 74  e if the first t
35e8c 6f 6b 65 6e 20 6f 66 0a 2a 2a 20 73 5b 5d 20 69  oken of.** s[] i
35e8d 73 20 74 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63  s t[]..*/.static
35e8e 20 69 6e 74 20 73 74 61 72 74 73 57 69 74 68 28   int startsWith(
35e8f 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 2c 20 63  const char *s, c
35e90 6f 6e 73 74 20 63 68 61 72 20 2a 74 29 7b 0a 20  onst char *t){. 
35e91 20 77 68 69 6c 65 28 20 73 61 66 65 5f 69 73 73   while( safe_iss
35e92 70 61 63 65 28 2a 73 29 20 29 7b 20 73 2b 2b 3b  pace(*s) ){ s++;
35e93 20 7d 0a 20 20 77 68 69 6c 65 28 20 2a 74 20 29   }.  while( *t )
35e94 7b 0a 20 20 20 20 69 66 28 20 73 61 66 65 5f 74  {.    if( safe_t
35e95 6f 6c 6f 77 65 72 28 2a 73 2b 2b 29 21 3d 73 61  olower(*s++)!=sa
35e96 66 65 5f 74 6f 6c 6f 77 65 72 28 2a 74 2b 2b 29  fe_tolower(*t++)
35e97 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d   ) return 0;.  }
35e98 0a 20 20 72 65 74 75 72 6e 20 2a 73 21 3d 27 5f  .  return *s!='_
35e99 27 20 26 26 20 21 73 61 66 65 5f 69 73 61 6c 6e  ' && !safe_isaln
35e9a 75 6d 28 2a 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  um(*s);.}../*.**
35e9b 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
35e9c 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 64  this structure d
35e9d 65 66 69 6e 65 73 20 74 68 65 20 22 73 70 65 63  efines the "spec
35e9e 22 20 6f 66 20 61 0a 2a 2a 20 66 75 6c 6c 20 74  " of a.** full t
35e9f 65 78 74 20 69 6e 64 65 78 2e 20 20 54 68 69 73  ext index.  This
35ea0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 70 6f   structure is po
35ea1 70 75 6c 61 74 65 64 20 62 79 20 70 61 72 73 65  pulated by parse
35ea2 53 70 65 63 0a 2a 2a 20 61 6e 64 20 75 73 65 20  Spec.** and use 
35ea3 62 79 20 66 75 6c 6c 74 65 78 74 43 6f 6e 6e 65  by fulltextConne
35ea4 63 74 20 61 6e 64 20 66 75 6c 6c 74 65 78 74 43  ct and fulltextC
35ea5 72 65 61 74 65 2e 0a 2a 2f 0a 74 79 70 65 64 65  reate..*/.typede
35ea6 66 20 73 74 72 75 63 74 20 54 61 62 6c 65 53 70  f struct TableSp
35ea7 65 63 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61  ec {.  const cha
35ea8 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20  r *zDb;         
35ea9 2f 2a 20 4c 6f 67 69 63 61 6c 20 64 61 74 61 62  /* Logical datab
35eaa 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f  ase name */.  co
35eab 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  nst char *zName;
35eac 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
35ead 66 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20  f the full-text 
35eae 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 6e  index */.  int n
35eaf 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20  Column;         
35eb0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
35eb1 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69   columns to be i
35eb2 6e 64 65 78 65 64 20 2a 2f 0a 20 20 63 68 61 72  ndexed */.  char
35eb3 20 2a 2a 61 7a 43 6f 6c 75 6d 6e 3b 20 20 20 20   **azColumn;    
35eb4 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c       /* Original
35eb5 20 6e 61 6d 65 73 20 6f 66 20 63 6f 6c 75 6d 6e   names of column
35eb6 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20  s to be indexed 
35eb7 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f  */.  char **azCo
35eb8 6e 74 65 6e 74 43 6f 6c 75 6d 6e 3b 20 20 2f 2a  ntentColumn;  /*
35eb9 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 6f   Column names fo
35eba 72 20 25 5f 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20  r %_content */. 
35ebb 20 63 68 61 72 20 2a 2a 61 7a 54 6f 6b 65 6e 69   char **azTokeni
35ebc 7a 65 72 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d  zer;      /* Nam
35ebd 65 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 72 20 61  e of tokenizer a
35ebe 6e 64 20 69 74 73 20 61 72 67 75 6d 65 6e 74 73  nd its arguments
35ebf 20 2a 2f 0a 7d 20 54 61 62 6c 65 53 70 65 63 3b   */.} TableSpec;
35ec0 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6c 61 69 6d 20  ../*.** Reclaim 
35ec1 61 6c 6c 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  all of the memor
35ec2 79 20 75 73 65 64 20 62 79 20 61 20 54 61 62 6c  y used by a Tabl
35ec3 65 53 70 65 63 0a 2a 2f 0a 73 74 61 74 69 63 20  eSpec.*/.static 
35ec4 76 6f 69 64 20 63 6c 65 61 72 54 61 62 6c 65 53  void clearTableS
35ec5 70 65 63 28 54 61 62 6c 65 53 70 65 63 20 2a 70  pec(TableSpec *p
35ec6 29 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  ) {.  sqlite3_fr
35ec7 65 65 28 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 29 3b  ee(p->azColumn);
35ec8 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
35ec9 70 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75  p->azContentColu
35eca 6d 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  mn);.  sqlite3_f
35ecb 72 65 65 28 70 2d 3e 61 7a 54 6f 6b 65 6e 69 7a  ree(p->azTokeniz
35ecc 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 50 61 72 73 65  er);.}../* Parse
35ecd 20 61 20 43 52 45 41 54 45 20 56 49 52 54 55 41   a CREATE VIRTUA
35ece 4c 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  L TABLE statemen
35ecf 74 2c 20 77 68 69 63 68 20 6c 6f 6f 6b 73 20 6c  t, which looks l
35ed0 69 6b 65 20 74 68 69 73 3a 0a 20 2a 0a 20 2a 20  ike this:. *. * 
35ed1 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54  CREATE VIRTUAL T
35ed2 41 42 4c 45 20 65 6d 61 69 6c 0a 20 2a 20 20 20  ABLE email. *   
35ed3 20 20 20 20 20 55 53 49 4e 47 20 66 74 73 33 28       USING fts3(
35ed4 73 75 62 6a 65 63 74 2c 20 62 6f 64 79 2c 20 74  subject, body, t
35ed5 6f 6b 65 6e 69 7a 65 20 6d 79 74 6f 6b 65 6e 69  okenize mytokeni
35ed6 7a 65 72 28 6d 79 61 72 67 29 29 0a 20 2a 0a 20  zer(myarg)). *. 
35ed7 2a 20 57 65 20 72 65 74 75 72 6e 20 70 61 72 73  * We return pars
35ed8 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ed information i
35ed9 6e 20 61 20 54 61 62 6c 65 53 70 65 63 20 73 74  n a TableSpec st
35eda 72 75 63 74 75 72 65 2e 0a 20 2a 20 0a 20 2a 2f  ructure.. * . */
35edb 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73  .static int pars
35edc 65 53 70 65 63 28 54 61 62 6c 65 53 70 65 63 20  eSpec(TableSpec 
35edd 2a 70 53 70 65 63 2c 20 69 6e 74 20 61 72 67 63  *pSpec, int argc
35ede 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f  , const char *co
35edf 6e 73 74 2a 61 72 67 76 2c 0a 20 20 20 20 20 20  nst*argv,.      
35ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
35ee1 68 61 72 2a 2a 70 7a 45 72 72 29 7b 0a 20 20 69  har**pzErr){.  i
35ee2 6e 74 20 69 2c 20 6e 3b 0a 20 20 63 68 61 72 20  nt i, n;.  char 
35ee3 2a 7a 2c 20 2a 7a 44 75 6d 6d 79 3b 0a 20 20 63  *z, *zDummy;.  c
35ee4 68 61 72 20 2a 2a 61 7a 41 72 67 3b 0a 20 20 63  har **azArg;.  c
35ee5 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65  onst char *zToke
35ee6 6e 69 7a 65 72 20 3d 20 30 3b 20 20 20 20 2f 2a  nizer = 0;    /*
35ee7 20 61 72 67 76 5b 5d 20 65 6e 74 72 79 20 64 65   argv[] entry de
35ee8 73 63 72 69 62 69 6e 67 20 74 68 65 20 74 6f 6b  scribing the tok
35ee9 65 6e 69 7a 65 72 20 2a 2f 0a 0a 20 20 61 73 73  enizer */..  ass
35eea 65 72 74 28 20 61 72 67 63 3e 3d 33 20 29 3b 0a  ert( argc>=3 );.
35eeb 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 69 6e 74    /* Current int
35eec 65 72 66 61 63 65 3a 0a 20 20 2a 2a 20 61 72 67  erface:.  ** arg
35eed 76 5b 30 5d 20 2d 20 6d 6f 64 75 6c 65 20 6e 61  v[0] - module na
35eee 6d 65 0a 20 20 2a 2a 20 61 72 67 76 5b 31 5d 20  me.  ** argv[1] 
35eef 2d 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a  - database name.
35ef0 20 20 2a 2a 20 61 72 67 76 5b 32 5d 20 2d 20 74    ** argv[2] - t
35ef1 61 62 6c 65 20 6e 61 6d 65 0a 20 20 2a 2a 20 61  able name.  ** a
35ef2 72 67 76 5b 33 2e 2e 5d 20 2d 20 63 6f 6c 75 6d  rgv[3..] - colum
35ef3 6e 73 2c 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 66  ns, optionally f
35ef4 6f 6c 6c 6f 77 65 64 20 62 79 20 74 6f 6b 65 6e  ollowed by token
35ef5 69 7a 65 72 20 73 70 65 63 69 66 69 63 61 74 69  izer specificati
35ef6 6f 6e 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20  on.  **         
35ef7 20 20 20 20 61 6e 64 20 73 6e 69 70 70 65 74 20      and snippet 
35ef8 64 65 6c 69 6d 69 74 65 72 73 20 73 70 65 63 69  delimiters speci
35ef9 66 69 63 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 0a  fication..  */..
35efa 20 20 2f 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79    /* Make a copy
35efb 20 6f 66 20 74 68 65 20 63 6f 6d 70 6c 65 74 65   of the complete
35efc 20 61 72 67 76 5b 5d 5b 5d 20 61 72 72 61 79 20   argv[][] array 
35efd 69 6e 20 61 20 73 69 6e 67 6c 65 20 61 6c 6c 6f  in a single allo
35efe 63 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68 65  cation..  ** The
35eff 20 61 72 67 76 5b 5d 5b 5d 20 61 72 72 61 79 20   argv[][] array 
35f00 69 73 20 72 65 61 64 2d 6f 6e 6c 79 20 61 6e 64  is read-only and
35f01 20 74 72 61 6e 73 69 65 6e 74 2e 20 20 57 65 20   transient.  We 
35f02 63 61 6e 20 77 72 69 74 65 20 74 6f 20 74 68 65  can write to the
35f03 0a 20 20 2a 2a 20 63 6f 70 79 20 69 6e 20 6f 72  .  ** copy in or
35f04 64 65 72 20 74 6f 20 6d 6f 64 69 66 79 20 74 68  der to modify th
35f05 69 6e 67 73 20 61 6e 64 20 74 68 65 20 63 6f 70  ings and the cop
35f06 79 20 69 73 20 70 65 72 73 69 73 74 65 6e 74 2e  y is persistent.
35f07 0a 20 20 2a 2f 0a 20 20 43 4c 45 41 52 28 70 53  .  */.  CLEAR(pS
35f08 70 65 63 29 3b 0a 20 20 66 6f 72 28 69 3d 6e 3d  pec);.  for(i=n=
35f09 30 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b  0; i<argc; i++){
35f0a 0a 20 20 20 20 6e 20 2b 3d 20 73 74 72 6c 65 6e  .    n += strlen
35f0b 28 61 72 67 76 5b 69 5d 29 20 2b 20 31 3b 0a 20  (argv[i]) + 1;. 
35f0c 20 7d 0a 20 20 61 7a 41 72 67 20 3d 20 73 71 6c   }.  azArg = sql
35f0d 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a  ite3_malloc( siz
35f0e 65 6f 66 28 63 68 61 72 2a 29 2a 61 72 67 63 20  eof(char*)*argc 
35f0f 2b 20 6e 20 29 3b 0a 20 20 69 66 28 20 61 7a 41  + n );.  if( azA
35f10 72 67 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  rg==0 ){.    ret
35f11 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
35f12 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61  ;.  }.  z = (cha
35f13 72 2a 29 26 61 7a 41 72 67 5b 61 72 67 63 5d 3b  r*)&azArg[argc];
35f14 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 61 72  .  for(i=0; i<ar
35f15 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 7a  gc; i++){.    az
35f16 41 72 67 5b 69 5d 20 3d 20 7a 3b 0a 20 20 20 20  Arg[i] = z;.    
35f17 73 74 72 63 70 79 28 7a 2c 20 61 72 67 76 5b 69  strcpy(z, argv[i
35f18 5d 29 3b 0a 20 20 20 20 7a 20 2b 3d 20 73 74 72  ]);.    z += str
35f19 6c 65 6e 28 7a 29 2b 31 3b 0a 20 20 7d 0a 0a 20  len(z)+1;.  }.. 
35f1a 20 2f 2a 20 49 64 65 6e 74 69 66 79 20 74 68 65   /* Identify the
35f1b 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 6e   column names an
35f1c 64 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20  d the tokenizer 
35f1d 61 6e 64 20 64 65 6c 69 6d 69 74 65 72 20 61 72  and delimiter ar
35f1e 67 75 6d 65 6e 74 73 0a 20 20 2a 2a 20 69 6e 20  guments.  ** in 
35f1f 74 68 65 20 61 72 67 76 5b 5d 5b 5d 20 61 72 72  the argv[][] arr
35f20 61 79 2e 0a 20 20 2a 2f 0a 20 20 70 53 70 65 63  ay..  */.  pSpec
35f21 2d 3e 7a 44 62 20 3d 20 61 7a 41 72 67 5b 31 5d  ->zDb = azArg[1]
35f22 3b 0a 20 20 70 53 70 65 63 2d 3e 7a 4e 61 6d 65  ;.  pSpec->zName
35f23 20 3d 20 61 7a 41 72 67 5b 32 5d 3b 0a 20 20 70   = azArg[2];.  p
35f24 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20  Spec->nColumn = 
35f25 30 3b 0a 20 20 70 53 70 65 63 2d 3e 61 7a 43 6f  0;.  pSpec->azCo
35f26 6c 75 6d 6e 20 3d 20 61 7a 41 72 67 3b 0a 20 20  lumn = azArg;.  
35f27 7a 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 22 74 6f  zTokenizer = "to
35f28 6b 65 6e 69 7a 65 20 73 69 6d 70 6c 65 22 3b 0a  kenize simple";.
35f29 20 20 66 6f 72 28 69 3d 33 3b 20 69 3c 61 72 67    for(i=3; i<arg
35f2a 63 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 69 66 28  c; ++i){.    if(
35f2b 20 73 74 61 72 74 73 57 69 74 68 28 61 7a 41 72   startsWith(azAr
35f2c 67 5b 69 5d 2c 22 74 6f 6b 65 6e 69 7a 65 22 29  g[i],"tokenize")
35f2d 20 29 7b 0a 20 20 20 20 20 20 7a 54 6f 6b 65 6e   ){.      zToken
35f2e 69 7a 65 72 20 3d 20 61 7a 41 72 67 5b 69 5d 3b  izer = azArg[i];
35f2f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
35f30 20 20 7a 20 3d 20 61 7a 41 72 67 5b 70 53 70 65    z = azArg[pSpe
35f31 63 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 66 69  c->nColumn] = fi
35f32 72 73 74 54 6f 6b 65 6e 28 61 7a 41 72 67 5b 69  rstToken(azArg[i
35f33 5d 2c 20 26 7a 44 75 6d 6d 79 29 3b 0a 20 20 20  ], &zDummy);.   
35f34 20 20 20 70 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d     pSpec->nColum
35f35 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n++;.    }.  }. 
35f36 20 69 66 28 20 70 53 70 65 63 2d 3e 6e 43 6f 6c   if( pSpec->nCol
35f37 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 61 7a  umn==0 ){.    az
35f38 41 72 67 5b 30 5d 20 3d 20 22 63 6f 6e 74 65 6e  Arg[0] = "conten
35f39 74 22 3b 0a 20 20 20 20 70 53 70 65 63 2d 3e 6e  t";.    pSpec->n
35f3a 43 6f 6c 75 6d 6e 20 3d 20 31 3b 0a 20 20 7d 0a  Column = 1;.  }.
35f3b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6f 6e 73 74  .  /*.  ** Const
35f3c 72 75 63 74 20 74 68 65 20 6c 69 73 74 20 6f 66  ruct the list of
35f3d 20 63 6f 6e 74 65 6e 74 20 63 6f 6c 75 6d 6e 20   content column 
35f3e 6e 61 6d 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  names..  **.  **
35f3f 20 45 61 63 68 20 63 6f 6e 74 65 6e 74 20 63 6f   Each content co
35f40 6c 75 6d 6e 20 6e 61 6d 65 20 77 69 6c 6c 20 62  lumn name will b
35f41 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 63 4e  e of the form cN
35f42 4e 41 41 41 41 0a 20 20 2a 2a 20 77 68 65 72 65  NAAAA.  ** where
35f43 20 4e 4e 20 69 73 20 74 68 65 20 63 6f 6c 75 6d   NN is the colum
35f44 6e 20 6e 75 6d 62 65 72 20 61 6e 64 20 41 41 41  n number and AAA
35f45 41 20 69 73 20 74 68 65 20 73 61 6e 69 74 69 7a  A is the sanitiz
35f46 65 64 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6e  ed.  ** column n
35f47 61 6d 65 2e 20 20 22 73 61 6e 69 74 69 7a 65 64  ame.  "sanitized
35f48 22 20 6d 65 61 6e 73 20 74 68 61 74 20 73 70 65  " means that spe
35f49 63 69 61 6c 20 63 68 61 72 61 63 74 65 72 73 20  cial characters 
35f4a 61 72 65 0a 20 20 2a 2a 20 63 6f 6e 76 65 72 74  are.  ** convert
35f4b 65 64 20 74 6f 20 22 5f 22 2e 20 20 54 68 65 20  ed to "_".  The 
35f4c 63 4e 4e 20 70 72 65 66 69 78 20 67 75 61 72 61  cNN prefix guara
35f4d 6e 74 65 65 73 20 74 68 61 74 20 61 6c 6c 20 63  ntees that all c
35f4e 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 6e 61 6d 65 73  olumn.  ** names
35f4f 20 61 72 65 20 75 6e 69 71 75 65 2e 0a 20 20 2a   are unique..  *
35f50 2a 0a 20 20 2a 2a 20 54 68 65 20 41 41 41 41 20  *.  ** The AAAA 
35f51 73 75 66 66 69 78 20 69 73 20 6e 6f 74 20 73 74  suffix is not st
35f52 72 69 63 74 6c 79 20 6e 65 63 65 73 73 61 72 79  rictly necessary
35f53 2e 20 20 49 74 20 69 73 20 69 6e 63 6c 75 64 65  .  It is include
35f54 64 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 63  d.  ** for the c
35f55 6f 6e 76 65 6e 69 65 6e 63 65 20 6f 66 20 70 65  onvenience of pe
35f56 6f 70 6c 65 20 77 68 6f 20 6d 69 67 68 74 20 65  ople who might e
35f57 78 61 6d 69 6e 65 20 74 68 65 20 67 65 6e 65 72  xamine the gener
35f58 61 74 65 64 0a 20 20 2a 2a 20 25 5f 63 6f 6e 74  ated.  ** %_cont
35f59 65 6e 74 20 74 61 62 6c 65 20 61 6e 64 20 77 6f  ent table and wo
35f5a 6e 64 65 72 20 77 68 61 74 20 74 68 65 20 63 6f  nder what the co
35f5b 6c 75 6d 6e 73 20 61 72 65 20 75 73 65 64 20 66  lumns are used f
35f5c 6f 72 2e 0a 20 20 2a 2f 0a 20 20 70 53 70 65 63  or..  */.  pSpec
35f5d 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  ->azContentColum
35f5e 6e 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  n = sqlite3_mall
35f5f 6f 63 28 20 70 53 70 65 63 2d 3e 6e 43 6f 6c 75  oc( pSpec->nColu
35f60 6d 6e 20 2a 20 73 69 7a 65 6f 66 28 63 68 61 72  mn * sizeof(char
35f61 20 2a 29 20 29 3b 0a 20 20 69 66 28 20 70 53 70   *) );.  if( pSp
35f62 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c  ec->azContentCol
35f63 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6c  umn==0 ){.    cl
35f64 65 61 72 54 61 62 6c 65 53 70 65 63 28 70 53 70  earTableSpec(pSp
35f65 65 63 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ec);.    return 
35f66 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
35f67 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  }.  for(i=0; i<p
35f68 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  Spec->nColumn; i
35f69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 70  ++){.    char *p
35f6a 3b 0a 20 20 20 20 70 53 70 65 63 2d 3e 61 7a 43  ;.    pSpec->azC
35f6b 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 5b 69 5d 20  ontentColumn[i] 
35f6c 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
35f6d 66 28 22 63 25 64 25 73 22 2c 20 69 2c 20 61 7a  f("c%d%s", i, az
35f6e 41 72 67 5b 69 5d 29 3b 0a 20 20 20 20 66 6f 72  Arg[i]);.    for
35f6f 20 28 70 20 3d 20 70 53 70 65 63 2d 3e 61 7a 43   (p = pSpec->azC
35f70 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 5b 69 5d 3b  ontentColumn[i];
35f71 20 2a 70 20 3b 20 2b 2b 70 29 20 7b 0a 20 20 20   *p ; ++p) {.   
35f72 20 20 20 69 66 28 20 21 73 61 66 65 5f 69 73 61     if( !safe_isa
35f73 6c 6e 75 6d 28 2a 70 29 20 29 20 2a 70 20 3d 20  lnum(*p) ) *p = 
35f74 27 5f 27 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  '_';.    }.  }..
35f75 20 20 2f 2a 0a 20 20 2a 2a 20 50 61 72 73 65 20    /*.  ** Parse 
35f76 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 73 70  the tokenizer sp
35f77 65 63 69 66 69 63 61 74 69 6f 6e 20 73 74 72 69  ecification stri
35f78 6e 67 2e 0a 20 20 2a 2f 0a 20 20 70 53 70 65 63  ng..  */.  pSpec
35f79 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72 20 3d 20  ->azTokenizer = 
35f7a 74 6f 6b 65 6e 69 7a 65 53 74 72 69 6e 67 28 7a  tokenizeString(z
35f7b 54 6f 6b 65 6e 69 7a 65 72 2c 20 26 6e 29 3b 0a  Tokenizer, &n);.
35f7c 20 20 74 6f 6b 65 6e 4c 69 73 74 54 6f 49 64 4c    tokenListToIdL
35f7d 69 73 74 28 70 53 70 65 63 2d 3e 61 7a 54 6f 6b  ist(pSpec->azTok
35f7e 65 6e 69 7a 65 72 29 3b 0a 0a 20 20 72 65 74 75  enizer);..  retu
35f7f 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
35f80 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
35f81 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  a CREATE TABLE s
35f82 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 64 65  tatement that de
35f83 73 63 72 69 62 65 73 20 74 68 65 20 73 63 68 65  scribes the sche
35f84 6d 61 20 6f 66 0a 2a 2a 20 74 68 65 20 76 69 72  ma of.** the vir
35f85 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 52 65 74  tual table.  Ret
35f86 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
35f87 20 74 68 69 73 20 73 63 68 65 6d 61 20 73 74 72   this schema str
35f88 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65  ing..**.** Space
35f89 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f   is obtained fro
35f8a 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  m sqlite3_mprint
35f8b 66 28 29 20 61 6e 64 20 73 68 6f 75 6c 64 20 62  f() and should b
35f8c 65 20 66 72 65 65 64 0a 2a 2a 20 75 73 69 6e 67  e freed.** using
35f8d 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 2e   sqlite3_free().
35f8e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20  .*/.static char 
35f8f 2a 66 75 6c 6c 74 65 78 74 53 63 68 65 6d 61 28  *fulltextSchema(
35f90 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 2c 20  .  int nColumn, 
35f91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35f92 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f   /* Number of co
35f93 6c 75 6d 6e 73 20 2a 2f 0a 20 20 63 6f 6e 73 74  lumns */.  const
35f94 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 20 61 7a   char *const* az
35f95 43 6f 6c 75 6d 6e 2c 20 20 2f 2a 20 4c 69 73 74  Column,  /* List
35f96 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20   of columns */. 
35f97 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61   const char *zTa
35f98 62 6c 65 4e 61 6d 65 20 20 20 20 20 20 20 20 2f  bleName        /
35f99 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  * Name of the ta
35f9a 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ble */.){.  int 
35f9b 69 3b 0a 20 20 63 68 61 72 20 2a 7a 53 63 68 65  i;.  char *zSche
35f9c 6d 61 2c 20 2a 7a 4e 65 78 74 3b 0a 20 20 63 6f  ma, *zNext;.  co
35f9d 6e 73 74 20 63 68 61 72 20 2a 7a 53 65 70 20 3d  nst char *zSep =
35f9e 20 22 28 22 3b 0a 20 20 7a 53 63 68 65 6d 61 20   "(";.  zSchema 
35f9f 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
35fa0 66 28 22 43 52 45 41 54 45 20 54 41 42 4c 45 20  f("CREATE TABLE 
35fa1 78 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  x");.  for(i=0; 
35fa2 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  i<nColumn; i++){
35fa3 0a 20 20 20 20 7a 4e 65 78 74 20 3d 20 73 71 6c  .    zNext = sql
35fa4 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73  ite3_mprintf("%s
35fa5 25 73 25 51 22 2c 20 7a 53 63 68 65 6d 61 2c 20  %s%Q", zSchema, 
35fa6 7a 53 65 70 2c 20 61 7a 43 6f 6c 75 6d 6e 5b 69  zSep, azColumn[i
35fa7 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ]);.    sqlite3_
35fa8 66 72 65 65 28 7a 53 63 68 65 6d 61 29 3b 0a 20  free(zSchema);. 
35fa9 20 20 20 7a 53 63 68 65 6d 61 20 3d 20 7a 4e 65     zSchema = zNe
35faa 78 74 3b 0a 20 20 20 20 7a 53 65 70 20 3d 20 22  xt;.    zSep = "
35fab 2c 22 3b 0a 20 20 7d 0a 20 20 7a 4e 65 78 74 20  ,";.  }.  zNext 
35fac 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
35fad 66 28 22 25 73 2c 25 51 20 48 49 44 44 45 4e 22  f("%s,%Q HIDDEN"
35fae 2c 20 7a 53 63 68 65 6d 61 2c 20 7a 54 61 62 6c  , zSchema, zTabl
35faf 65 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  eName);.  sqlite
35fb0 33 5f 66 72 65 65 28 7a 53 63 68 65 6d 61 29 3b  3_free(zSchema);
35fb1 0a 20 20 7a 53 63 68 65 6d 61 20 3d 20 7a 4e 65  .  zSchema = zNe
35fb2 78 74 3b 0a 20 20 7a 4e 65 78 74 20 3d 20 73 71  xt;.  zNext = sq
35fb3 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25  lite3_mprintf("%
35fb4 73 2c 64 6f 63 69 64 20 48 49 44 44 45 4e 29 22  s,docid HIDDEN)"
35fb5 2c 20 7a 53 63 68 65 6d 61 29 3b 0a 20 20 73 71  , zSchema);.  sq
35fb6 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 63 68 65  lite3_free(zSche
35fb7 6d 61 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 4e  ma);.  return zN
35fb8 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 75  ext;.}../*.** Bu
35fb9 69 6c 64 20 61 20 6e 65 77 20 73 71 6c 69 74 65  ild a new sqlite
35fba 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65  3_vtab structure
35fbb 20 74 68 61 74 20 77 69 6c 6c 20 64 65 73 63 72   that will descr
35fbc 69 62 65 20 74 68 65 0a 2a 2a 20 66 75 6c 6c 74  ibe the.** fullt
35fbd 65 78 74 20 69 6e 64 65 78 20 64 65 66 69 6e 65  ext index define
35fbe 64 20 62 79 20 73 70 65 63 2e 0a 2a 2f 0a 73 74  d by spec..*/.st
35fbf 61 74 69 63 20 69 6e 74 20 63 6f 6e 73 74 72 75  atic int constru
35fc0 63 74 56 74 61 62 28 0a 20 20 73 71 6c 69 74 65  ctVtab(.  sqlite
35fc1 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
35fc2 20 20 20 20 2f 2a 20 54 68 65 20 53 51 4c 69 74      /* The SQLit
35fc3 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
35fc4 63 74 69 6f 6e 20 2a 2f 0a 20 20 66 74 73 33 48  ction */.  fts3H
35fc5 61 73 68 20 2a 70 48 61 73 68 2c 20 20 20 20 20  ash *pHash,     
35fc6 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62       /* Hash tab
35fc7 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 6f  le containing to
35fc8 6b 65 6e 69 7a 65 72 73 20 2a 2f 0a 20 20 54 61  kenizers */.  Ta
35fc9 62 6c 65 53 70 65 63 20 2a 73 70 65 63 2c 20 20  bleSpec *spec,  
35fca 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
35fcb 64 20 73 70 65 63 20 69 6e 66 6f 72 6d 61 74 69  d spec informati
35fcc 6f 6e 20 66 72 6f 6d 20 70 61 72 73 65 53 70 65  on from parseSpe
35fcd 63 28 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  c() */.  sqlite3
35fce 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 62 2c 20  _vtab **ppVTab, 
35fcf 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
35fd0 72 65 73 75 6c 74 69 6e 67 20 76 74 61 62 20 73  resulting vtab s
35fd1 74 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f  tructure here */
35fd2 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 20  .  char **pzErr 
35fd3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35fd4 57 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20  Write any error 
35fd5 6d 65 73 73 61 67 65 20 68 65 72 65 20 2a 2f 0a  message here */.
35fd6 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  ){.  int rc;.  i
35fd7 6e 74 20 6e 3b 0a 20 20 66 75 6c 6c 74 65 78 74  nt n;.  fulltext
35fd8 5f 76 74 61 62 20 2a 76 20 3d 20 30 3b 0a 20 20  _vtab *v = 0;.  
35fd9 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
35fda 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a  kenizer_module *
35fdb 6d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 63 68 61 72  m = NULL;.  char
35fdc 20 2a 73 63 68 65 6d 61 3b 0a 0a 20 20 63 68 61   *schema;..  cha
35fdd 72 20 63 6f 6e 73 74 20 2a 7a 54 6f 6b 3b 20 20  r const *zTok;  
35fde 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
35fdf 66 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f 20 75  f tokenizer to u
35fe0 73 65 20 66 6f 72 20 74 68 69 73 20 66 74 73 20  se for this fts 
35fe1 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  table */.  int n
35fe2 54 6f 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  Tok;            
35fe3 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
35fe4 66 20 7a 54 6f 6b 2c 20 69 6e 63 6c 75 64 69 6e  f zTok, includin
35fe5 67 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 6f 72  g nul terminator
35fe6 20 2a 2f 0a 0a 20 20 76 20 3d 20 28 66 75 6c 6c   */..  v = (full
35fe7 74 65 78 74 5f 76 74 61 62 20 2a 29 20 73 71 6c  text_vtab *) sql
35fe8 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
35fe9 6f 66 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  of(fulltext_vtab
35fea 29 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  ));.  if( v==0 )
35feb 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
35fec 4f 4d 45 4d 3b 0a 20 20 43 4c 45 41 52 28 76 29  OMEM;.  CLEAR(v)
35fed 3b 0a 20 20 2f 2a 20 73 71 6c 69 74 65 20 77 69  ;.  /* sqlite wi
35fee 6c 6c 20 69 6e 69 74 69 61 6c 69 7a 65 20 76 2d  ll initialize v-
35fef 3e 62 61 73 65 20 2a 2f 0a 20 20 76 2d 3e 64 62  >base */.  v->db
35ff0 20 3d 20 64 62 3b 0a 20 20 76 2d 3e 7a 44 62 20   = db;.  v->zDb 
35ff1 3d 20 73 70 65 63 2d 3e 7a 44 62 3b 20 20 20 20  = spec->zDb;    
35ff2 20 20 20 2f 2a 20 46 72 65 65 64 20 77 68 65 6e     /* Freed when
35ff3 20 61 7a 43 6f 6c 75 6d 6e 20 69 73 20 66 72 65   azColumn is fre
35ff4 65 64 20 2a 2f 0a 20 20 76 2d 3e 7a 4e 61 6d 65  ed */.  v->zName
35ff5 20 3d 20 73 70 65 63 2d 3e 7a 4e 61 6d 65 3b 20   = spec->zName; 
35ff6 20 20 2f 2a 20 46 72 65 65 64 20 77 68 65 6e 20    /* Freed when 
35ff7 61 7a 43 6f 6c 75 6d 6e 20 69 73 20 66 72 65 65  azColumn is free
35ff8 64 20 2a 2f 0a 20 20 76 2d 3e 6e 43 6f 6c 75 6d  d */.  v->nColum
35ff9 6e 20 3d 20 73 70 65 63 2d 3e 6e 43 6f 6c 75 6d  n = spec->nColum
35ffa 6e 3b 0a 20 20 76 2d 3e 61 7a 43 6f 6e 74 65 6e  n;.  v->azConten
35ffb 74 43 6f 6c 75 6d 6e 20 3d 20 73 70 65 63 2d 3e  tColumn = spec->
35ffc 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 3b  azContentColumn;
35ffd 0a 20 20 73 70 65 63 2d 3e 61 7a 43 6f 6e 74 65  .  spec->azConte
35ffe 6e 74 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20  ntColumn = 0;.  
35fff 76 2d 3e 61 7a 43 6f 6c 75 6d 6e 20 3d 20 73 70  v->azColumn = sp
36000 65 63 2d 3e 61 7a 43 6f 6c 75 6d 6e 3b 0a 20 20  ec->azColumn;.  
36001 73 70 65 63 2d 3e 61 7a 43 6f 6c 75 6d 6e 20 3d  spec->azColumn =
36002 20 30 3b 0a 0a 20 20 69 66 28 20 73 70 65 63 2d   0;..  if( spec-
36003 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72 3d 3d 30 20  >azTokenizer==0 
36004 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
36005 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
36006 0a 20 20 7a 54 6f 6b 20 3d 20 73 70 65 63 2d 3e  .  zTok = spec->
36007 61 7a 54 6f 6b 65 6e 69 7a 65 72 5b 30 5d 3b 20  azTokenizer[0]; 
36008 0a 20 20 69 66 28 20 21 7a 54 6f 6b 20 29 7b 0a  .  if( !zTok ){.
36009 20 20 20 20 7a 54 6f 6b 20 3d 20 22 73 69 6d 70      zTok = "simp
3600a 6c 65 22 3b 0a 20 20 7d 0a 20 20 6e 54 6f 6b 20  le";.  }.  nTok 
3600b 3d 20 73 74 72 6c 65 6e 28 7a 54 6f 6b 29 2b 31  = strlen(zTok)+1
3600c 3b 0a 0a 20 20 6d 20 3d 20 28 73 71 6c 69 74 65  ;..  m = (sqlite
3600d 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
3600e 6c 65 20 2a 29 73 71 6c 69 74 65 33 46 74 73 33  le *)sqlite3Fts3
3600f 48 61 73 68 46 69 6e 64 28 70 48 61 73 68 2c 20  HashFind(pHash, 
36010 7a 54 6f 6b 2c 20 6e 54 6f 6b 29 3b 0a 20 20 69  zTok, nTok);.  i
36011 66 28 20 21 6d 20 29 7b 0a 20 20 20 20 2a 70 7a  f( !m ){.    *pz
36012 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Err = sqlite3_mp
36013 72 69 6e 74 66 28 22 75 6e 6b 6e 6f 77 6e 20 74  rintf("unknown t
36014 6f 6b 65 6e 69 7a 65 72 3a 20 25 73 22 2c 20 73  okenizer: %s", s
36015 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72  pec->azTokenizer
36016 5b 30 5d 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  [0]);.    rc = S
36017 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
36018 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20 7d 0a 0a   goto err;.  }..
36019 20 20 66 6f 72 28 6e 3d 30 3b 20 73 70 65 63 2d    for(n=0; spec-
3601a 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72 5b 6e 5d 3b  >azTokenizer[n];
3601b 20 6e 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 6e 20   n++){}.  if( n 
3601c 29 7b 0a 20 20 20 20 72 63 20 3d 20 6d 2d 3e 78  ){.    rc = m->x
3601d 43 72 65 61 74 65 28 6e 2d 31 2c 20 28 63 6f 6e  Create(n-1, (con
3601e 73 74 20 63 68 61 72 2a 63 6f 6e 73 74 2a 29 26  st char*const*)&
3601f 73 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65  spec->azTokenize
36020 72 5b 31 5d 2c 0a 20 20 20 20 20 20 20 20 20 20  r[1],.          
36021 20 20 20 20 20 20 20 20 20 20 26 76 2d 3e 70 54            &v->pT
36022 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 7d 65 6c  okenizer);.  }el
36023 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 6d 2d 3e  se{.    rc = m->
36024 78 43 72 65 61 74 65 28 30 2c 20 30 2c 20 26 76  xCreate(0, 0, &v
36025 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20  ->pTokenizer);. 
36026 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c   }.  if( rc!=SQL
36027 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72  ITE_OK ) goto er
36028 72 3b 0a 20 20 76 2d 3e 70 54 6f 6b 65 6e 69 7a  r;.  v->pTokeniz
36029 65 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 6d 3b  er->pModule = m;
3602a 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 76 65 72  ..  /* TODO: ver
3602b 69 66 79 20 74 68 65 20 65 78 69 73 74 65 6e 63  ify the existenc
3602c 65 20 6f 66 20 62 61 63 6b 69 6e 67 20 74 61 62  e of backing tab
3602d 6c 65 73 20 66 6f 6f 5f 63 6f 6e 74 65 6e 74 2c  les foo_content,
3602e 20 66 6f 6f 5f 74 65 72 6d 20 2a 2f 0a 0a 20 20   foo_term */..  
3602f 73 63 68 65 6d 61 20 3d 20 66 75 6c 6c 74 65 78  schema = fulltex
36030 74 53 63 68 65 6d 61 28 76 2d 3e 6e 43 6f 6c 75  tSchema(v->nColu
36031 6d 6e 2c 20 28 63 6f 6e 73 74 20 63 68 61 72 2a  mn, (const char*
36032 63 6f 6e 73 74 2a 29 76 2d 3e 61 7a 43 6f 6c 75  const*)v->azColu
36033 6d 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  mn,.            
36034 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 70                sp
36035 65 63 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 72 63  ec->zName);.  rc
36036 20 3d 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61   = sqlite3_decla
36037 72 65 5f 76 74 61 62 28 64 62 2c 20 73 63 68 65  re_vtab(db, sche
36038 6d 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ma);.  sqlite3_f
36039 72 65 65 28 73 63 68 65 6d 61 29 3b 0a 20 20 69  ree(schema);.  i
3603a 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3603b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20   ) goto err;..  
3603c 6d 65 6d 73 65 74 28 76 2d 3e 70 46 75 6c 6c 74  memset(v->pFullt
3603d 65 78 74 53 74 61 74 65 6d 65 6e 74 73 2c 20 30  extStatements, 0
3603e 2c 20 73 69 7a 65 6f 66 28 76 2d 3e 70 46 75 6c  , sizeof(v->pFul
3603f 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73 29  ltextStatements)
36040 29 3b 0a 0a 20 20 2f 2a 20 49 6e 64 69 63 61 74  );..  /* Indicat
36041 65 20 74 68 61 74 20 74 68 65 20 62 75 66 66 65  e that the buffe
36042 72 20 69 73 20 6e 6f 74 20 6c 69 76 65 2e 20 2a  r is not live. *
36043 2f 0a 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44  /.  v->nPendingD
36044 61 74 61 20 3d 20 2d 31 3b 0a 0a 20 20 2a 70 70  ata = -1;..  *pp
36045 56 54 61 62 20 3d 20 26 76 2d 3e 62 61 73 65 3b  VTab = &v->base;
36046 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54  .  FTSTRACE(("FT
36047 53 33 20 43 6f 6e 6e 65 63 74 20 25 70 5c 6e 22  S3 Connect %p\n"
36048 2c 20 76 29 29 3b 0a 0a 20 20 72 65 74 75 72 6e  , v));..  return
36049 20 72 63 3b 0a 0a 65 72 72 3a 0a 20 20 66 75 6c   rc;..err:.  ful
3604a 6c 74 65 78 74 5f 76 74 61 62 5f 64 65 73 74 72  ltext_vtab_destr
3604b 6f 79 28 76 29 3b 0a 20 20 72 65 74 75 72 6e 20  oy(v);.  return 
3604c 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  rc;.}..static in
3604d 74 20 66 75 6c 6c 74 65 78 74 43 6f 6e 6e 65 63  t fulltextConnec
3604e 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  t(.  sqlite3 *db
3604f 2c 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a  ,.  void *pAux,.
36050 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73    int argc, cons
36051 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72  t char *const*ar
36052 67 76 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  gv,.  sqlite3_vt
36053 61 62 20 2a 2a 70 70 56 54 61 62 2c 0a 20 20 63  ab **ppVTab,.  c
36054 68 61 72 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20  har **pzErr.){. 
36055 20 54 61 62 6c 65 53 70 65 63 20 73 70 65 63 3b   TableSpec spec;
36056 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 61 72 73  .  int rc = pars
36057 65 53 70 65 63 28 26 73 70 65 63 2c 20 61 72 67  eSpec(&spec, arg
36058 63 2c 20 61 72 67 76 2c 20 70 7a 45 72 72 29 3b  c, argv, pzErr);
36059 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
3605a 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
3605b 3b 0a 0a 20 20 72 63 20 3d 20 63 6f 6e 73 74 72  ;..  rc = constr
3605c 75 63 74 56 74 61 62 28 64 62 2c 20 28 66 74 73  uctVtab(db, (fts
3605d 33 48 61 73 68 20 2a 29 70 41 75 78 2c 20 26 73  3Hash *)pAux, &s
3605e 70 65 63 2c 20 70 70 56 54 61 62 2c 20 70 7a 45  pec, ppVTab, pzE
3605f 72 72 29 3b 0a 20 20 63 6c 65 61 72 54 61 62 6c  rr);.  clearTabl
36060 65 53 70 65 63 28 26 73 70 65 63 29 3b 0a 20 20  eSpec(&spec);.  
36061 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
36062 20 54 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74   The %_content t
36063 61 62 6c 65 20 68 6f 6c 64 73 20 74 68 65 20 74  able holds the t
36064 65 78 74 20 6f 66 20 65 61 63 68 20 64 6f 63 75  ext of each docu
36065 6d 65 6e 74 2c 20 77 69 74 68 0a 2a 2a 20 74 68  ment, with.** th
36066 65 20 64 6f 63 69 64 20 63 6f 6c 75 6d 6e 20 65  e docid column e
36067 78 70 6f 73 65 64 20 61 73 20 74 68 65 20 53 51  xposed as the SQ
36068 4c 69 74 65 20 72 6f 77 69 64 20 66 6f 72 20 74  Lite rowid for t
36069 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20  he table..*/./* 
3606a 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68 69 73  TODO(shess) This
3606b 20 63 6f 6d 6d 65 6e 74 20 6e 65 65 64 73 20 65   comment needs e
3606c 6c 61 62 6f 72 61 74 69 6f 6e 20 74 6f 20 6d 61  laboration to ma
3606d 74 63 68 20 74 68 65 20 75 70 64 61 74 65 64 0a  tch the updated.
3606e 2a 2a 20 63 6f 64 65 2e 20 20 57 6f 72 6b 20 69  ** code.  Work i
3606f 74 20 69 6e 74 6f 20 74 68 65 20 74 6f 70 2d 6f  t into the top-o
36070 66 2d 66 69 6c 65 20 63 6f 6d 6d 65 6e 74 20 61  f-file comment a
36071 74 20 74 68 61 74 20 74 69 6d 65 2e 0a 2a 2f 0a  t that time..*/.
36072 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74  static int fullt
36073 65 78 74 43 72 65 61 74 65 28 73 71 6c 69 74 65  extCreate(sqlite
36074 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 41 75  3 *db, void *pAu
36075 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  x,.             
36076 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
36077 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61   argc, const cha
36078 72 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c  r * const *argv,
36079 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3607a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
3607b 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 62  e3_vtab **ppVTab
3607c 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 29 7b  , char **pzErr){
3607d 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 54 61 62  .  int rc;.  Tab
3607e 6c 65 53 70 65 63 20 73 70 65 63 3b 0a 20 20 53  leSpec spec;.  S
3607f 74 72 69 6e 67 42 75 66 66 65 72 20 73 63 68 65  tringBuffer sche
36080 6d 61 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28  ma;.  FTSTRACE((
36081 22 46 54 53 33 20 43 72 65 61 74 65 5c 6e 22 29  "FTS3 Create\n")
36082 29 3b 0a 0a 20 20 72 63 20 3d 20 70 61 72 73 65  );..  rc = parse
36083 53 70 65 63 28 26 73 70 65 63 2c 20 61 72 67 63  Spec(&spec, argc
36084 2c 20 61 72 67 76 2c 20 70 7a 45 72 72 29 3b 0a  , argv, pzErr);.
36085 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
36086 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
36087 0a 0a 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75  ..  initStringBu
36088 66 66 65 72 28 26 73 63 68 65 6d 61 29 3b 0a 20  ffer(&schema);. 
36089 20 61 70 70 65 6e 64 28 26 73 63 68 65 6d 61 2c   append(&schema,
3608a 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 25   "CREATE TABLE %
3608b 5f 63 6f 6e 74 65 6e 74 28 22 29 3b 0a 20 20 61  _content(");.  a
3608c 70 70 65 6e 64 28 26 73 63 68 65 6d 61 2c 20 22  ppend(&schema, "
3608d 20 20 64 6f 63 69 64 20 49 4e 54 45 47 45 52 20    docid INTEGER 
3608e 50 52 49 4d 41 52 59 20 4b 45 59 2c 22 29 3b 0a  PRIMARY KEY,");.
3608f 20 20 61 70 70 65 6e 64 4c 69 73 74 28 26 73 63    appendList(&sc
36090 68 65 6d 61 2c 20 73 70 65 63 2e 6e 43 6f 6c 75  hema, spec.nColu
36091 6d 6e 2c 20 73 70 65 63 2e 61 7a 43 6f 6e 74 65  mn, spec.azConte
36092 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20 61 70 70  ntColumn);.  app
36093 65 6e 64 28 26 73 63 68 65 6d 61 2c 20 22 29 22  end(&schema, ")"
36094 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 5f 65 78  );.  rc = sql_ex
36095 65 63 28 64 62 2c 20 73 70 65 63 2e 7a 44 62 2c  ec(db, spec.zDb,
36096 20 73 70 65 63 2e 7a 4e 61 6d 65 2c 20 73 74 72   spec.zName, str
36097 69 6e 67 42 75 66 66 65 72 44 61 74 61 28 26 73  ingBufferData(&s
36098 63 68 65 6d 61 29 29 3b 0a 20 20 73 74 72 69 6e  chema));.  strin
36099 67 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  gBufferDestroy(&
3609a 73 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 20 72  schema);.  if( r
3609b 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
3609c 6f 74 6f 20 6f 75 74 3b 0a 0a 20 20 72 63 20 3d  oto out;..  rc =
3609d 20 73 71 6c 5f 65 78 65 63 28 64 62 2c 20 73 70   sql_exec(db, sp
3609e 65 63 2e 7a 44 62 2c 20 73 70 65 63 2e 7a 4e 61  ec.zDb, spec.zNa
3609f 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  me,.            
360a0 20 20 20 20 22 63 72 65 61 74 65 20 74 61 62 6c      "create tabl
360a1 65 20 25 5f 73 65 67 6d 65 6e 74 73 28 22 0a 20  e %_segments(". 
360a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
360a3 20 20 62 6c 6f 63 6b 69 64 20 49 4e 54 45 47 45    blockid INTEGE
360a4 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 22 0a  R PRIMARY KEY,".
360a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
360a6 22 20 20 62 6c 6f 63 6b 20 62 6c 6f 62 22 0a 20  "  block blob". 
360a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
360a8 29 3b 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  );".            
360a9 20 20 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21      );.  if( rc!
360aa 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
360ab 6f 20 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20 73  o out;..  rc = s
360ac 71 6c 5f 65 78 65 63 28 64 62 2c 20 73 70 65 63  ql_exec(db, spec
360ad 2e 7a 44 62 2c 20 73 70 65 63 2e 7a 4e 61 6d 65  .zDb, spec.zName
360ae 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
360af 20 20 22 63 72 65 61 74 65 20 74 61 62 6c 65 20    "create table 
360b0 25 5f 73 65 67 64 69 72 28 22 0a 20 20 20 20 20  %_segdir(".     
360b1 20 20 20 20 20 20 20 20 20 20 20 22 20 20 6c 65             "  le
360b2 76 65 6c 20 69 6e 74 65 67 65 72 2c 22 0a 20 20  vel integer,".  
360b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 20                " 
360b4 20 69 64 78 20 69 6e 74 65 67 65 72 2c 22 0a 20   idx integer,". 
360b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
360b6 20 20 73 74 61 72 74 5f 62 6c 6f 63 6b 20 69 6e    start_block in
360b7 74 65 67 65 72 2c 22 0a 20 20 20 20 20 20 20 20  teger,".        
360b8 20 20 20 20 20 20 20 20 22 20 20 6c 65 61 76 65          "  leave
360b9 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 69 6e 74 65  s_end_block inte
360ba 67 65 72 2c 22 0a 20 20 20 20 20 20 20 20 20 20  ger,".          
360bb 20 20 20 20 20 20 22 20 20 65 6e 64 5f 62 6c 6f        "  end_blo
360bc 63 6b 20 69 6e 74 65 67 65 72 2c 22 0a 20 20 20  ck integer,".   
360bd 20 20 20 20 20 20 20 20 20 20 20 20 20 22 20 20               "  
360be 72 6f 6f 74 20 62 6c 6f 62 2c 22 0a 20 20 20 20  root blob,".    
360bf 20 20 20 20 20 20 20 20 20 20 20 20 22 20 20 70              "  p
360c0 72 69 6d 61 72 79 20 6b 65 79 28 6c 65 76 65 6c  rimary key(level
360c1 2c 20 69 64 78 29 22 0a 20 20 20 20 20 20 20 20  , idx)".        
360c2 20 20 20 20 20 20 20 20 22 29 3b 22 29 3b 0a 20          ");");. 
360c3 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
360c4 4f 4b 20 29 20 67 6f 74 6f 20 6f 75 74 3b 0a 0a  OK ) goto out;..
360c5 20 20 72 63 20 3d 20 63 6f 6e 73 74 72 75 63 74    rc = construct
360c6 56 74 61 62 28 64 62 2c 20 28 66 74 73 33 48 61  Vtab(db, (fts3Ha
360c7 73 68 20 2a 29 70 41 75 78 2c 20 26 73 70 65 63  sh *)pAux, &spec
360c8 2c 20 70 70 56 54 61 62 2c 20 70 7a 45 72 72 29  , ppVTab, pzErr)
360c9 3b 0a 0a 6f 75 74 3a 0a 20 20 63 6c 65 61 72 54  ;..out:.  clearT
360ca 61 62 6c 65 53 70 65 63 28 26 73 70 65 63 29 3b  ableSpec(&spec);
360cb 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
360cc 0a 2f 2a 20 44 65 63 69 64 65 20 68 6f 77 20 74  ./* Decide how t
360cd 6f 20 68 61 6e 64 6c 65 20 61 6e 20 53 51 4c 20  o handle an SQL 
360ce 71 75 65 72 79 2e 20 2a 2f 0a 73 74 61 74 69 63  query. */.static
360cf 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 42 65 73   int fulltextBes
360d0 74 49 6e 64 65 78 28 73 71 6c 69 74 65 33 5f 76  tIndex(sqlite3_v
360d1 74 61 62 20 2a 70 56 54 61 62 2c 20 73 71 6c 69  tab *pVTab, sqli
360d2 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
360d3 70 49 6e 66 6f 29 7b 0a 20 20 66 75 6c 6c 74 65  pInfo){.  fullte
360d4 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 28 66 75  xt_vtab *v = (fu
360d5 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 70 56  lltext_vtab *)pV
360d6 54 61 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  Tab;.  int i;.  
360d7 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20  FTSTRACE(("FTS3 
360d8 42 65 73 74 49 6e 64 65 78 5c 6e 22 29 29 3b 0a  BestIndex\n"));.
360d9 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49  .  for(i=0; i<pI
360da 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74  nfo->nConstraint
360db 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 63 6f 6e 73  ; ++i){.    cons
360dc 74 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  t struct sqlite3
360dd 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
360de 74 20 2a 70 43 6f 6e 73 74 72 61 69 6e 74 3b 0a  t *pConstraint;.
360df 20 20 20 20 70 43 6f 6e 73 74 72 61 69 6e 74 20      pConstraint 
360e0 3d 20 26 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74  = &pInfo->aConst
360e1 72 61 69 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66  raint[i];.    if
360e2 28 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 75  ( pConstraint->u
360e3 73 61 62 6c 65 20 29 20 7b 0a 20 20 20 20 20 20  sable ) {.      
360e4 69 66 28 20 28 70 43 6f 6e 73 74 72 61 69 6e 74  if( (pConstraint
360e5 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 2d 31 20 7c 7c  ->iColumn==-1 ||
360e6 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43   pConstraint->iC
360e7 6f 6c 75 6d 6e 3d 3d 76 2d 3e 6e 43 6f 6c 75 6d  olumn==v->nColum
360e8 6e 2b 31 29 20 26 26 0a 20 20 20 20 20 20 20 20  n+1) &&.        
360e9 20 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 6f    pConstraint->o
360ea 70 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  p==SQLITE_INDEX_
360eb 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20 29 7b  CONSTRAINT_EQ ){
360ec 0a 20 20 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e  .        pInfo->
360ed 69 64 78 4e 75 6d 20 3d 20 51 55 45 52 59 5f 44  idxNum = QUERY_D
360ee 4f 43 49 44 3b 20 20 20 20 20 20 2f 2a 20 6c 6f  OCID;      /* lo
360ef 6f 6b 75 70 20 62 79 20 64 6f 63 69 64 20 2a 2f  okup by docid */
360f0 0a 20 20 20 20 20 20 20 20 46 54 53 54 52 41 43  .        FTSTRAC
360f1 45 28 28 22 46 54 53 33 20 51 55 45 52 59 5f 44  E(("FTS3 QUERY_D
360f2 4f 43 49 44 5c 6e 22 29 29 3b 0a 20 20 20 20 20  OCID\n"));.     
360f3 20 7d 20 65 6c 73 65 20 69 66 28 20 70 43 6f 6e   } else if( pCon
360f4 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e  straint->iColumn
360f5 3e 3d 30 20 26 26 20 70 43 6f 6e 73 74 72 61 69  >=0 && pConstrai
360f6 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 3c 3d 76 2d 3e  nt->iColumn<=v->
360f7 6e 43 6f 6c 75 6d 6e 20 26 26 0a 20 20 20 20 20  nColumn &&.     
360f8 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6e              pCon
360f9 73 74 72 61 69 6e 74 2d 3e 6f 70 3d 3d 53 51 4c  straint->op==SQL
360fa 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
360fb 41 49 4e 54 5f 4d 41 54 43 48 20 29 7b 0a 20 20  AINT_MATCH ){.  
360fc 20 20 20 20 20 20 2f 2a 20 66 75 6c 6c 2d 74 65        /* full-te
360fd 78 74 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 20  xt search */.   
360fe 20 20 20 20 20 70 49 6e 66 6f 2d 3e 69 64 78 4e       pInfo->idxN
360ff 75 6d 20 3d 20 51 55 45 52 59 5f 46 55 4c 4c 54  um = QUERY_FULLT
36100 45 58 54 20 2b 20 70 43 6f 6e 73 74 72 61 69 6e  EXT + pConstrain
36101 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  t->iColumn;.    
36102 20 20 20 20 46 54 53 54 52 41 43 45 28 28 22 46      FTSTRACE(("F
36103 54 53 33 20 51 55 45 52 59 5f 46 55 4c 4c 54 45  TS3 QUERY_FULLTE
36104 58 54 20 25 64 5c 6e 22 2c 20 70 43 6f 6e 73 74  XT %d\n", pConst
36105 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 29 29  raint->iColumn))
36106 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 63  ;.      } else c
36107 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20 20 20  ontinue;..      
36108 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  pInfo->aConstrai
36109 6e 74 55 73 61 67 65 5b 69 5d 2e 61 72 67 76 49  ntUsage[i].argvI
3610a 6e 64 65 78 20 3d 20 31 3b 0a 20 20 20 20 20 20  ndex = 1;.      
3610b 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  pInfo->aConstrai
3610c 6e 74 55 73 61 67 65 5b 69 5d 2e 6f 6d 69 74 20  ntUsage[i].omit 
3610d 3d 20 31 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 41  = 1;..      /* A
3610e 6e 20 61 72 62 69 74 72 61 72 79 20 76 61 6c 75  n arbitrary valu
3610f 65 20 66 6f 72 20 6e 6f 77 2e 0a 20 20 20 20 20  e for now..     
36110 20 20 2a 20 54 4f 44 4f 3a 20 50 65 72 68 61 70    * TODO: Perhap
36111 73 20 64 6f 63 69 64 20 6d 61 74 63 68 65 73 20  s docid matches 
36112 73 68 6f 75 6c 64 20 62 65 20 63 6f 6e 73 69 64  should be consid
36113 65 72 65 64 20 63 68 65 61 70 65 72 20 74 68 61  ered cheaper tha
36114 6e 0a 20 20 20 20 20 20 20 2a 20 66 75 6c 6c 2d  n.       * full-
36115 74 65 78 74 20 73 65 61 72 63 68 65 73 2e 20 2a  text searches. *
36116 2f 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 65  /.      pInfo->e
36117 73 74 69 6d 61 74 65 64 43 6f 73 74 20 3d 20 31  stimatedCost = 1
36118 2e 30 3b 20 20 20 0a 0a 20 20 20 20 20 20 72 65  .0;   ..      re
36119 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
3611a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 49 6e 66      }.  }.  pInf
3611b 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 51 55 45 52  o->idxNum = QUER
3611c 59 5f 47 45 4e 45 52 49 43 3b 0a 20 20 72 65 74  Y_GENERIC;.  ret
3611d 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
3611e 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c  ..static int ful
3611f 6c 74 65 78 74 44 69 73 63 6f 6e 6e 65 63 74 28  ltextDisconnect(
36120 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
36121 54 61 62 29 7b 0a 20 20 46 54 53 54 52 41 43 45  Tab){.  FTSTRACE
36122 28 28 22 46 54 53 33 20 44 69 73 63 6f 6e 6e 65  (("FTS3 Disconne
36123 63 74 20 25 70 5c 6e 22 2c 20 70 56 54 61 62 29  ct %p\n", pVTab)
36124 29 3b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74  );.  fulltext_vt
36125 61 62 5f 64 65 73 74 72 6f 79 28 28 66 75 6c 6c  ab_destroy((full
36126 74 65 78 74 5f 76 74 61 62 20 2a 29 70 56 54 61  text_vtab *)pVTa
36127 62 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  b);.  return SQL
36128 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69  ITE_OK;.}..stati
36129 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 44 65  c int fulltextDe
3612a 73 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 76 74  stroy(sqlite3_vt
3612b 61 62 20 2a 70 56 54 61 62 29 7b 0a 20 20 66 75  ab *pVTab){.  fu
3612c 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d  lltext_vtab *v =
3612d 20 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20   (fulltext_vtab 
3612e 2a 29 70 56 54 61 62 3b 0a 20 20 69 6e 74 20 72  *)pVTab;.  int r
3612f 63 3b 0a 0a 20 20 46 54 53 54 52 41 43 45 28 28  c;..  FTSTRACE((
36130 22 46 54 53 33 20 44 65 73 74 72 6f 79 20 25 70  "FTS3 Destroy %p
36131 5c 6e 22 2c 20 70 56 54 61 62 29 29 3b 0a 20 20  \n", pVTab));.  
36132 72 63 20 3d 20 73 71 6c 5f 65 78 65 63 28 76 2d  rc = sql_exec(v-
36133 3e 64 62 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e  >db, v->zDb, v->
36134 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20  zName,.         
36135 20 20 20 20 20 20 20 22 64 72 6f 70 20 74 61 62         "drop tab
36136 6c 65 20 69 66 20 65 78 69 73 74 73 20 25 5f 63  le if exists %_c
36137 6f 6e 74 65 6e 74 3b 22 0a 20 20 20 20 20 20 20  ontent;".       
36138 20 20 20 20 20 20 20 20 20 22 64 72 6f 70 20 74           "drop t
36139 61 62 6c 65 20 69 66 20 65 78 69 73 74 73 20 25  able if exists %
3613a 5f 73 65 67 6d 65 6e 74 73 3b 22 0a 20 20 20 20  _segments;".    
3613b 20 20 20 20 20 20 20 20 20 20 20 20 22 64 72 6f              "dro
3613c 70 20 74 61 62 6c 65 20 69 66 20 65 78 69 73 74  p table if exist
3613d 73 20 25 5f 73 65 67 64 69 72 3b 22 0a 20 20 20  s %_segdir;".   
3613e 20 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a               );.
3613f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
36140 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
36141 0a 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61  ..  fulltext_vta
36142 62 5f 64 65 73 74 72 6f 79 28 28 66 75 6c 6c 74  b_destroy((fullt
36143 65 78 74 5f 76 74 61 62 20 2a 29 70 56 54 61 62  ext_vtab *)pVTab
36144 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
36145 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63  TE_OK;.}..static
36146 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 4f 70 65   int fulltextOpe
36147 6e 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  n(sqlite3_vtab *
36148 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 76  pVTab, sqlite3_v
36149 74 61 62 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43  tab_cursor **ppC
3614a 75 72 73 6f 72 29 7b 0a 20 20 66 75 6c 6c 74 65  ursor){.  fullte
3614b 78 74 5f 63 75 72 73 6f 72 20 2a 63 3b 0a 0a 20  xt_cursor *c;.. 
3614c 20 63 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63   c = (fulltext_c
3614d 75 72 73 6f 72 20 2a 29 20 73 71 6c 69 74 65 33  ursor *) sqlite3
3614e 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 66  _malloc(sizeof(f
3614f 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 29 29  ulltext_cursor))
36150 3b 0a 20 20 69 66 28 20 63 20 29 7b 0a 20 20 20  ;.  if( c ){.   
36151 20 6d 65 6d 73 65 74 28 63 2c 20 30 2c 20 73 69   memset(c, 0, si
36152 7a 65 6f 66 28 66 75 6c 6c 74 65 78 74 5f 63 75  zeof(fulltext_cu
36153 72 73 6f 72 29 29 3b 0a 20 20 20 20 2f 2a 20 73  rsor));.    /* s
36154 71 6c 69 74 65 20 77 69 6c 6c 20 69 6e 69 74 69  qlite will initi
36155 61 6c 69 7a 65 20 63 2d 3e 62 61 73 65 20 2a 2f  alize c->base */
36156 0a 20 20 20 20 2a 70 70 43 75 72 73 6f 72 20 3d  .    *ppCursor =
36157 20 26 63 2d 3e 62 61 73 65 3b 0a 20 20 20 20 46   &c->base;.    F
36158 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20 4f  TSTRACE(("FTS3 O
36159 70 65 6e 20 25 70 3a 20 25 70 5c 6e 22 2c 20 70  pen %p: %p\n", p
3615a 56 54 61 62 2c 20 63 29 29 3b 0a 20 20 20 20 72  VTab, c));.    r
3615b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
3615c 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
3615d 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
3615e 4d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 46 72 65  M;.  }.}../* Fre
3615f 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 64 79 6e  e all of the dyn
36160 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74  amically allocat
36161 65 64 20 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62  ed memory held b
36162 79 20 74 68 65 0a 2a 2a 20 53 6e 69 70 70 65 74  y the.** Snippet
36163 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
36164 73 6e 69 70 70 65 74 43 6c 65 61 72 28 53 6e 69  snippetClear(Sni
36165 70 70 65 74 20 2a 70 29 7b 0a 20 20 73 71 6c 69  ppet *p){.  sqli
36166 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 4d 61 74  te3_free(p->aMat
36167 63 68 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ch);.  sqlite3_f
36168 72 65 65 28 70 2d 3e 7a 4f 66 66 73 65 74 29 3b  ree(p->zOffset);
36169 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
3616a 70 2d 3e 7a 53 6e 69 70 70 65 74 29 3b 0a 20 20  p->zSnippet);.  
3616b 43 4c 45 41 52 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a  CLEAR(p);.}../*.
3616c 2a 2a 20 41 70 70 65 6e 64 20 61 20 73 69 6e 67  ** Append a sing
3616d 6c 65 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20  le entry to the 
3616e 70 2d 3e 61 4d 61 74 63 68 5b 5d 20 6c 6f 67 2e  p->aMatch[] log.
3616f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
36170 73 6e 69 70 70 65 74 41 70 70 65 6e 64 4d 61 74  snippetAppendMat
36171 63 68 28 0a 20 20 53 6e 69 70 70 65 74 20 2a 70  ch(.  Snippet *p
36172 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
36173 2f 2a 20 41 70 70 65 6e 64 20 74 68 65 20 65 6e  /* Append the en
36174 74 72 79 20 74 6f 20 74 68 69 73 20 73 6e 69 70  try to this snip
36175 70 65 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f  pet */.  int iCo
36176 6c 2c 20 69 6e 74 20 69 54 65 72 6d 2c 20 20 20  l, int iTerm,   
36177 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e     /* The column
36178 20 61 6e 64 20 71 75 65 72 79 20 74 65 72 6d 20   and query term 
36179 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e 2c  */.  int iToken,
3617a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3617b 2a 20 4d 61 74 63 68 69 6e 67 20 74 6f 6b 65 6e  * Matching token
3617c 20 69 6e 20 64 6f 63 75 6d 65 6e 74 20 2a 2f 0a   in document */.
3617d 20 20 69 6e 74 20 69 53 74 61 72 74 2c 20 69 6e    int iStart, in
3617e 74 20 6e 42 79 74 65 20 20 20 20 20 2f 2a 20 4f  t nByte     /* O
3617f 66 66 73 65 74 20 61 6e 64 20 73 69 7a 65 20 6f  ffset and size o
36180 66 20 74 68 65 20 6d 61 74 63 68 20 2a 2f 0a 29  f the match */.)
36181 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  {.  int i;.  str
36182 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68  uct snippetMatch
36183 20 2a 70 4d 61 74 63 68 3b 0a 20 20 69 66 28 20   *pMatch;.  if( 
36184 70 2d 3e 6e 4d 61 74 63 68 2b 31 3e 3d 70 2d 3e  p->nMatch+1>=p->
36185 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 70 2d  nAlloc ){.    p-
36186 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 41 6c  >nAlloc = p->nAl
36187 6c 6f 63 2a 32 20 2b 20 31 30 3b 0a 20 20 20 20  loc*2 + 10;.    
36188 70 2d 3e 61 4d 61 74 63 68 20 3d 20 73 71 6c 69  p->aMatch = sqli
36189 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 2d 3e 61  te3_realloc(p->a
3618a 4d 61 74 63 68 2c 20 70 2d 3e 6e 41 6c 6c 6f 63  Match, p->nAlloc
3618b 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 4d 61 74 63  *sizeof(p->aMatc
3618c 68 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69 66 28  h[0]) );.    if(
3618d 20 70 2d 3e 61 4d 61 74 63 68 3d 3d 30 20 29 7b   p->aMatch==0 ){
3618e 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 61 74 63 68  .      p->nMatch
3618f 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 6e   = 0;.      p->n
36190 41 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 20  Alloc = 0;.     
36191 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
36192 20 7d 0a 20 20 69 20 3d 20 70 2d 3e 6e 4d 61 74   }.  i = p->nMat
36193 63 68 2b 2b 3b 0a 20 20 70 4d 61 74 63 68 20 3d  ch++;.  pMatch =
36194 20 26 70 2d 3e 61 4d 61 74 63 68 5b 69 5d 3b 0a   &p->aMatch[i];.
36195 20 20 70 4d 61 74 63 68 2d 3e 69 43 6f 6c 20 3d    pMatch->iCol =
36196 20 69 43 6f 6c 3b 0a 20 20 70 4d 61 74 63 68 2d   iCol;.  pMatch-
36197 3e 69 54 65 72 6d 20 3d 20 69 54 65 72 6d 3b 0a  >iTerm = iTerm;.
36198 20 20 70 4d 61 74 63 68 2d 3e 69 54 6f 6b 65 6e    pMatch->iToken
36199 20 3d 20 69 54 6f 6b 65 6e 3b 0a 20 20 70 4d 61   = iToken;.  pMa
3619a 74 63 68 2d 3e 69 53 74 61 72 74 20 3d 20 69 53  tch->iStart = iS
3619b 74 61 72 74 3b 0a 20 20 70 4d 61 74 63 68 2d 3e  tart;.  pMatch->
3619c 6e 42 79 74 65 20 3d 20 6e 42 79 74 65 3b 0a 7d  nByte = nByte;.}
3619d 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 69 6e 67 20 69  ../*.** Sizing i
3619e 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74  nformation for t
3619f 68 65 20 63 69 72 63 75 6c 61 72 20 62 75 66 66  he circular buff
361a0 65 72 20 75 73 65 64 20 69 6e 20 73 6e 69 70 70  er used in snipp
361a1 65 74 4f 66 66 73 65 74 73 4f 66 43 6f 6c 75 6d  etOffsetsOfColum
361a2 6e 28 29 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46  n().*/.#define F
361a3 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 20 20 20 28  TS3_ROTOR_SZ   (
361a4 33 32 29 0a 23 64 65 66 69 6e 65 20 46 54 53 33  32).#define FTS3
361a5 5f 52 4f 54 4f 52 5f 4d 41 53 4b 20 28 46 54 53  _ROTOR_MASK (FTS
361a6 33 5f 52 4f 54 4f 52 5f 53 5a 2d 31 29 0a 0a 2f  3_ROTOR_SZ-1)../
361a7 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f  *.** Function to
361a8 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
361a9 20 74 68 65 20 74 6f 6b 65 6e 73 20 6f 66 20 61   the tokens of a
361aa 20 63 6f 6d 70 69 6c 65 64 20 65 78 70 72 65 73   compiled expres
361ab 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78 63 65  sion..**.** Exce
361ac 70 74 2c 20 73 6b 69 70 20 61 6c 6c 20 74 6f 6b  pt, skip all tok
361ad 65 6e 73 20 6f 6e 20 74 68 65 20 72 69 67 68 74  ens on the right
361ae 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 61 20  -hand side of a 
361af 4e 4f 54 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a  NOT operator..**
361b0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
361b1 73 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20 74  s used to find t
361b2 6f 6b 65 6e 73 20 61 73 20 70 61 72 74 20 6f 66  okens as part of
361b3 20 73 6e 69 70 70 65 74 20 61 6e 64 20 6f 66 66   snippet and off
361b4 73 65 74 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6f  set.** generatio
361b5 6e 20 61 6e 64 20 77 65 20 64 6f 20 6e 74 20 77  n and we do nt w
361b6 61 6e 74 20 73 6e 69 70 70 65 74 73 20 61 6e 64  ant snippets and
361b7 20 6f 66 66 73 65 74 73 20 74 6f 20 72 65 70 6f   offsets to repo
361b8 72 74 20 6d 61 74 63 68 65 73 0a 2a 2a 20 66 6f  rt matches.** fo
361b9 72 20 74 6f 6b 65 6e 73 20 6f 6e 20 74 68 65 20  r tokens on the 
361ba 52 48 53 20 6f 66 20 61 20 4e 4f 54 2e 0a 2a 2f  RHS of a NOT..*/
361bb 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
361bc 4e 65 78 74 45 78 70 72 54 6f 6b 65 6e 28 46 74  NextExprToken(Ft
361bd 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70 72 2c  s3Expr **ppExpr,
361be 20 69 6e 74 20 2a 70 69 54 6f 6b 65 6e 29 7b 0a   int *piToken){.
361bf 20 20 46 74 73 33 45 78 70 72 20 2a 70 20 3d 20    Fts3Expr *p = 
361c0 2a 70 70 45 78 70 72 3b 0a 20 20 69 6e 74 20 69  *ppExpr;.  int i
361c1 54 6f 6b 65 6e 20 3d 20 2a 70 69 54 6f 6b 65 6e  Token = *piToken
361c2 3b 0a 20 20 69 66 28 20 69 54 6f 6b 65 6e 3c 30  ;.  if( iToken<0
361c3 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68   ){.    /* In th
361c4 69 73 20 63 61 73 65 20 74 68 65 20 65 78 70 72  is case the expr
361c5 65 73 73 69 6f 6e 20 70 20 69 73 20 74 68 65 20  ession p is the 
361c6 72 6f 6f 74 20 6f 66 20 61 6e 20 65 78 70 72 65  root of an expre
361c7 73 73 69 6f 6e 20 74 72 65 65 2e 0a 20 20 20 20  ssion tree..    
361c8 2a 2a 20 4d 6f 76 65 20 74 6f 20 74 68 65 20 66  ** Move to the f
361c9 69 72 73 74 20 74 6f 6b 65 6e 20 69 6e 20 74 68  irst token in th
361ca 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  e expression tre
361cb 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68  e..    */.    wh
361cc 69 6c 65 28 20 70 2d 3e 70 4c 65 66 74 20 29 7b  ile( p->pLeft ){
361cd 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4c  .      p = p->pL
361ce 65 66 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  eft;.    }.    i
361cf 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 7d 65 6c  Token = 0;.  }el
361d0 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 70  se{.    assert(p
361d1 20 26 26 20 70 2d 3e 65 54 79 70 65 3d 3d 46 54   && p->eType==FT
361d2 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29 3b  SQUERY_PHRASE );
361d3 0a 20 20 20 20 69 66 28 20 69 54 6f 6b 65 6e 3c  .    if( iToken<
361d4 28 70 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f  (p->pPhrase->nTo
361d5 6b 65 6e 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  ken-1) ){.      
361d6 69 54 6f 6b 65 6e 2b 2b 3b 0a 20 20 20 20 7d 65  iToken++;.    }e
361d7 6c 73 65 7b 0a 20 20 20 20 20 20 69 54 6f 6b 65  lse{.      iToke
361d8 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 77 68 69  n = 0;.      whi
361d9 6c 65 28 20 70 2d 3e 70 50 61 72 65 6e 74 20 26  le( p->pParent &
361da 26 20 70 2d 3e 70 50 61 72 65 6e 74 2d 3e 70 4c  & p->pParent->pL
361db 65 66 74 21 3d 70 20 29 7b 0a 20 20 20 20 20 20  eft!=p ){.      
361dc 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 61    assert( p->pPa
361dd 72 65 6e 74 2d 3e 70 52 69 67 68 74 3d 3d 70 20  rent->pRight==p 
361de 29 3b 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70  );.        p = p
361df 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 20  ->pParent;.     
361e0 20 7d 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e   }.      p = p->
361e1 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 20 20 69  pParent;.      i
361e2 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20  f( p ){.        
361e3 61 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67 68  assert( p->pRigh
361e4 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  t!=0 );.        
361e5 70 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20  p = p->pRight;. 
361e6 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d         while( p-
361e7 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20  >pLeft ){.      
361e8 20 20 20 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74      p = p->pLeft
361e9 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
361ea 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
361eb 20 2a 70 70 45 78 70 72 20 3d 20 70 3b 0a 20 20   *ppExpr = p;.  
361ec 2a 70 69 54 6f 6b 65 6e 20 3d 20 69 54 6f 6b 65  *piToken = iToke
361ed 6e 3b 0a 20 20 72 65 74 75 72 6e 20 70 3f 31 3a  n;.  return p?1:
361ee 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  0;.}../*.** Retu
361ef 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20 65  rn TRUE if the e
361f0 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 70  xpression node p
361f1 45 78 70 72 20 69 73 20 6c 6f 63 61 74 65 64 20  Expr is located 
361f2 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 52  beneath the.** R
361f3 48 53 20 6f 66 20 61 20 4e 4f 54 20 6f 70 65 72  HS of a NOT oper
361f4 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ator..*/.static 
361f5 69 6e 74 20 66 74 73 33 45 78 70 72 42 65 6e 65  int fts3ExprBene
361f6 61 74 68 4e 6f 74 28 46 74 73 33 45 78 70 72 20  athNot(Fts3Expr 
361f7 2a 70 29 7b 0a 20 20 46 74 73 33 45 78 70 72 20  *p){.  Fts3Expr 
361f8 2a 70 50 61 72 65 6e 74 3b 0a 20 20 77 68 69 6c  *pParent;.  whil
361f9 65 28 20 70 20 29 7b 0a 20 20 20 20 70 50 61 72  e( p ){.    pPar
361fa 65 6e 74 20 3d 20 70 2d 3e 70 50 61 72 65 6e 74  ent = p->pParent
361fb 3b 0a 20 20 20 20 69 66 28 20 70 50 61 72 65 6e  ;.    if( pParen
361fc 74 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 65 54  t && pParent->eT
361fd 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 4f  ype==FTSQUERY_NO
361fe 54 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 70 52  T && pParent->pR
361ff 69 67 68 74 3d 3d 70 20 29 7b 0a 20 20 20 20 20  ight==p ){.     
36200 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
36201 0a 20 20 20 20 70 20 3d 20 70 50 61 72 65 6e 74  .    p = pParent
36202 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
36203 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 65  ;.}../*.** Add e
36204 6e 74 72 69 65 73 20 74 6f 20 70 53 6e 69 70 70  ntries to pSnipp
36205 65 74 2d 3e 61 4d 61 74 63 68 5b 5d 20 66 6f 72  et->aMatch[] for
36206 20 65 76 65 72 79 20 6d 61 74 63 68 20 74 68 61   every match tha
36207 74 20 6f 63 63 75 72 73 20 61 67 61 69 6e 73 74  t occurs against
36208 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 20 7a 44 6f  .** document zDo
36209 63 5b 30 2e 2e 6e 44 6f 63 2d 31 5d 20 77 68 69  c[0..nDoc-1] whi
3620a 63 68 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  ch is stored in 
3620b 63 6f 6c 75 6d 6e 20 69 43 6f 6c 75 6d 6e 2e 0a  column iColumn..
3620c 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
3620d 6e 69 70 70 65 74 4f 66 66 73 65 74 73 4f 66 43  nippetOffsetsOfC
3620e 6f 6c 75 6d 6e 28 0a 20 20 66 75 6c 6c 74 65 78  olumn(.  fulltex
3620f 74 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  t_cursor *pCur, 
36210 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
36211 75 6c 6c 74 65 73 74 20 73 65 61 72 63 68 20 63  ulltest search c
36212 75 72 73 6f 72 20 2a 2f 0a 20 20 53 6e 69 70 70  ursor */.  Snipp
36213 65 74 20 2a 70 53 6e 69 70 70 65 74 2c 20 20 20  et *pSnippet,   
36214 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
36215 20 53 6e 69 70 70 65 74 20 6f 62 6a 65 63 74 20   Snippet object 
36216 74 6f 20 62 65 20 66 69 6c 6c 65 64 20 69 6e 20  to be filled in 
36217 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  */.  int iColumn
36218 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
36219 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
3621a 66 75 6c 6c 74 65 78 74 20 74 61 62 6c 65 20 63  fulltext table c
3621b 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  olumn */.  const
3621c 20 63 68 61 72 20 2a 7a 44 6f 63 2c 20 20 20 20   char *zDoc,    
3621d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
3621e 74 20 6f 66 20 74 68 65 20 66 75 6c 6c 74 65 78  t of the fulltex
3621f 74 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 2a  t table column *
36220 2f 0a 20 20 69 6e 74 20 6e 44 6f 63 20 20 20 20  /.  int nDoc    
36221 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36222 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
36223 7a 44 6f 63 20 69 6e 20 62 79 74 65 73 20 2a 2f  zDoc in bytes */
36224 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  .){.  const sqli
36225 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
36226 64 75 6c 65 20 2a 70 54 4d 6f 64 75 6c 65 3b 20  dule *pTModule; 
36227 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65   /* The tokenize
36228 72 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 73 71  r module */.  sq
36229 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
3622a 2a 70 54 6f 6b 65 6e 69 7a 65 72 3b 20 20 20 20  *pTokenizer;    
3622b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
3622c 73 70 65 63 69 66 69 63 20 74 6f 6b 65 6e 69 7a  specific tokeniz
3622d 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  er */.  sqlite3_
3622e 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
3622f 20 2a 70 54 43 75 72 73 6f 72 3b 20 20 20 20 20   *pTCursor;     
36230 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20     /* Tokenizer 
36231 63 75 72 73 6f 72 20 2a 2f 0a 20 20 66 75 6c 6c  cursor */.  full
36232 74 65 78 74 5f 76 74 61 62 20 2a 70 56 74 61 62  text_vtab *pVtab
36233 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
36234 20 2f 2a 20 54 68 65 20 66 75 6c 6c 20 74 65 78   /* The full tex
36235 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74  t index */.  int
36236 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20   nColumn;       
36237 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36238 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
36239 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 69 6e  olumns in the in
3623a 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20  dex */.  int i, 
3623b 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  j;              
3623c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3623d 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a   Loop counters *
3623e 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
3623f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36240 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
36241 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 6e 73  rn code */.  uns
36242 69 67 6e 65 64 20 69 6e 74 20 6d 61 74 63 68 2c  igned int match,
36243 20 70 72 65 76 4d 61 74 63 68 3b 20 20 20 20 20   prevMatch;     
36244 20 20 2f 2a 20 50 68 72 61 73 65 20 73 65 61 72    /* Phrase sear
36245 63 68 20 62 69 74 6d 61 73 6b 73 20 2a 2f 0a 20  ch bitmasks */. 
36246 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f   const char *zTo
36247 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  ken;            
36248 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 74 6f        /* Next to
36249 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 74 6f 6b  ken from the tok
3624a 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 69 6e 74 20  enizer */.  int 
3624b 6e 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20  nToken;         
3624c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3624d 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 54 6f 6b   /* Size of zTok
3624e 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 42 65 67  en */.  int iBeg
3624f 69 6e 2c 20 69 45 6e 64 2c 20 69 50 6f 73 3b 20  in, iEnd, iPos; 
36250 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36251 4f 66 66 73 65 74 73 20 6f 66 20 62 65 67 69 6e  Offsets of begin
36252 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20 2a 2f 0a  ning and end */.
36253 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  /* The follow
36254 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20 6b 65  ing variables ke
36255 65 70 20 61 20 63 69 72 63 75 6c 61 72 20 62 75  ep a circular bu
36256 66 66 65 72 20 6f 66 20 74 68 65 20 6c 61 73 74  ffer of the last
36257 0a 20 20 2a 2a 20 66 65 77 20 74 6f 6b 65 6e 73  .  ** few tokens
36258 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69   */.  unsigned i
36259 6e 74 20 69 52 6f 74 6f 72 20 3d 20 30 3b 20 20  nt iRotor = 0;  
3625a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
3625b 64 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20 74  dex of current t
3625c 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 52  oken */.  int iR
3625d 6f 74 6f 72 42 65 67 69 6e 5b 46 54 53 33 5f 52  otorBegin[FTS3_R
3625e 4f 54 4f 52 5f 53 5a 5d 3b 20 20 20 20 20 20 2f  OTOR_SZ];      /
3625f 2a 20 42 65 67 69 6e 6e 69 6e 67 20 6f 66 66 73  * Beginning offs
36260 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20  et of token */. 
36261 20 69 6e 74 20 69 52 6f 74 6f 72 4c 65 6e 5b 46   int iRotorLen[F
36262 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 5d 3b 20 20  TS3_ROTOR_SZ];  
36263 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
36264 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 0a 20 20 70  of token */..  p
36265 56 74 61 62 20 3d 20 63 75 72 73 6f 72 5f 76 74  Vtab = cursor_vt
36266 61 62 28 70 43 75 72 29 3b 0a 20 20 6e 43 6f 6c  ab(pCur);.  nCol
36267 75 6d 6e 20 3d 20 70 56 74 61 62 2d 3e 6e 43 6f  umn = pVtab->nCo
36268 6c 75 6d 6e 3b 0a 20 20 70 54 6f 6b 65 6e 69 7a  lumn;.  pTokeniz
36269 65 72 20 3d 20 70 56 74 61 62 2d 3e 70 54 6f 6b  er = pVtab->pTok
3626a 65 6e 69 7a 65 72 3b 0a 20 20 70 54 4d 6f 64 75  enizer;.  pTModu
3626b 6c 65 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 2d  le = pTokenizer-
3626c 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 72 63 20 3d  >pModule;.  rc =
3626d 20 70 54 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e   pTModule->xOpen
3626e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a 44 6f  (pTokenizer, zDo
3626f 63 2c 20 6e 44 6f 63 2c 20 26 70 54 43 75 72 73  c, nDoc, &pTCurs
36270 6f 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  or);.  if( rc ) 
36271 72 65 74 75 72 6e 3b 0a 20 20 70 54 43 75 72 73  return;.  pTCurs
36272 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d  or->pTokenizer =
36273 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 0a 20 20   pTokenizer;..  
36274 70 72 65 76 4d 61 74 63 68 20 3d 20 30 3b 0a 20  prevMatch = 0;. 
36275 20 77 68 69 6c 65 28 20 21 70 54 4d 6f 64 75 6c   while( !pTModul
36276 65 2d 3e 78 4e 65 78 74 28 70 54 43 75 72 73 6f  e->xNext(pTCurso
36277 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f  r, &zToken, &nTo
36278 6b 65 6e 2c 20 26 69 42 65 67 69 6e 2c 20 26 69  ken, &iBegin, &i
36279 45 6e 64 2c 20 26 69 50 6f 73 29 20 29 7b 0a 20  End, &iPos) ){. 
3627a 20 20 20 46 74 73 33 45 78 70 72 20 2a 70 49 74     Fts3Expr *pIt
3627b 65 72 20 3d 20 70 43 75 72 2d 3e 70 45 78 70 72  er = pCur->pExpr
3627c 3b 0a 20 20 20 20 69 6e 74 20 69 49 74 65 72 20  ;.    int iIter 
3627d 3d 20 2d 31 3b 0a 20 20 20 20 69 52 6f 74 6f 72  = -1;.    iRotor
3627e 42 65 67 69 6e 5b 69 52 6f 74 6f 72 26 46 54 53  Begin[iRotor&FTS
3627f 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b 5d 20 3d 20  3_ROTOR_MASK] = 
36280 69 42 65 67 69 6e 3b 0a 20 20 20 20 69 52 6f 74  iBegin;.    iRot
36281 6f 72 4c 65 6e 5b 69 52 6f 74 6f 72 26 46 54 53  orLen[iRotor&FTS
36282 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b 5d 20 3d 20  3_ROTOR_MASK] = 
36283 69 45 6e 64 2d 69 42 65 67 69 6e 3b 0a 20 20 20  iEnd-iBegin;.   
36284 20 6d 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20   match = 0;.    
36285 66 6f 72 28 69 3d 30 3b 20 69 3c 28 46 54 53 33  for(i=0; i<(FTS3
36286 5f 52 4f 54 4f 52 5f 53 5a 2d 31 29 20 26 26 20  _ROTOR_SZ-1) && 
36287 66 74 73 33 4e 65 78 74 45 78 70 72 54 6f 6b 65  fts3NextExprToke
36288 6e 28 26 70 49 74 65 72 2c 20 26 69 49 74 65 72  n(&pIter, &iIter
36289 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  ); i++){.      i
3628a 6e 74 20 6e 50 68 72 61 73 65 3b 20 20 20 20 20  nt nPhrase;     
3628b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3628c 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65  * Number of toke
3628d 6e 73 20 69 6e 20 63 75 72 72 65 6e 74 20 70 68  ns in current ph
3628e 72 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 73 74  rase */.      st
3628f 72 75 63 74 20 50 68 72 61 73 65 54 6f 6b 65 6e  ruct PhraseToken
36290 20 2a 70 54 6f 6b 65 6e 3b 20 20 20 20 20 2f 2a   *pToken;     /*
36291 20 43 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a   Current token *
36292 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c  /.      int iCol
36293 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
36294 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d          /* Colum
36295 6e 20 69 6e 64 65 78 20 2a 2f 0a 0a 20 20 20 20  n index */..    
36296 20 20 69 66 28 20 66 74 73 33 45 78 70 72 42 65    if( fts3ExprBe
36297 6e 65 61 74 68 4e 6f 74 28 70 49 74 65 72 29 20  neathNot(pIter) 
36298 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
36299 20 20 6e 50 68 72 61 73 65 20 3d 20 70 49 74 65    nPhrase = pIte
3629a 72 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  r->pPhrase->nTok
3629b 65 6e 3b 0a 20 20 20 20 20 20 70 54 6f 6b 65 6e  en;.      pToken
3629c 20 3d 20 26 70 49 74 65 72 2d 3e 70 50 68 72 61   = &pIter->pPhra
3629d 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 49 74 65 72  se->aToken[iIter
3629e 5d 3b 0a 20 20 20 20 20 20 69 43 6f 6c 20 3d 20  ];.      iCol = 
3629f 70 49 74 65 72 2d 3e 70 50 68 72 61 73 65 2d 3e  pIter->pPhrase->
362a0 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 69  iColumn;.      i
362a1 66 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43  f( iCol>=0 && iC
362a2 6f 6c 3c 6e 43 6f 6c 75 6d 6e 20 26 26 20 69 43  ol<nColumn && iC
362a3 6f 6c 21 3d 69 43 6f 6c 75 6d 6e 20 29 20 63 6f  ol!=iColumn ) co
362a4 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66  ntinue;.      if
362a5 28 20 70 54 6f 6b 65 6e 2d 3e 6e 3e 6e 54 6f 6b  ( pToken->n>nTok
362a6 65 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  en ) continue;. 
362a7 20 20 20 20 20 69 66 28 20 21 70 54 6f 6b 65 6e       if( !pToken
362a8 2d 3e 69 73 50 72 65 66 69 78 20 26 26 20 70 54  ->isPrefix && pT
362a9 6f 6b 65 6e 2d 3e 6e 3c 6e 54 6f 6b 65 6e 20 29  oken->n<nToken )
362aa 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
362ab 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d   assert( pToken-
362ac 3e 6e 3c 3d 6e 54 6f 6b 65 6e 20 29 3b 0a 20 20  >n<=nToken );.  
362ad 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70      if( memcmp(p
362ae 54 6f 6b 65 6e 2d 3e 7a 2c 20 7a 54 6f 6b 65 6e  Token->z, zToken
362af 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 20 29 20 63  , pToken->n) ) c
362b0 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69  ontinue;.      i
362b1 66 28 20 69 49 74 65 72 3e 30 20 26 26 20 28 70  f( iIter>0 && (p
362b2 72 65 76 4d 61 74 63 68 20 26 20 28 31 3c 3c 69  revMatch & (1<<i
362b3 29 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65  ))==0 ) continue
362b4 3b 0a 20 20 20 20 20 20 6d 61 74 63 68 20 7c 3d  ;.      match |=
362b5 20 31 3c 3c 69 3b 0a 20 20 20 20 20 20 69 66 28   1<<i;.      if(
362b6 20 69 3d 3d 28 46 54 53 33 5f 52 4f 54 4f 52 5f   i==(FTS3_ROTOR_
362b7 53 5a 2d 32 29 20 7c 7c 20 6e 50 68 72 61 73 65  SZ-2) || nPhrase
362b8 3d 3d 69 49 74 65 72 2b 31 20 29 7b 0a 20 20 20  ==iIter+1 ){.   
362b9 20 20 20 20 20 66 6f 72 28 6a 3d 6e 50 68 72 61       for(j=nPhra
362ba 73 65 2d 31 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 29  se-1; j>=0; j--)
362bb 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  {.          int 
362bc 6b 20 3d 20 28 69 52 6f 74 6f 72 2d 6a 29 20 26  k = (iRotor-j) &
362bd 20 46 54 53 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b   FTS3_ROTOR_MASK
362be 3b 0a 20 20 20 20 20 20 20 20 20 20 73 6e 69 70  ;.          snip
362bf 70 65 74 41 70 70 65 6e 64 4d 61 74 63 68 28 70  petAppendMatch(p
362c0 53 6e 69 70 70 65 74 2c 20 69 43 6f 6c 75 6d 6e  Snippet, iColumn
362c1 2c 20 69 2d 6a 2c 20 69 50 6f 73 2d 6a 2c 0a 20  , i-j, iPos-j,. 
362c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
362c3 52 6f 74 6f 72 42 65 67 69 6e 5b 6b 5d 2c 20 69  RotorBegin[k], i
362c4 52 6f 74 6f 72 4c 65 6e 5b 6b 5d 29 3b 0a 20 20  RotorLen[k]);.  
362c5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
362c6 20 20 20 20 7d 0a 20 20 20 20 70 72 65 76 4d 61      }.    prevMa
362c7 74 63 68 20 3d 20 6d 61 74 63 68 3c 3c 31 3b 0a  tch = match<<1;.
362c8 20 20 20 20 69 52 6f 74 6f 72 2b 2b 3b 0a 20 20      iRotor++;.  
362c9 7d 0a 20 20 70 54 4d 6f 64 75 6c 65 2d 3e 78 43  }.  pTModule->xC
362ca 6c 6f 73 65 28 70 54 43 75 72 73 6f 72 29 3b 20  lose(pTCursor); 
362cb 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76   .}../*.** Remov
362cc 65 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74  e entries from t
362cd 68 65 20 70 53 6e 69 70 70 65 74 20 73 74 72 75  he pSnippet stru
362ce 63 74 75 72 65 20 74 6f 20 61 63 63 6f 75 6e 74  cture to account
362cf 20 66 6f 72 20 74 68 65 20 4e 45 41 52 0a 2a 2a   for the NEAR.**
362d0 20 6f 70 65 72 61 74 6f 72 2e 20 57 68 65 6e 20   operator. When 
362d1 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20  this is called, 
362d2 70 53 6e 69 70 70 65 74 20 63 6f 6e 74 61 69 6e  pSnippet contain
362d3 73 20 74 68 65 20 6c 69 73 74 20 6f 66 20 74 6f  s the list of to
362d4 6b 65 6e 20 0a 2a 2a 20 6f 66 66 73 65 74 73 20  ken .** offsets 
362d5 70 72 6f 64 75 63 65 64 20 62 79 20 74 72 65 61  produced by trea
362d6 74 69 6e 67 20 61 6c 6c 20 4e 45 41 52 20 6f 70  ting all NEAR op
362d7 65 72 61 74 6f 72 73 20 61 73 20 41 4e 44 20 6f  erators as AND o
362d8 70 65 72 61 74 6f 72 73 2e 0a 2a 2a 20 54 68 69  perators..** Thi
362d9 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 6d 6f 76  s function remov
362da 65 73 20 61 6e 79 20 65 6e 74 72 69 65 73 20 74  es any entries t
362db 68 61 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  hat should not b
362dc 65 20 70 72 65 73 65 6e 74 20 61 66 74 65 72 0a  e present after.
362dd 2a 2a 20 61 63 63 6f 75 6e 74 69 6e 67 20 66 6f  ** accounting fo
362de 72 20 74 68 65 20 4e 45 41 52 20 72 65 73 74 72  r the NEAR restr
362df 69 63 74 69 6f 6e 2e 20 46 6f 72 20 65 78 61 6d  iction. For exam
362e0 70 6c 65 2c 20 69 66 20 74 68 65 20 71 75 65 72  ple, if the quer
362e1 69 65 64 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 20  ied.** document 
362e2 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 22 41  is:.**.**     "A
362e3 20 42 20 43 20 44 20 45 20 41 22 0a 2a 2a 0a 2a   B C D E A".**.*
362e4 2a 20 61 6e 64 20 74 68 65 20 71 75 65 72 79 20  * and the query 
362e5 69 73 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 41  is:.** .**     A
362e6 20 4e 45 41 52 2f 30 20 45 0a 2a 2a 0a 2a 2a 20   NEAR/0 E.**.** 
362e7 74 68 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66  then when this f
362e8 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
362e9 64 20 74 68 65 20 53 6e 69 70 70 65 74 20 63 6f  d the Snippet co
362ea 6e 74 61 69 6e 73 20 74 6f 6b 65 6e 20 6f 66 66  ntains token off
362eb 73 65 74 73 0a 2a 2a 20 30 2c 20 34 20 61 6e 64  sets.** 0, 4 and
362ec 20 35 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f   5. This functio
362ed 6e 20 72 65 6d 6f 76 65 73 20 74 68 65 20 22 30  n removes the "0
362ee 22 20 65 6e 74 72 79 20 28 62 65 63 61 75 73 65  " entry (because
362ef 20 74 68 65 20 66 69 72 73 74 20 41 0a 2a 2a 20   the first A.** 
362f0 69 73 20 6e 6f 74 20 6e 65 61 72 20 65 6e 6f 75  is not near enou
362f1 67 68 20 74 6f 20 61 6e 20 45 29 2e 0a 2a 2a 0a  gh to an E)..**.
362f2 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 66 75 6e  ** When this fun
362f3 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c  ction is called,
362f4 20 74 68 65 20 76 61 6c 75 65 20 70 6f 69 6e 74   the value point
362f5 65 64 20 74 6f 20 62 79 20 70 61 72 61 6d 65 74  ed to by paramet
362f6 65 72 20 70 69 4c 65 66 74 20 69 73 0a 2a 2a 20  er piLeft is.** 
362f7 74 68 65 20 69 6e 74 65 67 65 72 20 69 64 20 6f  the integer id o
362f8 66 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20  f the left-most 
362f9 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 65 78 70  token in the exp
362fa 72 65 73 73 69 6f 6e 20 74 72 65 65 20 68 65 61  ression tree hea
362fb 64 65 64 20 62 79 0a 2a 2a 20 70 45 78 70 72 2e  ded by.** pExpr.
362fc 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
362fd 6e 63 72 65 6d 65 6e 74 73 20 2a 70 69 4c 65 66  ncrements *piLef
362fe 74 20 62 79 20 74 68 65 20 74 6f 74 61 6c 20 6e  t by the total n
362ff 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 0a  umber of tokens.
36300 2a 2a 20 69 6e 20 74 68 65 20 65 78 70 72 65 73  ** in the expres
36301 73 69 6f 6e 20 74 72 65 65 20 68 65 61 64 65 64  sion tree headed
36302 20 62 79 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a   by pExpr..**.**
36303 20 52 65 74 75 72 6e 20 31 20 69 66 20 61 6e 79   Return 1 if any
36304 20 74 72 69 6d 6d 69 6e 67 20 6f 63 63 75 72 73   trimming occurs
36305 2e 20 20 52 65 74 75 72 6e 20 30 20 69 66 20 6e  .  Return 0 if n
36306 6f 20 74 72 69 6d 6d 69 6e 67 20 69 73 20 72 65  o trimming is re
36307 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  quired..*/.stati
36308 63 20 69 6e 74 20 74 72 69 6d 53 6e 69 70 70 65  c int trimSnippe
36309 74 4f 66 66 73 65 74 73 28 0a 20 20 46 74 73 33  tOffsets(.  Fts3
3630a 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20  Expr *pExpr,    
3630b 20 20 2f 2a 20 54 68 65 20 73 65 61 72 63 68 20    /* The search 
3630c 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
3630d 53 6e 69 70 70 65 74 20 2a 70 53 6e 69 70 70 65  Snippet *pSnippe
3630e 74 2c 20 20 20 20 2f 2a 20 54 68 65 20 73 65 74  t,    /* The set
3630f 20 6f 66 20 73 6e 69 70 70 65 74 20 6f 66 66 73   of snippet offs
36310 65 74 73 20 74 6f 20 62 65 20 74 72 69 6d 6d 65  ets to be trimme
36311 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 4c 65  d */.  int *piLe
36312 66 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ft           /* 
36313 49 6e 64 65 78 20 6f 66 20 6c 65 66 74 2d 6d 6f  Index of left-mo
36314 73 74 20 74 6f 6b 65 6e 20 69 6e 20 70 45 78 70  st token in pExp
36315 72 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70 45  r */.){.  if( pE
36316 78 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20 74  xpr ){.    if( t
36317 72 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65 74  rimSnippetOffset
36318 73 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  s(pExpr->pLeft, 
36319 70 53 6e 69 70 70 65 74 2c 20 70 69 4c 65 66 74  pSnippet, piLeft
3631a 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
3631b 6e 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  n 1;.    }..    
3631c 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 65  switch( pExpr->e
3631d 54 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61  Type ){.      ca
3631e 73 65 20 46 54 53 51 55 45 52 59 5f 50 48 52 41  se FTSQUERY_PHRA
3631f 53 45 3a 0a 20 20 20 20 20 20 20 20 2a 70 69 4c  SE:.        *piL
36320 65 66 74 20 2b 3d 20 70 45 78 70 72 2d 3e 70 50  eft += pExpr->pP
36321 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20  hrase->nToken;. 
36322 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
36323 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52      case FTSQUER
36324 59 5f 4e 45 41 52 3a 20 7b 0a 20 20 20 20 20 20  Y_NEAR: {.      
36325 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 68    /* The right-h
36326 61 6e 64 2d 73 69 64 65 20 6f 66 20 61 20 4e 45  and-side of a NE
36327 41 52 20 6f 70 65 72 61 74 6f 72 20 69 73 20 61  AR operator is a
36328 6c 77 61 79 73 20 61 20 70 68 72 61 73 65 2e 20  lways a phrase. 
36329 54 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6c  The.        ** l
3632a 65 66 74 2d 68 61 6e 64 2d 73 69 64 65 20 69 73  eft-hand-side is
3632b 20 65 69 74 68 65 72 20 61 20 70 68 72 61 73 65   either a phrase
3632c 20 6f 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f   or an expressio
3632d 6e 20 74 72 65 65 20 74 68 61 74 20 69 73 20 0a  n tree that is .
3632e 20 20 20 20 20 20 20 20 2a 2a 20 69 74 73 65 6c          ** itsel
3632f 66 20 68 65 61 64 65 64 20 62 79 20 61 20 4e 45  f headed by a NE
36330 41 52 20 6f 70 65 72 61 74 6f 72 2e 20 54 68 65  AR operator. The
36331 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 69 74 69   following initi
36332 61 6c 69 7a 61 74 69 6f 6e 73 0a 20 20 20 20 20  alizations.     
36333 20 20 20 2a 2a 20 73 65 74 20 6c 6f 63 61 6c 20     ** set local 
36334 76 61 72 69 61 62 6c 65 20 69 4c 65 66 74 20 74  variable iLeft t
36335 6f 20 74 68 65 20 74 6f 6b 65 6e 20 6e 75 6d 62  o the token numb
36336 65 72 20 6f 66 20 74 68 65 20 6c 65 66 74 2d 6d  er of the left-m
36337 6f 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  ost.        ** t
36338 6f 6b 65 6e 20 69 6e 20 74 68 65 20 72 69 67 68  oken in the righ
36339 74 2d 68 61 6e 64 20 70 68 72 61 73 65 2c 20 61  t-hand phrase, a
3633a 6e 64 20 69 52 69 67 68 74 20 74 6f 20 74 68 65  nd iRight to the
3633b 20 72 69 67 68 74 20 6d 6f 73 74 0a 20 20 20 20   right most.    
3633c 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 69 6e 20      ** token in 
3633d 74 68 65 20 73 61 6d 65 20 70 68 72 61 73 65 2e  the same phrase.
3633e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66   For example, if
3633f 20 77 65 20 68 61 64 3a 0a 20 20 20 20 20 20 20   we had:.       
36340 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20   **.        **  
36341 20 20 20 3c 63 6f 6c 3e 20 4d 41 54 43 48 20 27     <col> MATCH '
36342 22 61 62 63 20 64 65 66 22 20 4e 45 41 52 2f 32  "abc def" NEAR/2
36343 20 22 67 68 69 20 6a 6b 6c 22 27 0a 20 20 20 20   "ghi jkl"'.    
36344 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
36345 2a 20 74 68 65 6e 20 69 4c 65 66 74 20 77 69 6c  * then iLeft wil
36346 6c 20 62 65 20 73 65 74 20 74 6f 20 32 20 28 74  l be set to 2 (t
36347 6f 6b 65 6e 20 6e 75 6d 62 65 72 20 6f 66 20 67  oken number of g
36348 68 69 29 20 61 6e 64 20 6e 54 6f 6b 65 6e 20 77  hi) and nToken w
36349 69 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 62  ill.        ** b
3634a 65 20 73 65 74 20 74 6f 20 34 2e 0a 20 20 20 20  e set to 4..    
3634b 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 46      */.        F
3634c 74 73 33 45 78 70 72 20 2a 70 4c 65 66 74 20 3d  ts3Expr *pLeft =
3634d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20   pExpr->pLeft;. 
3634e 20 20 20 20 20 20 20 46 74 73 33 45 78 70 72 20         Fts3Expr 
3634f 2a 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d  *pRight = pExpr-
36350 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20  >pRight;.       
36351 20 69 6e 74 20 69 4c 65 66 74 20 3d 20 2a 70 69   int iLeft = *pi
36352 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20 69 6e  Left;.        in
36353 74 20 6e 4e 65 61 72 20 3d 20 70 45 78 70 72 2d  t nNear = pExpr-
36354 3e 6e 4e 65 61 72 3b 0a 20 20 20 20 20 20 20 20  >nNear;.        
36355 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20 70 52 69  int nToken = pRi
36356 67 68 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54  ght->pPhrase->nT
36357 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 69 6e  oken;.        in
36358 74 20 6a 6a 2c 20 69 69 3b 0a 20 20 20 20 20 20  t jj, ii;.      
36359 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 65 54 79    if( pLeft->eTy
3635a 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41  pe==FTSQUERY_NEA
3635b 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  R ){.          p
3635c 4c 65 66 74 20 3d 20 70 4c 65 66 74 2d 3e 70 52  Left = pLeft->pR
3635d 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ight;.        }.
3635e 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
3635f 70 52 69 67 68 74 2d 3e 65 54 79 70 65 3d 3d 46  pRight->eType==F
36360 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29  TSQUERY_PHRASE )
36361 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
36362 28 20 70 4c 65 66 74 2d 3e 65 54 79 70 65 3d 3d  ( pLeft->eType==
36363 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20  FTSQUERY_PHRASE 
36364 29 3b 0a 20 20 20 20 20 20 20 20 6e 54 6f 6b 65  );.        nToke
36365 6e 20 2b 3d 20 70 4c 65 66 74 2d 3e 70 50 68 72  n += pLeft->pPhr
36366 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 0a 20 20  ase->nToken;..  
36367 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20        for(ii=0; 
36368 69 69 3c 70 53 6e 69 70 70 65 74 2d 3e 6e 4d 61  ii<pSnippet->nMa
36369 74 63 68 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  tch; ii++){.    
3636a 20 20 20 20 20 20 73 74 72 75 63 74 20 73 6e 69        struct sni
3636b 70 70 65 74 4d 61 74 63 68 20 2a 70 20 3d 20 26  ppetMatch *p = &
3636c 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68  pSnippet->aMatch
3636d 5b 69 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  [ii];.          
3636e 69 66 28 20 70 2d 3e 69 54 65 72 6d 3d 3d 69 4c  if( p->iTerm==iL
3636f 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  eft ){.         
36370 20 20 20 69 6e 74 20 69 73 4f 6b 20 3d 20 30 3b     int isOk = 0;
36371 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  .            /* 
36372 53 6e 69 70 70 65 74 20 69 69 20 69 73 20 61 6e  Snippet ii is an
36373 20 6f 63 63 75 72 65 6e 63 65 20 6f 66 20 71 75   occurence of qu
36374 65 72 79 20 74 65 72 6d 20 69 4c 65 66 74 20 69  ery term iLeft i
36375 6e 20 74 68 65 20 64 6f 63 75 6d 65 6e 74 2e 0a  n the document..
36376 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 49              ** I
36377 74 20 6f 63 63 75 72 73 20 61 74 20 70 6f 73 69  t occurs at posi
36378 74 69 6f 6e 20 28 70 2d 3e 69 54 6f 6b 65 6e 29  tion (p->iToken)
36379 20 6f 66 20 74 68 65 20 64 6f 63 75 6d 65 6e 74   of the document
3637a 2e 20 57 65 20 6e 6f 77 0a 20 20 20 20 20 20 20  . We now.       
3637b 20 20 20 20 20 2a 2a 20 73 65 61 72 63 68 20 66       ** search f
3637c 6f 72 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  or an instance o
3637d 66 20 74 6f 6b 65 6e 20 28 69 4c 65 66 74 2d 31  f token (iLeft-1
3637e 29 20 73 6f 6d 65 77 68 65 72 65 20 69 6e 20 74  ) somewhere in t
3637f 68 65 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  he .            
36380 2a 2a 20 72 61 6e 67 65 20 28 70 2d 3e 69 54 6f  ** range (p->iTo
36381 6b 65 6e 20 2d 20 6e 4e 65 61 72 29 2e 2e 2e 28  ken - nNear)...(
36382 70 2d 3e 69 54 6f 6b 65 6e 20 2b 20 6e 4e 65 61  p->iToken + nNea
36383 72 20 2b 20 6e 54 6f 6b 65 6e 29 20 77 69 74 68  r + nToken) with
36384 69 6e 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  in .            
36385 2a 2a 20 74 68 65 20 73 65 74 20 6f 66 20 73 6e  ** the set of sn
36386 69 70 70 65 74 4d 61 74 63 68 20 73 74 72 75 63  ippetMatch struc
36387 74 75 72 65 73 2e 20 49 66 20 6f 6e 65 20 69 73  tures. If one is
36388 20 66 6f 75 6e 64 2c 20 70 72 6f 63 65 65 64 2e   found, proceed.
36389 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a   .            **
3638a 20 49 66 20 6f 6e 65 20 63 61 6e 6e 6f 74 20 62   If one cannot b
3638b 65 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 72 65  e found, then re
3638c 6d 6f 76 65 20 73 6e 69 70 70 65 74 73 20 69 69  move snippets ii
3638d 2e 2e 28 69 69 2b 4e 2d 31 29 20 0a 20 20 20 20  ..(ii+N-1) .    
3638e 20 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20          ** from 
3638f 74 68 65 20 6d 61 74 63 68 69 6e 67 20 73 6e 69  the matching sni
36390 70 70 65 74 73 2c 20 77 68 65 72 65 20 4e 20 69  ppets, where N i
36391 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
36392 74 6f 6b 65 6e 73 20 0a 20 20 20 20 20 20 20 20  tokens .        
36393 20 20 20 20 2a 2a 20 69 6e 20 70 68 72 61 73 65      ** in phrase
36394 20 70 52 69 67 68 74 2d 3e 70 50 68 72 61 73 65   pRight->pPhrase
36395 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  ..            */
36396 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72  .            for
36397 28 6a 6a 3d 30 3b 20 69 73 4f 6b 3d 3d 30 20 26  (jj=0; isOk==0 &
36398 26 20 6a 6a 3c 70 53 6e 69 70 70 65 74 2d 3e 6e  & jj<pSnippet->n
36399 4d 61 74 63 68 3b 20 6a 6a 2b 2b 29 7b 0a 20 20  Match; jj++){.  
3639a 20 20 20 20 20 20 20 20 20 20 20 20 73 74 72 75              stru
3639b 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20  ct snippetMatch 
3639c 2a 70 32 20 3d 20 26 70 53 6e 69 70 70 65 74 2d  *p2 = &pSnippet-
3639d 3e 61 4d 61 74 63 68 5b 6a 6a 5d 3b 0a 20 20 20  >aMatch[jj];.   
3639e 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70             if( p
3639f 32 2d 3e 69 54 65 72 6d 3d 3d 28 69 4c 65 66 74  2->iTerm==(iLeft
363a0 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  -1) ){.         
363a1 20 20 20 20 20 20 20 69 66 28 20 70 32 2d 3e 69         if( p2->i
363a2 54 6f 6b 65 6e 3e 3d 28 70 2d 3e 69 54 6f 6b 65  Token>=(p->iToke
363a3 6e 2d 6e 4e 65 61 72 2d 31 29 20 0a 20 20 20 20  n-nNear-1) .    
363a4 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
363a5 70 32 2d 3e 69 54 6f 6b 65 6e 3c 28 70 2d 3e 69  p2->iToken<(p->i
363a6 54 6f 6b 65 6e 2b 6e 4e 65 61 72 2b 6e 54 6f 6b  Token+nNear+nTok
363a7 65 6e 29 20 0a 20 20 20 20 20 20 20 20 20 20 20  en) .           
363a8 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
363a9 20 20 20 20 20 20 20 20 20 20 69 73 4f 6b 20 3d            isOk =
363aa 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   1;.            
363ab 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
363ac 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
363ad 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
363ae 69 66 28 20 21 69 73 4f 6b 20 29 7b 0a 20 20 20  if( !isOk ){.   
363af 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b             int k
363b0 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  k;.             
363b1 20 66 6f 72 28 6b 6b 3d 30 3b 20 6b 6b 3c 70 52   for(kk=0; kk<pR
363b2 69 67 68 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e  ight->pPhrase->n
363b3 54 6f 6b 65 6e 3b 20 6b 6b 2b 2b 29 7b 0a 20 20  Token; kk++){.  
363b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 53                pS
363b5 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b 6b  nippet->aMatch[k
363b6 6b 2b 69 69 5d 2e 69 54 65 72 6d 20 3d 20 2d 32  k+ii].iTerm = -2
363b7 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
363b8 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
363b9 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
363ba 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
363bb 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66    }.          if
363bc 28 20 70 2d 3e 69 54 65 72 6d 3d 3d 28 69 4c 65  ( p->iTerm==(iLe
363bd 66 74 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20  ft-1) ){.       
363be 20 20 20 20 20 69 6e 74 20 69 73 4f 6b 20 3d 20       int isOk = 
363bf 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66  0;.            f
363c0 6f 72 28 6a 6a 3d 30 3b 20 69 73 4f 6b 3d 3d 30  or(jj=0; isOk==0
363c1 20 26 26 20 6a 6a 3c 70 53 6e 69 70 70 65 74 2d   && jj<pSnippet-
363c2 3e 6e 4d 61 74 63 68 3b 20 6a 6a 2b 2b 29 7b 0a  >nMatch; jj++){.
363c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74                st
363c4 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63  ruct snippetMatc
363c5 68 20 2a 70 32 20 3d 20 26 70 53 6e 69 70 70 65  h *p2 = &pSnippe
363c6 74 2d 3e 61 4d 61 74 63 68 5b 6a 6a 5d 3b 0a 20  t->aMatch[jj];. 
363c7 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
363c8 20 70 32 2d 3e 69 54 65 72 6d 3d 3d 69 4c 65 66   p2->iTerm==iLef
363c9 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  t ){.           
363ca 20 20 20 20 20 69 66 28 20 70 32 2d 3e 69 54 6f       if( p2->iTo
363cb 6b 65 6e 3c 3d 28 70 2d 3e 69 54 6f 6b 65 6e 2b  ken<=(p->iToken+
363cc 6e 4e 65 61 72 2b 31 29 20 0a 20 20 20 20 20 20  nNear+1) .      
363cd 20 20 20 20 20 20 20 20 20 20 20 26 26 20 70 32             && p2
363ce 2d 3e 69 54 6f 6b 65 6e 3e 28 70 2d 3e 69 54 6f  ->iToken>(p->iTo
363cf 6b 65 6e 2d 6e 4e 65 61 72 2d 6e 54 6f 6b 65 6e  ken-nNear-nToken
363d0 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ) .             
363d1 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20     ){.          
363d2 20 20 20 20 20 20 20 20 69 73 4f 6b 20 3d 20 31          isOk = 1
363d3 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
363d4 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
363d5 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
363d6 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  }.            if
363d7 28 20 21 69 73 4f 6b 20 29 7b 0a 20 20 20 20 20  ( !isOk ){.     
363d8 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 6b 3b           int kk;
363d9 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66  .              f
363da 6f 72 28 6b 6b 3d 30 3b 20 6b 6b 3c 70 4c 65 66  or(kk=0; kk<pLef
363db 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  t->pPhrase->nTok
363dc 65 6e 3b 20 6b 6b 2b 2b 29 7b 0a 20 20 20 20 20  en; kk++){.     
363dd 20 20 20 20 20 20 20 20 20 20 20 70 53 6e 69 70             pSnip
363de 70 65 74 2d 3e 61 4d 61 74 63 68 5b 69 69 2d 6b  pet->aMatch[ii-k
363df 6b 5d 2e 69 54 65 72 6d 20 3d 20 2d 32 3b 0a 20  k].iTerm = -2;. 
363e0 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
363e1 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74               ret
363e2 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 20  urn 1;.         
363e3 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
363e4 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
363e5 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
363e6 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  }.    }..    if(
363e7 20 74 72 69 6d 53 6e 69 70 70 65 74 4f 66 66 73   trimSnippetOffs
363e8 65 74 73 28 70 45 78 70 72 2d 3e 70 52 69 67 68  ets(pExpr->pRigh
363e9 74 2c 20 70 53 6e 69 70 70 65 74 2c 20 70 69 4c  t, pSnippet, piL
363ea 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65  eft) ){.      re
363eb 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
363ec 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
363ed 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61  ./*.** Compute a
363ee 6c 6c 20 6f 66 66 73 65 74 73 20 66 6f 72 20 74  ll offsets for t
363ef 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f  he current row o
363f0 66 20 74 68 65 20 71 75 65 72 79 2e 20 20 0a 2a  f the query.  .*
363f1 2a 20 49 66 20 74 68 65 20 6f 66 66 73 65 74 73  * If the offsets
363f2 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65   have already be
363f3 65 6e 20 63 6f 6d 70 75 74 65 64 2c 20 74 68 69  en computed, thi
363f4 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
363f5 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  o-op..*/.static 
363f6 76 6f 69 64 20 73 6e 69 70 70 65 74 41 6c 6c 4f  void snippetAllO
363f7 66 66 73 65 74 73 28 66 75 6c 6c 74 65 78 74 5f  ffsets(fulltext_
363f8 63 75 72 73 6f 72 20 2a 70 29 7b 0a 20 20 69 6e  cursor *p){.  in
363f9 74 20 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 6e 74  t nColumn;.  int
363fa 20 69 43 6f 6c 75 6d 6e 2c 20 69 3b 0a 20 20 69   iColumn, i;.  i
363fb 6e 74 20 69 46 69 72 73 74 2c 20 69 4c 61 73 74  nt iFirst, iLast
363fc 3b 0a 20 20 69 6e 74 20 69 54 65 72 6d 20 3d 20  ;.  int iTerm = 
363fd 30 3b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74  0;.  fulltext_vt
363fe 61 62 20 2a 70 46 74 73 20 3d 20 63 75 72 73 6f  ab *pFts = curso
363ff 72 5f 76 74 61 62 28 70 29 3b 0a 0a 20 20 69 66  r_vtab(p);..  if
36400 28 20 70 2d 3e 73 6e 69 70 70 65 74 2e 6e 4d 61  ( p->snippet.nMa
36401 74 63 68 20 7c 7c 20 70 2d 3e 70 45 78 70 72 3d  tch || p->pExpr=
36402 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
36403 3b 0a 20 20 7d 0a 20 20 6e 43 6f 6c 75 6d 6e 20  ;.  }.  nColumn 
36404 3d 20 70 46 74 73 2d 3e 6e 43 6f 6c 75 6d 6e 3b  = pFts->nColumn;
36405 0a 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 28 70 2d  .  iColumn = (p-
36406 3e 69 43 75 72 73 6f 72 54 79 70 65 20 2d 20 51  >iCursorType - Q
36407 55 45 52 59 5f 46 55 4c 4c 54 45 58 54 29 3b 0a  UERY_FULLTEXT);.
36408 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3c 30 20    if( iColumn<0 
36409 7c 7c 20 69 43 6f 6c 75 6d 6e 3e 3d 6e 43 6f 6c  || iColumn>=nCol
3640a 75 6d 6e 20 29 7b 0a 20 20 20 20 2f 2a 20 4c 6f  umn ){.    /* Lo
3640b 6f 6b 20 66 6f 72 20 6d 61 74 63 68 65 73 20 6f  ok for matches o
3640c 76 65 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20  ver all columns 
3640d 6f 66 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74  of the full-text
3640e 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69 46   index */.    iF
3640f 69 72 73 74 20 3d 20 30 3b 0a 20 20 20 20 69 4c  irst = 0;.    iL
36410 61 73 74 20 3d 20 6e 43 6f 6c 75 6d 6e 2d 31 3b  ast = nColumn-1;
36411 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
36412 20 4c 6f 6f 6b 20 66 6f 72 20 6d 61 74 63 68 65   Look for matche
36413 73 20 69 6e 20 74 68 65 20 69 43 6f 6c 75 6d 6e  s in the iColumn
36414 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  -th column of th
36415 65 20 69 6e 64 65 78 20 6f 6e 6c 79 20 2a 2f 0a  e index only */.
36416 20 20 20 20 69 46 69 72 73 74 20 3d 20 69 43 6f      iFirst = iCo
36417 6c 75 6d 6e 3b 0a 20 20 20 20 69 4c 61 73 74 20  lumn;.    iLast 
36418 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 20  = iColumn;.  }. 
36419 20 66 6f 72 28 69 3d 69 46 69 72 73 74 3b 20 69   for(i=iFirst; i
3641a 3c 3d 69 4c 61 73 74 3b 20 69 2b 2b 29 7b 0a 20  <=iLast; i++){. 
3641b 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
3641c 44 6f 63 3b 0a 20 20 20 20 69 6e 74 20 6e 44 6f  Doc;.    int nDo
3641d 63 3b 0a 20 20 20 20 7a 44 6f 63 20 3d 20 28 63  c;.    zDoc = (c
3641e 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c 69 74  onst char*)sqlit
3641f 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70  e3_column_text(p
36420 2d 3e 70 53 74 6d 74 2c 20 69 2b 31 29 3b 0a 20  ->pStmt, i+1);. 
36421 20 20 20 6e 44 6f 63 20 3d 20 73 71 6c 69 74 65     nDoc = sqlite
36422 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70  3_column_bytes(p
36423 2d 3e 70 53 74 6d 74 2c 20 69 2b 31 29 3b 0a 20  ->pStmt, i+1);. 
36424 20 20 20 73 6e 69 70 70 65 74 4f 66 66 73 65 74     snippetOffset
36425 73 4f 66 43 6f 6c 75 6d 6e 28 70 2c 20 26 70 2d  sOfColumn(p, &p-
36426 3e 73 6e 69 70 70 65 74 2c 20 69 2c 20 7a 44 6f  >snippet, i, zDo
36427 63 2c 20 6e 44 6f 63 29 3b 0a 20 20 7d 0a 0a 20  c, nDoc);.  }.. 
36428 20 77 68 69 6c 65 28 20 74 72 69 6d 53 6e 69 70   while( trimSnip
36429 70 65 74 4f 66 66 73 65 74 73 28 70 2d 3e 70 45  petOffsets(p->pE
3642a 78 70 72 2c 20 26 70 2d 3e 73 6e 69 70 70 65 74  xpr, &p->snippet
3642b 2c 20 26 69 54 65 72 6d 29 20 29 7b 0a 20 20 20  , &iTerm) ){.   
3642c 20 69 54 65 72 6d 20 3d 20 30 3b 0a 20 20 7d 0a   iTerm = 0;.  }.
3642d 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  }../*.** Convert
3642e 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   the information
3642f 20 69 6e 20 74 68 65 20 61 4d 61 74 63 68 5b 5d   in the aMatch[]
36430 20 61 72 72 61 79 20 6f 66 20 74 68 65 20 73 6e   array of the sn
36431 69 70 70 65 74 0a 2a 2a 20 69 6e 74 6f 20 74 68  ippet.** into th
36432 65 20 73 74 72 69 6e 67 20 7a 4f 66 66 73 65 74  e string zOffset
36433 5b 30 2e 2e 6e 4f 66 66 73 65 74 2d 31 5d 2e 20  [0..nOffset-1]. 
36434 54 68 69 73 20 73 74 72 69 6e 67 20 69 73 20 75  This string is u
36435 73 65 64 20 61 73 0a 2a 2a 20 74 68 65 20 72 65  sed as.** the re
36436 74 75 72 6e 20 6f 66 20 74 68 65 20 53 51 4c 20  turn of the SQL 
36437 6f 66 66 73 65 74 73 28 29 20 66 75 6e 63 74 69  offsets() functi
36438 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
36439 69 64 20 73 6e 69 70 70 65 74 4f 66 66 73 65 74  id snippetOffset
3643a 54 65 78 74 28 53 6e 69 70 70 65 74 20 2a 70 29  Text(Snippet *p)
3643b 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
3643c 20 63 6e 74 20 3d 20 30 3b 0a 20 20 53 74 72 69   cnt = 0;.  Stri
3643d 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20 63  ngBuffer sb;.  c
3643e 68 61 72 20 7a 42 75 66 5b 32 30 30 5d 3b 0a 20  har zBuf[200];. 
3643f 20 69 66 28 20 70 2d 3e 7a 4f 66 66 73 65 74 20   if( p->zOffset 
36440 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 6e 69 74  ) return;.  init
36441 53 74 72 69 6e 67 42 75 66 66 65 72 28 26 73 62  StringBuffer(&sb
36442 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
36443 70 2d 3e 6e 4d 61 74 63 68 3b 20 69 2b 2b 29 7b  p->nMatch; i++){
36444 0a 20 20 20 20 73 74 72 75 63 74 20 73 6e 69 70  .    struct snip
36445 70 65 74 4d 61 74 63 68 20 2a 70 4d 61 74 63 68  petMatch *pMatch
36446 20 3d 20 26 70 2d 3e 61 4d 61 74 63 68 5b 69 5d   = &p->aMatch[i]
36447 3b 0a 20 20 20 20 69 66 28 20 70 4d 61 74 63 68  ;.    if( pMatch
36448 2d 3e 69 54 65 72 6d 3e 3d 30 20 29 7b 0a 20 20  ->iTerm>=0 ){.  
36449 20 20 20 20 2f 2a 20 49 66 20 73 6e 69 70 70 65      /* If snippe
3644a 74 4d 61 74 63 68 2e 69 54 65 72 6d 20 69 73 20  tMatch.iTerm is 
3644b 6c 65 73 73 20 74 68 61 6e 20 30 2c 20 74 68 65  less than 0, the
3644c 6e 20 74 68 65 20 6d 61 74 63 68 20 77 61 73 20  n the match was 
3644d 0a 20 20 20 20 20 20 2a 2a 20 64 69 73 63 61 72  .      ** discar
3644e 64 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 70  ded as part of p
3644f 72 6f 63 65 73 73 69 6e 67 20 74 68 65 20 4e 45  rocessing the NE
36450 41 52 20 6f 70 65 72 61 74 6f 72 20 28 73 65 65  AR operator (see
36451 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 74   the .      ** t
36452 72 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65 74  rimSnippetOffset
36453 73 46 6f 72 4e 65 61 72 28 29 20 66 75 6e 63 74  sForNear() funct
36454 69 6f 6e 20 66 6f 72 20 64 65 74 61 69 6c 73 29  ion for details)
36455 2e 20 49 67 6e 6f 72 65 20 0a 20 20 20 20 20 20  . Ignore .      
36456 2a 2a 20 69 74 20 69 6e 20 74 68 69 73 20 63 61  ** it in this ca
36457 73 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  se.      */.    
36458 20 20 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27 3b    zBuf[0] = ' ';
36459 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
3645a 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
3645b 42 75 66 29 2d 31 2c 20 26 7a 42 75 66 5b 63 6e  Buf)-1, &zBuf[cn
3645c 74 3e 30 5d 2c 20 22 25 64 20 25 64 20 25 64 20  t>0], "%d %d %d 
3645d 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70  %d",.          p
3645e 4d 61 74 63 68 2d 3e 69 43 6f 6c 2c 20 70 4d 61  Match->iCol, pMa
3645f 74 63 68 2d 3e 69 54 65 72 6d 2c 20 70 4d 61 74  tch->iTerm, pMat
36460 63 68 2d 3e 69 53 74 61 72 74 2c 20 70 4d 61 74  ch->iStart, pMat
36461 63 68 2d 3e 6e 42 79 74 65 29 3b 0a 20 20 20 20  ch->nByte);.    
36462 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 42    append(&sb, zB
36463 75 66 29 3b 0a 20 20 20 20 20 20 63 6e 74 2b 2b  uf);.      cnt++
36464 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 2d  ;.    }.  }.  p-
36465 3e 7a 4f 66 66 73 65 74 20 3d 20 73 74 72 69 6e  >zOffset = strin
36466 67 42 75 66 66 65 72 44 61 74 61 28 26 73 62 29  gBufferData(&sb)
36467 3b 0a 20 20 70 2d 3e 6e 4f 66 66 73 65 74 20 3d  ;.  p->nOffset =
36468 20 73 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e   stringBufferLen
36469 67 74 68 28 26 73 62 29 3b 0a 7d 0a 0a 2f 2a 0a  gth(&sb);.}../*.
3646a 2a 2a 20 7a 44 6f 63 5b 30 2e 2e 6e 44 6f 63 2d  ** zDoc[0..nDoc-
3646b 31 5d 20 69 73 20 70 68 72 61 73 65 20 6f 66 20  1] is phrase of 
3646c 74 65 78 74 2e 20 20 61 4d 61 74 63 68 5b 30 2e  text.  aMatch[0.
3646d 2e 6e 4d 61 74 63 68 2d 31 5d 20 61 72 65 20 61  .nMatch-1] are a
3646e 20 73 65 74 0a 2a 2a 20 6f 66 20 6d 61 74 63 68   set.** of match
3646f 69 6e 67 20 77 6f 72 64 73 20 73 6f 6d 65 20 6f  ing words some o
36470 66 20 77 68 69 63 68 20 6d 69 67 68 74 20 62 65  f which might be
36471 20 69 6e 20 7a 44 6f 63 2e 20 20 7a 44 6f 63 20   in zDoc.  zDoc 
36472 69 73 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 75 6d  is column.** num
36473 62 65 72 20 69 43 6f 6c 2e 0a 2a 2a 0a 2a 2a 20  ber iCol..**.** 
36474 69 42 72 65 61 6b 20 69 73 20 73 75 67 67 65 73  iBreak is sugges
36475 74 65 64 20 73 70 6f 74 20 69 6e 20 7a 44 6f 63  ted spot in zDoc
36476 20 77 68 65 72 65 20 77 65 20 63 6f 75 6c 64 20   where we could 
36477 62 65 67 69 6e 20 6f 72 20 65 6e 64 20 61 6e 0a  begin or end an.
36478 2a 2a 20 65 78 63 65 72 70 74 2e 20 20 52 65 74  ** excerpt.  Ret
36479 75 72 6e 20 61 20 76 61 6c 75 65 20 73 69 6d 69  urn a value simi
3647a 6c 61 72 20 74 6f 20 69 42 72 65 61 6b 20 62 75  lar to iBreak bu
3647b 74 20 70 6f 73 73 69 62 6c 79 20 61 64 6a 75 73  t possibly adjus
3647c 74 65 64 0a 2a 2a 20 74 6f 20 62 65 20 61 20 6c  ted.** to be a l
3647d 69 74 74 6c 65 20 6c 65 66 74 20 6f 72 20 72 69  ittle left or ri
3647e 67 68 74 20 73 6f 20 74 68 61 74 20 74 68 65 20  ght so that the 
3647f 62 72 65 61 6b 20 70 6f 69 6e 74 20 69 73 20 62  break point is b
36480 65 74 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  etter..*/.static
36481 20 69 6e 74 20 77 6f 72 64 42 6f 75 6e 64 61 72   int wordBoundar
36482 79 28 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 2c  y(.  int iBreak,
36483 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36484 20 20 20 2f 2a 20 54 68 65 20 73 75 67 67 65 73     /* The sugges
36485 74 65 64 20 62 72 65 61 6b 20 70 6f 69 6e 74 20  ted break point 
36486 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
36487 2a 7a 44 6f 63 2c 20 20 20 20 20 20 20 20 20 20  *zDoc,          
36488 20 20 20 2f 2a 20 44 6f 63 75 6d 65 6e 74 20 74     /* Document t
36489 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 6f  ext */.  int nDo
3648a 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
3648b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
3648c 20 6f 66 20 62 79 74 65 73 20 69 6e 20 7a 44 6f   of bytes in zDo
3648d 63 5b 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  c[] */.  struct 
3648e 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 61 4d  snippetMatch *aM
3648f 61 74 63 68 2c 20 20 2f 2a 20 4d 61 74 63 68 69  atch,  /* Matchi
36490 6e 67 20 77 6f 72 64 73 20 2a 2f 0a 20 20 69 6e  ng words */.  in
36491 74 20 6e 4d 61 74 63 68 2c 20 20 20 20 20 20 20  t nMatch,       
36492 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
36493 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
36494 20 69 6e 20 61 4d 61 74 63 68 5b 5d 20 2a 2f 0a   in aMatch[] */.
36495 20 20 69 6e 74 20 69 43 6f 6c 20 20 20 20 20 20    int iCol      
36496 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36497 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 75  /* The column nu
36498 6d 62 65 72 20 66 6f 72 20 7a 44 6f 63 5b 5d 20  mber for zDoc[] 
36499 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  */.){.  int i;. 
3649a 20 69 66 28 20 69 42 72 65 61 6b 3c 3d 31 30 20   if( iBreak<=10 
3649b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
3649c 0a 20 20 7d 0a 20 20 69 66 28 20 69 42 72 65 61  .  }.  if( iBrea
3649d 6b 3e 3d 6e 44 6f 63 2d 31 30 20 29 7b 0a 20 20  k>=nDoc-10 ){.  
3649e 20 20 72 65 74 75 72 6e 20 6e 44 6f 63 3b 0a 20    return nDoc;. 
3649f 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
364a0 6e 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63 68  nMatch && aMatch
364a1 5b 69 5d 2e 69 43 6f 6c 3c 69 43 6f 6c 3b 20 69  [i].iCol<iCol; i
364a2 2b 2b 29 7b 7d 0a 20 20 77 68 69 6c 65 28 20 69  ++){}.  while( i
364a3 3c 6e 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63  <nMatch && aMatc
364a4 68 5b 69 5d 2e 69 53 74 61 72 74 2b 61 4d 61 74  h[i].iStart+aMat
364a5 63 68 5b 69 5d 2e 6e 42 79 74 65 3c 69 42 72 65  ch[i].nByte<iBre
364a6 61 6b 20 29 7b 20 69 2b 2b 3b 20 7d 0a 20 20 69  ak ){ i++; }.  i
364a7 66 28 20 69 3c 6e 4d 61 74 63 68 20 29 7b 0a 20  f( i<nMatch ){. 
364a8 20 20 20 69 66 28 20 61 4d 61 74 63 68 5b 69 5d     if( aMatch[i]
364a9 2e 69 53 74 61 72 74 3c 69 42 72 65 61 6b 2b 31  .iStart<iBreak+1
364aa 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
364ab 6e 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61  n aMatch[i].iSta
364ac 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  rt;.    }.    if
364ad 28 20 69 3e 30 20 26 26 20 61 4d 61 74 63 68 5b  ( i>0 && aMatch[
364ae 69 2d 31 5d 2e 69 53 74 61 72 74 2b 61 4d 61 74  i-1].iStart+aMat
364af 63 68 5b 69 2d 31 5d 2e 6e 42 79 74 65 3e 3d 69  ch[i-1].nByte>=i
364b0 42 72 65 61 6b 20 29 7b 0a 20 20 20 20 20 20 72  Break ){.      r
364b1 65 74 75 72 6e 20 61 4d 61 74 63 68 5b 69 2d 31  eturn aMatch[i-1
364b2 5d 2e 69 53 74 61 72 74 3b 0a 20 20 20 20 7d 0a  ].iStart;.    }.
364b3 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69    }.  for(i=1; i
364b4 3c 3d 31 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  <=10; i++){.    
364b5 69 66 28 20 73 61 66 65 5f 69 73 73 70 61 63 65  if( safe_isspace
364b6 28 7a 44 6f 63 5b 69 42 72 65 61 6b 2d 69 5d 29  (zDoc[iBreak-i])
364b7 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
364b8 20 69 42 72 65 61 6b 20 2d 20 69 20 2b 20 31 3b   iBreak - i + 1;
364b9 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
364ba 61 66 65 5f 69 73 73 70 61 63 65 28 7a 44 6f 63  afe_isspace(zDoc
364bb 5b 69 42 72 65 61 6b 2b 69 5d 29 20 29 7b 0a 20  [iBreak+i]) ){. 
364bc 20 20 20 20 20 72 65 74 75 72 6e 20 69 42 72 65       return iBre
364bd 61 6b 20 2b 20 69 20 2b 20 31 3b 0a 20 20 20 20  ak + i + 1;.    
364be 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69  }.  }.  return i
364bf 42 72 65 61 6b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a  Break;.}..../*.*
364c0 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  * Allowed values
364c1 20 66 6f 72 20 53 6e 69 70 70 65 74 2e 61 4d 61   for Snippet.aMa
364c2 74 63 68 5b 5d 2e 73 6e 53 74 61 74 75 73 0a 2a  tch[].snStatus.*
364c3 2f 0a 23 64 65 66 69 6e 65 20 53 4e 49 50 50 45  /.#define SNIPPE
364c4 54 5f 49 47 4e 4f 52 45 20 20 30 20 20 20 2f 2a  T_IGNORE  0   /*
364c5 20 49 74 20 69 73 20 6f 6b 20 74 6f 20 6f 6d 69   It is ok to omi
364c6 74 20 74 68 69 73 20 6d 61 74 63 68 20 66 72 6f  t this match fro
364c7 6d 20 74 68 65 20 73 6e 69 70 70 65 74 20 2a 2f  m the snippet */
364c8 0a 23 64 65 66 69 6e 65 20 53 4e 49 50 50 45 54  .#define SNIPPET
364c9 5f 44 45 53 49 52 45 44 20 31 20 20 20 2f 2a 20  _DESIRED 1   /* 
364ca 57 65 20 77 61 6e 74 20 74 6f 20 69 6e 63 6c 75  We want to inclu
364cb 64 65 20 74 68 69 73 20 6d 61 74 63 68 20 69 6e  de this match in
364cc 20 74 68 65 20 73 6e 69 70 70 65 74 20 2a 2f 0a   the snippet */.
364cd 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
364ce 74 68 65 20 74 65 78 74 20 6f 66 20 61 20 73 6e  the text of a sn
364cf 69 70 70 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  ippet..*/.static
364d0 20 76 6f 69 64 20 73 6e 69 70 70 65 74 54 65 78   void snippetTex
364d1 74 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75  t(.  fulltext_cu
364d2 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20  rsor *pCursor,  
364d3 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 77   /* The cursor w
364d4 65 20 6e 65 65 64 20 74 68 65 20 73 6e 69 70 70  e need the snipp
364d5 65 74 20 66 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73  et for */.  cons
364d6 74 20 63 68 61 72 20 2a 7a 53 74 61 72 74 4d 61  t char *zStartMa
364d7 72 6b 2c 20 20 20 20 20 2f 2a 20 4d 61 72 6b 75  rk,     /* Marku
364d8 70 20 74 6f 20 61 70 70 65 61 72 20 62 65 66 6f  p to appear befo
364d9 72 65 20 65 61 63 68 20 6d 61 74 63 68 20 2a 2f  re each match */
364da 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
364db 45 6e 64 4d 61 72 6b 2c 20 20 20 20 20 20 20 2f  EndMark,       /
364dc 2a 20 4d 61 72 6b 75 70 20 74 6f 20 61 70 70 65  * Markup to appe
364dd 61 72 20 61 66 74 65 72 20 65 61 63 68 20 6d 61  ar after each ma
364de 74 63 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  tch */.  const c
364df 68 61 72 20 2a 7a 45 6c 6c 69 70 73 69 73 20 20  har *zEllipsis  
364e0 20 20 20 20 20 2f 2a 20 45 6c 6c 69 70 73 69 73       /* Ellipsis
364e1 20 6d 61 72 6b 20 2a 2f 0a 29 7b 0a 20 20 69 6e   mark */.){.  in
364e2 74 20 69 2c 20 6a 3b 0a 20 20 73 74 72 75 63 74  t i, j;.  struct
364e3 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 61   snippetMatch *a
364e4 4d 61 74 63 68 3b 0a 20 20 69 6e 74 20 6e 4d 61  Match;.  int nMa
364e5 74 63 68 3b 0a 20 20 69 6e 74 20 6e 44 65 73 69  tch;.  int nDesi
364e6 72 65 64 3b 0a 20 20 53 74 72 69 6e 67 42 75 66  red;.  StringBuf
364e7 66 65 72 20 73 62 3b 0a 20 20 69 6e 74 20 74 61  fer sb;.  int ta
364e8 69 6c 43 6f 6c 3b 0a 20 20 69 6e 74 20 74 61 69  ilCol;.  int tai
364e9 6c 4f 66 66 73 65 74 3b 0a 20 20 69 6e 74 20 69  lOffset;.  int i
364ea 43 6f 6c 3b 0a 20 20 69 6e 74 20 6e 44 6f 63 3b  Col;.  int nDoc;
364eb 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
364ec 44 6f 63 3b 0a 20 20 69 6e 74 20 69 53 74 61 72  Doc;.  int iStar
364ed 74 2c 20 69 45 6e 64 3b 0a 20 20 69 6e 74 20 74  t, iEnd;.  int t
364ee 61 69 6c 45 6c 6c 69 70 73 69 73 20 3d 20 30 3b  ailEllipsis = 0;
364ef 0a 20 20 69 6e 74 20 69 4d 61 74 63 68 3b 0a 20  .  int iMatch;. 
364f0 20 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   ..  sqlite3_fre
364f1 65 28 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70  e(pCursor->snipp
364f2 65 74 2e 7a 53 6e 69 70 70 65 74 29 3b 0a 20 20  et.zSnippet);.  
364f3 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74  pCursor->snippet
364f4 2e 7a 53 6e 69 70 70 65 74 20 3d 20 30 3b 0a 20  .zSnippet = 0;. 
364f5 20 61 4d 61 74 63 68 20 3d 20 70 43 75 72 73 6f   aMatch = pCurso
364f6 72 2d 3e 73 6e 69 70 70 65 74 2e 61 4d 61 74 63  r->snippet.aMatc
364f7 68 3b 0a 20 20 6e 4d 61 74 63 68 20 3d 20 70 43  h;.  nMatch = pC
364f8 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 6e  ursor->snippet.n
364f9 4d 61 74 63 68 3b 0a 20 20 69 6e 69 74 53 74 72  Match;.  initStr
364fa 69 6e 67 42 75 66 66 65 72 28 26 73 62 29 3b 0a  ingBuffer(&sb);.
364fb 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4d  .  for(i=0; i<nM
364fc 61 74 63 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  atch; i++){.    
364fd 61 4d 61 74 63 68 5b 69 5d 2e 73 6e 53 74 61 74  aMatch[i].snStat
364fe 75 73 20 3d 20 53 4e 49 50 50 45 54 5f 49 47 4e  us = SNIPPET_IGN
364ff 4f 52 45 3b 0a 20 20 7d 0a 20 20 6e 44 65 73 69  ORE;.  }.  nDesi
36500 72 65 64 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69  red = 0;.  for(i
36501 3d 30 3b 20 69 3c 46 54 53 33 5f 52 4f 54 4f 52  =0; i<FTS3_ROTOR
36502 5f 53 5a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 66  _SZ; i++){.    f
36503 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 4d 61 74 63 68  or(j=0; j<nMatch
36504 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; j++){.      if
36505 28 20 61 4d 61 74 63 68 5b 6a 5d 2e 69 54 65 72  ( aMatch[j].iTer
36506 6d 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 20 20  m==i ){.        
36507 61 4d 61 74 63 68 5b 6a 5d 2e 73 6e 53 74 61 74  aMatch[j].snStat
36508 75 73 20 3d 20 53 4e 49 50 50 45 54 5f 44 45 53  us = SNIPPET_DES
36509 49 52 45 44 3b 0a 20 20 20 20 20 20 20 20 6e 44  IRED;.        nD
3650a 65 73 69 72 65 64 2b 2b 3b 0a 20 20 20 20 20 20  esired++;.      
3650b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
3650c 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 4d  .    }.  }..  iM
3650d 61 74 63 68 20 3d 20 30 3b 0a 20 20 74 61 69 6c  atch = 0;.  tail
3650e 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 74 61 69 6c  Col = -1;.  tail
3650f 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 66 6f  Offset = 0;.  fo
36510 72 28 69 3d 30 3b 20 69 3c 6e 4d 61 74 63 68 20  r(i=0; i<nMatch 
36511 26 26 20 6e 44 65 73 69 72 65 64 3e 30 3b 20 69  && nDesired>0; i
36512 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 4d 61  ++){.    if( aMa
36513 74 63 68 5b 69 5d 2e 73 6e 53 74 61 74 75 73 21  tch[i].snStatus!
36514 3d 53 4e 49 50 50 45 54 5f 44 45 53 49 52 45 44  =SNIPPET_DESIRED
36515 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
36516 20 6e 44 65 73 69 72 65 64 2d 2d 3b 0a 20 20 20   nDesired--;.   
36517 20 69 43 6f 6c 20 3d 20 61 4d 61 74 63 68 5b 69   iCol = aMatch[i
36518 5d 2e 69 43 6f 6c 3b 0a 20 20 20 20 7a 44 6f 63  ].iCol;.    zDoc
36519 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29   = (const char*)
3651a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
3651b 65 78 74 28 70 43 75 72 73 6f 72 2d 3e 70 53 74  ext(pCursor->pSt
3651c 6d 74 2c 20 69 43 6f 6c 2b 31 29 3b 0a 20 20 20  mt, iCol+1);.   
3651d 20 6e 44 6f 63 20 3d 20 73 71 6c 69 74 65 33 5f   nDoc = sqlite3_
3651e 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 43 75  column_bytes(pCu
3651f 72 73 6f 72 2d 3e 70 53 74 6d 74 2c 20 69 43 6f  rsor->pStmt, iCo
36520 6c 2b 31 29 3b 0a 20 20 20 20 69 53 74 61 72 74  l+1);.    iStart
36521 20 3d 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74   = aMatch[i].iSt
36522 61 72 74 20 2d 20 34 30 3b 0a 20 20 20 20 69 53  art - 40;.    iS
36523 74 61 72 74 20 3d 20 77 6f 72 64 42 6f 75 6e 64  tart = wordBound
36524 61 72 79 28 69 53 74 61 72 74 2c 20 7a 44 6f 63  ary(iStart, zDoc
36525 2c 20 6e 44 6f 63 2c 20 61 4d 61 74 63 68 2c 20  , nDoc, aMatch, 
36526 6e 4d 61 74 63 68 2c 20 69 43 6f 6c 29 3b 0a 20  nMatch, iCol);. 
36527 20 20 20 69 66 28 20 69 53 74 61 72 74 3c 3d 31     if( iStart<=1
36528 30 20 29 7b 0a 20 20 20 20 20 20 69 53 74 61 72  0 ){.      iStar
36529 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  t = 0;.    }.   
3652a 20 69 66 28 20 69 43 6f 6c 3d 3d 74 61 69 6c 43   if( iCol==tailC
3652b 6f 6c 20 26 26 20 69 53 74 61 72 74 3c 3d 74 61  ol && iStart<=ta
3652c 69 6c 4f 66 66 73 65 74 2b 32 30 20 29 7b 0a 20  ilOffset+20 ){. 
3652d 20 20 20 20 20 69 53 74 61 72 74 20 3d 20 74 61       iStart = ta
3652e 69 6c 4f 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a  ilOffset;.    }.
3652f 20 20 20 20 69 66 28 20 28 69 43 6f 6c 21 3d 74      if( (iCol!=t
36530 61 69 6c 43 6f 6c 20 26 26 20 74 61 69 6c 43 6f  ailCol && tailCo
36531 6c 3e 3d 30 29 20 7c 7c 20 69 53 74 61 72 74 21  l>=0) || iStart!
36532 3d 74 61 69 6c 4f 66 66 73 65 74 20 29 7b 0a 20  =tailOffset ){. 
36533 20 20 20 20 20 74 72 69 6d 57 68 69 74 65 53 70       trimWhiteSp
36534 61 63 65 28 26 73 62 29 3b 0a 20 20 20 20 20 20  ace(&sb);.      
36535 61 70 70 65 6e 64 57 68 69 74 65 53 70 61 63 65  appendWhiteSpace
36536 28 26 73 62 29 3b 0a 20 20 20 20 20 20 61 70 70  (&sb);.      app
36537 65 6e 64 28 26 73 62 2c 20 7a 45 6c 6c 69 70 73  end(&sb, zEllips
36538 69 73 29 3b 0a 20 20 20 20 20 20 61 70 70 65 6e  is);.      appen
36539 64 57 68 69 74 65 53 70 61 63 65 28 26 73 62 29  dWhiteSpace(&sb)
3653a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 45 6e 64  ;.    }.    iEnd
3653b 20 3d 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74   = aMatch[i].iSt
3653c 61 72 74 20 2b 20 61 4d 61 74 63 68 5b 69 5d 2e  art + aMatch[i].
3653d 6e 42 79 74 65 20 2b 20 34 30 3b 0a 20 20 20 20  nByte + 40;.    
3653e 69 45 6e 64 20 3d 20 77 6f 72 64 42 6f 75 6e 64  iEnd = wordBound
3653f 61 72 79 28 69 45 6e 64 2c 20 7a 44 6f 63 2c 20  ary(iEnd, zDoc, 
36540 6e 44 6f 63 2c 20 61 4d 61 74 63 68 2c 20 6e 4d  nDoc, aMatch, nM
36541 61 74 63 68 2c 20 69 43 6f 6c 29 3b 0a 20 20 20  atch, iCol);.   
36542 20 69 66 28 20 69 45 6e 64 3e 3d 6e 44 6f 63 2d   if( iEnd>=nDoc-
36543 31 30 20 29 7b 0a 20 20 20 20 20 20 69 45 6e 64  10 ){.      iEnd
36544 20 3d 20 6e 44 6f 63 3b 0a 20 20 20 20 20 20 74   = nDoc;.      t
36545 61 69 6c 45 6c 6c 69 70 73 69 73 20 3d 20 30 3b  ailEllipsis = 0;
36546 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
36547 20 20 74 61 69 6c 45 6c 6c 69 70 73 69 73 20 3d    tailEllipsis =
36548 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 68   1;.    }.    wh
36549 69 6c 65 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74  ile( iMatch<nMat
3654a 63 68 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61  ch && aMatch[iMa
3654b 74 63 68 5d 2e 69 43 6f 6c 3c 69 43 6f 6c 20 29  tch].iCol<iCol )
3654c 7b 20 69 4d 61 74 63 68 2b 2b 3b 20 7d 0a 20 20  { iMatch++; }.  
3654d 20 20 77 68 69 6c 65 28 20 69 53 74 61 72 74 3c    while( iStart<
3654e 69 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 77 68  iEnd ){.      wh
3654f 69 6c 65 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74  ile( iMatch<nMat
36550 63 68 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61  ch && aMatch[iMa
36551 74 63 68 5d 2e 69 53 74 61 72 74 3c 69 53 74 61  tch].iStart<iSta
36552 72 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  rt.             
36553 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68  && aMatch[iMatch
36554 5d 2e 69 43 6f 6c 3c 3d 69 43 6f 6c 20 29 7b 0a  ].iCol<=iCol ){.
36555 20 20 20 20 20 20 20 20 69 4d 61 74 63 68 2b 2b          iMatch++
36556 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
36557 69 66 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63  if( iMatch<nMatc
36558 68 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74  h && aMatch[iMat
36559 63 68 5d 2e 69 53 74 61 72 74 3c 69 45 6e 64 0a  ch].iStart<iEnd.
3655a 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
3655b 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69  aMatch[iMatch].i
3655c 43 6f 6c 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20  Col==iCol ){.   
3655d 20 20 20 20 20 6e 61 70 70 65 6e 64 28 26 73 62       nappend(&sb
3655e 2c 20 26 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c  , &zDoc[iStart],
3655f 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e   aMatch[iMatch].
36560 69 53 74 61 72 74 20 2d 20 69 53 74 61 72 74 29  iStart - iStart)
36561 3b 0a 20 20 20 20 20 20 20 20 69 53 74 61 72 74  ;.        iStart
36562 20 3d 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68   = aMatch[iMatch
36563 5d 2e 69 53 74 61 72 74 3b 0a 20 20 20 20 20 20  ].iStart;.      
36564 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 53    append(&sb, zS
36565 74 61 72 74 4d 61 72 6b 29 3b 0a 20 20 20 20 20  tartMark);.     
36566 20 20 20 6e 61 70 70 65 6e 64 28 26 73 62 2c 20     nappend(&sb, 
36567 26 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c 20 61  &zDoc[iStart], a
36568 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 6e 42  Match[iMatch].nB
36569 79 74 65 29 3b 0a 20 20 20 20 20 20 20 20 61 70  yte);.        ap
3656a 70 65 6e 64 28 26 73 62 2c 20 7a 45 6e 64 4d 61  pend(&sb, zEndMa
3656b 72 6b 29 3b 0a 20 20 20 20 20 20 20 20 69 53 74  rk);.        iSt
3656c 61 72 74 20 2b 3d 20 61 4d 61 74 63 68 5b 69 4d  art += aMatch[iM
3656d 61 74 63 68 5d 2e 6e 42 79 74 65 3b 0a 20 20 20  atch].nByte;.   
3656e 20 20 20 20 20 66 6f 72 28 6a 3d 69 4d 61 74 63       for(j=iMatc
3656f 68 2b 31 3b 20 6a 3c 6e 4d 61 74 63 68 3b 20 6a  h+1; j<nMatch; j
36570 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
36571 66 28 20 61 4d 61 74 63 68 5b 6a 5d 2e 69 54 65  f( aMatch[j].iTe
36572 72 6d 3d 3d 61 4d 61 74 63 68 5b 69 4d 61 74 63  rm==aMatch[iMatc
36573 68 5d 2e 69 54 65 72 6d 0a 20 20 20 20 20 20 20  h].iTerm.       
36574 20 20 20 20 20 20 20 26 26 20 61 4d 61 74 63 68         && aMatch
36575 5b 6a 5d 2e 73 6e 53 74 61 74 75 73 3d 3d 53 4e  [j].snStatus==SN
36576 49 50 50 45 54 5f 44 45 53 49 52 45 44 20 29 7b  IPPET_DESIRED ){
36577 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 44 65  .            nDe
36578 73 69 72 65 64 2d 2d 3b 0a 20 20 20 20 20 20 20  sired--;.       
36579 20 20 20 20 20 61 4d 61 74 63 68 5b 6a 5d 2e 73       aMatch[j].s
3657a 6e 53 74 61 74 75 73 20 3d 20 53 4e 49 50 50 45  nStatus = SNIPPE
3657b 54 5f 49 47 4e 4f 52 45 3b 0a 20 20 20 20 20 20  T_IGNORE;.      
3657c 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
3657d 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
3657e 20 20 20 20 20 6e 61 70 70 65 6e 64 28 26 73 62       nappend(&sb
3657f 2c 20 26 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c  , &zDoc[iStart],
36580 20 69 45 6e 64 20 2d 20 69 53 74 61 72 74 29 3b   iEnd - iStart);
36581 0a 20 20 20 20 20 20 20 20 69 53 74 61 72 74 20  .        iStart 
36582 3d 20 69 45 6e 64 3b 0a 20 20 20 20 20 20 7d 0a  = iEnd;.      }.
36583 20 20 20 20 7d 0a 20 20 20 20 74 61 69 6c 43 6f      }.    tailCo
36584 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 74 61  l = iCol;.    ta
36585 69 6c 4f 66 66 73 65 74 20 3d 20 69 45 6e 64 3b  ilOffset = iEnd;
36586 0a 20 20 7d 0a 20 20 74 72 69 6d 57 68 69 74 65  .  }.  trimWhite
36587 53 70 61 63 65 28 26 73 62 29 3b 0a 20 20 69 66  Space(&sb);.  if
36588 28 20 74 61 69 6c 45 6c 6c 69 70 73 69 73 20 29  ( tailEllipsis )
36589 7b 0a 20 20 20 20 61 70 70 65 6e 64 57 68 69 74  {.    appendWhit
3658a 65 53 70 61 63 65 28 26 73 62 29 3b 0a 20 20 20  eSpace(&sb);.   
3658b 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 45 6c   append(&sb, zEl
3658c 6c 69 70 73 69 73 29 3b 0a 20 20 7d 0a 20 20 70  lipsis);.  }.  p
3658d 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e  Cursor->snippet.
3658e 7a 53 6e 69 70 70 65 74 20 3d 20 73 74 72 69 6e  zSnippet = strin
3658f 67 42 75 66 66 65 72 44 61 74 61 28 26 73 62 29  gBufferData(&sb)
36590 3b 0a 20 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69  ;.  pCursor->sni
36591 70 70 65 74 2e 6e 53 6e 69 70 70 65 74 20 3d 20  ppet.nSnippet = 
36592 73 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67  stringBufferLeng
36593 74 68 28 26 73 62 29 3b 0a 7d 0a 0a 0a 2f 2a 0a  th(&sb);.}.../*.
36594 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 63 75 72  ** Close the cur
36595 73 6f 72 2e 20 20 46 6f 72 20 61 64 64 69 74 69  sor.  For additi
36596 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
36597 20 73 65 65 20 74 68 65 20 64 6f 63 75 6d 65 6e   see the documen
36598 74 61 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 68 65  tation.** on the
36599 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 6f   xClose method o
3659a 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  f the virtual ta
3659b 62 6c 65 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a  ble interface..*
3659c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c  /.static int ful
3659d 6c 74 65 78 74 43 6c 6f 73 65 28 73 71 6c 69 74  ltextClose(sqlit
3659e 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a  e3_vtab_cursor *
3659f 70 43 75 72 73 6f 72 29 7b 0a 20 20 66 75 6c 6c  pCursor){.  full
365a0 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 63 20 3d  text_cursor *c =
365a1 20 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f   (fulltext_curso
365a2 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20  r *) pCursor;.  
365a3 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20  FTSTRACE(("FTS3 
365a4 43 6c 6f 73 65 20 25 70 5c 6e 22 2c 20 63 29 29  Close %p\n", c))
365a5 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  ;.  sqlite3_fina
365a6 6c 69 7a 65 28 63 2d 3e 70 53 74 6d 74 29 3b 0a  lize(c->pStmt);.
365a7 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70    sqlite3Fts3Exp
365a8 72 46 72 65 65 28 63 2d 3e 70 45 78 70 72 29 3b  rFree(c->pExpr);
365a9 0a 20 20 73 6e 69 70 70 65 74 43 6c 65 61 72 28  .  snippetClear(
365aa 26 63 2d 3e 73 6e 69 70 70 65 74 29 3b 0a 20 20  &c->snippet);.  
365ab 69 66 28 20 63 2d 3e 72 65 73 75 6c 74 2e 6e 44  if( c->result.nD
365ac 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 64 6c  ata!=0 ){.    dl
365ad 72 44 65 73 74 72 6f 79 28 26 63 2d 3e 72 65 61  rDestroy(&c->rea
365ae 64 65 72 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61  der);.  }.  data
365af 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 63  BufferDestroy(&c
365b0 2d 3e 72 65 73 75 6c 74 29 3b 0a 20 20 73 71 6c  ->result);.  sql
365b1 69 74 65 33 5f 66 72 65 65 28 63 29 3b 0a 20 20  ite3_free(c);.  
365b2 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
365b3 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
365b4 66 75 6c 6c 74 65 78 74 4e 65 78 74 28 73 71 6c  fulltextNext(sql
365b5 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
365b6 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 66 75   *pCursor){.  fu
365b7 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 63  lltext_cursor *c
365b8 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63 75 72   = (fulltext_cur
365b9 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a  sor *) pCursor;.
365ba 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 46 54 53    int rc;..  FTS
365bb 54 52 41 43 45 28 28 22 46 54 53 33 20 4e 65 78  TRACE(("FTS3 Nex
365bc 74 20 25 70 5c 6e 22 2c 20 70 43 75 72 73 6f 72  t %p\n", pCursor
365bd 29 29 3b 0a 20 20 73 6e 69 70 70 65 74 43 6c 65  ));.  snippetCle
365be 61 72 28 26 63 2d 3e 73 6e 69 70 70 65 74 29 3b  ar(&c->snippet);
365bf 0a 20 20 69 66 28 20 63 2d 3e 69 43 75 72 73 6f  .  if( c->iCurso
365c0 72 54 79 70 65 20 3c 20 51 55 45 52 59 5f 46 55  rType < QUERY_FU
365c1 4c 4c 54 45 58 54 20 29 7b 0a 20 20 20 20 2f 2a  LLTEXT ){.    /*
365c2 20 54 4f 44 4f 28 73 68 65 73 73 29 20 48 61 6e   TODO(shess) Han
365c3 64 6c 65 20 53 51 4c 49 54 45 5f 53 43 48 45 4d  dle SQLITE_SCHEM
365c4 41 20 41 4e 44 20 53 51 4c 49 54 45 5f 42 55 53  A AND SQLITE_BUS
365c5 59 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73  Y. */.    rc = s
365c6 71 6c 69 74 65 33 5f 73 74 65 70 28 63 2d 3e 70  qlite3_step(c->p
365c7 53 74 6d 74 29 3b 0a 20 20 20 20 73 77 69 74 63  Stmt);.    switc
365c8 68 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 63  h( rc ){.      c
365c9 61 73 65 20 53 51 4c 49 54 45 5f 52 4f 57 3a 0a  ase SQLITE_ROW:.
365ca 20 20 20 20 20 20 20 20 63 2d 3e 65 6f 66 20 3d          c->eof =
365cb 20 30 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75   0;.        retu
365cc 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
365cd 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
365ce 44 4f 4e 45 3a 0a 20 20 20 20 20 20 20 20 63 2d  DONE:.        c-
365cf 3e 65 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20  >eof = 1;.      
365d0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
365d1 4f 4b 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c  OK;.      defaul
365d2 74 3a 0a 20 20 20 20 20 20 20 20 63 2d 3e 65 6f  t:.        c->eo
365d3 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72  f = 1;.        r
365d4 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
365d5 20 20 7d 20 65 6c 73 65 20 7b 20 20 2f 2a 20 66    } else {  /* f
365d6 75 6c 6c 2d 74 65 78 74 20 71 75 65 72 79 20 2a  ull-text query *
365d7 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
365d8 65 33 5f 72 65 73 65 74 28 63 2d 3e 70 53 74 6d  e3_reset(c->pStm
365d9 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  t);.    if( rc!=
365da 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
365db 72 6e 20 72 63 3b 0a 0a 20 20 20 20 69 66 28 20  rn rc;..    if( 
365dc 63 2d 3e 72 65 73 75 6c 74 2e 6e 44 61 74 61 3d  c->result.nData=
365dd 3d 30 20 7c 7c 20 64 6c 72 41 74 45 6e 64 28 26  =0 || dlrAtEnd(&
365de 63 2d 3e 72 65 61 64 65 72 29 20 29 7b 0a 20 20  c->reader) ){.  
365df 20 20 20 20 63 2d 3e 65 6f 66 20 3d 20 31 3b 0a      c->eof = 1;.
365e0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
365e1 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20  ITE_OK;.    }.  
365e2 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
365e3 69 6e 64 5f 69 6e 74 36 34 28 63 2d 3e 70 53 74  ind_int64(c->pSt
365e4 6d 74 2c 20 31 2c 20 64 6c 72 44 6f 63 69 64 28  mt, 1, dlrDocid(
365e5 26 63 2d 3e 72 65 61 64 65 72 29 29 3b 0a 20 20  &c->reader));.  
365e6 20 20 64 6c 72 53 74 65 70 28 26 63 2d 3e 72 65    dlrStep(&c->re
365e7 61 64 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72  ader);.    if( r
365e8 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
365e9 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 2f 2a  eturn rc;.    /*
365ea 20 54 4f 44 4f 28 73 68 65 73 73 29 20 48 61 6e   TODO(shess) Han
365eb 64 6c 65 20 53 51 4c 49 54 45 5f 53 43 48 45 4d  dle SQLITE_SCHEM
365ec 41 20 41 4e 44 20 53 51 4c 49 54 45 5f 42 55 53  A AND SQLITE_BUS
365ed 59 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73  Y. */.    rc = s
365ee 71 6c 69 74 65 33 5f 73 74 65 70 28 63 2d 3e 70  qlite3_step(c->p
365ef 53 74 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 72  Stmt);.    if( r
365f0 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  c==SQLITE_ROW ){
365f1 20 20 20 2f 2a 20 74 68 65 20 63 61 73 65 20 77     /* the case w
365f2 65 20 65 78 70 65 63 74 20 2a 2f 0a 20 20 20 20  e expect */.    
365f3 20 20 63 2d 3e 65 6f 66 20 3d 20 30 3b 0a 20 20    c->eof = 0;.  
365f4 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
365f5 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  E_OK;.    }.    
365f6 2f 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  /* an error occu
365f7 72 72 65 64 3b 20 61 62 6f 72 74 20 2a 2f 0a 20  rred; abort */. 
365f8 20 20 20 72 65 74 75 72 6e 20 72 63 3d 3d 53 51     return rc==SQ
365f9 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c 49  LITE_DONE ? SQLI
365fa 54 45 5f 45 52 52 4f 52 20 3a 20 72 63 3b 0a 20  TE_ERROR : rc;. 
365fb 20 7d 0a 7d 0a 0a 0a 2f 2a 20 54 4f 44 4f 28 73   }.}.../* TODO(s
365fc 68 65 73 73 29 20 49 66 20 77 65 20 70 75 73 68  hess) If we push
365fd 65 64 20 4c 65 61 66 52 65 61 64 65 72 20 74 6f  ed LeafReader to
365fe 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20   the top of the 
365ff 66 69 6c 65 2c 20 6f 72 20 74 6f 0a 2a 2a 20 61  file, or to.** a
36600 6e 6f 74 68 65 72 20 66 69 6c 65 2c 20 74 65 72  nother file, ter
36601 6d 5f 73 65 6c 65 63 74 28 29 20 63 6f 75 6c 64  m_select() could
36602 20 62 65 20 70 75 73 68 65 64 20 61 62 6f 76 65   be pushed above
36603 0a 2a 2a 20 64 6f 63 4c 69 73 74 4f 66 54 65 72  .** docListOfTer
36604 6d 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  m()..*/.static i
36605 6e 74 20 74 65 72 6d 53 65 6c 65 63 74 28 66 75  nt termSelect(fu
36606 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
36607 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 0a 20 20 20  int iColumn,.   
36608 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36609 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
3660a 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c  Term, int nTerm,
3660b 20 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20   int isPrefix,. 
3660c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3660d 20 20 20 20 20 44 6f 63 4c 69 73 74 54 79 70 65       DocListType
3660e 20 69 54 79 70 65 2c 20 44 61 74 61 42 75 66 66   iType, DataBuff
3660f 65 72 20 2a 6f 75 74 29 3b 0a 0a 2f 2a 20 0a 2a  er *out);../* .*
36610 2a 20 52 65 74 75 72 6e 20 61 20 44 6f 63 4c 69  * Return a DocLi
36611 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  st corresponding
36612 20 74 6f 20 74 68 65 20 70 68 72 61 73 65 20 2a   to the phrase *
36613 70 50 68 72 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54  pPhrase..**.** T
36614 68 65 20 72 65 73 75 6c 74 69 6e 67 20 44 4c 5f  he resulting DL_
36615 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 20 69  DOCIDS doclist i
36616 73 20 73 74 6f 72 65 64 20 69 6e 20 70 52 65 73  s stored in pRes
36617 75 6c 74 2c 20 77 68 69 63 68 20 69 73 0a 2a 2a  ult, which is.**
36618 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 0a 2a 2f   overwritten..*/
36619 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 63 4c  .static int docL
3661a 69 73 74 4f 66 50 68 72 61 73 65 28 0a 20 20 66  istOfPhrase(.  f
3661b 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 70 54  ulltext_vtab *pT
3661c 61 62 2c 20 20 20 2f 2a 20 54 68 65 20 66 75 6c  ab,   /* The ful
3661d 6c 20 74 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a  l text index */.
3661e 20 20 46 74 73 33 50 68 72 61 73 65 20 2a 70 50    Fts3Phrase *pP
3661f 68 72 61 73 65 2c 20 20 20 2f 2a 20 50 68 72 61  hrase,   /* Phra
36620 73 65 20 74 6f 20 72 65 74 75 72 6e 20 61 20 64  se to return a d
36621 6f 63 6c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e  oclist correspon
36622 64 69 6e 67 20 74 6f 20 2a 2f 0a 20 20 44 6f 63  ding to */.  Doc
36623 4c 69 73 74 54 79 70 65 20 65 4c 69 73 74 54 79  ListType eListTy
36624 70 65 2c 20 2f 2a 20 45 69 74 68 65 72 20 44 4c  pe, /* Either DL
36625 5f 44 4f 43 49 44 53 20 6f 72 20 44 4c 5f 50 4f  _DOCIDS or DL_PO
36626 53 49 54 49 4f 4e 53 20 2a 2f 0a 20 20 44 61 74  SITIONS */.  Dat
36627 61 42 75 66 66 65 72 20 2a 70 52 65 73 75 6c 74  aBuffer *pResult
36628 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
36629 20 72 65 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a   result here */.
3662a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69  ){.  int ii;.  i
3662b 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
3662c 4b 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20  K;.  int iCol = 
3662d 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e  pPhrase->iColumn
3662e 3b 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20  ;.  DocListType 
3662f 65 54 79 70 65 20 3d 20 65 4c 69 73 74 54 79 70  eType = eListTyp
36630 65 3b 0a 20 20 61 73 73 65 72 74 28 20 65 54 79  e;.  assert( eTy
36631 70 65 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  pe==DL_POSITIONS
36632 20 7c 7c 20 65 54 79 70 65 3d 3d 44 4c 5f 44 4f   || eType==DL_DO
36633 43 49 44 53 20 29 3b 0a 20 20 69 66 28 20 70 50  CIDS );.  if( pP
36634 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3e 31 20  hrase->nToken>1 
36635 29 7b 0a 20 20 20 20 65 54 79 70 65 20 3d 20 44  ){.    eType = D
36636 4c 5f 50 4f 53 49 54 49 4f 4e 53 3b 0a 20 20 7d  L_POSITIONS;.  }
36637 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f 64 65  ..  /* This code
36638 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65   should never be
36639 20 63 61 6c 6c 65 64 20 77 69 74 68 20 62 75 66   called with buf
3663a 66 65 72 65 64 20 75 70 64 61 74 65 73 2e 20 2a  fered updates. *
3663b 2f 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  /.  assert( pTab
3663c 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c 30  ->nPendingData<0
3663d 20 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b   );..  for(ii=0;
3663e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
3663f 26 20 69 69 3c 70 50 68 72 61 73 65 2d 3e 6e 54  & ii<pPhrase->nT
36640 6f 6b 65 6e 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  oken; ii++){.   
36641 20 44 61 74 61 42 75 66 66 65 72 20 74 6d 70 3b   DataBuffer tmp;
36642 0a 20 20 20 20 73 74 72 75 63 74 20 50 68 72 61  .    struct Phra
36643 73 65 54 6f 6b 65 6e 20 2a 70 20 3d 20 26 70 50  seToken *p = &pP
36644 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 69  hrase->aToken[ii
36645 5d 3b 0a 20 20 20 20 72 63 20 3d 20 74 65 72 6d  ];.    rc = term
36646 53 65 6c 65 63 74 28 70 54 61 62 2c 20 69 43 6f  Select(pTab, iCo
36647 6c 2c 20 70 2d 3e 7a 2c 20 70 2d 3e 6e 2c 20 70  l, p->z, p->n, p
36648 2d 3e 69 73 50 72 65 66 69 78 2c 20 65 54 79 70  ->isPrefix, eTyp
36649 65 2c 20 26 74 6d 70 29 3b 0a 20 20 20 20 69 66  e, &tmp);.    if
3664a 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
3664b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 69 3d  ){.      if( ii=
3664c 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70  =0 ){.        *p
3664d 52 65 73 75 6c 74 20 3d 20 74 6d 70 3b 0a 20 20  Result = tmp;.  
3664e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3664f 20 20 20 44 61 74 61 42 75 66 66 65 72 20 72 65     DataBuffer re
36650 73 20 3d 20 2a 70 52 65 73 75 6c 74 3b 0a 20 20  s = *pResult;.  
36651 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72        dataBuffer
36652 49 6e 69 74 28 70 52 65 73 75 6c 74 2c 20 30 29  Init(pResult, 0)
36653 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 69  ;.        if( ii
36654 3d 3d 28 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  ==(pPhrase->nTok
36655 65 6e 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20  en-1) ){.       
36656 20 20 20 65 54 79 70 65 20 3d 20 65 4c 69 73 74     eType = eList
36657 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Type;.        }.
36658 20 20 20 20 20 20 20 20 64 6f 63 4c 69 73 74 50          docListP
36659 68 72 61 73 65 4d 65 72 67 65 28 0a 20 20 20 20  hraseMerge(.    
3665a 20 20 20 20 20 20 72 65 73 2e 70 44 61 74 61 2c        res.pData,
3665b 20 72 65 73 2e 6e 44 61 74 61 2c 20 74 6d 70 2e   res.nData, tmp.
3665c 70 44 61 74 61 2c 20 74 6d 70 2e 6e 44 61 74 61  pData, tmp.nData
3665d 2c 20 30 2c 20 30 2c 20 65 54 79 70 65 2c 20 70  , 0, 0, eType, p
3665e 52 65 73 75 6c 74 0a 20 20 20 20 20 20 20 20 29  Result.        )
3665f 3b 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75  ;.        dataBu
36660 66 66 65 72 44 65 73 74 72 6f 79 28 26 72 65 73  fferDestroy(&res
36661 29 3b 0a 20 20 20 20 20 20 20 20 64 61 74 61 42  );.        dataB
36662 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 74 6d  ufferDestroy(&tm
36663 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  p);.      }.    
36664 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
36665 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 76 61  rc;.}../*.** Eva
36666 6c 75 61 74 65 20 74 68 65 20 66 75 6c 6c 2d 74  luate the full-t
36667 65 78 74 20 65 78 70 72 65 73 73 69 6f 6e 20 70  ext expression p
36668 45 78 70 72 20 61 67 61 69 6e 73 74 20 66 74 73  Expr against fts
36669 33 20 74 61 62 6c 65 20 70 54 61 62 2e 20 57 72  3 table pTab. Wr
3666a 69 74 65 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c  ite.** the resul
3666b 74 73 20 69 6e 74 6f 20 70 52 65 73 2e 0a 2a 2f  ts into pRes..*/
3666c 0a 73 74 61 74 69 63 20 69 6e 74 20 65 76 61 6c  .static int eval
3666d 46 74 73 33 45 78 70 72 28 0a 20 20 66 75 6c 6c  Fts3Expr(.  full
3666e 74 65 78 74 5f 76 74 61 62 20 2a 70 54 61 62 2c  text_vtab *pTab,
3666f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 74             /* Ft
36670 73 33 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  s3 Virtual table
36671 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 46 74 73   object */.  Fts
36672 33 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20  3Expr *pExpr,   
36673 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
36674 61 72 73 65 64 20 66 74 73 33 20 65 78 70 72 65  arsed fts3 expre
36675 73 73 69 6f 6e 20 2a 2f 0a 20 20 44 61 74 61 42  ssion */.  DataB
36676 75 66 66 65 72 20 2a 70 52 65 73 20 20 20 20 20  uffer *pRes     
36677 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
36678 3a 20 57 72 69 74 65 20 72 65 73 75 6c 74 73 20  : Write results 
36679 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
3667a 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  n here */.){.  i
3667b 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
3667c 4b 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c  K;..  /* Initial
3667d 69 7a 65 20 74 68 65 20 6f 75 74 70 75 74 20 62  ize the output b
3667e 75 66 66 65 72 2e 20 49 66 20 74 68 69 73 20 69  uffer. If this i
3667f 73 20 61 6e 20 65 6d 70 74 79 20 71 75 65 72 79  s an empty query
36680 20 28 70 45 78 70 72 3d 3d 30 29 2c 20 0a 20 20   (pExpr==0), .  
36681 2a 2a 20 74 68 69 73 20 69 73 20 61 6c 6c 20 74  ** this is all t
36682 68 61 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20  hat needs to be 
36683 64 6f 6e 65 2e 20 45 6d 70 74 79 20 71 75 65 72  done. Empty quer
36684 69 65 73 20 70 72 6f 64 75 63 65 20 65 6d 70 74  ies produce empt
36685 79 20 0a 20 20 2a 2a 20 72 65 73 75 6c 74 20 73  y .  ** result s
36686 65 74 73 2e 0a 20 20 2a 2f 0a 20 20 64 61 74 61  ets..  */.  data
36687 42 75 66 66 65 72 49 6e 69 74 28 70 52 65 73 2c  BufferInit(pRes,
36688 20 30 29 3b 0a 0a 20 20 69 66 28 20 70 45 78 70   0);..  if( pExp
36689 72 20 29 7b 0a 20 20 20 20 69 66 28 20 70 45 78  r ){.    if( pEx
3668a 70 72 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55  pr->eType==FTSQU
3668b 45 52 59 5f 50 48 52 41 53 45 20 29 7b 0a 20 20  ERY_PHRASE ){.  
3668c 20 20 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20      DocListType 
3668d 65 54 79 70 65 20 3d 20 44 4c 5f 44 4f 43 49 44  eType = DL_DOCID
3668e 53 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78  S;.      if( pEx
3668f 70 72 2d 3e 70 50 61 72 65 6e 74 20 26 26 20 70  pr->pParent && p
36690 45 78 70 72 2d 3e 70 50 61 72 65 6e 74 2d 3e 65  Expr->pParent->e
36691 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e  Type==FTSQUERY_N
36692 45 41 52 20 29 7b 0a 20 20 20 20 20 20 20 20 65  EAR ){.        e
36693 54 79 70 65 20 3d 20 44 4c 5f 50 4f 53 49 54 49  Type = DL_POSITI
36694 4f 4e 53 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ONS;.      }.   
36695 20 20 20 72 63 20 3d 20 64 6f 63 4c 69 73 74 4f     rc = docListO
36696 66 50 68 72 61 73 65 28 70 54 61 62 2c 20 70 45  fPhrase(pTab, pE
36697 78 70 72 2d 3e 70 50 68 72 61 73 65 2c 20 65 54  xpr->pPhrase, eT
36698 79 70 65 2c 20 70 52 65 73 29 3b 0a 20 20 20 20  ype, pRes);.    
36699 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 44 61 74  }else{.      Dat
3669a 61 42 75 66 66 65 72 20 6c 68 73 3b 0a 20 20 20  aBuffer lhs;.   
3669b 20 20 20 44 61 74 61 42 75 66 66 65 72 20 72 68     DataBuffer rh
3669c 73 3b 0a 0a 20 20 20 20 20 20 64 61 74 61 42 75  s;..      dataBu
3669d 66 66 65 72 49 6e 69 74 28 26 72 68 73 2c 20 30  fferInit(&rhs, 0
3669e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c  );.      if( SQL
3669f 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 65 76  ITE_OK==(rc = ev
366a0 61 6c 46 74 73 33 45 78 70 72 28 70 54 61 62 2c  alFts3Expr(pTab,
366a1 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26   pExpr->pLeft, &
366a2 6c 68 73 29 29 20 0a 20 20 20 20 20 20 20 26 26  lhs)) .       &&
366a3 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20   SQLITE_OK==(rc 
366a4 3d 20 65 76 61 6c 46 74 73 33 45 78 70 72 28 70  = evalFts3Expr(p
366a5 54 61 62 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  Tab, pExpr->pRig
366a6 68 74 2c 20 26 72 68 73 29 29 20 0a 20 20 20 20  ht, &rhs)) .    
366a7 20 20 29 7b 0a 20 20 20 20 20 20 20 20 73 77 69    ){.        swi
366a8 74 63 68 28 20 70 45 78 70 72 2d 3e 65 54 79 70  tch( pExpr->eTyp
366a9 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63  e ){.          c
366aa 61 73 65 20 46 54 53 51 55 45 52 59 5f 4e 45 41  ase FTSQUERY_NEA
366ab 52 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20  R: {.           
366ac 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20   int nToken;.   
366ad 20 20 20 20 20 20 20 20 20 46 74 73 33 45 78 70           Fts3Exp
366ae 72 20 2a 70 4c 65 66 74 3b 0a 20 20 20 20 20 20  r *pLeft;.      
366af 20 20 20 20 20 20 44 6f 63 4c 69 73 74 54 79 70        DocListTyp
366b0 65 20 65 54 79 70 65 20 3d 20 44 4c 5f 44 4f 43  e eType = DL_DOC
366b1 49 44 53 3b 0a 20 20 20 20 20 20 20 20 20 20 20  IDS;.           
366b2 20 69 66 28 20 70 45 78 70 72 2d 3e 70 50 61 72   if( pExpr->pPar
366b3 65 6e 74 20 26 26 20 70 45 78 70 72 2d 3e 70 50  ent && pExpr->pP
366b4 61 72 65 6e 74 2d 3e 65 54 79 70 65 3d 3d 46 54  arent->eType==FT
366b5 53 51 55 45 52 59 5f 4e 45 41 52 20 29 7b 0a 20  SQUERY_NEAR ){. 
366b6 20 20 20 20 20 20 20 20 20 20 20 20 20 65 54 79               eTy
366b7 70 65 20 3d 20 44 4c 5f 50 4f 53 49 54 49 4f 4e  pe = DL_POSITION
366b8 53 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  S;.            }
366b9 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4c 65  .            pLe
366ba 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66  ft = pExpr->pLef
366bb 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 77  t;.            w
366bc 68 69 6c 65 28 20 70 4c 65 66 74 2d 3e 65 54 79  hile( pLeft->eTy
366bd 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41  pe==FTSQUERY_NEA
366be 52 20 29 7b 20 0a 20 20 20 20 20 20 20 20 20 20  R ){ .          
366bf 20 20 20 20 70 4c 65 66 74 3d 70 4c 65 66 74 2d      pLeft=pLeft-
366c0 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20  >pRight;.       
366c1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
366c2 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
366c3 2d 3e 70 52 69 67 68 74 2d 3e 65 54 79 70 65 3d  ->pRight->eType=
366c4 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45  =FTSQUERY_PHRASE
366c5 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
366c6 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e 65  assert( pLeft->e
366c7 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50  Type==FTSQUERY_P
366c8 48 52 41 53 45 20 29 3b 0a 20 20 20 20 20 20 20  HRASE );.       
366c9 20 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20 70 4c       nToken = pL
366ca 65 66 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54  eft->pPhrase->nT
366cb 6f 6b 65 6e 20 2b 20 70 45 78 70 72 2d 3e 70 52  oken + pExpr->pR
366cc 69 67 68 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e  ight->pPhrase->n
366cd 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 20  Token;.         
366ce 20 20 20 64 6f 63 4c 69 73 74 50 68 72 61 73 65     docListPhrase
366cf 4d 65 72 67 65 28 6c 68 73 2e 70 44 61 74 61 2c  Merge(lhs.pData,
366d0 20 6c 68 73 2e 6e 44 61 74 61 2c 20 72 68 73 2e   lhs.nData, rhs.
366d1 70 44 61 74 61 2c 20 72 68 73 2e 6e 44 61 74 61  pData, rhs.nData
366d2 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
366d3 20 20 20 70 45 78 70 72 2d 3e 6e 4e 65 61 72 2b     pExpr->nNear+
366d4 31 2c 20 6e 54 6f 6b 65 6e 2c 20 65 54 79 70 65  1, nToken, eType
366d5 2c 20 70 52 65 73 0a 20 20 20 20 20 20 20 20 20  , pRes.         
366d6 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20     );.          
366d7 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
366d8 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 63     }.          c
366d9 61 73 65 20 46 54 53 51 55 45 52 59 5f 4e 4f 54  ase FTSQUERY_NOT
366da 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  : {.            
366db 64 6f 63 4c 69 73 74 45 78 63 65 70 74 4d 65 72  docListExceptMer
366dc 67 65 28 6c 68 73 2e 70 44 61 74 61 2c 20 6c 68  ge(lhs.pData, lh
366dd 73 2e 6e 44 61 74 61 2c 20 72 68 73 2e 70 44 61  s.nData, rhs.pDa
366de 74 61 2c 20 72 68 73 2e 6e 44 61 74 61 2c 70 52  ta, rhs.nData,pR
366df 65 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  es);.           
366e0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
366e1 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 63 61    }.          ca
366e2 73 65 20 46 54 53 51 55 45 52 59 5f 41 4e 44 3a  se FTSQUERY_AND:
366e3 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64   {.            d
366e4 6f 63 4c 69 73 74 41 6e 64 4d 65 72 67 65 28 6c  ocListAndMerge(l
366e5 68 73 2e 70 44 61 74 61 2c 20 6c 68 73 2e 6e 44  hs.pData, lhs.nD
366e6 61 74 61 2c 20 72 68 73 2e 70 44 61 74 61 2c 20  ata, rhs.pData, 
366e7 72 68 73 2e 6e 44 61 74 61 2c 20 70 52 65 73 29  rhs.nData, pRes)
366e8 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  ;.            br
366e9 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
366ea 0a 20 20 20 20 20 20 20 20 20 20 63 61 73 65 20  .          case 
366eb 46 54 53 51 55 45 52 59 5f 4f 52 3a 20 7b 0a 20  FTSQUERY_OR: {. 
366ec 20 20 20 20 20 20 20 20 20 20 20 64 6f 63 4c 69             docLi
366ed 73 74 4f 72 4d 65 72 67 65 28 6c 68 73 2e 70 44  stOrMerge(lhs.pD
366ee 61 74 61 2c 20 6c 68 73 2e 6e 44 61 74 61 2c 20  ata, lhs.nData, 
366ef 72 68 73 2e 70 44 61 74 61 2c 20 72 68 73 2e 6e  rhs.pData, rhs.n
366f0 44 61 74 61 2c 20 70 52 65 73 29 3b 0a 20 20 20  Data, pRes);.   
366f1 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
366f2 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
366f3 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
366f4 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65      dataBufferDe
366f5 73 74 72 6f 79 28 26 6c 68 73 29 3b 0a 20 20 20  stroy(&lhs);.   
366f6 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73     dataBufferDes
366f7 74 72 6f 79 28 26 72 68 73 29 3b 0a 20 20 20 20  troy(&rhs);.    
366f8 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
366f9 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73  rc;.}../* TODO(s
366fa 68 65 73 73 29 20 52 65 66 61 63 74 6f 72 20 74  hess) Refactor t
366fb 68 65 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76  he code to remov
366fc 65 20 74 68 69 73 20 66 6f 72 77 61 72 64 20 64  e this forward d
366fd 65 63 6c 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  ecl. */.static i
366fe 6e 74 20 66 6c 75 73 68 50 65 6e 64 69 6e 67 54  nt flushPendingT
366ff 65 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74  erms(fulltext_vt
36700 61 62 20 2a 76 29 3b 0a 0a 2f 2a 20 50 65 72 66  ab *v);../* Perf
36701 6f 72 6d 20 61 20 66 75 6c 6c 2d 74 65 78 74 20  orm a full-text 
36702 71 75 65 72 79 20 75 73 69 6e 67 20 74 68 65 20  query using the 
36703 73 65 61 72 63 68 20 65 78 70 72 65 73 73 69 6f  search expressio
36704 6e 20 69 6e 0a 2a 2a 20 7a 49 6e 70 75 74 5b 30  n in.** zInput[0
36705 2e 2e 6e 49 6e 70 75 74 2d 31 5d 2e 20 20 52 65  ..nInput-1].  Re
36706 74 75 72 6e 20 61 20 6c 69 73 74 20 6f 66 20 6d  turn a list of m
36707 61 74 63 68 69 6e 67 20 64 6f 63 75 6d 65 6e 74  atching document
36708 73 0a 2a 2a 20 69 6e 20 70 52 65 73 75 6c 74 2e  s.** in pResult.
36709 0a 2a 2a 0a 2a 2a 20 51 75 65 72 69 65 73 20 6d  .**.** Queries m
3670a 75 73 74 20 6d 61 74 63 68 20 63 6f 6c 75 6d 6e  ust match column
3670b 20 69 43 6f 6c 75 6d 6e 2e 20 20 4f 72 20 69 66   iColumn.  Or if
3670c 20 69 43 6f 6c 75 6d 6e 3e 3d 6e 43 6f 6c 75 6d   iColumn>=nColum
3670d 6e 0a 2a 2a 20 74 68 65 79 20 61 72 65 20 61 6c  n.** they are al
3670e 6c 6f 77 65 64 20 74 6f 20 6d 61 74 63 68 20 61  lowed to match a
3670f 67 61 69 6e 73 74 20 61 6e 79 20 63 6f 6c 75 6d  gainst any colum
36710 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
36711 20 66 75 6c 6c 74 65 78 74 51 75 65 72 79 28 0a   fulltextQuery(.
36712 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20    fulltext_vtab 
36713 2a 76 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  *v,      /* The 
36714 66 75 6c 6c 20 74 65 78 74 20 69 6e 64 65 78 20  full text index 
36715 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  */.  int iColumn
36716 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  ,           /* M
36717 61 74 63 68 20 61 67 61 69 6e 73 74 20 74 68 69  atch against thi
36718 73 20 63 6f 6c 75 6d 6e 20 62 79 20 64 65 66 61  s column by defa
36719 75 6c 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ult */.  const c
3671a 68 61 72 20 2a 7a 49 6e 70 75 74 2c 20 20 20 20  har *zInput,    
3671b 2f 2a 20 54 68 65 20 71 75 65 72 79 20 73 74 72  /* The query str
3671c 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e  ing */.  int nIn
3671d 70 75 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  put,            
3671e 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
3671f 65 73 20 69 6e 20 7a 49 6e 70 75 74 5b 5d 20 2a  es in zInput[] *
36720 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a  /.  DataBuffer *
36721 70 52 65 73 75 6c 74 2c 20 20 20 2f 2a 20 57 72  pResult,   /* Wr
36722 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 20 64  ite the result d
36723 6f 63 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 20  oclist here */. 
36724 20 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78   Fts3Expr **ppEx
36725 70 72 20 20 20 20 20 20 20 20 2f 2a 20 50 75 74  pr        /* Put
36726 20 70 61 72 73 65 64 20 71 75 65 72 79 20 73 74   parsed query st
36727 72 69 6e 67 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  ring here */.){.
36728 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20    int rc;..  /* 
36729 54 4f 44 4f 28 73 68 65 73 73 29 20 49 6e 73 74  TODO(shess) Inst
3672a 65 61 64 20 6f 66 20 66 6c 75 73 68 69 6e 67 20  ead of flushing 
3672b 70 65 6e 64 69 6e 67 54 65 72 6d 73 2c 20 77 65  pendingTerms, we
3672c 20 63 6f 75 6c 64 20 71 75 65 72 79 20 66 6f 72   could query for
3672d 0a 20 20 2a 2a 20 74 68 65 20 72 65 6c 65 76 61  .  ** the releva
3672e 6e 74 20 74 65 72 6d 20 61 6e 64 20 6d 65 72 67  nt term and merg
3672f 65 20 74 68 65 20 64 6f 63 6c 69 73 74 20 69 6e  e the doclist in
36730 74 6f 20 77 68 61 74 20 77 65 20 72 65 63 65 69  to what we recei
36731 76 65 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65  ve from.  ** the
36732 20 64 61 74 61 62 61 73 65 2e 20 20 57 61 69 74   database.  Wait
36733 20 61 6e 64 20 73 65 65 20 69 66 20 74 68 69 73   and see if this
36734 20 69 73 20 61 20 63 6f 6d 6d 6f 6e 20 69 73 73   is a common iss
36735 75 65 2c 20 66 69 72 73 74 2e 0a 20 20 2a 2a 0a  ue, first..  **.
36736 20 20 2a 2a 20 41 20 67 6f 6f 64 20 72 65 61 73    ** A good reas
36737 6f 6e 20 6e 6f 74 20 74 6f 20 66 6c 75 73 68 20  on not to flush 
36738 69 73 20 74 6f 20 6e 6f 74 20 67 65 6e 65 72 61  is to not genera
36739 74 65 20 75 70 64 61 74 65 2d 72 65 6c 61 74 65  te update-relate
3673a 64 0a 20 20 2a 2a 20 65 72 72 6f 72 20 63 6f 64  d.  ** error cod
3673b 65 73 20 66 72 6f 6d 20 68 65 72 65 2e 0a 20 20  es from here..  
3673c 2a 2f 0a 0a 20 20 2f 2a 20 46 6c 75 73 68 20 61  */..  /* Flush a
3673d 6e 79 20 62 75 66 66 65 72 65 64 20 75 70 64 61  ny buffered upda
3673e 74 65 73 20 62 65 66 6f 72 65 20 65 78 65 63 75  tes before execu
3673f 74 69 6e 67 20 74 68 65 20 71 75 65 72 79 2e 20  ting the query. 
36740 2a 2f 0a 20 20 72 63 20 3d 20 66 6c 75 73 68 50  */.  rc = flushP
36741 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a  endingTerms(v);.
36742 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
36743 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
36744 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  n rc;.  }..  /* 
36745 50 61 72 73 65 20 74 68 65 20 71 75 65 72 79 20  Parse the query 
36746 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 4d 41  passed to the MA
36747 54 43 48 20 6f 70 65 72 61 74 6f 72 2e 20 2a 2f  TCH operator. */
36748 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46  .  rc = sqlite3F
36749 74 73 33 45 78 70 72 50 61 72 73 65 28 76 2d 3e  ts3ExprParse(v->
3674a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 0a 20 20 20  pTokenizer, .   
3674b 20 20 20 76 2d 3e 61 7a 43 6f 6c 75 6d 6e 2c 20     v->azColumn, 
3674c 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 69 43 6f 6c  v->nColumn, iCol
3674d 75 6d 6e 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e  umn, zInput, nIn
3674e 70 75 74 2c 20 70 70 45 78 70 72 0a 20 20 29 3b  put, ppExpr.  );
3674f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
36750 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65  E_OK ){.    asse
36751 72 74 28 20 30 3d 3d 28 2a 70 70 45 78 70 72 29  rt( 0==(*ppExpr)
36752 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   );.    return r
36753 63 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  c;.  }..  return
36754 20 65 76 61 6c 46 74 73 33 45 78 70 72 28 76 2c   evalFts3Expr(v,
36755 20 2a 70 70 45 78 70 72 2c 20 70 52 65 73 75 6c   *ppExpr, pResul
36756 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  t);.}../*.** Thi
36757 73 20 69 73 20 74 68 65 20 78 46 69 6c 74 65 72  s is the xFilter
36758 20 69 6e 74 65 72 66 61 63 65 20 66 6f 72 20 74   interface for t
36759 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
3675a 2e 20 20 53 65 65 0a 2a 2a 20 74 68 65 20 76 69  .  See.** the vi
3675b 72 74 75 61 6c 20 74 61 62 6c 65 20 78 46 69 6c  rtual table xFil
3675c 74 65 72 20 6d 65 74 68 6f 64 20 64 6f 63 75 6d  ter method docum
3675d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64  entation for add
3675e 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72  itional.** infor
3675f 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  mation..**.** If
36760 20 69 64 78 4e 75 6d 3d 3d 51 55 45 52 59 5f 47   idxNum==QUERY_G
36761 45 4e 45 52 49 43 20 74 68 65 6e 20 64 6f 20 61  ENERIC then do a
36762 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e   full table scan
36763 20 61 67 61 69 6e 73 74 0a 2a 2a 20 74 68 65 20   against.** the 
36764 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 2e  %_content table.
36765 0a 2a 2a 0a 2a 2a 20 49 66 20 69 64 78 4e 75 6d  .**.** If idxNum
36766 3d 3d 51 55 45 52 59 5f 44 4f 43 49 44 20 74 68  ==QUERY_DOCID th
36767 65 6e 20 64 6f 20 61 20 64 6f 63 69 64 20 6c 6f  en do a docid lo
36768 6f 6b 75 70 20 66 6f 72 20 61 20 73 69 6e 67 6c  okup for a singl
36769 65 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 74 68  e entry.** in th
3676a 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c  e %_content tabl
3676b 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 64 78 4e  e..**.** If idxN
3676c 75 6d 3e 3d 51 55 45 52 59 5f 46 55 4c 4c 54 45  um>=QUERY_FULLTE
3676d 58 54 20 74 68 65 6e 20 75 73 65 20 74 68 65 20  XT then use the 
3676e 66 75 6c 6c 20 74 65 78 74 20 69 6e 64 65 78 2e  full text index.
3676f 20 20 54 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20    The.** column 
36770 6f 6e 20 74 68 65 20 6c 65 66 74 2d 68 61 6e 64  on the left-hand
36771 20 73 69 64 65 20 6f 66 20 74 68 65 20 4d 41 54   side of the MAT
36772 43 48 20 6f 70 65 72 61 74 6f 72 20 69 73 20 63  CH operator is c
36773 6f 6c 75 6d 6e 0a 2a 2a 20 6e 75 6d 62 65 72 20  olumn.** number 
36774 69 64 78 4e 75 6d 2d 51 55 45 52 59 5f 46 55 4c  idxNum-QUERY_FUL
36775 4c 54 45 58 54 2c 20 30 20 69 6e 64 65 78 65 64  LTEXT, 0 indexed
36776 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 20 74 68  .  argv[0] is th
36777 65 20 72 69 67 68 74 2d 68 61 6e 64 0a 2a 2a 20  e right-hand.** 
36778 73 69 64 65 20 6f 66 20 74 68 65 20 4d 41 54 43  side of the MATC
36779 48 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 2f  H operator..*/./
3677a 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 55 70  * TODO(shess) Up
3677b 67 72 61 64 65 20 74 68 65 20 63 75 72 73 6f 72  grade the cursor
3677c 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
3677d 61 6e 64 20 64 65 73 74 72 75 63 74 69 6f 6e 20  and destruction 
3677e 74 6f 0a 2a 2a 20 61 63 63 6f 75 6e 74 20 66 6f  to.** account fo
3677f 72 20 66 75 6c 6c 74 65 78 74 46 69 6c 74 65 72  r fulltextFilter
36780 28 29 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20  () being called 
36781 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 6f  multiple times o
36782 6e 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 63 75  n the.** same cu
36783 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72 72 65  rsor.  The curre
36784 6e 74 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 76  nt solution is v
36785 65 72 79 20 66 72 61 67 69 6c 65 2e 20 20 41 70  ery fragile.  Ap
36786 70 6c 79 20 66 69 78 20 74 6f 0a 2a 2a 20 66 74  ply fix to.** ft
36787 73 33 20 61 73 20 61 70 70 72 6f 70 72 69 61 74  s3 as appropriat
36788 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
36789 20 66 75 6c 6c 74 65 78 74 46 69 6c 74 65 72 28   fulltextFilter(
3678a 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  .  sqlite3_vtab_
3678b 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c  cursor *pCursor,
3678c 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73       /* The curs
3678d 6f 72 20 75 73 65 64 20 66 6f 72 20 74 68 69 73  or used for this
3678e 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20   query */.  int 
3678f 69 64 78 4e 75 6d 2c 20 63 6f 6e 73 74 20 63 68  idxNum, const ch
36790 61 72 20 2a 69 64 78 53 74 72 2c 20 20 20 2f 2a  ar *idxStr,   /*
36791 20 57 68 69 63 68 20 69 6e 64 65 78 69 6e 67 20   Which indexing 
36792 73 63 68 65 6d 65 20 74 6f 20 75 73 65 20 2a 2f  scheme to use */
36793 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  .  int argc, sql
36794 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
36795 76 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74  v    /* Argument
36796 73 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 69  s for the indexi
36797 6e 67 20 73 63 68 65 6d 65 20 2a 2f 0a 29 7b 0a  ng scheme */.){.
36798 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f    fulltext_curso
36799 72 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78 74  r *c = (fulltext
3679a 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73  _cursor *) pCurs
3679b 6f 72 3b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76  or;.  fulltext_v
3679c 74 61 62 20 2a 76 20 3d 20 63 75 72 73 6f 72 5f  tab *v = cursor_
3679d 76 74 61 62 28 63 29 3b 0a 20 20 69 6e 74 20 72  vtab(c);.  int r
3679e 63 3b 0a 0a 20 20 46 54 53 54 52 41 43 45 28 28  c;..  FTSTRACE((
3679f 22 46 54 53 33 20 46 69 6c 74 65 72 20 25 70 5c  "FTS3 Filter %p\
367a0 6e 22 2c 70 43 75 72 73 6f 72 29 29 3b 0a 0a 20  n",pCursor));.. 
367a1 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f   /* If the curso
367a2 72 20 68 61 73 20 61 20 73 74 61 74 65 6d 65 6e  r has a statemen
367a3 74 20 74 68 61 74 20 77 61 73 20 6e 6f 74 20 70  t that was not p
367a4 72 65 70 61 72 65 64 20 61 63 63 6f 72 64 69 6e  repared accordin
367a5 67 20 74 6f 0a 20 20 2a 2a 20 69 64 78 4e 75 6d  g to.  ** idxNum
367a6 2c 20 63 6c 65 61 72 20 69 74 2e 20 20 49 20 62  , clear it.  I b
367a7 65 6c 69 65 76 65 20 61 6c 6c 20 63 61 6c 6c 73  elieve all calls
367a8 20 74 6f 20 66 75 6c 6c 74 65 78 74 46 69 6c 74   to fulltextFilt
367a9 65 72 20 77 69 74 68 20 61 0a 20 20 2a 2a 20 67  er with a.  ** g
367aa 69 76 65 6e 20 63 75 72 73 6f 72 20 77 69 6c 6c  iven cursor will
367ab 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 69   have the same i
367ac 64 78 4e 75 6d 20 2c 20 62 75 74 20 69 6e 20 74  dxNum , but in t
367ad 68 69 73 20 63 61 73 65 20 69 74 27 73 0a 20 20  his case it's.  
367ae 2a 2a 20 65 61 73 79 20 74 6f 20 62 65 20 73 61  ** easy to be sa
367af 66 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63  fe..  */.  if( c
367b0 2d 3e 70 53 74 6d 74 20 26 26 20 63 2d 3e 69 43  ->pStmt && c->iC
367b1 75 72 73 6f 72 54 79 70 65 21 3d 69 64 78 4e 75  ursorType!=idxNu
367b2 6d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  m ){.    sqlite3
367b3 5f 66 69 6e 61 6c 69 7a 65 28 63 2d 3e 70 53 74  _finalize(c->pSt
367b4 6d 74 29 3b 0a 20 20 20 20 63 2d 3e 70 53 74 6d  mt);.    c->pStm
367b5 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a 0a 20  t = NULL;.  }.. 
367b6 20 2f 2a 20 47 65 74 20 61 20 66 72 65 73 68 20   /* Get a fresh 
367b7 73 74 61 74 65 6d 65 6e 74 20 61 70 70 72 6f 70  statement approp
367b8 72 69 61 74 65 20 74 6f 20 69 64 78 4e 75 6d 2e  riate to idxNum.
367b9 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68   */.  /* TODO(sh
367ba 65 73 73 29 3a 20 41 64 64 20 61 20 70 72 65 70  ess): Add a prep
367bb 61 72 65 64 2d 73 74 61 74 65 6d 65 6e 74 20 63  ared-statement c
367bc 61 63 68 65 20 69 6e 20 74 68 65 20 76 74 20 73  ache in the vt s
367bd 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20 54  tructure..  ** T
367be 68 65 20 63 61 63 68 65 20 6d 75 73 74 20 68 61  he cache must ha
367bf 6e 64 6c 65 20 6d 75 6c 74 69 70 6c 65 20 6f 70  ndle multiple op
367c0 65 6e 20 63 75 72 73 6f 72 73 2e 20 20 45 61 73  en cursors.  Eas
367c1 69 65 72 20 74 6f 20 63 61 63 68 65 20 74 68 65  ier to cache the
367c2 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  .  ** statement 
367c3 76 61 72 69 61 6e 74 73 20 61 74 20 74 68 65 20  variants at the 
367c4 76 74 20 74 6f 20 72 65 64 75 63 65 20 6d 61 6c  vt to reduce mal
367c5 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65  loc/realloc/free
367c6 20 68 65 72 65 2e 0a 20 20 2a 2a 20 4f 72 20 77   here..  ** Or w
367c7 65 20 63 6f 75 6c 64 20 68 61 76 65 20 61 20 53  e could have a S
367c8 74 72 69 6e 67 42 75 66 66 65 72 20 76 61 72 69  tringBuffer vari
367c9 61 6e 74 20 77 68 69 63 68 20 61 6c 6c 6f 77 65  ant which allowe
367ca 64 20 73 74 61 63 6b 0a 20 20 2a 2a 20 63 6f 6e  d stack.  ** con
367cb 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 73 6d  struction for sm
367cc 61 6c 6c 20 76 61 6c 75 65 73 2e 0a 20 20 2a 2f  all values..  */
367cd 0a 20 20 69 66 28 20 21 63 2d 3e 70 53 74 6d 74  .  if( !c->pStmt
367ce 20 29 7b 0a 20 20 20 20 53 74 72 69 6e 67 42 75   ){.    StringBu
367cf 66 66 65 72 20 73 62 3b 0a 20 20 20 20 69 6e 69  ffer sb;.    ini
367d0 74 53 74 72 69 6e 67 42 75 66 66 65 72 28 26 73  tStringBuffer(&s
367d1 62 29 3b 0a 20 20 20 20 61 70 70 65 6e 64 28 26  b);.    append(&
367d2 73 62 2c 20 22 53 45 4c 45 43 54 20 64 6f 63 69  sb, "SELECT doci
367d3 64 2c 20 22 29 3b 0a 20 20 20 20 61 70 70 65 6e  d, ");.    appen
367d4 64 4c 69 73 74 28 26 73 62 2c 20 76 2d 3e 6e 43  dList(&sb, v->nC
367d5 6f 6c 75 6d 6e 2c 20 76 2d 3e 61 7a 43 6f 6e 74  olumn, v->azCont
367d6 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  entColumn);.    
367d7 61 70 70 65 6e 64 28 26 73 62 2c 20 22 20 46 52  append(&sb, " FR
367d8 4f 4d 20 25 5f 63 6f 6e 74 65 6e 74 22 29 3b 0a  OM %_content");.
367d9 20 20 20 20 69 66 28 20 69 64 78 4e 75 6d 21 3d      if( idxNum!=
367da 51 55 45 52 59 5f 47 45 4e 45 52 49 43 20 29 20  QUERY_GENERIC ) 
367db 61 70 70 65 6e 64 28 26 73 62 2c 20 22 20 57 48  append(&sb, " WH
367dc 45 52 45 20 64 6f 63 69 64 20 3d 20 3f 22 29 3b  ERE docid = ?");
367dd 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 5f 70 72  .    rc = sql_pr
367de 65 70 61 72 65 28 76 2d 3e 64 62 2c 20 76 2d 3e  epare(v->db, v->
367df 7a 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c 20 26  zDb, v->zName, &
367e0 63 2d 3e 70 53 74 6d 74 2c 0a 20 20 20 20 20 20  c->pStmt,.      
367e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
367e2 74 72 69 6e 67 42 75 66 66 65 72 44 61 74 61 28  tringBufferData(
367e3 26 73 62 29 29 3b 0a 20 20 20 20 73 74 72 69 6e  &sb));.    strin
367e4 67 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  gBufferDestroy(&
367e5 73 62 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  sb);.    if( rc!
367e6 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
367e7 75 72 6e 20 72 63 3b 0a 20 20 20 20 63 2d 3e 69  urn rc;.    c->i
367e8 43 75 72 73 6f 72 54 79 70 65 20 3d 20 69 64 78  CursorType = idx
367e9 4e 75 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  Num;.  }else{.  
367ea 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28    sqlite3_reset(
367eb 63 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 61  c->pStmt);.    a
367ec 73 73 65 72 74 28 20 63 2d 3e 69 43 75 72 73 6f  ssert( c->iCurso
367ed 72 54 79 70 65 3d 3d 69 64 78 4e 75 6d 20 29 3b  rType==idxNum );
367ee 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63 68 28 20  .  }..  switch( 
367ef 69 64 78 4e 75 6d 20 29 7b 0a 20 20 20 20 63 61  idxNum ){.    ca
367f0 73 65 20 51 55 45 52 59 5f 47 45 4e 45 52 49 43  se QUERY_GENERIC
367f1 3a 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a  :.      break;..
367f2 20 20 20 20 63 61 73 65 20 51 55 45 52 59 5f 44      case QUERY_D
367f3 4f 43 49 44 3a 0a 20 20 20 20 20 20 72 63 20 3d  OCID:.      rc =
367f4 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
367f5 74 36 34 28 63 2d 3e 70 53 74 6d 74 2c 20 31 2c  t64(c->pStmt, 1,
367f6 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
367f7 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 29 3b 0a  nt64(argv[0]));.
367f8 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
367f9 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
367fa 20 72 63 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   rc;.      break
367fb 3b 0a 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  ;..    default: 
367fc 20 20 2f 2a 20 66 75 6c 6c 2d 74 65 78 74 20 73    /* full-text s
367fd 65 61 72 63 68 20 2a 2f 0a 20 20 20 20 7b 0a 20  earch */.    {. 
367fe 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20       int iCol = 
367ff 69 64 78 4e 75 6d 2d 51 55 45 52 59 5f 46 55 4c  idxNum-QUERY_FUL
36800 4c 54 45 58 54 3b 0a 20 20 20 20 20 20 63 6f 6e  LTEXT;.      con
36801 73 74 20 63 68 61 72 20 2a 7a 51 75 65 72 79 20  st char *zQuery 
36802 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
36803 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
36804 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  xt(argv[0]);.   
36805 20 20 20 61 73 73 65 72 74 28 20 69 64 78 4e 75     assert( idxNu
36806 6d 3c 3d 51 55 45 52 59 5f 46 55 4c 4c 54 45 58  m<=QUERY_FULLTEX
36807 54 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20  T+v->nColumn);. 
36808 20 20 20 20 20 61 73 73 65 72 74 28 20 61 72 67       assert( arg
36809 63 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66  c==1 );.      if
3680a 28 20 63 2d 3e 72 65 73 75 6c 74 2e 6e 44 61 74  ( c->result.nDat
3680b 61 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  a!=0 ){.        
3680c 2f 2a 20 54 68 69 73 20 63 61 73 65 20 68 61 70  /* This case hap
3680d 70 65 6e 73 20 69 66 20 74 68 65 20 73 61 6d 65  pens if the same
3680e 20 63 75 72 73 6f 72 20 69 73 20 75 73 65 64 20   cursor is used 
3680f 72 65 70 65 61 74 65 64 6c 79 2e 20 2a 2f 0a 20  repeatedly. */. 
36810 20 20 20 20 20 20 20 64 6c 72 44 65 73 74 72 6f         dlrDestro
36811 79 28 26 63 2d 3e 72 65 61 64 65 72 29 3b 0a 20  y(&c->reader);. 
36812 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65         dataBuffe
36813 72 52 65 73 65 74 28 26 63 2d 3e 72 65 73 75 6c  rReset(&c->resul
36814 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  t);.      }else{
36815 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66  .        dataBuf
36816 66 65 72 49 6e 69 74 28 26 63 2d 3e 72 65 73 75  ferInit(&c->resu
36817 6c 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  lt, 0);.      }.
36818 20 20 20 20 20 20 72 63 20 3d 20 66 75 6c 6c 74        rc = fullt
36819 65 78 74 51 75 65 72 79 28 76 2c 20 69 43 6f 6c  extQuery(v, iCol
3681a 2c 20 7a 51 75 65 72 79 2c 20 2d 31 2c 20 26 63  , zQuery, -1, &c
3681b 2d 3e 72 65 73 75 6c 74 2c 20 26 63 2d 3e 70 45  ->result, &c->pE
3681c 78 70 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  xpr);.      if( 
3681d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
3681e 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
3681f 20 69 66 28 20 63 2d 3e 72 65 73 75 6c 74 2e 6e   if( c->result.n
36820 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 20  Data!=0 ){.     
36821 20 20 20 64 6c 72 49 6e 69 74 28 26 63 2d 3e 72     dlrInit(&c->r
36822 65 61 64 65 72 2c 20 44 4c 5f 44 4f 43 49 44 53  eader, DL_DOCIDS
36823 2c 20 63 2d 3e 72 65 73 75 6c 74 2e 70 44 61 74  , c->result.pDat
36824 61 2c 20 63 2d 3e 72 65 73 75 6c 74 2e 6e 44 61  a, c->result.nDa
36825 74 61 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ta);.      }.   
36826 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
36827 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 66 75    }..  return fu
36828 6c 6c 74 65 78 74 4e 65 78 74 28 70 43 75 72 73  lltextNext(pCurs
36829 6f 72 29 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20  or);.}../* This 
3682a 69 73 20 74 68 65 20 78 45 6f 66 20 6d 65 74 68  is the xEof meth
3682b 6f 64 20 6f 66 20 74 68 65 20 76 69 72 74 75 61  od of the virtua
3682c 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20 53 51  l table.  The SQ
3682d 4c 69 74 65 20 63 6f 72 65 0a 2a 2a 20 63 61 6c  Lite core.** cal
3682e 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
3682f 74 6f 20 66 69 6e 64 20 6f 75 74 20 69 66 20 69  to find out if i
36830 74 20 68 61 73 20 72 65 61 63 68 65 64 20 74 68  t has reached th
36831 65 20 65 6e 64 20 6f 66 0a 2a 2a 20 61 20 71 75  e end of.** a qu
36832 65 72 79 27 73 20 72 65 73 75 6c 74 73 20 73 65  ery's results se
36833 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
36834 20 66 75 6c 6c 74 65 78 74 45 6f 66 28 73 71 6c   fulltextEof(sql
36835 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
36836 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 66 75   *pCursor){.  fu
36837 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 63  lltext_cursor *c
36838 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63 75 72   = (fulltext_cur
36839 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a  sor *) pCursor;.
3683a 20 20 72 65 74 75 72 6e 20 63 2d 3e 65 6f 66 3b    return c->eof;
3683b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 69 73 20 74  .}../* This is t
3683c 68 65 20 78 43 6f 6c 75 6d 6e 20 6d 65 74 68 6f  he xColumn metho
3683d 64 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  d of the virtual
3683e 20 74 61 62 6c 65 2e 20 20 54 68 65 20 53 51 4c   table.  The SQL
3683f 69 74 65 0a 2a 2a 20 63 6f 72 65 20 63 61 6c 6c  ite.** core call
36840 73 20 74 68 69 73 20 6d 65 74 68 6f 64 20 64 75  s this method du
36841 72 69 6e 67 20 61 20 71 75 65 72 79 20 77 68 65  ring a query whe
36842 6e 20 69 74 20 6e 65 65 64 73 20 74 68 65 20 76  n it needs the v
36843 61 6c 75 65 0a 2a 2a 20 6f 66 20 61 20 63 6f 6c  alue.** of a col
36844 75 6d 6e 20 66 72 6f 6d 20 74 68 65 20 76 69 72  umn from the vir
36845 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 69  tual table.  Thi
36846 73 20 6d 65 74 68 6f 64 20 6e 65 65 64 73 20 74  s method needs t
36847 6f 20 75 73 65 0a 2a 2a 20 6f 6e 65 20 6f 66 20  o use.** one of 
36848 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  the sqlite3_resu
36849 6c 74 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20  lt_*() routines 
3684a 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72 65 71  to store the req
3684b 75 65 73 74 65 64 0a 2a 2a 20 76 61 6c 75 65 20  uested.** value 
3684c 62 61 63 6b 20 69 6e 20 74 68 65 20 70 43 6f 6e  back in the pCon
3684d 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  text..*/.static 
3684e 69 6e 74 20 66 75 6c 6c 74 65 78 74 43 6f 6c 75  int fulltextColu
3684f 6d 6e 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  mn(sqlite3_vtab_
36850 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c  cursor *pCursor,
36851 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
36852 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
36853 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e  e3_context *pCon
36854 74 65 78 74 2c 20 69 6e 74 20 69 64 78 43 6f 6c  text, int idxCol
36855 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75  ){.  fulltext_cu
36856 72 73 6f 72 20 2a 63 20 3d 20 28 66 75 6c 6c 74  rsor *c = (fullt
36857 65 78 74 5f 63 75 72 73 6f 72 20 2a 29 20 70 43  ext_cursor *) pC
36858 75 72 73 6f 72 3b 0a 20 20 66 75 6c 6c 74 65 78  ursor;.  fulltex
36859 74 5f 76 74 61 62 20 2a 76 20 3d 20 63 75 72 73  t_vtab *v = curs
3685a 6f 72 5f 76 74 61 62 28 63 29 3b 0a 0a 20 20 69  or_vtab(c);..  i
3685b 66 28 20 69 64 78 43 6f 6c 3c 76 2d 3e 6e 43 6f  f( idxCol<v->nCo
3685c 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69  lumn ){.    sqli
3685d 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20  te3_value *pVal 
3685e 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
3685f 5f 76 61 6c 75 65 28 63 2d 3e 70 53 74 6d 74 2c  _value(c->pStmt,
36860 20 69 64 78 43 6f 6c 2b 31 29 3b 0a 20 20 20 20   idxCol+1);.    
36861 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
36862 61 6c 75 65 28 70 43 6f 6e 74 65 78 74 2c 20 70  alue(pContext, p
36863 56 61 6c 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  Val);.  }else if
36864 28 20 69 64 78 43 6f 6c 3d 3d 76 2d 3e 6e 43 6f  ( idxCol==v->nCo
36865 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 2f 2a 20 54  lumn ){.    /* T
36866 68 65 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e 20  he extra column 
36867 77 68 6f 73 65 20 6e 61 6d 65 20 69 73 20 74 68  whose name is th
36868 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 74 61  e same as the ta
36869 62 6c 65 2e 0a 20 20 20 20 2a 2a 20 52 65 74 75  ble..    ** Retu
3686a 72 6e 20 61 20 62 6c 6f 62 20 77 68 69 63 68 20  rn a blob which 
3686b 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
3686c 74 68 65 20 63 75 72 73 6f 72 0a 20 20 20 20 2a  the cursor.    *
3686d 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  /.    sqlite3_re
3686e 73 75 6c 74 5f 62 6c 6f 62 28 70 43 6f 6e 74 65  sult_blob(pConte
3686f 78 74 2c 20 26 63 2c 20 73 69 7a 65 6f 66 28 63  xt, &c, sizeof(c
36870 29 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  ), SQLITE_TRANSI
36871 45 4e 54 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ENT);.  }else if
36872 28 20 69 64 78 43 6f 6c 3d 3d 76 2d 3e 6e 43 6f  ( idxCol==v->nCo
36873 6c 75 6d 6e 2b 31 20 29 7b 0a 20 20 20 20 2f 2a  lumn+1 ){.    /*
36874 20 54 68 65 20 64 6f 63 69 64 20 63 6f 6c 75 6d   The docid colum
36875 6e 2c 20 77 68 69 63 68 20 69 73 20 61 6e 20 61  n, which is an a
36876 6c 69 61 73 20 66 6f 72 20 72 6f 77 69 64 2e 20  lias for rowid. 
36877 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  */.    sqlite3_v
36878 61 6c 75 65 20 2a 70 56 61 6c 20 3d 20 73 71 6c  alue *pVal = sql
36879 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75  ite3_column_valu
3687a 65 28 63 2d 3e 70 53 74 6d 74 2c 20 30 29 3b 0a  e(c->pStmt, 0);.
3687b 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
3687c 6c 74 5f 76 61 6c 75 65 28 70 43 6f 6e 74 65 78  lt_value(pContex
3687d 74 2c 20 70 56 61 6c 29 3b 0a 20 20 7d 0a 20 20  t, pVal);.  }.  
3687e 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
3687f 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 69 73 20  ;.}../* This is 
36880 74 68 65 20 78 52 6f 77 69 64 20 6d 65 74 68 6f  the xRowid metho
36881 64 2e 20 20 54 68 65 20 53 51 4c 69 74 65 20 63  d.  The SQLite c
36882 6f 72 65 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ore calls this r
36883 6f 75 74 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 74  outine to.** ret
36884 72 69 65 76 65 20 74 68 65 20 72 6f 77 69 64 20  rieve the rowid 
36885 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
36886 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c  row of the resul
36887 74 20 73 65 74 2e 20 20 66 74 73 33 0a 2a 2a 20  t set.  fts3.** 
36888 65 78 70 6f 73 65 73 20 25 5f 63 6f 6e 74 65 6e  exposes %_conten
36889 74 2e 64 6f 63 69 64 20 61 73 20 74 68 65 20 72  t.docid as the r
3688a 6f 77 69 64 20 66 6f 72 20 74 68 65 20 76 69 72  owid for the vir
3688b 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65  tual table.  The
3688c 0a 2a 2a 20 72 6f 77 69 64 20 73 68 6f 75 6c 64  .** rowid should
3688d 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 20 2a   be written to *
3688e 70 52 6f 77 69 64 2e 0a 2a 2f 0a 73 74 61 74 69  pRowid..*/.stati
3688f 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 52 6f  c int fulltextRo
36890 77 69 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62  wid(sqlite3_vtab
36891 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
36892 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a  , sqlite_int64 *
36893 70 52 6f 77 69 64 29 7b 0a 20 20 66 75 6c 6c 74  pRowid){.  fullt
36894 65 78 74 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20  ext_cursor *c = 
36895 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72  (fulltext_cursor
36896 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 0a 20 20   *) pCursor;..  
36897 2a 70 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65  *pRowid = sqlite
36898 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 63  3_column_int64(c
36899 2d 3e 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 72  ->pStmt, 0);.  r
3689a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
3689b 0a 7d 0a 0a 2f 2a 20 41 64 64 20 61 6c 6c 20 74  .}../* Add all t
3689c 65 72 6d 73 20 69 6e 20 5b 7a 54 65 78 74 5d 20  erms in [zText] 
3689d 74 6f 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20  to pendingTerms 
3689e 74 61 62 6c 65 2e 20 20 49 66 20 5b 69 43 6f 6c  table.  If [iCol
3689f 75 6d 6e 5d 20 3e 20 30 2c 0a 2a 2a 20 77 65 20  umn] > 0,.** we 
368a0 61 6c 73 6f 20 73 74 6f 72 65 20 70 6f 73 69 74  also store posit
368a1 69 6f 6e 73 20 61 6e 64 20 6f 66 66 73 65 74 73  ions and offsets
368a2 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62   in the hash tab
368a3 6c 65 20 75 73 69 6e 67 20 74 68 61 74 0a 2a 2a  le using that.**
368a4 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a   column number..
368a5 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 75  */.static int bu
368a6 69 6c 64 54 65 72 6d 73 28 66 75 6c 6c 74 65 78  ildTerms(fulltex
368a7 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74  t_vtab *v, sqlit
368a8 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 2c 0a  e_int64 iDocid,.
368a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
368aa 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
368ab 20 2a 7a 54 65 78 74 2c 20 69 6e 74 20 69 43 6f   *zText, int iCo
368ac 6c 75 6d 6e 29 7b 0a 20 20 73 71 6c 69 74 65 33  lumn){.  sqlite3
368ad 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b  _tokenizer *pTok
368ae 65 6e 69 7a 65 72 20 3d 20 76 2d 3e 70 54 6f 6b  enizer = v->pTok
368af 65 6e 69 7a 65 72 3b 0a 20 20 73 71 6c 69 74 65  enizer;.  sqlite
368b0 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
368b1 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 63  or *pCursor;.  c
368b2 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 6f 6b 65  onst char *pToke
368b3 6e 3b 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 42  n;.  int nTokenB
368b4 79 74 65 73 3b 0a 20 20 69 6e 74 20 69 53 74 61  ytes;.  int iSta
368b5 72 74 4f 66 66 73 65 74 2c 20 69 45 6e 64 4f 66  rtOffset, iEndOf
368b6 66 73 65 74 2c 20 69 50 6f 73 69 74 69 6f 6e 3b  fset, iPosition;
368b7 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63  .  int rc;..  rc
368b8 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70   = pTokenizer->p
368b9 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 70 54  Module->xOpen(pT
368ba 6f 6b 65 6e 69 7a 65 72 2c 20 7a 54 65 78 74 2c  okenizer, zText,
368bb 20 2d 31 2c 20 26 70 43 75 72 73 6f 72 29 3b 0a   -1, &pCursor);.
368bc 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
368bd 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
368be 0a 0a 20 20 70 43 75 72 73 6f 72 2d 3e 70 54 6f  ..  pCursor->pTo
368bf 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e  kenizer = pToken
368c0 69 7a 65 72 3b 0a 20 20 77 68 69 6c 65 28 20 53  izer;.  while( S
368c1 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 3d 70 54  QLITE_OK==(rc=pT
368c2 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c  okenizer->pModul
368c3 65 2d 3e 78 4e 65 78 74 28 70 43 75 72 73 6f 72  e->xNext(pCursor
368c4 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
368c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
368c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
368c7 20 20 20 20 20 26 70 54 6f 6b 65 6e 2c 20 26 6e       &pToken, &n
368c8 54 6f 6b 65 6e 42 79 74 65 73 2c 0a 20 20 20 20  TokenBytes,.    
368c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
368ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
368cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
368cc 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 26 69  iStartOffset, &i
368cd 45 6e 64 4f 66 66 73 65 74 2c 0a 20 20 20 20 20  EndOffset,.     
368ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
368cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
368d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 69                &i
368d1 50 6f 73 69 74 69 6f 6e 29 29 20 29 7b 0a 20 20  Position)) ){.  
368d2 20 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70    DLCollector *p
368d3 3b 0a 20 20 20 20 69 6e 74 20 6e 44 61 74 61 3b  ;.    int nData;
368d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
368d5 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 64 6f     /* Size of do
368d6 63 6c 69 73 74 20 62 65 66 6f 72 65 20 6f 75 72  clist before our
368d7 20 75 70 64 61 74 65 2e 20 2a 2f 0a 0a 20 20 20   update. */..   
368d8 20 2f 2a 20 50 6f 73 69 74 69 6f 6e 73 20 63 61   /* Positions ca
368d9 6e 27 74 20 62 65 20 6e 65 67 61 74 69 76 65 3b  n't be negative;
368da 20 77 65 20 75 73 65 20 2d 31 20 61 73 20 61 20   we use -1 as a 
368db 74 65 72 6d 69 6e 61 74 6f 72 0a 20 20 20 20 20  terminator.     
368dc 2a 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 20 20 54  * internally.  T
368dd 6f 6b 65 6e 20 63 61 6e 27 74 20 62 65 20 4e 55  oken can't be NU
368de 4c 4c 20 6f 72 20 65 6d 70 74 79 2e 20 2a 2f 0a  LL or empty. */.
368df 20 20 20 20 69 66 28 20 69 50 6f 73 69 74 69 6f      if( iPositio
368e0 6e 3c 30 20 7c 7c 20 70 54 6f 6b 65 6e 20 3d 3d  n<0 || pToken ==
368e1 20 4e 55 4c 4c 20 7c 7c 20 6e 54 6f 6b 65 6e 42   NULL || nTokenB
368e2 79 74 65 73 20 3d 3d 20 30 20 29 7b 0a 20 20 20  ytes == 0 ){.   
368e3 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
368e4 52 52 4f 52 3b 0a 20 20 20 20 20 20 62 72 65 61  RROR;.      brea
368e5 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 20  k;.    }..    p 
368e6 3d 20 66 74 73 33 48 61 73 68 46 69 6e 64 28 26  = fts3HashFind(&
368e7 76 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 2c  v->pendingTerms,
368e8 20 70 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 42   pToken, nTokenB
368e9 79 74 65 73 29 3b 0a 20 20 20 20 69 66 28 20 70  ytes);.    if( p
368ea 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20  ==NULL ){.      
368eb 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20 20  nData = 0;.     
368ec 20 70 20 3d 20 64 6c 63 4e 65 77 28 69 44 6f 63   p = dlcNew(iDoc
368ed 69 64 2c 20 44 4c 5f 44 45 46 41 55 4c 54 29 3b  id, DL_DEFAULT);
368ee 0a 20 20 20 20 20 20 66 74 73 33 48 61 73 68 49  .      fts3HashI
368ef 6e 73 65 72 74 28 26 76 2d 3e 70 65 6e 64 69 6e  nsert(&v->pendin
368f0 67 54 65 72 6d 73 2c 20 70 54 6f 6b 65 6e 2c 20  gTerms, pToken, 
368f1 6e 54 6f 6b 65 6e 42 79 74 65 73 2c 20 70 29 3b  nTokenBytes, p);
368f2 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 76 65 72 68  ..      /* Overh
368f3 65 61 64 20 66 6f 72 20 6f 75 72 20 68 61 73 68  ead for our hash
368f4 20 74 61 62 6c 65 20 65 6e 74 72 79 2c 20 74 68   table entry, th
368f5 65 20 6b 65 79 2c 20 61 6e 64 20 74 68 65 20 76  e key, and the v
368f6 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 20 20 76  alue. */.      v
368f7 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 2b  ->nPendingData +
368f8 3d 20 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20  = sizeof(struct 
368f9 66 74 73 33 48 61 73 68 45 6c 65 6d 29 2b 73 69  fts3HashElem)+si
368fa 7a 65 6f 66 28 2a 70 29 2b 6e 54 6f 6b 65 6e 42  zeof(*p)+nTokenB
368fb 79 74 65 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ytes;.    }else{
368fc 0a 20 20 20 20 20 20 6e 44 61 74 61 20 3d 20 70  .      nData = p
368fd 2d 3e 62 2e 6e 44 61 74 61 3b 0a 20 20 20 20 20  ->b.nData;.     
368fe 20 69 66 28 20 70 2d 3e 64 6c 77 2e 69 50 72 65   if( p->dlw.iPre
368ff 76 44 6f 63 69 64 21 3d 69 44 6f 63 69 64 20 29  vDocid!=iDocid )
36900 20 64 6c 63 4e 65 78 74 28 70 2c 20 69 44 6f 63   dlcNext(p, iDoc
36901 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  id);.    }.    i
36902 66 28 20 69 43 6f 6c 75 6d 6e 3e 3d 30 20 29 7b  f( iColumn>=0 ){
36903 0a 20 20 20 20 20 20 64 6c 63 41 64 64 50 6f 73  .      dlcAddPos
36904 28 70 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 50 6f  (p, iColumn, iPo
36905 73 69 74 69 6f 6e 2c 20 69 53 74 61 72 74 4f 66  sition, iStartOf
36906 66 73 65 74 2c 20 69 45 6e 64 4f 66 66 73 65 74  fset, iEndOffset
36907 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
36908 20 41 63 63 75 6d 75 6c 61 74 65 20 64 61 74 61   Accumulate data
36909 20 61 64 64 65 64 20 62 79 20 64 6c 63 4e 65 77   added by dlcNew
3690a 20 6f 72 20 64 6c 63 4e 65 78 74 2c 20 61 6e 64   or dlcNext, and
3690b 20 64 6c 63 41 64 64 50 6f 73 2e 20 2a 2f 0a 20   dlcAddPos. */. 
3690c 20 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61     v->nPendingDa
3690d 74 61 20 2b 3d 20 70 2d 3e 62 2e 6e 44 61 74 61  ta += p->b.nData
3690e 2d 6e 44 61 74 61 3b 0a 20 20 7d 0a 0a 20 20 2f  -nData;.  }..  /
3690f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 43 68  * TODO(shess) Ch
36910 65 63 6b 20 72 65 74 75 72 6e 3f 20 20 53 68 6f  eck return?  Sho
36911 75 6c 64 20 74 68 69 73 20 62 65 20 61 62 6c 65  uld this be able
36912 20 74 6f 20 63 61 75 73 65 20 65 72 72 6f 72 73   to cause errors
36913 20 61 74 0a 20 20 2a 2a 20 74 68 69 73 20 70 6f   at.  ** this po
36914 69 6e 74 3f 20 20 41 63 74 75 61 6c 6c 79 2c 20  int?  Actually, 
36915 73 61 6d 65 20 71 75 65 73 74 69 6f 6e 20 61 62  same question ab
36916 6f 75 74 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  out sqlite3_fina
36917 6c 69 7a 65 28 29 2c 0a 20 20 2a 2a 20 74 68 6f  lize(),.  ** tho
36918 75 67 68 20 6f 6e 65 20 63 6f 75 6c 64 20 61 72  ugh one could ar
36919 67 75 65 20 74 68 61 74 20 66 61 69 6c 75 72 65  gue that failure
3691a 20 74 68 65 72 65 20 6d 65 61 6e 73 20 74 68 61   there means tha
3691b 74 20 74 68 65 20 64 61 74 61 20 69 73 0a 20 20  t the data is.  
3691c 2a 2a 20 6e 6f 74 20 64 75 72 61 62 6c 65 2e 20  ** not durable. 
3691d 20 2a 70 6f 6e 64 65 72 2a 0a 20 20 2a 2f 0a 20   *ponder*.  */. 
3691e 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f   pTokenizer->pMo
3691f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43 75  dule->xClose(pCu
36920 72 73 6f 72 29 3b 0a 20 20 69 66 28 20 53 51 4c  rsor);.  if( SQL
36921 49 54 45 5f 44 4f 4e 45 20 3d 3d 20 72 63 20 29  ITE_DONE == rc )
36922 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
36923 4b 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  K;.  return rc;.
36924 7d 0a 0a 2f 2a 20 41 64 64 20 64 6f 63 6c 69 73  }../* Add doclis
36925 74 73 20 66 6f 72 20 61 6c 6c 20 74 65 72 6d 73  ts for all terms
36926 20 69 6e 20 5b 70 56 61 6c 75 65 73 5d 20 74 6f   in [pValues] to
36927 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 74 61   pendingTerms ta
36928 62 6c 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  ble. */.static i
36929 6e 74 20 69 6e 73 65 72 74 54 65 72 6d 73 28 66  nt insertTerms(f
3692a 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
3692b 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44   sqlite_int64 iD
3692c 6f 63 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20  ocid,.          
3692d 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
3692e 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 56 61  ite3_value **pVa
3692f 6c 75 65 73 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  lues){.  int i;.
36930 20 20 66 6f 72 28 69 20 3d 20 30 3b 20 69 20 3c    for(i = 0; i <
36931 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 3b 20 2b 2b   v->nColumn ; ++
36932 69 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54  i){.    char *zT
36933 65 78 74 20 3d 20 28 63 68 61 72 2a 29 73 71 6c  ext = (char*)sql
36934 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
36935 70 56 61 6c 75 65 73 5b 69 5d 29 3b 0a 20 20 20  pValues[i]);.   
36936 20 69 6e 74 20 72 63 20 3d 20 62 75 69 6c 64 54   int rc = buildT
36937 65 72 6d 73 28 76 2c 20 69 44 6f 63 69 64 2c 20  erms(v, iDocid, 
36938 7a 54 65 78 74 2c 20 69 29 3b 0a 20 20 20 20 69  zText, i);.    i
36939 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3693a 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
3693b 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
3693c 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20  E_OK;.}../* Add 
3693d 65 6d 70 74 79 20 64 6f 63 6c 69 73 74 73 20 66  empty doclists f
3693e 6f 72 20 61 6c 6c 20 74 65 72 6d 73 20 69 6e 20  or all terms in 
3693f 74 68 65 20 67 69 76 65 6e 20 72 6f 77 27 73 20  the given row's 
36940 63 6f 6e 74 65 6e 74 20 74 6f 0a 2a 2a 20 70 65  content to.** pe
36941 6e 64 69 6e 67 54 65 72 6d 73 2e 0a 2a 2f 0a 73  ndingTerms..*/.s
36942 74 61 74 69 63 20 69 6e 74 20 64 65 6c 65 74 65  tatic int delete
36943 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76  Terms(fulltext_v
36944 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69  tab *v, sqlite_i
36945 6e 74 36 34 20 69 44 6f 63 69 64 29 7b 0a 20 20  nt64 iDocid){.  
36946 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 56 61  const char **pVa
36947 6c 75 65 73 3b 0a 20 20 69 6e 74 20 69 2c 20 72  lues;.  int i, r
36948 63 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68  c;..  /* TODO(sh
36949 65 73 73 29 20 53 68 6f 75 6c 64 20 77 65 20 61  ess) Should we a
3694a 6c 6c 6f 77 20 73 75 63 68 20 74 61 62 6c 65 73  llow such tables
3694b 20 61 74 20 61 6c 6c 3f 20 2a 2f 0a 20 20 69 66   at all? */.  if
3694c 28 20 44 4c 5f 44 45 46 41 55 4c 54 3d 3d 44 4c  ( DL_DEFAULT==DL
3694d 5f 44 4f 43 49 44 53 20 29 20 72 65 74 75 72 6e  _DOCIDS ) return
3694e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 0a   SQLITE_ERROR;..
3694f 20 20 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f 73    rc = content_s
36950 65 6c 65 63 74 28 76 2c 20 69 44 6f 63 69 64 2c  elect(v, iDocid,
36951 20 26 70 56 61 6c 75 65 73 29 3b 0a 20 20 69 66   &pValues);.  if
36952 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
36953 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
36954 66 6f 72 28 69 20 3d 20 30 20 3b 20 69 20 3c 20  for(i = 0 ; i < 
36955 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29  v->nColumn; ++i)
36956 20 7b 0a 20 20 20 20 72 63 20 3d 20 62 75 69 6c   {.    rc = buil
36957 64 54 65 72 6d 73 28 76 2c 20 69 44 6f 63 69 64  dTerms(v, iDocid
36958 2c 20 70 56 61 6c 75 65 73 5b 69 5d 2c 20 2d 31  , pValues[i], -1
36959 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
3695a 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b  QLITE_OK ) break
3695b 3b 0a 20 20 7d 0a 0a 20 20 66 72 65 65 53 74 72  ;.  }..  freeStr
3695c 69 6e 67 41 72 72 61 79 28 76 2d 3e 6e 43 6f 6c  ingArray(v->nCol
3695d 75 6d 6e 2c 20 70 56 61 6c 75 65 73 29 3b 0a 20  umn, pValues);. 
3695e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3695f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68  K;.}../* TODO(sh
36960 65 73 73 29 20 52 65 66 61 63 74 6f 72 20 74 68  ess) Refactor th
36961 65 20 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65  e code to remove
36962 20 74 68 69 73 20 66 6f 72 77 61 72 64 20 64 65   this forward de
36963 63 6c 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  cl. */.static in
36964 74 20 69 6e 69 74 50 65 6e 64 69 6e 67 54 65 72  t initPendingTer
36965 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ms(fulltext_vtab
36966 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36   *v, sqlite_int6
36967 34 20 69 44 6f 63 69 64 29 3b 0a 0a 2f 2a 20 49  4 iDocid);../* I
36968 6e 73 65 72 74 20 61 20 72 6f 77 20 69 6e 74 6f  nsert a row into
36969 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74   the %_content t
3696a 61 62 6c 65 3b 20 73 65 74 20 2a 70 69 44 6f 63  able; set *piDoc
3696b 69 64 20 74 6f 20 62 65 20 74 68 65 20 49 44 20  id to be the ID 
3696c 6f 66 20 74 68 65 0a 2a 2a 20 6e 65 77 20 72 6f  of the.** new ro
3696d 77 2e 20 20 41 64 64 20 64 6f 63 6c 69 73 74 73  w.  Add doclists
3696e 20 66 6f 72 20 74 65 72 6d 73 20 74 6f 20 70 65   for terms to pe
3696f 6e 64 69 6e 67 54 65 72 6d 73 2e 0a 2a 2f 0a 73  ndingTerms..*/.s
36970 74 61 74 69 63 20 69 6e 74 20 69 6e 64 65 78 5f  tatic int index_
36971 69 6e 73 65 72 74 28 66 75 6c 6c 74 65 78 74 5f  insert(fulltext_
36972 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 33  vtab *v, sqlite3
36973 5f 76 61 6c 75 65 20 2a 70 52 65 71 75 65 73 74  _value *pRequest
36974 44 6f 63 69 64 2c 0a 20 20 20 20 20 20 20 20 20  Docid,.         
36975 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
36976 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70  qlite3_value **p
36977 56 61 6c 75 65 73 2c 20 73 71 6c 69 74 65 5f 69  Values, sqlite_i
36978 6e 74 36 34 20 2a 70 69 44 6f 63 69 64 29 7b 0a  nt64 *piDocid){.
36979 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20    int rc;..  rc 
3697a 3d 20 63 6f 6e 74 65 6e 74 5f 69 6e 73 65 72 74  = content_insert
3697b 28 76 2c 20 70 52 65 71 75 65 73 74 44 6f 63 69  (v, pRequestDoci
3697c 64 2c 20 70 56 61 6c 75 65 73 29 3b 20 20 2f 2a  d, pValues);  /*
3697d 20 65 78 65 63 75 74 65 20 61 6e 20 53 51 4c 20   execute an SQL 
3697e 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69 66 28 20  INSERT */.  if( 
3697f 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
36980 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a  return rc;..  /*
36981 20 64 6f 63 69 64 20 63 6f 6c 75 6d 6e 20 69 73   docid column is
36982 20 61 6e 20 61 6c 69 61 73 20 66 6f 72 20 72 6f   an alias for ro
36983 77 69 64 2e 20 2a 2f 0a 20 20 2a 70 69 44 6f 63  wid. */.  *piDoc
36984 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73  id = sqlite3_las
36985 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 76  t_insert_rowid(v
36986 2d 3e 64 62 29 3b 0a 20 20 72 63 20 3d 20 69 6e  ->db);.  rc = in
36987 69 74 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76  itPendingTerms(v
36988 2c 20 2a 70 69 44 6f 63 69 64 29 3b 0a 20 20 69  , *piDocid);.  i
36989 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3698a 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
3698b 20 72 65 74 75 72 6e 20 69 6e 73 65 72 74 54 65   return insertTe
3698c 72 6d 73 28 76 2c 20 2a 70 69 44 6f 63 69 64 2c  rms(v, *piDocid,
3698d 20 70 56 61 6c 75 65 73 29 3b 0a 7d 0a 0a 2f 2a   pValues);.}../*
3698e 20 44 65 6c 65 74 65 20 61 20 72 6f 77 20 66 72   Delete a row fr
3698f 6f 6d 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74  om the %_content
36990 20 74 61 62 6c 65 3b 20 61 64 64 20 65 6d 70 74   table; add empt
36991 79 20 64 6f 63 6c 69 73 74 73 20 66 6f 72 20 74  y doclists for t
36992 65 72 6d 73 0a 2a 2a 20 74 6f 20 70 65 6e 64 69  erms.** to pendi
36993 6e 67 54 65 72 6d 73 2e 0a 2a 2f 0a 73 74 61 74  ngTerms..*/.stat
36994 69 63 20 69 6e 74 20 69 6e 64 65 78 5f 64 65 6c  ic int index_del
36995 65 74 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  ete(fulltext_vta
36996 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74  b *v, sqlite_int
36997 36 34 20 69 52 6f 77 29 7b 0a 20 20 69 6e 74 20  64 iRow){.  int 
36998 72 63 20 3d 20 69 6e 69 74 50 65 6e 64 69 6e 67  rc = initPending
36999 54 65 72 6d 73 28 76 2c 20 69 52 6f 77 29 3b 0a  Terms(v, iRow);.
3699a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
3699b 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
3699c 0a 0a 20 20 72 63 20 3d 20 64 65 6c 65 74 65 54  ..  rc = deleteT
3699d 65 72 6d 73 28 76 2c 20 69 52 6f 77 29 3b 0a 20  erms(v, iRow);. 
3699e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3699f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
369a0 0a 20 20 72 65 74 75 72 6e 20 63 6f 6e 74 65 6e  .  return conten
369a1 74 5f 64 65 6c 65 74 65 28 76 2c 20 69 52 6f 77  t_delete(v, iRow
369a2 29 3b 20 20 2f 2a 20 65 78 65 63 75 74 65 20 61  );  /* execute a
369a3 6e 20 53 51 4c 20 44 45 4c 45 54 45 20 2a 2f 0a  n SQL DELETE */.
369a4 7d 0a 0a 2f 2a 20 55 70 64 61 74 65 20 61 20 72  }../* Update a r
369a5 6f 77 20 69 6e 20 74 68 65 20 25 5f 63 6f 6e 74  ow in the %_cont
369a6 65 6e 74 20 74 61 62 6c 65 3b 20 61 64 64 20 64  ent table; add d
369a7 65 6c 65 74 65 20 64 6f 63 6c 69 73 74 73 20 74  elete doclists t
369a8 6f 0a 2a 2a 20 70 65 6e 64 69 6e 67 54 65 72 6d  o.** pendingTerm
369a9 73 20 66 6f 72 20 6f 6c 64 20 74 65 72 6d 73 20  s for old terms 
369aa 6e 6f 74 20 69 6e 20 74 68 65 20 6e 65 77 20 64  not in the new d
369ab 61 74 61 2c 20 61 64 64 20 69 6e 73 65 72 74 20  ata, add insert 
369ac 64 6f 63 6c 69 73 74 73 0a 2a 2a 20 74 6f 20 70  doclists.** to p
369ad 65 6e 64 69 6e 67 54 65 72 6d 73 20 66 6f 72 20  endingTerms for 
369ae 74 65 72 6d 73 20 69 6e 20 74 68 65 20 6e 65 77  terms in the new
369af 20 64 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 63   data..*/.static
369b0 20 69 6e 74 20 69 6e 64 65 78 5f 75 70 64 61 74   int index_updat
369b1 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  e(fulltext_vtab 
369b2 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *v, sqlite_int64
369b3 20 69 52 6f 77 2c 0a 20 20 20 20 20 20 20 20 20   iRow,.         
369b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
369b5 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70  qlite3_value **p
369b6 56 61 6c 75 65 73 29 7b 0a 20 20 69 6e 74 20 72  Values){.  int r
369b7 63 20 3d 20 69 6e 69 74 50 65 6e 64 69 6e 67 54  c = initPendingT
369b8 65 72 6d 73 28 76 2c 20 69 52 6f 77 29 3b 0a 20  erms(v, iRow);. 
369b9 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
369ba 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
369bb 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61  .  /* Generate a
369bc 6e 20 65 6d 70 74 79 20 64 6f 63 6c 69 73 74 20  n empty doclist 
369bd 66 6f 72 20 65 61 63 68 20 74 65 72 6d 20 74 68  for each term th
369be 61 74 20 70 72 65 76 69 6f 75 73 6c 79 20 61 70  at previously ap
369bf 70 65 61 72 65 64 20 69 6e 20 74 68 69 73 0a 20  peared in this. 
369c0 20 20 2a 20 72 6f 77 2e 20 2a 2f 0a 20 20 72 63    * row. */.  rc
369c1 20 3d 20 64 65 6c 65 74 65 54 65 72 6d 73 28 76   = deleteTerms(v
369c2 2c 20 69 52 6f 77 29 3b 0a 20 20 69 66 28 20 72  , iRow);.  if( r
369c3 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
369c4 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
369c5 3d 20 63 6f 6e 74 65 6e 74 5f 75 70 64 61 74 65  = content_update
369c6 28 76 2c 20 70 56 61 6c 75 65 73 2c 20 69 52 6f  (v, pValues, iRo
369c7 77 29 3b 20 20 2f 2a 20 65 78 65 63 75 74 65 20  w);  /* execute 
369c8 61 6e 20 53 51 4c 20 55 50 44 41 54 45 20 2a 2f  an SQL UPDATE */
369c9 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
369ca 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
369cb 3b 0a 0a 20 20 2f 2a 20 4e 6f 77 20 61 64 64 20  ;..  /* Now add 
369cc 70 6f 73 69 74 69 6f 6e 73 20 66 6f 72 20 74 65  positions for te
369cd 72 6d 73 20 77 68 69 63 68 20 61 70 70 65 61 72  rms which appear
369ce 20 69 6e 20 74 68 65 20 75 70 64 61 74 65 64 20   in the updated 
369cf 72 6f 77 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e  row. */.  return
369d0 20 69 6e 73 65 72 74 54 65 72 6d 73 28 76 2c 20   insertTerms(v, 
369d1 69 52 6f 77 2c 20 70 56 61 6c 75 65 73 29 3b 0a  iRow, pValues);.
369d2 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
369d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
369d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
369d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
369d6 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 49 6e 74 65  *******/./* Inte
369d7 72 69 6f 72 57 72 69 74 65 72 20 69 73 20 75 73  riorWriter is us
369d8 65 64 20 74 6f 20 63 6f 6c 6c 65 63 74 20 74 65  ed to collect te
369d9 72 6d 73 20 61 6e 64 20 62 6c 6f 63 6b 20 72 65  rms and block re
369da 66 65 72 65 6e 63 65 73 20 69 6e 74 6f 0a 2a 2a  ferences into.**
369db 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20   interior nodes 
369dc 69 6e 20 25 5f 73 65 67 6d 65 6e 74 73 2e 20 20  in %_segments.  
369dd 53 65 65 20 63 6f 6d 6d 65 6e 74 61 72 79 20 61  See commentary a
369de 74 20 74 6f 70 20 6f 66 20 66 69 6c 65 20 66 6f  t top of file fo
369df 72 0a 2a 2a 20 66 6f 72 6d 61 74 2e 0a 2a 2f 0a  r.** format..*/.
369e0 0a 2f 2a 20 48 6f 77 20 6c 61 72 67 65 20 69 6e  ./* How large in
369e1 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 63 61 6e  terior nodes can
369e2 20 67 72 6f 77 2e 20 2a 2f 0a 23 64 65 66 69 6e   grow. */.#defin
369e3 65 20 49 4e 54 45 52 49 4f 52 5f 4d 41 58 20 32  e INTERIOR_MAX 2
369e4 30 34 38 0a 0a 2f 2a 20 4d 69 6e 69 6d 75 6d 20  048../* Minimum 
369e5 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20  number of terms 
369e6 70 65 72 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  per interior nod
369e7 65 20 28 65 78 63 65 70 74 20 74 68 65 20 72 6f  e (except the ro
369e8 6f 74 29 2e 20 54 68 69 73 0a 2a 2a 20 70 72 65  ot). This.** pre
369e9 76 65 6e 74 73 20 6c 61 72 67 65 20 74 65 72 6d  vents large term
369ea 73 20 66 72 6f 6d 20 6d 61 6b 69 6e 67 20 74 68  s from making th
369eb 65 20 74 72 65 65 20 74 6f 6f 20 73 6b 69 6e 6e  e tree too skinn
369ec 79 20 2d 20 6d 75 73 74 20 62 65 20 3e 30 0a 2a  y - must be >0.*
369ed 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 74 72  * so that the tr
369ee 65 65 20 61 6c 77 61 79 73 20 6d 61 6b 65 73 20  ee always makes 
369ef 70 72 6f 67 72 65 73 73 2e 20 20 4e 6f 74 65 20  progress.  Note 
369f0 74 68 61 74 20 74 68 65 20 6d 69 6e 20 74 72 65  that the min tre
369f1 65 0a 2a 2a 20 66 61 6e 6f 75 74 20 77 69 6c 6c  e.** fanout will
369f2 20 62 65 20 49 4e 54 45 52 49 4f 52 5f 4d 49 4e   be INTERIOR_MIN
369f3 5f 54 45 52 4d 53 2b 31 2e 0a 2a 2f 0a 23 64 65  _TERMS+1..*/.#de
369f4 66 69 6e 65 20 49 4e 54 45 52 49 4f 52 5f 4d 49  fine INTERIOR_MI
369f5 4e 5f 54 45 52 4d 53 20 37 0a 23 69 66 20 49 4e  N_TERMS 7.#if IN
369f6 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53  TERIOR_MIN_TERMS
369f7 3c 31 0a 23 20 65 72 72 6f 72 20 49 4e 54 45 52  <1.# error INTER
369f8 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 6d 75  IOR_MIN_TERMS mu
369f9 73 74 20 62 65 20 67 72 65 61 74 65 72 20 74 68  st be greater th
369fa 61 6e 20 30 2e 0a 23 65 6e 64 69 66 0a 0a 2f 2a  an 0..#endif../*
369fb 20 52 4f 4f 54 5f 4d 41 58 20 63 6f 6e 74 72 6f   ROOT_MAX contro
369fc 6c 73 20 68 6f 77 20 6d 75 63 68 20 64 61 74 61  ls how much data
369fd 20 69 73 20 73 74 6f 72 65 64 20 69 6e 6c 69 6e   is stored inlin
369fe 65 20 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74  e in the segment
369ff 0a 2a 2a 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a  .** directory..*
36a00 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
36a01 20 50 75 73 68 20 52 4f 4f 54 5f 4d 41 58 20 64   Push ROOT_MAX d
36a02 6f 77 6e 20 74 6f 20 77 68 6f 65 76 65 72 20 69  own to whoever i
36a03 73 20 77 72 69 74 69 6e 67 20 74 68 69 6e 67 73  s writing things
36a04 2e 20 20 49 74 27 73 0a 2a 2a 20 6f 6e 6c 79 20  .  It's.** only 
36a05 68 65 72 65 20 73 6f 20 74 68 61 74 20 69 6e 74  here so that int
36a06 65 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74 49  eriorWriterRootI
36a07 6e 66 6f 28 29 20 61 6e 64 20 6c 65 61 66 57 72  nfo() and leafWr
36a08 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 29 0a 2a  iterRootInfo().*
36a09 2a 20 63 61 6e 20 62 6f 74 68 20 73 65 65 20 69  * can both see i
36a0a 74 2c 20 62 75 74 20 69 66 20 74 68 65 20 63 61  t, but if the ca
36a0b 6c 6c 65 72 20 70 61 73 73 65 64 20 69 74 20 69  ller passed it i
36a0c 6e 2c 20 77 65 20 77 6f 75 6c 64 6e 27 74 20 65  n, we wouldn't e
36a0d 76 65 6e 0a 2a 2a 20 6e 65 65 64 20 61 20 64 65  ven.** need a de
36a0e 66 69 6e 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  fine..*/.#define
36a0f 20 52 4f 4f 54 5f 4d 41 58 20 31 30 32 34 0a 23   ROOT_MAX 1024.#
36a10 69 66 20 52 4f 4f 54 5f 4d 41 58 3c 56 41 52 49  if ROOT_MAX<VARI
36a11 4e 54 5f 4d 41 58 2a 32 0a 23 20 65 72 72 6f 72  NT_MAX*2.# error
36a12 20 52 4f 4f 54 5f 4d 41 58 20 6d 75 73 74 20 68   ROOT_MAX must h
36a13 61 76 65 20 65 6e 6f 75 67 68 20 73 70 61 63 65  ave enough space
36a14 20 66 6f 72 20 61 20 68 65 61 64 65 72 2e 0a 23   for a header..#
36a15 65 6e 64 69 66 0a 0a 2f 2a 20 49 6e 74 65 72 69  endif../* Interi
36a16 6f 72 42 6c 6f 63 6b 20 73 74 6f 72 65 73 20 61  orBlock stores a
36a17 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 6f 66 20   linked-list of 
36a18 69 6e 74 65 72 69 6f 72 20 62 6c 6f 63 6b 73 20  interior blocks 
36a19 77 68 69 6c 65 20 61 20 6c 6f 77 65 72 0a 2a 2a  while a lower.**
36a1a 20 6c 61 79 65 72 20 69 73 20 62 65 69 6e 67 20   layer is being 
36a1b 63 6f 6e 73 74 72 75 63 74 65 64 2e 0a 2a 2f 0a  constructed..*/.
36a1c 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49  typedef struct I
36a1d 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 7b 0a 20  nteriorBlock {. 
36a1e 20 44 61 74 61 42 75 66 66 65 72 20 74 65 72 6d   DataBuffer term
36a1f 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  ;           /* L
36a20 65 66 74 6d 6f 73 74 20 74 65 72 6d 20 69 6e 20  eftmost term in 
36a21 62 6c 6f 63 6b 27 73 20 73 75 62 74 72 65 65 2e  block's subtree.
36a22 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72   */.  DataBuffer
36a23 20 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20   data;          
36a24 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 64 20   /* Accumulated 
36a25 64 61 74 61 20 66 6f 72 20 74 68 65 20 62 6c 6f  data for the blo
36a26 63 6b 2e 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ck. */.  struct 
36a27 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 6e  InteriorBlock *n
36a28 65 78 74 3b 0a 7d 20 49 6e 74 65 72 69 6f 72 42  ext;.} InteriorB
36a29 6c 6f 63 6b 3b 0a 0a 73 74 61 74 69 63 20 49 6e  lock;..static In
36a2a 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 69 6e 74  teriorBlock *int
36a2b 65 72 69 6f 72 42 6c 6f 63 6b 4e 65 77 28 69 6e  eriorBlockNew(in
36a2c 74 20 69 48 65 69 67 68 74 2c 20 73 71 6c 69 74  t iHeight, sqlit
36a2d 65 5f 69 6e 74 36 34 20 69 43 68 69 6c 64 42 6c  e_int64 iChildBl
36a2e 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ock,.           
36a2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36a30 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
36a31 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69  t char *pTerm, i
36a32 6e 74 20 6e 54 65 72 6d 29 7b 0a 20 20 49 6e 74  nt nTerm){.  Int
36a33 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 62 6c 6f 63  eriorBlock *bloc
36a34 6b 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  k = sqlite3_mall
36a35 6f 63 28 73 69 7a 65 6f 66 28 49 6e 74 65 72 69  oc(sizeof(Interi
36a36 6f 72 42 6c 6f 63 6b 29 29 3b 0a 20 20 63 68 61  orBlock));.  cha
36a37 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b 56  r c[VARINT_MAX+V
36a38 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e  ARINT_MAX];.  in
36a39 74 20 6e 3b 0a 0a 20 20 69 66 28 20 62 6c 6f 63  t n;..  if( bloc
36a3a 6b 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28  k ){.    memset(
36a3b 62 6c 6f 63 6b 2c 20 30 2c 20 73 69 7a 65 6f 66  block, 0, sizeof
36a3c 28 2a 62 6c 6f 63 6b 29 29 3b 0a 20 20 20 20 64  (*block));.    d
36a3d 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 62  ataBufferInit(&b
36a3e 6c 6f 63 6b 2d 3e 74 65 72 6d 2c 20 30 29 3b 0a  lock->term, 0);.
36a3f 20 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65      dataBufferRe
36a40 70 6c 61 63 65 28 26 62 6c 6f 63 6b 2d 3e 74 65  place(&block->te
36a41 72 6d 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d  rm, pTerm, nTerm
36a42 29 3b 0a 0a 20 20 20 20 6e 20 3d 20 66 74 73 33  );..    n = fts3
36a43 50 75 74 56 61 72 69 6e 74 28 63 2c 20 69 48 65  PutVarint(c, iHe
36a44 69 67 68 74 29 3b 0a 20 20 20 20 6e 20 2b 3d 20  ight);.    n += 
36a45 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b  fts3PutVarint(c+
36a46 6e 2c 20 69 43 68 69 6c 64 42 6c 6f 63 6b 29 3b  n, iChildBlock);
36a47 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 49  .    dataBufferI
36a48 6e 69 74 28 26 62 6c 6f 63 6b 2d 3e 64 61 74 61  nit(&block->data
36a49 2c 20 49 4e 54 45 52 49 4f 52 5f 4d 41 58 29 3b  , INTERIOR_MAX);
36a4a 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 52  .    dataBufferR
36a4b 65 70 6c 61 63 65 28 26 62 6c 6f 63 6b 2d 3e 64  eplace(&block->d
36a4c 61 74 61 2c 20 63 2c 20 6e 29 3b 0a 20 20 7d 0a  ata, c, n);.  }.
36a4d 20 20 72 65 74 75 72 6e 20 62 6c 6f 63 6b 3b 0a    return block;.
36a4e 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  }..#ifndef NDEBU
36a4f 47 0a 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74  G./* Verify that
36a50 20 74 68 65 20 64 61 74 61 20 69 73 20 72 65 61   the data is rea
36a51 64 61 62 6c 65 20 61 73 20 61 6e 20 69 6e 74 65  dable as an inte
36a52 72 69 6f 72 20 6e 6f 64 65 2e 20 2a 2f 0a 73 74  rior node. */.st
36a53 61 74 69 63 20 76 6f 69 64 20 69 6e 74 65 72 69  atic void interi
36a54 6f 72 42 6c 6f 63 6b 56 61 6c 69 64 61 74 65 28  orBlockValidate(
36a55 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 70  InteriorBlock *p
36a56 42 6c 6f 63 6b 29 7b 0a 20 20 63 6f 6e 73 74 20  Block){.  const 
36a57 63 68 61 72 20 2a 70 44 61 74 61 20 3d 20 70 42  char *pData = pB
36a58 6c 6f 63 6b 2d 3e 64 61 74 61 2e 70 44 61 74 61  lock->data.pData
36a59 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61 20 3d 20  ;.  int nData = 
36a5a 70 42 6c 6f 63 6b 2d 3e 64 61 74 61 2e 6e 44 61  pBlock->data.nDa
36a5b 74 61 3b 0a 20 20 69 6e 74 20 6e 2c 20 69 44 75  ta;.  int n, iDu
36a5c 6d 6d 79 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e  mmy;.  sqlite_in
36a5d 74 36 34 20 69 42 6c 6f 63 6b 69 64 3b 0a 0a 20  t64 iBlockid;.. 
36a5e 20 61 73 73 65 72 74 28 20 6e 44 61 74 61 3e 30   assert( nData>0
36a5f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44   );.  assert( pD
36a60 61 74 61 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ata!=0 );.  asse
36a61 72 74 28 20 70 44 61 74 61 2b 6e 44 61 74 61 3e  rt( pData+nData>
36a62 70 44 61 74 61 20 29 3b 0a 0a 20 20 2f 2a 20 4d  pData );..  /* M
36a63 75 73 74 20 6c 65 61 64 20 77 69 74 68 20 68 65  ust lead with he
36a64 69 67 68 74 20 6f 66 20 6e 6f 64 65 20 61 73 20  ight of node as 
36a65 61 20 76 61 72 69 6e 74 28 6e 29 2c 20 6e 3e 30  a varint(n), n>0
36a66 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65   */.  n = fts3Ge
36a67 74 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c  tVarint32(pData,
36a68 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73   &iDummy);.  ass
36a69 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73  ert( n>0 );.  as
36a6a 73 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29  sert( iDummy>0 )
36a6b 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3c 6e 44  ;.  assert( n<nD
36a6c 61 74 61 20 29 3b 0a 20 20 70 44 61 74 61 20 2b  ata );.  pData +
36a6d 3d 20 6e 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20  = n;.  nData -= 
36a6e 6e 3b 0a 0a 20 20 2f 2a 20 4d 75 73 74 20 63 6f  n;..  /* Must co
36a6f 6e 74 61 69 6e 20 69 42 6c 6f 63 6b 69 64 2e 20  ntain iBlockid. 
36a70 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65 74  */.  n = fts3Get
36a71 56 61 72 69 6e 74 28 70 44 61 74 61 2c 20 26 69  Varint(pData, &i
36a72 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 61 73 73 65  Blockid);.  asse
36a73 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73  rt( n>0 );.  ass
36a74 65 72 74 28 20 6e 3c 3d 6e 44 61 74 61 20 29 3b  ert( n<=nData );
36a75 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20  .  pData += n;. 
36a76 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a 0a 20 20   nData -= n;..  
36a77 2f 2a 20 5a 65 72 6f 20 6f 72 20 6d 6f 72 65 20  /* Zero or more 
36a78 74 65 72 6d 73 20 6f 66 20 70 6f 73 69 74 69 76  terms of positiv
36a79 65 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 69 66  e length */.  if
36a7a 28 20 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20 20  ( nData!=0 ){.  
36a7b 20 20 2f 2a 20 46 69 72 73 74 20 74 65 72 6d 20    /* First term 
36a7c 69 73 20 6e 6f 74 20 64 65 6c 74 61 2d 65 6e 63  is not delta-enc
36a7d 6f 64 65 64 2e 20 2a 2f 0a 20 20 20 20 6e 20 3d  oded. */.    n =
36a7e 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
36a7f 28 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29  (pData, &iDummy)
36a80 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e  ;.    assert( n>
36a81 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
36a82 20 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20   iDummy>0 );.   
36a83 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d   assert( n+iDumm
36a84 79 3e 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74  y>0);.    assert
36a85 28 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e 44 61 74  ( n+iDummy<=nDat
36a86 61 20 29 3b 0a 20 20 20 20 70 44 61 74 61 20 2b  a );.    pData +
36a87 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20 20 20  = n+iDummy;.    
36a88 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d  nData -= n+iDumm
36a89 79 3b 0a 0a 20 20 20 20 2f 2a 20 46 6f 6c 6c 6f  y;..    /* Follo
36a8a 77 69 6e 67 20 74 65 72 6d 73 20 64 65 6c 74 61  wing terms delta
36a8b 2d 65 6e 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 20  -encoded. */.   
36a8c 20 77 68 69 6c 65 28 20 6e 44 61 74 61 21 3d 30   while( nData!=0
36a8d 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4c 65 6e   ){.      /* Len
36a8e 67 74 68 20 6f 66 20 73 68 61 72 65 64 20 70 72  gth of shared pr
36a8f 65 66 69 78 2e 20 2a 2f 0a 20 20 20 20 20 20 6e  efix. */.      n
36a90 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
36a91 33 32 28 70 44 61 74 61 2c 20 26 69 44 75 6d 6d  32(pData, &iDumm
36a92 79 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  y);.      assert
36a93 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20 20 20 61  ( n>0 );.      a
36a94 73 73 65 72 74 28 20 69 44 75 6d 6d 79 3e 3d 30  ssert( iDummy>=0
36a95 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
36a96 28 20 6e 3c 6e 44 61 74 61 20 29 3b 0a 20 20 20  ( n<nData );.   
36a97 20 20 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20     pData += n;. 
36a98 20 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 3b       nData -= n;
36a99 0a 0a 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74  ..      /* Lengt
36a9a 68 20 61 6e 64 20 64 61 74 61 20 6f 66 20 64 69  h and data of di
36a9b 73 74 69 6e 63 74 20 73 75 66 66 69 78 2e 20 2a  stinct suffix. *
36a9c 2f 0a 20 20 20 20 20 20 6e 20 3d 20 66 74 73 33  /.      n = fts3
36a9d 47 65 74 56 61 72 69 6e 74 33 32 28 70 44 61 74  GetVarint32(pDat
36a9e 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20  a, &iDummy);.   
36a9f 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29     assert( n>0 )
36aa0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
36aa1 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20  iDummy>0 );.    
36aa2 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d    assert( n+iDum
36aa3 6d 79 3e 30 29 3b 0a 20 20 20 20 20 20 61 73 73  my>0);.      ass
36aa4 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e  ert( n+iDummy<=n
36aa5 44 61 74 61 20 29 3b 0a 20 20 20 20 20 20 70 44  Data );.      pD
36aa6 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b  ata += n+iDummy;
36aa7 0a 20 20 20 20 20 20 6e 44 61 74 61 20 2d 3d 20  .      nData -= 
36aa8 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20 20 20 7d 0a  n+iDummy;.    }.
36aa9 20 20 7d 0a 7d 0a 23 64 65 66 69 6e 65 20 41 53    }.}.#define AS
36aaa 53 45 52 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52  SERT_VALID_INTER
36aab 49 4f 52 5f 42 4c 4f 43 4b 28 78 29 20 69 6e 74  IOR_BLOCK(x) int
36aac 65 72 69 6f 72 42 6c 6f 63 6b 56 61 6c 69 64 61  eriorBlockValida
36aad 74 65 28 78 29 0a 23 65 6c 73 65 0a 23 64 65 66  te(x).#else.#def
36aae 69 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49 44  ine ASSERT_VALID
36aaf 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28  _INTERIOR_BLOCK(
36ab0 78 29 20 61 73 73 65 72 74 28 20 31 20 29 0a 23  x) assert( 1 ).#
36ab1 65 6e 64 69 66 0a 0a 74 79 70 65 64 65 66 20 73  endif..typedef s
36ab2 74 72 75 63 74 20 49 6e 74 65 72 69 6f 72 57 72  truct InteriorWr
36ab3 69 74 65 72 20 7b 0a 20 20 69 6e 74 20 69 48 65  iter {.  int iHe
36ab4 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20  ight;           
36ab5 20 20 20 20 20 20 20 20 2f 2a 20 66 72 6f 6d 20          /* from 
36ab6 30 20 61 74 20 6c 65 61 76 65 73 2e 20 2a 2f 0a  0 at leaves. */.
36ab7 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20    InteriorBlock 
36ab8 2a 66 69 72 73 74 2c 20 2a 6c 61 73 74 3b 0a 20  *first, *last;. 
36ab9 20 73 74 72 75 63 74 20 49 6e 74 65 72 69 6f 72   struct Interior
36aba 57 72 69 74 65 72 20 2a 70 61 72 65 6e 74 57 72  Writer *parentWr
36abb 69 74 65 72 3b 0a 0a 20 20 44 61 74 61 42 75 66  iter;..  DataBuf
36abc 66 65 72 20 74 65 72 6d 3b 20 20 20 20 20 20 20  fer term;       
36abd 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20          /* Last 
36abe 74 65 72 6d 20 77 72 69 74 74 65 6e 20 74 6f 20  term written to 
36abf 62 6c 6f 63 6b 20 22 6c 61 73 74 22 2e 20 2a 2f  block "last". */
36ac0 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
36ac1 69 4f 70 65 6e 69 6e 67 43 68 69 6c 64 42 6c 6f  iOpeningChildBlo
36ac2 63 6b 3b 20 2f 2a 20 46 69 72 73 74 20 63 68 69  ck; /* First chi
36ac3 6c 64 20 62 6c 6f 63 6b 20 69 6e 20 62 6c 6f 63  ld block in bloc
36ac4 6b 20 22 6c 61 73 74 22 2e 20 2a 2f 0a 23 69 66  k "last". */.#if
36ac5 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 73 71  ndef NDEBUG.  sq
36ac6 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c 61 73 74  lite_int64 iLast
36ac7 43 68 69 6c 64 42 6c 6f 63 6b 3b 20 20 2f 2a 20  ChildBlock;  /* 
36ac8 66 6f 72 20 63 6f 6e 73 69 73 74 65 6e 63 79 20  for consistency 
36ac9 63 68 65 63 6b 73 2e 20 2a 2f 0a 23 65 6e 64 69  checks. */.#endi
36aca 66 0a 7d 20 49 6e 74 65 72 69 6f 72 57 72 69 74  f.} InteriorWrit
36acb 65 72 3b 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69  er;../* Initiali
36acc 7a 65 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e  ze an interior n
36acd 6f 64 65 20 77 68 65 72 65 20 70 54 65 72 6d 5b  ode where pTerm[
36ace 6e 54 65 72 6d 5d 20 6d 61 72 6b 73 20 74 68 65  nTerm] marks the
36acf 20 6c 65 66 74 6d 6f 73 74 0a 2a 2a 20 74 65 72   leftmost.** ter
36ad0 6d 20 69 6e 20 74 68 65 20 74 72 65 65 2e 20 20  m in the tree.  
36ad1 69 43 68 69 6c 64 42 6c 6f 63 6b 20 69 73 20 74  iChildBlock is t
36ad2 68 65 20 6c 65 66 74 6d 6f 73 74 20 63 68 69 6c  he leftmost chil
36ad3 64 20 62 6c 6f 63 6b 20 61 74 20 74 68 65 0a 2a  d block at the.*
36ad4 2a 20 6e 65 78 74 20 6c 65 76 65 6c 20 64 6f 77  * next level dow
36ad5 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73  n the tree..*/.s
36ad6 74 61 74 69 63 20 76 6f 69 64 20 69 6e 74 65 72  tatic void inter
36ad7 69 6f 72 57 72 69 74 65 72 49 6e 69 74 28 69 6e  iorWriterInit(in
36ad8 74 20 69 48 65 69 67 68 74 2c 20 63 6f 6e 73 74  t iHeight, const
36ad9 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e   char *pTerm, in
36ada 74 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20 20  t nTerm,.       
36adb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36adc 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69          sqlite_i
36add 6e 74 36 34 20 69 43 68 69 6c 64 42 6c 6f 63 6b  nt64 iChildBlock
36ade 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
36adf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ae0 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20   InteriorWriter 
36ae1 2a 70 57 72 69 74 65 72 29 7b 0a 20 20 49 6e 74  *pWriter){.  Int
36ae2 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 62 6c 6f 63  eriorBlock *bloc
36ae3 6b 3b 0a 20 20 61 73 73 65 72 74 28 20 69 48 65  k;.  assert( iHe
36ae4 69 67 68 74 3e 30 20 29 3b 0a 20 20 43 4c 45 41  ight>0 );.  CLEA
36ae5 52 28 70 57 72 69 74 65 72 29 3b 0a 0a 20 20 70  R(pWriter);..  p
36ae6 57 72 69 74 65 72 2d 3e 69 48 65 69 67 68 74 20  Writer->iHeight 
36ae7 3d 20 69 48 65 69 67 68 74 3b 0a 20 20 70 57 72  = iHeight;.  pWr
36ae8 69 74 65 72 2d 3e 69 4f 70 65 6e 69 6e 67 43 68  iter->iOpeningCh
36ae9 69 6c 64 42 6c 6f 63 6b 20 3d 20 69 43 68 69 6c  ildBlock = iChil
36aea 64 42 6c 6f 63 6b 3b 0a 23 69 66 6e 64 65 66 20  dBlock;.#ifndef 
36aeb 4e 44 45 42 55 47 0a 20 20 70 57 72 69 74 65 72  NDEBUG.  pWriter
36aec 2d 3e 69 4c 61 73 74 43 68 69 6c 64 42 6c 6f 63  ->iLastChildBloc
36aed 6b 20 3d 20 69 43 68 69 6c 64 42 6c 6f 63 6b 3b  k = iChildBlock;
36aee 0a 23 65 6e 64 69 66 0a 20 20 62 6c 6f 63 6b 20  .#endif.  block 
36aef 3d 20 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 4e  = interiorBlockN
36af0 65 77 28 69 48 65 69 67 68 74 2c 20 69 43 68 69  ew(iHeight, iChi
36af1 6c 64 42 6c 6f 63 6b 2c 20 70 54 65 72 6d 2c 20  ldBlock, pTerm, 
36af2 6e 54 65 72 6d 29 3b 0a 20 20 70 57 72 69 74 65  nTerm);.  pWrite
36af3 72 2d 3e 6c 61 73 74 20 3d 20 70 57 72 69 74 65  r->last = pWrite
36af4 72 2d 3e 66 69 72 73 74 20 3d 20 62 6c 6f 63 6b  r->first = block
36af5 3b 0a 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44  ;.  ASSERT_VALID
36af6 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28  _INTERIOR_BLOCK(
36af7 70 57 72 69 74 65 72 2d 3e 6c 61 73 74 29 3b 0a  pWriter->last);.
36af8 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74    dataBufferInit
36af9 28 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2c  (&pWriter->term,
36afa 20 30 29 3b 0a 7d 0a 0a 2f 2a 20 41 70 70 65 6e   0);.}../* Appen
36afb 64 20 74 68 65 20 63 68 69 6c 64 20 6e 6f 64 65  d the child node
36afc 20 72 6f 6f 74 65 64 20 61 74 20 69 43 68 69 6c   rooted at iChil
36afd 64 42 6c 6f 63 6b 20 74 6f 20 74 68 65 20 69 6e  dBlock to the in
36afe 74 65 72 69 6f 72 20 6e 6f 64 65 2c 0a 2a 2a 20  terior node,.** 
36aff 77 69 74 68 20 70 54 65 72 6d 5b 6e 54 65 72 6d  with pTerm[nTerm
36b00 5d 20 61 73 20 74 68 65 20 6c 65 66 74 6d 6f 73  ] as the leftmos
36b01 74 20 74 65 72 6d 20 69 6e 20 69 43 68 69 6c 64  t term in iChild
36b02 42 6c 6f 63 6b 27 73 20 73 75 62 74 72 65 65 2e  Block's subtree.
36b03 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
36b04 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 41 70  interiorWriterAp
36b05 70 65 6e 64 28 49 6e 74 65 72 69 6f 72 57 72 69  pend(InteriorWri
36b06 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20  ter *pWriter,.  
36b07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
36b09 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d  onst char *pTerm
36b0a 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20  , int nTerm,.   
36b0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
36b0d 6c 69 74 65 5f 69 6e 74 36 34 20 69 43 68 69 6c  lite_int64 iChil
36b0e 64 42 6c 6f 63 6b 29 7b 0a 20 20 63 68 61 72 20  dBlock){.  char 
36b0f 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b 56 41 52  c[VARINT_MAX+VAR
36b10 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20  INT_MAX];.  int 
36b11 6e 2c 20 6e 50 72 65 66 69 78 20 3d 20 30 3b 0a  n, nPrefix = 0;.
36b12 0a 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  .  ASSERT_VALID_
36b13 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 70  INTERIOR_BLOCK(p
36b14 57 72 69 74 65 72 2d 3e 6c 61 73 74 29 3b 0a 0a  Writer->last);..
36b15 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74    /* The first t
36b16 65 72 6d 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  erm written into
36b17 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64   an interior nod
36b18 65 20 69 73 20 61 63 74 75 61 6c 6c 79 0a 20 20  e is actually.  
36b19 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ** associated wi
36b1a 74 68 20 74 68 65 20 73 65 63 6f 6e 64 20 63 68  th the second ch
36b1b 69 6c 64 20 61 64 64 65 64 20 28 74 68 65 20 66  ild added (the f
36b1c 69 72 73 74 20 63 68 69 6c 64 20 77 61 73 20 61  irst child was a
36b1d 64 64 65 64 0a 20 20 2a 2a 20 69 6e 20 69 6e 74  dded.  ** in int
36b1e 65 72 69 6f 72 57 72 69 74 65 72 49 6e 69 74 2c  eriorWriterInit,
36b1f 20 6f 72 20 69 6e 20 74 68 65 20 69 66 20 63 6c   or in the if cl
36b20 61 75 73 65 20 61 74 20 74 68 65 20 62 6f 74 74  ause at the bott
36b21 6f 6d 20 6f 66 20 74 68 69 73 0a 20 20 2a 2a 20  om of this.  ** 
36b22 66 75 6e 63 74 69 6f 6e 29 2e 20 20 54 68 61 74  function).  That
36b23 20 74 65 72 6d 20 67 65 74 73 20 65 6e 63 6f 64   term gets encod
36b24 65 64 20 73 74 72 61 69 67 68 74 20 75 70 2c 20  ed straight up, 
36b25 77 69 74 68 20 6e 50 72 65 66 69 78 20 6c 65 66  with nPrefix lef
36b26 74 0a 20 20 2a 2a 20 61 74 20 30 2e 0a 20 20 2a  t.  ** at 0..  *
36b27 2f 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d  /.  if( pWriter-
36b28 3e 74 65 72 6d 2e 6e 44 61 74 61 3d 3d 30 20 29  >term.nData==0 )
36b29 7b 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 50 75  {.    n = fts3Pu
36b2a 74 56 61 72 69 6e 74 28 63 2c 20 6e 54 65 72 6d  tVarint(c, nTerm
36b2b 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
36b2c 77 68 69 6c 65 28 20 6e 50 72 65 66 69 78 3c 70  while( nPrefix<p
36b2d 57 72 69 74 65 72 2d 3e 74 65 72 6d 2e 6e 44 61  Writer->term.nDa
36b2e 74 61 20 26 26 0a 20 20 20 20 20 20 20 20 20 20  ta &&.          
36b2f 20 70 54 65 72 6d 5b 6e 50 72 65 66 69 78 5d 3d   pTerm[nPrefix]=
36b30 3d 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2e 70  =pWriter->term.p
36b31 44 61 74 61 5b 6e 50 72 65 66 69 78 5d 20 29 7b  Data[nPrefix] ){
36b32 0a 20 20 20 20 20 20 6e 50 72 65 66 69 78 2b 2b  .      nPrefix++
36b33 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 20 3d  ;.    }..    n =
36b34 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
36b35 2c 20 6e 50 72 65 66 69 78 29 3b 0a 20 20 20 20  , nPrefix);.    
36b36 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69  n += fts3PutVari
36b37 6e 74 28 63 2b 6e 2c 20 6e 54 65 72 6d 2d 6e 50  nt(c+n, nTerm-nP
36b38 72 65 66 69 78 29 3b 0a 20 20 7d 0a 0a 23 69 66  refix);.  }..#if
36b39 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 70 57  ndef NDEBUG.  pW
36b3a 72 69 74 65 72 2d 3e 69 4c 61 73 74 43 68 69 6c  riter->iLastChil
36b3b 64 42 6c 6f 63 6b 2b 2b 3b 0a 23 65 6e 64 69 66  dBlock++;.#endif
36b3c 0a 20 20 61 73 73 65 72 74 28 20 70 57 72 69 74  .  assert( pWrit
36b3d 65 72 2d 3e 69 4c 61 73 74 43 68 69 6c 64 42 6c  er->iLastChildBl
36b3e 6f 63 6b 3d 3d 69 43 68 69 6c 64 42 6c 6f 63 6b  ock==iChildBlock
36b3f 20 29 3b 0a 0a 20 20 2f 2a 20 4f 76 65 72 66 6c   );..  /* Overfl
36b40 6f 77 20 74 6f 20 61 20 6e 65 77 20 62 6c 6f 63  ow to a new bloc
36b41 6b 20 69 66 20 74 68 65 20 6e 65 77 20 74 65 72  k if the new ter
36b42 6d 20 6d 61 6b 65 73 20 74 68 65 20 63 75 72 72  m makes the curr
36b43 65 6e 74 20 62 6c 6f 63 6b 0a 20 20 2a 2a 20 74  ent block.  ** t
36b44 6f 6f 20 62 69 67 2c 20 61 6e 64 20 74 68 65 20  oo big, and the 
36b45 63 75 72 72 65 6e 74 20 62 6c 6f 63 6b 20 61 6c  current block al
36b46 72 65 61 64 79 20 68 61 73 20 65 6e 6f 75 67 68  ready has enough
36b47 20 74 65 72 6d 73 2e 0a 20 20 2a 2f 0a 20 20 69   terms..  */.  i
36b48 66 28 20 70 57 72 69 74 65 72 2d 3e 6c 61 73 74  f( pWriter->last
36b49 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2b 6e 2b 6e  ->data.nData+n+n
36b4a 54 65 72 6d 2d 6e 50 72 65 66 69 78 3e 49 4e 54  Term-nPrefix>INT
36b4b 45 52 49 4f 52 5f 4d 41 58 20 26 26 0a 20 20 20  ERIOR_MAX &&.   
36b4c 20 20 20 69 43 68 69 6c 64 42 6c 6f 63 6b 2d 70     iChildBlock-p
36b4d 57 72 69 74 65 72 2d 3e 69 4f 70 65 6e 69 6e 67  Writer->iOpening
36b4e 43 68 69 6c 64 42 6c 6f 63 6b 3e 49 4e 54 45 52  ChildBlock>INTER
36b4f 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 29 7b  IOR_MIN_TERMS ){
36b50 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 6c 61  .    pWriter->la
36b51 73 74 2d 3e 6e 65 78 74 20 3d 20 69 6e 74 65 72  st->next = inter
36b52 69 6f 72 42 6c 6f 63 6b 4e 65 77 28 70 57 72 69  iorBlockNew(pWri
36b53 74 65 72 2d 3e 69 48 65 69 67 68 74 2c 20 69 43  ter->iHeight, iC
36b54 68 69 6c 64 42 6c 6f 63 6b 2c 0a 20 20 20 20 20  hildBlock,.     
36b55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b57 20 20 20 20 20 20 70 54 65 72 6d 2c 20 6e 54 65        pTerm, nTe
36b58 72 6d 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72  rm);.    pWriter
36b59 2d 3e 6c 61 73 74 20 3d 20 70 57 72 69 74 65 72  ->last = pWriter
36b5a 2d 3e 6c 61 73 74 2d 3e 6e 65 78 74 3b 0a 20 20  ->last->next;.  
36b5b 20 20 70 57 72 69 74 65 72 2d 3e 69 4f 70 65 6e    pWriter->iOpen
36b5c 69 6e 67 43 68 69 6c 64 42 6c 6f 63 6b 20 3d 20  ingChildBlock = 
36b5d 69 43 68 69 6c 64 42 6c 6f 63 6b 3b 0a 20 20 20  iChildBlock;.   
36b5e 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74   dataBufferReset
36b5f 28 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 29  (&pWriter->term)
36b60 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  ;.  }else{.    d
36b61 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32  ataBufferAppend2
36b62 28 26 70 57 72 69 74 65 72 2d 3e 6c 61 73 74 2d  (&pWriter->last-
36b63 3e 64 61 74 61 2c 20 63 2c 20 6e 2c 0a 20 20 20  >data, c, n,.   
36b64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b65 20 20 20 70 54 65 72 6d 2b 6e 50 72 65 66 69 78     pTerm+nPrefix
36b66 2c 20 6e 54 65 72 6d 2d 6e 50 72 65 66 69 78 29  , nTerm-nPrefix)
36b67 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  ;.    dataBuffer
36b68 52 65 70 6c 61 63 65 28 26 70 57 72 69 74 65 72  Replace(&pWriter
36b69 2d 3e 74 65 72 6d 2c 20 70 54 65 72 6d 2c 20 6e  ->term, pTerm, n
36b6a 54 65 72 6d 29 3b 0a 20 20 7d 0a 20 20 41 53 53  Term);.  }.  ASS
36b6b 45 52 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49  ERT_VALID_INTERI
36b6c 4f 52 5f 42 4c 4f 43 4b 28 70 57 72 69 74 65 72  OR_BLOCK(pWriter
36b6d 2d 3e 6c 61 73 74 29 3b 0a 7d 0a 0a 2f 2a 20 46  ->last);.}../* F
36b6e 72 65 65 20 74 68 65 20 73 70 61 63 65 20 75 73  ree the space us
36b6f 65 64 20 62 79 20 70 57 72 69 74 65 72 2c 20 69  ed by pWriter, i
36b70 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 6c 69 6e  ncluding the lin
36b71 6b 65 64 2d 6c 69 73 74 20 6f 66 0a 2a 2a 20 49  ked-list of.** I
36b72 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 73 2c 20 61  nteriorBlocks, a
36b73 6e 64 20 70 61 72 65 6e 74 57 72 69 74 65 72 2c  nd parentWriter,
36b74 20 69 66 20 70 72 65 73 65 6e 74 2e 0a 2a 2f 0a   if present..*/.
36b75 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72  static int inter
36b76 69 6f 72 57 72 69 74 65 72 44 65 73 74 72 6f 79  iorWriterDestroy
36b77 28 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20  (InteriorWriter 
36b78 2a 70 57 72 69 74 65 72 29 7b 0a 20 20 49 6e 74  *pWriter){.  Int
36b79 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 62 6c 6f 63  eriorBlock *bloc
36b7a 6b 20 3d 20 70 57 72 69 74 65 72 2d 3e 66 69 72  k = pWriter->fir
36b7b 73 74 3b 0a 0a 20 20 77 68 69 6c 65 28 20 62 6c  st;..  while( bl
36b7c 6f 63 6b 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20  ock!=NULL ){.   
36b7d 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a   InteriorBlock *
36b7e 62 20 3d 20 62 6c 6f 63 6b 3b 0a 20 20 20 20 62  b = block;.    b
36b7f 6c 6f 63 6b 20 3d 20 62 6c 6f 63 6b 2d 3e 6e 65  lock = block->ne
36b80 78 74 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66  xt;.    dataBuff
36b81 65 72 44 65 73 74 72 6f 79 28 26 62 2d 3e 74 65  erDestroy(&b->te
36b82 72 6d 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66  rm);.    dataBuf
36b83 66 65 72 44 65 73 74 72 6f 79 28 26 62 2d 3e 64  ferDestroy(&b->d
36b84 61 74 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ata);.    sqlite
36b85 33 5f 66 72 65 65 28 62 29 3b 0a 20 20 7d 0a 20  3_free(b);.  }. 
36b86 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e 70 61   if( pWriter->pa
36b87 72 65 6e 74 57 72 69 74 65 72 21 3d 4e 55 4c 4c  rentWriter!=NULL
36b88 20 29 7b 0a 20 20 20 20 69 6e 74 65 72 69 6f 72   ){.    interior
36b89 57 72 69 74 65 72 44 65 73 74 72 6f 79 28 70 57  WriterDestroy(pW
36b8a 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69  riter->parentWri
36b8b 74 65 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ter);.    sqlite
36b8c 33 5f 66 72 65 65 28 70 57 72 69 74 65 72 2d 3e  3_free(pWriter->
36b8d 70 61 72 65 6e 74 57 72 69 74 65 72 29 3b 0a 20  parentWriter);. 
36b8e 20 7d 0a 20 20 64 61 74 61 42 75 66 66 65 72 44   }.  dataBufferD
36b8f 65 73 74 72 6f 79 28 26 70 57 72 69 74 65 72 2d  estroy(&pWriter-
36b90 3e 74 65 72 6d 29 3b 0a 20 20 53 43 52 41 4d 42  >term);.  SCRAMB
36b91 4c 45 28 70 57 72 69 74 65 72 29 3b 0a 20 20 72  LE(pWriter);.  r
36b92 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
36b93 0a 7d 0a 0a 2f 2a 20 49 66 20 70 57 72 69 74 65  .}../* If pWrite
36b94 72 20 63 61 6e 20 66 69 74 20 65 6e 74 69 72 65  r can fit entire
36b95 6c 79 20 69 6e 20 52 4f 4f 54 5f 4d 41 58 2c 20  ly in ROOT_MAX, 
36b96 72 65 74 75 72 6e 20 69 74 20 61 73 20 74 68 65  return it as the
36b97 20 72 6f 6f 74 20 69 6e 66 6f 0a 2a 2a 20 64 69   root info.** di
36b98 72 65 63 74 6c 79 2c 20 6c 65 61 76 69 6e 67 20  rectly, leaving 
36b99 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20 75 6e  *piEndBlockid un
36b9a 63 68 61 6e 67 65 64 2e 20 20 4f 74 68 65 72 77  changed.  Otherw
36b9b 69 73 65 2c 20 66 6c 75 73 68 0a 2a 2a 20 70 57  ise, flush.** pW
36b9c 72 69 74 65 72 20 74 6f 20 25 5f 73 65 67 6d 65  riter to %_segme
36b9d 6e 74 73 2c 20 62 75 69 6c 64 69 6e 67 20 61 20  nts, building a 
36b9e 6e 65 77 20 6c 61 79 65 72 20 6f 66 20 69 6e 74  new layer of int
36b9f 65 72 69 6f 72 20 6e 6f 64 65 73 2c 20 61 6e 64  erior nodes, and
36ba0 0a 2a 2a 20 72 65 63 75 72 73 69 76 65 6c 79 20  .** recursively 
36ba1 61 73 6b 20 66 6f 72 20 74 68 65 69 72 20 72 6f  ask for their ro
36ba2 6f 74 20 69 6e 74 6f 2e 0a 2a 2f 0a 73 74 61 74  ot into..*/.stat
36ba3 69 63 20 69 6e 74 20 69 6e 74 65 72 69 6f 72 57  ic int interiorW
36ba4 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 66 75  riterRootInfo(fu
36ba5 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
36ba6 49 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20 2a  InteriorWriter *
36ba7 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20  pWriter,.       
36ba8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ba9 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20             char 
36baa 2a 2a 70 70 52 6f 6f 74 49 6e 66 6f 2c 20 69 6e  **ppRootInfo, in
36bab 74 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f 2c 0a 20  t *pnRootInfo,. 
36bac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36bad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36bae 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
36baf 69 45 6e 64 42 6c 6f 63 6b 69 64 29 7b 0a 20 20  iEndBlockid){.  
36bb0 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 62  InteriorBlock *b
36bb1 6c 6f 63 6b 20 3d 20 70 57 72 69 74 65 72 2d 3e  lock = pWriter->
36bb2 66 69 72 73 74 3b 0a 20 20 73 71 6c 69 74 65 5f  first;.  sqlite_
36bb3 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69 64 20 3d  int64 iBlockid =
36bb4 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20   0;.  int rc;.. 
36bb5 20 2f 2a 20 49 66 20 77 65 20 63 61 6e 20 66 69   /* If we can fi
36bb6 74 20 74 68 65 20 73 65 67 6d 65 6e 74 20 69 6e  t the segment in
36bb7 6c 69 6e 65 20 2a 2f 0a 20 20 69 66 28 20 62 6c  line */.  if( bl
36bb8 6f 63 6b 3d 3d 70 57 72 69 74 65 72 2d 3e 6c 61  ock==pWriter->la
36bb9 73 74 20 26 26 20 62 6c 6f 63 6b 2d 3e 64 61 74  st && block->dat
36bba 61 2e 6e 44 61 74 61 3c 52 4f 4f 54 5f 4d 41 58  a.nData<ROOT_MAX
36bbb 20 29 7b 0a 20 20 20 20 2a 70 70 52 6f 6f 74 49   ){.    *ppRootI
36bbc 6e 66 6f 20 3d 20 62 6c 6f 63 6b 2d 3e 64 61 74  nfo = block->dat
36bbd 61 2e 70 44 61 74 61 3b 0a 20 20 20 20 2a 70 6e  a.pData;.    *pn
36bbe 52 6f 6f 74 49 6e 66 6f 20 3d 20 62 6c 6f 63 6b  RootInfo = block
36bbf 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3b 0a 20 20  ->data.nData;.  
36bc0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
36bc1 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6c  OK;.  }..  /* Fl
36bc2 75 73 68 20 74 68 65 20 66 69 72 73 74 20 62 6c  ush the first bl
36bc3 6f 63 6b 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74  ock to %_segment
36bc4 73 2c 20 61 6e 64 20 63 72 65 61 74 65 20 61 20  s, and create a 
36bc5 6e 65 77 20 6c 65 76 65 6c 20 6f 66 0a 20 20 2a  new level of.  *
36bc6 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e  * interior node.
36bc7 0a 20 20 2a 2f 0a 20 20 41 53 53 45 52 54 5f 56  .  */.  ASSERT_V
36bc8 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c  ALID_INTERIOR_BL
36bc9 4f 43 4b 28 62 6c 6f 63 6b 29 3b 0a 20 20 72 63  OCK(block);.  rc
36bca 20 3d 20 62 6c 6f 63 6b 5f 69 6e 73 65 72 74 28   = block_insert(
36bcb 76 2c 20 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e 70  v, block->data.p
36bcc 44 61 74 61 2c 20 62 6c 6f 63 6b 2d 3e 64 61 74  Data, block->dat
36bcd 61 2e 6e 44 61 74 61 2c 20 26 69 42 6c 6f 63 6b  a.nData, &iBlock
36bce 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  id);.  if( rc!=S
36bcf 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
36bd0 6e 20 72 63 3b 0a 20 20 2a 70 69 45 6e 64 42 6c  n rc;.  *piEndBl
36bd1 6f 63 6b 69 64 20 3d 20 69 42 6c 6f 63 6b 69 64  ockid = iBlockid
36bd2 3b 0a 0a 20 20 70 57 72 69 74 65 72 2d 3e 70 61  ;..  pWriter->pa
36bd3 72 65 6e 74 57 72 69 74 65 72 20 3d 20 73 71 6c  rentWriter = sql
36bd4 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
36bd5 6f 66 28 2a 70 57 72 69 74 65 72 2d 3e 70 61 72  of(*pWriter->par
36bd6 65 6e 74 57 72 69 74 65 72 29 29 3b 0a 20 20 69  entWriter));.  i
36bd7 6e 74 65 72 69 6f 72 57 72 69 74 65 72 49 6e 69  nteriorWriterIni
36bd8 74 28 70 57 72 69 74 65 72 2d 3e 69 48 65 69 67  t(pWriter->iHeig
36bd9 68 74 2b 31 2c 0a 20 20 20 20 20 20 20 20 20 20  ht+1,.          
36bda 20 20 20 20 20 20 20 20 20 20 20 62 6c 6f 63 6b             block
36bdb 2d 3e 74 65 72 6d 2e 70 44 61 74 61 2c 20 62 6c  ->term.pData, bl
36bdc 6f 63 6b 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 2c  ock->term.nData,
36bdd 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
36bde 20 20 20 20 20 20 69 42 6c 6f 63 6b 69 64 2c 20        iBlockid, 
36bdf 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57  pWriter->parentW
36be0 72 69 74 65 72 29 3b 0a 0a 20 20 2f 2a 20 46 6c  riter);..  /* Fl
36be1 75 73 68 20 61 64 64 69 74 69 6f 6e 61 6c 20 62  ush additional b
36be2 6c 6f 63 6b 73 20 61 6e 64 20 61 70 70 65 6e 64  locks and append
36be3 20 74 6f 20 74 68 65 20 68 69 67 68 65 72 20 69   to the higher i
36be4 6e 74 65 72 69 6f 72 0a 20 20 2a 2a 20 6e 6f 64  nterior.  ** nod
36be5 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 62 6c  e..  */.  for(bl
36be6 6f 63 6b 3d 62 6c 6f 63 6b 2d 3e 6e 65 78 74 3b  ock=block->next;
36be7 20 62 6c 6f 63 6b 21 3d 4e 55 4c 4c 3b 20 62 6c   block!=NULL; bl
36be8 6f 63 6b 3d 62 6c 6f 63 6b 2d 3e 6e 65 78 74 29  ock=block->next)
36be9 7b 0a 20 20 20 20 41 53 53 45 52 54 5f 56 41 4c  {.    ASSERT_VAL
36bea 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43  ID_INTERIOR_BLOC
36beb 4b 28 62 6c 6f 63 6b 29 3b 0a 20 20 20 20 72 63  K(block);.    rc
36bec 20 3d 20 62 6c 6f 63 6b 5f 69 6e 73 65 72 74 28   = block_insert(
36bed 76 2c 20 62 6c 6f 63 6b 2d 3e 64 61 74 61 2e 70  v, block->data.p
36bee 44 61 74 61 2c 20 62 6c 6f 63 6b 2d 3e 64 61 74  Data, block->dat
36bef 61 2e 6e 44 61 74 61 2c 20 26 69 42 6c 6f 63 6b  a.nData, &iBlock
36bf0 69 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  id);.    if( rc!
36bf1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
36bf2 75 72 6e 20 72 63 3b 0a 20 20 20 20 2a 70 69 45  urn rc;.    *piE
36bf3 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 69 42 6c 6f  ndBlockid = iBlo
36bf4 63 6b 69 64 3b 0a 0a 20 20 20 20 69 6e 74 65 72  ckid;..    inter
36bf5 69 6f 72 57 72 69 74 65 72 41 70 70 65 6e 64 28  iorWriterAppend(
36bf6 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57  pWriter->parentW
36bf7 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20  riter,.         
36bf8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36bf9 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 70 44 61 74  block->term.pDat
36bfa 61 2c 20 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 6e  a, block->term.n
36bfb 44 61 74 61 2c 20 69 42 6c 6f 63 6b 69 64 29 3b  Data, iBlockid);
36bfc 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50 61 72 65 6e  .  }..  /* Paren
36bfd 74 20 6e 6f 64 65 20 67 65 74 73 20 74 68 65 20  t node gets the 
36bfe 63 68 61 6e 63 65 20 74 6f 20 62 65 20 74 68 65  chance to be the
36bff 20 72 6f 6f 74 2e 20 2a 2f 0a 20 20 72 65 74 75   root. */.  retu
36c00 72 6e 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65  rn interiorWrite
36c01 72 52 6f 6f 74 49 6e 66 6f 28 76 2c 20 70 57 72  rRootInfo(v, pWr
36c02 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72 69 74  iter->parentWrit
36c03 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
36c04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36c05 20 20 20 20 70 70 52 6f 6f 74 49 6e 66 6f 2c 20      ppRootInfo, 
36c06 70 6e 52 6f 6f 74 49 6e 66 6f 2c 20 70 69 45 6e  pnRootInfo, piEn
36c07 64 42 6c 6f 63 6b 69 64 29 3b 0a 7d 0a 0a 2f 2a  dBlockid);.}../*
36c08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36c09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36c0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36c0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
36c0c 0a 2f 2a 20 49 6e 74 65 72 69 6f 72 52 65 61 64  ./* InteriorRead
36c0d 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  er is used to re
36c0e 61 64 20 6f 66 66 20 74 68 65 20 64 61 74 61 20  ad off the data 
36c0f 66 72 6f 6d 20 61 6e 20 69 6e 74 65 72 69 6f 72  from an interior
36c10 20 6e 6f 64 65 0a 2a 2a 20 28 73 65 65 20 63 6f   node.** (see co
36c11 6d 6d 65 6e 74 20 61 74 20 74 6f 70 20 6f 66 20  mment at top of 
36c12 66 69 6c 65 20 66 6f 72 20 74 68 65 20 66 6f 72  file for the for
36c13 6d 61 74 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  mat)..*/.typedef
36c14 20 73 74 72 75 63 74 20 49 6e 74 65 72 69 6f 72   struct Interior
36c15 52 65 61 64 65 72 20 7b 0a 20 20 63 6f 6e 73 74  Reader {.  const
36c16 20 63 68 61 72 20 2a 70 44 61 74 61 3b 0a 20 20   char *pData;.  
36c17 69 6e 74 20 6e 44 61 74 61 3b 0a 0a 20 20 44 61  int nData;..  Da
36c18 74 61 42 75 66 66 65 72 20 74 65 72 6d 3b 20 20  taBuffer term;  
36c19 20 20 20 20 20 20 20 20 2f 2a 20 70 72 65 76 69          /* previ
36c1a 6f 75 73 20 74 65 72 6d 2c 20 66 6f 72 20 64 65  ous term, for de
36c1b 63 6f 64 69 6e 67 20 74 65 72 6d 20 64 65 6c 74  coding term delt
36c1c 61 2e 20 2a 2f 0a 0a 20 20 73 71 6c 69 74 65 5f  a. */..  sqlite_
36c1d 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69 64 3b 0a  int64 iBlockid;.
36c1e 7d 20 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72  } InteriorReader
36c1f 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69  ;..static void i
36c20 6e 74 65 72 69 6f 72 52 65 61 64 65 72 44 65 73  nteriorReaderDes
36c21 74 72 6f 79 28 49 6e 74 65 72 69 6f 72 52 65 61  troy(InteriorRea
36c22 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
36c23 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72   dataBufferDestr
36c24 6f 79 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72  oy(&pReader->ter
36c25 6d 29 3b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70  m);.  SCRAMBLE(p
36c26 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 54  Reader);.}../* T
36c27 4f 44 4f 28 73 68 65 73 73 29 20 54 68 65 20 61  ODO(shess) The a
36c28 73 73 65 72 74 69 6f 6e 73 20 61 72 65 20 67 72  ssertions are gr
36c29 65 61 74 2c 20 62 75 74 20 77 68 61 74 20 69 66  eat, but what if
36c2a 20 77 65 27 72 65 20 69 6e 20 4e 44 45 42 55 47   we're in NDEBUG
36c2b 0a 2a 2a 20 61 6e 64 20 74 68 65 20 62 6c 6f 62  .** and the blob
36c2c 20 69 73 20 65 6d 70 74 79 20 6f 72 20 6f 74 68   is empty or oth
36c2d 65 72 77 69 73 65 20 63 6f 6e 74 61 69 6e 73 20  erwise contains 
36c2e 73 75 73 70 65 63 74 20 64 61 74 61 3f 0a 2a 2f  suspect data?.*/
36c2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 74  .static void int
36c30 65 72 69 6f 72 52 65 61 64 65 72 49 6e 69 74 28  eriorReaderInit(
36c31 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
36c32 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20  a, int nData,.  
36c33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36c34 20 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 74               Int
36c35 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70 52 65  eriorReader *pRe
36c36 61 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 2c 20  ader){.  int n, 
36c37 6e 54 65 72 6d 3b 0a 0a 20 20 2f 2a 20 52 65 71  nTerm;..  /* Req
36c38 75 69 72 65 20 61 74 20 6c 65 61 73 74 20 74 68  uire at least th
36c39 65 20 6c 65 61 64 69 6e 67 20 66 6c 61 67 20 62  e leading flag b
36c3a 79 74 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  yte */.  assert(
36c3b 20 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 61 73   nData>0 );.  as
36c3c 73 65 72 74 28 20 70 44 61 74 61 5b 30 5d 21 3d  sert( pData[0]!=
36c3d 27 5c 30 27 20 29 3b 0a 0a 20 20 43 4c 45 41 52  '\0' );..  CLEAR
36c3e 28 70 52 65 61 64 65 72 29 3b 0a 0a 20 20 2f 2a  (pReader);..  /*
36c3f 20 44 65 63 6f 64 65 20 74 68 65 20 62 61 73 65   Decode the base
36c40 20 62 6c 6f 63 6b 69 64 2c 20 61 6e 64 20 73 65   blockid, and se
36c41 74 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20  t the cursor to 
36c42 74 68 65 20 66 69 72 73 74 20 74 65 72 6d 2e 20  the first term. 
36c43 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65 74  */.  n = fts3Get
36c44 56 61 72 69 6e 74 28 70 44 61 74 61 2b 31 2c 20  Varint(pData+1, 
36c45 26 70 52 65 61 64 65 72 2d 3e 69 42 6c 6f 63 6b  &pReader->iBlock
36c46 69 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 31  id);.  assert( 1
36c47 2b 6e 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20 70  +n<=nData );.  p
36c48 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20 3d 20  Reader->pData = 
36c49 70 44 61 74 61 2b 31 2b 6e 3b 0a 20 20 70 52 65  pData+1+n;.  pRe
36c4a 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20 6e 44  ader->nData = nD
36c4b 61 74 61 2d 28 31 2b 6e 29 3b 0a 0a 20 20 2f 2a  ata-(1+n);..  /*
36c4c 20 41 20 73 69 6e 67 6c 65 2d 63 68 69 6c 64 20   A single-child 
36c4d 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 28 73  interior node (s
36c4e 75 63 68 20 61 73 20 77 68 65 6e 20 61 20 6c 65  uch as when a le
36c4f 61 66 20 6e 6f 64 65 20 77 61 73 20 74 6f 6f 0a  af node was too.
36c50 20 20 2a 2a 20 6c 61 72 67 65 20 66 6f 72 20 74    ** large for t
36c51 68 65 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63  he segment direc
36c52 74 6f 72 79 29 20 77 6f 6e 27 74 20 68 61 76 65  tory) won't have
36c53 20 61 6e 79 20 74 65 72 6d 73 2e 0a 20 20 2a 2a   any terms..  **
36c54 20 4f 74 68 65 72 77 69 73 65 2c 20 64 65 63 6f   Otherwise, deco
36c55 64 65 20 74 68 65 20 66 69 72 73 74 20 74 65 72  de the first ter
36c56 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 52  m..  */.  if( pR
36c57 65 61 64 65 72 2d 3e 6e 44 61 74 61 3d 3d 30 20  eader->nData==0 
36c58 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65  ){.    dataBuffe
36c59 72 49 6e 69 74 28 26 70 52 65 61 64 65 72 2d 3e  rInit(&pReader->
36c5a 74 65 72 6d 2c 20 30 29 3b 0a 20 20 7d 65 6c 73  term, 0);.  }els
36c5b 65 7b 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47  e{.    n = fts3G
36c5c 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64  etVarint32(pRead
36c5d 65 72 2d 3e 70 44 61 74 61 2c 20 26 6e 54 65 72  er->pData, &nTer
36c5e 6d 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66  m);.    dataBuff
36c5f 65 72 49 6e 69 74 28 26 70 52 65 61 64 65 72 2d  erInit(&pReader-
36c60 3e 74 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20  >term, nTerm);. 
36c61 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65 70     dataBufferRep
36c62 6c 61 63 65 28 26 70 52 65 61 64 65 72 2d 3e 74  lace(&pReader->t
36c63 65 72 6d 2c 20 70 52 65 61 64 65 72 2d 3e 70 44  erm, pReader->pD
36c64 61 74 61 2b 6e 2c 20 6e 54 65 72 6d 29 3b 0a 20  ata+n, nTerm);. 
36c65 20 20 20 61 73 73 65 72 74 28 20 6e 2b 6e 54 65     assert( n+nTe
36c66 72 6d 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61  rm<=pReader->nDa
36c67 74 61 20 29 3b 0a 20 20 20 20 70 52 65 61 64 65  ta );.    pReade
36c68 72 2d 3e 70 44 61 74 61 20 2b 3d 20 6e 2b 6e 54  r->pData += n+nT
36c69 65 72 6d 3b 0a 20 20 20 20 70 52 65 61 64 65 72  erm;.    pReader
36c6a 2d 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b 6e 54 65  ->nData -= n+nTe
36c6b 72 6d 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69  rm;.  }.}..stati
36c6c 63 20 69 6e 74 20 69 6e 74 65 72 69 6f 72 52 65  c int interiorRe
36c6d 61 64 65 72 41 74 45 6e 64 28 49 6e 74 65 72 69  aderAtEnd(Interi
36c6e 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  orReader *pReade
36c6f 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 52 65  r){.  return pRe
36c70 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61  ader->term.nData
36c71 3d 3d 30 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 73  ==0;.}..static s
36c72 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 6e 74 65  qlite_int64 inte
36c73 72 69 6f 72 52 65 61 64 65 72 43 75 72 72 65 6e  riorReaderCurren
36c74 74 42 6c 6f 63 6b 69 64 28 49 6e 74 65 72 69 6f  tBlockid(Interio
36c75 72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  rReader *pReader
36c76 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61  ){.  return pRea
36c77 64 65 72 2d 3e 69 42 6c 6f 63 6b 69 64 3b 0a 7d  der->iBlockid;.}
36c78 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 74  ..static int int
36c79 65 72 69 6f 72 52 65 61 64 65 72 54 65 72 6d 42  eriorReaderTermB
36c7a 79 74 65 73 28 49 6e 74 65 72 69 6f 72 52 65 61  ytes(InteriorRea
36c7b 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
36c7c 20 61 73 73 65 72 74 28 20 21 69 6e 74 65 72 69   assert( !interi
36c7d 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28 70 52  orReaderAtEnd(pR
36c7e 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75  eader) );.  retu
36c7f 72 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  rn pReader->term
36c80 2e 6e 44 61 74 61 3b 0a 7d 0a 73 74 61 74 69 63  .nData;.}.static
36c81 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 74   const char *int
36c82 65 72 69 6f 72 52 65 61 64 65 72 54 65 72 6d 28  eriorReaderTerm(
36c83 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a  InteriorReader *
36c84 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65  pReader){.  asse
36c85 72 74 28 20 21 69 6e 74 65 72 69 6f 72 52 65 61  rt( !interiorRea
36c86 64 65 72 41 74 45 6e 64 28 70 52 65 61 64 65 72  derAtEnd(pReader
36c87 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52  ) );.  return pR
36c88 65 61 64 65 72 2d 3e 74 65 72 6d 2e 70 44 61 74  eader->term.pDat
36c89 61 3b 0a 7d 0a 0a 2f 2a 20 53 74 65 70 20 66 6f  a;.}../* Step fo
36c8a 72 77 61 72 64 20 74 6f 20 74 68 65 20 6e 65 78  rward to the nex
36c8b 74 20 74 65 72 6d 20 69 6e 20 74 68 65 20 6e 6f  t term in the no
36c8c 64 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f  de. */.static vo
36c8d 69 64 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65  id interiorReade
36c8e 72 53 74 65 70 28 49 6e 74 65 72 69 6f 72 52 65  rStep(InteriorRe
36c8f 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
36c90 20 20 61 73 73 65 72 74 28 20 21 69 6e 74 65 72    assert( !inter
36c91 69 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28 70  iorReaderAtEnd(p
36c92 52 65 61 64 65 72 29 20 29 3b 0a 0a 20 20 2f 2a  Reader) );..  /*
36c93 20 49 66 20 74 68 65 20 6c 61 73 74 20 74 65 72   If the last ter
36c94 6d 20 68 61 73 20 62 65 65 6e 20 72 65 61 64 2c  m has been read,
36c95 20 73 69 67 6e 61 6c 20 65 6f 66 2c 20 65 6c 73   signal eof, els
36c96 65 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 0a  e construct the.
36c97 20 20 2a 2a 20 6e 65 78 74 20 74 65 72 6d 2e 0a    ** next term..
36c98 20 20 2a 2f 0a 20 20 69 66 28 20 70 52 65 61 64    */.  if( pRead
36c99 65 72 2d 3e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a  er->nData==0 ){.
36c9a 20 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65      dataBufferRe
36c9b 73 65 74 28 26 70 52 65 61 64 65 72 2d 3e 74 65  set(&pReader->te
36c9c 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  rm);.  }else{.  
36c9d 20 20 69 6e 74 20 6e 2c 20 6e 50 72 65 66 69 78    int n, nPrefix
36c9e 2c 20 6e 53 75 66 66 69 78 3b 0a 0a 20 20 20 20  , nSuffix;..    
36c9f 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e  n = fts3GetVarin
36ca0 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61  t32(pReader->pDa
36ca1 74 61 2c 20 26 6e 50 72 65 66 69 78 29 3b 0a 20  ta, &nPrefix);. 
36ca2 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56     n += fts3GetV
36ca3 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d  arint32(pReader-
36ca4 3e 70 44 61 74 61 2b 6e 2c 20 26 6e 53 75 66 66  >pData+n, &nSuff
36ca5 69 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 72 75  ix);..    /* Tru
36ca6 6e 63 61 74 65 20 74 68 65 20 63 75 72 72 65 6e  ncate the curren
36ca7 74 20 74 65 72 6d 20 61 6e 64 20 61 70 70 65 6e  t term and appen
36ca8 64 20 73 75 66 66 69 78 20 64 61 74 61 2e 20 2a  d suffix data. *
36ca9 2f 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 74  /.    pReader->t
36caa 65 72 6d 2e 6e 44 61 74 61 20 3d 20 6e 50 72 65  erm.nData = nPre
36cab 66 69 78 3b 0a 20 20 20 20 64 61 74 61 42 75 66  fix;.    dataBuf
36cac 66 65 72 41 70 70 65 6e 64 28 26 70 52 65 61 64  ferAppend(&pRead
36cad 65 72 2d 3e 74 65 72 6d 2c 20 70 52 65 61 64 65  er->term, pReade
36cae 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 6e 53 75 66  r->pData+n, nSuf
36caf 66 69 78 29 3b 0a 0a 20 20 20 20 61 73 73 65 72  fix);..    asser
36cb0 74 28 20 6e 2b 6e 53 75 66 66 69 78 3c 3d 70 52  t( n+nSuffix<=pR
36cb1 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a  eader->nData );.
36cb2 20 20 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61      pReader->pDa
36cb3 74 61 20 2b 3d 20 6e 2b 6e 53 75 66 66 69 78 3b  ta += n+nSuffix;
36cb4 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 6e 44  .    pReader->nD
36cb5 61 74 61 20 2d 3d 20 6e 2b 6e 53 75 66 66 69 78  ata -= n+nSuffix
36cb6 3b 0a 20 20 7d 0a 20 20 70 52 65 61 64 65 72 2d  ;.  }.  pReader-
36cb7 3e 69 42 6c 6f 63 6b 69 64 2b 2b 3b 0a 7d 0a 0a  >iBlockid++;.}..
36cb8 2f 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20 63  /* Compare the c
36cb9 75 72 72 65 6e 74 20 74 65 72 6d 20 74 6f 20 70  urrent term to p
36cba 54 65 72 6d 5b 6e 54 65 72 6d 5d 2c 20 72 65 74  Term[nTerm], ret
36cbb 75 72 6e 69 6e 67 20 73 74 72 63 6d 70 2d 73 74  urning strcmp-st
36cbc 79 6c 65 0a 2a 2a 20 72 65 73 75 6c 74 73 2e 20  yle.** results. 
36cbd 20 49 66 20 69 73 50 72 65 66 69 78 2c 20 65 71   If isPrefix, eq
36cbe 75 61 6c 69 74 79 20 6d 65 61 6e 73 20 65 71 75  uality means equ
36cbf 61 6c 20 74 68 72 6f 75 67 68 20 6e 54 65 72 6d  al through nTerm
36cc0 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69   bytes..*/.stati
36cc1 63 20 69 6e 74 20 69 6e 74 65 72 69 6f 72 52 65  c int interiorRe
36cc2 61 64 65 72 54 65 72 6d 43 6d 70 28 49 6e 74 65  aderTermCmp(Inte
36cc3 72 69 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61  riorReader *pRea
36cc4 64 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  der,.           
36cc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36cc6 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
36cc7 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65   *pTerm, int nTe
36cc8 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78  rm, int isPrefix
36cc9 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
36cca 2a 70 52 65 61 64 65 72 54 65 72 6d 20 3d 20 69  *pReaderTerm = i
36ccb 6e 74 65 72 69 6f 72 52 65 61 64 65 72 54 65 72  nteriorReaderTer
36ccc 6d 28 70 52 65 61 64 65 72 29 3b 0a 20 20 69 6e  m(pReader);.  in
36ccd 74 20 6e 52 65 61 64 65 72 54 65 72 6d 20 3d 20  t nReaderTerm = 
36cce 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 54 65  interiorReaderTe
36ccf 72 6d 42 79 74 65 73 28 70 52 65 61 64 65 72 29  rmBytes(pReader)
36cd0 3b 0a 20 20 69 6e 74 20 63 2c 20 6e 20 3d 20 6e  ;.  int c, n = n
36cd1 52 65 61 64 65 72 54 65 72 6d 3c 6e 54 65 72 6d  ReaderTerm<nTerm
36cd2 20 3f 20 6e 52 65 61 64 65 72 54 65 72 6d 20 3a   ? nReaderTerm :
36cd3 20 6e 54 65 72 6d 3b 0a 0a 20 20 69 66 28 20 6e   nTerm;..  if( n
36cd4 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e  ==0 ){.    if( n
36cd5 52 65 61 64 65 72 54 65 72 6d 3e 30 20 29 20 72  ReaderTerm>0 ) r
36cd6 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 69 66  eturn -1;.    if
36cd7 28 20 6e 54 65 72 6d 3e 30 20 29 20 72 65 74 75  ( nTerm>0 ) retu
36cd8 72 6e 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e  rn 1;.    return
36cd9 20 30 3b 0a 20 20 7d 0a 0a 20 20 63 20 3d 20 6d   0;.  }..  c = m
36cda 65 6d 63 6d 70 28 70 52 65 61 64 65 72 54 65 72  emcmp(pReaderTer
36cdb 6d 2c 20 70 54 65 72 6d 2c 20 6e 29 3b 0a 20 20  m, pTerm, n);.  
36cdc 69 66 28 20 63 21 3d 30 20 29 20 72 65 74 75 72  if( c!=0 ) retur
36cdd 6e 20 63 3b 0a 20 20 69 66 28 20 69 73 50 72 65  n c;.  if( isPre
36cde 66 69 78 20 26 26 20 6e 3d 3d 6e 54 65 72 6d 20  fix && n==nTerm 
36cdf 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65  ) return 0;.  re
36ce0 74 75 72 6e 20 6e 52 65 61 64 65 72 54 65 72 6d  turn nReaderTerm
36ce1 20 2d 20 6e 54 65 72 6d 3b 0a 7d 0a 0a 2f 2a 2a   - nTerm;.}../**
36ce2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36ce3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36ce4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36ce5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
36ce6 2f 2a 20 4c 65 61 66 57 72 69 74 65 72 20 69 73  /* LeafWriter is
36ce7 20 75 73 65 64 20 74 6f 20 63 6f 6c 6c 65 63 74   used to collect
36ce8 20 74 65 72 6d 73 20 61 6e 64 20 61 73 73 6f 63   terms and assoc
36ce9 69 61 74 65 64 20 64 6f 63 6c 69 73 74 20 64 61  iated doclist da
36cea 74 61 0a 2a 2a 20 69 6e 74 6f 20 6c 65 61 66 20  ta.** into leaf 
36ceb 62 6c 6f 63 6b 73 20 69 6e 20 25 5f 73 65 67 6d  blocks in %_segm
36cec 65 6e 74 73 20 28 73 65 65 20 74 6f 70 20 6f 66  ents (see top of
36ced 20 66 69 6c 65 20 66 6f 72 20 66 6f 72 6d 61 74   file for format
36cee 20 69 6e 66 6f 29 2e 0a 2a 2a 20 45 78 70 65 63   info)..** Expec
36cef 74 65 64 20 75 73 61 67 65 20 69 73 3a 0a 2a 2a  ted usage is:.**
36cf0 0a 2a 2a 20 4c 65 61 66 57 72 69 74 65 72 20 77  .** LeafWriter w
36cf1 72 69 74 65 72 3b 0a 2a 2a 20 6c 65 61 66 57 72  riter;.** leafWr
36cf2 69 74 65 72 49 6e 69 74 28 30 2c 20 30 2c 20 26  iterInit(0, 0, &
36cf3 77 72 69 74 65 72 29 3b 0a 2a 2a 20 77 68 69 6c  writer);.** whil
36cf4 65 28 20 73 6f 72 74 65 64 5f 74 65 72 6d 73 5f  e( sorted_terms_
36cf5 6c 65 66 74 5f 74 6f 5f 70 72 6f 63 65 73 73 20  left_to_process 
36cf6 29 7b 0a 2a 2a 20 20 20 2f 2f 20 64 61 74 61 20  ){.**   // data 
36cf7 69 73 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20  is doclist data 
36cf8 66 6f 72 20 74 68 61 74 20 74 65 72 6d 2e 0a 2a  for that term..*
36cf9 2a 20 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69  *   rc = leafWri
36cfa 74 65 72 53 74 65 70 28 76 2c 20 26 77 72 69 74  terStep(v, &writ
36cfb 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d  er, pTerm, nTerm
36cfc 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 29 3b  , pData, nData);
36cfd 0a 2a 2a 20 20 20 69 66 28 20 72 63 21 3d 53 51  .**   if( rc!=SQ
36cfe 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
36cff 72 72 3b 0a 2a 2a 20 7d 0a 2a 2a 20 72 63 20 3d  rr;.** }.** rc =
36d00 20 6c 65 61 66 57 72 69 74 65 72 46 69 6e 61 6c   leafWriterFinal
36d01 69 7a 65 28 76 2c 20 26 77 72 69 74 65 72 29 3b  ize(v, &writer);
36d02 0a 2a 2a 65 72 72 3a 0a 2a 2a 20 6c 65 61 66 57  .**err:.** leafW
36d03 72 69 74 65 72 44 65 73 74 72 6f 79 28 26 77 72  riterDestroy(&wr
36d04 69 74 65 72 29 3b 0a 2a 2a 20 72 65 74 75 72 6e  iter);.** return
36d05 20 72 63 3b 0a 2a 2a 0a 2a 2a 20 6c 65 61 66 57   rc;.**.** leafW
36d06 72 69 74 65 72 53 74 65 70 28 29 20 6d 61 79 20  riterStep() may 
36d07 77 72 69 74 65 20 61 20 63 6f 6c 6c 65 63 74 65  write a collecte
36d08 64 20 6c 65 61 66 20 6f 75 74 20 74 6f 20 25 5f  d leaf out to %_
36d09 73 65 67 6d 65 6e 74 73 2e 0a 2a 2a 20 6c 65 61  segments..** lea
36d0a 66 57 72 69 74 65 72 46 69 6e 61 6c 69 7a 65 28  fWriterFinalize(
36d0b 29 20 66 69 6e 69 73 68 65 73 20 77 72 69 74 69  ) finishes writi
36d0c 6e 67 20 61 6e 79 20 62 75 66 66 65 72 65 64 20  ng any buffered 
36d0d 64 61 74 61 20 61 6e 64 20 73 74 6f 72 65 73 0a  data and stores.
36d0e 2a 2a 20 61 20 72 6f 6f 74 20 6e 6f 64 65 20 69  ** a root node i
36d0f 6e 20 25 5f 73 65 67 64 69 72 2e 20 20 6c 65 61  n %_segdir.  lea
36d10 66 57 72 69 74 65 72 44 65 73 74 72 6f 79 28 29  fWriterDestroy()
36d11 20 66 72 65 65 73 20 61 6c 6c 20 62 75 66 66 65   frees all buffe
36d12 72 73 20 61 6e 64 0a 2a 2a 20 49 6e 74 65 72 69  rs and.** Interi
36d13 6f 72 57 72 69 74 65 72 73 20 61 6c 6c 6f 63 61  orWriters alloca
36d14 74 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 77  ted as part of w
36d15 72 69 74 69 6e 67 20 74 68 69 73 20 73 65 67 6d  riting this segm
36d16 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28  ent..**.** TODO(
36d17 73 68 65 73 73 29 20 44 6f 63 75 6d 65 6e 74 20  shess) Document 
36d18 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 4d 65  leafWriterStepMe
36d19 72 67 65 28 29 2e 0a 2a 2f 0a 0a 2f 2a 20 50 75  rge()..*/../* Pu
36d1a 74 20 74 65 72 6d 73 20 77 69 74 68 20 64 61 74  t terms with dat
36d1b 61 20 74 68 69 73 20 62 69 67 20 69 6e 20 74 68  a this big in th
36d1c 65 69 72 20 6f 77 6e 20 62 6c 6f 63 6b 2e 20 2a  eir own block. *
36d1d 2f 0a 23 64 65 66 69 6e 65 20 53 54 41 4e 44 41  /.#define STANDA
36d1e 4c 4f 4e 45 5f 4d 49 4e 20 31 30 32 34 0a 0a 2f  LONE_MIN 1024../
36d1f 2a 20 4b 65 65 70 20 6c 65 61 66 20 62 6c 6f 63  * Keep leaf bloc
36d20 6b 73 20 62 65 6c 6f 77 20 74 68 69 73 20 73 69  ks below this si
36d21 7a 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4c  ze. */.#define L
36d22 45 41 46 5f 4d 41 58 20 32 30 34 38 0a 0a 74 79  EAF_MAX 2048..ty
36d23 70 65 64 65 66 20 73 74 72 75 63 74 20 4c 65 61  pedef struct Lea
36d24 66 57 72 69 74 65 72 20 7b 0a 20 20 69 6e 74 20  fWriter {.  int 
36d25 69 4c 65 76 65 6c 3b 0a 20 20 69 6e 74 20 69 64  iLevel;.  int id
36d26 78 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  x;.  sqlite_int6
36d27 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 3b  4 iStartBlockid;
36d28 20 20 20 20 20 2f 2a 20 6e 65 65 64 65 64 20 74       /* needed t
36d29 6f 20 63 72 65 61 74 65 20 74 68 65 20 72 6f 6f  o create the roo
36d2a 74 20 69 6e 66 6f 20 2a 2f 0a 20 20 73 71 6c 69  t info */.  sqli
36d2b 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f  te_int64 iEndBlo
36d2c 63 6b 69 64 3b 20 20 20 20 20 20 20 2f 2a 20 77  ckid;       /* w
36d2d 68 65 6e 20 77 65 27 72 65 20 64 6f 6e 65 20 77  hen we're done w
36d2e 72 69 74 69 6e 67 2e 20 2a 2f 0a 0a 20 20 44 61  riting. */..  Da
36d2f 74 61 42 75 66 66 65 72 20 74 65 72 6d 3b 20 20  taBuffer term;  
36d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36d31 20 70 72 65 76 69 6f 75 73 20 65 6e 63 6f 64 65   previous encode
36d32 64 20 74 65 72 6d 20 2a 2f 0a 20 20 44 61 74 61  d term */.  Data
36d33 42 75 66 66 65 72 20 64 61 74 61 3b 20 20 20 20  Buffer data;    
36d34 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 65              /* e
36d35 6e 63 6f 64 69 6e 67 20 62 75 66 66 65 72 20 2a  ncoding buffer *
36d36 2f 0a 0a 20 20 2f 2a 20 62 79 74 65 73 20 6f 66  /..  /* bytes of
36d37 20 66 69 72 73 74 20 74 65 72 6d 20 69 6e 20 74   first term in t
36d38 68 65 20 63 75 72 72 65 6e 74 20 6e 6f 64 65 20  he current node 
36d39 77 68 69 63 68 20 64 69 73 74 69 6e 67 75 69 73  which distinguis
36d3a 68 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 74 65  hes that.  ** te
36d3b 72 6d 20 66 72 6f 6d 20 74 68 65 20 6c 61 73 74  rm from the last
36d3c 20 74 65 72 6d 20 6f 66 20 74 68 65 20 70 72 65   term of the pre
36d3d 76 69 6f 75 73 20 6e 6f 64 65 2e 0a 20 20 2a 2f  vious node..  */
36d3e 0a 20 20 69 6e 74 20 6e 54 65 72 6d 44 69 73 74  .  int nTermDist
36d3f 69 6e 63 74 3b 0a 0a 20 20 49 6e 74 65 72 69 6f  inct;..  Interio
36d40 72 57 72 69 74 65 72 20 70 61 72 65 6e 74 57 72  rWriter parentWr
36d41 69 74 65 72 3b 20 20 20 20 2f 2a 20 69 66 20 77  iter;    /* if w
36d42 65 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20  e overflow */.  
36d43 69 6e 74 20 68 61 73 5f 70 61 72 65 6e 74 3b 0a  int has_parent;.
36d44 7d 20 4c 65 61 66 57 72 69 74 65 72 3b 0a 0a 73  } LeafWriter;..s
36d45 74 61 74 69 63 20 76 6f 69 64 20 6c 65 61 66 57  tatic void leafW
36d46 72 69 74 65 72 49 6e 69 74 28 69 6e 74 20 69 4c  riterInit(int iL
36d47 65 76 65 6c 2c 20 69 6e 74 20 69 64 78 2c 20 4c  evel, int idx, L
36d48 65 61 66 57 72 69 74 65 72 20 2a 70 57 72 69 74  eafWriter *pWrit
36d49 65 72 29 7b 0a 20 20 43 4c 45 41 52 28 70 57 72  er){.  CLEAR(pWr
36d4a 69 74 65 72 29 3b 0a 20 20 70 57 72 69 74 65 72  iter);.  pWriter
36d4b 2d 3e 69 4c 65 76 65 6c 20 3d 20 69 4c 65 76 65  ->iLevel = iLeve
36d4c 6c 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 64  l;.  pWriter->id
36d4d 78 20 3d 20 69 64 78 3b 0a 0a 20 20 64 61 74 61  x = idx;..  data
36d4e 42 75 66 66 65 72 49 6e 69 74 28 26 70 57 72 69  BufferInit(&pWri
36d4f 74 65 72 2d 3e 74 65 72 6d 2c 20 33 32 29 3b 0a  ter->term, 32);.
36d50 0a 20 20 2f 2a 20 53 74 61 72 74 20 6f 75 74 20  .  /* Start out 
36d51 77 69 74 68 20 61 20 72 65 61 73 6f 6e 61 62 6c  with a reasonabl
36d52 79 20 73 69 7a 65 64 20 62 6c 6f 63 6b 2c 20 74  y sized block, t
36d53 68 6f 75 67 68 20 69 74 20 63 61 6e 20 67 72 6f  hough it can gro
36d54 77 2e 20 2a 2f 0a 20 20 64 61 74 61 42 75 66 66  w. */.  dataBuff
36d55 65 72 49 6e 69 74 28 26 70 57 72 69 74 65 72 2d  erInit(&pWriter-
36d56 3e 64 61 74 61 2c 20 4c 45 41 46 5f 4d 41 58 29  >data, LEAF_MAX)
36d57 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  ;.}..#ifndef NDE
36d58 42 55 47 0a 2f 2a 20 56 65 72 69 66 79 20 74 68  BUG./* Verify th
36d59 61 74 20 74 68 65 20 64 61 74 61 20 69 73 20 72  at the data is r
36d5a 65 61 64 61 62 6c 65 20 61 73 20 61 20 6c 65 61  eadable as a lea
36d5b 66 20 6e 6f 64 65 2e 20 2a 2f 0a 73 74 61 74 69  f node. */.stati
36d5c 63 20 76 6f 69 64 20 6c 65 61 66 4e 6f 64 65 56  c void leafNodeV
36d5d 61 6c 69 64 61 74 65 28 63 6f 6e 73 74 20 63 68  alidate(const ch
36d5e 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e  ar *pData, int n
36d5f 44 61 74 61 29 7b 0a 20 20 69 6e 74 20 6e 2c 20  Data){.  int n, 
36d60 69 44 75 6d 6d 79 3b 0a 0a 20 20 69 66 28 20 6e  iDummy;..  if( n
36d61 44 61 74 61 3d 3d 30 20 29 20 72 65 74 75 72 6e  Data==0 ) return
36d62 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61 74  ;.  assert( nDat
36d63 61 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  a>0 );.  assert(
36d64 20 70 44 61 74 61 21 3d 30 20 29 3b 0a 20 20 61   pData!=0 );.  a
36d65 73 73 65 72 74 28 20 70 44 61 74 61 2b 6e 44 61  ssert( pData+nDa
36d66 74 61 3e 70 44 61 74 61 20 29 3b 0a 0a 20 20 2f  ta>pData );..  /
36d67 2a 20 4d 75 73 74 20 6c 65 61 64 20 77 69 74 68  * Must lead with
36d68 20 61 20 76 61 72 69 6e 74 28 30 29 20 2a 2f 0a   a varint(0) */.
36d69 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72    n = fts3GetVar
36d6a 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44  int32(pData, &iD
36d6b 75 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28  ummy);.  assert(
36d6c 20 69 44 75 6d 6d 79 3d 3d 30 20 29 3b 0a 20 20   iDummy==0 );.  
36d6d 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20  assert( n>0 );. 
36d6e 20 61 73 73 65 72 74 28 20 6e 3c 6e 44 61 74 61   assert( n<nData
36d6f 20 29 3b 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e   );.  pData += n
36d70 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a  ;.  nData -= n;.
36d71 0a 20 20 2f 2a 20 4c 65 61 64 69 6e 67 20 74 65  .  /* Leading te
36d72 72 6d 20 6c 65 6e 67 74 68 20 61 6e 64 20 64 61  rm length and da
36d73 74 61 20 6d 75 73 74 20 66 69 74 20 69 6e 20 62  ta must fit in b
36d74 75 66 66 65 72 2e 20 2a 2f 0a 20 20 6e 20 3d 20  uffer. */.  n = 
36d75 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
36d76 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b  pData, &iDummy);
36d77 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29  .  assert( n>0 )
36d78 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 75 6d  ;.  assert( iDum
36d79 6d 79 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74  my>0 );.  assert
36d7a 28 20 6e 2b 69 44 75 6d 6d 79 3e 30 20 29 3b 0a  ( n+iDummy>0 );.
36d7b 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d    assert( n+iDum
36d7c 6d 79 3c 6e 44 61 74 61 20 29 3b 0a 20 20 70 44  my<nData );.  pD
36d7d 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b  ata += n+iDummy;
36d7e 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44  .  nData -= n+iD
36d7f 75 6d 6d 79 3b 0a 0a 20 20 2f 2a 20 4c 65 61 64  ummy;..  /* Lead
36d80 69 6e 67 20 74 65 72 6d 27 73 20 64 6f 63 6c 69  ing term's docli
36d81 73 74 20 6c 65 6e 67 74 68 20 61 6e 64 20 64 61  st length and da
36d82 74 61 20 6d 75 73 74 20 66 69 74 2e 20 2a 2f 0a  ta must fit. */.
36d83 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72    n = fts3GetVar
36d84 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44  int32(pData, &iD
36d85 75 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28  ummy);.  assert(
36d86 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74   n>0 );.  assert
36d87 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20  ( iDummy>0 );.  
36d88 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79  assert( n+iDummy
36d89 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
36d8a 6e 2b 69 44 75 6d 6d 79 3c 3d 6e 44 61 74 61 20  n+iDummy<=nData 
36d8b 29 3b 0a 20 20 41 53 53 45 52 54 5f 56 41 4c 49  );.  ASSERT_VALI
36d8c 44 5f 44 4f 43 4c 49 53 54 28 44 4c 5f 44 45 46  D_DOCLIST(DL_DEF
36d8d 41 55 4c 54 2c 20 70 44 61 74 61 2b 6e 2c 20 69  AULT, pData+n, i
36d8e 44 75 6d 6d 79 2c 20 4e 55 4c 4c 29 3b 0a 20 20  Dummy, NULL);.  
36d8f 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d  pData += n+iDumm
36d90 79 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b  y;.  nData -= n+
36d91 69 44 75 6d 6d 79 3b 0a 0a 20 20 2f 2a 20 56 65  iDummy;..  /* Ve
36d92 72 69 66 79 20 74 68 61 74 20 74 72 61 69 6c 69  rify that traili
36d93 6e 67 20 74 65 72 6d 73 20 61 6e 64 20 64 6f 63  ng terms and doc
36d94 6c 69 73 74 73 20 61 6c 73 6f 20 61 72 65 20 72  lists also are r
36d95 65 61 64 61 62 6c 65 2e 20 2a 2f 0a 20 20 77 68  eadable. */.  wh
36d96 69 6c 65 28 20 6e 44 61 74 61 21 3d 30 20 29 7b  ile( nData!=0 ){
36d97 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74  .    n = fts3Get
36d98 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c 20  Varint32(pData, 
36d99 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 61 73  &iDummy);.    as
36d9a 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 20  sert( n>0 );.   
36d9b 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79 3e   assert( iDummy>
36d9c 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
36d9d 28 20 6e 3c 6e 44 61 74 61 20 29 3b 0a 20 20 20  ( n<nData );.   
36d9e 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20 20 20   pData += n;.   
36d9f 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a 20 20 20   nData -= n;.   
36da0 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69   n = fts3GetVari
36da1 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75  nt32(pData, &iDu
36da2 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74  mmy);.    assert
36da3 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20 61 73 73  ( n>0 );.    ass
36da4 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b  ert( iDummy>0 );
36da5 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69  .    assert( n+i
36da6 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20 61  Dummy>0 );.    a
36da7 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c  ssert( n+iDummy<
36da8 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 70 44 61  nData );.    pDa
36da9 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a  ta += n+iDummy;.
36daa 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b 69      nData -= n+i
36dab 44 75 6d 6d 79 3b 0a 0a 20 20 20 20 6e 20 3d 20  Dummy;..    n = 
36dac 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
36dad 70 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b  pData, &iDummy);
36dae 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30  .    assert( n>0
36daf 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
36db0 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20  iDummy>0 );.    
36db1 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79  assert( n+iDummy
36db2 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  >0 );.    assert
36db3 28 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e 44 61 74  ( n+iDummy<=nDat
36db4 61 20 29 3b 0a 20 20 20 20 41 53 53 45 52 54 5f  a );.    ASSERT_
36db5 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 44 4c  VALID_DOCLIST(DL
36db6 5f 44 45 46 41 55 4c 54 2c 20 70 44 61 74 61 2b  _DEFAULT, pData+
36db7 6e 2c 20 69 44 75 6d 6d 79 2c 20 4e 55 4c 4c 29  n, iDummy, NULL)
36db8 3b 0a 20 20 20 20 70 44 61 74 61 20 2b 3d 20 6e  ;.    pData += n
36db9 2b 69 44 75 6d 6d 79 3b 0a 20 20 20 20 6e 44 61  +iDummy;.    nDa
36dba 74 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a  ta -= n+iDummy;.
36dbb 20 20 7d 0a 7d 0a 23 64 65 66 69 6e 65 20 41 53    }.}.#define AS
36dbc 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45 41 46 5f  SERT_VALID_LEAF_
36dbd 4e 4f 44 45 28 70 2c 20 6e 29 20 6c 65 61 66 4e  NODE(p, n) leafN
36dbe 6f 64 65 56 61 6c 69 64 61 74 65 28 70 2c 20 6e  odeValidate(p, n
36dbf 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  ).#else.#define 
36dc0 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45 41  ASSERT_VALID_LEA
36dc1 46 5f 4e 4f 44 45 28 70 2c 20 6e 29 20 61 73 73  F_NODE(p, n) ass
36dc2 65 72 74 28 20 31 20 29 0a 23 65 6e 64 69 66 0a  ert( 1 ).#endif.
36dc3 0a 2f 2a 20 46 6c 75 73 68 20 74 68 65 20 63 75  ./* Flush the cu
36dc4 72 72 65 6e 74 20 6c 65 61 66 20 6e 6f 64 65 20  rrent leaf node 
36dc5 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 2c 20 61  to %_segments, a
36dc6 6e 64 20 61 64 64 69 6e 67 20 74 68 65 20 72 65  nd adding the re
36dc7 73 75 6c 74 69 6e 67 0a 2a 2a 20 62 6c 6f 63 6b  sulting.** block
36dc8 69 64 20 61 6e 64 20 74 68 65 20 73 74 61 72 74  id and the start
36dc9 69 6e 67 20 74 65 72 6d 20 74 6f 20 74 68 65 20  ing term to the 
36dca 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 77 68  interior node wh
36dcb 69 63 68 20 77 69 6c 6c 0a 2a 2a 20 63 6f 6e 74  ich will.** cont
36dcc 61 69 6e 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69  ain it..*/.stati
36dcd 63 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72  c int leafWriter
36dce 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68 28 66 75  InternalFlush(fu
36dcf 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
36dd0 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57 72 69  LeafWriter *pWri
36dd1 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ter,.           
36dd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36dd3 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 61 74          int iDat
36dd4 61 2c 20 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20  a, int nData){. 
36dd5 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 42   sqlite_int64 iB
36dd6 6c 6f 63 6b 69 64 20 3d 20 30 3b 0a 20 20 63 6f  lockid = 0;.  co
36dd7 6e 73 74 20 63 68 61 72 20 2a 70 53 74 61 72 74  nst char *pStart
36dd8 69 6e 67 54 65 72 6d 3b 0a 20 20 69 6e 74 20 6e  ingTerm;.  int n
36dd9 53 74 61 72 74 69 6e 67 54 65 72 6d 2c 20 72 63  StartingTerm, rc
36dda 2c 20 6e 3b 0a 0a 20 20 2f 2a 20 4d 75 73 74 20  , n;..  /* Must 
36ddb 68 61 76 65 20 74 68 65 20 6c 65 61 64 69 6e 67  have the leading
36ddc 20 76 61 72 69 6e 74 28 30 29 20 66 6c 61 67 2c   varint(0) flag,
36ddd 20 70 6c 75 73 20 61 74 20 6c 65 61 73 74 20 73   plus at least s
36dde 6f 6d 65 0a 20 20 2a 2a 20 76 61 6c 69 64 2d 6c  ome.  ** valid-l
36ddf 6f 6f 6b 69 6e 67 20 64 61 74 61 2e 0a 20 20 2a  ooking data..  *
36de0 2f 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61 74  /.  assert( nDat
36de1 61 3e 32 20 29 3b 0a 20 20 61 73 73 65 72 74 28  a>2 );.  assert(
36de2 20 69 44 61 74 61 3e 3d 30 20 29 3b 0a 20 20 61   iData>=0 );.  a
36de3 73 73 65 72 74 28 20 69 44 61 74 61 2b 6e 44 61  ssert( iData+nDa
36de4 74 61 3c 3d 70 57 72 69 74 65 72 2d 3e 64 61 74  ta<=pWriter->dat
36de5 61 2e 6e 44 61 74 61 20 29 3b 0a 20 20 41 53 53  a.nData );.  ASS
36de6 45 52 54 5f 56 41 4c 49 44 5f 4c 45 41 46 5f 4e  ERT_VALID_LEAF_N
36de7 4f 44 45 28 70 57 72 69 74 65 72 2d 3e 64 61 74  ODE(pWriter->dat
36de8 61 2e 70 44 61 74 61 2b 69 44 61 74 61 2c 20 6e  a.pData+iData, n
36de9 44 61 74 61 29 3b 0a 0a 20 20 72 63 20 3d 20 62  Data);..  rc = b
36dea 6c 6f 63 6b 5f 69 6e 73 65 72 74 28 76 2c 20 70  lock_insert(v, p
36deb 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61  Writer->data.pDa
36dec 74 61 2b 69 44 61 74 61 2c 20 6e 44 61 74 61 2c  ta+iData, nData,
36ded 20 26 69 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69   &iBlockid);.  i
36dee 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
36def 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
36df0 61 73 73 65 72 74 28 20 69 42 6c 6f 63 6b 69 64  assert( iBlockid
36df1 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 63  !=0 );..  /* Rec
36df2 6f 6e 73 74 72 75 63 74 20 74 68 65 20 66 69 72  onstruct the fir
36df3 73 74 20 74 65 72 6d 20 69 6e 20 74 68 65 20 6c  st term in the l
36df4 65 61 66 20 66 6f 72 20 70 75 72 70 6f 73 65 73  eaf for purposes
36df5 20 6f 66 20 62 75 69 6c 64 69 6e 67 0a 20 20 2a   of building.  *
36df6 2a 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20 6e  * the interior n
36df7 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 6e 20 3d 20  ode..  */.  n = 
36df8 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
36df9 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44  pWriter->data.pD
36dfa 61 74 61 2b 69 44 61 74 61 2b 31 2c 20 26 6e 53  ata+iData+1, &nS
36dfb 74 61 72 74 69 6e 67 54 65 72 6d 29 3b 0a 20 20  tartingTerm);.  
36dfc 70 53 74 61 72 74 69 6e 67 54 65 72 6d 20 3d 20  pStartingTerm = 
36dfd 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44  pWriter->data.pD
36dfe 61 74 61 2b 69 44 61 74 61 2b 31 2b 6e 3b 0a 20  ata+iData+1+n;. 
36dff 20 61 73 73 65 72 74 28 20 70 57 72 69 74 65 72   assert( pWriter
36e00 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3e 69 44 61  ->data.nData>iDa
36e01 74 61 2b 31 2b 6e 2b 6e 53 74 61 72 74 69 6e 67  ta+1+n+nStarting
36e02 54 65 72 6d 20 29 3b 0a 20 20 61 73 73 65 72 74  Term );.  assert
36e03 28 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72 6d  ( pWriter->nTerm
36e04 44 69 73 74 69 6e 63 74 3e 30 20 29 3b 0a 20 20  Distinct>0 );.  
36e05 61 73 73 65 72 74 28 20 70 57 72 69 74 65 72 2d  assert( pWriter-
36e06 3e 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3c 3d  >nTermDistinct<=
36e07 6e 53 74 61 72 74 69 6e 67 54 65 72 6d 20 29 3b  nStartingTerm );
36e08 0a 20 20 6e 53 74 61 72 74 69 6e 67 54 65 72 6d  .  nStartingTerm
36e09 20 3d 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72   = pWriter->nTer
36e0a 6d 44 69 73 74 69 6e 63 74 3b 0a 0a 20 20 69 66  mDistinct;..  if
36e0b 28 20 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 70  ( pWriter->has_p
36e0c 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74  arent ){.    int
36e0d 65 72 69 6f 72 57 72 69 74 65 72 41 70 70 65 6e  eriorWriterAppen
36e0e 64 28 26 70 57 72 69 74 65 72 2d 3e 70 61 72 65  d(&pWriter->pare
36e0f 6e 74 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20  ntWriter,.      
36e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36e11 20 20 20 70 53 74 61 72 74 69 6e 67 54 65 72 6d     pStartingTerm
36e12 2c 20 6e 53 74 61 72 74 69 6e 67 54 65 72 6d 2c  , nStartingTerm,
36e13 20 69 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 7d 65   iBlockid);.  }e
36e14 6c 73 65 7b 0a 20 20 20 20 69 6e 74 65 72 69 6f  lse{.    interio
36e15 72 57 72 69 74 65 72 49 6e 69 74 28 31 2c 20 70  rWriterInit(1, p
36e16 53 74 61 72 74 69 6e 67 54 65 72 6d 2c 20 6e 53  StartingTerm, nS
36e17 74 61 72 74 69 6e 67 54 65 72 6d 2c 20 69 42 6c  tartingTerm, iBl
36e18 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20  ockid,.         
36e19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70                &p
36e1a 57 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57 72  Writer->parentWr
36e1b 69 74 65 72 29 3b 0a 20 20 20 20 70 57 72 69 74  iter);.    pWrit
36e1c 65 72 2d 3e 68 61 73 5f 70 61 72 65 6e 74 20 3d  er->has_parent =
36e1d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 72   1;.  }..  /* Tr
36e1e 61 63 6b 20 74 68 65 20 73 70 61 6e 20 6f 66 20  ack the span of 
36e1f 74 68 69 73 20 73 65 67 6d 65 6e 74 27 73 20 6c  this segment's l
36e20 65 61 66 20 6e 6f 64 65 73 2e 20 2a 2f 0a 20 20  eaf nodes. */.  
36e21 69 66 28 20 70 57 72 69 74 65 72 2d 3e 69 45 6e  if( pWriter->iEn
36e22 64 42 6c 6f 63 6b 69 64 3d 3d 30 20 29 7b 0a 20  dBlockid==0 ){. 
36e23 20 20 20 70 57 72 69 74 65 72 2d 3e 69 45 6e 64     pWriter->iEnd
36e24 42 6c 6f 63 6b 69 64 20 3d 20 70 57 72 69 74 65  Blockid = pWrite
36e25 72 2d 3e 69 53 74 61 72 74 42 6c 6f 63 6b 69 64  r->iStartBlockid
36e26 20 3d 20 69 42 6c 6f 63 6b 69 64 3b 0a 20 20 7d   = iBlockid;.  }
36e27 65 6c 73 65 7b 0a 20 20 20 20 70 57 72 69 74 65  else{.    pWrite
36e28 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 2b 2b  r->iEndBlockid++
36e29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 42  ;.    assert( iB
36e2a 6c 6f 63 6b 69 64 3d 3d 70 57 72 69 74 65 72 2d  lockid==pWriter-
36e2b 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 20 29 3b 0a  >iEndBlockid );.
36e2c 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51    }..  return SQ
36e2d 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69  LITE_OK;.}.stati
36e2e 63 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65 72  c int leafWriter
36e2f 46 6c 75 73 68 28 66 75 6c 6c 74 65 78 74 5f 76  Flush(fulltext_v
36e30 74 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74  tab *v, LeafWrit
36e31 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20  er *pWriter){.  
36e32 69 6e 74 20 72 63 20 3d 20 6c 65 61 66 57 72 69  int rc = leafWri
36e33 74 65 72 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68  terInternalFlush
36e34 28 76 2c 20 70 57 72 69 74 65 72 2c 20 30 2c 20  (v, pWriter, 0, 
36e35 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
36e36 61 74 61 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  ata);.  if( rc!=
36e37 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
36e38 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 52 65 2d  rn rc;..  /* Re-
36e39 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f  initialize the o
36e3a 75 74 70 75 74 20 62 75 66 66 65 72 2e 20 2a 2f  utput buffer. */
36e3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 52 65 73  .  dataBufferRes
36e3c 65 74 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74  et(&pWriter->dat
36e3d 61 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51  a);..  return SQ
36e3e 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 46  LITE_OK;.}../* F
36e3f 65 74 63 68 20 74 68 65 20 72 6f 6f 74 20 69 6e  etch the root in
36e40 66 6f 20 66 6f 72 20 74 68 65 20 73 65 67 6d 65  fo for the segme
36e41 6e 74 2e 20 20 49 66 20 74 68 65 20 65 6e 74 69  nt.  If the enti
36e42 72 65 20 6c 65 61 66 20 66 69 74 73 0a 2a 2a 20  re leaf fits.** 
36e43 77 69 74 68 69 6e 20 52 4f 4f 54 5f 4d 41 58 2c  within ROOT_MAX,
36e44 20 74 68 65 6e 20 69 74 20 77 69 6c 6c 20 62 65   then it will be
36e45 20 72 65 74 75 72 6e 65 64 20 64 69 72 65 63 74   returned direct
36e46 6c 79 2c 20 6f 74 68 65 72 77 69 73 65 20 69 74  ly, otherwise it
36e47 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 66 6c 75 73  .** will be flus
36e48 68 65 64 20 61 6e 64 20 74 68 65 20 72 6f 6f 74  hed and the root
36e49 20 69 6e 66 6f 20 77 69 6c 6c 20 62 65 20 72 65   info will be re
36e4a 74 75 72 6e 65 64 20 66 72 6f 6d 20 74 68 65 0a  turned from the.
36e4b 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  ** interior node
36e4c 2e 20 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64  .  *piEndBlockid
36e4d 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 62   is set to the b
36e4e 6c 6f 63 6b 69 64 20 6f 66 20 74 68 65 20 6c 61  lockid of the la
36e4f 73 74 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6f  st.** interior o
36e50 72 20 6c 65 61 66 20 6e 6f 64 65 20 77 72 69 74  r leaf node writ
36e51 74 65 6e 20 74 6f 20 64 69 73 6b 20 28 30 20 69  ten to disk (0 i
36e52 66 20 6e 6f 6e 65 20 61 72 65 20 77 72 69 74 74  f none are writt
36e53 65 6e 20 61 74 0a 2a 2a 20 61 6c 6c 29 2e 0a 2a  en at.** all)..*
36e54 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61  /.static int lea
36e55 66 57 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28  fWriterRootInfo(
36e56 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
36e57 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57  , LeafWriter *pW
36e58 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20  riter,.         
36e59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36e5a 20 20 20 20 20 63 68 61 72 20 2a 2a 70 70 52 6f       char **ppRo
36e5b 6f 74 49 6e 66 6f 2c 20 69 6e 74 20 2a 70 6e 52  otInfo, int *pnR
36e5c 6f 6f 74 49 6e 66 6f 2c 0a 20 20 20 20 20 20 20  ootInfo,.       
36e5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36e5e 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e         sqlite_in
36e5f 74 36 34 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69  t64 *piEndBlocki
36e60 64 29 7b 0a 20 20 2f 2a 20 77 65 20 63 61 6e 20  d){.  /* we can 
36e61 66 69 74 20 74 68 65 20 73 65 67 6d 65 6e 74 20  fit the segment 
36e62 65 6e 74 69 72 65 6c 79 20 69 6e 6c 69 6e 65 20  entirely inline 
36e63 2a 2f 0a 20 20 69 66 28 20 21 70 57 72 69 74 65  */.  if( !pWrite
36e64 72 2d 3e 68 61 73 5f 70 61 72 65 6e 74 20 26 26  r->has_parent &&
36e65 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e   pWriter->data.n
36e66 44 61 74 61 3c 52 4f 4f 54 5f 4d 41 58 20 29 7b  Data<ROOT_MAX ){
36e67 0a 20 20 20 20 2a 70 70 52 6f 6f 74 49 6e 66 6f  .    *ppRootInfo
36e68 20 3d 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61   = pWriter->data
36e69 2e 70 44 61 74 61 3b 0a 20 20 20 20 2a 70 6e 52  .pData;.    *pnR
36e6a 6f 6f 74 49 6e 66 6f 20 3d 20 70 57 72 69 74 65  ootInfo = pWrite
36e6b 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3b 0a 20  r->data.nData;. 
36e6c 20 20 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64     *piEndBlockid
36e6d 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 0;.    return
36e6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
36e6f 0a 20 20 2f 2a 20 46 6c 75 73 68 20 72 65 6d 61  .  /* Flush rema
36e70 69 6e 69 6e 67 20 6c 65 61 66 20 64 61 74 61 2e  ining leaf data.
36e71 20 2a 2f 0a 20 20 69 66 28 20 70 57 72 69 74 65   */.  if( pWrite
36e72 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3e 30 20  r->data.nData>0 
36e73 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20  ){.    int rc = 
36e74 6c 65 61 66 57 72 69 74 65 72 46 6c 75 73 68 28  leafWriterFlush(
36e75 76 2c 20 70 57 72 69 74 65 72 29 3b 0a 20 20 20  v, pWriter);.   
36e76 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
36e77 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
36e78 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 20 6d 75 73    }..  /* We mus
36e79 74 20 68 61 76 65 20 66 6c 75 73 68 65 64 20 61  t have flushed a
36e7a 20 6c 65 61 66 20 61 74 20 73 6f 6d 65 20 70 6f   leaf at some po
36e7b 69 6e 74 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  int. */.  assert
36e7c 28 20 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 70  ( pWriter->has_p
36e7d 61 72 65 6e 74 20 29 3b 0a 0a 20 20 2f 2a 20 54  arent );..  /* T
36e7e 65 6e 61 74 69 76 65 6c 79 20 73 65 74 20 74 68  enatively set th
36e7f 65 20 65 6e 64 20 6c 65 61 66 20 62 6c 6f 63 6b  e end leaf block
36e80 69 64 20 61 73 20 74 68 65 20 65 6e 64 20 62 6c  id as the end bl
36e81 6f 63 6b 69 64 2e 20 20 49 66 20 74 68 65 0a 20  ockid.  If the. 
36e82 20 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64   ** interior nod
36e83 65 20 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65  e can be returne
36e84 64 20 69 6e 6c 69 6e 65 2c 20 74 68 69 73 20 77  d inline, this w
36e85 69 6c 6c 20 62 65 20 74 68 65 20 66 69 6e 61 6c  ill be the final
36e86 0a 20 20 2a 2a 20 62 6c 6f 63 6b 69 64 2c 20 6f  .  ** blockid, o
36e87 74 68 65 72 77 69 73 65 20 69 74 20 77 69 6c 6c  therwise it will
36e88 20 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20   be overwritten 
36e89 62 79 0a 20 20 2a 2a 20 69 6e 74 65 72 69 6f 72  by.  ** interior
36e8a 57 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 29  WriterRootInfo()
36e8b 2e 0a 20 20 2a 2f 0a 20 20 2a 70 69 45 6e 64 42  ..  */.  *piEndB
36e8c 6c 6f 63 6b 69 64 20 3d 20 70 57 72 69 74 65 72  lockid = pWriter
36e8d 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 3b 0a 0a  ->iEndBlockid;..
36e8e 20 20 72 65 74 75 72 6e 20 69 6e 74 65 72 69 6f    return interio
36e8f 72 57 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28  rWriterRootInfo(
36e90 76 2c 20 26 70 57 72 69 74 65 72 2d 3e 70 61 72  v, &pWriter->par
36e91 65 6e 74 57 72 69 74 65 72 2c 0a 20 20 20 20 20  entWriter,.     
36e92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36e93 20 20 20 20 20 20 20 20 20 20 20 70 70 52 6f 6f             ppRoo
36e94 74 49 6e 66 6f 2c 20 70 6e 52 6f 6f 74 49 6e 66  tInfo, pnRootInf
36e95 6f 2c 20 70 69 45 6e 64 42 6c 6f 63 6b 69 64 29  o, piEndBlockid)
36e96 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6c 6c 65 63 74 20  ;.}../* Collect 
36e97 74 68 65 20 72 6f 6f 74 49 6e 66 6f 20 64 61 74  the rootInfo dat
36e98 61 20 61 6e 64 20 73 74 6f 72 65 20 69 74 20 69  a and store it i
36e99 6e 74 6f 20 74 68 65 20 73 65 67 6d 65 6e 74 20  nto the segment 
36e9a 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a 20 54 68  directory..** Th
36e9b 69 73 20 68 61 73 20 74 68 65 20 65 66 66 65 63  is has the effec
36e9c 74 20 6f 66 20 66 6c 75 73 68 69 6e 67 20 74 68  t of flushing th
36e9d 65 20 73 65 67 6d 65 6e 74 27 73 20 6c 65 61 66  e segment's leaf
36e9e 20 64 61 74 61 20 74 6f 0a 2a 2a 20 25 5f 73 65   data to.** %_se
36e9f 67 6d 65 6e 74 73 2c 20 61 6e 64 20 61 6c 73 6f  gments, and also
36ea0 20 66 6c 75 73 68 69 6e 67 20 61 6e 79 20 69 6e   flushing any in
36ea1 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 74 6f 20  terior nodes to 
36ea2 25 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a 2f 0a 73  %_segments..*/.s
36ea3 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57 72  tatic int leafWr
36ea4 69 74 65 72 46 69 6e 61 6c 69 7a 65 28 66 75 6c  iterFinalize(ful
36ea5 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c  ltext_vtab *v, L
36ea6 65 61 66 57 72 69 74 65 72 20 2a 70 57 72 69 74  eafWriter *pWrit
36ea7 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e  er){.  sqlite_in
36ea8 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b 69 64 3b  t64 iEndBlockid;
36ea9 0a 20 20 63 68 61 72 20 2a 70 52 6f 6f 74 49 6e  .  char *pRootIn
36eaa 66 6f 3b 0a 20 20 69 6e 74 20 72 63 2c 20 6e 52  fo;.  int rc, nR
36eab 6f 6f 74 49 6e 66 6f 3b 0a 0a 20 20 72 63 20 3d  ootInfo;..  rc =
36eac 20 6c 65 61 66 57 72 69 74 65 72 52 6f 6f 74 49   leafWriterRootI
36ead 6e 66 6f 28 76 2c 20 70 57 72 69 74 65 72 2c 20  nfo(v, pWriter, 
36eae 26 70 52 6f 6f 74 49 6e 66 6f 2c 20 26 6e 52 6f  &pRootInfo, &nRo
36eaf 6f 74 49 6e 66 6f 2c 20 26 69 45 6e 64 42 6c 6f  otInfo, &iEndBlo
36eb0 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21  ckid);.  if( rc!
36eb1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
36eb2 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f  urn rc;..  /* Do
36eb3 6e 27 74 20 62 6f 74 68 65 72 20 73 74 6f 72 69  n't bother stori
36eb4 6e 67 20 61 6e 20 65 6e 74 69 72 65 6c 79 20 65  ng an entirely e
36eb5 6d 70 74 79 20 73 65 67 6d 65 6e 74 2e 20 2a 2f  mpty segment. */
36eb6 0a 20 20 69 66 28 20 69 45 6e 64 42 6c 6f 63 6b  .  if( iEndBlock
36eb7 69 64 3d 3d 30 20 26 26 20 6e 52 6f 6f 74 49 6e  id==0 && nRootIn
36eb8 66 6f 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53  fo==0 ) return S
36eb9 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 72 65 74  QLITE_OK;..  ret
36eba 75 72 6e 20 73 65 67 64 69 72 5f 73 65 74 28 76  urn segdir_set(v
36ebb 2c 20 70 57 72 69 74 65 72 2d 3e 69 4c 65 76 65  , pWriter->iLeve
36ebc 6c 2c 20 70 57 72 69 74 65 72 2d 3e 69 64 78 2c  l, pWriter->idx,
36ebd 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
36ebe 20 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69 53       pWriter->iS
36ebf 74 61 72 74 42 6c 6f 63 6b 69 64 2c 20 70 57 72  tartBlockid, pWr
36ec0 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69  iter->iEndBlocki
36ec1 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
36ec2 20 20 20 20 20 20 20 69 45 6e 64 42 6c 6f 63 6b         iEndBlock
36ec3 69 64 2c 20 70 52 6f 6f 74 49 6e 66 6f 2c 20 6e  id, pRootInfo, n
36ec4 52 6f 6f 74 49 6e 66 6f 29 3b 0a 7d 0a 0a 73 74  RootInfo);.}..st
36ec5 61 74 69 63 20 76 6f 69 64 20 6c 65 61 66 57 72  atic void leafWr
36ec6 69 74 65 72 44 65 73 74 72 6f 79 28 4c 65 61 66  iterDestroy(Leaf
36ec7 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 29  Writer *pWriter)
36ec8 7b 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d  {.  if( pWriter-
36ec9 3e 68 61 73 5f 70 61 72 65 6e 74 20 29 20 69 6e  >has_parent ) in
36eca 74 65 72 69 6f 72 57 72 69 74 65 72 44 65 73 74  teriorWriterDest
36ecb 72 6f 79 28 26 70 57 72 69 74 65 72 2d 3e 70 61  roy(&pWriter->pa
36ecc 72 65 6e 74 57 72 69 74 65 72 29 3b 0a 20 20 64  rentWriter);.  d
36ecd 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
36ece 28 26 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 29  (&pWriter->term)
36ecf 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  ;.  dataBufferDe
36ed0 73 74 72 6f 79 28 26 70 57 72 69 74 65 72 2d 3e  stroy(&pWriter->
36ed1 64 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 20 45 6e 63  data);.}../* Enc
36ed2 6f 64 65 20 61 20 74 65 72 6d 20 69 6e 74 6f 20  ode a term into 
36ed3 74 68 65 20 6c 65 61 66 57 72 69 74 65 72 2c 20  the leafWriter, 
36ed4 64 65 6c 74 61 2d 65 6e 63 6f 64 69 6e 67 20 61  delta-encoding a
36ed5 73 20 61 70 70 72 6f 70 72 69 61 74 65 2e 0a 2a  s appropriate..*
36ed6 2a 20 52 65 74 75 72 6e 73 20 74 68 65 20 6c 65  * Returns the le
36ed7 6e 67 74 68 20 6f 66 20 74 68 65 20 6e 65 77 20  ngth of the new 
36ed8 74 65 72 6d 20 77 68 69 63 68 20 64 69 73 74 69  term which disti
36ed9 6e 67 75 69 73 68 65 73 20 69 74 20 66 72 6f 6d  nguishes it from
36eda 20 74 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73   the.** previous
36edb 20 74 65 72 6d 2c 20 77 68 69 63 68 20 63 61 6e   term, which can
36edc 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 74 20   be used to set 
36edd 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 20 77 68  nTermDistinct wh
36ede 65 6e 20 61 20 6e 6f 64 65 0a 2a 2a 20 62 6f 75  en a node.** bou
36edf 6e 64 61 72 79 20 69 73 20 63 72 6f 73 73 65 64  ndary is crossed
36ee0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
36ee1 6c 65 61 66 57 72 69 74 65 72 45 6e 63 6f 64 65  leafWriterEncode
36ee2 54 65 72 6d 28 4c 65 61 66 57 72 69 74 65 72 20  Term(LeafWriter 
36ee3 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20  *pWriter,.      
36ee4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ee5 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
36ee6 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74  char *pTerm, int
36ee7 20 6e 54 65 72 6d 29 7b 0a 20 20 63 68 61 72 20   nTerm){.  char 
36ee8 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b 56 41 52  c[VARINT_MAX+VAR
36ee9 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20  INT_MAX];.  int 
36eea 6e 2c 20 6e 50 72 65 66 69 78 20 3d 20 30 3b 0a  n, nPrefix = 0;.
36eeb 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65 72 6d  .  assert( nTerm
36eec 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e  >0 );.  while( n
36eed 50 72 65 66 69 78 3c 70 57 72 69 74 65 72 2d 3e  Prefix<pWriter->
36eee 74 65 72 6d 2e 6e 44 61 74 61 20 26 26 0a 20 20  term.nData &&.  
36eef 20 20 20 20 20 20 20 70 54 65 72 6d 5b 6e 50 72         pTerm[nPr
36ef0 65 66 69 78 5d 3d 3d 70 57 72 69 74 65 72 2d 3e  efix]==pWriter->
36ef1 74 65 72 6d 2e 70 44 61 74 61 5b 6e 50 72 65 66  term.pData[nPref
36ef2 69 78 5d 20 29 7b 0a 20 20 20 20 6e 50 72 65 66  ix] ){.    nPref
36ef3 69 78 2b 2b 3b 0a 20 20 20 20 2f 2a 20 46 61 69  ix++;.    /* Fai
36ef4 6c 69 6e 67 20 74 68 69 73 20 69 6d 70 6c 69 65  ling this implie
36ef5 73 20 74 68 61 74 20 74 68 65 20 74 65 72 6d 73  s that the terms
36ef6 20 77 65 72 65 6e 27 74 20 69 6e 20 6f 72 64 65   weren't in orde
36ef7 72 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  r. */.    assert
36ef8 28 20 6e 50 72 65 66 69 78 3c 6e 54 65 72 6d 20  ( nPrefix<nTerm 
36ef9 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 57  );.  }..  if( pW
36efa 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
36efb 61 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 45  a==0 ){.    /* E
36efc 6e 63 6f 64 65 20 74 68 65 20 6e 6f 64 65 20 68  ncode the node h
36efd 65 61 64 65 72 20 61 6e 64 20 6c 65 61 64 69 6e  eader and leadin
36efe 67 20 74 65 72 6d 20 61 73 3a 0a 20 20 20 20 2a  g term as:.    *
36eff 2a 20 20 76 61 72 69 6e 74 28 30 29 0a 20 20 20  *  varint(0).   
36f00 20 2a 2a 20 20 76 61 72 69 6e 74 28 6e 54 65 72   **  varint(nTer
36f01 6d 29 0a 20 20 20 20 2a 2a 20 20 63 68 61 72 20  m).    **  char 
36f02 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 0a 20 20 20  pTerm[nTerm].   
36f03 20 2a 2f 0a 20 20 20 20 6e 20 3d 20 66 74 73 33   */.    n = fts3
36f04 50 75 74 56 61 72 69 6e 74 28 63 2c 20 27 5c 30  PutVarint(c, '\0
36f05 27 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73  ');.    n += fts
36f06 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20  3PutVarint(c+n, 
36f07 6e 54 65 72 6d 29 3b 0a 20 20 20 20 64 61 74 61  nTerm);.    data
36f08 42 75 66 66 65 72 41 70 70 65 6e 64 32 28 26 70  BufferAppend2(&p
36f09 57 72 69 74 65 72 2d 3e 64 61 74 61 2c 20 63 2c  Writer->data, c,
36f0a 20 6e 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d   n, pTerm, nTerm
36f0b 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
36f0c 2f 2a 20 44 65 6c 74 61 2d 65 6e 63 6f 64 65 20  /* Delta-encode 
36f0d 74 68 65 20 74 65 72 6d 20 61 73 3a 0a 20 20 20  the term as:.   
36f0e 20 2a 2a 20 20 76 61 72 69 6e 74 28 6e 50 72 65   **  varint(nPre
36f0f 66 69 78 29 0a 20 20 20 20 2a 2a 20 20 76 61 72  fix).    **  var
36f10 69 6e 74 28 6e 53 75 66 66 69 78 29 0a 20 20 20  int(nSuffix).   
36f11 20 2a 2a 20 20 63 68 61 72 20 70 54 65 72 6d 53   **  char pTermS
36f12 75 66 66 69 78 5b 6e 53 75 66 66 69 78 5d 0a 20  uffix[nSuffix]. 
36f13 20 20 20 2a 2f 0a 20 20 20 20 6e 20 3d 20 66 74     */.    n = ft
36f14 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20 6e  s3PutVarint(c, n
36f15 50 72 65 66 69 78 29 3b 0a 20 20 20 20 6e 20 2b  Prefix);.    n +
36f16 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28  = fts3PutVarint(
36f17 63 2b 6e 2c 20 6e 54 65 72 6d 2d 6e 50 72 65 66  c+n, nTerm-nPref
36f18 69 78 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66  ix);.    dataBuf
36f19 66 65 72 41 70 70 65 6e 64 32 28 26 70 57 72 69  ferAppend2(&pWri
36f1a 74 65 72 2d 3e 64 61 74 61 2c 20 63 2c 20 6e 2c  ter->data, c, n,
36f1b 20 70 54 65 72 6d 2b 6e 50 72 65 66 69 78 2c 20   pTerm+nPrefix, 
36f1c 6e 54 65 72 6d 2d 6e 50 72 65 66 69 78 29 3b 0a  nTerm-nPrefix);.
36f1d 20 20 7d 0a 20 20 64 61 74 61 42 75 66 66 65 72    }.  dataBuffer
36f1e 52 65 70 6c 61 63 65 28 26 70 57 72 69 74 65 72  Replace(&pWriter
36f1f 2d 3e 74 65 72 6d 2c 20 70 54 65 72 6d 2c 20 6e  ->term, pTerm, n
36f20 54 65 72 6d 29 3b 0a 0a 20 20 72 65 74 75 72 6e  Term);..  return
36f21 20 6e 50 72 65 66 69 78 2b 31 3b 0a 7d 0a 0a 2f   nPrefix+1;.}../
36f22 2a 20 55 73 65 64 20 74 6f 20 61 76 6f 69 64 20  * Used to avoid 
36f23 61 20 6d 65 6d 6d 6f 76 65 20 77 68 65 6e 20 61  a memmove when a
36f24 20 6c 61 72 67 65 20 61 6d 6f 75 6e 74 20 6f 66   large amount of
36f25 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 69 73   doclist data is
36f26 20 69 6e 0a 2a 2a 20 74 68 65 20 62 75 66 66 65   in.** the buffe
36f27 72 2e 20 20 54 68 69 73 20 63 6f 6e 73 74 72 75  r.  This constru
36f28 63 74 73 20 61 20 6e 6f 64 65 20 61 6e 64 20 74  cts a node and t
36f29 65 72 6d 20 68 65 61 64 65 72 20 62 65 66 6f 72  erm header befor
36f2a 65 0a 2a 2a 20 69 44 6f 63 6c 69 73 74 44 61 74  e.** iDoclistDat
36f2b 61 20 61 6e 64 20 66 6c 75 73 68 65 73 20 74 68  a and flushes th
36f2c 65 20 72 65 73 75 6c 74 69 6e 67 20 63 6f 6d 70  e resulting comp
36f2d 6c 65 74 65 20 6e 6f 64 65 20 75 73 69 6e 67 0a  lete node using.
36f2e 2a 2a 20 6c 65 61 66 57 72 69 74 65 72 49 6e 74  ** leafWriterInt
36f2f 65 72 6e 61 6c 46 6c 75 73 68 28 29 2e 0a 2a 2f  ernalFlush()..*/
36f30 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66  .static int leaf
36f31 57 72 69 74 65 72 49 6e 6c 69 6e 65 46 6c 75 73  WriterInlineFlus
36f32 68 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  h(fulltext_vtab 
36f33 2a 76 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a  *v, LeafWriter *
36f34 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20  pWriter,.       
36f35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36f36 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
36f37 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74  char *pTerm, int
36f38 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20 20 20   nTerm,.        
36f39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36f3a 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 6f           int iDo
36f3b 63 6c 69 73 74 44 61 74 61 29 7b 0a 20 20 63 68  clistData){.  ch
36f3c 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b  ar c[VARINT_MAX+
36f3d 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69  VARINT_MAX];.  i
36f3e 6e 74 20 69 44 61 74 61 2c 20 6e 20 3d 20 66 74  nt iData, n = ft
36f3f 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20 30  s3PutVarint(c, 0
36f40 29 3b 0a 20 20 6e 20 2b 3d 20 66 74 73 33 50 75  );.  n += fts3Pu
36f41 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 6e 54 65  tVarint(c+n, nTe
36f42 72 6d 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 72 65  rm);..  /* There
36f43 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62   should always b
36f44 65 20 72 6f 6f 6d 20 66 6f 72 20 74 68 65 20 68  e room for the h
36f45 65 61 64 65 72 2e 20 20 45 76 65 6e 20 69 66 20  eader.  Even if 
36f46 70 54 65 72 6d 20 73 68 61 72 65 64 0a 20 20 2a  pTerm shared.  *
36f47 2a 20 61 20 73 75 62 73 74 61 6e 74 69 61 6c 20  * a substantial 
36f48 70 72 65 66 69 78 20 77 69 74 68 20 74 68 65 20  prefix with the 
36f49 70 72 65 76 69 6f 75 73 20 74 65 72 6d 2c 20 74  previous term, t
36f4a 68 65 20 65 6e 74 69 72 65 20 70 72 65 66 69 78  he entire prefix
36f4b 0a 20 20 2a 2a 20 63 6f 75 6c 64 20 62 65 20 63  .  ** could be c
36f4c 6f 6e 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20  onstructed from 
36f4d 65 61 72 6c 69 65 72 20 64 61 74 61 20 69 6e 20  earlier data in 
36f4e 74 68 65 20 64 6f 63 6c 69 73 74 2c 20 73 6f 20  the doclist, so 
36f4f 74 68 65 72 65 0a 20 20 2a 2a 20 73 68 6f 75 6c  there.  ** shoul
36f50 64 20 62 65 20 72 6f 6f 6d 2e 0a 20 20 2a 2f 0a  d be room..  */.
36f51 20 20 61 73 73 65 72 74 28 20 69 44 6f 63 6c 69    assert( iDocli
36f52 73 74 44 61 74 61 3e 3d 6e 2b 6e 54 65 72 6d 20  stData>=n+nTerm 
36f53 29 3b 0a 0a 20 20 69 44 61 74 61 20 3d 20 69 44  );..  iData = iD
36f54 6f 63 6c 69 73 74 44 61 74 61 2d 28 6e 2b 6e 54  oclistData-(n+nT
36f55 65 72 6d 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70  erm);.  memcpy(p
36f56 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61  Writer->data.pDa
36f57 74 61 2b 69 44 61 74 61 2c 20 63 2c 20 6e 29 3b  ta+iData, c, n);
36f58 0a 20 20 6d 65 6d 63 70 79 28 70 57 72 69 74 65  .  memcpy(pWrite
36f59 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44  r->data.pData+iD
36f5a 61 74 61 2b 6e 2c 20 70 54 65 72 6d 2c 20 6e 54  ata+n, pTerm, nT
36f5b 65 72 6d 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  erm);..  return 
36f5c 6c 65 61 66 57 72 69 74 65 72 49 6e 74 65 72 6e  leafWriterIntern
36f5d 61 6c 46 6c 75 73 68 28 76 2c 20 70 57 72 69 74  alFlush(v, pWrit
36f5e 65 72 2c 20 69 44 61 74 61 2c 20 70 57 72 69 74  er, iData, pWrit
36f5f 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 69  er->data.nData-i
36f60 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 20 50 75 73  Data);.}../* Pus
36f61 68 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 20 61  h pTerm[nTerm] a
36f62 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 64 6f  long with the do
36f63 63 6c 69 73 74 20 64 61 74 61 20 74 6f 20 74 68  clist data to th
36f64 65 20 6c 65 61 66 20 6c 61 79 65 72 20 6f 66 0a  e leaf layer of.
36f65 2a 2a 20 25 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a  ** %_segments..*
36f66 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61  /.static int lea
36f67 66 57 72 69 74 65 72 53 74 65 70 4d 65 72 67 65  fWriterStepMerge
36f68 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
36f69 76 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70  v, LeafWriter *p
36f6a 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20  Writer,.        
36f6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36f6c 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
36f6d 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54  r *pTerm, int nT
36f6e 65 72 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20  erm,.           
36f6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36f70 20 20 20 20 44 4c 52 65 61 64 65 72 20 2a 70 52      DLReader *pR
36f71 65 61 64 65 72 73 2c 20 69 6e 74 20 6e 52 65 61  eaders, int nRea
36f72 64 65 72 73 29 7b 0a 20 20 63 68 61 72 20 63 5b  ders){.  char c[
36f73 56 41 52 49 4e 54 5f 4d 41 58 2b 56 41 52 49 4e  VARINT_MAX+VARIN
36f74 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 69 54  T_MAX];.  int iT
36f75 65 72 6d 44 61 74 61 20 3d 20 70 57 72 69 74 65  ermData = pWrite
36f76 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2c 20 69  r->data.nData, i
36f77 44 6f 63 6c 69 73 74 44 61 74 61 3b 0a 20 20 69  DoclistData;.  i
36f78 6e 74 20 69 2c 20 6e 44 61 74 61 2c 20 6e 2c 20  nt i, nData, n, 
36f79 6e 41 63 74 75 61 6c 44 61 74 61 2c 20 6e 41 63  nActualData, nAc
36f7a 74 75 61 6c 2c 20 72 63 2c 20 6e 54 65 72 6d 44  tual, rc, nTermD
36f7b 69 73 74 69 6e 63 74 3b 0a 0a 20 20 41 53 53 45  istinct;..  ASSE
36f7c 52 54 5f 56 41 4c 49 44 5f 4c 45 41 46 5f 4e 4f  RT_VALID_LEAF_NO
36f7d 44 45 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61  DE(pWriter->data
36f7e 2e 70 44 61 74 61 2c 20 70 57 72 69 74 65 72 2d  .pData, pWriter-
36f7f 3e 64 61 74 61 2e 6e 44 61 74 61 29 3b 0a 20 20  >data.nData);.  
36f80 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 20 3d 20  nTermDistinct = 
36f81 6c 65 61 66 57 72 69 74 65 72 45 6e 63 6f 64 65  leafWriterEncode
36f82 54 65 72 6d 28 70 57 72 69 74 65 72 2c 20 70 54  Term(pWriter, pT
36f83 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20  erm, nTerm);..  
36f84 2f 2a 20 52 65 6d 65 6d 62 65 72 20 6e 54 65 72  /* Remember nTer
36f85 6d 44 69 73 74 69 6e 63 74 20 69 66 20 6f 70 65  mDistinct if ope
36f86 6e 69 6e 67 20 61 20 6e 65 77 20 6e 6f 64 65 2e  ning a new node.
36f87 20 2a 2f 0a 20 20 69 66 28 20 69 54 65 72 6d 44   */.  if( iTermD
36f88 61 74 61 3d 3d 30 20 29 20 70 57 72 69 74 65 72  ata==0 ) pWriter
36f89 2d 3e 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 20  ->nTermDistinct 
36f8a 3d 20 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3b  = nTermDistinct;
36f8b 0a 0a 20 20 69 44 6f 63 6c 69 73 74 44 61 74 61  ..  iDoclistData
36f8c 20 3d 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61   = pWriter->data
36f8d 2e 6e 44 61 74 61 3b 0a 0a 20 20 2f 2a 20 45 73  .nData;..  /* Es
36f8e 74 69 6d 61 74 65 20 74 68 65 20 6c 65 6e 67 74  timate the lengt
36f8f 68 20 6f 66 20 74 68 65 20 6d 65 72 67 65 64 20  h of the merged 
36f90 64 6f 63 6c 69 73 74 20 73 6f 20 77 65 20 63 61  doclist so we ca
36f91 6e 20 6c 65 61 76 65 20 73 70 61 63 65 0a 20 20  n leave space.  
36f92 2a 2a 20 74 6f 20 65 6e 63 6f 64 65 20 69 74 2e  ** to encode it.
36f93 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c  .  */.  for(i=0,
36f94 20 6e 44 61 74 61 3d 30 3b 20 69 3c 6e 52 65 61   nData=0; i<nRea
36f95 64 65 72 73 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ders; i++){.    
36f96 6e 44 61 74 61 20 2b 3d 20 64 6c 72 41 6c 6c 44  nData += dlrAllD
36f97 61 74 61 42 79 74 65 73 28 26 70 52 65 61 64 65  ataBytes(&pReade
36f98 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 6e 20  rs[i]);.  }.  n 
36f99 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28  = fts3PutVarint(
36f9a 63 2c 20 6e 44 61 74 61 29 3b 0a 20 20 64 61 74  c, nData);.  dat
36f9b 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 26 70  aBufferAppend(&p
36f9c 57 72 69 74 65 72 2d 3e 64 61 74 61 2c 20 63 2c  Writer->data, c,
36f9d 20 6e 29 3b 0a 0a 20 20 64 6f 63 4c 69 73 74 4d   n);..  docListM
36f9e 65 72 67 65 28 26 70 57 72 69 74 65 72 2d 3e 64  erge(&pWriter->d
36f9f 61 74 61 2c 20 70 52 65 61 64 65 72 73 2c 20 6e  ata, pReaders, n
36fa0 52 65 61 64 65 72 73 29 3b 0a 20 20 41 53 53 45  Readers);.  ASSE
36fa1 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54  RT_VALID_DOCLIST
36fa2 28 44 4c 5f 44 45 46 41 55 4c 54 2c 0a 20 20 20  (DL_DEFAULT,.   
36fa3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36fa4 20 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74      pWriter->dat
36fa5 61 2e 70 44 61 74 61 2b 69 44 6f 63 6c 69 73 74  a.pData+iDoclist
36fa6 44 61 74 61 2b 6e 2c 0a 20 20 20 20 20 20 20 20  Data+n,.        
36fa7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
36fa8 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61  Writer->data.nDa
36fa9 74 61 2d 69 44 6f 63 6c 69 73 74 44 61 74 61 2d  ta-iDoclistData-
36faa 6e 2c 20 4e 55 4c 4c 29 3b 0a 0a 20 20 2f 2a 20  n, NULL);..  /* 
36fab 54 68 65 20 61 63 74 75 61 6c 20 61 6d 6f 75 6e  The actual amoun
36fac 74 20 6f 66 20 64 6f 63 6c 69 73 74 20 64 61 74  t of doclist dat
36fad 61 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20  a at this point 
36fae 63 6f 75 6c 64 20 62 65 20 73 6d 61 6c 6c 65 72  could be smaller
36faf 0a 20 20 2a 2a 20 74 68 61 6e 20 74 68 65 20 6c  .  ** than the l
36fb0 65 6e 67 74 68 20 77 65 20 65 6e 63 6f 64 65 64  ength we encoded
36fb1 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c  .  Additionally,
36fb2 20 74 68 65 20 73 70 61 63 65 20 72 65 71 75 69   the space requi
36fb3 72 65 64 20 74 6f 0a 20 20 2a 2a 20 65 6e 63 6f  red to.  ** enco
36fb4 64 65 20 74 68 69 73 20 6c 65 6e 67 74 68 20 63  de this length c
36fb5 6f 75 6c 64 20 62 65 20 73 6d 61 6c 6c 65 72 2e  ould be smaller.
36fb6 20 20 46 6f 72 20 73 6d 61 6c 6c 20 64 6f 63 6c    For small docl
36fb7 69 73 74 73 2c 20 74 68 69 73 20 69 73 0a 20 20  ists, this is.  
36fb8 2a 2a 20 6e 6f 74 20 61 20 62 69 67 20 64 65 61  ** not a big dea
36fb9 6c 2c 20 77 65 20 63 61 6e 20 6a 75 73 74 20 75  l, we can just u
36fba 73 65 20 6d 65 6d 6d 6f 76 65 28 29 20 74 6f 20  se memmove() to 
36fbb 61 64 6a 75 73 74 20 74 68 69 6e 67 73 2e 0a 20  adjust things.. 
36fbc 20 2a 2f 0a 20 20 6e 41 63 74 75 61 6c 44 61 74   */.  nActualDat
36fbd 61 20 3d 20 70 57 72 69 74 65 72 2d 3e 64 61 74  a = pWriter->dat
36fbe 61 2e 6e 44 61 74 61 2d 28 69 44 6f 63 6c 69 73  a.nData-(iDoclis
36fbf 74 44 61 74 61 2b 6e 29 3b 0a 20 20 6e 41 63 74  tData+n);.  nAct
36fc0 75 61 6c 20 3d 20 66 74 73 33 50 75 74 56 61 72  ual = fts3PutVar
36fc1 69 6e 74 28 63 2c 20 6e 41 63 74 75 61 6c 44 61  int(c, nActualDa
36fc2 74 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  ta);.  assert( n
36fc3 41 63 74 75 61 6c 44 61 74 61 3c 3d 6e 44 61 74  ActualData<=nDat
36fc4 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  a );.  assert( n
36fc5 41 63 74 75 61 6c 3c 3d 6e 20 29 3b 0a 0a 20 20  Actual<=n );..  
36fc6 2f 2a 20 49 66 20 74 68 65 20 6e 65 77 20 64 6f  /* If the new do
36fc7 63 6c 69 73 74 20 69 73 20 62 69 67 20 65 6e 6f  clist is big eno
36fc8 75 67 68 20 66 6f 72 20 66 6f 72 63 65 20 61 20  ugh for force a 
36fc9 73 74 61 6e 64 61 6c 6f 6e 65 20 6c 65 61 66 0a  standalone leaf.
36fca 20 20 2a 2a 20 6e 6f 64 65 2c 20 77 65 20 63 61    ** node, we ca
36fcb 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6c  n immediately fl
36fcc 75 73 68 20 69 74 20 69 6e 6c 69 6e 65 20 77 69  ush it inline wi
36fcd 74 68 6f 75 74 20 64 6f 69 6e 67 20 74 68 65 0a  thout doing the.
36fce 20 20 2a 2a 20 6d 65 6d 6d 6f 76 65 28 29 2e 0a    ** memmove()..
36fcf 20 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28 73    */.  /* TODO(s
36fd0 68 65 73 73 29 20 54 68 69 73 20 74 65 73 74 20  hess) This test 
36fd1 6d 61 74 63 68 65 73 20 6c 65 61 66 57 72 69 74  matches leafWrit
36fd2 65 72 53 74 65 70 28 29 2c 20 77 68 69 63 68 20  erStep(), which 
36fd3 64 6f 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 74  does this.  ** t
36fd4 65 73 74 20 62 65 66 6f 72 65 20 69 74 20 6b 6e  est before it kn
36fd5 6f 77 73 20 74 68 65 20 63 6f 73 74 20 74 6f 20  ows the cost to 
36fd6 76 61 72 69 6e 74 2d 65 6e 63 6f 64 65 20 74 68  varint-encode th
36fd7 65 20 74 65 72 6d 20 61 6e 64 0a 20 20 2a 2a 20  e term and.  ** 
36fd8 64 6f 63 6c 69 73 74 20 6c 65 6e 67 74 68 73 2e  doclist lengths.
36fd9 20 20 41 74 20 73 6f 6d 65 20 70 6f 69 6e 74 2c    At some point,
36fda 20 63 68 61 6e 67 65 20 74 6f 0a 20 20 2a 2a 20   change to.  ** 
36fdb 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
36fdc 61 74 61 2d 69 54 65 72 6d 44 61 74 61 3e 53 54  ata-iTermData>ST
36fdd 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 2e 0a 20 20  ANDALONE_MIN..  
36fde 2a 2f 0a 20 20 69 66 28 20 6e 54 65 72 6d 2b 6e  */.  if( nTerm+n
36fdf 41 63 74 75 61 6c 44 61 74 61 3e 53 54 41 4e 44  ActualData>STAND
36fe0 41 4c 4f 4e 45 5f 4d 49 4e 20 29 7b 0a 20 20 20  ALONE_MIN ){.   
36fe1 20 2f 2a 20 50 75 73 68 20 6c 65 61 66 20 6e 6f   /* Push leaf no
36fe2 64 65 20 66 72 6f 6d 20 62 65 66 6f 72 65 20 74  de from before t
36fe3 68 69 73 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 20  his term. */.   
36fe4 20 69 66 28 20 69 54 65 72 6d 44 61 74 61 3e 30   if( iTermData>0
36fe5 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c   ){.      rc = l
36fe6 65 61 66 57 72 69 74 65 72 49 6e 74 65 72 6e 61  eafWriterInterna
36fe7 6c 46 6c 75 73 68 28 76 2c 20 70 57 72 69 74 65  lFlush(v, pWrite
36fe8 72 2c 20 30 2c 20 69 54 65 72 6d 44 61 74 61 29  r, 0, iTermData)
36fe9 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
36fea 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
36feb 72 6e 20 72 63 3b 0a 0a 20 20 20 20 20 20 70 57  rn rc;..      pW
36fec 72 69 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74  riter->nTermDist
36fed 69 6e 63 74 20 3d 20 6e 54 65 72 6d 44 69 73 74  inct = nTermDist
36fee 69 6e 63 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  inct;.    }..   
36fef 20 2f 2a 20 46 69 78 20 74 68 65 20 65 6e 63 6f   /* Fix the enco
36ff0 64 65 64 20 64 6f 63 6c 69 73 74 20 6c 65 6e 67  ded doclist leng
36ff1 74 68 2e 20 2a 2f 0a 20 20 20 20 69 44 6f 63 6c  th. */.    iDocl
36ff2 69 73 74 44 61 74 61 20 2b 3d 20 6e 20 2d 20 6e  istData += n - n
36ff3 41 63 74 75 61 6c 3b 0a 20 20 20 20 6d 65 6d 63  Actual;.    memc
36ff4 70 79 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61  py(pWriter->data
36ff5 2e 70 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44  .pData+iDoclistD
36ff6 61 74 61 2c 20 63 2c 20 6e 41 63 74 75 61 6c 29  ata, c, nActual)
36ff7 3b 0a 0a 20 20 20 20 2f 2a 20 50 75 73 68 20 74  ;..    /* Push t
36ff8 68 65 20 73 74 61 6e 64 61 6c 6f 6e 65 20 6c 65  he standalone le
36ff9 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 20 20  af node. */.    
36ffa 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 49  rc = leafWriterI
36ffb 6e 6c 69 6e 65 46 6c 75 73 68 28 76 2c 20 70 57  nlineFlush(v, pW
36ffc 72 69 74 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54  riter, pTerm, nT
36ffd 65 72 6d 2c 20 69 44 6f 63 6c 69 73 74 44 61 74  erm, iDoclistDat
36ffe 61 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  a);.    if( rc!=
36fff 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
37000 72 6e 20 72 63 3b 0a 0a 20 20 20 20 2f 2a 20 4c  rn rc;..    /* L
37001 65 61 76 65 20 74 68 65 20 6e 6f 64 65 20 65 6d  eave the node em
37002 70 74 79 2e 20 2a 2f 0a 20 20 20 20 64 61 74 61  pty. */.    data
37003 42 75 66 66 65 72 52 65 73 65 74 28 26 70 57 72  BufferReset(&pWr
37004 69 74 65 72 2d 3e 64 61 74 61 29 3b 0a 0a 20 20  iter->data);..  
37005 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
37006 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70  ..  /* At this p
37007 6f 69 6e 74 2c 20 77 65 20 6b 6e 6f 77 20 74 68  oint, we know th
37008 61 74 20 74 68 65 20 64 6f 63 6c 69 73 74 20 77  at the doclist w
37009 61 73 20 73 6d 61 6c 6c 2c 20 73 6f 20 64 6f 20  as small, so do 
3700a 74 68 65 0a 20 20 2a 2a 20 6d 65 6d 6d 6f 76 65  the.  ** memmove
3700b 20 69 66 20 69 6e 64 69 63 61 74 65 64 2e 0a 20   if indicated.. 
3700c 20 2a 2f 0a 20 20 69 66 28 20 6e 41 63 74 75 61   */.  if( nActua
3700d 6c 3c 6e 20 29 7b 0a 20 20 20 20 6d 65 6d 6d 6f  l<n ){.    memmo
3700e 76 65 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61  ve(pWriter->data
3700f 2e 70 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44  .pData+iDoclistD
37010 61 74 61 2b 6e 41 63 74 75 61 6c 2c 0a 20 20 20  ata+nActual,.   
37011 20 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72           pWriter
37012 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f  ->data.pData+iDo
37013 63 6c 69 73 74 44 61 74 61 2b 6e 2c 0a 20 20 20  clistData+n,.   
37014 20 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72           pWriter
37015 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 28 69 44  ->data.nData-(iD
37016 6f 63 6c 69 73 74 44 61 74 61 2b 6e 29 29 3b 0a  oclistData+n));.
37017 20 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74      pWriter->dat
37018 61 2e 6e 44 61 74 61 20 2d 3d 20 6e 2d 6e 41 63  a.nData -= n-nAc
37019 74 75 61 6c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  tual;.  }..  /* 
3701a 52 65 70 6c 61 63 65 20 77 72 69 74 74 65 6e 20  Replace written 
3701b 6c 65 6e 67 74 68 20 77 69 74 68 20 61 63 74 75  length with actu
3701c 61 6c 20 6c 65 6e 67 74 68 2e 20 2a 2f 0a 20 20  al length. */.  
3701d 6d 65 6d 63 70 79 28 70 57 72 69 74 65 72 2d 3e  memcpy(pWriter->
3701e 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63 6c  data.pData+iDocl
3701f 69 73 74 44 61 74 61 2c 20 63 2c 20 6e 41 63 74  istData, c, nAct
37020 75 61 6c 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  ual);..  /* If t
37021 68 65 20 6e 6f 64 65 20 69 73 20 74 6f 6f 20 6c  he node is too l
37022 61 72 67 65 2c 20 62 72 65 61 6b 20 74 68 69 6e  arge, break thin
37023 67 73 20 75 70 2e 20 2a 2f 0a 20 20 2f 2a 20 54  gs up. */.  /* T
37024 4f 44 4f 28 73 68 65 73 73 29 20 54 68 69 73 20  ODO(shess) This 
37025 74 65 73 74 20 6d 61 74 63 68 65 73 20 6c 65 61  test matches lea
37026 66 57 72 69 74 65 72 53 74 65 70 28 29 2c 20 77  fWriterStep(), w
37027 68 69 63 68 20 64 6f 65 73 20 74 68 69 73 0a 20  hich does this. 
37028 20 2a 2a 20 74 65 73 74 20 62 65 66 6f 72 65 20   ** test before 
37029 69 74 20 6b 6e 6f 77 73 20 74 68 65 20 63 6f 73  it knows the cos
3702a 74 20 74 6f 20 76 61 72 69 6e 74 2d 65 6e 63 6f  t to varint-enco
3702b 64 65 20 74 68 65 20 74 65 72 6d 20 61 6e 64 0a  de the term and.
3702c 20 20 2a 2a 20 64 6f 63 6c 69 73 74 20 6c 65 6e    ** doclist len
3702d 67 74 68 73 2e 20 20 41 74 20 73 6f 6d 65 20 70  gths.  At some p
3702e 6f 69 6e 74 2c 20 63 68 61 6e 67 65 20 74 6f 0a  oint, change to.
3702f 20 20 2a 2a 20 70 57 72 69 74 65 72 2d 3e 64 61    ** pWriter->da
37030 74 61 2e 6e 44 61 74 61 3e 4c 45 41 46 5f 4d 41  ta.nData>LEAF_MA
37031 58 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 54  X..  */.  if( iT
37032 65 72 6d 44 61 74 61 2b 6e 54 65 72 6d 2b 6e 41  ermData+nTerm+nA
37033 63 74 75 61 6c 44 61 74 61 3e 4c 45 41 46 5f 4d  ctualData>LEAF_M
37034 41 58 20 29 7b 0a 20 20 20 20 2f 2a 20 46 6c 75  AX ){.    /* Flu
37035 73 68 20 6f 75 74 20 74 68 65 20 6c 65 61 64 69  sh out the leadi
37036 6e 67 20 64 61 74 61 20 61 73 20 61 20 6e 6f 64  ng data as a nod
37037 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 6c 65  e */.    rc = le
37038 61 66 57 72 69 74 65 72 49 6e 74 65 72 6e 61 6c  afWriterInternal
37039 46 6c 75 73 68 28 76 2c 20 70 57 72 69 74 65 72  Flush(v, pWriter
3703a 2c 20 30 2c 20 69 54 65 72 6d 44 61 74 61 29 3b  , 0, iTermData);
3703b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
3703c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
3703d 72 63 3b 0a 0a 20 20 20 20 70 57 72 69 74 65 72  rc;..    pWriter
3703e 2d 3e 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 20  ->nTermDistinct 
3703f 3d 20 6e 54 65 72 6d 44 69 73 74 69 6e 63 74 3b  = nTermDistinct;
37040 0a 0a 20 20 20 20 2f 2a 20 52 65 62 75 69 6c 64  ..    /* Rebuild
37041 20 68 65 61 64 65 72 20 75 73 69 6e 67 20 74 68   header using th
37042 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d 20 2a  e current term *
37043 2f 0a 20 20 20 20 6e 20 3d 20 66 74 73 33 50 75  /.    n = fts3Pu
37044 74 56 61 72 69 6e 74 28 70 57 72 69 74 65 72 2d  tVarint(pWriter-
37045 3e 64 61 74 61 2e 70 44 61 74 61 2c 20 30 29 3b  >data.pData, 0);
37046 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75  .    n += fts3Pu
37047 74 56 61 72 69 6e 74 28 70 57 72 69 74 65 72 2d  tVarint(pWriter-
37048 3e 64 61 74 61 2e 70 44 61 74 61 2b 6e 2c 20 6e  >data.pData+n, n
37049 54 65 72 6d 29 3b 0a 20 20 20 20 6d 65 6d 63 70  Term);.    memcp
3704a 79 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e  y(pWriter->data.
3704b 70 44 61 74 61 2b 6e 2c 20 70 54 65 72 6d 2c 20  pData+n, pTerm, 
3704c 6e 54 65 72 6d 29 3b 0a 20 20 20 20 6e 20 2b 3d  nTerm);.    n +=
3704d 20 6e 54 65 72 6d 3b 0a 0a 20 20 20 20 2f 2a 20   nTerm;..    /* 
3704e 54 68 65 72 65 20 73 68 6f 75 6c 64 20 61 6c 77  There should alw
3704f 61 79 73 20 62 65 20 72 6f 6f 6d 2c 20 62 65 63  ays be room, bec
37050 61 75 73 65 20 74 68 65 20 70 72 65 76 69 6f 75  ause the previou
37051 73 20 65 6e 63 6f 64 69 6e 67 0a 20 20 20 20 2a  s encoding.    *
37052 2a 20 69 6e 63 6c 75 64 65 64 20 61 6c 6c 20 64  * included all d
37053 61 74 61 20 6e 65 63 65 73 73 61 72 79 20 74 6f  ata necessary to
37054 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 74   construct the t
37055 65 72 6d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  erm..    */.    
37056 61 73 73 65 72 74 28 20 6e 3c 69 44 6f 63 6c 69  assert( n<iDocli
37057 73 74 44 61 74 61 20 29 3b 0a 20 20 20 20 2f 2a  stData );.    /*
37058 20 53 6f 20 6c 6f 6e 67 20 61 73 20 53 54 41 4e   So long as STAN
37059 44 41 4c 4f 4e 45 5f 4d 49 4e 20 69 73 20 68 61  DALONE_MIN is ha
3705a 6c 66 20 6f 72 20 6c 65 73 73 20 6f 66 20 4c 45  lf or less of LE
3705b 41 46 5f 4d 41 58 2c 20 74 68 65 0a 20 20 20 20  AF_MAX, the.    
3705c 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 65 6d  ** following mem
3705d 63 70 79 28 29 20 69 73 20 73 61 66 65 20 28 61  cpy() is safe (a
3705e 73 20 6f 70 70 6f 73 65 64 20 74 6f 20 6e 65 65  s opposed to nee
3705f 64 69 6e 67 20 61 20 6d 65 6d 6d 6f 76 65 29 2e  ding a memmove).
37060 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
37061 72 74 28 20 32 2a 53 54 41 4e 44 41 4c 4f 4e 45  rt( 2*STANDALONE
37062 5f 4d 49 4e 3c 3d 4c 45 41 46 5f 4d 41 58 20 29  _MIN<=LEAF_MAX )
37063 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b  ;.    assert( n+
37064 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44  pWriter->data.nD
37065 61 74 61 2d 69 44 6f 63 6c 69 73 74 44 61 74 61  ata-iDoclistData
37066 3c 69 44 6f 63 6c 69 73 74 44 61 74 61 20 29 3b  <iDoclistData );
37067 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 57 72 69  .    memcpy(pWri
37068 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b  ter->data.pData+
37069 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 57  n,.           pW
3706a 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74  riter->data.pDat
3706b 61 2b 69 44 6f 63 6c 69 73 74 44 61 74 61 2c 0a  a+iDoclistData,.
3706c 20 20 20 20 20 20 20 20 20 20 20 70 57 72 69 74             pWrit
3706d 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d 69  er->data.nData-i
3706e 44 6f 63 6c 69 73 74 44 61 74 61 29 3b 0a 20 20  DoclistData);.  
3706f 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e    pWriter->data.
37070 6e 44 61 74 61 20 2d 3d 20 69 44 6f 63 6c 69 73  nData -= iDoclis
37071 74 44 61 74 61 2d 6e 3b 0a 20 20 7d 0a 20 20 41  tData-n;.  }.  A
37072 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45 41 46  SSERT_VALID_LEAF
37073 5f 4e 4f 44 45 28 70 57 72 69 74 65 72 2d 3e 64  _NODE(pWriter->d
37074 61 74 61 2e 70 44 61 74 61 2c 20 70 57 72 69 74  ata.pData, pWrit
37075 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 29 3b  er->data.nData);
37076 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
37077 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 50 75 73 68  E_OK;.}../* Push
37078 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 20 61 6c   pTerm[nTerm] al
37079 6f 6e 67 20 77 69 74 68 20 74 68 65 20 64 6f 63  ong with the doc
3707a 6c 69 73 74 20 64 61 74 61 20 74 6f 20 74 68 65  list data to the
3707b 20 6c 65 61 66 20 6c 61 79 65 72 20 6f 66 0a 2a   leaf layer of.*
3707c 2a 20 25 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a 2f  * %_segments..*/
3707d 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
3707e 52 65 76 69 73 65 20 77 72 69 74 65 5a 65 72 6f  Revise writeZero
3707f 53 65 67 6d 65 6e 74 28 29 20 73 6f 20 74 68 61  Segment() so tha
37080 74 20 64 6f 63 6c 69 73 74 73 20 61 72 65 0a 2a  t doclists are.*
37081 2a 20 63 6f 6e 73 74 72 75 63 74 65 64 20 64 69  * constructed di
37082 72 65 63 74 6c 79 20 69 6e 20 70 57 72 69 74 65  rectly in pWrite
37083 72 2d 3e 64 61 74 61 2e 0a 2a 2f 0a 73 74 61 74  r->data..*/.stat
37084 69 63 20 69 6e 74 20 6c 65 61 66 57 72 69 74 65  ic int leafWrite
37085 72 53 74 65 70 28 66 75 6c 6c 74 65 78 74 5f 76  rStep(fulltext_v
37086 74 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74  tab *v, LeafWrit
37087 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20  er *pWriter,.   
37088 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37089 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
3708a 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54  r *pTerm, int nT
3708b 65 72 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20  erm,.           
3708c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
3708d 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61  onst char *pData
3708e 2c 20 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20 20  , int nData){.  
3708f 69 6e 74 20 72 63 3b 0a 20 20 44 4c 52 65 61 64  int rc;.  DLRead
37090 65 72 20 72 65 61 64 65 72 3b 0a 0a 20 20 64 6c  er reader;..  dl
37091 72 49 6e 69 74 28 26 72 65 61 64 65 72 2c 20 44  rInit(&reader, D
37092 4c 5f 44 45 46 41 55 4c 54 2c 20 70 44 61 74 61  L_DEFAULT, pData
37093 2c 20 6e 44 61 74 61 29 3b 0a 20 20 72 63 20 3d  , nData);.  rc =
37094 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 4d   leafWriterStepM
37095 65 72 67 65 28 76 2c 20 70 57 72 69 74 65 72 2c  erge(v, pWriter,
37096 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 26   pTerm, nTerm, &
37097 72 65 61 64 65 72 2c 20 31 29 3b 0a 20 20 64 6c  reader, 1);.  dl
37098 72 44 65 73 74 72 6f 79 28 26 72 65 61 64 65 72  rDestroy(&reader
37099 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  );..  return rc;
3709a 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}.../**********
3709b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3709c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3709d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3709e 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 4c 65 61 66 52  ******/./* LeafR
3709f 65 61 64 65 72 20 69 73 20 75 73 65 64 20 74 6f  eader is used to
370a0 20 69 74 65 72 61 74 65 20 6f 76 65 72 20 61 6e   iterate over an
370a1 20 69 6e 64 69 76 69 64 75 61 6c 20 6c 65 61 66   individual leaf
370a2 20 6e 6f 64 65 2e 20 2a 2f 0a 74 79 70 65 64 65   node. */.typede
370a3 66 20 73 74 72 75 63 74 20 4c 65 61 66 52 65 61  f struct LeafRea
370a4 64 65 72 20 7b 0a 20 20 44 61 74 61 42 75 66 66  der {.  DataBuff
370a5 65 72 20 74 65 72 6d 3b 20 20 20 20 20 20 20 20  er term;        
370a6 20 20 2f 2a 20 63 6f 70 79 20 6f 66 20 63 75 72    /* copy of cur
370a7 72 65 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 0a 20  rent term. */.. 
370a8 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61   const char *pDa
370a9 74 61 3b 20 20 20 20 20 20 20 20 2f 2a 20 64 61  ta;        /* da
370aa 74 61 20 66 6f 72 20 63 75 72 72 65 6e 74 20 74  ta for current t
370ab 65 72 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 44  erm. */.  int nD
370ac 61 74 61 3b 0a 7d 20 4c 65 61 66 52 65 61 64 65  ata;.} LeafReade
370ad 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  r;..static void 
370ae 6c 65 61 66 52 65 61 64 65 72 44 65 73 74 72 6f  leafReaderDestro
370af 79 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52  y(LeafReader *pR
370b0 65 61 64 65 72 29 7b 0a 20 20 64 61 74 61 42 75  eader){.  dataBu
370b1 66 66 65 72 44 65 73 74 72 6f 79 28 26 70 52 65  fferDestroy(&pRe
370b2 61 64 65 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 53  ader->term);.  S
370b3 43 52 41 4d 42 4c 45 28 70 52 65 61 64 65 72 29  CRAMBLE(pReader)
370b4 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
370b5 6c 65 61 66 52 65 61 64 65 72 41 74 45 6e 64 28  leafReaderAtEnd(
370b6 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52 65 61  LeafReader *pRea
370b7 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  der){.  return p
370b8 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 3c 3d 30  Reader->nData<=0
370b9 3b 0a 7d 0a 0a 2f 2a 20 41 63 63 65 73 73 20 74  ;.}../* Access t
370ba 68 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d 2e  he current term.
370bb 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c   */.static int l
370bc 65 61 66 52 65 61 64 65 72 54 65 72 6d 42 79 74  eafReaderTermByt
370bd 65 73 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70  es(LeafReader *p
370be 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72  Reader){.  retur
370bf 6e 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e  n pReader->term.
370c0 6e 44 61 74 61 3b 0a 7d 0a 73 74 61 74 69 63 20  nData;.}.static 
370c1 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 65 61 66  const char *leaf
370c2 52 65 61 64 65 72 54 65 72 6d 28 4c 65 61 66 52  ReaderTerm(LeafR
370c3 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
370c4 0a 20 20 61 73 73 65 72 74 28 20 70 52 65 61 64  .  assert( pRead
370c5 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3e 30  er->term.nData>0
370c6 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65   );.  return pRe
370c7 61 64 65 72 2d 3e 74 65 72 6d 2e 70 44 61 74 61  ader->term.pData
370c8 3b 0a 7d 0a 0a 2f 2a 20 41 63 63 65 73 73 20 74  ;.}../* Access t
370c9 68 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20  he doclist data 
370ca 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
370cb 74 65 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20  term. */.static 
370cc 69 6e 74 20 6c 65 61 66 52 65 61 64 65 72 44 61  int leafReaderDa
370cd 74 61 42 79 74 65 73 28 4c 65 61 66 52 65 61 64  taBytes(LeafRead
370ce 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
370cf 69 6e 74 20 6e 44 61 74 61 3b 0a 20 20 61 73 73  int nData;.  ass
370d0 65 72 74 28 20 70 52 65 61 64 65 72 2d 3e 74 65  ert( pReader->te
370d1 72 6d 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20  rm.nData>0 );.  
370d2 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
370d3 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20  pReader->pData, 
370d4 26 6e 44 61 74 61 29 3b 0a 20 20 72 65 74 75 72  &nData);.  retur
370d5 6e 20 6e 44 61 74 61 3b 0a 7d 0a 73 74 61 74 69  n nData;.}.stati
370d6 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 65  c const char *le
370d7 61 66 52 65 61 64 65 72 44 61 74 61 28 4c 65 61  afReaderData(Lea
370d8 66 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  fReader *pReader
370d9 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6e 44 61 74  ){.  int n, nDat
370da 61 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52 65  a;.  assert( pRe
370db 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61  ader->term.nData
370dc 3e 30 20 29 3b 0a 20 20 6e 20 3d 20 66 74 73 33  >0 );.  n = fts3
370dd 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61  GetVarint32(pRea
370de 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 6e 44 61  der->pData, &nDa
370df 74 61 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52  ta);.  return pR
370e0 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 3b 0a  eader->pData+n;.
370e1 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c  }..static void l
370e2 65 61 66 52 65 61 64 65 72 49 6e 69 74 28 63 6f  eafReaderInit(co
370e3 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c  nst char *pData,
370e4 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20   int nData,.    
370e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
370e6 20 20 20 20 20 20 20 4c 65 61 66 52 65 61 64 65         LeafReade
370e7 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 69  r *pReader){.  i
370e8 6e 74 20 6e 54 65 72 6d 2c 20 6e 3b 0a 0a 20 20  nt nTerm, n;..  
370e9 61 73 73 65 72 74 28 20 6e 44 61 74 61 3e 30 20  assert( nData>0 
370ea 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 61  );.  assert( pDa
370eb 74 61 5b 30 5d 3d 3d 27 5c 30 27 20 29 3b 0a 0a  ta[0]=='\0' );..
370ec 20 20 43 4c 45 41 52 28 70 52 65 61 64 65 72 29    CLEAR(pReader)
370ed 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65  ;..  /* Read the
370ee 20 66 69 72 73 74 20 74 65 72 6d 2c 20 73 6b 69   first term, ski
370ef 70 70 69 6e 67 20 74 68 65 20 68 65 61 64 65 72  pping the header
370f0 20 62 79 74 65 2e 20 2a 2f 0a 20 20 6e 20 3d 20   byte. */.  n = 
370f1 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
370f2 70 44 61 74 61 2b 31 2c 20 26 6e 54 65 72 6d 29  pData+1, &nTerm)
370f3 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e  ;.  dataBufferIn
370f4 69 74 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72  it(&pReader->ter
370f5 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 64 61 74  m, nTerm);.  dat
370f6 61 42 75 66 66 65 72 52 65 70 6c 61 63 65 28 26  aBufferReplace(&
370f7 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2c 20 70  pReader->term, p
370f8 44 61 74 61 2b 31 2b 6e 2c 20 6e 54 65 72 6d 29  Data+1+n, nTerm)
370f9 3b 0a 0a 20 20 2f 2a 20 50 6f 73 69 74 69 6f 6e  ;..  /* Position
370fa 20 61 66 74 65 72 20 74 68 65 20 66 69 72 73 74   after the first
370fb 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 61 73 73 65   term. */.  asse
370fc 72 74 28 20 31 2b 6e 2b 6e 54 65 72 6d 3c 6e 44  rt( 1+n+nTerm<nD
370fd 61 74 61 20 29 3b 0a 20 20 70 52 65 61 64 65 72  ata );.  pReader
370fe 2d 3e 70 44 61 74 61 20 3d 20 70 44 61 74 61 2b  ->pData = pData+
370ff 31 2b 6e 2b 6e 54 65 72 6d 3b 0a 20 20 70 52 65  1+n+nTerm;.  pRe
37100 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20 6e 44  ader->nData = nD
37101 61 74 61 2d 31 2d 6e 2d 6e 54 65 72 6d 3b 0a 7d  ata-1-n-nTerm;.}
37102 0a 0a 2f 2a 20 53 74 65 70 20 74 68 65 20 72 65  ../* Step the re
37103 61 64 65 72 20 66 6f 72 77 61 72 64 20 74 6f 20  ader forward to 
37104 74 68 65 20 6e 65 78 74 20 74 65 72 6d 2e 20 2a  the next term. *
37105 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65  /.static void le
37106 61 66 52 65 61 64 65 72 53 74 65 70 28 4c 65 61  afReaderStep(Lea
37107 66 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  fReader *pReader
37108 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6e 44 61 74  ){.  int n, nDat
37109 61 2c 20 6e 50 72 65 66 69 78 2c 20 6e 53 75 66  a, nPrefix, nSuf
3710a 66 69 78 3b 0a 20 20 61 73 73 65 72 74 28 20 21  fix;.  assert( !
3710b 6c 65 61 66 52 65 61 64 65 72 41 74 45 6e 64 28  leafReaderAtEnd(
3710c 70 52 65 61 64 65 72 29 20 29 3b 0a 0a 20 20 2f  pReader) );..  /
3710d 2a 20 53 6b 69 70 20 70 72 65 76 69 6f 75 73 20  * Skip previous 
3710e 65 6e 74 72 79 27 73 20 64 61 74 61 20 62 6c 6f  entry's data blo
3710f 63 6b 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73  ck. */.  n = fts
37110 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65  3GetVarint32(pRe
37111 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 6e 44  ader->pData, &nD
37112 61 74 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ata);.  assert( 
37113 6e 2b 6e 44 61 74 61 3c 3d 70 52 65 61 64 65 72  n+nData<=pReader
37114 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 70 52 65  ->nData );.  pRe
37115 61 64 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20 6e  ader->pData += n
37116 2b 6e 44 61 74 61 3b 0a 20 20 70 52 65 61 64 65  +nData;.  pReade
37117 72 2d 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b 6e 44  r->nData -= n+nD
37118 61 74 61 3b 0a 0a 20 20 69 66 28 20 21 6c 65 61  ata;..  if( !lea
37119 66 52 65 61 64 65 72 41 74 45 6e 64 28 70 52 65  fReaderAtEnd(pRe
3711a 61 64 65 72 29 20 29 7b 0a 20 20 20 20 2f 2a 20  ader) ){.    /* 
3711b 43 6f 6e 73 74 72 75 63 74 20 74 68 65 20 6e 65  Construct the ne
3711c 77 20 74 65 72 6d 20 75 73 69 6e 67 20 61 20 70  w term using a p
3711d 72 65 66 69 78 20 66 72 6f 6d 20 74 68 65 20 6f  refix from the o
3711e 6c 64 20 74 65 72 6d 20 70 6c 75 73 20 61 0a 20  ld term plus a. 
3711f 20 20 20 2a 2a 20 73 75 66 66 69 78 20 66 72 6f     ** suffix fro
37120 6d 20 74 68 65 20 6c 65 61 66 20 64 61 74 61 2e  m the leaf data.
37121 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 20 3d 20  .    */.    n = 
37122 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
37123 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20  pReader->pData, 
37124 26 6e 50 72 65 66 69 78 29 3b 0a 20 20 20 20 6e  &nPrefix);.    n
37125 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e   += fts3GetVarin
37126 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61  t32(pReader->pDa
37127 74 61 2b 6e 2c 20 26 6e 53 75 66 66 69 78 29 3b  ta+n, &nSuffix);
37128 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 6e  .    assert( n+n
37129 53 75 66 66 69 78 3c 70 52 65 61 64 65 72 2d 3e  Suffix<pReader->
3712a 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 70 52 65  nData );.    pRe
3712b 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61  ader->term.nData
3712c 20 3d 20 6e 50 72 65 66 69 78 3b 0a 20 20 20 20   = nPrefix;.    
3712d 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
3712e 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2c  (&pReader->term,
3712f 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b   pReader->pData+
37130 6e 2c 20 6e 53 75 66 66 69 78 29 3b 0a 0a 20 20  n, nSuffix);..  
37131 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61    pReader->pData
37132 20 2b 3d 20 6e 2b 6e 53 75 66 66 69 78 3b 0a 20   += n+nSuffix;. 
37133 20 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74     pReader->nDat
37134 61 20 2d 3d 20 6e 2b 6e 53 75 66 66 69 78 3b 0a  a -= n+nSuffix;.
37135 20 20 7d 0a 7d 0a 0a 2f 2a 20 73 74 72 63 6d 70    }.}../* strcmp
37136 2d 73 74 79 6c 65 20 63 6f 6d 70 61 72 69 73 6f  -style compariso
37137 6e 20 6f 66 20 70 52 65 61 64 65 72 27 73 20 63  n of pReader's c
37138 75 72 72 65 6e 74 20 74 65 72 6d 20 61 67 61 69  urrent term agai
37139 6e 73 74 20 70 54 65 72 6d 2e 0a 2a 2a 20 49 66  nst pTerm..** If
3713a 20 69 73 50 72 65 66 69 78 2c 20 65 71 75 61 6c   isPrefix, equal
3713b 69 74 79 20 6d 65 61 6e 73 20 65 71 75 61 6c 20  ity means equal 
3713c 74 68 72 6f 75 67 68 20 6e 54 65 72 6d 20 62 79  through nTerm by
3713d 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  tes..*/.static i
3713e 6e 74 20 6c 65 61 66 52 65 61 64 65 72 54 65 72  nt leafReaderTer
3713f 6d 43 6d 70 28 4c 65 61 66 52 65 61 64 65 72 20  mCmp(LeafReader 
37140 2a 70 52 65 61 64 65 72 2c 0a 20 20 20 20 20 20  *pReader,.      
37141 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37142 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
37143 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54  r *pTerm, int nT
37144 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69  erm, int isPrefi
37145 78 29 7b 0a 20 20 69 6e 74 20 63 2c 20 6e 20 3d  x){.  int c, n =
37146 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e   pReader->term.n
37147 44 61 74 61 3c 6e 54 65 72 6d 20 3f 20 70 52 65  Data<nTerm ? pRe
37148 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61  ader->term.nData
37149 20 3a 20 6e 54 65 72 6d 3b 0a 20 20 69 66 28 20   : nTerm;.  if( 
3714a 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  n==0 ){.    if( 
3714b 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44  pReader->term.nD
3714c 61 74 61 3e 30 20 29 20 72 65 74 75 72 6e 20 2d  ata>0 ) return -
3714d 31 3b 0a 20 20 20 20 69 66 28 6e 54 65 72 6d 3e  1;.    if(nTerm>
3714e 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  0 ) return 1;.  
3714f 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
37150 0a 20 20 63 20 3d 20 6d 65 6d 63 6d 70 28 70 52  .  c = memcmp(pR
37151 65 61 64 65 72 2d 3e 74 65 72 6d 2e 70 44 61 74  eader->term.pDat
37152 61 2c 20 70 54 65 72 6d 2c 20 6e 29 3b 0a 20 20  a, pTerm, n);.  
37153 69 66 28 20 63 21 3d 30 20 29 20 72 65 74 75 72  if( c!=0 ) retur
37154 6e 20 63 3b 0a 20 20 69 66 28 20 69 73 50 72 65  n c;.  if( isPre
37155 66 69 78 20 26 26 20 6e 3d 3d 6e 54 65 72 6d 20  fix && n==nTerm 
37156 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 65  ) return 0;.  re
37157 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 74 65  turn pReader->te
37158 72 6d 2e 6e 44 61 74 61 20 2d 20 6e 54 65 72 6d  rm.nData - nTerm
37159 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}.../*********
3715a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3715b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3715c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3715d 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 4c 65 61 76  *******/./* Leav
3715e 65 73 52 65 61 64 65 72 20 77 72 61 70 73 20 4c  esReader wraps L
3715f 65 61 66 52 65 61 64 65 72 20 74 6f 20 61 6c 6c  eafReader to all
37160 6f 77 20 69 74 65 72 61 74 69 6e 67 20 6f 76 65  ow iterating ove
37161 72 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a 20  r the entire.** 
37162 6c 65 61 66 20 6c 61 79 65 72 20 6f 66 20 74 68  leaf layer of th
37163 65 20 74 72 65 65 2e 0a 2a 2f 0a 74 79 70 65 64  e tree..*/.typed
37164 65 66 20 73 74 72 75 63 74 20 4c 65 61 76 65 73  ef struct Leaves
37165 52 65 61 64 65 72 20 7b 0a 20 20 69 6e 74 20 69  Reader {.  int i
37166 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
37167 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 77 69       /* Index wi
37168 74 68 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74  thin the segment
37169 2e 20 2a 2f 0a 0a 20 20 73 71 6c 69 74 65 33 5f  . */..  sqlite3_
3716a 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20 20  stmt *pStmt;    
3716b 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 77    /* Statement w
3716c 65 27 72 65 20 73 74 72 65 61 6d 69 6e 67 20 6c  e're streaming l
3716d 65 61 76 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20  eaves from. */. 
3716e 20 69 6e 74 20 65 6f 66 3b 20 20 20 20 20 20 20   int eof;       
3716f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 77 65             /* we
37170 27 76 65 20 73 65 65 6e 20 53 51 4c 49 54 45 5f  've seen SQLITE_
37171 44 4f 4e 45 20 66 72 6f 6d 20 70 53 74 6d 74 2e  DONE from pStmt.
37172 20 2a 2f 0a 0a 20 20 4c 65 61 66 52 65 61 64 65   */..  LeafReade
37173 72 20 6c 65 61 66 52 65 61 64 65 72 3b 20 20 20  r leafReader;   
37174 20 2f 2a 20 72 65 61 64 65 72 20 66 6f 72 20 74   /* reader for t
37175 68 65 20 63 75 72 72 65 6e 74 20 6c 65 61 66 2e  he current leaf.
37176 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72   */.  DataBuffer
37177 20 72 6f 6f 74 44 61 74 61 3b 20 20 20 20 20 20   rootData;      
37178 2f 2a 20 72 6f 6f 74 20 64 61 74 61 20 66 6f 72  /* root data for
37179 20 69 6e 6c 69 6e 65 2e 20 2a 2f 0a 7d 20 4c 65   inline. */.} Le
3717a 61 76 65 73 52 65 61 64 65 72 3b 0a 0a 2f 2a 20  avesReader;../* 
3717b 41 63 63 65 73 73 20 74 68 65 20 63 75 72 72 65  Access the curre
3717c 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 73 74 61 74  nt term. */.stat
3717d 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61  ic int leavesRea
3717e 64 65 72 54 65 72 6d 42 79 74 65 73 28 4c 65 61  derTermBytes(Lea
3717f 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
37180 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21  er){.  assert( !
37181 70 52 65 61 64 65 72 2d 3e 65 6f 66 20 29 3b 0a  pReader->eof );.
37182 20 20 72 65 74 75 72 6e 20 6c 65 61 66 52 65 61    return leafRea
37183 64 65 72 54 65 72 6d 42 79 74 65 73 28 26 70 52  derTermBytes(&pR
37184 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65  eader->leafReade
37185 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e  r);.}.static con
37186 73 74 20 63 68 61 72 20 2a 6c 65 61 76 65 73 52  st char *leavesR
37187 65 61 64 65 72 54 65 72 6d 28 4c 65 61 76 65 73  eaderTerm(Leaves
37188 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
37189 7b 0a 20 20 61 73 73 65 72 74 28 20 21 70 52 65  {.  assert( !pRe
3718a 61 64 65 72 2d 3e 65 6f 66 20 29 3b 0a 20 20 72  ader->eof );.  r
3718b 65 74 75 72 6e 20 6c 65 61 66 52 65 61 64 65 72  eturn leafReader
3718c 54 65 72 6d 28 26 70 52 65 61 64 65 72 2d 3e 6c  Term(&pReader->l
3718d 65 61 66 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 2f  eafReader);.}../
3718e 2a 20 41 63 63 65 73 73 20 74 68 65 20 64 6f 63  * Access the doc
3718f 6c 69 73 74 20 64 61 74 61 20 66 6f 72 20 74 68  list data for th
37190 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20  e current term. 
37191 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65  */.static int le
37192 61 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79  avesReaderDataBy
37193 74 65 73 28 4c 65 61 76 65 73 52 65 61 64 65 72  tes(LeavesReader
37194 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73   *pReader){.  as
37195 73 65 72 74 28 20 21 70 52 65 61 64 65 72 2d 3e  sert( !pReader->
37196 65 6f 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  eof );.  return 
37197 6c 65 61 66 52 65 61 64 65 72 44 61 74 61 42 79  leafReaderDataBy
37198 74 65 73 28 26 70 52 65 61 64 65 72 2d 3e 6c 65  tes(&pReader->le
37199 61 66 52 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61  afReader);.}.sta
3719a 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
3719b 6c 65 61 76 65 73 52 65 61 64 65 72 44 61 74 61  leavesReaderData
3719c 28 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70  (LeavesReader *p
3719d 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72  Reader){.  asser
3719e 74 28 20 21 70 52 65 61 64 65 72 2d 3e 65 6f 66  t( !pReader->eof
3719f 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61   );.  return lea
371a0 66 52 65 61 64 65 72 44 61 74 61 28 26 70 52 65  fReaderData(&pRe
371a1 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72  ader->leafReader
371a2 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  );.}..static int
371a3 20 6c 65 61 76 65 73 52 65 61 64 65 72 41 74 45   leavesReaderAtE
371a4 6e 64 28 4c 65 61 76 65 73 52 65 61 64 65 72 20  nd(LeavesReader 
371a5 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74  *pReader){.  ret
371a6 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 65 6f 66  urn pReader->eof
371a7 3b 0a 7d 0a 0a 2f 2a 20 6c 6f 61 64 53 65 67 6d  ;.}../* loadSegm
371a8 65 6e 74 4c 65 61 76 65 73 28 29 20 6d 61 79 20  entLeaves() may 
371a9 6e 6f 74 20 72 65 61 64 20 61 6c 6c 20 74 68 65  not read all the
371aa 20 77 61 79 20 74 6f 20 53 51 4c 49 54 45 5f 44   way to SQLITE_D
371ab 4f 4e 45 2c 20 74 68 75 73 0a 2a 2a 20 6c 65 61  ONE, thus.** lea
371ac 76 69 6e 67 20 74 68 65 20 73 74 61 74 65 6d 65  ving the stateme
371ad 6e 74 20 68 61 6e 64 6c 65 20 6f 70 65 6e 2c 20  nt handle open, 
371ae 77 68 69 63 68 20 6c 6f 63 6b 73 20 74 68 65 20  which locks the 
371af 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44  table..*/./* TOD
371b0 4f 28 73 68 65 73 73 29 20 54 68 69 73 20 22 73  O(shess) This "s
371b1 6f 6c 75 74 69 6f 6e 22 20 69 73 20 6e 6f 74 20  olution" is not 
371b2 73 61 74 69 73 66 61 63 74 6f 72 79 2e 20 20 52  satisfactory.  R
371b3 65 61 6c 6c 79 2c 20 74 68 65 72 65 0a 2a 2a 20  eally, there.** 
371b4 73 68 6f 75 6c 64 20 62 65 20 63 68 65 63 6b 2d  should be check-
371b5 69 6e 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20  in function for 
371b6 61 6c 6c 20 73 74 61 74 65 6d 65 6e 74 20 68 61  all statement ha
371b7 6e 64 6c 65 73 20 77 68 69 63 68 0a 2a 2a 20 61  ndles which.** a
371b8 72 72 61 6e 67 65 73 20 74 6f 20 63 61 6c 6c 20  rranges to call 
371b9 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 2e  sqlite3_reset().
371ba 20 20 54 68 69 73 20 6d 6f 73 74 20 6c 69 6b 65    This most like
371bb 6c 79 20 77 69 6c 6c 20 72 65 71 75 69 72 65 0a  ly will require.
371bc 2a 2a 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20  ** modification 
371bd 74 6f 20 63 6f 6e 74 72 6f 6c 20 66 6c 6f 77 20  to control flow 
371be 61 6c 6c 20 6f 76 65 72 20 74 68 65 20 70 6c 61  all over the pla
371bf 63 65 2c 20 74 68 6f 75 67 68 2c 20 73 6f 20 66  ce, though, so f
371c0 6f 72 20 6e 6f 77 0a 2a 2a 20 6a 75 73 74 20 70  or now.** just p
371c1 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  unt..**.** Note 
371c2 74 68 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  the the current 
371c3 73 79 73 74 65 6d 20 61 73 73 75 6d 65 73 20 74  system assumes t
371c4 68 61 74 20 73 65 67 6d 65 6e 74 20 6d 65 72 67  hat segment merg
371c5 65 73 20 77 69 6c 6c 20 72 75 6e 20 74 6f 0a 2a  es will run to.*
371c6 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 2c 20 77 68  * completion, wh
371c7 69 63 68 20 69 73 20 77 68 79 20 74 68 69 73 20  ich is why this 
371c8 70 61 72 74 69 63 75 6c 61 72 20 70 72 6f 62 61  particular proba
371c9 62 6c 79 20 68 61 73 6e 27 74 20 61 72 69 73 65  bly hasn't arise
371ca 6e 20 69 6e 0a 2a 2a 20 74 68 69 73 20 63 61 73  n in.** this cas
371cb 65 2e 20 20 50 72 6f 62 61 62 6c 79 20 61 20 62  e.  Probably a b
371cc 72 69 74 74 6c 65 20 61 73 73 75 6d 70 74 69 6f  rittle assumptio
371cd 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
371ce 20 6c 65 61 76 65 73 52 65 61 64 65 72 52 65 73   leavesReaderRes
371cf 65 74 28 4c 65 61 76 65 73 52 65 61 64 65 72 20  et(LeavesReader 
371d0 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74  *pReader){.  ret
371d1 75 72 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 65  urn sqlite3_rese
371d2 74 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74  t(pReader->pStmt
371d3 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  );.}..static voi
371d4 64 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 65  d leavesReaderDe
371d5 73 74 72 6f 79 28 4c 65 61 76 65 73 52 65 61 64  stroy(LeavesRead
371d6 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
371d7 2f 2a 20 49 66 20 69 64 78 20 69 73 20 2d 31 2c  /* If idx is -1,
371d8 20 74 68 61 74 20 6d 65 61 6e 73 20 77 65 27 72   that means we'r
371d9 65 20 75 73 69 6e 67 20 61 20 6e 6f 6e 2d 63 61  e using a non-ca
371da 63 68 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 20  ched statement. 
371db 20 2a 2a 20 68 61 6e 64 6c 65 20 69 6e 20 74 68   ** handle in th
371dc 65 20 6f 70 74 69 6d 69 7a 65 28 29 20 63 61 73  e optimize() cas
371dd 65 2c 20 73 6f 20 77 65 20 6e 65 65 64 20 74 6f  e, so we need to
371de 20 72 65 6c 65 61 73 65 20 69 74 2e 0a 20 20 2a   release it..  *
371df 2f 0a 20 20 69 66 28 20 70 52 65 61 64 65 72 2d  /.  if( pReader-
371e0 3e 70 53 74 6d 74 21 3d 4e 55 4c 4c 20 26 26 20  >pStmt!=NULL && 
371e1 70 52 65 61 64 65 72 2d 3e 69 64 78 3d 3d 2d 31  pReader->idx==-1
371e2 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
371e3 66 69 6e 61 6c 69 7a 65 28 70 52 65 61 64 65 72  finalize(pReader
371e4 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 7d 0a 20 20  ->pStmt);.  }.  
371e5 6c 65 61 66 52 65 61 64 65 72 44 65 73 74 72 6f  leafReaderDestro
371e6 79 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66  y(&pReader->leaf
371e7 52 65 61 64 65 72 29 3b 0a 20 20 64 61 74 61 42  Reader);.  dataB
371e8 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 70 52  ufferDestroy(&pR
371e9 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61 29  eader->rootData)
371ea 3b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 52 65  ;.  SCRAMBLE(pRe
371eb 61 64 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 49 6e 69  ader);.}../* Ini
371ec 74 69 61 6c 69 7a 65 20 70 52 65 61 64 65 72 20  tialize pReader 
371ed 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20 72  with the given r
371ee 6f 6f 74 20 64 61 74 61 20 28 69 66 20 69 53 74  oot data (if iSt
371ef 61 72 74 42 6c 6f 63 6b 69 64 3d 3d 30 0a 2a 2a  artBlockid==0.**
371f0 20 74 68 65 20 6c 65 61 66 20 64 61 74 61 20 77   the leaf data w
371f1 61 73 20 65 6e 74 69 72 65 6c 79 20 63 6f 6e 74  as entirely cont
371f2 61 69 6e 65 64 20 69 6e 20 74 68 65 20 72 6f 6f  ained in the roo
371f3 74 29 2c 20 6f 72 20 66 72 6f 6d 20 74 68 65 0a  t), or from the.
371f4 2a 2a 20 73 74 72 65 61 6d 20 6f 66 20 62 6c 6f  ** stream of blo
371f5 63 6b 73 20 62 65 74 77 65 65 6e 20 69 53 74 61  cks between iSta
371f6 72 74 42 6c 6f 63 6b 69 64 20 61 6e 64 20 69 45  rtBlockid and iE
371f7 6e 64 42 6c 6f 63 6b 69 64 2c 20 69 6e 63 6c 75  ndBlockid, inclu
371f8 73 69 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  sive..*/.static 
371f9 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72  int leavesReader
371fa 49 6e 69 74 28 66 75 6c 6c 74 65 78 74 5f 76 74  Init(fulltext_vt
371fb 61 62 20 2a 76 2c 0a 20 20 20 20 20 20 20 20 20  ab *v,.         
371fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
371fd 20 20 20 69 6e 74 20 69 64 78 2c 0a 20 20 20 20     int idx,.    
371fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
371ff 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69          sqlite_i
37200 6e 74 36 34 20 69 53 74 61 72 74 42 6c 6f 63 6b  nt64 iStartBlock
37201 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  id,.            
37202 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37203 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e  sqlite_int64 iEn
37204 64 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20  dBlockid,.      
37205 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37206 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
37207 20 2a 70 52 6f 6f 74 44 61 74 61 2c 20 69 6e 74   *pRootData, int
37208 20 6e 52 6f 6f 74 44 61 74 61 2c 0a 20 20 20 20   nRootData,.    
37209 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3720a 20 20 20 20 20 20 20 20 4c 65 61 76 65 73 52 65          LeavesRe
3720b 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
3720c 20 20 43 4c 45 41 52 28 70 52 65 61 64 65 72 29    CLEAR(pReader)
3720d 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 64 78  ;.  pReader->idx
3720e 20 3d 20 69 64 78 3b 0a 0a 20 20 64 61 74 61 42   = idx;..  dataB
3720f 75 66 66 65 72 49 6e 69 74 28 26 70 52 65 61 64  ufferInit(&pRead
37210 65 72 2d 3e 72 6f 6f 74 44 61 74 61 2c 20 30 29  er->rootData, 0)
37211 3b 0a 20 20 69 66 28 20 69 53 74 61 72 74 42 6c  ;.  if( iStartBl
37212 6f 63 6b 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20  ockid==0 ){.    
37213 2f 2a 20 45 6e 74 69 72 65 20 6c 65 61 66 20 6c  /* Entire leaf l
37214 65 76 65 6c 20 66 69 74 20 69 6e 20 72 6f 6f 74  evel fit in root
37215 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20 64 61   data. */.    da
37216 74 61 42 75 66 66 65 72 52 65 70 6c 61 63 65 28  taBufferReplace(
37217 26 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61  &pReader->rootDa
37218 74 61 2c 20 70 52 6f 6f 74 44 61 74 61 2c 20 6e  ta, pRootData, n
37219 52 6f 6f 74 44 61 74 61 29 3b 0a 20 20 20 20 6c  RootData);.    l
3721a 65 61 66 52 65 61 64 65 72 49 6e 69 74 28 70 52  eafReaderInit(pR
3721b 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61 2e  eader->rootData.
3721c 70 44 61 74 61 2c 20 70 52 65 61 64 65 72 2d 3e  pData, pReader->
3721d 72 6f 6f 74 44 61 74 61 2e 6e 44 61 74 61 2c 0a  rootData.nData,.
3721e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3721f 20 20 20 26 70 52 65 61 64 65 72 2d 3e 6c 65 61     &pReader->lea
37220 66 52 65 61 64 65 72 29 3b 0a 20 20 7d 65 6c 73  fReader);.  }els
37221 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  e{.    sqlite3_s
37222 74 6d 74 20 2a 73 3b 0a 20 20 20 20 69 6e 74 20  tmt *s;.    int 
37223 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 6c 65 61  rc = sql_get_lea
37224 66 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 69  f_statement(v, i
37225 64 78 2c 20 26 73 29 3b 0a 20 20 20 20 69 66 28  dx, &s);.    if(
37226 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
37227 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20   return rc;..   
37228 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
37229 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31 2c 20 69  nd_int64(s, 1, i
3722a 53 74 61 72 74 42 6c 6f 63 6b 69 64 29 3b 0a 20  StartBlockid);. 
3722b 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
3722c 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
3722d 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ;..    rc = sqli
3722e 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73  te3_bind_int64(s
3722f 2c 20 32 2c 20 69 45 6e 64 42 6c 6f 63 6b 69 64  , 2, iEndBlockid
37230 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
37231 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
37232 6e 20 72 63 3b 0a 0a 20 20 20 20 72 63 20 3d 20  n rc;..    rc = 
37233 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b  sqlite3_step(s);
37234 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
37235 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20  ITE_DONE ){.    
37236 20 20 70 52 65 61 64 65 72 2d 3e 65 6f 66 20 3d    pReader->eof =
37237 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   1;.      return
37238 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
37239 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  }.    if( rc!=SQ
3723a 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72  LITE_ROW ) retur
3723b 6e 20 72 63 3b 0a 0a 20 20 20 20 70 52 65 61 64  n rc;..    pRead
3723c 65 72 2d 3e 70 53 74 6d 74 20 3d 20 73 3b 0a 20  er->pStmt = s;. 
3723d 20 20 20 6c 65 61 66 52 65 61 64 65 72 49 6e 69     leafReaderIni
3723e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t(sqlite3_column
3723f 5f 62 6c 6f 62 28 70 52 65 61 64 65 72 2d 3e 70  _blob(pReader->p
37240 53 74 6d 74 2c 20 30 29 2c 0a 20 20 20 20 20 20  Stmt, 0),.      
37241 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
37242 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
37243 73 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74  s(pReader->pStmt
37244 2c 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20  , 0),.          
37245 20 20 20 20 20 20 20 20 20 26 70 52 65 61 64 65           &pReade
37246 72 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a  r->leafReader);.
37247 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
37248 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 53 74  ITE_OK;.}../* St
37249 65 70 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c  ep the current l
3724a 65 61 66 20 66 6f 72 77 61 72 64 20 74 6f 20 74  eaf forward to t
3724b 68 65 20 6e 65 78 74 20 74 65 72 6d 2e 20 20 49  he next term.  I
3724c 66 20 77 65 20 72 65 61 63 68 20 74 68 65 0a 2a  f we reach the.*
3724d 2a 20 65 6e 64 20 6f 66 20 74 68 65 20 63 75 72  * end of the cur
3724e 72 65 6e 74 20 6c 65 61 66 2c 20 73 74 65 70 20  rent leaf, step 
3724f 66 6f 72 77 61 72 64 20 74 6f 20 74 68 65 20 6e  forward to the n
37250 65 78 74 20 6c 65 61 66 20 62 6c 6f 63 6b 2e 0a  ext leaf block..
37251 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65  */.static int le
37252 61 76 65 73 52 65 61 64 65 72 53 74 65 70 28 66  avesReaderStep(f
37253 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
37254 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70   LeavesReader *p
37255 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72  Reader){.  asser
37256 74 28 20 21 6c 65 61 76 65 73 52 65 61 64 65 72  t( !leavesReader
37257 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29  AtEnd(pReader) )
37258 3b 0a 20 20 6c 65 61 66 52 65 61 64 65 72 53 74  ;.  leafReaderSt
37259 65 70 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61  ep(&pReader->lea
3725a 66 52 65 61 64 65 72 29 3b 0a 0a 20 20 69 66 28  fReader);..  if(
3725b 20 6c 65 61 66 52 65 61 64 65 72 41 74 45 6e 64   leafReaderAtEnd
3725c 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52  (&pReader->leafR
3725d 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20 69 6e  eader) ){.    in
3725e 74 20 72 63 3b 0a 20 20 20 20 69 66 28 20 70 52  t rc;.    if( pR
3725f 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61 2e  eader->rootData.
37260 70 44 61 74 61 20 29 7b 0a 20 20 20 20 20 20 70  pData ){.      p
37261 52 65 61 64 65 72 2d 3e 65 6f 66 20 3d 20 31 3b  Reader->eof = 1;
37262 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
37263 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
37264 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
37265 73 74 65 70 28 70 52 65 61 64 65 72 2d 3e 70 53  step(pReader->pS
37266 74 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  tmt);.    if( rc
37267 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  !=SQLITE_ROW ){.
37268 20 20 20 20 20 20 70 52 65 61 64 65 72 2d 3e 65        pReader->e
37269 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65  of = 1;.      re
3726a 74 75 72 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f  turn rc==SQLITE_
3726b 44 4f 4e 45 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  DONE ? SQLITE_OK
3726c 20 3a 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20   : rc;.    }.   
3726d 20 6c 65 61 66 52 65 61 64 65 72 44 65 73 74 72   leafReaderDestr
3726e 6f 79 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61  oy(&pReader->lea
3726f 66 52 65 61 64 65 72 29 3b 0a 20 20 20 20 6c 65  fReader);.    le
37270 61 66 52 65 61 64 65 72 49 6e 69 74 28 73 71 6c  afReaderInit(sql
37271 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
37272 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 2c  (pReader->pStmt,
37273 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20   0),.           
37274 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
37275 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 52 65  column_bytes(pRe
37276 61 64 65 72 2d 3e 70 53 74 6d 74 2c 20 30 29 2c  ader->pStmt, 0),
37277 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
37278 20 20 20 20 26 70 52 65 61 64 65 72 2d 3e 6c 65      &pReader->le
37279 61 66 52 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20  afReader);.  }. 
3727a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3727b 4b 3b 0a 7d 0a 0a 2f 2a 20 4f 72 64 65 72 20 4c  K;.}../* Order L
3727c 65 61 76 65 73 52 65 61 64 65 72 73 20 62 79 20  eavesReaders by 
3727d 74 68 65 69 72 20 74 65 72 6d 2c 20 69 67 6e 6f  their term, igno
3727e 72 69 6e 67 20 69 64 78 2e 20 20 52 65 61 64 65  ring idx.  Reade
3727f 72 73 20 61 74 20 65 6f 66 0a 2a 2a 20 61 6c 77  rs at eof.** alw
37280 61 79 73 20 73 6f 72 74 20 74 6f 20 74 68 65 20  ays sort to the 
37281 65 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  end..*/.static i
37282 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 54  nt leavesReaderT
37283 65 72 6d 43 6d 70 28 4c 65 61 76 65 73 52 65 61  ermCmp(LeavesRea
37284 64 65 72 20 2a 6c 72 31 2c 20 4c 65 61 76 65 73  der *lr1, Leaves
37285 52 65 61 64 65 72 20 2a 6c 72 32 29 7b 0a 20 20  Reader *lr2){.  
37286 69 66 28 20 6c 65 61 76 65 73 52 65 61 64 65 72  if( leavesReader
37287 41 74 45 6e 64 28 6c 72 31 29 20 29 7b 0a 20 20  AtEnd(lr1) ){.  
37288 20 20 69 66 28 20 6c 65 61 76 65 73 52 65 61 64    if( leavesRead
37289 65 72 41 74 45 6e 64 28 6c 72 32 29 20 29 20 72  erAtEnd(lr2) ) r
3728a 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 72 65 74  eturn 0;.    ret
3728b 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28  urn 1;.  }.  if(
3728c 20 6c 65 61 76 65 73 52 65 61 64 65 72 41 74 45   leavesReaderAtE
3728d 6e 64 28 6c 72 32 29 20 29 20 72 65 74 75 72 6e  nd(lr2) ) return
3728e 20 2d 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6c   -1;..  return l
3728f 65 61 66 52 65 61 64 65 72 54 65 72 6d 43 6d 70  eafReaderTermCmp
37290 28 26 6c 72 31 2d 3e 6c 65 61 66 52 65 61 64 65  (&lr1->leafReade
37291 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
37292 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 65                le
37293 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 6c  avesReaderTerm(l
37294 72 32 29 2c 20 6c 65 61 76 65 73 52 65 61 64 65  r2), leavesReade
37295 72 54 65 72 6d 42 79 74 65 73 28 6c 72 32 29 2c  rTermBytes(lr2),
37296 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
37297 20 20 20 20 20 20 20 20 20 20 20 20 30 29 3b 0a              0);.
37298 7d 0a 0a 2f 2a 20 53 69 6d 69 6c 61 72 20 74 6f  }../* Similar to
37299 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72   leavesReaderTer
3729a 6d 43 6d 70 28 29 2c 20 77 69 74 68 20 61 64 64  mCmp(), with add
3729b 69 74 69 6f 6e 61 6c 20 6f 72 64 65 72 69 6e 67  itional ordering
3729c 20 62 79 20 69 64 78 0a 2a 2a 20 73 6f 20 74 68   by idx.** so th
3729d 61 74 20 6f 6c 64 65 72 20 73 65 67 6d 65 6e 74  at older segment
3729e 73 20 73 6f 72 74 20 62 65 66 6f 72 65 20 6e 65  s sort before ne
3729f 77 65 72 20 73 65 67 6d 65 6e 74 73 2e 0a 2a 2f  wer segments..*/
372a0 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76  .static int leav
372a1 65 73 52 65 61 64 65 72 43 6d 70 28 4c 65 61 76  esReaderCmp(Leav
372a2 65 73 52 65 61 64 65 72 20 2a 6c 72 31 2c 20 4c  esReader *lr1, L
372a3 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72 32  eavesReader *lr2
372a4 29 7b 0a 20 20 69 6e 74 20 63 20 3d 20 6c 65 61  ){.  int c = lea
372a5 76 65 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70  vesReaderTermCmp
372a6 28 6c 72 31 2c 20 6c 72 32 29 3b 0a 20 20 69 66  (lr1, lr2);.  if
372a7 28 20 63 21 3d 30 20 29 20 72 65 74 75 72 6e 20  ( c!=0 ) return 
372a8 63 3b 0a 20 20 72 65 74 75 72 6e 20 6c 72 31 2d  c;.  return lr1-
372a9 3e 69 64 78 2d 6c 72 32 2d 3e 69 64 78 3b 0a 7d  >idx-lr2->idx;.}
372aa 0a 0a 2f 2a 20 41 73 73 75 6d 65 20 74 68 61 74  ../* Assume that
372ab 20 70 4c 72 5b 31 5d 2e 2e 70 4c 72 5b 6e 4c 72   pLr[1]..pLr[nLr
372ac 5d 20 61 72 65 20 73 6f 72 74 65 64 2e 20 20 42  ] are sorted.  B
372ad 75 62 62 6c 65 20 70 4c 72 5b 30 5d 20 69 6e 74  ubble pLr[0] int
372ae 6f 20 69 74 73 0a 2a 2a 20 73 6f 72 74 65 64 20  o its.** sorted 
372af 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  position..*/.sta
372b0 74 69 63 20 76 6f 69 64 20 6c 65 61 76 65 73 52  tic void leavesR
372b1 65 61 64 65 72 52 65 6f 72 64 65 72 28 4c 65 61  eaderReorder(Lea
372b2 76 65 73 52 65 61 64 65 72 20 2a 70 4c 72 2c 20  vesReader *pLr, 
372b3 69 6e 74 20 6e 4c 72 29 7b 0a 20 20 77 68 69 6c  int nLr){.  whil
372b4 65 28 20 6e 4c 72 3e 31 20 26 26 20 6c 65 61 76  e( nLr>1 && leav
372b5 65 73 52 65 61 64 65 72 43 6d 70 28 70 4c 72 2c  esReaderCmp(pLr,
372b6 20 70 4c 72 2b 31 29 3e 30 20 29 7b 0a 20 20 20   pLr+1)>0 ){.   
372b7 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 74 6d   LeavesReader tm
372b8 70 20 3d 20 70 4c 72 5b 30 5d 3b 0a 20 20 20 20  p = pLr[0];.    
372b9 70 4c 72 5b 30 5d 20 3d 20 70 4c 72 5b 31 5d 3b  pLr[0] = pLr[1];
372ba 0a 20 20 20 20 70 4c 72 5b 31 5d 20 3d 20 74 6d  .    pLr[1] = tm
372bb 70 3b 0a 20 20 20 20 6e 4c 72 2d 2d 3b 0a 20 20  p;.    nLr--;.  
372bc 20 20 70 4c 72 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a    pLr++;.  }.}..
372bd 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 73 20 70  /* Initializes p
372be 52 65 61 64 65 72 73 20 77 69 74 68 20 74 68 65  Readers with the
372bf 20 73 65 67 6d 65 6e 74 73 20 66 72 6f 6d 20 6c   segments from l
372c0 65 76 65 6c 20 69 4c 65 76 65 6c 2c 20 72 65 74  evel iLevel, ret
372c1 75 72 6e 69 6e 67 0a 2a 2a 20 74 68 65 20 6e 75  urning.** the nu
372c2 6d 62 65 72 20 6f 66 20 73 65 67 6d 65 6e 74 73  mber of segments
372c3 20 69 6e 20 2a 70 69 52 65 61 64 65 72 73 2e 20   in *piReaders. 
372c4 20 4c 65 61 76 65 73 20 70 52 65 61 64 65 72 73   Leaves pReaders
372c5 20 69 6e 20 73 6f 72 74 65 64 0a 2a 2a 20 6f 72   in sorted.** or
372c6 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  der..*/.static i
372c7 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 73  nt leavesReaders
372c8 49 6e 69 74 28 66 75 6c 6c 74 65 78 74 5f 76 74  Init(fulltext_vt
372c9 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65  ab *v, int iLeve
372ca 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  l,.             
372cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
372cc 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52  LeavesReader *pR
372cd 65 61 64 65 72 73 2c 20 69 6e 74 20 2a 70 69 52  eaders, int *piR
372ce 65 61 64 65 72 73 29 7b 0a 20 20 73 71 6c 69 74  eaders){.  sqlit
372cf 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e  e3_stmt *s;.  in
372d0 74 20 69 2c 20 72 63 20 3d 20 73 71 6c 5f 67 65  t i, rc = sql_ge
372d1 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 53  t_statement(v, S
372d2 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 4c 45 56  EGDIR_SELECT_LEV
372d3 45 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20  EL_STMT, &s);.  
372d4 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
372d5 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
372d6 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
372d7 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20 69 4c  ind_int(s, 1, iL
372d8 65 76 65 6c 29 3b 0a 20 20 69 66 28 20 72 63 21  evel);.  if( rc!
372d9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
372da 75 72 6e 20 72 63 3b 0a 0a 20 20 69 20 3d 20 30  urn rc;..  i = 0
372db 3b 0a 20 20 77 68 69 6c 65 28 20 28 72 63 20 3d  ;.  while( (rc =
372dc 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
372dd 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  )==SQLITE_ROW ){
372de 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36  .    sqlite_int6
372df 34 20 69 53 74 61 72 74 20 3d 20 73 71 6c 69 74  4 iStart = sqlit
372e0 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28  e3_column_int64(
372e1 73 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  s, 0);.    sqlit
372e2 65 5f 69 6e 74 36 34 20 69 45 6e 64 20 3d 20 73  e_int64 iEnd = s
372e3 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
372e4 74 36 34 28 73 2c 20 31 29 3b 0a 20 20 20 20 63  t64(s, 1);.    c
372e5 6f 6e 73 74 20 63 68 61 72 20 2a 70 52 6f 6f 74  onst char *pRoot
372e6 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 63  Data = sqlite3_c
372e7 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 2c 20 32 29  olumn_blob(s, 2)
372e8 3b 0a 20 20 20 20 69 6e 74 20 6e 52 6f 6f 74 44  ;.    int nRootD
372e9 61 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  ata = sqlite3_co
372ea 6c 75 6d 6e 5f 62 79 74 65 73 28 73 2c 20 32 29  lumn_bytes(s, 2)
372eb 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  ;..    assert( i
372ec 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29 3b 0a  <MERGE_COUNT );.
372ed 20 20 20 20 72 63 20 3d 20 6c 65 61 76 65 73 52      rc = leavesR
372ee 65 61 64 65 72 49 6e 69 74 28 76 2c 20 69 2c 20  eaderInit(v, i, 
372ef 69 53 74 61 72 74 2c 20 69 45 6e 64 2c 20 70 52  iStart, iEnd, pR
372f0 6f 6f 74 44 61 74 61 2c 20 6e 52 6f 6f 74 44 61  ootData, nRootDa
372f1 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ta,.            
372f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70                &p
372f3 52 65 61 64 65 72 73 5b 69 5d 29 3b 0a 20 20 20  Readers[i]);.   
372f4 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
372f5 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20  OK ) break;..   
372f6 20 69 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20   i++;.  }.  if( 
372f7 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc!=SQLITE_DONE 
372f8 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d  ){.    while( i-
372f9 2d 3e 30 20 29 7b 0a 20 20 20 20 20 20 6c 65 61  ->0 ){.      lea
372fa 76 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79  vesReaderDestroy
372fb 28 26 70 52 65 61 64 65 72 73 5b 69 5d 29 3b 0a  (&pReaders[i]);.
372fc 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
372fd 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69 52   rc;.  }..  *piR
372fe 65 61 64 65 72 73 20 3d 20 69 3b 0a 0a 20 20 2f  eaders = i;..  /
372ff 2a 20 4c 65 61 76 65 20 6f 75 72 20 72 65 73 75  * Leave our resu
37300 6c 74 73 20 73 6f 72 74 65 64 20 62 79 20 74 65  lts sorted by te
37301 72 6d 2c 20 74 68 65 6e 20 61 67 65 2e 20 2a 2f  rm, then age. */
37302 0a 20 20 77 68 69 6c 65 28 20 69 2d 2d 20 29 7b  .  while( i-- ){
37303 0a 20 20 20 20 6c 65 61 76 65 73 52 65 61 64 65  .    leavesReade
37304 72 52 65 6f 72 64 65 72 28 70 52 65 61 64 65 72  rReorder(pReader
37305 73 2b 69 2c 20 2a 70 69 52 65 61 64 65 72 73 2d  s+i, *piReaders-
37306 69 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  i);.  }.  return
37307 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
37308 2a 20 4d 65 72 67 65 20 64 6f 63 6c 69 73 74 73  * Merge doclists
37309 20 66 72 6f 6d 20 70 52 65 61 64 65 72 73 5b 6e   from pReaders[n
3730a 52 65 61 64 65 72 73 5d 20 69 6e 74 6f 20 61 20  Readers] into a 
3730b 73 69 6e 67 6c 65 20 64 6f 63 6c 69 73 74 2c 20  single doclist, 
3730c 77 68 69 63 68 0a 2a 2a 20 69 73 20 77 72 69 74  which.** is writ
3730d 74 65 6e 20 74 6f 20 70 57 72 69 74 65 72 2e 20  ten to pWriter. 
3730e 20 41 73 73 75 6d 65 73 20 70 52 65 61 64 65 72   Assumes pReader
3730f 73 20 69 73 20 6f 72 64 65 72 65 64 20 6f 6c 64  s is ordered old
37310 65 73 74 20 74 6f 0a 2a 2a 20 6e 65 77 65 73 74  est to.** newest
37311 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ..*/./* TODO(she
37312 73 73 29 20 43 6f 6e 73 69 64 65 72 20 70 75 74  ss) Consider put
37313 74 69 6e 67 20 74 68 69 73 20 69 6e 6c 69 6e 65  ting this inline
37314 20 69 6e 20 73 65 67 6d 65 6e 74 4d 65 72 67 65   in segmentMerge
37315 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  (). */.static in
37316 74 20 6c 65 61 76 65 73 52 65 61 64 65 72 73 4d  t leavesReadersM
37317 65 72 67 65 28 66 75 6c 6c 74 65 78 74 5f 76 74  erge(fulltext_vt
37318 61 62 20 2a 76 2c 0a 20 20 20 20 20 20 20 20 20  ab *v,.         
37319 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3731a 20 20 20 20 20 4c 65 61 76 65 73 52 65 61 64 65       LeavesReade
3731b 72 20 2a 70 52 65 61 64 65 72 73 2c 20 69 6e 74  r *pReaders, int
3731c 20 6e 52 65 61 64 65 72 73 2c 0a 20 20 20 20 20   nReaders,.     
3731d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3731e 20 20 20 20 20 20 20 20 20 4c 65 61 66 57 72 69           LeafWri
3731f 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20  ter *pWriter){. 
37320 20 44 4c 52 65 61 64 65 72 20 64 6c 52 65 61 64   DLReader dlRead
37321 65 72 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54 5d  ers[MERGE_COUNT]
37322 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
37323 70 54 65 72 6d 20 3d 20 6c 65 61 76 65 73 52 65  pTerm = leavesRe
37324 61 64 65 72 54 65 72 6d 28 70 52 65 61 64 65 72  aderTerm(pReader
37325 73 29 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 54 65  s);.  int i, nTe
37326 72 6d 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65  rm = leavesReade
37327 72 54 65 72 6d 42 79 74 65 73 28 70 52 65 61 64  rTermBytes(pRead
37328 65 72 73 29 3b 0a 0a 20 20 61 73 73 65 72 74 28  ers);..  assert(
37329 20 6e 52 65 61 64 65 72 73 3c 3d 4d 45 52 47 45   nReaders<=MERGE
3732a 5f 43 4f 55 4e 54 20 29 3b 0a 0a 20 20 66 6f 72  _COUNT );..  for
3732b 28 69 3d 30 3b 20 69 3c 6e 52 65 61 64 65 72 73  (i=0; i<nReaders
3732c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 64 6c 72 49  ; i++){.    dlrI
3732d 6e 69 74 28 26 64 6c 52 65 61 64 65 72 73 5b 69  nit(&dlReaders[i
3732e 5d 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 0a 20  ], DL_DEFAULT,. 
3732f 20 20 20 20 20 20 20 20 20 20 20 6c 65 61 76 65             leave
37330 73 52 65 61 64 65 72 44 61 74 61 28 70 52 65 61  sReaderData(pRea
37331 64 65 72 73 2b 69 29 2c 0a 20 20 20 20 20 20 20  ders+i),.       
37332 20 20 20 20 20 6c 65 61 76 65 73 52 65 61 64 65       leavesReade
37333 72 44 61 74 61 42 79 74 65 73 28 70 52 65 61 64  rDataBytes(pRead
37334 65 72 73 2b 69 29 29 3b 0a 20 20 7d 0a 0a 20 20  ers+i));.  }..  
37335 72 65 74 75 72 6e 20 6c 65 61 66 57 72 69 74 65  return leafWrite
37336 72 53 74 65 70 4d 65 72 67 65 28 76 2c 20 70 57  rStepMerge(v, pW
37337 72 69 74 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54  riter, pTerm, nT
37338 65 72 6d 2c 20 64 6c 52 65 61 64 65 72 73 2c 20  erm, dlReaders, 
37339 6e 52 65 61 64 65 72 73 29 3b 0a 7d 0a 0a 2f 2a  nReaders);.}../*
3733a 20 46 6f 72 77 61 72 64 20 72 65 66 20 64 75 65   Forward ref due
3733b 20 74 6f 20 6d 75 74 75 61 6c 20 72 65 63 75 72   to mutual recur
3733c 73 69 6f 6e 20 77 69 74 68 20 73 65 67 64 69 72  sion with segdir
3733d 4e 65 78 74 49 6e 64 65 78 28 29 2e 20 2a 2f 0a  NextIndex(). */.
3733e 73 74 61 74 69 63 20 69 6e 74 20 73 65 67 6d 65  static int segme
3733f 6e 74 4d 65 72 67 65 28 66 75 6c 6c 74 65 78 74  ntMerge(fulltext
37340 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c  _vtab *v, int iL
37341 65 76 65 6c 29 3b 0a 0a 2f 2a 20 50 75 74 20 74  evel);../* Put t
37342 68 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62 6c  he next availabl
37343 65 20 69 6e 64 65 78 20 61 74 20 69 4c 65 76 65  e index at iLeve
37344 6c 20 69 6e 74 6f 20 2a 70 69 64 78 2e 20 20 49  l into *pidx.  I
37345 66 20 69 4c 65 76 65 6c 0a 2a 2a 20 61 6c 72 65  f iLevel.** alre
37346 61 64 79 20 68 61 73 20 4d 45 52 47 45 5f 43 4f  ady has MERGE_CO
37347 55 4e 54 20 73 65 67 6d 65 6e 74 73 2c 20 74 68  UNT segments, th
37348 65 79 20 61 72 65 20 6d 65 72 67 65 64 20 74 6f  ey are merged to
37349 20 61 20 68 69 67 68 65 72 0a 2a 2a 20 6c 65 76   a higher.** lev
3734a 65 6c 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 2e  el to make room.
3734b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
3734c 65 67 64 69 72 4e 65 78 74 49 6e 64 65 78 28 66  egdirNextIndex(f
3734d 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
3734e 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20 69 6e 74   int iLevel, int
3734f 20 2a 70 69 64 78 29 7b 0a 20 20 69 6e 74 20 72   *pidx){.  int r
37350 63 20 3d 20 73 65 67 64 69 72 5f 6d 61 78 5f 69  c = segdir_max_i
37351 6e 64 65 78 28 76 2c 20 69 4c 65 76 65 6c 2c 20  ndex(v, iLevel, 
37352 70 69 64 78 29 3b 0a 20 20 69 66 28 20 72 63 3d  pidx);.  if( rc=
37353 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 20  =SQLITE_DONE ){ 
37354 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
37355 4e 6f 20 73 65 67 6d 65 6e 74 73 20 61 74 20 69  No segments at i
37356 4c 65 76 65 6c 2e 20 2a 2f 0a 20 20 20 20 2a 70  Level. */.    *p
37357 69 64 78 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  idx = 0;.  }else
37358 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
37359 52 4f 57 20 29 7b 0a 20 20 20 20 69 66 28 20 2a  ROW ){.    if( *
3735a 70 69 64 78 3d 3d 28 4d 45 52 47 45 5f 43 4f 55  pidx==(MERGE_COU
3735b 4e 54 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 72  NT-1) ){.      r
3735c 63 20 3d 20 73 65 67 6d 65 6e 74 4d 65 72 67 65  c = segmentMerge
3735d 28 76 2c 20 69 4c 65 76 65 6c 29 3b 0a 20 20 20  (v, iLevel);.   
3735e 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
3735f 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
37360 3b 0a 20 20 20 20 20 20 2a 70 69 64 78 20 3d 20  ;.      *pidx = 
37361 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
37362 20 20 20 20 28 2a 70 69 64 78 29 2b 2b 3b 0a 20      (*pidx)++;. 
37363 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
37364 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
37365 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
37366 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4d 65 72 67 65  _OK;.}../* Merge
37367 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 73 65 67   MERGE_COUNT seg
37368 6d 65 6e 74 73 20 61 74 20 69 4c 65 76 65 6c 20  ments at iLevel 
37369 69 6e 74 6f 20 61 20 6e 65 77 20 73 65 67 6d 65  into a new segme
3736a 6e 74 20 61 74 0a 2a 2a 20 69 4c 65 76 65 6c 2b  nt at.** iLevel+
3736b 31 2e 20 20 49 66 20 69 4c 65 76 65 6c 2b 31 20  1.  If iLevel+1 
3736c 69 73 20 61 6c 72 65 61 64 79 20 66 75 6c 6c 20  is already full 
3736d 6f 66 20 73 65 67 6d 65 6e 74 73 2c 20 74 68 6f  of segments, tho
3736e 73 65 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 6d 65  se will be.** me
3736f 72 67 65 64 20 74 6f 20 6d 61 6b 65 20 72 6f 6f  rged to make roo
37370 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  m..*/.static int
37371 20 73 65 67 6d 65 6e 74 4d 65 72 67 65 28 66 75   segmentMerge(fu
37372 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
37373 69 6e 74 20 69 4c 65 76 65 6c 29 7b 0a 20 20 4c  int iLevel){.  L
37374 65 61 66 57 72 69 74 65 72 20 77 72 69 74 65 72  eafWriter writer
37375 3b 0a 20 20 4c 65 61 76 65 73 52 65 61 64 65 72  ;.  LeavesReader
37376 20 6c 72 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54   lrs[MERGE_COUNT
37377 5d 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 2c 20  ];.  int i, rc, 
37378 69 64 78 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 44  idx = 0;..  /* D
37379 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6e 65 78  etermine the nex
3737a 74 20 61 76 61 69 6c 61 62 6c 65 20 73 65 67 6d  t available segm
3737b 65 6e 74 20 69 6e 64 65 78 20 61 74 20 74 68 65  ent index at the
3737c 20 6e 65 78 74 20 6c 65 76 65 6c 2c 0a 20 20 2a   next level,.  *
3737d 2a 20 6d 65 72 67 69 6e 67 20 61 73 20 6e 65 63  * merging as nec
3737e 65 73 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 72  essary..  */.  r
3737f 63 20 3d 20 73 65 67 64 69 72 4e 65 78 74 49 6e  c = segdirNextIn
37380 64 65 78 28 76 2c 20 69 4c 65 76 65 6c 2b 31 2c  dex(v, iLevel+1,
37381 20 26 69 64 78 29 3b 0a 20 20 69 66 28 20 72 63   &idx);.  if( rc
37382 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
37383 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 54  turn rc;..  /* T
37384 4f 44 4f 28 73 68 65 73 73 29 20 54 68 69 73 20  ODO(shess) This 
37385 61 73 73 75 6d 65 73 20 74 68 61 74 20 77 65 27  assumes that we'
37386 6c 6c 20 61 6c 77 61 79 73 20 73 65 65 20 65 78  ll always see ex
37387 61 63 74 6c 79 0a 20 20 2a 2a 20 4d 45 52 47 45  actly.  ** MERGE
37388 5f 43 4f 55 4e 54 20 73 65 67 6d 65 6e 74 73 20  _COUNT segments 
37389 74 6f 20 6d 65 72 67 65 20 61 74 20 61 20 67 69  to merge at a gi
3738a 76 65 6e 20 6c 65 76 65 6c 2e 20 20 54 68 61 74  ven level.  That
3738b 20 77 69 6c 6c 20 62 65 0a 20 20 2a 2a 20 62 72   will be.  ** br
3738c 6f 6b 65 6e 20 69 66 20 77 65 20 61 6c 6c 6f 77  oken if we allow
3738d 20 74 68 65 20 64 65 76 65 6c 6f 70 65 72 20 74   the developer t
3738e 6f 20 72 65 71 75 65 73 74 20 70 72 65 65 6d 70  o request preemp
3738f 74 69 76 65 20 6f 72 0a 20 20 2a 2a 20 64 65 66  tive or.  ** def
37390 65 72 72 65 64 20 6d 65 72 67 69 6e 67 2e 0a 20  erred merging.. 
37391 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 6c 72   */.  memset(&lr
37392 73 2c 20 27 5c 30 27 2c 20 73 69 7a 65 6f 66 28  s, '\0', sizeof(
37393 6c 72 73 29 29 3b 0a 20 20 72 63 20 3d 20 6c 65  lrs));.  rc = le
37394 61 76 65 73 52 65 61 64 65 72 73 49 6e 69 74 28  avesReadersInit(
37395 76 2c 20 69 4c 65 76 65 6c 2c 20 6c 72 73 2c 20  v, iLevel, lrs, 
37396 26 69 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &i);.  if( rc!=S
37397 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
37398 6e 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  n rc;.  assert( 
37399 69 3d 3d 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29  i==MERGE_COUNT )
3739a 3b 0a 0a 20 20 6c 65 61 66 57 72 69 74 65 72 49  ;..  leafWriterI
3739b 6e 69 74 28 69 4c 65 76 65 6c 2b 31 2c 20 69 64  nit(iLevel+1, id
3739c 78 2c 20 26 77 72 69 74 65 72 29 3b 0a 0a 20 20  x, &writer);..  
3739d 2f 2a 20 53 69 6e 63 65 20 6c 65 61 76 65 73 52  /* Since leavesR
3739e 65 61 64 65 72 52 65 6f 72 64 65 72 28 29 20 70  eaderReorder() p
3739f 75 73 68 65 73 20 72 65 61 64 65 72 73 20 61 74  ushes readers at
373a0 20 65 6f 66 20 74 6f 20 74 68 65 20 65 6e 64 2c   eof to the end,
373a1 0a 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 66  .  ** when the f
373a2 69 72 73 74 20 72 65 61 64 65 72 20 69 73 20 65  irst reader is e
373a3 6d 70 74 79 2c 20 61 6c 6c 20 77 69 6c 6c 20 62  mpty, all will b
373a4 65 20 65 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20 20  e empty..  */.  
373a5 77 68 69 6c 65 28 20 21 6c 65 61 76 65 73 52 65  while( !leavesRe
373a6 61 64 65 72 41 74 45 6e 64 28 6c 72 73 29 20 29  aderAtEnd(lrs) )
373a7 7b 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20  {.    /* Figure 
373a8 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 65 61  out how many rea
373a9 64 65 72 73 20 73 68 61 72 65 20 74 68 65 69 72  ders share their
373aa 20 6e 65 78 74 20 74 65 72 6d 2e 20 2a 2f 0a 20   next term. */. 
373ab 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 4d 45     for(i=1; i<ME
373ac 52 47 45 5f 43 4f 55 4e 54 20 26 26 20 21 6c 65  RGE_COUNT && !le
373ad 61 76 65 73 52 65 61 64 65 72 41 74 45 6e 64 28  avesReaderAtEnd(
373ae 6c 72 73 2b 69 29 3b 20 69 2b 2b 29 7b 0a 20 20  lrs+i); i++){.  
373af 20 20 20 20 69 66 28 20 30 21 3d 6c 65 61 76 65      if( 0!=leave
373b0 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 6c  sReaderTermCmp(l
373b1 72 73 2c 20 6c 72 73 2b 69 29 20 29 20 62 72 65  rs, lrs+i) ) bre
373b2 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72  ak;.    }..    r
373b3 63 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72  c = leavesReader
373b4 73 4d 65 72 67 65 28 76 2c 20 6c 72 73 2c 20 69  sMerge(v, lrs, i
373b5 2c 20 26 77 72 69 74 65 72 29 3b 0a 20 20 20 20  , &writer);.    
373b6 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
373b7 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a 20  K ) goto err;.. 
373b8 20 20 20 2f 2a 20 53 74 65 70 20 66 6f 72 77 61     /* Step forwa
373b9 72 64 20 74 68 6f 73 65 20 74 68 61 74 20 77 65  rd those that we
373ba 72 65 20 6d 65 72 67 65 64 2e 20 2a 2f 0a 20 20  re merged. */.  
373bb 20 20 77 68 69 6c 65 28 20 69 2d 2d 3e 30 20 29    while( i-->0 )
373bc 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 65 61  {.      rc = lea
373bd 76 65 73 52 65 61 64 65 72 53 74 65 70 28 76 2c  vesReaderStep(v,
373be 20 6c 72 73 2b 69 29 3b 0a 20 20 20 20 20 20 69   lrs+i);.      i
373bf 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
373c0 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20   ) goto err;..  
373c1 20 20 20 20 2f 2a 20 52 65 6f 72 64 65 72 20 62      /* Reorder b
373c2 79 20 74 65 72 6d 2c 20 74 68 65 6e 20 62 79 20  y term, then by 
373c3 61 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20 6c 65  age. */.      le
373c4 61 76 65 73 52 65 61 64 65 72 52 65 6f 72 64 65  avesReaderReorde
373c5 72 28 6c 72 73 2b 69 2c 20 4d 45 52 47 45 5f 43  r(lrs+i, MERGE_C
373c6 4f 55 4e 54 2d 69 29 3b 0a 20 20 20 20 7d 0a 20  OUNT-i);.    }. 
373c7 20 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   }..  for(i=0; i
373c8 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 3b 20 69 2b  <MERGE_COUNT; i+
373c9 2b 29 7b 0a 20 20 20 20 6c 65 61 76 65 73 52 65  +){.    leavesRe
373ca 61 64 65 72 44 65 73 74 72 6f 79 28 26 6c 72 73  aderDestroy(&lrs
373cb 5b 69 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  [i]);.  }..  rc 
373cc 3d 20 6c 65 61 66 57 72 69 74 65 72 46 69 6e 61  = leafWriterFina
373cd 6c 69 7a 65 28 76 2c 20 26 77 72 69 74 65 72 29  lize(v, &writer)
373ce 3b 0a 20 20 6c 65 61 66 57 72 69 74 65 72 44 65  ;.  leafWriterDe
373cf 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a  stroy(&writer);.
373d0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
373d1 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
373d2 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68  ..  /* Delete th
373d3 65 20 6d 65 72 67 65 64 20 73 65 67 6d 65 6e 74  e merged segment
373d4 20 64 61 74 61 2e 20 2a 2f 0a 20 20 72 65 74 75   data. */.  retu
373d5 72 6e 20 73 65 67 64 69 72 5f 64 65 6c 65 74 65  rn segdir_delete
373d6 28 76 2c 20 69 4c 65 76 65 6c 29 3b 0a 0a 20 65  (v, iLevel);.. e
373d7 72 72 3a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  rr:.  for(i=0; i
373d8 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 3b 20 69 2b  <MERGE_COUNT; i+
373d9 2b 29 7b 0a 20 20 20 20 6c 65 61 76 65 73 52 65  +){.    leavesRe
373da 61 64 65 72 44 65 73 74 72 6f 79 28 26 6c 72 73  aderDestroy(&lrs
373db 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 6c 65 61 66  [i]);.  }.  leaf
373dc 57 72 69 74 65 72 44 65 73 74 72 6f 79 28 26 77  WriterDestroy(&w
373dd 72 69 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e  riter);.  return
373de 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 41 63 63 75 6d   rc;.}../* Accum
373df 75 6c 61 74 65 20 74 68 65 20 75 6e 69 6f 6e 20  ulate the union 
373e0 6f 66 20 2a 61 63 63 20 61 6e 64 20 2a 70 44 61  of *acc and *pDa
373e1 74 61 20 69 6e 74 6f 20 2a 61 63 63 2e 20 2a 2f  ta into *acc. */
373e2 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63  .static void doc
373e3 4c 69 73 74 41 63 63 75 6d 75 6c 61 74 65 55 6e  ListAccumulateUn
373e4 69 6f 6e 28 44 61 74 61 42 75 66 66 65 72 20 2a  ion(DataBuffer *
373e5 61 63 63 2c 0a 20 20 20 20 20 20 20 20 20 20 20  acc,.           
373e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
373e7 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
373e8 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e  ar *pData, int n
373e9 44 61 74 61 29 20 7b 0a 20 20 44 61 74 61 42 75  Data) {.  DataBu
373ea 66 66 65 72 20 74 6d 70 20 3d 20 2a 61 63 63 3b  ffer tmp = *acc;
373eb 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69  .  dataBufferIni
373ec 74 28 61 63 63 2c 20 74 6d 70 2e 6e 44 61 74 61  t(acc, tmp.nData
373ed 2b 6e 44 61 74 61 29 3b 0a 20 20 64 6f 63 4c 69  +nData);.  docLi
373ee 73 74 55 6e 69 6f 6e 28 74 6d 70 2e 70 44 61 74  stUnion(tmp.pDat
373ef 61 2c 20 74 6d 70 2e 6e 44 61 74 61 2c 20 70 44  a, tmp.nData, pD
373f0 61 74 61 2c 20 6e 44 61 74 61 2c 20 61 63 63 29  ata, nData, acc)
373f1 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  ;.  dataBufferDe
373f2 73 74 72 6f 79 28 26 74 6d 70 29 3b 0a 7d 0a 0a  stroy(&tmp);.}..
373f3 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 49  /* TODO(shess) I
373f4 74 20 6d 69 67 68 74 20 62 65 20 69 6e 74 65 72  t might be inter
373f5 65 73 74 69 6e 67 20 74 6f 20 65 78 70 6c 6f 72  esting to explor
373f6 65 20 64 69 66 66 65 72 65 6e 74 20 6d 65 72 67  e different merg
373f7 65 0a 2a 2a 20 73 74 72 61 74 65 67 69 65 73 2c  e.** strategies,
373f8 20 68 65 72 65 2e 20 20 46 6f 72 20 69 6e 73 74   here.  For inst
373f9 61 6e 63 65 2c 20 73 69 6e 63 65 20 74 68 69 73  ance, since this
373fa 20 69 73 20 61 20 73 6f 72 74 65 64 20 6d 65 72   is a sorted mer
373fb 67 65 2c 20 77 65 0a 2a 2a 20 63 6f 75 6c 64 20  ge, we.** could 
373fc 65 61 73 69 6c 79 20 6d 65 72 67 65 20 6d 61 6e  easily merge man
373fd 79 20 64 6f 63 6c 69 73 74 73 20 69 6e 20 70 61  y doclists in pa
373fe 72 61 6c 6c 65 6c 2e 20 20 57 69 74 68 20 73 6f  rallel.  With so
373ff 6d 65 0a 2a 2a 20 63 6f 6d 70 72 65 68 65 6e 73  me.** comprehens
37400 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 6f 72 61  ion of the stora
37401 67 65 20 66 6f 72 6d 61 74 2c 20 77 65 20 63 6f  ge format, we co
37402 75 6c 64 20 6d 65 72 67 65 20 61 6c 6c 20 6f 66  uld merge all of
37403 20 74 68 65 0a 2a 2a 20 64 6f 63 6c 69 73 74 73   the.** doclists
37404 20 77 69 74 68 69 6e 20 61 20 6c 65 61 66 20 6e   within a leaf n
37405 6f 64 65 20 64 69 72 65 63 74 6c 79 20 66 72 6f  ode directly fro
37406 6d 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 27  m the leaf node'
37407 73 20 73 74 6f 72 61 67 65 2e 0a 2a 2a 20 49 74  s storage..** It
37408 20 6d 61 79 20 62 65 20 77 6f 72 74 68 77 68 69   may be worthwhi
37409 6c 65 20 74 6f 20 6d 65 72 67 65 20 73 6d 61 6c  le to merge smal
3740a 6c 65 72 20 64 6f 63 6c 69 73 74 73 20 62 65 66  ler doclists bef
3740b 6f 72 65 20 6c 61 72 67 65 72 0a 2a 2a 20 64 6f  ore larger.** do
3740c 63 6c 69 73 74 73 2c 20 73 69 6e 63 65 20 74 68  clists, since th
3740d 65 79 20 63 61 6e 20 62 65 20 74 72 61 76 65 72  ey can be traver
3740e 73 65 64 20 6d 6f 72 65 20 71 75 69 63 6b 6c 79  sed more quickly
3740f 20 2d 20 62 75 74 20 74 68 65 0a 2a 2a 20 72 65   - but the.** re
37410 73 75 6c 74 73 20 6d 61 79 20 68 61 76 65 20 6c  sults may have l
37411 65 73 73 20 6f 76 65 72 6c 61 70 2c 20 6d 61 6b  ess overlap, mak
37412 69 6e 67 20 74 68 65 6d 20 6d 6f 72 65 20 65 78  ing them more ex
37413 70 65 6e 73 69 76 65 20 69 6e 20 61 0a 2a 2a 20  pensive in a.** 
37414 64 69 66 66 65 72 65 6e 74 20 77 61 79 2e 0a 2a  different way..*
37415 2f 0a 0a 2f 2a 20 53 63 61 6e 20 70 52 65 61 64  /../* Scan pRead
37416 65 72 20 66 6f 72 20 70 54 65 72 6d 2f 6e 54 65  er for pTerm/nTe
37417 72 6d 2c 20 61 6e 64 20 6d 65 72 67 65 20 74 68  rm, and merge th
37418 65 20 74 65 72 6d 27 73 20 64 6f 63 6c 69 73 74  e term's doclist
37419 20 6f 76 65 72 0a 2a 2a 20 2a 6f 75 74 20 28 61   over.** *out (a
3741a 6e 79 20 64 6f 63 6c 69 73 74 73 20 77 69 74 68  ny doclists with
3741b 20 64 75 70 6c 69 63 61 74 65 20 64 6f 63 69 64   duplicate docid
3741c 73 20 6f 76 65 72 77 72 69 74 65 20 74 68 6f 73  s overwrite thos
3741d 65 20 69 6e 20 2a 6f 75 74 29 2e 0a 2a 2a 20 49  e in *out)..** I
3741e 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e  nternal function
3741f 20 66 6f 72 20 6c 6f 61 64 53 65 67 6d 65 6e 74   for loadSegment
37420 4c 65 61 66 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  Leaf()..*/.stati
37421 63 20 69 6e 74 20 6c 6f 61 64 53 65 67 6d 65 6e  c int loadSegmen
37422 74 4c 65 61 76 65 73 49 6e 74 28 66 75 6c 6c 74  tLeavesInt(fullt
37423 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61  ext_vtab *v, Lea
37424 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
37425 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
37426 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37427 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
37428 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d  pTerm, int nTerm
37429 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a  , int isPrefix,.
3742a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3742b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3742c 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75 74 29  DataBuffer *out)
3742d 7b 0a 20 20 2f 2a 20 64 6f 63 6c 69 73 74 20 64  {.  /* doclist d
3742e 61 74 61 20 69 73 20 61 63 63 75 6d 75 6c 61 74  ata is accumulat
3742f 65 64 20 69 6e 74 6f 20 70 42 75 66 66 65 72 73  ed into pBuffers
37430 20 73 69 6d 69 6c 61 72 20 74 6f 20 68 6f 77 20   similar to how 
37431 6f 6e 65 20 64 6f 65 73 0a 20 20 2a 2a 20 69 6e  one does.  ** in
37432 63 72 65 6d 65 6e 74 20 69 6e 20 62 69 6e 61 72  crement in binar
37433 79 20 61 72 69 74 68 6d 65 74 69 63 2e 20 20 49  y arithmetic.  I
37434 66 20 69 6e 64 65 78 20 30 20 69 73 20 65 6d 70  f index 0 is emp
37435 74 79 2c 20 74 68 65 20 64 61 74 61 20 69 73 0a  ty, the data is.
37436 20 20 2a 2a 20 73 74 6f 72 65 64 20 74 68 65 72    ** stored ther
37437 65 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20  e.  If there is 
37438 64 61 74 61 20 74 68 65 72 65 2c 20 69 74 20 69  data there, it i
37439 73 20 6d 65 72 67 65 64 20 61 6e 64 20 74 68 65  s merged and the
3743a 0a 20 20 2a 2a 20 72 65 73 75 6c 74 73 20 63 61  .  ** results ca
3743b 72 72 69 65 64 20 69 6e 74 6f 20 70 6f 73 69 74  rried into posit
3743c 69 6f 6e 20 31 2c 20 77 69 74 68 20 66 75 72 74  ion 1, with furt
3743d 68 65 72 20 6d 65 72 67 65 2d 61 6e 64 2d 63 61  her merge-and-ca
3743e 72 72 79 0a 20 20 2a 2a 20 75 6e 74 69 6c 20 61  rry.  ** until a
3743f 6e 20 65 6d 70 74 79 20 70 6f 73 69 74 69 6f 6e  n empty position
37440 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a   is found..  */.
37441 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 42    DataBuffer *pB
37442 75 66 66 65 72 73 20 3d 20 4e 55 4c 4c 3b 0a 20  uffers = NULL;. 
37443 20 69 6e 74 20 6e 42 75 66 66 65 72 73 20 3d 20   int nBuffers = 
37444 30 2c 20 6e 4d 61 78 42 75 66 66 65 72 73 20 3d  0, nMaxBuffers =
37445 20 30 2c 20 72 63 3b 0a 0a 20 20 61 73 73 65 72   0, rc;..  asser
37446 74 28 20 6e 54 65 72 6d 3e 30 20 29 3b 0a 0a 20  t( nTerm>0 );.. 
37447 20 66 6f 72 28 72 63 3d 53 51 4c 49 54 45 5f 4f   for(rc=SQLITE_O
37448 4b 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  K; rc==SQLITE_OK
37449 20 26 26 20 21 6c 65 61 76 65 73 52 65 61 64 65   && !leavesReade
3744a 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 3b  rAtEnd(pReader);
3744b 0a 20 20 20 20 20 20 72 63 3d 6c 65 61 76 65 73  .      rc=leaves
3744c 52 65 61 64 65 72 53 74 65 70 28 76 2c 20 70 52  ReaderStep(v, pR
3744d 65 61 64 65 72 29 29 7b 0a 20 20 20 20 2f 2a 20  eader)){.    /* 
3744e 54 4f 44 4f 28 73 68 65 73 73 29 20 52 65 61 6c  TODO(shess) Real
3744f 6c 79 20 77 61 6e 74 20 6c 65 61 76 65 73 52 65  ly want leavesRe
37450 61 64 65 72 54 65 72 6d 43 6d 70 28 29 2c 20 62  aderTermCmp(), b
37451 75 74 20 74 68 61 74 20 6e 61 6d 65 20 69 73 0a  ut that name is.
37452 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 74      ** already t
37453 61 6b 65 6e 20 74 6f 20 63 6f 6d 70 61 72 65 20  aken to compare 
37454 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 77 6f  the terms of two
37455 20 4c 65 61 76 65 73 52 65 61 64 65 72 73 2e 20   LeavesReaders. 
37456 20 54 68 69 6e 6b 0a 20 20 20 20 2a 2a 20 6f 6e   Think.    ** on
37457 20 61 20 62 65 74 74 65 72 20 6e 61 6d 65 2e 20   a better name. 
37458 20 5b 4d 65 61 6e 77 68 69 6c 65 2c 20 62 72 65   [Meanwhile, bre
37459 61 6b 20 65 6e 63 61 70 73 75 6c 61 74 69 6f 6e  ak encapsulation
3745a 20 72 61 74 68 65 72 20 74 68 61 6e 0a 20 20 20   rather than.   
3745b 20 2a 2a 20 75 73 65 20 61 20 63 6f 6e 66 75 73   ** use a confus
3745c 69 6e 67 20 6e 61 6d 65 2e 5d 0a 20 20 20 20 2a  ing name.].    *
3745d 2f 0a 20 20 20 20 69 6e 74 20 63 20 3d 20 6c 65  /.    int c = le
3745e 61 66 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28  afReaderTermCmp(
3745f 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52 65  &pReader->leafRe
37460 61 64 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65  ader, pTerm, nTe
37461 72 6d 2c 20 69 73 50 72 65 66 69 78 29 3b 0a 20  rm, isPrefix);. 
37462 20 20 20 69 66 28 20 63 3e 30 20 29 20 62 72 65     if( c>0 ) bre
37463 61 6b 3b 20 20 20 20 20 20 2f 2a 20 50 61 73 74  ak;      /* Past
37464 20 61 6e 79 20 70 6f 73 73 69 62 6c 65 20 6d 61   any possible ma
37465 74 63 68 65 73 2e 20 2a 2f 0a 20 20 20 20 69 66  tches. */.    if
37466 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ( c==0 ){.      
37467 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
37468 61 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72  a = leavesReader
37469 44 61 74 61 28 70 52 65 61 64 65 72 29 3b 0a 20  Data(pReader);. 
3746a 20 20 20 20 20 69 6e 74 20 69 42 75 66 66 65 72       int iBuffer
3746b 2c 20 6e 44 61 74 61 20 3d 20 6c 65 61 76 65 73  , nData = leaves
3746c 52 65 61 64 65 72 44 61 74 61 42 79 74 65 73 28  ReaderDataBytes(
3746d 70 52 65 61 64 65 72 29 3b 0a 0a 20 20 20 20 20  pReader);..     
3746e 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 66 69 72   /* Find the fir
3746f 73 74 20 65 6d 70 74 79 20 62 75 66 66 65 72 2e  st empty buffer.
37470 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 69 42   */.      for(iB
37471 75 66 66 65 72 3d 30 3b 20 69 42 75 66 66 65 72  uffer=0; iBuffer
37472 3c 6e 42 75 66 66 65 72 73 3b 20 2b 2b 69 42 75  <nBuffers; ++iBu
37473 66 66 65 72 29 7b 0a 20 20 20 20 20 20 20 20 69  ffer){.        i
37474 66 28 20 30 3d 3d 70 42 75 66 66 65 72 73 5b 69  f( 0==pBuffers[i
37475 42 75 66 66 65 72 5d 2e 6e 44 61 74 61 20 29 20  Buffer].nData ) 
37476 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a  break;.      }..
37477 20 20 20 20 20 20 2f 2a 20 4f 75 74 20 6f 66 20        /* Out of 
37478 62 75 66 66 65 72 73 2c 20 61 64 64 20 61 6e 20  buffers, add an 
37479 65 6d 70 74 79 20 6f 6e 65 2e 20 2a 2f 0a 20 20  empty one. */.  
3747a 20 20 20 20 69 66 28 20 69 42 75 66 66 65 72 3d      if( iBuffer=
3747b 3d 6e 42 75 66 66 65 72 73 20 29 7b 0a 20 20 20  =nBuffers ){.   
3747c 20 20 20 20 20 69 66 28 20 6e 42 75 66 66 65 72       if( nBuffer
3747d 73 3d 3d 6e 4d 61 78 42 75 66 66 65 72 73 20 29  s==nMaxBuffers )
3747e 7b 0a 20 20 20 20 20 20 20 20 20 20 44 61 74 61  {.          Data
3747f 42 75 66 66 65 72 20 2a 70 3b 0a 20 20 20 20 20  Buffer *p;.     
37480 20 20 20 20 20 6e 4d 61 78 42 75 66 66 65 72 73       nMaxBuffers
37481 20 2b 3d 20 32 30 3b 0a 0a 20 20 20 20 20 20 20   += 20;..       
37482 20 20 20 2f 2a 20 4d 61 6e 75 61 6c 20 72 65 61     /* Manual rea
37483 6c 6c 6f 63 20 73 6f 20 77 65 20 63 61 6e 20 68  lloc so we can h
37484 61 6e 64 6c 65 20 4e 55 4c 4c 20 61 70 70 72 6f  andle NULL appro
37485 70 72 69 61 74 65 6c 79 2e 20 2a 2f 0a 20 20 20  priately. */.   
37486 20 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69 74         p = sqlit
37487 65 33 5f 6d 61 6c 6c 6f 63 28 6e 4d 61 78 42 75  e3_malloc(nMaxBu
37488 66 66 65 72 73 2a 73 69 7a 65 6f 66 28 2a 70 42  ffers*sizeof(*pB
37489 75 66 66 65 72 73 29 29 3b 0a 20 20 20 20 20 20  uffers));.      
3748a 20 20 20 20 69 66 28 20 70 3d 3d 4e 55 4c 4c 20      if( p==NULL 
3748b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  ){.            r
3748c 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
3748d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  ;.            br
3748e 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
3748f 0a 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ..          if( 
37490 6e 42 75 66 66 65 72 73 3e 30 20 29 7b 0a 20 20  nBuffers>0 ){.  
37491 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
37492 28 70 42 75 66 66 65 72 73 21 3d 4e 55 4c 4c 29  (pBuffers!=NULL)
37493 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d 65  ;.            me
37494 6d 63 70 79 28 70 2c 20 70 42 75 66 66 65 72 73  mcpy(p, pBuffers
37495 2c 20 6e 42 75 66 66 65 72 73 2a 73 69 7a 65 6f  , nBuffers*sizeo
37496 66 28 2a 70 42 75 66 66 65 72 73 29 29 3b 0a 20  f(*pBuffers));. 
37497 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
37498 65 33 5f 66 72 65 65 28 70 42 75 66 66 65 72 73  e3_free(pBuffers
37499 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
3749a 20 20 20 20 20 20 20 20 20 70 42 75 66 66 65 72           pBuffer
3749b 73 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 7d  s = p;.        }
3749c 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66  .        dataBuf
3749d 66 65 72 49 6e 69 74 28 26 28 70 42 75 66 66 65  ferInit(&(pBuffe
3749e 72 73 5b 6e 42 75 66 66 65 72 73 5d 29 2c 20 30  rs[nBuffers]), 0
3749f 29 3b 0a 20 20 20 20 20 20 20 20 6e 42 75 66 66  );.        nBuff
374a0 65 72 73 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 0a  ers++;.      }..
374a1 20 20 20 20 20 20 2f 2a 20 41 74 20 74 68 69 73        /* At this
374a2 20 70 6f 69 6e 74 2c 20 6d 75 73 74 20 68 61 76   point, must hav
374a3 65 20 61 6e 20 65 6d 70 74 79 20 61 74 20 69 42  e an empty at iB
374a4 75 66 66 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20  uffer. */.      
374a5 61 73 73 65 72 74 28 69 42 75 66 66 65 72 3c 6e  assert(iBuffer<n
374a6 42 75 66 66 65 72 73 20 26 26 20 70 42 75 66 66  Buffers && pBuff
374a7 65 72 73 5b 69 42 75 66 66 65 72 5d 2e 6e 44 61  ers[iBuffer].nDa
374a8 74 61 3d 3d 30 29 3b 0a 0a 20 20 20 20 20 20 2f  ta==0);..      /
374a9 2a 20 49 66 20 65 6d 70 74 79 20 77 61 73 20 66  * If empty was f
374aa 69 72 73 74 20 62 75 66 66 65 72 2c 20 6e 6f 20  irst buffer, no 
374ab 6e 65 65 64 20 66 6f 72 20 6d 65 72 67 65 20 6c  need for merge l
374ac 6f 67 69 63 2e 20 2a 2f 0a 20 20 20 20 20 20 69  ogic. */.      i
374ad 66 28 20 69 42 75 66 66 65 72 3d 3d 30 20 29 7b  f( iBuffer==0 ){
374ae 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66  .        dataBuf
374af 66 65 72 52 65 70 6c 61 63 65 28 26 28 70 42 75  ferReplace(&(pBu
374b0 66 66 65 72 73 5b 30 5d 29 2c 20 70 44 61 74 61  ffers[0]), pData
374b1 2c 20 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20  , nData);.      
374b2 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
374b3 2a 20 70 41 63 63 20 69 73 20 74 68 65 20 65 6d  * pAcc is the em
374b4 70 74 79 20 62 75 66 66 65 72 20 74 68 65 20 6d  pty buffer the m
374b5 65 72 67 65 64 20 64 61 74 61 20 77 69 6c 6c 20  erged data will 
374b6 65 6e 64 20 75 70 20 69 6e 2e 20 2a 2f 0a 20 20  end up in. */.  
374b7 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72        DataBuffer
374b8 20 2a 70 41 63 63 20 3d 20 26 28 70 42 75 66 66   *pAcc = &(pBuff
374b9 65 72 73 5b 69 42 75 66 66 65 72 5d 29 3b 0a 20  ers[iBuffer]);. 
374ba 20 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65         DataBuffe
374bb 72 20 2a 70 20 3d 20 26 28 70 42 75 66 66 65 72  r *p = &(pBuffer
374bc 73 5b 30 5d 29 3b 0a 0a 20 20 20 20 20 20 20 20  s[0]);..        
374bd 2f 2a 20 48 61 6e 64 6c 65 20 70 6f 73 69 74 69  /* Handle positi
374be 6f 6e 20 30 20 73 70 65 63 69 61 6c 6c 79 20 74  on 0 specially t
374bf 6f 20 61 76 6f 69 64 20 6e 65 65 64 20 74 6f 20  o avoid need to 
374c0 70 72 69 6d 65 20 70 41 63 63 0a 20 20 20 20 20  prime pAcc.     
374c1 20 20 20 2a 2a 20 77 69 74 68 20 70 44 61 74 61     ** with pData
374c2 2f 6e 44 61 74 61 2e 0a 20 20 20 20 20 20 20 20  /nData..        
374c3 2a 2f 0a 20 20 20 20 20 20 20 20 64 61 74 61 42  */.        dataB
374c4 75 66 66 65 72 53 77 61 70 28 70 2c 20 70 41 63  ufferSwap(p, pAc
374c5 63 29 3b 0a 20 20 20 20 20 20 20 20 64 6f 63 4c  c);.        docL
374c6 69 73 74 41 63 63 75 6d 75 6c 61 74 65 55 6e 69  istAccumulateUni
374c7 6f 6e 28 70 41 63 63 2c 20 70 44 61 74 61 2c 20  on(pAcc, pData, 
374c8 6e 44 61 74 61 29 3b 0a 0a 20 20 20 20 20 20 20  nData);..       
374c9 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 72   /* Accumulate r
374ca 65 6d 61 69 6e 69 6e 67 20 64 6f 63 6c 69 73 74  emaining doclist
374cb 73 20 69 6e 74 6f 20 70 41 63 63 2e 20 2a 2f 0a  s into pAcc. */.
374cc 20 20 20 20 20 20 20 20 66 6f 72 28 2b 2b 70 3b          for(++p;
374cd 20 70 3c 70 41 63 63 3b 20 2b 2b 70 29 7b 0a 20   p<pAcc; ++p){. 
374ce 20 20 20 20 20 20 20 20 20 64 6f 63 4c 69 73 74           docList
374cf 41 63 63 75 6d 75 6c 61 74 65 55 6e 69 6f 6e 28  AccumulateUnion(
374d0 70 41 63 63 2c 20 70 2d 3e 70 44 61 74 61 2c 20  pAcc, p->pData, 
374d1 70 2d 3e 6e 44 61 74 61 29 3b 0a 0a 20 20 20 20  p->nData);..    
374d2 20 20 20 20 20 20 2f 2a 20 64 61 74 61 42 75 66        /* dataBuf
374d3 66 65 72 52 65 73 65 74 28 29 20 63 6f 75 6c 64  ferReset() could
374d4 20 61 6c 6c 6f 77 20 61 20 6c 61 72 67 65 20 64   allow a large d
374d5 6f 63 6c 69 73 74 20 74 6f 20 62 6c 6f 77 20 75  oclist to blow u
374d6 70 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f  p.          ** o
374d7 75 72 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 72  ur memory requir
374d8 65 6d 65 6e 74 73 2e 0a 20 20 20 20 20 20 20 20  ements..        
374d9 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69    */.          i
374da 66 28 20 70 2d 3e 6e 43 61 70 61 63 69 74 79 3c  f( p->nCapacity<
374db 31 30 32 34 20 29 7b 0a 20 20 20 20 20 20 20 20  1024 ){.        
374dc 20 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65      dataBufferRe
374dd 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20  set(p);.        
374de 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
374df 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44       dataBufferD
374e0 65 73 74 72 6f 79 28 70 29 3b 0a 20 20 20 20 20  estroy(p);.     
374e1 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65         dataBuffe
374e2 72 49 6e 69 74 28 70 2c 20 30 29 3b 0a 20 20 20  rInit(p, 0);.   
374e3 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
374e4 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
374e5 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 6e 69 6f 6e  .  }..  /* Union
374e6 20 61 6c 6c 20 74 68 65 20 64 6f 63 6c 69 73 74   all the doclist
374e7 73 20 74 6f 67 65 74 68 65 72 20 69 6e 74 6f 20  s together into 
374e8 2a 6f 75 74 2e 20 2a 2f 0a 20 20 2f 2a 20 54 4f  *out. */.  /* TO
374e9 44 4f 28 73 68 65 73 73 29 20 57 68 61 74 20 69  DO(shess) What i
374ea 66 20 2a 6f 75 74 20 69 73 20 62 69 67 3f 20 20  f *out is big?  
374eb 53 69 67 68 2e 20 2a 2f 0a 20 20 69 66 28 20 72  Sigh. */.  if( r
374ec 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
374ed 6e 42 75 66 66 65 72 73 3e 30 20 29 7b 0a 20 20  nBuffers>0 ){.  
374ee 20 20 69 6e 74 20 69 42 75 66 66 65 72 3b 0a 20    int iBuffer;. 
374ef 20 20 20 66 6f 72 28 69 42 75 66 66 65 72 3d 30     for(iBuffer=0
374f0 3b 20 69 42 75 66 66 65 72 3c 6e 42 75 66 66 65  ; iBuffer<nBuffe
374f1 72 73 3b 20 2b 2b 69 42 75 66 66 65 72 29 7b 0a  rs; ++iBuffer){.
374f2 20 20 20 20 20 20 69 66 28 20 70 42 75 66 66 65        if( pBuffe
374f3 72 73 5b 69 42 75 66 66 65 72 5d 2e 6e 44 61 74  rs[iBuffer].nDat
374f4 61 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  a>0 ){.        i
374f5 66 28 20 6f 75 74 2d 3e 6e 44 61 74 61 3d 3d 30  f( out->nData==0
374f6 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 61   ){.          da
374f7 74 61 42 75 66 66 65 72 53 77 61 70 28 6f 75 74  taBufferSwap(out
374f8 2c 20 26 28 70 42 75 66 66 65 72 73 5b 69 42 75  , &(pBuffers[iBu
374f9 66 66 65 72 5d 29 29 3b 0a 20 20 20 20 20 20 20  ffer]));.       
374fa 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
374fb 20 20 64 6f 63 4c 69 73 74 41 63 63 75 6d 75 6c    docListAccumul
374fc 61 74 65 55 6e 69 6f 6e 28 6f 75 74 2c 20 70 42  ateUnion(out, pB
374fd 75 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d 2e  uffers[iBuffer].
374fe 70 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20  pData,.         
374ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37500 20 20 20 20 20 20 20 20 70 42 75 66 66 65 72 73          pBuffers
37501 5b 69 42 75 66 66 65 72 5d 2e 6e 44 61 74 61 29  [iBuffer].nData)
37502 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
37503 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
37504 20 77 68 69 6c 65 28 20 6e 42 75 66 66 65 72 73   while( nBuffers
37505 2d 2d 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75  -- ){.    dataBu
37506 66 66 65 72 44 65 73 74 72 6f 79 28 26 28 70 42  fferDestroy(&(pB
37507 75 66 66 65 72 73 5b 6e 42 75 66 66 65 72 73 5d  uffers[nBuffers]
37508 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 42  ));.  }.  if( pB
37509 75 66 66 65 72 73 21 3d 4e 55 4c 4c 20 29 20 73  uffers!=NULL ) s
3750a 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 75 66  qlite3_free(pBuf
3750b 66 65 72 73 29 3b 0a 0a 20 20 72 65 74 75 72 6e  fers);..  return
3750c 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 43 61 6c 6c 20   rc;.}../* Call 
3750d 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65  loadSegmentLeave
3750e 73 49 6e 74 28 29 20 77 69 74 68 20 70 44 61 74  sInt() with pDat
3750f 61 2f 6e 44 61 74 61 20 61 73 20 69 6e 70 75 74  a/nData as input
37510 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  . */.static int 
37511 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 66 28  loadSegmentLeaf(
37512 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
37513 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44  , const char *pD
37514 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a  ata, int nData,.
37515 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37516 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
37517 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e   char *pTerm, in
37518 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50  t nTerm, int isP
37519 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20  refix,.         
3751a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3751b 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75    DataBuffer *ou
3751c 74 29 7b 0a 20 20 4c 65 61 76 65 73 52 65 61 64  t){.  LeavesRead
3751d 65 72 20 72 65 61 64 65 72 3b 0a 20 20 69 6e 74  er reader;.  int
3751e 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   rc;..  assert( 
3751f 6e 44 61 74 61 3e 31 20 29 3b 0a 20 20 61 73 73  nData>1 );.  ass
37520 65 72 74 28 20 2a 70 44 61 74 61 3d 3d 27 5c 30  ert( *pData=='\0
37521 27 20 29 3b 0a 20 20 72 63 20 3d 20 6c 65 61 76  ' );.  rc = leav
37522 65 73 52 65 61 64 65 72 49 6e 69 74 28 76 2c 20  esReaderInit(v, 
37523 30 2c 20 30 2c 20 30 2c 20 70 44 61 74 61 2c 20  0, 0, 0, pData, 
37524 6e 44 61 74 61 2c 20 26 72 65 61 64 65 72 29 3b  nData, &reader);
37525 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
37526 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
37527 3b 0a 0a 20 20 72 63 20 3d 20 6c 6f 61 64 53 65  ;..  rc = loadSe
37528 67 6d 65 6e 74 4c 65 61 76 65 73 49 6e 74 28 76  gmentLeavesInt(v
37529 2c 20 26 72 65 61 64 65 72 2c 20 70 54 65 72 6d  , &reader, pTerm
3752a 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69  , nTerm, isPrefi
3752b 78 2c 20 6f 75 74 29 3b 0a 20 20 6c 65 61 76 65  x, out);.  leave
3752c 73 52 65 61 64 65 72 52 65 73 65 74 28 26 72 65  sReaderReset(&re
3752d 61 64 65 72 29 3b 0a 20 20 6c 65 61 76 65 73 52  ader);.  leavesR
3752e 65 61 64 65 72 44 65 73 74 72 6f 79 28 26 72 65  eaderDestroy(&re
3752f 61 64 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  ader);.  return 
37530 72 63 3b 0a 7d 0a 0a 2f 2a 20 43 61 6c 6c 20 6c  rc;.}../* Call l
37531 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73  oadSegmentLeaves
37532 49 6e 74 28 29 20 77 69 74 68 20 74 68 65 20 6c  Int() with the l
37533 65 61 66 20 6e 6f 64 65 73 20 66 72 6f 6d 20 69  eaf nodes from i
37534 53 74 61 72 74 4c 65 61 66 20 74 6f 0a 2a 2a 20  StartLeaf to.** 
37535 69 45 6e 64 4c 65 61 66 20 28 69 6e 63 6c 75 73  iEndLeaf (inclus
37536 69 76 65 29 20 61 73 20 69 6e 70 75 74 2c 20 61  ive) as input, a
37537 6e 64 20 6d 65 72 67 65 20 74 68 65 20 72 65 73  nd merge the res
37538 75 6c 74 69 6e 67 20 64 6f 63 6c 69 73 74 20 69  ulting doclist i
37539 6e 74 6f 0a 2a 2a 20 6f 75 74 2e 0a 2a 2f 0a 73  nto.** out..*/.s
3753a 74 61 74 69 63 20 69 6e 74 20 6c 6f 61 64 53 65  tatic int loadSe
3753b 67 6d 65 6e 74 4c 65 61 76 65 73 28 66 75 6c 6c  gmentLeaves(full
3753c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20  text_vtab *v,.  
3753d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3753e 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
3753f 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74 4c 65  e_int64 iStartLe
37540 61 66 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  af, sqlite_int64
37541 20 69 45 6e 64 4c 65 61 66 2c 0a 20 20 20 20 20   iEndLeaf,.     
37542 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37543 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
37544 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e  ar *pTerm, int n
37545 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66  Term, int isPref
37546 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ix,.            
37547 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37548 20 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75 74   DataBuffer *out
37549 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4c  ){.  int rc;.  L
3754a 65 61 76 65 73 52 65 61 64 65 72 20 72 65 61 64  eavesReader read
3754b 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69  er;..  assert( i
3754c 53 74 61 72 74 4c 65 61 66 3c 3d 69 45 6e 64 4c  StartLeaf<=iEndL
3754d 65 61 66 20 29 3b 0a 20 20 72 63 20 3d 20 6c 65  eaf );.  rc = le
3754e 61 76 65 73 52 65 61 64 65 72 49 6e 69 74 28 76  avesReaderInit(v
3754f 2c 20 30 2c 20 69 53 74 61 72 74 4c 65 61 66 2c  , 0, iStartLeaf,
37550 20 69 45 6e 64 4c 65 61 66 2c 20 4e 55 4c 4c 2c   iEndLeaf, NULL,
37551 20 30 2c 20 26 72 65 61 64 65 72 29 3b 0a 20 20   0, &reader);.  
37552 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
37553 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
37554 20 20 72 63 20 3d 20 6c 6f 61 64 53 65 67 6d 65    rc = loadSegme
37555 6e 74 4c 65 61 76 65 73 49 6e 74 28 76 2c 20 26  ntLeavesInt(v, &
37556 72 65 61 64 65 72 2c 20 70 54 65 72 6d 2c 20 6e  reader, pTerm, n
37557 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 20  Term, isPrefix, 
37558 6f 75 74 29 3b 0a 20 20 6c 65 61 76 65 73 52 65  out);.  leavesRe
37559 61 64 65 72 52 65 73 65 74 28 26 72 65 61 64 65  aderReset(&reade
3755a 72 29 3b 0a 20 20 6c 65 61 76 65 73 52 65 61 64  r);.  leavesRead
3755b 65 72 44 65 73 74 72 6f 79 28 26 72 65 61 64 65  erDestroy(&reade
3755c 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  r);.  return rc;
3755d 0a 7d 0a 0a 2f 2a 20 54 61 6b 69 6e 67 20 70 44  .}../* Taking pD
3755e 61 74 61 2f 6e 44 61 74 61 20 61 73 20 61 6e 20  ata/nData as an 
3755f 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2c 20 66  interior node, f
37560 69 6e 64 20 74 68 65 20 73 65 71 75 65 6e 63 65  ind the sequence
37561 20 6f 66 20 63 68 69 6c 64 0a 2a 2a 20 6e 6f 64   of child.** nod
37562 65 73 20 77 68 69 63 68 20 63 6f 75 6c 64 20 69  es which could i
37563 6e 63 6c 75 64 65 20 70 54 65 72 6d 2f 6e 54 65  nclude pTerm/nTe
37564 72 6d 2f 69 73 50 72 65 66 69 78 2e 20 20 4e 6f  rm/isPrefix.  No
37565 74 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 69  te that the.** i
37566 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 74 65 72  nterior node ter
37567 6d 73 20 6c 6f 67 69 63 61 6c 6c 79 20 63 6f 6d  ms logically com
37568 65 20 62 65 74 77 65 65 6e 20 74 68 65 20 62 6c  e between the bl
37569 6f 63 6b 73 2c 20 73 6f 20 74 68 65 72 65 20 69  ocks, so there i
3756a 73 0a 2a 2a 20 6f 6e 65 20 6d 6f 72 65 20 62 6c  s.** one more bl
3756b 6f 63 6b 69 64 20 74 68 61 6e 20 74 68 65 72 65  ockid than there
3756c 20 61 72 65 20 74 65 72 6d 73 20 28 74 68 61 74   are terms (that
3756d 20 62 6c 6f 63 6b 20 63 6f 6e 74 61 69 6e 73 20   block contains 
3756e 74 65 72 6d 73 20 3e 3d 0a 2a 2a 20 74 68 65 20  terms >=.** the 
3756f 6c 61 73 74 20 69 6e 74 65 72 69 6f 72 2d 6e 6f  last interior-no
37570 64 65 20 74 65 72 6d 29 2e 0a 2a 2f 0a 2f 2a 20  de term)..*/./* 
37571 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68 65 20  TODO(shess) The 
37572 63 61 6c 6c 69 6e 67 20 63 6f 64 65 20 6d 61 79  calling code may
37573 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 20 74 68   already know th
37574 61 74 20 74 68 65 20 65 6e 64 20 63 68 69 6c 64  at the end child
37575 20 69 73 0a 2a 2a 20 6e 6f 74 20 77 6f 72 74 68   is.** not worth
37576 20 63 61 6c 63 75 6c 61 74 69 6e 67 2c 20 62 65   calculating, be
37577 63 61 75 73 65 20 74 68 65 20 65 6e 64 20 6d 61  cause the end ma
37578 79 20 62 65 20 69 6e 20 61 20 6c 61 74 65 72 20  y be in a later 
37579 73 69 62 6c 69 6e 67 0a 2a 2a 20 6e 6f 64 65 2e  sibling.** node.
3757a 20 20 43 6f 6e 73 69 64 65 72 20 77 68 65 74 68    Consider wheth
3757b 65 72 20 62 72 65 61 6b 69 6e 67 20 73 79 6d 6d  er breaking symm
3757c 65 74 72 79 20 69 73 20 77 6f 72 74 68 77 68 69  etry is worthwhi
3757d 6c 65 2e 20 20 49 20 73 75 73 70 65 63 74 0a 2a  le.  I suspect.*
3757e 2a 20 69 74 20 69 73 20 6e 6f 74 20 77 6f 72 74  * it is not wort
3757f 68 77 68 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  hwhile..*/.stati
37580 63 20 76 6f 69 64 20 67 65 74 43 68 69 6c 64 72  c void getChildr
37581 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 63 6f 6e  enContaining(con
37582 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
37583 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20  int nData,.     
37584 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37585 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
37586 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20  st char *pTerm, 
37587 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69  int nTerm, int i
37588 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20  sPrefix,.       
37589 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3758a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
3758b 65 5f 69 6e 74 36 34 20 2a 70 69 53 74 61 72 74  e_int64 *piStart
3758c 43 68 69 6c 64 2c 0a 20 20 20 20 20 20 20 20 20  Child,.         
3758d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3758e 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f           sqlite_
3758f 69 6e 74 36 34 20 2a 70 69 45 6e 64 43 68 69 6c  int64 *piEndChil
37590 64 29 7b 0a 20 20 49 6e 74 65 72 69 6f 72 52 65  d){.  InteriorRe
37591 61 64 65 72 20 72 65 61 64 65 72 3b 0a 0a 20 20  ader reader;..  
37592 61 73 73 65 72 74 28 20 6e 44 61 74 61 3e 31 20  assert( nData>1 
37593 29 3b 0a 20 20 61 73 73 65 72 74 28 20 2a 70 44  );.  assert( *pD
37594 61 74 61 21 3d 27 5c 30 27 20 29 3b 0a 20 20 69  ata!='\0' );.  i
37595 6e 74 65 72 69 6f 72 52 65 61 64 65 72 49 6e 69  nteriorReaderIni
37596 74 28 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20  t(pData, nData, 
37597 26 72 65 61 64 65 72 29 3b 0a 0a 20 20 2f 2a 20  &reader);..  /* 
37598 53 63 61 6e 20 66 6f 72 20 74 68 65 20 66 69 72  Scan for the fir
37599 73 74 20 63 68 69 6c 64 20 77 68 69 63 68 20 63  st child which c
3759a 6f 75 6c 64 20 63 6f 6e 74 61 69 6e 20 70 54 65  ould contain pTe
3759b 72 6d 2f 6e 54 65 72 6d 2e 20 2a 2f 0a 20 20 77  rm/nTerm. */.  w
3759c 68 69 6c 65 28 20 21 69 6e 74 65 72 69 6f 72 52  hile( !interiorR
3759d 65 61 64 65 72 41 74 45 6e 64 28 26 72 65 61 64  eaderAtEnd(&read
3759e 65 72 29 20 29 7b 0a 20 20 20 20 69 66 28 20 69  er) ){.    if( i
3759f 6e 74 65 72 69 6f 72 52 65 61 64 65 72 54 65 72  nteriorReaderTer
375a0 6d 43 6d 70 28 26 72 65 61 64 65 72 2c 20 70 54  mCmp(&reader, pT
375a1 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 30 29 3e 30  erm, nTerm, 0)>0
375a2 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 6e   ) break;.    in
375a3 74 65 72 69 6f 72 52 65 61 64 65 72 53 74 65 70  teriorReaderStep
375a4 28 26 72 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20  (&reader);.  }. 
375a5 20 2a 70 69 53 74 61 72 74 43 68 69 6c 64 20 3d   *piStartChild =
375a6 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 43   interiorReaderC
375a7 75 72 72 65 6e 74 42 6c 6f 63 6b 69 64 28 26 72  urrentBlockid(&r
375a8 65 61 64 65 72 29 3b 0a 0a 20 20 2f 2a 20 4b 65  eader);..  /* Ke
375a9 65 70 20 73 63 61 6e 6e 69 6e 67 20 74 6f 20 66  ep scanning to f
375aa 69 6e 64 20 61 20 74 65 72 6d 20 67 72 65 61 74  ind a term great
375ab 65 72 20 74 68 61 6e 20 6f 75 72 20 74 65 72 6d  er than our term
375ac 2c 20 75 73 69 6e 67 20 70 72 65 66 69 78 0a 20  , using prefix. 
375ad 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69   ** comparison i
375ae 66 20 69 6e 64 69 63 61 74 65 64 2e 20 20 49 66  f indicated.  If
375af 20 69 73 50 72 65 66 69 78 20 69 73 20 66 61 6c   isPrefix is fal
375b0 73 65 2c 20 74 68 69 73 20 77 69 6c 6c 20 62 65  se, this will be
375b1 20 74 68 65 0a 20 20 2a 2a 20 73 61 6d 65 20 62   the.  ** same b
375b2 6c 6f 63 6b 69 64 20 61 73 20 74 68 65 20 73 74  lockid as the st
375b3 61 72 74 69 6e 67 20 62 6c 6f 63 6b 2e 0a 20 20  arting block..  
375b4 2a 2f 0a 20 20 77 68 69 6c 65 28 20 21 69 6e 74  */.  while( !int
375b5 65 72 69 6f 72 52 65 61 64 65 72 41 74 45 6e 64  eriorReaderAtEnd
375b6 28 26 72 65 61 64 65 72 29 20 29 7b 0a 20 20 20  (&reader) ){.   
375b7 20 69 66 28 20 69 6e 74 65 72 69 6f 72 52 65 61   if( interiorRea
375b8 64 65 72 54 65 72 6d 43 6d 70 28 26 72 65 61 64  derTermCmp(&read
375b9 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d  er, pTerm, nTerm
375ba 2c 20 69 73 50 72 65 66 69 78 29 3e 30 20 29 20  , isPrefix)>0 ) 
375bb 62 72 65 61 6b 3b 0a 20 20 20 20 69 6e 74 65 72  break;.    inter
375bc 69 6f 72 52 65 61 64 65 72 53 74 65 70 28 26 72  iorReaderStep(&r
375bd 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20 20 2a 70  eader);.  }.  *p
375be 69 45 6e 64 43 68 69 6c 64 20 3d 20 69 6e 74 65  iEndChild = inte
375bf 72 69 6f 72 52 65 61 64 65 72 43 75 72 72 65 6e  riorReaderCurren
375c0 74 42 6c 6f 63 6b 69 64 28 26 72 65 61 64 65 72  tBlockid(&reader
375c1 29 3b 0a 0a 20 20 69 6e 74 65 72 69 6f 72 52 65  );..  interiorRe
375c2 61 64 65 72 44 65 73 74 72 6f 79 28 26 72 65 61  aderDestroy(&rea
375c3 64 65 72 29 3b 0a 0a 20 20 2f 2a 20 43 68 69 6c  der);..  /* Chil
375c4 64 72 65 6e 20 6d 75 73 74 20 61 73 63 65 6e 64  dren must ascend
375c5 2c 20 61 6e 64 20 69 66 20 21 70 72 65 66 69 78  , and if !prefix
375c6 2c 20 62 6f 74 68 20 6d 75 73 74 20 62 65 20 74  , both must be t
375c7 68 65 20 73 61 6d 65 2e 20 2a 2f 0a 20 20 61 73  he same. */.  as
375c8 73 65 72 74 28 20 2a 70 69 45 6e 64 43 68 69 6c  sert( *piEndChil
375c9 64 3e 3d 2a 70 69 53 74 61 72 74 43 68 69 6c 64  d>=*piStartChild
375ca 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73   );.  assert( is
375cb 50 72 65 66 69 78 20 7c 7c 20 2a 70 69 53 74 61  Prefix || *piSta
375cc 72 74 43 68 69 6c 64 3d 3d 2a 70 69 45 6e 64 43  rtChild==*piEndC
375cd 68 69 6c 64 20 29 3b 0a 7d 0a 0a 2f 2a 20 52 65  hild );.}../* Re
375ce 61 64 20 62 6c 6f 63 6b 20 61 74 20 69 42 6c 6f  ad block at iBlo
375cf 63 6b 69 64 20 61 6e 64 20 70 61 73 73 20 69 74  ckid and pass it
375d0 20 77 69 74 68 20 6f 74 68 65 72 20 70 61 72 61   with other para
375d1 6d 73 20 74 6f 0a 2a 2a 20 67 65 74 43 68 69 6c  ms to.** getChil
375d2 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 29  drenContaining()
375d3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
375d4 6c 6f 61 64 41 6e 64 47 65 74 43 68 69 6c 64 72  loadAndGetChildr
375d5 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 0a 20 20  enContaining(.  
375d6 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
375d7 2c 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ,.  sqlite_int64
375d8 20 69 42 6c 6f 63 6b 69 64 2c 0a 20 20 63 6f 6e   iBlockid,.  con
375d9 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20  st char *pTerm, 
375da 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69  int nTerm, int i
375db 73 50 72 65 66 69 78 2c 0a 20 20 73 71 6c 69 74  sPrefix,.  sqlit
375dc 65 5f 69 6e 74 36 34 20 2a 70 69 53 74 61 72 74  e_int64 *piStart
375dd 43 68 69 6c 64 2c 20 73 71 6c 69 74 65 5f 69 6e  Child, sqlite_in
375de 74 36 34 20 2a 70 69 45 6e 64 43 68 69 6c 64 0a  t64 *piEndChild.
375df 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
375e0 74 20 2a 73 20 3d 20 4e 55 4c 4c 3b 0a 20 20 69  t *s = NULL;.  i
375e1 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
375e2 28 20 69 42 6c 6f 63 6b 69 64 21 3d 30 20 29 3b  ( iBlockid!=0 );
375e3 0a 20 20 61 73 73 65 72 74 28 20 70 54 65 72 6d  .  assert( pTerm
375e4 21 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65  !=NULL );.  asse
375e5 72 74 28 20 6e 54 65 72 6d 21 3d 30 20 29 3b 20  rt( nTerm!=0 ); 
375e6 20 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73         /* TODO(s
375e7 68 65 73 73 29 20 57 68 79 20 6e 6f 74 20 61 6c  hess) Why not al
375e8 6c 6f 77 20 74 68 69 73 3f 20 2a 2f 0a 20 20 61  low this? */.  a
375e9 73 73 65 72 74 28 20 70 69 53 74 61 72 74 43 68  ssert( piStartCh
375ea 69 6c 64 21 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61  ild!=NULL );.  a
375eb 73 73 65 72 74 28 20 70 69 45 6e 64 43 68 69 6c  ssert( piEndChil
375ec 64 21 3d 4e 55 4c 4c 20 29 3b 0a 0a 20 20 72 63  d!=NULL );..  rc
375ed 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65   = sql_get_state
375ee 6d 65 6e 74 28 76 2c 20 42 4c 4f 43 4b 5f 53 45  ment(v, BLOCK_SE
375ef 4c 45 43 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a  LECT_STMT, &s);.
375f0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
375f1 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
375f2 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
375f3 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31  _bind_int64(s, 1
375f4 2c 20 69 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69  , iBlockid);.  i
375f5 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
375f6 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
375f7 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74   rc = sqlite3_st
375f8 65 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63 3d  ep(s);.  if( rc=
375f9 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72  =SQLITE_DONE ) r
375fa 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
375fb 4f 52 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  OR;.  if( rc!=SQ
375fc 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72  LITE_ROW ) retur
375fd 6e 20 72 63 3b 0a 0a 20 20 67 65 74 43 68 69 6c  n rc;..  getChil
375fe 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 73  drenContaining(s
375ff 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
37600 6f 62 28 73 2c 20 30 29 2c 20 73 71 6c 69 74 65  ob(s, 0), sqlite
37601 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73  3_column_bytes(s
37602 2c 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20  , 0),.          
37603 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54                pT
37604 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72  erm, nTerm, isPr
37605 65 66 69 78 2c 20 70 69 53 74 61 72 74 43 68 69  efix, piStartChi
37606 6c 64 2c 20 70 69 45 6e 64 43 68 69 6c 64 29 3b  ld, piEndChild);
37607 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70 65 63 74  ..  /* We expect
37608 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e 20 20   only one row.  
37609 57 65 20 6d 75 73 74 20 65 78 65 63 75 74 65 20  We must execute 
3760a 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65 33 5f  another sqlite3_
3760b 73 74 65 70 28 29 0a 20 20 20 2a 20 74 6f 20 63  step().   * to c
3760c 6f 6d 70 6c 65 74 65 20 74 68 65 20 69 74 65 72  omplete the iter
3760d 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69 73 65  ation; otherwise
3760e 20 74 68 65 20 74 61 62 6c 65 20 77 69 6c 6c 20   the table will 
3760f 72 65 6d 61 69 6e 0a 20 20 20 2a 20 6c 6f 63 6b  remain.   * lock
37610 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71  ed. */.  rc = sq
37611 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20  lite3_step(s);. 
37612 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
37613 52 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ROW ) return SQL
37614 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28  ITE_ERROR;.  if(
37615 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc!=SQLITE_DONE
37616 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
37617 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
37618 4b 3b 0a 7d 0a 0a 2f 2a 20 54 72 61 76 65 72 73  K;.}../* Travers
37619 65 20 74 68 65 20 74 72 65 65 20 72 65 70 72 65  e the tree repre
3761a 73 65 6e 74 65 64 20 62 79 20 70 44 61 74 61 5b  sented by pData[
3761b 6e 44 61 74 61 5d 20 6c 6f 6f 6b 69 6e 67 20 66  nData] looking f
3761c 6f 72 0a 2a 2a 20 70 54 65 72 6d 5b 6e 54 65 72  or.** pTerm[nTer
3761d 6d 5d 2c 20 70 6c 61 63 69 6e 67 20 69 74 73 20  m], placing its 
3761e 64 6f 63 6c 69 73 74 20 69 6e 74 6f 20 2a 6f 75  doclist into *ou
3761f 74 2e 20 20 54 68 69 73 20 69 73 20 69 6e 74 65  t.  This is inte
37620 72 6e 61 6c 20 74 6f 0a 2a 2a 20 6c 6f 61 64 53  rnal to.** loadS
37621 65 67 6d 65 6e 74 28 29 20 74 6f 20 6d 61 6b 65  egment() to make
37622 20 65 72 72 6f 72 2d 68 61 6e 64 6c 69 6e 67 20   error-handling 
37623 63 6c 65 61 6e 65 72 2e 0a 2a 2f 0a 73 74 61 74  cleaner..*/.stat
37624 69 63 20 69 6e 74 20 6c 6f 61 64 53 65 67 6d 65  ic int loadSegme
37625 6e 74 49 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76  ntInt(fulltext_v
37626 74 61 62 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68  tab *v, const ch
37627 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e  ar *pData, int n
37628 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20  Data,.          
37629 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3762a 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c 65  sqlite_int64 iLe
3762b 61 76 65 73 45 6e 64 2c 0a 20 20 20 20 20 20 20  avesEnd,.       
3762c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3762d 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
3762e 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c  Term, int nTerm,
3762f 20 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20   int isPrefix,. 
37630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37631 20 20 20 20 20 20 20 20 20 44 61 74 61 42 75 66           DataBuf
37632 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20 2f 2a 20  fer *out){.  /* 
37633 53 70 65 63 69 61 6c 20 63 61 73 65 20 77 68 65  Special case whe
37634 72 65 20 72 6f 6f 74 20 69 73 20 61 20 6c 65 61  re root is a lea
37635 66 2e 20 2a 2f 0a 20 20 69 66 28 20 2a 70 44 61  f. */.  if( *pDa
37636 74 61 3d 3d 27 5c 30 27 20 29 7b 0a 20 20 20 20  ta=='\0' ){.    
37637 72 65 74 75 72 6e 20 6c 6f 61 64 53 65 67 6d 65  return loadSegme
37638 6e 74 4c 65 61 66 28 76 2c 20 70 44 61 74 61 2c  ntLeaf(v, pData,
37639 20 6e 44 61 74 61 2c 20 70 54 65 72 6d 2c 20 6e   nData, pTerm, n
3763a 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 20  Term, isPrefix, 
3763b 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  out);.  }else{. 
3763c 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 73     int rc;.    s
3763d 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61  qlite_int64 iSta
3763e 72 74 43 68 69 6c 64 2c 20 69 45 6e 64 43 68 69  rtChild, iEndChi
3763f 6c 64 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f 63  ld;..    /* Proc
37640 65 73 73 20 70 44 61 74 61 20 61 73 20 61 6e 20  ess pData as an 
37641 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2c 20 74  interior node, t
37642 68 65 6e 20 6c 6f 6f 70 20 64 6f 77 6e 20 74 68  hen loop down th
37643 65 20 74 72 65 65 0a 20 20 20 20 2a 2a 20 75 6e  e tree.    ** un
37644 74 69 6c 20 77 65 20 66 69 6e 64 20 74 68 65 20  til we find the 
37645 73 65 74 20 6f 66 20 6c 65 61 66 20 6e 6f 64 65  set of leaf node
37646 73 20 74 6f 20 73 63 61 6e 20 66 6f 72 20 74 68  s to scan for th
37647 65 20 74 65 72 6d 2e 0a 20 20 20 20 2a 2f 0a 20  e term..    */. 
37648 20 20 20 67 65 74 43 68 69 6c 64 72 65 6e 43 6f     getChildrenCo
37649 6e 74 61 69 6e 69 6e 67 28 70 44 61 74 61 2c 20  ntaining(pData, 
3764a 6e 44 61 74 61 2c 20 70 54 65 72 6d 2c 20 6e 54  nData, pTerm, nT
3764b 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 0a 20  erm, isPrefix,. 
3764c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3764d 20 20 20 20 20 20 20 20 20 26 69 53 74 61 72 74           &iStart
3764e 43 68 69 6c 64 2c 20 26 69 45 6e 64 43 68 69 6c  Child, &iEndChil
3764f 64 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69  d);.    while( i
37650 53 74 61 72 74 43 68 69 6c 64 3e 69 4c 65 61 76  StartChild>iLeav
37651 65 73 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 73  esEnd ){.      s
37652 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4e 65 78  qlite_int64 iNex
37653 74 53 74 61 72 74 2c 20 69 4e 65 78 74 45 6e 64  tStart, iNextEnd
37654 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 61  ;.      rc = loa
37655 64 41 6e 64 47 65 74 43 68 69 6c 64 72 65 6e 43  dAndGetChildrenC
37656 6f 6e 74 61 69 6e 69 6e 67 28 76 2c 20 69 53 74  ontaining(v, iSt
37657 61 72 74 43 68 69 6c 64 2c 20 70 54 65 72 6d 2c  artChild, pTerm,
37658 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78   nTerm, isPrefix
37659 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
3765a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3765b 20 20 20 20 20 20 20 20 20 20 26 69 4e 65 78 74            &iNext
3765c 53 74 61 72 74 2c 20 26 69 4e 65 78 74 45 6e 64  Start, &iNextEnd
3765d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
3765e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
3765f 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 20 20 2f  urn rc;..      /
37660 2a 20 49 66 20 77 65 27 76 65 20 62 72 61 6e 63  * If we've branc
37661 68 65 64 2c 20 66 6f 6c 6c 6f 77 20 74 68 65 20  hed, follow the 
37662 65 6e 64 20 62 72 61 6e 63 68 2c 20 74 6f 6f 2e  end branch, too.
37663 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 53   */.      if( iS
37664 74 61 72 74 43 68 69 6c 64 21 3d 69 45 6e 64 43  tartChild!=iEndC
37665 68 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 20 20  hild ){.        
37666 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 75  sqlite_int64 iDu
37667 6d 6d 79 3b 0a 20 20 20 20 20 20 20 20 72 63 20  mmy;.        rc 
37668 3d 20 6c 6f 61 64 41 6e 64 47 65 74 43 68 69 6c  = loadAndGetChil
37669 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 76  drenContaining(v
3766a 2c 20 69 45 6e 64 43 68 69 6c 64 2c 20 70 54 65  , iEndChild, pTe
3766b 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65  rm, nTerm, isPre
3766c 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20  fix,.           
3766d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3766e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
3766f 69 44 75 6d 6d 79 2c 20 26 69 4e 65 78 74 45 6e  iDummy, &iNextEn
37670 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  d);.        if( 
37671 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
37672 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
37673 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74   }..      assert
37674 28 20 69 4e 65 78 74 53 74 61 72 74 3c 3d 69 4e  ( iNextStart<=iN
37675 65 78 74 45 6e 64 20 29 3b 0a 20 20 20 20 20 20  extEnd );.      
37676 69 53 74 61 72 74 43 68 69 6c 64 20 3d 20 69 4e  iStartChild = iN
37677 65 78 74 53 74 61 72 74 3b 0a 20 20 20 20 20 20  extStart;.      
37678 69 45 6e 64 43 68 69 6c 64 20 3d 20 69 4e 65 78  iEndChild = iNex
37679 74 45 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20  tEnd;.    }.    
3767a 61 73 73 65 72 74 28 20 69 53 74 61 72 74 43 68  assert( iStartCh
3767b 69 6c 64 3c 3d 69 4c 65 61 76 65 73 45 6e 64 20  ild<=iLeavesEnd 
3767c 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  );.    assert( i
3767d 45 6e 64 43 68 69 6c 64 3c 3d 69 4c 65 61 76 65  EndChild<=iLeave
3767e 73 45 6e 64 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  sEnd );..    /* 
3767f 53 63 61 6e 20 74 68 72 6f 75 67 68 20 74 68 65  Scan through the
37680 20 6c 65 61 66 20 73 65 67 6d 65 6e 74 73 20 66   leaf segments f
37681 6f 72 20 64 6f 63 6c 69 73 74 73 2e 20 2a 2f 0a  or doclists. */.
37682 20 20 20 20 72 65 74 75 72 6e 20 6c 6f 61 64 53      return loadS
37683 65 67 6d 65 6e 74 4c 65 61 76 65 73 28 76 2c 20  egmentLeaves(v, 
37684 69 53 74 61 72 74 43 68 69 6c 64 2c 20 69 45 6e  iStartChild, iEn
37685 64 43 68 69 6c 64 2c 0a 20 20 20 20 20 20 20 20  dChild,.        
37686 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37687 20 20 20 20 20 70 54 65 72 6d 2c 20 6e 54 65 72       pTerm, nTer
37688 6d 2c 20 69 73 50 72 65 66 69 78 2c 20 6f 75 74  m, isPrefix, out
37689 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 43 61 6c  );.  }.}../* Cal
3768a 6c 20 6c 6f 61 64 53 65 67 6d 65 6e 74 49 6e 74  l loadSegmentInt
3768b 28 29 20 74 6f 20 63 6f 6c 6c 65 63 74 20 74 68  () to collect th
3768c 65 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 70 54  e doclist for pT
3768d 65 72 6d 2f 6e 54 65 72 6d 2c 20 74 68 65 6e 0a  erm/nTerm, then.
3768e 2a 2a 20 6d 65 72 67 65 20 69 74 73 20 64 6f 63  ** merge its doc
3768f 6c 69 73 74 20 6f 76 65 72 20 2a 6f 75 74 20 28  list over *out (
37690 61 6e 79 20 64 75 70 6c 69 63 61 74 65 20 64 6f  any duplicate do
37691 63 6c 69 73 74 73 20 72 65 61 64 20 66 72 6f 6d  clists read from
37692 20 74 68 65 0a 2a 2a 20 73 65 67 6d 65 6e 74 20   the.** segment 
37693 72 6f 6f 74 65 64 20 61 74 20 70 44 61 74 61 20  rooted at pData 
37694 77 69 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 74  will overwrite t
37695 68 6f 73 65 20 69 6e 20 2a 6f 75 74 29 2e 0a 2a  hose in *out)..*
37696 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
37697 20 43 6f 6e 73 69 64 65 72 20 63 68 61 6e 67 69   Consider changi
37698 6e 67 20 74 68 69 73 20 74 6f 20 64 65 74 65 72  ng this to deter
37699 6d 69 6e 65 20 74 68 65 20 64 65 70 74 68 20 6f  mine the depth o
3769a 66 20 74 68 65 0a 2a 2a 20 6c 65 61 76 65 73 20  f the.** leaves 
3769b 75 73 69 6e 67 20 65 69 74 68 65 72 20 74 68 65  using either the
3769c 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72   first character
3769d 73 20 6f 66 20 69 6e 74 65 72 69 6f 72 20 6e 6f  s of interior no
3769e 64 65 73 20 28 77 68 65 6e 0a 2a 2a 20 3d 3d 31  des (when.** ==1
3769f 2c 20 77 65 27 72 65 20 6f 6e 65 20 6c 65 76 65  , we're one leve
376a0 6c 20 61 62 6f 76 65 20 74 68 65 20 6c 65 61 76  l above the leav
376a1 65 73 29 2c 20 6f 72 20 74 68 65 20 66 69 72 73  es), or the firs
376a2 74 20 63 68 61 72 61 63 74 65 72 20 6f 66 0a 2a  t character of.*
376a3 2a 20 74 68 65 20 72 6f 6f 74 20 28 77 68 69 63  * the root (whic
376a4 68 20 77 69 6c 6c 20 64 65 73 63 72 69 62 65 20  h will describe 
376a5 74 68 65 20 68 65 69 67 68 74 20 6f 66 20 74 68  the height of th
376a6 65 20 74 72 65 65 20 64 69 72 65 63 74 6c 79 29  e tree directly)
376a7 2e 0a 2a 2a 20 45 69 74 68 65 72 20 66 65 65 6c  ..** Either feel
376a8 73 20 73 6f 6d 65 77 68 61 74 20 74 72 69 63 6b  s somewhat trick
376a9 79 20 74 6f 20 6d 65 2e 0a 2a 2f 0a 2f 2a 20 54  y to me..*/./* T
376aa 4f 44 4f 28 73 68 65 73 73 29 20 54 68 65 20 63  ODO(shess) The c
376ab 75 72 72 65 6e 74 20 6d 65 72 67 65 20 69 73 20  urrent merge is 
376ac 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 73 6c 6f  likely to be slo
376ad 77 20 66 6f 72 20 6c 61 72 67 65 0a 2a 2a 20 64  w for large.** d
376ae 6f 63 6c 69 73 74 73 20 28 74 68 6f 75 67 68 20  oclists (though 
376af 69 74 20 73 68 6f 75 6c 64 20 70 72 6f 63 65 73  it should proces
376b0 73 20 66 72 6f 6d 20 6e 65 77 65 73 74 2f 73 6d  s from newest/sm
376b1 61 6c 6c 65 73 74 20 74 6f 0a 2a 2a 20 6f 6c 64  allest to.** old
376b2 65 73 74 2f 6c 61 72 67 65 73 74 2c 20 73 6f 20  est/largest, so 
376b3 69 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 68  it may not be th
376b4 61 74 20 62 61 64 29 2e 20 20 49 74 20 6d 69 67  at bad).  It mig
376b5 68 74 20 62 65 20 75 73 65 66 75 6c 20 74 6f 0a  ht be useful to.
376b6 2a 2a 20 6d 6f 64 69 66 79 20 74 68 69 6e 67 73  ** modify things
376b7 20 74 6f 20 61 6c 6c 6f 77 20 66 6f 72 20 4e 2d   to allow for N-
376b8 77 61 79 20 6d 65 72 67 69 6e 67 2e 20 20 54 68  way merging.  Th
376b9 69 73 20 63 6f 75 6c 64 20 65 69 74 68 65 72 20  is could either 
376ba 62 65 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 73  be.** within a s
376bb 65 67 6d 65 6e 74 2c 20 77 69 74 68 20 70 61 69  egment, with pai
376bc 72 77 69 73 65 20 6d 65 72 67 65 73 20 61 63 72  rwise merges acr
376bd 6f 73 73 20 73 65 67 6d 65 6e 74 73 2c 20 6f 72  oss segments, or
376be 20 61 63 72 6f 73 73 0a 2a 2a 20 61 6c 6c 20 73   across.** all s
376bf 65 67 6d 65 6e 74 73 20 61 74 20 6f 6e 63 65 2e  egments at once.
376c0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
376c1 6f 61 64 53 65 67 6d 65 6e 74 28 66 75 6c 6c 74  oadSegment(fullt
376c2 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 63 6f 6e  ext_vtab *v, con
376c3 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
376c4 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20  int nData,.     
376c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
376c6 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
376c7 4c 65 61 76 65 73 45 6e 64 2c 0a 20 20 20 20 20  LeavesEnd,.     
376c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
376c9 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
376ca 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20  erm, int nTerm, 
376cb 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20  int isPrefix,.  
376cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
376cd 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20       DataBuffer 
376ce 2a 6f 75 74 29 7b 0a 20 20 44 61 74 61 42 75 66  *out){.  DataBuf
376cf 66 65 72 20 72 65 73 75 6c 74 3b 0a 20 20 69 6e  fer result;.  in
376d0 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
376d1 20 6e 44 61 74 61 3e 31 20 29 3b 0a 0a 20 20 2f   nData>1 );..  /
376d2 2a 20 54 68 69 73 20 63 6f 64 65 20 73 68 6f 75  * This code shou
376d3 6c 64 20 6e 65 76 65 72 20 62 65 20 63 61 6c 6c  ld never be call
376d4 65 64 20 77 69 74 68 20 62 75 66 66 65 72 65 64  ed with buffered
376d5 20 75 70 64 61 74 65 73 2e 20 2a 2f 0a 20 20 61   updates. */.  a
376d6 73 73 65 72 74 28 20 76 2d 3e 6e 50 65 6e 64 69  ssert( v->nPendi
376d7 6e 67 44 61 74 61 3c 30 20 29 3b 0a 0a 20 20 64  ngData<0 );..  d
376d8 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 72  ataBufferInit(&r
376d9 65 73 75 6c 74 2c 20 30 29 3b 0a 20 20 72 63 20  esult, 0);.  rc 
376da 3d 20 6c 6f 61 64 53 65 67 6d 65 6e 74 49 6e 74  = loadSegmentInt
376db 28 76 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61  (v, pData, nData
376dc 2c 20 69 4c 65 61 76 65 73 45 6e 64 2c 0a 20 20  , iLeavesEnd,.  
376dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
376de 20 20 20 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d      pTerm, nTerm
376df 2c 20 69 73 50 72 65 66 69 78 2c 20 26 72 65 73  , isPrefix, &res
376e0 75 6c 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  ult);.  if( rc==
376e1 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65 73  SQLITE_OK && res
376e2 75 6c 74 2e 6e 44 61 74 61 3e 30 20 29 7b 0a 20  ult.nData>0 ){. 
376e3 20 20 20 69 66 28 20 6f 75 74 2d 3e 6e 44 61 74     if( out->nDat
376e4 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 44 61  a==0 ){.      Da
376e5 74 61 42 75 66 66 65 72 20 74 6d 70 20 3d 20 2a  taBuffer tmp = *
376e6 6f 75 74 3b 0a 20 20 20 20 20 20 2a 6f 75 74 20  out;.      *out 
376e7 3d 20 72 65 73 75 6c 74 3b 0a 20 20 20 20 20 20  = result;.      
376e8 72 65 73 75 6c 74 20 3d 20 74 6d 70 3b 0a 20 20  result = tmp;.  
376e9 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 44    }else{.      D
376ea 61 74 61 42 75 66 66 65 72 20 6d 65 72 67 65 64  ataBuffer merged
376eb 3b 0a 20 20 20 20 20 20 44 4c 52 65 61 64 65 72  ;.      DLReader
376ec 20 72 65 61 64 65 72 73 5b 32 5d 3b 0a 0a 20 20   readers[2];..  
376ed 20 20 20 20 64 6c 72 49 6e 69 74 28 26 72 65 61      dlrInit(&rea
376ee 64 65 72 73 5b 30 5d 2c 20 44 4c 5f 44 45 46 41  ders[0], DL_DEFA
376ef 55 4c 54 2c 20 6f 75 74 2d 3e 70 44 61 74 61 2c  ULT, out->pData,
376f0 20 6f 75 74 2d 3e 6e 44 61 74 61 29 3b 0a 20 20   out->nData);.  
376f1 20 20 20 20 64 6c 72 49 6e 69 74 28 26 72 65 61      dlrInit(&rea
376f2 64 65 72 73 5b 31 5d 2c 20 44 4c 5f 44 45 46 41  ders[1], DL_DEFA
376f3 55 4c 54 2c 20 72 65 73 75 6c 74 2e 70 44 61 74  ULT, result.pDat
376f4 61 2c 20 72 65 73 75 6c 74 2e 6e 44 61 74 61 29  a, result.nData)
376f5 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66  ;.      dataBuff
376f6 65 72 49 6e 69 74 28 26 6d 65 72 67 65 64 2c 20  erInit(&merged, 
376f7 6f 75 74 2d 3e 6e 44 61 74 61 2b 72 65 73 75 6c  out->nData+resul
376f8 74 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20  t.nData);.      
376f9 64 6f 63 4c 69 73 74 4d 65 72 67 65 28 26 6d 65  docListMerge(&me
376fa 72 67 65 64 2c 20 72 65 61 64 65 72 73 2c 20 32  rged, readers, 2
376fb 29 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75 66  );.      dataBuf
376fc 66 65 72 44 65 73 74 72 6f 79 28 6f 75 74 29 3b  ferDestroy(out);
376fd 0a 20 20 20 20 20 20 2a 6f 75 74 20 3d 20 6d 65  .      *out = me
376fe 72 67 65 64 3b 0a 20 20 20 20 20 20 64 6c 72 44  rged;.      dlrD
376ff 65 73 74 72 6f 79 28 26 72 65 61 64 65 72 73 5b  estroy(&readers[
37700 30 5d 29 3b 0a 20 20 20 20 20 20 64 6c 72 44 65  0]);.      dlrDe
37701 73 74 72 6f 79 28 26 72 65 61 64 65 72 73 5b 31  stroy(&readers[1
37702 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ]);.    }.  }.  
37703 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
37704 79 28 26 72 65 73 75 6c 74 29 3b 0a 20 20 72 65  y(&result);.  re
37705 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 53  turn rc;.}../* S
37706 63 61 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  can the database
37707 20 61 6e 64 20 6d 65 72 67 65 20 74 6f 67 65 74   and merge toget
37708 68 65 72 20 74 68 65 20 70 6f 73 74 69 6e 67 20  her the posting 
37709 6c 69 73 74 73 20 66 6f 72 20 74 68 65 20 74 65  lists for the te
3770a 72 6d 0a 2a 2a 20 69 6e 74 6f 20 2a 6f 75 74 2e  rm.** into *out.
3770b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74  .*/.static int t
3770c 65 72 6d 53 65 6c 65 63 74 28 0a 20 20 66 75 6c  ermSelect(.  ful
3770d 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 0a  ltext_vtab *v, .
3770e 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 0a 20    int iColumn,. 
3770f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65   const char *pTe
37710 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 20  rm, int nTerm,  
37711 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65             /* Te
37712 72 6d 20 74 6f 20 71 75 65 72 79 20 66 6f 72 20  rm to query for 
37713 2a 2f 0a 20 20 69 6e 74 20 69 73 50 72 65 66 69  */.  int isPrefi
37714 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x,              
37715 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37716 2a 20 54 72 75 65 20 66 6f 72 20 61 20 70 72 65  * True for a pre
37717 66 69 78 20 73 65 61 72 63 68 20 2a 2f 0a 20 20  fix search */.  
37718 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79 70  DocListType iTyp
37719 65 2c 20 0a 20 20 44 61 74 61 42 75 66 66 65 72  e, .  DataBuffer
3771a 20 2a 6f 75 74 20 20 20 20 20 20 20 20 20 20 20   *out           
3771b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3771c 2f 2a 20 57 72 69 74 65 20 72 65 73 75 6c 74 73  /* Write results
3771d 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 61   here */.){.  Da
3771e 74 61 42 75 66 66 65 72 20 64 6f 63 6c 69 73 74  taBuffer doclist
3771f 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  ;.  sqlite3_stmt
37720 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20   *s;.  int rc = 
37721 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e  sql_get_statemen
37722 74 28 76 2c 20 53 45 47 44 49 52 5f 53 45 4c 45  t(v, SEGDIR_SELE
37723 43 54 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26 73 29  CT_ALL_STMT, &s)
37724 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
37725 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
37726 63 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f  c;..  /* This co
37727 64 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20  de should never 
37728 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68 20 62  be called with b
37729 75 66 66 65 72 65 64 20 75 70 64 61 74 65 73 2e  uffered updates.
3772a 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 76 2d   */.  assert( v-
3772b 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c 30 20  >nPendingData<0 
3772c 29 3b 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72  );..  dataBuffer
3772d 49 6e 69 74 28 26 64 6f 63 6c 69 73 74 2c 20 30  Init(&doclist, 0
3772e 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 49  );.  dataBufferI
3772f 6e 69 74 28 6f 75 74 2c 20 30 29 3b 0a 0a 20 20  nit(out, 0);..  
37730 2f 2a 20 54 72 61 76 65 72 73 65 20 74 68 65 20  /* Traverse the 
37731 73 65 67 6d 65 6e 74 73 20 66 72 6f 6d 20 6f 6c  segments from ol
37732 64 65 73 74 20 74 6f 20 6e 65 77 65 73 74 20 73  dest to newest s
37733 6f 20 74 68 61 74 20 6e 65 77 65 72 20 64 6f 63  o that newer doc
37734 6c 69 73 74 0a 20 20 2a 2a 20 65 6c 65 6d 65 6e  list.  ** elemen
37735 74 73 20 66 6f 72 20 67 69 76 65 6e 20 64 6f 63  ts for given doc
37736 69 64 73 20 6f 76 65 72 77 72 69 74 65 20 6f 6c  ids overwrite ol
37737 64 65 72 20 65 6c 65 6d 65 6e 74 73 2e 0a 20 20  der elements..  
37738 2a 2f 0a 20 20 77 68 69 6c 65 28 20 28 72 63 20  */.  while( (rc 
37739 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73  = sqlite3_step(s
3773a 29 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  ))==SQLITE_ROW )
3773b 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
3773c 20 2a 70 44 61 74 61 20 3d 20 73 71 6c 69 74 65   *pData = sqlite
3773d 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 2c  3_column_blob(s,
3773e 20 32 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69   2);.    const i
3773f 6e 74 20 6e 44 61 74 61 20 3d 20 73 71 6c 69 74  nt nData = sqlit
37740 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
37741 73 2c 20 32 29 3b 0a 20 20 20 20 63 6f 6e 73 74  s, 2);.    const
37742 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c   sqlite_int64 iL
37743 65 61 76 65 73 45 6e 64 20 3d 20 73 71 6c 69 74  eavesEnd = sqlit
37744 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28  e3_column_int64(
37745 73 2c 20 31 29 3b 0a 20 20 20 20 72 63 20 3d 20  s, 1);.    rc = 
37746 6c 6f 61 64 53 65 67 6d 65 6e 74 28 76 2c 20 70  loadSegment(v, p
37747 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 69 4c 65  Data, nData, iLe
37748 61 76 65 73 45 6e 64 2c 20 70 54 65 72 6d 2c 20  avesEnd, pTerm, 
37749 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c  nTerm, isPrefix,
3774a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3774b 20 20 20 20 20 20 26 64 6f 63 6c 69 73 74 29 3b        &doclist);
3774c 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
3774d 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72  ITE_OK ) goto er
3774e 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  r;.  }.  if( rc=
3774f 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a  =SQLITE_DONE ){.
37750 20 20 20 20 69 66 28 20 64 6f 63 6c 69 73 74 2e      if( doclist.
37751 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20  nData!=0 ){.    
37752 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
37753 20 54 68 65 20 6f 6c 64 20 74 65 72 6d 5f 73 65   The old term_se
37754 6c 65 63 74 5f 61 6c 6c 28 29 20 63 6f 64 65 20  lect_all() code 
37755 61 70 70 6c 69 65 64 20 74 68 65 20 63 6f 6c 75  applied the colu
37756 6d 6e 0a 20 20 20 20 20 20 2a 2a 20 72 65 73 74  mn.      ** rest
37757 72 69 63 74 20 61 73 20 77 65 20 6d 65 72 67 65  rict as we merge
37758 64 20 73 65 67 6d 65 6e 74 73 2c 20 6c 65 61 64  d segments, lead
37759 69 6e 67 20 74 6f 20 73 6d 61 6c 6c 65 72 20 62  ing to smaller b
3775a 75 66 66 65 72 73 2e 0a 20 20 20 20 20 20 2a 2a  uffers..      **
3775b 20 54 68 69 73 20 69 73 20 70 72 6f 62 61 62 6c   This is probabl
3775c 79 20 77 6f 72 74 68 77 68 69 6c 65 20 74 6f 20  y worthwhile to 
3775d 62 72 69 6e 67 20 62 61 63 6b 2c 20 6f 6e 63 65  bring back, once
3775e 20 74 68 65 20 6e 65 77 20 73 74 6f 72 61 67 65   the new storage
3775f 0a 20 20 20 20 20 20 2a 2a 20 73 79 73 74 65 6d  .      ** system
37760 20 69 73 20 63 68 65 63 6b 65 64 20 69 6e 2e 0a   is checked in..
37761 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
37762 66 28 20 69 43 6f 6c 75 6d 6e 3d 3d 76 2d 3e 6e  f( iColumn==v->n
37763 43 6f 6c 75 6d 6e 29 20 69 43 6f 6c 75 6d 6e 20  Column) iColumn 
37764 3d 20 2d 31 3b 0a 20 20 20 20 20 20 64 6f 63 4c  = -1;.      docL
37765 69 73 74 54 72 69 6d 28 44 4c 5f 44 45 46 41 55  istTrim(DL_DEFAU
37766 4c 54 2c 20 64 6f 63 6c 69 73 74 2e 70 44 61 74  LT, doclist.pDat
37767 61 2c 20 64 6f 63 6c 69 73 74 2e 6e 44 61 74 61  a, doclist.nData
37768 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
37769 20 20 20 20 69 43 6f 6c 75 6d 6e 2c 20 69 54 79      iColumn, iTy
3776a 70 65 2c 20 6f 75 74 29 3b 0a 20 20 20 20 7d 0a  pe, out);.    }.
3776b 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
3776c 4f 4b 3b 0a 20 20 7d 0a 0a 20 65 72 72 3a 0a 20  OK;.  }.. err:. 
3776d 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72   dataBufferDestr
3776e 6f 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20  oy(&doclist);.  
3776f 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
37770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37771 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37772 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37773 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
37774 0a 2f 2a 20 55 73 65 64 20 74 6f 20 68 6f 6c 64  ./* Used to hold
37775 20 68 61 73 68 74 61 62 6c 65 20 64 61 74 61 20   hashtable data 
37776 66 6f 72 20 73 6f 72 74 69 6e 67 2e 20 2a 2f 0a  for sorting. */.
37777 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54  typedef struct T
37778 65 72 6d 44 61 74 61 20 7b 0a 20 20 63 6f 6e 73  ermData {.  cons
37779 74 20 63 68 61 72 20 2a 70 54 65 72 6d 3b 0a 20  t char *pTerm;. 
3777a 20 69 6e 74 20 6e 54 65 72 6d 3b 0a 20 20 44 4c   int nTerm;.  DL
3777b 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c  Collector *pColl
3777c 65 63 74 6f 72 3b 0a 7d 20 54 65 72 6d 44 61 74  ector;.} TermDat
3777d 61 3b 0a 0a 2f 2a 20 4f 72 64 65 72 73 20 54 65  a;../* Orders Te
3777e 72 6d 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 20  rmData elements 
3777f 69 6e 20 73 74 72 63 6d 70 20 66 61 73 68 69 6f  in strcmp fashio
37780 6e 20 28 20 3c 30 20 66 6f 72 20 6c 65 73 73 2d  n ( <0 for less-
37781 74 68 61 6e 2c 20 30 0a 2a 2a 20 66 6f 72 20 65  than, 0.** for e
37782 71 75 61 6c 2c 20 3e 30 20 66 6f 72 20 67 72 65  qual, >0 for gre
37783 61 74 65 72 2d 74 68 61 6e 29 2e 0a 2a 2f 0a 73  ater-than)..*/.s
37784 74 61 74 69 63 20 69 6e 74 20 74 65 72 6d 44 61  tatic int termDa
37785 74 61 43 6d 70 28 63 6f 6e 73 74 20 76 6f 69 64  taCmp(const void
37786 20 2a 61 76 2c 20 63 6f 6e 73 74 20 76 6f 69 64   *av, const void
37787 20 2a 62 76 29 7b 0a 20 20 63 6f 6e 73 74 20 54   *bv){.  const T
37788 65 72 6d 44 61 74 61 20 2a 61 20 3d 20 28 63 6f  ermData *a = (co
37789 6e 73 74 20 54 65 72 6d 44 61 74 61 20 2a 29 61  nst TermData *)a
3778a 76 3b 0a 20 20 63 6f 6e 73 74 20 54 65 72 6d 44  v;.  const TermD
3778b 61 74 61 20 2a 62 20 3d 20 28 63 6f 6e 73 74 20  ata *b = (const 
3778c 54 65 72 6d 44 61 74 61 20 2a 29 62 76 3b 0a 20  TermData *)bv;. 
3778d 20 69 6e 74 20 6e 20 3d 20 61 2d 3e 6e 54 65 72   int n = a->nTer
3778e 6d 3c 62 2d 3e 6e 54 65 72 6d 20 3f 20 61 2d 3e  m<b->nTerm ? a->
3778f 6e 54 65 72 6d 20 3a 20 62 2d 3e 6e 54 65 72 6d  nTerm : b->nTerm
37790 3b 0a 20 20 69 6e 74 20 63 20 3d 20 6d 65 6d 63  ;.  int c = memc
37791 6d 70 28 61 2d 3e 70 54 65 72 6d 2c 20 62 2d 3e  mp(a->pTerm, b->
37792 70 54 65 72 6d 2c 20 6e 29 3b 0a 20 20 69 66 28  pTerm, n);.  if(
37793 20 63 21 3d 30 20 29 20 72 65 74 75 72 6e 20 63   c!=0 ) return c
37794 3b 0a 20 20 72 65 74 75 72 6e 20 61 2d 3e 6e 54  ;.  return a->nT
37795 65 72 6d 2d 62 2d 3e 6e 54 65 72 6d 3b 0a 7d 0a  erm-b->nTerm;.}.
37796 0a 2f 2a 20 4f 72 64 65 72 20 70 54 65 72 6d 73  ./* Order pTerms
37797 20 64 61 74 61 20 62 79 20 74 65 72 6d 2c 20 74   data by term, t
37798 68 65 6e 20 77 72 69 74 65 20 61 20 6e 65 77 20  hen write a new 
37799 6c 65 76 65 6c 20 30 20 73 65 67 6d 65 6e 74 20  level 0 segment 
3779a 75 73 69 6e 67 0a 2a 2a 20 4c 65 61 66 57 72 69  using.** LeafWri
3779b 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ter..*/.static i
3779c 6e 74 20 77 72 69 74 65 5a 65 72 6f 53 65 67 6d  nt writeZeroSegm
3779d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  ent(fulltext_vta
3779e 62 20 2a 76 2c 20 66 74 73 33 48 61 73 68 20 2a  b *v, fts3Hash *
3779f 70 54 65 72 6d 73 29 7b 0a 20 20 66 74 73 33 48  pTerms){.  fts3H
377a0 61 73 68 45 6c 65 6d 20 2a 65 3b 0a 20 20 69 6e  ashElem *e;.  in
377a1 74 20 69 64 78 2c 20 72 63 2c 20 69 2c 20 6e 3b  t idx, rc, i, n;
377a2 0a 20 20 54 65 72 6d 44 61 74 61 20 2a 70 44 61  .  TermData *pDa
377a3 74 61 3b 0a 20 20 4c 65 61 66 57 72 69 74 65 72  ta;.  LeafWriter
377a4 20 77 72 69 74 65 72 3b 0a 20 20 44 61 74 61 42   writer;.  DataB
377a5 75 66 66 65 72 20 64 6c 3b 0a 0a 20 20 2f 2a 20  uffer dl;..  /* 
377a6 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6e 65  Determine the ne
377a7 78 74 20 69 6e 64 65 78 20 61 74 20 6c 65 76 65  xt index at leve
377a8 6c 20 30 2c 20 6d 65 72 67 69 6e 67 20 61 73 20  l 0, merging as 
377a9 6e 65 63 65 73 73 61 72 79 2e 20 2a 2f 0a 20 20  necessary. */.  
377aa 72 63 20 3d 20 73 65 67 64 69 72 4e 65 78 74 49  rc = segdirNextI
377ab 6e 64 65 78 28 76 2c 20 30 2c 20 26 69 64 78 29  ndex(v, 0, &idx)
377ac 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
377ad 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
377ae 63 3b 0a 0a 20 20 6e 20 3d 20 66 74 73 33 48 61  c;..  n = fts3Ha
377af 73 68 43 6f 75 6e 74 28 70 54 65 72 6d 73 29 3b  shCount(pTerms);
377b0 0a 20 20 70 44 61 74 61 20 3d 20 73 71 6c 69 74  .  pData = sqlit
377b1 65 33 5f 6d 61 6c 6c 6f 63 28 6e 2a 73 69 7a 65  e3_malloc(n*size
377b2 6f 66 28 54 65 72 6d 44 61 74 61 29 29 3b 0a 0a  of(TermData));..
377b3 20 20 66 6f 72 28 69 20 3d 20 30 2c 20 65 20 3d    for(i = 0, e =
377b4 20 66 74 73 33 48 61 73 68 46 69 72 73 74 28 70   fts3HashFirst(p
377b5 54 65 72 6d 73 29 3b 20 65 3b 20 69 2b 2b 2c 20  Terms); e; i++, 
377b6 65 20 3d 20 66 74 73 33 48 61 73 68 4e 65 78 74  e = fts3HashNext
377b7 28 65 29 29 7b 0a 20 20 20 20 61 73 73 65 72 74  (e)){.    assert
377b8 28 20 69 3c 6e 20 29 3b 0a 20 20 20 20 70 44 61  ( i<n );.    pDa
377b9 74 61 5b 69 5d 2e 70 54 65 72 6d 20 3d 20 66 74  ta[i].pTerm = ft
377ba 73 33 48 61 73 68 4b 65 79 28 65 29 3b 0a 20 20  s3HashKey(e);.  
377bb 20 20 70 44 61 74 61 5b 69 5d 2e 6e 54 65 72 6d    pData[i].nTerm
377bc 20 3d 20 66 74 73 33 48 61 73 68 4b 65 79 73 69   = fts3HashKeysi
377bd 7a 65 28 65 29 3b 0a 20 20 20 20 70 44 61 74 61  ze(e);.    pData
377be 5b 69 5d 2e 70 43 6f 6c 6c 65 63 74 6f 72 20 3d  [i].pCollector =
377bf 20 66 74 73 33 48 61 73 68 44 61 74 61 28 65 29   fts3HashData(e)
377c0 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
377c1 69 3d 3d 6e 20 29 3b 0a 0a 20 20 2f 2a 20 54 4f  i==n );..  /* TO
377c2 44 4f 28 73 68 65 73 73 29 20 53 68 6f 75 6c 64  DO(shess) Should
377c3 20 77 65 20 61 6c 6c 6f 77 20 75 73 65 72 2d 64   we allow user-d
377c4 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  efined collation
377c5 20 73 65 71 75 65 6e 63 65 73 2c 0a 20 20 2a 2a   sequences,.  **
377c6 20 68 65 72 65 3f 20 20 49 20 74 68 69 6e 6b 20   here?  I think 
377c7 77 65 20 6f 6e 6c 79 20 6e 65 65 64 20 74 68 61  we only need tha
377c8 74 20 6f 6e 63 65 20 77 65 20 73 75 70 70 6f 72  t once we suppor
377c9 74 20 70 72 65 66 69 78 20 73 65 61 72 63 68 65  t prefix searche
377ca 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 3e  s..  */.  if( n>
377cb 31 20 29 20 71 73 6f 72 74 28 70 44 61 74 61 2c  1 ) qsort(pData,
377cc 20 6e 2c 20 73 69 7a 65 6f 66 28 2a 70 44 61 74   n, sizeof(*pDat
377cd 61 29 2c 20 74 65 72 6d 44 61 74 61 43 6d 70 29  a), termDataCmp)
377ce 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65  ;..  /* TODO(she
377cf 73 73 29 20 52 65 66 61 63 74 6f 72 20 73 6f 20  ss) Refactor so 
377d0 74 68 61 74 20 77 65 20 63 61 6e 20 77 72 69 74  that we can writ
377d1 65 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68  e directly to th
377d2 65 20 73 65 67 6d 65 6e 74 0a 20 20 2a 2a 20 44  e segment.  ** D
377d3 61 74 61 42 75 66 66 65 72 2c 20 61 73 20 68 61  ataBuffer, as ha
377d4 70 70 65 6e 73 20 66 6f 72 20 73 65 67 6d 65 6e  ppens for segmen
377d5 74 20 6d 65 72 67 65 73 2e 0a 20 20 2a 2f 0a 20  t merges..  */. 
377d6 20 6c 65 61 66 57 72 69 74 65 72 49 6e 69 74 28   leafWriterInit(
377d7 30 2c 20 69 64 78 2c 20 26 77 72 69 74 65 72 29  0, idx, &writer)
377d8 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e  ;.  dataBufferIn
377d9 69 74 28 26 64 6c 2c 20 30 29 3b 0a 20 20 66 6f  it(&dl, 0);.  fo
377da 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29  r(i=0; i<n; i++)
377db 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  {.    dataBuffer
377dc 52 65 73 65 74 28 26 64 6c 29 3b 0a 20 20 20 20  Reset(&dl);.    
377dd 64 6c 63 41 64 64 44 6f 63 6c 69 73 74 28 70 44  dlcAddDoclist(pD
377de 61 74 61 5b 69 5d 2e 70 43 6f 6c 6c 65 63 74 6f  ata[i].pCollecto
377df 72 2c 20 26 64 6c 29 3b 0a 20 20 20 20 72 63 20  r, &dl);.    rc 
377e0 3d 20 6c 65 61 66 57 72 69 74 65 72 53 74 65 70  = leafWriterStep
377e1 28 76 2c 20 26 77 72 69 74 65 72 2c 0a 20 20 20  (v, &writer,.   
377e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
377e3 20 20 20 20 20 70 44 61 74 61 5b 69 5d 2e 70 54       pData[i].pT
377e4 65 72 6d 2c 20 70 44 61 74 61 5b 69 5d 2e 6e 54  erm, pData[i].nT
377e5 65 72 6d 2c 20 64 6c 2e 70 44 61 74 61 2c 20 64  erm, dl.pData, d
377e6 6c 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 69 66  l.nData);.    if
377e7 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
377e8 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20 7d 0a  ) goto err;.  }.
377e9 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65    rc = leafWrite
377ea 72 46 69 6e 61 6c 69 7a 65 28 76 2c 20 26 77 72  rFinalize(v, &wr
377eb 69 74 65 72 29 3b 0a 0a 20 65 72 72 3a 0a 20 20  iter);.. err:.  
377ec 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
377ed 79 28 26 64 6c 29 3b 0a 20 20 73 71 6c 69 74 65  y(&dl);.  sqlite
377ee 33 5f 66 72 65 65 28 70 44 61 74 61 29 3b 0a 20  3_free(pData);. 
377ef 20 6c 65 61 66 57 72 69 74 65 72 44 65 73 74 72   leafWriterDestr
377f0 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 20 20 72  oy(&writer);.  r
377f1 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
377f2 49 66 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20  If pendingTerms 
377f3 68 61 73 20 64 61 74 61 2c 20 66 72 65 65 20 69  has data, free i
377f4 74 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t. */.static int
377f5 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72   clearPendingTer
377f6 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ms(fulltext_vtab
377f7 20 2a 76 29 7b 0a 20 20 69 66 28 20 76 2d 3e 6e   *v){.  if( v->n
377f8 50 65 6e 64 69 6e 67 44 61 74 61 3e 3d 30 20 29  PendingData>=0 )
377f9 7b 0a 20 20 20 20 66 74 73 33 48 61 73 68 45 6c  {.    fts3HashEl
377fa 65 6d 20 2a 65 3b 0a 20 20 20 20 66 6f 72 28 65  em *e;.    for(e
377fb 3d 66 74 73 33 48 61 73 68 46 69 72 73 74 28 26  =fts3HashFirst(&
377fc 76 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 29  v->pendingTerms)
377fd 3b 20 65 3b 20 65 3d 66 74 73 33 48 61 73 68 4e  ; e; e=fts3HashN
377fe 65 78 74 28 65 29 29 7b 0a 20 20 20 20 20 20 64  ext(e)){.      d
377ff 6c 63 44 65 6c 65 74 65 28 66 74 73 33 48 61 73  lcDelete(fts3Has
37800 68 44 61 74 61 28 65 29 29 3b 0a 20 20 20 20 7d  hData(e));.    }
37801 0a 20 20 20 20 66 74 73 33 48 61 73 68 43 6c 65  .    fts3HashCle
37802 61 72 28 26 76 2d 3e 70 65 6e 64 69 6e 67 54 65  ar(&v->pendingTe
37803 72 6d 73 29 3b 0a 20 20 20 20 76 2d 3e 6e 50 65  rms);.    v->nPe
37804 6e 64 69 6e 67 44 61 74 61 20 3d 20 2d 31 3b 0a  ndingData = -1;.
37805 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
37806 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 66  ITE_OK;.}../* If
37807 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 68 61   pendingTerms ha
37808 73 20 64 61 74 61 2c 20 66 6c 75 73 68 20 69 74  s data, flush it
37809 20 74 6f 20 61 20 6c 65 76 65 6c 2d 7a 65 72 6f   to a level-zero
3780a 20 73 65 67 6d 65 6e 74 2c 20 61 6e 64 0a 2a 2a   segment, and.**
3780b 20 66 72 65 65 20 69 74 2e 0a 2a 2f 0a 73 74 61   free it..*/.sta
3780c 74 69 63 20 69 6e 74 20 66 6c 75 73 68 50 65 6e  tic int flushPen
3780d 64 69 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65  dingTerms(fullte
3780e 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 69  xt_vtab *v){.  i
3780f 66 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61  f( v->nPendingDa
37810 74 61 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ta>=0 ){.    int
37811 20 72 63 20 3d 20 77 72 69 74 65 5a 65 72 6f 53   rc = writeZeroS
37812 65 67 6d 65 6e 74 28 76 2c 20 26 76 2d 3e 70 65  egment(v, &v->pe
37813 6e 64 69 6e 67 54 65 72 6d 73 29 3b 0a 20 20 20  ndingTerms);.   
37814 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
37815 4f 4b 20 29 20 63 6c 65 61 72 50 65 6e 64 69 6e  OK ) clearPendin
37816 67 54 65 72 6d 73 28 76 29 3b 0a 20 20 20 20 72  gTerms(v);.    r
37817 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
37818 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
37819 3b 0a 7d 0a 0a 2f 2a 20 49 66 20 70 65 6e 64 69  ;.}../* If pendi
3781a 6e 67 54 65 72 6d 73 20 69 73 20 22 74 6f 6f 20  ngTerms is "too 
3781b 62 69 67 22 2c 20 6f 72 20 64 6f 63 69 64 20 69  big", or docid i
3781c 73 20 6f 75 74 20 6f 66 20 6f 72 64 65 72 2c 20  s out of order, 
3781d 66 6c 75 73 68 20 69 74 2e 0a 2a 2a 20 52 65 67  flush it..** Reg
3781e 61 72 64 6c 65 73 73 2c 20 62 65 20 63 65 72 74  ardless, be cert
3781f 61 69 6e 20 74 68 61 74 20 70 65 6e 64 69 6e 67  ain that pending
37820 54 65 72 6d 73 20 69 73 20 69 6e 69 74 69 61 6c  Terms is initial
37821 69 7a 65 64 20 66 6f 72 20 75 73 65 2e 0a 2a 2f  ized for use..*/
37822 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 69 74  .static int init
37823 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 66 75 6c  PendingTerms(ful
37824 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73  ltext_vtab *v, s
37825 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63  qlite_int64 iDoc
37826 69 64 29 7b 0a 20 20 2f 2a 20 54 4f 44 4f 28 73  id){.  /* TODO(s
37827 68 65 73 73 29 20 45 78 70 6c 6f 72 65 20 77 68  hess) Explore wh
37828 65 74 68 65 72 20 70 61 72 74 69 61 6c 6c 79 20  ether partially 
37829 66 6c 75 73 68 69 6e 67 20 74 68 65 20 62 75 66  flushing the buf
3782a 66 65 72 20 6f 6e 0a 20 20 2a 2a 20 66 6f 72 63  fer on.  ** forc
3782b 65 64 2d 66 6c 75 73 68 20 77 6f 75 6c 64 20 70  ed-flush would p
3782c 72 6f 76 69 64 65 20 62 65 74 74 65 72 20 70 65  rovide better pe
3782d 72 66 6f 72 6d 61 6e 63 65 2e 20 20 49 20 73 75  rformance.  I su
3782e 73 70 65 63 74 20 74 68 61 74 20 69 66 0a 20 20  spect that if.  
3782f 2a 2a 20 77 65 20 6f 72 64 65 72 65 64 20 74 68  ** we ordered th
37830 65 20 64 6f 63 6c 69 73 74 73 20 62 79 20 73 69  e doclists by si
37831 7a 65 20 61 6e 64 20 66 6c 75 73 68 65 64 20 74  ze and flushed t
37832 68 65 20 6c 61 72 67 65 73 74 20 75 6e 74 69 6c  he largest until
37833 20 74 68 65 0a 20 20 2a 2a 20 62 75 66 66 65 72   the.  ** buffer
37834 20 77 61 73 20 68 61 6c 66 20 65 6d 70 74 79 2c   was half empty,
37835 20 74 68 61 74 20 77 6f 75 6c 64 20 6c 65 74 20   that would let 
37836 74 68 65 20 6c 65 73 73 20 66 72 65 71 75 65 6e  the less frequen
37837 74 20 74 65 72 6d 73 0a 20 20 2a 2a 20 67 65 6e  t terms.  ** gen
37838 65 72 61 74 65 20 6c 6f 6e 67 65 72 20 64 6f 63  erate longer doc
37839 6c 69 73 74 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  lists..  */.  if
3783a 28 20 69 44 6f 63 69 64 3c 3d 76 2d 3e 69 50 72  ( iDocid<=v->iPr
3783b 65 76 44 6f 63 69 64 20 7c 7c 20 76 2d 3e 6e 50  evDocid || v->nP
3783c 65 6e 64 69 6e 67 44 61 74 61 3e 6b 50 65 6e 64  endingData>kPend
3783d 69 6e 67 54 68 72 65 73 68 6f 6c 64 20 29 7b 0a  ingThreshold ){.
3783e 20 20 20 20 69 6e 74 20 72 63 20 3d 20 66 6c 75      int rc = flu
3783f 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76  shPendingTerms(v
37840 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
37841 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
37842 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20  n rc;.  }.  if( 
37843 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c  v->nPendingData<
37844 30 20 29 7b 0a 20 20 20 20 66 74 73 33 48 61 73  0 ){.    fts3Has
37845 68 49 6e 69 74 28 26 76 2d 3e 70 65 6e 64 69 6e  hInit(&v->pendin
37846 67 54 65 72 6d 73 2c 20 46 54 53 33 5f 48 41 53  gTerms, FTS3_HAS
37847 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a 20 20  H_STRING, 1);.  
37848 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74    v->nPendingDat
37849 61 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 76 2d 3e  a = 0;.  }.  v->
3784a 69 50 72 65 76 44 6f 63 69 64 20 3d 20 69 44 6f  iPrevDocid = iDo
3784b 63 69 64 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  cid;.  return SQ
3784c 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54  LITE_OK;.}../* T
3784d 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  his function imp
3784e 6c 65 6d 65 6e 74 73 20 74 68 65 20 78 55 70 64  lements the xUpd
3784f 61 74 65 20 63 61 6c 6c 62 61 63 6b 3b 20 69 74  ate callback; it
37850 20 69 73 20 74 68 65 20 74 6f 70 2d 6c 65 76 65   is the top-leve
37851 6c 20 65 6e 74 72 79 0a 20 2a 20 70 6f 69 6e 74  l entry. * point
37852 20 66 6f 72 20 69 6e 73 65 72 74 69 6e 67 2c 20   for inserting, 
37853 64 65 6c 65 74 69 6e 67 20 6f 72 20 75 70 64 61  deleting or upda
37854 74 69 6e 67 20 61 20 72 6f 77 20 69 6e 20 61 20  ting a row in a 
37855 66 75 6c 6c 2d 74 65 78 74 20 74 61 62 6c 65 2e  full-text table.
37856 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66   */.static int f
37857 75 6c 6c 74 65 78 74 55 70 64 61 74 65 28 73 71  ulltextUpdate(sq
37858 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
37859 62 2c 20 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c  b, int nArg, sql
3785a 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70 70 41  ite3_value **ppA
3785b 72 67 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  rg,.            
3785c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
3785d 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 52 6f 77  lite_int64 *pRow
3785e 69 64 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  id){.  fulltext_
3785f 76 74 61 62 20 2a 76 20 3d 20 28 66 75 6c 6c 74  vtab *v = (fullt
37860 65 78 74 5f 76 74 61 62 20 2a 29 20 70 56 74 61  ext_vtab *) pVta
37861 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  b;.  int rc;..  
37862 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20  FTSTRACE(("FTS3 
37863 55 70 64 61 74 65 20 25 70 5c 6e 22 2c 20 70 56  Update %p\n", pV
37864 74 61 62 29 29 3b 0a 0a 20 20 69 66 28 20 6e 41  tab));..  if( nA
37865 72 67 3c 32 20 29 7b 0a 20 20 20 20 72 63 20 3d  rg<2 ){.    rc =
37866 20 69 6e 64 65 78 5f 64 65 6c 65 74 65 28 76 2c   index_delete(v,
37867 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
37868 6e 74 36 34 28 70 70 41 72 67 5b 30 5d 29 29 3b  nt64(ppArg[0]));
37869 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
3786a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
3786b 2f 2a 20 49 66 20 77 65 20 6a 75 73 74 20 64 65  /* If we just de
3786c 6c 65 74 65 64 20 74 68 65 20 6c 61 73 74 20 72  leted the last r
3786d 6f 77 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c  ow in the table,
3786e 20 63 6c 65 61 72 20 6f 75 74 20 74 68 65 0a 20   clear out the. 
3786f 20 20 20 20 20 2a 2a 20 69 6e 64 65 78 20 64 61       ** index da
37870 74 61 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ta..      */.   
37871 20 20 20 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f     rc = content_
37872 65 78 69 73 74 73 28 76 29 3b 0a 20 20 20 20 20  exists(v);.     
37873 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
37874 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ROW ){.        r
37875 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
37876 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72       }else if( r
37877 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c==SQLITE_DONE )
37878 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 6c 65  {.        /* Cle
37879 61 72 20 74 68 65 20 70 65 6e 64 69 6e 67 20 74  ar the pending t
3787a 65 72 6d 73 20 73 6f 20 77 65 20 64 6f 6e 27 74  erms so we don't
3787b 20 66 6c 75 73 68 20 61 20 75 73 65 6c 65 73 73   flush a useless
3787c 20 6c 65 76 65 6c 2d 30 0a 20 20 20 20 20 20 20   level-0.       
3787d 20 2a 2a 20 73 65 67 6d 65 6e 74 20 77 68 65 6e   ** segment when
3787e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
3787f 20 63 6c 6f 73 65 73 2e 0a 20 20 20 20 20 20 20   closes..       
37880 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d   */.        rc =
37881 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72   clearPendingTer
37882 6d 73 28 76 29 3b 0a 20 20 20 20 20 20 20 20 69  ms(v);.        i
37883 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
37884 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
37885 20 3d 20 73 65 67 64 69 72 5f 64 65 6c 65 74 65   = segdir_delete
37886 5f 61 6c 6c 28 76 29 3b 0a 20 20 20 20 20 20 20  _all(v);.       
37887 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
37888 0a 20 20 7d 20 65 6c 73 65 20 69 66 28 20 73 71  .  } else if( sq
37889 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
3788a 28 70 70 41 72 67 5b 30 5d 29 20 21 3d 20 53 51  (ppArg[0]) != SQ
3788b 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20  LITE_NULL ){.   
3788c 20 2f 2a 20 41 6e 20 75 70 64 61 74 65 3a 0a 20   /* An update:. 
3788d 20 20 20 20 2a 20 70 70 41 72 67 5b 30 5d 20 3d      * ppArg[0] =
3788e 20 6f 6c 64 20 72 6f 77 69 64 0a 20 20 20 20 20   old rowid.     
3788f 2a 20 70 70 41 72 67 5b 31 5d 20 3d 20 6e 65 77  * ppArg[1] = new
37890 20 72 6f 77 69 64 0a 20 20 20 20 20 2a 20 70 70   rowid.     * pp
37891 41 72 67 5b 32 2e 2e 32 2b 76 2d 3e 6e 43 6f 6c  Arg[2..2+v->nCol
37892 75 6d 6e 2d 31 5d 20 3d 20 76 61 6c 75 65 73 0a  umn-1] = values.
37893 20 20 20 20 20 2a 20 70 70 41 72 67 5b 32 2b 76       * ppArg[2+v
37894 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 76 61 6c  ->nColumn] = val
37895 75 65 20 66 6f 72 20 6d 61 67 69 63 20 63 6f 6c  ue for magic col
37896 75 6d 6e 20 28 77 65 20 69 67 6e 6f 72 65 20 74  umn (we ignore t
37897 68 69 73 29 0a 20 20 20 20 20 2a 20 70 70 41 72  his).     * ppAr
37898 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31  g[2+v->nColumn+1
37899 5d 20 3d 20 76 61 6c 75 65 20 66 6f 72 20 64 6f  ] = value for do
3789a 63 69 64 0a 20 20 20 20 20 2a 2f 0a 20 20 20 20  cid.     */.    
3789b 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72 6f 77  sqlite_int64 row
3789c 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  id = sqlite3_val
3789d 75 65 5f 69 6e 74 36 34 28 70 70 41 72 67 5b 30  ue_int64(ppArg[0
3789e 5d 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  ]);.    if( sqli
3789f 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 70  te3_value_type(p
378a0 70 41 72 67 5b 31 5d 29 20 21 3d 20 53 51 4c 49  pArg[1]) != SQLI
378a1 54 45 5f 49 4e 54 45 47 45 52 20 7c 7c 0a 20 20  TE_INTEGER ||.  
378a2 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61        sqlite3_va
378a3 6c 75 65 5f 69 6e 74 36 34 28 70 70 41 72 67 5b  lue_int64(ppArg[
378a4 31 5d 29 20 21 3d 20 72 6f 77 69 64 20 29 7b 0a  1]) != rowid ){.
378a5 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
378a6 45 5f 45 52 52 4f 52 3b 20 20 2f 2a 20 77 65 20  E_ERROR;  /* we 
378a7 64 6f 6e 27 74 20 61 6c 6c 6f 77 20 63 68 61 6e  don't allow chan
378a8 67 69 6e 67 20 74 68 65 20 72 6f 77 69 64 20 2a  ging the rowid *
378a9 2f 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  /.    }else if( 
378aa 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
378ab 70 65 28 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43  pe(ppArg[2+v->nC
378ac 6f 6c 75 6d 6e 2b 31 5d 29 20 21 3d 20 53 51 4c  olumn+1]) != SQL
378ad 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 7c 0a 20  ITE_INTEGER ||. 
378ae 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
378af 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
378b0 28 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c  (ppArg[2+v->nCol
378b1 75 6d 6e 2b 31 5d 29 20 21 3d 20 72 6f 77 69 64  umn+1]) != rowid
378b2 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
378b3 51 4c 49 54 45 5f 45 52 52 4f 52 3b 20 20 2f 2a  QLITE_ERROR;  /*
378b4 20 77 65 20 64 6f 6e 27 74 20 61 6c 6c 6f 77 20   we don't allow 
378b5 63 68 61 6e 67 69 6e 67 20 74 68 65 20 64 6f 63  changing the doc
378b6 69 64 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 7b  id */.    }else{
378b7 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e  .      assert( n
378b8 41 72 67 3d 3d 32 2b 76 2d 3e 6e 43 6f 6c 75 6d  Arg==2+v->nColum
378b9 6e 2b 32 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  n+2);.      rc =
378ba 20 69 6e 64 65 78 5f 75 70 64 61 74 65 28 76 2c   index_update(v,
378bb 20 72 6f 77 69 64 2c 20 26 70 70 41 72 67 5b 32   rowid, &ppArg[2
378bc 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c  ]);.    }.  } el
378bd 73 65 20 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 69  se {.    /* An i
378be 6e 73 65 72 74 3a 0a 20 20 20 20 20 2a 20 70 70  nsert:.     * pp
378bf 41 72 67 5b 31 5d 20 3d 20 72 65 71 75 65 73 74  Arg[1] = request
378c0 65 64 20 72 6f 77 69 64 0a 20 20 20 20 20 2a 20  ed rowid.     * 
378c1 70 70 41 72 67 5b 32 2e 2e 32 2b 76 2d 3e 6e 43  ppArg[2..2+v->nC
378c2 6f 6c 75 6d 6e 2d 31 5d 20 3d 20 76 61 6c 75 65  olumn-1] = value
378c3 73 0a 20 20 20 20 20 2a 20 70 70 41 72 67 5b 32  s.     * ppArg[2
378c4 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 76  +v->nColumn] = v
378c5 61 6c 75 65 20 66 6f 72 20 6d 61 67 69 63 20 63  alue for magic c
378c6 6f 6c 75 6d 6e 20 28 77 65 20 69 67 6e 6f 72 65  olumn (we ignore
378c7 20 74 68 69 73 29 0a 20 20 20 20 20 2a 20 70 70   this).     * pp
378c8 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e  Arg[2+v->nColumn
378c9 2b 31 5d 20 3d 20 76 61 6c 75 65 20 66 6f 72 20  +1] = value for 
378ca 64 6f 63 69 64 0a 20 20 20 20 20 2a 2f 0a 20 20  docid.     */.  
378cb 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
378cc 2a 70 52 65 71 75 65 73 74 44 6f 63 69 64 20 3d  *pRequestDocid =
378cd 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c   ppArg[2+v->nCol
378ce 75 6d 6e 2b 31 5d 3b 0a 20 20 20 20 61 73 73 65  umn+1];.    asse
378cf 72 74 28 20 6e 41 72 67 3d 3d 32 2b 76 2d 3e 6e  rt( nArg==2+v->n
378d0 43 6f 6c 75 6d 6e 2b 32 29 3b 0a 20 20 20 20 69  Column+2);.    i
378d1 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 21  f( SQLITE_NULL !
378d2 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
378d3 74 79 70 65 28 70 52 65 71 75 65 73 74 44 6f 63  type(pRequestDoc
378d4 69 64 29 20 26 26 0a 20 20 20 20 20 20 20 20 53  id) &&.        S
378d5 51 4c 49 54 45 5f 4e 55 4c 4c 20 21 3d 20 73 71  QLITE_NULL != sq
378d6 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
378d7 28 70 70 41 72 67 5b 31 5d 29 20 29 7b 0a 20 20  (ppArg[1]) ){.  
378d8 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73      /* TODO(shes
378d9 73 29 20 43 6f 6e 73 69 64 65 72 20 61 6c 6c 6f  s) Consider allo
378da 77 69 6e 67 20 74 68 69 73 20 74 6f 20 77 6f 72  wing this to wor
378db 6b 20 69 66 20 74 68 65 20 76 61 6c 75 65 73 20  k if the values 
378dc 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 69 64 65  are.      ** ide
378dd 6e 74 69 63 61 6c 2e 20 20 49 27 6d 20 69 6e 63  ntical.  I'm inc
378de 6c 69 6e 65 64 20 74 6f 20 64 69 73 63 6f 75 72  lined to discour
378df 61 67 65 20 74 68 61 74 20 75 73 61 67 65 2c 20  age that usage, 
378e0 74 68 6f 75 67 68 2c 0a 20 20 20 20 20 20 2a 2a  though,.      **
378e1 20 67 69 76 65 6e 20 74 68 61 74 20 62 6f 74 68   given that both
378e2 20 72 6f 77 69 64 20 61 6e 64 20 64 6f 63 69 64   rowid and docid
378e3 20 61 72 65 20 73 70 65 63 69 61 6c 20 63 6f 6c   are special col
378e4 75 6d 6e 73 2e 20 20 42 65 74 74 65 72 0a 20 20  umns.  Better.  
378e5 20 20 20 20 2a 2a 20 77 6f 75 6c 64 20 62 65 20      ** would be 
378e6 74 6f 20 64 65 66 69 6e 65 20 6f 6e 65 20 6f 72  to define one or
378e7 20 74 68 65 20 6f 74 68 65 72 20 61 73 20 74 68   the other as th
378e8 65 20 64 65 66 61 75 6c 74 20 77 69 6e 6e 65 72  e default winner
378e9 2c 0a 20 20 20 20 20 20 2a 2a 20 62 75 74 20 73  ,.      ** but s
378ea 68 6f 75 6c 64 20 69 74 20 62 65 20 66 74 73 33  hould it be fts3
378eb 2d 63 65 6e 74 72 69 63 20 28 64 6f 63 69 64 29  -centric (docid)
378ec 20 6f 72 20 53 51 4c 69 74 65 2d 63 65 6e 74 72   or SQLite-centr
378ed 69 63 0a 20 20 20 20 20 20 2a 2a 20 28 72 6f 77  ic.      ** (row
378ee 69 64 29 3f 0a 20 20 20 20 20 20 2a 2f 0a 20 20  id)?.      */.  
378ef 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
378f0 45 52 52 4f 52 3b 0a 20 20 20 20 7d 65 6c 73 65  ERROR;.    }else
378f1 7b 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49  {.      if( SQLI
378f2 54 45 5f 4e 55 4c 4c 20 3d 3d 20 73 71 6c 69 74  TE_NULL == sqlit
378f3 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 70 52  e3_value_type(pR
378f4 65 71 75 65 73 74 44 6f 63 69 64 29 20 29 7b 0a  equestDocid) ){.
378f5 20 20 20 20 20 20 20 20 70 52 65 71 75 65 73 74          pRequest
378f6 44 6f 63 69 64 20 3d 20 70 70 41 72 67 5b 31 5d  Docid = ppArg[1]
378f7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
378f8 72 63 20 3d 20 69 6e 64 65 78 5f 69 6e 73 65 72  rc = index_inser
378f9 74 28 76 2c 20 70 52 65 71 75 65 73 74 44 6f 63  t(v, pRequestDoc
378fa 69 64 2c 20 26 70 70 41 72 67 5b 32 5d 2c 20 70  id, &ppArg[2], p
378fb 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20  Rowid);.    }.  
378fc 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
378fd 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75  }..static int fu
378fe 6c 6c 74 65 78 74 53 79 6e 63 28 73 71 6c 69 74  lltextSync(sqlit
378ff 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b  e3_vtab *pVtab){
37900 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54  .  FTSTRACE(("FT
37901 53 33 20 78 53 79 6e 63 28 29 5c 6e 22 29 29 3b  S3 xSync()\n"));
37902 0a 20 20 72 65 74 75 72 6e 20 66 6c 75 73 68 50  .  return flushP
37903 65 6e 64 69 6e 67 54 65 72 6d 73 28 28 66 75 6c  endingTerms((ful
37904 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 70 56 74  ltext_vtab *)pVt
37905 61 62 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69  ab);.}..static i
37906 6e 74 20 66 75 6c 6c 74 65 78 74 42 65 67 69 6e  nt fulltextBegin
37907 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
37908 56 74 61 62 29 7b 0a 20 20 66 75 6c 6c 74 65 78  Vtab){.  fulltex
37909 74 5f 76 74 61 62 20 2a 76 20 3d 20 28 66 75 6c  t_vtab *v = (ful
3790a 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 20 70 56  ltext_vtab *) pV
3790b 74 61 62 3b 0a 20 20 46 54 53 54 52 41 43 45 28  tab;.  FTSTRACE(
3790c 28 22 46 54 53 33 20 78 42 65 67 69 6e 28 29 5c  ("FTS3 xBegin()\
3790d 6e 22 29 29 3b 0a 0a 20 20 2f 2a 20 41 6e 79 20  n"));..  /* Any 
3790e 62 75 66 66 65 72 65 64 20 75 70 64 61 74 65 73  buffered updates
3790f 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65   should have bee
37910 6e 20 63 6c 65 61 72 65 64 20 62 79 20 74 68 65  n cleared by the
37911 20 70 72 65 76 69 6f 75 73 0a 20 20 2a 2a 20 74   previous.  ** t
37912 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f  ransaction..  */
37913 0a 20 20 61 73 73 65 72 74 28 20 76 2d 3e 6e 50  .  assert( v->nP
37914 65 6e 64 69 6e 67 44 61 74 61 3c 30 20 29 3b 0a  endingData<0 );.
37915 20 20 72 65 74 75 72 6e 20 63 6c 65 61 72 50 65    return clearPe
37916 6e 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a 7d  ndingTerms(v);.}
37917 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c  ..static int ful
37918 6c 74 65 78 74 43 6f 6d 6d 69 74 28 73 71 6c 69  ltextCommit(sqli
37919 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29  te3_vtab *pVtab)
3791a 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61  {.  fulltext_vta
3791b 62 20 2a 76 20 3d 20 28 66 75 6c 6c 74 65 78 74  b *v = (fulltext
3791c 5f 76 74 61 62 20 2a 29 20 70 56 74 61 62 3b 0a  _vtab *) pVtab;.
3791d 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53    FTSTRACE(("FTS
3791e 33 20 78 43 6f 6d 6d 69 74 28 29 5c 6e 22 29 29  3 xCommit()\n"))
3791f 3b 0a 0a 20 20 2f 2a 20 42 75 66 66 65 72 65 64  ;..  /* Buffered
37920 20 75 70 64 61 74 65 73 20 73 68 6f 75 6c 64 20   updates should 
37921 68 61 76 65 20 62 65 65 6e 20 63 6c 65 61 72 65  have been cleare
37922 64 20 62 79 20 66 75 6c 6c 74 65 78 74 53 79 6e  d by fulltextSyn
37923 63 28 29 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  c(). */.  assert
37924 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74  ( v->nPendingDat
37925 61 3c 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  a<0 );.  return 
37926 63 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72 6d  clearPendingTerm
37927 73 28 76 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  s(v);.}..static 
37928 69 6e 74 20 66 75 6c 6c 74 65 78 74 52 6f 6c 6c  int fulltextRoll
37929 62 61 63 6b 28 73 71 6c 69 74 65 33 5f 76 74 61  back(sqlite3_vta
3792a 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 46 54 53  b *pVtab){.  FTS
3792b 54 52 41 43 45 28 28 22 46 54 53 33 20 78 52 6f  TRACE(("FTS3 xRo
3792c 6c 6c 62 61 63 6b 28 29 5c 6e 22 29 29 3b 0a 20  llback()\n"));. 
3792d 20 72 65 74 75 72 6e 20 63 6c 65 61 72 50 65 6e   return clearPen
3792e 64 69 6e 67 54 65 72 6d 73 28 28 66 75 6c 6c 74  dingTerms((fullt
3792f 65 78 74 5f 76 74 61 62 20 2a 29 70 56 74 61 62  ext_vtab *)pVtab
37930 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  );.}../*.** Impl
37931 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
37932 65 20 73 6e 69 70 70 65 74 28 29 20 66 75 6e 63  e snippet() func
37933 74 69 6f 6e 20 66 6f 72 20 46 54 53 33 0a 2a 2f  tion for FTS3.*/
37934 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6e 69  .static void sni
37935 70 70 65 74 46 75 6e 63 28 0a 20 20 73 71 6c 69  ppetFunc(.  sqli
37936 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f  te3_context *pCo
37937 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
37938 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
37939 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 66  ue **argv.){.  f
3793a 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a  ulltext_cursor *
3793b 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 61  pCursor;.  if( a
3793c 72 67 63 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a  rgc<1 ) return;.
3793d 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61    if( sqlite3_va
3793e 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
3793f 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 7c  )!=SQLITE_BLOB |
37940 7c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  |.      sqlite3_
37941 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
37942 5b 30 5d 29 21 3d 73 69 7a 65 6f 66 28 70 43 75  [0])!=sizeof(pCu
37943 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 73 71 6c  rsor) ){.    sql
37944 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
37945 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 69 6c 6c  r(pContext, "ill
37946 65 67 61 6c 20 66 69 72 73 74 20 61 72 67 75 6d  egal first argum
37947 65 6e 74 20 74 6f 20 68 74 6d 6c 5f 73 6e 69 70  ent to html_snip
37948 70 65 74 22 2c 2d 31 29 3b 0a 20 20 7d 65 6c 73  pet",-1);.  }els
37949 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  e{.    const cha
3794a 72 20 2a 7a 53 74 61 72 74 20 3d 20 22 3c 62 3e  r *zStart = "<b>
3794b 22 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ";.    const cha
3794c 72 20 2a 7a 45 6e 64 20 3d 20 22 3c 2f 62 3e 22  r *zEnd = "</b>"
3794d 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
3794e 20 2a 7a 45 6c 6c 69 70 73 69 73 20 3d 20 22 3c   *zEllipsis = "<
3794f 62 3e 2e 2e 2e 3c 2f 62 3e 22 3b 0a 20 20 20 20  b>...</b>";.    
37950 6d 65 6d 63 70 79 28 26 70 43 75 72 73 6f 72 2c  memcpy(&pCursor,
37951 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
37952 6c 6f 62 28 61 72 67 76 5b 30 5d 29 2c 20 73 69  lob(argv[0]), si
37953 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 29 3b 0a  zeof(pCursor));.
37954 20 20 20 20 69 66 28 20 61 72 67 63 3e 3d 32 20      if( argc>=2 
37955 29 7b 0a 20 20 20 20 20 20 7a 53 74 61 72 74 20  ){.      zStart 
37956 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73  = (const char*)s
37957 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
37958 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20  t(argv[1]);.    
37959 20 20 69 66 28 20 61 72 67 63 3e 3d 33 20 29 7b    if( argc>=3 ){
3795a 0a 20 20 20 20 20 20 20 20 7a 45 6e 64 20 3d 20  .        zEnd = 
3795b 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c  (const char*)sql
3795c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
3795d 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 20 20  argv[2]);.      
3795e 20 20 69 66 28 20 61 72 67 63 3e 3d 34 20 29 7b    if( argc>=4 ){
3795f 0a 20 20 20 20 20 20 20 20 20 20 7a 45 6c 6c 69  .          zElli
37960 70 73 69 73 20 3d 20 28 63 6f 6e 73 74 20 63 68  psis = (const ch
37961 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  ar*)sqlite3_valu
37962 65 5f 74 65 78 74 28 61 72 67 76 5b 33 5d 29 3b  e_text(argv[3]);
37963 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
37964 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 6e 69   }.    }.    sni
37965 70 70 65 74 41 6c 6c 4f 66 66 73 65 74 73 28 70  ppetAllOffsets(p
37966 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73 6e 69  Cursor);.    sni
37967 70 70 65 74 54 65 78 74 28 70 43 75 72 73 6f 72  ppetText(pCursor
37968 2c 20 7a 53 74 61 72 74 2c 20 7a 45 6e 64 2c 20  , zStart, zEnd, 
37969 7a 45 6c 6c 69 70 73 69 73 29 3b 0a 20 20 20 20  zEllipsis);.    
3796a 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
3796b 65 78 74 28 70 43 6f 6e 74 65 78 74 2c 20 70 43  ext(pContext, pC
3796c 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 7a  ursor->snippet.z
3796d 53 6e 69 70 70 65 74 2c 0a 20 20 20 20 20 20 20  Snippet,.       
3796e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3796f 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65   pCursor->snippe
37970 74 2e 6e 53 6e 69 70 70 65 74 2c 20 53 51 4c 49  t.nSnippet, SQLI
37971 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a  TE_STATIC);.  }.
37972 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
37973 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f  ntation of the o
37974 66 66 73 65 74 73 28 29 20 66 75 6e 63 74 69 6f  ffsets() functio
37975 6e 20 66 6f 72 20 46 54 53 33 0a 2a 2f 0a 73 74  n for FTS3.*/.st
37976 61 74 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65  atic void snippe
37977 74 4f 66 66 73 65 74 73 46 75 6e 63 28 0a 20 20  tOffsetsFunc(.  
37978 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
37979 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74  *pContext,.  int
3797a 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
3797b 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
3797c 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
3797d 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 69  or *pCursor;.  i
3797e 66 28 20 61 72 67 63 3c 31 20 29 20 72 65 74 75  f( argc<1 ) retu
3797f 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  rn;.  if( sqlite
37980 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
37981 76 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42 4c  v[0])!=SQLITE_BL
37982 4f 42 20 7c 7c 0a 20 20 20 20 20 20 73 71 6c 69  OB ||.      sqli
37983 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
37984 61 72 67 76 5b 30 5d 29 21 3d 73 69 7a 65 6f 66  argv[0])!=sizeof
37985 28 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20  (pCursor) ){.   
37986 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
37987 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  error(pContext, 
37988 22 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61  "illegal first a
37989 72 67 75 6d 65 6e 74 20 74 6f 20 6f 66 66 73 65  rgument to offse
3798a 74 73 22 2c 2d 31 29 3b 0a 20 20 7d 65 6c 73 65  ts",-1);.  }else
3798b 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 43  {.    memcpy(&pC
3798c 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 33 5f 76  ursor, sqlite3_v
3798d 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30  alue_blob(argv[0
3798e 5d 29 2c 20 73 69 7a 65 6f 66 28 70 43 75 72 73  ]), sizeof(pCurs
3798f 6f 72 29 29 3b 0a 20 20 20 20 73 6e 69 70 70 65  or));.    snippe
37990 74 41 6c 6c 4f 66 66 73 65 74 73 28 70 43 75 72  tAllOffsets(pCur
37991 73 6f 72 29 3b 0a 20 20 20 20 73 6e 69 70 70 65  sor);.    snippe
37992 74 4f 66 66 73 65 74 54 65 78 74 28 26 70 43 75  tOffsetText(&pCu
37993 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 29 3b 0a  rsor->snippet);.
37994 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
37995 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78 74  lt_text(pContext
37996 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
37997 20 20 20 20 20 20 20 20 20 20 70 43 75 72 73 6f            pCurso
37998 72 2d 3e 73 6e 69 70 70 65 74 2e 7a 4f 66 66 73  r->snippet.zOffs
37999 65 74 2c 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69  et, pCursor->sni
3799a 70 70 65 74 2e 6e 4f 66 66 73 65 74 2c 0a 20 20  ppet.nOffset,.  
3799b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3799c 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 54 41        SQLITE_STA
3799d 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20  TIC);.  }.}../* 
3799e 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20  OptLeavesReader 
3799f 69 73 20 6e 65 61 72 6c 79 20 69 64 65 6e 74 69  is nearly identi
379a0 63 61 6c 20 74 6f 20 4c 65 61 76 65 73 52 65 61  cal to LeavesRea
379a1 64 65 72 2c 20 65 78 63 65 70 74 20 74 68 61 74  der, except that
379a2 0a 2a 2a 20 77 68 65 72 65 20 4c 65 61 76 65 73  .** where Leaves
379a3 52 65 61 64 65 72 20 69 73 20 67 65 61 72 65 64  Reader is geared
379a4 20 74 6f 77 61 72 64 73 20 74 68 65 20 6d 65 72   towards the mer
379a5 67 69 6e 67 20 6f 66 20 63 6f 6d 70 6c 65 74 65  ging of complete
379a6 0a 2a 2a 20 73 65 67 6d 65 6e 74 20 6c 65 76 65  .** segment leve
379a7 6c 73 20 28 77 69 74 68 20 65 78 61 63 74 6c 79  ls (with exactly
379a8 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 73 65 67   MERGE_COUNT seg
379a9 6d 65 6e 74 73 29 2c 20 4f 70 74 4c 65 61 76 65  ments), OptLeave
379aa 73 52 65 61 64 65 72 0a 2a 2a 20 69 73 20 67 65  sReader.** is ge
379ab 61 72 65 64 20 74 6f 77 61 72 64 73 20 69 6d 70  ared towards imp
379ac 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
379ad 68 65 20 6f 70 74 69 6d 69 7a 65 28 29 20 66 75  he optimize() fu
379ae 6e 63 74 69 6f 6e 2c 20 61 6e 64 0a 2a 2a 20 63  nction, and.** c
379af 61 6e 20 6d 65 72 67 65 20 61 6c 6c 20 73 65 67  an merge all seg
379b0 6d 65 6e 74 73 20 73 69 6d 75 6c 74 61 6e 65 6f  ments simultaneo
379b1 75 73 6c 79 2e 20 20 54 68 69 73 20 76 65 72 73  usly.  This vers
379b2 69 6f 6e 20 6d 61 79 20 62 65 0a 2a 2a 20 73 6f  ion may be.** so
379b3 6d 65 77 68 61 74 20 6c 65 73 73 20 65 66 66 69  mewhat less effi
379b4 63 69 65 6e 74 20 74 68 61 6e 20 4c 65 61 76 65  cient than Leave
379b5 73 52 65 61 64 65 72 20 62 65 63 61 75 73 65 20  sReader because 
379b6 69 74 20 6d 65 72 67 65 73 20 69 6e 74 6f 20 61  it merges into a
379b7 6e 0a 2a 2a 20 61 63 63 75 6d 75 6c 61 74 6f 72  n.** accumulator
379b8 20 72 61 74 68 65 72 20 74 68 61 6e 20 64 6f 69   rather than doi
379b9 6e 67 20 61 6e 20 4e 2d 77 61 79 20 6d 65 72 67  ng an N-way merg
379ba 65 2c 20 62 75 74 20 73 69 6e 63 65 20 73 65 67  e, but since seg
379bb 6d 65 6e 74 0a 2a 2a 20 73 69 7a 65 20 67 72 6f  ment.** size gro
379bc 77 73 20 65 78 70 6f 6e 65 6e 74 69 61 6c 6c 79  ws exponentially
379bd 20 28 73 6f 20 73 65 67 6d 65 6e 74 20 63 6f 75   (so segment cou
379be 6e 74 20 6c 6f 67 72 69 74 68 6d 69 63 61 6c 6c  nt logrithmicall
379bf 79 29 20 74 68 69 73 20 69 73 0a 2a 2a 20 70 72  y) this is.** pr
379c0 6f 62 61 62 6c 79 20 6e 6f 74 20 61 6e 20 69 6d  obably not an im
379c1 6d 65 64 69 61 74 65 20 70 72 6f 62 6c 65 6d 2e  mediate problem.
379c2 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
379c3 73 29 3a 20 50 72 6f 76 65 20 74 68 61 74 20 61  s): Prove that a
379c4 73 73 65 72 74 69 6f 6e 2c 20 6f 72 20 65 78 74  ssertion, or ext
379c5 65 6e 64 20 74 68 65 20 6d 65 72 67 65 20 63 6f  end the merge co
379c6 64 65 20 74 6f 0a 2a 2a 20 6d 65 72 67 65 20 74  de to.** merge t
379c7 72 65 65 20 66 61 73 68 69 6f 6e 20 28 6c 69 6b  ree fashion (lik
379c8 65 20 74 68 65 20 70 72 65 66 69 78 2d 73 65 61  e the prefix-sea
379c9 72 63 68 69 6e 67 20 63 6f 64 65 20 64 6f 65 73  rching code does
379ca 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  )..*/./* TODO(sh
379cb 65 73 73 29 3a 20 4f 70 74 4c 65 61 76 65 73 52  ess): OptLeavesR
379cc 65 61 64 65 72 20 61 6e 64 20 4c 65 61 76 65 73  eader and Leaves
379cd 52 65 61 64 65 72 20 63 6f 75 6c 64 20 70 72 6f  Reader could pro
379ce 62 61 62 6c 79 20 62 65 0a 2a 2a 20 6d 65 72 67  bably be.** merg
379cf 65 64 20 77 69 74 68 20 6c 69 74 74 6c 65 20 6f  ed with little o
379d0 72 20 6e 6f 20 6c 6f 73 73 20 6f 66 20 70 65 72  r no loss of per
379d1 66 6f 72 6d 61 6e 63 65 20 66 6f 72 20 4c 65 61  formance for Lea
379d2 76 65 73 52 65 61 64 65 72 2e 20 20 54 68 65 0a  vesReader.  The.
379d3 2a 2a 20 6d 65 72 67 65 64 20 63 6f 64 65 20 77  ** merged code w
379d4 6f 75 6c 64 20 6e 65 65 64 20 74 6f 20 68 61 6e  ould need to han
379d5 64 6c 65 20 3e 4d 45 52 47 45 5f 43 4f 55 4e 54  dle >MERGE_COUNT
379d6 20 73 65 67 6d 65 6e 74 73 2c 20 61 6e 64 20 77   segments, and w
379d7 6f 75 6c 64 0a 2a 2a 20 61 6c 73 6f 20 6e 65 65  ould.** also nee
379d8 64 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20  d to be able to 
379d9 6f 70 74 69 6f 6e 61 6c 6c 79 20 6f 70 74 69 6d  optionally optim
379da 69 7a 65 20 61 77 61 79 20 64 65 6c 65 74 65 73  ize away deletes
379db 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
379dc 75 63 74 20 4f 70 74 4c 65 61 76 65 73 52 65 61  uct OptLeavesRea
379dd 64 65 72 20 7b 0a 20 20 2f 2a 20 53 65 67 6d 65  der {.  /* Segme
379de 6e 74 20 6e 75 6d 62 65 72 2c 20 74 6f 20 6f 72  nt number, to or
379df 64 65 72 20 72 65 61 64 65 72 73 20 62 79 20 61  der readers by a
379e0 67 65 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 65 67  ge. */.  int seg
379e1 6d 65 6e 74 3b 0a 20 20 4c 65 61 76 65 73 52 65  ment;.  LeavesRe
379e2 61 64 65 72 20 72 65 61 64 65 72 3b 0a 7d 20 4f  ader reader;.} O
379e3 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 3b 0a  ptLeavesReader;.
379e4 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 74 4c  .static int optL
379e5 65 61 76 65 73 52 65 61 64 65 72 41 74 45 6e 64  eavesReaderAtEnd
379e6 28 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72  (OptLeavesReader
379e7 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65   *pReader){.  re
379e8 74 75 72 6e 20 6c 65 61 76 65 73 52 65 61 64 65  turn leavesReade
379e9 72 41 74 45 6e 64 28 26 70 52 65 61 64 65 72 2d  rAtEnd(&pReader-
379ea 3e 72 65 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74  >reader);.}.stat
379eb 69 63 20 69 6e 74 20 6f 70 74 4c 65 61 76 65 73  ic int optLeaves
379ec 52 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28  ReaderTermBytes(
379ed 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20  OptLeavesReader 
379ee 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74  *pReader){.  ret
379ef 75 72 6e 20 6c 65 61 76 65 73 52 65 61 64 65 72  urn leavesReader
379f0 54 65 72 6d 42 79 74 65 73 28 26 70 52 65 61 64  TermBytes(&pRead
379f1 65 72 2d 3e 72 65 61 64 65 72 29 3b 0a 7d 0a 73  er->reader);.}.s
379f2 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
379f3 20 2a 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65   *optLeavesReade
379f4 72 44 61 74 61 28 4f 70 74 4c 65 61 76 65 73 52  rData(OptLeavesR
379f5 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
379f6 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73  .  return leaves
379f7 52 65 61 64 65 72 44 61 74 61 28 26 70 52 65 61  ReaderData(&pRea
379f8 64 65 72 2d 3e 72 65 61 64 65 72 29 3b 0a 7d 0a  der->reader);.}.
379f9 73 74 61 74 69 63 20 69 6e 74 20 6f 70 74 4c 65  static int optLe
379fa 61 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79  avesReaderDataBy
379fb 74 65 73 28 4f 70 74 4c 65 61 76 65 73 52 65 61  tes(OptLeavesRea
379fc 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
379fd 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73 52 65   return leavesRe
379fe 61 64 65 72 44 61 74 61 42 79 74 65 73 28 26 70  aderDataBytes(&p
379ff 52 65 61 64 65 72 2d 3e 72 65 61 64 65 72 29 3b  Reader->reader);
37a00 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .}.static const 
37a01 63 68 61 72 20 2a 6f 70 74 4c 65 61 76 65 73 52  char *optLeavesR
37a02 65 61 64 65 72 54 65 72 6d 28 4f 70 74 4c 65 61  eaderTerm(OptLea
37a03 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
37a04 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65  er){.  return le
37a05 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 26  avesReaderTerm(&
37a06 70 52 65 61 64 65 72 2d 3e 72 65 61 64 65 72 29  pReader->reader)
37a07 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  ;.}.static int o
37a08 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 53 74  ptLeavesReaderSt
37a09 65 70 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  ep(fulltext_vtab
37a0a 20 2a 76 2c 20 4f 70 74 4c 65 61 76 65 73 52 65   *v, OptLeavesRe
37a0b 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
37a0c 20 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73 52    return leavesR
37a0d 65 61 64 65 72 53 74 65 70 28 76 2c 20 26 70 52  eaderStep(v, &pR
37a0e 65 61 64 65 72 2d 3e 72 65 61 64 65 72 29 3b 0a  eader->reader);.
37a0f 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 74  }.static int opt
37a10 4c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  LeavesReaderTerm
37a11 43 6d 70 28 4f 70 74 4c 65 61 76 65 73 52 65 61  Cmp(OptLeavesRea
37a12 64 65 72 20 2a 6c 72 31 2c 20 4f 70 74 4c 65 61  der *lr1, OptLea
37a13 76 65 73 52 65 61 64 65 72 20 2a 6c 72 32 29 7b  vesReader *lr2){
37a14 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73  .  return leaves
37a15 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 26 6c  ReaderTermCmp(&l
37a16 72 31 2d 3e 72 65 61 64 65 72 2c 20 26 6c 72 32  r1->reader, &lr2
37a17 2d 3e 72 65 61 64 65 72 29 3b 0a 7d 0a 2f 2a 20  ->reader);.}./* 
37a18 4f 72 64 65 72 20 62 79 20 74 65 72 6d 20 61 73  Order by term as
37a19 63 65 6e 64 69 6e 67 2c 20 73 65 67 6d 65 6e 74  cending, segment
37a1a 20 61 73 63 65 6e 64 69 6e 67 20 28 6f 6c 64 65   ascending (olde
37a1b 73 74 20 74 6f 20 6e 65 77 65 73 74 29 2c 20 77  st to newest), w
37a1c 69 74 68 0a 2a 2a 20 65 78 68 61 75 73 74 65 64  ith.** exhausted
37a1d 20 72 65 61 64 65 72 73 20 74 6f 20 74 68 65 20   readers to the 
37a1e 65 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  end..*/.static i
37a1f 6e 74 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64  nt optLeavesRead
37a20 65 72 43 6d 70 28 4f 70 74 4c 65 61 76 65 73 52  erCmp(OptLeavesR
37a21 65 61 64 65 72 20 2a 6c 72 31 2c 20 4f 70 74 4c  eader *lr1, OptL
37a22 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72 32  eavesReader *lr2
37a23 29 7b 0a 20 20 69 6e 74 20 63 20 3d 20 6f 70 74  ){.  int c = opt
37a24 4c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  LeavesReaderTerm
37a25 43 6d 70 28 6c 72 31 2c 20 6c 72 32 29 3b 0a 20  Cmp(lr1, lr2);. 
37a26 20 69 66 28 20 63 21 3d 30 20 29 20 72 65 74 75   if( c!=0 ) retu
37a27 72 6e 20 63 3b 0a 20 20 72 65 74 75 72 6e 20 6c  rn c;.  return l
37a28 72 31 2d 3e 73 65 67 6d 65 6e 74 2d 6c 72 32 2d  r1->segment-lr2-
37a29 3e 73 65 67 6d 65 6e 74 3b 0a 7d 0a 2f 2a 20 42  >segment;.}./* B
37a2a 75 62 62 6c 65 20 70 4c 72 5b 30 5d 20 74 6f 20  ubble pLr[0] to 
37a2b 61 70 70 72 6f 70 72 69 61 74 65 20 70 6c 61 63  appropriate plac
37a2c 65 20 69 6e 20 70 4c 72 5b 31 2e 2e 6e 4c 72 2d  e in pLr[1..nLr-
37a2d 31 5d 2e 20 20 41 73 73 75 6d 65 73 20 74 68 61  1].  Assumes tha
37a2e 74 0a 2a 2a 20 70 4c 72 5b 31 2e 2e 6e 4c 72 2d  t.** pLr[1..nLr-
37a2f 31 5d 20 69 73 20 61 6c 72 65 61 64 79 20 73 6f  1] is already so
37a30 72 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rted..*/.static 
37a31 76 6f 69 64 20 6f 70 74 4c 65 61 76 65 73 52 65  void optLeavesRe
37a32 61 64 65 72 52 65 6f 72 64 65 72 28 4f 70 74 4c  aderReorder(OptL
37a33 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 4c 72  eavesReader *pLr
37a34 2c 20 69 6e 74 20 6e 4c 72 29 7b 0a 20 20 77 68  , int nLr){.  wh
37a35 69 6c 65 28 20 6e 4c 72 3e 31 20 26 26 20 6f 70  ile( nLr>1 && op
37a36 74 4c 65 61 76 65 73 52 65 61 64 65 72 43 6d 70  tLeavesReaderCmp
37a37 28 70 4c 72 2c 20 70 4c 72 2b 31 29 3e 30 20 29  (pLr, pLr+1)>0 )
37a38 7b 0a 20 20 20 20 4f 70 74 4c 65 61 76 65 73 52  {.    OptLeavesR
37a39 65 61 64 65 72 20 74 6d 70 20 3d 20 70 4c 72 5b  eader tmp = pLr[
37a3a 30 5d 3b 0a 20 20 20 20 70 4c 72 5b 30 5d 20 3d  0];.    pLr[0] =
37a3b 20 70 4c 72 5b 31 5d 3b 0a 20 20 20 20 70 4c 72   pLr[1];.    pLr
37a3c 5b 31 5d 20 3d 20 74 6d 70 3b 0a 20 20 20 20 6e  [1] = tmp;.    n
37a3d 4c 72 2d 2d 3b 0a 20 20 20 20 70 4c 72 2b 2b 3b  Lr--;.    pLr++;
37a3e 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 6f 70 74 69 6d  .  }.}../* optim
37a3f 69 7a 65 28 29 20 68 65 6c 70 65 72 20 66 75 6e  ize() helper fun
37a40 63 74 69 6f 6e 2e 20 20 50 75 74 20 74 68 65 20  ction.  Put the 
37a41 72 65 61 64 65 72 73 20 69 6e 20 6f 72 64 65 72  readers in order
37a42 20 61 6e 64 20 69 74 65 72 61 74 65 0a 2a 2a 20   and iterate.** 
37a43 74 68 72 6f 75 67 68 20 74 68 65 6d 2c 20 6d 65  through them, me
37a44 72 67 69 6e 67 20 64 6f 63 6c 69 73 74 73 20 66  rging doclists f
37a45 6f 72 20 6d 61 74 63 68 69 6e 67 20 74 65 72 6d  or matching term
37a46 73 20 69 6e 74 6f 20 70 57 72 69 74 65 72 2e 0a  s into pWriter..
37a47 2a 2a 20 52 65 74 75 72 6e 73 20 53 51 4c 49 54  ** Returns SQLIT
37a48 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2c  E_OK on success,
37a49 20 6f 72 20 74 68 65 20 53 51 4c 69 74 65 20 65   or the SQLite e
37a4a 72 72 6f 72 20 63 6f 64 65 20 77 68 69 63 68 0a  rror code which.
37a4b 2a 2a 20 70 72 65 76 65 6e 74 65 64 20 73 75 63  ** prevented suc
37a4c 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  cess..*/.static 
37a4d 69 6e 74 20 6f 70 74 69 6d 69 7a 65 49 6e 74 65  int optimizeInte
37a4e 72 6e 61 6c 28 66 75 6c 6c 74 65 78 74 5f 76 74  rnal(fulltext_vt
37a4f 61 62 20 2a 76 2c 0a 20 20 20 20 20 20 20 20 20  ab *v,.         
37a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37a51 20 20 20 4f 70 74 4c 65 61 76 65 73 52 65 61 64     OptLeavesRead
37a52 65 72 20 2a 72 65 61 64 65 72 73 2c 20 69 6e 74  er *readers, int
37a53 20 6e 52 65 61 64 65 72 73 2c 0a 20 20 20 20 20   nReaders,.     
37a54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37a55 20 20 20 20 20 20 20 4c 65 61 66 57 72 69 74 65         LeafWrite
37a56 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20 69  r *pWriter){.  i
37a57 6e 74 20 69 2c 20 72 63 20 3d 20 53 51 4c 49 54  nt i, rc = SQLIT
37a58 45 5f 4f 4b 3b 0a 20 20 44 61 74 61 42 75 66 66  E_OK;.  DataBuff
37a59 65 72 20 64 6f 63 6c 69 73 74 2c 20 6d 65 72 67  er doclist, merg
37a5a 65 64 2c 20 74 6d 70 3b 0a 0a 20 20 2f 2a 20 4f  ed, tmp;..  /* O
37a5b 72 64 65 72 20 74 68 65 20 72 65 61 64 65 72 73  rder the readers
37a5c 2e 20 2a 2f 0a 20 20 69 20 3d 20 6e 52 65 61 64  . */.  i = nRead
37a5d 65 72 73 3b 0a 20 20 77 68 69 6c 65 28 20 69 2d  ers;.  while( i-
37a5e 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 6f 70 74  - > 0 ){.    opt
37a5f 4c 65 61 76 65 73 52 65 61 64 65 72 52 65 6f 72  LeavesReaderReor
37a60 64 65 72 28 26 72 65 61 64 65 72 73 5b 69 5d 2c  der(&readers[i],
37a61 20 6e 52 65 61 64 65 72 73 2d 69 29 3b 0a 20 20   nReaders-i);.  
37a62 7d 0a 0a 20 20 64 61 74 61 42 75 66 66 65 72 49  }..  dataBufferI
37a63 6e 69 74 28 26 64 6f 63 6c 69 73 74 2c 20 4c 45  nit(&doclist, LE
37a64 41 46 5f 4d 41 58 29 3b 0a 20 20 64 61 74 61 42  AF_MAX);.  dataB
37a65 75 66 66 65 72 49 6e 69 74 28 26 6d 65 72 67 65  ufferInit(&merge
37a66 64 2c 20 4c 45 41 46 5f 4d 41 58 29 3b 0a 0a 20  d, LEAF_MAX);.. 
37a67 20 2f 2a 20 45 78 68 61 75 73 74 65 64 20 72 65   /* Exhausted re
37a68 61 64 65 72 73 20 62 75 62 62 6c 65 20 74 6f 20  aders bubble to 
37a69 74 68 65 20 65 6e 64 2c 20 73 6f 20 77 68 65 6e  the end, so when
37a6a 20 74 68 65 20 66 69 72 73 74 20 72 65 61 64 65   the first reade
37a6b 72 20 69 73 0a 20 20 2a 2a 20 61 74 20 65 6f 66  r is.  ** at eof
37a6c 2c 20 61 6c 6c 20 61 72 65 20 61 74 20 65 6f 66  , all are at eof
37a6d 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20  ..  */.  while( 
37a6e 21 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72  !optLeavesReader
37a6f 41 74 45 6e 64 28 26 72 65 61 64 65 72 73 5b 30  AtEnd(&readers[0
37a70 5d 29 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 46 69  ]) ){..    /* Fi
37a71 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e  gure out how man
37a72 79 20 72 65 61 64 65 72 73 20 73 68 61 72 65 20  y readers share 
37a73 74 68 65 20 6e 65 78 74 20 74 65 72 6d 2e 20 2a  the next term. *
37a74 2f 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69  /.    for(i=1; i
37a75 3c 6e 52 65 61 64 65 72 73 20 26 26 20 21 6f 70  <nReaders && !op
37a76 74 4c 65 61 76 65 73 52 65 61 64 65 72 41 74 45  tLeavesReaderAtE
37a77 6e 64 28 26 72 65 61 64 65 72 73 5b 69 5d 29 3b  nd(&readers[i]);
37a78 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
37a79 20 30 21 3d 6f 70 74 4c 65 61 76 65 73 52 65 61   0!=optLeavesRea
37a7a 64 65 72 54 65 72 6d 43 6d 70 28 26 72 65 61 64  derTermCmp(&read
37a7b 65 72 73 5b 30 5d 2c 20 26 72 65 61 64 65 72 73  ers[0], &readers
37a7c 5b 69 5d 29 20 29 20 62 72 65 61 6b 3b 0a 20 20  [i]) ) break;.  
37a7d 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 70 65 63    }..    /* Spec
37a7e 69 61 6c 2d 63 61 73 65 20 66 6f 72 20 6e 6f 20  ial-case for no 
37a7f 6d 65 72 67 65 2e 20 2a 2f 0a 20 20 20 20 69 66  merge. */.    if
37a80 28 20 69 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  ( i==1 ){.      
37a81 2f 2a 20 54 72 69 6d 20 64 65 6c 65 74 69 6f 6e  /* Trim deletion
37a82 73 20 66 72 6f 6d 20 74 68 65 20 64 6f 63 6c 69  s from the docli
37a83 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 64 61 74  st. */.      dat
37a84 61 42 75 66 66 65 72 52 65 73 65 74 28 26 6d 65  aBufferReset(&me
37a85 72 67 65 64 29 3b 0a 20 20 20 20 20 20 64 6f 63  rged);.      doc
37a86 4c 69 73 74 54 72 69 6d 28 44 4c 5f 44 45 46 41  ListTrim(DL_DEFA
37a87 55 4c 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ULT,.           
37a88 20 20 20 20 20 20 20 6f 70 74 4c 65 61 76 65 73         optLeaves
37a89 52 65 61 64 65 72 44 61 74 61 28 26 72 65 61 64  ReaderData(&read
37a8a 65 72 73 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20  ers[0]),.       
37a8b 20 20 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65             optLe
37a8c 61 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79  avesReaderDataBy
37a8d 74 65 73 28 26 72 65 61 64 65 72 73 5b 30 5d 29  tes(&readers[0])
37a8e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
37a8f 20 20 20 20 2d 31 2c 20 44 4c 5f 44 45 46 41 55      -1, DL_DEFAU
37a90 4c 54 2c 20 26 6d 65 72 67 65 64 29 3b 0a 20 20  LT, &merged);.  
37a91 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 44    }else{.      D
37a92 4c 52 65 61 64 65 72 20 64 6c 52 65 61 64 65 72  LReader dlReader
37a93 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54 5d 3b 0a  s[MERGE_COUNT];.
37a94 20 20 20 20 20 20 69 6e 74 20 69 52 65 61 64 65        int iReade
37a95 72 2c 20 6e 52 65 61 64 65 72 73 3b 0a 0a 20 20  r, nReaders;..  
37a96 20 20 20 20 2f 2a 20 50 72 69 6d 65 20 74 68 65      /* Prime the
37a97 20 70 69 70 65 6c 69 6e 65 20 77 69 74 68 20 74   pipeline with t
37a98 68 65 20 66 69 72 73 74 20 72 65 61 64 65 72 27  he first reader'
37a99 73 20 64 6f 63 6c 69 73 74 2e 20 20 41 66 74 65  s doclist.  Afte
37a9a 72 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 65 20 70  r.      ** one p
37a9b 61 73 73 20 69 6e 64 65 78 20 30 20 77 69 6c 6c  ass index 0 will
37a9c 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 61   reference the a
37a9d 63 63 75 6d 75 6c 61 74 65 64 20 64 6f 63 6c 69  ccumulated docli
37a9e 73 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  st..      */.   
37a9f 20 20 20 64 6c 72 49 6e 69 74 28 26 64 6c 52 65     dlrInit(&dlRe
37aa0 61 64 65 72 73 5b 30 5d 2c 20 44 4c 5f 44 45 46  aders[0], DL_DEF
37aa1 41 55 4c 54 2c 0a 20 20 20 20 20 20 20 20 20 20  AULT,.          
37aa2 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61      optLeavesRea
37aa3 64 65 72 44 61 74 61 28 26 72 65 61 64 65 72 73  derData(&readers
37aa4 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20  [0]),.          
37aa5 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61      optLeavesRea
37aa6 64 65 72 44 61 74 61 42 79 74 65 73 28 26 72 65  derDataBytes(&re
37aa7 61 64 65 72 73 5b 30 5d 29 29 3b 0a 20 20 20 20  aders[0]));.    
37aa8 20 20 69 52 65 61 64 65 72 20 3d 20 31 3b 0a 0a    iReader = 1;..
37aa9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 52        assert( iR
37aaa 65 61 64 65 72 3c 69 20 29 3b 20 20 2f 2a 20 4d  eader<i );  /* M
37aab 75 73 74 20 65 78 65 63 75 74 65 20 74 68 65 20  ust execute the 
37aac 6c 6f 6f 70 20 61 74 20 6c 65 61 73 74 20 6f 6e  loop at least on
37aad 63 65 2e 20 2a 2f 0a 20 20 20 20 20 20 77 68 69  ce. */.      whi
37aae 6c 65 28 20 69 52 65 61 64 65 72 3c 69 20 29 7b  le( iReader<i ){
37aaf 0a 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 72 67  .        /* Merg
37ab0 65 20 31 36 20 69 6e 70 75 74 73 20 70 65 72 20  e 16 inputs per 
37ab1 70 61 73 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20  pass. */.       
37ab2 20 66 6f 72 28 20 6e 52 65 61 64 65 72 73 3d 31   for( nReaders=1
37ab3 3b 20 69 52 65 61 64 65 72 3c 69 20 26 26 20 6e  ; iReader<i && n
37ab4 52 65 61 64 65 72 73 3c 4d 45 52 47 45 5f 43 4f  Readers<MERGE_CO
37ab5 55 4e 54 3b 0a 20 20 20 20 20 20 20 20 20 20 20  UNT;.           
37ab6 20 20 69 52 65 61 64 65 72 2b 2b 2c 20 6e 52 65    iReader++, nRe
37ab7 61 64 65 72 73 2b 2b 20 29 7b 0a 20 20 20 20 20  aders++ ){.     
37ab8 20 20 20 20 20 64 6c 72 49 6e 69 74 28 26 64 6c       dlrInit(&dl
37ab9 52 65 61 64 65 72 73 5b 6e 52 65 61 64 65 72 73  Readers[nReaders
37aba 5d 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 0a 20  ], DL_DEFAULT,. 
37abb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37abc 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
37abd 44 61 74 61 28 26 72 65 61 64 65 72 73 5b 69 52  Data(&readers[iR
37abe 65 61 64 65 72 5d 29 2c 0a 20 20 20 20 20 20 20  eader]),.       
37abf 20 20 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65             optLe
37ac0 61 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79  avesReaderDataBy
37ac1 74 65 73 28 26 72 65 61 64 65 72 73 5b 69 52 65  tes(&readers[iRe
37ac2 61 64 65 72 5d 29 29 3b 0a 20 20 20 20 20 20 20  ader]));.       
37ac3 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4d   }..        /* M
37ac4 65 72 67 65 20 64 6f 63 6c 69 73 74 73 20 61 6e  erge doclists an
37ac5 64 20 73 77 61 70 20 72 65 73 75 6c 74 20 69 6e  d swap result in
37ac6 74 6f 20 61 63 63 75 6d 75 6c 61 74 6f 72 2e 20  to accumulator. 
37ac7 2a 2f 0a 20 20 20 20 20 20 20 20 64 61 74 61 42  */.        dataB
37ac8 75 66 66 65 72 52 65 73 65 74 28 26 6d 65 72 67  ufferReset(&merg
37ac9 65 64 29 3b 0a 20 20 20 20 20 20 20 20 64 6f 63  ed);.        doc
37aca 4c 69 73 74 4d 65 72 67 65 28 26 6d 65 72 67 65  ListMerge(&merge
37acb 64 2c 20 64 6c 52 65 61 64 65 72 73 2c 20 6e 52  d, dlReaders, nR
37acc 65 61 64 65 72 73 29 3b 0a 20 20 20 20 20 20 20  eaders);.       
37acd 20 74 6d 70 20 3d 20 6d 65 72 67 65 64 3b 0a 20   tmp = merged;. 
37ace 20 20 20 20 20 20 20 6d 65 72 67 65 64 20 3d 20         merged = 
37acf 64 6f 63 6c 69 73 74 3b 0a 20 20 20 20 20 20 20  doclist;.       
37ad0 20 64 6f 63 6c 69 73 74 20 3d 20 74 6d 70 3b 0a   doclist = tmp;.
37ad1 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
37ad2 6e 52 65 61 64 65 72 73 2d 2d 20 3e 20 30 20 29  nReaders-- > 0 )
37ad3 7b 0a 20 20 20 20 20 20 20 20 20 20 64 6c 72 44  {.          dlrD
37ad4 65 73 74 72 6f 79 28 26 64 6c 52 65 61 64 65 72  estroy(&dlReader
37ad5 73 5b 6e 52 65 61 64 65 72 73 5d 29 3b 0a 20 20  s[nReaders]);.  
37ad6 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20        }..       
37ad7 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 64 20   /* Accumulated 
37ad8 64 6f 63 6c 69 73 74 20 74 6f 20 72 65 61 64 65  doclist to reade
37ad9 72 20 30 20 66 6f 72 20 6e 65 78 74 20 70 61 73  r 0 for next pas
37ada 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 64 6c  s. */.        dl
37adb 72 49 6e 69 74 28 26 64 6c 52 65 61 64 65 72 73  rInit(&dlReaders
37adc 5b 30 5d 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c  [0], DL_DEFAULT,
37add 20 64 6f 63 6c 69 73 74 2e 70 44 61 74 61 2c 20   doclist.pData, 
37ade 64 6f 63 6c 69 73 74 2e 6e 44 61 74 61 29 3b 0a  doclist.nData);.
37adf 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
37ae0 2a 20 44 65 73 74 72 6f 79 20 72 65 61 64 65 72  * Destroy reader
37ae1 20 74 68 61 74 20 77 61 73 20 6c 65 66 74 20 69   that was left i
37ae2 6e 20 74 68 65 20 70 69 70 65 6c 69 6e 65 2e 20  n the pipeline. 
37ae3 2a 2f 0a 20 20 20 20 20 20 64 6c 72 44 65 73 74  */.      dlrDest
37ae4 72 6f 79 28 26 64 6c 52 65 61 64 65 72 73 5b 30  roy(&dlReaders[0
37ae5 5d 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 72  ]);..      /* Tr
37ae6 69 6d 20 64 65 6c 65 74 69 6f 6e 73 20 66 72 6f  im deletions fro
37ae7 6d 20 74 68 65 20 64 6f 63 6c 69 73 74 2e 20 2a  m the doclist. *
37ae8 2f 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66  /.      dataBuff
37ae9 65 72 52 65 73 65 74 28 26 6d 65 72 67 65 64 29  erReset(&merged)
37aea 3b 0a 20 20 20 20 20 20 64 6f 63 4c 69 73 74 54  ;.      docListT
37aeb 72 69 6d 28 44 4c 5f 44 45 46 41 55 4c 54 2c 20  rim(DL_DEFAULT, 
37aec 64 6f 63 6c 69 73 74 2e 70 44 61 74 61 2c 20 64  doclist.pData, d
37aed 6f 63 6c 69 73 74 2e 6e 44 61 74 61 2c 0a 20 20  oclist.nData,.  
37aee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37aef 2d 31 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20  -1, DL_DEFAULT, 
37af0 26 6d 65 72 67 65 64 29 3b 0a 20 20 20 20 7d 0a  &merged);.    }.
37af1 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 70 61 73  .    /* Only pas
37af2 73 20 64 6f 63 6c 69 73 74 73 20 77 69 74 68 20  s doclists with 
37af3 68 69 74 73 20 28 73 6b 69 70 20 69 66 20 61 6c  hits (skip if al
37af4 6c 20 68 69 74 73 20 64 65 6c 65 74 65 64 29 2e  l hits deleted).
37af5 20 2a 2f 0a 20 20 20 20 69 66 28 20 6d 65 72 67   */.    if( merg
37af6 65 64 2e 6e 44 61 74 61 3e 30 20 29 7b 0a 20 20  ed.nData>0 ){.  
37af7 20 20 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69      rc = leafWri
37af8 74 65 72 53 74 65 70 28 76 2c 20 70 57 72 69 74  terStep(v, pWrit
37af9 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
37afa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 70                op
37afb 74 4c 65 61 76 65 73 52 65 61 64 65 72 54 65 72  tLeavesReaderTer
37afc 6d 28 26 72 65 61 64 65 72 73 5b 30 5d 29 2c 0a  m(&readers[0]),.
37afd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37afe 20 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65 61            optLea
37aff 76 65 73 52 65 61 64 65 72 54 65 72 6d 42 79 74  vesReaderTermByt
37b00 65 73 28 26 72 65 61 64 65 72 73 5b 30 5d 29 2c  es(&readers[0]),
37b01 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
37b02 20 20 20 20 20 20 20 20 20 20 20 6d 65 72 67 65             merge
37b03 64 2e 70 44 61 74 61 2c 20 6d 65 72 67 65 64 2e  d.pData, merged.
37b04 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20 69 66  nData);.      if
37b05 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
37b06 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20 20 20  ) goto err;.    
37b07 7d 0a 0a 20 20 20 20 2f 2a 20 53 74 65 70 20 6d  }..    /* Step m
37b08 65 72 67 65 64 20 72 65 61 64 65 72 73 20 74 6f  erged readers to
37b09 20 6e 65 78 74 20 74 65 72 6d 20 61 6e 64 20 72   next term and r
37b0a 65 6f 72 64 65 72 2e 20 2a 2f 0a 20 20 20 20 77  eorder. */.    w
37b0b 68 69 6c 65 28 20 69 2d 2d 20 3e 20 30 20 29 7b  hile( i-- > 0 ){
37b0c 0a 20 20 20 20 20 20 72 63 20 3d 20 6f 70 74 4c  .      rc = optL
37b0d 65 61 76 65 73 52 65 61 64 65 72 53 74 65 70 28  eavesReaderStep(
37b0e 76 2c 20 26 72 65 61 64 65 72 73 5b 69 5d 29 3b  v, &readers[i]);
37b0f 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
37b10 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
37b11 65 72 72 3b 0a 0a 20 20 20 20 20 20 6f 70 74 4c  err;..      optL
37b12 65 61 76 65 73 52 65 61 64 65 72 52 65 6f 72 64  eavesReaderReord
37b13 65 72 28 26 72 65 61 64 65 72 73 5b 69 5d 2c 20  er(&readers[i], 
37b14 6e 52 65 61 64 65 72 73 2d 69 29 3b 0a 20 20 20  nReaders-i);.   
37b15 20 7d 0a 20 20 7d 0a 0a 20 65 72 72 3a 0a 20 20   }.  }.. err:.  
37b16 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
37b17 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20 64  y(&doclist);.  d
37b18 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
37b19 28 26 6d 65 72 67 65 64 29 3b 0a 20 20 72 65 74  (&merged);.  ret
37b1a 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 49 6d  urn rc;.}../* Im
37b1b 70 6c 65 6d 65 6e 74 20 6f 70 74 69 6d 69 7a 65  plement optimize
37b1c 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20  () function for 
37b1d 46 54 53 33 2e 20 20 6f 70 74 69 6d 69 7a 65 28  FTS3.  optimize(
37b1e 74 29 20 6d 65 72 67 65 73 20 61 6c 6c 0a 2a 2a  t) merges all.**
37b1f 20 73 65 67 6d 65 6e 74 73 20 69 6e 20 74 68 65   segments in the
37b20 20 66 74 73 20 69 6e 64 65 78 20 69 6e 74 6f 20   fts index into 
37b21 61 20 73 69 6e 67 6c 65 20 73 65 67 6d 65 6e 74  a single segment
37b22 2e 20 20 27 74 27 20 69 73 20 74 68 65 20 6d 61  .  't' is the ma
37b23 67 69 63 0a 2a 2a 20 74 61 62 6c 65 2d 6e 61 6d  gic.** table-nam
37b24 65 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74  ed column..*/.st
37b25 61 74 69 63 20 76 6f 69 64 20 6f 70 74 69 6d 69  atic void optimi
37b26 7a 65 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63  zeFunc(sqlite3_c
37b27 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74  ontext *pContext
37b28 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
37b29 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 61             int a
37b2a 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
37b2b 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 66 75  ue **argv){.  fu
37b2c 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 70  lltext_cursor *p
37b2d 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 61 72  Cursor;.  if( ar
37b2e 67 63 3e 31 20 29 7b 0a 20 20 20 20 73 71 6c 69  gc>1 ){.    sqli
37b2f 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
37b30 28 70 43 6f 6e 74 65 78 74 2c 20 22 65 78 63 65  (pContext, "exce
37b31 73 73 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  ss arguments to 
37b32 6f 70 74 69 6d 69 7a 65 28 29 22 2c 2d 31 29 3b  optimize()",-1);
37b33 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c  .  }else if( sql
37b34 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
37b35 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54 45  argv[0])!=SQLITE
37b36 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20 20  _BLOB ||.       
37b37 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c       sqlite3_val
37b38 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
37b39 29 21 3d 73 69 7a 65 6f 66 28 70 43 75 72 73 6f  )!=sizeof(pCurso
37b3a 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  r) ){.    sqlite
37b3b 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70  3_result_error(p
37b3c 43 6f 6e 74 65 78 74 2c 20 22 69 6c 6c 65 67 61  Context, "illega
37b3d 6c 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  l first argument
37b3e 20 74 6f 20 6f 70 74 69 6d 69 7a 65 22 2c 2d 31   to optimize",-1
37b3f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
37b40 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
37b41 3b 0a 20 20 20 20 69 6e 74 20 69 2c 20 72 63 2c  ;.    int i, rc,
37b42 20 69 4d 61 78 4c 65 76 65 6c 3b 0a 20 20 20 20   iMaxLevel;.    
37b43 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20  OptLeavesReader 
37b44 2a 72 65 61 64 65 72 73 3b 0a 20 20 20 20 69 6e  *readers;.    in
37b45 74 20 6e 52 65 61 64 65 72 73 3b 0a 20 20 20 20  t nReaders;.    
37b46 4c 65 61 66 57 72 69 74 65 72 20 77 72 69 74 65  LeafWriter write
37b47 72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  r;.    sqlite3_s
37b48 74 6d 74 20 2a 73 3b 0a 0a 20 20 20 20 6d 65 6d  tmt *s;..    mem
37b49 63 70 79 28 26 70 43 75 72 73 6f 72 2c 20 73 71  cpy(&pCursor, sq
37b4a 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
37b4b 28 61 72 67 76 5b 30 5d 29 2c 20 73 69 7a 65 6f  (argv[0]), sizeo
37b4c 66 28 70 43 75 72 73 6f 72 29 29 3b 0a 20 20 20  f(pCursor));.   
37b4d 20 76 20 3d 20 63 75 72 73 6f 72 5f 76 74 61 62   v = cursor_vtab
37b4e 28 70 43 75 72 73 6f 72 29 3b 0a 0a 20 20 20 20  (pCursor);..    
37b4f 2f 2a 20 46 6c 75 73 68 20 61 6e 79 20 62 75 66  /* Flush any buf
37b50 66 65 72 65 64 20 75 70 64 61 74 65 73 20 62 65  fered updates be
37b51 66 6f 72 65 20 6f 70 74 69 6d 69 7a 69 6e 67 2e  fore optimizing.
37b52 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 66 6c 75   */.    rc = flu
37b53 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76  shPendingTerms(v
37b54 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
37b55 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
37b56 65 72 72 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73  err;..    rc = s
37b57 65 67 64 69 72 5f 63 6f 75 6e 74 28 76 2c 20 26  egdir_count(v, &
37b58 6e 52 65 61 64 65 72 73 2c 20 26 69 4d 61 78 4c  nReaders, &iMaxL
37b59 65 76 65 6c 29 3b 0a 20 20 20 20 69 66 28 20 72  evel);.    if( r
37b5a 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
37b5b 6f 74 6f 20 65 72 72 3b 0a 20 20 20 20 69 66 28  oto err;.    if(
37b5c 20 6e 52 65 61 64 65 72 73 3d 3d 30 20 7c 7c 20   nReaders==0 || 
37b5d 6e 52 65 61 64 65 72 73 3d 3d 31 20 29 7b 0a 20  nReaders==1 ){. 
37b5e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
37b5f 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78  ult_text(pContex
37b60 74 2c 20 22 49 6e 64 65 78 20 61 6c 72 65 61 64  t, "Index alread
37b61 79 20 6f 70 74 69 6d 61 6c 22 2c 20 2d 31 2c 0a  y optimal", -1,.
37b62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37b63 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
37b64 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
37b65 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 0a 20  return;.    }.. 
37b66 20 20 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f     rc = sql_get_
37b67 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45 47  statement(v, SEG
37b68 44 49 52 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f 53  DIR_SELECT_ALL_S
37b69 54 4d 54 2c 20 26 73 29 3b 0a 20 20 20 20 69 66  TMT, &s);.    if
37b6a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
37b6b 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 20  ) goto err;..   
37b6c 20 72 65 61 64 65 72 73 20 3d 20 73 71 6c 69 74   readers = sqlit
37b6d 65 33 5f 6d 61 6c 6c 6f 63 28 6e 52 65 61 64 65  e3_malloc(nReade
37b6e 72 73 2a 73 69 7a 65 6f 66 28 72 65 61 64 65 72  rs*sizeof(reader
37b6f 73 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20  s[0]));.    if( 
37b70 72 65 61 64 65 72 73 3d 3d 4e 55 4c 4c 20 29 20  readers==NULL ) 
37b71 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 2f  goto err;..    /
37b72 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 72  * Note that ther
37b73 65 20 77 69 6c 6c 20 61 6c 72 65 61 64 79 20 62  e will already b
37b74 65 20 61 20 73 65 67 6d 65 6e 74 20 61 74 20 74  e a segment at t
37b75 68 69 73 20 70 6f 73 69 74 69 6f 6e 0a 20 20 20  his position.   
37b76 20 2a 2a 20 75 6e 74 69 6c 20 77 65 20 63 61 6c   ** until we cal
37b77 6c 20 73 65 67 64 69 72 5f 64 65 6c 65 74 65 28  l segdir_delete(
37b78 29 20 6f 6e 20 69 4d 61 78 4c 65 76 65 6c 2e 0a  ) on iMaxLevel..
37b79 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65 61 66 57      */.    leafW
37b7a 72 69 74 65 72 49 6e 69 74 28 69 4d 61 78 4c 65  riterInit(iMaxLe
37b7b 76 65 6c 2c 20 30 2c 20 26 77 72 69 74 65 72 29  vel, 0, &writer)
37b7c 3b 0a 0a 20 20 20 20 69 20 3d 20 30 3b 0a 20 20  ;..    i = 0;.  
37b7d 20 20 77 68 69 6c 65 28 20 28 72 63 20 3d 20 73    while( (rc = s
37b7e 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 29 3d  qlite3_step(s))=
37b7f 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
37b80 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
37b81 34 20 69 53 74 61 72 74 20 3d 20 73 71 6c 69 74  4 iStart = sqlit
37b82 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28  e3_column_int64(
37b83 73 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  s, 0);.      sql
37b84 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 20 3d  ite_int64 iEnd =
37b85 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
37b86 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a 20 20 20  int64(s, 1);.   
37b87 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
37b88 52 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69 74  RootData = sqlit
37b89 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73  e3_column_blob(s
37b8a 2c 20 32 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  , 2);.      int 
37b8b 6e 52 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69  nRootData = sqli
37b8c 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
37b8d 28 73 2c 20 32 29 3b 0a 0a 20 20 20 20 20 20 61  (s, 2);..      a
37b8e 73 73 65 72 74 28 20 69 3c 6e 52 65 61 64 65 72  ssert( i<nReader
37b8f 73 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  s );.      rc = 
37b90 6c 65 61 76 65 73 52 65 61 64 65 72 49 6e 69 74  leavesReaderInit
37b91 28 76 2c 20 2d 31 2c 20 69 53 74 61 72 74 2c 20  (v, -1, iStart, 
37b92 69 45 6e 64 2c 20 70 52 6f 6f 74 44 61 74 61 2c  iEnd, pRootData,
37b93 20 6e 52 6f 6f 74 44 61 74 61 2c 0a 20 20 20 20   nRootData,.    
37b94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37b95 20 20 20 20 20 20 20 20 26 72 65 61 64 65 72 73          &readers
37b96 5b 69 5d 2e 72 65 61 64 65 72 29 3b 0a 20 20 20  [i].reader);.   
37b97 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
37b98 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 0a 20  E_OK ) break;.. 
37b99 20 20 20 20 20 72 65 61 64 65 72 73 5b 69 5d 2e       readers[i].
37b9a 73 65 67 6d 65 6e 74 20 3d 20 69 3b 0a 20 20 20  segment = i;.   
37b9b 20 20 20 69 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20     i++;.    }.. 
37b9c 20 20 20 2f 2a 20 49 66 20 77 65 20 6d 61 6e 61     /* If we mana
37b9d 67 65 64 20 74 6f 20 73 75 63 63 65 73 73 66 75  ged to successfu
37b9e 6c 6c 79 20 72 65 61 64 20 74 68 65 6d 20 61 6c  lly read them al
37b9f 6c 2c 20 6f 70 74 69 6d 69 7a 65 20 74 68 65 6d  l, optimize them
37ba0 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d  . */.    if( rc=
37ba1 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a  =SQLITE_DONE ){.
37ba2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3d        assert( i=
37ba3 3d 6e 52 65 61 64 65 72 73 20 29 3b 0a 20 20 20  =nReaders );.   
37ba4 20 20 20 72 63 20 3d 20 6f 70 74 69 6d 69 7a 65     rc = optimize
37ba5 49 6e 74 65 72 6e 61 6c 28 76 2c 20 72 65 61 64  Internal(v, read
37ba6 65 72 73 2c 20 6e 52 65 61 64 65 72 73 2c 20 26  ers, nReaders, &
37ba7 77 72 69 74 65 72 29 3b 0a 20 20 20 20 7d 0a 0a  writer);.    }..
37ba8 20 20 20 20 77 68 69 6c 65 28 20 69 2d 2d 20 3e      while( i-- >
37ba9 20 30 20 29 7b 0a 20 20 20 20 20 20 6c 65 61 76   0 ){.      leav
37baa 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28  esReaderDestroy(
37bab 26 72 65 61 64 65 72 73 5b 69 5d 2e 72 65 61 64  &readers[i].read
37bac 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  er);.    }.    s
37bad 71 6c 69 74 65 33 5f 66 72 65 65 28 72 65 61 64  qlite3_free(read
37bae 65 72 73 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66  ers);..    /* If
37baf 20 77 65 27 76 65 20 73 75 63 63 65 73 73 66 75   we've successfu
37bb0 6c 6c 79 20 67 6f 74 74 65 6e 20 74 6f 20 68 65  lly gotten to he
37bb1 72 65 2c 20 64 65 6c 65 74 65 20 74 68 65 20 6f  re, delete the o
37bb2 6c 64 20 73 65 67 6d 65 6e 74 73 0a 20 20 20 20  ld segments.    
37bb3 2a 2a 20 61 6e 64 20 66 6c 75 73 68 20 74 68 65  ** and flush the
37bb4 20 69 6e 74 65 72 69 6f 72 20 73 74 72 75 63 74   interior struct
37bb5 75 72 65 20 6f 66 20 74 68 65 20 6e 65 77 20 73  ure of the new s
37bb6 65 67 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20  egment..    */. 
37bb7 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
37bb8 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 66 6f  E_OK ){.      fo
37bb9 72 28 20 69 3d 30 3b 20 69 3c 3d 69 4d 61 78 4c  r( i=0; i<=iMaxL
37bba 65 76 65 6c 3b 20 69 2b 2b 20 29 7b 0a 20 20 20  evel; i++ ){.   
37bbb 20 20 20 20 20 72 63 20 3d 20 73 65 67 64 69 72       rc = segdir
37bbc 5f 64 65 6c 65 74 65 28 76 2c 20 69 29 3b 0a 20  _delete(v, i);. 
37bbd 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
37bbe 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b  QLITE_OK ) break
37bbf 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
37bc0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
37bc1 4f 4b 20 29 20 72 63 20 3d 20 6c 65 61 66 57 72  OK ) rc = leafWr
37bc2 69 74 65 72 46 69 6e 61 6c 69 7a 65 28 76 2c 20  iterFinalize(v, 
37bc3 26 77 72 69 74 65 72 29 3b 0a 20 20 20 20 7d 0a  &writer);.    }.
37bc4 0a 20 20 20 20 6c 65 61 66 57 72 69 74 65 72 44  .    leafWriterD
37bc5 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b  estroy(&writer);
37bc6 0a 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ..    if( rc!=SQ
37bc7 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
37bc8 72 72 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33  rr;..    sqlite3
37bc9 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f  _result_text(pCo
37bca 6e 74 65 78 74 2c 20 22 49 6e 64 65 78 20 6f 70  ntext, "Index op
37bcb 74 69 6d 69 7a 65 64 22 2c 20 2d 31 2c 20 53 51  timized", -1, SQ
37bcc 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
37bcd 20 20 72 65 74 75 72 6e 3b 0a 0a 20 20 20 20 2f    return;..    /
37bce 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20 45  * TODO(shess): E
37bcf 72 72 6f 72 2d 68 61 6e 64 6c 69 6e 67 20 6e 65  rror-handling ne
37bd0 65 64 73 20 74 6f 20 62 65 20 69 6d 70 72 6f 76  eds to be improv
37bd1 65 64 20 61 6c 6f 6e 67 20 74 68 65 0a 20 20 20  ed along the.   
37bd2 20 2a 2a 20 6c 69 6e 65 73 20 6f 66 20 74 68 65   ** lines of the
37bd3 20 64 75 6d 70 5f 20 66 75 6e 63 74 69 6f 6e 73   dump_ functions
37bd4 2e 0a 20 20 20 20 2a 2f 0a 20 65 72 72 3a 0a 20  ..    */. err:. 
37bd5 20 20 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20     {.      char 
37bd6 62 75 66 5b 35 31 32 5d 3b 0a 20 20 20 20 20 20  buf[512];.      
37bd7 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
37bd8 28 73 69 7a 65 6f 66 28 62 75 66 29 2c 20 62 75  (sizeof(buf), bu
37bd9 66 2c 20 22 45 72 72 6f 72 20 69 6e 20 6f 70 74  f, "Error in opt
37bda 69 6d 69 7a 65 3a 20 25 73 22 2c 0a 20 20 20 20  imize: %s",.    
37bdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37bdc 20 20 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73     sqlite3_errms
37bdd 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  g(sqlite3_contex
37bde 74 5f 64 62 5f 68 61 6e 64 6c 65 28 70 43 6f 6e  t_db_handle(pCon
37bdf 74 65 78 74 29 29 29 3b 0a 20 20 20 20 20 20 73  text)));.      s
37be0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
37be1 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 62 75  ror(pContext, bu
37be2 66 2c 20 2d 31 29 3b 0a 20 20 20 20 7d 0a 20 20  f, -1);.    }.  
37be3 7d 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  }.}..#ifdef SQLI
37be4 54 45 5f 54 45 53 54 0a 2f 2a 20 47 65 6e 65 72  TE_TEST./* Gener
37be5 61 74 65 20 61 6e 20 65 72 72 6f 72 20 6f 66 20  ate an error of 
37be6 74 68 65 20 66 6f 72 6d 20 22 3c 70 72 65 66 69  the form "<prefi
37be7 78 3e 3a 20 3c 6d 73 67 3e 22 2e 20 20 49 66 20  x>: <msg>".  If 
37be8 6d 73 67 20 69 73 20 4e 55 4c 4c 2c 0a 2a 2a 20  msg is NULL,.** 
37be9 70 75 6c 6c 20 74 68 65 20 65 72 72 6f 72 20 66  pull the error f
37bea 72 6f 6d 20 74 68 65 20 63 6f 6e 74 65 78 74 27  rom the context'
37beb 73 20 64 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a  s db handle..*/.
37bec 73 74 61 74 69 63 20 76 6f 69 64 20 67 65 6e 65  static void gene
37bed 72 61 74 65 45 72 72 6f 72 28 73 71 6c 69 74 65  rateError(sqlite
37bee 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74  3_context *pCont
37bef 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ext,.           
37bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
37bf1 6f 6e 73 74 20 63 68 61 72 20 2a 70 72 65 66 69  onst char *prefi
37bf2 78 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6d  x, const char *m
37bf3 73 67 29 7b 0a 20 20 63 68 61 72 20 62 75 66 5b  sg){.  char buf[
37bf4 35 31 32 5d 3b 0a 20 20 69 66 28 20 6d 73 67 3d  512];.  if( msg=
37bf5 3d 4e 55 4c 4c 20 29 20 6d 73 67 20 3d 20 73 71  =NULL ) msg = sq
37bf6 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71 6c  lite3_errmsg(sql
37bf7 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
37bf8 68 61 6e 64 6c 65 28 70 43 6f 6e 74 65 78 74 29  handle(pContext)
37bf9 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  );.  sqlite3_snp
37bfa 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66  rintf(sizeof(buf
37bfb 29 2c 20 62 75 66 2c 20 22 25 73 3a 20 25 73 22  ), buf, "%s: %s"
37bfc 2c 20 70 72 65 66 69 78 2c 20 6d 73 67 29 3b 0a  , prefix, msg);.
37bfd 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
37bfe 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c  _error(pContext,
37bff 20 62 75 66 2c 20 2d 31 29 3b 0a 7d 0a 0a 2f 2a   buf, -1);.}../*
37c00 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e   Helper function
37c01 20 74 6f 20 63 6f 6c 6c 65 63 74 20 74 68 65 20   to collect the 
37c02 73 65 74 20 6f 66 20 74 65 72 6d 73 20 69 6e 20  set of terms in 
37c03 74 68 65 20 73 65 67 6d 65 6e 74 20 69 6e 74 6f  the segment into
37c04 0a 2a 2a 20 70 54 65 72 6d 73 2e 20 20 54 68 65  .** pTerms.  The
37c05 20 73 65 67 6d 65 6e 74 20 69 73 20 64 65 66 69   segment is defi
37c06 6e 65 64 20 62 79 20 74 68 65 20 6c 65 61 66 20  ned by the leaf 
37c07 6e 6f 64 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a  nodes between.**
37c08 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 20 61   iStartBlockid a
37c09 6e 64 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 20  nd iEndBlockid, 
37c0a 69 6e 63 6c 75 73 69 76 65 2c 20 6f 72 20 62 79  inclusive, or by
37c0b 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
37c0c 0a 2a 2a 20 70 52 6f 6f 74 44 61 74 61 20 69 66  .** pRootData if
37c0d 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 20 69   iStartBlockid i
37c0e 73 20 30 20 28 69 6e 20 77 68 69 63 68 20 63 61  s 0 (in which ca
37c0f 73 65 20 74 68 65 20 65 6e 74 69 72 65 20 73 65  se the entire se
37c10 67 6d 65 6e 74 0a 2a 2a 20 66 69 74 20 69 6e 20  gment.** fit in 
37c11 61 20 6c 65 61 66 29 2e 0a 2a 2f 0a 73 74 61 74  a leaf)..*/.stat
37c12 69 63 20 69 6e 74 20 63 6f 6c 6c 65 63 74 53 65  ic int collectSe
37c13 67 6d 65 6e 74 54 65 72 6d 73 28 66 75 6c 6c 74  gmentTerms(fullt
37c14 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c  ext_vtab *v, sql
37c15 69 74 65 33 5f 73 74 6d 74 20 2a 73 2c 0a 20 20  ite3_stmt *s,.  
37c16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37c17 20 20 20 20 20 20 20 20 20 20 20 20 20 66 74 73               fts
37c18 33 48 61 73 68 20 2a 70 54 65 72 6d 73 29 7b 0a  3Hash *pTerms){.
37c19 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 5f 69    const sqlite_i
37c1a 6e 74 36 34 20 69 53 74 61 72 74 42 6c 6f 63 6b  nt64 iStartBlock
37c1b 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  id = sqlite3_col
37c1c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 30 29 3b  umn_int64(s, 0);
37c1d 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 5f  .  const sqlite_
37c1e 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b 69  int64 iEndBlocki
37c1f 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  d = sqlite3_colu
37c20 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a  mn_int64(s, 1);.
37c21 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52    const char *pR
37c22 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69 74 65  ootData = sqlite
37c23 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 2c  3_column_blob(s,
37c24 20 32 29 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74   2);.  const int
37c25 20 6e 52 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c   nRootData = sql
37c26 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
37c27 73 28 73 2c 20 32 29 3b 0a 20 20 4c 65 61 76 65  s(s, 2);.  Leave
37c28 73 52 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a  sReader reader;.
37c29 20 20 69 6e 74 20 72 63 20 3d 20 6c 65 61 76 65    int rc = leave
37c2a 73 52 65 61 64 65 72 49 6e 69 74 28 76 2c 20 30  sReaderInit(v, 0
37c2b 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c  , iStartBlockid,
37c2c 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 0a 20 20   iEndBlockid,.  
37c2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37c2e 20 20 20 20 20 20 20 20 20 20 70 52 6f 6f 74 44            pRootD
37c2f 61 74 61 2c 20 6e 52 6f 6f 74 44 61 74 61 2c 20  ata, nRootData, 
37c30 26 72 65 61 64 65 72 29 3b 0a 20 20 69 66 28 20  &reader);.  if( 
37c31 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
37c32 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 77 68  return rc;..  wh
37c33 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
37c34 4f 4b 20 26 26 20 21 6c 65 61 76 65 73 52 65 61  OK && !leavesRea
37c35 64 65 72 41 74 45 6e 64 28 26 72 65 61 64 65 72  derAtEnd(&reader
37c36 29 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ) ){.    const c
37c37 68 61 72 20 2a 70 54 65 72 6d 20 3d 20 6c 65 61  har *pTerm = lea
37c38 76 65 73 52 65 61 64 65 72 54 65 72 6d 28 26 72  vesReaderTerm(&r
37c39 65 61 64 65 72 29 3b 0a 20 20 20 20 63 6f 6e 73  eader);.    cons
37c3a 74 20 69 6e 74 20 6e 54 65 72 6d 20 3d 20 6c 65  t int nTerm = le
37c3b 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 42 79  avesReaderTermBy
37c3c 74 65 73 28 26 72 65 61 64 65 72 29 3b 0a 20 20  tes(&reader);.  
37c3d 20 20 76 6f 69 64 20 2a 6f 6c 64 56 61 6c 75 65    void *oldValue
37c3e 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 48 61   = sqlite3Fts3Ha
37c3f 73 68 46 69 6e 64 28 70 54 65 72 6d 73 2c 20 70  shFind(pTerms, p
37c40 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20  Term, nTerm);.  
37c41 20 20 76 6f 69 64 20 2a 6e 65 77 56 61 6c 75 65    void *newValue
37c42 20 3d 20 28 76 6f 69 64 20 2a 29 28 28 63 68 61   = (void *)((cha
37c43 72 20 2a 29 6f 6c 64 56 61 6c 75 65 2b 31 29 3b  r *)oldValue+1);
37c44 0a 0a 20 20 20 20 2f 2a 20 46 72 6f 6d 20 74 68  ..    /* From th
37c45 65 20 63 6f 6d 6d 65 6e 74 20 62 65 66 6f 72 65  e comment before
37c46 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
37c47 49 6e 73 65 72 74 20 69 6e 20 66 74 73 33 5f 68  Insert in fts3_h
37c48 61 73 68 2e 63 2c 0a 20 20 20 20 2a 2a 20 74 68  ash.c,.    ** th
37c49 65 20 64 61 74 61 20 76 61 6c 75 65 20 70 61 73  e data value pas
37c4a 73 65 64 20 69 73 20 72 65 74 75 72 6e 65 64 20  sed is returned 
37c4b 69 6e 20 63 61 73 65 20 6f 66 20 6d 61 6c 6c 6f  in case of mallo
37c4c 63 20 66 61 69 6c 75 72 65 2e 0a 20 20 20 20 2a  c failure..    *
37c4d 2f 0a 20 20 20 20 69 66 28 20 6e 65 77 56 61 6c  /.    if( newVal
37c4e 75 65 3d 3d 73 71 6c 69 74 65 33 46 74 73 33 48  ue==sqlite3Fts3H
37c4f 61 73 68 49 6e 73 65 72 74 28 70 54 65 72 6d 73  ashInsert(pTerms
37c50 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20  , pTerm, nTerm, 
37c51 6e 65 77 56 61 6c 75 65 29 20 29 7b 0a 20 20 20  newValue) ){.   
37c52 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
37c53 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  OMEM;.    }else{
37c54 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 65 61 76  .      rc = leav
37c55 65 73 52 65 61 64 65 72 53 74 65 70 28 76 2c 20  esReaderStep(v, 
37c56 26 72 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a  &reader);.    }.
37c57 20 20 7d 0a 0a 20 20 6c 65 61 76 65 73 52 65 61    }..  leavesRea
37c58 64 65 72 44 65 73 74 72 6f 79 28 26 72 65 61 64  derDestroy(&read
37c59 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  er);.  return rc
37c5a 3b 0a 7d 0a 0a 2f 2a 20 48 65 6c 70 65 72 20 66  ;.}../* Helper f
37c5b 75 6e 63 74 69 6f 6e 20 74 6f 20 62 75 69 6c 64  unction to build
37c5c 20 74 68 65 20 72 65 73 75 6c 74 20 73 74 72 69   the result stri
37c5d 6e 67 20 66 6f 72 20 64 75 6d 70 5f 74 65 72 6d  ng for dump_term
37c5e 73 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  s(). */.static i
37c5f 6e 74 20 67 65 6e 65 72 61 74 65 54 65 72 6d 73  nt generateTerms
37c60 52 65 73 75 6c 74 28 73 71 6c 69 74 65 33 5f 63  Result(sqlite3_c
37c61 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74  ontext *pContext
37c62 2c 20 66 74 73 33 48 61 73 68 20 2a 70 54 65 72  , fts3Hash *pTer
37c63 6d 73 29 7b 0a 20 20 69 6e 74 20 69 54 65 72 6d  ms){.  int iTerm
37c64 2c 20 6e 54 65 72 6d 73 2c 20 6e 52 65 73 75 6c  , nTerms, nResul
37c65 74 42 79 74 65 73 2c 20 69 42 79 74 65 3b 0a 20  tBytes, iByte;. 
37c66 20 63 68 61 72 20 2a 72 65 73 75 6c 74 3b 0a 20   char *result;. 
37c67 20 54 65 72 6d 44 61 74 61 20 2a 70 44 61 74 61   TermData *pData
37c68 3b 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d  ;.  fts3HashElem
37c69 20 2a 65 3b 0a 0a 20 20 2f 2a 20 49 74 65 72 61   *e;..  /* Itera
37c6a 74 65 20 70 54 65 72 6d 73 20 74 6f 20 67 65 6e  te pTerms to gen
37c6b 65 72 61 74 65 20 61 6e 20 61 72 72 61 79 20 6f  erate an array o
37c6c 66 20 74 65 72 6d 73 20 69 6e 20 70 44 61 74 61  f terms in pData
37c6d 20 66 6f 72 0a 20 20 2a 2a 20 73 6f 72 74 69 6e   for.  ** sortin
37c6e 67 2e 0a 20 20 2a 2f 0a 20 20 6e 54 65 72 6d 73  g..  */.  nTerms
37c6f 20 3d 20 66 74 73 33 48 61 73 68 43 6f 75 6e 74   = fts3HashCount
37c70 28 70 54 65 72 6d 73 29 3b 0a 20 20 61 73 73 65  (pTerms);.  asse
37c71 72 74 28 20 6e 54 65 72 6d 73 3e 30 20 29 3b 0a  rt( nTerms>0 );.
37c72 20 20 70 44 61 74 61 20 3d 20 73 71 6c 69 74 65    pData = sqlite
37c73 33 5f 6d 61 6c 6c 6f 63 28 6e 54 65 72 6d 73 2a  3_malloc(nTerms*
37c74 73 69 7a 65 6f 66 28 54 65 72 6d 44 61 74 61 29  sizeof(TermData)
37c75 29 3b 0a 20 20 69 66 28 20 70 44 61 74 61 3d 3d  );.  if( pData==
37c76 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51  NULL ) return SQ
37c77 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 6e  LITE_NOMEM;..  n
37c78 52 65 73 75 6c 74 42 79 74 65 73 20 3d 20 30 3b  ResultBytes = 0;
37c79 0a 20 20 66 6f 72 28 69 54 65 72 6d 20 3d 20 30  .  for(iTerm = 0
37c7a 2c 20 65 20 3d 20 66 74 73 33 48 61 73 68 46 69  , e = fts3HashFi
37c7b 72 73 74 28 70 54 65 72 6d 73 29 3b 20 65 3b 20  rst(pTerms); e; 
37c7c 69 54 65 72 6d 2b 2b 2c 20 65 20 3d 20 66 74 73  iTerm++, e = fts
37c7d 33 48 61 73 68 4e 65 78 74 28 65 29 29 7b 0a 20  3HashNext(e)){. 
37c7e 20 20 20 6e 52 65 73 75 6c 74 42 79 74 65 73 20     nResultBytes 
37c7f 2b 3d 20 66 74 73 33 48 61 73 68 4b 65 79 73 69  += fts3HashKeysi
37c80 7a 65 28 65 29 2b 31 3b 20 20 20 2f 2a 20 54 65  ze(e)+1;   /* Te
37c81 72 6d 20 70 6c 75 73 20 74 72 61 69 6c 69 6e 67  rm plus trailing
37c82 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 61 73   space */.    as
37c83 73 65 72 74 28 20 69 54 65 72 6d 3c 6e 54 65 72  sert( iTerm<nTer
37c84 6d 73 20 29 3b 0a 20 20 20 20 70 44 61 74 61 5b  ms );.    pData[
37c85 69 54 65 72 6d 5d 2e 70 54 65 72 6d 20 3d 20 66  iTerm].pTerm = f
37c86 74 73 33 48 61 73 68 4b 65 79 28 65 29 3b 0a 20  ts3HashKey(e);. 
37c87 20 20 20 70 44 61 74 61 5b 69 54 65 72 6d 5d 2e     pData[iTerm].
37c88 6e 54 65 72 6d 20 3d 20 66 74 73 33 48 61 73 68  nTerm = fts3Hash
37c89 4b 65 79 73 69 7a 65 28 65 29 3b 0a 20 20 20 20  Keysize(e);.    
37c8a 70 44 61 74 61 5b 69 54 65 72 6d 5d 2e 70 43 6f  pData[iTerm].pCo
37c8b 6c 6c 65 63 74 6f 72 20 3d 20 66 74 73 33 48 61  llector = fts3Ha
37c8c 73 68 44 61 74 61 28 65 29 3b 20 20 2f 2a 20 75  shData(e);  /* u
37c8d 6e 75 73 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 61  nused */.  }.  a
37c8e 73 73 65 72 74 28 20 69 54 65 72 6d 3d 3d 6e 54  ssert( iTerm==nT
37c8f 65 72 6d 73 20 29 3b 0a 0a 20 20 61 73 73 65 72  erms );..  asser
37c90 74 28 20 6e 52 65 73 75 6c 74 42 79 74 65 73 3e  t( nResultBytes>
37c91 30 20 29 3b 20 20 20 2f 2a 20 6e 54 65 72 6d 73  0 );   /* nTerms
37c92 3e 30 2c 20 6e 52 65 73 75 6c 74 73 42 79 74 65  >0, nResultsByte
37c93 73 20 6d 75 73 74 20 62 65 2c 20 74 6f 6f 2e 20  s must be, too. 
37c94 2a 2f 0a 20 20 72 65 73 75 6c 74 20 3d 20 73 71  */.  result = sq
37c95 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 52 65  lite3_malloc(nRe
37c96 73 75 6c 74 42 79 74 65 73 29 3b 0a 20 20 69 66  sultBytes);.  if
37c97 28 20 72 65 73 75 6c 74 3d 3d 4e 55 4c 4c 20 29  ( result==NULL )
37c98 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
37c99 65 65 28 70 44 61 74 61 29 3b 0a 20 20 20 20 72  ee(pData);.    r
37c9a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
37c9b 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e  EM;.  }..  if( n
37c9c 54 65 72 6d 73 3e 31 20 29 20 71 73 6f 72 74 28  Terms>1 ) qsort(
37c9d 70 44 61 74 61 2c 20 6e 54 65 72 6d 73 2c 20 73  pData, nTerms, s
37c9e 69 7a 65 6f 66 28 2a 70 44 61 74 61 29 2c 20 74  izeof(*pData), t
37c9f 65 72 6d 44 61 74 61 43 6d 70 29 3b 0a 0a 20 20  ermDataCmp);..  
37ca0 2f 2a 20 52 65 61 64 20 74 68 65 20 74 65 72 6d  /* Read the term
37ca1 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 75  s in order to bu
37ca2 69 6c 64 20 74 68 65 20 72 65 73 75 6c 74 2e 20  ild the result. 
37ca3 2a 2f 0a 20 20 69 42 79 74 65 20 3d 20 30 3b 0a  */.  iByte = 0;.
37ca4 20 20 66 6f 72 28 69 54 65 72 6d 3d 30 3b 20 69    for(iTerm=0; i
37ca5 54 65 72 6d 3c 6e 54 65 72 6d 73 3b 20 2b 2b 69  Term<nTerms; ++i
37ca6 54 65 72 6d 29 7b 0a 20 20 20 20 6d 65 6d 63 70  Term){.    memcp
37ca7 79 28 72 65 73 75 6c 74 2b 69 42 79 74 65 2c 20  y(result+iByte, 
37ca8 70 44 61 74 61 5b 69 54 65 72 6d 5d 2e 70 54 65  pData[iTerm].pTe
37ca9 72 6d 2c 20 70 44 61 74 61 5b 69 54 65 72 6d 5d  rm, pData[iTerm]
37caa 2e 6e 54 65 72 6d 29 3b 0a 20 20 20 20 69 42 79  .nTerm);.    iBy
37cab 74 65 20 2b 3d 20 70 44 61 74 61 5b 69 54 65 72  te += pData[iTer
37cac 6d 5d 2e 6e 54 65 72 6d 3b 0a 20 20 20 20 72 65  m].nTerm;.    re
37cad 73 75 6c 74 5b 69 42 79 74 65 2b 2b 5d 20 3d 20  sult[iByte++] = 
37cae 27 20 27 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  ' ';.  }.  asser
37caf 74 28 20 69 42 79 74 65 3d 3d 6e 52 65 73 75 6c  t( iByte==nResul
37cb0 74 42 79 74 65 73 20 29 3b 0a 20 20 61 73 73 65  tBytes );.  asse
37cb1 72 74 28 20 72 65 73 75 6c 74 5b 6e 52 65 73 75  rt( result[nResu
37cb2 6c 74 42 79 74 65 73 2d 31 5d 3d 3d 27 20 27 20  ltBytes-1]==' ' 
37cb3 29 3b 0a 20 20 72 65 73 75 6c 74 5b 6e 52 65 73  );.  result[nRes
37cb4 75 6c 74 42 79 74 65 73 2d 31 5d 20 3d 20 27 5c  ultBytes-1] = '\
37cb5 30 27 3b 0a 0a 20 20 2f 2a 20 50 61 73 73 65 73  0';..  /* Passes
37cb6 20 61 77 61 79 20 6f 77 6e 65 72 73 68 69 70 20   away ownership 
37cb7 6f 66 20 72 65 73 75 6c 74 2e 20 2a 2f 0a 20 20  of result. */.  
37cb8 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
37cb9 65 78 74 28 70 43 6f 6e 74 65 78 74 2c 20 72 65  ext(pContext, re
37cba 73 75 6c 74 2c 20 6e 52 65 73 75 6c 74 42 79 74  sult, nResultByt
37cbb 65 73 2d 31 2c 20 73 71 6c 69 74 65 33 5f 66 72  es-1, sqlite3_fr
37cbc 65 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ee);.  sqlite3_f
37cbd 72 65 65 28 70 44 61 74 61 29 3b 0a 20 20 72 65  ree(pData);.  re
37cbe 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
37cbf 7d 0a 0a 2f 2a 20 49 6d 70 6c 65 6d 65 6e 74 73  }../* Implements
37cc0 20 64 75 6d 70 5f 74 65 72 6d 73 28 29 20 66 6f   dump_terms() fo
37cc1 72 20 75 73 65 20 69 6e 20 69 6e 73 70 65 63 74  r use in inspect
37cc2 69 6e 67 20 74 68 65 20 66 74 73 33 20 69 6e 64  ing the fts3 ind
37cc3 65 78 20 66 72 6f 6d 0a 2a 2a 20 74 65 73 74 73  ex from.** tests
37cc4 2e 20 20 54 45 58 54 20 72 65 73 75 6c 74 20 63  .  TEXT result c
37cc5 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6f 72  ontaining the or
37cc6 64 65 72 65 64 20 6c 69 73 74 20 6f 66 20 74 65  dered list of te
37cc7 72 6d 73 20 6a 6f 69 6e 65 64 20 62 79 0a 2a 2a  rms joined by.**
37cc8 20 73 70 61 63 65 73 2e 20 20 64 75 6d 70 5f 74   spaces.  dump_t
37cc9 65 72 6d 73 28 74 2c 20 6c 65 76 65 6c 2c 20 69  erms(t, level, i
37cca 64 78 29 20 64 75 6d 70 73 20 74 68 65 20 74 65  dx) dumps the te
37ccb 72 6d 73 20 66 6f 72 20 74 68 65 20 73 65 67 6d  rms for the segm
37ccc 65 6e 74 0a 2a 2a 20 73 70 65 63 69 66 69 65 64  ent.** specified
37ccd 20 62 79 20 6c 65 76 65 6c 2c 20 69 64 78 20 28   by level, idx (
37cce 69 6e 20 25 5f 73 65 67 64 69 72 29 2c 20 77 68  in %_segdir), wh
37ccf 69 6c 65 20 64 75 6d 70 5f 74 65 72 6d 73 28 74  ile dump_terms(t
37cd0 29 20 64 75 6d 70 73 0a 2a 2a 20 61 6c 6c 20 74  ) dumps.** all t
37cd1 65 72 6d 73 20 69 6e 20 74 68 65 20 69 6e 64 65  erms in the inde
37cd2 78 2e 20 20 49 6e 20 62 6f 74 68 20 63 61 73 65  x.  In both case
37cd3 73 20 74 20 69 73 20 74 68 65 20 66 74 73 20 74  s t is the fts t
37cd4 61 62 6c 65 27 73 20 6d 61 67 69 63 0a 2a 2a 20  able's magic.** 
37cd5 74 61 62 6c 65 2d 6e 61 6d 65 64 20 63 6f 6c 75  table-named colu
37cd6 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  mn..*/.static vo
37cd7 69 64 20 64 75 6d 70 54 65 72 6d 73 46 75 6e 63  id dumpTermsFunc
37cd8 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
37cd9 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20  ext *pContext,. 
37cda 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74   int argc, sqlit
37cdb 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
37cdc 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75  ){.  fulltext_cu
37cdd 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20  rsor *pCursor;. 
37cde 20 69 66 28 20 61 72 67 63 21 3d 33 20 26 26 20   if( argc!=3 && 
37cdf 61 72 67 63 21 3d 31 20 29 7b 0a 20 20 20 20 67  argc!=1 ){.    g
37ce0 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f  enerateError(pCo
37ce1 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 74 65 72  ntext, "dump_ter
37ce2 6d 73 22 2c 20 22 69 6e 63 6f 72 72 65 63 74 20  ms", "incorrect 
37ce3 61 72 67 75 6d 65 6e 74 73 22 29 3b 0a 20 20 7d  arguments");.  }
37ce4 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33  else if( sqlite3
37ce5 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
37ce6 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f  [0])!=SQLITE_BLO
37ce7 42 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20  B ||.           
37ce8 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
37ce9 79 74 65 73 28 61 72 67 76 5b 30 5d 29 21 3d 73  ytes(argv[0])!=s
37cea 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 20 29  izeof(pCursor) )
37ceb 7b 0a 20 20 20 20 67 65 6e 65 72 61 74 65 45 72  {.    generateEr
37cec 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64  ror(pContext, "d
37ced 75 6d 70 5f 74 65 72 6d 73 22 2c 20 22 69 6c 6c  ump_terms", "ill
37cee 65 67 61 6c 20 66 69 72 73 74 20 61 72 67 75 6d  egal first argum
37cef 65 6e 74 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ent");.  }else{.
37cf0 20 20 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61      fulltext_vta
37cf1 62 20 2a 76 3b 0a 20 20 20 20 66 74 73 33 48 61  b *v;.    fts3Ha
37cf2 73 68 20 74 65 72 6d 73 3b 0a 20 20 20 20 73 71  sh terms;.    sq
37cf3 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 20 3d 20  lite3_stmt *s = 
37cf4 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20 72 63  NULL;.    int rc
37cf5 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70  ;..    memcpy(&p
37cf6 43 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 33 5f  Cursor, sqlite3_
37cf7 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b  value_blob(argv[
37cf8 30 5d 29 2c 20 73 69 7a 65 6f 66 28 70 43 75 72  0]), sizeof(pCur
37cf9 73 6f 72 29 29 3b 0a 20 20 20 20 76 20 3d 20 63  sor));.    v = c
37cfa 75 72 73 6f 72 5f 76 74 61 62 28 70 43 75 72 73  ursor_vtab(pCurs
37cfb 6f 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  or);..    /* If 
37cfc 70 61 73 73 65 64 20 6f 6e 6c 79 20 74 68 65 20  passed only the 
37cfd 63 75 72 73 6f 72 20 63 6f 6c 75 6d 6e 2c 20 67  cursor column, g
37cfe 65 74 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73 2e  et all segments.
37cff 20 20 4f 74 68 65 72 77 69 73 65 0a 20 20 20 20    Otherwise.    
37d00 2a 2a 20 67 65 74 20 74 68 65 20 73 65 67 6d 65  ** get the segme
37d01 6e 74 20 64 65 73 63 72 69 62 65 64 20 62 79 20  nt described by 
37d02 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77  the following tw
37d03 6f 20 61 72 67 75 6d 65 6e 74 73 2e 0a 20 20 20  o arguments..   
37d04 20 2a 2f 0a 20 20 20 20 69 66 28 20 61 72 67 63   */.    if( argc
37d05 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ==1 ){.      rc 
37d06 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d  = sql_get_statem
37d07 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 53 45  ent(v, SEGDIR_SE
37d08 4c 45 43 54 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26  LECT_ALL_STMT, &
37d09 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  s);.    }else{. 
37d0a 20 20 20 20 20 72 63 20 3d 20 73 71 6c 5f 67 65       rc = sql_ge
37d0b 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 53  t_statement(v, S
37d0c 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 53 45 47  EGDIR_SELECT_SEG
37d0d 4d 45 4e 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a  MENT_STMT, &s);.
37d0e 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
37d0f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
37d10 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
37d11 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20 73  bind_int(s, 1, s
37d12 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
37d13 28 61 72 67 76 5b 31 5d 29 29 3b 0a 20 20 20 20  (argv[1]));.    
37d14 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
37d15 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
37d16 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
37d17 62 69 6e 64 5f 69 6e 74 28 73 2c 20 32 2c 20 73  bind_int(s, 2, s
37d18 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
37d19 28 61 72 67 76 5b 32 5d 29 29 3b 0a 20 20 20 20  (argv[2]));.    
37d1a 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
37d1b 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 21    }..    if( rc!
37d1c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
37d1d 20 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f      generateErro
37d1e 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d  r(pContext, "dum
37d1f 70 5f 74 65 72 6d 73 22 2c 20 4e 55 4c 4c 29 3b  p_terms", NULL);
37d20 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
37d21 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6c     }..    /* Col
37d22 6c 65 63 74 20 74 68 65 20 74 65 72 6d 73 20 66  lect the terms f
37d23 6f 72 20 65 61 63 68 20 73 65 67 6d 65 6e 74 2e  or each segment.
37d24 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 46   */.    sqlite3F
37d25 74 73 33 48 61 73 68 49 6e 69 74 28 26 74 65 72  ts3HashInit(&ter
37d26 6d 73 2c 20 46 54 53 33 5f 48 41 53 48 5f 53 54  ms, FTS3_HASH_ST
37d27 52 49 4e 47 2c 20 31 29 3b 0a 20 20 20 20 77 68  RING, 1);.    wh
37d28 69 6c 65 28 20 28 72 63 20 3d 20 73 71 6c 69 74  ile( (rc = sqlit
37d29 65 33 5f 73 74 65 70 28 73 29 29 3d 3d 53 51 4c  e3_step(s))==SQL
37d2a 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
37d2b 20 72 63 20 3d 20 63 6f 6c 6c 65 63 74 53 65 67   rc = collectSeg
37d2c 6d 65 6e 74 54 65 72 6d 73 28 76 2c 20 73 2c 20  mentTerms(v, s, 
37d2d 26 74 65 72 6d 73 29 3b 0a 20 20 20 20 20 20 69  &terms);.      i
37d2e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
37d2f 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a   ) break;.    }.
37d30 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
37d31 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20  ITE_DONE ){.    
37d32 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28    sqlite3_reset(
37d33 73 29 3b 0a 20 20 20 20 20 20 67 65 6e 65 72 61  s);.      genera
37d34 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74  teError(pContext
37d35 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20  , "dump_terms", 
37d36 4e 55 4c 4c 29 3b 0a 20 20 20 20 7d 65 6c 73 65  NULL);.    }else
37d37 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e  {.      const in
37d38 74 20 6e 54 65 72 6d 73 20 3d 20 66 74 73 33 48  t nTerms = fts3H
37d39 61 73 68 43 6f 75 6e 74 28 26 74 65 72 6d 73 29  ashCount(&terms)
37d3a 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 54 65 72  ;.      if( nTer
37d3b 6d 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ms>0 ){.        
37d3c 72 63 20 3d 20 67 65 6e 65 72 61 74 65 54 65 72  rc = generateTer
37d3d 6d 73 52 65 73 75 6c 74 28 70 43 6f 6e 74 65 78  msResult(pContex
37d3e 74 2c 20 26 74 65 72 6d 73 29 3b 0a 20 20 20 20  t, &terms);.    
37d3f 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
37d40 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20  TE_NOMEM ){.    
37d41 20 20 20 20 20 20 67 65 6e 65 72 61 74 65 45 72        generateEr
37d42 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64  ror(pContext, "d
37d43 75 6d 70 5f 74 65 72 6d 73 22 2c 20 22 6f 75 74  ump_terms", "out
37d44 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20   of memory");.  
37d45 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
37d46 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72         assert( r
37d47 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c==SQLITE_OK );.
37d48 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
37d49 7d 65 6c 73 65 20 69 66 28 20 61 72 67 63 3d 3d  }else if( argc==
37d4a 33 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  3 ){.        /* 
37d4b 54 68 65 20 73 70 65 63 69 66 69 63 20 73 65 67  The specific seg
37d4c 6d 65 6e 74 20 61 73 6b 65 64 20 66 6f 72 20 63  ment asked for c
37d4d 6f 75 6c 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e  ould not be foun
37d4e 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 67 65  d. */.        ge
37d4f 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e  nerateError(pCon
37d50 74 65 78 74 2c 20 22 64 75 6d 70 5f 74 65 72 6d  text, "dump_term
37d51 73 22 2c 20 22 73 65 67 6d 65 6e 74 20 6e 6f 74  s", "segment not
37d52 20 66 6f 75 6e 64 22 29 3b 0a 20 20 20 20 20 20   found");.      
37d53 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
37d54 2a 20 4e 6f 20 73 65 67 6d 65 6e 74 73 20 66 6f  * No segments fo
37d55 75 6e 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  und. */.        
37d56 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20  /* TODO(shess): 
37d57 49 74 20 73 68 6f 75 6c 64 20 62 65 20 69 6d 70  It should be imp
37d58 6f 73 73 69 62 6c 65 20 74 6f 20 72 65 61 63 68  ossible to reach
37d59 20 74 68 69 73 2e 20 20 54 68 69 73 0a 20 20 20   this.  This.   
37d5a 20 20 20 20 20 2a 2a 20 63 61 73 65 20 63 61 6e       ** case can
37d5b 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 66 6f 72   only happen for
37d5c 20 61 6e 20 65 6d 70 74 79 20 74 61 62 6c 65 2c   an empty table,
37d5d 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 0a 20   in which case. 
37d5e 20 20 20 20 20 20 20 2a 2a 20 53 51 4c 69 74 65         ** SQLite
37d5f 20 68 61 73 20 6e 6f 20 72 6f 77 73 20 74 6f 20   has no rows to 
37d60 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69  call this functi
37d61 6f 6e 20 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a  on on..        *
37d62 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  /.        sqlite
37d63 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 70 43  3_result_null(pC
37d64 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 7d  ontext);.      }
37d65 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
37d66 65 33 46 74 73 33 48 61 73 68 43 6c 65 61 72 28  e3Fts3HashClear(
37d67 26 74 65 72 6d 73 29 3b 0a 20 20 7d 0a 7d 0a 0a  &terms);.  }.}..
37d68 2f 2a 20 45 78 70 61 6e 64 20 74 68 65 20 44 4c  /* Expand the DL
37d69 5f 44 45 46 41 55 4c 54 20 64 6f 63 6c 69 73 74  _DEFAULT doclist
37d6a 20 69 6e 20 70 44 61 74 61 20 69 6e 74 6f 20 61   in pData into a
37d6b 20 74 65 78 74 20 72 65 73 75 6c 74 20 69 6e 0a   text result in.
37d6c 2a 2a 20 70 43 6f 6e 74 65 78 74 2e 0a 2a 2f 0a  ** pContext..*/.
37d6d 73 74 61 74 69 63 20 76 6f 69 64 20 63 72 65 61  static void crea
37d6e 74 65 44 6f 63 6c 69 73 74 52 65 73 75 6c 74 28  teDoclistResult(
37d6f 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
37d70 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20  *pContext,.     
37d71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37d72 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
37d73 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e   char *pData, in
37d74 74 20 6e 44 61 74 61 29 7b 0a 20 20 44 61 74 61  t nData){.  Data
37d75 42 75 66 66 65 72 20 64 75 6d 70 3b 0a 20 20 44  Buffer dump;.  D
37d76 4c 52 65 61 64 65 72 20 64 6c 52 65 61 64 65 72  LReader dlReader
37d77 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 44 61  ;..  assert( pDa
37d78 74 61 21 3d 4e 55 4c 4c 20 26 26 20 6e 44 61 74  ta!=NULL && nDat
37d79 61 3e 30 20 29 3b 0a 0a 20 20 64 61 74 61 42 75  a>0 );..  dataBu
37d7a 66 66 65 72 49 6e 69 74 28 26 64 75 6d 70 2c 20  fferInit(&dump, 
37d7b 30 29 3b 0a 20 20 64 6c 72 49 6e 69 74 28 26 64  0);.  dlrInit(&d
37d7c 6c 52 65 61 64 65 72 2c 20 44 4c 5f 44 45 46 41  lReader, DL_DEFA
37d7d 55 4c 54 2c 20 70 44 61 74 61 2c 20 6e 44 61 74  ULT, pData, nDat
37d7e 61 29 3b 0a 20 20 66 6f 72 28 20 3b 20 21 64 6c  a);.  for( ; !dl
37d7f 72 41 74 45 6e 64 28 26 64 6c 52 65 61 64 65 72  rAtEnd(&dlReader
37d80 29 3b 20 64 6c 72 53 74 65 70 28 26 64 6c 52 65  ); dlrStep(&dlRe
37d81 61 64 65 72 29 20 29 7b 0a 20 20 20 20 63 68 61  ader) ){.    cha
37d82 72 20 62 75 66 5b 32 35 36 5d 3b 0a 20 20 20 20  r buf[256];.    
37d83 50 4c 52 65 61 64 65 72 20 70 6c 52 65 61 64 65  PLReader plReade
37d84 72 3b 0a 0a 20 20 20 20 70 6c 72 49 6e 69 74 28  r;..    plrInit(
37d85 26 70 6c 52 65 61 64 65 72 2c 20 26 64 6c 52 65  &plReader, &dlRe
37d86 61 64 65 72 29 3b 0a 20 20 20 20 69 66 28 20 44  ader);.    if( D
37d87 4c 5f 44 45 46 41 55 4c 54 3d 3d 44 4c 5f 44 4f  L_DEFAULT==DL_DO
37d88 43 49 44 53 20 7c 7c 20 70 6c 72 41 74 45 6e 64  CIDS || plrAtEnd
37d89 28 26 70 6c 52 65 61 64 65 72 29 20 29 7b 0a 20  (&plReader) ){. 
37d8a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
37d8b 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66  rintf(sizeof(buf
37d8c 29 2c 20 62 75 66 2c 20 22 5b 25 6c 6c 64 5d 20  ), buf, "[%lld] 
37d8d 22 2c 20 64 6c 72 44 6f 63 69 64 28 26 64 6c 52  ", dlrDocid(&dlR
37d8e 65 61 64 65 72 29 29 3b 0a 20 20 20 20 20 20 64  eader));.      d
37d8f 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
37d90 26 64 75 6d 70 2c 20 62 75 66 2c 20 73 74 72 6c  &dump, buf, strl
37d91 65 6e 28 62 75 66 29 29 3b 0a 20 20 20 20 7d 65  en(buf));.    }e
37d92 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  lse{.      int i
37d93 43 6f 6c 75 6d 6e 20 3d 20 70 6c 72 43 6f 6c 75  Column = plrColu
37d94 6d 6e 28 26 70 6c 52 65 61 64 65 72 29 3b 0a 0a  mn(&plReader);..
37d95 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
37d96 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 62 75  printf(sizeof(bu
37d97 66 29 2c 20 62 75 66 2c 20 22 5b 25 6c 6c 64 20  f), buf, "[%lld 
37d98 25 64 5b 22 2c 0a 20 20 20 20 20 20 20 20 20 20  %d[",.          
37d99 20 20 20 20 20 20 20 20 20 20 20 20 20 64 6c 72               dlr
37d9a 44 6f 63 69 64 28 26 64 6c 52 65 61 64 65 72 29  Docid(&dlReader)
37d9b 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  , iColumn);.    
37d9c 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
37d9d 6e 64 28 26 64 75 6d 70 2c 20 62 75 66 2c 20 73  nd(&dump, buf, s
37d9e 74 72 6c 65 6e 28 62 75 66 29 29 3b 0a 0a 20 20  trlen(buf));..  
37d9f 20 20 20 20 66 6f 72 28 20 3b 20 21 70 6c 72 41      for( ; !plrA
37da0 74 45 6e 64 28 26 70 6c 52 65 61 64 65 72 29 3b  tEnd(&plReader);
37da1 20 70 6c 72 53 74 65 70 28 26 70 6c 52 65 61 64   plrStep(&plRead
37da2 65 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69  er) ){.        i
37da3 66 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 70 6c  f( plrColumn(&pl
37da4 52 65 61 64 65 72 29 21 3d 69 43 6f 6c 75 6d 6e  Reader)!=iColumn
37da5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 43   ){.          iC
37da6 6f 6c 75 6d 6e 20 3d 20 70 6c 72 43 6f 6c 75 6d  olumn = plrColum
37da7 6e 28 26 70 6c 52 65 61 64 65 72 29 3b 0a 20 20  n(&plReader);.  
37da8 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
37da9 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
37daa 62 75 66 29 2c 20 62 75 66 2c 20 22 5d 20 25 64  buf), buf, "] %d
37dab 5b 22 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20  [", iColumn);.  
37dac 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
37dad 64 75 6d 70 2e 6e 44 61 74 61 3e 30 20 29 3b 0a  dump.nData>0 );.
37dae 20 20 20 20 20 20 20 20 20 20 64 75 6d 70 2e 6e            dump.n
37daf 44 61 74 61 2d 2d 3b 20 20 20 20 20 20 20 20 20  Data--;         
37db0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
37db1 76 65 72 77 72 69 74 65 20 74 72 61 69 6c 69 6e  verwrite trailin
37db2 67 20 73 70 61 63 65 2e 20 2a 2f 0a 20 20 20 20  g space. */.    
37db3 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64 75        assert( du
37db4 6d 70 2e 70 44 61 74 61 5b 64 75 6d 70 2e 6e 44  mp.pData[dump.nD
37db5 61 74 61 5d 3d 3d 27 20 27 29 3b 0a 20 20 20 20  ata]==' ');.    
37db6 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72        dataBuffer
37db7 41 70 70 65 6e 64 28 26 64 75 6d 70 2c 20 62 75  Append(&dump, bu
37db8 66 2c 20 73 74 72 6c 65 6e 28 62 75 66 29 29 3b  f, strlen(buf));
37db9 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
37dba 20 20 20 69 66 28 20 44 4c 5f 44 45 46 41 55 4c     if( DL_DEFAUL
37dbb 54 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f  T==DL_POSITIONS_
37dbc 4f 46 46 53 45 54 53 20 29 7b 0a 20 20 20 20 20  OFFSETS ){.     
37dbd 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
37dbe 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66  rintf(sizeof(buf
37dbf 29 2c 20 62 75 66 2c 20 22 25 64 2c 25 64 2c 25  ), buf, "%d,%d,%
37dc0 64 20 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  d ",.           
37dc1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37dc2 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26 70 6c 52  plrPosition(&plR
37dc3 65 61 64 65 72 29 2c 0a 20 20 20 20 20 20 20 20  eader),.        
37dc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37dc5 20 20 20 70 6c 72 53 74 61 72 74 4f 66 66 73 65     plrStartOffse
37dc6 74 28 26 70 6c 52 65 61 64 65 72 29 2c 20 70 6c  t(&plReader), pl
37dc7 72 45 6e 64 4f 66 66 73 65 74 28 26 70 6c 52 65  rEndOffset(&plRe
37dc8 61 64 65 72 29 29 3b 0a 20 20 20 20 20 20 20 20  ader));.        
37dc9 7d 65 6c 73 65 20 69 66 28 20 44 4c 5f 44 45 46  }else if( DL_DEF
37dca 41 55 4c 54 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f  AULT==DL_POSITIO
37dcb 4e 53 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  NS ){.          
37dcc 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
37dcd 28 73 69 7a 65 6f 66 28 62 75 66 29 2c 20 62 75  (sizeof(buf), bu
37dce 66 2c 20 22 25 64 20 22 2c 20 70 6c 72 50 6f 73  f, "%d ", plrPos
37dcf 69 74 69 6f 6e 28 26 70 6c 52 65 61 64 65 72 29  ition(&plReader)
37dd0 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
37dd1 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
37dd2 72 74 28 20 4e 55 4c 4c 3d 3d 22 55 6e 68 61 6e  rt( NULL=="Unhan
37dd3 64 6c 65 64 20 44 4c 5f 44 45 46 41 55 4c 54 20  dled DL_DEFAULT 
37dd4 76 61 6c 75 65 22 29 3b 0a 20 20 20 20 20 20 20  value");.       
37dd5 20 7d 0a 20 20 20 20 20 20 20 20 64 61 74 61 42   }.        dataB
37dd6 75 66 66 65 72 41 70 70 65 6e 64 28 26 64 75 6d  ufferAppend(&dum
37dd7 70 2c 20 62 75 66 2c 20 73 74 72 6c 65 6e 28 62  p, buf, strlen(b
37dd8 75 66 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  uf));.      }.  
37dd9 20 20 20 20 70 6c 72 44 65 73 74 72 6f 79 28 26      plrDestroy(&
37dda 70 6c 52 65 61 64 65 72 29 3b 0a 0a 20 20 20 20  plReader);..    
37ddb 20 20 61 73 73 65 72 74 28 20 64 75 6d 70 2e 6e    assert( dump.n
37ddc 44 61 74 61 3e 30 20 29 3b 0a 20 20 20 20 20 20  Data>0 );.      
37ddd 64 75 6d 70 2e 6e 44 61 74 61 2d 2d 3b 20 20 20  dump.nData--;   
37dde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37ddf 20 20 2f 2a 20 4f 76 65 72 77 72 69 74 65 20 74    /* Overwrite t
37de0 72 61 69 6c 69 6e 67 20 73 70 61 63 65 2e 20 2a  railing space. *
37de1 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  /.      assert( 
37de2 64 75 6d 70 2e 70 44 61 74 61 5b 64 75 6d 70 2e  dump.pData[dump.
37de3 6e 44 61 74 61 5d 3d 3d 27 20 27 29 3b 0a 20 20  nData]==' ');.  
37de4 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70      dataBufferAp
37de5 70 65 6e 64 28 26 64 75 6d 70 2c 20 22 5d 5d 20  pend(&dump, "]] 
37de6 22 2c 20 33 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ", 3);.    }.  }
37de7 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 64  .  dlrDestroy(&d
37de8 6c 52 65 61 64 65 72 29 3b 0a 0a 20 20 61 73 73  lReader);..  ass
37de9 65 72 74 28 20 64 75 6d 70 2e 6e 44 61 74 61 3e  ert( dump.nData>
37dea 30 20 29 3b 0a 20 20 64 75 6d 70 2e 6e 44 61 74  0 );.  dump.nDat
37deb 61 2d 2d 3b 20 20 20 20 20 20 20 20 20 20 20 20  a--;            
37dec 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 76 65 72           /* Over
37ded 77 72 69 74 65 20 74 72 61 69 6c 69 6e 67 20 73  write trailing s
37dee 70 61 63 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72  pace. */.  asser
37def 74 28 20 64 75 6d 70 2e 70 44 61 74 61 5b 64 75  t( dump.pData[du
37df0 6d 70 2e 6e 44 61 74 61 5d 3d 3d 27 20 27 29 3b  mp.nData]==' ');
37df1 0a 20 20 64 75 6d 70 2e 70 44 61 74 61 5b 64 75  .  dump.pData[du
37df2 6d 70 2e 6e 44 61 74 61 5d 20 3d 20 27 5c 30 27  mp.nData] = '\0'
37df3 3b 0a 20 20 61 73 73 65 72 74 28 20 64 75 6d 70  ;.  assert( dump
37df4 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 0a 20 20 2f  .nData>0 );..  /
37df5 2a 20 50 61 73 73 65 73 20 6f 77 6e 65 72 73 68  * Passes ownersh
37df6 69 70 20 6f 66 20 64 75 6d 70 27 73 20 62 75 66  ip of dump's buf
37df7 66 65 72 20 74 6f 20 70 43 6f 6e 74 65 78 74 2e  fer to pContext.
37df8 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 72 65   */.  sqlite3_re
37df9 73 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65  sult_text(pConte
37dfa 78 74 2c 20 64 75 6d 70 2e 70 44 61 74 61 2c 20  xt, dump.pData, 
37dfb 64 75 6d 70 2e 6e 44 61 74 61 2c 20 73 71 6c 69  dump.nData, sqli
37dfc 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 64 75 6d  te3_free);.  dum
37dfd 70 2e 70 44 61 74 61 20 3d 20 4e 55 4c 4c 3b 0a  p.pData = NULL;.
37dfe 20 20 64 75 6d 70 2e 6e 44 61 74 61 20 3d 20 64    dump.nData = d
37dff 75 6d 70 2e 6e 43 61 70 61 63 69 74 79 20 3d 20  ump.nCapacity = 
37e00 30 3b 0a 7d 0a 0a 2f 2a 20 49 6d 70 6c 65 6d 65  0;.}../* Impleme
37e01 6e 74 73 20 64 75 6d 70 5f 64 6f 63 6c 69 73 74  nts dump_doclist
37e02 28 29 20 66 6f 72 20 75 73 65 20 69 6e 20 69 6e  () for use in in
37e03 73 70 65 63 74 69 6e 67 20 74 68 65 20 66 74 73  specting the fts
37e04 33 20 69 6e 64 65 78 20 66 72 6f 6d 0a 2a 2a 20  3 index from.** 
37e05 74 65 73 74 73 2e 20 20 54 45 58 54 20 72 65 73  tests.  TEXT res
37e06 75 6c 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61  ult containing a
37e07 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e   string represen
37e08 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a  tation of the.**
37e09 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 74 68 65   doclist for the
37e0a 20 69 6e 64 69 63 61 74 65 64 20 74 65 72 6d 2e   indicated term.
37e0b 20 20 64 75 6d 70 5f 64 6f 63 6c 69 73 74 28 74    dump_doclist(t
37e0c 2c 20 74 65 72 6d 2c 20 6c 65 76 65 6c 2c 20 69  , term, level, i
37e0d 64 78 29 0a 2a 2a 20 64 75 6d 70 73 20 74 68 65  dx).** dumps the
37e0e 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 74 65 72   doclist for ter
37e0f 6d 20 66 72 6f 6d 20 74 68 65 20 73 65 67 6d 65  m from the segme
37e10 6e 74 20 73 70 65 63 69 66 69 65 64 20 62 79 20  nt specified by 
37e11 6c 65 76 65 6c 2c 20 69 64 78 0a 2a 2a 20 28 69  level, idx.** (i
37e12 6e 20 25 5f 73 65 67 64 69 72 29 2c 20 77 68 69  n %_segdir), whi
37e13 6c 65 20 64 75 6d 70 5f 64 6f 63 6c 69 73 74 28  le dump_doclist(
37e14 74 2c 20 74 65 72 6d 29 20 64 75 6d 70 73 20 74  t, term) dumps t
37e15 68 65 20 6c 6f 67 69 63 61 6c 0a 2a 2a 20 64 6f  he logical.** do
37e16 63 6c 69 73 74 20 66 6f 72 20 74 68 65 20 74 65  clist for the te
37e17 72 6d 20 61 63 72 6f 73 73 20 61 6c 6c 20 73 65  rm across all se
37e18 67 6d 65 6e 74 73 2e 20 20 54 68 65 20 70 65 72  gments.  The per
37e19 2d 73 65 67 6d 65 6e 74 20 64 6f 63 6c 69 73 74  -segment doclist
37e1a 0a 2a 2a 20 63 61 6e 20 63 6f 6e 74 61 69 6e 20  .** can contain 
37e1b 64 65 6c 65 74 69 6f 6e 73 2c 20 77 68 69 6c 65  deletions, while
37e1c 20 74 68 65 20 66 75 6c 6c 2d 69 6e 64 65 78 20   the full-index 
37e1d 64 6f 63 6c 69 73 74 20 77 69 6c 6c 20 6e 6f 74  doclist will not
37e1e 0a 2a 2a 20 28 64 65 6c 65 74 69 6f 6e 73 20 61  .** (deletions a
37e1f 72 65 20 6f 6d 69 74 74 65 64 29 2e 0a 2a 2a 0a  re omitted)..**.
37e20 2a 2a 20 52 65 73 75 6c 74 20 66 6f 72 6d 61 74  ** Result format
37e21 73 20 64 69 66 66 65 72 20 77 69 74 68 20 74 68  s differ with th
37e22 65 20 73 65 74 74 69 6e 67 20 6f 66 20 44 4c 5f  e setting of DL_
37e23 44 45 46 41 55 4c 54 53 2e 20 20 45 78 61 6d 70  DEFAULTS.  Examp
37e24 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 44 4c 5f 44 4f  les:.**.** DL_DO
37e25 43 49 44 53 3a 20 5b 31 5d 20 5b 33 5d 20 5b 37  CIDS: [1] [3] [7
37e26 5d 0a 2a 2a 20 44 4c 5f 50 4f 53 49 54 49 4f 4e  ].** DL_POSITION
37e27 53 3a 20 5b 31 20 30 5b 30 20 34 5d 20 31 5b 31  S: [1 0[0 4] 1[1
37e28 37 5d 5d 20 5b 33 20 31 5b 35 5d 5d 0a 2a 2a 20  7]] [3 1[5]].** 
37e29 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46  DL_POSITIONS_OFF
37e2a 53 45 54 53 3a 20 5b 31 20 30 5b 30 2c 30 2c 33  SETS: [1 0[0,0,3
37e2b 20 34 2c 32 33 2c 32 36 5d 20 31 5b 31 37 2c 31   4,23,26] 1[17,1
37e2c 30 32 2c 31 30 35 5d 5d 20 5b 33 20 31 5b 35 2c  02,105]] [3 1[5,
37e2d 32 30 2c 32 33 5d 5d 0a 2a 2a 0a 2a 2a 20 49 6e  20,23]].**.** In
37e2e 20 65 61 63 68 20 63 61 73 65 20 74 68 65 20 6e   each case the n
37e2f 75 6d 62 65 72 20 61 66 74 65 72 20 74 68 65 20  umber after the 
37e30 6f 75 74 65 72 20 27 5b 27 20 69 73 20 74 68 65  outer '[' is the
37e31 20 64 6f 63 69 64 2e 20 20 49 6e 20 74 68 65 0a   docid.  In the.
37e32 2a 2a 20 6c 61 74 74 65 72 20 74 77 6f 20 63 61  ** latter two ca
37e33 73 65 73 2c 20 74 68 65 20 6e 75 6d 62 65 72 20  ses, the number 
37e34 62 65 66 6f 72 65 20 74 68 65 20 69 6e 6e 65 72  before the inner
37e35 20 27 5b 27 20 69 73 20 74 68 65 20 63 6f 6c 75   '[' is the colu
37e36 6d 6e 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64  mn.** associated
37e37 20 77 69 74 68 20 74 68 65 20 76 61 6c 75 65 73   with the values
37e38 20 77 69 74 68 69 6e 2e 20 20 46 6f 72 20 44 4c   within.  For DL
37e39 5f 50 4f 53 49 54 49 4f 4e 53 20 74 68 65 20 6e  _POSITIONS the n
37e3a 75 6d 62 65 72 73 0a 2a 2a 20 77 69 74 68 69 6e  umbers.** within
37e3b 20 61 72 65 20 74 68 65 20 70 6f 73 69 74 69 6f   are the positio
37e3c 6e 73 2c 20 66 6f 72 20 44 4c 5f 50 4f 53 49 54  ns, for DL_POSIT
37e3d 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 74 68 65  IONS_OFFSETS the
37e3e 79 20 61 72 65 20 74 68 65 0a 2a 2a 20 70 6f 73  y are the.** pos
37e3f 69 74 69 6f 6e 2c 20 74 68 65 20 73 74 61 72 74  ition, the start
37e40 20 6f 66 66 73 65 74 2c 20 61 6e 64 20 74 68 65   offset, and the
37e41 20 65 6e 64 20 6f 66 66 73 65 74 2e 0a 2a 2f 0a   end offset..*/.
37e42 73 74 61 74 69 63 20 76 6f 69 64 20 64 75 6d 70  static void dump
37e43 44 6f 63 6c 69 73 74 46 75 6e 63 28 0a 20 20 73  DoclistFunc(.  s
37e44 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
37e45 70 43 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  pContext,.  int 
37e46 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61  argc, sqlite3_va
37e47 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
37e48 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20  fulltext_cursor 
37e49 2a 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 20  *pCursor;.  if( 
37e4a 61 72 67 63 21 3d 32 20 26 26 20 61 72 67 63 21  argc!=2 && argc!
37e4b 3d 34 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61  =4 ){.    genera
37e4c 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74  teError(pContext
37e4d 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22  , "dump_doclist"
37e4e 2c 20 22 69 6e 63 6f 72 72 65 63 74 20 61 72 67  , "incorrect arg
37e4f 75 6d 65 6e 74 73 22 29 3b 0a 20 20 7d 65 6c 73  uments");.  }els
37e50 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61  e if( sqlite3_va
37e51 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
37e52 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 7c  )!=SQLITE_BLOB |
37e53 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  |.            sq
37e54 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
37e55 73 28 61 72 67 76 5b 30 5d 29 21 3d 73 69 7a 65  s(argv[0])!=size
37e56 6f 66 28 70 43 75 72 73 6f 72 29 20 29 7b 0a 20  of(pCursor) ){. 
37e57 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72     generateError
37e58 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70  (pContext, "dump
37e59 5f 64 6f 63 6c 69 73 74 22 2c 20 22 69 6c 6c 65  _doclist", "ille
37e5a 67 61 6c 20 66 69 72 73 74 20 61 72 67 75 6d 65  gal first argume
37e5b 6e 74 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  nt");.  }else if
37e5c 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
37e5d 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3d 3d 4e  text(argv[1])==N
37e5e 55 4c 4c 20 7c 7c 0a 20 20 20 20 20 20 20 20 20  ULL ||.         
37e5f 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65     sqlite3_value
37e60 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 5b 30  _text(argv[1])[0
37e61 5d 3d 3d 27 5c 30 27 20 29 7b 0a 20 20 20 20 67  ]=='\0' ){.    g
37e62 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f  enerateError(pCo
37e63 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63  ntext, "dump_doc
37e64 6c 69 73 74 22 2c 20 22 65 6d 70 74 79 20 73 65  list", "empty se
37e65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 22 29 3b  cond argument");
37e66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f  .  }else{.    co
37e67 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 20  nst char *pTerm 
37e68 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
37e69 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
37e6a 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20  xt(argv[1]);.   
37e6b 20 63 6f 6e 73 74 20 69 6e 74 20 6e 54 65 72 6d   const int nTerm
37e6c 20 3d 20 73 74 72 6c 65 6e 28 70 54 65 72 6d 29   = strlen(pTerm)
37e6d 3b 0a 20 20 20 20 66 75 6c 6c 74 65 78 74 5f 76  ;.    fulltext_v
37e6e 74 61 62 20 2a 76 3b 0a 20 20 20 20 69 6e 74 20  tab *v;.    int 
37e6f 72 63 3b 0a 20 20 20 20 44 61 74 61 42 75 66 66  rc;.    DataBuff
37e70 65 72 20 64 6f 63 6c 69 73 74 3b 0a 0a 20 20 20  er doclist;..   
37e71 20 6d 65 6d 63 70 79 28 26 70 43 75 72 73 6f 72   memcpy(&pCursor
37e72 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  , sqlite3_value_
37e73 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 2c 20 73  blob(argv[0]), s
37e74 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 29 3b  izeof(pCursor));
37e75 0a 20 20 20 20 76 20 3d 20 63 75 72 73 6f 72 5f  .    v = cursor_
37e76 76 74 61 62 28 70 43 75 72 73 6f 72 29 3b 0a 0a  vtab(pCursor);..
37e77 20 20 20 20 64 61 74 61 42 75 66 66 65 72 49 6e      dataBufferIn
37e78 69 74 28 26 64 6f 63 6c 69 73 74 2c 20 30 29 3b  it(&doclist, 0);
37e79 0a 0a 20 20 20 20 2f 2a 20 74 65 72 6d 53 65 6c  ..    /* termSel
37e7a 65 63 74 28 29 20 79 69 65 6c 64 73 20 74 68 65  ect() yields the
37e7b 20 73 61 6d 65 20 6c 6f 67 69 63 61 6c 20 64 6f   same logical do
37e7c 63 6c 69 73 74 20 74 68 61 74 20 71 75 65 72 69  clist that queri
37e7d 65 73 20 61 72 65 0a 20 20 20 20 2a 2a 20 72 75  es are.    ** ru
37e7e 6e 20 61 67 61 69 6e 73 74 2e 0a 20 20 20 20 2a  n against..    *
37e7f 2f 0a 20 20 20 20 69 66 28 20 61 72 67 63 3d 3d  /.    if( argc==
37e80 32 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  2 ){.      rc = 
37e81 74 65 72 6d 53 65 6c 65 63 74 28 76 2c 20 76 2d  termSelect(v, v-
37e82 3e 6e 43 6f 6c 75 6d 6e 2c 20 70 54 65 72 6d 2c  >nColumn, pTerm,
37e83 20 6e 54 65 72 6d 2c 20 30 2c 20 44 4c 5f 44 45   nTerm, 0, DL_DE
37e84 46 41 55 4c 54 2c 20 26 64 6f 63 6c 69 73 74 29  FAULT, &doclist)
37e85 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
37e86 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20     sqlite3_stmt 
37e87 2a 73 20 3d 20 4e 55 4c 4c 3b 0a 0a 20 20 20 20  *s = NULL;..    
37e88 20 20 2f 2a 20 47 65 74 20 6f 75 72 20 73 70 65    /* Get our spe
37e89 63 69 66 69 63 20 73 65 67 6d 65 6e 74 27 73 20  cific segment's 
37e8a 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 2a 2f 0a  information. */.
37e8b 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 5f 67        rc = sql_g
37e8c 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
37e8d 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 53 45  SEGDIR_SELECT_SE
37e8e 47 4d 45 4e 54 5f 53 54 4d 54 2c 20 26 73 29 3b  GMENT_STMT, &s);
37e8f 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
37e90 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
37e91 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
37e92 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20  _bind_int(s, 1, 
37e93 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
37e94 74 28 61 72 67 76 5b 32 5d 29 29 3b 0a 20 20 20  t(argv[2]));.   
37e95 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
37e96 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
37e97 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
37e98 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 32 2c 20  _bind_int(s, 2, 
37e99 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
37e9a 74 28 61 72 67 76 5b 33 5d 29 29 3b 0a 20 20 20  t(argv[3]));.   
37e9b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a       }.      }..
37e9c 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
37e9d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
37e9e 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
37e9f 73 74 65 70 28 73 29 3b 0a 0a 20 20 20 20 20 20  step(s);..      
37ea0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
37ea1 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20  _DONE ){.       
37ea2 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73     dataBufferDes
37ea3 74 72 6f 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a  troy(&doclist);.
37ea4 20 20 20 20 20 20 20 20 20 20 67 65 6e 65 72 61            genera
37ea5 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74  teError(pContext
37ea6 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22  , "dump_doclist"
37ea7 2c 20 22 73 65 67 6d 65 6e 74 20 6e 6f 74 20 66  , "segment not f
37ea8 6f 75 6e 64 22 29 3b 0a 20 20 20 20 20 20 20 20  ound");.        
37ea9 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
37eaa 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20    }..        /* 
37eab 46 6f 75 6e 64 20 61 20 73 65 67 6d 65 6e 74 2c  Found a segment,
37eac 20 6c 6f 61 64 20 69 74 20 69 6e 74 6f 20 64 6f   load it into do
37ead 63 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20  clist. */.      
37eae 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
37eaf 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 20 20  _ROW ){.        
37eb0 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 5f 69    const sqlite_i
37eb1 6e 74 36 34 20 69 4c 65 61 76 65 73 45 6e 64 20  nt64 iLeavesEnd 
37eb2 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
37eb3 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a 20 20  _int64(s, 1);.  
37eb4 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
37eb5 61 72 20 2a 70 44 61 74 61 20 3d 20 73 71 6c 69  ar *pData = sqli
37eb6 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
37eb7 73 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 20  s, 2);.         
37eb8 20 63 6f 6e 73 74 20 69 6e 74 20 6e 44 61 74 61   const int nData
37eb9 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
37eba 6e 5f 62 79 74 65 73 28 73 2c 20 32 29 3b 0a 0a  n_bytes(s, 2);..
37ebb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6c 6f 61            /* loa
37ebc 64 53 65 67 6d 65 6e 74 28 29 20 69 73 20 75 73  dSegment() is us
37ebd 65 64 20 62 79 20 74 65 72 6d 53 65 6c 65 63 74  ed by termSelect
37ebe 28 29 20 74 6f 20 6c 6f 61 64 20 65 61 63 68 0a  () to load each.
37ebf 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 65 67            ** seg
37ec0 6d 65 6e 74 27 73 20 64 61 74 61 2e 0a 20 20 20  ment's data..   
37ec1 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
37ec2 20 20 20 20 72 63 20 3d 20 6c 6f 61 64 53 65 67      rc = loadSeg
37ec3 6d 65 6e 74 28 76 2c 20 70 44 61 74 61 2c 20 6e  ment(v, pData, n
37ec4 44 61 74 61 2c 20 69 4c 65 61 76 65 73 45 6e 64  Data, iLeavesEnd
37ec5 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20  , pTerm, nTerm, 
37ec6 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
37ec7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 64                &d
37ec8 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20 20 20 20  oclist);.       
37ec9 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
37eca 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
37ecb 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
37ecc 5f 73 74 65 70 28 73 29 3b 0a 0a 20 20 20 20 20  _step(s);..     
37ecd 20 20 20 20 20 20 20 2f 2a 20 53 68 6f 75 6c 64         /* Should
37ece 20 6e 6f 74 20 68 61 76 65 20 6d 6f 72 65 20 74   not have more t
37ecf 68 61 6e 20 6f 6e 65 20 6d 61 74 63 68 69 6e 67  han one matching
37ed0 20 73 65 67 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20   segment. */.   
37ed1 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21           if( rc!
37ed2 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a  =SQLITE_DONE ){.
37ed3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
37ed4 6c 69 74 65 33 5f 72 65 73 65 74 28 73 29 3b 0a  lite3_reset(s);.
37ed5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64 61                da
37ed6 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
37ed7 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20 20  &doclist);.     
37ed8 20 20 20 20 20 20 20 20 20 67 65 6e 65 72 61 74           generat
37ed9 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c  eError(pContext,
37eda 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c   "dump_doclist",
37edb 20 22 69 6e 76 61 6c 69 64 20 73 65 67 64 69 72   "invalid segdir
37edc 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ");.            
37edd 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
37ede 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
37edf 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
37ee0 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  OK;.          }.
37ee1 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
37ee2 7d 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  }..      sqlite3
37ee3 5f 72 65 73 65 74 28 73 29 3b 0a 20 20 20 20 7d  _reset(s);.    }
37ee4 0a 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ..    if( rc==SQ
37ee5 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
37ee6 20 69 66 28 20 64 6f 63 6c 69 73 74 2e 6e 44 61   if( doclist.nDa
37ee7 74 61 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ta>0 ){.        
37ee8 63 72 65 61 74 65 44 6f 63 6c 69 73 74 52 65 73  createDoclistRes
37ee9 75 6c 74 28 70 43 6f 6e 74 65 78 74 2c 20 64 6f  ult(pContext, do
37eea 63 6c 69 73 74 2e 70 44 61 74 61 2c 20 64 6f 63  clist.pData, doc
37eeb 6c 69 73 74 2e 6e 44 61 74 61 29 3b 0a 20 20 20  list.nData);.   
37eec 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
37eed 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
37eee 3a 20 54 68 69 73 20 63 61 6e 20 68 61 70 70 65  : This can happe
37eef 6e 20 69 66 20 74 68 65 20 74 65 72 6d 20 69 73  n if the term is
37ef0 20 6e 6f 74 20 70 72 65 73 65 6e 74 2c 20 6f 72   not present, or
37ef1 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 66 20 61  .        ** if a
37ef2 6c 6c 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20  ll instances of 
37ef3 74 68 65 20 74 65 72 6d 20 68 61 76 65 20 62 65  the term have be
37ef4 65 6e 20 64 65 6c 65 74 65 64 20 61 6e 64 20 74  en deleted and t
37ef5 68 69 73 20 69 73 0a 20 20 20 20 20 20 20 20 2a  his is.        *
37ef6 2a 20 61 6e 20 61 6c 6c 2d 69 6e 64 65 78 20 64  * an all-index d
37ef7 75 6d 70 2e 20 20 49 74 20 6d 61 79 20 62 65 20  ump.  It may be 
37ef8 69 6e 74 65 72 65 73 74 69 6e 67 20 74 6f 20 64  interesting to d
37ef9 69 73 74 69 6e 67 75 69 73 68 0a 20 20 20 20 20  istinguish.     
37efa 20 20 20 2a 2a 20 74 68 65 73 65 20 63 61 73 65     ** these case
37efb 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  s..        */.  
37efc 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
37efd 73 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74 65  sult_text(pConte
37efe 78 74 2c 20 22 22 2c 20 30 2c 20 53 51 4c 49 54  xt, "", 0, SQLIT
37eff 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
37f00 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
37f01 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
37f02 4d 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 48 61  M ){.      /* Ha
37f03 6e 64 6c 65 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f  ndle out-of-memo
37f04 72 79 20 63 61 73 65 73 20 73 70 65 63 69 61 6c  ry cases special
37f05 6c 79 20 62 65 63 61 75 73 65 20 69 66 20 74 68  ly because if th
37f06 65 79 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20  ey are.      ** 
37f07 67 65 6e 65 72 61 74 65 64 20 69 6e 20 66 74 73  generated in fts
37f08 33 20 63 6f 64 65 20 74 68 65 79 20 6d 61 79 20  3 code they may 
37f09 6e 6f 74 20 62 65 20 72 65 66 6c 65 63 74 65 64  not be reflected
37f0a 20 69 6e 20 74 68 65 20 64 62 0a 20 20 20 20 20   in the db.     
37f0b 20 2a 2a 20 68 61 6e 64 6c 65 2e 0a 20 20 20 20   ** handle..    
37f0c 20 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 54 4f    */.      /* TO
37f0d 44 4f 28 73 68 65 73 73 29 3a 20 48 61 6e 64 6c  DO(shess): Handl
37f0e 65 20 74 68 69 73 20 6d 6f 72 65 20 63 6f 6d 70  e this more comp
37f0f 72 65 68 65 6e 73 69 76 65 6c 79 2e 0a 20 20 20  rehensively..   
37f10 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 45 72 72     ** sqlite3Err
37f11 53 74 72 28 29 20 68 61 73 20 77 68 61 74 20 49  Str() has what I
37f12 20 6e 65 65 64 2c 20 62 75 74 20 69 73 20 69 6e   need, but is in
37f13 74 65 72 6e 61 6c 2e 0a 20 20 20 20 20 20 2a 2f  ternal..      */
37f14 0a 20 20 20 20 20 20 67 65 6e 65 72 61 74 65 45  .      generateE
37f15 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22  rror(pContext, "
37f16 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20 22  dump_doclist", "
37f17 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b  out of memory");
37f18 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
37f19 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28    generateError(
37f1a 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f  pContext, "dump_
37f1b 64 6f 63 6c 69 73 74 22 2c 20 4e 55 4c 4c 29 3b  doclist", NULL);
37f1c 0a 20 20 20 20 7d 0a 0a 20 20 20 20 64 61 74 61  .    }..    data
37f1d 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 64  BufferDestroy(&d
37f1e 6f 63 6c 69 73 74 29 3b 0a 20 20 7d 0a 7d 0a 23  oclist);.  }.}.#
37f1f 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  endif../*.** Thi
37f20 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d  s routine implem
37f21 65 6e 74 73 20 74 68 65 20 78 46 69 6e 64 46 75  ents the xFindFu
37f22 6e 63 74 69 6f 6e 20 6d 65 74 68 6f 64 20 66 6f  nction method fo
37f23 72 20 74 68 65 20 46 54 53 33 0a 2a 2a 20 76 69  r the FTS3.** vi
37f24 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a  rtual table..*/.
37f25 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74  static int fullt
37f26 65 78 74 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28  extFindFunction(
37f27 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
37f28 2a 70 56 74 61 62 2c 0a 20 20 69 6e 74 20 6e 41  *pVtab,.  int nA
37f29 72 67 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  rg,.  const char
37f2a 20 2a 7a 4e 61 6d 65 2c 0a 20 20 76 6f 69 64 20   *zName,.  void 
37f2b 28 2a 2a 70 78 46 75 6e 63 29 28 73 71 6c 69 74  (**pxFunc)(sqlit
37f2c 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
37f2d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
37f2e 2c 0a 20 20 76 6f 69 64 20 2a 2a 70 70 41 72 67  ,.  void **ppArg
37f2f 0a 29 7b 0a 20 20 69 66 28 20 73 74 72 63 6d 70  .){.  if( strcmp
37f30 28 7a 4e 61 6d 65 2c 22 73 6e 69 70 70 65 74 22  (zName,"snippet"
37f31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 78 46  )==0 ){.    *pxF
37f32 75 6e 63 20 3d 20 73 6e 69 70 70 65 74 46 75 6e  unc = snippetFun
37f33 63 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  c;.    return 1;
37f34 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72  .  }else if( str
37f35 63 6d 70 28 7a 4e 61 6d 65 2c 22 6f 66 66 73 65  cmp(zName,"offse
37f36 74 73 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a  ts")==0 ){.    *
37f37 70 78 46 75 6e 63 20 3d 20 73 6e 69 70 70 65 74  pxFunc = snippet
37f38 4f 66 66 73 65 74 73 46 75 6e 63 3b 0a 20 20 20  OffsetsFunc;.   
37f39 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c   return 1;.  }el
37f3a 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a 4e  se if( strcmp(zN
37f3b 61 6d 65 2c 22 6f 70 74 69 6d 69 7a 65 22 29 3d  ame,"optimize")=
37f3c 3d 30 20 29 7b 0a 20 20 20 20 2a 70 78 46 75 6e  =0 ){.    *pxFun
37f3d 63 20 3d 20 6f 70 74 69 6d 69 7a 65 46 75 6e 63  c = optimizeFunc
37f3e 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
37f3f 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
37f40 53 54 0a 20 20 20 20 2f 2a 20 4e 4f 54 45 28 73  ST.    /* NOTE(s
37f41 68 65 73 73 29 3a 20 54 68 65 73 65 20 66 75 6e  hess): These fun
37f42 63 74 69 6f 6e 73 20 61 72 65 20 70 72 65 73 65  ctions are prese
37f43 6e 74 20 6f 6e 6c 79 20 66 6f 72 20 74 65 73 74  nt only for test
37f44 69 6e 67 0a 20 20 20 20 2a 2a 20 70 75 72 70 6f  ing.    ** purpo
37f45 73 65 73 2e 20 20 4e 6f 20 70 61 72 74 69 63 75  ses.  No particu
37f46 6c 61 72 20 65 66 66 6f 72 74 20 69 73 20 6d 61  lar effort is ma
37f47 64 65 20 74 6f 20 6f 70 74 69 6d 69 7a 65 20 74  de to optimize t
37f48 68 65 69 72 0a 20 20 20 20 2a 2a 20 65 78 65 63  heir.    ** exec
37f49 75 74 69 6f 6e 20 6f 72 20 68 6f 77 20 74 68 65  ution or how the
37f4a 79 20 62 75 69 6c 64 20 74 68 65 69 72 20 72 65  y build their re
37f4b 73 75 6c 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  sults..    */.  
37f4c 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70  }else if( strcmp
37f4d 28 7a 4e 61 6d 65 2c 22 64 75 6d 70 5f 74 65 72  (zName,"dump_ter
37f4e 6d 73 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  ms")==0 ){.    /
37f4f 2a 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72  * fprintf(stderr
37f50 2c 20 22 46 6f 75 6e 64 20 64 75 6d 70 5f 74 65  , "Found dump_te
37f51 72 6d 73 5c 6e 22 29 3b 20 2a 2f 0a 20 20 20 20  rms\n"); */.    
37f52 2a 70 78 46 75 6e 63 20 3d 20 64 75 6d 70 54 65  *pxFunc = dumpTe
37f53 72 6d 73 46 75 6e 63 3b 0a 20 20 20 20 72 65 74  rmsFunc;.    ret
37f54 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69  urn 1;.  }else i
37f55 66 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c  f( strcmp(zName,
37f56 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 29 3d  "dump_doclist")=
37f57 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 66 70 72  =0 ){.    /* fpr
37f58 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 46 6f  intf(stderr, "Fo
37f59 75 6e 64 20 64 75 6d 70 5f 64 6f 63 6c 69 73 74  und dump_doclist
37f5a 5c 6e 22 29 3b 20 2a 2f 0a 20 20 20 20 2a 70 78  \n"); */.    *px
37f5b 46 75 6e 63 20 3d 20 64 75 6d 70 44 6f 63 6c 69  Func = dumpDocli
37f5c 73 74 46 75 6e 63 3b 0a 20 20 20 20 72 65 74 75  stFunc;.    retu
37f5d 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  rn 1;.#endif.  }
37f5e 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
37f5f 2f 2a 0a 2a 2a 20 52 65 6e 61 6d 65 20 61 6e 20  /*.** Rename an 
37f60 66 74 73 33 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73  fts3 table..*/.s
37f61 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65  tatic int fullte
37f62 78 74 52 65 6e 61 6d 65 28 0a 20 20 73 71 6c 69  xtRename(.  sqli
37f63 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c  te3_vtab *pVtab,
37f64 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
37f65 4e 61 6d 65 0a 29 7b 0a 20 20 66 75 6c 6c 74 65  Name.){.  fullte
37f66 78 74 5f 76 74 61 62 20 2a 70 20 3d 20 28 66 75  xt_vtab *p = (fu
37f67 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 70 56  lltext_vtab *)pV
37f68 74 61 62 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  tab;.  int rc = 
37f69 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
37f6a 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71 6c  char *zSql = sql
37f6b 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20  ite3_mprintf(.  
37f6c 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25    "ALTER TABLE %
37f6d 51 2e 27 25 71 5f 63 6f 6e 74 65 6e 74 27 20 20  Q.'%q_content'  
37f6e 52 45 4e 41 4d 45 20 54 4f 20 27 25 71 5f 63 6f  RENAME TO '%q_co
37f6f 6e 74 65 6e 74 27 3b 22 0a 20 20 20 20 22 41 4c  ntent';".    "AL
37f70 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27 25 71  TER TABLE %Q.'%q
37f71 5f 73 65 67 6d 65 6e 74 73 27 20 52 45 4e 41 4d  _segments' RENAM
37f72 45 20 54 4f 20 27 25 71 5f 73 65 67 6d 65 6e 74  E TO '%q_segment
37f73 73 27 3b 22 0a 20 20 20 20 22 41 4c 54 45 52 20  s';".    "ALTER 
37f74 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 73 65 67  TABLE %Q.'%q_seg
37f75 64 69 72 27 20 20 20 52 45 4e 41 4d 45 20 54 4f  dir'   RENAME TO
37f76 20 27 25 71 5f 73 65 67 64 69 72 27 3b 22 0a 20   '%q_segdir';". 
37f77 20 20 20 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e     , p->zDb, p->
37f78 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 20 0a 20 20  zName, zName .  
37f79 20 20 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a    , p->zDb, p->z
37f7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 20 0a 20 20 20  Name, zName .   
37f7b 20 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e   , p->zDb, p->zN
37f7c 61 6d 65 2c 20 7a 4e 61 6d 65 0a 20 20 29 3b 0a  ame, zName.  );.
37f7d 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20    if( zSql ){.  
37f7e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65    rc = sqlite3_e
37f7f 78 65 63 28 70 2d 3e 64 62 2c 20 7a 53 71 6c 2c  xec(p->db, zSql,
37f80 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73   0, 0, 0);.    s
37f81 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71 6c  qlite3_free(zSql
37f82 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
37f83 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 63 6f  rc;.}..static co
37f84 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
37f85 6c 65 20 66 74 73 33 4d 6f 64 75 6c 65 20 3d 20  le fts3Module = 
37f86 7b 0a 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20  {.  /* iVersion 
37f87 20 20 20 20 20 2a 2f 20 30 2c 0a 20 20 2f 2a 20       */ 0,.  /* 
37f88 78 43 72 65 61 74 65 20 20 20 20 20 20 20 2a 2f  xCreate       */
37f89 20 66 75 6c 6c 74 65 78 74 43 72 65 61 74 65 2c   fulltextCreate,
37f8a 0a 20 20 2f 2a 20 78 43 6f 6e 6e 65 63 74 20 20  .  /* xConnect  
37f8b 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 43      */ fulltextC
37f8c 6f 6e 6e 65 63 74 2c 0a 20 20 2f 2a 20 78 42 65  onnect,.  /* xBe
37f8d 73 74 49 6e 64 65 78 20 20 20 20 2a 2f 20 66 75  stIndex    */ fu
37f8e 6c 6c 74 65 78 74 42 65 73 74 49 6e 64 65 78 2c  lltextBestIndex,
37f8f 0a 20 20 2f 2a 20 78 44 69 73 63 6f 6e 6e 65 63  .  /* xDisconnec
37f90 74 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 44  t   */ fulltextD
37f91 69 73 63 6f 6e 6e 65 63 74 2c 0a 20 20 2f 2a 20  isconnect,.  /* 
37f92 78 44 65 73 74 72 6f 79 20 20 20 20 20 20 2a 2f  xDestroy      */
37f93 20 66 75 6c 6c 74 65 78 74 44 65 73 74 72 6f 79   fulltextDestroy
37f94 2c 0a 20 20 2f 2a 20 78 4f 70 65 6e 20 20 20 20  ,.  /* xOpen    
37f95 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74       */ fulltext
37f96 4f 70 65 6e 2c 0a 20 20 2f 2a 20 78 43 6c 6f 73  Open,.  /* xClos
37f97 65 20 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c  e        */ full
37f98 74 65 78 74 43 6c 6f 73 65 2c 0a 20 20 2f 2a 20  textClose,.  /* 
37f99 78 46 69 6c 74 65 72 20 20 20 20 20 20 20 2a 2f  xFilter       */
37f9a 20 66 75 6c 6c 74 65 78 74 46 69 6c 74 65 72 2c   fulltextFilter,
37f9b 0a 20 20 2f 2a 20 78 4e 65 78 74 20 20 20 20 20  .  /* xNext     
37f9c 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 4e      */ fulltextN
37f9d 65 78 74 2c 0a 20 20 2f 2a 20 78 45 6f 66 20 20  ext,.  /* xEof  
37f9e 20 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74          */ fullt
37f9f 65 78 74 45 6f 66 2c 0a 20 20 2f 2a 20 78 43 6f  extEof,.  /* xCo
37fa0 6c 75 6d 6e 20 20 20 20 20 20 20 2a 2f 20 66 75  lumn       */ fu
37fa1 6c 6c 74 65 78 74 43 6f 6c 75 6d 6e 2c 0a 20 20  lltextColumn,.  
37fa2 2f 2a 20 78 52 6f 77 69 64 20 20 20 20 20 20 20  /* xRowid       
37fa3 20 2a 2f 20 66 75 6c 6c 74 65 78 74 52 6f 77 69   */ fulltextRowi
37fa4 64 2c 0a 20 20 2f 2a 20 78 55 70 64 61 74 65 20  d,.  /* xUpdate 
37fa5 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78        */ fulltex
37fa6 74 55 70 64 61 74 65 2c 0a 20 20 2f 2a 20 78 42  tUpdate,.  /* xB
37fa7 65 67 69 6e 20 20 20 20 20 20 20 20 2a 2f 20 66  egin        */ f
37fa8 75 6c 6c 74 65 78 74 42 65 67 69 6e 2c 0a 20 20  ulltextBegin,.  
37fa9 2f 2a 20 78 53 79 6e 63 20 20 20 20 20 20 20 20  /* xSync        
37faa 20 2a 2f 20 66 75 6c 6c 74 65 78 74 53 79 6e 63   */ fulltextSync
37fab 2c 0a 20 20 2f 2a 20 78 43 6f 6d 6d 69 74 20 20  ,.  /* xCommit  
37fac 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74       */ fulltext
37fad 43 6f 6d 6d 69 74 2c 0a 20 20 2f 2a 20 78 52 6f  Commit,.  /* xRo
37fae 6c 6c 62 61 63 6b 20 20 20 20 20 2a 2f 20 66 75  llback     */ fu
37faf 6c 6c 74 65 78 74 52 6f 6c 6c 62 61 63 6b 2c 0a  lltextRollback,.
37fb0 20 20 2f 2a 20 78 46 69 6e 64 46 75 6e 63 74 69    /* xFindFuncti
37fb1 6f 6e 20 2a 2f 20 66 75 6c 6c 74 65 78 74 46 69  on */ fulltextFi
37fb2 6e 64 46 75 6e 63 74 69 6f 6e 2c 0a 20 20 2f 2a  ndFunction,.  /*
37fb3 20 78 52 65 6e 61 6d 65 20 2a 2f 20 20 20 20 20   xRename */     
37fb4 20 20 66 75 6c 6c 74 65 78 74 52 65 6e 61 6d 65    fulltextRename
37fb5 2c 0a 7d 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69  ,.};..static voi
37fb6 64 20 68 61 73 68 44 65 73 74 72 6f 79 28 76 6f  d hashDestroy(vo
37fb7 69 64 20 2a 70 29 7b 0a 20 20 66 74 73 33 48 61  id *p){.  fts3Ha
37fb8 73 68 20 2a 70 48 61 73 68 20 3d 20 28 66 74 73  sh *pHash = (fts
37fb9 33 48 61 73 68 20 2a 29 70 3b 0a 20 20 73 71 6c  3Hash *)p;.  sql
37fba 69 74 65 33 46 74 73 33 48 61 73 68 43 6c 65 61  ite3Fts3HashClea
37fbb 72 28 70 48 61 73 68 29 3b 0a 20 20 73 71 6c 69  r(pHash);.  sqli
37fbc 74 65 33 5f 66 72 65 65 28 70 48 61 73 68 29 3b  te3_free(pHash);
37fbd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 74  .}../*.** The ft
37fbe 73 33 20 62 75 69 6c 74 2d 69 6e 20 74 6f 6b 65  s3 built-in toke
37fbf 6e 69 7a 65 72 73 20 2d 20 22 73 69 6d 70 6c 65  nizers - "simple
37fc0 22 20 61 6e 64 20 22 70 6f 72 74 65 72 22 20 2d  " and "porter" -
37fc1 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64   are implemented
37fc2 0a 2a 2a 20 69 6e 20 66 69 6c 65 73 20 66 74 73  .** in files fts
37fc3 33 5f 74 6f 6b 65 6e 69 7a 65 72 31 2e 63 20 61  3_tokenizer1.c a
37fc4 6e 64 20 66 74 73 33 5f 70 6f 72 74 65 72 2e 63  nd fts3_porter.c
37fc5 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54   respectively. T
37fc6 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
37fc7 74 77 6f 20 66 6f 72 77 61 72 64 20 64 65 63 6c  two forward decl
37fc8 61 72 61 74 69 6f 6e 73 20 61 72 65 20 66 6f 72  arations are for
37fc9 20 66 75 6e 63 74 69 6f 6e 73 20 64 65 63 6c 61   functions decla
37fca 72 65 64 20 69 6e 20 74 68 65 73 65 20 66 69 6c  red in these fil
37fcb 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 72 65  es.** used to re
37fcc 74 72 69 65 76 65 20 74 68 65 20 72 65 73 70 65  trieve the respe
37fcd 63 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61  ctive implementa
37fce 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c  tions..**.** Cal
37fcf 6c 69 6e 67 20 73 71 6c 69 74 65 33 46 74 73 33  ling sqlite3Fts3
37fd0 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d  SimpleTokenizerM
37fd1 6f 64 75 6c 65 28 29 20 73 65 74 73 20 74 68 65  odule() sets the
37fd2 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64 0a 2a   value pointed.*
37fd3 2a 20 74 6f 20 62 79 20 74 68 65 20 61 72 67 75  * to by the argu
37fd4 6d 65 6e 74 20 74 6f 20 70 6f 69 6e 74 20 61 20  ment to point a 
37fd5 74 68 65 20 22 73 69 6d 70 6c 65 22 20 74 6f 6b  the "simple" tok
37fd6 65 6e 69 7a 65 72 20 69 6d 70 6c 65 6d 65 6e 74  enizer implement
37fd7 61 74 69 6f 6e 2e 0a 2a 2a 20 46 75 6e 63 74 69  ation..** Functi
37fd8 6f 6e 20 2e 2e 2e 50 6f 72 74 65 72 54 6f 6b 65  on ...PorterToke
37fd9 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 29 20 73 65  nizerModule() se
37fda 74 73 20 2a 70 4d 6f 64 75 6c 65 20 74 6f 20 70  ts *pModule to p
37fdb 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 70  oint to the.** p
37fdc 6f 72 74 65 72 20 74 6f 6b 65 6e 69 7a 65 72 2f  orter tokenizer/
37fdd 73 74 65 6d 6d 65 72 20 69 6d 70 6c 65 6d 65 6e  stemmer implemen
37fde 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  tation..*/.SQLIT
37fdf 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
37fe0 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65  qlite3Fts3Simple
37fe1 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28  TokenizerModule(
37fe2 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
37fe3 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a  r_module const**
37fe4 70 70 4d 6f 64 75 6c 65 29 3b 0a 53 51 4c 49 54  ppModule);.SQLIT
37fe5 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
37fe6 71 6c 69 74 65 33 46 74 73 33 50 6f 72 74 65 72  qlite3Fts3Porter
37fe7 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28  TokenizerModule(
37fe8 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
37fe9 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a  r_module const**
37fea 70 70 4d 6f 64 75 6c 65 29 3b 0a 53 51 4c 49 54  ppModule);.SQLIT
37feb 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
37fec 71 6c 69 74 65 33 46 74 73 33 49 63 75 54 6f 6b  qlite3Fts3IcuTok
37fed 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 73 71 6c  enizerModule(sql
37fee 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
37fef 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d  odule const**ppM
37ff0 6f 64 75 6c 65 29 3b 0a 0a 53 51 4c 49 54 45 5f  odule);..SQLITE_
37ff1 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
37ff2 74 65 33 46 74 73 33 49 6e 69 74 48 61 73 68 54  te3Fts3InitHashT
37ff3 61 62 6c 65 28 73 71 6c 69 74 65 33 20 2a 2c 20  able(sqlite3 *, 
37ff4 66 74 73 33 48 61 73 68 20 2a 2c 20 63 6f 6e 73  fts3Hash *, cons
37ff5 74 20 63 68 61 72 20 2a 29 3b 0a 0a 2f 2a 0a 2a  t char *);../*.*
37ff6 2a 20 49 6e 69 74 69 61 6c 69 73 65 20 74 68 65  * Initialise the
37ff7 20 66 74 73 33 20 65 78 74 65 6e 73 69 6f 6e 2e   fts3 extension.
37ff8 20 49 66 20 74 68 69 73 20 65 78 74 65 6e 73 69   If this extensi
37ff9 6f 6e 20 69 73 20 62 75 69 6c 74 20 61 73 20 70  on is built as p
37ffa 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71  art.** of the sq
37ffb 6c 69 74 65 20 6c 69 62 72 61 72 79 2c 20 74 68  lite library, th
37ffc 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
37ffd 20 69 73 20 63 61 6c 6c 65 64 20 64 69 72 65 63   is called direc
37ffe 74 6c 79 20 62 79 0a 2a 2a 20 53 51 4c 69 74 65  tly by.** SQLite
37fff 2e 20 49 66 20 66 74 73 33 20 69 73 20 62 75 69  . If fts3 is bui
38000 6c 74 20 61 73 20 61 20 64 79 6e 61 6d 69 63 61  lt as a dynamica
38001 6c 6c 79 20 6c 6f 61 64 61 62 6c 65 20 65 78 74  lly loadable ext
38002 65 6e 73 69 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20  ension, this.** 
38003 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
38004 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65  ed by the sqlite
38005 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74  3_extension_init
38006 28 29 20 65 6e 74 72 79 20 70 6f 69 6e 74 2e 0a  () entry point..
38007 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
38008 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
38009 33 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64  3Init(sqlite3 *d
3800a 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  b){.  int rc = S
3800b 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 66 74 73 33  QLITE_OK;.  fts3
3800c 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 30 3b  Hash *pHash = 0;
3800d 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
3800e 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
3800f 65 20 2a 70 53 69 6d 70 6c 65 20 3d 20 30 3b 0a  e *pSimple = 0;.
38010 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
38011 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
38012 20 2a 70 50 6f 72 74 65 72 20 3d 20 30 3b 0a 20   *pPorter = 0;. 
38013 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74   const sqlite3_t
38014 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
38015 2a 70 49 63 75 20 3d 20 30 3b 0a 0a 20 20 73 71  *pIcu = 0;..  sq
38016 6c 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65 54  lite3Fts3SimpleT
38017 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 26  okenizerModule(&
38018 70 53 69 6d 70 6c 65 29 3b 0a 20 20 73 71 6c 69  pSimple);.  sqli
38019 74 65 33 46 74 73 33 50 6f 72 74 65 72 54 6f 6b  te3Fts3PorterTok
3801a 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 26 70 50  enizerModule(&pP
3801b 6f 72 74 65 72 29 3b 0a 23 69 66 64 65 66 20 53  orter);.#ifdef S
3801c 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55  QLITE_ENABLE_ICU
3801d 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 49 63  .  sqlite3Fts3Ic
3801e 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  uTokenizerModule
3801f 28 26 70 49 63 75 29 3b 0a 23 65 6e 64 69 66 0a  (&pIcu);.#endif.
38020 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61  .  /* Allocate a
38021 6e 64 20 69 6e 69 74 69 61 6c 69 73 65 20 74 68  nd initialise th
38022 65 20 68 61 73 68 2d 74 61 62 6c 65 20 75 73 65  e hash-table use
38023 64 20 74 6f 20 73 74 6f 72 65 20 74 6f 6b 65 6e  d to store token
38024 69 7a 65 72 73 2e 20 2a 2f 0a 20 20 70 48 61 73  izers. */.  pHas
38025 68 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  h = sqlite3_mall
38026 6f 63 28 73 69 7a 65 6f 66 28 66 74 73 33 48 61  oc(sizeof(fts3Ha
38027 73 68 29 29 3b 0a 20 20 69 66 28 20 21 70 48 61  sh));.  if( !pHa
38028 73 68 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  sh ){.    rc = S
38029 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
3802a 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
3802b 33 46 74 73 33 48 61 73 68 49 6e 69 74 28 70 48  3Fts3HashInit(pH
3802c 61 73 68 2c 20 46 54 53 33 5f 48 41 53 48 5f 53  ash, FTS3_HASH_S
3802d 54 52 49 4e 47 2c 20 31 29 3b 0a 20 20 7d 0a 0a  TRING, 1);.  }..
3802e 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 62 75    /* Load the bu
3802f 69 6c 74 2d 69 6e 20 74 6f 6b 65 6e 69 7a 65 72  ilt-in tokenizer
38030 73 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20  s into the hash 
38031 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 72  table */.  if( r
38032 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
38033 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
38034 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 70 48  ts3HashInsert(pH
38035 61 73 68 2c 20 22 73 69 6d 70 6c 65 22 2c 20 37  ash, "simple", 7
38036 2c 20 28 76 6f 69 64 20 2a 29 70 53 69 6d 70 6c  , (void *)pSimpl
38037 65 29 0a 20 20 20 20 20 7c 7c 20 73 71 6c 69 74  e).     || sqlit
38038 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74  e3Fts3HashInsert
38039 28 70 48 61 73 68 2c 20 22 70 6f 72 74 65 72 22  (pHash, "porter"
3803a 2c 20 37 2c 20 28 76 6f 69 64 20 2a 29 70 50 6f  , 7, (void *)pPo
3803b 72 74 65 72 29 20 0a 20 20 20 20 20 7c 7c 20 28  rter) .     || (
3803c 70 49 63 75 20 26 26 20 73 71 6c 69 74 65 33 46  pIcu && sqlite3F
3803d 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 70 48  ts3HashInsert(pH
3803e 61 73 68 2c 20 22 69 63 75 22 2c 20 34 2c 20 28  ash, "icu", 4, (
3803f 76 6f 69 64 20 2a 29 70 49 63 75 29 29 0a 20 20  void *)pIcu)).  
38040 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20    ){.      rc = 
38041 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
38042 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20    }.  }..#ifdef 
38043 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73 71  SQLITE_TEST.  sq
38044 6c 69 74 65 33 46 74 73 33 45 78 70 72 49 6e 69  lite3Fts3ExprIni
38045 74 54 65 73 74 49 6e 74 65 72 66 61 63 65 28 64  tTestInterface(d
38046 62 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a  b);.#endif..  /*
38047 20 43 72 65 61 74 65 20 74 68 65 20 76 69 72 74   Create the virt
38048 75 61 6c 20 74 61 62 6c 65 20 77 72 61 70 70 65  ual table wrappe
38049 72 20 61 72 6f 75 6e 64 20 74 68 65 20 68 61 73  r around the has
3804a 68 2d 74 61 62 6c 65 20 61 6e 64 20 6f 76 65 72  h-table and over
3804b 6c 6f 61 64 20 0a 20 20 2a 2a 20 74 68 65 20 74  load .  ** the t
3804c 77 6f 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69  wo scalar functi
3804d 6f 6e 73 2e 20 49 66 20 74 68 69 73 20 69 73 20  ons. If this is 
3804e 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 67 69  successful, regi
3804f 73 74 65 72 20 74 68 65 0a 20 20 2a 2a 20 6d 6f  ster the.  ** mo
38050 64 75 6c 65 20 77 69 74 68 20 73 71 6c 69 74 65  dule with sqlite
38051 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c  ..  */.  if( SQL
38052 49 54 45 5f 4f 4b 3d 3d 72 63 20 0a 20 20 20 26  ITE_OK==rc .   &
38053 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63  & SQLITE_OK==(rc
38054 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e   = sqlite3Fts3In
38055 69 74 48 61 73 68 54 61 62 6c 65 28 64 62 2c 20  itHashTable(db, 
38056 70 48 61 73 68 2c 20 22 66 74 73 33 5f 74 6f 6b  pHash, "fts3_tok
38057 65 6e 69 7a 65 72 22 29 29 0a 20 20 20 26 26 20  enizer")).   && 
38058 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
38059 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61   sqlite3_overloa
3805a 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22  d_function(db, "
3805b 73 6e 69 70 70 65 74 22 2c 20 2d 31 29 29 0a 20  snippet", -1)). 
3805c 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d    && SQLITE_OK==
3805d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76  (rc = sqlite3_ov
3805e 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28  erload_function(
3805f 64 62 2c 20 22 6f 66 66 73 65 74 73 22 2c 20 2d  db, "offsets", -
38060 31 29 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45  1)).   && SQLITE
38061 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK==(rc = sqlit
38062 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63  e3_overload_func
38063 74 69 6f 6e 28 64 62 2c 20 22 6f 70 74 69 6d 69  tion(db, "optimi
38064 7a 65 22 2c 20 2d 31 29 29 0a 23 69 66 64 65 66  ze", -1)).#ifdef
38065 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20   SQLITE_TEST.   
38066 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72  && SQLITE_OK==(r
38067 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65 72  c = sqlite3_over
38068 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62  load_function(db
38069 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20  , "dump_terms", 
3806a 2d 31 29 29 0a 20 20 20 26 26 20 53 51 4c 49 54  -1)).   && SQLIT
3806b 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK==(rc = sqli
3806c 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e  te3_overload_fun
3806d 63 74 69 6f 6e 28 64 62 2c 20 22 64 75 6d 70 5f  ction(db, "dump_
3806e 64 6f 63 6c 69 73 74 22 2c 20 2d 31 29 29 0a 23  doclist", -1)).#
3806f 65 6e 64 69 66 0a 20 20 29 7b 0a 20 20 20 20 72  endif.  ){.    r
38070 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 63 72  eturn sqlite3_cr
38071 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 0a  eate_module_v2(.
38072 20 20 20 20 20 20 20 20 64 62 2c 20 22 66 74 73          db, "fts
38073 33 22 2c 20 26 66 74 73 33 4d 6f 64 75 6c 65 2c  3", &fts3Module,
38074 20 28 76 6f 69 64 20 2a 29 70 48 61 73 68 2c 20   (void *)pHash, 
38075 68 61 73 68 44 65 73 74 72 6f 79 0a 20 20 20 20  hashDestroy.    
38076 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6e 20  );.  }..  /* An 
38077 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72  error has occurr
38078 65 64 2e 20 44 65 6c 65 74 65 20 74 68 65 20 68  ed. Delete the h
38079 61 73 68 20 74 61 62 6c 65 20 61 6e 64 20 72 65  ash table and re
3807a 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63  turn the error c
3807b 6f 64 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  ode. */.  assert
3807c 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
3807d 29 3b 0a 20 20 69 66 28 20 70 48 61 73 68 20 29  );.  if( pHash )
3807e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  {.    sqlite3Fts
3807f 33 48 61 73 68 43 6c 65 61 72 28 70 48 61 73 68  3HashClear(pHash
38080 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
38081 72 65 65 28 70 48 61 73 68 29 3b 0a 20 20 7d 0a  ree(pHash);.  }.
38082 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
38083 23 69 66 20 21 53 51 4c 49 54 45 5f 43 4f 52 45  #if !SQLITE_CORE
38084 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
38085 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f  sqlite3_extensio
38086 6e 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74 65  n_init(.  sqlite
38087 33 20 2a 64 62 2c 20 0a 20 20 63 68 61 72 20 2a  3 *db, .  char *
38088 2a 70 7a 45 72 72 4d 73 67 2c 0a 20 20 63 6f 6e  *pzErrMsg,.  con
38089 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  st sqlite3_api_r
3808a 6f 75 74 69 6e 65 73 20 2a 70 41 70 69 0a 29 7b  outines *pApi.){
3808b 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53  .  SQLITE_EXTENS
3808c 49 4f 4e 5f 49 4e 49 54 32 28 70 41 70 69 29 0a  ION_INIT2(pApi).
3808d 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
3808e 46 74 73 33 49 6e 69 74 28 64 62 29 3b 0a 7d 0a  Fts3Init(db);.}.
3808f 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f  #endif..#endif /
38090 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
38091 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
38092 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
38093 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _FTS3) */../****
38094 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
38095 66 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f fts3.c *******
38096 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38097 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38098 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
38099 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
3809a 20 66 69 6c 65 20 66 74 73 33 5f 65 78 70 72 2e   file fts3_expr.
3809b 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
3809c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3809d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
3809e 20 32 30 30 38 20 4e 6f 76 20 32 38 0a 2a 2a 0a   2008 Nov 28.**.
3809f 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
380a0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
380a1 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
380a2 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
380a3 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
380a4 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
380a5 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
380a6 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
380a7 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
380a8 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
380a9 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
380aa 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
380ab 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
380ac 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
380ad 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
380ae 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
380af 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
380b0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
380b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
380b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
380b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
380b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
380b5 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75  .**.** This modu
380b6 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
380b7 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
380b8 20 61 20 70 61 72 73 65 72 20 66 6f 72 20 66 74   a parser for ft
380b9 73 33 20 71 75 65 72 79 20 73 74 72 69 6e 67 73  s3 query strings
380ba 0a 2a 2a 20 28 74 68 65 20 72 69 67 68 74 2d 68  .** (the right-h
380bb 61 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  and argument to 
380bc 74 68 65 20 4d 41 54 43 48 20 6f 70 65 72 61 74  the MATCH operat
380bd 6f 72 29 2e 20 42 65 63 61 75 73 65 20 74 68 65  or). Because the
380be 20 73 75 70 70 6f 72 74 65 64 20 0a 2a 2a 20 73   supported .** s
380bf 79 6e 74 61 78 20 69 73 20 72 65 6c 61 74 69 76  yntax is relativ
380c0 65 6c 79 20 73 69 6d 70 6c 65 2c 20 74 68 65 20  ely simple, the 
380c1 77 68 6f 6c 65 20 74 6f 6b 65 6e 69 7a 65 72 2f  whole tokenizer/
380c2 70 61 72 73 65 72 20 73 79 73 74 65 6d 20 69 73  parser system is
380c3 0a 2a 2a 20 68 61 6e 64 2d 63 6f 64 65 64 2e 20  .** hand-coded. 
380c4 54 68 65 20 70 75 62 6c 69 63 20 69 6e 74 65 72  The public inter
380c5 66 61 63 65 20 74 6f 20 74 68 69 73 20 6d 6f 64  face to this mod
380c6 75 6c 65 20 69 73 20 64 65 63 6c 61 72 65 64 20  ule is declared 
380c7 69 6e 20 73 6f 75 72 63 65 0a 2a 2a 20 63 6f 64  in source.** cod
380c8 65 20 66 69 6c 65 20 22 66 74 73 33 5f 65 78 70  e file "fts3_exp
380c9 72 2e 68 22 2e 0a 2a 2f 0a 23 69 66 20 21 64 65  r.h"..*/.#if !de
380ca 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
380cb 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
380cc 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
380cd 29 0a 0a 2f 2a 0a 2a 2a 20 42 79 20 64 65 66 61  )../*.** By defa
380ce 75 6c 74 2c 20 74 68 69 73 20 6d 6f 64 75 6c 65  ult, this module
380cf 20 70 61 72 73 65 73 20 74 68 65 20 6c 65 67 61   parses the lega
380d0 63 79 20 73 79 6e 74 61 78 20 74 68 61 74 20 68  cy syntax that h
380d1 61 73 20 62 65 65 6e 20 0a 2a 2a 20 74 72 61 64  as been .** trad
380d2 69 74 69 6f 6e 61 6c 6c 79 20 75 73 65 64 20 62  itionally used b
380d3 79 20 66 74 73 33 2e 20 4f 72 2c 20 69 66 20 53  y fts3. Or, if S
380d4 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
380d5 33 5f 50 41 52 45 4e 54 48 45 53 49 53 0a 2a 2a  3_PARENTHESIS.**
380d6 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65   is defined, the
380d7 6e 20 69 74 20 75 73 65 73 20 74 68 65 20 6e 65  n it uses the ne
380d8 77 20 73 79 6e 74 61 78 2e 20 54 68 65 20 64 69  w syntax. The di
380d9 66 66 65 72 65 6e 63 65 73 20 62 65 74 77 65 65  fferences betwee
380da 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20 61 6e 64  n.** the new and
380db 20 74 68 65 20 6f 6c 64 20 73 79 6e 74 61 78 65   the old syntaxe
380dc 73 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 61 29  s are:.**.**  a)
380dd 20 54 68 65 20 6e 65 77 20 73 79 6e 74 61 78 20   The new syntax 
380de 73 75 70 70 6f 72 74 73 20 70 61 72 65 6e 74 68  supports parenth
380df 65 73 69 73 2e 20 54 68 65 20 6f 6c 64 20 64 6f  esis. The old do
380e0 65 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 20 62  es not..**.**  b
380e1 29 20 54 68 65 20 6e 65 77 20 73 79 6e 74 61 78  ) The new syntax
380e2 20 73 75 70 70 6f 72 74 73 20 74 68 65 20 41 4e   supports the AN
380e3 44 20 61 6e 64 20 4e 4f 54 20 6f 70 65 72 61 74  D and NOT operat
380e4 6f 72 73 2e 20 54 68 65 20 6f 6c 64 20 64 6f 65  ors. The old doe
380e5 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 20 63 29  s not..**.**  c)
380e6 20 54 68 65 20 6f 6c 64 20 73 79 6e 74 61 78 20   The old syntax 
380e7 73 75 70 70 6f 72 74 73 20 74 68 65 20 22 2d 22  supports the "-"
380e8 20 74 6f 6b 65 6e 20 71 75 61 6c 69 66 69 65 72   token qualifier
380e9 2e 20 54 68 69 73 20 69 73 20 6e 6f 74 20 0a 2a  . This is not .*
380ea 2a 20 20 20 20 20 73 75 70 70 6f 72 74 65 64 20  *     supported 
380eb 62 79 20 74 68 65 20 6e 65 77 20 73 79 6e 74 61  by the new synta
380ec 78 20 28 69 74 20 69 73 20 72 65 70 6c 61 63 65  x (it is replace
380ed 64 20 62 79 20 74 68 65 20 4e 4f 54 20 6f 70 65  d by the NOT ope
380ee 72 61 74 6f 72 29 2e 0a 2a 2a 0a 2a 2a 20 20 64  rator)..**.**  d
380ef 29 20 57 68 65 6e 20 75 73 69 6e 67 20 74 68 65  ) When using the
380f0 20 6f 6c 64 20 73 79 6e 74 61 78 2c 20 74 68 65   old syntax, the
380f1 20 4f 52 20 6f 70 65 72 61 74 6f 72 20 68 61 73   OR operator has
380f2 20 61 20 67 72 65 61 74 65 72 20 70 72 65 63 65   a greater prece
380f3 64 65 6e 63 65 0a 2a 2a 20 20 20 20 20 74 68 61  dence.**     tha
380f4 6e 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 41 4e  n an implicit AN
380f5 44 2e 20 57 68 65 6e 20 75 73 69 6e 67 20 74 68  D. When using th
380f6 65 20 6e 65 77 2c 20 62 6f 74 68 20 69 6d 70 6c  e new, both impl
380f7 69 63 69 74 79 20 61 6e 64 20 65 78 70 6c 69 63  icity and explic
380f8 69 74 0a 2a 2a 20 20 20 20 20 41 4e 44 20 6f 70  it.**     AND op
380f9 65 72 61 74 6f 72 73 20 68 61 76 65 20 61 20 68  erators have a h
380fa 69 67 68 65 72 20 70 72 65 63 65 64 65 6e 63 65  igher precedence
380fb 20 74 68 61 6e 20 4f 52 2e 0a 2a 2a 0a 2a 2a 20   than OR..**.** 
380fc 49 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  If compiled with
380fd 20 53 51 4c 49 54 45 5f 54 45 53 54 20 64 65 66   SQLITE_TEST def
380fe 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 69 73 20  ined, then this 
380ff 6d 6f 64 75 6c 65 20 65 78 70 6f 72 74 73 20 74  module exports t
38100 68 65 0a 2a 2a 20 73 79 6d 62 6f 6c 20 22 69 6e  he.** symbol "in
38101 74 20 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65  t sqlite3_fts3_e
38102 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65  nable_parenthese
38103 73 22 2e 20 53 65 74 74 69 6e 67 20 74 68 69 73  s". Setting this
38104 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 74 6f 20   variable.** to 
38105 7a 65 72 6f 20 63 61 75 73 65 73 20 74 68 65 20  zero causes the 
38106 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65 20 74 68  module to use th
38107 65 20 6f 6c 64 20 73 79 6e 74 61 78 2e 20 49 66  e old syntax. If
38108 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 0a 2a   it is set to .*
38109 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 20 6e  * non-zero the n
3810a 65 77 20 73 79 6e 74 61 78 20 69 73 20 61 63 74  ew syntax is act
3810b 69 76 61 74 65 64 2e 20 54 68 69 73 20 69 73 20  ivated. This is 
3810c 73 6f 20 62 6f 74 68 20 73 79 6e 74 61 78 65 73  so both syntaxes
3810d 20 63 61 6e 0a 2a 2a 20 62 65 20 74 65 73 74 65   can.** be teste
3810e 64 20 75 73 69 6e 67 20 61 20 73 69 6e 67 6c 65  d using a single
3810f 20 62 75 69 6c 64 20 6f 66 20 74 65 73 74 66 69   build of testfi
38110 78 74 75 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66  xture..*/.#ifdef
38111 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
38112 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
38113 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f  te3_fts3_enable_
38114 70 61 72 65 6e 74 68 65 73 65 73 20 3d 20 30 3b  parentheses = 0;
38115 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 53  .#else.# ifdef S
38116 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
38117 33 5f 50 41 52 45 4e 54 48 45 53 49 53 20 0a 23  3_PARENTHESIS .#
38118 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
38119 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72  _fts3_enable_par
3811a 65 6e 74 68 65 73 65 73 20 31 0a 23 20 65 6c 73  entheses 1.# els
3811b 65 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69  e.#  define sqli
3811c 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f  te3_fts3_enable_
3811d 70 61 72 65 6e 74 68 65 73 65 73 20 30 0a 23 20  parentheses 0.# 
3811e 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a  endif.#endif../*
3811f 0a 2a 2a 20 44 65 66 61 75 6c 74 20 73 70 61 6e  .** Default span
38120 20 66 6f 72 20 4e 45 41 52 20 6f 70 65 72 61 74   for NEAR operat
38121 6f 72 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ors..*/.#define 
38122 53 51 4c 49 54 45 5f 46 54 53 33 5f 44 45 46 41  SQLITE_FTS3_DEFA
38123 55 4c 54 5f 4e 45 41 52 5f 50 41 52 41 4d 20 31  ULT_NEAR_PARAM 1
38124 30 0a 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75  0...typedef stru
38125 63 74 20 50 61 72 73 65 43 6f 6e 74 65 78 74 20  ct ParseContext 
38126 50 61 72 73 65 43 6f 6e 74 65 78 74 3b 0a 73 74  ParseContext;.st
38127 72 75 63 74 20 50 61 72 73 65 43 6f 6e 74 65 78  ruct ParseContex
38128 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  t {.  sqlite3_to
38129 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69  kenizer *pTokeni
3812a 7a 65 72 3b 20 20 20 20 20 20 2f 2a 20 54 6f 6b  zer;      /* Tok
3812b 65 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 20 2a 2f  enizer module */
3812c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
3812d 61 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20  azCol;          
3812e 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20         /* Array 
3812f 6f 66 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  of column names 
38130 66 6f 72 20 66 74 73 33 20 74 61 62 6c 65 20 2a  for fts3 table *
38131 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20  /.  int nCol;   
38132 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38133 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
38134 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
38135 61 7a 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74  azCol[] */.  int
38136 20 69 44 65 66 61 75 6c 74 43 6f 6c 3b 20 20 20   iDefaultCol;   
38137 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38138 20 2f 2a 20 44 65 66 61 75 6c 74 20 63 6f 6c 75   /* Default colu
38139 6d 6e 20 74 6f 20 71 75 65 72 79 20 2a 2f 0a 20  mn to query */. 
3813a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
3813b 20 2a 70 43 74 78 3b 20 20 20 20 20 20 20 20 20   *pCtx;         
3813c 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 65 72       /* Write er
3813d 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65 72 65  ror message here
3813e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 65 73 74 3b   */.  int nNest;
3813f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38140 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
38141 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 62 72  ber of nested br
38142 61 63 6b 65 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  ackets */.};../*
38143 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
38144 6e 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20  n is equivalent 
38145 74 6f 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  to the standard 
38146 69 73 73 70 61 63 65 28 29 20 66 75 6e 63 74 69  isspace() functi
38147 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  on. .**.** The s
38148 74 61 6e 64 61 72 64 20 69 73 73 70 61 63 65 28  tandard isspace(
38149 29 20 63 61 6e 20 62 65 20 61 77 6b 77 61 72 64  ) can be awkward
3814a 20 74 6f 20 75 73 65 20 73 61 66 65 6c 79 2c 20   to use safely, 
3814b 62 65 63 61 75 73 65 20 61 6c 74 68 6f 75 67 68  because although
3814c 20 69 74 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65   it.** is define
3814d 64 20 74 6f 20 61 63 63 65 70 74 20 61 6e 20 61  d to accept an a
3814e 72 67 75 6d 65 6e 74 20 6f 66 20 74 79 70 65 20  rgument of type 
3814f 69 6e 74 2c 20 69 74 73 20 62 65 68 61 76 69 6f  int, its behavio
38150 75 72 20 77 68 65 6e 20 70 61 73 73 65 64 0a 2a  ur when passed.*
38151 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61  * an integer tha
38152 74 20 66 61 6c 6c 73 20 6f 75 74 73 69 64 65 20  t falls outside 
38153 6f 66 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20  of the range of 
38154 74 68 65 20 75 6e 73 69 67 6e 65 64 20 63 68 61  the unsigned cha
38155 72 20 74 79 70 65 0a 2a 2a 20 69 73 20 75 6e 64  r type.** is und
38156 65 66 69 6e 65 64 20 28 61 6e 64 20 73 6f 6d 65  efined (and some
38157 74 69 6d 65 73 2c 20 22 75 6e 64 65 66 69 6e 65  times, "undefine
38158 64 22 20 6d 65 61 6e 73 20 73 65 67 66 61 75 6c  d" means segfaul
38159 74 29 2e 20 54 68 69 73 20 77 72 61 70 70 65 72  t). This wrapper
3815a 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 20 74  .** is defined t
3815b 6f 20 61 63 63 65 70 74 20 61 6e 20 61 72 67 75  o accept an argu
3815c 6d 65 6e 74 20 6f 66 20 74 79 70 65 20 63 68 61  ment of type cha
3815d 72 2c 20 61 6e 64 20 61 6c 77 61 79 73 20 72 65  r, and always re
3815e 74 75 72 6e 73 20 30 20 66 6f 72 0a 2a 2a 20 61  turns 0 for.** a
3815f 6e 79 20 76 61 6c 75 65 73 20 74 68 61 74 20 66  ny values that f
38160 61 6c 6c 20 6f 75 74 73 69 64 65 20 6f 66 20 74  all outside of t
38161 68 65 20 72 61 6e 67 65 20 6f 66 20 74 68 65 20  he range of the 
38162 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 79  unsigned char ty
38163 70 65 20 28 69 2e 65 2e 0a 2a 2a 20 6e 65 67 61  pe (i.e..** nega
38164 74 69 76 65 20 76 61 6c 75 65 73 29 2e 0a 2a 2f  tive values)..*/
38165 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
38166 69 73 73 70 61 63 65 28 63 68 61 72 20 63 29 7b  isspace(char c){
38167 0a 20 20 72 65 74 75 72 6e 20 28 63 26 30 78 38  .  return (c&0x8
38168 30 29 3d 3d 30 20 3f 20 69 73 73 70 61 63 65 28  0)==0 ? isspace(
38169 63 29 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  c) : 0;.}../*.**
3816a 20 45 78 74 72 61 63 74 20 74 68 65 20 6e 65 78   Extract the nex
3816b 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 62 75 66  t token from buf
3816c 66 65 72 20 7a 20 28 6c 65 6e 67 74 68 20 6e 29  fer z (length n)
3816d 20 75 73 69 6e 67 20 74 68 65 20 74 6f 6b 65 6e   using the token
3816e 69 7a 65 72 0a 2a 2a 20 61 6e 64 20 6f 74 68 65  izer.** and othe
3816f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 28 63  r information (c
38170 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 65 74 63 2e  olumn names etc.
38171 29 20 69 6e 20 70 50 61 72 73 65 2e 20 43 72 65  ) in pParse. Cre
38172 61 74 65 20 61 6e 20 46 74 73 33 45 78 70 72 0a  ate an Fts3Expr.
38173 2a 2a 20 73 74 72 75 63 74 75 72 65 20 6f 66 20  ** structure of 
38174 74 79 70 65 20 46 54 53 51 55 45 52 59 5f 50 48  type FTSQUERY_PH
38175 52 41 53 45 20 63 6f 6e 74 61 69 6e 69 6e 67 20  RASE containing 
38176 61 20 70 68 72 61 73 65 20 63 6f 6e 73 69 73 74  a phrase consist
38177 69 6e 67 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73  ing of this.** s
38178 69 6e 67 6c 65 20 74 6f 6b 65 6e 20 61 6e 64 20  ingle token and 
38179 73 65 74 20 2a 70 70 45 78 70 72 20 74 6f 20 70  set *ppExpr to p
3817a 6f 69 6e 74 20 74 6f 20 69 74 2e 20 49 66 20 74  oint to it. If t
3817b 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 62 75  he end of the bu
3817c 66 66 65 72 20 69 73 0a 2a 2a 20 72 65 61 63 68  ffer is.** reach
3817d 65 64 20 62 65 66 6f 72 65 20 61 20 74 6f 6b 65  ed before a toke
3817e 6e 20 69 73 20 66 6f 75 6e 64 2c 20 73 65 74 20  n is found, set 
3817f 2a 70 70 45 78 70 72 20 74 6f 20 7a 65 72 6f 2e  *ppExpr to zero.
38180 20 49 74 20 69 73 20 74 68 65 0a 2a 2a 20 72 65   It is the.** re
38181 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20  sponsibility of 
38182 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 65 76  the caller to ev
38183 65 6e 74 75 61 6c 6c 79 20 64 65 61 6c 6c 6f 63  entually dealloc
38184 61 74 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 65  ate the allocate
38185 64 20 0a 2a 2a 20 46 74 73 33 45 78 70 72 20 73  d .** Fts3Expr s
38186 74 72 75 63 74 75 72 65 20 28 69 66 20 61 6e 79  tructure (if any
38187 29 20 62 79 20 70 61 73 73 69 6e 67 20 69 74 20  ) by passing it 
38188 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  to sqlite3_free(
38189 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  )..**.** Return 
3818a 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63  SQLITE_OK if suc
3818b 63 65 73 73 66 75 6c 2c 20 6f 72 20 53 51 4c 49  cessful, or SQLI
3818c 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 65  TE_NOMEM if a me
3818d 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a  mory allocation.
3818e 2a 2a 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61  ** fails..*/.sta
3818f 74 69 63 20 69 6e 74 20 67 65 74 4e 65 78 74 54  tic int getNextT
38190 6f 6b 65 6e 28 0a 20 20 50 61 72 73 65 43 6f 6e  oken(.  ParseCon
38191 74 65 78 74 20 2a 70 50 61 72 73 65 2c 20 20 20  text *pParse,   
38192 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38193 2f 2a 20 66 74 73 33 20 71 75 65 72 79 20 70 61  /* fts3 query pa
38194 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  rse context */. 
38195 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20 20 20 20   int iCol,      
38196 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38197 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
38198 65 20 66 6f 72 20 46 74 73 33 50 68 72 61 73 65  e for Fts3Phrase
38199 2e 69 43 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 6f  .iColumn */.  co
3819a 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
3819b 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   n,             
3819c 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73        /* Input s
3819d 74 72 69 6e 67 20 2a 2f 0a 20 20 46 74 73 33 45  tring */.  Fts3E
3819e 78 70 72 20 2a 2a 70 70 45 78 70 72 2c 20 20 20  xpr **ppExpr,   
3819f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
381a0 20 20 20 2f 2a 20 4f 55 54 3a 20 65 78 70 72 65     /* OUT: expre
381a1 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a  ssion */.  int *
381a2 70 6e 43 6f 6e 73 75 6d 65 64 20 20 20 20 20 20  pnConsumed      
381a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
381a4 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65     /* OUT: Numbe
381a5 72 20 6f 66 20 62 79 74 65 73 20 63 6f 6e 73 75  r of bytes consu
381a6 6d 65 64 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  med */.){.  sqli
381a7 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
381a8 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 50 61 72  Tokenizer = pPar
381a9 73 65 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a  se->pTokenizer;.
381aa 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
381ab 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
381ac 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 70 54 6f 6b   *pModule = pTok
381ad 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b  enizer->pModule;
381ae 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c  .  int rc;.  sql
381af 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
381b0 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a  ursor *pCursor;.
381b1 20 20 46 74 73 33 45 78 70 72 20 2a 70 52 65 74    Fts3Expr *pRet
381b2 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 43 6f 6e   = 0;.  int nCon
381b3 73 75 6d 65 64 20 3d 20 30 3b 0a 0a 20 20 72 63  sumed = 0;..  rc
381b4 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65   = pModule->xOpe
381b5 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a 2c  n(pTokenizer, z,
381b6 20 6e 2c 20 26 70 43 75 72 73 6f 72 29 3b 0a 20   n, &pCursor);. 
381b7 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
381b8 4f 4b 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  OK ){.    const 
381b9 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 0a 20 20  char *zToken;.  
381ba 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 2c 20 69 53    int nToken, iS
381bb 74 61 72 74 2c 20 69 45 6e 64 2c 20 69 50 6f 73  tart, iEnd, iPos
381bc 69 74 69 6f 6e 3b 0a 20 20 20 20 69 6e 74 20 6e  ition;.    int n
381bd 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 20  Byte;           
381be 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
381bf 20 20 20 20 2f 2a 20 74 6f 74 61 6c 20 73 70 61      /* total spa
381c0 63 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a  ce to allocate *
381c1 2f 0a 0a 20 20 20 20 70 43 75 72 73 6f 72 2d 3e  /..    pCursor->
381c2 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f  pTokenizer = pTo
381c3 6b 65 6e 69 7a 65 72 3b 0a 20 20 20 20 72 63 20  kenizer;.    rc 
381c4 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74  = pModule->xNext
381c5 28 70 43 75 72 73 6f 72 2c 20 26 7a 54 6f 6b 65  (pCursor, &zToke
381c6 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 53 74  n, &nToken, &iSt
381c7 61 72 74 2c 20 26 69 45 6e 64 2c 20 26 69 50 6f  art, &iEnd, &iPo
381c8 73 69 74 69 6f 6e 29 3b 0a 0a 20 20 20 20 69 66  sition);..    if
381c9 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
381ca 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d  ){.      nByte =
381cb 20 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72   sizeof(Fts3Expr
381cc 29 20 2b 20 73 69 7a 65 6f 66 28 46 74 73 33 50  ) + sizeof(Fts3P
381cd 68 72 61 73 65 29 20 2b 20 6e 54 6f 6b 65 6e 3b  hrase) + nToken;
381ce 0a 20 20 20 20 20 20 70 52 65 74 20 3d 20 28 46  .      pRet = (F
381cf 74 73 33 45 78 70 72 20 2a 29 73 71 6c 69 74 65  ts3Expr *)sqlite
381d0 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b  3_malloc(nByte);
381d1 0a 20 20 20 20 20 20 69 66 28 20 21 70 52 65 74  .      if( !pRet
381d2 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
381d3 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
381d4 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
381d5 20 20 20 20 6d 65 6d 73 65 74 28 70 52 65 74 2c      memset(pRet,
381d6 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20   0, nByte);.    
381d7 20 20 20 20 70 52 65 74 2d 3e 65 54 79 70 65 20      pRet->eType 
381d8 3d 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  = FTSQUERY_PHRAS
381d9 45 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d  E;.        pRet-
381da 3e 70 50 68 72 61 73 65 20 3d 20 28 46 74 73 33  >pPhrase = (Fts3
381db 50 68 72 61 73 65 20 2a 29 26 70 52 65 74 5b 31  Phrase *)&pRet[1
381dc 5d 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d  ];.        pRet-
381dd 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e  >pPhrase->nToken
381de 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 52   = 1;.        pR
381df 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 69 43 6f  et->pPhrase->iCo
381e0 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 20  lumn = iCol;.   
381e1 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68 72 61       pRet->pPhra
381e2 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 6e 20  se->aToken[0].n 
381e3 3d 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20  = nToken;.      
381e4 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73 65 2d    pRet->pPhrase-
381e5 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 7a 20 3d 20 28  >aToken[0].z = (
381e6 63 68 61 72 20 2a 29 26 70 52 65 74 2d 3e 70 50  char *)&pRet->pP
381e7 68 72 61 73 65 5b 31 5d 3b 0a 20 20 20 20 20 20  hrase[1];.      
381e8 20 20 6d 65 6d 63 70 79 28 70 52 65 74 2d 3e 70    memcpy(pRet->p
381e9 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30  Phrase->aToken[0
381ea 5d 2e 7a 2c 20 7a 54 6f 6b 65 6e 2c 20 6e 54 6f  ].z, zToken, nTo
381eb 6b 65 6e 29 3b 0a 0a 20 20 20 20 20 20 20 20 69  ken);..        i
381ec 66 28 20 69 45 6e 64 3c 6e 20 26 26 20 7a 5b 69  f( iEnd<n && z[i
381ed 45 6e 64 5d 3d 3d 27 2a 27 20 29 7b 0a 20 20 20  End]=='*' ){.   
381ee 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68         pRet->pPh
381ef 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e  rase->aToken[0].
381f0 69 73 50 72 65 66 69 78 20 3d 20 31 3b 0a 20 20  isPrefix = 1;.  
381f1 20 20 20 20 20 20 20 20 69 45 6e 64 2b 2b 3b 0a          iEnd++;.
381f2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
381f3 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 66    if( !sqlite3_f
381f4 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e  ts3_enable_paren
381f5 74 68 65 73 65 73 20 26 26 20 69 53 74 61 72 74  theses && iStart
381f6 3e 30 20 26 26 20 7a 5b 69 53 74 61 72 74 2d 31  >0 && z[iStart-1
381f7 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20  ]=='-' ){.      
381f8 20 20 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73      pRet->pPhras
381f9 65 2d 3e 69 73 4e 6f 74 20 3d 20 31 3b 0a 20 20  e->isNot = 1;.  
381fa 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
381fb 20 20 20 20 20 20 6e 43 6f 6e 73 75 6d 65 64 20        nConsumed 
381fc 3d 20 69 45 6e 64 3b 0a 20 20 20 20 7d 0a 0a 20  = iEnd;.    }.. 
381fd 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f     pModule->xClo
381fe 73 65 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d  se(pCursor);.  }
381ff 0a 20 20 0a 20 20 2a 70 6e 43 6f 6e 73 75 6d 65  .  .  *pnConsume
38200 64 20 3d 20 6e 43 6f 6e 73 75 6d 65 64 3b 0a 20  d = nConsumed;. 
38201 20 2a 70 70 45 78 70 72 20 3d 20 70 52 65 74 3b   *ppExpr = pRet;
38202 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
38203 0a 0a 2f 2a 0a 2a 2a 20 45 6e 6c 61 72 67 65 20  ../*.** Enlarge 
38204 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
38205 69 6f 6e 2e 20 20 49 66 20 61 6e 20 6f 75 74 2d  ion.  If an out-
38206 6f 66 2d 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  of-memory alloca
38207 74 69 6f 6e 20 6f 63 63 75 72 73 2c 0a 2a 2a 20  tion occurs,.** 
38208 74 68 65 6e 20 66 72 65 65 20 74 68 65 20 6f 6c  then free the ol
38209 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f  d allocation..*/
3820a 0a 76 6f 69 64 20 2a 66 74 73 33 52 65 61 6c 6c  .void *fts3Reall
3820b 6f 63 4f 72 46 72 65 65 28 76 6f 69 64 20 2a 70  ocOrFree(void *p
3820c 4f 72 69 67 2c 20 69 6e 74 20 6e 4e 65 77 29 7b  Orig, int nNew){
3820d 0a 20 20 76 6f 69 64 20 2a 70 52 65 74 20 3d 20  .  void *pRet = 
3820e 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
3820f 70 4f 72 69 67 2c 20 6e 4e 65 77 29 3b 0a 20 20  pOrig, nNew);.  
38210 69 66 28 20 21 70 52 65 74 20 29 7b 0a 20 20 20  if( !pRet ){.   
38211 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4f   sqlite3_free(pO
38212 72 69 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rig);.  }.  retu
38213 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pRet;.}../*.*
38214 2a 20 42 75 66 66 65 72 20 7a 49 6e 70 75 74 2c  * Buffer zInput,
38215 20 6c 65 6e 67 74 68 20 6e 49 6e 70 75 74 2c 20   length nInput, 
38216 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e  contains the con
38217 74 65 6e 74 73 20 6f 66 20 61 20 71 75 6f 74 65  tents of a quote
38218 64 20 73 74 72 69 6e 67 0a 2a 2a 20 74 68 61 74  d string.** that
38219 20 61 70 70 65 61 72 65 64 20 61 73 20 70 61 72   appeared as par
3821a 74 20 6f 66 20 61 6e 20 66 74 73 33 20 71 75 65  t of an fts3 que
3821b 72 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e  ry expression. N
3821c 65 69 74 68 65 72 20 71 75 6f 74 65 20 63 68 61  either quote cha
3821d 72 61 63 74 65 72 0a 2a 2a 20 69 73 20 69 6e 63  racter.** is inc
3821e 6c 75 64 65 64 20 69 6e 20 74 68 65 20 62 75 66  luded in the buf
3821f 66 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69  fer. This functi
38220 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 74  on attempts to t
38221 6f 6b 65 6e 69 7a 65 20 74 68 65 20 65 6e 74 69  okenize the enti
38222 72 65 0a 2a 2a 20 69 6e 70 75 74 20 62 75 66 66  re.** input buff
38223 65 72 20 61 6e 64 20 63 72 65 61 74 65 20 61 6e  er and create an
38224 20 46 74 73 33 45 78 70 72 20 73 74 72 75 63 74   Fts3Expr struct
38225 75 72 65 20 6f 66 20 74 79 70 65 20 46 54 53 51  ure of type FTSQ
38226 55 45 52 59 5f 50 48 52 41 53 45 20 0a 2a 2a 20  UERY_PHRASE .** 
38227 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72  containing the r
38228 65 73 75 6c 74 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  esults..**.** If
38229 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c   successful, SQL
3822a 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
3822b 65 64 20 61 6e 64 20 2a 70 70 45 78 70 72 20 73  ed and *ppExpr s
3822c 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74  et to point at t
3822d 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  he.** allocated 
3822e 46 74 73 33 45 78 70 72 20 73 74 72 75 63 74 75  Fts3Expr structu
3822f 72 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 65  re. Otherwise, e
38230 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4e 4f 4d  ither SQLITE_NOM
38231 45 4d 20 28 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  EM (out of memor
38232 79 0a 2a 2a 20 65 72 72 6f 72 29 20 6f 72 20 53  y.** error) or S
38233 51 4c 49 54 45 5f 45 52 52 4f 52 20 28 74 6f 6b  QLITE_ERROR (tok
38234 65 6e 69 7a 61 74 69 6f 6e 20 65 72 72 6f 72 29  enization error)
38235 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
38236 20 2a 70 70 45 78 70 72 20 73 65 74 0a 2a 2a 20   *ppExpr set.** 
38237 74 6f 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  to 0..*/.static 
38238 69 6e 74 20 67 65 74 4e 65 78 74 53 74 72 69 6e  int getNextStrin
38239 67 28 0a 20 20 50 61 72 73 65 43 6f 6e 74 65 78  g(.  ParseContex
3823a 74 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  t *pParse,      
3823b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3823c 66 74 73 33 20 71 75 65 72 79 20 70 61 72 73 65  fts3 query parse
3823d 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f   context */.  co
3823e 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74  nst char *zInput
3823f 2c 20 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20 20  , int nInput,   
38240 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73        /* Input s
38241 74 72 69 6e 67 20 2a 2f 0a 20 20 46 74 73 33 45  tring */.  Fts3E
38242 78 70 72 20 2a 2a 70 70 45 78 70 72 20 20 20 20  xpr **ppExpr    
38243 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38244 20 20 20 2f 2a 20 4f 55 54 3a 20 65 78 70 72 65     /* OUT: expre
38245 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71  ssion */.){.  sq
38246 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
38247 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 50  *pTokenizer = pP
38248 61 72 73 65 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72  arse->pTokenizer
38249 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ;.  sqlite3_toke
3824a 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e  nizer_module con
3824b 73 74 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 70 54  st *pModule = pT
3824c 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c  okenizer->pModul
3824d 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 46  e;.  int rc;.  F
3824e 74 73 33 45 78 70 72 20 2a 70 20 3d 20 30 3b 0a  ts3Expr *p = 0;.
3824f 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
38250 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
38251 73 6f 72 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  sor = 0;.  char 
38252 2a 7a 54 65 6d 70 20 3d 20 30 3b 0a 20 20 69 6e  *zTemp = 0;.  in
38253 74 20 6e 54 65 6d 70 20 3d 20 30 3b 0a 0a 20 20  t nTemp = 0;..  
38254 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4f  rc = pModule->xO
38255 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20  pen(pTokenizer, 
38256 7a 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20  zInput, nInput, 
38257 26 70 43 75 72 73 6f 72 29 3b 0a 20 20 69 66 28  &pCursor);.  if(
38258 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
38259 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20  {.    int ii;.  
3825a 20 20 70 43 75 72 73 6f 72 2d 3e 70 54 6f 6b 65    pCursor->pToke
3825b 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a  nizer = pTokeniz
3825c 65 72 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30  er;.    for(ii=0
3825d 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b  ; rc==SQLITE_OK;
3825e 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 6f   ii++){.      co
3825f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e  nst char *zToken
38260 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 6b  ;.      int nTok
38261 65 6e 2c 20 69 42 65 67 69 6e 2c 20 69 45 6e 64  en, iBegin, iEnd
38262 2c 20 69 50 6f 73 3b 0a 20 20 20 20 20 20 72 63  , iPos;.      rc
38263 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78   = pModule->xNex
38264 74 28 70 43 75 72 73 6f 72 2c 20 26 7a 54 6f 6b  t(pCursor, &zTok
38265 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 42  en, &nToken, &iB
38266 65 67 69 6e 2c 20 26 69 45 6e 64 2c 20 26 69 50  egin, &iEnd, &iP
38267 6f 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  os);.      if( r
38268 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
38269 20 20 20 20 20 20 20 20 69 6e 74 20 6e 42 79 74          int nByt
3826a 65 20 3d 20 73 69 7a 65 6f 66 28 46 74 73 33 45  e = sizeof(Fts3E
3826b 78 70 72 29 20 2b 20 73 69 7a 65 6f 66 28 46 74  xpr) + sizeof(Ft
3826c 73 33 50 68 72 61 73 65 29 3b 0a 20 20 20 20 20  s3Phrase);.     
3826d 20 20 20 70 20 3d 20 66 74 73 33 52 65 61 6c 6c     p = fts3Reall
3826e 6f 63 4f 72 46 72 65 65 28 70 2c 20 6e 42 79 74  ocOrFree(p, nByt
3826f 65 2b 69 69 2a 73 69 7a 65 6f 66 28 73 74 72 75  e+ii*sizeof(stru
38270 63 74 20 50 68 72 61 73 65 54 6f 6b 65 6e 29 29  ct PhraseToken))
38271 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 6d 70 20  ;.        zTemp 
38272 3d 20 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46  = fts3ReallocOrF
38273 72 65 65 28 7a 54 65 6d 70 2c 20 6e 54 65 6d 70  ree(zTemp, nTemp
38274 20 2b 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20   + nToken);.    
38275 20 20 20 20 69 66 28 20 21 70 20 7c 7c 20 21 7a      if( !p || !z
38276 54 65 6d 70 20 29 7b 0a 20 20 20 20 20 20 20 20  Temp ){.        
38277 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
38278 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
38279 20 69 66 28 20 69 69 3d 3d 30 20 29 7b 0a 20 20   if( ii==0 ){.  
3827a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70          memset(p
3827b 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20  , 0, nByte);.   
3827c 20 20 20 20 20 20 20 70 2d 3e 70 50 68 72 61 73         p->pPhras
3827d 65 20 3d 20 28 46 74 73 33 50 68 72 61 73 65 20  e = (Fts3Phrase 
3827e 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20 20 20 20  *)&p[1];.       
3827f 20 7d 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 50   }.        p->pP
38280 68 72 61 73 65 20 3d 20 28 46 74 73 33 50 68 72  hrase = (Fts3Phr
38281 61 73 65 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20  ase *)&p[1];.   
38282 20 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65 2d       p->pPhrase-
38283 3e 6e 54 6f 6b 65 6e 20 3d 20 69 69 2b 31 3b 0a  >nToken = ii+1;.
38284 20 20 20 20 20 20 20 20 70 2d 3e 70 50 68 72 61          p->pPhra
38285 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 69 5d 2e 6e  se->aToken[ii].n
38286 20 3d 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20   = nToken;.     
38287 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70     memcpy(&zTemp
38288 5b 6e 54 65 6d 70 5d 2c 20 7a 54 6f 6b 65 6e 2c  [nTemp], zToken,
38289 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20   nToken);.      
3828a 20 20 6e 54 65 6d 70 20 2b 3d 20 6e 54 6f 6b 65    nTemp += nToke
3828b 6e 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  n;.        if( i
3828c 45 6e 64 3c 6e 49 6e 70 75 74 20 26 26 20 7a 49  End<nInput && zI
3828d 6e 70 75 74 5b 69 45 6e 64 5d 3d 3d 27 2a 27 20  nput[iEnd]=='*' 
3828e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e  ){.          p->
3828f 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
38290 69 69 5d 2e 69 73 50 72 65 66 69 78 20 3d 20 31  ii].isPrefix = 1
38291 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
38292 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50  .          p->pP
38293 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 69  hrase->aToken[ii
38294 5d 2e 69 73 50 72 65 66 69 78 20 3d 20 30 3b 0a  ].isPrefix = 0;.
38295 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
38296 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 4d 6f  }.    }..    pMo
38297 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43 75  dule->xClose(pCu
38298 72 73 6f 72 29 3b 0a 20 20 20 20 70 43 75 72 73  rsor);.    pCurs
38299 6f 72 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 69  or = 0;.  }..  i
3829a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
3829b 4e 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 6a  NE ){.    int jj
3829c 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77  ;.    char *zNew
3829d 3b 0a 20 20 20 20 69 6e 74 20 6e 4e 65 77 20 3d  ;.    int nNew =
3829e 20 30 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74   0;.    int nByt
3829f 65 20 3d 20 73 69 7a 65 6f 66 28 46 74 73 33 45  e = sizeof(Fts3E
382a0 78 70 72 29 20 2b 20 73 69 7a 65 6f 66 28 46 74  xpr) + sizeof(Ft
382a1 73 33 50 68 72 61 73 65 29 3b 0a 20 20 20 20 6e  s3Phrase);.    n
382a2 42 79 74 65 20 2b 3d 20 28 70 3f 28 70 2d 3e 70  Byte += (p?(p->p
382a3 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 2d 31  Phrase->nToken-1
382a4 29 3a 30 29 20 2a 20 73 69 7a 65 6f 66 28 73 74  ):0) * sizeof(st
382a5 72 75 63 74 20 50 68 72 61 73 65 54 6f 6b 65 6e  ruct PhraseToken
382a6 29 3b 0a 20 20 20 20 70 20 3d 20 66 74 73 33 52  );.    p = fts3R
382a7 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 2c 20  eallocOrFree(p, 
382a8 6e 42 79 74 65 20 2b 20 6e 54 65 6d 70 29 3b 0a  nByte + nTemp);.
382a9 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20      if( !p ){.  
382aa 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b      goto no_mem;
382ab 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 7a  .    }.    if( z
382ac 54 65 6d 70 20 29 7b 0a 20 20 20 20 20 20 7a 4e  Temp ){.      zN
382ad 65 77 20 3d 20 26 28 28 28 63 68 61 72 20 2a 29  ew = &(((char *)
382ae 70 29 5b 6e 42 79 74 65 5d 29 3b 0a 20 20 20 20  p)[nByte]);.    
382af 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 7a    memcpy(zNew, z
382b0 54 65 6d 70 2c 20 6e 54 65 6d 70 29 3b 0a 20 20  Temp, nTemp);.  
382b1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d    }else{.      m
382b2 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 42 79 74  emset(p, 0, nByt
382b3 65 2b 6e 54 65 6d 70 29 3b 0a 20 20 20 20 7d 0a  e+nTemp);.    }.
382b4 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65 20 3d      p->pPhrase =
382b5 20 28 46 74 73 33 50 68 72 61 73 65 20 2a 29 26   (Fts3Phrase *)&
382b6 70 5b 31 5d 3b 0a 20 20 20 20 66 6f 72 28 6a 6a  p[1];.    for(jj
382b7 3d 30 3b 20 6a 6a 3c 70 2d 3e 70 50 68 72 61 73  =0; jj<p->pPhras
382b8 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 6a 6a 2b 2b 29  e->nToken; jj++)
382b9 7b 0a 20 20 20 20 20 20 70 2d 3e 70 50 68 72 61  {.      p->pPhra
382ba 73 65 2d 3e 61 54 6f 6b 65 6e 5b 6a 6a 5d 2e 7a  se->aToken[jj].z
382bb 20 3d 20 26 7a 4e 65 77 5b 6e 4e 65 77 5d 3b 0a   = &zNew[nNew];.
382bc 20 20 20 20 20 20 6e 4e 65 77 20 2b 3d 20 70 2d        nNew += p-
382bd 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e  >pPhrase->aToken
382be 5b 6a 6a 5d 2e 6e 3b 0a 20 20 20 20 7d 0a 20 20  [jj].n;.    }.  
382bf 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
382c0 54 65 6d 70 29 3b 0a 20 20 20 20 70 2d 3e 65 54  Temp);.    p->eT
382c1 79 70 65 20 3d 20 46 54 53 51 55 45 52 59 5f 50  ype = FTSQUERY_P
382c2 48 52 41 53 45 3b 0a 20 20 20 20 70 2d 3e 70 50  HRASE;.    p->pP
382c3 68 72 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e 20 3d  hrase->iColumn =
382c4 20 70 50 61 72 73 65 2d 3e 69 44 65 66 61 75 6c   pParse->iDefaul
382c5 74 43 6f 6c 3b 0a 20 20 20 20 72 63 20 3d 20 53  tCol;.    rc = S
382c6 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
382c7 20 2a 70 70 45 78 70 72 20 3d 20 70 3b 0a 20 20   *ppExpr = p;.  
382c8 72 65 74 75 72 6e 20 72 63 3b 0a 6e 6f 5f 6d 65  return rc;.no_me
382c9 6d 3a 0a 0a 20 20 69 66 28 20 70 43 75 72 73 6f  m:..  if( pCurso
382ca 72 20 29 7b 0a 20 20 20 20 70 4d 6f 64 75 6c 65  r ){.    pModule
382cb 2d 3e 78 43 6c 6f 73 65 28 70 43 75 72 73 6f 72  ->xClose(pCursor
382cc 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
382cd 5f 66 72 65 65 28 7a 54 65 6d 70 29 3b 0a 20 20  _free(zTemp);.  
382ce 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
382cf 0a 20 20 2a 70 70 45 78 70 72 20 3d 20 30 3b 0a  .  *ppExpr = 0;.
382d0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
382d1 4e 4f 4d 45 4d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  NOMEM;.}../*.** 
382d2 46 75 6e 63 74 69 6f 6e 20 67 65 74 4e 65 78 74  Function getNext
382d3 4e 6f 64 65 28 29 2c 20 77 68 69 63 68 20 69 73  Node(), which is
382d4 20 63 61 6c 6c 65 64 20 62 79 20 66 74 73 33 45   called by fts3E
382d5 78 70 72 50 61 72 73 65 28 29 2c 20 6d 61 79 20  xprParse(), may 
382d6 69 74 73 65 6c 66 0a 2a 2a 20 63 61 6c 6c 20 66  itself.** call f
382d7 74 73 33 45 78 70 72 50 61 72 73 65 28 29 2e 20  ts3ExprParse(). 
382d8 53 6f 20 74 68 69 73 20 66 6f 72 77 61 72 64 20  So this forward 
382d9 64 65 63 6c 61 72 61 74 69 6f 6e 20 69 73 20 72  declaration is r
382da 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74  equired..*/.stat
382db 69 63 20 69 6e 74 20 66 74 73 33 45 78 70 72 50  ic int fts3ExprP
382dc 61 72 73 65 28 50 61 72 73 65 43 6f 6e 74 65 78  arse(ParseContex
382dd 74 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  t *, const char 
382de 2a 2c 20 69 6e 74 2c 20 46 74 73 33 45 78 70 72  *, int, Fts3Expr
382df 20 2a 2a 2c 20 69 6e 74 20 2a 29 3b 0a 0a 2f 2a   **, int *);../*
382e0 0a 2a 2a 20 54 68 65 20 6f 75 74 70 75 74 20 76  .** The output v
382e1 61 72 69 61 62 6c 65 20 2a 70 70 45 78 70 72 20  ariable *ppExpr 
382e2 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74  is populated wit
382e3 68 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 46  h an allocated F
382e4 74 73 33 45 78 70 72 20 0a 2a 2a 20 73 74 72 75  ts3Expr .** stru
382e5 63 74 75 72 65 2c 20 6f 72 20 73 65 74 20 74 6f  cture, or set to
382e6 20 30 20 69 66 20 74 68 65 20 65 6e 64 20 6f 66   0 if the end of
382e7 20 74 68 65 20 69 6e 70 75 74 20 62 75 66 66 65   the input buffe
382e8 72 20 69 73 20 72 65 61 63 68 65 64 2e 0a 2a 2a  r is reached..**
382e9 0a 2a 2a 20 52 65 74 75 72 6e 73 20 61 6e 20 53  .** Returns an S
382ea 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
382eb 2e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65  . SQLITE_OK if e
382ec 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b 73 2c  verything works,
382ed 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a   SQLITE_NOMEM.**
382ee 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69   if a malloc fai
382ef 6c 75 72 65 20 6f 63 63 75 72 73 2c 20 6f 72 20  lure occurs, or 
382f0 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20  SQLITE_ERROR if 
382f1 61 20 70 61 72 73 65 20 65 72 72 6f 72 20 69 73  a parse error is
382f2 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a   encountered..**
382f3 20 49 66 20 53 51 4c 49 54 45 5f 45 52 52 4f 52   If SQLITE_ERROR
382f4 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 70 43   is returned, pC
382f5 6f 6e 74 65 78 74 20 69 73 20 70 6f 70 75 6c 61  ontext is popula
382f6 74 65 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f  ted with an erro
382f7 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a 73 74  r message..*/.st
382f8 61 74 69 63 20 69 6e 74 20 67 65 74 4e 65 78 74  atic int getNext
382f9 4e 6f 64 65 28 0a 20 20 50 61 72 73 65 43 6f 6e  Node(.  ParseCon
382fa 74 65 78 74 20 2a 70 50 61 72 73 65 2c 20 20 20  text *pParse,   
382fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
382fc 2f 2a 20 66 74 73 33 20 71 75 65 72 79 20 70 61  /* fts3 query pa
382fd 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  rse context */. 
382fe 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
382ff 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20  int n,          
38300 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75           /* Inpu
38301 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 46 74  t string */.  Ft
38302 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70 72 2c  s3Expr **ppExpr,
38303 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38304 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 65 78        /* OUT: ex
38305 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e  pression */.  in
38306 74 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 20 20  t *pnConsumed   
38307 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38308 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75        /* OUT: Nu
38309 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 63 6f  mber of bytes co
3830a 6e 73 75 6d 65 64 20 2a 2f 0a 29 7b 0a 20 20 73  nsumed */.){.  s
3830b 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75  tatic const stru
3830c 63 74 20 46 74 73 33 4b 65 79 77 6f 72 64 20 7b  ct Fts3Keyword {
3830d 0a 20 20 20 20 63 68 61 72 20 7a 5b 34 5d 3b 20  .    char z[4]; 
3830e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3830f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65             /* Ke
38310 79 77 6f 72 64 20 74 65 78 74 20 2a 2f 0a 20 20  yword text */.  
38311 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
38312 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
38313 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
38314 68 20 6f 66 20 74 68 65 20 6b 65 79 77 6f 72 64  h of the keyword
38315 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64   */.    unsigned
38316 20 63 68 61 72 20 70 61 72 65 6e 4f 6e 6c 79 3b   char parenOnly;
38317 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
38318 20 4f 6e 6c 79 20 76 61 6c 69 64 20 69 6e 20 70   Only valid in p
38319 61 72 65 6e 20 6d 6f 64 65 20 2a 2f 0a 20 20 20  aren mode */.   
3831a 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 65   unsigned char e
3831b 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20  Type;           
3831c 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 77 6f 72         /* Keywor
3831d 64 20 63 6f 64 65 20 2a 2f 0a 20 20 7d 20 61 4b  d code */.  } aK
3831e 65 79 77 6f 72 64 5b 5d 20 3d 20 7b 0a 20 20 20  eyword[] = {.   
3831f 20 7b 20 22 4f 52 22 20 2c 20 20 32 2c 20 30 2c   { "OR" ,  2, 0,
38320 20 46 54 53 51 55 45 52 59 5f 4f 52 20 20 20 7d   FTSQUERY_OR   }
38321 2c 0a 20 20 20 20 7b 20 22 41 4e 44 22 2c 20 20  ,.    { "AND",  
38322 33 2c 20 31 2c 20 46 54 53 51 55 45 52 59 5f 41  3, 1, FTSQUERY_A
38323 4e 44 20 20 7d 2c 0a 20 20 20 20 7b 20 22 4e 4f  ND  },.    { "NO
38324 54 22 2c 20 20 33 2c 20 31 2c 20 46 54 53 51 55  T",  3, 1, FTSQU
38325 45 52 59 5f 4e 4f 54 20 20 7d 2c 0a 20 20 20 20  ERY_NOT  },.    
38326 7b 20 22 4e 45 41 52 22 2c 20 34 2c 20 30 2c 20  { "NEAR", 4, 0, 
38327 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20 7d 0a  FTSQUERY_NEAR }.
38328 20 20 7d 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20    };.  int ii;. 
38329 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 69 6e 74   int iCol;.  int
3832a 20 69 43 6f 6c 4c 65 6e 3b 0a 20 20 69 6e 74 20   iColLen;.  int 
3832b 72 63 3b 0a 20 20 46 74 73 33 45 78 70 72 20 2a  rc;.  Fts3Expr *
3832c 70 52 65 74 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e  pRet = 0;..  con
3832d 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74 20  st char *zInput 
3832e 3d 20 7a 3b 0a 20 20 69 6e 74 20 6e 49 6e 70 75  = z;.  int nInpu
3832f 74 20 3d 20 6e 3b 0a 0a 20 20 2f 2a 20 53 6b 69  t = n;..  /* Ski
38330 70 20 6f 76 65 72 20 61 6e 79 20 77 68 69 74 65  p over any white
38331 73 70 61 63 65 20 62 65 66 6f 72 65 20 63 68 65  space before che
38332 63 6b 69 6e 67 20 66 6f 72 20 61 20 6b 65 79 77  cking for a keyw
38333 6f 72 64 2c 20 61 6e 20 6f 70 65 6e 20 6f 72 0a  ord, an open or.
38334 20 20 2a 2a 20 63 6c 6f 73 65 20 62 72 61 63 6b    ** close brack
38335 65 74 2c 20 6f 72 20 61 20 71 75 6f 74 65 64 20  et, or a quoted 
38336 73 74 72 69 6e 67 2e 20 0a 20 20 2a 2f 0a 20 20  string. .  */.  
38337 77 68 69 6c 65 28 20 6e 49 6e 70 75 74 3e 30 20  while( nInput>0 
38338 26 26 20 66 74 73 33 69 73 73 70 61 63 65 28 2a  && fts3isspace(*
38339 7a 49 6e 70 75 74 29 20 29 7b 0a 20 20 20 20 6e  zInput) ){.    n
3833a 49 6e 70 75 74 2d 2d 3b 0a 20 20 20 20 7a 49 6e  Input--;.    zIn
3833b 70 75 74 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28  put++;.  }.  if(
3833c 20 6e 49 6e 70 75 74 3d 3d 30 20 29 7b 0a 20 20   nInput==0 ){.  
3833d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3833e 44 4f 4e 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  DONE;.  }..  /* 
3833f 53 65 65 20 69 66 20 77 65 20 61 72 65 20 64 65  See if we are de
38340 61 6c 69 6e 67 20 77 69 74 68 20 61 20 6b 65 79  aling with a key
38341 77 6f 72 64 2e 20 2a 2f 0a 20 20 66 6f 72 28 69  word. */.  for(i
38342 69 3d 30 3b 20 69 69 3c 28 69 6e 74 29 28 73 69  i=0; ii<(int)(si
38343 7a 65 6f 66 28 61 4b 65 79 77 6f 72 64 29 2f 73  zeof(aKeyword)/s
38344 69 7a 65 6f 66 28 73 74 72 75 63 74 20 46 74 73  izeof(struct Fts
38345 33 4b 65 79 77 6f 72 64 29 29 3b 20 69 69 2b 2b  3Keyword)); ii++
38346 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 73 74 72  ){.    const str
38347 75 63 74 20 46 74 73 33 4b 65 79 77 6f 72 64 20  uct Fts3Keyword 
38348 2a 70 4b 65 79 20 3d 20 26 61 4b 65 79 77 6f 72  *pKey = &aKeywor
38349 64 5b 69 69 5d 3b 0a 0a 20 20 20 20 69 66 28 20  d[ii];..    if( 
3834a 28 70 4b 65 79 2d 3e 70 61 72 65 6e 4f 6e 6c 79  (pKey->parenOnly
3834b 20 26 20 7e 73 71 6c 69 74 65 33 5f 66 74 73 33   & ~sqlite3_fts3
3834c 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65  _enable_parenthe
3834d 73 65 73 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ses)!=0 ){.     
3834e 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d   continue;.    }
3834f 0a 0a 20 20 20 20 69 66 28 20 6e 49 6e 70 75 74  ..    if( nInput
38350 3e 3d 70 4b 65 79 2d 3e 6e 20 26 26 20 30 3d 3d  >=pKey->n && 0==
38351 6d 65 6d 63 6d 70 28 7a 49 6e 70 75 74 2c 20 70  memcmp(zInput, p
38352 4b 65 79 2d 3e 7a 2c 20 70 4b 65 79 2d 3e 6e 29  Key->z, pKey->n)
38353 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4e   ){.      int nN
38354 65 61 72 20 3d 20 53 51 4c 49 54 45 5f 46 54 53  ear = SQLITE_FTS
38355 33 5f 44 45 46 41 55 4c 54 5f 4e 45 41 52 5f 50  3_DEFAULT_NEAR_P
38356 41 52 41 4d 3b 0a 20 20 20 20 20 20 69 6e 74 20  ARAM;.      int 
38357 6e 4b 65 79 20 3d 20 70 4b 65 79 2d 3e 6e 3b 0a  nKey = pKey->n;.
38358 20 20 20 20 20 20 63 68 61 72 20 63 4e 65 78 74        char cNext
38359 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  ;..      /* If t
3835a 68 69 73 20 69 73 20 61 20 22 4e 45 41 52 22 20  his is a "NEAR" 
3835b 6b 65 79 77 6f 72 64 2c 20 63 68 65 63 6b 20 66  keyword, check f
3835c 6f 72 20 61 6e 20 65 78 70 6c 69 63 69 74 20 6e  or an explicit n
3835d 65 61 72 6e 65 73 73 2e 20 2a 2f 0a 20 20 20 20  earness. */.    
3835e 20 20 69 66 28 20 70 4b 65 79 2d 3e 65 54 79 70    if( pKey->eTyp
3835f 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52  e==FTSQUERY_NEAR
38360 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
38361 72 74 28 20 6e 4b 65 79 3d 3d 34 20 29 3b 0a 20  rt( nKey==4 );. 
38362 20 20 20 20 20 20 20 69 66 28 20 7a 49 6e 70 75         if( zInpu
38363 74 5b 34 5d 3d 3d 27 2f 27 20 26 26 20 7a 49 6e  t[4]=='/' && zIn
38364 70 75 74 5b 35 5d 3e 3d 27 30 27 20 26 26 20 7a  put[5]>='0' && z
38365 49 6e 70 75 74 5b 35 5d 3c 3d 27 39 27 20 29 7b  Input[5]<='9' ){
38366 0a 20 20 20 20 20 20 20 20 20 20 6e 4e 65 61 72  .          nNear
38367 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
38368 66 6f 72 28 6e 4b 65 79 3d 35 3b 20 7a 49 6e 70  for(nKey=5; zInp
38369 75 74 5b 6e 4b 65 79 5d 3e 3d 27 30 27 20 26 26  ut[nKey]>='0' &&
3836a 20 7a 49 6e 70 75 74 5b 6e 4b 65 79 5d 3c 3d 27   zInput[nKey]<='
3836b 39 27 3b 20 6e 4b 65 79 2b 2b 29 7b 0a 20 20 20  9'; nKey++){.   
3836c 20 20 20 20 20 20 20 20 20 6e 4e 65 61 72 20 3d           nNear =
3836d 20 6e 4e 65 61 72 20 2a 20 31 30 20 2b 20 28 7a   nNear * 10 + (z
3836e 49 6e 70 75 74 5b 6e 4b 65 79 5d 20 2d 20 27 30  Input[nKey] - '0
3836f 27 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ');.          }.
38370 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
38371 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 41 74 20 74  }..      /* At t
38372 68 69 73 20 70 6f 69 6e 74 20 74 68 69 73 20 69  his point this i
38373 73 20 70 72 6f 62 61 62 6c 79 20 61 20 6b 65 79  s probably a key
38374 77 6f 72 64 2e 20 42 75 74 20 66 6f 72 20 74 68  word. But for th
38375 61 74 20 74 6f 20 62 65 20 74 72 75 65 2c 0a 20  at to be true,. 
38376 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 78 74       ** the next
38377 20 62 79 74 65 20 6d 75 73 74 20 63 6f 6e 74 61   byte must conta
38378 69 6e 20 65 69 74 68 65 72 20 77 68 69 74 65 73  in either whites
38379 70 61 63 65 2c 20 61 6e 20 6f 70 65 6e 20 6f 72  pace, an open or
3837a 20 63 6c 6f 73 65 0a 20 20 20 20 20 20 2a 2a 20   close.      ** 
3837b 70 61 72 65 6e 74 68 65 73 69 73 2c 20 61 20 71  parenthesis, a q
3837c 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 2c 20  uote character, 
3837d 6f 72 20 45 4f 46 2e 20 0a 20 20 20 20 20 20 2a  or EOF. .      *
3837e 2f 0a 20 20 20 20 20 20 63 4e 65 78 74 20 3d 20  /.      cNext = 
3837f 7a 49 6e 70 75 74 5b 6e 4b 65 79 5d 3b 0a 20 20  zInput[nKey];.  
38380 20 20 20 20 69 66 28 20 66 74 73 33 69 73 73 70      if( fts3issp
38381 61 63 65 28 63 4e 65 78 74 29 20 0a 20 20 20 20  ace(cNext) .    
38382 20 20 20 7c 7c 20 63 4e 65 78 74 3d 3d 27 22 27     || cNext=='"'
38383 20 7c 7c 20 63 4e 65 78 74 3d 3d 27 28 27 20 7c   || cNext=='(' |
38384 7c 20 63 4e 65 78 74 3d 3d 27 29 27 20 7c 7c 20  | cNext==')' || 
38385 63 4e 65 78 74 3d 3d 30 0a 20 20 20 20 20 20 29  cNext==0.      )
38386 7b 0a 20 20 20 20 20 20 20 20 70 52 65 74 20 3d  {.        pRet =
38387 20 28 46 74 73 33 45 78 70 72 20 2a 29 73 71 6c   (Fts3Expr *)sql
38388 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
38389 6f 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20  of(Fts3Expr));. 
3838a 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 52         memset(pR
3838b 65 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 46 74  et, 0, sizeof(Ft
3838c 73 33 45 78 70 72 29 29 3b 0a 20 20 20 20 20 20  s3Expr));.      
3838d 20 20 70 52 65 74 2d 3e 65 54 79 70 65 20 3d 20    pRet->eType = 
3838e 70 4b 65 79 2d 3e 65 54 79 70 65 3b 0a 20 20 20  pKey->eType;.   
3838f 20 20 20 20 20 70 52 65 74 2d 3e 6e 4e 65 61 72       pRet->nNear
38390 20 3d 20 6e 4e 65 61 72 3b 0a 20 20 20 20 20 20   = nNear;.      
38391 20 20 2a 70 70 45 78 70 72 20 3d 20 70 52 65 74    *ppExpr = pRet
38392 3b 0a 20 20 20 20 20 20 20 20 2a 70 6e 43 6f 6e  ;.        *pnCon
38393 73 75 6d 65 64 20 3d 20 28 7a 49 6e 70 75 74 20  sumed = (zInput 
38394 2d 20 7a 29 20 2b 20 6e 4b 65 79 3b 0a 20 20 20  - z) + nKey;.   
38395 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
38396 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 0a  TE_OK;.      }..
38397 20 20 20 20 20 20 2f 2a 20 54 75 72 6e 73 20 6f        /* Turns o
38398 75 74 20 74 68 61 74 20 77 61 73 6e 27 74 20 61  ut that wasn't a
38399 20 6b 65 79 77 6f 72 64 20 61 66 74 65 72 20 61   keyword after a
3839a 6c 6c 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73  ll. This happens
3839b 20 69 66 20 74 68 65 0a 20 20 20 20 20 20 2a 2a   if the.      **
3839c 20 75 73 65 72 20 68 61 73 20 73 75 70 70 6c 69   user has suppli
3839d 65 64 20 61 20 74 6f 6b 65 6e 20 73 75 63 68 20  ed a token such 
3839e 61 73 20 22 4f 52 61 63 6c 65 22 2e 20 43 6f 6e  as "ORacle". Con
3839f 74 69 6e 75 65 2e 0a 20 20 20 20 20 20 2a 2f 0a  tinue..      */.
383a0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
383a1 43 68 65 63 6b 20 66 6f 72 20 61 6e 20 6f 70 65  Check for an ope
383a2 6e 20 62 72 61 63 6b 65 74 2e 20 2a 2f 0a 20 20  n bracket. */.  
383a3 69 66 28 20 73 71 6c 69 74 65 33 5f 66 74 73 33  if( sqlite3_fts3
383a4 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65  _enable_parenthe
383a5 73 65 73 20 29 7b 0a 20 20 20 20 69 66 28 20 2a  ses ){.    if( *
383a6 7a 49 6e 70 75 74 3d 3d 27 28 27 20 29 7b 0a 20  zInput=='(' ){. 
383a7 20 20 20 20 20 69 6e 74 20 6e 43 6f 6e 73 75 6d       int nConsum
383a8 65 64 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 63  ed;.      int rc
383a9 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
383aa 6e 4e 65 73 74 2b 2b 3b 0a 20 20 20 20 20 20 72  nNest++;.      r
383ab 63 20 3d 20 66 74 73 33 45 78 70 72 50 61 72 73  c = fts3ExprPars
383ac 65 28 70 50 61 72 73 65 2c 20 26 7a 49 6e 70 75  e(pParse, &zInpu
383ad 74 5b 31 5d 2c 20 6e 49 6e 70 75 74 2d 31 2c 20  t[1], nInput-1, 
383ae 70 70 45 78 70 72 2c 20 26 6e 43 6f 6e 73 75 6d  ppExpr, &nConsum
383af 65 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ed);.      if( r
383b0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
383b1 21 2a 70 70 45 78 70 72 20 29 7b 0a 20 20 20 20  !*ppExpr ){.    
383b2 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
383b3 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20  DONE;.      }.  
383b4 20 20 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20      *pnConsumed 
383b5 3d 20 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b  = (zInput - z) +
383b6 20 31 20 2b 20 6e 43 6f 6e 73 75 6d 65 64 3b 0a   1 + nConsumed;.
383b7 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
383b8 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
383b9 20 43 68 65 63 6b 20 66 6f 72 20 61 20 63 6c 6f   Check for a clo
383ba 73 65 20 62 72 61 63 6b 65 74 2e 20 2a 2f 0a 20  se bracket. */. 
383bb 20 20 20 69 66 28 20 2a 7a 49 6e 70 75 74 3d 3d     if( *zInput==
383bc 27 29 27 20 29 7b 0a 20 20 20 20 20 20 70 50 61  ')' ){.      pPa
383bd 72 73 65 2d 3e 6e 4e 65 73 74 2d 2d 3b 0a 20 20  rse->nNest--;.  
383be 20 20 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20      *pnConsumed 
383bf 3d 20 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b  = (zInput - z) +
383c0 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   1;.      return
383c1 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20   SQLITE_DONE;.  
383c2 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65    }.  }..  /* Se
383c3 65 20 69 66 20 77 65 20 61 72 65 20 64 65 61 6c  e if we are deal
383c4 69 6e 67 20 77 69 74 68 20 61 20 71 75 6f 74 65  ing with a quote
383c5 64 20 70 68 72 61 73 65 2e 20 49 66 20 74 68 69  d phrase. If thi
383c6 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74  s is the case, t
383c7 68 65 6e 0a 20 20 2a 2a 20 73 65 61 72 63 68 20  hen.  ** search 
383c8 66 6f 72 20 74 68 65 20 63 6c 6f 73 69 6e 67 20  for the closing 
383c9 71 75 6f 74 65 20 61 6e 64 20 70 61 73 73 20 74  quote and pass t
383ca 68 65 20 77 68 6f 6c 65 20 73 74 72 69 6e 67 20  he whole string 
383cb 74 6f 20 67 65 74 4e 65 78 74 53 74 72 69 6e 67  to getNextString
383cc 28 29 0a 20 20 2a 2a 20 66 6f 72 20 70 72 6f 63  ().  ** for proc
383cd 65 73 73 69 6e 67 2e 20 54 68 69 73 20 69 73 20  essing. This is 
383ce 65 61 73 79 20 74 6f 20 64 6f 2c 20 61 73 20 66  easy to do, as f
383cf 74 73 33 20 68 61 73 20 6e 6f 20 73 79 6e 74 61  ts3 has no synta
383d0 78 20 66 6f 72 20 65 73 63 61 70 69 6e 67 0a 20  x for escaping. 
383d1 20 2a 2a 20 61 20 71 75 6f 74 65 20 63 68 61 72   ** a quote char
383d2 61 63 74 65 72 20 65 6d 62 65 64 64 65 64 20 69  acter embedded i
383d3 6e 20 61 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2f  n a string..  */
383d4 0a 20 20 69 66 28 20 2a 7a 49 6e 70 75 74 3d 3d  .  if( *zInput==
383d5 27 22 27 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  '"' ){.    for(i
383d6 69 3d 31 3b 20 69 69 3c 6e 49 6e 70 75 74 20 26  i=1; ii<nInput &
383d7 26 20 7a 49 6e 70 75 74 5b 69 69 5d 21 3d 27 22  & zInput[ii]!='"
383d8 27 3b 20 69 69 2b 2b 29 3b 0a 20 20 20 20 2a 70  '; ii++);.    *p
383d9 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 28 7a 49 6e  nConsumed = (zIn
383da 70 75 74 20 2d 20 7a 29 20 2b 20 69 69 20 2b 20  put - z) + ii + 
383db 31 3b 0a 20 20 20 20 69 66 28 20 69 69 3d 3d 6e  1;.    if( ii==n
383dc 49 6e 70 75 74 20 29 7b 0a 20 20 20 20 20 20 72  Input ){.      r
383dd 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
383de 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  OR;.    }.    re
383df 74 75 72 6e 20 67 65 74 4e 65 78 74 53 74 72 69  turn getNextStri
383e0 6e 67 28 70 50 61 72 73 65 2c 20 26 7a 49 6e 70  ng(pParse, &zInp
383e1 75 74 5b 31 5d 2c 20 69 69 2d 31 2c 20 70 70 45  ut[1], ii-1, ppE
383e2 78 70 72 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a  xpr);.  }...  /*
383e3 20 49 66 20 63 6f 6e 74 72 6f 6c 20 66 6c 6f 77   If control flow
383e4 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 2c  s to this point,
383e5 20 74 68 69 73 20 6d 75 73 74 20 62 65 20 61 20   this must be a 
383e6 72 65 67 75 6c 61 72 20 74 6f 6b 65 6e 2c 20 6f  regular token, o
383e7 72 20 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64 20  r .  ** the end 
383e8 6f 66 20 74 68 65 20 69 6e 70 75 74 2e 20 52 65  of the input. Re
383e9 61 64 20 61 20 72 65 67 75 6c 61 72 20 74 6f 6b  ad a regular tok
383ea 65 6e 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c  en using the sql
383eb 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a 20  ite3_tokenizer. 
383ec 20 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 20 42   ** interface. B
383ed 65 66 6f 72 65 20 64 6f 69 6e 67 20 73 6f 2c 20  efore doing so, 
383ee 66 69 67 75 72 65 20 6f 75 74 20 69 66 20 74 68  figure out if th
383ef 65 72 65 20 69 73 20 61 6e 20 65 78 70 6c 69 63  ere is an explic
383f0 69 74 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 73  it.  ** column s
383f1 70 65 63 69 66 69 65 72 20 66 6f 72 20 74 68 65  pecifier for the
383f2 20 74 6f 6b 65 6e 2e 20 0a 20 20 2a 2a 0a 20 20   token. .  **.  
383f3 2a 2a 20 54 4f 44 4f 3a 20 53 74 72 61 6e 67 65  ** TODO: Strange
383f4 6c 79 2c 20 69 74 20 69 73 20 6e 6f 74 20 70 6f  ly, it is not po
383f5 73 73 69 62 6c 65 20 74 6f 20 61 73 73 6f 63 69  ssible to associ
383f6 61 74 65 20 61 20 63 6f 6c 75 6d 6e 20 73 70 65  ate a column spe
383f7 63 69 66 69 65 72 0a 20 20 2a 2a 20 77 69 74 68  cifier.  ** with
383f8 20 61 20 71 75 6f 74 65 64 20 70 68 72 61 73 65   a quoted phrase
383f9 2c 20 6f 6e 6c 79 20 77 69 74 68 20 61 20 73 69  , only with a si
383fa 6e 67 6c 65 20 74 6f 6b 65 6e 2e 20 4e 6f 74 20  ngle token. Not 
383fb 73 75 72 65 20 69 66 20 74 68 69 73 20 77 61 73  sure if this was
383fc 0a 20 20 2a 2a 20 61 6e 20 69 6d 70 6c 65 6d 65  .  ** an impleme
383fd 6e 74 61 74 69 6f 6e 20 61 72 74 69 66 61 63 74  ntation artifact
383fe 20 6f 72 20 61 6e 20 69 6e 74 65 6e 74 69 6f 6e   or an intention
383ff 61 6c 20 64 65 63 69 73 69 6f 6e 20 77 68 65 6e  al decision when
38400 20 66 74 73 33 20 77 61 73 0a 20 20 2a 2a 20 66   fts3 was.  ** f
38401 69 72 73 74 20 69 6d 70 6c 65 6d 65 6e 74 65 64  irst implemented
38402 2e 20 57 68 69 63 68 65 76 65 72 20 69 74 20 77  . Whichever it w
38403 61 73 2c 20 74 68 69 73 20 6d 6f 64 75 6c 65 20  as, this module 
38404 64 75 70 6c 69 63 61 74 65 73 20 74 68 65 20 0a  duplicates the .
38405 20 20 2a 2a 20 6c 69 6d 69 74 61 74 69 6f 6e 2e    ** limitation.
38406 0a 20 20 2a 2f 0a 20 20 69 43 6f 6c 20 3d 20 70  .  */.  iCol = p
38407 50 61 72 73 65 2d 3e 69 44 65 66 61 75 6c 74 43  Parse->iDefaultC
38408 6f 6c 3b 0a 20 20 69 43 6f 6c 4c 65 6e 20 3d 20  ol;.  iColLen = 
38409 30 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  0;.  for(ii=0; i
3840a 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 3b 20  i<pParse->nCol; 
3840b 69 69 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74  ii++){.    const
3840c 20 63 68 61 72 20 2a 7a 53 74 72 20 3d 20 70 50   char *zStr = pP
3840d 61 72 73 65 2d 3e 61 7a 43 6f 6c 5b 69 69 5d 3b  arse->azCol[ii];
3840e 0a 20 20 20 20 69 6e 74 20 6e 53 74 72 20 3d 20  .    int nStr = 
3840f 73 74 72 6c 65 6e 28 7a 53 74 72 29 3b 0a 20 20  strlen(zStr);.  
38410 20 20 69 66 28 20 6e 49 6e 70 75 74 3e 6e 53 74    if( nInput>nSt
38411 72 20 26 26 20 7a 49 6e 70 75 74 5b 6e 53 74 72  r && zInput[nStr
38412 5d 3d 3d 27 3a 27 20 0a 20 20 20 20 20 26 26 20  ]==':' .     && 
38413 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70  sqlite3_strnicmp
38414 28 7a 53 74 72 2c 20 7a 49 6e 70 75 74 2c 20 6e  (zStr, zInput, n
38415 53 74 72 29 3d 3d 30 20 0a 20 20 20 20 29 7b 0a  Str)==0 .    ){.
38416 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 69 69 3b        iCol = ii;
38417 0a 20 20 20 20 20 20 69 43 6f 6c 4c 65 6e 20 3d  .      iColLen =
38418 20 28 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b   ((zInput - z) +
38419 20 6e 53 74 72 20 2b 20 31 29 3b 0a 20 20 20 20   nStr + 1);.    
3841a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
3841b 20 7d 0a 20 20 72 63 20 3d 20 67 65 74 4e 65 78   }.  rc = getNex
3841c 74 54 6f 6b 65 6e 28 70 50 61 72 73 65 2c 20 69  tToken(pParse, i
3841d 43 6f 6c 2c 20 26 7a 5b 69 43 6f 6c 4c 65 6e 5d  Col, &z[iColLen]
3841e 2c 20 6e 2d 69 43 6f 6c 4c 65 6e 2c 20 70 70 45  , n-iColLen, ppE
3841f 78 70 72 2c 20 70 6e 43 6f 6e 73 75 6d 65 64 29  xpr, pnConsumed)
38420 3b 0a 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20  ;.  *pnConsumed 
38421 2b 3d 20 69 43 6f 6c 4c 65 6e 3b 0a 20 20 72 65  += iColLen;.  re
38422 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
38423 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69  * The argument i
38424 73 20 61 6e 20 46 74 73 33 45 78 70 72 20 73 74  s an Fts3Expr st
38425 72 75 63 74 75 72 65 20 66 6f 72 20 61 20 62 69  ructure for a bi
38426 6e 61 72 79 20 6f 70 65 72 61 74 6f 72 20 28 61  nary operator (a
38427 6e 79 20 74 79 70 65 0a 2a 2a 20 65 78 63 65 70  ny type.** excep
38428 74 20 61 6e 20 46 54 53 51 55 45 52 59 5f 50 48  t an FTSQUERY_PH
38429 52 41 53 45 29 2e 20 52 65 74 75 72 6e 20 61 6e  RASE). Return an
3842a 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 72   integer value r
3842b 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 0a  epresenting the.
3842c 2a 2a 20 70 72 65 63 65 64 65 6e 63 65 20 6f 66  ** precedence of
3842d 20 74 68 65 20 6f 70 65 72 61 74 6f 72 2e 20 4c   the operator. L
3842e 6f 77 65 72 20 76 61 6c 75 65 73 20 68 61 76 65  ower values have
3842f 20 61 20 68 69 67 68 65 72 20 70 72 65 63 65 64   a higher preced
38430 65 6e 63 65 20 28 69 2e 65 2e 0a 2a 2a 20 67 72  ence (i.e..** gr
38431 6f 75 70 20 6d 6f 72 65 20 74 69 67 68 74 6c 79  oup more tightly
38432 29 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  ). For example, 
38433 69 6e 20 74 68 65 20 43 20 6c 61 6e 67 75 61 67  in the C languag
38434 65 2c 20 74 68 65 20 3d 3d 20 6f 70 65 72 61 74  e, the == operat
38435 6f 72 0a 2a 2a 20 67 72 6f 75 70 73 20 6d 6f 72  or.** groups mor
38436 65 20 74 69 67 68 74 6c 79 20 74 68 61 6e 20 7c  e tightly than |
38437 7c 2c 20 61 6e 64 20 77 6f 75 6c 64 20 74 68 65  |, and would the
38438 72 65 66 6f 72 65 20 68 61 76 65 20 61 20 68 69  refore have a hi
38439 67 68 65 72 20 70 72 65 63 65 64 65 6e 63 65 2e  gher precedence.
3843a 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 75 73 69 6e  .**.** When usin
3843b 67 20 74 68 65 20 6e 65 77 20 66 74 73 33 20 71  g the new fts3 q
3843c 75 65 72 79 20 73 79 6e 74 61 78 20 28 77 68 65  uery syntax (whe
3843d 6e 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  n SQLITE_ENABLE_
3843e 46 54 53 33 5f 50 41 52 45 4e 54 48 45 53 49 53  FTS3_PARENTHESIS
3843f 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 29 2c  .** is defined),
38440 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68   the order of th
38441 65 20 6f 70 65 72 61 74 6f 72 73 20 69 6e 20 70  e operators in p
38442 72 65 63 65 64 65 6e 63 65 20 66 72 6f 6d 20 68  recedence from h
38443 69 67 68 65 73 74 20 74 6f 0a 2a 2a 20 6c 6f 77  ighest to.** low
38444 65 73 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  est is:.**.**   
38445 4e 45 41 52 0a 2a 2a 20 20 20 4e 4f 54 0a 2a 2a  NEAR.**   NOT.**
38446 20 20 20 41 4e 44 20 28 69 6e 63 6c 75 64 69 6e     AND (includin
38447 67 20 69 6d 70 6c 69 63 69 74 20 41 4e 44 73 29  g implicit ANDs)
38448 0a 2a 2a 20 20 20 4f 52 0a 2a 2a 0a 2a 2a 20 4e  .**   OR.**.** N
38449 6f 74 65 20 74 68 61 74 20 77 68 65 6e 20 75 73  ote that when us
3844a 69 6e 67 20 74 68 65 20 6f 6c 64 20 71 75 65 72  ing the old quer
3844b 79 20 73 79 6e 74 61 78 2c 20 74 68 65 20 4f 52  y syntax, the OR
3844c 20 6f 70 65 72 61 74 6f 72 20 68 61 73 20 61 20   operator has a 
3844d 68 69 67 68 65 72 0a 2a 2a 20 70 72 65 63 65 64  higher.** preced
3844e 65 6e 63 65 20 74 68 61 6e 20 74 68 65 20 41 4e  ence than the AN
3844f 44 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73  D operator..*/.s
38450 74 61 74 69 63 20 69 6e 74 20 6f 70 50 72 65 63  tatic int opPrec
38451 65 64 65 6e 63 65 28 46 74 73 33 45 78 70 72 20  edence(Fts3Expr 
38452 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
38453 2d 3e 65 54 79 70 65 21 3d 46 54 53 51 55 45 52  ->eType!=FTSQUER
38454 59 5f 50 48 52 41 53 45 20 29 3b 0a 20 20 69 66  Y_PHRASE );.  if
38455 28 20 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65  ( sqlite3_fts3_e
38456 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65  nable_parenthese
38457 73 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  s ){.    return 
38458 70 2d 3e 65 54 79 70 65 3b 0a 20 20 7d 65 6c 73  p->eType;.  }els
38459 65 20 69 66 28 20 70 2d 3e 65 54 79 70 65 3d 3d  e if( p->eType==
3845a 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20 29 7b  FTSQUERY_NEAR ){
3845b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
3845c 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 54   }else if( p->eT
3845d 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4f 52  ype==FTSQUERY_OR
3845e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 32   ){.    return 2
3845f 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
38460 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  p->eType==FTSQUE
38461 52 59 5f 41 4e 44 20 29 3b 0a 20 20 72 65 74 75  RY_AND );.  retu
38462 72 6e 20 33 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  rn 3;.}../*.** A
38463 72 67 75 6d 65 6e 74 20 70 70 48 65 61 64 20 63  rgument ppHead c
38464 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65  ontains a pointe
38465 72 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74  r to the current
38466 20 68 65 61 64 20 6f 66 20 61 20 71 75 65 72 79   head of a query
38467 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20   .** expression 
38468 74 72 65 65 20 62 65 69 6e 67 20 70 61 72 73 65  tree being parse
38469 64 2e 20 70 50 72 65 76 20 69 73 20 74 68 65 20  d. pPrev is the 
3846a 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20  expression node 
3846b 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a  most recently.**
3846c 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74   inserted into t
3846d 68 65 20 74 72 65 65 2e 20 54 68 69 73 20 66 75  he tree. This fu
3846e 6e 63 74 69 6f 6e 20 61 64 64 73 20 70 4e 65 77  nction adds pNew
3846f 2c 20 77 68 69 63 68 20 69 73 20 61 6c 77 61 79  , which is alway
38470 73 20 61 20 62 69 6e 61 72 79 0a 2a 2a 20 6f 70  s a binary.** op
38471 65 72 61 74 6f 72 20 6e 6f 64 65 2c 20 69 6e 74  erator node, int
38472 6f 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  o the expression
38473 20 74 72 65 65 20 62 61 73 65 64 20 6f 6e 20 74   tree based on t
38474 68 65 20 72 65 6c 61 74 69 76 65 20 70 72 65 63  he relative prec
38475 65 64 65 6e 63 65 0a 2a 2a 20 6f 66 20 70 4e 65  edence.** of pNe
38476 77 20 61 6e 64 20 74 68 65 20 65 78 69 73 74 69  w and the existi
38477 6e 67 20 6e 6f 64 65 73 20 6f 66 20 74 68 65 20  ng nodes of the 
38478 74 72 65 65 2e 20 54 68 69 73 20 6d 61 79 20 72  tree. This may r
38479 65 73 75 6c 74 20 69 6e 20 74 68 65 20 68 65 61  esult in the hea
3847a 64 0a 2a 2a 20 6f 66 20 74 68 65 20 74 72 65 65  d.** of the tree
3847b 20 63 68 61 6e 67 69 6e 67 2c 20 69 6e 20 77 68   changing, in wh
3847c 69 63 68 20 63 61 73 65 20 2a 70 70 48 65 61 64  ich case *ppHead
3847d 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e   is set to the n
3847e 65 77 20 72 6f 6f 74 20 6e 6f 64 65 2e 0a 2a 2f  ew root node..*/
3847f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 73  .static void ins
38480 65 72 74 42 69 6e 61 72 79 4f 70 65 72 61 74 6f  ertBinaryOperato
38481 72 28 0a 20 20 46 74 73 33 45 78 70 72 20 2a 2a  r(.  Fts3Expr **
38482 70 70 48 65 61 64 2c 20 20 20 20 20 20 20 2f 2a  ppHead,       /*
38483 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   Pointer to the 
38484 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 61 20 74  root node of a t
38485 72 65 65 20 2a 2f 0a 20 20 46 74 73 33 45 78 70  ree */.  Fts3Exp
38486 72 20 2a 70 50 72 65 76 2c 20 20 20 20 20 20 20  r *pPrev,       
38487 20 20 2f 2a 20 4e 6f 64 65 20 6d 6f 73 74 20 72    /* Node most r
38488 65 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64  ecently inserted
38489 20 69 6e 74 6f 20 74 68 65 20 74 72 65 65 20 2a   into the tree *
3848a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 4e  /.  Fts3Expr *pN
3848b 65 77 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ew           /* 
3848c 4e 65 77 20 62 69 6e 61 72 79 20 6e 6f 64 65 20  New binary node 
3848d 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 65  to insert into e
3848e 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 2a  xpression tree *
3848f 2f 0a 29 7b 0a 20 20 46 74 73 33 45 78 70 72 20  /.){.  Fts3Expr 
38490 2a 70 53 70 6c 69 74 20 3d 20 70 50 72 65 76 3b  *pSplit = pPrev;
38491 0a 20 20 77 68 69 6c 65 28 20 70 53 70 6c 69 74  .  while( pSplit
38492 2d 3e 70 50 61 72 65 6e 74 20 26 26 20 6f 70 50  ->pParent && opP
38493 72 65 63 65 64 65 6e 63 65 28 70 53 70 6c 69 74  recedence(pSplit
38494 2d 3e 70 50 61 72 65 6e 74 29 3c 3d 6f 70 50 72  ->pParent)<=opPr
38495 65 63 65 64 65 6e 63 65 28 70 4e 65 77 29 20 29  ecedence(pNew) )
38496 7b 0a 20 20 20 20 70 53 70 6c 69 74 20 3d 20 70  {.    pSplit = p
38497 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 3b 0a  Split->pParent;.
38498 20 20 7d 0a 0a 20 20 69 66 28 20 70 53 70 6c 69    }..  if( pSpli
38499 74 2d 3e 70 50 61 72 65 6e 74 20 29 7b 0a 20 20  t->pParent ){.  
3849a 20 20 61 73 73 65 72 74 28 20 70 53 70 6c 69 74    assert( pSplit
3849b 2d 3e 70 50 61 72 65 6e 74 2d 3e 70 52 69 67 68  ->pParent->pRigh
3849c 74 3d 3d 70 53 70 6c 69 74 20 29 3b 0a 20 20 20  t==pSplit );.   
3849d 20 70 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74   pSplit->pParent
3849e 2d 3e 70 52 69 67 68 74 20 3d 20 70 4e 65 77 3b  ->pRight = pNew;
3849f 0a 20 20 20 20 70 4e 65 77 2d 3e 70 50 61 72 65  .    pNew->pPare
384a0 6e 74 20 3d 20 70 53 70 6c 69 74 2d 3e 70 50 61  nt = pSplit->pPa
384a1 72 65 6e 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  rent;.  }else{. 
384a2 20 20 20 2a 70 70 48 65 61 64 20 3d 20 70 4e 65     *ppHead = pNe
384a3 77 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 70  w;.  }.  pNew->p
384a4 4c 65 66 74 20 3d 20 70 53 70 6c 69 74 3b 0a 20  Left = pSplit;. 
384a5 20 70 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74   pSplit->pParent
384a6 20 3d 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a   = pNew;.}../*.*
384a7 2a 20 50 61 72 73 65 20 74 68 65 20 66 74 73 33  * Parse the fts3
384a8 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69 6f   query expressio
384a9 6e 20 66 6f 75 6e 64 20 69 6e 20 62 75 66 66 65  n found in buffe
384aa 72 20 7a 2c 20 6c 65 6e 67 74 68 20 6e 2e 20 54  r z, length n. T
384ab 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  his function.** 
384ac 72 65 74 75 72 6e 73 20 65 69 74 68 65 72 20 77  returns either w
384ad 68 65 6e 20 74 68 65 20 65 6e 64 20 6f 66 20 74  hen the end of t
384ae 68 65 20 62 75 66 66 65 72 20 69 73 20 72 65 61  he buffer is rea
384af 63 68 65 64 20 6f 72 20 61 6e 20 75 6e 6d 61 74  ched or an unmat
384b0 63 68 65 64 20 0a 2a 2a 20 63 6c 6f 73 69 6e 67  ched .** closing
384b1 20 62 72 61 63 6b 65 74 20 2d 20 27 29 27 20 2d   bracket - ')' -
384b2 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e   is encountered.
384b3 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73  .**.** If succes
384b4 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  sful, SQLITE_OK 
384b5 69 73 20 72 65 74 75 72 6e 65 64 2c 20 2a 70 70  is returned, *pp
384b6 45 78 70 72 20 69 73 20 73 65 74 20 74 6f 20 70  Expr is set to p
384b7 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a 20 70  oint to the.** p
384b8 61 72 73 65 64 20 66 6f 72 6d 20 6f 66 20 74 68  arsed form of th
384b9 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64  e expression and
384ba 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 69 73 20   *pnConsumed is 
384bb 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65  set to the numbe
384bc 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 72 65  r of.** bytes re
384bd 61 64 20 66 72 6f 6d 20 62 75 66 66 65 72 20 7a  ad from buffer z
384be 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 2a 70 70  . Otherwise, *pp
384bf 45 78 70 72 20 69 73 20 73 65 74 20 74 6f 20 30  Expr is set to 0
384c0 20 61 6e 64 20 53 51 4c 49 54 45 5f 4e 4f 4d 45   and SQLITE_NOME
384c1 4d 0a 2a 2a 20 28 6f 75 74 20 6f 66 20 6d 65 6d  M.** (out of mem
384c2 6f 72 79 20 65 72 72 6f 72 29 20 6f 72 20 53 51  ory error) or SQ
384c3 4c 49 54 45 5f 45 52 52 4f 52 20 28 70 61 72 73  LITE_ERROR (pars
384c4 65 20 65 72 72 6f 72 29 20 69 73 20 72 65 74 75  e error) is retu
384c5 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rned..*/.static 
384c6 69 6e 74 20 66 74 73 33 45 78 70 72 50 61 72 73  int fts3ExprPars
384c7 65 28 0a 20 20 50 61 72 73 65 43 6f 6e 74 65 78  e(.  ParseContex
384c8 74 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  t *pParse,      
384c9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
384ca 66 74 73 33 20 71 75 65 72 79 20 70 61 72 73 65  fts3 query parse
384cb 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f   context */.  co
384cc 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
384cd 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   n,             
384ce 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66        /* Text of
384cf 20 4d 41 54 43 48 20 71 75 65 72 79 20 2a 2f 0a   MATCH query */.
384d0 20 20 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45    Fts3Expr **ppE
384d1 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  xpr,            
384d2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
384d3 3a 20 50 61 72 73 65 64 20 71 75 65 72 79 20 73  : Parsed query s
384d4 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e  tructure */.  in
384d5 74 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 20 20  t *pnConsumed   
384d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
384d7 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75        /* OUT: Nu
384d8 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 63 6f  mber of bytes co
384d9 6e 73 75 6d 65 64 20 2a 2f 0a 29 7b 0a 20 20 46  nsumed */.){.  F
384da 74 73 33 45 78 70 72 20 2a 70 52 65 74 20 3d 20  ts3Expr *pRet = 
384db 30 3b 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70  0;.  Fts3Expr *p
384dc 50 72 65 76 20 3d 20 30 3b 0a 20 20 46 74 73 33  Prev = 0;.  Fts3
384dd 45 78 70 72 20 2a 70 4e 6f 74 42 72 61 6e 63 68  Expr *pNotBranch
384de 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
384df 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 75 73 65 64      /* Only used
384e0 20 69 6e 20 6c 65 67 61 63 79 20 70 61 72 73 65   in legacy parse
384e1 20 6d 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   mode */.  int n
384e2 49 6e 20 3d 20 6e 3b 0a 20 20 63 6f 6e 73 74 20  In = n;.  const 
384e3 63 68 61 72 20 2a 7a 49 6e 20 3d 20 7a 3b 0a 20  char *zIn = z;. 
384e4 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
384e5 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 73 52 65 71  _OK;.  int isReq
384e6 75 69 72 65 50 68 72 61 73 65 20 3d 20 31 3b 0a  uirePhrase = 1;.
384e7 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51  .  while( rc==SQ
384e8 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 46  LITE_OK ){.    F
384e9 74 73 33 45 78 70 72 20 2a 70 20 3d 20 30 3b 0a  ts3Expr *p = 0;.
384ea 20 20 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20      int nByte = 
384eb 30 3b 0a 20 20 20 20 72 63 20 3d 20 67 65 74 4e  0;.    rc = getN
384ec 65 78 74 4e 6f 64 65 28 70 50 61 72 73 65 2c 20  extNode(pParse, 
384ed 7a 49 6e 2c 20 6e 49 6e 2c 20 26 70 2c 20 26 6e  zIn, nIn, &p, &n
384ee 42 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Byte);.    if( r
384ef 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
384f0 20 20 20 20 20 20 69 6e 74 20 69 73 50 68 72 61        int isPhra
384f1 73 65 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 21  se;..      if( !
384f2 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61  sqlite3_fts3_ena
384f3 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73 20  ble_parentheses 
384f4 0a 20 20 20 20 20 20 20 26 26 20 70 2d 3e 65 54  .       && p->eT
384f5 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48  ype==FTSQUERY_PH
384f6 52 41 53 45 20 26 26 20 70 2d 3e 70 50 68 72 61  RASE && p->pPhra
384f7 73 65 2d 3e 69 73 4e 6f 74 20 0a 20 20 20 20 20  se->isNot .     
384f8 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43   ){.        /* C
384f9 72 65 61 74 65 20 61 6e 20 69 6d 70 6c 69 63 69  reate an implici
384fa 74 20 4e 4f 54 20 6f 70 65 72 61 74 6f 72 2e 20  t NOT operator. 
384fb 2a 2f 0a 20 20 20 20 20 20 20 20 46 74 73 33 45  */.        Fts3E
384fc 78 70 72 20 2a 70 4e 6f 74 20 3d 20 73 71 6c 69  xpr *pNot = sqli
384fd 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
384fe 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20 20  f(Fts3Expr));.  
384ff 20 20 20 20 20 20 69 66 28 20 21 70 4e 6f 74 20        if( !pNot 
38500 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
38501 69 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65  ite3Fts3ExprFree
38502 28 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72  (p);.          r
38503 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
38504 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
38505 20 65 78 70 72 70 61 72 73 65 5f 6f 75 74 3b 0a   exprparse_out;.
38506 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
38507 20 20 6d 65 6d 73 65 74 28 70 4e 6f 74 2c 20 30    memset(pNot, 0
38508 2c 20 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70  , sizeof(Fts3Exp
38509 72 29 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 6f  r));.        pNo
3850a 74 2d 3e 65 54 79 70 65 20 3d 20 46 54 53 51 55  t->eType = FTSQU
3850b 45 52 59 5f 4e 4f 54 3b 0a 20 20 20 20 20 20 20  ERY_NOT;.       
3850c 20 70 4e 6f 74 2d 3e 70 52 69 67 68 74 20 3d 20   pNot->pRight = 
3850d 70 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  p;.        if( p
3850e 4e 6f 74 42 72 61 6e 63 68 20 29 7b 0a 20 20 20  NotBranch ){.   
3850f 20 20 20 20 20 20 20 70 4e 6f 74 2d 3e 70 4c 65         pNot->pLe
38510 66 74 20 3d 20 70 4e 6f 74 42 72 61 6e 63 68 3b  ft = pNotBranch;
38511 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
38512 20 20 20 70 4e 6f 74 42 72 61 6e 63 68 20 3d 20     pNotBranch = 
38513 70 4e 6f 74 3b 0a 20 20 20 20 20 20 20 20 70 20  pNot;.        p 
38514 3d 20 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d  = pPrev;.      }
38515 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e  else{.        in
38516 74 20 65 54 79 70 65 20 3d 20 70 2d 3e 65 54 79  t eType = p->eTy
38517 70 65 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  pe;.        asse
38518 72 74 28 20 65 54 79 70 65 21 3d 46 54 53 51 55  rt( eType!=FTSQU
38519 45 52 59 5f 50 48 52 41 53 45 20 7c 7c 20 21 70  ERY_PHRASE || !p
3851a 2d 3e 70 50 68 72 61 73 65 2d 3e 69 73 4e 6f 74  ->pPhrase->isNot
3851b 20 29 3b 0a 20 20 20 20 20 20 20 20 69 73 50 68   );.        isPh
3851c 72 61 73 65 20 3d 20 28 65 54 79 70 65 3d 3d 46  rase = (eType==F
3851d 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 7c  TSQUERY_PHRASE |
3851e 7c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20 20  | p->pLeft);..  
3851f 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 73 52        /* The isR
38520 65 71 75 69 72 65 50 68 72 61 73 65 20 76 61 72  equirePhrase var
38521 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20  iable is set to 
38522 74 72 75 65 20 69 66 20 61 20 70 68 72 61 73 65  true if a phrase
38523 20 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 61   or.        ** a
38524 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e  n expression con
38525 74 61 69 6e 65 64 20 69 6e 20 70 61 72 65 6e 74  tained in parent
38526 68 65 73 69 73 20 69 73 20 72 65 71 75 69 72 65  hesis is require
38527 64 2e 20 49 66 20 61 0a 20 20 20 20 20 20 20 20  d. If a.        
38528 2a 2a 20 62 69 6e 61 72 79 20 6f 70 65 72 61 74  ** binary operat
38529 6f 72 20 28 41 4e 44 2c 20 4f 52 2c 20 4e 4f 54  or (AND, OR, NOT
3852a 20 6f 72 20 4e 45 41 52 29 20 69 73 20 65 6e 63   or NEAR) is enc
3852b 6f 75 6e 74 65 64 20 77 68 65 6e 0a 20 20 20 20  ounted when.    
3852c 20 20 20 20 2a 2a 20 69 73 52 65 71 75 69 72 65      ** isRequire
3852d 50 68 72 61 73 65 20 69 73 20 73 65 74 2c 20 74  Phrase is set, t
3852e 68 69 73 20 69 73 20 61 20 73 79 6e 74 61 78 20  his is a syntax 
3852f 65 72 72 6f 72 2e 0a 20 20 20 20 20 20 20 20 2a  error..        *
38530 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 69  /.        if( !i
38531 73 50 68 72 61 73 65 20 26 26 20 69 73 52 65 71  sPhrase && isReq
38532 75 69 72 65 50 68 72 61 73 65 20 29 7b 0a 20 20  uirePhrase ){.  
38533 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 46          sqlite3F
38534 74 73 33 45 78 70 72 46 72 65 65 28 70 29 3b 0a  ts3ExprFree(p);.
38535 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
38536 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
38537 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 70 72         goto expr
38538 70 61 72 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20  parse_out;.     
38539 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 20 20     }.  .        
3853a 69 66 28 20 69 73 50 68 72 61 73 65 20 26 26 20  if( isPhrase && 
3853b 21 69 73 52 65 71 75 69 72 65 50 68 72 61 73 65  !isRequirePhrase
3853c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
3853d 20 49 6e 73 65 72 74 20 61 6e 20 69 6d 70 6c 69   Insert an impli
3853e 63 69 74 20 41 4e 44 20 6f 70 65 72 61 74 6f 72  cit AND operator
3853f 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 46  . */.          F
38540 74 73 33 45 78 70 72 20 2a 70 41 6e 64 3b 0a 20  ts3Expr *pAnd;. 
38541 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
38542 20 70 52 65 74 20 26 26 20 70 50 72 65 76 20 29   pRet && pPrev )
38543 3b 0a 20 20 20 20 20 20 20 20 20 20 70 41 6e 64  ;.          pAnd
38544 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
38545 63 28 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70  c(sizeof(Fts3Exp
38546 72 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  r));.          i
38547 66 28 20 21 70 41 6e 64 20 29 7b 0a 20 20 20 20  f( !pAnd ){.    
38548 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 46          sqlite3F
38549 74 73 33 45 78 70 72 46 72 65 65 28 70 29 3b 0a  ts3ExprFree(p);.
3854a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
3854b 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
3854c 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20             goto 
3854d 65 78 70 72 70 61 72 73 65 5f 6f 75 74 3b 0a 20  exprparse_out;. 
3854e 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
3854f 20 20 20 20 20 6d 65 6d 73 65 74 28 70 41 6e 64       memset(pAnd
38550 2c 20 30 2c 20 73 69 7a 65 6f 66 28 46 74 73 33  , 0, sizeof(Fts3
38551 45 78 70 72 29 29 3b 0a 20 20 20 20 20 20 20 20  Expr));.        
38552 20 20 70 41 6e 64 2d 3e 65 54 79 70 65 20 3d 20    pAnd->eType = 
38553 46 54 53 51 55 45 52 59 5f 41 4e 44 3b 0a 20 20  FTSQUERY_AND;.  
38554 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 42 69          insertBi
38555 6e 61 72 79 4f 70 65 72 61 74 6f 72 28 26 70 52  naryOperator(&pR
38556 65 74 2c 20 70 50 72 65 76 2c 20 70 41 6e 64 29  et, pPrev, pAnd)
38557 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 72 65  ;.          pPre
38558 76 20 3d 20 70 41 6e 64 3b 0a 20 20 20 20 20 20  v = pAnd;.      
38559 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20    }..        /* 
3855a 54 68 69 73 20 74 65 73 74 20 63 61 74 63 68 65  This test catche
3855b 73 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6d 61  s attempts to ma
3855c 6b 65 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e  ke either operan
3855d 64 20 6f 66 20 61 20 4e 45 41 52 0a 20 20 20 20  d of a NEAR.    
3855e 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72 20      ** operator 
3855f 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 20  something other 
38560 74 68 61 6e 20 61 20 70 68 72 61 73 65 2e 20 46  than a phrase. F
38561 6f 72 20 65 78 61 6d 70 6c 65 2c 20 65 69 74 68  or example, eith
38562 65 72 20 6f 66 0a 20 20 20 20 20 20 20 20 2a 2a  er of.        **
38563 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
38564 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
38565 20 20 20 2a 2a 20 20 20 20 28 62 72 61 63 6b 65     **    (bracke
38566 74 65 64 20 65 78 70 72 65 73 73 69 6f 6e 29 20  ted expression) 
38567 4e 45 41 52 20 70 68 72 61 73 65 0a 20 20 20 20  NEAR phrase.    
38568 20 20 20 20 2a 2a 20 20 20 20 70 68 72 61 73 65      **    phrase
38569 20 4e 45 41 52 20 28 62 72 61 63 6b 65 74 65 64   NEAR (bracketed
3856a 20 65 78 70 72 65 73 73 69 6f 6e 29 0a 20 20 20   expression).   
3856b 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
3856c 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72  ** Return an err
3856d 6f 72 20 69 6e 20 65 69 74 68 65 72 20 63 61 73  or in either cas
3856e 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  e..        */.  
3856f 20 20 20 20 20 20 69 66 28 20 70 50 72 65 76 20        if( pPrev 
38570 26 26 20 28 0a 20 20 20 20 20 20 20 20 20 20 20  && (.           
38571 20 28 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52   (eType==FTSQUER
38572 59 5f 4e 45 41 52 20 26 26 20 21 69 73 50 68 72  Y_NEAR && !isPhr
38573 61 73 65 20 26 26 20 70 50 72 65 76 2d 3e 65 54  ase && pPrev->eT
38574 79 70 65 21 3d 46 54 53 51 55 45 52 59 5f 50 48  ype!=FTSQUERY_PH
38575 52 41 53 45 29 0a 20 20 20 20 20 20 20 20 20 7c  RASE).         |
38576 7c 20 28 65 54 79 70 65 21 3d 46 54 53 51 55 45  | (eType!=FTSQUE
38577 52 59 5f 50 48 52 41 53 45 20 26 26 20 69 73 50  RY_PHRASE && isP
38578 68 72 61 73 65 20 26 26 20 70 50 72 65 76 2d 3e  hrase && pPrev->
38579 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f  eType==FTSQUERY_
3857a 4e 45 41 52 29 0a 20 20 20 20 20 20 20 20 29 29  NEAR).        ))
3857b 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
3857c 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28  te3Fts3ExprFree(
3857d 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63  p);.          rc
3857e 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
3857f 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
38580 65 78 70 72 70 61 72 73 65 5f 6f 75 74 3b 0a 20  exprparse_out;. 
38581 20 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20         }.  .    
38582 20 20 20 20 69 66 28 20 69 73 50 68 72 61 73 65      if( isPhrase
38583 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
38584 28 20 70 52 65 74 20 29 7b 0a 20 20 20 20 20 20  ( pRet ){.      
38585 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
38586 72 65 76 20 26 26 20 70 50 72 65 76 2d 3e 70 4c  rev && pPrev->pL
38587 65 66 74 20 26 26 20 70 50 72 65 76 2d 3e 70 52  eft && pPrev->pR
38588 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ight==0 );.     
38589 20 20 20 20 20 20 20 70 50 72 65 76 2d 3e 70 52         pPrev->pR
3858a 69 67 68 74 20 3d 20 70 3b 0a 20 20 20 20 20 20  ight = p;.      
3858b 20 20 20 20 20 20 70 2d 3e 70 50 61 72 65 6e 74        p->pParent
3858c 20 3d 20 70 50 72 65 76 3b 0a 20 20 20 20 20 20   = pPrev;.      
3858d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3858e 20 20 20 20 20 20 20 70 52 65 74 20 3d 20 70 3b         pRet = p;
3858f 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
38590 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
38591 20 20 20 20 20 20 69 6e 73 65 72 74 42 69 6e 61        insertBina
38592 72 79 4f 70 65 72 61 74 6f 72 28 26 70 52 65 74  ryOperator(&pRet
38593 2c 20 70 50 72 65 76 2c 20 70 29 3b 0a 20 20 20  , pPrev, p);.   
38594 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
38595 73 52 65 71 75 69 72 65 50 68 72 61 73 65 20 3d  sRequirePhrase =
38596 20 21 69 73 50 68 72 61 73 65 3b 0a 20 20 20 20   !isPhrase;.    
38597 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74    }.      assert
38598 28 20 6e 42 79 74 65 3e 30 20 29 3b 0a 20 20 20  ( nByte>0 );.   
38599 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 72   }.    assert( r
3859a 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c!=SQLITE_OK || 
3859b 28 6e 42 79 74 65 3e 30 20 26 26 20 6e 42 79 74  (nByte>0 && nByt
3859c 65 3c 3d 6e 49 6e 29 20 29 3b 0a 20 20 20 20 6e  e<=nIn) );.    n
3859d 49 6e 20 2d 3d 20 6e 42 79 74 65 3b 0a 20 20 20  In -= nByte;.   
3859e 20 7a 49 6e 20 2b 3d 20 6e 42 79 74 65 3b 0a 20   zIn += nByte;. 
3859f 20 20 20 70 50 72 65 76 20 3d 20 70 3b 0a 20 20     pPrev = p;.  
385a0 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  }..  if( rc==SQL
385a1 49 54 45 5f 44 4f 4e 45 20 26 26 20 70 52 65 74  ITE_DONE && pRet
385a2 20 26 26 20 69 73 52 65 71 75 69 72 65 50 68 72   && isRequirePhr
385a3 61 73 65 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ase ){.    rc = 
385a4 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
385a5 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  }..  if( rc==SQL
385a6 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20  ITE_DONE ){.    
385a7 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
385a8 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33      if( !sqlite3
385a9 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72  _fts3_enable_par
385aa 65 6e 74 68 65 73 65 73 20 26 26 20 70 4e 6f 74  entheses && pNot
385ab 42 72 61 6e 63 68 20 29 7b 0a 20 20 20 20 20 20  Branch ){.      
385ac 69 66 28 20 21 70 52 65 74 20 29 7b 0a 20 20 20  if( !pRet ){.   
385ad 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
385ae 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 65  _ERROR;.      }e
385af 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 46 74 73  lse{.        Fts
385b0 33 45 78 70 72 20 2a 70 49 74 65 72 20 3d 20 70  3Expr *pIter = p
385b1 4e 6f 74 42 72 61 6e 63 68 3b 0a 20 20 20 20 20  NotBranch;.     
385b2 20 20 20 77 68 69 6c 65 28 20 70 49 74 65 72 2d     while( pIter-
385b3 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20  >pLeft ){.      
385b4 20 20 20 20 70 49 74 65 72 20 3d 20 70 49 74 65      pIter = pIte
385b5 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20  r->pLeft;.      
385b6 20 20 7d 0a 20 20 20 20 20 20 20 20 70 49 74 65    }.        pIte
385b7 72 2d 3e 70 4c 65 66 74 20 3d 20 70 52 65 74 3b  r->pLeft = pRet;
385b8 0a 20 20 20 20 20 20 20 20 70 52 65 74 20 3d 20  .        pRet = 
385b9 70 4e 6f 74 42 72 61 6e 63 68 3b 0a 20 20 20 20  pNotBranch;.    
385ba 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
385bb 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 6e 20  *pnConsumed = n 
385bc 2d 20 6e 49 6e 3b 0a 0a 65 78 70 72 70 61 72 73  - nIn;..exprpars
385bd 65 5f 6f 75 74 3a 0a 20 20 69 66 28 20 72 63 21  e_out:.  if( rc!
385be 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
385bf 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70    sqlite3Fts3Exp
385c0 72 46 72 65 65 28 70 52 65 74 29 3b 0a 20 20 20  rFree(pRet);.   
385c1 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
385c2 46 72 65 65 28 70 4e 6f 74 42 72 61 6e 63 68 29  Free(pNotBranch)
385c3 3b 0a 20 20 20 20 70 52 65 74 20 3d 20 30 3b 0a  ;.    pRet = 0;.
385c4 20 20 7d 0a 20 20 2a 70 70 45 78 70 72 20 3d 20    }.  *ppExpr = 
385c5 70 52 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 72  pRet;.  return r
385c6 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61  c;.}../*.** Para
385c7 6d 65 74 65 72 73 20 7a 20 61 6e 64 20 6e 20 63  meters z and n c
385c8 6f 6e 74 61 69 6e 20 61 20 70 6f 69 6e 74 65 72  ontain a pointer
385c9 20 74 6f 20 61 6e 64 20 6c 65 6e 67 74 68 20 6f   to and length o
385ca 66 20 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61  f a buffer conta
385cb 69 6e 69 6e 67 0a 2a 2a 20 61 6e 20 66 74 73 33  ining.** an fts3
385cc 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69 6f   query expressio
385cd 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  n, respectively.
385ce 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   This function a
385cf 74 74 65 6d 70 74 73 20 74 6f 20 70 61 72 73 65  ttempts to parse
385d0 20 74 68 65 0a 2a 2a 20 71 75 65 72 79 20 65 78   the.** query ex
385d1 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 63 72 65  pression and cre
385d2 61 74 65 20 61 20 74 72 65 65 20 6f 66 20 46 74  ate a tree of Ft
385d3 73 33 45 78 70 72 20 73 74 72 75 63 74 75 72 65  s3Expr structure
385d4 73 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74  s representing t
385d5 68 65 0a 2a 2a 20 70 61 72 73 65 64 20 65 78 70  he.** parsed exp
385d6 72 65 73 73 69 6f 6e 2e 20 49 66 20 73 75 63 63  ression. If succ
385d7 65 73 73 66 75 6c 2c 20 2a 70 70 45 78 70 72 20  essful, *ppExpr 
385d8 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  is set to point 
385d9 74 6f 20 74 68 65 20 68 65 61 64 0a 2a 2a 20 6f  to the head.** o
385da 66 20 74 68 65 20 70 61 72 73 65 64 20 65 78 70  f the parsed exp
385db 72 65 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64  ression tree and
385dc 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
385dd 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72  turned. If an er
385de 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 65  ror.** occurs, e
385df 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4e 4f 4d  ither SQLITE_NOM
385e0 45 4d 20 28 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72  EM (out-of-memor
385e1 79 20 65 72 72 6f 72 29 20 6f 72 20 53 51 4c 49  y error) or SQLI
385e2 54 45 5f 45 52 52 4f 52 20 28 70 61 72 73 65 0a  TE_ERROR (parse.
385e3 2a 2a 20 65 72 72 6f 72 29 20 69 73 20 72 65 74  ** error) is ret
385e4 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 45 78 70  urned and *ppExp
385e5 72 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a  r is set to 0..*
385e6 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65  *.** If paramete
385e7 72 20 6e 20 69 73 20 61 20 6e 65 67 61 74 69 76  r n is a negativ
385e8 65 20 6e 75 6d 62 65 72 2c 20 74 68 65 6e 20 7a  e number, then z
385e9 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 70   is assumed to p
385ea 6f 69 6e 74 20 74 6f 20 61 0a 2a 2a 20 6e 75 6c  oint to a.** nul
385eb 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69  -terminated stri
385ec 6e 67 20 61 6e 64 20 74 68 65 20 6c 65 6e 67 74  ng and the lengt
385ed 68 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20  h is determined 
385ee 75 73 69 6e 67 20 73 74 72 6c 65 6e 28 29 2e 0a  using strlen()..
385ef 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
385f0 70 61 72 61 6d 65 74 65 72 2c 20 70 54 6f 6b 65  parameter, pToke
385f1 6e 69 7a 65 72 2c 20 69 73 20 70 61 73 73 65 64  nizer, is passed
385f2 20 74 68 65 20 66 74 73 33 20 74 6f 6b 65 6e 69   the fts3 tokeni
385f3 7a 65 72 20 6d 6f 64 75 6c 65 20 74 6f 0a 2a 2a  zer module to.**
385f4 20 75 73 65 20 74 6f 20 6e 6f 72 6d 61 6c 69 7a   use to normaliz
385f5 65 20 71 75 65 72 79 20 74 6f 6b 65 6e 73 20 77  e query tokens w
385f6 68 69 6c 65 20 70 61 72 73 69 6e 67 20 74 68 65  hile parsing the
385f7 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 54 68 65   expression. The
385f8 20 61 7a 43 6f 6c 5b 5d 0a 2a 2a 20 61 72 72 61   azCol[].** arra
385f9 79 2c 20 77 68 69 63 68 20 69 73 20 61 73 73 75  y, which is assu
385fa 6d 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 6e  med to contain n
385fb 43 6f 6c 20 65 6e 74 72 69 65 73 2c 20 73 68 6f  Col entries, sho
385fc 75 6c 64 20 63 6f 6e 74 61 69 6e 20 74 68 65 20  uld contain the 
385fd 6e 61 6d 65 73 0a 2a 2a 20 6f 66 20 65 61 63 68  names.** of each
385fe 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 74   column in the t
385ff 61 72 67 65 74 20 66 74 73 33 20 74 61 62 6c 65  arget fts3 table
38600 2c 20 69 6e 20 6f 72 64 65 72 20 66 72 6f 6d 20  , in order from 
38601 6c 65 66 74 20 74 6f 20 72 69 67 68 74 2e 20 0a  left to right. .
38602 2a 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  ** Column names 
38603 6d 75 73 74 20 62 65 20 6e 75 6c 2d 74 65 72 6d  must be nul-term
38604 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 2e 0a  inated strings..
38605 2a 2a 0a 2a 2a 20 54 68 65 20 69 44 65 66 61 75  **.** The iDefau
38606 6c 74 43 6f 6c 20 70 61 72 61 6d 65 74 65 72 20  ltCol parameter 
38607 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64  should be passed
38608 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
38609 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 0a 2a  e table column.*
3860a 2a 20 74 68 61 74 20 61 70 70 65 61 72 73 20 6f  * that appears o
3860b 6e 20 74 68 65 20 6c 65 66 74 2d 68 61 6e 64 2d  n the left-hand-
3860c 73 69 64 65 20 6f 66 20 74 68 65 20 4d 41 54 43  side of the MATC
3860d 48 20 6f 70 65 72 61 74 6f 72 20 28 74 68 65 20  H operator (the 
3860e 64 65 66 61 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d  default.** colum
3860f 6e 20 74 6f 20 6d 61 74 63 68 20 61 67 61 69 6e  n to match again
38610 73 74 20 66 6f 72 20 74 6f 6b 65 6e 73 20 66 6f  st for tokens fo
38611 72 20 77 68 69 63 68 20 61 20 63 6f 6c 75 6d 6e  r which a column
38612 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 65 78 70   name is not exp
38613 6c 69 63 69 74 6c 79 0a 2a 2a 20 73 70 65 63 69  licitly.** speci
38614 66 69 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  fied as part of 
38615 74 68 65 20 71 75 65 72 79 20 73 74 72 69 6e 67  the query string
38616 29 2c 20 6f 72 20 2d 31 20 69 66 20 74 6f 6b 65  ), or -1 if toke
38617 6e 73 20 6d 61 79 20 62 79 20 64 65 66 61 75 6c  ns may by defaul
38618 74 0a 2a 2a 20 6d 61 74 63 68 20 61 6e 79 20 74  t.** match any t
38619 61 62 6c 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a  able column..*/.
3861a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
3861b 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 45 78  nt sqlite3Fts3Ex
3861c 70 72 50 61 72 73 65 28 0a 20 20 73 71 6c 69 74  prParse(.  sqlit
3861d 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
3861e 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20 20 2f  okenizer,      /
3861f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75  * Tokenizer modu
38620 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61  le */.  char **a
38621 7a 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20  zCol,           
38622 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
38623 72 72 61 79 20 6f 66 20 63 6f 6c 75 6d 6e 20 6e  rray of column n
38624 61 6d 65 73 20 66 6f 72 20 66 74 73 33 20 74 61  ames for fts3 ta
38625 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  ble */.  int nCo
38626 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
38627 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38628 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
38629 73 20 69 6e 20 61 7a 43 6f 6c 5b 5d 20 2a 2f 0a  s in azCol[] */.
3862a 20 20 69 6e 74 20 69 44 65 66 61 75 6c 74 43 6f    int iDefaultCo
3862b 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
3862c 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74        /* Default
3862d 20 63 6f 6c 75 6d 6e 20 74 6f 20 71 75 65 72 79   column to query
3862e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
3862f 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20 20 20 20   *z, int n,     
38630 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
38631 74 20 6f 66 20 4d 41 54 43 48 20 71 75 65 72 79  t of MATCH query
38632 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a   */.  Fts3Expr *
38633 2a 70 70 45 78 70 72 20 20 20 20 20 20 20 20 20  *ppExpr         
38634 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
38635 3a 20 50 61 72 73 65 64 20 71 75 65 72 79 20 73  : Parsed query s
38636 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20  tructure */.){. 
38637 20 69 6e 74 20 6e 50 61 72 73 65 64 3b 0a 20 20   int nParsed;.  
38638 69 6e 74 20 72 63 3b 0a 20 20 50 61 72 73 65 43  int rc;.  ParseC
38639 6f 6e 74 65 78 74 20 73 50 61 72 73 65 3b 0a 20  ontext sParse;. 
3863a 20 73 50 61 72 73 65 2e 70 54 6f 6b 65 6e 69 7a   sParse.pTokeniz
3863b 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b  er = pTokenizer;
3863c 0a 20 20 73 50 61 72 73 65 2e 61 7a 43 6f 6c 20  .  sParse.azCol 
3863d 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  = (const char **
3863e 29 61 7a 43 6f 6c 3b 0a 20 20 73 50 61 72 73 65  )azCol;.  sParse
3863f 2e 6e 43 6f 6c 20 3d 20 6e 43 6f 6c 3b 0a 20 20  .nCol = nCol;.  
38640 73 50 61 72 73 65 2e 69 44 65 66 61 75 6c 74 43  sParse.iDefaultC
38641 6f 6c 20 3d 20 69 44 65 66 61 75 6c 74 43 6f 6c  ol = iDefaultCol
38642 3b 0a 20 20 73 50 61 72 73 65 2e 6e 4e 65 73 74  ;.  sParse.nNest
38643 20 3d 20 30 3b 0a 20 20 69 66 28 20 7a 3d 3d 30   = 0;.  if( z==0
38644 20 29 7b 0a 20 20 20 20 2a 70 70 45 78 70 72 20   ){.    *ppExpr 
38645 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 0;.    return 
38646 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
38647 20 69 66 28 20 6e 3c 30 20 29 7b 0a 20 20 20 20   if( n<0 ){.    
38648 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20  n = strlen(z);. 
38649 20 7d 0a 20 20 72 63 20 3d 20 66 74 73 33 45 78   }.  rc = fts3Ex
3864a 70 72 50 61 72 73 65 28 26 73 50 61 72 73 65 2c  prParse(&sParse,
3864b 20 7a 2c 20 6e 2c 20 70 70 45 78 70 72 2c 20 26   z, n, ppExpr, &
3864c 6e 50 61 72 73 65 64 29 3b 0a 0a 20 20 2f 2a 20  nParsed);..  /* 
3864d 43 68 65 63 6b 20 66 6f 72 20 6d 69 73 6d 61 74  Check for mismat
3864e 63 68 65 64 20 70 61 72 65 6e 74 68 65 73 69 73  ched parenthesis
3864f 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
38650 4c 49 54 45 5f 4f 4b 20 26 26 20 73 50 61 72 73  LITE_OK && sPars
38651 65 2e 6e 4e 65 73 74 20 29 7b 0a 20 20 20 20 72  e.nNest ){.    r
38652 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
38653 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  ;.    sqlite3Fts
38654 33 45 78 70 72 46 72 65 65 28 2a 70 70 45 78 70  3ExprFree(*ppExp
38655 72 29 3b 0a 20 20 20 20 2a 70 70 45 78 70 72 20  r);.    *ppExpr 
38656 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  = 0;.  }..  retu
38657 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
38658 46 72 65 65 20 61 20 70 61 72 73 65 64 20 66 74  Free a parsed ft
38659 73 33 20 71 75 65 72 79 20 65 78 70 72 65 73 73  s3 query express
3865a 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  ion allocated by
3865b 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
3865c 50 61 72 73 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49  Parse()..*/.SQLI
3865d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
3865e 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 46  sqlite3Fts3ExprF
3865f 72 65 65 28 46 74 73 33 45 78 70 72 20 2a 70 29  ree(Fts3Expr *p)
38660 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  {.  if( p ){.   
38661 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
38662 46 72 65 65 28 70 2d 3e 70 4c 65 66 74 29 3b 0a  Free(p->pLeft);.
38663 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 45      sqlite3Fts3E
38664 78 70 72 46 72 65 65 28 70 2d 3e 70 52 69 67 68  xprFree(p->pRigh
38665 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  t);.    sqlite3_
38666 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  free(p);.  }.}..
38667 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
38668 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38669 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3866a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3866b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
3866c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3866d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3866e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3866f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 45  ***********.** E
38671 76 65 72 79 74 68 69 6e 67 20 61 66 74 65 72 20  verything after 
38672 74 68 69 73 20 70 6f 69 6e 74 20 69 73 20 6a 75  this point is ju
38673 73 74 20 74 65 73 74 20 63 6f 64 65 2e 0a 2a 2f  st test code..*/
38674 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
38675 54 45 53 54 0a 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e  TEST.../*.** Fun
38676 63 74 69 6f 6e 20 74 6f 20 71 75 65 72 79 20 74  ction to query t
38677 68 65 20 68 61 73 68 2d 74 61 62 6c 65 20 6f 66  he hash-table of
38678 20 74 6f 6b 65 6e 69 7a 65 72 73 20 28 73 65 65   tokenizers (see
38679 20 52 45 41 44 4d 45 2e 74 6f 6b 65 6e 69 7a 65   README.tokenize
3867a 72 73 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  rs)..*/.static i
3867b 6e 74 20 71 75 65 72 79 54 65 73 74 54 6f 6b 65  nt queryTestToke
3867c 6e 69 7a 65 72 28 0a 20 20 73 71 6c 69 74 65 33  nizer(.  sqlite3
3867d 20 2a 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63   *db, .  const c
3867e 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 0a 20 20  har *zName,  .  
3867f 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
38680 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a  kenizer_module *
38681 2a 70 70 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b  *pp.){.  int rc;
38682 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
38683 2a 70 53 74 6d 74 3b 0a 20 20 63 6f 6e 73 74 20  *pStmt;.  const 
38684 63 68 61 72 20 7a 53 71 6c 5b 5d 20 3d 20 22 53  char zSql[] = "S
38685 45 4c 45 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e  ELECT fts3_token
38686 69 7a 65 72 28 3f 29 22 3b 0a 0a 20 20 2a 70 70  izer(?)";..  *pp
38687 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c   = 0;.  rc = sql
38688 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
38689 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70  db, zSql, -1, &p
3868a 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28 20  Stmt, 0);.  if( 
3868b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
3868c 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
3868d 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 62    }..  sqlite3_b
3868e 69 6e 64 5f 74 65 78 74 28 70 53 74 6d 74 2c 20  ind_text(pStmt, 
3868f 31 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51  1, zName, -1, SQ
38690 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
38691 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d  if( SQLITE_ROW==
38692 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74  sqlite3_step(pSt
38693 6d 74 29 20 29 7b 0a 20 20 20 20 69 66 28 20 73  mt) ){.    if( s
38694 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
38695 70 65 28 70 53 74 6d 74 2c 20 30 29 3d 3d 53 51  pe(pStmt, 0)==SQ
38696 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20  LITE_BLOB ){.   
38697 20 20 20 6d 65 6d 63 70 79 28 70 70 2c 20 73 71     memcpy(pp, sq
38698 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
38699 62 28 70 53 74 6d 74 2c 20 30 29 2c 20 73 69 7a  b(pStmt, 0), siz
3869a 65 6f 66 28 2a 70 70 29 29 3b 0a 20 20 20 20 7d  eof(*pp));.    }
3869b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 73  .  }..  return s
3869c 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
3869d 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pStmt);.}../*.**
3869e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
3869f 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 65  s part of the te
386a0 73 74 20 69 6e 74 65 72 66 61 63 65 20 66 6f 72  st interface for
386a1 20 74 68 65 20 71 75 65 72 79 20 70 61 72 73 65   the query parse
386a2 72 2e 20 49 74 0a 2a 2a 20 77 72 69 74 65 73 20  r. It.** writes 
386a3 61 20 74 65 78 74 20 72 65 70 72 65 73 65 6e 74  a text represent
386a4 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 65  ation of the que
386a5 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45  ry expression pE
386a6 78 70 72 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20  xpr into the.** 
386a7 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74  buffer pointed t
386a8 6f 20 62 79 20 61 72 67 75 6d 65 6e 74 20 7a 42  o by argument zB
386a9 75 66 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65  uf. It is assume
386aa 64 20 74 68 61 74 20 7a 42 75 66 20 69 73 20 6c  d that zBuf is l
386ab 61 72 67 65 20 0a 2a 2a 20 65 6e 6f 75 67 68 20  arge .** enough 
386ac 74 6f 20 73 74 6f 72 65 20 74 68 65 20 72 65 71  to store the req
386ad 75 69 72 65 64 20 74 65 78 74 20 72 65 70 72 65  uired text repre
386ae 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  sentation..*/.st
386af 61 74 69 63 20 76 6f 69 64 20 65 78 70 72 54 6f  atic void exprTo
386b0 53 74 72 69 6e 67 28 46 74 73 33 45 78 70 72 20  String(Fts3Expr 
386b1 2a 70 45 78 70 72 2c 20 63 68 61 72 20 2a 7a 42  *pExpr, char *zB
386b2 75 66 29 7b 0a 20 20 73 77 69 74 63 68 28 20 70  uf){.  switch( p
386b3 45 78 70 72 2d 3e 65 54 79 70 65 20 29 7b 0a 20  Expr->eType ){. 
386b4 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52 59     case FTSQUERY
386b5 5f 50 48 52 41 53 45 3a 20 7b 0a 20 20 20 20 20  _PHRASE: {.     
386b6 20 46 74 73 33 50 68 72 61 73 65 20 2a 70 50 68   Fts3Phrase *pPh
386b7 72 61 73 65 20 3d 20 70 45 78 70 72 2d 3e 70 50  rase = pExpr->pP
386b8 68 72 61 73 65 3b 0a 20 20 20 20 20 20 69 6e 74  hrase;.      int
386b9 20 69 3b 0a 20 20 20 20 20 20 7a 42 75 66 20 2b   i;.      zBuf +
386ba 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20  = sprintf(zBuf, 
386bb 22 50 48 52 41 53 45 20 25 64 20 25 64 22 2c 20  "PHRASE %d %d", 
386bc 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e  pPhrase->iColumn
386bd 2c 20 70 50 68 72 61 73 65 2d 3e 69 73 4e 6f 74  , pPhrase->isNot
386be 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  );.      for(i=0
386bf 3b 20 69 3c 70 50 68 72 61 73 65 2d 3e 6e 54 6f  ; i<pPhrase->nTo
386c0 6b 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  ken; i++){.     
386c1 20 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e     zBuf += sprin
386c2 74 66 28 7a 42 75 66 2c 22 20 25 2e 2a 73 22 2c  tf(zBuf," %.*s",
386c3 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
386c4 69 5d 2e 6e 2c 70 50 68 72 61 73 65 2d 3e 61 54  i].n,pPhrase->aT
386c5 6f 6b 65 6e 5b 69 5d 2e 7a 29 3b 0a 20 20 20 20  oken[i].z);.    
386c6 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69      zBuf += spri
386c7 6e 74 66 28 7a 42 75 66 2c 22 25 73 22 2c 20 28  ntf(zBuf,"%s", (
386c8 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
386c9 69 5d 2e 69 73 50 72 65 66 69 78 3f 22 2b 22 3a  i].isPrefix?"+":
386ca 22 22 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ""));.      }.  
386cb 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
386cc 7d 0a 0a 20 20 20 20 63 61 73 65 20 46 54 53 51  }..    case FTSQ
386cd 55 45 52 59 5f 4e 45 41 52 3a 0a 20 20 20 20 20  UERY_NEAR:.     
386ce 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66   zBuf += sprintf
386cf 28 7a 42 75 66 2c 20 22 4e 45 41 52 2f 25 64 20  (zBuf, "NEAR/%d 
386d0 22 2c 20 70 45 78 70 72 2d 3e 6e 4e 65 61 72 29  ", pExpr->nNear)
386d1 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
386d2 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52 59     case FTSQUERY
386d3 5f 4e 4f 54 3a 0a 20 20 20 20 20 20 7a 42 75 66  _NOT:.      zBuf
386d4 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66   += sprintf(zBuf
386d5 2c 20 22 4e 4f 54 20 22 29 3b 0a 20 20 20 20 20  , "NOT ");.     
386d6 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
386d7 20 46 54 53 51 55 45 52 59 5f 41 4e 44 3a 0a 20   FTSQUERY_AND:. 
386d8 20 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70 72       zBuf += spr
386d9 69 6e 74 66 28 7a 42 75 66 2c 20 22 41 4e 44 20  intf(zBuf, "AND 
386da 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ");.      break;
386db 0a 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45  .    case FTSQUE
386dc 52 59 5f 4f 52 3a 0a 20 20 20 20 20 20 7a 42 75  RY_OR:.      zBu
386dd 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75  f += sprintf(zBu
386de 66 2c 20 22 4f 52 20 22 29 3b 0a 20 20 20 20 20  f, "OR ");.     
386df 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 7a   break;.  }..  z
386e0 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a  Buf += sprintf(z
386e1 42 75 66 2c 20 22 7b 22 29 3b 0a 20 20 65 78 70  Buf, "{");.  exp
386e2 72 54 6f 53 74 72 69 6e 67 28 70 45 78 70 72 2d  rToString(pExpr-
386e3 3e 70 4c 65 66 74 2c 20 7a 42 75 66 29 3b 0a 20  >pLeft, zBuf);. 
386e4 20 7a 42 75 66 20 2b 3d 20 73 74 72 6c 65 6e 28   zBuf += strlen(
386e5 7a 42 75 66 29 3b 0a 20 20 7a 42 75 66 20 2b 3d  zBuf);.  zBuf +=
386e6 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22   sprintf(zBuf, "
386e7 7d 20 22 29 3b 0a 0a 20 20 7a 42 75 66 20 2b 3d  } ");..  zBuf +=
386e8 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22   sprintf(zBuf, "
386e9 7b 22 29 3b 0a 20 20 65 78 70 72 54 6f 53 74 72  {");.  exprToStr
386ea 69 6e 67 28 70 45 78 70 72 2d 3e 70 52 69 67 68  ing(pExpr->pRigh
386eb 74 2c 20 7a 42 75 66 29 3b 0a 20 20 7a 42 75 66  t, zBuf);.  zBuf
386ec 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 42 75 66 29   += strlen(zBuf)
386ed 3b 0a 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69  ;.  zBuf += spri
386ee 6e 74 66 28 7a 42 75 66 2c 20 22 7d 22 29 3b 0a  ntf(zBuf, "}");.
386ef 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73  }../*.** This is
386f0 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
386f1 69 6f 6e 20 6f 66 20 61 20 73 63 61 6c 61 72 20  ion of a scalar 
386f2 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 75 73 65  SQL function use
386f3 64 20 74 6f 20 74 65 73 74 20 74 68 65 20 0a 2a  d to test the .*
386f4 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 70 61 72  * expression par
386f5 73 65 72 2e 20 49 74 20 73 68 6f 75 6c 64 20 62  ser. It should b
386f6 65 20 63 61 6c 6c 65 64 20 61 73 20 66 6f 6c 6c  e called as foll
386f7 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 66 74 73  ows:.**.**   fts
386f8 33 5f 65 78 70 72 74 65 73 74 28 3c 74 6f 6b 65  3_exprtest(<toke
386f9 6e 69 7a 65 72 3e 2c 20 3c 65 78 70 72 3e 2c 20  nizer>, <expr>, 
386fa 3c 63 6f 6c 75 6d 6e 20 31 3e 2c 20 2e 2e 2e 29  <column 1>, ...)
386fb 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  ;.**.** The firs
386fc 74 20 61 72 67 75 6d 65 6e 74 2c 20 3c 74 6f 6b  t argument, <tok
386fd 65 6e 69 7a 65 72 3e 2c 20 69 73 20 74 68 65 20  enizer>, is the 
386fe 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 74 73 33  name of the fts3
386ff 20 74 6f 6b 65 6e 69 7a 65 72 20 75 73 65 64 0a   tokenizer used.
38700 2a 2a 20 74 6f 20 70 61 72 73 65 20 74 68 65 20  ** to parse the 
38701 71 75 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e  query expression
38702 20 28 73 65 65 20 52 45 41 44 4d 45 2e 74 6f 6b   (see README.tok
38703 65 6e 69 7a 65 72 73 29 2e 20 54 68 65 20 73 65  enizers). The se
38704 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  cond argument.**
38705 20 69 73 20 74 68 65 20 71 75 65 72 79 20 65 78   is the query ex
38706 70 72 65 73 73 69 6f 6e 20 74 6f 20 70 61 72 73  pression to pars
38707 65 2e 20 45 61 63 68 20 73 75 62 73 65 71 75 65  e. Each subseque
38708 6e 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  nt argument is t
38709 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 20  he name.** of a 
3870a 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 66 74  column of the ft
3870b 73 33 20 74 61 62 6c 65 20 74 68 61 74 20 74 68  s3 table that th
3870c 65 20 71 75 65 72 79 20 65 78 70 72 65 73 73 69  e query expressi
3870d 6f 6e 20 6d 61 79 20 72 65 66 65 72 20 74 6f 2e  on may refer to.
3870e 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a  .** For example:
3870f 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20  .**.**   SELECT 
38710 66 74 73 33 5f 65 78 70 72 74 65 73 74 28 27 73  fts3_exprtest('s
38711 69 6d 70 6c 65 27 2c 20 27 42 69 6c 6c 20 63 6f  imple', 'Bill co
38712 6c 32 3a 42 6c 6f 67 67 73 27 2c 20 27 63 6f 6c  l2:Bloggs', 'col
38713 31 27 2c 20 27 63 6f 6c 32 27 29 3b 0a 2a 2f 0a  1', 'col2');.*/.
38714 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33  static void fts3
38715 45 78 70 72 54 65 73 74 28 0a 20 20 73 71 6c 69  ExprTest(.  sqli
38716 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
38717 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
38718 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
38719 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 73 71  e **argv.){.  sq
3871a 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
3871b 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 20 2a 70 4d  module const *pM
3871c 6f 64 75 6c 65 20 3d 20 30 3b 0a 20 20 73 71 6c  odule = 0;.  sql
3871d 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
3871e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 30 3b 0a  pTokenizer = 0;.
3871f 20 20 69 6e 74 20 72 63 3b 0a 20 20 63 68 61 72    int rc;.  char
38720 20 2a 2a 61 7a 43 6f 6c 20 3d 20 30 3b 0a 20 20   **azCol = 0;.  
38721 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 78 70  const char *zExp
38722 72 3b 0a 20 20 69 6e 74 20 6e 45 78 70 72 3b 0a  r;.  int nExpr;.
38723 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 69 6e    int nCol;.  in
38724 74 20 69 69 3b 0a 20 20 46 74 73 33 45 78 70 72  t ii;.  Fts3Expr
38725 20 2a 70 45 78 70 72 3b 0a 20 20 73 71 6c 69 74   *pExpr;.  sqlit
38726 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
38727 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
38728 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20  le(context);..  
38729 69 66 28 20 61 72 67 63 3c 33 20 29 7b 0a 20 20  if( argc<3 ){.  
3872a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
3872b 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
3872c 0a 20 20 20 20 20 20 20 20 22 55 73 61 67 65 3a  .        "Usage:
3872d 20 66 74 73 33 5f 65 78 70 72 74 65 73 74 28 74   fts3_exprtest(t
3872e 6f 6b 65 6e 69 7a 65 72 2c 20 65 78 70 72 2c 20  okenizer, expr, 
3872f 63 6f 6c 31 2c 20 2e 2e 2e 22 2c 20 2d 31 0a 20  col1, ...", -1. 
38730 20 20 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e     );.    return
38731 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 71 75  ;.  }..  rc = qu
38732 65 72 79 54 65 73 74 54 6f 6b 65 6e 69 7a 65 72  eryTestTokenizer
38733 28 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20  (db,.           
38734 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
38735 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c  const char *)sql
38736 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
38737 61 72 67 76 5b 30 5d 29 2c 20 26 70 4d 6f 64 75  argv[0]), &pModu
38738 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  le);.  if( rc==S
38739 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  QLITE_NOMEM ){. 
3873a 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
3873b 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f  t_error_nomem(co
3873c 6e 74 65 78 74 29 3b 0a 20 20 20 20 67 6f 74 6f  ntext);.    goto
3873d 20 65 78 70 72 74 65 73 74 5f 6f 75 74 3b 0a 20   exprtest_out;. 
3873e 20 7d 65 6c 73 65 20 69 66 28 20 21 70 4d 6f 64   }else if( !pMod
3873f 75 6c 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ule ){.    sqlit
38740 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
38741 63 6f 6e 74 65 78 74 2c 20 22 4e 6f 20 73 75 63  context, "No suc
38742 68 20 74 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75  h tokenizer modu
38743 6c 65 22 2c 20 2d 31 29 3b 0a 20 20 20 20 67 6f  le", -1);.    go
38744 74 6f 20 65 78 70 72 74 65 73 74 5f 6f 75 74 3b  to exprtest_out;
38745 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 70 4d 6f  .  }..  rc = pMo
38746 64 75 6c 65 2d 3e 78 43 72 65 61 74 65 28 30 2c  dule->xCreate(0,
38747 20 30 2c 20 26 70 54 6f 6b 65 6e 69 7a 65 72 29   0, &pTokenizer)
38748 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ;.  assert( rc==
38749 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20  SQLITE_NOMEM || 
3874a 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
3874b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
3874c 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73  E_NOMEM ){.    s
3874d 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
3874e 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78  ror_nomem(contex
3874f 74 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 70  t);.    goto exp
38750 72 74 65 73 74 5f 6f 75 74 3b 0a 20 20 7d 0a 20  rtest_out;.  }. 
38751 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f   pTokenizer->pMo
38752 64 75 6c 65 20 3d 20 70 4d 6f 64 75 6c 65 3b 0a  dule = pModule;.
38753 0a 20 20 7a 45 78 70 72 20 3d 20 28 63 6f 6e 73  .  zExpr = (cons
38754 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
38755 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
38756 5b 31 5d 29 3b 0a 20 20 6e 45 78 70 72 20 3d 20  [1]);.  nExpr = 
38757 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
38758 74 65 73 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  tes(argv[1]);.  
38759 6e 43 6f 6c 20 3d 20 61 72 67 63 2d 32 3b 0a 20  nCol = argc-2;. 
3875a 20 61 7a 43 6f 6c 20 3d 20 28 63 68 61 72 20 2a   azCol = (char *
3875b 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
3875c 28 6e 43 6f 6c 2a 73 69 7a 65 6f 66 28 63 68 61  (nCol*sizeof(cha
3875d 72 20 2a 29 29 3b 0a 20 20 69 66 28 20 21 61 7a  r *));.  if( !az
3875e 43 6f 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Col ){.    sqlit
3875f 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
38760 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a  nomem(context);.
38761 20 20 20 20 67 6f 74 6f 20 65 78 70 72 74 65 73      goto exprtes
38762 74 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 66 6f 72  t_out;.  }.  for
38763 28 69 69 3d 30 3b 20 69 69 3c 6e 43 6f 6c 3b 20  (ii=0; ii<nCol; 
38764 69 69 2b 2b 29 7b 0a 20 20 20 20 61 7a 43 6f 6c  ii++){.    azCol
38765 5b 69 69 5d 20 3d 20 28 63 68 61 72 20 2a 29 73  [ii] = (char *)s
38766 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
38767 74 28 61 72 67 76 5b 69 69 2b 32 5d 29 3b 0a 20  t(argv[ii+2]);. 
38768 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   }..  rc = sqlit
38769 65 33 46 74 73 33 45 78 70 72 50 61 72 73 65 28  e3Fts3ExprParse(
3876a 0a 20 20 20 20 20 20 70 54 6f 6b 65 6e 69 7a 65  .      pTokenize
3876b 72 2c 20 61 7a 43 6f 6c 2c 20 6e 43 6f 6c 2c 20  r, azCol, nCol, 
3876c 6e 43 6f 6c 2c 20 7a 45 78 70 72 2c 20 6e 45 78  nCol, zExpr, nEx
3876d 70 72 2c 20 26 70 45 78 70 72 0a 20 20 29 3b 0a  pr, &pExpr.  );.
3876e 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
3876f 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71  _NOMEM ){.    sq
38770 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
38771 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74  or_nomem(context
38772 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 70 72  );.    goto expr
38773 74 65 73 74 5f 6f 75 74 3b 0a 20 20 7d 65 6c 73  test_out;.  }els
38774 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45  e if( rc==SQLITE
38775 5f 4f 4b 20 29 7b 0a 20 20 20 20 63 68 61 72 20  _OK ){.    char 
38776 7a 42 75 66 5b 34 30 39 36 5d 3b 0a 20 20 20 20  zBuf[4096];.    
38777 65 78 70 72 54 6f 53 74 72 69 6e 67 28 70 45 78  exprToString(pEx
38778 70 72 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 73  pr, zBuf);.    s
38779 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
3877a 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66  xt(context, zBuf
3877b 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41  , -1, SQLITE_TRA
3877c 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 73 71 6c  NSIENT);.    sql
3877d 69 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65  ite3Fts3ExprFree
3877e 28 70 45 78 70 72 29 3b 0a 20 20 7d 65 6c 73 65  (pExpr);.  }else
3877f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
38780 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65  sult_error(conte
38781 78 74 2c 20 22 45 72 72 6f 72 20 70 61 72 73 69  xt, "Error parsi
38782 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 22 2c 20  ng expression", 
38783 2d 31 29 3b 0a 20 20 7d 0a 0a 65 78 70 72 74 65  -1);.  }..exprte
38784 73 74 5f 6f 75 74 3a 0a 20 20 69 66 28 20 70 4d  st_out:.  if( pM
38785 6f 64 75 6c 65 20 26 26 20 70 54 6f 6b 65 6e 69  odule && pTokeni
38786 7a 65 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  zer ){.    rc = 
38787 70 4d 6f 64 75 6c 65 2d 3e 78 44 65 73 74 72 6f  pModule->xDestro
38788 79 28 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20  y(pTokenizer);. 
38789 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   }.  sqlite3_fre
3878a 65 28 61 7a 43 6f 6c 29 3b 0a 7d 0a 0a 2f 2a 0a  e(azCol);.}../*.
3878b 2a 2a 20 52 65 67 69 73 74 65 72 20 74 68 65 20  ** Register the 
3878c 71 75 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e  query expression
3878d 20 70 61 72 73 65 72 20 74 65 73 74 20 66 75 6e   parser test fun
3878e 63 74 69 6f 6e 20 66 74 73 33 5f 65 78 70 72 74  ction fts3_exprt
3878f 65 73 74 28 29 20 0a 2a 2a 20 77 69 74 68 20 64  est() .** with d
38790 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
38791 6f 6e 20 64 62 2e 20 0a 2a 2f 0a 53 51 4c 49 54  on db. .*/.SQLIT
38792 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
38793 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 49 6e  qlite3Fts3ExprIn
38794 69 74 54 65 73 74 49 6e 74 65 72 66 61 63 65 28  itTestInterface(
38795 73 71 6c 69 74 65 33 2a 20 64 62 29 7b 0a 20 20  sqlite3* db){.  
38796 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
38797 75 6e 63 74 69 6f 6e 28 0a 20 20 20 20 20 20 64  unction(.      d
38798 62 2c 20 22 66 74 73 33 5f 65 78 70 72 74 65 73  b, "fts3_exprtes
38799 74 22 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55  t", -1, SQLITE_U
3879a 54 46 38 2c 20 30 2c 20 66 74 73 33 45 78 70 72  TF8, 0, fts3Expr
3879b 54 65 73 74 2c 20 30 2c 20 30 0a 20 20 29 3b 0a  Test, 0, 0.  );.
3879c 7d 0a 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66  }..#endif.#endif
3879d 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
3879e 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66  ITE_CORE) || def
3879f 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
387a0 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a  LE_FTS3) */../**
387a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
387a2 20 6f 66 20 66 74 73 33 5f 65 78 70 72 2e 63 20   of fts3_expr.c 
387a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
387a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
387a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
387a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
387a7 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 68 61 73  in file fts3_has
387a8 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h.c ************
387a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
387aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
387ab 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
387ac 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 22.**.** The a
387ad 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
387ae 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
387af 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
387b0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
387b1 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
387b2 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
387b3 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
387b4 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
387b5 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
387b6 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
387b7 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
387b8 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
387b9 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
387ba 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
387bb 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
387bc 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
387bd 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
387be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
387bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
387c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
387c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
387c2 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74  ***.** This is t
387c3 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
387c4 6e 20 6f 66 20 67 65 6e 65 72 69 63 20 68 61 73  n of generic has
387c5 68 2d 74 61 62 6c 65 73 20 75 73 65 64 20 69 6e  h-tables used in
387c6 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 57 65 27 76   SQLite..** We'v
387c7 65 20 6d 6f 64 69 66 69 65 64 20 69 74 20 73 6c  e modified it sl
387c8 69 67 68 74 6c 79 20 74 6f 20 73 65 72 76 65 20  ightly to serve 
387c9 61 73 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65 20  as a standalone 
387ca 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a 20 69 6d  hash table.** im
387cb 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  plementation for
387cc 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 69   the full-text i
387cd 6e 64 65 78 69 6e 67 20 6d 6f 64 75 6c 65 2e 0a  ndexing module..
387ce 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f  */../*.** The co
387cf 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
387d0 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64  is only compiled
387d1 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a   if:.**.**     *
387d2 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65   The FTS3 module
387d3 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20   is being built 
387d4 61 73 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 0a  as an extension.
387d5 2a 2a 20 20 20 20 20 20 20 28 69 6e 20 77 68 69  **       (in whi
387d6 63 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43  ch case SQLITE_C
387d7 4f 52 45 20 69 73 20 6e 6f 74 20 64 65 66 69 6e  ORE is not defin
387d8 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20  ed), or.**.**   
387d9 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64    * The FTS3 mod
387da 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69  ule is being bui
387db 6c 74 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 65  lt into the core
387dc 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c   of.**       SQL
387dd 69 74 65 20 28 69 6e 20 77 68 69 63 68 20 63 61  ite (in which ca
387de 73 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  se SQLITE_ENABLE
387df 5f 46 54 53 33 20 69 73 20 64 65 66 69 6e 65 64  _FTS3 is defined
387e0 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  )..*/.#if !defin
387e1 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20  ed(SQLITE_CORE) 
387e2 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
387e3 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a  E_ENABLE_FTS3)..
387e4 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 61  ../*.** Malloc a
387e5 6e 64 20 46 72 65 65 20 66 75 6e 63 74 69 6f 6e  nd Free function
387e6 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  s.*/.static void
387e7 20 2a 66 74 73 33 48 61 73 68 4d 61 6c 6c 6f 63   *fts3HashMalloc
387e8 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20  (int n){.  void 
387e9 2a 70 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  *p = sqlite3_mal
387ea 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 28 20 70 20  loc(n);.  if( p 
387eb 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c  ){.    memset(p,
387ec 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65   0, n);.  }.  re
387ed 74 75 72 6e 20 70 3b 0a 7d 0a 73 74 61 74 69 63  turn p;.}.static
387ee 20 76 6f 69 64 20 66 74 73 33 48 61 73 68 46 72   void fts3HashFr
387ef 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 73  ee(void *p){.  s
387f0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
387f1 7d 0a 0a 2f 2a 20 54 75 72 6e 20 62 75 6c 6b 20  }../* Turn bulk 
387f2 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 61 20 68 61  memory into a ha
387f3 73 68 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20  sh table object 
387f4 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20  by initializing 
387f5 74 68 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6f 66  the.** fields of
387f6 20 74 68 65 20 48 61 73 68 20 73 74 72 75 63 74   the Hash struct
387f7 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77  ure..**.** "pNew
387f8 22 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  " is a pointer t
387f9 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  o the hash table
387fa 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20 69   that is to be i
387fb 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 6b  nitialized..** k
387fc 65 79 43 6c 61 73 73 20 69 73 20 6f 6e 65 20 6f  eyClass is one o
387fd 66 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20  f the constants 
387fe 0a 2a 2a 20 46 54 53 33 5f 48 41 53 48 5f 42 49  .** FTS3_HASH_BI
387ff 4e 41 52 59 20 6f 72 20 46 54 53 33 5f 48 41 53  NARY or FTS3_HAS
38800 48 5f 53 54 52 49 4e 47 2e 20 20 54 68 65 20 76  H_STRING.  The v
38801 61 6c 75 65 20 6f 66 20 6b 65 79 43 6c 61 73 73  alue of keyClass
38802 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 73 20   .** determines 
38803 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 6b 65 79  what kind of key
38804 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
38805 77 69 6c 6c 20 75 73 65 2e 20 20 22 63 6f 70 79  will use.  "copy
38806 4b 65 79 22 20 69 73 0a 2a 2a 20 74 72 75 65 20  Key" is.** true 
38807 69 66 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  if the hash tabl
38808 65 20 73 68 6f 75 6c 64 20 6d 61 6b 65 20 69 74  e should make it
38809 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f  s own private co
3880a 70 79 20 6f 66 20 6b 65 79 73 20 61 6e 64 0a 2a  py of keys and.*
3880b 2a 20 66 61 6c 73 65 20 69 66 20 69 74 20 73 68  * false if it sh
3880c 6f 75 6c 64 20 6a 75 73 74 20 75 73 65 20 74 68  ould just use th
3880d 65 20 73 75 70 70 6c 69 65 64 20 70 6f 69 6e 74  e supplied point
3880e 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
3880f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
38810 65 33 46 74 73 33 48 61 73 68 49 6e 69 74 28 66  e3Fts3HashInit(f
38811 74 73 33 48 61 73 68 20 2a 70 4e 65 77 2c 20 69  ts3Hash *pNew, i
38812 6e 74 20 6b 65 79 43 6c 61 73 73 2c 20 69 6e 74  nt keyClass, int
38813 20 63 6f 70 79 4b 65 79 29 7b 0a 20 20 61 73 73   copyKey){.  ass
38814 65 72 74 28 20 70 4e 65 77 21 3d 30 20 29 3b 0a  ert( pNew!=0 );.
38815 20 20 61 73 73 65 72 74 28 20 6b 65 79 43 6c 61    assert( keyCla
38816 73 73 3e 3d 46 54 53 33 5f 48 41 53 48 5f 53 54  ss>=FTS3_HASH_ST
38817 52 49 4e 47 20 26 26 20 6b 65 79 43 6c 61 73 73  RING && keyClass
38818 3c 3d 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41  <=FTS3_HASH_BINA
38819 52 59 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 6b 65  RY );.  pNew->ke
3881a 79 43 6c 61 73 73 20 3d 20 6b 65 79 43 6c 61 73  yClass = keyClas
3881b 73 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 70 79 4b  s;.  pNew->copyK
3881c 65 79 20 3d 20 63 6f 70 79 4b 65 79 3b 0a 20 20  ey = copyKey;.  
3881d 70 4e 65 77 2d 3e 66 69 72 73 74 20 3d 20 30 3b  pNew->first = 0;
3881e 0a 20 20 70 4e 65 77 2d 3e 63 6f 75 6e 74 20 3d  .  pNew->count =
3881f 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 73 69   0;.  pNew->htsi
38820 7a 65 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e  ze = 0;.  pNew->
38821 68 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65  ht = 0;.}../* Re
38822 6d 6f 76 65 20 61 6c 6c 20 65 6e 74 72 69 65 73  move all entries
38823 20 66 72 6f 6d 20 61 20 68 61 73 68 20 74 61 62   from a hash tab
38824 6c 65 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c  le.  Reclaim all
38825 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 43 61 6c 6c   memory..** Call
38826 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f   this routine to
38827 20 64 65 6c 65 74 65 20 61 20 68 61 73 68 20 74   delete a hash t
38828 61 62 6c 65 20 6f 72 20 74 6f 20 72 65 73 65 74  able or to reset
38829 20 61 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a   a hash table.**
3882a 20 74 6f 20 74 68 65 20 65 6d 70 74 79 20 73 74   to the empty st
3882b 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ate..*/.SQLITE_P
3882c 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
3882d 74 65 33 46 74 73 33 48 61 73 68 43 6c 65 61 72  te3Fts3HashClear
3882e 28 66 74 73 33 48 61 73 68 20 2a 70 48 29 7b 0a  (fts3Hash *pH){.
3882f 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a    fts3HashElem *
38830 65 6c 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a  elem;         /*
38831 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
38832 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f  r all elements o
38833 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 0a  f the table */..
38834 20 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20    assert( pH!=0 
38835 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e  );.  elem = pH->
38836 66 69 72 73 74 3b 0a 20 20 70 48 2d 3e 66 69 72  first;.  pH->fir
38837 73 74 20 3d 20 30 3b 0a 20 20 66 74 73 33 48 61  st = 0;.  fts3Ha
38838 73 68 46 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a  shFree(pH->ht);.
38839 20 20 70 48 2d 3e 68 74 20 3d 20 30 3b 0a 20 20    pH->ht = 0;.  
3883a 70 48 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a  pH->htsize = 0;.
3883b 20 20 77 68 69 6c 65 28 20 65 6c 65 6d 20 29 7b    while( elem ){
3883c 0a 20 20 20 20 66 74 73 33 48 61 73 68 45 6c 65  .    fts3HashEle
3883d 6d 20 2a 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 65  m *next_elem = e
3883e 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 69  lem->next;.    i
3883f 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20 26  f( pH->copyKey &
38840 26 20 65 6c 65 6d 2d 3e 70 4b 65 79 20 29 7b 0a  & elem->pKey ){.
38841 20 20 20 20 20 20 66 74 73 33 48 61 73 68 46 72        fts3HashFr
38842 65 65 28 65 6c 65 6d 2d 3e 70 4b 65 79 29 3b 0a  ee(elem->pKey);.
38843 20 20 20 20 7d 0a 20 20 20 20 66 74 73 33 48 61      }.    fts3Ha
38844 73 68 46 72 65 65 28 65 6c 65 6d 29 3b 0a 20 20  shFree(elem);.  
38845 20 20 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c    elem = next_el
38846 65 6d 3b 0a 20 20 7d 0a 20 20 70 48 2d 3e 63 6f  em;.  }.  pH->co
38847 75 6e 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  unt = 0;.}../*.*
38848 2a 20 48 61 73 68 20 61 6e 64 20 63 6f 6d 70 61  * Hash and compa
38849 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20  rison functions 
3884a 77 68 65 6e 20 74 68 65 20 6d 6f 64 65 20 69 73  when the mode is
3884b 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e   FTS3_HASH_STRIN
3884c 47 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  G.*/.static int 
3884d 66 74 73 33 53 74 72 48 61 73 68 28 63 6f 6e 73  fts3StrHash(cons
3884e 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e  t void *pKey, in
3884f 74 20 6e 4b 65 79 29 7b 0a 20 20 63 6f 6e 73 74  t nKey){.  const
38850 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73   char *z = (cons
38851 74 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20  t char *)pKey;. 
38852 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20 69 66   int h = 0;.  if
38853 28 20 6e 4b 65 79 3c 3d 30 20 29 20 6e 4b 65 79  ( nKey<=0 ) nKey
38854 20 3d 20 28 69 6e 74 29 20 73 74 72 6c 65 6e 28   = (int) strlen(
38855 7a 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4b 65  z);.  while( nKe
38856 79 20 3e 20 30 20 20 29 7b 0a 20 20 20 20 68 20  y > 0  ){.    h 
38857 3d 20 28 68 3c 3c 33 29 20 5e 20 68 20 5e 20 2a  = (h<<3) ^ h ^ *
38858 7a 2b 2b 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b  z++;.    nKey--;
38859 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 20  .  }.  return h 
3885a 26 20 30 78 37 66 66 66 66 66 66 66 3b 0a 7d 0a  & 0x7fffffff;.}.
3885b 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53  static int fts3S
3885c 74 72 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20  trCompare(const 
3885d 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20 69 6e 74  void *pKey1, int
3885e 20 6e 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20   n1, const void 
3885f 2a 70 4b 65 79 32 2c 20 69 6e 74 20 6e 32 29 7b  *pKey2, int n2){
38860 0a 20 20 69 66 28 20 6e 31 21 3d 6e 32 20 29 20  .  if( n1!=n2 ) 
38861 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74 75  return 1;.  retu
38862 72 6e 20 73 74 72 6e 63 6d 70 28 28 63 6f 6e 73  rn strncmp((cons
38863 74 20 63 68 61 72 2a 29 70 4b 65 79 31 2c 28 63  t char*)pKey1,(c
38864 6f 6e 73 74 20 63 68 61 72 2a 29 70 4b 65 79 32  onst char*)pKey2
38865 2c 6e 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48  ,n1);.}../*.** H
38866 61 73 68 20 61 6e 64 20 63 6f 6d 70 61 72 69 73  ash and comparis
38867 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 77 68 65  on functions whe
38868 6e 20 74 68 65 20 6d 6f 64 65 20 69 73 20 46 54  n the mode is FT
38869 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 0a 2a  S3_HASH_BINARY.*
3886a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
3886b 33 42 69 6e 48 61 73 68 28 63 6f 6e 73 74 20 76  3BinHash(const v
3886c 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e  oid *pKey, int n
3886d 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68 20 3d 20  Key){.  int h = 
3886e 30 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  0;.  const char 
3886f 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  *z = (const char
38870 20 2a 29 70 4b 65 79 3b 0a 20 20 77 68 69 6c 65   *)pKey;.  while
38871 28 20 6e 4b 65 79 2d 2d 20 3e 20 30 20 29 7b 0a  ( nKey-- > 0 ){.
38872 20 20 20 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e      h = (h<<3) ^
38873 20 68 20 5e 20 2a 28 7a 2b 2b 29 3b 0a 20 20 7d   h ^ *(z++);.  }
38874 0a 20 20 72 65 74 75 72 6e 20 68 20 26 20 30 78  .  return h & 0x
38875 37 66 66 66 66 66 66 66 3b 0a 7d 0a 73 74 61 74  7fffffff;.}.stat
38876 69 63 20 69 6e 74 20 66 74 73 33 42 69 6e 43 6f  ic int fts3BinCo
38877 6d 70 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64  mpare(const void
38878 20 2a 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c   *pKey1, int n1,
38879 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
3887a 79 32 2c 20 69 6e 74 20 6e 32 29 7b 0a 20 20 69  y2, int n2){.  i
3887b 66 28 20 6e 31 21 3d 6e 32 20 29 20 72 65 74 75  f( n1!=n2 ) retu
3887c 72 6e 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 6d  rn 1;.  return m
3887d 65 6d 63 6d 70 28 70 4b 65 79 31 2c 70 4b 65 79  emcmp(pKey1,pKey
3887e 32 2c 6e 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  2,n1);.}../*.** 
3887f 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
38880 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69   to the appropri
38881 61 74 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f  ate hash functio
38882 6e 20 67 69 76 65 6e 20 74 68 65 20 6b 65 79 20  n given the key 
38883 63 6c 61 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  class..**.** The
38884 20 43 20 73 79 6e 74 61 78 20 69 6e 20 74 68 69   C syntax in thi
38885 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e  s function defin
38886 69 74 69 6f 6e 20 6d 61 79 20 62 65 20 75 6e 66  ition may be unf
38887 61 6d 69 6c 61 72 20 74 6f 20 73 6f 6d 65 20 0a  amilar to some .
38888 2a 2a 20 70 72 6f 67 72 61 6d 6d 65 72 73 2c 20  ** programmers, 
38889 73 6f 20 77 65 20 70 72 6f 76 69 64 65 20 74 68  so we provide th
3888a 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 64 64 69  e following addi
3888b 74 69 6f 6e 61 6c 20 65 78 70 6c 61 6e 61 74 69  tional explanati
3888c 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61  on:.**.** The na
3888d 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  me of the functi
3888e 6f 6e 20 69 73 20 22 66 74 73 48 61 73 68 46 75  on is "ftsHashFu
3888f 6e 63 74 69 6f 6e 22 2e 20 20 54 68 65 20 66 75  nction".  The fu
38890 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 0a 2a  nction takes a.*
38891 2a 20 73 69 6e 67 6c 65 20 70 61 72 61 6d 65 74  * single paramet
38892 65 72 20 22 6b 65 79 43 6c 61 73 73 22 2e 20 20  er "keyClass".  
38893 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
38894 20 6f 66 20 66 74 73 48 61 73 68 46 75 6e 63 74   of ftsHashFunct
38895 69 6f 6e 28 29 0a 2a 2a 20 69 73 20 61 20 70 6f  ion().** is a po
38896 69 6e 74 65 72 20 74 6f 20 61 6e 6f 74 68 65 72  inter to another
38897 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 70 65 63   function.  Spec
38898 69 66 69 63 61 6c 6c 79 2c 20 74 68 65 20 72 65  ifically, the re
38899 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66  turn value.** of
3889a 20 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f 6e   ftsHashFunction
3889b 28 29 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  () is a pointer 
3889c 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 68  to a function th
3889d 61 74 20 74 61 6b 65 73 20 74 77 6f 20 70 61 72  at takes two par
3889e 61 6d 65 74 65 72 73 0a 2a 2a 20 77 69 74 68 20  ameters.** with 
3889f 74 79 70 65 73 20 22 63 6f 6e 73 74 20 76 6f 69  types "const voi
388a0 64 2a 22 20 61 6e 64 20 22 69 6e 74 22 20 61 6e  d*" and "int" an
388a1 64 20 72 65 74 75 72 6e 73 20 61 6e 20 22 69 6e  d returns an "in
388a2 74 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  t"..*/.static in
388a3 74 20 28 2a 66 74 73 48 61 73 68 46 75 6e 63 74  t (*ftsHashFunct
388a4 69 6f 6e 28 69 6e 74 20 6b 65 79 43 6c 61 73 73  ion(int keyClass
388a5 29 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  ))(const void*,i
388a6 6e 74 29 7b 0a 20 20 69 66 28 20 6b 65 79 43 6c  nt){.  if( keyCl
388a7 61 73 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f 53  ass==FTS3_HASH_S
388a8 54 52 49 4e 47 20 29 7b 0a 20 20 20 20 72 65 74  TRING ){.    ret
388a9 75 72 6e 20 26 66 74 73 33 53 74 72 48 61 73 68  urn &fts3StrHash
388aa 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
388ab 73 73 65 72 74 28 20 6b 65 79 43 6c 61 73 73 3d  ssert( keyClass=
388ac 3d 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52  =FTS3_HASH_BINAR
388ad 59 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  Y );.    return 
388ae 26 66 74 73 33 42 69 6e 48 61 73 68 3b 0a 20 20  &fts3BinHash;.  
388af 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  }.}../*.** Retur
388b0 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
388b1 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 68  he appropriate h
388b2 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 67 69 76  ash function giv
388b3 65 6e 20 74 68 65 20 6b 65 79 20 63 6c 61 73 73  en the key class
388b4 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 68 65 6c 70  ..**.** For help
388b5 20 69 6e 20 69 6e 74 65 72 70 72 65 74 65 64 20   in interpreted 
388b6 74 68 65 20 6f 62 73 63 75 72 65 20 43 20 63 6f  the obscure C co
388b7 64 65 20 69 6e 20 74 68 65 20 66 75 6e 63 74 69  de in the functi
388b8 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2c 0a 2a  on definition,.*
388b9 2a 20 73 65 65 20 74 68 65 20 68 65 61 64 65 72  * see the header
388ba 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 65 20   comment on the 
388bb 70 72 65 76 69 6f 75 73 20 66 75 6e 63 74 69 6f  previous functio
388bc 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
388bd 20 28 2a 66 74 73 43 6f 6d 70 61 72 65 46 75 6e   (*ftsCompareFun
388be 63 74 69 6f 6e 28 69 6e 74 20 6b 65 79 43 6c 61  ction(int keyCla
388bf 73 73 29 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a  ss))(const void*
388c0 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
388c1 2c 69 6e 74 29 7b 0a 20 20 69 66 28 20 6b 65 79  ,int){.  if( key
388c2 43 6c 61 73 73 3d 3d 46 54 53 33 5f 48 41 53 48  Class==FTS3_HASH
388c3 5f 53 54 52 49 4e 47 20 29 7b 0a 20 20 20 20 72  _STRING ){.    r
388c4 65 74 75 72 6e 20 26 66 74 73 33 53 74 72 43 6f  eturn &fts3StrCo
388c5 6d 70 61 72 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  mpare;.  }else{.
388c6 20 20 20 20 61 73 73 65 72 74 28 20 6b 65 79 43      assert( keyC
388c7 6c 61 73 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f  lass==FTS3_HASH_
388c8 42 49 4e 41 52 59 20 29 3b 0a 20 20 20 20 72 65  BINARY );.    re
388c9 74 75 72 6e 20 26 66 74 73 33 42 69 6e 43 6f 6d  turn &fts3BinCom
388ca 70 61 72 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20  pare;.  }.}../* 
388cb 4c 69 6e 6b 20 61 6e 20 65 6c 65 6d 65 6e 74 20  Link an element 
388cc 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74 61  into the hash ta
388cd 62 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ble.*/.static vo
388ce 69 64 20 66 74 73 33 48 61 73 68 49 6e 73 65 72  id fts3HashInser
388cf 74 45 6c 65 6d 65 6e 74 28 0a 20 20 66 74 73 33  tElement(.  fts3
388d0 48 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20  Hash *pH,       
388d1 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70       /* The comp
388d2 6c 65 74 65 20 68 61 73 68 20 74 61 62 6c 65 20  lete hash table 
388d3 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 66 74 73  */.  struct _fts
388d4 33 68 74 20 2a 70 45 6e 74 72 79 2c 20 20 2f 2a  3ht *pEntry,  /*
388d5 20 54 68 65 20 65 6e 74 72 79 20 69 6e 74 6f 20   The entry into 
388d6 77 68 69 63 68 20 70 4e 65 77 20 69 73 20 69 6e  which pNew is in
388d7 73 65 72 74 65 64 20 2a 2f 0a 20 20 66 74 73 33  serted */.  fts3
388d8 48 61 73 68 45 6c 65 6d 20 2a 70 4e 65 77 20 20  HashElem *pNew  
388d9 20 20 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d       /* The elem
388da 65 6e 74 20 74 6f 20 62 65 20 69 6e 73 65 72 74  ent to be insert
388db 65 64 20 2a 2f 0a 29 7b 0a 20 20 66 74 73 33 48  ed */.){.  fts3H
388dc 61 73 68 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20  ashElem *pHead; 
388dd 20 20 20 20 2f 2a 20 46 69 72 73 74 20 65 6c 65      /* First ele
388de 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 69 6e 20  ment already in 
388df 70 45 6e 74 72 79 20 2a 2f 0a 20 20 70 48 65 61  pEntry */.  pHea
388e0 64 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61 69  d = pEntry->chai
388e1 6e 3b 0a 20 20 69 66 28 20 70 48 65 61 64 20 29  n;.  if( pHead )
388e2 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74  {.    pNew->next
388e3 20 3d 20 70 48 65 61 64 3b 0a 20 20 20 20 70 4e   = pHead;.    pN
388e4 65 77 2d 3e 70 72 65 76 20 3d 20 70 48 65 61 64  ew->prev = pHead
388e5 2d 3e 70 72 65 76 3b 0a 20 20 20 20 69 66 28 20  ->prev;.    if( 
388e6 70 48 65 61 64 2d 3e 70 72 65 76 20 29 7b 20 70  pHead->prev ){ p
388e7 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65 78 74  Head->prev->next
388e8 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 65   = pNew; }.    e
388e9 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20  lse             
388ea 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e  { pH->first = pN
388eb 65 77 3b 20 7d 0a 20 20 20 20 70 48 65 61 64 2d  ew; }.    pHead-
388ec 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 0a 20 20  >prev = pNew;.  
388ed 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d  }else{.    pNew-
388ee 3e 6e 65 78 74 20 3d 20 70 48 2d 3e 66 69 72 73  >next = pH->firs
388ef 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d 3e 66  t;.    if( pH->f
388f0 69 72 73 74 20 29 7b 20 70 48 2d 3e 66 69 72 73  irst ){ pH->firs
388f1 74 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 20  t->prev = pNew; 
388f2 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 76  }.    pNew->prev
388f3 20 3d 20 30 3b 0a 20 20 20 20 70 48 2d 3e 66 69   = 0;.    pH->fi
388f4 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a  rst = pNew;.  }.
388f5 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2b    pEntry->count+
388f6 2b 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 63 68 61  +;.  pEntry->cha
388f7 69 6e 20 3d 20 70 4e 65 77 3b 0a 7d 0a 0a 0a 2f  in = pNew;.}.../
388f8 2a 20 52 65 73 69 7a 65 20 74 68 65 20 68 61 73  * Resize the has
388f9 68 20 74 61 62 6c 65 20 73 6f 20 74 68 61 74 20  h table so that 
388fa 69 74 20 63 61 6e 74 61 69 6e 73 20 22 6e 65 77  it cantains "new
388fb 5f 73 69 7a 65 22 20 62 75 63 6b 65 74 73 2e 0a  _size" buckets..
388fc 2a 2a 20 22 6e 65 77 5f 73 69 7a 65 22 20 6d 75  ** "new_size" mu
388fd 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66  st be a power of
388fe 20 32 2e 20 20 54 68 65 20 68 61 73 68 20 74 61   2.  The hash ta
388ff 62 6c 65 20 6d 69 67 68 74 20 66 61 69 6c 20 0a  ble might fail .
38900 2a 2a 20 74 6f 20 72 65 73 69 7a 65 20 69 66 20  ** to resize if 
38901 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 66  sqliteMalloc() f
38902 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ails..*/.static 
38903 76 6f 69 64 20 66 74 73 33 52 65 68 61 73 68 28  void fts3Rehash(
38904 66 74 73 33 48 61 73 68 20 2a 70 48 2c 20 69 6e  fts3Hash *pH, in
38905 74 20 6e 65 77 5f 73 69 7a 65 29 7b 0a 20 20 73  t new_size){.  s
38906 74 72 75 63 74 20 5f 66 74 73 33 68 74 20 2a 6e  truct _fts3ht *n
38907 65 77 5f 68 74 3b 20 20 20 20 20 20 20 20 20 20  ew_ht;          
38908 2f 2a 20 54 68 65 20 6e 65 77 20 68 61 73 68 20  /* The new hash 
38909 74 61 62 6c 65 20 2a 2f 0a 20 20 66 74 73 33 48  table */.  fts3H
3890a 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 2c 20 2a  ashElem *elem, *
3890b 6e 65 78 74 5f 65 6c 65 6d 3b 20 20 2f 2a 20 46  next_elem;  /* F
3890c 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
3890d 65 78 69 73 74 69 6e 67 20 65 6c 65 6d 65 6e 74  existing element
3890e 73 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 48 61  s */.  int (*xHa
3890f 73 68 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  sh)(const void*,
38910 69 6e 74 29 3b 20 20 20 2f 2a 20 54 68 65 20 68  int);   /* The h
38911 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ash function */.
38912 0a 20 20 61 73 73 65 72 74 28 20 28 6e 65 77 5f  .  assert( (new_
38913 73 69 7a 65 20 26 20 28 6e 65 77 5f 73 69 7a 65  size & (new_size
38914 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 6e 65 77  -1))==0 );.  new
38915 5f 68 74 20 3d 20 28 73 74 72 75 63 74 20 5f 66  _ht = (struct _f
38916 74 73 33 68 74 20 2a 29 66 74 73 33 48 61 73 68  ts3ht *)fts3Hash
38917 4d 61 6c 6c 6f 63 28 20 6e 65 77 5f 73 69 7a 65  Malloc( new_size
38918 2a 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 5f  *sizeof(struct _
38919 66 74 73 33 68 74 29 20 29 3b 0a 20 20 69 66 28  fts3ht) );.  if(
3891a 20 6e 65 77 5f 68 74 3d 3d 30 20 29 20 72 65 74   new_ht==0 ) ret
3891b 75 72 6e 3b 0a 20 20 66 74 73 33 48 61 73 68 46  urn;.  fts3HashF
3891c 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70  ree(pH->ht);.  p
3891d 48 2d 3e 68 74 20 3d 20 6e 65 77 5f 68 74 3b 0a  H->ht = new_ht;.
3891e 20 20 70 48 2d 3e 68 74 73 69 7a 65 20 3d 20 6e    pH->htsize = n
3891f 65 77 5f 73 69 7a 65 3b 0a 20 20 78 48 61 73 68  ew_size;.  xHash
38920 20 3d 20 66 74 73 48 61 73 68 46 75 6e 63 74 69   = ftsHashFuncti
38921 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29  on(pH->keyClass)
38922 3b 0a 20 20 66 6f 72 28 65 6c 65 6d 3d 70 48 2d  ;.  for(elem=pH-
38923 3e 66 69 72 73 74 2c 20 70 48 2d 3e 66 69 72 73  >first, pH->firs
38924 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c 65 6d 20  t=0; elem; elem 
38925 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 7b 0a 20 20  = next_elem){.  
38926 20 20 69 6e 74 20 68 20 3d 20 28 2a 78 48 61 73    int h = (*xHas
38927 68 29 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 65  h)(elem->pKey, e
38928 6c 65 6d 2d 3e 6e 4b 65 79 29 20 26 20 28 6e 65  lem->nKey) & (ne
38929 77 5f 73 69 7a 65 2d 31 29 3b 0a 20 20 20 20 6e  w_size-1);.    n
3892a 65 78 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d  ext_elem = elem-
3892b 3e 6e 65 78 74 3b 0a 20 20 20 20 66 74 73 33 48  >next;.    fts3H
3892c 61 73 68 49 6e 73 65 72 74 45 6c 65 6d 65 6e 74  ashInsertElement
3892d 28 70 48 2c 20 26 6e 65 77 5f 68 74 5b 68 5d 2c  (pH, &new_ht[h],
3892e 20 65 6c 65 6d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f   elem);.  }.}../
3892f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
38930 28 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73  (for internal us
38931 65 20 6f 6e 6c 79 29 20 6c 6f 63 61 74 65 73 20  e only) locates 
38932 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 20 61 6e  an element in an
38933 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 74  .** hash table t
38934 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 20  hat matches the 
38935 67 69 76 65 6e 20 6b 65 79 2e 20 20 54 68 65 20  given key.  The 
38936 68 61 73 68 20 66 6f 72 20 74 68 69 73 20 6b 65  hash for this ke
38937 79 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79  y has.** already
38938 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 20 61   been computed a
38939 6e 64 20 69 73 20 70 61 73 73 65 64 20 61 73 20  nd is passed as 
3893a 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65  the 4th paramete
3893b 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 66 74 73  r..*/.static fts
3893c 33 48 61 73 68 45 6c 65 6d 20 2a 66 74 73 33 46  3HashElem *fts3F
3893d 69 6e 64 45 6c 65 6d 65 6e 74 42 79 48 61 73 68  indElementByHash
3893e 28 0a 20 20 63 6f 6e 73 74 20 66 74 73 33 48 61  (.  const fts3Ha
3893f 73 68 20 2a 70 48 2c 20 2f 2a 20 54 68 65 20 70  sh *pH, /* The p
38940 48 20 74 6f 20 62 65 20 73 65 61 72 63 68 65 64  H to be searched
38941 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
38942 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 54 68 65   *pKey,   /* The
38943 20 6b 65 79 20 77 65 20 61 72 65 20 73 65 61 72   key we are sear
38944 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a 20 20 69  ching for */.  i
38945 6e 74 20 6e 4b 65 79 2c 0a 20 20 69 6e 74 20 68  nt nKey,.  int h
38946 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
38947 2a 20 54 68 65 20 68 61 73 68 20 66 6f 72 20 74  * The hash for t
38948 68 69 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20  his key. */.){. 
38949 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65   fts3HashElem *e
3894a 6c 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  lem;            
3894b 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20  /* Used to loop 
3894c 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74  thru the element
3894d 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 63   list */.  int c
3894e 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  ount;           
3894f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
38950 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
38951 6c 65 66 74 20 74 6f 20 74 65 73 74 20 2a 2f 0a  left to test */.
38952 20 20 69 6e 74 20 28 2a 78 43 6f 6d 70 61 72 65    int (*xCompare
38953 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  )(const void*,in
38954 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  t,const void*,in
38955 74 29 3b 20 20 2f 2a 20 63 6f 6d 70 61 72 69 73  t);  /* comparis
38956 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a  on function */..
38957 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a    if( pH->ht ){.
38958 20 20 20 20 73 74 72 75 63 74 20 5f 66 74 73 33      struct _fts3
38959 68 74 20 2a 70 45 6e 74 72 79 20 3d 20 26 70 48  ht *pEntry = &pH
3895a 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 20 65 6c 65  ->ht[h];.    ele
3895b 6d 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61 69  m = pEntry->chai
3895c 6e 3b 0a 20 20 20 20 63 6f 75 6e 74 20 3d 20 70  n;.    count = p
3895d 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20  Entry->count;.  
3895e 20 20 78 43 6f 6d 70 61 72 65 20 3d 20 66 74 73    xCompare = fts
3895f 43 6f 6d 70 61 72 65 46 75 6e 63 74 69 6f 6e 28  CompareFunction(
38960 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b 0a 20  pH->keyClass);. 
38961 20 20 20 77 68 69 6c 65 28 20 63 6f 75 6e 74 2d     while( count-
38962 2d 20 26 26 20 65 6c 65 6d 20 29 7b 0a 20 20 20  - && elem ){.   
38963 20 20 20 69 66 28 20 28 2a 78 43 6f 6d 70 61 72     if( (*xCompar
38964 65 29 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 65 6c  e)(elem->pKey,el
38965 65 6d 2d 3e 6e 4b 65 79 2c 70 4b 65 79 2c 6e 4b  em->nKey,pKey,nK
38966 65 79 29 3d 3d 30 20 29 7b 20 0a 20 20 20 20 20  ey)==0 ){ .     
38967 20 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 3b 0a     return elem;.
38968 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 65 6c        }.      el
38969 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b  em = elem->next;
3896a 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
3896b 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d  urn 0;.}../* Rem
3896c 6f 76 65 20 61 20 73 69 6e 67 6c 65 20 65 6e 74  ove a single ent
3896d 72 79 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68  ry from the hash
3896e 20 74 61 62 6c 65 20 67 69 76 65 6e 20 61 20 70   table given a p
3896f 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74 0a 2a  ointer to that.*
38970 2a 20 65 6c 65 6d 65 6e 74 20 61 6e 64 20 61 20  * element and a 
38971 68 61 73 68 20 6f 6e 20 74 68 65 20 65 6c 65 6d  hash on the elem
38972 65 6e 74 27 73 20 6b 65 79 2e 0a 2a 2f 0a 73 74  ent's key..*/.st
38973 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 52 65  atic void fts3Re
38974 6d 6f 76 65 45 6c 65 6d 65 6e 74 42 79 48 61 73  moveElementByHas
38975 68 28 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70  h(.  fts3Hash *p
38976 48 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  H,         /* Th
38977 65 20 70 48 20 63 6f 6e 74 61 69 6e 69 6e 67 20  e pH containing 
38978 22 65 6c 65 6d 22 20 2a 2f 0a 20 20 66 74 73 33  "elem" */.  fts3
38979 48 61 73 68 45 6c 65 6d 2a 20 65 6c 65 6d 2c 20  HashElem* elem, 
3897a 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74    /* The element
3897b 20 74 6f 20 62 65 20 72 65 6d 6f 76 65 64 20 66   to be removed f
3897c 72 6f 6d 20 74 68 65 20 70 48 20 2a 2f 0a 20 20  rom the pH */.  
3897d 69 6e 74 20 68 20 20 20 20 20 20 20 20 20 20 20  int h           
3897e 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 76 61        /* Hash va
3897f 6c 75 65 20 66 6f 72 20 74 68 65 20 65 6c 65 6d  lue for the elem
38980 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75  ent */.){.  stru
38981 63 74 20 5f 66 74 73 33 68 74 20 2a 70 45 6e 74  ct _fts3ht *pEnt
38982 72 79 3b 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e  ry;.  if( elem->
38983 70 72 65 76 20 29 7b 0a 20 20 20 20 65 6c 65 6d  prev ){.    elem
38984 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 65  ->prev->next = e
38985 6c 65 6d 2d 3e 6e 65 78 74 3b 20 0a 20 20 7d 65  lem->next; .  }e
38986 6c 73 65 7b 0a 20 20 20 20 70 48 2d 3e 66 69 72  lse{.    pH->fir
38987 73 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b  st = elem->next;
38988 0a 20 20 7d 0a 20 20 69 66 28 20 65 6c 65 6d 2d  .  }.  if( elem-
38989 3e 6e 65 78 74 20 29 7b 0a 20 20 20 20 65 6c 65  >next ){.    ele
3898a 6d 2d 3e 6e 65 78 74 2d 3e 70 72 65 76 20 3d 20  m->next->prev = 
3898b 65 6c 65 6d 2d 3e 70 72 65 76 3b 0a 20 20 7d 0a  elem->prev;.  }.
3898c 20 20 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e    pEntry = &pH->
3898d 68 74 5b 68 5d 3b 0a 20 20 69 66 28 20 70 45 6e  ht[h];.  if( pEn
3898e 74 72 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d  try->chain==elem
3898f 20 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e   ){.    pEntry->
38990 63 68 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65  chain = elem->ne
38991 78 74 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79  xt;.  }.  pEntry
38992 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28  ->count--;.  if(
38993 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3c 3d   pEntry->count<=
38994 30 20 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 2d  0 ){.    pEntry-
38995 3e 63 68 61 69 6e 20 3d 20 30 3b 0a 20 20 7d 0a  >chain = 0;.  }.
38996 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65    if( pH->copyKe
38997 79 20 26 26 20 65 6c 65 6d 2d 3e 70 4b 65 79 20  y && elem->pKey 
38998 29 7b 0a 20 20 20 20 66 74 73 33 48 61 73 68 46  ){.    fts3HashF
38999 72 65 65 28 65 6c 65 6d 2d 3e 70 4b 65 79 29 3b  ree(elem->pKey);
3899a 0a 20 20 7d 0a 20 20 66 74 73 33 48 61 73 68 46  .  }.  fts3HashF
3899b 72 65 65 28 20 65 6c 65 6d 20 29 3b 0a 20 20 70  ree( elem );.  p
3899c 48 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66  H->count--;.  if
3899d 28 20 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29  ( pH->count<=0 )
3899e 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 48  {.    assert( pH
3899f 2d 3e 66 69 72 73 74 3d 3d 30 20 29 3b 0a 20 20  ->first==0 );.  
389a0 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 63 6f    assert( pH->co
389a1 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 66 74  unt==0 );.    ft
389a2 73 33 48 61 73 68 43 6c 65 61 72 28 70 48 29 3b  s3HashClear(pH);
389a3 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74 74 65 6d  .  }.}../* Attem
389a4 70 74 20 74 6f 20 6c 6f 63 61 74 65 20 61 6e 20  pt to locate an 
389a5 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 68  element of the h
389a6 61 73 68 20 74 61 62 6c 65 20 70 48 20 77 69 74  ash table pH wit
389a7 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68 61 74 20  h a key.** that 
389a8 6d 61 74 63 68 65 73 20 70 4b 65 79 2c 6e 4b 65  matches pKey,nKe
389a9 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 64  y.  Return the d
389aa 61 74 61 20 66 6f 72 20 74 68 69 73 20 65 6c 65  ata for this ele
389ab 6d 65 6e 74 20 69 66 20 69 74 20 69 73 0a 2a 2a  ment if it is.**
389ac 20 66 6f 75 6e 64 2c 20 6f 72 20 4e 55 4c 4c 20   found, or NULL 
389ad 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d  if there is no m
389ae 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  atch..*/.SQLITE_
389af 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
389b0 6c 69 74 65 33 46 74 73 33 48 61 73 68 46 69 6e  lite3Fts3HashFin
389b1 64 28 63 6f 6e 73 74 20 66 74 73 33 48 61 73 68  d(const fts3Hash
389b2 20 2a 70 48 2c 20 63 6f 6e 73 74 20 76 6f 69 64   *pH, const void
389b3 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79   *pKey, int nKey
389b4 29 7b 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 20  ){.  int h;     
389b5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
389b6 20 68 61 73 68 20 6f 6e 20 6b 65 79 20 2a 2f 0a   hash on key */.
389b7 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a    fts3HashElem *
389b8 65 6c 65 6d 3b 20 20 20 20 2f 2a 20 54 68 65 20  elem;    /* The 
389b9 65 6c 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 74  element that mat
389ba 63 68 65 73 20 6b 65 79 20 2a 2f 0a 20 20 69 6e  ches key */.  in
389bb 74 20 28 2a 78 48 61 73 68 29 28 63 6f 6e 73 74  t (*xHash)(const
389bc 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a   void*,int);  /*
389bd 20 54 68 65 20 68 61 73 68 20 66 75 6e 63 74 69   The hash functi
389be 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 3d  on */..  if( pH=
389bf 3d 30 20 7c 7c 20 70 48 2d 3e 68 74 3d 3d 30 20  =0 || pH->ht==0 
389c0 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 78 48  ) return 0;.  xH
389c1 61 73 68 20 3d 20 66 74 73 48 61 73 68 46 75 6e  ash = ftsHashFun
389c2 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61  ction(pH->keyCla
389c3 73 73 29 3b 0a 20 20 61 73 73 65 72 74 28 20 78  ss);.  assert( x
389c4 48 61 73 68 21 3d 30 20 29 3b 0a 20 20 68 20 3d  Hash!=0 );.  h =
389c5 20 28 2a 78 48 61 73 68 29 28 70 4b 65 79 2c 6e   (*xHash)(pKey,n
389c6 4b 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Key);.  assert( 
389c7 28 70 48 2d 3e 68 74 73 69 7a 65 20 26 20 28 70  (pH->htsize & (p
389c8 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29 3d 3d 30  H->htsize-1))==0
389c9 20 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 66 74 73   );.  elem = fts
389ca 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42 79 48 61  3FindElementByHa
389cb 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c  sh(pH,pKey,nKey,
389cc 20 68 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65   h & (pH->htsize
389cd 2d 31 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 65  -1));.  return e
389ce 6c 65 6d 20 3f 20 65 6c 65 6d 2d 3e 64 61 74 61  lem ? elem->data
389cf 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 20 49 6e 73 65   : 0;.}../* Inse
389d0 72 74 20 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e  rt an element in
389d1 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  to the hash tabl
389d2 65 20 70 48 2e 20 20 54 68 65 20 6b 65 79 20 69  e pH.  The key i
389d3 73 20 70 4b 65 79 2c 6e 4b 65 79 0a 2a 2a 20 61  s pKey,nKey.** a
389d4 6e 64 20 74 68 65 20 64 61 74 61 20 69 73 20 22  nd the data is "
389d5 64 61 74 61 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  data"..**.** If 
389d6 6e 6f 20 65 6c 65 6d 65 6e 74 20 65 78 69 73 74  no element exist
389d7 73 20 77 69 74 68 20 61 20 6d 61 74 63 68 69 6e  s with a matchin
389d8 67 20 6b 65 79 2c 20 74 68 65 6e 20 61 20 6e 65  g key, then a ne
389d9 77 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 69 73 20  w.** element is 
389da 63 72 65 61 74 65 64 2e 20 20 41 20 63 6f 70 79  created.  A copy
389db 20 6f 66 20 74 68 65 20 6b 65 79 20 69 73 20 6d   of the key is m
389dc 61 64 65 20 69 66 20 74 68 65 20 63 6f 70 79 4b  ade if the copyK
389dd 65 79 0a 2a 2a 20 66 6c 61 67 20 69 73 20 73 65  ey.** flag is se
389de 74 2e 20 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  t.  NULL is retu
389df 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  rned..**.** If a
389e0 6e 6f 74 68 65 72 20 65 6c 65 6d 65 6e 74 20 61  nother element a
389e1 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 77 69  lready exists wi
389e2 74 68 20 74 68 65 20 73 61 6d 65 20 6b 65 79 2c  th the same key,
389e3 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6e 65 77   then the.** new
389e4 20 64 61 74 61 20 72 65 70 6c 61 63 65 73 20 74   data replaces t
389e5 68 65 20 6f 6c 64 20 64 61 74 61 20 61 6e 64 20  he old data and 
389e6 74 68 65 20 6f 6c 64 20 64 61 74 61 20 69 73 20  the old data is 
389e7 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 68 65  returned..** The
389e8 20 6b 65 79 20 69 73 20 6e 6f 74 20 63 6f 70 69   key is not copi
389e9 65 64 20 69 6e 20 74 68 69 73 20 69 6e 73 74 61  ed in this insta
389ea 6e 63 65 2e 20 20 49 66 20 61 20 6d 61 6c 6c 6f  nce.  If a mallo
389eb 63 20 66 61 69 6c 73 2c 20 74 68 65 6e 0a 2a 2a  c fails, then.**
389ec 20 74 68 65 20 6e 65 77 20 64 61 74 61 20 69 73   the new data is
389ed 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68   returned and th
389ee 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20  e hash table is 
389ef 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a  unchanged..**.**
389f0 20 49 66 20 74 68 65 20 22 64 61 74 61 22 20 70   If the "data" p
389f1 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 69 73  arameter to this
389f2 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c   function is NUL
389f3 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 65  L, then the.** e
389f4 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73 70 6f 6e  lement correspon
389f5 64 69 6e 67 20 74 6f 20 22 6b 65 79 22 20 69 73  ding to "key" is
389f6 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
389f7 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f  e hash table..*/
389f8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
389f9 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 46 74 73  void *sqlite3Fts
389fa 33 48 61 73 68 49 6e 73 65 72 74 28 0a 20 20 66  3HashInsert(.  f
389fb 74 73 33 48 61 73 68 20 2a 70 48 2c 20 20 20 20  ts3Hash *pH,    
389fc 20 20 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20      /* The hash 
389fd 74 61 62 6c 65 20 74 6f 20 69 6e 73 65 72 74 20  table to insert 
389fe 69 6e 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  into */.  const 
389ff 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 20 2f  void *pKey,    /
38a00 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 20 20 69  * The key */.  i
38a01 6e 74 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20  nt nKey,        
38a02 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
38a03 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6b 65   bytes in the ke
38a04 79 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 64 61 74  y */.  void *dat
38a05 61 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  a           /* T
38a06 68 65 20 64 61 74 61 20 2a 2f 0a 29 7b 0a 20 20  he data */.){.  
38a07 69 6e 74 20 68 72 61 77 3b 20 20 20 20 20 20 20  int hraw;       
38a08 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 61 77            /* Raw
38a09 20 68 61 73 68 20 76 61 6c 75 65 20 6f 66 20 74   hash value of t
38a0a 68 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20  he key */.  int 
38a0b 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
38a0c 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73        /* the has
38a0d 68 20 6f 66 20 74 68 65 20 6b 65 79 20 6d 6f 64  h of the key mod
38a0e 75 6c 6f 20 68 61 73 68 20 74 61 62 6c 65 20 73  ulo hash table s
38a0f 69 7a 65 20 2a 2f 0a 20 20 66 74 73 33 48 61 73  ize */.  fts3Has
38a10 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20  hElem *elem;    
38a11 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f     /* Used to lo
38a12 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c 65 6d  op thru the elem
38a13 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 66 74  ent list */.  ft
38a14 73 33 48 61 73 68 45 6c 65 6d 20 2a 6e 65 77 5f  s3HashElem *new_
38a15 65 6c 65 6d 3b 20 20 20 2f 2a 20 4e 65 77 20 65  elem;   /* New e
38a16 6c 65 6d 65 6e 74 20 61 64 64 65 64 20 74 6f 20  lement added to 
38a17 74 68 65 20 70 48 20 2a 2f 0a 20 20 69 6e 74 20  the pH */.  int 
38a18 28 2a 78 48 61 73 68 29 28 63 6f 6e 73 74 20 76  (*xHash)(const v
38a19 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 54  oid*,int);  /* T
38a1a 68 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e  he hash function
38a1b 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
38a1c 48 21 3d 30 20 29 3b 0a 20 20 78 48 61 73 68 20  H!=0 );.  xHash 
38a1d 3d 20 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f  = ftsHashFunctio
38a1e 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b  n(pH->keyClass);
38a1f 0a 20 20 61 73 73 65 72 74 28 20 78 48 61 73 68  .  assert( xHash
38a20 21 3d 30 20 29 3b 0a 20 20 68 72 61 77 20 3d 20  !=0 );.  hraw = 
38a21 28 2a 78 48 61 73 68 29 28 70 4b 65 79 2c 20 6e  (*xHash)(pKey, n
38a22 4b 65 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Key);.  assert( 
38a23 28 70 48 2d 3e 68 74 73 69 7a 65 20 26 20 28 70  (pH->htsize & (p
38a24 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29 3d 3d 30  H->htsize-1))==0
38a25 20 29 3b 0a 20 20 68 20 3d 20 68 72 61 77 20 26   );.  h = hraw &
38a26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 3b   (pH->htsize-1);
38a27 0a 20 20 65 6c 65 6d 20 3d 20 66 74 73 33 46 69  .  elem = fts3Fi
38a28 6e 64 45 6c 65 6d 65 6e 74 42 79 48 61 73 68 28  ndElementByHash(
38a29 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 68 29 3b  pH,pKey,nKey,h);
38a2a 0a 20 20 69 66 28 20 65 6c 65 6d 20 29 7b 0a 20  .  if( elem ){. 
38a2b 20 20 20 76 6f 69 64 20 2a 6f 6c 64 5f 64 61 74     void *old_dat
38a2c 61 20 3d 20 65 6c 65 6d 2d 3e 64 61 74 61 3b 0a  a = elem->data;.
38a2d 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20      if( data==0 
38a2e 29 7b 0a 20 20 20 20 20 20 66 74 73 33 52 65 6d  ){.      fts3Rem
38a2f 6f 76 65 45 6c 65 6d 65 6e 74 42 79 48 61 73 68  oveElementByHash
38a30 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20 20 20  (pH,elem,h);.   
38a31 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 65 6c   }else{.      el
38a32 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b  em->data = data;
38a33 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
38a34 6e 20 6f 6c 64 5f 64 61 74 61 3b 0a 20 20 7d 0a  n old_data;.  }.
38a35 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20 29 20    if( data==0 ) 
38a36 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
38a37 70 48 2d 3e 68 74 73 69 7a 65 3d 3d 30 20 29 7b  pH->htsize==0 ){
38a38 0a 20 20 20 20 66 74 73 33 52 65 68 61 73 68 28  .    fts3Rehash(
38a39 70 48 2c 38 29 3b 0a 20 20 20 20 69 66 28 20 70  pH,8);.    if( p
38a3a 48 2d 3e 68 74 73 69 7a 65 3d 3d 30 20 29 7b 0a  H->htsize==0 ){.
38a3b 20 20 20 20 20 20 70 48 2d 3e 63 6f 75 6e 74 20        pH->count 
38a3c 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72  = 0;.      retur
38a3d 6e 20 64 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20  n data;.    }.  
38a3e 7d 0a 20 20 6e 65 77 5f 65 6c 65 6d 20 3d 20 28  }.  new_elem = (
38a3f 66 74 73 33 48 61 73 68 45 6c 65 6d 2a 29 66 74  fts3HashElem*)ft
38a40 73 33 48 61 73 68 4d 61 6c 6c 6f 63 28 20 73 69  s3HashMalloc( si
38a41 7a 65 6f 66 28 66 74 73 33 48 61 73 68 45 6c 65  zeof(fts3HashEle
38a42 6d 29 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 5f  m) );.  if( new_
38a43 65 6c 65 6d 3d 3d 30 20 29 20 72 65 74 75 72 6e  elem==0 ) return
38a44 20 64 61 74 61 3b 0a 20 20 69 66 28 20 70 48 2d   data;.  if( pH-
38a45 3e 63 6f 70 79 4b 65 79 20 26 26 20 70 4b 65 79  >copyKey && pKey
38a46 21 3d 30 20 29 7b 0a 20 20 20 20 6e 65 77 5f 65  !=0 ){.    new_e
38a47 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 66 74 73 33  lem->pKey = fts3
38a48 48 61 73 68 4d 61 6c 6c 6f 63 28 20 6e 4b 65 79  HashMalloc( nKey
38a49 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 65 77 5f   );.    if( new_
38a4a 65 6c 65 6d 2d 3e 70 4b 65 79 3d 3d 30 20 29 7b  elem->pKey==0 ){
38a4b 0a 20 20 20 20 20 20 66 74 73 33 48 61 73 68 46  .      fts3HashF
38a4c 72 65 65 28 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20  ree(new_elem);. 
38a4d 20 20 20 20 20 72 65 74 75 72 6e 20 64 61 74 61       return data
38a4e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63  ;.    }.    memc
38a4f 70 79 28 28 76 6f 69 64 2a 29 6e 65 77 5f 65 6c  py((void*)new_el
38a50 65 6d 2d 3e 70 4b 65 79 2c 20 70 4b 65 79 2c 20  em->pKey, pKey, 
38a51 6e 4b 65 79 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  nKey);.  }else{.
38a52 20 20 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b      new_elem->pK
38a53 65 79 20 3d 20 28 76 6f 69 64 2a 29 70 4b 65 79  ey = (void*)pKey
38a54 3b 0a 20 20 7d 0a 20 20 6e 65 77 5f 65 6c 65 6d  ;.  }.  new_elem
38a55 2d 3e 6e 4b 65 79 20 3d 20 6e 4b 65 79 3b 0a 20  ->nKey = nKey;. 
38a56 20 70 48 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20   pH->count++;.  
38a57 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74 20 3e 20  if( pH->count > 
38a58 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20 20  pH->htsize ){.  
38a59 20 20 66 74 73 33 52 65 68 61 73 68 28 70 48 2c    fts3Rehash(pH,
38a5a 70 48 2d 3e 68 74 73 69 7a 65 2a 32 29 3b 0a 20  pH->htsize*2);. 
38a5b 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 48 2d   }.  assert( pH-
38a5c 3e 68 74 73 69 7a 65 3e 30 20 29 3b 0a 20 20 61  >htsize>0 );.  a
38a5d 73 73 65 72 74 28 20 28 70 48 2d 3e 68 74 73 69  ssert( (pH->htsi
38a5e 7a 65 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65  ze & (pH->htsize
38a5f 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20 68 20 3d  -1))==0 );.  h =
38a60 20 68 72 61 77 20 26 20 28 70 48 2d 3e 68 74 73   hraw & (pH->hts
38a61 69 7a 65 2d 31 29 3b 0a 20 20 66 74 73 33 48 61  ize-1);.  fts3Ha
38a62 73 68 49 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28  shInsertElement(
38a63 70 48 2c 20 26 70 48 2d 3e 68 74 5b 68 5d 2c 20  pH, &pH->ht[h], 
38a64 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 6e 65 77  new_elem);.  new
38a65 5f 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61  _elem->data = da
38a66 74 61 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  ta;.  return 0;.
38a67 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  }..#endif /* !de
38a68 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
38a69 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
38a6a 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
38a6b 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
38a6c 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
38a6d 33 5f 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a  3_hash.c *******
38a6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38a6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38a70 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
38a71 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
38a72 20 66 74 73 33 5f 70 6f 72 74 65 72 2e 63 20 2a   fts3_porter.c *
38a73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38a74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38a75 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36  ****/./*.** 2006
38a76 20 53 65 70 74 65 6d 62 65 72 20 33 30 0a 2a 2a   September 30.**
38a77 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
38a78 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
38a79 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
38a7a 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
38a7b 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
38a7c 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
38a7d 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
38a7e 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
38a7f 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
38a80 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
38a81 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
38a82 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
38a83 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
38a84 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
38a85 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
38a86 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
38a87 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
38a88 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
38a89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38a8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38a8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38a8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
38a8d 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
38a8e 66 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 2d  f the full-text-
38a8f 73 65 61 72 63 68 20 74 6f 6b 65 6e 69 7a 65 72  search tokenizer
38a90 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
38a91 0a 2a 2a 20 61 20 50 6f 72 74 65 72 20 73 74 65  .** a Porter ste
38a92 6d 6d 65 72 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  mmer..*/../*.** 
38a93 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  The code in this
38a94 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63 6f   file is only co
38a95 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a  mpiled if:.**.**
38a96 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33 20       * The FTS3 
38a97 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20  module is being 
38a98 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78 74 65  built as an exte
38a99 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 28  nsion.**       (
38a9a 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51  in which case SQ
38a9b 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f 74  LITE_CORE is not
38a9c 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a   defined), or.**
38a9d 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54  .**     * The FT
38a9e 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69  S3 module is bei
38a9f 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68  ng built into th
38aa0 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20 20  e core of.**    
38aa1 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20 77 68     SQLite (in wh
38aa2 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f  ich case SQLITE_
38aa3 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73 20 64  ENABLE_FTS3 is d
38aa4 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66 20  efined)..*/.#if 
38aa5 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
38aa6 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64  CORE) || defined
38aa7 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46  (SQLITE_ENABLE_F
38aa8 54 53 33 29 0a 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 43  TS3)...../*.** C
38aa9 6c 61 73 73 20 64 65 72 69 76 65 64 20 66 72 6f  lass derived fro
38aaa 6d 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  m sqlite3_tokeni
38aab 7a 65 72 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  zer.*/.typedef s
38aac 74 72 75 63 74 20 70 6f 72 74 65 72 5f 74 6f 6b  truct porter_tok
38aad 65 6e 69 7a 65 72 20 7b 0a 20 20 73 71 6c 69 74  enizer {.  sqlit
38aae 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 62 61 73  e3_tokenizer bas
38aaf 65 3b 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20  e;      /* Base 
38ab0 63 6c 61 73 73 20 2a 2f 0a 7d 20 70 6f 72 74 65  class */.} porte
38ab1 72 5f 74 6f 6b 65 6e 69 7a 65 72 3b 0a 0a 2f 2a  r_tokenizer;../*
38ab2 0a 2a 2a 20 43 6c 61 73 73 20 64 65 72 69 76 65  .** Class derive
38ab3 64 20 66 72 6f 6d 20 73 71 6c 69 74 33 5f 74 6f  d from sqlit3_to
38ab4 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 0a 2a  kenizer_cursor.*
38ab5 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
38ab6 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65   porter_tokenize
38ab7 72 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c  r_cursor {.  sql
38ab8 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
38ab9 75 72 73 6f 72 20 62 61 73 65 3b 0a 20 20 63 6f  ursor base;.  co
38aba 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74  nst char *zInput
38abb 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e  ;          /* in
38abc 70 75 74 20 77 65 20 61 72 65 20 74 6f 6b 65 6e  put we are token
38abd 69 7a 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e  izing */.  int n
38abe 49 6e 70 75 74 3b 20 20 20 20 20 20 20 20 20 20  Input;          
38abf 20 20 20 20 20 20 20 20 2f 2a 20 73 69 7a 65 20          /* size 
38ac0 6f 66 20 74 68 65 20 69 6e 70 75 74 20 2a 2f 0a  of the input */.
38ac1 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20    int iOffset;  
38ac2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
38ac3 2a 20 63 75 72 72 65 6e 74 20 70 6f 73 69 74 69  * current positi
38ac4 6f 6e 20 69 6e 20 7a 49 6e 70 75 74 20 2a 2f 0a  on in zInput */.
38ac5 20 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 20 20 20    int iToken;   
38ac6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
38ac7 2a 20 69 6e 64 65 78 20 6f 66 20 6e 65 78 74 20  * index of next 
38ac8 74 6f 6b 65 6e 20 74 6f 20 62 65 20 72 65 74 75  token to be retu
38ac9 72 6e 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  rned */.  char *
38aca 7a 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20  zToken;         
38acb 20 20 20 20 20 20 20 2f 2a 20 73 74 6f 72 61 67         /* storag
38acc 65 20 66 6f 72 20 63 75 72 72 65 6e 74 20 74 6f  e for current to
38acd 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c  ken */.  int nAl
38ace 6c 6f 63 61 74 65 64 3b 20 20 20 20 20 20 20 20  located;        
38acf 20 20 20 20 20 20 2f 2a 20 73 70 61 63 65 20 61        /* space a
38ad0 6c 6c 6f 63 61 74 65 64 20 74 6f 20 7a 54 6f 6b  llocated to zTok
38ad1 65 6e 20 62 75 66 66 65 72 20 2a 2f 0a 7d 20 70  en buffer */.} p
38ad2 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f  orter_tokenizer_
38ad3 63 75 72 73 6f 72 3b 0a 0a 0a 2f 2a 20 46 6f 72  cursor;.../* For
38ad4 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e  ward declaration
38ad5 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74   */.static const
38ad6 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
38ad7 65 72 5f 6d 6f 64 75 6c 65 20 70 6f 72 74 65 72  er_module porter
38ad8 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b  TokenizerModule;
38ad9 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  .../*.** Create 
38ada 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65 72 20  a new tokenizer 
38adb 69 6e 73 74 61 6e 63 65 2e 0a 2a 2f 0a 73 74 61  instance..*/.sta
38adc 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72 43 72  tic int porterCr
38add 65 61 74 65 28 0a 20 20 69 6e 74 20 61 72 67 63  eate(.  int argc
38ade 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63  , const char * c
38adf 6f 6e 73 74 20 2a 61 72 67 76 2c 0a 20 20 73 71  onst *argv,.  sq
38ae0 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
38ae1 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 0a 29 7b  **ppTokenizer.){
38ae2 0a 20 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69  .  porter_tokeni
38ae3 7a 65 72 20 2a 74 3b 0a 20 20 74 20 3d 20 28 70  zer *t;.  t = (p
38ae4 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 20  orter_tokenizer 
38ae5 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  *) sqlite3_mallo
38ae6 63 28 73 69 7a 65 6f 66 28 2a 74 29 29 3b 0a 20  c(sizeof(*t));. 
38ae7 20 69 66 28 20 74 3d 3d 4e 55 4c 4c 20 29 20 72   if( t==NULL ) r
38ae8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
38ae9 45 4d 3b 0a 20 20 6d 65 6d 73 65 74 28 74 2c 20  EM;.  memset(t, 
38aea 30 2c 20 73 69 7a 65 6f 66 28 2a 74 29 29 3b 0a  0, sizeof(*t));.
38aeb 20 20 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20 3d    *ppTokenizer =
38aec 20 26 74 2d 3e 62 61 73 65 3b 0a 20 20 72 65 74   &t->base;.  ret
38aed 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
38aee 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20  ../*.** Destroy 
38aef 61 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73  a tokenizer.*/.s
38af0 74 61 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72  tatic int porter
38af1 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 5f  Destroy(sqlite3_
38af2 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65  tokenizer *pToke
38af3 6e 69 7a 65 72 29 7b 0a 20 20 73 71 6c 69 74 65  nizer){.  sqlite
38af4 33 5f 66 72 65 65 28 70 54 6f 6b 65 6e 69 7a 65  3_free(pTokenize
38af5 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  r);.  return SQL
38af6 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
38af7 20 50 72 65 70 61 72 65 20 74 6f 20 62 65 67 69   Prepare to begi
38af8 6e 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 20 70  n tokenizing a p
38af9 61 72 74 69 63 75 6c 61 72 20 73 74 72 69 6e 67  articular string
38afa 2e 20 20 54 68 65 20 69 6e 70 75 74 0a 2a 2a 20  .  The input.** 
38afb 73 74 72 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b  string to be tok
38afc 65 6e 69 7a 65 64 20 69 73 20 7a 49 6e 70 75 74  enized is zInput
38afd 5b 30 2e 2e 6e 49 6e 70 75 74 2d 31 5d 2e 20 20  [0..nInput-1].  
38afe 41 20 63 75 72 73 6f 72 0a 2a 2a 20 75 73 65 64  A cursor.** used
38aff 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 61 6c 6c   to incrementall
38b00 79 20 74 6f 6b 65 6e 69 7a 65 20 74 68 69 73 20  y tokenize this 
38b01 73 74 72 69 6e 67 20 69 73 20 72 65 74 75 72 6e  string is return
38b02 65 64 20 69 6e 20 0a 2a 2a 20 2a 70 70 43 75 72  ed in .** *ppCur
38b03 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  sor..*/.static i
38b04 6e 74 20 70 6f 72 74 65 72 4f 70 65 6e 28 0a 20  nt porterOpen(. 
38b05 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
38b06 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20  er *pTokenizer, 
38b07 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
38b08 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 63 6f  okenizer */.  co
38b09 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74  nst char *zInput
38b0a 2c 20 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20 20  , int nInput,   
38b0b 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 74       /* String t
38b0c 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20 2a  o be tokenized *
38b0d 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  /.  sqlite3_toke
38b0e 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 2a 70  nizer_cursor **p
38b0f 70 43 75 72 73 6f 72 20 20 20 20 2f 2a 20 4f 55  pCursor    /* OU
38b10 54 3a 20 54 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20  T: Tokenization 
38b11 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 70  cursor */.){.  p
38b12 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f  orter_tokenizer_
38b13 63 75 72 73 6f 72 20 2a 63 3b 0a 0a 20 20 63 20  cursor *c;..  c 
38b14 3d 20 28 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69  = (porter_tokeni
38b15 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 20 73 71  zer_cursor *) sq
38b16 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
38b17 65 6f 66 28 2a 63 29 29 3b 0a 20 20 69 66 28 20  eof(*c));.  if( 
38b18 63 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e  c==NULL ) return
38b19 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a   SQLITE_NOMEM;..
38b1a 20 20 63 2d 3e 7a 49 6e 70 75 74 20 3d 20 7a 49    c->zInput = zI
38b1b 6e 70 75 74 3b 0a 20 20 69 66 28 20 7a 49 6e 70  nput;.  if( zInp
38b1c 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 63 2d 3e  ut==0 ){.    c->
38b1d 6e 49 6e 70 75 74 20 3d 20 30 3b 0a 20 20 7d 65  nInput = 0;.  }e
38b1e 6c 73 65 20 69 66 28 20 6e 49 6e 70 75 74 3c 30  lse if( nInput<0
38b1f 20 29 7b 0a 20 20 20 20 63 2d 3e 6e 49 6e 70 75   ){.    c->nInpu
38b20 74 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28  t = (int)strlen(
38b21 7a 49 6e 70 75 74 29 3b 0a 20 20 7d 65 6c 73 65  zInput);.  }else
38b22 7b 0a 20 20 20 20 63 2d 3e 6e 49 6e 70 75 74 20  {.    c->nInput 
38b23 3d 20 6e 49 6e 70 75 74 3b 0a 20 20 7d 0a 20 20  = nInput;.  }.  
38b24 63 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 20  c->iOffset = 0; 
38b25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38b26 2f 2a 20 73 74 61 72 74 20 74 6f 6b 65 6e 69 7a  /* start tokeniz
38b27 69 6e 67 20 61 74 20 74 68 65 20 62 65 67 69 6e  ing at the begin
38b28 6e 69 6e 67 20 2a 2f 0a 20 20 63 2d 3e 69 54 6f  ning */.  c->iTo
38b29 6b 65 6e 20 3d 20 30 3b 0a 20 20 63 2d 3e 7a 54  ken = 0;.  c->zT
38b2a 6f 6b 65 6e 20 3d 20 4e 55 4c 4c 3b 20 20 20 20  oken = NULL;    
38b2b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f             /* no
38b2c 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64   space allocated
38b2d 2c 20 79 65 74 2e 20 2a 2f 0a 20 20 63 2d 3e 6e  , yet. */.  c->n
38b2e 41 6c 6c 6f 63 61 74 65 64 20 3d 20 30 3b 0a 0a  Allocated = 0;..
38b2f 20 20 2a 70 70 43 75 72 73 6f 72 20 3d 20 26 63    *ppCursor = &c
38b30 2d 3e 62 61 73 65 3b 0a 20 20 72 65 74 75 72 6e  ->base;.  return
38b31 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
38b32 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 74 6f 6b  *.** Close a tok
38b33 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72  enization cursor
38b34 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e   previously open
38b35 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a  ed by a call to.
38b36 2a 2a 20 70 6f 72 74 65 72 4f 70 65 6e 28 29 20  ** porterOpen() 
38b37 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  above..*/.static
38b38 20 69 6e 74 20 70 6f 72 74 65 72 43 6c 6f 73 65   int porterClose
38b39 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a  (sqlite3_tokeniz
38b3a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  er_cursor *pCurs
38b3b 6f 72 29 7b 0a 20 20 70 6f 72 74 65 72 5f 74 6f  or){.  porter_to
38b3c 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
38b3d 63 20 3d 20 28 70 6f 72 74 65 72 5f 74 6f 6b 65  c = (porter_toke
38b3e 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 20  nizer_cursor *) 
38b3f 70 43 75 72 73 6f 72 3b 0a 20 20 73 71 6c 69 74  pCursor;.  sqlit
38b40 65 33 5f 66 72 65 65 28 63 2d 3e 7a 54 6f 6b 65  e3_free(c->zToke
38b41 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  n);.  sqlite3_fr
38b42 65 65 28 63 29 3b 0a 20 20 72 65 74 75 72 6e 20  ee(c);.  return 
38b43 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 2f 2a 0a  SQLITE_OK;.}./*.
38b44 2a 2a 20 56 6f 77 65 6c 20 6f 72 20 63 6f 6e 73  ** Vowel or cons
38b45 6f 6e 61 6e 74 0a 2a 2f 0a 73 74 61 74 69 63 20  onant.*/.static 
38b46 63 6f 6e 73 74 20 63 68 61 72 20 63 54 79 70 65  const char cType
38b47 5b 5d 20 3d 20 7b 0a 20 20 20 30 2c 20 31 2c 20  [] = {.   0, 1, 
38b48 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31  1, 1, 0, 1, 1, 1
38b49 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 0, 1, 1, 1, 1,
38b4a 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 0, 1, 1, 1, 
38b4b 31 2c 20 31 2c 20 30 2c 0a 20 20 20 31 2c 20 31  1, 1, 0,.   1, 1
38b4c 2c 20 31 2c 20 32 2c 20 31 0a 7d 3b 0a 0a 2f 2a  , 1, 2, 1.};../*
38b4d 0a 2a 2a 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28  .** isConsonant(
38b4e 29 20 61 6e 64 20 69 73 56 6f 77 65 6c 28 29 20  ) and isVowel() 
38b4f 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65  determine if the
38b50 69 72 20 66 69 72 73 74 20 63 68 61 72 61 63 74  ir first charact
38b51 65 72 20 69 6e 0a 2a 2a 20 74 68 65 20 73 74 72  er in.** the str
38b52 69 6e 67 20 74 68 65 79 20 70 6f 69 6e 74 20 74  ing they point t
38b53 6f 20 69 73 20 61 20 63 6f 6e 73 6f 6e 61 6e 74  o is a consonant
38b54 20 6f 72 20 61 20 76 6f 77 65 6c 2c 20 61 63 63   or a vowel, acc
38b55 6f 72 64 69 6e 67 0a 2a 2a 20 74 6f 20 50 6f 72  ording.** to Por
38b56 74 65 72 20 72 75 6c 73 2e 20 20 0a 2a 2a 0a 2a  ter ruls.  .**.*
38b57 2a 20 41 20 63 6f 6e 73 6f 6e 61 74 65 20 69 73  * A consonate is
38b58 20 61 6e 79 20 6c 65 74 74 65 72 20 6f 74 68 65   any letter othe
38b59 72 20 74 68 61 6e 20 27 61 27 2c 20 27 65 27 2c  r than 'a', 'e',
38b5a 20 27 69 27 2c 20 27 6f 27 2c 20 6f 72 20 27 75   'i', 'o', or 'u
38b5b 27 2e 0a 2a 2a 20 27 59 27 20 69 73 20 61 20 63  '..** 'Y' is a c
38b5c 6f 6e 73 6f 6e 61 6e 74 20 75 6e 6c 65 73 73 20  onsonant unless 
38b5d 69 74 20 66 6f 6c 6c 6f 77 73 20 61 6e 6f 74 68  it follows anoth
38b5e 65 72 20 63 6f 6e 73 6f 6e 61 6e 74 2c 0a 2a 2a  er consonant,.**
38b5f 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 69   in which case i
38b60 74 20 69 73 20 61 20 76 6f 77 65 6c 2e 0a 2a 2a  t is a vowel..**
38b61 0a 2a 2a 20 49 6e 20 74 68 65 73 65 20 72 6f 75  .** In these rou
38b62 74 69 6e 65 2c 20 74 68 65 20 6c 65 74 74 65 72  tine, the letter
38b63 73 20 61 72 65 20 69 6e 20 72 65 76 65 72 73 65  s are in reverse
38b64 20 6f 72 64 65 72 2e 20 20 53 6f 20 74 68 65 20   order.  So the 
38b65 27 79 27 20 72 75 6c 65 0a 2a 2a 20 69 73 20 74  'y' rule.** is t
38b66 68 61 74 20 27 79 27 20 69 73 20 61 20 63 6f 6e  hat 'y' is a con
38b67 73 6f 6e 61 6e 74 20 75 6e 6c 65 73 73 20 69 74  sonant unless it
38b68 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20   is followed by 
38b69 61 6e 6f 74 68 65 72 0a 2a 2a 20 63 6f 6e 73 6f  another.** conso
38b6a 6e 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  nent..*/.static 
38b6b 69 6e 74 20 69 73 56 6f 77 65 6c 28 63 6f 6e 73  int isVowel(cons
38b6c 74 20 63 68 61 72 2a 29 3b 0a 73 74 61 74 69 63  t char*);.static
38b6d 20 69 6e 74 20 69 73 43 6f 6e 73 6f 6e 61 6e 74   int isConsonant
38b6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
38b6f 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 63 68 61 72  .  int j;.  char
38b70 20 78 20 3d 20 2a 7a 3b 0a 20 20 69 66 28 20 78   x = *z;.  if( x
38b71 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
38b72 20 20 61 73 73 65 72 74 28 20 78 3e 3d 27 61 27    assert( x>='a'
38b73 20 26 26 20 78 3c 3d 27 7a 27 20 29 3b 0a 20 20   && x<='z' );.  
38b74 6a 20 3d 20 63 54 79 70 65 5b 78 2d 27 61 27 5d  j = cType[x-'a']
38b75 3b 0a 20 20 69 66 28 20 6a 3c 32 20 29 20 72 65  ;.  if( j<2 ) re
38b76 74 75 72 6e 20 6a 3b 0a 20 20 72 65 74 75 72 6e  turn j;.  return
38b77 20 7a 5b 31 5d 3d 3d 30 20 7c 7c 20 69 73 56 6f   z[1]==0 || isVo
38b78 77 65 6c 28 7a 20 2b 20 31 29 3b 0a 7d 0a 73 74  wel(z + 1);.}.st
38b79 61 74 69 63 20 69 6e 74 20 69 73 56 6f 77 65 6c  atic int isVowel
38b7a 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
38b7b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 63 68 61 72  .  int j;.  char
38b7c 20 78 20 3d 20 2a 7a 3b 0a 20 20 69 66 28 20 78   x = *z;.  if( x
38b7d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
38b7e 20 20 61 73 73 65 72 74 28 20 78 3e 3d 27 61 27    assert( x>='a'
38b7f 20 26 26 20 78 3c 3d 27 7a 27 20 29 3b 0a 20 20   && x<='z' );.  
38b80 6a 20 3d 20 63 54 79 70 65 5b 78 2d 27 61 27 5d  j = cType[x-'a']
38b81 3b 0a 20 20 69 66 28 20 6a 3c 32 20 29 20 72 65  ;.  if( j<2 ) re
38b82 74 75 72 6e 20 31 2d 6a 3b 0a 20 20 72 65 74 75  turn 1-j;.  retu
38b83 72 6e 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a  rn isConsonant(z
38b84 20 2b 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   + 1);.}../*.** 
38b85 4c 65 74 20 61 6e 79 20 73 65 71 75 65 6e 63 65  Let any sequence
38b86 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20   of one or more 
38b87 76 6f 77 65 6c 73 20 62 65 20 72 65 70 72 65 73  vowels be repres
38b88 65 6e 74 65 64 20 62 79 20 56 20 61 6e 64 20 6c  ented by V and l
38b89 65 74 0a 2a 2a 20 43 20 62 65 20 73 65 71 75 65  et.** C be seque
38b8a 6e 63 65 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f  nce of one or mo
38b8b 72 65 20 63 6f 6e 73 6f 6e 61 6e 74 73 2e 20 20  re consonants.  
38b8c 54 68 65 6e 20 65 76 65 72 79 20 77 6f 72 64 20  Then every word 
38b8d 63 61 6e 20 62 65 0a 2a 2a 20 72 65 70 72 65 73  can be.** repres
38b8e 65 6e 74 65 64 20 61 73 3a 0a 2a 2a 0a 2a 2a 20  ented as:.**.** 
38b8f 20 20 20 20 20 20 20 20 20 20 5b 43 5d 20 28 56            [C] (V
38b90 43 29 7b 6d 7d 20 5b 56 5d 0a 2a 2a 0a 2a 2a 20  C){m} [V].**.** 
38b91 49 6e 20 70 72 6f 73 65 3a 20 20 41 20 77 6f 72  In prose:  A wor
38b92 64 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c  d is an optional
38b93 20 63 6f 6e 73 6f 6e 61 6e 74 20 66 6f 6c 6c 6f   consonant follo
38b94 77 65 64 20 62 79 20 7a 65 72 6f 20 6f 72 0a 2a  wed by zero or.*
38b95 2a 20 76 6f 77 65 6c 2d 63 6f 6e 73 6f 6e 61 6e  * vowel-consonan
38b96 74 20 70 61 69 72 73 20 66 6f 6c 6c 6f 77 65 64  t pairs followed
38b97 20 62 79 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20   by an optional 
38b98 76 6f 77 65 6c 2e 20 20 22 6d 22 20 69 73 20 74  vowel.  "m" is t
38b99 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  he.** number of 
38b9a 76 6f 77 65 6c 20 63 6f 6e 73 6f 6e 61 6e 74 20  vowel consonant 
38b9b 70 61 69 72 73 2e 20 20 54 68 69 73 20 72 6f 75  pairs.  This rou
38b9c 74 69 6e 65 20 63 6f 6d 70 75 74 65 73 20 74 68  tine computes th
38b9d 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 6d 20  e value.** of m 
38b9e 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 69 20  for the first i 
38b9f 62 79 74 65 73 20 6f 66 20 61 20 77 6f 72 64 2e  bytes of a word.
38ba0 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  .**.** Return tr
38ba1 75 65 20 69 66 20 74 68 65 20 6d 2d 76 61 6c 75  ue if the m-valu
38ba2 65 20 66 6f 72 20 7a 20 69 73 20 31 20 6f 72 20  e for z is 1 or 
38ba3 6d 6f 72 65 2e 20 20 49 6e 20 6f 74 68 65 72 20  more.  In other 
38ba4 77 6f 72 64 73 2c 0a 2a 2a 20 72 65 74 75 72 6e  words,.** return
38ba5 20 74 72 75 65 20 69 66 20 7a 20 63 6f 6e 74 61   true if z conta
38ba6 69 6e 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65  ins at least one
38ba7 20 76 6f 77 65 6c 20 74 68 61 74 20 69 73 20 66   vowel that is f
38ba8 6f 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20 61 20  ollowed.** by a 
38ba9 63 6f 6e 73 6f 6e 61 6e 74 2e 0a 2a 2a 0a 2a 2a  consonant..**.**
38baa 20 49 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65   In this routine
38bab 20 7a 5b 5d 20 69 73 20 69 6e 20 72 65 76 65 72   z[] is in rever
38bac 73 65 20 6f 72 64 65 72 2e 20 20 53 6f 20 77 65  se order.  So we
38bad 20 61 72 65 20 72 65 61 6c 6c 79 20 6c 6f 6f 6b   are really look
38bae 69 6e 67 0a 2a 2a 20 66 6f 72 20 61 6e 20 69 6e  ing.** for an in
38baf 73 74 61 6e 63 65 20 6f 66 20 6f 66 20 61 20 63  stance of of a c
38bb0 6f 6e 73 6f 6e 61 6e 74 20 66 6f 6c 6c 6f 77 65  onsonant followe
38bb1 64 20 62 79 20 61 20 76 6f 77 65 6c 2e 0a 2a 2f  d by a vowel..*/
38bb2 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 5f 67 74  .static int m_gt
38bb3 5f 30 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  _0(const char *z
38bb4 29 7b 0a 20 20 77 68 69 6c 65 28 20 69 73 56 6f  ){.  while( isVo
38bb5 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d  wel(z) ){ z++; }
38bb6 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72  .  if( *z==0 ) r
38bb7 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65  eturn 0;.  while
38bb8 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29  ( isConsonant(z)
38bb9 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74   ){ z++; }.  ret
38bba 75 72 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a 0a 2f 2a  urn *z!=0;.}../*
38bbb 20 4c 69 6b 65 20 6d 67 74 30 20 61 62 6f 76 65   Like mgt0 above
38bbc 20 65 78 63 65 70 74 20 77 65 20 61 72 65 20 6c   except we are l
38bbd 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 76 61 6c  ooking for a val
38bbe 75 65 20 6f 66 20 6d 20 77 68 69 63 68 20 69 73  ue of m which is
38bbf 0a 2a 2a 20 65 78 61 63 74 6c 79 20 31 0a 2a 2f  .** exactly 1.*/
38bc0 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 5f 65 71  .static int m_eq
38bc1 5f 31 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  _1(const char *z
38bc2 29 7b 0a 20 20 77 68 69 6c 65 28 20 69 73 56 6f  ){.  while( isVo
38bc3 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d  wel(z) ){ z++; }
38bc4 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72  .  if( *z==0 ) r
38bc5 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65  eturn 0;.  while
38bc6 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29  ( isConsonant(z)
38bc7 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28   ){ z++; }.  if(
38bc8 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20   *z==0 ) return 
38bc9 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 56 6f  0;.  while( isVo
38bca 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d  wel(z) ){ z++; }
38bcb 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72  .  if( *z==0 ) r
38bcc 65 74 75 72 6e 20 31 3b 0a 20 20 77 68 69 6c 65  eturn 1;.  while
38bcd 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29  ( isConsonant(z)
38bce 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74   ){ z++; }.  ret
38bcf 75 72 6e 20 2a 7a 3d 3d 30 3b 0a 7d 0a 0a 2f 2a  urn *z==0;.}../*
38bd0 20 4c 69 6b 65 20 6d 67 74 30 20 61 62 6f 76 65   Like mgt0 above
38bd1 20 65 78 63 65 70 74 20 77 65 20 61 72 65 20 6c   except we are l
38bd2 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 76 61 6c  ooking for a val
38bd3 75 65 20 6f 66 20 6d 3e 31 20 69 6e 73 74 65 61  ue of m>1 instea
38bd4 64 0a 2a 2a 20 6f 72 20 6d 3e 30 0a 2a 2f 0a 73  d.** or m>0.*/.s
38bd5 74 61 74 69 63 20 69 6e 74 20 6d 5f 67 74 5f 31  tatic int m_gt_1
38bd6 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
38bd7 0a 20 20 77 68 69 6c 65 28 20 69 73 56 6f 77 65  .  while( isVowe
38bd8 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20  l(z) ){ z++; }. 
38bd9 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74   if( *z==0 ) ret
38bda 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20  urn 0;.  while( 
38bdb 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29  isConsonant(z) )
38bdc 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a  { z++; }.  if( *
38bdd 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  z==0 ) return 0;
38bde 0a 20 20 77 68 69 6c 65 28 20 69 73 56 6f 77 65  .  while( isVowe
38bdf 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20  l(z) ){ z++; }. 
38be0 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74   if( *z==0 ) ret
38be1 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20  urn 0;.  while( 
38be2 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29  isConsonant(z) )
38be3 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72  { z++; }.  retur
38be4 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  n *z!=0;.}../*.*
38be5 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
38be6 20 74 68 65 72 65 20 69 73 20 61 20 76 6f 77 65   there is a vowe
38be7 6c 20 61 6e 79 77 68 65 72 65 20 77 69 74 68 69  l anywhere withi
38be8 6e 20 7a 5b 30 2e 2e 6e 2d 31 5d 0a 2a 2f 0a 73  n z[0..n-1].*/.s
38be9 74 61 74 69 63 20 69 6e 74 20 68 61 73 56 6f 77  tatic int hasVow
38bea 65 6c 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  el(const char *z
38beb 29 7b 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f  ){.  while( isCo
38bec 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b  nsonant(z) ){ z+
38bed 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a  +; }.  return *z
38bee 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  !=0;.}../*.** Re
38bef 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65  turn TRUE if the
38bf0 20 77 6f 72 64 20 65 6e 64 73 20 69 6e 20 61 20   word ends in a 
38bf1 64 6f 75 62 6c 65 20 63 6f 6e 73 6f 6e 61 6e 74  double consonant
38bf2 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65 78 74  ..**.** The text
38bf3 20 69 73 20 72 65 76 65 72 73 65 64 20 68 65 72   is reversed her
38bf4 65 2e 20 53 6f 20 77 65 20 61 72 65 20 72 65 61  e. So we are rea
38bf5 6c 6c 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 0a 2a  lly looking at.*
38bf6 2a 20 74 68 65 20 66 69 72 73 74 20 74 77 6f 20  * the first two 
38bf7 63 68 61 72 61 63 74 65 72 73 20 6f 66 20 7a 5b  characters of z[
38bf8 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  ]..*/.static int
38bf9 20 64 6f 75 62 6c 65 43 6f 6e 73 6f 6e 61 6e 74   doubleConsonant
38bfa 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
38bfb 0a 20 20 72 65 74 75 72 6e 20 69 73 43 6f 6e 73  .  return isCons
38bfc 6f 6e 61 6e 74 28 7a 29 20 26 26 20 7a 5b 30 5d  onant(z) && z[0]
38bfd 3d 3d 7a 5b 31 5d 20 26 26 20 69 73 43 6f 6e 73  ==z[1] && isCons
38bfe 6f 6e 61 6e 74 28 7a 2b 31 29 3b 0a 7d 0a 0a 2f  onant(z+1);.}../
38bff 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  *.** Return TRUE
38c00 20 69 66 20 74 68 65 20 77 6f 72 64 20 65 6e 64   if the word end
38c01 73 20 77 69 74 68 20 74 68 72 65 65 20 6c 65 74  s with three let
38c02 74 65 72 73 20 77 68 69 63 68 0a 2a 2a 20 61 72  ters which.** ar
38c03 65 20 63 6f 6e 73 6f 6e 61 6e 74 2d 76 6f 77 65  e consonant-vowe
38c04 6c 2d 63 6f 6e 73 6f 6e 65 6e 74 20 61 6e 64 20  l-consonent and 
38c05 77 68 65 72 65 20 74 68 65 20 66 69 6e 61 6c 20  where the final 
38c06 63 6f 6e 73 6f 6e 61 6e 74 0a 2a 2a 20 69 73 20  consonant.** is 
38c07 6e 6f 74 20 27 77 27 2c 20 27 78 27 2c 20 6f 72  not 'w', 'x', or
38c08 20 27 79 27 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20   'y'..**.** The 
38c09 77 6f 72 64 20 69 73 20 72 65 76 65 72 73 65 64  word is reversed
38c0a 20 68 65 72 65 2e 20 20 53 6f 20 77 65 20 61 72   here.  So we ar
38c0b 65 20 72 65 61 6c 6c 79 20 63 68 65 63 6b 69 6e  e really checkin
38c0c 67 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 74  g the.** first t
38c0d 68 72 65 65 20 6c 65 74 74 65 72 73 20 61 6e 64  hree letters and
38c0e 20 74 68 65 20 66 69 72 73 74 20 6f 6e 65 20 63   the first one c
38c0f 61 6e 6e 6f 74 20 62 65 20 69 6e 20 5b 77 78 79  annot be in [wxy
38c10 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  ]..*/.static int
38c11 20 73 74 61 72 5f 6f 68 28 63 6f 6e 73 74 20 63   star_oh(const c
38c12 68 61 72 20 2a 7a 29 7b 0a 20 20 72 65 74 75 72  har *z){.  retur
38c13 6e 0a 20 20 20 20 7a 5b 30 5d 21 3d 30 20 26 26  n.    z[0]!=0 &&
38c14 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20   isConsonant(z) 
38c15 26 26 0a 20 20 20 20 7a 5b 30 5d 21 3d 27 77 27  &&.    z[0]!='w'
38c16 20 26 26 20 7a 5b 30 5d 21 3d 27 78 27 20 26 26   && z[0]!='x' &&
38c17 20 7a 5b 30 5d 21 3d 27 79 27 20 26 26 0a 20 20   z[0]!='y' &&.  
38c18 20 20 7a 5b 31 5d 21 3d 30 20 26 26 20 69 73 56    z[1]!=0 && isV
38c19 6f 77 65 6c 28 7a 2b 31 29 20 26 26 0a 20 20 20  owel(z+1) &&.   
38c1a 20 7a 5b 32 5d 21 3d 30 20 26 26 20 69 73 43 6f   z[2]!=0 && isCo
38c1b 6e 73 6f 6e 61 6e 74 28 7a 2b 32 29 3b 0a 7d 0a  nsonant(z+2);.}.
38c1c 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 77 6f  ./*.** If the wo
38c1d 72 64 20 65 6e 64 73 20 77 69 74 68 20 7a 46 72  rd ends with zFr
38c1e 6f 6d 20 61 6e 64 20 78 43 6f 6e 64 28 29 20 69  om and xCond() i
38c1f 73 20 74 72 75 65 20 66 6f 72 20 74 68 65 20 73  s true for the s
38c20 74 65 6d 0a 2a 2a 20 6f 66 20 74 68 65 20 77 6f  tem.** of the wo
38c21 72 64 20 74 68 61 74 20 70 72 65 63 65 65 64 73  rd that preceeds
38c22 20 74 68 65 20 7a 46 72 6f 6d 20 65 6e 64 69 6e   the zFrom endin
38c23 67 2c 20 74 68 65 6e 20 63 68 61 6e 67 65 20 74  g, then change t
38c24 68 65 20 0a 2a 2a 20 65 6e 64 69 6e 67 20 74 6f  he .** ending to
38c25 20 7a 54 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20   zTo..**.** The 
38c26 69 6e 70 75 74 20 77 6f 72 64 20 2a 70 7a 20 61  input word *pz a
38c27 6e 64 20 7a 46 72 6f 6d 20 61 72 65 20 62 6f 74  nd zFrom are bot
38c28 68 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64  h in reverse ord
38c29 65 72 2e 20 20 7a 54 6f 0a 2a 2a 20 69 73 20 69  er.  zTo.** is i
38c2a 6e 20 6e 6f 72 6d 61 6c 20 6f 72 64 65 72 2e 20  n normal order. 
38c2b 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  .**.** Return TR
38c2c 55 45 20 69 66 20 7a 46 72 6f 6d 20 6d 61 74 63  UE if zFrom matc
38c2d 68 65 73 2e 20 20 52 65 74 75 72 6e 20 46 41 4c  hes.  Return FAL
38c2e 53 45 20 69 66 20 7a 46 72 6f 6d 20 64 6f 65 73  SE if zFrom does
38c2f 20 6e 6f 74 0a 2a 2a 20 6d 61 74 63 68 2e 20 20   not.** match.  
38c30 4e 6f 74 20 74 68 61 74 20 54 52 55 45 20 69 73  Not that TRUE is
38c31 20 72 65 74 75 72 6e 65 64 20 65 76 65 6e 20 69   returned even i
38c32 66 20 78 43 6f 6e 64 28 29 20 66 61 69 6c 73 20  f xCond() fails 
38c33 61 6e 64 0a 2a 2a 20 6e 6f 20 73 75 62 73 74 69  and.** no substi
38c34 74 75 74 69 6f 6e 20 6f 63 63 75 72 73 2e 0a 2a  tution occurs..*
38c35 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 74 65  /.static int ste
38c36 6d 28 0a 20 20 63 68 61 72 20 2a 2a 70 7a 2c 20  m(.  char **pz, 
38c37 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
38c38 68 65 20 77 6f 72 64 20 62 65 69 6e 67 20 73 74  he word being st
38c39 65 6d 6d 65 64 20 28 52 65 76 65 72 73 65 64 29  emmed (Reversed)
38c3a 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
38c3b 20 2a 7a 46 72 6f 6d 2c 20 20 20 20 20 2f 2a 20   *zFrom,     /* 
38c3c 49 66 20 74 68 65 20 65 6e 64 69 6e 67 20 6d 61  If the ending ma
38c3d 74 63 68 65 73 20 74 68 69 73 2e 2e 2e 20 28 52  tches this... (R
38c3e 65 76 65 72 73 65 64 29 20 2a 2f 0a 20 20 63 6f  eversed) */.  co
38c3f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 2c 20 20  nst char *zTo,  
38c40 20 20 20 20 20 2f 2a 20 2e 2e 2e 20 63 68 61 6e       /* ... chan
38c41 67 65 20 74 68 65 20 65 6e 64 69 6e 67 20 74 6f  ge the ending to
38c42 20 74 68 69 73 20 28 6e 6f 74 20 72 65 76 65 72   this (not rever
38c43 73 65 64 29 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  sed) */.  int (*
38c44 78 43 6f 6e 64 29 28 63 6f 6e 73 74 20 63 68 61  xCond)(const cha
38c45 72 2a 29 20 20 20 2f 2a 20 43 6f 6e 64 69 74 69  r*)   /* Conditi
38c46 6f 6e 20 74 68 61 74 20 6d 75 73 74 20 62 65 20  on that must be 
38c47 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 63 68 61  true */.){.  cha
38c48 72 20 2a 7a 20 3d 20 2a 70 7a 3b 0a 20 20 77 68  r *z = *pz;.  wh
38c49 69 6c 65 28 20 2a 7a 46 72 6f 6d 20 26 26 20 2a  ile( *zFrom && *
38c4a 7a 46 72 6f 6d 3d 3d 2a 7a 20 29 7b 20 7a 2b 2b  zFrom==*z ){ z++
38c4b 3b 20 7a 46 72 6f 6d 2b 2b 3b 20 7d 0a 20 20 69  ; zFrom++; }.  i
38c4c 66 28 20 2a 7a 46 72 6f 6d 21 3d 30 20 29 20 72  f( *zFrom!=0 ) r
38c4d 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 78  eturn 0;.  if( x
38c4e 43 6f 6e 64 20 26 26 20 21 78 43 6f 6e 64 28 7a  Cond && !xCond(z
38c4f 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  ) ) return 1;.  
38c50 77 68 69 6c 65 28 20 2a 7a 54 6f 20 29 7b 0a 20  while( *zTo ){. 
38c51 20 20 20 2a 28 2d 2d 7a 29 20 3d 20 2a 28 7a 54     *(--z) = *(zT
38c52 6f 2b 2b 29 3b 0a 20 20 7d 0a 20 20 2a 70 7a 20  o++);.  }.  *pz 
38c53 3d 20 7a 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  = z;.  return 1;
38c54 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69  .}../*.** This i
38c55 73 20 74 68 65 20 66 61 6c 6c 62 61 63 6b 20 73  s the fallback s
38c56 74 65 6d 6d 65 72 20 75 73 65 64 20 77 68 65 6e  temmer used when
38c57 20 74 68 65 20 70 6f 72 74 65 72 20 73 74 65 6d   the porter stem
38c58 6d 65 72 20 69 73 0a 2a 2a 20 69 6e 61 70 70 72  mer is.** inappr
38c59 6f 70 72 69 61 74 65 2e 20 20 54 68 65 20 69 6e  opriate.  The in
38c5a 70 75 74 20 77 6f 72 64 20 69 73 20 63 6f 70 69  put word is copi
38c5b 65 64 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70  ed into the outp
38c5c 75 74 20 77 69 74 68 0a 2a 2a 20 55 53 2d 41 53  ut with.** US-AS
38c5d 43 49 49 20 63 61 73 65 20 66 6f 6c 64 69 6e 67  CII case folding
38c5e 2e 20 20 49 66 20 74 68 65 20 69 6e 70 75 74 20  .  If the input 
38c5f 77 6f 72 64 20 69 73 20 74 6f 6f 20 6c 6f 6e 67  word is too long
38c60 20 28 6d 6f 72 65 0a 2a 2a 20 74 68 61 6e 20 32   (more.** than 2
38c61 30 20 62 79 74 65 73 20 69 66 20 69 74 20 63 6f  0 bytes if it co
38c62 6e 74 61 69 6e 73 20 6e 6f 20 64 69 67 69 74 73  ntains no digits
38c63 20 6f 72 20 6d 6f 72 65 20 74 68 61 6e 20 36 20   or more than 6 
38c64 62 79 74 65 73 20 69 66 0a 2a 2a 20 69 74 20 63  bytes if.** it c
38c65 6f 6e 74 61 69 6e 73 20 64 69 67 69 74 73 29 20  ontains digits) 
38c66 74 68 65 6e 20 77 6f 72 64 20 69 73 20 74 72 75  then word is tru
38c67 6e 63 61 74 65 64 20 74 6f 20 32 30 20 6f 72 20  ncated to 20 or 
38c68 36 20 62 79 74 65 73 0a 2a 2a 20 62 79 20 74 61  6 bytes.** by ta
38c69 6b 69 6e 67 20 31 30 20 6f 72 20 33 20 62 79 74  king 10 or 3 byt
38c6a 65 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69  es from the begi
38c6b 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 2e 0a 2a  nning and end..*
38c6c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  /.static void co
38c6d 70 79 5f 73 74 65 6d 6d 65 72 28 63 6f 6e 73 74  py_stemmer(const
38c6e 20 63 68 61 72 20 2a 7a 49 6e 2c 20 69 6e 74 20   char *zIn, int 
38c6f 6e 49 6e 2c 20 63 68 61 72 20 2a 7a 4f 75 74 2c  nIn, char *zOut,
38c70 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a 20 20   int *pnOut){.  
38c71 69 6e 74 20 69 2c 20 6d 78 2c 20 6a 3b 0a 20 20  int i, mx, j;.  
38c72 69 6e 74 20 68 61 73 44 69 67 69 74 20 3d 20 30  int hasDigit = 0
38c73 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  ;.  for(i=0; i<n
38c74 49 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e  In; i++){.    in
38c75 74 20 63 20 3d 20 7a 49 6e 5b 69 5d 3b 0a 20 20  t c = zIn[i];.  
38c76 20 20 69 66 28 20 63 3e 3d 27 41 27 20 26 26 20    if( c>='A' && 
38c77 63 3c 3d 27 5a 27 20 29 7b 0a 20 20 20 20 20 20  c<='Z' ){.      
38c78 7a 4f 75 74 5b 69 5d 20 3d 20 63 20 2d 20 27 41  zOut[i] = c - 'A
38c79 27 20 2b 20 27 61 27 3b 0a 20 20 20 20 7d 65 6c  ' + 'a';.    }el
38c7a 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 63 3e  se{.      if( c>
38c7b 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29  ='0' && c<='9' )
38c7c 20 68 61 73 44 69 67 69 74 20 3d 20 31 3b 0a 20   hasDigit = 1;. 
38c7d 20 20 20 20 20 7a 4f 75 74 5b 69 5d 20 3d 20 63       zOut[i] = c
38c7e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 78  ;.    }.  }.  mx
38c7f 20 3d 20 68 61 73 44 69 67 69 74 20 3f 20 33 20   = hasDigit ? 3 
38c80 3a 20 31 30 3b 0a 20 20 69 66 28 20 6e 49 6e 3e  : 10;.  if( nIn>
38c81 6d 78 2a 32 20 29 7b 0a 20 20 20 20 66 6f 72 28  mx*2 ){.    for(
38c82 6a 3d 6d 78 2c 20 69 3d 6e 49 6e 2d 6d 78 3b 20  j=mx, i=nIn-mx; 
38c83 69 3c 6e 49 6e 3b 20 69 2b 2b 2c 20 6a 2b 2b 29  i<nIn; i++, j++)
38c84 7b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 6a 5d 20  {.      zOut[j] 
38c85 3d 20 7a 4f 75 74 5b 69 5d 3b 0a 20 20 20 20 7d  = zOut[i];.    }
38c86 0a 20 20 20 20 69 20 3d 20 6a 3b 0a 20 20 7d 0a  .    i = j;.  }.
38c87 20 20 7a 4f 75 74 5b 69 5d 20 3d 20 30 3b 0a 20    zOut[i] = 0;. 
38c88 20 2a 70 6e 4f 75 74 20 3d 20 69 3b 0a 7d 0a 0a   *pnOut = i;.}..
38c89 0a 2f 2a 0a 2a 2a 20 53 74 65 6d 20 74 68 65 20  ./*.** Stem the 
38c8a 69 6e 70 75 74 20 77 6f 72 64 20 7a 49 6e 5b 30  input word zIn[0
38c8b 2e 2e 6e 49 6e 2d 31 5d 2e 20 20 53 74 6f 72 65  ..nIn-1].  Store
38c8c 20 74 68 65 20 6f 75 74 70 75 74 20 69 6e 20 7a   the output in z
38c8d 4f 75 74 2e 0a 2a 2a 20 7a 4f 75 74 20 69 73 20  Out..** zOut is 
38c8e 61 74 20 6c 65 61 73 74 20 62 69 67 20 65 6e 6f  at least big eno
38c8f 75 67 68 20 74 6f 20 68 6f 6c 64 20 6e 49 6e 20  ugh to hold nIn 
38c90 62 79 74 65 73 2e 20 20 57 72 69 74 65 20 74 68  bytes.  Write th
38c91 65 20 61 63 74 75 61 6c 0a 2a 2a 20 73 69 7a 65  e actual.** size
38c92 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 20 77   of the output w
38c93 6f 72 64 20 28 65 78 63 6c 75 73 69 76 65 20 6f  ord (exclusive o
38c94 66 20 74 68 65 20 27 5c 30 27 20 74 65 72 6d 69  f the '\0' termi
38c95 6e 61 74 6f 72 29 20 69 6e 74 6f 20 2a 70 6e 4f  nator) into *pnO
38c96 75 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 75 70  ut..**.** Any up
38c97 70 65 72 2d 63 61 73 65 20 63 68 61 72 61 63 74  per-case charact
38c98 65 72 73 20 69 6e 20 74 68 65 20 55 53 2d 41 53  ers in the US-AS
38c99 43 49 49 20 63 68 61 72 61 63 74 65 72 20 73 65  CII character se
38c9a 74 20 28 5b 41 2d 5a 5d 29 0a 2a 2a 20 61 72 65  t ([A-Z]).** are
38c9b 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 6c 6f   converted to lo
38c9c 77 65 72 20 63 61 73 65 2e 20 20 55 70 70 65 72  wer case.  Upper
38c9d 2d 63 61 73 65 20 55 54 46 20 63 68 61 72 61 63  -case UTF charac
38c9e 74 65 72 73 20 61 72 65 0a 2a 2a 20 75 6e 63 68  ters are.** unch
38c9f 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 6f 72  anged..**.** Wor
38ca0 64 73 20 74 68 61 74 20 61 72 65 20 6c 6f 6e 67  ds that are long
38ca1 65 72 20 74 68 61 6e 20 61 62 6f 75 74 20 32 30  er than about 20
38ca2 20 62 79 74 65 73 20 61 72 65 20 73 74 65 6d 6d   bytes are stemm
38ca3 65 64 20 62 79 20 72 65 74 61 69 6e 69 6e 67 0a  ed by retaining.
38ca4 2a 2a 20 61 20 66 65 77 20 62 79 74 65 73 20 66  ** a few bytes f
38ca5 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  rom the beginnin
38ca6 67 20 61 6e 64 20 74 68 65 20 65 6e 64 20 6f 66  g and the end of
38ca7 20 74 68 65 20 77 6f 72 64 2e 20 20 49 66 20 74   the word.  If t
38ca8 68 65 0a 2a 2a 20 77 6f 72 64 20 63 6f 6e 74 61  he.** word conta
38ca9 69 6e 73 20 64 69 67 69 74 73 2c 20 33 20 62 79  ins digits, 3 by
38caa 74 65 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72  tes are taken fr
38cab 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  om the beginning
38cac 20 61 6e 64 0a 2a 2a 20 33 20 62 79 74 65 73 20   and.** 3 bytes 
38cad 66 72 6f 6d 20 74 68 65 20 65 6e 64 2e 20 20 46  from the end.  F
38cae 6f 72 20 6c 6f 6e 67 20 77 6f 72 64 73 20 77 69  or long words wi
38caf 74 68 6f 75 74 20 64 69 67 69 74 73 2c 20 31 30  thout digits, 10
38cb0 20 62 79 74 65 73 0a 2a 2a 20 61 72 65 20 74 61   bytes.** are ta
38cb1 6b 65 6e 20 66 72 6f 6d 20 65 61 63 68 20 65 6e  ken from each en
38cb2 64 2e 20 20 55 53 2d 41 53 43 49 49 20 63 61 73  d.  US-ASCII cas
38cb3 65 20 66 6f 6c 64 69 6e 67 20 73 74 69 6c 6c 20  e folding still 
38cb4 61 70 70 6c 69 65 73 2e 0a 2a 2a 20 0a 2a 2a 20  applies..** .** 
38cb5 49 66 20 74 68 65 20 69 6e 70 75 74 20 77 6f 72  If the input wor
38cb6 64 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 74 20 64  d contains not d
38cb7 69 67 69 74 73 20 62 75 74 20 64 6f 65 73 20 63  igits but does c
38cb8 68 61 72 61 63 74 65 72 73 20 6e 6f 74 20 0a 2a  haracters not .*
38cb9 2a 20 69 6e 20 5b 61 2d 7a 41 2d 5a 5d 20 74 68  * in [a-zA-Z] th
38cba 65 6e 20 6e 6f 20 73 74 65 6d 6d 69 6e 67 20 69  en no stemming i
38cbb 73 20 61 74 74 65 6d 70 74 65 64 20 61 6e 64 20  s attempted and 
38cbc 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 73  this routine jus
38cbd 74 20 0a 2a 2a 20 63 6f 70 69 65 73 20 74 68 65  t .** copies the
38cbe 20 69 6e 70 75 74 20 69 6e 74 6f 20 74 68 65 20   input into the 
38cbf 69 6e 70 75 74 20 69 6e 74 6f 20 74 68 65 20 6f  input into the o
38cc0 75 74 70 75 74 20 77 69 74 68 20 55 53 2d 41 53  utput with US-AS
38cc1 43 49 49 0a 2a 2a 20 63 61 73 65 20 66 6f 6c 64  CII.** case fold
38cc2 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 74 65 6d 6d  ing..**.** Stemm
38cc3 69 6e 67 20 6e 65 76 65 72 20 69 6e 63 72 65 61  ing never increa
38cc4 73 65 73 20 74 68 65 20 6c 65 6e 67 74 68 20 6f  ses the length o
38cc5 66 20 74 68 65 20 77 6f 72 64 2e 20 20 53 6f 20  f the word.  So 
38cc6 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 20 63  there is.** no c
38cc7 68 61 6e 63 65 20 6f 66 20 6f 76 65 72 66 6c 6f  hance of overflo
38cc8 77 69 6e 67 20 74 68 65 20 7a 4f 75 74 20 62 75  wing the zOut bu
38cc9 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ffer..*/.static 
38cca 76 6f 69 64 20 70 6f 72 74 65 72 5f 73 74 65 6d  void porter_stem
38ccb 6d 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  mer(const char *
38ccc 7a 49 6e 2c 20 69 6e 74 20 6e 49 6e 2c 20 63 68  zIn, int nIn, ch
38ccd 61 72 20 2a 7a 4f 75 74 2c 20 69 6e 74 20 2a 70  ar *zOut, int *p
38cce 6e 4f 75 74 29 7b 0a 20 20 69 6e 74 20 69 2c 20  nOut){.  int i, 
38ccf 6a 2c 20 63 3b 0a 20 20 63 68 61 72 20 7a 52 65  j, c;.  char zRe
38cd0 76 65 72 73 65 5b 32 38 5d 3b 0a 20 20 63 68 61  verse[28];.  cha
38cd1 72 20 2a 7a 2c 20 2a 7a 32 3b 0a 20 20 69 66 28  r *z, *z2;.  if(
38cd2 20 6e 49 6e 3c 33 20 7c 7c 20 6e 49 6e 3e 3d 73   nIn<3 || nIn>=s
38cd3 69 7a 65 6f 66 28 7a 52 65 76 65 72 73 65 29 2d  izeof(zReverse)-
38cd4 37 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  7 ){.    /* The 
38cd5 77 6f 72 64 20 69 73 20 74 6f 6f 20 62 69 67 20  word is too big 
38cd6 6f 72 20 74 6f 6f 20 73 6d 61 6c 6c 20 66 6f 72  or too small for
38cd7 20 74 68 65 20 70 6f 72 74 65 72 20 73 74 65 6d   the porter stem
38cd8 6d 65 72 2e 0a 20 20 20 20 2a 2a 20 46 61 6c 6c  mer..    ** Fall
38cd9 62 61 63 6b 20 74 6f 20 74 68 65 20 63 6f 70 79  back to the copy
38cda 20 73 74 65 6d 6d 65 72 20 2a 2f 0a 20 20 20 20   stemmer */.    
38cdb 63 6f 70 79 5f 73 74 65 6d 6d 65 72 28 7a 49 6e  copy_stemmer(zIn
38cdc 2c 20 6e 49 6e 2c 20 7a 4f 75 74 2c 20 70 6e 4f  , nIn, zOut, pnO
38cdd 75 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ut);.    return;
38cde 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 2c 20  .  }.  for(i=0, 
38cdf 6a 3d 73 69 7a 65 6f 66 28 7a 52 65 76 65 72 73  j=sizeof(zRevers
38ce0 65 29 2d 36 3b 20 69 3c 6e 49 6e 3b 20 69 2b 2b  e)-6; i<nIn; i++
38ce1 2c 20 6a 2d 2d 29 7b 0a 20 20 20 20 63 20 3d 20  , j--){.    c = 
38ce2 7a 49 6e 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20  zIn[i];.    if( 
38ce3 63 3e 3d 27 41 27 20 26 26 20 63 3c 3d 27 5a 27  c>='A' && c<='Z'
38ce4 20 29 7b 0a 20 20 20 20 20 20 7a 52 65 76 65 72   ){.      zRever
38ce5 73 65 5b 6a 5d 20 3d 20 63 20 2b 20 27 61 27 20  se[j] = c + 'a' 
38ce6 2d 20 27 41 27 3b 0a 20 20 20 20 7d 65 6c 73 65  - 'A';.    }else
38ce7 20 69 66 28 20 63 3e 3d 27 61 27 20 26 26 20 63   if( c>='a' && c
38ce8 3c 3d 27 7a 27 20 29 7b 0a 20 20 20 20 20 20 7a  <='z' ){.      z
38ce9 52 65 76 65 72 73 65 5b 6a 5d 20 3d 20 63 3b 0a  Reverse[j] = c;.
38cea 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
38ceb 20 2f 2a 20 54 68 65 20 75 73 65 20 6f 66 20 61   /* The use of a
38cec 20 63 68 61 72 61 63 74 65 72 20 6e 6f 74 20 69   character not i
38ced 6e 20 5b 61 2d 7a 41 2d 5a 5d 20 6d 65 61 6e 73  n [a-zA-Z] means
38cee 20 74 68 61 74 20 77 65 20 66 61 6c 6c 62 61 63   that we fallbac
38cef 6b 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68  k.      ** to th
38cf0 65 20 63 6f 70 79 20 73 74 65 6d 6d 65 72 20 2a  e copy stemmer *
38cf1 2f 0a 20 20 20 20 20 20 63 6f 70 79 5f 73 74 65  /.      copy_ste
38cf2 6d 6d 65 72 28 7a 49 6e 2c 20 6e 49 6e 2c 20 7a  mmer(zIn, nIn, z
38cf3 4f 75 74 2c 20 70 6e 4f 75 74 29 3b 0a 20 20 20  Out, pnOut);.   
38cf4 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
38cf5 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 7a  .  }.  memset(&z
38cf6 52 65 76 65 72 73 65 5b 73 69 7a 65 6f 66 28 7a  Reverse[sizeof(z
38cf7 52 65 76 65 72 73 65 29 2d 35 5d 2c 20 30 2c 20  Reverse)-5], 0, 
38cf8 35 29 3b 0a 20 20 7a 20 3d 20 26 7a 52 65 76 65  5);.  z = &zReve
38cf9 72 73 65 5b 6a 2b 31 5d 3b 0a 0a 0a 20 20 2f 2a  rse[j+1];...  /*
38cfa 20 53 74 65 70 20 31 61 20 2a 2f 0a 20 20 69 66   Step 1a */.  if
38cfb 28 20 7a 5b 30 5d 3d 3d 27 73 27 20 29 7b 0a 20  ( z[0]=='s' ){. 
38cfc 20 20 20 69 66 28 0a 20 20 20 20 20 21 73 74 65     if(.     !ste
38cfd 6d 28 26 7a 2c 20 22 73 65 73 73 22 2c 20 22 73  m(&z, "sess", "s
38cfe 73 22 2c 20 30 29 20 26 26 0a 20 20 20 20 20 21  s", 0) &&.     !
38cff 73 74 65 6d 28 26 7a 2c 20 22 73 65 69 22 2c 20  stem(&z, "sei", 
38d00 22 69 22 2c 20 30 29 20 20 26 26 0a 20 20 20 20  "i", 0)  &&.    
38d01 20 21 73 74 65 6d 28 26 7a 2c 20 22 73 73 22 2c   !stem(&z, "ss",
38d02 20 22 73 73 22 2c 20 30 29 0a 20 20 20 20 29 7b   "ss", 0).    ){
38d03 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20  .      z++;.    
38d04 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70  }.  }..  /* Step
38d05 20 31 62 20 2a 2f 20 20 0a 20 20 7a 32 20 3d 20   1b */  .  z2 = 
38d06 7a 3b 0a 20 20 69 66 28 20 73 74 65 6d 28 26 7a  z;.  if( stem(&z
38d07 2c 20 22 64 65 65 22 2c 20 22 65 65 22 2c 20 6d  , "dee", "ee", m
38d08 5f 67 74 5f 30 29 20 29 7b 0a 20 20 20 20 2f 2a  _gt_0) ){.    /*
38d09 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54 68   Do nothing.  Th
38d0a 65 20 77 6f 72 6b 20 77 61 73 20 61 6c 6c 20 69  e work was all i
38d0b 6e 20 74 68 65 20 74 65 73 74 20 2a 2f 0a 20 20  n the test */.  
38d0c 7d 65 6c 73 65 20 69 66 28 20 0a 20 20 20 20 20  }else if( .     
38d0d 28 73 74 65 6d 28 26 7a 2c 20 22 67 6e 69 22 2c  (stem(&z, "gni",
38d0e 20 22 22 2c 20 68 61 73 56 6f 77 65 6c 29 20 7c   "", hasVowel) |
38d0f 7c 20 73 74 65 6d 28 26 7a 2c 20 22 64 65 22 2c  | stem(&z, "de",
38d10 20 22 22 2c 20 68 61 73 56 6f 77 65 6c 29 29 0a   "", hasVowel)).
38d11 20 20 20 20 20 20 26 26 20 7a 21 3d 7a 32 0a 20        && z!=z2. 
38d12 20 29 7b 0a 20 20 20 20 20 69 66 28 20 73 74 65   ){.     if( ste
38d13 6d 28 26 7a 2c 20 22 74 61 22 2c 20 22 61 74 65  m(&z, "ta", "ate
38d14 22 2c 20 30 29 20 7c 7c 0a 20 20 20 20 20 20 20  ", 0) ||.       
38d15 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c 62 22 2c    stem(&z, "lb",
38d16 20 22 62 6c 65 22 2c 20 30 29 20 7c 7c 0a 20 20   "ble", 0) ||.  
38d17 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20         stem(&z, 
38d18 22 7a 69 22 2c 20 22 69 7a 65 22 2c 20 30 29 20  "zi", "ize", 0) 
38d19 29 7b 0a 20 20 20 20 20 20 20 2f 2a 20 44 6f 20  ){.       /* Do 
38d1a 6e 6f 74 68 69 6e 67 2e 20 20 54 68 65 20 77 6f  nothing.  The wo
38d1b 72 6b 20 77 61 73 20 61 6c 6c 20 69 6e 20 74 68  rk was all in th
38d1c 65 20 74 65 73 74 20 2a 2f 0a 20 20 20 20 20 7d  e test */.     }
38d1d 65 6c 73 65 20 69 66 28 20 64 6f 75 62 6c 65 43  else if( doubleC
38d1e 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 26 26 20 28  onsonant(z) && (
38d1f 2a 7a 21 3d 27 6c 27 20 26 26 20 2a 7a 21 3d 27  *z!='l' && *z!='
38d20 73 27 20 26 26 20 2a 7a 21 3d 27 7a 27 29 20 29  s' && *z!='z') )
38d21 7b 0a 20 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20  {.       z++;.  
38d22 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6d 5f 65     }else if( m_e
38d23 71 5f 31 28 7a 29 20 26 26 20 73 74 61 72 5f 6f  q_1(z) && star_o
38d24 68 28 7a 29 20 29 7b 0a 20 20 20 20 20 20 20 2a  h(z) ){.       *
38d25 28 2d 2d 7a 29 20 3d 20 27 65 27 3b 0a 20 20 20  (--z) = 'e';.   
38d26 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74    }.  }..  /* St
38d27 65 70 20 31 63 20 2a 2f 0a 20 20 69 66 28 20 7a  ep 1c */.  if( z
38d28 5b 30 5d 3d 3d 27 79 27 20 26 26 20 68 61 73 56  [0]=='y' && hasV
38d29 6f 77 65 6c 28 7a 2b 31 29 20 29 7b 0a 20 20 20  owel(z+1) ){.   
38d2a 20 7a 5b 30 5d 20 3d 20 27 69 27 3b 0a 20 20 7d   z[0] = 'i';.  }
38d2b 0a 0a 20 20 2f 2a 20 53 74 65 70 20 32 20 2a 2f  ..  /* Step 2 */
38d2c 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 31 5d 20  .  switch( z[1] 
38d2d 29 7b 0a 20 20 20 63 61 73 65 20 27 61 27 3a 0a  ){.   case 'a':.
38d2e 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c       stem(&z, "l
38d2f 61 6e 6f 69 74 61 22 2c 20 22 61 74 65 22 2c 20  anoita", "ate", 
38d30 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
38d31 73 74 65 6d 28 26 7a 2c 20 22 6c 61 6e 6f 69 74  stem(&z, "lanoit
38d32 22 2c 20 22 74 69 6f 6e 22 2c 20 6d 5f 67 74 5f  ", "tion", m_gt_
38d33 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a  0);.     break;.
38d34 20 20 20 63 61 73 65 20 27 63 27 3a 0a 20 20 20     case 'c':.   
38d35 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 63 6e 65    stem(&z, "icne
38d36 22 2c 20 22 65 6e 63 65 22 2c 20 6d 5f 67 74 5f  ", "ence", m_gt_
38d37 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28  0) ||.     stem(
38d38 26 7a 2c 20 22 69 63 6e 61 22 2c 20 22 61 6e 63  &z, "icna", "anc
38d39 65 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20  e", m_gt_0);.   
38d3a 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
38d3b 20 27 65 27 3a 0a 20 20 20 20 20 73 74 65 6d 28   'e':.     stem(
38d3c 26 7a 2c 20 22 72 65 7a 69 22 2c 20 22 69 7a 65  &z, "rezi", "ize
38d3d 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20  ", m_gt_0);.    
38d3e 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
38d3f 27 67 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  'g':.     stem(&
38d40 7a 2c 20 22 69 67 6f 6c 22 2c 20 22 6c 6f 67 22  z, "igol", "log"
38d41 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20  , m_gt_0);.     
38d42 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
38d43 6c 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a  l':.     stem(&z
38d44 2c 20 22 69 6c 62 22 2c 20 22 62 6c 65 22 2c 20  , "ilb", "ble", 
38d45 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
38d46 73 74 65 6d 28 26 7a 2c 20 22 69 6c 6c 61 22 2c  stem(&z, "illa",
38d47 20 22 61 6c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c   "al", m_gt_0) |
38d48 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  |.     stem(&z, 
38d49 22 69 6c 74 6e 65 22 2c 20 22 65 6e 74 22 2c 20  "iltne", "ent", 
38d4a 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
38d4b 73 74 65 6d 28 26 7a 2c 20 22 69 6c 65 22 2c 20  stem(&z, "ile", 
38d4c 22 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  "e", m_gt_0) ||.
38d4d 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
38d4e 6c 73 75 6f 22 2c 20 22 6f 75 73 22 2c 20 6d 5f  lsuo", "ous", m_
38d4f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61  gt_0);.     brea
38d50 6b 3b 0a 20 20 20 63 61 73 65 20 27 6f 27 3a 0a  k;.   case 'o':.
38d51 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6e       stem(&z, "n
38d52 6f 69 74 61 7a 69 22 2c 20 22 69 7a 65 22 2c 20  oitazi", "ize", 
38d53 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
38d54 73 74 65 6d 28 26 7a 2c 20 22 6e 6f 69 74 61 22  stem(&z, "noita"
38d55 2c 20 22 61 74 65 22 2c 20 6d 5f 67 74 5f 30 29  , "ate", m_gt_0)
38d56 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a   ||.     stem(&z
38d57 2c 20 22 72 6f 74 61 22 2c 20 22 61 74 65 22 2c  , "rota", "ate",
38d58 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62   m_gt_0);.     b
38d59 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 73  reak;.   case 's
38d5a 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ':.     stem(&z,
38d5b 20 22 6d 73 69 6c 61 22 2c 20 22 61 6c 22 2c 20   "msila", "al", 
38d5c 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
38d5d 73 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e 65 76  stem(&z, "ssenev
38d5e 69 22 2c 20 22 69 76 65 22 2c 20 6d 5f 67 74 5f  i", "ive", m_gt_
38d5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28  0) ||.     stem(
38d60 26 7a 2c 20 22 73 73 65 6e 6c 75 66 22 2c 20 22  &z, "ssenluf", "
38d61 66 75 6c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c  ful", m_gt_0) ||
38d62 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22  .     stem(&z, "
38d63 73 73 65 6e 73 75 6f 22 2c 20 22 6f 75 73 22 2c  ssensuo", "ous",
38d64 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62   m_gt_0);.     b
38d65 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 74  reak;.   case 't
38d66 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ':.     stem(&z,
38d67 20 22 69 74 69 6c 61 22 2c 20 22 61 6c 22 2c 20   "itila", "al", 
38d68 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
38d69 73 74 65 6d 28 26 7a 2c 20 22 69 74 69 76 69 22  stem(&z, "itivi"
38d6a 2c 20 22 69 76 65 22 2c 20 6d 5f 67 74 5f 30 29  , "ive", m_gt_0)
38d6b 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a   ||.     stem(&z
38d6c 2c 20 22 69 74 69 6c 69 62 22 2c 20 22 62 6c 65  , "itilib", "ble
38d6d 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20  ", m_gt_0);.    
38d6e 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f   break;.  }..  /
38d6f 2a 20 53 74 65 70 20 33 20 2a 2f 0a 20 20 73 77  * Step 3 */.  sw
38d70 69 74 63 68 28 20 7a 5b 30 5d 20 29 7b 0a 20 20  itch( z[0] ){.  
38d71 20 63 61 73 65 20 27 65 27 3a 0a 20 20 20 20 20   case 'e':.     
38d72 73 74 65 6d 28 26 7a 2c 20 22 65 74 61 63 69 22  stem(&z, "etaci"
38d73 2c 20 22 69 63 22 2c 20 6d 5f 67 74 5f 30 29 20  , "ic", m_gt_0) 
38d74 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ||.     stem(&z,
38d75 20 22 65 76 69 74 61 22 2c 20 22 22 2c 20 6d 5f   "evita", "", m_
38d76 67 74 5f 30 29 20 20 20 7c 7c 0a 20 20 20 20 20  gt_0)   ||.     
38d77 73 74 65 6d 28 26 7a 2c 20 22 65 7a 69 6c 61 22  stem(&z, "ezila"
38d78 2c 20 22 61 6c 22 2c 20 6d 5f 67 74 5f 30 29 3b  , "al", m_gt_0);
38d79 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
38d7a 63 61 73 65 20 27 69 27 3a 0a 20 20 20 20 20 73  case 'i':.     s
38d7b 74 65 6d 28 26 7a 2c 20 22 69 74 69 63 69 22 2c  tem(&z, "itici",
38d7c 20 22 69 63 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a   "ic", m_gt_0);.
38d7d 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
38d7e 61 73 65 20 27 6c 27 3a 0a 20 20 20 20 20 73 74  ase 'l':.     st
38d7f 65 6d 28 26 7a 2c 20 22 6c 61 63 69 22 2c 20 22  em(&z, "laci", "
38d80 69 63 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  ic", m_gt_0) ||.
38d81 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c       stem(&z, "l
38d82 75 66 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 30 29  uf", "", m_gt_0)
38d83 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  ;.     break;.  
38d84 20 63 61 73 65 20 27 73 27 3a 0a 20 20 20 20 20   case 's':.     
38d85 73 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e 22 2c  stem(&z, "ssen",
38d86 20 22 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20   "", m_gt_0);.  
38d87 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20     break;.  }.. 
38d88 20 2f 2a 20 53 74 65 70 20 34 20 2a 2f 0a 20 20   /* Step 4 */.  
38d89 73 77 69 74 63 68 28 20 7a 5b 31 5d 20 29 7b 0a  switch( z[1] ){.
38d8a 20 20 20 63 61 73 65 20 27 61 27 3a 0a 20 20 20     case 'a':.   
38d8b 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 6c 27 20    if( z[0]=='l' 
38d8c 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 32 29 20 29  && m_gt_1(z+2) )
38d8d 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 32 3b  {.       z += 2;
38d8e 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65  .     }.     bre
38d8f 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 63 27 3a  ak;.   case 'c':
38d90 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d  .     if( z[0]==
38d91 27 65 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 6e 27  'e' && z[2]=='n'
38d92 20 26 26 20 28 7a 5b 33 5d 3d 3d 27 61 27 20 7c   && (z[3]=='a' |
38d93 7c 20 7a 5b 33 5d 3d 3d 27 65 27 29 20 20 26 26  | z[3]=='e')  &&
38d94 20 6d 5f 67 74 5f 31 28 7a 2b 34 29 20 20 29 7b   m_gt_1(z+4)  ){
38d95 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 34 3b 0a  .       z += 4;.
38d96 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61       }.     brea
38d97 6b 3b 0a 20 20 20 63 61 73 65 20 27 65 27 3a 0a  k;.   case 'e':.
38d98 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27       if( z[0]=='
38d99 72 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 32  r' && m_gt_1(z+2
38d9a 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d  ) ){.       z +=
38d9b 20 32 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20   2;.     }.     
38d9c 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
38d9d 69 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30  i':.     if( z[0
38d9e 5d 3d 3d 27 63 27 20 26 26 20 6d 5f 67 74 5f 31  ]=='c' && m_gt_1
38d9f 28 7a 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 20  (z+2) ){.       
38da0 7a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 7d 0a 20  z += 2;.     }. 
38da1 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61      break;.   ca
38da2 73 65 20 27 6c 27 3a 0a 20 20 20 20 20 69 66 28  se 'l':.     if(
38da3 20 7a 5b 30 5d 3d 3d 27 65 27 20 26 26 20 7a 5b   z[0]=='e' && z[
38da4 32 5d 3d 3d 27 62 27 20 26 26 20 28 7a 5b 33 5d  2]=='b' && (z[3]
38da5 3d 3d 27 61 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27  =='a' || z[3]=='
38da6 69 27 29 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b  i') && m_gt_1(z+
38da7 34 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b  4) ){.       z +
38da8 3d 20 34 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20  = 4;.     }.    
38da9 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
38daa 27 6e 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b  'n':.     if( z[
38dab 30 5d 3d 3d 27 74 27 20 29 7b 0a 20 20 20 20 20  0]=='t' ){.     
38dac 20 20 69 66 28 20 7a 5b 32 5d 3d 3d 27 61 27 20    if( z[2]=='a' 
38dad 29 7b 0a 20 20 20 20 20 20 20 20 20 69 66 28 20  ){.         if( 
38dae 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29 7b 0a 20  m_gt_1(z+3) ){. 
38daf 20 20 20 20 20 20 20 20 20 20 7a 20 2b 3d 20 33            z += 3
38db0 3b 0a 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  ;.         }.   
38db1 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 5b      }else if( z[
38db2 32 5d 3d 3d 27 65 27 20 29 7b 0a 20 20 20 20 20  2]=='e' ){.     
38db3 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 74 6e      stem(&z, "tn
38db4 65 6d 65 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31  eme", "", m_gt_1
38db5 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 73 74  ) ||.         st
38db6 65 6d 28 26 7a 2c 20 22 74 6e 65 6d 22 2c 20 22  em(&z, "tnem", "
38db7 22 2c 20 6d 5f 67 74 5f 31 29 20 7c 7c 0a 20 20  ", m_gt_1) ||.  
38db8 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20         stem(&z, 
38db9 22 74 6e 65 22 2c 20 22 22 2c 20 6d 5f 67 74 5f  "tne", "", m_gt_
38dba 31 29 3b 0a 20 20 20 20 20 20 20 7d 0a 20 20 20  1);.       }.   
38dbb 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a    }.     break;.
38dbc 20 20 20 63 61 73 65 20 27 6f 27 3a 0a 20 20 20     case 'o':.   
38dbd 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 75 27 20    if( z[0]=='u' 
38dbe 29 7b 0a 20 20 20 20 20 20 20 69 66 28 20 6d 5f  ){.       if( m_
38dbf 67 74 5f 31 28 7a 2b 32 29 20 29 7b 0a 20 20 20  gt_1(z+2) ){.   
38dc0 20 20 20 20 20 20 7a 20 2b 3d 20 32 3b 0a 20 20        z += 2;.  
38dc1 20 20 20 20 20 7d 0a 20 20 20 20 20 7d 65 6c 73       }.     }els
38dc2 65 20 69 66 28 20 7a 5b 33 5d 3d 3d 27 73 27 20  e if( z[3]=='s' 
38dc3 7c 7c 20 7a 5b 33 5d 3d 3d 27 74 27 20 29 7b 0a  || z[3]=='t' ){.
38dc4 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20         stem(&z, 
38dc5 22 6e 6f 69 22 2c 20 22 22 2c 20 6d 5f 67 74 5f  "noi", "", m_gt_
38dc6 31 29 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20  1);.     }.     
38dc7 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
38dc8 73 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30  s':.     if( z[0
38dc9 5d 3d 3d 27 6d 27 20 26 26 20 7a 5b 32 5d 3d 3d  ]=='m' && z[2]==
38dca 27 69 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b  'i' && m_gt_1(z+
38dcb 33 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b  3) ){.       z +
38dcc 3d 20 33 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20  = 3;.     }.    
38dcd 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
38dce 27 74 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  't':.     stem(&
38dcf 7a 2c 20 22 65 74 61 22 2c 20 22 22 2c 20 6d 5f  z, "eta", "", m_
38dd0 67 74 5f 31 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_1) ||.     st
38dd1 65 6d 28 26 7a 2c 20 22 69 74 69 22 2c 20 22 22  em(&z, "iti", ""
38dd2 2c 20 6d 5f 67 74 5f 31 29 3b 0a 20 20 20 20 20  , m_gt_1);.     
38dd3 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
38dd4 75 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30  u':.     if( z[0
38dd5 5d 3d 3d 27 73 27 20 26 26 20 7a 5b 32 5d 3d 3d  ]=='s' && z[2]==
38dd6 27 6f 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b  'o' && m_gt_1(z+
38dd7 33 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b  3) ){.       z +
38dd8 3d 20 33 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20  = 3;.     }.    
38dd9 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
38dda 27 76 27 3a 0a 20 20 20 63 61 73 65 20 27 7a 27  'v':.   case 'z'
38ddb 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  :.     if( z[0]=
38ddc 3d 27 65 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 69  ='e' && z[2]=='i
38ddd 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 33 29  ' && m_gt_1(z+3)
38dde 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20   ){.       z += 
38ddf 33 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62  3;.     }.     b
38de0 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  reak;.  }..  /* 
38de1 53 74 65 70 20 35 61 20 2a 2f 0a 20 20 69 66 28  Step 5a */.  if(
38de2 20 7a 5b 30 5d 3d 3d 27 65 27 20 29 7b 0a 20 20   z[0]=='e' ){.  
38de3 20 20 69 66 28 20 6d 5f 67 74 5f 31 28 7a 2b 31    if( m_gt_1(z+1
38de4 29 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a  ) ){.      z++;.
38de5 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6d 5f      }else if( m_
38de6 65 71 5f 31 28 7a 2b 31 29 20 26 26 20 21 73 74  eq_1(z+1) && !st
38de7 61 72 5f 6f 68 28 7a 2b 31 29 20 29 7b 0a 20 20  ar_oh(z+1) ){.  
38de8 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20      z++;.    }. 
38de9 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35 62   }..  /* Step 5b
38dea 20 2a 2f 0a 20 20 69 66 28 20 6d 5f 67 74 5f 31   */.  if( m_gt_1
38deb 28 7a 29 20 26 26 20 7a 5b 30 5d 3d 3d 27 6c 27  (z) && z[0]=='l'
38dec 20 26 26 20 7a 5b 31 5d 3d 3d 27 6c 27 20 29 7b   && z[1]=='l' ){
38ded 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 0a 20  .    z++;.  }.. 
38dee 20 2f 2a 20 7a 5b 5d 20 69 73 20 6e 6f 77 20 74   /* z[] is now t
38def 68 65 20 73 74 65 6d 6d 65 64 20 77 6f 72 64 20  he stemmed word 
38df0 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72  in reverse order
38df1 2e 20 20 46 6c 69 70 20 69 74 20 62 61 63 6b 0a  .  Flip it back.
38df2 20 20 2a 2a 20 61 72 6f 75 6e 64 20 69 6e 74 6f    ** around into
38df3 20 66 6f 72 77 61 72 64 20 6f 72 64 65 72 20 61   forward order a
38df4 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a  nd return..  */.
38df5 20 20 2a 70 6e 4f 75 74 20 3d 20 69 20 3d 20 73    *pnOut = i = s
38df6 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 7a 4f 75 74  trlen(z);.  zOut
38df7 5b 69 5d 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65  [i] = 0;.  while
38df8 28 20 2a 7a 20 29 7b 0a 20 20 20 20 7a 4f 75 74  ( *z ){.    zOut
38df9 5b 2d 2d 69 5d 20 3d 20 2a 28 7a 2b 2b 29 3b 0a  [--i] = *(z++);.
38dfa 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61    }.}../*.** Cha
38dfb 72 61 63 74 65 72 73 20 74 68 61 74 20 63 61 6e  racters that can
38dfc 20 62 65 20 70 61 72 74 20 6f 66 20 61 20 74 6f   be part of a to
38dfd 6b 65 6e 2e 20 20 57 65 20 61 73 73 75 6d 65 20  ken.  We assume 
38dfe 61 6e 79 20 63 68 61 72 61 63 74 65 72 0a 2a 2a  any character.**
38dff 20 77 68 6f 73 65 20 76 61 6c 75 65 20 69 73 20   whose value is 
38e00 67 72 65 61 74 65 72 20 74 68 61 6e 20 30 78 38  greater than 0x8
38e01 30 20 28 61 6e 79 20 55 54 46 20 63 68 61 72 61  0 (any UTF chara
38e02 63 74 65 72 29 20 63 61 6e 20 62 65 0a 2a 2a 20  cter) can be.** 
38e03 70 61 72 74 20 6f 66 20 61 20 74 6f 6b 65 6e 2e  part of a token.
38e04 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
38e05 2c 20 64 65 6c 69 6d 69 74 65 72 73 20 61 6c 6c  , delimiters all
38e06 20 6d 75 73 74 20 68 61 76 65 0a 2a 2a 20 76 61   must have.** va
38e07 6c 75 65 73 20 6f 66 20 30 78 37 66 20 6f 72 20  lues of 0x7f or 
38e08 6c 6f 77 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  lower..*/.static
38e09 20 63 6f 6e 73 74 20 63 68 61 72 20 70 6f 72 74   const char port
38e0a 65 72 49 64 43 68 61 72 5b 5d 20 3d 20 7b 0a 2f  erIdChar[] = {./
38e0b 2a 20 78 30 20 78 31 20 78 32 20 78 33 20 78 34  * x0 x1 x2 x3 x4
38e0c 20 78 35 20 78 36 20 78 37 20 78 38 20 78 39 20   x5 x6 x7 x8 x9 
38e0d 78 41 20 78 42 20 78 43 20 78 44 20 78 45 20 78  xA xB xC xD xE x
38e0e 46 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31  F */.    1, 1, 1
38e0f 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
38e10 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 1, 0, 0, 0, 
38e11 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 33 78 20  0, 0, 0,  /* 3x 
38e12 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20  */.    0, 1, 1, 
38e13 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
38e14 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
38e15 20 31 2c 20 31 2c 20 20 2f 2a 20 34 78 20 2a 2f   1, 1,  /* 4x */
38e16 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c  .    1, 1, 1, 1,
38e17 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
38e18 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 1, 0, 0, 0, 0
38e19 2c 20 31 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a 20  , 1,  /* 5x */. 
38e1a 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31     0, 1, 1, 1, 1
38e1b 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
38e1c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
38e1d 31 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20  1,  /* 6x */.   
38e1e 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
38e1f 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
38e20 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
38e21 20 20 2f 2a 20 37 78 20 2a 2f 0a 7d 3b 0a 23 64    /* 7x */.};.#d
38e22 65 66 69 6e 65 20 69 73 44 65 6c 69 6d 28 43 29  efine isDelim(C)
38e23 20 28 28 28 63 68 3d 43 29 26 30 78 38 30 29 3d   (((ch=C)&0x80)=
38e24 3d 30 20 26 26 20 28 63 68 3c 30 78 33 30 20 7c  =0 && (ch<0x30 |
38e25 7c 20 21 70 6f 72 74 65 72 49 64 43 68 61 72 5b  | !porterIdChar[
38e26 63 68 2d 30 78 33 30 5d 29 29 0a 0a 2f 2a 0a 2a  ch-0x30]))../*.*
38e27 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 6e 65  * Extract the ne
38e28 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 61 20  xt token from a 
38e29 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72  tokenization cur
38e2a 73 6f 72 2e 20 20 54 68 65 20 63 75 72 73 6f 72  sor.  The cursor
38e2b 20 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20 62 65   must.** have be
38e2c 65 6e 20 6f 70 65 6e 65 64 20 62 79 20 61 20 70  en opened by a p
38e2d 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 70 6f 72  rior call to por
38e2e 74 65 72 4f 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74  terOpen()..*/.st
38e2f 61 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72 4e  atic int porterN
38e30 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 74  ext(.  sqlite3_t
38e31 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
38e32 2a 70 43 75 72 73 6f 72 2c 20 20 2f 2a 20 43 75  *pCursor,  /* Cu
38e33 72 73 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79  rsor returned by
38e34 20 70 6f 72 74 65 72 4f 70 65 6e 20 2a 2f 0a 20   porterOpen */. 
38e35 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a   const char **pz
38e36 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20 20 20  Token,          
38e37 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 2a 70 7a       /* OUT: *pz
38e38 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 74 6f 6b  Token is the tok
38e39 65 6e 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74  en text */.  int
38e3a 20 2a 70 6e 42 79 74 65 73 2c 20 20 20 20 20 20   *pnBytes,      
38e3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38e3c 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20   /* OUT: Number 
38e3d 6f 66 20 62 79 74 65 73 20 69 6e 20 74 6f 6b 65  of bytes in toke
38e3e 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 53 74  n */.  int *piSt
38e3f 61 72 74 4f 66 66 73 65 74 2c 20 20 20 20 20 20  artOffset,      
38e40 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
38e41 54 3a 20 53 74 61 72 74 69 6e 67 20 6f 66 66 73  T: Starting offs
38e42 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20  et of token */. 
38e43 20 69 6e 74 20 2a 70 69 45 6e 64 4f 66 66 73 65   int *piEndOffse
38e44 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
38e45 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64       /* OUT: End
38e46 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74 6f  ing offset of to
38e47 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69  ken */.  int *pi
38e48 50 6f 73 69 74 69 6f 6e 20 20 20 20 20 20 20 20  Position        
38e49 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38e4a 4f 55 54 3a 20 50 6f 73 69 74 69 6f 6e 20 69 6e  OUT: Position in
38e4b 74 65 67 65 72 20 6f 66 20 74 6f 6b 65 6e 20 2a  teger of token *
38e4c 2f 0a 29 7b 0a 20 20 70 6f 72 74 65 72 5f 74 6f  /.){.  porter_to
38e4d 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
38e4e 63 20 3d 20 28 70 6f 72 74 65 72 5f 74 6f 6b 65  c = (porter_toke
38e4f 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 20  nizer_cursor *) 
38e50 70 43 75 72 73 6f 72 3b 0a 20 20 63 6f 6e 73 74  pCursor;.  const
38e51 20 63 68 61 72 20 2a 7a 20 3d 20 63 2d 3e 7a 49   char *z = c->zI
38e52 6e 70 75 74 3b 0a 0a 20 20 77 68 69 6c 65 28 20  nput;..  while( 
38e53 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 49  c->iOffset<c->nI
38e54 6e 70 75 74 20 29 7b 0a 20 20 20 20 69 6e 74 20  nput ){.    int 
38e55 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 63 68  iStartOffset, ch
38e56 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e 20 70  ;..    /* Scan p
38e57 61 73 74 20 64 65 6c 69 6d 69 74 65 72 20 63 68  ast delimiter ch
38e58 61 72 61 63 74 65 72 73 20 2a 2f 0a 20 20 20 20  aracters */.    
38e59 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65  while( c->iOffse
38e5a 74 3c 63 2d 3e 6e 49 6e 70 75 74 20 26 26 20 69  t<c->nInput && i
38e5b 73 44 65 6c 69 6d 28 7a 5b 63 2d 3e 69 4f 66 66  sDelim(z[c->iOff
38e5c 73 65 74 5d 29 20 29 7b 0a 20 20 20 20 20 20 63  set]) ){.      c
38e5d 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a 20 20 20  ->iOffset++;.   
38e5e 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 75 6e 74   }..    /* Count
38e5f 20 6e 6f 6e 2d 64 65 6c 69 6d 69 74 65 72 20 63   non-delimiter c
38e60 68 61 72 61 63 74 65 72 73 2e 20 2a 2f 0a 20 20  haracters. */.  
38e61 20 20 69 53 74 61 72 74 4f 66 66 73 65 74 20 3d    iStartOffset =
38e62 20 63 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20   c->iOffset;.   
38e63 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73   while( c->iOffs
38e64 65 74 3c 63 2d 3e 6e 49 6e 70 75 74 20 26 26 20  et<c->nInput && 
38e65 21 69 73 44 65 6c 69 6d 28 7a 5b 63 2d 3e 69 4f  !isDelim(z[c->iO
38e66 66 66 73 65 74 5d 29 20 29 7b 0a 20 20 20 20 20  ffset]) ){.     
38e67 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a 20   c->iOffset++;. 
38e68 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 63 2d     }..    if( c-
38e69 3e 69 4f 66 66 73 65 74 3e 69 53 74 61 72 74 4f  >iOffset>iStartO
38e6a 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20 69  ffset ){.      i
38e6b 6e 74 20 6e 20 3d 20 63 2d 3e 69 4f 66 66 73 65  nt n = c->iOffse
38e6c 74 2d 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a  t-iStartOffset;.
38e6d 20 20 20 20 20 20 69 66 28 20 6e 3e 63 2d 3e 6e        if( n>c->n
38e6e 41 6c 6c 6f 63 61 74 65 64 20 29 7b 0a 20 20 20  Allocated ){.   
38e6f 20 20 20 20 20 63 2d 3e 6e 41 6c 6c 6f 63 61 74       c->nAllocat
38e70 65 64 20 3d 20 6e 2b 32 30 3b 0a 20 20 20 20 20  ed = n+20;.     
38e71 20 20 20 63 2d 3e 7a 54 6f 6b 65 6e 20 3d 20 73     c->zToken = s
38e72 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 63  qlite3_realloc(c
38e73 2d 3e 7a 54 6f 6b 65 6e 2c 20 63 2d 3e 6e 41 6c  ->zToken, c->nAl
38e74 6c 6f 63 61 74 65 64 29 3b 0a 20 20 20 20 20 20  located);.      
38e75 20 20 69 66 28 20 63 2d 3e 7a 54 6f 6b 65 6e 3d    if( c->zToken=
38e76 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53  =NULL ) return S
38e77 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
38e78 20 20 20 7d 0a 20 20 20 20 20 20 70 6f 72 74 65     }.      porte
38e79 72 5f 73 74 65 6d 6d 65 72 28 26 7a 5b 69 53 74  r_stemmer(&z[iSt
38e7a 61 72 74 4f 66 66 73 65 74 5d 2c 20 6e 2c 20 63  artOffset], n, c
38e7b 2d 3e 7a 54 6f 6b 65 6e 2c 20 70 6e 42 79 74 65  ->zToken, pnByte
38e7c 73 29 3b 0a 20 20 20 20 20 20 2a 70 7a 54 6f 6b  s);.      *pzTok
38e7d 65 6e 20 3d 20 63 2d 3e 7a 54 6f 6b 65 6e 3b 0a  en = c->zToken;.
38e7e 20 20 20 20 20 20 2a 70 69 53 74 61 72 74 4f 66        *piStartOf
38e7f 66 73 65 74 20 3d 20 69 53 74 61 72 74 4f 66 66  fset = iStartOff
38e80 73 65 74 3b 0a 20 20 20 20 20 20 2a 70 69 45 6e  set;.      *piEn
38e81 64 4f 66 66 73 65 74 20 3d 20 63 2d 3e 69 4f 66  dOffset = c->iOf
38e82 66 73 65 74 3b 0a 20 20 20 20 20 20 2a 70 69 50  fset;.      *piP
38e83 6f 73 69 74 69 6f 6e 20 3d 20 63 2d 3e 69 54 6f  osition = c->iTo
38e84 6b 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74  ken++;.      ret
38e85 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
38e86 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
38e87 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 7d  n SQLITE_DONE;.}
38e88 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 74 20  ../*.** The set 
38e89 6f 66 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  of routines that
38e8a 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 70   implement the p
38e8b 6f 72 74 65 72 2d 73 74 65 6d 6d 65 72 20 74 6f  orter-stemmer to
38e8c 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69  kenizer.*/.stati
38e8d 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
38e8e 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
38e8f 20 70 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72   porterTokenizer
38e90 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c 0a  Module = {.  0,.
38e91 20 20 70 6f 72 74 65 72 43 72 65 61 74 65 2c 0a    porterCreate,.
38e92 20 20 70 6f 72 74 65 72 44 65 73 74 72 6f 79 2c    porterDestroy,
38e93 0a 20 20 70 6f 72 74 65 72 4f 70 65 6e 2c 0a 20  .  porterOpen,. 
38e94 20 70 6f 72 74 65 72 43 6c 6f 73 65 2c 0a 20 20   porterClose,.  
38e95 70 6f 72 74 65 72 4e 65 78 74 2c 0a 7d 3b 0a 0a  porterNext,.};..
38e96 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  /*.** Allocate a
38e97 20 6e 65 77 20 70 6f 72 74 65 72 20 74 6f 6b 65   new porter toke
38e98 6e 69 7a 65 72 2e 20 20 52 65 74 75 72 6e 20 61  nizer.  Return a
38e99 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
38e9a 6e 65 77 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72  new.** tokenizer
38e9b 20 69 6e 20 2a 70 70 4d 6f 64 75 6c 65 0a 2a 2f   in *ppModule.*/
38e9c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
38e9d 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
38e9e 50 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d  PorterTokenizerM
38e9f 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33  odule(.  sqlite3
38ea0 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
38ea1 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c  e const**ppModul
38ea2 65 0a 29 7b 0a 20 20 2a 70 70 4d 6f 64 75 6c 65  e.){.  *ppModule
38ea3 20 3d 20 26 70 6f 72 74 65 72 54 6f 6b 65 6e 69   = &porterTokeni
38ea4 7a 65 72 4d 6f 64 75 6c 65 3b 0a 7d 0a 0a 23 65  zerModule;.}..#e
38ea5 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
38ea6 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c  (SQLITE_CORE) ||
38ea7 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
38ea8 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a  ENABLE_FTS3) */.
38ea9 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
38eaa 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 70 6f 72   End of fts3_por
38eab 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ter.c **********
38eac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38ead 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
38eae 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
38eaf 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33   Begin file fts3
38eb0 5f 74 6f 6b 65 6e 69 7a 65 72 2e 63 20 2a 2a 2a  _tokenizer.c ***
38eb1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38eb2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
38eb3 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4a 75 6e 65  ./*.** 2007 June
38eb4 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   22.**.** The au
38eb5 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
38eb6 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
38eb7 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
38eb8 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
38eb9 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
38eba 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
38ebb 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
38ebc 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
38ebd 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
38ebe 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
38ebf 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
38ec0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
38ec1 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
38ec2 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
38ec3 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
38ec4 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
38ec5 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
38ec6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38ec7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38ec8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38ec9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38eca 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
38ecb 69 73 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e  is is part of an
38ecc 20 53 51 4c 69 74 65 20 6d 6f 64 75 6c 65 20 69   SQLite module i
38ecd 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 66 75 6c 6c  mplementing full
38ece 2d 74 65 78 74 20 73 65 61 72 63 68 2e 0a 2a 2a  -text search..**
38ecf 20 54 68 69 73 20 70 61 72 74 69 63 75 6c 61 72   This particular
38ed0 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73   file implements
38ed1 20 74 68 65 20 67 65 6e 65 72 69 63 20 74 6f 6b   the generic tok
38ed2 65 6e 69 7a 65 72 20 69 6e 74 65 72 66 61 63 65  enizer interface
38ed3 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ..*/../*.** The 
38ed4 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
38ed5 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c  e is only compil
38ed6 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ed if:.**.**    
38ed7 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75   * The FTS3 modu
38ed8 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c  le is being buil
38ed9 74 20 61 73 20 61 6e 20 65 78 74 65 6e 73 69 6f  t as an extensio
38eda 6e 0a 2a 2a 20 20 20 20 20 20 20 28 69 6e 20 77  n.**       (in w
38edb 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45  hich case SQLITE
38edc 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20 64 65 66  _CORE is not def
38edd 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20  ined), or.**.** 
38ede 20 20 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d      * The FTS3 m
38edf 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62  odule is being b
38ee0 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 63 6f  uilt into the co
38ee1 72 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 53  re of.**       S
38ee2 51 4c 69 74 65 20 28 69 6e 20 77 68 69 63 68 20  QLite (in which 
38ee3 63 61 73 65 20 53 51 4c 49 54 45 5f 45 4e 41 42  case SQLITE_ENAB
38ee4 4c 45 5f 46 54 53 33 20 69 73 20 64 65 66 69 6e  LE_FTS3 is defin
38ee5 65 64 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66  ed)..*/.#if !def
38ee6 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
38ee7 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
38ee8 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29  ITE_ENABLE_FTS3)
38ee9 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
38eea 5f 43 4f 52 45 0a 20 20 53 51 4c 49 54 45 5f 45  _CORE.  SQLITE_E
38eeb 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a 23  XTENSION_INIT1.#
38eec 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d  endif.../*.** Im
38eed 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
38eee 74 68 65 20 53 51 4c 20 73 63 61 6c 61 72 20 66  the SQL scalar f
38eef 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 63 63 65  unction for acce
38ef0 73 73 69 6e 67 20 74 68 65 20 75 6e 64 65 72 6c  ssing the underl
38ef1 79 69 6e 67 20 0a 2a 2a 20 68 61 73 68 20 74 61  ying .** hash ta
38ef2 62 6c 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69  ble. This functi
38ef3 6f 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64  on may be called
38ef4 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
38ef5 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c 66 75 6e  **   SELECT <fun
38ef6 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79  ction-name>(<key
38ef7 2d 6e 61 6d 65 3e 29 3b 0a 2a 2a 20 20 20 53 45  -name>);.**   SE
38ef8 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e  LECT <function-n
38ef9 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65 3e 2c  ame>(<key-name>,
38efa 20 3c 70 6f 69 6e 74 65 72 3e 29 3b 0a 2a 2a 0a   <pointer>);.**.
38efb 2a 2a 20 77 68 65 72 65 20 3c 66 75 6e 63 74 69  ** where <functi
38efc 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20 74 68 65 20  on-name> is the 
38efd 6e 61 6d 65 20 70 61 73 73 65 64 20 61 73 20 74  name passed as t
38efe 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
38eff 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 73 71 6c  nt.** to the sql
38f00 69 74 65 33 46 74 73 33 49 6e 69 74 48 61 73 68  ite3Fts3InitHash
38f01 54 61 62 6c 65 28 29 20 66 75 6e 63 74 69 6f 6e  Table() function
38f02 20 28 65 2e 67 2e 20 27 66 74 73 33 5f 74 6f 6b   (e.g. 'fts3_tok
38f03 65 6e 69 7a 65 72 27 29 2e 0a 2a 2a 0a 2a 2a 20  enizer')..**.** 
38f04 49 66 20 74 68 65 20 3c 70 6f 69 6e 74 65 72 3e  If the <pointer>
38f05 20 61 72 67 75 6d 65 6e 74 20 69 73 20 73 70 65   argument is spe
38f06 63 69 66 69 65 64 2c 20 69 74 20 6d 75 73 74 20  cified, it must 
38f07 62 65 20 61 20 62 6c 6f 62 20 76 61 6c 75 65 0a  be a blob value.
38f08 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 20  ** containing a 
38f09 70 6f 69 6e 74 65 72 20 74 6f 20 62 65 20 73 74  pointer to be st
38f0a 6f 72 65 64 20 61 73 20 74 68 65 20 68 61 73 68  ored as the hash
38f0b 20 64 61 74 61 20 63 6f 72 72 65 73 70 6f 6e 64   data correspond
38f0c 69 6e 67 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74  ing.** to the st
38f0d 72 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d 65 3e 2e  ring <key-name>.
38f0e 20 49 66 20 3c 70 6f 69 6e 74 65 72 3e 20 69 73   If <pointer> is
38f0f 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 2c 20   not specified, 
38f10 74 68 65 6e 0a 2a 2a 20 74 68 65 20 73 74 72 69  then.** the stri
38f11 6e 67 20 3c 6b 65 79 2d 6e 61 6d 65 3e 20 6d 75  ng <key-name> mu
38f12 73 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  st already exist
38f13 20 69 6e 20 74 68 65 20 68 61 73 20 74 61 62 6c   in the has tabl
38f14 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 2a 2a  e. Otherwise,.**
38f15 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74   an error is ret
38f16 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  urned..**.** Whe
38f17 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
38f18 3c 70 6f 69 6e 74 65 72 3e 20 61 72 67 75 6d 65  <pointer> argume
38f19 6e 74 20 69 73 20 73 70 65 63 69 66 69 65 64 2c  nt is specified,
38f1a 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   the value retur
38f1b 6e 65 64 0a 2a 2a 20 69 73 20 61 20 62 6c 6f 62  ned.** is a blob
38f1c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
38f1d 70 6f 69 6e 74 65 72 20 73 74 6f 72 65 64 20 61  pointer stored a
38f1e 73 20 74 68 65 20 68 61 73 68 20 64 61 74 61 20  s the hash data 
38f1f 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a  corresponding.**
38f20 20 74 6f 20 73 74 72 69 6e 67 20 3c 6b 65 79 2d   to string <key-
38f21 6e 61 6d 65 3e 20 28 61 66 74 65 72 20 74 68 65  name> (after the
38f22 20 68 61 73 68 2d 74 61 62 6c 65 20 69 73 20 75   hash-table is u
38f23 70 64 61 74 65 64 2c 20 69 66 20 61 70 70 6c 69  pdated, if appli
38f24 63 61 62 6c 65 29 2e 0a 2a 2f 0a 73 74 61 74 69  cable)..*/.stati
38f25 63 20 76 6f 69 64 20 73 63 61 6c 61 72 46 75 6e  c void scalarFun
38f26 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
38f27 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
38f28 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
38f29 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
38f2a 76 0a 29 7b 0a 20 20 66 74 73 33 48 61 73 68 20  v.){.  fts3Hash 
38f2b 2a 70 48 61 73 68 3b 0a 20 20 76 6f 69 64 20 2a  *pHash;.  void *
38f2c 70 50 74 72 20 3d 20 30 3b 0a 20 20 63 6f 6e 73  pPtr = 0;.  cons
38f2d 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
38f2e 2a 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e 4e  *zName;.  int nN
38f2f 61 6d 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ame;..  assert( 
38f30 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72 67 63 3d  argc==1 || argc=
38f31 3d 32 20 29 3b 0a 0a 20 20 70 48 61 73 68 20 3d  =2 );..  pHash =
38f32 20 28 66 74 73 33 48 61 73 68 20 2a 29 73 71 6c   (fts3Hash *)sql
38f33 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63  ite3_user_data(c
38f34 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 7a 4e 61 6d  ontext);..  zNam
38f35 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
38f36 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
38f37 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74  .  nName = sqlit
38f38 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
38f39 72 67 76 5b 30 5d 29 2b 31 3b 0a 0a 20 20 69 66  rgv[0])+1;..  if
38f3a 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20  ( argc==2 ){.   
38f3b 20 76 6f 69 64 20 2a 70 4f 6c 64 3b 0a 20 20 20   void *pOld;.   
38f3c 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33   int n = sqlite3
38f3d 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
38f3e 76 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e  v[1]);.    if( n
38f3f 21 3d 73 69 7a 65 6f 66 28 70 50 74 72 29 20 29  !=sizeof(pPtr) )
38f40 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
38f41 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e  result_error(con
38f42 74 65 78 74 2c 20 22 61 72 67 75 6d 65 6e 74 20  text, "argument 
38f43 74 79 70 65 20 6d 69 73 6d 61 74 63 68 22 2c 20  type mismatch", 
38f44 2d 31 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  -1);.      retur
38f45 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 74  n;.    }.    pPt
38f46 72 20 3d 20 2a 28 76 6f 69 64 20 2a 2a 29 73 71  r = *(void **)sq
38f47 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
38f48 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 70  (argv[1]);.    p
38f49 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33 46 74 73  Old = sqlite3Fts
38f4a 33 48 61 73 68 49 6e 73 65 72 74 28 70 48 61 73  3HashInsert(pHas
38f4b 68 2c 20 28 76 6f 69 64 20 2a 29 7a 4e 61 6d 65  h, (void *)zName
38f4c 2c 20 6e 4e 61 6d 65 2c 20 70 50 74 72 29 3b 0a  , nName, pPtr);.
38f4d 20 20 20 20 69 66 28 20 70 4f 6c 64 3d 3d 70 50      if( pOld==pP
38f4e 74 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  tr ){.      sqli
38f4f 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
38f50 28 63 6f 6e 74 65 78 74 2c 20 22 6f 75 74 20 6f  (context, "out o
38f51 66 20 6d 65 6d 6f 72 79 22 2c 20 2d 31 29 3b 0a  f memory", -1);.
38f52 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
38f53 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
38f54 20 70 50 74 72 20 3d 20 73 71 6c 69 74 65 33 46   pPtr = sqlite3F
38f55 74 73 33 48 61 73 68 46 69 6e 64 28 70 48 61 73  ts3HashFind(pHas
38f56 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  h, zName, nName)
38f57 3b 0a 20 20 20 20 69 66 28 20 21 70 50 74 72 20  ;.    if( !pPtr 
38f58 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  ){.      char *z
38f59 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Err = sqlite3_mp
38f5a 72 69 6e 74 66 28 22 75 6e 6b 6e 6f 77 6e 20 74  rintf("unknown t
38f5b 6f 6b 65 6e 69 7a 65 72 3a 20 25 73 22 2c 20 7a  okenizer: %s", z
38f5c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  Name);.      sql
38f5d 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
38f5e 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c  r(context, zErr,
38f5f 20 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   -1);.      sqli
38f60 74 65 33 5f 66 72 65 65 28 7a 45 72 72 29 3b 0a  te3_free(zErr);.
38f61 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
38f62 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74    }.  }..  sqlit
38f63 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 63  e3_result_blob(c
38f64 6f 6e 74 65 78 74 2c 20 28 76 6f 69 64 20 2a 29  ontext, (void *)
38f65 26 70 50 74 72 2c 20 73 69 7a 65 6f 66 28 70 50  &pPtr, sizeof(pP
38f66 74 72 29 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  tr), SQLITE_TRAN
38f67 53 49 45 4e 54 29 3b 0a 7d 0a 0a 23 69 66 64 65  SIENT);.}..#ifde
38f68 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 0a 0a  f SQLITE_TEST...
38f69 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
38f6a 74 69 6f 6e 20 6f 66 20 61 20 73 70 65 63 69 61  tion of a specia
38f6b 6c 20 53 51 4c 20 73 63 61 6c 61 72 20 66 75 6e  l SQL scalar fun
38f6c 63 74 69 6f 6e 20 66 6f 72 20 74 65 73 74 69 6e  ction for testin
38f6d 67 20 74 6f 6b 65 6e 69 7a 65 72 73 20 0a 2a 2a  g tokenizers .**
38f6e 20 64 65 73 69 67 6e 65 64 20 74 6f 20 62 65 20   designed to be 
38f6f 75 73 65 64 20 69 6e 20 63 6f 6e 63 65 72 74 20  used in concert 
38f70 77 69 74 68 20 74 68 65 20 54 63 6c 20 74 65 73  with the Tcl tes
38f71 74 69 6e 67 20 66 72 61 6d 65 77 6f 72 6b 2e 20  ting framework. 
38f72 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  This.** function
38f73 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20   must be called 
38f74 77 69 74 68 20 74 77 6f 20 61 72 67 75 6d 65 6e  with two argumen
38f75 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45  ts:.**.**   SELE
38f76 43 54 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d  CT <function-nam
38f77 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65 3e 2c 20 3c  e>(<key-name>, <
38f78 69 6e 70 75 74 2d 73 74 72 69 6e 67 3e 29 3b 0a  input-string>);.
38f79 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c 66 75 6e  **   SELECT <fun
38f7a 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79  ction-name>(<key
38f7b 2d 6e 61 6d 65 3e 2c 20 3c 70 6f 69 6e 74 65 72  -name>, <pointer
38f7c 3e 29 3b 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20  >);.**.** where 
38f7d 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 20  <function-name> 
38f7e 69 73 20 74 68 65 20 6e 61 6d 65 20 70 61 73 73  is the name pass
38f7f 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  ed as the second
38f80 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20   argument.** to 
38f81 74 68 65 20 73 71 6c 69 74 65 33 46 74 73 33 49  the sqlite3Fts3I
38f82 6e 69 74 48 61 73 68 54 61 62 6c 65 28 29 20 66  nitHashTable() f
38f83 75 6e 63 74 69 6f 6e 20 28 65 2e 67 2e 20 27 66  unction (e.g. 'f
38f84 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 27 29 0a  ts3_tokenizer').
38f85 2a 2a 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 20  ** concatenated 
38f86 77 69 74 68 20 74 68 65 20 73 74 72 69 6e 67 20  with the string 
38f87 27 5f 74 65 73 74 27 20 28 65 2e 67 2e 20 27 66  '_test' (e.g. 'f
38f88 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 74 65  ts3_tokenizer_te
38f89 73 74 27 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st')..**.** The 
38f8a 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
38f8b 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 6d 61  a string that ma
38f8c 79 20 62 65 20 69 6e 74 65 72 70 72 65 74 65 64  y be interpreted
38f8d 20 61 73 20 61 20 54 63 6c 0a 2a 2a 20 6c 69 73   as a Tcl.** lis
38f8e 74 2e 20 46 6f 72 20 65 61 63 68 20 74 6f 6b 65  t. For each toke
38f8f 6e 20 69 6e 20 74 68 65 20 3c 69 6e 70 75 74 2d  n in the <input-
38f90 73 74 72 69 6e 67 3e 2c 20 74 68 72 65 65 20 65  string>, three e
38f91 6c 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 61  lements are.** a
38f92 64 64 65 64 20 74 6f 20 74 68 65 20 72 65 74 75  dded to the retu
38f93 72 6e 65 64 20 6c 69 73 74 2e 20 54 68 65 20 66  rned list. The f
38f94 69 72 73 74 20 69 73 20 74 68 65 20 74 6f 6b 65  irst is the toke
38f95 6e 20 70 6f 73 69 74 69 6f 6e 2c 20 74 68 65 20  n position, the 
38f96 0a 2a 2a 20 73 65 63 6f 6e 64 20 69 73 20 74 68  .** second is th
38f97 65 20 74 6f 6b 65 6e 20 74 65 78 74 20 28 66 6f  e token text (fo
38f98 6c 64 65 64 2c 20 73 74 65 6d 6d 65 64 2c 20 65  lded, stemmed, e
38f99 74 63 2e 29 20 61 6e 64 20 74 68 65 20 74 68 69  tc.) and the thi
38f9a 72 64 20 69 73 20 74 68 65 0a 2a 2a 20 73 75 62  rd is the.** sub
38f9b 73 74 72 69 6e 67 20 6f 66 20 3c 69 6e 70 75 74  string of <input
38f9c 2d 73 74 72 69 6e 67 3e 20 61 73 73 6f 63 69 61  -string> associa
38f9d 74 65 64 20 77 69 74 68 20 74 68 65 20 74 6f 6b  ted with the tok
38f9e 65 6e 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  en. For example,
38f9f 20 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 62   .** using the b
38fa0 75 69 6c 74 2d 69 6e 20 22 73 69 6d 70 6c 65 22  uilt-in "simple"
38fa1 20 74 6f 6b 65 6e 69 7a 65 72 3a 0a 2a 2a 0a 2a   tokenizer:.**.*
38fa2 2a 20 20 20 53 45 4c 45 43 54 20 66 74 73 5f 74  *   SELECT fts_t
38fa3 6f 6b 65 6e 69 7a 65 72 5f 74 65 73 74 28 27 73  okenizer_test('s
38fa4 69 6d 70 6c 65 27 2c 20 27 49 20 64 6f 6e 27 74  imple', 'I don't
38fa5 20 73 65 65 20 68 6f 77 27 29 3b 0a 2a 2a 0a 2a   see how');.**.*
38fa6 2a 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 74 68  * will return th
38fa7 65 20 73 74 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  e string:.**.** 
38fa8 20 20 22 7b 30 20 69 20 49 20 31 20 64 6f 6e 74    "{0 i I 1 dont
38fa9 20 64 6f 6e 27 74 20 32 20 73 65 65 20 73 65 65   don't 2 see see
38faa 20 33 20 68 6f 77 20 68 6f 77 7d 22 0a 2a 2a 20   3 how how}".** 
38fab 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69    .*/.static voi
38fac 64 20 74 65 73 74 46 75 6e 63 28 0a 20 20 73 71  d testFunc(.  sq
38fad 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
38fae 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
38faf 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
38fb0 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
38fb1 66 74 73 33 48 61 73 68 20 2a 70 48 61 73 68 3b  fts3Hash *pHash;
38fb2 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
38fb3 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 3b 0a  izer_module *p;.
38fb4 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
38fb5 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 20  zer *pTokenizer 
38fb6 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74  = 0;.  sqlite3_t
38fb7 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
38fb8 2a 70 43 73 72 20 3d 20 30 3b 0a 0a 20 20 63 6f  *pCsr = 0;..  co
38fb9 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72 20 3d  nst char *zErr =
38fba 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61   0;..  const cha
38fbb 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74 20  r *zName;.  int 
38fbc 6e 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73 74 20 63  nName;.  const c
38fbd 68 61 72 20 2a 7a 49 6e 70 75 74 3b 0a 20 20 69  har *zInput;.  i
38fbe 6e 74 20 6e 49 6e 70 75 74 3b 0a 0a 20 20 63 6f  nt nInput;..  co
38fbf 6e 73 74 20 63 68 61 72 20 2a 7a 41 72 67 20 3d  nst char *zArg =
38fc0 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61   0;..  const cha
38fc1 72 20 2a 7a 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74  r *zToken;.  int
38fc2 20 6e 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 69   nToken;.  int i
38fc3 53 74 61 72 74 3b 0a 20 20 69 6e 74 20 69 45 6e  Start;.  int iEn
38fc4 64 3b 0a 20 20 69 6e 74 20 69 50 6f 73 3b 0a 0a  d;.  int iPos;..
38fc5 20 20 54 63 6c 5f 4f 62 6a 20 2a 70 52 65 74 3b    Tcl_Obj *pRet;
38fc6 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ..  assert( argc
38fc7 3d 3d 32 20 7c 7c 20 61 72 67 63 3d 3d 33 20 29  ==2 || argc==3 )
38fc8 3b 0a 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c  ;..  nName = sql
38fc9 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
38fca 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7a 4e 61  (argv[0]);.  zNa
38fcb 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  me = (const char
38fcc 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
38fcd 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
38fce 20 20 6e 49 6e 70 75 74 20 3d 20 73 71 6c 69 74    nInput = sqlit
38fcf 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
38fd0 72 67 76 5b 61 72 67 63 2d 31 5d 29 3b 0a 20 20  rgv[argc-1]);.  
38fd1 7a 49 6e 70 75 74 20 3d 20 28 63 6f 6e 73 74 20  zInput = (const 
38fd2 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76  char *)sqlite3_v
38fd3 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 61  alue_text(argv[a
38fd4 72 67 63 2d 31 5d 29 3b 0a 0a 20 20 69 66 28 20  rgc-1]);..  if( 
38fd5 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20 7a  argc==3 ){.    z
38fd6 41 72 67 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  Arg = (const cha
38fd7 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  r *)sqlite3_valu
38fd8 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b  e_text(argv[1]);
38fd9 0a 20 20 7d 0a 0a 20 20 70 48 61 73 68 20 3d 20  .  }..  pHash = 
38fda 28 66 74 73 33 48 61 73 68 20 2a 29 73 71 6c 69  (fts3Hash *)sqli
38fdb 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f  te3_user_data(co
38fdc 6e 74 65 78 74 29 3b 0a 20 20 70 20 3d 20 28 73  ntext);.  p = (s
38fdd 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
38fde 5f 6d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65  _module *)sqlite
38fdf 33 46 74 73 33 48 61 73 68 46 69 6e 64 28 70 48  3Fts3HashFind(pH
38fe0 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  ash, zName, nNam
38fe1 65 2b 31 29 3b 0a 0a 20 20 69 66 28 20 21 70 20  e+1);..  if( !p 
38fe2 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 72  ){.    char *zEr
38fe3 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  r = sqlite3_mpri
38fe4 6e 74 66 28 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b  ntf("unknown tok
38fe5 65 6e 69 7a 65 72 3a 20 25 73 22 2c 20 7a 4e 61  enizer: %s", zNa
38fe6 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  me);.    sqlite3
38fe7 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
38fe8 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29  ntext, zErr, -1)
38fe9 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
38fea 65 65 28 7a 45 72 72 29 3b 0a 20 20 20 20 72 65  ee(zErr);.    re
38feb 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 52 65  turn;.  }..  pRe
38fec 74 20 3d 20 54 63 6c 5f 4e 65 77 4f 62 6a 28 29  t = Tcl_NewObj()
38fed 3b 0a 20 20 54 63 6c 5f 49 6e 63 72 52 65 66 43  ;.  Tcl_IncrRefC
38fee 6f 75 6e 74 28 70 52 65 74 29 3b 0a 0a 20 20 69  ount(pRet);..  i
38fef 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d  f( SQLITE_OK!=p-
38ff0 3e 78 43 72 65 61 74 65 28 7a 41 72 67 20 3f 20  >xCreate(zArg ? 
38ff1 31 20 3a 20 30 2c 20 26 7a 41 72 67 2c 20 26 70  1 : 0, &zArg, &p
38ff2 54 6f 6b 65 6e 69 7a 65 72 29 20 29 7b 0a 20 20  Tokenizer) ){.  
38ff3 20 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72 20    zErr = "error 
38ff4 69 6e 20 78 43 72 65 61 74 65 28 29 22 3b 0a 20  in xCreate()";. 
38ff5 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 3b 0a     goto finish;.
38ff6 20 20 7d 0a 20 20 70 54 6f 6b 65 6e 69 7a 65 72    }.  pTokenizer
38ff7 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 3b 0a 20  ->pModule = p;. 
38ff8 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
38ff9 70 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69  p->xOpen(pTokeni
38ffa 7a 65 72 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e  zer, zInput, nIn
38ffb 70 75 74 2c 20 26 70 43 73 72 29 20 29 7b 0a 20  put, &pCsr) ){. 
38ffc 20 20 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72     zErr = "error
38ffd 20 69 6e 20 78 4f 70 65 6e 28 29 22 3b 0a 20 20   in xOpen()";.  
38ffe 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 3b 0a 20    goto finish;. 
38fff 20 7d 0a 20 20 70 43 73 72 2d 3e 70 54 6f 6b 65   }.  pCsr->pToke
39000 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a  nizer = pTokeniz
39001 65 72 3b 0a 0a 20 20 77 68 69 6c 65 28 20 53 51  er;..  while( SQ
39002 4c 49 54 45 5f 4f 4b 3d 3d 70 2d 3e 78 4e 65 78  LITE_OK==p->xNex
39003 74 28 70 43 73 72 2c 20 26 7a 54 6f 6b 65 6e 2c  t(pCsr, &zToken,
39004 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 53 74 61 72   &nToken, &iStar
39005 74 2c 20 26 69 45 6e 64 2c 20 26 69 50 6f 73 29  t, &iEnd, &iPos)
39006 20 29 7b 0a 20 20 20 20 54 63 6c 5f 4c 69 73 74   ){.    Tcl_List
39007 4f 62 6a 41 70 70 65 6e 64 45 6c 65 6d 65 6e 74  ObjAppendElement
39008 28 30 2c 20 70 52 65 74 2c 20 54 63 6c 5f 4e 65  (0, pRet, Tcl_Ne
39009 77 49 6e 74 4f 62 6a 28 69 50 6f 73 29 29 3b 0a  wIntObj(iPos));.
3900a 20 20 20 20 54 63 6c 5f 4c 69 73 74 4f 62 6a 41      Tcl_ListObjA
3900b 70 70 65 6e 64 45 6c 65 6d 65 6e 74 28 30 2c 20  ppendElement(0, 
3900c 70 52 65 74 2c 20 54 63 6c 5f 4e 65 77 53 74 72  pRet, Tcl_NewStr
3900d 69 6e 67 4f 62 6a 28 7a 54 6f 6b 65 6e 2c 20 6e  ingObj(zToken, n
3900e 54 6f 6b 65 6e 29 29 3b 0a 20 20 20 20 7a 54 6f  Token));.    zTo
3900f 6b 65 6e 20 3d 20 26 7a 49 6e 70 75 74 5b 69 53  ken = &zInput[iS
39010 74 61 72 74 5d 3b 0a 20 20 20 20 6e 54 6f 6b 65  tart];.    nToke
39011 6e 20 3d 20 69 45 6e 64 2d 69 53 74 61 72 74 3b  n = iEnd-iStart;
39012 0a 20 20 20 20 54 63 6c 5f 4c 69 73 74 4f 62 6a  .    Tcl_ListObj
39013 41 70 70 65 6e 64 45 6c 65 6d 65 6e 74 28 30 2c  AppendElement(0,
39014 20 70 52 65 74 2c 20 54 63 6c 5f 4e 65 77 53 74   pRet, Tcl_NewSt
39015 72 69 6e 67 4f 62 6a 28 7a 54 6f 6b 65 6e 2c 20  ringObj(zToken, 
39016 6e 54 6f 6b 65 6e 29 29 3b 0a 20 20 7d 0a 0a 20  nToken));.  }.. 
39017 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
39018 70 2d 3e 78 43 6c 6f 73 65 28 70 43 73 72 29 20  p->xClose(pCsr) 
39019 29 7b 0a 20 20 20 20 7a 45 72 72 20 3d 20 22 65  ){.    zErr = "e
3901a 72 72 6f 72 20 69 6e 20 78 43 6c 6f 73 65 28 29  rror in xClose()
3901b 22 3b 0a 20 20 20 20 67 6f 74 6f 20 66 69 6e 69  ";.    goto fini
3901c 73 68 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51  sh;.  }.  if( SQ
3901d 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 44 65 73  LITE_OK!=p->xDes
3901e 74 72 6f 79 28 70 54 6f 6b 65 6e 69 7a 65 72 29  troy(pTokenizer)
3901f 20 29 7b 0a 20 20 20 20 7a 45 72 72 20 3d 20 22   ){.    zErr = "
39020 65 72 72 6f 72 20 69 6e 20 78 44 65 73 74 72 6f  error in xDestro
39021 79 28 29 22 3b 0a 20 20 20 20 67 6f 74 6f 20 66  y()";.    goto f
39022 69 6e 69 73 68 3b 0a 20 20 7d 0a 0a 66 69 6e 69  inish;.  }..fini
39023 73 68 3a 0a 20 20 69 66 28 20 7a 45 72 72 20 29  sh:.  if( zErr )
39024 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
39025 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65  sult_error(conte
39026 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20  xt, zErr, -1);. 
39027 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
39028 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
39029 63 6f 6e 74 65 78 74 2c 20 54 63 6c 5f 47 65 74  context, Tcl_Get
3902a 53 74 72 69 6e 67 28 70 52 65 74 29 2c 20 2d 31  String(pRet), -1
3902b 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
3902c 4e 54 29 3b 0a 20 20 7d 0a 20 20 54 63 6c 5f 44  NT);.  }.  Tcl_D
3902d 65 63 72 52 65 66 43 6f 75 6e 74 28 70 52 65 74  ecrRefCount(pRet
3902e 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 0a 69 6e 74  );.}..static.int
3902f 20 72 65 67 69 73 74 65 72 54 6f 6b 65 6e 69 7a   registerTokeniz
39030 65 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  er(.  sqlite3 *d
39031 62 2c 20 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d  b, .  char *zNam
39032 65 2c 20 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  e, .  const sqli
39033 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
39034 64 75 6c 65 20 2a 70 0a 29 7b 0a 20 20 69 6e 74  dule *p.){.  int
39035 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73   rc;.  sqlite3_s
39036 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 63 6f  tmt *pStmt;.  co
39037 6e 73 74 20 63 68 61 72 20 7a 53 71 6c 5b 5d 20  nst char zSql[] 
39038 3d 20 22 53 45 4c 45 43 54 20 66 74 73 33 5f 74  = "SELECT fts3_t
39039 6f 6b 65 6e 69 7a 65 72 28 3f 2c 20 3f 29 22 3b  okenizer(?, ?)";
3903a 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
3903b 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20  _prepare_v2(db, 
3903c 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74  zSql, -1, &pStmt
3903d 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  , 0);.  if( rc!=
3903e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
3903f 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
39040 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
39041 74 65 78 74 28 70 53 74 6d 74 2c 20 31 2c 20 7a  text(pStmt, 1, z
39042 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45  Name, -1, SQLITE
39043 5f 53 54 41 54 49 43 29 3b 0a 20 20 73 71 6c 69  _STATIC);.  sqli
39044 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 70 53  te3_bind_blob(pS
39045 74 6d 74 2c 20 32 2c 20 26 70 2c 20 73 69 7a 65  tmt, 2, &p, size
39046 6f 66 28 70 29 2c 20 53 51 4c 49 54 45 5f 53 54  of(p), SQLITE_ST
39047 41 54 49 43 29 3b 0a 20 20 73 71 6c 69 74 65 33  ATIC);.  sqlite3
39048 5f 73 74 65 70 28 70 53 74 6d 74 29 3b 0a 0a 20  _step(pStmt);.. 
39049 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f   return sqlite3_
3904a 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b  finalize(pStmt);
3904b 0a 7d 0a 0a 73 74 61 74 69 63 0a 69 6e 74 20 71  .}..static.int q
3904c 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72 28 0a 20  ueryTokenizer(. 
3904d 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20   sqlite3 *db, . 
3904e 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 0a   char *zName,  .
3904f 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
39050 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
39051 20 2a 2a 70 70 0a 29 7b 0a 20 20 69 6e 74 20 72   **pp.){.  int r
39052 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  c;.  sqlite3_stm
39053 74 20 2a 70 53 74 6d 74 3b 0a 20 20 63 6f 6e 73  t *pStmt;.  cons
39054 74 20 63 68 61 72 20 7a 53 71 6c 5b 5d 20 3d 20  t char zSql[] = 
39055 22 53 45 4c 45 43 54 20 66 74 73 33 5f 74 6f 6b  "SELECT fts3_tok
39056 65 6e 69 7a 65 72 28 3f 29 22 3b 0a 0a 20 20 2a  enizer(?)";..  *
39057 70 70 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73  pp = 0;.  rc = s
39058 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
39059 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20  2(db, zSql, -1, 
3905a 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66  &pStmt, 0);.  if
3905b 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
3905c 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
3905d 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33  ;.  }..  sqlite3
3905e 5f 62 69 6e 64 5f 74 65 78 74 28 70 53 74 6d 74  _bind_text(pStmt
3905f 2c 20 31 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20  , 1, zName, -1, 
39060 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
39061 20 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57    if( SQLITE_ROW
39062 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70  ==sqlite3_step(p
39063 53 74 6d 74 29 20 29 7b 0a 20 20 20 20 69 66 28  Stmt) ){.    if(
39064 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
39065 74 79 70 65 28 70 53 74 6d 74 2c 20 30 29 3d 3d  type(pStmt, 0)==
39066 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20  SQLITE_BLOB ){. 
39067 20 20 20 20 20 6d 65 6d 63 70 79 28 70 70 2c 20       memcpy(pp, 
39068 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
39069 6c 6f 62 28 70 53 74 6d 74 2c 20 30 29 2c 20 73  lob(pStmt, 0), s
3906a 69 7a 65 6f 66 28 2a 70 70 29 29 3b 0a 20 20 20  izeof(*pp));.   
3906b 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
3906c 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
3906d 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 53 51 4c  e(pStmt);.}..SQL
3906e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
3906f 20 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70   sqlite3Fts3Simp
39070 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  leTokenizerModul
39071 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e(sqlite3_tokeni
39072 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
39073 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 0a 2f 2a  **ppModule);../*
39074 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
39075 6f 6e 20 6f 66 20 74 68 65 20 73 63 61 6c 61 72  on of the scalar
39076 20 66 75 6e 63 74 69 6f 6e 20 66 74 73 33 5f 74   function fts3_t
39077 6f 6b 65 6e 69 7a 65 72 5f 69 6e 74 65 72 6e 61  okenizer_interna
39078 6c 5f 74 65 73 74 28 29 2e 0a 2a 2a 20 54 68 69  l_test()..** Thi
39079 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
3907a 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 6f  ed for testing o
3907b 6e 6c 79 2c 20 69 74 20 69 73 20 6e 6f 74 20 69  nly, it is not i
3907c 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 0a 2a  ncluded in the.*
3907d 2a 20 62 75 69 6c 64 20 75 6e 6c 65 73 73 20 53  * build unless S
3907e 51 4c 49 54 45 5f 54 45 53 54 20 69 73 20 64 65  QLITE_TEST is de
3907f 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  fined..**.** The
39080 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73   purpose of this
39081 20 69 73 20 74 6f 20 74 65 73 74 20 74 68 61 74   is to test that
39082 20 74 68 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69   the fts3_tokeni
39083 7a 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a  zer() function.*
39084 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73  * can be used as
39085 20 64 65 73 69 67 6e 65 64 20 62 79 20 74 68 65   designed by the
39086 20 43 2d 63 6f 64 65 20 69 6e 20 74 68 65 20 71   C-code in the q
39087 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72 20 61 6e  ueryTokenizer an
39088 64 0a 2a 2a 20 72 65 67 69 73 74 65 72 54 6f 6b  d.** registerTok
39089 65 6e 69 7a 65 72 28 29 20 66 75 6e 63 74 69 6f  enizer() functio
3908a 6e 73 20 61 62 6f 76 65 2e 20 54 68 65 73 65 20  ns above. These 
3908b 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20 61 72  two functions ar
3908c 65 20 72 65 70 65 61 74 65 64 0a 2a 2a 20 69 6e  e repeated.** in
3908d 20 74 68 65 20 52 45 41 44 4d 45 2e 74 6f 6b 65   the README.toke
3908e 6e 69 7a 65 72 20 66 69 6c 65 20 61 73 20 61 6e  nizer file as an
3908f 20 65 78 61 6d 70 6c 65 2c 20 73 6f 20 69 74 20   example, so it 
39090 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 0a  is important to.
39091 2a 2a 20 74 65 73 74 20 74 68 65 6d 2e 0a 2a 2a  ** test them..**
39092 0a 2a 2a 20 54 6f 20 72 75 6e 20 74 68 65 20 74  .** To run the t
39093 65 73 74 73 2c 20 65 76 61 6c 75 61 74 65 20 74  ests, evaluate t
39094 68 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65  he fts3_tokenize
39095 72 5f 69 6e 74 65 72 6e 61 6c 5f 74 65 73 74 28  r_internal_test(
39096 29 20 73 63 61 6c 61 72 0a 2a 2a 20 66 75 6e 63  ) scalar.** func
39097 74 69 6f 6e 20 77 69 74 68 20 6e 6f 20 61 72 67  tion with no arg
39098 75 6d 65 6e 74 73 2e 20 41 6e 20 61 73 73 65 72  uments. An asser
39099 74 28 29 20 77 69 6c 6c 20 66 61 69 6c 20 69 66  t() will fail if
3909a 20 61 20 70 72 6f 62 6c 65 6d 20 69 73 0a 2a 2a   a problem is.**
3909b 20 64 65 74 65 63 74 65 64 2e 20 69 2e 65 2e 3a   detected. i.e.:
3909c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43  .**.**     SELEC
3909d 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72  T fts3_tokenizer
3909e 5f 69 6e 74 65 72 6e 61 6c 5f 74 65 73 74 28 29  _internal_test()
3909f 3b 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ;.**.*/.static v
390a0 6f 69 64 20 69 6e 74 54 65 73 74 46 75 6e 63 28  oid intTestFunc(
390a1 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
390a2 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
390a3 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
390a4 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
390a5 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 63  ){.  int rc;.  c
390a6 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  onst sqlite3_tok
390a7 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 70  enizer_module *p
390a8 31 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  1;.  const sqlit
390a9 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
390aa 75 6c 65 20 2a 70 32 3b 0a 20 20 73 71 6c 69 74  ule *p2;.  sqlit
390ab 65 33 20 2a 64 62 20 3d 20 28 73 71 6c 69 74 65  e3 *db = (sqlite
390ac 33 20 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 72  3 *)sqlite3_user
390ad 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a  _data(context);.
390ae 0a 20 20 2f 2a 20 54 65 73 74 20 74 68 65 20 71  .  /* Test the q
390af 75 65 72 79 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  uery function */
390b0 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 53 69  .  sqlite3Fts3Si
390b1 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  mpleTokenizerMod
390b2 75 6c 65 28 26 70 31 29 3b 0a 20 20 72 63 20 3d  ule(&p1);.  rc =
390b3 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72 28   queryTokenizer(
390b4 64 62 2c 20 22 73 69 6d 70 6c 65 22 2c 20 26 70  db, "simple", &p
390b5 32 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  2);.  assert( rc
390b6 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  ==SQLITE_OK );. 
390b7 20 61 73 73 65 72 74 28 20 70 31 3d 3d 70 32 20   assert( p1==p2 
390b8 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72 79 54  );.  rc = queryT
390b9 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20 22 6e 6f  okenizer(db, "no
390ba 73 75 63 68 74 6f 6b 65 6e 69 7a 65 72 22 2c 20  suchtokenizer", 
390bb 26 70 32 29 3b 0a 20 20 61 73 73 65 72 74 28 20  &p2);.  assert( 
390bc 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f 52  rc==SQLITE_ERROR
390bd 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 32   );.  assert( p2
390be 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
390bf 20 30 3d 3d 73 74 72 63 6d 70 28 73 71 6c 69 74   0==strcmp(sqlit
390c0 65 33 5f 65 72 72 6d 73 67 28 64 62 29 2c 20 22  e3_errmsg(db), "
390c1 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65  unknown tokenize
390c2 72 3a 20 6e 6f 73 75 63 68 74 6f 6b 65 6e 69 7a  r: nosuchtokeniz
390c3 65 72 22 29 20 29 3b 0a 0a 20 20 2f 2a 20 54 65  er") );..  /* Te
390c4 73 74 20 74 68 65 20 73 74 6f 72 61 67 65 20 66  st the storage f
390c5 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 72 63 20  unction */.  rc 
390c6 3d 20 72 65 67 69 73 74 65 72 54 6f 6b 65 6e 69  = registerTokeni
390c7 7a 65 72 28 64 62 2c 20 22 6e 6f 73 75 63 68 74  zer(db, "nosucht
390c8 6f 6b 65 6e 69 7a 65 72 22 2c 20 70 31 29 3b 0a  okenizer", p1);.
390c9 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
390ca 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20  LITE_OK );.  rc 
390cb 3d 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72  = queryTokenizer
390cc 28 64 62 2c 20 22 6e 6f 73 75 63 68 74 6f 6b 65  (db, "nosuchtoke
390cd 6e 69 7a 65 72 22 2c 20 26 70 32 29 3b 0a 20 20  nizer", &p2);.  
390ce 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
390cf 54 45 5f 4f 4b 20 29 3b 0a 20 20 61 73 73 65 72  TE_OK );.  asser
390d0 74 28 20 70 32 3d 3d 70 31 20 29 3b 0a 0a 20 20  t( p2==p1 );..  
390d1 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
390d2 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 22 6f 6b  ext(context, "ok
390d3 22 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54  ", -1, SQLITE_ST
390d4 41 54 49 43 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66  ATIC);.}..#endif
390d5 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 53  ../*.** Set up S
390d6 51 4c 20 6f 62 6a 65 63 74 73 20 69 6e 20 64 61  QL objects in da
390d7 74 61 62 61 73 65 20 64 62 20 75 73 65 64 20 74  tabase db used t
390d8 6f 20 61 63 63 65 73 73 20 74 68 65 20 63 6f 6e  o access the con
390d9 74 65 6e 74 73 20 6f 66 0a 2a 2a 20 74 68 65 20  tents of.** the 
390da 68 61 73 68 20 74 61 62 6c 65 20 70 6f 69 6e 74  hash table point
390db 65 64 20 74 6f 20 62 79 20 61 72 67 75 6d 65 6e  ed to by argumen
390dc 74 20 70 48 61 73 68 2e 20 54 68 65 20 68 61 73  t pHash. The has
390dd 68 20 74 61 62 6c 65 20 6d 75 73 74 0a 2a 2a 20  h table must.** 
390de 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 73 65 64  been initialised
390df 20 74 6f 20 75 73 65 20 73 74 72 69 6e 67 20 6b   to use string k
390e0 65 79 73 2c 20 61 6e 64 20 74 6f 20 74 61 6b 65  eys, and to take
390e1 20 61 20 70 72 69 76 61 74 65 20 63 6f 70 79 20   a private copy 
390e2 0a 2a 2a 20 6f 66 20 74 68 65 20 6b 65 79 20 77  .** of the key w
390e3 68 65 6e 20 61 20 76 61 6c 75 65 20 69 73 20 69  hen a value is i
390e4 6e 73 65 72 74 65 64 2e 20 69 2e 65 2e 20 62 79  nserted. i.e. by
390e5 20 61 20 63 61 6c 6c 20 73 69 6d 69 6c 61 72 20   a call similar 
390e6 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 73 71 6c  to:.**.**    sql
390e7 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 69 74  ite3Fts3HashInit
390e8 28 70 48 61 73 68 2c 20 46 54 53 33 5f 48 41 53  (pHash, FTS3_HAS
390e9 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a 2a 2a  H_STRING, 1);.**
390ea 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
390eb 6e 20 61 64 64 73 20 61 20 73 63 61 6c 61 72 20  n adds a scalar 
390ec 66 75 6e 63 74 69 6f 6e 20 28 73 65 65 20 68 65  function (see he
390ed 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 61 62 6f  ader comment abo
390ee 76 65 0a 2a 2a 20 73 63 61 6c 61 72 46 75 6e 63  ve.** scalarFunc
390ef 28 29 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  () in this file 
390f0 66 6f 72 20 64 65 74 61 69 6c 73 29 20 61 6e 64  for details) and
390f1 2c 20 69 66 20 45 4e 41 42 4c 45 5f 54 41 42 4c  , if ENABLE_TABL
390f2 45 20 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 20  E is.** defined 
390f3 61 74 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 74  at compilation t
390f4 69 6d 65 2c 20 61 20 74 65 6d 70 6f 72 61 72 79  ime, a temporary
390f5 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 28   virtual table (
390f6 73 65 65 20 68 65 61 64 65 72 20 0a 2a 2a 20 63  see header .** c
390f7 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 20 73 74 72  omment above str
390f8 75 63 74 20 48 61 73 68 54 61 62 6c 65 56 74 61  uct HashTableVta
390f9 62 29 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  b) to the databa
390fa 73 65 20 73 63 68 65 6d 61 2e 20 42 6f 74 68 20  se schema. Both 
390fb 0a 2a 2a 20 70 72 6f 76 69 64 65 20 72 65 61 64  .** provide read
390fc 2f 77 72 69 74 65 20 61 63 63 65 73 73 20 74 6f  /write access to
390fd 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
390fe 20 2a 70 48 61 73 68 2e 0a 2a 2a 0a 2a 2a 20 54   *pHash..**.** T
390ff 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
39100 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  t to this functi
39101 6f 6e 2c 20 7a 4e 61 6d 65 2c 20 69 73 20 75 73  on, zName, is us
39102 65 64 20 61 73 20 74 68 65 20 6e 61 6d 65 0a 2a  ed as the name.*
39103 2a 20 6f 66 20 62 6f 74 68 20 74 68 65 20 73 63  * of both the sc
39104 61 6c 61 72 20 61 6e 64 2c 20 69 66 20 63 72 65  alar and, if cre
39105 61 74 65 64 2c 20 74 68 65 20 76 69 72 74 75 61  ated, the virtua
39106 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  l table..*/.SQLI
39107 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
39108 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48 61  qlite3Fts3InitHa
39109 73 68 54 61 62 6c 65 28 0a 20 20 73 71 6c 69 74  shTable(.  sqlit
3910a 65 33 20 2a 64 62 2c 20 0a 20 20 66 74 73 33 48  e3 *db, .  fts3H
3910b 61 73 68 20 2a 70 48 61 73 68 2c 20 0a 20 20 63  ash *pHash, .  c
3910c 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
3910d 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
3910e 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 76 6f 69 64  QLITE_OK;.  void
3910f 20 2a 70 20 3d 20 28 76 6f 69 64 20 2a 29 70 48   *p = (void *)pH
39110 61 73 68 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74  ash;.  const int
39111 20 61 6e 79 20 3d 20 53 51 4c 49 54 45 5f 41 4e   any = SQLITE_AN
39112 59 3b 0a 20 20 63 68 61 72 20 2a 7a 54 65 73 74  Y;.  char *zTest
39113 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 54   = 0;.  char *zT
39114 65 73 74 32 20 3d 20 30 3b 0a 0a 23 69 66 64 65  est2 = 0;..#ifde
39115 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
39116 76 6f 69 64 20 2a 70 64 62 20 3d 20 28 76 6f 69  void *pdb = (voi
39117 64 20 2a 29 64 62 3b 0a 20 20 7a 54 65 73 74 20  d *)db;.  zTest 
39118 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
39119 66 28 22 25 73 5f 74 65 73 74 22 2c 20 7a 4e 61  f("%s_test", zNa
3911a 6d 65 29 3b 0a 20 20 7a 54 65 73 74 32 20 3d 20  me);.  zTest2 = 
3911b 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
3911c 22 25 73 5f 69 6e 74 65 72 6e 61 6c 5f 74 65 73  "%s_internal_tes
3911d 74 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66  t", zName);.  if
3911e 28 20 21 7a 54 65 73 74 20 7c 7c 20 21 7a 54 65  ( !zTest || !zTe
3911f 73 74 32 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  st2 ){.    rc = 
39120 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
39121 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20  }.#endif..  if( 
39122 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20  rc!=SQLITE_OK.  
39123 20 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69 74 65   || (rc = sqlite
39124 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
39125 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 31 2c 20  n(db, zName, 1, 
39126 61 6e 79 2c 20 70 2c 20 73 63 61 6c 61 72 46 75  any, p, scalarFu
39127 6e 63 2c 20 30 2c 20 30 29 29 0a 20 20 20 7c 7c  nc, 0, 0)).   ||
39128 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63   (rc = sqlite3_c
39129 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64  reate_function(d
3912a 62 2c 20 7a 4e 61 6d 65 2c 20 32 2c 20 61 6e 79  b, zName, 2, any
3912b 2c 20 70 2c 20 73 63 61 6c 61 72 46 75 6e 63 2c  , p, scalarFunc,
3912c 20 30 2c 20 30 29 29 0a 23 69 66 64 65 66 20 53   0, 0)).#ifdef S
3912d 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 7c 7c  QLITE_TEST.   ||
3912e 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63   (rc = sqlite3_c
3912f 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64  reate_function(d
39130 62 2c 20 7a 54 65 73 74 2c 20 32 2c 20 61 6e 79  b, zTest, 2, any
39131 2c 20 70 2c 20 74 65 73 74 46 75 6e 63 2c 20 30  , p, testFunc, 0
39132 2c 20 30 29 29 0a 20 20 20 7c 7c 20 28 72 63 20  , 0)).   || (rc 
39133 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  = sqlite3_create
39134 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 54  _function(db, zT
39135 65 73 74 2c 20 33 2c 20 61 6e 79 2c 20 70 2c 20  est, 3, any, p, 
39136 74 65 73 74 46 75 6e 63 2c 20 30 2c 20 30 29 29  testFunc, 0, 0))
39137 0a 20 20 20 7c 7c 20 28 72 63 20 3d 20 73 71 6c  .   || (rc = sql
39138 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
39139 74 69 6f 6e 28 64 62 2c 20 7a 54 65 73 74 32 2c  tion(db, zTest2,
3913a 20 30 2c 20 61 6e 79 2c 20 70 64 62 2c 20 69 6e   0, any, pdb, in
3913b 74 54 65 73 74 46 75 6e 63 2c 20 30 2c 20 30 29  tTestFunc, 0, 0)
3913c 29 0a 23 65 6e 64 69 66 0a 20 20 29 3b 0a 0a 20  ).#endif.  );.. 
3913d 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54   sqlite3_free(zT
3913e 65 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  est);.  sqlite3_
3913f 66 72 65 65 28 7a 54 65 73 74 32 29 3b 0a 20 20  free(zTest2);.  
39140 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65  return rc;.}..#e
39141 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
39142 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c  (SQLITE_CORE) ||
39143 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
39144 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a  ENABLE_FTS3) */.
39145 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
39146 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 74 6f 6b   End of fts3_tok
39147 65 6e 69 7a 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a  enizer.c *******
39148 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39149 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3914a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3914b 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33   Begin file fts3
3914c 5f 74 6f 6b 65 6e 69 7a 65 72 31 2e 63 20 2a 2a  _tokenizer1.c **
3914d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3914e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3914f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4f 63 74 20  ./*.** 2006 Oct 
39150 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  10.**.** The aut
39151 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
39152 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
39153 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
39154 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
39155 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
39156 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
39157 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
39158 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
39159 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
3915a 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
3915b 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
3915c 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
3915d 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
3915e 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
3915f 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
39160 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
39161 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
39162 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39163 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39164 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39165 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39166 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6d 70  ******.**.** Imp
39167 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
39168 68 65 20 22 73 69 6d 70 6c 65 22 20 66 75 6c 6c  he "simple" full
39169 2d 74 65 78 74 2d 73 65 61 72 63 68 20 74 6f 6b  -text-search tok
3916a 65 6e 69 7a 65 72 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  enizer..*/../*.*
3916b 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68  * The code in th
3916c 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20  is file is only 
3916d 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a  compiled if:.**.
3916e 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53  **     * The FTS
3916f 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e  3 module is bein
39170 67 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78  g built as an ex
39171 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20  tension.**      
39172 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20   (in which case 
39173 53 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e  SQLITE_CORE is n
39174 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a  ot defined), or.
39175 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20  **.**     * The 
39176 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62  FTS3 module is b
39177 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f 20  eing built into 
39178 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20  the core of.**  
39179 20 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20       SQLite (in 
3917a 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54  which case SQLIT
3917b 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73  E_ENABLE_FTS3 is
3917c 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69   defined)..*/.#i
3917d 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
3917e 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
3917f 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
39180 5f 46 54 53 33 29 0a 0a 0a 0a 0a 74 79 70 65 64  _FTS3).....typed
39181 65 66 20 73 74 72 75 63 74 20 73 69 6d 70 6c 65  ef struct simple
39182 5f 74 6f 6b 65 6e 69 7a 65 72 20 7b 0a 20 20 73  _tokenizer {.  s
39183 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
39184 20 62 61 73 65 3b 0a 20 20 63 68 61 72 20 64 65   base;.  char de
39185 6c 69 6d 5b 31 32 38 5d 3b 20 20 20 20 20 20 20  lim[128];       
39186 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 20 41 53        /* flag AS
39187 43 49 49 20 64 65 6c 69 6d 69 74 65 72 73 20 2a  CII delimiters *
39188 2f 0a 7d 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e  /.} simple_token
39189 69 7a 65 72 3b 0a 0a 74 79 70 65 64 65 66 20 73  izer;..typedef s
3918a 74 72 75 63 74 20 73 69 6d 70 6c 65 5f 74 6f 6b  truct simple_tok
3918b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 7b 0a  enizer_cursor {.
3918c 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
3918d 7a 65 72 5f 63 75 72 73 6f 72 20 62 61 73 65 3b  zer_cursor base;
3918e 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  .  const char *p
3918f 49 6e 70 75 74 3b 20 20 20 20 20 20 20 20 20 20  Input;          
39190 2f 2a 20 69 6e 70 75 74 20 77 65 20 61 72 65 20  /* input we are 
39191 74 6f 6b 65 6e 69 7a 69 6e 67 20 2a 2f 0a 20 20  tokenizing */.  
39192 69 6e 74 20 6e 42 79 74 65 73 3b 20 20 20 20 20  int nBytes;     
39193 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39194 73 69 7a 65 20 6f 66 20 74 68 65 20 69 6e 70 75  size of the inpu
39195 74 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73  t */.  int iOffs
39196 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  et;             
39197 20 20 20 20 2f 2a 20 63 75 72 72 65 6e 74 20 70      /* current p
39198 6f 73 69 74 69 6f 6e 20 69 6e 20 70 49 6e 70 75  osition in pInpu
39199 74 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 6b 65  t */.  int iToke
3919a 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
3919b 20 20 20 20 2f 2a 20 69 6e 64 65 78 20 6f 66 20      /* index of 
3919c 6e 65 78 74 20 74 6f 6b 65 6e 20 74 6f 20 62 65  next token to be
3919d 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a 20 20 63   returned */.  c
3919e 68 61 72 20 2a 70 54 6f 6b 65 6e 3b 20 20 20 20  har *pToken;    
3919f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
391a0 74 6f 72 61 67 65 20 66 6f 72 20 63 75 72 72 65  torage for curre
391a1 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e  nt token */.  in
391a2 74 20 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65  t nTokenAllocate
391a3 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 73 70  d;         /* sp
391a4 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f  ace allocated to
391a5 20 7a 54 6f 6b 65 6e 20 62 75 66 66 65 72 20 2a   zToken buffer *
391a6 2f 0a 7d 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e  /.} simple_token
391a7 69 7a 65 72 5f 63 75 72 73 6f 72 3b 0a 0a 0a 2f  izer_cursor;.../
391a8 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
391a9 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20  ation */.static 
391aa 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
391ab 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 73  kenizer_module s
391ac 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f  impleTokenizerMo
391ad 64 75 6c 65 3b 0a 0a 73 74 61 74 69 63 20 69 6e  dule;..static in
391ae 74 20 73 69 6d 70 6c 65 44 65 6c 69 6d 28 73 69  t simpleDelim(si
391af 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  mple_tokenizer *
391b0 74 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  t, unsigned char
391b1 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 3c   c){.  return c<
391b2 30 78 38 30 20 26 26 20 74 2d 3e 64 65 6c 69 6d  0x80 && t->delim
391b3 5b 63 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72  [c];.}../*.** Cr
391b4 65 61 74 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e  eate a new token
391b5 69 7a 65 72 20 69 6e 73 74 61 6e 63 65 2e 0a 2a  izer instance..*
391b6 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d  /.static int sim
391b7 70 6c 65 43 72 65 61 74 65 28 0a 20 20 69 6e 74  pleCreate(.  int
391b8 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61   argc, const cha
391b9 72 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c  r * const *argv,
391ba 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
391bb 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a  izer **ppTokeniz
391bc 65 72 0a 29 7b 0a 20 20 73 69 6d 70 6c 65 5f 74  er.){.  simple_t
391bd 6f 6b 65 6e 69 7a 65 72 20 2a 74 3b 0a 0a 20 20  okenizer *t;..  
391be 74 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65  t = (simple_toke
391bf 6e 69 7a 65 72 20 2a 29 20 73 71 6c 69 74 65 33  nizer *) sqlite3
391c0 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a  _malloc(sizeof(*
391c1 74 29 29 3b 0a 20 20 69 66 28 20 74 3d 3d 4e 55  t));.  if( t==NU
391c2 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  LL ) return SQLI
391c3 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 6d 65 6d 73  TE_NOMEM;.  mems
391c4 65 74 28 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28  et(t, 0, sizeof(
391c5 2a 74 29 29 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f  *t));..  /* TODO
391c6 28 73 68 65 73 73 29 20 44 65 6c 69 6d 69 74 65  (shess) Delimite
391c7 72 73 20 6e 65 65 64 20 74 6f 20 72 65 6d 61 69  rs need to remai
391c8 6e 20 74 68 65 20 73 61 6d 65 20 66 72 6f 6d 20  n the same from 
391c9 72 75 6e 20 74 6f 20 72 75 6e 2c 0a 20 20 2a 2a  run to run,.  **
391ca 20 65 6c 73 65 20 77 65 20 6e 65 65 64 20 74 6f   else we need to
391cb 20 72 65 69 6e 64 65 78 2e 20 20 4f 6e 65 20 73   reindex.  One s
391cc 6f 6c 75 74 69 6f 6e 20 77 6f 75 6c 64 20 62 65  olution would be
391cd 20 61 20 6d 65 74 61 2d 74 61 62 6c 65 20 74 6f   a meta-table to
391ce 0a 20 20 2a 2a 20 74 72 61 63 6b 20 73 75 63 68  .  ** track such
391cf 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20   information in 
391d0 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 74 68  the database, th
391d1 65 6e 20 77 65 27 64 20 6f 6e 6c 79 20 77 61 6e  en we'd only wan
391d2 74 20 74 68 69 73 0a 20 20 2a 2a 20 69 6e 66 6f  t this.  ** info
391d3 72 6d 61 74 69 6f 6e 20 6f 6e 20 74 68 65 20 69  rmation on the i
391d4 6e 69 74 69 61 6c 20 63 72 65 61 74 65 2e 0a 20  nitial create.. 
391d5 20 2a 2f 0a 20 20 69 66 28 20 61 72 67 63 3e 31   */.  if( argc>1
391d6 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6e   ){.    int i, n
391d7 20 3d 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 31   = strlen(argv[1
391d8 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  ]);.    for(i=0;
391d9 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20   i<n; i++){.    
391da 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
391db 63 68 20 3d 20 61 72 67 76 5b 31 5d 5b 69 5d 3b  ch = argv[1][i];
391dc 0a 20 20 20 20 20 20 2f 2a 20 57 65 20 65 78 70  .      /* We exp
391dd 6c 69 63 69 74 6c 79 20 64 6f 6e 27 74 20 73 75  licitly don't su
391de 70 70 6f 72 74 20 55 54 46 2d 38 20 64 65 6c 69  pport UTF-8 deli
391df 6d 69 74 65 72 73 20 66 6f 72 20 6e 6f 77 2e 20  miters for now. 
391e0 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 63 68 3e  */.      if( ch>
391e1 3d 30 78 38 30 20 29 7b 0a 20 20 20 20 20 20 20  =0x80 ){.       
391e2 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 74 29   sqlite3_free(t)
391e3 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
391e4 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
391e5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 74 2d 3e       }.      t->
391e6 64 65 6c 69 6d 5b 63 68 5d 20 3d 20 31 3b 0a 20  delim[ch] = 1;. 
391e7 20 20 20 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 0a     }.  } else {.
391e8 20 20 20 20 2f 2a 20 4d 61 72 6b 20 6e 6f 6e 2d      /* Mark non-
391e9 61 6c 70 68 61 6e 75 6d 65 72 69 63 20 41 53 43  alphanumeric ASC
391ea 49 49 20 63 68 61 72 61 63 74 65 72 73 20 61 73  II characters as
391eb 20 64 65 6c 69 6d 69 74 65 72 73 20 2a 2f 0a 20   delimiters */. 
391ec 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f     int i;.    fo
391ed 72 28 69 3d 31 3b 20 69 3c 30 78 38 30 3b 20 69  r(i=1; i<0x80; i
391ee 2b 2b 29 7b 0a 20 20 20 20 20 20 74 2d 3e 64 65  ++){.      t->de
391ef 6c 69 6d 5b 69 5d 20 3d 20 21 69 73 61 6c 6e 75  lim[i] = !isalnu
391f0 6d 28 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  m(i);.    }.  }.
391f1 0a 20 20 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20  .  *ppTokenizer 
391f2 3d 20 26 74 2d 3e 62 61 73 65 3b 0a 20 20 72 65  = &t->base;.  re
391f3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
391f4 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79  }../*.** Destroy
391f5 20 61 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a   a tokenizer.*/.
391f6 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c  static int simpl
391f7 65 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33  eDestroy(sqlite3
391f8 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b  _tokenizer *pTok
391f9 65 6e 69 7a 65 72 29 7b 0a 20 20 73 71 6c 69 74  enizer){.  sqlit
391fa 65 33 5f 66 72 65 65 28 70 54 6f 6b 65 6e 69 7a  e3_free(pTokeniz
391fb 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  er);.  return SQ
391fc 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
391fd 2a 20 50 72 65 70 61 72 65 20 74 6f 20 62 65 67  * Prepare to beg
391fe 69 6e 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 20  in tokenizing a 
391ff 70 61 72 74 69 63 75 6c 61 72 20 73 74 72 69 6e  particular strin
39200 67 2e 20 20 54 68 65 20 69 6e 70 75 74 0a 2a 2a  g.  The input.**
39201 20 73 74 72 69 6e 67 20 74 6f 20 62 65 20 74 6f   string to be to
39202 6b 65 6e 69 7a 65 64 20 69 73 20 70 49 6e 70 75  kenized is pInpu
39203 74 5b 30 2e 2e 6e 42 79 74 65 73 2d 31 5d 2e 20  t[0..nBytes-1]. 
39204 20 41 20 63 75 72 73 6f 72 0a 2a 2a 20 75 73 65   A cursor.** use
39205 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 61 6c  d to incremental
39206 6c 79 20 74 6f 6b 65 6e 69 7a 65 20 74 68 69 73  ly tokenize this
39207 20 73 74 72 69 6e 67 20 69 73 20 72 65 74 75 72   string is retur
39208 6e 65 64 20 69 6e 20 0a 2a 2a 20 2a 70 70 43 75  ned in .** *ppCu
39209 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rsor..*/.static 
3920a 69 6e 74 20 73 69 6d 70 6c 65 4f 70 65 6e 28 0a  int simpleOpen(.
3920b 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
3920c 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c  zer *pTokenizer,
3920d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
3920e 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 63  tokenizer */.  c
3920f 6f 6e 73 74 20 63 68 61 72 20 2a 70 49 6e 70 75  onst char *pInpu
39210 74 2c 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20  t, int nBytes,  
39211 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20        /* String 
39212 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20  to be tokenized 
39213 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  */.  sqlite3_tok
39214 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 2a  enizer_cursor **
39215 70 70 43 75 72 73 6f 72 20 20 20 20 2f 2a 20 4f  ppCursor    /* O
39216 55 54 3a 20 54 6f 6b 65 6e 69 7a 61 74 69 6f 6e  UT: Tokenization
39217 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20   cursor */.){.  
39218 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72  simple_tokenizer
39219 5f 63 75 72 73 6f 72 20 2a 63 3b 0a 0a 20 20 63  _cursor *c;..  c
3921a 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e   = (simple_token
3921b 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 20 73  izer_cursor *) s
3921c 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
3921d 7a 65 6f 66 28 2a 63 29 29 3b 0a 20 20 69 66 28  zeof(*c));.  if(
3921e 20 63 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72   c==NULL ) retur
3921f 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
39220 0a 20 20 63 2d 3e 70 49 6e 70 75 74 20 3d 20 70  .  c->pInput = p
39221 49 6e 70 75 74 3b 0a 20 20 69 66 28 20 70 49 6e  Input;.  if( pIn
39222 70 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 63 2d  put==0 ){.    c-
39223 3e 6e 42 79 74 65 73 20 3d 20 30 3b 0a 20 20 7d  >nBytes = 0;.  }
39224 65 6c 73 65 20 69 66 28 20 6e 42 79 74 65 73 3c  else if( nBytes<
39225 30 20 29 7b 0a 20 20 20 20 63 2d 3e 6e 42 79 74  0 ){.    c->nByt
39226 65 73 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e  es = (int)strlen
39227 28 70 49 6e 70 75 74 29 3b 0a 20 20 7d 65 6c 73  (pInput);.  }els
39228 65 7b 0a 20 20 20 20 63 2d 3e 6e 42 79 74 65 73  e{.    c->nBytes
39229 20 3d 20 6e 42 79 74 65 73 3b 0a 20 20 7d 0a 20   = nBytes;.  }. 
3922a 20 63 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b   c->iOffset = 0;
3922b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3922c 20 2f 2a 20 73 74 61 72 74 20 74 6f 6b 65 6e 69   /* start tokeni
3922d 7a 69 6e 67 20 61 74 20 74 68 65 20 62 65 67 69  zing at the begi
3922e 6e 6e 69 6e 67 20 2a 2f 0a 20 20 63 2d 3e 69 54  nning */.  c->iT
3922f 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 63 2d 3e 70  oken = 0;.  c->p
39230 54 6f 6b 65 6e 20 3d 20 4e 55 4c 4c 3b 20 20 20  Token = NULL;   
39231 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e              /* n
39232 6f 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65  o space allocate
39233 64 2c 20 79 65 74 2e 20 2a 2f 0a 20 20 63 2d 3e  d, yet. */.  c->
39234 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 20  nTokenAllocated 
39235 3d 20 30 3b 0a 0a 20 20 2a 70 70 43 75 72 73 6f  = 0;..  *ppCurso
39236 72 20 3d 20 26 63 2d 3e 62 61 73 65 3b 0a 20 20  r = &c->base;.  
39237 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
39238 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
39239 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20   a tokenization 
3923a 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73 6c  cursor previousl
3923b 79 20 6f 70 65 6e 65 64 20 62 79 20 61 20 63 61  y opened by a ca
3923c 6c 6c 20 74 6f 0a 2a 2a 20 73 69 6d 70 6c 65 4f  ll to.** simpleO
3923d 70 65 6e 28 29 20 61 62 6f 76 65 2e 0a 2a 2f 0a  pen() above..*/.
3923e 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c  static int simpl
3923f 65 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 74  eClose(sqlite3_t
39240 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
39241 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 73 69 6d  *pCursor){.  sim
39242 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  ple_tokenizer_cu
39243 72 73 6f 72 20 2a 63 20 3d 20 28 73 69 6d 70 6c  rsor *c = (simpl
39244 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  e_tokenizer_curs
39245 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20  or *) pCursor;. 
39246 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63 2d   sqlite3_free(c-
39247 3e 70 54 6f 6b 65 6e 29 3b 0a 20 20 73 71 6c 69  >pToken);.  sqli
39248 74 65 33 5f 66 72 65 65 28 63 29 3b 0a 20 20 72  te3_free(c);.  r
39249 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
3924a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63  .}../*.** Extrac
3924b 74 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e  t the next token
3924c 20 66 72 6f 6d 20 61 20 74 6f 6b 65 6e 69 7a 61   from a tokeniza
3924d 74 69 6f 6e 20 63 75 72 73 6f 72 2e 20 20 54 68  tion cursor.  Th
3924e 65 20 63 75 72 73 6f 72 20 6d 75 73 74 0a 2a 2a  e cursor must.**
3924f 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65   have been opene
39250 64 20 62 79 20 61 20 70 72 69 6f 72 20 63 61 6c  d by a prior cal
39251 6c 20 74 6f 20 73 69 6d 70 6c 65 4f 70 65 6e 28  l to simpleOpen(
39252 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
39253 20 73 69 6d 70 6c 65 4e 65 78 74 28 0a 20 20 73   simpleNext(.  s
39254 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
39255 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
39256 2c 20 20 2f 2a 20 43 75 72 73 6f 72 20 72 65 74  ,  /* Cursor ret
39257 75 72 6e 65 64 20 62 79 20 73 69 6d 70 6c 65 4f  urned by simpleO
39258 70 65 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  pen */.  const c
39259 68 61 72 20 2a 2a 70 70 54 6f 6b 65 6e 2c 20 20  har **ppToken,  
3925a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3925b 4f 55 54 3a 20 2a 70 70 54 6f 6b 65 6e 20 69 73  OUT: *ppToken is
3925c 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78 74 20   the token text 
3925d 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79 74 65  */.  int *pnByte
3925e 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
3925f 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
39260 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
39261 20 69 6e 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69   in token */.  i
39262 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65  nt *piStartOffse
39263 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
39264 20 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 72 74     /* OUT: Start
39265 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74 6f  ing offset of to
39266 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69  ken */.  int *pi
39267 45 6e 64 4f 66 66 73 65 74 2c 20 20 20 20 20 20  EndOffset,      
39268 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39269 4f 55 54 3a 20 45 6e 64 69 6e 67 20 6f 66 66 73  OUT: Ending offs
3926a 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20  et of token */. 
3926b 20 69 6e 74 20 2a 70 69 50 6f 73 69 74 69 6f 6e   int *piPosition
3926c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3926d 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 73       /* OUT: Pos
3926e 69 74 69 6f 6e 20 69 6e 74 65 67 65 72 20 6f 66  ition integer of
3926f 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 73   token */.){.  s
39270 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f  imple_tokenizer_
39271 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 73 69 6d  cursor *c = (sim
39272 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  ple_tokenizer_cu
39273 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b  rsor *) pCursor;
39274 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  .  simple_tokeni
39275 7a 65 72 20 2a 74 20 3d 20 28 73 69 6d 70 6c 65  zer *t = (simple
39276 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 20 70 43  _tokenizer *) pC
39277 75 72 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65  ursor->pTokenize
39278 72 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  r;.  unsigned ch
39279 61 72 20 2a 70 20 3d 20 28 75 6e 73 69 67 6e 65  ar *p = (unsigne
3927a 64 20 63 68 61 72 20 2a 29 63 2d 3e 70 49 6e 70  d char *)c->pInp
3927b 75 74 3b 0a 0a 20 20 77 68 69 6c 65 28 20 63 2d  ut;..  while( c-
3927c 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 42 79 74  >iOffset<c->nByt
3927d 65 73 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 53  es ){.    int iS
3927e 74 61 72 74 4f 66 66 73 65 74 3b 0a 0a 20 20 20  tartOffset;..   
3927f 20 2f 2a 20 53 63 61 6e 20 70 61 73 74 20 64 65   /* Scan past de
39280 6c 69 6d 69 74 65 72 20 63 68 61 72 61 63 74 65  limiter characte
39281 72 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28  rs */.    while(
39282 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e   c->iOffset<c->n
39283 42 79 74 65 73 20 26 26 20 73 69 6d 70 6c 65 44  Bytes && simpleD
39284 65 6c 69 6d 28 74 2c 20 70 5b 63 2d 3e 69 4f 66  elim(t, p[c->iOf
39285 66 73 65 74 5d 29 20 29 7b 0a 20 20 20 20 20 20  fset]) ){.      
39286 63 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a 20 20  c->iOffset++;.  
39287 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 75 6e    }..    /* Coun
39288 74 20 6e 6f 6e 2d 64 65 6c 69 6d 69 74 65 72 20  t non-delimiter 
39289 63 68 61 72 61 63 74 65 72 73 2e 20 2a 2f 0a 20  characters. */. 
3928a 20 20 20 69 53 74 61 72 74 4f 66 66 73 65 74 20     iStartOffset 
3928b 3d 20 63 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20  = c->iOffset;.  
3928c 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66    while( c->iOff
3928d 73 65 74 3c 63 2d 3e 6e 42 79 74 65 73 20 26 26  set<c->nBytes &&
3928e 20 21 73 69 6d 70 6c 65 44 65 6c 69 6d 28 74 2c   !simpleDelim(t,
3928f 20 70 5b 63 2d 3e 69 4f 66 66 73 65 74 5d 29 20   p[c->iOffset]) 
39290 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69 4f 66 66  ){.      c->iOff
39291 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20  set++;.    }..  
39292 20 20 69 66 28 20 63 2d 3e 69 4f 66 66 73 65 74    if( c->iOffset
39293 3e 69 53 74 61 72 74 4f 66 66 73 65 74 20 29 7b  >iStartOffset ){
39294 0a 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6e 20  .      int i, n 
39295 3d 20 63 2d 3e 69 4f 66 66 73 65 74 2d 69 53 74  = c->iOffset-iSt
39296 61 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 20  artOffset;.     
39297 20 69 66 28 20 6e 3e 63 2d 3e 6e 54 6f 6b 65 6e   if( n>c->nToken
39298 41 6c 6c 6f 63 61 74 65 64 20 29 7b 0a 20 20 20  Allocated ){.   
39299 20 20 20 20 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c       c->nTokenAl
3929a 6c 6f 63 61 74 65 64 20 3d 20 6e 2b 32 30 3b 0a  located = n+20;.
3929b 20 20 20 20 20 20 20 20 63 2d 3e 70 54 6f 6b 65          c->pToke
3929c 6e 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  n = sqlite3_real
3929d 6c 6f 63 28 63 2d 3e 70 54 6f 6b 65 6e 2c 20 63  loc(c->pToken, c
3929e 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65  ->nTokenAllocate
3929f 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  d);.        if( 
392a0 63 2d 3e 70 54 6f 6b 65 6e 3d 3d 4e 55 4c 4c 20  c->pToken==NULL 
392a1 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
392a2 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20  NOMEM;.      }. 
392a3 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
392a4 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  n; i++){.       
392a5 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
392a6 54 68 69 73 20 6e 65 65 64 73 20 65 78 70 61 6e  This needs expan
392a7 73 69 6f 6e 20 74 6f 20 68 61 6e 64 6c 65 20 55  sion to handle U
392a8 54 46 2d 38 0a 20 20 20 20 20 20 20 20 2a 2a 20  TF-8.        ** 
392a9 63 61 73 65 2d 69 6e 73 65 6e 73 69 74 69 76 69  case-insensitivi
392aa 74 79 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ty..        */. 
392ab 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20         unsigned 
392ac 63 68 61 72 20 63 68 20 3d 20 70 5b 69 53 74 61  char ch = p[iSta
392ad 72 74 4f 66 66 73 65 74 2b 69 5d 3b 0a 20 20 20  rtOffset+i];.   
392ae 20 20 20 20 20 63 2d 3e 70 54 6f 6b 65 6e 5b 69       c->pToken[i
392af 5d 20 3d 20 63 68 3c 30 78 38 30 20 3f 20 74 6f  ] = ch<0x80 ? to
392b0 6c 6f 77 65 72 28 63 68 29 20 3a 20 63 68 3b 0a  lower(ch) : ch;.
392b1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70        }.      *p
392b2 70 54 6f 6b 65 6e 20 3d 20 63 2d 3e 70 54 6f 6b  pToken = c->pTok
392b3 65 6e 3b 0a 20 20 20 20 20 20 2a 70 6e 42 79 74  en;.      *pnByt
392b4 65 73 20 3d 20 6e 3b 0a 20 20 20 20 20 20 2a 70  es = n;.      *p
392b5 69 53 74 61 72 74 4f 66 66 73 65 74 20 3d 20 69  iStartOffset = i
392b6 53 74 61 72 74 4f 66 66 73 65 74 3b 0a 20 20 20  StartOffset;.   
392b7 20 20 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 20     *piEndOffset 
392b8 3d 20 63 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20  = c->iOffset;.  
392b9 20 20 20 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20      *piPosition 
392ba 3d 20 63 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 0a  = c->iToken++;..
392bb 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
392bc 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20  ITE_OK;.    }.  
392bd 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
392be 45 5f 44 4f 4e 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  E_DONE;.}../*.**
392bf 20 54 68 65 20 73 65 74 20 6f 66 20 72 6f 75 74   The set of rout
392c0 69 6e 65 73 20 74 68 61 74 20 69 6d 70 6c 65 6d  ines that implem
392c1 65 6e 74 20 74 68 65 20 73 69 6d 70 6c 65 20 74  ent the simple t
392c2 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74  okenizer.*/.stat
392c3 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
392c4 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
392c5 65 20 73 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65  e simpleTokenize
392c6 72 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c  rModule = {.  0,
392c7 0a 20 20 73 69 6d 70 6c 65 43 72 65 61 74 65 2c  .  simpleCreate,
392c8 0a 20 20 73 69 6d 70 6c 65 44 65 73 74 72 6f 79  .  simpleDestroy
392c9 2c 0a 20 20 73 69 6d 70 6c 65 4f 70 65 6e 2c 0a  ,.  simpleOpen,.
392ca 20 20 73 69 6d 70 6c 65 43 6c 6f 73 65 2c 0a 20    simpleClose,. 
392cb 20 73 69 6d 70 6c 65 4e 65 78 74 2c 0a 7d 3b 0a   simpleNext,.};.
392cc 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
392cd 61 20 6e 65 77 20 73 69 6d 70 6c 65 20 74 6f 6b  a new simple tok
392ce 65 6e 69 7a 65 72 2e 20 20 52 65 74 75 72 6e 20  enizer.  Return 
392cf 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
392d0 20 6e 65 77 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65   new.** tokenize
392d1 72 20 69 6e 20 2a 70 70 4d 6f 64 75 6c 65 0a 2a  r in *ppModule.*
392d2 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
392d3 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
392d4 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72  3SimpleTokenizer
392d5 4d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65  Module(.  sqlite
392d6 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
392d7 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75  le const**ppModu
392d8 6c 65 0a 29 7b 0a 20 20 2a 70 70 4d 6f 64 75 6c  le.){.  *ppModul
392d9 65 20 3d 20 26 73 69 6d 70 6c 65 54 6f 6b 65 6e  e = &simpleToken
392da 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a 7d 0a 0a 23  izerModule;.}..#
392db 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
392dc 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
392dd 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
392de 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f  _ENABLE_FTS3) */
392df 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
392e0 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 74 6f  * End of fts3_to
392e1 6b 65 6e 69 7a 65 72 31 2e 63 20 2a 2a 2a 2a 2a  kenizer1.c *****
392e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
392e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
392e4 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
392e5 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 74 72  * Begin file rtr
392e6 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ee.c ***********
392e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
392e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
392e9 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
392ea 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
392eb 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
392ec 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
392ed 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
392ee 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
392ef 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
392f0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
392f1 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
392f2 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
392f3 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
392f4 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
392f5 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
392f6 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
392f7 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
392f8 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
392f9 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
392fa 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
392fb 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
392fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
392fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
392fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
392ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39300 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
39301 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
39302 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e  ode for implemen
39303 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 72  tations of the r
39304 2d 74 72 65 65 20 61 6e 64 20 72 2a 2d 74 72 65  -tree and r*-tre
39305 65 0a 2a 2a 20 61 6c 67 6f 72 69 74 68 6d 73 20  e.** algorithms 
39306 70 61 63 6b 61 67 65 64 20 61 73 20 61 6e 20 53  packaged as an S
39307 51 4c 69 74 65 20 76 69 72 74 75 61 6c 20 74 61  QLite virtual ta
39308 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a  ble module..**.*
39309 2a 20 24 49 64 3a 20 72 74 72 65 65 2e 63 2c 76  * $Id: rtree.c,v
3930a 20 31 2e 31 34 20 32 30 30 39 2f 30 38 2f 30 36   1.14 2009/08/06
3930b 20 31 38 3a 33 36 3a 34 37 20 64 61 6e 69 65 6c   18:36:47 daniel
3930c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
3930d 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
3930e 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66  ITE_CORE) || def
3930f 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
39310 4c 45 5f 52 54 52 45 45 29 0a 0a 2f 2a 0a 2a 2a  LE_RTREE)../*.**
39311 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
39312 69 6e 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74  ins an implement
39313 61 74 69 6f 6e 20 6f 66 20 61 20 63 6f 75 70 6c  ation of a coupl
39314 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20 76  e of different v
39315 61 72 69 61 6e 74 73 0a 2a 2a 20 6f 66 20 74 68  ariants.** of th
39316 65 20 72 2d 74 72 65 65 20 61 6c 67 6f 72 69 74  e r-tree algorit
39317 68 6d 2e 20 53 65 65 20 74 68 65 20 52 45 41 44  hm. See the READ
39318 4d 45 20 66 69 6c 65 20 66 6f 72 20 66 75 72 74  ME file for furt
39319 68 65 72 20 64 65 74 61 69 6c 73 2e 20 54 68 65  her details. The
3931a 20 0a 2a 2a 20 73 61 6d 65 20 64 61 74 61 2d 73   .** same data-s
3931b 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64  tructure is used
3931c 20 66 6f 72 20 61 6c 6c 2c 20 62 75 74 20 74 68   for all, but th
3931d 65 20 61 6c 67 6f 72 69 74 68 6d 73 20 66 6f 72  e algorithms for
3931e 20 69 6e 73 65 72 74 20 61 6e 64 0a 2a 2a 20 64   insert and.** d
3931f 65 6c 65 74 65 20 6f 70 65 72 61 74 69 6f 6e 73  elete operations
39320 20 76 61 72 79 2e 20 54 68 65 20 76 61 72 69 61   vary. The varia
39321 6e 74 73 20 75 73 65 64 20 61 72 65 20 73 65 6c  nts used are sel
39322 65 63 74 65 64 20 61 74 20 63 6f 6d 70 69 6c 65  ected at compile
39323 20 74 69 6d 65 20 0a 2a 2a 20 62 79 20 64 65 66   time .** by def
39324 69 6e 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77  ining the follow
39325 69 6e 67 20 73 79 6d 62 6f 6c 73 3a 0a 2a 2f 0a  ing symbols:.*/.
39326 0a 2f 2a 20 45 69 74 68 65 72 2c 20 62 6f 74 68  ./* Either, both
39327 20 6f 72 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20   or none of the 
39328 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 79 20 62 65  following may be
39329 20 73 65 74 20 74 6f 20 61 63 74 69 76 61 74 65   set to activate
3932a 20 0a 2a 2a 20 72 2a 74 72 65 65 20 76 61 72 69   .** r*tree vari
3932b 61 6e 74 20 61 6c 67 6f 72 69 74 68 6d 73 2e 0a  ant algorithms..
3932c 2a 2f 0a 23 64 65 66 69 6e 65 20 56 41 52 49 41  */.#define VARIA
3932d 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 43 48 4f  NT_RSTARTREE_CHO
3932e 4f 53 45 53 55 42 54 52 45 45 20 30 0a 23 64 65  OSESUBTREE 0.#de
3932f 66 69 6e 65 20 56 41 52 49 41 4e 54 5f 52 53 54  fine VARIANT_RST
39330 41 52 54 52 45 45 5f 52 45 49 4e 53 45 52 54 20  ARTREE_REINSERT 
39331 20 20 20 20 20 31 0a 0a 2f 2a 20 0a 2a 2a 20 45       1../* .** E
39332 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68  xactly one of th
39333 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 75 73 74  e following must
39334 20 62 65 20 73 65 74 20 74 6f 20 31 2e 0a 2a 2f   be set to 1..*/
39335 0a 23 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54  .#define VARIANT
39336 5f 47 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54  _GUTTMAN_QUADRAT
39337 49 43 5f 53 50 4c 49 54 20 30 0a 23 64 65 66 69  IC_SPLIT 0.#defi
39338 6e 65 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d  ne VARIANT_GUTTM
39339 41 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 20  AN_LINEAR_SPLIT 
3933a 20 20 20 30 0a 23 64 65 66 69 6e 65 20 56 41 52     0.#define VAR
3933b 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 53  IANT_RSTARTREE_S
3933c 50 4c 49 54 20 20 20 20 20 20 20 20 20 31 0a 0a  PLIT         1..
3933d 23 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54 5f  #define VARIANT_
3933e 47 55 54 54 4d 41 4e 5f 53 50 4c 49 54 20 5c 0a  GUTTMAN_SPLIT \.
3933f 20 20 20 20 20 20 20 20 28 56 41 52 49 41 4e 54          (VARIANT
39340 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f  _GUTTMAN_LINEAR_
39341 53 50 4c 49 54 7c 7c 56 41 52 49 41 4e 54 5f 47  SPLIT||VARIANT_G
39342 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43  UTTMAN_QUADRATIC
39343 5f 53 50 4c 49 54 29 0a 0a 23 69 66 20 56 41 52  _SPLIT)..#if VAR
39344 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55 41  IANT_GUTTMAN_QUA
39345 44 52 41 54 49 43 5f 53 50 4c 49 54 0a 20 20 23  DRATIC_SPLIT.  #
39346 64 65 66 69 6e 65 20 50 69 63 6b 4e 65 78 74 20  define PickNext 
39347 51 75 61 64 72 61 74 69 63 50 69 63 6b 4e 65 78  QuadraticPickNex
39348 74 0a 20 20 23 64 65 66 69 6e 65 20 50 69 63 6b  t.  #define Pick
39349 53 65 65 64 73 20 51 75 61 64 72 61 74 69 63 50  Seeds QuadraticP
3934a 69 63 6b 53 65 65 64 73 0a 20 20 23 64 65 66 69  ickSeeds.  #defi
3934b 6e 65 20 41 73 73 69 67 6e 43 65 6c 6c 73 20 73  ne AssignCells s
3934c 70 6c 69 74 4e 6f 64 65 47 75 74 74 6d 61 6e 0a  plitNodeGuttman.
3934d 23 65 6e 64 69 66 0a 23 69 66 20 56 41 52 49 41  #endif.#if VARIA
3934e 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41  NT_GUTTMAN_LINEA
3934f 52 5f 53 50 4c 49 54 0a 20 20 23 64 65 66 69 6e  R_SPLIT.  #defin
39350 65 20 50 69 63 6b 4e 65 78 74 20 4c 69 6e 65 61  e PickNext Linea
39351 72 50 69 63 6b 4e 65 78 74 0a 20 20 23 64 65 66  rPickNext.  #def
39352 69 6e 65 20 50 69 63 6b 53 65 65 64 73 20 4c 69  ine PickSeeds Li
39353 6e 65 61 72 50 69 63 6b 53 65 65 64 73 0a 20 20  nearPickSeeds.  
39354 23 64 65 66 69 6e 65 20 41 73 73 69 67 6e 43 65  #define AssignCe
39355 6c 6c 73 20 73 70 6c 69 74 4e 6f 64 65 47 75 74  lls splitNodeGut
39356 74 6d 61 6e 0a 23 65 6e 64 69 66 0a 23 69 66 20  tman.#endif.#if 
39357 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45  VARIANT_RSTARTRE
39358 45 5f 53 50 4c 49 54 0a 20 20 23 64 65 66 69 6e  E_SPLIT.  #defin
39359 65 20 41 73 73 69 67 6e 43 65 6c 6c 73 20 73 70  e AssignCells sp
3935a 6c 69 74 4e 6f 64 65 53 74 61 72 74 72 65 65 0a  litNodeStartree.
3935b 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66  #endif...#ifndef
3935c 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a 20 20 53   SQLITE_CORE.  S
3935d 51 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f  QLITE_EXTENSION_
3935e 49 4e 49 54 31 0a 23 65 6c 73 65 0a 23 65 6e 64  INIT1.#else.#end
3935f 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  if...#ifndef SQL
39360 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e  ITE_AMALGAMATION
39361 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 33  .typedef sqlite3
39362 5f 69 6e 74 36 34 20 69 36 34 3b 0a 74 79 70 65  _int64 i64;.type
39363 64 65 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61  def unsigned cha
39364 72 20 75 38 3b 0a 74 79 70 65 64 65 66 20 75 6e  r u8;.typedef un
39365 73 69 67 6e 65 64 20 69 6e 74 20 75 33 32 3b 0a  signed int u32;.
39366 23 65 6e 64 69 66 0a 0a 74 79 70 65 64 65 66 20  #endif..typedef 
39367 73 74 72 75 63 74 20 52 74 72 65 65 20 52 74 72  struct Rtree Rtr
39368 65 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ee;.typedef stru
39369 63 74 20 52 74 72 65 65 43 75 72 73 6f 72 20 52  ct RtreeCursor R
3936a 74 72 65 65 43 75 72 73 6f 72 3b 0a 74 79 70 65  treeCursor;.type
3936b 64 65 66 20 73 74 72 75 63 74 20 52 74 72 65 65  def struct Rtree
3936c 4e 6f 64 65 20 52 74 72 65 65 4e 6f 64 65 3b 0a  Node RtreeNode;.
3936d 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 52  typedef struct R
3936e 74 72 65 65 43 65 6c 6c 20 52 74 72 65 65 43 65  treeCell RtreeCe
3936f 6c 6c 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ll;.typedef stru
39370 63 74 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69  ct RtreeConstrai
39371 6e 74 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69  nt RtreeConstrai
39372 6e 74 3b 0a 74 79 70 65 64 65 66 20 75 6e 69 6f  nt;.typedef unio
39373 6e 20 52 74 72 65 65 43 6f 6f 72 64 20 52 74 72  n RtreeCoord Rtr
39374 65 65 43 6f 6f 72 64 3b 0a 0a 2f 2a 20 54 68 65  eeCoord;../* The
39375 20 72 74 72 65 65 20 6d 61 79 20 68 61 76 65 20   rtree may have 
39376 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 52 54  between 1 and RT
39377 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f  REE_MAX_DIMENSIO
39378 4e 53 20 64 69 6d 65 6e 73 69 6f 6e 73 2e 20 2a  NS dimensions. *
39379 2f 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f  /.#define RTREE_
3937a 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 20 35  MAX_DIMENSIONS 5
3937b 0a 0a 2f 2a 20 53 69 7a 65 20 6f 66 20 68 61 73  ../* Size of has
3937c 68 20 74 61 62 6c 65 20 52 74 72 65 65 2e 61 48  h table Rtree.aH
3937d 61 73 68 2e 20 54 68 69 73 20 68 61 73 68 20 74  ash. This hash t
3937e 61 62 6c 65 20 69 73 20 6e 6f 74 20 65 78 70 65  able is not expe
3937f 63 74 65 64 20 74 6f 0a 2a 2a 20 65 76 65 72 20  cted to.** ever 
39380 63 6f 6e 74 61 69 6e 20 76 65 72 79 20 6d 61 6e  contain very man
39381 79 20 65 6e 74 72 69 65 73 2c 20 73 6f 20 61 20  y entries, so a 
39382 66 69 78 65 64 20 6e 75 6d 62 65 72 20 6f 66 20  fixed number of 
39383 62 75 63 6b 65 74 73 20 69 73 20 0a 2a 2a 20 75  buckets is .** u
39384 73 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  sed..*/.#define 
39385 48 41 53 48 53 49 5a 45 20 31 32 38 0a 0a 2f 2a  HASHSIZE 128../*
39386 20 0a 2a 2a 20 41 6e 20 72 74 72 65 65 20 76 69   .** An rtree vi
39387 72 74 75 61 6c 2d 74 61 62 6c 65 20 6f 62 6a 65  rtual-table obje
39388 63 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74  ct..*/.struct Rt
39389 72 65 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  ree {.  sqlite3_
3938a 76 74 61 62 20 62 61 73 65 3b 0a 20 20 73 71 6c  vtab base;.  sql
3938b 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
3938c 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 73 74           /* Host
3938d 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
3938e 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 4e  tion */.  int iN
3938f 6f 64 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20  odeSize;        
39390 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 69 6e        /* Size in
39391 20 62 79 74 65 73 20 6f 66 20 65 61 63 68 20 6e   bytes of each n
39392 6f 64 65 20 69 6e 20 74 68 65 20 6e 6f 64 65 20  ode in the node 
39393 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  table */.  int n
39394 44 69 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Dim;            
39395 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
39396 20 6f 66 20 64 69 6d 65 6e 73 69 6f 6e 73 20 2a   of dimensions *
39397 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 50 65  /.  int nBytesPe
39398 72 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  rCell;          
39399 2f 2a 20 42 79 74 65 73 20 63 6f 6e 73 75 6d 65  /* Bytes consume
3939a 64 20 70 65 72 20 63 65 6c 6c 20 2a 2f 0a 20 20  d per cell */.  
3939b 69 6e 74 20 69 44 65 70 74 68 3b 20 20 20 20 20  int iDepth;     
3939c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
3939d 75 72 72 65 6e 74 20 64 65 70 74 68 20 6f 66 20  urrent depth of 
3939e 74 68 65 20 72 2d 74 72 65 65 20 73 74 72 75 63  the r-tree struc
3939f 74 75 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ture */.  char *
393a0 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  zDb;            
393a1 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
393a2 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
393a3 6e 69 6e 67 20 72 2d 74 72 65 65 20 74 61 62 6c  ning r-tree tabl
393a4 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61  e */.  char *zNa
393a5 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  me;             
393a6 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 72 2d     /* Name of r-
393a7 74 72 65 65 20 74 61 62 6c 65 20 2a 2f 20 0a 20  tree table */ . 
393a8 20 52 74 72 65 65 4e 6f 64 65 20 2a 61 48 61 73   RtreeNode *aHas
393a9 68 5b 48 41 53 48 53 49 5a 45 5d 3b 20 2f 2a 20  h[HASHSIZE]; /* 
393aa 48 61 73 68 20 74 61 62 6c 65 20 6f 66 20 69 6e  Hash table of in
393ab 2d 6d 65 6d 6f 72 79 20 6e 6f 64 65 73 2e 20 2a  -memory nodes. *
393ac 2f 20 0a 20 20 69 6e 74 20 6e 42 75 73 79 3b 20  / .  int nBusy; 
393ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
393ae 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d 62   /* Current numb
393af 65 72 20 6f 66 20 75 73 65 72 73 20 6f 66 20 74  er of users of t
393b0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f  his structure */
393b1 0a 0a 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 6e  ..  /* List of n
393b2 6f 64 65 73 20 72 65 6d 6f 76 65 64 20 64 75 72  odes removed dur
393b3 69 6e 67 20 61 20 43 6f 6e 64 65 6e 73 65 54 72  ing a CondenseTr
393b4 65 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 4c 69  ee operation. Li
393b5 73 74 20 69 73 0a 20 20 2a 2a 20 6c 69 6e 6b 65  st is.  ** linke
393b6 64 20 74 6f 67 65 74 68 65 72 20 76 69 61 20 74  d together via t
393b7 68 65 20 70 6f 69 6e 74 65 72 20 6e 6f 72 6d 61  he pointer norma
393b8 6c 6c 79 20 75 73 65 64 20 66 6f 72 20 68 61 73  lly used for has
393b9 68 20 63 68 61 69 6e 73 20 2d 0a 20 20 2a 2a 20  h chains -.  ** 
393ba 52 74 72 65 65 4e 6f 64 65 2e 70 4e 65 78 74 2e  RtreeNode.pNext.
393bb 20 52 74 72 65 65 4e 6f 64 65 2e 69 4e 6f 64 65   RtreeNode.iNode
393bc 20 73 74 6f 72 65 73 20 74 68 65 20 64 65 70 74   stores the dept
393bd 68 20 6f 66 20 74 68 65 20 73 75 62 2d 74 72 65  h of the sub-tre
393be 65 20 0a 20 20 2a 2a 20 68 65 61 64 65 64 20 62  e .  ** headed b
393bf 79 20 74 68 65 20 6e 6f 64 65 20 28 6c 65 61 66  y the node (leaf
393c0 20 6e 6f 64 65 73 20 68 61 76 65 20 52 74 72 65   nodes have Rtre
393c1 65 4e 6f 64 65 2e 69 4e 6f 64 65 3d 3d 30 29 2e  eNode.iNode==0).
393c2 0a 20 20 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64  .  */.  RtreeNod
393c3 65 20 2a 70 44 65 6c 65 74 65 64 3b 0a 20 20 69  e *pDeleted;.  i
393c4 6e 74 20 69 52 65 69 6e 73 65 72 74 48 65 69 67  nt iReinsertHeig
393c5 68 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 48 65  ht;        /* He
393c6 69 67 68 74 20 6f 66 20 73 75 62 2d 74 72 65 65  ight of sub-tree
393c7 73 20 52 65 69 6e 73 65 72 74 28 29 20 68 61 73  s Reinsert() has
393c8 20 72 75 6e 20 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a   run on */..  /*
393c9 20 53 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 72   Statements to r
393ca 65 61 64 2f 77 72 69 74 65 2f 64 65 6c 65 74 65  ead/write/delete
393cb 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 78   a record from x
393cc 78 78 5f 6e 6f 64 65 20 2a 2f 0a 20 20 73 71 6c  xx_node */.  sql
393cd 69 74 65 33 5f 73 74 6d 74 20 2a 70 52 65 61 64  ite3_stmt *pRead
393ce 4e 6f 64 65 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Node;.  sqlite3_
393cf 73 74 6d 74 20 2a 70 57 72 69 74 65 4e 6f 64 65  stmt *pWriteNode
393d0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  ;.  sqlite3_stmt
393d1 20 2a 70 44 65 6c 65 74 65 4e 6f 64 65 3b 0a 0a   *pDeleteNode;..
393d2 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 73 20    /* Statements 
393d3 74 6f 20 72 65 61 64 2f 77 72 69 74 65 2f 64 65  to read/write/de
393d4 6c 65 74 65 20 61 20 72 65 63 6f 72 64 20 66 72  lete a record fr
393d5 6f 6d 20 78 78 78 5f 72 6f 77 69 64 20 2a 2f 0a  om xxx_rowid */.
393d6 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
393d7 70 52 65 61 64 52 6f 77 69 64 3b 0a 20 20 73 71  pReadRowid;.  sq
393d8 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 57 72 69  lite3_stmt *pWri
393d9 74 65 52 6f 77 69 64 3b 0a 20 20 73 71 6c 69 74  teRowid;.  sqlit
393da 65 33 5f 73 74 6d 74 20 2a 70 44 65 6c 65 74 65  e3_stmt *pDelete
393db 52 6f 77 69 64 3b 0a 0a 20 20 2f 2a 20 53 74 61  Rowid;..  /* Sta
393dc 74 65 6d 65 6e 74 73 20 74 6f 20 72 65 61 64 2f  tements to read/
393dd 77 72 69 74 65 2f 64 65 6c 65 74 65 20 61 20 72  write/delete a r
393de 65 63 6f 72 64 20 66 72 6f 6d 20 78 78 78 5f 70  ecord from xxx_p
393df 61 72 65 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74  arent */.  sqlit
393e0 65 33 5f 73 74 6d 74 20 2a 70 52 65 61 64 50 61  e3_stmt *pReadPa
393e1 72 65 6e 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f  rent;.  sqlite3_
393e2 73 74 6d 74 20 2a 70 57 72 69 74 65 50 61 72 65  stmt *pWritePare
393e3 6e 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  nt;.  sqlite3_st
393e4 6d 74 20 2a 70 44 65 6c 65 74 65 50 61 72 65 6e  mt *pDeleteParen
393e5 74 3b 0a 0a 20 20 69 6e 74 20 65 43 6f 6f 72 64  t;..  int eCoord
393e6 54 79 70 65 3b 0a 7d 3b 0a 0a 2f 2a 20 50 6f 73  Type;.};../* Pos
393e7 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72  sible values for
393e8 20 65 43 6f 6f 72 64 54 79 70 65 3a 20 2a 2f 0a   eCoordType: */.
393e9 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f 43 4f  #define RTREE_CO
393ea 4f 52 44 5f 52 45 41 4c 33 32 20 30 0a 23 64 65  ORD_REAL32 0.#de
393eb 66 69 6e 65 20 52 54 52 45 45 5f 43 4f 4f 52 44  fine RTREE_COORD
393ec 5f 49 4e 54 33 32 20 20 31 0a 0a 2f 2a 0a 2a 2a  _INT32  1../*.**
393ed 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 6e 75 6d   The minimum num
393ee 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 61 6c 6c  ber of cells all
393ef 6f 77 65 64 20 66 6f 72 20 61 20 6e 6f 64 65 20  owed for a node 
393f0 69 73 20 61 20 74 68 69 72 64 20 6f 66 20 74 68  is a third of th
393f1 65 20 0a 2a 2a 20 6d 61 78 69 6d 75 6d 2e 20 49  e .** maximum. I
393f2 6e 20 47 75 74 6d 61 6e 27 73 20 6e 6f 74 61 74  n Gutman's notat
393f3 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 6d  ion:.**.**     m
393f4 20 3d 20 4d 2f 33 0a 2a 2a 0a 2a 2a 20 49 66 20   = M/3.**.** If 
393f5 61 6e 20 52 2a 2d 74 72 65 65 20 22 52 65 69 6e  an R*-tree "Rein
393f6 73 65 72 74 22 20 6f 70 65 72 61 74 69 6f 6e 20  sert" operation 
393f7 69 73 20 72 65 71 75 69 72 65 64 2c 20 74 68 65  is required, the
393f8 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 0a   same number of.
393f9 2a 2a 20 63 65 6c 6c 73 20 61 72 65 20 72 65 6d  ** cells are rem
393fa 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 6f 76  oved from the ov
393fb 65 72 66 75 6c 6c 20 6e 6f 64 65 20 61 6e 64 20  erfull node and 
393fc 72 65 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  reinserted into 
393fd 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 23 64 65  the tree..*/.#de
393fe 66 69 6e 65 20 52 54 52 45 45 5f 4d 49 4e 43 45  fine RTREE_MINCE
393ff 4c 4c 53 28 70 29 20 28 28 28 28 70 29 2d 3e 69  LLS(p) ((((p)->i
39400 4e 6f 64 65 53 69 7a 65 2d 34 29 2f 28 70 29 2d  NodeSize-4)/(p)-
39401 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 29 2f  >nBytesPerCell)/
39402 33 29 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45  3).#define RTREE
39403 5f 52 45 49 4e 53 45 52 54 28 70 29 20 52 54 52  _REINSERT(p) RTR
39404 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 29 0a 23  EE_MINCELLS(p).#
39405 64 65 66 69 6e 65 20 52 54 52 45 45 5f 4d 41 58  define RTREE_MAX
39406 43 45 4c 4c 53 20 35 31 0a 0a 2f 2a 20 0a 2a 2a  CELLS 51../* .**
39407 20 41 6e 20 72 74 72 65 65 20 63 75 72 73 6f 72   An rtree cursor
39408 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72 75   object..*/.stru
39409 63 74 20 52 74 72 65 65 43 75 72 73 6f 72 20 7b  ct RtreeCursor {
3940a 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  .  sqlite3_vtab_
3940b 63 75 72 73 6f 72 20 62 61 73 65 3b 0a 20 20 52  cursor base;.  R
3940c 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b  treeNode *pNode;
3940d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3940e 20 2f 2a 20 4e 6f 64 65 20 63 75 72 73 6f 72 20   /* Node cursor 
3940f 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  is currently poi
39410 6e 74 69 6e 67 20 61 74 20 2a 2f 0a 20 20 69 6e  nting at */.  in
39411 74 20 69 43 65 6c 6c 3b 20 20 20 20 20 20 20 20  t iCell;        
39412 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39413 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 72  /* Index of curr
39414 65 6e 74 20 63 65 6c 6c 20 69 6e 20 70 4e 6f 64  ent cell in pNod
39415 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 72 61  e */.  int iStra
39416 74 65 67 79 3b 20 20 20 20 20 20 20 20 20 20 20  tegy;           
39417 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79           /* Copy
39418 20 6f 66 20 69 64 78 4e 75 6d 20 73 65 61 72 63   of idxNum searc
39419 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20  h parameter */. 
3941a 20 69 6e 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74   int nConstraint
3941b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3941c 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
3941d 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6e 73  entries in aCons
3941e 74 72 61 69 6e 74 20 2a 2f 0a 20 20 52 74 72 65  traint */.  Rtre
3941f 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a 61 43 6f  eConstraint *aCo
39420 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 2f 2a  nstraint;     /*
39421 20 53 65 61 72 63 68 20 63 6f 6e 73 74 72 61 69   Search constrai
39422 6e 74 73 2e 20 2a 2f 0a 7d 3b 0a 0a 75 6e 69 6f  nts. */.};..unio
39423 6e 20 52 74 72 65 65 43 6f 6f 72 64 20 7b 0a 20  n RtreeCoord {. 
39424 20 66 6c 6f 61 74 20 66 3b 0a 20 20 69 6e 74 20   float f;.  int 
39425 69 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  i;.};../*.** The
39426 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20   argument is an 
39427 52 74 72 65 65 43 6f 6f 72 64 2e 20 52 65 74 75  RtreeCoord. Retu
39428 72 6e 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f  rn the value sto
39429 72 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 52  red within the R
3942a 74 72 65 65 43 6f 6f 72 64 0a 2a 2a 20 66 6f 72  treeCoord.** for
3942b 6d 61 74 74 65 64 20 61 73 20 61 20 64 6f 75 62  matted as a doub
3942c 6c 65 2e 20 54 68 69 73 20 6d 61 63 72 6f 20 61  le. This macro a
3942d 73 73 75 6d 65 73 20 74 68 61 74 20 6c 6f 63 61  ssumes that loca
3942e 6c 20 76 61 72 69 61 62 6c 65 20 70 52 74 72 65  l variable pRtre
3942f 65 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 74  e points.** to t
39430 68 65 20 52 74 72 65 65 20 73 74 72 75 63 74 75  he Rtree structu
39431 72 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  re associated wi
39432 74 68 20 74 68 65 20 52 74 72 65 65 43 6f 6f 72  th the RtreeCoor
39433 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 43  d..*/.#define DC
39434 4f 4f 52 44 28 63 6f 6f 72 64 29 20 28 20 20 20  OORD(coord) (   
39435 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39436 20 20 20 20 20 20 20 20 5c 0a 20 20 28 70 52 74          \.  (pRt
39437 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d  ree->eCoordType=
39438 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41  =RTREE_COORD_REA
39439 4c 33 32 29 20 3f 20 20 20 20 20 20 5c 0a 20 20  L32) ?      \.  
3943a 20 20 28 28 64 6f 75 62 6c 65 29 63 6f 6f 72 64    ((double)coord
3943b 2e 66 29 20 3a 20 20 20 20 20 20 20 20 20 20 20  .f) :           
3943c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3943d 5c 0a 20 20 20 20 28 28 64 6f 75 62 6c 65 29 63  \.    ((double)c
3943e 6f 6f 72 64 2e 69 29 20 20 20 20 20 20 20 20 20  oord.i)         
3943f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39440 20 20 20 20 5c 0a 29 0a 0a 2f 2a 0a 2a 2a 20 41      \.)../*.** A
39441 20 73 65 61 72 63 68 20 63 6f 6e 73 74 72 61 69   search constrai
39442 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74  nt..*/.struct Rt
39443 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 7b 0a  reeConstraint {.
39444 20 20 69 6e 74 20 69 43 6f 6f 72 64 3b 20 20 20    int iCoord;   
39445 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39446 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
39447 63 6f 6e 73 74 72 61 69 6e 65 64 20 63 6f 6f 72  constrained coor
39448 64 69 6e 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20  dinate */.  int 
39449 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
3944a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3944b 20 43 6f 6e 73 74 72 61 69 6e 69 6e 67 20 6f 70   Constraining op
3944c 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 64 6f 75  eration */.  dou
3944d 62 6c 65 20 72 56 61 6c 75 65 3b 20 20 20 20 20  ble rValue;     
3944e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3944f 2a 20 43 6f 6e 73 74 72 61 69 6e 74 20 76 61 6c  * Constraint val
39450 75 65 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 50 6f  ue. */.};../* Po
39451 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f  ssible values fo
39452 72 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e  r RtreeConstrain
39453 74 2e 6f 70 20 2a 2f 0a 23 64 65 66 69 6e 65 20  t.op */.#define 
39454 52 54 52 45 45 5f 45 51 20 30 78 34 31 0a 23 64  RTREE_EQ 0x41.#d
39455 65 66 69 6e 65 20 52 54 52 45 45 5f 4c 45 20 30  efine RTREE_LE 0
39456 78 34 32 0a 23 64 65 66 69 6e 65 20 52 54 52 45  x42.#define RTRE
39457 45 5f 4c 54 20 30 78 34 33 0a 23 64 65 66 69 6e  E_LT 0x43.#defin
39458 65 20 52 54 52 45 45 5f 47 45 20 30 78 34 34 0a  e RTREE_GE 0x44.
39459 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f 47 54  #define RTREE_GT
3945a 20 30 78 34 35 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e   0x45../* .** An
3945b 20 72 74 72 65 65 20 73 74 72 75 63 74 75 72 65   rtree structure
3945c 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 44 61 74   node..**.** Dat
3945d 61 20 66 6f 72 6d 61 74 20 28 52 74 72 65 65 4e  a format (RtreeN
3945e 6f 64 65 2e 7a 44 61 74 61 29 3a 0a 2a 2a 0a 2a  ode.zData):.**.*
3945f 2a 20 20 20 31 2e 20 49 66 20 74 68 65 20 6e 6f  *   1. If the no
39460 64 65 20 69 73 20 74 68 65 20 72 6f 6f 74 20 6e  de is the root n
39461 6f 64 65 20 28 6e 6f 64 65 20 31 29 2c 20 74 68  ode (node 1), th
39462 65 6e 20 74 68 65 20 66 69 72 73 74 20 32 20 62  en the first 2 b
39463 79 74 65 73 0a 2a 2a 20 20 20 20 20 20 6f 66 20  ytes.**      of 
39464 74 68 65 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e  the node contain
39465 20 74 68 65 20 74 72 65 65 20 64 65 70 74 68 20   the tree depth 
39466 61 73 20 61 20 62 69 67 2d 65 6e 64 69 61 6e 20  as a big-endian 
39467 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 20 20 20 20  integer..**     
39468 20 46 6f 72 20 6e 6f 6e 2d 72 6f 6f 74 20 6e 6f   For non-root no
39469 64 65 73 2c 20 74 68 65 20 66 69 72 73 74 20 32  des, the first 2
3946a 20 62 79 74 65 73 20 61 72 65 20 6c 65 66 74 20   bytes are left 
3946b 75 6e 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20  unused..**.**   
3946c 32 2e 20 54 68 65 20 6e 65 78 74 20 32 20 62 79  2. The next 2 by
3946d 74 65 73 20 63 6f 6e 74 61 69 6e 20 74 68 65 20  tes contain the 
3946e 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  number of entrie
3946f 73 20 63 75 72 72 65 6e 74 6c 79 20 0a 2a 2a 20  s currently .** 
39470 20 20 20 20 20 73 74 6f 72 65 64 20 69 6e 20 74       stored in t
39471 68 65 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 20  he node..**.**  
39472 20 33 2e 20 54 68 65 20 72 65 6d 61 69 6e 64 65   3. The remainde
39473 72 20 6f 66 20 74 68 65 20 6e 6f 64 65 20 63 6f  r of the node co
39474 6e 74 61 69 6e 73 20 74 68 65 20 6e 6f 64 65 20  ntains the node 
39475 65 6e 74 72 69 65 73 2e 20 45 61 63 68 20 65 6e  entries. Each en
39476 74 72 79 0a 2a 2a 20 20 20 20 20 20 63 6f 6e 73  try.**      cons
39477 69 73 74 73 20 6f 66 20 61 20 73 69 6e 67 6c 65  ists of a single
39478 20 38 2d 62 79 74 65 20 69 6e 74 65 67 65 72 20   8-byte integer 
39479 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 6e 20 65  followed by an e
3947a 76 65 6e 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20  ven number.**   
3947b 20 20 20 6f 66 20 34 2d 62 79 74 65 20 63 6f 6f     of 4-byte coo
3947c 72 64 69 6e 61 74 65 73 2e 20 46 6f 72 20 6c 65  rdinates. For le
3947d 61 66 20 6e 6f 64 65 73 20 74 68 65 20 69 6e 74  af nodes the int
3947e 65 67 65 72 20 69 73 20 74 68 65 20 72 6f 77 69  eger is the rowi
3947f 64 0a 2a 2a 20 20 20 20 20 20 6f 66 20 61 20 72  d.**      of a r
39480 65 63 6f 72 64 2e 20 46 6f 72 20 69 6e 74 65 72  ecord. For inter
39481 6e 61 6c 20 6e 6f 64 65 73 20 69 74 20 69 73 20  nal nodes it is 
39482 74 68 65 20 6e 6f 64 65 20 6e 75 6d 62 65 72 20  the node number 
39483 6f 66 20 61 0a 2a 2a 20 20 20 20 20 20 63 68 69  of a.**      chi
39484 6c 64 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 72 75  ld page..*/.stru
39485 63 74 20 52 74 72 65 65 4e 6f 64 65 20 7b 0a 20  ct RtreeNode {. 
39486 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72   RtreeNode *pPar
39487 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ent;            
39488 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 6e 6f 64     /* Parent nod
39489 65 20 2a 2f 0a 20 20 69 36 34 20 69 4e 6f 64 65  e */.  i64 iNode
3948a 3b 0a 20 20 69 6e 74 20 6e 52 65 66 3b 0a 20 20  ;.  int nRef;.  
3948b 69 6e 74 20 69 73 44 69 72 74 79 3b 0a 20 20 75  int isDirty;.  u
3948c 38 20 2a 7a 44 61 74 61 3b 0a 20 20 52 74 72 65  8 *zData;.  Rtre
3948d 65 4e 6f 64 65 20 2a 70 4e 65 78 74 3b 20 20 20  eNode *pNext;   
3948e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3948f 20 4e 65 78 74 20 6e 6f 64 65 20 69 6e 20 74 68   Next node in th
39490 69 73 20 68 61 73 68 20 63 68 61 69 6e 20 2a 2f  is hash chain */
39491 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 4e 43 45 4c  .};.#define NCEL
39492 4c 28 70 4e 6f 64 65 29 20 72 65 61 64 49 6e 74  L(pNode) readInt
39493 31 36 28 26 28 70 4e 6f 64 65 29 2d 3e 7a 44 61  16(&(pNode)->zDa
39494 74 61 5b 32 5d 29 0a 0a 2f 2a 20 0a 2a 2a 20 53  ta[2])../* .** S
39495 74 72 75 63 74 75 72 65 20 74 6f 20 73 74 6f 72  tructure to stor
39496 65 20 61 20 64 65 73 65 72 69 61 6c 69 7a 65 64  e a deserialized
39497 20 72 74 72 65 65 20 72 65 63 6f 72 64 2e 0a 2a   rtree record..*
39498 2f 0a 73 74 72 75 63 74 20 52 74 72 65 65 43 65  /.struct RtreeCe
39499 6c 6c 20 7b 0a 20 20 69 36 34 20 69 52 6f 77 69  ll {.  i64 iRowi
3949a 64 3b 0a 20 20 52 74 72 65 65 43 6f 6f 72 64 20  d;.  RtreeCoord 
3949b 61 43 6f 6f 72 64 5b 52 54 52 45 45 5f 4d 41 58  aCoord[RTREE_MAX
3949c 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a 32 5d 3b 0a  _DIMENSIONS*2];.
3949d 7d 3b 0a 0a 23 69 66 6e 64 65 66 20 4d 41 58 0a  };..#ifndef MAX.
3949e 23 20 64 65 66 69 6e 65 20 4d 41 58 28 78 2c 79  # define MAX(x,y
3949f 29 20 28 28 78 29 20 3c 20 28 79 29 20 3f 20 28  ) ((x) < (y) ? (
394a0 79 29 20 3a 20 28 78 29 29 0a 23 65 6e 64 69 66  y) : (x)).#endif
394a1 0a 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23 20 64  .#ifndef MIN.# d
394a2 65 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28  efine MIN(x,y) (
394a3 28 78 29 20 3e 20 28 79 29 20 3f 20 28 79 29 20  (x) > (y) ? (y) 
394a4 3a 20 28 78 29 29 0a 23 65 6e 64 69 66 0a 0a 2f  : (x)).#endif../
394a5 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 74  *.** Functions t
394a6 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 20 61 20  o deserialize a 
394a7 31 36 20 62 69 74 20 69 6e 74 65 67 65 72 2c 20  16 bit integer, 
394a8 33 32 20 62 69 74 20 72 65 61 6c 20 6e 75 6d 62  32 bit real numb
394a9 65 72 20 61 6e 64 0a 2a 2a 20 36 34 20 62 69 74  er and.** 64 bit
394aa 20 69 6e 74 65 67 65 72 2e 20 54 68 65 20 64 65   integer. The de
394ab 73 65 72 69 61 6c 69 7a 65 64 20 76 61 6c 75 65  serialized value
394ac 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
394ad 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64  .static int read
394ae 49 6e 74 31 36 28 75 38 20 2a 70 29 7b 0a 20 20  Int16(u8 *p){.  
394af 72 65 74 75 72 6e 20 28 70 5b 30 5d 3c 3c 38 29  return (p[0]<<8)
394b0 20 2b 20 70 5b 31 5d 3b 0a 7d 0a 73 74 61 74 69   + p[1];.}.stati
394b1 63 20 76 6f 69 64 20 72 65 61 64 43 6f 6f 72 64  c void readCoord
394b2 28 75 38 20 2a 70 2c 20 52 74 72 65 65 43 6f 6f  (u8 *p, RtreeCoo
394b3 72 64 20 2a 70 43 6f 6f 72 64 29 7b 0a 20 20 75  rd *pCoord){.  u
394b4 33 32 20 69 20 3d 20 28 0a 20 20 20 20 28 28 28  32 i = (.    (((
394b5 75 33 32 29 70 5b 30 5d 29 20 3c 3c 20 32 34 29  u32)p[0]) << 24)
394b6 20 2b 20 0a 20 20 20 20 28 28 28 75 33 32 29 70   + .    (((u32)p
394b7 5b 31 5d 29 20 3c 3c 20 31 36 29 20 2b 20 0a 20  [1]) << 16) + . 
394b8 20 20 20 28 28 28 75 33 32 29 70 5b 32 5d 29 20     (((u32)p[2]) 
394b9 3c 3c 20 20 38 29 20 2b 20 0a 20 20 20 20 28 28  <<  8) + .    ((
394ba 28 75 33 32 29 70 5b 33 5d 29 20 3c 3c 20 20 30  (u32)p[3]) <<  0
394bb 29 0a 20 20 29 3b 0a 20 20 2a 28 75 33 32 20 2a  ).  );.  *(u32 *
394bc 29 70 43 6f 6f 72 64 20 3d 20 69 3b 0a 7d 0a 73  )pCoord = i;.}.s
394bd 74 61 74 69 63 20 69 36 34 20 72 65 61 64 49 6e  tatic i64 readIn
394be 74 36 34 28 75 38 20 2a 70 29 7b 0a 20 20 72 65  t64(u8 *p){.  re
394bf 74 75 72 6e 20 28 0a 20 20 20 20 28 28 28 69 36  turn (.    (((i6
394c0 34 29 70 5b 30 5d 29 20 3c 3c 20 35 36 29 20 2b  4)p[0]) << 56) +
394c1 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 31   .    (((i64)p[1
394c2 5d 29 20 3c 3c 20 34 38 29 20 2b 20 0a 20 20 20  ]) << 48) + .   
394c3 20 28 28 28 69 36 34 29 70 5b 32 5d 29 20 3c 3c   (((i64)p[2]) <<
394c4 20 34 30 29 20 2b 20 0a 20 20 20 20 28 28 28 69   40) + .    (((i
394c5 36 34 29 70 5b 33 5d 29 20 3c 3c 20 33 32 29 20  64)p[3]) << 32) 
394c6 2b 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b  + .    (((i64)p[
394c7 34 5d 29 20 3c 3c 20 32 34 29 20 2b 20 0a 20 20  4]) << 24) + .  
394c8 20 20 28 28 28 69 36 34 29 70 5b 35 5d 29 20 3c    (((i64)p[5]) <
394c9 3c 20 31 36 29 20 2b 20 0a 20 20 20 20 28 28 28  < 16) + .    (((
394ca 69 36 34 29 70 5b 36 5d 29 20 3c 3c 20 20 38 29  i64)p[6]) <<  8)
394cb 20 2b 20 0a 20 20 20 20 28 28 28 69 36 34 29 70   + .    (((i64)p
394cc 5b 37 5d 29 20 3c 3c 20 20 30 29 0a 20 20 29 3b  [7]) <<  0).  );
394cd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69  .}../*.** Functi
394ce 6f 6e 73 20 74 6f 20 73 65 72 69 61 6c 69 7a 65  ons to serialize
394cf 20 61 20 31 36 20 62 69 74 20 69 6e 74 65 67 65   a 16 bit intege
394d0 72 2c 20 33 32 20 62 69 74 20 72 65 61 6c 20 6e  r, 32 bit real n
394d1 75 6d 62 65 72 20 61 6e 64 0a 2a 2a 20 36 34 20  umber and.** 64 
394d2 62 69 74 20 69 6e 74 65 67 65 72 2e 20 54 68 65  bit integer. The
394d3 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
394d4 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
394d5 20 62 79 74 65 73 20 77 72 69 74 74 65 6e 0a 2a   bytes written.*
394d6 2a 20 74 6f 20 74 68 65 20 61 72 67 75 6d 65 6e  * to the argumen
394d7 74 20 62 75 66 66 65 72 20 28 61 6c 77 61 79 73  t buffer (always
394d8 20 32 2c 20 34 20 61 6e 64 20 38 20 72 65 73 70   2, 4 and 8 resp
394d9 65 63 74 69 76 65 6c 79 29 2e 0a 2a 2f 0a 73 74  ectively)..*/.st
394da 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 49 6e  atic int writeIn
394db 74 31 36 28 75 38 20 2a 70 2c 20 69 6e 74 20 69  t16(u8 *p, int i
394dc 29 7b 0a 20 20 70 5b 30 5d 20 3d 20 28 69 3e 3e  ){.  p[0] = (i>>
394dd 20 38 29 26 30 78 46 46 3b 0a 20 20 70 5b 31 5d   8)&0xFF;.  p[1]
394de 20 3d 20 28 69 3e 3e 20 30 29 26 30 78 46 46 3b   = (i>> 0)&0xFF;
394df 0a 20 20 72 65 74 75 72 6e 20 32 3b 0a 7d 0a 73  .  return 2;.}.s
394e0 74 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 43  tatic int writeC
394e1 6f 6f 72 64 28 75 38 20 2a 70 2c 20 52 74 72 65  oord(u8 *p, Rtre
394e2 65 43 6f 6f 72 64 20 2a 70 43 6f 6f 72 64 29 7b  eCoord *pCoord){
394e3 0a 20 20 75 33 32 20 69 3b 0a 20 20 61 73 73 65  .  u32 i;.  asse
394e4 72 74 28 20 73 69 7a 65 6f 66 28 52 74 72 65 65  rt( sizeof(Rtree
394e5 43 6f 6f 72 64 29 3d 3d 34 20 29 3b 0a 20 20 61  Coord)==4 );.  a
394e6 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 33  ssert( sizeof(u3
394e7 32 29 3d 3d 34 20 29 3b 0a 20 20 69 20 3d 20 2a  2)==4 );.  i = *
394e8 28 75 33 32 20 2a 29 70 43 6f 6f 72 64 3b 0a 20  (u32 *)pCoord;. 
394e9 20 70 5b 30 5d 20 3d 20 28 69 3e 3e 32 34 29 26   p[0] = (i>>24)&
394ea 30 78 46 46 3b 0a 20 20 70 5b 31 5d 20 3d 20 28  0xFF;.  p[1] = (
394eb 69 3e 3e 31 36 29 26 30 78 46 46 3b 0a 20 20 70  i>>16)&0xFF;.  p
394ec 5b 32 5d 20 3d 20 28 69 3e 3e 20 38 29 26 30 78  [2] = (i>> 8)&0x
394ed 46 46 3b 0a 20 20 70 5b 33 5d 20 3d 20 28 69 3e  FF;.  p[3] = (i>
394ee 3e 20 30 29 26 30 78 46 46 3b 0a 20 20 72 65 74  > 0)&0xFF;.  ret
394ef 75 72 6e 20 34 3b 0a 7d 0a 73 74 61 74 69 63 20  urn 4;.}.static 
394f0 69 6e 74 20 77 72 69 74 65 49 6e 74 36 34 28 75  int writeInt64(u
394f1 38 20 2a 70 2c 20 69 36 34 20 69 29 7b 0a 20 20  8 *p, i64 i){.  
394f2 70 5b 30 5d 20 3d 20 28 69 3e 3e 35 36 29 26 30  p[0] = (i>>56)&0
394f3 78 46 46 3b 0a 20 20 70 5b 31 5d 20 3d 20 28 69  xFF;.  p[1] = (i
394f4 3e 3e 34 38 29 26 30 78 46 46 3b 0a 20 20 70 5b  >>48)&0xFF;.  p[
394f5 32 5d 20 3d 20 28 69 3e 3e 34 30 29 26 30 78 46  2] = (i>>40)&0xF
394f6 46 3b 0a 20 20 70 5b 33 5d 20 3d 20 28 69 3e 3e  F;.  p[3] = (i>>
394f7 33 32 29 26 30 78 46 46 3b 0a 20 20 70 5b 34 5d  32)&0xFF;.  p[4]
394f8 20 3d 20 28 69 3e 3e 32 34 29 26 30 78 46 46 3b   = (i>>24)&0xFF;
394f9 0a 20 20 70 5b 35 5d 20 3d 20 28 69 3e 3e 31 36  .  p[5] = (i>>16
394fa 29 26 30 78 46 46 3b 0a 20 20 70 5b 36 5d 20 3d  )&0xFF;.  p[6] =
394fb 20 28 69 3e 3e 20 38 29 26 30 78 46 46 3b 0a 20   (i>> 8)&0xFF;. 
394fc 20 70 5b 37 5d 20 3d 20 28 69 3e 3e 20 30 29 26   p[7] = (i>> 0)&
394fd 30 78 46 46 3b 0a 20 20 72 65 74 75 72 6e 20 38  0xFF;.  return 8
394fe 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65  ;.}../*.** Incre
394ff 6d 65 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e  ment the referen
39500 63 65 20 63 6f 75 6e 74 20 6f 66 20 6e 6f 64 65  ce count of node
39501 20 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f   p..*/.static vo
39502 69 64 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65  id nodeReference
39503 28 52 74 72 65 65 4e 6f 64 65 20 2a 70 29 7b 0a  (RtreeNode *p){.
39504 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70    if( p ){.    p
39505 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 7d 0a 7d 0a  ->nRef++;.  }.}.
39506 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65  ./*.** Clear the
39507 20 63 6f 6e 74 65 6e 74 20 6f 66 20 6e 6f 64 65   content of node
39508 20 70 20 28 73 65 74 20 61 6c 6c 20 62 79 74 65   p (set all byte
39509 73 20 74 6f 20 30 78 30 30 29 2e 0a 2a 2f 0a 73  s to 0x00)..*/.s
3950a 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 5a  tatic void nodeZ
3950b 65 72 6f 28 52 74 72 65 65 20 2a 70 52 74 72 65  ero(Rtree *pRtre
3950c 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 29  e, RtreeNode *p)
3950d 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  {.  if( p ){.   
3950e 20 6d 65 6d 73 65 74 28 26 70 2d 3e 7a 44 61 74   memset(&p->zDat
3950f 61 5b 32 5d 2c 20 30 2c 20 70 52 74 72 65 65 2d  a[2], 0, pRtree-
39510 3e 69 4e 6f 64 65 53 69 7a 65 2d 32 29 3b 0a 20  >iNodeSize-2);. 
39511 20 20 20 70 2d 3e 69 73 44 69 72 74 79 20 3d 20     p->isDirty = 
39512 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  1;.  }.}../*.** 
39513 47 69 76 65 6e 20 61 20 6e 6f 64 65 20 6e 75 6d  Given a node num
39514 62 65 72 20 69 4e 6f 64 65 2c 20 72 65 74 75 72  ber iNode, retur
39515 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  n the correspond
39516 69 6e 67 20 6b 65 79 20 74 6f 20 75 73 65 0a 2a  ing key to use.*
39517 2a 20 69 6e 20 74 68 65 20 52 74 72 65 65 2e 61  * in the Rtree.a
39518 48 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73  Hash table..*/.s
39519 74 61 74 69 63 20 69 6e 74 20 6e 6f 64 65 48 61  tatic int nodeHa
3951a 73 68 28 69 36 34 20 69 4e 6f 64 65 29 7b 0a 20  sh(i64 iNode){. 
3951b 20 72 65 74 75 72 6e 20 28 0a 20 20 20 20 28 69   return (.    (i
3951c 4e 6f 64 65 3e 3e 35 36 29 20 5e 20 28 69 4e 6f  Node>>56) ^ (iNo
3951d 64 65 3e 3e 34 38 29 20 5e 20 28 69 4e 6f 64 65  de>>48) ^ (iNode
3951e 3e 3e 34 30 29 20 5e 20 28 69 4e 6f 64 65 3e 3e  >>40) ^ (iNode>>
3951f 33 32 29 20 5e 20 0a 20 20 20 20 28 69 4e 6f 64  32) ^ .    (iNod
39520 65 3e 3e 32 34 29 20 5e 20 28 69 4e 6f 64 65 3e  e>>24) ^ (iNode>
39521 3e 31 36 29 20 5e 20 28 69 4e 6f 64 65 3e 3e 20  >16) ^ (iNode>> 
39522 38 29 20 5e 20 28 69 4e 6f 64 65 3e 3e 20 30 29  8) ^ (iNode>> 0)
39523 0a 20 20 29 20 25 20 48 41 53 48 53 49 5a 45 3b  .  ) % HASHSIZE;
39524 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68  .}../*.** Search
39525 20 74 68 65 20 6e 6f 64 65 20 68 61 73 68 20 74   the node hash t
39526 61 62 6c 65 20 66 6f 72 20 6e 6f 64 65 20 69 4e  able for node iN
39527 6f 64 65 2e 20 49 66 20 66 6f 75 6e 64 2c 20 72  ode. If found, r
39528 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a  eturn a pointer.
39529 2a 2a 20 74 6f 20 69 74 2e 20 4f 74 68 65 72 77  ** to it. Otherw
3952a 69 73 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a  ise, return 0..*
3952b 2f 0a 73 74 61 74 69 63 20 52 74 72 65 65 4e 6f  /.static RtreeNo
3952c 64 65 20 2a 6e 6f 64 65 48 61 73 68 4c 6f 6f 6b  de *nodeHashLook
3952d 75 70 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  up(Rtree *pRtree
3952e 2c 20 69 36 34 20 69 4e 6f 64 65 29 7b 0a 20 20  , i64 iNode){.  
3952f 52 74 72 65 65 4e 6f 64 65 20 2a 70 3b 0a 20 20  RtreeNode *p;.  
39530 61 73 73 65 72 74 28 20 69 4e 6f 64 65 21 3d 30  assert( iNode!=0
39531 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 52 74 72   );.  for(p=pRtr
39532 65 65 2d 3e 61 48 61 73 68 5b 6e 6f 64 65 48 61  ee->aHash[nodeHa
39533 73 68 28 69 4e 6f 64 65 29 5d 3b 20 70 20 26 26  sh(iNode)]; p &&
39534 20 70 2d 3e 69 4e 6f 64 65 21 3d 69 4e 6f 64 65   p->iNode!=iNode
39535 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 3b 0a 20  ; p=p->pNext);. 
39536 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
39537 0a 2a 2a 20 41 64 64 20 6e 6f 64 65 20 70 4e 6f  .** Add node pNo
39538 64 65 20 74 6f 20 74 68 65 20 6e 6f 64 65 20 68  de to the node h
39539 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  ash table..*/.st
3953a 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 48 61  atic void nodeHa
3953b 73 68 49 6e 73 65 72 74 28 52 74 72 65 65 20 2a  shInsert(Rtree *
3953c 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64  pRtree, RtreeNod
3953d 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 69 66 28  e *pNode){.  if(
3953e 20 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20 69 6e   pNode ){.    in
3953f 74 20 69 48 61 73 68 3b 0a 20 20 20 20 61 73 73  t iHash;.    ass
39540 65 72 74 28 20 70 4e 6f 64 65 2d 3e 70 4e 65 78  ert( pNode->pNex
39541 74 3d 3d 30 20 29 3b 0a 20 20 20 20 69 48 61 73  t==0 );.    iHas
39542 68 20 3d 20 6e 6f 64 65 48 61 73 68 28 70 4e 6f  h = nodeHash(pNo
39543 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20  de->iNode);.    
39544 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 70  pNode->pNext = p
39545 52 74 72 65 65 2d 3e 61 48 61 73 68 5b 69 48 61  Rtree->aHash[iHa
39546 73 68 5d 3b 0a 20 20 20 20 70 52 74 72 65 65 2d  sh];.    pRtree-
39547 3e 61 48 61 73 68 5b 69 48 61 73 68 5d 20 3d 20  >aHash[iHash] = 
39548 70 4e 6f 64 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  pNode;.  }.}../*
39549 0a 2a 2a 20 52 65 6d 6f 76 65 20 6e 6f 64 65 20  .** Remove node 
3954a 70 4e 6f 64 65 20 66 72 6f 6d 20 74 68 65 20 6e  pNode from the n
3954b 6f 64 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a  ode hash table..
3954c 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e  */.static void n
3954d 6f 64 65 48 61 73 68 44 65 6c 65 74 65 28 52 74  odeHashDelete(Rt
3954e 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
3954f 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a  eeNode *pNode){.
39550 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a 70 70    RtreeNode **pp
39551 3b 0a 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69  ;.  if( pNode->i
39552 4e 6f 64 65 21 3d 30 20 29 7b 0a 20 20 20 20 70  Node!=0 ){.    p
39553 70 20 3d 20 26 70 52 74 72 65 65 2d 3e 61 48 61  p = &pRtree->aHa
39554 73 68 5b 6e 6f 64 65 48 61 73 68 28 70 4e 6f 64  sh[nodeHash(pNod
39555 65 2d 3e 69 4e 6f 64 65 29 5d 3b 0a 20 20 20 20  e->iNode)];.    
39556 66 6f 72 28 20 3b 20 28 2a 70 70 29 21 3d 70 4e  for( ; (*pp)!=pN
39557 6f 64 65 3b 20 70 70 20 3d 20 26 28 2a 70 70 29  ode; pp = &(*pp)
39558 2d 3e 70 4e 65 78 74 29 7b 20 61 73 73 65 72 74  ->pNext){ assert
39559 28 2a 70 70 29 3b 20 7d 0a 20 20 20 20 2a 70 70  (*pp); }.    *pp
3955a 20 3d 20 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 3b   = pNode->pNext;
3955b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 70 4e 65 78  .    pNode->pNex
3955c 74 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  t = 0;.  }.}../*
3955d 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64  .** Allocate and
3955e 20 72 65 74 75 72 6e 20 6e 65 77 20 72 2d 74 72   return new r-tr
3955f 65 65 20 6e 6f 64 65 2e 20 49 6e 69 74 69 61 6c  ee node. Initial
39560 6c 79 2c 20 28 52 74 72 65 65 4e 6f 64 65 2e 69  ly, (RtreeNode.i
39561 4e 6f 64 65 3d 3d 30 29 2c 0a 2a 2a 20 69 6e 64  Node==0),.** ind
39562 69 63 61 74 69 6e 67 20 74 68 61 74 20 6e 6f 64  icating that nod
39563 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65  e has not yet be
39564 65 6e 20 61 73 73 69 67 6e 65 64 20 61 20 6e 6f  en assigned a no
39565 64 65 20 6e 75 6d 62 65 72 2e 20 49 74 20 69 73  de number. It is
39566 0a 2a 2a 20 61 73 73 69 67 6e 65 64 20 61 20 6e  .** assigned a n
39567 6f 64 65 20 6e 75 6d 62 65 72 20 77 68 65 6e 20  ode number when 
39568 6e 6f 64 65 57 72 69 74 65 28 29 20 69 73 20 63  nodeWrite() is c
39569 61 6c 6c 65 64 20 74 6f 20 77 72 69 74 65 20 74  alled to write t
3956a 68 65 0a 2a 2a 20 6e 6f 64 65 20 63 6f 6e 74 65  he.** node conte
3956b 6e 74 73 20 6f 75 74 20 74 6f 20 74 68 65 20 64  nts out to the d
3956c 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74  atabase..*/.stat
3956d 69 63 20 52 74 72 65 65 4e 6f 64 65 20 2a 6e 6f  ic RtreeNode *no
3956e 64 65 4e 65 77 28 52 74 72 65 65 20 2a 70 52 74  deNew(Rtree *pRt
3956f 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a  ree, RtreeNode *
39570 70 50 61 72 65 6e 74 2c 20 69 6e 74 20 7a 65 72  pParent, int zer
39571 6f 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  o){.  RtreeNode 
39572 2a 70 4e 6f 64 65 3b 0a 20 20 70 4e 6f 64 65 20  *pNode;.  pNode 
39573 3d 20 28 52 74 72 65 65 4e 6f 64 65 20 2a 29 73  = (RtreeNode *)s
39574 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
39575 7a 65 6f 66 28 52 74 72 65 65 4e 6f 64 65 29 20  zeof(RtreeNode) 
39576 2b 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53  + pRtree->iNodeS
39577 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 4e 6f 64  ize);.  if( pNod
39578 65 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28  e ){.    memset(
39579 70 4e 6f 64 65 2c 20 30 2c 20 73 69 7a 65 6f 66  pNode, 0, sizeof
3957a 28 52 74 72 65 65 4e 6f 64 65 29 20 2b 20 28 7a  (RtreeNode) + (z
3957b 65 72 6f 3f 70 52 74 72 65 65 2d 3e 69 4e 6f 64  ero?pRtree->iNod
3957c 65 53 69 7a 65 3a 30 29 29 3b 0a 20 20 20 20 70  eSize:0));.    p
3957d 4e 6f 64 65 2d 3e 7a 44 61 74 61 20 3d 20 28 75  Node->zData = (u
3957e 38 20 2a 29 26 70 4e 6f 64 65 5b 31 5d 3b 0a 20  8 *)&pNode[1];. 
3957f 20 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 20 3d     pNode->nRef =
39580 20 31 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 70   1;.    pNode->p
39581 50 61 72 65 6e 74 20 3d 20 70 50 61 72 65 6e 74  Parent = pParent
39582 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 69 73 44  ;.    pNode->isD
39583 69 72 74 79 20 3d 20 31 3b 0a 20 20 20 20 6e 6f  irty = 1;.    no
39584 64 65 52 65 66 65 72 65 6e 63 65 28 70 50 61 72  deReference(pPar
39585 65 6e 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ent);.  }.  retu
39586 72 6e 20 70 4e 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a  rn pNode;.}../*.
39587 2a 2a 20 4f 62 74 61 69 6e 20 61 20 72 65 66 65  ** Obtain a refe
39588 72 65 6e 63 65 20 74 6f 20 61 6e 20 72 2d 74 72  rence to an r-tr
39589 65 65 20 6e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74  ee node..*/.stat
3958a 69 63 20 69 6e 74 0a 6e 6f 64 65 41 63 71 75 69  ic int.nodeAcqui
3958b 72 65 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  re(.  Rtree *pRt
3958c 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ree,            
3958d 20 2f 2a 20 52 2d 74 72 65 65 20 73 74 72 75 63   /* R-tree struc
3958e 74 75 72 65 20 2a 2f 0a 20 20 69 36 34 20 69 4e  ture */.  i64 iN
3958f 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ode,            
39590 20 20 20 20 20 2f 2a 20 4e 6f 64 65 20 6e 75 6d       /* Node num
39591 62 65 72 20 74 6f 20 6c 6f 61 64 20 2a 2f 0a 20  ber to load */. 
39592 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72   RtreeNode *pPar
39593 65 6e 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 45  ent,        /* E
39594 69 74 68 65 72 20 74 68 65 20 70 61 72 65 6e 74  ither the parent
39595 20 6e 6f 64 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f   node or NULL */
39596 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a 70  .  RtreeNode **p
39597 70 4e 6f 64 65 20 20 20 20 20 20 20 20 20 2f 2a  pNode         /*
39598 20 4f 55 54 3a 20 41 63 71 75 69 72 65 64 20 6e   OUT: Acquired n
39599 6f 64 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ode */.){.  int 
3959a 72 63 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  rc;.  RtreeNode 
3959b 2a 70 4e 6f 64 65 3b 0a 0a 20 20 2f 2a 20 43 68  *pNode;..  /* Ch
3959c 65 63 6b 20 69 66 20 74 68 65 20 72 65 71 75 65  eck if the reque
3959d 73 74 65 64 20 6e 6f 64 65 20 69 73 20 61 6c 72  sted node is alr
3959e 65 61 64 79 20 69 6e 20 74 68 65 20 68 61 73 68  eady in the hash
3959f 20 74 61 62 6c 65 2e 20 49 66 20 73 6f 2c 0a 20   table. If so,. 
395a0 20 2a 2a 20 69 6e 63 72 65 61 73 65 20 69 74 73   ** increase its
395a1 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
395a2 20 61 6e 64 20 72 65 74 75 72 6e 20 69 74 2e 0a   and return it..
395a3 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 4e 6f 64    */.  if( (pNod
395a4 65 20 3d 20 6e 6f 64 65 48 61 73 68 4c 6f 6f 6b  e = nodeHashLook
395a5 75 70 28 70 52 74 72 65 65 2c 20 69 4e 6f 64 65  up(pRtree, iNode
395a6 29 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  )) ){.    assert
395a7 28 20 21 70 50 61 72 65 6e 74 20 7c 7c 20 21 70  ( !pParent || !p
395a8 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 7c 7c  Node->pParent ||
395a9 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3d   pNode->pParent=
395aa 3d 70 50 61 72 65 6e 74 20 29 3b 0a 20 20 20 20  =pParent );.    
395ab 69 66 28 20 70 50 61 72 65 6e 74 20 26 26 20 21  if( pParent && !
395ac 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 29  pNode->pParent )
395ad 7b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 66 65  {.      nodeRefe
395ae 72 65 6e 63 65 28 70 50 61 72 65 6e 74 29 3b 0a  rence(pParent);.
395af 20 20 20 20 20 20 70 4e 6f 64 65 2d 3e 70 50 61        pNode->pPa
395b0 72 65 6e 74 20 3d 20 70 50 61 72 65 6e 74 3b 0a  rent = pParent;.
395b1 20 20 20 20 7d 0a 20 20 20 20 70 4e 6f 64 65 2d      }.    pNode-
395b2 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 2a 70 70  >nRef++;.    *pp
395b3 4e 6f 64 65 20 3d 20 70 4e 6f 64 65 3b 0a 20 20  Node = pNode;.  
395b4 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
395b5 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 70 4e 6f 64 65  OK;.  }..  pNode
395b6 20 3d 20 28 52 74 72 65 65 4e 6f 64 65 20 2a 29   = (RtreeNode *)
395b7 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
395b8 69 7a 65 6f 66 28 52 74 72 65 65 4e 6f 64 65 29  izeof(RtreeNode)
395b9 20 2b 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65   + pRtree->iNode
395ba 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 21 70 4e  Size);.  if( !pN
395bb 6f 64 65 20 29 7b 0a 20 20 20 20 2a 70 70 4e 6f  ode ){.    *ppNo
395bc 64 65 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75  de = 0;.    retu
395bd 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
395be 0a 20 20 7d 0a 20 20 70 4e 6f 64 65 2d 3e 70 50  .  }.  pNode->pP
395bf 61 72 65 6e 74 20 3d 20 70 50 61 72 65 6e 74 3b  arent = pParent;
395c0 0a 20 20 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 20  .  pNode->zData 
395c1 3d 20 28 75 38 20 2a 29 26 70 4e 6f 64 65 5b 31  = (u8 *)&pNode[1
395c2 5d 3b 0a 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66  ];.  pNode->nRef
395c3 20 3d 20 31 3b 0a 20 20 70 4e 6f 64 65 2d 3e 69   = 1;.  pNode->i
395c4 4e 6f 64 65 20 3d 20 69 4e 6f 64 65 3b 0a 20 20  Node = iNode;.  
395c5 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d  pNode->isDirty =
395c6 20 30 3b 0a 20 20 70 4e 6f 64 65 2d 3e 70 4e 65   0;.  pNode->pNe
395c7 78 74 20 3d 20 30 3b 0a 0a 20 20 73 71 6c 69 74  xt = 0;..  sqlit
395c8 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52  e3_bind_int64(pR
395c9 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65 2c  tree->pReadNode,
395ca 20 31 2c 20 69 4e 6f 64 65 29 3b 0a 20 20 72 63   1, iNode);.  rc
395cb 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
395cc 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64  pRtree->pReadNod
395cd 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  e);.  if( rc==SQ
395ce 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
395cf 63 6f 6e 73 74 20 75 38 20 2a 7a 42 6c 6f 62 20  const u8 *zBlob 
395d0 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
395d1 5f 62 6c 6f 62 28 70 52 74 72 65 65 2d 3e 70 52  _blob(pRtree->pR
395d2 65 61 64 4e 6f 64 65 2c 20 30 29 3b 0a 20 20 20  eadNode, 0);.   
395d3 20 6d 65 6d 63 70 79 28 70 4e 6f 64 65 2d 3e 7a   memcpy(pNode->z
395d4 44 61 74 61 2c 20 7a 42 6c 6f 62 2c 20 70 52 74  Data, zBlob, pRt
395d5 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b  ree->iNodeSize);
395d6 0a 20 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e  .    nodeReferen
395d7 63 65 28 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d  ce(pParent);.  }
395d8 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
395d9 33 5f 66 72 65 65 28 70 4e 6f 64 65 29 3b 0a 20  3_free(pNode);. 
395da 20 20 20 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20     pNode = 0;.  
395db 7d 0a 0a 20 20 2a 70 70 4e 6f 64 65 20 3d 20 70  }..  *ppNode = p
395dc 4e 6f 64 65 3b 0a 20 20 72 63 20 3d 20 73 71 6c  Node;.  rc = sql
395dd 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65  ite3_reset(pRtre
395de 65 2d 3e 70 52 65 61 64 4e 6f 64 65 29 3b 0a 0a  e->pReadNode);..
395df 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
395e0 5f 4f 4b 20 26 26 20 69 4e 6f 64 65 3d 3d 31 20  _OK && iNode==1 
395e1 29 7b 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 69  ){.    pRtree->i
395e2 44 65 70 74 68 20 3d 20 72 65 61 64 49 6e 74 31  Depth = readInt1
395e3 36 28 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 29 3b  6(pNode->zData);
395e4 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
395e5 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26  (rc==SQLITE_OK &
395e6 26 20 70 4e 6f 64 65 29 20 7c 7c 20 28 70 4e 6f  & pNode) || (pNo
395e7 64 65 3d 3d 30 20 26 26 20 72 63 21 3d 53 51 4c  de==0 && rc!=SQL
395e8 49 54 45 5f 4f 4b 29 20 29 3b 0a 20 20 6e 6f 64  ITE_OK) );.  nod
395e9 65 48 61 73 68 49 6e 73 65 72 74 28 70 52 74 72  eHashInsert(pRtr
395ea 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 0a 20 20 72  ee, pNode);..  r
395eb 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
395ec 2a 2a 20 4f 76 65 72 77 72 69 74 65 20 63 65 6c  ** Overwrite cel
395ed 6c 20 69 43 65 6c 6c 20 6f 66 20 6e 6f 64 65 20  l iCell of node 
395ee 70 4e 6f 64 65 20 77 69 74 68 20 74 68 65 20 63  pNode with the c
395ef 6f 6e 74 65 6e 74 73 20 6f 66 20 70 43 65 6c 6c  ontents of pCell
395f0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
395f1 20 6e 6f 64 65 4f 76 65 72 77 72 69 74 65 43 65   nodeOverwriteCe
395f2 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  ll(.  Rtree *pRt
395f3 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64  ree, .  RtreeNod
395f4 65 20 2a 70 4e 6f 64 65 2c 20 20 0a 20 20 52 74  e *pNode,  .  Rt
395f5 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 20  reeCell *pCell, 
395f6 0a 20 20 69 6e 74 20 69 43 65 6c 6c 0a 29 7b 0a  .  int iCell.){.
395f7 20 20 69 6e 74 20 69 69 3b 0a 20 20 75 38 20 2a    int ii;.  u8 *
395f8 70 20 3d 20 26 70 4e 6f 64 65 2d 3e 7a 44 61 74  p = &pNode->zDat
395f9 61 5b 34 20 2b 20 70 52 74 72 65 65 2d 3e 6e 42  a[4 + pRtree->nB
395fa 79 74 65 73 50 65 72 43 65 6c 6c 2a 69 43 65 6c  ytesPerCell*iCel
395fb 6c 5d 3b 0a 20 20 70 20 2b 3d 20 77 72 69 74 65  l];.  p += write
395fc 49 6e 74 36 34 28 70 2c 20 70 43 65 6c 6c 2d 3e  Int64(p, pCell->
395fd 69 52 6f 77 69 64 29 3b 0a 20 20 66 6f 72 28 69  iRowid);.  for(i
395fe 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d  i=0; ii<(pRtree-
395ff 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 2b 29 7b  >nDim*2); ii++){
39600 0a 20 20 20 20 70 20 2b 3d 20 77 72 69 74 65 43  .    p += writeC
39601 6f 6f 72 64 28 70 2c 20 26 70 43 65 6c 6c 2d 3e  oord(p, &pCell->
39602 61 43 6f 6f 72 64 5b 69 69 5d 29 3b 0a 20 20 7d  aCoord[ii]);.  }
39603 0a 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74  .  pNode->isDirt
39604 79 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  y = 1;.}../*.** 
39605 52 65 6d 6f 76 65 20 63 65 6c 6c 20 74 68 65 20  Remove cell the 
39606 63 65 6c 6c 20 77 69 74 68 20 69 6e 64 65 78 20  cell with index 
39607 69 43 65 6c 6c 20 66 72 6f 6d 20 6e 6f 64 65 20  iCell from node 
39608 70 4e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  pNode..*/.static
39609 20 76 6f 69 64 20 6e 6f 64 65 44 65 6c 65 74 65   void nodeDelete
3960a 43 65 6c 6c 28 52 74 72 65 65 20 2a 70 52 74 72  Cell(Rtree *pRtr
3960b 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ee, RtreeNode *p
3960c 4e 6f 64 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29  Node, int iCell)
3960d 7b 0a 20 20 75 38 20 2a 70 44 73 74 20 3d 20 26  {.  u8 *pDst = &
3960e 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 34 20 2b  pNode->zData[4 +
3960f 20 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50   pRtree->nBytesP
39610 65 72 43 65 6c 6c 2a 69 43 65 6c 6c 5d 3b 0a 20  erCell*iCell];. 
39611 20 75 38 20 2a 70 53 72 63 20 3d 20 26 70 44 73   u8 *pSrc = &pDs
39612 74 5b 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73  t[pRtree->nBytes
39613 50 65 72 43 65 6c 6c 5d 3b 0a 20 20 69 6e 74 20  PerCell];.  int 
39614 6e 42 79 74 65 20 3d 20 28 4e 43 45 4c 4c 28 70  nByte = (NCELL(p
39615 4e 6f 64 65 29 20 2d 20 69 43 65 6c 6c 20 2d 20  Node) - iCell - 
39616 31 29 20 2a 20 70 52 74 72 65 65 2d 3e 6e 42 79  1) * pRtree->nBy
39617 74 65 73 50 65 72 43 65 6c 6c 3b 0a 20 20 6d 65  tesPerCell;.  me
39618 6d 6d 6f 76 65 28 70 44 73 74 2c 20 70 53 72 63  mmove(pDst, pSrc
39619 2c 20 6e 42 79 74 65 29 3b 0a 20 20 77 72 69 74  , nByte);.  writ
3961a 65 49 6e 74 31 36 28 26 70 4e 6f 64 65 2d 3e 7a  eInt16(&pNode->z
3961b 44 61 74 61 5b 32 5d 2c 20 4e 43 45 4c 4c 28 70  Data[2], NCELL(p
3961c 4e 6f 64 65 29 2d 31 29 3b 0a 20 20 70 4e 6f 64  Node)-1);.  pNod
3961d 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0a  e->isDirty = 1;.
3961e 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20  }../*.** Insert 
3961f 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
39620 63 65 6c 6c 20 70 43 65 6c 6c 20 69 6e 74 6f 20  cell pCell into 
39621 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 49 66 20 74  node pNode. If t
39622 68 65 20 69 6e 73 65 72 74 0a 2a 2a 20 69 73 20  he insert.** is 
39623 73 75 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75  successful, retu
39624 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a  rn SQLITE_OK..**
39625 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20  .** If there is 
39626 6e 6f 74 20 65 6e 6f 75 67 68 20 66 72 65 65 20  not enough free 
39627 73 70 61 63 65 20 69 6e 20 70 4e 6f 64 65 2c 20  space in pNode, 
39628 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55  return SQLITE_FU
39629 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  LL..*/.static in
3962a 74 0a 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c  t.nodeInsertCell
3962b 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  (.  Rtree *pRtre
3962c 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  e, .  RtreeNode 
3962d 2a 70 4e 6f 64 65 2c 20 0a 20 20 52 74 72 65 65  *pNode, .  Rtree
3962e 43 65 6c 6c 20 2a 70 43 65 6c 6c 20 0a 29 7b 0a  Cell *pCell .){.
3962f 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20    int nCell;    
39630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39631 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d 62 65  /* Current numbe
39632 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 70 4e  r of cells in pN
39633 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61  ode */.  int nMa
39634 78 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  xCell;          
39635 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
39636 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c  m number of cell
39637 73 20 66 6f 72 20 70 4e 6f 64 65 20 2a 2f 0a 0a  s for pNode */..
39638 20 20 6e 4d 61 78 43 65 6c 6c 20 3d 20 28 70 52    nMaxCell = (pR
39639 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 2d  tree->iNodeSize-
3963a 34 29 2f 70 52 74 72 65 65 2d 3e 6e 42 79 74 65  4)/pRtree->nByte
3963b 73 50 65 72 43 65 6c 6c 3b 0a 20 20 6e 43 65 6c  sPerCell;.  nCel
3963c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29  l = NCELL(pNode)
3963d 3b 0a 0a 20 20 61 73 73 65 72 74 28 6e 43 65 6c  ;..  assert(nCel
3963e 6c 3c 3d 6e 4d 61 78 43 65 6c 6c 29 3b 0a 0a 20  l<=nMaxCell);.. 
3963f 20 69 66 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43   if( nCell<nMaxC
39640 65 6c 6c 20 29 7b 0a 20 20 20 20 6e 6f 64 65 4f  ell ){.    nodeO
39641 76 65 72 77 72 69 74 65 43 65 6c 6c 28 70 52 74  verwriteCell(pRt
39642 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c  ree, pNode, pCel
39643 6c 2c 20 6e 43 65 6c 6c 29 3b 0a 20 20 20 20 77  l, nCell);.    w
39644 72 69 74 65 49 6e 74 31 36 28 26 70 4e 6f 64 65  riteInt16(&pNode
39645 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20 6e 43 65 6c  ->zData[2], nCel
39646 6c 2b 31 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  l+1);.    pNode-
39647 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 20 20  >isDirty = 1;.  
39648 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28 6e 43 65  }..  return (nCe
39649 6c 6c 3d 3d 6e 4d 61 78 43 65 6c 6c 29 3b 0a 7d  ll==nMaxCell);.}
3964a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e  ../*.** If the n
3964b 6f 64 65 20 69 73 20 64 69 72 74 79 2c 20 77 72  ode is dirty, wr
3964c 69 74 65 20 69 74 20 6f 75 74 20 74 6f 20 74 68  ite it out to th
3964d 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73  e database..*/.s
3964e 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64 65 57 72  tatic int.nodeWr
3964f 69 74 65 28 52 74 72 65 65 20 2a 70 52 74 72 65  ite(Rtree *pRtre
39650 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  e, RtreeNode *pN
39651 6f 64 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ode){.  int rc =
39652 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
39653 28 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79  ( pNode->isDirty
39654 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
39655 73 74 6d 74 20 2a 70 20 3d 20 70 52 74 72 65 65  stmt *p = pRtree
39656 2d 3e 70 57 72 69 74 65 4e 6f 64 65 3b 0a 20 20  ->pWriteNode;.  
39657 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f    if( pNode->iNo
39658 64 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  de ){.      sqli
39659 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
3965a 2c 20 31 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64  , 1, pNode->iNod
3965b 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  e);.    }else{. 
3965c 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e       sqlite3_bin
3965d 64 5f 6e 75 6c 6c 28 70 2c 20 31 29 3b 0a 20 20  d_null(p, 1);.  
3965e 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
3965f 62 69 6e 64 5f 62 6c 6f 62 28 70 2c 20 32 2c 20  bind_blob(p, 2, 
39660 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c 20 70 52  pNode->zData, pR
39661 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 2c  tree->iNodeSize,
39662 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
39663 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 65  .    sqlite3_ste
39664 70 28 70 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  p(p);.    pNode-
39665 3e 69 73 44 69 72 74 79 20 3d 20 30 3b 0a 20 20  >isDirty = 0;.  
39666 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72    rc = sqlite3_r
39667 65 73 65 74 28 70 29 3b 0a 20 20 20 20 69 66 28  eset(p);.    if(
39668 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 30   pNode->iNode==0
39669 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   && rc==SQLITE_O
3966a 4b 20 29 7b 0a 20 20 20 20 20 20 70 4e 6f 64 65  K ){.      pNode
3966b 2d 3e 69 4e 6f 64 65 20 3d 20 73 71 6c 69 74 65  ->iNode = sqlite
3966c 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  3_last_insert_ro
3966d 77 69 64 28 70 52 74 72 65 65 2d 3e 64 62 29 3b  wid(pRtree->db);
3966e 0a 20 20 20 20 20 20 6e 6f 64 65 48 61 73 68 49  .      nodeHashI
3966f 6e 73 65 72 74 28 70 52 74 72 65 65 2c 20 70 4e  nsert(pRtree, pN
39670 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ode);.    }.  }.
39671 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
39672 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20  /*.** Release a 
39673 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 6e  reference to a n
39674 6f 64 65 2e 20 49 66 20 74 68 65 20 6e 6f 64 65  ode. If the node
39675 20 69 73 20 64 69 72 74 79 20 61 6e 64 20 74 68   is dirty and th
39676 65 20 72 65 66 65 72 65 6e 63 65 0a 2a 2a 20 63  e reference.** c
39677 6f 75 6e 74 20 64 72 6f 70 73 20 74 6f 20 7a 65  ount drops to ze
39678 72 6f 2c 20 74 68 65 20 6e 6f 64 65 20 64 61 74  ro, the node dat
39679 61 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20  a is written to 
3967a 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f  the database..*/
3967b 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64 65  .static int.node
3967c 52 65 6c 65 61 73 65 28 52 74 72 65 65 20 2a 70  Release(Rtree *p
3967d 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
3967e 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 69 6e 74 20   *pNode){.  int 
3967f 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
39680 20 20 69 66 28 20 70 4e 6f 64 65 20 29 7b 0a 20    if( pNode ){. 
39681 20 20 20 61 73 73 65 72 74 28 20 70 4e 6f 64 65     assert( pNode
39682 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20  ->nRef>0 );.    
39683 70 4e 6f 64 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20  pNode->nRef--;. 
39684 20 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 6e 52     if( pNode->nR
39685 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  ef==0 ){.      i
39686 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d  f( pNode->iNode=
39687 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52  =1 ){.        pR
39688 74 72 65 65 2d 3e 69 44 65 70 74 68 20 3d 20 2d  tree->iDepth = -
39689 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
3968a 20 69 66 28 20 70 4e 6f 64 65 2d 3e 70 50 61 72   if( pNode->pPar
3968b 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ent ){.        r
3968c 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  c = nodeRelease(
3968d 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2d 3e 70  pRtree, pNode->p
3968e 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 7d  Parent);.      }
3968f 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
39690 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
39691 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 57 72 69      rc = nodeWri
39692 74 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  te(pRtree, pNode
39693 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
39694 20 6e 6f 64 65 48 61 73 68 44 65 6c 65 74 65 28   nodeHashDelete(
39695 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a  pRtree, pNode);.
39696 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
39697 65 65 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20 7d  ee(pNode);.    }
39698 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
39699 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
3969a 6e 20 74 68 65 20 36 34 2d 62 69 74 20 69 6e 74  n the 64-bit int
3969b 65 67 65 72 20 76 61 6c 75 65 20 61 73 73 6f 63  eger value assoc
3969c 69 61 74 65 64 20 77 69 74 68 20 63 65 6c 6c 20  iated with cell 
3969d 69 43 65 6c 6c 20 6f 66 0a 2a 2a 20 6e 6f 64 65  iCell of.** node
3969e 20 70 4e 6f 64 65 2e 20 49 66 20 70 4e 6f 64 65   pNode. If pNode
3969f 20 69 73 20 61 20 6c 65 61 66 20 6e 6f 64 65 2c   is a leaf node,
396a0 20 74 68 69 73 20 69 73 20 61 20 72 6f 77 69 64   this is a rowid
396a1 2e 20 49 66 20 69 74 20 69 73 0a 2a 2a 20 61 6e  . If it is.** an
396a2 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 2c 20   internal node, 
396a3 74 68 65 6e 20 74 68 65 20 36 34 2d 62 69 74 20  then the 64-bit 
396a4 69 6e 74 65 67 65 72 20 69 73 20 61 20 63 68 69  integer is a chi
396a5 6c 64 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a  ld page number..
396a6 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20 6e 6f  */.static i64 no
396a7 64 65 47 65 74 52 6f 77 69 64 28 0a 20 20 52 74  deGetRowid(.  Rt
396a8 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20  ree *pRtree, .  
396a9 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
396aa 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c 6c 0a 29  , .  int iCell.)
396ab 7b 0a 20 20 61 73 73 65 72 74 28 20 69 43 65 6c  {.  assert( iCel
396ac 6c 3c 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 20 29  l<NCELL(pNode) )
396ad 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 61 64 49  ;.  return readI
396ae 6e 74 36 34 28 26 70 4e 6f 64 65 2d 3e 7a 44 61  nt64(&pNode->zDa
396af 74 61 5b 34 20 2b 20 70 52 74 72 65 65 2d 3e 6e  ta[4 + pRtree->n
396b0 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 69 43 65  BytesPerCell*iCe
396b1 6c 6c 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ll]);.}../*.** R
396b2 65 74 75 72 6e 20 63 6f 6f 72 64 69 6e 61 74 65  eturn coordinate
396b3 20 69 43 6f 6f 72 64 20 66 72 6f 6d 20 63 65 6c   iCoord from cel
396b4 6c 20 69 43 65 6c 6c 20 69 6e 20 6e 6f 64 65 20  l iCell in node 
396b5 70 4e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  pNode..*/.static
396b6 20 76 6f 69 64 20 6e 6f 64 65 47 65 74 43 6f 6f   void nodeGetCoo
396b7 72 64 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  rd(.  Rtree *pRt
396b8 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64  ree, .  RtreeNod
396b9 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69 6e 74  e *pNode, .  int
396ba 20 69 43 65 6c 6c 2c 0a 20 20 69 6e 74 20 69 43   iCell,.  int iC
396bb 6f 6f 72 64 2c 0a 20 20 52 74 72 65 65 43 6f 6f  oord,.  RtreeCoo
396bc 72 64 20 2a 70 43 6f 6f 72 64 20 20 20 20 20 20  rd *pCoord      
396bd 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f       /* Space to
396be 20 77 72 69 74 65 20 72 65 73 75 6c 74 20 74 6f   write result to
396bf 20 2a 2f 0a 29 7b 0a 20 20 72 65 61 64 43 6f 6f   */.){.  readCoo
396c0 72 64 28 26 70 4e 6f 64 65 2d 3e 7a 44 61 74 61  rd(&pNode->zData
396c1 5b 31 32 20 2b 20 70 52 74 72 65 65 2d 3e 6e 42  [12 + pRtree->nB
396c2 79 74 65 73 50 65 72 43 65 6c 6c 2a 69 43 65 6c  ytesPerCell*iCel
396c3 6c 20 2b 20 34 2a 69 43 6f 6f 72 64 5d 2c 20 70  l + 4*iCoord], p
396c4 43 6f 6f 72 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Coord);.}../*.**
396c5 20 44 65 73 65 72 69 61 6c 69 7a 65 20 63 65 6c   Deserialize cel
396c6 6c 20 69 43 65 6c 6c 20 6f 66 20 6e 6f 64 65 20  l iCell of node 
396c7 70 4e 6f 64 65 2e 20 50 6f 70 75 6c 61 74 65 20  pNode. Populate 
396c8 74 68 65 20 73 74 72 75 63 74 75 72 65 20 70 6f  the structure po
396c9 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 70  inted.** to by p
396ca 43 65 6c 6c 20 77 69 74 68 20 74 68 65 20 72 65  Cell with the re
396cb 73 75 6c 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  sults..*/.static
396cc 20 76 6f 69 64 20 6e 6f 64 65 47 65 74 43 65 6c   void nodeGetCel
396cd 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  l(.  Rtree *pRtr
396ce 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64 65  ee, .  RtreeNode
396cf 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69 6e 74 20   *pNode, .  int 
396d0 69 43 65 6c 6c 2c 0a 20 20 52 74 72 65 65 43 65  iCell,.  RtreeCe
396d1 6c 6c 20 2a 70 43 65 6c 6c 0a 29 7b 0a 20 20 69  ll *pCell.){.  i
396d2 6e 74 20 69 69 3b 0a 20 20 70 43 65 6c 6c 2d 3e  nt ii;.  pCell->
396d3 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74  iRowid = nodeGet
396d4 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70 4e  Rowid(pRtree, pN
396d5 6f 64 65 2c 20 69 43 65 6c 6c 29 3b 0a 20 20 66  ode, iCell);.  f
396d6 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 52 74 72  or(ii=0; ii<pRtr
396d7 65 65 2d 3e 6e 44 69 6d 2a 32 3b 20 69 69 2b 2b  ee->nDim*2; ii++
396d8 29 7b 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 6f  ){.    nodeGetCo
396d9 6f 72 64 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  ord(pRtree, pNod
396da 65 2c 20 69 43 65 6c 6c 2c 20 69 69 2c 20 26 70  e, iCell, ii, &p
396db 43 65 6c 6c 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d  Cell->aCoord[ii]
396dc 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 46 6f  );.  }.}.../* Fo
396dd 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
396de 6e 20 66 6f 72 20 74 68 65 20 66 75 6e 63 74 69  n for the functi
396df 6f 6e 20 74 68 61 74 20 64 6f 65 73 20 74 68 65  on that does the
396e0 20 77 6f 72 6b 20 6f 66 0a 2a 2a 20 74 68 65 20   work of.** the 
396e1 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
396e2 64 75 6c 65 20 78 43 72 65 61 74 65 28 29 20 61  dule xCreate() a
396e3 6e 64 20 78 43 6f 6e 6e 65 63 74 28 29 20 6d 65  nd xConnect() me
396e4 74 68 6f 64 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  thods..*/.static
396e5 20 69 6e 74 20 72 74 72 65 65 49 6e 69 74 28 0a   int rtreeInit(.
396e6 20 20 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69    sqlite3 *, voi
396e7 64 20 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20  d *, int, const 
396e8 63 68 61 72 20 2a 63 6f 6e 73 74 2a 2c 20 73 71  char *const*, sq
396e9 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 2c 20 63  lite3_vtab **, c
396ea 68 61 72 20 2a 2a 2c 20 69 6e 74 0a 29 3b 0a 0a  har **, int.);..
396eb 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72  /* .** Rtree vir
396ec 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
396ed 65 20 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64  e xCreate method
396ee 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
396ef 72 74 72 65 65 43 72 65 61 74 65 28 0a 20 20 73  rtreeCreate(.  s
396f0 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f  qlite3 *db,.  vo
396f1 69 64 20 2a 70 41 75 78 2c 0a 20 20 69 6e 74 20  id *pAux,.  int 
396f2 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72  argc, const char
396f3 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20   *const*argv,.  
396f4 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70  sqlite3_vtab **p
396f5 70 56 74 61 62 2c 0a 20 20 63 68 61 72 20 2a 2a  pVtab,.  char **
396f6 70 7a 45 72 72 0a 29 7b 0a 20 20 72 65 74 75 72  pzErr.){.  retur
396f7 6e 20 72 74 72 65 65 49 6e 69 74 28 64 62 2c 20  n rtreeInit(db, 
396f8 70 41 75 78 2c 20 61 72 67 63 2c 20 61 72 67 76  pAux, argc, argv
396f9 2c 20 70 70 56 74 61 62 2c 20 70 7a 45 72 72 2c  , ppVtab, pzErr,
396fa 20 31 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52   1);.}../* .** R
396fb 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62  tree virtual tab
396fc 6c 65 20 6d 6f 64 75 6c 65 20 78 43 6f 6e 6e 65  le module xConne
396fd 63 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74  ct method..*/.st
396fe 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 43 6f  atic int rtreeCo
396ff 6e 6e 65 63 74 28 0a 20 20 73 71 6c 69 74 65 33  nnect(.  sqlite3
39700 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20 2a 70 41   *db,.  void *pA
39701 75 78 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  ux,.  int argc, 
39702 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
39703 74 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65  t*argv,.  sqlite
39704 33 5f 76 74 61 62 20 2a 2a 70 70 56 74 61 62 2c  3_vtab **ppVtab,
39705 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 0a  .  char **pzErr.
39706 29 7b 0a 20 20 72 65 74 75 72 6e 20 72 74 72 65  ){.  return rtre
39707 65 49 6e 69 74 28 64 62 2c 20 70 41 75 78 2c 20  eInit(db, pAux, 
39708 61 72 67 63 2c 20 61 72 67 76 2c 20 70 70 56 74  argc, argv, ppVt
39709 61 62 2c 20 70 7a 45 72 72 2c 20 30 29 3b 0a 7d  ab, pzErr, 0);.}
3970a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e  ../*.** Incremen
3970b 74 20 74 68 65 20 72 2d 74 72 65 65 20 72 65 66  t the r-tree ref
3970c 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e 0a 2a 2f  erence count..*/
3970d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 74 72  .static void rtr
3970e 65 65 52 65 66 65 72 65 6e 63 65 28 52 74 72 65  eeReference(Rtre
3970f 65 20 2a 70 52 74 72 65 65 29 7b 0a 20 20 70 52  e *pRtree){.  pR
39710 74 72 65 65 2d 3e 6e 42 75 73 79 2b 2b 3b 0a 7d  tree->nBusy++;.}
39711 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e  ../*.** Decremen
39712 74 20 74 68 65 20 72 2d 74 72 65 65 20 72 65 66  t the r-tree ref
39713 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e 20 57 68  erence count. Wh
39714 65 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  en the reference
39715 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 0a 2a   count reaches.*
39716 2a 20 7a 65 72 6f 20 74 68 65 20 73 74 72 75 63  * zero the struc
39717 74 75 72 65 20 69 73 20 64 65 6c 65 74 65 64 2e  ture is deleted.
39718 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
39719 72 74 72 65 65 52 65 6c 65 61 73 65 28 52 74 72  rtreeRelease(Rtr
3971a 65 65 20 2a 70 52 74 72 65 65 29 7b 0a 20 20 70  ee *pRtree){.  p
3971b 52 74 72 65 65 2d 3e 6e 42 75 73 79 2d 2d 3b 0a  Rtree->nBusy--;.
3971c 20 20 69 66 28 20 70 52 74 72 65 65 2d 3e 6e 42    if( pRtree->nB
3971d 75 73 79 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  usy==0 ){.    sq
3971e 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70  lite3_finalize(p
3971f 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65  Rtree->pReadNode
39720 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
39721 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e  inalize(pRtree->
39722 70 57 72 69 74 65 4e 6f 64 65 29 3b 0a 20 20 20  pWriteNode);.   
39723 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
39724 65 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  e(pRtree->pDelet
39725 65 4e 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69  eNode);.    sqli
39726 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74  te3_finalize(pRt
39727 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29  ree->pReadRowid)
39728 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  ;.    sqlite3_fi
39729 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70  nalize(pRtree->p
3972a 57 72 69 74 65 52 6f 77 69 64 29 3b 0a 20 20 20  WriteRowid);.   
3972b 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
3972c 65 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  e(pRtree->pDelet
3972d 65 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c  eRowid);.    sql
3972e 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52  ite3_finalize(pR
3972f 74 72 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e  tree->pReadParen
39730 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  t);.    sqlite3_
39731 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d  finalize(pRtree-
39732 3e 70 57 72 69 74 65 50 61 72 65 6e 74 29 3b 0a  >pWriteParent);.
39733 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61      sqlite3_fina
39734 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70 44 65  lize(pRtree->pDe
39735 6c 65 74 65 50 61 72 65 6e 74 29 3b 0a 20 20 20  leteParent);.   
39736 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 52   sqlite3_free(pR
39737 74 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  tree);.  }.}../*
39738 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75   .** Rtree virtu
39739 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20  al table module 
3973a 78 44 69 73 63 6f 6e 6e 65 63 74 20 6d 65 74 68  xDisconnect meth
3973b 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  od..*/.static in
3973c 74 20 72 74 72 65 65 44 69 73 63 6f 6e 6e 65 63  t rtreeDisconnec
3973d 74 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  t(sqlite3_vtab *
3973e 70 56 74 61 62 29 7b 0a 20 20 72 74 72 65 65 52  pVtab){.  rtreeR
3973f 65 6c 65 61 73 65 28 28 52 74 72 65 65 20 2a 29  elease((Rtree *)
39740 70 56 74 61 62 29 3b 0a 20 20 72 65 74 75 72 6e  pVtab);.  return
39741 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
39742 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74  * .** Rtree virt
39743 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65  ual table module
39744 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64   xDestroy method
39745 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
39746 72 74 72 65 65 44 65 73 74 72 6f 79 28 73 71 6c  rtreeDestroy(sql
39747 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
39748 29 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  ){.  Rtree *pRtr
39749 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29 70 56  ee = (Rtree *)pV
3974a 74 61 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  tab;.  int rc;. 
3974b 20 63 68 61 72 20 2a 7a 43 72 65 61 74 65 20 3d   char *zCreate =
3974c 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
3974d 28 0a 20 20 20 20 22 44 52 4f 50 20 54 41 42 4c  (.    "DROP TABL
3974e 45 20 27 25 71 27 2e 27 25 71 5f 6e 6f 64 65 27  E '%q'.'%q_node'
3974f 3b 22 0a 20 20 20 20 22 44 52 4f 50 20 54 41 42  ;".    "DROP TAB
39750 4c 45 20 27 25 71 27 2e 27 25 71 5f 72 6f 77 69  LE '%q'.'%q_rowi
39751 64 27 3b 22 0a 20 20 20 20 22 44 52 4f 50 20 54  d';".    "DROP T
39752 41 42 4c 45 20 27 25 71 27 2e 27 25 71 5f 70 61  ABLE '%q'.'%q_pa
39753 72 65 6e 74 27 3b 22 2c 0a 20 20 20 20 70 52 74  rent';",.    pRt
39754 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65 65  ree->zDb, pRtree
39755 2d 3e 7a 4e 61 6d 65 2c 20 0a 20 20 20 20 70 52  ->zName, .    pR
39756 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65  tree->zDb, pRtre
39757 65 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 70 52  e->zName,.    pR
39758 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65  tree->zDb, pRtre
39759 65 2d 3e 7a 4e 61 6d 65 0a 20 20 29 3b 0a 20 20  e->zName.  );.  
3975a 69 66 28 20 21 7a 43 72 65 61 74 65 20 29 7b 0a  if( !zCreate ){.
3975b 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
3975c 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a  NOMEM;.  }else{.
3975d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
3975e 5f 65 78 65 63 28 70 52 74 72 65 65 2d 3e 64 62  _exec(pRtree->db
3975f 2c 20 7a 43 72 65 61 74 65 2c 20 30 2c 20 30 2c  , zCreate, 0, 0,
39760 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
39761 5f 66 72 65 65 28 7a 43 72 65 61 74 65 29 3b 0a  _free(zCreate);.
39762 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
39763 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
39764 74 72 65 65 52 65 6c 65 61 73 65 28 70 52 74 72  treeRelease(pRtr
39765 65 65 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  ee);.  }..  retu
39766 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  rn rc;.}../* .**
39767 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74   Rtree virtual t
39768 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 4f 70 65  able module xOpe
39769 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61  n method..*/.sta
3976a 74 69 63 20 69 6e 74 20 72 74 72 65 65 4f 70 65  tic int rtreeOpe
3976b 6e 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  n(sqlite3_vtab *
3976c 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 76  pVTab, sqlite3_v
3976d 74 61 62 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43  tab_cursor **ppC
3976e 75 72 73 6f 72 29 7b 0a 20 20 69 6e 74 20 72 63  ursor){.  int rc
3976f 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
39770 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a  .  RtreeCursor *
39771 70 43 73 72 3b 0a 0a 20 20 70 43 73 72 20 3d 20  pCsr;..  pCsr = 
39772 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29 73  (RtreeCursor *)s
39773 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
39774 7a 65 6f 66 28 52 74 72 65 65 43 75 72 73 6f 72  zeof(RtreeCursor
39775 29 29 3b 0a 20 20 69 66 28 20 70 43 73 72 20 29  ));.  if( pCsr )
39776 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 43 73  {.    memset(pCs
39777 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72  r, 0, sizeof(Rtr
39778 65 65 43 75 72 73 6f 72 29 29 3b 0a 20 20 20 20  eeCursor));.    
39779 70 43 73 72 2d 3e 62 61 73 65 2e 70 56 74 61 62  pCsr->base.pVtab
3977a 20 3d 20 70 56 54 61 62 3b 0a 20 20 20 20 72 63   = pVTab;.    rc
3977b 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
3977c 7d 0a 20 20 2a 70 70 43 75 72 73 6f 72 20 3d 20  }.  *ppCursor = 
3977d 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
3977e 72 73 6f 72 20 2a 29 70 43 73 72 3b 0a 0a 20 20  rsor *)pCsr;..  
3977f 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
39780 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75   .** Rtree virtu
39781 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20  al table module 
39782 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 2e 0a 2a  xClose method..*
39783 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72  /.static int rtr
39784 65 65 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  eeClose(sqlite3_
39785 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 63 75 72  vtab_cursor *cur
39786 29 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  ){.  Rtree *pRtr
39787 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29 28 63  ee = (Rtree *)(c
39788 75 72 2d 3e 70 56 74 61 62 29 3b 0a 20 20 69 6e  ur->pVtab);.  in
39789 74 20 72 63 3b 0a 20 20 52 74 72 65 65 43 75 72  t rc;.  RtreeCur
3978a 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 52 74 72  sor *pCsr = (Rtr
3978b 65 65 43 75 72 73 6f 72 20 2a 29 63 75 72 3b 0a  eeCursor *)cur;.
3978c 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
3978d 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  Csr->aConstraint
3978e 29 3b 0a 20 20 72 63 20 3d 20 6e 6f 64 65 52 65  );.  rc = nodeRe
3978f 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 43  lease(pRtree, pC
39790 73 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20 73 71  sr->pNode);.  sq
39791 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73 72 29  lite3_free(pCsr)
39792 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
39793 0a 0a 2f 2a 0a 2a 2a 20 52 74 72 65 65 20 76 69  ../*.** Rtree vi
39794 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
39795 6c 65 20 78 45 6f 66 20 6d 65 74 68 6f 64 2e 0a  le xEof method..
39796 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e  **.** Return non
39797 2d 7a 65 72 6f 20 69 66 20 74 68 65 20 63 75 72  -zero if the cur
39798 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 63 75 72  sor does not cur
39799 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20 74 6f 20  rently point to 
3979a 61 20 76 61 6c 69 64 20 0a 2a 2a 20 72 65 63 6f  a valid .** reco
3979b 72 64 20 28 69 2e 65 20 69 66 20 74 68 65 20 73  rd (i.e if the s
3979c 63 61 6e 20 68 61 73 20 66 69 6e 69 73 68 65 64  can has finished
3979d 29 2c 20 6f 72 20 7a 65 72 6f 20 6f 74 68 65 72  ), or zero other
3979e 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  wise..*/.static 
3979f 69 6e 74 20 72 74 72 65 65 45 6f 66 28 73 71 6c  int rtreeEof(sql
397a0 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
397a1 20 2a 63 75 72 29 7b 0a 20 20 52 74 72 65 65 43   *cur){.  RtreeC
397a2 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 52  ursor *pCsr = (R
397a3 74 72 65 65 43 75 72 73 6f 72 20 2a 29 63 75 72  treeCursor *)cur
397a4 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 43 73 72  ;.  return (pCsr
397a5 2d 3e 70 4e 6f 64 65 3d 3d 30 29 3b 0a 7d 0a 0a  ->pNode==0);.}..
397a6 2f 2a 20 0a 2a 2a 20 43 75 72 73 6f 72 20 70 43  /* .** Cursor pC
397a7 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20  ursor currently 
397a8 70 6f 69 6e 74 73 20 74 6f 20 61 20 63 65 6c 6c  points to a cell
397a9 20 69 6e 20 61 20 6e 6f 6e 2d 6c 65 61 66 20 70   in a non-leaf p
397aa 61 67 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74  age..** Return t
397ab 72 75 65 20 69 66 20 74 68 65 20 73 75 62 2d 74  rue if the sub-t
397ac 72 65 65 20 68 65 61 64 65 64 20 62 79 20 74 68  ree headed by th
397ad 65 20 63 65 6c 6c 20 69 73 20 66 69 6c 74 65 72  e cell is filter
397ae 65 64 0a 2a 2a 20 28 65 78 63 6c 75 64 65 64 29  ed.** (excluded)
397af 20 62 79 20 74 68 65 20 63 6f 6e 73 74 72 61 69   by the constrai
397b0 6e 74 73 20 69 6e 20 74 68 65 20 70 43 75 72 73  nts in the pCurs
397b1 6f 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b  or->aConstraint[
397b2 5d 20 0a 2a 2a 20 61 72 72 61 79 2c 20 6f 72 20  ] .** array, or 
397b3 66 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65 2e  false otherwise.
397b4 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74  .*/.static int t
397b5 65 73 74 52 74 72 65 65 43 65 6c 6c 28 52 74 72  estRtreeCell(Rtr
397b6 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
397b7 65 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  eCursor *pCursor
397b8 29 7b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 63  ){.  RtreeCell c
397b9 65 6c 6c 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  ell;.  int ii;. 
397ba 20 69 6e 74 20 62 52 65 73 20 3d 20 30 3b 0a 0a   int bRes = 0;..
397bb 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52    nodeGetCell(pR
397bc 74 72 65 65 2c 20 70 43 75 72 73 6f 72 2d 3e 70  tree, pCursor->p
397bd 4e 6f 64 65 2c 20 70 43 75 72 73 6f 72 2d 3e 69  Node, pCursor->i
397be 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20  Cell, &cell);.  
397bf 66 6f 72 28 69 69 3d 30 3b 20 62 52 65 73 3d 3d  for(ii=0; bRes==
397c0 30 20 26 26 20 69 69 3c 70 43 75 72 73 6f 72 2d  0 && ii<pCursor-
397c1 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 69  >nConstraint; ii
397c2 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65 65 43 6f  ++){.    RtreeCo
397c3 6e 73 74 72 61 69 6e 74 20 2a 70 20 3d 20 26 70  nstraint *p = &p
397c4 43 75 72 73 6f 72 2d 3e 61 43 6f 6e 73 74 72 61  Cursor->aConstra
397c5 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 64 6f 75  int[ii];.    dou
397c6 62 6c 65 20 63 65 6c 6c 5f 6d 69 6e 20 3d 20 44  ble cell_min = D
397c7 43 4f 4f 52 44 28 63 65 6c 6c 2e 61 43 6f 6f 72  COORD(cell.aCoor
397c8 64 5b 28 70 2d 3e 69 43 6f 6f 72 64 3e 3e 31 29  d[(p->iCoord>>1)
397c9 2a 32 5d 29 3b 0a 20 20 20 20 64 6f 75 62 6c 65  *2]);.    double
397ca 20 63 65 6c 6c 5f 6d 61 78 20 3d 20 44 43 4f 4f   cell_max = DCOO
397cb 52 44 28 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 28  RD(cell.aCoord[(
397cc 70 2d 3e 69 43 6f 6f 72 64 3e 3e 31 29 2a 32 2b  p->iCoord>>1)*2+
397cd 31 5d 29 3b 0a 0a 20 20 20 20 61 73 73 65 72 74  1]);..    assert
397ce 28 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c 45  (p->op==RTREE_LE
397cf 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45   || p->op==RTREE
397d0 5f 4c 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54  _LT || p->op==RT
397d1 52 45 45 5f 47 45 20 0a 20 20 20 20 20 20 20 20  REE_GE .        
397d2 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f  || p->op==RTREE_
397d3 47 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52  GT || p->op==RTR
397d4 45 45 5f 45 51 0a 20 20 20 20 29 3b 0a 0a 20 20  EE_EQ.    );..  
397d5 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20    switch( p->op 
397d6 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 52 54  ){.      case RT
397d7 52 45 45 5f 4c 45 3a 20 63 61 73 65 20 52 54 52  REE_LE: case RTR
397d8 45 45 5f 4c 54 3a 20 62 52 65 73 20 3d 20 70 2d  EE_LT: bRes = p-
397d9 3e 72 56 61 6c 75 65 3c 63 65 6c 6c 5f 6d 69 6e  >rValue<cell_min
397da 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63  ; break;.      c
397db 61 73 65 20 52 54 52 45 45 5f 47 45 3a 20 63 61  ase RTREE_GE: ca
397dc 73 65 20 52 54 52 45 45 5f 47 54 3a 20 62 52 65  se RTREE_GT: bRe
397dd 73 20 3d 20 70 2d 3e 72 56 61 6c 75 65 3e 63 65  s = p->rValue>ce
397de 6c 6c 5f 6d 61 78 3b 20 62 72 65 61 6b 3b 0a 20  ll_max; break;. 
397df 20 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f       case RTREE_
397e0 45 51 3a 20 0a 20 20 20 20 20 20 20 20 62 52 65  EQ: .        bRe
397e1 73 20 3d 20 28 70 2d 3e 72 56 61 6c 75 65 3e 63  s = (p->rValue>c
397e2 65 6c 6c 5f 6d 61 78 20 7c 7c 20 70 2d 3e 72 56  ell_max || p->rV
397e3 61 6c 75 65 3c 63 65 6c 6c 5f 6d 69 6e 29 3b 0a  alue<cell_min);.
397e4 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
397e5 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
397e6 72 6e 20 62 52 65 73 3b 0a 7d 0a 0a 2f 2a 20 0a  rn bRes;.}../* .
397e7 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
397e8 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20  f the cell that 
397e9 63 75 72 73 6f 72 20 70 43 75 72 73 6f 72 20 63  cursor pCursor c
397ea 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20  urrently points 
397eb 74 6f 0a 2a 2a 20 77 6f 75 6c 64 20 62 65 20 66  to.** would be f
397ec 69 6c 74 65 72 65 64 20 28 65 78 63 6c 75 64 65  iltered (exclude
397ed 64 29 20 62 79 20 74 68 65 20 63 6f 6e 73 74 72  d) by the constr
397ee 61 69 6e 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a  aints in the .**
397ef 20 70 43 75 72 73 6f 72 2d 3e 61 43 6f 6e 73 74   pCursor->aConst
397f0 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 2c 20 6f  raint[] array, o
397f1 72 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69 73  r false otherwis
397f2 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  e..**.** This fu
397f3 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74  nction assumes t
397f4 68 61 74 20 74 68 65 20 63 65 6c 6c 20 69 73 20  hat the cell is 
397f5 70 61 72 74 20 6f 66 20 61 20 6c 65 61 66 20 6e  part of a leaf n
397f6 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ode..*/.static i
397f7 6e 74 20 74 65 73 74 52 74 72 65 65 45 6e 74 72  nt testRtreeEntr
397f8 79 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  y(Rtree *pRtree,
397f9 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43   RtreeCursor *pC
397fa 75 72 73 6f 72 29 7b 0a 20 20 52 74 72 65 65 43  ursor){.  RtreeC
397fb 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 69 6e 74 20  ell cell;.  int 
397fc 69 69 3b 0a 0a 20 20 6e 6f 64 65 47 65 74 43 65  ii;..  nodeGetCe
397fd 6c 6c 28 70 52 74 72 65 65 2c 20 70 43 75 72 73  ll(pRtree, pCurs
397fe 6f 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 75 72 73  or->pNode, pCurs
397ff 6f 72 2d 3e 69 43 65 6c 6c 2c 20 26 63 65 6c 6c  or->iCell, &cell
39800 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  );.  for(ii=0; i
39801 69 3c 70 43 75 72 73 6f 72 2d 3e 6e 43 6f 6e 73  i<pCursor->nCons
39802 74 72 61 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20  traint; ii++){. 
39803 20 20 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69     RtreeConstrai
39804 6e 74 20 2a 70 20 3d 20 26 70 43 75 72 73 6f 72  nt *p = &pCursor
39805 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69  ->aConstraint[ii
39806 5d 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 63 6f  ];.    double co
39807 6f 72 64 20 3d 20 44 43 4f 4f 52 44 28 63 65 6c  ord = DCOORD(cel
39808 6c 2e 61 43 6f 6f 72 64 5b 70 2d 3e 69 43 6f 6f  l.aCoord[p->iCoo
39809 72 64 5d 29 3b 0a 20 20 20 20 69 6e 74 20 72 65  rd]);.    int re
3980a 73 3b 0a 20 20 20 20 61 73 73 65 72 74 28 70 2d  s;.    assert(p-
3980b 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c 45 20 7c 7c  >op==RTREE_LE ||
3980c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c 54   p->op==RTREE_LT
3980d 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45   || p->op==RTREE
3980e 5f 47 45 20 0a 20 20 20 20 20 20 20 20 7c 7c 20  _GE .        || 
3980f 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 47 54 20  p->op==RTREE_GT 
39810 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f  || p->op==RTREE_
39811 45 51 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 77  EQ.    );.    sw
39812 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20  itch( p->op ){. 
39813 20 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f       case RTREE_
39814 4c 45 3a 20 72 65 73 20 3d 20 28 63 6f 6f 72 64  LE: res = (coord
39815 3c 3d 70 2d 3e 72 56 61 6c 75 65 29 3b 20 62 72  <=p->rValue); br
39816 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
39817 52 54 52 45 45 5f 4c 54 3a 20 72 65 73 20 3d 20  RTREE_LT: res = 
39818 28 63 6f 6f 72 64 3c 70 2d 3e 72 56 61 6c 75 65  (coord<p->rValue
39819 29 3b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  );  break;.     
3981a 20 63 61 73 65 20 52 54 52 45 45 5f 47 45 3a 20   case RTREE_GE: 
3981b 72 65 73 20 3d 20 28 63 6f 6f 72 64 3e 3d 70 2d  res = (coord>=p-
3981c 3e 72 56 61 6c 75 65 29 3b 20 62 72 65 61 6b 3b  >rValue); break;
3981d 0a 20 20 20 20 20 20 63 61 73 65 20 52 54 52 45  .      case RTRE
3981e 45 5f 47 54 3a 20 72 65 73 20 3d 20 28 63 6f 6f  E_GT: res = (coo
3981f 72 64 3e 70 2d 3e 72 56 61 6c 75 65 29 3b 20 20  rd>p->rValue);  
39820 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
39821 65 20 52 54 52 45 45 5f 45 51 3a 20 72 65 73 20  e RTREE_EQ: res 
39822 3d 20 28 63 6f 6f 72 64 3d 3d 70 2d 3e 72 56 61  = (coord==p->rVa
39823 6c 75 65 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20  lue); break;.   
39824 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 72 65 73   }..    if( !res
39825 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d   ) return 1;.  }
39826 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ..  return 0;.}.
39827 0a 2f 2a 0a 2a 2a 20 43 75 72 73 6f 72 20 70 43  ./*.** Cursor pC
39828 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20  ursor currently 
39829 70 6f 69 6e 74 73 20 61 74 20 61 20 6e 6f 64 65  points at a node
3982a 20 74 68 61 74 20 68 65 61 64 73 20 61 20 73 75   that heads a su
3982b 62 2d 74 72 65 65 20 6f 66 0a 2a 2a 20 68 65 69  b-tree of.** hei
3982c 67 68 74 20 69 48 65 69 67 68 74 20 28 69 66 20  ght iHeight (if 
3982d 69 48 65 69 67 68 74 3d 3d 30 2c 20 74 68 65 6e  iHeight==0, then
3982e 20 74 68 65 20 6e 6f 64 65 20 69 73 20 61 20 6c   the node is a l
3982f 65 61 66 29 2e 20 44 65 73 63 65 6e 64 0a 2a 2a  eaf). Descend.**
39830 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
39831 20 6c 65 66 74 2d 6d 6f 73 74 20 63 65 6c 6c 20   left-most cell 
39832 6f 66 20 74 68 65 20 73 75 62 2d 74 72 65 65 20  of the sub-tree 
39833 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65  that matches the
39834 20 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 65 64 20   .** configured 
39835 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 2a 2f 0a  constraints..*/.
39836 73 74 61 74 69 63 20 69 6e 74 20 64 65 73 63 65  static int desce
39837 6e 64 54 6f 43 65 6c 6c 28 0a 20 20 52 74 72 65  ndToCell(.  Rtre
39838 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74  e *pRtree, .  Rt
39839 72 65 65 43 75 72 73 6f 72 20 2a 70 43 75 72 73  reeCursor *pCurs
3983a 6f 72 2c 20 0a 20 20 69 6e 74 20 69 48 65 69 67  or, .  int iHeig
3983b 68 74 2c 0a 20 20 69 6e 74 20 2a 70 45 6f 66 20  ht,.  int *pEof 
3983c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3983d 2f 2a 20 4f 55 54 3a 20 53 65 74 20 74 6f 20 74  /* OUT: Set to t
3983e 72 75 65 20 69 66 20 63 61 6e 6e 6f 74 20 64 65  rue if cannot de
3983f 73 63 65 6e 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e  scend */.){.  in
39840 74 20 69 73 45 6f 66 3b 0a 20 20 69 6e 74 20 72  t isEof;.  int r
39841 63 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 52  c;.  int ii;.  R
39842 74 72 65 65 4e 6f 64 65 20 2a 70 43 68 69 6c 64  treeNode *pChild
39843 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ;.  sqlite3_int6
39844 34 20 69 52 6f 77 69 64 3b 0a 0a 20 20 52 74 72  4 iRowid;..  Rtr
39845 65 65 4e 6f 64 65 20 2a 70 53 61 76 65 64 4e 6f  eeNode *pSavedNo
39846 64 65 20 3d 20 70 43 75 72 73 6f 72 2d 3e 70 4e  de = pCursor->pN
39847 6f 64 65 3b 0a 20 20 69 6e 74 20 69 53 61 76 65  ode;.  int iSave
39848 64 43 65 6c 6c 20 3d 20 70 43 75 72 73 6f 72 2d  dCell = pCursor-
39849 3e 69 43 65 6c 6c 3b 0a 0a 20 20 61 73 73 65 72  >iCell;..  asser
3984a 74 28 20 69 48 65 69 67 68 74 3e 3d 30 20 29 3b  t( iHeight>=0 );
3984b 0a 0a 20 20 69 66 28 20 69 48 65 69 67 68 74 3d  ..  if( iHeight=
3984c 3d 30 20 29 7b 0a 20 20 20 20 69 73 45 6f 66 20  =0 ){.    isEof 
3984d 3d 20 74 65 73 74 52 74 72 65 65 45 6e 74 72 79  = testRtreeEntry
3984e 28 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72  (pRtree, pCursor
3984f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
39850 69 73 45 6f 66 20 3d 20 74 65 73 74 52 74 72 65  isEof = testRtre
39851 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 43  eCell(pRtree, pC
39852 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66  ursor);.  }.  if
39853 28 20 69 73 45 6f 66 20 7c 7c 20 69 48 65 69 67  ( isEof || iHeig
39854 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 45  ht==0 ){.    *pE
39855 6f 66 20 3d 20 69 73 45 6f 66 3b 0a 20 20 20 20  of = isEof;.    
39856 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
39857 3b 0a 20 20 7d 0a 0a 20 20 69 52 6f 77 69 64 20  ;.  }..  iRowid 
39858 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70  = nodeGetRowid(p
39859 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72 2d 3e  Rtree, pCursor->
3985a 70 4e 6f 64 65 2c 20 70 43 75 72 73 6f 72 2d 3e  pNode, pCursor->
3985b 69 43 65 6c 6c 29 3b 0a 20 20 72 63 20 3d 20 6e  iCell);.  rc = n
3985c 6f 64 65 41 63 71 75 69 72 65 28 70 52 74 72 65  odeAcquire(pRtre
3985d 65 2c 20 69 52 6f 77 69 64 2c 20 70 43 75 72 73  e, iRowid, pCurs
3985e 6f 72 2d 3e 70 4e 6f 64 65 2c 20 26 70 43 68 69  or->pNode, &pChi
3985f 6c 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  ld);.  if( rc!=S
39860 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
39861 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
39862 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52    nodeRelease(pR
39863 74 72 65 65 2c 20 70 43 75 72 73 6f 72 2d 3e 70  tree, pCursor->p
39864 4e 6f 64 65 29 3b 0a 20 20 70 43 75 72 73 6f 72  Node);.  pCursor
39865 2d 3e 70 4e 6f 64 65 20 3d 20 70 43 68 69 6c 64  ->pNode = pChild
39866 3b 0a 20 20 69 73 45 6f 66 20 3d 20 31 3b 0a 20  ;.  isEof = 1;. 
39867 20 66 6f 72 28 69 69 3d 30 3b 20 69 73 45 6f 66   for(ii=0; isEof
39868 20 26 26 20 69 69 3c 4e 43 45 4c 4c 28 70 43 68   && ii<NCELL(pCh
39869 69 6c 64 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  ild); ii++){.   
3986a 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 20   pCursor->iCell 
3986b 3d 20 69 69 3b 0a 20 20 20 20 72 63 20 3d 20 64  = ii;.    rc = d
3986c 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28 70 52 74  escendToCell(pRt
3986d 72 65 65 2c 20 70 43 75 72 73 6f 72 2c 20 69 48  ree, pCursor, iH
3986e 65 69 67 68 74 2d 31 2c 20 26 69 73 45 6f 66 29  eight-1, &isEof)
3986f 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
39870 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
39871 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
39872 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 73 45  }.  }..  if( isE
39873 6f 66 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  of ){.    assert
39874 28 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65  ( pCursor->pNode
39875 3d 3d 70 43 68 69 6c 64 20 29 3b 0a 20 20 20 20  ==pChild );.    
39876 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70 53  nodeReference(pS
39877 61 76 65 64 4e 6f 64 65 29 3b 0a 20 20 20 20 6e  avedNode);.    n
39878 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
39879 65 2c 20 70 43 68 69 6c 64 29 3b 0a 20 20 20 20  e, pChild);.    
3987a 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 20 3d  pCursor->pNode =
3987b 20 70 53 61 76 65 64 4e 6f 64 65 3b 0a 20 20 20   pSavedNode;.   
3987c 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 20   pCursor->iCell 
3987d 3d 20 69 53 61 76 65 64 43 65 6c 6c 3b 0a 20 20  = iSavedCell;.  
3987e 7d 0a 0a 20 20 2a 70 45 6f 66 20 3d 20 69 73 45  }..  *pEof = isE
3987f 6f 66 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  of;.  return SQL
39880 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
39881 20 4f 6e 65 20 6f 66 20 74 68 65 20 63 65 6c 6c   One of the cell
39882 73 20 69 6e 20 6e 6f 64 65 20 70 4e 6f 64 65 20  s in node pNode 
39883 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  is guaranteed to
39884 20 68 61 76 65 20 61 20 36 34 2d 62 69 74 20 0a   have a 64-bit .
39885 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  ** integer value
39886 20 65 71 75 61 6c 20 74 6f 20 69 52 6f 77 69 64   equal to iRowid
39887 2e 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64  . Return the ind
39888 65 78 20 6f 66 20 74 68 69 73 20 63 65 6c 6c 2e  ex of this cell.
39889 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  .*/.static int n
3988a 6f 64 65 52 6f 77 69 64 49 6e 64 65 78 28 52 74  odeRowidIndex(Rt
3988b 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
3988c 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 69  eeNode *pNode, i
3988d 36 34 20 69 52 6f 77 69 64 29 7b 0a 20 20 69 6e  64 iRowid){.  in
3988e 74 20 69 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30  t ii;.  for(ii=0
3988f 3b 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70  ; nodeGetRowid(p
39890 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69  Rtree, pNode, ii
39891 29 21 3d 69 52 6f 77 69 64 3b 20 69 69 2b 2b 29  )!=iRowid; ii++)
39892 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 69  {.    assert( ii
39893 3c 28 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 2d 31  <(NCELL(pNode)-1
39894 29 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ) );.  }.  retur
39895 6e 20 69 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n ii;.}../*.** R
39896 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20  eturn the index 
39897 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  of the cell cont
39898 61 69 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65 72  aining a pointer
39899 20 74 6f 20 6e 6f 64 65 20 70 4e 6f 64 65 0a 2a   to node pNode.*
3989a 2a 20 69 6e 20 69 74 73 20 70 61 72 65 6e 74 2e  * in its parent.
3989b 20 49 66 20 70 4e 6f 64 65 20 69 73 20 74 68 65   If pNode is the
3989c 20 72 6f 6f 74 20 6e 6f 64 65 2c 20 72 65 74 75   root node, retu
3989d 72 6e 20 2d 31 2e 0a 2a 2f 0a 73 74 61 74 69 63  rn -1..*/.static
3989e 20 69 6e 74 20 6e 6f 64 65 50 61 72 65 6e 74 49   int nodeParentI
3989f 6e 64 65 78 28 52 74 72 65 65 20 2a 70 52 74 72  ndex(Rtree *pRtr
398a0 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ee, RtreeNode *p
398a1 4e 6f 64 65 29 7b 0a 20 20 52 74 72 65 65 4e 6f  Node){.  RtreeNo
398a2 64 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 4e  de *pParent = pN
398a3 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20  ode->pParent;.  
398a4 69 66 28 20 70 50 61 72 65 6e 74 20 29 7b 0a 20  if( pParent ){. 
398a5 20 20 20 72 65 74 75 72 6e 20 6e 6f 64 65 52 6f     return nodeRo
398a6 77 69 64 49 6e 64 65 78 28 70 52 74 72 65 65 2c  widIndex(pRtree,
398a7 20 70 50 61 72 65 6e 74 2c 20 70 4e 6f 64 65 2d   pParent, pNode-
398a8 3e 69 4e 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 72  >iNode);.  }.  r
398a9 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a 20  eturn -1;.}../* 
398aa 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61  .** Rtree virtua
398ab 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
398ac 4e 65 78 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a  Next method..*/.
398ad 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
398ae 4e 65 78 74 28 73 71 6c 69 74 65 33 5f 76 74 61  Next(sqlite3_vta
398af 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43  b_cursor *pVtabC
398b0 75 72 73 6f 72 29 7b 0a 20 20 52 74 72 65 65 20  ursor){.  Rtree 
398b1 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65  *pRtree = (Rtree
398b2 20 2a 29 28 70 56 74 61 62 43 75 72 73 6f 72 2d   *)(pVtabCursor-
398b3 3e 70 56 74 61 62 29 3b 0a 20 20 52 74 72 65 65  >pVtab);.  Rtree
398b4 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28  Cursor *pCsr = (
398b5 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29 70 56  RtreeCursor *)pV
398b6 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 69 6e 74  tabCursor;.  int
398b7 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
398b8 0a 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 69 53  ..  if( pCsr->iS
398b9 74 72 61 74 65 67 79 3d 3d 31 20 29 7b 0a 20 20  trategy==1 ){.  
398ba 20 20 2f 2a 20 54 68 69 73 20 22 73 63 61 6e 22    /* This "scan"
398bb 20 69 73 20 61 20 64 69 72 65 63 74 20 6c 6f 6f   is a direct loo
398bc 6b 75 70 20 62 79 20 72 6f 77 69 64 2e 20 54 68  kup by rowid. Th
398bd 65 72 65 20 69 73 20 6e 6f 20 6e 65 78 74 20 65  ere is no next e
398be 6e 74 72 79 2e 20 2a 2f 0a 20 20 20 20 6e 6f 64  ntry. */.    nod
398bf 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
398c0 20 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20   pCsr->pNode);. 
398c1 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d     pCsr->pNode =
398c2 20 30 3b 0a 20 20 7d 0a 0a 20 20 65 6c 73 65 20   0;.  }..  else 
398c3 69 66 28 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20  if( pCsr->pNode 
398c4 29 7b 0a 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74  ){.    /* Move t
398c5 6f 20 74 68 65 20 6e 65 78 74 20 65 6e 74 72 79  o the next entry
398c6 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 68   that matches th
398c7 65 20 63 6f 6e 66 69 67 75 72 65 64 20 63 6f 6e  e configured con
398c8 73 74 72 61 69 6e 74 73 2e 20 2a 2f 0a 20 20 20  straints. */.   
398c9 20 69 6e 74 20 69 48 65 69 67 68 74 20 3d 20 30   int iHeight = 0
398ca 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43 73  ;.    while( pCs
398cb 72 2d 3e 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20  r->pNode ){.    
398cc 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
398cd 64 65 20 3d 20 70 43 73 72 2d 3e 70 4e 6f 64 65  de = pCsr->pNode
398ce 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 43 65 6c  ;.      int nCel
398cf 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29  l = NCELL(pNode)
398d0 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 43 73 72  ;.      for(pCsr
398d1 2d 3e 69 43 65 6c 6c 2b 2b 3b 20 70 43 73 72 2d  ->iCell++; pCsr-
398d2 3e 69 43 65 6c 6c 3c 6e 43 65 6c 6c 3b 20 70 43  >iCell<nCell; pC
398d3 73 72 2d 3e 69 43 65 6c 6c 2b 2b 29 7b 0a 20 20  sr->iCell++){.  
398d4 20 20 20 20 20 20 69 6e 74 20 69 73 45 6f 66 3b        int isEof;
398d5 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 64 65  .        rc = de
398d6 73 63 65 6e 64 54 6f 43 65 6c 6c 28 70 52 74 72  scendToCell(pRtr
398d7 65 65 2c 20 70 43 73 72 2c 20 69 48 65 69 67 68  ee, pCsr, iHeigh
398d8 74 2c 20 26 69 73 45 6f 66 29 3b 0a 20 20 20 20  t, &isEof);.    
398d9 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
398da 54 45 5f 4f 4b 20 7c 7c 20 21 69 73 45 6f 66 20  TE_OK || !isEof 
398db 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  ){.          ret
398dc 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20  urn rc;.        
398dd 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
398de 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 4e  pCsr->pNode = pN
398df 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20  ode->pParent;.  
398e0 20 20 20 20 70 43 73 72 2d 3e 69 43 65 6c 6c 20      pCsr->iCell 
398e1 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64 65  = nodeParentInde
398e2 78 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29  x(pRtree, pNode)
398e3 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 66 65  ;.      nodeRefe
398e4 72 65 6e 63 65 28 70 43 73 72 2d 3e 70 4e 6f 64  rence(pCsr->pNod
398e5 65 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65  e);.      nodeRe
398e6 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4e  lease(pRtree, pN
398e7 6f 64 65 29 3b 0a 20 20 20 20 20 20 69 48 65 69  ode);.      iHei
398e8 67 68 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  ght++;.    }.  }
398e9 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ..  return rc;.}
398ea 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76  ../* .** Rtree v
398eb 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
398ec 75 6c 65 20 78 52 6f 77 69 64 20 6d 65 74 68 6f  ule xRowid metho
398ed 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
398ee 20 72 74 72 65 65 52 6f 77 69 64 28 73 71 6c 69   rtreeRowid(sqli
398ef 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
398f0 2a 70 56 74 61 62 43 75 72 73 6f 72 2c 20 73 71  *pVtabCursor, sq
398f1 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 52 6f 77  lite_int64 *pRow
398f2 69 64 29 7b 0a 20 20 52 74 72 65 65 20 2a 70 52  id){.  Rtree *pR
398f3 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29  tree = (Rtree *)
398f4 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74  pVtabCursor->pVt
398f5 61 62 3b 0a 20 20 52 74 72 65 65 43 75 72 73 6f  ab;.  RtreeCurso
398f6 72 20 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65  r *pCsr = (Rtree
398f7 43 75 72 73 6f 72 20 2a 29 70 56 74 61 62 43 75  Cursor *)pVtabCu
398f8 72 73 6f 72 3b 0a 0a 20 20 61 73 73 65 72 74 28  rsor;..  assert(
398f9 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20  pCsr->pNode);.  
398fa 2a 70 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65  *pRowid = nodeGe
398fb 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70  tRowid(pRtree, p
398fc 43 73 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 73 72  Csr->pNode, pCsr
398fd 2d 3e 69 43 65 6c 6c 29 3b 0a 0a 20 20 72 65 74  ->iCell);..  ret
398fe 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
398ff 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76  ../* .** Rtree v
39900 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
39901 75 6c 65 20 78 43 6f 6c 75 6d 6e 20 6d 65 74 68  ule xColumn meth
39902 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  od..*/.static in
39903 74 20 72 74 72 65 65 43 6f 6c 75 6d 6e 28 73 71  t rtreeColumn(sq
39904 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
39905 72 20 2a 63 75 72 2c 20 73 71 6c 69 74 65 33 5f  r *cur, sqlite3_
39906 63 6f 6e 74 65 78 74 20 2a 63 74 78 2c 20 69 6e  context *ctx, in
39907 74 20 69 29 7b 0a 20 20 52 74 72 65 65 20 2a 70  t i){.  Rtree *p
39908 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a  Rtree = (Rtree *
39909 29 63 75 72 2d 3e 70 56 74 61 62 3b 0a 20 20 52  )cur->pVtab;.  R
3990a 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72  treeCursor *pCsr
3990b 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72 20   = (RtreeCursor 
3990c 2a 29 63 75 72 3b 0a 0a 20 20 69 66 28 20 69 3d  *)cur;..  if( i=
3990d 3d 30 20 29 7b 0a 20 20 20 20 69 36 34 20 69 52  =0 ){.    i64 iR
3990e 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f  owid = nodeGetRo
3990f 77 69 64 28 70 52 74 72 65 65 2c 20 70 43 73 72  wid(pRtree, pCsr
39910 2d 3e 70 4e 6f 64 65 2c 20 70 43 73 72 2d 3e 69  ->pNode, pCsr->i
39911 43 65 6c 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74  Cell);.    sqlit
39912 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28  e3_result_int64(
39913 63 74 78 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20  ctx, iRowid);.  
39914 7d 65 6c 73 65 7b 0a 20 20 20 20 52 74 72 65 65  }else{.    Rtree
39915 43 6f 6f 72 64 20 63 3b 0a 20 20 20 20 6e 6f 64  Coord c;.    nod
39916 65 47 65 74 43 6f 6f 72 64 28 70 52 74 72 65 65  eGetCoord(pRtree
39917 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65 2c 20 70  , pCsr->pNode, p
39918 43 73 72 2d 3e 69 43 65 6c 6c 2c 20 69 2d 31 2c  Csr->iCell, i-1,
39919 20 26 63 29 3b 0a 20 20 20 20 69 66 28 20 70 52   &c);.    if( pR
3991a 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65  tree->eCoordType
3991b 3d 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45  ==RTREE_COORD_RE
3991c 41 4c 33 32 20 29 7b 0a 20 20 20 20 20 20 73 71  AL32 ){.      sq
3991d 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
3991e 62 6c 65 28 63 74 78 2c 20 63 2e 66 29 3b 0a 20  ble(ctx, c.f);. 
3991f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
39920 61 73 73 65 72 74 28 20 70 52 74 72 65 65 2d 3e  assert( pRtree->
39921 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45  eCoordType==RTRE
39922 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32 20 29 3b  E_COORD_INT32 );
39923 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
39924 65 73 75 6c 74 5f 69 6e 74 28 63 74 78 2c 20 63  esult_int(ctx, c
39925 2e 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  .i);.    }.  }..
39926 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
39927 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 55 73  OK;.}../* .** Us
39928 65 20 6e 6f 64 65 41 63 71 75 69 72 65 28 29 20  e nodeAcquire() 
39929 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 65  to obtain the le
3992a 61 66 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 69  af node containi
3992b 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 77 69  ng the record wi
3992c 74 68 20 0a 2a 2a 20 72 6f 77 69 64 20 69 52 6f  th .** rowid iRo
3992d 77 69 64 2e 20 49 66 20 73 75 63 63 65 73 73 66  wid. If successf
3992e 75 6c 2c 20 73 65 74 20 2a 70 70 4c 65 61 66 20  ul, set *ppLeaf 
3992f 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  to point to the 
39930 6e 6f 64 65 20 61 6e 64 0a 2a 2a 20 72 65 74 75  node and.** retu
39931 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66  rn SQLITE_OK. If
39932 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63   there is no suc
39933 68 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65 20  h record in the 
39934 74 61 62 6c 65 2c 20 73 65 74 0a 2a 2a 20 2a 70  table, set.** *p
39935 70 4c 65 61 66 20 74 6f 20 30 20 61 6e 64 20 72  pLeaf to 0 and r
39936 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
39937 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
39938 75 72 73 2c 20 73 65 74 20 2a 70 70 4c 65 61 66  urs, set *ppLeaf
39939 0a 2a 2a 20 74 6f 20 7a 65 72 6f 20 61 6e 64 20  .** to zero and 
3993a 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 69 74 65  return an SQLite
3993b 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a   error code..*/.
3993c 73 74 61 74 69 63 20 69 6e 74 20 66 69 6e 64 4c  static int findL
3993d 65 61 66 4e 6f 64 65 28 52 74 72 65 65 20 2a 70  eafNode(Rtree *p
3993e 52 74 72 65 65 2c 20 69 36 34 20 69 52 6f 77 69  Rtree, i64 iRowi
3993f 64 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a 70  d, RtreeNode **p
39940 70 4c 65 61 66 29 7b 0a 20 20 69 6e 74 20 72 63  pLeaf){.  int rc
39941 3b 0a 20 20 2a 70 70 4c 65 61 66 20 3d 20 30 3b  ;.  *ppLeaf = 0;
39942 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
39943 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52  int64(pRtree->pR
39944 65 61 64 52 6f 77 69 64 2c 20 31 2c 20 69 52 6f  eadRowid, 1, iRo
39945 77 69 64 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  wid);.  if( sqli
39946 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d  te3_step(pRtree-
39947 3e 70 52 65 61 64 52 6f 77 69 64 29 3d 3d 53 51  >pReadRowid)==SQ
39948 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20  LITE_ROW ){.    
39949 69 36 34 20 69 4e 6f 64 65 20 3d 20 73 71 6c 69  i64 iNode = sqli
3994a 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
3994b 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f  (pRtree->pReadRo
3994c 77 69 64 2c 20 30 29 3b 0a 20 20 20 20 72 63 20  wid, 0);.    rc 
3994d 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52  = nodeAcquire(pR
3994e 74 72 65 65 2c 20 69 4e 6f 64 65 2c 20 30 2c 20  tree, iNode, 0, 
3994f 70 70 4c 65 61 66 29 3b 0a 20 20 20 20 73 71 6c  ppLeaf);.    sql
39950 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65  ite3_reset(pRtre
39951 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3b 0a  e->pReadRowid);.
39952 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
39953 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
39954 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77  pRtree->pReadRow
39955 69 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  id);.  }.  retur
39956 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 20 0a 2a 2a  n rc;.}.../* .**
39957 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74   Rtree virtual t
39958 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 46 69 6c  able module xFil
39959 74 65 72 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  ter method..*/.s
3995a 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 46  tatic int rtreeF
3995b 69 6c 74 65 72 28 0a 20 20 73 71 6c 69 74 65 33  ilter(.  sqlite3
3995c 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56  _vtab_cursor *pV
3995d 74 61 62 43 75 72 73 6f 72 2c 20 0a 20 20 69 6e  tabCursor, .  in
3995e 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e 73 74 20  t idxNum, const 
3995f 63 68 61 72 20 2a 69 64 78 53 74 72 2c 0a 20 20  char *idxStr,.  
39960 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
39961 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
39962 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  {.  Rtree *pRtre
39963 65 20 3d 20 28 52 74 72 65 65 20 2a 29 70 56 74  e = (Rtree *)pVt
39964 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b  abCursor->pVtab;
39965 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a  .  RtreeCursor *
39966 70 43 73 72 20 3d 20 28 52 74 72 65 65 43 75 72  pCsr = (RtreeCur
39967 73 6f 72 20 2a 29 70 56 74 61 62 43 75 72 73 6f  sor *)pVtabCurso
39968 72 3b 0a 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  r;..  RtreeNode 
39969 2a 70 52 6f 6f 74 20 3d 20 30 3b 0a 20 20 69 6e  *pRoot = 0;.  in
3996a 74 20 69 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d  t ii;.  int rc =
3996b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 72   SQLITE_OK;..  r
3996c 74 72 65 65 52 65 66 65 72 65 6e 63 65 28 70 52  treeReference(pR
3996d 74 72 65 65 29 3b 0a 0a 20 20 73 71 6c 69 74 65  tree);..  sqlite
3996e 33 5f 66 72 65 65 28 70 43 73 72 2d 3e 61 43 6f  3_free(pCsr->aCo
3996f 6e 73 74 72 61 69 6e 74 29 3b 0a 20 20 70 43 73  nstraint);.  pCs
39970 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d  r->aConstraint =
39971 20 30 3b 0a 20 20 70 43 73 72 2d 3e 69 53 74 72   0;.  pCsr->iStr
39972 61 74 65 67 79 20 3d 20 69 64 78 4e 75 6d 3b 0a  ategy = idxNum;.
39973 0a 20 20 69 66 28 20 69 64 78 4e 75 6d 3d 3d 31  .  if( idxNum==1
39974 20 29 7b 0a 20 20 20 20 2f 2a 20 53 70 65 63 69   ){.    /* Speci
39975 61 6c 20 63 61 73 65 20 2d 20 6c 6f 6f 6b 75 70  al case - lookup
39976 20 62 79 20 72 6f 77 69 64 2e 20 2a 2f 0a 20 20   by rowid. */.  
39977 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65    RtreeNode *pLe
39978 61 66 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 65  af;        /* Le
39979 61 66 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20  af on which the 
3997a 72 65 71 75 69 72 65 64 20 63 65 6c 6c 20 72 65  required cell re
3997b 73 69 64 65 73 20 2a 2f 0a 20 20 20 20 69 36 34  sides */.    i64
3997c 20 69 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65   iRowid = sqlite
3997d 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72  3_value_int64(ar
3997e 67 76 5b 30 5d 29 3b 0a 20 20 20 20 72 63 20 3d  gv[0]);.    rc =
3997f 20 66 69 6e 64 4c 65 61 66 4e 6f 64 65 28 70 52   findLeafNode(pR
39980 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 26 70  tree, iRowid, &p
39981 4c 65 61 66 29 3b 0a 20 20 20 20 70 43 73 72 2d  Leaf);.    pCsr-
39982 3e 70 4e 6f 64 65 20 3d 20 70 4c 65 61 66 3b 20  >pNode = pLeaf; 
39983 0a 20 20 20 20 69 66 28 20 70 4c 65 61 66 20 26  .    if( pLeaf &
39984 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  & rc==SQLITE_OK 
39985 29 7b 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 69  ){.      pCsr->i
39986 43 65 6c 6c 20 3d 20 6e 6f 64 65 52 6f 77 69 64  Cell = nodeRowid
39987 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70 4c  Index(pRtree, pL
39988 65 61 66 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20  eaf, iRowid);.  
39989 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
3998a 20 2f 2a 20 4e 6f 72 6d 61 6c 20 63 61 73 65 20   /* Normal case 
3998b 2d 20 72 2d 74 72 65 65 20 73 63 61 6e 2e 20 53  - r-tree scan. S
3998c 65 74 20 75 70 20 74 68 65 20 52 74 72 65 65 43  et up the RtreeC
3998d 75 72 73 6f 72 2e 61 43 6f 6e 73 74 72 61 69 6e  ursor.aConstrain
3998e 74 20 61 72 72 61 79 20 0a 20 20 20 20 2a 2a 20  t array .    ** 
3998f 77 69 74 68 20 74 68 65 20 63 6f 6e 66 69 67 75  with the configu
39990 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e  red constraints.
39991 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28   .    */.    if(
39992 20 61 72 67 63 3e 30 20 29 7b 0a 20 20 20 20 20   argc>0 ){.     
39993 20 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69   pCsr->aConstrai
39994 6e 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  nt = sqlite3_mal
39995 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65  loc(sizeof(Rtree
39996 43 6f 6e 73 74 72 61 69 6e 74 29 2a 61 72 67 63  Constraint)*argc
39997 29 3b 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 6e  );.      pCsr->n
39998 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 61 72 67  Constraint = arg
39999 63 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 43  c;.      if( !pC
3999a 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20  sr->aConstraint 
3999b 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
3999c 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
3999d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3999e 20 20 20 61 73 73 65 72 74 28 20 28 69 64 78 53     assert( (idxS
3999f 74 72 3d 3d 30 20 26 26 20 61 72 67 63 3d 3d 30  tr==0 && argc==0
399a0 29 20 7c 7c 20 73 74 72 6c 65 6e 28 69 64 78 53  ) || strlen(idxS
399a1 74 72 29 3d 3d 61 72 67 63 2a 32 20 29 3b 0a 20  tr)==argc*2 );. 
399a2 20 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b         for(ii=0;
399a3 20 69 69 3c 61 72 67 63 3b 20 69 69 2b 2b 29 7b   ii<argc; ii++){
399a4 0a 20 20 20 20 20 20 20 20 20 20 52 74 72 65 65  .          Rtree
399a5 43 6f 6e 73 74 72 61 69 6e 74 20 2a 70 20 3d 20  Constraint *p = 
399a6 26 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69  &pCsr->aConstrai
399a7 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 20 20 20 20  nt[ii];.        
399a8 20 20 70 2d 3e 6f 70 20 3d 20 69 64 78 53 74 72    p->op = idxStr
399a9 5b 69 69 2a 32 5d 3b 0a 20 20 20 20 20 20 20 20  [ii*2];.        
399aa 20 20 70 2d 3e 69 43 6f 6f 72 64 20 3d 20 69 64    p->iCoord = id
399ab 78 53 74 72 5b 69 69 2a 32 2b 31 5d 2d 27 61 27  xStr[ii*2+1]-'a'
399ac 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72  ;.          p->r
399ad 56 61 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 5f  Value = sqlite3_
399ae 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67  value_double(arg
399af 76 5b 69 69 5d 29 3b 0a 20 20 20 20 20 20 20 20  v[ii]);.        
399b0 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
399b1 20 20 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53    .    if( rc==S
399b2 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
399b3 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d 20    pCsr->pNode = 
399b4 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 6f  0;.      rc = no
399b5 64 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65  deAcquire(pRtree
399b6 2c 20 31 2c 20 30 2c 20 26 70 52 6f 6f 74 29 3b  , 1, 0, &pRoot);
399b7 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
399b8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
399b9 20 20 20 20 20 20 69 6e 74 20 69 73 45 6f 66 20        int isEof 
399ba 3d 20 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  = 1;.      int n
399bb 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 52 6f  Cell = NCELL(pRo
399bc 6f 74 29 3b 0a 20 20 20 20 20 20 70 43 73 72 2d  ot);.      pCsr-
399bd 3e 70 4e 6f 64 65 20 3d 20 70 52 6f 6f 74 3b 0a  >pNode = pRoot;.
399be 20 20 20 20 20 20 66 6f 72 28 70 43 73 72 2d 3e        for(pCsr->
399bf 69 43 65 6c 6c 3d 30 3b 20 72 63 3d 3d 53 51 4c  iCell=0; rc==SQL
399c0 49 54 45 5f 4f 4b 20 26 26 20 70 43 73 72 2d 3e  ITE_OK && pCsr->
399c1 69 43 65 6c 6c 3c 6e 43 65 6c 6c 3b 20 70 43 73  iCell<nCell; pCs
399c2 72 2d 3e 69 43 65 6c 6c 2b 2b 29 7b 0a 20 20 20  r->iCell++){.   
399c3 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 73       assert( pCs
399c4 72 2d 3e 70 4e 6f 64 65 3d 3d 70 52 6f 6f 74 20  r->pNode==pRoot 
399c5 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
399c6 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28 70 52  descendToCell(pR
399c7 74 72 65 65 2c 20 70 43 73 72 2c 20 70 52 74 72  tree, pCsr, pRtr
399c8 65 65 2d 3e 69 44 65 70 74 68 2c 20 26 69 73 45  ee->iDepth, &isE
399c9 6f 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  of);.        if(
399ca 20 21 69 73 45 6f 66 20 29 7b 0a 20 20 20 20 20   !isEof ){.     
399cb 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
399cc 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
399cd 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
399ce 54 45 5f 4f 4b 20 26 26 20 69 73 45 6f 66 20 29  TE_OK && isEof )
399cf 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
399d0 28 20 70 43 73 72 2d 3e 70 4e 6f 64 65 3d 3d 70  ( pCsr->pNode==p
399d1 52 6f 6f 74 20 29 3b 0a 20 20 20 20 20 20 20 20  Root );.        
399d2 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72  nodeRelease(pRtr
399d3 65 65 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20  ee, pRoot);.    
399d4 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20      pCsr->pNode 
399d5 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
399d6 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
399d7 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 70 43 73  QLITE_OK || !pCs
399d8 72 2d 3e 70 4e 6f 64 65 20 7c 7c 20 70 43 73 72  r->pNode || pCsr
399d9 2d 3e 69 43 65 6c 6c 3c 4e 43 45 4c 4c 28 70 43  ->iCell<NCELL(pC
399da 73 72 2d 3e 70 4e 6f 64 65 29 20 29 3b 0a 20 20  sr->pNode) );.  
399db 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 74 72 65 65    }.  }..  rtree
399dc 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 29 3b  Release(pRtree);
399dd 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
399de 0a 2f 2a 0a 2a 2a 20 52 74 72 65 65 20 76 69 72  ./*.** Rtree vir
399df 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
399e0 65 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65 74  e xBestIndex met
399e1 68 6f 64 2e 20 54 68 65 72 65 20 61 72 65 20 74  hod. There are t
399e2 68 72 65 65 0a 2a 2a 20 74 61 62 6c 65 20 73 63  hree.** table sc
399e3 61 6e 20 73 74 72 61 74 65 67 69 65 73 20 74 6f  an strategies to
399e4 20 63 68 6f 6f 73 65 20 66 72 6f 6d 20 28 69 6e   choose from (in
399e5 20 6f 72 64 65 72 20 66 72 6f 6d 20 6d 6f 73 74   order from most
399e6 20 74 6f 20 0a 2a 2a 20 6c 65 61 73 74 20 64 65   to .** least de
399e7 73 69 72 61 62 6c 65 29 3a 0a 2a 2a 0a 2a 2a 20  sirable):.**.** 
399e8 20 20 69 64 78 4e 75 6d 20 20 20 20 20 69 64 78    idxNum     idx
399e9 53 74 72 20 20 20 20 20 20 20 20 53 74 72 61 74  Str        Strat
399ea 65 67 79 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  egy.**   -------
399eb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
399ec 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
399ed 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20  ---------.**    
399ee 20 31 20 20 20 20 20 20 20 20 55 6e 75 73 65 64   1        Unused
399ef 20 20 20 20 20 20 20 20 44 69 72 65 63 74 20 6c          Direct l
399f0 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69 64 2e 0a  ookup by rowid..
399f1 2a 2a 20 20 20 20 20 32 20 20 20 20 20 20 20 20  **     2        
399f2 53 65 65 20 62 65 6c 6f 77 20 20 20 20 20 52 2d  See below     R-
399f3 74 72 65 65 20 71 75 65 72 79 2e 0a 2a 2a 20 20  tree query..**  
399f4 20 20 20 33 20 20 20 20 20 20 20 20 55 6e 75 73     3        Unus
399f5 65 64 20 20 20 20 20 20 20 20 46 75 6c 6c 20 74  ed        Full t
399f6 61 62 6c 65 20 73 63 61 6e 2e 0a 2a 2a 20 20 20  able scan..**   
399f7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
399f8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
399f9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
399fa 0a 2a 2a 0a 2a 2a 20 49 66 20 73 74 72 61 74 65  .**.** If strate
399fb 67 79 20 31 20 6f 72 20 33 20 69 73 20 75 73 65  gy 1 or 3 is use
399fc 64 2c 20 74 68 65 6e 20 69 64 78 53 74 72 20 69  d, then idxStr i
399fd 73 20 6e 6f 74 20 6d 65 61 6e 69 6e 67 66 75 6c  s not meaningful
399fe 2e 20 49 66 20 73 74 72 61 74 65 67 79 0a 2a 2a  . If strategy.**
399ff 20 32 20 69 73 20 75 73 65 64 2c 20 69 64 78 53   2 is used, idxS
39a00 74 72 20 69 73 20 66 6f 72 6d 61 74 74 65 64 20  tr is formatted 
39a01 74 6f 20 63 6f 6e 74 61 69 6e 20 32 20 62 79 74  to contain 2 byt
39a02 65 73 20 66 6f 72 20 65 61 63 68 20 0a 2a 2a 20  es for each .** 
39a03 63 6f 6e 73 74 72 61 69 6e 74 20 75 73 65 64 2e  constraint used.
39a04 20 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 62   The first two b
39a05 79 74 65 73 20 6f 66 20 69 64 78 53 74 72 20 63  ytes of idxStr c
39a06 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 0a 2a 2a  orrespond to .**
39a07 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20   the constraint 
39a08 69 6e 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  in sqlite3_index
39a09 5f 69 6e 66 6f 2e 61 43 6f 6e 73 74 72 61 69 6e  _info.aConstrain
39a0a 74 55 73 61 67 65 5b 5d 20 77 69 74 68 0a 2a 2a  tUsage[] with.**
39a0b 20 28 61 72 67 76 49 6e 64 65 78 3d 3d 31 29 20   (argvIndex==1) 
39a0c 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  etc..**.** The f
39a0d 69 72 73 74 20 6f 66 20 65 61 63 68 20 70 61 69  irst of each pai
39a0e 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 69 64  r of bytes in id
39a0f 78 53 74 72 20 69 64 65 6e 74 69 66 69 65 73 20  xStr identifies 
39a10 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a  the constraint.*
39a11 2a 20 6f 70 65 72 61 74 6f 72 20 61 73 20 66 6f  * operator as fo
39a12 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f  llows:.**.**   O
39a13 70 65 72 61 74 6f 72 20 20 20 20 42 79 74 65 20  perator    Byte 
39a14 56 61 6c 75 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d  Value.**   -----
39a15 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39a16 2d 0a 2a 2a 20 20 20 20 20 20 3d 20 20 20 20 20  -.**      =     
39a17 20 20 20 30 78 34 31 20 28 27 41 27 29 0a 2a 2a     0x41 ('A').**
39a18 20 20 20 20 20 3c 3d 20 20 20 20 20 20 20 20 30       <=        0
39a19 78 34 32 20 28 27 42 27 29 0a 2a 2a 20 20 20 20  x42 ('B').**    
39a1a 20 20 3c 20 20 20 20 20 20 20 20 30 78 34 33 20    <        0x43 
39a1b 28 27 43 27 29 0a 2a 2a 20 20 20 20 20 3e 3d 20  ('C').**     >= 
39a1c 20 20 20 20 20 20 20 30 78 34 34 20 28 27 44 27         0x44 ('D'
39a1d 29 0a 2a 2a 20 20 20 20 20 20 3e 20 20 20 20 20  ).**      >     
39a1e 20 20 20 30 78 34 35 20 28 27 45 27 29 0a 2a 2a     0x45 ('E').**
39a1f 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
39a20 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20  ---------.**.** 
39a21 54 68 65 20 73 65 63 6f 6e 64 20 6f 66 20 65 61  The second of ea
39a22 63 68 20 70 61 69 72 20 6f 66 20 62 79 74 65 73  ch pair of bytes
39a23 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20   identifies the 
39a24 63 6f 6f 72 64 69 6e 61 74 65 20 63 6f 6c 75 6d  coordinate colum
39a25 6e 0a 2a 2a 20 74 6f 20 77 68 69 63 68 20 74 68  n.** to which th
39a26 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 70 70  e constraint app
39a27 6c 69 65 73 2e 20 54 68 65 20 6c 65 66 74 6d 6f  lies. The leftmo
39a28 73 74 20 63 6f 6f 72 64 69 6e 61 74 65 20 63 6f  st coordinate co
39a29 6c 75 6d 6e 0a 2a 2a 20 69 73 20 27 61 27 2c 20  lumn.** is 'a', 
39a2a 74 68 65 20 73 65 63 6f 6e 64 20 66 72 6f 6d 20  the second from 
39a2b 74 68 65 20 6c 65 66 74 20 27 62 27 20 65 74 63  the left 'b' etc
39a2c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
39a2d 72 74 72 65 65 42 65 73 74 49 6e 64 65 78 28 73  rtreeBestIndex(s
39a2e 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 74 61 62  qlite3_vtab *tab
39a2f 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  , sqlite3_index_
39a30 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66 6f 29 7b  info *pIdxInfo){
39a31 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
39a32 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 69 2c  TE_OK;.  int ii,
39a33 20 63 43 6f 6c 3b 0a 0a 20 20 69 6e 74 20 69 49   cCol;..  int iI
39a34 64 78 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 7a  dx = 0;.  char z
39a35 49 64 78 53 74 72 5b 52 54 52 45 45 5f 4d 41 58  IdxStr[RTREE_MAX
39a36 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a 38 2b 31 5d  _DIMENSIONS*8+1]
39a37 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 49 64 78 53  ;.  memset(zIdxS
39a38 74 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 7a 49  tr, 0, sizeof(zI
39a39 64 78 53 74 72 29 29 3b 0a 0a 20 20 61 73 73 65  dxStr));..  asse
39a3a 72 74 28 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64  rt( pIdxInfo->id
39a3b 78 53 74 72 3d 3d 30 20 29 3b 0a 20 20 66 6f 72  xStr==0 );.  for
39a3c 28 69 69 3d 30 3b 20 69 69 3c 70 49 64 78 49 6e  (ii=0; ii<pIdxIn
39a3d 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  fo->nConstraint;
39a3e 20 69 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75   ii++){.    stru
39a3f 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
39a40 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a 70 20 3d  _constraint *p =
39a41 20 26 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e   &pIdxInfo->aCon
39a42 73 74 72 61 69 6e 74 5b 69 69 5d 3b 0a 0a 20 20  straint[ii];..  
39a43 20 20 69 66 28 20 70 2d 3e 75 73 61 62 6c 65 20    if( p->usable 
39a44 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 30  && p->iColumn==0
39a45 20 26 26 20 70 2d 3e 6f 70 3d 3d 53 51 4c 49 54   && p->op==SQLIT
39a46 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
39a47 4e 54 5f 45 51 20 29 7b 0a 20 20 20 20 20 20 2f  NT_EQ ){.      /
39a48 2a 20 57 65 20 68 61 76 65 20 61 6e 20 65 71 75  * We have an equ
39a49 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
39a4a 20 6f 6e 20 74 68 65 20 72 6f 77 69 64 2e 20 55   on the rowid. U
39a4b 73 65 20 73 74 72 61 74 65 67 79 20 31 2e 20 2a  se strategy 1. *
39a4c 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a  /.      int jj;.
39a4d 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20        for(jj=0; 
39a4e 6a 6a 3c 69 69 3b 20 6a 6a 2b 2b 29 7b 0a 20 20  jj<ii; jj++){.  
39a4f 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e        pIdxInfo->
39a50 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
39a51 5b 6a 6a 5d 2e 61 72 67 76 49 6e 64 65 78 20 3d  [jj].argvIndex =
39a52 20 30 3b 0a 20 20 20 20 20 20 20 20 70 49 64 78   0;.        pIdx
39a53 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
39a54 74 55 73 61 67 65 5b 6a 6a 5d 2e 6f 6d 69 74 20  tUsage[jj].omit 
39a55 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
39a56 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78     pIdxInfo->idx
39a57 4e 75 6d 20 3d 20 31 3b 0a 20 20 20 20 20 20 70  Num = 1;.      p
39a58 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
39a59 61 69 6e 74 55 73 61 67 65 5b 69 69 5d 2e 61 72  aintUsage[ii].ar
39a5a 67 76 49 6e 64 65 78 20 3d 20 31 3b 0a 20 20 20  gvIndex = 1;.   
39a5b 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f     pIdxInfo->aCo
39a5c 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 6a 6a  nstraintUsage[jj
39a5d 5d 2e 6f 6d 69 74 20 3d 20 31 3b 0a 0a 20 20 20  ].omit = 1;..   
39a5e 20 20 20 2f 2a 20 54 68 69 73 20 73 74 72 61 74     /* This strat
39a5f 65 67 79 20 69 6e 76 6f 6c 76 65 73 20 61 20 74  egy involves a t
39a60 77 6f 20 72 6f 77 69 64 20 6c 6f 6f 6b 75 70 73  wo rowid lookups
39a61 20 6f 6e 20 61 6e 20 42 2d 54 72 65 65 20 73 74   on an B-Tree st
39a62 72 75 63 74 75 72 65 73 0a 20 20 20 20 20 20 2a  ructures.      *
39a63 2a 20 61 6e 64 20 74 68 65 6e 20 61 20 6c 69 6e  * and then a lin
39a64 65 61 72 20 73 65 61 72 63 68 20 6f 66 20 61 6e  ear search of an
39a65 20 52 2d 54 72 65 65 20 6e 6f 64 65 2e 20 54 68   R-Tree node. Th
39a66 69 73 20 73 68 6f 75 6c 64 20 62 65 20 0a 20 20  is should be .  
39a67 20 20 20 20 2a 2a 20 63 6f 6e 73 69 64 65 72 65      ** considere
39a68 64 20 61 6c 6d 6f 73 74 20 61 73 20 71 75 69 63  d almost as quic
39a69 6b 20 61 73 20 61 20 64 69 72 65 63 74 20 72 6f  k as a direct ro
39a6a 77 69 64 20 6c 6f 6f 6b 75 70 20 28 66 6f 72 20  wid lookup (for 
39a6b 77 68 69 63 68 20 0a 20 20 20 20 20 20 2a 2a 20  which .      ** 
39a6c 73 71 6c 69 74 65 20 75 73 65 73 20 61 6e 20 69  sqlite uses an i
39a6d 6e 74 65 72 6e 61 6c 20 63 6f 73 74 20 6f 66 20  nternal cost of 
39a6e 30 2e 30 29 2e 0a 20 20 20 20 20 20 2a 2f 20 0a  0.0)..      */ .
39a6f 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e        pIdxInfo->
39a70 65 73 74 69 6d 61 74 65 64 43 6f 73 74 20 3d 20  estimatedCost = 
39a71 31 30 2e 30 3b 0a 20 20 20 20 20 20 72 65 74 75  10.0;.      retu
39a72 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
39a73 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e    }..    if( p->
39a74 75 73 61 62 6c 65 20 26 26 20 70 2d 3e 69 43 6f  usable && p->iCo
39a75 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20  lumn>0 ){.      
39a76 75 38 20 6f 70 20 3d 20 30 3b 0a 20 20 20 20 20  u8 op = 0;.     
39a77 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29   switch( p->op )
39a78 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 53  {.        case S
39a79 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
39a7a 54 52 41 49 4e 54 5f 45 51 3a 20 6f 70 20 3d 20  TRAINT_EQ: op = 
39a7b 52 54 52 45 45 5f 45 51 3b 20 62 72 65 61 6b 3b  RTREE_EQ; break;
39a7c 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 53 51  .        case SQ
39a7d 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
39a7e 52 41 49 4e 54 5f 47 54 3a 20 6f 70 20 3d 20 52  RAINT_GT: op = R
39a7f 54 52 45 45 5f 47 54 3b 20 62 72 65 61 6b 3b 0a  TREE_GT; break;.
39a80 20 20 20 20 20 20 20 20 63 61 73 65 20 53 51 4c          case SQL
39a81 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
39a82 41 49 4e 54 5f 4c 45 3a 20 6f 70 20 3d 20 52 54  AINT_LE: op = RT
39a83 52 45 45 5f 4c 45 3b 20 62 72 65 61 6b 3b 0a 20  REE_LE; break;. 
39a84 20 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49         case SQLI
39a85 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
39a86 49 4e 54 5f 4c 54 3a 20 6f 70 20 3d 20 52 54 52  INT_LT: op = RTR
39a87 45 45 5f 4c 54 3b 20 62 72 65 61 6b 3b 0a 20 20  EE_LT; break;.  
39a88 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54        case SQLIT
39a89 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
39a8a 4e 54 5f 47 45 3a 20 6f 70 20 3d 20 52 54 52 45  NT_GE: op = RTRE
39a8b 45 5f 47 45 3b 20 62 72 65 61 6b 3b 0a 20 20 20  E_GE; break;.   
39a8c 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6f     }.      if( o
39a8d 70 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  p ){.        /* 
39a8e 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73 20 70  Make sure this p
39a8f 61 72 74 69 63 75 6c 61 72 20 63 6f 6e 73 74 72  articular constr
39a90 61 69 6e 74 20 68 61 73 20 6e 6f 74 20 62 65 65  aint has not bee
39a91 6e 20 75 73 65 64 20 62 65 66 6f 72 65 2e 0a 20  n used before.. 
39a92 20 20 20 20 20 20 20 2a 2a 20 49 66 20 69 74 20         ** If it 
39a93 68 61 73 20 62 65 65 6e 20 75 73 65 64 20 62 65  has been used be
39a94 66 6f 72 65 2c 20 69 67 6e 6f 72 65 20 69 74 2e  fore, ignore it.
39a95 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
39a96 20 20 20 20 2a 2a 20 41 20 3c 3d 20 6f 72 20 3c      ** A <= or <
39a97 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 66 20   can be used if 
39a98 74 68 65 72 65 20 69 73 20 61 20 70 72 69 6f 72  there is a prior
39a99 20 3e 3d 20 6f 72 20 3e 2e 0a 20 20 20 20 20 20   >= or >..      
39a9a 20 20 2a 2a 20 41 20 3e 3d 20 6f 72 20 3e 20 63    ** A >= or > c
39a9b 61 6e 20 62 65 20 75 73 65 64 20 69 66 20 74 68  an be used if th
39a9c 65 72 65 20 69 73 20 61 20 70 72 69 6f 72 20 3c  ere is a prior <
39a9d 20 6f 72 20 3c 3d 2e 0a 20 20 20 20 20 20 20 20   or <=..        
39a9e 2a 2a 20 41 20 3c 3d 20 6f 72 20 3c 20 69 73 20  ** A <= or < is 
39a9f 64 69 73 71 75 61 6c 69 66 69 65 64 20 69 66 20  disqualified if 
39aa0 74 68 65 72 65 20 69 73 20 61 20 70 72 69 6f 72  there is a prior
39aa1 20 3c 3d 2c 20 3c 2c 20 6f 72 20 3d 3d 2e 0a 20   <=, <, or ==.. 
39aa2 20 20 20 20 20 20 20 2a 2a 20 41 20 3e 3d 20 6f         ** A >= o
39aa3 72 20 3e 20 69 73 20 64 69 73 71 75 61 6c 69 66  r > is disqualif
39aa4 69 65 64 20 69 66 20 74 68 65 72 65 20 69 73 20  ied if there is 
39aa5 61 20 70 72 69 6f 72 20 3e 3d 2c 20 3e 2c 20 6f  a prior >=, >, o
39aa6 72 20 3d 3d 2e 0a 20 20 20 20 20 20 20 20 2a 2a  r ==..        **
39aa7 20 41 20 3d 3d 20 69 73 20 64 69 73 71 75 61 6c   A == is disqual
39aa8 69 66 65 64 20 69 66 20 74 68 65 72 65 20 69 73  ifed if there is
39aa9 20 61 6e 79 20 70 72 69 6f 72 20 63 6f 6e 73 74   any prior const
39aaa 72 61 69 6e 74 2e 0a 20 20 20 20 20 20 20 20 2a  raint..        *
39aab 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 2c  /.        int j,
39aac 20 6f 70 6d 73 6b 3b 0a 20 20 20 20 20 20 20 20   opmsk;.        
39aad 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
39aae 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6d 70 61  igned char compa
39aaf 74 69 62 6c 65 5b 5d 20 3d 20 7b 20 30 2c 20 30  tible[] = { 0, 0
39ab0 2c 20 31 2c 20 31 2c 20 32 2c 20 32 20 7d 3b 0a  , 1, 1, 2, 2 };.
39ab1 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
39ab2 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52 45 45  compatible[RTREE
39ab3 5f 45 51 20 26 20 37 5d 3d 3d 30 20 29 3b 0a 20  _EQ & 7]==0 );. 
39ab4 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 63         assert( c
39ab5 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52 45 45 5f  ompatible[RTREE_
39ab6 4c 54 20 26 20 37 5d 3d 3d 31 20 29 3b 0a 20 20  LT & 7]==1 );.  
39ab7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 63 6f        assert( co
39ab8 6d 70 61 74 69 62 6c 65 5b 52 54 52 45 45 5f 4c  mpatible[RTREE_L
39ab9 45 20 26 20 37 5d 3d 3d 31 20 29 3b 0a 20 20 20  E & 7]==1 );.   
39aba 20 20 20 20 20 61 73 73 65 72 74 28 20 63 6f 6d       assert( com
39abb 70 61 74 69 62 6c 65 5b 52 54 52 45 45 5f 47 54  patible[RTREE_GT
39abc 20 26 20 37 5d 3d 3d 32 20 29 3b 0a 20 20 20 20   & 7]==2 );.    
39abd 20 20 20 20 61 73 73 65 72 74 28 20 63 6f 6d 70      assert( comp
39abe 61 74 69 62 6c 65 5b 52 54 52 45 45 5f 47 45 20  atible[RTREE_GE 
39abf 26 20 37 5d 3d 3d 32 20 29 3b 0a 20 20 20 20 20  & 7]==2 );.     
39ac0 20 20 20 63 43 6f 6c 20 3d 20 70 2d 3e 69 43 6f     cCol = p->iCo
39ac1 6c 75 6d 6e 20 2d 20 31 20 2b 20 27 61 27 3b 0a  lumn - 1 + 'a';.
39ac2 20 20 20 20 20 20 20 20 6f 70 6d 73 6b 20 3d 20          opmsk = 
39ac3 63 6f 6d 70 61 74 69 62 6c 65 5b 6f 70 20 26 20  compatible[op & 
39ac4 37 5d 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  7];.        for(
39ac5 6a 3d 30 3b 20 6a 3c 69 49 64 78 3b 20 6a 2b 3d  j=0; j<iIdx; j+=
39ac6 32 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  2){.          if
39ac7 28 20 7a 49 64 78 53 74 72 5b 6a 2b 31 5d 3d 3d  ( zIdxStr[j+1]==
39ac8 63 43 6f 6c 20 26 26 20 28 63 6f 6d 70 61 74 69  cCol && (compati
39ac9 62 6c 65 5b 7a 49 64 78 53 74 72 5b 6a 5d 20 26  ble[zIdxStr[j] &
39aca 20 37 5d 20 26 20 6f 70 6d 73 6b 29 21 3d 30 20   7] & opmsk)!=0 
39acb 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6f  ){.            o
39acc 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  p = 0;.         
39acd 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
39ace 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
39acf 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
39ad0 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20 20 20  ( op ){.        
39ad1 61 73 73 65 72 74 28 20 69 49 64 78 3c 73 69 7a  assert( iIdx<siz
39ad2 65 6f 66 28 7a 49 64 78 53 74 72 29 2d 31 20 29  eof(zIdxStr)-1 )
39ad3 3b 0a 20 20 20 20 20 20 20 20 7a 49 64 78 53 74  ;.        zIdxSt
39ad4 72 5b 69 49 64 78 2b 2b 5d 20 3d 20 6f 70 3b 0a  r[iIdx++] = op;.
39ad5 20 20 20 20 20 20 20 20 7a 49 64 78 53 74 72 5b          zIdxStr[
39ad6 69 49 64 78 2b 2b 5d 20 3d 20 63 43 6f 6c 3b 0a  iIdx++] = cCol;.
39ad7 20 20 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f          pIdxInfo
39ad8 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  ->aConstraintUsa
39ad9 67 65 5b 69 69 5d 2e 61 72 67 76 49 6e 64 65 78  ge[ii].argvIndex
39ada 20 3d 20 28 69 49 64 78 2f 32 29 3b 0a 20 20 20   = (iIdx/2);.   
39adb 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61       pIdxInfo->a
39adc 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b  ConstraintUsage[
39add 69 69 5d 2e 6f 6d 69 74 20 3d 20 31 3b 0a 20 20  ii].omit = 1;.  
39ade 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
39adf 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78  .  pIdxInfo->idx
39ae0 4e 75 6d 20 3d 20 32 3b 0a 20 20 70 49 64 78 49  Num = 2;.  pIdxI
39ae1 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49  nfo->needToFreeI
39ae2 64 78 53 74 72 20 3d 20 31 3b 0a 20 20 69 66 28  dxStr = 1;.  if(
39ae3 20 69 49 64 78 3e 30 20 26 26 20 30 3d 3d 28 70   iIdx>0 && 0==(p
39ae4 49 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 20  IdxInfo->idxStr 
39ae5 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
39ae6 66 28 22 25 73 22 2c 20 7a 49 64 78 53 74 72 29  f("%s", zIdxStr)
39ae7 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
39ae8 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
39ae9 7d 0a 20 20 61 73 73 65 72 74 28 20 69 49 64 78  }.  assert( iIdx
39aea 3e 3d 30 20 29 3b 0a 20 20 70 49 64 78 49 6e 66  >=0 );.  pIdxInf
39aeb 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74  o->estimatedCost
39aec 20 3d 20 28 32 30 30 30 30 30 30 2e 30 20 2f 20   = (2000000.0 / 
39aed 28 64 6f 75 62 6c 65 29 28 69 49 64 78 20 2b 20  (double)(iIdx + 
39aee 31 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  1));.  return rc
39aef 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
39af0 6e 20 74 68 65 20 4e 2d 64 69 6d 65 6e 73 69 6f  n the N-dimensio
39af1 6e 61 6c 20 76 6f 6c 75 6d 6e 20 6f 66 20 74 68  nal volumn of th
39af2 65 20 63 65 6c 6c 20 73 74 6f 72 65 64 20 69 6e  e cell stored in
39af3 20 2a 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 66   *p..*/.static f
39af4 6c 6f 61 74 20 63 65 6c 6c 41 72 65 61 28 52 74  loat cellArea(Rt
39af5 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
39af6 65 65 43 65 6c 6c 20 2a 70 29 7b 0a 20 20 66 6c  eeCell *p){.  fl
39af7 6f 61 74 20 61 72 65 61 20 3d 20 31 2e 30 3b 0a  oat area = 1.0;.
39af8 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28    int ii;.  for(
39af9 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65 65  ii=0; ii<(pRtree
39afa 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32  ->nDim*2); ii+=2
39afb 29 7b 0a 20 20 20 20 61 72 65 61 20 3d 20 61 72  ){.    area = ar
39afc 65 61 20 2a 20 28 44 43 4f 4f 52 44 28 70 2d 3e  ea * (DCOORD(p->
39afd 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 29 20 2d 20  aCoord[ii+1]) - 
39afe 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64  DCOORD(p->aCoord
39aff 5b 69 69 5d 29 29 3b 0a 20 20 7d 0a 20 20 72 65  [ii]));.  }.  re
39b00 74 75 72 6e 20 61 72 65 61 3b 0a 7d 0a 0a 2f 2a  turn area;.}../*
39b01 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d  .** Return the m
39b02 61 72 67 69 6e 20 6c 65 6e 67 74 68 20 6f 66 20  argin length of 
39b03 63 65 6c 6c 20 70 2e 20 54 68 65 20 6d 61 72 67  cell p. The marg
39b04 69 6e 20 6c 65 6e 67 74 68 20 69 73 20 74 68 65  in length is the
39b05 20 73 75 6d 0a 2a 2a 20 6f 66 20 74 68 65 20 6f   sum.** of the o
39b06 62 6a 65 63 74 73 20 73 69 7a 65 20 69 6e 20 65  bjects size in e
39b07 61 63 68 20 64 69 6d 65 6e 73 69 6f 6e 2e 0a 2a  ach dimension..*
39b08 2f 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20 63  /.static float c
39b09 65 6c 6c 4d 61 72 67 69 6e 28 52 74 72 65 65 20  ellMargin(Rtree 
39b0a 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 43 65  *pRtree, RtreeCe
39b0b 6c 6c 20 2a 70 29 7b 0a 20 20 66 6c 6f 61 74 20  ll *p){.  float 
39b0c 6d 61 72 67 69 6e 20 3d 20 30 2e 30 3b 0a 20 20  margin = 0.0;.  
39b0d 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28 69 69  int ii;.  for(ii
39b0e 3d 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e  =0; ii<(pRtree->
39b0f 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b  nDim*2); ii+=2){
39b10 0a 20 20 20 20 6d 61 72 67 69 6e 20 2b 3d 20 28  .    margin += (
39b11 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64  DCOORD(p->aCoord
39b12 5b 69 69 2b 31 5d 29 20 2d 20 44 43 4f 4f 52 44  [ii+1]) - DCOORD
39b13 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 29 29  (p->aCoord[ii]))
39b14 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d  ;.  }.  return m
39b15 61 72 67 69 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  argin;.}../*.** 
39b16 53 74 6f 72 65 20 74 68 65 20 75 6e 69 6f 6e 20  Store the union 
39b17 6f 66 20 63 65 6c 6c 73 20 70 31 20 61 6e 64 20  of cells p1 and 
39b18 70 32 20 69 6e 20 70 31 2e 0a 2a 2f 0a 73 74 61  p2 in p1..*/.sta
39b19 74 69 63 20 76 6f 69 64 20 63 65 6c 6c 55 6e 69  tic void cellUni
39b1a 6f 6e 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  on(Rtree *pRtree
39b1b 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 31 2c  , RtreeCell *p1,
39b1c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 32 29 7b   RtreeCell *p2){
39b1d 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 66 28  .  int ii;.  if(
39b1e 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54   pRtree->eCoordT
39b1f 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52 44  ype==RTREE_COORD
39b20 5f 52 45 41 4c 33 32 20 29 7b 0a 20 20 20 20 66  _REAL32 ){.    f
39b21 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74  or(ii=0; ii<(pRt
39b22 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69  ree->nDim*2); ii
39b23 2b 3d 32 29 7b 0a 20 20 20 20 20 20 70 31 2d 3e  +=2){.      p1->
39b24 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 20 3d 20 4d  aCoord[ii].f = M
39b25 49 4e 28 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69  IN(p1->aCoord[ii
39b26 5d 2e 66 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b  ].f, p2->aCoord[
39b27 69 69 5d 2e 66 29 3b 0a 20 20 20 20 20 20 70 31  ii].f);.      p1
39b28 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66  ->aCoord[ii+1].f
39b29 20 3d 20 4d 41 58 28 70 31 2d 3e 61 43 6f 6f 72   = MAX(p1->aCoor
39b2a 64 5b 69 69 2b 31 5d 2e 66 2c 20 70 32 2d 3e 61  d[ii+1].f, p2->a
39b2b 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 29 3b 0a  Coord[ii+1].f);.
39b2c 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
39b2d 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c     for(ii=0; ii<
39b2e 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29  (pRtree->nDim*2)
39b2f 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20  ; ii+=2){.      
39b30 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 69  p1->aCoord[ii].i
39b31 20 3d 20 4d 49 4e 28 70 31 2d 3e 61 43 6f 6f 72   = MIN(p1->aCoor
39b32 64 5b 69 69 5d 2e 69 2c 20 70 32 2d 3e 61 43 6f  d[ii].i, p2->aCo
39b33 6f 72 64 5b 69 69 5d 2e 69 29 3b 0a 20 20 20 20  ord[ii].i);.    
39b34 20 20 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b    p1->aCoord[ii+
39b35 31 5d 2e 69 20 3d 20 4d 41 58 28 70 31 2d 3e 61  1].i = MAX(p1->a
39b36 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69 2c 20 70  Coord[ii+1].i, p
39b37 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e  2->aCoord[ii+1].
39b38 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  i);.    }.  }.}.
39b39 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72  ./*.** Return tr
39b3a 75 65 20 69 66 20 74 68 65 20 61 72 65 61 20 63  ue if the area c
39b3b 6f 76 65 72 65 64 20 62 79 20 70 32 20 69 73 20  overed by p2 is 
39b3c 61 20 73 75 62 73 65 74 20 6f 66 20 74 68 65 20  a subset of the 
39b3d 61 72 65 61 20 63 6f 76 65 72 65 64 0a 2a 2a 20  area covered.** 
39b3e 62 79 20 70 31 2e 20 46 61 6c 73 65 20 6f 74 68  by p1. False oth
39b3f 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  erwise..*/.stati
39b40 63 20 69 6e 74 20 63 65 6c 6c 43 6f 6e 74 61 69  c int cellContai
39b41 6e 73 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  ns(Rtree *pRtree
39b42 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 31 2c  , RtreeCell *p1,
39b43 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 32 29 7b   RtreeCell *p2){
39b44 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74  .  int ii;.  int
39b45 20 69 73 49 6e 74 20 3d 20 28 70 52 74 72 65 65   isInt = (pRtree
39b46 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54  ->eCoordType==RT
39b47 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32 29  REE_COORD_INT32)
39b48 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ;.  for(ii=0; ii
39b49 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
39b4a 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 52  ); ii+=2){.    R
39b4b 74 72 65 65 43 6f 6f 72 64 20 2a 61 31 20 3d 20  treeCoord *a1 = 
39b4c 26 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 3b  &p1->aCoord[ii];
39b4d 0a 20 20 20 20 52 74 72 65 65 43 6f 6f 72 64 20  .    RtreeCoord 
39b4e 2a 61 32 20 3d 20 26 70 32 2d 3e 61 43 6f 6f 72  *a2 = &p2->aCoor
39b4f 64 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28 20 28  d[ii];.    if( (
39b50 21 69 73 49 6e 74 20 26 26 20 28 61 32 5b 30 5d  !isInt && (a2[0]
39b51 2e 66 3c 61 31 5b 30 5d 2e 66 20 7c 7c 20 61 32  .f<a1[0].f || a2
39b52 5b 31 5d 2e 66 3e 61 31 5b 31 5d 2e 66 29 29 20  [1].f>a1[1].f)) 
39b53 0a 20 20 20 20 20 7c 7c 20 28 20 69 73 49 6e 74  .     || ( isInt
39b54 20 26 26 20 28 61 32 5b 30 5d 2e 69 3c 61 31 5b   && (a2[0].i<a1[
39b55 30 5d 2e 69 20 7c 7c 20 61 32 5b 31 5d 2e 69 3e  0].i || a2[1].i>
39b56 61 31 5b 31 5d 2e 69 29 29 20 0a 20 20 20 20 29  a1[1].i)) .    )
39b57 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
39b58 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
39b59 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 1;.}../*.**
39b5a 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75   Return the amou
39b5b 6e 74 20 63 65 6c 6c 20 70 20 77 6f 75 6c 64 20  nt cell p would 
39b5c 67 72 6f 77 20 62 79 20 69 66 20 69 74 20 77 65  grow by if it we
39b5d 72 65 20 75 6e 69 6f 6e 65 64 20 77 69 74 68 20  re unioned with 
39b5e 70 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63  pCell..*/.static
39b5f 20 66 6c 6f 61 74 20 63 65 6c 6c 47 72 6f 77 74   float cellGrowt
39b60 68 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  h(Rtree *pRtree,
39b61 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 2c 20 52   RtreeCell *p, R
39b62 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 29  treeCell *pCell)
39b63 7b 0a 20 20 66 6c 6f 61 74 20 61 72 65 61 3b 0a  {.  float area;.
39b64 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c    RtreeCell cell
39b65 3b 0a 20 20 6d 65 6d 63 70 79 28 26 63 65 6c 6c  ;.  memcpy(&cell
39b66 2c 20 70 2c 20 73 69 7a 65 6f 66 28 52 74 72 65  , p, sizeof(Rtre
39b67 65 43 65 6c 6c 29 29 3b 0a 20 20 61 72 65 61 20  eCell));.  area 
39b68 3d 20 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65  = cellArea(pRtre
39b69 65 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 63 65 6c  e, &cell);.  cel
39b6a 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26  lUnion(pRtree, &
39b6b 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 3b 0a 20 20  cell, pCell);.  
39b6c 72 65 74 75 72 6e 20 28 63 65 6c 6c 41 72 65 61  return (cellArea
39b6d 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 29 2d  (pRtree, &cell)-
39b6e 61 72 65 61 29 3b 0a 7d 0a 0a 23 69 66 20 56 41  area);.}..#if VA
39b6f 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f  RIANT_RSTARTREE_
39b70 43 48 4f 4f 53 45 53 55 42 54 52 45 45 20 7c 7c  CHOOSESUBTREE ||
39b71 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52   VARIANT_RSTARTR
39b72 45 45 5f 53 50 4c 49 54 0a 73 74 61 74 69 63 20  EE_SPLIT.static 
39b73 66 6c 6f 61 74 20 63 65 6c 6c 4f 76 65 72 6c 61  float cellOverla
39b74 70 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  p(.  Rtree *pRtr
39b75 65 65 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c  ee, .  RtreeCell
39b76 20 2a 70 2c 20 0a 20 20 52 74 72 65 65 43 65 6c   *p, .  RtreeCel
39b77 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74  l *aCell, .  int
39b78 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 69   nCell, .  int i
39b79 45 78 63 6c 75 64 65 0a 29 7b 0a 20 20 69 6e 74  Exclude.){.  int
39b7a 20 69 69 3b 0a 20 20 66 6c 6f 61 74 20 6f 76 65   ii;.  float ove
39b7b 72 6c 61 70 20 3d 20 30 2e 30 3b 0a 20 20 66 6f  rlap = 0.0;.  fo
39b7c 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c  r(ii=0; ii<nCell
39b7d 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  ; ii++){.    if(
39b7e 20 69 69 21 3d 69 45 78 63 6c 75 64 65 20 29 7b   ii!=iExclude ){
39b7f 0a 20 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 20  .      int jj;. 
39b80 20 20 20 20 20 66 6c 6f 61 74 20 6f 20 3d 20 31       float o = 1
39b81 2e 30 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 6a  .0;.      for(jj
39b82 3d 30 3b 20 6a 6a 3c 28 70 52 74 72 65 65 2d 3e  =0; jj<(pRtree->
39b83 6e 44 69 6d 2a 32 29 3b 20 6a 6a 2b 3d 32 29 7b  nDim*2); jj+=2){
39b84 0a 20 20 20 20 20 20 20 20 64 6f 75 62 6c 65 20  .        double 
39b85 78 31 3b 0a 20 20 20 20 20 20 20 20 64 6f 75 62  x1;.        doub
39b86 6c 65 20 78 32 3b 0a 0a 20 20 20 20 20 20 20 20  le x2;..        
39b87 78 31 20 3d 20 4d 41 58 28 44 43 4f 4f 52 44 28  x1 = MAX(DCOORD(
39b88 70 2d 3e 61 43 6f 6f 72 64 5b 6a 6a 5d 29 2c 20  p->aCoord[jj]), 
39b89 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d  DCOORD(aCell[ii]
39b8a 2e 61 43 6f 6f 72 64 5b 6a 6a 5d 29 29 3b 0a 20  .aCoord[jj]));. 
39b8b 20 20 20 20 20 20 20 78 32 20 3d 20 4d 49 4e 28         x2 = MIN(
39b8c 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64  DCOORD(p->aCoord
39b8d 5b 6a 6a 2b 31 5d 29 2c 20 44 43 4f 4f 52 44 28  [jj+1]), DCOORD(
39b8e 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64  aCell[ii].aCoord
39b8f 5b 6a 6a 2b 31 5d 29 29 3b 0a 0a 20 20 20 20 20  [jj+1]));..     
39b90 20 20 20 69 66 28 20 78 32 3c 78 31 20 29 7b 0a     if( x2<x1 ){.
39b91 20 20 20 20 20 20 20 20 20 20 6f 20 3d 20 30 2e            o = 0.
39b92 30 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  0;.          bre
39b93 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ak;.        }els
39b94 65 7b 0a 20 20 20 20 20 20 20 20 20 20 6f 20 3d  e{.          o =
39b95 20 6f 20 2a 20 28 78 32 2d 78 31 29 3b 0a 20 20   o * (x2-x1);.  
39b96 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
39b97 20 20 20 20 20 20 6f 76 65 72 6c 61 70 20 2b 3d        overlap +=
39b98 20 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   o;.    }.  }.  
39b99 72 65 74 75 72 6e 20 6f 76 65 72 6c 61 70 3b 0a  return overlap;.
39b9a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 56 41  }.#endif..#if VA
39b9b 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f  RIANT_RSTARTREE_
39b9c 43 48 4f 4f 53 45 53 55 42 54 52 45 45 0a 73 74  CHOOSESUBTREE.st
39b9d 61 74 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 4f  atic float cellO
39b9e 76 65 72 6c 61 70 45 6e 6c 61 72 67 65 6d 65 6e  verlapEnlargemen
39b9f 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  t(.  Rtree *pRtr
39ba0 65 65 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c  ee, .  RtreeCell
39ba1 20 2a 70 2c 20 0a 20 20 52 74 72 65 65 43 65 6c   *p, .  RtreeCel
39ba2 6c 20 2a 70 49 6e 73 65 72 74 2c 20 0a 20 20 52  l *pInsert, .  R
39ba3 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c  treeCell *aCell,
39ba4 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a   .  int nCell, .
39ba5 20 20 69 6e 74 20 69 45 78 63 6c 75 64 65 0a 29    int iExclude.)
39ba6 7b 0a 20 20 66 6c 6f 61 74 20 62 65 66 6f 72 65  {.  float before
39ba7 3b 0a 20 20 66 6c 6f 61 74 20 61 66 74 65 72 3b  ;.  float after;
39ba8 0a 20 20 62 65 66 6f 72 65 20 3d 20 63 65 6c 6c  .  before = cell
39ba9 4f 76 65 72 6c 61 70 28 70 52 74 72 65 65 2c 20  Overlap(pRtree, 
39baa 70 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c  p, aCell, nCell,
39bab 20 69 45 78 63 6c 75 64 65 29 3b 0a 20 20 63 65   iExclude);.  ce
39bac 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20  llUnion(pRtree, 
39bad 70 2c 20 70 49 6e 73 65 72 74 29 3b 0a 20 20 61  p, pInsert);.  a
39bae 66 74 65 72 20 3d 20 63 65 6c 6c 4f 76 65 72 6c  fter = cellOverl
39baf 61 70 28 70 52 74 72 65 65 2c 20 70 2c 20 61 43  ap(pRtree, p, aC
39bb0 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 69 45 78 63  ell, nCell, iExc
39bb1 6c 75 64 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  lude);.  return 
39bb2 61 66 74 65 72 2d 62 65 66 6f 72 65 3b 0a 7d 0a  after-before;.}.
39bb3 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54  #endif.../*.** T
39bb4 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  his function imp
39bb5 6c 65 6d 65 6e 74 73 20 74 68 65 20 43 68 6f 6f  lements the Choo
39bb6 73 65 4c 65 61 66 20 61 6c 67 6f 72 69 74 68 6d  seLeaf algorithm
39bb7 20 66 72 6f 6d 20 47 75 74 6d 61 6e 5b 38 34 5d   from Gutman[84]
39bb8 2e 0a 2a 2a 20 43 68 6f 6f 73 65 53 75 62 54 72  ..** ChooseSubTr
39bb9 65 65 20 69 6e 20 72 2a 74 72 65 65 20 74 65 72  ee in r*tree ter
39bba 6d 69 6e 6f 6c 6f 67 79 2e 0a 2a 2f 0a 73 74 61  minology..*/.sta
39bbb 74 69 63 20 69 6e 74 20 43 68 6f 6f 73 65 4c 65  tic int ChooseLe
39bbc 61 66 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  af(.  Rtree *pRt
39bbd 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ree,            
39bbe 20 20 20 2f 2a 20 52 74 72 65 65 20 74 61 62 6c     /* Rtree tabl
39bbf 65 20 2a 2f 0a 20 20 52 74 72 65 65 43 65 6c 6c  e */.  RtreeCell
39bc0 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20 20   *pCell,        
39bc1 20 20 20 20 2f 2a 20 43 65 6c 6c 20 74 6f 20 69      /* Cell to i
39bc2 6e 73 65 72 74 20 69 6e 74 6f 20 72 74 72 65 65  nsert into rtree
39bc3 20 2a 2f 0a 20 20 69 6e 74 20 69 48 65 69 67 68   */.  int iHeigh
39bc4 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
39bc5 20 20 20 2f 2a 20 48 65 69 67 68 74 20 6f 66 20     /* Height of 
39bc6 73 75 62 2d 74 72 65 65 20 72 6f 6f 74 65 64 20  sub-tree rooted 
39bc7 61 74 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 52 74  at pCell */.  Rt
39bc8 72 65 65 4e 6f 64 65 20 2a 2a 70 70 4c 65 61 66  reeNode **ppLeaf
39bc9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
39bca 54 3a 20 53 65 6c 65 63 74 65 64 20 6c 65 61 66  T: Selected leaf
39bcb 20 70 61 67 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   page */.){.  in
39bcc 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 69 3b 0a  t rc;.  int ii;.
39bcd 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
39bce 64 65 3b 0a 20 20 72 63 20 3d 20 6e 6f 64 65 41  de;.  rc = nodeA
39bcf 63 71 75 69 72 65 28 70 52 74 72 65 65 2c 20 31  cquire(pRtree, 1
39bd0 2c 20 30 2c 20 26 70 4e 6f 64 65 29 3b 0a 0a 20  , 0, &pNode);.. 
39bd1 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53   for(ii=0; rc==S
39bd2 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 28  QLITE_OK && ii<(
39bd3 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d 69  pRtree->iDepth-i
39bd4 48 65 69 67 68 74 29 3b 20 69 69 2b 2b 29 7b 0a  Height); ii++){.
39bd5 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 3b 0a 20      int iCell;. 
39bd6 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34     sqlite3_int64
39bd7 20 69 42 65 73 74 3b 0a 0a 20 20 20 20 66 6c 6f   iBest;..    flo
39bd8 61 74 20 66 4d 69 6e 47 72 6f 77 74 68 3b 0a 20  at fMinGrowth;. 
39bd9 20 20 20 66 6c 6f 61 74 20 66 4d 69 6e 41 72 65     float fMinAre
39bda 61 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 4d 69  a;.    float fMi
39bdb 6e 4f 76 65 72 6c 61 70 3b 0a 0a 20 20 20 20 69  nOverlap;..    i
39bdc 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c  nt nCell = NCELL
39bdd 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20 52 74 72  (pNode);.    Rtr
39bde 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20  eeCell cell;.   
39bdf 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43 68 69   RtreeNode *pChi
39be0 6c 64 3b 0a 0a 20 20 20 20 52 74 72 65 65 43 65  ld;..    RtreeCe
39be1 6c 6c 20 2a 61 43 65 6c 6c 20 3d 20 30 3b 0a 0a  ll *aCell = 0;..
39be2 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41  #if VARIANT_RSTA
39be3 52 54 52 45 45 5f 43 48 4f 4f 53 45 53 55 42 54  RTREE_CHOOSESUBT
39be4 52 45 45 0a 20 20 20 20 69 66 28 20 69 69 3d 3d  REE.    if( ii==
39be5 28 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d  (pRtree->iDepth-
39be6 31 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  1) ){.      int 
39be7 6a 6a 3b 0a 20 20 20 20 20 20 61 43 65 6c 6c 20  jj;.      aCell 
39be8 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
39be9 28 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c  (sizeof(RtreeCel
39bea 6c 29 2a 6e 43 65 6c 6c 29 3b 0a 20 20 20 20 20  l)*nCell);.     
39beb 20 69 66 28 20 21 61 43 65 6c 6c 20 29 7b 0a 20   if( !aCell ){. 
39bec 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
39bed 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
39bee 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52    nodeRelease(pR
39bef 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20  tree, pNode);.  
39bf0 20 20 20 20 20 20 70 4e 6f 64 65 20 3d 20 30 3b        pNode = 0;
39bf1 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75  .        continu
39bf2 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  e;.      }.     
39bf3 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 6e 43   for(jj=0; jj<nC
39bf4 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20  ell; jj++){.    
39bf5 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28      nodeGetCell(
39bf6 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 6a  pRtree, pNode, j
39bf7 6a 2c 20 26 61 43 65 6c 6c 5b 6a 6a 5d 29 3b 0a  j, &aCell[jj]);.
39bf8 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65        }.    }.#e
39bf9 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 53 65 6c  ndif..    /* Sel
39bfa 65 63 74 20 74 68 65 20 63 68 69 6c 64 20 6e 6f  ect the child no
39bfb 64 65 20 77 68 69 63 68 20 77 69 6c 6c 20 62 65  de which will be
39bfc 20 65 6e 6c 61 72 67 65 64 20 74 68 65 20 6c 65   enlarged the le
39bfd 61 73 74 20 69 66 20 70 43 65 6c 6c 0a 20 20 20  ast if pCell.   
39bfe 20 2a 2a 20 69 73 20 69 6e 73 65 72 74 65 64 20   ** is inserted 
39bff 69 6e 74 6f 20 69 74 2e 20 52 65 73 6f 6c 76 65  into it. Resolve
39c00 20 74 69 65 73 20 62 79 20 63 68 6f 6f 73 69 6e   ties by choosin
39c01 67 20 74 68 65 20 65 6e 74 72 79 20 77 69 74 68  g the entry with
39c02 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 6d 61 6c  .    ** the smal
39c03 6c 65 73 74 20 61 72 65 61 2e 0a 20 20 20 20 2a  lest area..    *
39c04 2f 0a 20 20 20 20 66 6f 72 28 69 43 65 6c 6c 3d  /.    for(iCell=
39c05 30 3b 20 69 43 65 6c 6c 3c 6e 43 65 6c 6c 3b 20  0; iCell<nCell; 
39c06 69 43 65 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20 20  iCell++){.      
39c07 66 6c 6f 61 74 20 67 72 6f 77 74 68 3b 0a 20 20  float growth;.  
39c08 20 20 20 20 66 6c 6f 61 74 20 61 72 65 61 3b 0a      float area;.
39c09 20 20 20 20 20 20 66 6c 6f 61 74 20 6f 76 65 72        float over
39c0a 6c 61 70 20 3d 20 30 2e 30 3b 0a 20 20 20 20 20  lap = 0.0;.     
39c0b 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74   nodeGetCell(pRt
39c0c 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65 6c  ree, pNode, iCel
39c0d 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 20 20 20  l, &cell);.     
39c0e 20 67 72 6f 77 74 68 20 3d 20 63 65 6c 6c 47 72   growth = cellGr
39c0f 6f 77 74 68 28 70 52 74 72 65 65 2c 20 26 63 65  owth(pRtree, &ce
39c10 6c 6c 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20  ll, pCell);.    
39c11 20 20 61 72 65 61 20 3d 20 63 65 6c 6c 41 72 65    area = cellAre
39c12 61 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 29  a(pRtree, &cell)
39c13 3b 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53  ;.#if VARIANT_RS
39c14 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45 53 55  TARTREE_CHOOSESU
39c15 42 54 52 45 45 0a 20 20 20 20 20 20 69 66 28 20  BTREE.      if( 
39c16 69 69 3d 3d 28 70 52 74 72 65 65 2d 3e 69 44 65  ii==(pRtree->iDe
39c17 70 74 68 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  pth-1) ){.      
39c18 20 20 6f 76 65 72 6c 61 70 20 3d 20 63 65 6c 6c    overlap = cell
39c19 4f 76 65 72 6c 61 70 45 6e 6c 61 72 67 65 6d 65  OverlapEnlargeme
39c1a 6e 74 28 70 52 74 72 65 65 2c 26 63 65 6c 6c 2c  nt(pRtree,&cell,
39c1b 70 43 65 6c 6c 2c 61 43 65 6c 6c 2c 6e 43 65 6c  pCell,aCell,nCel
39c1c 6c 2c 69 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20  l,iCell);.      
39c1d 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69  }.#endif.      i
39c1e 66 28 20 28 69 43 65 6c 6c 3d 3d 30 29 20 0a 20  f( (iCell==0) . 
39c1f 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c 61        || (overla
39c20 70 3c 66 4d 69 6e 4f 76 65 72 6c 61 70 29 20 0a  p<fMinOverlap) .
39c21 20 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c         || (overl
39c22 61 70 3d 3d 66 4d 69 6e 4f 76 65 72 6c 61 70 20  ap==fMinOverlap 
39c23 26 26 20 67 72 6f 77 74 68 3c 66 4d 69 6e 47 72  && growth<fMinGr
39c24 6f 77 74 68 29 0a 20 20 20 20 20 20 20 7c 7c 20  owth).       || 
39c25 28 6f 76 65 72 6c 61 70 3d 3d 66 4d 69 6e 4f 76  (overlap==fMinOv
39c26 65 72 6c 61 70 20 26 26 20 67 72 6f 77 74 68 3d  erlap && growth=
39c27 3d 66 4d 69 6e 47 72 6f 77 74 68 20 26 26 20 61  =fMinGrowth && a
39c28 72 65 61 3c 66 4d 69 6e 41 72 65 61 29 0a 20 20  rea<fMinArea).  
39c29 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 66      ){.        f
39c2a 4d 69 6e 4f 76 65 72 6c 61 70 20 3d 20 6f 76 65  MinOverlap = ove
39c2b 72 6c 61 70 3b 0a 20 20 20 20 20 20 20 20 66 4d  rlap;.        fM
39c2c 69 6e 47 72 6f 77 74 68 20 3d 20 67 72 6f 77 74  inGrowth = growt
39c2d 68 3b 0a 20 20 20 20 20 20 20 20 66 4d 69 6e 41  h;.        fMinA
39c2e 72 65 61 20 3d 20 61 72 65 61 3b 0a 20 20 20 20  rea = area;.    
39c2f 20 20 20 20 69 42 65 73 74 20 3d 20 63 65 6c 6c      iBest = cell
39c30 2e 69 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 7d  .iRowid;.      }
39c31 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69  .    }..    sqli
39c32 74 65 33 5f 66 72 65 65 28 61 43 65 6c 6c 29 3b  te3_free(aCell);
39c33 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 41 63  .    rc = nodeAc
39c34 71 75 69 72 65 28 70 52 74 72 65 65 2c 20 69 42  quire(pRtree, iB
39c35 65 73 74 2c 20 70 4e 6f 64 65 2c 20 26 70 43 68  est, pNode, &pCh
39c36 69 6c 64 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65  ild);.    nodeRe
39c37 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4e  lease(pRtree, pN
39c38 6f 64 65 29 3b 0a 20 20 20 20 70 4e 6f 64 65 20  ode);.    pNode 
39c39 3d 20 70 43 68 69 6c 64 3b 0a 20 20 7d 0a 0a 20  = pChild;.  }.. 
39c3a 20 2a 70 70 4c 65 61 66 20 3d 20 70 4e 6f 64 65   *ppLeaf = pNode
39c3b 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
39c3c 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 65 6c 6c 20 77  ../*.** A cell w
39c3d 69 74 68 20 74 68 65 20 73 61 6d 65 20 63 6f 6e  ith the same con
39c3e 74 65 6e 74 20 61 73 20 70 43 65 6c 6c 20 68 61  tent as pCell ha
39c3f 73 20 6a 75 73 74 20 62 65 65 6e 20 69 6e 73 65  s just been inse
39c40 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65  rted into.** the
39c41 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 54 68 69   node pNode. Thi
39c42 73 20 66 75 6e 63 74 69 6f 6e 20 75 70 64 61 74  s function updat
39c43 65 73 20 74 68 65 20 62 6f 75 6e 64 69 6e 67 20  es the bounding 
39c44 62 6f 78 20 63 65 6c 6c 73 20 69 6e 0a 2a 2a 20  box cells in.** 
39c45 61 6c 6c 20 61 6e 63 65 73 74 6f 72 20 65 6c 65  all ancestor ele
39c46 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  ments..*/.static
39c47 20 76 6f 69 64 20 41 64 6a 75 73 74 54 72 65 65   void AdjustTree
39c48 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  (.  Rtree *pRtre
39c49 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
39c4a 20 20 20 20 20 20 2f 2a 20 52 74 72 65 65 20 74        /* Rtree t
39c4b 61 62 6c 65 20 2a 2f 0a 20 20 52 74 72 65 65 4e  able */.  RtreeN
39c4c 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 20 20 20 20  ode *pNode,     
39c4d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
39c4e 64 6a 75 73 74 20 61 6e 63 65 73 74 72 79 20 6f  djust ancestry o
39c4f 66 20 74 68 69 73 20 6e 6f 64 65 2e 20 2a 2f 0a  f this node. */.
39c50 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65    RtreeCell *pCe
39c51 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ll              
39c52 20 20 20 20 2f 2a 20 54 68 69 73 20 63 65 6c 6c      /* This cell
39c53 20 77 61 73 20 6a 75 73 74 20 69 6e 73 65 72 74   was just insert
39c54 65 64 20 2a 2f 0a 29 7b 0a 20 20 52 74 72 65 65  ed */.){.  Rtree
39c55 4e 6f 64 65 20 2a 70 20 3d 20 70 4e 6f 64 65 3b  Node *p = pNode;
39c56 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 50 61  .  while( p->pPa
39c57 72 65 6e 74 20 29 7b 0a 20 20 20 20 52 74 72 65  rent ){.    Rtre
39c58 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20 20  eCell cell;.    
39c59 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65  RtreeNode *pPare
39c5a 6e 74 20 3d 20 70 2d 3e 70 50 61 72 65 6e 74 3b  nt = p->pParent;
39c5b 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 20 3d  .    int iCell =
39c5c 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78   nodeParentIndex
39c5d 28 70 52 74 72 65 65 2c 20 70 29 3b 0a 0a 20 20  (pRtree, p);..  
39c5e 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52    nodeGetCell(pR
39c5f 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 69  tree, pParent, i
39c60 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20  Cell, &cell);.  
39c61 20 20 69 66 28 20 21 63 65 6c 6c 43 6f 6e 74 61    if( !cellConta
39c62 69 6e 73 28 70 52 74 72 65 65 2c 20 26 63 65 6c  ins(pRtree, &cel
39c63 6c 2c 20 70 43 65 6c 6c 29 20 29 7b 0a 20 20 20  l, pCell) ){.   
39c64 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74     cellUnion(pRt
39c65 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70 43 65 6c  ree, &cell, pCel
39c66 6c 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 4f 76  l);.      nodeOv
39c67 65 72 77 72 69 74 65 43 65 6c 6c 28 70 52 74 72  erwriteCell(pRtr
39c68 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 26 63 65  ee, pParent, &ce
39c69 6c 6c 2c 20 69 43 65 6c 6c 29 3b 0a 20 20 20 20  ll, iCell);.    
39c6a 7d 0a 20 0a 20 20 20 20 70 20 3d 20 70 50 61 72  }. .    p = pPar
39c6b 65 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ent;.  }.}../*.*
39c6c 2a 20 57 72 69 74 65 20 6d 61 70 70 69 6e 67 20  * Write mapping 
39c6d 28 69 52 6f 77 69 64 2d 3e 69 4e 6f 64 65 29 20  (iRowid->iNode) 
39c6e 74 6f 20 74 68 65 20 3c 72 74 72 65 65 3e 5f 72  to the <rtree>_r
39c6f 6f 77 69 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73  owid table..*/.s
39c70 74 61 74 69 63 20 69 6e 74 20 72 6f 77 69 64 57  tatic int rowidW
39c71 72 69 74 65 28 52 74 72 65 65 20 2a 70 52 74 72  rite(Rtree *pRtr
39c72 65 65 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ee, sqlite3_int6
39c73 34 20 69 52 6f 77 69 64 2c 20 73 71 6c 69 74 65  4 iRowid, sqlite
39c74 33 5f 69 6e 74 36 34 20 69 4e 6f 64 65 29 7b 0a  3_int64 iNode){.
39c75 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
39c76 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 57 72  nt64(pRtree->pWr
39c77 69 74 65 52 6f 77 69 64 2c 20 31 2c 20 69 52 6f  iteRowid, 1, iRo
39c78 77 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  wid);.  sqlite3_
39c79 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65  bind_int64(pRtre
39c7a 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 2c 20  e->pWriteRowid, 
39c7b 32 2c 20 69 4e 6f 64 65 29 3b 0a 20 20 73 71 6c  2, iNode);.  sql
39c7c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65  ite3_step(pRtree
39c7d 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29 3b 0a  ->pWriteRowid);.
39c7e 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
39c7f 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70  _reset(pRtree->p
39c80 57 72 69 74 65 52 6f 77 69 64 29 3b 0a 7d 0a 0a  WriteRowid);.}..
39c81 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 61 70 70  /*.** Write mapp
39c82 69 6e 67 20 28 69 4e 6f 64 65 2d 3e 69 50 61 72  ing (iNode->iPar
39c83 29 20 74 6f 20 74 68 65 20 3c 72 74 72 65 65 3e  ) to the <rtree>
39c84 5f 70 61 72 65 6e 74 20 74 61 62 6c 65 2e 0a 2a  _parent table..*
39c85 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72  /.static int par
39c86 65 6e 74 57 72 69 74 65 28 52 74 72 65 65 20 2a  entWrite(Rtree *
39c87 70 52 74 72 65 65 2c 20 73 71 6c 69 74 65 33 5f  pRtree, sqlite3_
39c88 69 6e 74 36 34 20 69 4e 6f 64 65 2c 20 73 71 6c  int64 iNode, sql
39c89 69 74 65 33 5f 69 6e 74 36 34 20 69 50 61 72 29  ite3_int64 iPar)
39c8a 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  {.  sqlite3_bind
39c8b 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
39c8c 57 72 69 74 65 50 61 72 65 6e 74 2c 20 31 2c 20  WriteParent, 1, 
39c8d 69 4e 6f 64 65 29 3b 0a 20 20 73 71 6c 69 74 65  iNode);.  sqlite
39c8e 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74  3_bind_int64(pRt
39c8f 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e  ree->pWriteParen
39c90 74 2c 20 32 2c 20 69 50 61 72 29 3b 0a 20 20 73  t, 2, iPar);.  s
39c91 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72  qlite3_step(pRtr
39c92 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74  ee->pWriteParent
39c93 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  );.  return sqli
39c94 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65  te3_reset(pRtree
39c95 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74 29 3b  ->pWriteParent);
39c96 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .}..static int r
39c97 74 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 52  treeInsertCell(R
39c98 74 72 65 65 20 2a 2c 20 52 74 72 65 65 4e 6f 64  tree *, RtreeNod
39c99 65 20 2a 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a  e *, RtreeCell *
39c9a 2c 20 69 6e 74 29 3b 0a 0a 23 69 66 20 56 41 52  , int);..#if VAR
39c9b 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e  IANT_GUTTMAN_LIN
39c9c 45 41 52 5f 53 50 4c 49 54 0a 2f 2a 0a 2a 2a 20  EAR_SPLIT./*.** 
39c9d 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
39c9e 66 20 74 68 65 20 6c 69 6e 65 61 72 20 76 61 72  f the linear var
39c9f 69 61 6e 74 20 6f 66 20 74 68 65 20 50 69 63 6b  iant of the Pick
39ca0 4e 65 78 74 28 29 20 66 75 6e 63 74 69 6f 6e 20  Next() function 
39ca1 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b  from.** Guttman[
39ca2 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52  84]..*/.static R
39ca3 74 72 65 65 43 65 6c 6c 20 2a 4c 69 6e 65 61 72  treeCell *Linear
39ca4 50 69 63 6b 4e 65 78 74 28 0a 20 20 52 74 72 65  PickNext(.  Rtre
39ca5 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72  e *pRtree,.  Rtr
39ca6 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a  eeCell *aCell, .
39ca7 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20    int nCell, .  
39ca8 52 74 72 65 65 43 65 6c 6c 20 2a 70 4c 65 66 74  RtreeCell *pLeft
39ca9 42 6f 78 2c 20 0a 20 20 52 74 72 65 65 43 65 6c  Box, .  RtreeCel
39caa 6c 20 2a 70 52 69 67 68 74 42 6f 78 2c 0a 20 20  l *pRightBox,.  
39cab 69 6e 74 20 2a 61 69 55 73 65 64 0a 29 7b 0a 20  int *aiUsed.){. 
39cac 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28 69   int ii;.  for(i
39cad 69 3d 30 3b 20 61 69 55 73 65 64 5b 69 69 5d 3b  i=0; aiUsed[ii];
39cae 20 69 69 2b 2b 29 3b 0a 20 20 61 69 55 73 65 64   ii++);.  aiUsed
39caf 5b 69 69 5d 20 3d 20 31 3b 0a 20 20 72 65 74 75  [ii] = 1;.  retu
39cb0 72 6e 20 26 61 43 65 6c 6c 5b 69 69 5d 3b 0a 7d  rn &aCell[ii];.}
39cb1 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
39cb2 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69  tation of the li
39cb3 6e 65 61 72 20 76 61 72 69 61 6e 74 20 6f 66 20  near variant of 
39cb4 74 68 65 20 50 69 63 6b 53 65 65 64 73 28 29 20  the PickSeeds() 
39cb5 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a  function from.**
39cb6 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f   Guttman[84]..*/
39cb7 0a 73 74 61 74 69 63 20 76 6f 69 64 20 4c 69 6e  .static void Lin
39cb8 65 61 72 50 69 63 6b 53 65 65 64 73 28 0a 20 20  earPickSeeds(.  
39cb9 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20  Rtree *pRtree,. 
39cba 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c   RtreeCell *aCel
39cbb 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c  l, .  int nCell,
39cbc 20 0a 20 20 69 6e 74 20 2a 70 69 4c 65 66 74 53   .  int *piLeftS
39cbd 65 65 64 2c 20 0a 20 20 69 6e 74 20 2a 70 69 52  eed, .  int *piR
39cbe 69 67 68 74 53 65 65 64 0a 29 7b 0a 20 20 69 6e  ightSeed.){.  in
39cbf 74 20 69 3b 0a 20 20 69 6e 74 20 69 4c 65 66 74  t i;.  int iLeft
39cc0 53 65 65 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20  Seed = 0;.  int 
39cc1 69 52 69 67 68 74 53 65 65 64 20 3d 20 31 3b 0a  iRightSeed = 1;.
39cc2 20 20 66 6c 6f 61 74 20 6d 61 78 4e 6f 72 6d 61    float maxNorma
39cc3 6c 49 6e 6e 65 72 57 69 64 74 68 20 3d 20 30 2e  lInnerWidth = 0.
39cc4 30 3b 0a 0a 20 20 2f 2a 20 50 69 63 6b 20 74 77  0;..  /* Pick tw
39cc5 6f 20 22 73 65 65 64 22 20 63 65 6c 6c 73 20 66  o "seed" cells f
39cc6 72 6f 6d 20 74 68 65 20 61 72 72 61 79 20 6f 66  rom the array of
39cc7 20 63 65 6c 6c 73 2e 20 54 68 65 20 61 6c 67 6f   cells. The algo
39cc8 72 69 74 68 6d 20 75 73 65 64 0a 20 20 2a 2a 20  rithm used.  ** 
39cc9 68 65 72 65 20 69 73 20 74 68 65 20 4c 69 6e 65  here is the Line
39cca 61 72 50 69 63 6b 53 65 65 64 73 20 61 6c 67 6f  arPickSeeds algo
39ccb 72 69 74 68 6d 20 66 72 6f 6d 20 47 75 74 6d 61  rithm from Gutma
39ccc 6e 5b 31 39 38 34 5d 2e 20 54 68 65 20 0a 20 20  n[1984]. The .  
39ccd 2a 2a 20 69 6e 64 69 63 65 73 20 6f 66 20 74 68  ** indices of th
39cce 65 20 74 77 6f 20 73 65 65 64 20 63 65 6c 6c 73  e two seed cells
39ccf 20 69 6e 20 74 68 65 20 61 72 72 61 79 20 61 72   in the array ar
39cd0 65 20 73 74 6f 72 65 64 20 69 6e 20 6c 6f 63 61  e stored in loca
39cd1 6c 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 73  l.  ** variables
39cd2 20 69 4c 65 66 74 53 65 65 6b 20 61 6e 64 20 69   iLeftSeek and i
39cd3 52 69 67 68 74 53 65 65 64 2e 0a 20 20 2a 2f 0a  RightSeed..  */.
39cd4 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 52 74    for(i=0; i<pRt
39cd5 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 2b 2b 29 7b  ree->nDim; i++){
39cd6 0a 20 20 20 20 66 6c 6f 61 74 20 78 31 20 3d 20  .    float x1 = 
39cd7 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 30 5d 2e  DCOORD(aCell[0].
39cd8 61 43 6f 6f 72 64 5b 69 2a 32 5d 29 3b 0a 20 20  aCoord[i*2]);.  
39cd9 20 20 66 6c 6f 61 74 20 78 32 20 3d 20 44 43 4f    float x2 = DCO
39cda 4f 52 44 28 61 43 65 6c 6c 5b 30 5d 2e 61 43 6f  ORD(aCell[0].aCo
39cdb 6f 72 64 5b 69 2a 32 2b 31 5d 29 3b 0a 20 20 20  ord[i*2+1]);.   
39cdc 20 66 6c 6f 61 74 20 78 33 20 3d 20 78 31 3b 0a   float x3 = x1;.
39cdd 20 20 20 20 66 6c 6f 61 74 20 78 34 20 3d 20 78      float x4 = x
39cde 32 3b 0a 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 0a  2;.    int jj;..
39cdf 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 4c 65 66      int iCellLef
39ce0 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 69  t = 0;.    int i
39ce1 43 65 6c 6c 52 69 67 68 74 20 3d 20 30 3b 0a 0a  CellRight = 0;..
39ce2 20 20 20 20 66 6f 72 28 6a 6a 3d 31 3b 20 6a 6a      for(jj=1; jj
39ce3 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20  <nCell; jj++){. 
39ce4 20 20 20 20 20 66 6c 6f 61 74 20 6c 65 66 74 20       float left 
39ce5 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 6a  = DCOORD(aCell[j
39ce6 6a 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32 5d 29 3b  j].aCoord[i*2]);
39ce7 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 72 69 67  .      float rig
39ce8 68 74 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c  ht = DCOORD(aCel
39ce9 6c 5b 6a 6a 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32  l[jj].aCoord[i*2
39cea 2b 31 5d 29 3b 0a 0a 20 20 20 20 20 20 69 66 28  +1]);..      if(
39ceb 20 6c 65 66 74 3c 78 31 20 29 20 78 31 20 3d 20   left<x1 ) x1 = 
39cec 6c 65 66 74 3b 0a 20 20 20 20 20 20 69 66 28 20  left;.      if( 
39ced 72 69 67 68 74 3e 78 34 20 29 20 78 34 20 3d 20  right>x4 ) x4 = 
39cee 72 69 67 68 74 3b 0a 20 20 20 20 20 20 69 66 28  right;.      if(
39cef 20 6c 65 66 74 3e 78 33 20 29 7b 0a 20 20 20 20   left>x3 ){.    
39cf0 20 20 20 20 78 33 20 3d 20 6c 65 66 74 3b 0a 20      x3 = left;. 
39cf1 20 20 20 20 20 20 20 69 43 65 6c 6c 52 69 67 68         iCellRigh
39cf2 74 20 3d 20 6a 6a 3b 0a 20 20 20 20 20 20 7d 0a  t = jj;.      }.
39cf3 20 20 20 20 20 20 69 66 28 20 72 69 67 68 74 3c        if( right<
39cf4 78 32 20 29 7b 0a 20 20 20 20 20 20 20 20 78 32  x2 ){.        x2
39cf5 20 3d 20 72 69 67 68 74 3b 0a 20 20 20 20 20 20   = right;.      
39cf6 20 20 69 43 65 6c 6c 4c 65 66 74 20 3d 20 6a 6a    iCellLeft = jj
39cf7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
39cf8 0a 20 20 20 20 69 66 28 20 78 34 21 3d 78 31 20  .    if( x4!=x1 
39cf9 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 6e  ){.      float n
39cfa 6f 72 6d 61 6c 77 69 64 74 68 20 3d 20 28 78 33  ormalwidth = (x3
39cfb 20 2d 20 78 32 29 20 2f 20 28 78 34 20 2d 20 78   - x2) / (x4 - x
39cfc 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 6f  1);.      if( no
39cfd 72 6d 61 6c 77 69 64 74 68 3e 6d 61 78 4e 6f 72  rmalwidth>maxNor
39cfe 6d 61 6c 49 6e 6e 65 72 57 69 64 74 68 20 29 7b  malInnerWidth ){
39cff 0a 20 20 20 20 20 20 20 20 69 4c 65 66 74 53 65  .        iLeftSe
39d00 65 64 20 3d 20 69 43 65 6c 6c 4c 65 66 74 3b 0a  ed = iCellLeft;.
39d01 20 20 20 20 20 20 20 20 69 52 69 67 68 74 53 65          iRightSe
39d02 65 64 20 3d 20 69 43 65 6c 6c 52 69 67 68 74 3b  ed = iCellRight;
39d03 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
39d04 20 7d 0a 0a 20 20 2a 70 69 4c 65 66 74 53 65 65   }..  *piLeftSee
39d05 64 20 3d 20 69 4c 65 66 74 53 65 65 64 3b 0a 20  d = iLeftSeed;. 
39d06 20 2a 70 69 52 69 67 68 74 53 65 65 64 20 3d 20   *piRightSeed = 
39d07 69 52 69 67 68 74 53 65 65 64 3b 0a 7d 0a 23 65  iRightSeed;.}.#e
39d08 6e 64 69 66 20 2f 2a 20 56 41 52 49 41 4e 54 5f  ndif /* VARIANT_
39d09 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53  GUTTMAN_LINEAR_S
39d0a 50 4c 49 54 20 2a 2f 0a 0a 23 69 66 20 56 41 52  PLIT */..#if VAR
39d0b 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55 41  IANT_GUTTMAN_QUA
39d0c 44 52 41 54 49 43 5f 53 50 4c 49 54 0a 2f 2a 0a  DRATIC_SPLIT./*.
39d0d 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
39d0e 6e 20 6f 66 20 74 68 65 20 71 75 61 64 72 61 74  n of the quadrat
39d0f 69 63 20 76 61 72 69 61 6e 74 20 6f 66 20 74 68  ic variant of th
39d10 65 20 50 69 63 6b 4e 65 78 74 28 29 20 66 75 6e  e PickNext() fun
39d11 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 47 75  ction from.** Gu
39d12 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f 0a 73 74  ttman[84]..*/.st
39d13 61 74 69 63 20 52 74 72 65 65 43 65 6c 6c 20 2a  atic RtreeCell *
39d14 51 75 61 64 72 61 74 69 63 50 69 63 6b 4e 65 78  QuadraticPickNex
39d15 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  t(.  Rtree *pRtr
39d16 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  ee,.  RtreeCell 
39d17 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e  *aCell, .  int n
39d18 43 65 6c 6c 2c 20 0a 20 20 52 74 72 65 65 43 65  Cell, .  RtreeCe
39d19 6c 6c 20 2a 70 4c 65 66 74 42 6f 78 2c 20 0a 20  ll *pLeftBox, . 
39d1a 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 52 69 67   RtreeCell *pRig
39d1b 68 74 42 6f 78 2c 0a 20 20 69 6e 74 20 2a 61 69  htBox,.  int *ai
39d1c 55 73 65 64 0a 29 7b 0a 20 20 23 64 65 66 69 6e  Used.){.  #defin
39d1d 65 20 46 41 42 53 28 61 29 20 28 28 61 29 3c 30  e FABS(a) ((a)<0
39d1e 2e 30 3f 2d 31 2e 30 2a 28 61 29 3a 28 61 29 29  .0?-1.0*(a):(a))
39d1f 0a 0a 20 20 69 6e 74 20 69 53 65 6c 65 63 74 20  ..  int iSelect 
39d20 3d 20 2d 31 3b 0a 20 20 66 6c 6f 61 74 20 66 44  = -1;.  float fD
39d21 69 66 66 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  iff;.  int ii;. 
39d22 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43   for(ii=0; ii<nC
39d23 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  ell; ii++){.    
39d24 69 66 28 20 61 69 55 73 65 64 5b 69 69 5d 3d 3d  if( aiUsed[ii]==
39d25 30 20 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74  0 ){.      float
39d26 20 6c 65 66 74 20 3d 20 63 65 6c 6c 47 72 6f 77   left = cellGrow
39d27 74 68 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74  th(pRtree, pLeft
39d28 42 6f 78 2c 20 26 61 43 65 6c 6c 5b 69 69 5d 29  Box, &aCell[ii])
39d29 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 72 69  ;.      float ri
39d2a 67 68 74 20 3d 20 63 65 6c 6c 47 72 6f 77 74 68  ght = cellGrowth
39d2b 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 42 6f  (pRtree, pLeftBo
39d2c 78 2c 20 26 61 43 65 6c 6c 5b 69 69 5d 29 3b 0a  x, &aCell[ii]);.
39d2d 20 20 20 20 20 20 66 6c 6f 61 74 20 64 69 66 66        float diff
39d2e 20 3d 20 46 41 42 53 28 72 69 67 68 74 2d 6c 65   = FABS(right-le
39d2f 66 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69  ft);.      if( i
39d30 53 65 6c 65 63 74 3c 30 20 7c 7c 20 64 69 66 66  Select<0 || diff
39d31 3e 66 44 69 66 66 20 29 7b 0a 20 20 20 20 20 20  >fDiff ){.      
39d32 20 20 66 44 69 66 66 20 3d 20 64 69 66 66 3b 0a    fDiff = diff;.
39d33 20 20 20 20 20 20 20 20 69 53 65 6c 65 63 74 20          iSelect 
39d34 3d 20 69 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20  = ii;.      }.  
39d35 20 20 7d 0a 20 20 7d 0a 20 20 61 69 55 73 65 64    }.  }.  aiUsed
39d36 5b 69 53 65 6c 65 63 74 5d 20 3d 20 31 3b 0a 20  [iSelect] = 1;. 
39d37 20 72 65 74 75 72 6e 20 26 61 43 65 6c 6c 5b 69   return &aCell[i
39d38 53 65 6c 65 63 74 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a  Select];.}../*.*
39d39 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
39d3a 20 6f 66 20 74 68 65 20 71 75 61 64 72 61 74 69   of the quadrati
39d3b 63 20 76 61 72 69 61 6e 74 20 6f 66 20 74 68 65  c variant of the
39d3c 20 50 69 63 6b 53 65 65 64 73 28 29 20 66 75 6e   PickSeeds() fun
39d3d 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 47 75  ction from.** Gu
39d3e 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f 0a 73 74  ttman[84]..*/.st
39d3f 61 74 69 63 20 76 6f 69 64 20 51 75 61 64 72 61  atic void Quadra
39d40 74 69 63 50 69 63 6b 53 65 65 64 73 28 0a 20 20  ticPickSeeds(.  
39d41 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20  Rtree *pRtree,. 
39d42 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c   RtreeCell *aCel
39d43 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c  l, .  int nCell,
39d44 20 0a 20 20 69 6e 74 20 2a 70 69 4c 65 66 74 53   .  int *piLeftS
39d45 65 65 64 2c 20 0a 20 20 69 6e 74 20 2a 70 69 52  eed, .  int *piR
39d46 69 67 68 74 53 65 65 64 0a 29 7b 0a 20 20 69 6e  ightSeed.){.  in
39d47 74 20 69 69 3b 0a 20 20 69 6e 74 20 6a 6a 3b 0a  t ii;.  int jj;.
39d48 0a 20 20 69 6e 74 20 69 4c 65 66 74 53 65 65 64  .  int iLeftSeed
39d49 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 52 69 67   = 0;.  int iRig
39d4a 68 74 53 65 65 64 20 3d 20 31 3b 0a 20 20 66 6c  htSeed = 1;.  fl
39d4b 6f 61 74 20 66 57 61 73 74 65 20 3d 20 30 2e 30  oat fWaste = 0.0
39d4c 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  ;..  for(ii=0; i
39d4d 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a  i<nCell; ii++){.
39d4e 20 20 20 20 66 6f 72 28 6a 6a 3d 69 69 2b 31 3b      for(jj=ii+1;
39d4f 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29   jj<nCell; jj++)
39d50 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 72 69  {.      float ri
39d51 67 68 74 20 3d 20 63 65 6c 6c 41 72 65 61 28 70  ght = cellArea(p
39d52 52 74 72 65 65 2c 20 26 61 43 65 6c 6c 5b 6a 6a  Rtree, &aCell[jj
39d53 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  ]);.      float 
39d54 67 72 6f 77 74 68 20 3d 20 63 65 6c 6c 47 72 6f  growth = cellGro
39d55 77 74 68 28 70 52 74 72 65 65 2c 20 26 61 43 65  wth(pRtree, &aCe
39d56 6c 6c 5b 69 69 5d 2c 20 26 61 43 65 6c 6c 5b 6a  ll[ii], &aCell[j
39d57 6a 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74  j]);.      float
39d58 20 77 61 73 74 65 20 3d 20 67 72 6f 77 74 68 20   waste = growth 
39d59 2d 20 72 69 67 68 74 3b 0a 0a 20 20 20 20 20 20  - right;..      
39d5a 69 66 28 20 77 61 73 74 65 3e 66 57 61 73 74 65  if( waste>fWaste
39d5b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4c 65 66   ){.        iLef
39d5c 74 53 65 65 64 20 3d 20 69 69 3b 0a 20 20 20 20  tSeed = ii;.    
39d5d 20 20 20 20 69 52 69 67 68 74 53 65 65 64 20 3d      iRightSeed =
39d5e 20 6a 6a 3b 0a 20 20 20 20 20 20 20 20 66 57 61   jj;.        fWa
39d5f 73 74 65 20 3d 20 77 61 73 74 65 3b 0a 20 20 20  ste = waste;.   
39d60 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
39d61 20 20 2a 70 69 4c 65 66 74 53 65 65 64 20 3d 20    *piLeftSeed = 
39d62 69 4c 65 66 74 53 65 65 64 3b 0a 20 20 2a 70 69  iLeftSeed;.  *pi
39d63 52 69 67 68 74 53 65 65 64 20 3d 20 69 52 69 67  RightSeed = iRig
39d64 68 74 53 65 65 64 3b 0a 7d 0a 23 65 6e 64 69 66  htSeed;.}.#endif
39d65 20 2f 2a 20 56 41 52 49 41 4e 54 5f 47 55 54 54   /* VARIANT_GUTT
39d66 4d 41 4e 5f 51 55 41 44 52 41 54 49 43 5f 53 50  MAN_QUADRATIC_SP
39d67 4c 49 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 72  LIT */../*.** Ar
39d68 67 75 6d 65 6e 74 73 20 61 49 64 78 2c 20 61 44  guments aIdx, aD
39d69 69 73 74 61 6e 63 65 20 61 6e 64 20 61 53 70 61  istance and aSpa
39d6a 72 65 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f 20  re all point to 
39d6b 61 72 72 61 79 73 20 6f 66 20 73 69 7a 65 0a 2a  arrays of size.*
39d6c 2a 20 6e 49 64 78 2e 20 54 68 65 20 61 49 64 78  * nIdx. The aIdx
39d6d 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20   array contains 
39d6e 74 68 65 20 73 65 74 20 6f 66 20 69 6e 74 65 67  the set of integ
39d6f 65 72 73 20 66 72 6f 6d 20 30 20 74 6f 20 0a 2a  ers from 0 to .*
39d70 2a 20 28 6e 49 64 78 2d 31 29 20 69 6e 20 6e 6f  * (nIdx-1) in no
39d71 20 70 61 72 74 69 63 75 6c 61 72 20 6f 72 64 65   particular orde
39d72 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  r. This function
39d73 20 73 6f 72 74 73 20 74 68 65 20 76 61 6c 75 65   sorts the value
39d74 73 0a 2a 2a 20 69 6e 20 61 49 64 78 20 61 63 63  s.** in aIdx acc
39d75 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 69 6e  ording to the in
39d76 64 65 78 65 64 20 76 61 6c 75 65 73 20 69 6e 20  dexed values in 
39d77 61 44 69 73 74 61 6e 63 65 2e 20 46 6f 72 0a 2a  aDistance. For.*
39d78 2a 20 65 78 61 6d 70 6c 65 2c 20 61 73 73 75 6d  * example, assum
39d79 69 6e 67 20 74 68 65 20 69 6e 70 75 74 73 3a 0a  ing the inputs:.
39d7a 2a 2a 0a 2a 2a 20 20 20 61 49 64 78 20 20 20 20  **.**   aIdx    
39d7b 20 20 3d 20 7b 20 30 2c 20 20 20 31 2c 20 20 20    = { 0,   1,   
39d7c 32 2c 20 20 20 33 20 7d 0a 2a 2a 20 20 20 61 44  2,   3 }.**   aD
39d7d 69 73 74 61 6e 63 65 20 3d 20 7b 20 35 2e 30 2c  istance = { 5.0,
39d7e 20 32 2e 30 2c 20 37 2e 30 2c 20 36 2e 30 20 7d   2.0, 7.0, 6.0 }
39d7f 0a 2a 2a 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63  .**.** this func
39d80 74 69 6f 6e 20 73 65 74 73 20 74 68 65 20 61 49  tion sets the aI
39d81 64 78 20 61 72 72 61 79 20 74 6f 20 63 6f 6e 74  dx array to cont
39d82 61 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 49 64  ain:.**.**   aId
39d83 78 20 20 20 20 20 20 3d 20 7b 20 30 2c 20 20 20  x      = { 0,   
39d84 31 2c 20 20 20 32 2c 20 20 20 33 20 7d 0a 2a 2a  1,   2,   3 }.**
39d85 0a 2a 2a 20 54 68 65 20 61 53 70 61 72 65 20 61  .** The aSpare a
39d86 72 72 61 79 20 69 73 20 75 73 65 64 20 61 73 20  rray is used as 
39d87 74 65 6d 70 6f 72 61 72 79 20 77 6f 72 6b 69 6e  temporary workin
39d88 67 20 73 70 61 63 65 20 62 79 20 74 68 65 0a 2a  g space by the.*
39d89 2a 20 73 6f 72 74 69 6e 67 20 61 6c 67 6f 72 69  * sorting algori
39d8a 74 68 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  thm..*/.static v
39d8b 6f 69 64 20 53 6f 72 74 42 79 44 69 73 74 61 6e  oid SortByDistan
39d8c 63 65 28 0a 20 20 69 6e 74 20 2a 61 49 64 78 2c  ce(.  int *aIdx,
39d8d 20 0a 20 20 69 6e 74 20 6e 49 64 78 2c 20 0a 20   .  int nIdx, . 
39d8e 20 66 6c 6f 61 74 20 2a 61 44 69 73 74 61 6e 63   float *aDistanc
39d8f 65 2c 20 0a 20 20 69 6e 74 20 2a 61 53 70 61 72  e, .  int *aSpar
39d90 65 0a 29 7b 0a 20 20 69 66 28 20 6e 49 64 78 3e  e.){.  if( nIdx>
39d91 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 4c 65  1 ){.    int iLe
39d92 66 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  ft = 0;.    int 
39d93 69 52 69 67 68 74 20 3d 20 30 3b 0a 0a 20 20 20  iRight = 0;..   
39d94 20 69 6e 74 20 6e 4c 65 66 74 20 3d 20 6e 49 64   int nLeft = nId
39d95 78 2f 32 3b 0a 20 20 20 20 69 6e 74 20 6e 52 69  x/2;.    int nRi
39d96 67 68 74 20 3d 20 6e 49 64 78 2d 6e 4c 65 66 74  ght = nIdx-nLeft
39d97 3b 0a 20 20 20 20 69 6e 74 20 2a 61 4c 65 66 74  ;.    int *aLeft
39d98 20 3d 20 61 49 64 78 3b 0a 20 20 20 20 69 6e 74   = aIdx;.    int
39d99 20 2a 61 52 69 67 68 74 20 3d 20 26 61 49 64 78   *aRight = &aIdx
39d9a 5b 6e 4c 65 66 74 5d 3b 0a 0a 20 20 20 20 53 6f  [nLeft];..    So
39d9b 72 74 42 79 44 69 73 74 61 6e 63 65 28 61 4c 65  rtByDistance(aLe
39d9c 66 74 2c 20 6e 4c 65 66 74 2c 20 61 44 69 73 74  ft, nLeft, aDist
39d9d 61 6e 63 65 2c 20 61 53 70 61 72 65 29 3b 0a 20  ance, aSpare);. 
39d9e 20 20 20 53 6f 72 74 42 79 44 69 73 74 61 6e 63     SortByDistanc
39d9f 65 28 61 52 69 67 68 74 2c 20 6e 52 69 67 68 74  e(aRight, nRight
39da0 2c 20 61 44 69 73 74 61 6e 63 65 2c 20 61 53 70  , aDistance, aSp
39da1 61 72 65 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70  are);..    memcp
39da2 79 28 61 53 70 61 72 65 2c 20 61 4c 65 66 74 2c  y(aSpare, aLeft,
39da3 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 4c 65   sizeof(int)*nLe
39da4 66 74 29 3b 0a 20 20 20 20 61 4c 65 66 74 20 3d  ft);.    aLeft =
39da5 20 61 53 70 61 72 65 3b 0a 0a 20 20 20 20 77 68   aSpare;..    wh
39da6 69 6c 65 28 20 69 4c 65 66 74 3c 6e 4c 65 66 74  ile( iLeft<nLeft
39da7 20 7c 7c 20 69 52 69 67 68 74 3c 6e 52 69 67 68   || iRight<nRigh
39da8 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69  t ){.      if( i
39da9 4c 65 66 74 3d 3d 6e 4c 65 66 74 20 29 7b 0a 20  Left==nLeft ){. 
39daa 20 20 20 20 20 20 20 61 49 64 78 5b 69 4c 65 66         aIdx[iLef
39dab 74 2b 69 52 69 67 68 74 5d 20 3d 20 61 52 69 67  t+iRight] = aRig
39dac 68 74 5b 69 52 69 67 68 74 5d 3b 0a 20 20 20 20  ht[iRight];.    
39dad 20 20 20 20 69 52 69 67 68 74 2b 2b 3b 0a 20 20      iRight++;.  
39dae 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 52      }else if( iR
39daf 69 67 68 74 3d 3d 6e 52 69 67 68 74 20 29 7b 0a  ight==nRight ){.
39db0 20 20 20 20 20 20 20 20 61 49 64 78 5b 69 4c 65          aIdx[iLe
39db1 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61 4c 65  ft+iRight] = aLe
39db2 66 74 5b 69 4c 65 66 74 5d 3b 0a 20 20 20 20 20  ft[iLeft];.     
39db3 20 20 20 69 4c 65 66 74 2b 2b 3b 0a 20 20 20 20     iLeft++;.    
39db4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
39db5 20 66 6c 6f 61 74 20 66 4c 65 66 74 20 3d 20 61   float fLeft = a
39db6 44 69 73 74 61 6e 63 65 5b 61 4c 65 66 74 5b 69  Distance[aLeft[i
39db7 4c 65 66 74 5d 5d 3b 0a 20 20 20 20 20 20 20 20  Left]];.        
39db8 66 6c 6f 61 74 20 66 52 69 67 68 74 20 3d 20 61  float fRight = a
39db9 44 69 73 74 61 6e 63 65 5b 61 52 69 67 68 74 5b  Distance[aRight[
39dba 69 52 69 67 68 74 5d 5d 3b 0a 20 20 20 20 20 20  iRight]];.      
39dbb 20 20 69 66 28 20 66 4c 65 66 74 3c 66 52 69 67    if( fLeft<fRig
39dbc 68 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ht ){.          
39dbd 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67 68  aIdx[iLeft+iRigh
39dbe 74 5d 20 3d 20 61 4c 65 66 74 5b 69 4c 65 66 74  t] = aLeft[iLeft
39dbf 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 4c 65  ];.          iLe
39dc0 66 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65  ft++;.        }e
39dc1 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61  lse{.          a
39dc2 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67 68 74  Idx[iLeft+iRight
39dc3 5d 20 3d 20 61 52 69 67 68 74 5b 69 52 69 67 68  ] = aRight[iRigh
39dc4 74 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 52  t];.          iR
39dc5 69 67 68 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20  ight++;.        
39dc6 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
39dc7 0a 23 69 66 20 30 0a 20 20 20 20 2f 2a 20 43 68  .#if 0.    /* Ch
39dc8 65 63 6b 20 74 68 61 74 20 74 68 65 20 73 6f 72  eck that the sor
39dc9 74 20 77 6f 72 6b 65 64 20 2a 2f 0a 20 20 20 20  t worked */.    
39dca 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a  {.      int jj;.
39dcb 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d 31 3b 20        for(jj=1; 
39dcc 6a 6a 3c 6e 49 64 78 3b 20 6a 6a 2b 2b 29 7b 0a  jj<nIdx; jj++){.
39dcd 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20 6c 65          float le
39dce 66 74 20 3d 20 61 44 69 73 74 61 6e 63 65 5b 61  ft = aDistance[a
39dcf 49 64 78 5b 6a 6a 2d 31 5d 5d 3b 0a 20 20 20 20  Idx[jj-1]];.    
39dd0 20 20 20 20 66 6c 6f 61 74 20 72 69 67 68 74 20      float right 
39dd1 3d 20 61 44 69 73 74 61 6e 63 65 5b 61 49 64 78  = aDistance[aIdx
39dd2 5b 6a 6a 5d 5d 3b 0a 20 20 20 20 20 20 20 20 61  [jj]];.        a
39dd3 73 73 65 72 74 28 20 6c 65 66 74 3c 3d 72 69 67  ssert( left<=rig
39dd4 68 74 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ht );.      }.  
39dd5 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d    }.#endif.  }.}
39dd6 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ../*.** Argument
39dd7 73 20 61 49 64 78 2c 20 61 43 65 6c 6c 20 61 6e  s aIdx, aCell an
39dd8 64 20 61 53 70 61 72 65 20 61 6c 6c 20 70 6f 69  d aSpare all poi
39dd9 6e 74 20 74 6f 20 61 72 72 61 79 73 20 6f 66 20  nt to arrays of 
39dda 73 69 7a 65 0a 2a 2a 20 6e 49 64 78 2e 20 54 68  size.** nIdx. Th
39ddb 65 20 61 49 64 78 20 61 72 72 61 79 20 63 6f 6e  e aIdx array con
39ddc 74 61 69 6e 73 20 74 68 65 20 73 65 74 20 6f 66  tains the set of
39ddd 20 69 6e 74 65 67 65 72 73 20 66 72 6f 6d 20 30   integers from 0
39dde 20 74 6f 20 0a 2a 2a 20 28 6e 49 64 78 2d 31 29   to .** (nIdx-1)
39ddf 20 69 6e 20 6e 6f 20 70 61 72 74 69 63 75 6c 61   in no particula
39de0 72 20 6f 72 64 65 72 2e 20 54 68 69 73 20 66 75  r order. This fu
39de1 6e 63 74 69 6f 6e 20 73 6f 72 74 73 20 74 68 65  nction sorts the
39de2 20 76 61 6c 75 65 73 0a 2a 2a 20 69 6e 20 61 49   values.** in aI
39de3 64 78 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20  dx according to 
39de4 64 69 6d 65 6e 73 69 6f 6e 20 69 44 69 6d 20 6f  dimension iDim o
39de5 66 20 74 68 65 20 63 65 6c 6c 73 20 69 6e 20 61  f the cells in a
39de6 43 65 6c 6c 2e 20 54 68 65 0a 2a 2a 20 6d 69 6e  Cell. The.** min
39de7 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20 64 69  imum value of di
39de8 6d 65 6e 73 69 6f 6e 20 69 44 69 6d 20 69 73 20  mension iDim is 
39de9 63 6f 6e 73 69 64 65 72 65 64 20 66 69 72 73 74  considered first
39dea 2c 20 74 68 65 0a 2a 2a 20 6d 61 78 69 6d 75 6d  , the.** maximum
39deb 20 75 73 65 64 20 74 6f 20 62 72 65 61 6b 20 74   used to break t
39dec 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  ies..**.** The a
39ded 53 70 61 72 65 20 61 72 72 61 79 20 69 73 20 75  Spare array is u
39dee 73 65 64 20 61 73 20 74 65 6d 70 6f 72 61 72 79  sed as temporary
39def 20 77 6f 72 6b 69 6e 67 20 73 70 61 63 65 20 62   working space b
39df0 79 20 74 68 65 0a 2a 2a 20 73 6f 72 74 69 6e 67  y the.** sorting
39df1 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 2a 2f 0a 73   algorithm..*/.s
39df2 74 61 74 69 63 20 76 6f 69 64 20 53 6f 72 74 42  tatic void SortB
39df3 79 44 69 6d 65 6e 73 69 6f 6e 28 0a 20 20 52 74  yDimension(.  Rt
39df4 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 69  ree *pRtree,.  i
39df5 6e 74 20 2a 61 49 64 78 2c 20 0a 20 20 69 6e 74  nt *aIdx, .  int
39df6 20 6e 49 64 78 2c 20 0a 20 20 69 6e 74 20 69 44   nIdx, .  int iD
39df7 69 6d 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c  im, .  RtreeCell
39df8 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20   *aCell, .  int 
39df9 2a 61 53 70 61 72 65 0a 29 7b 0a 20 20 69 66 28  *aSpare.){.  if(
39dfa 20 6e 49 64 78 3e 31 20 29 7b 0a 0a 20 20 20 20   nIdx>1 ){..    
39dfb 69 6e 74 20 69 4c 65 66 74 20 3d 20 30 3b 0a 20  int iLeft = 0;. 
39dfc 20 20 20 69 6e 74 20 69 52 69 67 68 74 20 3d 20     int iRight = 
39dfd 30 3b 0a 0a 20 20 20 20 69 6e 74 20 6e 4c 65 66  0;..    int nLef
39dfe 74 20 3d 20 6e 49 64 78 2f 32 3b 0a 20 20 20 20  t = nIdx/2;.    
39dff 69 6e 74 20 6e 52 69 67 68 74 20 3d 20 6e 49 64  int nRight = nId
39e00 78 2d 6e 4c 65 66 74 3b 0a 20 20 20 20 69 6e 74  x-nLeft;.    int
39e01 20 2a 61 4c 65 66 74 20 3d 20 61 49 64 78 3b 0a   *aLeft = aIdx;.
39e02 20 20 20 20 69 6e 74 20 2a 61 52 69 67 68 74 20      int *aRight 
39e03 3d 20 26 61 49 64 78 5b 6e 4c 65 66 74 5d 3b 0a  = &aIdx[nLeft];.
39e04 0a 20 20 20 20 53 6f 72 74 42 79 44 69 6d 65 6e  .    SortByDimen
39e05 73 69 6f 6e 28 70 52 74 72 65 65 2c 20 61 4c 65  sion(pRtree, aLe
39e06 66 74 2c 20 6e 4c 65 66 74 2c 20 69 44 69 6d 2c  ft, nLeft, iDim,
39e07 20 61 43 65 6c 6c 2c 20 61 53 70 61 72 65 29 3b   aCell, aSpare);
39e08 0a 20 20 20 20 53 6f 72 74 42 79 44 69 6d 65 6e  .    SortByDimen
39e09 73 69 6f 6e 28 70 52 74 72 65 65 2c 20 61 52 69  sion(pRtree, aRi
39e0a 67 68 74 2c 20 6e 52 69 67 68 74 2c 20 69 44 69  ght, nRight, iDi
39e0b 6d 2c 20 61 43 65 6c 6c 2c 20 61 53 70 61 72 65  m, aCell, aSpare
39e0c 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28 61  );..    memcpy(a
39e0d 53 70 61 72 65 2c 20 61 4c 65 66 74 2c 20 73 69  Spare, aLeft, si
39e0e 7a 65 6f 66 28 69 6e 74 29 2a 6e 4c 65 66 74 29  zeof(int)*nLeft)
39e0f 3b 0a 20 20 20 20 61 4c 65 66 74 20 3d 20 61 53  ;.    aLeft = aS
39e10 70 61 72 65 3b 0a 20 20 20 20 77 68 69 6c 65 28  pare;.    while(
39e11 20 69 4c 65 66 74 3c 6e 4c 65 66 74 20 7c 7c 20   iLeft<nLeft || 
39e12 69 52 69 67 68 74 3c 6e 52 69 67 68 74 20 29 7b  iRight<nRight ){
39e13 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 78 6c  .      double xl
39e14 65 66 74 31 20 3d 20 44 43 4f 4f 52 44 28 61 43  eft1 = DCOORD(aC
39e15 65 6c 6c 5b 61 4c 65 66 74 5b 69 4c 65 66 74 5d  ell[aLeft[iLeft]
39e16 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d  ].aCoord[iDim*2]
39e17 29 3b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20  );.      double 
39e18 78 6c 65 66 74 32 20 3d 20 44 43 4f 4f 52 44 28  xleft2 = DCOORD(
39e19 61 43 65 6c 6c 5b 61 4c 65 66 74 5b 69 4c 65 66  aCell[aLeft[iLef
39e1a 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a  t]].aCoord[iDim*
39e1b 32 2b 31 5d 29 3b 0a 20 20 20 20 20 20 64 6f 75  2+1]);.      dou
39e1c 62 6c 65 20 78 72 69 67 68 74 31 20 3d 20 44 43  ble xright1 = DC
39e1d 4f 4f 52 44 28 61 43 65 6c 6c 5b 61 52 69 67 68  OORD(aCell[aRigh
39e1e 74 5b 69 52 69 67 68 74 5d 5d 2e 61 43 6f 6f 72  t[iRight]].aCoor
39e1f 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20 20 20 20  d[iDim*2]);.    
39e20 20 20 64 6f 75 62 6c 65 20 78 72 69 67 68 74 32    double xright2
39e21 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b   = DCOORD(aCell[
39e22 61 52 69 67 68 74 5b 69 52 69 67 68 74 5d 5d 2e  aRight[iRight]].
39e23 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d  aCoord[iDim*2+1]
39e24 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 69 4c  );.      if( (iL
39e25 65 66 74 21 3d 6e 4c 65 66 74 29 20 26 26 20 28  eft!=nLeft) && (
39e26 28 69 52 69 67 68 74 3d 3d 6e 52 69 67 68 74 29  (iRight==nRight)
39e27 0a 20 20 20 20 20 20 20 7c 7c 20 28 78 6c 65 66  .       || (xlef
39e28 74 31 3c 78 72 69 67 68 74 31 29 0a 20 20 20 20  t1<xright1).    
39e29 20 20 20 7c 7c 20 28 78 6c 65 66 74 31 3d 3d 78     || (xleft1==x
39e2a 72 69 67 68 74 31 20 26 26 20 78 6c 65 66 74 32  right1 && xleft2
39e2b 3c 78 72 69 67 68 74 32 29 0a 20 20 20 20 20 20  <xright2).      
39e2c 29 29 7b 0a 20 20 20 20 20 20 20 20 61 49 64 78  )){.        aIdx
39e2d 5b 69 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d  [iLeft+iRight] =
39e2e 20 61 4c 65 66 74 5b 69 4c 65 66 74 5d 3b 0a 20   aLeft[iLeft];. 
39e2f 20 20 20 20 20 20 20 69 4c 65 66 74 2b 2b 3b 0a         iLeft++;.
39e30 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
39e31 20 20 20 20 20 61 49 64 78 5b 69 4c 65 66 74 2b       aIdx[iLeft+
39e32 69 52 69 67 68 74 5d 20 3d 20 61 52 69 67 68 74  iRight] = aRight
39e33 5b 69 52 69 67 68 74 5d 3b 0a 20 20 20 20 20 20  [iRight];.      
39e34 20 20 69 52 69 67 68 74 2b 2b 3b 0a 20 20 20 20    iRight++;.    
39e35 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30    }.    }..#if 0
39e36 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  .    /* Check th
39e37 61 74 20 74 68 65 20 73 6f 72 74 20 77 6f 72 6b  at the sort work
39e38 65 64 20 2a 2f 0a 20 20 20 20 7b 0a 20 20 20 20  ed */.    {.    
39e39 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20    int jj;.      
39e3a 66 6f 72 28 6a 6a 3d 31 3b 20 6a 6a 3c 6e 49 64  for(jj=1; jj<nId
39e3b 78 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  x; jj++){.      
39e3c 20 20 66 6c 6f 61 74 20 78 6c 65 66 74 31 20 3d    float xleft1 =
39e3d 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a 2d 31   aCell[aIdx[jj-1
39e3e 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32  ]].aCoord[iDim*2
39e3f 5d 3b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74  ];.        float
39e40 20 78 6c 65 66 74 32 20 3d 20 61 43 65 6c 6c 5b   xleft2 = aCell[
39e41 61 49 64 78 5b 6a 6a 2d 31 5d 5d 2e 61 43 6f 6f  aIdx[jj-1]].aCoo
39e42 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 3b 0a 20 20  rd[iDim*2+1];.  
39e43 20 20 20 20 20 20 66 6c 6f 61 74 20 78 72 69 67        float xrig
39e44 68 74 31 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78  ht1 = aCell[aIdx
39e45 5b 6a 6a 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69  [jj]].aCoord[iDi
39e46 6d 2a 32 5d 3b 0a 20 20 20 20 20 20 20 20 66 6c  m*2];.        fl
39e47 6f 61 74 20 78 72 69 67 68 74 32 20 3d 20 61 43  oat xright2 = aC
39e48 65 6c 6c 5b 61 49 64 78 5b 6a 6a 5d 5d 2e 61 43  ell[aIdx[jj]].aC
39e49 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 3b 0a  oord[iDim*2+1];.
39e4a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
39e4b 78 6c 65 66 74 31 3c 3d 78 72 69 67 68 74 31 20  xleft1<=xright1 
39e4c 26 26 20 28 78 6c 65 66 74 31 3c 78 72 69 67 68  && (xleft1<xrigh
39e4d 74 31 20 7c 7c 20 78 6c 65 66 74 32 3c 3d 78 72  t1 || xleft2<=xr
39e4e 69 67 68 74 32 29 20 29 3b 0a 20 20 20 20 20 20  ight2) );.      
39e4f 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  }.    }.#endif. 
39e50 20 7d 0a 7d 0a 0a 23 69 66 20 56 41 52 49 41 4e   }.}..#if VARIAN
39e51 54 5f 52 53 54 41 52 54 52 45 45 5f 53 50 4c 49  T_RSTARTREE_SPLI
39e52 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  T./*.** Implemen
39e53 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 52 2a  tation of the R*
39e54 2d 74 72 65 65 20 76 61 72 69 61 6e 74 20 6f 66  -tree variant of
39e55 20 53 70 6c 69 74 4e 6f 64 65 20 66 72 6f 6d 20   SplitNode from 
39e56 42 65 63 6b 6d 61 6e 5b 31 39 39 30 5d 2e 0a 2a  Beckman[1990]..*
39e57 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 70 6c  /.static int spl
39e58 69 74 4e 6f 64 65 53 74 61 72 74 72 65 65 28 0a  itNodeStartree(.
39e59 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c    Rtree *pRtree,
39e5a 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43  .  RtreeCell *aC
39e5b 65 6c 6c 2c 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  ell,.  int nCell
39e5c 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ,.  RtreeNode *p
39e5d 4c 65 66 74 2c 0a 20 20 52 74 72 65 65 4e 6f 64  Left,.  RtreeNod
39e5e 65 20 2a 70 52 69 67 68 74 2c 0a 20 20 52 74 72  e *pRight,.  Rtr
39e5f 65 65 43 65 6c 6c 20 2a 70 42 62 6f 78 4c 65 66  eeCell *pBboxLef
39e60 74 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  t,.  RtreeCell *
39e61 70 42 62 6f 78 52 69 67 68 74 0a 29 7b 0a 20 20  pBboxRight.){.  
39e62 69 6e 74 20 2a 2a 61 61 53 6f 72 74 65 64 3b 0a  int **aaSorted;.
39e63 20 20 69 6e 74 20 2a 61 53 70 61 72 65 3b 0a 20    int *aSpare;. 
39e64 20 69 6e 74 20 69 69 3b 0a 0a 20 20 69 6e 74 20   int ii;..  int 
39e65 69 42 65 73 74 44 69 6d 3b 0a 20 20 69 6e 74 20  iBestDim;.  int 
39e66 69 42 65 73 74 53 70 6c 69 74 3b 0a 20 20 66 6c  iBestSplit;.  fl
39e67 6f 61 74 20 66 42 65 73 74 4d 61 72 67 69 6e 3b  oat fBestMargin;
39e68 0a 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20  ..  int nByte = 
39e69 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2b 31 29  (pRtree->nDim+1)
39e6a 2a 28 73 69 7a 65 6f 66 28 69 6e 74 2a 29 2b 6e  *(sizeof(int*)+n
39e6b 43 65 6c 6c 2a 73 69 7a 65 6f 66 28 69 6e 74 29  Cell*sizeof(int)
39e6c 29 3b 0a 0a 20 20 61 61 53 6f 72 74 65 64 20 3d  );..  aaSorted =
39e6d 20 28 69 6e 74 20 2a 2a 29 73 71 6c 69 74 65 33   (int **)sqlite3
39e6e 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a  _malloc(nByte);.
39e6f 20 20 69 66 28 20 21 61 61 53 6f 72 74 65 64 20    if( !aaSorted 
39e70 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
39e71 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
39e72 0a 20 20 61 53 70 61 72 65 20 3d 20 26 28 28 69  .  aSpare = &((i
39e73 6e 74 20 2a 29 26 61 61 53 6f 72 74 65 64 5b 70  nt *)&aaSorted[p
39e74 52 74 72 65 65 2d 3e 6e 44 69 6d 5d 29 5b 70 52  Rtree->nDim])[pR
39e75 74 72 65 65 2d 3e 6e 44 69 6d 2a 6e 43 65 6c 6c  tree->nDim*nCell
39e76 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61 61 53 6f  ];.  memset(aaSo
39e77 72 74 65 64 2c 20 30 2c 20 6e 42 79 74 65 29 3b  rted, 0, nByte);
39e78 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c  .  for(ii=0; ii<
39e79 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 69  pRtree->nDim; ii
39e7a 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6a 6a 3b  ++){.    int jj;
39e7b 0a 20 20 20 20 61 61 53 6f 72 74 65 64 5b 69 69  .    aaSorted[ii
39e7c 5d 20 3d 20 26 28 28 69 6e 74 20 2a 29 26 61 61  ] = &((int *)&aa
39e7d 53 6f 72 74 65 64 5b 70 52 74 72 65 65 2d 3e 6e  Sorted[pRtree->n
39e7e 44 69 6d 5d 29 5b 69 69 2a 6e 43 65 6c 6c 5d 3b  Dim])[ii*nCell];
39e7f 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a  .    for(jj=0; j
39e80 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a  j<nCell; jj++){.
39e81 20 20 20 20 20 20 61 61 53 6f 72 74 65 64 5b 69        aaSorted[i
39e82 69 5d 5b 6a 6a 5d 20 3d 20 6a 6a 3b 0a 20 20 20  i][jj] = jj;.   
39e83 20 7d 0a 20 20 20 20 53 6f 72 74 42 79 44 69 6d   }.    SortByDim
39e84 65 6e 73 69 6f 6e 28 70 52 74 72 65 65 2c 20 61  ension(pRtree, a
39e85 61 53 6f 72 74 65 64 5b 69 69 5d 2c 20 6e 43 65  aSorted[ii], nCe
39e86 6c 6c 2c 20 69 69 2c 20 61 43 65 6c 6c 2c 20 61  ll, ii, aCell, a
39e87 53 70 61 72 65 29 3b 0a 20 20 7d 0a 0a 20 20 66  Spare);.  }..  f
39e88 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 52 74 72  or(ii=0; ii<pRtr
39e89 65 65 2d 3e 6e 44 69 6d 3b 20 69 69 2b 2b 29 7b  ee->nDim; ii++){
39e8a 0a 20 20 20 20 66 6c 6f 61 74 20 6d 61 72 67 69  .    float margi
39e8b 6e 20 3d 20 30 2e 30 3b 0a 20 20 20 20 66 6c 6f  n = 0.0;.    flo
39e8c 61 74 20 66 42 65 73 74 4f 76 65 72 6c 61 70 3b  at fBestOverlap;
39e8d 0a 20 20 20 20 66 6c 6f 61 74 20 66 42 65 73 74  .    float fBest
39e8e 41 72 65 61 3b 0a 20 20 20 20 69 6e 74 20 69 42  Area;.    int iB
39e8f 65 73 74 4c 65 66 74 3b 0a 20 20 20 20 69 6e 74  estLeft;.    int
39e90 20 6e 4c 65 66 74 3b 0a 0a 20 20 20 20 66 6f 72   nLeft;..    for
39e91 28 0a 20 20 20 20 20 20 6e 4c 65 66 74 3d 52 54  (.      nLeft=RT
39e92 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74  REE_MINCELLS(pRt
39e93 72 65 65 29 3b 20 0a 20 20 20 20 20 20 6e 4c 65  ree); .      nLe
39e94 66 74 3c 3d 28 6e 43 65 6c 6c 2d 52 54 52 45 45  ft<=(nCell-RTREE
39e95 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65  _MINCELLS(pRtree
39e96 29 29 3b 20 0a 20 20 20 20 20 20 6e 4c 65 66 74  )); .      nLeft
39e97 2b 2b 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  ++.    ){.      
39e98 52 74 72 65 65 43 65 6c 6c 20 6c 65 66 74 3b 0a  RtreeCell left;.
39e99 20 20 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20        RtreeCell 
39e9a 72 69 67 68 74 3b 0a 20 20 20 20 20 20 69 6e 74  right;.      int
39e9b 20 6b 6b 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74   kk;.      float
39e9c 20 6f 76 65 72 6c 61 70 3b 0a 20 20 20 20 20 20   overlap;.      
39e9d 66 6c 6f 61 74 20 61 72 65 61 3b 0a 0a 20 20 20  float area;..   
39e9e 20 20 20 6d 65 6d 63 70 79 28 26 6c 65 66 74 2c     memcpy(&left,
39e9f 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64   &aCell[aaSorted
39ea0 5b 69 69 5d 5b 30 5d 5d 2c 20 73 69 7a 65 6f 66  [ii][0]], sizeof
39ea1 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20  (RtreeCell));.  
39ea2 20 20 20 20 6d 65 6d 63 70 79 28 26 72 69 67 68      memcpy(&righ
39ea3 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74  t, &aCell[aaSort
39ea4 65 64 5b 69 69 5d 5b 6e 43 65 6c 6c 2d 31 5d 5d  ed[ii][nCell-1]]
39ea5 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65  , sizeof(RtreeCe
39ea6 6c 6c 29 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  ll));.      for(
39ea7 6b 6b 3d 31 3b 20 6b 6b 3c 28 6e 43 65 6c 6c 2d  kk=1; kk<(nCell-
39ea8 31 29 3b 20 6b 6b 2b 2b 29 7b 0a 20 20 20 20 20  1); kk++){.     
39ea9 20 20 20 69 66 28 20 6b 6b 3c 6e 4c 65 66 74 20     if( kk<nLeft 
39eaa 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 65 6c  ){.          cel
39eab 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26  lUnion(pRtree, &
39eac 6c 65 66 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53  left, &aCell[aaS
39ead 6f 72 74 65 64 5b 69 69 5d 5b 6b 6b 5d 5d 29 3b  orted[ii][kk]]);
39eae 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
39eaf 20 20 20 20 20 20 20 20 20 20 63 65 6c 6c 55 6e            cellUn
39eb0 69 6f 6e 28 70 52 74 72 65 65 2c 20 26 72 69 67  ion(pRtree, &rig
39eb1 68 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72  ht, &aCell[aaSor
39eb2 74 65 64 5b 69 69 5d 5b 6b 6b 5d 5d 29 3b 0a 20  ted[ii][kk]]);. 
39eb3 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
39eb4 0a 20 20 20 20 20 20 6d 61 72 67 69 6e 20 2b 3d  .      margin +=
39eb5 20 63 65 6c 6c 4d 61 72 67 69 6e 28 70 52 74 72   cellMargin(pRtr
39eb6 65 65 2c 20 26 6c 65 66 74 29 3b 0a 20 20 20 20  ee, &left);.    
39eb7 20 20 6d 61 72 67 69 6e 20 2b 3d 20 63 65 6c 6c    margin += cell
39eb8 4d 61 72 67 69 6e 28 70 52 74 72 65 65 2c 20 26  Margin(pRtree, &
39eb9 72 69 67 68 74 29 3b 0a 20 20 20 20 20 20 6f 76  right);.      ov
39eba 65 72 6c 61 70 20 3d 20 63 65 6c 6c 4f 76 65 72  erlap = cellOver
39ebb 6c 61 70 28 70 52 74 72 65 65 2c 20 26 6c 65 66  lap(pRtree, &lef
39ebc 74 2c 20 26 72 69 67 68 74 2c 20 31 2c 20 2d 31  t, &right, 1, -1
39ebd 29 3b 0a 20 20 20 20 20 20 61 72 65 61 20 3d 20  );.      area = 
39ebe 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65 65 2c  cellArea(pRtree,
39ebf 20 26 6c 65 66 74 29 20 2b 20 63 65 6c 6c 41 72   &left) + cellAr
39ec0 65 61 28 70 52 74 72 65 65 2c 20 26 72 69 67 68  ea(pRtree, &righ
39ec1 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 6e  t);.      if( (n
39ec2 4c 65 66 74 3d 3d 52 54 52 45 45 5f 4d 49 4e 43  Left==RTREE_MINC
39ec3 45 4c 4c 53 28 70 52 74 72 65 65 29 29 0a 20 20  ELLS(pRtree)).  
39ec4 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c 61 70       || (overlap
39ec5 3c 66 42 65 73 74 4f 76 65 72 6c 61 70 29 0a 20  <fBestOverlap). 
39ec6 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c 61        || (overla
39ec7 70 3d 3d 66 42 65 73 74 4f 76 65 72 6c 61 70 20  p==fBestOverlap 
39ec8 26 26 20 61 72 65 61 3c 66 42 65 73 74 41 72 65  && area<fBestAre
39ec9 61 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  a).      ){.    
39eca 20 20 20 20 69 42 65 73 74 4c 65 66 74 20 3d 20      iBestLeft = 
39ecb 6e 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20 66  nLeft;.        f
39ecc 42 65 73 74 4f 76 65 72 6c 61 70 20 3d 20 6f 76  BestOverlap = ov
39ecd 65 72 6c 61 70 3b 0a 20 20 20 20 20 20 20 20 66  erlap;.        f
39ece 42 65 73 74 41 72 65 61 20 3d 20 61 72 65 61 3b  BestArea = area;
39ecf 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
39ed0 20 20 20 20 69 66 28 20 69 69 3d 3d 30 20 7c 7c      if( ii==0 ||
39ed1 20 6d 61 72 67 69 6e 3c 66 42 65 73 74 4d 61 72   margin<fBestMar
39ed2 67 69 6e 20 29 7b 0a 20 20 20 20 20 20 69 42 65  gin ){.      iBe
39ed3 73 74 44 69 6d 20 3d 20 69 69 3b 0a 20 20 20 20  stDim = ii;.    
39ed4 20 20 66 42 65 73 74 4d 61 72 67 69 6e 20 3d 20    fBestMargin = 
39ed5 6d 61 72 67 69 6e 3b 0a 20 20 20 20 20 20 69 42  margin;.      iB
39ed6 65 73 74 53 70 6c 69 74 20 3d 20 69 42 65 73 74  estSplit = iBest
39ed7 4c 65 66 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Left;.    }.  }.
39ed8 0a 20 20 6d 65 6d 63 70 79 28 70 42 62 6f 78 4c  .  memcpy(pBboxL
39ed9 65 66 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f  eft, &aCell[aaSo
39eda 72 74 65 64 5b 69 42 65 73 74 44 69 6d 5d 5b 30  rted[iBestDim][0
39edb 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65  ]], sizeof(Rtree
39edc 43 65 6c 6c 29 29 3b 0a 20 20 6d 65 6d 63 70 79  Cell));.  memcpy
39edd 28 70 42 62 6f 78 52 69 67 68 74 2c 20 26 61 43  (pBboxRight, &aC
39ede 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69 42 65  ell[aaSorted[iBe
39edf 73 74 44 69 6d 5d 5b 69 42 65 73 74 53 70 6c 69  stDim][iBestSpli
39ee0 74 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65  t]], sizeof(Rtre
39ee1 65 43 65 6c 6c 29 29 3b 0a 20 20 66 6f 72 28 69  eCell));.  for(i
39ee2 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69  i=0; ii<nCell; i
39ee3 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65 65 4e  i++){.    RtreeN
39ee4 6f 64 65 20 2a 70 54 61 72 67 65 74 20 3d 20 28  ode *pTarget = (
39ee5 69 69 3c 69 42 65 73 74 53 70 6c 69 74 29 3f 70  ii<iBestSplit)?p
39ee6 4c 65 66 74 3a 70 52 69 67 68 74 3b 0a 20 20 20  Left:pRight;.   
39ee7 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f   RtreeCell *pBbo
39ee8 78 20 3d 20 28 69 69 3c 69 42 65 73 74 53 70 6c  x = (ii<iBestSpl
39ee9 69 74 29 3f 70 42 62 6f 78 4c 65 66 74 3a 70 42  it)?pBboxLeft:pB
39eea 62 6f 78 52 69 67 68 74 3b 0a 20 20 20 20 52 74  boxRight;.    Rt
39eeb 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 20 3d  reeCell *pCell =
39eec 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64   &aCell[aaSorted
39eed 5b 69 42 65 73 74 44 69 6d 5d 5b 69 69 5d 5d 3b  [iBestDim][ii]];
39eee 0a 20 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43  .    nodeInsertC
39eef 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 54 61 72  ell(pRtree, pTar
39ef0 67 65 74 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20  get, pCell);.   
39ef1 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65   cellUnion(pRtre
39ef2 65 2c 20 70 42 62 6f 78 2c 20 70 43 65 6c 6c 29  e, pBbox, pCell)
39ef3 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33  ;.  }..  sqlite3
39ef4 5f 66 72 65 65 28 61 61 53 6f 72 74 65 64 29 3b  _free(aaSorted);
39ef5 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
39ef6 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  _OK;.}.#endif..#
39ef7 69 66 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d  if VARIANT_GUTTM
39ef8 41 4e 5f 53 50 4c 49 54 0a 2f 2a 0a 2a 2a 20 49  AN_SPLIT./*.** I
39ef9 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
39efa 20 74 68 65 20 72 65 67 75 6c 61 72 20 52 2d 74   the regular R-t
39efb 72 65 65 20 53 70 6c 69 74 4e 6f 64 65 20 66 72  ree SplitNode fr
39efc 6f 6d 20 47 75 74 74 6d 61 6e 5b 31 39 38 34 5d  om Guttman[1984]
39efd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
39efe 73 70 6c 69 74 4e 6f 64 65 47 75 74 74 6d 61 6e  splitNodeGuttman
39eff 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  (.  Rtree *pRtre
39f00 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  e,.  RtreeCell *
39f01 61 43 65 6c 6c 2c 0a 20 20 69 6e 74 20 6e 43 65  aCell,.  int nCe
39f02 6c 6c 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  ll,.  RtreeNode 
39f03 2a 70 4c 65 66 74 2c 0a 20 20 52 74 72 65 65 4e  *pLeft,.  RtreeN
39f04 6f 64 65 20 2a 70 52 69 67 68 74 2c 0a 20 20 52  ode *pRight,.  R
39f05 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f 78 4c  treeCell *pBboxL
39f06 65 66 74 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c  eft,.  RtreeCell
39f07 20 2a 70 42 62 6f 78 52 69 67 68 74 0a 29 7b 0a   *pBboxRight.){.
39f08 20 20 69 6e 74 20 69 4c 65 66 74 53 65 65 64 20    int iLeftSeed 
39f09 3d 20 30 3b 0a 20 20 69 6e 74 20 69 52 69 67 68  = 0;.  int iRigh
39f0a 74 53 65 65 64 20 3d 20 31 3b 0a 20 20 69 6e 74  tSeed = 1;.  int
39f0b 20 2a 61 69 55 73 65 64 3b 0a 20 20 69 6e 74 20   *aiUsed;.  int 
39f0c 69 3b 0a 0a 20 20 61 69 55 73 65 64 20 3d 20 73  i;..  aiUsed = s
39f0d 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
39f0e 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 65 6c 6c 29  zeof(int)*nCell)
39f0f 3b 0a 20 20 69 66 28 20 21 61 69 55 73 65 64 20  ;.  if( !aiUsed 
39f10 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
39f11 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
39f12 20 20 6d 65 6d 73 65 74 28 61 69 55 73 65 64 2c    memset(aiUsed,
39f13 20 30 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a   0, sizeof(int)*
39f14 6e 43 65 6c 6c 29 3b 0a 0a 20 20 50 69 63 6b 53  nCell);..  PickS
39f15 65 65 64 73 28 70 52 74 72 65 65 2c 20 61 43 65  eeds(pRtree, aCe
39f16 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 26 69 4c 65 66  ll, nCell, &iLef
39f17 74 53 65 65 64 2c 20 26 69 52 69 67 68 74 53 65  tSeed, &iRightSe
39f18 65 64 29 3b 0a 0a 20 20 6d 65 6d 63 70 79 28 70  ed);..  memcpy(p
39f19 42 62 6f 78 4c 65 66 74 2c 20 26 61 43 65 6c 6c  BboxLeft, &aCell
39f1a 5b 69 4c 65 66 74 53 65 65 64 5d 2c 20 73 69 7a  [iLeftSeed], siz
39f1b 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b  eof(RtreeCell));
39f1c 0a 20 20 6d 65 6d 63 70 79 28 70 42 62 6f 78 52  .  memcpy(pBboxR
39f1d 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 69 52 69  ight, &aCell[iRi
39f1e 67 68 74 53 65 65 64 5d 2c 20 73 69 7a 65 6f 66  ghtSeed], sizeof
39f1f 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20  (RtreeCell));.  
39f20 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70  nodeInsertCell(p
39f21 52 74 72 65 65 2c 20 70 4c 65 66 74 2c 20 26 61  Rtree, pLeft, &a
39f22 43 65 6c 6c 5b 69 4c 65 66 74 53 65 65 64 5d 29  Cell[iLeftSeed])
39f23 3b 0a 20 20 6e 6f 64 65 49 6e 73 65 72 74 43 65  ;.  nodeInsertCe
39f24 6c 6c 28 70 52 74 72 65 65 2c 20 70 52 69 67 68  ll(pRtree, pRigh
39f25 74 2c 20 26 61 43 65 6c 6c 5b 69 52 69 67 68 74  t, &aCell[iRight
39f26 53 65 65 64 5d 29 3b 0a 20 20 61 69 55 73 65 64  Seed]);.  aiUsed
39f27 5b 69 4c 65 66 74 53 65 65 64 5d 20 3d 20 31 3b  [iLeftSeed] = 1;
39f28 0a 20 20 61 69 55 73 65 64 5b 69 52 69 67 68 74  .  aiUsed[iRight
39f29 53 65 65 64 5d 20 3d 20 31 3b 0a 0a 20 20 66 6f  Seed] = 1;..  fo
39f2a 72 28 69 3d 6e 43 65 6c 6c 2d 32 3b 20 69 3e 30  r(i=nCell-2; i>0
39f2b 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 52 74 72 65  ; i--){.    Rtre
39f2c 65 43 65 6c 6c 20 2a 70 4e 65 78 74 3b 0a 20 20  eCell *pNext;.  
39f2d 20 20 70 4e 65 78 74 20 3d 20 50 69 63 6b 4e 65    pNext = PickNe
39f2e 78 74 28 70 52 74 72 65 65 2c 20 61 43 65 6c 6c  xt(pRtree, aCell
39f2f 2c 20 6e 43 65 6c 6c 2c 20 70 42 62 6f 78 4c 65  , nCell, pBboxLe
39f30 66 74 2c 20 70 42 62 6f 78 52 69 67 68 74 2c 20  ft, pBboxRight, 
39f31 61 69 55 73 65 64 29 3b 0a 20 20 20 20 66 6c 6f  aiUsed);.    flo
39f32 61 74 20 64 69 66 66 20 3d 20 20 0a 20 20 20 20  at diff =  .    
39f33 20 20 63 65 6c 6c 47 72 6f 77 74 68 28 70 52 74    cellGrowth(pRt
39f34 72 65 65 2c 20 70 42 62 6f 78 4c 65 66 74 2c 20  ree, pBboxLeft, 
39f35 70 4e 65 78 74 29 20 2d 20 0a 20 20 20 20 20 20  pNext) - .      
39f36 63 65 6c 6c 47 72 6f 77 74 68 28 70 52 74 72 65  cellGrowth(pRtre
39f37 65 2c 20 70 42 62 6f 78 52 69 67 68 74 2c 20 70  e, pBboxRight, p
39f38 4e 65 78 74 29 0a 20 20 20 20 3b 0a 20 20 20 20  Next).    ;.    
39f39 69 66 28 20 28 52 54 52 45 45 5f 4d 49 4e 43 45  if( (RTREE_MINCE
39f3a 4c 4c 53 28 70 52 74 72 65 65 29 2d 4e 43 45 4c  LLS(pRtree)-NCEL
39f3b 4c 28 70 52 69 67 68 74 29 3d 3d 69 29 0a 20 20  L(pRight)==i).  
39f3c 20 20 20 7c 7c 20 28 64 69 66 66 3e 30 2e 30 20     || (diff>0.0 
39f3d 26 26 20 28 52 54 52 45 45 5f 4d 49 4e 43 45 4c  && (RTREE_MINCEL
39f3e 4c 53 28 70 52 74 72 65 65 29 2d 4e 43 45 4c 4c  LS(pRtree)-NCELL
39f3f 28 70 4c 65 66 74 29 21 3d 69 29 29 0a 20 20 20  (pLeft)!=i)).   
39f40 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64 65 49 6e   ){.      nodeIn
39f41 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
39f42 20 70 52 69 67 68 74 2c 20 70 4e 65 78 74 29 3b   pRight, pNext);
39f43 0a 20 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e  .      cellUnion
39f44 28 70 52 74 72 65 65 2c 20 70 42 62 6f 78 52 69  (pRtree, pBboxRi
39f45 67 68 74 2c 20 70 4e 65 78 74 29 3b 0a 20 20 20  ght, pNext);.   
39f46 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 6f   }else{.      no
39f47 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74  deInsertCell(pRt
39f48 72 65 65 2c 20 70 4c 65 66 74 2c 20 70 4e 65 78  ree, pLeft, pNex
39f49 74 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c 55 6e  t);.      cellUn
39f4a 69 6f 6e 28 70 52 74 72 65 65 2c 20 70 42 62 6f  ion(pRtree, pBbo
39f4b 78 4c 65 66 74 2c 20 70 4e 65 78 74 29 3b 0a 20  xLeft, pNext);. 
39f4c 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69     }.  }..  sqli
39f4d 74 65 33 5f 66 72 65 65 28 61 69 55 73 65 64 29  te3_free(aiUsed)
39f4e 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
39f4f 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  E_OK;.}.#endif..
39f50 73 74 61 74 69 63 20 69 6e 74 20 75 70 64 61 74  static int updat
39f51 65 4d 61 70 70 69 6e 67 28 0a 20 20 52 74 72 65  eMapping(.  Rtre
39f52 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20 69 36  e *pRtree, .  i6
39f53 34 20 69 52 6f 77 69 64 2c 20 0a 20 20 52 74 72  4 iRowid, .  Rtr
39f54 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a  eeNode *pNode, .
39f55 20 20 69 6e 74 20 69 48 65 69 67 68 74 0a 29 7b    int iHeight.){
39f56 0a 20 20 69 6e 74 20 28 2a 78 53 65 74 4d 61 70  .  int (*xSetMap
39f57 70 69 6e 67 29 28 52 74 72 65 65 20 2a 2c 20 73  ping)(Rtree *, s
39f58 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 73 71  qlite3_int64, sq
39f59 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 20 20  lite3_int64);.  
39f5a 78 53 65 74 4d 61 70 70 69 6e 67 20 3d 20 28 28  xSetMapping = ((
39f5b 69 48 65 69 67 68 74 3d 3d 30 29 3f 72 6f 77 69  iHeight==0)?rowi
39f5c 64 57 72 69 74 65 3a 70 61 72 65 6e 74 57 72 69  dWrite:parentWri
39f5d 74 65 29 3b 0a 20 20 69 66 28 20 69 48 65 69 67  te);.  if( iHeig
39f5e 68 74 3e 30 20 29 7b 0a 20 20 20 20 52 74 72 65  ht>0 ){.    Rtre
39f5f 65 4e 6f 64 65 20 2a 70 43 68 69 6c 64 20 3d 20  eNode *pChild = 
39f60 6e 6f 64 65 48 61 73 68 4c 6f 6f 6b 75 70 28 70  nodeHashLookup(p
39f61 52 74 72 65 65 2c 20 69 52 6f 77 69 64 29 3b 0a  Rtree, iRowid);.
39f62 20 20 20 20 69 66 28 20 70 43 68 69 6c 64 20 29      if( pChild )
39f63 7b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 6c 65  {.      nodeRele
39f64 61 73 65 28 70 52 74 72 65 65 2c 20 70 43 68 69  ase(pRtree, pChi
39f65 6c 64 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20  ld->pParent);.  
39f66 20 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63      nodeReferenc
39f67 65 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20  e(pNode);.      
39f68 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74 20  pChild->pParent 
39f69 3d 20 70 4e 6f 64 65 3b 0a 20 20 20 20 7d 0a 20  = pNode;.    }. 
39f6a 20 7d 0a 20 20 72 65 74 75 72 6e 20 78 53 65 74   }.  return xSet
39f6b 4d 61 70 70 69 6e 67 28 70 52 74 72 65 65 2c 20  Mapping(pRtree, 
39f6c 69 52 6f 77 69 64 2c 20 70 4e 6f 64 65 2d 3e 69  iRowid, pNode->i
39f6d 4e 6f 64 65 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  Node);.}..static
39f6e 20 69 6e 74 20 53 70 6c 69 74 4e 6f 64 65 28 0a   int SplitNode(.
39f6f 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c    Rtree *pRtree,
39f70 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  .  RtreeNode *pN
39f71 6f 64 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c  ode,.  RtreeCell
39f72 20 2a 70 43 65 6c 6c 2c 0a 20 20 69 6e 74 20 69   *pCell,.  int i
39f73 48 65 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20  Height.){.  int 
39f74 69 3b 0a 20 20 69 6e 74 20 6e 65 77 43 65 6c 6c  i;.  int newCell
39f75 49 73 52 69 67 68 74 20 3d 20 30 3b 0a 0a 20 20  IsRight = 0;..  
39f76 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
39f77 4f 4b 3b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 20  OK;.  int nCell 
39f78 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a  = NCELL(pNode);.
39f79 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65    RtreeCell *aCe
39f7a 6c 6c 3b 0a 20 20 69 6e 74 20 2a 61 69 55 73 65  ll;.  int *aiUse
39f7b 64 3b 0a 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  d;..  RtreeNode 
39f7c 2a 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20 52 74  *pLeft = 0;.  Rt
39f7d 72 65 65 4e 6f 64 65 20 2a 70 52 69 67 68 74 20  reeNode *pRight 
39f7e 3d 20 30 3b 0a 0a 20 20 52 74 72 65 65 43 65 6c  = 0;..  RtreeCel
39f7f 6c 20 6c 65 66 74 62 62 6f 78 3b 0a 20 20 52 74  l leftbbox;.  Rt
39f80 72 65 65 43 65 6c 6c 20 72 69 67 68 74 62 62 6f  reeCell rightbbo
39f81 78 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  x;..  /* Allocat
39f82 65 20 61 6e 20 61 72 72 61 79 20 61 6e 64 20 70  e an array and p
39f83 6f 70 75 6c 61 74 65 20 69 74 20 77 69 74 68 20  opulate it with 
39f84 61 20 63 6f 70 79 20 6f 66 20 70 43 65 6c 6c 20  a copy of pCell 
39f85 61 6e 64 20 0a 20 20 2a 2a 20 61 6c 6c 20 63 65  and .  ** all ce
39f86 6c 6c 73 20 66 72 6f 6d 20 6e 6f 64 65 20 70 4c  lls from node pL
39f87 65 66 74 2e 20 54 68 65 6e 20 7a 65 72 6f 20 74  eft. Then zero t
39f88 68 65 20 6f 72 69 67 69 6e 61 6c 20 6e 6f 64 65  he original node
39f89 2e 0a 20 20 2a 2f 0a 20 20 61 43 65 6c 6c 20 3d  ..  */.  aCell =
39f8a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
39f8b 28 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c  (sizeof(RtreeCel
39f8c 6c 29 2b 73 69 7a 65 6f 66 28 69 6e 74 29 29 2a  l)+sizeof(int))*
39f8d 28 6e 43 65 6c 6c 2b 31 29 29 3b 0a 20 20 69 66  (nCell+1));.  if
39f8e 28 20 21 61 43 65 6c 6c 20 29 7b 0a 20 20 20 20  ( !aCell ){.    
39f8f 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
39f90 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c 69  M;.    goto spli
39f91 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20  tnode_out;.  }. 
39f92 20 61 69 55 73 65 64 20 3d 20 28 69 6e 74 20 2a   aiUsed = (int *
39f93 29 26 61 43 65 6c 6c 5b 6e 43 65 6c 6c 2b 31 5d  )&aCell[nCell+1]
39f94 3b 0a 20 20 6d 65 6d 73 65 74 28 61 69 55 73 65  ;.  memset(aiUse
39f95 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 69 6e 74  d, 0, sizeof(int
39f96 29 2a 28 6e 43 65 6c 6c 2b 31 29 29 3b 0a 20 20  )*(nCell+1));.  
39f97 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c  for(i=0; i<nCell
39f98 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 6f 64 65  ; i++){.    node
39f99 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  GetCell(pRtree, 
39f9a 70 4e 6f 64 65 2c 20 69 2c 20 26 61 43 65 6c 6c  pNode, i, &aCell
39f9b 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 6e 6f 64 65  [i]);.  }.  node
39f9c 5a 65 72 6f 28 70 52 74 72 65 65 2c 20 70 4e 6f  Zero(pRtree, pNo
39f9d 64 65 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 61  de);.  memcpy(&a
39f9e 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c 20 70 43 65  Cell[nCell], pCe
39f9f 6c 6c 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65  ll, sizeof(Rtree
39fa0 43 65 6c 6c 29 29 3b 0a 20 20 6e 43 65 6c 6c 2b  Cell));.  nCell+
39fa1 2b 3b 0a 0a 20 20 69 66 28 20 70 4e 6f 64 65 2d  +;..  if( pNode-
39fa2 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20 20  >iNode==1 ){.   
39fa3 20 70 52 69 67 68 74 20 3d 20 6e 6f 64 65 4e 65   pRight = nodeNe
39fa4 77 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  w(pRtree, pNode,
39fa5 20 31 29 3b 0a 20 20 20 20 70 4c 65 66 74 20 3d   1);.    pLeft =
39fa6 20 6e 6f 64 65 4e 65 77 28 70 52 74 72 65 65 2c   nodeNew(pRtree,
39fa7 20 70 4e 6f 64 65 2c 20 31 29 3b 0a 20 20 20 20   pNode, 1);.    
39fa8 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2b 2b  pRtree->iDepth++
39fa9 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 69 73 44  ;.    pNode->isD
39faa 69 72 74 79 20 3d 20 31 3b 0a 20 20 20 20 77 72  irty = 1;.    wr
39fab 69 74 65 49 6e 74 31 36 28 70 4e 6f 64 65 2d 3e  iteInt16(pNode->
39fac 7a 44 61 74 61 2c 20 70 52 74 72 65 65 2d 3e 69  zData, pRtree->i
39fad 44 65 70 74 68 29 3b 0a 20 20 7d 65 6c 73 65 7b  Depth);.  }else{
39fae 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 70 4e 6f  .    pLeft = pNo
39faf 64 65 3b 0a 20 20 20 20 70 52 69 67 68 74 20 3d  de;.    pRight =
39fb0 20 6e 6f 64 65 4e 65 77 28 70 52 74 72 65 65 2c   nodeNew(pRtree,
39fb1 20 70 4c 65 66 74 2d 3e 70 50 61 72 65 6e 74 2c   pLeft->pParent,
39fb2 20 31 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65 66   1);.    nodeRef
39fb3 65 72 65 6e 63 65 28 70 4c 65 66 74 29 3b 0a 20  erence(pLeft);. 
39fb4 20 7d 0a 0a 20 20 69 66 28 20 21 70 4c 65 66 74   }..  if( !pLeft
39fb5 20 7c 7c 20 21 70 52 69 67 68 74 20 29 7b 0a 20   || !pRight ){. 
39fb6 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
39fb7 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 73  OMEM;.    goto s
39fb8 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20  plitnode_out;.  
39fb9 7d 0a 0a 20 20 6d 65 6d 73 65 74 28 70 4c 65 66  }..  memset(pLef
39fba 74 2d 3e 7a 44 61 74 61 2c 20 30 2c 20 70 52 74  t->zData, 0, pRt
39fbb 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b  ree->iNodeSize);
39fbc 0a 20 20 6d 65 6d 73 65 74 28 70 52 69 67 68 74  .  memset(pRight
39fbd 2d 3e 7a 44 61 74 61 2c 20 30 2c 20 70 52 74 72  ->zData, 0, pRtr
39fbe 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b 0a  ee->iNodeSize);.
39fbf 0a 20 20 72 63 20 3d 20 41 73 73 69 67 6e 43 65  .  rc = AssignCe
39fc0 6c 6c 73 28 70 52 74 72 65 65 2c 20 61 43 65 6c  lls(pRtree, aCel
39fc1 6c 2c 20 6e 43 65 6c 6c 2c 20 70 4c 65 66 74 2c  l, nCell, pLeft,
39fc2 20 70 52 69 67 68 74 2c 20 26 6c 65 66 74 62 62   pRight, &leftbb
39fc3 6f 78 2c 20 26 72 69 67 68 74 62 62 6f 78 29 3b  ox, &rightbbox);
39fc4 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
39fc5 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f  E_OK ){.    goto
39fc6 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a   splitnode_out;.
39fc7 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65    }..  /* Ensure
39fc8 20 62 6f 74 68 20 63 68 69 6c 64 20 6e 6f 64 65   both child node
39fc9 73 20 68 61 76 65 20 6e 6f 64 65 20 6e 75 6d 62  s have node numb
39fca 65 72 73 20 61 73 73 69 67 6e 65 64 20 74 6f 20  ers assigned to 
39fcb 74 68 65 6d 2e 20 2a 2f 0a 20 20 69 66 28 20 28  them. */.  if( (
39fcc 30 3d 3d 70 52 69 67 68 74 2d 3e 69 4e 6f 64 65  0==pRight->iNode
39fcd 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   && SQLITE_OK!=(
39fce 72 63 20 3d 20 6e 6f 64 65 57 72 69 74 65 28 70  rc = nodeWrite(p
39fcf 52 74 72 65 65 2c 20 70 52 69 67 68 74 29 29 29  Rtree, pRight)))
39fd0 0a 20 20 20 7c 7c 20 28 30 3d 3d 70 4c 65 66 74  .   || (0==pLeft
39fd1 2d 3e 69 4e 6f 64 65 20 26 26 20 53 51 4c 49 54  ->iNode && SQLIT
39fd2 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 6e 6f 64 65  E_OK!=(rc = node
39fd3 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70 4c  Write(pRtree, pL
39fd4 65 66 74 29 29 29 0a 20 20 29 7b 0a 20 20 20 20  eft))).  ){.    
39fd5 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f  goto splitnode_o
39fd6 75 74 3b 0a 20 20 7d 0a 0a 20 20 72 69 67 68 74  ut;.  }..  right
39fd7 62 62 6f 78 2e 69 52 6f 77 69 64 20 3d 20 70 52  bbox.iRowid = pR
39fd8 69 67 68 74 2d 3e 69 4e 6f 64 65 3b 0a 20 20 6c  ight->iNode;.  l
39fd9 65 66 74 62 62 6f 78 2e 69 52 6f 77 69 64 20 3d  eftbbox.iRowid =
39fda 20 70 4c 65 66 74 2d 3e 69 4e 6f 64 65 3b 0a 0a   pLeft->iNode;..
39fdb 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f    if( pNode->iNo
39fdc 64 65 3d 3d 31 20 29 7b 0a 20 20 20 20 72 63 20  de==1 ){.    rc 
39fdd 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c  = rtreeInsertCel
39fde 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 2d  l(pRtree, pLeft-
39fdf 3e 70 50 61 72 65 6e 74 2c 20 26 6c 65 66 74 62  >pParent, &leftb
39fe0 62 6f 78 2c 20 69 48 65 69 67 68 74 2b 31 29 3b  box, iHeight+1);
39fe1 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
39fe2 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
39fe3 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f  goto splitnode_o
39fe4 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  ut;.    }.  }els
39fe5 65 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65  e{.    RtreeNode
39fe6 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 4c 65 66   *pParent = pLef
39fe7 74 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20  t->pParent;.    
39fe8 69 6e 74 20 69 43 65 6c 6c 20 3d 20 6e 6f 64 65  int iCell = node
39fe9 50 61 72 65 6e 74 49 6e 64 65 78 28 70 52 74 72  ParentIndex(pRtr
39fea 65 65 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20  ee, pLeft);.    
39feb 6e 6f 64 65 4f 76 65 72 77 72 69 74 65 43 65 6c  nodeOverwriteCel
39fec 6c 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e  l(pRtree, pParen
39fed 74 2c 20 26 6c 65 66 74 62 62 6f 78 2c 20 69 43  t, &leftbbox, iC
39fee 65 6c 6c 29 3b 0a 20 20 20 20 41 64 6a 75 73 74  ell);.    Adjust
39fef 54 72 65 65 28 70 52 74 72 65 65 2c 20 70 50 61  Tree(pRtree, pPa
39ff0 72 65 6e 74 2c 20 26 6c 65 66 74 62 62 6f 78 29  rent, &leftbbox)
39ff1 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28 72 63 20  ;.  }.  if( (rc 
39ff2 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c  = rtreeInsertCel
39ff3 6c 28 70 52 74 72 65 65 2c 20 70 52 69 67 68 74  l(pRtree, pRight
39ff4 2d 3e 70 50 61 72 65 6e 74 2c 20 26 72 69 67 68  ->pParent, &righ
39ff5 74 62 62 6f 78 2c 20 69 48 65 69 67 68 74 2b 31  tbbox, iHeight+1
39ff6 29 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73  )) ){.    goto s
39ff7 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20  plitnode_out;.  
39ff8 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  }..  for(i=0; i<
39ff9 4e 43 45 4c 4c 28 70 52 69 67 68 74 29 3b 20 69  NCELL(pRight); i
39ffa 2b 2b 29 7b 0a 20 20 20 20 69 36 34 20 69 52 6f  ++){.    i64 iRo
39ffb 77 69 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77  wid = nodeGetRow
39ffc 69 64 28 70 52 74 72 65 65 2c 20 70 52 69 67 68  id(pRtree, pRigh
39ffd 74 2c 20 69 29 3b 0a 20 20 20 20 72 63 20 3d 20  t, i);.    rc = 
39ffe 75 70 64 61 74 65 4d 61 70 70 69 6e 67 28 70 52  updateMapping(pR
39fff 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 70 52  tree, iRowid, pR
3a000 69 67 68 74 2c 20 69 48 65 69 67 68 74 29 3b 0a  ight, iHeight);.
3a001 20 20 20 20 69 66 28 20 69 52 6f 77 69 64 3d 3d      if( iRowid==
3a002 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 20 29 7b  pCell->iRowid ){
3a003 0a 20 20 20 20 20 20 6e 65 77 43 65 6c 6c 49 73  .      newCellIs
3a004 52 69 67 68 74 20 3d 20 31 3b 0a 20 20 20 20 7d  Right = 1;.    }
3a005 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
3a006 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
3a007 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f  goto splitnode_o
3a008 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ut;.    }.  }.  
3a009 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  if( pNode->iNode
3a00a 3d 3d 31 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  ==1 ){.    for(i
3a00b 3d 30 3b 20 69 3c 4e 43 45 4c 4c 28 70 4c 65 66  =0; i<NCELL(pLef
3a00c 74 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  t); i++){.      
3a00d 69 36 34 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64  i64 iRowid = nod
3a00e 65 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65  eGetRowid(pRtree
3a00f 2c 20 70 4c 65 66 74 2c 20 69 29 3b 0a 20 20 20  , pLeft, i);.   
3a010 20 20 20 72 63 20 3d 20 75 70 64 61 74 65 4d 61     rc = updateMa
3a011 70 70 69 6e 67 28 70 52 74 72 65 65 2c 20 69 52  pping(pRtree, iR
3a012 6f 77 69 64 2c 20 70 4c 65 66 74 2c 20 69 48 65  owid, pLeft, iHe
3a013 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28  ight);.      if(
3a014 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
3a015 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 73  {.        goto s
3a016 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20  plitnode_out;.  
3a017 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
3a018 6c 73 65 20 69 66 28 20 6e 65 77 43 65 6c 6c 49  lse if( newCellI
3a019 73 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20  sRight==0 ){.   
3a01a 20 72 63 20 3d 20 75 70 64 61 74 65 4d 61 70 70   rc = updateMapp
3a01b 69 6e 67 28 70 52 74 72 65 65 2c 20 70 43 65 6c  ing(pRtree, pCel
3a01c 6c 2d 3e 69 52 6f 77 69 64 2c 20 70 4c 65 66 74  l->iRowid, pLeft
3a01d 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a  , iHeight);.  }.
3a01e 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
3a01f 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d  E_OK ){.    rc =
3a020 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74   nodeRelease(pRt
3a021 72 65 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20  ree, pRight);.  
3a022 20 20 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20    pRight = 0;.  
3a023 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
3a024 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20  TE_OK ){.    rc 
3a025 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52  = nodeRelease(pR
3a026 74 72 65 65 2c 20 70 4c 65 66 74 29 3b 0a 20 20  tree, pLeft);.  
3a027 20 20 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20 7d    pLeft = 0;.  }
3a028 0a 0a 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3a  ..splitnode_out:
3a029 0a 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70  .  nodeRelease(p
3a02a 52 74 72 65 65 2c 20 70 52 69 67 68 74 29 3b 0a  Rtree, pRight);.
3a02b 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52    nodeRelease(pR
3a02c 74 72 65 65 2c 20 70 4c 65 66 74 29 3b 0a 20 20  tree, pLeft);.  
3a02d 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 43 65  sqlite3_free(aCe
3a02e 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ll);.  return rc
3a02f 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
3a030 66 69 78 4c 65 61 66 50 61 72 65 6e 74 28 52 74  fixLeafParent(Rt
3a031 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
3a032 65 65 4e 6f 64 65 20 2a 70 4c 65 61 66 29 7b 0a  eeNode *pLeaf){.
3a033 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
3a034 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 4c 65 61  E_OK;.  if( pLea
3a035 66 2d 3e 69 4e 6f 64 65 21 3d 31 20 26 26 20 70  f->iNode!=1 && p
3a036 4c 65 61 66 2d 3e 70 50 61 72 65 6e 74 3d 3d 30  Leaf->pParent==0
3a037 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
3a038 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65  bind_int64(pRtre
3a039 65 2d 3e 70 52 65 61 64 50 61 72 65 6e 74 2c 20  e->pReadParent, 
3a03a 31 2c 20 70 4c 65 61 66 2d 3e 69 4e 6f 64 65 29  1, pLeaf->iNode)
3a03b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
3a03c 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70  3_step(pRtree->p
3a03d 52 65 61 64 50 61 72 65 6e 74 29 3d 3d 53 51 4c  ReadParent)==SQL
3a03e 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
3a03f 20 69 36 34 20 69 4e 6f 64 65 20 3d 20 73 71 6c   i64 iNode = sql
3a040 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36  ite3_column_int6
3a041 34 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 50  4(pRtree->pReadP
3a042 61 72 65 6e 74 2c 20 30 29 3b 0a 20 20 20 20 20  arent, 0);.     
3a043 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72   rc = nodeAcquir
3a044 65 28 70 52 74 72 65 65 2c 20 69 4e 6f 64 65 2c  e(pRtree, iNode,
3a045 20 30 2c 20 26 70 4c 65 61 66 2d 3e 70 50 61 72   0, &pLeaf->pPar
3a046 65 6e 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ent);.    }else{
3a047 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
3a048 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a  TE_ERROR;.    }.
3a049 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65      sqlite3_rese
3a04a 74 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 50  t(pRtree->pReadP
3a04b 61 72 65 6e 74 29 3b 0a 20 20 20 20 69 66 28 20  arent);.    if( 
3a04c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
3a04d 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69 78 4c  .      rc = fixL
3a04e 65 61 66 50 61 72 65 6e 74 28 70 52 74 72 65 65  eafParent(pRtree
3a04f 2c 20 70 4c 65 61 66 2d 3e 70 50 61 72 65 6e 74  , pLeaf->pParent
3a050 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
3a051 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61  eturn rc;.}..sta
3a052 74 69 63 20 69 6e 74 20 64 65 6c 65 74 65 43 65  tic int deleteCe
3a053 6c 6c 28 52 74 72 65 65 20 2a 2c 20 52 74 72 65  ll(Rtree *, Rtre
3a054 65 4e 6f 64 65 20 2a 2c 20 69 6e 74 2c 20 69 6e  eNode *, int, in
3a055 74 29 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  t);..static int 
3a056 72 65 6d 6f 76 65 4e 6f 64 65 28 52 74 72 65 65  removeNode(Rtree
3a057 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e   *pRtree, RtreeN
3a058 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 69 6e 74 20  ode *pNode, int 
3a059 69 48 65 69 67 68 74 29 7b 0a 20 20 69 6e 74 20  iHeight){.  int 
3a05a 72 63 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  rc;.  RtreeNode 
3a05b 2a 70 50 61 72 65 6e 74 3b 0a 20 20 69 6e 74 20  *pParent;.  int 
3a05c 69 43 65 6c 6c 3b 0a 0a 20 20 61 73 73 65 72 74  iCell;..  assert
3a05d 28 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 3d 3d 31  ( pNode->nRef==1
3a05e 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65   );..  /* Remove
3a05f 20 74 68 65 20 65 6e 74 72 79 20 69 6e 20 74 68   the entry in th
3a060 65 20 70 61 72 65 6e 74 20 63 65 6c 6c 2e 20 2a  e parent cell. *
3a061 2f 0a 20 20 69 43 65 6c 6c 20 3d 20 6e 6f 64 65  /.  iCell = node
3a062 50 61 72 65 6e 74 49 6e 64 65 78 28 70 52 74 72  ParentIndex(pRtr
3a063 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 70 50  ee, pNode);.  pP
3a064 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d 3e 70  arent = pNode->p
3a065 50 61 72 65 6e 74 3b 0a 20 20 70 4e 6f 64 65 2d  Parent;.  pNode-
3a066 3e 70 50 61 72 65 6e 74 20 3d 20 30 3b 0a 20 20  >pParent = 0;.  
3a067 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28  if( SQLITE_OK!=(
3a068 72 63 20 3d 20 64 65 6c 65 74 65 43 65 6c 6c 28  rc = deleteCell(
3a069 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c  pRtree, pParent,
3a06a 20 69 43 65 6c 6c 2c 20 69 48 65 69 67 68 74 2b   iCell, iHeight+
3a06b 31 29 29 20 0a 20 20 20 7c 7c 20 53 51 4c 49 54  1)) .   || SQLIT
3a06c 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 6e 6f 64 65  E_OK!=(rc = node
3a06d 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
3a06e 70 50 61 72 65 6e 74 29 29 0a 20 20 29 7b 0a 20  pParent)).  ){. 
3a06f 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
3a070 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74  }..  /* Remove t
3a071 68 65 20 78 78 78 5f 6e 6f 64 65 20 65 6e 74 72  he xxx_node entr
3a072 79 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  y. */.  sqlite3_
3a073 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65  bind_int64(pRtre
3a074 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 2c 20  e->pDeleteNode, 
3a075 31 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29  1, pNode->iNode)
3a076 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ;.  sqlite3_step
3a077 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65  (pRtree->pDelete
3a078 4e 6f 64 65 29 3b 0a 20 20 69 66 28 20 53 51 4c  Node);.  if( SQL
3a079 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71  ITE_OK!=(rc = sq
3a07a 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72  lite3_reset(pRtr
3a07b 65 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 29  ee->pDeleteNode)
3a07c 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
3a07d 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65  rc;.  }..  /* Re
3a07e 6d 6f 76 65 20 74 68 65 20 78 78 78 5f 70 61 72  move the xxx_par
3a07f 65 6e 74 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20  ent entry. */.  
3a080 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
3a081 36 34 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  64(pRtree->pDele
3a082 74 65 50 61 72 65 6e 74 2c 20 31 2c 20 70 4e 6f  teParent, 1, pNo
3a083 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 73 71  de->iNode);.  sq
3a084 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65  lite3_step(pRtre
3a085 65 2d 3e 70 44 65 6c 65 74 65 50 61 72 65 6e 74  e->pDeleteParent
3a086 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  );.  if( SQLITE_
3a087 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK!=(rc = sqlite
3a088 33 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e  3_reset(pRtree->
3a089 70 44 65 6c 65 74 65 50 61 72 65 6e 74 29 29 20  pDeleteParent)) 
3a08a 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
3a08b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 52 65  ;.  }.  .  /* Re
3a08c 6d 6f 76 65 20 74 68 65 20 6e 6f 64 65 20 66 72  move the node fr
3a08d 6f 6d 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  om the in-memory
3a08e 20 68 61 73 68 20 74 61 62 6c 65 20 61 6e 64 20   hash table and 
3a08f 6c 69 6e 6b 20 69 74 20 69 6e 74 6f 0a 20 20 2a  link it into.  *
3a090 2a 20 74 68 65 20 52 74 72 65 65 2e 70 44 65 6c  * the Rtree.pDel
3a091 65 74 65 64 20 6c 69 73 74 2e 20 49 74 73 20 63  eted list. Its c
3a092 6f 6e 74 65 6e 74 73 20 77 69 6c 6c 20 62 65 20  ontents will be 
3a093 72 65 2d 69 6e 73 65 72 74 65 64 20 6c 61 74 65  re-inserted late
3a094 72 20 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 6e 6f 64  r on..  */.  nod
3a095 65 48 61 73 68 44 65 6c 65 74 65 28 70 52 74 72  eHashDelete(pRtr
3a096 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 70 4e  ee, pNode);.  pN
3a097 6f 64 65 2d 3e 69 4e 6f 64 65 20 3d 20 69 48 65  ode->iNode = iHe
3a098 69 67 68 74 3b 0a 20 20 70 4e 6f 64 65 2d 3e 70  ight;.  pNode->p
3a099 4e 65 78 74 20 3d 20 70 52 74 72 65 65 2d 3e 70  Next = pRtree->p
3a09a 44 65 6c 65 74 65 64 3b 0a 20 20 70 4e 6f 64 65  Deleted;.  pNode
3a09b 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 70 52 74 72  ->nRef++;.  pRtr
3a09c 65 65 2d 3e 70 44 65 6c 65 74 65 64 20 3d 20 70  ee->pDeleted = p
3a09d 4e 6f 64 65 3b 0a 0a 20 20 72 65 74 75 72 6e 20  Node;..  return 
3a09e 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74  SQLITE_OK;.}..st
3a09f 61 74 69 63 20 76 6f 69 64 20 66 69 78 42 6f 75  atic void fixBou
3a0a0 6e 64 69 6e 67 42 6f 78 28 52 74 72 65 65 20 2a  ndingBox(Rtree *
3a0a1 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64  pRtree, RtreeNod
3a0a2 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 52 74 72  e *pNode){.  Rtr
3a0a3 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 20  eeNode *pParent 
3a0a4 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74  = pNode->pParent
3a0a5 3b 0a 20 20 69 66 28 20 70 50 61 72 65 6e 74 20  ;.  if( pParent 
3a0a6 29 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 0a  ){.    int ii; .
3a0a7 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20      int nCell = 
3a0a8 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 20 20  NCELL(pNode);.  
3a0a9 20 20 52 74 72 65 65 43 65 6c 6c 20 62 6f 78 3b    RtreeCell box;
3a0aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a0ab 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
3a0ac 6f 75 6e 64 69 6e 67 20 62 6f 78 20 66 6f 72 20  ounding box for 
3a0ad 70 4e 6f 64 65 20 2a 2f 0a 20 20 20 20 6e 6f 64  pNode */.    nod
3a0ae 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  eGetCell(pRtree,
3a0af 20 70 4e 6f 64 65 2c 20 30 2c 20 26 62 6f 78 29   pNode, 0, &box)
3a0b0 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 31 3b 20  ;.    for(ii=1; 
3a0b1 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b  ii<nCell; ii++){
3a0b2 0a 20 20 20 20 20 20 52 74 72 65 65 43 65 6c 6c  .      RtreeCell
3a0b3 20 63 65 6c 6c 3b 0a 20 20 20 20 20 20 6e 6f 64   cell;.      nod
3a0b4 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  eGetCell(pRtree,
3a0b5 20 70 4e 6f 64 65 2c 20 69 69 2c 20 26 63 65 6c   pNode, ii, &cel
3a0b6 6c 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c 55 6e  l);.      cellUn
3a0b7 69 6f 6e 28 70 52 74 72 65 65 2c 20 26 62 6f 78  ion(pRtree, &box
3a0b8 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 20 20 7d 0a  , &cell);.    }.
3a0b9 20 20 20 20 62 6f 78 2e 69 52 6f 77 69 64 20 3d      box.iRowid =
3a0ba 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3b 0a 20   pNode->iNode;. 
3a0bb 20 20 20 69 69 20 3d 20 6e 6f 64 65 50 61 72 65     ii = nodePare
3a0bc 6e 74 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20  ntIndex(pRtree, 
3a0bd 70 4e 6f 64 65 29 3b 0a 20 20 20 20 6e 6f 64 65  pNode);.    node
3a0be 4f 76 65 72 77 72 69 74 65 43 65 6c 6c 28 70 52  OverwriteCell(pR
3a0bf 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 26  tree, pParent, &
3a0c0 62 6f 78 2c 20 69 69 29 3b 0a 20 20 20 20 66 69  box, ii);.    fi
3a0c1 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28 70 52 74  xBoundingBox(pRt
3a0c2 72 65 65 2c 20 70 50 61 72 65 6e 74 29 3b 0a 20  ree, pParent);. 
3a0c3 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65   }.}../*.** Dele
3a0c4 74 65 20 74 68 65 20 63 65 6c 6c 20 61 74 20 69  te the cell at i
3a0c5 6e 64 65 78 20 69 43 65 6c 6c 20 6f 66 20 6e 6f  ndex iCell of no
3a0c6 64 65 20 70 4e 6f 64 65 2e 20 41 66 74 65 72 20  de pNode. After 
3a0c7 72 65 6d 6f 76 69 6e 67 20 74 68 65 0a 2a 2a 20  removing the.** 
3a0c8 63 65 6c 6c 2c 20 61 64 6a 75 73 74 20 74 68 65  cell, adjust the
3a0c9 20 72 2d 74 72 65 65 20 64 61 74 61 20 73 74 72   r-tree data str
3a0ca 75 63 74 75 72 65 20 69 66 20 72 65 71 75 69 72  ucture if requir
3a0cb 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
3a0cc 74 20 64 65 6c 65 74 65 43 65 6c 6c 28 52 74 72  t deleteCell(Rtr
3a0cd 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
3a0ce 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 69 6e  eNode *pNode, in
3a0cf 74 20 69 43 65 6c 6c 2c 20 69 6e 74 20 69 48 65  t iCell, int iHe
3a0d0 69 67 68 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b  ight){.  int rc;
3a0d1 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ..  if( SQLITE_O
3a0d2 4b 21 3d 28 72 63 20 3d 20 66 69 78 4c 65 61 66  K!=(rc = fixLeaf
3a0d3 50 61 72 65 6e 74 28 70 52 74 72 65 65 2c 20 70  Parent(pRtree, p
3a0d4 4e 6f 64 65 29 29 20 29 7b 0a 20 20 20 20 72 65  Node)) ){.    re
3a0d5 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
3a0d6 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 63 65  /* Remove the ce
3a0d7 6c 6c 20 66 72 6f 6d 20 74 68 65 20 6e 6f 64 65  ll from the node
3a0d8 2e 20 54 68 69 73 20 63 61 6c 6c 20 6a 75 73 74  . This call just
3a0d9 20 6d 6f 76 65 73 20 62 79 74 65 73 20 61 72 6f   moves bytes aro
3a0da 75 6e 64 0a 20 20 2a 2a 20 74 68 65 20 69 6e 2d  und.  ** the in-
3a0db 6d 65 6d 6f 72 79 20 6e 6f 64 65 20 69 6d 61 67  memory node imag
3a0dc 65 2c 20 73 6f 20 69 74 20 63 61 6e 6e 6f 74 20  e, so it cannot 
3a0dd 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 6e 6f 64  fail..  */.  nod
3a0de 65 44 65 6c 65 74 65 43 65 6c 6c 28 70 52 74 72  eDeleteCell(pRtr
3a0df 65 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65 6c 6c  ee, pNode, iCell
3a0e0 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
3a0e1 6e 6f 64 65 20 69 73 20 6e 6f 74 20 74 68 65 20  node is not the 
3a0e2 74 72 65 65 20 72 6f 6f 74 20 61 6e 64 20 6e 6f  tree root and no
3a0e3 77 20 68 61 73 20 6c 65 73 73 20 74 68 61 6e 20  w has less than 
3a0e4 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 20 20 2a 2a  the minimum.  **
3a0e5 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73   number of cells
3a0e6 2c 20 72 65 6d 6f 76 65 20 69 74 20 66 72 6f 6d  , remove it from
3a0e7 20 74 68 65 20 74 72 65 65 2e 20 4f 74 68 65 72   the tree. Other
3a0e8 77 69 73 65 2c 20 75 70 64 61 74 65 20 74 68 65  wise, update the
3a0e9 0a 20 20 2a 2a 20 63 65 6c 6c 20 69 6e 20 74 68  .  ** cell in th
3a0ea 65 20 70 61 72 65 6e 74 20 6e 6f 64 65 20 73 6f  e parent node so
3a0eb 20 74 68 61 74 20 69 74 20 74 69 67 68 74 6c 79   that it tightly
3a0ec 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 75 70   contains the up
3a0ed 64 61 74 65 64 0a 20 20 2a 2a 20 6e 6f 64 65 2e  dated.  ** node.
3a0ee 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4e 6f 64  .  */.  if( pNod
3a0ef 65 2d 3e 69 4e 6f 64 65 21 3d 31 20 29 7b 0a 20  e->iNode!=1 ){. 
3a0f0 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50     RtreeNode *pP
3a0f1 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d 3e 70  arent = pNode->p
3a0f2 50 61 72 65 6e 74 3b 0a 20 20 20 20 69 66 28 20  Parent;.    if( 
3a0f3 28 70 50 61 72 65 6e 74 2d 3e 69 4e 6f 64 65 21  (pParent->iNode!
3a0f4 3d 31 20 7c 7c 20 4e 43 45 4c 4c 28 70 50 61 72  =1 || NCELL(pPar
3a0f5 65 6e 74 29 21 3d 31 29 20 0a 20 20 20 20 20 26  ent)!=1) .     &
3a0f6 26 20 28 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 3c  & (NCELL(pNode)<
3a0f7 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70  RTREE_MINCELLS(p
3a0f8 52 74 72 65 65 29 29 0a 20 20 20 20 29 7b 0a 20  Rtree)).    ){. 
3a0f9 20 20 20 20 20 72 63 20 3d 20 72 65 6d 6f 76 65       rc = remove
3a0fa 4e 6f 64 65 28 70 52 74 72 65 65 2c 20 70 4e 6f  Node(pRtree, pNo
3a0fb 64 65 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20  de, iHeight);.  
3a0fc 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66    }else{.      f
3a0fd 69 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28 70 52  ixBoundingBox(pR
3a0fe 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20  tree, pNode);.  
3a0ff 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72    }.  }..  retur
3a100 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  n rc;.}..static 
3a101 69 6e 74 20 52 65 69 6e 73 65 72 74 28 0a 20 20  int Reinsert(.  
3a102 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a  Rtree *pRtree, .
3a103 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
3a104 64 65 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c  de, .  RtreeCell
3a105 20 2a 70 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20   *pCell, .  int 
3a106 69 48 65 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74  iHeight.){.  int
3a107 20 2a 61 4f 72 64 65 72 3b 0a 20 20 69 6e 74 20   *aOrder;.  int 
3a108 2a 61 53 70 61 72 65 3b 0a 20 20 52 74 72 65 65  *aSpare;.  Rtree
3a109 43 65 6c 6c 20 2a 61 43 65 6c 6c 3b 0a 20 20 66  Cell *aCell;.  f
3a10a 6c 6f 61 74 20 2a 61 44 69 73 74 61 6e 63 65 3b  loat *aDistance;
3a10b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20  .  int nCell;.  
3a10c 66 6c 6f 61 74 20 61 43 65 6e 74 65 72 43 6f 6f  float aCenterCoo
3a10d 72 64 5b 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d  rd[RTREE_MAX_DIM
3a10e 45 4e 53 49 4f 4e 53 5d 3b 0a 20 20 69 6e 74 20  ENSIONS];.  int 
3a10f 69 44 69 6d 3b 0a 20 20 69 6e 74 20 69 69 3b 0a  iDim;.  int ii;.
3a110 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
3a111 45 5f 4f 4b 3b 0a 0a 20 20 6d 65 6d 73 65 74 28  E_OK;..  memset(
3a112 61 43 65 6e 74 65 72 43 6f 6f 72 64 2c 20 30 2c  aCenterCoord, 0,
3a113 20 73 69 7a 65 6f 66 28 66 6c 6f 61 74 29 2a 52   sizeof(float)*R
3a114 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49  TREE_MAX_DIMENSI
3a115 4f 4e 53 29 3b 0a 0a 20 20 6e 43 65 6c 6c 20 3d  ONS);..  nCell =
3a116 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 2b 31 3b   NCELL(pNode)+1;
3a117 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
3a118 74 68 65 20 62 75 66 66 65 72 73 20 75 73 65 64  the buffers used
3a119 20 62 79 20 74 68 69 73 20 6f 70 65 72 61 74 69   by this operati
3a11a 6f 6e 2e 20 54 68 65 20 61 6c 6c 6f 63 61 74 69  on. The allocati
3a11b 6f 6e 20 69 73 0a 20 20 2a 2a 20 72 65 6c 69 6e  on is.  ** relin
3a11c 71 75 69 73 68 65 64 20 62 65 66 6f 72 65 20 74  quished before t
3a11d 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
3a11e 75 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20 61 43 65  urns..  */.  aCe
3a11f 6c 6c 20 3d 20 28 52 74 72 65 65 43 65 6c 6c 20  ll = (RtreeCell 
3a120 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
3a121 28 6e 43 65 6c 6c 20 2a 20 28 0a 20 20 20 20 73  (nCell * (.    s
3a122 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
3a123 20 2b 20 20 20 20 20 20 20 20 20 2f 2a 20 61 43   +         /* aC
3a124 65 6c 6c 20 61 72 72 61 79 20 2a 2f 0a 20 20 20  ell array */.   
3a125 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 20 20 20   sizeof(int)    
3a126 20 20 20 2b 20 20 20 20 20 20 20 20 20 2f 2a 20     +         /* 
3a127 61 4f 72 64 65 72 20 61 72 72 61 79 20 2a 2f 0a  aOrder array */.
3a128 20 20 20 20 73 69 7a 65 6f 66 28 69 6e 74 29 20      sizeof(int) 
3a129 20 20 20 20 20 20 2b 20 20 20 20 20 20 20 20 20        +         
3a12a 2f 2a 20 61 53 70 61 72 65 20 61 72 72 61 79 20  /* aSpare array 
3a12b 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 66 6c  */.    sizeof(fl
3a12c 6f 61 74 29 20 20 20 20 20 20 20 20 20 20 20 20  oat)            
3a12d 20 20 20 2f 2a 20 61 44 69 73 74 61 6e 63 65 20     /* aDistance 
3a12e 61 72 72 61 79 20 2a 2f 0a 20 20 29 29 3b 0a 20  array */.  ));. 
3a12f 20 69 66 28 20 21 61 43 65 6c 6c 20 29 7b 0a 20   if( !aCell ){. 
3a130 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
3a131 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 4f  _NOMEM;.  }.  aO
3a132 72 64 65 72 20 20 20 20 3d 20 28 69 6e 74 20 2a  rder    = (int *
3a133 29 26 61 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 3b 0a  )&aCell[nCell];.
3a134 20 20 61 53 70 61 72 65 20 20 20 20 3d 20 28 69    aSpare    = (i
3a135 6e 74 20 2a 29 26 61 4f 72 64 65 72 5b 6e 43 65  nt *)&aOrder[nCe
3a136 6c 6c 5d 3b 0a 20 20 61 44 69 73 74 61 6e 63 65  ll];.  aDistance
3a137 20 3d 20 28 66 6c 6f 61 74 20 2a 29 26 61 53 70   = (float *)&aSp
3a138 61 72 65 5b 6e 43 65 6c 6c 5d 3b 0a 0a 20 20 66  are[nCell];..  f
3a139 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c  or(ii=0; ii<nCel
3a13a 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66  l; ii++){.    if
3a13b 28 20 69 69 3d 3d 28 6e 43 65 6c 6c 2d 31 29 20  ( ii==(nCell-1) 
3a13c 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
3a13d 26 61 43 65 6c 6c 5b 69 69 5d 2c 20 70 43 65 6c  &aCell[ii], pCel
3a13e 6c 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 43  l, sizeof(RtreeC
3a13f 65 6c 6c 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ell));.    }else
3a140 7b 0a 20 20 20 20 20 20 6e 6f 64 65 47 65 74 43  {.      nodeGetC
3a141 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  ell(pRtree, pNod
3a142 65 2c 20 69 69 2c 20 26 61 43 65 6c 6c 5b 69 69  e, ii, &aCell[ii
3a143 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 4f  ]);.    }.    aO
3a144 72 64 65 72 5b 69 69 5d 20 3d 20 69 69 3b 0a 20  rder[ii] = ii;. 
3a145 20 20 20 66 6f 72 28 69 44 69 6d 3d 30 3b 20 69     for(iDim=0; i
3a146 44 69 6d 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d  Dim<pRtree->nDim
3a147 3b 20 69 44 69 6d 2b 2b 29 7b 0a 20 20 20 20 20  ; iDim++){.     
3a148 20 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44   aCenterCoord[iD
3a149 69 6d 5d 20 2b 3d 20 44 43 4f 4f 52 44 28 61 43  im] += DCOORD(aC
3a14a 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64 5b 69  ell[ii].aCoord[i
3a14b 44 69 6d 2a 32 5d 29 3b 0a 20 20 20 20 20 20 61  Dim*2]);.      a
3a14c 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d  CenterCoord[iDim
3a14d 5d 20 2b 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c  ] += DCOORD(aCel
3a14e 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64 5b 69 44 69  l[ii].aCoord[iDi
3a14f 6d 2a 32 2b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20  m*2+1]);.    }. 
3a150 20 7d 0a 20 20 66 6f 72 28 69 44 69 6d 3d 30 3b   }.  for(iDim=0;
3a151 20 69 44 69 6d 3c 70 52 74 72 65 65 2d 3e 6e 44   iDim<pRtree->nD
3a152 69 6d 3b 20 69 44 69 6d 2b 2b 29 7b 0a 20 20 20  im; iDim++){.   
3a153 20 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44   aCenterCoord[iD
3a154 69 6d 5d 20 3d 20 61 43 65 6e 74 65 72 43 6f 6f  im] = aCenterCoo
3a155 72 64 5b 69 44 69 6d 5d 2f 28 28 66 6c 6f 61 74  rd[iDim]/((float
3a156 29 6e 43 65 6c 6c 2a 32 2e 30 29 3b 0a 20 20 7d  )nCell*2.0);.  }
3a157 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ..  for(ii=0; ii
3a158 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20  <nCell; ii++){. 
3a159 20 20 20 61 44 69 73 74 61 6e 63 65 5b 69 69 5d     aDistance[ii]
3a15a 20 3d 20 30 2e 30 3b 0a 20 20 20 20 66 6f 72 28   = 0.0;.    for(
3a15b 69 44 69 6d 3d 30 3b 20 69 44 69 6d 3c 70 52 74  iDim=0; iDim<pRt
3a15c 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 44 69 6d 2b  ree->nDim; iDim+
3a15d 2b 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  +){.      float 
3a15e 63 6f 6f 72 64 20 3d 20 44 43 4f 4f 52 44 28 61  coord = DCOORD(a
3a15f 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64 5b  Cell[ii].aCoord[
3a160 69 44 69 6d 2a 32 2b 31 5d 29 20 2d 20 0a 20 20  iDim*2+1]) - .  
3a161 20 20 20 20 20 20 20 20 44 43 4f 4f 52 44 28 61          DCOORD(a
3a162 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64 5b  Cell[ii].aCoord[
3a163 69 44 69 6d 2a 32 5d 29 3b 0a 20 20 20 20 20 20  iDim*2]);.      
3a164 61 44 69 73 74 61 6e 63 65 5b 69 69 5d 20 2b 3d  aDistance[ii] +=
3a165 20 28 63 6f 6f 72 64 2d 61 43 65 6e 74 65 72 43   (coord-aCenterC
3a166 6f 6f 72 64 5b 69 44 69 6d 5d 29 2a 28 63 6f 6f  oord[iDim])*(coo
3a167 72 64 2d 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b  rd-aCenterCoord[
3a168 69 44 69 6d 5d 29 3b 0a 20 20 20 20 7d 0a 20 20  iDim]);.    }.  
3a169 7d 0a 0a 20 20 53 6f 72 74 42 79 44 69 73 74 61  }..  SortByDista
3a16a 6e 63 65 28 61 4f 72 64 65 72 2c 20 6e 43 65 6c  nce(aOrder, nCel
3a16b 6c 2c 20 61 44 69 73 74 61 6e 63 65 2c 20 61 53  l, aDistance, aS
3a16c 70 61 72 65 29 3b 0a 20 20 6e 6f 64 65 5a 65 72  pare);.  nodeZer
3a16d 6f 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29  o(pRtree, pNode)
3a16e 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 72  ;..  for(ii=0; r
3a16f 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
3a170 69 69 3c 28 6e 43 65 6c 6c 2d 28 52 54 52 45 45  ii<(nCell-(RTREE
3a171 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65  _MINCELLS(pRtree
3a172 29 2b 31 29 29 3b 20 69 69 2b 2b 29 7b 0a 20 20  )+1)); ii++){.  
3a173 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 20 3d    RtreeCell *p =
3a174 20 26 61 43 65 6c 6c 5b 61 4f 72 64 65 72 5b 69   &aCell[aOrder[i
3a175 69 5d 5d 3b 0a 20 20 20 20 6e 6f 64 65 49 6e 73  i]];.    nodeIns
3a176 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  ertCell(pRtree, 
3a177 70 4e 6f 64 65 2c 20 70 29 3b 0a 20 20 20 20 69  pNode, p);.    i
3a178 66 28 20 70 2d 3e 69 52 6f 77 69 64 3d 3d 70 43  f( p->iRowid==pC
3a179 65 6c 6c 2d 3e 69 52 6f 77 69 64 20 29 7b 0a 20  ell->iRowid ){. 
3a17a 20 20 20 20 20 69 66 28 20 69 48 65 69 67 68 74       if( iHeight
3a17b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
3a17c 63 20 3d 20 72 6f 77 69 64 57 72 69 74 65 28 70  c = rowidWrite(p
3a17d 52 74 72 65 65 2c 20 70 2d 3e 69 52 6f 77 69 64  Rtree, p->iRowid
3a17e 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b  , pNode->iNode);
3a17f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
3a180 20 20 20 20 20 20 72 63 20 3d 20 70 61 72 65 6e        rc = paren
3a181 74 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70  tWrite(pRtree, p
3a182 2d 3e 69 52 6f 77 69 64 2c 20 70 4e 6f 64 65 2d  ->iRowid, pNode-
3a183 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d  >iNode);.      }
3a184 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
3a185 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
3a186 7b 0a 20 20 20 20 66 69 78 42 6f 75 6e 64 69 6e  {.    fixBoundin
3a187 67 42 6f 78 28 70 52 74 72 65 65 2c 20 70 4e 6f  gBox(pRtree, pNo
3a188 64 65 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 3b  de);.  }.  for(;
3a189 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
3a18a 26 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b  & ii<nCell; ii++
3a18b 29 7b 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61  ){.    /* Find a
3a18c 20 6e 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74   node to store t
3a18d 68 69 73 20 63 65 6c 6c 20 69 6e 2e 20 70 4e 6f  his cell in. pNo
3a18e 64 65 2d 3e 69 4e 6f 64 65 20 63 75 72 72 65 6e  de->iNode curren
3a18f 74 6c 79 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20  tly contains.   
3a190 20 2a 2a 20 74 68 65 20 68 65 69 67 68 74 20 6f   ** the height o
3a191 66 20 74 68 65 20 73 75 62 2d 74 72 65 65 20 68  f the sub-tree h
3a192 65 61 64 65 64 20 62 79 20 74 68 65 20 63 65 6c  eaded by the cel
3a193 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 52 74  l..    */.    Rt
3a194 72 65 65 4e 6f 64 65 20 2a 70 49 6e 73 65 72 74  reeNode *pInsert
3a195 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20  ;.    RtreeCell 
3a196 2a 70 20 3d 20 26 61 43 65 6c 6c 5b 61 4f 72 64  *p = &aCell[aOrd
3a197 65 72 5b 69 69 5d 5d 3b 0a 20 20 20 20 72 63 20  er[ii]];.    rc 
3a198 3d 20 43 68 6f 6f 73 65 4c 65 61 66 28 70 52 74  = ChooseLeaf(pRt
3a199 72 65 65 2c 20 70 2c 20 69 48 65 69 67 68 74 2c  ree, p, iHeight,
3a19a 20 26 70 49 6e 73 65 72 74 29 3b 0a 20 20 20 20   &pInsert);.    
3a19b 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3a19c 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  K ){.      int r
3a19d 63 32 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72  c2;.      rc = r
3a19e 74 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 70  treeInsertCell(p
3a19f 52 74 72 65 65 2c 20 70 49 6e 73 65 72 74 2c 20  Rtree, pInsert, 
3a1a0 70 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20 20  p, iHeight);.   
3a1a1 20 20 20 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c     rc2 = nodeRel
3a1a2 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 49 6e  ease(pRtree, pIn
3a1a3 73 65 72 74 29 3b 0a 20 20 20 20 20 20 69 66 28  sert);.      if(
3a1a4 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
3a1a5 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72  {.        rc = r
3a1a6 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  c2;.      }.    
3a1a7 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33  }.  }..  sqlite3
3a1a8 5f 66 72 65 65 28 61 43 65 6c 6c 29 3b 0a 20 20  _free(aCell);.  
3a1a9 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
3a1aa 0a 2a 2a 20 49 6e 73 65 72 74 20 63 65 6c 6c 20  .** Insert cell 
3a1ab 70 43 65 6c 6c 20 69 6e 74 6f 20 6e 6f 64 65 20  pCell into node 
3a1ac 70 4e 6f 64 65 2e 20 4e 6f 64 65 20 70 4e 6f 64  pNode. Node pNod
3a1ad 65 20 69 73 20 74 68 65 20 68 65 61 64 20 6f 66  e is the head of
3a1ae 20 61 20 0a 2a 2a 20 73 75 62 74 72 65 65 20 69   a .** subtree i
3a1af 48 65 69 67 68 74 20 68 69 67 68 20 28 6c 65 61  Height high (lea
3a1b0 66 20 6e 6f 64 65 73 20 68 61 76 65 20 69 48 65  f nodes have iHe
3a1b1 69 67 68 74 3d 3d 30 29 2e 0a 2a 2f 0a 73 74 61  ight==0)..*/.sta
3a1b2 74 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e 73  tic int rtreeIns
3a1b3 65 72 74 43 65 6c 6c 28 0a 20 20 52 74 72 65 65  ertCell(.  Rtree
3a1b4 20 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65   *pRtree,.  Rtre
3a1b5 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 0a 20 20  eNode *pNode,.  
3a1b6 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c  RtreeCell *pCell
3a1b7 2c 0a 20 20 69 6e 74 20 69 48 65 69 67 68 74 0a  ,.  int iHeight.
3a1b8 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
3a1b9 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69  LITE_OK;.  if( i
3a1ba 48 65 69 67 68 74 3e 30 20 29 7b 0a 20 20 20 20  Height>0 ){.    
3a1bb 52 74 72 65 65 4e 6f 64 65 20 2a 70 43 68 69 6c  RtreeNode *pChil
3a1bc 64 20 3d 20 6e 6f 64 65 48 61 73 68 4c 6f 6f 6b  d = nodeHashLook
3a1bd 75 70 28 70 52 74 72 65 65 2c 20 70 43 65 6c 6c  up(pRtree, pCell
3a1be 2d 3e 69 52 6f 77 69 64 29 3b 0a 20 20 20 20 69  ->iRowid);.    i
3a1bf 66 28 20 70 43 68 69 6c 64 20 29 7b 0a 20 20 20  f( pChild ){.   
3a1c0 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70     nodeRelease(p
3a1c1 52 74 72 65 65 2c 20 70 43 68 69 6c 64 2d 3e 70  Rtree, pChild->p
3a1c2 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 6e  Parent);.      n
3a1c3 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70 4e 6f  odeReference(pNo
3a1c4 64 65 29 3b 0a 20 20 20 20 20 20 70 43 68 69 6c  de);.      pChil
3a1c5 64 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f  d->pParent = pNo
3a1c6 64 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  de;.    }.  }.  
3a1c7 69 66 28 20 6e 6f 64 65 49 6e 73 65 72 74 43 65  if( nodeInsertCe
3a1c8 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
3a1c9 2c 20 70 43 65 6c 6c 29 20 29 7b 0a 23 69 66 20  , pCell) ){.#if 
3a1ca 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45  VARIANT_RSTARTRE
3a1cb 45 5f 52 45 49 4e 53 45 52 54 0a 20 20 20 20 69  E_REINSERT.    i
3a1cc 66 28 20 69 48 65 69 67 68 74 3c 3d 70 52 74 72  f( iHeight<=pRtr
3a1cd 65 65 2d 3e 69 52 65 69 6e 73 65 72 74 48 65 69  ee->iReinsertHei
3a1ce 67 68 74 20 7c 7c 20 70 4e 6f 64 65 2d 3e 69 4e  ght || pNode->iN
3a1cf 6f 64 65 3d 3d 31 29 7b 0a 20 20 20 20 20 20 72  ode==1){.      r
3a1d0 63 20 3d 20 53 70 6c 69 74 4e 6f 64 65 28 70 52  c = SplitNode(pR
3a1d1 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65  tree, pNode, pCe
3a1d2 6c 6c 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20  ll, iHeight);.  
3a1d3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
3a1d4 52 74 72 65 65 2d 3e 69 52 65 69 6e 73 65 72 74  Rtree->iReinsert
3a1d5 48 65 69 67 68 74 20 3d 20 69 48 65 69 67 68 74  Height = iHeight
3a1d6 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 52 65 69  ;.      rc = Rei
3a1d7 6e 73 65 72 74 28 70 52 74 72 65 65 2c 20 70 4e  nsert(pRtree, pN
3a1d8 6f 64 65 2c 20 70 43 65 6c 6c 2c 20 69 48 65 69  ode, pCell, iHei
3a1d9 67 68 74 29 3b 0a 20 20 20 20 7d 0a 23 65 6c 73  ght);.    }.#els
3a1da 65 0a 20 20 20 20 72 63 20 3d 20 53 70 6c 69 74  e.    rc = Split
3a1db 4e 6f 64 65 28 70 52 74 72 65 65 2c 20 70 4e 6f  Node(pRtree, pNo
3a1dc 64 65 2c 20 70 43 65 6c 6c 2c 20 69 48 65 69 67  de, pCell, iHeig
3a1dd 68 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65  ht);.#endif.  }e
3a1de 6c 73 65 7b 0a 20 20 20 20 41 64 6a 75 73 74 54  lse{.    AdjustT
3a1df 72 65 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64  ree(pRtree, pNod
3a1e0 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 69  e, pCell);.    i
3a1e1 66 28 20 69 48 65 69 67 68 74 3d 3d 30 20 29 7b  f( iHeight==0 ){
3a1e2 0a 20 20 20 20 20 20 72 63 20 3d 20 72 6f 77 69  .      rc = rowi
3a1e3 64 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70  dWrite(pRtree, p
3a1e4 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 2c 20 70 4e  Cell->iRowid, pN
3a1e5 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20  ode->iNode);.   
3a1e6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
3a1e7 20 3d 20 70 61 72 65 6e 74 57 72 69 74 65 28 70   = parentWrite(p
3a1e8 52 74 72 65 65 2c 20 70 43 65 6c 6c 2d 3e 69 52  Rtree, pCell->iR
3a1e9 6f 77 69 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f  owid, pNode->iNo
3a1ea 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  de);.    }.  }. 
3a1eb 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73   return rc;.}..s
3a1ec 74 61 74 69 63 20 69 6e 74 20 72 65 69 6e 73 65  tatic int reinse
3a1ed 72 74 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 52 74  rtNodeContent(Rt
3a1ee 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
3a1ef 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a  eeNode *pNode){.
3a1f0 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20    int ii;.  int 
3a1f1 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
3a1f2 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43    int nCell = NC
3a1f3 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 0a 20 20 66  ELL(pNode);..  f
3a1f4 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c  or(ii=0; rc==SQL
3a1f5 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e 43 65  ITE_OK && ii<nCe
3a1f6 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52  ll; ii++){.    R
3a1f7 74 72 65 65 4e 6f 64 65 20 2a 70 49 6e 73 65 72  treeNode *pInser
3a1f8 74 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c  t;.    RtreeCell
3a1f9 20 63 65 6c 6c 3b 0a 20 20 20 20 6e 6f 64 65 47   cell;.    nodeG
3a1fa 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  etCell(pRtree, p
3a1fb 4e 6f 64 65 2c 20 69 69 2c 20 26 63 65 6c 6c 29  Node, ii, &cell)
3a1fc 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61  ;..    /* Find a
3a1fd 20 6e 6f 64 65 20 74 6f 20 73 74 6f 72 65 20 74   node to store t
3a1fe 68 69 73 20 63 65 6c 6c 20 69 6e 2e 20 70 4e 6f  his cell in. pNo
3a1ff 64 65 2d 3e 69 4e 6f 64 65 20 63 75 72 72 65 6e  de->iNode curren
3a200 74 6c 79 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20  tly contains.   
3a201 20 2a 2a 20 74 68 65 20 68 65 69 67 68 74 20 6f   ** the height o
3a202 66 20 74 68 65 20 73 75 62 2d 74 72 65 65 20 68  f the sub-tree h
3a203 65 61 64 65 64 20 62 79 20 74 68 65 20 63 65 6c  eaded by the cel
3a204 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63  l..    */.    rc
3a205 20 3d 20 43 68 6f 6f 73 65 4c 65 61 66 28 70 52   = ChooseLeaf(pR
3a206 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70 4e 6f  tree, &cell, pNo
3a207 64 65 2d 3e 69 4e 6f 64 65 2c 20 26 70 49 6e 73  de->iNode, &pIns
3a208 65 72 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ert);.    if( rc
3a209 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3a20a 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 20 20       int rc2;.  
3a20b 20 20 20 20 72 63 20 3d 20 72 74 72 65 65 49 6e      rc = rtreeIn
3a20c 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
3a20d 20 70 49 6e 73 65 72 74 2c 20 26 63 65 6c 6c 2c   pInsert, &cell,
3a20e 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a   pNode->iNode);.
3a20f 20 20 20 20 20 20 72 63 32 20 3d 20 6e 6f 64 65        rc2 = node
3a210 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
3a211 70 49 6e 73 65 72 74 29 3b 0a 20 20 20 20 20 20  pInsert);.      
3a212 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3a213 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  K ){.        rc 
3a214 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20  = rc2;.      }. 
3a215 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
3a216 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  n rc;.}../*.** S
3a217 65 6c 65 63 74 20 61 20 63 75 72 72 65 6e 74 6c  elect a currentl
3a218 79 20 75 6e 75 73 65 64 20 72 6f 77 69 64 20 66  y unused rowid f
3a219 6f 72 20 61 20 6e 65 77 20 72 2d 74 72 65 65 20  or a new r-tree 
3a21a 72 65 63 6f 72 64 2e 0a 2a 2f 0a 73 74 61 74 69  record..*/.stati
3a21b 63 20 69 6e 74 20 6e 65 77 52 6f 77 69 64 28 52  c int newRowid(R
3a21c 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 69 36  tree *pRtree, i6
3a21d 34 20 2a 70 69 52 6f 77 69 64 29 7b 0a 20 20 69  4 *piRowid){.  i
3a21e 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33  nt rc;.  sqlite3
3a21f 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 52 74 72 65  _bind_null(pRtre
3a220 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 2c 20  e->pWriteRowid, 
3a221 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69  1);.  sqlite3_bi
3a222 6e 64 5f 6e 75 6c 6c 28 70 52 74 72 65 65 2d 3e  nd_null(pRtree->
3a223 70 57 72 69 74 65 52 6f 77 69 64 2c 20 32 29 3b  pWriteRowid, 2);
3a224 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  .  sqlite3_step(
3a225 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f  pRtree->pWriteRo
3a226 77 69 64 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  wid);.  rc = sql
3a227 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65  ite3_reset(pRtre
3a228 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29 3b  e->pWriteRowid);
3a229 0a 20 20 2a 70 69 52 6f 77 69 64 20 3d 20 73 71  .  *piRowid = sq
3a22a 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
3a22b 74 5f 72 6f 77 69 64 28 70 52 74 72 65 65 2d 3e  t_rowid(pRtree->
3a22c 64 62 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  db);.  return rc
3a22d 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  ;.}..#ifndef NDE
3a22e 42 55 47 0a 73 74 61 74 69 63 20 69 6e 74 20 68  BUG.static int h
3a22f 61 73 68 49 73 45 6d 70 74 79 28 52 74 72 65 65  ashIsEmpty(Rtree
3a230 20 2a 70 52 74 72 65 65 29 7b 0a 20 20 69 6e 74   *pRtree){.  int
3a231 20 69 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b   ii;.  for(ii=0;
3a232 20 69 69 3c 48 41 53 48 53 49 5a 45 3b 20 69 69   ii<HASHSIZE; ii
3a233 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ++){.    assert(
3a234 20 21 70 52 74 72 65 65 2d 3e 61 48 61 73 68 5b   !pRtree->aHash[
3a235 69 69 5d 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ii] );.  }.  ret
3a236 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a  urn 1;.}.#endif.
3a237 0a 2f 2a 0a 2a 2a 20 54 68 65 20 78 55 70 64 61  ./*.** The xUpda
3a238 74 65 20 6d 65 74 68 6f 64 20 66 6f 72 20 72 74  te method for rt
3a239 72 65 65 20 6d 6f 64 75 6c 65 20 76 69 72 74 75  ree module virtu
3a23a 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 73 74  al tables..*/.st
3a23b 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 55 70  atic int rtreeUp
3a23c 64 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  date(.  sqlite3_
3a23d 76 74 61 62 20 2a 70 56 74 61 62 2c 20 0a 20 20  vtab *pVtab, .  
3a23e 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 73 71  int nData, .  sq
3a23f 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 7a  lite3_value **az
3a240 44 61 74 61 2c 20 0a 20 20 73 71 6c 69 74 65 5f  Data, .  sqlite_
3a241 69 6e 74 36 34 20 2a 70 52 6f 77 69 64 0a 29 7b  int64 *pRowid.){
3a242 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
3a243 20 3d 20 28 52 74 72 65 65 20 2a 29 70 56 74 61   = (Rtree *)pVta
3a244 62 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  b;.  int rc = SQ
3a245 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 72 74 72 65  LITE_OK;..  rtre
3a246 65 52 65 66 65 72 65 6e 63 65 28 70 52 74 72 65  eReference(pRtre
3a247 65 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 6e 44  e);..  assert(nD
3a248 61 74 61 3e 3d 31 29 3b 0a 20 20 61 73 73 65 72  ata>=1);.  asser
3a249 74 28 68 61 73 68 49 73 45 6d 70 74 79 28 70 52  t(hashIsEmpty(pR
3a24a 74 72 65 65 29 29 3b 0a 0a 20 20 2f 2a 20 49 66  tree));..  /* If
3a24b 20 61 7a 44 61 74 61 5b 30 5d 20 69 73 20 6e 6f   azData[0] is no
3a24c 74 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76 61  t an SQL NULL va
3a24d 6c 75 65 2c 20 69 74 20 69 73 20 74 68 65 20 72  lue, it is the r
3a24e 6f 77 69 64 20 6f 66 20 61 0a 20 20 2a 2a 20 72  owid of a.  ** r
3a24f 65 63 6f 72 64 20 74 6f 20 64 65 6c 65 74 65 20  ecord to delete 
3a250 66 72 6f 6d 20 74 68 65 20 72 2d 74 72 65 65 20  from the r-tree 
3a251 74 61 62 6c 65 2e 20 54 68 65 20 66 6f 6c 6c 6f  table. The follo
3a252 77 69 6e 67 20 62 6c 6f 63 6b 20 64 6f 65 73 0a  wing block does.
3a253 20 20 2a 2a 20 6a 75 73 74 20 74 68 61 74 2e 0a    ** just that..
3a254 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
3a255 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 7a  e3_value_type(az
3a256 44 61 74 61 5b 30 5d 29 21 3d 53 51 4c 49 54 45  Data[0])!=SQLITE
3a257 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69 36 34  _NULL ){.    i64
3a258 20 69 44 65 6c 65 74 65 3b 20 20 20 20 20 20 20   iDelete;       
3a259 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
3a25a 72 6f 77 69 64 20 74 6f 20 64 65 6c 65 74 65 20  rowid to delete 
3a25b 2a 2f 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65  */.    RtreeNode
3a25c 20 2a 70 4c 65 61 66 3b 20 20 20 20 20 20 20 20   *pLeaf;        
3a25d 20 20 20 2f 2a 20 4c 65 61 66 20 6e 6f 64 65 20     /* Leaf node 
3a25e 63 6f 6e 74 61 69 6e 69 6e 67 20 72 65 63 6f 72  containing recor
3a25f 64 20 69 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20  d iDelete */.   
3a260 20 69 6e 74 20 69 43 65 6c 6c 3b 20 20 20 20 20   int iCell;     
3a261 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a262 49 6e 64 65 78 20 6f 66 20 69 44 65 6c 65 74 65  Index of iDelete
3a263 20 63 65 6c 6c 20 69 6e 20 70 4c 65 61 66 20 2a   cell in pLeaf *
3a264 2f 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20  /.    RtreeNode 
3a265 2a 70 52 6f 6f 74 3b 0a 0a 20 20 20 20 2f 2a 20  *pRoot;..    /* 
3a266 4f 62 74 61 69 6e 20 61 20 72 65 66 65 72 65 6e  Obtain a referen
3a267 63 65 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e  ce to the root n
3a268 6f 64 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 73  ode to initialis
3a269 65 20 52 74 72 65 65 2e 69 44 65 70 74 68 20 2a  e Rtree.iDepth *
3a26a 2f 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 41  /.    rc = nodeA
3a26b 63 71 75 69 72 65 28 70 52 74 72 65 65 2c 20 31  cquire(pRtree, 1
3a26c 2c 20 30 2c 20 26 70 52 6f 6f 74 29 3b 0a 0a 20  , 0, &pRoot);.. 
3a26d 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20 72     /* Obtain a r
3a26e 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20  eference to the 
3a26f 6c 65 61 66 20 6e 6f 64 65 20 74 68 61 74 20 63  leaf node that c
3a270 6f 6e 74 61 69 6e 73 20 74 68 65 20 65 6e 74 72  ontains the entr
3a271 79 20 0a 20 20 20 20 2a 2a 20 61 62 6f 75 74 20  y .    ** about 
3a272 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e 20 0a  to be deleted. .
3a273 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72      */.    if( r
3a274 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
3a275 20 20 20 20 20 20 69 44 65 6c 65 74 65 20 3d 20        iDelete = 
3a276 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
3a277 74 36 34 28 61 7a 44 61 74 61 5b 30 5d 29 3b 0a  t64(azData[0]);.
3a278 20 20 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c        rc = findL
3a279 65 61 66 4e 6f 64 65 28 70 52 74 72 65 65 2c 20  eafNode(pRtree, 
3a27a 69 44 65 6c 65 74 65 2c 20 26 70 4c 65 61 66 29  iDelete, &pLeaf)
3a27b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
3a27c 44 65 6c 65 74 65 20 74 68 65 20 63 65 6c 6c 20  Delete the cell 
3a27d 69 6e 20 71 75 65 73 74 69 6f 6e 20 66 72 6f 6d  in question from
3a27e 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 2e 20   the leaf node. 
3a27f 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  */.    if( rc==S
3a280 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3a281 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20    int rc2;.     
3a282 20 69 43 65 6c 6c 20 3d 20 6e 6f 64 65 52 6f 77   iCell = nodeRow
3a283 69 64 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20  idIndex(pRtree, 
3a284 70 4c 65 61 66 2c 20 69 44 65 6c 65 74 65 29 3b  pLeaf, iDelete);
3a285 0a 20 20 20 20 20 20 72 63 20 3d 20 64 65 6c 65  .      rc = dele
3a286 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  teCell(pRtree, p
3a287 4c 65 61 66 2c 20 69 43 65 6c 6c 2c 20 30 29 3b  Leaf, iCell, 0);
3a288 0a 20 20 20 20 20 20 72 63 32 20 3d 20 6e 6f 64  .      rc2 = nod
3a289 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
3a28a 20 70 4c 65 61 66 29 3b 0a 20 20 20 20 20 20 69   pLeaf);.      i
3a28b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
3a28c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
3a28d 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20   rc2;.      }.  
3a28e 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65    }..    /* Dele
3a28f 74 65 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  te the correspon
3a290 64 69 6e 67 20 65 6e 74 72 79 20 69 6e 20 74 68  ding entry in th
3a291 65 20 3c 72 74 72 65 65 3e 5f 72 6f 77 69 64 20  e <rtree>_rowid 
3a292 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66  table. */.    if
3a293 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
3a294 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
3a295 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72  _bind_int64(pRtr
3a296 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64  ee->pDeleteRowid
3a297 2c 20 31 2c 20 69 44 65 6c 65 74 65 29 3b 0a 20  , 1, iDelete);. 
3a298 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 65       sqlite3_ste
3a299 70 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  p(pRtree->pDelet
3a29a 65 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 72  eRowid);.      r
3a29b 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  c = sqlite3_rese
3a29c 74 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  t(pRtree->pDelet
3a29d 65 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 0a  eRowid);.    }..
3a29e 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20      /* Check if 
3a29f 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6e 6f  the root node no
3a2a0 77 20 68 61 73 20 65 78 61 63 74 6c 79 20 6f 6e  w has exactly on
3a2a1 65 20 63 68 69 6c 64 2e 20 49 66 20 73 6f 2c 20  e child. If so, 
3a2a2 72 65 6d 6f 76 65 0a 20 20 20 20 2a 2a 20 69 74  remove.    ** it
3a2a3 2c 20 73 63 68 65 64 75 6c 65 20 74 68 65 20 63  , schedule the c
3a2a4 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63  ontents of the c
3a2a5 68 69 6c 64 20 66 6f 72 20 72 65 69 6e 73 65 72  hild for reinser
3a2a6 74 69 6f 6e 20 61 6e 64 20 0a 20 20 20 20 2a 2a  tion and .    **
3a2a7 20 72 65 64 75 63 65 20 74 68 65 20 74 72 65 65   reduce the tree
3a2a8 20 68 65 69 67 68 74 20 62 79 20 6f 6e 65 2e 0a   height by one..
3a2a9 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
3a2aa 69 73 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74  is is equivalent
3a2ab 20 74 6f 20 63 6f 70 79 69 6e 67 20 74 68 65 20   to copying the 
3a2ac 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
3a2ad 63 68 69 6c 64 20 69 6e 74 6f 0a 20 20 20 20 2a  child into.    *
3a2ae 2a 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20  * the root node 
3a2af 28 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74  (the operation t
3a2b0 68 61 74 20 47 75 74 6d 61 6e 27 73 20 70 61 70  hat Gutman's pap
3a2b1 65 72 20 73 61 79 73 20 74 6f 20 70 65 72 66 6f  er says to perfo
3a2b2 72 6d 20 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68  rm .    ** in th
3a2b3 69 73 20 73 63 65 6e 61 72 69 6f 29 2e 0a 20 20  is scenario)..  
3a2b4 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d    */.    if( rc=
3a2b5 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 52  =SQLITE_OK && pR
3a2b6 74 72 65 65 2d 3e 69 44 65 70 74 68 3e 30 20 29  tree->iDepth>0 )
3a2b7 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  {.      if( rc==
3a2b8 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 4e 43 45  SQLITE_OK && NCE
3a2b9 4c 4c 28 70 52 6f 6f 74 29 3d 3d 31 20 29 7b 0a  LL(pRoot)==1 ){.
3a2ba 20 20 20 20 20 20 20 20 52 74 72 65 65 4e 6f 64          RtreeNod
3a2bb 65 20 2a 70 43 68 69 6c 64 3b 0a 20 20 20 20 20  e *pChild;.     
3a2bc 20 20 20 69 36 34 20 69 43 68 69 6c 64 20 3d 20     i64 iChild = 
3a2bd 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52 74  nodeGetRowid(pRt
3a2be 72 65 65 2c 20 70 52 6f 6f 74 2c 20 30 29 3b 0a  ree, pRoot, 0);.
3a2bf 20 20 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64          rc = nod
3a2c0 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65 2c  eAcquire(pRtree,
3a2c1 20 69 43 68 69 6c 64 2c 20 70 52 6f 6f 74 2c 20   iChild, pRoot, 
3a2c2 26 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20  &pChild);.      
3a2c3 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
3a2c4 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
3a2c5 20 72 63 20 3d 20 72 65 6d 6f 76 65 4e 6f 64 65   rc = removeNode
3a2c6 28 70 52 74 72 65 65 2c 20 70 43 68 69 6c 64 2c  (pRtree, pChild,
3a2c7 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d   pRtree->iDepth-
3a2c8 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  1);.        }.  
3a2c9 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
3a2ca 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
3a2cb 20 20 20 20 20 70 52 74 72 65 65 2d 3e 69 44 65       pRtree->iDe
3a2cc 70 74 68 2d 2d 3b 0a 20 20 20 20 20 20 20 20 20  pth--;.         
3a2cd 20 77 72 69 74 65 49 6e 74 31 36 28 70 52 6f 6f   writeInt16(pRoo
3a2ce 74 2d 3e 7a 44 61 74 61 2c 20 70 52 74 72 65 65  t->zData, pRtree
3a2cf 2d 3e 69 44 65 70 74 68 29 3b 0a 20 20 20 20 20  ->iDepth);.     
3a2d0 20 20 20 20 20 70 52 6f 6f 74 2d 3e 69 73 44 69       pRoot->isDi
3a2d1 72 74 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  rty = 1;.       
3a2d2 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
3a2d3 0a 0a 20 20 20 20 2f 2a 20 52 65 2d 69 6e 73 65  ..    /* Re-inse
3a2d4 72 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  rt the contents 
3a2d5 6f 66 20 61 6e 79 20 75 6e 64 65 72 66 75 6c 6c  of any underfull
3a2d6 20 6e 6f 64 65 73 20 72 65 6d 6f 76 65 64 20 66   nodes removed f
3a2d7 72 6f 6d 20 74 68 65 20 74 72 65 65 2e 20 2a 2f  rom the tree. */
3a2d8 0a 20 20 20 20 66 6f 72 28 70 4c 65 61 66 3d 70  .    for(pLeaf=p
3a2d9 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 64 3b  Rtree->pDeleted;
3a2da 20 70 4c 65 61 66 3b 20 70 4c 65 61 66 3d 70 52   pLeaf; pLeaf=pR
3a2db 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 64 29 7b  tree->pDeleted){
3a2dc 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
3a2dd 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3a2de 20 20 20 20 72 63 20 3d 20 72 65 69 6e 73 65 72      rc = reinser
3a2df 74 4e 6f 64 65 43 6f 6e 74 65 6e 74 28 70 52 74  tNodeContent(pRt
3a2e0 72 65 65 2c 20 70 4c 65 61 66 29 3b 0a 20 20 20  ree, pLeaf);.   
3a2e1 20 20 20 7d 0a 20 20 20 20 20 20 70 52 74 72 65     }.      pRtre
3a2e2 65 2d 3e 70 44 65 6c 65 74 65 64 20 3d 20 70 4c  e->pDeleted = pL
3a2e3 65 61 66 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  eaf->pNext;.    
3a2e4 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
3a2e5 4c 65 61 66 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  Leaf);.    }..  
3a2e6 20 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65    /* Release the
3a2e7 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68   reference to th
3a2e8 65 20 72 6f 6f 74 20 6e 6f 64 65 2e 20 2a 2f 0a  e root node. */.
3a2e9 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
3a2ea 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
3a2eb 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  c = nodeRelease(
3a2ec 70 52 74 72 65 65 2c 20 70 52 6f 6f 74 29 3b 0a  pRtree, pRoot);.
3a2ed 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3a2ee 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74   nodeRelease(pRt
3a2ef 72 65 65 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20  ree, pRoot);.   
3a2f0 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
3a2f1 74 68 65 20 61 7a 44 61 74 61 5b 5d 20 61 72 72  the azData[] arr
3a2f2 61 79 20 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65  ay contains more
3a2f3 20 74 68 61 6e 20 6f 6e 65 20 65 6c 65 6d 65 6e   than one elemen
3a2f4 74 2c 20 65 6c 65 6d 65 6e 74 73 0a 20 20 2a 2a  t, elements.  **
3a2f5 20 28 61 7a 44 61 74 61 5b 32 5d 2e 2e 61 7a 44   (azData[2]..azD
3a2f6 61 74 61 5b 61 72 67 63 2d 31 5d 29 20 63 6f 6e  ata[argc-1]) con
3a2f7 74 61 69 6e 20 61 20 6e 65 77 20 72 65 63 6f 72  tain a new recor
3a2f8 64 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f  d to insert into
3a2f9 0a 20 20 2a 2a 20 74 68 65 20 72 2d 74 72 65 65  .  ** the r-tree
3a2fa 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2f   structure..  */
3a2fb 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
3a2fc 45 5f 4f 4b 20 26 26 20 6e 44 61 74 61 3e 31 20  E_OK && nData>1 
3a2fd 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74  ){.    /* Insert
3a2fe 20 61 20 6e 65 77 20 72 65 63 6f 72 64 20 69 6e   a new record in
3a2ff 74 6f 20 74 68 65 20 72 2d 74 72 65 65 20 2a 2f  to the r-tree */
3a300 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 63  .    RtreeCell c
3a301 65 6c 6c 3b 0a 20 20 20 20 69 6e 74 20 69 69 3b  ell;.    int ii;
3a302 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  .    RtreeNode *
3a303 70 4c 65 61 66 3b 0a 0a 20 20 20 20 2f 2a 20 50  pLeaf;..    /* P
3a304 6f 70 75 6c 61 74 65 20 74 68 65 20 63 65 6c 6c  opulate the cell
3a305 2e 61 43 6f 6f 72 64 5b 5d 20 61 72 72 61 79 2e  .aCoord[] array.
3a306 20 54 68 65 20 66 69 72 73 74 20 63 6f 6f 72 64   The first coord
3a307 69 6e 61 74 65 20 69 73 20 61 7a 44 61 74 61 5b  inate is azData[
3a308 33 5d 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  3]. */.    asser
3a309 74 28 20 6e 44 61 74 61 3d 3d 28 70 52 74 72 65  t( nData==(pRtre
3a30a 65 2d 3e 6e 44 69 6d 2a 32 20 2b 20 33 29 20 29  e->nDim*2 + 3) )
3a30b 3b 0a 20 20 20 20 69 66 28 20 70 52 74 72 65 65  ;.    if( pRtree
3a30c 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54  ->eCoordType==RT
3a30d 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32  REE_COORD_REAL32
3a30e 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 69   ){.      for(ii
3a30f 3d 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e  =0; ii<(pRtree->
3a310 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b  nDim*2); ii+=2){
3a311 0a 20 20 20 20 20 20 20 20 63 65 6c 6c 2e 61 43  .        cell.aC
3a312 6f 6f 72 64 5b 69 69 5d 2e 66 20 3d 20 28 66 6c  oord[ii].f = (fl
3a313 6f 61 74 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  oat)sqlite3_valu
3a314 65 5f 64 6f 75 62 6c 65 28 61 7a 44 61 74 61 5b  e_double(azData[
3a315 69 69 2b 33 5d 29 3b 0a 20 20 20 20 20 20 20 20  ii+3]);.        
3a316 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31  cell.aCoord[ii+1
3a317 5d 2e 66 20 3d 20 28 66 6c 6f 61 74 29 73 71 6c  ].f = (float)sql
3a318 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c  ite3_value_doubl
3a319 65 28 61 7a 44 61 74 61 5b 69 69 2b 34 5d 29 3b  e(azData[ii+4]);
3a31a 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 65 6c  .        if( cel
3a31b 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 3e 63  l.aCoord[ii].f>c
3a31c 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d  ell.aCoord[ii+1]
3a31d 2e 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  .f ){.          
3a31e 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53  rc = SQLITE_CONS
3a31f 54 52 41 49 4e 54 3b 0a 20 20 20 20 20 20 20 20  TRAINT;.        
3a320 20 20 67 6f 74 6f 20 63 6f 6e 73 74 72 61 69 6e    goto constrain
3a321 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
3a322 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
3a323 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20        for(ii=0; 
3a324 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d  ii<(pRtree->nDim
3a325 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20  *2); ii+=2){.   
3a326 20 20 20 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64       cell.aCoord
3a327 5b 69 69 5d 2e 69 20 3d 20 73 71 6c 69 74 65 33  [ii].i = sqlite3
3a328 5f 76 61 6c 75 65 5f 69 6e 74 28 61 7a 44 61 74  _value_int(azDat
3a329 61 5b 69 69 2b 33 5d 29 3b 0a 20 20 20 20 20 20  a[ii+3]);.      
3a32a 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69    cell.aCoord[ii
3a32b 2b 31 5d 2e 69 20 3d 20 73 71 6c 69 74 65 33 5f  +1].i = sqlite3_
3a32c 76 61 6c 75 65 5f 69 6e 74 28 61 7a 44 61 74 61  value_int(azData
3a32d 5b 69 69 2b 34 5d 29 3b 0a 20 20 20 20 20 20 20  [ii+4]);.       
3a32e 20 69 66 28 20 63 65 6c 6c 2e 61 43 6f 6f 72 64   if( cell.aCoord
3a32f 5b 69 69 5d 2e 69 3e 63 65 6c 6c 2e 61 43 6f 6f  [ii].i>cell.aCoo
3a330 72 64 5b 69 69 2b 31 5d 2e 69 20 29 7b 0a 20 20  rd[ii+1].i ){.  
3a331 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
3a332 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a  ITE_CONSTRAINT;.
3a333 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 63            goto c
3a334 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 20  onstraint;.     
3a335 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
3a336 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72   }..    /* Figur
3a337 65 20 6f 75 74 20 74 68 65 20 72 6f 77 69 64 20  e out the rowid 
3a338 6f 66 20 74 68 65 20 6e 65 77 20 72 6f 77 2e 20  of the new row. 
3a339 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  */.    if( sqlit
3a33a 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 7a  e3_value_type(az
3a33b 44 61 74 61 5b 32 5d 29 3d 3d 53 51 4c 49 54 45  Data[2])==SQLITE
3a33c 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 72  _NULL ){.      r
3a33d 63 20 3d 20 6e 65 77 52 6f 77 69 64 28 70 52 74  c = newRowid(pRt
3a33e 72 65 65 2c 20 26 63 65 6c 6c 2e 69 52 6f 77 69  ree, &cell.iRowi
3a33f 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  d);.    }else{. 
3a340 20 20 20 20 20 63 65 6c 6c 2e 69 52 6f 77 69 64       cell.iRowid
3a341 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
3a342 5f 69 6e 74 36 34 28 61 7a 44 61 74 61 5b 32 5d  _int64(azData[2]
3a343 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
3a344 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72  _bind_int64(pRtr
3a345 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 2c 20  ee->pReadRowid, 
3a346 31 2c 20 63 65 6c 6c 2e 69 52 6f 77 69 64 29 3b  1, cell.iRowid);
3a347 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54  .      if( SQLIT
3a348 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73  E_ROW==sqlite3_s
3a349 74 65 70 28 70 52 74 72 65 65 2d 3e 70 52 65 61  tep(pRtree->pRea
3a34a 64 52 6f 77 69 64 29 20 29 7b 0a 20 20 20 20 20  dRowid) ){.     
3a34b 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74     sqlite3_reset
3a34c 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f  (pRtree->pReadRo
3a34d 77 69 64 29 3b 0a 20 20 20 20 20 20 20 20 72 63  wid);.        rc
3a34e 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52   = SQLITE_CONSTR
3a34f 41 49 4e 54 3b 0a 20 20 20 20 20 20 20 20 67 6f  AINT;.        go
3a350 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74 3b 0a 20  to constraint;. 
3a351 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20       }.      rc 
3a352 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
3a353 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77  pRtree->pReadRow
3a354 69 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  id);.    }..    
3a355 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3a356 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
3a357 43 68 6f 6f 73 65 4c 65 61 66 28 70 52 74 72 65  ChooseLeaf(pRtre
3a358 65 2c 20 26 63 65 6c 6c 2c 20 30 2c 20 26 70 4c  e, &cell, 0, &pL
3a359 65 61 66 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  eaf);.    }.    
3a35a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3a35b 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72  K ){.      int r
3a35c 63 32 3b 0a 20 20 20 20 20 20 70 52 74 72 65 65  c2;.      pRtree
3a35d 2d 3e 69 52 65 69 6e 73 65 72 74 48 65 69 67 68  ->iReinsertHeigh
3a35e 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72 63  t = -1;.      rc
3a35f 20 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43 65   = rtreeInsertCe
3a360 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66  ll(pRtree, pLeaf
3a361 2c 20 26 63 65 6c 6c 2c 20 30 29 3b 0a 20 20 20  , &cell, 0);.   
3a362 20 20 20 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c     rc2 = nodeRel
3a363 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65  ease(pRtree, pLe
3a364 61 66 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  af);.      if( r
3a365 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
3a366 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32          rc = rc2
3a367 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
3a368 20 20 7d 0a 0a 63 6f 6e 73 74 72 61 69 6e 74 3a    }..constraint:
3a369 0a 20 20 72 74 72 65 65 52 65 6c 65 61 73 65 28  .  rtreeRelease(
3a36a 70 52 74 72 65 65 29 3b 0a 20 20 72 65 74 75 72  pRtree);.  retur
3a36b 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
3a36c 68 65 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f  he xRename metho
3a36d 64 20 66 6f 72 20 72 74 72 65 65 20 6d 6f 64 75  d for rtree modu
3a36e 6c 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  le virtual table
3a36f 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
3a370 20 72 74 72 65 65 52 65 6e 61 6d 65 28 73 71 6c   rtreeRename(sql
3a371 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
3a372 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
3a373 65 77 4e 61 6d 65 29 7b 0a 20 20 52 74 72 65 65  ewName){.  Rtree
3a374 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65   *pRtree = (Rtre
3a375 65 20 2a 29 70 56 74 61 62 3b 0a 20 20 69 6e 74  e *)pVtab;.  int
3a376 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
3a377 45 4d 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c  EM;.  char *zSql
3a378 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
3a379 74 66 28 0a 20 20 20 20 22 41 4c 54 45 52 20 54  tf(.    "ALTER T
3a37a 41 42 4c 45 20 25 51 2e 27 25 71 5f 6e 6f 64 65  ABLE %Q.'%q_node
3a37b 27 20 20 20 52 45 4e 41 4d 45 20 54 4f 20 5c 22  '   RENAME TO \"
3a37c 25 77 5f 6e 6f 64 65 5c 22 3b 22 0a 20 20 20 20  %w_node\";".    
3a37d 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e  "ALTER TABLE %Q.
3a37e 27 25 71 5f 70 61 72 65 6e 74 27 20 52 45 4e 41  '%q_parent' RENA
3a37f 4d 45 20 54 4f 20 5c 22 25 77 5f 70 61 72 65 6e  ME TO \"%w_paren
3a380 74 5c 22 3b 22 0a 20 20 20 20 22 41 4c 54 45 52  t\";".    "ALTER
3a381 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 72 6f   TABLE %Q.'%q_ro
3a382 77 69 64 27 20 20 52 45 4e 41 4d 45 20 54 4f 20  wid'  RENAME TO 
3a383 5c 22 25 77 5f 72 6f 77 69 64 5c 22 3b 22 0a 20  \"%w_rowid\";". 
3a384 20 20 20 2c 20 70 52 74 72 65 65 2d 3e 7a 44 62     , pRtree->zDb
3a385 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c  , pRtree->zName,
3a386 20 7a 4e 65 77 4e 61 6d 65 20 0a 20 20 20 20 2c   zNewName .    ,
3a387 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52   pRtree->zDb, pR
3a388 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 65  tree->zName, zNe
3a389 77 4e 61 6d 65 20 0a 20 20 20 20 2c 20 70 52 74  wName .    , pRt
3a38a 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65 65  ree->zDb, pRtree
3a38b 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e 61 6d  ->zName, zNewNam
3a38c 65 0a 20 20 29 3b 0a 20 20 69 66 28 20 7a 53 71  e.  );.  if( zSq
3a38d 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  l ){.    rc = sq
3a38e 6c 69 74 65 33 5f 65 78 65 63 28 70 52 74 72 65  lite3_exec(pRtre
3a38f 65 2d 3e 64 62 2c 20 7a 53 71 6c 2c 20 30 2c 20  e->db, zSql, 0, 
3a390 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
3a391 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20  e3_free(zSql);. 
3a392 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
3a393 7d 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65  }..static sqlite
3a394 33 5f 6d 6f 64 75 6c 65 20 72 74 72 65 65 4d 6f  3_module rtreeMo
3a395 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c 20 20 20  dule = {.  0,   
3a396 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a397 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
3a398 6e 20 2a 2f 0a 20 20 72 74 72 65 65 43 72 65 61  n */.  rtreeCrea
3a399 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  te,             
3a39a 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20 2d 20     /* xCreate - 
3a39b 63 72 65 61 74 65 20 61 20 74 61 62 6c 65 20 2a  create a table *
3a39c 2f 0a 20 20 72 74 72 65 65 43 6f 6e 6e 65 63 74  /.  rtreeConnect
3a39d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3a39e 2f 2a 20 78 43 6f 6e 6e 65 63 74 20 2d 20 63 6f  /* xConnect - co
3a39f 6e 6e 65 63 74 20 74 6f 20 61 6e 20 65 78 69 73  nnect to an exis
3a3a0 74 69 6e 67 20 74 61 62 6c 65 20 2a 2f 0a 20 20  ting table */.  
3a3a1 72 74 72 65 65 42 65 73 74 49 6e 64 65 78 2c 20  rtreeBestIndex, 
3a3a2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
3a3a3 42 65 73 74 49 6e 64 65 78 20 2d 20 44 65 74 65  BestIndex - Dete
3a3a4 72 6d 69 6e 65 20 73 65 61 72 63 68 20 73 74 72  rmine search str
3a3a5 61 74 65 67 79 20 2a 2f 0a 20 20 72 74 72 65 65  ategy */.  rtree
3a3a6 44 69 73 63 6f 6e 6e 65 63 74 2c 20 20 20 20 20  Disconnect,     
3a3a7 20 20 20 20 20 20 20 2f 2a 20 78 44 69 73 63 6f         /* xDisco
3a3a8 6e 6e 65 63 74 20 2d 20 44 69 73 63 6f 6e 6e 65  nnect - Disconne
3a3a9 63 74 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20  ct from a table 
3a3aa 2a 2f 0a 20 20 72 74 72 65 65 44 65 73 74 72 6f  */.  rtreeDestro
3a3ab 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y,              
3a3ac 20 2f 2a 20 78 44 65 73 74 72 6f 79 20 2d 20 44   /* xDestroy - D
3a3ad 72 6f 70 20 61 20 74 61 62 6c 65 20 2a 2f 0a 20  rop a table */. 
3a3ae 20 72 74 72 65 65 4f 70 65 6e 2c 20 20 20 20 20   rtreeOpen,     
3a3af 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a3b0 78 4f 70 65 6e 20 2d 20 6f 70 65 6e 20 61 20 63  xOpen - open a c
3a3b1 75 72 73 6f 72 20 2a 2f 0a 20 20 72 74 72 65 65  ursor */.  rtree
3a3b2 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20  Close,          
3a3b3 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65         /* xClose
3a3b4 20 2d 20 63 6c 6f 73 65 20 61 20 63 75 72 73 6f   - close a curso
3a3b5 72 20 2a 2f 0a 20 20 72 74 72 65 65 46 69 6c 74  r */.  rtreeFilt
3a3b6 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  er,             
3a3b7 20 20 20 2f 2a 20 78 46 69 6c 74 65 72 20 2d 20     /* xFilter - 
3a3b8 63 6f 6e 66 69 67 75 72 65 20 73 63 61 6e 20 63  configure scan c
3a3b9 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20  onstraints */.  
3a3ba 72 74 72 65 65 4e 65 78 74 2c 20 20 20 20 20 20  rtreeNext,      
3a3bb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
3a3bc 4e 65 78 74 20 2d 20 61 64 76 61 6e 63 65 20 61  Next - advance a
3a3bd 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 72 74 72   cursor */.  rtr
3a3be 65 65 45 6f 66 2c 20 20 20 20 20 20 20 20 20 20  eeEof,          
3a3bf 20 20 20 20 20 20 20 20 20 2f 2a 20 78 45 6f 66           /* xEof
3a3c0 20 2a 2f 0a 20 20 72 74 72 65 65 43 6f 6c 75 6d   */.  rtreeColum
3a3c1 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
3a3c2 20 20 2f 2a 20 78 43 6f 6c 75 6d 6e 20 2d 20 72    /* xColumn - r
3a3c3 65 61 64 20 64 61 74 61 20 2a 2f 0a 20 20 72 74  ead data */.  rt
3a3c4 72 65 65 52 6f 77 69 64 2c 20 20 20 20 20 20 20  reeRowid,       
3a3c5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 52 6f            /* xRo
3a3c6 77 69 64 20 2d 20 72 65 61 64 20 64 61 74 61 20  wid - read data 
3a3c7 2a 2f 0a 20 20 72 74 72 65 65 55 70 64 61 74 65  */.  rtreeUpdate
3a3c8 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3a3c9 20 2f 2a 20 78 55 70 64 61 74 65 20 2d 20 77 72   /* xUpdate - wr
3a3ca 69 74 65 20 64 61 74 61 20 2a 2f 0a 20 20 30 2c  ite data */.  0,
3a3cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a3cc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 42 65            /* xBe
3a3cd 67 69 6e 20 2d 20 62 65 67 69 6e 20 74 72 61 6e  gin - begin tran
3a3ce 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20  saction */.  0, 
3a3cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a3d0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53 79 6e           /* xSyn
3a3d1 63 20 2d 20 73 79 6e 63 20 74 72 61 6e 73 61 63  c - sync transac
3a3d2 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20 20  tion */.  0,    
3a3d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a3d4 20 20 20 20 20 20 2f 2a 20 78 43 6f 6d 6d 69 74        /* xCommit
3a3d5 20 2d 20 63 6f 6d 6d 69 74 20 74 72 61 6e 73 61   - commit transa
3a3d6 63 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20  ction */.  0,   
3a3d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a3d8 20 20 20 20 20 20 20 2f 2a 20 78 52 6f 6c 6c 62         /* xRollb
3a3d9 61 63 6b 20 2d 20 72 6f 6c 6c 62 61 63 6b 20 74  ack - rollback t
3a3da 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20  ransaction */.  
3a3db 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
3a3dc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
3a3dd 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20 2d 20 66  FindFunction - f
3a3de 75 6e 63 74 69 6f 6e 20 6f 76 65 72 6c 6f 61 64  unction overload
3a3df 69 6e 67 20 2a 2f 0a 20 20 72 74 72 65 65 52 65  ing */.  rtreeRe
3a3e0 6e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20  name            
3a3e1 20 20 20 20 20 2f 2a 20 78 52 65 6e 61 6d 65 20       /* xRename 
3a3e2 2d 20 72 65 6e 61 6d 65 20 74 68 65 20 74 61 62  - rename the tab
3a3e3 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 61 74 69 63  le */.};..static
3a3e4 20 69 6e 74 20 72 74 72 65 65 53 71 6c 49 6e 69   int rtreeSqlIni
3a3e5 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  t(.  Rtree *pRtr
3a3e6 65 65 2c 20 0a 20 20 73 71 6c 69 74 65 33 20 2a  ee, .  sqlite3 *
3a3e7 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  db, .  const cha
3a3e8 72 20 2a 7a 44 62 2c 20 0a 20 20 63 6f 6e 73 74  r *zDb, .  const
3a3e9 20 63 68 61 72 20 2a 7a 50 72 65 66 69 78 2c 20   char *zPrefix, 
3a3ea 0a 20 20 69 6e 74 20 69 73 43 72 65 61 74 65 0a  .  int isCreate.
3a3eb 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
3a3ec 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 23 64 65 66  LITE_OK;..  #def
3a3ed 69 6e 65 20 4e 5f 53 54 41 54 45 4d 45 4e 54 20  ine N_STATEMENT 
3a3ee 39 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  9.  static const
3a3ef 20 63 68 61 72 20 2a 61 7a 53 71 6c 5b 4e 5f 53   char *azSql[N_S
3a3f0 54 41 54 45 4d 45 4e 54 5d 20 3d 20 7b 0a 20 20  TATEMENT] = {.  
3a3f1 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72    /* Read and wr
3a3f2 69 74 65 20 74 68 65 20 78 78 78 5f 6e 6f 64 65  ite the xxx_node
3a3f3 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 22 53   table */.    "S
3a3f4 45 4c 45 43 54 20 64 61 74 61 20 46 52 4f 4d 20  ELECT data FROM 
3a3f5 27 25 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 57  '%q'.'%q_node' W
3a3f6 48 45 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31  HERE nodeno = :1
3a3f7 22 2c 0a 20 20 20 20 22 49 4e 53 45 52 54 20 4f  ",.    "INSERT O
3a3f8 52 20 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 27  R REPLACE INTO '
3a3f9 25 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 56 41  %q'.'%q_node' VA
3a3fa 4c 55 45 53 28 3a 31 2c 20 3a 32 29 22 2c 0a 20  LUES(:1, :2)",. 
3a3fb 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20     "DELETE FROM 
3a3fc 27 25 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 57  '%q'.'%q_node' W
3a3fd 48 45 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31  HERE nodeno = :1
3a3fe 22 2c 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20  ",..    /* Read 
3a3ff 61 6e 64 20 77 72 69 74 65 20 74 68 65 20 78 78  and write the xx
3a400 78 5f 72 6f 77 69 64 20 74 61 62 6c 65 20 2a 2f  x_rowid table */
3a401 0a 20 20 20 20 22 53 45 4c 45 43 54 20 6e 6f 64  .    "SELECT nod
3a402 65 6e 6f 20 46 52 4f 4d 20 27 25 71 27 2e 27 25  eno FROM '%q'.'%
3a403 71 5f 72 6f 77 69 64 27 20 57 48 45 52 45 20 72  q_rowid' WHERE r
3a404 6f 77 69 64 20 3d 20 3a 31 22 2c 0a 20 20 20 20  owid = :1",.    
3a405 22 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41  "INSERT OR REPLA
3a406 43 45 20 49 4e 54 4f 20 27 25 71 27 2e 27 25 71  CE INTO '%q'.'%q
3a407 5f 72 6f 77 69 64 27 20 56 41 4c 55 45 53 28 3a  _rowid' VALUES(:
3a408 31 2c 20 3a 32 29 22 2c 0a 20 20 20 20 22 44 45  1, :2)",.    "DE
3a409 4c 45 54 45 20 46 52 4f 4d 20 27 25 71 27 2e 27  LETE FROM '%q'.'
3a40a 25 71 5f 72 6f 77 69 64 27 20 57 48 45 52 45 20  %q_rowid' WHERE 
3a40b 72 6f 77 69 64 20 3d 20 3a 31 22 2c 0a 0a 20 20  rowid = :1",..  
3a40c 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72    /* Read and wr
3a40d 69 74 65 20 74 68 65 20 78 78 78 5f 70 61 72 65  ite the xxx_pare
3a40e 6e 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  nt table */.    
3a40f 22 53 45 4c 45 43 54 20 70 61 72 65 6e 74 6e 6f  "SELECT parentno
3a410 64 65 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71  de FROM '%q'.'%q
3a411 5f 70 61 72 65 6e 74 27 20 57 48 45 52 45 20 6e  _parent' WHERE n
3a412 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c 0a 20 20 20  odeno = :1",.   
3a413 20 22 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c   "INSERT OR REPL
3a414 41 43 45 20 49 4e 54 4f 20 27 25 71 27 2e 27 25  ACE INTO '%q'.'%
3a415 71 5f 70 61 72 65 6e 74 27 20 56 41 4c 55 45 53  q_parent' VALUES
3a416 28 3a 31 2c 20 3a 32 29 22 2c 0a 20 20 20 20 22  (:1, :2)",.    "
3a417 44 45 4c 45 54 45 20 46 52 4f 4d 20 27 25 71 27  DELETE FROM '%q'
3a418 2e 27 25 71 5f 70 61 72 65 6e 74 27 20 57 48 45  .'%q_parent' WHE
3a419 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 0a  RE nodeno = :1".
3a41a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73    };.  sqlite3_s
3a41b 74 6d 74 20 2a 2a 61 70 70 53 74 6d 74 5b 4e 5f  tmt **appStmt[N_
3a41c 53 54 41 54 45 4d 45 4e 54 5d 3b 0a 20 20 69 6e  STATEMENT];.  in
3a41d 74 20 69 3b 0a 0a 20 20 70 52 74 72 65 65 2d 3e  t i;..  pRtree->
3a41e 64 62 20 3d 20 64 62 3b 0a 0a 20 20 69 66 28 20  db = db;..  if( 
3a41f 69 73 43 72 65 61 74 65 20 29 7b 0a 20 20 20 20  isCreate ){.    
3a420 63 68 61 72 20 2a 7a 43 72 65 61 74 65 20 3d 20  char *zCreate = 
3a421 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
3a422 0a 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 5c  ."CREATE TABLE \
3a423 22 25 77 5c 22 2e 5c 22 25 77 5f 6e 6f 64 65 5c  "%w\".\"%w_node\
3a424 22 28 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45 52  "(nodeno INTEGER
3a425 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 64 61   PRIMARY KEY, da
3a426 74 61 20 42 4c 4f 42 29 3b 22 0a 22 43 52 45 41  ta BLOB);"."CREA
3a427 54 45 20 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e  TE TABLE \"%w\".
3a428 5c 22 25 77 5f 72 6f 77 69 64 5c 22 28 72 6f 77  \"%w_rowid\"(row
3a429 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  id INTEGER PRIMA
3a42a 52 59 20 4b 45 59 2c 20 6e 6f 64 65 6e 6f 20 49  RY KEY, nodeno I
3a42b 4e 54 45 47 45 52 29 3b 22 0a 22 43 52 45 41 54  NTEGER);"."CREAT
3a42c 45 20 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e 5c  E TABLE \"%w\".\
3a42d 22 25 77 5f 70 61 72 65 6e 74 5c 22 28 6e 6f 64  "%w_parent\"(nod
3a42e 65 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d  eno INTEGER PRIM
3a42f 41 52 59 20 4b 45 59 2c 20 70 61 72 65 6e 74 6e  ARY KEY, parentn
3a430 6f 64 65 20 49 4e 54 45 47 45 52 29 3b 22 0a 22  ode INTEGER);"."
3a431 49 4e 53 45 52 54 20 49 4e 54 4f 20 27 25 71 27  INSERT INTO '%q'
3a432 2e 27 25 71 5f 6e 6f 64 65 27 20 56 41 4c 55 45  .'%q_node' VALUE
3a433 53 28 31 2c 20 7a 65 72 6f 62 6c 6f 62 28 25 64  S(1, zeroblob(%d
3a434 29 29 22 2c 0a 20 20 20 20 20 20 7a 44 62 2c 20  ))",.      zDb, 
3a435 7a 50 72 65 66 69 78 2c 20 7a 44 62 2c 20 7a 50  zPrefix, zDb, zP
3a436 72 65 66 69 78 2c 20 7a 44 62 2c 20 7a 50 72 65  refix, zDb, zPre
3a437 66 69 78 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69  fix, zDb, zPrefi
3a438 78 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  x, pRtree->iNode
3a439 53 69 7a 65 0a 20 20 20 20 29 3b 0a 20 20 20 20  Size.    );.    
3a43a 69 66 28 20 21 7a 43 72 65 61 74 65 20 29 7b 0a  if( !zCreate ){.
3a43b 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
3a43c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
3a43d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
3a43e 33 5f 65 78 65 63 28 64 62 2c 20 7a 43 72 65 61  3_exec(db, zCrea
3a43f 74 65 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  te, 0, 0, 0);.  
3a440 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
3a441 43 72 65 61 74 65 29 3b 0a 20 20 20 20 69 66 28  Create);.    if(
3a442 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
3a443 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
3a444 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  c;.    }.  }..  
3a445 61 70 70 53 74 6d 74 5b 30 5d 20 3d 20 26 70 52  appStmt[0] = &pR
3a446 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65 3b  tree->pReadNode;
3a447 0a 20 20 61 70 70 53 74 6d 74 5b 31 5d 20 3d 20  .  appStmt[1] = 
3a448 26 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 4e  &pRtree->pWriteN
3a449 6f 64 65 3b 0a 20 20 61 70 70 53 74 6d 74 5b 32  ode;.  appStmt[2
3a44a 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 44 65  ] = &pRtree->pDe
3a44b 6c 65 74 65 4e 6f 64 65 3b 0a 20 20 61 70 70 53  leteNode;.  appS
3a44c 74 6d 74 5b 33 5d 20 3d 20 26 70 52 74 72 65 65  tmt[3] = &pRtree
3a44d 2d 3e 70 52 65 61 64 52 6f 77 69 64 3b 0a 20 20  ->pReadRowid;.  
3a44e 61 70 70 53 74 6d 74 5b 34 5d 20 3d 20 26 70 52  appStmt[4] = &pR
3a44f 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69  tree->pWriteRowi
3a450 64 3b 0a 20 20 61 70 70 53 74 6d 74 5b 35 5d 20  d;.  appStmt[5] 
3a451 3d 20 26 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  = &pRtree->pDele
3a452 74 65 52 6f 77 69 64 3b 0a 20 20 61 70 70 53 74  teRowid;.  appSt
3a453 6d 74 5b 36 5d 20 3d 20 26 70 52 74 72 65 65 2d  mt[6] = &pRtree-
3a454 3e 70 52 65 61 64 50 61 72 65 6e 74 3b 0a 20 20  >pReadParent;.  
3a455 61 70 70 53 74 6d 74 5b 37 5d 20 3d 20 26 70 52  appStmt[7] = &pR
3a456 74 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65  tree->pWritePare
3a457 6e 74 3b 0a 20 20 61 70 70 53 74 6d 74 5b 38 5d  nt;.  appStmt[8]
3a458 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 44 65 6c   = &pRtree->pDel
3a459 65 74 65 50 61 72 65 6e 74 3b 0a 0a 20 20 66 6f  eteParent;..  fo
3a45a 72 28 69 3d 30 3b 20 69 3c 4e 5f 53 54 41 54 45  r(i=0; i<N_STATE
3a45b 4d 45 4e 54 20 26 26 20 72 63 3d 3d 53 51 4c 49  MENT && rc==SQLI
3a45c 54 45 5f 4f 4b 3b 20 69 2b 2b 29 7b 0a 20 20 20  TE_OK; i++){.   
3a45d 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71   char *zSql = sq
3a45e 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 61 7a  lite3_mprintf(az
3a45f 53 71 6c 5b 69 5d 2c 20 7a 44 62 2c 20 7a 50 72  Sql[i], zDb, zPr
3a460 65 66 69 78 29 3b 0a 20 20 20 20 69 66 28 20 7a  efix);.    if( z
3a461 53 71 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20  Sql ){.      rc 
3a462 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
3a463 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d  e_v2(db, zSql, -
3a464 31 2c 20 61 70 70 53 74 6d 74 5b 69 5d 2c 20 30  1, appStmt[i], 0
3a465 29 3b 20 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ); .    }else{. 
3a466 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
3a467 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
3a468 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
3a469 53 71 6c 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  Sql);.  }..  ret
3a46a 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
3a46b 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 71 75   This routine qu
3a46c 65 72 69 65 73 20 64 61 74 61 62 61 73 65 20 68  eries database h
3a46d 61 6e 64 6c 65 20 64 62 20 66 6f 72 20 74 68 65  andle db for the
3a46e 20 70 61 67 65 2d 73 69 7a 65 20 75 73 65 64 20   page-size used 
3a46f 62 79 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 7a  by.** database z
3a470 44 62 2e 20 49 66 20 73 75 63 63 65 73 73 66 75  Db. If successfu
3a471 6c 2c 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65  l, the page-size
3a472 20 69 6e 20 62 79 74 65 73 20 69 73 20 77 72 69   in bytes is wri
3a473 74 74 65 6e 20 74 6f 0a 2a 2a 20 2a 70 69 50 61  tten to.** *piPa
3a474 67 65 53 69 7a 65 20 61 6e 64 20 53 51 4c 49 54  geSize and SQLIT
3a475 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 4f  E_OK returned. O
3a476 74 68 65 72 77 69 73 65 2c 20 61 6e 64 20 61 6e  therwise, and an
3a477 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 0a 2a   SQLite error .*
3a478 2a 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  * code is return
3a479 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
3a47a 74 20 67 65 74 50 61 67 65 53 69 7a 65 28 73 71  t getPageSize(sq
3a47b 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
3a47c 20 63 68 61 72 20 2a 7a 44 62 2c 20 69 6e 74 20   char *zDb, int 
3a47d 2a 70 69 50 61 67 65 53 69 7a 65 29 7b 0a 20 20  *piPageSize){.  
3a47e 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
3a47f 4e 4f 4d 45 4d 3b 0a 20 20 63 68 61 72 20 2a 7a  NOMEM;.  char *z
3a480 53 71 6c 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  Sql;.  sqlite3_s
3a481 74 6d 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b 0a  tmt *pStmt = 0;.
3a482 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65  .  zSql = sqlite
3a483 33 5f 6d 70 72 69 6e 74 66 28 22 50 52 41 47 4d  3_mprintf("PRAGM
3a484 41 20 25 51 2e 70 61 67 65 5f 73 69 7a 65 22 2c  A %Q.page_size",
3a485 20 7a 44 62 29 3b 0a 20 20 69 66 28 20 21 7a 53   zDb);.  if( !zS
3a486 71 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ql ){.    return
3a487 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
3a488 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   }..  rc = sqlit
3a489 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62  e3_prepare_v2(db
3a48a 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74  , zSql, -1, &pSt
3a48b 6d 74 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65  mt, 0);.  sqlite
3a48c 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20  3_free(zSql);.  
3a48d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
3a48e 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
3a48f 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 53  rc;.  }..  if( S
3a490 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74  QLITE_ROW==sqlit
3a491 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29  e3_step(pStmt) )
3a492 7b 0a 20 20 20 20 2a 70 69 50 61 67 65 53 69 7a  {.    *piPageSiz
3a493 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  e = sqlite3_colu
3a494 6d 6e 5f 69 6e 74 28 70 53 74 6d 74 2c 20 30 29  mn_int(pStmt, 0)
3a495 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73  ;.  }.  return s
3a496 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
3a497 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a  pStmt);.}../* .*
3a498 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
3a499 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  is the implement
3a49a 61 74 69 6f 6e 20 6f 66 20 62 6f 74 68 20 74 68  ation of both th
3a49b 65 20 78 43 6f 6e 6e 65 63 74 20 61 6e 64 20 78  e xConnect and x
3a49c 43 72 65 61 74 65 0a 2a 2a 20 6d 65 74 68 6f 64  Create.** method
3a49d 73 20 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20  s of the r-tree 
3a49e 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a  virtual table..*
3a49f 2a 0a 2a 2a 20 20 20 61 72 67 76 5b 30 5d 20 20  *.**   argv[0]  
3a4a0 20 2d 3e 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 0a   -> module name.
3a4a1 2a 2a 20 20 20 61 72 67 76 5b 31 5d 20 20 20 2d  **   argv[1]   -
3a4a2 3e 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a  > database name.
3a4a3 2a 2a 20 20 20 61 72 67 76 5b 32 5d 20 20 20 2d  **   argv[2]   -
3a4a4 3e 20 74 61 62 6c 65 20 6e 61 6d 65 0a 2a 2a 20  > table name.** 
3a4a5 20 20 61 72 67 76 5b 2e 2e 2e 5d 20 2d 3e 20 63    argv[...] -> c
3a4a6 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 2e 2e 0a 2a  olumn names....*
3a4a7 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72  /.static int rtr
3a4a8 65 65 49 6e 69 74 28 0a 20 20 73 71 6c 69 74 65  eeInit(.  sqlite
3a4a9 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
3a4aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3a4ab 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
3a4ac 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  tion */.  void *
3a4ad 70 41 75 78 2c 20 20 20 20 20 20 20 20 20 20 20  pAux,           
3a4ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3a4af 20 4f 6e 65 20 6f 66 20 74 68 65 20 52 54 52 45   One of the RTRE
3a4b0 45 5f 43 4f 4f 52 44 5f 2a 20 63 6f 6e 73 74 61  E_COORD_* consta
3a4b1 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 61 72 67  nts */.  int arg
3a4b2 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  c, const char *c
3a4b3 6f 6e 73 74 2a 61 72 67 76 2c 20 20 20 2f 2a 20  onst*argv,   /* 
3a4b4 50 61 72 61 6d 65 74 65 72 73 20 74 6f 20 43 52  Parameters to CR
3a4b5 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
3a4b6 6d 65 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ment */.  sqlite
3a4b7 33 5f 76 74 61 62 20 2a 2a 70 70 56 74 61 62 2c  3_vtab **ppVtab,
3a4b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3a4b9 20 4f 55 54 3a 20 4e 65 77 20 76 69 72 74 75 61   OUT: New virtua
3a4ba 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61  l table */.  cha
3a4bb 72 20 2a 2a 70 7a 45 72 72 2c 20 20 20 20 20 20  r **pzErr,      
3a4bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a4bd 20 2f 2a 20 4f 55 54 3a 20 45 72 72 6f 72 20 6d   /* OUT: Error m
3a4be 65 73 73 61 67 65 2c 20 69 66 20 61 6e 79 20 2a  essage, if any *
3a4bf 2f 0a 20 20 69 6e 74 20 69 73 43 72 65 61 74 65  /.  int isCreate
3a4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a4c1 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
3a4c2 66 6f 72 20 78 43 72 65 61 74 65 2c 20 66 61 6c  for xCreate, fal
3a4c3 73 65 20 66 6f 72 20 78 43 6f 6e 6e 65 63 74 20  se for xConnect 
3a4c4 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  */.){.  int rc =
3a4c5 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
3a4c6 74 20 69 50 61 67 65 53 69 7a 65 20 3d 20 30 3b  t iPageSize = 0;
3a4c7 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
3a4c8 3b 0a 20 20 69 6e 74 20 6e 44 62 3b 20 20 20 20  ;.  int nDb;    
3a4c9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
3a4ca 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 61 72  gth of string ar
3a4cb 67 76 5b 31 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e  gv[1] */.  int n
3a4cc 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
3a4cd 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74   /* Length of st
3a4ce 72 69 6e 67 20 61 72 67 76 5b 32 5d 20 2a 2f 0a  ring argv[2] */.
3a4cf 20 20 69 6e 74 20 65 43 6f 6f 72 64 54 79 70 65    int eCoordType
3a4d0 20 3d 20 28 69 6e 74 29 70 41 75 78 3b 0a 0a 20   = (int)pAux;.. 
3a4d1 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 45 72   const char *aEr
3a4d2 72 4d 73 67 5b 5d 20 3d 20 7b 0a 20 20 20 20 30  rMsg[] = {.    0
3a4d3 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3a4d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a4d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a4d6 20 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20       /* 0 */.   
3a4d7 20 22 57 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f   "Wrong number o
3a4d8 66 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 61 6e  f columns for an
3a4d9 20 72 74 72 65 65 20 74 61 62 6c 65 22 2c 20 20   rtree table",  
3a4da 20 20 20 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20         /* 1 */. 
3a4db 20 20 20 22 54 6f 6f 20 66 65 77 20 63 6f 6c 75     "Too few colu
3a4dc 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74 72 65 65  mns for an rtree
3a4dd 20 74 61 62 6c 65 22 2c 20 20 20 20 20 20 20 20   table",        
3a4de 20 20 20 20 20 20 20 20 20 2f 2a 20 32 20 2a 2f           /* 2 */
3a4df 0a 20 20 20 20 22 54 6f 6f 20 6d 61 6e 79 20 63  .    "Too many c
3a4e0 6f 6c 75 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74  olumns for an rt
3a4e1 72 65 65 20 74 61 62 6c 65 22 20 20 20 20 20 20  ree table"      
3a4e2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 33 20             /* 3 
3a4e3 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 69  */.  };..  int i
3a4e4 45 72 72 20 3d 20 28 61 72 67 63 3c 36 29 20 3f  Err = (argc<6) ?
3a4e5 20 32 20 3a 20 61 72 67 63 3e 28 52 54 52 45 45   2 : argc>(RTREE
3a4e6 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a  _MAX_DIMENSIONS*
3a4e7 32 2b 34 29 20 3f 20 33 20 3a 20 61 72 67 63 25  2+4) ? 3 : argc%
3a4e8 32 3b 0a 20 20 69 66 28 20 61 45 72 72 4d 73 67  2;.  if( aErrMsg
3a4e9 5b 69 45 72 72 5d 20 29 7b 0a 20 20 20 20 2a 70  [iErr] ){.    *p
3a4ea 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zErr = sqlite3_m
3a4eb 70 72 69 6e 74 66 28 22 25 73 22 2c 20 61 45 72  printf("%s", aEr
3a4ec 72 4d 73 67 5b 69 45 72 72 5d 29 3b 0a 20 20 20  rMsg[iErr]);.   
3a4ed 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
3a4ee 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 72 63 20  RROR;.  }..  rc 
3a4ef 3d 20 67 65 74 50 61 67 65 53 69 7a 65 28 64 62  = getPageSize(db
3a4f0 2c 20 61 72 67 76 5b 31 5d 2c 20 26 69 50 61 67  , argv[1], &iPag
3a4f1 65 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 72 63  eSize);.  if( rc
3a4f2 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
3a4f3 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
3a4f4 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  }..  /* Allocate
3a4f5 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61   the sqlite3_vta
3a4f6 62 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  b structure */. 
3a4f7 20 6e 44 62 20 3d 20 73 74 72 6c 65 6e 28 61 72   nDb = strlen(ar
3a4f8 67 76 5b 31 5d 29 3b 0a 20 20 6e 4e 61 6d 65 20  gv[1]);.  nName 
3a4f9 3d 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 32 5d  = strlen(argv[2]
3a4fa 29 3b 0a 20 20 70 52 74 72 65 65 20 3d 20 28 52  );.  pRtree = (R
3a4fb 74 72 65 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d  tree *)sqlite3_m
3a4fc 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72  alloc(sizeof(Rtr
3a4fd 65 65 29 2b 6e 44 62 2b 6e 4e 61 6d 65 2b 32 29  ee)+nDb+nName+2)
3a4fe 3b 0a 20 20 69 66 28 20 21 70 52 74 72 65 65 20  ;.  if( !pRtree 
3a4ff 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
3a500 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
3a501 20 20 6d 65 6d 73 65 74 28 70 52 74 72 65 65 2c    memset(pRtree,
3a502 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65   0, sizeof(Rtree
3a503 29 2b 6e 44 62 2b 6e 4e 61 6d 65 2b 32 29 3b 0a  )+nDb+nName+2);.
3a504 20 20 70 52 74 72 65 65 2d 3e 6e 42 75 73 79 20    pRtree->nBusy 
3a505 3d 20 31 3b 0a 20 20 70 52 74 72 65 65 2d 3e 62  = 1;.  pRtree->b
3a506 61 73 65 2e 70 4d 6f 64 75 6c 65 20 3d 20 26 72  ase.pModule = &r
3a507 74 72 65 65 4d 6f 64 75 6c 65 3b 0a 20 20 70 52  treeModule;.  pR
3a508 74 72 65 65 2d 3e 7a 44 62 20 3d 20 28 63 68 61  tree->zDb = (cha
3a509 72 20 2a 29 26 70 52 74 72 65 65 5b 31 5d 3b 0a  r *)&pRtree[1];.
3a50a 20 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 20    pRtree->zName 
3a50b 3d 20 26 70 52 74 72 65 65 2d 3e 7a 44 62 5b 6e  = &pRtree->zDb[n
3a50c 44 62 2b 31 5d 3b 0a 20 20 70 52 74 72 65 65 2d  Db+1];.  pRtree-
3a50d 3e 6e 44 69 6d 20 3d 20 28 61 72 67 63 2d 34 29  >nDim = (argc-4)
3a50e 2f 32 3b 0a 20 20 70 52 74 72 65 65 2d 3e 6e 42  /2;.  pRtree->nB
3a50f 79 74 65 73 50 65 72 43 65 6c 6c 20 3d 20 38 20  ytesPerCell = 8 
3a510 2b 20 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 34  + pRtree->nDim*4
3a511 2a 32 3b 0a 20 20 70 52 74 72 65 65 2d 3e 65 43  *2;.  pRtree->eC
3a512 6f 6f 72 64 54 79 70 65 20 3d 20 65 43 6f 6f 72  oordType = eCoor
3a513 64 54 79 70 65 3b 0a 20 20 6d 65 6d 63 70 79 28  dType;.  memcpy(
3a514 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 61 72 67  pRtree->zDb, arg
3a515 76 5b 31 5d 2c 20 6e 44 62 29 3b 0a 20 20 6d 65  v[1], nDb);.  me
3a516 6d 63 70 79 28 70 52 74 72 65 65 2d 3e 7a 4e 61  mcpy(pRtree->zNa
3a517 6d 65 2c 20 61 72 67 76 5b 32 5d 2c 20 6e 4e 61  me, argv[2], nNa
3a518 6d 65 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72  me);..  /* Figur
3a519 65 20 6f 75 74 20 74 68 65 20 6e 6f 64 65 20 73  e out the node s
3a51a 69 7a 65 20 74 6f 20 75 73 65 2e 20 42 79 20 64  ize to use. By d
3a51b 65 66 61 75 6c 74 2c 20 75 73 65 20 36 34 20 62  efault, use 64 b
3a51c 79 74 65 73 20 6c 65 73 73 20 74 68 61 6e 0a 20  ytes less than. 
3a51d 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65   ** the database
3a51e 20 70 61 67 65 2d 73 69 7a 65 2e 20 54 68 69 73   page-size. This
3a51f 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 65 61   ensures that ea
3a520 63 68 20 6e 6f 64 65 20 69 73 20 73 74 6f 72 65  ch node is store
3a521 64 20 6f 6e 0a 20 20 2a 2a 20 61 20 73 69 6e 67  d on.  ** a sing
3a522 6c 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  le database page
3a523 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74  ..  **.  ** If t
3a524 68 65 20 64 61 74 61 62 61 73 64 20 70 61 67 65  he databasd page
3a525 2d 73 69 7a 65 20 69 73 20 73 6f 20 6c 61 72 67  -size is so larg
3a526 65 20 74 68 61 74 20 6d 6f 72 65 20 74 68 61 6e  e that more than
3a527 20 52 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53 0a   RTREE_MAXCELLS.
3a528 20 20 2a 2a 20 65 6e 74 72 69 65 73 20 77 6f 75    ** entries wou
3a529 6c 64 20 66 69 74 20 69 6e 20 61 20 73 69 6e 67  ld fit in a sing
3a52a 6c 65 20 6e 6f 64 65 2c 20 75 73 65 20 61 20 73  le node, use a s
3a52b 6d 61 6c 6c 65 72 20 6e 6f 64 65 2d 73 69 7a 65  maller node-size
3a52c 2e 0a 20 20 2a 2f 0a 20 20 70 52 74 72 65 65 2d  ..  */.  pRtree-
3a52d 3e 69 4e 6f 64 65 53 69 7a 65 20 3d 20 69 50 61  >iNodeSize = iPa
3a52e 67 65 53 69 7a 65 2d 36 34 3b 0a 20 20 69 66 28  geSize-64;.  if(
3a52f 20 28 34 2b 70 52 74 72 65 65 2d 3e 6e 42 79 74   (4+pRtree->nByt
3a530 65 73 50 65 72 43 65 6c 6c 2a 52 54 52 45 45 5f  esPerCell*RTREE_
3a531 4d 41 58 43 45 4c 4c 53 29 3c 70 52 74 72 65 65  MAXCELLS)<pRtree
3a532 2d 3e 69 4e 6f 64 65 53 69 7a 65 20 29 7b 0a 20  ->iNodeSize ){. 
3a533 20 20 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65     pRtree->iNode
3a534 53 69 7a 65 20 3d 20 34 2b 70 52 74 72 65 65 2d  Size = 4+pRtree-
3a535 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 52  >nBytesPerCell*R
3a536 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53 3b 0a 20  TREE_MAXCELLS;. 
3a537 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 2f   }..  /* Create/
3a538 43 6f 6e 6e 65 63 74 20 74 6f 20 74 68 65 20 75  Connect to the u
3a539 6e 64 65 72 6c 79 69 6e 67 20 72 65 6c 61 74 69  nderlying relati
3a53a 6f 6e 61 6c 20 64 61 74 61 62 61 73 65 20 73 63  onal database sc
3a53b 68 65 6d 61 2e 20 49 66 0a 20 20 2a 2a 20 74 68  hema. If.  ** th
3a53c 61 74 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  at is successful
3a53d 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 64  , call sqlite3_d
3a53e 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 74 6f  eclare_vtab() to
3a53f 20 63 6f 6e 66 69 67 75 72 65 0a 20 20 2a 2a 20   configure.  ** 
3a540 74 68 65 20 72 2d 74 72 65 65 20 74 61 62 6c 65  the r-tree table
3a541 20 73 63 68 65 6d 61 2e 0a 20 20 2a 2f 0a 20 20   schema..  */.  
3a542 69 66 28 20 28 72 63 20 3d 20 72 74 72 65 65 53  if( (rc = rtreeS
3a543 71 6c 49 6e 69 74 28 70 52 74 72 65 65 2c 20 64  qlInit(pRtree, d
3a544 62 2c 20 61 72 67 76 5b 31 5d 2c 20 61 72 67 76  b, argv[1], argv
3a545 5b 32 5d 2c 20 69 73 43 72 65 61 74 65 29 29 20  [2], isCreate)) 
3a546 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20  ){.    *pzErr = 
3a547 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
3a548 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 65 72  "%s", sqlite3_er
3a549 72 6d 73 67 28 64 62 29 29 3b 0a 20 20 7d 65 6c  rmsg(db));.  }el
3a54a 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53  se{.    char *zS
3a54b 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  ql = sqlite3_mpr
3a54c 69 6e 74 66 28 22 43 52 45 41 54 45 20 54 41 42  intf("CREATE TAB
3a54d 4c 45 20 78 28 25 73 22 2c 20 61 72 67 76 5b 33  LE x(%s", argv[3
3a54e 5d 29 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54  ]);.    char *zT
3a54f 6d 70 3b 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a  mp;.    int ii;.
3a550 20 20 20 20 66 6f 72 28 69 69 3d 34 3b 20 7a 53      for(ii=4; zS
3a551 71 6c 20 26 26 20 69 69 3c 61 72 67 63 3b 20 69  ql && ii<argc; i
3a552 69 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 54 6d 70  i++){.      zTmp
3a553 20 3d 20 7a 53 71 6c 3b 0a 20 20 20 20 20 20 7a   = zSql;.      z
3a554 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Sql = sqlite3_mp
3a555 72 69 6e 74 66 28 22 25 73 2c 20 25 73 22 2c 20  rintf("%s, %s", 
3a556 7a 54 6d 70 2c 20 61 72 67 76 5b 69 69 5d 29 3b  zTmp, argv[ii]);
3a557 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
3a558 72 65 65 28 7a 54 6d 70 29 3b 0a 20 20 20 20 7d  ree(zTmp);.    }
3a559 0a 20 20 20 20 69 66 28 20 7a 53 71 6c 20 29 7b  .    if( zSql ){
3a55a 0a 20 20 20 20 20 20 7a 54 6d 70 20 3d 20 7a 53  .      zTmp = zS
3a55b 71 6c 3b 0a 20 20 20 20 20 20 7a 53 71 6c 20 3d  ql;.      zSql =
3a55c 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
3a55d 28 22 25 73 29 3b 22 2c 20 7a 54 6d 70 29 3b 0a  ("%s);", zTmp);.
3a55e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
3a55f 65 65 28 7a 54 6d 70 29 3b 0a 20 20 20 20 7d 0a  ee(zTmp);.    }.
3a560 20 20 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b      if( !zSql ){
3a561 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
3a562 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65  TE_NOMEM;.    }e
3a563 6c 73 65 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  lse if( SQLITE_O
3a564 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K!=(rc = sqlite3
3a565 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28 64 62  _declare_vtab(db
3a566 2c 20 7a 53 71 6c 29 29 20 29 7b 0a 20 20 20 20  , zSql)) ){.    
3a567 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74    *pzErr = sqlit
3a568 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c  e3_mprintf("%s",
3a569 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28   sqlite3_errmsg(
3a56a 64 62 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  db));.    }.    
3a56b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71  sqlite3_free(zSq
3a56c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72  l);.  }..  if( r
3a56d 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
3a56e 20 20 20 20 2a 70 70 56 74 61 62 20 3d 20 28 73      *ppVtab = (s
3a56f 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 70 52  qlite3_vtab *)pR
3a570 74 72 65 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  tree;.  }else{. 
3a571 20 20 20 72 74 72 65 65 52 65 6c 65 61 73 65 28     rtreeRelease(
3a572 70 52 74 72 65 65 29 3b 0a 20 20 7d 0a 20 20 72  pRtree);.  }.  r
3a573 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
3a574 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
3a575 6f 6e 20 6f 66 20 61 20 73 63 61 6c 61 72 20 66  on of a scalar f
3a576 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 64 65 63  unction that dec
3a577 6f 64 65 73 20 72 2d 74 72 65 65 20 6e 6f 64 65  odes r-tree node
3a578 73 20 74 6f 0a 2a 2a 20 68 75 6d 61 6e 20 72 65  s to.** human re
3a579 61 64 61 62 6c 65 20 73 74 72 69 6e 67 73 2e 20  adable strings. 
3a57a 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64  This can be used
3a57b 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 61   for debugging a
3a57c 6e 64 20 61 6e 61 6c 79 73 69 73 2e 0a 2a 2a 0a  nd analysis..**.
3a57d 2a 2a 20 54 68 65 20 73 63 61 6c 61 72 20 66 75  ** The scalar fu
3a57e 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20 74 77 6f  nction takes two
3a57f 20 61 72 67 75 6d 65 6e 74 73 2c 20 61 20 62 6c   arguments, a bl
3a580 6f 62 20 6f 66 20 64 61 74 61 20 63 6f 6e 74 61  ob of data conta
3a581 69 6e 69 6e 67 0a 2a 2a 20 61 6e 20 72 2d 74 72  ining.** an r-tr
3a582 65 65 20 6e 6f 64 65 2c 20 61 6e 64 20 74 68 65  ee node, and the
3a583 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 6d 65 6e   number of dimen
3a584 73 69 6f 6e 73 20 74 68 65 20 72 2d 74 72 65 65  sions the r-tree
3a585 20 69 6e 64 65 78 65 73 2e 0a 2a 2a 20 46 6f 72   indexes..** For
3a586 20 61 20 74 77 6f 2d 64 69 6d 65 6e 73 69 6f 6e   a two-dimension
3a587 61 6c 20 72 2d 74 72 65 65 20 73 74 72 75 63 74  al r-tree struct
3a588 75 72 65 20 63 61 6c 6c 65 64 20 22 72 74 22 2c  ure called "rt",
3a589 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 0a   to deserialize.
3a58a 2a 2a 20 61 6c 6c 20 6e 6f 64 65 73 2c 20 61 20  ** all nodes, a 
3a58b 73 74 61 74 65 6d 65 6e 74 20 6c 69 6b 65 3a 0a  statement like:.
3a58c 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 72  **.**   SELECT r
3a58d 74 72 65 65 6e 6f 64 65 28 32 2c 20 64 61 74 61  treenode(2, data
3a58e 29 20 46 52 4f 4d 20 72 74 5f 6e 6f 64 65 3b 0a  ) FROM rt_node;.
3a58f 2a 2a 0a 2a 2a 20 54 68 65 20 68 75 6d 61 6e 20  **.** The human 
3a590 72 65 61 64 61 62 6c 65 20 73 74 72 69 6e 67 20  readable string 
3a591 74 61 6b 65 73 20 74 68 65 20 66 6f 72 6d 20 6f  takes the form o
3a592 66 20 61 20 54 63 6c 20 6c 69 73 74 20 77 69 74  f a Tcl list wit
3a593 68 20 6f 6e 65 0a 2a 2a 20 65 6e 74 72 79 20 66  h one.** entry f
3a594 6f 72 20 65 61 63 68 20 63 65 6c 6c 20 69 6e 20  or each cell in 
3a595 74 68 65 20 72 2d 74 72 65 65 20 6e 6f 64 65 2e  the r-tree node.
3a596 20 45 61 63 68 20 65 6e 74 72 79 20 69 73 20 69   Each entry is i
3a597 74 73 65 6c 66 20 61 0a 2a 2a 20 6c 69 73 74 2c  tself a.** list,
3a598 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
3a599 38 2d 62 79 74 65 20 72 6f 77 69 64 2f 70 61 67  8-byte rowid/pag
3a59a 65 6e 6f 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  eno followed by 
3a59b 74 68 65 20 0a 2a 2a 20 3c 6e 75 6d 2d 64 69 6d  the .** <num-dim
3a59c 65 6e 73 69 6f 6e 3e 2a 32 20 63 6f 6f 72 64 69  ension>*2 coordi
3a59d 6e 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  nates..*/.static
3a59e 20 76 6f 69 64 20 72 74 72 65 65 6e 6f 64 65 28   void rtreenode(
3a59f 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
3a5a0 2a 63 74 78 2c 20 69 6e 74 20 6e 41 72 67 2c 20  *ctx, int nArg, 
3a5a1 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
3a5a2 61 70 41 72 67 29 7b 0a 20 20 63 68 61 72 20 2a  apArg){.  char *
3a5a3 7a 54 65 78 74 20 3d 20 30 3b 0a 20 20 52 74 72  zText = 0;.  Rtr
3a5a4 65 65 4e 6f 64 65 20 6e 6f 64 65 3b 0a 20 20 52  eeNode node;.  R
3a5a5 74 72 65 65 20 74 72 65 65 3b 0a 20 20 69 6e 74  tree tree;.  int
3a5a6 20 69 69 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 26   ii;..  memset(&
3a5a7 6e 6f 64 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28  node, 0, sizeof(
3a5a8 52 74 72 65 65 4e 6f 64 65 29 29 3b 0a 20 20 6d  RtreeNode));.  m
3a5a9 65 6d 73 65 74 28 26 74 72 65 65 2c 20 30 2c 20  emset(&tree, 0, 
3a5aa 73 69 7a 65 6f 66 28 52 74 72 65 65 29 29 3b 0a  sizeof(Rtree));.
3a5ab 20 20 74 72 65 65 2e 6e 44 69 6d 20 3d 20 73 71    tree.nDim = sq
3a5ac 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28  lite3_value_int(
3a5ad 61 70 41 72 67 5b 30 5d 29 3b 0a 20 20 74 72 65  apArg[0]);.  tre
3a5ae 65 2e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 20  e.nBytesPerCell 
3a5af 3d 20 38 20 2b 20 38 20 2a 20 74 72 65 65 2e 6e  = 8 + 8 * tree.n
3a5b0 44 69 6d 3b 0a 20 20 6e 6f 64 65 2e 7a 44 61 74  Dim;.  node.zDat
3a5b1 61 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65  a = (u8 *)sqlite
3a5b2 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 70 41  3_value_blob(apA
3a5b3 72 67 5b 31 5d 29 3b 0a 0a 20 20 66 6f 72 28 69  rg[1]);..  for(i
3a5b4 69 3d 30 3b 20 69 69 3c 4e 43 45 4c 4c 28 26 6e  i=0; ii<NCELL(&n
3a5b5 6f 64 65 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  ode); ii++){.   
3a5b6 20 63 68 61 72 20 7a 43 65 6c 6c 5b 35 31 32 5d   char zCell[512]
3a5b7 3b 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20  ;.    int nCell 
3a5b8 3d 20 30 3b 0a 20 20 20 20 52 74 72 65 65 43 65  = 0;.    RtreeCe
3a5b9 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20 20 69 6e 74  ll cell;.    int
3a5ba 20 6a 6a 3b 0a 0a 20 20 20 20 6e 6f 64 65 47 65   jj;..    nodeGe
3a5bb 74 43 65 6c 6c 28 26 74 72 65 65 2c 20 26 6e 6f  tCell(&tree, &no
3a5bc 64 65 2c 20 69 69 2c 20 26 63 65 6c 6c 29 3b 0a  de, ii, &cell);.
3a5bd 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
3a5be 69 6e 74 66 28 35 31 32 2d 6e 43 65 6c 6c 2c 26  intf(512-nCell,&
3a5bf 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c 22 25 64  zCell[nCell],"%d
3a5c0 22 2c 20 63 65 6c 6c 2e 69 52 6f 77 69 64 29 3b  ", cell.iRowid);
3a5c1 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20 73 74 72  .    nCell = str
3a5c2 6c 65 6e 28 7a 43 65 6c 6c 29 3b 0a 20 20 20 20  len(zCell);.    
3a5c3 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 74 72 65  for(jj=0; jj<tre
3a5c4 65 2e 6e 44 69 6d 2a 32 3b 20 6a 6a 2b 2b 29 7b  e.nDim*2; jj++){
3a5c5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
3a5c6 6e 70 72 69 6e 74 66 28 35 31 32 2d 6e 43 65 6c  nprintf(512-nCel
3a5c7 6c 2c 26 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c  l,&zCell[nCell],
3a5c8 22 20 25 66 22 2c 28 64 6f 75 62 6c 65 29 63 65  " %f",(double)ce
3a5c9 6c 6c 2e 61 43 6f 6f 72 64 5b 6a 6a 5d 2e 66 29  ll.aCoord[jj].f)
3a5ca 3b 0a 20 20 20 20 20 20 6e 43 65 6c 6c 20 3d 20  ;.      nCell = 
3a5cb 73 74 72 6c 65 6e 28 7a 43 65 6c 6c 29 3b 0a 20  strlen(zCell);. 
3a5cc 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 7a 54     }..    if( zT
3a5cd 65 78 74 20 29 7b 0a 20 20 20 20 20 20 63 68 61  ext ){.      cha
3a5ce 72 20 2a 7a 54 65 78 74 4e 65 77 20 3d 20 73 71  r *zTextNew = sq
3a5cf 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25  lite3_mprintf("%
3a5d0 73 20 7b 25 73 7d 22 2c 20 7a 54 65 78 74 2c 20  s {%s}", zText, 
3a5d1 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71  zCell);.      sq
3a5d2 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 65 78 74  lite3_free(zText
3a5d3 29 3b 0a 20 20 20 20 20 20 7a 54 65 78 74 20 3d  );.      zText =
3a5d4 20 7a 54 65 78 74 4e 65 77 3b 0a 20 20 20 20 7d   zTextNew;.    }
3a5d5 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 54 65 78  else{.      zTex
3a5d6 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  t = sqlite3_mpri
3a5d7 6e 74 66 28 22 7b 25 73 7d 22 2c 20 7a 43 65 6c  ntf("{%s}", zCel
3a5d8 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  l);.    }.  }.  
3a5d9 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
3a5da 74 5f 74 65 78 74 28 63 74 78 2c 20 7a 54 65 78  t_text(ctx, zTex
3a5db 74 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 5f 66  t, -1, sqlite3_f
3a5dc 72 65 65 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  ree);.}..static 
3a5dd 76 6f 69 64 20 72 74 72 65 65 64 65 70 74 68 28  void rtreedepth(
3a5de 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
3a5df 2a 63 74 78 2c 20 69 6e 74 20 6e 41 72 67 2c 20  *ctx, int nArg, 
3a5e0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
3a5e1 61 70 41 72 67 29 7b 0a 20 20 69 66 28 20 73 71  apArg){.  if( sq
3a5e2 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
3a5e3 28 61 70 41 72 67 5b 30 5d 29 21 3d 53 51 4c 49  (apArg[0])!=SQLI
3a5e4 54 45 5f 42 4c 4f 42 20 0a 20 20 20 7c 7c 20 73  TE_BLOB .   || s
3a5e5 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
3a5e6 65 73 28 61 70 41 72 67 5b 30 5d 29 3c 32 0a 20  es(apArg[0])<2. 
3a5e7 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
3a5e8 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 74 78  result_error(ctx
3a5e9 2c 20 22 49 6e 76 61 6c 69 64 20 61 72 67 75 6d  , "Invalid argum
3a5ea 65 6e 74 20 74 6f 20 72 74 72 65 65 64 65 70 74  ent to rtreedept
3a5eb 68 28 29 22 2c 20 2d 31 29 3b 20 0a 20 20 7d 65  h()", -1); .  }e
3a5ec 6c 73 65 7b 0a 20 20 20 20 75 38 20 2a 7a 42 6c  lse{.    u8 *zBl
3a5ed 6f 62 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74  ob = (u8 *)sqlit
3a5ee 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 70  e3_value_blob(ap
3a5ef 41 72 67 5b 30 5d 29 3b 0a 20 20 20 20 73 71 6c  Arg[0]);.    sql
3a5f0 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28  ite3_result_int(
3a5f1 63 74 78 2c 20 72 65 61 64 49 6e 74 31 36 28 7a  ctx, readInt16(z
3a5f2 42 6c 6f 62 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  Blob));.  }.}../
3a5f3 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 74 68  *.** Register th
3a5f4 65 20 72 2d 74 72 65 65 20 6d 6f 64 75 6c 65 20  e r-tree module 
3a5f5 77 69 74 68 20 64 61 74 61 62 61 73 65 20 68 61  with database ha
3a5f6 6e 64 6c 65 20 64 62 2e 20 54 68 69 73 20 63 72  ndle db. This cr
3a5f7 65 61 74 65 73 20 74 68 65 0a 2a 2a 20 76 69 72  eates the.** vir
3a5f8 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
3a5f9 65 20 22 72 74 72 65 65 22 20 61 6e 64 20 74 68  e "rtree" and th
3a5fa 65 20 64 65 62 75 67 67 69 6e 67 2f 61 6e 61 6c  e debugging/anal
3a5fb 79 73 69 73 20 73 63 61 6c 61 72 20 0a 2a 2a 20  ysis scalar .** 
3a5fc 66 75 6e 63 74 69 6f 6e 20 22 72 74 72 65 65 6e  function "rtreen
3a5fd 6f 64 65 22 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ode"..*/.SQLITE_
3a5fe 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
3a5ff 74 65 33 52 74 72 65 65 49 6e 69 74 28 73 71 6c  te3RtreeInit(sql
3a600 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74  ite3 *db){.  int
3a601 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
3a602 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
3a603 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74  TE_OK ){.    int
3a604 20 75 74 66 38 20 3d 20 53 51 4c 49 54 45 5f 55   utf8 = SQLITE_U
3a605 54 46 38 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  TF8;.    rc = sq
3a606 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
3a607 63 74 69 6f 6e 28 64 62 2c 20 22 72 74 72 65 65  ction(db, "rtree
3a608 6e 6f 64 65 22 2c 20 32 2c 20 75 74 66 38 2c 20  node", 2, utf8, 
3a609 30 2c 20 72 74 72 65 65 6e 6f 64 65 2c 20 30 2c  0, rtreenode, 0,
3a60a 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72   0);.  }.  if( r
3a60b 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
3a60c 20 20 20 20 69 6e 74 20 75 74 66 38 20 3d 20 53      int utf8 = S
3a60d 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20  QLITE_UTF8;.    
3a60e 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65  rc = sqlite3_cre
3a60f 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c  ate_function(db,
3a610 20 22 72 74 72 65 65 64 65 70 74 68 22 2c 20 31   "rtreedepth", 1
3a611 2c 20 75 74 66 38 2c 20 30 2c 72 74 72 65 65 64  , utf8, 0,rtreed
3a612 65 70 74 68 2c 20 30 2c 20 30 29 3b 0a 20 20 7d  epth, 0, 0);.  }
3a613 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
3a614 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 76 6f 69 64  E_OK ){.    void
3a615 20 2a 63 20 3d 20 28 76 6f 69 64 20 2a 29 52 54   *c = (void *)RT
3a616 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32  REE_COORD_REAL32
3a617 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
3a618 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
3a619 5f 76 32 28 64 62 2c 20 22 72 74 72 65 65 22 2c  _v2(db, "rtree",
3a61a 20 26 72 74 72 65 65 4d 6f 64 75 6c 65 2c 20 63   &rtreeModule, c
3a61b 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  , 0);.  }.  if( 
3a61c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
3a61d 0a 20 20 20 20 76 6f 69 64 20 2a 63 20 3d 20 28  .    void *c = (
3a61e 76 6f 69 64 20 2a 29 52 54 52 45 45 5f 43 4f 4f  void *)RTREE_COO
3a61f 52 44 5f 49 4e 54 33 32 3b 0a 20 20 20 20 72 63  RD_INT32;.    rc
3a620 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74   = sqlite3_creat
3a621 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 64 62 2c 20  e_module_v2(db, 
3a622 22 72 74 72 65 65 5f 69 33 32 22 2c 20 26 72 74  "rtree_i32", &rt
3a623 72 65 65 4d 6f 64 75 6c 65 2c 20 63 2c 20 30 29  reeModule, c, 0)
3a624 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
3a625 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 53 51 4c 49  rc;.}..#if !SQLI
3a626 54 45 5f 43 4f 52 45 0a 53 51 4c 49 54 45 5f 41  TE_CORE.SQLITE_A
3a627 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  PI int sqlite3_e
3a628 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 28 0a 20  xtension_init(. 
3a629 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20   sqlite3 *db,.  
3a62a 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c  char **pzErrMsg,
3a62b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
3a62c 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 2a 70  _api_routines *p
3a62d 41 70 69 0a 29 7b 0a 20 20 53 51 4c 49 54 45 5f  Api.){.  SQLITE_
3a62e 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 32 28  EXTENSION_INIT2(
3a62f 70 41 70 69 29 0a 20 20 72 65 74 75 72 6e 20 73  pApi).  return s
3a630 71 6c 69 74 65 33 52 74 72 65 65 49 6e 69 74 28  qlite3RtreeInit(
3a631 64 62 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  db);.}.#endif..#
3a632 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  endif../********
3a633 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 74  ****** End of rt
3a634 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ree.c **********
3a635 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a636 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a637 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
3a638 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
3a639 65 20 69 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  e icu.c ********
3a63a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a63b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a63c 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
3a63d 37 20 4d 61 79 20 36 0a 2a 2a 0a 2a 2a 20 54 68  7 May 6.**.** Th
3a63e 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
3a63f 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
3a640 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
3a641 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
3a642 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
3a643 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
3a644 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
3a645 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
3a646 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
3a647 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
3a648 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
3a649 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
3a64a 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
3a64b 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
3a64c 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
3a64d 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
3a64e 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
3a64f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a651 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a652 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a653 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 69  ******.** $Id: i
3a654 63 75 2e 63 2c 76 20 31 2e 37 20 32 30 30 37 2f  cu.c,v 1.7 2007/
3a655 31 32 2f 31 33 20 32 31 3a 35 34 3a 31 31 20 64  12/13 21:54:11 d
3a656 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54  rh Exp $.**.** T
3a657 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65  his file impleme
3a658 6e 74 73 20 61 6e 20 69 6e 74 65 67 72 61 74 69  nts an integrati
3a659 6f 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 49  on between the I
3a65a 43 55 20 6c 69 62 72 61 72 79 20 0a 2a 2a 20 28  CU library .** (
3a65b 22 49 6e 74 65 72 6e 61 74 69 6f 6e 61 6c 20 43  "International C
3a65c 6f 6d 70 6f 6e 65 6e 74 73 20 66 6f 72 20 55 6e  omponents for Un
3a65d 69 63 6f 64 65 22 2c 20 61 6e 20 6f 70 65 6e 2d  icode", an open-
3a65e 73 6f 75 72 63 65 20 6c 69 62 72 61 72 79 20 0a  source library .
3a65f 2a 2a 20 66 6f 72 20 68 61 6e 64 6c 69 6e 67 20  ** for handling 
3a660 75 6e 69 63 6f 64 65 20 64 61 74 61 29 20 61 6e  unicode data) an
3a661 64 20 53 51 4c 69 74 65 2e 20 54 68 65 20 69 6e  d SQLite. The in
3a662 74 65 67 72 61 74 69 6f 6e 20 75 73 65 73 20 0a  tegration uses .
3a663 2a 2a 20 49 43 55 20 74 6f 20 70 72 6f 76 69 64  ** ICU to provid
3a664 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  e the following 
3a665 74 6f 20 53 51 4c 69 74 65 3a 0a 2a 2a 0a 2a 2a  to SQLite:.**.**
3a666 20 20 20 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e     * An implemen
3a667 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 53 51  tation of the SQ
3a668 4c 20 72 65 67 65 78 70 28 29 20 66 75 6e 63 74  L regexp() funct
3a669 69 6f 6e 20 28 61 6e 64 20 68 65 6e 63 65 20 52  ion (and hence R
3a66a 45 47 45 58 50 0a 2a 2a 20 20 20 20 20 6f 70 65  EGEXP.**     ope
3a66b 72 61 74 6f 72 29 20 75 73 69 6e 67 20 74 68 65  rator) using the
3a66c 20 49 43 55 20 75 72 65 67 65 78 5f 58 58 28 29   ICU uregex_XX()
3a66d 20 41 50 49 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a   APIs..**.**   *
3a66e 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   Implementations
3a66f 20 6f 66 20 74 68 65 20 53 51 4c 20 73 63 61 6c   of the SQL scal
3a670 61 72 20 75 70 70 65 72 28 29 20 61 6e 64 20 6c  ar upper() and l
3a671 6f 77 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 73  ower() functions
3a672 0a 2a 2a 20 20 20 20 20 66 6f 72 20 63 61 73 65  .**     for case
3a673 20 6d 61 70 70 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20   mapping..**.** 
3a674 20 20 2a 20 49 6e 74 65 67 72 61 74 69 6f 6e 20    * Integration 
3a675 6f 66 20 49 43 55 20 61 6e 64 20 53 51 4c 69 74  of ICU and SQLit
3a676 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 65  e collation seqe
3a677 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  nces..**.**   * 
3a678 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  An implementatio
3a679 6e 20 6f 66 20 74 68 65 20 4c 49 4b 45 20 6f 70  n of the LIKE op
3a67a 65 72 61 74 6f 72 20 74 68 61 74 20 75 73 65 73  erator that uses
3a67b 20 49 43 55 20 74 6f 20 0a 2a 2a 20 20 20 20 20   ICU to .**     
3a67c 70 72 6f 76 69 64 65 20 63 61 73 65 2d 69 6e 64  provide case-ind
3a67d 65 70 65 6e 64 65 6e 74 20 6d 61 74 63 68 69 6e  ependent matchin
3a67e 67 2e 0a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69  g..*/..#if !defi
3a67f 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29  ned(SQLITE_CORE)
3a680 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
3a681 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55 29 0a 0a  TE_ENABLE_ICU)..
3a682 2f 2a 20 49 6e 63 6c 75 64 65 20 49 43 55 20 68  /* Include ICU h
3a683 65 61 64 65 72 73 20 2a 2f 0a 23 69 6e 63 6c 75  eaders */.#inclu
3a684 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75 74 79 70  de <unicode/utyp
3a685 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  es.h>.#include <
3a686 75 6e 69 63 6f 64 65 2f 75 72 65 67 65 78 2e 68  unicode/uregex.h
3a687 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63  >.#include <unic
3a688 6f 64 65 2f 75 73 74 72 69 6e 67 2e 68 3e 0a 23  ode/ustring.h>.#
3a689 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65  include <unicode
3a68a 2f 75 63 6f 6c 2e 68 3e 0a 0a 0a 23 69 66 6e 64  /ucol.h>...#ifnd
3a68b 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a 20  ef SQLITE_CORE. 
3a68c 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f   SQLITE_EXTENSIO
3a68d 4e 5f 49 4e 49 54 31 0a 23 65 6c 73 65 0a 23 65  N_INIT1.#else.#e
3a68e 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69  ndif../*.** Maxi
3a68f 6d 75 6d 20 6c 65 6e 67 74 68 20 28 69 6e 20 62  mum length (in b
3a690 79 74 65 73 29 20 6f 66 20 74 68 65 20 70 61 74  ytes) of the pat
3a691 74 65 72 6e 20 69 6e 20 61 20 4c 49 4b 45 20 6f  tern in a LIKE o
3a692 72 20 47 4c 4f 42 0a 2a 2a 20 6f 70 65 72 61 74  r GLOB.** operat
3a693 6f 72 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  or..*/.#ifndef S
3a694 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50  QLITE_MAX_LIKE_P
3a695 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 0a 23 20  ATTERN_LENGTH.# 
3a696 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
3a697 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c  X_LIKE_PATTERN_L
3a698 45 4e 47 54 48 20 35 30 30 30 30 0a 23 65 6e 64  ENGTH 50000.#end
3a699 69 66 0a 0a 2f 2a 0a 2a 2a 20 56 65 72 73 69 6f  if../*.** Versio
3a69a 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 72 65  n of sqlite3_fre
3a69b 65 28 29 20 74 68 61 74 20 69 73 20 61 6c 77 61  e() that is alwa
3a69c 79 73 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 6e  ys a function, n
3a69d 65 76 65 72 20 61 20 6d 61 63 72 6f 2e 0a 2a 2f  ever a macro..*/
3a69e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 78 46 72  .static void xFr
3a69f 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 73  ee(void *p){.  s
3a6a0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
3a6a1 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65  }../*.** Compare
3a6a2 20 74 77 6f 20 55 54 46 2d 38 20 73 74 72 69 6e   two UTF-8 strin
3a6a3 67 73 20 66 6f 72 20 65 71 75 61 6c 69 74 79 20  gs for equality 
3a6a4 77 68 65 72 65 20 74 68 65 20 66 69 72 73 74 20  where the first 
3a6a5 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20 61 20 22  string is.** a "
3a6a6 4c 49 4b 45 22 20 65 78 70 72 65 73 73 69 6f 6e  LIKE" expression
3a6a7 2e 20 52 65 74 75 72 6e 20 74 72 75 65 20 28 31  . Return true (1
3a6a8 29 20 69 66 20 74 68 65 79 20 61 72 65 20 74 68  ) if they are th
3a6a9 65 20 73 61 6d 65 20 61 6e 64 20 0a 2a 2a 20 66  e same and .** f
3a6aa 61 6c 73 65 20 28 30 29 20 69 66 20 74 68 65 79  alse (0) if they
3a6ab 20 61 72 65 20 64 69 66 66 65 72 65 6e 74 2e 0a   are different..
3a6ac 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 63  */.static int ic
3a6ad 75 4c 69 6b 65 43 6f 6d 70 61 72 65 28 0a 20 20  uLikeCompare(.  
3a6ae 63 6f 6e 73 74 20 75 69 6e 74 38 5f 74 20 2a 7a  const uint8_t *z
3a6af 50 61 74 74 65 72 6e 2c 20 20 20 2f 2a 20 4c 49  Pattern,   /* LI
3a6b0 4b 45 20 70 61 74 74 65 72 6e 20 2a 2f 0a 20 20  KE pattern */.  
3a6b1 63 6f 6e 73 74 20 75 69 6e 74 38 5f 74 20 2a 7a  const uint8_t *z
3a6b2 53 74 72 69 6e 67 2c 20 20 20 20 2f 2a 20 54 68  String,    /* Th
3a6b3 65 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74  e UTF-8 string t
3a6b4 6f 20 63 6f 6d 70 61 72 65 20 61 67 61 69 6e 73  o compare agains
3a6b5 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 55 43 68  t */.  const UCh
3a6b6 61 72 33 32 20 75 45 73 63 20 20 20 20 20 20 20  ar32 uEsc       
3a6b7 20 20 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20    /* The escape 
3a6b8 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 29 7b 0a  character */.){.
3a6b9 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69    static const i
3a6ba 6e 74 20 4d 41 54 43 48 5f 4f 4e 45 20 3d 20 28  nt MATCH_ONE = (
3a6bb 55 43 68 61 72 33 32 29 27 5f 27 3b 0a 20 20 73  UChar32)'_';.  s
3a6bc 74 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20  tatic const int 
3a6bd 4d 41 54 43 48 5f 41 4c 4c 20 3d 20 28 55 43 68  MATCH_ALL = (UCh
3a6be 61 72 33 32 29 27 25 27 3b 0a 0a 20 20 69 6e 74  ar32)'%';..  int
3a6bf 20 69 50 61 74 74 65 72 6e 20 3d 20 30 3b 20 20   iPattern = 0;  
3a6c0 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
3a6c1 62 79 74 65 20 69 6e 64 65 78 20 69 6e 20 7a 50  byte index in zP
3a6c2 61 74 74 65 72 6e 20 2a 2f 0a 20 20 69 6e 74 20  attern */.  int 
3a6c3 69 53 74 72 69 6e 67 20 3d 20 30 3b 20 20 20 20  iString = 0;    
3a6c4 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 62      /* Current b
3a6c5 79 74 65 20 69 6e 64 65 78 20 69 6e 20 7a 53 74  yte index in zSt
3a6c6 72 69 6e 67 20 2a 2f 0a 0a 20 20 69 6e 74 20 70  ring */..  int p
3a6c7 72 65 76 45 73 63 61 70 65 20 3d 20 30 3b 20 20  revEscape = 0;  
3a6c8 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
3a6c9 65 20 70 72 65 76 69 6f 75 73 20 63 68 61 72 61  e previous chara
3a6ca 63 74 65 72 20 77 61 73 20 75 45 73 63 20 2a 2f  cter was uEsc */
3a6cb 0a 0a 20 20 77 68 69 6c 65 28 20 7a 50 61 74 74  ..  while( zPatt
3a6cc 65 72 6e 5b 69 50 61 74 74 65 72 6e 5d 21 3d 30  ern[iPattern]!=0
3a6cd 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64   ){..    /* Read
3a6ce 20 28 61 6e 64 20 63 6f 6e 73 75 6d 65 29 20 74   (and consume) t
3a6cf 68 65 20 6e 65 78 74 20 63 68 61 72 61 63 74 65  he next characte
3a6d0 72 20 66 72 6f 6d 20 74 68 65 20 69 6e 70 75 74  r from the input
3a6d1 20 70 61 74 74 65 72 6e 2e 20 2a 2f 0a 20 20 20   pattern. */.   
3a6d2 20 55 43 68 61 72 33 32 20 75 50 61 74 74 65 72   UChar32 uPatter
3a6d3 6e 3b 0a 20 20 20 20 55 38 5f 4e 45 58 54 5f 55  n;.    U8_NEXT_U
3a6d4 4e 53 41 46 45 28 7a 50 61 74 74 65 72 6e 2c 20  NSAFE(zPattern, 
3a6d5 69 50 61 74 74 65 72 6e 2c 20 75 50 61 74 74 65  iPattern, uPatte
3a6d6 72 6e 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  rn);.    assert(
3a6d7 75 50 61 74 74 65 72 6e 21 3d 30 29 3b 0a 0a 20  uPattern!=0);.. 
3a6d8 20 20 20 2f 2a 20 54 68 65 72 65 20 61 72 65 20     /* There are 
3a6d9 6e 6f 77 20 34 20 70 6f 73 73 69 62 69 6c 69 74  now 4 possibilit
3a6da 69 65 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ies:.    **.    
3a6db 2a 2a 20 20 20 20 20 31 2e 20 75 50 61 74 74 65  **     1. uPatte
3a6dc 72 6e 20 69 73 20 61 6e 20 75 6e 65 73 63 61 70  rn is an unescap
3a6dd 65 64 20 6d 61 74 63 68 2d 61 6c 6c 20 63 68 61  ed match-all cha
3a6de 72 61 63 74 65 72 20 22 25 22 2c 0a 20 20 20 20  racter "%",.    
3a6df 2a 2a 20 20 20 20 20 32 2e 20 75 50 61 74 74 65  **     2. uPatte
3a6e0 72 6e 20 69 73 20 61 6e 20 75 6e 65 73 63 61 70  rn is an unescap
3a6e1 65 64 20 6d 61 74 63 68 2d 6f 6e 65 20 63 68 61  ed match-one cha
3a6e2 72 61 63 74 65 72 20 22 5f 22 2c 0a 20 20 20 20  racter "_",.    
3a6e3 2a 2a 20 20 20 20 20 33 2e 20 75 50 61 74 74 65  **     3. uPatte
3a6e4 72 6e 20 69 73 20 61 6e 20 75 6e 65 73 63 61 70  rn is an unescap
3a6e5 65 64 20 65 73 63 61 70 65 20 63 68 61 72 61 63  ed escape charac
3a6e6 74 65 72 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 20  ter, or.    **  
3a6e7 20 20 20 34 2e 20 75 50 61 74 74 65 72 6e 20 69     4. uPattern i
3a6e8 73 20 74 6f 20 62 65 20 68 61 6e 64 6c 65 64 20  s to be handled 
3a6e9 61 73 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 63  as an ordinary c
3a6ea 68 61 72 61 63 74 65 72 0a 20 20 20 20 2a 2f 0a  haracter.    */.
3a6eb 20 20 20 20 69 66 28 20 21 70 72 65 76 45 73 63      if( !prevEsc
3a6ec 61 70 65 20 26 26 20 75 50 61 74 74 65 72 6e 3d  ape && uPattern=
3a6ed 3d 4d 41 54 43 48 5f 41 4c 4c 20 29 7b 0a 20 20  =MATCH_ALL ){.  
3a6ee 20 20 20 20 2f 2a 20 43 61 73 65 20 31 2e 20 2a      /* Case 1. *
3a6ef 2f 0a 20 20 20 20 20 20 75 69 6e 74 38 5f 74 20  /.      uint8_t 
3a6f0 63 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 53 6b 69  c;..      /* Ski
3a6f1 70 20 61 6e 79 20 4d 41 54 43 48 5f 41 4c 4c 20  p any MATCH_ALL 
3a6f2 6f 72 20 4d 41 54 43 48 5f 4f 4e 45 20 63 68 61  or MATCH_ONE cha
3a6f3 72 61 63 74 65 72 73 20 74 68 61 74 20 66 6f 6c  racters that fol
3a6f4 6c 6f 77 20 61 0a 20 20 20 20 20 20 2a 2a 20 4d  low a.      ** M
3a6f5 41 54 43 48 5f 41 4c 4c 2e 20 46 6f 72 20 65 61  ATCH_ALL. For ea
3a6f6 63 68 20 4d 41 54 43 48 5f 4f 4e 45 2c 20 73 6b  ch MATCH_ONE, sk
3a6f7 69 70 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72  ip one character
3a6f8 20 69 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a   in the .      *
3a6f9 2a 20 74 65 73 74 20 73 74 72 69 6e 67 2e 0a 20  * test string.. 
3a6fa 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 77 68       */.      wh
3a6fb 69 6c 65 28 20 28 63 3d 7a 50 61 74 74 65 72 6e  ile( (c=zPattern
3a6fc 5b 69 50 61 74 74 65 72 6e 5d 29 20 3d 3d 20 4d  [iPattern]) == M
3a6fd 41 54 43 48 5f 41 4c 4c 20 7c 7c 20 63 20 3d 3d  ATCH_ALL || c ==
3a6fe 20 4d 41 54 43 48 5f 4f 4e 45 20 29 7b 0a 20 20   MATCH_ONE ){.  
3a6ff 20 20 20 20 20 20 69 66 28 20 63 3d 3d 4d 41 54        if( c==MAT
3a700 43 48 5f 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20  CH_ONE ){.      
3a701 20 20 20 20 69 66 28 20 7a 53 74 72 69 6e 67 5b      if( zString[
3a702 69 53 74 72 69 6e 67 5d 3d 3d 30 20 29 20 72 65  iString]==0 ) re
3a703 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20  turn 0;.        
3a704 20 20 55 38 5f 46 57 44 5f 31 5f 55 4e 53 41 46    U8_FWD_1_UNSAF
3a705 45 28 7a 53 74 72 69 6e 67 2c 20 69 53 74 72 69  E(zString, iStri
3a706 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ng);.        }. 
3a707 20 20 20 20 20 20 20 69 50 61 74 74 65 72 6e 2b         iPattern+
3a708 2b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  +;.      }..    
3a709 20 20 69 66 28 20 7a 50 61 74 74 65 72 6e 5b 69    if( zPattern[i
3a70a 50 61 74 74 65 72 6e 5d 3d 3d 30 20 29 20 72 65  Pattern]==0 ) re
3a70b 74 75 72 6e 20 31 3b 0a 0a 20 20 20 20 20 20 77  turn 1;..      w
3a70c 68 69 6c 65 28 20 7a 53 74 72 69 6e 67 5b 69 53  hile( zString[iS
3a70d 74 72 69 6e 67 5d 20 29 7b 0a 20 20 20 20 20 20  tring] ){.      
3a70e 20 20 69 66 28 20 69 63 75 4c 69 6b 65 43 6f 6d    if( icuLikeCom
3a70f 70 61 72 65 28 26 7a 50 61 74 74 65 72 6e 5b 69  pare(&zPattern[i
3a710 50 61 74 74 65 72 6e 5d 2c 20 26 7a 53 74 72 69  Pattern], &zStri
3a711 6e 67 5b 69 53 74 72 69 6e 67 5d 2c 20 75 45 73  ng[iString], uEs
3a712 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  c) ){.          
3a713 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
3a714 20 20 7d 0a 20 20 20 20 20 20 20 20 55 38 5f 46    }.        U8_F
3a715 57 44 5f 31 5f 55 4e 53 41 46 45 28 7a 53 74 72  WD_1_UNSAFE(zStr
3a716 69 6e 67 2c 20 69 53 74 72 69 6e 67 29 3b 0a 20  ing, iString);. 
3a717 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74       }.      ret
3a718 75 72 6e 20 30 3b 0a 0a 20 20 20 20 7d 65 6c 73  urn 0;..    }els
3a719 65 20 69 66 28 20 21 70 72 65 76 45 73 63 61 70  e if( !prevEscap
3a71a 65 20 26 26 20 75 50 61 74 74 65 72 6e 3d 3d 4d  e && uPattern==M
3a71b 41 54 43 48 5f 4f 4e 45 20 29 7b 0a 20 20 20 20  ATCH_ONE ){.    
3a71c 20 20 2f 2a 20 43 61 73 65 20 32 2e 20 2a 2f 0a    /* Case 2. */.
3a71d 20 20 20 20 20 20 69 66 28 20 7a 53 74 72 69 6e        if( zStrin
3a71e 67 5b 69 53 74 72 69 6e 67 5d 3d 3d 30 20 29 20  g[iString]==0 ) 
3a71f 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
3a720 55 38 5f 46 57 44 5f 31 5f 55 4e 53 41 46 45 28  U8_FWD_1_UNSAFE(
3a721 7a 53 74 72 69 6e 67 2c 20 69 53 74 72 69 6e 67  zString, iString
3a722 29 3b 0a 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  );..    }else if
3a723 28 20 21 70 72 65 76 45 73 63 61 70 65 20 26 26  ( !prevEscape &&
3a724 20 75 50 61 74 74 65 72 6e 3d 3d 75 45 73 63 29   uPattern==uEsc)
3a725 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20  {.      /* Case 
3a726 33 2e 20 2a 2f 0a 20 20 20 20 20 20 70 72 65 76  3. */.      prev
3a727 45 73 63 61 70 65 20 3d 20 31 3b 0a 0a 20 20 20  Escape = 1;..   
3a728 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a   }else{.      /*
3a729 20 43 61 73 65 20 34 2e 20 2a 2f 0a 20 20 20 20   Case 4. */.    
3a72a 20 20 55 43 68 61 72 33 32 20 75 53 74 72 69 6e    UChar32 uStrin
3a72b 67 3b 0a 20 20 20 20 20 20 55 38 5f 4e 45 58 54  g;.      U8_NEXT
3a72c 5f 55 4e 53 41 46 45 28 7a 53 74 72 69 6e 67 2c  _UNSAFE(zString,
3a72d 20 69 53 74 72 69 6e 67 2c 20 75 53 74 72 69 6e   iString, uStrin
3a72e 67 29 3b 0a 20 20 20 20 20 20 75 53 74 72 69 6e  g);.      uStrin
3a72f 67 20 3d 20 75 5f 66 6f 6c 64 43 61 73 65 28 75  g = u_foldCase(u
3a730 53 74 72 69 6e 67 2c 20 55 5f 46 4f 4c 44 5f 43  String, U_FOLD_C
3a731 41 53 45 5f 44 45 46 41 55 4c 54 29 3b 0a 20 20  ASE_DEFAULT);.  
3a732 20 20 20 20 75 50 61 74 74 65 72 6e 20 3d 20 75      uPattern = u
3a733 5f 66 6f 6c 64 43 61 73 65 28 75 50 61 74 74 65  _foldCase(uPatte
3a734 72 6e 2c 20 55 5f 46 4f 4c 44 5f 43 41 53 45 5f  rn, U_FOLD_CASE_
3a735 44 45 46 41 55 4c 54 29 3b 0a 20 20 20 20 20 20  DEFAULT);.      
3a736 69 66 28 20 75 53 74 72 69 6e 67 21 3d 75 50 61  if( uString!=uPa
3a737 74 74 65 72 6e 20 29 7b 0a 20 20 20 20 20 20 20  ttern ){.       
3a738 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
3a739 20 7d 0a 20 20 20 20 20 20 70 72 65 76 45 73 63   }.      prevEsc
3a73a 61 70 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  ape = 0;.    }. 
3a73b 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 7a 53 74   }..  return zSt
3a73c 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d 3d 3d 30  ring[iString]==0
3a73d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
3a73e 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
3a73f 20 6c 69 6b 65 28 29 20 53 51 4c 20 66 75 6e 63   like() SQL func
3a740 74 69 6f 6e 2e 20 20 54 68 69 73 20 66 75 6e 63  tion.  This func
3a741 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 0a  tion implements.
3a742 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d 69 6e 20  ** the build-in 
3a743 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 2e 20 20  LIKE operator.  
3a744 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
3a745 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69  nt to the functi
3a746 6f 6e 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 74  on is the.** pat
3a747 74 65 72 6e 20 61 6e 64 20 74 68 65 20 73 65 63  tern and the sec
3a748 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ond argument is 
3a749 74 68 65 20 73 74 72 69 6e 67 2e 20 20 53 6f 2c  the string.  So,
3a74a 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
3a74b 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  nts:.**.**      
3a74c 20 41 20 4c 49 4b 45 20 42 0a 2a 2a 0a 2a 2a 20   A LIKE B.**.** 
3a74d 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61  is implemented a
3a74e 73 20 6c 69 6b 65 28 42 2c 20 41 29 2e 20 49 66  s like(B, A). If
3a74f 20 74 68 65 72 65 20 69 73 20 61 6e 20 65 73 63   there is an esc
3a750 61 70 65 20 63 68 61 72 61 63 74 65 72 20 45 2c  ape character E,
3a751 20 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 41 20   .**.**       A 
3a752 4c 49 4b 45 20 42 20 45 53 43 41 50 45 20 45 0a  LIKE B ESCAPE E.
3a753 2a 2a 0a 2a 2a 20 69 73 20 6d 61 70 70 65 64 20  **.** is mapped 
3a754 74 6f 20 6c 69 6b 65 28 42 2c 20 41 2c 20 45 29  to like(B, A, E)
3a755 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
3a756 20 69 63 75 4c 69 6b 65 46 75 6e 63 28 0a 20 20   icuLikeFunc(.  
3a757 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
3a758 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74  *context, .  int
3a759 20 61 72 67 63 2c 20 0a 20 20 73 71 6c 69 74 65   argc, .  sqlite
3a75a 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
3a75b 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  {.  const unsign
3a75c 65 64 20 63 68 61 72 20 2a 7a 41 20 3d 20 73 71  ed char *zA = sq
3a75d 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
3a75e 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 6f 6e  (argv[0]);.  con
3a75f 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
3a760 20 2a 7a 42 20 3d 20 73 71 6c 69 74 65 33 5f 76   *zB = sqlite3_v
3a761 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31  alue_text(argv[1
3a762 5d 29 3b 0a 20 20 55 43 68 61 72 33 32 20 75 45  ]);.  UChar32 uE
3a763 73 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 4c 69  sc = 0;..  /* Li
3a764 6d 69 74 20 74 68 65 20 6c 65 6e 67 74 68 20 6f  mit the length o
3a765 66 20 74 68 65 20 4c 49 4b 45 20 6f 72 20 47 4c  f the LIKE or GL
3a766 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 20 61 76  OB pattern to av
3a767 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a  oid problems.  *
3a768 2a 20 6f 66 20 64 65 65 70 20 72 65 63 75 72 73  * of deep recurs
3a769 69 6f 6e 20 61 6e 64 20 4e 2a 4e 20 62 65 68 61  ion and N*N beha
3a76a 76 69 6f 72 20 69 6e 20 70 61 74 74 65 72 6e 43  vior in patternC
3a76b 6f 6d 70 61 72 65 28 29 2e 0a 20 20 2a 2f 0a 20  ompare()..  */. 
3a76c 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
3a76d 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
3a76e 29 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b  )>SQLITE_MAX_LIK
3a76f 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48  E_PATTERN_LENGTH
3a770 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
3a771 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e  result_error(con
3a772 74 65 78 74 2c 20 22 4c 49 4b 45 20 6f 72 20 47  text, "LIKE or G
3a773 4c 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 6f 20  LOB pattern too 
3a774 63 6f 6d 70 6c 65 78 22 2c 20 2d 31 29 3b 0a 20  complex", -1);. 
3a775 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
3a776 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 33 20 29  .  if( argc==3 )
3a777 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65 73 63  {.    /* The esc
3a778 61 70 65 20 63 68 61 72 61 63 74 65 72 20 73 74  ape character st
3a779 72 69 6e 67 20 6d 75 73 74 20 63 6f 6e 73 69 73  ring must consis
3a77a 74 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 55 54  t of a single UT
3a77b 46 2d 38 20 63 68 61 72 61 63 74 65 72 2e 0a 20  F-8 character.. 
3a77c 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c     ** Otherwise,
3a77d 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   return an error
3a77e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  ..    */.    int
3a77f 20 6e 45 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   nE= sqlite3_val
3a780 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 32 5d  ue_bytes(argv[2]
3a781 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73  );.    const uns
3a782 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 45 20 3d  igned char *zE =
3a783 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
3a784 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20  ext(argv[2]);.  
3a785 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 20    int i = 0;.   
3a786 20 69 66 28 20 7a 45 3d 3d 30 20 29 20 72 65 74   if( zE==0 ) ret
3a787 75 72 6e 3b 0a 20 20 20 20 55 38 5f 4e 45 58 54  urn;.    U8_NEXT
3a788 28 7a 45 2c 20 69 2c 20 6e 45 2c 20 75 45 73 63  (zE, i, nE, uEsc
3a789 29 3b 0a 20 20 20 20 69 66 28 20 69 21 3d 6e 45  );.    if( i!=nE
3a78a 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
3a78b 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
3a78c 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20 20  ntext, .        
3a78d 20 20 22 45 53 43 41 50 45 20 65 78 70 72 65 73    "ESCAPE expres
3a78e 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 61 20 73  sion must be a s
3a78f 69 6e 67 6c 65 20 63 68 61 72 61 63 74 65 72 22  ingle character"
3a790 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 72 65 74  , -1);.      ret
3a791 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  urn;.    }.  }..
3a792 20 20 69 66 28 20 7a 41 20 26 26 20 7a 42 20 29    if( zA && zB )
3a793 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
3a794 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74  sult_int(context
3a795 2c 20 69 63 75 4c 69 6b 65 43 6f 6d 70 61 72 65  , icuLikeCompare
3a796 28 7a 41 2c 20 7a 42 2c 20 75 45 73 63 29 29 3b  (zA, zB, uEsc));
3a797 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
3a798 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
3a799 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 49 43  alled when an IC
3a79a 55 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 65  U function calle
3a79b 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 0a 2a 2a  d from within.**
3a79c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
3a79d 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 73 63  ion of an SQL sc
3a79e 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 72 65  alar function re
3a79f 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 2e 0a  turns an error..
3a7a0 2a 2a 0a 2a 2a 20 54 68 65 20 73 63 61 6c 61 72  **.** The scalar
3a7a1 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78   function contex
3a7a2 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  t passed as the 
3a7a3 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69  first argument i
3a7a4 73 20 0a 2a 2a 20 6c 6f 61 64 65 64 20 77 69 74  s .** loaded wit
3a7a5 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  h an error messa
3a7a6 67 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20  ge based on the 
3a7a7 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 61 72  following two ar
3a7a8 67 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  gs..*/.static vo
3a7a9 69 64 20 69 63 75 46 75 6e 63 74 69 6f 6e 45 72  id icuFunctionEr
3a7aa 72 6f 72 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  ror(.  sqlite3_c
3a7ab 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 20 20  ontext *pCtx,   
3a7ac 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 73 63      /* SQLite sc
3a7ad 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 63 6f  alar function co
3a7ae 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  ntext */.  const
3a7af 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20   char *zName,   
3a7b0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
3a7b1 6f 66 20 49 43 55 20 66 75 6e 63 74 69 6f 6e 20  of ICU function 
3a7b2 74 68 61 74 20 66 61 69 6c 65 64 20 2a 2f 0a 20  that failed */. 
3a7b3 20 55 45 72 72 6f 72 43 6f 64 65 20 65 20 20 20   UErrorCode e   
3a7b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3a7b5 20 45 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75   Error code retu
3a7b6 72 6e 65 64 20 62 79 20 49 43 55 20 66 75 6e 63  rned by ICU func
3a7b7 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 63 68 61  tion */.){.  cha
3a7b8 72 20 7a 42 75 66 5b 31 32 38 5d 3b 0a 20 20 73  r zBuf[128];.  s
3a7b9 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
3a7ba 31 32 38 2c 20 7a 42 75 66 2c 20 22 49 43 55 20  128, zBuf, "ICU 
3a7bb 65 72 72 6f 72 3a 20 25 73 28 29 3a 20 25 73 22  error: %s(): %s"
3a7bc 2c 20 7a 4e 61 6d 65 2c 20 75 5f 65 72 72 6f 72  , zName, u_error
3a7bd 4e 61 6d 65 28 65 29 29 3b 0a 20 20 7a 42 75 66  Name(e));.  zBuf
3a7be 5b 31 32 37 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  [127] = '\0';.  
3a7bf 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
3a7c0 72 72 6f 72 28 70 43 74 78 2c 20 7a 42 75 66 2c  rror(pCtx, zBuf,
3a7c1 20 2d 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46   -1);.}../*.** F
3a7c2 75 6e 63 74 69 6f 6e 20 74 6f 20 64 65 6c 65 74  unction to delet
3a7c3 65 20 63 6f 6d 70 69 6c 65 64 20 72 65 67 65 78  e compiled regex
3a7c4 70 20 6f 62 6a 65 63 74 73 2e 20 52 65 67 69 73  p objects. Regis
3a7c5 74 65 72 65 64 20 61 73 0a 2a 2a 20 61 20 64 65  tered as.** a de
3a7c6 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f  structor functio
3a7c7 6e 20 77 69 74 68 20 73 71 6c 69 74 65 33 5f 73  n with sqlite3_s
3a7c8 65 74 5f 61 75 78 64 61 74 61 28 29 2e 0a 2a 2f  et_auxdata()..*/
3a7c9 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 63 75  .static void icu
3a7ca 52 65 67 65 78 70 44 65 6c 65 74 65 28 76 6f 69  RegexpDelete(voi
3a7cb 64 20 2a 70 29 7b 0a 20 20 55 52 65 67 75 6c 61  d *p){.  URegula
3a7cc 72 45 78 70 72 65 73 73 69 6f 6e 20 2a 70 45 78  rExpression *pEx
3a7cd 70 72 20 3d 20 28 55 52 65 67 75 6c 61 72 45 78  pr = (URegularEx
3a7ce 70 72 65 73 73 69 6f 6e 20 2a 29 70 3b 0a 20 20  pression *)p;.  
3a7cf 75 72 65 67 65 78 5f 63 6c 6f 73 65 28 70 45 78  uregex_close(pEx
3a7d0 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  pr);.}../*.** Im
3a7d1 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
3a7d2 53 51 4c 69 74 65 20 52 45 47 45 58 50 20 6f 70  SQLite REGEXP op
3a7d3 65 72 61 74 6f 72 2e 20 54 68 69 73 20 73 63 61  erator. This sca
3a7d4 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b  lar function tak
3a7d5 65 73 0a 2a 2a 20 74 77 6f 20 61 72 67 75 6d 65  es.** two argume
3a7d6 6e 74 73 2e 20 54 68 65 20 66 69 72 73 74 20 69  nts. The first i
3a7d7 73 20 61 20 72 65 67 75 6c 61 72 20 65 78 70 72  s a regular expr
3a7d8 65 73 73 69 6f 6e 20 70 61 74 74 65 72 6e 20 74  ession pattern t
3a7d9 6f 20 63 6f 6d 70 69 6c 65 0a 2a 2a 20 74 68 65  o compile.** the
3a7da 20 73 65 63 6f 6e 64 20 69 73 20 61 20 73 74 72   second is a str
3a7db 69 6e 67 20 74 6f 20 6d 61 74 63 68 20 61 67 61  ing to match aga
3a7dc 69 6e 73 74 20 74 68 61 74 20 70 61 74 74 65 72  inst that patter
3a7dd 6e 2e 20 49 66 20 65 69 74 68 65 72 20 0a 2a 2a  n. If either .**
3a7de 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20   argument is an 
3a7df 53 51 4c 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 4e  SQL NULL, then N
3a7e0 55 4c 4c 20 49 73 20 72 65 74 75 72 6e 65 64 2e  ULL Is returned.
3a7e1 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20   Otherwise, the 
3a7e2 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20 31 20 69  result.** is 1 i
3a7e3 66 20 74 68 65 20 73 74 72 69 6e 67 20 6d 61 74  f the string mat
3a7e4 63 68 65 73 20 74 68 65 20 70 61 74 74 65 72 6e  ches the pattern
3a7e5 2c 20 6f 72 20 30 20 6f 74 68 65 72 77 69 73 65  , or 0 otherwise
3a7e6 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6d  ..**.** SQLite m
3a7e7 61 70 73 20 74 68 65 20 72 65 67 65 78 70 28 29  aps the regexp()
3a7e8 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 65   function to the
3a7e9 20 72 65 67 65 78 70 28 29 20 6f 70 65 72 61 74   regexp() operat
3a7ea 6f 72 20 73 75 63 68 0a 2a 2a 20 74 68 61 74 20  or such.** that 
3a7eb 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77  the following tw
3a7ec 6f 20 61 72 65 20 65 71 75 69 76 61 6c 65 6e 74  o are equivalent
3a7ed 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 7a 53 74 72  :.**.**     zStr
3a7ee 69 6e 67 20 52 45 47 45 58 50 20 7a 50 61 74 74  ing REGEXP zPatt
3a7ef 65 72 6e 0a 2a 2a 20 20 20 20 20 72 65 67 65 78  ern.**     regex
3a7f0 70 28 7a 50 61 74 74 65 72 6e 2c 20 7a 53 74 72  p(zPattern, zStr
3a7f1 69 6e 67 29 0a 2a 2a 0a 2a 2a 20 55 73 65 73 20  ing).**.** Uses 
3a7f2 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 49 43  the following IC
3a7f3 55 20 72 65 67 65 78 70 20 41 50 49 73 3a 0a 2a  U regexp APIs:.*
3a7f4 2a 0a 2a 2a 20 20 20 20 20 75 72 65 67 65 78 5f  *.**     uregex_
3a7f5 6f 70 65 6e 28 29 0a 2a 2a 20 20 20 20 20 75 72  open().**     ur
3a7f6 65 67 65 78 5f 6d 61 74 63 68 65 73 28 29 0a 2a  egex_matches().*
3a7f7 2a 20 20 20 20 20 75 72 65 67 65 78 5f 63 6c 6f  *     uregex_clo
3a7f8 73 65 28 29 0a 2a 2f 0a 73 74 61 74 69 63 20 76  se().*/.static v
3a7f9 6f 69 64 20 69 63 75 52 65 67 65 78 70 46 75 6e  oid icuRegexpFun
3a7fa 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  c(sqlite3_contex
3a7fb 74 20 2a 70 2c 20 69 6e 74 20 6e 41 72 67 2c 20  t *p, int nArg, 
3a7fc 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
3a7fd 61 70 41 72 67 29 7b 0a 20 20 55 45 72 72 6f 72  apArg){.  UError
3a7fe 43 6f 64 65 20 73 74 61 74 75 73 20 3d 20 55 5f  Code status = U_
3a7ff 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 52  ZERO_ERROR;.  UR
3a800 65 67 75 6c 61 72 45 78 70 72 65 73 73 69 6f 6e  egularExpression
3a801 20 2a 70 45 78 70 72 3b 0a 20 20 55 42 6f 6f 6c   *pExpr;.  UBool
3a802 20 72 65 73 3b 0a 20 20 63 6f 6e 73 74 20 55 43   res;.  const UC
3a803 68 61 72 20 2a 7a 53 74 72 69 6e 67 20 3d 20 73  har *zString = s
3a804 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
3a805 74 31 36 28 61 70 41 72 67 5b 31 5d 29 3b 0a 0a  t16(apArg[1]);..
3a806 20 20 2f 2a 20 49 66 20 74 68 65 20 6c 65 66 74    /* If the left
3a807 20 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68   hand side of th
3a808 65 20 72 65 67 65 78 70 20 6f 70 65 72 61 74 6f  e regexp operato
3a809 72 20 69 73 20 4e 55 4c 4c 2c 20 0a 20 20 2a 2a  r is NULL, .  **
3a80a 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74   then the result
3a80b 20 69 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e 20 0a   is also NULL. .
3a80c 20 20 2a 2f 0a 20 20 69 66 28 20 21 7a 53 74 72    */.  if( !zStr
3a80d 69 6e 67 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ing ){.    retur
3a80e 6e 3b 0a 20 20 7d 0a 0a 20 20 70 45 78 70 72 20  n;.  }..  pExpr 
3a80f 3d 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75  = sqlite3_get_au
3a810 78 64 61 74 61 28 70 2c 20 30 29 3b 0a 20 20 69  xdata(p, 0);.  i
3a811 66 28 20 21 70 45 78 70 72 20 29 7b 0a 20 20 20  f( !pExpr ){.   
3a812 20 63 6f 6e 73 74 20 55 43 68 61 72 20 2a 7a 50   const UChar *zP
3a813 61 74 74 65 72 6e 20 3d 20 73 71 6c 69 74 65 33  attern = sqlite3
3a814 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 61 70  _value_text16(ap
3a815 41 72 67 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28  Arg[0]);.    if(
3a816 20 21 7a 50 61 74 74 65 72 6e 20 29 7b 0a 20 20   !zPattern ){.  
3a817 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
3a818 7d 0a 20 20 20 20 70 45 78 70 72 20 3d 20 75 72  }.    pExpr = ur
3a819 65 67 65 78 5f 6f 70 65 6e 28 7a 50 61 74 74 65  egex_open(zPatte
3a81a 72 6e 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 26 73  rn, -1, 0, 0, &s
3a81b 74 61 74 75 73 29 3b 0a 0a 20 20 20 20 69 66 28  tatus);..    if(
3a81c 20 55 5f 53 55 43 43 45 53 53 28 73 74 61 74 75   U_SUCCESS(statu
3a81d 73 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  s) ){.      sqli
3a81e 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28  te3_set_auxdata(
3a81f 70 2c 20 30 2c 20 70 45 78 70 72 2c 20 69 63 75  p, 0, pExpr, icu
3a820 52 65 67 65 78 70 44 65 6c 65 74 65 29 3b 0a 20  RegexpDelete);. 
3a821 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
3a822 61 73 73 65 72 74 28 21 70 45 78 70 72 29 3b 0a  assert(!pExpr);.
3a823 20 20 20 20 20 20 69 63 75 46 75 6e 63 74 69 6f        icuFunctio
3a824 6e 45 72 72 6f 72 28 70 2c 20 22 75 72 65 67 65  nError(p, "urege
3a825 78 5f 6f 70 65 6e 22 2c 20 73 74 61 74 75 73 29  x_open", status)
3a826 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
3a827 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
3a828 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 74 65  Configure the te
3a829 78 74 20 74 68 61 74 20 74 68 65 20 72 65 67 75  xt that the regu
3a82a 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 6f  lar expression o
3a82b 70 65 72 61 74 65 73 20 6f 6e 2e 20 2a 2f 0a 20  perates on. */. 
3a82c 20 75 72 65 67 65 78 5f 73 65 74 54 65 78 74 28   uregex_setText(
3a82d 70 45 78 70 72 2c 20 7a 53 74 72 69 6e 67 2c 20  pExpr, zString, 
3a82e 2d 31 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20  -1, &status);.  
3a82f 69 66 28 20 21 55 5f 53 55 43 43 45 53 53 28 73  if( !U_SUCCESS(s
3a830 74 61 74 75 73 29 20 29 7b 0a 20 20 20 20 69 63  tatus) ){.    ic
3a831 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 70  uFunctionError(p
3a832 2c 20 22 75 72 65 67 65 78 5f 73 65 74 54 65 78  , "uregex_setTex
3a833 74 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20  t", status);.   
3a834 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
3a835 2f 2a 20 41 74 74 65 6d 70 74 20 74 68 65 20 6d  /* Attempt the m
3a836 61 74 63 68 20 2a 2f 0a 20 20 72 65 73 20 3d 20  atch */.  res = 
3a837 75 72 65 67 65 78 5f 6d 61 74 63 68 65 73 28 70  uregex_matches(p
3a838 45 78 70 72 2c 20 30 2c 20 26 73 74 61 74 75 73  Expr, 0, &status
3a839 29 3b 0a 20 20 69 66 28 20 21 55 5f 53 55 43 43  );.  if( !U_SUCC
3a83a 45 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a 20  ESS(status) ){. 
3a83b 20 20 20 69 63 75 46 75 6e 63 74 69 6f 6e 45 72     icuFunctionEr
3a83c 72 6f 72 28 70 2c 20 22 75 72 65 67 65 78 5f 6d  ror(p, "uregex_m
3a83d 61 74 63 68 65 73 22 2c 20 73 74 61 74 75 73 29  atches", status)
3a83e 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
3a83f 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20  }..  /* Set the 
3a840 74 65 78 74 20 74 68 61 74 20 74 68 65 20 72 65  text that the re
3a841 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e  gular expression
3a842 20 6f 70 65 72 61 74 65 73 20 6f 6e 20 74 6f 20   operates on to 
3a843 61 20 4e 55 4c 4c 0a 20 20 2a 2a 20 70 6f 69 6e  a NULL.  ** poin
3a844 74 65 72 2e 20 54 68 69 73 20 69 73 20 6e 6f 74  ter. This is not
3a845 20 72 65 61 6c 6c 79 20 6e 65 63 65 73 73 61 72   really necessar
3a846 79 2c 20 62 75 74 20 69 74 20 69 73 20 74 69 64  y, but it is tid
3a847 69 65 72 20 74 68 61 6e 20 0a 20 20 2a 2a 20 6c  ier than .  ** l
3a848 65 61 76 69 6e 67 20 74 68 65 20 72 65 67 75 6c  eaving the regul
3a849 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 62  ar expression ob
3a84a 6a 65 63 74 20 63 6f 6e 66 69 67 75 72 65 64 20  ject configured 
3a84b 77 69 74 68 20 61 6e 20 69 6e 76 61 6c 69 64 0a  with an invalid.
3a84c 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 61 66 74    ** pointer aft
3a84d 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  er this function
3a84e 20 72 65 74 75 72 6e 73 2e 0a 20 20 2a 2f 0a 20   returns..  */. 
3a84f 20 75 72 65 67 65 78 5f 73 65 74 54 65 78 74 28   uregex_setText(
3a850 70 45 78 70 72 2c 20 30 2c 20 30 2c 20 26 73 74  pExpr, 0, 0, &st
3a851 61 74 75 73 29 3b 0a 0a 20 20 2f 2a 20 52 65 74  atus);..  /* Ret
3a852 75 72 6e 20 31 20 6f 72 20 30 2e 20 2a 2f 0a 20  urn 1 or 0. */. 
3a853 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
3a854 69 6e 74 28 70 2c 20 72 65 73 20 3f 20 31 20 3a  int(p, res ? 1 :
3a855 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d   0);.}../*.** Im
3a856 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66  plementations of
3a857 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
3a858 73 20 66 6f 72 20 63 61 73 65 20 6d 61 70 70 69  s for case mappi
3a859 6e 67 20 2d 20 75 70 70 65 72 28 29 20 61 6e 64  ng - upper() and
3a85a 20 0a 2a 2a 20 6c 6f 77 65 72 28 29 2e 20 46 75   .** lower(). Fu
3a85b 6e 63 74 69 6f 6e 20 75 70 70 65 72 28 29 20 63  nction upper() c
3a85c 6f 6e 76 65 72 74 73 20 69 74 73 20 69 6e 70 75  onverts its inpu
3a85d 74 20 74 6f 20 75 70 70 65 72 2d 63 61 73 65 20  t to upper-case 
3a85e 28 41 42 43 29 2e 0a 2a 2a 20 46 75 6e 63 74 69  (ABC)..** Functi
3a85f 6f 6e 20 6c 6f 77 65 72 28 29 20 63 6f 6e 76 65  on lower() conve
3a860 72 74 73 20 74 6f 20 6c 6f 77 65 72 2d 63 61 73  rts to lower-cas
3a861 65 20 28 61 62 63 29 2e 0a 2a 2a 0a 2a 2a 20 49  e (abc)..**.** I
3a862 43 55 20 70 72 6f 76 69 64 65 73 20 74 77 6f 20  CU provides two 
3a863 74 79 70 65 73 20 6f 66 20 63 61 73 65 20 6d 61  types of case ma
3a864 70 70 69 6e 67 2c 20 22 67 65 6e 65 72 61 6c 22  pping, "general"
3a865 20 63 61 73 65 20 6d 61 70 70 69 6e 67 20 61 6e   case mapping an
3a866 64 0a 2a 2a 20 22 6c 61 6e 67 75 61 67 65 20 73  d.** "language s
3a867 70 65 63 69 66 69 63 22 2e 20 52 65 66 65 72 20  pecific". Refer 
3a868 74 6f 20 49 43 55 20 64 6f 63 75 6d 65 6e 74 61  to ICU documenta
3a869 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 64 69 66  tion for the dif
3a86a 66 65 72 65 6e 63 65 73 0a 2a 2a 20 62 65 74 77  ferences.** betw
3a86b 65 65 6e 20 74 68 65 20 74 77 6f 2e 0a 2a 2a 0a  een the two..**.
3a86c 2a 2a 20 54 6f 20 75 74 69 6c 69 73 65 20 22 67  ** To utilise "g
3a86d 65 6e 65 72 61 6c 22 20 63 61 73 65 20 6d 61 70  eneral" case map
3a86e 70 69 6e 67 2c 20 74 68 65 20 75 70 70 65 72 28  ping, the upper(
3a86f 29 20 6f 72 20 6c 6f 77 65 72 28 29 20 73 63 61  ) or lower() sca
3a870 6c 61 72 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  lar .** function
3a871 73 20 61 72 65 20 69 6e 76 6f 6b 65 64 20 77 69  s are invoked wi
3a872 74 68 20 6f 6e 65 20 61 72 67 75 6d 65 6e 74 3a  th one argument:
3a873 0a 2a 2a 0a 2a 2a 20 20 20 20 20 75 70 70 65 72  .**.**     upper
3a874 28 27 41 42 43 27 29 20 2d 3e 20 27 61 62 63 27  ('ABC') -> 'abc'
3a875 0a 2a 2a 20 20 20 20 20 6c 6f 77 65 72 28 27 61  .**     lower('a
3a876 62 63 27 29 20 2d 3e 20 27 41 42 43 27 0a 2a 2a  bc') -> 'ABC'.**
3a877 0a 2a 2a 20 54 6f 20 61 63 63 65 73 73 20 49 43  .** To access IC
3a878 55 20 22 6c 61 6e 67 75 61 67 65 20 73 70 65 63  U "language spec
3a879 69 66 69 63 22 20 63 61 73 65 20 6d 61 70 70 69  ific" case mappi
3a87a 6e 67 2c 20 75 70 70 65 72 28 29 20 6f 72 20 6c  ng, upper() or l
3a87b 6f 77 65 72 28 29 0a 2a 2a 20 73 68 6f 75 6c 64  ower().** should
3a87c 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 69 74 68   be invoked with
3a87d 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 20   two arguments. 
3a87e 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
3a87f 65 6e 74 20 69 73 20 74 68 65 20 6e 61 6d 65 0a  ent is the name.
3a880 2a 2a 20 6f 66 20 74 68 65 20 6c 6f 63 61 6c 65  ** of the locale
3a881 20 74 6f 20 75 73 65 2e 20 50 61 73 73 69 6e 67   to use. Passing
3a882 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67   an empty string
3a883 20 28 22 22 29 20 6f 72 20 53 51 4c 20 4e 55 4c   ("") or SQL NUL
3a884 4c 20 76 61 6c 75 65 0a 2a 2a 20 61 73 20 74 68  L value.** as th
3a885 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
3a886 74 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  t is the same as
3a887 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 31 20   invoking the 1 
3a888 61 72 67 75 6d 65 6e 74 20 76 65 72 73 69 6f 6e  argument version
3a889 0a 2a 2a 20 6f 66 20 75 70 70 65 72 28 29 20 6f  .** of upper() o
3a88a 72 20 6c 6f 77 65 72 28 29 2e 0a 2a 2a 0a 2a 2a  r lower()..**.**
3a88b 20 20 20 20 20 6c 6f 77 65 72 28 27 49 27 2c 20       lower('I', 
3a88c 27 65 6e 5f 75 73 27 29 20 2d 3e 20 27 69 27 0a  'en_us') -> 'i'.
3a88d 2a 2a 20 20 20 20 20 6c 6f 77 65 72 28 27 49 27  **     lower('I'
3a88e 2c 20 27 74 72 5f 74 72 27 29 20 2d 3e 20 27 c4  , 'tr_tr') -> '.
3a88f b1 27 20 28 73 6d 61 6c 6c 20 64 6f 74 6c 65 73  .' (small dotles
3a890 73 20 69 29 0a 2a 2a 0a 2a 2a 20 68 74 74 70 3a  s i).**.** http:
3a891 2f 2f 77 77 77 2e 69 63 75 2d 70 72 6f 6a 65 63  //www.icu-projec
3a892 74 2e 6f 72 67 2f 75 73 65 72 67 75 69 64 65 2f  t.org/userguide/
3a893 70 6f 73 69 78 2e 68 74 6d 6c 23 63 61 73 65 5f  posix.html#case_
3a894 6d 61 70 70 69 6e 67 73 0a 2a 2f 0a 73 74 61 74  mappings.*/.stat
3a895 69 63 20 76 6f 69 64 20 69 63 75 43 61 73 65 46  ic void icuCaseF
3a896 75 6e 63 31 36 28 73 71 6c 69 74 65 33 5f 63 6f  unc16(sqlite3_co
3a897 6e 74 65 78 74 20 2a 70 2c 20 69 6e 74 20 6e 41  ntext *p, int nA
3a898 72 67 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  rg, sqlite3_valu
3a899 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20 63 6f  e **apArg){.  co
3a89a 6e 73 74 20 55 43 68 61 72 20 2a 7a 49 6e 70 75  nst UChar *zInpu
3a89b 74 3b 0a 20 20 55 43 68 61 72 20 2a 7a 4f 75 74  t;.  UChar *zOut
3a89c 70 75 74 3b 0a 20 20 69 6e 74 20 6e 49 6e 70 75  put;.  int nInpu
3a89d 74 3b 0a 20 20 69 6e 74 20 6e 4f 75 74 70 75 74  t;.  int nOutput
3a89e 3b 0a 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20  ;..  UErrorCode 
3a89f 73 74 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f  status = U_ZERO_
3a8a0 45 52 52 4f 52 3b 0a 20 20 63 6f 6e 73 74 20 63  ERROR;.  const c
3a8a1 68 61 72 20 2a 7a 4c 6f 63 61 6c 65 20 3d 20 30  har *zLocale = 0
3a8a2 3b 0a 0a 20 20 61 73 73 65 72 74 28 6e 41 72 67  ;..  assert(nArg
3a8a3 3d 3d 31 20 7c 7c 20 6e 41 72 67 3d 3d 32 29 3b  ==1 || nArg==2);
3a8a4 0a 20 20 69 66 28 20 6e 41 72 67 3d 3d 32 20 29  .  if( nArg==2 )
3a8a5 7b 0a 20 20 20 20 7a 4c 6f 63 61 6c 65 20 3d 20  {.    zLocale = 
3a8a6 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
3a8a7 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
3a8a8 28 61 70 41 72 67 5b 31 5d 29 3b 0a 20 20 7d 0a  (apArg[1]);.  }.
3a8a9 0a 20 20 7a 49 6e 70 75 74 20 3d 20 73 71 6c 69  .  zInput = sqli
3a8aa 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
3a8ab 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20 20 69 66  (apArg[0]);.  if
3a8ac 28 20 21 7a 49 6e 70 75 74 20 29 7b 0a 20 20 20  ( !zInput ){.   
3a8ad 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6e   return;.  }.  n
3a8ae 49 6e 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f  Input = sqlite3_
3a8af 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 61 70  value_bytes16(ap
3a8b0 41 72 67 5b 30 5d 29 3b 0a 0a 20 20 6e 4f 75 74  Arg[0]);..  nOut
3a8b1 70 75 74 20 3d 20 6e 49 6e 70 75 74 20 2a 20 32  put = nInput * 2
3a8b2 20 2b 20 32 3b 0a 20 20 7a 4f 75 74 70 75 74 20   + 2;.  zOutput 
3a8b3 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
3a8b4 28 6e 4f 75 74 70 75 74 29 3b 0a 20 20 69 66 28  (nOutput);.  if(
3a8b5 20 21 7a 4f 75 74 70 75 74 20 29 7b 0a 20 20 20   !zOutput ){.   
3a8b6 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
3a8b7 69 66 28 20 73 71 6c 69 74 65 33 5f 75 73 65 72  if( sqlite3_user
3a8b8 5f 64 61 74 61 28 70 29 20 29 7b 0a 20 20 20 20  _data(p) ){.    
3a8b9 75 5f 73 74 72 54 6f 55 70 70 65 72 28 7a 4f 75  u_strToUpper(zOu
3a8ba 74 70 75 74 2c 20 6e 4f 75 74 70 75 74 2f 32 2c  tput, nOutput/2,
3a8bb 20 7a 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2f   zInput, nInput/
3a8bc 32 2c 20 7a 4c 6f 63 61 6c 65 2c 20 26 73 74 61  2, zLocale, &sta
3a8bd 74 75 73 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  tus);.  }else{. 
3a8be 20 20 20 75 5f 73 74 72 54 6f 4c 6f 77 65 72 28     u_strToLower(
3a8bf 7a 4f 75 74 70 75 74 2c 20 6e 4f 75 74 70 75 74  zOutput, nOutput
3a8c0 2f 32 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e 70  /2, zInput, nInp
3a8c1 75 74 2f 32 2c 20 7a 4c 6f 63 61 6c 65 2c 20 26  ut/2, zLocale, &
3a8c2 73 74 61 74 75 73 29 3b 0a 20 20 7d 0a 0a 20 20  status);.  }..  
3a8c3 69 66 28 20 21 55 5f 53 55 43 43 45 53 53 28 73  if( !U_SUCCESS(s
3a8c4 74 61 74 75 73 29 20 29 7b 0a 20 20 20 20 69 63  tatus) ){.    ic
3a8c5 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 70  uFunctionError(p
3a8c6 2c 20 22 75 5f 73 74 72 54 6f 4c 6f 77 65 72 28  , "u_strToLower(
3a8c7 29 2f 75 5f 73 74 72 54 6f 55 70 70 65 72 22 2c  )/u_strToUpper",
3a8c8 20 73 74 61 74 75 73 29 3b 0a 20 20 20 20 72 65   status);.    re
3a8c9 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c  turn;.  }..  sql
3a8ca 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
3a8cb 31 36 28 70 2c 20 7a 4f 75 74 70 75 74 2c 20 2d  16(p, zOutput, -
3a8cc 31 2c 20 78 46 72 65 65 29 3b 0a 7d 0a 0a 2f 2a  1, xFree);.}../*
3a8cd 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65  .** Collation se
3a8ce 71 75 65 6e 63 65 20 64 65 73 74 72 75 63 74 6f  quence destructo
3a8cf 72 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20  r function. The 
3a8d0 70 43 74 78 20 61 72 67 75 6d 65 6e 74 20 70 6f  pCtx argument po
3a8d1 69 6e 74 73 20 74 6f 0a 2a 2a 20 61 20 55 43 6f  ints to.** a UCo
3a8d2 6c 6c 61 74 6f 72 20 73 74 72 75 63 74 75 72 65  llator structure
3a8d3 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f   previously allo
3a8d4 63 61 74 65 64 20 75 73 69 6e 67 20 75 63 6f 6c  cated using ucol
3a8d5 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74  _open()..*/.stat
3a8d6 69 63 20 76 6f 69 64 20 69 63 75 43 6f 6c 6c 61  ic void icuColla
3a8d7 74 69 6f 6e 44 65 6c 28 76 6f 69 64 20 2a 70 43  tionDel(void *pC
3a8d8 74 78 29 7b 0a 20 20 55 43 6f 6c 6c 61 74 6f 72  tx){.  UCollator
3a8d9 20 2a 70 20 3d 20 28 55 43 6f 6c 6c 61 74 6f 72   *p = (UCollator
3a8da 20 2a 29 70 43 74 78 3b 0a 20 20 75 63 6f 6c 5f   *)pCtx;.  ucol_
3a8db 63 6c 6f 73 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a  close(p);.}../*.
3a8dc 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ** Collation seq
3a8dd 75 65 6e 63 65 20 63 6f 6d 70 61 72 69 73 6f 6e  uence comparison
3a8de 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 70   function. The p
3a8df 43 74 78 20 61 72 67 75 6d 65 6e 74 20 70 6f 69  Ctx argument poi
3a8e0 6e 74 73 20 74 6f 0a 2a 2a 20 61 20 55 43 6f 6c  nts to.** a UCol
3a8e1 6c 61 74 6f 72 20 73 74 72 75 63 74 75 72 65 20  lator structure 
3a8e2 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63  previously alloc
3a8e3 61 74 65 64 20 75 73 69 6e 67 20 75 63 6f 6c 5f  ated using ucol_
3a8e4 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  open()..*/.stati
3a8e5 63 20 69 6e 74 20 69 63 75 43 6f 6c 6c 61 74 69  c int icuCollati
3a8e6 6f 6e 43 6f 6c 6c 28 0a 20 20 76 6f 69 64 20 2a  onColl(.  void *
3a8e7 70 43 74 78 2c 0a 20 20 69 6e 74 20 6e 4c 65 66  pCtx,.  int nLef
3a8e8 74 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  t,.  const void 
3a8e9 2a 7a 4c 65 66 74 2c 0a 20 20 69 6e 74 20 6e 52  *zLeft,.  int nR
3a8ea 69 67 68 74 2c 0a 20 20 63 6f 6e 73 74 20 76 6f  ight,.  const vo
3a8eb 69 64 20 2a 7a 52 69 67 68 74 0a 29 7b 0a 20 20  id *zRight.){.  
3a8ec 55 43 6f 6c 6c 61 74 69 6f 6e 52 65 73 75 6c 74  UCollationResult
3a8ed 20 72 65 73 3b 0a 20 20 55 43 6f 6c 6c 61 74 6f   res;.  UCollato
3a8ee 72 20 2a 70 20 3d 20 28 55 43 6f 6c 6c 61 74 6f  r *p = (UCollato
3a8ef 72 20 2a 29 70 43 74 78 3b 0a 20 20 72 65 73 20  r *)pCtx;.  res 
3a8f0 3d 20 75 63 6f 6c 5f 73 74 72 63 6f 6c 6c 28 70  = ucol_strcoll(p
3a8f1 2c 20 28 55 43 68 61 72 20 2a 29 7a 4c 65 66 74  , (UChar *)zLeft
3a8f2 2c 20 6e 4c 65 66 74 2f 32 2c 20 28 55 43 68 61  , nLeft/2, (UCha
3a8f3 72 20 2a 29 7a 52 69 67 68 74 2c 20 6e 52 69 67  r *)zRight, nRig
3a8f4 68 74 2f 32 29 3b 0a 20 20 73 77 69 74 63 68 28  ht/2);.  switch(
3a8f5 20 72 65 73 20 29 7b 0a 20 20 20 20 63 61 73 65   res ){.    case
3a8f6 20 55 43 4f 4c 5f 4c 45 53 53 3a 20 20 20 20 72   UCOL_LESS:    r
3a8f7 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 63 61  eturn -1;.    ca
3a8f8 73 65 20 55 43 4f 4c 5f 47 52 45 41 54 45 52 3a  se UCOL_GREATER:
3a8f9 20 72 65 74 75 72 6e 20 2b 31 3b 0a 20 20 20 20   return +1;.    
3a8fa 63 61 73 65 20 55 43 4f 4c 5f 45 51 55 41 4c 3a  case UCOL_EQUAL:
3a8fb 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
3a8fc 0a 20 20 61 73 73 65 72 74 28 21 22 55 6e 65 78  .  assert(!"Unex
3a8fd 70 65 63 74 65 64 20 72 65 74 75 72 6e 20 76 61  pected return va
3a8fe 6c 75 65 20 66 72 6f 6d 20 75 63 6f 6c 5f 73 74  lue from ucol_st
3a8ff 72 63 6f 6c 6c 28 29 22 29 3b 0a 20 20 72 65 74  rcoll()");.  ret
3a900 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
3a901 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
3a902 66 20 74 68 65 20 73 63 61 6c 61 72 20 66 75 6e  f the scalar fun
3a903 63 74 69 6f 6e 20 69 63 75 5f 6c 6f 61 64 5f 63  ction icu_load_c
3a904 6f 6c 6c 61 74 69 6f 6e 28 29 2e 0a 2a 2a 0a 2a  ollation()..**.*
3a905 2a 20 54 68 69 73 20 73 63 61 6c 61 72 20 66 75  * This scalar fu
3a906 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  nction is used t
3a907 6f 20 61 64 64 20 49 43 55 20 63 6f 6c 6c 61 74  o add ICU collat
3a908 69 6f 6e 20 62 61 73 65 64 20 63 6f 6c 6c 61 74  ion based collat
3a909 69 6f 6e 20 0a 2a 2a 20 74 79 70 65 73 20 74 6f  ion .** types to
3a90a 20 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62   an SQLite datab
3a90b 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  ase connection. 
3a90c 49 74 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74  It is intended t
3a90d 6f 20 62 65 20 63 61 6c 6c 65 64 0a 2a 2a 20 61  o be called.** a
3a90e 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
3a90f 20 20 20 20 20 53 45 4c 45 43 54 20 69 63 75 5f       SELECT icu_
3a910 6c 6f 61 64 5f 63 6f 6c 6c 61 74 69 6f 6e 28 3c  load_collation(<
3a911 6c 6f 63 61 6c 65 3e 2c 20 3c 63 6f 6c 6c 61 74  locale>, <collat
3a912 69 6f 6e 2d 6e 61 6d 65 3e 29 3b 0a 2a 2a 0a 2a  ion-name>);.**.*
3a913 2a 20 57 68 65 72 65 20 3c 6c 6f 63 61 6c 65 3e  * Where <locale>
3a914 20 69 73 20 61 20 73 74 72 69 6e 67 20 63 6f 6e   is a string con
3a915 74 61 69 6e 69 6e 67 20 61 6e 20 49 43 55 20 6c  taining an ICU l
3a916 6f 63 61 6c 65 20 69 64 65 6e 74 69 66 69 65 72  ocale identifier
3a917 20 28 69 2e 65 2e 0a 2a 2a 20 22 65 6e 5f 41 55   (i.e..** "en_AU
3a918 22 2c 20 22 74 72 5f 54 52 22 20 65 74 63 2e 29  ", "tr_TR" etc.)
3a919 20 61 6e 64 20 3c 63 6f 6c 6c 61 74 69 6f 6e 2d   and <collation-
3a91a 6e 61 6d 65 3e 20 69 73 20 74 68 65 20 6e 61 6d  name> is the nam
3a91b 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6c 6c  e of the.** coll
3a91c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 74  ation sequence t
3a91d 6f 20 63 72 65 61 74 65 2e 0a 2a 2f 0a 73 74 61  o create..*/.sta
3a91e 74 69 63 20 76 6f 69 64 20 69 63 75 4c 6f 61 64  tic void icuLoad
3a91f 43 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71 6c  Collation(.  sql
3a920 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 2c  ite3_context *p,
3a921 20 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 0a 20   .  int nArg, . 
3a922 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
3a923 2a 61 70 41 72 67 0a 29 7b 0a 20 20 73 71 6c 69  *apArg.){.  sqli
3a924 74 65 33 20 2a 64 62 20 3d 20 28 73 71 6c 69 74  te3 *db = (sqlit
3a925 65 33 20 2a 29 73 71 6c 69 74 65 33 5f 75 73 65  e3 *)sqlite3_use
3a926 72 5f 64 61 74 61 28 70 29 3b 0a 20 20 55 45 72  r_data(p);.  UEr
3a927 72 6f 72 43 6f 64 65 20 73 74 61 74 75 73 20 3d  rorCode status =
3a928 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20   U_ZERO_ERROR;. 
3a929 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 6f   const char *zLo
3a92a 63 61 6c 65 3b 20 20 20 20 20 20 2f 2a 20 4c 6f  cale;      /* Lo
3a92b 63 61 6c 65 20 69 64 65 6e 74 69 66 69 65 72 20  cale identifier 
3a92c 2d 20 28 65 67 2e 20 22 6a 70 5f 4a 50 22 29 20  - (eg. "jp_JP") 
3a92d 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
3a92e 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 2f  *zName;        /
3a92f 2a 20 53 51 4c 20 43 6f 6c 6c 61 74 69 6f 6e 20  * SQL Collation 
3a930 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 20 28 65  sequence name (e
3a931 67 2e 20 22 6a 61 70 61 6e 65 73 65 22 29 20 2a  g. "japanese") *
3a932 2f 0a 20 20 55 43 6f 6c 6c 61 74 6f 72 20 2a 70  /.  UCollator *p
3a933 55 43 6f 6c 6c 61 74 6f 72 3b 20 20 20 20 2f 2a  UCollator;    /*
3a934 20 49 43 55 20 6c 69 62 72 61 72 79 20 63 6f 6c   ICU library col
3a935 6c 61 74 69 6f 6e 20 6f 62 6a 65 63 74 20 2a 2f  lation object */
3a936 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
3a937 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a938 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d  Return code from
3a939 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
3a93a 63 6f 6c 6c 61 74 69 6f 6e 5f 78 28 29 20 2a 2f  collation_x() */
3a93b 0a 0a 20 20 61 73 73 65 72 74 28 6e 41 72 67 3d  ..  assert(nArg=
3a93c 3d 32 29 3b 0a 20 20 7a 4c 6f 63 61 6c 65 20 3d  =2);.  zLocale =
3a93d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
3a93e 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
3a93f 74 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20 20 7a  t(apArg[0]);.  z
3a940 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63 68  Name = (const ch
3a941 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
3a942 75 65 5f 74 65 78 74 28 61 70 41 72 67 5b 31 5d  ue_text(apArg[1]
3a943 29 3b 0a 0a 20 20 69 66 28 20 21 7a 4c 6f 63 61  );..  if( !zLoca
3a944 6c 65 20 7c 7c 20 21 7a 4e 61 6d 65 20 29 7b 0a  le || !zName ){.
3a945 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
3a946 0a 20 20 70 55 43 6f 6c 6c 61 74 6f 72 20 3d 20  .  pUCollator = 
3a947 75 63 6f 6c 5f 6f 70 65 6e 28 7a 4c 6f 63 61 6c  ucol_open(zLocal
3a948 65 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20 69  e, &status);.  i
3a949 66 28 20 21 55 5f 53 55 43 43 45 53 53 28 73 74  f( !U_SUCCESS(st
3a94a 61 74 75 73 29 20 29 7b 0a 20 20 20 20 69 63 75  atus) ){.    icu
3a94b 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 70 2c  FunctionError(p,
3a94c 20 22 75 63 6f 6c 5f 6f 70 65 6e 22 2c 20 73 74   "ucol_open", st
3a94d 61 74 75 73 29 3b 0a 20 20 20 20 72 65 74 75 72  atus);.    retur
3a94e 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  n;.  }.  assert(
3a94f 70 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  p);..  rc = sqli
3a950 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
3a951 74 69 6f 6e 5f 76 32 28 64 62 2c 20 7a 4e 61 6d  tion_v2(db, zNam
3a952 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 2c  e, SQLITE_UTF16,
3a953 20 28 76 6f 69 64 20 2a 29 70 55 43 6f 6c 6c 61   (void *)pUColla
3a954 74 6f 72 2c 20 0a 20 20 20 20 20 20 69 63 75 43  tor, .      icuC
3a955 6f 6c 6c 61 74 69 6f 6e 43 6f 6c 6c 2c 20 69 63  ollationColl, ic
3a956 75 43 6f 6c 6c 61 74 69 6f 6e 44 65 6c 0a 20 20  uCollationDel.  
3a957 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
3a958 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 63  ITE_OK ){.    uc
3a959 6f 6c 5f 63 6c 6f 73 65 28 70 55 43 6f 6c 6c 61  ol_close(pUColla
3a95a 74 6f 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  tor);.    sqlite
3a95b 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70  3_result_error(p
3a95c 2c 20 22 45 72 72 6f 72 20 72 65 67 69 73 74 65  , "Error registe
3a95d 72 69 6e 67 20 63 6f 6c 6c 61 74 69 6f 6e 20 66  ring collation f
3a95e 75 6e 63 74 69 6f 6e 22 2c 20 2d 31 29 3b 0a 20  unction", -1);. 
3a95f 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69   }.}../*.** Regi
3a960 73 74 65 72 20 74 68 65 20 49 43 55 20 65 78 74  ster the ICU ext
3a961 65 6e 73 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73  ension functions
3a962 20 77 69 74 68 20 64 61 74 61 62 61 73 65 20 64   with database d
3a963 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  b..*/.SQLITE_PRI
3a964 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
3a965 49 63 75 49 6e 69 74 28 73 71 6c 69 74 65 33 20  IcuInit(sqlite3 
3a966 2a 64 62 29 7b 0a 20 20 73 74 72 75 63 74 20 49  *db){.  struct I
3a967 63 75 53 63 61 6c 61 72 20 7b 0a 20 20 20 20 63  cuScalar {.    c
3a968 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
3a969 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3a96a 20 20 20 20 20 20 20 20 20 2f 2a 20 46 75 6e 63           /* Func
3a96b 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20  tion name */.   
3a96c 20 69 6e 74 20 6e 41 72 67 3b 20 20 20 20 20 20   int nArg;      
3a96d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a96e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
3a96f 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
3a970 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 65 6e 63  s */.    int enc
3a971 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3a972 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a973 20 20 20 2f 2a 20 4f 70 74 69 6d 61 6c 20 74 65     /* Optimal te
3a974 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20  xt encoding */. 
3a975 20 20 20 76 6f 69 64 20 2a 70 43 6f 6e 74 65 78     void *pContex
3a976 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
3a977 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a978 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
3a979 61 28 29 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  a() context */. 
3a97a 20 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29     void (*xFunc)
3a97b 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
3a97c 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
3a97d 6c 75 65 2a 2a 29 3b 0a 20 20 7d 20 73 63 61 6c  lue**);.  } scal
3a97e 61 72 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 22  ars[] = {.    {"
3a97f 72 65 67 65 78 70 22 2c 2d 31 2c 20 53 51 4c 49  regexp",-1, SQLI
3a980 54 45 5f 41 4e 59 2c 20 20 20 20 20 20 20 20 20  TE_ANY,         
3a981 20 30 2c 20 69 63 75 52 65 67 65 78 70 46 75 6e   0, icuRegexpFun
3a982 63 7d 2c 0a 0a 20 20 20 20 7b 22 6c 6f 77 65 72  c},..    {"lower
3a983 22 2c 20 20 31 2c 20 53 51 4c 49 54 45 5f 55 54  ",  1, SQLITE_UT
3a984 46 31 36 2c 20 20 20 20 20 20 20 20 30 2c 20 69  F16,        0, i
3a985 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20  cuCaseFunc16},. 
3a986 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20 20 32 2c     {"lower",  2,
3a987 20 53 51 4c 49 54 45 5f 55 54 46 31 36 2c 20 20   SQLITE_UTF16,  
3a988 20 20 20 20 20 20 30 2c 20 69 63 75 43 61 73 65        0, icuCase
3a989 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22 75  Func16},.    {"u
3a98a 70 70 65 72 22 2c 20 20 31 2c 20 53 51 4c 49 54  pper",  1, SQLIT
3a98b 45 5f 55 54 46 31 36 2c 20 28 76 6f 69 64 2a 29  E_UTF16, (void*)
3a98c 31 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36  1, icuCaseFunc16
3a98d 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65 72 22 2c  },.    {"upper",
3a98e 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 31    2, SQLITE_UTF1
3a98f 36 2c 20 28 76 6f 69 64 2a 29 31 2c 20 69 63 75  6, (void*)1, icu
3a990 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 0a 20 20  CaseFunc16},..  
3a991 20 20 7b 22 6c 6f 77 65 72 22 2c 20 20 31 2c 20    {"lower",  1, 
3a992 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20  SQLITE_UTF8,    
3a993 20 20 20 20 20 30 2c 20 69 63 75 43 61 73 65 46       0, icuCaseF
3a994 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22 6c 6f  unc16},.    {"lo
3a995 77 65 72 22 2c 20 20 32 2c 20 53 51 4c 49 54 45  wer",  2, SQLITE
3a996 5f 55 54 46 38 2c 20 20 20 20 20 20 20 20 20 30  _UTF8,         0
3a997 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d  , icuCaseFunc16}
3a998 2c 0a 20 20 20 20 7b 22 75 70 70 65 72 22 2c 20  ,.    {"upper", 
3a999 20 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   1, SQLITE_UTF8,
3a99a 20 20 28 76 6f 69 64 2a 29 31 2c 20 69 63 75 43    (void*)1, icuC
3a99b 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20  aseFunc16},.    
3a99c 7b 22 75 70 70 65 72 22 2c 20 20 32 2c 20 53 51  {"upper",  2, SQ
3a99d 4c 49 54 45 5f 55 54 46 38 2c 20 20 28 76 6f 69  LITE_UTF8,  (voi
3a99e 64 2a 29 31 2c 20 69 63 75 43 61 73 65 46 75 6e  d*)1, icuCaseFun
3a99f 63 31 36 7d 2c 0a 0a 20 20 20 20 7b 22 6c 69 6b  c16},..    {"lik
3a9a0 65 22 2c 20 20 20 32 2c 20 53 51 4c 49 54 45 5f  e",   2, SQLITE_
3a9a1 55 54 46 38 2c 20 20 20 20 20 20 20 20 20 30 2c  UTF8,         0,
3a9a2 20 69 63 75 4c 69 6b 65 46 75 6e 63 7d 2c 0a 20   icuLikeFunc},. 
3a9a3 20 20 20 7b 22 6c 69 6b 65 22 2c 20 20 20 33 2c     {"like",   3,
3a9a4 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20   SQLITE_UTF8,   
3a9a5 20 20 20 20 20 20 30 2c 20 69 63 75 4c 69 6b 65        0, icuLike
3a9a6 46 75 6e 63 7d 2c 0a 0a 20 20 20 20 7b 22 69 63  Func},..    {"ic
3a9a7 75 5f 6c 6f 61 64 5f 63 6f 6c 6c 61 74 69 6f 6e  u_load_collation
3a9a8 22 2c 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54  ",  2, SQLITE_UT
3a9a9 46 38 2c 20 28 76 6f 69 64 2a 29 64 62 2c 20 69  F8, (void*)db, i
3a9aa 63 75 4c 6f 61 64 43 6f 6c 6c 61 74 69 6f 6e 7d  cuLoadCollation}
3a9ab 2c 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 72 63  ,.  };..  int rc
3a9ac 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
3a9ad 69 6e 74 20 69 3b 0a 0a 20 20 66 6f 72 28 69 3d  int i;..  for(i=
3a9ae 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  0; rc==SQLITE_OK
3a9af 20 26 26 20 69 3c 28 73 69 7a 65 6f 66 28 73 63   && i<(sizeof(sc
3a9b0 61 6c 61 72 73 29 2f 73 69 7a 65 6f 66 28 73 74  alars)/sizeof(st
3a9b1 72 75 63 74 20 49 63 75 53 63 61 6c 61 72 29 29  ruct IcuScalar))
3a9b2 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75  ; i++){.    stru
3a9b3 63 74 20 49 63 75 53 63 61 6c 61 72 20 2a 70 20  ct IcuScalar *p 
3a9b4 3d 20 26 73 63 61 6c 61 72 73 5b 69 5d 3b 0a 20  = &scalars[i];. 
3a9b5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
3a9b6 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
3a9b7 0a 20 20 20 20 20 20 20 20 64 62 2c 20 70 2d 3e  .        db, p->
3a9b8 7a 4e 61 6d 65 2c 20 70 2d 3e 6e 41 72 67 2c 20  zName, p->nArg, 
3a9b9 70 2d 3e 65 6e 63 2c 20 70 2d 3e 70 43 6f 6e 74  p->enc, p->pCont
3a9ba 65 78 74 2c 20 70 2d 3e 78 46 75 6e 63 2c 20 30  ext, p->xFunc, 0
3a9bb 2c 20 30 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a  , 0.    );.  }..
3a9bc 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
3a9bd 23 69 66 20 21 53 51 4c 49 54 45 5f 43 4f 52 45  #if !SQLITE_CORE
3a9be 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
3a9bf 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f  sqlite3_extensio
3a9c0 6e 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74 65  n_init(.  sqlite
3a9c1 33 20 2a 64 62 2c 20 0a 20 20 63 68 61 72 20 2a  3 *db, .  char *
3a9c2 2a 70 7a 45 72 72 4d 73 67 2c 0a 20 20 63 6f 6e  *pzErrMsg,.  con
3a9c3 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  st sqlite3_api_r
3a9c4 6f 75 74 69 6e 65 73 20 2a 70 41 70 69 0a 29 7b  outines *pApi.){
3a9c5 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53  .  SQLITE_EXTENS
3a9c6 49 4f 4e 5f 49 4e 49 54 32 28 70 41 70 69 29 0a  ION_INIT2(pApi).
3a9c7 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
3a9c8 49 63 75 49 6e 69 74 28 64 62 29 3b 0a 7d 0a 23  IcuInit(db);.}.#
3a9c9 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f  endif..#endif../
3a9ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
3a9cb 6e 64 20 6f 66 20 69 63 75 2e 63 20 2a 2a 2a 2a  nd of icu.c ****
3a9cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a9cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a9ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
3a9cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
3a9d0 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 69  egin file fts3_i
3a9d1 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  cu.c ***********
3a9d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a9d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
3a9d4 2a 0a 2a 2a 20 32 30 30 37 20 4a 75 6e 65 20 32  *.** 2007 June 2
3a9d5 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
3a9d6 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
3a9d7 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
3a9d8 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
3a9d9 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
3a9da 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
3a9db 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
3a9dc 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
3a9dd 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
3a9de 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
3a9df 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
3a9e0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
3a9e1 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
3a9e2 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
3a9e3 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
3a9e4 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
3a9e5 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
3a9e6 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
3a9e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a9e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a9e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a9ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a9eb 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d  .** This file im
3a9ec 70 6c 65 6d 65 6e 74 73 20 61 20 74 6f 6b 65 6e  plements a token
3a9ed 69 7a 65 72 20 66 6f 72 20 66 74 73 33 20 62 61  izer for fts3 ba
3a9ee 73 65 64 20 6f 6e 20 74 68 65 20 49 43 55 20 6c  sed on the ICU l
3a9ef 69 62 72 61 72 79 2e 0a 2a 2a 20 0a 2a 2a 20 24  ibrary..** .** $
3a9f0 49 64 3a 20 66 74 73 33 5f 69 63 75 2e 63 2c 76  Id: fts3_icu.c,v
3a9f1 20 31 2e 33 20 32 30 30 38 2f 30 39 2f 30 31 20   1.3 2008/09/01 
3a9f2 31 38 3a 33 34 3a 32 30 20 64 61 6e 69 65 6c 6b  18:34:20 danielk
3a9f3 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23  1977 Exp $.*/..#
3a9f4 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
3a9f5 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69  TE_CORE) || defi
3a9f6 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
3a9f7 45 5f 46 54 53 33 29 0a 23 69 66 64 65 66 20 53  E_FTS3).#ifdef S
3a9f8 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55  QLITE_ENABLE_ICU
3a9f9 0a 0a 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69  ...#include <uni
3a9fa 63 6f 64 65 2f 75 62 72 6b 2e 68 3e 0a 23 69 6e  code/ubrk.h>.#in
3a9fb 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75  clude <unicode/u
3a9fc 74 66 31 36 2e 68 3e 0a 0a 74 79 70 65 64 65 66  tf16.h>..typedef
3a9fd 20 73 74 72 75 63 74 20 49 63 75 54 6f 6b 65 6e   struct IcuToken
3a9fe 69 7a 65 72 20 49 63 75 54 6f 6b 65 6e 69 7a 65  izer IcuTokenize
3a9ff 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  r;.typedef struc
3aa00 74 20 49 63 75 43 75 72 73 6f 72 20 49 63 75 43  t IcuCursor IcuC
3aa01 75 72 73 6f 72 3b 0a 0a 73 74 72 75 63 74 20 49  ursor;..struct I
3aa02 63 75 54 6f 6b 65 6e 69 7a 65 72 20 7b 0a 20 20  cuTokenizer {.  
3aa03 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
3aa04 72 20 62 61 73 65 3b 0a 20 20 63 68 61 72 20 2a  r base;.  char *
3aa05 7a 4c 6f 63 61 6c 65 3b 0a 7d 3b 0a 0a 73 74 72  zLocale;.};..str
3aa06 75 63 74 20 49 63 75 43 75 72 73 6f 72 20 7b 0a  uct IcuCursor {.
3aa07 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
3aa08 7a 65 72 5f 63 75 72 73 6f 72 20 62 61 73 65 3b  zer_cursor base;
3aa09 0a 0a 20 20 55 42 72 65 61 6b 49 74 65 72 61 74  ..  UBreakIterat
3aa0a 6f 72 20 2a 70 49 74 65 72 3b 20 20 20 20 20 20  or *pIter;      
3aa0b 2f 2a 20 49 43 55 20 62 72 65 61 6b 2d 69 74 65  /* ICU break-ite
3aa0c 72 61 74 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0a  rator object */.
3aa0d 20 20 69 6e 74 20 6e 43 68 61 72 3b 20 20 20 20    int nChar;    
3aa0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3aa0f 20 4e 75 6d 62 65 72 20 6f 66 20 55 43 68 61 72   Number of UChar
3aa10 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 70 49 6e   elements in pIn
3aa11 70 75 74 20 2a 2f 0a 20 20 55 43 68 61 72 20 2a  put */.  UChar *
3aa12 61 43 68 61 72 3b 20 20 20 20 20 20 20 20 20 20  aChar;          
3aa13 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20       /* Copy of 
3aa14 69 6e 70 75 74 20 75 73 69 6e 67 20 75 74 66 2d  input using utf-
3aa15 31 36 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20  16 encoding */. 
3aa16 20 69 6e 74 20 2a 61 4f 66 66 73 65 74 3b 20 20   int *aOffset;  
3aa17 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3aa18 4f 66 66 73 65 74 73 20 6f 66 20 65 61 63 68 20  Offsets of each 
3aa19 63 68 61 72 61 63 74 65 72 20 69 6e 20 75 74 66  character in utf
3aa1a 2d 38 20 69 6e 70 75 74 20 2a 2f 0a 0a 20 20 69  -8 input */..  i
3aa1b 6e 74 20 6e 42 75 66 66 65 72 3b 0a 20 20 63 68  nt nBuffer;.  ch
3aa1c 61 72 20 2a 7a 42 75 66 66 65 72 3b 0a 0a 20 20  ar *zBuffer;..  
3aa1d 69 6e 74 20 69 54 6f 6b 65 6e 3b 0a 7d 3b 0a 0a  int iToken;.};..
3aa1e 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e  /*.** Create a n
3aa1f 65 77 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 73  ew tokenizer ins
3aa20 74 61 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  tance..*/.static
3aa21 20 69 6e 74 20 69 63 75 43 72 65 61 74 65 28 0a   int icuCreate(.
3aa22 20 20 69 6e 74 20 61 72 67 63 2c 20 20 20 20 20    int argc,     
3aa23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3aa24 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
3aa25 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
3aa26 72 67 76 5b 5d 20 2a 2f 0a 20 20 63 6f 6e 73 74  rgv[] */.  const
3aa27 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a 61   char * const *a
3aa28 72 67 76 2c 20 20 20 20 20 20 20 20 20 20 20 20  rgv,            
3aa29 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 63 72 65  /* Tokenizer cre
3aa2a 61 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20  ation arguments 
3aa2b 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  */.  sqlite3_tok
3aa2c 65 6e 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 65 6e  enizer **ppToken
3aa2d 69 7a 65 72 20 20 20 20 20 20 2f 2a 20 4f 55 54  izer      /* OUT
3aa2e 3a 20 43 72 65 61 74 65 64 20 74 6f 6b 65 6e 69  : Created tokeni
3aa2f 7a 65 72 20 2a 2f 0a 29 7b 0a 20 20 49 63 75 54  zer */.){.  IcuT
3aa30 6f 6b 65 6e 69 7a 65 72 20 2a 70 3b 0a 20 20 69  okenizer *p;.  i
3aa31 6e 74 20 6e 20 3d 20 30 3b 0a 0a 20 20 69 66 28  nt n = 0;..  if(
3aa32 20 61 72 67 63 3e 30 20 29 7b 0a 20 20 20 20 6e   argc>0 ){.    n
3aa33 20 3d 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 30   = strlen(argv[0
3aa34 5d 29 2b 31 3b 0a 20 20 7d 0a 20 20 70 20 3d 20  ])+1;.  }.  p = 
3aa35 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 29  (IcuTokenizer *)
3aa36 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
3aa37 69 7a 65 6f 66 28 49 63 75 54 6f 6b 65 6e 69 7a  izeof(IcuTokeniz
3aa38 65 72 29 2b 6e 29 3b 0a 20 20 69 66 28 20 21 70  er)+n);.  if( !p
3aa39 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
3aa3a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
3aa3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20  .  memset(p, 0, 
3aa3c 73 69 7a 65 6f 66 28 49 63 75 54 6f 6b 65 6e 69  sizeof(IcuTokeni
3aa3d 7a 65 72 29 29 3b 0a 0a 20 20 69 66 28 20 6e 20  zer));..  if( n 
3aa3e 29 7b 0a 20 20 20 20 70 2d 3e 7a 4c 6f 63 61 6c  ){.    p->zLocal
3aa3f 65 20 3d 20 28 63 68 61 72 20 2a 29 26 70 5b 31  e = (char *)&p[1
3aa40 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 2d  ];.    memcpy(p-
3aa41 3e 7a 4c 6f 63 61 6c 65 2c 20 61 72 67 76 5b 30  >zLocale, argv[0
3aa42 5d 2c 20 6e 29 3b 0a 20 20 7d 0a 0a 20 20 2a 70  ], n);.  }..  *p
3aa43 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 28 73 71  pTokenizer = (sq
3aa44 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
3aa45 2a 29 70 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53  *)p;..  return S
3aa46 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
3aa47 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 74 6f 6b  ** Destroy a tok
3aa48 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63  enizer.*/.static
3aa49 20 69 6e 74 20 69 63 75 44 65 73 74 72 6f 79 28   int icuDestroy(
3aa4a 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
3aa4b 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 29 7b 0a  r *pTokenizer){.
3aa4c 20 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a    IcuTokenizer *
3aa4d 70 20 3d 20 28 49 63 75 54 6f 6b 65 6e 69 7a 65  p = (IcuTokenize
3aa4e 72 20 2a 29 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a  r *)pTokenizer;.
3aa4f 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
3aa50 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
3aa51 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
3aa52 50 72 65 70 61 72 65 20 74 6f 20 62 65 67 69 6e  Prepare to begin
3aa53 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 20 70 61   tokenizing a pa
3aa54 72 74 69 63 75 6c 61 72 20 73 74 72 69 6e 67 2e  rticular string.
3aa55 20 20 54 68 65 20 69 6e 70 75 74 0a 2a 2a 20 73    The input.** s
3aa56 74 72 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b 65  tring to be toke
3aa57 6e 69 7a 65 64 20 69 73 20 70 49 6e 70 75 74 5b  nized is pInput[
3aa58 30 2e 2e 6e 42 79 74 65 73 2d 31 5d 2e 20 20 41  0..nBytes-1].  A
3aa59 20 63 75 72 73 6f 72 0a 2a 2a 20 75 73 65 64 20   cursor.** used 
3aa5a 74 6f 20 69 6e 63 72 65 6d 65 6e 74 61 6c 6c 79  to incrementally
3aa5b 20 74 6f 6b 65 6e 69 7a 65 20 74 68 69 73 20 73   tokenize this s
3aa5c 74 72 69 6e 67 20 69 73 20 72 65 74 75 72 6e 65  tring is returne
3aa5d 64 20 69 6e 20 0a 2a 2a 20 2a 70 70 43 75 72 73  d in .** *ppCurs
3aa5e 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  or..*/.static in
3aa5f 74 20 69 63 75 4f 70 65 6e 28 0a 20 20 73 71 6c  t icuOpen(.  sql
3aa60 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
3aa61 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20  pTokenizer,     
3aa62 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e      /* The token
3aa63 69 7a 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  izer */.  const 
3aa64 63 68 61 72 20 2a 7a 49 6e 70 75 74 2c 20 20 20  char *zInput,   
3aa65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3aa66 20 2f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67   /* Input string
3aa67 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 70 75 74   */.  int nInput
3aa68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3aa69 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3aa6a 4c 65 6e 67 74 68 20 6f 66 20 7a 49 6e 70 75 74  Length of zInput
3aa6b 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 73   in bytes */.  s
3aa6c 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3aa6d 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73  _cursor **ppCurs
3aa6e 6f 72 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f  or    /* OUT: To
3aa6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f  kenization curso
3aa70 72 20 2a 2f 0a 29 7b 0a 20 20 49 63 75 54 6f 6b  r */.){.  IcuTok
3aa71 65 6e 69 7a 65 72 20 2a 70 20 3d 20 28 49 63 75  enizer *p = (Icu
3aa72 54 6f 6b 65 6e 69 7a 65 72 20 2a 29 70 54 6f 6b  Tokenizer *)pTok
3aa73 65 6e 69 7a 65 72 3b 0a 20 20 49 63 75 43 75 72  enizer;.  IcuCur
3aa74 73 6f 72 20 2a 70 43 73 72 3b 0a 0a 20 20 63 6f  sor *pCsr;..  co
3aa75 6e 73 74 20 69 6e 74 33 32 5f 74 20 6f 70 74 20  nst int32_t opt 
3aa76 3d 20 55 5f 46 4f 4c 44 5f 43 41 53 45 5f 44 45  = U_FOLD_CASE_DE
3aa77 46 41 55 4c 54 3b 0a 20 20 55 45 72 72 6f 72 43  FAULT;.  UErrorC
3aa78 6f 64 65 20 73 74 61 74 75 73 20 3d 20 55 5f 5a  ode status = U_Z
3aa79 45 52 4f 5f 45 52 52 4f 52 3b 0a 20 20 69 6e 74  ERO_ERROR;.  int
3aa7a 20 6e 43 68 61 72 3b 0a 0a 20 20 55 43 68 61 72   nChar;..  UChar
3aa7b 33 32 20 63 3b 0a 20 20 69 6e 74 20 69 49 6e 70  32 c;.  int iInp
3aa7c 75 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 4f  ut = 0;.  int iO
3aa7d 75 74 20 3d 20 30 3b 0a 0a 20 20 2a 70 70 43 75  ut = 0;..  *ppCu
3aa7e 72 73 6f 72 20 3d 20 30 3b 0a 0a 20 20 69 66 28  rsor = 0;..  if(
3aa7f 20 6e 49 6e 70 75 74 3c 30 20 29 7b 0a 20 20 20   nInput<0 ){.   
3aa80 20 6e 49 6e 70 75 74 20 3d 20 73 74 72 6c 65 6e   nInput = strlen
3aa81 28 7a 49 6e 70 75 74 29 3b 0a 20 20 7d 0a 20 20  (zInput);.  }.  
3aa82 6e 43 68 61 72 20 3d 20 6e 49 6e 70 75 74 2b 31  nChar = nInput+1
3aa83 3b 0a 20 20 70 43 73 72 20 3d 20 28 49 63 75 43  ;.  pCsr = (IcuC
3aa84 75 72 73 6f 72 20 2a 29 73 71 6c 69 74 65 33 5f  ursor *)sqlite3_
3aa85 6d 61 6c 6c 6f 63 28 0a 20 20 20 20 20 20 73 69  malloc(.      si
3aa86 7a 65 6f 66 28 49 63 75 43 75 72 73 6f 72 29 20  zeof(IcuCursor) 
3aa87 2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  +               
3aa88 20 2f 2a 20 49 63 75 43 75 72 73 6f 72 20 2a 2f   /* IcuCursor */
3aa89 0a 20 20 20 20 20 20 6e 43 68 61 72 20 2a 20 73  .      nChar * s
3aa8a 69 7a 65 6f 66 28 55 43 68 61 72 29 20 2b 20 20  izeof(UChar) +  
3aa8b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 63 75            /* Icu
3aa8c 43 75 72 73 6f 72 2e 61 43 68 61 72 5b 5d 20 2a  Cursor.aChar[] *
3aa8d 2f 0a 20 20 20 20 20 20 28 6e 43 68 61 72 2b 31  /.      (nChar+1
3aa8e 29 20 2a 20 73 69 7a 65 6f 66 28 69 6e 74 29 20  ) * sizeof(int) 
3aa8f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 63             /* Ic
3aa90 75 43 75 72 73 6f 72 2e 61 4f 66 66 73 65 74 5b  uCursor.aOffset[
3aa91 5d 20 2a 2f 0a 20 20 29 3b 0a 20 20 69 66 28 20  ] */.  );.  if( 
3aa92 21 70 43 73 72 20 29 7b 0a 20 20 20 20 72 65 74  !pCsr ){.    ret
3aa93 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
3aa94 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70  ;.  }.  memset(p
3aa95 43 73 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 49  Csr, 0, sizeof(I
3aa96 63 75 43 75 72 73 6f 72 29 29 3b 0a 20 20 70 43  cuCursor));.  pC
3aa97 73 72 2d 3e 61 43 68 61 72 20 3d 20 28 55 43 68  sr->aChar = (UCh
3aa98 61 72 20 2a 29 26 70 43 73 72 5b 31 5d 3b 0a 20  ar *)&pCsr[1];. 
3aa99 20 70 43 73 72 2d 3e 61 4f 66 66 73 65 74 20 3d   pCsr->aOffset =
3aa9a 20 28 69 6e 74 20 2a 29 26 70 43 73 72 2d 3e 61   (int *)&pCsr->a
3aa9b 43 68 61 72 5b 6e 43 68 61 72 5d 3b 0a 0a 20 20  Char[nChar];..  
3aa9c 70 43 73 72 2d 3e 61 4f 66 66 73 65 74 5b 69 4f  pCsr->aOffset[iO
3aa9d 75 74 5d 20 3d 20 69 49 6e 70 75 74 3b 0a 20 20  ut] = iInput;.  
3aa9e 55 38 5f 4e 45 58 54 28 7a 49 6e 70 75 74 2c 20  U8_NEXT(zInput, 
3aa9f 69 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20  iInput, nInput, 
3aaa0 63 29 3b 20 0a 20 20 77 68 69 6c 65 28 20 63 3e  c); .  while( c>
3aaa1 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 73 45  0 ){.    int isE
3aaa2 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20 20 63 20  rror = 0;.    c 
3aaa3 3d 20 75 5f 66 6f 6c 64 43 61 73 65 28 63 2c 20  = u_foldCase(c, 
3aaa4 6f 70 74 29 3b 0a 20 20 20 20 55 31 36 5f 41 50  opt);.    U16_AP
3aaa5 50 45 4e 44 28 70 43 73 72 2d 3e 61 43 68 61 72  PEND(pCsr->aChar
3aaa6 2c 20 69 4f 75 74 2c 20 6e 43 68 61 72 2c 20 63  , iOut, nChar, c
3aaa7 2c 20 69 73 45 72 72 6f 72 29 3b 0a 20 20 20 20  , isError);.    
3aaa8 69 66 28 20 69 73 45 72 72 6f 72 20 29 7b 0a 20  if( isError ){. 
3aaa9 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
3aaaa 65 28 70 43 73 72 29 3b 0a 20 20 20 20 20 20 72  e(pCsr);.      r
3aaab 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
3aaac 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43  OR;.    }.    pC
3aaad 73 72 2d 3e 61 4f 66 66 73 65 74 5b 69 4f 75 74  sr->aOffset[iOut
3aaae 5d 20 3d 20 69 49 6e 70 75 74 3b 0a 0a 20 20 20  ] = iInput;..   
3aaaf 20 69 66 28 20 69 49 6e 70 75 74 3c 6e 49 6e 70   if( iInput<nInp
3aab0 75 74 20 29 7b 0a 20 20 20 20 20 20 55 38 5f 4e  ut ){.      U8_N
3aab1 45 58 54 28 7a 49 6e 70 75 74 2c 20 69 49 6e 70  EXT(zInput, iInp
3aab2 75 74 2c 20 6e 49 6e 70 75 74 2c 20 63 29 3b 0a  ut, nInput, c);.
3aab3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3aab4 20 63 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20   c = 0;.    }.  
3aab5 7d 0a 0a 20 20 70 43 73 72 2d 3e 70 49 74 65 72  }..  pCsr->pIter
3aab6 20 3d 20 75 62 72 6b 5f 6f 70 65 6e 28 55 42 52   = ubrk_open(UBR
3aab7 4b 5f 57 4f 52 44 2c 20 70 2d 3e 7a 4c 6f 63 61  K_WORD, p->zLoca
3aab8 6c 65 2c 20 70 43 73 72 2d 3e 61 43 68 61 72 2c  le, pCsr->aChar,
3aab9 20 69 4f 75 74 2c 20 26 73 74 61 74 75 73 29 3b   iOut, &status);
3aaba 0a 20 20 69 66 28 20 21 55 5f 53 55 43 43 45 53  .  if( !U_SUCCES
3aabb 53 28 73 74 61 74 75 73 29 20 29 7b 0a 20 20 20  S(status) ){.   
3aabc 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
3aabd 73 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  sr);.    return 
3aabe 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
3aabf 7d 0a 20 20 70 43 73 72 2d 3e 6e 43 68 61 72 20  }.  pCsr->nChar 
3aac0 3d 20 69 4f 75 74 3b 0a 0a 20 20 75 62 72 6b 5f  = iOut;..  ubrk_
3aac1 66 69 72 73 74 28 70 43 73 72 2d 3e 70 49 74 65  first(pCsr->pIte
3aac2 72 29 3b 0a 20 20 2a 70 70 43 75 72 73 6f 72 20  r);.  *ppCursor 
3aac3 3d 20 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  = (sqlite3_token
3aac4 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 70 43  izer_cursor *)pC
3aac5 73 72 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  sr;.  return SQL
3aac6 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
3aac7 20 43 6c 6f 73 65 20 61 20 74 6f 6b 65 6e 69 7a   Close a tokeniz
3aac8 61 74 69 6f 6e 20 63 75 72 73 6f 72 20 70 72 65  ation cursor pre
3aac9 76 69 6f 75 73 6c 79 20 6f 70 65 6e 65 64 20 62  viously opened b
3aaca 79 20 61 20 63 61 6c 6c 20 74 6f 20 69 63 75 4f  y a call to icuO
3aacb 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  pen()..*/.static
3aacc 20 69 6e 74 20 69 63 75 43 6c 6f 73 65 28 73 71   int icuClose(sq
3aacd 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
3aace 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29  cursor *pCursor)
3aacf 7b 0a 20 20 49 63 75 43 75 72 73 6f 72 20 2a 70  {.  IcuCursor *p
3aad0 43 73 72 20 3d 20 28 49 63 75 43 75 72 73 6f 72  Csr = (IcuCursor
3aad1 20 2a 29 70 43 75 72 73 6f 72 3b 0a 20 20 75 62   *)pCursor;.  ub
3aad2 72 6b 5f 63 6c 6f 73 65 28 70 43 73 72 2d 3e 70  rk_close(pCsr->p
3aad3 49 74 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  Iter);.  sqlite3
3aad4 5f 66 72 65 65 28 70 43 73 72 2d 3e 7a 42 75 66  _free(pCsr->zBuf
3aad5 66 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  fer);.  sqlite3_
3aad6 66 72 65 65 28 70 43 73 72 29 3b 0a 20 20 72 65  free(pCsr);.  re
3aad7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
3aad8 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74  }../*.** Extract
3aad9 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20   the next token 
3aada 66 72 6f 6d 20 61 20 74 6f 6b 65 6e 69 7a 61 74  from a tokenizat
3aadb 69 6f 6e 20 63 75 72 73 6f 72 2e 0a 2a 2f 0a 73  ion cursor..*/.s
3aadc 74 61 74 69 63 20 69 6e 74 20 69 63 75 4e 65 78  tatic int icuNex
3aadd 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  t(.  sqlite3_tok
3aade 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70  enizer_cursor *p
3aadf 43 75 72 73 6f 72 2c 20 20 2f 2a 20 43 75 72 73  Cursor,  /* Curs
3aae0 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  or returned by s
3aae1 69 6d 70 6c 65 4f 70 65 6e 20 2a 2f 0a 20 20 63  impleOpen */.  c
3aae2 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 70 54 6f  onst char **ppTo
3aae3 6b 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  ken,            
3aae4 20 20 20 2f 2a 20 4f 55 54 3a 20 2a 70 70 54 6f     /* OUT: *ppTo
3aae5 6b 65 6e 20 69 73 20 74 68 65 20 74 6f 6b 65 6e  ken is the token
3aae6 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 2a   text */.  int *
3aae7 70 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20  pnBytes,        
3aae8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3aae9 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66  * OUT: Number of
3aaea 20 62 79 74 65 73 20 69 6e 20 74 6f 6b 65 6e 20   bytes in token 
3aaeb 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 53 74 61 72  */.  int *piStar
3aaec 74 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20  tOffset,        
3aaed 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
3aaee 20 53 74 61 72 74 69 6e 67 20 6f 66 66 73 65 74   Starting offset
3aaef 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69   of token */.  i
3aaf0 6e 74 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c  nt *piEndOffset,
3aaf1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3aaf2 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 69 6e     /* OUT: Endin
3aaf3 67 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65  g offset of toke
3aaf4 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 50 6f  n */.  int *piPo
3aaf5 73 69 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20  sition          
3aaf6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
3aaf7 54 3a 20 50 6f 73 69 74 69 6f 6e 20 69 6e 74 65  T: Position inte
3aaf8 67 65 72 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a  ger of token */.
3aaf9 29 7b 0a 20 20 49 63 75 43 75 72 73 6f 72 20 2a  ){.  IcuCursor *
3aafa 70 43 73 72 20 3d 20 28 49 63 75 43 75 72 73 6f  pCsr = (IcuCurso
3aafb 72 20 2a 29 70 43 75 72 73 6f 72 3b 0a 0a 20 20  r *)pCursor;..  
3aafc 69 6e 74 20 69 53 74 61 72 74 20 3d 20 30 3b 0a  int iStart = 0;.
3aafd 20 20 69 6e 74 20 69 45 6e 64 20 3d 20 30 3b 0a    int iEnd = 0;.
3aafe 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 30 3b    int nByte = 0;
3aaff 0a 0a 20 20 77 68 69 6c 65 28 20 69 53 74 61 72  ..  while( iStar
3ab00 74 3d 3d 69 45 6e 64 20 29 7b 0a 20 20 20 20 55  t==iEnd ){.    U
3ab01 43 68 61 72 33 32 20 63 3b 0a 0a 20 20 20 20 69  Char32 c;..    i
3ab02 53 74 61 72 74 20 3d 20 75 62 72 6b 5f 63 75 72  Start = ubrk_cur
3ab03 72 65 6e 74 28 70 43 73 72 2d 3e 70 49 74 65 72  rent(pCsr->pIter
3ab04 29 3b 0a 20 20 20 20 69 45 6e 64 20 3d 20 75 62  );.    iEnd = ub
3ab05 72 6b 5f 6e 65 78 74 28 70 43 73 72 2d 3e 70 49  rk_next(pCsr->pI
3ab06 74 65 72 29 3b 0a 20 20 20 20 69 66 28 20 69 45  ter);.    if( iE
3ab07 6e 64 3d 3d 55 42 52 4b 5f 44 4f 4e 45 20 29 7b  nd==UBRK_DONE ){
3ab08 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
3ab09 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d  LITE_DONE;.    }
3ab0a 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 69 53 74  ..    while( iSt
3ab0b 61 72 74 3c 69 45 6e 64 20 29 7b 0a 20 20 20 20  art<iEnd ){.    
3ab0c 20 20 69 6e 74 20 69 57 68 69 74 65 20 3d 20 69    int iWhite = i
3ab0d 53 74 61 72 74 3b 0a 20 20 20 20 20 20 55 38 5f  Start;.      U8_
3ab0e 4e 45 58 54 28 70 43 73 72 2d 3e 61 43 68 61 72  NEXT(pCsr->aChar
3ab0f 2c 20 69 57 68 69 74 65 2c 20 70 43 73 72 2d 3e  , iWhite, pCsr->
3ab10 6e 43 68 61 72 2c 20 63 29 3b 0a 20 20 20 20 20  nChar, c);.     
3ab11 20 69 66 28 20 75 5f 69 73 73 70 61 63 65 28 63   if( u_isspace(c
3ab12 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 53 74  ) ){.        iSt
3ab13 61 72 74 20 3d 20 69 57 68 69 74 65 3b 0a 20 20  art = iWhite;.  
3ab14 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3ab15 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3ab16 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  }.    }.    asse
3ab17 72 74 28 69 53 74 61 72 74 3c 3d 69 45 6e 64 29  rt(iStart<=iEnd)
3ab18 3b 0a 20 20 7d 0a 0a 20 20 64 6f 20 7b 0a 20 20  ;.  }..  do {.  
3ab19 20 20 55 45 72 72 6f 72 43 6f 64 65 20 73 74 61    UErrorCode sta
3ab1a 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52  tus = U_ZERO_ERR
3ab1b 4f 52 3b 0a 20 20 20 20 69 66 28 20 6e 42 79 74  OR;.    if( nByt
3ab1c 65 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20  e ){.      char 
3ab1d 2a 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f  *zNew = sqlite3_
3ab1e 72 65 61 6c 6c 6f 63 28 70 43 73 72 2d 3e 7a 42  realloc(pCsr->zB
3ab1f 75 66 66 65 72 2c 20 6e 42 79 74 65 29 3b 0a 20  uffer, nByte);. 
3ab20 20 20 20 20 20 69 66 28 20 21 7a 4e 65 77 20 29       if( !zNew )
3ab21 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
3ab22 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
3ab23 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 73       }.      pCs
3ab24 72 2d 3e 7a 42 75 66 66 65 72 20 3d 20 7a 4e 65  r->zBuffer = zNe
3ab25 77 3b 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 6e  w;.      pCsr->n
3ab26 42 75 66 66 65 72 20 3d 20 6e 42 79 74 65 3b 0a  Buffer = nByte;.
3ab27 20 20 20 20 7d 0a 0a 20 20 20 20 75 5f 73 74 72      }..    u_str
3ab28 54 6f 55 54 46 38 28 0a 20 20 20 20 20 20 20 20  ToUTF8(.        
3ab29 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 2c 20 70  pCsr->zBuffer, p
3ab2a 43 73 72 2d 3e 6e 42 75 66 66 65 72 2c 20 26 6e  Csr->nBuffer, &n
3ab2b 42 79 74 65 2c 20 20 20 20 2f 2a 20 4f 75 74 70  Byte,    /* Outp
3ab2c 75 74 20 76 61 72 73 20 2a 2f 0a 20 20 20 20 20  ut vars */.     
3ab2d 20 20 20 26 70 43 73 72 2d 3e 61 43 68 61 72 5b     &pCsr->aChar[
3ab2e 69 53 74 61 72 74 5d 2c 20 69 45 6e 64 2d 69 53  iStart], iEnd-iS
3ab2f 74 61 72 74 2c 20 20 20 20 20 20 20 2f 2a 20 49  tart,       /* I
3ab30 6e 70 75 74 20 76 61 72 73 20 2a 2f 0a 20 20 20  nput vars */.   
3ab31 20 20 20 20 20 26 73 74 61 74 75 73 20 20 20 20       &status    
3ab32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ab33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3ab34 20 4f 75 74 70 75 74 20 73 75 63 63 65 73 73 2f   Output success/
3ab35 66 61 69 6c 75 72 65 20 2a 2f 0a 20 20 20 20 29  failure */.    )
3ab36 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e 42 79  ;.  } while( nBy
3ab37 74 65 3e 70 43 73 72 2d 3e 6e 42 75 66 66 65 72  te>pCsr->nBuffer
3ab38 20 29 3b 0a 0a 20 20 2a 70 70 54 6f 6b 65 6e 20   );..  *ppToken 
3ab39 3d 20 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 3b  = pCsr->zBuffer;
3ab3a 0a 20 20 2a 70 6e 42 79 74 65 73 20 3d 20 6e 42  .  *pnBytes = nB
3ab3b 79 74 65 3b 0a 20 20 2a 70 69 53 74 61 72 74 4f  yte;.  *piStartO
3ab3c 66 66 73 65 74 20 3d 20 70 43 73 72 2d 3e 61 4f  ffset = pCsr->aO
3ab3d 66 66 73 65 74 5b 69 53 74 61 72 74 5d 3b 0a 20  ffset[iStart];. 
3ab3e 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 20 3d 20   *piEndOffset = 
3ab3f 70 43 73 72 2d 3e 61 4f 66 66 73 65 74 5b 69 45  pCsr->aOffset[iE
3ab40 6e 64 5d 3b 0a 20 20 2a 70 69 50 6f 73 69 74 69  nd];.  *piPositi
3ab41 6f 6e 20 3d 20 70 43 73 72 2d 3e 69 54 6f 6b 65  on = pCsr->iToke
3ab42 6e 2b 2b 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53  n++;..  return S
3ab43 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
3ab44 2a 2a 20 54 68 65 20 73 65 74 20 6f 66 20 72 6f  ** The set of ro
3ab45 75 74 69 6e 65 73 20 74 68 61 74 20 69 6d 70 6c  utines that impl
3ab46 65 6d 65 6e 74 20 74 68 65 20 73 69 6d 70 6c 65  ement the simple
3ab47 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74   tokenizer.*/.st
3ab48 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
3ab49 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
3ab4a 75 6c 65 20 69 63 75 54 6f 6b 65 6e 69 7a 65 72  ule icuTokenizer
3ab4b 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c 20  Module = {.  0, 
3ab4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ab4d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65            /* iVe
3ab4e 72 73 69 6f 6e 20 2a 2f 0a 20 20 69 63 75 43 72  rsion */.  icuCr
3ab4f 65 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  eate,           
3ab50 20 20 20 20 20 20 20 20 2f 2a 20 78 43 72 65 61          /* xCrea
3ab51 74 65 20 20 2a 2f 0a 20 20 69 63 75 44 65 73 74  te  */.  icuDest
3ab52 72 6f 79 2c 20 20 20 20 20 20 20 20 20 20 20 20  roy,            
3ab53 20 20 20 20 20 20 2f 2a 20 78 43 72 65 61 74 65        /* xCreate
3ab54 20 20 2a 2f 0a 20 20 69 63 75 4f 70 65 6e 2c 20    */.  icuOpen, 
3ab55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ab56 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 20 20 20      /* xOpen    
3ab57 2a 2f 0a 20 20 69 63 75 43 6c 6f 73 65 2c 20 20  */.  icuClose,  
3ab58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ab59 20 20 2f 2a 20 78 43 6c 6f 73 65 20 20 20 2a 2f    /* xClose   */
3ab5a 0a 20 20 69 63 75 4e 65 78 74 2c 20 20 20 20 20  .  icuNext,     
3ab5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ab5c 2f 2a 20 78 4e 65 78 74 20 20 20 20 2a 2f 0a 7d  /* xNext    */.}
3ab5d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 70  ;../*.** Set *pp
3ab5e 4d 6f 64 75 6c 65 20 74 6f 20 70 6f 69 6e 74 20  Module to point 
3ab5f 61 74 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  at the implement
3ab60 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 49 43 55  ation of the ICU
3ab61 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2f 0a 53   tokenizer..*/.S
3ab62 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
3ab63 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 49 63  id sqlite3Fts3Ic
3ab64 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  uTokenizerModule
3ab65 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  (.  sqlite3_toke
3ab66 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e  nizer_module con
3ab67 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 0a 29 7b 0a  st**ppModule.){.
3ab68 20 20 2a 70 70 4d 6f 64 75 6c 65 20 3d 20 26 69    *ppModule = &i
3ab69 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  cuTokenizerModul
3ab6a 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  e;.}..#endif /* 
3ab6b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
3ab6c 4e 41 42 4c 45 5f 49 43 55 29 20 2a 2f 0a 23 65  NABLE_ICU) */.#e
3ab6d 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
3ab6e 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c  (SQLITE_CORE) ||
3ab6f 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
3ab70 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a  ENABLE_FTS3) */.
3ab71 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3ab72 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 69 63 75   End of fts3_icu
3ab73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
3ab74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ab75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3ab76 0a                                               .